(put 'makefile-space-face 'face-alias 'makefile-space)
[bpt/emacs.git] / lisp / progmodes / make-mode.el
CommitLineData
80e01c19 1;;; make-mode.el --- makefile editing commands for Emacs
72c0ae01 2
33f8eee3 3;; Copyright (C) 1992,94,99,2000,2001, 2002, 2003 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
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
7c938215 15;; the Free Software Foundation; either version 2, or (at your option)
72c0ae01
ER
16;; any later version.
17
18;; GNU Emacs is distributed in the hope that it will be useful,
19;; but WITHOUT ANY WARRANTY; without even the implied warranty of
20;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21;; GNU General Public License for more details.
22
23;; You should have received a copy of the GNU General Public License
b578f267
EN
24;; along with GNU Emacs; see the file COPYING. If not, write to the
25;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
26;; Boston, MA 02111-1307, USA.
72c0ae01 27
e41b2db1
ER
28;;; Commentary:
29
9e9bf716
ER
30;; A major mode for editing makefiles. The mode knows about Makefile
31;; syntax and defines M-n and M-p to move to next and previous productions.
32;;
33;; The keys $, =, : and . are electric; they try to help you fill in a
34;; macro reference, macro definition, ordinary target name, or special
35;; target name, respectively. Such names are completed using a list of
36;; targets and macro names parsed out of the makefile. This list is
37;; automatically updated, if necessary, whenever you invoke one of
38;; these commands. You can force it to be updated with C-c C-p.
39;;
6783b1ce 40;; The command C-c C-f adds certain filenames in the current directory
9e9bf716
ER
41;; as targets. You can filter out filenames by setting the variable
42;; makefile-ignored-files-in-pickup-regex.
43;;
44;; The command C-c C-u grinds for a bit, then pops up a report buffer
45;; showing which target names are up-to-date with respect to their
46;; prerequisites, which targets are out-of-date, and which have no
47;; prerequisites.
48;;
6783b1ce 49;; The command C-c C-b pops up a browser window listing all target and
10f518f7 50;; macro names. You can mark or unmark items with C-c SPC, and insert
9e9bf716
ER
51;; all marked items back in the Makefile with C-c TAB.
52;;
53;; The command C-c TAB in the makefile buffer inserts a GNU make builtin.
54;; You will be prompted for the builtin's args.
55;;
56;; There are numerous other customization variables.
e41b2db1 57
6783b1ce
RS
58;;
59;; To Do:
60;;
e008a4e5 61;; * Add missing doc strings, improve terse doc strings.
6783b1ce
RS
62;; * Eliminate electric stuff entirely.
63;; * It might be nice to highlight targets differently depending on
64;; whether they are up-to-date or not. Not sure how this would
65;; interact with font-lock.
66;; * Would be nice to edit the commands in ksh-mode and have
67;; indentation and slashification done automatically. Hard.
68;; * Consider removing browser mode. It seems useless.
69;; * ":" should notice when a new target is made and add it to the
70;; list (or at least set makefile-need-target-pickup).
308a2860 71;; * Make browser into a major mode.
6783b1ce
RS
72;; * Clean up macro insertion stuff. It is a mess.
73;; * Browser entry and exit is weird. Normalize.
74;; * Browser needs to be rewritten. Right now it is kind of a crock.
75;; Should at least:
76;; * Act more like dired/buffer menu/whatever.
77;; * Highlight as mouse traverses.
78;; * B2 inserts.
79;; * Update documentation above.
80;; * Update texinfo manual.
81;; * Update files.el.
82
83\f
84
72c0ae01
ER
85;;; Code:
86
6783b1ce
RS
87;; Sadly we need this for a macro.
88(eval-when-compile
9ffb4859
GM
89 (require 'imenu)
90 (require 'dabbrev)
91 (require 'add-log))
6783b1ce 92
72c0ae01 93;;; ------------------------------------------------------------
eb8c3be9 94;;; Configurable stuff
72c0ae01
ER
95;;; ------------------------------------------------------------
96
c098c7f1
RS
97(defgroup makefile nil
98 "Makefile editing commands for Emacs."
99 :group 'tools
100 :prefix "makefile-")
72c0ae01 101
602dc0da 102(defface makefile-space
9c859203
DP
103 '((((class color)) (:background "hotpink"))
104 (t (:reverse-video t)))
dc3fc797
RS
105 "Face to use for highlighting leading spaces in Font-Lock mode."
106 :group 'faces
a27b41ab 107 :group 'makefile)
a4bf534f 108(put 'makefile-space-face 'face-alias 'makefile-space)
dc3fc797 109
602dc0da 110(defface makefile-targets
9c859203 111 ;; This needs to go along both with foreground and background colors (i.e. shell)
602dc0da 112 '((t (:inherit font-lock-function-name-face)))
9c859203
DP
113 "Face to use for additionally highlighting rule targets in Font-Lock mode."
114 :group 'faces
30edba6e
DP
115 :group 'makefile
116 :version "22.1")
9c859203 117
602dc0da
DP
118(defface makefile-shell
119 ()
120 ;;'((((class color) (min-colors 88) (background light)) (:background "seashell1"))
121 ;; (((class color) (min-colors 88) (background dark)) (:background "seashell4")))
9c859203
DP
122 "Face to use for additionally highlighting Shell commands in Font-Lock mode."
123 :group 'faces
30edba6e
DP
124 :group 'makefile
125 :version "22.1")
9c859203 126
602dc0da 127(defface makefile-makepp-perl
9c859203
DP
128 '((((class color) (background light)) (:background "LightBlue1")) ; Camel Book
129 (((class color) (background dark)) (:background "DarkBlue"))
130 (t (:reverse-video t)))
131 "Face to use for additionally highlighting Perl code in Font-Lock mode."
132 :group 'faces
30edba6e
DP
133 :group 'makefile
134 :version "22.1")
9c859203 135
c098c7f1 136(defcustom makefile-browser-buffer-name "*Macros and Targets*"
9ffb4859 137 "*Name of the macro- and target browser buffer."
c098c7f1
RS
138 :type 'string
139 :group 'makefile)
140
141(defcustom makefile-target-colon ":"
9ffb4859 142 "*String to append to all target names inserted by `makefile-insert-target'.
c098c7f1
RS
143\":\" or \"::\" are common values."
144 :type 'string
145 :group 'makefile)
72c0ae01 146
c098c7f1 147(defcustom makefile-macro-assign " = "
9ffb4859 148 "*String to append to all macro names inserted by `makefile-insert-macro'.
72c0ae01 149The normal value should be \" = \", since this is what
a564ebfc 150standard make expects. However, newer makes such as dmake
72c0ae01 151allow a larger variety of different macro assignments, so you
c098c7f1
RS
152might prefer to use \" += \" or \" := \" ."
153 :type 'string
154 :group 'makefile)
72c0ae01 155
c098c7f1 156(defcustom makefile-electric-keys nil
9ffb4859 157 "*If non-nil, Makefile mode should install electric keybindings.
c098c7f1
RS
158Default is nil."
159 :type 'boolean
160 :group 'makefile)
6783b1ce 161
c098c7f1 162(defcustom makefile-use-curly-braces-for-macros-p nil
9ffb4859 163 "*Controls the style of generated macro references.
c098c7f1
RS
164Non-nil means macro references should use curly braces, like `${this}'.
165nil means use parentheses, like `$(this)'."
166 :type 'boolean
167 :group 'makefile)
72c0ae01 168
c098c7f1 169(defcustom makefile-tab-after-target-colon t
9ffb4859 170 "*If non-nil, insert a TAB after a target colon.
308a2860 171Otherwise, a space is inserted.
c098c7f1
RS
172The default is t."
173 :type 'boolean
174 :group 'makefile)
175
176(defcustom makefile-browser-leftmost-column 10
9ffb4859 177 "*Number of blanks to the left of the browser selection mark."
c098c7f1
RS
178 :type 'integer
179 :group 'makefile)
180
181(defcustom makefile-browser-cursor-column 10
9ffb4859 182 "*Column the cursor goes to when it moves up or down in the Makefile browser."
c098c7f1
RS
183 :type 'integer
184 :group 'makefile)
185
186(defcustom makefile-backslash-column 48
187 "*Column in which `makefile-backslash-region' inserts backslashes."
188 :type 'integer
189 :group 'makefile)
190
191(defcustom makefile-backslash-align t
9ffb4859 192 "*If non-nil, `makefile-backslash-region' will align backslashes."
c098c7f1
RS
193 :type 'boolean
194 :group 'makefile)
195
196(defcustom makefile-browser-selected-mark "+ "
9ffb4859 197 "*String used to mark selected entries in the Makefile browser."
c098c7f1
RS
198 :type 'string
199 :group 'makefile)
200
201(defcustom makefile-browser-unselected-mark " "
9ffb4859 202 "*String used to mark unselected entries in the Makefile browser."
c098c7f1
RS
203 :type 'string
204 :group 'makefile)
205
206(defcustom makefile-browser-auto-advance-after-selection-p t
9ffb4859 207 "*If non-nil, cursor will move after item is selected in Makefile browser."
c098c7f1
RS
208 :type 'boolean
209 :group 'makefile)
210
211(defcustom makefile-pickup-everything-picks-up-filenames-p nil
9ffb4859 212 "*If non-nil, `makefile-pickup-everything' picks up filenames as targets.
b20a44dc 213This means it calls `makefile-pickup-filenames-as-targets'.
c098c7f1
RS
214Otherwise filenames are omitted."
215 :type 'boolean
216 :group 'makefile)
72c0ae01 217
a11e2800 218(defcustom makefile-cleanup-continuations nil
9ffb4859 219 "*If non-nil, automatically clean up continuation lines when saving.
308a2860
RS
220A line is cleaned up by removing all whitespace following a trailing
221backslash. This is done silently.
a564ebfc 222IMPORTANT: Please note that enabling this option causes Makefile mode
c098c7f1
RS
223to MODIFY A FILE WITHOUT YOUR CONFIRMATION when \"it seems necessary\"."
224 :type 'boolean
225 :group 'makefile)
72c0ae01 226
66c8215f 227(defcustom makefile-mode-hook nil
9ffb4859 228 "*Normal hook run by `makefile-mode'."
66c8215f
DL
229 :type 'hook
230 :group 'makefile)
231
308a2860 232(defvar makefile-browser-hook '())
72c0ae01
ER
233
234;;
235;; Special targets for DMake, Sun's make ...
a1506d29 236;;
c098c7f1 237(defcustom makefile-special-targets-list
72c0ae01
ER
238 '(("DEFAULT") ("DONE") ("ERROR") ("EXPORT")
239 ("FAILED") ("GROUPEPILOG") ("GROUPPROLOG") ("IGNORE")
240 ("IMPORT") ("INCLUDE") ("INCLUDEDIRS") ("INIT")
241 ("KEEP_STATE") ("MAKEFILES") ("MAKE_VERSION") ("NO_PARALLEL")
242 ("PARALLEL") ("PHONY") ("PRECIOUS") ("REMOVE")
243 ("SCCS_GET") ("SILENT") ("SOURCE") ("SUFFIXES")
244 ("WAIT") ("c.o") ("C.o") ("m.o")
245 ("el.elc") ("y.c") ("s.o"))
9ffb4859 246 "*List of special targets.
308a2860 247You will be offered to complete on one of those in the minibuffer whenever
9ffb4859 248you enter a \".\" at the beginning of a line in `makefile-mode'."
c098c7f1
RS
249 :type '(repeat (list string))
250 :group 'makefile)
72c0ae01 251
c098c7f1 252(defcustom makefile-runtime-macros-list
1f35e359 253 '(("@") ("&") (">") ("<") ("*") ("^") ("+") ("?") ("%") ("$"))
9ffb4859 254 "*List of macros that are resolved by make at runtime.
a564ebfc
DL
255If you insert a macro reference using `makefile-insert-macro-ref', the name
256of the macro is checked against this list. If it can be found its name will
c098c7f1
RS
257not be enclosed in { } or ( )."
258 :type '(repeat (list string))
259 :group 'makefile)
72c0ae01 260
6783b1ce 261;; Note that the first big subexpression is used by font lock. Note
3b7f63b1
RS
262;; that if you change this regexp you might have to fix the imenu
263;; index in makefile-imenu-generic-expression.
92984345
DP
264(defvar makefile-dependency-regex
265 ;; Allow for two nested levels $(v1:$(v2:$(v3:a=b)=c)=d)
9391790a 266 "^\\(\\(?:\\$\\(?:[({]\\(?:\\$\\(?:[({]\\(?:\\$\\(?:[^({]\\|.[^\n$#})]+?[})]\\)\\|[^\n$#)}]\\)+?[})]\\|[^({]\\)\\|[^\n$#)}]\\)+?[})]\\|[^({]\\)\\|[^\n$#:=]\\)+?\\)\\(:\\)\\(?:[ \t]*$\\|[^=\n]\\(?:[^#\n]*?;[ \t]*\\(.+\\)\\)?\\)"
72c0ae01
ER
267 "Regex used to find dependency lines in a makefile.")
268
30edba6e
DP
269(defconst makefile-bsdmake-dependency-regex
270 (progn (string-match (regexp-quote "\\(:\\)") makefile-dependency-regex)
271 (replace-match "\\([:!]\\)" t t makefile-dependency-regex))
272 "Regex used to find dependency lines in a BSD makefile.")
273
0b11ce59
DP
274(defvar makefile-dependency-skip "^:"
275 "Characters to skip to find a line that might be a dependency.")
276
92984345 277(defvar makefile-rule-action-regex
adde7693 278 "^\t[ \t]*\\([-@]*\\)[ \t]*\\(\\(?:.*\\\\\n\\)*.*\\)"
9c859203
DP
279 "Regex used to highlight rule action lines in font lock mode.")
280
30edba6e
DP
281(defconst makefile-makepp-rule-action-regex
282 ;; Don't care about initial tab, but I don't know how to font-lock correctly without.
283 "^\t[ \t]*\\(\\(?:\\(?:noecho\\|ignore[-_]error\\|[-@]+\\)[ \t]*\\)*\\)\\(\\(&\\S +\\)?\\(?:.*\\\\\n\\)*.*\\)"
284 "Regex used to highlight makepp rule action lines in font lock mode.")
285
286(defconst makefile-bsdmake-rule-action-regex
287 (progn (string-match "-@" makefile-rule-action-regex)
288 (replace-match "-+@" t t makefile-rule-action-regex))
289 "Regex used to highlight BSD rule action lines in font lock mode.")
290
9c859203
DP
291;; Note that the first and second subexpression is used by font lock. Note
292;; that if you change this regexp you might have to fix the imenu index in
293;; makefile-imenu-generic-expression.
72c0ae01 294(defconst makefile-macroassign-regex
dce16363 295 "^ *\\([^ \n\t][^:#= \t\n]*\\)[ \t]*\\(?:!=\\|[*:+]?[:?]?=\\)"
72c0ae01
ER
296 "Regex used to find macro assignment lines in a makefile.")
297
9c859203 298(defconst makefile-var-use-regex
92984345 299 "[^$]\\$[({]\\([-a-zA-Z0-9_.]+\\|[@%<?^+*][FD]?\\)"
9c859203
DP
300 "Regex used to find $(macro) uses in a makefile.")
301
72c0ae01 302(defconst makefile-ignored-files-in-pickup-regex
0c5b5a13 303 "\\(^\\..*\\)\\|\\(.*~$\\)\\|\\(.*,v$\\)\\|\\(\\.[chy]\\)"
72c0ae01
ER
304 "Regex for filenames that will NOT be included in the target list.")
305
5f55e159 306(if (fboundp 'facemenu-unlisted-faces)
602dc0da
DP
307 (add-to-list 'facemenu-unlisted-faces 'makefile-space))
308(defvar makefile-space 'makefile-space
118a9857 309 "Face to use for highlighting leading spaces in Font-Lock mode.")
308a2860 310
9c859203
DP
311;; These lists were inspired by the old solution. But they are silly, because
312;; you can't differentiate what follows. They need to be split up.
313(defconst makefile-statements '("include")
314 "List of keywords understood by standard make.")
315
316(defconst makefile-automake-statements
317 `("if" "else" "endif" ,@makefile-statements)
318 "List of keywords understood by automake.")
319
320(defconst makefile-gmake-statements
321 `("-sinclude" "sinclude" "override" "vpath"
322 "ifdef" "ifndef" "ifeq" "ifneq" "-include" "define" "endef" "export"
323 "unexport"
324 ,@(cdr makefile-automake-statements))
325 "List of keywords understood by gmake.")
326
327;; These are even more silly, because you can have more spaces in between.
328(defconst makefile-makepp-statements
329 `("and ifdef" "and ifndef" "and ifeq" "and ifneq" "and ifperl"
330 "and ifmakeperl" "and ifsys" "and ifnsys" "build_cache" "build_check"
331 "else ifdef" "else ifndef" "else ifeq" "else ifneq" "else ifperl"
332 "else ifmakeperl" "else ifsys" "else ifnsys" "enddef" "load_makefile"
333 "ifperl" "ifmakeperl" "ifsys" "ifnsys" "_include" "makeperl" "makesub"
334 "no_implicit_load" "perl" "perl-begin" "perl_begin" "perl-end" "perl_end"
335 "prebuild" "or ifdef" "or ifndef" "or ifeq" "or ifneq" "or ifperl"
336 "or ifmakeperl" "or ifsys" "or ifnsys" "register_command_parser"
337 "register_scanner" "repository" "runtime" "signature" "sub"
338 ,@(nthcdr 4 makefile-gmake-statements))
339 "List of keywords understood by gmake.")
340
341(defconst makefile-bsdmake-statements
342 `(".elif" ".elifdef" ".elifmake" ".elifndef" ".elifnmake" ".else" ".endfor"
343 ".endif" ".for" ".if" ".ifdef" ".ifmake" ".ifndef" ".ifnmake" ".undef")
344 "List of keywords understood by BSD make.")
345
92984345
DP
346(defun makefile-make-font-lock-keywords (var keywords space
347 &optional negation
348 &rest font-lock-keywords)
9c859203
DP
349 `(;; Do macro assignments. These get the "variable-name" face.
350 (,makefile-macroassign-regex
351 (1 font-lock-variable-name-face)
352 ;; This is for after !=
602dc0da 353 (2 'makefile-shell prepend t)
8ade3c25
DP
354 ;; This is for after normal assignment
355 (3 'font-lock-string-face prepend t))
9c859203 356
9c859203 357 ;; Rule actions.
92984345 358 (makefile-match-action
9c859203 359 (1 font-lock-type-face)
602dc0da 360 (2 'makefile-shell prepend)
9c859203
DP
361 ;; Only makepp has builtin commands.
362 (3 font-lock-builtin-face prepend t))
363
364 ;; Variable references even in targets/strings/comments.
365 (,var 1 font-lock-variable-name-face prepend)
366
367 ;; Automatic variable references and single character variable references,
368 ;; but not shell variables references.
369 ("[^$]\\$\\([@%<?^+*_]\\|[a-zA-Z0-9]\\>\\)"
370 1 font-lock-constant-face prepend)
371 ("[^$]\\(\\$[@%*]\\)"
602dc0da 372 1 'makefile-targets append)
9ffb4859 373
9c859203 374 ;; Fontify conditionals and includes.
9c859203
DP
375 (,(concat "^\\(?: [ \t]*\\)?"
376 (regexp-opt keywords t)
377 "\\>[ \t]*\\([^: \t\n#]*\\)")
378 (1 font-lock-keyword-face) (2 font-lock-variable-name-face))
9ffb4859 379
92984345 380 ,@(if negation
5c8b5442
DP
381 `((,negation (1 font-lock-negation-char-face prepend)
382 (2 font-lock-negation-char-face prepend t))))
9ffb4859 383
9c859203
DP
384 ,@(if space
385 '(;; Highlight lines that contain just whitespace.
386 ;; They can cause trouble, especially if they start with a tab.
602dc0da 387 ("^[ \t]+$" . makefile-space)
61bc75ab 388
9c859203
DP
389 ;; Highlight shell comments that Make treats as commands,
390 ;; since these can fool people.
602dc0da 391 ("^\t+#" 0 makefile-space t)
ba080a3c 392
9c859203
DP
393 ;; Highlight spaces that precede tabs.
394 ;; They can make a tab fail to be effective.
602dc0da 395 ("^\\( +\\)\t" 1 makefile-space)))
10f518f7 396
92984345
DP
397 ,@font-lock-keywords
398
399 ;; Do dependencies.
400 (makefile-match-dependency
602dc0da
DP
401 (1 'makefile-targets prepend)
402 (3 'makefile-shell prepend t))))
bd0e4eb1 403
9c859203
DP
404(defconst makefile-font-lock-keywords
405 (makefile-make-font-lock-keywords
9c859203
DP
406 makefile-var-use-regex
407 makefile-statements
408 t))
409
410(defconst makefile-automake-font-lock-keywords
411 (makefile-make-font-lock-keywords
9c859203
DP
412 makefile-var-use-regex
413 makefile-automake-statements
414 t))
415
416(defconst makefile-gmake-font-lock-keywords
417 (makefile-make-font-lock-keywords
9c859203
DP
418 makefile-var-use-regex
419 makefile-gmake-statements
420 t
421 "^\\(?: [ \t]*\\)?if\\(n\\)\\(?:def\\|eq\\)\\>"
422
423 '("[^$]\\(\\$[({][@%*][DF][})]\\)"
602dc0da 424 1 'makefile-targets append)
9c859203
DP
425
426 ;; $(function ...) ${function ...}
92984345
DP
427 '("[^$]\\$[({]\\([-a-zA-Z0-9_.]+\\s \\)"
428 1 font-lock-function-name-face prepend)
9c859203
DP
429
430 ;; $(shell ...) ${shell ...}
431 '("[^$]\\$\\([({]\\)shell[ \t]+"
432 makefile-match-function-end nil nil
602dc0da 433 (1 'makefile-shell prepend t))))
9c859203
DP
434
435(defconst makefile-makepp-font-lock-keywords
436 (makefile-make-font-lock-keywords
9c859203
DP
437 makefile-var-use-regex
438 makefile-makepp-statements
439 nil
440 "^\\(?: [ \t]*\\)?\\(?:and[ \t]+\\|else[ \t]+\\|or[ \t]+\\)?if\\(n\\)\\(?:def\\|eq\\|sys\\)\\>"
441
adde7693 442 '("[^$]\\(\\$[({]\\(?:output\\|stem\\|target\\)s?\\_>.*?[})]\\)"
602dc0da 443 1 'makefile-targets append)
9c859203
DP
444
445 ;; Colon modifier keywords.
92984345
DP
446 '("\\(:\\s *\\)\\(build_c\\(?:ache\\|heck\\)\\|env\\(?:ironment\\)?\\|foreach\\|signature\\|scanner\\|quickscan\\|smartscan\\)\\>\\([^:\n]*\\)"
447 (1 font-lock-type-face t)
448 (2 font-lock-keyword-face t)
449 (3 font-lock-variable-name-face t))
9c859203
DP
450
451 ;; $(function ...) $((function ...)) ${function ...} ${{function ...}}
92984345
DP
452 '("[^$]\\$\\(?:((?\\|{{?\\)\\([-a-zA-Z0-9_.]+\\s \\)"
453 1 font-lock-function-name-face prepend)
9c859203
DP
454
455 ;; $(shell ...) $((shell ...)) ${shell ...} ${{shell ...}}
456 '("[^$]\\$\\(((?\\|{{?\\)shell\\(?:[-_]\\(?:global[-_]\\)?once\\)?[ \t]+"
457 makefile-match-function-end nil nil
602dc0da 458 (1 'makefile-shell prepend t))
9c859203
DP
459
460 ;; $(perl ...) $((perl ...)) ${perl ...} ${{perl ...}}
461 '("[^$]\\$\\(((?\\|{{?\\)makeperl[ \t]+"
462 makefile-match-function-end nil nil
602dc0da 463 (1 'makefile-makepp-perl prepend t))
9c859203
DP
464 '("[^$]\\$\\(((?\\|{{?\\)perl[ \t]+"
465 makefile-match-function-end nil nil
602dc0da 466 (1 'makefile-makepp-perl t t))
9c859203
DP
467
468 ;; Can we unify these with (if (match-end 1) 'prepend t)?
602dc0da
DP
469 '("ifmakeperl\\s +\\(.*\\)" 1 'makefile-makepp-perl prepend)
470 '("ifperl\\s +\\(.*\\)" 1 'makefile-makepp-perl t)
9c859203
DP
471
472 ;; Perl block single- or multiline, as statement or rule action.
473 ;; Don't know why the initial newline in 2nd variant of group 2 doesn't get skipped.
474 '("\\<make\\(?:perl\\|sub\\s +\\S +\\)\\s *\n?\\s *{\\(?:{\\s *\n?\\(\\(?:.*\n\\)+?\\)\\s *}\\|\\s *\\(\\(?:.*?\\|\n?\\(?:.*\n\\)+?\\)\\)\\)}"
602dc0da
DP
475 (1 'makefile-makepp-perl prepend t)
476 (2 'makefile-makepp-perl prepend t))
9c859203 477 '("\\<\\(?:perl\\|sub\\s +\\S +\\)\\s *\n?\\s *{\\(?:{\\s *\n?\\(\\(?:.*\n\\)+?\\)\\s *}\\|\\s *\\(\\(?:.*?\\|\n?\\(?:.*\n\\)+?\\)\\)\\)}"
602dc0da
DP
478 (1 'makefile-makepp-perl t t)
479 (2 'makefile-makepp-perl t t))
9c859203
DP
480
481 ;; Statement style perl block.
482 '("perl[-_]begin\\s *\\(?:\\s #.*\\)?\n\\(\\(?:.*\n\\)+?\\)\\s *perl[-_]end\\>"
602dc0da 483 1 'makefile-makepp-perl t)))
9c859203 484
9c859203
DP
485(defconst makefile-bsdmake-font-lock-keywords
486 (makefile-make-font-lock-keywords
92984345 487 ;; A lot more could be done for variables here:
9c859203
DP
488 makefile-var-use-regex
489 makefile-bsdmake-statements
490 t
491 "^\\(?: [ \t]*\\)?\\.\\(?:el\\)?if\\(n?\\)\\(?:def\\|make\\)?\\>[ \t]*\\(!?\\)"
492 '("^[ \t]*\\.for[ \t].+[ \t]\\(in\\)\\>" 1 font-lock-keyword-face)))
f067044f 493
6783b1ce 494
0b51ba8a 495(defconst makefile-font-lock-syntactic-keywords
310ee6ad
SM
496 ;; From sh-script.el.
497 ;; A `#' begins a comment in sh when it is unquoted and at the beginning
498 ;; of a word. In the shell, words are separated by metacharacters.
499 ;; The list of special chars is taken from the single-unix spec of the
500 ;; shell command language (under `quoting') but with `$' removed.
501 '(("[^|&;<>()`\\\"' \t\n]\\(#+\\)" 1 "_")
502 ;; Change the syntax of a quoted newline so that it does not end a comment.
503 ("\\\\\n" 0 ".")))
0b51ba8a 504
db85e69c 505(defvar makefile-imenu-generic-expression
774cd454 506 `(("Dependencies" makefile-previous-dependency 1)
92984345 507 ("Macro Assignment" ,makefile-macroassign-regex 1))
a564ebfc 508 "Imenu generic expression for Makefile mode. See `imenu-generic-expression'.")
db85e69c 509
72c0ae01
ER
510;;; ------------------------------------------------------------
511;;; The following configurable variables are used in the
512;;; up-to-date overview .
eb8c3be9 513;;; The standard configuration assumes that your `make' program
72c0ae01
ER
514;;; can be run in question/query mode using the `-q' option, this
515;;; means that the command
516;;;
517;;; make -q foo
518;;;
519;;; should return an exit status of zero if the target `foo' is
520;;; up to date and a nonzero exit status otherwise.
521;;; Many makes can do this although the docs/manpages do not mention
9e9bf716
ER
522;;; it. Try it with your favourite one. GNU make, System V make, and
523;;; Dennis Vadura's DMake have no problems.
72c0ae01
ER
524;;; Set the variable `makefile-brave-make' to the name of the
525;;; make utility that does this on your system.
eb8c3be9 526;;; To understand what this is all about see the function definition
72c0ae01
ER
527;;; of `makefile-query-by-make-minus-q' .
528;;; ------------------------------------------------------------
529
c098c7f1 530(defcustom makefile-brave-make "make"
9ffb4859 531 "*How to invoke make, for `makefile-query-targets'.
c098c7f1
RS
532This should identify a `make' command that can handle the `-q' option."
533 :type 'string
534 :group 'makefile)
72c0ae01 535
c098c7f1 536(defcustom makefile-query-one-target-method 'makefile-query-by-make-minus-q
9ffb4859 537 "*Function to call to determine whether a make target is up to date.
bd0e4eb1 538The function must satisfy this calling convention:
72c0ae01
ER
539
540* As its first argument, it must accept the name of the target to
541 be checked, as a string.
542
543* As its second argument, it may accept the name of a makefile
a564ebfc 544 as a string. Depending on what you're going to do you may
72c0ae01
ER
545 not need this.
546
547* It must return the integer value 0 (zero) if the given target
548 should be considered up-to-date in the context of the given
c098c7f1
RS
549 makefile, any nonzero integer value otherwise."
550 :type 'function
551 :group 'makefile)
552
553(defcustom makefile-up-to-date-buffer-name "*Makefile Up-to-date overview*"
9ffb4859 554 "*Name of the Up-to-date overview buffer."
c098c7f1
RS
555 :type 'string
556 :group 'makefile)
72c0ae01 557
72c0ae01
ER
558;;; --- end of up-to-date-overview configuration ------------------
559
9ffb4859
GM
560(defvar makefile-mode-abbrev-table nil
561 "Abbrev table in use in Makefile buffers.")
562(if makefile-mode-abbrev-table
563 ()
564 (define-abbrev-table 'makefile-mode-abbrev-table ()))
565
9c859203
DP
566(defvar makefile-mode-map
567 (let ((map (make-sparse-keymap)))
568 ;; set up the keymap
569 (define-key map "\C-c:" 'makefile-insert-target-ref)
570 (if makefile-electric-keys
571 (progn
572 (define-key map "$" 'makefile-insert-macro-ref)
573 (define-key map ":" 'makefile-electric-colon)
574 (define-key map "=" 'makefile-electric-equal)
575 (define-key map "." 'makefile-electric-dot)))
576 (define-key map "\C-c\C-f" 'makefile-pickup-filenames-as-targets)
577 (define-key map "\C-c\C-b" 'makefile-switch-to-browser)
578 (define-key map "\C-c\C-c" 'comment-region)
579 (define-key map "\C-c\C-p" 'makefile-pickup-everything)
580 (define-key map "\C-c\C-u" 'makefile-create-up-to-date-overview)
581 (define-key map "\C-c\C-i" 'makefile-insert-gmake-function)
582 (define-key map "\C-c\C-\\" 'makefile-backslash-region)
583 (define-key map "\C-c\C-m\C-a" 'makefile-automake-mode)
584 (define-key map "\C-c\C-m\C-b" 'makefile-bsdmake-mode)
585 (define-key map "\C-c\C-m\C-g" 'makefile-gmake-mode)
586 (define-key map "\C-c\C-m\C-m" 'makefile-mode)
587 (define-key map "\C-c\C-m\C-p" 'makefile-makepp-mode)
588 (define-key map "\M-p" 'makefile-previous-dependency)
589 (define-key map "\M-n" 'makefile-next-dependency)
590 (define-key map "\e\t" 'makefile-complete)
591
592 ;; Make menus.
593 (define-key map [menu-bar makefile-mode]
594 (cons "Makefile" (make-sparse-keymap "Makefile")))
595
596 (define-key map [menu-bar makefile-mode browse]
597 '("Pop up Makefile Browser" . makefile-switch-to-browser))
598 (define-key map [menu-bar makefile-mode complete]
599 '("Complete Target or Macro" . makefile-complete))
600 (define-key map [menu-bar makefile-mode pickup]
601 '("Find Targets and Macros" . makefile-pickup-everything))
602
603 (define-key map [menu-bar makefile-mode prev]
604 '("Move to Previous Dependency" . makefile-previous-dependency))
605 (define-key map [menu-bar makefile-mode next]
606 '("Move to Next Dependency" . makefile-next-dependency))
607 map)
bd0e4eb1
RS
608 "The keymap that is used in Makefile mode.")
609
72c0ae01
ER
610(defvar makefile-browser-map nil
611 "The keymap that is used in the macro- and target browser.")
612(if makefile-browser-map
613 ()
614 (setq makefile-browser-map (make-sparse-keymap))
615 (define-key makefile-browser-map "n" 'makefile-browser-next-line)
a564ebfc 616 (define-key makefile-browser-map "\C-n" 'makefile-browser-next-line)
72c0ae01
ER
617 (define-key makefile-browser-map "p" 'makefile-browser-previous-line)
618 (define-key makefile-browser-map "\C-p" 'makefile-browser-previous-line)
619 (define-key makefile-browser-map " " 'makefile-browser-toggle)
620 (define-key makefile-browser-map "i" 'makefile-browser-insert-selection)
a564ebfc 621 (define-key makefile-browser-map "I" 'makefile-browser-insert-selection-and-quit)
72c0ae01
ER
622 (define-key makefile-browser-map "\C-c\C-m" 'makefile-browser-insert-continuation)
623 (define-key makefile-browser-map "q" 'makefile-browser-quit)
624 ;; disable horizontal movement
625 (define-key makefile-browser-map "\C-b" 'undefined)
a564ebfc 626 (define-key makefile-browser-map "\C-f" 'undefined))
72c0ae01
ER
627
628
308a2860 629(defvar makefile-mode-syntax-table nil)
72c0ae01
ER
630(if makefile-mode-syntax-table
631 ()
632 (setq makefile-mode-syntax-table (make-syntax-table))
633 (modify-syntax-entry ?\( "() " makefile-mode-syntax-table)
634 (modify-syntax-entry ?\) ")( " makefile-mode-syntax-table)
635 (modify-syntax-entry ?\[ "(] " makefile-mode-syntax-table)
6a99a3ea 636 (modify-syntax-entry ?\] ")[ " makefile-mode-syntax-table)
a564ebfc 637 (modify-syntax-entry ?\{ "(} " makefile-mode-syntax-table)
72c0ae01 638 (modify-syntax-entry ?\} "){ " makefile-mode-syntax-table)
af4b1991
SM
639 (modify-syntax-entry ?\' "\" " makefile-mode-syntax-table)
640 (modify-syntax-entry ?\` "\" " makefile-mode-syntax-table)
72c0ae01
ER
641 (modify-syntax-entry ?# "< " makefile-mode-syntax-table)
642 (modify-syntax-entry ?\n "> " makefile-mode-syntax-table))
6783b1ce
RS
643
644
72c0ae01
ER
645;;; ------------------------------------------------------------
646;;; Internal variables.
647;;; You don't need to configure below this line.
648;;; ------------------------------------------------------------
649
650(defvar makefile-target-table nil
308a2860 651 "Table of all target names known for this buffer.")
72c0ae01
ER
652
653(defvar makefile-macro-table nil
308a2860 654 "Table of all macro names known for this buffer.")
72c0ae01
ER
655
656(defvar makefile-browser-client
bd0e4eb1 657 "A buffer in Makefile mode that is currently using the browser.")
72c0ae01
ER
658
659(defvar makefile-browser-selection-vector nil)
9e9bf716
ER
660(defvar makefile-has-prereqs nil)
661(defvar makefile-need-target-pickup t)
662(defvar makefile-need-macro-pickup t)
72c0ae01
ER
663
664(defvar makefile-mode-hook '())
665
308a2860
RS
666;; Each element looks like '("GNU MAKE FUNCTION" "ARG" "ARG" ... )
667;; Each "ARG" is used as a prompt for a required argument.
72c0ae01 668(defconst makefile-gnumake-functions-alist
72c0ae01
ER
669 '(
670 ;; Text functions
671 ("subst" "From" "To" "In")
672 ("patsubst" "Pattern" "Replacement" "In")
673 ("strip" "Text")
674 ("findstring" "Find what" "In")
675 ("filter" "Pattern" "Text")
676 ("filter-out" "Pattern" "Text")
677 ("sort" "List")
678 ;; Filename functions
679 ("dir" "Names")
680 ("notdir" "Names")
681 ("suffix" "Names")
682 ("basename" "Names")
e1775344 683 ("addprefix" "Prefix" "Names")
72c0ae01
ER
684 ("addsuffix" "Suffix" "Names")
685 ("join" "List 1" "List 2")
686 ("word" "Index" "Text")
687 ("words" "Text")
688 ("firstword" "Text")
689 ("wildcard" "Pattern")
690 ;; Misc functions
691 ("foreach" "Variable" "List" "Text")
692 ("origin" "Variable")
308a2860 693 ("shell" "Command")))
72c0ae01
ER
694
695
696;;; ------------------------------------------------------------
697;;; The mode function itself.
698;;; ------------------------------------------------------------
699
5c2946c7 700;;;###autoload
72c0ae01 701(defun makefile-mode ()
9c859203
DP
702 "Major mode for editing standard Makefiles.
703
704If you are editing a file for a different make, try one of the
705variants `makefile-automake-mode', `makefile-gmake-mode',
706`makefile-makepp-mode' or `makefile-bsdmake-mode'. All but the
707last should be correctly chosen based on the file name, except if
708it is *.mk. This function ends by invoking the function(s)
709`makefile-mode-hook'.
72c0ae01 710
92984345
DP
711It is strongly recommended to use `font-lock-mode', because that
712provides additional parsing information. This is used for
713example to see that a rule action `echo foo: bar' is a not rule
714dependency, despite the colon.
715
72c0ae01
ER
716\\{makefile-mode-map}
717
718In the browser, use the following keys:
719
720\\{makefile-browser-map}
721
bd0e4eb1 722Makefile mode can be configured by modifying the following variables:
72c0ae01 723
debe2de2 724`makefile-browser-buffer-name':
72c0ae01
ER
725 Name of the macro- and target browser buffer.
726
debe2de2 727`makefile-target-colon':
72c0ae01 728 The string that gets appended to all target names
bd0e4eb1 729 inserted by `makefile-insert-target'.
72c0ae01
ER
730 \":\" or \"::\" are quite common values.
731
debe2de2 732`makefile-macro-assign':
72c0ae01 733 The string that gets appended to all macro names
bd0e4eb1 734 inserted by `makefile-insert-macro'.
72c0ae01 735 The normal value should be \" = \", since this is what
a564ebfc 736 standard make expects. However, newer makes such as dmake
72c0ae01
ER
737 allow a larger variety of different macro assignments, so you
738 might prefer to use \" += \" or \" := \" .
739
debe2de2 740`makefile-tab-after-target-colon':
72c0ae01
ER
741 If you want a TAB (instead of a space) to be appended after the
742 target colon, then set this to a non-nil value.
743
debe2de2 744`makefile-browser-leftmost-column':
72c0ae01
ER
745 Number of blanks to the left of the browser selection mark.
746
debe2de2 747`makefile-browser-cursor-column':
72c0ae01
ER
748 Column in which the cursor is positioned when it moves
749 up or down in the browser.
750
debe2de2 751`makefile-browser-selected-mark':
72c0ae01
ER
752 String used to mark selected entries in the browser.
753
debe2de2 754`makefile-browser-unselected-mark':
72c0ae01
ER
755 String used to mark unselected entries in the browser.
756
debe2de2 757`makefile-browser-auto-advance-after-selection-p':
72c0ae01
ER
758 If this variable is set to a non-nil value the cursor
759 will automagically advance to the next line after an item
760 has been selected in the browser.
761
debe2de2 762`makefile-pickup-everything-picks-up-filenames-p':
72c0ae01 763 If this variable is set to a non-nil value then
bd0e4eb1 764 `makefile-pickup-everything' also picks up filenames as targets
b20a44dc 765 (i.e. it calls `makefile-pickup-filenames-as-targets'), otherwise
72c0ae01
ER
766 filenames are omitted.
767
e006b3cd 768`makefile-cleanup-continuations':
a564ebfc 769 If this variable is set to a non-nil value then Makefile mode
72c0ae01
ER
770 will assure that no line in the file ends with a backslash
771 (the continuation character) followed by any whitespace.
772 This is done by silently removing the trailing whitespace, leaving
773 the backslash itself intact.
a564ebfc 774 IMPORTANT: Please note that enabling this option causes Makefile mode
bd0e4eb1 775 to MODIFY A FILE WITHOUT YOUR CONFIRMATION when \"it seems necessary\".
72c0ae01 776
debe2de2 777`makefile-browser-hook':
72c0ae01 778 A function or list of functions to be called just before the
9e9bf716 779 browser is entered. This is executed in the makefile buffer.
72c0ae01 780
debe2de2 781`makefile-special-targets-list':
72c0ae01 782 List of special targets. You will be offered to complete
bd0e4eb1
RS
783 on one of those in the minibuffer whenever you enter a `.'.
784 at the beginning of a line in Makefile mode."
6783b1ce 785
72c0ae01
ER
786 (interactive)
787 (kill-all-local-variables)
e006b3cd
SM
788 (add-hook 'write-file-functions
789 'makefile-warn-suspicious-lines nil t)
a11e2800
RS
790 (add-hook 'write-file-functions
791 'makefile-warn-continuations nil t)
e006b3cd
SM
792 (add-hook 'write-file-functions
793 'makefile-cleanup-continuations nil t)
b8ca7cc3
RS
794 (make-local-variable 'makefile-target-table)
795 (make-local-variable 'makefile-macro-table)
796 (make-local-variable 'makefile-has-prereqs)
797 (make-local-variable 'makefile-need-target-pickup)
798 (make-local-variable 'makefile-need-macro-pickup)
6783b1ce
RS
799
800 ;; Font lock.
af4b1991 801 (make-local-variable 'font-lock-defaults)
c7ca58df
EZ
802 (setq font-lock-defaults
803 ;; SYNTAX-BEGIN set to backward-paragraph to avoid slow-down
804 ;; near the end of a large buffer, due to parse-partial-sexp's
805 ;; trying to parse all the way till the beginning of buffer.
0b51ba8a
JB
806 '(makefile-font-lock-keywords
807 nil nil
808 ((?$ . "."))
809 backward-paragraph
8ade3c25
DP
810 (font-lock-syntactic-keywords . makefile-font-lock-syntactic-keywords)
811 (font-lock-support-mode))) ; JIT breaks on long series of continuation lines.
6783b1ce
RS
812
813 ;; Add-log.
814 (make-local-variable 'add-log-current-defun-function)
815 (setq add-log-current-defun-function 'makefile-add-log-defun)
816
817 ;; Imenu.
db85e69c
RS
818 (make-local-variable 'imenu-generic-expression)
819 (setq imenu-generic-expression makefile-imenu-generic-expression)
6783b1ce 820
3b114205
RS
821 ;; Dabbrev.
822 (make-local-variable 'dabbrev-abbrev-skip-leading-regexp)
823 (setq dabbrev-abbrev-skip-leading-regexp "\\$")
824
9ffb4859
GM
825 ;; Other abbrevs.
826 (setq local-abbrev-table makefile-mode-abbrev-table)
827
dfa9e6fc
RS
828 ;; Filling.
829 (make-local-variable 'fill-paragraph-function)
830 (setq fill-paragraph-function 'makefile-fill-paragraph)
831
6783b1ce 832 ;; Comment stuff.
531b2a28 833 (make-local-variable 'comment-start)
72c0ae01 834 (setq comment-start "#")
6783b1ce 835 (make-local-variable 'comment-end)
72c0ae01 836 (setq comment-end "")
6783b1ce
RS
837 (make-local-variable 'comment-start-skip)
838 (setq comment-start-skip "#+[ \t]*")
839
debe2de2
SM
840 ;; Make sure TAB really inserts \t.
841 (set (make-local-variable 'indent-line-function) 'indent-to-left-margin)
842
72c0ae01
ER
843 ;; become the current major mode
844 (setq major-mode 'makefile-mode)
6783b1ce
RS
845 (setq mode-name "Makefile")
846
847 ;; Activate keymap and syntax table.
72c0ae01
ER
848 (use-local-map makefile-mode-map)
849 (set-syntax-table makefile-mode-syntax-table)
6783b1ce
RS
850
851 ;; Real TABs are important in makefiles
852 (setq indent-tabs-mode t)
e1c45277 853 (run-mode-hooks 'makefile-mode-hook))
6783b1ce 854
9c859203
DP
855;; These should do more than just differentiate font-lock.
856;;;###autoload
857(define-derived-mode makefile-automake-mode makefile-mode "Makefile.am"
858 "An adapted `makefile-mode' that knows about automake."
859 (setq font-lock-defaults
860 `(makefile-automake-font-lock-keywords ,@(cdr font-lock-defaults))))
861
862;;;###autoload
863(define-derived-mode makefile-gmake-mode makefile-mode "GNUmakefile"
864 "An adapted `makefile-mode' that knows about gmake."
865 (setq font-lock-defaults
866 `(makefile-gmake-font-lock-keywords ,@(cdr font-lock-defaults))))
867
868;;;###autoload
869(define-derived-mode makefile-makepp-mode makefile-mode "Makeppfile"
870 "An adapted `makefile-mode' that knows about makepp."
30edba6e
DP
871 (set (make-local-variable 'makefile-rule-action-regex)
872 makefile-makepp-rule-action-regex)
9c859203 873 (setq font-lock-defaults
774cd454
DP
874 `(makefile-makepp-font-lock-keywords ,@(cdr font-lock-defaults))
875 imenu-generic-expression
876 `(("Functions" "^[ \t]*\\(?:make\\)?sub[ \t]+\\([A-Za-z0-9_]+\\)" 1)
877 ,@imenu-generic-expression)))
9c859203
DP
878
879;;;###autoload
880(define-derived-mode makefile-bsdmake-mode makefile-mode "BSDmakefile"
881 "An adapted `makefile-mode' that knows about BSD make."
92984345 882 (set (make-local-variable 'makefile-dependency-regex)
30edba6e 883 makefile-bsdmake-dependency-regex)
0b11ce59 884 (set (make-local-variable 'makefile-dependency-skip) "^:!")
92984345 885 (set (make-local-variable 'makefile-rule-action-regex)
30edba6e 886 makefile-bsdmake-rule-action-regex)
9c859203 887 (setq font-lock-defaults
774cd454 888 `(makefile-bsdmake-font-lock-keywords ,@(cdr font-lock-defaults))))
9c859203 889
6783b1ce
RS
890\f
891
892;;; Motion code.
72c0ae01 893
72c0ae01 894(defun makefile-next-dependency ()
bd0e4eb1 895 "Move point to the beginning of the next dependency line."
72c0ae01
ER
896 (interactive)
897 (let ((here (point)))
898 (end-of-line)
0b11ce59 899 (if (makefile-match-dependency nil)
72c0ae01
ER
900 (progn (beginning-of-line) t) ; indicate success
901 (goto-char here) nil)))
6783b1ce 902
72c0ae01 903(defun makefile-previous-dependency ()
bd0e4eb1 904 "Move point to the beginning of the previous dependency line."
72c0ae01 905 (interactive)
0b11ce59 906 (let ((pt (point)))
72c0ae01 907 (beginning-of-line)
0b11ce59
DP
908 ;; makefile-match-dependency done backwards:
909 (catch 'found
dfa89b5d
DP
910 (while (progn (skip-chars-backward makefile-dependency-skip)
911 (not (bobp)))
5c8b5442
DP
912 (or (prog1 (eq (char-after) ?=)
913 (backward-char))
914 (get-text-property (point) 'face)
0b11ce59 915 (beginning-of-line)
602dc0da
DP
916 (if (> (point) (+ (point-min) 2))
917 (eq (char-before (1- (point))) ?\\))
0b11ce59
DP
918 (if (looking-at makefile-dependency-regex)
919 (throw 'found t))))
920 (goto-char pt)
921 nil)))
72c0ae01 922
6783b1ce 923\f
72c0ae01 924
6783b1ce 925;;; Electric keys. Blech.
9e9bf716 926
6783b1ce
RS
927(defun makefile-electric-dot (arg)
928 "Prompt for the name of a special target to insert.
929Only does electric insertion at beginning of line.
930Anywhere else just self-inserts."
931 (interactive "p")
72c0ae01
ER
932 (if (bolp)
933 (makefile-insert-special-target)
6783b1ce 934 (self-insert-command arg)))
72c0ae01 935
72c0ae01 936(defun makefile-insert-special-target ()
ac282af8 937 "Prompt for and insert a special target name.
6783b1ce 938Uses `makefile-special-targets' list."
72c0ae01 939 (interactive)
9e9bf716 940 (makefile-pickup-targets)
6783b1ce
RS
941 (let ((special-target
942 (completing-read "Special target: "
943 makefile-special-targets-list nil nil nil)))
72c0ae01
ER
944 (if (zerop (length special-target))
945 ()
6783b1ce 946 (insert "." special-target ":")
72c0ae01
ER
947 (makefile-forward-after-target-colon))))
948
6783b1ce
RS
949(defun makefile-electric-equal (arg)
950 "Prompt for name of a macro to insert.
951Only does prompting if point is at beginning of line.
952Anywhere else just self-inserts."
953 (interactive "p")
9e9bf716 954 (makefile-pickup-macros)
72c0ae01
ER
955 (if (bolp)
956 (call-interactively 'makefile-insert-macro)
6783b1ce 957 (self-insert-command arg)))
72c0ae01
ER
958
959(defun makefile-insert-macro (macro-name)
960 "Prepare definition of a new macro."
961 (interactive "sMacro Name: ")
9e9bf716 962 (makefile-pickup-macros)
72c0ae01
ER
963 (if (not (zerop (length macro-name)))
964 (progn
965 (beginning-of-line)
6783b1ce 966 (insert macro-name makefile-macro-assign)
9e9bf716 967 (setq makefile-need-macro-pickup t)
72c0ae01
ER
968 (makefile-remember-macro macro-name))))
969
72c0ae01 970(defun makefile-insert-macro-ref (macro-name)
bd0e4eb1 971 "Complete on a list of known macros, then insert complete ref at point."
72c0ae01
ER
972 (interactive
973 (list
9e9bf716
ER
974 (progn
975 (makefile-pickup-macros)
976 (completing-read "Refer to macro: " makefile-macro-table nil nil nil))))
6783b1ce 977 (makefile-do-macro-insertion macro-name))
72c0ae01 978
72c0ae01
ER
979(defun makefile-insert-target (target-name)
980 "Prepare definition of a new target (dependency line)."
981 (interactive "sTarget: ")
982 (if (not (zerop (length target-name)))
983 (progn
984 (beginning-of-line)
6783b1ce 985 (insert target-name makefile-target-colon)
72c0ae01
ER
986 (makefile-forward-after-target-colon)
987 (end-of-line)
9e9bf716 988 (setq makefile-need-target-pickup t)
72c0ae01
ER
989 (makefile-remember-target target-name))))
990
72c0ae01 991(defun makefile-insert-target-ref (target-name)
a564ebfc 992 "Complete on a list of known targets, then insert TARGET-NAME at point."
72c0ae01
ER
993 (interactive
994 (list
0c5b5a13 995 (progn
9e9bf716
ER
996 (makefile-pickup-targets)
997 (completing-read "Refer to target: " makefile-target-table nil nil nil))))
72c0ae01 998 (if (not (zerop (length target-name)))
6783b1ce 999 (insert target-name " ")))
72c0ae01 1000
6783b1ce
RS
1001(defun makefile-electric-colon (arg)
1002 "Prompt for name of new target.
1003Prompting only happens at beginning of line.
1004Anywhere else just self-inserts."
1005 (interactive "p")
72c0ae01
ER
1006 (if (bolp)
1007 (call-interactively 'makefile-insert-target)
6783b1ce
RS
1008 (self-insert-command arg)))
1009
1010\f
72c0ae01 1011
72c0ae01
ER
1012;;; ------------------------------------------------------------
1013;;; Extracting targets and macros from an existing makefile
1014;;; ------------------------------------------------------------
1015
1016(defun makefile-pickup-targets ()
308a2860 1017 "Notice names of all target definitions in Makefile."
72c0ae01 1018 (interactive)
dfa89b5d
DP
1019 (when makefile-need-target-pickup
1020 (setq makefile-need-target-pickup nil
1021 makefile-target-table nil
1022 makefile-has-prereqs nil)
9e9bf716
ER
1023 (save-excursion
1024 (goto-char (point-min))
92984345 1025 (while (makefile-match-dependency nil)
dfa89b5d
DP
1026 (goto-char (match-beginning 1))
1027 (while (let ((target-name
1028 (buffer-substring-no-properties (point)
1029 (progn
1030 (skip-chars-forward "^ \t:#")
1031 (point))))
9e9bf716
ER
1032 (has-prereqs
1033 (not (looking-at ":[ \t]*$"))))
dfa89b5d
DP
1034 (if (makefile-remember-target target-name has-prereqs)
1035 (message "Picked up target \"%s\" from line %d"
1036 target-name (line-number-at-pos)))
1037 (skip-chars-forward " \t")
1038 (not (or (eolp) (eq (char-after) ?:)))))
1039 (forward-line)))
1040 (message "Read targets OK.")))
72c0ae01 1041
72c0ae01 1042(defun makefile-pickup-macros ()
308a2860 1043 "Notice names of all macro definitions in Makefile."
72c0ae01 1044 (interactive)
dfa89b5d
DP
1045 (when makefile-need-macro-pickup
1046 (setq makefile-need-macro-pickup nil
1047 makefile-macro-table nil)
9e9bf716
ER
1048 (save-excursion
1049 (goto-char (point-min))
e006b3cd 1050 (while (re-search-forward makefile-macroassign-regex nil t)
dfa89b5d
DP
1051 (goto-char (match-beginning 1))
1052 (let ((macro-name (buffer-substring-no-properties (point)
1053 (progn
1054 (skip-chars-forward "^ \t:#=*")
1055 (point)))))
1056 (if (makefile-remember-macro macro-name)
1057 (message "Picked up macro \"%s\" from line %d"
1058 macro-name (line-number-at-pos))))
1059 (forward-line)))
9e9bf716 1060 (message "Read macros OK.")))
72c0ae01 1061
6783b1ce 1062(defun makefile-pickup-everything (arg)
308a2860 1063 "Notice names of all macros and targets in Makefile.
6783b1ce
RS
1064Prefix arg means force pickups to be redone."
1065 (interactive "P")
1066 (if arg
dfa89b5d
DP
1067 (setq makefile-need-target-pickup t
1068 makefile-need-macro-pickup t))
72c0ae01
ER
1069 (makefile-pickup-macros)
1070 (makefile-pickup-targets)
1071 (if makefile-pickup-everything-picks-up-filenames-p
1072 (makefile-pickup-filenames-as-targets)))
1073
72c0ae01 1074(defun makefile-pickup-filenames-as-targets ()
308a2860
RS
1075 "Scan the current directory for filenames to use as targets.
1076Checks each filename against `makefile-ignored-files-in-pickup-regex'
1077and adds all qualifying names to the list of known targets."
72c0ae01 1078 (interactive)
dfa89b5d
DP
1079 (mapc (lambda (name)
1080 (or (file-directory-p name)
1081 (string-match makefile-ignored-files-in-pickup-regex name)
1082 (if (makefile-remember-target name)
1083 (message "Picked up file \"%s\" as target" name))))
1084 (file-name-all-completions "" (or (file-name-directory (buffer-file-name)) ""))))
72c0ae01 1085
6783b1ce
RS
1086\f
1087
1088;;; Completion.
1089
1090(defun makefile-complete ()
1091 "Perform completion on Makefile construct preceding point.
1092Can complete variable and target names.
1093The context determines which are considered."
1094 (interactive)
1095 (let* ((beg (save-excursion
1096 (skip-chars-backward "^$(){}:#= \t\n")
1097 (point)))
1098 (try (buffer-substring beg (point)))
1099 (do-macros nil)
1100 (paren nil))
1101
1102 (save-excursion
1103 (goto-char beg)
1104 (let ((pc (preceding-char)))
1105 (cond
1106 ;; Beginning of line means anything.
1107 ((bolp)
1108 ())
1109
1110 ;; Preceding "$" means macros only.
1111 ((= pc ?$)
1112 (setq do-macros t))
1113
1114 ;; Preceding "$(" or "${" means macros only.
1115 ((and (or (= pc ?{)
1116 (= pc ?\())
1117 (progn
1118 (setq paren pc)
1119 (backward-char)
1120 (and (not (bolp))
1121 (= (preceding-char) ?$))))
1122 (setq do-macros t)))))
1123
1124 ;; Try completion.
1125 (let* ((table (append (if do-macros
1126 '()
1127 makefile-target-table)
1128 makefile-macro-table))
1129 (completion (try-completion try table)))
1130 (cond
1131 ;; Exact match, so insert closing paren or colon.
1132 ((eq completion t)
1133 (insert (if do-macros
1134 (if (eq paren ?{)
1135 ?}
1136 ?\))
1137 (if (save-excursion
1138 (goto-char beg)
1139 (bolp))
1140 ":"
1141 " "))))
1142
1143 ;; No match.
1144 ((null completion)
1145 (message "Can't find completion for \"%s\"" try)
1146 (ding))
1147
1148 ;; Partial completion.
1149 ((not (string= try completion))
1150 ;; FIXME it would be nice to supply the closing paren if an
1151 ;; exact, unambiguous match were found. That is not possible
1152 ;; right now. Ditto closing ":" for targets.
1153 (delete-region beg (point))
1154
1155 ;; DO-MACROS means doing macros only. If not that, then check
1156 ;; to see if this completion is a macro. Special insertion
1157 ;; must be done for macros.
1158 (if (or do-macros
1159 (assoc completion makefile-macro-table))
1160 (let ((makefile-use-curly-braces-for-macros-p
1161 (or (eq paren ?{)
1162 makefile-use-curly-braces-for-macros-p)))
1163 (delete-backward-char 2)
1164 (makefile-do-macro-insertion completion)
1165 (delete-backward-char 1))
1166
1167 ;; Just insert targets.
1168 (insert completion)))
1169
1170 ;; Can't complete any more, so make completion list. FIXME
1171 ;; this doesn't do the right thing when the completion is
1172 ;; actually inserted. I don't think there is an easy way to do
1173 ;; that.
1174 (t
1175 (message "Making completion list...")
1176 (let ((list (all-completions try table)))
1177 (with-output-to-temp-buffer "*Completions*"
1178 (display-completion-list list)))
1179 (message "Making completion list...done"))))))
1180
1181\f
1182
308a2860
RS
1183;; Backslashification. Stolen from cc-mode.el.
1184
dfa9e6fc
RS
1185(defun makefile-backslash-region (from to delete-flag)
1186 "Insert, align, or delete end-of-line backslashes on the lines in the region.
1187With no argument, inserts backslashes and aligns existing backslashes.
1188With an argument, deletes the backslashes.
1189
a564ebfc 1190This function does not modify the last line of the region if the region ends
dfa9e6fc
RS
1191right at the start of the following line; it does not modify blank lines
1192at the start of the region. So you can put the region around an entire macro
1193definition and conveniently use this command."
308a2860
RS
1194 (interactive "r\nP")
1195 (save-excursion
dfa9e6fc
RS
1196 (goto-char from)
1197 (let ((column makefile-backslash-column)
1198 (endmark (make-marker)))
1199 (move-marker endmark to)
1200 ;; Compute the smallest column number past the ends of all the lines.
1201 (if makefile-backslash-align
1202 (progn
1203 (if (not delete-flag)
1204 (while (< (point) to)
1205 (end-of-line)
1206 (if (= (preceding-char) ?\\)
1207 (progn (forward-char -1)
1208 (skip-chars-backward " \t")))
1209 (setq column (max column (1+ (current-column))))
1210 (forward-line 1)))
1211 ;; Adjust upward to a tab column, if that doesn't push
1212 ;; past the margin.
1213 (if (> (% column tab-width) 0)
1214 (let ((adjusted (* (/ (+ column tab-width -1) tab-width)
1215 tab-width)))
1216 (if (< adjusted (window-width))
1217 (setq column adjusted))))))
1218 ;; Don't modify blank lines at start of region.
1219 (goto-char from)
1220 (while (and (< (point) endmark) (eolp))
1221 (forward-line 1))
1222 ;; Add or remove backslashes on all the lines.
1223 (while (and (< (point) endmark)
1224 ;; Don't backslashify the last line
1225 ;; if the region ends right at the start of the next line.
1226 (save-excursion
1227 (forward-line 1)
1228 (< (point) endmark)))
1229 (if (not delete-flag)
1230 (makefile-append-backslash column)
1231 (makefile-delete-backslash))
1232 (forward-line 1))
1233 (move-marker endmark nil))))
1234
1235(defun makefile-append-backslash (column)
1236 (end-of-line)
1237 ;; Note that "\\\\" is needed to get one backslash.
1238 (if (= (preceding-char) ?\\)
1239 (progn (forward-char -1)
1240 (delete-horizontal-space)
1241 (indent-to column (if makefile-backslash-align nil 1)))
1242 (indent-to column (if makefile-backslash-align nil 1))
1243 (insert "\\")))
1244
1245(defun makefile-delete-backslash ()
1246 (end-of-line)
1247 (or (bolp)
1248 (progn
1249 (forward-char -1)
1250 (if (looking-at "\\\\")
1251 (delete-region (1+ (point))
1252 (progn (skip-chars-backward " \t") (point)))))))
1253
1254\f
1255
1256;; Filling
1257
1258(defun makefile-fill-paragraph (arg)
1259 ;; Fill comments, backslashed lines, and variable definitions
1260 ;; specially.
1261 (save-excursion
1262 (beginning-of-line)
1263 (cond
4beb7453 1264 ((looking-at "^#+")
bb5b7758
RS
1265 ;; Found a comment. Set the fill prefix, and find the paragraph
1266 ;; boundaries by searching for lines that look like comment-only
1267 ;; lines.
e006b3cd 1268 (let ((fill-prefix (match-string-no-properties 0))
dfa9e6fc 1269 (fill-paragraph-function nil))
bb5b7758
RS
1270 (save-excursion
1271 (save-restriction
1272 (narrow-to-region
1273 ;; Search backwards.
1274 (save-excursion
1275 (while (and (zerop (forward-line -1))
1276 (looking-at "^#")))
1277 ;; We may have gone too far. Go forward again.
1278 (or (looking-at "^#")
1279 (forward-line 1))
1280 (point))
1281 ;; Search forwards.
1282 (save-excursion
1283 (while (looking-at "^#")
1284 (forward-line))
1285 (point)))
1286 (fill-paragraph nil)
1287 t))))
dfa9e6fc
RS
1288
1289 ;; Must look for backslashed-region before looking for variable
1290 ;; assignment.
e006b3cd
SM
1291 ((or (eq (char-before (line-end-position 1)) ?\\)
1292 (eq (char-before (line-end-position 0)) ?\\))
dfa9e6fc
RS
1293 ;; A backslash region. Find beginning and end, remove
1294 ;; backslashes, fill, and then reapply backslahes.
1295 (end-of-line)
1296 (let ((beginning
1297 (save-excursion
1298 (end-of-line 0)
1299 (while (= (preceding-char) ?\\)
1300 (end-of-line 0))
1301 (forward-char)
1302 (point)))
1303 (end
1304 (save-excursion
1305 (while (= (preceding-char) ?\\)
1306 (end-of-line 2))
1307 (point))))
1308 (save-restriction
1309 (narrow-to-region beginning end)
1310 (makefile-backslash-region (point-min) (point-max) t)
1311 (let ((fill-paragraph-function nil))
1312 (fill-paragraph nil))
1313 (makefile-backslash-region (point-min) (point-max) nil)
1314 (goto-char (point-max))
1315 (if (< (skip-chars-backward "\n") 0)
1316 (delete-region (point) (point-max))))))
1317
1318 ((looking-at makefile-macroassign-regex)
1319 ;; Have a macro assign. Fill just this line, and then backslash
1320 ;; resulting region.
308a2860 1321 (save-restriction
debe2de2 1322 (narrow-to-region (point) (line-beginning-position 2))
dfa9e6fc
RS
1323 (let ((fill-paragraph-function nil))
1324 (fill-paragraph nil))
1325 (makefile-backslash-region (point-min) (point-max) nil)))))
1326
1327 ;; Always return non-nil so we don't fill anything else.
1328 t)
308a2860
RS
1329
1330\f
1331
72c0ae01 1332;;; ------------------------------------------------------------
6783b1ce 1333;;; Browser mode.
72c0ae01
ER
1334;;; ------------------------------------------------------------
1335
72c0ae01
ER
1336(defun makefile-browser-format-target-line (target selected)
1337 (format
1338 (concat (make-string makefile-browser-leftmost-column ?\ )
1339 (if selected
1340 makefile-browser-selected-mark
1341 makefile-browser-unselected-mark)
1342 "%s%s")
1343 target makefile-target-colon))
1344
1345(defun makefile-browser-format-macro-line (macro selected)
1346 (format
1347 (concat (make-string makefile-browser-leftmost-column ?\ )
1348 (if selected
1349 makefile-browser-selected-mark
1350 makefile-browser-unselected-mark)
1351 (makefile-format-macro-ref macro))))
1352
1353(defun makefile-browser-fill (targets macros)
c72344c7
RS
1354 (let ((inhibit-read-only t))
1355 (goto-char (point-min))
1356 (erase-buffer)
1357 (mapconcat
1358 (function
1359 (lambda (item) (insert (makefile-browser-format-target-line (car item) nil) "\n")))
1360 targets
1361 "")
1362 (mapconcat
1363 (function
1364 (lambda (item) (insert (makefile-browser-format-macro-line (car item) nil) "\n")))
1365 macros
1366 "")
1367 (sort-lines nil (point-min) (point-max))
1368 (goto-char (1- (point-max)))
1369 (delete-char 1) ; remove unnecessary newline at eob
1370 (goto-char (point-min))
1371 (forward-char makefile-browser-cursor-column)))
72c0ae01
ER
1372
1373;;;
1374;;; Moving up and down in the browser
1375;;;
1376
1377(defun makefile-browser-next-line ()
1378 "Move the browser selection cursor to the next line."
1379 (interactive)
1380 (if (not (makefile-last-line-p))
1381 (progn
1382 (forward-line 1)
1383 (forward-char makefile-browser-cursor-column))))
1384
1385(defun makefile-browser-previous-line ()
1386 "Move the browser selection cursor to the previous line."
1387 (interactive)
1388 (if (not (makefile-first-line-p))
1389 (progn
1390 (forward-line -1)
1391 (forward-char makefile-browser-cursor-column))))
1392
1393;;;
1394;;; Quitting the browser (returns to client buffer)
1395;;;
1396
1397(defun makefile-browser-quit ()
308a2860 1398 "Leave the browser and return to the makefile buffer."
72c0ae01
ER
1399 (interactive)
1400 (let ((my-client makefile-browser-client))
1401 (setq makefile-browser-client nil) ; we quitted, so NO client!
1402 (set-buffer-modified-p nil)
59047653 1403 (quit-window t)
72c0ae01
ER
1404 (pop-to-buffer my-client)))
1405
1406;;;
1407;;; Toggle state of a browser item
1408;;;
1409
1410(defun makefile-browser-toggle ()
1411 "Toggle the selection state of the browser item at the cursor position."
1412 (interactive)
1413 (let ((this-line (count-lines (point-min) (point))))
1414 (setq this-line (max 1 this-line))
1415 (makefile-browser-toggle-state-for-line this-line)
1416 (goto-line this-line)
c72344c7
RS
1417 (let ((inhibit-read-only t))
1418 (beginning-of-line)
1419 (if (makefile-browser-on-macro-line-p)
1420 (let ((macro-name (makefile-browser-this-line-macro-name)))
8d3a7934 1421 (delete-region (point) (progn (end-of-line) (point)))
c72344c7
RS
1422 (insert
1423 (makefile-browser-format-macro-line
1424 macro-name
1425 (makefile-browser-get-state-for-line this-line))))
1426 (let ((target-name (makefile-browser-this-line-target-name)))
8d3a7934 1427 (delete-region (point) (progn (end-of-line) (point)))
72c0ae01 1428 (insert
c72344c7
RS
1429 (makefile-browser-format-target-line
1430 target-name
1431 (makefile-browser-get-state-for-line this-line))))))
72c0ae01
ER
1432 (beginning-of-line)
1433 (forward-char makefile-browser-cursor-column)
1434 (if makefile-browser-auto-advance-after-selection-p
1435 (makefile-browser-next-line))))
1436
1437;;;
1438;;; Making insertions into the client buffer
1439;;;
1440
1441(defun makefile-browser-insert-continuation ()
9e9bf716 1442 "Insert a makefile continuation.
308a2860 1443In the makefile buffer, go to (end-of-line), insert a \'\\\'
72c0ae01 1444character, insert a new blank line, go to that line and indent by one TAB.
9e9bf716
ER
1445This is most useful in the process of creating continued lines when copying
1446large dependencies from the browser to the client buffer.
a4e104bf 1447\(point) advances accordingly in the client buffer."
72c0ae01 1448 (interactive)
debe2de2 1449 (with-current-buffer makefile-browser-client
72c0ae01
ER
1450 (end-of-line)
1451 (insert "\\\n\t")))
1452
1453(defun makefile-browser-insert-selection ()
308a2860 1454 "Insert all selected targets and/or macros in the makefile buffer.
bd0e4eb1 1455Insertion takes place at point."
72c0ae01
ER
1456 (interactive)
1457 (save-excursion
1458 (goto-line 1)
1459 (let ((current-line 1))
1460 (while (not (eobp))
1461 (if (makefile-browser-get-state-for-line current-line)
1462 (makefile-browser-send-this-line-item))
1463 (forward-line 1)
1464 (setq current-line (1+ current-line))))))
1465
1466(defun makefile-browser-insert-selection-and-quit ()
1467 (interactive)
1468 (makefile-browser-insert-selection)
1469 (makefile-browser-quit))
1470
1471(defun makefile-browser-send-this-line-item ()
1472 (if (makefile-browser-on-macro-line-p)
1473 (save-excursion
1474 (let ((macro-name (makefile-browser-this-line-macro-name)))
1475 (set-buffer makefile-browser-client)
1476 (insert (makefile-format-macro-ref macro-name) " ")))
1477 (save-excursion
1478 (let ((target-name (makefile-browser-this-line-target-name)))
1479 (set-buffer makefile-browser-client)
1480 (insert target-name " ")))))
1481
72c0ae01
ER
1482(defun makefile-browser-start-interaction ()
1483 (use-local-map makefile-browser-map)
1484 (setq buffer-read-only t))
1485
72c0ae01
ER
1486(defun makefile-browse (targets macros)
1487 (interactive)
1488 (if (zerop (+ (length targets) (length macros)))
1489 (progn
1490 (beep)
1491 (message "No macros or targets to browse! Consider running 'makefile-pickup-everything\'"))
1492 (let ((browser-buffer (get-buffer-create makefile-browser-buffer-name)))
1493 (pop-to-buffer browser-buffer)
72c0ae01 1494 (makefile-browser-fill targets macros)
9e9bf716 1495 (shrink-window-if-larger-than-buffer)
a7e34f79
SM
1496 (set (make-local-variable 'makefile-browser-selection-vector)
1497 (make-vector (+ (length targets) (length macros)) nil))
72c0ae01 1498 (makefile-browser-start-interaction))))
72c0ae01
ER
1499
1500(defun makefile-switch-to-browser ()
1501 (interactive)
1502 (run-hooks 'makefile-browser-hook)
1503 (setq makefile-browser-client (current-buffer))
9e9bf716
ER
1504 (makefile-pickup-targets)
1505 (makefile-pickup-macros)
72c0ae01
ER
1506 (makefile-browse makefile-target-table makefile-macro-table))
1507
6783b1ce 1508\f
72c0ae01
ER
1509
1510;;; ------------------------------------------------------------
1511;;; Up-to-date overview buffer
1512;;; ------------------------------------------------------------
1513
1514(defun makefile-create-up-to-date-overview ()
9e9bf716 1515 "Create a buffer containing an overview of the state of all known targets.
72c0ae01
ER
1516Known targets are targets that are explicitly defined in that makefile;
1517in other words, all targets that appear on the left hand side of a
1518dependency in the makefile."
1519 (interactive)
1520 (if (y-or-n-p "Are you sure that the makefile being edited is consistent? ")
1521 ;;
1522 ;; The rest of this function operates on a temporary makefile, created by
1523 ;; writing the current contents of the makefile buffer.
1524 ;;
1525 (let ((saved-target-table makefile-target-table)
1526 (this-buffer (current-buffer))
1527 (makefile-up-to-date-buffer
1528 (get-buffer-create makefile-up-to-date-buffer-name))
1529 (filename (makefile-save-temporary))
1530 ;;
1531 ;; Forget the target table because it may contain picked-up filenames
1532 ;; that are not really targets in the current makefile.
1533 ;; We don't want to query these, so get a new target-table with just the
1534 ;; targets that can be found in the makefile buffer.
1535 ;; The 'old' target table will be restored later.
1536 ;;
1537 (real-targets (progn
72c0ae01 1538 (makefile-pickup-targets)
9e9bf716
ER
1539 makefile-target-table))
1540 (prereqs makefile-has-prereqs)
1541 )
72c0ae01
ER
1542
1543 (set-buffer makefile-up-to-date-buffer)
1544 (setq buffer-read-only nil)
1545 (erase-buffer)
9e9bf716 1546 (makefile-query-targets filename real-targets prereqs)
72c0ae01
ER
1547 (if (zerop (buffer-size)) ; if it did not get us anything
1548 (progn
1549 (kill-buffer (current-buffer))
1550 (message "No overview created!")))
1551 (set-buffer this-buffer)
1552 (setq makefile-target-table saved-target-table)
1553 (if (get-buffer makefile-up-to-date-buffer-name)
1554 (progn
1555 (pop-to-buffer (get-buffer makefile-up-to-date-buffer-name))
9e9bf716 1556 (shrink-window-if-larger-than-buffer)
72c0ae01
ER
1557 (sort-lines nil (point-min) (point-max))
1558 (setq buffer-read-only t))))))
72c0ae01
ER
1559
1560(defun makefile-save-temporary ()
1561 "Create a temporary file from the current makefile buffer."
1562 (let ((filename (makefile-generate-temporary-filename)))
1563 (write-region (point-min) (point-max) filename nil 0)
1564 filename)) ; return the filename
1565
1566(defun makefile-generate-temporary-filename ()
308a2860 1567 "Create a filename suitable for use in `makefile-save-temporary'.
72c0ae01 1568Be careful to allow brain-dead file systems (DOS, SYSV ...) to cope
308a2860 1569with the generated name!"
72c0ae01
ER
1570 (let ((my-name (user-login-name))
1571 (my-uid (int-to-string (user-uid))))
1572 (concat "mktmp"
1573 (if (> (length my-name) 3)
1574 (substring my-name 0 3)
1575 my-name)
1576 "."
1577 (if (> (length my-uid) 3)
1578 (substring my-uid 0 3)
1579 my-uid))))
1580
9e9bf716 1581(defun makefile-query-targets (filename target-table prereq-list)
bd0e4eb1 1582 "Fill the up-to-date overview buffer.
308a2860 1583Checks each target in TARGET-TABLE using `makefile-query-one-target-method'
72c0ae01
ER
1584and generates the overview, one line per target name."
1585 (insert
9e9bf716
ER
1586 (mapconcat
1587 (function (lambda (item)
1588 (let* ((target-name (car item))
1589 (no-prereqs (not (member target-name prereq-list)))
a564ebfc 1590 (needs-rebuild (or no-prereqs
9e9bf716
ER
1591 (funcall
1592 makefile-query-one-target-method
1593 target-name
1594 filename))))
1595 (format "\t%s%s"
1596 target-name
1597 (cond (no-prereqs " .. has no prerequisites")
1598 (needs-rebuild " .. NEEDS REBUILD")
1599 (t " .. is up to date"))))
1600 ))
1601 target-table "\n"))
72c0ae01
ER
1602 (goto-char (point-min))
1603 (delete-file filename)) ; remove the tmpfile
1604
1605(defun makefile-query-by-make-minus-q (target &optional filename)
15502042 1606 (not (eq 0
308a2860
RS
1607 (call-process makefile-brave-make nil nil nil
1608 "-f" filename "-q" target))))
72c0ae01 1609
6783b1ce
RS
1610\f
1611
72c0ae01
ER
1612;;; ------------------------------------------------------------
1613;;; Continuation cleanup
1614;;; ------------------------------------------------------------
1615
1616(defun makefile-cleanup-continuations ()
1617 (if (eq major-mode 'makefile-mode)
e006b3cd 1618 (if (and makefile-cleanup-continuations
72c0ae01
ER
1619 (not buffer-read-only))
1620 (save-excursion
1621 (goto-char (point-min))
e006b3cd 1622 (while (re-search-forward "\\\\[ \t]+$" nil t)
72c0ae01
ER
1623 (replace-match "\\" t t))))))
1624
b8ca7cc3
RS
1625
1626;;; ------------------------------------------------------------
1627;;; Warn of suspicious lines
1628;;; ------------------------------------------------------------
1629
1630(defun makefile-warn-suspicious-lines ()
68049bfa
SM
1631 ;; Returning non-nil cancels the save operation
1632 (if (eq major-mode 'makefile-mode)
1633 (save-excursion
1634 (goto-char (point-min))
1635 (if (re-search-forward "^\\(\t+$\\| +\t\\)" nil t)
1636 (not (y-or-n-p
a11e2800
RS
1637 (format "Suspicious line %d. Save anyway? "
1638 (count-lines (point-min) (point)))))))))
1639
1640(defun makefile-warn-continuations ()
1641 (if (eq major-mode 'makefile-mode)
1642 (save-excursion
1643 (goto-char (point-min))
5cc2d137 1644 (if (re-search-forward "\\\\[ \t]+$" nil t)
a11e2800
RS
1645 (not (y-or-n-p
1646 (format "Suspicious continuation in line %d. Save anyway? "
68049bfa 1647 (count-lines (point-min) (point)))))))))
6783b1ce 1648\f
b8ca7cc3 1649
72c0ae01
ER
1650;;; ------------------------------------------------------------
1651;;; GNU make function support
1652;;; ------------------------------------------------------------
1653
1654(defun makefile-insert-gmake-function ()
308a2860
RS
1655 "Insert a GNU make function call.
1656Asks for the name of the function to use (with completion).
1657Then prompts for all required parameters."
72c0ae01
ER
1658 (interactive)
1659 (let* ((gm-function-name (completing-read
1660 "Function: "
1661 makefile-gnumake-functions-alist
1662 nil t nil))
1663 (gm-function-prompts
1664 (cdr (assoc gm-function-name makefile-gnumake-functions-alist))))
1665 (if (not (zerop (length gm-function-name)))
1666 (insert (makefile-format-macro-ref
1667 (concat gm-function-name " "
1668 (makefile-prompt-for-gmake-funargs
1669 gm-function-name gm-function-prompts)))
1670 " "))))
1671
1672(defun makefile-prompt-for-gmake-funargs (function-name prompt-list)
1673 (mapconcat
1674 (function (lambda (one-prompt)
308a2860
RS
1675 (read-string (format "[%s] %s: " function-name one-prompt)
1676 nil)))
72c0ae01
ER
1677 prompt-list
1678 ","))
308a2860 1679
6783b1ce 1680\f
72c0ae01
ER
1681
1682;;; ------------------------------------------------------------
1683;;; Utility functions
1684;;; ------------------------------------------------------------
1685
9c859203
DP
1686(defun makefile-match-function-end (end)
1687 "To be called as an anchored matcher by font-lock.
1688The anchor must have matched the opening parens in the first group."
1689 (let ((s (match-string-no-properties 1)))
1690 (setq s (cond ((string= s "(") "\\(.*?\\)[ \t]*)")
1691 ((string= s "{") "\\(.*?\\)[ \t]*}")
1692 ((string= s "((") "\\(.*?\\)[ \t]*))")
1693 ((string= s "{{") "\\(.*?\\)[ \t]*}}")))
1694 (if s (looking-at s))))
1695
0b11ce59 1696(defun makefile-match-dependency (bound)
92984345 1697 "Search for `makefile-dependency-regex' up to BOUND.
92984345
DP
1698Checks that the colon has not already been fontified, else we
1699matched in a rule action."
1700 (catch 'found
0b11ce59 1701 (let ((pt (point)))
dfa89b5d 1702 (while (progn (skip-chars-forward makefile-dependency-skip bound)
774cd454 1703 (< (point) (or bound (point-max))))
0b11ce59 1704 (forward-char)
5c8b5442
DP
1705 (or (eq (char-after) ?=)
1706 (get-text-property (1- (point)) 'face)
602dc0da
DP
1707 (if (> (line-beginning-position) (+ (point-min) 2))
1708 (eq (char-before (line-end-position 0)) ?\\))
0b11ce59
DP
1709 (when (save-excursion
1710 (beginning-of-line)
1711 (looking-at makefile-dependency-regex))
dce16363
RS
1712 (save-excursion
1713 (let ((deps-end (match-end 1))
1714 (match-data (match-data)))
1715 (goto-char deps-end)
1716 (skip-chars-backward " \t")
1717 (setq deps-end (point))
1718 (beginning-of-line)
1719 (skip-chars-forward " \t")
1720 ;; Alter the bounds recorded for subexp 1,
1721 ;; which is what is supposed to match the targets.
1722 (setcar (nthcdr 2 match-data) (point))
1723 (setcar (nthcdr 3 match-data) deps-end)
1724 (store-match-data match-data)))
0b11ce59
DP
1725 (end-of-line)
1726 (throw 'found (point)))))
1727 (goto-char pt))
1728 nil))
92984345
DP
1729
1730(defun makefile-match-action (bound)
1731 (catch 'found
1732 (while (re-search-forward makefile-rule-action-regex bound t)
1733 (or (eq ?\\ (char-after (- (match-beginning 0) 2)))
1734 (throw 'found t)))))
1735
6783b1ce
RS
1736(defun makefile-do-macro-insertion (macro-name)
1737 "Insert a macro reference."
1738 (if (not (zerop (length macro-name)))
1739 (if (assoc macro-name makefile-runtime-macros-list)
1740 (insert "$" macro-name)
1741 (insert (makefile-format-macro-ref macro-name)))))
1742
9e9bf716 1743(defun makefile-remember-target (target-name &optional has-prereqs)
72c0ae01
ER
1744 "Remember a given target if it is not already remembered for this buffer."
1745 (if (not (zerop (length target-name)))
9e9bf716 1746 (progn
72c0ae01
ER
1747 (if (not (assoc target-name makefile-target-table))
1748 (setq makefile-target-table
9e9bf716
ER
1749 (cons (list target-name) makefile-target-table)))
1750 (if has-prereqs
1751 (setq makefile-has-prereqs
1752 (cons target-name makefile-has-prereqs))))))
72c0ae01
ER
1753
1754(defun makefile-remember-macro (macro-name)
1755 "Remember a given macro if it is not already remembered for this buffer."
1756 (if (not (zerop (length macro-name)))
1757 (if (not (assoc macro-name makefile-macro-table))
1758 (setq makefile-macro-table
1759 (cons (list macro-name) makefile-macro-table)))))
1760
1761(defun makefile-forward-after-target-colon ()
308a2860
RS
1762 "Move point forward after inserting the terminating colon of a target.
1763This acts according to the value of `makefile-tab-after-target-colon'."
72c0ae01
ER
1764 (if makefile-tab-after-target-colon
1765 (insert "\t")
1766 (insert " ")))
1767
1768(defun makefile-browser-on-macro-line-p ()
1769 "Determine if point is on a macro line in the browser."
1770 (save-excursion
1771 (beginning-of-line)
debe2de2 1772 (re-search-forward "\\$[{(]" (line-end-position) t)))
72c0ae01
ER
1773
1774(defun makefile-browser-this-line-target-name ()
1775 "Extract the target name from a line in the browser."
1776 (save-excursion
1777 (end-of-line)
1778 (skip-chars-backward "^ \t")
debe2de2 1779 (buffer-substring (point) (1- (line-end-position)))))
72c0ae01
ER
1780
1781(defun makefile-browser-this-line-macro-name ()
1782 "Extract the macro name from a line in the browser."
1783 (save-excursion
1784 (beginning-of-line)
debe2de2 1785 (re-search-forward "\\$[{(]" (line-end-position) t)
72c0ae01
ER
1786 (let ((macro-start (point)))
1787 (skip-chars-forward "^})")
1788 (buffer-substring macro-start (point)))))
1789
1790(defun makefile-format-macro-ref (macro-name)
308a2860
RS
1791 "Format a macro reference.
1792Uses `makefile-use-curly-braces-for-macros-p'."
b8ca7cc3
RS
1793 (if (or (char-equal ?\( (string-to-char macro-name))
1794 (char-equal ?\{ (string-to-char macro-name)))
1795 (format "$%s" macro-name)
1796 (if makefile-use-curly-braces-for-macros-p
1797 (format "${%s}" macro-name)
1798 (format "$(%s)" macro-name))))
72c0ae01
ER
1799
1800(defun makefile-browser-get-state-for-line (n)
1801 (aref makefile-browser-selection-vector (1- n)))
1802
1803(defun makefile-browser-set-state-for-line (n to-state)
1804 (aset makefile-browser-selection-vector (1- n) to-state))
1805
1806(defun makefile-browser-toggle-state-for-line (n)
1807 (makefile-browser-set-state-for-line n (not (makefile-browser-get-state-for-line n))))
1808
72c0ae01 1809(defun makefile-last-line-p ()
debe2de2 1810 (= (line-end-position) (point-max)))
72c0ae01
ER
1811
1812(defun makefile-first-line-p ()
debe2de2 1813 (= (line-beginning-position) (point-min)))
72c0ae01 1814
6783b1ce
RS
1815\f
1816
3b7f63b1 1817;;; Support for other packages, like add-log.
6783b1ce
RS
1818
1819(defun makefile-add-log-defun ()
32b558fa
RS
1820 "Return name of target or variable assignment that point is in.
1821If it isn't in one, return nil."
6783b1ce 1822 (save-excursion
32b558fa
RS
1823 (let (found)
1824 (beginning-of-line)
1825 ;; Scan back line by line, noticing when we come to a
1826 ;; variable or rule definition, and giving up when we see
1827 ;; a line that is not part of either of those.
e006b3cd
SM
1828 (while (not (or (setq found
1829 (when (or (looking-at makefile-macroassign-regex)
1830 (looking-at makefile-dependency-regex))
1831 (match-string-no-properties 1)))
1832 ;; Don't keep looking across a blank line or comment.
1833 (looking-at "$\\|#")
1834 (not (zerop (forward-line -1))))))
1835 found)))
6783b1ce 1836
0017329b
SM
1837(provide 'make-mode)
1838
ab5796a9 1839;;; arch-tag: bd23545a-de91-44fb-b1b2-feafbb2635a0
80e01c19 1840;;; make-mode.el ends here