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