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