1 ;;; make-mode.el --- makefile editing commands for Emacs
3 ;; Copyright (C) 1992,94,99,2000,2001, 2002, 2003 Free Software Foundation, Inc.
5 ;; Author: Thomas Neumann <tom@smart.bo.open.de>
6 ;; Eric S. Raymond <esr@snark.thyrsus.com>
9 ;; Keywords: unix, tools
11 ;; This file is part of GNU Emacs.
13 ;; GNU Emacs is free software; you can redistribute it and/or modify
14 ;; it under the terms of the GNU General Public License as published by
15 ;; the Free Software Foundation; either version 2, or (at your option)
18 ;; GNU Emacs is distributed in the hope that it will be useful,
19 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
20 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 ;; GNU General Public License for more details.
23 ;; You should have received a copy of the GNU General Public License
24 ;; along with GNU Emacs; see the file COPYING. If not, write to the
25 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
26 ;; Boston, MA 02111-1307, USA.
30 ;; A major mode for editing makefiles. The mode knows about Makefile
31 ;; syntax and defines M-n and M-p to move to next and previous productions.
33 ;; The keys $, =, : and . are electric; they try to help you fill in a
34 ;; macro reference, macro definition, ordinary target name, or special
35 ;; target name, respectively. Such names are completed using a list of
36 ;; targets and macro names parsed out of the makefile. This list is
37 ;; automatically updated, if necessary, whenever you invoke one of
38 ;; these commands. You can force it to be updated with C-c C-p.
40 ;; The command C-c C-f adds certain filenames in the current directory
41 ;; as targets. You can filter out filenames by setting the variable
42 ;; makefile-ignored-files-in-pickup-regex.
44 ;; The command C-c C-u grinds for a bit, then pops up a report buffer
45 ;; showing which target names are up-to-date with respect to their
46 ;; prerequisites, which targets are out-of-date, and which have no
49 ;; The command C-c C-b pops up a browser window listing all target and
50 ;; macro names. You can mark or unmark items with C-c SPC, and insert
51 ;; all marked items back in the Makefile with C-c TAB.
53 ;; The command C-c TAB in the makefile buffer inserts a GNU make builtin.
54 ;; You will be prompted for the builtin's args.
56 ;; There are numerous other customization variables.
61 ;; * Add missing doc strings, improve terse doc strings.
62 ;; * Eliminate electric stuff entirely.
63 ;; * It might be nice to highlight targets differently depending on
64 ;; whether they are up-to-date or not. Not sure how this would
65 ;; interact with font-lock.
66 ;; * Would be nice to edit the commands in ksh-mode and have
67 ;; indentation and slashification done automatically. Hard.
68 ;; * Consider removing browser mode. It seems useless.
69 ;; * ":" should notice when a new target is made and add it to the
70 ;; list (or at least set makefile-need-target-pickup).
71 ;; * Make browser into a major mode.
72 ;; * Clean up macro insertion stuff. It is a mess.
73 ;; * Browser entry and exit is weird. Normalize.
74 ;; * Browser needs to be rewritten. Right now it is kind of a crock.
76 ;; * Act more like dired/buffer menu/whatever.
77 ;; * Highlight as mouse traverses.
79 ;; * Update documentation above.
80 ;; * Update texinfo manual.
87 ;; Sadly we need this for a macro.
93 ;;; ------------------------------------------------------------
94 ;;; Configurable stuff
95 ;;; ------------------------------------------------------------
97 (defgroup makefile nil
98 "Makefile editing commands for Emacs."
102 (defface makefile-space-face
103 '((((class color
)) (:background
"hotpink"))
104 (t (:reverse-video t
)))
105 "Face to use for highlighting leading spaces in Font-Lock mode."
109 (defface makefile-targets-face
110 ;; This needs to go along both with foreground and background colors (i.e. shell)
111 '((t (:underline t
)))
112 "Face to use for additionally highlighting rule targets in Font-Lock mode."
117 (defface makefile-shell-face
118 '((((class color
) (background light
)) (:background
"seashell1"))
119 (((class color
) (background dark
)) (:background
"seashell4"))
120 (t (:reverse-video t
)))
121 "Face to use for additionally highlighting Shell commands in Font-Lock mode."
126 (defface makefile-makepp-perl-face
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."
135 (defcustom makefile-browser-buffer-name
"*Macros and Targets*"
136 "*Name of the macro- and target browser buffer."
140 (defcustom makefile-target-colon
":"
141 "*String to append to all target names inserted by `makefile-insert-target'.
142 \":\" or \"::\" are common values."
146 (defcustom makefile-macro-assign
" = "
147 "*String to append to all macro names inserted by `makefile-insert-macro'.
148 The normal value should be \" = \", since this is what
149 standard make expects. However, newer makes such as dmake
150 allow a larger variety of different macro assignments, so you
151 might prefer to use \" += \" or \" := \" ."
155 (defcustom makefile-electric-keys nil
156 "*If non-nil, Makefile mode should install electric keybindings.
161 (defcustom makefile-use-curly-braces-for-macros-p nil
162 "*Controls the style of generated macro references.
163 Non-nil means macro references should use curly braces, like `${this}'.
164 nil means use parentheses, like `$(this)'."
168 (defcustom makefile-tab-after-target-colon t
169 "*If non-nil, insert a TAB after a target colon.
170 Otherwise, a space is inserted.
175 (defcustom makefile-browser-leftmost-column
10
176 "*Number of blanks to the left of the browser selection mark."
180 (defcustom makefile-browser-cursor-column
10
181 "*Column the cursor goes to when it moves up or down in the Makefile browser."
185 (defcustom makefile-backslash-column
48
186 "*Column in which `makefile-backslash-region' inserts backslashes."
190 (defcustom makefile-backslash-align t
191 "*If non-nil, `makefile-backslash-region' will align backslashes."
195 (defcustom makefile-browser-selected-mark
"+ "
196 "*String used to mark selected entries in the Makefile browser."
200 (defcustom makefile-browser-unselected-mark
" "
201 "*String used to mark unselected entries in the Makefile browser."
205 (defcustom makefile-browser-auto-advance-after-selection-p t
206 "*If non-nil, cursor will move after item is selected in Makefile browser."
210 (defcustom makefile-pickup-everything-picks-up-filenames-p nil
211 "*If non-nil, `makefile-pickup-everything' picks up filenames as targets.
212 This means it calls `makefile-pickup-filenames-as-targets'.
213 Otherwise filenames are omitted."
217 (defcustom makefile-cleanup-continuations nil
218 "*If non-nil, automatically clean up continuation lines when saving.
219 A line is cleaned up by removing all whitespace following a trailing
220 backslash. This is done silently.
221 IMPORTANT: Please note that enabling this option causes Makefile mode
222 to MODIFY A FILE WITHOUT YOUR CONFIRMATION when \"it seems necessary\"."
226 (defcustom makefile-mode-hook nil
227 "*Normal hook run by `makefile-mode'."
231 (defvar makefile-browser-hook
'())
234 ;; Special targets for DMake, Sun's make ...
236 (defcustom makefile-special-targets-list
237 '(("DEFAULT") ("DONE") ("ERROR") ("EXPORT")
238 ("FAILED") ("GROUPEPILOG") ("GROUPPROLOG") ("IGNORE")
239 ("IMPORT") ("INCLUDE") ("INCLUDEDIRS") ("INIT")
240 ("KEEP_STATE") ("MAKEFILES") ("MAKE_VERSION") ("NO_PARALLEL")
241 ("PARALLEL") ("PHONY") ("PRECIOUS") ("REMOVE")
242 ("SCCS_GET") ("SILENT") ("SOURCE") ("SUFFIXES")
243 ("WAIT") ("c.o") ("C.o") ("m.o")
244 ("el.elc") ("y.c") ("s.o"))
245 "*List of special targets.
246 You will be offered to complete on one of those in the minibuffer whenever
247 you enter a \".\" at the beginning of a line in `makefile-mode'."
248 :type
'(repeat (list string
))
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]*\\(:\\)\\(?:[ \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 "^ *\\([^ \n\t][^:#= \t\n]*\\)[ \t]*\\(?:!=[ \t]*\\(\\(?:.+\\\\\n\\)*.+\\)\\|[*:+]?[:?]?=[ \t]*\\(\\(?:.*\\\\\n\\)*.*\\)\\)"
295 "Regex used to find macro assignment lines in a makefile.")
297 (defconst makefile-var-use-regex
298 "[^$]\\$[({]\\([-a-zA-Z0-9_.]+\\|[@%<?^+*][FD]?\\)"
299 "Regex used to find $(macro) uses in a makefile.")
301 (defconst makefile-ignored-files-in-pickup-regex
302 "\\(^\\..*\\)\\|\\(.*~$\\)\\|\\(.*,v$\\)\\|\\(\\.[chy]\\)"
303 "Regex for filenames that will NOT be included in the target list.")
305 (if (fboundp 'facemenu-unlisted-faces
)
306 (add-to-list 'facemenu-unlisted-faces
'makefile-space-face
))
307 (defvar makefile-space-face
'makefile-space-face
308 "Face to use for highlighting leading spaces in Font-Lock mode.")
310 ;; These lists were inspired by the old solution. But they are silly, because
311 ;; you can't differentiate what follows. They need to be split up.
312 (defconst makefile-statements
'("include")
313 "List of keywords understood by standard make.")
315 (defconst makefile-automake-statements
316 `("if" "else" "endif" ,@makefile-statements
)
317 "List of keywords understood by automake.")
319 (defconst makefile-gmake-statements
320 `("-sinclude" "sinclude" "override" "vpath"
321 "ifdef" "ifndef" "ifeq" "ifneq" "-include" "define" "endef" "export"
323 ,@(cdr makefile-automake-statements
))
324 "List of keywords understood by gmake.")
326 ;; These are even more silly, because you can have more spaces in between.
327 (defconst makefile-makepp-statements
328 `("and ifdef" "and ifndef" "and ifeq" "and ifneq" "and ifperl"
329 "and ifmakeperl" "and ifsys" "and ifnsys" "build_cache" "build_check"
330 "else ifdef" "else ifndef" "else ifeq" "else ifneq" "else ifperl"
331 "else ifmakeperl" "else ifsys" "else ifnsys" "enddef" "load_makefile"
332 "ifperl" "ifmakeperl" "ifsys" "ifnsys" "_include" "makeperl" "makesub"
333 "no_implicit_load" "perl" "perl-begin" "perl_begin" "perl-end" "perl_end"
334 "prebuild" "or ifdef" "or ifndef" "or ifeq" "or ifneq" "or ifperl"
335 "or ifmakeperl" "or ifsys" "or ifnsys" "register_command_parser"
336 "register_scanner" "repository" "runtime" "signature" "sub"
337 ,@(nthcdr 4 makefile-gmake-statements
))
338 "List of keywords understood by gmake.")
340 (defconst makefile-bsdmake-statements
341 `(".elif" ".elifdef" ".elifmake" ".elifndef" ".elifnmake" ".else" ".endfor"
342 ".endif" ".for" ".if" ".ifdef" ".ifmake" ".ifndef" ".ifnmake" ".undef")
343 "List of keywords understood by BSD make.")
345 (defun makefile-make-font-lock-keywords (var keywords space
347 &rest font-lock-keywords
)
348 `(;; Do macro assignments. These get the "variable-name" face.
349 (,makefile-macroassign-regex
350 (1 font-lock-variable-name-face
)
351 ;; This is for after !=
352 (2 'makefile-shell-face prepend t
)
353 ;; This is for after normal assignment
354 (3 'font-lock-string-face prepend t
))
357 (makefile-match-action
358 (1 font-lock-type-face
)
359 (2 'makefile-shell-face prepend
)
360 ;; Only makepp has builtin commands.
361 (3 font-lock-builtin-face prepend t
))
363 ;; Variable references even in targets/strings/comments.
364 (,var
1 font-lock-variable-name-face prepend
)
366 ;; Automatic variable references and single character variable references,
367 ;; but not shell variables references.
368 ("[^$]\\$\\([@%<?^+*_]\\|[a-zA-Z0-9]\\>\\)"
369 1 font-lock-constant-face prepend
)
370 ("[^$]\\(\\$[@%*]\\)"
371 1 'makefile-targets-face prepend
)
373 ;; Fontify conditionals and includes.
374 (,(concat "^\\(?: [ \t]*\\)?"
375 (regexp-opt keywords t
)
376 "\\>[ \t]*\\([^: \t\n#]*\\)")
377 (1 font-lock-keyword-face
) (2 font-lock-variable-name-face
))
380 `((,negation
(1 font-lock-negation-char-face prepend
)
381 (2 font-lock-negation-char-face prepend t
))))
384 '(;; Highlight lines that contain just whitespace.
385 ;; They can cause trouble, especially if they start with a tab.
386 ("^[ \t]+$" . makefile-space-face
)
388 ;; Highlight shell comments that Make treats as commands,
389 ;; since these can fool people.
390 ("^\t+#" 0 makefile-space-face t
)
392 ;; Highlight spaces that precede tabs.
393 ;; They can make a tab fail to be effective.
394 ("^\\( +\\)\t" 1 makefile-space-face
)))
399 (makefile-match-dependency
400 (1 'makefile-targets-face prepend
)
401 (3 'makefile-shell-face prepend t
))))
403 (defconst makefile-font-lock-keywords
404 (makefile-make-font-lock-keywords
405 makefile-var-use-regex
409 (defconst makefile-automake-font-lock-keywords
410 (makefile-make-font-lock-keywords
411 makefile-var-use-regex
412 makefile-automake-statements
415 (defconst makefile-gmake-font-lock-keywords
416 (makefile-make-font-lock-keywords
417 makefile-var-use-regex
418 makefile-gmake-statements
420 "^\\(?: [ \t]*\\)?if\\(n\\)\\(?:def\\|eq\\)\\>"
422 '("[^$]\\(\\$[({][@%*][DF][})]\\)"
423 1 'makefile-targets-face prepend
)
425 ;; $(function ...) ${function ...}
426 '("[^$]\\$[({]\\([-a-zA-Z0-9_.]+\\s \\)"
427 1 font-lock-function-name-face prepend
)
429 ;; $(shell ...) ${shell ...}
430 '("[^$]\\$\\([({]\\)shell[ \t]+"
431 makefile-match-function-end nil nil
432 (1 'makefile-shell-face prepend t
))))
434 (defconst makefile-makepp-font-lock-keywords
435 (makefile-make-font-lock-keywords
436 makefile-var-use-regex
437 makefile-makepp-statements
439 "^\\(?: [ \t]*\\)?\\(?:and[ \t]+\\|else[ \t]+\\|or[ \t]+\\)?if\\(n\\)\\(?:def\\|eq\\|sys\\)\\>"
441 '("[^$]\\(\\$[({]\\(?:output\\|stem\\|target\\)s?\\_>.*?[})]\\)"
442 1 'makefile-targets-face prepend
)
444 ;; Colon modifier keywords.
445 '("\\(:\\s *\\)\\(build_c\\(?:ache\\|heck\\)\\|env\\(?:ironment\\)?\\|foreach\\|signature\\|scanner\\|quickscan\\|smartscan\\)\\>\\([^:\n]*\\)"
446 (1 font-lock-type-face t
)
447 (2 font-lock-keyword-face t
)
448 (3 font-lock-variable-name-face t
))
450 ;; $(function ...) $((function ...)) ${function ...} ${{function ...}}
451 '("[^$]\\$\\(?:((?\\|{{?\\)\\([-a-zA-Z0-9_.]+\\s \\)"
452 1 font-lock-function-name-face prepend
)
454 ;; $(shell ...) $((shell ...)) ${shell ...} ${{shell ...}}
455 '("[^$]\\$\\(((?\\|{{?\\)shell\\(?:[-_]\\(?:global[-_]\\)?once\\)?[ \t]+"
456 makefile-match-function-end nil nil
457 (1 'makefile-shell-face prepend t
))
459 ;; $(perl ...) $((perl ...)) ${perl ...} ${{perl ...}}
460 '("[^$]\\$\\(((?\\|{{?\\)makeperl[ \t]+"
461 makefile-match-function-end nil nil
462 (1 'makefile-makepp-perl-face prepend t
))
463 '("[^$]\\$\\(((?\\|{{?\\)perl[ \t]+"
464 makefile-match-function-end nil nil
465 (1 'makefile-makepp-perl-face t t
))
467 ;; Can we unify these with (if (match-end 1) 'prepend t)?
468 '("ifmakeperl\\s +\\(.*\\)" 1 'makefile-makepp-perl-face prepend
)
469 '("ifperl\\s +\\(.*\\)" 1 'makefile-makepp-perl-face t
)
471 ;; Perl block single- or multiline, as statement or rule action.
472 ;; Don't know why the initial newline in 2nd variant of group 2 doesn't get skipped.
473 '("\\<make\\(?:perl\\|sub\\s +\\S +\\)\\s *\n?\\s *{\\(?:{\\s *\n?\\(\\(?:.*\n\\)+?\\)\\s *}\\|\\s *\\(\\(?:.*?\\|\n?\\(?:.*\n\\)+?\\)\\)\\)}"
474 (1 'makefile-makepp-perl-face prepend t
)
475 (2 'makefile-makepp-perl-face prepend t
))
476 '("\\<\\(?:perl\\|sub\\s +\\S +\\)\\s *\n?\\s *{\\(?:{\\s *\n?\\(\\(?:.*\n\\)+?\\)\\s *}\\|\\s *\\(\\(?:.*?\\|\n?\\(?:.*\n\\)+?\\)\\)\\)}"
477 (1 'makefile-makepp-perl-face t t
)
478 (2 'makefile-makepp-perl-face t t
))
480 ;; Statement style perl block.
481 '("perl[-_]begin\\s *\\(?:\\s #.*\\)?\n\\(\\(?:.*\n\\)+?\\)\\s *perl[-_]end\\>"
482 1 'makefile-makepp-perl-face t
)))
484 (defconst makefile-bsdmake-font-lock-keywords
485 (makefile-make-font-lock-keywords
486 ;; A lot more could be done for variables here:
487 makefile-var-use-regex
488 makefile-bsdmake-statements
490 "^\\(?: [ \t]*\\)?\\.\\(?:el\\)?if\\(n?\\)\\(?:def\\|make\\)?\\>[ \t]*\\(!?\\)"
491 '("^[ \t]*\\.for[ \t].+[ \t]\\(in\\)\\>" 1 font-lock-keyword-face
)))
494 (defconst makefile-font-lock-syntactic-keywords
495 ;; From sh-script.el.
496 ;; A `#' begins a comment in sh when it is unquoted and at the beginning
497 ;; of a word. In the shell, words are separated by metacharacters.
498 ;; The list of special chars is taken from the single-unix spec of the
499 ;; shell command language (under `quoting') but with `$' removed.
500 '(("[^|&;<>()`\\\"' \t\n]\\(#+\\)" 1 "_")
501 ;; Change the syntax of a quoted newline so that it does not end a comment.
504 (defvar makefile-imenu-generic-expression
505 `(("Dependencies" makefile-previous-dependency
1)
506 ("Macro Assignment" ,makefile-macroassign-regex
1))
507 "Imenu generic expression for Makefile mode. See `imenu-generic-expression'.")
509 ;;; ------------------------------------------------------------
510 ;;; The following configurable variables are used in the
511 ;;; up-to-date overview .
512 ;;; The standard configuration assumes that your `make' program
513 ;;; can be run in question/query mode using the `-q' option, this
514 ;;; means that the command
518 ;;; should return an exit status of zero if the target `foo' is
519 ;;; up to date and a nonzero exit status otherwise.
520 ;;; Many makes can do this although the docs/manpages do not mention
521 ;;; it. Try it with your favourite one. GNU make, System V make, and
522 ;;; Dennis Vadura's DMake have no problems.
523 ;;; Set the variable `makefile-brave-make' to the name of the
524 ;;; make utility that does this on your system.
525 ;;; To understand what this is all about see the function definition
526 ;;; of `makefile-query-by-make-minus-q' .
527 ;;; ------------------------------------------------------------
529 (defcustom makefile-brave-make
"make"
530 "*How to invoke make, for `makefile-query-targets'.
531 This should identify a `make' command that can handle the `-q' option."
535 (defcustom makefile-query-one-target-method
'makefile-query-by-make-minus-q
536 "*Function to call to determine whether a make target is up to date.
537 The function must satisfy this calling convention:
539 * As its first argument, it must accept the name of the target to
540 be checked, as a string.
542 * As its second argument, it may accept the name of a makefile
543 as a string. Depending on what you're going to do you may
546 * It must return the integer value 0 (zero) if the given target
547 should be considered up-to-date in the context of the given
548 makefile, any nonzero integer value otherwise."
552 (defcustom makefile-up-to-date-buffer-name
"*Makefile Up-to-date overview*"
553 "*Name of the Up-to-date overview buffer."
557 ;;; --- end of up-to-date-overview configuration ------------------
559 (defvar makefile-mode-abbrev-table nil
560 "Abbrev table in use in Makefile buffers.")
561 (if makefile-mode-abbrev-table
563 (define-abbrev-table 'makefile-mode-abbrev-table
()))
565 (defvar makefile-mode-map
566 (let ((map (make-sparse-keymap)))
568 (define-key map
"\C-c:" 'makefile-insert-target-ref
)
569 (if makefile-electric-keys
571 (define-key map
"$" 'makefile-insert-macro-ref
)
572 (define-key map
":" 'makefile-electric-colon
)
573 (define-key map
"=" 'makefile-electric-equal
)
574 (define-key map
"." 'makefile-electric-dot
)))
575 (define-key map
"\C-c\C-f" 'makefile-pickup-filenames-as-targets
)
576 (define-key map
"\C-c\C-b" 'makefile-switch-to-browser
)
577 (define-key map
"\C-c\C-c" 'comment-region
)
578 (define-key map
"\C-c\C-p" 'makefile-pickup-everything
)
579 (define-key map
"\C-c\C-u" 'makefile-create-up-to-date-overview
)
580 (define-key map
"\C-c\C-i" 'makefile-insert-gmake-function
)
581 (define-key map
"\C-c\C-\\" 'makefile-backslash-region
)
582 (define-key map
"\C-c\C-m\C-a" 'makefile-automake-mode
)
583 (define-key map
"\C-c\C-m\C-b" 'makefile-bsdmake-mode
)
584 (define-key map
"\C-c\C-m\C-g" 'makefile-gmake-mode
)
585 (define-key map
"\C-c\C-m\C-m" 'makefile-mode
)
586 (define-key map
"\C-c\C-m\C-p" 'makefile-makepp-mode
)
587 (define-key map
"\M-p" 'makefile-previous-dependency
)
588 (define-key map
"\M-n" 'makefile-next-dependency
)
589 (define-key map
"\e\t" 'makefile-complete
)
592 (define-key map
[menu-bar makefile-mode
]
593 (cons "Makefile" (make-sparse-keymap "Makefile")))
595 (define-key map
[menu-bar makefile-mode browse
]
596 '("Pop up Makefile Browser" . makefile-switch-to-browser
))
597 (define-key map
[menu-bar makefile-mode complete
]
598 '("Complete Target or Macro" . makefile-complete
))
599 (define-key map
[menu-bar makefile-mode pickup
]
600 '("Find Targets and Macros" . makefile-pickup-everything
))
602 (define-key map
[menu-bar makefile-mode prev
]
603 '("Move to Previous Dependency" . makefile-previous-dependency
))
604 (define-key map
[menu-bar makefile-mode next
]
605 '("Move to Next Dependency" . makefile-next-dependency
))
607 "The keymap that is used in Makefile mode.")
609 (defvar makefile-browser-map nil
610 "The keymap that is used in the macro- and target browser.")
611 (if makefile-browser-map
613 (setq makefile-browser-map
(make-sparse-keymap))
614 (define-key makefile-browser-map
"n" 'makefile-browser-next-line
)
615 (define-key makefile-browser-map
"\C-n" 'makefile-browser-next-line
)
616 (define-key makefile-browser-map
"p" 'makefile-browser-previous-line
)
617 (define-key makefile-browser-map
"\C-p" 'makefile-browser-previous-line
)
618 (define-key makefile-browser-map
" " 'makefile-browser-toggle
)
619 (define-key makefile-browser-map
"i" 'makefile-browser-insert-selection
)
620 (define-key makefile-browser-map
"I" 'makefile-browser-insert-selection-and-quit
)
621 (define-key makefile-browser-map
"\C-c\C-m" 'makefile-browser-insert-continuation
)
622 (define-key makefile-browser-map
"q" 'makefile-browser-quit
)
623 ;; disable horizontal movement
624 (define-key makefile-browser-map
"\C-b" 'undefined
)
625 (define-key makefile-browser-map
"\C-f" 'undefined
))
628 (defvar makefile-mode-syntax-table nil
)
629 (if makefile-mode-syntax-table
631 (setq makefile-mode-syntax-table
(make-syntax-table))
632 (modify-syntax-entry ?\
( "() " makefile-mode-syntax-table
)
633 (modify-syntax-entry ?\
) ")( " makefile-mode-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 ?
\n "> " makefile-mode-syntax-table
))
644 ;;; ------------------------------------------------------------
645 ;;; Internal variables.
646 ;;; You don't need to configure below this line.
647 ;;; ------------------------------------------------------------
649 (defvar makefile-target-table nil
650 "Table of all target names known for this buffer.")
652 (defvar makefile-macro-table nil
653 "Table of all macro names known for this buffer.")
655 (defvar makefile-browser-client
656 "A buffer in Makefile mode that is currently using the browser.")
658 (defvar makefile-browser-selection-vector nil
)
659 (defvar makefile-has-prereqs nil
)
660 (defvar makefile-need-target-pickup t
)
661 (defvar makefile-need-macro-pickup t
)
663 (defvar makefile-mode-hook
'())
665 ;; Each element looks like '("GNU MAKE FUNCTION" "ARG" "ARG" ... )
666 ;; Each "ARG" is used as a prompt for a required argument.
667 (defconst makefile-gnumake-functions-alist
670 ("subst" "From" "To" "In")
671 ("patsubst" "Pattern" "Replacement" "In")
673 ("findstring" "Find what" "In")
674 ("filter" "Pattern" "Text")
675 ("filter-out" "Pattern" "Text")
677 ;; Filename functions
682 ("addprefix" "Prefix" "Names")
683 ("addsuffix" "Suffix" "Names")
684 ("join" "List 1" "List 2")
685 ("word" "Index" "Text")
688 ("wildcard" "Pattern")
690 ("foreach" "Variable" "List" "Text")
691 ("origin" "Variable")
692 ("shell" "Command")))
695 ;;; ------------------------------------------------------------
696 ;;; The mode function itself.
697 ;;; ------------------------------------------------------------
700 (defun makefile-mode ()
701 "Major mode for editing standard Makefiles.
703 If you are editing a file for a different make, try one of the
704 variants `makefile-automake-mode', `makefile-gmake-mode',
705 `makefile-makepp-mode' or `makefile-bsdmake-mode'. All but the
706 last should be correctly chosen based on the file name, except if
707 it is *.mk. This function ends by invoking the function(s)
708 `makefile-mode-hook'.
710 It is strongly recommended to use `font-lock-mode', because that
711 provides additional parsing information. This is used for
712 example to see that a rule action `echo foo: bar' is a not rule
713 dependency, despite the colon.
715 \\{makefile-mode-map}
717 In the browser, use the following keys:
719 \\{makefile-browser-map}
721 Makefile mode can be configured by modifying the following variables:
723 `makefile-browser-buffer-name':
724 Name of the macro- and target browser buffer.
726 `makefile-target-colon':
727 The string that gets appended to all target names
728 inserted by `makefile-insert-target'.
729 \":\" or \"::\" are quite common values.
731 `makefile-macro-assign':
732 The string that gets appended to all macro names
733 inserted by `makefile-insert-macro'.
734 The normal value should be \" = \", since this is what
735 standard make expects. However, newer makes such as dmake
736 allow a larger variety of different macro assignments, so you
737 might prefer to use \" += \" or \" := \" .
739 `makefile-tab-after-target-colon':
740 If you want a TAB (instead of a space) to be appended after the
741 target colon, then set this to a non-nil value.
743 `makefile-browser-leftmost-column':
744 Number of blanks to the left of the browser selection mark.
746 `makefile-browser-cursor-column':
747 Column in which the cursor is positioned when it moves
748 up or down in the browser.
750 `makefile-browser-selected-mark':
751 String used to mark selected entries in the browser.
753 `makefile-browser-unselected-mark':
754 String used to mark unselected entries in the browser.
756 `makefile-browser-auto-advance-after-selection-p':
757 If this variable is set to a non-nil value the cursor
758 will automagically advance to the next line after an item
759 has been selected in the browser.
761 `makefile-pickup-everything-picks-up-filenames-p':
762 If this variable is set to a non-nil value then
763 `makefile-pickup-everything' also picks up filenames as targets
764 (i.e. it calls `makefile-pickup-filenames-as-targets'), otherwise
765 filenames are omitted.
767 `makefile-cleanup-continuations':
768 If this variable is set to a non-nil value then Makefile mode
769 will assure that no line in the file ends with a backslash
770 (the continuation character) followed by any whitespace.
771 This is done by silently removing the trailing whitespace, leaving
772 the backslash itself intact.
773 IMPORTANT: Please note that enabling this option causes Makefile mode
774 to MODIFY A FILE WITHOUT YOUR CONFIRMATION when \"it seems necessary\".
776 `makefile-browser-hook':
777 A function or list of functions to be called just before the
778 browser is entered. This is executed in the makefile buffer.
780 `makefile-special-targets-list':
781 List of special targets. You will be offered to complete
782 on one of those in the minibuffer whenever you enter a `.'.
783 at the beginning of a line in Makefile mode."
786 (kill-all-local-variables)
787 (add-hook 'write-file-functions
788 'makefile-warn-suspicious-lines nil t
)
789 (add-hook 'write-file-functions
790 'makefile-warn-continuations nil t
)
791 (add-hook 'write-file-functions
792 'makefile-cleanup-continuations nil t
)
793 (make-local-variable 'makefile-target-table
)
794 (make-local-variable 'makefile-macro-table
)
795 (make-local-variable 'makefile-has-prereqs
)
796 (make-local-variable 'makefile-need-target-pickup
)
797 (make-local-variable 'makefile-need-macro-pickup
)
800 (make-local-variable 'font-lock-defaults
)
801 (setq font-lock-defaults
802 ;; SYNTAX-BEGIN set to backward-paragraph to avoid slow-down
803 ;; near the end of a large buffer, due to parse-partial-sexp's
804 ;; trying to parse all the way till the beginning of buffer.
805 '(makefile-font-lock-keywords
809 (font-lock-syntactic-keywords . makefile-font-lock-syntactic-keywords
)
810 (font-lock-support-mode))) ; JIT breaks on long series of continuation lines.
813 (make-local-variable 'add-log-current-defun-function
)
814 (setq add-log-current-defun-function
'makefile-add-log-defun
)
817 (make-local-variable 'imenu-generic-expression
)
818 (setq imenu-generic-expression makefile-imenu-generic-expression
)
821 (make-local-variable 'dabbrev-abbrev-skip-leading-regexp
)
822 (setq dabbrev-abbrev-skip-leading-regexp
"\\$")
825 (setq local-abbrev-table makefile-mode-abbrev-table
)
828 (make-local-variable 'fill-paragraph-function
)
829 (setq fill-paragraph-function
'makefile-fill-paragraph
)
832 (make-local-variable 'comment-start
)
833 (setq comment-start
"#")
834 (make-local-variable 'comment-end
)
835 (setq comment-end
"")
836 (make-local-variable 'comment-start-skip
)
837 (setq comment-start-skip
"#+[ \t]*")
839 ;; Make sure TAB really inserts \t.
840 (set (make-local-variable 'indent-line-function
) 'indent-to-left-margin
)
842 ;; become the current major mode
843 (setq major-mode
'makefile-mode
)
844 (setq mode-name
"Makefile")
846 ;; Activate keymap and syntax table.
847 (use-local-map makefile-mode-map
)
848 (set-syntax-table makefile-mode-syntax-table
)
850 ;; Real TABs are important in makefiles
851 (setq indent-tabs-mode t
)
852 (run-mode-hooks 'makefile-mode-hook
))
854 ;; These should do more than just differentiate font-lock.
856 (define-derived-mode makefile-automake-mode makefile-mode
"Makefile.am"
857 "An adapted `makefile-mode' that knows about automake."
858 (setq font-lock-defaults
859 `(makefile-automake-font-lock-keywords ,@(cdr font-lock-defaults
))))
862 (define-derived-mode makefile-gmake-mode makefile-mode
"GNUmakefile"
863 "An adapted `makefile-mode' that knows about gmake."
864 (setq font-lock-defaults
865 `(makefile-gmake-font-lock-keywords ,@(cdr font-lock-defaults
))))
868 (define-derived-mode makefile-makepp-mode makefile-mode
"Makeppfile"
869 "An adapted `makefile-mode' that knows about makepp."
870 (set (make-local-variable 'makefile-rule-action-regex
)
871 makefile-makepp-rule-action-regex
)
872 (setq font-lock-defaults
873 `(makefile-makepp-font-lock-keywords ,@(cdr font-lock-defaults
))
874 imenu-generic-expression
875 `(("Functions" "^[ \t]*\\(?:make\\)?sub[ \t]+\\([A-Za-z0-9_]+\\)" 1)
876 ,@imenu-generic-expression
)))
879 (define-derived-mode makefile-bsdmake-mode makefile-mode
"BSDmakefile"
880 "An adapted `makefile-mode' that knows about BSD make."
881 (set (make-local-variable 'makefile-dependency-regex
)
882 makefile-bsdmake-dependency-regex
)
883 (set (make-local-variable 'makefile-dependency-skip
) "^:!")
884 (set (make-local-variable 'makefile-rule-action-regex
)
885 makefile-bsdmake-rule-action-regex
)
886 (setq font-lock-defaults
887 `(makefile-bsdmake-font-lock-keywords ,@(cdr font-lock-defaults
))))
893 (defun makefile-next-dependency ()
894 "Move point to the beginning of the next dependency line."
896 (let ((here (point)))
898 (if (makefile-match-dependency nil
)
899 (progn (beginning-of-line) t
) ; indicate success
900 (goto-char here
) nil
)))
902 (defun makefile-previous-dependency ()
903 "Move point to the beginning of the previous dependency line."
907 ;; makefile-match-dependency done backwards:
909 (while (progn (skip-chars-backward makefile-dependency-skip
)
911 (or (prog1 (eq (char-after) ?
=)
913 (get-text-property (point) 'face
)
915 (if (looking-at makefile-dependency-regex
)
922 ;;; Electric keys. Blech.
924 (defun makefile-electric-dot (arg)
925 "Prompt for the name of a special target to insert.
926 Only does electric insertion at beginning of line.
927 Anywhere else just self-inserts."
930 (makefile-insert-special-target)
931 (self-insert-command arg
)))
933 (defun makefile-insert-special-target ()
934 "Prompt for and insert a special target name.
935 Uses `makefile-special-targets' list."
937 (makefile-pickup-targets)
938 (let ((special-target
939 (completing-read "Special target: "
940 makefile-special-targets-list nil nil nil
)))
941 (if (zerop (length special-target
))
943 (insert "." special-target
":")
944 (makefile-forward-after-target-colon))))
946 (defun makefile-electric-equal (arg)
947 "Prompt for name of a macro to insert.
948 Only does prompting if point is at beginning of line.
949 Anywhere else just self-inserts."
951 (makefile-pickup-macros)
953 (call-interactively 'makefile-insert-macro
)
954 (self-insert-command arg
)))
956 (defun makefile-insert-macro (macro-name)
957 "Prepare definition of a new macro."
958 (interactive "sMacro Name: ")
959 (makefile-pickup-macros)
960 (if (not (zerop (length macro-name
)))
963 (insert macro-name makefile-macro-assign
)
964 (setq makefile-need-macro-pickup t
)
965 (makefile-remember-macro macro-name
))))
967 (defun makefile-insert-macro-ref (macro-name)
968 "Complete on a list of known macros, then insert complete ref at point."
972 (makefile-pickup-macros)
973 (completing-read "Refer to macro: " makefile-macro-table nil nil nil
))))
974 (makefile-do-macro-insertion macro-name
))
976 (defun makefile-insert-target (target-name)
977 "Prepare definition of a new target (dependency line)."
978 (interactive "sTarget: ")
979 (if (not (zerop (length target-name
)))
982 (insert target-name makefile-target-colon
)
983 (makefile-forward-after-target-colon)
985 (setq makefile-need-target-pickup t
)
986 (makefile-remember-target target-name
))))
988 (defun makefile-insert-target-ref (target-name)
989 "Complete on a list of known targets, then insert TARGET-NAME at point."
993 (makefile-pickup-targets)
994 (completing-read "Refer to target: " makefile-target-table nil nil nil
))))
995 (if (not (zerop (length target-name
)))
996 (insert target-name
" ")))
998 (defun makefile-electric-colon (arg)
999 "Prompt for name of new target.
1000 Prompting only happens at beginning of line.
1001 Anywhere else just self-inserts."
1004 (call-interactively 'makefile-insert-target
)
1005 (self-insert-command arg
)))
1009 ;;; ------------------------------------------------------------
1010 ;;; Extracting targets and macros from an existing makefile
1011 ;;; ------------------------------------------------------------
1013 (defun makefile-pickup-targets ()
1014 "Notice names of all target definitions in Makefile."
1016 (when makefile-need-target-pickup
1017 (setq makefile-need-target-pickup nil
1018 makefile-target-table nil
1019 makefile-has-prereqs nil
)
1021 (goto-char (point-min))
1022 (while (makefile-match-dependency nil
)
1023 (goto-char (match-beginning 1))
1024 (while (let ((target-name
1025 (buffer-substring-no-properties (point)
1027 (skip-chars-forward "^ \t:#")
1030 (not (looking-at ":[ \t]*$"))))
1031 (if (makefile-remember-target target-name has-prereqs
)
1032 (message "Picked up target \"%s\" from line %d"
1033 target-name
(line-number-at-pos)))
1034 (skip-chars-forward " \t")
1035 (not (or (eolp) (eq (char-after) ?
:)))))
1037 (message "Read targets OK.")))
1039 (defun makefile-pickup-macros ()
1040 "Notice names of all macro definitions in Makefile."
1042 (when makefile-need-macro-pickup
1043 (setq makefile-need-macro-pickup nil
1044 makefile-macro-table nil
)
1046 (goto-char (point-min))
1047 (while (re-search-forward makefile-macroassign-regex nil t
)
1048 (goto-char (match-beginning 1))
1049 (let ((macro-name (buffer-substring-no-properties (point)
1051 (skip-chars-forward "^ \t:#=*")
1053 (if (makefile-remember-macro macro-name
)
1054 (message "Picked up macro \"%s\" from line %d"
1055 macro-name
(line-number-at-pos))))
1057 (message "Read macros OK.")))
1059 (defun makefile-pickup-everything (arg)
1060 "Notice names of all macros and targets in Makefile.
1061 Prefix arg means force pickups to be redone."
1064 (setq makefile-need-target-pickup t
1065 makefile-need-macro-pickup t
))
1066 (makefile-pickup-macros)
1067 (makefile-pickup-targets)
1068 (if makefile-pickup-everything-picks-up-filenames-p
1069 (makefile-pickup-filenames-as-targets)))
1071 (defun makefile-pickup-filenames-as-targets ()
1072 "Scan the current directory for filenames to use as targets.
1073 Checks each filename against `makefile-ignored-files-in-pickup-regex'
1074 and adds all qualifying names to the list of known targets."
1076 (mapc (lambda (name)
1077 (or (file-directory-p name
)
1078 (string-match makefile-ignored-files-in-pickup-regex name
)
1079 (if (makefile-remember-target name
)
1080 (message "Picked up file \"%s\" as target" name
))))
1081 (file-name-all-completions "" (or (file-name-directory (buffer-file-name)) ""))))
1087 (defun makefile-complete ()
1088 "Perform completion on Makefile construct preceding point.
1089 Can complete variable and target names.
1090 The context determines which are considered."
1092 (let* ((beg (save-excursion
1093 (skip-chars-backward "^$(){}:#= \t\n")
1095 (try (buffer-substring beg
(point)))
1101 (let ((pc (preceding-char)))
1103 ;; Beginning of line means anything.
1107 ;; Preceding "$" means macros only.
1111 ;; Preceding "$(" or "${" means macros only.
1118 (= (preceding-char) ?$
))))
1119 (setq do-macros t
)))))
1122 (let* ((table (append (if do-macros
1124 makefile-target-table
)
1125 makefile-macro-table
))
1126 (completion (try-completion try table
)))
1128 ;; Exact match, so insert closing paren or colon.
1130 (insert (if do-macros
1142 (message "Can't find completion for \"%s\"" try
)
1145 ;; Partial completion.
1146 ((not (string= try completion
))
1147 ;; FIXME it would be nice to supply the closing paren if an
1148 ;; exact, unambiguous match were found. That is not possible
1149 ;; right now. Ditto closing ":" for targets.
1150 (delete-region beg
(point))
1152 ;; DO-MACROS means doing macros only. If not that, then check
1153 ;; to see if this completion is a macro. Special insertion
1154 ;; must be done for macros.
1156 (assoc completion makefile-macro-table
))
1157 (let ((makefile-use-curly-braces-for-macros-p
1159 makefile-use-curly-braces-for-macros-p
)))
1160 (delete-backward-char 2)
1161 (makefile-do-macro-insertion completion
)
1162 (delete-backward-char 1))
1164 ;; Just insert targets.
1165 (insert completion
)))
1167 ;; Can't complete any more, so make completion list. FIXME
1168 ;; this doesn't do the right thing when the completion is
1169 ;; actually inserted. I don't think there is an easy way to do
1172 (message "Making completion list...")
1173 (let ((list (all-completions try table
)))
1174 (with-output-to-temp-buffer "*Completions*"
1175 (display-completion-list list
)))
1176 (message "Making completion list...done"))))))
1180 ;; Backslashification. Stolen from cc-mode.el.
1182 (defun makefile-backslash-region (from to delete-flag
)
1183 "Insert, align, or delete end-of-line backslashes on the lines in the region.
1184 With no argument, inserts backslashes and aligns existing backslashes.
1185 With an argument, deletes the backslashes.
1187 This function does not modify the last line of the region if the region ends
1188 right at the start of the following line; it does not modify blank lines
1189 at the start of the region. So you can put the region around an entire macro
1190 definition and conveniently use this command."
1191 (interactive "r\nP")
1194 (let ((column makefile-backslash-column
)
1195 (endmark (make-marker)))
1196 (move-marker endmark to
)
1197 ;; Compute the smallest column number past the ends of all the lines.
1198 (if makefile-backslash-align
1200 (if (not delete-flag
)
1201 (while (< (point) to
)
1203 (if (= (preceding-char) ?
\\)
1204 (progn (forward-char -
1)
1205 (skip-chars-backward " \t")))
1206 (setq column
(max column
(1+ (current-column))))
1208 ;; Adjust upward to a tab column, if that doesn't push
1210 (if (> (% column tab-width
) 0)
1211 (let ((adjusted (* (/ (+ column tab-width -
1) tab-width
)
1213 (if (< adjusted
(window-width))
1214 (setq column adjusted
))))))
1215 ;; Don't modify blank lines at start of region.
1217 (while (and (< (point) endmark
) (eolp))
1219 ;; Add or remove backslashes on all the lines.
1220 (while (and (< (point) endmark
)
1221 ;; Don't backslashify the last line
1222 ;; if the region ends right at the start of the next line.
1225 (< (point) endmark
)))
1226 (if (not delete-flag
)
1227 (makefile-append-backslash column
)
1228 (makefile-delete-backslash))
1230 (move-marker endmark nil
))))
1232 (defun makefile-append-backslash (column)
1234 ;; Note that "\\\\" is needed to get one backslash.
1235 (if (= (preceding-char) ?
\\)
1236 (progn (forward-char -
1)
1237 (delete-horizontal-space)
1238 (indent-to column
(if makefile-backslash-align nil
1)))
1239 (indent-to column
(if makefile-backslash-align nil
1))
1242 (defun makefile-delete-backslash ()
1247 (if (looking-at "\\\\")
1248 (delete-region (1+ (point))
1249 (progn (skip-chars-backward " \t") (point)))))))
1255 (defun makefile-fill-paragraph (arg)
1256 ;; Fill comments, backslashed lines, and variable definitions
1262 ;; Found a comment. Set the fill prefix, and find the paragraph
1263 ;; boundaries by searching for lines that look like comment-only
1265 (let ((fill-prefix (match-string-no-properties 0))
1266 (fill-paragraph-function nil
))
1270 ;; Search backwards.
1272 (while (and (zerop (forward-line -
1))
1274 ;; We may have gone too far. Go forward again.
1275 (or (looking-at "^#")
1280 (while (looking-at "^#")
1283 (fill-paragraph nil
)
1286 ;; Must look for backslashed-region before looking for variable
1288 ((or (eq (char-before (line-end-position 1)) ?
\\)
1289 (eq (char-before (line-end-position 0)) ?
\\))
1290 ;; A backslash region. Find beginning and end, remove
1291 ;; backslashes, fill, and then reapply backslahes.
1296 (while (= (preceding-char) ?
\\)
1302 (while (= (preceding-char) ?
\\)
1306 (narrow-to-region beginning end
)
1307 (makefile-backslash-region (point-min) (point-max) t
)
1308 (let ((fill-paragraph-function nil
))
1309 (fill-paragraph nil
))
1310 (makefile-backslash-region (point-min) (point-max) nil
)
1311 (goto-char (point-max))
1312 (if (< (skip-chars-backward "\n") 0)
1313 (delete-region (point) (point-max))))))
1315 ((looking-at makefile-macroassign-regex
)
1316 ;; Have a macro assign. Fill just this line, and then backslash
1317 ;; resulting region.
1319 (narrow-to-region (point) (line-beginning-position 2))
1320 (let ((fill-paragraph-function nil
))
1321 (fill-paragraph nil
))
1322 (makefile-backslash-region (point-min) (point-max) nil
)))))
1324 ;; Always return non-nil so we don't fill anything else.
1329 ;;; ------------------------------------------------------------
1331 ;;; ------------------------------------------------------------
1333 (defun makefile-browser-format-target-line (target selected
)
1335 (concat (make-string makefile-browser-leftmost-column ?\
)
1337 makefile-browser-selected-mark
1338 makefile-browser-unselected-mark
)
1340 target makefile-target-colon
))
1342 (defun makefile-browser-format-macro-line (macro selected
)
1344 (concat (make-string makefile-browser-leftmost-column ?\
)
1346 makefile-browser-selected-mark
1347 makefile-browser-unselected-mark
)
1348 (makefile-format-macro-ref macro
))))
1350 (defun makefile-browser-fill (targets macros
)
1351 (let ((inhibit-read-only t
))
1352 (goto-char (point-min))
1356 (lambda (item) (insert (makefile-browser-format-target-line (car item
) nil
) "\n")))
1361 (lambda (item) (insert (makefile-browser-format-macro-line (car item
) nil
) "\n")))
1364 (sort-lines nil
(point-min) (point-max))
1365 (goto-char (1- (point-max)))
1366 (delete-char 1) ; remove unnecessary newline at eob
1367 (goto-char (point-min))
1368 (forward-char makefile-browser-cursor-column
)))
1371 ;;; Moving up and down in the browser
1374 (defun makefile-browser-next-line ()
1375 "Move the browser selection cursor to the next line."
1377 (if (not (makefile-last-line-p))
1380 (forward-char makefile-browser-cursor-column
))))
1382 (defun makefile-browser-previous-line ()
1383 "Move the browser selection cursor to the previous line."
1385 (if (not (makefile-first-line-p))
1388 (forward-char makefile-browser-cursor-column
))))
1391 ;;; Quitting the browser (returns to client buffer)
1394 (defun makefile-browser-quit ()
1395 "Leave the browser and return to the makefile buffer."
1397 (let ((my-client makefile-browser-client
))
1398 (setq makefile-browser-client nil
) ; we quitted, so NO client!
1399 (set-buffer-modified-p nil
)
1401 (pop-to-buffer my-client
)))
1404 ;;; Toggle state of a browser item
1407 (defun makefile-browser-toggle ()
1408 "Toggle the selection state of the browser item at the cursor position."
1410 (let ((this-line (count-lines (point-min) (point))))
1411 (setq this-line
(max 1 this-line
))
1412 (makefile-browser-toggle-state-for-line this-line
)
1413 (goto-line this-line
)
1414 (let ((inhibit-read-only t
))
1416 (if (makefile-browser-on-macro-line-p)
1417 (let ((macro-name (makefile-browser-this-line-macro-name)))
1418 (delete-region (point) (progn (end-of-line) (point)))
1420 (makefile-browser-format-macro-line
1422 (makefile-browser-get-state-for-line this-line
))))
1423 (let ((target-name (makefile-browser-this-line-target-name)))
1424 (delete-region (point) (progn (end-of-line) (point)))
1426 (makefile-browser-format-target-line
1428 (makefile-browser-get-state-for-line this-line
))))))
1430 (forward-char makefile-browser-cursor-column
)
1431 (if makefile-browser-auto-advance-after-selection-p
1432 (makefile-browser-next-line))))
1435 ;;; Making insertions into the client buffer
1438 (defun makefile-browser-insert-continuation ()
1439 "Insert a makefile continuation.
1440 In the makefile buffer, go to (end-of-line), insert a \'\\\'
1441 character, insert a new blank line, go to that line and indent by one TAB.
1442 This is most useful in the process of creating continued lines when copying
1443 large dependencies from the browser to the client buffer.
1444 \(point) advances accordingly in the client buffer."
1446 (with-current-buffer makefile-browser-client
1450 (defun makefile-browser-insert-selection ()
1451 "Insert all selected targets and/or macros in the makefile buffer.
1452 Insertion takes place at point."
1456 (let ((current-line 1))
1458 (if (makefile-browser-get-state-for-line current-line
)
1459 (makefile-browser-send-this-line-item))
1461 (setq current-line
(1+ current-line
))))))
1463 (defun makefile-browser-insert-selection-and-quit ()
1465 (makefile-browser-insert-selection)
1466 (makefile-browser-quit))
1468 (defun makefile-browser-send-this-line-item ()
1469 (if (makefile-browser-on-macro-line-p)
1471 (let ((macro-name (makefile-browser-this-line-macro-name)))
1472 (set-buffer makefile-browser-client
)
1473 (insert (makefile-format-macro-ref macro-name
) " ")))
1475 (let ((target-name (makefile-browser-this-line-target-name)))
1476 (set-buffer makefile-browser-client
)
1477 (insert target-name
" ")))))
1479 (defun makefile-browser-start-interaction ()
1480 (use-local-map makefile-browser-map
)
1481 (setq buffer-read-only t
))
1483 (defun makefile-browse (targets macros
)
1485 (if (zerop (+ (length targets
) (length macros
)))
1488 (message "No macros or targets to browse! Consider running 'makefile-pickup-everything\'"))
1489 (let ((browser-buffer (get-buffer-create makefile-browser-buffer-name
)))
1490 (pop-to-buffer browser-buffer
)
1491 (makefile-browser-fill targets macros
)
1492 (shrink-window-if-larger-than-buffer)
1493 (set (make-local-variable 'makefile-browser-selection-vector
)
1494 (make-vector (+ (length targets
) (length macros
)) nil
))
1495 (makefile-browser-start-interaction))))
1497 (defun makefile-switch-to-browser ()
1499 (run-hooks 'makefile-browser-hook
)
1500 (setq makefile-browser-client
(current-buffer))
1501 (makefile-pickup-targets)
1502 (makefile-pickup-macros)
1503 (makefile-browse makefile-target-table makefile-macro-table
))
1507 ;;; ------------------------------------------------------------
1508 ;;; Up-to-date overview buffer
1509 ;;; ------------------------------------------------------------
1511 (defun makefile-create-up-to-date-overview ()
1512 "Create a buffer containing an overview of the state of all known targets.
1513 Known targets are targets that are explicitly defined in that makefile;
1514 in other words, all targets that appear on the left hand side of a
1515 dependency in the makefile."
1517 (if (y-or-n-p "Are you sure that the makefile being edited is consistent? ")
1519 ;; The rest of this function operates on a temporary makefile, created by
1520 ;; writing the current contents of the makefile buffer.
1522 (let ((saved-target-table makefile-target-table
)
1523 (this-buffer (current-buffer))
1524 (makefile-up-to-date-buffer
1525 (get-buffer-create makefile-up-to-date-buffer-name
))
1526 (filename (makefile-save-temporary))
1528 ;; Forget the target table because it may contain picked-up filenames
1529 ;; that are not really targets in the current makefile.
1530 ;; We don't want to query these, so get a new target-table with just the
1531 ;; targets that can be found in the makefile buffer.
1532 ;; The 'old' target table will be restored later.
1534 (real-targets (progn
1535 (makefile-pickup-targets)
1536 makefile-target-table
))
1537 (prereqs makefile-has-prereqs
)
1540 (set-buffer makefile-up-to-date-buffer
)
1541 (setq buffer-read-only nil
)
1543 (makefile-query-targets filename real-targets prereqs
)
1544 (if (zerop (buffer-size)) ; if it did not get us anything
1546 (kill-buffer (current-buffer))
1547 (message "No overview created!")))
1548 (set-buffer this-buffer
)
1549 (setq makefile-target-table saved-target-table
)
1550 (if (get-buffer makefile-up-to-date-buffer-name
)
1552 (pop-to-buffer (get-buffer makefile-up-to-date-buffer-name
))
1553 (shrink-window-if-larger-than-buffer)
1554 (sort-lines nil
(point-min) (point-max))
1555 (setq buffer-read-only t
))))))
1557 (defun makefile-save-temporary ()
1558 "Create a temporary file from the current makefile buffer."
1559 (let ((filename (makefile-generate-temporary-filename)))
1560 (write-region (point-min) (point-max) filename nil
0)
1561 filename
)) ; return the filename
1563 (defun makefile-generate-temporary-filename ()
1564 "Create a filename suitable for use in `makefile-save-temporary'.
1565 Be careful to allow brain-dead file systems (DOS, SYSV ...) to cope
1566 with the generated name!"
1567 (let ((my-name (user-login-name))
1568 (my-uid (int-to-string (user-uid))))
1570 (if (> (length my-name
) 3)
1571 (substring my-name
0 3)
1574 (if (> (length my-uid
) 3)
1575 (substring my-uid
0 3)
1578 (defun makefile-query-targets (filename target-table prereq-list
)
1579 "Fill the up-to-date overview buffer.
1580 Checks each target in TARGET-TABLE using `makefile-query-one-target-method'
1581 and generates the overview, one line per target name."
1584 (function (lambda (item)
1585 (let* ((target-name (car item
))
1586 (no-prereqs (not (member target-name prereq-list
)))
1587 (needs-rebuild (or no-prereqs
1589 makefile-query-one-target-method
1594 (cond (no-prereqs " .. has no prerequisites")
1595 (needs-rebuild " .. NEEDS REBUILD")
1596 (t " .. is up to date"))))
1599 (goto-char (point-min))
1600 (delete-file filename
)) ; remove the tmpfile
1602 (defun makefile-query-by-make-minus-q (target &optional filename
)
1604 (call-process makefile-brave-make nil nil nil
1605 "-f" filename
"-q" target
))))
1609 ;;; ------------------------------------------------------------
1610 ;;; Continuation cleanup
1611 ;;; ------------------------------------------------------------
1613 (defun makefile-cleanup-continuations ()
1614 (if (eq major-mode
'makefile-mode
)
1615 (if (and makefile-cleanup-continuations
1616 (not buffer-read-only
))
1618 (goto-char (point-min))
1619 (while (re-search-forward "\\\\[ \t]+$" nil t
)
1620 (replace-match "\\" t t
))))))
1623 ;;; ------------------------------------------------------------
1624 ;;; Warn of suspicious lines
1625 ;;; ------------------------------------------------------------
1627 (defun makefile-warn-suspicious-lines ()
1628 ;; Returning non-nil cancels the save operation
1629 (if (eq major-mode
'makefile-mode
)
1631 (goto-char (point-min))
1632 (if (re-search-forward "^\\(\t+$\\| +\t\\)" nil t
)
1634 (format "Suspicious line %d. Save anyway? "
1635 (count-lines (point-min) (point)))))))))
1637 (defun makefile-warn-continuations ()
1638 (if (eq major-mode
'makefile-mode
)
1640 (goto-char (point-min))
1641 (if (re-search-forward "\\\\[ \t]+$" nil t
)
1643 (format "Suspicious continuation in line %d. Save anyway? "
1644 (count-lines (point-min) (point)))))))))
1647 ;;; ------------------------------------------------------------
1648 ;;; GNU make function support
1649 ;;; ------------------------------------------------------------
1651 (defun makefile-insert-gmake-function ()
1652 "Insert a GNU make function call.
1653 Asks for the name of the function to use (with completion).
1654 Then prompts for all required parameters."
1656 (let* ((gm-function-name (completing-read
1658 makefile-gnumake-functions-alist
1660 (gm-function-prompts
1661 (cdr (assoc gm-function-name makefile-gnumake-functions-alist
))))
1662 (if (not (zerop (length gm-function-name
)))
1663 (insert (makefile-format-macro-ref
1664 (concat gm-function-name
" "
1665 (makefile-prompt-for-gmake-funargs
1666 gm-function-name gm-function-prompts
)))
1669 (defun makefile-prompt-for-gmake-funargs (function-name prompt-list
)
1671 (function (lambda (one-prompt)
1672 (read-string (format "[%s] %s: " function-name one-prompt
)
1679 ;;; ------------------------------------------------------------
1680 ;;; Utility functions
1681 ;;; ------------------------------------------------------------
1683 (defun makefile-match-function-end (end)
1684 "To be called as an anchored matcher by font-lock.
1685 The anchor must have matched the opening parens in the first group."
1686 (let ((s (match-string-no-properties 1)))
1687 (setq s
(cond ((string= s
"(") "\\(.*?\\)[ \t]*)")
1688 ((string= s
"{") "\\(.*?\\)[ \t]*}")
1689 ((string= s
"((") "\\(.*?\\)[ \t]*))")
1690 ((string= s
"{{") "\\(.*?\\)[ \t]*}}")))
1691 (if s
(looking-at s
))))
1693 (defun makefile-match-dependency (bound)
1694 "Search for `makefile-dependency-regex' up to BOUND.
1695 Checks that the colon has not already been fontified, else we
1696 matched in a rule action."
1699 (while (progn (skip-chars-forward makefile-dependency-skip bound
)
1700 (< (point) (or bound
(point-max))))
1702 (or (eq (char-after) ?
=)
1703 (get-text-property (1- (point)) 'face
)
1704 (when (save-excursion
1706 (looking-at makefile-dependency-regex
))
1708 (throw 'found
(point)))))
1712 (defun makefile-match-action (bound)
1714 (while (re-search-forward makefile-rule-action-regex bound t
)
1715 (or (eq ?
\\ (char-after (- (match-beginning 0) 2)))
1716 (throw 'found t
)))))
1718 (defun makefile-do-macro-insertion (macro-name)
1719 "Insert a macro reference."
1720 (if (not (zerop (length macro-name
)))
1721 (if (assoc macro-name makefile-runtime-macros-list
)
1722 (insert "$" macro-name
)
1723 (insert (makefile-format-macro-ref macro-name
)))))
1725 (defun makefile-remember-target (target-name &optional has-prereqs
)
1726 "Remember a given target if it is not already remembered for this buffer."
1727 (if (not (zerop (length target-name
)))
1729 (if (not (assoc target-name makefile-target-table
))
1730 (setq makefile-target-table
1731 (cons (list target-name
) makefile-target-table
)))
1733 (setq makefile-has-prereqs
1734 (cons target-name makefile-has-prereqs
))))))
1736 (defun makefile-remember-macro (macro-name)
1737 "Remember a given macro if it is not already remembered for this buffer."
1738 (if (not (zerop (length macro-name
)))
1739 (if (not (assoc macro-name makefile-macro-table
))
1740 (setq makefile-macro-table
1741 (cons (list macro-name
) makefile-macro-table
)))))
1743 (defun makefile-forward-after-target-colon ()
1744 "Move point forward after inserting the terminating colon of a target.
1745 This acts according to the value of `makefile-tab-after-target-colon'."
1746 (if makefile-tab-after-target-colon
1750 (defun makefile-browser-on-macro-line-p ()
1751 "Determine if point is on a macro line in the browser."
1754 (re-search-forward "\\$[{(]" (line-end-position) t
)))
1756 (defun makefile-browser-this-line-target-name ()
1757 "Extract the target name from a line in the browser."
1760 (skip-chars-backward "^ \t")
1761 (buffer-substring (point) (1- (line-end-position)))))
1763 (defun makefile-browser-this-line-macro-name ()
1764 "Extract the macro name from a line in the browser."
1767 (re-search-forward "\\$[{(]" (line-end-position) t
)
1768 (let ((macro-start (point)))
1769 (skip-chars-forward "^})")
1770 (buffer-substring macro-start
(point)))))
1772 (defun makefile-format-macro-ref (macro-name)
1773 "Format a macro reference.
1774 Uses `makefile-use-curly-braces-for-macros-p'."
1775 (if (or (char-equal ?\
( (string-to-char macro-name
))
1776 (char-equal ?\
{ (string-to-char macro-name
)))
1777 (format "$%s" macro-name
)
1778 (if makefile-use-curly-braces-for-macros-p
1779 (format "${%s}" macro-name
)
1780 (format "$(%s)" macro-name
))))
1782 (defun makefile-browser-get-state-for-line (n)
1783 (aref makefile-browser-selection-vector
(1- n
)))
1785 (defun makefile-browser-set-state-for-line (n to-state
)
1786 (aset makefile-browser-selection-vector
(1- n
) to-state
))
1788 (defun makefile-browser-toggle-state-for-line (n)
1789 (makefile-browser-set-state-for-line n
(not (makefile-browser-get-state-for-line n
))))
1791 (defun makefile-last-line-p ()
1792 (= (line-end-position) (point-max)))
1794 (defun makefile-first-line-p ()
1795 (= (line-beginning-position) (point-min)))
1799 ;;; Support for other packages, like add-log.
1801 (defun makefile-add-log-defun ()
1802 "Return name of target or variable assignment that point is in.
1803 If it isn't in one, return nil."
1807 ;; Scan back line by line, noticing when we come to a
1808 ;; variable or rule definition, and giving up when we see
1809 ;; a line that is not part of either of those.
1810 (while (not (or (setq found
1811 (when (or (looking-at makefile-macroassign-regex
)
1812 (looking-at makefile-dependency-regex
))
1813 (match-string-no-properties 1)))
1814 ;; Don't keep looking across a blank line or comment.
1815 (looking-at "$\\|#")
1816 (not (zerop (forward-line -
1))))))
1819 (provide 'make-mode
)
1821 ;;; arch-tag: bd23545a-de91-44fb-b1b2-feafbb2635a0
1822 ;;; make-mode.el ends here