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