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."
100 :link
'(custom-group-link :tag
"Font Lock Faces group" font-lock-faces
)
104 (defface makefile-space
105 '((((class color
)) (:background
"hotpink"))
106 (t (:reverse-video t
)))
107 "Face to use for highlighting leading spaces in Font-Lock mode."
109 (put 'makefile-space-face
'face-alias
'makefile-space
)
111 (defface makefile-targets
112 ;; This needs to go along both with foreground and background colors (i.e. shell)
113 '((t (:inherit font-lock-function-name-face
)))
114 "Face to use for additionally highlighting rule targets in Font-Lock mode."
118 (defface makefile-shell
120 ;;'((((class color) (min-colors 88) (background light)) (:background "seashell1"))
121 ;; (((class color) (min-colors 88) (background dark)) (:background "seashell4")))
122 "Face to use for additionally highlighting Shell commands in Font-Lock mode."
126 (defface makefile-makepp-perl
127 '((((class color
) (background light
)) (:background
"LightBlue1")) ; Camel Book
128 (((class color
) (background dark
)) (:background
"DarkBlue"))
129 (t (:reverse-video t
)))
130 "Face to use for additionally highlighting Perl code in Font-Lock mode."
134 (defcustom makefile-browser-buffer-name
"*Macros and Targets*"
135 "*Name of the macro- and target browser buffer."
139 (defcustom makefile-target-colon
":"
140 "*String to append to all target names inserted by `makefile-insert-target'.
141 \":\" or \"::\" are common values."
145 (defcustom makefile-macro-assign
" = "
146 "*String to append to all macro names inserted by `makefile-insert-macro'.
147 The normal value should be \" = \", since this is what
148 standard make expects. However, newer makes such as dmake
149 allow a larger variety of different macro assignments, so you
150 might prefer to use \" += \" or \" := \" ."
154 (defcustom makefile-electric-keys nil
155 "*If non-nil, Makefile mode should install electric keybindings.
160 (defcustom makefile-use-curly-braces-for-macros-p nil
161 "*Controls the style of generated macro references.
162 Non-nil means macro references should use curly braces, like `${this}'.
163 nil means use parentheses, like `$(this)'."
167 (defcustom makefile-tab-after-target-colon t
168 "*If non-nil, insert a TAB after a target colon.
169 Otherwise, a space is inserted.
174 (defcustom makefile-browser-leftmost-column
10
175 "*Number of blanks to the left of the browser selection mark."
179 (defcustom makefile-browser-cursor-column
10
180 "*Column the cursor goes to when it moves up or down in the Makefile browser."
184 (defcustom makefile-backslash-column
48
185 "*Column in which `makefile-backslash-region' inserts backslashes."
189 (defcustom makefile-backslash-align t
190 "*If non-nil, `makefile-backslash-region' will align backslashes."
194 (defcustom makefile-browser-selected-mark
"+ "
195 "*String used to mark selected entries in the Makefile browser."
199 (defcustom makefile-browser-unselected-mark
" "
200 "*String used to mark unselected entries in the Makefile browser."
204 (defcustom makefile-browser-auto-advance-after-selection-p t
205 "*If non-nil, cursor will move after item is selected in Makefile browser."
209 (defcustom makefile-pickup-everything-picks-up-filenames-p nil
210 "*If non-nil, `makefile-pickup-everything' picks up filenames as targets.
211 This means it calls `makefile-pickup-filenames-as-targets'.
212 Otherwise filenames are omitted."
216 (defcustom makefile-cleanup-continuations nil
217 "*If non-nil, automatically clean up continuation lines when saving.
218 A line is cleaned up by removing all whitespace following a trailing
219 backslash. This is done silently.
220 IMPORTANT: Please note that enabling this option causes Makefile mode
221 to MODIFY A FILE WITHOUT YOUR CONFIRMATION when \"it seems necessary\"."
225 (defcustom makefile-mode-hook nil
226 "*Normal hook run by `makefile-mode'."
230 (defvar makefile-browser-hook
'())
233 ;; Special targets for DMake, Sun's make ...
235 (defcustom makefile-special-targets-list
236 '(("DEFAULT") ("DONE") ("ERROR") ("EXPORT")
237 ("FAILED") ("GROUPEPILOG") ("GROUPPROLOG") ("IGNORE")
238 ("IMPORT") ("INCLUDE") ("INCLUDEDIRS") ("INIT")
239 ("KEEP_STATE") ("MAKEFILES") ("MAKE_VERSION") ("NO_PARALLEL")
240 ("PARALLEL") ("PHONY") ("PRECIOUS") ("REMOVE")
241 ("SCCS_GET") ("SILENT") ("SOURCE") ("SUFFIXES")
242 ("WAIT") ("c.o") ("C.o") ("m.o")
243 ("el.elc") ("y.c") ("s.o"))
244 "*List of special targets.
245 You will be offered to complete on one of those in the minibuffer whenever
246 you enter a \".\" at the beginning of a line in `makefile-mode'."
247 :type
'(repeat (list string
))
250 (defcustom makefile-runtime-macros-list
251 '(("@") ("&") (">") ("<") ("*") ("^") ("+") ("?") ("%") ("$"))
252 "*List of macros that are resolved by make at runtime.
253 If you insert a macro reference using `makefile-insert-macro-ref', the name
254 of the macro is checked against this list. If it can be found its name will
255 not be enclosed in { } or ( )."
256 :type
'(repeat (list string
))
259 ;; Note that the first big subexpression is used by font lock. Note
260 ;; that if you change this regexp you might have to fix the imenu
261 ;; index in makefile-imenu-generic-expression.
262 (defvar makefile-dependency-regex
263 ;; Allow for two nested levels $(v1:$(v2:$(v3:a=b)=c)=d)
264 "^\\(\\(?:\\$\\(?:[({]\\(?:\\$\\(?:[({]\\(?:\\$\\(?:[^({]\\|.[^\n$#})]+?[})]\\)\\|[^\n$#)}]\\)+?[})]\\|[^({]\\)\\|[^\n$#)}]\\)+?[})]\\|[^({]\\)\\|[^\n$#:=]\\)+?\\)\\(:\\)\\(?:[ \t]*$\\|[^=\n]\\(?:[^#\n]*?;[ \t]*\\(.+\\)\\)?\\)"
265 "Regex used to find dependency lines in a makefile.")
267 (defconst makefile-bsdmake-dependency-regex
268 (progn (string-match (regexp-quote "\\(:\\)") makefile-dependency-regex
)
269 (replace-match "\\([:!]\\)" t t makefile-dependency-regex
))
270 "Regex used to find dependency lines in a BSD makefile.")
272 (defvar makefile-dependency-skip
"^:"
273 "Characters to skip to find a line that might be a dependency.")
275 (defvar makefile-rule-action-regex
276 "^\t[ \t]*\\([-@]*\\)[ \t]*\\(\\(?:.*\\\\\n\\)*.*\\)"
277 "Regex used to highlight rule action lines in font lock mode.")
279 (defconst makefile-makepp-rule-action-regex
280 ;; Don't care about initial tab, but I don't know how to font-lock correctly without.
281 "^\t[ \t]*\\(\\(?:\\(?:noecho\\|ignore[-_]error\\|[-@]+\\)[ \t]*\\)*\\)\\(\\(&\\S +\\)?\\(?:.*\\\\\n\\)*.*\\)"
282 "Regex used to highlight makepp rule action lines in font lock mode.")
284 (defconst makefile-bsdmake-rule-action-regex
285 (progn (string-match "-@" makefile-rule-action-regex
)
286 (replace-match "-+@" t t makefile-rule-action-regex
))
287 "Regex used to highlight BSD rule action lines in font lock mode.")
289 ;; Note that the first and second subexpression is used by font lock. Note
290 ;; that if you change this regexp you might have to fix the imenu index in
291 ;; makefile-imenu-generic-expression.
292 (defconst makefile-macroassign-regex
293 "^ *\\([^ \n\t][^:#= \t\n]*\\)[ \t]*\\(?:!=\\|[*:+]?[:?]?=\\)"
294 "Regex used to find macro assignment lines in a makefile.")
296 (defconst makefile-var-use-regex
297 "[^$]\\$[({]\\([-a-zA-Z0-9_.]+\\|[@%<?^+*][FD]?\\)"
298 "Regex used to find $(macro) uses in a makefile.")
300 (defconst makefile-ignored-files-in-pickup-regex
301 "\\(^\\..*\\)\\|\\(.*~$\\)\\|\\(.*,v$\\)\\|\\(\\.[chy]\\)"
302 "Regex for filenames that will NOT be included in the target list.")
304 (if (fboundp 'facemenu-unlisted-faces
)
305 (add-to-list 'facemenu-unlisted-faces
'makefile-space
))
306 (defvar makefile-space
'makefile-space
307 "Face to use for highlighting leading spaces in Font-Lock mode.")
309 ;; These lists were inspired by the old solution. But they are silly, because
310 ;; you can't differentiate what follows. They need to be split up.
311 (defconst makefile-statements
'("include")
312 "List of keywords understood by standard make.")
314 (defconst makefile-automake-statements
315 `("if" "else" "endif" ,@makefile-statements
)
316 "List of keywords understood by automake.")
318 (defconst makefile-gmake-statements
319 `("-sinclude" "sinclude" "override" "vpath"
320 "ifdef" "ifndef" "ifeq" "ifneq" "-include" "define" "endef" "export"
322 ,@(cdr makefile-automake-statements
))
323 "List of keywords understood by gmake.")
325 ;; These are even more silly, because you can have more spaces in between.
326 (defconst makefile-makepp-statements
327 `("and ifdef" "and ifndef" "and ifeq" "and ifneq" "and ifperl"
328 "and ifmakeperl" "and ifsys" "and ifnsys" "build_cache" "build_check"
329 "else ifdef" "else ifndef" "else ifeq" "else ifneq" "else ifperl"
330 "else ifmakeperl" "else ifsys" "else ifnsys" "enddef" "load_makefile"
331 "ifperl" "ifmakeperl" "ifsys" "ifnsys" "_include" "makeperl" "makesub"
332 "no_implicit_load" "perl" "perl-begin" "perl_begin" "perl-end" "perl_end"
333 "prebuild" "or ifdef" "or ifndef" "or ifeq" "or ifneq" "or ifperl"
334 "or ifmakeperl" "or ifsys" "or ifnsys" "register_command_parser"
335 "register_scanner" "repository" "runtime" "signature" "sub"
336 ,@(nthcdr 4 makefile-gmake-statements
))
337 "List of keywords understood by gmake.")
339 (defconst makefile-bsdmake-statements
340 `(".elif" ".elifdef" ".elifmake" ".elifndef" ".elifnmake" ".else" ".endfor"
341 ".endif" ".for" ".if" ".ifdef" ".ifmake" ".ifndef" ".ifnmake" ".undef")
342 "List of keywords understood by BSD make.")
344 (defun makefile-make-font-lock-keywords (var keywords space
346 &rest font-lock-keywords
)
347 `(;; Do macro assignments. These get the "variable-name" face.
348 (,makefile-macroassign-regex
349 (1 font-lock-variable-name-face
)
350 ;; This is for after !=
351 (2 'makefile-shell prepend t
)
352 ;; This is for after normal assignment
353 (3 'font-lock-string-face prepend t
))
356 (makefile-match-action
357 (1 font-lock-type-face
)
358 (2 'makefile-shell prepend
)
359 ;; Only makepp has builtin commands.
360 (3 font-lock-builtin-face prepend t
))
362 ;; Variable references even in targets/strings/comments.
363 (,var
1 font-lock-variable-name-face prepend
)
365 ;; Automatic variable references and single character variable references,
366 ;; but not shell variables references.
367 ("[^$]\\$\\([@%<?^+*_]\\|[a-zA-Z0-9]\\>\\)"
368 1 font-lock-constant-face prepend
)
369 ("[^$]\\(\\$[@%*]\\)"
370 1 'makefile-targets append
)
372 ;; Fontify conditionals and includes.
373 (,(concat "^\\(?: [ \t]*\\)?"
374 (regexp-opt keywords t
)
375 "\\>[ \t]*\\([^: \t\n#]*\\)")
376 (1 font-lock-keyword-face
) (2 font-lock-variable-name-face
))
379 `((,negation
(1 font-lock-negation-char-face prepend
)
380 (2 font-lock-negation-char-face prepend t
))))
383 '(;; Highlight lines that contain just whitespace.
384 ;; They can cause trouble, especially if they start with a tab.
385 ("^[ \t]+$" . makefile-space
)
387 ;; Highlight shell comments that Make treats as commands,
388 ;; since these can fool people.
389 ("^\t+#" 0 makefile-space t
)
391 ;; Highlight spaces that precede tabs.
392 ;; They can make a tab fail to be effective.
393 ("^\\( +\\)\t" 1 makefile-space
)))
398 (makefile-match-dependency
399 (1 'makefile-targets prepend
)
400 (3 'makefile-shell prepend t
))))
402 (defconst makefile-font-lock-keywords
403 (makefile-make-font-lock-keywords
404 makefile-var-use-regex
408 (defconst makefile-automake-font-lock-keywords
409 (makefile-make-font-lock-keywords
410 makefile-var-use-regex
411 makefile-automake-statements
414 (defconst makefile-gmake-font-lock-keywords
415 (makefile-make-font-lock-keywords
416 makefile-var-use-regex
417 makefile-gmake-statements
419 "^\\(?: [ \t]*\\)?if\\(n\\)\\(?:def\\|eq\\)\\>"
421 '("[^$]\\(\\$[({][@%*][DF][})]\\)"
422 1 'makefile-targets append
)
424 ;; $(function ...) ${function ...}
425 '("[^$]\\$[({]\\([-a-zA-Z0-9_.]+\\s \\)"
426 1 font-lock-function-name-face prepend
)
428 ;; $(shell ...) ${shell ...}
429 '("[^$]\\$\\([({]\\)shell[ \t]+"
430 makefile-match-function-end nil nil
431 (1 'makefile-shell prepend t
))))
433 (defconst makefile-makepp-font-lock-keywords
434 (makefile-make-font-lock-keywords
435 makefile-var-use-regex
436 makefile-makepp-statements
438 "^\\(?: [ \t]*\\)?\\(?:and[ \t]+\\|else[ \t]+\\|or[ \t]+\\)?if\\(n\\)\\(?:def\\|eq\\|sys\\)\\>"
440 '("[^$]\\(\\$[({]\\(?:output\\|stem\\|target\\)s?\\_>.*?[})]\\)"
441 1 'makefile-targets append
)
443 ;; Colon modifier keywords.
444 '("\\(:\\s *\\)\\(build_c\\(?:ache\\|heck\\)\\|env\\(?:ironment\\)?\\|foreach\\|signature\\|scanner\\|quickscan\\|smartscan\\)\\>\\([^:\n]*\\)"
445 (1 font-lock-type-face t
)
446 (2 font-lock-keyword-face t
)
447 (3 font-lock-variable-name-face t
))
449 ;; $(function ...) $((function ...)) ${function ...} ${{function ...}}
450 '("[^$]\\$\\(?:((?\\|{{?\\)\\([-a-zA-Z0-9_.]+\\s \\)"
451 1 font-lock-function-name-face prepend
)
453 ;; $(shell ...) $((shell ...)) ${shell ...} ${{shell ...}}
454 '("[^$]\\$\\(((?\\|{{?\\)shell\\(?:[-_]\\(?:global[-_]\\)?once\\)?[ \t]+"
455 makefile-match-function-end nil nil
456 (1 'makefile-shell prepend t
))
458 ;; $(perl ...) $((perl ...)) ${perl ...} ${{perl ...}}
459 '("[^$]\\$\\(((?\\|{{?\\)makeperl[ \t]+"
460 makefile-match-function-end nil nil
461 (1 'makefile-makepp-perl prepend t
))
462 '("[^$]\\$\\(((?\\|{{?\\)perl[ \t]+"
463 makefile-match-function-end nil nil
464 (1 'makefile-makepp-perl t t
))
466 ;; Can we unify these with (if (match-end 1) 'prepend t)?
467 '("ifmakeperl\\s +\\(.*\\)" 1 'makefile-makepp-perl prepend
)
468 '("ifperl\\s +\\(.*\\)" 1 'makefile-makepp-perl t
)
470 ;; Perl block single- or multiline, as statement or rule action.
471 ;; Don't know why the initial newline in 2nd variant of group 2 doesn't get skipped.
472 '("\\<make\\(?:perl\\|sub\\s +\\S +\\)\\s *\n?\\s *{\\(?:{\\s *\n?\\(\\(?:.*\n\\)+?\\)\\s *}\\|\\s *\\(\\(?:.*?\\|\n?\\(?:.*\n\\)+?\\)\\)\\)}"
473 (1 'makefile-makepp-perl prepend t
)
474 (2 'makefile-makepp-perl prepend t
))
475 '("\\<\\(?:perl\\|sub\\s +\\S +\\)\\s *\n?\\s *{\\(?:{\\s *\n?\\(\\(?:.*\n\\)+?\\)\\s *}\\|\\s *\\(\\(?:.*?\\|\n?\\(?:.*\n\\)+?\\)\\)\\)}"
476 (1 'makefile-makepp-perl t t
)
477 (2 'makefile-makepp-perl t t
))
479 ;; Statement style perl block.
480 '("perl[-_]begin\\s *\\(?:\\s #.*\\)?\n\\(\\(?:.*\n\\)+?\\)\\s *perl[-_]end\\>"
481 1 'makefile-makepp-perl t
)))
483 (defconst makefile-bsdmake-font-lock-keywords
484 (makefile-make-font-lock-keywords
485 ;; A lot more could be done for variables here:
486 makefile-var-use-regex
487 makefile-bsdmake-statements
489 "^\\(?: [ \t]*\\)?\\.\\(?:el\\)?if\\(n?\\)\\(?:def\\|make\\)?\\>[ \t]*\\(!?\\)"
490 '("^[ \t]*\\.for[ \t].+[ \t]\\(in\\)\\>" 1 font-lock-keyword-face
)))
493 (defconst makefile-font-lock-syntactic-keywords
494 ;; From sh-script.el.
495 ;; A `#' begins a comment in sh when it is unquoted and at the beginning
496 ;; of a word. In the shell, words are separated by metacharacters.
497 ;; The list of special chars is taken from the single-unix spec of the
498 ;; shell command language (under `quoting') but with `$' removed.
499 '(("[^|&;<>()`\\\"' \t\n]\\(#+\\)" 1 "_")
500 ;; Change the syntax of a quoted newline so that it does not end a comment.
503 (defvar makefile-imenu-generic-expression
504 `(("Dependencies" makefile-previous-dependency
1)
505 ("Macro Assignment" ,makefile-macroassign-regex
1))
506 "Imenu generic expression for Makefile mode. See `imenu-generic-expression'.")
508 ;;; ------------------------------------------------------------
509 ;;; The following configurable variables are used in the
510 ;;; up-to-date overview .
511 ;;; The standard configuration assumes that your `make' program
512 ;;; can be run in question/query mode using the `-q' option, this
513 ;;; means that the command
517 ;;; should return an exit status of zero if the target `foo' is
518 ;;; up to date and a nonzero exit status otherwise.
519 ;;; Many makes can do this although the docs/manpages do not mention
520 ;;; it. Try it with your favourite one. GNU make, System V make, and
521 ;;; Dennis Vadura's DMake have no problems.
522 ;;; Set the variable `makefile-brave-make' to the name of the
523 ;;; make utility that does this on your system.
524 ;;; To understand what this is all about see the function definition
525 ;;; of `makefile-query-by-make-minus-q' .
526 ;;; ------------------------------------------------------------
528 (defcustom makefile-brave-make
"make"
529 "*How to invoke make, for `makefile-query-targets'.
530 This should identify a `make' command that can handle the `-q' option."
534 (defcustom makefile-query-one-target-method
'makefile-query-by-make-minus-q
535 "*Function to call to determine whether a make target is up to date.
536 The function must satisfy this calling convention:
538 * As its first argument, it must accept the name of the target to
539 be checked, as a string.
541 * As its second argument, it may accept the name of a makefile
542 as a string. Depending on what you're going to do you may
545 * It must return the integer value 0 (zero) if the given target
546 should be considered up-to-date in the context of the given
547 makefile, any nonzero integer value otherwise."
551 (defcustom makefile-up-to-date-buffer-name
"*Makefile Up-to-date overview*"
552 "*Name of the Up-to-date overview buffer."
556 ;;; --- end of up-to-date-overview configuration ------------------
558 (defvar makefile-mode-abbrev-table nil
559 "Abbrev table in use in Makefile buffers.")
560 (if makefile-mode-abbrev-table
562 (define-abbrev-table 'makefile-mode-abbrev-table
()))
564 (defvar makefile-mode-map
565 (let ((map (make-sparse-keymap)))
567 (define-key map
"\C-c:" 'makefile-insert-target-ref
)
568 (if makefile-electric-keys
570 (define-key map
"$" 'makefile-insert-macro-ref
)
571 (define-key map
":" 'makefile-electric-colon
)
572 (define-key map
"=" 'makefile-electric-equal
)
573 (define-key map
"." 'makefile-electric-dot
)))
574 (define-key map
"\C-c\C-f" 'makefile-pickup-filenames-as-targets
)
575 (define-key map
"\C-c\C-b" 'makefile-switch-to-browser
)
576 (define-key map
"\C-c\C-c" 'comment-region
)
577 (define-key map
"\C-c\C-p" 'makefile-pickup-everything
)
578 (define-key map
"\C-c\C-u" 'makefile-create-up-to-date-overview
)
579 (define-key map
"\C-c\C-i" 'makefile-insert-gmake-function
)
580 (define-key map
"\C-c\C-\\" 'makefile-backslash-region
)
581 (define-key map
"\C-c\C-m\C-a" 'makefile-automake-mode
)
582 (define-key map
"\C-c\C-m\C-b" 'makefile-bsdmake-mode
)
583 (define-key map
"\C-c\C-m\C-g" 'makefile-gmake-mode
)
584 (define-key map
"\C-c\C-m\C-m" 'makefile-mode
)
585 (define-key map
"\C-c\C-m\C-p" 'makefile-makepp-mode
)
586 (define-key map
"\M-p" 'makefile-previous-dependency
)
587 (define-key map
"\M-n" 'makefile-next-dependency
)
588 (define-key map
"\e\t" 'makefile-complete
)
591 (define-key map
[menu-bar makefile-mode
]
592 (cons "Makefile" (make-sparse-keymap "Makefile")))
594 (define-key map
[menu-bar makefile-mode browse
]
595 '("Pop up Makefile Browser" . makefile-switch-to-browser
))
596 (define-key map
[menu-bar makefile-mode complete
]
597 '("Complete Target or Macro" . makefile-complete
))
598 (define-key map
[menu-bar makefile-mode pickup
]
599 '("Find Targets and Macros" . makefile-pickup-everything
))
601 (define-key map
[menu-bar makefile-mode prev
]
602 '("Move to Previous Dependency" . makefile-previous-dependency
))
603 (define-key map
[menu-bar makefile-mode next
]
604 '("Move to Next Dependency" . makefile-next-dependency
))
606 "The keymap that is used in Makefile mode.")
608 (defvar makefile-browser-map nil
609 "The keymap that is used in the macro- and target browser.")
610 (if makefile-browser-map
612 (setq makefile-browser-map
(make-sparse-keymap))
613 (define-key makefile-browser-map
"n" 'makefile-browser-next-line
)
614 (define-key makefile-browser-map
"\C-n" 'makefile-browser-next-line
)
615 (define-key makefile-browser-map
"p" 'makefile-browser-previous-line
)
616 (define-key makefile-browser-map
"\C-p" 'makefile-browser-previous-line
)
617 (define-key makefile-browser-map
" " 'makefile-browser-toggle
)
618 (define-key makefile-browser-map
"i" 'makefile-browser-insert-selection
)
619 (define-key makefile-browser-map
"I" 'makefile-browser-insert-selection-and-quit
)
620 (define-key makefile-browser-map
"\C-c\C-m" 'makefile-browser-insert-continuation
)
621 (define-key makefile-browser-map
"q" 'makefile-browser-quit
)
622 ;; disable horizontal movement
623 (define-key makefile-browser-map
"\C-b" 'undefined
)
624 (define-key makefile-browser-map
"\C-f" 'undefined
))
627 (defvar makefile-mode-syntax-table nil
)
628 (if makefile-mode-syntax-table
630 (setq makefile-mode-syntax-table
(make-syntax-table))
631 (modify-syntax-entry ?\
( "() " makefile-mode-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 ?
\n "> " makefile-mode-syntax-table
))
643 ;;; ------------------------------------------------------------
644 ;;; Internal variables.
645 ;;; You don't need to configure below this line.
646 ;;; ------------------------------------------------------------
648 (defvar makefile-target-table nil
649 "Table of all target names known for this buffer.")
651 (defvar makefile-macro-table nil
652 "Table of all macro names known for this buffer.")
654 (defvar makefile-browser-client
655 "A buffer in Makefile mode that is currently using the browser.")
657 (defvar makefile-browser-selection-vector nil
)
658 (defvar makefile-has-prereqs nil
)
659 (defvar makefile-need-target-pickup t
)
660 (defvar makefile-need-macro-pickup t
)
662 (defvar makefile-mode-hook
'())
664 ;; Each element looks like '("GNU MAKE FUNCTION" "ARG" "ARG" ... )
665 ;; Each "ARG" is used as a prompt for a required argument.
666 (defconst makefile-gnumake-functions-alist
669 ("subst" "From" "To" "In")
670 ("patsubst" "Pattern" "Replacement" "In")
672 ("findstring" "Find what" "In")
673 ("filter" "Pattern" "Text")
674 ("filter-out" "Pattern" "Text")
676 ;; Filename functions
681 ("addprefix" "Prefix" "Names")
682 ("addsuffix" "Suffix" "Names")
683 ("join" "List 1" "List 2")
684 ("word" "Index" "Text")
687 ("wildcard" "Pattern")
689 ("foreach" "Variable" "List" "Text")
690 ("origin" "Variable")
691 ("shell" "Command")))
694 ;;; ------------------------------------------------------------
695 ;;; The mode function itself.
696 ;;; ------------------------------------------------------------
699 (defun makefile-mode ()
700 "Major mode for editing standard Makefiles.
702 If you are editing a file for a different make, try one of the
703 variants `makefile-automake-mode', `makefile-gmake-mode',
704 `makefile-makepp-mode' or `makefile-bsdmake-mode'. All but the
705 last should be correctly chosen based on the file name, except if
706 it is *.mk. This function ends by invoking the function(s)
707 `makefile-mode-hook'.
709 It is strongly recommended to use `font-lock-mode', because that
710 provides additional parsing information. This is used for
711 example to see that a rule action `echo foo: bar' is a not rule
712 dependency, despite the colon.
714 \\{makefile-mode-map}
716 In the browser, use the following keys:
718 \\{makefile-browser-map}
720 Makefile mode can be configured by modifying the following variables:
722 `makefile-browser-buffer-name':
723 Name of the macro- and target browser buffer.
725 `makefile-target-colon':
726 The string that gets appended to all target names
727 inserted by `makefile-insert-target'.
728 \":\" or \"::\" are quite common values.
730 `makefile-macro-assign':
731 The string that gets appended to all macro names
732 inserted by `makefile-insert-macro'.
733 The normal value should be \" = \", since this is what
734 standard make expects. However, newer makes such as dmake
735 allow a larger variety of different macro assignments, so you
736 might prefer to use \" += \" or \" := \" .
738 `makefile-tab-after-target-colon':
739 If you want a TAB (instead of a space) to be appended after the
740 target colon, then set this to a non-nil value.
742 `makefile-browser-leftmost-column':
743 Number of blanks to the left of the browser selection mark.
745 `makefile-browser-cursor-column':
746 Column in which the cursor is positioned when it moves
747 up or down in the browser.
749 `makefile-browser-selected-mark':
750 String used to mark selected entries in the browser.
752 `makefile-browser-unselected-mark':
753 String used to mark unselected entries in the browser.
755 `makefile-browser-auto-advance-after-selection-p':
756 If this variable is set to a non-nil value the cursor
757 will automagically advance to the next line after an item
758 has been selected in the browser.
760 `makefile-pickup-everything-picks-up-filenames-p':
761 If this variable is set to a non-nil value then
762 `makefile-pickup-everything' also picks up filenames as targets
763 (i.e. it calls `makefile-pickup-filenames-as-targets'), otherwise
764 filenames are omitted.
766 `makefile-cleanup-continuations':
767 If this variable is set to a non-nil value then Makefile mode
768 will assure that no line in the file ends with a backslash
769 (the continuation character) followed by any whitespace.
770 This is done by silently removing the trailing whitespace, leaving
771 the backslash itself intact.
772 IMPORTANT: Please note that enabling this option causes Makefile mode
773 to MODIFY A FILE WITHOUT YOUR CONFIRMATION when \"it seems necessary\".
775 `makefile-browser-hook':
776 A function or list of functions to be called just before the
777 browser is entered. This is executed in the makefile buffer.
779 `makefile-special-targets-list':
780 List of special targets. You will be offered to complete
781 on one of those in the minibuffer whenever you enter a `.'.
782 at the beginning of a line in Makefile mode."
785 (kill-all-local-variables)
786 (add-hook 'write-file-functions
787 'makefile-warn-suspicious-lines nil t
)
788 (add-hook 'write-file-functions
789 'makefile-warn-continuations nil t
)
790 (add-hook 'write-file-functions
791 'makefile-cleanup-continuations nil t
)
792 (make-local-variable 'makefile-target-table
)
793 (make-local-variable 'makefile-macro-table
)
794 (make-local-variable 'makefile-has-prereqs
)
795 (make-local-variable 'makefile-need-target-pickup
)
796 (make-local-variable 'makefile-need-macro-pickup
)
799 (make-local-variable 'font-lock-defaults
)
800 (setq font-lock-defaults
801 ;; SYNTAX-BEGIN set to backward-paragraph to avoid slow-down
802 ;; near the end of a large buffer, due to parse-partial-sexp's
803 ;; trying to parse all the way till the beginning of buffer.
804 '(makefile-font-lock-keywords
808 (font-lock-syntactic-keywords . makefile-font-lock-syntactic-keywords
)
809 (font-lock-support-mode))) ; JIT breaks on long series of continuation lines.
812 (make-local-variable 'add-log-current-defun-function
)
813 (setq add-log-current-defun-function
'makefile-add-log-defun
)
816 (make-local-variable 'imenu-generic-expression
)
817 (setq imenu-generic-expression makefile-imenu-generic-expression
)
820 (make-local-variable 'dabbrev-abbrev-skip-leading-regexp
)
821 (setq dabbrev-abbrev-skip-leading-regexp
"\\$")
824 (setq local-abbrev-table makefile-mode-abbrev-table
)
827 (make-local-variable 'fill-paragraph-function
)
828 (setq fill-paragraph-function
'makefile-fill-paragraph
)
831 (make-local-variable 'comment-start
)
832 (setq comment-start
"#")
833 (make-local-variable 'comment-end
)
834 (setq comment-end
"")
835 (make-local-variable 'comment-start-skip
)
836 (setq comment-start-skip
"#+[ \t]*")
838 ;; Make sure TAB really inserts \t.
839 (set (make-local-variable 'indent-line-function
) 'indent-to-left-margin
)
841 ;; become the current major mode
842 (setq major-mode
'makefile-mode
)
843 (setq mode-name
"Makefile")
845 ;; Activate keymap and syntax table.
846 (use-local-map makefile-mode-map
)
847 (set-syntax-table makefile-mode-syntax-table
)
849 ;; Real TABs are important in makefiles
850 (setq indent-tabs-mode t
)
851 (run-mode-hooks 'makefile-mode-hook
))
853 ;; These should do more than just differentiate font-lock.
855 (define-derived-mode makefile-automake-mode makefile-mode
"Makefile.am"
856 "An adapted `makefile-mode' that knows about automake."
857 (setq font-lock-defaults
858 `(makefile-automake-font-lock-keywords ,@(cdr font-lock-defaults
))))
861 (define-derived-mode makefile-gmake-mode makefile-mode
"GNUmakefile"
862 "An adapted `makefile-mode' that knows about gmake."
863 (setq font-lock-defaults
864 `(makefile-gmake-font-lock-keywords ,@(cdr font-lock-defaults
))))
867 (define-derived-mode makefile-makepp-mode makefile-mode
"Makeppfile"
868 "An adapted `makefile-mode' that knows about makepp."
869 (set (make-local-variable 'makefile-rule-action-regex
)
870 makefile-makepp-rule-action-regex
)
871 (setq font-lock-defaults
872 `(makefile-makepp-font-lock-keywords ,@(cdr font-lock-defaults
))
873 imenu-generic-expression
874 `(("Functions" "^[ \t]*\\(?:make\\)?sub[ \t]+\\([A-Za-z0-9_]+\\)" 1)
875 ,@imenu-generic-expression
)))
878 (define-derived-mode makefile-bsdmake-mode makefile-mode
"BSDmakefile"
879 "An adapted `makefile-mode' that knows about BSD make."
880 (set (make-local-variable 'makefile-dependency-regex
)
881 makefile-bsdmake-dependency-regex
)
882 (set (make-local-variable 'makefile-dependency-skip
) "^:!")
883 (set (make-local-variable 'makefile-rule-action-regex
)
884 makefile-bsdmake-rule-action-regex
)
885 (setq font-lock-defaults
886 `(makefile-bsdmake-font-lock-keywords ,@(cdr font-lock-defaults
))))
892 (defun makefile-next-dependency ()
893 "Move point to the beginning of the next dependency line."
895 (let ((here (point)))
897 (if (makefile-match-dependency nil
)
898 (progn (beginning-of-line) t
) ; indicate success
899 (goto-char here
) nil
)))
901 (defun makefile-previous-dependency ()
902 "Move point to the beginning of the previous dependency line."
906 ;; makefile-match-dependency done backwards:
908 (while (progn (skip-chars-backward makefile-dependency-skip
)
910 (or (prog1 (eq (char-after) ?
=)
912 (get-text-property (point) 'face
)
914 (if (> (point) (+ (point-min) 2))
915 (eq (char-before (1- (point))) ?
\\))
916 (if (looking-at makefile-dependency-regex
)
923 ;;; Electric keys. Blech.
925 (defun makefile-electric-dot (arg)
926 "Prompt for the name of a special target to insert.
927 Only does electric insertion at beginning of line.
928 Anywhere else just self-inserts."
931 (makefile-insert-special-target)
932 (self-insert-command arg
)))
934 (defun makefile-insert-special-target ()
935 "Prompt for and insert a special target name.
936 Uses `makefile-special-targets' list."
938 (makefile-pickup-targets)
939 (let ((special-target
940 (completing-read "Special target: "
941 makefile-special-targets-list nil nil nil
)))
942 (if (zerop (length special-target
))
944 (insert "." special-target
":")
945 (makefile-forward-after-target-colon))))
947 (defun makefile-electric-equal (arg)
948 "Prompt for name of a macro to insert.
949 Only does prompting if point is at beginning of line.
950 Anywhere else just self-inserts."
952 (makefile-pickup-macros)
954 (call-interactively 'makefile-insert-macro
)
955 (self-insert-command arg
)))
957 (defun makefile-insert-macro (macro-name)
958 "Prepare definition of a new macro."
959 (interactive "sMacro Name: ")
960 (makefile-pickup-macros)
961 (if (not (zerop (length macro-name
)))
964 (insert macro-name makefile-macro-assign
)
965 (setq makefile-need-macro-pickup t
)
966 (makefile-remember-macro macro-name
))))
968 (defun makefile-insert-macro-ref (macro-name)
969 "Complete on a list of known macros, then insert complete ref at point."
973 (makefile-pickup-macros)
974 (completing-read "Refer to macro: " makefile-macro-table nil nil nil
))))
975 (makefile-do-macro-insertion macro-name
))
977 (defun makefile-insert-target (target-name)
978 "Prepare definition of a new target (dependency line)."
979 (interactive "sTarget: ")
980 (if (not (zerop (length target-name
)))
983 (insert target-name makefile-target-colon
)
984 (makefile-forward-after-target-colon)
986 (setq makefile-need-target-pickup t
)
987 (makefile-remember-target target-name
))))
989 (defun makefile-insert-target-ref (target-name)
990 "Complete on a list of known targets, then insert TARGET-NAME at point."
994 (makefile-pickup-targets)
995 (completing-read "Refer to target: " makefile-target-table nil nil nil
))))
996 (if (not (zerop (length target-name
)))
997 (insert target-name
" ")))
999 (defun makefile-electric-colon (arg)
1000 "Prompt for name of new target.
1001 Prompting only happens at beginning of line.
1002 Anywhere else just self-inserts."
1005 (call-interactively 'makefile-insert-target
)
1006 (self-insert-command arg
)))
1010 ;;; ------------------------------------------------------------
1011 ;;; Extracting targets and macros from an existing makefile
1012 ;;; ------------------------------------------------------------
1014 (defun makefile-pickup-targets ()
1015 "Notice names of all target definitions in Makefile."
1017 (when makefile-need-target-pickup
1018 (setq makefile-need-target-pickup nil
1019 makefile-target-table nil
1020 makefile-has-prereqs nil
)
1022 (goto-char (point-min))
1023 (while (makefile-match-dependency nil
)
1024 (goto-char (match-beginning 1))
1025 (while (let ((target-name
1026 (buffer-substring-no-properties (point)
1028 (skip-chars-forward "^ \t:#")
1031 (not (looking-at ":[ \t]*$"))))
1032 (if (makefile-remember-target target-name has-prereqs
)
1033 (message "Picked up target \"%s\" from line %d"
1034 target-name
(line-number-at-pos)))
1035 (skip-chars-forward " \t")
1036 (not (or (eolp) (eq (char-after) ?
:)))))
1038 (message "Read targets OK.")))
1040 (defun makefile-pickup-macros ()
1041 "Notice names of all macro definitions in Makefile."
1043 (when makefile-need-macro-pickup
1044 (setq makefile-need-macro-pickup nil
1045 makefile-macro-table nil
)
1047 (goto-char (point-min))
1048 (while (re-search-forward makefile-macroassign-regex nil t
)
1049 (goto-char (match-beginning 1))
1050 (let ((macro-name (buffer-substring-no-properties (point)
1052 (skip-chars-forward "^ \t:#=*")
1054 (if (makefile-remember-macro macro-name
)
1055 (message "Picked up macro \"%s\" from line %d"
1056 macro-name
(line-number-at-pos))))
1058 (message "Read macros OK.")))
1060 (defun makefile-pickup-everything (arg)
1061 "Notice names of all macros and targets in Makefile.
1062 Prefix arg means force pickups to be redone."
1065 (setq makefile-need-target-pickup t
1066 makefile-need-macro-pickup t
))
1067 (makefile-pickup-macros)
1068 (makefile-pickup-targets)
1069 (if makefile-pickup-everything-picks-up-filenames-p
1070 (makefile-pickup-filenames-as-targets)))
1072 (defun makefile-pickup-filenames-as-targets ()
1073 "Scan the current directory for filenames to use as targets.
1074 Checks each filename against `makefile-ignored-files-in-pickup-regex'
1075 and adds all qualifying names to the list of known targets."
1077 (mapc (lambda (name)
1078 (or (file-directory-p name
)
1079 (string-match makefile-ignored-files-in-pickup-regex name
)
1080 (if (makefile-remember-target name
)
1081 (message "Picked up file \"%s\" as target" name
))))
1082 (file-name-all-completions "" (or (file-name-directory (buffer-file-name)) ""))))
1088 (defun makefile-complete ()
1089 "Perform completion on Makefile construct preceding point.
1090 Can complete variable and target names.
1091 The context determines which are considered."
1093 (let* ((beg (save-excursion
1094 (skip-chars-backward "^$(){}:#= \t\n")
1096 (try (buffer-substring beg
(point)))
1102 (let ((pc (preceding-char)))
1104 ;; Beginning of line means anything.
1108 ;; Preceding "$" means macros only.
1112 ;; Preceding "$(" or "${" means macros only.
1119 (= (preceding-char) ?$
))))
1120 (setq do-macros t
)))))
1123 (let* ((table (append (if do-macros
1125 makefile-target-table
)
1126 makefile-macro-table
))
1127 (completion (try-completion try table
)))
1129 ;; Exact match, so insert closing paren or colon.
1131 (insert (if do-macros
1143 (message "Can't find completion for \"%s\"" try
)
1146 ;; Partial completion.
1147 ((not (string= try completion
))
1148 ;; FIXME it would be nice to supply the closing paren if an
1149 ;; exact, unambiguous match were found. That is not possible
1150 ;; right now. Ditto closing ":" for targets.
1151 (delete-region beg
(point))
1153 ;; DO-MACROS means doing macros only. If not that, then check
1154 ;; to see if this completion is a macro. Special insertion
1155 ;; must be done for macros.
1157 (assoc completion makefile-macro-table
))
1158 (let ((makefile-use-curly-braces-for-macros-p
1160 makefile-use-curly-braces-for-macros-p
)))
1161 (delete-backward-char 2)
1162 (makefile-do-macro-insertion completion
)
1163 (delete-backward-char 1))
1165 ;; Just insert targets.
1166 (insert completion
)))
1168 ;; Can't complete any more, so make completion list. FIXME
1169 ;; this doesn't do the right thing when the completion is
1170 ;; actually inserted. I don't think there is an easy way to do
1173 (message "Making completion list...")
1174 (let ((list (all-completions try table
)))
1175 (with-output-to-temp-buffer "*Completions*"
1176 (display-completion-list list try
)))
1177 (message "Making completion list...done"))))))
1181 ;; Backslashification. Stolen from cc-mode.el.
1183 (defun makefile-backslash-region (from to delete-flag
)
1184 "Insert, align, or delete end-of-line backslashes on the lines in the region.
1185 With no argument, inserts backslashes and aligns existing backslashes.
1186 With an argument, deletes the backslashes.
1188 This function does not modify the last line of the region if the region ends
1189 right at the start of the following line; it does not modify blank lines
1190 at the start of the region. So you can put the region around an entire macro
1191 definition and conveniently use this command."
1192 (interactive "r\nP")
1195 (let ((column makefile-backslash-column
)
1196 (endmark (make-marker)))
1197 (move-marker endmark to
)
1198 ;; Compute the smallest column number past the ends of all the lines.
1199 (if makefile-backslash-align
1201 (if (not delete-flag
)
1202 (while (< (point) to
)
1204 (if (= (preceding-char) ?
\\)
1205 (progn (forward-char -
1)
1206 (skip-chars-backward " \t")))
1207 (setq column
(max column
(1+ (current-column))))
1209 ;; Adjust upward to a tab column, if that doesn't push
1211 (if (> (% column tab-width
) 0)
1212 (let ((adjusted (* (/ (+ column tab-width -
1) tab-width
)
1214 (if (< adjusted
(window-width))
1215 (setq column adjusted
))))))
1216 ;; Don't modify blank lines at start of region.
1218 (while (and (< (point) endmark
) (eolp))
1220 ;; Add or remove backslashes on all the lines.
1221 (while (and (< (point) endmark
)
1222 ;; Don't backslashify the last line
1223 ;; if the region ends right at the start of the next line.
1226 (< (point) endmark
)))
1227 (if (not delete-flag
)
1228 (makefile-append-backslash column
)
1229 (makefile-delete-backslash))
1231 (move-marker endmark nil
))))
1233 (defun makefile-append-backslash (column)
1235 ;; Note that "\\\\" is needed to get one backslash.
1236 (if (= (preceding-char) ?
\\)
1237 (progn (forward-char -
1)
1238 (delete-horizontal-space)
1239 (indent-to column
(if makefile-backslash-align nil
1)))
1240 (indent-to column
(if makefile-backslash-align nil
1))
1243 (defun makefile-delete-backslash ()
1248 (if (looking-at "\\\\")
1249 (delete-region (1+ (point))
1250 (progn (skip-chars-backward " \t") (point)))))))
1256 (defun makefile-fill-paragraph (arg)
1257 ;; Fill comments, backslashed lines, and variable definitions
1263 ;; Found a comment. Set the fill prefix, and find the paragraph
1264 ;; boundaries by searching for lines that look like comment-only
1266 (let ((fill-prefix (match-string-no-properties 0))
1267 (fill-paragraph-function nil
))
1271 ;; Search backwards.
1273 (while (and (zerop (forward-line -
1))
1275 ;; We may have gone too far. Go forward again.
1276 (or (looking-at "^#")
1281 (while (looking-at "^#")
1284 (fill-paragraph nil
)
1287 ;; Must look for backslashed-region before looking for variable
1289 ((or (eq (char-before (line-end-position 1)) ?
\\)
1290 (eq (char-before (line-end-position 0)) ?
\\))
1291 ;; A backslash region. Find beginning and end, remove
1292 ;; backslashes, fill, and then reapply backslahes.
1297 (while (= (preceding-char) ?
\\)
1303 (while (= (preceding-char) ?
\\)
1307 (narrow-to-region beginning end
)
1308 (makefile-backslash-region (point-min) (point-max) t
)
1309 (let ((fill-paragraph-function nil
))
1310 (fill-paragraph nil
))
1311 (makefile-backslash-region (point-min) (point-max) nil
)
1312 (goto-char (point-max))
1313 (if (< (skip-chars-backward "\n") 0)
1314 (delete-region (point) (point-max))))))
1316 ((looking-at makefile-macroassign-regex
)
1317 ;; Have a macro assign. Fill just this line, and then backslash
1318 ;; resulting region.
1320 (narrow-to-region (point) (line-beginning-position 2))
1321 (let ((fill-paragraph-function nil
))
1322 (fill-paragraph nil
))
1323 (makefile-backslash-region (point-min) (point-max) nil
)))))
1325 ;; Always return non-nil so we don't fill anything else.
1330 ;;; ------------------------------------------------------------
1332 ;;; ------------------------------------------------------------
1334 (defun makefile-browser-format-target-line (target selected
)
1336 (concat (make-string makefile-browser-leftmost-column ?\
)
1338 makefile-browser-selected-mark
1339 makefile-browser-unselected-mark
)
1341 target makefile-target-colon
))
1343 (defun makefile-browser-format-macro-line (macro selected
)
1345 (concat (make-string makefile-browser-leftmost-column ?\
)
1347 makefile-browser-selected-mark
1348 makefile-browser-unselected-mark
)
1349 (makefile-format-macro-ref macro
))))
1351 (defun makefile-browser-fill (targets macros
)
1352 (let ((inhibit-read-only t
))
1353 (goto-char (point-min))
1357 (lambda (item) (insert (makefile-browser-format-target-line (car item
) nil
) "\n")))
1362 (lambda (item) (insert (makefile-browser-format-macro-line (car item
) nil
) "\n")))
1365 (sort-lines nil
(point-min) (point-max))
1366 (goto-char (1- (point-max)))
1367 (delete-char 1) ; remove unnecessary newline at eob
1368 (goto-char (point-min))
1369 (forward-char makefile-browser-cursor-column
)))
1372 ;;; Moving up and down in the browser
1375 (defun makefile-browser-next-line ()
1376 "Move the browser selection cursor to the next line."
1378 (if (not (makefile-last-line-p))
1381 (forward-char makefile-browser-cursor-column
))))
1383 (defun makefile-browser-previous-line ()
1384 "Move the browser selection cursor to the previous line."
1386 (if (not (makefile-first-line-p))
1389 (forward-char makefile-browser-cursor-column
))))
1392 ;;; Quitting the browser (returns to client buffer)
1395 (defun makefile-browser-quit ()
1396 "Leave the browser and return to the makefile buffer."
1398 (let ((my-client makefile-browser-client
))
1399 (setq makefile-browser-client nil
) ; we quitted, so NO client!
1400 (set-buffer-modified-p nil
)
1402 (pop-to-buffer my-client
)))
1405 ;;; Toggle state of a browser item
1408 (defun makefile-browser-toggle ()
1409 "Toggle the selection state of the browser item at the cursor position."
1411 (let ((this-line (count-lines (point-min) (point))))
1412 (setq this-line
(max 1 this-line
))
1413 (makefile-browser-toggle-state-for-line this-line
)
1414 (goto-line this-line
)
1415 (let ((inhibit-read-only t
))
1417 (if (makefile-browser-on-macro-line-p)
1418 (let ((macro-name (makefile-browser-this-line-macro-name)))
1419 (delete-region (point) (progn (end-of-line) (point)))
1421 (makefile-browser-format-macro-line
1423 (makefile-browser-get-state-for-line this-line
))))
1424 (let ((target-name (makefile-browser-this-line-target-name)))
1425 (delete-region (point) (progn (end-of-line) (point)))
1427 (makefile-browser-format-target-line
1429 (makefile-browser-get-state-for-line this-line
))))))
1431 (forward-char makefile-browser-cursor-column
)
1432 (if makefile-browser-auto-advance-after-selection-p
1433 (makefile-browser-next-line))))
1436 ;;; Making insertions into the client buffer
1439 (defun makefile-browser-insert-continuation ()
1440 "Insert a makefile continuation.
1441 In the makefile buffer, go to (end-of-line), insert a \'\\\'
1442 character, insert a new blank line, go to that line and indent by one TAB.
1443 This is most useful in the process of creating continued lines when copying
1444 large dependencies from the browser to the client buffer.
1445 \(point) advances accordingly in the client buffer."
1447 (with-current-buffer makefile-browser-client
1451 (defun makefile-browser-insert-selection ()
1452 "Insert all selected targets and/or macros in the makefile buffer.
1453 Insertion takes place at point."
1457 (let ((current-line 1))
1459 (if (makefile-browser-get-state-for-line current-line
)
1460 (makefile-browser-send-this-line-item))
1462 (setq current-line
(1+ current-line
))))))
1464 (defun makefile-browser-insert-selection-and-quit ()
1466 (makefile-browser-insert-selection)
1467 (makefile-browser-quit))
1469 (defun makefile-browser-send-this-line-item ()
1470 (if (makefile-browser-on-macro-line-p)
1472 (let ((macro-name (makefile-browser-this-line-macro-name)))
1473 (set-buffer makefile-browser-client
)
1474 (insert (makefile-format-macro-ref macro-name
) " ")))
1476 (let ((target-name (makefile-browser-this-line-target-name)))
1477 (set-buffer makefile-browser-client
)
1478 (insert target-name
" ")))))
1480 (defun makefile-browser-start-interaction ()
1481 (use-local-map makefile-browser-map
)
1482 (setq buffer-read-only t
))
1484 (defun makefile-browse (targets macros
)
1486 (if (zerop (+ (length targets
) (length macros
)))
1489 (message "No macros or targets to browse! Consider running 'makefile-pickup-everything\'"))
1490 (let ((browser-buffer (get-buffer-create makefile-browser-buffer-name
)))
1491 (pop-to-buffer browser-buffer
)
1492 (makefile-browser-fill targets macros
)
1493 (shrink-window-if-larger-than-buffer)
1494 (set (make-local-variable 'makefile-browser-selection-vector
)
1495 (make-vector (+ (length targets
) (length macros
)) nil
))
1496 (makefile-browser-start-interaction))))
1498 (defun makefile-switch-to-browser ()
1500 (run-hooks 'makefile-browser-hook
)
1501 (setq makefile-browser-client
(current-buffer))
1502 (makefile-pickup-targets)
1503 (makefile-pickup-macros)
1504 (makefile-browse makefile-target-table makefile-macro-table
))
1508 ;;; ------------------------------------------------------------
1509 ;;; Up-to-date overview buffer
1510 ;;; ------------------------------------------------------------
1512 (defun makefile-create-up-to-date-overview ()
1513 "Create a buffer containing an overview of the state of all known targets.
1514 Known targets are targets that are explicitly defined in that makefile;
1515 in other words, all targets that appear on the left hand side of a
1516 dependency in the makefile."
1518 (if (y-or-n-p "Are you sure that the makefile being edited is consistent? ")
1520 ;; The rest of this function operates on a temporary makefile, created by
1521 ;; writing the current contents of the makefile buffer.
1523 (let ((saved-target-table makefile-target-table
)
1524 (this-buffer (current-buffer))
1525 (makefile-up-to-date-buffer
1526 (get-buffer-create makefile-up-to-date-buffer-name
))
1527 (filename (makefile-save-temporary))
1529 ;; Forget the target table because it may contain picked-up filenames
1530 ;; that are not really targets in the current makefile.
1531 ;; We don't want to query these, so get a new target-table with just the
1532 ;; targets that can be found in the makefile buffer.
1533 ;; The 'old' target table will be restored later.
1535 (real-targets (progn
1536 (makefile-pickup-targets)
1537 makefile-target-table
))
1538 (prereqs makefile-has-prereqs
)
1541 (set-buffer makefile-up-to-date-buffer
)
1542 (setq buffer-read-only nil
)
1544 (makefile-query-targets filename real-targets prereqs
)
1545 (if (zerop (buffer-size)) ; if it did not get us anything
1547 (kill-buffer (current-buffer))
1548 (message "No overview created!")))
1549 (set-buffer this-buffer
)
1550 (setq makefile-target-table saved-target-table
)
1551 (if (get-buffer makefile-up-to-date-buffer-name
)
1553 (pop-to-buffer (get-buffer makefile-up-to-date-buffer-name
))
1554 (shrink-window-if-larger-than-buffer)
1555 (sort-lines nil
(point-min) (point-max))
1556 (setq buffer-read-only t
))))))
1558 (defun makefile-save-temporary ()
1559 "Create a temporary file from the current makefile buffer."
1560 (let ((filename (makefile-generate-temporary-filename)))
1561 (write-region (point-min) (point-max) filename nil
0)
1562 filename
)) ; return the filename
1564 (defun makefile-generate-temporary-filename ()
1565 "Create a filename suitable for use in `makefile-save-temporary'.
1566 Be careful to allow brain-dead file systems (DOS, SYSV ...) to cope
1567 with the generated name!"
1568 (let ((my-name (user-login-name))
1569 (my-uid (int-to-string (user-uid))))
1571 (if (> (length my-name
) 3)
1572 (substring my-name
0 3)
1575 (if (> (length my-uid
) 3)
1576 (substring my-uid
0 3)
1579 (defun makefile-query-targets (filename target-table prereq-list
)
1580 "Fill the up-to-date overview buffer.
1581 Checks each target in TARGET-TABLE using `makefile-query-one-target-method'
1582 and generates the overview, one line per target name."
1585 (function (lambda (item)
1586 (let* ((target-name (car item
))
1587 (no-prereqs (not (member target-name prereq-list
)))
1588 (needs-rebuild (or no-prereqs
1590 makefile-query-one-target-method
1595 (cond (no-prereqs " .. has no prerequisites")
1596 (needs-rebuild " .. NEEDS REBUILD")
1597 (t " .. is up to date"))))
1600 (goto-char (point-min))
1601 (delete-file filename
)) ; remove the tmpfile
1603 (defun makefile-query-by-make-minus-q (target &optional filename
)
1605 (call-process makefile-brave-make nil nil nil
1606 "-f" filename
"-q" target
))))
1610 ;;; ------------------------------------------------------------
1611 ;;; Continuation cleanup
1612 ;;; ------------------------------------------------------------
1614 (defun makefile-cleanup-continuations ()
1615 (if (eq major-mode
'makefile-mode
)
1616 (if (and makefile-cleanup-continuations
1617 (not buffer-read-only
))
1619 (goto-char (point-min))
1620 (while (re-search-forward "\\\\[ \t]+$" nil t
)
1621 (replace-match "\\" t t
))))))
1624 ;;; ------------------------------------------------------------
1625 ;;; Warn of suspicious lines
1626 ;;; ------------------------------------------------------------
1628 (defun makefile-warn-suspicious-lines ()
1629 ;; Returning non-nil cancels the save operation
1630 (if (eq major-mode
'makefile-mode
)
1632 (goto-char (point-min))
1633 (if (re-search-forward "^\\(\t+$\\| +\t\\)" nil t
)
1635 (format "Suspicious line %d. Save anyway? "
1636 (count-lines (point-min) (point)))))))))
1638 (defun makefile-warn-continuations ()
1639 (if (eq major-mode
'makefile-mode
)
1641 (goto-char (point-min))
1642 (if (re-search-forward "\\\\[ \t]+$" nil t
)
1644 (format "Suspicious continuation in line %d. Save anyway? "
1645 (count-lines (point-min) (point)))))))))
1648 ;;; ------------------------------------------------------------
1649 ;;; GNU make function support
1650 ;;; ------------------------------------------------------------
1652 (defun makefile-insert-gmake-function ()
1653 "Insert a GNU make function call.
1654 Asks for the name of the function to use (with completion).
1655 Then prompts for all required parameters."
1657 (let* ((gm-function-name (completing-read
1659 makefile-gnumake-functions-alist
1661 (gm-function-prompts
1662 (cdr (assoc gm-function-name makefile-gnumake-functions-alist
))))
1663 (if (not (zerop (length gm-function-name
)))
1664 (insert (makefile-format-macro-ref
1665 (concat gm-function-name
" "
1666 (makefile-prompt-for-gmake-funargs
1667 gm-function-name gm-function-prompts
)))
1670 (defun makefile-prompt-for-gmake-funargs (function-name prompt-list
)
1672 (function (lambda (one-prompt)
1673 (read-string (format "[%s] %s: " function-name one-prompt
)
1680 ;;; ------------------------------------------------------------
1681 ;;; Utility functions
1682 ;;; ------------------------------------------------------------
1684 (defun makefile-match-function-end (end)
1685 "To be called as an anchored matcher by font-lock.
1686 The anchor must have matched the opening parens in the first group."
1687 (let ((s (match-string-no-properties 1)))
1688 (setq s
(cond ((string= s
"(") "\\(.*?\\)[ \t]*)")
1689 ((string= s
"{") "\\(.*?\\)[ \t]*}")
1690 ((string= s
"((") "\\(.*?\\)[ \t]*))")
1691 ((string= s
"{{") "\\(.*?\\)[ \t]*}}")))
1692 (if s
(looking-at s
))))
1694 (defun makefile-match-dependency (bound)
1695 "Search for `makefile-dependency-regex' up to BOUND.
1696 Checks that the colon has not already been fontified, else we
1697 matched in a rule action."
1700 (while (progn (skip-chars-forward makefile-dependency-skip bound
)
1701 (< (point) (or bound
(point-max))))
1703 (or (eq (char-after) ?
=)
1704 (get-text-property (1- (point)) 'face
)
1705 (if (> (line-beginning-position) (+ (point-min) 2))
1706 (eq (char-before (line-end-position 0)) ?
\\))
1707 (when (save-excursion
1709 (looking-at makefile-dependency-regex
))
1711 (let ((deps-end (match-end 1))
1712 (match-data (match-data)))
1713 (goto-char deps-end
)
1714 (skip-chars-backward " \t")
1715 (setq deps-end
(point))
1717 (skip-chars-forward " \t")
1718 ;; Alter the bounds recorded for subexp 1,
1719 ;; which is what is supposed to match the targets.
1720 (setcar (nthcdr 2 match-data
) (point))
1721 (setcar (nthcdr 3 match-data
) deps-end
)
1722 (store-match-data match-data
)))
1724 (throw 'found
(point)))))
1728 (defun makefile-match-action (bound)
1730 (while (re-search-forward makefile-rule-action-regex bound t
)
1731 (or (eq ?
\\ (char-after (- (match-beginning 0) 2)))
1732 (throw 'found t
)))))
1734 (defun makefile-do-macro-insertion (macro-name)
1735 "Insert a macro reference."
1736 (if (not (zerop (length macro-name
)))
1737 (if (assoc macro-name makefile-runtime-macros-list
)
1738 (insert "$" macro-name
)
1739 (insert (makefile-format-macro-ref macro-name
)))))
1741 (defun makefile-remember-target (target-name &optional has-prereqs
)
1742 "Remember a given target if it is not already remembered for this buffer."
1743 (if (not (zerop (length target-name
)))
1745 (if (not (assoc target-name makefile-target-table
))
1746 (setq makefile-target-table
1747 (cons (list target-name
) makefile-target-table
)))
1749 (setq makefile-has-prereqs
1750 (cons target-name makefile-has-prereqs
))))))
1752 (defun makefile-remember-macro (macro-name)
1753 "Remember a given macro if it is not already remembered for this buffer."
1754 (if (not (zerop (length macro-name
)))
1755 (if (not (assoc macro-name makefile-macro-table
))
1756 (setq makefile-macro-table
1757 (cons (list macro-name
) makefile-macro-table
)))))
1759 (defun makefile-forward-after-target-colon ()
1760 "Move point forward after inserting the terminating colon of a target.
1761 This acts according to the value of `makefile-tab-after-target-colon'."
1762 (if makefile-tab-after-target-colon
1766 (defun makefile-browser-on-macro-line-p ()
1767 "Determine if point is on a macro line in the browser."
1770 (re-search-forward "\\$[{(]" (line-end-position) t
)))
1772 (defun makefile-browser-this-line-target-name ()
1773 "Extract the target name from a line in the browser."
1776 (skip-chars-backward "^ \t")
1777 (buffer-substring (point) (1- (line-end-position)))))
1779 (defun makefile-browser-this-line-macro-name ()
1780 "Extract the macro name from a line in the browser."
1783 (re-search-forward "\\$[{(]" (line-end-position) t
)
1784 (let ((macro-start (point)))
1785 (skip-chars-forward "^})")
1786 (buffer-substring macro-start
(point)))))
1788 (defun makefile-format-macro-ref (macro-name)
1789 "Format a macro reference.
1790 Uses `makefile-use-curly-braces-for-macros-p'."
1791 (if (or (char-equal ?\
( (string-to-char macro-name
))
1792 (char-equal ?\
{ (string-to-char macro-name
)))
1793 (format "$%s" macro-name
)
1794 (if makefile-use-curly-braces-for-macros-p
1795 (format "${%s}" macro-name
)
1796 (format "$(%s)" macro-name
))))
1798 (defun makefile-browser-get-state-for-line (n)
1799 (aref makefile-browser-selection-vector
(1- n
)))
1801 (defun makefile-browser-set-state-for-line (n to-state
)
1802 (aset makefile-browser-selection-vector
(1- n
) to-state
))
1804 (defun makefile-browser-toggle-state-for-line (n)
1805 (makefile-browser-set-state-for-line n
(not (makefile-browser-get-state-for-line n
))))
1807 (defun makefile-last-line-p ()
1808 (= (line-end-position) (point-max)))
1810 (defun makefile-first-line-p ()
1811 (= (line-beginning-position) (point-min)))
1815 ;;; Support for other packages, like add-log.
1817 (defun makefile-add-log-defun ()
1818 "Return name of target or variable assignment that point is in.
1819 If it isn't in one, return nil."
1823 ;; Scan back line by line, noticing when we come to a
1824 ;; variable or rule definition, and giving up when we see
1825 ;; a line that is not part of either of those.
1826 (while (not (or (setq found
1827 (when (or (looking-at makefile-macroassign-regex
)
1828 (looking-at makefile-dependency-regex
))
1829 (match-string-no-properties 1)))
1830 ;; Don't keep looking across a blank line or comment.
1831 (looking-at "$\\|#")
1832 (not (zerop (forward-line -
1))))))
1833 ;; Remove leading and trailing whitespace.
1835 (setq found
(replace-regexp-in-string "[ \t]+\\'" "" found
))
1836 (setq found
(replace-regexp-in-string "\\`[ \t]+" "" found
)))
1839 (provide 'make-mode
)
1841 ;;; arch-tag: bd23545a-de91-44fb-b1b2-feafbb2635a0
1842 ;;; make-mode.el ends here