9

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) ?

  • What about indent-according-to-mode command? – Andrey Tykhonov Nov 6 '14 at 22:24
1

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)))
  • 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 2: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)))
  • Well, those 3 dots are sth I am not sure how to fill... But thanks for partial pointer. – Mekk Nov 7 '14 at 9:23

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.