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