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