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