]> code.delx.au - gnu-emacs/blob - lisp/progmodes/make-mode.el
Merge from emacs--rel--22
[gnu-emacs] / lisp / progmodes / make-mode.el
1 ;;; make-mode.el --- makefile editing commands for Emacs
2
3 ;; Copyright (C) 1992, 1994, 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008
4 ;; Free Software Foundation, Inc.
5
6 ;; Author: Thomas Neumann <tom@smart.bo.open.de>
7 ;; Eric S. Raymond <esr@snark.thyrsus.com>
8 ;; Maintainer: FSF
9 ;; Adapted-By: ESR
10 ;; Keywords: unix, tools
11
12 ;; This file is part of GNU Emacs.
13
14 ;; GNU Emacs is free software; you can redistribute it and/or modify
15 ;; it under the terms of the GNU General Public License as published by
16 ;; the Free Software Foundation; either version 3, or (at your option)
17 ;; any later version.
18
19 ;; GNU Emacs is distributed in the hope that it will be useful,
20 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
21 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 ;; GNU General Public License for more details.
23
24 ;; You should have received a copy of the GNU General Public License
25 ;; along with GNU Emacs; see the file COPYING. If not, write to the
26 ;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
27 ;; Boston, MA 02110-1301, USA.
28
29 ;;; Commentary:
30
31 ;; A major mode for editing makefiles. The mode knows about Makefile
32 ;; syntax and defines M-n and M-p to move to next and previous productions.
33 ;;
34 ;; The keys $, =, : and . are electric; they try to help you fill in a
35 ;; macro reference, macro definition, ordinary target name, or special
36 ;; target name, respectively. Such names are completed using a list of
37 ;; targets and macro names parsed out of the makefile. This list is
38 ;; automatically updated, if necessary, whenever you invoke one of
39 ;; these commands. You can force it to be updated with C-c C-p.
40 ;;
41 ;; The command C-c C-f adds certain filenames in the current directory
42 ;; as targets. You can filter out filenames by setting the variable
43 ;; makefile-ignored-files-in-pickup-regex.
44 ;;
45 ;; The command C-c C-u grinds for a bit, then pops up a report buffer
46 ;; showing which target names are up-to-date with respect to their
47 ;; prerequisites, which targets are out-of-date, and which have no
48 ;; prerequisites.
49 ;;
50 ;; The command C-c C-b pops up a browser window listing all target and
51 ;; macro names. You can mark or unmark items with C-c SPC, and insert
52 ;; all marked items back in the Makefile with C-c TAB.
53 ;;
54 ;; The command C-c TAB in the makefile buffer inserts a GNU make builtin.
55 ;; You will be prompted for the builtin's args.
56 ;;
57 ;; There are numerous other customization variables.
58
59 ;;
60 ;; To Do:
61 ;;
62 ;; * Add missing doc strings, improve terse doc strings.
63 ;; * Eliminate electric stuff entirely.
64 ;; * It might be nice to highlight targets differently depending on
65 ;; whether they are up-to-date or not. Not sure how this would
66 ;; interact with font-lock.
67 ;; * Would be nice to edit the commands in ksh-mode and have
68 ;; indentation and slashification done automatically. Hard.
69 ;; * Consider removing browser mode. It seems useless.
70 ;; * ":" should notice when a new target is made and add it to the
71 ;; list (or at least set makefile-need-target-pickup).
72 ;; * Make browser into a major mode.
73 ;; * Clean up macro insertion stuff. It is a mess.
74 ;; * Browser entry and exit is weird. Normalize.
75 ;; * Browser needs to be rewritten. Right now it is kind of a crock.
76 ;; Should at least:
77 ;; * Act more like dired/buffer menu/whatever.
78 ;; * Highlight as mouse traverses.
79 ;; * B2 inserts.
80 ;; * Update documentation above.
81 ;; * Update texinfo manual.
82 ;; * Update files.el.
83
84 \f
85
86 ;;; Code:
87
88 ;; Sadly we need this for a macro.
89 (eval-when-compile
90 (require 'imenu)
91 (require 'dabbrev)
92 (require 'add-log))
93
94 ;;; ------------------------------------------------------------
95 ;;; Configurable stuff
96 ;;; ------------------------------------------------------------
97
98 (defgroup makefile nil
99 "Makefile editing commands for Emacs."
100 :link '(custom-group-link :tag "Font Lock Faces group" font-lock-faces)
101 :group 'tools
102 :prefix "makefile-")
103
104 (defface makefile-space
105 '((((class color)) (:background "hotpink"))
106 (t (:reverse-video t)))
107 "Face to use for highlighting leading spaces in Font-Lock mode."
108 :group 'makefile)
109 (put 'makefile-space-face 'face-alias 'makefile-space)
110
111 (defface makefile-targets
112 ;; This needs to go along both with foreground and background colors (i.e. shell)
113 '((t (:inherit font-lock-function-name-face)))
114 "Face to use for additionally highlighting rule targets in Font-Lock mode."
115 :group 'makefile
116 :version "22.1")
117
118 (defface makefile-shell
119 ()
120 ;;'((((class color) (min-colors 88) (background light)) (:background "seashell1"))
121 ;; (((class color) (min-colors 88) (background dark)) (:background "seashell4")))
122 "Face to use for additionally highlighting Shell commands in Font-Lock mode."
123 :group 'makefile
124 :version "22.1")
125
126 (defface makefile-makepp-perl
127 '((((class color) (background light)) (:background "LightBlue1")) ; Camel Book
128 (((class color) (background dark)) (:background "DarkBlue"))
129 (t (:reverse-video t)))
130 "Face to use for additionally highlighting Perl code in Font-Lock mode."
131 :group 'makefile
132 :version "22.1")
133
134 (defcustom makefile-browser-buffer-name "*Macros and Targets*"
135 "*Name of the macro- and target browser buffer."
136 :type 'string
137 :group 'makefile)
138
139 (defcustom makefile-target-colon ":"
140 "*String to append to all target names inserted by `makefile-insert-target'.
141 \":\" or \"::\" are common values."
142 :type 'string
143 :group 'makefile)
144
145 (defcustom makefile-macro-assign " = "
146 "*String to append to all macro names inserted by `makefile-insert-macro'.
147 The normal value should be \" = \", since this is what
148 standard make expects. However, newer makes such as dmake
149 allow a larger variety of different macro assignments, so you
150 might prefer to use \" += \" or \" := \" ."
151 :type 'string
152 :group 'makefile)
153
154 (defcustom makefile-electric-keys nil
155 "*If non-nil, Makefile mode should install electric keybindings.
156 Default is nil."
157 :type 'boolean
158 :group 'makefile)
159
160 (defcustom makefile-use-curly-braces-for-macros-p nil
161 "*Controls the style of generated macro references.
162 Non-nil means macro references should use curly braces, like `${this}'.
163 nil means use parentheses, like `$(this)'."
164 :type 'boolean
165 :group 'makefile)
166
167 (defcustom makefile-tab-after-target-colon t
168 "*If non-nil, insert a TAB after a target colon.
169 Otherwise, a space is inserted.
170 The default is t."
171 :type 'boolean
172 :group 'makefile)
173
174 (defcustom makefile-browser-leftmost-column 10
175 "*Number of blanks to the left of the browser selection mark."
176 :type 'integer
177 :group 'makefile)
178
179 (defcustom makefile-browser-cursor-column 10
180 "*Column the cursor goes to when it moves up or down in the Makefile browser."
181 :type 'integer
182 :group 'makefile)
183
184 (defcustom makefile-backslash-column 48
185 "*Column in which `makefile-backslash-region' inserts backslashes."
186 :type 'integer
187 :group 'makefile)
188
189 (defcustom makefile-backslash-align t
190 "*If non-nil, `makefile-backslash-region' will align backslashes."
191 :type 'boolean
192 :group 'makefile)
193
194 (defcustom makefile-browser-selected-mark "+ "
195 "*String used to mark selected entries in the Makefile browser."
196 :type 'string
197 :group 'makefile)
198
199 (defcustom makefile-browser-unselected-mark " "
200 "*String used to mark unselected entries in the Makefile browser."
201 :type 'string
202 :group 'makefile)
203
204 (defcustom makefile-browser-auto-advance-after-selection-p t
205 "*If non-nil, cursor will move after item is selected in Makefile browser."
206 :type 'boolean
207 :group 'makefile)
208
209 (defcustom makefile-pickup-everything-picks-up-filenames-p nil
210 "*If non-nil, `makefile-pickup-everything' picks up filenames as targets.
211 This means it calls `makefile-pickup-filenames-as-targets'.
212 Otherwise filenames are omitted."
213 :type 'boolean
214 :group 'makefile)
215
216 (defcustom makefile-cleanup-continuations nil
217 "*If non-nil, automatically clean up continuation lines when saving.
218 A line is cleaned up by removing all whitespace following a trailing
219 backslash. This is done silently.
220 IMPORTANT: Please note that enabling this option causes Makefile mode
221 to MODIFY A FILE WITHOUT YOUR CONFIRMATION when \"it seems necessary\"."
222 :type 'boolean
223 :group 'makefile)
224
225 (defcustom makefile-mode-hook nil
226 "*Normal hook run by `makefile-mode'."
227 :type 'hook
228 :group 'makefile)
229
230 (defvar makefile-browser-hook '())
231
232 ;;
233 ;; Special targets for DMake, Sun's make ...
234 ;;
235 (defcustom makefile-special-targets-list
236 '(("DEFAULT") ("DONE") ("ERROR") ("EXPORT")
237 ("FAILED") ("GROUPEPILOG") ("GROUPPROLOG") ("IGNORE")
238 ("IMPORT") ("INCLUDE") ("INCLUDEDIRS") ("INIT")
239 ("KEEP_STATE") ("MAKEFILES") ("MAKE_VERSION") ("NO_PARALLEL")
240 ("PARALLEL") ("PHONY") ("PRECIOUS") ("REMOVE")
241 ("SCCS_GET") ("SILENT") ("SOURCE") ("SUFFIXES")
242 ("WAIT") ("c.o") ("C.o") ("m.o")
243 ("el.elc") ("y.c") ("s.o"))
244 "*List of special targets.
245 You will be offered to complete on one of those in the minibuffer whenever
246 you enter a \".\" at the beginning of a line in `makefile-mode'."
247 :type '(repeat (list string))
248 :group 'makefile)
249 (put 'makefile-special-targets-list 'risky-local-variable t)
250
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)
259
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]*$\\|[^=\n]\\(?:[^#\n]*?;[ \t]*\\(.+\\)\\)?\\)"
266 "Regex used to find dependency lines in a makefile.")
267
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.")
272
273 (defvar makefile-dependency-skip "^:"
274 "Characters to skip to find a line that might be a dependency.")
275
276 (defvar makefile-rule-action-regex
277 "^\t[ \t]*\\([-@]*\\)[ \t]*\\(\\(?:.*\\\\\n\\)*.*\\)"
278 "Regex used to highlight rule action lines in font lock mode.")
279
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.")
284
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.")
289
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 ;; We used to match not just the varname but also the whole value
295 ;; (spanning potentially several lines).
296 ;; "^ *\\([^ \n\t][^:#= \t\n]*\\)[ \t]*\\(?:!=[ \t]*\\(\\(?:.+\\\\\n\\)*.+\\)\\|[*:+]?[:?]?=[ \t]*\\(\\(?:.*\\\\\n\\)*.*\\)\\)"
297 ;; What about the define statement? What about differentiating this for makepp?
298 "\\(?:^\\|^export\\|^override\\|:\\|: *override\\) *\\([^ \n\t][^:#= \t\n]*\\)[ \t]*\\(?:!=\\|[*:+]?[:?]?=\\)"
299 "Regex used to find macro assignment lines in a makefile.")
300
301 (defconst makefile-var-use-regex
302 "[^$]\\$[({]\\([-a-zA-Z0-9_.]+\\|[@%<?^+*][FD]?\\)"
303 "Regex used to find $(macro) uses in a makefile.")
304
305 (defconst makefile-ignored-files-in-pickup-regex
306 "\\(^\\..*\\)\\|\\(.*~$\\)\\|\\(.*,v$\\)\\|\\(\\.[chy]\\)"
307 "Regex for filenames that will NOT be included in the target list.")
308
309 (defvar makefile-space 'makefile-space
310 "Face to use for highlighting leading spaces in Font-Lock mode.")
311
312 ;; These lists were inspired by the old solution. But they are silly, because
313 ;; you can't differentiate what follows. They need to be split up.
314 (defconst makefile-statements '("include")
315 "List of keywords understood by standard make.")
316
317 (defconst makefile-automake-statements
318 `("if" "else" "endif" ,@makefile-statements)
319 "List of keywords understood by automake.")
320
321 (defconst makefile-gmake-statements
322 `("-sinclude" "sinclude" "vpath" ; makefile-makepp-statements takes rest
323 "ifdef" "ifndef" "ifeq" "ifneq" "-include" "define" "endef" "export"
324 "override define" "override" "unexport"
325 ,@(cdr makefile-automake-statements))
326 "List of keywords understood by gmake.")
327
328 ;; These are even more silly, because you can have more spaces in between.
329 (defconst makefile-makepp-statements
330 `("and ifdef" "and ifndef" "and ifeq" "and ifneq" "and ifperl"
331 "and ifmakeperl" "and ifsys" "and ifnsys" "build_cache" "build_check"
332 "else ifdef" "else ifndef" "else ifeq" "else ifneq" "else ifperl"
333 "else ifmakeperl" "else ifsys" "else ifnsys" "enddef" "global"
334 "load_makefile" "ifperl" "ifmakeperl" "ifsys" "ifnsys" "_include"
335 "makeperl" "makesub" "no_implicit_load" "perl" "perl-begin" "perl_begin"
336 "perl-end" "perl_end" "prebuild" "or ifdef" "or ifndef" "or ifeq"
337 "or ifneq" "or ifperl" "or ifmakeperl" "or ifsys" "or ifnsys"
338 "override export" "override global" "register_command_parser"
339 "register_scanner" "repository" "runtime" "signature" "sub"
340 ,@(nthcdr 3 makefile-gmake-statements))
341 "List of keywords understood by gmake.")
342
343 (defconst makefile-bsdmake-statements
344 `(".elif" ".elifdef" ".elifmake" ".elifndef" ".elifnmake" ".else" ".endfor"
345 ".endif" ".for" ".if" ".ifdef" ".ifmake" ".ifndef" ".ifnmake" ".undef")
346 "List of keywords understood by BSD make.")
347
348 (defun makefile-make-font-lock-keywords (var keywords space
349 &optional negation
350 &rest font-lock-keywords)
351 `(;; Do macro assignments. These get the "variable-name" face.
352 (,makefile-macroassign-regex
353 (1 font-lock-variable-name-face)
354 ;; This is for after !=
355 (2 'makefile-shell prepend t)
356 ;; This is for after normal assignment
357 (3 'font-lock-string-face prepend t))
358
359 ;; Rule actions.
360 (makefile-match-action
361 (1 font-lock-type-face)
362 (2 'makefile-shell prepend)
363 ;; Only makepp has builtin commands.
364 (3 font-lock-builtin-face prepend t))
365
366 ;; Variable references even in targets/strings/comments.
367 (,var 1 font-lock-variable-name-face prepend)
368
369 ;; Automatic variable references and single character variable references,
370 ;; but not shell variables references.
371 ("[^$]\\$\\([@%<?^+*_]\\|[a-zA-Z0-9]\\>\\)"
372 1 font-lock-constant-face prepend)
373 ("[^$]\\(\\$[@%*]\\)"
374 1 'makefile-targets append)
375
376 ;; Fontify conditionals and includes.
377 (,(concat "^\\(?: [ \t]*\\)?"
378 (regexp-opt keywords t)
379 "\\>[ \t]*\\([^: \t\n#]*\\)")
380 (1 font-lock-keyword-face) (2 font-lock-variable-name-face))
381
382 ,@(if negation
383 `((,negation (1 font-lock-negation-char-face prepend)
384 (2 font-lock-negation-char-face prepend t))))
385
386 ,@(if space
387 '(;; Highlight lines that contain just whitespace.
388 ;; They can cause trouble, especially if they start with a tab.
389 ("^[ \t]+$" . makefile-space)
390
391 ;; Highlight shell comments that Make treats as commands,
392 ;; since these can fool people.
393 ("^\t+#" 0 makefile-space t)
394
395 ;; Highlight spaces that precede tabs.
396 ;; They can make a tab fail to be effective.
397 ("^\\( +\\)\t" 1 makefile-space)))
398
399 ,@font-lock-keywords
400
401 ;; Do dependencies.
402 (makefile-match-dependency
403 (1 'makefile-targets prepend)
404 (3 'makefile-shell prepend t))))
405
406 (defconst makefile-font-lock-keywords
407 (makefile-make-font-lock-keywords
408 makefile-var-use-regex
409 makefile-statements
410 t))
411
412 (defconst makefile-automake-font-lock-keywords
413 (makefile-make-font-lock-keywords
414 makefile-var-use-regex
415 makefile-automake-statements
416 t))
417
418 (defconst makefile-gmake-font-lock-keywords
419 (makefile-make-font-lock-keywords
420 makefile-var-use-regex
421 makefile-gmake-statements
422 t
423 "^\\(?: [ \t]*\\)?if\\(n\\)\\(?:def\\|eq\\)\\>"
424
425 '("[^$]\\(\\$[({][@%*][DF][})]\\)"
426 1 'makefile-targets append)
427
428 ;; $(function ...) ${function ...}
429 '("[^$]\\$[({]\\([-a-zA-Z0-9_.]+\\s \\)"
430 1 font-lock-function-name-face prepend)
431
432 ;; $(shell ...) ${shell ...}
433 '("[^$]\\$\\([({]\\)shell[ \t]+"
434 makefile-match-function-end nil nil
435 (1 'makefile-shell prepend t))))
436
437 (defconst makefile-makepp-font-lock-keywords
438 (makefile-make-font-lock-keywords
439 makefile-var-use-regex
440 makefile-makepp-statements
441 nil
442 "^\\(?: [ \t]*\\)?\\(?:and[ \t]+\\|else[ \t]+\\|or[ \t]+\\)?if\\(n\\)\\(?:def\\|eq\\|sys\\)\\>"
443
444 '("[^$]\\(\\$[({]\\(?:output\\|stem\\|target\\)s?\\_>.*?[})]\\)"
445 1 'makefile-targets append)
446
447 ;; Colon modifier keywords.
448 '("\\(:\\s *\\)\\(build_c\\(?:ache\\|heck\\)\\|env\\(?:ironment\\)?\\|foreach\\|signature\\|scanner\\|quickscan\\|smartscan\\)\\>\\([^:\n]*\\)"
449 (1 font-lock-type-face t)
450 (2 font-lock-keyword-face t)
451 (3 font-lock-variable-name-face t))
452
453 ;; $(function ...) $((function ...)) ${function ...} ${{function ...}}
454 '("[^$]\\$\\(?:((?\\|{{?\\)\\([-a-zA-Z0-9_.]+\\s \\)"
455 1 font-lock-function-name-face prepend)
456
457 ;; $(shell ...) $((shell ...)) ${shell ...} ${{shell ...}}
458 '("[^$]\\$\\(((?\\|{{?\\)shell\\(?:[-_]\\(?:global[-_]\\)?once\\)?[ \t]+"
459 makefile-match-function-end nil nil
460 (1 'makefile-shell prepend t))
461
462 ;; $(perl ...) $((perl ...)) ${perl ...} ${{perl ...}}
463 '("[^$]\\$\\(((?\\|{{?\\)makeperl[ \t]+"
464 makefile-match-function-end nil nil
465 (1 'makefile-makepp-perl prepend t))
466 '("[^$]\\$\\(((?\\|{{?\\)perl[ \t]+"
467 makefile-match-function-end nil nil
468 (1 'makefile-makepp-perl t t))
469
470 ;; Can we unify these with (if (match-end 1) 'prepend t)?
471 '("ifmakeperl\\s +\\(.*\\)" 1 'makefile-makepp-perl prepend)
472 '("ifperl\\s +\\(.*\\)" 1 'makefile-makepp-perl t)
473
474 ;; Perl block single- or multiline, as statement or rule action.
475 ;; Don't know why the initial newline in 2nd variant of group 2 doesn't get skipped.
476 '("\\<make\\(?:perl\\|sub\\s +\\S +\\)\\s *\n?\\s *{\\(?:{\\s *\n?\\(\\(?:.*\n\\)+?\\)\\s *}\\|\\s *\\(\\(?:.*?\\|\n?\\(?:.*\n\\)+?\\)\\)\\)}"
477 (1 'makefile-makepp-perl prepend t)
478 (2 'makefile-makepp-perl prepend t))
479 '("\\<\\(?:perl\\|sub\\s +\\S +\\)\\s *\n?\\s *{\\(?:{\\s *\n?\\(\\(?:.*\n\\)+?\\)\\s *}\\|\\s *\\(\\(?:.*?\\|\n?\\(?:.*\n\\)+?\\)\\)\\)}"
480 (1 'makefile-makepp-perl t t)
481 (2 'makefile-makepp-perl t t))
482
483 ;; Statement style perl block.
484 '("perl[-_]begin\\s *\\(?:\\s #.*\\)?\n\\(\\(?:.*\n\\)+?\\)\\s *perl[-_]end\\>"
485 1 'makefile-makepp-perl t)))
486
487 (defconst makefile-bsdmake-font-lock-keywords
488 (makefile-make-font-lock-keywords
489 ;; A lot more could be done for variables here:
490 makefile-var-use-regex
491 makefile-bsdmake-statements
492 t
493 "^\\(?: [ \t]*\\)?\\.\\(?:el\\)?if\\(n?\\)\\(?:def\\|make\\)?\\>[ \t]*\\(!?\\)"
494 '("^[ \t]*\\.for[ \t].+[ \t]\\(in\\)\\>" 1 font-lock-keyword-face)))
495
496 (defconst makefile-imake-font-lock-keywords
497 (append
498 (makefile-make-font-lock-keywords
499 makefile-var-use-regex
500 makefile-statements
501 t
502 nil
503 '("^XCOMM.*$" . font-lock-comment-face)
504 '("XVAR\\(?:use\\|def\\)[0-9]" 0 font-lock-keyword-face prepend)
505 '("@@" . font-lock-preprocessor-face)
506 )
507 cpp-font-lock-keywords))
508
509
510 (defconst makefile-font-lock-syntactic-keywords
511 ;; From sh-script.el.
512 ;; A `#' begins a comment in sh when it is unquoted and at the beginning
513 ;; of a word. In the shell, words are separated by metacharacters.
514 ;; The list of special chars is taken from the single-unix spec of the
515 ;; shell command language (under `quoting') but with `$' removed.
516 '(("[^|&;<>()`\\\"' \t\n]\\(#+\\)" 1 "_")
517 ;; Change the syntax of a quoted newline so that it does not end a comment.
518 ("\\\\\n" 0 ".")))
519
520 (defvar makefile-imenu-generic-expression
521 `(("Dependencies" makefile-previous-dependency 1)
522 ("Macro Assignment" ,makefile-macroassign-regex 1))
523 "Imenu generic expression for Makefile mode. See `imenu-generic-expression'.")
524
525 ;;; ------------------------------------------------------------
526 ;;; The following configurable variables are used in the
527 ;;; up-to-date overview .
528 ;;; The standard configuration assumes that your `make' program
529 ;;; can be run in question/query mode using the `-q' option, this
530 ;;; means that the command
531 ;;;
532 ;;; make -q foo
533 ;;;
534 ;;; should return an exit status of zero if the target `foo' is
535 ;;; up to date and a nonzero exit status otherwise.
536 ;;; Many makes can do this although the docs/manpages do not mention
537 ;;; it. Try it with your favourite one. GNU make, System V make, and
538 ;;; Dennis Vadura's DMake have no problems.
539 ;;; Set the variable `makefile-brave-make' to the name of the
540 ;;; make utility that does this on your system.
541 ;;; To understand what this is all about see the function definition
542 ;;; of `makefile-query-by-make-minus-q' .
543 ;;; ------------------------------------------------------------
544
545 (defcustom makefile-brave-make "make"
546 "*How to invoke make, for `makefile-query-targets'.
547 This should identify a `make' command that can handle the `-q' option."
548 :type 'string
549 :group 'makefile)
550
551 (defcustom makefile-query-one-target-method-function
552 'makefile-query-by-make-minus-q
553 "*Function to call to determine whether a make target is up to date.
554 The function must satisfy this calling convention:
555
556 * As its first argument, it must accept the name of the target to
557 be checked, as a string.
558
559 * As its second argument, it may accept the name of a makefile
560 as a string. Depending on what you're going to do you may
561 not need this.
562
563 * It must return the integer value 0 (zero) if the given target
564 should be considered up-to-date in the context of the given
565 makefile, any nonzero integer value otherwise."
566 :type 'function
567 :group 'makefile)
568 (defvaralias 'makefile-query-one-target-method
569 'makefile-query-one-target-method-function)
570
571 (defcustom makefile-up-to-date-buffer-name "*Makefile Up-to-date overview*"
572 "*Name of the Up-to-date overview buffer."
573 :type 'string
574 :group 'makefile)
575
576 ;;; --- end of up-to-date-overview configuration ------------------
577
578 (defvar makefile-mode-abbrev-table nil
579 "Abbrev table in use in Makefile buffers.")
580 (if makefile-mode-abbrev-table
581 ()
582 (define-abbrev-table 'makefile-mode-abbrev-table ()))
583
584 (defvar makefile-mode-map
585 (let ((map (make-sparse-keymap))
586 (opt-map (make-sparse-keymap)))
587 ;; set up the keymap
588 (define-key map "\C-c:" 'makefile-insert-target-ref)
589 (if makefile-electric-keys
590 (progn
591 (define-key map "$" 'makefile-insert-macro-ref)
592 (define-key map ":" 'makefile-electric-colon)
593 (define-key map "=" 'makefile-electric-equal)
594 (define-key map "." 'makefile-electric-dot)))
595 (define-key map "\C-c\C-f" 'makefile-pickup-filenames-as-targets)
596 (define-key map "\C-c\C-b" 'makefile-switch-to-browser)
597 (define-key map "\C-c\C-c" 'comment-region)
598 (define-key map "\C-c\C-p" 'makefile-pickup-everything)
599 (define-key map "\C-c\C-u" 'makefile-create-up-to-date-overview)
600 (define-key map "\C-c\C-i" 'makefile-insert-gmake-function)
601 (define-key map "\C-c\C-\\" 'makefile-backslash-region)
602 (define-key map "\C-c\C-m\C-a" 'makefile-automake-mode)
603 (define-key map "\C-c\C-m\C-b" 'makefile-bsdmake-mode)
604 (define-key map "\C-c\C-m\C-g" 'makefile-gmake-mode)
605 (define-key map "\C-c\C-m\C-i" 'makefile-imake-mode)
606 (define-key map "\C-c\C-m\C-m" 'makefile-mode)
607 (define-key map "\C-c\C-m\C-p" 'makefile-makepp-mode)
608 (define-key map "\M-p" 'makefile-previous-dependency)
609 (define-key map "\M-n" 'makefile-next-dependency)
610 (define-key map "\e\t" 'makefile-complete)
611
612 ;; Make menus.
613 (define-key map [menu-bar makefile-mode]
614 (cons "Makefile" (make-sparse-keymap "Makefile")))
615
616 (define-key map [menu-bar makefile-mode makefile-type]
617 (cons "Switch Makefile Type" opt-map))
618 (define-key opt-map [makefile-makepp-mode]
619 '(menu-item "Makepp" makefile-makepp-mode
620 :help "An adapted `makefile-mode' that knows about makepp"
621 :button (:radio . (eq major-mode 'makefile-makepp-mode))))
622 (define-key opt-map [makefile-imake-mode]
623 '(menu-item "Imake" makefile-imake-mode
624 :help "An adapted `makefile-mode' that knows about imake"
625 :button (:radio . (eq major-mode 'makefile-imake-mode))))
626 (define-key opt-map [makefile-mode]
627 '(menu-item "Classic" makefile-mode
628 :help "`makefile-mode' with no special functionality"
629 :button (:radio . (eq major-mode 'makefile-mode))))
630 (define-key opt-map [makefile-bsdmake-mode]
631 '(menu-item "BSD" makefile-bsdmake-mode
632 :help "An adapted `makefile-mode' that knows about BSD make"
633 :button (:radio . (eq major-mode 'makefile-bsdmake-mode))))
634 (define-key opt-map [makefile-automake-mode]
635 '(menu-item "Automake" makefile-automake-mode
636 :help "An adapted `makefile-mode' that knows about automake"
637 :button (:radio . (eq major-mode 'makefile-automake-mode))))
638 (define-key opt-map [makefile-gmake-mode]
639 '(menu-item "GNU make" makefile-gmake-mode
640 :help "An adapted `makefile-mode' that knows about GNU make"
641 :button (:radio . (eq major-mode 'makefile-gmake-mode))))
642 (define-key map [menu-bar makefile-mode browse]
643 '(menu-item "Pop up Makefile Browser" makefile-switch-to-browser
644 ;; XXX: this needs a better string, the function is not documented...
645 :help "Pop up Makefile Browser"))
646 (define-key map [menu-bar makefile-mode overview]
647 '(menu-item "Up To Date Overview" makefile-create-up-to-date-overview
648 :help "Create a buffer containing an overview of the state of all known targets"))
649 ;; Target related
650 (define-key map [menu-bar makefile-mode separator1] '("----"))
651 (define-key map [menu-bar makefile-mode pickup-file]
652 '(menu-item "Pick File Name as Target" makefile-pickup-filenames-as-targets
653 :help "Scan the current directory for filenames to use as targets"))
654 (define-key map [menu-bar makefile-mode function]
655 '(menu-item "Insert GNU make function" makefile-insert-gmake-function
656 :help "Insert a GNU make function call"))
657 (define-key map [menu-bar makefile-mode pickup]
658 '(menu-item "Find Targets and Macros" makefile-pickup-everything
659 :help "Notice names of all macros and targets in Makefile"))
660 (define-key map [menu-bar makefile-mode complete]
661 '(menu-item "Complete Target or Macro" makefile-complete
662 :help "Perform completion on Makefile construct preceding point"))
663 (define-key map [menu-bar makefile-mode backslash]
664 '(menu-item "Backslash Region" makefile-backslash-region
665 :help "Insert, align, or delete end-of-line backslashes on the lines in the region"))
666 ;; Motion
667 (define-key map [menu-bar makefile-mode separator] '("----"))
668 (define-key map [menu-bar makefile-mode prev]
669 '(menu-item "Move to Previous Dependency" makefile-previous-dependency
670 :help "Move point to the beginning of the previous dependency line"))
671 (define-key map [menu-bar makefile-mode next]
672 '(menu-item "Move to Next Dependency" makefile-next-dependency
673 :help "Move point to the beginning of the next dependency line"))
674 map)
675 "The keymap that is used in Makefile mode.")
676
677
678 (defvar makefile-browser-map
679 (let ((map (make-sparse-keymap)))
680 (define-key map "n" 'makefile-browser-next-line)
681 (define-key map "\C-n" 'makefile-browser-next-line)
682 (define-key map "p" 'makefile-browser-previous-line)
683 (define-key map "\C-p" 'makefile-browser-previous-line)
684 (define-key map " " 'makefile-browser-toggle)
685 (define-key map "i" 'makefile-browser-insert-selection)
686 (define-key map "I" 'makefile-browser-insert-selection-and-quit)
687 (define-key map "\C-c\C-m" 'makefile-browser-insert-continuation)
688 (define-key map "q" 'makefile-browser-quit)
689 ;; disable horizontal movement
690 (define-key map "\C-b" 'undefined)
691 (define-key map "\C-f" 'undefined)
692 map)
693 "The keymap that is used in the macro- and target browser.")
694
695
696 (defvar makefile-mode-syntax-table
697 (let ((st (make-syntax-table)))
698 (modify-syntax-entry ?\( "() " st)
699 (modify-syntax-entry ?\) ")( " st)
700 (modify-syntax-entry ?\[ "(] " st)
701 (modify-syntax-entry ?\] ")[ " st)
702 (modify-syntax-entry ?\{ "(} " st)
703 (modify-syntax-entry ?\} "){ " st)
704 (modify-syntax-entry ?\' "\" " st)
705 (modify-syntax-entry ?\` "\" " st)
706 (modify-syntax-entry ?# "< " st)
707 (modify-syntax-entry ?\n "> " st)
708 st))
709
710 (defvar makefile-imake-mode-syntax-table (copy-syntax-table
711 makefile-mode-syntax-table))
712 (if makefile-imake-mode-syntax-table
713 ()
714 (modify-syntax-entry ?/ ". 14" makefile-imake-mode-syntax-table)
715 (modify-syntax-entry ?* ". 23" makefile-imake-mode-syntax-table)
716 (modify-syntax-entry ?# "'" makefile-imake-mode-syntax-table)
717 (modify-syntax-entry ?\n ". b" makefile-imake-mode-syntax-table))
718
719
720 ;;; ------------------------------------------------------------
721 ;;; Internal variables.
722 ;;; You don't need to configure below this line.
723 ;;; ------------------------------------------------------------
724
725 (defvar makefile-target-table nil
726 "Table of all target names known for this buffer.")
727 (put 'makefile-target-table 'risky-local-variable t)
728
729 (defvar makefile-macro-table nil
730 "Table of all macro names known for this buffer.")
731 (put 'makefile-macro-table 'risky-local-variable t)
732
733 (defvar makefile-browser-client
734 "A buffer in Makefile mode that is currently using the browser.")
735
736 (defvar makefile-browser-selection-vector nil)
737 (defvar makefile-has-prereqs nil)
738 (defvar makefile-need-target-pickup t)
739 (defvar makefile-need-macro-pickup t)
740
741 (defvar makefile-mode-hook '())
742
743 ;; Each element looks like '("GNU MAKE FUNCTION" "ARG" "ARG" ... )
744 ;; Each "ARG" is used as a prompt for a required argument.
745 (defconst makefile-gnumake-functions-alist
746 '(
747 ;; Text functions
748 ("subst" "From" "To" "In")
749 ("patsubst" "Pattern" "Replacement" "In")
750 ("strip" "Text")
751 ("findstring" "Find what" "In")
752 ("filter" "Pattern" "Text")
753 ("filter-out" "Pattern" "Text")
754 ("sort" "List")
755 ;; Filename functions
756 ("dir" "Names")
757 ("notdir" "Names")
758 ("suffix" "Names")
759 ("basename" "Names")
760 ("addprefix" "Prefix" "Names")
761 ("addsuffix" "Suffix" "Names")
762 ("join" "List 1" "List 2")
763 ("word" "Index" "Text")
764 ("words" "Text")
765 ("firstword" "Text")
766 ("wildcard" "Pattern")
767 ;; Misc functions
768 ("foreach" "Variable" "List" "Text")
769 ("origin" "Variable")
770 ("shell" "Command")))
771
772
773 ;;; ------------------------------------------------------------
774 ;;; The mode function itself.
775 ;;; ------------------------------------------------------------
776
777 ;;;###autoload
778 (defun makefile-mode ()
779 "Major mode for editing standard Makefiles.
780
781 If you are editing a file for a different make, try one of the
782 variants `makefile-automake-mode', `makefile-gmake-mode',
783 `makefile-makepp-mode', `makefile-bsdmake-mode' or,
784 `makefile-imake-mode'. All but the last should be correctly
785 chosen based on the file name, except if it is *.mk. This
786 function ends by invoking the function(s) `makefile-mode-hook'.
787
788 It is strongly recommended to use `font-lock-mode', because that
789 provides additional parsing information. This is used for
790 example to see that a rule action `echo foo: bar' is a not rule
791 dependency, despite the colon.
792
793 \\{makefile-mode-map}
794
795 In the browser, use the following keys:
796
797 \\{makefile-browser-map}
798
799 Makefile mode can be configured by modifying the following variables:
800
801 `makefile-browser-buffer-name':
802 Name of the macro- and target browser buffer.
803
804 `makefile-target-colon':
805 The string that gets appended to all target names
806 inserted by `makefile-insert-target'.
807 \":\" or \"::\" are quite common values.
808
809 `makefile-macro-assign':
810 The string that gets appended to all macro names
811 inserted by `makefile-insert-macro'.
812 The normal value should be \" = \", since this is what
813 standard make expects. However, newer makes such as dmake
814 allow a larger variety of different macro assignments, so you
815 might prefer to use \" += \" or \" := \" .
816
817 `makefile-tab-after-target-colon':
818 If you want a TAB (instead of a space) to be appended after the
819 target colon, then set this to a non-nil value.
820
821 `makefile-browser-leftmost-column':
822 Number of blanks to the left of the browser selection mark.
823
824 `makefile-browser-cursor-column':
825 Column in which the cursor is positioned when it moves
826 up or down in the browser.
827
828 `makefile-browser-selected-mark':
829 String used to mark selected entries in the browser.
830
831 `makefile-browser-unselected-mark':
832 String used to mark unselected entries in the browser.
833
834 `makefile-browser-auto-advance-after-selection-p':
835 If this variable is set to a non-nil value the cursor
836 will automagically advance to the next line after an item
837 has been selected in the browser.
838
839 `makefile-pickup-everything-picks-up-filenames-p':
840 If this variable is set to a non-nil value then
841 `makefile-pickup-everything' also picks up filenames as targets
842 (i.e. it calls `makefile-pickup-filenames-as-targets'), otherwise
843 filenames are omitted.
844
845 `makefile-cleanup-continuations':
846 If this variable is set to a non-nil value then Makefile mode
847 will assure that no line in the file ends with a backslash
848 (the continuation character) followed by any whitespace.
849 This is done by silently removing the trailing whitespace, leaving
850 the backslash itself intact.
851 IMPORTANT: Please note that enabling this option causes Makefile mode
852 to MODIFY A FILE WITHOUT YOUR CONFIRMATION when \"it seems necessary\".
853
854 `makefile-browser-hook':
855 A function or list of functions to be called just before the
856 browser is entered. This is executed in the makefile buffer.
857
858 `makefile-special-targets-list':
859 List of special targets. You will be offered to complete
860 on one of those in the minibuffer whenever you enter a `.'.
861 at the beginning of a line in Makefile mode."
862
863 (interactive)
864 (kill-all-local-variables)
865 (add-hook 'write-file-functions
866 'makefile-warn-suspicious-lines nil t)
867 (add-hook 'write-file-functions
868 'makefile-warn-continuations nil t)
869 (add-hook 'write-file-functions
870 'makefile-cleanup-continuations nil t)
871 (make-local-variable 'makefile-target-table)
872 (make-local-variable 'makefile-macro-table)
873 (make-local-variable 'makefile-has-prereqs)
874 (make-local-variable 'makefile-need-target-pickup)
875 (make-local-variable 'makefile-need-macro-pickup)
876
877 ;; Font lock.
878 (make-local-variable 'font-lock-defaults)
879 (setq font-lock-defaults
880 ;; SYNTAX-BEGIN set to backward-paragraph to avoid slow-down
881 ;; near the end of a large buffer, due to parse-partial-sexp's
882 ;; trying to parse all the way till the beginning of buffer.
883 '(makefile-font-lock-keywords
884 nil nil
885 ((?$ . "."))
886 backward-paragraph
887 (font-lock-syntactic-keywords
888 . makefile-font-lock-syntactic-keywords)))
889
890 ;; Add-log.
891 (make-local-variable 'add-log-current-defun-function)
892 (setq add-log-current-defun-function 'makefile-add-log-defun)
893
894 ;; Imenu.
895 (make-local-variable 'imenu-generic-expression)
896 (setq imenu-generic-expression makefile-imenu-generic-expression)
897
898 ;; Dabbrev.
899 (make-local-variable 'dabbrev-abbrev-skip-leading-regexp)
900 (setq dabbrev-abbrev-skip-leading-regexp "\\$")
901
902 ;; Other abbrevs.
903 (setq local-abbrev-table makefile-mode-abbrev-table)
904
905 ;; Filling.
906 (make-local-variable 'fill-paragraph-function)
907 (setq fill-paragraph-function 'makefile-fill-paragraph)
908
909 ;; Comment stuff.
910 (make-local-variable 'comment-start)
911 (setq comment-start "#")
912 (make-local-variable 'comment-end)
913 (setq comment-end "")
914 (make-local-variable 'comment-start-skip)
915 (setq comment-start-skip "#+[ \t]*")
916
917 ;; Make sure TAB really inserts \t.
918 (set (make-local-variable 'indent-line-function) 'indent-to-left-margin)
919
920 ;; become the current major mode
921 (setq major-mode 'makefile-mode)
922 (setq mode-name "Makefile")
923
924 ;; Activate keymap and syntax table.
925 (use-local-map makefile-mode-map)
926 (set-syntax-table makefile-mode-syntax-table)
927
928 ;; Real TABs are important in makefiles
929 (setq indent-tabs-mode t)
930 (run-mode-hooks 'makefile-mode-hook))
931
932 ;; These should do more than just differentiate font-lock.
933 ;;;###autoload
934 (define-derived-mode makefile-automake-mode makefile-mode "Makefile.am"
935 "An adapted `makefile-mode' that knows about automake."
936 (setq font-lock-defaults
937 `(makefile-automake-font-lock-keywords ,@(cdr font-lock-defaults))))
938
939 ;;;###autoload
940 (define-derived-mode makefile-gmake-mode makefile-mode "GNUmakefile"
941 "An adapted `makefile-mode' that knows about gmake."
942 (setq font-lock-defaults
943 `(makefile-gmake-font-lock-keywords ,@(cdr font-lock-defaults))))
944
945 ;;;###autoload
946 (define-derived-mode makefile-makepp-mode makefile-mode "Makeppfile"
947 "An adapted `makefile-mode' that knows about makepp."
948 (set (make-local-variable 'makefile-rule-action-regex)
949 makefile-makepp-rule-action-regex)
950 (setq font-lock-defaults
951 `(makefile-makepp-font-lock-keywords ,@(cdr font-lock-defaults))
952 imenu-generic-expression
953 `(("Functions" "^[ \t]*\\(?:make\\)?sub[ \t]+\\([A-Za-z0-9_]+\\)" 1)
954 ,@imenu-generic-expression)))
955
956 ;;;###autoload
957 (define-derived-mode makefile-bsdmake-mode makefile-mode "BSDmakefile"
958 "An adapted `makefile-mode' that knows about BSD make."
959 (set (make-local-variable 'makefile-dependency-regex)
960 makefile-bsdmake-dependency-regex)
961 (set (make-local-variable 'makefile-dependency-skip) "^:!")
962 (set (make-local-variable 'makefile-rule-action-regex)
963 makefile-bsdmake-rule-action-regex)
964 (setq font-lock-defaults
965 `(makefile-bsdmake-font-lock-keywords ,@(cdr font-lock-defaults))))
966
967 ;;;###autoload
968 (define-derived-mode makefile-imake-mode makefile-mode "Imakefile"
969 "An adapted `makefile-mode' that knows about imake."
970 :syntax-table makefile-imake-mode-syntax-table
971 (let ((base `(makefile-imake-font-lock-keywords ,@(cdr font-lock-defaults)))
972 new)
973 ;; Remove `font-lock-syntactic-keywords' entry from font-lock-defaults.
974 (mapc (lambda (elt)
975 (unless (and (consp elt)
976 (eq (car elt) 'font-lock-syntactic-keywords))
977 (setq new (cons elt new))))
978 base)
979 (setq font-lock-defaults (nreverse new))))
980
981 \f
982
983 ;;; Motion code.
984
985 (defun makefile-next-dependency ()
986 "Move point to the beginning of the next dependency line."
987 (interactive)
988 (let ((here (point)))
989 (end-of-line)
990 (if (makefile-match-dependency nil)
991 (progn (beginning-of-line) t) ; indicate success
992 (goto-char here) nil)))
993
994 (defun makefile-previous-dependency ()
995 "Move point to the beginning of the previous dependency line."
996 (interactive)
997 (let ((pt (point)))
998 (beginning-of-line)
999 ;; makefile-match-dependency done backwards:
1000 (catch 'found
1001 (while (progn (skip-chars-backward makefile-dependency-skip)
1002 (not (bobp)))
1003 (or (prog1 (eq (char-after) ?=)
1004 (backward-char))
1005 (get-text-property (point) 'face)
1006 (beginning-of-line)
1007 (if (> (point) (+ (point-min) 2))
1008 (eq (char-before (1- (point))) ?\\))
1009 (if (looking-at makefile-dependency-regex)
1010 (throw 'found t))))
1011 (goto-char pt)
1012 nil)))
1013
1014 \f
1015
1016 ;;; Electric keys. Blech.
1017
1018 (defun makefile-electric-dot (arg)
1019 "Prompt for the name of a special target to insert.
1020 Only does electric insertion at beginning of line.
1021 Anywhere else just self-inserts."
1022 (interactive "p")
1023 (if (bolp)
1024 (makefile-insert-special-target)
1025 (self-insert-command arg)))
1026
1027 (defun makefile-insert-special-target ()
1028 "Prompt for and insert a special target name.
1029 Uses `makefile-special-targets' list."
1030 (interactive)
1031 (makefile-pickup-targets)
1032 (let ((special-target
1033 (completing-read "Special target: "
1034 makefile-special-targets-list nil nil nil)))
1035 (if (zerop (length special-target))
1036 ()
1037 (insert "." special-target ":")
1038 (makefile-forward-after-target-colon))))
1039
1040 (defun makefile-electric-equal (arg)
1041 "Prompt for name of a macro to insert.
1042 Only does prompting if point is at beginning of line.
1043 Anywhere else just self-inserts."
1044 (interactive "p")
1045 (makefile-pickup-macros)
1046 (if (bolp)
1047 (call-interactively 'makefile-insert-macro)
1048 (self-insert-command arg)))
1049
1050 (defun makefile-insert-macro (macro-name)
1051 "Prepare definition of a new macro."
1052 (interactive "sMacro Name: ")
1053 (makefile-pickup-macros)
1054 (if (not (zerop (length macro-name)))
1055 (progn
1056 (beginning-of-line)
1057 (insert macro-name makefile-macro-assign)
1058 (setq makefile-need-macro-pickup t)
1059 (makefile-remember-macro macro-name))))
1060
1061 (defun makefile-insert-macro-ref (macro-name)
1062 "Complete on a list of known macros, then insert complete ref at point."
1063 (interactive
1064 (list
1065 (progn
1066 (makefile-pickup-macros)
1067 (completing-read "Refer to macro: " makefile-macro-table nil nil nil))))
1068 (makefile-do-macro-insertion macro-name))
1069
1070 (defun makefile-insert-target (target-name)
1071 "Prepare definition of a new target (dependency line)."
1072 (interactive "sTarget: ")
1073 (if (not (zerop (length target-name)))
1074 (progn
1075 (beginning-of-line)
1076 (insert target-name makefile-target-colon)
1077 (makefile-forward-after-target-colon)
1078 (end-of-line)
1079 (setq makefile-need-target-pickup t)
1080 (makefile-remember-target target-name))))
1081
1082 (defun makefile-insert-target-ref (target-name)
1083 "Complete on a list of known targets, then insert TARGET-NAME at point."
1084 (interactive
1085 (list
1086 (progn
1087 (makefile-pickup-targets)
1088 (completing-read "Refer to target: " makefile-target-table nil nil nil))))
1089 (if (not (zerop (length target-name)))
1090 (insert target-name " ")))
1091
1092 (defun makefile-electric-colon (arg)
1093 "Prompt for name of new target.
1094 Prompting only happens at beginning of line.
1095 Anywhere else just self-inserts."
1096 (interactive "p")
1097 (if (bolp)
1098 (call-interactively 'makefile-insert-target)
1099 (self-insert-command arg)))
1100
1101 \f
1102
1103 ;;; ------------------------------------------------------------
1104 ;;; Extracting targets and macros from an existing makefile
1105 ;;; ------------------------------------------------------------
1106
1107 (defun makefile-pickup-targets ()
1108 "Notice names of all target definitions in Makefile."
1109 (interactive)
1110 (when makefile-need-target-pickup
1111 (setq makefile-need-target-pickup nil
1112 makefile-target-table nil
1113 makefile-has-prereqs nil)
1114 (save-excursion
1115 (goto-char (point-min))
1116 (while (makefile-match-dependency nil)
1117 (goto-char (match-beginning 1))
1118 (while (let ((target-name
1119 (buffer-substring-no-properties (point)
1120 (progn
1121 (skip-chars-forward "^ \t:#")
1122 (point))))
1123 (has-prereqs
1124 (not (looking-at ":[ \t]*$"))))
1125 (if (makefile-remember-target target-name has-prereqs)
1126 (message "Picked up target \"%s\" from line %d"
1127 target-name (line-number-at-pos)))
1128 (skip-chars-forward " \t")
1129 (not (or (eolp) (eq (char-after) ?:)))))
1130 (forward-line)))
1131 (message "Read targets OK.")))
1132
1133 (defun makefile-pickup-macros ()
1134 "Notice names of all macro definitions in Makefile."
1135 (interactive)
1136 (when makefile-need-macro-pickup
1137 (setq makefile-need-macro-pickup nil
1138 makefile-macro-table nil)
1139 (save-excursion
1140 (goto-char (point-min))
1141 (while (re-search-forward makefile-macroassign-regex nil t)
1142 (goto-char (match-beginning 1))
1143 (let ((macro-name (buffer-substring-no-properties (point)
1144 (progn
1145 (skip-chars-forward "^ \t:#=*")
1146 (point)))))
1147 (if (makefile-remember-macro macro-name)
1148 (message "Picked up macro \"%s\" from line %d"
1149 macro-name (line-number-at-pos))))
1150 (forward-line)))
1151 (message "Read macros OK.")))
1152
1153 (defun makefile-pickup-everything (arg)
1154 "Notice names of all macros and targets in Makefile.
1155 Prefix arg means force pickups to be redone."
1156 (interactive "P")
1157 (if arg
1158 (setq makefile-need-target-pickup t
1159 makefile-need-macro-pickup t))
1160 (makefile-pickup-macros)
1161 (makefile-pickup-targets)
1162 (if makefile-pickup-everything-picks-up-filenames-p
1163 (makefile-pickup-filenames-as-targets)))
1164
1165 (defun makefile-pickup-filenames-as-targets ()
1166 "Scan the current directory for filenames to use as targets.
1167 Checks each filename against `makefile-ignored-files-in-pickup-regex'
1168 and adds all qualifying names to the list of known targets."
1169 (interactive)
1170 (mapc (lambda (name)
1171 (or (file-directory-p name)
1172 (string-match makefile-ignored-files-in-pickup-regex name)
1173 (if (makefile-remember-target name)
1174 (message "Picked up file \"%s\" as target" name))))
1175 (file-name-all-completions "" (or (file-name-directory (buffer-file-name)) ""))))
1176
1177 \f
1178
1179 ;;; Completion.
1180
1181 (defun makefile-complete ()
1182 "Perform completion on Makefile construct preceding point.
1183 Can complete variable and target names.
1184 The context determines which are considered."
1185 (interactive)
1186 (let* ((beg (save-excursion
1187 (skip-chars-backward "^$(){}:#= \t\n")
1188 (point)))
1189 (try (buffer-substring beg (point)))
1190 (do-macros nil)
1191 (paren nil))
1192
1193 (save-excursion
1194 (goto-char beg)
1195 (let ((pc (preceding-char)))
1196 (cond
1197 ;; Beginning of line means anything.
1198 ((bolp)
1199 ())
1200
1201 ;; Preceding "$" means macros only.
1202 ((= pc ?$)
1203 (setq do-macros t))
1204
1205 ;; Preceding "$(" or "${" means macros only.
1206 ((and (or (= pc ?{)
1207 (= pc ?\())
1208 (progn
1209 (setq paren pc)
1210 (backward-char)
1211 (and (not (bolp))
1212 (= (preceding-char) ?$))))
1213 (setq do-macros t)))))
1214
1215 ;; Try completion.
1216 (let* ((table (append (if do-macros
1217 '()
1218 makefile-target-table)
1219 makefile-macro-table))
1220 (completion (try-completion try table)))
1221 (cond
1222 ;; Exact match, so insert closing paren or colon.
1223 ((eq completion t)
1224 (insert (if do-macros
1225 (if (eq paren ?{)
1226 ?}
1227 ?\))
1228 (if (save-excursion
1229 (goto-char beg)
1230 (bolp))
1231 ":"
1232 " "))))
1233
1234 ;; No match.
1235 ((null completion)
1236 (message "Can't find completion for \"%s\"" try)
1237 (ding))
1238
1239 ;; Partial completion.
1240 ((not (string= try completion))
1241 ;; FIXME it would be nice to supply the closing paren if an
1242 ;; exact, unambiguous match were found. That is not possible
1243 ;; right now. Ditto closing ":" for targets.
1244 (delete-region beg (point))
1245
1246 ;; DO-MACROS means doing macros only. If not that, then check
1247 ;; to see if this completion is a macro. Special insertion
1248 ;; must be done for macros.
1249 (if (or do-macros
1250 (assoc completion makefile-macro-table))
1251 (let ((makefile-use-curly-braces-for-macros-p
1252 (or (eq paren ?{)
1253 makefile-use-curly-braces-for-macros-p)))
1254 (delete-backward-char 2)
1255 (makefile-do-macro-insertion completion)
1256 (delete-backward-char 1))
1257
1258 ;; Just insert targets.
1259 (insert completion)))
1260
1261 ;; Can't complete any more, so make completion list. FIXME
1262 ;; this doesn't do the right thing when the completion is
1263 ;; actually inserted. I don't think there is an easy way to do
1264 ;; that.
1265 (t
1266 (message "Making completion list...")
1267 (let ((list (all-completions try table)))
1268 (with-output-to-temp-buffer "*Completions*"
1269 (display-completion-list list try)))
1270 (message "Making completion list...done"))))))
1271
1272 \f
1273
1274 ;; Backslashification. Stolen from cc-mode.el.
1275
1276 (defun makefile-backslash-region (from to delete-flag)
1277 "Insert, align, or delete end-of-line backslashes on the lines in the region.
1278 With no argument, inserts backslashes and aligns existing backslashes.
1279 With an argument, deletes the backslashes.
1280
1281 This function does not modify the last line of the region if the region ends
1282 right at the start of the following line; it does not modify blank lines
1283 at the start of the region. So you can put the region around an entire macro
1284 definition and conveniently use this command."
1285 (interactive "r\nP")
1286 (save-excursion
1287 (goto-char from)
1288 (let ((column makefile-backslash-column)
1289 (endmark (make-marker)))
1290 (move-marker endmark to)
1291 ;; Compute the smallest column number past the ends of all the lines.
1292 (if makefile-backslash-align
1293 (progn
1294 (if (not delete-flag)
1295 (while (< (point) to)
1296 (end-of-line)
1297 (if (= (preceding-char) ?\\)
1298 (progn (forward-char -1)
1299 (skip-chars-backward " \t")))
1300 (setq column (max column (1+ (current-column))))
1301 (forward-line 1)))
1302 ;; Adjust upward to a tab column, if that doesn't push
1303 ;; past the margin.
1304 (if (> (% column tab-width) 0)
1305 (let ((adjusted (* (/ (+ column tab-width -1) tab-width)
1306 tab-width)))
1307 (if (< adjusted (window-width))
1308 (setq column adjusted))))))
1309 ;; Don't modify blank lines at start of region.
1310 (goto-char from)
1311 (while (and (< (point) endmark) (eolp))
1312 (forward-line 1))
1313 ;; Add or remove backslashes on all the lines.
1314 (while (and (< (point) endmark)
1315 ;; Don't backslashify the last line
1316 ;; if the region ends right at the start of the next line.
1317 (save-excursion
1318 (forward-line 1)
1319 (< (point) endmark)))
1320 (if (not delete-flag)
1321 (makefile-append-backslash column)
1322 (makefile-delete-backslash))
1323 (forward-line 1))
1324 (move-marker endmark nil))))
1325
1326 (defun makefile-append-backslash (column)
1327 (end-of-line)
1328 ;; Note that "\\\\" is needed to get one backslash.
1329 (if (= (preceding-char) ?\\)
1330 (progn (forward-char -1)
1331 (delete-horizontal-space)
1332 (indent-to column (if makefile-backslash-align nil 1)))
1333 (indent-to column (if makefile-backslash-align nil 1))
1334 (insert "\\")))
1335
1336 (defun makefile-delete-backslash ()
1337 (end-of-line)
1338 (or (bolp)
1339 (progn
1340 (forward-char -1)
1341 (if (looking-at "\\\\")
1342 (delete-region (1+ (point))
1343 (progn (skip-chars-backward " \t") (point)))))))
1344
1345 \f
1346
1347 ;; Filling
1348
1349 (defun makefile-fill-paragraph (arg)
1350 ;; Fill comments, backslashed lines, and variable definitions
1351 ;; specially.
1352 (save-excursion
1353 (beginning-of-line)
1354 (cond
1355 ((looking-at "^[ \t]*#+\\s-*")
1356 ;; Found a comment. Return nil to let normal filling take place.
1357 nil)
1358
1359 ;; Must look for backslashed-region before looking for variable
1360 ;; assignment.
1361 ((or (eq (char-before (line-end-position 1)) ?\\)
1362 (eq (char-before (line-end-position 0)) ?\\))
1363 ;; A backslash region. Find beginning and end, remove
1364 ;; backslashes, fill, and then reapply backslahes.
1365 (end-of-line)
1366 (let ((beginning
1367 (save-excursion
1368 (end-of-line 0)
1369 (while (= (preceding-char) ?\\)
1370 (end-of-line 0))
1371 (forward-char)
1372 (point)))
1373 (end
1374 (save-excursion
1375 (while (= (preceding-char) ?\\)
1376 (end-of-line 2))
1377 (point))))
1378 (save-restriction
1379 (narrow-to-region beginning end)
1380 (makefile-backslash-region (point-min) (point-max) t)
1381 (let ((fill-paragraph-function nil))
1382 (fill-paragraph nil))
1383 (makefile-backslash-region (point-min) (point-max) nil)
1384 (goto-char (point-max))
1385 (if (< (skip-chars-backward "\n") 0)
1386 (delete-region (point) (point-max)))))
1387 ;; Return non-nil to indicate it's been filled.
1388 t)
1389
1390 ((looking-at makefile-macroassign-regex)
1391 ;; Have a macro assign. Fill just this line, and then backslash
1392 ;; resulting region.
1393 (save-restriction
1394 (narrow-to-region (point) (line-beginning-position 2))
1395 (let ((fill-paragraph-function nil))
1396 (fill-paragraph nil))
1397 (makefile-backslash-region (point-min) (point-max) nil))
1398 ;; Return non-nil to indicate it's been filled.
1399 t)
1400
1401 (t
1402 ;; Return non-nil so we don't fill anything else.
1403 t))))
1404
1405 \f
1406
1407 ;;; ------------------------------------------------------------
1408 ;;; Browser mode.
1409 ;;; ------------------------------------------------------------
1410
1411 (defun makefile-browser-format-target-line (target selected)
1412 (format
1413 (concat (make-string makefile-browser-leftmost-column ?\ )
1414 (if selected
1415 makefile-browser-selected-mark
1416 makefile-browser-unselected-mark)
1417 "%s%s")
1418 target makefile-target-colon))
1419
1420 (defun makefile-browser-format-macro-line (macro selected)
1421 (format
1422 (concat (make-string makefile-browser-leftmost-column ?\ )
1423 (if selected
1424 makefile-browser-selected-mark
1425 makefile-browser-unselected-mark)
1426 (makefile-format-macro-ref macro))))
1427
1428 (defun makefile-browser-fill (targets macros)
1429 (let ((inhibit-read-only t))
1430 (goto-char (point-min))
1431 (erase-buffer)
1432 (mapconcat
1433 (function
1434 (lambda (item) (insert (makefile-browser-format-target-line (car item) nil) "\n")))
1435 targets
1436 "")
1437 (mapconcat
1438 (function
1439 (lambda (item) (insert (makefile-browser-format-macro-line (car item) nil) "\n")))
1440 macros
1441 "")
1442 (sort-lines nil (point-min) (point-max))
1443 (goto-char (1- (point-max)))
1444 (delete-char 1) ; remove unnecessary newline at eob
1445 (goto-char (point-min))
1446 (forward-char makefile-browser-cursor-column)))
1447
1448 ;;;
1449 ;;; Moving up and down in the browser
1450 ;;;
1451
1452 (defun makefile-browser-next-line ()
1453 "Move the browser selection cursor to the next line."
1454 (interactive)
1455 (if (not (makefile-last-line-p))
1456 (progn
1457 (forward-line 1)
1458 (forward-char makefile-browser-cursor-column))))
1459
1460 (defun makefile-browser-previous-line ()
1461 "Move the browser selection cursor to the previous line."
1462 (interactive)
1463 (if (not (makefile-first-line-p))
1464 (progn
1465 (forward-line -1)
1466 (forward-char makefile-browser-cursor-column))))
1467
1468 ;;;
1469 ;;; Quitting the browser (returns to client buffer)
1470 ;;;
1471
1472 (defun makefile-browser-quit ()
1473 "Leave the browser and return to the makefile buffer."
1474 (interactive)
1475 (let ((my-client makefile-browser-client))
1476 (setq makefile-browser-client nil) ; we quitted, so NO client!
1477 (set-buffer-modified-p nil)
1478 (quit-window t)
1479 (pop-to-buffer my-client)))
1480
1481 ;;;
1482 ;;; Toggle state of a browser item
1483 ;;;
1484
1485 (defun makefile-browser-toggle ()
1486 "Toggle the selection state of the browser item at the cursor position."
1487 (interactive)
1488 (let ((this-line (count-lines (point-min) (point))))
1489 (setq this-line (max 1 this-line))
1490 (makefile-browser-toggle-state-for-line this-line)
1491 (goto-line this-line)
1492 (let ((inhibit-read-only t))
1493 (beginning-of-line)
1494 (if (makefile-browser-on-macro-line-p)
1495 (let ((macro-name (makefile-browser-this-line-macro-name)))
1496 (delete-region (point) (progn (end-of-line) (point)))
1497 (insert
1498 (makefile-browser-format-macro-line
1499 macro-name
1500 (makefile-browser-get-state-for-line this-line))))
1501 (let ((target-name (makefile-browser-this-line-target-name)))
1502 (delete-region (point) (progn (end-of-line) (point)))
1503 (insert
1504 (makefile-browser-format-target-line
1505 target-name
1506 (makefile-browser-get-state-for-line this-line))))))
1507 (beginning-of-line)
1508 (forward-char makefile-browser-cursor-column)
1509 (if makefile-browser-auto-advance-after-selection-p
1510 (makefile-browser-next-line))))
1511
1512 ;;;
1513 ;;; Making insertions into the client buffer
1514 ;;;
1515
1516 (defun makefile-browser-insert-continuation ()
1517 "Insert a makefile continuation.
1518 In the makefile buffer, go to (end-of-line), insert a \'\\\'
1519 character, insert a new blank line, go to that line and indent by one TAB.
1520 This is most useful in the process of creating continued lines when copying
1521 large dependencies from the browser to the client buffer.
1522 \(point) advances accordingly in the client buffer."
1523 (interactive)
1524 (with-current-buffer makefile-browser-client
1525 (end-of-line)
1526 (insert "\\\n\t")))
1527
1528 (defun makefile-browser-insert-selection ()
1529 "Insert all selected targets and/or macros in the makefile buffer.
1530 Insertion takes place at point."
1531 (interactive)
1532 (save-excursion
1533 (goto-line 1)
1534 (let ((current-line 1))
1535 (while (not (eobp))
1536 (if (makefile-browser-get-state-for-line current-line)
1537 (makefile-browser-send-this-line-item))
1538 (forward-line 1)
1539 (setq current-line (1+ current-line))))))
1540
1541 (defun makefile-browser-insert-selection-and-quit ()
1542 (interactive)
1543 (makefile-browser-insert-selection)
1544 (makefile-browser-quit))
1545
1546 (defun makefile-browser-send-this-line-item ()
1547 (if (makefile-browser-on-macro-line-p)
1548 (save-excursion
1549 (let ((macro-name (makefile-browser-this-line-macro-name)))
1550 (set-buffer makefile-browser-client)
1551 (insert (makefile-format-macro-ref macro-name) " ")))
1552 (save-excursion
1553 (let ((target-name (makefile-browser-this-line-target-name)))
1554 (set-buffer makefile-browser-client)
1555 (insert target-name " ")))))
1556
1557 (defun makefile-browser-start-interaction ()
1558 (use-local-map makefile-browser-map)
1559 (setq buffer-read-only t))
1560
1561 (defun makefile-browse (targets macros)
1562 (interactive)
1563 (if (zerop (+ (length targets) (length macros)))
1564 (progn
1565 (beep)
1566 (message "No macros or targets to browse! Consider running 'makefile-pickup-everything\'"))
1567 (let ((browser-buffer (get-buffer-create makefile-browser-buffer-name)))
1568 (pop-to-buffer browser-buffer)
1569 (makefile-browser-fill targets macros)
1570 (shrink-window-if-larger-than-buffer)
1571 (set (make-local-variable 'makefile-browser-selection-vector)
1572 (make-vector (+ (length targets) (length macros)) nil))
1573 (makefile-browser-start-interaction))))
1574
1575 (defun makefile-switch-to-browser ()
1576 (interactive)
1577 (run-hooks 'makefile-browser-hook)
1578 (setq makefile-browser-client (current-buffer))
1579 (makefile-pickup-targets)
1580 (makefile-pickup-macros)
1581 (makefile-browse makefile-target-table makefile-macro-table))
1582
1583 \f
1584
1585 ;;; ------------------------------------------------------------
1586 ;;; Up-to-date overview buffer
1587 ;;; ------------------------------------------------------------
1588
1589 (defun makefile-create-up-to-date-overview ()
1590 "Create a buffer containing an overview of the state of all known targets.
1591 Known targets are targets that are explicitly defined in that makefile;
1592 in other words, all targets that appear on the left hand side of a
1593 dependency in the makefile."
1594 (interactive)
1595 (if (y-or-n-p "Are you sure that the makefile being edited is consistent? ")
1596 ;;
1597 ;; The rest of this function operates on a temporary makefile, created by
1598 ;; writing the current contents of the makefile buffer.
1599 ;;
1600 (let ((saved-target-table makefile-target-table)
1601 (this-buffer (current-buffer))
1602 (makefile-up-to-date-buffer
1603 (get-buffer-create makefile-up-to-date-buffer-name))
1604 (filename (makefile-save-temporary))
1605 ;;
1606 ;; Forget the target table because it may contain picked-up filenames
1607 ;; that are not really targets in the current makefile.
1608 ;; We don't want to query these, so get a new target-table with just the
1609 ;; targets that can be found in the makefile buffer.
1610 ;; The 'old' target table will be restored later.
1611 ;;
1612 (real-targets (progn
1613 (makefile-pickup-targets)
1614 makefile-target-table))
1615 (prereqs makefile-has-prereqs)
1616 )
1617
1618 (set-buffer makefile-up-to-date-buffer)
1619 (setq buffer-read-only nil)
1620 (erase-buffer)
1621 (makefile-query-targets filename real-targets prereqs)
1622 (if (zerop (buffer-size)) ; if it did not get us anything
1623 (progn
1624 (kill-buffer (current-buffer))
1625 (message "No overview created!")))
1626 (set-buffer this-buffer)
1627 (setq makefile-target-table saved-target-table)
1628 (if (get-buffer makefile-up-to-date-buffer-name)
1629 (progn
1630 (pop-to-buffer (get-buffer makefile-up-to-date-buffer-name))
1631 (shrink-window-if-larger-than-buffer)
1632 (sort-lines nil (point-min) (point-max))
1633 (setq buffer-read-only t))))))
1634
1635 (defun makefile-save-temporary ()
1636 "Create a temporary file from the current makefile buffer."
1637 (let ((filename (makefile-generate-temporary-filename)))
1638 (write-region (point-min) (point-max) filename nil 0)
1639 filename)) ; return the filename
1640
1641 (defun makefile-generate-temporary-filename ()
1642 "Create a filename suitable for use in `makefile-save-temporary'.
1643 Be careful to allow brain-dead file systems (DOS, SYSV ...) to cope
1644 with the generated name!"
1645 (let ((my-name (user-login-name))
1646 (my-uid (int-to-string (user-uid))))
1647 (concat "mktmp"
1648 (if (> (length my-name) 3)
1649 (substring my-name 0 3)
1650 my-name)
1651 "."
1652 (if (> (length my-uid) 3)
1653 (substring my-uid 0 3)
1654 my-uid))))
1655
1656 (defun makefile-query-targets (filename target-table prereq-list)
1657 "Fill the up-to-date overview buffer.
1658 Checks each target in TARGET-TABLE using
1659 `makefile-query-one-target-method-function'
1660 and generates the overview, one line per target name."
1661 (insert
1662 (mapconcat
1663 (function (lambda (item)
1664 (let* ((target-name (car item))
1665 (no-prereqs (not (member target-name prereq-list)))
1666 (needs-rebuild (or no-prereqs
1667 (funcall
1668 makefile-query-one-target-method-function
1669 target-name
1670 filename))))
1671 (format "\t%s%s"
1672 target-name
1673 (cond (no-prereqs " .. has no prerequisites")
1674 (needs-rebuild " .. NEEDS REBUILD")
1675 (t " .. is up to date"))))
1676 ))
1677 target-table "\n"))
1678 (goto-char (point-min))
1679 (delete-file filename)) ; remove the tmpfile
1680
1681 (defun makefile-query-by-make-minus-q (target &optional filename)
1682 (not (eq 0
1683 (call-process makefile-brave-make nil nil nil
1684 "-f" filename "-q" target))))
1685
1686 \f
1687
1688 ;;; ------------------------------------------------------------
1689 ;;; Continuation cleanup
1690 ;;; ------------------------------------------------------------
1691
1692 (defun makefile-cleanup-continuations ()
1693 (if (eq major-mode 'makefile-mode)
1694 (if (and makefile-cleanup-continuations
1695 (not buffer-read-only))
1696 (save-excursion
1697 (goto-char (point-min))
1698 (while (re-search-forward "\\\\[ \t]+$" nil t)
1699 (replace-match "\\" t t))))))
1700
1701
1702 ;;; ------------------------------------------------------------
1703 ;;; Warn of suspicious lines
1704 ;;; ------------------------------------------------------------
1705
1706 (defun makefile-warn-suspicious-lines ()
1707 ;; Returning non-nil cancels the save operation
1708 (if (eq major-mode 'makefile-mode)
1709 (save-excursion
1710 (goto-char (point-min))
1711 (if (re-search-forward "^\\(\t+$\\| +\t\\)" nil t)
1712 (not (y-or-n-p
1713 (format "Suspicious line %d. Save anyway? "
1714 (count-lines (point-min) (point)))))))))
1715
1716 (defun makefile-warn-continuations ()
1717 (if (eq major-mode 'makefile-mode)
1718 (save-excursion
1719 (goto-char (point-min))
1720 (if (re-search-forward "\\\\[ \t]+$" nil t)
1721 (not (y-or-n-p
1722 (format "Suspicious continuation in line %d. Save anyway? "
1723 (count-lines (point-min) (point)))))))))
1724 \f
1725
1726 ;;; ------------------------------------------------------------
1727 ;;; GNU make function support
1728 ;;; ------------------------------------------------------------
1729
1730 (defun makefile-insert-gmake-function ()
1731 "Insert a GNU make function call.
1732 Asks for the name of the function to use (with completion).
1733 Then prompts for all required parameters."
1734 (interactive)
1735 (let* ((gm-function-name (completing-read
1736 "Function: "
1737 makefile-gnumake-functions-alist
1738 nil t nil))
1739 (gm-function-prompts
1740 (cdr (assoc gm-function-name makefile-gnumake-functions-alist))))
1741 (if (not (zerop (length gm-function-name)))
1742 (insert (makefile-format-macro-ref
1743 (concat gm-function-name " "
1744 (makefile-prompt-for-gmake-funargs
1745 gm-function-name gm-function-prompts)))
1746 " "))))
1747
1748 (defun makefile-prompt-for-gmake-funargs (function-name prompt-list)
1749 (mapconcat
1750 (function (lambda (one-prompt)
1751 (read-string (format "[%s] %s: " function-name one-prompt)
1752 nil)))
1753 prompt-list
1754 ","))
1755
1756 \f
1757
1758 ;;; ------------------------------------------------------------
1759 ;;; Utility functions
1760 ;;; ------------------------------------------------------------
1761
1762 (defun makefile-match-function-end (end)
1763 "To be called as an anchored matcher by font-lock.
1764 The anchor must have matched the opening parens in the first group."
1765 (let ((s (match-string-no-properties 1)))
1766 (setq s (cond ((string= s "(") "\\(.*?\\)[ \t]*)")
1767 ((string= s "{") "\\(.*?\\)[ \t]*}")
1768 ((string= s "((") "\\(.*?\\)[ \t]*))")
1769 ((string= s "{{") "\\(.*?\\)[ \t]*}}")))
1770 (if s (looking-at s))))
1771
1772 (defun makefile-match-dependency (bound)
1773 "Search for `makefile-dependency-regex' up to BOUND.
1774 Checks that the colon has not already been fontified, else we
1775 matched in a rule action."
1776 (catch 'found
1777 (let ((pt (point)))
1778 (while (progn (skip-chars-forward makefile-dependency-skip bound)
1779 (< (point) (or bound (point-max))))
1780 (forward-char)
1781 (or (eq (char-after) ?=)
1782 (get-text-property (1- (point)) 'face)
1783 (if (> (line-beginning-position) (+ (point-min) 2))
1784 (eq (char-before (line-end-position 0)) ?\\))
1785 (when (save-excursion
1786 (beginning-of-line)
1787 (looking-at makefile-dependency-regex))
1788 (save-excursion
1789 (let ((deps-end (match-end 1))
1790 (match-data (match-data)))
1791 (goto-char deps-end)
1792 (skip-chars-backward " \t")
1793 (setq deps-end (point))
1794 (beginning-of-line)
1795 (skip-chars-forward " \t")
1796 ;; Alter the bounds recorded for subexp 1,
1797 ;; which is what is supposed to match the targets.
1798 (setcar (nthcdr 2 match-data) (point))
1799 (setcar (nthcdr 3 match-data) deps-end)
1800 (store-match-data match-data)))
1801 (end-of-line)
1802 (throw 'found (point)))))
1803 (goto-char pt))
1804 nil))
1805
1806 (defun makefile-match-action (bound)
1807 (catch 'found
1808 (while (re-search-forward makefile-rule-action-regex bound t)
1809 (or (eq ?\\ (char-after (- (match-beginning 0) 2)))
1810 (throw 'found t)))))
1811
1812 (defun makefile-do-macro-insertion (macro-name)
1813 "Insert a macro reference."
1814 (if (not (zerop (length macro-name)))
1815 (if (assoc macro-name makefile-runtime-macros-list)
1816 (insert "$" macro-name)
1817 (insert (makefile-format-macro-ref macro-name)))))
1818
1819 (defun makefile-remember-target (target-name &optional has-prereqs)
1820 "Remember a given target if it is not already remembered for this buffer."
1821 (if (not (zerop (length target-name)))
1822 (progn
1823 (if (not (assoc target-name makefile-target-table))
1824 (setq makefile-target-table
1825 (cons (list target-name) makefile-target-table)))
1826 (if has-prereqs
1827 (setq makefile-has-prereqs
1828 (cons target-name makefile-has-prereqs))))))
1829
1830 (defun makefile-remember-macro (macro-name)
1831 "Remember a given macro if it is not already remembered for this buffer."
1832 (if (not (zerop (length macro-name)))
1833 (if (not (assoc macro-name makefile-macro-table))
1834 (setq makefile-macro-table
1835 (cons (list macro-name) makefile-macro-table)))))
1836
1837 (defun makefile-forward-after-target-colon ()
1838 "Move point forward after inserting the terminating colon of a target.
1839 This acts according to the value of `makefile-tab-after-target-colon'."
1840 (if makefile-tab-after-target-colon
1841 (insert "\t")
1842 (insert " ")))
1843
1844 (defun makefile-browser-on-macro-line-p ()
1845 "Determine if point is on a macro line in the browser."
1846 (save-excursion
1847 (beginning-of-line)
1848 (re-search-forward "\\$[{(]" (line-end-position) t)))
1849
1850 (defun makefile-browser-this-line-target-name ()
1851 "Extract the target name from a line in the browser."
1852 (save-excursion
1853 (end-of-line)
1854 (skip-chars-backward "^ \t")
1855 (buffer-substring (point) (1- (line-end-position)))))
1856
1857 (defun makefile-browser-this-line-macro-name ()
1858 "Extract the macro name from a line in the browser."
1859 (save-excursion
1860 (beginning-of-line)
1861 (re-search-forward "\\$[{(]" (line-end-position) t)
1862 (let ((macro-start (point)))
1863 (skip-chars-forward "^})")
1864 (buffer-substring macro-start (point)))))
1865
1866 (defun makefile-format-macro-ref (macro-name)
1867 "Format a macro reference.
1868 Uses `makefile-use-curly-braces-for-macros-p'."
1869 (if (or (char-equal ?\( (string-to-char macro-name))
1870 (char-equal ?\{ (string-to-char macro-name)))
1871 (format "$%s" macro-name)
1872 (if makefile-use-curly-braces-for-macros-p
1873 (format "${%s}" macro-name)
1874 (format "$(%s)" macro-name))))
1875
1876 (defun makefile-browser-get-state-for-line (n)
1877 (aref makefile-browser-selection-vector (1- n)))
1878
1879 (defun makefile-browser-set-state-for-line (n to-state)
1880 (aset makefile-browser-selection-vector (1- n) to-state))
1881
1882 (defun makefile-browser-toggle-state-for-line (n)
1883 (makefile-browser-set-state-for-line n (not (makefile-browser-get-state-for-line n))))
1884
1885 (defun makefile-last-line-p ()
1886 (= (line-end-position) (point-max)))
1887
1888 (defun makefile-first-line-p ()
1889 (= (line-beginning-position) (point-min)))
1890
1891 \f
1892
1893 ;;; Support for other packages, like add-log.
1894
1895 (defun makefile-add-log-defun ()
1896 "Return name of target or variable assignment that point is in.
1897 If it isn't in one, return nil."
1898 (save-excursion
1899 (let (found)
1900 (beginning-of-line)
1901 ;; Scan back line by line, noticing when we come to a
1902 ;; variable or rule definition, and giving up when we see
1903 ;; a line that is not part of either of those.
1904 (while (not (or (setq found
1905 (when (or (looking-at makefile-macroassign-regex)
1906 (looking-at makefile-dependency-regex))
1907 (match-string-no-properties 1)))
1908 ;; Don't keep looking across a blank line or comment.
1909 (looking-at "$\\|#")
1910 (not (zerop (forward-line -1))))))
1911 ;; Remove leading and trailing whitespace.
1912 (when found
1913 (setq found (replace-regexp-in-string "[ \t]+\\'" "" found))
1914 (setq found (replace-regexp-in-string "\\`[ \t]+" "" found)))
1915 found)))
1916
1917 (provide 'make-mode)
1918
1919 ;; arch-tag: bd23545a-de91-44fb-b1b2-feafbb2635a0
1920 ;;; make-mode.el ends here