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