Spelling fixes.
[bpt/emacs.git] / lisp / progmodes / make-mode.el
1 ;;; make-mode.el --- makefile editing commands for Emacs -*- lexical-binding:t -*-
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 fl-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 ,@fl-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 favorite 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" 'completion-at-point)
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" completion-at-point
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 'completion-at-point-functions
856 #'makefile-completions-at-point nil t)
857 (add-hook 'write-file-functions
858 'makefile-warn-suspicious-lines nil t)
859 (add-hook 'write-file-functions
860 'makefile-warn-continuations nil t)
861 (add-hook 'write-file-functions
862 'makefile-cleanup-continuations nil t)
863 (make-local-variable 'makefile-target-table)
864 (make-local-variable 'makefile-macro-table)
865 (make-local-variable 'makefile-has-prereqs)
866 (make-local-variable 'makefile-need-target-pickup)
867 (make-local-variable 'makefile-need-macro-pickup)
868
869 ;; Font lock.
870 (set (make-local-variable 'font-lock-defaults)
871 ;; SYNTAX-BEGIN set to backward-paragraph to avoid slow-down
872 ;; near the end of a large buffer, due to parse-partial-sexp's
873 ;; trying to parse all the way till the beginning of buffer.
874 '(makefile-font-lock-keywords
875 nil nil
876 ((?$ . "."))
877 backward-paragraph))
878 (set (make-local-variable 'syntax-propertize-function)
879 makefile-syntax-propertize-function)
880
881 ;; Add-log.
882 (set (make-local-variable 'add-log-current-defun-function)
883 'makefile-add-log-defun)
884
885 ;; Imenu.
886 (set (make-local-variable 'imenu-generic-expression)
887 makefile-imenu-generic-expression)
888
889 ;; Dabbrev.
890 (set (make-local-variable 'dabbrev-abbrev-skip-leading-regexp) "\\$")
891
892 ;; Other abbrevs.
893 (setq local-abbrev-table makefile-mode-abbrev-table)
894
895 ;; Filling.
896 (set (make-local-variable 'fill-paragraph-function) 'makefile-fill-paragraph)
897
898 ;; Comment stuff.
899 (set (make-local-variable 'comment-start) "#")
900 (set (make-local-variable 'comment-end) "")
901 (set (make-local-variable 'comment-start-skip) "#+[ \t]*")
902
903 ;; Make sure TAB really inserts \t.
904 (set (make-local-variable 'indent-line-function) 'indent-to-left-margin)
905
906 ;; Real TABs are important in makefiles
907 (setq indent-tabs-mode t))
908
909 ;; These should do more than just differentiate font-lock.
910 ;;;###autoload
911 (define-derived-mode makefile-automake-mode makefile-mode "Makefile.am"
912 "An adapted `makefile-mode' that knows about automake."
913 (setq font-lock-defaults
914 `(makefile-automake-font-lock-keywords ,@(cdr font-lock-defaults))))
915
916 ;;;###autoload
917 (define-derived-mode makefile-gmake-mode makefile-mode "GNUmakefile"
918 "An adapted `makefile-mode' that knows about gmake."
919 (setq font-lock-defaults
920 `(makefile-gmake-font-lock-keywords ,@(cdr font-lock-defaults))))
921
922 ;;;###autoload
923 (define-derived-mode makefile-makepp-mode makefile-mode "Makeppfile"
924 "An adapted `makefile-mode' that knows about makepp."
925 (set (make-local-variable 'makefile-rule-action-regex)
926 makefile-makepp-rule-action-regex)
927 (setq font-lock-defaults
928 `(makefile-makepp-font-lock-keywords ,@(cdr font-lock-defaults))
929 imenu-generic-expression
930 `(("Functions" "^[ \t]*\\(?:make\\)?sub[ \t]+\\([A-Za-z0-9_]+\\)" 1)
931 ,@imenu-generic-expression)))
932
933 ;;;###autoload
934 (define-derived-mode makefile-bsdmake-mode makefile-mode "BSDmakefile"
935 "An adapted `makefile-mode' that knows about BSD make."
936 (set (make-local-variable 'makefile-dependency-regex)
937 makefile-bsdmake-dependency-regex)
938 (set (make-local-variable 'makefile-dependency-skip) "^:!")
939 (set (make-local-variable 'makefile-rule-action-regex)
940 makefile-bsdmake-rule-action-regex)
941 (setq font-lock-defaults
942 `(makefile-bsdmake-font-lock-keywords ,@(cdr font-lock-defaults))))
943
944 ;;;###autoload
945 (define-derived-mode makefile-imake-mode makefile-mode "Imakefile"
946 "An adapted `makefile-mode' that knows about imake."
947 :syntax-table makefile-imake-mode-syntax-table
948 (set (make-local-variable 'syntax-propertize-function) nil)
949 (setq font-lock-defaults
950 `(makefile-imake-font-lock-keywords ,@(cdr font-lock-defaults))))
951
952 \f
953
954 ;;; Motion code.
955
956 (defun makefile-next-dependency ()
957 "Move point to the beginning of the next dependency line."
958 (interactive)
959 (let ((here (point)))
960 (end-of-line)
961 (if (makefile-match-dependency nil)
962 (progn (beginning-of-line) t) ; indicate success
963 (goto-char here) nil)))
964
965 (defun makefile-previous-dependency ()
966 "Move point to the beginning of the previous dependency line."
967 (interactive)
968 (let ((pt (point)))
969 (beginning-of-line)
970 ;; makefile-match-dependency done backwards:
971 (catch 'found
972 (while (progn (skip-chars-backward makefile-dependency-skip)
973 (not (bobp)))
974 (or (prog1 (eq (char-after) ?=)
975 (backward-char))
976 (get-text-property (point) 'face)
977 (beginning-of-line)
978 (if (> (point) (+ (point-min) 2))
979 (eq (char-before (1- (point))) ?\\))
980 (if (looking-at makefile-dependency-regex)
981 (throw 'found t))))
982 (goto-char pt)
983 nil)))
984
985 \f
986
987 ;;; Electric keys. Blech.
988
989 (defun makefile-electric-dot (arg)
990 "Prompt for the name of a special target to insert.
991 Only does electric insertion at beginning of line.
992 Anywhere else just self-inserts."
993 (interactive "p")
994 (if (bolp)
995 (makefile-insert-special-target)
996 (self-insert-command arg)))
997
998 (defun makefile-insert-special-target ()
999 "Prompt for and insert a special target name.
1000 Uses `makefile-special-targets' list."
1001 (interactive)
1002 (makefile-pickup-targets)
1003 (let ((special-target
1004 (completing-read "Special target: "
1005 makefile-special-targets-list nil nil nil)))
1006 (if (zerop (length special-target))
1007 ()
1008 (insert "." special-target ":")
1009 (makefile-forward-after-target-colon))))
1010
1011 (defun makefile-electric-equal (arg)
1012 "Prompt for name of a macro to insert.
1013 Only does prompting if point is at beginning of line.
1014 Anywhere else just self-inserts."
1015 (interactive "p")
1016 (makefile-pickup-macros)
1017 (if (bolp)
1018 (call-interactively 'makefile-insert-macro)
1019 (self-insert-command arg)))
1020
1021 (defun makefile-insert-macro (macro-name)
1022 "Prepare definition of a new macro."
1023 (interactive "sMacro Name: ")
1024 (makefile-pickup-macros)
1025 (if (not (zerop (length macro-name)))
1026 (progn
1027 (beginning-of-line)
1028 (insert macro-name makefile-macro-assign)
1029 (setq makefile-need-macro-pickup t)
1030 (makefile-remember-macro macro-name))))
1031
1032 (defun makefile-insert-macro-ref (macro-name)
1033 "Complete on a list of known macros, then insert complete ref at point."
1034 (interactive
1035 (list
1036 (progn
1037 (makefile-pickup-macros)
1038 (completing-read "Refer to macro: " makefile-macro-table nil nil nil))))
1039 (makefile-do-macro-insertion macro-name))
1040
1041 (defun makefile-insert-target (target-name)
1042 "Prepare definition of a new target (dependency line)."
1043 (interactive "sTarget: ")
1044 (if (not (zerop (length target-name)))
1045 (progn
1046 (beginning-of-line)
1047 (insert target-name makefile-target-colon)
1048 (makefile-forward-after-target-colon)
1049 (end-of-line)
1050 (setq makefile-need-target-pickup t)
1051 (makefile-remember-target target-name))))
1052
1053 (defun makefile-insert-target-ref (target-name)
1054 "Complete on a list of known targets, then insert TARGET-NAME at point."
1055 (interactive
1056 (list
1057 (progn
1058 (makefile-pickup-targets)
1059 (completing-read "Refer to target: " makefile-target-table nil nil nil))))
1060 (if (not (zerop (length target-name)))
1061 (insert target-name " ")))
1062
1063 (defun makefile-electric-colon (arg)
1064 "Prompt for name of new target.
1065 Prompting only happens at beginning of line.
1066 Anywhere else just self-inserts."
1067 (interactive "p")
1068 (if (bolp)
1069 (call-interactively 'makefile-insert-target)
1070 (self-insert-command arg)))
1071
1072 \f
1073
1074 ;;; ------------------------------------------------------------
1075 ;;; Extracting targets and macros from an existing makefile
1076 ;;; ------------------------------------------------------------
1077
1078 (defun makefile-pickup-targets ()
1079 "Notice names of all target definitions in Makefile."
1080 (interactive)
1081 (when makefile-need-target-pickup
1082 (setq makefile-need-target-pickup nil
1083 makefile-target-table nil
1084 makefile-has-prereqs nil)
1085 (save-excursion
1086 (goto-char (point-min))
1087 (while (makefile-match-dependency nil)
1088 (goto-char (match-beginning 1))
1089 (while (let ((target-name
1090 (buffer-substring-no-properties (point)
1091 (progn
1092 (skip-chars-forward "^ \t:#")
1093 (point))))
1094 (has-prereqs
1095 (not (looking-at ":[ \t]*$"))))
1096 (if (makefile-remember-target target-name has-prereqs)
1097 (message "Picked up target \"%s\" from line %d"
1098 target-name (line-number-at-pos)))
1099 (skip-chars-forward " \t")
1100 (not (or (eolp) (eq (char-after) ?:)))))
1101 (forward-line)))
1102 (message "Read targets OK.")))
1103
1104 (defun makefile-pickup-macros ()
1105 "Notice names of all macro definitions in Makefile."
1106 (interactive)
1107 (when makefile-need-macro-pickup
1108 (setq makefile-need-macro-pickup nil
1109 makefile-macro-table nil)
1110 (save-excursion
1111 (goto-char (point-min))
1112 (while (re-search-forward makefile-macroassign-regex nil t)
1113 (goto-char (match-beginning 1))
1114 (let ((macro-name (buffer-substring-no-properties (point)
1115 (progn
1116 (skip-chars-forward "^ \t:#=*")
1117 (point)))))
1118 (if (makefile-remember-macro macro-name)
1119 (message "Picked up macro \"%s\" from line %d"
1120 macro-name (line-number-at-pos))))
1121 (forward-line)))
1122 (message "Read macros OK.")))
1123
1124 (defun makefile-pickup-everything (arg)
1125 "Notice names of all macros and targets in Makefile.
1126 Prefix arg means force pickups to be redone."
1127 (interactive "P")
1128 (if arg
1129 (setq makefile-need-target-pickup t
1130 makefile-need-macro-pickup t))
1131 (makefile-pickup-macros)
1132 (makefile-pickup-targets)
1133 (if makefile-pickup-everything-picks-up-filenames-p
1134 (makefile-pickup-filenames-as-targets)))
1135
1136 (defun makefile-pickup-filenames-as-targets ()
1137 "Scan the current directory for filenames to use as targets.
1138 Checks each filename against `makefile-ignored-files-in-pickup-regex'
1139 and adds all qualifying names to the list of known targets."
1140 (interactive)
1141 (mapc (lambda (name)
1142 (or (file-directory-p name)
1143 (string-match makefile-ignored-files-in-pickup-regex name)
1144 (if (makefile-remember-target name)
1145 (message "Picked up file \"%s\" as target" name))))
1146 (file-name-all-completions "" (or (file-name-directory (buffer-file-name)) ""))))
1147
1148 \f
1149
1150 ;;; Completion.
1151
1152 (defun makefile-completions-at-point ()
1153 (let* ((beg (save-excursion
1154 (skip-chars-backward "^$(){}:#= \t\n")
1155 (point)))
1156 (paren nil)
1157 (do-macros
1158 (save-excursion
1159 (goto-char beg)
1160 (let ((pc (preceding-char)))
1161 (cond
1162 ;; Preceding "$" means macros only.
1163 ((= pc ?$)
1164 t)
1165
1166 ;; Preceding "$(" or "${" means macros only.
1167 ((and (memq pc '(?\{ ?\())
1168 (progn
1169 (setq paren (if (eq pc ?\{) ?\} ?\)))
1170 (backward-char)
1171 (= (preceding-char) ?$)))
1172 t)))))
1173 (suffix (cond
1174 (do-macros (if paren (string paren)))
1175 ((save-excursion (goto-char beg) (bolp)) ":")
1176 (t " "))))
1177 (list beg (point)
1178 (append (if do-macros '() makefile-target-table)
1179 makefile-macro-table)
1180 :exit-function
1181 (if suffix
1182 (lambda (_s finished)
1183 (when (memq finished '(sole finished))
1184 (if (looking-at (regexp-quote suffix))
1185 (goto-char (match-end 0))
1186 (insert suffix))))))))
1187
1188 (define-obsolete-function-alias 'makefile-complete 'completion-at-point "24.1")
1189 \f
1190
1191 ;; Backslashification. Stolen from cc-mode.el.
1192
1193 (defun makefile-backslash-region (from to delete-flag)
1194 "Insert, align, or delete end-of-line backslashes on the lines in the region.
1195 With no argument, inserts backslashes and aligns existing backslashes.
1196 With an argument, deletes the backslashes.
1197
1198 This function does not modify the last line of the region if the region ends
1199 right at the start of the following line; it does not modify blank lines
1200 at the start of the region. So you can put the region around an entire macro
1201 definition and conveniently use this command."
1202 (interactive "r\nP")
1203 (save-excursion
1204 (goto-char from)
1205 (let ((column makefile-backslash-column)
1206 (endmark (make-marker)))
1207 (move-marker endmark to)
1208 ;; Compute the smallest column number past the ends of all the lines.
1209 (if makefile-backslash-align
1210 (progn
1211 (if (not delete-flag)
1212 (while (< (point) to)
1213 (end-of-line)
1214 (if (= (preceding-char) ?\\)
1215 (progn (forward-char -1)
1216 (skip-chars-backward " \t")))
1217 (setq column (max column (1+ (current-column))))
1218 (forward-line 1)))
1219 ;; Adjust upward to a tab column, if that doesn't push
1220 ;; past the margin.
1221 (if (> (% column tab-width) 0)
1222 (let ((adjusted (* (/ (+ column tab-width -1) tab-width)
1223 tab-width)))
1224 (if (< adjusted (window-width))
1225 (setq column adjusted))))))
1226 ;; Don't modify blank lines at start of region.
1227 (goto-char from)
1228 (while (and (< (point) endmark) (eolp))
1229 (forward-line 1))
1230 ;; Add or remove backslashes on all the lines.
1231 (while (and (< (point) endmark)
1232 ;; Don't backslashify the last line
1233 ;; if the region ends right at the start of the next line.
1234 (save-excursion
1235 (forward-line 1)
1236 (< (point) endmark)))
1237 (if (not delete-flag)
1238 (makefile-append-backslash column)
1239 (makefile-delete-backslash))
1240 (forward-line 1))
1241 (move-marker endmark nil))))
1242
1243 (defun makefile-append-backslash (column)
1244 (end-of-line)
1245 ;; Note that "\\\\" is needed to get one backslash.
1246 (if (= (preceding-char) ?\\)
1247 (progn (forward-char -1)
1248 (delete-horizontal-space)
1249 (indent-to column (if makefile-backslash-align nil 1)))
1250 (indent-to column (if makefile-backslash-align nil 1))
1251 (insert "\\")))
1252
1253 (defun makefile-delete-backslash ()
1254 (end-of-line)
1255 (or (bolp)
1256 (progn
1257 (forward-char -1)
1258 (if (looking-at "\\\\")
1259 (delete-region (1+ (point))
1260 (progn (skip-chars-backward " \t") (point)))))))
1261
1262 \f
1263
1264 ;; Filling
1265
1266 (defun makefile-fill-paragraph (_arg)
1267 ;; Fill comments, backslashed lines, and variable definitions
1268 ;; specially.
1269 (save-excursion
1270 (beginning-of-line)
1271 (cond
1272 ((looking-at "^[ \t]*#+\\s-*")
1273 ;; Found a comment. Return nil to let normal filling take place.
1274 nil)
1275
1276 ;; Must look for backslashed-region before looking for variable
1277 ;; assignment.
1278 ((or (eq (char-before (line-end-position 1)) ?\\)
1279 (eq (char-before (line-end-position 0)) ?\\))
1280 ;; A backslash region. Find beginning and end, remove
1281 ;; backslashes, fill, and then reapply backslahes.
1282 (end-of-line)
1283 (let ((beginning
1284 (save-excursion
1285 (end-of-line 0)
1286 (while (= (preceding-char) ?\\)
1287 (end-of-line 0))
1288 (forward-char)
1289 (point)))
1290 (end
1291 (save-excursion
1292 (while (= (preceding-char) ?\\)
1293 (end-of-line 2))
1294 (point))))
1295 (save-restriction
1296 (narrow-to-region beginning end)
1297 (makefile-backslash-region (point-min) (point-max) t)
1298 (let ((fill-paragraph-function nil)
1299 ;; Adjust fill-column to allow space for the backslash.
1300 (fill-column (- fill-column 1)))
1301 (fill-paragraph nil))
1302 (makefile-backslash-region (point-min) (point-max) nil)
1303 (goto-char (point-max))
1304 (if (< (skip-chars-backward "\n") 0)
1305 (delete-region (point) (point-max)))))
1306 ;; Return non-nil to indicate it's been filled.
1307 t)
1308
1309 ((looking-at makefile-macroassign-regex)
1310 ;; Have a macro assign. Fill just this line, and then backslash
1311 ;; resulting region.
1312 (save-restriction
1313 (narrow-to-region (point) (line-beginning-position 2))
1314 (let ((fill-paragraph-function nil)
1315 ;; Adjust fill-column to allow space for the backslash.
1316 (fill-column (- fill-column 1)))
1317 (fill-paragraph nil))
1318 (makefile-backslash-region (point-min) (point-max) nil))
1319 ;; Return non-nil to indicate it's been filled.
1320 t)
1321
1322 (t
1323 ;; Return non-nil so we don't fill anything else.
1324 t))))
1325
1326 \f
1327
1328 ;;; ------------------------------------------------------------
1329 ;;; Browser mode.
1330 ;;; ------------------------------------------------------------
1331
1332 (defun makefile-browser-format-target-line (target selected)
1333 (format
1334 (concat (make-string makefile-browser-leftmost-column ?\ )
1335 (if selected
1336 makefile-browser-selected-mark
1337 makefile-browser-unselected-mark)
1338 "%s%s")
1339 target makefile-target-colon))
1340
1341 (defun makefile-browser-format-macro-line (macro selected)
1342 (format
1343 (concat (make-string makefile-browser-leftmost-column ?\ )
1344 (if selected
1345 makefile-browser-selected-mark
1346 makefile-browser-unselected-mark)
1347 (makefile-format-macro-ref macro))))
1348
1349 (defun makefile-browser-fill (targets macros)
1350 (let ((inhibit-read-only t))
1351 (goto-char (point-min))
1352 (erase-buffer)
1353 (mapconcat
1354 (function
1355 (lambda (item) (insert (makefile-browser-format-target-line (car item) nil) "\n")))
1356 targets
1357 "")
1358 (mapconcat
1359 (function
1360 (lambda (item) (insert (makefile-browser-format-macro-line (car item) nil) "\n")))
1361 macros
1362 "")
1363 (sort-lines nil (point-min) (point-max))
1364 (goto-char (1- (point-max)))
1365 (delete-char 1) ; remove unnecessary newline at eob
1366 (goto-char (point-min))
1367 (forward-char makefile-browser-cursor-column)))
1368
1369 ;;;
1370 ;;; Moving up and down in the browser
1371 ;;;
1372
1373 (defun makefile-browser-next-line ()
1374 "Move the browser selection cursor to the next line."
1375 (interactive)
1376 (if (not (makefile-last-line-p))
1377 (progn
1378 (forward-line 1)
1379 (forward-char makefile-browser-cursor-column))))
1380
1381 (defun makefile-browser-previous-line ()
1382 "Move the browser selection cursor to the previous line."
1383 (interactive)
1384 (if (not (makefile-first-line-p))
1385 (progn
1386 (forward-line -1)
1387 (forward-char makefile-browser-cursor-column))))
1388
1389 ;;;
1390 ;;; Quitting the browser (returns to client buffer)
1391 ;;;
1392
1393 (defun makefile-browser-quit ()
1394 "Leave the browser and return to the makefile buffer."
1395 (interactive)
1396 (let ((my-client makefile-browser-client))
1397 (setq makefile-browser-client nil) ; we quitted, so NO client!
1398 (set-buffer-modified-p nil)
1399 (quit-window t)
1400 (pop-to-buffer my-client)))
1401
1402 ;;;
1403 ;;; Toggle state of a browser item
1404 ;;;
1405
1406 (defun makefile-browser-toggle ()
1407 "Toggle the selection state of the browser item at the cursor position."
1408 (interactive)
1409 (let ((this-line (count-lines (point-min) (point))))
1410 (setq this-line (max 1 this-line))
1411 (makefile-browser-toggle-state-for-line this-line)
1412 (goto-char (point-min))
1413 (forward-line (1- this-line))
1414 (let ((inhibit-read-only t))
1415 (beginning-of-line) ; redundant?
1416 (if (makefile-browser-on-macro-line-p)
1417 (let ((macro-name (makefile-browser-this-line-macro-name)))
1418 (delete-region (point) (progn (end-of-line) (point)))
1419 (insert
1420 (makefile-browser-format-macro-line
1421 macro-name
1422 (makefile-browser-get-state-for-line this-line))))
1423 (let ((target-name (makefile-browser-this-line-target-name)))
1424 (delete-region (point) (progn (end-of-line) (point)))
1425 (insert
1426 (makefile-browser-format-target-line
1427 target-name
1428 (makefile-browser-get-state-for-line this-line))))))
1429 (beginning-of-line)
1430 (forward-char makefile-browser-cursor-column)
1431 (if makefile-browser-auto-advance-after-selection-p
1432 (makefile-browser-next-line))))
1433
1434 ;;;
1435 ;;; Making insertions into the client buffer
1436 ;;;
1437
1438 (defun makefile-browser-insert-continuation ()
1439 "Insert a makefile continuation.
1440 In the makefile buffer, go to (end-of-line), insert a \'\\\'
1441 character, insert a new blank line, go to that line and indent by one TAB.
1442 This is most useful in the process of creating continued lines when copying
1443 large dependencies from the browser to the client buffer.
1444 \(point) advances accordingly in the client buffer."
1445 (interactive)
1446 (with-current-buffer makefile-browser-client
1447 (end-of-line)
1448 (insert "\\\n\t")))
1449
1450 (defun makefile-browser-insert-selection ()
1451 "Insert all selected targets and/or macros in the makefile buffer.
1452 Insertion takes place at point."
1453 (interactive)
1454 (save-excursion
1455 (goto-char (point-min))
1456 (let ((current-line 1))
1457 (while (not (eobp))
1458 (if (makefile-browser-get-state-for-line current-line)
1459 (makefile-browser-send-this-line-item))
1460 (forward-line 1)
1461 (setq current-line (1+ current-line))))))
1462
1463 (defun makefile-browser-insert-selection-and-quit ()
1464 (interactive)
1465 (makefile-browser-insert-selection)
1466 (makefile-browser-quit))
1467
1468 (defun makefile-browser-send-this-line-item ()
1469 (if (makefile-browser-on-macro-line-p)
1470 (save-excursion
1471 (let ((macro-name (makefile-browser-this-line-macro-name)))
1472 (set-buffer makefile-browser-client)
1473 (insert (makefile-format-macro-ref macro-name) " ")))
1474 (save-excursion
1475 (let ((target-name (makefile-browser-this-line-target-name)))
1476 (set-buffer makefile-browser-client)
1477 (insert target-name " ")))))
1478
1479 (defun makefile-browser-start-interaction ()
1480 (use-local-map makefile-browser-map)
1481 (setq buffer-read-only t))
1482
1483 (defun makefile-browse (targets macros)
1484 (interactive)
1485 (if (zerop (+ (length targets) (length macros)))
1486 (progn
1487 (beep)
1488 (message "No macros or targets to browse! Consider running 'makefile-pickup-everything\'"))
1489 (let ((browser-buffer (get-buffer-create makefile-browser-buffer-name)))
1490 (pop-to-buffer browser-buffer)
1491 (makefile-browser-fill targets macros)
1492 (shrink-window-if-larger-than-buffer)
1493 (set (make-local-variable 'makefile-browser-selection-vector)
1494 (make-vector (+ (length targets) (length macros)) nil))
1495 (makefile-browser-start-interaction))))
1496
1497 (defun makefile-switch-to-browser ()
1498 (interactive)
1499 (run-hooks 'makefile-browser-hook)
1500 (setq makefile-browser-client (current-buffer))
1501 (makefile-pickup-targets)
1502 (makefile-pickup-macros)
1503 (makefile-browse makefile-target-table makefile-macro-table))
1504
1505 \f
1506
1507 ;;; ------------------------------------------------------------
1508 ;;; Up-to-date overview buffer
1509 ;;; ------------------------------------------------------------
1510
1511 (defun makefile-create-up-to-date-overview ()
1512 "Create a buffer containing an overview of the state of all known targets.
1513 Known targets are targets that are explicitly defined in that makefile;
1514 in other words, all targets that appear on the left hand side of a
1515 dependency in the makefile."
1516 (interactive)
1517 (if (y-or-n-p "Are you sure that the makefile being edited is consistent? ")
1518 ;;
1519 ;; The rest of this function operates on a temporary makefile, created by
1520 ;; writing the current contents of the makefile buffer.
1521 ;;
1522 (let ((saved-target-table makefile-target-table)
1523 (this-buffer (current-buffer))
1524 (makefile-up-to-date-buffer
1525 (get-buffer-create makefile-up-to-date-buffer-name))
1526 (filename (makefile-save-temporary))
1527 ;;
1528 ;; Forget the target table because it may contain picked-up filenames
1529 ;; that are not really targets in the current makefile.
1530 ;; We don't want to query these, so get a new target-table with just the
1531 ;; targets that can be found in the makefile buffer.
1532 ;; The 'old' target table will be restored later.
1533 ;;
1534 (real-targets (progn
1535 (makefile-pickup-targets)
1536 makefile-target-table))
1537 (prereqs makefile-has-prereqs)
1538 )
1539
1540 (set-buffer makefile-up-to-date-buffer)
1541 (setq buffer-read-only nil)
1542 (erase-buffer)
1543 (makefile-query-targets filename real-targets prereqs)
1544 (if (zerop (buffer-size)) ; if it did not get us anything
1545 (progn
1546 (kill-buffer (current-buffer))
1547 (message "No overview created!")))
1548 (set-buffer this-buffer)
1549 (setq makefile-target-table saved-target-table)
1550 (if (get-buffer makefile-up-to-date-buffer-name)
1551 (progn
1552 (pop-to-buffer (get-buffer makefile-up-to-date-buffer-name))
1553 (shrink-window-if-larger-than-buffer)
1554 (sort-lines nil (point-min) (point-max))
1555 (setq buffer-read-only t))))))
1556
1557 (defun makefile-save-temporary ()
1558 "Create a temporary file from the current makefile buffer."
1559 (let ((filename (makefile-generate-temporary-filename)))
1560 (write-region (point-min) (point-max) filename nil 0)
1561 filename)) ; return the filename
1562
1563 (defun makefile-generate-temporary-filename ()
1564 "Create a filename suitable for use in `makefile-save-temporary'.
1565 Be careful to allow brain-dead file systems (DOS, SYSV ...) to cope
1566 with the generated name!"
1567 (let ((my-name (user-login-name))
1568 (my-uid (int-to-string (user-uid))))
1569 (concat "mktmp"
1570 (if (> (length my-name) 3)
1571 (substring my-name 0 3)
1572 my-name)
1573 "."
1574 (if (> (length my-uid) 3)
1575 (substring my-uid 0 3)
1576 my-uid))))
1577
1578 (defun makefile-query-targets (filename target-table prereq-list)
1579 "Fill the up-to-date overview buffer.
1580 Checks each target in TARGET-TABLE using
1581 `makefile-query-one-target-method-function'
1582 and generates the overview, one line per target name."
1583 (insert
1584 (mapconcat
1585 (function (lambda (item)
1586 (let* ((target-name (car item))
1587 (no-prereqs (not (member target-name prereq-list)))
1588 (needs-rebuild (or no-prereqs
1589 (funcall
1590 makefile-query-one-target-method-function
1591 target-name
1592 filename))))
1593 (format "\t%s%s"
1594 target-name
1595 (cond (no-prereqs " .. has no prerequisites")
1596 (needs-rebuild " .. NEEDS REBUILD")
1597 (t " .. is up to date"))))
1598 ))
1599 target-table "\n"))
1600 (goto-char (point-min))
1601 (delete-file filename)) ; remove the tmpfile
1602
1603 (defun makefile-query-by-make-minus-q (target &optional filename)
1604 (not (eq 0
1605 (call-process makefile-brave-make nil nil nil
1606 "-f" filename "-q" target))))
1607
1608 \f
1609
1610 ;;; ------------------------------------------------------------
1611 ;;; Continuation cleanup
1612 ;;; ------------------------------------------------------------
1613
1614 (defun makefile-cleanup-continuations ()
1615 (if (derived-mode-p 'makefile-mode)
1616 (if (and makefile-cleanup-continuations
1617 (not buffer-read-only))
1618 (save-excursion
1619 (goto-char (point-min))
1620 (while (re-search-forward "\\\\[ \t]+$" nil t)
1621 (replace-match "\\" t t))))))
1622
1623
1624 ;;; ------------------------------------------------------------
1625 ;;; Warn of suspicious lines
1626 ;;; ------------------------------------------------------------
1627
1628 (defun makefile-warn-suspicious-lines ()
1629 ;; Returning non-nil cancels the save operation
1630 (if (derived-mode-p 'makefile-mode)
1631 (save-excursion
1632 (goto-char (point-min))
1633 (if (re-search-forward "^\\(\t+$\\| +\t\\)" nil t)
1634 (not (y-or-n-p
1635 (format "Suspicious line %d. Save anyway? "
1636 (count-lines (point-min) (point)))))))))
1637
1638 (defun makefile-warn-continuations ()
1639 (if (derived-mode-p 'makefile-mode)
1640 (save-excursion
1641 (goto-char (point-min))
1642 (if (re-search-forward "\\\\[ \t]+$" nil t)
1643 (not (y-or-n-p
1644 (format "Suspicious continuation in line %d. Save anyway? "
1645 (count-lines (point-min) (point)))))))))
1646 \f
1647
1648 ;;; ------------------------------------------------------------
1649 ;;; GNU make function support
1650 ;;; ------------------------------------------------------------
1651
1652 (defun makefile-insert-gmake-function ()
1653 "Insert a GNU make function call.
1654 Asks for the name of the function to use (with completion).
1655 Then prompts for all required parameters."
1656 (interactive)
1657 (let* ((gm-function-name (completing-read
1658 "Function: "
1659 makefile-gnumake-functions-alist
1660 nil t nil))
1661 (gm-function-prompts
1662 (cdr (assoc gm-function-name makefile-gnumake-functions-alist))))
1663 (if (not (zerop (length gm-function-name)))
1664 (insert (makefile-format-macro-ref
1665 (concat gm-function-name " "
1666 (makefile-prompt-for-gmake-funargs
1667 gm-function-name gm-function-prompts)))
1668 " "))))
1669
1670 (defun makefile-prompt-for-gmake-funargs (function-name prompt-list)
1671 (mapconcat
1672 (function (lambda (one-prompt)
1673 (read-string (format "[%s] %s: " function-name one-prompt)
1674 nil)))
1675 prompt-list
1676 ","))
1677
1678 \f
1679
1680 ;;; ------------------------------------------------------------
1681 ;;; Utility functions
1682 ;;; ------------------------------------------------------------
1683
1684 (defun makefile-match-function-end (_end)
1685 "To be called as an anchored matcher by font-lock.
1686 The anchor must have matched the opening parens in the first group."
1687 (let ((s (match-string-no-properties 1)))
1688 ;; FIXME forward-sexp or somesuch would be better?
1689 (if (setq s (cond ((string= s "(") ")")
1690 ((string= s "{") "}")
1691 ((string= s "((") "))")
1692 ((string= s "{{") "}}")))
1693 (re-search-forward (concat "\\(.*\\)[ \t]*" s) (line-end-position) t))))
1694
1695 (defun makefile-match-dependency (bound)
1696 "Search for `makefile-dependency-regex' up to BOUND.
1697 Checks that the colon has not already been fontified, else we
1698 matched in a rule action."
1699 (catch 'found
1700 (let ((pt (point)))
1701 (while (progn (skip-chars-forward makefile-dependency-skip bound)
1702 (< (point) (or bound (point-max))))
1703 (forward-char)
1704 (or (eq (char-after) ?=)
1705 (get-text-property (1- (point)) 'face)
1706 (if (> (line-beginning-position) (+ (point-min) 2))
1707 (eq (char-before (line-end-position 0)) ?\\))
1708 (when (save-excursion
1709 (beginning-of-line)
1710 (looking-at makefile-dependency-regex))
1711 (save-excursion
1712 (let ((deps-end (match-end 1))
1713 (match-data (match-data)))
1714 (goto-char deps-end)
1715 (skip-chars-backward " \t")
1716 (setq deps-end (point))
1717 (beginning-of-line)
1718 (skip-chars-forward " \t")
1719 ;; Alter the bounds recorded for subexp 1,
1720 ;; which is what is supposed to match the targets.
1721 (setcar (nthcdr 2 match-data) (point))
1722 (setcar (nthcdr 3 match-data) deps-end)
1723 (store-match-data match-data)))
1724 (end-of-line)
1725 (throw 'found (point)))))
1726 (goto-char pt))
1727 nil))
1728
1729 (defun makefile-match-action (bound)
1730 (catch 'found
1731 (while (re-search-forward makefile-rule-action-regex bound t)
1732 (or (eq ?\\ (char-after (- (match-beginning 0) 2)))
1733 (throw 'found t)))))
1734
1735 (defun makefile-do-macro-insertion (macro-name)
1736 "Insert a macro reference."
1737 (if (not (zerop (length macro-name)))
1738 (if (assoc macro-name makefile-runtime-macros-list)
1739 (insert "$" macro-name)
1740 (insert (makefile-format-macro-ref macro-name)))))
1741
1742 (defun makefile-remember-target (target-name &optional has-prereqs)
1743 "Remember a given target if it is not already remembered for this buffer."
1744 (if (not (zerop (length target-name)))
1745 (progn
1746 (if (not (assoc target-name makefile-target-table))
1747 (setq makefile-target-table
1748 (cons (list target-name) makefile-target-table)))
1749 (if has-prereqs
1750 (setq makefile-has-prereqs
1751 (cons target-name makefile-has-prereqs))))))
1752
1753 (defun makefile-remember-macro (macro-name)
1754 "Remember a given macro if it is not already remembered for this buffer."
1755 (if (not (zerop (length macro-name)))
1756 (if (not (assoc macro-name makefile-macro-table))
1757 (setq makefile-macro-table
1758 (cons (list macro-name) makefile-macro-table)))))
1759
1760 (defun makefile-forward-after-target-colon ()
1761 "Move point forward after inserting the terminating colon of a target.
1762 This acts according to the value of `makefile-tab-after-target-colon'."
1763 (if makefile-tab-after-target-colon
1764 (insert "\t")
1765 (insert " ")))
1766
1767 (defun makefile-browser-on-macro-line-p ()
1768 "Determine if point is on a macro line in the browser."
1769 (save-excursion
1770 (beginning-of-line)
1771 (re-search-forward "\\$[{(]" (line-end-position) t)))
1772
1773 (defun makefile-browser-this-line-target-name ()
1774 "Extract the target name from a line in the browser."
1775 (save-excursion
1776 (end-of-line)
1777 (skip-chars-backward "^ \t")
1778 (buffer-substring (point) (1- (line-end-position)))))
1779
1780 (defun makefile-browser-this-line-macro-name ()
1781 "Extract the macro name from a line in the browser."
1782 (save-excursion
1783 (beginning-of-line)
1784 (re-search-forward "\\$[{(]" (line-end-position) t)
1785 (let ((macro-start (point)))
1786 (skip-chars-forward "^})")
1787 (buffer-substring macro-start (point)))))
1788
1789 (defun makefile-format-macro-ref (macro-name)
1790 "Format a macro reference.
1791 Uses `makefile-use-curly-braces-for-macros-p'."
1792 (if (or (char-equal ?\( (string-to-char macro-name))
1793 (char-equal ?\{ (string-to-char macro-name)))
1794 (format "$%s" macro-name)
1795 (if makefile-use-curly-braces-for-macros-p
1796 (format "${%s}" macro-name)
1797 (format "$(%s)" macro-name))))
1798
1799 (defun makefile-browser-get-state-for-line (n)
1800 (aref makefile-browser-selection-vector (1- n)))
1801
1802 (defun makefile-browser-set-state-for-line (n to-state)
1803 (aset makefile-browser-selection-vector (1- n) to-state))
1804
1805 (defun makefile-browser-toggle-state-for-line (n)
1806 (makefile-browser-set-state-for-line n (not (makefile-browser-get-state-for-line n))))
1807
1808 (defun makefile-last-line-p ()
1809 (= (line-end-position) (point-max)))
1810
1811 (defun makefile-first-line-p ()
1812 (= (line-beginning-position) (point-min)))
1813
1814 \f
1815
1816 ;;; Support for other packages, like add-log.
1817
1818 (defun makefile-add-log-defun ()
1819 "Return name of target or variable assignment that point is in.
1820 If it isn't in one, return nil."
1821 (save-excursion
1822 (let (found)
1823 (beginning-of-line)
1824 ;; Scan back line by line, noticing when we come to a
1825 ;; variable or rule definition, and giving up when we see
1826 ;; a line that is not part of either of those.
1827 (while (not (or (setq found
1828 (when (or (looking-at makefile-macroassign-regex)
1829 (looking-at makefile-dependency-regex))
1830 (match-string-no-properties 1)))
1831 ;; Don't keep looking across a blank line or comment.
1832 (looking-at "$\\|#")
1833 (not (zerop (forward-line -1))))))
1834 ;; Remove leading and trailing whitespace.
1835 (when found
1836 (setq found (replace-regexp-in-string "[ \t]+\\'" "" found))
1837 (setq found (replace-regexp-in-string "\\`[ \t]+" "" found)))
1838 found)))
1839
1840 (provide 'make-mode)
1841
1842 ;;; make-mode.el ends here