(cperl-set-style-back): Fix spelling in docstrings.
[bpt/emacs.git] / lisp / progmodes / make-mode.el
1 ;;; make-mode.el --- makefile editing commands for Emacs
2
3 ;; Copyright (C) 1992,94,99,2000,2001, 2002, 2003 Free Software Foundation, Inc.
4
5 ;; Author: Thomas Neumann <tom@smart.bo.open.de>
6 ;; Eric S. Raymond <esr@snark.thyrsus.com>
7 ;; Maintainer: FSF
8 ;; Adapted-By: ESR
9 ;; Keywords: unix, tools
10
11 ;; This file is part of GNU Emacs.
12
13 ;; GNU Emacs is free software; you can redistribute it and/or modify
14 ;; it under the terms of the GNU General Public License as published by
15 ;; the Free Software Foundation; either version 2, or (at your option)
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
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.
27
28 ;;; Commentary:
29
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 ;;
40 ;; The command C-c C-f adds certain filenames in the current directory
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 ;;
49 ;; The command C-c C-b pops up a browser window listing all target and
50 ;; macro names. You can mark or unmark items with C-c SPC, and insert
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.
57
58 ;;
59 ;; To Do:
60 ;;
61 ;; * Add missing doc strings, improve terse doc strings.
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).
71 ;; * Make browser into a major mode.
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
85 ;;; Code:
86
87 ;; Sadly we need this for a macro.
88 (eval-when-compile
89 (require 'imenu)
90 (require 'dabbrev)
91 (require 'add-log))
92
93 ;;; ------------------------------------------------------------
94 ;;; Configurable stuff
95 ;;; ------------------------------------------------------------
96
97 (defgroup makefile nil
98 "Makefile editing commands for Emacs."
99 :group 'tools
100 :prefix "makefile-")
101
102 (defface makefile-space
103 '((((class color)) (:background "hotpink"))
104 (t (:reverse-video t)))
105 "Face to use for highlighting leading spaces in Font-Lock mode."
106 :group 'faces
107 :group 'makefile)
108 (put 'makefile-space-face 'face-alias 'makefile-space)
109
110 (defface makefile-targets
111 ;; This needs to go along both with foreground and background colors (i.e. shell)
112 '((t (:inherit font-lock-function-name-face)))
113 "Face to use for additionally highlighting rule targets in Font-Lock mode."
114 :group 'faces
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 'faces
124 :group 'makefile
125 :version "22.1")
126
127 (defface makefile-makepp-perl
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
133 :group 'makefile
134 :version "22.1")
135
136 (defcustom makefile-browser-buffer-name "*Macros and Targets*"
137 "*Name of the macro- and target browser buffer."
138 :type 'string
139 :group 'makefile)
140
141 (defcustom makefile-target-colon ":"
142 "*String to append to all target names inserted by `makefile-insert-target'.
143 \":\" or \"::\" are common values."
144 :type 'string
145 :group 'makefile)
146
147 (defcustom makefile-macro-assign " = "
148 "*String to append to all macro names inserted by `makefile-insert-macro'.
149 The normal value should be \" = \", since this is what
150 standard make expects. However, newer makes such as dmake
151 allow a larger variety of different macro assignments, so you
152 might prefer to use \" += \" or \" := \" ."
153 :type 'string
154 :group 'makefile)
155
156 (defcustom makefile-electric-keys nil
157 "*If non-nil, Makefile mode should install electric keybindings.
158 Default is nil."
159 :type 'boolean
160 :group 'makefile)
161
162 (defcustom makefile-use-curly-braces-for-macros-p nil
163 "*Controls the style of generated macro references.
164 Non-nil means macro references should use curly braces, like `${this}'.
165 nil means use parentheses, like `$(this)'."
166 :type 'boolean
167 :group 'makefile)
168
169 (defcustom makefile-tab-after-target-colon t
170 "*If non-nil, insert a TAB after a target colon.
171 Otherwise, a space is inserted.
172 The default is t."
173 :type 'boolean
174 :group 'makefile)
175
176 (defcustom makefile-browser-leftmost-column 10
177 "*Number of blanks to the left of the browser selection mark."
178 :type 'integer
179 :group 'makefile)
180
181 (defcustom makefile-browser-cursor-column 10
182 "*Column the cursor goes to when it moves up or down in the Makefile browser."
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
192 "*If non-nil, `makefile-backslash-region' will align backslashes."
193 :type 'boolean
194 :group 'makefile)
195
196 (defcustom makefile-browser-selected-mark "+ "
197 "*String used to mark selected entries in the Makefile browser."
198 :type 'string
199 :group 'makefile)
200
201 (defcustom makefile-browser-unselected-mark " "
202 "*String used to mark unselected entries in the Makefile browser."
203 :type 'string
204 :group 'makefile)
205
206 (defcustom makefile-browser-auto-advance-after-selection-p t
207 "*If non-nil, cursor will move after item is selected in Makefile browser."
208 :type 'boolean
209 :group 'makefile)
210
211 (defcustom makefile-pickup-everything-picks-up-filenames-p nil
212 "*If non-nil, `makefile-pickup-everything' picks up filenames as targets.
213 This means it calls `makefile-pickup-filenames-as-targets'.
214 Otherwise filenames are omitted."
215 :type 'boolean
216 :group 'makefile)
217
218 (defcustom makefile-cleanup-continuations nil
219 "*If non-nil, automatically clean up continuation lines when saving.
220 A line is cleaned up by removing all whitespace following a trailing
221 backslash. This is done silently.
222 IMPORTANT: Please note that enabling this option causes Makefile mode
223 to MODIFY A FILE WITHOUT YOUR CONFIRMATION when \"it seems necessary\"."
224 :type 'boolean
225 :group 'makefile)
226
227 (defcustom makefile-mode-hook nil
228 "*Normal hook run by `makefile-mode'."
229 :type 'hook
230 :group 'makefile)
231
232 (defvar makefile-browser-hook '())
233
234 ;;
235 ;; Special targets for DMake, Sun's make ...
236 ;;
237 (defcustom makefile-special-targets-list
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"))
246 "*List of special targets.
247 You will be offered to complete on one of those in the minibuffer whenever
248 you enter a \".\" at the beginning of a line in `makefile-mode'."
249 :type '(repeat (list string))
250 :group 'makefile)
251
252 (defcustom makefile-runtime-macros-list
253 '(("@") ("&") (">") ("<") ("*") ("^") ("+") ("?") ("%") ("$"))
254 "*List of macros that are resolved by make at runtime.
255 If you insert a macro reference using `makefile-insert-macro-ref', the name
256 of the macro is checked against this list. If it can be found its name will
257 not be enclosed in { } or ( )."
258 :type '(repeat (list string))
259 :group 'makefile)
260
261 ;; Note that the first big subexpression is used by font lock. Note
262 ;; that if you change this regexp you might have to fix the imenu
263 ;; index in makefile-imenu-generic-expression.
264 (defvar makefile-dependency-regex
265 ;; Allow for two nested levels $(v1:$(v2:$(v3:a=b)=c)=d)
266 "^\\(\\(?:\\$\\(?:[({]\\(?:\\$\\(?:[({]\\(?:\\$\\(?:[^({]\\|.[^\n$#})]+?[})]\\)\\|[^\n$#)}]\\)+?[})]\\|[^({]\\)\\|[^\n$#)}]\\)+?[})]\\|[^({]\\)\\|[^\n$#:=]\\)+?\\)\\(:\\)\\(?:[ \t]*$\\|[^=\n]\\(?:[^#\n]*?;[ \t]*\\(.+\\)\\)?\\)"
267 "Regex used to find dependency lines in a makefile.")
268
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
274 (defvar makefile-dependency-skip "^:"
275 "Characters to skip to find a line that might be a dependency.")
276
277 (defvar makefile-rule-action-regex
278 "^\t[ \t]*\\([-@]*\\)[ \t]*\\(\\(?:.*\\\\\n\\)*.*\\)"
279 "Regex used to highlight rule action lines in font lock mode.")
280
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
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.
294 (defconst makefile-macroassign-regex
295 "^ *\\([^ \n\t][^:#= \t\n]*\\)[ \t]*\\(?:!=\\|[*:+]?[:?]?=\\)"
296 "Regex used to find macro assignment lines in a makefile.")
297
298 (defconst makefile-var-use-regex
299 "[^$]\\$[({]\\([-a-zA-Z0-9_.]+\\|[@%<?^+*][FD]?\\)"
300 "Regex used to find $(macro) uses in a makefile.")
301
302 (defconst makefile-ignored-files-in-pickup-regex
303 "\\(^\\..*\\)\\|\\(.*~$\\)\\|\\(.*,v$\\)\\|\\(\\.[chy]\\)"
304 "Regex for filenames that will NOT be included in the target list.")
305
306 (if (fboundp 'facemenu-unlisted-faces)
307 (add-to-list 'facemenu-unlisted-faces 'makefile-space))
308 (defvar makefile-space 'makefile-space
309 "Face to use for highlighting leading spaces in Font-Lock mode.")
310
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
346 (defun makefile-make-font-lock-keywords (var keywords space
347 &optional negation
348 &rest font-lock-keywords)
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 !=
353 (2 'makefile-shell prepend t)
354 ;; This is for after normal assignment
355 (3 'font-lock-string-face prepend t))
356
357 ;; Rule actions.
358 (makefile-match-action
359 (1 font-lock-type-face)
360 (2 'makefile-shell prepend)
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 ("[^$]\\(\\$[@%*]\\)"
372 1 'makefile-targets append)
373
374 ;; Fontify conditionals and includes.
375 (,(concat "^\\(?: [ \t]*\\)?"
376 (regexp-opt keywords t)
377 "\\>[ \t]*\\([^: \t\n#]*\\)")
378 (1 font-lock-keyword-face) (2 font-lock-variable-name-face))
379
380 ,@(if negation
381 `((,negation (1 font-lock-negation-char-face prepend)
382 (2 font-lock-negation-char-face prepend t))))
383
384 ,@(if space
385 '(;; Highlight lines that contain just whitespace.
386 ;; They can cause trouble, especially if they start with a tab.
387 ("^[ \t]+$" . makefile-space)
388
389 ;; Highlight shell comments that Make treats as commands,
390 ;; since these can fool people.
391 ("^\t+#" 0 makefile-space t)
392
393 ;; Highlight spaces that precede tabs.
394 ;; They can make a tab fail to be effective.
395 ("^\\( +\\)\t" 1 makefile-space)))
396
397 ,@font-lock-keywords
398
399 ;; Do dependencies.
400 (makefile-match-dependency
401 (1 'makefile-targets prepend)
402 (3 'makefile-shell prepend t))))
403
404 (defconst makefile-font-lock-keywords
405 (makefile-make-font-lock-keywords
406 makefile-var-use-regex
407 makefile-statements
408 t))
409
410 (defconst makefile-automake-font-lock-keywords
411 (makefile-make-font-lock-keywords
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
418 makefile-var-use-regex
419 makefile-gmake-statements
420 t
421 "^\\(?: [ \t]*\\)?if\\(n\\)\\(?:def\\|eq\\)\\>"
422
423 '("[^$]\\(\\$[({][@%*][DF][})]\\)"
424 1 'makefile-targets append)
425
426 ;; $(function ...) ${function ...}
427 '("[^$]\\$[({]\\([-a-zA-Z0-9_.]+\\s \\)"
428 1 font-lock-function-name-face prepend)
429
430 ;; $(shell ...) ${shell ...}
431 '("[^$]\\$\\([({]\\)shell[ \t]+"
432 makefile-match-function-end nil nil
433 (1 'makefile-shell prepend t))))
434
435 (defconst makefile-makepp-font-lock-keywords
436 (makefile-make-font-lock-keywords
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
442 '("[^$]\\(\\$[({]\\(?:output\\|stem\\|target\\)s?\\_>.*?[})]\\)"
443 1 'makefile-targets append)
444
445 ;; Colon modifier keywords.
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))
450
451 ;; $(function ...) $((function ...)) ${function ...} ${{function ...}}
452 '("[^$]\\$\\(?:((?\\|{{?\\)\\([-a-zA-Z0-9_.]+\\s \\)"
453 1 font-lock-function-name-face prepend)
454
455 ;; $(shell ...) $((shell ...)) ${shell ...} ${{shell ...}}
456 '("[^$]\\$\\(((?\\|{{?\\)shell\\(?:[-_]\\(?:global[-_]\\)?once\\)?[ \t]+"
457 makefile-match-function-end nil nil
458 (1 'makefile-shell prepend t))
459
460 ;; $(perl ...) $((perl ...)) ${perl ...} ${{perl ...}}
461 '("[^$]\\$\\(((?\\|{{?\\)makeperl[ \t]+"
462 makefile-match-function-end nil nil
463 (1 'makefile-makepp-perl prepend t))
464 '("[^$]\\$\\(((?\\|{{?\\)perl[ \t]+"
465 makefile-match-function-end nil nil
466 (1 'makefile-makepp-perl t t))
467
468 ;; Can we unify these with (if (match-end 1) 'prepend t)?
469 '("ifmakeperl\\s +\\(.*\\)" 1 'makefile-makepp-perl prepend)
470 '("ifperl\\s +\\(.*\\)" 1 'makefile-makepp-perl t)
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\\)+?\\)\\)\\)}"
475 (1 'makefile-makepp-perl prepend t)
476 (2 'makefile-makepp-perl prepend t))
477 '("\\<\\(?:perl\\|sub\\s +\\S +\\)\\s *\n?\\s *{\\(?:{\\s *\n?\\(\\(?:.*\n\\)+?\\)\\s *}\\|\\s *\\(\\(?:.*?\\|\n?\\(?:.*\n\\)+?\\)\\)\\)}"
478 (1 'makefile-makepp-perl t t)
479 (2 'makefile-makepp-perl t t))
480
481 ;; Statement style perl block.
482 '("perl[-_]begin\\s *\\(?:\\s #.*\\)?\n\\(\\(?:.*\n\\)+?\\)\\s *perl[-_]end\\>"
483 1 'makefile-makepp-perl t)))
484
485 (defconst makefile-bsdmake-font-lock-keywords
486 (makefile-make-font-lock-keywords
487 ;; A lot more could be done for variables here:
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)))
493
494
495 (defconst makefile-font-lock-syntactic-keywords
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 ".")))
504
505 (defvar makefile-imenu-generic-expression
506 `(("Dependencies" makefile-previous-dependency 1)
507 ("Macro Assignment" ,makefile-macroassign-regex 1))
508 "Imenu generic expression for Makefile mode. See `imenu-generic-expression'.")
509
510 ;;; ------------------------------------------------------------
511 ;;; The following configurable variables are used in the
512 ;;; up-to-date overview .
513 ;;; The standard configuration assumes that your `make' program
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
522 ;;; it. Try it with your favourite one. GNU make, System V make, and
523 ;;; Dennis Vadura's DMake have no problems.
524 ;;; Set the variable `makefile-brave-make' to the name of the
525 ;;; make utility that does this on your system.
526 ;;; To understand what this is all about see the function definition
527 ;;; of `makefile-query-by-make-minus-q' .
528 ;;; ------------------------------------------------------------
529
530 (defcustom makefile-brave-make "make"
531 "*How to invoke make, for `makefile-query-targets'.
532 This should identify a `make' command that can handle the `-q' option."
533 :type 'string
534 :group 'makefile)
535
536 (defcustom makefile-query-one-target-method 'makefile-query-by-make-minus-q
537 "*Function to call to determine whether a make target is up to date.
538 The function must satisfy this calling convention:
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
544 as a string. Depending on what you're going to do you may
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
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*"
554 "*Name of the Up-to-date overview buffer."
555 :type 'string
556 :group 'makefile)
557
558 ;;; --- end of up-to-date-overview configuration ------------------
559
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
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)
608 "The keymap that is used in Makefile mode.")
609
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)
616 (define-key makefile-browser-map "\C-n" 'makefile-browser-next-line)
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)
621 (define-key makefile-browser-map "I" 'makefile-browser-insert-selection-and-quit)
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)
626 (define-key makefile-browser-map "\C-f" 'undefined))
627
628
629 (defvar makefile-mode-syntax-table nil)
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)
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 ?\` "\" " makefile-mode-syntax-table)
641 (modify-syntax-entry ?# "< " makefile-mode-syntax-table)
642 (modify-syntax-entry ?\n "> " makefile-mode-syntax-table))
643
644
645 ;;; ------------------------------------------------------------
646 ;;; Internal variables.
647 ;;; You don't need to configure below this line.
648 ;;; ------------------------------------------------------------
649
650 (defvar makefile-target-table nil
651 "Table of all target names known for this buffer.")
652
653 (defvar makefile-macro-table nil
654 "Table of all macro names known for this buffer.")
655
656 (defvar makefile-browser-client
657 "A buffer in Makefile mode that is currently using the browser.")
658
659 (defvar makefile-browser-selection-vector nil)
660 (defvar makefile-has-prereqs nil)
661 (defvar makefile-need-target-pickup t)
662 (defvar makefile-need-macro-pickup t)
663
664 (defvar makefile-mode-hook '())
665
666 ;; Each element looks like '("GNU MAKE FUNCTION" "ARG" "ARG" ... )
667 ;; Each "ARG" is used as a prompt for a required argument.
668 (defconst makefile-gnumake-functions-alist
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")
683 ("addprefix" "Prefix" "Names")
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")
693 ("shell" "Command")))
694
695
696 ;;; ------------------------------------------------------------
697 ;;; The mode function itself.
698 ;;; ------------------------------------------------------------
699
700 ;;;###autoload
701 (defun makefile-mode ()
702 "Major mode for editing standard Makefiles.
703
704 If you are editing a file for a different make, try one of the
705 variants `makefile-automake-mode', `makefile-gmake-mode',
706 `makefile-makepp-mode' or `makefile-bsdmake-mode'. All but the
707 last should be correctly chosen based on the file name, except if
708 it is *.mk. This function ends by invoking the function(s)
709 `makefile-mode-hook'.
710
711 It is strongly recommended to use `font-lock-mode', because that
712 provides additional parsing information. This is used for
713 example to see that a rule action `echo foo: bar' is a not rule
714 dependency, despite the colon.
715
716 \\{makefile-mode-map}
717
718 In the browser, use the following keys:
719
720 \\{makefile-browser-map}
721
722 Makefile mode can be configured by modifying the following variables:
723
724 `makefile-browser-buffer-name':
725 Name of the macro- and target browser buffer.
726
727 `makefile-target-colon':
728 The string that gets appended to all target names
729 inserted by `makefile-insert-target'.
730 \":\" or \"::\" are quite common values.
731
732 `makefile-macro-assign':
733 The string that gets appended to all macro names
734 inserted by `makefile-insert-macro'.
735 The normal value should be \" = \", since this is what
736 standard make expects. However, newer makes such as dmake
737 allow a larger variety of different macro assignments, so you
738 might prefer to use \" += \" or \" := \" .
739
740 `makefile-tab-after-target-colon':
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
744 `makefile-browser-leftmost-column':
745 Number of blanks to the left of the browser selection mark.
746
747 `makefile-browser-cursor-column':
748 Column in which the cursor is positioned when it moves
749 up or down in the browser.
750
751 `makefile-browser-selected-mark':
752 String used to mark selected entries in the browser.
753
754 `makefile-browser-unselected-mark':
755 String used to mark unselected entries in the browser.
756
757 `makefile-browser-auto-advance-after-selection-p':
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
762 `makefile-pickup-everything-picks-up-filenames-p':
763 If this variable is set to a non-nil value then
764 `makefile-pickup-everything' also picks up filenames as targets
765 (i.e. it calls `makefile-pickup-filenames-as-targets'), otherwise
766 filenames are omitted.
767
768 `makefile-cleanup-continuations':
769 If this variable is set to a non-nil value then Makefile mode
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.
774 IMPORTANT: Please note that enabling this option causes Makefile mode
775 to MODIFY A FILE WITHOUT YOUR CONFIRMATION when \"it seems necessary\".
776
777 `makefile-browser-hook':
778 A function or list of functions to be called just before the
779 browser is entered. This is executed in the makefile buffer.
780
781 `makefile-special-targets-list':
782 List of special targets. You will be offered to complete
783 on one of those in the minibuffer whenever you enter a `.'.
784 at the beginning of a line in Makefile mode."
785
786 (interactive)
787 (kill-all-local-variables)
788 (add-hook 'write-file-functions
789 'makefile-warn-suspicious-lines nil t)
790 (add-hook 'write-file-functions
791 'makefile-warn-continuations nil t)
792 (add-hook 'write-file-functions
793 'makefile-cleanup-continuations nil t)
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)
799
800 ;; Font lock.
801 (make-local-variable 'font-lock-defaults)
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.
806 '(makefile-font-lock-keywords
807 nil nil
808 ((?$ . "."))
809 backward-paragraph
810 (font-lock-syntactic-keywords . makefile-font-lock-syntactic-keywords)
811 (font-lock-support-mode))) ; JIT breaks on long series of continuation lines.
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.
818 (make-local-variable 'imenu-generic-expression)
819 (setq imenu-generic-expression makefile-imenu-generic-expression)
820
821 ;; Dabbrev.
822 (make-local-variable 'dabbrev-abbrev-skip-leading-regexp)
823 (setq dabbrev-abbrev-skip-leading-regexp "\\$")
824
825 ;; Other abbrevs.
826 (setq local-abbrev-table makefile-mode-abbrev-table)
827
828 ;; Filling.
829 (make-local-variable 'fill-paragraph-function)
830 (setq fill-paragraph-function 'makefile-fill-paragraph)
831
832 ;; Comment stuff.
833 (make-local-variable 'comment-start)
834 (setq comment-start "#")
835 (make-local-variable 'comment-end)
836 (setq comment-end "")
837 (make-local-variable 'comment-start-skip)
838 (setq comment-start-skip "#+[ \t]*")
839
840 ;; Make sure TAB really inserts \t.
841 (set (make-local-variable 'indent-line-function) 'indent-to-left-margin)
842
843 ;; become the current major mode
844 (setq major-mode 'makefile-mode)
845 (setq mode-name "Makefile")
846
847 ;; Activate keymap and syntax table.
848 (use-local-map makefile-mode-map)
849 (set-syntax-table makefile-mode-syntax-table)
850
851 ;; Real TABs are important in makefiles
852 (setq indent-tabs-mode t)
853 (run-mode-hooks 'makefile-mode-hook))
854
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."
871 (set (make-local-variable 'makefile-rule-action-regex)
872 makefile-makepp-rule-action-regex)
873 (setq font-lock-defaults
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)))
878
879 ;;;###autoload
880 (define-derived-mode makefile-bsdmake-mode makefile-mode "BSDmakefile"
881 "An adapted `makefile-mode' that knows about BSD make."
882 (set (make-local-variable 'makefile-dependency-regex)
883 makefile-bsdmake-dependency-regex)
884 (set (make-local-variable 'makefile-dependency-skip) "^:!")
885 (set (make-local-variable 'makefile-rule-action-regex)
886 makefile-bsdmake-rule-action-regex)
887 (setq font-lock-defaults
888 `(makefile-bsdmake-font-lock-keywords ,@(cdr font-lock-defaults))))
889
890 \f
891
892 ;;; Motion code.
893
894 (defun makefile-next-dependency ()
895 "Move point to the beginning of the next dependency line."
896 (interactive)
897 (let ((here (point)))
898 (end-of-line)
899 (if (makefile-match-dependency nil)
900 (progn (beginning-of-line) t) ; indicate success
901 (goto-char here) nil)))
902
903 (defun makefile-previous-dependency ()
904 "Move point to the beginning of the previous dependency line."
905 (interactive)
906 (let ((pt (point)))
907 (beginning-of-line)
908 ;; makefile-match-dependency done backwards:
909 (catch 'found
910 (while (progn (skip-chars-backward makefile-dependency-skip)
911 (not (bobp)))
912 (or (prog1 (eq (char-after) ?=)
913 (backward-char))
914 (get-text-property (point) 'face)
915 (beginning-of-line)
916 (if (> (point) (+ (point-min) 2))
917 (eq (char-before (1- (point))) ?\\))
918 (if (looking-at makefile-dependency-regex)
919 (throw 'found t))))
920 (goto-char pt)
921 nil)))
922
923 \f
924
925 ;;; Electric keys. Blech.
926
927 (defun makefile-electric-dot (arg)
928 "Prompt for the name of a special target to insert.
929 Only does electric insertion at beginning of line.
930 Anywhere else just self-inserts."
931 (interactive "p")
932 (if (bolp)
933 (makefile-insert-special-target)
934 (self-insert-command arg)))
935
936 (defun makefile-insert-special-target ()
937 "Prompt for and insert a special target name.
938 Uses `makefile-special-targets' list."
939 (interactive)
940 (makefile-pickup-targets)
941 (let ((special-target
942 (completing-read "Special target: "
943 makefile-special-targets-list nil nil nil)))
944 (if (zerop (length special-target))
945 ()
946 (insert "." special-target ":")
947 (makefile-forward-after-target-colon))))
948
949 (defun makefile-electric-equal (arg)
950 "Prompt for name of a macro to insert.
951 Only does prompting if point is at beginning of line.
952 Anywhere else just self-inserts."
953 (interactive "p")
954 (makefile-pickup-macros)
955 (if (bolp)
956 (call-interactively 'makefile-insert-macro)
957 (self-insert-command arg)))
958
959 (defun makefile-insert-macro (macro-name)
960 "Prepare definition of a new macro."
961 (interactive "sMacro Name: ")
962 (makefile-pickup-macros)
963 (if (not (zerop (length macro-name)))
964 (progn
965 (beginning-of-line)
966 (insert macro-name makefile-macro-assign)
967 (setq makefile-need-macro-pickup t)
968 (makefile-remember-macro macro-name))))
969
970 (defun makefile-insert-macro-ref (macro-name)
971 "Complete on a list of known macros, then insert complete ref at point."
972 (interactive
973 (list
974 (progn
975 (makefile-pickup-macros)
976 (completing-read "Refer to macro: " makefile-macro-table nil nil nil))))
977 (makefile-do-macro-insertion macro-name))
978
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)
985 (insert target-name makefile-target-colon)
986 (makefile-forward-after-target-colon)
987 (end-of-line)
988 (setq makefile-need-target-pickup t)
989 (makefile-remember-target target-name))))
990
991 (defun makefile-insert-target-ref (target-name)
992 "Complete on a list of known targets, then insert TARGET-NAME at point."
993 (interactive
994 (list
995 (progn
996 (makefile-pickup-targets)
997 (completing-read "Refer to target: " makefile-target-table nil nil nil))))
998 (if (not (zerop (length target-name)))
999 (insert target-name " ")))
1000
1001 (defun makefile-electric-colon (arg)
1002 "Prompt for name of new target.
1003 Prompting only happens at beginning of line.
1004 Anywhere else just self-inserts."
1005 (interactive "p")
1006 (if (bolp)
1007 (call-interactively 'makefile-insert-target)
1008 (self-insert-command arg)))
1009
1010 \f
1011
1012 ;;; ------------------------------------------------------------
1013 ;;; Extracting targets and macros from an existing makefile
1014 ;;; ------------------------------------------------------------
1015
1016 (defun makefile-pickup-targets ()
1017 "Notice names of all target definitions in Makefile."
1018 (interactive)
1019 (when makefile-need-target-pickup
1020 (setq makefile-need-target-pickup nil
1021 makefile-target-table nil
1022 makefile-has-prereqs nil)
1023 (save-excursion
1024 (goto-char (point-min))
1025 (while (makefile-match-dependency nil)
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))))
1032 (has-prereqs
1033 (not (looking-at ":[ \t]*$"))))
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.")))
1041
1042 (defun makefile-pickup-macros ()
1043 "Notice names of all macro definitions in Makefile."
1044 (interactive)
1045 (when makefile-need-macro-pickup
1046 (setq makefile-need-macro-pickup nil
1047 makefile-macro-table nil)
1048 (save-excursion
1049 (goto-char (point-min))
1050 (while (re-search-forward makefile-macroassign-regex nil t)
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)))
1060 (message "Read macros OK.")))
1061
1062 (defun makefile-pickup-everything (arg)
1063 "Notice names of all macros and targets in Makefile.
1064 Prefix arg means force pickups to be redone."
1065 (interactive "P")
1066 (if arg
1067 (setq makefile-need-target-pickup t
1068 makefile-need-macro-pickup t))
1069 (makefile-pickup-macros)
1070 (makefile-pickup-targets)
1071 (if makefile-pickup-everything-picks-up-filenames-p
1072 (makefile-pickup-filenames-as-targets)))
1073
1074 (defun makefile-pickup-filenames-as-targets ()
1075 "Scan the current directory for filenames to use as targets.
1076 Checks each filename against `makefile-ignored-files-in-pickup-regex'
1077 and adds all qualifying names to the list of known targets."
1078 (interactive)
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)) ""))))
1085
1086 \f
1087
1088 ;;; Completion.
1089
1090 (defun makefile-complete ()
1091 "Perform completion on Makefile construct preceding point.
1092 Can complete variable and target names.
1093 The 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
1183 ;; Backslashification. Stolen from cc-mode.el.
1184
1185 (defun makefile-backslash-region (from to delete-flag)
1186 "Insert, align, or delete end-of-line backslashes on the lines in the region.
1187 With no argument, inserts backslashes and aligns existing backslashes.
1188 With an argument, deletes the backslashes.
1189
1190 This function does not modify the last line of the region if the region ends
1191 right at the start of the following line; it does not modify blank lines
1192 at the start of the region. So you can put the region around an entire macro
1193 definition and conveniently use this command."
1194 (interactive "r\nP")
1195 (save-excursion
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
1264 ((looking-at "^#+")
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.
1268 (let ((fill-prefix (match-string-no-properties 0))
1269 (fill-paragraph-function nil))
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))))
1288
1289 ;; Must look for backslashed-region before looking for variable
1290 ;; assignment.
1291 ((or (eq (char-before (line-end-position 1)) ?\\)
1292 (eq (char-before (line-end-position 0)) ?\\))
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.
1321 (save-restriction
1322 (narrow-to-region (point) (line-beginning-position 2))
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)
1329
1330 \f
1331
1332 ;;; ------------------------------------------------------------
1333 ;;; Browser mode.
1334 ;;; ------------------------------------------------------------
1335
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)
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)))
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 ()
1398 "Leave the browser and return to the makefile buffer."
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)
1403 (quit-window t)
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)
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)))
1421 (delete-region (point) (progn (end-of-line) (point)))
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)))
1427 (delete-region (point) (progn (end-of-line) (point)))
1428 (insert
1429 (makefile-browser-format-target-line
1430 target-name
1431 (makefile-browser-get-state-for-line this-line))))))
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 ()
1442 "Insert a makefile continuation.
1443 In the makefile buffer, go to (end-of-line), insert a \'\\\'
1444 character, insert a new blank line, go to that line and indent by one TAB.
1445 This is most useful in the process of creating continued lines when copying
1446 large dependencies from the browser to the client buffer.
1447 \(point) advances accordingly in the client buffer."
1448 (interactive)
1449 (with-current-buffer makefile-browser-client
1450 (end-of-line)
1451 (insert "\\\n\t")))
1452
1453 (defun makefile-browser-insert-selection ()
1454 "Insert all selected targets and/or macros in the makefile buffer.
1455 Insertion takes place at point."
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
1482 (defun makefile-browser-start-interaction ()
1483 (use-local-map makefile-browser-map)
1484 (setq buffer-read-only t))
1485
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)
1494 (makefile-browser-fill targets macros)
1495 (shrink-window-if-larger-than-buffer)
1496 (set (make-local-variable 'makefile-browser-selection-vector)
1497 (make-vector (+ (length targets) (length macros)) nil))
1498 (makefile-browser-start-interaction))))
1499
1500 (defun makefile-switch-to-browser ()
1501 (interactive)
1502 (run-hooks 'makefile-browser-hook)
1503 (setq makefile-browser-client (current-buffer))
1504 (makefile-pickup-targets)
1505 (makefile-pickup-macros)
1506 (makefile-browse makefile-target-table makefile-macro-table))
1507
1508 \f
1509
1510 ;;; ------------------------------------------------------------
1511 ;;; Up-to-date overview buffer
1512 ;;; ------------------------------------------------------------
1513
1514 (defun makefile-create-up-to-date-overview ()
1515 "Create a buffer containing an overview of the state of all known targets.
1516 Known targets are targets that are explicitly defined in that makefile;
1517 in other words, all targets that appear on the left hand side of a
1518 dependency 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
1538 (makefile-pickup-targets)
1539 makefile-target-table))
1540 (prereqs makefile-has-prereqs)
1541 )
1542
1543 (set-buffer makefile-up-to-date-buffer)
1544 (setq buffer-read-only nil)
1545 (erase-buffer)
1546 (makefile-query-targets filename real-targets prereqs)
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))
1556 (shrink-window-if-larger-than-buffer)
1557 (sort-lines nil (point-min) (point-max))
1558 (setq buffer-read-only t))))))
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 ()
1567 "Create a filename suitable for use in `makefile-save-temporary'.
1568 Be careful to allow brain-dead file systems (DOS, SYSV ...) to cope
1569 with the generated name!"
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
1581 (defun makefile-query-targets (filename target-table prereq-list)
1582 "Fill the up-to-date overview buffer.
1583 Checks each target in TARGET-TABLE using `makefile-query-one-target-method'
1584 and generates the overview, one line per target name."
1585 (insert
1586 (mapconcat
1587 (function (lambda (item)
1588 (let* ((target-name (car item))
1589 (no-prereqs (not (member target-name prereq-list)))
1590 (needs-rebuild (or no-prereqs
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"))
1602 (goto-char (point-min))
1603 (delete-file filename)) ; remove the tmpfile
1604
1605 (defun makefile-query-by-make-minus-q (target &optional filename)
1606 (not (eq 0
1607 (call-process makefile-brave-make nil nil nil
1608 "-f" filename "-q" target))))
1609
1610 \f
1611
1612 ;;; ------------------------------------------------------------
1613 ;;; Continuation cleanup
1614 ;;; ------------------------------------------------------------
1615
1616 (defun makefile-cleanup-continuations ()
1617 (if (eq major-mode 'makefile-mode)
1618 (if (and makefile-cleanup-continuations
1619 (not buffer-read-only))
1620 (save-excursion
1621 (goto-char (point-min))
1622 (while (re-search-forward "\\\\[ \t]+$" nil t)
1623 (replace-match "\\" t t))))))
1624
1625
1626 ;;; ------------------------------------------------------------
1627 ;;; Warn of suspicious lines
1628 ;;; ------------------------------------------------------------
1629
1630 (defun makefile-warn-suspicious-lines ()
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
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))
1644 (if (re-search-forward "\\\\[ \t]+$" nil t)
1645 (not (y-or-n-p
1646 (format "Suspicious continuation in line %d. Save anyway? "
1647 (count-lines (point-min) (point)))))))))
1648 \f
1649
1650 ;;; ------------------------------------------------------------
1651 ;;; GNU make function support
1652 ;;; ------------------------------------------------------------
1653
1654 (defun makefile-insert-gmake-function ()
1655 "Insert a GNU make function call.
1656 Asks for the name of the function to use (with completion).
1657 Then prompts for all required parameters."
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)
1675 (read-string (format "[%s] %s: " function-name one-prompt)
1676 nil)))
1677 prompt-list
1678 ","))
1679
1680 \f
1681
1682 ;;; ------------------------------------------------------------
1683 ;;; Utility functions
1684 ;;; ------------------------------------------------------------
1685
1686 (defun makefile-match-function-end (end)
1687 "To be called as an anchored matcher by font-lock.
1688 The 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
1696 (defun makefile-match-dependency (bound)
1697 "Search for `makefile-dependency-regex' up to BOUND.
1698 Checks that the colon has not already been fontified, else we
1699 matched in a rule action."
1700 (catch 'found
1701 (let ((pt (point)))
1702 (while (progn (skip-chars-forward makefile-dependency-skip bound)
1703 (< (point) (or bound (point-max))))
1704 (forward-char)
1705 (or (eq (char-after) ?=)
1706 (get-text-property (1- (point)) 'face)
1707 (if (> (line-beginning-position) (+ (point-min) 2))
1708 (eq (char-before (line-end-position 0)) ?\\))
1709 (when (save-excursion
1710 (beginning-of-line)
1711 (looking-at makefile-dependency-regex))
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)))
1725 (end-of-line)
1726 (throw 'found (point)))))
1727 (goto-char pt))
1728 nil))
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
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
1743 (defun makefile-remember-target (target-name &optional has-prereqs)
1744 "Remember a given target if it is not already remembered for this buffer."
1745 (if (not (zerop (length target-name)))
1746 (progn
1747 (if (not (assoc target-name makefile-target-table))
1748 (setq makefile-target-table
1749 (cons (list target-name) makefile-target-table)))
1750 (if has-prereqs
1751 (setq makefile-has-prereqs
1752 (cons target-name makefile-has-prereqs))))))
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 ()
1762 "Move point forward after inserting the terminating colon of a target.
1763 This acts according to the value of `makefile-tab-after-target-colon'."
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)
1772 (re-search-forward "\\$[{(]" (line-end-position) t)))
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")
1779 (buffer-substring (point) (1- (line-end-position)))))
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)
1785 (re-search-forward "\\$[{(]" (line-end-position) t)
1786 (let ((macro-start (point)))
1787 (skip-chars-forward "^})")
1788 (buffer-substring macro-start (point)))))
1789
1790 (defun makefile-format-macro-ref (macro-name)
1791 "Format a macro reference.
1792 Uses `makefile-use-curly-braces-for-macros-p'."
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))))
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
1809 (defun makefile-last-line-p ()
1810 (= (line-end-position) (point-max)))
1811
1812 (defun makefile-first-line-p ()
1813 (= (line-beginning-position) (point-min)))
1814
1815 \f
1816
1817 ;;; Support for other packages, like add-log.
1818
1819 (defun makefile-add-log-defun ()
1820 "Return name of target or variable assignment that point is in.
1821 If it isn't in one, return nil."
1822 (save-excursion
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.
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)))
1836
1837 (provide 'make-mode)
1838
1839 ;;; arch-tag: bd23545a-de91-44fb-b1b2-feafbb2635a0
1840 ;;; make-mode.el ends here