python.el now bytecompiles without warnings
[bpt/emacs.git] / lisp / progmodes / make-mode.el
CommitLineData
bcd70d97 1;;; make-mode.el --- makefile editing commands for Emacs -*- lexical-binding:t -*-
72c0ae01 2
acaf905b 3;; Copyright (C) 1992, 1994, 1999-2012 Free Software Foundation, Inc.
58142744 4
72c0ae01 5;; Author: Thomas Neumann <tom@smart.bo.open.de>
9e9bf716 6;; Eric S. Raymond <esr@snark.thyrsus.com>
4228277d 7;; Maintainer: FSF
72c0ae01 8;; Adapted-By: ESR
fd7fa35a 9;; Keywords: unix, tools
72c0ae01 10
72c0ae01
ER
11;; This file is part of GNU Emacs.
12
b1fc2b50 13;; GNU Emacs is free software: you can redistribute it and/or modify
72c0ae01 14;; it under the terms of the GNU General Public License as published by
b1fc2b50
GM
15;; the Free Software Foundation, either version 3 of the License, or
16;; (at your option) any later version.
72c0ae01
ER
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
b1fc2b50 24;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
72c0ae01 25
e41b2db1
ER
26;;; Commentary:
27
9e9bf716
ER
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;;
6783b1ce 38;; The command C-c C-f adds certain filenames in the current directory
9e9bf716
ER
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;;
6783b1ce 47;; The command C-c C-b pops up a browser window listing all target and
10f518f7 48;; macro names. You can mark or unmark items with C-c SPC, and insert
9e9bf716
ER
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.
e41b2db1 55
6783b1ce
RS
56;;
57;; To Do:
58;;
e008a4e5 59;; * Add missing doc strings, improve terse doc strings.
6783b1ce
RS
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).
308a2860 69;; * Make browser into a major mode.
6783b1ce
RS
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
72c0ae01
ER
83;;; Code:
84
6783b1ce
RS
85;; Sadly we need this for a macro.
86(eval-when-compile
9ffb4859
GM
87 (require 'imenu)
88 (require 'dabbrev)
89 (require 'add-log))
6783b1ce 90
72c0ae01 91;;; ------------------------------------------------------------
eb8c3be9 92;;; Configurable stuff
72c0ae01
ER
93;;; ------------------------------------------------------------
94
c098c7f1
RS
95(defgroup makefile nil
96 "Makefile editing commands for Emacs."
8ec3bce0 97 :link '(custom-group-link :tag "Font Lock Faces group" font-lock-faces)
c098c7f1
RS
98 :group 'tools
99 :prefix "makefile-")
72c0ae01 100
602dc0da 101(defface makefile-space
9c859203
DP
102 '((((class color)) (:background "hotpink"))
103 (t (:reverse-video t)))
dc3fc797 104 "Face to use for highlighting leading spaces in Font-Lock mode."
a27b41ab 105 :group 'makefile)
c4f6e489 106(define-obsolete-face-alias 'makefile-space-face 'makefile-space "22.1")
dc3fc797 107
602dc0da 108(defface makefile-targets
9c859203 109 ;; This needs to go along both with foreground and background colors (i.e. shell)
602dc0da 110 '((t (:inherit font-lock-function-name-face)))
9c859203 111 "Face to use for additionally highlighting rule targets in Font-Lock mode."
30edba6e
DP
112 :group 'makefile
113 :version "22.1")
9c859203 114
602dc0da
DP
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")))
9c859203 119 "Face to use for additionally highlighting Shell commands in Font-Lock mode."
30edba6e
DP
120 :group 'makefile
121 :version "22.1")
9c859203 122
602dc0da 123(defface makefile-makepp-perl
9c859203
DP
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."
30edba6e
DP
128 :group 'makefile
129 :version "22.1")
9c859203 130
c098c7f1 131(defcustom makefile-browser-buffer-name "*Macros and Targets*"
fb7ada5f 132 "Name of the macro- and target browser buffer."
c098c7f1
RS
133 :type 'string
134 :group 'makefile)
135
136(defcustom makefile-target-colon ":"
fb7ada5f 137 "String to append to all target names inserted by `makefile-insert-target'.
c098c7f1
RS
138\":\" or \"::\" are common values."
139 :type 'string
140 :group 'makefile)
72c0ae01 141
c098c7f1 142(defcustom makefile-macro-assign " = "
fb7ada5f 143 "String to append to all macro names inserted by `makefile-insert-macro'.
72c0ae01 144The normal value should be \" = \", since this is what
a564ebfc 145standard make expects. However, newer makes such as dmake
72c0ae01 146allow a larger variety of different macro assignments, so you
c098c7f1
RS
147might prefer to use \" += \" or \" := \" ."
148 :type 'string
149 :group 'makefile)
72c0ae01 150
c098c7f1 151(defcustom makefile-electric-keys nil
fb7ada5f 152 "If non-nil, Makefile mode should install electric keybindings.
c098c7f1
RS
153Default is nil."
154 :type 'boolean
155 :group 'makefile)
6783b1ce 156
c098c7f1 157(defcustom makefile-use-curly-braces-for-macros-p nil
fb7ada5f 158 "Controls the style of generated macro references.
c098c7f1
RS
159Non-nil means macro references should use curly braces, like `${this}'.
160nil means use parentheses, like `$(this)'."
161 :type 'boolean
162 :group 'makefile)
72c0ae01 163
c098c7f1 164(defcustom makefile-tab-after-target-colon t
fb7ada5f 165 "If non-nil, insert a TAB after a target colon.
308a2860 166Otherwise, a space is inserted.
c098c7f1
RS
167The default is t."
168 :type 'boolean
169 :group 'makefile)
170
171(defcustom makefile-browser-leftmost-column 10
fb7ada5f 172 "Number of blanks to the left of the browser selection mark."
c098c7f1
RS
173 :type 'integer
174 :group 'makefile)
175
176(defcustom makefile-browser-cursor-column 10
fb7ada5f 177 "Column the cursor goes to when it moves up or down in the Makefile browser."
c098c7f1
RS
178 :type 'integer
179 :group 'makefile)
180
181(defcustom makefile-backslash-column 48
fb7ada5f 182 "Column in which `makefile-backslash-region' inserts backslashes."
c098c7f1
RS
183 :type 'integer
184 :group 'makefile)
185
186(defcustom makefile-backslash-align t
fb7ada5f 187 "If non-nil, `makefile-backslash-region' will align backslashes."
c098c7f1
RS
188 :type 'boolean
189 :group 'makefile)
190
191(defcustom makefile-browser-selected-mark "+ "
fb7ada5f 192 "String used to mark selected entries in the Makefile browser."
c098c7f1
RS
193 :type 'string
194 :group 'makefile)
195
196(defcustom makefile-browser-unselected-mark " "
fb7ada5f 197 "String used to mark unselected entries in the Makefile browser."
c098c7f1
RS
198 :type 'string
199 :group 'makefile)
200
201(defcustom makefile-browser-auto-advance-after-selection-p t
fb7ada5f 202 "If non-nil, cursor will move after item is selected in Makefile browser."
c098c7f1
RS
203 :type 'boolean
204 :group 'makefile)
205
206(defcustom makefile-pickup-everything-picks-up-filenames-p nil
fb7ada5f 207 "If non-nil, `makefile-pickup-everything' picks up filenames as targets.
b20a44dc 208This means it calls `makefile-pickup-filenames-as-targets'.
c098c7f1
RS
209Otherwise filenames are omitted."
210 :type 'boolean
211 :group 'makefile)
72c0ae01 212
a11e2800 213(defcustom makefile-cleanup-continuations nil
fb7ada5f 214 "If non-nil, automatically clean up continuation lines when saving.
308a2860
RS
215A line is cleaned up by removing all whitespace following a trailing
216backslash. This is done silently.
a564ebfc 217IMPORTANT: Please note that enabling this option causes Makefile mode
c098c7f1
RS
218to MODIFY A FILE WITHOUT YOUR CONFIRMATION when \"it seems necessary\"."
219 :type 'boolean
220 :group 'makefile)
72c0ae01 221
66c8215f 222(defcustom makefile-mode-hook nil
fb7ada5f 223 "Normal hook run by `makefile-mode'."
66c8215f
DL
224 :type 'hook
225 :group 'makefile)
226
308a2860 227(defvar makefile-browser-hook '())
72c0ae01
ER
228
229;;
230;; Special targets for DMake, Sun's make ...
a1506d29 231;;
c098c7f1 232(defcustom makefile-special-targets-list
ea52206b
SM
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.
308a2860 242You will be offered to complete on one of those in the minibuffer whenever
9ffb4859 243you enter a \".\" at the beginning of a line in `makefile-mode'."
c098c7f1
RS
244 :type '(repeat (list string))
245 :group 'makefile)
9e2d84cc 246(put 'makefile-special-targets-list 'risky-local-variable t)
72c0ae01 247
c098c7f1 248(defcustom makefile-runtime-macros-list
1f35e359 249 '(("@") ("&") (">") ("<") ("*") ("^") ("+") ("?") ("%") ("$"))
fb7ada5f 250 "List of macros that are resolved by make at runtime.
a564ebfc
DL
251If you insert a macro reference using `makefile-insert-macro-ref', the name
252of the macro is checked against this list. If it can be found its name will
c098c7f1
RS
253not be enclosed in { } or ( )."
254 :type '(repeat (list string))
255 :group 'makefile)
72c0ae01 256
6783b1ce 257;; Note that the first big subexpression is used by font lock. Note
3b7f63b1
RS
258;; that if you change this regexp you might have to fix the imenu
259;; index in makefile-imenu-generic-expression.
92984345
DP
260(defvar makefile-dependency-regex
261 ;; Allow for two nested levels $(v1:$(v2:$(v3:a=b)=c)=d)
9391790a 262 "^\\(\\(?:\\$\\(?:[({]\\(?:\\$\\(?:[({]\\(?:\\$\\(?:[^({]\\|.[^\n$#})]+?[})]\\)\\|[^\n$#)}]\\)+?[})]\\|[^({]\\)\\|[^\n$#)}]\\)+?[})]\\|[^({]\\)\\|[^\n$#:=]\\)+?\\)\\(:\\)\\(?:[ \t]*$\\|[^=\n]\\(?:[^#\n]*?;[ \t]*\\(.+\\)\\)?\\)"
72c0ae01
ER
263 "Regex used to find dependency lines in a makefile.")
264
30edba6e
DP
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
0b11ce59
DP
270(defvar makefile-dependency-skip "^:"
271 "Characters to skip to find a line that might be a dependency.")
272
92984345 273(defvar makefile-rule-action-regex
efee6a6d 274 "^\t[ \t]*\\(?:\\([-@]+\\)[ \t]*\\)\\(.*\\(?:\\\\\n.*\\)*\\)"
9c859203
DP
275 "Regex used to highlight rule action lines in font lock mode.")
276
30edba6e
DP
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
9586c41a 283 (replace-regexp-in-string "-@" "-+@" makefile-rule-action-regex)
30edba6e
DP
284 "Regex used to highlight BSD rule action lines in font lock mode.")
285
9c859203
DP
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.
72c0ae01 289(defconst makefile-macroassign-regex
c5301b5c
SM
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\\)*.*\\)\\)"
88983f8c
DP
293 ;; What about the define statement? What about differentiating this for makepp?
294 "\\(?:^\\|^export\\|^override\\|:\\|: *override\\) *\\([^ \n\t][^:#= \t\n]*\\)[ \t]*\\(?:!=\\|[*:+]?[:?]?=\\)"
72c0ae01
ER
295 "Regex used to find macro assignment lines in a makefile.")
296
9c859203 297(defconst makefile-var-use-regex
92984345 298 "[^$]\\$[({]\\([-a-zA-Z0-9_.]+\\|[@%<?^+*][FD]?\\)"
9c859203
DP
299 "Regex used to find $(macro) uses in a makefile.")
300
72c0ae01 301(defconst makefile-ignored-files-in-pickup-regex
0c5b5a13 302 "\\(^\\..*\\)\\|\\(.*~$\\)\\|\\(.*,v$\\)\\|\\(\\.[chy]\\)"
72c0ae01
ER
303 "Regex for filenames that will NOT be included in the target list.")
304
602dc0da 305(defvar makefile-space 'makefile-space
118a9857 306 "Face to use for highlighting leading spaces in Font-Lock mode.")
308a2860 307
9c859203
DP
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
87393f26 318 `("-sinclude" "sinclude" ; makefile-makepp-statements takes rest
9c859203 319 "ifdef" "ifndef" "ifeq" "ifneq" "-include" "define" "endef" "export"
87393f26 320 "override define" "override" "unexport" "vpath"
9c859203
DP
321 ,@(cdr makefile-automake-statements))
322 "List of keywords understood by gmake.")
323
9c859203 324(defconst makefile-makepp-statements
87393f26
DP
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"
9c859203 330 "else ifdef" "else ifndef" "else ifeq" "else ifneq" "else ifperl"
87393f26
DP
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))
9c859203
DP
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
92984345
DP
351(defun makefile-make-font-lock-keywords (var keywords space
352 &optional negation
e02f48d7 353 &rest fl-keywords)
9c859203
DP
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 !=
602dc0da 358 (2 'makefile-shell prepend t)
8ade3c25
DP
359 ;; This is for after normal assignment
360 (3 'font-lock-string-face prepend t))
9c859203 361
9c859203 362 ;; Rule actions.
efee6a6d 363 ;; FIXME: When this spans multiple lines we need font-lock-multiline.
92984345 364 (makefile-match-action
efee6a6d 365 (1 font-lock-type-face nil t)
602dc0da 366 (2 'makefile-shell prepend)
9c859203
DP
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 ("[^$]\\(\\$[@%*]\\)"
602dc0da 378 1 'makefile-targets append)
9ffb4859 379
9c859203 380 ;; Fontify conditionals and includes.
9c859203 381 (,(concat "^\\(?: [ \t]*\\)?"
87393f26
DP
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)))
9c859203
DP
388 "\\>[ \t]*\\([^: \t\n#]*\\)")
389 (1 font-lock-keyword-face) (2 font-lock-variable-name-face))
9ffb4859 390
92984345 391 ,@(if negation
5c8b5442
DP
392 `((,negation (1 font-lock-negation-char-face prepend)
393 (2 font-lock-negation-char-face prepend t))))
9ffb4859 394
9c859203
DP
395 ,@(if space
396 '(;; Highlight lines that contain just whitespace.
397 ;; They can cause trouble, especially if they start with a tab.
602dc0da 398 ("^[ \t]+$" . makefile-space)
61bc75ab 399
9c859203
DP
400 ;; Highlight shell comments that Make treats as commands,
401 ;; since these can fool people.
602dc0da 402 ("^\t+#" 0 makefile-space t)
ba080a3c 403
9c859203
DP
404 ;; Highlight spaces that precede tabs.
405 ;; They can make a tab fail to be effective.
602dc0da 406 ("^\\( +\\)\t" 1 makefile-space)))
10f518f7 407
e02f48d7 408 ,@fl-keywords
92984345
DP
409
410 ;; Do dependencies.
411 (makefile-match-dependency
602dc0da
DP
412 (1 'makefile-targets prepend)
413 (3 'makefile-shell prepend t))))
bd0e4eb1 414
9c859203
DP
415(defconst makefile-font-lock-keywords
416 (makefile-make-font-lock-keywords
9c859203
DP
417 makefile-var-use-regex
418 makefile-statements
419 t))
420
421(defconst makefile-automake-font-lock-keywords
422 (makefile-make-font-lock-keywords
9c859203
DP
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
9c859203
DP
429 makefile-var-use-regex
430 makefile-gmake-statements
431 t
432 "^\\(?: [ \t]*\\)?if\\(n\\)\\(?:def\\|eq\\)\\>"
433
434 '("[^$]\\(\\$[({][@%*][DF][})]\\)"
602dc0da 435 1 'makefile-targets append)
9c859203
DP
436
437 ;; $(function ...) ${function ...}
92984345
DP
438 '("[^$]\\$[({]\\([-a-zA-Z0-9_.]+\\s \\)"
439 1 font-lock-function-name-face prepend)
9c859203
DP
440
441 ;; $(shell ...) ${shell ...}
442 '("[^$]\\$\\([({]\\)shell[ \t]+"
443 makefile-match-function-end nil nil
602dc0da 444 (1 'makefile-shell prepend t))))
9c859203
DP
445
446(defconst makefile-makepp-font-lock-keywords
447 (makefile-make-font-lock-keywords
9c859203
DP
448 makefile-var-use-regex
449 makefile-makepp-statements
450 nil
87393f26 451 "^\\(?: [ \t]*\\)?\\(?:and[ \t]+\\|else[ \t]+\\|or[ \t]+\\)?if\\(n\\)\\(?:def\\|eq\\|sys\\|true\\)\\>"
9c859203 452
adde7693 453 '("[^$]\\(\\$[({]\\(?:output\\|stem\\|target\\)s?\\_>.*?[})]\\)"
602dc0da 454 1 'makefile-targets append)
9c859203
DP
455
456 ;; Colon modifier keywords.
92984345
DP
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))
9c859203 461
87393f26
DP
462 ;; $(function ...) $((function ...)) ${...} ${{...}} $[...] $[[...]]
463 '("[^$]\\$\\(?:((?\\|{{?\\|\\[\\[?\\)\\([-a-zA-Z0-9_.]+\\s \\)"
92984345 464 1 font-lock-function-name-face prepend)
9c859203 465
87393f26
DP
466 ;; $(shell ...) $((shell ...)) ${...} ${{...}} $[...] $[[...]]
467 '("[^$]\\$\\(((?\\|{{?\\|\\[\\[?\\)shell\\(?:[-_]\\(?:global[-_]\\)?once\\)?[ \t]+"
9c859203 468 makefile-match-function-end nil nil
602dc0da 469 (1 'makefile-shell prepend t))
9c859203 470
87393f26
DP
471 ;; $(perl ...) $((perl ...)) ${...} ${{...}} $[...] $[[...]]
472 '("[^$]\\$\\(((?\\|{{?\\|\\[\\[?\\)makeperl[ \t]+"
9c859203 473 makefile-match-function-end nil nil
602dc0da 474 (1 'makefile-makepp-perl prepend t))
9c859203
DP
475 '("[^$]\\$\\(((?\\|{{?\\)perl[ \t]+"
476 makefile-match-function-end nil nil
602dc0da 477 (1 'makefile-makepp-perl t t))
9c859203
DP
478
479 ;; Can we unify these with (if (match-end 1) 'prepend t)?
602dc0da
DP
480 '("ifmakeperl\\s +\\(.*\\)" 1 'makefile-makepp-perl prepend)
481 '("ifperl\\s +\\(.*\\)" 1 'makefile-makepp-perl t)
9c859203
DP
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\\)+?\\)\\)\\)}"
602dc0da
DP
486 (1 'makefile-makepp-perl prepend t)
487 (2 'makefile-makepp-perl prepend t))
9c859203 488 '("\\<\\(?:perl\\|sub\\s +\\S +\\)\\s *\n?\\s *{\\(?:{\\s *\n?\\(\\(?:.*\n\\)+?\\)\\s *}\\|\\s *\\(\\(?:.*?\\|\n?\\(?:.*\n\\)+?\\)\\)\\)}"
602dc0da
DP
489 (1 'makefile-makepp-perl t t)
490 (2 'makefile-makepp-perl t t))
9c859203
DP
491
492 ;; Statement style perl block.
493 '("perl[-_]begin\\s *\\(?:\\s #.*\\)?\n\\(\\(?:.*\n\\)+?\\)\\s *perl[-_]end\\>"
602dc0da 494 1 'makefile-makepp-perl t)))
9c859203 495
9c859203
DP
496(defconst makefile-bsdmake-font-lock-keywords
497 (makefile-make-font-lock-keywords
92984345 498 ;; A lot more could be done for variables here:
9c859203
DP
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)))
f067044f 504
3968c89f 505(defconst makefile-imake-font-lock-keywords
e02f48d7 506 (append
3968c89f
MY
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
6783b1ce 518
cf38dd42
SM
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 "."))))
0b51ba8a 529
db85e69c 530(defvar makefile-imenu-generic-expression
774cd454 531 `(("Dependencies" makefile-previous-dependency 1)
92984345 532 ("Macro Assignment" ,makefile-macroassign-regex 1))
a564ebfc 533 "Imenu generic expression for Makefile mode. See `imenu-generic-expression'.")
db85e69c 534
9586c41a
SM
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
c5e87d10 547;; it. Try it with your favorite one. GNU make, System V make, and
9586c41a
SM
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;; ------------------------------------------------------------
72c0ae01 554
c098c7f1 555(defcustom makefile-brave-make "make"
fb7ada5f 556 "How to invoke make, for `makefile-query-targets'.
c098c7f1
RS
557This should identify a `make' command that can handle the `-q' option."
558 :type 'string
559 :group 'makefile)
72c0ae01 560
b308197b
RW
561(defcustom makefile-query-one-target-method-function
562 'makefile-query-by-make-minus-q
fb7ada5f 563 "Function to call to determine whether a make target is up to date.
bd0e4eb1 564The function must satisfy this calling convention:
72c0ae01
ER
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
a564ebfc 570 as a string. Depending on what you're going to do you may
72c0ae01
ER
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
c098c7f1
RS
575 makefile, any nonzero integer value otherwise."
576 :type 'function
577 :group 'makefile)
b308197b
RW
578(defvaralias 'makefile-query-one-target-method
579 'makefile-query-one-target-method-function)
c098c7f1
RS
580
581(defcustom makefile-up-to-date-buffer-name "*Makefile Up-to-date overview*"
fb7ada5f 582 "Name of the Up-to-date overview buffer."
c098c7f1
RS
583 :type 'string
584 :group 'makefile)
72c0ae01 585
72c0ae01
ER
586;;; --- end of up-to-date-overview configuration ------------------
587
9586c41a 588(define-abbrev-table 'makefile-mode-abbrev-table ()
9ffb4859 589 "Abbrev table in use in Makefile buffers.")
9ffb4859 590
9c859203 591(defvar makefile-mode-map
2f9a87f5
DN
592 (let ((map (make-sparse-keymap))
593 (opt-map (make-sparse-keymap)))
9c859203
DP
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)
3968c89f 612 (define-key map "\C-c\C-m\C-i" 'makefile-imake-mode)
9c859203
DP
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)
bcd70d97 617 (define-key map "\e\t" 'completion-at-point)
9c859203
DP
618
619 ;; Make menus.
620 (define-key map [menu-bar makefile-mode]
621 (cons "Makefile" (make-sparse-keymap "Makefile")))
622
2f9a87f5
DN
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))))
9c859203 649 (define-key map [menu-bar makefile-mode browse]
2f9a87f5
DN
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"))
9c859203 664 (define-key map [menu-bar makefile-mode pickup]
2f9a87f5
DN
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]
bcd70d97 668 '(menu-item "Complete Target or Macro" completion-at-point
2f9a87f5
DN
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] '("----"))
9c859203 675 (define-key map [menu-bar makefile-mode prev]
2f9a87f5
DN
676 '(menu-item "Move to Previous Dependency" makefile-previous-dependency
677 :help "Move point to the beginning of the previous dependency line"))
9c859203 678 (define-key map [menu-bar makefile-mode next]
2f9a87f5
DN
679 '(menu-item "Move to Next Dependency" makefile-next-dependency
680 :help "Move point to the beginning of the next dependency line"))
9c859203 681 map)
bd0e4eb1
RS
682 "The keymap that is used in Makefile mode.")
683
c5301b5c
SM
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)
72c0ae01 700 "The keymap that is used in the macro- and target browser.")
c5301b5c
SM
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))
6783b1ce 716
9586c41a
SM
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))
6783b1ce 724
72c0ae01
ER
725;;; ------------------------------------------------------------
726;;; Internal variables.
727;;; You don't need to configure below this line.
728;;; ------------------------------------------------------------
729
730(defvar makefile-target-table nil
308a2860 731 "Table of all target names known for this buffer.")
9e2d84cc 732(put 'makefile-target-table 'risky-local-variable t)
72c0ae01
ER
733
734(defvar makefile-macro-table nil
308a2860 735 "Table of all macro names known for this buffer.")
9e2d84cc 736(put 'makefile-macro-table 'risky-local-variable t)
72c0ae01
ER
737
738(defvar makefile-browser-client
bd0e4eb1 739 "A buffer in Makefile mode that is currently using the browser.")
72c0ae01
ER
740
741(defvar makefile-browser-selection-vector nil)
9e9bf716
ER
742(defvar makefile-has-prereqs nil)
743(defvar makefile-need-target-pickup t)
744(defvar makefile-need-macro-pickup t)
72c0ae01
ER
745
746(defvar makefile-mode-hook '())
747
308a2860
RS
748;; Each element looks like '("GNU MAKE FUNCTION" "ARG" "ARG" ... )
749;; Each "ARG" is used as a prompt for a required argument.
72c0ae01 750(defconst makefile-gnumake-functions-alist
72c0ae01
ER
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")
e1775344 765 ("addprefix" "Prefix" "Names")
72c0ae01
ER
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")
308a2860 775 ("shell" "Command")))
72c0ae01
ER
776
777
778;;; ------------------------------------------------------------
779;;; The mode function itself.
780;;; ------------------------------------------------------------
781
5c2946c7 782;;;###autoload
ae0c2494 783(define-derived-mode makefile-mode prog-mode "Makefile"
9c859203
DP
784 "Major mode for editing standard Makefiles.
785
786If you are editing a file for a different make, try one of the
787variants `makefile-automake-mode', `makefile-gmake-mode',
73290fbd
EZ
788`makefile-makepp-mode', `makefile-bsdmake-mode' or,
789`makefile-imake-mode'. All but the last should be correctly
790chosen based on the file name, except if it is *.mk. This
791function ends by invoking the function(s) `makefile-mode-hook'.
72c0ae01 792
92984345
DP
793It is strongly recommended to use `font-lock-mode', because that
794provides additional parsing information. This is used for
795example to see that a rule action `echo foo: bar' is a not rule
796dependency, despite the colon.
797
72c0ae01
ER
798\\{makefile-mode-map}
799
800In the browser, use the following keys:
801
802\\{makefile-browser-map}
803
bd0e4eb1 804Makefile mode can be configured by modifying the following variables:
72c0ae01 805
debe2de2 806`makefile-browser-buffer-name':
72c0ae01
ER
807 Name of the macro- and target browser buffer.
808
debe2de2 809`makefile-target-colon':
72c0ae01 810 The string that gets appended to all target names
bd0e4eb1 811 inserted by `makefile-insert-target'.
72c0ae01
ER
812 \":\" or \"::\" are quite common values.
813
debe2de2 814`makefile-macro-assign':
72c0ae01 815 The string that gets appended to all macro names
bd0e4eb1 816 inserted by `makefile-insert-macro'.
72c0ae01 817 The normal value should be \" = \", since this is what
a564ebfc 818 standard make expects. However, newer makes such as dmake
72c0ae01
ER
819 allow a larger variety of different macro assignments, so you
820 might prefer to use \" += \" or \" := \" .
821
debe2de2 822`makefile-tab-after-target-colon':
72c0ae01
ER
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
debe2de2 826`makefile-browser-leftmost-column':
72c0ae01
ER
827 Number of blanks to the left of the browser selection mark.
828
debe2de2 829`makefile-browser-cursor-column':
72c0ae01
ER
830 Column in which the cursor is positioned when it moves
831 up or down in the browser.
832
debe2de2 833`makefile-browser-selected-mark':
72c0ae01
ER
834 String used to mark selected entries in the browser.
835
debe2de2 836`makefile-browser-unselected-mark':
72c0ae01
ER
837 String used to mark unselected entries in the browser.
838
debe2de2 839`makefile-browser-auto-advance-after-selection-p':
72c0ae01
ER
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
debe2de2 844`makefile-pickup-everything-picks-up-filenames-p':
72c0ae01 845 If this variable is set to a non-nil value then
bd0e4eb1 846 `makefile-pickup-everything' also picks up filenames as targets
b20a44dc 847 (i.e. it calls `makefile-pickup-filenames-as-targets'), otherwise
72c0ae01
ER
848 filenames are omitted.
849
e006b3cd 850`makefile-cleanup-continuations':
a564ebfc 851 If this variable is set to a non-nil value then Makefile mode
72c0ae01
ER
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.
a564ebfc 856 IMPORTANT: Please note that enabling this option causes Makefile mode
bd0e4eb1 857 to MODIFY A FILE WITHOUT YOUR CONFIRMATION when \"it seems necessary\".
72c0ae01 858
debe2de2 859`makefile-browser-hook':
72c0ae01 860 A function or list of functions to be called just before the
9e9bf716 861 browser is entered. This is executed in the makefile buffer.
72c0ae01 862
debe2de2 863`makefile-special-targets-list':
72c0ae01 864 List of special targets. You will be offered to complete
bd0e4eb1
RS
865 on one of those in the minibuffer whenever you enter a `.'.
866 at the beginning of a line in Makefile mode."
bcd70d97
SM
867 (add-hook 'completion-at-point-functions
868 #'makefile-completions-at-point nil t)
e006b3cd
SM
869 (add-hook 'write-file-functions
870 'makefile-warn-suspicious-lines nil t)
a11e2800
RS
871 (add-hook 'write-file-functions
872 'makefile-warn-continuations nil t)
e006b3cd
SM
873 (add-hook 'write-file-functions
874 'makefile-cleanup-continuations nil t)
b8ca7cc3
RS
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)
6783b1ce
RS
880
881 ;; Font lock.
9586c41a
SM
882 (set (make-local-variable 'font-lock-defaults)
883 ;; SYNTAX-BEGIN set to backward-paragraph to avoid slow-down
884 ;; near the end of a large buffer, due to parse-partial-sexp's
885 ;; trying to parse all the way till the beginning of buffer.
886 '(makefile-font-lock-keywords
887 nil nil
888 ((?$ . "."))
cf38dd42
SM
889 backward-paragraph))
890 (set (make-local-variable 'syntax-propertize-function)
891 makefile-syntax-propertize-function)
6783b1ce
RS
892
893 ;; Add-log.
9586c41a
SM
894 (set (make-local-variable 'add-log-current-defun-function)
895 'makefile-add-log-defun)
6783b1ce
RS
896
897 ;; Imenu.
9586c41a
SM
898 (set (make-local-variable 'imenu-generic-expression)
899 makefile-imenu-generic-expression)
6783b1ce 900
3b114205 901 ;; Dabbrev.
9586c41a 902 (set (make-local-variable 'dabbrev-abbrev-skip-leading-regexp) "\\$")
3b114205 903
9ffb4859
GM
904 ;; Other abbrevs.
905 (setq local-abbrev-table makefile-mode-abbrev-table)
906
dfa9e6fc 907 ;; Filling.
9586c41a 908 (set (make-local-variable 'fill-paragraph-function) 'makefile-fill-paragraph)
dfa9e6fc 909
6783b1ce 910 ;; Comment stuff.
9586c41a
SM
911 (set (make-local-variable 'comment-start) "#")
912 (set (make-local-variable 'comment-end) "")
913 (set (make-local-variable 'comment-start-skip) "#+[ \t]*")
6783b1ce 914
debe2de2
SM
915 ;; Make sure TAB really inserts \t.
916 (set (make-local-variable 'indent-line-function) 'indent-to-left-margin)
917
6783b1ce 918 ;; Real TABs are important in makefiles
9586c41a 919 (setq indent-tabs-mode t))
6783b1ce 920
9c859203
DP
921;; These should do more than just differentiate font-lock.
922;;;###autoload
923(define-derived-mode makefile-automake-mode makefile-mode "Makefile.am"
924 "An adapted `makefile-mode' that knows about automake."
925 (setq font-lock-defaults
926 `(makefile-automake-font-lock-keywords ,@(cdr font-lock-defaults))))
927
928;;;###autoload
929(define-derived-mode makefile-gmake-mode makefile-mode "GNUmakefile"
930 "An adapted `makefile-mode' that knows about gmake."
931 (setq font-lock-defaults
932 `(makefile-gmake-font-lock-keywords ,@(cdr font-lock-defaults))))
933
934;;;###autoload
935(define-derived-mode makefile-makepp-mode makefile-mode "Makeppfile"
936 "An adapted `makefile-mode' that knows about makepp."
30edba6e
DP
937 (set (make-local-variable 'makefile-rule-action-regex)
938 makefile-makepp-rule-action-regex)
9c859203 939 (setq font-lock-defaults
774cd454
DP
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)))
9c859203
DP
944
945;;;###autoload
946(define-derived-mode makefile-bsdmake-mode makefile-mode "BSDmakefile"
947 "An adapted `makefile-mode' that knows about BSD make."
92984345 948 (set (make-local-variable 'makefile-dependency-regex)
30edba6e 949 makefile-bsdmake-dependency-regex)
0b11ce59 950 (set (make-local-variable 'makefile-dependency-skip) "^:!")
92984345 951 (set (make-local-variable 'makefile-rule-action-regex)
30edba6e 952 makefile-bsdmake-rule-action-regex)
9c859203 953 (setq font-lock-defaults
774cd454 954 `(makefile-bsdmake-font-lock-keywords ,@(cdr font-lock-defaults))))
9c859203 955
3968c89f
MY
956;;;###autoload
957(define-derived-mode makefile-imake-mode makefile-mode "Imakefile"
958 "An adapted `makefile-mode' that knows about imake."
959 :syntax-table makefile-imake-mode-syntax-table
cf38dd42
SM
960 (set (make-local-variable 'syntax-propertize-function) nil)
961 (setq font-lock-defaults
962 `(makefile-imake-font-lock-keywords ,@(cdr font-lock-defaults))))
3968c89f 963
6783b1ce
RS
964\f
965
966;;; Motion code.
72c0ae01 967
72c0ae01 968(defun makefile-next-dependency ()
bd0e4eb1 969 "Move point to the beginning of the next dependency line."
72c0ae01
ER
970 (interactive)
971 (let ((here (point)))
972 (end-of-line)
0b11ce59 973 (if (makefile-match-dependency nil)
72c0ae01
ER
974 (progn (beginning-of-line) t) ; indicate success
975 (goto-char here) nil)))
6783b1ce 976
72c0ae01 977(defun makefile-previous-dependency ()
bd0e4eb1 978 "Move point to the beginning of the previous dependency line."
72c0ae01 979 (interactive)
0b11ce59 980 (let ((pt (point)))
72c0ae01 981 (beginning-of-line)
0b11ce59
DP
982 ;; makefile-match-dependency done backwards:
983 (catch 'found
dfa89b5d
DP
984 (while (progn (skip-chars-backward makefile-dependency-skip)
985 (not (bobp)))
5c8b5442
DP
986 (or (prog1 (eq (char-after) ?=)
987 (backward-char))
988 (get-text-property (point) 'face)
0b11ce59 989 (beginning-of-line)
602dc0da
DP
990 (if (> (point) (+ (point-min) 2))
991 (eq (char-before (1- (point))) ?\\))
0b11ce59
DP
992 (if (looking-at makefile-dependency-regex)
993 (throw 'found t))))
994 (goto-char pt)
995 nil)))
72c0ae01 996
6783b1ce 997\f
72c0ae01 998
6783b1ce 999;;; Electric keys. Blech.
9e9bf716 1000
6783b1ce
RS
1001(defun makefile-electric-dot (arg)
1002 "Prompt for the name of a special target to insert.
1003Only does electric insertion at beginning of line.
1004Anywhere else just self-inserts."
1005 (interactive "p")
72c0ae01
ER
1006 (if (bolp)
1007 (makefile-insert-special-target)
6783b1ce 1008 (self-insert-command arg)))
72c0ae01 1009
72c0ae01 1010(defun makefile-insert-special-target ()
ac282af8 1011 "Prompt for and insert a special target name.
6783b1ce 1012Uses `makefile-special-targets' list."
72c0ae01 1013 (interactive)
9e9bf716 1014 (makefile-pickup-targets)
6783b1ce
RS
1015 (let ((special-target
1016 (completing-read "Special target: "
1017 makefile-special-targets-list nil nil nil)))
72c0ae01
ER
1018 (if (zerop (length special-target))
1019 ()
6783b1ce 1020 (insert "." special-target ":")
72c0ae01
ER
1021 (makefile-forward-after-target-colon))))
1022
6783b1ce
RS
1023(defun makefile-electric-equal (arg)
1024 "Prompt for name of a macro to insert.
1025Only does prompting if point is at beginning of line.
1026Anywhere else just self-inserts."
1027 (interactive "p")
9e9bf716 1028 (makefile-pickup-macros)
72c0ae01
ER
1029 (if (bolp)
1030 (call-interactively 'makefile-insert-macro)
6783b1ce 1031 (self-insert-command arg)))
72c0ae01
ER
1032
1033(defun makefile-insert-macro (macro-name)
1034 "Prepare definition of a new macro."
1035 (interactive "sMacro Name: ")
9e9bf716 1036 (makefile-pickup-macros)
72c0ae01
ER
1037 (if (not (zerop (length macro-name)))
1038 (progn
1039 (beginning-of-line)
6783b1ce 1040 (insert macro-name makefile-macro-assign)
9e9bf716 1041 (setq makefile-need-macro-pickup t)
72c0ae01
ER
1042 (makefile-remember-macro macro-name))))
1043
72c0ae01 1044(defun makefile-insert-macro-ref (macro-name)
bd0e4eb1 1045 "Complete on a list of known macros, then insert complete ref at point."
72c0ae01
ER
1046 (interactive
1047 (list
9e9bf716
ER
1048 (progn
1049 (makefile-pickup-macros)
1050 (completing-read "Refer to macro: " makefile-macro-table nil nil nil))))
6783b1ce 1051 (makefile-do-macro-insertion macro-name))
72c0ae01 1052
72c0ae01
ER
1053(defun makefile-insert-target (target-name)
1054 "Prepare definition of a new target (dependency line)."
1055 (interactive "sTarget: ")
1056 (if (not (zerop (length target-name)))
1057 (progn
1058 (beginning-of-line)
6783b1ce 1059 (insert target-name makefile-target-colon)
72c0ae01
ER
1060 (makefile-forward-after-target-colon)
1061 (end-of-line)
9e9bf716 1062 (setq makefile-need-target-pickup t)
72c0ae01
ER
1063 (makefile-remember-target target-name))))
1064
72c0ae01 1065(defun makefile-insert-target-ref (target-name)
a564ebfc 1066 "Complete on a list of known targets, then insert TARGET-NAME at point."
72c0ae01
ER
1067 (interactive
1068 (list
0c5b5a13 1069 (progn
9e9bf716
ER
1070 (makefile-pickup-targets)
1071 (completing-read "Refer to target: " makefile-target-table nil nil nil))))
72c0ae01 1072 (if (not (zerop (length target-name)))
6783b1ce 1073 (insert target-name " ")))
72c0ae01 1074
6783b1ce
RS
1075(defun makefile-electric-colon (arg)
1076 "Prompt for name of new target.
1077Prompting only happens at beginning of line.
1078Anywhere else just self-inserts."
1079 (interactive "p")
72c0ae01
ER
1080 (if (bolp)
1081 (call-interactively 'makefile-insert-target)
6783b1ce
RS
1082 (self-insert-command arg)))
1083
1084\f
72c0ae01 1085
72c0ae01
ER
1086;;; ------------------------------------------------------------
1087;;; Extracting targets and macros from an existing makefile
1088;;; ------------------------------------------------------------
1089
1090(defun makefile-pickup-targets ()
308a2860 1091 "Notice names of all target definitions in Makefile."
72c0ae01 1092 (interactive)
dfa89b5d
DP
1093 (when makefile-need-target-pickup
1094 (setq makefile-need-target-pickup nil
1095 makefile-target-table nil
1096 makefile-has-prereqs nil)
9e9bf716
ER
1097 (save-excursion
1098 (goto-char (point-min))
92984345 1099 (while (makefile-match-dependency nil)
dfa89b5d
DP
1100 (goto-char (match-beginning 1))
1101 (while (let ((target-name
1102 (buffer-substring-no-properties (point)
1103 (progn
1104 (skip-chars-forward "^ \t:#")
1105 (point))))
9e9bf716
ER
1106 (has-prereqs
1107 (not (looking-at ":[ \t]*$"))))
dfa89b5d
DP
1108 (if (makefile-remember-target target-name has-prereqs)
1109 (message "Picked up target \"%s\" from line %d"
1110 target-name (line-number-at-pos)))
1111 (skip-chars-forward " \t")
1112 (not (or (eolp) (eq (char-after) ?:)))))
1113 (forward-line)))
1114 (message "Read targets OK.")))
72c0ae01 1115
72c0ae01 1116(defun makefile-pickup-macros ()
308a2860 1117 "Notice names of all macro definitions in Makefile."
72c0ae01 1118 (interactive)
dfa89b5d
DP
1119 (when makefile-need-macro-pickup
1120 (setq makefile-need-macro-pickup nil
1121 makefile-macro-table nil)
9e9bf716
ER
1122 (save-excursion
1123 (goto-char (point-min))
e006b3cd 1124 (while (re-search-forward makefile-macroassign-regex nil t)
dfa89b5d
DP
1125 (goto-char (match-beginning 1))
1126 (let ((macro-name (buffer-substring-no-properties (point)
1127 (progn
1128 (skip-chars-forward "^ \t:#=*")
1129 (point)))))
1130 (if (makefile-remember-macro macro-name)
1131 (message "Picked up macro \"%s\" from line %d"
1132 macro-name (line-number-at-pos))))
1133 (forward-line)))
9e9bf716 1134 (message "Read macros OK.")))
72c0ae01 1135
6783b1ce 1136(defun makefile-pickup-everything (arg)
308a2860 1137 "Notice names of all macros and targets in Makefile.
6783b1ce
RS
1138Prefix arg means force pickups to be redone."
1139 (interactive "P")
1140 (if arg
dfa89b5d
DP
1141 (setq makefile-need-target-pickup t
1142 makefile-need-macro-pickup t))
72c0ae01
ER
1143 (makefile-pickup-macros)
1144 (makefile-pickup-targets)
1145 (if makefile-pickup-everything-picks-up-filenames-p
1146 (makefile-pickup-filenames-as-targets)))
1147
72c0ae01 1148(defun makefile-pickup-filenames-as-targets ()
308a2860
RS
1149 "Scan the current directory for filenames to use as targets.
1150Checks each filename against `makefile-ignored-files-in-pickup-regex'
1151and adds all qualifying names to the list of known targets."
72c0ae01 1152 (interactive)
dfa89b5d
DP
1153 (mapc (lambda (name)
1154 (or (file-directory-p name)
1155 (string-match makefile-ignored-files-in-pickup-regex name)
1156 (if (makefile-remember-target name)
1157 (message "Picked up file \"%s\" as target" name))))
1158 (file-name-all-completions "" (or (file-name-directory (buffer-file-name)) ""))))
72c0ae01 1159
6783b1ce
RS
1160\f
1161
1162;;; Completion.
1163
bcd70d97 1164(defun makefile-completions-at-point ()
6783b1ce
RS
1165 (let* ((beg (save-excursion
1166 (skip-chars-backward "^$(){}:#= \t\n")
1167 (point)))
ea52206b
SM
1168 (paren nil)
1169 (do-macros
1170 (save-excursion
1171 (goto-char beg)
1172 (let ((pc (preceding-char)))
1173 (cond
1174 ;; Preceding "$" means macros only.
1175 ((= pc ?$)
1176 t)
1177
1178 ;; Preceding "$(" or "${" means macros only.
1179 ((and (memq pc '(?\{ ?\())
1180 (progn
bcd70d97 1181 (setq paren (if (eq pc ?\{) ?\} ?\)))
ea52206b
SM
1182 (backward-char)
1183 (= (preceding-char) ?$)))
1184 t)))))
bcd70d97
SM
1185 (suffix (cond
1186 (do-macros (if paren (string paren)))
1187 ((save-excursion (goto-char beg) (bolp)) ":")
1188 (t " "))))
1189 (list beg (point)
1190 (append (if do-macros '() makefile-target-table)
1191 makefile-macro-table)
1192 :exit-function
1193 (if suffix
1194 (lambda (_s finished)
1195 (when (memq finished '(sole finished))
1196 (if (looking-at (regexp-quote suffix))
1197 (goto-char (match-end 0))
1198 (insert suffix))))))))
1199
1200(define-obsolete-function-alias 'makefile-complete 'completion-at-point "24.1")
6783b1ce
RS
1201\f
1202
308a2860
RS
1203;; Backslashification. Stolen from cc-mode.el.
1204
dfa9e6fc
RS
1205(defun makefile-backslash-region (from to delete-flag)
1206 "Insert, align, or delete end-of-line backslashes on the lines in the region.
1207With no argument, inserts backslashes and aligns existing backslashes.
1208With an argument, deletes the backslashes.
1209
a564ebfc 1210This function does not modify the last line of the region if the region ends
dfa9e6fc
RS
1211right at the start of the following line; it does not modify blank lines
1212at the start of the region. So you can put the region around an entire macro
1213definition and conveniently use this command."
308a2860
RS
1214 (interactive "r\nP")
1215 (save-excursion
dfa9e6fc
RS
1216 (goto-char from)
1217 (let ((column makefile-backslash-column)
1218 (endmark (make-marker)))
1219 (move-marker endmark to)
1220 ;; Compute the smallest column number past the ends of all the lines.
1221 (if makefile-backslash-align
1222 (progn
1223 (if (not delete-flag)
1224 (while (< (point) to)
1225 (end-of-line)
1226 (if (= (preceding-char) ?\\)
1227 (progn (forward-char -1)
1228 (skip-chars-backward " \t")))
1229 (setq column (max column (1+ (current-column))))
1230 (forward-line 1)))
1231 ;; Adjust upward to a tab column, if that doesn't push
1232 ;; past the margin.
1233 (if (> (% column tab-width) 0)
1234 (let ((adjusted (* (/ (+ column tab-width -1) tab-width)
1235 tab-width)))
1236 (if (< adjusted (window-width))
1237 (setq column adjusted))))))
1238 ;; Don't modify blank lines at start of region.
1239 (goto-char from)
1240 (while (and (< (point) endmark) (eolp))
1241 (forward-line 1))
1242 ;; Add or remove backslashes on all the lines.
1243 (while (and (< (point) endmark)
1244 ;; Don't backslashify the last line
1245 ;; if the region ends right at the start of the next line.
1246 (save-excursion
1247 (forward-line 1)
1248 (< (point) endmark)))
1249 (if (not delete-flag)
1250 (makefile-append-backslash column)
1251 (makefile-delete-backslash))
1252 (forward-line 1))
1253 (move-marker endmark nil))))
1254
1255(defun makefile-append-backslash (column)
1256 (end-of-line)
1257 ;; Note that "\\\\" is needed to get one backslash.
1258 (if (= (preceding-char) ?\\)
1259 (progn (forward-char -1)
1260 (delete-horizontal-space)
1261 (indent-to column (if makefile-backslash-align nil 1)))
1262 (indent-to column (if makefile-backslash-align nil 1))
1263 (insert "\\")))
1264
1265(defun makefile-delete-backslash ()
1266 (end-of-line)
1267 (or (bolp)
1268 (progn
1269 (forward-char -1)
1270 (if (looking-at "\\\\")
1271 (delete-region (1+ (point))
1272 (progn (skip-chars-backward " \t") (point)))))))
1273
1274\f
1275
1276;; Filling
1277
e02f48d7 1278(defun makefile-fill-paragraph (_arg)
dfa9e6fc
RS
1279 ;; Fill comments, backslashed lines, and variable definitions
1280 ;; specially.
1281 (save-excursion
1282 (beginning-of-line)
1283 (cond
5e01f1ca 1284 ((looking-at "^[ \t]*#+\\s-*")
c5301b5c
SM
1285 ;; Found a comment. Return nil to let normal filling take place.
1286 nil)
dfa9e6fc
RS
1287
1288 ;; Must look for backslashed-region before looking for variable
1289 ;; assignment.
e006b3cd
SM
1290 ((or (eq (char-before (line-end-position 1)) ?\\)
1291 (eq (char-before (line-end-position 0)) ?\\))
dfa9e6fc 1292 ;; A backslash region. Find beginning and end, remove
b0b19974 1293 ;; backslashes, fill, and then reapply backslashes.
dfa9e6fc
RS
1294 (end-of-line)
1295 (let ((beginning
1296 (save-excursion
1297 (end-of-line 0)
1298 (while (= (preceding-char) ?\\)
1299 (end-of-line 0))
1300 (forward-char)
1301 (point)))
1302 (end
1303 (save-excursion
1304 (while (= (preceding-char) ?\\)
1305 (end-of-line 2))
1306 (point))))
1307 (save-restriction
1308 (narrow-to-region beginning end)
1309 (makefile-backslash-region (point-min) (point-max) t)
8f081e96
SM
1310 (let ((fill-paragraph-function nil)
1311 ;; Adjust fill-column to allow space for the backslash.
1312 (fill-column (- fill-column 1)))
dfa9e6fc
RS
1313 (fill-paragraph nil))
1314 (makefile-backslash-region (point-min) (point-max) nil)
1315 (goto-char (point-max))
1316 (if (< (skip-chars-backward "\n") 0)
c5301b5c
SM
1317 (delete-region (point) (point-max)))))
1318 ;; Return non-nil to indicate it's been filled.
1319 t)
dfa9e6fc
RS
1320
1321 ((looking-at makefile-macroassign-regex)
1322 ;; Have a macro assign. Fill just this line, and then backslash
1323 ;; resulting region.
308a2860 1324 (save-restriction
debe2de2 1325 (narrow-to-region (point) (line-beginning-position 2))
8f081e96
SM
1326 (let ((fill-paragraph-function nil)
1327 ;; Adjust fill-column to allow space for the backslash.
1328 (fill-column (- fill-column 1)))
dfa9e6fc 1329 (fill-paragraph nil))
c5301b5c
SM
1330 (makefile-backslash-region (point-min) (point-max) nil))
1331 ;; Return non-nil to indicate it's been filled.
1332 t)
dfa9e6fc 1333
c5301b5c
SM
1334 (t
1335 ;; Return non-nil so we don't fill anything else.
1336 t))))
308a2860
RS
1337
1338\f
1339
72c0ae01 1340;;; ------------------------------------------------------------
6783b1ce 1341;;; Browser mode.
72c0ae01
ER
1342;;; ------------------------------------------------------------
1343
72c0ae01
ER
1344(defun makefile-browser-format-target-line (target selected)
1345 (format
1346 (concat (make-string makefile-browser-leftmost-column ?\ )
1347 (if selected
1348 makefile-browser-selected-mark
1349 makefile-browser-unselected-mark)
1350 "%s%s")
1351 target makefile-target-colon))
1352
1353(defun makefile-browser-format-macro-line (macro selected)
1354 (format
1355 (concat (make-string makefile-browser-leftmost-column ?\ )
1356 (if selected
1357 makefile-browser-selected-mark
1358 makefile-browser-unselected-mark)
1359 (makefile-format-macro-ref macro))))
1360
1361(defun makefile-browser-fill (targets macros)
c72344c7
RS
1362 (let ((inhibit-read-only t))
1363 (goto-char (point-min))
1364 (erase-buffer)
1365 (mapconcat
1366 (function
1367 (lambda (item) (insert (makefile-browser-format-target-line (car item) nil) "\n")))
1368 targets
1369 "")
1370 (mapconcat
1371 (function
1372 (lambda (item) (insert (makefile-browser-format-macro-line (car item) nil) "\n")))
1373 macros
1374 "")
1375 (sort-lines nil (point-min) (point-max))
1376 (goto-char (1- (point-max)))
1377 (delete-char 1) ; remove unnecessary newline at eob
1378 (goto-char (point-min))
1379 (forward-char makefile-browser-cursor-column)))
72c0ae01
ER
1380
1381;;;
1382;;; Moving up and down in the browser
1383;;;
1384
1385(defun makefile-browser-next-line ()
1386 "Move the browser selection cursor to the next line."
1387 (interactive)
1388 (if (not (makefile-last-line-p))
1389 (progn
1390 (forward-line 1)
1391 (forward-char makefile-browser-cursor-column))))
1392
1393(defun makefile-browser-previous-line ()
1394 "Move the browser selection cursor to the previous line."
1395 (interactive)
1396 (if (not (makefile-first-line-p))
1397 (progn
1398 (forward-line -1)
1399 (forward-char makefile-browser-cursor-column))))
1400
1401;;;
1402;;; Quitting the browser (returns to client buffer)
1403;;;
1404
1405(defun makefile-browser-quit ()
308a2860 1406 "Leave the browser and return to the makefile buffer."
72c0ae01
ER
1407 (interactive)
1408 (let ((my-client makefile-browser-client))
1409 (setq makefile-browser-client nil) ; we quitted, so NO client!
1410 (set-buffer-modified-p nil)
59047653 1411 (quit-window t)
72c0ae01
ER
1412 (pop-to-buffer my-client)))
1413
1414;;;
1415;;; Toggle state of a browser item
1416;;;
1417
1418(defun makefile-browser-toggle ()
1419 "Toggle the selection state of the browser item at the cursor position."
1420 (interactive)
1421 (let ((this-line (count-lines (point-min) (point))))
1422 (setq this-line (max 1 this-line))
1423 (makefile-browser-toggle-state-for-line this-line)
df1c29a0
GM
1424 (goto-char (point-min))
1425 (forward-line (1- this-line))
c72344c7 1426 (let ((inhibit-read-only t))
df1c29a0 1427 (beginning-of-line) ; redundant?
c72344c7
RS
1428 (if (makefile-browser-on-macro-line-p)
1429 (let ((macro-name (makefile-browser-this-line-macro-name)))
8d3a7934 1430 (delete-region (point) (progn (end-of-line) (point)))
c72344c7
RS
1431 (insert
1432 (makefile-browser-format-macro-line
1433 macro-name
1434 (makefile-browser-get-state-for-line this-line))))
1435 (let ((target-name (makefile-browser-this-line-target-name)))
8d3a7934 1436 (delete-region (point) (progn (end-of-line) (point)))
72c0ae01 1437 (insert
c72344c7
RS
1438 (makefile-browser-format-target-line
1439 target-name
1440 (makefile-browser-get-state-for-line this-line))))))
72c0ae01
ER
1441 (beginning-of-line)
1442 (forward-char makefile-browser-cursor-column)
1443 (if makefile-browser-auto-advance-after-selection-p
1444 (makefile-browser-next-line))))
1445
1446;;;
1447;;; Making insertions into the client buffer
1448;;;
1449
1450(defun makefile-browser-insert-continuation ()
9e9bf716 1451 "Insert a makefile continuation.
308a2860 1452In the makefile buffer, go to (end-of-line), insert a \'\\\'
72c0ae01 1453character, insert a new blank line, go to that line and indent by one TAB.
9e9bf716
ER
1454This is most useful in the process of creating continued lines when copying
1455large dependencies from the browser to the client buffer.
a4e104bf 1456\(point) advances accordingly in the client buffer."
72c0ae01 1457 (interactive)
debe2de2 1458 (with-current-buffer makefile-browser-client
72c0ae01
ER
1459 (end-of-line)
1460 (insert "\\\n\t")))
1461
1462(defun makefile-browser-insert-selection ()
308a2860 1463 "Insert all selected targets and/or macros in the makefile buffer.
bd0e4eb1 1464Insertion takes place at point."
72c0ae01
ER
1465 (interactive)
1466 (save-excursion
df1c29a0 1467 (goto-char (point-min))
72c0ae01
ER
1468 (let ((current-line 1))
1469 (while (not (eobp))
1470 (if (makefile-browser-get-state-for-line current-line)
1471 (makefile-browser-send-this-line-item))
1472 (forward-line 1)
1473 (setq current-line (1+ current-line))))))
1474
1475(defun makefile-browser-insert-selection-and-quit ()
1476 (interactive)
1477 (makefile-browser-insert-selection)
1478 (makefile-browser-quit))
1479
1480(defun makefile-browser-send-this-line-item ()
1481 (if (makefile-browser-on-macro-line-p)
1482 (save-excursion
1483 (let ((macro-name (makefile-browser-this-line-macro-name)))
1484 (set-buffer makefile-browser-client)
1485 (insert (makefile-format-macro-ref macro-name) " ")))
1486 (save-excursion
1487 (let ((target-name (makefile-browser-this-line-target-name)))
1488 (set-buffer makefile-browser-client)
1489 (insert target-name " ")))))
1490
72c0ae01
ER
1491(defun makefile-browser-start-interaction ()
1492 (use-local-map makefile-browser-map)
1493 (setq buffer-read-only t))
1494
72c0ae01 1495(defun makefile-browse (targets macros)
72c0ae01
ER
1496 (if (zerop (+ (length targets) (length macros)))
1497 (progn
1498 (beep)
1499 (message "No macros or targets to browse! Consider running 'makefile-pickup-everything\'"))
1500 (let ((browser-buffer (get-buffer-create makefile-browser-buffer-name)))
1501 (pop-to-buffer browser-buffer)
72c0ae01 1502 (makefile-browser-fill targets macros)
9e9bf716 1503 (shrink-window-if-larger-than-buffer)
a7e34f79
SM
1504 (set (make-local-variable 'makefile-browser-selection-vector)
1505 (make-vector (+ (length targets) (length macros)) nil))
72c0ae01 1506 (makefile-browser-start-interaction))))
72c0ae01
ER
1507
1508(defun makefile-switch-to-browser ()
1509 (interactive)
1510 (run-hooks 'makefile-browser-hook)
1511 (setq makefile-browser-client (current-buffer))
9e9bf716
ER
1512 (makefile-pickup-targets)
1513 (makefile-pickup-macros)
72c0ae01
ER
1514 (makefile-browse makefile-target-table makefile-macro-table))
1515
6783b1ce 1516\f
72c0ae01
ER
1517
1518;;; ------------------------------------------------------------
1519;;; Up-to-date overview buffer
1520;;; ------------------------------------------------------------
1521
1522(defun makefile-create-up-to-date-overview ()
9e9bf716 1523 "Create a buffer containing an overview of the state of all known targets.
72c0ae01
ER
1524Known targets are targets that are explicitly defined in that makefile;
1525in other words, all targets that appear on the left hand side of a
1526dependency in the makefile."
1527 (interactive)
1528 (if (y-or-n-p "Are you sure that the makefile being edited is consistent? ")
1529 ;;
1530 ;; The rest of this function operates on a temporary makefile, created by
1531 ;; writing the current contents of the makefile buffer.
1532 ;;
1533 (let ((saved-target-table makefile-target-table)
1534 (this-buffer (current-buffer))
1535 (makefile-up-to-date-buffer
1536 (get-buffer-create makefile-up-to-date-buffer-name))
1537 (filename (makefile-save-temporary))
1538 ;;
1539 ;; Forget the target table because it may contain picked-up filenames
1540 ;; that are not really targets in the current makefile.
1541 ;; We don't want to query these, so get a new target-table with just the
1542 ;; targets that can be found in the makefile buffer.
1543 ;; The 'old' target table will be restored later.
1544 ;;
1545 (real-targets (progn
72c0ae01 1546 (makefile-pickup-targets)
9e9bf716
ER
1547 makefile-target-table))
1548 (prereqs makefile-has-prereqs)
1549 )
72c0ae01
ER
1550
1551 (set-buffer makefile-up-to-date-buffer)
1552 (setq buffer-read-only nil)
1553 (erase-buffer)
9e9bf716 1554 (makefile-query-targets filename real-targets prereqs)
72c0ae01
ER
1555 (if (zerop (buffer-size)) ; if it did not get us anything
1556 (progn
1557 (kill-buffer (current-buffer))
1558 (message "No overview created!")))
1559 (set-buffer this-buffer)
1560 (setq makefile-target-table saved-target-table)
1561 (if (get-buffer makefile-up-to-date-buffer-name)
1562 (progn
1563 (pop-to-buffer (get-buffer makefile-up-to-date-buffer-name))
9e9bf716 1564 (shrink-window-if-larger-than-buffer)
72c0ae01
ER
1565 (sort-lines nil (point-min) (point-max))
1566 (setq buffer-read-only t))))))
72c0ae01
ER
1567
1568(defun makefile-save-temporary ()
1569 "Create a temporary file from the current makefile buffer."
1570 (let ((filename (makefile-generate-temporary-filename)))
1571 (write-region (point-min) (point-max) filename nil 0)
1572 filename)) ; return the filename
1573
1574(defun makefile-generate-temporary-filename ()
308a2860 1575 "Create a filename suitable for use in `makefile-save-temporary'.
72c0ae01 1576Be careful to allow brain-dead file systems (DOS, SYSV ...) to cope
308a2860 1577with the generated name!"
72c0ae01
ER
1578 (let ((my-name (user-login-name))
1579 (my-uid (int-to-string (user-uid))))
1580 (concat "mktmp"
1581 (if (> (length my-name) 3)
1582 (substring my-name 0 3)
1583 my-name)
1584 "."
1585 (if (> (length my-uid) 3)
1586 (substring my-uid 0 3)
1587 my-uid))))
1588
9e9bf716 1589(defun makefile-query-targets (filename target-table prereq-list)
bd0e4eb1 1590 "Fill the up-to-date overview buffer.
b308197b
RW
1591Checks each target in TARGET-TABLE using
1592`makefile-query-one-target-method-function'
72c0ae01
ER
1593and generates the overview, one line per target name."
1594 (insert
9e9bf716
ER
1595 (mapconcat
1596 (function (lambda (item)
1597 (let* ((target-name (car item))
1598 (no-prereqs (not (member target-name prereq-list)))
a564ebfc 1599 (needs-rebuild (or no-prereqs
9e9bf716 1600 (funcall
b308197b 1601 makefile-query-one-target-method-function
9e9bf716
ER
1602 target-name
1603 filename))))
1604 (format "\t%s%s"
1605 target-name
1606 (cond (no-prereqs " .. has no prerequisites")
1607 (needs-rebuild " .. NEEDS REBUILD")
1608 (t " .. is up to date"))))
1609 ))
1610 target-table "\n"))
72c0ae01
ER
1611 (goto-char (point-min))
1612 (delete-file filename)) ; remove the tmpfile
1613
1614(defun makefile-query-by-make-minus-q (target &optional filename)
15502042 1615 (not (eq 0
308a2860
RS
1616 (call-process makefile-brave-make nil nil nil
1617 "-f" filename "-q" target))))
72c0ae01 1618
6783b1ce
RS
1619\f
1620
72c0ae01
ER
1621;;; ------------------------------------------------------------
1622;;; Continuation cleanup
1623;;; ------------------------------------------------------------
1624
1625(defun makefile-cleanup-continuations ()
e956634f 1626 (if (derived-mode-p 'makefile-mode)
e006b3cd 1627 (if (and makefile-cleanup-continuations
72c0ae01
ER
1628 (not buffer-read-only))
1629 (save-excursion
1630 (goto-char (point-min))
e006b3cd 1631 (while (re-search-forward "\\\\[ \t]+$" nil t)
72c0ae01
ER
1632 (replace-match "\\" t t))))))
1633
b8ca7cc3
RS
1634
1635;;; ------------------------------------------------------------
1636;;; Warn of suspicious lines
1637;;; ------------------------------------------------------------
1638
1639(defun makefile-warn-suspicious-lines ()
68049bfa 1640 ;; Returning non-nil cancels the save operation
e956634f 1641 (if (derived-mode-p 'makefile-mode)
68049bfa
SM
1642 (save-excursion
1643 (goto-char (point-min))
1644 (if (re-search-forward "^\\(\t+$\\| +\t\\)" nil t)
1645 (not (y-or-n-p
a11e2800
RS
1646 (format "Suspicious line %d. Save anyway? "
1647 (count-lines (point-min) (point)))))))))
1648
1649(defun makefile-warn-continuations ()
e956634f 1650 (if (derived-mode-p 'makefile-mode)
a11e2800
RS
1651 (save-excursion
1652 (goto-char (point-min))
5cc2d137 1653 (if (re-search-forward "\\\\[ \t]+$" nil t)
a11e2800
RS
1654 (not (y-or-n-p
1655 (format "Suspicious continuation in line %d. Save anyway? "
68049bfa 1656 (count-lines (point-min) (point)))))))))
6783b1ce 1657\f
b8ca7cc3 1658
72c0ae01
ER
1659;;; ------------------------------------------------------------
1660;;; GNU make function support
1661;;; ------------------------------------------------------------
1662
1663(defun makefile-insert-gmake-function ()
308a2860
RS
1664 "Insert a GNU make function call.
1665Asks for the name of the function to use (with completion).
1666Then prompts for all required parameters."
72c0ae01
ER
1667 (interactive)
1668 (let* ((gm-function-name (completing-read
1669 "Function: "
1670 makefile-gnumake-functions-alist
1671 nil t nil))
1672 (gm-function-prompts
1673 (cdr (assoc gm-function-name makefile-gnumake-functions-alist))))
1674 (if (not (zerop (length gm-function-name)))
1675 (insert (makefile-format-macro-ref
1676 (concat gm-function-name " "
1677 (makefile-prompt-for-gmake-funargs
1678 gm-function-name gm-function-prompts)))
1679 " "))))
1680
1681(defun makefile-prompt-for-gmake-funargs (function-name prompt-list)
1682 (mapconcat
1683 (function (lambda (one-prompt)
308a2860
RS
1684 (read-string (format "[%s] %s: " function-name one-prompt)
1685 nil)))
72c0ae01
ER
1686 prompt-list
1687 ","))
308a2860 1688
6783b1ce 1689\f
72c0ae01
ER
1690
1691;;; ------------------------------------------------------------
1692;;; Utility functions
1693;;; ------------------------------------------------------------
1694
e02f48d7 1695(defun makefile-match-function-end (_end)
9c859203
DP
1696 "To be called as an anchored matcher by font-lock.
1697The anchor must have matched the opening parens in the first group."
1698 (let ((s (match-string-no-properties 1)))
dc184009
GM
1699 ;; FIXME forward-sexp or somesuch would be better?
1700 (if (setq s (cond ((string= s "(") ")")
1701 ((string= s "{") "}")
87393f26 1702 ((string= s "[") "]")
dc184009 1703 ((string= s "((") "))")
87393f26
DP
1704 ((string= s "{{") "}}")
1705 ((string= s "[[") "]]")))
dc184009 1706 (re-search-forward (concat "\\(.*\\)[ \t]*" s) (line-end-position) t))))
9c859203 1707
0b11ce59 1708(defun makefile-match-dependency (bound)
92984345 1709 "Search for `makefile-dependency-regex' up to BOUND.
92984345
DP
1710Checks that the colon has not already been fontified, else we
1711matched in a rule action."
1712 (catch 'found
0b11ce59 1713 (let ((pt (point)))
dfa89b5d 1714 (while (progn (skip-chars-forward makefile-dependency-skip bound)
774cd454 1715 (< (point) (or bound (point-max))))
0b11ce59 1716 (forward-char)
5c8b5442
DP
1717 (or (eq (char-after) ?=)
1718 (get-text-property (1- (point)) 'face)
602dc0da
DP
1719 (if (> (line-beginning-position) (+ (point-min) 2))
1720 (eq (char-before (line-end-position 0)) ?\\))
0b11ce59
DP
1721 (when (save-excursion
1722 (beginning-of-line)
1723 (looking-at makefile-dependency-regex))
dce16363
RS
1724 (save-excursion
1725 (let ((deps-end (match-end 1))
1726 (match-data (match-data)))
1727 (goto-char deps-end)
1728 (skip-chars-backward " \t")
1729 (setq deps-end (point))
1730 (beginning-of-line)
1731 (skip-chars-forward " \t")
1732 ;; Alter the bounds recorded for subexp 1,
1733 ;; which is what is supposed to match the targets.
1734 (setcar (nthcdr 2 match-data) (point))
1735 (setcar (nthcdr 3 match-data) deps-end)
1736 (store-match-data match-data)))
0b11ce59
DP
1737 (end-of-line)
1738 (throw 'found (point)))))
1739 (goto-char pt))
1740 nil))
92984345
DP
1741
1742(defun makefile-match-action (bound)
1743 (catch 'found
1744 (while (re-search-forward makefile-rule-action-regex bound t)
1745 (or (eq ?\\ (char-after (- (match-beginning 0) 2)))
1746 (throw 'found t)))))
1747
6783b1ce
RS
1748(defun makefile-do-macro-insertion (macro-name)
1749 "Insert a macro reference."
1750 (if (not (zerop (length macro-name)))
1751 (if (assoc macro-name makefile-runtime-macros-list)
1752 (insert "$" macro-name)
1753 (insert (makefile-format-macro-ref macro-name)))))
1754
9e9bf716 1755(defun makefile-remember-target (target-name &optional has-prereqs)
72c0ae01
ER
1756 "Remember a given target if it is not already remembered for this buffer."
1757 (if (not (zerop (length target-name)))
9e9bf716 1758 (progn
72c0ae01
ER
1759 (if (not (assoc target-name makefile-target-table))
1760 (setq makefile-target-table
9e9bf716
ER
1761 (cons (list target-name) makefile-target-table)))
1762 (if has-prereqs
1763 (setq makefile-has-prereqs
1764 (cons target-name makefile-has-prereqs))))))
72c0ae01
ER
1765
1766(defun makefile-remember-macro (macro-name)
1767 "Remember a given macro if it is not already remembered for this buffer."
1768 (if (not (zerop (length macro-name)))
1769 (if (not (assoc macro-name makefile-macro-table))
1770 (setq makefile-macro-table
1771 (cons (list macro-name) makefile-macro-table)))))
1772
1773(defun makefile-forward-after-target-colon ()
308a2860
RS
1774 "Move point forward after inserting the terminating colon of a target.
1775This acts according to the value of `makefile-tab-after-target-colon'."
72c0ae01
ER
1776 (if makefile-tab-after-target-colon
1777 (insert "\t")
1778 (insert " ")))
1779
1780(defun makefile-browser-on-macro-line-p ()
1781 "Determine if point is on a macro line in the browser."
1782 (save-excursion
1783 (beginning-of-line)
debe2de2 1784 (re-search-forward "\\$[{(]" (line-end-position) t)))
72c0ae01
ER
1785
1786(defun makefile-browser-this-line-target-name ()
1787 "Extract the target name from a line in the browser."
1788 (save-excursion
1789 (end-of-line)
1790 (skip-chars-backward "^ \t")
debe2de2 1791 (buffer-substring (point) (1- (line-end-position)))))
72c0ae01
ER
1792
1793(defun makefile-browser-this-line-macro-name ()
1794 "Extract the macro name from a line in the browser."
1795 (save-excursion
1796 (beginning-of-line)
debe2de2 1797 (re-search-forward "\\$[{(]" (line-end-position) t)
72c0ae01
ER
1798 (let ((macro-start (point)))
1799 (skip-chars-forward "^})")
1800 (buffer-substring macro-start (point)))))
1801
1802(defun makefile-format-macro-ref (macro-name)
308a2860
RS
1803 "Format a macro reference.
1804Uses `makefile-use-curly-braces-for-macros-p'."
b8ca7cc3
RS
1805 (if (or (char-equal ?\( (string-to-char macro-name))
1806 (char-equal ?\{ (string-to-char macro-name)))
1807 (format "$%s" macro-name)
1808 (if makefile-use-curly-braces-for-macros-p
1809 (format "${%s}" macro-name)
1810 (format "$(%s)" macro-name))))
72c0ae01
ER
1811
1812(defun makefile-browser-get-state-for-line (n)
1813 (aref makefile-browser-selection-vector (1- n)))
1814
1815(defun makefile-browser-set-state-for-line (n to-state)
1816 (aset makefile-browser-selection-vector (1- n) to-state))
1817
1818(defun makefile-browser-toggle-state-for-line (n)
1819 (makefile-browser-set-state-for-line n (not (makefile-browser-get-state-for-line n))))
1820
72c0ae01 1821(defun makefile-last-line-p ()
debe2de2 1822 (= (line-end-position) (point-max)))
72c0ae01
ER
1823
1824(defun makefile-first-line-p ()
debe2de2 1825 (= (line-beginning-position) (point-min)))
72c0ae01 1826
6783b1ce
RS
1827\f
1828
3b7f63b1 1829;;; Support for other packages, like add-log.
6783b1ce
RS
1830
1831(defun makefile-add-log-defun ()
32b558fa
RS
1832 "Return name of target or variable assignment that point is in.
1833If it isn't in one, return nil."
6783b1ce 1834 (save-excursion
32b558fa
RS
1835 (let (found)
1836 (beginning-of-line)
1837 ;; Scan back line by line, noticing when we come to a
1838 ;; variable or rule definition, and giving up when we see
1839 ;; a line that is not part of either of those.
e006b3cd
SM
1840 (while (not (or (setq found
1841 (when (or (looking-at makefile-macroassign-regex)
1842 (looking-at makefile-dependency-regex))
1843 (match-string-no-properties 1)))
1844 ;; Don't keep looking across a blank line or comment.
1845 (looking-at "$\\|#")
1846 (not (zerop (forward-line -1))))))
cac9ce0d
RS
1847 ;; Remove leading and trailing whitespace.
1848 (when found
1849 (setq found (replace-regexp-in-string "[ \t]+\\'" "" found))
1850 (setq found (replace-regexp-in-string "\\`[ \t]+" "" found)))
e006b3cd 1851 found)))
6783b1ce 1852
0017329b
SM
1853(provide 'make-mode)
1854
80e01c19 1855;;; make-mode.el ends here