1 ;;; makefile.el --- makefile editing commands for Emacs
3 ;; Copyright (C) 1992, 1994 Free Software Foundation, Inc.
5 ;; Author: Thomas Neumann <tom@smart.bo.open.de>
6 ;; Eric S. Raymond <esr@snark.thyrsus.com>
8 ;; Keywords: unix, tools
12 ;; Also, the doc strings need fixing: the first line doesn't stand alone,
13 ;; and other usage is not high quality. Symbol names don't have `...'.
15 ;; So, for the meantime, this is not the default mode for makefiles.
17 ;; $Id: makefile.el,v 1.18 1994/10/12 08:55:27 simon Exp rms $
19 ;; This file is part of GNU Emacs.
21 ;; GNU Emacs is free software; you can redistribute it and/or modify
22 ;; it under the terms of the GNU General Public License as published by
23 ;; the Free Software Foundation; either version 1, or (at your option)
26 ;; GNU Emacs is distributed in the hope that it will be useful,
27 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
28 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
29 ;; GNU General Public License for more details.
31 ;; You should have received a copy of the GNU General Public License
32 ;; along with GNU Emacs; see the file COPYING. If not, write to
33 ;; the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
37 ;; A major mode for editing makefiles. The mode knows about Makefile
38 ;; syntax and defines M-n and M-p to move to next and previous productions.
40 ;; The keys $, =, : and . are electric; they try to help you fill in a
41 ;; macro reference, macro definition, ordinary target name, or special
42 ;; target name, respectively. Such names are completed using a list of
43 ;; targets and macro names parsed out of the makefile. This list is
44 ;; automatically updated, if necessary, whenever you invoke one of
45 ;; these commands. You can force it to be updated with C-c C-p.
47 ;; The command C-c C-f adds certain filenames in the current directory
48 ;; as targets. You can filter out filenames by setting the variable
49 ;; makefile-ignored-files-in-pickup-regex.
51 ;; The command C-c C-u grinds for a bit, then pops up a report buffer
52 ;; showing which target names are up-to-date with respect to their
53 ;; prerequisites, which targets are out-of-date, and which have no
56 ;; The command C-c C-b pops up a browser window listing all target and
57 ;; macro names. You can mark or unmark items wit C-c SPC, and insert
58 ;; all marked items back in the Makefile with C-c TAB.
60 ;; The command C-c TAB in the makefile buffer inserts a GNU make builtin.
61 ;; You will be prompted for the builtin's args.
63 ;; There are numerous other customization variables.
68 ;; * makefile-backslash-region should be given better behavior.
69 ;; * Consider binding C-c C-c to comment-region (like cc-mode).
70 ;; * Eliminate electric stuff entirely.
71 ;; * It might be nice to highlight targets differently depending on
72 ;; whether they are up-to-date or not. Not sure how this would
73 ;; interact with font-lock.
74 ;; * Would be nice to edit the commands in ksh-mode and have
75 ;; indentation and slashification done automatically. Hard.
76 ;; * Consider removing browser mode. It seems useless.
77 ;; * ":" should notice when a new target is made and add it to the
78 ;; list (or at least set makefile-need-target-pickup).
79 ;; * Make browser into a major mode.
80 ;; * Clean up macro insertion stuff. It is a mess.
81 ;; * Browser entry and exit is weird. Normalize.
82 ;; * Browser needs to be rewritten. Right now it is kind of a crock.
84 ;; * Act more like dired/buffer menu/whatever.
85 ;; * Highlight as mouse traverses.
87 ;; * Update documentation above.
88 ;; * Update texinfo manual.
97 ;; Sadly we need this for a macro.
101 ;;; ------------------------------------------------------------
102 ;;; Configurable stuff
103 ;;; ------------------------------------------------------------
105 (defvar makefile-browser-buffer-name "*Macros and Targets*"
106 "Name of the macro- and target browser buffer.")
108 (defvar makefile-target-colon ":"
109 "String to append to all target names inserted by `makefile-insert-target'.
110 \":\" or \"::\" are common values.")
112 (defvar makefile-macro-assign " = "
113 "String to append to all macro names inserted by `makefile-insert-macro'.
114 The normal value should be \" = \", since this is what
115 standard make expects. However, newer makes such as dmake
116 allow a larger variety of different macro assignments, so you
117 might prefer to use \" += \" or \" := \" .")
119 (defvar makefile-electric-keys nil
120 "If non-nil, install electric keybindings.
123 (defvar makefile-use-curly-braces-for-macros-p nil
124 "Controls the style of generated macro references.
125 t (actually non-nil) means macro references should use curly braces,
127 nil means use parentheses, like `$(this)'.")
129 (defvar makefile-tab-after-target-colon t
130 "If non-nil, insert a TAB after a target colon.
131 Otherwise, a space is inserted.
134 (defvar makefile-browser-leftmost-column 10
135 "Number of blanks to the left of the browser selection mark.")
137 (defvar makefile-browser-cursor-column 10
138 "Column in which the cursor is positioned when it moves
139 up or down in the browser.")
141 (defvar makefile-backslash-column 48
142 "*Column in which `makefile-backslash-region' inserts backslashes.")
144 (defvar makefile-browser-selected-mark "+ "
145 "String used to mark selected entries in the browser.")
147 (defvar makefile-browser-unselected-mark " "
148 "String used to mark unselected entries in the browser.")
150 (defvar makefile-browser-auto-advance-after-selection-p t
151 "If non-nil, cursor will move after item is selected in browser.")
153 (defvar makefile-pickup-everything-picks-up-filenames-p nil
154 "If non-nil, `makefile-pickup-everything' picks up filenames as targets.
155 \(i.e. it calls `makefile-find-filenames-as-targets').
156 Otherwise filenames are omitted.")
158 (defvar makefile-cleanup-continuations-p t
159 "If non-nil, automatically clean up continuation lines when saving.
160 A line is cleaned up by removing all whitespace following a trailing
161 backslash. This is done silently.
162 IMPORTANT: Please note that enabling this option causes makefile-mode
163 to MODIFY A FILE WITHOUT YOUR CONFIRMATION when \'it seems necessary\'.")
165 (defvar makefile-browser-hook '())
168 ;; Special targets for DMake, Sun's make ...
170 (defvar makefile-special-targets-list
171 '(("DEFAULT") ("DONE") ("ERROR") ("EXPORT")
172 ("FAILED") ("GROUPEPILOG") ("GROUPPROLOG") ("IGNORE")
173 ("IMPORT") ("INCLUDE") ("INCLUDEDIRS") ("INIT")
174 ("KEEP_STATE") ("MAKEFILES") ("MAKE_VERSION") ("NO_PARALLEL")
175 ("PARALLEL") ("PHONY") ("PRECIOUS") ("REMOVE")
176 ("SCCS_GET") ("SILENT") ("SOURCE") ("SUFFIXES")
177 ("WAIT") ("c.o") ("C.o") ("m.o")
178 ("el.elc") ("y.c") ("s.o"))
179 "List of special targets.
180 You will be offered to complete on one of those in the minibuffer whenever
181 you enter a \".\" at the beginning of a line in makefile-mode.")
183 (defvar makefile-runtime-macros-list
184 '(("@") ("&") (">") ("<") ("*") ("^") ("?") ("%") ("$"))
185 "List of macros that are resolved by make at runtime.
186 If you insert a macro reference using makefile-insert-macro-ref, the name
187 of the macro is checked against this list. If it can be found its name will
188 not be enclosed in { } or ( ).")
190 ;; Note that the first big subexpression is used by font lock. Note
191 ;; that if you change this regexp you must fix the imenu index
192 ;; function defined at the end of the file.
193 (defconst makefile-dependency-regex
194 "^\\([^ \n\t#:]+\\([ \t]+[^ \t\n#:]+\\)*\\)[ \t]*:\\([ \t]*$\\|\\([^=\n].*$\\)\\)"
195 "Regex used to find dependency lines in a makefile.")
197 ;; Note that the first subexpression is used by font lock. Note that
198 ;; if you change this regexp you must fix the imenu index function
199 ;; defined at the end of the file.
200 (defconst makefile-macroassign-regex
201 "^\\([^ \n\t][^:#= \t\n]*\\)[ \t]*[*:+]?:?="
202 "Regex used to find macro assignment lines in a makefile.")
204 (defconst makefile-ignored-files-in-pickup-regex
205 "\\(^\\..*\\)\\|\\(.*~$\\)\\|\\(.*,v$\\)\\|\\(\\.[chy]\\)"
206 "Regex for filenames that will NOT be included in the target list.")
208 ;; TABs are important in Makefiles. So highlight them in font-lock.
209 ;; Thanks to Job Ganzevoort <Job.Ganzevoort@cwi.nl> for this.
210 (defvar makefile-tab-face
211 (if (fboundp 'make-face)
213 (make-face 'makefile-tab-face)
214 (or (face-differs-from-default-p 'makefile-tab-face)
215 (set-face-background 'makefile-tab-face "hotpink"))
218 "Face to use for highlighting leading tabs in font-lock-mode.")
220 (defconst makefile-font-lock-keywords
222 ;; Do macro assignments. These get the "variable-name" face rather
224 (list makefile-macroassign-regex 1 'font-lock-variable-name-face)
226 ;; Variable references even in targets/strings/comments:
227 '("\\$[({]\\([a-zA-Z0-9_]+\\)[})]" 1 font-lock-reference-face t)
229 ;; Do dependencies. These get the function name face.
230 (list makefile-dependency-regex 1 'font-lock-function-name-face)
232 ;; Highlight leading tab. Maybe highlighting all leading TABs
233 ;; would be nice? I don't know.
234 '("^\t" . makefile-tab-face)))
236 ;;; ------------------------------------------------------------
237 ;;; The following configurable variables are used in the
238 ;;; up-to-date overview .
239 ;;; The standard configuration assumes that your `make' program
240 ;;; can be run in question/query mode using the `-q' option, this
241 ;;; means that the command
245 ;;; should return an exit status of zero if the target `foo' is
246 ;;; up to date and a nonzero exit status otherwise.
247 ;;; Many makes can do this although the docs/manpages do not mention
248 ;;; it. Try it with your favourite one. GNU make, System V make, and
249 ;;; Dennis Vadura's DMake have no problems.
250 ;;; Set the variable `makefile-brave-make' to the name of the
251 ;;; make utility that does this on your system.
252 ;;; To understand what this is all about see the function definition
253 ;;; of `makefile-query-by-make-minus-q' .
254 ;;; ------------------------------------------------------------
256 (defvar makefile-brave-make "make"
257 "A make that can handle the \'-q\' option.")
259 (defvar makefile-query-one-target-method 'makefile-query-by-make-minus-q
260 "A function symbol [one that can be used as the first argument to
261 funcall] that provides a function that must conform to the following
264 * As its first argument, it must accept the name of the target to
265 be checked, as a string.
267 * As its second argument, it may accept the name of a makefile
268 as a string. Depending on what you're going to do you may
271 * It must return the integer value 0 (zero) if the given target
272 should be considered up-to-date in the context of the given
273 makefile, any nonzero integer value otherwise.")
275 (defvar makefile-up-to-date-buffer-name "*Makefile Up-to-date overview*"
276 "Name of the Up-to-date overview buffer.")
278 ;;; --- end of up-to-date-overview configuration ------------------
280 (defvar makefile-mode-map nil
281 "The keymap that is used in makefile-mode.")
282 (if makefile-mode-map
284 (setq makefile-mode-map (make-sparse-keymap))
286 (define-key makefile-mode-map "\C-c:" 'makefile-insert-target-ref)
287 (if makefile-electric-keys
289 (define-key makefile-mode-map "$" 'makefile-insert-macro-ref)
290 (define-key makefile-mode-map ":" 'makefile-electric-colon)
291 (define-key makefile-mode-map "=" 'makefile-electric-equal)
292 (define-key makefile-mode-map "." 'makefile-electric-dot)))
293 (define-key makefile-mode-map "\C-c\C-f" 'makefile-pickup-filenames-as-targets)
294 (define-key makefile-mode-map "\C-c\C-b" 'makefile-switch-to-browser)
295 (define-key makefile-mode-map "\C-c\C-p" 'makefile-pickup-everything)
296 (define-key makefile-mode-map "\C-c\C-u" 'makefile-create-up-to-date-overview)
297 (define-key makefile-mode-map "\C-c\C-i" 'makefile-insert-gmake-function)
298 (define-key makefile-mode-map "\C-c\C-\\" 'makefile-backslash-region)
299 (define-key makefile-mode-map "\M-p" 'makefile-previous-dependency)
300 (define-key makefile-mode-map "\M-n" 'makefile-next-dependency)
301 (define-key makefile-mode-map "\e\t" 'makefile-complete)
304 (define-key makefile-mode-map [menu-bar makefile-mode]
305 (cons "Makefile" (make-sparse-keymap "Makefile")))
307 (define-key makefile-mode-map [menu-bar makefile-mode browse]
308 '("Pop up Makefile browser" . makefile-switch-to-browser))
309 (define-key makefile-mode-map [menu-bar makefile-mode complete]
310 '("Complete target or macro" . makefile-complete))
311 (define-key makefile-mode-map [menu-bar makefile-mode pickup]
312 '("Find targets and macros" . makefile-pickup-everything))
314 (define-key makefile-mode-map [menu-bar makefile-mode prev]
315 '("Move to previous dependency" . makefile-previous-dependency))
316 (define-key makefile-mode-map [menu-bar makefile-mode next]
317 '("Move to next dependency" . makefile-next-dependency)))
319 (defvar makefile-browser-map nil
320 "The keymap that is used in the macro- and target browser.")
321 (if makefile-browser-map
323 (setq makefile-browser-map (make-sparse-keymap))
324 (define-key makefile-browser-map "n" 'makefile-browser-next-line)
325 (define-key makefile-browser-map "\C-n" 'makefile-browser-next-line)
326 (define-key makefile-browser-map "p" 'makefile-browser-previous-line)
327 (define-key makefile-browser-map "\C-p" 'makefile-browser-previous-line)
328 (define-key makefile-browser-map " " 'makefile-browser-toggle)
329 (define-key makefile-browser-map "i" 'makefile-browser-insert-selection)
330 (define-key makefile-browser-map "I" 'makefile-browser-insert-selection-and-quit)
331 (define-key makefile-browser-map "\C-c\C-m" 'makefile-browser-insert-continuation)
332 (define-key makefile-browser-map "q" 'makefile-browser-quit)
333 ;; disable horizontal movement
334 (define-key makefile-browser-map "\C-b" 'undefined)
335 (define-key makefile-browser-map "\C-f" 'undefined))
338 (defvar makefile-mode-syntax-table nil)
339 (if makefile-mode-syntax-table
341 (setq makefile-mode-syntax-table (make-syntax-table))
342 (modify-syntax-entry ?\( "() " makefile-mode-syntax-table)
343 (modify-syntax-entry ?\) ")( " makefile-mode-syntax-table)
344 (modify-syntax-entry ?\[ "(] " makefile-mode-syntax-table)
345 (modify-syntax-entry ?\] "([ " makefile-mode-syntax-table)
346 (modify-syntax-entry ?\{ "(} " makefile-mode-syntax-table)
347 (modify-syntax-entry ?\} "){ " makefile-mode-syntax-table)
348 (modify-syntax-entry ?\' "\" " makefile-mode-syntax-table)
349 (modify-syntax-entry ?\` "\" " makefile-mode-syntax-table)
350 (modify-syntax-entry ?# "< " makefile-mode-syntax-table)
351 (modify-syntax-entry ?\n "> " makefile-mode-syntax-table))
354 ;;; ------------------------------------------------------------
355 ;;; Internal variables.
356 ;;; You don't need to configure below this line.
357 ;;; ------------------------------------------------------------
359 (defvar makefile-target-table nil
360 "Table of all target names known for this buffer.")
362 (defvar makefile-macro-table nil
363 "Table of all macro names known for this buffer.")
365 (defvar makefile-browser-client
366 "A buffer in makefile-mode that is currently using the browser.")
368 (defvar makefile-browser-selection-vector nil)
369 (defvar makefile-has-prereqs nil)
370 (defvar makefile-need-target-pickup t)
371 (defvar makefile-need-macro-pickup t)
373 (defvar makefile-mode-hook '())
375 ;; Each element looks like '("GNU MAKE FUNCTION" "ARG" "ARG" ... )
376 ;; Each "ARG" is used as a prompt for a required argument.
377 (defconst makefile-gnumake-functions-alist
380 ("subst" "From" "To" "In")
381 ("patsubst" "Pattern" "Replacement" "In")
383 ("findstring" "Find what" "In")
384 ("filter" "Pattern" "Text")
385 ("filter-out" "Pattern" "Text")
387 ;; Filename functions
392 ("addsuffix" "Suffix" "Names")
393 ("join" "List 1" "List 2")
394 ("word" "Index" "Text")
397 ("wildcard" "Pattern")
399 ("foreach" "Variable" "List" "Text")
400 ("origin" "Variable")
401 ("shell" "Command")))
404 ;;; ------------------------------------------------------------
405 ;;; The mode function itself.
406 ;;; ------------------------------------------------------------
409 (defun makefile-mode ()
410 "Major mode for editing Makefiles.
411 Calling this function invokes the function(s) \"makefile-mode-hook\" before
414 \\{makefile-mode-map}
416 In the browser, use the following keys:
418 \\{makefile-browser-map}
420 makefile-mode can be configured by modifying the following
423 makefile-browser-buffer-name:
424 Name of the macro- and target browser buffer.
426 makefile-target-colon:
427 The string that gets appended to all target names
428 inserted by makefile-insert-target.
429 \":\" or \"::\" are quite common values.
431 makefile-macro-assign:
432 The string that gets appended to all macro names
433 inserted by makefile-insert-macro.
434 The normal value should be \" = \", since this is what
435 standard make expects. However, newer makes such as dmake
436 allow a larger variety of different macro assignments, so you
437 might prefer to use \" += \" or \" := \" .
439 makefile-tab-after-target-colon:
440 If you want a TAB (instead of a space) to be appended after the
441 target colon, then set this to a non-nil value.
443 makefile-browser-leftmost-column:
444 Number of blanks to the left of the browser selection mark.
446 makefile-browser-cursor-column:
447 Column in which the cursor is positioned when it moves
448 up or down in the browser.
450 makefile-browser-selected-mark:
451 String used to mark selected entries in the browser.
453 makefile-browser-unselected-mark:
454 String used to mark unselected entries in the browser.
456 makefile-browser-auto-advance-after-selection-p:
457 If this variable is set to a non-nil value the cursor
458 will automagically advance to the next line after an item
459 has been selected in the browser.
461 makefile-pickup-everything-picks-up-filenames-p:
462 If this variable is set to a non-nil value then
463 makefile-pickup-everything also picks up filenames as targets
464 (i.e. it calls makefile-find-filenames-as-targets), otherwise
465 filenames are omitted.
467 makefile-cleanup-continuations-p:
468 If this variable is set to a non-nil value then makefile-mode
469 will assure that no line in the file ends with a backslash
470 (the continuation character) followed by any whitespace.
471 This is done by silently removing the trailing whitespace, leaving
472 the backslash itself intact.
473 IMPORTANT: Please note that enabling this option causes makefile-mode
474 to MODIFY A FILE WITHOUT YOUR CONFIRMATION when \'it seems necessary\'.
476 makefile-browser-hook:
477 A function or list of functions to be called just before the
478 browser is entered. This is executed in the makefile buffer.
480 makefile-special-targets-list:
481 List of special targets. You will be offered to complete
482 on one of those in the minibuffer whenever you enter a \".\"
483 at the beginning of a line in makefile-mode."
486 (kill-all-local-variables)
487 (make-local-variable 'local-write-file-hooks)
488 (setq local-write-file-hooks
489 '(makefile-cleanup-continuations makefile-warn-suspicious-lines))
490 (make-local-variable 'makefile-target-table)
491 (make-local-variable 'makefile-macro-table)
492 (make-local-variable 'makefile-has-prereqs)
493 (make-local-variable 'makefile-need-target-pickup)
494 (make-local-variable 'makefile-need-macro-pickup)
497 (make-local-variable 'font-lock-defaults)
498 (setq font-lock-defaults '(makefile-font-lock-keywords))
501 (make-local-variable 'add-log-current-defun-function)
502 (setq add-log-current-defun-function 'makefile-add-log-defun)
505 (make-local-variable 'imenu-create-index-function)
506 (setq imenu-create-index-function 'makefile-menu-index-function)
509 (make-local-variable 'comment-start)
510 (setq comment-start "#")
511 (make-local-variable 'comment-end)
512 (setq comment-end "")
513 (make-local-variable 'comment-start-skip)
514 (setq comment-start-skip "#+[ \t]*")
516 ;; become the current major mode
517 (setq major-mode 'makefile-mode)
518 (setq mode-name "Makefile")
520 ;; Activate keymap and syntax table.
521 (use-local-map makefile-mode-map)
522 (set-syntax-table makefile-mode-syntax-table)
524 ;; Real TABs are important in makefiles
525 (setq indent-tabs-mode t)
526 (run-hooks 'makefile-mode-hook))
532 (defun makefile-next-dependency ()
533 "Move (point) to the beginning of the next dependency line below (point)."
535 (let ((here (point)))
537 (if (re-search-forward makefile-dependency-regex (point-max) t)
538 (progn (beginning-of-line) t) ; indicate success
539 (goto-char here) nil)))
541 (defun makefile-previous-dependency ()
542 "Move (point) to the beginning of the next dependency line above (point)."
544 (let ((here (point)))
546 (if (re-search-backward makefile-dependency-regex (point-min) t)
547 (progn (beginning-of-line) t) ; indicate success
548 (goto-char here) nil)))
552 ;;; Electric keys. Blech.
554 (defun makefile-electric-dot (arg)
555 "Prompt for the name of a special target to insert.
556 Only does electric insertion at beginning of line.
557 Anywhere else just self-inserts."
560 (makefile-insert-special-target)
561 (self-insert-command arg)))
563 (defun makefile-insert-special-target ()
564 "Propmt for and insert a special target name.
565 Uses `makefile-special-targets' list."
567 (makefile-pickup-targets)
568 (let ((special-target
569 (completing-read "Special target: "
570 makefile-special-targets-list nil nil nil)))
571 (if (zerop (length special-target))
573 (insert "." special-target ":")
574 (makefile-forward-after-target-colon))))
576 (defun makefile-electric-equal (arg)
577 "Prompt for name of a macro to insert.
578 Only does prompting if point is at beginning of line.
579 Anywhere else just self-inserts."
581 (makefile-pickup-macros)
583 (call-interactively 'makefile-insert-macro)
584 (self-insert-command arg)))
586 (defun makefile-insert-macro (macro-name)
587 "Prepare definition of a new macro."
588 (interactive "sMacro Name: ")
589 (makefile-pickup-macros)
590 (if (not (zerop (length macro-name)))
593 (insert macro-name makefile-macro-assign)
594 (setq makefile-need-macro-pickup t)
595 (makefile-remember-macro macro-name))))
597 (defun makefile-insert-macro-ref (macro-name)
598 "Complete on a list of known macros, then insert complete ref at (point)."
602 (makefile-pickup-macros)
603 (completing-read "Refer to macro: " makefile-macro-table nil nil nil))))
604 (makefile-do-macro-insertion macro-name))
606 (defun makefile-insert-target (target-name)
607 "Prepare definition of a new target (dependency line)."
608 (interactive "sTarget: ")
609 (if (not (zerop (length target-name)))
612 (insert target-name makefile-target-colon)
613 (makefile-forward-after-target-colon)
615 (setq makefile-need-target-pickup t)
616 (makefile-remember-target target-name))))
618 (defun makefile-insert-target-ref (target-name)
619 "Complete on a list of known targets, then insert target-ref at (point) ."
623 (makefile-pickup-targets)
624 (completing-read "Refer to target: " makefile-target-table nil nil nil))))
625 (if (not (zerop (length target-name)))
626 (insert target-name " ")))
628 (defun makefile-electric-colon (arg)
629 "Prompt for name of new target.
630 Prompting only happens at beginning of line.
631 Anywhere else just self-inserts."
634 (call-interactively 'makefile-insert-target)
635 (self-insert-command arg)))
639 ;;; ------------------------------------------------------------
640 ;;; Extracting targets and macros from an existing makefile
641 ;;; ------------------------------------------------------------
643 (defun makefile-pickup-targets ()
644 "Notice names of all target definitions in Makefile."
646 (if (not makefile-need-target-pickup)
648 (setq makefile-need-target-pickup nil)
649 (setq makefile-target-table nil)
650 (setq makefile-has-prereqs nil)
652 (goto-char (point-min))
653 (while (re-search-forward makefile-dependency-regex (point-max) t)
654 (makefile-add-this-line-targets)))
655 (message "Read targets OK.")))
657 (defun makefile-add-this-line-targets ()
660 (let ((done-with-line nil)
661 (line-number (1+ (count-lines (point-min) (point)))))
662 (while (not done-with-line)
663 (skip-chars-forward " \t")
664 (if (not (setq done-with-line (or (eolp)
665 (char-equal (char-after (point)) ?:))))
667 (let* ((start-of-target-name (point))
670 (skip-chars-forward "^ \t:#")
671 (buffer-substring start-of-target-name (point))))
673 (not (looking-at ":[ \t]*$"))))
674 (if (makefile-remember-target target-name has-prereqs)
675 (message "Picked up target \"%s\" from line %d"
676 target-name line-number)))))))))
678 (defun makefile-pickup-macros ()
679 "Notice names of all macro definitions in Makefile."
681 (if (not makefile-need-macro-pickup)
683 (setq makefile-need-macro-pickup nil)
684 (setq makefile-macro-table nil)
686 (goto-char (point-min))
687 (while (re-search-forward makefile-macroassign-regex (point-max) t)
688 (makefile-add-this-line-macro)
690 (message "Read macros OK.")))
692 (defun makefile-add-this-line-macro ()
695 (skip-chars-forward " \t")
697 (let* ((start-of-macro-name (point))
698 (line-number (1+ (count-lines (point-min) (point))))
700 (skip-chars-forward "^ \t:#=*")
701 (buffer-substring start-of-macro-name (point)))))
702 (if (makefile-remember-macro macro-name)
703 (message "Picked up macro \"%s\" from line %d"
704 macro-name line-number))))))
706 (defun makefile-pickup-everything (arg)
707 "Notice names of all macros and targets in Makefile.
708 Prefix arg means force pickups to be redone."
712 (setq makefile-need-target-pickup t)
713 (setq makefile-need-macro-pickup t)))
714 (makefile-pickup-macros)
715 (makefile-pickup-targets)
716 (if makefile-pickup-everything-picks-up-filenames-p
717 (makefile-pickup-filenames-as-targets)))
719 (defun makefile-pickup-filenames-as-targets ()
720 "Scan the current directory for filenames to use as targets.
721 Checks each filename against `makefile-ignored-files-in-pickup-regex'
722 and adds all qualifying names to the list of known targets."
724 (let* ((dir (file-name-directory (buffer-file-name)))
725 (raw-filename-list (if dir
726 (file-name-all-completions "" dir)
727 (file-name-all-completions "" ""))))
728 (mapcar '(lambda (name)
729 (if (and (not (file-directory-p name))
730 (not (string-match makefile-ignored-files-in-pickup-regex
732 (if (makefile-remember-target name)
733 (message "Picked up file \"%s\" as target" name))))
740 (defun makefile-complete ()
741 "Perform completion on Makefile construct preceding point.
742 Can complete variable and target names.
743 The context determines which are considered."
745 (let* ((beg (save-excursion
746 (skip-chars-backward "^$(){}:#= \t\n")
748 (try (buffer-substring beg (point)))
754 (let ((pc (preceding-char)))
756 ;; Beginning of line means anything.
760 ;; Preceding "$" means macros only.
764 ;; Preceding "$(" or "${" means macros only.
771 (= (preceding-char) ?$))))
772 (setq do-macros t)))))
775 (let* ((table (append (if do-macros
777 makefile-target-table)
778 makefile-macro-table))
779 (completion (try-completion try table)))
781 ;; Exact match, so insert closing paren or colon.
783 (insert (if do-macros
795 (message "Can't find completion for \"%s\"" try)
798 ;; Partial completion.
799 ((not (string= try completion))
800 ;; FIXME it would be nice to supply the closing paren if an
801 ;; exact, unambiguous match were found. That is not possible
802 ;; right now. Ditto closing ":" for targets.
803 (delete-region beg (point))
805 ;; DO-MACROS means doing macros only. If not that, then check
806 ;; to see if this completion is a macro. Special insertion
807 ;; must be done for macros.
809 (assoc completion makefile-macro-table))
810 (let ((makefile-use-curly-braces-for-macros-p
812 makefile-use-curly-braces-for-macros-p)))
813 (delete-backward-char 2)
814 (makefile-do-macro-insertion completion)
815 (delete-backward-char 1))
817 ;; Just insert targets.
818 (insert completion)))
820 ;; Can't complete any more, so make completion list. FIXME
821 ;; this doesn't do the right thing when the completion is
822 ;; actually inserted. I don't think there is an easy way to do
825 (message "Making completion list...")
826 (let ((list (all-completions try table)))
827 (with-output-to-temp-buffer "*Completions*"
828 (display-completion-list list)))
829 (message "Making completion list...done"))))))
833 ;; Backslashification. Stolen from cc-mode.el.
835 (defun makefile-backslashify-current-line (doit)
838 (if (not (save-excursion
840 (eq (char-after (point)) ?\\ )))
842 (if (>= (current-column) makefile-backslash-column)
844 (while (<= (current-column) makefile-backslash-column)
848 (while (< (current-column) makefile-backslash-column)
855 (if (eq (char-after (point)) ?\\ )
856 (let ((saved (save-excursion
859 (skip-chars-backward " \t")
860 (delete-region (point) saved)))))))
862 (defun makefile-backslash-region (beg end arg)
863 "Insert backslashes at end of every line in region.
864 Useful for defining multi-line rules.
865 If called with a prefix argument, trailing backslahes are removed."
868 (let ((do-lastline-p (progn (goto-char end) (not (bolp)))))
870 (narrow-to-region beg end)
871 (goto-char (point-min))
872 (while (not (save-excursion
875 (makefile-backslashify-current-line (null arg))
878 (progn (goto-char end)
879 (makefile-backslashify-current-line (null arg)))))))
883 ;;; ------------------------------------------------------------
885 ;;; ------------------------------------------------------------
887 (defun makefile-browser-format-target-line (target selected)
889 (concat (make-string makefile-browser-leftmost-column ?\ )
891 makefile-browser-selected-mark
892 makefile-browser-unselected-mark)
894 target makefile-target-colon))
896 (defun makefile-browser-format-macro-line (macro selected)
898 (concat (make-string makefile-browser-leftmost-column ?\ )
900 makefile-browser-selected-mark
901 makefile-browser-unselected-mark)
902 (makefile-format-macro-ref macro))))
904 (defun makefile-browser-fill (targets macros)
905 (let ((inhibit-read-only t))
906 (goto-char (point-min))
910 (lambda (item) (insert (makefile-browser-format-target-line (car item) nil) "\n")))
915 (lambda (item) (insert (makefile-browser-format-macro-line (car item) nil) "\n")))
918 (sort-lines nil (point-min) (point-max))
919 (goto-char (1- (point-max)))
920 (delete-char 1) ; remove unnecessary newline at eob
921 (goto-char (point-min))
922 (forward-char makefile-browser-cursor-column)))
925 ;;; Moving up and down in the browser
928 (defun makefile-browser-next-line ()
929 "Move the browser selection cursor to the next line."
931 (if (not (makefile-last-line-p))
934 (forward-char makefile-browser-cursor-column))))
936 (defun makefile-browser-previous-line ()
937 "Move the browser selection cursor to the previous line."
939 (if (not (makefile-first-line-p))
942 (forward-char makefile-browser-cursor-column))))
945 ;;; Quitting the browser (returns to client buffer)
948 (defun makefile-browser-quit ()
949 "Leave the browser and return to the makefile buffer."
951 (let ((my-client makefile-browser-client))
952 (setq makefile-browser-client nil) ; we quitted, so NO client!
953 (set-buffer-modified-p nil)
954 (kill-buffer (current-buffer))
955 (pop-to-buffer my-client)))
958 ;;; Toggle state of a browser item
961 (defun makefile-browser-toggle ()
962 "Toggle the selection state of the browser item at the cursor position."
964 (let ((this-line (count-lines (point-min) (point))))
965 (setq this-line (max 1 this-line))
966 (makefile-browser-toggle-state-for-line this-line)
967 (goto-line this-line)
968 (let ((inhibit-read-only t))
970 (if (makefile-browser-on-macro-line-p)
971 (let ((macro-name (makefile-browser-this-line-macro-name)))
974 (makefile-browser-format-macro-line
976 (makefile-browser-get-state-for-line this-line))))
977 (let ((target-name (makefile-browser-this-line-target-name)))
980 (makefile-browser-format-target-line
982 (makefile-browser-get-state-for-line this-line))))))
984 (forward-char makefile-browser-cursor-column)
985 (if makefile-browser-auto-advance-after-selection-p
986 (makefile-browser-next-line))))
989 ;;; Making insertions into the client buffer
992 (defun makefile-browser-insert-continuation ()
993 "Insert a makefile continuation.
994 In the makefile buffer, go to (end-of-line), insert a \'\\\'
995 character, insert a new blank line, go to that line and indent by one TAB.
996 This is most useful in the process of creating continued lines when copying
997 large dependencies from the browser to the client buffer.
998 \(point) advances accordingly in the client buffer."
1001 (set-buffer makefile-browser-client)
1005 (defun makefile-browser-insert-selection ()
1006 "Insert all selected targets and/or macros in the makefile buffer.
1007 Insertion takes place at (point)."
1011 (let ((current-line 1))
1013 (if (makefile-browser-get-state-for-line current-line)
1014 (makefile-browser-send-this-line-item))
1016 (setq current-line (1+ current-line))))))
1018 (defun makefile-browser-insert-selection-and-quit ()
1020 (makefile-browser-insert-selection)
1021 (makefile-browser-quit))
1023 (defun makefile-browser-send-this-line-item ()
1024 (if (makefile-browser-on-macro-line-p)
1026 (let ((macro-name (makefile-browser-this-line-macro-name)))
1027 (set-buffer makefile-browser-client)
1028 (insert (makefile-format-macro-ref macro-name) " ")))
1030 (let ((target-name (makefile-browser-this-line-target-name)))
1031 (set-buffer makefile-browser-client)
1032 (insert target-name " ")))))
1034 (defun makefile-browser-start-interaction ()
1035 (use-local-map makefile-browser-map)
1036 (setq buffer-read-only t))
1038 (defun makefile-browse (targets macros)
1040 (if (zerop (+ (length targets) (length macros)))
1043 (message "No macros or targets to browse! Consider running 'makefile-pickup-everything\'"))
1044 (let ((browser-buffer (get-buffer-create makefile-browser-buffer-name)))
1045 (pop-to-buffer browser-buffer)
1046 (make-variable-buffer-local 'makefile-browser-selection-vector)
1047 (makefile-browser-fill targets macros)
1048 (shrink-window-if-larger-than-buffer)
1049 (setq makefile-browser-selection-vector
1050 (make-vector (+ (length targets) (length macros)) nil))
1051 (makefile-browser-start-interaction))))
1053 (defun makefile-switch-to-browser ()
1055 (run-hooks 'makefile-browser-hook)
1056 (setq makefile-browser-client (current-buffer))
1057 (makefile-pickup-targets)
1058 (makefile-pickup-macros)
1059 (makefile-browse makefile-target-table makefile-macro-table))
1063 ;;; ------------------------------------------------------------
1064 ;;; Up-to-date overview buffer
1065 ;;; ------------------------------------------------------------
1067 (defun makefile-create-up-to-date-overview ()
1068 "Create a buffer containing an overview of the state of all known targets.
1069 Known targets are targets that are explicitly defined in that makefile;
1070 in other words, all targets that appear on the left hand side of a
1071 dependency in the makefile."
1073 (if (y-or-n-p "Are you sure that the makefile being edited is consistent? ")
1075 ;; The rest of this function operates on a temporary makefile, created by
1076 ;; writing the current contents of the makefile buffer.
1078 (let ((saved-target-table makefile-target-table)
1079 (this-buffer (current-buffer))
1080 (makefile-up-to-date-buffer
1081 (get-buffer-create makefile-up-to-date-buffer-name))
1082 (filename (makefile-save-temporary))
1084 ;; Forget the target table because it may contain picked-up filenames
1085 ;; that are not really targets in the current makefile.
1086 ;; We don't want to query these, so get a new target-table with just the
1087 ;; targets that can be found in the makefile buffer.
1088 ;; The 'old' target table will be restored later.
1090 (real-targets (progn
1091 (makefile-pickup-targets)
1092 makefile-target-table))
1093 (prereqs makefile-has-prereqs)
1096 (set-buffer makefile-up-to-date-buffer)
1097 (setq buffer-read-only nil)
1099 (makefile-query-targets filename real-targets prereqs)
1100 (if (zerop (buffer-size)) ; if it did not get us anything
1102 (kill-buffer (current-buffer))
1103 (message "No overview created!")))
1104 (set-buffer this-buffer)
1105 (setq makefile-target-table saved-target-table)
1106 (if (get-buffer makefile-up-to-date-buffer-name)
1108 (pop-to-buffer (get-buffer makefile-up-to-date-buffer-name))
1109 (shrink-window-if-larger-than-buffer)
1110 (sort-lines nil (point-min) (point-max))
1111 (setq buffer-read-only t))))))
1113 (defun makefile-save-temporary ()
1114 "Create a temporary file from the current makefile buffer."
1115 (let ((filename (makefile-generate-temporary-filename)))
1116 (write-region (point-min) (point-max) filename nil 0)
1117 filename)) ; return the filename
1119 (defun makefile-generate-temporary-filename ()
1120 "Create a filename suitable for use in `makefile-save-temporary'.
1121 Be careful to allow brain-dead file systems (DOS, SYSV ...) to cope
1122 with the generated name!"
1123 (let ((my-name (user-login-name))
1124 (my-uid (int-to-string (user-uid))))
1126 (if (> (length my-name) 3)
1127 (substring my-name 0 3)
1130 (if (> (length my-uid) 3)
1131 (substring my-uid 0 3)
1134 (defun makefile-query-targets (filename target-table prereq-list)
1135 "Fill the up-to-date-overview-buffer.
1136 Checks each target in TARGET-TABLE using `makefile-query-one-target-method'
1137 and generates the overview, one line per target name."
1140 (function (lambda (item)
1141 (let* ((target-name (car item))
1142 (no-prereqs (not (member target-name prereq-list)))
1143 (needs-rebuild (or no-prereqs
1145 makefile-query-one-target-method
1150 (cond (no-prereqs " .. has no prerequisites")
1151 (needs-rebuild " .. NEEDS REBUILD")
1152 (t " .. is up to date"))))
1155 (goto-char (point-min))
1156 (delete-file filename)) ; remove the tmpfile
1158 (defun makefile-query-by-make-minus-q (target &optional filename)
1160 (call-process makefile-brave-make nil nil nil
1161 "-f" filename "-q" target))))
1165 ;;; ------------------------------------------------------------
1166 ;;; Continuation cleanup
1167 ;;; ------------------------------------------------------------
1169 (defun makefile-cleanup-continuations ()
1170 (if (eq major-mode 'makefile-mode)
1171 (if (and makefile-cleanup-continuations-p
1172 (not buffer-read-only))
1174 (goto-char (point-min))
1175 (while (re-search-forward "\\\\[ \t]+$" (point-max) t)
1176 (replace-match "\\" t t))))))
1179 ;;; ------------------------------------------------------------
1180 ;;; Warn of suspicious lines
1181 ;;; ------------------------------------------------------------
1183 (defun makefile-warn-suspicious-lines ()
1184 (let ((dont-save nil))
1185 (if (eq major-mode 'makefile-mode)
1188 (goto-char (point-min))
1190 "\\(^[\t]+$\\)\\|\\(^[ ]+[\t]\\)" (point-max) t))))
1192 (let ((line-nr (count-lines (point-min) suspicious)))
1195 (format "Suspicious line %d. Save anyway "
1201 ;;; ------------------------------------------------------------
1202 ;;; GNU make function support
1203 ;;; ------------------------------------------------------------
1205 (defun makefile-insert-gmake-function ()
1206 "Insert a GNU make function call.
1207 Asks for the name of the function to use (with completion).
1208 Then prompts for all required parameters."
1210 (let* ((gm-function-name (completing-read
1212 makefile-gnumake-functions-alist
1214 (gm-function-prompts
1215 (cdr (assoc gm-function-name makefile-gnumake-functions-alist))))
1216 (if (not (zerop (length gm-function-name)))
1217 (insert (makefile-format-macro-ref
1218 (concat gm-function-name " "
1219 (makefile-prompt-for-gmake-funargs
1220 gm-function-name gm-function-prompts)))
1223 (defun makefile-prompt-for-gmake-funargs (function-name prompt-list)
1225 (function (lambda (one-prompt)
1226 (read-string (format "[%s] %s: " function-name one-prompt)
1233 ;;; ------------------------------------------------------------
1234 ;;; Utility functions
1235 ;;; ------------------------------------------------------------
1237 (defun makefile-do-macro-insertion (macro-name)
1238 "Insert a macro reference."
1239 (if (not (zerop (length macro-name)))
1240 (if (assoc macro-name makefile-runtime-macros-list)
1241 (insert "$" macro-name)
1242 (insert (makefile-format-macro-ref macro-name)))))
1244 (defun makefile-remember-target (target-name &optional has-prereqs)
1245 "Remember a given target if it is not already remembered for this buffer."
1246 (if (not (zerop (length target-name)))
1248 (if (not (assoc target-name makefile-target-table))
1249 (setq makefile-target-table
1250 (cons (list target-name) makefile-target-table)))
1252 (setq makefile-has-prereqs
1253 (cons target-name makefile-has-prereqs))))))
1255 (defun makefile-remember-macro (macro-name)
1256 "Remember a given macro if it is not already remembered for this buffer."
1257 (if (not (zerop (length macro-name)))
1258 (if (not (assoc macro-name makefile-macro-table))
1259 (setq makefile-macro-table
1260 (cons (list macro-name) makefile-macro-table)))))
1262 (defun makefile-forward-after-target-colon ()
1263 "Move point forward after inserting the terminating colon of a target.
1264 This acts according to the value of `makefile-tab-after-target-colon'."
1265 (if makefile-tab-after-target-colon
1269 (defun makefile-browser-on-macro-line-p ()
1270 "Determine if point is on a macro line in the browser."
1273 (re-search-forward "\\$[{(]" (makefile-end-of-line-point) t)))
1275 (defun makefile-browser-this-line-target-name ()
1276 "Extract the target name from a line in the browser."
1279 (skip-chars-backward "^ \t")
1280 (buffer-substring (point) (1- (makefile-end-of-line-point)))))
1282 (defun makefile-browser-this-line-macro-name ()
1283 "Extract the macro name from a line in the browser."
1286 (re-search-forward "\\$[{(]" (makefile-end-of-line-point) t)
1287 (let ((macro-start (point)))
1288 (skip-chars-forward "^})")
1289 (buffer-substring macro-start (point)))))
1291 (defun makefile-format-macro-ref (macro-name)
1292 "Format a macro reference.
1293 Uses `makefile-use-curly-braces-for-macros-p'."
1294 (if (or (char-equal ?\( (string-to-char macro-name))
1295 (char-equal ?\{ (string-to-char macro-name)))
1296 (format "$%s" macro-name)
1297 (if makefile-use-curly-braces-for-macros-p
1298 (format "${%s}" macro-name)
1299 (format "$(%s)" macro-name))))
1301 (defun makefile-browser-get-state-for-line (n)
1302 (aref makefile-browser-selection-vector (1- n)))
1304 (defun makefile-browser-set-state-for-line (n to-state)
1305 (aset makefile-browser-selection-vector (1- n) to-state))
1307 (defun makefile-browser-toggle-state-for-line (n)
1308 (makefile-browser-set-state-for-line n (not (makefile-browser-get-state-for-line n))))
1310 (defun makefile-beginning-of-line-point ()
1315 (defun makefile-end-of-line-point ()
1320 (defun makefile-last-line-p ()
1321 (= (makefile-end-of-line-point) (point-max)))
1323 (defun makefile-first-line-p ()
1324 (= (makefile-beginning-of-line-point) (point-min)))
1328 ;;; Support for other packages, like add-log and imenu.
1330 (defun makefile-add-log-defun ()
1331 ;; "Return name of target or macro point is in, or nil."
1335 ((looking-at makefile-macroassign-regex)
1336 (buffer-substring (match-beginning 1)
1340 (re-search-backward makefile-dependency-regex nil t))
1341 (buffer-substring (match-beginning 1)
1345 ;; FIXME it might be nice to have them separated by macro vs target.
1346 (defun makefile-menu-index-function ()
1347 ;; "Generate alist of indices for imenu."
1350 (re (concat makefile-dependency-regex
1352 makefile-macroassign-regex)))
1353 (imenu-progress-message stupid 0)
1354 (goto-char (point-min))
1355 (while (re-search-forward re nil t)
1356 (imenu-progress-message stupid)
1357 (let ((n (if (match-beginning 1) 1 5)))
1359 (cons (buffer-substring (match-beginning n)
1361 (match-beginning n))
1363 (imenu-progress-message stupid 100)
1366 ;;; makefile.el ends here