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