Commit | Line | Data |
---|---|---|
72c0ae01 ER |
1 | ;;; makefile.el --- makefile editing commands for Emacs |
2 | ||
3 | ;; Author: Thomas Neumann <tom@smart.bo.open.de> | |
4 | ;; Adapted-By: ESR | |
fd7fa35a | 5 | ;; Keywords: unix, tools |
72c0ae01 ER |
6 | |
7 | ;; $Id: makefile.el,v 1.7.1.17 1992/07/15 20:05:15 tom Exp tom $ | |
8 | ||
9 | ;; Copyright (C) 1992 Free Software Foundation, Inc. | |
10 | ||
11 | ;; This file is part of GNU Emacs. | |
12 | ||
13 | ;; GNU Emacs is free software; you can redistribute it and/or modify | |
14 | ;; it under the terms of the GNU General Public License as published by | |
15 | ;; the Free Software Foundation; either version 1, or (at your option) | |
16 | ;; any later version. | |
17 | ||
18 | ;; GNU Emacs is distributed in the hope that it will be useful, | |
19 | ;; but WITHOUT ANY WARRANTY; without even the implied warranty of | |
20 | ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
21 | ;; GNU General Public License for more details. | |
22 | ||
23 | ;; You should have received a copy of the GNU General Public License | |
24 | ;; along with GNU Emacs; see the file COPYING. If not, write to | |
25 | ;; the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. | |
26 | ||
27 | ;;; Code: | |
28 | ||
29 | (provide 'makefile) | |
30 | ||
31 | ;;; ------------------------------------------------------------ | |
32 | ;;; Configureable stuff | |
33 | ;;; ------------------------------------------------------------ | |
34 | ||
35 | (defvar makefile-mode-name "makefile" | |
36 | "The \"pretty name\" of makefile-mode, as it | |
37 | appears in the modeline.") | |
38 | ||
39 | (defvar makefile-browser-buffer-name "*Macros and Targets*" | |
40 | "Name of the macro- and target browser buffer.") | |
41 | ||
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.") | |
46 | ||
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 \" := \" .") | |
54 | ||
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 | |
59 | look like $(this) .") | |
60 | ||
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.") | |
64 | ||
65 | (defvar makefile-browser-leftmost-column 10 | |
66 | "Number of blanks to the left of the browser selection mark.") | |
67 | ||
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.") | |
71 | ||
72 | (defvar makefile-browser-selected-mark "+ " | |
73 | "String used to mark selected entries in the browser.") | |
74 | ||
75 | (defvar makefile-browser-unselected-mark " " | |
76 | "String used to mark unselected entries in the browser.") | |
77 | ||
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.") | |
82 | ||
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.") | |
87 | ||
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.") | |
93 | ||
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\'.") | |
102 | ||
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.") | |
107 | ||
108 | ;; | |
109 | ;; Special targets for DMake, Sun's make ... | |
110 | ;; | |
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.") | |
123 | ||
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 ( ).") | |
130 | ||
131 | (defconst makefile-dependency-regex | |
132 | "^[^ \t#:]+\\([ \t]+[^ \t#:]+\\)*[ \t]*:\\($\\|\\([^=].*$\\)\\)" | |
133 | "Regex used to find dependency lines in a makefile.") | |
134 | ||
135 | (defconst makefile-macroassign-regex | |
136 | "^[^ \t][^:#=]*[\\*:\\+]?:?=.*$" | |
137 | "Regex used to find macro assignment lines in a makefile.") | |
138 | ||
139 | (defconst makefile-ignored-files-in-pickup-regex | |
140 | "\\(^\\..*\\)\\|\\(.*~$\\)\\|\\(.*,v$\\)" | |
141 | "Regex for filenames that will NOT be included in the target list.") | |
142 | ||
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 | |
149 | ;;; | |
150 | ;;; make -q foo | |
151 | ;;; | |
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 | ;;; ------------------------------------------------------------ | |
162 | ||
163 | (defvar makefile-brave-make "gmake" | |
164 | "A make that can handle the \'-q\' option.") | |
165 | ||
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 | |
169 | interface: | |
170 | ||
171 | * As its first argument, it must accept the name of the target to | |
172 | be checked, as a string. | |
173 | ||
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 | |
176 | not need this. | |
177 | ||
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.") | |
181 | ||
182 | (defvar makefile-up-to-date-buffer-name "*Makefile Up-to-date overview*" | |
183 | "Name of the Up-to-date overview buffer.") | |
184 | ||
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.") | |
188 | ||
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.") | |
192 | ||
193 | ;;; --- end of up-to-date-overview configuration ------------------ | |
194 | ||
195 | ||
196 | (defvar makefile-mode-map nil | |
197 | "The keymap that is used in makefile-mode.") | |
198 | (if makefile-mode-map | |
199 | () | |
200 | (setq makefile-mode-map (make-sparse-keymap)) | |
201 | ;; set up the 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)) | |
218 | ||
219 | (defvar makefile-browser-map nil | |
220 | "The keymap that is used in the macro- and target browser.") | |
221 | (if makefile-browser-map | |
222 | () | |
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)) | |
236 | ||
237 | ||
238 | (defvar makefile-mode-syntax-table nil | |
239 | "The syntax-table used in makefile mode.") | |
240 | (if makefile-mode-syntax-table | |
241 | () | |
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)) | |
251 | ||
252 | ||
253 | ;;; ------------------------------------------------------------ | |
254 | ;;; Internal variables. | |
255 | ;;; You don't need to configure below this line. | |
256 | ;;; ------------------------------------------------------------ | |
257 | ||
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.") | |
262 | ||
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.") | |
267 | ||
268 | (defvar makefile-browser-client | |
269 | "A buffer in makefile-mode that is currently using the browser.") | |
270 | ||
271 | (defvar makefile-browser-selection-vector nil) | |
272 | ||
273 | (defvar makefile-mode-hook '()) | |
274 | ||
275 | (defconst makefile-gnumake-functions-alist | |
276 | ||
277 | '( | |
278 | ;; Text functions | |
279 | ("subst" "From" "To" "In") | |
280 | ("patsubst" "Pattern" "Replacement" "In") | |
281 | ("strip" "Text") | |
282 | ("findstring" "Find what" "In") | |
283 | ("filter" "Pattern" "Text") | |
284 | ("filter-out" "Pattern" "Text") | |
285 | ("sort" "List") | |
286 | ;; Filename functions | |
287 | ("dir" "Names") | |
288 | ("notdir" "Names") | |
289 | ("suffix" "Names") | |
290 | ("basename" "Names") | |
291 | ("addsuffix" "Suffix" "Names") | |
292 | ("join" "List 1" "List 2") | |
293 | ("word" "Index" "Text") | |
294 | ("words" "Text") | |
295 | ("firstword" "Text") | |
296 | ("wildcard" "Pattern") | |
297 | ;; Misc functions | |
298 | ("foreach" "Variable" "List" "Text") | |
299 | ("origin" "Variable") | |
300 | ("shell" "Command")) | |
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 .") | |
304 | ||
305 | ||
306 | ;;; ------------------------------------------------------------ | |
307 | ;;; The mode function itself. | |
308 | ;;; ------------------------------------------------------------ | |
309 | ||
310 | (defun makefile-mode () | |
311 | "Major mode for editing Makefiles. | |
312 | Calling this function invokes the function(s) \"makefile-mode-hook\" before | |
313 | doing anything else. | |
314 | ||
315 | \\{makefile-mode-map} | |
316 | ||
317 | In the browser, use the following keys: | |
318 | ||
319 | \\{makefile-browser-map} | |
320 | ||
321 | makefile-mode can be configured by modifying the following | |
322 | variables: | |
323 | ||
324 | makefile-mode-name: | |
325 | The \"pretty name\" of makefile-mode, as it | |
326 | appears in the modeline. | |
327 | ||
328 | makefile-browser-buffer-name: | |
329 | Name of the macro- and target browser buffer. | |
330 | ||
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. | |
335 | ||
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 \" := \" . | |
343 | ||
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. | |
347 | ||
348 | makefile-browser-leftmost-column: | |
349 | Number of blanks to the left of the browser selection mark. | |
350 | ||
351 | makefile-browser-cursor-column: | |
352 | Column in which the cursor is positioned when it moves | |
353 | up or down in the browser. | |
354 | ||
355 | makefile-browser-selected-mark: | |
356 | String used to mark selected entries in the browser. | |
357 | ||
358 | makefile-browser-unselected-mark: | |
359 | String used to mark unselected entries in the browser. | |
360 | ||
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. | |
365 | ||
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. | |
370 | ||
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. | |
376 | ||
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\'. | |
385 | ||
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. | |
390 | ||
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." | |
395 | (interactive) | |
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) | |
413 | ;; activate keymap | |
414 | (use-local-map makefile-mode-map) | |
415 | (set-syntax-table makefile-mode-syntax-table) | |
416 | (run-hooks 'makefile-mode-hook)) | |
417 | ||
418 | ||
419 | (defun makefile-find-file-autopickup () | |
420 | (if (eq major-mode 'makefile-mode) | |
421 | (if makefile-find-file-autopickup-p | |
422 | (makefile-pickup-everything)))) | |
423 | ||
424 | (defun makefile-next-dependency () | |
425 | "Move (point) to the beginning of the next dependency line | |
426 | below the current position of (point)." | |
427 | (interactive) | |
428 | (let ((here (point))) | |
429 | (end-of-line) | |
430 | (if (re-search-forward makefile-dependency-regex (point-max) t) | |
431 | (progn (beginning-of-line) t) ; indicate success | |
432 | (goto-char here) nil))) | |
433 | ||
434 | (defun makefile-previous-dependency () | |
435 | "Move (point) to the beginning of the next dependency line | |
436 | above the current position of (point)." | |
437 | (interactive) | |
438 | (let ((here (point))) | |
439 | (beginning-of-line) | |
440 | (if (re-search-backward makefile-dependency-regex (point-min) t) | |
441 | (progn (beginning-of-line) t) ; indicate success | |
442 | (goto-char here) nil))) | |
443 | ||
444 | ||
445 | (defun makefile-electric-dot () | |
446 | "At (bol), offer completion on makefile-special-targets-list. | |
447 | Anywhere else just insert a dot." | |
448 | (interactive) | |
449 | (if (bolp) | |
450 | (makefile-insert-special-target) | |
451 | (insert "."))) | |
452 | ||
453 | ||
454 | (defun makefile-insert-special-target () | |
455 | "Offer completion on makefile-special-targets-list and insert | |
456 | the result at (point)." | |
457 | (interactive) | |
458 | (let | |
459 | ((special-target | |
460 | (completing-read "Special target: " | |
461 | makefile-special-targets-list nil nil nil))) | |
462 | (if (zerop (length special-target)) | |
463 | () | |
464 | (insert (format ".%s:" special-target)) | |
465 | (makefile-forward-after-target-colon)))) | |
466 | ||
467 | ||
468 | (defun makefile-electric-equal () | |
469 | "At (bol) do makefile-insert-macro. Anywhere else just | |
470 | self-insert." | |
471 | (interactive) | |
472 | (if (bolp) | |
473 | (call-interactively 'makefile-insert-macro) | |
474 | (insert "="))) | |
475 | ||
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))) | |
480 | (progn | |
481 | (beginning-of-line) | |
482 | (insert (format "%s%s" macro-name makefile-macro-assign)) | |
483 | (makefile-remember-macro macro-name)))) | |
484 | ||
485 | ||
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) ." | |
489 | (interactive | |
490 | (list | |
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) " ")))) | |
496 | ||
497 | ||
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))) | |
502 | (progn | |
503 | (beginning-of-line) | |
504 | (insert (format "%s%s" target-name makefile-target-colon)) | |
505 | (makefile-forward-after-target-colon) | |
506 | (end-of-line) | |
507 | (makefile-remember-target target-name)))) | |
508 | ||
509 | ||
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) ." | |
513 | (interactive | |
514 | (list | |
515 | (completing-read "Refer to target: " makefile-target-table nil nil nil))) | |
516 | (if (not (zerop (length target-name))) | |
517 | (progn | |
518 | (insert (format "%s " target-name))))) | |
519 | ||
520 | (defun makefile-electric-colon () | |
521 | "At (bol) defines a new target, anywhere else just self-insert ." | |
522 | (interactive) | |
523 | (if (bolp) | |
524 | (call-interactively 'makefile-insert-target) | |
525 | (insert ":"))) | |
526 | ||
527 | ||
528 | ;;; ------------------------------------------------------------ | |
529 | ;;; Extracting targets and macros from an existing makefile | |
530 | ;;; ------------------------------------------------------------ | |
531 | ||
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." | |
535 | (interactive) | |
536 | (save-excursion | |
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)))) | |
541 | ||
542 | (defun makefile-add-this-line-targets () | |
543 | (save-excursion | |
544 | (beginning-of-line) | |
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)) ?:)))) | |
550 | (progn | |
551 | (let* ((start-of-target-name (point)) | |
552 | (target-name | |
553 | (progn | |
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))))))))) | |
558 | ||
559 | ||
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." | |
563 | (interactive) | |
564 | (save-excursion | |
565 | (goto-char (point-min)) | |
566 | (while (re-search-forward makefile-macroassign-regex (point-max) t) | |
567 | (makefile-add-this-line-macro) | |
568 | (forward-line 1)))) | |
569 | ||
570 | (defun makefile-add-this-line-macro () | |
571 | (save-excursion | |
572 | (beginning-of-line) | |
573 | (skip-chars-forward " \t") | |
574 | (if (not (eolp)) | |
575 | (let* ((start-of-macro-name (point)) | |
576 | (macro-name (progn | |
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)))))) | |
581 | ||
582 | ||
583 | (defun makefile-pickup-everything () | |
584 | "Calls makefile-pickup-targets and makefile-pickup-macros. | |
585 | See their documentation for what they do." | |
586 | (interactive) | |
587 | (makefile-pickup-macros) | |
588 | (makefile-pickup-targets) | |
589 | (if makefile-pickup-everything-picks-up-filenames-p | |
590 | (makefile-pickup-filenames-as-targets))) | |
591 | ||
592 | ||
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." | |
597 | (interactive) | |
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 | |
605 | name))) | |
606 | (if (makefile-remember-target name) | |
607 | (message "Picked up file \"%s\" as target" name)))) | |
608 | raw-filename-list))) | |
609 | ||
610 | ;;; ------------------------------------------------------------ | |
611 | ;;; The browser window | |
612 | ;;; ------------------------------------------------------------ | |
613 | ||
614 | ||
615 | (defun makefile-browser-format-target-line (target selected) | |
616 | (format | |
617 | (concat (make-string makefile-browser-leftmost-column ?\ ) | |
618 | (if selected | |
619 | makefile-browser-selected-mark | |
620 | makefile-browser-unselected-mark) | |
621 | "%s%s") | |
622 | target makefile-target-colon)) | |
623 | ||
624 | (defun makefile-browser-format-macro-line (macro selected) | |
625 | (format | |
626 | (concat (make-string makefile-browser-leftmost-column ?\ ) | |
627 | (if selected | |
628 | makefile-browser-selected-mark | |
629 | makefile-browser-unselected-mark) | |
630 | (makefile-format-macro-ref macro)))) | |
631 | ||
632 | (defun makefile-browser-fill (targets macros) | |
633 | (setq buffer-read-only nil) | |
634 | (goto-char (point-min)) | |
635 | (erase-buffer) | |
636 | (mapconcat | |
637 | (function | |
638 | (lambda (item) (insert (makefile-browser-format-target-line (car item) nil) "\n"))) | |
639 | targets | |
640 | "") | |
641 | (mapconcat | |
642 | (function | |
643 | (lambda (item) (insert (makefile-browser-format-macro-line (car item) nil) "\n"))) | |
644 | macros | |
645 | "") | |
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)) | |
652 | ||
653 | ||
654 | ;;; | |
655 | ;;; Moving up and down in the browser | |
656 | ;;; | |
657 | ||
658 | (defun makefile-browser-next-line () | |
659 | "Move the browser selection cursor to the next line." | |
660 | (interactive) | |
661 | (if (not (makefile-last-line-p)) | |
662 | (progn | |
663 | (forward-line 1) | |
664 | (forward-char makefile-browser-cursor-column)))) | |
665 | ||
666 | (defun makefile-browser-previous-line () | |
667 | "Move the browser selection cursor to the previous line." | |
668 | (interactive) | |
669 | (if (not (makefile-first-line-p)) | |
670 | (progn | |
671 | (forward-line -1) | |
672 | (forward-char makefile-browser-cursor-column)))) | |
673 | ||
674 | ;;; | |
675 | ;;; Quitting the browser (returns to client buffer) | |
676 | ;;; | |
677 | ||
678 | (defun makefile-browser-quit () | |
679 | "Leave the makefile-browser-buffer and return to the buffer | |
680 | from that it has been entered." | |
681 | (interactive) | |
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))) | |
687 | ||
688 | ;;; | |
689 | ;;; Toggle state of a browser item | |
690 | ;;; | |
691 | ||
692 | (defun makefile-browser-toggle () | |
693 | "Toggle the selection state of the browser item at the cursor position." | |
694 | (interactive) | |
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) | |
700 | (beginning-of-line) | |
701 | (if (makefile-browser-on-macro-line-p) | |
702 | (let ((macro-name (makefile-browser-this-line-macro-name))) | |
703 | (kill-line) | |
704 | (insert | |
705 | (makefile-browser-format-macro-line | |
706 | macro-name | |
707 | (makefile-browser-get-state-for-line this-line)))) | |
708 | (let ((target-name (makefile-browser-this-line-target-name))) | |
709 | (kill-line) | |
710 | (insert | |
711 | (makefile-browser-format-target-line | |
712 | target-name | |
713 | (makefile-browser-get-state-for-line this-line))))) | |
714 | (setq buffer-read-only t) | |
715 | (beginning-of-line) | |
716 | (forward-char makefile-browser-cursor-column) | |
717 | (if makefile-browser-auto-advance-after-selection-p | |
718 | (makefile-browser-next-line)))) | |
719 | ||
720 | ;;; | |
721 | ;;; Making insertions into the client buffer | |
722 | ;;; | |
723 | ||
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." | |
730 | (interactive) | |
731 | (save-excursion | |
732 | (set-buffer makefile-browser-client) | |
733 | (end-of-line) | |
734 | (insert "\\\n\t"))) | |
735 | ||
736 | (defun makefile-browser-insert-selection () | |
737 | "Insert all browser-selected targets and/or macros in the browser\'s | |
738 | client buffer. | |
739 | Insertion takes place at (point)." | |
740 | (interactive) | |
741 | (save-excursion | |
742 | (goto-line 1) | |
743 | (let ((current-line 1)) | |
744 | (while (not (eobp)) | |
745 | (if (makefile-browser-get-state-for-line current-line) | |
746 | (makefile-browser-send-this-line-item)) | |
747 | (forward-line 1) | |
748 | (setq current-line (1+ current-line)))))) | |
749 | ||
750 | (defun makefile-browser-insert-selection-and-quit () | |
751 | (interactive) | |
752 | (makefile-browser-insert-selection) | |
753 | (makefile-browser-quit)) | |
754 | ||
755 | (defun makefile-browser-send-this-line-item () | |
756 | (if (makefile-browser-on-macro-line-p) | |
757 | (save-excursion | |
758 | (let ((macro-name (makefile-browser-this-line-macro-name))) | |
759 | (set-buffer makefile-browser-client) | |
760 | (insert (makefile-format-macro-ref macro-name) " "))) | |
761 | (save-excursion | |
762 | (let ((target-name (makefile-browser-this-line-target-name))) | |
763 | (set-buffer makefile-browser-client) | |
764 | (insert target-name " "))))) | |
765 | ||
766 | ||
767 | (defun makefile-browser-start-interaction () | |
768 | (use-local-map makefile-browser-map) | |
769 | (setq buffer-read-only t)) | |
770 | ||
771 | ||
772 | (defun makefile-browse (targets macros) | |
773 | (interactive) | |
774 | (if (zerop (+ (length targets) (length macros))) | |
775 | (progn | |
776 | (beep) | |
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)))) | |
785 | ||
786 | ||
787 | (defun makefile-switch-to-browser () | |
788 | (interactive) | |
789 | (run-hooks 'makefile-browser-hook) | |
790 | (setq makefile-browser-client (current-buffer)) | |
791 | (makefile-browse makefile-target-table makefile-macro-table)) | |
792 | ||
793 | ||
794 | ;;; ------------------------------------------------------------ | |
795 | ;;; Up-to-date overview buffer | |
796 | ;;; ------------------------------------------------------------ | |
797 | ||
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." | |
804 | (interactive) | |
805 | (if (y-or-n-p "Are you sure that the makefile being edited is consistent? ") | |
806 | ;; | |
807 | ;; The rest of this function operates on a temporary makefile, created by | |
808 | ;; writing the current contents of the makefile buffer. | |
809 | ;; | |
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)) | |
815 | ;; | |
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. | |
821 | ;; | |
822 | (real-targets (progn | |
823 | (makefile-forget-all-targets) | |
824 | (makefile-pickup-targets) | |
825 | makefile-target-table))) | |
826 | ||
827 | (set-buffer makefile-up-to-date-buffer) | |
828 | (setq buffer-read-only nil) | |
829 | (erase-buffer) | |
830 | (makefile-query-targets filename real-targets) | |
831 | (if (zerop (buffer-size)) ; if it did not get us anything | |
832 | (progn | |
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) | |
838 | (progn | |
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)))))) | |
842 | ||
843 | ||
844 | ||
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 | |
850 | ||
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)))) | |
857 | (concat "mktmp" | |
858 | (if (> (length my-name) 3) | |
859 | (substring my-name 0 3) | |
860 | my-name) | |
861 | "." | |
862 | (if (> (length my-uid) 3) | |
863 | (substring my-uid 0 3) | |
864 | my-uid)))) | |
865 | ||
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." | |
870 | (insert | |
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))) | |
876 | target-table "\n")) | |
877 | (goto-char (point-min)) | |
878 | (delete-file filename)) ; remove the tmpfile | |
879 | ||
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)))) | |
882 | ||
883 | (defun makefile-format-up-to-date-buffer-entry (needs-rebuild target) | |
884 | (format "\t%s%s" | |
885 | target | |
886 | (if needs-rebuild | |
887 | makefile-target-needs-rebuild-mark | |
888 | makefile-target-up-to-date-mark))) | |
889 | ||
890 | ||
891 | ;;; ------------------------------------------------------------ | |
892 | ;;; Continuation cleanup | |
893 | ;;; ------------------------------------------------------------ | |
894 | ||
895 | (defun makefile-cleanup-continuations () | |
896 | (if (eq major-mode 'makefile-mode) | |
897 | (if (and makefile-cleanup-continuations-p | |
898 | (not buffer-read-only)) | |
899 | (save-excursion | |
900 | (goto-char (point-min)) | |
901 | (while (re-search-forward "\\\\[ \t]+$" (point-max) t) | |
902 | (replace-match "\\" t t)))))) | |
903 | ||
904 | ;;; ------------------------------------------------------------ | |
905 | ;;; GNU make function support | |
906 | ;;; ------------------------------------------------------------ | |
907 | ||
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." | |
918 | (interactive) | |
919 | (let* ((gm-function-name (completing-read | |
920 | "Function: " | |
921 | makefile-gnumake-functions-alist | |
922 | nil t nil)) | |
923 | (gm-function-prompts | |
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))) | |
930 | " ")))) | |
931 | ||
932 | (defun makefile-prompt-for-gmake-funargs (function-name prompt-list) | |
933 | (mapconcat | |
934 | (function (lambda (one-prompt) | |
935 | (read-string (format "[%s] %s: " function-name one-prompt) nil))) | |
936 | prompt-list | |
937 | ",")) | |
938 | ||
939 | ||
940 | ||
941 | ;;; ------------------------------------------------------------ | |
942 | ;;; Utility functions | |
943 | ;;; ------------------------------------------------------------ | |
944 | ||
945 | (defun makefile-forget-all-targets () | |
946 | "Clear the target-table for this buffer." | |
947 | (interactive) | |
948 | (setq makefile-target-table '())) | |
949 | ||
950 | (defun makefile-forget-all-macros () | |
951 | "Clear the macro-table for this buffer." | |
952 | (interactive) | |
953 | (setq makefile-macro-table '())) | |
954 | ||
955 | ||
956 | (defun makefile-forget-everything () | |
957 | "Clear the macro-table AND the target-table for this buffer." | |
958 | (interactive) | |
959 | (if (y-or-n-p "Really forget all macro- and target information ? ") | |
960 | (progn | |
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.")))) | |
966 | ||
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))))) | |
973 | ||
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))))) | |
980 | ||
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 | |
986 | (insert "\t") | |
987 | (insert " "))) | |
988 | ||
989 | (defun makefile-browser-on-macro-line-p () | |
990 | "Determine if point is on a macro line in the browser." | |
991 | (save-excursion | |
992 | (beginning-of-line) | |
993 | (re-search-forward "\\$[{(]" (makefile-end-of-line-point) t))) | |
994 | ||
995 | (defun makefile-browser-this-line-target-name () | |
996 | "Extract the target name from a line in the browser." | |
997 | (save-excursion | |
998 | (end-of-line) | |
999 | (skip-chars-backward "^ \t") | |
1000 | (buffer-substring (point) (1- (makefile-end-of-line-point))))) | |
1001 | ||
1002 | (defun makefile-browser-this-line-macro-name () | |
1003 | "Extract the macro name from a line in the browser." | |
1004 | (save-excursion | |
1005 | (beginning-of-line) | |
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))))) | |
1010 | ||
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))) | |
1017 | ||
1018 | (defun makefile-browser-get-state-for-line (n) | |
1019 | (aref makefile-browser-selection-vector (1- n))) | |
1020 | ||
1021 | (defun makefile-browser-set-state-for-line (n to-state) | |
1022 | (aset makefile-browser-selection-vector (1- n) to-state)) | |
1023 | ||
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)))) | |
1026 | ||
1027 | (defun makefile-beginning-of-line-point () | |
1028 | (save-excursion | |
1029 | (beginning-of-line) | |
1030 | (point))) | |
1031 | ||
1032 | (defun makefile-end-of-line-point () | |
1033 | (save-excursion | |
1034 | (end-of-line) | |
1035 | (point))) | |
1036 | ||
1037 | (defun makefile-last-line-p () | |
1038 | (= (makefile-end-of-line-point) (point-max))) | |
1039 | ||
1040 | (defun makefile-first-line-p () | |
1041 | (= (makefile-beginning-of-line-point) (point-min))) | |
1042 | ||
1043 | ;; makefile.el ends here |