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