1 ;;; make-mode.el --- makefile editing commands for Emacs
3 ;; Copyright (C) 1992, 1994, 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006
4 ;; 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 2, or (at your option)
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; see the file COPYING. If not, write to the
26 ;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
27 ;; Boston, MA 02110-1301, USA.
31 ;; A major mode for editing makefiles. The mode knows about Makefile
32 ;; syntax and defines M-n and M-p to move to next and previous productions.
34 ;; The keys $, =, : and . are electric; they try to help you fill in a
35 ;; macro reference, macro definition, ordinary target name, or special
36 ;; target name, respectively. Such names are completed using a list of
37 ;; targets and macro names parsed out of the makefile. This list is
38 ;; automatically updated, if necessary, whenever you invoke one of
39 ;; these commands. You can force it to be updated with C-c C-p.
41 ;; The command C-c C-f adds certain filenames in the current directory
42 ;; as targets. You can filter out filenames by setting the variable
43 ;; makefile-ignored-files-in-pickup-regex.
45 ;; The command C-c C-u grinds for a bit, then pops up a report buffer
46 ;; showing which target names are up-to-date with respect to their
47 ;; prerequisites, which targets are out-of-date, and which have no
50 ;; The command C-c C-b pops up a browser window listing all target and
51 ;; macro names. You can mark or unmark items with C-c SPC, and insert
52 ;; all marked items back in the Makefile with C-c TAB.
54 ;; The command C-c TAB in the makefile buffer inserts a GNU make builtin.
55 ;; You will be prompted for the builtin's args.
57 ;; There are numerous other customization variables.
62 ;; * Add missing doc strings, improve terse doc strings.
63 ;; * Eliminate electric stuff entirely.
64 ;; * It might be nice to highlight targets differently depending on
65 ;; whether they are up-to-date or not. Not sure how this would
66 ;; interact with font-lock.
67 ;; * Would be nice to edit the commands in ksh-mode and have
68 ;; indentation and slashification done automatically. Hard.
69 ;; * Consider removing browser mode. It seems useless.
70 ;; * ":" should notice when a new target is made and add it to the
71 ;; list (or at least set makefile-need-target-pickup).
72 ;; * Make browser into a major mode.
73 ;; * Clean up macro insertion stuff. It is a mess.
74 ;; * Browser entry and exit is weird. Normalize.
75 ;; * Browser needs to be rewritten. Right now it is kind of a crock.
77 ;; * Act more like dired/buffer menu/whatever.
78 ;; * Highlight as mouse traverses.
80 ;; * Update documentation above.
81 ;; * Update texinfo manual.
88 ;; Sadly we need this for a macro.
94 ;;; ------------------------------------------------------------
95 ;;; Configurable stuff
96 ;;; ------------------------------------------------------------
98 (defgroup makefile nil
99 "Makefile editing commands for Emacs."
100 :link
'(custom-group-link :tag
"Font Lock Faces group" font-lock-faces
)
104 (defface makefile-space
105 '((((class color
)) (:background
"hotpink"))
106 (t (:reverse-video t
)))
107 "Face to use for highlighting leading spaces in Font-Lock mode."
109 (put 'makefile-space-face
'face-alias
'makefile-space
)
111 (defface makefile-targets
112 ;; This needs to go along both with foreground and background colors (i.e. shell)
113 '((t (:inherit font-lock-function-name-face
)))
114 "Face to use for additionally highlighting rule targets in Font-Lock mode."
118 (defface makefile-shell
120 ;;'((((class color) (min-colors 88) (background light)) (:background "seashell1"))
121 ;; (((class color) (min-colors 88) (background dark)) (:background "seashell4")))
122 "Face to use for additionally highlighting Shell commands in Font-Lock mode."
126 (defface makefile-makepp-perl
127 '((((class color
) (background light
)) (:background
"LightBlue1")) ; Camel Book
128 (((class color
) (background dark
)) (:background
"DarkBlue"))
129 (t (:reverse-video t
)))
130 "Face to use for additionally highlighting Perl code in Font-Lock mode."
134 (defcustom makefile-browser-buffer-name
"*Macros and Targets*"
135 "*Name of the macro- and target browser buffer."
139 (defcustom makefile-target-colon
":"
140 "*String to append to all target names inserted by `makefile-insert-target'.
141 \":\" or \"::\" are common values."
145 (defcustom makefile-macro-assign
" = "
146 "*String to append to all macro names inserted by `makefile-insert-macro'.
147 The normal value should be \" = \", since this is what
148 standard make expects. However, newer makes such as dmake
149 allow a larger variety of different macro assignments, so you
150 might prefer to use \" += \" or \" := \" ."
154 (defcustom makefile-electric-keys nil
155 "*If non-nil, Makefile mode should install electric keybindings.
160 (defcustom makefile-use-curly-braces-for-macros-p nil
161 "*Controls the style of generated macro references.
162 Non-nil means macro references should use curly braces, like `${this}'.
163 nil means use parentheses, like `$(this)'."
167 (defcustom makefile-tab-after-target-colon t
168 "*If non-nil, insert a TAB after a target colon.
169 Otherwise, a space is inserted.
174 (defcustom makefile-browser-leftmost-column
10
175 "*Number of blanks to the left of the browser selection mark."
179 (defcustom makefile-browser-cursor-column
10
180 "*Column the cursor goes to when it moves up or down in the Makefile browser."
184 (defcustom makefile-backslash-column
48
185 "*Column in which `makefile-backslash-region' inserts backslashes."
189 (defcustom makefile-backslash-align t
190 "*If non-nil, `makefile-backslash-region' will align backslashes."
194 (defcustom makefile-browser-selected-mark
"+ "
195 "*String used to mark selected entries in the Makefile browser."
199 (defcustom makefile-browser-unselected-mark
" "
200 "*String used to mark unselected entries in the Makefile browser."
204 (defcustom makefile-browser-auto-advance-after-selection-p t
205 "*If non-nil, cursor will move after item is selected in Makefile browser."
209 (defcustom makefile-pickup-everything-picks-up-filenames-p nil
210 "*If non-nil, `makefile-pickup-everything' picks up filenames as targets.
211 This means it calls `makefile-pickup-filenames-as-targets'.
212 Otherwise filenames are omitted."
216 (defcustom makefile-cleanup-continuations nil
217 "*If non-nil, automatically clean up continuation lines when saving.
218 A line is cleaned up by removing all whitespace following a trailing
219 backslash. This is done silently.
220 IMPORTANT: Please note that enabling this option causes Makefile mode
221 to MODIFY A FILE WITHOUT YOUR CONFIRMATION when \"it seems necessary\"."
225 (defcustom makefile-mode-hook nil
226 "*Normal hook run by `makefile-mode'."
230 (defvar makefile-browser-hook
'())
233 ;; Special targets for DMake, Sun's make ...
235 (defcustom makefile-special-targets-list
236 '(("DEFAULT") ("DONE") ("ERROR") ("EXPORT")
237 ("FAILED") ("GROUPEPILOG") ("GROUPPROLOG") ("IGNORE")
238 ("IMPORT") ("INCLUDE") ("INCLUDEDIRS") ("INIT")
239 ("KEEP_STATE") ("MAKEFILES") ("MAKE_VERSION") ("NO_PARALLEL")
240 ("PARALLEL") ("PHONY") ("PRECIOUS") ("REMOVE")
241 ("SCCS_GET") ("SILENT") ("SOURCE") ("SUFFIXES")
242 ("WAIT") ("c.o") ("C.o") ("m.o")
243 ("el.elc") ("y.c") ("s.o"))
244 "*List of special targets.
245 You will be offered to complete on one of those in the minibuffer whenever
246 you enter a \".\" at the beginning of a line in `makefile-mode'."
247 :type
'(repeat (list string
))
249 (put 'makefile-special-targets-list
'risky-local-variable t
)
251 (defcustom makefile-runtime-macros-list
252 '(("@") ("&") (">") ("<") ("*") ("^") ("+") ("?") ("%") ("$"))
253 "*List of macros that are resolved by make at runtime.
254 If you insert a macro reference using `makefile-insert-macro-ref', the name
255 of the macro is checked against this list. If it can be found its name will
256 not be enclosed in { } or ( )."
257 :type
'(repeat (list string
))
260 ;; Note that the first big subexpression is used by font lock. Note
261 ;; that if you change this regexp you might have to fix the imenu
262 ;; index in makefile-imenu-generic-expression.
263 (defvar makefile-dependency-regex
264 ;; Allow for two nested levels $(v1:$(v2:$(v3:a=b)=c)=d)
265 "^\\(\\(?:\\$\\(?:[({]\\(?:\\$\\(?:[({]\\(?:\\$\\(?:[^({]\\|.[^\n$#})]+?[})]\\)\\|[^\n$#)}]\\)+?[})]\\|[^({]\\)\\|[^\n$#)}]\\)+?[})]\\|[^({]\\)\\|[^\n$#:=]\\)+?\\)\\(:\\)\\(?:[ \t]*$\\|[^=\n]\\(?:[^#\n]*?;[ \t]*\\(.+\\)\\)?\\)"
266 "Regex used to find dependency lines in a makefile.")
268 (defconst makefile-bsdmake-dependency-regex
269 (progn (string-match (regexp-quote "\\(:\\)") makefile-dependency-regex
)
270 (replace-match "\\([:!]\\)" t t makefile-dependency-regex
))
271 "Regex used to find dependency lines in a BSD makefile.")
273 (defvar makefile-dependency-skip
"^:"
274 "Characters to skip to find a line that might be a dependency.")
276 (defvar makefile-rule-action-regex
277 "^\t[ \t]*\\([-@]*\\)[ \t]*\\(\\(?:.*\\\\\n\\)*.*\\)"
278 "Regex used to highlight rule action lines in font lock mode.")
280 (defconst makefile-makepp-rule-action-regex
281 ;; Don't care about initial tab, but I don't know how to font-lock correctly without.
282 "^\t[ \t]*\\(\\(?:\\(?:noecho\\|ignore[-_]error\\|[-@]+\\)[ \t]*\\)*\\)\\(\\(&\\S +\\)?\\(?:.*\\\\\n\\)*.*\\)"
283 "Regex used to highlight makepp rule action lines in font lock mode.")
285 (defconst makefile-bsdmake-rule-action-regex
286 (progn (string-match "-@" makefile-rule-action-regex
)
287 (replace-match "-+@" t t makefile-rule-action-regex
))
288 "Regex used to highlight BSD rule action lines in font lock mode.")
290 ;; Note that the first and second subexpression is used by font lock. Note
291 ;; that if you change this regexp you might have to fix the imenu index in
292 ;; makefile-imenu-generic-expression.
293 (defconst makefile-macroassign-regex
294 ;; We used to match not just the varname but also the whole value
295 ;; (spanning potentially several lines).
296 ;; "^ *\\([^ \n\t][^:#= \t\n]*\\)[ \t]*\\(?:!=[ \t]*\\(\\(?:.+\\\\\n\\)*.+\\)\\|[*:+]?[:?]?=[ \t]*\\(\\(?:.*\\\\\n\\)*.*\\)\\)"
297 ;; What about the define statement? What about differentiating this for makepp?
298 "\\(?:^\\|^export\\|^override\\|:\\|: *override\\) *\\([^ \n\t][^:#= \t\n]*\\)[ \t]*\\(?:!=\\|[*:+]?[:?]?=\\)"
299 "Regex used to find macro assignment lines in a makefile.")
301 (defconst makefile-var-use-regex
302 "[^$]\\$[({]\\([-a-zA-Z0-9_.]+\\|[@%<?^+*][FD]?\\)"
303 "Regex used to find $(macro) uses in a makefile.")
305 (defconst makefile-ignored-files-in-pickup-regex
306 "\\(^\\..*\\)\\|\\(.*~$\\)\\|\\(.*,v$\\)\\|\\(\\.[chy]\\)"
307 "Regex for filenames that will NOT be included in the target list.")
309 (defvar makefile-space
'makefile-space
310 "Face to use for highlighting leading spaces in Font-Lock mode.")
312 ;; These lists were inspired by the old solution. But they are silly, because
313 ;; you can't differentiate what follows. They need to be split up.
314 (defconst makefile-statements
'("include")
315 "List of keywords understood by standard make.")
317 (defconst makefile-automake-statements
318 `("if" "else" "endif" ,@makefile-statements
)
319 "List of keywords understood by automake.")
321 (defconst makefile-gmake-statements
322 `("-sinclude" "sinclude" "vpath" ; makefile-makepp-statements takes rest
323 "ifdef" "ifndef" "ifeq" "ifneq" "-include" "define" "endef" "export"
324 "override define" "override" "unexport"
325 ,@(cdr makefile-automake-statements
))
326 "List of keywords understood by gmake.")
328 ;; These are even more silly, because you can have more spaces in between.
329 (defconst makefile-makepp-statements
330 `("and ifdef" "and ifndef" "and ifeq" "and ifneq" "and ifperl"
331 "and ifmakeperl" "and ifsys" "and ifnsys" "build_cache" "build_check"
332 "else ifdef" "else ifndef" "else ifeq" "else ifneq" "else ifperl"
333 "else ifmakeperl" "else ifsys" "else ifnsys" "enddef" "global"
334 "load_makefile" "ifperl" "ifmakeperl" "ifsys" "ifnsys" "_include"
335 "makeperl" "makesub" "no_implicit_load" "perl" "perl-begin" "perl_begin"
336 "perl-end" "perl_end" "prebuild" "or ifdef" "or ifndef" "or ifeq"
337 "or ifneq" "or ifperl" "or ifmakeperl" "or ifsys" "or ifnsys"
338 "override export" "override global" "register_command_parser"
339 "register_scanner" "repository" "runtime" "signature" "sub"
340 ,@(nthcdr 3 makefile-gmake-statements
))
341 "List of keywords understood by gmake.")
343 (defconst makefile-bsdmake-statements
344 `(".elif" ".elifdef" ".elifmake" ".elifndef" ".elifnmake" ".else" ".endfor"
345 ".endif" ".for" ".if" ".ifdef" ".ifmake" ".ifndef" ".ifnmake" ".undef")
346 "List of keywords understood by BSD make.")
348 (defun makefile-make-font-lock-keywords (var keywords space
350 &rest font-lock-keywords
)
351 `(;; Do macro assignments. These get the "variable-name" face.
352 (,makefile-macroassign-regex
353 (1 font-lock-variable-name-face
)
354 ;; This is for after !=
355 (2 'makefile-shell prepend t
)
356 ;; This is for after normal assignment
357 (3 'font-lock-string-face prepend t
))
360 (makefile-match-action
361 (1 font-lock-type-face
)
362 (2 'makefile-shell prepend
)
363 ;; Only makepp has builtin commands.
364 (3 font-lock-builtin-face prepend t
))
366 ;; Variable references even in targets/strings/comments.
367 (,var
1 font-lock-variable-name-face prepend
)
369 ;; Automatic variable references and single character variable references,
370 ;; but not shell variables references.
371 ("[^$]\\$\\([@%<?^+*_]\\|[a-zA-Z0-9]\\>\\)"
372 1 font-lock-constant-face prepend
)
373 ("[^$]\\(\\$[@%*]\\)"
374 1 'makefile-targets append
)
376 ;; Fontify conditionals and includes.
377 (,(concat "^\\(?: [ \t]*\\)?"
378 (regexp-opt keywords t
)
379 "\\>[ \t]*\\([^: \t\n#]*\\)")
380 (1 font-lock-keyword-face
) (2 font-lock-variable-name-face
))
383 `((,negation
(1 font-lock-negation-char-face prepend
)
384 (2 font-lock-negation-char-face prepend t
))))
387 '(;; Highlight lines that contain just whitespace.
388 ;; They can cause trouble, especially if they start with a tab.
389 ("^[ \t]+$" . makefile-space
)
391 ;; Highlight shell comments that Make treats as commands,
392 ;; since these can fool people.
393 ("^\t+#" 0 makefile-space t
)
395 ;; Highlight spaces that precede tabs.
396 ;; They can make a tab fail to be effective.
397 ("^\\( +\\)\t" 1 makefile-space
)))
402 (makefile-match-dependency
403 (1 'makefile-targets prepend
)
404 (3 'makefile-shell prepend t
))))
406 (defconst makefile-font-lock-keywords
407 (makefile-make-font-lock-keywords
408 makefile-var-use-regex
412 (defconst makefile-automake-font-lock-keywords
413 (makefile-make-font-lock-keywords
414 makefile-var-use-regex
415 makefile-automake-statements
418 (defconst makefile-gmake-font-lock-keywords
419 (makefile-make-font-lock-keywords
420 makefile-var-use-regex
421 makefile-gmake-statements
423 "^\\(?: [ \t]*\\)?if\\(n\\)\\(?:def\\|eq\\)\\>"
425 '("[^$]\\(\\$[({][@%*][DF][})]\\)"
426 1 'makefile-targets append
)
428 ;; $(function ...) ${function ...}
429 '("[^$]\\$[({]\\([-a-zA-Z0-9_.]+\\s \\)"
430 1 font-lock-function-name-face prepend
)
432 ;; $(shell ...) ${shell ...}
433 '("[^$]\\$\\([({]\\)shell[ \t]+"
434 makefile-match-function-end nil nil
435 (1 'makefile-shell prepend t
))))
437 (defconst makefile-makepp-font-lock-keywords
438 (makefile-make-font-lock-keywords
439 makefile-var-use-regex
440 makefile-makepp-statements
442 "^\\(?: [ \t]*\\)?\\(?:and[ \t]+\\|else[ \t]+\\|or[ \t]+\\)?if\\(n\\)\\(?:def\\|eq\\|sys\\)\\>"
444 '("[^$]\\(\\$[({]\\(?:output\\|stem\\|target\\)s?\\_>.*?[})]\\)"
445 1 'makefile-targets append
)
447 ;; Colon modifier keywords.
448 '("\\(:\\s *\\)\\(build_c\\(?:ache\\|heck\\)\\|env\\(?:ironment\\)?\\|foreach\\|signature\\|scanner\\|quickscan\\|smartscan\\)\\>\\([^:\n]*\\)"
449 (1 font-lock-type-face t
)
450 (2 font-lock-keyword-face t
)
451 (3 font-lock-variable-name-face t
))
453 ;; $(function ...) $((function ...)) ${function ...} ${{function ...}}
454 '("[^$]\\$\\(?:((?\\|{{?\\)\\([-a-zA-Z0-9_.]+\\s \\)"
455 1 font-lock-function-name-face prepend
)
457 ;; $(shell ...) $((shell ...)) ${shell ...} ${{shell ...}}
458 '("[^$]\\$\\(((?\\|{{?\\)shell\\(?:[-_]\\(?:global[-_]\\)?once\\)?[ \t]+"
459 makefile-match-function-end nil nil
460 (1 'makefile-shell prepend t
))
462 ;; $(perl ...) $((perl ...)) ${perl ...} ${{perl ...}}
463 '("[^$]\\$\\(((?\\|{{?\\)makeperl[ \t]+"
464 makefile-match-function-end nil nil
465 (1 'makefile-makepp-perl prepend t
))
466 '("[^$]\\$\\(((?\\|{{?\\)perl[ \t]+"
467 makefile-match-function-end nil nil
468 (1 'makefile-makepp-perl t t
))
470 ;; Can we unify these with (if (match-end 1) 'prepend t)?
471 '("ifmakeperl\\s +\\(.*\\)" 1 'makefile-makepp-perl prepend
)
472 '("ifperl\\s +\\(.*\\)" 1 'makefile-makepp-perl t
)
474 ;; Perl block single- or multiline, as statement or rule action.
475 ;; Don't know why the initial newline in 2nd variant of group 2 doesn't get skipped.
476 '("\\<make\\(?:perl\\|sub\\s +\\S +\\)\\s *\n?\\s *{\\(?:{\\s *\n?\\(\\(?:.*\n\\)+?\\)\\s *}\\|\\s *\\(\\(?:.*?\\|\n?\\(?:.*\n\\)+?\\)\\)\\)}"
477 (1 'makefile-makepp-perl prepend t
)
478 (2 'makefile-makepp-perl prepend t
))
479 '("\\<\\(?:perl\\|sub\\s +\\S +\\)\\s *\n?\\s *{\\(?:{\\s *\n?\\(\\(?:.*\n\\)+?\\)\\s *}\\|\\s *\\(\\(?:.*?\\|\n?\\(?:.*\n\\)+?\\)\\)\\)}"
480 (1 'makefile-makepp-perl t t
)
481 (2 'makefile-makepp-perl t t
))
483 ;; Statement style perl block.
484 '("perl[-_]begin\\s *\\(?:\\s #.*\\)?\n\\(\\(?:.*\n\\)+?\\)\\s *perl[-_]end\\>"
485 1 'makefile-makepp-perl t
)))
487 (defconst makefile-bsdmake-font-lock-keywords
488 (makefile-make-font-lock-keywords
489 ;; A lot more could be done for variables here:
490 makefile-var-use-regex
491 makefile-bsdmake-statements
493 "^\\(?: [ \t]*\\)?\\.\\(?:el\\)?if\\(n?\\)\\(?:def\\|make\\)?\\>[ \t]*\\(!?\\)"
494 '("^[ \t]*\\.for[ \t].+[ \t]\\(in\\)\\>" 1 font-lock-keyword-face
)))
496 (defconst makefile-imake-font-lock-keywords
498 (makefile-make-font-lock-keywords
499 makefile-var-use-regex
503 '("^XCOMM.*$" . font-lock-comment-face
)
504 '("XVAR\\(?:use\\|def\\)[0-9]" 0 font-lock-keyword-face prepend
)
505 '("@@" . font-lock-preprocessor-face
)
507 cpp-font-lock-keywords
))
510 (defconst makefile-font-lock-syntactic-keywords
511 ;; From sh-script.el.
512 ;; A `#' begins a comment in sh when it is unquoted and at the beginning
513 ;; of a word. In the shell, words are separated by metacharacters.
514 ;; The list of special chars is taken from the single-unix spec of the
515 ;; shell command language (under `quoting') but with `$' removed.
516 '(("[^|&;<>()`\\\"' \t\n]\\(#+\\)" 1 "_")
517 ;; Change the syntax of a quoted newline so that it does not end a comment.
520 (defvar makefile-imenu-generic-expression
521 `(("Dependencies" makefile-previous-dependency
1)
522 ("Macro Assignment" ,makefile-macroassign-regex
1))
523 "Imenu generic expression for Makefile mode. See `imenu-generic-expression'.")
525 ;;; ------------------------------------------------------------
526 ;;; The following configurable variables are used in the
527 ;;; up-to-date overview .
528 ;;; The standard configuration assumes that your `make' program
529 ;;; can be run in question/query mode using the `-q' option, this
530 ;;; means that the command
534 ;;; should return an exit status of zero if the target `foo' is
535 ;;; up to date and a nonzero exit status otherwise.
536 ;;; Many makes can do this although the docs/manpages do not mention
537 ;;; it. Try it with your favourite one. GNU make, System V make, and
538 ;;; Dennis Vadura's DMake have no problems.
539 ;;; Set the variable `makefile-brave-make' to the name of the
540 ;;; make utility that does this on your system.
541 ;;; To understand what this is all about see the function definition
542 ;;; of `makefile-query-by-make-minus-q' .
543 ;;; ------------------------------------------------------------
545 (defcustom makefile-brave-make
"make"
546 "*How to invoke make, for `makefile-query-targets'.
547 This should identify a `make' command that can handle the `-q' option."
551 (defcustom makefile-query-one-target-method-function
552 'makefile-query-by-make-minus-q
553 "*Function to call to determine whether a make target is up to date.
554 The function must satisfy this calling convention:
556 * As its first argument, it must accept the name of the target to
557 be checked, as a string.
559 * As its second argument, it may accept the name of a makefile
560 as a string. Depending on what you're going to do you may
563 * It must return the integer value 0 (zero) if the given target
564 should be considered up-to-date in the context of the given
565 makefile, any nonzero integer value otherwise."
568 (defvaralias 'makefile-query-one-target-method
569 'makefile-query-one-target-method-function
)
571 (defcustom makefile-up-to-date-buffer-name
"*Makefile Up-to-date overview*"
572 "*Name of the Up-to-date overview buffer."
576 ;;; --- end of up-to-date-overview configuration ------------------
578 (defvar makefile-mode-abbrev-table nil
579 "Abbrev table in use in Makefile buffers.")
580 (if makefile-mode-abbrev-table
582 (define-abbrev-table 'makefile-mode-abbrev-table
()))
584 (defvar makefile-mode-map
585 (let ((map (make-sparse-keymap)))
587 (define-key map
"\C-c:" 'makefile-insert-target-ref
)
588 (if makefile-electric-keys
590 (define-key map
"$" 'makefile-insert-macro-ref
)
591 (define-key map
":" 'makefile-electric-colon
)
592 (define-key map
"=" 'makefile-electric-equal
)
593 (define-key map
"." 'makefile-electric-dot
)))
594 (define-key map
"\C-c\C-f" 'makefile-pickup-filenames-as-targets
)
595 (define-key map
"\C-c\C-b" 'makefile-switch-to-browser
)
596 (define-key map
"\C-c\C-c" 'comment-region
)
597 (define-key map
"\C-c\C-p" 'makefile-pickup-everything
)
598 (define-key map
"\C-c\C-u" 'makefile-create-up-to-date-overview
)
599 (define-key map
"\C-c\C-i" 'makefile-insert-gmake-function
)
600 (define-key map
"\C-c\C-\\" 'makefile-backslash-region
)
601 (define-key map
"\C-c\C-m\C-a" 'makefile-automake-mode
)
602 (define-key map
"\C-c\C-m\C-b" 'makefile-bsdmake-mode
)
603 (define-key map
"\C-c\C-m\C-g" 'makefile-gmake-mode
)
604 (define-key map
"\C-c\C-m\C-i" 'makefile-imake-mode
)
605 (define-key map
"\C-c\C-m\C-m" 'makefile-mode
)
606 (define-key map
"\C-c\C-m\C-p" 'makefile-makepp-mode
)
607 (define-key map
"\M-p" 'makefile-previous-dependency
)
608 (define-key map
"\M-n" 'makefile-next-dependency
)
609 (define-key map
"\e\t" 'makefile-complete
)
612 (define-key map
[menu-bar makefile-mode
]
613 (cons "Makefile" (make-sparse-keymap "Makefile")))
615 (define-key map
[menu-bar makefile-mode browse
]
616 '("Pop up Makefile Browser" . makefile-switch-to-browser
))
617 (define-key map
[menu-bar makefile-mode complete
]
618 '("Complete Target or Macro" . makefile-complete
))
619 (define-key map
[menu-bar makefile-mode pickup
]
620 '("Find Targets and Macros" . makefile-pickup-everything
))
622 (define-key map
[menu-bar makefile-mode prev
]
623 '("Move to Previous Dependency" . makefile-previous-dependency
))
624 (define-key map
[menu-bar makefile-mode next
]
625 '("Move to Next Dependency" . makefile-next-dependency
))
627 "The keymap that is used in Makefile mode.")
630 (defvar makefile-browser-map
631 (let ((map (make-sparse-keymap)))
632 (define-key map
"n" 'makefile-browser-next-line
)
633 (define-key map
"\C-n" 'makefile-browser-next-line
)
634 (define-key map
"p" 'makefile-browser-previous-line
)
635 (define-key map
"\C-p" 'makefile-browser-previous-line
)
636 (define-key map
" " 'makefile-browser-toggle
)
637 (define-key map
"i" 'makefile-browser-insert-selection
)
638 (define-key map
"I" 'makefile-browser-insert-selection-and-quit
)
639 (define-key map
"\C-c\C-m" 'makefile-browser-insert-continuation
)
640 (define-key map
"q" 'makefile-browser-quit
)
641 ;; disable horizontal movement
642 (define-key map
"\C-b" 'undefined
)
643 (define-key map
"\C-f" 'undefined
)
645 "The keymap that is used in the macro- and target browser.")
648 (defvar makefile-mode-syntax-table
649 (let ((st (make-syntax-table)))
650 (modify-syntax-entry ?\
( "() " st
)
651 (modify-syntax-entry ?\
) ")( " st
)
652 (modify-syntax-entry ?\
[ "(] " st
)
653 (modify-syntax-entry ?\
] ")[ " st
)
654 (modify-syntax-entry ?\
{ "(} " st
)
655 (modify-syntax-entry ?\
} "){ " st
)
656 (modify-syntax-entry ?
\' "\" " st
)
657 (modify-syntax-entry ?\
` "\" " st
)
658 (modify-syntax-entry ?
# "< " st
)
659 (modify-syntax-entry ?
\n "> " st
)
662 (defvar makefile-imake-mode-syntax-table
(copy-syntax-table
663 makefile-mode-syntax-table
))
664 (if makefile-imake-mode-syntax-table
666 (modify-syntax-entry ?
/ ". 14" makefile-imake-mode-syntax-table
)
667 (modify-syntax-entry ?
* ". 23" makefile-imake-mode-syntax-table
)
668 (modify-syntax-entry ?
# "'" makefile-imake-mode-syntax-table
)
669 (modify-syntax-entry ?
\n ". b" makefile-imake-mode-syntax-table
))
672 ;;; ------------------------------------------------------------
673 ;;; Internal variables.
674 ;;; You don't need to configure below this line.
675 ;;; ------------------------------------------------------------
677 (defvar makefile-target-table nil
678 "Table of all target names known for this buffer.")
679 (put 'makefile-target-table
'risky-local-variable t
)
681 (defvar makefile-macro-table nil
682 "Table of all macro names known for this buffer.")
683 (put 'makefile-macro-table
'risky-local-variable t
)
685 (defvar makefile-browser-client
686 "A buffer in Makefile mode that is currently using the browser.")
688 (defvar makefile-browser-selection-vector nil
)
689 (defvar makefile-has-prereqs nil
)
690 (defvar makefile-need-target-pickup t
)
691 (defvar makefile-need-macro-pickup t
)
693 (defvar makefile-mode-hook
'())
695 ;; Each element looks like '("GNU MAKE FUNCTION" "ARG" "ARG" ... )
696 ;; Each "ARG" is used as a prompt for a required argument.
697 (defconst makefile-gnumake-functions-alist
700 ("subst" "From" "To" "In")
701 ("patsubst" "Pattern" "Replacement" "In")
703 ("findstring" "Find what" "In")
704 ("filter" "Pattern" "Text")
705 ("filter-out" "Pattern" "Text")
707 ;; Filename functions
712 ("addprefix" "Prefix" "Names")
713 ("addsuffix" "Suffix" "Names")
714 ("join" "List 1" "List 2")
715 ("word" "Index" "Text")
718 ("wildcard" "Pattern")
720 ("foreach" "Variable" "List" "Text")
721 ("origin" "Variable")
722 ("shell" "Command")))
725 ;;; ------------------------------------------------------------
726 ;;; The mode function itself.
727 ;;; ------------------------------------------------------------
730 (defun makefile-mode ()
731 "Major mode for editing standard Makefiles.
733 If you are editing a file for a different make, try one of the
734 variants `makefile-automake-mode', `makefile-gmake-mode',
735 `makefile-makepp-mode', `makefile-bsdmake-mode' or,
736 `makefile-imake-mode'. All but the last should be correctly
737 chosen based on the file name, except if it is *.mk. This
738 function ends by invoking the function(s) `makefile-mode-hook'.
740 It is strongly recommended to use `font-lock-mode', because that
741 provides additional parsing information. This is used for
742 example to see that a rule action `echo foo: bar' is a not rule
743 dependency, despite the colon.
745 \\{makefile-mode-map}
747 In the browser, use the following keys:
749 \\{makefile-browser-map}
751 Makefile mode can be configured by modifying the following variables:
753 `makefile-browser-buffer-name':
754 Name of the macro- and target browser buffer.
756 `makefile-target-colon':
757 The string that gets appended to all target names
758 inserted by `makefile-insert-target'.
759 \":\" or \"::\" are quite common values.
761 `makefile-macro-assign':
762 The string that gets appended to all macro names
763 inserted by `makefile-insert-macro'.
764 The normal value should be \" = \", since this is what
765 standard make expects. However, newer makes such as dmake
766 allow a larger variety of different macro assignments, so you
767 might prefer to use \" += \" or \" := \" .
769 `makefile-tab-after-target-colon':
770 If you want a TAB (instead of a space) to be appended after the
771 target colon, then set this to a non-nil value.
773 `makefile-browser-leftmost-column':
774 Number of blanks to the left of the browser selection mark.
776 `makefile-browser-cursor-column':
777 Column in which the cursor is positioned when it moves
778 up or down in the browser.
780 `makefile-browser-selected-mark':
781 String used to mark selected entries in the browser.
783 `makefile-browser-unselected-mark':
784 String used to mark unselected entries in the browser.
786 `makefile-browser-auto-advance-after-selection-p':
787 If this variable is set to a non-nil value the cursor
788 will automagically advance to the next line after an item
789 has been selected in the browser.
791 `makefile-pickup-everything-picks-up-filenames-p':
792 If this variable is set to a non-nil value then
793 `makefile-pickup-everything' also picks up filenames as targets
794 (i.e. it calls `makefile-pickup-filenames-as-targets'), otherwise
795 filenames are omitted.
797 `makefile-cleanup-continuations':
798 If this variable is set to a non-nil value then Makefile mode
799 will assure that no line in the file ends with a backslash
800 (the continuation character) followed by any whitespace.
801 This is done by silently removing the trailing whitespace, leaving
802 the backslash itself intact.
803 IMPORTANT: Please note that enabling this option causes Makefile mode
804 to MODIFY A FILE WITHOUT YOUR CONFIRMATION when \"it seems necessary\".
806 `makefile-browser-hook':
807 A function or list of functions to be called just before the
808 browser is entered. This is executed in the makefile buffer.
810 `makefile-special-targets-list':
811 List of special targets. You will be offered to complete
812 on one of those in the minibuffer whenever you enter a `.'.
813 at the beginning of a line in Makefile mode."
816 (kill-all-local-variables)
817 (add-hook 'write-file-functions
818 'makefile-warn-suspicious-lines nil t
)
819 (add-hook 'write-file-functions
820 'makefile-warn-continuations nil t
)
821 (add-hook 'write-file-functions
822 'makefile-cleanup-continuations nil t
)
823 (make-local-variable 'makefile-target-table
)
824 (make-local-variable 'makefile-macro-table
)
825 (make-local-variable 'makefile-has-prereqs
)
826 (make-local-variable 'makefile-need-target-pickup
)
827 (make-local-variable 'makefile-need-macro-pickup
)
830 (make-local-variable 'font-lock-defaults
)
831 (setq font-lock-defaults
832 ;; SYNTAX-BEGIN set to backward-paragraph to avoid slow-down
833 ;; near the end of a large buffer, due to parse-partial-sexp's
834 ;; trying to parse all the way till the beginning of buffer.
835 '(makefile-font-lock-keywords
839 (font-lock-syntactic-keywords
840 . makefile-font-lock-syntactic-keywords
)))
843 (make-local-variable 'add-log-current-defun-function
)
844 (setq add-log-current-defun-function
'makefile-add-log-defun
)
847 (make-local-variable 'imenu-generic-expression
)
848 (setq imenu-generic-expression makefile-imenu-generic-expression
)
851 (make-local-variable 'dabbrev-abbrev-skip-leading-regexp
)
852 (setq dabbrev-abbrev-skip-leading-regexp
"\\$")
855 (setq local-abbrev-table makefile-mode-abbrev-table
)
858 (make-local-variable 'fill-paragraph-function
)
859 (setq fill-paragraph-function
'makefile-fill-paragraph
)
862 (make-local-variable 'comment-start
)
863 (setq comment-start
"#")
864 (make-local-variable 'comment-end
)
865 (setq comment-end
"")
866 (make-local-variable 'comment-start-skip
)
867 (setq comment-start-skip
"#+[ \t]*")
869 ;; Make sure TAB really inserts \t.
870 (set (make-local-variable 'indent-line-function
) 'indent-to-left-margin
)
872 ;; become the current major mode
873 (setq major-mode
'makefile-mode
)
874 (setq mode-name
"Makefile")
876 ;; Activate keymap and syntax table.
877 (use-local-map makefile-mode-map
)
878 (set-syntax-table makefile-mode-syntax-table
)
880 ;; Real TABs are important in makefiles
881 (setq indent-tabs-mode t
)
882 (run-mode-hooks 'makefile-mode-hook
))
884 ;; These should do more than just differentiate font-lock.
886 (define-derived-mode makefile-automake-mode makefile-mode
"Makefile.am"
887 "An adapted `makefile-mode' that knows about automake."
888 (setq font-lock-defaults
889 `(makefile-automake-font-lock-keywords ,@(cdr font-lock-defaults
))))
892 (define-derived-mode makefile-gmake-mode makefile-mode
"GNUmakefile"
893 "An adapted `makefile-mode' that knows about gmake."
894 (setq font-lock-defaults
895 `(makefile-gmake-font-lock-keywords ,@(cdr font-lock-defaults
))))
898 (define-derived-mode makefile-makepp-mode makefile-mode
"Makeppfile"
899 "An adapted `makefile-mode' that knows about makepp."
900 (set (make-local-variable 'makefile-rule-action-regex
)
901 makefile-makepp-rule-action-regex
)
902 (setq font-lock-defaults
903 `(makefile-makepp-font-lock-keywords ,@(cdr font-lock-defaults
))
904 imenu-generic-expression
905 `(("Functions" "^[ \t]*\\(?:make\\)?sub[ \t]+\\([A-Za-z0-9_]+\\)" 1)
906 ,@imenu-generic-expression
)))
909 (define-derived-mode makefile-bsdmake-mode makefile-mode
"BSDmakefile"
910 "An adapted `makefile-mode' that knows about BSD make."
911 (set (make-local-variable 'makefile-dependency-regex
)
912 makefile-bsdmake-dependency-regex
)
913 (set (make-local-variable 'makefile-dependency-skip
) "^:!")
914 (set (make-local-variable 'makefile-rule-action-regex
)
915 makefile-bsdmake-rule-action-regex
)
916 (setq font-lock-defaults
917 `(makefile-bsdmake-font-lock-keywords ,@(cdr font-lock-defaults
))))
920 (define-derived-mode makefile-imake-mode makefile-mode
"Imakefile"
921 "An adapted `makefile-mode' that knows about imake."
922 :syntax-table makefile-imake-mode-syntax-table
923 (let ((base `(makefile-imake-font-lock-keywords ,@(cdr font-lock-defaults
)))
925 ;; Remove `font-lock-syntactic-keywords' entry from font-lock-defaults.
927 (unless (and (consp elt
)
928 (eq (car elt
) 'font-lock-syntactic-keywords
))
929 (setq new
(cons elt new
))))
931 (setq font-lock-defaults
(nreverse new
))))
937 (defun makefile-next-dependency ()
938 "Move point to the beginning of the next dependency line."
940 (let ((here (point)))
942 (if (makefile-match-dependency nil
)
943 (progn (beginning-of-line) t
) ; indicate success
944 (goto-char here
) nil
)))
946 (defun makefile-previous-dependency ()
947 "Move point to the beginning of the previous dependency line."
951 ;; makefile-match-dependency done backwards:
953 (while (progn (skip-chars-backward makefile-dependency-skip
)
955 (or (prog1 (eq (char-after) ?
=)
957 (get-text-property (point) 'face
)
959 (if (> (point) (+ (point-min) 2))
960 (eq (char-before (1- (point))) ?
\\))
961 (if (looking-at makefile-dependency-regex
)
968 ;;; Electric keys. Blech.
970 (defun makefile-electric-dot (arg)
971 "Prompt for the name of a special target to insert.
972 Only does electric insertion at beginning of line.
973 Anywhere else just self-inserts."
976 (makefile-insert-special-target)
977 (self-insert-command arg
)))
979 (defun makefile-insert-special-target ()
980 "Prompt for and insert a special target name.
981 Uses `makefile-special-targets' list."
983 (makefile-pickup-targets)
984 (let ((special-target
985 (completing-read "Special target: "
986 makefile-special-targets-list nil nil nil
)))
987 (if (zerop (length special-target
))
989 (insert "." special-target
":")
990 (makefile-forward-after-target-colon))))
992 (defun makefile-electric-equal (arg)
993 "Prompt for name of a macro to insert.
994 Only does prompting if point is at beginning of line.
995 Anywhere else just self-inserts."
997 (makefile-pickup-macros)
999 (call-interactively 'makefile-insert-macro
)
1000 (self-insert-command arg
)))
1002 (defun makefile-insert-macro (macro-name)
1003 "Prepare definition of a new macro."
1004 (interactive "sMacro Name: ")
1005 (makefile-pickup-macros)
1006 (if (not (zerop (length macro-name
)))
1009 (insert macro-name makefile-macro-assign
)
1010 (setq makefile-need-macro-pickup t
)
1011 (makefile-remember-macro macro-name
))))
1013 (defun makefile-insert-macro-ref (macro-name)
1014 "Complete on a list of known macros, then insert complete ref at point."
1018 (makefile-pickup-macros)
1019 (completing-read "Refer to macro: " makefile-macro-table nil nil nil
))))
1020 (makefile-do-macro-insertion macro-name
))
1022 (defun makefile-insert-target (target-name)
1023 "Prepare definition of a new target (dependency line)."
1024 (interactive "sTarget: ")
1025 (if (not (zerop (length target-name
)))
1028 (insert target-name makefile-target-colon
)
1029 (makefile-forward-after-target-colon)
1031 (setq makefile-need-target-pickup t
)
1032 (makefile-remember-target target-name
))))
1034 (defun makefile-insert-target-ref (target-name)
1035 "Complete on a list of known targets, then insert TARGET-NAME at point."
1039 (makefile-pickup-targets)
1040 (completing-read "Refer to target: " makefile-target-table nil nil nil
))))
1041 (if (not (zerop (length target-name
)))
1042 (insert target-name
" ")))
1044 (defun makefile-electric-colon (arg)
1045 "Prompt for name of new target.
1046 Prompting only happens at beginning of line.
1047 Anywhere else just self-inserts."
1050 (call-interactively 'makefile-insert-target
)
1051 (self-insert-command arg
)))
1055 ;;; ------------------------------------------------------------
1056 ;;; Extracting targets and macros from an existing makefile
1057 ;;; ------------------------------------------------------------
1059 (defun makefile-pickup-targets ()
1060 "Notice names of all target definitions in Makefile."
1062 (when makefile-need-target-pickup
1063 (setq makefile-need-target-pickup nil
1064 makefile-target-table nil
1065 makefile-has-prereqs nil
)
1067 (goto-char (point-min))
1068 (while (makefile-match-dependency nil
)
1069 (goto-char (match-beginning 1))
1070 (while (let ((target-name
1071 (buffer-substring-no-properties (point)
1073 (skip-chars-forward "^ \t:#")
1076 (not (looking-at ":[ \t]*$"))))
1077 (if (makefile-remember-target target-name has-prereqs
)
1078 (message "Picked up target \"%s\" from line %d"
1079 target-name
(line-number-at-pos)))
1080 (skip-chars-forward " \t")
1081 (not (or (eolp) (eq (char-after) ?
:)))))
1083 (message "Read targets OK.")))
1085 (defun makefile-pickup-macros ()
1086 "Notice names of all macro definitions in Makefile."
1088 (when makefile-need-macro-pickup
1089 (setq makefile-need-macro-pickup nil
1090 makefile-macro-table nil
)
1092 (goto-char (point-min))
1093 (while (re-search-forward makefile-macroassign-regex nil t
)
1094 (goto-char (match-beginning 1))
1095 (let ((macro-name (buffer-substring-no-properties (point)
1097 (skip-chars-forward "^ \t:#=*")
1099 (if (makefile-remember-macro macro-name
)
1100 (message "Picked up macro \"%s\" from line %d"
1101 macro-name
(line-number-at-pos))))
1103 (message "Read macros OK.")))
1105 (defun makefile-pickup-everything (arg)
1106 "Notice names of all macros and targets in Makefile.
1107 Prefix arg means force pickups to be redone."
1110 (setq makefile-need-target-pickup t
1111 makefile-need-macro-pickup t
))
1112 (makefile-pickup-macros)
1113 (makefile-pickup-targets)
1114 (if makefile-pickup-everything-picks-up-filenames-p
1115 (makefile-pickup-filenames-as-targets)))
1117 (defun makefile-pickup-filenames-as-targets ()
1118 "Scan the current directory for filenames to use as targets.
1119 Checks each filename against `makefile-ignored-files-in-pickup-regex'
1120 and adds all qualifying names to the list of known targets."
1122 (mapc (lambda (name)
1123 (or (file-directory-p name
)
1124 (string-match makefile-ignored-files-in-pickup-regex name
)
1125 (if (makefile-remember-target name
)
1126 (message "Picked up file \"%s\" as target" name
))))
1127 (file-name-all-completions "" (or (file-name-directory (buffer-file-name)) ""))))
1133 (defun makefile-complete ()
1134 "Perform completion on Makefile construct preceding point.
1135 Can complete variable and target names.
1136 The context determines which are considered."
1138 (let* ((beg (save-excursion
1139 (skip-chars-backward "^$(){}:#= \t\n")
1141 (try (buffer-substring beg
(point)))
1147 (let ((pc (preceding-char)))
1149 ;; Beginning of line means anything.
1153 ;; Preceding "$" means macros only.
1157 ;; Preceding "$(" or "${" means macros only.
1164 (= (preceding-char) ?$
))))
1165 (setq do-macros t
)))))
1168 (let* ((table (append (if do-macros
1170 makefile-target-table
)
1171 makefile-macro-table
))
1172 (completion (try-completion try table
)))
1174 ;; Exact match, so insert closing paren or colon.
1176 (insert (if do-macros
1188 (message "Can't find completion for \"%s\"" try
)
1191 ;; Partial completion.
1192 ((not (string= try completion
))
1193 ;; FIXME it would be nice to supply the closing paren if an
1194 ;; exact, unambiguous match were found. That is not possible
1195 ;; right now. Ditto closing ":" for targets.
1196 (delete-region beg
(point))
1198 ;; DO-MACROS means doing macros only. If not that, then check
1199 ;; to see if this completion is a macro. Special insertion
1200 ;; must be done for macros.
1202 (assoc completion makefile-macro-table
))
1203 (let ((makefile-use-curly-braces-for-macros-p
1205 makefile-use-curly-braces-for-macros-p
)))
1206 (delete-backward-char 2)
1207 (makefile-do-macro-insertion completion
)
1208 (delete-backward-char 1))
1210 ;; Just insert targets.
1211 (insert completion
)))
1213 ;; Can't complete any more, so make completion list. FIXME
1214 ;; this doesn't do the right thing when the completion is
1215 ;; actually inserted. I don't think there is an easy way to do
1218 (message "Making completion list...")
1219 (let ((list (all-completions try table
)))
1220 (with-output-to-temp-buffer "*Completions*"
1221 (display-completion-list list try
)))
1222 (message "Making completion list...done"))))))
1226 ;; Backslashification. Stolen from cc-mode.el.
1228 (defun makefile-backslash-region (from to delete-flag
)
1229 "Insert, align, or delete end-of-line backslashes on the lines in the region.
1230 With no argument, inserts backslashes and aligns existing backslashes.
1231 With an argument, deletes the backslashes.
1233 This function does not modify the last line of the region if the region ends
1234 right at the start of the following line; it does not modify blank lines
1235 at the start of the region. So you can put the region around an entire macro
1236 definition and conveniently use this command."
1237 (interactive "r\nP")
1240 (let ((column makefile-backslash-column
)
1241 (endmark (make-marker)))
1242 (move-marker endmark to
)
1243 ;; Compute the smallest column number past the ends of all the lines.
1244 (if makefile-backslash-align
1246 (if (not delete-flag
)
1247 (while (< (point) to
)
1249 (if (= (preceding-char) ?
\\)
1250 (progn (forward-char -
1)
1251 (skip-chars-backward " \t")))
1252 (setq column
(max column
(1+ (current-column))))
1254 ;; Adjust upward to a tab column, if that doesn't push
1256 (if (> (% column tab-width
) 0)
1257 (let ((adjusted (* (/ (+ column tab-width -
1) tab-width
)
1259 (if (< adjusted
(window-width))
1260 (setq column adjusted
))))))
1261 ;; Don't modify blank lines at start of region.
1263 (while (and (< (point) endmark
) (eolp))
1265 ;; Add or remove backslashes on all the lines.
1266 (while (and (< (point) endmark
)
1267 ;; Don't backslashify the last line
1268 ;; if the region ends right at the start of the next line.
1271 (< (point) endmark
)))
1272 (if (not delete-flag
)
1273 (makefile-append-backslash column
)
1274 (makefile-delete-backslash))
1276 (move-marker endmark nil
))))
1278 (defun makefile-append-backslash (column)
1280 ;; Note that "\\\\" is needed to get one backslash.
1281 (if (= (preceding-char) ?
\\)
1282 (progn (forward-char -
1)
1283 (delete-horizontal-space)
1284 (indent-to column
(if makefile-backslash-align nil
1)))
1285 (indent-to column
(if makefile-backslash-align nil
1))
1288 (defun makefile-delete-backslash ()
1293 (if (looking-at "\\\\")
1294 (delete-region (1+ (point))
1295 (progn (skip-chars-backward " \t") (point)))))))
1301 (defun makefile-fill-paragraph (arg)
1302 ;; Fill comments, backslashed lines, and variable definitions
1307 ((looking-at "^#+\\s-*")
1308 ;; Found a comment. Return nil to let normal filling take place.
1311 ;; Must look for backslashed-region before looking for variable
1313 ((or (eq (char-before (line-end-position 1)) ?
\\)
1314 (eq (char-before (line-end-position 0)) ?
\\))
1315 ;; A backslash region. Find beginning and end, remove
1316 ;; backslashes, fill, and then reapply backslahes.
1321 (while (= (preceding-char) ?
\\)
1327 (while (= (preceding-char) ?
\\)
1331 (narrow-to-region beginning end
)
1332 (makefile-backslash-region (point-min) (point-max) t
)
1333 (let ((fill-paragraph-function nil
))
1334 (fill-paragraph nil
))
1335 (makefile-backslash-region (point-min) (point-max) nil
)
1336 (goto-char (point-max))
1337 (if (< (skip-chars-backward "\n") 0)
1338 (delete-region (point) (point-max)))))
1339 ;; Return non-nil to indicate it's been filled.
1342 ((looking-at makefile-macroassign-regex
)
1343 ;; Have a macro assign. Fill just this line, and then backslash
1344 ;; resulting region.
1346 (narrow-to-region (point) (line-beginning-position 2))
1347 (let ((fill-paragraph-function nil
))
1348 (fill-paragraph nil
))
1349 (makefile-backslash-region (point-min) (point-max) nil
))
1350 ;; Return non-nil to indicate it's been filled.
1354 ;; Return non-nil so we don't fill anything else.
1359 ;;; ------------------------------------------------------------
1361 ;;; ------------------------------------------------------------
1363 (defun makefile-browser-format-target-line (target selected
)
1365 (concat (make-string makefile-browser-leftmost-column ?\
)
1367 makefile-browser-selected-mark
1368 makefile-browser-unselected-mark
)
1370 target makefile-target-colon
))
1372 (defun makefile-browser-format-macro-line (macro selected
)
1374 (concat (make-string makefile-browser-leftmost-column ?\
)
1376 makefile-browser-selected-mark
1377 makefile-browser-unselected-mark
)
1378 (makefile-format-macro-ref macro
))))
1380 (defun makefile-browser-fill (targets macros
)
1381 (let ((inhibit-read-only t
))
1382 (goto-char (point-min))
1386 (lambda (item) (insert (makefile-browser-format-target-line (car item
) nil
) "\n")))
1391 (lambda (item) (insert (makefile-browser-format-macro-line (car item
) nil
) "\n")))
1394 (sort-lines nil
(point-min) (point-max))
1395 (goto-char (1- (point-max)))
1396 (delete-char 1) ; remove unnecessary newline at eob
1397 (goto-char (point-min))
1398 (forward-char makefile-browser-cursor-column
)))
1401 ;;; Moving up and down in the browser
1404 (defun makefile-browser-next-line ()
1405 "Move the browser selection cursor to the next line."
1407 (if (not (makefile-last-line-p))
1410 (forward-char makefile-browser-cursor-column
))))
1412 (defun makefile-browser-previous-line ()
1413 "Move the browser selection cursor to the previous line."
1415 (if (not (makefile-first-line-p))
1418 (forward-char makefile-browser-cursor-column
))))
1421 ;;; Quitting the browser (returns to client buffer)
1424 (defun makefile-browser-quit ()
1425 "Leave the browser and return to the makefile buffer."
1427 (let ((my-client makefile-browser-client
))
1428 (setq makefile-browser-client nil
) ; we quitted, so NO client!
1429 (set-buffer-modified-p nil
)
1431 (pop-to-buffer my-client
)))
1434 ;;; Toggle state of a browser item
1437 (defun makefile-browser-toggle ()
1438 "Toggle the selection state of the browser item at the cursor position."
1440 (let ((this-line (count-lines (point-min) (point))))
1441 (setq this-line
(max 1 this-line
))
1442 (makefile-browser-toggle-state-for-line this-line
)
1443 (goto-line this-line
)
1444 (let ((inhibit-read-only t
))
1446 (if (makefile-browser-on-macro-line-p)
1447 (let ((macro-name (makefile-browser-this-line-macro-name)))
1448 (delete-region (point) (progn (end-of-line) (point)))
1450 (makefile-browser-format-macro-line
1452 (makefile-browser-get-state-for-line this-line
))))
1453 (let ((target-name (makefile-browser-this-line-target-name)))
1454 (delete-region (point) (progn (end-of-line) (point)))
1456 (makefile-browser-format-target-line
1458 (makefile-browser-get-state-for-line this-line
))))))
1460 (forward-char makefile-browser-cursor-column
)
1461 (if makefile-browser-auto-advance-after-selection-p
1462 (makefile-browser-next-line))))
1465 ;;; Making insertions into the client buffer
1468 (defun makefile-browser-insert-continuation ()
1469 "Insert a makefile continuation.
1470 In the makefile buffer, go to (end-of-line), insert a \'\\\'
1471 character, insert a new blank line, go to that line and indent by one TAB.
1472 This is most useful in the process of creating continued lines when copying
1473 large dependencies from the browser to the client buffer.
1474 \(point) advances accordingly in the client buffer."
1476 (with-current-buffer makefile-browser-client
1480 (defun makefile-browser-insert-selection ()
1481 "Insert all selected targets and/or macros in the makefile buffer.
1482 Insertion takes place at point."
1486 (let ((current-line 1))
1488 (if (makefile-browser-get-state-for-line current-line
)
1489 (makefile-browser-send-this-line-item))
1491 (setq current-line
(1+ current-line
))))))
1493 (defun makefile-browser-insert-selection-and-quit ()
1495 (makefile-browser-insert-selection)
1496 (makefile-browser-quit))
1498 (defun makefile-browser-send-this-line-item ()
1499 (if (makefile-browser-on-macro-line-p)
1501 (let ((macro-name (makefile-browser-this-line-macro-name)))
1502 (set-buffer makefile-browser-client
)
1503 (insert (makefile-format-macro-ref macro-name
) " ")))
1505 (let ((target-name (makefile-browser-this-line-target-name)))
1506 (set-buffer makefile-browser-client
)
1507 (insert target-name
" ")))))
1509 (defun makefile-browser-start-interaction ()
1510 (use-local-map makefile-browser-map
)
1511 (setq buffer-read-only t
))
1513 (defun makefile-browse (targets macros
)
1515 (if (zerop (+ (length targets
) (length macros
)))
1518 (message "No macros or targets to browse! Consider running 'makefile-pickup-everything\'"))
1519 (let ((browser-buffer (get-buffer-create makefile-browser-buffer-name
)))
1520 (pop-to-buffer browser-buffer
)
1521 (makefile-browser-fill targets macros
)
1522 (shrink-window-if-larger-than-buffer)
1523 (set (make-local-variable 'makefile-browser-selection-vector
)
1524 (make-vector (+ (length targets
) (length macros
)) nil
))
1525 (makefile-browser-start-interaction))))
1527 (defun makefile-switch-to-browser ()
1529 (run-hooks 'makefile-browser-hook
)
1530 (setq makefile-browser-client
(current-buffer))
1531 (makefile-pickup-targets)
1532 (makefile-pickup-macros)
1533 (makefile-browse makefile-target-table makefile-macro-table
))
1537 ;;; ------------------------------------------------------------
1538 ;;; Up-to-date overview buffer
1539 ;;; ------------------------------------------------------------
1541 (defun makefile-create-up-to-date-overview ()
1542 "Create a buffer containing an overview of the state of all known targets.
1543 Known targets are targets that are explicitly defined in that makefile;
1544 in other words, all targets that appear on the left hand side of a
1545 dependency in the makefile."
1547 (if (y-or-n-p "Are you sure that the makefile being edited is consistent? ")
1549 ;; The rest of this function operates on a temporary makefile, created by
1550 ;; writing the current contents of the makefile buffer.
1552 (let ((saved-target-table makefile-target-table
)
1553 (this-buffer (current-buffer))
1554 (makefile-up-to-date-buffer
1555 (get-buffer-create makefile-up-to-date-buffer-name
))
1556 (filename (makefile-save-temporary))
1558 ;; Forget the target table because it may contain picked-up filenames
1559 ;; that are not really targets in the current makefile.
1560 ;; We don't want to query these, so get a new target-table with just the
1561 ;; targets that can be found in the makefile buffer.
1562 ;; The 'old' target table will be restored later.
1564 (real-targets (progn
1565 (makefile-pickup-targets)
1566 makefile-target-table
))
1567 (prereqs makefile-has-prereqs
)
1570 (set-buffer makefile-up-to-date-buffer
)
1571 (setq buffer-read-only nil
)
1573 (makefile-query-targets filename real-targets prereqs
)
1574 (if (zerop (buffer-size)) ; if it did not get us anything
1576 (kill-buffer (current-buffer))
1577 (message "No overview created!")))
1578 (set-buffer this-buffer
)
1579 (setq makefile-target-table saved-target-table
)
1580 (if (get-buffer makefile-up-to-date-buffer-name
)
1582 (pop-to-buffer (get-buffer makefile-up-to-date-buffer-name
))
1583 (shrink-window-if-larger-than-buffer)
1584 (sort-lines nil
(point-min) (point-max))
1585 (setq buffer-read-only t
))))))
1587 (defun makefile-save-temporary ()
1588 "Create a temporary file from the current makefile buffer."
1589 (let ((filename (makefile-generate-temporary-filename)))
1590 (write-region (point-min) (point-max) filename nil
0)
1591 filename
)) ; return the filename
1593 (defun makefile-generate-temporary-filename ()
1594 "Create a filename suitable for use in `makefile-save-temporary'.
1595 Be careful to allow brain-dead file systems (DOS, SYSV ...) to cope
1596 with the generated name!"
1597 (let ((my-name (user-login-name))
1598 (my-uid (int-to-string (user-uid))))
1600 (if (> (length my-name
) 3)
1601 (substring my-name
0 3)
1604 (if (> (length my-uid
) 3)
1605 (substring my-uid
0 3)
1608 (defun makefile-query-targets (filename target-table prereq-list
)
1609 "Fill the up-to-date overview buffer.
1610 Checks each target in TARGET-TABLE using
1611 `makefile-query-one-target-method-function'
1612 and generates the overview, one line per target name."
1615 (function (lambda (item)
1616 (let* ((target-name (car item
))
1617 (no-prereqs (not (member target-name prereq-list
)))
1618 (needs-rebuild (or no-prereqs
1620 makefile-query-one-target-method-function
1625 (cond (no-prereqs " .. has no prerequisites")
1626 (needs-rebuild " .. NEEDS REBUILD")
1627 (t " .. is up to date"))))
1630 (goto-char (point-min))
1631 (delete-file filename
)) ; remove the tmpfile
1633 (defun makefile-query-by-make-minus-q (target &optional filename
)
1635 (call-process makefile-brave-make nil nil nil
1636 "-f" filename
"-q" target
))))
1640 ;;; ------------------------------------------------------------
1641 ;;; Continuation cleanup
1642 ;;; ------------------------------------------------------------
1644 (defun makefile-cleanup-continuations ()
1645 (if (eq major-mode
'makefile-mode
)
1646 (if (and makefile-cleanup-continuations
1647 (not buffer-read-only
))
1649 (goto-char (point-min))
1650 (while (re-search-forward "\\\\[ \t]+$" nil t
)
1651 (replace-match "\\" t t
))))))
1654 ;;; ------------------------------------------------------------
1655 ;;; Warn of suspicious lines
1656 ;;; ------------------------------------------------------------
1658 (defun makefile-warn-suspicious-lines ()
1659 ;; Returning non-nil cancels the save operation
1660 (if (eq major-mode
'makefile-mode
)
1662 (goto-char (point-min))
1663 (if (re-search-forward "^\\(\t+$\\| +\t\\)" nil t
)
1665 (format "Suspicious line %d. Save anyway? "
1666 (count-lines (point-min) (point)))))))))
1668 (defun makefile-warn-continuations ()
1669 (if (eq major-mode
'makefile-mode
)
1671 (goto-char (point-min))
1672 (if (re-search-forward "\\\\[ \t]+$" nil t
)
1674 (format "Suspicious continuation in line %d. Save anyway? "
1675 (count-lines (point-min) (point)))))))))
1678 ;;; ------------------------------------------------------------
1679 ;;; GNU make function support
1680 ;;; ------------------------------------------------------------
1682 (defun makefile-insert-gmake-function ()
1683 "Insert a GNU make function call.
1684 Asks for the name of the function to use (with completion).
1685 Then prompts for all required parameters."
1687 (let* ((gm-function-name (completing-read
1689 makefile-gnumake-functions-alist
1691 (gm-function-prompts
1692 (cdr (assoc gm-function-name makefile-gnumake-functions-alist
))))
1693 (if (not (zerop (length gm-function-name
)))
1694 (insert (makefile-format-macro-ref
1695 (concat gm-function-name
" "
1696 (makefile-prompt-for-gmake-funargs
1697 gm-function-name gm-function-prompts
)))
1700 (defun makefile-prompt-for-gmake-funargs (function-name prompt-list
)
1702 (function (lambda (one-prompt)
1703 (read-string (format "[%s] %s: " function-name one-prompt
)
1710 ;;; ------------------------------------------------------------
1711 ;;; Utility functions
1712 ;;; ------------------------------------------------------------
1714 (defun makefile-match-function-end (end)
1715 "To be called as an anchored matcher by font-lock.
1716 The anchor must have matched the opening parens in the first group."
1717 (let ((s (match-string-no-properties 1)))
1718 (setq s
(cond ((string= s
"(") "\\(.*?\\)[ \t]*)")
1719 ((string= s
"{") "\\(.*?\\)[ \t]*}")
1720 ((string= s
"((") "\\(.*?\\)[ \t]*))")
1721 ((string= s
"{{") "\\(.*?\\)[ \t]*}}")))
1722 (if s
(looking-at s
))))
1724 (defun makefile-match-dependency (bound)
1725 "Search for `makefile-dependency-regex' up to BOUND.
1726 Checks that the colon has not already been fontified, else we
1727 matched in a rule action."
1730 (while (progn (skip-chars-forward makefile-dependency-skip bound
)
1731 (< (point) (or bound
(point-max))))
1733 (or (eq (char-after) ?
=)
1734 (get-text-property (1- (point)) 'face
)
1735 (if (> (line-beginning-position) (+ (point-min) 2))
1736 (eq (char-before (line-end-position 0)) ?
\\))
1737 (when (save-excursion
1739 (looking-at makefile-dependency-regex
))
1741 (let ((deps-end (match-end 1))
1742 (match-data (match-data)))
1743 (goto-char deps-end
)
1744 (skip-chars-backward " \t")
1745 (setq deps-end
(point))
1747 (skip-chars-forward " \t")
1748 ;; Alter the bounds recorded for subexp 1,
1749 ;; which is what is supposed to match the targets.
1750 (setcar (nthcdr 2 match-data
) (point))
1751 (setcar (nthcdr 3 match-data
) deps-end
)
1752 (store-match-data match-data
)))
1754 (throw 'found
(point)))))
1758 (defun makefile-match-action (bound)
1760 (while (re-search-forward makefile-rule-action-regex bound t
)
1761 (or (eq ?
\\ (char-after (- (match-beginning 0) 2)))
1762 (throw 'found t
)))))
1764 (defun makefile-do-macro-insertion (macro-name)
1765 "Insert a macro reference."
1766 (if (not (zerop (length macro-name
)))
1767 (if (assoc macro-name makefile-runtime-macros-list
)
1768 (insert "$" macro-name
)
1769 (insert (makefile-format-macro-ref macro-name
)))))
1771 (defun makefile-remember-target (target-name &optional has-prereqs
)
1772 "Remember a given target if it is not already remembered for this buffer."
1773 (if (not (zerop (length target-name
)))
1775 (if (not (assoc target-name makefile-target-table
))
1776 (setq makefile-target-table
1777 (cons (list target-name
) makefile-target-table
)))
1779 (setq makefile-has-prereqs
1780 (cons target-name makefile-has-prereqs
))))))
1782 (defun makefile-remember-macro (macro-name)
1783 "Remember a given macro if it is not already remembered for this buffer."
1784 (if (not (zerop (length macro-name
)))
1785 (if (not (assoc macro-name makefile-macro-table
))
1786 (setq makefile-macro-table
1787 (cons (list macro-name
) makefile-macro-table
)))))
1789 (defun makefile-forward-after-target-colon ()
1790 "Move point forward after inserting the terminating colon of a target.
1791 This acts according to the value of `makefile-tab-after-target-colon'."
1792 (if makefile-tab-after-target-colon
1796 (defun makefile-browser-on-macro-line-p ()
1797 "Determine if point is on a macro line in the browser."
1800 (re-search-forward "\\$[{(]" (line-end-position) t
)))
1802 (defun makefile-browser-this-line-target-name ()
1803 "Extract the target name from a line in the browser."
1806 (skip-chars-backward "^ \t")
1807 (buffer-substring (point) (1- (line-end-position)))))
1809 (defun makefile-browser-this-line-macro-name ()
1810 "Extract the macro name from a line in the browser."
1813 (re-search-forward "\\$[{(]" (line-end-position) t
)
1814 (let ((macro-start (point)))
1815 (skip-chars-forward "^})")
1816 (buffer-substring macro-start
(point)))))
1818 (defun makefile-format-macro-ref (macro-name)
1819 "Format a macro reference.
1820 Uses `makefile-use-curly-braces-for-macros-p'."
1821 (if (or (char-equal ?\
( (string-to-char macro-name
))
1822 (char-equal ?\
{ (string-to-char macro-name
)))
1823 (format "$%s" macro-name
)
1824 (if makefile-use-curly-braces-for-macros-p
1825 (format "${%s}" macro-name
)
1826 (format "$(%s)" macro-name
))))
1828 (defun makefile-browser-get-state-for-line (n)
1829 (aref makefile-browser-selection-vector
(1- n
)))
1831 (defun makefile-browser-set-state-for-line (n to-state
)
1832 (aset makefile-browser-selection-vector
(1- n
) to-state
))
1834 (defun makefile-browser-toggle-state-for-line (n)
1835 (makefile-browser-set-state-for-line n
(not (makefile-browser-get-state-for-line n
))))
1837 (defun makefile-last-line-p ()
1838 (= (line-end-position) (point-max)))
1840 (defun makefile-first-line-p ()
1841 (= (line-beginning-position) (point-min)))
1845 ;;; Support for other packages, like add-log.
1847 (defun makefile-add-log-defun ()
1848 "Return name of target or variable assignment that point is in.
1849 If it isn't in one, return nil."
1853 ;; Scan back line by line, noticing when we come to a
1854 ;; variable or rule definition, and giving up when we see
1855 ;; a line that is not part of either of those.
1856 (while (not (or (setq found
1857 (when (or (looking-at makefile-macroassign-regex
)
1858 (looking-at makefile-dependency-regex
))
1859 (match-string-no-properties 1)))
1860 ;; Don't keep looking across a blank line or comment.
1861 (looking-at "$\\|#")
1862 (not (zerop (forward-line -
1))))))
1863 ;; Remove leading and trailing whitespace.
1865 (setq found
(replace-regexp-in-string "[ \t]+\\'" "" found
))
1866 (setq found
(replace-regexp-in-string "\\`[ \t]+" "" found
)))
1869 (provide 'make-mode
)
1871 ;; arch-tag: bd23545a-de91-44fb-b1b2-feafbb2635a0
1872 ;;; make-mode.el ends here