1 ;;; makefile.el --- makefile editing commands for Emacs
3 ;; Author: Thomas Neumann <tom@smart.bo.open.de>
5 ;; Keywords: unix, tools
7 ;; $Id: makefile.el,v 1.7.1.17 1992/07/15 20:05:15 tom Exp tom $
9 ;; Copyright (C) 1992 Free Software Foundation, Inc.
11 ;; This file is part of GNU Emacs.
13 ;; GNU Emacs is free software; you can redistribute it and/or modify
14 ;; it under the terms of the GNU General Public License as published by
15 ;; the Free Software Foundation; either version 1, or (at your option)
18 ;; GNU Emacs is distributed in the hope that it will be useful,
19 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
20 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 ;; GNU General Public License for more details.
23 ;; You should have received a copy of the GNU General Public License
24 ;; along with GNU Emacs; see the file COPYING. If not, write to
25 ;; the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
31 ;;; ------------------------------------------------------------
32 ;;; Configureable stuff
33 ;;; ------------------------------------------------------------
35 (defvar makefile-mode-name
"makefile"
36 "The \"pretty name\" of makefile-mode, as it
37 appears in the modeline.")
39 (defvar makefile-browser-buffer-name
"*Macros and Targets*"
40 "Name of the macro- and target browser buffer.")
42 (defvar makefile-target-colon
":"
43 "The string that gets appended to all target names
44 inserted by makefile-insert-target.
45 \":\" or \"::\" are quite common values.")
47 (defvar makefile-macro-assign
" = "
48 "The string that gets appended to all macro names
49 inserted by makefile-insert-macro.
50 The normal value should be \" = \", since this is what
51 standard make expects. However, newer makes such as dmake
52 allow a larger variety of different macro assignments, so you
53 might prefer to use \" += \" or \" := \" .")
55 (defvar makefile-use-curly-braces-for-macros-p nil
56 "Set this variable to a non-nil value if you prefer curly braces
57 in macro-references, so it looks like ${this}. A value of nil
58 will cause makefile-mode to use parantheses, making macro references
61 (defvar makefile-tab-after-target-colon t
62 "If you want a TAB (instead of a space) to be appended after the
63 target colon, then set this to a non-nil value.")
65 (defvar makefile-browser-leftmost-column
10
66 "Number of blanks to the left of the browser selection mark.")
68 (defvar makefile-browser-cursor-column
10
69 "Column in which the cursor is positioned when it moves
70 up or down in the browser.")
72 (defvar makefile-browser-selected-mark
"+ "
73 "String used to mark selected entries in the browser.")
75 (defvar makefile-browser-unselected-mark
" "
76 "String used to mark unselected entries in the browser.")
78 (defvar makefile-browser-auto-advance-after-selection-p t
79 "If this variable is set to a non-nil value the cursor
80 will automagically advance to the next line after an item
81 has been selected in the browser.")
83 (defvar makefile-find-file-autopickup-p t
84 "If this variable is set to a non-nil value then finding a file in
85 a makefile-mode buffer will cause an automatic initial pickup of
86 all macros and targets from the found file.")
88 (defvar makefile-pickup-everything-picks-up-filenames-p nil
89 "If this variable is set to a non-nil value then
90 makefile-pickup-everything also picks up filenames as targets
91 (i.e. it calls makefile-find-filenames-as-targets), otherwise
92 filenames are omitted.")
94 (defvar makefile-cleanup-continuations-p t
95 "If this variable is set to a non-nil value then makefile-mode
96 will assure that no line in the file ends with a backslash
97 (the continuation character) followed by any whitespace.
98 This is done by silently removing the trailing whitespace, leaving
99 the backslash itself intact.
100 IMPORTANT: Please note that enabling this option causes makefile-mode
101 to MODIFY A FILE WITHOUT YOUR CONFIRMATION when \'it seems necessary\'.")
103 (defvar makefile-browser-hook
'()
104 "A function or list of functions to be called just before the
105 browser is entered. This is executed in the makefile buffer, so
106 you can for example run a makefile-pickup-everything automatically.")
109 ;; Special targets for DMake, Sun's make ...
111 (defvar makefile-special-targets-list
112 '(("DEFAULT") ("DONE") ("ERROR") ("EXPORT")
113 ("FAILED") ("GROUPEPILOG") ("GROUPPROLOG") ("IGNORE")
114 ("IMPORT") ("INCLUDE") ("INCLUDEDIRS") ("INIT")
115 ("KEEP_STATE") ("MAKEFILES") ("MAKE_VERSION") ("NO_PARALLEL")
116 ("PARALLEL") ("PHONY") ("PRECIOUS") ("REMOVE")
117 ("SCCS_GET") ("SILENT") ("SOURCE") ("SUFFIXES")
118 ("WAIT") ("c.o") ("C.o") ("m.o")
119 ("el.elc") ("y.c") ("s.o"))
120 "List of special targets. You will be offered to complete
121 on one of those in the minibuffer whenever you enter a \".\"
122 at the beginning of a line in makefile-mode.")
124 (defvar makefile-runtime-macros-list
125 '(("@") ("&") (">") ("<") ("*") ("^") ("?") ("%"))
126 "List of macros that are resolved by make at runtime.
127 If you insert a macro reference using makefile-insert-macro-ref, the name
128 of the macro is checked against this list. If it can be found its name will
129 not be enclosed in { } or ( ).")
131 (defconst makefile-dependency-regex
132 "^[^ \t#:]+\\([ \t]+[^ \t#:]+\\)*[ \t]*:\\($\\|\\([^=].*$\\)\\)"
133 "Regex used to find dependency lines in a makefile.")
135 (defconst makefile-macroassign-regex
136 "^[^ \t][^:#=]*[\\*:\\+]?:?=.*$"
137 "Regex used to find macro assignment lines in a makefile.")
139 (defconst makefile-ignored-files-in-pickup-regex
140 "\\(^\\..*\\)\\|\\(.*~$\\)\\|\\(.*,v$\\)"
141 "Regex for filenames that will NOT be included in the target list.")
143 ;;; ------------------------------------------------------------
144 ;;; The following configurable variables are used in the
145 ;;; up-to-date overview .
146 ;;; The standard configuration assumes that your `make' programm
147 ;;; can be run in question/query mode using the `-q' option, this
148 ;;; means that the command
152 ;;; should return an exit status of zero if the target `foo' is
153 ;;; up to date and a nonzero exit status otherwise.
154 ;;; Many makes can do this although the docs/manpages do not mention
155 ;;; it. Try it with your favourite one. GNU make and Dennis Vaduras
156 ;;; DMake have no problems.
157 ;;; Set the variable `makefile-brave-make' to the name of the
158 ;;; make utility that does this on your system.
159 ;;; To understand what this is all about see the function defintion
160 ;;; of `makefile-query-by-make-minus-q' .
161 ;;; ------------------------------------------------------------
163 (defvar makefile-brave-make
"gmake"
164 "A make that can handle the \'-q\' option.")
166 (defvar makefile-query-one-target-method
'makefile-query-by-make-minus-q
167 "A function symbol [one that can be used as the first argument to
168 funcall] that provides a function that must conform to the following
171 * As its first argument, it must accept the name of the target to
172 be checked, as a string.
174 * As its second argument, it may accept the name of a makefile
175 as a string. Depending on what you\'re going to do you may
178 * It must return the integer value 0 (zero) if the given target
179 should be considered up-to-date in the context of the given
180 makefile, any nonzero integer value otherwise.")
182 (defvar makefile-up-to-date-buffer-name
"*Makefile Up-to-date overview*"
183 "Name of the Up-to-date overview buffer.")
185 (defvar makefile-target-needs-rebuild-mark
" .. NEEDS REBUILD"
186 "A string that is appended to the target name in the up-to-date
187 overview if that target is considered to require a rebuild.")
189 (defvar makefile-target-up-to-date-mark
" .. is up to date"
190 "A string that is appenden to the target name in the up-to-date
191 overview if that target is considered up-to-date.")
193 ;;; --- end of up-to-date-overview configuration ------------------
196 (defvar makefile-mode-map nil
197 "The keymap that is used in makefile-mode.")
198 (if makefile-mode-map
200 (setq makefile-mode-map
(make-sparse-keymap))
202 (define-key makefile-mode-map
"$" 'makefile-insert-macro-ref
)
203 (define-key makefile-mode-map
"\C-c:" 'makefile-insert-target-ref
)
204 (define-key makefile-mode-map
":" 'makefile-electric-colon
)
205 (define-key makefile-mode-map
"=" 'makefile-electric-equal
)
206 (define-key makefile-mode-map
"." 'makefile-electric-dot
)
207 (define-key makefile-mode-map
"\C-c\C-t" 'makefile-pickup-targets
)
208 (define-key makefile-mode-map
"\C-c\C-m" 'makefile-pickup-macros
)
209 (define-key makefile-mode-map
"\C-c\C-f" 'makefile-pickup-filenames-as-targets
)
210 (define-key makefile-mode-map
"\C-c\C-0" 'makefile-forget-everything
)
211 (define-key makefile-mode-map
"\C-c0" 'makefile-forget-everything
)
212 (define-key makefile-mode-map
"\C-c\C-b" 'makefile-switch-to-browser
)
213 (define-key makefile-mode-map
"\C-c\C-p" 'makefile-pickup-everything
)
214 (define-key makefile-mode-map
"\C-c\C-u" 'makefile-create-up-to-date-overview
)
215 (define-key makefile-mode-map
"\C-c\C-i" 'makefile-insert-gmake-function
)
216 (define-key makefile-mode-map
"\M-p" 'makefile-previous-dependency
)
217 (define-key makefile-mode-map
"\M-n" 'makefile-next-dependency
))
219 (defvar makefile-browser-map nil
220 "The keymap that is used in the macro- and target browser.")
221 (if makefile-browser-map
223 (setq makefile-browser-map
(make-sparse-keymap))
224 (define-key makefile-browser-map
"n" 'makefile-browser-next-line
)
225 (define-key makefile-browser-map
"\C-n" 'makefile-browser-next-line
)
226 (define-key makefile-browser-map
"p" 'makefile-browser-previous-line
)
227 (define-key makefile-browser-map
"\C-p" 'makefile-browser-previous-line
)
228 (define-key makefile-browser-map
" " 'makefile-browser-toggle
)
229 (define-key makefile-browser-map
"i" 'makefile-browser-insert-selection
)
230 (define-key makefile-browser-map
"I" 'makefile-browser-insert-selection-and-quit
)
231 (define-key makefile-browser-map
"\C-c\C-m" 'makefile-browser-insert-continuation
)
232 (define-key makefile-browser-map
"q" 'makefile-browser-quit
)
233 ;; disable horizontal movement
234 (define-key makefile-browser-map
"\C-b" 'undefined
)
235 (define-key makefile-browser-map
"\C-f" 'undefined
))
238 (defvar makefile-mode-syntax-table nil
239 "The syntax-table used in makefile mode.")
240 (if makefile-mode-syntax-table
242 (setq makefile-mode-syntax-table
(make-syntax-table))
243 (modify-syntax-entry ?\
( "() " makefile-mode-syntax-table
)
244 (modify-syntax-entry ?\
) ")( " makefile-mode-syntax-table
)
245 (modify-syntax-entry ?\
[ "(] " makefile-mode-syntax-table
)
246 (modify-syntax-entry ?\
] "([ " makefile-mode-syntax-table
)
247 (modify-syntax-entry ?\
{ "(} " makefile-mode-syntax-table
)
248 (modify-syntax-entry ?\
} "){ " makefile-mode-syntax-table
)
249 (modify-syntax-entry ?
# "< " makefile-mode-syntax-table
)
250 (modify-syntax-entry ?
\n "> " makefile-mode-syntax-table
))
253 ;;; ------------------------------------------------------------
254 ;;; Internal variables.
255 ;;; You don't need to configure below this line.
256 ;;; ------------------------------------------------------------
258 (defvar makefile-target-table nil
259 "Table of all targets that have been inserted in
260 this Makefile buffer using makefile-insert-target or picked up
261 using makefile-pickup-targets.")
263 (defvar makefile-macro-table nil
264 "Table of all macros that have been iserted in
265 this Makefile buffer using makefile-insert-macro or picked up
266 using makefile-pickup-macros.")
268 (defvar makefile-browser-client
269 "A buffer in makefile-mode that is currently using the browser.")
271 (defvar makefile-browser-selection-vector nil
)
273 (defvar makefile-mode-hook
'())
275 (defconst makefile-gnumake-functions-alist
279 ("subst" "From" "To" "In")
280 ("patsubst" "Pattern" "Replacement" "In")
282 ("findstring" "Find what" "In")
283 ("filter" "Pattern" "Text")
284 ("filter-out" "Pattern" "Text")
286 ;; Filename functions
291 ("addsuffix" "Suffix" "Names")
292 ("join" "List 1" "List 2")
293 ("word" "Index" "Text")
296 ("wildcard" "Pattern")
298 ("foreach" "Variable" "List" "Text")
299 ("origin" "Variable")
301 "A list of GNU make 3.62 function names associated with
302 the prompts for each function.
303 This is used in the function makefile-insert-gmake-function .")
306 ;;; ------------------------------------------------------------
307 ;;; The mode function itself.
308 ;;; ------------------------------------------------------------
310 (defun makefile-mode ()
311 "Major mode for editing Makefiles.
312 Calling this function invokes the function(s) \"makefile-mode-hook\" before
315 \\{makefile-mode-map}
317 In the browser, use the following keys:
319 \\{makefile-browser-map}
321 makefile-mode can be configured by modifying the following
325 The \"pretty name\" of makefile-mode, as it
326 appears in the modeline.
328 makefile-browser-buffer-name:
329 Name of the macro- and target browser buffer.
331 makefile-target-colon:
332 The string that gets appended to all target names
333 inserted by makefile-insert-target.
334 \":\" or \"::\" are quite common values.
336 makefile-macro-assign:
337 The string that gets appended to all macro names
338 inserted by makefile-insert-macro.
339 The normal value should be \" = \", since this is what
340 standard make expects. However, newer makes such as dmake
341 allow a larger variety of different macro assignments, so you
342 might prefer to use \" += \" or \" := \" .
344 makefile-tab-after-target-colon:
345 If you want a TAB (instead of a space) to be appended after the
346 target colon, then set this to a non-nil value.
348 makefile-browser-leftmost-column:
349 Number of blanks to the left of the browser selection mark.
351 makefile-browser-cursor-column:
352 Column in which the cursor is positioned when it moves
353 up or down in the browser.
355 makefile-browser-selected-mark:
356 String used to mark selected entries in the browser.
358 makefile-browser-unselected-mark:
359 String used to mark unselected entries in the browser.
361 makefile-browser-auto-advance-after-selection-p:
362 If this variable is set to a non-nil value the cursor
363 will automagically advance to the next line after an item
364 has been selected in the browser.
366 makefile-find-file-autopickup-p:
367 If this variable is set to a non-nil value then finding a file in
368 a makefile-mode buffer will cause an automatic initial pickup of
369 all macros and targets from the found file.
371 makefile-pickup-everything-picks-up-filenames-p:
372 If this variable is set to a non-nil value then
373 makefile-pickup-everything also picks up filenames as targets
374 (i.e. it calls makefile-find-filenames-as-targets), otherwise
375 filenames are omitted.
377 makefile-cleanup-continuations-p:
378 If this variable is set to a non-nil value then makefile-mode
379 will assure that no line in the file ends with a backslash
380 (the continuation character) followed by any whitespace.
381 This is done by silently removing the trailing whitespace, leaving
382 the backslash itself intact.
383 IMPORTANT: Please note that enabling this option causes makefile-mode
384 to MODIFY A FILE WITHOUT YOUR CONFIRMATION when \'it seems necessary\'.
386 makefile-browser-hook:
387 A function or list of functions to be called just before the
388 browser is entered. This is executed in the makefile buffer, so
389 you can for example run a makefile-pickup-everything automatically.
391 makefile-special-targets-list:
392 List of special targets. You will be offered to complete
393 on one of those in the minibuffer whenever you enter a \".\"
394 at the beginning of a line in makefile-mode."
396 (kill-all-local-variables)
397 (if (not (memq 'makefile-find-file-autopickup find-file-hooks
))
398 (setq find-file-hooks
399 (append find-file-hooks
(list 'makefile-find-file-autopickup
))))
400 (if (not (memq 'makefile-cleanup-continuations write-file-hooks
))
401 (setq write-file-hooks
402 (append write-file-hooks
(list 'makefile-cleanup-continuations
))))
403 (make-variable-buffer-local 'makefile-target-table
)
404 (make-variable-buffer-local 'makefile-macro-table
)
405 (makefile-forget-all-macros)
406 (makefile-forget-all-targets)
407 (setq comment-start
"#")
408 (setq comment-end
"")
409 (setq comment-start-skip
"#[ \t]*")
410 ;; become the current major mode
411 (setq major-mode
'makefile-mode
)
412 (setq mode-name makefile-mode-name
)
414 (use-local-map makefile-mode-map
)
415 (set-syntax-table makefile-mode-syntax-table
)
416 (run-hooks 'makefile-mode-hook
))
419 (defun makefile-find-file-autopickup ()
420 (if (eq major-mode
'makefile-mode
)
421 (if makefile-find-file-autopickup-p
422 (makefile-pickup-everything))))
424 (defun makefile-next-dependency ()
425 "Move (point) to the beginning of the next dependency line
426 below the current position of (point)."
428 (let ((here (point)))
430 (if (re-search-forward makefile-dependency-regex
(point-max) t
)
431 (progn (beginning-of-line) t
) ; indicate success
432 (goto-char here
) nil
)))
434 (defun makefile-previous-dependency ()
435 "Move (point) to the beginning of the next dependency line
436 above the current position of (point)."
438 (let ((here (point)))
440 (if (re-search-backward makefile-dependency-regex
(point-min) t
)
441 (progn (beginning-of-line) t
) ; indicate success
442 (goto-char here
) nil
)))
445 (defun makefile-electric-dot ()
446 "At (bol), offer completion on makefile-special-targets-list.
447 Anywhere else just insert a dot."
450 (makefile-insert-special-target)
454 (defun makefile-insert-special-target ()
455 "Offer completion on makefile-special-targets-list and insert
456 the result at (point)."
460 (completing-read "Special target: "
461 makefile-special-targets-list nil nil nil
)))
462 (if (zerop (length special-target
))
464 (insert (format ".%s:" special-target
))
465 (makefile-forward-after-target-colon))))
468 (defun makefile-electric-equal ()
469 "At (bol) do makefile-insert-macro. Anywhere else just
473 (call-interactively 'makefile-insert-macro
)
476 (defun makefile-insert-macro (macro-name)
477 "Prepare definition of a new macro."
478 (interactive "sMacro Name: ")
479 (if (not (zerop (length macro-name
)))
482 (insert (format "%s%s" macro-name makefile-macro-assign
))
483 (makefile-remember-macro macro-name
))))
486 (defun makefile-insert-macro-ref (macro-name)
487 "Offer completion on a list of known macros, then
488 insert complete macro-ref at (point) ."
491 (completing-read "Refer to macro: " makefile-macro-table nil nil nil
)))
492 (if (not (zerop (length macro-name
)))
493 (if (assoc macro-name makefile-runtime-macros-list
)
494 (insert (format "$%s " macro-name
))
495 (insert (makefile-format-macro-ref macro-name
) " "))))
498 (defun makefile-insert-target (target-name)
499 "Prepare definition of a new target (dependency line)."
500 (interactive "sTarget: ")
501 (if (not (zerop (length target-name
)))
504 (insert (format "%s%s" target-name makefile-target-colon
))
505 (makefile-forward-after-target-colon)
507 (makefile-remember-target target-name
))))
510 (defun makefile-insert-target-ref (target-name)
511 "Offer completion on a list of known targets, then
512 insert complete target-ref at (point) ."
515 (completing-read "Refer to target: " makefile-target-table nil nil nil
)))
516 (if (not (zerop (length target-name
)))
518 (insert (format "%s " target-name
)))))
520 (defun makefile-electric-colon ()
521 "At (bol) defines a new target, anywhere else just self-insert ."
524 (call-interactively 'makefile-insert-target
)
528 ;;; ------------------------------------------------------------
529 ;;; Extracting targets and macros from an existing makefile
530 ;;; ------------------------------------------------------------
532 (defun makefile-pickup-targets ()
533 "Scan a buffer that contains a makefile for target definitions (dependencies)
534 and add them to the list of known targets."
537 (goto-char (point-min))
538 (while (re-search-forward makefile-dependency-regex
(point-max) t
)
539 (makefile-add-this-line-targets))))
540 ; (forward-line 1))))
542 (defun makefile-add-this-line-targets ()
545 (let ((done-with-line nil
))
546 (while (not done-with-line
)
547 (skip-chars-forward " \t")
548 (if (not (setq done-with-line
(or (eolp)
549 (char-equal (char-after (point)) ?
:))))
551 (let* ((start-of-target-name (point))
554 (skip-chars-forward "^ \t:#")
555 (buffer-substring start-of-target-name
(point)))))
556 (if (makefile-remember-target target-name
)
557 (message "Picked up target \"%s\"" target-name
)))))))))
560 (defun makefile-pickup-macros ()
561 "Scan a buffer that contains a makefile for macro definitions
562 and add them to the list of known macros."
565 (goto-char (point-min))
566 (while (re-search-forward makefile-macroassign-regex
(point-max) t
)
567 (makefile-add-this-line-macro)
570 (defun makefile-add-this-line-macro ()
573 (skip-chars-forward " \t")
575 (let* ((start-of-macro-name (point))
577 (skip-chars-forward "^ \t:#=*")
578 (buffer-substring start-of-macro-name
(point)))))
579 (if (makefile-remember-macro macro-name
)
580 (message "Picked up macro \"%s\"" macro-name
))))))
583 (defun makefile-pickup-everything ()
584 "Calls makefile-pickup-targets and makefile-pickup-macros.
585 See their documentation for what they do."
587 (makefile-pickup-macros)
588 (makefile-pickup-targets)
589 (if makefile-pickup-everything-picks-up-filenames-p
590 (makefile-pickup-filenames-as-targets)))
593 (defun makefile-pickup-filenames-as-targets ()
594 "Scan the current directory for filenames, check each filename
595 against makefile-ignored-files-in-pickup-regex and add all qualifying
596 names to the list of known targets."
598 (let* ((dir (file-name-directory (buffer-file-name)))
599 (raw-filename-list (if dir
600 (file-name-all-completions "" dir
)
601 (file-name-all-completions "" ""))))
602 (mapcar '(lambda (name)
603 (if (and (not (file-directory-p name
))
604 (not (string-match makefile-ignored-files-in-pickup-regex
606 (if (makefile-remember-target name
)
607 (message "Picked up file \"%s\" as target" name
))))
610 ;;; ------------------------------------------------------------
611 ;;; The browser window
612 ;;; ------------------------------------------------------------
615 (defun makefile-browser-format-target-line (target selected
)
617 (concat (make-string makefile-browser-leftmost-column ?\
)
619 makefile-browser-selected-mark
620 makefile-browser-unselected-mark
)
622 target makefile-target-colon
))
624 (defun makefile-browser-format-macro-line (macro selected
)
626 (concat (make-string makefile-browser-leftmost-column ?\
)
628 makefile-browser-selected-mark
629 makefile-browser-unselected-mark
)
630 (makefile-format-macro-ref macro
))))
632 (defun makefile-browser-fill (targets macros
)
633 (setq buffer-read-only nil
)
634 (goto-char (point-min))
638 (lambda (item) (insert (makefile-browser-format-target-line (car item
) nil
) "\n")))
643 (lambda (item) (insert (makefile-browser-format-macro-line (car item
) nil
) "\n")))
646 (sort-lines nil
(point-min) (point-max))
647 (goto-char (1- (point-max)))
648 (delete-char 1) ; remove unnecessary newline at eob
649 (goto-char (point-min))
650 (forward-char makefile-browser-cursor-column
)
651 (setq buffer-read-only t
))
655 ;;; Moving up and down in the browser
658 (defun makefile-browser-next-line ()
659 "Move the browser selection cursor to the next line."
661 (if (not (makefile-last-line-p))
664 (forward-char makefile-browser-cursor-column
))))
666 (defun makefile-browser-previous-line ()
667 "Move the browser selection cursor to the previous line."
669 (if (not (makefile-first-line-p))
672 (forward-char makefile-browser-cursor-column
))))
675 ;;; Quitting the browser (returns to client buffer)
678 (defun makefile-browser-quit ()
679 "Leave the makefile-browser-buffer and return to the buffer
680 from that it has been entered."
682 (let ((my-client makefile-browser-client
))
683 (setq makefile-browser-client nil
) ; we quitted, so NO client!
684 (set-buffer-modified-p nil
)
685 (kill-buffer (current-buffer))
686 (pop-to-buffer my-client
)))
689 ;;; Toggle state of a browser item
692 (defun makefile-browser-toggle ()
693 "Toggle the selection state of the browser item at the cursor position."
695 (let ((this-line (count-lines (point-min) (point))))
696 (setq this-line
(max 1 this-line
))
697 (makefile-browser-toggle-state-for-line this-line
)
698 (goto-line this-line
)
699 (setq buffer-read-only nil
)
701 (if (makefile-browser-on-macro-line-p)
702 (let ((macro-name (makefile-browser-this-line-macro-name)))
705 (makefile-browser-format-macro-line
707 (makefile-browser-get-state-for-line this-line
))))
708 (let ((target-name (makefile-browser-this-line-target-name)))
711 (makefile-browser-format-target-line
713 (makefile-browser-get-state-for-line this-line
)))))
714 (setq buffer-read-only t
)
716 (forward-char makefile-browser-cursor-column
)
717 (if makefile-browser-auto-advance-after-selection-p
718 (makefile-browser-next-line))))
721 ;;; Making insertions into the client buffer
724 (defun makefile-browser-insert-continuation ()
725 "In the browser\'s client buffer, go to (end-of-line), insert a \'\\\'
726 character, insert a new blank line, go to that line and indent by one TAB.
727 This is most useful in the process of creating continued lines when 'sending' large
728 dependencies from the browser to the client buffer.
729 (point) advances accordingly in the client buffer."
732 (set-buffer makefile-browser-client
)
736 (defun makefile-browser-insert-selection ()
737 "Insert all browser-selected targets and/or macros in the browser\'s
739 Insertion takes place at (point)."
743 (let ((current-line 1))
745 (if (makefile-browser-get-state-for-line current-line
)
746 (makefile-browser-send-this-line-item))
748 (setq current-line
(1+ current-line
))))))
750 (defun makefile-browser-insert-selection-and-quit ()
752 (makefile-browser-insert-selection)
753 (makefile-browser-quit))
755 (defun makefile-browser-send-this-line-item ()
756 (if (makefile-browser-on-macro-line-p)
758 (let ((macro-name (makefile-browser-this-line-macro-name)))
759 (set-buffer makefile-browser-client
)
760 (insert (makefile-format-macro-ref macro-name
) " ")))
762 (let ((target-name (makefile-browser-this-line-target-name)))
763 (set-buffer makefile-browser-client
)
764 (insert target-name
" ")))))
767 (defun makefile-browser-start-interaction ()
768 (use-local-map makefile-browser-map
)
769 (setq buffer-read-only t
))
772 (defun makefile-browse (targets macros
)
774 (if (zerop (+ (length targets
) (length macros
)))
777 (message "No macros or targets to browse! Consider running 'makefile-pickup-everything\'"))
778 (let ((browser-buffer (get-buffer-create makefile-browser-buffer-name
)))
779 (pop-to-buffer browser-buffer
)
780 (make-variable-buffer-local 'makefile-browser-selection-vector
)
781 (makefile-browser-fill targets macros
)
782 (setq makefile-browser-selection-vector
783 (make-vector (+ (length targets
) (length macros
)) nil
))
784 (makefile-browser-start-interaction))))
787 (defun makefile-switch-to-browser ()
789 (run-hooks 'makefile-browser-hook
)
790 (setq makefile-browser-client
(current-buffer))
791 (makefile-browse makefile-target-table makefile-macro-table
))
794 ;;; ------------------------------------------------------------
795 ;;; Up-to-date overview buffer
796 ;;; ------------------------------------------------------------
798 (defun makefile-create-up-to-date-overview ()
799 "Create a buffer containing an overview of the state of all
800 known targets from the makefile that is currently being edited.
801 Known targets are targets that are explicitly defined in that makefile;
802 in other words, all targets that appear on the left hand side of a
803 dependency in the makefile."
805 (if (y-or-n-p "Are you sure that the makefile being edited is consistent? ")
807 ;; The rest of this function operates on a temporary makefile, created by
808 ;; writing the current contents of the makefile buffer.
810 (let ((saved-target-table makefile-target-table
)
811 (this-buffer (current-buffer))
812 (makefile-up-to-date-buffer
813 (get-buffer-create makefile-up-to-date-buffer-name
))
814 (filename (makefile-save-temporary))
816 ;; Forget the target table because it may contain picked-up filenames
817 ;; that are not really targets in the current makefile.
818 ;; We don't want to query these, so get a new target-table with just the
819 ;; targets that can be found in the makefile buffer.
820 ;; The 'old' target table will be restored later.
823 (makefile-forget-all-targets)
824 (makefile-pickup-targets)
825 makefile-target-table
)))
827 (set-buffer makefile-up-to-date-buffer
)
828 (setq buffer-read-only nil
)
830 (makefile-query-targets filename real-targets
)
831 (if (zerop (buffer-size)) ; if it did not get us anything
833 (kill-buffer (current-buffer))
834 (message "No overview created!")))
835 (set-buffer this-buffer
)
836 (setq makefile-target-table saved-target-table
)
837 (if (get-buffer makefile-up-to-date-buffer-name
)
839 (pop-to-buffer (get-buffer makefile-up-to-date-buffer-name
))
840 (sort-lines nil
(point-min) (point-max))
841 (setq buffer-read-only t
))))))
845 (defun makefile-save-temporary ()
846 "Create a temporary file from the current makefile buffer."
847 (let ((filename (makefile-generate-temporary-filename)))
848 (write-region (point-min) (point-max) filename nil
0)
849 filename
)) ; return the filename
851 (defun makefile-generate-temporary-filename ()
852 "Create a filename suitable for use in makefile-save-temporary.
853 Be careful to allow brain-dead file systems (DOS, SYSV ...) to cope
854 with the generated name !"
855 (let ((my-name (user-login-name))
856 (my-uid (int-to-string (user-uid))))
858 (if (> (length my-name
) 3)
859 (substring my-name
0 3)
862 (if (> (length my-uid
) 3)
863 (substring my-uid
0 3)
866 (defun makefile-query-targets (filename target-table
)
867 "This function fills the up-to-date-overview-buffer.
868 It checks each target in target-table using makefile-query-one-target-method
869 and generates the overview, one line per target name."
871 (mapconcat '(lambda (item)
872 (let ((target-name (car item
)))
873 (makefile-format-up-to-date-buffer-entry
874 (funcall makefile-query-one-target-method
875 target-name filename
) target-name
)))
877 (goto-char (point-min))
878 (delete-file filename
)) ; remove the tmpfile
880 (defun makefile-query-by-make-minus-q (target &optional filename
)
881 (not (zerop (call-process makefile-brave-make nil nil nil
"-f" filename
"-q" target
))))
883 (defun makefile-format-up-to-date-buffer-entry (needs-rebuild target
)
887 makefile-target-needs-rebuild-mark
888 makefile-target-up-to-date-mark
)))
891 ;;; ------------------------------------------------------------
892 ;;; Continuation cleanup
893 ;;; ------------------------------------------------------------
895 (defun makefile-cleanup-continuations ()
896 (if (eq major-mode
'makefile-mode
)
897 (if (and makefile-cleanup-continuations-p
898 (not buffer-read-only
))
900 (goto-char (point-min))
901 (while (re-search-forward "\\\\[ \t]+$" (point-max) t
)
902 (replace-match "\\" t t
))))))
904 ;;; ------------------------------------------------------------
905 ;;; GNU make function support
906 ;;; ------------------------------------------------------------
908 (defun makefile-insert-gmake-function ()
909 "This function is intended to help you using the numerous
910 macro-like \'function calls\' of GNU make.
911 It will ask you for the name of the function you wish to
912 use (with completion), then, after you selected the function,
913 it will prompt you for all required parameters.
914 This function \'knows\' about the required parameters of every
915 GNU make function and will use meaningfull prompts for the
916 various args, making it much easier to take advantage of this
917 powerfull GNU make feature."
919 (let* ((gm-function-name (completing-read
921 makefile-gnumake-functions-alist
924 (cdr (assoc gm-function-name makefile-gnumake-functions-alist
))))
925 (if (not (zerop (length gm-function-name
)))
926 (insert (makefile-format-macro-ref
927 (concat gm-function-name
" "
928 (makefile-prompt-for-gmake-funargs
929 gm-function-name gm-function-prompts
)))
932 (defun makefile-prompt-for-gmake-funargs (function-name prompt-list
)
934 (function (lambda (one-prompt)
935 (read-string (format "[%s] %s: " function-name one-prompt
) nil
)))
941 ;;; ------------------------------------------------------------
942 ;;; Utility functions
943 ;;; ------------------------------------------------------------
945 (defun makefile-forget-all-targets ()
946 "Clear the target-table for this buffer."
948 (setq makefile-target-table
'()))
950 (defun makefile-forget-all-macros ()
951 "Clear the macro-table for this buffer."
953 (setq makefile-macro-table
'()))
956 (defun makefile-forget-everything ()
957 "Clear the macro-table AND the target-table for this buffer."
959 (if (y-or-n-p "Really forget all macro- and target information ? ")
961 (makefile-forget-all-targets)
962 (makefile-forget-all-macros)
963 (if (get-buffer makefile-browser-buffer-name
)
964 (kill-buffer makefile-browser-buffer-name
))
965 (message "Cleared macro- and target tables."))))
967 (defun makefile-remember-target (target-name)
968 "Remember a given target if it is not already remembered for this buffer."
969 (if (not (zerop (length target-name
)))
970 (if (not (assoc target-name makefile-target-table
))
971 (setq makefile-target-table
972 (cons (list target-name
) makefile-target-table
)))))
974 (defun makefile-remember-macro (macro-name)
975 "Remember a given macro if it is not already remembered for this buffer."
976 (if (not (zerop (length macro-name
)))
977 (if (not (assoc macro-name makefile-macro-table
))
978 (setq makefile-macro-table
979 (cons (list macro-name
) makefile-macro-table
)))))
981 (defun makefile-forward-after-target-colon ()
982 "Move point forward after the terminating colon
983 of a target has been inserted.
984 This accts according to the value of makefile-tab-after-target-colon ."
985 (if makefile-tab-after-target-colon
989 (defun makefile-browser-on-macro-line-p ()
990 "Determine if point is on a macro line in the browser."
993 (re-search-forward "\\$[{(]" (makefile-end-of-line-point) t
)))
995 (defun makefile-browser-this-line-target-name ()
996 "Extract the target name from a line in the browser."
999 (skip-chars-backward "^ \t")
1000 (buffer-substring (point) (1- (makefile-end-of-line-point)))))
1002 (defun makefile-browser-this-line-macro-name ()
1003 "Extract the macro name from a line in the browser."
1006 (re-search-forward "\\$[{(]" (makefile-end-of-line-point) t
)
1007 (let ((macro-start (point)))
1008 (skip-chars-forward "^})")
1009 (buffer-substring macro-start
(point)))))
1011 (defun makefile-format-macro-ref (macro-name)
1012 "Format a macro reference according to the value of the
1013 configuration variable makefile-use-curly-braces-for-macros-p ."
1014 (if makefile-use-curly-braces-for-macros-p
1015 (format "${%s}" macro-name
)
1016 (format "$(%s)" macro-name
)))
1018 (defun makefile-browser-get-state-for-line (n)
1019 (aref makefile-browser-selection-vector
(1- n
)))
1021 (defun makefile-browser-set-state-for-line (n to-state
)
1022 (aset makefile-browser-selection-vector
(1- n
) to-state
))
1024 (defun makefile-browser-toggle-state-for-line (n)
1025 (makefile-browser-set-state-for-line n
(not (makefile-browser-get-state-for-line n
))))
1027 (defun makefile-beginning-of-line-point ()
1032 (defun makefile-end-of-line-point ()
1037 (defun makefile-last-line-p ()
1038 (= (makefile-end-of-line-point) (point-max)))
1040 (defun makefile-first-line-p ()
1041 (= (makefile-beginning-of-line-point) (point-min)))
1043 ;; makefile.el ends here