(rmail-last-rmail-file): Initialize to a file name.
[bpt/emacs.git] / lisp / progmodes / make-mode.el
1 ;;; makefile.el --- makefile editing commands for Emacs
2
3 ;; Copyright (C) 1992 Free Software Foundation, Inc.
4
5 ;; Author: Thomas Neumann <tom@smart.bo.open.de>
6 ;; Eric S. Raymond <esr@snark.thyrsus.com>
7 ;; Adapted-By: ESR
8 ;; Keywords: unix, tools
9
10 ;; RMS:
11 ;; This needs work. The electric characters are too obnoxious.
12 ;; It should not define C-c LETTER.
13 ;; It should support knowing the list of existing macros and targets
14 ;; via M-TAB completion, not by preempting insertion of references.
15 ;; Also, the doc strings need fixing: the first line doesn't stand alone,
16 ;; and other usage is not high quality. Symbol names don't have `...'.
17 ;; The Mode names is written as makefile-mode instead of Makefile mode.
18
19 ;; So, for the meantime, this is not the default mode for makefiles.
20
21 ;; $Id: makefile.el,v 1.8 1993/05/06 01:30:26 rms Exp jimb $
22
23 ;; This file is part of GNU Emacs.
24
25 ;; GNU Emacs is free software; you can redistribute it and/or modify
26 ;; it under the terms of the GNU General Public License as published by
27 ;; the Free Software Foundation; either version 1, or (at your option)
28 ;; any later version.
29
30 ;; GNU Emacs is distributed in the hope that it will be useful,
31 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
32 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
33 ;; GNU General Public License for more details.
34
35 ;; You should have received a copy of the GNU General Public License
36 ;; along with GNU Emacs; see the file COPYING. If not, write to
37 ;; the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
38
39 ;;; Commentary:
40
41 ;; A major mode for editing makefiles. The mode knows about Makefile
42 ;; syntax and defines M-n and M-p to move to next and previous productions.
43 ;;
44 ;; The keys $, =, : and . are electric; they try to help you fill in a
45 ;; macro reference, macro definition, ordinary target name, or special
46 ;; target name, respectively. Such names are completed using a list of
47 ;; targets and macro names parsed out of the makefile. This list is
48 ;; automatically updated, if necessary, whenever you invoke one of
49 ;; these commands. You can force it to be updated with C-c C-p.
50 ;;
51 ;; The command C-c f adds certain filenames in the current directory
52 ;; as targets. You can filter out filenames by setting the variable
53 ;; makefile-ignored-files-in-pickup-regex.
54 ;;
55 ;; The command C-c C-u grinds for a bit, then pops up a report buffer
56 ;; showing which target names are up-to-date with respect to their
57 ;; prerequisites, which targets are out-of-date, and which have no
58 ;; prerequisites.
59 ;;
60 ;; The command C-c b pops up a browser window listing all target and
61 ;; macro names. You can mark or unmark items wit C-c SPC, and insert
62 ;; all marked items back in the Makefile with C-c TAB.
63 ;;
64 ;; The command C-c TAB in the makefile buffer inserts a GNU make builtin.
65 ;; You will be prompted for the builtin's args.
66 ;;
67 ;; There are numerous other customization variables.
68
69 ;;; Code:
70
71 (provide 'makefile)
72
73 ;;; ------------------------------------------------------------
74 ;;; Configurable stuff
75 ;;; ------------------------------------------------------------
76
77 (defconst makefile-mode-name "makefile"
78 "The \"pretty name\" of makefile-mode, as it appears in the modeline.")
79
80 (defvar makefile-browser-buffer-name "*Macros and Targets*"
81 "Name of the macro- and target browser buffer.")
82
83 (defvar makefile-target-colon ":"
84 "The string that gets appended to all target names
85 inserted by makefile-insert-target.
86 \":\" or \"::\" are quite common values.")
87
88 (defvar makefile-macro-assign " = "
89 "The string that gets appended to all macro names
90 inserted by makefile-insert-macro.
91 The normal value should be \" = \", since this is what
92 standard make expects. However, newer makes such as dmake
93 allow a larger variety of different macro assignments, so you
94 might prefer to use \" += \" or \" := \" .")
95
96 (defvar makefile-use-curly-braces-for-macros-p nil
97 "Controls the style of generated macro references.
98 Set this variable to a non-nil value if you prefer curly braces
99 in macro-references, so it looks like ${this}. A value of nil
100 will cause makefile-mode to use parentheses, making macro references
101 look like $(this) .")
102
103 (defvar makefile-tab-after-target-colon t
104 "If you want a TAB (instead of a space) to be appended after the
105 target colon, then set this to a non-nil value.")
106
107 (defvar makefile-browser-leftmost-column 10
108 "Number of blanks to the left of the browser selection mark.")
109
110 (defvar makefile-browser-cursor-column 10
111 "Column in which the cursor is positioned when it moves
112 up or down in the browser.")
113
114 (defvar makefile-browser-selected-mark "+ "
115 "String used to mark selected entries in the browser.")
116
117 (defvar makefile-browser-unselected-mark " "
118 "String used to mark unselected entries in the browser.")
119
120 (defvar makefile-browser-auto-advance-after-selection-p t
121 "If non-nil, the cursor will automagically advance to the next line after
122 an item has been selected in the browser.")
123
124 (defvar makefile-pickup-everything-picks-up-filenames-p nil
125 "If non-nil, makefile-pickup-everything also picks up filenames as targets
126 (i.e. it calls makefile-find-filenames-as-targets), otherwise filenames are
127 omitted.")
128
129 (defvar makefile-cleanup-continuations-p t
130 "If non-nil, makefile-mode will assure that no line in the file ends with a
131 backslash (the continuation character) followed by any whitespace. This is
132 done by silently removing the trailing whitespace, leaving the backslash itself
133 intact. IMPORTANT: Please note that enabling this option causes makefile-mode
134 to MODIFY A FILE WITHOUT YOUR CONFIRMATION when \'it seems necessary\'.")
135
136 (defvar makefile-browser-hook '()
137 "A function or list of functions to be called just before the
138 browser is entered. This is executed in the makefile buffer.")
139
140 ;;
141 ;; Special targets for DMake, Sun's make ...
142 ;;
143 (defvar makefile-special-targets-list
144 '(("DEFAULT") ("DONE") ("ERROR") ("EXPORT")
145 ("FAILED") ("GROUPEPILOG") ("GROUPPROLOG") ("IGNORE")
146 ("IMPORT") ("INCLUDE") ("INCLUDEDIRS") ("INIT")
147 ("KEEP_STATE") ("MAKEFILES") ("MAKE_VERSION") ("NO_PARALLEL")
148 ("PARALLEL") ("PHONY") ("PRECIOUS") ("REMOVE")
149 ("SCCS_GET") ("SILENT") ("SOURCE") ("SUFFIXES")
150 ("WAIT") ("c.o") ("C.o") ("m.o")
151 ("el.elc") ("y.c") ("s.o"))
152 "List of special targets. You will be offered to complete
153 on one of those in the minibuffer whenever you enter a \".\"
154 at the beginning of a line in makefile-mode.")
155
156 (defvar makefile-runtime-macros-list
157 '(("@") ("&") (">") ("<") ("*") ("^") ("?") ("%"))
158 "List of macros that are resolved by make at runtime.
159 If you insert a macro reference using makefile-insert-macro-ref, the name
160 of the macro is checked against this list. If it can be found its name will
161 not be enclosed in { } or ( ).")
162
163 (defconst makefile-dependency-regex
164 "^[^ \t#:]+\\([ \t]+[^ \t#:]+\\)*[ \t]*:\\([ \t]*$\\|\\([^=\n].*$\\)\\)"
165 "Regex used to find dependency lines in a makefile.")
166
167 (defconst makefile-macroassign-regex
168 "^[^ \t][^:#=]*[\\*:\\+]?:?=.*$"
169 "Regex used to find macro assignment lines in a makefile.")
170
171 (defconst makefile-ignored-files-in-pickup-regex
172 "\\(^\\..*\\)\\|\\(.*~$\\)\\|\\(.*,v$\\)\\|(\\.[chy]\\)"
173 "Regex for filenames that will NOT be included in the target list.")
174
175 ;;; ------------------------------------------------------------
176 ;;; The following configurable variables are used in the
177 ;;; up-to-date overview .
178 ;;; The standard configuration assumes that your `make' program
179 ;;; can be run in question/query mode using the `-q' option, this
180 ;;; means that the command
181 ;;;
182 ;;; make -q foo
183 ;;;
184 ;;; should return an exit status of zero if the target `foo' is
185 ;;; up to date and a nonzero exit status otherwise.
186 ;;; Many makes can do this although the docs/manpages do not mention
187 ;;; it. Try it with your favourite one. GNU make, System V make, and
188 ;;; Dennis Vadura's DMake have no problems.
189 ;;; Set the variable `makefile-brave-make' to the name of the
190 ;;; make utility that does this on your system.
191 ;;; To understand what this is all about see the function definition
192 ;;; of `makefile-query-by-make-minus-q' .
193 ;;; ------------------------------------------------------------
194
195 (defvar makefile-brave-make "make"
196 "A make that can handle the \'-q\' option.")
197
198 (defvar makefile-query-one-target-method 'makefile-query-by-make-minus-q
199 "A function symbol [one that can be used as the first argument to
200 funcall] that provides a function that must conform to the following
201 interface:
202
203 * As its first argument, it must accept the name of the target to
204 be checked, as a string.
205
206 * As its second argument, it may accept the name of a makefile
207 as a string. Depending on what you're going to do you may
208 not need this.
209
210 * It must return the integer value 0 (zero) if the given target
211 should be considered up-to-date in the context of the given
212 makefile, any nonzero integer value otherwise.")
213
214 (defvar makefile-up-to-date-buffer-name "*Makefile Up-to-date overview*"
215 "Name of the Up-to-date overview buffer.")
216
217 ;;; --- end of up-to-date-overview configuration ------------------
218
219 (defvar makefile-mode-map nil
220 "The keymap that is used in makefile-mode.")
221 (if makefile-mode-map
222 ()
223 (setq makefile-mode-map (make-sparse-keymap))
224 ;; set up the keymap
225 (define-key makefile-mode-map "$" 'makefile-insert-macro-ref)
226 (define-key makefile-mode-map "\C-c:" 'makefile-insert-target-ref)
227 (define-key makefile-mode-map ":" 'makefile-electric-colon)
228 (define-key makefile-mode-map "=" 'makefile-electric-equal)
229 (define-key makefile-mode-map "." 'makefile-electric-dot)
230 (define-key makefile-mode-map "\C-c\C-f" 'makefile-pickup-filenames-as-targets)
231 (define-key makefile-mode-map "\C-c\C-b" 'makefile-switch-to-browser)
232 (define-key makefile-mode-map "\C-c\C-p" 'makefile-pickup-everything)
233 (define-key makefile-mode-map "\C-c\C-u" 'makefile-create-up-to-date-overview)
234 (define-key makefile-mode-map "\C-c\C-i" 'makefile-insert-gmake-function)
235 (define-key makefile-mode-map "\M-p" 'makefile-previous-dependency)
236 (define-key makefile-mode-map "\M-n" 'makefile-next-dependency))
237
238 (defvar makefile-browser-map nil
239 "The keymap that is used in the macro- and target browser.")
240 (if makefile-browser-map
241 ()
242 (setq makefile-browser-map (make-sparse-keymap))
243 (define-key makefile-browser-map "n" 'makefile-browser-next-line)
244 (define-key makefile-browser-map "\C-n" 'makefile-browser-next-line)
245 (define-key makefile-browser-map "p" 'makefile-browser-previous-line)
246 (define-key makefile-browser-map "\C-p" 'makefile-browser-previous-line)
247 (define-key makefile-browser-map " " 'makefile-browser-toggle)
248 (define-key makefile-browser-map "i" 'makefile-browser-insert-selection)
249 (define-key makefile-browser-map "I" 'makefile-browser-insert-selection-and-quit)
250 (define-key makefile-browser-map "\C-c\C-m" 'makefile-browser-insert-continuation)
251 (define-key makefile-browser-map "q" 'makefile-browser-quit)
252 ;; disable horizontal movement
253 (define-key makefile-browser-map "\C-b" 'undefined)
254 (define-key makefile-browser-map "\C-f" 'undefined))
255
256
257 (defvar makefile-mode-syntax-table nil
258 "The syntax-table used in makefile mode.")
259 (if makefile-mode-syntax-table
260 ()
261 (setq makefile-mode-syntax-table (make-syntax-table))
262 (modify-syntax-entry ?\( "() " makefile-mode-syntax-table)
263 (modify-syntax-entry ?\) ")( " makefile-mode-syntax-table)
264 (modify-syntax-entry ?\[ "(] " makefile-mode-syntax-table)
265 (modify-syntax-entry ?\] "([ " makefile-mode-syntax-table)
266 (modify-syntax-entry ?\{ "(} " makefile-mode-syntax-table)
267 (modify-syntax-entry ?\} "){ " makefile-mode-syntax-table)
268 (modify-syntax-entry ?# "< " makefile-mode-syntax-table)
269 (modify-syntax-entry ?\n "> " makefile-mode-syntax-table))
270
271
272 ;;; ------------------------------------------------------------
273 ;;; Internal variables.
274 ;;; You don't need to configure below this line.
275 ;;; ------------------------------------------------------------
276
277 (defvar makefile-target-table nil
278 "Table of all targets that have been inserted in
279 this Makefile buffer using makefile-insert-target or picked up
280 using makefile-pickup-targets.")
281
282 (defvar makefile-macro-table nil
283 "Table of all macros that have been iserted in
284 this Makefile buffer using makefile-insert-macro or picked up
285 using makefile-pickup-macros.")
286
287 (defvar makefile-browser-client
288 "A buffer in makefile-mode that is currently using the browser.")
289
290 (defvar makefile-browser-selection-vector nil)
291 (defvar makefile-has-prereqs nil)
292 (defvar makefile-need-target-pickup t)
293 (defvar makefile-need-macro-pickup t)
294
295 (defvar makefile-mode-hook '())
296
297 (defconst makefile-gnumake-functions-alist
298 '(
299 ;; Text functions
300 ("subst" "From" "To" "In")
301 ("patsubst" "Pattern" "Replacement" "In")
302 ("strip" "Text")
303 ("findstring" "Find what" "In")
304 ("filter" "Pattern" "Text")
305 ("filter-out" "Pattern" "Text")
306 ("sort" "List")
307 ;; Filename functions
308 ("dir" "Names")
309 ("notdir" "Names")
310 ("suffix" "Names")
311 ("basename" "Names")
312 ("addsuffix" "Suffix" "Names")
313 ("join" "List 1" "List 2")
314 ("word" "Index" "Text")
315 ("words" "Text")
316 ("firstword" "Text")
317 ("wildcard" "Pattern")
318 ;; Misc functions
319 ("foreach" "Variable" "List" "Text")
320 ("origin" "Variable")
321 ("shell" "Command"))
322 "A list of GNU make 3.62 function names associated with
323 the prompts for each function.
324 This is used in the function makefile-insert-gmake-function .")
325
326
327 ;;; ------------------------------------------------------------
328 ;;; The mode function itself.
329 ;;; ------------------------------------------------------------
330
331 ;;;###autoload
332 (defun makefile-mode ()
333 "Major mode for editing Makefiles.
334 Calling this function invokes the function(s) \"makefile-mode-hook\" before
335 doing anything else.
336
337 \\{makefile-mode-map}
338
339 In the browser, use the following keys:
340
341 \\{makefile-browser-map}
342
343 makefile-mode can be configured by modifying the following
344 variables:
345
346 makefile-mode-name:
347 The \"pretty name\" of makefile-mode, as it
348 appears in the modeline.
349
350 makefile-browser-buffer-name:
351 Name of the macro- and target browser buffer.
352
353 makefile-target-colon:
354 The string that gets appended to all target names
355 inserted by makefile-insert-target.
356 \":\" or \"::\" are quite common values.
357
358 makefile-macro-assign:
359 The string that gets appended to all macro names
360 inserted by makefile-insert-macro.
361 The normal value should be \" = \", since this is what
362 standard make expects. However, newer makes such as dmake
363 allow a larger variety of different macro assignments, so you
364 might prefer to use \" += \" or \" := \" .
365
366 makefile-tab-after-target-colon:
367 If you want a TAB (instead of a space) to be appended after the
368 target colon, then set this to a non-nil value.
369
370 makefile-browser-leftmost-column:
371 Number of blanks to the left of the browser selection mark.
372
373 makefile-browser-cursor-column:
374 Column in which the cursor is positioned when it moves
375 up or down in the browser.
376
377 makefile-browser-selected-mark:
378 String used to mark selected entries in the browser.
379
380 makefile-browser-unselected-mark:
381 String used to mark unselected entries in the browser.
382
383 makefile-browser-auto-advance-after-selection-p:
384 If this variable is set to a non-nil value the cursor
385 will automagically advance to the next line after an item
386 has been selected in the browser.
387
388 makefile-pickup-everything-picks-up-filenames-p:
389 If this variable is set to a non-nil value then
390 makefile-pickup-everything also picks up filenames as targets
391 (i.e. it calls makefile-find-filenames-as-targets), otherwise
392 filenames are omitted.
393
394 makefile-cleanup-continuations-p:
395 If this variable is set to a non-nil value then makefile-mode
396 will assure that no line in the file ends with a backslash
397 (the continuation character) followed by any whitespace.
398 This is done by silently removing the trailing whitespace, leaving
399 the backslash itself intact.
400 IMPORTANT: Please note that enabling this option causes makefile-mode
401 to MODIFY A FILE WITHOUT YOUR CONFIRMATION when \'it seems necessary\'.
402
403 makefile-browser-hook:
404 A function or list of functions to be called just before the
405 browser is entered. This is executed in the makefile buffer.
406
407 makefile-special-targets-list:
408 List of special targets. You will be offered to complete
409 on one of those in the minibuffer whenever you enter a \".\"
410 at the beginning of a line in makefile-mode."
411 (interactive)
412 (kill-all-local-variables)
413 (if (not (memq 'makefile-cleanup-continuations write-file-hooks))
414 (setq write-file-hooks
415 (append write-file-hooks (list 'makefile-cleanup-continuations))))
416 (make-variable-buffer-local 'makefile-target-table)
417 (make-variable-buffer-local 'makefile-macro-table)
418 (make-variable-buffer-local 'makefile-has-prereqs)
419 (make-variable-buffer-local 'makefile-need-target-pickup)
420 (make-variable-buffer-local 'makefile-need-macro-pickup)
421 (setq comment-start "#")
422 (setq comment-end "")
423 (setq comment-start-skip "#[ \t]*")
424 ;; become the current major mode
425 (setq major-mode 'makefile-mode)
426 (setq mode-name makefile-mode-name)
427 ;; activate keymap
428 (use-local-map makefile-mode-map)
429 (set-syntax-table makefile-mode-syntax-table)
430 (run-hooks 'makefile-mode-hook))
431
432 (defun makefile-next-dependency ()
433 "Move (point) to the beginning of the next dependency line below (point)."
434 (interactive)
435 (let ((here (point)))
436 (end-of-line)
437 (if (re-search-forward makefile-dependency-regex (point-max) t)
438 (progn (beginning-of-line) t) ; indicate success
439 (goto-char here) nil)))
440
441 (defun makefile-previous-dependency ()
442 "Move (point) to the beginning of the next dependency line above (point)."
443 (interactive)
444 (let ((here (point)))
445 (beginning-of-line)
446 (if (re-search-backward makefile-dependency-regex (point-min) t)
447 (progn (beginning-of-line) t) ; indicate success
448 (goto-char here) nil)))
449
450
451 ;;; Stuff below here depends on the pickup state
452
453 (defun makefile-electric-dot ()
454 "At (bol), offer completion on makefile-special-targets-list.
455 Anywhere else just insert a dot."
456 (interactive)
457 (if (bolp)
458 (makefile-insert-special-target)
459 (insert ".")))
460
461 (defun makefile-insert-special-target ()
462 "Complete on makefile-special-targets-list, insert result at (point)."
463 (interactive)
464 (makefile-pickup-targets)
465 (let
466 ((special-target
467 (completing-read "Special target: "
468 makefile-special-targets-list nil nil nil)))
469 (if (zerop (length special-target))
470 ()
471 (insert (format ".%s:" special-target))
472 (makefile-forward-after-target-colon))))
473
474 (defun makefile-electric-equal ()
475 "At (bol) do makefile-insert-macro. Anywhere else just self-insert."
476 (interactive)
477 (makefile-pickup-macros)
478 (if (bolp)
479 (call-interactively 'makefile-insert-macro)
480 (insert "=")))
481
482 (defun makefile-insert-macro (macro-name)
483 "Prepare definition of a new macro."
484 (interactive "sMacro Name: ")
485 (makefile-pickup-macros)
486 (if (not (zerop (length macro-name)))
487 (progn
488 (beginning-of-line)
489 (insert (format "%s%s" macro-name makefile-macro-assign))
490 (setq makefile-need-macro-pickup t)
491 (makefile-remember-macro macro-name))))
492
493 (defun makefile-insert-macro-ref (macro-name)
494 "Complete on a list of known macros, then insert complete ref at (point)."
495 (interactive
496 (list
497 (progn
498 (makefile-pickup-macros)
499 (completing-read "Refer to macro: " makefile-macro-table nil nil nil))))
500 (if (not (zerop (length macro-name)))
501 (if (assoc macro-name makefile-runtime-macros-list)
502 (insert (format "$%s " macro-name))
503 (insert (makefile-format-macro-ref macro-name) " "))))
504
505 (defun makefile-insert-target (target-name)
506 "Prepare definition of a new target (dependency line)."
507 (interactive "sTarget: ")
508 (if (not (zerop (length target-name)))
509 (progn
510 (beginning-of-line)
511 (insert (format "%s%s" target-name makefile-target-colon))
512 (makefile-forward-after-target-colon)
513 (end-of-line)
514 (setq makefile-need-target-pickup t)
515 (makefile-remember-target target-name))))
516
517 (defun makefile-insert-target-ref (target-name)
518 "Complete on a list of known targets, then insert target-ref at (point) ."
519 (interactive
520 (list
521 (pogn
522 (makefile-pickup-targets)
523 (completing-read "Refer to target: " makefile-target-table nil nil nil))))
524 (if (not (zerop (length target-name)))
525 (progn
526 (insert (format "%s " target-name)))))
527
528 (defun makefile-electric-colon ()
529 "At (bol) defines a new target, anywhere else just self-insert ."
530 (interactive)
531 (if (bolp)
532 (call-interactively 'makefile-insert-target)
533 (insert ":")))
534
535 ;;; ------------------------------------------------------------
536 ;;; Extracting targets and macros from an existing makefile
537 ;;; ------------------------------------------------------------
538
539 (defun makefile-pickup-targets ()
540 "Scan a buffer that contains a makefile for target definitions (dependencies)
541 and add them to the list of known targets."
542 (interactive)
543 (if (not makefile-need-target-pickup)
544 nil
545 (setq makefile-need-target-pickup nil)
546 (setq makefile-target-table nil)
547 (setq makefile-has-prereqs nil)
548 (save-excursion
549 (goto-char (point-min))
550 (while (re-search-forward makefile-dependency-regex (point-max) t)
551 (makefile-add-this-line-targets)))
552 (message "Read targets OK.")))
553
554 (defun makefile-add-this-line-targets ()
555 (save-excursion
556 (beginning-of-line)
557 (let ((done-with-line nil)
558 (line-number (1+ (count-lines (point-min) (point)))))
559 (while (not done-with-line)
560 (skip-chars-forward " \t")
561 (if (not (setq done-with-line (or (eolp)
562 (char-equal (char-after (point)) ?:))))
563 (progn
564 (let* ((start-of-target-name (point))
565 (target-name
566 (progn
567 (skip-chars-forward "^ \t:#")
568 (buffer-substring start-of-target-name (point))))
569 (has-prereqs
570 (not (looking-at ":[ \t]*$"))))
571 (if (makefile-remember-target target-name has-prereqs)
572 (message "Picked up target \"%s\" from line %d"
573 target-name line-number)))))))))
574
575
576 (defun makefile-pickup-macros ()
577 "Scan a buffer that contains a makefile for macro definitions
578 and add them to the list of known macros."
579 (interactive)
580 (if (not makefile-need-macro-pickup)
581 nil
582 (setq makefile-need-macro-pickup nil)
583 (setq makefile-macro-table nil)
584 (save-excursion
585 (goto-char (point-min))
586 (while (re-search-forward makefile-macroassign-regex (point-max) t)
587 (makefile-add-this-line-macro)
588 (forward-line 1)))
589 (message "Read macros OK.")))
590
591 (defun makefile-add-this-line-macro ()
592 (save-excursion
593 (beginning-of-line)
594 (skip-chars-forward " \t")
595 (if (not (eolp))
596 (let* ((start-of-macro-name (point))
597 (line-number (1+ (count-lines (point-min) (point))))
598 (macro-name (progn
599 (skip-chars-forward "^ \t:#=*")
600 (buffer-substring start-of-macro-name (point)))))
601 (if (makefile-remember-macro macro-name)
602 (message "Picked up macro \"%s\" from line %d"
603 macro-name line-number))))))
604
605 (defun makefile-pickup-everything ()
606 "Calls makefile-pickup-targets and makefile-pickup-macros.
607 See their documentation for what they do."
608 (interactive)
609 (makefile-pickup-macros)
610 (makefile-pickup-targets)
611 (if makefile-pickup-everything-picks-up-filenames-p
612 (makefile-pickup-filenames-as-targets)))
613
614
615 (defun makefile-pickup-filenames-as-targets ()
616 "Scan the current directory for filenames, check each filename
617 against makefile-ignored-files-in-pickup-regex and add all qualifying
618 names to the list of known targets."
619 (interactive)
620 (let* ((dir (file-name-directory (buffer-file-name)))
621 (raw-filename-list (if dir
622 (file-name-all-completions "" dir)
623 (file-name-all-completions "" ""))))
624 (mapcar '(lambda (name)
625 (if (and (not (file-directory-p name))
626 (not (string-match makefile-ignored-files-in-pickup-regex
627 name)))
628 (if (makefile-remember-target name)
629 (message "Picked up file \"%s\" as target" name))))
630 raw-filename-list)))
631
632 ;;; ------------------------------------------------------------
633 ;;; The browser window
634 ;;; ------------------------------------------------------------
635
636 (defun makefile-browser-format-target-line (target selected)
637 (format
638 (concat (make-string makefile-browser-leftmost-column ?\ )
639 (if selected
640 makefile-browser-selected-mark
641 makefile-browser-unselected-mark)
642 "%s%s")
643 target makefile-target-colon))
644
645 (defun makefile-browser-format-macro-line (macro selected)
646 (format
647 (concat (make-string makefile-browser-leftmost-column ?\ )
648 (if selected
649 makefile-browser-selected-mark
650 makefile-browser-unselected-mark)
651 (makefile-format-macro-ref macro))))
652
653 (defun makefile-browser-fill (targets macros)
654 (setq buffer-read-only nil)
655 (goto-char (point-min))
656 (erase-buffer)
657 (mapconcat
658 (function
659 (lambda (item) (insert (makefile-browser-format-target-line (car item) nil) "\n")))
660 targets
661 "")
662 (mapconcat
663 (function
664 (lambda (item) (insert (makefile-browser-format-macro-line (car item) nil) "\n")))
665 macros
666 "")
667 (sort-lines nil (point-min) (point-max))
668 (goto-char (1- (point-max)))
669 (delete-char 1) ; remove unnecessary newline at eob
670 (goto-char (point-min))
671 (forward-char makefile-browser-cursor-column)
672 (setq buffer-read-only t))
673
674 ;;;
675 ;;; Moving up and down in the browser
676 ;;;
677
678 (defun makefile-browser-next-line ()
679 "Move the browser selection cursor to the next line."
680 (interactive)
681 (if (not (makefile-last-line-p))
682 (progn
683 (forward-line 1)
684 (forward-char makefile-browser-cursor-column))))
685
686 (defun makefile-browser-previous-line ()
687 "Move the browser selection cursor to the previous line."
688 (interactive)
689 (if (not (makefile-first-line-p))
690 (progn
691 (forward-line -1)
692 (forward-char makefile-browser-cursor-column))))
693
694 ;;;
695 ;;; Quitting the browser (returns to client buffer)
696 ;;;
697
698 (defun makefile-browser-quit ()
699 "Leave the makefile-browser-buffer and return to the buffer
700 from that it has been entered."
701 (interactive)
702 (let ((my-client makefile-browser-client))
703 (setq makefile-browser-client nil) ; we quitted, so NO client!
704 (set-buffer-modified-p nil)
705 (kill-buffer (current-buffer))
706 (pop-to-buffer my-client)))
707
708 ;;;
709 ;;; Toggle state of a browser item
710 ;;;
711
712 (defun makefile-browser-toggle ()
713 "Toggle the selection state of the browser item at the cursor position."
714 (interactive)
715 (let ((this-line (count-lines (point-min) (point))))
716 (setq this-line (max 1 this-line))
717 (makefile-browser-toggle-state-for-line this-line)
718 (goto-line this-line)
719 (setq buffer-read-only nil)
720 (beginning-of-line)
721 (if (makefile-browser-on-macro-line-p)
722 (let ((macro-name (makefile-browser-this-line-macro-name)))
723 (kill-line)
724 (insert
725 (makefile-browser-format-macro-line
726 macro-name
727 (makefile-browser-get-state-for-line this-line))))
728 (let ((target-name (makefile-browser-this-line-target-name)))
729 (kill-line)
730 (insert
731 (makefile-browser-format-target-line
732 target-name
733 (makefile-browser-get-state-for-line this-line)))))
734 (setq buffer-read-only t)
735 (beginning-of-line)
736 (forward-char makefile-browser-cursor-column)
737 (if makefile-browser-auto-advance-after-selection-p
738 (makefile-browser-next-line))))
739
740 ;;;
741 ;;; Making insertions into the client buffer
742 ;;;
743
744 (defun makefile-browser-insert-continuation ()
745 "Insert a makefile continuation.
746 In the browser\'s client buffer, go to (end-of-line), insert a \'\\\'
747 character, insert a new blank line, go to that line and indent by one TAB.
748 This is most useful in the process of creating continued lines when copying
749 large dependencies from the browser to the client buffer.
750 (point) advances accordingly in the client buffer."
751 (interactive)
752 (save-excursion
753 (set-buffer makefile-browser-client)
754 (end-of-line)
755 (insert "\\\n\t")))
756
757 (defun makefile-browser-insert-selection ()
758 "Insert all browser-selected targets and/or macros in the browser\'s
759 client buffer.
760 Insertion takes place at (point)."
761 (interactive)
762 (save-excursion
763 (goto-line 1)
764 (let ((current-line 1))
765 (while (not (eobp))
766 (if (makefile-browser-get-state-for-line current-line)
767 (makefile-browser-send-this-line-item))
768 (forward-line 1)
769 (setq current-line (1+ current-line))))))
770
771 (defun makefile-browser-insert-selection-and-quit ()
772 (interactive)
773 (makefile-browser-insert-selection)
774 (makefile-browser-quit))
775
776 (defun makefile-browser-send-this-line-item ()
777 (if (makefile-browser-on-macro-line-p)
778 (save-excursion
779 (let ((macro-name (makefile-browser-this-line-macro-name)))
780 (set-buffer makefile-browser-client)
781 (insert (makefile-format-macro-ref macro-name) " ")))
782 (save-excursion
783 (let ((target-name (makefile-browser-this-line-target-name)))
784 (set-buffer makefile-browser-client)
785 (insert target-name " ")))))
786
787 (defun makefile-browser-start-interaction ()
788 (use-local-map makefile-browser-map)
789 (setq buffer-read-only t))
790
791 (defun makefile-browse (targets macros)
792 (interactive)
793 (if (zerop (+ (length targets) (length macros)))
794 (progn
795 (beep)
796 (message "No macros or targets to browse! Consider running 'makefile-pickup-everything\'"))
797 (let ((browser-buffer (get-buffer-create makefile-browser-buffer-name)))
798 (pop-to-buffer browser-buffer)
799 (make-variable-buffer-local 'makefile-browser-selection-vector)
800 (makefile-browser-fill targets macros)
801 (shrink-window-if-larger-than-buffer)
802 (setq makefile-browser-selection-vector
803 (make-vector (+ (length targets) (length macros)) nil))
804 (makefile-browser-start-interaction))))
805
806 (defun makefile-switch-to-browser ()
807 (interactive)
808 (run-hooks 'makefile-browser-hook)
809 (setq makefile-browser-client (current-buffer))
810 (makefile-pickup-targets)
811 (makefile-pickup-macros)
812 (makefile-browse makefile-target-table makefile-macro-table))
813
814
815 ;;; ------------------------------------------------------------
816 ;;; Up-to-date overview buffer
817 ;;; ------------------------------------------------------------
818
819 (defun makefile-create-up-to-date-overview ()
820 "Create a buffer containing an overview of the state of all known targets.
821 Known targets are targets that are explicitly defined in that makefile;
822 in other words, all targets that appear on the left hand side of a
823 dependency in the makefile."
824 (interactive)
825 (if (y-or-n-p "Are you sure that the makefile being edited is consistent? ")
826 ;;
827 ;; The rest of this function operates on a temporary makefile, created by
828 ;; writing the current contents of the makefile buffer.
829 ;;
830 (let ((saved-target-table makefile-target-table)
831 (this-buffer (current-buffer))
832 (makefile-up-to-date-buffer
833 (get-buffer-create makefile-up-to-date-buffer-name))
834 (filename (makefile-save-temporary))
835 ;;
836 ;; Forget the target table because it may contain picked-up filenames
837 ;; that are not really targets in the current makefile.
838 ;; We don't want to query these, so get a new target-table with just the
839 ;; targets that can be found in the makefile buffer.
840 ;; The 'old' target table will be restored later.
841 ;;
842 (real-targets (progn
843 (makefile-pickup-targets)
844 makefile-target-table))
845 (prereqs makefile-has-prereqs)
846 )
847
848 (set-buffer makefile-up-to-date-buffer)
849 (setq buffer-read-only nil)
850 (erase-buffer)
851 (makefile-query-targets filename real-targets prereqs)
852 (if (zerop (buffer-size)) ; if it did not get us anything
853 (progn
854 (kill-buffer (current-buffer))
855 (message "No overview created!")))
856 (set-buffer this-buffer)
857 (setq makefile-target-table saved-target-table)
858 (if (get-buffer makefile-up-to-date-buffer-name)
859 (progn
860 (pop-to-buffer (get-buffer makefile-up-to-date-buffer-name))
861 (shrink-window-if-larger-than-buffer)
862 (sort-lines nil (point-min) (point-max))
863 (setq buffer-read-only t))))))
864
865 (defun makefile-save-temporary ()
866 "Create a temporary file from the current makefile buffer."
867 (let ((filename (makefile-generate-temporary-filename)))
868 (write-region (point-min) (point-max) filename nil 0)
869 filename)) ; return the filename
870
871 (defun makefile-generate-temporary-filename ()
872 "Create a filename suitable for use in makefile-save-temporary.
873 Be careful to allow brain-dead file systems (DOS, SYSV ...) to cope
874 with the generated name !"
875 (let ((my-name (user-login-name))
876 (my-uid (int-to-string (user-uid))))
877 (concat "mktmp"
878 (if (> (length my-name) 3)
879 (substring my-name 0 3)
880 my-name)
881 "."
882 (if (> (length my-uid) 3)
883 (substring my-uid 0 3)
884 my-uid))))
885
886 (defun makefile-query-targets (filename target-table prereq-list)
887 "This function fills the up-to-date-overview-buffer.
888 It checks each target in target-table using makefile-query-one-target-method
889 and generates the overview, one line per target name."
890 (insert
891 (mapconcat
892 (function (lambda (item)
893 (let* ((target-name (car item))
894 (no-prereqs (not (member target-name prereq-list)))
895 (needs-rebuild (or no-prereqs
896 (funcall
897 makefile-query-one-target-method
898 target-name
899 filename))))
900 (format "\t%s%s"
901 target-name
902 (cond (no-prereqs " .. has no prerequisites")
903 (needs-rebuild " .. NEEDS REBUILD")
904 (t " .. is up to date"))))
905 ))
906 target-table "\n"))
907 (goto-char (point-min))
908 (delete-file filename)) ; remove the tmpfile
909
910 (defun makefile-query-by-make-minus-q (target &optional filename)
911 (not (zerop (call-process makefile-brave-make nil nil nil "-f" filename "-q" target))))
912
913 ;;; ------------------------------------------------------------
914 ;;; Continuation cleanup
915 ;;; ------------------------------------------------------------
916
917 (defun makefile-cleanup-continuations ()
918 (if (eq major-mode 'makefile-mode)
919 (if (and makefile-cleanup-continuations-p
920 (not buffer-read-only))
921 (save-excursion
922 (goto-char (point-min))
923 (while (re-search-forward "\\\\[ \t]+$" (point-max) t)
924 (replace-match "\\" t t))))))
925
926 ;;; ------------------------------------------------------------
927 ;;; GNU make function support
928 ;;; ------------------------------------------------------------
929
930 (defun makefile-insert-gmake-function ()
931 "This function is intended to help you using the numerous
932 macro-like \'function calls\' of GNU make.
933 It will ask you for the name of the function you wish to
934 use (with completion), then, after you selected the function,
935 it will prompt you for all required parameters.
936 This function \'knows\' about the required parameters of every
937 GNU make function and will use meaningfull prompts for the
938 various args, making it much easier to take advantage of this
939 powerful GNU make feature."
940 (interactive)
941 (let* ((gm-function-name (completing-read
942 "Function: "
943 makefile-gnumake-functions-alist
944 nil t nil))
945 (gm-function-prompts
946 (cdr (assoc gm-function-name makefile-gnumake-functions-alist))))
947 (if (not (zerop (length gm-function-name)))
948 (insert (makefile-format-macro-ref
949 (concat gm-function-name " "
950 (makefile-prompt-for-gmake-funargs
951 gm-function-name gm-function-prompts)))
952 " "))))
953
954 (defun makefile-prompt-for-gmake-funargs (function-name prompt-list)
955 (mapconcat
956 (function (lambda (one-prompt)
957 (read-string (format "[%s] %s: " function-name one-prompt) nil)))
958 prompt-list
959 ","))
960
961
962
963 ;;; ------------------------------------------------------------
964 ;;; Utility functions
965 ;;; ------------------------------------------------------------
966
967 (defun makefile-remember-target (target-name &optional has-prereqs)
968 "Remember a given target if it is not already remembered for this buffer."
969 (if (not (zerop (length target-name)))
970 (progn
971 (if (not (assoc target-name makefile-target-table))
972 (setq makefile-target-table
973 (cons (list target-name) makefile-target-table)))
974 (if has-prereqs
975 (setq makefile-has-prereqs
976 (cons target-name makefile-has-prereqs))))))
977
978 (defun makefile-remember-macro (macro-name)
979 "Remember a given macro if it is not already remembered for this buffer."
980 (if (not (zerop (length macro-name)))
981 (if (not (assoc macro-name makefile-macro-table))
982 (setq makefile-macro-table
983 (cons (list macro-name) makefile-macro-table)))))
984
985 (defun makefile-forward-after-target-colon ()
986 "Move point forward after the terminating colon
987 of a target has been inserted.
988 This accts according to the value of makefile-tab-after-target-colon ."
989 (if makefile-tab-after-target-colon
990 (insert "\t")
991 (insert " ")))
992
993 (defun makefile-browser-on-macro-line-p ()
994 "Determine if point is on a macro line in the browser."
995 (save-excursion
996 (beginning-of-line)
997 (re-search-forward "\\$[{(]" (makefile-end-of-line-point) t)))
998
999 (defun makefile-browser-this-line-target-name ()
1000 "Extract the target name from a line in the browser."
1001 (save-excursion
1002 (end-of-line)
1003 (skip-chars-backward "^ \t")
1004 (buffer-substring (point) (1- (makefile-end-of-line-point)))))
1005
1006 (defun makefile-browser-this-line-macro-name ()
1007 "Extract the macro name from a line in the browser."
1008 (save-excursion
1009 (beginning-of-line)
1010 (re-search-forward "\\$[{(]" (makefile-end-of-line-point) t)
1011 (let ((macro-start (point)))
1012 (skip-chars-forward "^})")
1013 (buffer-substring macro-start (point)))))
1014
1015 (defun makefile-format-macro-ref (macro-name)
1016 "Format a macro reference according to the value of the
1017 configuration variable makefile-use-curly-braces-for-macros-p ."
1018 (if makefile-use-curly-braces-for-macros-p
1019 (format "${%s}" macro-name)
1020 (format "$(%s)" macro-name)))
1021
1022 (defun makefile-browser-get-state-for-line (n)
1023 (aref makefile-browser-selection-vector (1- n)))
1024
1025 (defun makefile-browser-set-state-for-line (n to-state)
1026 (aset makefile-browser-selection-vector (1- n) to-state))
1027
1028 (defun makefile-browser-toggle-state-for-line (n)
1029 (makefile-browser-set-state-for-line n (not (makefile-browser-get-state-for-line n))))
1030
1031 (defun makefile-beginning-of-line-point ()
1032 (save-excursion
1033 (beginning-of-line)
1034 (point)))
1035
1036 (defun makefile-end-of-line-point ()
1037 (save-excursion
1038 (end-of-line)
1039 (point)))
1040
1041 (defun makefile-last-line-p ()
1042 (= (makefile-end-of-line-point) (point-max)))
1043
1044 (defun makefile-first-line-p ()
1045 (= (makefile-beginning-of-line-point) (point-min)))
1046
1047 ;; makefile.el ends here