1 ;;; make-mode.el --- makefile editing commands for Emacs
3 ;; Copyright (C) 1992, 1994, 1999, 2000, 2001, 2002, 2003, 2004, 2005,
4 ;; 2006, 2007, 2008, 2009, 2010 Free Software Foundation, Inc.
6 ;; Author: Thomas Neumann <tom@smart.bo.open.de>
7 ;; Eric S. Raymond <esr@snark.thyrsus.com>
10 ;; Keywords: unix, tools
12 ;; This file is part of GNU Emacs.
14 ;; GNU Emacs is free software: you can redistribute it and/or modify
15 ;; it under the terms of the GNU General Public License as published by
16 ;; the Free Software Foundation, either version 3 of the License, or
17 ;; (at your option) any later version.
19 ;; GNU Emacs is distributed in the hope that it will be useful,
20 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
21 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 ;; GNU General Public License for more details.
24 ;; You should have received a copy of the GNU General Public License
25 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
29 ;; A major mode for editing makefiles. The mode knows about Makefile
30 ;; syntax and defines M-n and M-p to move to next and previous productions.
32 ;; The keys $, =, : and . are electric; they try to help you fill in a
33 ;; macro reference, macro definition, ordinary target name, or special
34 ;; target name, respectively. Such names are completed using a list of
35 ;; targets and macro names parsed out of the makefile. This list is
36 ;; automatically updated, if necessary, whenever you invoke one of
37 ;; these commands. You can force it to be updated with C-c C-p.
39 ;; The command C-c C-f adds certain filenames in the current directory
40 ;; as targets. You can filter out filenames by setting the variable
41 ;; makefile-ignored-files-in-pickup-regex.
43 ;; The command C-c C-u grinds for a bit, then pops up a report buffer
44 ;; showing which target names are up-to-date with respect to their
45 ;; prerequisites, which targets are out-of-date, and which have no
48 ;; The command C-c C-b pops up a browser window listing all target and
49 ;; macro names. You can mark or unmark items with C-c SPC, and insert
50 ;; all marked items back in the Makefile with C-c TAB.
52 ;; The command C-c TAB in the makefile buffer inserts a GNU make builtin.
53 ;; You will be prompted for the builtin's args.
55 ;; There are numerous other customization variables.
60 ;; * Add missing doc strings, improve terse doc strings.
61 ;; * Eliminate electric stuff entirely.
62 ;; * It might be nice to highlight targets differently depending on
63 ;; whether they are up-to-date or not. Not sure how this would
64 ;; interact with font-lock.
65 ;; * Would be nice to edit the commands in ksh-mode and have
66 ;; indentation and slashification done automatically. Hard.
67 ;; * Consider removing browser mode. It seems useless.
68 ;; * ":" should notice when a new target is made and add it to the
69 ;; list (or at least set makefile-need-target-pickup).
70 ;; * Make browser into a major mode.
71 ;; * Clean up macro insertion stuff. It is a mess.
72 ;; * Browser entry and exit is weird. Normalize.
73 ;; * Browser needs to be rewritten. Right now it is kind of a crock.
75 ;; * Act more like dired/buffer menu/whatever.
76 ;; * Highlight as mouse traverses.
78 ;; * Update documentation above.
79 ;; * Update texinfo manual.
86 ;; Sadly we need this for a macro.
92 ;;; ------------------------------------------------------------
93 ;;; Configurable stuff
94 ;;; ------------------------------------------------------------
96 (defgroup makefile nil
97 "Makefile editing commands for Emacs."
98 :link
'(custom-group-link :tag
"Font Lock Faces group" font-lock-faces
)
102 (defface makefile-space
103 '((((class color
)) (:background
"hotpink"))
104 (t (:reverse-video t
)))
105 "Face to use for highlighting leading spaces in Font-Lock mode."
107 (define-obsolete-face-alias 'makefile-space-face
'makefile-space
"22.1")
109 (defface makefile-targets
110 ;; This needs to go along both with foreground and background colors (i.e. shell)
111 '((t (:inherit font-lock-function-name-face
)))
112 "Face to use for additionally highlighting rule targets in Font-Lock mode."
116 (defface makefile-shell
118 ;;'((((class color) (min-colors 88) (background light)) (:background "seashell1"))
119 ;; (((class color) (min-colors 88) (background dark)) (:background "seashell4")))
120 "Face to use for additionally highlighting Shell commands in Font-Lock mode."
124 (defface makefile-makepp-perl
125 '((((class color
) (background light
)) (:background
"LightBlue1")) ; Camel Book
126 (((class color
) (background dark
)) (:background
"DarkBlue"))
127 (t (:reverse-video t
)))
128 "Face to use for additionally highlighting Perl code in Font-Lock mode."
132 (defcustom makefile-browser-buffer-name
"*Macros and Targets*"
133 "*Name of the macro- and target browser buffer."
137 (defcustom makefile-target-colon
":"
138 "*String to append to all target names inserted by `makefile-insert-target'.
139 \":\" or \"::\" are common values."
143 (defcustom makefile-macro-assign
" = "
144 "*String to append to all macro names inserted by `makefile-insert-macro'.
145 The normal value should be \" = \", since this is what
146 standard make expects. However, newer makes such as dmake
147 allow a larger variety of different macro assignments, so you
148 might prefer to use \" += \" or \" := \" ."
152 (defcustom makefile-electric-keys nil
153 "*If non-nil, Makefile mode should install electric keybindings.
158 (defcustom makefile-use-curly-braces-for-macros-p nil
159 "*Controls the style of generated macro references.
160 Non-nil means macro references should use curly braces, like `${this}'.
161 nil means use parentheses, like `$(this)'."
165 (defcustom makefile-tab-after-target-colon t
166 "*If non-nil, insert a TAB after a target colon.
167 Otherwise, a space is inserted.
172 (defcustom makefile-browser-leftmost-column
10
173 "*Number of blanks to the left of the browser selection mark."
177 (defcustom makefile-browser-cursor-column
10
178 "*Column the cursor goes to when it moves up or down in the Makefile browser."
182 (defcustom makefile-backslash-column
48
183 "*Column in which `makefile-backslash-region' inserts backslashes."
187 (defcustom makefile-backslash-align t
188 "*If non-nil, `makefile-backslash-region' will align backslashes."
192 (defcustom makefile-browser-selected-mark
"+ "
193 "*String used to mark selected entries in the Makefile browser."
197 (defcustom makefile-browser-unselected-mark
" "
198 "*String used to mark unselected entries in the Makefile browser."
202 (defcustom makefile-browser-auto-advance-after-selection-p t
203 "*If non-nil, cursor will move after item is selected in Makefile browser."
207 (defcustom makefile-pickup-everything-picks-up-filenames-p nil
208 "*If non-nil, `makefile-pickup-everything' picks up filenames as targets.
209 This means it calls `makefile-pickup-filenames-as-targets'.
210 Otherwise filenames are omitted."
214 (defcustom makefile-cleanup-continuations nil
215 "*If non-nil, automatically clean up continuation lines when saving.
216 A line is cleaned up by removing all whitespace following a trailing
217 backslash. This is done silently.
218 IMPORTANT: Please note that enabling this option causes Makefile mode
219 to MODIFY A FILE WITHOUT YOUR CONFIRMATION when \"it seems necessary\"."
223 (defcustom makefile-mode-hook nil
224 "*Normal hook run by `makefile-mode'."
228 (defvar makefile-browser-hook
'())
231 ;; Special targets for DMake, Sun's make ...
233 (defcustom makefile-special-targets-list
234 '("DEFAULT" "DONE" "ERROR" "EXPORT"
235 "FAILED" "GROUPEPILOG" "GROUPPROLOG" "IGNORE"
236 "IMPORT" "INCLUDE" "INCLUDEDIRS" "INIT"
237 "KEEP_STATE" "MAKEFILES" "MAKE_VERSION" "NO_PARALLEL"
238 "PARALLEL" "PHONY" "PRECIOUS" "REMOVE"
239 "SCCS_GET" "SILENT" "SOURCE" "SUFFIXES"
240 "WAIT" "c.o" "C.o" "m.o"
241 "el.elc" "y.c" "s.o")
242 "List of special targets.
243 You will be offered to complete on one of those in the minibuffer whenever
244 you enter a \".\" at the beginning of a line in `makefile-mode'."
245 :type
'(repeat (list string
))
247 (put 'makefile-special-targets-list
'risky-local-variable t
)
249 (defcustom makefile-runtime-macros-list
250 '(("@") ("&") (">") ("<") ("*") ("^") ("+") ("?") ("%") ("$"))
251 "*List of macros that are resolved by make at runtime.
252 If you insert a macro reference using `makefile-insert-macro-ref', the name
253 of the macro is checked against this list. If it can be found its name will
254 not be enclosed in { } or ( )."
255 :type
'(repeat (list string
))
258 ;; Note that the first big subexpression is used by font lock. Note
259 ;; that if you change this regexp you might have to fix the imenu
260 ;; index in makefile-imenu-generic-expression.
261 (defvar makefile-dependency-regex
262 ;; Allow for two nested levels $(v1:$(v2:$(v3:a=b)=c)=d)
263 "^\\(\\(?:\\$\\(?:[({]\\(?:\\$\\(?:[({]\\(?:\\$\\(?:[^({]\\|.[^\n$#})]+?[})]\\)\\|[^\n$#)}]\\)+?[})]\\|[^({]\\)\\|[^\n$#)}]\\)+?[})]\\|[^({]\\)\\|[^\n$#:=]\\)+?\\)\\(:\\)\\(?:[ \t]*$\\|[^=\n]\\(?:[^#\n]*?;[ \t]*\\(.+\\)\\)?\\)"
264 "Regex used to find dependency lines in a makefile.")
266 (defconst makefile-bsdmake-dependency-regex
267 (progn (string-match (regexp-quote "\\(:\\)") makefile-dependency-regex
)
268 (replace-match "\\([:!]\\)" t t makefile-dependency-regex
))
269 "Regex used to find dependency lines in a BSD makefile.")
271 (defvar makefile-dependency-skip
"^:"
272 "Characters to skip to find a line that might be a dependency.")
274 (defvar makefile-rule-action-regex
275 "^\t[ \t]*\\(?:\\([-@]+\\)[ \t]*\\)\\(.*\\(?:\\\\\n.*\\)*\\)"
276 "Regex used to highlight rule action lines in font lock mode.")
278 (defconst makefile-makepp-rule-action-regex
279 ;; Don't care about initial tab, but I don't know how to font-lock correctly without.
280 "^\t[ \t]*\\(\\(?:\\(?:noecho\\|ignore[-_]error\\|[-@]+\\)[ \t]*\\)*\\)\\(\\(&\\S +\\)?\\(?:.*\\\\\n\\)*.*\\)"
281 "Regex used to highlight makepp rule action lines in font lock mode.")
283 (defconst makefile-bsdmake-rule-action-regex
284 (progn (string-match "-@" makefile-rule-action-regex
)
285 (replace-match "-+@" t t makefile-rule-action-regex
))
286 "Regex used to highlight BSD rule action lines in font lock mode.")
288 ;; Note that the first and second subexpression is used by font lock. Note
289 ;; that if you change this regexp you might have to fix the imenu index in
290 ;; makefile-imenu-generic-expression.
291 (defconst makefile-macroassign-regex
292 ;; We used to match not just the varname but also the whole value
293 ;; (spanning potentially several lines).
294 ;; "^ *\\([^ \n\t][^:#= \t\n]*\\)[ \t]*\\(?:!=[ \t]*\\(\\(?:.+\\\\\n\\)*.+\\)\\|[*:+]?[:?]?=[ \t]*\\(\\(?:.*\\\\\n\\)*.*\\)\\)"
295 ;; What about the define statement? What about differentiating this for makepp?
296 "\\(?:^\\|^export\\|^override\\|:\\|: *override\\) *\\([^ \n\t][^:#= \t\n]*\\)[ \t]*\\(?:!=\\|[*:+]?[:?]?=\\)"
297 "Regex used to find macro assignment lines in a makefile.")
299 (defconst makefile-var-use-regex
300 "[^$]\\$[({]\\([-a-zA-Z0-9_.]+\\|[@%<?^+*][FD]?\\)"
301 "Regex used to find $(macro) uses in a makefile.")
303 (defconst makefile-ignored-files-in-pickup-regex
304 "\\(^\\..*\\)\\|\\(.*~$\\)\\|\\(.*,v$\\)\\|\\(\\.[chy]\\)"
305 "Regex for filenames that will NOT be included in the target list.")
307 (defvar makefile-space
'makefile-space
308 "Face to use for highlighting leading spaces in Font-Lock mode.")
310 ;; These lists were inspired by the old solution. But they are silly, because
311 ;; you can't differentiate what follows. They need to be split up.
312 (defconst makefile-statements
'("include")
313 "List of keywords understood by standard make.")
315 (defconst makefile-automake-statements
316 `("if" "else" "endif" ,@makefile-statements
)
317 "List of keywords understood by automake.")
319 (defconst makefile-gmake-statements
320 `("-sinclude" "sinclude" "vpath" ; makefile-makepp-statements takes rest
321 "ifdef" "ifndef" "ifeq" "ifneq" "-include" "define" "endef" "export"
322 "override define" "override" "unexport"
323 ,@(cdr makefile-automake-statements
))
324 "List of keywords understood by gmake.")
326 ;; These are even more silly, because you can have more spaces in between.
327 (defconst makefile-makepp-statements
328 `("and ifdef" "and ifndef" "and ifeq" "and ifneq" "and ifperl"
329 "and ifmakeperl" "and ifsys" "and ifnsys" "build_cache" "build_check"
330 "else ifdef" "else ifndef" "else ifeq" "else ifneq" "else ifperl"
331 "else ifmakeperl" "else ifsys" "else ifnsys" "enddef" "global"
332 "load_makefile" "ifperl" "ifmakeperl" "ifsys" "ifnsys" "_include"
333 "makeperl" "makesub" "no_implicit_load" "perl" "perl-begin" "perl_begin"
334 "perl-end" "perl_end" "prebuild" "or ifdef" "or ifndef" "or ifeq"
335 "or ifneq" "or ifperl" "or ifmakeperl" "or ifsys" "or ifnsys"
336 "override export" "override global" "register_command_parser"
337 "register_scanner" "repository" "runtime" "signature" "sub"
338 ,@(nthcdr 3 makefile-gmake-statements
))
339 "List of keywords understood by gmake.")
341 (defconst makefile-bsdmake-statements
342 `(".elif" ".elifdef" ".elifmake" ".elifndef" ".elifnmake" ".else" ".endfor"
343 ".endif" ".for" ".if" ".ifdef" ".ifmake" ".ifndef" ".ifnmake" ".undef")
344 "List of keywords understood by BSD make.")
346 (defun makefile-make-font-lock-keywords (var keywords space
348 &rest font-lock-keywords
)
349 `(;; Do macro assignments. These get the "variable-name" face.
350 (,makefile-macroassign-regex
351 (1 font-lock-variable-name-face
)
352 ;; This is for after !=
353 (2 'makefile-shell prepend t
)
354 ;; This is for after normal assignment
355 (3 'font-lock-string-face prepend t
))
358 ;; FIXME: When this spans multiple lines we need font-lock-multiline.
359 (makefile-match-action
360 (1 font-lock-type-face nil t
)
361 (2 'makefile-shell prepend
)
362 ;; Only makepp has builtin commands.
363 (3 font-lock-builtin-face prepend t
))
365 ;; Variable references even in targets/strings/comments.
366 (,var
1 font-lock-variable-name-face prepend
)
368 ;; Automatic variable references and single character variable references,
369 ;; but not shell variables references.
370 ("[^$]\\$\\([@%<?^+*_]\\|[a-zA-Z0-9]\\>\\)"
371 1 font-lock-constant-face prepend
)
372 ("[^$]\\(\\$[@%*]\\)"
373 1 'makefile-targets append
)
375 ;; Fontify conditionals and includes.
376 (,(concat "^\\(?: [ \t]*\\)?"
377 (regexp-opt keywords t
)
378 "\\>[ \t]*\\([^: \t\n#]*\\)")
379 (1 font-lock-keyword-face
) (2 font-lock-variable-name-face
))
382 `((,negation
(1 font-lock-negation-char-face prepend
)
383 (2 font-lock-negation-char-face prepend t
))))
386 '(;; Highlight lines that contain just whitespace.
387 ;; They can cause trouble, especially if they start with a tab.
388 ("^[ \t]+$" . makefile-space
)
390 ;; Highlight shell comments that Make treats as commands,
391 ;; since these can fool people.
392 ("^\t+#" 0 makefile-space t
)
394 ;; Highlight spaces that precede tabs.
395 ;; They can make a tab fail to be effective.
396 ("^\\( +\\)\t" 1 makefile-space
)))
401 (makefile-match-dependency
402 (1 'makefile-targets prepend
)
403 (3 'makefile-shell prepend t
))))
405 (defconst makefile-font-lock-keywords
406 (makefile-make-font-lock-keywords
407 makefile-var-use-regex
411 (defconst makefile-automake-font-lock-keywords
412 (makefile-make-font-lock-keywords
413 makefile-var-use-regex
414 makefile-automake-statements
417 (defconst makefile-gmake-font-lock-keywords
418 (makefile-make-font-lock-keywords
419 makefile-var-use-regex
420 makefile-gmake-statements
422 "^\\(?: [ \t]*\\)?if\\(n\\)\\(?:def\\|eq\\)\\>"
424 '("[^$]\\(\\$[({][@%*][DF][})]\\)"
425 1 'makefile-targets append
)
427 ;; $(function ...) ${function ...}
428 '("[^$]\\$[({]\\([-a-zA-Z0-9_.]+\\s \\)"
429 1 font-lock-function-name-face prepend
)
431 ;; $(shell ...) ${shell ...}
432 '("[^$]\\$\\([({]\\)shell[ \t]+"
433 makefile-match-function-end nil nil
434 (1 'makefile-shell prepend t
))))
436 (defconst makefile-makepp-font-lock-keywords
437 (makefile-make-font-lock-keywords
438 makefile-var-use-regex
439 makefile-makepp-statements
441 "^\\(?: [ \t]*\\)?\\(?:and[ \t]+\\|else[ \t]+\\|or[ \t]+\\)?if\\(n\\)\\(?:def\\|eq\\|sys\\)\\>"
443 '("[^$]\\(\\$[({]\\(?:output\\|stem\\|target\\)s?\\_>.*?[})]\\)"
444 1 'makefile-targets append
)
446 ;; Colon modifier keywords.
447 '("\\(:\\s *\\)\\(build_c\\(?:ache\\|heck\\)\\|env\\(?:ironment\\)?\\|foreach\\|signature\\|scanner\\|quickscan\\|smartscan\\)\\>\\([^:\n]*\\)"
448 (1 font-lock-type-face t
)
449 (2 font-lock-keyword-face t
)
450 (3 font-lock-variable-name-face t
))
452 ;; $(function ...) $((function ...)) ${function ...} ${{function ...}}
453 '("[^$]\\$\\(?:((?\\|{{?\\)\\([-a-zA-Z0-9_.]+\\s \\)"
454 1 font-lock-function-name-face prepend
)
456 ;; $(shell ...) $((shell ...)) ${shell ...} ${{shell ...}}
457 '("[^$]\\$\\(((?\\|{{?\\)shell\\(?:[-_]\\(?:global[-_]\\)?once\\)?[ \t]+"
458 makefile-match-function-end nil nil
459 (1 'makefile-shell prepend t
))
461 ;; $(perl ...) $((perl ...)) ${perl ...} ${{perl ...}}
462 '("[^$]\\$\\(((?\\|{{?\\)makeperl[ \t]+"
463 makefile-match-function-end nil nil
464 (1 'makefile-makepp-perl prepend t
))
465 '("[^$]\\$\\(((?\\|{{?\\)perl[ \t]+"
466 makefile-match-function-end nil nil
467 (1 'makefile-makepp-perl t t
))
469 ;; Can we unify these with (if (match-end 1) 'prepend t)?
470 '("ifmakeperl\\s +\\(.*\\)" 1 'makefile-makepp-perl prepend
)
471 '("ifperl\\s +\\(.*\\)" 1 'makefile-makepp-perl t
)
473 ;; Perl block single- or multiline, as statement or rule action.
474 ;; Don't know why the initial newline in 2nd variant of group 2 doesn't get skipped.
475 '("\\<make\\(?:perl\\|sub\\s +\\S +\\)\\s *\n?\\s *{\\(?:{\\s *\n?\\(\\(?:.*\n\\)+?\\)\\s *}\\|\\s *\\(\\(?:.*?\\|\n?\\(?:.*\n\\)+?\\)\\)\\)}"
476 (1 'makefile-makepp-perl prepend t
)
477 (2 'makefile-makepp-perl prepend t
))
478 '("\\<\\(?:perl\\|sub\\s +\\S +\\)\\s *\n?\\s *{\\(?:{\\s *\n?\\(\\(?:.*\n\\)+?\\)\\s *}\\|\\s *\\(\\(?:.*?\\|\n?\\(?:.*\n\\)+?\\)\\)\\)}"
479 (1 'makefile-makepp-perl t t
)
480 (2 'makefile-makepp-perl t t
))
482 ;; Statement style perl block.
483 '("perl[-_]begin\\s *\\(?:\\s #.*\\)?\n\\(\\(?:.*\n\\)+?\\)\\s *perl[-_]end\\>"
484 1 'makefile-makepp-perl t
)))
486 (defconst makefile-bsdmake-font-lock-keywords
487 (makefile-make-font-lock-keywords
488 ;; A lot more could be done for variables here:
489 makefile-var-use-regex
490 makefile-bsdmake-statements
492 "^\\(?: [ \t]*\\)?\\.\\(?:el\\)?if\\(n?\\)\\(?:def\\|make\\)?\\>[ \t]*\\(!?\\)"
493 '("^[ \t]*\\.for[ \t].+[ \t]\\(in\\)\\>" 1 font-lock-keyword-face
)))
495 (defconst makefile-imake-font-lock-keywords
497 (makefile-make-font-lock-keywords
498 makefile-var-use-regex
502 '("^XCOMM.*$" . font-lock-comment-face
)
503 '("XVAR\\(?:use\\|def\\)[0-9]" 0 font-lock-keyword-face prepend
)
504 '("@@" . font-lock-preprocessor-face
)
506 cpp-font-lock-keywords
))
509 (defconst makefile-font-lock-syntactic-keywords
510 ;; From sh-script.el.
511 ;; A `#' begins a comment in sh when it is unquoted and at the beginning
512 ;; of a word. In the shell, words are separated by metacharacters.
513 ;; The list of special chars is taken from the single-unix spec of the
514 ;; shell command language (under `quoting') but with `$' removed.
515 '(("[^|&;<>()`\\\"' \t\n]\\(#+\\)" 1 "_")
516 ;; Change the syntax of a quoted newline so that it does not end a comment.
519 (defvar makefile-imenu-generic-expression
520 `(("Dependencies" makefile-previous-dependency
1)
521 ("Macro Assignment" ,makefile-macroassign-regex
1))
522 "Imenu generic expression for Makefile mode. See `imenu-generic-expression'.")
524 ;;; ------------------------------------------------------------
525 ;;; The following configurable variables are used in the
526 ;;; up-to-date overview .
527 ;;; The standard configuration assumes that your `make' program
528 ;;; can be run in question/query mode using the `-q' option, this
529 ;;; means that the command
533 ;;; should return an exit status of zero if the target `foo' is
534 ;;; up to date and a nonzero exit status otherwise.
535 ;;; Many makes can do this although the docs/manpages do not mention
536 ;;; it. Try it with your favourite one. GNU make, System V make, and
537 ;;; Dennis Vadura's DMake have no problems.
538 ;;; Set the variable `makefile-brave-make' to the name of the
539 ;;; make utility that does this on your system.
540 ;;; To understand what this is all about see the function definition
541 ;;; of `makefile-query-by-make-minus-q' .
542 ;;; ------------------------------------------------------------
544 (defcustom makefile-brave-make
"make"
545 "*How to invoke make, for `makefile-query-targets'.
546 This should identify a `make' command that can handle the `-q' option."
550 (defcustom makefile-query-one-target-method-function
551 'makefile-query-by-make-minus-q
552 "*Function to call to determine whether a make target is up to date.
553 The function must satisfy this calling convention:
555 * As its first argument, it must accept the name of the target to
556 be checked, as a string.
558 * As its second argument, it may accept the name of a makefile
559 as a string. Depending on what you're going to do you may
562 * It must return the integer value 0 (zero) if the given target
563 should be considered up-to-date in the context of the given
564 makefile, any nonzero integer value otherwise."
567 (defvaralias 'makefile-query-one-target-method
568 'makefile-query-one-target-method-function
)
570 (defcustom makefile-up-to-date-buffer-name
"*Makefile Up-to-date overview*"
571 "*Name of the Up-to-date overview buffer."
575 ;;; --- end of up-to-date-overview configuration ------------------
577 (defvar makefile-mode-abbrev-table nil
578 "Abbrev table in use in Makefile buffers.")
579 (if makefile-mode-abbrev-table
581 (define-abbrev-table 'makefile-mode-abbrev-table
()))
583 (defvar makefile-mode-map
584 (let ((map (make-sparse-keymap))
585 (opt-map (make-sparse-keymap)))
587 (define-key map
"\C-c:" 'makefile-insert-target-ref
)
588 (if makefile-electric-keys
590 (define-key map
"$" 'makefile-insert-macro-ref
)
591 (define-key map
":" 'makefile-electric-colon
)
592 (define-key map
"=" 'makefile-electric-equal
)
593 (define-key map
"." 'makefile-electric-dot
)))
594 (define-key map
"\C-c\C-f" 'makefile-pickup-filenames-as-targets
)
595 (define-key map
"\C-c\C-b" 'makefile-switch-to-browser
)
596 (define-key map
"\C-c\C-c" 'comment-region
)
597 (define-key map
"\C-c\C-p" 'makefile-pickup-everything
)
598 (define-key map
"\C-c\C-u" 'makefile-create-up-to-date-overview
)
599 (define-key map
"\C-c\C-i" 'makefile-insert-gmake-function
)
600 (define-key map
"\C-c\C-\\" 'makefile-backslash-region
)
601 (define-key map
"\C-c\C-m\C-a" 'makefile-automake-mode
)
602 (define-key map
"\C-c\C-m\C-b" 'makefile-bsdmake-mode
)
603 (define-key map
"\C-c\C-m\C-g" 'makefile-gmake-mode
)
604 (define-key map
"\C-c\C-m\C-i" 'makefile-imake-mode
)
605 (define-key map
"\C-c\C-m\C-m" 'makefile-mode
)
606 (define-key map
"\C-c\C-m\C-p" 'makefile-makepp-mode
)
607 (define-key map
"\M-p" 'makefile-previous-dependency
)
608 (define-key map
"\M-n" 'makefile-next-dependency
)
609 (define-key map
"\e\t" 'makefile-complete
)
612 (define-key map
[menu-bar makefile-mode
]
613 (cons "Makefile" (make-sparse-keymap "Makefile")))
615 (define-key map
[menu-bar makefile-mode makefile-type
]
616 (cons "Switch Makefile Type" opt-map
))
617 (define-key opt-map
[makefile-makepp-mode
]
618 '(menu-item "Makepp" makefile-makepp-mode
619 :help
"An adapted `makefile-mode' that knows about makepp"
620 :button
(:radio .
(eq major-mode
'makefile-makepp-mode
))))
621 (define-key opt-map
[makefile-imake-mode
]
622 '(menu-item "Imake" makefile-imake-mode
623 :help
"An adapted `makefile-mode' that knows about imake"
624 :button
(:radio .
(eq major-mode
'makefile-imake-mode
))))
625 (define-key opt-map
[makefile-mode
]
626 '(menu-item "Classic" makefile-mode
627 :help
"`makefile-mode' with no special functionality"
628 :button
(:radio .
(eq major-mode
'makefile-mode
))))
629 (define-key opt-map
[makefile-bsdmake-mode
]
630 '(menu-item "BSD" makefile-bsdmake-mode
631 :help
"An adapted `makefile-mode' that knows about BSD make"
632 :button
(:radio .
(eq major-mode
'makefile-bsdmake-mode
))))
633 (define-key opt-map
[makefile-automake-mode
]
634 '(menu-item "Automake" makefile-automake-mode
635 :help
"An adapted `makefile-mode' that knows about automake"
636 :button
(:radio .
(eq major-mode
'makefile-automake-mode
))))
637 (define-key opt-map
[makefile-gmake-mode
]
638 '(menu-item "GNU make" makefile-gmake-mode
639 :help
"An adapted `makefile-mode' that knows about GNU make"
640 :button
(:radio .
(eq major-mode
'makefile-gmake-mode
))))
641 (define-key map
[menu-bar makefile-mode browse
]
642 '(menu-item "Pop up Makefile Browser" makefile-switch-to-browser
643 ;; XXX: this needs a better string, the function is not documented...
644 :help
"Pop up Makefile Browser"))
645 (define-key map
[menu-bar makefile-mode overview
]
646 '(menu-item "Up To Date Overview" makefile-create-up-to-date-overview
647 :help
"Create a buffer containing an overview of the state of all known targets"))
649 (define-key map
[menu-bar makefile-mode separator1
] '("----"))
650 (define-key map
[menu-bar makefile-mode pickup-file
]
651 '(menu-item "Pick File Name as Target" makefile-pickup-filenames-as-targets
652 :help
"Scan the current directory for filenames to use as targets"))
653 (define-key map
[menu-bar makefile-mode function
]
654 '(menu-item "Insert GNU make function" makefile-insert-gmake-function
655 :help
"Insert a GNU make function call"))
656 (define-key map
[menu-bar makefile-mode pickup
]
657 '(menu-item "Find Targets and Macros" makefile-pickup-everything
658 :help
"Notice names of all macros and targets in Makefile"))
659 (define-key map
[menu-bar makefile-mode complete
]
660 '(menu-item "Complete Target or Macro" makefile-complete
661 :help
"Perform completion on Makefile construct preceding point"))
662 (define-key map
[menu-bar makefile-mode backslash
]
663 '(menu-item "Backslash Region" makefile-backslash-region
664 :help
"Insert, align, or delete end-of-line backslashes on the lines in the region"))
666 (define-key map
[menu-bar makefile-mode separator
] '("----"))
667 (define-key map
[menu-bar makefile-mode prev
]
668 '(menu-item "Move to Previous Dependency" makefile-previous-dependency
669 :help
"Move point to the beginning of the previous dependency line"))
670 (define-key map
[menu-bar makefile-mode next
]
671 '(menu-item "Move to Next Dependency" makefile-next-dependency
672 :help
"Move point to the beginning of the next dependency line"))
674 "The keymap that is used in Makefile mode.")
677 (defvar makefile-browser-map
678 (let ((map (make-sparse-keymap)))
679 (define-key map
"n" 'makefile-browser-next-line
)
680 (define-key map
"\C-n" 'makefile-browser-next-line
)
681 (define-key map
"p" 'makefile-browser-previous-line
)
682 (define-key map
"\C-p" 'makefile-browser-previous-line
)
683 (define-key map
" " 'makefile-browser-toggle
)
684 (define-key map
"i" 'makefile-browser-insert-selection
)
685 (define-key map
"I" 'makefile-browser-insert-selection-and-quit
)
686 (define-key map
"\C-c\C-m" 'makefile-browser-insert-continuation
)
687 (define-key map
"q" 'makefile-browser-quit
)
688 ;; disable horizontal movement
689 (define-key map
"\C-b" 'undefined
)
690 (define-key map
"\C-f" 'undefined
)
692 "The keymap that is used in the macro- and target browser.")
695 (defvar makefile-mode-syntax-table
696 (let ((st (make-syntax-table)))
697 (modify-syntax-entry ?\
( "() " st
)
698 (modify-syntax-entry ?\
) ")( " st
)
699 (modify-syntax-entry ?\
[ "(] " st
)
700 (modify-syntax-entry ?\
] ")[ " st
)
701 (modify-syntax-entry ?\
{ "(} " st
)
702 (modify-syntax-entry ?\
} "){ " st
)
703 (modify-syntax-entry ?
\' "\" " st
)
704 (modify-syntax-entry ?\
` "\" " st
)
705 (modify-syntax-entry ?
# "< " st
)
706 (modify-syntax-entry ?
\n "> " st
)
709 (defvar makefile-imake-mode-syntax-table
(copy-syntax-table
710 makefile-mode-syntax-table
))
711 (if makefile-imake-mode-syntax-table
713 (modify-syntax-entry ?
/ ". 14" makefile-imake-mode-syntax-table
)
714 (modify-syntax-entry ?
* ". 23" makefile-imake-mode-syntax-table
)
715 (modify-syntax-entry ?
# "'" makefile-imake-mode-syntax-table
)
716 (modify-syntax-entry ?
\n ". b" makefile-imake-mode-syntax-table
))
719 ;;; ------------------------------------------------------------
720 ;;; Internal variables.
721 ;;; You don't need to configure below this line.
722 ;;; ------------------------------------------------------------
724 (defvar makefile-target-table nil
725 "Table of all target names known for this buffer.")
726 (put 'makefile-target-table
'risky-local-variable t
)
728 (defvar makefile-macro-table nil
729 "Table of all macro names known for this buffer.")
730 (put 'makefile-macro-table
'risky-local-variable t
)
732 (defvar makefile-browser-client
733 "A buffer in Makefile mode that is currently using the browser.")
735 (defvar makefile-browser-selection-vector nil
)
736 (defvar makefile-has-prereqs nil
)
737 (defvar makefile-need-target-pickup t
)
738 (defvar makefile-need-macro-pickup t
)
740 (defvar makefile-mode-hook
'())
742 ;; Each element looks like '("GNU MAKE FUNCTION" "ARG" "ARG" ... )
743 ;; Each "ARG" is used as a prompt for a required argument.
744 (defconst makefile-gnumake-functions-alist
747 ("subst" "From" "To" "In")
748 ("patsubst" "Pattern" "Replacement" "In")
750 ("findstring" "Find what" "In")
751 ("filter" "Pattern" "Text")
752 ("filter-out" "Pattern" "Text")
754 ;; Filename functions
759 ("addprefix" "Prefix" "Names")
760 ("addsuffix" "Suffix" "Names")
761 ("join" "List 1" "List 2")
762 ("word" "Index" "Text")
765 ("wildcard" "Pattern")
767 ("foreach" "Variable" "List" "Text")
768 ("origin" "Variable")
769 ("shell" "Command")))
772 ;;; ------------------------------------------------------------
773 ;;; The mode function itself.
774 ;;; ------------------------------------------------------------
777 (defun makefile-mode ()
778 "Major mode for editing standard Makefiles.
780 If you are editing a file for a different make, try one of the
781 variants `makefile-automake-mode', `makefile-gmake-mode',
782 `makefile-makepp-mode', `makefile-bsdmake-mode' or,
783 `makefile-imake-mode'. All but the last should be correctly
784 chosen based on the file name, except if it is *.mk. This
785 function ends by invoking the function(s) `makefile-mode-hook'.
787 It is strongly recommended to use `font-lock-mode', because that
788 provides additional parsing information. This is used for
789 example to see that a rule action `echo foo: bar' is a not rule
790 dependency, despite the colon.
792 \\{makefile-mode-map}
794 In the browser, use the following keys:
796 \\{makefile-browser-map}
798 Makefile mode can be configured by modifying the following variables:
800 `makefile-browser-buffer-name':
801 Name of the macro- and target browser buffer.
803 `makefile-target-colon':
804 The string that gets appended to all target names
805 inserted by `makefile-insert-target'.
806 \":\" or \"::\" are quite common values.
808 `makefile-macro-assign':
809 The string that gets appended to all macro names
810 inserted by `makefile-insert-macro'.
811 The normal value should be \" = \", since this is what
812 standard make expects. However, newer makes such as dmake
813 allow a larger variety of different macro assignments, so you
814 might prefer to use \" += \" or \" := \" .
816 `makefile-tab-after-target-colon':
817 If you want a TAB (instead of a space) to be appended after the
818 target colon, then set this to a non-nil value.
820 `makefile-browser-leftmost-column':
821 Number of blanks to the left of the browser selection mark.
823 `makefile-browser-cursor-column':
824 Column in which the cursor is positioned when it moves
825 up or down in the browser.
827 `makefile-browser-selected-mark':
828 String used to mark selected entries in the browser.
830 `makefile-browser-unselected-mark':
831 String used to mark unselected entries in the browser.
833 `makefile-browser-auto-advance-after-selection-p':
834 If this variable is set to a non-nil value the cursor
835 will automagically advance to the next line after an item
836 has been selected in the browser.
838 `makefile-pickup-everything-picks-up-filenames-p':
839 If this variable is set to a non-nil value then
840 `makefile-pickup-everything' also picks up filenames as targets
841 (i.e. it calls `makefile-pickup-filenames-as-targets'), otherwise
842 filenames are omitted.
844 `makefile-cleanup-continuations':
845 If this variable is set to a non-nil value then Makefile mode
846 will assure that no line in the file ends with a backslash
847 (the continuation character) followed by any whitespace.
848 This is done by silently removing the trailing whitespace, leaving
849 the backslash itself intact.
850 IMPORTANT: Please note that enabling this option causes Makefile mode
851 to MODIFY A FILE WITHOUT YOUR CONFIRMATION when \"it seems necessary\".
853 `makefile-browser-hook':
854 A function or list of functions to be called just before the
855 browser is entered. This is executed in the makefile buffer.
857 `makefile-special-targets-list':
858 List of special targets. You will be offered to complete
859 on one of those in the minibuffer whenever you enter a `.'.
860 at the beginning of a line in Makefile mode."
863 (kill-all-local-variables)
864 (add-hook 'write-file-functions
865 'makefile-warn-suspicious-lines nil t
)
866 (add-hook 'write-file-functions
867 'makefile-warn-continuations nil t
)
868 (add-hook 'write-file-functions
869 'makefile-cleanup-continuations nil t
)
870 (make-local-variable 'makefile-target-table
)
871 (make-local-variable 'makefile-macro-table
)
872 (make-local-variable 'makefile-has-prereqs
)
873 (make-local-variable 'makefile-need-target-pickup
)
874 (make-local-variable 'makefile-need-macro-pickup
)
877 (make-local-variable 'font-lock-defaults
)
878 (setq font-lock-defaults
879 ;; SYNTAX-BEGIN set to backward-paragraph to avoid slow-down
880 ;; near the end of a large buffer, due to parse-partial-sexp's
881 ;; trying to parse all the way till the beginning of buffer.
882 '(makefile-font-lock-keywords
886 (font-lock-syntactic-keywords
887 . makefile-font-lock-syntactic-keywords
)))
890 (make-local-variable 'add-log-current-defun-function
)
891 (setq add-log-current-defun-function
'makefile-add-log-defun
)
894 (make-local-variable 'imenu-generic-expression
)
895 (setq imenu-generic-expression makefile-imenu-generic-expression
)
898 (make-local-variable 'dabbrev-abbrev-skip-leading-regexp
)
899 (setq dabbrev-abbrev-skip-leading-regexp
"\\$")
902 (setq local-abbrev-table makefile-mode-abbrev-table
)
905 (make-local-variable 'fill-paragraph-function
)
906 (setq fill-paragraph-function
'makefile-fill-paragraph
)
909 (make-local-variable 'comment-start
)
910 (setq comment-start
"#")
911 (make-local-variable 'comment-end
)
912 (setq comment-end
"")
913 (make-local-variable 'comment-start-skip
)
914 (setq comment-start-skip
"#+[ \t]*")
916 ;; Make sure TAB really inserts \t.
917 (set (make-local-variable 'indent-line-function
) 'indent-to-left-margin
)
919 ;; become the current major mode
920 (setq major-mode
'makefile-mode
)
921 (setq mode-name
"Makefile")
923 ;; Activate keymap and syntax table.
924 (use-local-map makefile-mode-map
)
925 (set-syntax-table makefile-mode-syntax-table
)
927 ;; Real TABs are important in makefiles
928 (setq indent-tabs-mode t
)
929 (run-mode-hooks 'makefile-mode-hook
))
931 ;; These should do more than just differentiate font-lock.
933 (define-derived-mode makefile-automake-mode makefile-mode
"Makefile.am"
934 "An adapted `makefile-mode' that knows about automake."
935 (setq font-lock-defaults
936 `(makefile-automake-font-lock-keywords ,@(cdr font-lock-defaults
))))
939 (define-derived-mode makefile-gmake-mode makefile-mode
"GNUmakefile"
940 "An adapted `makefile-mode' that knows about gmake."
941 (setq font-lock-defaults
942 `(makefile-gmake-font-lock-keywords ,@(cdr font-lock-defaults
))))
945 (define-derived-mode makefile-makepp-mode makefile-mode
"Makeppfile"
946 "An adapted `makefile-mode' that knows about makepp."
947 (set (make-local-variable 'makefile-rule-action-regex
)
948 makefile-makepp-rule-action-regex
)
949 (setq font-lock-defaults
950 `(makefile-makepp-font-lock-keywords ,@(cdr font-lock-defaults
))
951 imenu-generic-expression
952 `(("Functions" "^[ \t]*\\(?:make\\)?sub[ \t]+\\([A-Za-z0-9_]+\\)" 1)
953 ,@imenu-generic-expression
)))
956 (define-derived-mode makefile-bsdmake-mode makefile-mode
"BSDmakefile"
957 "An adapted `makefile-mode' that knows about BSD make."
958 (set (make-local-variable 'makefile-dependency-regex
)
959 makefile-bsdmake-dependency-regex
)
960 (set (make-local-variable 'makefile-dependency-skip
) "^:!")
961 (set (make-local-variable 'makefile-rule-action-regex
)
962 makefile-bsdmake-rule-action-regex
)
963 (setq font-lock-defaults
964 `(makefile-bsdmake-font-lock-keywords ,@(cdr font-lock-defaults
))))
967 (define-derived-mode makefile-imake-mode makefile-mode
"Imakefile"
968 "An adapted `makefile-mode' that knows about imake."
969 :syntax-table makefile-imake-mode-syntax-table
970 (let ((base `(makefile-imake-font-lock-keywords ,@(cdr font-lock-defaults
)))
972 ;; Remove `font-lock-syntactic-keywords' entry from font-lock-defaults.
974 (unless (and (consp elt
)
975 (eq (car elt
) 'font-lock-syntactic-keywords
))
976 (setq new
(cons elt new
))))
978 (setq font-lock-defaults
(nreverse new
))))
984 (defun makefile-next-dependency ()
985 "Move point to the beginning of the next dependency line."
987 (let ((here (point)))
989 (if (makefile-match-dependency nil
)
990 (progn (beginning-of-line) t
) ; indicate success
991 (goto-char here
) nil
)))
993 (defun makefile-previous-dependency ()
994 "Move point to the beginning of the previous dependency line."
998 ;; makefile-match-dependency done backwards:
1000 (while (progn (skip-chars-backward makefile-dependency-skip
)
1002 (or (prog1 (eq (char-after) ?
=)
1004 (get-text-property (point) 'face
)
1006 (if (> (point) (+ (point-min) 2))
1007 (eq (char-before (1- (point))) ?
\\))
1008 (if (looking-at makefile-dependency-regex
)
1015 ;;; Electric keys. Blech.
1017 (defun makefile-electric-dot (arg)
1018 "Prompt for the name of a special target to insert.
1019 Only does electric insertion at beginning of line.
1020 Anywhere else just self-inserts."
1023 (makefile-insert-special-target)
1024 (self-insert-command arg
)))
1026 (defun makefile-insert-special-target ()
1027 "Prompt for and insert a special target name.
1028 Uses `makefile-special-targets' list."
1030 (makefile-pickup-targets)
1031 (let ((special-target
1032 (completing-read "Special target: "
1033 makefile-special-targets-list nil nil nil
)))
1034 (if (zerop (length special-target
))
1036 (insert "." special-target
":")
1037 (makefile-forward-after-target-colon))))
1039 (defun makefile-electric-equal (arg)
1040 "Prompt for name of a macro to insert.
1041 Only does prompting if point is at beginning of line.
1042 Anywhere else just self-inserts."
1044 (makefile-pickup-macros)
1046 (call-interactively 'makefile-insert-macro
)
1047 (self-insert-command arg
)))
1049 (defun makefile-insert-macro (macro-name)
1050 "Prepare definition of a new macro."
1051 (interactive "sMacro Name: ")
1052 (makefile-pickup-macros)
1053 (if (not (zerop (length macro-name
)))
1056 (insert macro-name makefile-macro-assign
)
1057 (setq makefile-need-macro-pickup t
)
1058 (makefile-remember-macro macro-name
))))
1060 (defun makefile-insert-macro-ref (macro-name)
1061 "Complete on a list of known macros, then insert complete ref at point."
1065 (makefile-pickup-macros)
1066 (completing-read "Refer to macro: " makefile-macro-table nil nil nil
))))
1067 (makefile-do-macro-insertion macro-name
))
1069 (defun makefile-insert-target (target-name)
1070 "Prepare definition of a new target (dependency line)."
1071 (interactive "sTarget: ")
1072 (if (not (zerop (length target-name
)))
1075 (insert target-name makefile-target-colon
)
1076 (makefile-forward-after-target-colon)
1078 (setq makefile-need-target-pickup t
)
1079 (makefile-remember-target target-name
))))
1081 (defun makefile-insert-target-ref (target-name)
1082 "Complete on a list of known targets, then insert TARGET-NAME at point."
1086 (makefile-pickup-targets)
1087 (completing-read "Refer to target: " makefile-target-table nil nil nil
))))
1088 (if (not (zerop (length target-name
)))
1089 (insert target-name
" ")))
1091 (defun makefile-electric-colon (arg)
1092 "Prompt for name of new target.
1093 Prompting only happens at beginning of line.
1094 Anywhere else just self-inserts."
1097 (call-interactively 'makefile-insert-target
)
1098 (self-insert-command arg
)))
1102 ;;; ------------------------------------------------------------
1103 ;;; Extracting targets and macros from an existing makefile
1104 ;;; ------------------------------------------------------------
1106 (defun makefile-pickup-targets ()
1107 "Notice names of all target definitions in Makefile."
1109 (when makefile-need-target-pickup
1110 (setq makefile-need-target-pickup nil
1111 makefile-target-table nil
1112 makefile-has-prereqs nil
)
1114 (goto-char (point-min))
1115 (while (makefile-match-dependency nil
)
1116 (goto-char (match-beginning 1))
1117 (while (let ((target-name
1118 (buffer-substring-no-properties (point)
1120 (skip-chars-forward "^ \t:#")
1123 (not (looking-at ":[ \t]*$"))))
1124 (if (makefile-remember-target target-name has-prereqs
)
1125 (message "Picked up target \"%s\" from line %d"
1126 target-name
(line-number-at-pos)))
1127 (skip-chars-forward " \t")
1128 (not (or (eolp) (eq (char-after) ?
:)))))
1130 (message "Read targets OK.")))
1132 (defun makefile-pickup-macros ()
1133 "Notice names of all macro definitions in Makefile."
1135 (when makefile-need-macro-pickup
1136 (setq makefile-need-macro-pickup nil
1137 makefile-macro-table nil
)
1139 (goto-char (point-min))
1140 (while (re-search-forward makefile-macroassign-regex nil t
)
1141 (goto-char (match-beginning 1))
1142 (let ((macro-name (buffer-substring-no-properties (point)
1144 (skip-chars-forward "^ \t:#=*")
1146 (if (makefile-remember-macro macro-name
)
1147 (message "Picked up macro \"%s\" from line %d"
1148 macro-name
(line-number-at-pos))))
1150 (message "Read macros OK.")))
1152 (defun makefile-pickup-everything (arg)
1153 "Notice names of all macros and targets in Makefile.
1154 Prefix arg means force pickups to be redone."
1157 (setq makefile-need-target-pickup t
1158 makefile-need-macro-pickup t
))
1159 (makefile-pickup-macros)
1160 (makefile-pickup-targets)
1161 (if makefile-pickup-everything-picks-up-filenames-p
1162 (makefile-pickup-filenames-as-targets)))
1164 (defun makefile-pickup-filenames-as-targets ()
1165 "Scan the current directory for filenames to use as targets.
1166 Checks each filename against `makefile-ignored-files-in-pickup-regex'
1167 and adds all qualifying names to the list of known targets."
1169 (mapc (lambda (name)
1170 (or (file-directory-p name
)
1171 (string-match makefile-ignored-files-in-pickup-regex name
)
1172 (if (makefile-remember-target name
)
1173 (message "Picked up file \"%s\" as target" name
))))
1174 (file-name-all-completions "" (or (file-name-directory (buffer-file-name)) ""))))
1180 (defun makefile-complete ()
1181 "Perform completion on Makefile construct preceding point.
1182 Can complete variable and target names.
1183 The context determines which are considered."
1185 (let* ((beg (save-excursion
1186 (skip-chars-backward "^$(){}:#= \t\n")
1188 (try (buffer-substring beg
(point)))
1193 (let ((pc (preceding-char)))
1195 ;; Preceding "$" means macros only.
1199 ;; Preceding "$(" or "${" means macros only.
1200 ((and (memq pc
'(?\
{ ?\
())
1202 (setq paren
(if (eq paren ?\
{) ?\
} ?\
)))
1204 (= (preceding-char) ?$
)))
1207 (table (apply-partially 'completion-table-with-terminator
1209 (do-macros (or paren
""))
1210 ((save-excursion (goto-char beg
) (bolp)) ":")
1212 (append (if do-macros
1214 makefile-target-table
)
1215 makefile-macro-table
))))
1216 (completion-in-region beg
(point) table
)))
1220 ;; Backslashification. Stolen from cc-mode.el.
1222 (defun makefile-backslash-region (from to delete-flag
)
1223 "Insert, align, or delete end-of-line backslashes on the lines in the region.
1224 With no argument, inserts backslashes and aligns existing backslashes.
1225 With an argument, deletes the backslashes.
1227 This function does not modify the last line of the region if the region ends
1228 right at the start of the following line; it does not modify blank lines
1229 at the start of the region. So you can put the region around an entire macro
1230 definition and conveniently use this command."
1231 (interactive "r\nP")
1234 (let ((column makefile-backslash-column
)
1235 (endmark (make-marker)))
1236 (move-marker endmark to
)
1237 ;; Compute the smallest column number past the ends of all the lines.
1238 (if makefile-backslash-align
1240 (if (not delete-flag
)
1241 (while (< (point) to
)
1243 (if (= (preceding-char) ?
\\)
1244 (progn (forward-char -
1)
1245 (skip-chars-backward " \t")))
1246 (setq column
(max column
(1+ (current-column))))
1248 ;; Adjust upward to a tab column, if that doesn't push
1250 (if (> (% column tab-width
) 0)
1251 (let ((adjusted (* (/ (+ column tab-width -
1) tab-width
)
1253 (if (< adjusted
(window-width))
1254 (setq column adjusted
))))))
1255 ;; Don't modify blank lines at start of region.
1257 (while (and (< (point) endmark
) (eolp))
1259 ;; Add or remove backslashes on all the lines.
1260 (while (and (< (point) endmark
)
1261 ;; Don't backslashify the last line
1262 ;; if the region ends right at the start of the next line.
1265 (< (point) endmark
)))
1266 (if (not delete-flag
)
1267 (makefile-append-backslash column
)
1268 (makefile-delete-backslash))
1270 (move-marker endmark nil
))))
1272 (defun makefile-append-backslash (column)
1274 ;; Note that "\\\\" is needed to get one backslash.
1275 (if (= (preceding-char) ?
\\)
1276 (progn (forward-char -
1)
1277 (delete-horizontal-space)
1278 (indent-to column
(if makefile-backslash-align nil
1)))
1279 (indent-to column
(if makefile-backslash-align nil
1))
1282 (defun makefile-delete-backslash ()
1287 (if (looking-at "\\\\")
1288 (delete-region (1+ (point))
1289 (progn (skip-chars-backward " \t") (point)))))))
1295 (defun makefile-fill-paragraph (arg)
1296 ;; Fill comments, backslashed lines, and variable definitions
1301 ((looking-at "^[ \t]*#+\\s-*")
1302 ;; Found a comment. Return nil to let normal filling take place.
1305 ;; Must look for backslashed-region before looking for variable
1307 ((or (eq (char-before (line-end-position 1)) ?
\\)
1308 (eq (char-before (line-end-position 0)) ?
\\))
1309 ;; A backslash region. Find beginning and end, remove
1310 ;; backslashes, fill, and then reapply backslahes.
1315 (while (= (preceding-char) ?
\\)
1321 (while (= (preceding-char) ?
\\)
1325 (narrow-to-region beginning end
)
1326 (makefile-backslash-region (point-min) (point-max) t
)
1327 (let ((fill-paragraph-function nil
))
1328 (fill-paragraph nil
))
1329 (makefile-backslash-region (point-min) (point-max) nil
)
1330 (goto-char (point-max))
1331 (if (< (skip-chars-backward "\n") 0)
1332 (delete-region (point) (point-max)))))
1333 ;; Return non-nil to indicate it's been filled.
1336 ((looking-at makefile-macroassign-regex
)
1337 ;; Have a macro assign. Fill just this line, and then backslash
1338 ;; resulting region.
1340 (narrow-to-region (point) (line-beginning-position 2))
1341 (let ((fill-paragraph-function nil
))
1342 (fill-paragraph nil
))
1343 (makefile-backslash-region (point-min) (point-max) nil
))
1344 ;; Return non-nil to indicate it's been filled.
1348 ;; Return non-nil so we don't fill anything else.
1353 ;;; ------------------------------------------------------------
1355 ;;; ------------------------------------------------------------
1357 (defun makefile-browser-format-target-line (target selected
)
1359 (concat (make-string makefile-browser-leftmost-column ?\
)
1361 makefile-browser-selected-mark
1362 makefile-browser-unselected-mark
)
1364 target makefile-target-colon
))
1366 (defun makefile-browser-format-macro-line (macro selected
)
1368 (concat (make-string makefile-browser-leftmost-column ?\
)
1370 makefile-browser-selected-mark
1371 makefile-browser-unselected-mark
)
1372 (makefile-format-macro-ref macro
))))
1374 (defun makefile-browser-fill (targets macros
)
1375 (let ((inhibit-read-only t
))
1376 (goto-char (point-min))
1380 (lambda (item) (insert (makefile-browser-format-target-line (car item
) nil
) "\n")))
1385 (lambda (item) (insert (makefile-browser-format-macro-line (car item
) nil
) "\n")))
1388 (sort-lines nil
(point-min) (point-max))
1389 (goto-char (1- (point-max)))
1390 (delete-char 1) ; remove unnecessary newline at eob
1391 (goto-char (point-min))
1392 (forward-char makefile-browser-cursor-column
)))
1395 ;;; Moving up and down in the browser
1398 (defun makefile-browser-next-line ()
1399 "Move the browser selection cursor to the next line."
1401 (if (not (makefile-last-line-p))
1404 (forward-char makefile-browser-cursor-column
))))
1406 (defun makefile-browser-previous-line ()
1407 "Move the browser selection cursor to the previous line."
1409 (if (not (makefile-first-line-p))
1412 (forward-char makefile-browser-cursor-column
))))
1415 ;;; Quitting the browser (returns to client buffer)
1418 (defun makefile-browser-quit ()
1419 "Leave the browser and return to the makefile buffer."
1421 (let ((my-client makefile-browser-client
))
1422 (setq makefile-browser-client nil
) ; we quitted, so NO client!
1423 (set-buffer-modified-p nil
)
1425 (pop-to-buffer my-client
)))
1428 ;;; Toggle state of a browser item
1431 (defun makefile-browser-toggle ()
1432 "Toggle the selection state of the browser item at the cursor position."
1434 (let ((this-line (count-lines (point-min) (point))))
1435 (setq this-line
(max 1 this-line
))
1436 (makefile-browser-toggle-state-for-line this-line
)
1437 (goto-char (point-min))
1438 (forward-line (1- this-line
))
1439 (let ((inhibit-read-only t
))
1440 (beginning-of-line) ; redundant?
1441 (if (makefile-browser-on-macro-line-p)
1442 (let ((macro-name (makefile-browser-this-line-macro-name)))
1443 (delete-region (point) (progn (end-of-line) (point)))
1445 (makefile-browser-format-macro-line
1447 (makefile-browser-get-state-for-line this-line
))))
1448 (let ((target-name (makefile-browser-this-line-target-name)))
1449 (delete-region (point) (progn (end-of-line) (point)))
1451 (makefile-browser-format-target-line
1453 (makefile-browser-get-state-for-line this-line
))))))
1455 (forward-char makefile-browser-cursor-column
)
1456 (if makefile-browser-auto-advance-after-selection-p
1457 (makefile-browser-next-line))))
1460 ;;; Making insertions into the client buffer
1463 (defun makefile-browser-insert-continuation ()
1464 "Insert a makefile continuation.
1465 In the makefile buffer, go to (end-of-line), insert a \'\\\'
1466 character, insert a new blank line, go to that line and indent by one TAB.
1467 This is most useful in the process of creating continued lines when copying
1468 large dependencies from the browser to the client buffer.
1469 \(point) advances accordingly in the client buffer."
1471 (with-current-buffer makefile-browser-client
1475 (defun makefile-browser-insert-selection ()
1476 "Insert all selected targets and/or macros in the makefile buffer.
1477 Insertion takes place at point."
1480 (goto-char (point-min))
1481 (let ((current-line 1))
1483 (if (makefile-browser-get-state-for-line current-line
)
1484 (makefile-browser-send-this-line-item))
1486 (setq current-line
(1+ current-line
))))))
1488 (defun makefile-browser-insert-selection-and-quit ()
1490 (makefile-browser-insert-selection)
1491 (makefile-browser-quit))
1493 (defun makefile-browser-send-this-line-item ()
1494 (if (makefile-browser-on-macro-line-p)
1496 (let ((macro-name (makefile-browser-this-line-macro-name)))
1497 (set-buffer makefile-browser-client
)
1498 (insert (makefile-format-macro-ref macro-name
) " ")))
1500 (let ((target-name (makefile-browser-this-line-target-name)))
1501 (set-buffer makefile-browser-client
)
1502 (insert target-name
" ")))))
1504 (defun makefile-browser-start-interaction ()
1505 (use-local-map makefile-browser-map
)
1506 (setq buffer-read-only t
))
1508 (defun makefile-browse (targets macros
)
1510 (if (zerop (+ (length targets
) (length macros
)))
1513 (message "No macros or targets to browse! Consider running 'makefile-pickup-everything\'"))
1514 (let ((browser-buffer (get-buffer-create makefile-browser-buffer-name
)))
1515 (pop-to-buffer browser-buffer
)
1516 (makefile-browser-fill targets macros
)
1517 (shrink-window-if-larger-than-buffer)
1518 (set (make-local-variable 'makefile-browser-selection-vector
)
1519 (make-vector (+ (length targets
) (length macros
)) nil
))
1520 (makefile-browser-start-interaction))))
1522 (defun makefile-switch-to-browser ()
1524 (run-hooks 'makefile-browser-hook
)
1525 (setq makefile-browser-client
(current-buffer))
1526 (makefile-pickup-targets)
1527 (makefile-pickup-macros)
1528 (makefile-browse makefile-target-table makefile-macro-table
))
1532 ;;; ------------------------------------------------------------
1533 ;;; Up-to-date overview buffer
1534 ;;; ------------------------------------------------------------
1536 (defun makefile-create-up-to-date-overview ()
1537 "Create a buffer containing an overview of the state of all known targets.
1538 Known targets are targets that are explicitly defined in that makefile;
1539 in other words, all targets that appear on the left hand side of a
1540 dependency in the makefile."
1542 (if (y-or-n-p "Are you sure that the makefile being edited is consistent? ")
1544 ;; The rest of this function operates on a temporary makefile, created by
1545 ;; writing the current contents of the makefile buffer.
1547 (let ((saved-target-table makefile-target-table
)
1548 (this-buffer (current-buffer))
1549 (makefile-up-to-date-buffer
1550 (get-buffer-create makefile-up-to-date-buffer-name
))
1551 (filename (makefile-save-temporary))
1553 ;; Forget the target table because it may contain picked-up filenames
1554 ;; that are not really targets in the current makefile.
1555 ;; We don't want to query these, so get a new target-table with just the
1556 ;; targets that can be found in the makefile buffer.
1557 ;; The 'old' target table will be restored later.
1559 (real-targets (progn
1560 (makefile-pickup-targets)
1561 makefile-target-table
))
1562 (prereqs makefile-has-prereqs
)
1565 (set-buffer makefile-up-to-date-buffer
)
1566 (setq buffer-read-only nil
)
1568 (makefile-query-targets filename real-targets prereqs
)
1569 (if (zerop (buffer-size)) ; if it did not get us anything
1571 (kill-buffer (current-buffer))
1572 (message "No overview created!")))
1573 (set-buffer this-buffer
)
1574 (setq makefile-target-table saved-target-table
)
1575 (if (get-buffer makefile-up-to-date-buffer-name
)
1577 (pop-to-buffer (get-buffer makefile-up-to-date-buffer-name
))
1578 (shrink-window-if-larger-than-buffer)
1579 (sort-lines nil
(point-min) (point-max))
1580 (setq buffer-read-only t
))))))
1582 (defun makefile-save-temporary ()
1583 "Create a temporary file from the current makefile buffer."
1584 (let ((filename (makefile-generate-temporary-filename)))
1585 (write-region (point-min) (point-max) filename nil
0)
1586 filename
)) ; return the filename
1588 (defun makefile-generate-temporary-filename ()
1589 "Create a filename suitable for use in `makefile-save-temporary'.
1590 Be careful to allow brain-dead file systems (DOS, SYSV ...) to cope
1591 with the generated name!"
1592 (let ((my-name (user-login-name))
1593 (my-uid (int-to-string (user-uid))))
1595 (if (> (length my-name
) 3)
1596 (substring my-name
0 3)
1599 (if (> (length my-uid
) 3)
1600 (substring my-uid
0 3)
1603 (defun makefile-query-targets (filename target-table prereq-list
)
1604 "Fill the up-to-date overview buffer.
1605 Checks each target in TARGET-TABLE using
1606 `makefile-query-one-target-method-function'
1607 and generates the overview, one line per target name."
1610 (function (lambda (item)
1611 (let* ((target-name (car item
))
1612 (no-prereqs (not (member target-name prereq-list
)))
1613 (needs-rebuild (or no-prereqs
1615 makefile-query-one-target-method-function
1620 (cond (no-prereqs " .. has no prerequisites")
1621 (needs-rebuild " .. NEEDS REBUILD")
1622 (t " .. is up to date"))))
1625 (goto-char (point-min))
1626 (delete-file filename
)) ; remove the tmpfile
1628 (defun makefile-query-by-make-minus-q (target &optional filename
)
1630 (call-process makefile-brave-make nil nil nil
1631 "-f" filename
"-q" target
))))
1635 ;;; ------------------------------------------------------------
1636 ;;; Continuation cleanup
1637 ;;; ------------------------------------------------------------
1639 (defun makefile-cleanup-continuations ()
1640 (if (derived-mode-p 'makefile-mode
)
1641 (if (and makefile-cleanup-continuations
1642 (not buffer-read-only
))
1644 (goto-char (point-min))
1645 (while (re-search-forward "\\\\[ \t]+$" nil t
)
1646 (replace-match "\\" t t
))))))
1649 ;;; ------------------------------------------------------------
1650 ;;; Warn of suspicious lines
1651 ;;; ------------------------------------------------------------
1653 (defun makefile-warn-suspicious-lines ()
1654 ;; Returning non-nil cancels the save operation
1655 (if (derived-mode-p 'makefile-mode
)
1657 (goto-char (point-min))
1658 (if (re-search-forward "^\\(\t+$\\| +\t\\)" nil t
)
1660 (format "Suspicious line %d. Save anyway? "
1661 (count-lines (point-min) (point)))))))))
1663 (defun makefile-warn-continuations ()
1664 (if (derived-mode-p 'makefile-mode
)
1666 (goto-char (point-min))
1667 (if (re-search-forward "\\\\[ \t]+$" nil t
)
1669 (format "Suspicious continuation in line %d. Save anyway? "
1670 (count-lines (point-min) (point)))))))))
1673 ;;; ------------------------------------------------------------
1674 ;;; GNU make function support
1675 ;;; ------------------------------------------------------------
1677 (defun makefile-insert-gmake-function ()
1678 "Insert a GNU make function call.
1679 Asks for the name of the function to use (with completion).
1680 Then prompts for all required parameters."
1682 (let* ((gm-function-name (completing-read
1684 makefile-gnumake-functions-alist
1686 (gm-function-prompts
1687 (cdr (assoc gm-function-name makefile-gnumake-functions-alist
))))
1688 (if (not (zerop (length gm-function-name
)))
1689 (insert (makefile-format-macro-ref
1690 (concat gm-function-name
" "
1691 (makefile-prompt-for-gmake-funargs
1692 gm-function-name gm-function-prompts
)))
1695 (defun makefile-prompt-for-gmake-funargs (function-name prompt-list
)
1697 (function (lambda (one-prompt)
1698 (read-string (format "[%s] %s: " function-name one-prompt
)
1705 ;;; ------------------------------------------------------------
1706 ;;; Utility functions
1707 ;;; ------------------------------------------------------------
1709 (defun makefile-match-function-end (end)
1710 "To be called as an anchored matcher by font-lock.
1711 The anchor must have matched the opening parens in the first group."
1712 (let ((s (match-string-no-properties 1)))
1713 ;; FIXME forward-sexp or somesuch would be better?
1714 (if (setq s
(cond ((string= s
"(") ")")
1715 ((string= s
"{") "}")
1716 ((string= s
"((") "))")
1717 ((string= s
"{{") "}}")))
1718 (re-search-forward (concat "\\(.*\\)[ \t]*" s
) (line-end-position) t
))))
1720 (defun makefile-match-dependency (bound)
1721 "Search for `makefile-dependency-regex' up to BOUND.
1722 Checks that the colon has not already been fontified, else we
1723 matched in a rule action."
1726 (while (progn (skip-chars-forward makefile-dependency-skip bound
)
1727 (< (point) (or bound
(point-max))))
1729 (or (eq (char-after) ?
=)
1730 (get-text-property (1- (point)) 'face
)
1731 (if (> (line-beginning-position) (+ (point-min) 2))
1732 (eq (char-before (line-end-position 0)) ?
\\))
1733 (when (save-excursion
1735 (looking-at makefile-dependency-regex
))
1737 (let ((deps-end (match-end 1))
1738 (match-data (match-data)))
1739 (goto-char deps-end
)
1740 (skip-chars-backward " \t")
1741 (setq deps-end
(point))
1743 (skip-chars-forward " \t")
1744 ;; Alter the bounds recorded for subexp 1,
1745 ;; which is what is supposed to match the targets.
1746 (setcar (nthcdr 2 match-data
) (point))
1747 (setcar (nthcdr 3 match-data
) deps-end
)
1748 (store-match-data match-data
)))
1750 (throw 'found
(point)))))
1754 (defun makefile-match-action (bound)
1756 (while (re-search-forward makefile-rule-action-regex bound t
)
1757 (or (eq ?
\\ (char-after (- (match-beginning 0) 2)))
1758 (throw 'found t
)))))
1760 (defun makefile-do-macro-insertion (macro-name)
1761 "Insert a macro reference."
1762 (if (not (zerop (length macro-name
)))
1763 (if (assoc macro-name makefile-runtime-macros-list
)
1764 (insert "$" macro-name
)
1765 (insert (makefile-format-macro-ref macro-name
)))))
1767 (defun makefile-remember-target (target-name &optional has-prereqs
)
1768 "Remember a given target if it is not already remembered for this buffer."
1769 (if (not (zerop (length target-name
)))
1771 (if (not (assoc target-name makefile-target-table
))
1772 (setq makefile-target-table
1773 (cons (list target-name
) makefile-target-table
)))
1775 (setq makefile-has-prereqs
1776 (cons target-name makefile-has-prereqs
))))))
1778 (defun makefile-remember-macro (macro-name)
1779 "Remember a given macro if it is not already remembered for this buffer."
1780 (if (not (zerop (length macro-name
)))
1781 (if (not (assoc macro-name makefile-macro-table
))
1782 (setq makefile-macro-table
1783 (cons (list macro-name
) makefile-macro-table
)))))
1785 (defun makefile-forward-after-target-colon ()
1786 "Move point forward after inserting the terminating colon of a target.
1787 This acts according to the value of `makefile-tab-after-target-colon'."
1788 (if makefile-tab-after-target-colon
1792 (defun makefile-browser-on-macro-line-p ()
1793 "Determine if point is on a macro line in the browser."
1796 (re-search-forward "\\$[{(]" (line-end-position) t
)))
1798 (defun makefile-browser-this-line-target-name ()
1799 "Extract the target name from a line in the browser."
1802 (skip-chars-backward "^ \t")
1803 (buffer-substring (point) (1- (line-end-position)))))
1805 (defun makefile-browser-this-line-macro-name ()
1806 "Extract the macro name from a line in the browser."
1809 (re-search-forward "\\$[{(]" (line-end-position) t
)
1810 (let ((macro-start (point)))
1811 (skip-chars-forward "^})")
1812 (buffer-substring macro-start
(point)))))
1814 (defun makefile-format-macro-ref (macro-name)
1815 "Format a macro reference.
1816 Uses `makefile-use-curly-braces-for-macros-p'."
1817 (if (or (char-equal ?\
( (string-to-char macro-name
))
1818 (char-equal ?\
{ (string-to-char macro-name
)))
1819 (format "$%s" macro-name
)
1820 (if makefile-use-curly-braces-for-macros-p
1821 (format "${%s}" macro-name
)
1822 (format "$(%s)" macro-name
))))
1824 (defun makefile-browser-get-state-for-line (n)
1825 (aref makefile-browser-selection-vector
(1- n
)))
1827 (defun makefile-browser-set-state-for-line (n to-state
)
1828 (aset makefile-browser-selection-vector
(1- n
) to-state
))
1830 (defun makefile-browser-toggle-state-for-line (n)
1831 (makefile-browser-set-state-for-line n
(not (makefile-browser-get-state-for-line n
))))
1833 (defun makefile-last-line-p ()
1834 (= (line-end-position) (point-max)))
1836 (defun makefile-first-line-p ()
1837 (= (line-beginning-position) (point-min)))
1841 ;;; Support for other packages, like add-log.
1843 (defun makefile-add-log-defun ()
1844 "Return name of target or variable assignment that point is in.
1845 If it isn't in one, return nil."
1849 ;; Scan back line by line, noticing when we come to a
1850 ;; variable or rule definition, and giving up when we see
1851 ;; a line that is not part of either of those.
1852 (while (not (or (setq found
1853 (when (or (looking-at makefile-macroassign-regex
)
1854 (looking-at makefile-dependency-regex
))
1855 (match-string-no-properties 1)))
1856 ;; Don't keep looking across a blank line or comment.
1857 (looking-at "$\\|#")
1858 (not (zerop (forward-line -
1))))))
1859 ;; Remove leading and trailing whitespace.
1861 (setq found
(replace-regexp-in-string "[ \t]+\\'" "" found
))
1862 (setq found
(replace-regexp-in-string "\\`[ \t]+" "" found
)))
1865 (provide 'make-mode
)
1867 ;; arch-tag: bd23545a-de91-44fb-b1b2-feafbb2635a0
1868 ;;; make-mode.el ends here