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