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