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."
116 (defface makefile-shell-face
117 '((((class color
) (background light
)) (:background
"seashell1"))
118 (((class color
) (background dark
)) (:background
"seashell4"))
119 (t (:reverse-video t
)))
120 "Face to use for additionally highlighting Shell commands in Font-Lock mode."
124 (defface makefile-makepp-perl-face
125 '((((class color
) (background light
)) (:background
"LightBlue1")) ; Camel Book
126 (((class color
) (background dark
)) (:background
"DarkBlue"))
127 (t (:reverse-video t
)))
128 "Face to use for additionally highlighting Perl code in Font-Lock mode."
132 (defcustom makefile-browser-buffer-name
"*Macros and Targets*"
133 "*Name of the macro- and target browser buffer."
137 (defcustom makefile-target-colon
":"
138 "*String to append to all target names inserted by `makefile-insert-target'.
139 \":\" or \"::\" are common values."
143 (defcustom makefile-macro-assign
" = "
144 "*String to append to all macro names inserted by `makefile-insert-macro'.
145 The normal value should be \" = \", since this is what
146 standard make expects. However, newer makes such as dmake
147 allow a larger variety of different macro assignments, so you
148 might prefer to use \" += \" or \" := \" ."
152 (defcustom makefile-electric-keys nil
153 "*If non-nil, Makefile mode should install electric keybindings.
158 (defcustom makefile-use-curly-braces-for-macros-p nil
159 "*Controls the style of generated macro references.
160 Non-nil means macro references should use curly braces, like `${this}'.
161 nil means use parentheses, like `$(this)'."
165 (defcustom makefile-tab-after-target-colon t
166 "*If non-nil, insert a TAB after a target colon.
167 Otherwise, a space is inserted.
172 (defcustom makefile-browser-leftmost-column
10
173 "*Number of blanks to the left of the browser selection mark."
177 (defcustom makefile-browser-cursor-column
10
178 "*Column the cursor goes to when it moves up or down in the Makefile browser."
182 (defcustom makefile-backslash-column
48
183 "*Column in which `makefile-backslash-region' inserts backslashes."
187 (defcustom makefile-backslash-align t
188 "*If non-nil, `makefile-backslash-region' will align backslashes."
192 (defcustom makefile-browser-selected-mark
"+ "
193 "*String used to mark selected entries in the Makefile browser."
197 (defcustom makefile-browser-unselected-mark
" "
198 "*String used to mark unselected entries in the Makefile browser."
202 (defcustom makefile-browser-auto-advance-after-selection-p t
203 "*If non-nil, cursor will move after item is selected in Makefile browser."
207 (defcustom makefile-pickup-everything-picks-up-filenames-p nil
208 "*If non-nil, `makefile-pickup-everything' picks up filenames as targets.
209 This means it calls `makefile-pickup-filenames-as-targets'.
210 Otherwise filenames are omitted."
214 (defcustom makefile-cleanup-continuations nil
215 "*If non-nil, automatically clean up continuation lines when saving.
216 A line is cleaned up by removing all whitespace following a trailing
217 backslash. This is done silently.
218 IMPORTANT: Please note that enabling this option causes Makefile mode
219 to MODIFY A FILE WITHOUT YOUR CONFIRMATION when \"it seems necessary\"."
223 (defcustom makefile-mode-hook nil
224 "*Normal hook run by `makefile-mode'."
228 (defvar makefile-browser-hook
'())
231 ;; Special targets for DMake, Sun's make ...
233 (defcustom makefile-special-targets-list
234 '(("DEFAULT") ("DONE") ("ERROR") ("EXPORT")
235 ("FAILED") ("GROUPEPILOG") ("GROUPPROLOG") ("IGNORE")
236 ("IMPORT") ("INCLUDE") ("INCLUDEDIRS") ("INIT")
237 ("KEEP_STATE") ("MAKEFILES") ("MAKE_VERSION") ("NO_PARALLEL")
238 ("PARALLEL") ("PHONY") ("PRECIOUS") ("REMOVE")
239 ("SCCS_GET") ("SILENT") ("SOURCE") ("SUFFIXES")
240 ("WAIT") ("c.o") ("C.o") ("m.o")
241 ("el.elc") ("y.c") ("s.o"))
242 "*List of special targets.
243 You will be offered to complete on one of those in the minibuffer whenever
244 you enter a \".\" at the beginning of a line in `makefile-mode'."
245 :type
'(repeat (list string
))
248 (defcustom makefile-runtime-macros-list
249 '(("@") ("&") (">") ("<") ("*") ("^") ("+") ("?") ("%") ("$"))
250 "*List of macros that are resolved by make at runtime.
251 If you insert a macro reference using `makefile-insert-macro-ref', the name
252 of the macro is checked against this list. If it can be found its name will
253 not be enclosed in { } or ( )."
254 :type
'(repeat (list string
))
257 ;; Note that the first big subexpression is used by font lock. Note
258 ;; that if you change this regexp you might have to fix the imenu
259 ;; index in makefile-imenu-generic-expression.
260 (defconst makefile-dependency-regex
261 ;; Allow for one nested level $($(var:a=b):c=d)
262 ;; Scan non-$ constructs char by char, so we don't miss a $ while allowing $$.
263 ;; Since we must allow space between targets, I then don't know how to evite a command starting with :.
264 "^ *\\(\\(?:\\$[({]\\(?:\\$[({][^ \t\n#})]+?[})]\\|[^\n#]\\)+?[})]\\|[^\n#:=]\\)+?\\)[ \t]*:\\(?:[ \t]*$\\|[^=\n].*$\\)"
265 "Regex used to find dependency lines in a makefile.")
267 (defconst makefile-rule-action-regex
268 "^\t[ \t]*\\([-@]*\\)[ \t]*\\(.+\\)"
269 "Regex used to highlight rule action lines in font lock mode.")
271 ;; Note that the first and second subexpression is used by font lock. Note
272 ;; that if you change this regexp you might have to fix the imenu index in
273 ;; makefile-imenu-generic-expression.
274 (defconst makefile-macroassign-regex
275 "^ *\\([^ \n\t][^:#= \t\n]*\\)[ \t]*\\(?:!=[ \t]*\\(.*\\)\\|[*:+]?[:?]?=\\)"
276 "Regex used to find macro assignment lines in a makefile.")
278 (defconst makefile-var-use-regex
279 "[^$]\\$[({]\\([-a-zA-Z0-9_.]+\\|[@%<?^+*][FD]?\\)[}):]"
280 "Regex used to find $(macro) uses in a makefile.")
282 (defconst makefile-ignored-files-in-pickup-regex
283 "\\(^\\..*\\)\\|\\(.*~$\\)\\|\\(.*,v$\\)\\|\\(\\.[chy]\\)"
284 "Regex for filenames that will NOT be included in the target list.")
286 (if (fboundp 'facemenu-unlisted-faces
)
287 (add-to-list 'facemenu-unlisted-faces
'makefile-space-face
))
288 (defvar makefile-space-face
'makefile-space-face
289 "Face to use for highlighting leading spaces in Font-Lock mode.")
291 ;; These lists were inspired by the old solution. But they are silly, because
292 ;; you can't differentiate what follows. They need to be split up.
293 (defconst makefile-statements
'("include")
294 "List of keywords understood by standard make.")
296 (defconst makefile-automake-statements
297 `("if" "else" "endif" ,@makefile-statements
)
298 "List of keywords understood by automake.")
300 (defconst makefile-gmake-statements
301 `("-sinclude" "sinclude" "override" "vpath"
302 "ifdef" "ifndef" "ifeq" "ifneq" "-include" "define" "endef" "export"
304 ,@(cdr makefile-automake-statements
))
305 "List of keywords understood by gmake.")
307 ;; These are even more silly, because you can have more spaces in between.
308 (defconst makefile-makepp-statements
309 `("and ifdef" "and ifndef" "and ifeq" "and ifneq" "and ifperl"
310 "and ifmakeperl" "and ifsys" "and ifnsys" "build_cache" "build_check"
311 "else ifdef" "else ifndef" "else ifeq" "else ifneq" "else ifperl"
312 "else ifmakeperl" "else ifsys" "else ifnsys" "enddef" "load_makefile"
313 "ifperl" "ifmakeperl" "ifsys" "ifnsys" "_include" "makeperl" "makesub"
314 "no_implicit_load" "perl" "perl-begin" "perl_begin" "perl-end" "perl_end"
315 "prebuild" "or ifdef" "or ifndef" "or ifeq" "or ifneq" "or ifperl"
316 "or ifmakeperl" "or ifsys" "or ifnsys" "register_command_parser"
317 "register_scanner" "repository" "runtime" "signature" "sub"
318 ,@(nthcdr 4 makefile-gmake-statements
))
319 "List of keywords understood by gmake.")
321 (defconst makefile-bsdmake-statements
322 `(".elif" ".elifdef" ".elifmake" ".elifndef" ".elifnmake" ".else" ".endfor"
323 ".endif" ".for" ".if" ".ifdef" ".ifmake" ".ifndef" ".ifnmake" ".undef")
324 "List of keywords understood by BSD make.")
326 (defun makefile-make-font-lock-keywords (dependency action var keywords space
328 &rest font-lock-keywords
)
329 `(;; Do macro assignments. These get the "variable-name" face.
330 (,makefile-macroassign-regex
331 (1 font-lock-variable-name-face
)
332 ;; This is for after !=
333 (2 'makefile-shell-face prepend t
))
336 (,dependency
1 'makefile-targets-face prepend
)
340 (1 font-lock-type-face
)
341 (2 'makefile-shell-face prepend
)
342 ;; Only makepp has builtin commands.
343 (3 font-lock-builtin-face prepend t
))
345 ;; Variable references even in targets/strings/comments.
346 (,var
1 font-lock-variable-name-face prepend
)
348 ;; Automatic variable references and single character variable references,
349 ;; but not shell variables references.
350 ("[^$]\\$\\([@%<?^+*_]\\|[a-zA-Z0-9]\\>\\)"
351 1 font-lock-constant-face prepend
)
352 ("[^$]\\(\\$[@%*]\\)"
353 1 'makefile-targets-face prepend
)
355 ;; Fontify conditionals and includes.
356 ;; Note that plain `if' is an automake conditional, and not a bug.
357 (,(concat "^\\(?: [ \t]*\\)?"
358 (regexp-opt keywords t
)
359 "\\>[ \t]*\\([^: \t\n#]*\\)")
360 (1 font-lock-keyword-face
) (2 font-lock-variable-name-face
))
363 `(,negation
(1 font-lock-negation-char-face prepend
)
364 (2 font-lock-negation-char-face prepend t
)))
367 '(;; Highlight lines that contain just whitespace.
368 ;; They can cause trouble, especially if they start with a tab.
369 ("^[ \t]+$" . makefile-space-face
)
371 ;; Highlight shell comments that Make treats as commands,
372 ;; since these can fool people.
373 ("^\t+#" 0 makefile-space-face t
)
375 ;; Highlight spaces that precede tabs.
376 ;; They can make a tab fail to be effective.
377 ("^\\( +\\)\t" 1 makefile-space-face
)))
379 ,@font-lock-keywords
))
381 (defconst makefile-font-lock-keywords
382 (makefile-make-font-lock-keywords
383 makefile-dependency-regex
384 makefile-rule-action-regex
385 makefile-var-use-regex
389 (defconst makefile-automake-font-lock-keywords
390 (makefile-make-font-lock-keywords
391 makefile-dependency-regex
392 makefile-rule-action-regex
393 makefile-var-use-regex
394 makefile-automake-statements
397 (defconst makefile-gmake-font-lock-keywords
398 (makefile-make-font-lock-keywords
399 makefile-dependency-regex
400 makefile-rule-action-regex
401 makefile-var-use-regex
402 makefile-gmake-statements
404 "^\\(?: [ \t]*\\)?if\\(n\\)\\(?:def\\|eq\\)\\>"
406 '("[^$]\\(\\$[({][@%*][DF][})]\\)"
407 1 'makefile-targets-face prepend
)
409 ;; $(function ...) ${function ...}
410 '("[^$]\\$[({]\\(\\S +\\s \\)"
411 1 font-lock-function-name-face
)
413 ;; $(shell ...) ${shell ...}
414 '("[^$]\\$\\([({]\\)shell[ \t]+"
415 makefile-match-function-end nil nil
416 (1 'makefile-shell-face prepend t
))))
418 (defconst makefile-makepp-font-lock-keywords
419 (makefile-make-font-lock-keywords
420 makefile-dependency-regex
421 ;; Don't care about initial tab, but I don't know how to font-lock correctly without.
422 "^\t[ \t]*\\(\\(?:[ \t]*noecho\\>\\|[ \t]*ignore[-_]error\\>\\|[ \t]*[-@]+\\)*\\)[ \t]*\\(\\(&\\S +\\)?.+\\)"
423 makefile-var-use-regex
424 makefile-makepp-statements
426 "^\\(?: [ \t]*\\)?\\(?:and[ \t]+\\|else[ \t]+\\|or[ \t]+\\)?if\\(n\\)\\(?:def\\|eq\\|sys\\)\\>"
428 '("[^$]\\(\\$[({]\\(?:target\\|output\\)s?[})]\\)"
429 1 'makefile-targets-face prepend
)
431 ;; Colon modifier keywords.
432 '(":\\s *\\(build_c\\(?:ache\\|heck\\)\\|env\\(?:ironment\\)?\\|foreach\\|signature\\|scanner\\|quickscan\\|smartscan\\)\\>"
433 1 font-lock-keyword-face t
)
435 ;; $(function ...) $((function ...)) ${function ...} ${{function ...}}
436 '("[^$]\\$\\(((?\\|{{?\\)\\(\\S +\\s \\)"
437 2 font-lock-function-name-face
)
439 ;; $(shell ...) $((shell ...)) ${shell ...} ${{shell ...}}
440 '("[^$]\\$\\(((?\\|{{?\\)shell\\(?:[-_]\\(?:global[-_]\\)?once\\)?[ \t]+"
441 makefile-match-function-end nil nil
442 (1 'makefile-shell-face prepend t
))
444 ;; $(perl ...) $((perl ...)) ${perl ...} ${{perl ...}}
445 '("[^$]\\$\\(((?\\|{{?\\)makeperl[ \t]+"
446 makefile-match-function-end nil nil
447 (1 'makefile-makepp-perl-face prepend t
))
448 '("[^$]\\$\\(((?\\|{{?\\)perl[ \t]+"
449 makefile-match-function-end nil nil
450 (1 'makefile-makepp-perl-face t t
))
452 ;; Can we unify these with (if (match-end 1) 'prepend t)?
453 '("ifmakeperl\\s +\\(.*\\)" 1 'makefile-makepp-perl-face prepend
)
454 '("ifperl\\s +\\(.*\\)" 1 'makefile-makepp-perl-face t
)
456 ;; Perl block single- or multiline, as statement or rule action.
457 ;; Don't know why the initial newline in 2nd variant of group 2 doesn't get skipped.
458 '("\\<make\\(?:perl\\|sub\\s +\\S +\\)\\s *\n?\\s *{\\(?:{\\s *\n?\\(\\(?:.*\n\\)+?\\)\\s *}\\|\\s *\\(\\(?:.*?\\|\n?\\(?:.*\n\\)+?\\)\\)\\)}"
459 (1 'makefile-makepp-perl-face prepend t
)
460 (2 'makefile-makepp-perl-face prepend t
))
461 '("\\<\\(?:perl\\|sub\\s +\\S +\\)\\s *\n?\\s *{\\(?:{\\s *\n?\\(\\(?:.*\n\\)+?\\)\\s *}\\|\\s *\\(\\(?:.*?\\|\n?\\(?:.*\n\\)+?\\)\\)\\)}"
462 (1 'makefile-makepp-perl-face t t
)
463 (2 'makefile-makepp-perl-face t t
))
465 ;; Statement style perl block.
466 '("perl[-_]begin\\s *\\(?:\\s #.*\\)?\n\\(\\(?:.*\n\\)+?\\)\\s *perl[-_]end\\>"
467 1 'makefile-makepp-perl-face t
)))
469 ;; A lot more could be done for variables here:
470 (defconst makefile-bsdmake-font-lock-keywords
471 (makefile-make-font-lock-keywords
472 "^ *\\(\\(?:\\$[({]\\(?:\\$[({][^\n#})]+?[})]\\|[^\n#]\\)+?[})]\\|[^\n#:=]\\)+?\\)[ \t]*[:!]\\(?:[ \t]*$\\|[^=\n].*$\\)"
473 "^\t[ \t]*\\([-+@]*\\)[ \t]*\\(.+\\)"
474 makefile-var-use-regex
475 makefile-bsdmake-statements
477 "^\\(?: [ \t]*\\)?\\.\\(?:el\\)?if\\(n?\\)\\(?:def\\|make\\)?\\>[ \t]*\\(!?\\)"
478 '("^[ \t]*\\.for[ \t].+[ \t]\\(in\\)\\>" 1 font-lock-keyword-face
)))
481 (defconst makefile-font-lock-syntactic-keywords
482 ;; From sh-script.el.
483 ;; A `#' begins a comment in sh when it is unquoted and at the beginning
484 ;; of a word. In the shell, words are separated by metacharacters.
485 ;; The list of special chars is taken from the single-unix spec of the
486 ;; shell command language (under `quoting') but with `$' removed.
487 '(("[^|&;<>()`\\\"' \t\n]\\(#+\\)" 1 "_")
488 ;; Change the syntax of a quoted newline so that it does not end a comment.
491 (defvar makefile-imenu-generic-expression
493 (list "Dependencies" makefile-dependency-regex
1)
494 (list "Macro Assignment" makefile-macroassign-regex
1))
495 "Imenu generic expression for Makefile mode. See `imenu-generic-expression'.")
497 ;;; ------------------------------------------------------------
498 ;;; The following configurable variables are used in the
499 ;;; up-to-date overview .
500 ;;; The standard configuration assumes that your `make' program
501 ;;; can be run in question/query mode using the `-q' option, this
502 ;;; means that the command
506 ;;; should return an exit status of zero if the target `foo' is
507 ;;; up to date and a nonzero exit status otherwise.
508 ;;; Many makes can do this although the docs/manpages do not mention
509 ;;; it. Try it with your favourite one. GNU make, System V make, and
510 ;;; Dennis Vadura's DMake have no problems.
511 ;;; Set the variable `makefile-brave-make' to the name of the
512 ;;; make utility that does this on your system.
513 ;;; To understand what this is all about see the function definition
514 ;;; of `makefile-query-by-make-minus-q' .
515 ;;; ------------------------------------------------------------
517 (defcustom makefile-brave-make
"make"
518 "*How to invoke make, for `makefile-query-targets'.
519 This should identify a `make' command that can handle the `-q' option."
523 (defcustom makefile-query-one-target-method
'makefile-query-by-make-minus-q
524 "*Function to call to determine whether a make target is up to date.
525 The function must satisfy this calling convention:
527 * As its first argument, it must accept the name of the target to
528 be checked, as a string.
530 * As its second argument, it may accept the name of a makefile
531 as a string. Depending on what you're going to do you may
534 * It must return the integer value 0 (zero) if the given target
535 should be considered up-to-date in the context of the given
536 makefile, any nonzero integer value otherwise."
540 (defcustom makefile-up-to-date-buffer-name
"*Makefile Up-to-date overview*"
541 "*Name of the Up-to-date overview buffer."
545 ;;; --- end of up-to-date-overview configuration ------------------
547 (defvar makefile-mode-abbrev-table nil
548 "Abbrev table in use in Makefile buffers.")
549 (if makefile-mode-abbrev-table
551 (define-abbrev-table 'makefile-mode-abbrev-table
()))
553 (defvar makefile-mode-map
554 (let ((map (make-sparse-keymap)))
556 (define-key map
"\C-c:" 'makefile-insert-target-ref
)
557 (if makefile-electric-keys
559 (define-key map
"$" 'makefile-insert-macro-ref
)
560 (define-key map
":" 'makefile-electric-colon
)
561 (define-key map
"=" 'makefile-electric-equal
)
562 (define-key map
"." 'makefile-electric-dot
)))
563 (define-key map
"\C-c\C-f" 'makefile-pickup-filenames-as-targets
)
564 (define-key map
"\C-c\C-b" 'makefile-switch-to-browser
)
565 (define-key map
"\C-c\C-c" 'comment-region
)
566 (define-key map
"\C-c\C-p" 'makefile-pickup-everything
)
567 (define-key map
"\C-c\C-u" 'makefile-create-up-to-date-overview
)
568 (define-key map
"\C-c\C-i" 'makefile-insert-gmake-function
)
569 (define-key map
"\C-c\C-\\" 'makefile-backslash-region
)
570 (define-key map
"\C-c\C-m\C-a" 'makefile-automake-mode
)
571 (define-key map
"\C-c\C-m\C-b" 'makefile-bsdmake-mode
)
572 (define-key map
"\C-c\C-m\C-g" 'makefile-gmake-mode
)
573 (define-key map
"\C-c\C-m\C-m" 'makefile-mode
)
574 (define-key map
"\C-c\C-m\C-p" 'makefile-makepp-mode
)
575 (define-key map
"\M-p" 'makefile-previous-dependency
)
576 (define-key map
"\M-n" 'makefile-next-dependency
)
577 (define-key map
"\e\t" 'makefile-complete
)
580 (define-key map
[menu-bar makefile-mode
]
581 (cons "Makefile" (make-sparse-keymap "Makefile")))
583 (define-key map
[menu-bar makefile-mode browse
]
584 '("Pop up Makefile Browser" . makefile-switch-to-browser
))
585 (define-key map
[menu-bar makefile-mode complete
]
586 '("Complete Target or Macro" . makefile-complete
))
587 (define-key map
[menu-bar makefile-mode pickup
]
588 '("Find Targets and Macros" . makefile-pickup-everything
))
590 (define-key map
[menu-bar makefile-mode prev
]
591 '("Move to Previous Dependency" . makefile-previous-dependency
))
592 (define-key map
[menu-bar makefile-mode next
]
593 '("Move to Next Dependency" . makefile-next-dependency
))
595 "The keymap that is used in Makefile mode.")
597 (defvar makefile-browser-map nil
598 "The keymap that is used in the macro- and target browser.")
599 (if makefile-browser-map
601 (setq makefile-browser-map
(make-sparse-keymap))
602 (define-key makefile-browser-map
"n" 'makefile-browser-next-line
)
603 (define-key makefile-browser-map
"\C-n" 'makefile-browser-next-line
)
604 (define-key makefile-browser-map
"p" 'makefile-browser-previous-line
)
605 (define-key makefile-browser-map
"\C-p" 'makefile-browser-previous-line
)
606 (define-key makefile-browser-map
" " 'makefile-browser-toggle
)
607 (define-key makefile-browser-map
"i" 'makefile-browser-insert-selection
)
608 (define-key makefile-browser-map
"I" 'makefile-browser-insert-selection-and-quit
)
609 (define-key makefile-browser-map
"\C-c\C-m" 'makefile-browser-insert-continuation
)
610 (define-key makefile-browser-map
"q" 'makefile-browser-quit
)
611 ;; disable horizontal movement
612 (define-key makefile-browser-map
"\C-b" 'undefined
)
613 (define-key makefile-browser-map
"\C-f" 'undefined
))
616 (defvar makefile-mode-syntax-table nil
)
617 (if makefile-mode-syntax-table
619 (setq makefile-mode-syntax-table
(make-syntax-table))
620 (modify-syntax-entry ?\
( "() " makefile-mode-syntax-table
)
621 (modify-syntax-entry ?\
) ")( " makefile-mode-syntax-table
)
622 (modify-syntax-entry ?\
[ "(] " makefile-mode-syntax-table
)
623 (modify-syntax-entry ?\
] ")[ " makefile-mode-syntax-table
)
624 (modify-syntax-entry ?\
{ "(} " makefile-mode-syntax-table
)
625 (modify-syntax-entry ?\
} "){ " makefile-mode-syntax-table
)
626 (modify-syntax-entry ?
\' "\" " makefile-mode-syntax-table
)
627 (modify-syntax-entry ?\
` "\" " makefile-mode-syntax-table
)
628 (modify-syntax-entry ?
# "< " makefile-mode-syntax-table
)
629 (modify-syntax-entry ?
\n "> " makefile-mode-syntax-table
))
632 ;;; ------------------------------------------------------------
633 ;;; Internal variables.
634 ;;; You don't need to configure below this line.
635 ;;; ------------------------------------------------------------
637 (defvar makefile-target-table nil
638 "Table of all target names known for this buffer.")
640 (defvar makefile-macro-table nil
641 "Table of all macro names known for this buffer.")
643 (defvar makefile-browser-client
644 "A buffer in Makefile mode that is currently using the browser.")
646 (defvar makefile-browser-selection-vector nil
)
647 (defvar makefile-has-prereqs nil
)
648 (defvar makefile-need-target-pickup t
)
649 (defvar makefile-need-macro-pickup t
)
651 (defvar makefile-mode-hook
'())
653 ;; Each element looks like '("GNU MAKE FUNCTION" "ARG" "ARG" ... )
654 ;; Each "ARG" is used as a prompt for a required argument.
655 (defconst makefile-gnumake-functions-alist
658 ("subst" "From" "To" "In")
659 ("patsubst" "Pattern" "Replacement" "In")
661 ("findstring" "Find what" "In")
662 ("filter" "Pattern" "Text")
663 ("filter-out" "Pattern" "Text")
665 ;; Filename functions
670 ("addprefix" "Prefix" "Names")
671 ("addsuffix" "Suffix" "Names")
672 ("join" "List 1" "List 2")
673 ("word" "Index" "Text")
676 ("wildcard" "Pattern")
678 ("foreach" "Variable" "List" "Text")
679 ("origin" "Variable")
680 ("shell" "Command")))
683 ;;; ------------------------------------------------------------
684 ;;; The mode function itself.
685 ;;; ------------------------------------------------------------
688 (defun makefile-mode ()
689 "Major mode for editing standard Makefiles.
691 If you are editing a file for a different make, try one of the
692 variants `makefile-automake-mode', `makefile-gmake-mode',
693 `makefile-makepp-mode' or `makefile-bsdmake-mode'. All but the
694 last should be correctly chosen based on the file name, except if
695 it is *.mk. This function ends by invoking the function(s)
696 `makefile-mode-hook'.
698 \\{makefile-mode-map}
700 In the browser, use the following keys:
702 \\{makefile-browser-map}
704 Makefile mode can be configured by modifying the following variables:
706 `makefile-browser-buffer-name':
707 Name of the macro- and target browser buffer.
709 `makefile-target-colon':
710 The string that gets appended to all target names
711 inserted by `makefile-insert-target'.
712 \":\" or \"::\" are quite common values.
714 `makefile-macro-assign':
715 The string that gets appended to all macro names
716 inserted by `makefile-insert-macro'.
717 The normal value should be \" = \", since this is what
718 standard make expects. However, newer makes such as dmake
719 allow a larger variety of different macro assignments, so you
720 might prefer to use \" += \" or \" := \" .
722 `makefile-tab-after-target-colon':
723 If you want a TAB (instead of a space) to be appended after the
724 target colon, then set this to a non-nil value.
726 `makefile-browser-leftmost-column':
727 Number of blanks to the left of the browser selection mark.
729 `makefile-browser-cursor-column':
730 Column in which the cursor is positioned when it moves
731 up or down in the browser.
733 `makefile-browser-selected-mark':
734 String used to mark selected entries in the browser.
736 `makefile-browser-unselected-mark':
737 String used to mark unselected entries in the browser.
739 `makefile-browser-auto-advance-after-selection-p':
740 If this variable is set to a non-nil value the cursor
741 will automagically advance to the next line after an item
742 has been selected in the browser.
744 `makefile-pickup-everything-picks-up-filenames-p':
745 If this variable is set to a non-nil value then
746 `makefile-pickup-everything' also picks up filenames as targets
747 (i.e. it calls `makefile-pickup-filenames-as-targets'), otherwise
748 filenames are omitted.
750 `makefile-cleanup-continuations':
751 If this variable is set to a non-nil value then Makefile mode
752 will assure that no line in the file ends with a backslash
753 (the continuation character) followed by any whitespace.
754 This is done by silently removing the trailing whitespace, leaving
755 the backslash itself intact.
756 IMPORTANT: Please note that enabling this option causes Makefile mode
757 to MODIFY A FILE WITHOUT YOUR CONFIRMATION when \"it seems necessary\".
759 `makefile-browser-hook':
760 A function or list of functions to be called just before the
761 browser is entered. This is executed in the makefile buffer.
763 `makefile-special-targets-list':
764 List of special targets. You will be offered to complete
765 on one of those in the minibuffer whenever you enter a `.'.
766 at the beginning of a line in Makefile mode."
769 (kill-all-local-variables)
770 (add-hook 'write-file-functions
771 'makefile-warn-suspicious-lines nil t
)
772 (add-hook 'write-file-functions
773 'makefile-warn-continuations nil t
)
774 (add-hook 'write-file-functions
775 'makefile-cleanup-continuations nil t
)
776 (make-local-variable 'makefile-target-table
)
777 (make-local-variable 'makefile-macro-table
)
778 (make-local-variable 'makefile-has-prereqs
)
779 (make-local-variable 'makefile-need-target-pickup
)
780 (make-local-variable 'makefile-need-macro-pickup
)
783 (make-local-variable 'font-lock-defaults
)
784 (setq font-lock-defaults
785 ;; SYNTAX-BEGIN set to backward-paragraph to avoid slow-down
786 ;; near the end of a large buffer, due to parse-partial-sexp's
787 ;; trying to parse all the way till the beginning of buffer.
788 '(makefile-font-lock-keywords
792 (font-lock-syntactic-keywords . makefile-font-lock-syntactic-keywords
)))
795 (make-local-variable 'add-log-current-defun-function
)
796 (setq add-log-current-defun-function
'makefile-add-log-defun
)
799 (make-local-variable 'imenu-generic-expression
)
800 (setq imenu-generic-expression makefile-imenu-generic-expression
)
803 (make-local-variable 'dabbrev-abbrev-skip-leading-regexp
)
804 (setq dabbrev-abbrev-skip-leading-regexp
"\\$")
807 (setq local-abbrev-table makefile-mode-abbrev-table
)
810 (make-local-variable 'fill-paragraph-function
)
811 (setq fill-paragraph-function
'makefile-fill-paragraph
)
814 (make-local-variable 'comment-start
)
815 (setq comment-start
"#")
816 (make-local-variable 'comment-end
)
817 (setq comment-end
"")
818 (make-local-variable 'comment-start-skip
)
819 (setq comment-start-skip
"#+[ \t]*")
821 ;; Make sure TAB really inserts \t.
822 (set (make-local-variable 'indent-line-function
) 'indent-to-left-margin
)
824 ;; become the current major mode
825 (setq major-mode
'makefile-mode
)
826 (setq mode-name
"Makefile")
828 ;; Activate keymap and syntax table.
829 (use-local-map makefile-mode-map
)
830 (set-syntax-table makefile-mode-syntax-table
)
832 ;; Real TABs are important in makefiles
833 (setq indent-tabs-mode t
)
834 (run-hooks 'makefile-mode-hook
))
836 ;; These should do more than just differentiate font-lock.
838 (define-derived-mode makefile-automake-mode makefile-mode
"Makefile.am"
839 "An adapted `makefile-mode' that knows about automake."
840 (setq font-lock-defaults
841 `(makefile-automake-font-lock-keywords ,@(cdr font-lock-defaults
))))
844 (define-derived-mode makefile-gmake-mode makefile-mode
"GNUmakefile"
845 "An adapted `makefile-mode' that knows about gmake."
846 (setq font-lock-defaults
847 `(makefile-gmake-font-lock-keywords ,@(cdr font-lock-defaults
))))
850 (define-derived-mode makefile-makepp-mode makefile-mode
"Makeppfile"
851 "An adapted `makefile-mode' that knows about makepp."
852 (setq font-lock-defaults
853 `(makefile-makepp-font-lock-keywords ,@(cdr font-lock-defaults
))))
856 (define-derived-mode makefile-bsdmake-mode makefile-mode
"BSDmakefile"
857 "An adapted `makefile-mode' that knows about BSD make."
858 (setq font-lock-defaults
859 `(makefile-bsdmake-font-lock-keywords ,@(cdr font-lock-defaults
))))
865 (defun makefile-next-dependency ()
866 "Move point to the beginning of the next dependency line."
868 (let ((here (point)))
870 (if (re-search-forward makefile-dependency-regex
(point-max) t
)
871 (progn (beginning-of-line) t
) ; indicate success
872 (goto-char here
) nil
)))
874 (defun makefile-previous-dependency ()
875 "Move point to the beginning of the previous dependency line."
877 (let ((here (point)))
879 (if (re-search-backward makefile-dependency-regex
(point-min) t
)
880 (progn (beginning-of-line) t
) ; indicate success
881 (goto-char here
) nil
)))
885 ;;; Electric keys. Blech.
887 (defun makefile-electric-dot (arg)
888 "Prompt for the name of a special target to insert.
889 Only does electric insertion at beginning of line.
890 Anywhere else just self-inserts."
893 (makefile-insert-special-target)
894 (self-insert-command arg
)))
896 (defun makefile-insert-special-target ()
897 "Prompt for and insert a special target name.
898 Uses `makefile-special-targets' list."
900 (makefile-pickup-targets)
901 (let ((special-target
902 (completing-read "Special target: "
903 makefile-special-targets-list nil nil nil
)))
904 (if (zerop (length special-target
))
906 (insert "." special-target
":")
907 (makefile-forward-after-target-colon))))
909 (defun makefile-electric-equal (arg)
910 "Prompt for name of a macro to insert.
911 Only does prompting if point is at beginning of line.
912 Anywhere else just self-inserts."
914 (makefile-pickup-macros)
916 (call-interactively 'makefile-insert-macro
)
917 (self-insert-command arg
)))
919 (defun makefile-insert-macro (macro-name)
920 "Prepare definition of a new macro."
921 (interactive "sMacro Name: ")
922 (makefile-pickup-macros)
923 (if (not (zerop (length macro-name
)))
926 (insert macro-name makefile-macro-assign
)
927 (setq makefile-need-macro-pickup t
)
928 (makefile-remember-macro macro-name
))))
930 (defun makefile-insert-macro-ref (macro-name)
931 "Complete on a list of known macros, then insert complete ref at point."
935 (makefile-pickup-macros)
936 (completing-read "Refer to macro: " makefile-macro-table nil nil nil
))))
937 (makefile-do-macro-insertion macro-name
))
939 (defun makefile-insert-target (target-name)
940 "Prepare definition of a new target (dependency line)."
941 (interactive "sTarget: ")
942 (if (not (zerop (length target-name
)))
945 (insert target-name makefile-target-colon
)
946 (makefile-forward-after-target-colon)
948 (setq makefile-need-target-pickup t
)
949 (makefile-remember-target target-name
))))
951 (defun makefile-insert-target-ref (target-name)
952 "Complete on a list of known targets, then insert TARGET-NAME at point."
956 (makefile-pickup-targets)
957 (completing-read "Refer to target: " makefile-target-table nil nil nil
))))
958 (if (not (zerop (length target-name
)))
959 (insert target-name
" ")))
961 (defun makefile-electric-colon (arg)
962 "Prompt for name of new target.
963 Prompting only happens at beginning of line.
964 Anywhere else just self-inserts."
967 (call-interactively 'makefile-insert-target
)
968 (self-insert-command arg
)))
972 ;;; ------------------------------------------------------------
973 ;;; Extracting targets and macros from an existing makefile
974 ;;; ------------------------------------------------------------
976 (defun makefile-pickup-targets ()
977 "Notice names of all target definitions in Makefile."
979 (if (not makefile-need-target-pickup
)
981 (setq makefile-need-target-pickup nil
)
982 (setq makefile-target-table nil
)
983 (setq makefile-has-prereqs nil
)
985 (goto-char (point-min))
986 (while (re-search-forward makefile-dependency-regex nil t
)
987 (makefile-add-this-line-targets)))
988 (message "Read targets OK.")))
990 (defun makefile-add-this-line-targets ()
993 (let ((done-with-line nil
)
994 (line-number (1+ (count-lines (point-min) (point)))))
995 (while (not done-with-line
)
996 (skip-chars-forward " \t")
997 (if (not (setq done-with-line
(or (eolp)
998 (char-equal (char-after (point)) ?
:))))
1000 (let* ((start-of-target-name (point))
1003 (skip-chars-forward "^ \t:#")
1004 (buffer-substring start-of-target-name
(point))))
1006 (not (looking-at ":[ \t]*$"))))
1007 (if (makefile-remember-target target-name has-prereqs
)
1008 (message "Picked up target \"%s\" from line %d"
1009 target-name line-number
)))))))))
1011 (defun makefile-pickup-macros ()
1012 "Notice names of all macro definitions in Makefile."
1014 (if (not makefile-need-macro-pickup
)
1016 (setq makefile-need-macro-pickup nil
)
1017 (setq makefile-macro-table nil
)
1019 (goto-char (point-min))
1020 (while (re-search-forward makefile-macroassign-regex nil t
)
1021 (makefile-add-this-line-macro)
1023 (message "Read macros OK.")))
1025 (defun makefile-add-this-line-macro ()
1028 (skip-chars-forward " \t")
1030 (let* ((start-of-macro-name (point))
1031 (line-number (1+ (count-lines (point-min) (point))))
1033 (skip-chars-forward "^ \t:#=*")
1034 (buffer-substring start-of-macro-name
(point)))))
1035 (if (makefile-remember-macro macro-name
)
1036 (message "Picked up macro \"%s\" from line %d"
1037 macro-name line-number
))))))
1039 (defun makefile-pickup-everything (arg)
1040 "Notice names of all macros and targets in Makefile.
1041 Prefix arg means force pickups to be redone."
1045 (setq makefile-need-target-pickup t
)
1046 (setq makefile-need-macro-pickup t
)))
1047 (makefile-pickup-macros)
1048 (makefile-pickup-targets)
1049 (if makefile-pickup-everything-picks-up-filenames-p
1050 (makefile-pickup-filenames-as-targets)))
1052 (defun makefile-pickup-filenames-as-targets ()
1053 "Scan the current directory for filenames to use as targets.
1054 Checks each filename against `makefile-ignored-files-in-pickup-regex'
1055 and adds all qualifying names to the list of known targets."
1057 (let* ((dir (file-name-directory (buffer-file-name)))
1058 (raw-filename-list (if dir
1059 (file-name-all-completions "" dir
)
1060 (file-name-all-completions "" ""))))
1061 (mapcar (lambda (name)
1062 (if (and (not (file-directory-p name
))
1063 (not (string-match makefile-ignored-files-in-pickup-regex
1065 (if (makefile-remember-target name
)
1066 (message "Picked up file \"%s\" as target" name
))))
1067 raw-filename-list
)))
1073 (defun makefile-complete ()
1074 "Perform completion on Makefile construct preceding point.
1075 Can complete variable and target names.
1076 The context determines which are considered."
1078 (let* ((beg (save-excursion
1079 (skip-chars-backward "^$(){}:#= \t\n")
1081 (try (buffer-substring beg
(point)))
1087 (let ((pc (preceding-char)))
1089 ;; Beginning of line means anything.
1093 ;; Preceding "$" means macros only.
1097 ;; Preceding "$(" or "${" means macros only.
1104 (= (preceding-char) ?$
))))
1105 (setq do-macros t
)))))
1108 (let* ((table (append (if do-macros
1110 makefile-target-table
)
1111 makefile-macro-table
))
1112 (completion (try-completion try table
)))
1114 ;; Exact match, so insert closing paren or colon.
1116 (insert (if do-macros
1128 (message "Can't find completion for \"%s\"" try
)
1131 ;; Partial completion.
1132 ((not (string= try completion
))
1133 ;; FIXME it would be nice to supply the closing paren if an
1134 ;; exact, unambiguous match were found. That is not possible
1135 ;; right now. Ditto closing ":" for targets.
1136 (delete-region beg
(point))
1138 ;; DO-MACROS means doing macros only. If not that, then check
1139 ;; to see if this completion is a macro. Special insertion
1140 ;; must be done for macros.
1142 (assoc completion makefile-macro-table
))
1143 (let ((makefile-use-curly-braces-for-macros-p
1145 makefile-use-curly-braces-for-macros-p
)))
1146 (delete-backward-char 2)
1147 (makefile-do-macro-insertion completion
)
1148 (delete-backward-char 1))
1150 ;; Just insert targets.
1151 (insert completion
)))
1153 ;; Can't complete any more, so make completion list. FIXME
1154 ;; this doesn't do the right thing when the completion is
1155 ;; actually inserted. I don't think there is an easy way to do
1158 (message "Making completion list...")
1159 (let ((list (all-completions try table
)))
1160 (with-output-to-temp-buffer "*Completions*"
1161 (display-completion-list list
)))
1162 (message "Making completion list...done"))))))
1166 ;; Backslashification. Stolen from cc-mode.el.
1168 (defun makefile-backslash-region (from to delete-flag
)
1169 "Insert, align, or delete end-of-line backslashes on the lines in the region.
1170 With no argument, inserts backslashes and aligns existing backslashes.
1171 With an argument, deletes the backslashes.
1173 This function does not modify the last line of the region if the region ends
1174 right at the start of the following line; it does not modify blank lines
1175 at the start of the region. So you can put the region around an entire macro
1176 definition and conveniently use this command."
1177 (interactive "r\nP")
1180 (let ((column makefile-backslash-column
)
1181 (endmark (make-marker)))
1182 (move-marker endmark to
)
1183 ;; Compute the smallest column number past the ends of all the lines.
1184 (if makefile-backslash-align
1186 (if (not delete-flag
)
1187 (while (< (point) to
)
1189 (if (= (preceding-char) ?
\\)
1190 (progn (forward-char -
1)
1191 (skip-chars-backward " \t")))
1192 (setq column
(max column
(1+ (current-column))))
1194 ;; Adjust upward to a tab column, if that doesn't push
1196 (if (> (% column tab-width
) 0)
1197 (let ((adjusted (* (/ (+ column tab-width -
1) tab-width
)
1199 (if (< adjusted
(window-width))
1200 (setq column adjusted
))))))
1201 ;; Don't modify blank lines at start of region.
1203 (while (and (< (point) endmark
) (eolp))
1205 ;; Add or remove backslashes on all the lines.
1206 (while (and (< (point) endmark
)
1207 ;; Don't backslashify the last line
1208 ;; if the region ends right at the start of the next line.
1211 (< (point) endmark
)))
1212 (if (not delete-flag
)
1213 (makefile-append-backslash column
)
1214 (makefile-delete-backslash))
1216 (move-marker endmark nil
))))
1218 (defun makefile-append-backslash (column)
1220 ;; Note that "\\\\" is needed to get one backslash.
1221 (if (= (preceding-char) ?
\\)
1222 (progn (forward-char -
1)
1223 (delete-horizontal-space)
1224 (indent-to column
(if makefile-backslash-align nil
1)))
1225 (indent-to column
(if makefile-backslash-align nil
1))
1228 (defun makefile-delete-backslash ()
1233 (if (looking-at "\\\\")
1234 (delete-region (1+ (point))
1235 (progn (skip-chars-backward " \t") (point)))))))
1241 (defun makefile-fill-paragraph (arg)
1242 ;; Fill comments, backslashed lines, and variable definitions
1248 ;; Found a comment. Set the fill prefix, and find the paragraph
1249 ;; boundaries by searching for lines that look like comment-only
1251 (let ((fill-prefix (match-string-no-properties 0))
1252 (fill-paragraph-function nil
))
1256 ;; Search backwards.
1258 (while (and (zerop (forward-line -
1))
1260 ;; We may have gone too far. Go forward again.
1261 (or (looking-at "^#")
1266 (while (looking-at "^#")
1269 (fill-paragraph nil
)
1272 ;; Must look for backslashed-region before looking for variable
1274 ((or (eq (char-before (line-end-position 1)) ?
\\)
1275 (eq (char-before (line-end-position 0)) ?
\\))
1276 ;; A backslash region. Find beginning and end, remove
1277 ;; backslashes, fill, and then reapply backslahes.
1282 (while (= (preceding-char) ?
\\)
1288 (while (= (preceding-char) ?
\\)
1292 (narrow-to-region beginning end
)
1293 (makefile-backslash-region (point-min) (point-max) t
)
1294 (let ((fill-paragraph-function nil
))
1295 (fill-paragraph nil
))
1296 (makefile-backslash-region (point-min) (point-max) nil
)
1297 (goto-char (point-max))
1298 (if (< (skip-chars-backward "\n") 0)
1299 (delete-region (point) (point-max))))))
1301 ((looking-at makefile-macroassign-regex
)
1302 ;; Have a macro assign. Fill just this line, and then backslash
1303 ;; resulting region.
1305 (narrow-to-region (point) (line-beginning-position 2))
1306 (let ((fill-paragraph-function nil
))
1307 (fill-paragraph nil
))
1308 (makefile-backslash-region (point-min) (point-max) nil
)))))
1310 ;; Always return non-nil so we don't fill anything else.
1315 ;;; ------------------------------------------------------------
1317 ;;; ------------------------------------------------------------
1319 (defun makefile-browser-format-target-line (target selected
)
1321 (concat (make-string makefile-browser-leftmost-column ?\
)
1323 makefile-browser-selected-mark
1324 makefile-browser-unselected-mark
)
1326 target makefile-target-colon
))
1328 (defun makefile-browser-format-macro-line (macro selected
)
1330 (concat (make-string makefile-browser-leftmost-column ?\
)
1332 makefile-browser-selected-mark
1333 makefile-browser-unselected-mark
)
1334 (makefile-format-macro-ref macro
))))
1336 (defun makefile-browser-fill (targets macros
)
1337 (let ((inhibit-read-only t
))
1338 (goto-char (point-min))
1342 (lambda (item) (insert (makefile-browser-format-target-line (car item
) nil
) "\n")))
1347 (lambda (item) (insert (makefile-browser-format-macro-line (car item
) nil
) "\n")))
1350 (sort-lines nil
(point-min) (point-max))
1351 (goto-char (1- (point-max)))
1352 (delete-char 1) ; remove unnecessary newline at eob
1353 (goto-char (point-min))
1354 (forward-char makefile-browser-cursor-column
)))
1357 ;;; Moving up and down in the browser
1360 (defun makefile-browser-next-line ()
1361 "Move the browser selection cursor to the next line."
1363 (if (not (makefile-last-line-p))
1366 (forward-char makefile-browser-cursor-column
))))
1368 (defun makefile-browser-previous-line ()
1369 "Move the browser selection cursor to the previous line."
1371 (if (not (makefile-first-line-p))
1374 (forward-char makefile-browser-cursor-column
))))
1377 ;;; Quitting the browser (returns to client buffer)
1380 (defun makefile-browser-quit ()
1381 "Leave the browser and return to the makefile buffer."
1383 (let ((my-client makefile-browser-client
))
1384 (setq makefile-browser-client nil
) ; we quitted, so NO client!
1385 (set-buffer-modified-p nil
)
1387 (pop-to-buffer my-client
)))
1390 ;;; Toggle state of a browser item
1393 (defun makefile-browser-toggle ()
1394 "Toggle the selection state of the browser item at the cursor position."
1396 (let ((this-line (count-lines (point-min) (point))))
1397 (setq this-line
(max 1 this-line
))
1398 (makefile-browser-toggle-state-for-line this-line
)
1399 (goto-line this-line
)
1400 (let ((inhibit-read-only t
))
1402 (if (makefile-browser-on-macro-line-p)
1403 (let ((macro-name (makefile-browser-this-line-macro-name)))
1404 (delete-region (point) (progn (end-of-line) (point)))
1406 (makefile-browser-format-macro-line
1408 (makefile-browser-get-state-for-line this-line
))))
1409 (let ((target-name (makefile-browser-this-line-target-name)))
1410 (delete-region (point) (progn (end-of-line) (point)))
1412 (makefile-browser-format-target-line
1414 (makefile-browser-get-state-for-line this-line
))))))
1416 (forward-char makefile-browser-cursor-column
)
1417 (if makefile-browser-auto-advance-after-selection-p
1418 (makefile-browser-next-line))))
1421 ;;; Making insertions into the client buffer
1424 (defun makefile-browser-insert-continuation ()
1425 "Insert a makefile continuation.
1426 In the makefile buffer, go to (end-of-line), insert a \'\\\'
1427 character, insert a new blank line, go to that line and indent by one TAB.
1428 This is most useful in the process of creating continued lines when copying
1429 large dependencies from the browser to the client buffer.
1430 \(point) advances accordingly in the client buffer."
1432 (with-current-buffer makefile-browser-client
1436 (defun makefile-browser-insert-selection ()
1437 "Insert all selected targets and/or macros in the makefile buffer.
1438 Insertion takes place at point."
1442 (let ((current-line 1))
1444 (if (makefile-browser-get-state-for-line current-line
)
1445 (makefile-browser-send-this-line-item))
1447 (setq current-line
(1+ current-line
))))))
1449 (defun makefile-browser-insert-selection-and-quit ()
1451 (makefile-browser-insert-selection)
1452 (makefile-browser-quit))
1454 (defun makefile-browser-send-this-line-item ()
1455 (if (makefile-browser-on-macro-line-p)
1457 (let ((macro-name (makefile-browser-this-line-macro-name)))
1458 (set-buffer makefile-browser-client
)
1459 (insert (makefile-format-macro-ref macro-name
) " ")))
1461 (let ((target-name (makefile-browser-this-line-target-name)))
1462 (set-buffer makefile-browser-client
)
1463 (insert target-name
" ")))))
1465 (defun makefile-browser-start-interaction ()
1466 (use-local-map makefile-browser-map
)
1467 (setq buffer-read-only t
))
1469 (defun makefile-browse (targets macros
)
1471 (if (zerop (+ (length targets
) (length macros
)))
1474 (message "No macros or targets to browse! Consider running 'makefile-pickup-everything\'"))
1475 (let ((browser-buffer (get-buffer-create makefile-browser-buffer-name
)))
1476 (pop-to-buffer browser-buffer
)
1477 (makefile-browser-fill targets macros
)
1478 (shrink-window-if-larger-than-buffer)
1479 (set (make-local-variable 'makefile-browser-selection-vector
)
1480 (make-vector (+ (length targets
) (length macros
)) nil
))
1481 (makefile-browser-start-interaction))))
1483 (defun makefile-switch-to-browser ()
1485 (run-hooks 'makefile-browser-hook
)
1486 (setq makefile-browser-client
(current-buffer))
1487 (makefile-pickup-targets)
1488 (makefile-pickup-macros)
1489 (makefile-browse makefile-target-table makefile-macro-table
))
1493 ;;; ------------------------------------------------------------
1494 ;;; Up-to-date overview buffer
1495 ;;; ------------------------------------------------------------
1497 (defun makefile-create-up-to-date-overview ()
1498 "Create a buffer containing an overview of the state of all known targets.
1499 Known targets are targets that are explicitly defined in that makefile;
1500 in other words, all targets that appear on the left hand side of a
1501 dependency in the makefile."
1503 (if (y-or-n-p "Are you sure that the makefile being edited is consistent? ")
1505 ;; The rest of this function operates on a temporary makefile, created by
1506 ;; writing the current contents of the makefile buffer.
1508 (let ((saved-target-table makefile-target-table
)
1509 (this-buffer (current-buffer))
1510 (makefile-up-to-date-buffer
1511 (get-buffer-create makefile-up-to-date-buffer-name
))
1512 (filename (makefile-save-temporary))
1514 ;; Forget the target table because it may contain picked-up filenames
1515 ;; that are not really targets in the current makefile.
1516 ;; We don't want to query these, so get a new target-table with just the
1517 ;; targets that can be found in the makefile buffer.
1518 ;; The 'old' target table will be restored later.
1520 (real-targets (progn
1521 (makefile-pickup-targets)
1522 makefile-target-table
))
1523 (prereqs makefile-has-prereqs
)
1526 (set-buffer makefile-up-to-date-buffer
)
1527 (setq buffer-read-only nil
)
1529 (makefile-query-targets filename real-targets prereqs
)
1530 (if (zerop (buffer-size)) ; if it did not get us anything
1532 (kill-buffer (current-buffer))
1533 (message "No overview created!")))
1534 (set-buffer this-buffer
)
1535 (setq makefile-target-table saved-target-table
)
1536 (if (get-buffer makefile-up-to-date-buffer-name
)
1538 (pop-to-buffer (get-buffer makefile-up-to-date-buffer-name
))
1539 (shrink-window-if-larger-than-buffer)
1540 (sort-lines nil
(point-min) (point-max))
1541 (setq buffer-read-only t
))))))
1543 (defun makefile-save-temporary ()
1544 "Create a temporary file from the current makefile buffer."
1545 (let ((filename (makefile-generate-temporary-filename)))
1546 (write-region (point-min) (point-max) filename nil
0)
1547 filename
)) ; return the filename
1549 (defun makefile-generate-temporary-filename ()
1550 "Create a filename suitable for use in `makefile-save-temporary'.
1551 Be careful to allow brain-dead file systems (DOS, SYSV ...) to cope
1552 with the generated name!"
1553 (let ((my-name (user-login-name))
1554 (my-uid (int-to-string (user-uid))))
1556 (if (> (length my-name
) 3)
1557 (substring my-name
0 3)
1560 (if (> (length my-uid
) 3)
1561 (substring my-uid
0 3)
1564 (defun makefile-query-targets (filename target-table prereq-list
)
1565 "Fill the up-to-date overview buffer.
1566 Checks each target in TARGET-TABLE using `makefile-query-one-target-method'
1567 and generates the overview, one line per target name."
1570 (function (lambda (item)
1571 (let* ((target-name (car item
))
1572 (no-prereqs (not (member target-name prereq-list
)))
1573 (needs-rebuild (or no-prereqs
1575 makefile-query-one-target-method
1580 (cond (no-prereqs " .. has no prerequisites")
1581 (needs-rebuild " .. NEEDS REBUILD")
1582 (t " .. is up to date"))))
1585 (goto-char (point-min))
1586 (delete-file filename
)) ; remove the tmpfile
1588 (defun makefile-query-by-make-minus-q (target &optional filename
)
1590 (call-process makefile-brave-make nil nil nil
1591 "-f" filename
"-q" target
))))
1595 ;;; ------------------------------------------------------------
1596 ;;; Continuation cleanup
1597 ;;; ------------------------------------------------------------
1599 (defun makefile-cleanup-continuations ()
1600 (if (eq major-mode
'makefile-mode
)
1601 (if (and makefile-cleanup-continuations
1602 (not buffer-read-only
))
1604 (goto-char (point-min))
1605 (while (re-search-forward "\\\\[ \t]+$" nil t
)
1606 (replace-match "\\" t t
))))))
1609 ;;; ------------------------------------------------------------
1610 ;;; Warn of suspicious lines
1611 ;;; ------------------------------------------------------------
1613 (defun makefile-warn-suspicious-lines ()
1614 ;; Returning non-nil cancels the save operation
1615 (if (eq major-mode
'makefile-mode
)
1617 (goto-char (point-min))
1618 (if (re-search-forward "^\\(\t+$\\| +\t\\)" nil t
)
1620 (format "Suspicious line %d. Save anyway? "
1621 (count-lines (point-min) (point)))))))))
1623 (defun makefile-warn-continuations ()
1624 (if (eq major-mode
'makefile-mode
)
1626 (goto-char (point-min))
1627 (if (re-search-forward "\\\\[ \t]+$" nil t
)
1629 (format "Suspicious continuation in line %d. Save anyway? "
1630 (count-lines (point-min) (point)))))))))
1633 ;;; ------------------------------------------------------------
1634 ;;; GNU make function support
1635 ;;; ------------------------------------------------------------
1637 (defun makefile-insert-gmake-function ()
1638 "Insert a GNU make function call.
1639 Asks for the name of the function to use (with completion).
1640 Then prompts for all required parameters."
1642 (let* ((gm-function-name (completing-read
1644 makefile-gnumake-functions-alist
1646 (gm-function-prompts
1647 (cdr (assoc gm-function-name makefile-gnumake-functions-alist
))))
1648 (if (not (zerop (length gm-function-name
)))
1649 (insert (makefile-format-macro-ref
1650 (concat gm-function-name
" "
1651 (makefile-prompt-for-gmake-funargs
1652 gm-function-name gm-function-prompts
)))
1655 (defun makefile-prompt-for-gmake-funargs (function-name prompt-list
)
1657 (function (lambda (one-prompt)
1658 (read-string (format "[%s] %s: " function-name one-prompt
)
1665 ;;; ------------------------------------------------------------
1666 ;;; Utility functions
1667 ;;; ------------------------------------------------------------
1669 (defun makefile-match-function-end (end)
1670 "To be called as an anchored matcher by font-lock.
1671 The anchor must have matched the opening parens in the first group."
1672 (let ((s (match-string-no-properties 1)))
1673 (setq s
(cond ((string= s
"(") "\\(.*?\\)[ \t]*)")
1674 ((string= s
"{") "\\(.*?\\)[ \t]*}")
1675 ((string= s
"((") "\\(.*?\\)[ \t]*))")
1676 ((string= s
"{{") "\\(.*?\\)[ \t]*}}")))
1677 (if s
(looking-at s
))))
1679 (defun makefile-do-macro-insertion (macro-name)
1680 "Insert a macro reference."
1681 (if (not (zerop (length macro-name
)))
1682 (if (assoc macro-name makefile-runtime-macros-list
)
1683 (insert "$" macro-name
)
1684 (insert (makefile-format-macro-ref macro-name
)))))
1686 (defun makefile-remember-target (target-name &optional has-prereqs
)
1687 "Remember a given target if it is not already remembered for this buffer."
1688 (if (not (zerop (length target-name
)))
1690 (if (not (assoc target-name makefile-target-table
))
1691 (setq makefile-target-table
1692 (cons (list target-name
) makefile-target-table
)))
1694 (setq makefile-has-prereqs
1695 (cons target-name makefile-has-prereqs
))))))
1697 (defun makefile-remember-macro (macro-name)
1698 "Remember a given macro if it is not already remembered for this buffer."
1699 (if (not (zerop (length macro-name
)))
1700 (if (not (assoc macro-name makefile-macro-table
))
1701 (setq makefile-macro-table
1702 (cons (list macro-name
) makefile-macro-table
)))))
1704 (defun makefile-forward-after-target-colon ()
1705 "Move point forward after inserting the terminating colon of a target.
1706 This acts according to the value of `makefile-tab-after-target-colon'."
1707 (if makefile-tab-after-target-colon
1711 (defun makefile-browser-on-macro-line-p ()
1712 "Determine if point is on a macro line in the browser."
1715 (re-search-forward "\\$[{(]" (line-end-position) t
)))
1717 (defun makefile-browser-this-line-target-name ()
1718 "Extract the target name from a line in the browser."
1721 (skip-chars-backward "^ \t")
1722 (buffer-substring (point) (1- (line-end-position)))))
1724 (defun makefile-browser-this-line-macro-name ()
1725 "Extract the macro name from a line in the browser."
1728 (re-search-forward "\\$[{(]" (line-end-position) t
)
1729 (let ((macro-start (point)))
1730 (skip-chars-forward "^})")
1731 (buffer-substring macro-start
(point)))))
1733 (defun makefile-format-macro-ref (macro-name)
1734 "Format a macro reference.
1735 Uses `makefile-use-curly-braces-for-macros-p'."
1736 (if (or (char-equal ?\
( (string-to-char macro-name
))
1737 (char-equal ?\
{ (string-to-char macro-name
)))
1738 (format "$%s" macro-name
)
1739 (if makefile-use-curly-braces-for-macros-p
1740 (format "${%s}" macro-name
)
1741 (format "$(%s)" macro-name
))))
1743 (defun makefile-browser-get-state-for-line (n)
1744 (aref makefile-browser-selection-vector
(1- n
)))
1746 (defun makefile-browser-set-state-for-line (n to-state
)
1747 (aset makefile-browser-selection-vector
(1- n
) to-state
))
1749 (defun makefile-browser-toggle-state-for-line (n)
1750 (makefile-browser-set-state-for-line n
(not (makefile-browser-get-state-for-line n
))))
1752 (defun makefile-last-line-p ()
1753 (= (line-end-position) (point-max)))
1755 (defun makefile-first-line-p ()
1756 (= (line-beginning-position) (point-min)))
1760 ;;; Support for other packages, like add-log.
1762 (defun makefile-add-log-defun ()
1763 "Return name of target or variable assignment that point is in.
1764 If it isn't in one, return nil."
1768 ;; Scan back line by line, noticing when we come to a
1769 ;; variable or rule definition, and giving up when we see
1770 ;; a line that is not part of either of those.
1771 (while (not (or (setq found
1772 (when (or (looking-at makefile-macroassign-regex
)
1773 (looking-at makefile-dependency-regex
))
1774 (match-string-no-properties 1)))
1775 ;; Don't keep looking across a blank line or comment.
1776 (looking-at "$\\|#")
1777 (not (zerop (forward-line -
1))))))
1780 (provide 'make-mode
)
1782 ;;; arch-tag: bd23545a-de91-44fb-b1b2-feafbb2635a0
1783 ;;; make-mode.el ends here