(latexenc-find-file-coding-system): Don't inherit the EOL part of the
[emacs.git] / lisp / progmodes / make-mode.el
blob7356583fb9087f4b03f9a85d7d4b7a4b8fc2fa88
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>
7 ;; Maintainer: FSF
8 ;; Adapted-By: ESR
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)
16 ;; any later version.
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.
28 ;;; Commentary:
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
47 ;; prerequisites.
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.
59 ;; To Do:
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.
75 ;; Should at least:
76 ;; * Act more like dired/buffer menu/whatever.
77 ;; * Highlight as mouse traverses.
78 ;; * B2 inserts.
79 ;; * Update documentation above.
80 ;; * Update texinfo manual.
81 ;; * Update files.el.
85 ;;; Code:
87 ;; Sadly we need this for a macro.
88 (eval-when-compile
89 (require 'imenu)
90 (require 'dabbrev)
91 (require 'add-log))
93 ;;; ------------------------------------------------------------
94 ;;; Configurable stuff
95 ;;; ------------------------------------------------------------
97 (defgroup makefile nil
98 "Makefile editing commands for Emacs."
99 :group 'tools
100 :prefix "makefile-")
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."
106 :group 'faces
107 :group 'makefile)
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."
113 :group 'faces
114 :group 'makefile
115 :version "22.1")
117 (defface makefile-shell-face
118 '((((class color) (background light)) (:background "seashell1"))
119 (((class color) (background dark)) (:background "seashell4"))
120 (t (:reverse-video t)))
121 "Face to use for additionally highlighting Shell commands in Font-Lock mode."
122 :group 'faces
123 :group 'makefile
124 :version "22.1")
126 (defface makefile-makepp-perl-face
127 '((((class color) (background light)) (:background "LightBlue1")) ; Camel Book
128 (((class color) (background dark)) (:background "DarkBlue"))
129 (t (:reverse-video t)))
130 "Face to use for additionally highlighting Perl code in Font-Lock mode."
131 :group 'faces
132 :group 'makefile
133 :version "22.1")
135 (defcustom makefile-browser-buffer-name "*Macros and Targets*"
136 "*Name of the macro- and target browser buffer."
137 :type 'string
138 :group 'makefile)
140 (defcustom makefile-target-colon ":"
141 "*String to append to all target names inserted by `makefile-insert-target'.
142 \":\" or \"::\" are common values."
143 :type 'string
144 :group 'makefile)
146 (defcustom makefile-macro-assign " = "
147 "*String to append to all macro names inserted by `makefile-insert-macro'.
148 The normal value should be \" = \", since this is what
149 standard make expects. However, newer makes such as dmake
150 allow a larger variety of different macro assignments, so you
151 might prefer to use \" += \" or \" := \" ."
152 :type 'string
153 :group 'makefile)
155 (defcustom makefile-electric-keys nil
156 "*If non-nil, Makefile mode should install electric keybindings.
157 Default is nil."
158 :type 'boolean
159 :group 'makefile)
161 (defcustom makefile-use-curly-braces-for-macros-p nil
162 "*Controls the style of generated macro references.
163 Non-nil means macro references should use curly braces, like `${this}'.
164 nil means use parentheses, like `$(this)'."
165 :type 'boolean
166 :group 'makefile)
168 (defcustom makefile-tab-after-target-colon t
169 "*If non-nil, insert a TAB after a target colon.
170 Otherwise, a space is inserted.
171 The default is t."
172 :type 'boolean
173 :group 'makefile)
175 (defcustom makefile-browser-leftmost-column 10
176 "*Number of blanks to the left of the browser selection mark."
177 :type 'integer
178 :group 'makefile)
180 (defcustom makefile-browser-cursor-column 10
181 "*Column the cursor goes to when it moves up or down in the Makefile browser."
182 :type 'integer
183 :group 'makefile)
185 (defcustom makefile-backslash-column 48
186 "*Column in which `makefile-backslash-region' inserts backslashes."
187 :type 'integer
188 :group 'makefile)
190 (defcustom makefile-backslash-align t
191 "*If non-nil, `makefile-backslash-region' will align backslashes."
192 :type 'boolean
193 :group 'makefile)
195 (defcustom makefile-browser-selected-mark "+ "
196 "*String used to mark selected entries in the Makefile browser."
197 :type 'string
198 :group 'makefile)
200 (defcustom makefile-browser-unselected-mark " "
201 "*String used to mark unselected entries in the Makefile browser."
202 :type 'string
203 :group 'makefile)
205 (defcustom makefile-browser-auto-advance-after-selection-p t
206 "*If non-nil, cursor will move after item is selected in Makefile browser."
207 :type 'boolean
208 :group 'makefile)
210 (defcustom makefile-pickup-everything-picks-up-filenames-p nil
211 "*If non-nil, `makefile-pickup-everything' picks up filenames as targets.
212 This means it calls `makefile-pickup-filenames-as-targets'.
213 Otherwise filenames are omitted."
214 :type 'boolean
215 :group 'makefile)
217 (defcustom makefile-cleanup-continuations nil
218 "*If non-nil, automatically clean up continuation lines when saving.
219 A line is cleaned up by removing all whitespace following a trailing
220 backslash. This is done silently.
221 IMPORTANT: Please note that enabling this option causes Makefile mode
222 to MODIFY A FILE WITHOUT YOUR CONFIRMATION when \"it seems necessary\"."
223 :type 'boolean
224 :group 'makefile)
226 (defcustom makefile-mode-hook nil
227 "*Normal hook run by `makefile-mode'."
228 :type 'hook
229 :group 'makefile)
231 (defvar makefile-browser-hook '())
234 ;; Special targets for DMake, Sun's make ...
236 (defcustom makefile-special-targets-list
237 '(("DEFAULT") ("DONE") ("ERROR") ("EXPORT")
238 ("FAILED") ("GROUPEPILOG") ("GROUPPROLOG") ("IGNORE")
239 ("IMPORT") ("INCLUDE") ("INCLUDEDIRS") ("INIT")
240 ("KEEP_STATE") ("MAKEFILES") ("MAKE_VERSION") ("NO_PARALLEL")
241 ("PARALLEL") ("PHONY") ("PRECIOUS") ("REMOVE")
242 ("SCCS_GET") ("SILENT") ("SOURCE") ("SUFFIXES")
243 ("WAIT") ("c.o") ("C.o") ("m.o")
244 ("el.elc") ("y.c") ("s.o"))
245 "*List of special targets.
246 You will be offered to complete on one of those in the minibuffer whenever
247 you enter a \".\" at the beginning of a line in `makefile-mode'."
248 :type '(repeat (list string))
249 :group 'makefile)
251 (defcustom makefile-runtime-macros-list
252 '(("@") ("&") (">") ("<") ("*") ("^") ("+") ("?") ("%") ("$"))
253 "*List of macros that are resolved by make at runtime.
254 If you insert a macro reference using `makefile-insert-macro-ref', the name
255 of the macro is checked against this list. If it can be found its name will
256 not be enclosed in { } or ( )."
257 :type '(repeat (list string))
258 :group 'makefile)
260 ;; Note that the first big subexpression is used by font lock. Note
261 ;; that if you change this regexp you might have to fix the imenu
262 ;; index in makefile-imenu-generic-expression.
263 (defvar makefile-dependency-regex
264 ;; Allow for two nested levels $(v1:$(v2:$(v3:a=b)=c)=d)
265 "^ *\\(\\(?: *\\$\\(?:[({]\\(?:\\$\\(?:[({]\\(?:\\$\\(?:[^({]\\|.[^\n$#})]+?[})]\\)\\|[^\n$#)}]\\)+?[})]\\|[^({]\\)\\|[^\n$#)}]\\)+?[})]\\|[^({]\\)\\| *[^ \n$#:=]+\\)+?\\)[ \t]*\\(:\\)\\(?:[ \t]*$\\|[^=\n]\\(?:[^#\n]*?;[ \t]*\\(.+\\)\\)?\\)"
266 "Regex used to find dependency lines in a makefile.")
268 (defconst makefile-bsdmake-dependency-regex
269 (progn (string-match (regexp-quote "\\(:\\)") makefile-dependency-regex)
270 (replace-match "\\([:!]\\)" t t makefile-dependency-regex))
271 "Regex used to find dependency lines in a BSD makefile.")
273 (defvar makefile-dependency-skip "^:"
274 "Characters to skip to find a line that might be a dependency.")
276 (defvar makefile-rule-action-regex
277 "^\t[ \t]*\\([-@]*\\)[ \t]*\\(\\(?:.*\\\\\n\\)*.*\\)"
278 "Regex used to highlight rule action lines in font lock mode.")
280 (defconst makefile-makepp-rule-action-regex
281 ;; Don't care about initial tab, but I don't know how to font-lock correctly without.
282 "^\t[ \t]*\\(\\(?:\\(?:noecho\\|ignore[-_]error\\|[-@]+\\)[ \t]*\\)*\\)\\(\\(&\\S +\\)?\\(?:.*\\\\\n\\)*.*\\)"
283 "Regex used to highlight makepp rule action lines in font lock mode.")
285 (defconst makefile-bsdmake-rule-action-regex
286 (progn (string-match "-@" makefile-rule-action-regex)
287 (replace-match "-+@" t t makefile-rule-action-regex))
288 "Regex used to highlight BSD rule action lines in font lock mode.")
290 ;; Note that the first and second subexpression is used by font lock. Note
291 ;; that if you change this regexp you might have to fix the imenu index in
292 ;; makefile-imenu-generic-expression.
293 (defconst makefile-macroassign-regex
294 "^ *\\([^ \n\t][^:#= \t\n]*\\)[ \t]*\\(?:!=[ \t]*\\(\\(?:.+\\\\\n\\)*.+\\)\\|[*:+]?[:?]?=[ \t]*\\(\\(?:.*\\\\\n\\)*.*\\)\\)"
295 "Regex used to find macro assignment lines in a makefile.")
297 (defconst makefile-var-use-regex
298 "[^$]\\$[({]\\([-a-zA-Z0-9_.]+\\|[@%<?^+*][FD]?\\)"
299 "Regex used to find $(macro) uses in a makefile.")
301 (defconst makefile-ignored-files-in-pickup-regex
302 "\\(^\\..*\\)\\|\\(.*~$\\)\\|\\(.*,v$\\)\\|\\(\\.[chy]\\)"
303 "Regex for filenames that will NOT be included in the target list.")
305 (if (fboundp 'facemenu-unlisted-faces)
306 (add-to-list 'facemenu-unlisted-faces 'makefile-space-face))
307 (defvar makefile-space-face 'makefile-space-face
308 "Face to use for highlighting leading spaces in Font-Lock mode.")
310 ;; These lists were inspired by the old solution. But they are silly, because
311 ;; you can't differentiate what follows. They need to be split up.
312 (defconst makefile-statements '("include")
313 "List of keywords understood by standard make.")
315 (defconst makefile-automake-statements
316 `("if" "else" "endif" ,@makefile-statements)
317 "List of keywords understood by automake.")
319 (defconst makefile-gmake-statements
320 `("-sinclude" "sinclude" "override" "vpath"
321 "ifdef" "ifndef" "ifeq" "ifneq" "-include" "define" "endef" "export"
322 "unexport"
323 ,@(cdr makefile-automake-statements))
324 "List of keywords understood by gmake.")
326 ;; These are even more silly, because you can have more spaces in between.
327 (defconst makefile-makepp-statements
328 `("and ifdef" "and ifndef" "and ifeq" "and ifneq" "and ifperl"
329 "and ifmakeperl" "and ifsys" "and ifnsys" "build_cache" "build_check"
330 "else ifdef" "else ifndef" "else ifeq" "else ifneq" "else ifperl"
331 "else ifmakeperl" "else ifsys" "else ifnsys" "enddef" "load_makefile"
332 "ifperl" "ifmakeperl" "ifsys" "ifnsys" "_include" "makeperl" "makesub"
333 "no_implicit_load" "perl" "perl-begin" "perl_begin" "perl-end" "perl_end"
334 "prebuild" "or ifdef" "or ifndef" "or ifeq" "or ifneq" "or ifperl"
335 "or ifmakeperl" "or ifsys" "or ifnsys" "register_command_parser"
336 "register_scanner" "repository" "runtime" "signature" "sub"
337 ,@(nthcdr 4 makefile-gmake-statements))
338 "List of keywords understood by gmake.")
340 (defconst makefile-bsdmake-statements
341 `(".elif" ".elifdef" ".elifmake" ".elifndef" ".elifnmake" ".else" ".endfor"
342 ".endif" ".for" ".if" ".ifdef" ".ifmake" ".ifndef" ".ifnmake" ".undef")
343 "List of keywords understood by BSD make.")
345 (defun makefile-make-font-lock-keywords (var keywords space
346 &optional negation
347 &rest font-lock-keywords)
348 `(;; Do macro assignments. These get the "variable-name" face.
349 (,makefile-macroassign-regex
350 (1 font-lock-variable-name-face)
351 ;; This is for after !=
352 (2 'makefile-shell-face prepend t)
353 ;; This is for after normal assignment
354 (3 'font-lock-string-face prepend t))
356 ;; Rule actions.
357 (makefile-match-action
358 (1 font-lock-type-face)
359 (2 'makefile-shell-face prepend)
360 ;; Only makepp has builtin commands.
361 (3 font-lock-builtin-face prepend t))
363 ;; Variable references even in targets/strings/comments.
364 (,var 1 font-lock-variable-name-face prepend)
366 ;; Automatic variable references and single character variable references,
367 ;; but not shell variables references.
368 ("[^$]\\$\\([@%<?^+*_]\\|[a-zA-Z0-9]\\>\\)"
369 1 font-lock-constant-face prepend)
370 ("[^$]\\(\\$[@%*]\\)"
371 1 'makefile-targets-face prepend)
373 ;; Fontify conditionals and includes.
374 (,(concat "^\\(?: [ \t]*\\)?"
375 (regexp-opt keywords t)
376 "\\>[ \t]*\\([^: \t\n#]*\\)")
377 (1 font-lock-keyword-face) (2 font-lock-variable-name-face))
379 ,@(if negation
380 `((,negation (1 font-lock-negation-char-face prepend)
381 (2 font-lock-negation-char-face prepend t))))
383 ,@(if space
384 '(;; Highlight lines that contain just whitespace.
385 ;; They can cause trouble, especially if they start with a tab.
386 ("^[ \t]+$" . makefile-space-face)
388 ;; Highlight shell comments that Make treats as commands,
389 ;; since these can fool people.
390 ("^\t+#" 0 makefile-space-face t)
392 ;; Highlight spaces that precede tabs.
393 ;; They can make a tab fail to be effective.
394 ("^\\( +\\)\t" 1 makefile-space-face)))
396 ,@font-lock-keywords
398 ;; Do dependencies.
399 (makefile-match-dependency
400 (1 'makefile-targets-face prepend)
401 (3 'makefile-shell-face prepend t))))
403 (defconst makefile-font-lock-keywords
404 (makefile-make-font-lock-keywords
405 makefile-var-use-regex
406 makefile-statements
409 (defconst makefile-automake-font-lock-keywords
410 (makefile-make-font-lock-keywords
411 makefile-var-use-regex
412 makefile-automake-statements
415 (defconst makefile-gmake-font-lock-keywords
416 (makefile-make-font-lock-keywords
417 makefile-var-use-regex
418 makefile-gmake-statements
420 "^\\(?: [ \t]*\\)?if\\(n\\)\\(?:def\\|eq\\)\\>"
422 '("[^$]\\(\\$[({][@%*][DF][})]\\)"
423 1 'makefile-targets-face prepend)
425 ;; $(function ...) ${function ...}
426 '("[^$]\\$[({]\\([-a-zA-Z0-9_.]+\\s \\)"
427 1 font-lock-function-name-face prepend)
429 ;; $(shell ...) ${shell ...}
430 '("[^$]\\$\\([({]\\)shell[ \t]+"
431 makefile-match-function-end nil nil
432 (1 'makefile-shell-face prepend t))))
434 (defconst makefile-makepp-font-lock-keywords
435 (makefile-make-font-lock-keywords
436 makefile-var-use-regex
437 makefile-makepp-statements
439 "^\\(?: [ \t]*\\)?\\(?:and[ \t]+\\|else[ \t]+\\|or[ \t]+\\)?if\\(n\\)\\(?:def\\|eq\\|sys\\)\\>"
441 '("[^$]\\(\\$[({]\\(?:output\\|stem\\|target\\)s?\\_>.*?[})]\\)"
442 1 'makefile-targets-face prepend)
444 ;; Colon modifier keywords.
445 '("\\(:\\s *\\)\\(build_c\\(?:ache\\|heck\\)\\|env\\(?:ironment\\)?\\|foreach\\|signature\\|scanner\\|quickscan\\|smartscan\\)\\>\\([^:\n]*\\)"
446 (1 font-lock-type-face t)
447 (2 font-lock-keyword-face t)
448 (3 font-lock-variable-name-face t))
450 ;; $(function ...) $((function ...)) ${function ...} ${{function ...}}
451 '("[^$]\\$\\(?:((?\\|{{?\\)\\([-a-zA-Z0-9_.]+\\s \\)"
452 1 font-lock-function-name-face prepend)
454 ;; $(shell ...) $((shell ...)) ${shell ...} ${{shell ...}}
455 '("[^$]\\$\\(((?\\|{{?\\)shell\\(?:[-_]\\(?:global[-_]\\)?once\\)?[ \t]+"
456 makefile-match-function-end nil nil
457 (1 'makefile-shell-face prepend t))
459 ;; $(perl ...) $((perl ...)) ${perl ...} ${{perl ...}}
460 '("[^$]\\$\\(((?\\|{{?\\)makeperl[ \t]+"
461 makefile-match-function-end nil nil
462 (1 'makefile-makepp-perl-face prepend t))
463 '("[^$]\\$\\(((?\\|{{?\\)perl[ \t]+"
464 makefile-match-function-end nil nil
465 (1 'makefile-makepp-perl-face t t))
467 ;; Can we unify these with (if (match-end 1) 'prepend t)?
468 '("ifmakeperl\\s +\\(.*\\)" 1 'makefile-makepp-perl-face prepend)
469 '("ifperl\\s +\\(.*\\)" 1 'makefile-makepp-perl-face t)
471 ;; Perl block single- or multiline, as statement or rule action.
472 ;; Don't know why the initial newline in 2nd variant of group 2 doesn't get skipped.
473 '("\\<make\\(?:perl\\|sub\\s +\\S +\\)\\s *\n?\\s *{\\(?:{\\s *\n?\\(\\(?:.*\n\\)+?\\)\\s *}\\|\\s *\\(\\(?:.*?\\|\n?\\(?:.*\n\\)+?\\)\\)\\)}"
474 (1 'makefile-makepp-perl-face prepend t)
475 (2 'makefile-makepp-perl-face prepend t))
476 '("\\<\\(?:perl\\|sub\\s +\\S +\\)\\s *\n?\\s *{\\(?:{\\s *\n?\\(\\(?:.*\n\\)+?\\)\\s *}\\|\\s *\\(\\(?:.*?\\|\n?\\(?:.*\n\\)+?\\)\\)\\)}"
477 (1 'makefile-makepp-perl-face t t)
478 (2 'makefile-makepp-perl-face t t))
480 ;; Statement style perl block.
481 '("perl[-_]begin\\s *\\(?:\\s #.*\\)?\n\\(\\(?:.*\n\\)+?\\)\\s *perl[-_]end\\>"
482 1 'makefile-makepp-perl-face t)))
484 (defconst makefile-bsdmake-font-lock-keywords
485 (makefile-make-font-lock-keywords
486 ;; A lot more could be done for variables here:
487 makefile-var-use-regex
488 makefile-bsdmake-statements
490 "^\\(?: [ \t]*\\)?\\.\\(?:el\\)?if\\(n?\\)\\(?:def\\|make\\)?\\>[ \t]*\\(!?\\)"
491 '("^[ \t]*\\.for[ \t].+[ \t]\\(in\\)\\>" 1 font-lock-keyword-face)))
494 (defconst makefile-font-lock-syntactic-keywords
495 ;; From sh-script.el.
496 ;; A `#' begins a comment in sh when it is unquoted and at the beginning
497 ;; of a word. In the shell, words are separated by metacharacters.
498 ;; The list of special chars is taken from the single-unix spec of the
499 ;; shell command language (under `quoting') but with `$' removed.
500 '(("[^|&;<>()`\\\"' \t\n]\\(#+\\)" 1 "_")
501 ;; Change the syntax of a quoted newline so that it does not end a comment.
502 ("\\\\\n" 0 ".")))
504 (defvar makefile-imenu-generic-expression
505 `(("Dependencies" makefile-previous-dependency 1)
506 ("Macro Assignment" ,makefile-macroassign-regex 1))
507 "Imenu generic expression for Makefile mode. See `imenu-generic-expression'.")
509 ;;; ------------------------------------------------------------
510 ;;; The following configurable variables are used in the
511 ;;; up-to-date overview .
512 ;;; The standard configuration assumes that your `make' program
513 ;;; can be run in question/query mode using the `-q' option, this
514 ;;; means that the command
516 ;;; make -q foo
518 ;;; should return an exit status of zero if the target `foo' is
519 ;;; up to date and a nonzero exit status otherwise.
520 ;;; Many makes can do this although the docs/manpages do not mention
521 ;;; it. Try it with your favourite one. GNU make, System V make, and
522 ;;; Dennis Vadura's DMake have no problems.
523 ;;; Set the variable `makefile-brave-make' to the name of the
524 ;;; make utility that does this on your system.
525 ;;; To understand what this is all about see the function definition
526 ;;; of `makefile-query-by-make-minus-q' .
527 ;;; ------------------------------------------------------------
529 (defcustom makefile-brave-make "make"
530 "*How to invoke make, for `makefile-query-targets'.
531 This should identify a `make' command that can handle the `-q' option."
532 :type 'string
533 :group 'makefile)
535 (defcustom makefile-query-one-target-method 'makefile-query-by-make-minus-q
536 "*Function to call to determine whether a make target is up to date.
537 The function must satisfy this calling convention:
539 * As its first argument, it must accept the name of the target to
540 be checked, as a string.
542 * As its second argument, it may accept the name of a makefile
543 as a string. Depending on what you're going to do you may
544 not need this.
546 * It must return the integer value 0 (zero) if the given target
547 should be considered up-to-date in the context of the given
548 makefile, any nonzero integer value otherwise."
549 :type 'function
550 :group 'makefile)
552 (defcustom makefile-up-to-date-buffer-name "*Makefile Up-to-date overview*"
553 "*Name of the Up-to-date overview buffer."
554 :type 'string
555 :group 'makefile)
557 ;;; --- end of up-to-date-overview configuration ------------------
559 (defvar makefile-mode-abbrev-table nil
560 "Abbrev table in use in Makefile buffers.")
561 (if makefile-mode-abbrev-table
563 (define-abbrev-table 'makefile-mode-abbrev-table ()))
565 (defvar makefile-mode-map
566 (let ((map (make-sparse-keymap)))
567 ;; set up the keymap
568 (define-key map "\C-c:" 'makefile-insert-target-ref)
569 (if makefile-electric-keys
570 (progn
571 (define-key map "$" 'makefile-insert-macro-ref)
572 (define-key map ":" 'makefile-electric-colon)
573 (define-key map "=" 'makefile-electric-equal)
574 (define-key map "." 'makefile-electric-dot)))
575 (define-key map "\C-c\C-f" 'makefile-pickup-filenames-as-targets)
576 (define-key map "\C-c\C-b" 'makefile-switch-to-browser)
577 (define-key map "\C-c\C-c" 'comment-region)
578 (define-key map "\C-c\C-p" 'makefile-pickup-everything)
579 (define-key map "\C-c\C-u" 'makefile-create-up-to-date-overview)
580 (define-key map "\C-c\C-i" 'makefile-insert-gmake-function)
581 (define-key map "\C-c\C-\\" 'makefile-backslash-region)
582 (define-key map "\C-c\C-m\C-a" 'makefile-automake-mode)
583 (define-key map "\C-c\C-m\C-b" 'makefile-bsdmake-mode)
584 (define-key map "\C-c\C-m\C-g" 'makefile-gmake-mode)
585 (define-key map "\C-c\C-m\C-m" 'makefile-mode)
586 (define-key map "\C-c\C-m\C-p" 'makefile-makepp-mode)
587 (define-key map "\M-p" 'makefile-previous-dependency)
588 (define-key map "\M-n" 'makefile-next-dependency)
589 (define-key map "\e\t" 'makefile-complete)
591 ;; Make menus.
592 (define-key map [menu-bar makefile-mode]
593 (cons "Makefile" (make-sparse-keymap "Makefile")))
595 (define-key map [menu-bar makefile-mode browse]
596 '("Pop up Makefile Browser" . makefile-switch-to-browser))
597 (define-key map [menu-bar makefile-mode complete]
598 '("Complete Target or Macro" . makefile-complete))
599 (define-key map [menu-bar makefile-mode pickup]
600 '("Find Targets and Macros" . makefile-pickup-everything))
602 (define-key map [menu-bar makefile-mode prev]
603 '("Move to Previous Dependency" . makefile-previous-dependency))
604 (define-key map [menu-bar makefile-mode next]
605 '("Move to Next Dependency" . makefile-next-dependency))
606 map)
607 "The keymap that is used in Makefile mode.")
609 (defvar makefile-browser-map nil
610 "The keymap that is used in the macro- and target browser.")
611 (if makefile-browser-map
613 (setq makefile-browser-map (make-sparse-keymap))
614 (define-key makefile-browser-map "n" 'makefile-browser-next-line)
615 (define-key makefile-browser-map "\C-n" 'makefile-browser-next-line)
616 (define-key makefile-browser-map "p" 'makefile-browser-previous-line)
617 (define-key makefile-browser-map "\C-p" 'makefile-browser-previous-line)
618 (define-key makefile-browser-map " " 'makefile-browser-toggle)
619 (define-key makefile-browser-map "i" 'makefile-browser-insert-selection)
620 (define-key makefile-browser-map "I" 'makefile-browser-insert-selection-and-quit)
621 (define-key makefile-browser-map "\C-c\C-m" 'makefile-browser-insert-continuation)
622 (define-key makefile-browser-map "q" 'makefile-browser-quit)
623 ;; disable horizontal movement
624 (define-key makefile-browser-map "\C-b" 'undefined)
625 (define-key makefile-browser-map "\C-f" 'undefined))
628 (defvar makefile-mode-syntax-table nil)
629 (if makefile-mode-syntax-table
631 (setq makefile-mode-syntax-table (make-syntax-table))
632 (modify-syntax-entry ?\( "() " makefile-mode-syntax-table)
633 (modify-syntax-entry ?\) ")( " makefile-mode-syntax-table)
634 (modify-syntax-entry ?\[ "(] " makefile-mode-syntax-table)
635 (modify-syntax-entry ?\] ")[ " makefile-mode-syntax-table)
636 (modify-syntax-entry ?\{ "(} " makefile-mode-syntax-table)
637 (modify-syntax-entry ?\} "){ " makefile-mode-syntax-table)
638 (modify-syntax-entry ?\' "\" " makefile-mode-syntax-table)
639 (modify-syntax-entry ?\` "\" " makefile-mode-syntax-table)
640 (modify-syntax-entry ?# "< " makefile-mode-syntax-table)
641 (modify-syntax-entry ?\n "> " makefile-mode-syntax-table))
644 ;;; ------------------------------------------------------------
645 ;;; Internal variables.
646 ;;; You don't need to configure below this line.
647 ;;; ------------------------------------------------------------
649 (defvar makefile-target-table nil
650 "Table of all target names known for this buffer.")
652 (defvar makefile-macro-table nil
653 "Table of all macro names known for this buffer.")
655 (defvar makefile-browser-client
656 "A buffer in Makefile mode that is currently using the browser.")
658 (defvar makefile-browser-selection-vector nil)
659 (defvar makefile-has-prereqs nil)
660 (defvar makefile-need-target-pickup t)
661 (defvar makefile-need-macro-pickup t)
663 (defvar makefile-mode-hook '())
665 ;; Each element looks like '("GNU MAKE FUNCTION" "ARG" "ARG" ... )
666 ;; Each "ARG" is used as a prompt for a required argument.
667 (defconst makefile-gnumake-functions-alist
669 ;; Text functions
670 ("subst" "From" "To" "In")
671 ("patsubst" "Pattern" "Replacement" "In")
672 ("strip" "Text")
673 ("findstring" "Find what" "In")
674 ("filter" "Pattern" "Text")
675 ("filter-out" "Pattern" "Text")
676 ("sort" "List")
677 ;; Filename functions
678 ("dir" "Names")
679 ("notdir" "Names")
680 ("suffix" "Names")
681 ("basename" "Names")
682 ("addprefix" "Prefix" "Names")
683 ("addsuffix" "Suffix" "Names")
684 ("join" "List 1" "List 2")
685 ("word" "Index" "Text")
686 ("words" "Text")
687 ("firstword" "Text")
688 ("wildcard" "Pattern")
689 ;; Misc functions
690 ("foreach" "Variable" "List" "Text")
691 ("origin" "Variable")
692 ("shell" "Command")))
695 ;;; ------------------------------------------------------------
696 ;;; The mode function itself.
697 ;;; ------------------------------------------------------------
699 ;;;###autoload
700 (defun makefile-mode ()
701 "Major mode for editing standard Makefiles.
703 If you are editing a file for a different make, try one of the
704 variants `makefile-automake-mode', `makefile-gmake-mode',
705 `makefile-makepp-mode' or `makefile-bsdmake-mode'. All but the
706 last should be correctly chosen based on the file name, except if
707 it is *.mk. This function ends by invoking the function(s)
708 `makefile-mode-hook'.
710 It is strongly recommended to use `font-lock-mode', because that
711 provides additional parsing information. This is used for
712 example to see that a rule action `echo foo: bar' is a not rule
713 dependency, despite the colon.
715 \\{makefile-mode-map}
717 In the browser, use the following keys:
719 \\{makefile-browser-map}
721 Makefile mode can be configured by modifying the following variables:
723 `makefile-browser-buffer-name':
724 Name of the macro- and target browser buffer.
726 `makefile-target-colon':
727 The string that gets appended to all target names
728 inserted by `makefile-insert-target'.
729 \":\" or \"::\" are quite common values.
731 `makefile-macro-assign':
732 The string that gets appended to all macro names
733 inserted by `makefile-insert-macro'.
734 The normal value should be \" = \", since this is what
735 standard make expects. However, newer makes such as dmake
736 allow a larger variety of different macro assignments, so you
737 might prefer to use \" += \" or \" := \" .
739 `makefile-tab-after-target-colon':
740 If you want a TAB (instead of a space) to be appended after the
741 target colon, then set this to a non-nil value.
743 `makefile-browser-leftmost-column':
744 Number of blanks to the left of the browser selection mark.
746 `makefile-browser-cursor-column':
747 Column in which the cursor is positioned when it moves
748 up or down in the browser.
750 `makefile-browser-selected-mark':
751 String used to mark selected entries in the browser.
753 `makefile-browser-unselected-mark':
754 String used to mark unselected entries in the browser.
756 `makefile-browser-auto-advance-after-selection-p':
757 If this variable is set to a non-nil value the cursor
758 will automagically advance to the next line after an item
759 has been selected in the browser.
761 `makefile-pickup-everything-picks-up-filenames-p':
762 If this variable is set to a non-nil value then
763 `makefile-pickup-everything' also picks up filenames as targets
764 (i.e. it calls `makefile-pickup-filenames-as-targets'), otherwise
765 filenames are omitted.
767 `makefile-cleanup-continuations':
768 If this variable is set to a non-nil value then Makefile mode
769 will assure that no line in the file ends with a backslash
770 (the continuation character) followed by any whitespace.
771 This is done by silently removing the trailing whitespace, leaving
772 the backslash itself intact.
773 IMPORTANT: Please note that enabling this option causes Makefile mode
774 to MODIFY A FILE WITHOUT YOUR CONFIRMATION when \"it seems necessary\".
776 `makefile-browser-hook':
777 A function or list of functions to be called just before the
778 browser is entered. This is executed in the makefile buffer.
780 `makefile-special-targets-list':
781 List of special targets. You will be offered to complete
782 on one of those in the minibuffer whenever you enter a `.'.
783 at the beginning of a line in Makefile mode."
785 (interactive)
786 (kill-all-local-variables)
787 (add-hook 'write-file-functions
788 'makefile-warn-suspicious-lines nil t)
789 (add-hook 'write-file-functions
790 'makefile-warn-continuations nil t)
791 (add-hook 'write-file-functions
792 'makefile-cleanup-continuations nil t)
793 (make-local-variable 'makefile-target-table)
794 (make-local-variable 'makefile-macro-table)
795 (make-local-variable 'makefile-has-prereqs)
796 (make-local-variable 'makefile-need-target-pickup)
797 (make-local-variable 'makefile-need-macro-pickup)
799 ;; Font lock.
800 (make-local-variable 'font-lock-defaults)
801 (setq font-lock-defaults
802 ;; SYNTAX-BEGIN set to backward-paragraph to avoid slow-down
803 ;; near the end of a large buffer, due to parse-partial-sexp's
804 ;; trying to parse all the way till the beginning of buffer.
805 '(makefile-font-lock-keywords
806 nil nil
807 ((?$ . "."))
808 backward-paragraph
809 (font-lock-syntactic-keywords . makefile-font-lock-syntactic-keywords)
810 (font-lock-support-mode))) ; JIT breaks on long series of continuation lines.
812 ;; Add-log.
813 (make-local-variable 'add-log-current-defun-function)
814 (setq add-log-current-defun-function 'makefile-add-log-defun)
816 ;; Imenu.
817 (make-local-variable 'imenu-generic-expression)
818 (setq imenu-generic-expression makefile-imenu-generic-expression)
820 ;; Dabbrev.
821 (make-local-variable 'dabbrev-abbrev-skip-leading-regexp)
822 (setq dabbrev-abbrev-skip-leading-regexp "\\$")
824 ;; Other abbrevs.
825 (setq local-abbrev-table makefile-mode-abbrev-table)
827 ;; Filling.
828 (make-local-variable 'fill-paragraph-function)
829 (setq fill-paragraph-function 'makefile-fill-paragraph)
831 ;; Comment stuff.
832 (make-local-variable 'comment-start)
833 (setq comment-start "#")
834 (make-local-variable 'comment-end)
835 (setq comment-end "")
836 (make-local-variable 'comment-start-skip)
837 (setq comment-start-skip "#+[ \t]*")
839 ;; Make sure TAB really inserts \t.
840 (set (make-local-variable 'indent-line-function) 'indent-to-left-margin)
842 ;; become the current major mode
843 (setq major-mode 'makefile-mode)
844 (setq mode-name "Makefile")
846 ;; Activate keymap and syntax table.
847 (use-local-map makefile-mode-map)
848 (set-syntax-table makefile-mode-syntax-table)
850 ;; Real TABs are important in makefiles
851 (setq indent-tabs-mode t)
852 (run-mode-hooks 'makefile-mode-hook))
854 ;; These should do more than just differentiate font-lock.
855 ;;;###autoload
856 (define-derived-mode makefile-automake-mode makefile-mode "Makefile.am"
857 "An adapted `makefile-mode' that knows about automake."
858 (setq font-lock-defaults
859 `(makefile-automake-font-lock-keywords ,@(cdr font-lock-defaults))))
861 ;;;###autoload
862 (define-derived-mode makefile-gmake-mode makefile-mode "GNUmakefile"
863 "An adapted `makefile-mode' that knows about gmake."
864 (setq font-lock-defaults
865 `(makefile-gmake-font-lock-keywords ,@(cdr font-lock-defaults))))
867 ;;;###autoload
868 (define-derived-mode makefile-makepp-mode makefile-mode "Makeppfile"
869 "An adapted `makefile-mode' that knows about makepp."
870 (set (make-local-variable 'makefile-rule-action-regex)
871 makefile-makepp-rule-action-regex)
872 (setq font-lock-defaults
873 `(makefile-makepp-font-lock-keywords ,@(cdr font-lock-defaults))
874 imenu-generic-expression
875 `(("Functions" "^[ \t]*\\(?:make\\)?sub[ \t]+\\([A-Za-z0-9_]+\\)" 1)
876 ,@imenu-generic-expression)))
878 ;;;###autoload
879 (define-derived-mode makefile-bsdmake-mode makefile-mode "BSDmakefile"
880 "An adapted `makefile-mode' that knows about BSD make."
881 (set (make-local-variable 'makefile-dependency-regex)
882 makefile-bsdmake-dependency-regex)
883 (set (make-local-variable 'makefile-dependency-skip) "^:!")
884 (set (make-local-variable 'makefile-rule-action-regex)
885 makefile-bsdmake-rule-action-regex)
886 (setq font-lock-defaults
887 `(makefile-bsdmake-font-lock-keywords ,@(cdr font-lock-defaults))))
891 ;;; Motion code.
893 (defun makefile-next-dependency ()
894 "Move point to the beginning of the next dependency line."
895 (interactive)
896 (let ((here (point)))
897 (end-of-line)
898 (if (makefile-match-dependency nil)
899 (progn (beginning-of-line) t) ; indicate success
900 (goto-char here) nil)))
902 (defun makefile-previous-dependency ()
903 "Move point to the beginning of the previous dependency line."
904 (interactive)
905 (let ((pt (point)))
906 (beginning-of-line)
907 ;; makefile-match-dependency done backwards:
908 (catch 'found
909 (while (progn (skip-chars-backward makefile-dependency-skip)
910 (not (bobp)))
911 (or (prog1 (eq (char-after) ?=)
912 (backward-char))
913 (get-text-property (point) 'face)
914 (beginning-of-line)
915 (if (looking-at makefile-dependency-regex)
916 (throw 'found t))))
917 (goto-char pt)
918 nil)))
922 ;;; Electric keys. Blech.
924 (defun makefile-electric-dot (arg)
925 "Prompt for the name of a special target to insert.
926 Only does electric insertion at beginning of line.
927 Anywhere else just self-inserts."
928 (interactive "p")
929 (if (bolp)
930 (makefile-insert-special-target)
931 (self-insert-command arg)))
933 (defun makefile-insert-special-target ()
934 "Prompt for and insert a special target name.
935 Uses `makefile-special-targets' list."
936 (interactive)
937 (makefile-pickup-targets)
938 (let ((special-target
939 (completing-read "Special target: "
940 makefile-special-targets-list nil nil nil)))
941 (if (zerop (length special-target))
943 (insert "." special-target ":")
944 (makefile-forward-after-target-colon))))
946 (defun makefile-electric-equal (arg)
947 "Prompt for name of a macro to insert.
948 Only does prompting if point is at beginning of line.
949 Anywhere else just self-inserts."
950 (interactive "p")
951 (makefile-pickup-macros)
952 (if (bolp)
953 (call-interactively 'makefile-insert-macro)
954 (self-insert-command arg)))
956 (defun makefile-insert-macro (macro-name)
957 "Prepare definition of a new macro."
958 (interactive "sMacro Name: ")
959 (makefile-pickup-macros)
960 (if (not (zerop (length macro-name)))
961 (progn
962 (beginning-of-line)
963 (insert macro-name makefile-macro-assign)
964 (setq makefile-need-macro-pickup t)
965 (makefile-remember-macro macro-name))))
967 (defun makefile-insert-macro-ref (macro-name)
968 "Complete on a list of known macros, then insert complete ref at point."
969 (interactive
970 (list
971 (progn
972 (makefile-pickup-macros)
973 (completing-read "Refer to macro: " makefile-macro-table nil nil nil))))
974 (makefile-do-macro-insertion macro-name))
976 (defun makefile-insert-target (target-name)
977 "Prepare definition of a new target (dependency line)."
978 (interactive "sTarget: ")
979 (if (not (zerop (length target-name)))
980 (progn
981 (beginning-of-line)
982 (insert target-name makefile-target-colon)
983 (makefile-forward-after-target-colon)
984 (end-of-line)
985 (setq makefile-need-target-pickup t)
986 (makefile-remember-target target-name))))
988 (defun makefile-insert-target-ref (target-name)
989 "Complete on a list of known targets, then insert TARGET-NAME at point."
990 (interactive
991 (list
992 (progn
993 (makefile-pickup-targets)
994 (completing-read "Refer to target: " makefile-target-table nil nil nil))))
995 (if (not (zerop (length target-name)))
996 (insert target-name " ")))
998 (defun makefile-electric-colon (arg)
999 "Prompt for name of new target.
1000 Prompting only happens at beginning of line.
1001 Anywhere else just self-inserts."
1002 (interactive "p")
1003 (if (bolp)
1004 (call-interactively 'makefile-insert-target)
1005 (self-insert-command arg)))
1009 ;;; ------------------------------------------------------------
1010 ;;; Extracting targets and macros from an existing makefile
1011 ;;; ------------------------------------------------------------
1013 (defun makefile-pickup-targets ()
1014 "Notice names of all target definitions in Makefile."
1015 (interactive)
1016 (when makefile-need-target-pickup
1017 (setq makefile-need-target-pickup nil
1018 makefile-target-table nil
1019 makefile-has-prereqs nil)
1020 (save-excursion
1021 (goto-char (point-min))
1022 (while (makefile-match-dependency nil)
1023 (goto-char (match-beginning 1))
1024 (while (let ((target-name
1025 (buffer-substring-no-properties (point)
1026 (progn
1027 (skip-chars-forward "^ \t:#")
1028 (point))))
1029 (has-prereqs
1030 (not (looking-at ":[ \t]*$"))))
1031 (if (makefile-remember-target target-name has-prereqs)
1032 (message "Picked up target \"%s\" from line %d"
1033 target-name (line-number-at-pos)))
1034 (skip-chars-forward " \t")
1035 (not (or (eolp) (eq (char-after) ?:)))))
1036 (forward-line)))
1037 (message "Read targets OK.")))
1039 (defun makefile-pickup-macros ()
1040 "Notice names of all macro definitions in Makefile."
1041 (interactive)
1042 (when makefile-need-macro-pickup
1043 (setq makefile-need-macro-pickup nil
1044 makefile-macro-table nil)
1045 (save-excursion
1046 (goto-char (point-min))
1047 (while (re-search-forward makefile-macroassign-regex nil t)
1048 (goto-char (match-beginning 1))
1049 (let ((macro-name (buffer-substring-no-properties (point)
1050 (progn
1051 (skip-chars-forward "^ \t:#=*")
1052 (point)))))
1053 (if (makefile-remember-macro macro-name)
1054 (message "Picked up macro \"%s\" from line %d"
1055 macro-name (line-number-at-pos))))
1056 (forward-line)))
1057 (message "Read macros OK.")))
1059 (defun makefile-pickup-everything (arg)
1060 "Notice names of all macros and targets in Makefile.
1061 Prefix arg means force pickups to be redone."
1062 (interactive "P")
1063 (if arg
1064 (setq makefile-need-target-pickup t
1065 makefile-need-macro-pickup t))
1066 (makefile-pickup-macros)
1067 (makefile-pickup-targets)
1068 (if makefile-pickup-everything-picks-up-filenames-p
1069 (makefile-pickup-filenames-as-targets)))
1071 (defun makefile-pickup-filenames-as-targets ()
1072 "Scan the current directory for filenames to use as targets.
1073 Checks each filename against `makefile-ignored-files-in-pickup-regex'
1074 and adds all qualifying names to the list of known targets."
1075 (interactive)
1076 (mapc (lambda (name)
1077 (or (file-directory-p name)
1078 (string-match makefile-ignored-files-in-pickup-regex name)
1079 (if (makefile-remember-target name)
1080 (message "Picked up file \"%s\" as target" name))))
1081 (file-name-all-completions "" (or (file-name-directory (buffer-file-name)) ""))))
1085 ;;; Completion.
1087 (defun makefile-complete ()
1088 "Perform completion on Makefile construct preceding point.
1089 Can complete variable and target names.
1090 The context determines which are considered."
1091 (interactive)
1092 (let* ((beg (save-excursion
1093 (skip-chars-backward "^$(){}:#= \t\n")
1094 (point)))
1095 (try (buffer-substring beg (point)))
1096 (do-macros nil)
1097 (paren nil))
1099 (save-excursion
1100 (goto-char beg)
1101 (let ((pc (preceding-char)))
1102 (cond
1103 ;; Beginning of line means anything.
1104 ((bolp)
1107 ;; Preceding "$" means macros only.
1108 ((= pc ?$)
1109 (setq do-macros t))
1111 ;; Preceding "$(" or "${" means macros only.
1112 ((and (or (= pc ?{)
1113 (= pc ?\())
1114 (progn
1115 (setq paren pc)
1116 (backward-char)
1117 (and (not (bolp))
1118 (= (preceding-char) ?$))))
1119 (setq do-macros t)))))
1121 ;; Try completion.
1122 (let* ((table (append (if do-macros
1124 makefile-target-table)
1125 makefile-macro-table))
1126 (completion (try-completion try table)))
1127 (cond
1128 ;; Exact match, so insert closing paren or colon.
1129 ((eq completion t)
1130 (insert (if do-macros
1131 (if (eq paren ?{)
1133 ?\))
1134 (if (save-excursion
1135 (goto-char beg)
1136 (bolp))
1138 " "))))
1140 ;; No match.
1141 ((null completion)
1142 (message "Can't find completion for \"%s\"" try)
1143 (ding))
1145 ;; Partial completion.
1146 ((not (string= try completion))
1147 ;; FIXME it would be nice to supply the closing paren if an
1148 ;; exact, unambiguous match were found. That is not possible
1149 ;; right now. Ditto closing ":" for targets.
1150 (delete-region beg (point))
1152 ;; DO-MACROS means doing macros only. If not that, then check
1153 ;; to see if this completion is a macro. Special insertion
1154 ;; must be done for macros.
1155 (if (or do-macros
1156 (assoc completion makefile-macro-table))
1157 (let ((makefile-use-curly-braces-for-macros-p
1158 (or (eq paren ?{)
1159 makefile-use-curly-braces-for-macros-p)))
1160 (delete-backward-char 2)
1161 (makefile-do-macro-insertion completion)
1162 (delete-backward-char 1))
1164 ;; Just insert targets.
1165 (insert completion)))
1167 ;; Can't complete any more, so make completion list. FIXME
1168 ;; this doesn't do the right thing when the completion is
1169 ;; actually inserted. I don't think there is an easy way to do
1170 ;; that.
1172 (message "Making completion list...")
1173 (let ((list (all-completions try table)))
1174 (with-output-to-temp-buffer "*Completions*"
1175 (display-completion-list list)))
1176 (message "Making completion list...done"))))))
1180 ;; Backslashification. Stolen from cc-mode.el.
1182 (defun makefile-backslash-region (from to delete-flag)
1183 "Insert, align, or delete end-of-line backslashes on the lines in the region.
1184 With no argument, inserts backslashes and aligns existing backslashes.
1185 With an argument, deletes the backslashes.
1187 This function does not modify the last line of the region if the region ends
1188 right at the start of the following line; it does not modify blank lines
1189 at the start of the region. So you can put the region around an entire macro
1190 definition and conveniently use this command."
1191 (interactive "r\nP")
1192 (save-excursion
1193 (goto-char from)
1194 (let ((column makefile-backslash-column)
1195 (endmark (make-marker)))
1196 (move-marker endmark to)
1197 ;; Compute the smallest column number past the ends of all the lines.
1198 (if makefile-backslash-align
1199 (progn
1200 (if (not delete-flag)
1201 (while (< (point) to)
1202 (end-of-line)
1203 (if (= (preceding-char) ?\\)
1204 (progn (forward-char -1)
1205 (skip-chars-backward " \t")))
1206 (setq column (max column (1+ (current-column))))
1207 (forward-line 1)))
1208 ;; Adjust upward to a tab column, if that doesn't push
1209 ;; past the margin.
1210 (if (> (% column tab-width) 0)
1211 (let ((adjusted (* (/ (+ column tab-width -1) tab-width)
1212 tab-width)))
1213 (if (< adjusted (window-width))
1214 (setq column adjusted))))))
1215 ;; Don't modify blank lines at start of region.
1216 (goto-char from)
1217 (while (and (< (point) endmark) (eolp))
1218 (forward-line 1))
1219 ;; Add or remove backslashes on all the lines.
1220 (while (and (< (point) endmark)
1221 ;; Don't backslashify the last line
1222 ;; if the region ends right at the start of the next line.
1223 (save-excursion
1224 (forward-line 1)
1225 (< (point) endmark)))
1226 (if (not delete-flag)
1227 (makefile-append-backslash column)
1228 (makefile-delete-backslash))
1229 (forward-line 1))
1230 (move-marker endmark nil))))
1232 (defun makefile-append-backslash (column)
1233 (end-of-line)
1234 ;; Note that "\\\\" is needed to get one backslash.
1235 (if (= (preceding-char) ?\\)
1236 (progn (forward-char -1)
1237 (delete-horizontal-space)
1238 (indent-to column (if makefile-backslash-align nil 1)))
1239 (indent-to column (if makefile-backslash-align nil 1))
1240 (insert "\\")))
1242 (defun makefile-delete-backslash ()
1243 (end-of-line)
1244 (or (bolp)
1245 (progn
1246 (forward-char -1)
1247 (if (looking-at "\\\\")
1248 (delete-region (1+ (point))
1249 (progn (skip-chars-backward " \t") (point)))))))
1253 ;; Filling
1255 (defun makefile-fill-paragraph (arg)
1256 ;; Fill comments, backslashed lines, and variable definitions
1257 ;; specially.
1258 (save-excursion
1259 (beginning-of-line)
1260 (cond
1261 ((looking-at "^#+")
1262 ;; Found a comment. Set the fill prefix, and find the paragraph
1263 ;; boundaries by searching for lines that look like comment-only
1264 ;; lines.
1265 (let ((fill-prefix (match-string-no-properties 0))
1266 (fill-paragraph-function nil))
1267 (save-excursion
1268 (save-restriction
1269 (narrow-to-region
1270 ;; Search backwards.
1271 (save-excursion
1272 (while (and (zerop (forward-line -1))
1273 (looking-at "^#")))
1274 ;; We may have gone too far. Go forward again.
1275 (or (looking-at "^#")
1276 (forward-line 1))
1277 (point))
1278 ;; Search forwards.
1279 (save-excursion
1280 (while (looking-at "^#")
1281 (forward-line))
1282 (point)))
1283 (fill-paragraph nil)
1284 t))))
1286 ;; Must look for backslashed-region before looking for variable
1287 ;; assignment.
1288 ((or (eq (char-before (line-end-position 1)) ?\\)
1289 (eq (char-before (line-end-position 0)) ?\\))
1290 ;; A backslash region. Find beginning and end, remove
1291 ;; backslashes, fill, and then reapply backslahes.
1292 (end-of-line)
1293 (let ((beginning
1294 (save-excursion
1295 (end-of-line 0)
1296 (while (= (preceding-char) ?\\)
1297 (end-of-line 0))
1298 (forward-char)
1299 (point)))
1300 (end
1301 (save-excursion
1302 (while (= (preceding-char) ?\\)
1303 (end-of-line 2))
1304 (point))))
1305 (save-restriction
1306 (narrow-to-region beginning end)
1307 (makefile-backslash-region (point-min) (point-max) t)
1308 (let ((fill-paragraph-function nil))
1309 (fill-paragraph nil))
1310 (makefile-backslash-region (point-min) (point-max) nil)
1311 (goto-char (point-max))
1312 (if (< (skip-chars-backward "\n") 0)
1313 (delete-region (point) (point-max))))))
1315 ((looking-at makefile-macroassign-regex)
1316 ;; Have a macro assign. Fill just this line, and then backslash
1317 ;; resulting region.
1318 (save-restriction
1319 (narrow-to-region (point) (line-beginning-position 2))
1320 (let ((fill-paragraph-function nil))
1321 (fill-paragraph nil))
1322 (makefile-backslash-region (point-min) (point-max) nil)))))
1324 ;; Always return non-nil so we don't fill anything else.
1329 ;;; ------------------------------------------------------------
1330 ;;; Browser mode.
1331 ;;; ------------------------------------------------------------
1333 (defun makefile-browser-format-target-line (target selected)
1334 (format
1335 (concat (make-string makefile-browser-leftmost-column ?\ )
1336 (if selected
1337 makefile-browser-selected-mark
1338 makefile-browser-unselected-mark)
1339 "%s%s")
1340 target makefile-target-colon))
1342 (defun makefile-browser-format-macro-line (macro selected)
1343 (format
1344 (concat (make-string makefile-browser-leftmost-column ?\ )
1345 (if selected
1346 makefile-browser-selected-mark
1347 makefile-browser-unselected-mark)
1348 (makefile-format-macro-ref macro))))
1350 (defun makefile-browser-fill (targets macros)
1351 (let ((inhibit-read-only t))
1352 (goto-char (point-min))
1353 (erase-buffer)
1354 (mapconcat
1355 (function
1356 (lambda (item) (insert (makefile-browser-format-target-line (car item) nil) "\n")))
1357 targets
1359 (mapconcat
1360 (function
1361 (lambda (item) (insert (makefile-browser-format-macro-line (car item) nil) "\n")))
1362 macros
1364 (sort-lines nil (point-min) (point-max))
1365 (goto-char (1- (point-max)))
1366 (delete-char 1) ; remove unnecessary newline at eob
1367 (goto-char (point-min))
1368 (forward-char makefile-browser-cursor-column)))
1371 ;;; Moving up and down in the browser
1374 (defun makefile-browser-next-line ()
1375 "Move the browser selection cursor to the next line."
1376 (interactive)
1377 (if (not (makefile-last-line-p))
1378 (progn
1379 (forward-line 1)
1380 (forward-char makefile-browser-cursor-column))))
1382 (defun makefile-browser-previous-line ()
1383 "Move the browser selection cursor to the previous line."
1384 (interactive)
1385 (if (not (makefile-first-line-p))
1386 (progn
1387 (forward-line -1)
1388 (forward-char makefile-browser-cursor-column))))
1391 ;;; Quitting the browser (returns to client buffer)
1394 (defun makefile-browser-quit ()
1395 "Leave the browser and return to the makefile buffer."
1396 (interactive)
1397 (let ((my-client makefile-browser-client))
1398 (setq makefile-browser-client nil) ; we quitted, so NO client!
1399 (set-buffer-modified-p nil)
1400 (quit-window t)
1401 (pop-to-buffer my-client)))
1404 ;;; Toggle state of a browser item
1407 (defun makefile-browser-toggle ()
1408 "Toggle the selection state of the browser item at the cursor position."
1409 (interactive)
1410 (let ((this-line (count-lines (point-min) (point))))
1411 (setq this-line (max 1 this-line))
1412 (makefile-browser-toggle-state-for-line this-line)
1413 (goto-line this-line)
1414 (let ((inhibit-read-only t))
1415 (beginning-of-line)
1416 (if (makefile-browser-on-macro-line-p)
1417 (let ((macro-name (makefile-browser-this-line-macro-name)))
1418 (delete-region (point) (progn (end-of-line) (point)))
1419 (insert
1420 (makefile-browser-format-macro-line
1421 macro-name
1422 (makefile-browser-get-state-for-line this-line))))
1423 (let ((target-name (makefile-browser-this-line-target-name)))
1424 (delete-region (point) (progn (end-of-line) (point)))
1425 (insert
1426 (makefile-browser-format-target-line
1427 target-name
1428 (makefile-browser-get-state-for-line this-line))))))
1429 (beginning-of-line)
1430 (forward-char makefile-browser-cursor-column)
1431 (if makefile-browser-auto-advance-after-selection-p
1432 (makefile-browser-next-line))))
1435 ;;; Making insertions into the client buffer
1438 (defun makefile-browser-insert-continuation ()
1439 "Insert a makefile continuation.
1440 In the makefile buffer, go to (end-of-line), insert a \'\\\'
1441 character, insert a new blank line, go to that line and indent by one TAB.
1442 This is most useful in the process of creating continued lines when copying
1443 large dependencies from the browser to the client buffer.
1444 \(point) advances accordingly in the client buffer."
1445 (interactive)
1446 (with-current-buffer makefile-browser-client
1447 (end-of-line)
1448 (insert "\\\n\t")))
1450 (defun makefile-browser-insert-selection ()
1451 "Insert all selected targets and/or macros in the makefile buffer.
1452 Insertion takes place at point."
1453 (interactive)
1454 (save-excursion
1455 (goto-line 1)
1456 (let ((current-line 1))
1457 (while (not (eobp))
1458 (if (makefile-browser-get-state-for-line current-line)
1459 (makefile-browser-send-this-line-item))
1460 (forward-line 1)
1461 (setq current-line (1+ current-line))))))
1463 (defun makefile-browser-insert-selection-and-quit ()
1464 (interactive)
1465 (makefile-browser-insert-selection)
1466 (makefile-browser-quit))
1468 (defun makefile-browser-send-this-line-item ()
1469 (if (makefile-browser-on-macro-line-p)
1470 (save-excursion
1471 (let ((macro-name (makefile-browser-this-line-macro-name)))
1472 (set-buffer makefile-browser-client)
1473 (insert (makefile-format-macro-ref macro-name) " ")))
1474 (save-excursion
1475 (let ((target-name (makefile-browser-this-line-target-name)))
1476 (set-buffer makefile-browser-client)
1477 (insert target-name " ")))))
1479 (defun makefile-browser-start-interaction ()
1480 (use-local-map makefile-browser-map)
1481 (setq buffer-read-only t))
1483 (defun makefile-browse (targets macros)
1484 (interactive)
1485 (if (zerop (+ (length targets) (length macros)))
1486 (progn
1487 (beep)
1488 (message "No macros or targets to browse! Consider running 'makefile-pickup-everything\'"))
1489 (let ((browser-buffer (get-buffer-create makefile-browser-buffer-name)))
1490 (pop-to-buffer browser-buffer)
1491 (makefile-browser-fill targets macros)
1492 (shrink-window-if-larger-than-buffer)
1493 (set (make-local-variable 'makefile-browser-selection-vector)
1494 (make-vector (+ (length targets) (length macros)) nil))
1495 (makefile-browser-start-interaction))))
1497 (defun makefile-switch-to-browser ()
1498 (interactive)
1499 (run-hooks 'makefile-browser-hook)
1500 (setq makefile-browser-client (current-buffer))
1501 (makefile-pickup-targets)
1502 (makefile-pickup-macros)
1503 (makefile-browse makefile-target-table makefile-macro-table))
1507 ;;; ------------------------------------------------------------
1508 ;;; Up-to-date overview buffer
1509 ;;; ------------------------------------------------------------
1511 (defun makefile-create-up-to-date-overview ()
1512 "Create a buffer containing an overview of the state of all known targets.
1513 Known targets are targets that are explicitly defined in that makefile;
1514 in other words, all targets that appear on the left hand side of a
1515 dependency in the makefile."
1516 (interactive)
1517 (if (y-or-n-p "Are you sure that the makefile being edited is consistent? ")
1519 ;; The rest of this function operates on a temporary makefile, created by
1520 ;; writing the current contents of the makefile buffer.
1522 (let ((saved-target-table makefile-target-table)
1523 (this-buffer (current-buffer))
1524 (makefile-up-to-date-buffer
1525 (get-buffer-create makefile-up-to-date-buffer-name))
1526 (filename (makefile-save-temporary))
1528 ;; Forget the target table because it may contain picked-up filenames
1529 ;; that are not really targets in the current makefile.
1530 ;; We don't want to query these, so get a new target-table with just the
1531 ;; targets that can be found in the makefile buffer.
1532 ;; The 'old' target table will be restored later.
1534 (real-targets (progn
1535 (makefile-pickup-targets)
1536 makefile-target-table))
1537 (prereqs makefile-has-prereqs)
1540 (set-buffer makefile-up-to-date-buffer)
1541 (setq buffer-read-only nil)
1542 (erase-buffer)
1543 (makefile-query-targets filename real-targets prereqs)
1544 (if (zerop (buffer-size)) ; if it did not get us anything
1545 (progn
1546 (kill-buffer (current-buffer))
1547 (message "No overview created!")))
1548 (set-buffer this-buffer)
1549 (setq makefile-target-table saved-target-table)
1550 (if (get-buffer makefile-up-to-date-buffer-name)
1551 (progn
1552 (pop-to-buffer (get-buffer makefile-up-to-date-buffer-name))
1553 (shrink-window-if-larger-than-buffer)
1554 (sort-lines nil (point-min) (point-max))
1555 (setq buffer-read-only t))))))
1557 (defun makefile-save-temporary ()
1558 "Create a temporary file from the current makefile buffer."
1559 (let ((filename (makefile-generate-temporary-filename)))
1560 (write-region (point-min) (point-max) filename nil 0)
1561 filename)) ; return the filename
1563 (defun makefile-generate-temporary-filename ()
1564 "Create a filename suitable for use in `makefile-save-temporary'.
1565 Be careful to allow brain-dead file systems (DOS, SYSV ...) to cope
1566 with the generated name!"
1567 (let ((my-name (user-login-name))
1568 (my-uid (int-to-string (user-uid))))
1569 (concat "mktmp"
1570 (if (> (length my-name) 3)
1571 (substring my-name 0 3)
1572 my-name)
1574 (if (> (length my-uid) 3)
1575 (substring my-uid 0 3)
1576 my-uid))))
1578 (defun makefile-query-targets (filename target-table prereq-list)
1579 "Fill the up-to-date overview buffer.
1580 Checks each target in TARGET-TABLE using `makefile-query-one-target-method'
1581 and generates the overview, one line per target name."
1582 (insert
1583 (mapconcat
1584 (function (lambda (item)
1585 (let* ((target-name (car item))
1586 (no-prereqs (not (member target-name prereq-list)))
1587 (needs-rebuild (or no-prereqs
1588 (funcall
1589 makefile-query-one-target-method
1590 target-name
1591 filename))))
1592 (format "\t%s%s"
1593 target-name
1594 (cond (no-prereqs " .. has no prerequisites")
1595 (needs-rebuild " .. NEEDS REBUILD")
1596 (t " .. is up to date"))))
1598 target-table "\n"))
1599 (goto-char (point-min))
1600 (delete-file filename)) ; remove the tmpfile
1602 (defun makefile-query-by-make-minus-q (target &optional filename)
1603 (not (eq 0
1604 (call-process makefile-brave-make nil nil nil
1605 "-f" filename "-q" target))))
1609 ;;; ------------------------------------------------------------
1610 ;;; Continuation cleanup
1611 ;;; ------------------------------------------------------------
1613 (defun makefile-cleanup-continuations ()
1614 (if (eq major-mode 'makefile-mode)
1615 (if (and makefile-cleanup-continuations
1616 (not buffer-read-only))
1617 (save-excursion
1618 (goto-char (point-min))
1619 (while (re-search-forward "\\\\[ \t]+$" nil t)
1620 (replace-match "\\" t t))))))
1623 ;;; ------------------------------------------------------------
1624 ;;; Warn of suspicious lines
1625 ;;; ------------------------------------------------------------
1627 (defun makefile-warn-suspicious-lines ()
1628 ;; Returning non-nil cancels the save operation
1629 (if (eq major-mode 'makefile-mode)
1630 (save-excursion
1631 (goto-char (point-min))
1632 (if (re-search-forward "^\\(\t+$\\| +\t\\)" nil t)
1633 (not (y-or-n-p
1634 (format "Suspicious line %d. Save anyway? "
1635 (count-lines (point-min) (point)))))))))
1637 (defun makefile-warn-continuations ()
1638 (if (eq major-mode 'makefile-mode)
1639 (save-excursion
1640 (goto-char (point-min))
1641 (if (re-search-forward "\\\\[ \t]+$" nil t)
1642 (not (y-or-n-p
1643 (format "Suspicious continuation in line %d. Save anyway? "
1644 (count-lines (point-min) (point)))))))))
1647 ;;; ------------------------------------------------------------
1648 ;;; GNU make function support
1649 ;;; ------------------------------------------------------------
1651 (defun makefile-insert-gmake-function ()
1652 "Insert a GNU make function call.
1653 Asks for the name of the function to use (with completion).
1654 Then prompts for all required parameters."
1655 (interactive)
1656 (let* ((gm-function-name (completing-read
1657 "Function: "
1658 makefile-gnumake-functions-alist
1659 nil t nil))
1660 (gm-function-prompts
1661 (cdr (assoc gm-function-name makefile-gnumake-functions-alist))))
1662 (if (not (zerop (length gm-function-name)))
1663 (insert (makefile-format-macro-ref
1664 (concat gm-function-name " "
1665 (makefile-prompt-for-gmake-funargs
1666 gm-function-name gm-function-prompts)))
1667 " "))))
1669 (defun makefile-prompt-for-gmake-funargs (function-name prompt-list)
1670 (mapconcat
1671 (function (lambda (one-prompt)
1672 (read-string (format "[%s] %s: " function-name one-prompt)
1673 nil)))
1674 prompt-list
1675 ","))
1679 ;;; ------------------------------------------------------------
1680 ;;; Utility functions
1681 ;;; ------------------------------------------------------------
1683 (defun makefile-match-function-end (end)
1684 "To be called as an anchored matcher by font-lock.
1685 The anchor must have matched the opening parens in the first group."
1686 (let ((s (match-string-no-properties 1)))
1687 (setq s (cond ((string= s "(") "\\(.*?\\)[ \t]*)")
1688 ((string= s "{") "\\(.*?\\)[ \t]*}")
1689 ((string= s "((") "\\(.*?\\)[ \t]*))")
1690 ((string= s "{{") "\\(.*?\\)[ \t]*}}")))
1691 (if s (looking-at s))))
1693 (defun makefile-match-dependency (bound)
1694 "Search for `makefile-dependency-regex' up to BOUND.
1695 Checks that the colon has not already been fontified, else we
1696 matched in a rule action."
1697 (catch 'found
1698 (let ((pt (point)))
1699 (while (progn (skip-chars-forward makefile-dependency-skip bound)
1700 (< (point) (or bound (point-max))))
1701 (forward-char)
1702 (or (eq (char-after) ?=)
1703 (get-text-property (1- (point)) 'face)
1704 (when (save-excursion
1705 (beginning-of-line)
1706 (looking-at makefile-dependency-regex))
1707 (end-of-line)
1708 (throw 'found (point)))))
1709 (goto-char pt))
1710 nil))
1712 (defun makefile-match-action (bound)
1713 (catch 'found
1714 (while (re-search-forward makefile-rule-action-regex bound t)
1715 (or (eq ?\\ (char-after (- (match-beginning 0) 2)))
1716 (throw 'found t)))))
1718 (defun makefile-do-macro-insertion (macro-name)
1719 "Insert a macro reference."
1720 (if (not (zerop (length macro-name)))
1721 (if (assoc macro-name makefile-runtime-macros-list)
1722 (insert "$" macro-name)
1723 (insert (makefile-format-macro-ref macro-name)))))
1725 (defun makefile-remember-target (target-name &optional has-prereqs)
1726 "Remember a given target if it is not already remembered for this buffer."
1727 (if (not (zerop (length target-name)))
1728 (progn
1729 (if (not (assoc target-name makefile-target-table))
1730 (setq makefile-target-table
1731 (cons (list target-name) makefile-target-table)))
1732 (if has-prereqs
1733 (setq makefile-has-prereqs
1734 (cons target-name makefile-has-prereqs))))))
1736 (defun makefile-remember-macro (macro-name)
1737 "Remember a given macro if it is not already remembered for this buffer."
1738 (if (not (zerop (length macro-name)))
1739 (if (not (assoc macro-name makefile-macro-table))
1740 (setq makefile-macro-table
1741 (cons (list macro-name) makefile-macro-table)))))
1743 (defun makefile-forward-after-target-colon ()
1744 "Move point forward after inserting the terminating colon of a target.
1745 This acts according to the value of `makefile-tab-after-target-colon'."
1746 (if makefile-tab-after-target-colon
1747 (insert "\t")
1748 (insert " ")))
1750 (defun makefile-browser-on-macro-line-p ()
1751 "Determine if point is on a macro line in the browser."
1752 (save-excursion
1753 (beginning-of-line)
1754 (re-search-forward "\\$[{(]" (line-end-position) t)))
1756 (defun makefile-browser-this-line-target-name ()
1757 "Extract the target name from a line in the browser."
1758 (save-excursion
1759 (end-of-line)
1760 (skip-chars-backward "^ \t")
1761 (buffer-substring (point) (1- (line-end-position)))))
1763 (defun makefile-browser-this-line-macro-name ()
1764 "Extract the macro name from a line in the browser."
1765 (save-excursion
1766 (beginning-of-line)
1767 (re-search-forward "\\$[{(]" (line-end-position) t)
1768 (let ((macro-start (point)))
1769 (skip-chars-forward "^})")
1770 (buffer-substring macro-start (point)))))
1772 (defun makefile-format-macro-ref (macro-name)
1773 "Format a macro reference.
1774 Uses `makefile-use-curly-braces-for-macros-p'."
1775 (if (or (char-equal ?\( (string-to-char macro-name))
1776 (char-equal ?\{ (string-to-char macro-name)))
1777 (format "$%s" macro-name)
1778 (if makefile-use-curly-braces-for-macros-p
1779 (format "${%s}" macro-name)
1780 (format "$(%s)" macro-name))))
1782 (defun makefile-browser-get-state-for-line (n)
1783 (aref makefile-browser-selection-vector (1- n)))
1785 (defun makefile-browser-set-state-for-line (n to-state)
1786 (aset makefile-browser-selection-vector (1- n) to-state))
1788 (defun makefile-browser-toggle-state-for-line (n)
1789 (makefile-browser-set-state-for-line n (not (makefile-browser-get-state-for-line n))))
1791 (defun makefile-last-line-p ()
1792 (= (line-end-position) (point-max)))
1794 (defun makefile-first-line-p ()
1795 (= (line-beginning-position) (point-min)))
1799 ;;; Support for other packages, like add-log.
1801 (defun makefile-add-log-defun ()
1802 "Return name of target or variable assignment that point is in.
1803 If it isn't in one, return nil."
1804 (save-excursion
1805 (let (found)
1806 (beginning-of-line)
1807 ;; Scan back line by line, noticing when we come to a
1808 ;; variable or rule definition, and giving up when we see
1809 ;; a line that is not part of either of those.
1810 (while (not (or (setq found
1811 (when (or (looking-at makefile-macroassign-regex)
1812 (looking-at makefile-dependency-regex))
1813 (match-string-no-properties 1)))
1814 ;; Don't keep looking across a blank line or comment.
1815 (looking-at "$\\|#")
1816 (not (zerop (forward-line -1))))))
1817 found)))
1819 (provide 'make-mode)
1821 ;;; arch-tag: bd23545a-de91-44fb-b1b2-feafbb2635a0
1822 ;;; make-mode.el ends here