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 (replace-regexp-in-string "-@" "-+@" makefile-rule-action-regex
)
285 "Regex used to highlight BSD rule action lines in font lock mode.")
287 ;; Note that the first and second subexpression is used by font lock. Note
288 ;; that if you change this regexp you might have to fix the imenu index in
289 ;; makefile-imenu-generic-expression.
290 (defconst makefile-macroassign-regex
291 ;; We used to match not just the varname but also the whole value
292 ;; (spanning potentially several lines).
293 ;; "^ *\\([^ \n\t][^:#= \t\n]*\\)[ \t]*\\(?:!=[ \t]*\\(\\(?:.+\\\\\n\\)*.+\\)\\|[*:+]?[:?]?=[ \t]*\\(\\(?:.*\\\\\n\\)*.*\\)\\)"
294 ;; What about the define statement? What about differentiating this for makepp?
295 "\\(?:^\\|^export\\|^override\\|:\\|: *override\\) *\\([^ \n\t][^:#= \t\n]*\\)[ \t]*\\(?:!=\\|[*:+]?[:?]?=\\)"
296 "Regex used to find macro assignment lines in a makefile.")
298 (defconst makefile-var-use-regex
299 "[^$]\\$[({]\\([-a-zA-Z0-9_.]+\\|[@%<?^+*][FD]?\\)"
300 "Regex used to find $(macro) uses in a makefile.")
302 (defconst makefile-ignored-files-in-pickup-regex
303 "\\(^\\..*\\)\\|\\(.*~$\\)\\|\\(.*,v$\\)\\|\\(\\.[chy]\\)"
304 "Regex for filenames that will NOT be included in the target list.")
306 (defvar makefile-space
'makefile-space
307 "Face to use for highlighting leading spaces in Font-Lock mode.")
309 ;; These lists were inspired by the old solution. But they are silly, because
310 ;; you can't differentiate what follows. They need to be split up.
311 (defconst makefile-statements
'("include")
312 "List of keywords understood by standard make.")
314 (defconst makefile-automake-statements
315 `("if" "else" "endif" ,@makefile-statements
)
316 "List of keywords understood by automake.")
318 (defconst makefile-gmake-statements
319 `("-sinclude" "sinclude" "vpath" ; makefile-makepp-statements takes rest
320 "ifdef" "ifndef" "ifeq" "ifneq" "-include" "define" "endef" "export"
321 "override define" "override" "unexport"
322 ,@(cdr makefile-automake-statements
))
323 "List of keywords understood by gmake.")
325 ;; These are even more silly, because you can have more spaces in between.
326 (defconst makefile-makepp-statements
327 `("and ifdef" "and ifndef" "and ifeq" "and ifneq" "and ifperl"
328 "and ifmakeperl" "and ifsys" "and ifnsys" "build_cache" "build_check"
329 "else ifdef" "else ifndef" "else ifeq" "else ifneq" "else ifperl"
330 "else ifmakeperl" "else ifsys" "else ifnsys" "enddef" "global"
331 "load_makefile" "ifperl" "ifmakeperl" "ifsys" "ifnsys" "_include"
332 "makeperl" "makesub" "no_implicit_load" "perl" "perl-begin" "perl_begin"
333 "perl-end" "perl_end" "prebuild" "or ifdef" "or ifndef" "or ifeq"
334 "or ifneq" "or ifperl" "or ifmakeperl" "or ifsys" "or ifnsys"
335 "override export" "override global" "register_command_parser"
336 "register_scanner" "repository" "runtime" "signature" "sub"
337 ,@(nthcdr 3 makefile-gmake-statements
))
338 "List of keywords understood by gmake.")
340 (defconst makefile-bsdmake-statements
341 `(".elif" ".elifdef" ".elifmake" ".elifndef" ".elifnmake" ".else" ".endfor"
342 ".endif" ".for" ".if" ".ifdef" ".ifmake" ".ifndef" ".ifnmake" ".undef")
343 "List of keywords understood by BSD make.")
345 (defun makefile-make-font-lock-keywords (var keywords space
347 &rest font-lock-keywords
)
348 `(;; Do macro assignments. These get the "variable-name" face.
349 (,makefile-macroassign-regex
350 (1 font-lock-variable-name-face
)
351 ;; This is for after !=
352 (2 'makefile-shell prepend t
)
353 ;; This is for after normal assignment
354 (3 'font-lock-string-face prepend t
))
357 ;; FIXME: When this spans multiple lines we need font-lock-multiline.
358 (makefile-match-action
359 (1 font-lock-type-face nil t
)
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-syntax-propertize-function
509 (syntax-propertize-rules
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 (define-abbrev-table 'makefile-mode-abbrev-table
()
578 "Abbrev table in use in Makefile buffers.")
580 (defvar makefile-mode-map
581 (let ((map (make-sparse-keymap))
582 (opt-map (make-sparse-keymap)))
584 (define-key map
"\C-c:" 'makefile-insert-target-ref
)
585 (if makefile-electric-keys
587 (define-key map
"$" 'makefile-insert-macro-ref
)
588 (define-key map
":" 'makefile-electric-colon
)
589 (define-key map
"=" 'makefile-electric-equal
)
590 (define-key map
"." 'makefile-electric-dot
)))
591 (define-key map
"\C-c\C-f" 'makefile-pickup-filenames-as-targets
)
592 (define-key map
"\C-c\C-b" 'makefile-switch-to-browser
)
593 (define-key map
"\C-c\C-c" 'comment-region
)
594 (define-key map
"\C-c\C-p" 'makefile-pickup-everything
)
595 (define-key map
"\C-c\C-u" 'makefile-create-up-to-date-overview
)
596 (define-key map
"\C-c\C-i" 'makefile-insert-gmake-function
)
597 (define-key map
"\C-c\C-\\" 'makefile-backslash-region
)
598 (define-key map
"\C-c\C-m\C-a" 'makefile-automake-mode
)
599 (define-key map
"\C-c\C-m\C-b" 'makefile-bsdmake-mode
)
600 (define-key map
"\C-c\C-m\C-g" 'makefile-gmake-mode
)
601 (define-key map
"\C-c\C-m\C-i" 'makefile-imake-mode
)
602 (define-key map
"\C-c\C-m\C-m" 'makefile-mode
)
603 (define-key map
"\C-c\C-m\C-p" 'makefile-makepp-mode
)
604 (define-key map
"\M-p" 'makefile-previous-dependency
)
605 (define-key map
"\M-n" 'makefile-next-dependency
)
606 (define-key map
"\e\t" 'makefile-complete
)
609 (define-key map
[menu-bar makefile-mode
]
610 (cons "Makefile" (make-sparse-keymap "Makefile")))
612 (define-key map
[menu-bar makefile-mode makefile-type
]
613 (cons "Switch Makefile Type" opt-map
))
614 (define-key opt-map
[makefile-makepp-mode
]
615 '(menu-item "Makepp" makefile-makepp-mode
616 :help
"An adapted `makefile-mode' that knows about makepp"
617 :button
(:radio .
(eq major-mode
'makefile-makepp-mode
))))
618 (define-key opt-map
[makefile-imake-mode
]
619 '(menu-item "Imake" makefile-imake-mode
620 :help
"An adapted `makefile-mode' that knows about imake"
621 :button
(:radio .
(eq major-mode
'makefile-imake-mode
))))
622 (define-key opt-map
[makefile-mode
]
623 '(menu-item "Classic" makefile-mode
624 :help
"`makefile-mode' with no special functionality"
625 :button
(:radio .
(eq major-mode
'makefile-mode
))))
626 (define-key opt-map
[makefile-bsdmake-mode
]
627 '(menu-item "BSD" makefile-bsdmake-mode
628 :help
"An adapted `makefile-mode' that knows about BSD make"
629 :button
(:radio .
(eq major-mode
'makefile-bsdmake-mode
))))
630 (define-key opt-map
[makefile-automake-mode
]
631 '(menu-item "Automake" makefile-automake-mode
632 :help
"An adapted `makefile-mode' that knows about automake"
633 :button
(:radio .
(eq major-mode
'makefile-automake-mode
))))
634 (define-key opt-map
[makefile-gmake-mode
]
635 '(menu-item "GNU make" makefile-gmake-mode
636 :help
"An adapted `makefile-mode' that knows about GNU make"
637 :button
(:radio .
(eq major-mode
'makefile-gmake-mode
))))
638 (define-key map
[menu-bar makefile-mode browse
]
639 '(menu-item "Pop up Makefile Browser" makefile-switch-to-browser
640 ;; XXX: this needs a better string, the function is not documented...
641 :help
"Pop up Makefile Browser"))
642 (define-key map
[menu-bar makefile-mode overview
]
643 '(menu-item "Up To Date Overview" makefile-create-up-to-date-overview
644 :help
"Create a buffer containing an overview of the state of all known targets"))
646 (define-key map
[menu-bar makefile-mode separator1
] '("----"))
647 (define-key map
[menu-bar makefile-mode pickup-file
]
648 '(menu-item "Pick File Name as Target" makefile-pickup-filenames-as-targets
649 :help
"Scan the current directory for filenames to use as targets"))
650 (define-key map
[menu-bar makefile-mode function
]
651 '(menu-item "Insert GNU make function" makefile-insert-gmake-function
652 :help
"Insert a GNU make function call"))
653 (define-key map
[menu-bar makefile-mode pickup
]
654 '(menu-item "Find Targets and Macros" makefile-pickup-everything
655 :help
"Notice names of all macros and targets in Makefile"))
656 (define-key map
[menu-bar makefile-mode complete
]
657 '(menu-item "Complete Target or Macro" makefile-complete
658 :help
"Perform completion on Makefile construct preceding point"))
659 (define-key map
[menu-bar makefile-mode backslash
]
660 '(menu-item "Backslash Region" makefile-backslash-region
661 :help
"Insert, align, or delete end-of-line backslashes on the lines in the region"))
663 (define-key map
[menu-bar makefile-mode separator
] '("----"))
664 (define-key map
[menu-bar makefile-mode prev
]
665 '(menu-item "Move to Previous Dependency" makefile-previous-dependency
666 :help
"Move point to the beginning of the previous dependency line"))
667 (define-key map
[menu-bar makefile-mode next
]
668 '(menu-item "Move to Next Dependency" makefile-next-dependency
669 :help
"Move point to the beginning of the next dependency line"))
671 "The keymap that is used in Makefile mode.")
674 (defvar makefile-browser-map
675 (let ((map (make-sparse-keymap)))
676 (define-key map
"n" 'makefile-browser-next-line
)
677 (define-key map
"\C-n" 'makefile-browser-next-line
)
678 (define-key map
"p" 'makefile-browser-previous-line
)
679 (define-key map
"\C-p" 'makefile-browser-previous-line
)
680 (define-key map
" " 'makefile-browser-toggle
)
681 (define-key map
"i" 'makefile-browser-insert-selection
)
682 (define-key map
"I" 'makefile-browser-insert-selection-and-quit
)
683 (define-key map
"\C-c\C-m" 'makefile-browser-insert-continuation
)
684 (define-key map
"q" 'makefile-browser-quit
)
685 ;; disable horizontal movement
686 (define-key map
"\C-b" 'undefined
)
687 (define-key map
"\C-f" 'undefined
)
689 "The keymap that is used in the macro- and target browser.")
692 (defvar makefile-mode-syntax-table
693 (let ((st (make-syntax-table)))
694 (modify-syntax-entry ?\
( "() " st
)
695 (modify-syntax-entry ?\
) ")( " st
)
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 ?
\n "> " st
)
706 (defvar makefile-imake-mode-syntax-table
707 (let ((st (make-syntax-table makefile-mode-syntax-table
)))
708 (modify-syntax-entry ?
/ ". 14" st
)
709 (modify-syntax-entry ?
* ". 23" st
)
710 (modify-syntax-entry ?
# "'" st
)
711 (modify-syntax-entry ?
\n ". b" st
)
714 ;;; ------------------------------------------------------------
715 ;;; Internal variables.
716 ;;; You don't need to configure below this line.
717 ;;; ------------------------------------------------------------
719 (defvar makefile-target-table nil
720 "Table of all target names known for this buffer.")
721 (put 'makefile-target-table
'risky-local-variable t
)
723 (defvar makefile-macro-table nil
724 "Table of all macro names known for this buffer.")
725 (put 'makefile-macro-table
'risky-local-variable t
)
727 (defvar makefile-browser-client
728 "A buffer in Makefile mode that is currently using the browser.")
730 (defvar makefile-browser-selection-vector nil
)
731 (defvar makefile-has-prereqs nil
)
732 (defvar makefile-need-target-pickup t
)
733 (defvar makefile-need-macro-pickup t
)
735 (defvar makefile-mode-hook
'())
737 ;; Each element looks like '("GNU MAKE FUNCTION" "ARG" "ARG" ... )
738 ;; Each "ARG" is used as a prompt for a required argument.
739 (defconst makefile-gnumake-functions-alist
742 ("subst" "From" "To" "In")
743 ("patsubst" "Pattern" "Replacement" "In")
745 ("findstring" "Find what" "In")
746 ("filter" "Pattern" "Text")
747 ("filter-out" "Pattern" "Text")
749 ;; Filename functions
754 ("addprefix" "Prefix" "Names")
755 ("addsuffix" "Suffix" "Names")
756 ("join" "List 1" "List 2")
757 ("word" "Index" "Text")
760 ("wildcard" "Pattern")
762 ("foreach" "Variable" "List" "Text")
763 ("origin" "Variable")
764 ("shell" "Command")))
767 ;;; ------------------------------------------------------------
768 ;;; The mode function itself.
769 ;;; ------------------------------------------------------------
772 (define-derived-mode makefile-mode prog-mode
"Makefile"
773 "Major mode for editing standard Makefiles.
775 If you are editing a file for a different make, try one of the
776 variants `makefile-automake-mode', `makefile-gmake-mode',
777 `makefile-makepp-mode', `makefile-bsdmake-mode' or,
778 `makefile-imake-mode'. All but the last should be correctly
779 chosen based on the file name, except if it is *.mk. This
780 function ends by invoking the function(s) `makefile-mode-hook'.
782 It is strongly recommended to use `font-lock-mode', because that
783 provides additional parsing information. This is used for
784 example to see that a rule action `echo foo: bar' is a not rule
785 dependency, despite the colon.
787 \\{makefile-mode-map}
789 In the browser, use the following keys:
791 \\{makefile-browser-map}
793 Makefile mode can be configured by modifying the following variables:
795 `makefile-browser-buffer-name':
796 Name of the macro- and target browser buffer.
798 `makefile-target-colon':
799 The string that gets appended to all target names
800 inserted by `makefile-insert-target'.
801 \":\" or \"::\" are quite common values.
803 `makefile-macro-assign':
804 The string that gets appended to all macro names
805 inserted by `makefile-insert-macro'.
806 The normal value should be \" = \", since this is what
807 standard make expects. However, newer makes such as dmake
808 allow a larger variety of different macro assignments, so you
809 might prefer to use \" += \" or \" := \" .
811 `makefile-tab-after-target-colon':
812 If you want a TAB (instead of a space) to be appended after the
813 target colon, then set this to a non-nil value.
815 `makefile-browser-leftmost-column':
816 Number of blanks to the left of the browser selection mark.
818 `makefile-browser-cursor-column':
819 Column in which the cursor is positioned when it moves
820 up or down in the browser.
822 `makefile-browser-selected-mark':
823 String used to mark selected entries in the browser.
825 `makefile-browser-unselected-mark':
826 String used to mark unselected entries in the browser.
828 `makefile-browser-auto-advance-after-selection-p':
829 If this variable is set to a non-nil value the cursor
830 will automagically advance to the next line after an item
831 has been selected in the browser.
833 `makefile-pickup-everything-picks-up-filenames-p':
834 If this variable is set to a non-nil value then
835 `makefile-pickup-everything' also picks up filenames as targets
836 (i.e. it calls `makefile-pickup-filenames-as-targets'), otherwise
837 filenames are omitted.
839 `makefile-cleanup-continuations':
840 If this variable is set to a non-nil value then Makefile mode
841 will assure that no line in the file ends with a backslash
842 (the continuation character) followed by any whitespace.
843 This is done by silently removing the trailing whitespace, leaving
844 the backslash itself intact.
845 IMPORTANT: Please note that enabling this option causes Makefile mode
846 to MODIFY A FILE WITHOUT YOUR CONFIRMATION when \"it seems necessary\".
848 `makefile-browser-hook':
849 A function or list of functions to be called just before the
850 browser is entered. This is executed in the makefile buffer.
852 `makefile-special-targets-list':
853 List of special targets. You will be offered to complete
854 on one of those in the minibuffer whenever you enter a `.'.
855 at the beginning of a line in Makefile mode."
856 (add-hook 'write-file-functions
857 'makefile-warn-suspicious-lines nil t
)
858 (add-hook 'write-file-functions
859 'makefile-warn-continuations nil t
)
860 (add-hook 'write-file-functions
861 'makefile-cleanup-continuations nil t
)
862 (make-local-variable 'makefile-target-table
)
863 (make-local-variable 'makefile-macro-table
)
864 (make-local-variable 'makefile-has-prereqs
)
865 (make-local-variable 'makefile-need-target-pickup
)
866 (make-local-variable 'makefile-need-macro-pickup
)
869 (set (make-local-variable 'font-lock-defaults
)
870 ;; SYNTAX-BEGIN set to backward-paragraph to avoid slow-down
871 ;; near the end of a large buffer, due to parse-partial-sexp's
872 ;; trying to parse all the way till the beginning of buffer.
873 '(makefile-font-lock-keywords
877 (set (make-local-variable 'syntax-propertize-function
)
878 makefile-syntax-propertize-function
)
881 (set (make-local-variable 'add-log-current-defun-function
)
882 'makefile-add-log-defun
)
885 (set (make-local-variable 'imenu-generic-expression
)
886 makefile-imenu-generic-expression
)
889 (set (make-local-variable 'dabbrev-abbrev-skip-leading-regexp
) "\\$")
892 (setq local-abbrev-table makefile-mode-abbrev-table
)
895 (set (make-local-variable 'fill-paragraph-function
) 'makefile-fill-paragraph
)
898 (set (make-local-variable 'comment-start
) "#")
899 (set (make-local-variable 'comment-end
) "")
900 (set (make-local-variable 'comment-start-skip
) "#+[ \t]*")
902 ;; Make sure TAB really inserts \t.
903 (set (make-local-variable 'indent-line-function
) 'indent-to-left-margin
)
905 ;; Real TABs are important in makefiles
906 (setq indent-tabs-mode t
))
908 ;; These should do more than just differentiate font-lock.
910 (define-derived-mode makefile-automake-mode makefile-mode
"Makefile.am"
911 "An adapted `makefile-mode' that knows about automake."
912 (setq font-lock-defaults
913 `(makefile-automake-font-lock-keywords ,@(cdr font-lock-defaults
))))
916 (define-derived-mode makefile-gmake-mode makefile-mode
"GNUmakefile"
917 "An adapted `makefile-mode' that knows about gmake."
918 (setq font-lock-defaults
919 `(makefile-gmake-font-lock-keywords ,@(cdr font-lock-defaults
))))
922 (define-derived-mode makefile-makepp-mode makefile-mode
"Makeppfile"
923 "An adapted `makefile-mode' that knows about makepp."
924 (set (make-local-variable 'makefile-rule-action-regex
)
925 makefile-makepp-rule-action-regex
)
926 (setq font-lock-defaults
927 `(makefile-makepp-font-lock-keywords ,@(cdr font-lock-defaults
))
928 imenu-generic-expression
929 `(("Functions" "^[ \t]*\\(?:make\\)?sub[ \t]+\\([A-Za-z0-9_]+\\)" 1)
930 ,@imenu-generic-expression
)))
933 (define-derived-mode makefile-bsdmake-mode makefile-mode
"BSDmakefile"
934 "An adapted `makefile-mode' that knows about BSD make."
935 (set (make-local-variable 'makefile-dependency-regex
)
936 makefile-bsdmake-dependency-regex
)
937 (set (make-local-variable 'makefile-dependency-skip
) "^:!")
938 (set (make-local-variable 'makefile-rule-action-regex
)
939 makefile-bsdmake-rule-action-regex
)
940 (setq font-lock-defaults
941 `(makefile-bsdmake-font-lock-keywords ,@(cdr font-lock-defaults
))))
944 (define-derived-mode makefile-imake-mode makefile-mode
"Imakefile"
945 "An adapted `makefile-mode' that knows about imake."
946 :syntax-table makefile-imake-mode-syntax-table
947 (set (make-local-variable 'syntax-propertize-function
) nil
)
948 (setq font-lock-defaults
949 `(makefile-imake-font-lock-keywords ,@(cdr font-lock-defaults
))))
955 (defun makefile-next-dependency ()
956 "Move point to the beginning of the next dependency line."
958 (let ((here (point)))
960 (if (makefile-match-dependency nil
)
961 (progn (beginning-of-line) t
) ; indicate success
962 (goto-char here
) nil
)))
964 (defun makefile-previous-dependency ()
965 "Move point to the beginning of the previous dependency line."
969 ;; makefile-match-dependency done backwards:
971 (while (progn (skip-chars-backward makefile-dependency-skip
)
973 (or (prog1 (eq (char-after) ?
=)
975 (get-text-property (point) 'face
)
977 (if (> (point) (+ (point-min) 2))
978 (eq (char-before (1- (point))) ?
\\))
979 (if (looking-at makefile-dependency-regex
)
986 ;;; Electric keys. Blech.
988 (defun makefile-electric-dot (arg)
989 "Prompt for the name of a special target to insert.
990 Only does electric insertion at beginning of line.
991 Anywhere else just self-inserts."
994 (makefile-insert-special-target)
995 (self-insert-command arg
)))
997 (defun makefile-insert-special-target ()
998 "Prompt for and insert a special target name.
999 Uses `makefile-special-targets' list."
1001 (makefile-pickup-targets)
1002 (let ((special-target
1003 (completing-read "Special target: "
1004 makefile-special-targets-list nil nil nil
)))
1005 (if (zerop (length special-target
))
1007 (insert "." special-target
":")
1008 (makefile-forward-after-target-colon))))
1010 (defun makefile-electric-equal (arg)
1011 "Prompt for name of a macro to insert.
1012 Only does prompting if point is at beginning of line.
1013 Anywhere else just self-inserts."
1015 (makefile-pickup-macros)
1017 (call-interactively 'makefile-insert-macro
)
1018 (self-insert-command arg
)))
1020 (defun makefile-insert-macro (macro-name)
1021 "Prepare definition of a new macro."
1022 (interactive "sMacro Name: ")
1023 (makefile-pickup-macros)
1024 (if (not (zerop (length macro-name
)))
1027 (insert macro-name makefile-macro-assign
)
1028 (setq makefile-need-macro-pickup t
)
1029 (makefile-remember-macro macro-name
))))
1031 (defun makefile-insert-macro-ref (macro-name)
1032 "Complete on a list of known macros, then insert complete ref at point."
1036 (makefile-pickup-macros)
1037 (completing-read "Refer to macro: " makefile-macro-table nil nil nil
))))
1038 (makefile-do-macro-insertion macro-name
))
1040 (defun makefile-insert-target (target-name)
1041 "Prepare definition of a new target (dependency line)."
1042 (interactive "sTarget: ")
1043 (if (not (zerop (length target-name
)))
1046 (insert target-name makefile-target-colon
)
1047 (makefile-forward-after-target-colon)
1049 (setq makefile-need-target-pickup t
)
1050 (makefile-remember-target target-name
))))
1052 (defun makefile-insert-target-ref (target-name)
1053 "Complete on a list of known targets, then insert TARGET-NAME at point."
1057 (makefile-pickup-targets)
1058 (completing-read "Refer to target: " makefile-target-table nil nil nil
))))
1059 (if (not (zerop (length target-name
)))
1060 (insert target-name
" ")))
1062 (defun makefile-electric-colon (arg)
1063 "Prompt for name of new target.
1064 Prompting only happens at beginning of line.
1065 Anywhere else just self-inserts."
1068 (call-interactively 'makefile-insert-target
)
1069 (self-insert-command arg
)))
1073 ;;; ------------------------------------------------------------
1074 ;;; Extracting targets and macros from an existing makefile
1075 ;;; ------------------------------------------------------------
1077 (defun makefile-pickup-targets ()
1078 "Notice names of all target definitions in Makefile."
1080 (when makefile-need-target-pickup
1081 (setq makefile-need-target-pickup nil
1082 makefile-target-table nil
1083 makefile-has-prereqs nil
)
1085 (goto-char (point-min))
1086 (while (makefile-match-dependency nil
)
1087 (goto-char (match-beginning 1))
1088 (while (let ((target-name
1089 (buffer-substring-no-properties (point)
1091 (skip-chars-forward "^ \t:#")
1094 (not (looking-at ":[ \t]*$"))))
1095 (if (makefile-remember-target target-name has-prereqs
)
1096 (message "Picked up target \"%s\" from line %d"
1097 target-name
(line-number-at-pos)))
1098 (skip-chars-forward " \t")
1099 (not (or (eolp) (eq (char-after) ?
:)))))
1101 (message "Read targets OK.")))
1103 (defun makefile-pickup-macros ()
1104 "Notice names of all macro definitions in Makefile."
1106 (when makefile-need-macro-pickup
1107 (setq makefile-need-macro-pickup nil
1108 makefile-macro-table nil
)
1110 (goto-char (point-min))
1111 (while (re-search-forward makefile-macroassign-regex nil t
)
1112 (goto-char (match-beginning 1))
1113 (let ((macro-name (buffer-substring-no-properties (point)
1115 (skip-chars-forward "^ \t:#=*")
1117 (if (makefile-remember-macro macro-name
)
1118 (message "Picked up macro \"%s\" from line %d"
1119 macro-name
(line-number-at-pos))))
1121 (message "Read macros OK.")))
1123 (defun makefile-pickup-everything (arg)
1124 "Notice names of all macros and targets in Makefile.
1125 Prefix arg means force pickups to be redone."
1128 (setq makefile-need-target-pickup t
1129 makefile-need-macro-pickup t
))
1130 (makefile-pickup-macros)
1131 (makefile-pickup-targets)
1132 (if makefile-pickup-everything-picks-up-filenames-p
1133 (makefile-pickup-filenames-as-targets)))
1135 (defun makefile-pickup-filenames-as-targets ()
1136 "Scan the current directory for filenames to use as targets.
1137 Checks each filename against `makefile-ignored-files-in-pickup-regex'
1138 and adds all qualifying names to the list of known targets."
1140 (mapc (lambda (name)
1141 (or (file-directory-p name
)
1142 (string-match makefile-ignored-files-in-pickup-regex name
)
1143 (if (makefile-remember-target name
)
1144 (message "Picked up file \"%s\" as target" name
))))
1145 (file-name-all-completions "" (or (file-name-directory (buffer-file-name)) ""))))
1151 (defun makefile-complete ()
1152 "Perform completion on Makefile construct preceding point.
1153 Can complete variable and target names.
1154 The context determines which are considered."
1156 (let* ((beg (save-excursion
1157 (skip-chars-backward "^$(){}:#= \t\n")
1159 (try (buffer-substring beg
(point)))
1164 (let ((pc (preceding-char)))
1166 ;; Preceding "$" means macros only.
1170 ;; Preceding "$(" or "${" means macros only.
1171 ((and (memq pc
'(?\
{ ?\
())
1173 (setq paren
(if (eq paren ?\
{) ?\
} ?\
)))
1175 (= (preceding-char) ?$
)))
1178 (table (apply-partially 'completion-table-with-terminator
1180 (do-macros (or paren
""))
1181 ((save-excursion (goto-char beg
) (bolp)) ":")
1183 (append (if do-macros
1185 makefile-target-table
)
1186 makefile-macro-table
))))
1187 (completion-in-region beg
(point) table
)))
1191 ;; Backslashification. Stolen from cc-mode.el.
1193 (defun makefile-backslash-region (from to delete-flag
)
1194 "Insert, align, or delete end-of-line backslashes on the lines in the region.
1195 With no argument, inserts backslashes and aligns existing backslashes.
1196 With an argument, deletes the backslashes.
1198 This function does not modify the last line of the region if the region ends
1199 right at the start of the following line; it does not modify blank lines
1200 at the start of the region. So you can put the region around an entire macro
1201 definition and conveniently use this command."
1202 (interactive "r\nP")
1205 (let ((column makefile-backslash-column
)
1206 (endmark (make-marker)))
1207 (move-marker endmark to
)
1208 ;; Compute the smallest column number past the ends of all the lines.
1209 (if makefile-backslash-align
1211 (if (not delete-flag
)
1212 (while (< (point) to
)
1214 (if (= (preceding-char) ?
\\)
1215 (progn (forward-char -
1)
1216 (skip-chars-backward " \t")))
1217 (setq column
(max column
(1+ (current-column))))
1219 ;; Adjust upward to a tab column, if that doesn't push
1221 (if (> (% column tab-width
) 0)
1222 (let ((adjusted (* (/ (+ column tab-width -
1) tab-width
)
1224 (if (< adjusted
(window-width))
1225 (setq column adjusted
))))))
1226 ;; Don't modify blank lines at start of region.
1228 (while (and (< (point) endmark
) (eolp))
1230 ;; Add or remove backslashes on all the lines.
1231 (while (and (< (point) endmark
)
1232 ;; Don't backslashify the last line
1233 ;; if the region ends right at the start of the next line.
1236 (< (point) endmark
)))
1237 (if (not delete-flag
)
1238 (makefile-append-backslash column
)
1239 (makefile-delete-backslash))
1241 (move-marker endmark nil
))))
1243 (defun makefile-append-backslash (column)
1245 ;; Note that "\\\\" is needed to get one backslash.
1246 (if (= (preceding-char) ?
\\)
1247 (progn (forward-char -
1)
1248 (delete-horizontal-space)
1249 (indent-to column
(if makefile-backslash-align nil
1)))
1250 (indent-to column
(if makefile-backslash-align nil
1))
1253 (defun makefile-delete-backslash ()
1258 (if (looking-at "\\\\")
1259 (delete-region (1+ (point))
1260 (progn (skip-chars-backward " \t") (point)))))))
1266 (defun makefile-fill-paragraph (arg)
1267 ;; Fill comments, backslashed lines, and variable definitions
1272 ((looking-at "^[ \t]*#+\\s-*")
1273 ;; Found a comment. Return nil to let normal filling take place.
1276 ;; Must look for backslashed-region before looking for variable
1278 ((or (eq (char-before (line-end-position 1)) ?
\\)
1279 (eq (char-before (line-end-position 0)) ?
\\))
1280 ;; A backslash region. Find beginning and end, remove
1281 ;; backslashes, fill, and then reapply backslahes.
1286 (while (= (preceding-char) ?
\\)
1292 (while (= (preceding-char) ?
\\)
1296 (narrow-to-region beginning end
)
1297 (makefile-backslash-region (point-min) (point-max) t
)
1298 (let ((fill-paragraph-function nil
)
1299 ;; Adjust fill-column to allow space for the backslash.
1300 (fill-column (- fill-column
1)))
1301 (fill-paragraph nil
))
1302 (makefile-backslash-region (point-min) (point-max) nil
)
1303 (goto-char (point-max))
1304 (if (< (skip-chars-backward "\n") 0)
1305 (delete-region (point) (point-max)))))
1306 ;; Return non-nil to indicate it's been filled.
1309 ((looking-at makefile-macroassign-regex
)
1310 ;; Have a macro assign. Fill just this line, and then backslash
1311 ;; resulting region.
1313 (narrow-to-region (point) (line-beginning-position 2))
1314 (let ((fill-paragraph-function nil
)
1315 ;; Adjust fill-column to allow space for the backslash.
1316 (fill-column (- fill-column
1)))
1317 (fill-paragraph nil
))
1318 (makefile-backslash-region (point-min) (point-max) nil
))
1319 ;; Return non-nil to indicate it's been filled.
1323 ;; Return non-nil so we don't fill anything else.
1328 ;;; ------------------------------------------------------------
1330 ;;; ------------------------------------------------------------
1332 (defun makefile-browser-format-target-line (target selected
)
1334 (concat (make-string makefile-browser-leftmost-column ?\
)
1336 makefile-browser-selected-mark
1337 makefile-browser-unselected-mark
)
1339 target makefile-target-colon
))
1341 (defun makefile-browser-format-macro-line (macro selected
)
1343 (concat (make-string makefile-browser-leftmost-column ?\
)
1345 makefile-browser-selected-mark
1346 makefile-browser-unselected-mark
)
1347 (makefile-format-macro-ref macro
))))
1349 (defun makefile-browser-fill (targets macros
)
1350 (let ((inhibit-read-only t
))
1351 (goto-char (point-min))
1355 (lambda (item) (insert (makefile-browser-format-target-line (car item
) nil
) "\n")))
1360 (lambda (item) (insert (makefile-browser-format-macro-line (car item
) nil
) "\n")))
1363 (sort-lines nil
(point-min) (point-max))
1364 (goto-char (1- (point-max)))
1365 (delete-char 1) ; remove unnecessary newline at eob
1366 (goto-char (point-min))
1367 (forward-char makefile-browser-cursor-column
)))
1370 ;;; Moving up and down in the browser
1373 (defun makefile-browser-next-line ()
1374 "Move the browser selection cursor to the next line."
1376 (if (not (makefile-last-line-p))
1379 (forward-char makefile-browser-cursor-column
))))
1381 (defun makefile-browser-previous-line ()
1382 "Move the browser selection cursor to the previous line."
1384 (if (not (makefile-first-line-p))
1387 (forward-char makefile-browser-cursor-column
))))
1390 ;;; Quitting the browser (returns to client buffer)
1393 (defun makefile-browser-quit ()
1394 "Leave the browser and return to the makefile buffer."
1396 (let ((my-client makefile-browser-client
))
1397 (setq makefile-browser-client nil
) ; we quitted, so NO client!
1398 (set-buffer-modified-p nil
)
1400 (pop-to-buffer my-client
)))
1403 ;;; Toggle state of a browser item
1406 (defun makefile-browser-toggle ()
1407 "Toggle the selection state of the browser item at the cursor position."
1409 (let ((this-line (count-lines (point-min) (point))))
1410 (setq this-line
(max 1 this-line
))
1411 (makefile-browser-toggle-state-for-line this-line
)
1412 (goto-char (point-min))
1413 (forward-line (1- this-line
))
1414 (let ((inhibit-read-only t
))
1415 (beginning-of-line) ; redundant?
1416 (if (makefile-browser-on-macro-line-p)
1417 (let ((macro-name (makefile-browser-this-line-macro-name)))
1418 (delete-region (point) (progn (end-of-line) (point)))
1420 (makefile-browser-format-macro-line
1422 (makefile-browser-get-state-for-line this-line
))))
1423 (let ((target-name (makefile-browser-this-line-target-name)))
1424 (delete-region (point) (progn (end-of-line) (point)))
1426 (makefile-browser-format-target-line
1428 (makefile-browser-get-state-for-line this-line
))))))
1430 (forward-char makefile-browser-cursor-column
)
1431 (if makefile-browser-auto-advance-after-selection-p
1432 (makefile-browser-next-line))))
1435 ;;; Making insertions into the client buffer
1438 (defun makefile-browser-insert-continuation ()
1439 "Insert a makefile continuation.
1440 In the makefile buffer, go to (end-of-line), insert a \'\\\'
1441 character, insert a new blank line, go to that line and indent by one TAB.
1442 This is most useful in the process of creating continued lines when copying
1443 large dependencies from the browser to the client buffer.
1444 \(point) advances accordingly in the client buffer."
1446 (with-current-buffer makefile-browser-client
1450 (defun makefile-browser-insert-selection ()
1451 "Insert all selected targets and/or macros in the makefile buffer.
1452 Insertion takes place at point."
1455 (goto-char (point-min))
1456 (let ((current-line 1))
1458 (if (makefile-browser-get-state-for-line current-line
)
1459 (makefile-browser-send-this-line-item))
1461 (setq current-line
(1+ current-line
))))))
1463 (defun makefile-browser-insert-selection-and-quit ()
1465 (makefile-browser-insert-selection)
1466 (makefile-browser-quit))
1468 (defun makefile-browser-send-this-line-item ()
1469 (if (makefile-browser-on-macro-line-p)
1471 (let ((macro-name (makefile-browser-this-line-macro-name)))
1472 (set-buffer makefile-browser-client
)
1473 (insert (makefile-format-macro-ref macro-name
) " ")))
1475 (let ((target-name (makefile-browser-this-line-target-name)))
1476 (set-buffer makefile-browser-client
)
1477 (insert target-name
" ")))))
1479 (defun makefile-browser-start-interaction ()
1480 (use-local-map makefile-browser-map
)
1481 (setq buffer-read-only t
))
1483 (defun makefile-browse (targets macros
)
1485 (if (zerop (+ (length targets
) (length macros
)))
1488 (message "No macros or targets to browse! Consider running 'makefile-pickup-everything\'"))
1489 (let ((browser-buffer (get-buffer-create makefile-browser-buffer-name
)))
1490 (pop-to-buffer browser-buffer
)
1491 (makefile-browser-fill targets macros
)
1492 (shrink-window-if-larger-than-buffer)
1493 (set (make-local-variable 'makefile-browser-selection-vector
)
1494 (make-vector (+ (length targets
) (length macros
)) nil
))
1495 (makefile-browser-start-interaction))))
1497 (defun makefile-switch-to-browser ()
1499 (run-hooks 'makefile-browser-hook
)
1500 (setq makefile-browser-client
(current-buffer))
1501 (makefile-pickup-targets)
1502 (makefile-pickup-macros)
1503 (makefile-browse makefile-target-table makefile-macro-table
))
1507 ;;; ------------------------------------------------------------
1508 ;;; Up-to-date overview buffer
1509 ;;; ------------------------------------------------------------
1511 (defun makefile-create-up-to-date-overview ()
1512 "Create a buffer containing an overview of the state of all known targets.
1513 Known targets are targets that are explicitly defined in that makefile;
1514 in other words, all targets that appear on the left hand side of a
1515 dependency in the makefile."
1517 (if (y-or-n-p "Are you sure that the makefile being edited is consistent? ")
1519 ;; The rest of this function operates on a temporary makefile, created by
1520 ;; writing the current contents of the makefile buffer.
1522 (let ((saved-target-table makefile-target-table
)
1523 (this-buffer (current-buffer))
1524 (makefile-up-to-date-buffer
1525 (get-buffer-create makefile-up-to-date-buffer-name
))
1526 (filename (makefile-save-temporary))
1528 ;; Forget the target table because it may contain picked-up filenames
1529 ;; that are not really targets in the current makefile.
1530 ;; We don't want to query these, so get a new target-table with just the
1531 ;; targets that can be found in the makefile buffer.
1532 ;; The 'old' target table will be restored later.
1534 (real-targets (progn
1535 (makefile-pickup-targets)
1536 makefile-target-table
))
1537 (prereqs makefile-has-prereqs
)
1540 (set-buffer makefile-up-to-date-buffer
)
1541 (setq buffer-read-only nil
)
1543 (makefile-query-targets filename real-targets prereqs
)
1544 (if (zerop (buffer-size)) ; if it did not get us anything
1546 (kill-buffer (current-buffer))
1547 (message "No overview created!")))
1548 (set-buffer this-buffer
)
1549 (setq makefile-target-table saved-target-table
)
1550 (if (get-buffer makefile-up-to-date-buffer-name
)
1552 (pop-to-buffer (get-buffer makefile-up-to-date-buffer-name
))
1553 (shrink-window-if-larger-than-buffer)
1554 (sort-lines nil
(point-min) (point-max))
1555 (setq buffer-read-only t
))))))
1557 (defun makefile-save-temporary ()
1558 "Create a temporary file from the current makefile buffer."
1559 (let ((filename (makefile-generate-temporary-filename)))
1560 (write-region (point-min) (point-max) filename nil
0)
1561 filename
)) ; return the filename
1563 (defun makefile-generate-temporary-filename ()
1564 "Create a filename suitable for use in `makefile-save-temporary'.
1565 Be careful to allow brain-dead file systems (DOS, SYSV ...) to cope
1566 with the generated name!"
1567 (let ((my-name (user-login-name))
1568 (my-uid (int-to-string (user-uid))))
1570 (if (> (length my-name
) 3)
1571 (substring my-name
0 3)
1574 (if (> (length my-uid
) 3)
1575 (substring my-uid
0 3)
1578 (defun makefile-query-targets (filename target-table prereq-list
)
1579 "Fill the up-to-date overview buffer.
1580 Checks each target in TARGET-TABLE using
1581 `makefile-query-one-target-method-function'
1582 and generates the overview, one line per target name."
1585 (function (lambda (item)
1586 (let* ((target-name (car item
))
1587 (no-prereqs (not (member target-name prereq-list
)))
1588 (needs-rebuild (or no-prereqs
1590 makefile-query-one-target-method-function
1595 (cond (no-prereqs " .. has no prerequisites")
1596 (needs-rebuild " .. NEEDS REBUILD")
1597 (t " .. is up to date"))))
1600 (goto-char (point-min))
1601 (delete-file filename
)) ; remove the tmpfile
1603 (defun makefile-query-by-make-minus-q (target &optional filename
)
1605 (call-process makefile-brave-make nil nil nil
1606 "-f" filename
"-q" target
))))
1610 ;;; ------------------------------------------------------------
1611 ;;; Continuation cleanup
1612 ;;; ------------------------------------------------------------
1614 (defun makefile-cleanup-continuations ()
1615 (if (derived-mode-p 'makefile-mode
)
1616 (if (and makefile-cleanup-continuations
1617 (not buffer-read-only
))
1619 (goto-char (point-min))
1620 (while (re-search-forward "\\\\[ \t]+$" nil t
)
1621 (replace-match "\\" t t
))))))
1624 ;;; ------------------------------------------------------------
1625 ;;; Warn of suspicious lines
1626 ;;; ------------------------------------------------------------
1628 (defun makefile-warn-suspicious-lines ()
1629 ;; Returning non-nil cancels the save operation
1630 (if (derived-mode-p 'makefile-mode
)
1632 (goto-char (point-min))
1633 (if (re-search-forward "^\\(\t+$\\| +\t\\)" nil t
)
1635 (format "Suspicious line %d. Save anyway? "
1636 (count-lines (point-min) (point)))))))))
1638 (defun makefile-warn-continuations ()
1639 (if (derived-mode-p 'makefile-mode
)
1641 (goto-char (point-min))
1642 (if (re-search-forward "\\\\[ \t]+$" nil t
)
1644 (format "Suspicious continuation in line %d. Save anyway? "
1645 (count-lines (point-min) (point)))))))))
1648 ;;; ------------------------------------------------------------
1649 ;;; GNU make function support
1650 ;;; ------------------------------------------------------------
1652 (defun makefile-insert-gmake-function ()
1653 "Insert a GNU make function call.
1654 Asks for the name of the function to use (with completion).
1655 Then prompts for all required parameters."
1657 (let* ((gm-function-name (completing-read
1659 makefile-gnumake-functions-alist
1661 (gm-function-prompts
1662 (cdr (assoc gm-function-name makefile-gnumake-functions-alist
))))
1663 (if (not (zerop (length gm-function-name
)))
1664 (insert (makefile-format-macro-ref
1665 (concat gm-function-name
" "
1666 (makefile-prompt-for-gmake-funargs
1667 gm-function-name gm-function-prompts
)))
1670 (defun makefile-prompt-for-gmake-funargs (function-name prompt-list
)
1672 (function (lambda (one-prompt)
1673 (read-string (format "[%s] %s: " function-name one-prompt
)
1680 ;;; ------------------------------------------------------------
1681 ;;; Utility functions
1682 ;;; ------------------------------------------------------------
1684 (defun makefile-match-function-end (end)
1685 "To be called as an anchored matcher by font-lock.
1686 The anchor must have matched the opening parens in the first group."
1687 (let ((s (match-string-no-properties 1)))
1688 ;; FIXME forward-sexp or somesuch would be better?
1689 (if (setq s
(cond ((string= s
"(") ")")
1690 ((string= s
"{") "}")
1691 ((string= s
"((") "))")
1692 ((string= s
"{{") "}}")))
1693 (re-search-forward (concat "\\(.*\\)[ \t]*" s
) (line-end-position) t
))))
1695 (defun makefile-match-dependency (bound)
1696 "Search for `makefile-dependency-regex' up to BOUND.
1697 Checks that the colon has not already been fontified, else we
1698 matched in a rule action."
1701 (while (progn (skip-chars-forward makefile-dependency-skip bound
)
1702 (< (point) (or bound
(point-max))))
1704 (or (eq (char-after) ?
=)
1705 (get-text-property (1- (point)) 'face
)
1706 (if (> (line-beginning-position) (+ (point-min) 2))
1707 (eq (char-before (line-end-position 0)) ?
\\))
1708 (when (save-excursion
1710 (looking-at makefile-dependency-regex
))
1712 (let ((deps-end (match-end 1))
1713 (match-data (match-data)))
1714 (goto-char deps-end
)
1715 (skip-chars-backward " \t")
1716 (setq deps-end
(point))
1718 (skip-chars-forward " \t")
1719 ;; Alter the bounds recorded for subexp 1,
1720 ;; which is what is supposed to match the targets.
1721 (setcar (nthcdr 2 match-data
) (point))
1722 (setcar (nthcdr 3 match-data
) deps-end
)
1723 (store-match-data match-data
)))
1725 (throw 'found
(point)))))
1729 (defun makefile-match-action (bound)
1731 (while (re-search-forward makefile-rule-action-regex bound t
)
1732 (or (eq ?
\\ (char-after (- (match-beginning 0) 2)))
1733 (throw 'found t
)))))
1735 (defun makefile-do-macro-insertion (macro-name)
1736 "Insert a macro reference."
1737 (if (not (zerop (length macro-name
)))
1738 (if (assoc macro-name makefile-runtime-macros-list
)
1739 (insert "$" macro-name
)
1740 (insert (makefile-format-macro-ref macro-name
)))))
1742 (defun makefile-remember-target (target-name &optional has-prereqs
)
1743 "Remember a given target if it is not already remembered for this buffer."
1744 (if (not (zerop (length target-name
)))
1746 (if (not (assoc target-name makefile-target-table
))
1747 (setq makefile-target-table
1748 (cons (list target-name
) makefile-target-table
)))
1750 (setq makefile-has-prereqs
1751 (cons target-name makefile-has-prereqs
))))))
1753 (defun makefile-remember-macro (macro-name)
1754 "Remember a given macro if it is not already remembered for this buffer."
1755 (if (not (zerop (length macro-name
)))
1756 (if (not (assoc macro-name makefile-macro-table
))
1757 (setq makefile-macro-table
1758 (cons (list macro-name
) makefile-macro-table
)))))
1760 (defun makefile-forward-after-target-colon ()
1761 "Move point forward after inserting the terminating colon of a target.
1762 This acts according to the value of `makefile-tab-after-target-colon'."
1763 (if makefile-tab-after-target-colon
1767 (defun makefile-browser-on-macro-line-p ()
1768 "Determine if point is on a macro line in the browser."
1771 (re-search-forward "\\$[{(]" (line-end-position) t
)))
1773 (defun makefile-browser-this-line-target-name ()
1774 "Extract the target name from a line in the browser."
1777 (skip-chars-backward "^ \t")
1778 (buffer-substring (point) (1- (line-end-position)))))
1780 (defun makefile-browser-this-line-macro-name ()
1781 "Extract the macro name from a line in the browser."
1784 (re-search-forward "\\$[{(]" (line-end-position) t
)
1785 (let ((macro-start (point)))
1786 (skip-chars-forward "^})")
1787 (buffer-substring macro-start
(point)))))
1789 (defun makefile-format-macro-ref (macro-name)
1790 "Format a macro reference.
1791 Uses `makefile-use-curly-braces-for-macros-p'."
1792 (if (or (char-equal ?\
( (string-to-char macro-name
))
1793 (char-equal ?\
{ (string-to-char macro-name
)))
1794 (format "$%s" macro-name
)
1795 (if makefile-use-curly-braces-for-macros-p
1796 (format "${%s}" macro-name
)
1797 (format "$(%s)" macro-name
))))
1799 (defun makefile-browser-get-state-for-line (n)
1800 (aref makefile-browser-selection-vector
(1- n
)))
1802 (defun makefile-browser-set-state-for-line (n to-state
)
1803 (aset makefile-browser-selection-vector
(1- n
) to-state
))
1805 (defun makefile-browser-toggle-state-for-line (n)
1806 (makefile-browser-set-state-for-line n
(not (makefile-browser-get-state-for-line n
))))
1808 (defun makefile-last-line-p ()
1809 (= (line-end-position) (point-max)))
1811 (defun makefile-first-line-p ()
1812 (= (line-beginning-position) (point-min)))
1816 ;;; Support for other packages, like add-log.
1818 (defun makefile-add-log-defun ()
1819 "Return name of target or variable assignment that point is in.
1820 If it isn't in one, return nil."
1824 ;; Scan back line by line, noticing when we come to a
1825 ;; variable or rule definition, and giving up when we see
1826 ;; a line that is not part of either of those.
1827 (while (not (or (setq found
1828 (when (or (looking-at makefile-macroassign-regex
)
1829 (looking-at makefile-dependency-regex
))
1830 (match-string-no-properties 1)))
1831 ;; Don't keep looking across a blank line or comment.
1832 (looking-at "$\\|#")
1833 (not (zerop (forward-line -
1))))))
1834 ;; Remove leading and trailing whitespace.
1836 (setq found
(replace-regexp-in-string "[ \t]+\\'" "" found
))
1837 (setq found
(replace-regexp-in-string "\\`[ \t]+" "" found
)))
1840 (provide 'make-mode
)
1842 ;; arch-tag: bd23545a-de91-44fb-b1b2-feafbb2635a0
1843 ;;; make-mode.el ends here