1 ;;; makefile.el --- makefile editing commands for Emacs
3 ;; Copyright (C) 1992 Free Software Foundation, Inc.
5 ;; Author: Thomas Neumann <tom@smart.bo.open.de>
6 ;; Eric S. Raymond <esr@snark.thyrsus.com>
8 ;; Keywords: unix, tools
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.
19 ;; So, for the meantime, this is not the default mode for makefiles.
21 ;; $Id: makefile.el,v 1.8 1993/05/06 01:30:26 rms Exp jimb $
23 ;; This file is part of GNU Emacs.
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)
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.
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.
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.
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.
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.
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
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.
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.
67 ;; There are numerous other customization variables.
73 ;;; ------------------------------------------------------------
74 ;;; Configurable stuff
75 ;;; ------------------------------------------------------------
77 (defconst makefile-mode-name
"makefile"
78 "The \"pretty name\" of makefile-mode, as it appears in the modeline.")
80 (defvar makefile-browser-buffer-name
"*Macros and Targets*"
81 "Name of the macro- and target browser buffer.")
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.")
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 \" := \" .")
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) .")
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.")
107 (defvar makefile-browser-leftmost-column
10
108 "Number of blanks to the left of the browser selection mark.")
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.")
114 (defvar makefile-browser-selected-mark
"+ "
115 "String used to mark selected entries in the browser.")
117 (defvar makefile-browser-unselected-mark
" "
118 "String used to mark unselected entries in the browser.")
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.")
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
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\'.")
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.")
141 ;; Special targets for DMake, Sun's make ...
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.")
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 ( ).")
163 (defconst makefile-dependency-regex
164 "^[^ \t#:]+\\([ \t]+[^ \t#:]+\\)*[ \t]*:\\([ \t]*$\\|\\([^=\n].*$\\)\\)"
165 "Regex used to find dependency lines in a makefile.")
167 (defconst makefile-macroassign-regex
168 "^[^ \t][^:#=]*[\\*:\\+]?:?=.*$"
169 "Regex used to find macro assignment lines in a makefile.")
171 (defconst makefile-ignored-files-in-pickup-regex
172 "\\(^\\..*\\)\\|\\(.*~$\\)\\|\\(.*,v$\\)\\|(\\.[chy]\\)"
173 "Regex for filenames that will NOT be included in the target list.")
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
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 ;;; ------------------------------------------------------------
195 (defvar makefile-brave-make
"make"
196 "A make that can handle the \'-q\' option.")
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
203 * As its first argument, it must accept the name of the target to
204 be checked, as a string.
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
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.")
214 (defvar makefile-up-to-date-buffer-name
"*Makefile Up-to-date overview*"
215 "Name of the Up-to-date overview buffer.")
217 ;;; --- end of up-to-date-overview configuration ------------------
219 (defvar makefile-mode-map nil
220 "The keymap that is used in makefile-mode.")
221 (if makefile-mode-map
223 (setq makefile-mode-map
(make-sparse-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
))
238 (defvar makefile-browser-map nil
239 "The keymap that is used in the macro- and target browser.")
240 (if makefile-browser-map
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
))
257 (defvar makefile-mode-syntax-table nil
258 "The syntax-table used in makefile mode.")
259 (if makefile-mode-syntax-table
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
))
272 ;;; ------------------------------------------------------------
273 ;;; Internal variables.
274 ;;; You don't need to configure below this line.
275 ;;; ------------------------------------------------------------
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.")
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.")
287 (defvar makefile-browser-client
288 "A buffer in makefile-mode that is currently using the browser.")
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
)
295 (defvar makefile-mode-hook
'())
297 (defconst makefile-gnumake-functions-alist
300 ("subst" "From" "To" "In")
301 ("patsubst" "Pattern" "Replacement" "In")
303 ("findstring" "Find what" "In")
304 ("filter" "Pattern" "Text")
305 ("filter-out" "Pattern" "Text")
307 ;; Filename functions
312 ("addsuffix" "Suffix" "Names")
313 ("join" "List 1" "List 2")
314 ("word" "Index" "Text")
317 ("wildcard" "Pattern")
319 ("foreach" "Variable" "List" "Text")
320 ("origin" "Variable")
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 .")
327 ;;; ------------------------------------------------------------
328 ;;; The mode function itself.
329 ;;; ------------------------------------------------------------
332 (defun makefile-mode ()
333 "Major mode for editing Makefiles.
334 Calling this function invokes the function(s) \"makefile-mode-hook\" before
337 \\{makefile-mode-map}
339 In the browser, use the following keys:
341 \\{makefile-browser-map}
343 makefile-mode can be configured by modifying the following
347 The \"pretty name\" of makefile-mode, as it
348 appears in the modeline.
350 makefile-browser-buffer-name:
351 Name of the macro- and target browser buffer.
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.
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 \" := \" .
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.
370 makefile-browser-leftmost-column:
371 Number of blanks to the left of the browser selection mark.
373 makefile-browser-cursor-column:
374 Column in which the cursor is positioned when it moves
375 up or down in the browser.
377 makefile-browser-selected-mark:
378 String used to mark selected entries in the browser.
380 makefile-browser-unselected-mark:
381 String used to mark unselected entries in the browser.
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.
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.
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\'.
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.
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."
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
)
428 (use-local-map makefile-mode-map
)
429 (set-syntax-table makefile-mode-syntax-table
)
430 (run-hooks 'makefile-mode-hook
))
432 (defun makefile-next-dependency ()
433 "Move (point) to the beginning of the next dependency line below (point)."
435 (let ((here (point)))
437 (if (re-search-forward makefile-dependency-regex
(point-max) t
)
438 (progn (beginning-of-line) t
) ; indicate success
439 (goto-char here
) nil
)))
441 (defun makefile-previous-dependency ()
442 "Move (point) to the beginning of the next dependency line above (point)."
444 (let ((here (point)))
446 (if (re-search-backward makefile-dependency-regex
(point-min) t
)
447 (progn (beginning-of-line) t
) ; indicate success
448 (goto-char here
) nil
)))
451 ;;; Stuff below here depends on the pickup state
453 (defun makefile-electric-dot ()
454 "At (bol), offer completion on makefile-special-targets-list.
455 Anywhere else just insert a dot."
458 (makefile-insert-special-target)
461 (defun makefile-insert-special-target ()
462 "Complete on makefile-special-targets-list, insert result at (point)."
464 (makefile-pickup-targets)
467 (completing-read "Special target: "
468 makefile-special-targets-list nil nil nil
)))
469 (if (zerop (length special-target
))
471 (insert (format ".%s:" special-target
))
472 (makefile-forward-after-target-colon))))
474 (defun makefile-electric-equal ()
475 "At (bol) do makefile-insert-macro. Anywhere else just self-insert."
477 (makefile-pickup-macros)
479 (call-interactively 'makefile-insert-macro
)
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
)))
489 (insert (format "%s%s" macro-name makefile-macro-assign
))
490 (setq makefile-need-macro-pickup t
)
491 (makefile-remember-macro macro-name
))))
493 (defun makefile-insert-macro-ref (macro-name)
494 "Complete on a list of known macros, then insert complete ref at (point)."
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
) " "))))
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
)))
511 (insert (format "%s%s" target-name makefile-target-colon
))
512 (makefile-forward-after-target-colon)
514 (setq makefile-need-target-pickup t
)
515 (makefile-remember-target target-name
))))
517 (defun makefile-insert-target-ref (target-name)
518 "Complete on a list of known targets, then insert target-ref at (point) ."
522 (makefile-pickup-targets)
523 (completing-read "Refer to target: " makefile-target-table nil nil nil
))))
524 (if (not (zerop (length target-name
)))
526 (insert (format "%s " target-name
)))))
528 (defun makefile-electric-colon ()
529 "At (bol) defines a new target, anywhere else just self-insert ."
532 (call-interactively 'makefile-insert-target
)
535 ;;; ------------------------------------------------------------
536 ;;; Extracting targets and macros from an existing makefile
537 ;;; ------------------------------------------------------------
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."
543 (if (not makefile-need-target-pickup
)
545 (setq makefile-need-target-pickup nil
)
546 (setq makefile-target-table nil
)
547 (setq makefile-has-prereqs nil
)
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.")))
554 (defun makefile-add-this-line-targets ()
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)) ?
:))))
564 (let* ((start-of-target-name (point))
567 (skip-chars-forward "^ \t:#")
568 (buffer-substring start-of-target-name
(point))))
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
)))))))))
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."
580 (if (not makefile-need-macro-pickup
)
582 (setq makefile-need-macro-pickup nil
)
583 (setq makefile-macro-table nil
)
585 (goto-char (point-min))
586 (while (re-search-forward makefile-macroassign-regex
(point-max) t
)
587 (makefile-add-this-line-macro)
589 (message "Read macros OK.")))
591 (defun makefile-add-this-line-macro ()
594 (skip-chars-forward " \t")
596 (let* ((start-of-macro-name (point))
597 (line-number (1+ (count-lines (point-min) (point))))
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
))))))
605 (defun makefile-pickup-everything ()
606 "Calls makefile-pickup-targets and makefile-pickup-macros.
607 See their documentation for what they do."
609 (makefile-pickup-macros)
610 (makefile-pickup-targets)
611 (if makefile-pickup-everything-picks-up-filenames-p
612 (makefile-pickup-filenames-as-targets)))
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."
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
628 (if (makefile-remember-target name
)
629 (message "Picked up file \"%s\" as target" name
))))
632 ;;; ------------------------------------------------------------
633 ;;; The browser window
634 ;;; ------------------------------------------------------------
636 (defun makefile-browser-format-target-line (target selected
)
638 (concat (make-string makefile-browser-leftmost-column ?\
)
640 makefile-browser-selected-mark
641 makefile-browser-unselected-mark
)
643 target makefile-target-colon
))
645 (defun makefile-browser-format-macro-line (macro selected
)
647 (concat (make-string makefile-browser-leftmost-column ?\
)
649 makefile-browser-selected-mark
650 makefile-browser-unselected-mark
)
651 (makefile-format-macro-ref macro
))))
653 (defun makefile-browser-fill (targets macros
)
654 (setq buffer-read-only nil
)
655 (goto-char (point-min))
659 (lambda (item) (insert (makefile-browser-format-target-line (car item
) nil
) "\n")))
664 (lambda (item) (insert (makefile-browser-format-macro-line (car item
) nil
) "\n")))
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
))
675 ;;; Moving up and down in the browser
678 (defun makefile-browser-next-line ()
679 "Move the browser selection cursor to the next line."
681 (if (not (makefile-last-line-p))
684 (forward-char makefile-browser-cursor-column
))))
686 (defun makefile-browser-previous-line ()
687 "Move the browser selection cursor to the previous line."
689 (if (not (makefile-first-line-p))
692 (forward-char makefile-browser-cursor-column
))))
695 ;;; Quitting the browser (returns to client buffer)
698 (defun makefile-browser-quit ()
699 "Leave the makefile-browser-buffer and return to the buffer
700 from that it has been entered."
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
)))
709 ;;; Toggle state of a browser item
712 (defun makefile-browser-toggle ()
713 "Toggle the selection state of the browser item at the cursor position."
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
)
721 (if (makefile-browser-on-macro-line-p)
722 (let ((macro-name (makefile-browser-this-line-macro-name)))
725 (makefile-browser-format-macro-line
727 (makefile-browser-get-state-for-line this-line
))))
728 (let ((target-name (makefile-browser-this-line-target-name)))
731 (makefile-browser-format-target-line
733 (makefile-browser-get-state-for-line this-line
)))))
734 (setq buffer-read-only t
)
736 (forward-char makefile-browser-cursor-column
)
737 (if makefile-browser-auto-advance-after-selection-p
738 (makefile-browser-next-line))))
741 ;;; Making insertions into the client buffer
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."
753 (set-buffer makefile-browser-client
)
757 (defun makefile-browser-insert-selection ()
758 "Insert all browser-selected targets and/or macros in the browser\'s
760 Insertion takes place at (point)."
764 (let ((current-line 1))
766 (if (makefile-browser-get-state-for-line current-line
)
767 (makefile-browser-send-this-line-item))
769 (setq current-line
(1+ current-line
))))))
771 (defun makefile-browser-insert-selection-and-quit ()
773 (makefile-browser-insert-selection)
774 (makefile-browser-quit))
776 (defun makefile-browser-send-this-line-item ()
777 (if (makefile-browser-on-macro-line-p)
779 (let ((macro-name (makefile-browser-this-line-macro-name)))
780 (set-buffer makefile-browser-client
)
781 (insert (makefile-format-macro-ref macro-name
) " ")))
783 (let ((target-name (makefile-browser-this-line-target-name)))
784 (set-buffer makefile-browser-client
)
785 (insert target-name
" ")))))
787 (defun makefile-browser-start-interaction ()
788 (use-local-map makefile-browser-map
)
789 (setq buffer-read-only t
))
791 (defun makefile-browse (targets macros
)
793 (if (zerop (+ (length targets
) (length macros
)))
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))))
806 (defun makefile-switch-to-browser ()
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
))
815 ;;; ------------------------------------------------------------
816 ;;; Up-to-date overview buffer
817 ;;; ------------------------------------------------------------
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."
825 (if (y-or-n-p "Are you sure that the makefile being edited is consistent? ")
827 ;; The rest of this function operates on a temporary makefile, created by
828 ;; writing the current contents of the makefile buffer.
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))
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.
843 (makefile-pickup-targets)
844 makefile-target-table
))
845 (prereqs makefile-has-prereqs
)
848 (set-buffer makefile-up-to-date-buffer
)
849 (setq buffer-read-only nil
)
851 (makefile-query-targets filename real-targets prereqs
)
852 (if (zerop (buffer-size)) ; if it did not get us anything
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
)
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
))))))
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
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))))
878 (if (> (length my-name
) 3)
879 (substring my-name
0 3)
882 (if (> (length my-uid
) 3)
883 (substring my-uid
0 3)
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."
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
897 makefile-query-one-target-method
902 (cond (no-prereqs " .. has no prerequisites")
903 (needs-rebuild " .. NEEDS REBUILD")
904 (t " .. is up to date"))))
907 (goto-char (point-min))
908 (delete-file filename
)) ; remove the tmpfile
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
))))
913 ;;; ------------------------------------------------------------
914 ;;; Continuation cleanup
915 ;;; ------------------------------------------------------------
917 (defun makefile-cleanup-continuations ()
918 (if (eq major-mode
'makefile-mode
)
919 (if (and makefile-cleanup-continuations-p
920 (not buffer-read-only
))
922 (goto-char (point-min))
923 (while (re-search-forward "\\\\[ \t]+$" (point-max) t
)
924 (replace-match "\\" t t
))))))
926 ;;; ------------------------------------------------------------
927 ;;; GNU make function support
928 ;;; ------------------------------------------------------------
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."
941 (let* ((gm-function-name (completing-read
943 makefile-gnumake-functions-alist
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
)))
954 (defun makefile-prompt-for-gmake-funargs (function-name prompt-list
)
956 (function (lambda (one-prompt)
957 (read-string (format "[%s] %s: " function-name one-prompt
) nil
)))
963 ;;; ------------------------------------------------------------
964 ;;; Utility functions
965 ;;; ------------------------------------------------------------
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
)))
971 (if (not (assoc target-name makefile-target-table
))
972 (setq makefile-target-table
973 (cons (list target-name
) makefile-target-table
)))
975 (setq makefile-has-prereqs
976 (cons target-name makefile-has-prereqs
))))))
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
)))))
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
993 (defun makefile-browser-on-macro-line-p ()
994 "Determine if point is on a macro line in the browser."
997 (re-search-forward "\\$[{(]" (makefile-end-of-line-point) t
)))
999 (defun makefile-browser-this-line-target-name ()
1000 "Extract the target name from a line in the browser."
1003 (skip-chars-backward "^ \t")
1004 (buffer-substring (point) (1- (makefile-end-of-line-point)))))
1006 (defun makefile-browser-this-line-macro-name ()
1007 "Extract the macro name from a line in the browser."
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)))))
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
)))
1022 (defun makefile-browser-get-state-for-line (n)
1023 (aref makefile-browser-selection-vector
(1- n
)))
1025 (defun makefile-browser-set-state-for-line (n to-state
)
1026 (aset makefile-browser-selection-vector
(1- n
) to-state
))
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
))))
1031 (defun makefile-beginning-of-line-point ()
1036 (defun makefile-end-of-line-point ()
1041 (defun makefile-last-line-p ()
1042 (= (makefile-end-of-line-point) (point-max)))
1044 (defun makefile-first-line-p ()
1045 (= (makefile-beginning-of-line-point) (point-min)))
1047 ;; makefile.el ends here