1 ;;; make-mode.el --- makefile editing commands for Emacs
3 ;; Copyright (C) 1992, 1994, 1999, 2000, 2001, 2002, 2003, 2004, 2005
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."
103 (defface makefile-space
104 '((((class color)) (:background "hotpink"))
105 (t (:reverse-video t)))
106 "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."
119 (defface makefile-shell
121 ;;'((((class color) (min-colors 88) (background light)) (:background "seashell1"))
122 ;; (((class color) (min-colors 88) (background dark)) (:background "seashell4")))
123 "Face to use for additionally highlighting Shell commands in Font-Lock mode."
128 (defface makefile-makepp-perl
129 '((((class color) (background light)) (:background "LightBlue1")) ; Camel Book
130 (((class color) (background dark)) (:background "DarkBlue"))
131 (t (:reverse-video t)))
132 "Face to use for additionally highlighting Perl code in Font-Lock mode."
137 (defcustom makefile-browser-buffer-name "*Macros and Targets*"
138 "*Name of the macro- and target browser buffer."
142 (defcustom makefile-target-colon ":"
143 "*String to append to all target names inserted by `makefile-insert-target'.
144 \":\" or \"::\" are common values."
148 (defcustom makefile-macro-assign " = "
149 "*String to append to all macro names inserted by `makefile-insert-macro'.
150 The normal value should be \" = \", since this is what
151 standard make expects. However, newer makes such as dmake
152 allow a larger variety of different macro assignments, so you
153 might prefer to use \" += \" or \" := \" ."
157 (defcustom makefile-electric-keys nil
158 "*If non-nil, Makefile mode should install electric keybindings.
163 (defcustom makefile-use-curly-braces-for-macros-p nil
164 "*Controls the style of generated macro references.
165 Non-nil means macro references should use curly braces, like `${this}'.
166 nil means use parentheses, like `$(this)'."
170 (defcustom makefile-tab-after-target-colon t
171 "*If non-nil, insert a TAB after a target colon.
172 Otherwise, a space is inserted.
177 (defcustom makefile-browser-leftmost-column 10
178 "*Number of blanks to the left of the browser selection mark."
182 (defcustom makefile-browser-cursor-column 10
183 "*Column the cursor goes to when it moves up or down in the Makefile browser."
187 (defcustom makefile-backslash-column 48
188 "*Column in which `makefile-backslash-region' inserts backslashes."
192 (defcustom makefile-backslash-align t
193 "*If non-nil, `makefile-backslash-region' will align backslashes."
197 (defcustom makefile-browser-selected-mark "+ "
198 "*String used to mark selected entries in the Makefile browser."
202 (defcustom makefile-browser-unselected-mark " "
203 "*String used to mark unselected entries in the Makefile browser."
207 (defcustom makefile-browser-auto-advance-after-selection-p t
208 "*If non-nil, cursor will move after item is selected in Makefile browser."
212 (defcustom makefile-pickup-everything-picks-up-filenames-p nil
213 "*If non-nil, `makefile-pickup-everything' picks up filenames as targets.
214 This means it calls `makefile-pickup-filenames-as-targets'.
215 Otherwise filenames are omitted."
219 (defcustom makefile-cleanup-continuations nil
220 "*If non-nil, automatically clean up continuation lines when saving.
221 A line is cleaned up by removing all whitespace following a trailing
222 backslash. This is done silently.
223 IMPORTANT: Please note that enabling this option causes Makefile mode
224 to MODIFY A FILE WITHOUT YOUR CONFIRMATION when \"it seems necessary\"."
228 (defcustom makefile-mode-hook nil
229 "*Normal hook run by `makefile-mode'."
233 (defvar makefile-browser-hook '())
236 ;; Special targets for DMake, Sun's make ...
238 (defcustom makefile-special-targets-list
239 '(("DEFAULT") ("DONE") ("ERROR") ("EXPORT")
240 ("FAILED") ("GROUPEPILOG") ("GROUPPROLOG") ("IGNORE")
241 ("IMPORT") ("INCLUDE") ("INCLUDEDIRS") ("INIT")
242 ("KEEP_STATE") ("MAKEFILES") ("MAKE_VERSION") ("NO_PARALLEL")
243 ("PARALLEL") ("PHONY") ("PRECIOUS") ("REMOVE")
244 ("SCCS_GET") ("SILENT") ("SOURCE") ("SUFFIXES")
245 ("WAIT") ("c.o") ("C.o") ("m.o")
246 ("el.elc") ("y.c") ("s.o"))
247 "*List of special targets.
248 You will be offered to complete on one of those in the minibuffer whenever
249 you enter a \".\" at the beginning of a line in `makefile-mode'."
250 :type '(repeat (list string))
253 (defcustom makefile-runtime-macros-list
254 '(("@") ("&") (">") ("<") ("*") ("^") ("+") ("?") ("%") ("$"))
255 "*List of macros that are resolved by make at runtime.
256 If you insert a macro reference using `makefile-insert-macro-ref', the name
257 of the macro is checked against this list. If it can be found its name will
258 not be enclosed in { } or ( )."
259 :type '(repeat (list string))
262 ;; Note that the first big subexpression is used by font lock. Note
263 ;; that if you change this regexp you might have to fix the imenu
264 ;; index in makefile-imenu-generic-expression.
265 (defvar makefile-dependency-regex
266 ;; Allow for two nested levels $(v1:$(v2:$(v3:a=b)=c)=d)
267 "^\\(\\(?:\\$\\(?:[({]\\(?:\\$\\(?:[({]\\(?:\\$\\(?:[^({]\\|.[^\n$#})]+?[})]\\)\\|[^\n$#)}]\\)+?[})]\\|[^({]\\)\\|[^\n$#)}]\\)+?[})]\\|[^({]\\)\\|[^\n$#:=]\\)+?\\)\\(:\\)\\(?:[ \t]*$\\|[^=\n]\\(?:[^#\n]*?;[ \t]*\\(.+\\)\\)?\\)"
268 "Regex used to find dependency lines in a makefile.")
270 (defconst makefile-bsdmake-dependency-regex
271 (progn (string-match (regexp-quote "\\(:\\)") makefile-dependency-regex)
272 (replace-match "\\([:!]\\)" t t makefile-dependency-regex))
273 "Regex used to find dependency lines in a BSD makefile.")
275 (defvar makefile-dependency-skip "^:"
276 "Characters to skip to find a line that might be a dependency.")
278 (defvar makefile-rule-action-regex
279 "^\t[ \t]*\\([-@]*\\)[ \t]*\\(\\(?:.*\\\\\n\\)*.*\\)"
280 "Regex used to highlight rule action lines in font lock mode.")
282 (defconst makefile-makepp-rule-action-regex
283 ;; Don't care about initial tab, but I don't know how to font-lock correctly without.
284 "^\t[ \t]*\\(\\(?:\\(?:noecho\\|ignore[-_]error\\|[-@]+\\)[ \t]*\\)*\\)\\(\\(&\\S +\\)?\\(?:.*\\\\\n\\)*.*\\)"
285 "Regex used to highlight makepp rule action lines in font lock mode.")
287 (defconst makefile-bsdmake-rule-action-regex
288 (progn (string-match "-@" makefile-rule-action-regex)
289 (replace-match "-+@" t t makefile-rule-action-regex))
290 "Regex used to highlight BSD rule action lines in font lock mode.")
292 ;; Note that the first and second subexpression is used by font lock. Note
293 ;; that if you change this regexp you might have to fix the imenu index in
294 ;; makefile-imenu-generic-expression.
295 (defconst makefile-macroassign-regex
296 "^ *\\([^ \n\t][^:#= \t\n]*\\)[ \t]*\\(?:!=\\|[*:+]?[:?]?=\\)"
297 "Regex used to find macro assignment lines in a makefile.")
299 (defconst makefile-var-use-regex
300 "[^$]\\$[({]\\([-a-zA-Z0-9_.]+\\|[@%<?^+*][FD]?\\)"
301 "Regex used to find $(macro) uses in a makefile.")
303 (defconst makefile-ignored-files-in-pickup-regex
304 "\\(^\\..*\\)\\|\\(.*~$\\)\\|\\(.*,v$\\)\\|\\(\\.[chy]\\)"
305 "Regex for filenames that will NOT be included in the target list.")
307 (if (fboundp 'facemenu-unlisted-faces)
308 (add-to-list 'facemenu-unlisted-faces 'makefile-space))
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" "override" "vpath"
323 "ifdef" "ifndef" "ifeq" "ifneq" "-include" "define" "endef" "export"
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" "load_makefile"
334 "ifperl" "ifmakeperl" "ifsys" "ifnsys" "_include" "makeperl" "makesub"
335 "no_implicit_load" "perl" "perl-begin" "perl_begin" "perl-end" "perl_end"
336 "prebuild" "or ifdef" "or ifndef" "or ifeq" "or ifneq" "or ifperl"
337 "or ifmakeperl" "or ifsys" "or ifnsys" "register_command_parser"
338 "register_scanner" "repository" "runtime" "signature" "sub"
339 ,@(nthcdr 4 makefile-gmake-statements))
340 "List of keywords understood by gmake.")
342 (defconst makefile-bsdmake-statements
343 `(".elif" ".elifdef" ".elifmake" ".elifndef" ".elifnmake" ".else" ".endfor"
344 ".endif" ".for" ".if" ".ifdef" ".ifmake" ".ifndef" ".ifnmake" ".undef")
345 "List of keywords understood by BSD make.")
347 (defun makefile-make-font-lock-keywords (var keywords space
349 &rest font-lock-keywords)
350 `(;; Do macro assignments. These get the "variable-name" face.
351 (,makefile-macroassign-regex
352 (1 font-lock-variable-name-face)
353 ;; This is for after !=
354 (2 'makefile-shell prepend t)
355 ;; This is for after normal assignment
356 (3 'font-lock-string-face prepend t))
359 (makefile-match-action
360 (1 font-lock-type-face)
361 (2 'makefile-shell prepend)
362 ;; Only makepp has builtin commands.
363 (3 font-lock-builtin-face prepend t))
365 ;; Variable references even in targets/strings/comments.
366 (,var 1 font-lock-variable-name-face prepend)
368 ;; Automatic variable references and single character variable references,
369 ;; but not shell variables references.
370 ("[^$]\\$\\([@%<?^+*_]\\|[a-zA-Z0-9]\\>\\)"
371 1 font-lock-constant-face prepend)
372 ("[^$]\\(\\$[@%*]\\)"
373 1 'makefile-targets append)
375 ;; Fontify conditionals and includes.
376 (,(concat "^\\(?: [ \t]*\\)?"
377 (regexp-opt keywords t)
378 "\\>[ \t]*\\([^: \t\n#]*\\)")
379 (1 font-lock-keyword-face) (2 font-lock-variable-name-face))
382 `((,negation (1 font-lock-negation-char-face prepend)
383 (2 font-lock-negation-char-face prepend t))))
386 '(;; Highlight lines that contain just whitespace.
387 ;; They can cause trouble, especially if they start with a tab.
388 ("^[ \t]+$" . makefile-space)
390 ;; Highlight shell comments that Make treats as commands,
391 ;; since these can fool people.
392 ("^\t+#" 0 makefile-space t)
394 ;; Highlight spaces that precede tabs.
395 ;; They can make a tab fail to be effective.
396 ("^\\( +\\)\t" 1 makefile-space)))
401 (makefile-match-dependency
402 (1 'makefile-targets prepend)
403 (3 'makefile-shell prepend t))))
405 (defconst makefile-font-lock-keywords
406 (makefile-make-font-lock-keywords
407 makefile-var-use-regex
411 (defconst makefile-automake-font-lock-keywords
412 (makefile-make-font-lock-keywords
413 makefile-var-use-regex
414 makefile-automake-statements
417 (defconst makefile-gmake-font-lock-keywords
418 (makefile-make-font-lock-keywords
419 makefile-var-use-regex
420 makefile-gmake-statements
422 "^\\(?: [ \t]*\\)?if\\(n\\)\\(?:def\\|eq\\)\\>"
424 '("[^$]\\(\\$[({][@%*][DF][})]\\)"
425 1 'makefile-targets append)
427 ;; $(function ...) ${function ...}
428 '("[^$]\\$[({]\\([-a-zA-Z0-9_.]+\\s \\)"
429 1 font-lock-function-name-face prepend)
431 ;; $(shell ...) ${shell ...}
432 '("[^$]\\$\\([({]\\)shell[ \t]+"
433 makefile-match-function-end nil nil
434 (1 'makefile-shell prepend t))))
436 (defconst makefile-makepp-font-lock-keywords
437 (makefile-make-font-lock-keywords
438 makefile-var-use-regex
439 makefile-makepp-statements
441 "^\\(?: [ \t]*\\)?\\(?:and[ \t]+\\|else[ \t]+\\|or[ \t]+\\)?if\\(n\\)\\(?:def\\|eq\\|sys\\)\\>"
443 '("[^$]\\(\\$[({]\\(?:output\\|stem\\|target\\)s?\\_>.*?[})]\\)"
444 1 'makefile-targets append)
446 ;; Colon modifier keywords.
447 '("\\(:\\s *\\)\\(build_c\\(?:ache\\|heck\\)\\|env\\(?:ironment\\)?\\|foreach\\|signature\\|scanner\\|quickscan\\|smartscan\\)\\>\\([^:\n]*\\)"
448 (1 font-lock-type-face t)
449 (2 font-lock-keyword-face t)
450 (3 font-lock-variable-name-face t))
452 ;; $(function ...) $((function ...)) ${function ...} ${{function ...}}
453 '("[^$]\\$\\(?:((?\\|{{?\\)\\([-a-zA-Z0-9_.]+\\s \\)"
454 1 font-lock-function-name-face prepend)
456 ;; $(shell ...) $((shell ...)) ${shell ...} ${{shell ...}}
457 '("[^$]\\$\\(((?\\|{{?\\)shell\\(?:[-_]\\(?:global[-_]\\)?once\\)?[ \t]+"
458 makefile-match-function-end nil nil
459 (1 'makefile-shell prepend t))
461 ;; $(perl ...) $((perl ...)) ${perl ...} ${{perl ...}}
462 '("[^$]\\$\\(((?\\|{{?\\)makeperl[ \t]+"
463 makefile-match-function-end nil nil
464 (1 'makefile-makepp-perl prepend t))
465 '("[^$]\\$\\(((?\\|{{?\\)perl[ \t]+"
466 makefile-match-function-end nil nil
467 (1 'makefile-makepp-perl t t))
469 ;; Can we unify these with (if (match-end 1) 'prepend t)?
470 '("ifmakeperl\\s +\\(.*\\)" 1 'makefile-makepp-perl prepend)
471 '("ifperl\\s +\\(.*\\)" 1 'makefile-makepp-perl t)
473 ;; Perl block single- or multiline, as statement or rule action.
474 ;; Don't know why the initial newline in 2nd variant of group 2 doesn't get skipped.
475 '("\\<make\\(?:perl\\|sub\\s +\\S +\\)\\s *\n?\\s *{\\(?:{\\s *\n?\\(\\(?:.*\n\\)+?\\)\\s *}\\|\\s *\\(\\(?:.*?\\|\n?\\(?:.*\n\\)+?\\)\\)\\)}"
476 (1 'makefile-makepp-perl prepend t)
477 (2 'makefile-makepp-perl prepend t))
478 '("\\<\\(?:perl\\|sub\\s +\\S +\\)\\s *\n?\\s *{\\(?:{\\s *\n?\\(\\(?:.*\n\\)+?\\)\\s *}\\|\\s *\\(\\(?:.*?\\|\n?\\(?:.*\n\\)+?\\)\\)\\)}"
479 (1 'makefile-makepp-perl t t)
480 (2 'makefile-makepp-perl t t))
482 ;; Statement style perl block.
483 '("perl[-_]begin\\s *\\(?:\\s #.*\\)?\n\\(\\(?:.*\n\\)+?\\)\\s *perl[-_]end\\>"
484 1 'makefile-makepp-perl t)))
486 (defconst makefile-bsdmake-font-lock-keywords
487 (makefile-make-font-lock-keywords
488 ;; A lot more could be done for variables here:
489 makefile-var-use-regex
490 makefile-bsdmake-statements
492 "^\\(?: [ \t]*\\)?\\.\\(?:el\\)?if\\(n?\\)\\(?:def\\|make\\)?\\>[ \t]*\\(!?\\)"
493 '("^[ \t]*\\.for[ \t].+[ \t]\\(in\\)\\>" 1 font-lock-keyword-face)))
496 (defconst makefile-font-lock-syntactic-keywords
497 ;; From sh-script.el.
498 ;; A `#' begins a comment in sh when it is unquoted and at the beginning
499 ;; of a word. In the shell, words are separated by metacharacters.
500 ;; The list of special chars is taken from the single-unix spec of the
501 ;; shell command language (under `quoting') but with `$' removed.
502 '(("[^|&;<>()`\\\"' \t\n]\\(#+\\)" 1 "_")
503 ;; Change the syntax of a quoted newline so that it does not end a comment.
506 (defvar makefile-imenu-generic-expression
507 `(("Dependencies" makefile-previous-dependency 1)
508 ("Macro Assignment" ,makefile-macroassign-regex 1))
509 "Imenu generic expression for Makefile mode. See `imenu-generic-expression'.")
511 ;;; ------------------------------------------------------------
512 ;;; The following configurable variables are used in the
513 ;;; up-to-date overview .
514 ;;; The standard configuration assumes that your `make' program
515 ;;; can be run in question/query mode using the `-q' option, this
516 ;;; means that the command
520 ;;; should return an exit status of zero if the target `foo' is
521 ;;; up to date and a nonzero exit status otherwise.
522 ;;; Many makes can do this although the docs/manpages do not mention
523 ;;; it. Try it with your favourite one. GNU make, System V make, and
524 ;;; Dennis Vadura's DMake have no problems.
525 ;;; Set the variable `makefile-brave-make' to the name of the
526 ;;; make utility that does this on your system.
527 ;;; To understand what this is all about see the function definition
528 ;;; of `makefile-query-by-make-minus-q' .
529 ;;; ------------------------------------------------------------
531 (defcustom makefile-brave-make "make"
532 "*How to invoke make, for `makefile-query-targets'.
533 This should identify a `make' command that can handle the `-q' option."
537 (defcustom makefile-query-one-target-method 'makefile-query-by-make-minus-q
538 "*Function to call to determine whether a make target is up to date.
539 The function must satisfy this calling convention:
541 * As its first argument, it must accept the name of the target to
542 be checked, as a string.
544 * As its second argument, it may accept the name of a makefile
545 as a string. Depending on what you're going to do you may
548 * It must return the integer value 0 (zero) if the given target
549 should be considered up-to-date in the context of the given
550 makefile, any nonzero integer value otherwise."
554 (defcustom makefile-up-to-date-buffer-name "*Makefile Up-to-date overview*"
555 "*Name of the Up-to-date overview buffer."
559 ;;; --- end of up-to-date-overview configuration ------------------
561 (defvar makefile-mode-abbrev-table nil
562 "Abbrev table in use in Makefile buffers.")
563 (if makefile-mode-abbrev-table
565 (define-abbrev-table 'makefile-mode-abbrev-table ()))
567 (defvar makefile-mode-map
568 (let ((map (make-sparse-keymap)))
570 (define-key map "\C-c:" 'makefile-insert-target-ref)
571 (if makefile-electric-keys
573 (define-key map "$" 'makefile-insert-macro-ref)
574 (define-key map ":" 'makefile-electric-colon)
575 (define-key map "=" 'makefile-electric-equal)
576 (define-key map "." 'makefile-electric-dot)))
577 (define-key map "\C-c\C-f" 'makefile-pickup-filenames-as-targets)
578 (define-key map "\C-c\C-b" 'makefile-switch-to-browser)
579 (define-key map "\C-c\C-c" 'comment-region)
580 (define-key map "\C-c\C-p" 'makefile-pickup-everything)
581 (define-key map "\C-c\C-u" 'makefile-create-up-to-date-overview)
582 (define-key map "\C-c\C-i" 'makefile-insert-gmake-function)
583 (define-key map "\C-c\C-\\" 'makefile-backslash-region)
584 (define-key map "\C-c\C-m\C-a" 'makefile-automake-mode)
585 (define-key map "\C-c\C-m\C-b" 'makefile-bsdmake-mode)
586 (define-key map "\C-c\C-m\C-g" 'makefile-gmake-mode)
587 (define-key map "\C-c\C-m\C-m" 'makefile-mode)
588 (define-key map "\C-c\C-m\C-p" 'makefile-makepp-mode)
589 (define-key map "\M-p" 'makefile-previous-dependency)
590 (define-key map "\M-n" 'makefile-next-dependency)
591 (define-key map "\e\t" 'makefile-complete)
594 (define-key map [menu-bar makefile-mode]
595 (cons "Makefile" (make-sparse-keymap "Makefile")))
597 (define-key map [menu-bar makefile-mode browse]
598 '("Pop up Makefile Browser" . makefile-switch-to-browser))
599 (define-key map [menu-bar makefile-mode complete]
600 '("Complete Target or Macro" . makefile-complete))
601 (define-key map [menu-bar makefile-mode pickup]
602 '("Find Targets and Macros" . makefile-pickup-everything))
604 (define-key map [menu-bar makefile-mode prev]
605 '("Move to Previous Dependency" . makefile-previous-dependency))
606 (define-key map [menu-bar makefile-mode next]
607 '("Move to Next Dependency" . makefile-next-dependency))
609 "The keymap that is used in Makefile mode.")
611 (defvar makefile-browser-map nil
612 "The keymap that is used in the macro- and target browser.")
613 (if makefile-browser-map
615 (setq makefile-browser-map (make-sparse-keymap))
616 (define-key makefile-browser-map "n" 'makefile-browser-next-line)
617 (define-key makefile-browser-map "\C-n" 'makefile-browser-next-line)
618 (define-key makefile-browser-map "p" 'makefile-browser-previous-line)
619 (define-key makefile-browser-map "\C-p" 'makefile-browser-previous-line)
620 (define-key makefile-browser-map " " 'makefile-browser-toggle)
621 (define-key makefile-browser-map "i" 'makefile-browser-insert-selection)
622 (define-key makefile-browser-map "I" 'makefile-browser-insert-selection-and-quit)
623 (define-key makefile-browser-map "\C-c\C-m" 'makefile-browser-insert-continuation)
624 (define-key makefile-browser-map "q" 'makefile-browser-quit)
625 ;; disable horizontal movement
626 (define-key makefile-browser-map "\C-b" 'undefined)
627 (define-key makefile-browser-map "\C-f" 'undefined))
630 (defvar makefile-mode-syntax-table nil)
631 (if makefile-mode-syntax-table
633 (setq makefile-mode-syntax-table (make-syntax-table))
634 (modify-syntax-entry ?\( "() " makefile-mode-syntax-table)
635 (modify-syntax-entry ?\) ")( " makefile-mode-syntax-table)
636 (modify-syntax-entry ?\[ "(] " makefile-mode-syntax-table)
637 (modify-syntax-entry ?\] ")[ " makefile-mode-syntax-table)
638 (modify-syntax-entry ?\{ "(} " makefile-mode-syntax-table)
639 (modify-syntax-entry ?\} "){ " makefile-mode-syntax-table)
640 (modify-syntax-entry ?\' "\" " makefile-mode-syntax-table)
641 (modify-syntax-entry ?\` "\" " makefile-mode-syntax-table)
642 (modify-syntax-entry ?# "< " makefile-mode-syntax-table)
643 (modify-syntax-entry ?\n "> " makefile-mode-syntax-table))
646 ;;; ------------------------------------------------------------
647 ;;; Internal variables.
648 ;;; You don't need to configure below this line.
649 ;;; ------------------------------------------------------------
651 (defvar makefile-target-table nil
652 "Table of all target names known for this buffer.")
654 (defvar makefile-macro-table nil
655 "Table of all macro names known for this buffer.")
657 (defvar makefile-browser-client
658 "A buffer in Makefile mode that is currently using the browser.")
660 (defvar makefile-browser-selection-vector nil)
661 (defvar makefile-has-prereqs nil)
662 (defvar makefile-need-target-pickup t)
663 (defvar makefile-need-macro-pickup t)
665 (defvar makefile-mode-hook '())
667 ;; Each element looks like '("GNU MAKE FUNCTION" "ARG" "ARG" ... )
668 ;; Each "ARG" is used as a prompt for a required argument.
669 (defconst makefile-gnumake-functions-alist
672 ("subst" "From" "To" "In")
673 ("patsubst" "Pattern" "Replacement" "In")
675 ("findstring" "Find what" "In")
676 ("filter" "Pattern" "Text")
677 ("filter-out" "Pattern" "Text")
679 ;; Filename functions
684 ("addprefix" "Prefix" "Names")
685 ("addsuffix" "Suffix" "Names")
686 ("join" "List 1" "List 2")
687 ("word" "Index" "Text")
690 ("wildcard" "Pattern")
692 ("foreach" "Variable" "List" "Text")
693 ("origin" "Variable")
694 ("shell" "Command")))
697 ;;; ------------------------------------------------------------
698 ;;; The mode function itself.
699 ;;; ------------------------------------------------------------
702 (defun makefile-mode ()
703 "Major mode for editing standard Makefiles.
705 If you are editing a file for a different make, try one of the
706 variants `makefile-automake-mode', `makefile-gmake-mode',
707 `makefile-makepp-mode' or `makefile-bsdmake-mode'. All but the
708 last should be correctly chosen based on the file name, except if
709 it is *.mk. This function ends by invoking the function(s)
710 `makefile-mode-hook'.
712 It is strongly recommended to use `font-lock-mode', because that
713 provides additional parsing information. This is used for
714 example to see that a rule action `echo foo: bar' is a not rule
715 dependency, despite the colon.
717 \\{makefile-mode-map}
719 In the browser, use the following keys:
721 \\{makefile-browser-map}
723 Makefile mode can be configured by modifying the following variables:
725 `makefile-browser-buffer-name':
726 Name of the macro- and target browser buffer.
728 `makefile-target-colon':
729 The string that gets appended to all target names
730 inserted by `makefile-insert-target'.
731 \":\" or \"::\" are quite common values.
733 `makefile-macro-assign':
734 The string that gets appended to all macro names
735 inserted by `makefile-insert-macro'.
736 The normal value should be \" = \", since this is what
737 standard make expects. However, newer makes such as dmake
738 allow a larger variety of different macro assignments, so you
739 might prefer to use \" += \" or \" := \" .
741 `makefile-tab-after-target-colon':
742 If you want a TAB (instead of a space) to be appended after the
743 target colon, then set this to a non-nil value.
745 `makefile-browser-leftmost-column':
746 Number of blanks to the left of the browser selection mark.
748 `makefile-browser-cursor-column':
749 Column in which the cursor is positioned when it moves
750 up or down in the browser.
752 `makefile-browser-selected-mark':
753 String used to mark selected entries in the browser.
755 `makefile-browser-unselected-mark':
756 String used to mark unselected entries in the browser.
758 `makefile-browser-auto-advance-after-selection-p':
759 If this variable is set to a non-nil value the cursor
760 will automagically advance to the next line after an item
761 has been selected in the browser.
763 `makefile-pickup-everything-picks-up-filenames-p':
764 If this variable is set to a non-nil value then
765 `makefile-pickup-everything' also picks up filenames as targets
766 (i.e. it calls `makefile-pickup-filenames-as-targets'), otherwise
767 filenames are omitted.
769 `makefile-cleanup-continuations':
770 If this variable is set to a non-nil value then Makefile mode
771 will assure that no line in the file ends with a backslash
772 (the continuation character) followed by any whitespace.
773 This is done by silently removing the trailing whitespace, leaving
774 the backslash itself intact.
775 IMPORTANT: Please note that enabling this option causes Makefile mode
776 to MODIFY A FILE WITHOUT YOUR CONFIRMATION when \"it seems necessary\".
778 `makefile-browser-hook':
779 A function or list of functions to be called just before the
780 browser is entered. This is executed in the makefile buffer.
782 `makefile-special-targets-list':
783 List of special targets. You will be offered to complete
784 on one of those in the minibuffer whenever you enter a `.'.
785 at the beginning of a line in Makefile mode."
788 (kill-all-local-variables)
789 (add-hook 'write-file-functions
790 'makefile-warn-suspicious-lines nil t)
791 (add-hook 'write-file-functions
792 'makefile-warn-continuations nil t)
793 (add-hook 'write-file-functions
794 'makefile-cleanup-continuations nil t)
795 (make-local-variable 'makefile-target-table)
796 (make-local-variable 'makefile-macro-table)
797 (make-local-variable 'makefile-has-prereqs)
798 (make-local-variable 'makefile-need-target-pickup)
799 (make-local-variable 'makefile-need-macro-pickup)
802 (make-local-variable 'font-lock-defaults)
803 (setq font-lock-defaults
804 ;; SYNTAX-BEGIN set to backward-paragraph to avoid slow-down
805 ;; near the end of a large buffer, due to parse-partial-sexp's
806 ;; trying to parse all the way till the beginning of buffer.
807 '(makefile-font-lock-keywords
811 (font-lock-syntactic-keywords . makefile-font-lock-syntactic-keywords)
812 (font-lock-support-mode))) ; JIT breaks on long series of continuation lines.
815 (make-local-variable 'add-log-current-defun-function)
816 (setq add-log-current-defun-function 'makefile-add-log-defun)
819 (make-local-variable 'imenu-generic-expression)
820 (setq imenu-generic-expression makefile-imenu-generic-expression)
823 (make-local-variable 'dabbrev-abbrev-skip-leading-regexp)
824 (setq dabbrev-abbrev-skip-leading-regexp "\\$")
827 (setq local-abbrev-table makefile-mode-abbrev-table)
830 (make-local-variable 'fill-paragraph-function)
831 (setq fill-paragraph-function 'makefile-fill-paragraph)
834 (make-local-variable 'comment-start)
835 (setq comment-start "#")
836 (make-local-variable 'comment-end)
837 (setq comment-end "")
838 (make-local-variable 'comment-start-skip)
839 (setq comment-start-skip "#+[ \t]*")
841 ;; Make sure TAB really inserts \t.
842 (set (make-local-variable 'indent-line-function) 'indent-to-left-margin)
844 ;; become the current major mode
845 (setq major-mode 'makefile-mode)
846 (setq mode-name "Makefile")
848 ;; Activate keymap and syntax table.
849 (use-local-map makefile-mode-map)
850 (set-syntax-table makefile-mode-syntax-table)
852 ;; Real TABs are important in makefiles
853 (setq indent-tabs-mode t)
854 (run-mode-hooks 'makefile-mode-hook))
856 ;; These should do more than just differentiate font-lock.
858 (define-derived-mode makefile-automake-mode makefile-mode "Makefile.am"
859 "An adapted `makefile-mode' that knows about automake."
860 (setq font-lock-defaults
861 `(makefile-automake-font-lock-keywords ,@(cdr font-lock-defaults))))
864 (define-derived-mode makefile-gmake-mode makefile-mode "GNUmakefile"
865 "An adapted `makefile-mode' that knows about gmake."
866 (setq font-lock-defaults
867 `(makefile-gmake-font-lock-keywords ,@(cdr font-lock-defaults))))
870 (define-derived-mode makefile-makepp-mode makefile-mode "Makeppfile"
871 "An adapted `makefile-mode' that knows about makepp."
872 (set (make-local-variable 'makefile-rule-action-regex)
873 makefile-makepp-rule-action-regex)
874 (setq font-lock-defaults
875 `(makefile-makepp-font-lock-keywords ,@(cdr font-lock-defaults))
876 imenu-generic-expression
877 `(("Functions" "^[ \t]*\\(?:make\\)?sub[ \t]+\\([A-Za-z0-9_]+\\)" 1)
878 ,@imenu-generic-expression)))
881 (define-derived-mode makefile-bsdmake-mode makefile-mode "BSDmakefile"
882 "An adapted `makefile-mode' that knows about BSD make."
883 (set (make-local-variable 'makefile-dependency-regex)
884 makefile-bsdmake-dependency-regex)
885 (set (make-local-variable 'makefile-dependency-skip) "^:!")
886 (set (make-local-variable 'makefile-rule-action-regex)
887 makefile-bsdmake-rule-action-regex)
888 (setq font-lock-defaults
889 `(makefile-bsdmake-font-lock-keywords ,@(cdr font-lock-defaults))))
895 (defun makefile-next-dependency ()
896 "Move point to the beginning of the next dependency line."
898 (let ((here (point)))
900 (if (makefile-match-dependency nil)
901 (progn (beginning-of-line) t) ; indicate success
902 (goto-char here) nil)))
904 (defun makefile-previous-dependency ()
905 "Move point to the beginning of the previous dependency line."
909 ;; makefile-match-dependency done backwards:
911 (while (progn (skip-chars-backward makefile-dependency-skip)
913 (or (prog1 (eq (char-after) ?=)
915 (get-text-property (point) 'face)
917 (if (> (point) (+ (point-min) 2))
918 (eq (char-before (1- (point))) ?\\))
919 (if (looking-at makefile-dependency-regex)
926 ;;; Electric keys. Blech.
928 (defun makefile-electric-dot (arg)
929 "Prompt for the name of a special target to insert.
930 Only does electric insertion at beginning of line.
931 Anywhere else just self-inserts."
934 (makefile-insert-special-target)
935 (self-insert-command arg)))
937 (defun makefile-insert-special-target ()
938 "Prompt for and insert a special target name.
939 Uses `makefile-special-targets' list."
941 (makefile-pickup-targets)
942 (let ((special-target
943 (completing-read "Special target: "
944 makefile-special-targets-list nil nil nil)))
945 (if (zerop (length special-target))
947 (insert "." special-target ":")
948 (makefile-forward-after-target-colon))))
950 (defun makefile-electric-equal (arg)
951 "Prompt for name of a macro to insert.
952 Only does prompting if point is at beginning of line.
953 Anywhere else just self-inserts."
955 (makefile-pickup-macros)
957 (call-interactively 'makefile-insert-macro)
958 (self-insert-command arg)))
960 (defun makefile-insert-macro (macro-name)
961 "Prepare definition of a new macro."
962 (interactive "sMacro Name: ")
963 (makefile-pickup-macros)
964 (if (not (zerop (length macro-name)))
967 (insert macro-name makefile-macro-assign)
968 (setq makefile-need-macro-pickup t)
969 (makefile-remember-macro macro-name))))
971 (defun makefile-insert-macro-ref (macro-name)
972 "Complete on a list of known macros, then insert complete ref at point."
976 (makefile-pickup-macros)
977 (completing-read "Refer to macro: " makefile-macro-table nil nil nil))))
978 (makefile-do-macro-insertion macro-name))
980 (defun makefile-insert-target (target-name)
981 "Prepare definition of a new target (dependency line)."
982 (interactive "sTarget: ")
983 (if (not (zerop (length target-name)))
986 (insert target-name makefile-target-colon)
987 (makefile-forward-after-target-colon)
989 (setq makefile-need-target-pickup t)
990 (makefile-remember-target target-name))))
992 (defun makefile-insert-target-ref (target-name)
993 "Complete on a list of known targets, then insert TARGET-NAME at point."
997 (makefile-pickup-targets)
998 (completing-read "Refer to target: " makefile-target-table nil nil nil))))
999 (if (not (zerop (length target-name)))
1000 (insert target-name " ")))
1002 (defun makefile-electric-colon (arg)
1003 "Prompt for name of new target.
1004 Prompting only happens at beginning of line.
1005 Anywhere else just self-inserts."
1008 (call-interactively 'makefile-insert-target)
1009 (self-insert-command arg)))
1013 ;;; ------------------------------------------------------------
1014 ;;; Extracting targets and macros from an existing makefile
1015 ;;; ------------------------------------------------------------
1017 (defun makefile-pickup-targets ()
1018 "Notice names of all target definitions in Makefile."
1020 (when makefile-need-target-pickup
1021 (setq makefile-need-target-pickup nil
1022 makefile-target-table nil
1023 makefile-has-prereqs nil)
1025 (goto-char (point-min))
1026 (while (makefile-match-dependency nil)
1027 (goto-char (match-beginning 1))
1028 (while (let ((target-name
1029 (buffer-substring-no-properties (point)
1031 (skip-chars-forward "^ \t:#")
1034 (not (looking-at ":[ \t]*$"))))
1035 (if (makefile-remember-target target-name has-prereqs)
1036 (message "Picked up target \"%s\" from line %d"
1037 target-name (line-number-at-pos)))
1038 (skip-chars-forward " \t")
1039 (not (or (eolp) (eq (char-after) ?:)))))
1041 (message "Read targets OK.")))
1043 (defun makefile-pickup-macros ()
1044 "Notice names of all macro definitions in Makefile."
1046 (when makefile-need-macro-pickup
1047 (setq makefile-need-macro-pickup nil
1048 makefile-macro-table nil)
1050 (goto-char (point-min))
1051 (while (re-search-forward makefile-macroassign-regex nil t)
1052 (goto-char (match-beginning 1))
1053 (let ((macro-name (buffer-substring-no-properties (point)
1055 (skip-chars-forward "^ \t:#=*")
1057 (if (makefile-remember-macro macro-name)
1058 (message "Picked up macro \"%s\" from line %d"
1059 macro-name (line-number-at-pos))))
1061 (message "Read macros OK.")))
1063 (defun makefile-pickup-everything (arg)
1064 "Notice names of all macros and targets in Makefile.
1065 Prefix arg means force pickups to be redone."
1068 (setq makefile-need-target-pickup t
1069 makefile-need-macro-pickup t))
1070 (makefile-pickup-macros)
1071 (makefile-pickup-targets)
1072 (if makefile-pickup-everything-picks-up-filenames-p
1073 (makefile-pickup-filenames-as-targets)))
1075 (defun makefile-pickup-filenames-as-targets ()
1076 "Scan the current directory for filenames to use as targets.
1077 Checks each filename against `makefile-ignored-files-in-pickup-regex'
1078 and adds all qualifying names to the list of known targets."
1080 (mapc (lambda (name)
1081 (or (file-directory-p name)
1082 (string-match makefile-ignored-files-in-pickup-regex name)
1083 (if (makefile-remember-target name)
1084 (message "Picked up file \"%s\" as target" name))))
1085 (file-name-all-completions "" (or (file-name-directory (buffer-file-name)) ""))))
1091 (defun makefile-complete ()
1092 "Perform completion on Makefile construct preceding point.
1093 Can complete variable and target names.
1094 The context determines which are considered."
1096 (let* ((beg (save-excursion
1097 (skip-chars-backward "^$(){}:#= \t\n")
1099 (try (buffer-substring beg (point)))
1105 (let ((pc (preceding-char)))
1107 ;; Beginning of line means anything.
1111 ;; Preceding "$" means macros only.
1115 ;; Preceding "$(" or "${" means macros only.
1122 (= (preceding-char) ?$))))
1123 (setq do-macros t)))))
1126 (let* ((table (append (if do-macros
1128 makefile-target-table)
1129 makefile-macro-table))
1130 (completion (try-completion try table)))
1132 ;; Exact match, so insert closing paren or colon.
1134 (insert (if do-macros
1146 (message "Can't find completion for \"%s\"" try)
1149 ;; Partial completion.
1150 ((not (string= try completion))
1151 ;; FIXME it would be nice to supply the closing paren if an
1152 ;; exact, unambiguous match were found. That is not possible
1153 ;; right now. Ditto closing ":" for targets.
1154 (delete-region beg (point))
1156 ;; DO-MACROS means doing macros only. If not that, then check
1157 ;; to see if this completion is a macro. Special insertion
1158 ;; must be done for macros.
1160 (assoc completion makefile-macro-table))
1161 (let ((makefile-use-curly-braces-for-macros-p
1163 makefile-use-curly-braces-for-macros-p)))
1164 (delete-backward-char 2)
1165 (makefile-do-macro-insertion completion)
1166 (delete-backward-char 1))
1168 ;; Just insert targets.
1169 (insert completion)))
1171 ;; Can't complete any more, so make completion list. FIXME
1172 ;; this doesn't do the right thing when the completion is
1173 ;; actually inserted. I don't think there is an easy way to do
1176 (message "Making completion list...")
1177 (let ((list (all-completions try table)))
1178 (with-output-to-temp-buffer "*Completions*"
1179 (display-completion-list list)))
1180 (message "Making completion list...done"))))))
1184 ;; Backslashification. Stolen from cc-mode.el.
1186 (defun makefile-backslash-region (from to delete-flag)
1187 "Insert, align, or delete end-of-line backslashes on the lines in the region.
1188 With no argument, inserts backslashes and aligns existing backslashes.
1189 With an argument, deletes the backslashes.
1191 This function does not modify the last line of the region if the region ends
1192 right at the start of the following line; it does not modify blank lines
1193 at the start of the region. So you can put the region around an entire macro
1194 definition and conveniently use this command."
1195 (interactive "r\nP")
1198 (let ((column makefile-backslash-column)
1199 (endmark (make-marker)))
1200 (move-marker endmark to)
1201 ;; Compute the smallest column number past the ends of all the lines.
1202 (if makefile-backslash-align
1204 (if (not delete-flag)
1205 (while (< (point) to)
1207 (if (= (preceding-char) ?\\)
1208 (progn (forward-char -1)
1209 (skip-chars-backward " \t")))
1210 (setq column (max column (1+ (current-column))))
1212 ;; Adjust upward to a tab column, if that doesn't push
1214 (if (> (% column tab-width) 0)
1215 (let ((adjusted (* (/ (+ column tab-width -1) tab-width)
1217 (if (< adjusted (window-width))
1218 (setq column adjusted))))))
1219 ;; Don't modify blank lines at start of region.
1221 (while (and (< (point) endmark) (eolp))
1223 ;; Add or remove backslashes on all the lines.
1224 (while (and (< (point) endmark)
1225 ;; Don't backslashify the last line
1226 ;; if the region ends right at the start of the next line.
1229 (< (point) endmark)))
1230 (if (not delete-flag)
1231 (makefile-append-backslash column)
1232 (makefile-delete-backslash))
1234 (move-marker endmark nil))))
1236 (defun makefile-append-backslash (column)
1238 ;; Note that "\\\\" is needed to get one backslash.
1239 (if (= (preceding-char) ?\\)
1240 (progn (forward-char -1)
1241 (delete-horizontal-space)
1242 (indent-to column (if makefile-backslash-align nil 1)))
1243 (indent-to column (if makefile-backslash-align nil 1))
1246 (defun makefile-delete-backslash ()
1251 (if (looking-at "\\\\")
1252 (delete-region (1+ (point))
1253 (progn (skip-chars-backward " \t") (point)))))))
1259 (defun makefile-fill-paragraph (arg)
1260 ;; Fill comments, backslashed lines, and variable definitions
1266 ;; Found a comment. Set the fill prefix, and find the paragraph
1267 ;; boundaries by searching for lines that look like comment-only
1269 (let ((fill-prefix (match-string-no-properties 0))
1270 (fill-paragraph-function nil))
1274 ;; Search backwards.
1276 (while (and (zerop (forward-line -1))
1278 ;; We may have gone too far. Go forward again.
1279 (or (looking-at "^#")
1284 (while (looking-at "^#")
1287 (fill-paragraph nil)
1290 ;; Must look for backslashed-region before looking for variable
1292 ((or (eq (char-before (line-end-position 1)) ?\\)
1293 (eq (char-before (line-end-position 0)) ?\\))
1294 ;; A backslash region. Find beginning and end, remove
1295 ;; backslashes, fill, and then reapply backslahes.
1300 (while (= (preceding-char) ?\\)
1306 (while (= (preceding-char) ?\\)
1310 (narrow-to-region beginning end)
1311 (makefile-backslash-region (point-min) (point-max) t)
1312 (let ((fill-paragraph-function nil))
1313 (fill-paragraph nil))
1314 (makefile-backslash-region (point-min) (point-max) nil)
1315 (goto-char (point-max))
1316 (if (< (skip-chars-backward "\n") 0)
1317 (delete-region (point) (point-max))))))
1319 ((looking-at makefile-macroassign-regex)
1320 ;; Have a macro assign. Fill just this line, and then backslash
1321 ;; resulting region.
1323 (narrow-to-region (point) (line-beginning-position 2))
1324 (let ((fill-paragraph-function nil))
1325 (fill-paragraph nil))
1326 (makefile-backslash-region (point-min) (point-max) nil)))))
1328 ;; Always return non-nil so we don't fill anything else.
1333 ;;; ------------------------------------------------------------
1335 ;;; ------------------------------------------------------------
1337 (defun makefile-browser-format-target-line (target selected)
1339 (concat (make-string makefile-browser-leftmost-column ?\ )
1341 makefile-browser-selected-mark
1342 makefile-browser-unselected-mark)
1344 target makefile-target-colon))
1346 (defun makefile-browser-format-macro-line (macro selected)
1348 (concat (make-string makefile-browser-leftmost-column ?\ )
1350 makefile-browser-selected-mark
1351 makefile-browser-unselected-mark)
1352 (makefile-format-macro-ref macro))))
1354 (defun makefile-browser-fill (targets macros)
1355 (let ((inhibit-read-only t))
1356 (goto-char (point-min))
1360 (lambda (item) (insert (makefile-browser-format-target-line (car item) nil) "\n")))
1365 (lambda (item) (insert (makefile-browser-format-macro-line (car item) nil) "\n")))
1368 (sort-lines nil (point-min) (point-max))
1369 (goto-char (1- (point-max)))
1370 (delete-char 1) ; remove unnecessary newline at eob
1371 (goto-char (point-min))
1372 (forward-char makefile-browser-cursor-column)))
1375 ;;; Moving up and down in the browser
1378 (defun makefile-browser-next-line ()
1379 "Move the browser selection cursor to the next line."
1381 (if (not (makefile-last-line-p))
1384 (forward-char makefile-browser-cursor-column))))
1386 (defun makefile-browser-previous-line ()
1387 "Move the browser selection cursor to the previous line."
1389 (if (not (makefile-first-line-p))
1392 (forward-char makefile-browser-cursor-column))))
1395 ;;; Quitting the browser (returns to client buffer)
1398 (defun makefile-browser-quit ()
1399 "Leave the browser and return to the makefile buffer."
1401 (let ((my-client makefile-browser-client))
1402 (setq makefile-browser-client nil) ; we quitted, so NO client!
1403 (set-buffer-modified-p nil)
1405 (pop-to-buffer my-client)))
1408 ;;; Toggle state of a browser item
1411 (defun makefile-browser-toggle ()
1412 "Toggle the selection state of the browser item at the cursor position."
1414 (let ((this-line (count-lines (point-min) (point))))
1415 (setq this-line (max 1 this-line))
1416 (makefile-browser-toggle-state-for-line this-line)
1417 (goto-line this-line)
1418 (let ((inhibit-read-only t))
1420 (if (makefile-browser-on-macro-line-p)
1421 (let ((macro-name (makefile-browser-this-line-macro-name)))
1422 (delete-region (point) (progn (end-of-line) (point)))
1424 (makefile-browser-format-macro-line
1426 (makefile-browser-get-state-for-line this-line))))
1427 (let ((target-name (makefile-browser-this-line-target-name)))
1428 (delete-region (point) (progn (end-of-line) (point)))
1430 (makefile-browser-format-target-line
1432 (makefile-browser-get-state-for-line this-line))))))
1434 (forward-char makefile-browser-cursor-column)
1435 (if makefile-browser-auto-advance-after-selection-p
1436 (makefile-browser-next-line))))
1439 ;;; Making insertions into the client buffer
1442 (defun makefile-browser-insert-continuation ()
1443 "Insert a makefile continuation.
1444 In the makefile buffer, go to (end-of-line), insert a \'\\\'
1445 character, insert a new blank line, go to that line and indent by one TAB.
1446 This is most useful in the process of creating continued lines when copying
1447 large dependencies from the browser to the client buffer.
1448 \(point) advances accordingly in the client buffer."
1450 (with-current-buffer makefile-browser-client
1454 (defun makefile-browser-insert-selection ()
1455 "Insert all selected targets and/or macros in the makefile buffer.
1456 Insertion takes place at point."
1460 (let ((current-line 1))
1462 (if (makefile-browser-get-state-for-line current-line)
1463 (makefile-browser-send-this-line-item))
1465 (setq current-line (1+ current-line))))))
1467 (defun makefile-browser-insert-selection-and-quit ()
1469 (makefile-browser-insert-selection)
1470 (makefile-browser-quit))
1472 (defun makefile-browser-send-this-line-item ()
1473 (if (makefile-browser-on-macro-line-p)
1475 (let ((macro-name (makefile-browser-this-line-macro-name)))
1476 (set-buffer makefile-browser-client)
1477 (insert (makefile-format-macro-ref macro-name) " ")))
1479 (let ((target-name (makefile-browser-this-line-target-name)))
1480 (set-buffer makefile-browser-client)
1481 (insert target-name " ")))))
1483 (defun makefile-browser-start-interaction ()
1484 (use-local-map makefile-browser-map)
1485 (setq buffer-read-only t))
1487 (defun makefile-browse (targets macros)
1489 (if (zerop (+ (length targets) (length macros)))
1492 (message "No macros or targets to browse! Consider running 'makefile-pickup-everything\'"))
1493 (let ((browser-buffer (get-buffer-create makefile-browser-buffer-name)))
1494 (pop-to-buffer browser-buffer)
1495 (makefile-browser-fill targets macros)
1496 (shrink-window-if-larger-than-buffer)
1497 (set (make-local-variable 'makefile-browser-selection-vector)
1498 (make-vector (+ (length targets) (length macros)) nil))
1499 (makefile-browser-start-interaction))))
1501 (defun makefile-switch-to-browser ()
1503 (run-hooks 'makefile-browser-hook)
1504 (setq makefile-browser-client (current-buffer))
1505 (makefile-pickup-targets)
1506 (makefile-pickup-macros)
1507 (makefile-browse makefile-target-table makefile-macro-table))
1511 ;;; ------------------------------------------------------------
1512 ;;; Up-to-date overview buffer
1513 ;;; ------------------------------------------------------------
1515 (defun makefile-create-up-to-date-overview ()
1516 "Create a buffer containing an overview of the state of all known targets.
1517 Known targets are targets that are explicitly defined in that makefile;
1518 in other words, all targets that appear on the left hand side of a
1519 dependency in the makefile."
1521 (if (y-or-n-p "Are you sure that the makefile being edited is consistent? ")
1523 ;; The rest of this function operates on a temporary makefile, created by
1524 ;; writing the current contents of the makefile buffer.
1526 (let ((saved-target-table makefile-target-table)
1527 (this-buffer (current-buffer))
1528 (makefile-up-to-date-buffer
1529 (get-buffer-create makefile-up-to-date-buffer-name))
1530 (filename (makefile-save-temporary))
1532 ;; Forget the target table because it may contain picked-up filenames
1533 ;; that are not really targets in the current makefile.
1534 ;; We don't want to query these, so get a new target-table with just the
1535 ;; targets that can be found in the makefile buffer.
1536 ;; The 'old' target table will be restored later.
1538 (real-targets (progn
1539 (makefile-pickup-targets)
1540 makefile-target-table))
1541 (prereqs makefile-has-prereqs)
1544 (set-buffer makefile-up-to-date-buffer)
1545 (setq buffer-read-only nil)
1547 (makefile-query-targets filename real-targets prereqs)
1548 (if (zerop (buffer-size)) ; if it did not get us anything
1550 (kill-buffer (current-buffer))
1551 (message "No overview created!")))
1552 (set-buffer this-buffer)
1553 (setq makefile-target-table saved-target-table)
1554 (if (get-buffer makefile-up-to-date-buffer-name)
1556 (pop-to-buffer (get-buffer makefile-up-to-date-buffer-name))
1557 (shrink-window-if-larger-than-buffer)
1558 (sort-lines nil (point-min) (point-max))
1559 (setq buffer-read-only t))))))
1561 (defun makefile-save-temporary ()
1562 "Create a temporary file from the current makefile buffer."
1563 (let ((filename (makefile-generate-temporary-filename)))
1564 (write-region (point-min) (point-max) filename nil 0)
1565 filename)) ; return the filename
1567 (defun makefile-generate-temporary-filename ()
1568 "Create a filename suitable for use in `makefile-save-temporary'.
1569 Be careful to allow brain-dead file systems (DOS, SYSV ...) to cope
1570 with the generated name!"
1571 (let ((my-name (user-login-name))
1572 (my-uid (int-to-string (user-uid))))
1574 (if (> (length my-name) 3)
1575 (substring my-name 0 3)
1578 (if (> (length my-uid) 3)
1579 (substring my-uid 0 3)
1582 (defun makefile-query-targets (filename target-table prereq-list)
1583 "Fill the up-to-date overview buffer.
1584 Checks each target in TARGET-TABLE using `makefile-query-one-target-method'
1585 and generates the overview, one line per target name."
1588 (function (lambda (item)
1589 (let* ((target-name (car item))
1590 (no-prereqs (not (member target-name prereq-list)))
1591 (needs-rebuild (or no-prereqs
1593 makefile-query-one-target-method
1598 (cond (no-prereqs " .. has no prerequisites")
1599 (needs-rebuild " .. NEEDS REBUILD")
1600 (t " .. is up to date"))))
1603 (goto-char (point-min))
1604 (delete-file filename)) ; remove the tmpfile
1606 (defun makefile-query-by-make-minus-q (target &optional filename)
1608 (call-process makefile-brave-make nil nil nil
1609 "-f" filename "-q" target))))
1613 ;;; ------------------------------------------------------------
1614 ;;; Continuation cleanup
1615 ;;; ------------------------------------------------------------
1617 (defun makefile-cleanup-continuations ()
1618 (if (eq major-mode 'makefile-mode)
1619 (if (and makefile-cleanup-continuations
1620 (not buffer-read-only))
1622 (goto-char (point-min))
1623 (while (re-search-forward "\\\\[ \t]+$" nil t)
1624 (replace-match "\\" t t))))))
1627 ;;; ------------------------------------------------------------
1628 ;;; Warn of suspicious lines
1629 ;;; ------------------------------------------------------------
1631 (defun makefile-warn-suspicious-lines ()
1632 ;; Returning non-nil cancels the save operation
1633 (if (eq major-mode 'makefile-mode)
1635 (goto-char (point-min))
1636 (if (re-search-forward "^\\(\t+$\\| +\t\\)" nil t)
1638 (format "Suspicious line %d. Save anyway? "
1639 (count-lines (point-min) (point)))))))))
1641 (defun makefile-warn-continuations ()
1642 (if (eq major-mode 'makefile-mode)
1644 (goto-char (point-min))
1645 (if (re-search-forward "\\\\[ \t]+$" nil t)
1647 (format "Suspicious continuation in line %d. Save anyway? "
1648 (count-lines (point-min) (point)))))))))
1651 ;;; ------------------------------------------------------------
1652 ;;; GNU make function support
1653 ;;; ------------------------------------------------------------
1655 (defun makefile-insert-gmake-function ()
1656 "Insert a GNU make function call.
1657 Asks for the name of the function to use (with completion).
1658 Then prompts for all required parameters."
1660 (let* ((gm-function-name (completing-read
1662 makefile-gnumake-functions-alist
1664 (gm-function-prompts
1665 (cdr (assoc gm-function-name makefile-gnumake-functions-alist))))
1666 (if (not (zerop (length gm-function-name)))
1667 (insert (makefile-format-macro-ref
1668 (concat gm-function-name " "
1669 (makefile-prompt-for-gmake-funargs
1670 gm-function-name gm-function-prompts)))
1673 (defun makefile-prompt-for-gmake-funargs (function-name prompt-list)
1675 (function (lambda (one-prompt)
1676 (read-string (format "[%s] %s: " function-name one-prompt)
1683 ;;; ------------------------------------------------------------
1684 ;;; Utility functions
1685 ;;; ------------------------------------------------------------
1687 (defun makefile-match-function-end (end)
1688 "To be called as an anchored matcher by font-lock.
1689 The anchor must have matched the opening parens in the first group."
1690 (let ((s (match-string-no-properties 1)))
1691 (setq s (cond ((string= s "(") "\\(.*?\\)[ \t]*)")
1692 ((string= s "{") "\\(.*?\\)[ \t]*}")
1693 ((string= s "((") "\\(.*?\\)[ \t]*))")
1694 ((string= s "{{") "\\(.*?\\)[ \t]*}}")))
1695 (if s (looking-at s))))
1697 (defun makefile-match-dependency (bound)
1698 "Search for `makefile-dependency-regex' up to BOUND.
1699 Checks that the colon has not already been fontified, else we
1700 matched in a rule action."
1703 (while (progn (skip-chars-forward makefile-dependency-skip bound)
1704 (< (point) (or bound (point-max))))
1706 (or (eq (char-after) ?=)
1707 (get-text-property (1- (point)) 'face)
1708 (if (> (line-beginning-position) (+ (point-min) 2))
1709 (eq (char-before (line-end-position 0)) ?\\))
1710 (when (save-excursion
1712 (looking-at makefile-dependency-regex))
1714 (let ((deps-end (match-end 1))
1715 (match-data (match-data)))
1716 (goto-char deps-end)
1717 (skip-chars-backward " \t")
1718 (setq deps-end (point))
1720 (skip-chars-forward " \t")
1721 ;; Alter the bounds recorded for subexp 1,
1722 ;; which is what is supposed to match the targets.
1723 (setcar (nthcdr 2 match-data) (point))
1724 (setcar (nthcdr 3 match-data) deps-end)
1725 (store-match-data match-data)))
1727 (throw 'found (point)))))
1731 (defun makefile-match-action (bound)
1733 (while (re-search-forward makefile-rule-action-regex bound t)
1734 (or (eq ?\\ (char-after (- (match-beginning 0) 2)))
1735 (throw 'found t)))))
1737 (defun makefile-do-macro-insertion (macro-name)
1738 "Insert a macro reference."
1739 (if (not (zerop (length macro-name)))
1740 (if (assoc macro-name makefile-runtime-macros-list)
1741 (insert "$" macro-name)
1742 (insert (makefile-format-macro-ref macro-name)))))
1744 (defun makefile-remember-target (target-name &optional has-prereqs)
1745 "Remember a given target if it is not already remembered for this buffer."
1746 (if (not (zerop (length target-name)))
1748 (if (not (assoc target-name makefile-target-table))
1749 (setq makefile-target-table
1750 (cons (list target-name) makefile-target-table)))
1752 (setq makefile-has-prereqs
1753 (cons target-name makefile-has-prereqs))))))
1755 (defun makefile-remember-macro (macro-name)
1756 "Remember a given macro if it is not already remembered for this buffer."
1757 (if (not (zerop (length macro-name)))
1758 (if (not (assoc macro-name makefile-macro-table))
1759 (setq makefile-macro-table
1760 (cons (list macro-name) makefile-macro-table)))))
1762 (defun makefile-forward-after-target-colon ()
1763 "Move point forward after inserting the terminating colon of a target.
1764 This acts according to the value of `makefile-tab-after-target-colon'."
1765 (if makefile-tab-after-target-colon
1769 (defun makefile-browser-on-macro-line-p ()
1770 "Determine if point is on a macro line in the browser."
1773 (re-search-forward "\\$[{(]" (line-end-position) t)))
1775 (defun makefile-browser-this-line-target-name ()
1776 "Extract the target name from a line in the browser."
1779 (skip-chars-backward "^ \t")
1780 (buffer-substring (point) (1- (line-end-position)))))
1782 (defun makefile-browser-this-line-macro-name ()
1783 "Extract the macro name from a line in the browser."
1786 (re-search-forward "\\$[{(]" (line-end-position) t)
1787 (let ((macro-start (point)))
1788 (skip-chars-forward "^})")
1789 (buffer-substring macro-start (point)))))
1791 (defun makefile-format-macro-ref (macro-name)
1792 "Format a macro reference.
1793 Uses `makefile-use-curly-braces-for-macros-p'."
1794 (if (or (char-equal ?\( (string-to-char macro-name))
1795 (char-equal ?\{ (string-to-char macro-name)))
1796 (format "$%s" macro-name)
1797 (if makefile-use-curly-braces-for-macros-p
1798 (format "${%s}" macro-name)
1799 (format "$(%s)" macro-name))))
1801 (defun makefile-browser-get-state-for-line (n)
1802 (aref makefile-browser-selection-vector (1- n)))
1804 (defun makefile-browser-set-state-for-line (n to-state)
1805 (aset makefile-browser-selection-vector (1- n) to-state))
1807 (defun makefile-browser-toggle-state-for-line (n)
1808 (makefile-browser-set-state-for-line n (not (makefile-browser-get-state-for-line n))))
1810 (defun makefile-last-line-p ()
1811 (= (line-end-position) (point-max)))
1813 (defun makefile-first-line-p ()
1814 (= (line-beginning-position) (point-min)))
1818 ;;; Support for other packages, like add-log.
1820 (defun makefile-add-log-defun ()
1821 "Return name of target or variable assignment that point is in.
1822 If it isn't in one, return nil."
1826 ;; Scan back line by line, noticing when we come to a
1827 ;; variable or rule definition, and giving up when we see
1828 ;; a line that is not part of either of those.
1829 (while (not (or (setq found
1830 (when (or (looking-at makefile-macroassign-regex)
1831 (looking-at makefile-dependency-regex))
1832 (match-string-no-properties 1)))
1833 ;; Don't keep looking across a blank line or comment.
1834 (looking-at "$\\|#")
1835 (not (zerop (forward-line -1))))))
1838 (provide 'make-mode)
1840 ;;; arch-tag: bd23545a-de91-44fb-b1b2-feafbb2635a0
1841 ;;; make-mode.el ends here