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