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