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 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 (put 'makefile-space-face
'face-alias
'makefile-space
)
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 (makefile-match-action
359 (1 font-lock-type-face
)
360 (2 'makefile-shell prepend
)
361 ;; Only makepp has builtin commands.
362 (3 font-lock-builtin-face prepend t
))
364 ;; Variable references even in targets/strings/comments.
365 (,var
1 font-lock-variable-name-face prepend
)
367 ;; Automatic variable references and single character variable references,
368 ;; but not shell variables references.
369 ("[^$]\\$\\([@%<?^+*_]\\|[a-zA-Z0-9]\\>\\)"
370 1 font-lock-constant-face prepend
)
371 ("[^$]\\(\\$[@%*]\\)"
372 1 'makefile-targets append
)
374 ;; Fontify conditionals and includes.
375 (,(concat "^\\(?: [ \t]*\\)?"
376 (regexp-opt keywords t
)
377 "\\>[ \t]*\\([^: \t\n#]*\\)")
378 (1 font-lock-keyword-face
) (2 font-lock-variable-name-face
))
381 `((,negation
(1 font-lock-negation-char-face prepend
)
382 (2 font-lock-negation-char-face prepend t
))))
385 '(;; Highlight lines that contain just whitespace.
386 ;; They can cause trouble, especially if they start with a tab.
387 ("^[ \t]+$" . makefile-space
)
389 ;; Highlight shell comments that Make treats as commands,
390 ;; since these can fool people.
391 ("^\t+#" 0 makefile-space t
)
393 ;; Highlight spaces that precede tabs.
394 ;; They can make a tab fail to be effective.
395 ("^\\( +\\)\t" 1 makefile-space
)))
400 (makefile-match-dependency
401 (1 'makefile-targets prepend
)
402 (3 'makefile-shell prepend t
))))
404 (defconst makefile-font-lock-keywords
405 (makefile-make-font-lock-keywords
406 makefile-var-use-regex
410 (defconst makefile-automake-font-lock-keywords
411 (makefile-make-font-lock-keywords
412 makefile-var-use-regex
413 makefile-automake-statements
416 (defconst makefile-gmake-font-lock-keywords
417 (makefile-make-font-lock-keywords
418 makefile-var-use-regex
419 makefile-gmake-statements
421 "^\\(?: [ \t]*\\)?if\\(n\\)\\(?:def\\|eq\\)\\>"
423 '("[^$]\\(\\$[({][@%*][DF][})]\\)"
424 1 'makefile-targets append
)
426 ;; $(function ...) ${function ...}
427 '("[^$]\\$[({]\\([-a-zA-Z0-9_.]+\\s \\)"
428 1 font-lock-function-name-face prepend
)
430 ;; $(shell ...) ${shell ...}
431 '("[^$]\\$\\([({]\\)shell[ \t]+"
432 makefile-match-function-end nil nil
433 (1 'makefile-shell prepend t
))))
435 (defconst makefile-makepp-font-lock-keywords
436 (makefile-make-font-lock-keywords
437 makefile-var-use-regex
438 makefile-makepp-statements
440 "^\\(?: [ \t]*\\)?\\(?:and[ \t]+\\|else[ \t]+\\|or[ \t]+\\)?if\\(n\\)\\(?:def\\|eq\\|sys\\)\\>"
442 '("[^$]\\(\\$[({]\\(?:output\\|stem\\|target\\)s?\\_>.*?[})]\\)"
443 1 'makefile-targets append
)
445 ;; Colon modifier keywords.
446 '("\\(:\\s *\\)\\(build_c\\(?:ache\\|heck\\)\\|env\\(?:ironment\\)?\\|foreach\\|signature\\|scanner\\|quickscan\\|smartscan\\)\\>\\([^:\n]*\\)"
447 (1 font-lock-type-face t
)
448 (2 font-lock-keyword-face t
)
449 (3 font-lock-variable-name-face t
))
451 ;; $(function ...) $((function ...)) ${function ...} ${{function ...}}
452 '("[^$]\\$\\(?:((?\\|{{?\\)\\([-a-zA-Z0-9_.]+\\s \\)"
453 1 font-lock-function-name-face prepend
)
455 ;; $(shell ...) $((shell ...)) ${shell ...} ${{shell ...}}
456 '("[^$]\\$\\(((?\\|{{?\\)shell\\(?:[-_]\\(?:global[-_]\\)?once\\)?[ \t]+"
457 makefile-match-function-end nil nil
458 (1 'makefile-shell prepend t
))
460 ;; $(perl ...) $((perl ...)) ${perl ...} ${{perl ...}}
461 '("[^$]\\$\\(((?\\|{{?\\)makeperl[ \t]+"
462 makefile-match-function-end nil nil
463 (1 'makefile-makepp-perl prepend t
))
464 '("[^$]\\$\\(((?\\|{{?\\)perl[ \t]+"
465 makefile-match-function-end nil nil
466 (1 'makefile-makepp-perl t t
))
468 ;; Can we unify these with (if (match-end 1) 'prepend t)?
469 '("ifmakeperl\\s +\\(.*\\)" 1 'makefile-makepp-perl prepend
)
470 '("ifperl\\s +\\(.*\\)" 1 'makefile-makepp-perl t
)
472 ;; Perl block single- or multiline, as statement or rule action.
473 ;; Don't know why the initial newline in 2nd variant of group 2 doesn't get skipped.
474 '("\\<make\\(?:perl\\|sub\\s +\\S +\\)\\s *\n?\\s *{\\(?:{\\s *\n?\\(\\(?:.*\n\\)+?\\)\\s *}\\|\\s *\\(\\(?:.*?\\|\n?\\(?:.*\n\\)+?\\)\\)\\)}"
475 (1 'makefile-makepp-perl prepend t
)
476 (2 'makefile-makepp-perl prepend t
))
477 '("\\<\\(?:perl\\|sub\\s +\\S +\\)\\s *\n?\\s *{\\(?:{\\s *\n?\\(\\(?:.*\n\\)+?\\)\\s *}\\|\\s *\\(\\(?:.*?\\|\n?\\(?:.*\n\\)+?\\)\\)\\)}"
478 (1 'makefile-makepp-perl t t
)
479 (2 'makefile-makepp-perl t t
))
481 ;; Statement style perl block.
482 '("perl[-_]begin\\s *\\(?:\\s #.*\\)?\n\\(\\(?:.*\n\\)+?\\)\\s *perl[-_]end\\>"
483 1 'makefile-makepp-perl t
)))
485 (defconst makefile-bsdmake-font-lock-keywords
486 (makefile-make-font-lock-keywords
487 ;; A lot more could be done for variables here:
488 makefile-var-use-regex
489 makefile-bsdmake-statements
491 "^\\(?: [ \t]*\\)?\\.\\(?:el\\)?if\\(n?\\)\\(?:def\\|make\\)?\\>[ \t]*\\(!?\\)"
492 '("^[ \t]*\\.for[ \t].+[ \t]\\(in\\)\\>" 1 font-lock-keyword-face
)))
494 (defconst makefile-imake-font-lock-keywords
496 (makefile-make-font-lock-keywords
497 makefile-var-use-regex
501 '("^XCOMM.*$" . font-lock-comment-face
)
502 '("XVAR\\(?:use\\|def\\)[0-9]" 0 font-lock-keyword-face prepend
)
503 '("@@" . font-lock-preprocessor-face
)
505 cpp-font-lock-keywords
))
508 (defconst makefile-font-lock-syntactic-keywords
509 ;; From sh-script.el.
510 ;; A `#' begins a comment in sh when it is unquoted and at the beginning
511 ;; of a word. In the shell, words are separated by metacharacters.
512 ;; The list of special chars is taken from the single-unix spec of the
513 ;; shell command language (under `quoting') but with `$' removed.
514 '(("[^|&;<>()`\\\"' \t\n]\\(#+\\)" 1 "_")
515 ;; Change the syntax of a quoted newline so that it does not end a comment.
518 (defvar makefile-imenu-generic-expression
519 `(("Dependencies" makefile-previous-dependency
1)
520 ("Macro Assignment" ,makefile-macroassign-regex
1))
521 "Imenu generic expression for Makefile mode. See `imenu-generic-expression'.")
523 ;;; ------------------------------------------------------------
524 ;;; The following configurable variables are used in the
525 ;;; up-to-date overview .
526 ;;; The standard configuration assumes that your `make' program
527 ;;; can be run in question/query mode using the `-q' option, this
528 ;;; means that the command
532 ;;; should return an exit status of zero if the target `foo' is
533 ;;; up to date and a nonzero exit status otherwise.
534 ;;; Many makes can do this although the docs/manpages do not mention
535 ;;; it. Try it with your favourite one. GNU make, System V make, and
536 ;;; Dennis Vadura's DMake have no problems.
537 ;;; Set the variable `makefile-brave-make' to the name of the
538 ;;; make utility that does this on your system.
539 ;;; To understand what this is all about see the function definition
540 ;;; of `makefile-query-by-make-minus-q' .
541 ;;; ------------------------------------------------------------
543 (defcustom makefile-brave-make
"make"
544 "*How to invoke make, for `makefile-query-targets'.
545 This should identify a `make' command that can handle the `-q' option."
549 (defcustom makefile-query-one-target-method-function
550 'makefile-query-by-make-minus-q
551 "*Function to call to determine whether a make target is up to date.
552 The function must satisfy this calling convention:
554 * As its first argument, it must accept the name of the target to
555 be checked, as a string.
557 * As its second argument, it may accept the name of a makefile
558 as a string. Depending on what you're going to do you may
561 * It must return the integer value 0 (zero) if the given target
562 should be considered up-to-date in the context of the given
563 makefile, any nonzero integer value otherwise."
566 (defvaralias 'makefile-query-one-target-method
567 'makefile-query-one-target-method-function
)
569 (defcustom makefile-up-to-date-buffer-name
"*Makefile Up-to-date overview*"
570 "*Name of the Up-to-date overview buffer."
574 ;;; --- end of up-to-date-overview configuration ------------------
576 (defvar makefile-mode-abbrev-table nil
577 "Abbrev table in use in Makefile buffers.")
578 (if makefile-mode-abbrev-table
580 (define-abbrev-table 'makefile-mode-abbrev-table
()))
582 (defvar makefile-mode-map
583 (let ((map (make-sparse-keymap))
584 (opt-map (make-sparse-keymap)))
586 (define-key map
"\C-c:" 'makefile-insert-target-ref
)
587 (if makefile-electric-keys
589 (define-key map
"$" 'makefile-insert-macro-ref
)
590 (define-key map
":" 'makefile-electric-colon
)
591 (define-key map
"=" 'makefile-electric-equal
)
592 (define-key map
"." 'makefile-electric-dot
)))
593 (define-key map
"\C-c\C-f" 'makefile-pickup-filenames-as-targets
)
594 (define-key map
"\C-c\C-b" 'makefile-switch-to-browser
)
595 (define-key map
"\C-c\C-c" 'comment-region
)
596 (define-key map
"\C-c\C-p" 'makefile-pickup-everything
)
597 (define-key map
"\C-c\C-u" 'makefile-create-up-to-date-overview
)
598 (define-key map
"\C-c\C-i" 'makefile-insert-gmake-function
)
599 (define-key map
"\C-c\C-\\" 'makefile-backslash-region
)
600 (define-key map
"\C-c\C-m\C-a" 'makefile-automake-mode
)
601 (define-key map
"\C-c\C-m\C-b" 'makefile-bsdmake-mode
)
602 (define-key map
"\C-c\C-m\C-g" 'makefile-gmake-mode
)
603 (define-key map
"\C-c\C-m\C-i" 'makefile-imake-mode
)
604 (define-key map
"\C-c\C-m\C-m" 'makefile-mode
)
605 (define-key map
"\C-c\C-m\C-p" 'makefile-makepp-mode
)
606 (define-key map
"\M-p" 'makefile-previous-dependency
)
607 (define-key map
"\M-n" 'makefile-next-dependency
)
608 (define-key map
"\e\t" 'makefile-complete
)
611 (define-key map
[menu-bar makefile-mode
]
612 (cons "Makefile" (make-sparse-keymap "Makefile")))
614 (define-key map
[menu-bar makefile-mode makefile-type
]
615 (cons "Switch Makefile Type" opt-map
))
616 (define-key opt-map
[makefile-makepp-mode
]
617 '(menu-item "Makepp" makefile-makepp-mode
618 :help
"An adapted `makefile-mode' that knows about makepp"
619 :button
(:radio .
(eq major-mode
'makefile-makepp-mode
))))
620 (define-key opt-map
[makefile-imake-mode
]
621 '(menu-item "Imake" makefile-imake-mode
622 :help
"An adapted `makefile-mode' that knows about imake"
623 :button
(:radio .
(eq major-mode
'makefile-imake-mode
))))
624 (define-key opt-map
[makefile-mode
]
625 '(menu-item "Classic" makefile-mode
626 :help
"`makefile-mode' with no special functionality"
627 :button
(:radio .
(eq major-mode
'makefile-mode
))))
628 (define-key opt-map
[makefile-bsdmake-mode
]
629 '(menu-item "BSD" makefile-bsdmake-mode
630 :help
"An adapted `makefile-mode' that knows about BSD make"
631 :button
(:radio .
(eq major-mode
'makefile-bsdmake-mode
))))
632 (define-key opt-map
[makefile-automake-mode
]
633 '(menu-item "Automake" makefile-automake-mode
634 :help
"An adapted `makefile-mode' that knows about automake"
635 :button
(:radio .
(eq major-mode
'makefile-automake-mode
))))
636 (define-key opt-map
[makefile-gmake-mode
]
637 '(menu-item "GNU make" makefile-gmake-mode
638 :help
"An adapted `makefile-mode' that knows about GNU make"
639 :button
(:radio .
(eq major-mode
'makefile-gmake-mode
))))
640 (define-key map
[menu-bar makefile-mode browse
]
641 '(menu-item "Pop up Makefile Browser" makefile-switch-to-browser
642 ;; XXX: this needs a better string, the function is not documented...
643 :help
"Pop up Makefile Browser"))
644 (define-key map
[menu-bar makefile-mode overview
]
645 '(menu-item "Up To Date Overview" makefile-create-up-to-date-overview
646 :help
"Create a buffer containing an overview of the state of all known targets"))
648 (define-key map
[menu-bar makefile-mode separator1
] '("----"))
649 (define-key map
[menu-bar makefile-mode pickup-file
]
650 '(menu-item "Pick File Name as Target" makefile-pickup-filenames-as-targets
651 :help
"Scan the current directory for filenames to use as targets"))
652 (define-key map
[menu-bar makefile-mode function
]
653 '(menu-item "Insert GNU make function" makefile-insert-gmake-function
654 :help
"Insert a GNU make function call"))
655 (define-key map
[menu-bar makefile-mode pickup
]
656 '(menu-item "Find Targets and Macros" makefile-pickup-everything
657 :help
"Notice names of all macros and targets in Makefile"))
658 (define-key map
[menu-bar makefile-mode complete
]
659 '(menu-item "Complete Target or Macro" makefile-complete
660 :help
"Perform completion on Makefile construct preceding point"))
661 (define-key map
[menu-bar makefile-mode backslash
]
662 '(menu-item "Backslash Region" makefile-backslash-region
663 :help
"Insert, align, or delete end-of-line backslashes on the lines in the region"))
665 (define-key map
[menu-bar makefile-mode separator
] '("----"))
666 (define-key map
[menu-bar makefile-mode prev
]
667 '(menu-item "Move to Previous Dependency" makefile-previous-dependency
668 :help
"Move point to the beginning of the previous dependency line"))
669 (define-key map
[menu-bar makefile-mode next
]
670 '(menu-item "Move to Next Dependency" makefile-next-dependency
671 :help
"Move point to the beginning of the next dependency line"))
673 "The keymap that is used in Makefile mode.")
676 (defvar makefile-browser-map
677 (let ((map (make-sparse-keymap)))
678 (define-key map
"n" 'makefile-browser-next-line
)
679 (define-key map
"\C-n" 'makefile-browser-next-line
)
680 (define-key map
"p" 'makefile-browser-previous-line
)
681 (define-key map
"\C-p" 'makefile-browser-previous-line
)
682 (define-key map
" " 'makefile-browser-toggle
)
683 (define-key map
"i" 'makefile-browser-insert-selection
)
684 (define-key map
"I" 'makefile-browser-insert-selection-and-quit
)
685 (define-key map
"\C-c\C-m" 'makefile-browser-insert-continuation
)
686 (define-key map
"q" 'makefile-browser-quit
)
687 ;; disable horizontal movement
688 (define-key map
"\C-b" 'undefined
)
689 (define-key map
"\C-f" 'undefined
)
691 "The keymap that is used in the macro- and target browser.")
694 (defvar makefile-mode-syntax-table
695 (let ((st (make-syntax-table)))
696 (modify-syntax-entry ?\
( "() " st
)
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 ?
\n "> " st
)
708 (defvar makefile-imake-mode-syntax-table
(copy-syntax-table
709 makefile-mode-syntax-table
))
710 (if makefile-imake-mode-syntax-table
712 (modify-syntax-entry ?
/ ". 14" makefile-imake-mode-syntax-table
)
713 (modify-syntax-entry ?
* ". 23" makefile-imake-mode-syntax-table
)
714 (modify-syntax-entry ?
# "'" makefile-imake-mode-syntax-table
)
715 (modify-syntax-entry ?
\n ". b" makefile-imake-mode-syntax-table
))
718 ;;; ------------------------------------------------------------
719 ;;; Internal variables.
720 ;;; You don't need to configure below this line.
721 ;;; ------------------------------------------------------------
723 (defvar makefile-target-table nil
724 "Table of all target names known for this buffer.")
725 (put 'makefile-target-table
'risky-local-variable t
)
727 (defvar makefile-macro-table nil
728 "Table of all macro names known for this buffer.")
729 (put 'makefile-macro-table
'risky-local-variable t
)
731 (defvar makefile-browser-client
732 "A buffer in Makefile mode that is currently using the browser.")
734 (defvar makefile-browser-selection-vector nil
)
735 (defvar makefile-has-prereqs nil
)
736 (defvar makefile-need-target-pickup t
)
737 (defvar makefile-need-macro-pickup t
)
739 (defvar makefile-mode-hook
'())
741 ;; Each element looks like '("GNU MAKE FUNCTION" "ARG" "ARG" ... )
742 ;; Each "ARG" is used as a prompt for a required argument.
743 (defconst makefile-gnumake-functions-alist
746 ("subst" "From" "To" "In")
747 ("patsubst" "Pattern" "Replacement" "In")
749 ("findstring" "Find what" "In")
750 ("filter" "Pattern" "Text")
751 ("filter-out" "Pattern" "Text")
753 ;; Filename functions
758 ("addprefix" "Prefix" "Names")
759 ("addsuffix" "Suffix" "Names")
760 ("join" "List 1" "List 2")
761 ("word" "Index" "Text")
764 ("wildcard" "Pattern")
766 ("foreach" "Variable" "List" "Text")
767 ("origin" "Variable")
768 ("shell" "Command")))
771 ;;; ------------------------------------------------------------
772 ;;; The mode function itself.
773 ;;; ------------------------------------------------------------
776 (defun makefile-mode ()
777 "Major mode for editing standard Makefiles.
779 If you are editing a file for a different make, try one of the
780 variants `makefile-automake-mode', `makefile-gmake-mode',
781 `makefile-makepp-mode', `makefile-bsdmake-mode' or,
782 `makefile-imake-mode'. All but the last should be correctly
783 chosen based on the file name, except if it is *.mk. This
784 function ends by invoking the function(s) `makefile-mode-hook'.
786 It is strongly recommended to use `font-lock-mode', because that
787 provides additional parsing information. This is used for
788 example to see that a rule action `echo foo: bar' is a not rule
789 dependency, despite the colon.
791 \\{makefile-mode-map}
793 In the browser, use the following keys:
795 \\{makefile-browser-map}
797 Makefile mode can be configured by modifying the following variables:
799 `makefile-browser-buffer-name':
800 Name of the macro- and target browser buffer.
802 `makefile-target-colon':
803 The string that gets appended to all target names
804 inserted by `makefile-insert-target'.
805 \":\" or \"::\" are quite common values.
807 `makefile-macro-assign':
808 The string that gets appended to all macro names
809 inserted by `makefile-insert-macro'.
810 The normal value should be \" = \", since this is what
811 standard make expects. However, newer makes such as dmake
812 allow a larger variety of different macro assignments, so you
813 might prefer to use \" += \" or \" := \" .
815 `makefile-tab-after-target-colon':
816 If you want a TAB (instead of a space) to be appended after the
817 target colon, then set this to a non-nil value.
819 `makefile-browser-leftmost-column':
820 Number of blanks to the left of the browser selection mark.
822 `makefile-browser-cursor-column':
823 Column in which the cursor is positioned when it moves
824 up or down in the browser.
826 `makefile-browser-selected-mark':
827 String used to mark selected entries in the browser.
829 `makefile-browser-unselected-mark':
830 String used to mark unselected entries in the browser.
832 `makefile-browser-auto-advance-after-selection-p':
833 If this variable is set to a non-nil value the cursor
834 will automagically advance to the next line after an item
835 has been selected in the browser.
837 `makefile-pickup-everything-picks-up-filenames-p':
838 If this variable is set to a non-nil value then
839 `makefile-pickup-everything' also picks up filenames as targets
840 (i.e. it calls `makefile-pickup-filenames-as-targets'), otherwise
841 filenames are omitted.
843 `makefile-cleanup-continuations':
844 If this variable is set to a non-nil value then Makefile mode
845 will assure that no line in the file ends with a backslash
846 (the continuation character) followed by any whitespace.
847 This is done by silently removing the trailing whitespace, leaving
848 the backslash itself intact.
849 IMPORTANT: Please note that enabling this option causes Makefile mode
850 to MODIFY A FILE WITHOUT YOUR CONFIRMATION when \"it seems necessary\".
852 `makefile-browser-hook':
853 A function or list of functions to be called just before the
854 browser is entered. This is executed in the makefile buffer.
856 `makefile-special-targets-list':
857 List of special targets. You will be offered to complete
858 on one of those in the minibuffer whenever you enter a `.'.
859 at the beginning of a line in Makefile mode."
862 (kill-all-local-variables)
863 (add-hook 'write-file-functions
864 'makefile-warn-suspicious-lines nil t
)
865 (add-hook 'write-file-functions
866 'makefile-warn-continuations nil t
)
867 (add-hook 'write-file-functions
868 'makefile-cleanup-continuations nil t
)
869 (make-local-variable 'makefile-target-table
)
870 (make-local-variable 'makefile-macro-table
)
871 (make-local-variable 'makefile-has-prereqs
)
872 (make-local-variable 'makefile-need-target-pickup
)
873 (make-local-variable 'makefile-need-macro-pickup
)
876 (make-local-variable 'font-lock-defaults
)
877 (setq font-lock-defaults
878 ;; SYNTAX-BEGIN set to backward-paragraph to avoid slow-down
879 ;; near the end of a large buffer, due to parse-partial-sexp's
880 ;; trying to parse all the way till the beginning of buffer.
881 '(makefile-font-lock-keywords
885 (font-lock-syntactic-keywords
886 . makefile-font-lock-syntactic-keywords
)))
889 (make-local-variable 'add-log-current-defun-function
)
890 (setq add-log-current-defun-function
'makefile-add-log-defun
)
893 (make-local-variable 'imenu-generic-expression
)
894 (setq imenu-generic-expression makefile-imenu-generic-expression
)
897 (make-local-variable 'dabbrev-abbrev-skip-leading-regexp
)
898 (setq dabbrev-abbrev-skip-leading-regexp
"\\$")
901 (setq local-abbrev-table makefile-mode-abbrev-table
)
904 (make-local-variable 'fill-paragraph-function
)
905 (setq fill-paragraph-function
'makefile-fill-paragraph
)
908 (make-local-variable 'comment-start
)
909 (setq comment-start
"#")
910 (make-local-variable 'comment-end
)
911 (setq comment-end
"")
912 (make-local-variable 'comment-start-skip
)
913 (setq comment-start-skip
"#+[ \t]*")
915 ;; Make sure TAB really inserts \t.
916 (set (make-local-variable 'indent-line-function
) 'indent-to-left-margin
)
918 ;; become the current major mode
919 (setq major-mode
'makefile-mode
)
920 (setq mode-name
"Makefile")
922 ;; Activate keymap and syntax table.
923 (use-local-map makefile-mode-map
)
924 (set-syntax-table makefile-mode-syntax-table
)
926 ;; Real TABs are important in makefiles
927 (setq indent-tabs-mode t
)
928 (run-mode-hooks 'makefile-mode-hook
))
930 ;; These should do more than just differentiate font-lock.
932 (define-derived-mode makefile-automake-mode makefile-mode
"Makefile.am"
933 "An adapted `makefile-mode' that knows about automake."
934 (setq font-lock-defaults
935 `(makefile-automake-font-lock-keywords ,@(cdr font-lock-defaults
))))
938 (define-derived-mode makefile-gmake-mode makefile-mode
"GNUmakefile"
939 "An adapted `makefile-mode' that knows about gmake."
940 (setq font-lock-defaults
941 `(makefile-gmake-font-lock-keywords ,@(cdr font-lock-defaults
))))
944 (define-derived-mode makefile-makepp-mode makefile-mode
"Makeppfile"
945 "An adapted `makefile-mode' that knows about makepp."
946 (set (make-local-variable 'makefile-rule-action-regex
)
947 makefile-makepp-rule-action-regex
)
948 (setq font-lock-defaults
949 `(makefile-makepp-font-lock-keywords ,@(cdr font-lock-defaults
))
950 imenu-generic-expression
951 `(("Functions" "^[ \t]*\\(?:make\\)?sub[ \t]+\\([A-Za-z0-9_]+\\)" 1)
952 ,@imenu-generic-expression
)))
955 (define-derived-mode makefile-bsdmake-mode makefile-mode
"BSDmakefile"
956 "An adapted `makefile-mode' that knows about BSD make."
957 (set (make-local-variable 'makefile-dependency-regex
)
958 makefile-bsdmake-dependency-regex
)
959 (set (make-local-variable 'makefile-dependency-skip
) "^:!")
960 (set (make-local-variable 'makefile-rule-action-regex
)
961 makefile-bsdmake-rule-action-regex
)
962 (setq font-lock-defaults
963 `(makefile-bsdmake-font-lock-keywords ,@(cdr font-lock-defaults
))))
966 (define-derived-mode makefile-imake-mode makefile-mode
"Imakefile"
967 "An adapted `makefile-mode' that knows about imake."
968 :syntax-table makefile-imake-mode-syntax-table
969 (let ((base `(makefile-imake-font-lock-keywords ,@(cdr font-lock-defaults
)))
971 ;; Remove `font-lock-syntactic-keywords' entry from font-lock-defaults.
973 (unless (and (consp elt
)
974 (eq (car elt
) 'font-lock-syntactic-keywords
))
975 (setq new
(cons elt new
))))
977 (setq font-lock-defaults
(nreverse new
))))
983 (defun makefile-next-dependency ()
984 "Move point to the beginning of the next dependency line."
986 (let ((here (point)))
988 (if (makefile-match-dependency nil
)
989 (progn (beginning-of-line) t
) ; indicate success
990 (goto-char here
) nil
)))
992 (defun makefile-previous-dependency ()
993 "Move point to the beginning of the previous dependency line."
997 ;; makefile-match-dependency done backwards:
999 (while (progn (skip-chars-backward makefile-dependency-skip
)
1001 (or (prog1 (eq (char-after) ?
=)
1003 (get-text-property (point) 'face
)
1005 (if (> (point) (+ (point-min) 2))
1006 (eq (char-before (1- (point))) ?
\\))
1007 (if (looking-at makefile-dependency-regex
)
1014 ;;; Electric keys. Blech.
1016 (defun makefile-electric-dot (arg)
1017 "Prompt for the name of a special target to insert.
1018 Only does electric insertion at beginning of line.
1019 Anywhere else just self-inserts."
1022 (makefile-insert-special-target)
1023 (self-insert-command arg
)))
1025 (defun makefile-insert-special-target ()
1026 "Prompt for and insert a special target name.
1027 Uses `makefile-special-targets' list."
1029 (makefile-pickup-targets)
1030 (let ((special-target
1031 (completing-read "Special target: "
1032 makefile-special-targets-list nil nil nil
)))
1033 (if (zerop (length special-target
))
1035 (insert "." special-target
":")
1036 (makefile-forward-after-target-colon))))
1038 (defun makefile-electric-equal (arg)
1039 "Prompt for name of a macro to insert.
1040 Only does prompting if point is at beginning of line.
1041 Anywhere else just self-inserts."
1043 (makefile-pickup-macros)
1045 (call-interactively 'makefile-insert-macro
)
1046 (self-insert-command arg
)))
1048 (defun makefile-insert-macro (macro-name)
1049 "Prepare definition of a new macro."
1050 (interactive "sMacro Name: ")
1051 (makefile-pickup-macros)
1052 (if (not (zerop (length macro-name
)))
1055 (insert macro-name makefile-macro-assign
)
1056 (setq makefile-need-macro-pickup t
)
1057 (makefile-remember-macro macro-name
))))
1059 (defun makefile-insert-macro-ref (macro-name)
1060 "Complete on a list of known macros, then insert complete ref at point."
1064 (makefile-pickup-macros)
1065 (completing-read "Refer to macro: " makefile-macro-table nil nil nil
))))
1066 (makefile-do-macro-insertion macro-name
))
1068 (defun makefile-insert-target (target-name)
1069 "Prepare definition of a new target (dependency line)."
1070 (interactive "sTarget: ")
1071 (if (not (zerop (length target-name
)))
1074 (insert target-name makefile-target-colon
)
1075 (makefile-forward-after-target-colon)
1077 (setq makefile-need-target-pickup t
)
1078 (makefile-remember-target target-name
))))
1080 (defun makefile-insert-target-ref (target-name)
1081 "Complete on a list of known targets, then insert TARGET-NAME at point."
1085 (makefile-pickup-targets)
1086 (completing-read "Refer to target: " makefile-target-table nil nil nil
))))
1087 (if (not (zerop (length target-name
)))
1088 (insert target-name
" ")))
1090 (defun makefile-electric-colon (arg)
1091 "Prompt for name of new target.
1092 Prompting only happens at beginning of line.
1093 Anywhere else just self-inserts."
1096 (call-interactively 'makefile-insert-target
)
1097 (self-insert-command arg
)))
1101 ;;; ------------------------------------------------------------
1102 ;;; Extracting targets and macros from an existing makefile
1103 ;;; ------------------------------------------------------------
1105 (defun makefile-pickup-targets ()
1106 "Notice names of all target definitions in Makefile."
1108 (when makefile-need-target-pickup
1109 (setq makefile-need-target-pickup nil
1110 makefile-target-table nil
1111 makefile-has-prereqs nil
)
1113 (goto-char (point-min))
1114 (while (makefile-match-dependency nil
)
1115 (goto-char (match-beginning 1))
1116 (while (let ((target-name
1117 (buffer-substring-no-properties (point)
1119 (skip-chars-forward "^ \t:#")
1122 (not (looking-at ":[ \t]*$"))))
1123 (if (makefile-remember-target target-name has-prereqs
)
1124 (message "Picked up target \"%s\" from line %d"
1125 target-name
(line-number-at-pos)))
1126 (skip-chars-forward " \t")
1127 (not (or (eolp) (eq (char-after) ?
:)))))
1129 (message "Read targets OK.")))
1131 (defun makefile-pickup-macros ()
1132 "Notice names of all macro definitions in Makefile."
1134 (when makefile-need-macro-pickup
1135 (setq makefile-need-macro-pickup nil
1136 makefile-macro-table nil
)
1138 (goto-char (point-min))
1139 (while (re-search-forward makefile-macroassign-regex nil t
)
1140 (goto-char (match-beginning 1))
1141 (let ((macro-name (buffer-substring-no-properties (point)
1143 (skip-chars-forward "^ \t:#=*")
1145 (if (makefile-remember-macro macro-name
)
1146 (message "Picked up macro \"%s\" from line %d"
1147 macro-name
(line-number-at-pos))))
1149 (message "Read macros OK.")))
1151 (defun makefile-pickup-everything (arg)
1152 "Notice names of all macros and targets in Makefile.
1153 Prefix arg means force pickups to be redone."
1156 (setq makefile-need-target-pickup t
1157 makefile-need-macro-pickup t
))
1158 (makefile-pickup-macros)
1159 (makefile-pickup-targets)
1160 (if makefile-pickup-everything-picks-up-filenames-p
1161 (makefile-pickup-filenames-as-targets)))
1163 (defun makefile-pickup-filenames-as-targets ()
1164 "Scan the current directory for filenames to use as targets.
1165 Checks each filename against `makefile-ignored-files-in-pickup-regex'
1166 and adds all qualifying names to the list of known targets."
1168 (mapc (lambda (name)
1169 (or (file-directory-p name
)
1170 (string-match makefile-ignored-files-in-pickup-regex name
)
1171 (if (makefile-remember-target name
)
1172 (message "Picked up file \"%s\" as target" name
))))
1173 (file-name-all-completions "" (or (file-name-directory (buffer-file-name)) ""))))
1179 (defun makefile-complete ()
1180 "Perform completion on Makefile construct preceding point.
1181 Can complete variable and target names.
1182 The context determines which are considered."
1184 (let* ((beg (save-excursion
1185 (skip-chars-backward "^$(){}:#= \t\n")
1187 (try (buffer-substring beg
(point)))
1193 (let ((pc (preceding-char)))
1195 ;; Beginning of line means anything.
1199 ;; Preceding "$" means macros only.
1203 ;; Preceding "$(" or "${" means macros only.
1210 (= (preceding-char) ?$
))))
1211 (setq do-macros t
)))))
1214 (let* ((table (append (if do-macros
1216 makefile-target-table
)
1217 makefile-macro-table
))
1218 (completion (try-completion try table
)))
1220 ;; Exact match, so insert closing paren or colon.
1222 (insert (if do-macros
1234 (message "Can't find completion for \"%s\"" try
)
1237 ;; Partial completion.
1238 ((not (string= try completion
))
1239 ;; FIXME it would be nice to supply the closing paren if an
1240 ;; exact, unambiguous match were found. That is not possible
1241 ;; right now. Ditto closing ":" for targets.
1242 (delete-region beg
(point))
1244 ;; DO-MACROS means doing macros only. If not that, then check
1245 ;; to see if this completion is a macro. Special insertion
1246 ;; must be done for macros.
1248 (assoc completion makefile-macro-table
))
1249 (let ((makefile-use-curly-braces-for-macros-p
1251 makefile-use-curly-braces-for-macros-p
)))
1252 (delete-backward-char 2)
1253 (makefile-do-macro-insertion completion
)
1254 (delete-backward-char 1))
1256 ;; Just insert targets.
1257 (insert completion
)))
1259 ;; Can't complete any more, so make completion list. FIXME
1260 ;; this doesn't do the right thing when the completion is
1261 ;; actually inserted. I don't think there is an easy way to do
1264 (message "Making completion list...")
1265 (let ((list (all-completions try table
)))
1266 (with-output-to-temp-buffer "*Completions*"
1267 (display-completion-list list try
)))
1268 (message "Making completion list...done"))))))
1272 ;; Backslashification. Stolen from cc-mode.el.
1274 (defun makefile-backslash-region (from to delete-flag
)
1275 "Insert, align, or delete end-of-line backslashes on the lines in the region.
1276 With no argument, inserts backslashes and aligns existing backslashes.
1277 With an argument, deletes the backslashes.
1279 This function does not modify the last line of the region if the region ends
1280 right at the start of the following line; it does not modify blank lines
1281 at the start of the region. So you can put the region around an entire macro
1282 definition and conveniently use this command."
1283 (interactive "r\nP")
1286 (let ((column makefile-backslash-column
)
1287 (endmark (make-marker)))
1288 (move-marker endmark to
)
1289 ;; Compute the smallest column number past the ends of all the lines.
1290 (if makefile-backslash-align
1292 (if (not delete-flag
)
1293 (while (< (point) to
)
1295 (if (= (preceding-char) ?
\\)
1296 (progn (forward-char -
1)
1297 (skip-chars-backward " \t")))
1298 (setq column
(max column
(1+ (current-column))))
1300 ;; Adjust upward to a tab column, if that doesn't push
1302 (if (> (% column tab-width
) 0)
1303 (let ((adjusted (* (/ (+ column tab-width -
1) tab-width
)
1305 (if (< adjusted
(window-width))
1306 (setq column adjusted
))))))
1307 ;; Don't modify blank lines at start of region.
1309 (while (and (< (point) endmark
) (eolp))
1311 ;; Add or remove backslashes on all the lines.
1312 (while (and (< (point) endmark
)
1313 ;; Don't backslashify the last line
1314 ;; if the region ends right at the start of the next line.
1317 (< (point) endmark
)))
1318 (if (not delete-flag
)
1319 (makefile-append-backslash column
)
1320 (makefile-delete-backslash))
1322 (move-marker endmark nil
))))
1324 (defun makefile-append-backslash (column)
1326 ;; Note that "\\\\" is needed to get one backslash.
1327 (if (= (preceding-char) ?
\\)
1328 (progn (forward-char -
1)
1329 (delete-horizontal-space)
1330 (indent-to column
(if makefile-backslash-align nil
1)))
1331 (indent-to column
(if makefile-backslash-align nil
1))
1334 (defun makefile-delete-backslash ()
1339 (if (looking-at "\\\\")
1340 (delete-region (1+ (point))
1341 (progn (skip-chars-backward " \t") (point)))))))
1347 (defun makefile-fill-paragraph (arg)
1348 ;; Fill comments, backslashed lines, and variable definitions
1353 ((looking-at "^[ \t]*#+\\s-*")
1354 ;; Found a comment. Return nil to let normal filling take place.
1357 ;; Must look for backslashed-region before looking for variable
1359 ((or (eq (char-before (line-end-position 1)) ?
\\)
1360 (eq (char-before (line-end-position 0)) ?
\\))
1361 ;; A backslash region. Find beginning and end, remove
1362 ;; backslashes, fill, and then reapply backslahes.
1367 (while (= (preceding-char) ?
\\)
1373 (while (= (preceding-char) ?
\\)
1377 (narrow-to-region beginning end
)
1378 (makefile-backslash-region (point-min) (point-max) t
)
1379 (let ((fill-paragraph-function nil
))
1380 (fill-paragraph nil
))
1381 (makefile-backslash-region (point-min) (point-max) nil
)
1382 (goto-char (point-max))
1383 (if (< (skip-chars-backward "\n") 0)
1384 (delete-region (point) (point-max)))))
1385 ;; Return non-nil to indicate it's been filled.
1388 ((looking-at makefile-macroassign-regex
)
1389 ;; Have a macro assign. Fill just this line, and then backslash
1390 ;; resulting region.
1392 (narrow-to-region (point) (line-beginning-position 2))
1393 (let ((fill-paragraph-function nil
))
1394 (fill-paragraph nil
))
1395 (makefile-backslash-region (point-min) (point-max) nil
))
1396 ;; Return non-nil to indicate it's been filled.
1400 ;; Return non-nil so we don't fill anything else.
1405 ;;; ------------------------------------------------------------
1407 ;;; ------------------------------------------------------------
1409 (defun makefile-browser-format-target-line (target selected
)
1411 (concat (make-string makefile-browser-leftmost-column ?\
)
1413 makefile-browser-selected-mark
1414 makefile-browser-unselected-mark
)
1416 target makefile-target-colon
))
1418 (defun makefile-browser-format-macro-line (macro selected
)
1420 (concat (make-string makefile-browser-leftmost-column ?\
)
1422 makefile-browser-selected-mark
1423 makefile-browser-unselected-mark
)
1424 (makefile-format-macro-ref macro
))))
1426 (defun makefile-browser-fill (targets macros
)
1427 (let ((inhibit-read-only t
))
1428 (goto-char (point-min))
1432 (lambda (item) (insert (makefile-browser-format-target-line (car item
) nil
) "\n")))
1437 (lambda (item) (insert (makefile-browser-format-macro-line (car item
) nil
) "\n")))
1440 (sort-lines nil
(point-min) (point-max))
1441 (goto-char (1- (point-max)))
1442 (delete-char 1) ; remove unnecessary newline at eob
1443 (goto-char (point-min))
1444 (forward-char makefile-browser-cursor-column
)))
1447 ;;; Moving up and down in the browser
1450 (defun makefile-browser-next-line ()
1451 "Move the browser selection cursor to the next line."
1453 (if (not (makefile-last-line-p))
1456 (forward-char makefile-browser-cursor-column
))))
1458 (defun makefile-browser-previous-line ()
1459 "Move the browser selection cursor to the previous line."
1461 (if (not (makefile-first-line-p))
1464 (forward-char makefile-browser-cursor-column
))))
1467 ;;; Quitting the browser (returns to client buffer)
1470 (defun makefile-browser-quit ()
1471 "Leave the browser and return to the makefile buffer."
1473 (let ((my-client makefile-browser-client
))
1474 (setq makefile-browser-client nil
) ; we quitted, so NO client!
1475 (set-buffer-modified-p nil
)
1477 (pop-to-buffer my-client
)))
1480 ;;; Toggle state of a browser item
1483 (defun makefile-browser-toggle ()
1484 "Toggle the selection state of the browser item at the cursor position."
1486 (let ((this-line (count-lines (point-min) (point))))
1487 (setq this-line
(max 1 this-line
))
1488 (makefile-browser-toggle-state-for-line this-line
)
1489 (goto-line this-line
)
1490 (let ((inhibit-read-only t
))
1492 (if (makefile-browser-on-macro-line-p)
1493 (let ((macro-name (makefile-browser-this-line-macro-name)))
1494 (delete-region (point) (progn (end-of-line) (point)))
1496 (makefile-browser-format-macro-line
1498 (makefile-browser-get-state-for-line this-line
))))
1499 (let ((target-name (makefile-browser-this-line-target-name)))
1500 (delete-region (point) (progn (end-of-line) (point)))
1502 (makefile-browser-format-target-line
1504 (makefile-browser-get-state-for-line this-line
))))))
1506 (forward-char makefile-browser-cursor-column
)
1507 (if makefile-browser-auto-advance-after-selection-p
1508 (makefile-browser-next-line))))
1511 ;;; Making insertions into the client buffer
1514 (defun makefile-browser-insert-continuation ()
1515 "Insert a makefile continuation.
1516 In the makefile buffer, go to (end-of-line), insert a \'\\\'
1517 character, insert a new blank line, go to that line and indent by one TAB.
1518 This is most useful in the process of creating continued lines when copying
1519 large dependencies from the browser to the client buffer.
1520 \(point) advances accordingly in the client buffer."
1522 (with-current-buffer makefile-browser-client
1526 (defun makefile-browser-insert-selection ()
1527 "Insert all selected targets and/or macros in the makefile buffer.
1528 Insertion takes place at point."
1532 (let ((current-line 1))
1534 (if (makefile-browser-get-state-for-line current-line
)
1535 (makefile-browser-send-this-line-item))
1537 (setq current-line
(1+ current-line
))))))
1539 (defun makefile-browser-insert-selection-and-quit ()
1541 (makefile-browser-insert-selection)
1542 (makefile-browser-quit))
1544 (defun makefile-browser-send-this-line-item ()
1545 (if (makefile-browser-on-macro-line-p)
1547 (let ((macro-name (makefile-browser-this-line-macro-name)))
1548 (set-buffer makefile-browser-client
)
1549 (insert (makefile-format-macro-ref macro-name
) " ")))
1551 (let ((target-name (makefile-browser-this-line-target-name)))
1552 (set-buffer makefile-browser-client
)
1553 (insert target-name
" ")))))
1555 (defun makefile-browser-start-interaction ()
1556 (use-local-map makefile-browser-map
)
1557 (setq buffer-read-only t
))
1559 (defun makefile-browse (targets macros
)
1561 (if (zerop (+ (length targets
) (length macros
)))
1564 (message "No macros or targets to browse! Consider running 'makefile-pickup-everything\'"))
1565 (let ((browser-buffer (get-buffer-create makefile-browser-buffer-name
)))
1566 (pop-to-buffer browser-buffer
)
1567 (makefile-browser-fill targets macros
)
1568 (shrink-window-if-larger-than-buffer)
1569 (set (make-local-variable 'makefile-browser-selection-vector
)
1570 (make-vector (+ (length targets
) (length macros
)) nil
))
1571 (makefile-browser-start-interaction))))
1573 (defun makefile-switch-to-browser ()
1575 (run-hooks 'makefile-browser-hook
)
1576 (setq makefile-browser-client
(current-buffer))
1577 (makefile-pickup-targets)
1578 (makefile-pickup-macros)
1579 (makefile-browse makefile-target-table makefile-macro-table
))
1583 ;;; ------------------------------------------------------------
1584 ;;; Up-to-date overview buffer
1585 ;;; ------------------------------------------------------------
1587 (defun makefile-create-up-to-date-overview ()
1588 "Create a buffer containing an overview of the state of all known targets.
1589 Known targets are targets that are explicitly defined in that makefile;
1590 in other words, all targets that appear on the left hand side of a
1591 dependency in the makefile."
1593 (if (y-or-n-p "Are you sure that the makefile being edited is consistent? ")
1595 ;; The rest of this function operates on a temporary makefile, created by
1596 ;; writing the current contents of the makefile buffer.
1598 (let ((saved-target-table makefile-target-table
)
1599 (this-buffer (current-buffer))
1600 (makefile-up-to-date-buffer
1601 (get-buffer-create makefile-up-to-date-buffer-name
))
1602 (filename (makefile-save-temporary))
1604 ;; Forget the target table because it may contain picked-up filenames
1605 ;; that are not really targets in the current makefile.
1606 ;; We don't want to query these, so get a new target-table with just the
1607 ;; targets that can be found in the makefile buffer.
1608 ;; The 'old' target table will be restored later.
1610 (real-targets (progn
1611 (makefile-pickup-targets)
1612 makefile-target-table
))
1613 (prereqs makefile-has-prereqs
)
1616 (set-buffer makefile-up-to-date-buffer
)
1617 (setq buffer-read-only nil
)
1619 (makefile-query-targets filename real-targets prereqs
)
1620 (if (zerop (buffer-size)) ; if it did not get us anything
1622 (kill-buffer (current-buffer))
1623 (message "No overview created!")))
1624 (set-buffer this-buffer
)
1625 (setq makefile-target-table saved-target-table
)
1626 (if (get-buffer makefile-up-to-date-buffer-name
)
1628 (pop-to-buffer (get-buffer makefile-up-to-date-buffer-name
))
1629 (shrink-window-if-larger-than-buffer)
1630 (sort-lines nil
(point-min) (point-max))
1631 (setq buffer-read-only t
))))))
1633 (defun makefile-save-temporary ()
1634 "Create a temporary file from the current makefile buffer."
1635 (let ((filename (makefile-generate-temporary-filename)))
1636 (write-region (point-min) (point-max) filename nil
0)
1637 filename
)) ; return the filename
1639 (defun makefile-generate-temporary-filename ()
1640 "Create a filename suitable for use in `makefile-save-temporary'.
1641 Be careful to allow brain-dead file systems (DOS, SYSV ...) to cope
1642 with the generated name!"
1643 (let ((my-name (user-login-name))
1644 (my-uid (int-to-string (user-uid))))
1646 (if (> (length my-name
) 3)
1647 (substring my-name
0 3)
1650 (if (> (length my-uid
) 3)
1651 (substring my-uid
0 3)
1654 (defun makefile-query-targets (filename target-table prereq-list
)
1655 "Fill the up-to-date overview buffer.
1656 Checks each target in TARGET-TABLE using
1657 `makefile-query-one-target-method-function'
1658 and generates the overview, one line per target name."
1661 (function (lambda (item)
1662 (let* ((target-name (car item
))
1663 (no-prereqs (not (member target-name prereq-list
)))
1664 (needs-rebuild (or no-prereqs
1666 makefile-query-one-target-method-function
1671 (cond (no-prereqs " .. has no prerequisites")
1672 (needs-rebuild " .. NEEDS REBUILD")
1673 (t " .. is up to date"))))
1676 (goto-char (point-min))
1677 (delete-file filename
)) ; remove the tmpfile
1679 (defun makefile-query-by-make-minus-q (target &optional filename
)
1681 (call-process makefile-brave-make nil nil nil
1682 "-f" filename
"-q" target
))))
1686 ;;; ------------------------------------------------------------
1687 ;;; Continuation cleanup
1688 ;;; ------------------------------------------------------------
1690 (defun makefile-cleanup-continuations ()
1691 (if (derived-mode-p 'makefile-mode
)
1692 (if (and makefile-cleanup-continuations
1693 (not buffer-read-only
))
1695 (goto-char (point-min))
1696 (while (re-search-forward "\\\\[ \t]+$" nil t
)
1697 (replace-match "\\" t t
))))))
1700 ;;; ------------------------------------------------------------
1701 ;;; Warn of suspicious lines
1702 ;;; ------------------------------------------------------------
1704 (defun makefile-warn-suspicious-lines ()
1705 ;; Returning non-nil cancels the save operation
1706 (if (derived-mode-p 'makefile-mode
)
1708 (goto-char (point-min))
1709 (if (re-search-forward "^\\(\t+$\\| +\t\\)" nil t
)
1711 (format "Suspicious line %d. Save anyway? "
1712 (count-lines (point-min) (point)))))))))
1714 (defun makefile-warn-continuations ()
1715 (if (derived-mode-p 'makefile-mode
)
1717 (goto-char (point-min))
1718 (if (re-search-forward "\\\\[ \t]+$" nil t
)
1720 (format "Suspicious continuation in line %d. Save anyway? "
1721 (count-lines (point-min) (point)))))))))
1724 ;;; ------------------------------------------------------------
1725 ;;; GNU make function support
1726 ;;; ------------------------------------------------------------
1728 (defun makefile-insert-gmake-function ()
1729 "Insert a GNU make function call.
1730 Asks for the name of the function to use (with completion).
1731 Then prompts for all required parameters."
1733 (let* ((gm-function-name (completing-read
1735 makefile-gnumake-functions-alist
1737 (gm-function-prompts
1738 (cdr (assoc gm-function-name makefile-gnumake-functions-alist
))))
1739 (if (not (zerop (length gm-function-name
)))
1740 (insert (makefile-format-macro-ref
1741 (concat gm-function-name
" "
1742 (makefile-prompt-for-gmake-funargs
1743 gm-function-name gm-function-prompts
)))
1746 (defun makefile-prompt-for-gmake-funargs (function-name prompt-list
)
1748 (function (lambda (one-prompt)
1749 (read-string (format "[%s] %s: " function-name one-prompt
)
1756 ;;; ------------------------------------------------------------
1757 ;;; Utility functions
1758 ;;; ------------------------------------------------------------
1760 (defun makefile-match-function-end (end)
1761 "To be called as an anchored matcher by font-lock.
1762 The anchor must have matched the opening parens in the first group."
1763 (let ((s (match-string-no-properties 1)))
1764 ;; FIXME forward-sexp or somesuch would be better?
1765 (if (setq s
(cond ((string= s
"(") ")")
1766 ((string= s
"{") "}")
1767 ((string= s
"((") "))")
1768 ((string= s
"{{") "}}")))
1769 (re-search-forward (concat "\\(.*\\)[ \t]*" s
) (line-end-position) t
))))
1771 (defun makefile-match-dependency (bound)
1772 "Search for `makefile-dependency-regex' up to BOUND.
1773 Checks that the colon has not already been fontified, else we
1774 matched in a rule action."
1777 (while (progn (skip-chars-forward makefile-dependency-skip bound
)
1778 (< (point) (or bound
(point-max))))
1780 (or (eq (char-after) ?
=)
1781 (get-text-property (1- (point)) 'face
)
1782 (if (> (line-beginning-position) (+ (point-min) 2))
1783 (eq (char-before (line-end-position 0)) ?
\\))
1784 (when (save-excursion
1786 (looking-at makefile-dependency-regex
))
1788 (let ((deps-end (match-end 1))
1789 (match-data (match-data)))
1790 (goto-char deps-end
)
1791 (skip-chars-backward " \t")
1792 (setq deps-end
(point))
1794 (skip-chars-forward " \t")
1795 ;; Alter the bounds recorded for subexp 1,
1796 ;; which is what is supposed to match the targets.
1797 (setcar (nthcdr 2 match-data
) (point))
1798 (setcar (nthcdr 3 match-data
) deps-end
)
1799 (store-match-data match-data
)))
1801 (throw 'found
(point)))))
1805 (defun makefile-match-action (bound)
1807 (while (re-search-forward makefile-rule-action-regex bound t
)
1808 (or (eq ?
\\ (char-after (- (match-beginning 0) 2)))
1809 (throw 'found t
)))))
1811 (defun makefile-do-macro-insertion (macro-name)
1812 "Insert a macro reference."
1813 (if (not (zerop (length macro-name
)))
1814 (if (assoc macro-name makefile-runtime-macros-list
)
1815 (insert "$" macro-name
)
1816 (insert (makefile-format-macro-ref macro-name
)))))
1818 (defun makefile-remember-target (target-name &optional has-prereqs
)
1819 "Remember a given target if it is not already remembered for this buffer."
1820 (if (not (zerop (length target-name
)))
1822 (if (not (assoc target-name makefile-target-table
))
1823 (setq makefile-target-table
1824 (cons (list target-name
) makefile-target-table
)))
1826 (setq makefile-has-prereqs
1827 (cons target-name makefile-has-prereqs
))))))
1829 (defun makefile-remember-macro (macro-name)
1830 "Remember a given macro if it is not already remembered for this buffer."
1831 (if (not (zerop (length macro-name
)))
1832 (if (not (assoc macro-name makefile-macro-table
))
1833 (setq makefile-macro-table
1834 (cons (list macro-name
) makefile-macro-table
)))))
1836 (defun makefile-forward-after-target-colon ()
1837 "Move point forward after inserting the terminating colon of a target.
1838 This acts according to the value of `makefile-tab-after-target-colon'."
1839 (if makefile-tab-after-target-colon
1843 (defun makefile-browser-on-macro-line-p ()
1844 "Determine if point is on a macro line in the browser."
1847 (re-search-forward "\\$[{(]" (line-end-position) t
)))
1849 (defun makefile-browser-this-line-target-name ()
1850 "Extract the target name from a line in the browser."
1853 (skip-chars-backward "^ \t")
1854 (buffer-substring (point) (1- (line-end-position)))))
1856 (defun makefile-browser-this-line-macro-name ()
1857 "Extract the macro name from a line in the browser."
1860 (re-search-forward "\\$[{(]" (line-end-position) t
)
1861 (let ((macro-start (point)))
1862 (skip-chars-forward "^})")
1863 (buffer-substring macro-start
(point)))))
1865 (defun makefile-format-macro-ref (macro-name)
1866 "Format a macro reference.
1867 Uses `makefile-use-curly-braces-for-macros-p'."
1868 (if (or (char-equal ?\
( (string-to-char macro-name
))
1869 (char-equal ?\
{ (string-to-char macro-name
)))
1870 (format "$%s" macro-name
)
1871 (if makefile-use-curly-braces-for-macros-p
1872 (format "${%s}" macro-name
)
1873 (format "$(%s)" macro-name
))))
1875 (defun makefile-browser-get-state-for-line (n)
1876 (aref makefile-browser-selection-vector
(1- n
)))
1878 (defun makefile-browser-set-state-for-line (n to-state
)
1879 (aset makefile-browser-selection-vector
(1- n
) to-state
))
1881 (defun makefile-browser-toggle-state-for-line (n)
1882 (makefile-browser-set-state-for-line n
(not (makefile-browser-get-state-for-line n
))))
1884 (defun makefile-last-line-p ()
1885 (= (line-end-position) (point-max)))
1887 (defun makefile-first-line-p ()
1888 (= (line-beginning-position) (point-min)))
1892 ;;; Support for other packages, like add-log.
1894 (defun makefile-add-log-defun ()
1895 "Return name of target or variable assignment that point is in.
1896 If it isn't in one, return nil."
1900 ;; Scan back line by line, noticing when we come to a
1901 ;; variable or rule definition, and giving up when we see
1902 ;; a line that is not part of either of those.
1903 (while (not (or (setq found
1904 (when (or (looking-at makefile-macroassign-regex
)
1905 (looking-at makefile-dependency-regex
))
1906 (match-string-no-properties 1)))
1907 ;; Don't keep looking across a blank line or comment.
1908 (looking-at "$\\|#")
1909 (not (zerop (forward-line -
1))))))
1910 ;; Remove leading and trailing whitespace.
1912 (setq found
(replace-regexp-in-string "[ \t]+\\'" "" found
))
1913 (setq found
(replace-regexp-in-string "\\`[ \t]+" "" found
)))
1916 (provide 'make-mode
)
1918 ;; arch-tag: bd23545a-de91-44fb-b1b2-feafbb2635a0
1919 ;;; make-mode.el ends here