11

In my makefiles, I prefer the following indentation for continuation lines:

FILES:=                \
    file1.cpp          \
    file2.cpp          \
    fileYetAnother.cpp

LIBS:=                 \
    libsth1.so         \
    libelsewhere.so

still, makefile mode indents it in the following way (when asked to reindent file or region):

FILES:=            \
file1.cpp          \
file2.cpp          \
fileYetAnother.cpp

LIBS:=             \
libsth1.so         \
libelsewhere.so

Is it possible to configure it somehow to use the former variant (= indent continuation lines by 4 spaces or tab) ?

Mekk
  • 1,017
  • 7
  • 14

3 Answers3

2

Building on purple_arrows' solution:

(defun my-makefile-indent-line ()
  (save-excursion
(forward-line 0)
(cond
 ;; keep TABs
 ((looking-at "\t")
  t)
 ;; indent continuation lines to 4
 ((and (not (bobp))
       (= (char-before (1- (point))) ?\\))
  (delete-horizontal-space)
  (indent-to 4))
 ;; delete all other leading whitespace
 ((looking-at "\\s-+")
  (replace-match "")))))

(add-hook 'makefile-mode-hook
      (lambda ()
    (setq-local indent-line-function 'my-makefile-indent-line)))
Alex Schröder
  • 356
  • 1
  • 4
  • The only problem is that this will not work if your file list is indented by TABs, because my code leaves those untouched. – Alex Schröder Nov 17 '14 at 11:06
  • fix the indentation to 4 is not a good solution, what if the `FILES` and `LIBS` are long, and there is one element after `:=`, and you better align the following elements with the first one after `:=` ? – CodyChan Apr 29 '15 at 02:22
  • Four spaces is what the question asked. – Alex Schröder May 15 '15 at 19:46
0

Yes. It is possible to configure it, somehow.

(With apologies for the snark.)

Write a function that indents a line the way you want, then set that function as the value of the variable indent-line-function for makefile-mode. Something like:

(defun my-makefile-indent-line ()
  ...)

(add-hook 'makefile-mode-hook (lambda () (setq-local indent-line-function 'my-makefile-indent-line)))
purple_arrows
  • 2,373
  • 10
  • 19
  • Well, those 3 dots are sth I am not sure how to fill... But thanks for partial pointer. – Mekk Nov 07 '14 at 09:23
0

In case you're using aggressive-indent-mode, it helped me to put makefile-mode onto the list of aggressive-indent-excluded-modes:

(global-aggressive-indent-mode)
(add-to-list 'aggressive-indent-excluded-modes 'makefile-mode)

Note that this only works with global-aggressive-indent-mode turned on.

Timm
  • 1,549
  • 12
  • 23