(makefile-font-lock-keywords): Highlight
[emacs.git] / lisp / progmodes / make-mode.el
blob62cbdbb291d350d0692c1c26c63eb35e4c0dc4bb
1 ;;; make-mode.el --- makefile editing commands for Emacs
3 ;; Copyright (C) 1992,94,99,2000,2001, 2002 Free Software Foundation, Inc.
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
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 `...'.
16 ;; This file is part of GNU Emacs.
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.
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.
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.
33 ;;; Commentary:
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.
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.
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.
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.
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.
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.
61 ;; There are numerous other customization variables.
64 ;; To Do:
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.
89 ;;; Code:
91 ;; Sadly we need this for a macro.
92 (eval-when-compile
93 (require 'imenu)
94 (require 'dabbrev)
95 (require 'add-log))
97 ;;; ------------------------------------------------------------
98 ;;; Configurable stuff
99 ;;; ------------------------------------------------------------
101 (defgroup makefile nil
102 "Makefile editing commands for Emacs."
103 :group 'tools
104 :prefix "makefile-")
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 'makefile)
113 (defcustom makefile-browser-buffer-name "*Macros and Targets*"
114 "*Name of the macro- and target browser buffer."
115 :type 'string
116 :group 'makefile)
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)
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)
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)
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)
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)
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)
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)
163 (defcustom makefile-backslash-column 48
164 "*Column in which `makefile-backslash-region' inserts backslashes."
165 :type 'integer
166 :group 'makefile)
168 (defcustom makefile-backslash-align t
169 "*If non-nil, `makefile-backslash-region' will align backslashes."
170 :type 'boolean
171 :group 'makefile)
173 (defcustom makefile-browser-selected-mark "+ "
174 "*String used to mark selected entries in the Makefile browser."
175 :type 'string
176 :group 'makefile)
178 (defcustom makefile-browser-unselected-mark " "
179 "*String used to mark unselected entries in the Makefile browser."
180 :type 'string
181 :group 'makefile)
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)
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)
195 (defcustom makefile-cleanup-continuations nil
196 "*If non-nil, automatically clean up continuation lines when saving.
197 A line is cleaned up by removing all whitespace following a trailing
198 backslash. This is done silently.
199 IMPORTANT: Please note that enabling this option causes Makefile mode
200 to MODIFY A FILE WITHOUT YOUR CONFIRMATION when \"it seems necessary\"."
201 :type 'boolean
202 :group 'makefile)
204 (defcustom makefile-mode-hook nil
205 "*Normal hook run by `makefile-mode'."
206 :type 'hook
207 :group 'makefile)
209 (defvar makefile-browser-hook '())
212 ;; Special targets for DMake, Sun's make ...
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)
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)
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.")
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.")
252 (defconst makefile-ignored-files-in-pickup-regex
253 "\\(^\\..*\\)\\|\\(.*~$\\)\\|\\(.*,v$\\)\\|\\(\\.[chy]\\)"
254 "Regex for filenames that will NOT be included in the target list.")
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.")
261 (defconst makefile-font-lock-keywords
262 (list
264 ;; Do macro assignments. These get the "variable-name" face rather
265 ;; arbitrarily.
266 (list makefile-macroassign-regex 1 'font-lock-variable-name-face)
268 ;; Do dependencies. These get the function name face.
269 (list makefile-dependency-regex 1 'font-lock-function-name-face)
271 ;; Variable references even in targets/strings/comments:
272 '("\\$[({]\\([-a-zA-Z0-9_.]+\\)[}):]" 1 font-lock-constant-face prepend)
274 ;; Automatic variable references.
275 '("\\$\\([@%<?^+*]\\)" 1 font-lock-reference-face prepend)
276 '("\\$[({]\\([@%<?^+*][FD]?\\)[}):]" 1 font-lock-reference-face prepend)
278 ;; Fontify conditionals and includes.
279 ;; Note that plain `if' is an automake conditional, and not a bug.
280 (list
281 (concat "^\\(?: [ \t]*\\)?"
282 (regexp-opt '("-include" "-sinclude" "include" "sinclude" "ifeq"
283 "if" "ifneq" "ifdef" "ifndef" "endif" "else"
284 "define" "endef" "override"
285 "export" "unexport" "vpath") t)
286 "\\>[ \t]*\\([^: \t\n#]*\\)")
287 '(1 font-lock-keyword-face) '(2 font-lock-variable-name-face))
289 ;; Highlight lines that contain just whitespace.
290 ;; They can cause trouble, especially if they start with a tab.
291 '("^[ \t]+$" . makefile-space-face)
293 ;; Highlight shell comments that Make treats as commands,
294 ;; since these can fool people.
295 '("^\t+#" 0 makefile-space-face t)
297 ;; Highlight spaces that precede tabs.
298 ;; They can make a tab fail to be effective.
299 '("^\\( +\\)\t" 1 makefile-space-face)))
301 (defvar makefile-imenu-generic-expression
302 (list
303 (list "Dependencies" makefile-dependency-regex 1)
304 (list "Macro Assignment" makefile-macroassign-regex 1))
305 "Imenu generic expression for Makefile mode. See `imenu-generic-expression'.")
307 ;;; ------------------------------------------------------------
308 ;;; The following configurable variables are used in the
309 ;;; up-to-date overview .
310 ;;; The standard configuration assumes that your `make' program
311 ;;; can be run in question/query mode using the `-q' option, this
312 ;;; means that the command
314 ;;; make -q foo
316 ;;; should return an exit status of zero if the target `foo' is
317 ;;; up to date and a nonzero exit status otherwise.
318 ;;; Many makes can do this although the docs/manpages do not mention
319 ;;; it. Try it with your favourite one. GNU make, System V make, and
320 ;;; Dennis Vadura's DMake have no problems.
321 ;;; Set the variable `makefile-brave-make' to the name of the
322 ;;; make utility that does this on your system.
323 ;;; To understand what this is all about see the function definition
324 ;;; of `makefile-query-by-make-minus-q' .
325 ;;; ------------------------------------------------------------
327 (defcustom makefile-brave-make "make"
328 "*How to invoke make, for `makefile-query-targets'.
329 This should identify a `make' command that can handle the `-q' option."
330 :type 'string
331 :group 'makefile)
333 (defcustom makefile-query-one-target-method 'makefile-query-by-make-minus-q
334 "*Function to call to determine whether a make target is up to date.
335 The function must satisfy this calling convention:
337 * As its first argument, it must accept the name of the target to
338 be checked, as a string.
340 * As its second argument, it may accept the name of a makefile
341 as a string. Depending on what you're going to do you may
342 not need this.
344 * It must return the integer value 0 (zero) if the given target
345 should be considered up-to-date in the context of the given
346 makefile, any nonzero integer value otherwise."
347 :type 'function
348 :group 'makefile)
350 (defcustom makefile-up-to-date-buffer-name "*Makefile Up-to-date overview*"
351 "*Name of the Up-to-date overview buffer."
352 :type 'string
353 :group 'makefile)
355 ;;; --- end of up-to-date-overview configuration ------------------
357 (defvar makefile-mode-abbrev-table nil
358 "Abbrev table in use in Makefile buffers.")
359 (if makefile-mode-abbrev-table
361 (define-abbrev-table 'makefile-mode-abbrev-table ()))
363 (defvar makefile-mode-map nil
364 "The keymap that is used in Makefile mode.")
366 (if makefile-mode-map
368 (setq makefile-mode-map (make-sparse-keymap))
369 ;; set up the keymap
370 (define-key makefile-mode-map "\C-c:" 'makefile-insert-target-ref)
371 (if makefile-electric-keys
372 (progn
373 (define-key makefile-mode-map "$" 'makefile-insert-macro-ref)
374 (define-key makefile-mode-map ":" 'makefile-electric-colon)
375 (define-key makefile-mode-map "=" 'makefile-electric-equal)
376 (define-key makefile-mode-map "." 'makefile-electric-dot)))
377 (define-key makefile-mode-map "\C-c\C-f" 'makefile-pickup-filenames-as-targets)
378 (define-key makefile-mode-map "\C-c\C-b" 'makefile-switch-to-browser)
379 (define-key makefile-mode-map "\C-c\C-c" 'comment-region)
380 (define-key makefile-mode-map "\C-c\C-p" 'makefile-pickup-everything)
381 (define-key makefile-mode-map "\C-c\C-u" 'makefile-create-up-to-date-overview)
382 (define-key makefile-mode-map "\C-c\C-i" 'makefile-insert-gmake-function)
383 (define-key makefile-mode-map "\C-c\C-\\" 'makefile-backslash-region)
384 (define-key makefile-mode-map "\M-p" 'makefile-previous-dependency)
385 (define-key makefile-mode-map "\M-n" 'makefile-next-dependency)
386 (define-key makefile-mode-map "\e\t" 'makefile-complete)
388 ;; Make menus.
389 (define-key makefile-mode-map [menu-bar makefile-mode]
390 (cons "Makefile" (make-sparse-keymap "Makefile")))
392 (define-key makefile-mode-map [menu-bar makefile-mode browse]
393 '("Pop up Makefile Browser" . makefile-switch-to-browser))
394 (define-key makefile-mode-map [menu-bar makefile-mode complete]
395 '("Complete Target or Macro" . makefile-complete))
396 (define-key makefile-mode-map [menu-bar makefile-mode pickup]
397 '("Find Targets and Macros" . makefile-pickup-everything))
399 (define-key makefile-mode-map [menu-bar makefile-mode prev]
400 '("Move to Previous Dependency" . makefile-previous-dependency))
401 (define-key makefile-mode-map [menu-bar makefile-mode next]
402 '("Move to Next Dependency" . makefile-next-dependency)))
404 (defvar makefile-browser-map nil
405 "The keymap that is used in the macro- and target browser.")
406 (if makefile-browser-map
408 (setq makefile-browser-map (make-sparse-keymap))
409 (define-key makefile-browser-map "n" 'makefile-browser-next-line)
410 (define-key makefile-browser-map "\C-n" 'makefile-browser-next-line)
411 (define-key makefile-browser-map "p" 'makefile-browser-previous-line)
412 (define-key makefile-browser-map "\C-p" 'makefile-browser-previous-line)
413 (define-key makefile-browser-map " " 'makefile-browser-toggle)
414 (define-key makefile-browser-map "i" 'makefile-browser-insert-selection)
415 (define-key makefile-browser-map "I" 'makefile-browser-insert-selection-and-quit)
416 (define-key makefile-browser-map "\C-c\C-m" 'makefile-browser-insert-continuation)
417 (define-key makefile-browser-map "q" 'makefile-browser-quit)
418 ;; disable horizontal movement
419 (define-key makefile-browser-map "\C-b" 'undefined)
420 (define-key makefile-browser-map "\C-f" 'undefined))
423 (defvar makefile-mode-syntax-table nil)
424 (if makefile-mode-syntax-table
426 (setq makefile-mode-syntax-table (make-syntax-table))
427 (modify-syntax-entry ?\( "() " makefile-mode-syntax-table)
428 (modify-syntax-entry ?\) ")( " makefile-mode-syntax-table)
429 (modify-syntax-entry ?\[ "(] " makefile-mode-syntax-table)
430 (modify-syntax-entry ?\] ")[ " makefile-mode-syntax-table)
431 (modify-syntax-entry ?\{ "(} " makefile-mode-syntax-table)
432 (modify-syntax-entry ?\} "){ " makefile-mode-syntax-table)
433 (modify-syntax-entry ?\' "\" " makefile-mode-syntax-table)
434 (modify-syntax-entry ?\` "\" " makefile-mode-syntax-table)
435 (modify-syntax-entry ?# "< " makefile-mode-syntax-table)
436 (modify-syntax-entry ?\n "> " makefile-mode-syntax-table))
439 ;;; ------------------------------------------------------------
440 ;;; Internal variables.
441 ;;; You don't need to configure below this line.
442 ;;; ------------------------------------------------------------
444 (defvar makefile-target-table nil
445 "Table of all target names known for this buffer.")
447 (defvar makefile-macro-table nil
448 "Table of all macro names known for this buffer.")
450 (defvar makefile-browser-client
451 "A buffer in Makefile mode that is currently using the browser.")
453 (defvar makefile-browser-selection-vector nil)
454 (defvar makefile-has-prereqs nil)
455 (defvar makefile-need-target-pickup t)
456 (defvar makefile-need-macro-pickup t)
458 (defvar makefile-mode-hook '())
460 ;; Each element looks like '("GNU MAKE FUNCTION" "ARG" "ARG" ... )
461 ;; Each "ARG" is used as a prompt for a required argument.
462 (defconst makefile-gnumake-functions-alist
464 ;; Text functions
465 ("subst" "From" "To" "In")
466 ("patsubst" "Pattern" "Replacement" "In")
467 ("strip" "Text")
468 ("findstring" "Find what" "In")
469 ("filter" "Pattern" "Text")
470 ("filter-out" "Pattern" "Text")
471 ("sort" "List")
472 ;; Filename functions
473 ("dir" "Names")
474 ("notdir" "Names")
475 ("suffix" "Names")
476 ("basename" "Names")
477 ("addprefix" "Prefix" "Names")
478 ("addsuffix" "Suffix" "Names")
479 ("join" "List 1" "List 2")
480 ("word" "Index" "Text")
481 ("words" "Text")
482 ("firstword" "Text")
483 ("wildcard" "Pattern")
484 ;; Misc functions
485 ("foreach" "Variable" "List" "Text")
486 ("origin" "Variable")
487 ("shell" "Command")))
490 ;;; ------------------------------------------------------------
491 ;;; The mode function itself.
492 ;;; ------------------------------------------------------------
494 ;;;###autoload
495 (defun makefile-mode ()
496 "Major mode for editing Makefiles.
497 This function ends by invoking the function(s) `makefile-mode-hook'.
499 \\{makefile-mode-map}
501 In the browser, use the following keys:
503 \\{makefile-browser-map}
505 Makefile mode can be configured by modifying the following variables:
507 `makefile-browser-buffer-name':
508 Name of the macro- and target browser buffer.
510 `makefile-target-colon':
511 The string that gets appended to all target names
512 inserted by `makefile-insert-target'.
513 \":\" or \"::\" are quite common values.
515 `makefile-macro-assign':
516 The string that gets appended to all macro names
517 inserted by `makefile-insert-macro'.
518 The normal value should be \" = \", since this is what
519 standard make expects. However, newer makes such as dmake
520 allow a larger variety of different macro assignments, so you
521 might prefer to use \" += \" or \" := \" .
523 `makefile-tab-after-target-colon':
524 If you want a TAB (instead of a space) to be appended after the
525 target colon, then set this to a non-nil value.
527 `makefile-browser-leftmost-column':
528 Number of blanks to the left of the browser selection mark.
530 `makefile-browser-cursor-column':
531 Column in which the cursor is positioned when it moves
532 up or down in the browser.
534 `makefile-browser-selected-mark':
535 String used to mark selected entries in the browser.
537 `makefile-browser-unselected-mark':
538 String used to mark unselected entries in the browser.
540 `makefile-browser-auto-advance-after-selection-p':
541 If this variable is set to a non-nil value the cursor
542 will automagically advance to the next line after an item
543 has been selected in the browser.
545 `makefile-pickup-everything-picks-up-filenames-p':
546 If this variable is set to a non-nil value then
547 `makefile-pickup-everything' also picks up filenames as targets
548 (i.e. it calls `makefile-pickup-filenames-as-targets'), otherwise
549 filenames are omitted.
551 `makefile-cleanup-continuations':
552 If this variable is set to a non-nil value then Makefile mode
553 will assure that no line in the file ends with a backslash
554 (the continuation character) followed by any whitespace.
555 This is done by silently removing the trailing whitespace, leaving
556 the backslash itself intact.
557 IMPORTANT: Please note that enabling this option causes Makefile mode
558 to MODIFY A FILE WITHOUT YOUR CONFIRMATION when \"it seems necessary\".
560 `makefile-browser-hook':
561 A function or list of functions to be called just before the
562 browser is entered. This is executed in the makefile buffer.
564 `makefile-special-targets-list':
565 List of special targets. You will be offered to complete
566 on one of those in the minibuffer whenever you enter a `.'.
567 at the beginning of a line in Makefile mode."
569 (interactive)
570 (kill-all-local-variables)
571 (add-hook 'write-file-functions
572 'makefile-warn-suspicious-lines nil t)
573 (add-hook 'write-file-functions
574 'makefile-warn-continuations nil t)
575 (add-hook 'write-file-functions
576 'makefile-cleanup-continuations nil t)
577 (make-local-variable 'makefile-target-table)
578 (make-local-variable 'makefile-macro-table)
579 (make-local-variable 'makefile-has-prereqs)
580 (make-local-variable 'makefile-need-target-pickup)
581 (make-local-variable 'makefile-need-macro-pickup)
583 ;; Font lock.
584 (make-local-variable 'font-lock-defaults)
585 (setq font-lock-defaults
586 ;; SYNTAX-BEGIN set to backward-paragraph to avoid slow-down
587 ;; near the end of a large buffer, due to parse-partial-sexp's
588 ;; trying to parse all the way till the beginning of buffer.
589 '(makefile-font-lock-keywords nil nil nil backward-paragraph))
591 ;; Add-log.
592 (make-local-variable 'add-log-current-defun-function)
593 (setq add-log-current-defun-function 'makefile-add-log-defun)
595 ;; Imenu.
596 (make-local-variable 'imenu-generic-expression)
597 (setq imenu-generic-expression makefile-imenu-generic-expression)
599 ;; Dabbrev.
600 (make-local-variable 'dabbrev-abbrev-skip-leading-regexp)
601 (setq dabbrev-abbrev-skip-leading-regexp "\\$")
603 ;; Other abbrevs.
604 (setq local-abbrev-table makefile-mode-abbrev-table)
606 ;; Filling.
607 (make-local-variable 'fill-paragraph-function)
608 (setq fill-paragraph-function 'makefile-fill-paragraph)
610 ;; Comment stuff.
611 (make-local-variable 'comment-start)
612 (setq comment-start "#")
613 (make-local-variable 'comment-end)
614 (setq comment-end "")
615 (make-local-variable 'comment-start-skip)
616 (setq comment-start-skip "#+[ \t]*")
618 ;; Make sure TAB really inserts \t.
619 (set (make-local-variable 'indent-line-function) 'indent-to-left-margin)
621 ;; become the current major mode
622 (setq major-mode 'makefile-mode)
623 (setq mode-name "Makefile")
625 ;; Activate keymap and syntax table.
626 (use-local-map makefile-mode-map)
627 (set-syntax-table makefile-mode-syntax-table)
629 ;; Real TABs are important in makefiles
630 (setq indent-tabs-mode t)
631 (run-hooks 'makefile-mode-hook))
635 ;;; Motion code.
637 (defun makefile-next-dependency ()
638 "Move point to the beginning of the next dependency line."
639 (interactive)
640 (let ((here (point)))
641 (end-of-line)
642 (if (re-search-forward makefile-dependency-regex (point-max) t)
643 (progn (beginning-of-line) t) ; indicate success
644 (goto-char here) nil)))
646 (defun makefile-previous-dependency ()
647 "Move point to the beginning of the previous dependency line."
648 (interactive)
649 (let ((here (point)))
650 (beginning-of-line)
651 (if (re-search-backward makefile-dependency-regex (point-min) t)
652 (progn (beginning-of-line) t) ; indicate success
653 (goto-char here) nil)))
657 ;;; Electric keys. Blech.
659 (defun makefile-electric-dot (arg)
660 "Prompt for the name of a special target to insert.
661 Only does electric insertion at beginning of line.
662 Anywhere else just self-inserts."
663 (interactive "p")
664 (if (bolp)
665 (makefile-insert-special-target)
666 (self-insert-command arg)))
668 (defun makefile-insert-special-target ()
669 "Prompt for and insert a special target name.
670 Uses `makefile-special-targets' list."
671 (interactive)
672 (makefile-pickup-targets)
673 (let ((special-target
674 (completing-read "Special target: "
675 makefile-special-targets-list nil nil nil)))
676 (if (zerop (length special-target))
678 (insert "." special-target ":")
679 (makefile-forward-after-target-colon))))
681 (defun makefile-electric-equal (arg)
682 "Prompt for name of a macro to insert.
683 Only does prompting if point is at beginning of line.
684 Anywhere else just self-inserts."
685 (interactive "p")
686 (makefile-pickup-macros)
687 (if (bolp)
688 (call-interactively 'makefile-insert-macro)
689 (self-insert-command arg)))
691 (defun makefile-insert-macro (macro-name)
692 "Prepare definition of a new macro."
693 (interactive "sMacro Name: ")
694 (makefile-pickup-macros)
695 (if (not (zerop (length macro-name)))
696 (progn
697 (beginning-of-line)
698 (insert macro-name makefile-macro-assign)
699 (setq makefile-need-macro-pickup t)
700 (makefile-remember-macro macro-name))))
702 (defun makefile-insert-macro-ref (macro-name)
703 "Complete on a list of known macros, then insert complete ref at point."
704 (interactive
705 (list
706 (progn
707 (makefile-pickup-macros)
708 (completing-read "Refer to macro: " makefile-macro-table nil nil nil))))
709 (makefile-do-macro-insertion macro-name))
711 (defun makefile-insert-target (target-name)
712 "Prepare definition of a new target (dependency line)."
713 (interactive "sTarget: ")
714 (if (not (zerop (length target-name)))
715 (progn
716 (beginning-of-line)
717 (insert target-name makefile-target-colon)
718 (makefile-forward-after-target-colon)
719 (end-of-line)
720 (setq makefile-need-target-pickup t)
721 (makefile-remember-target target-name))))
723 (defun makefile-insert-target-ref (target-name)
724 "Complete on a list of known targets, then insert TARGET-NAME at point."
725 (interactive
726 (list
727 (progn
728 (makefile-pickup-targets)
729 (completing-read "Refer to target: " makefile-target-table nil nil nil))))
730 (if (not (zerop (length target-name)))
731 (insert target-name " ")))
733 (defun makefile-electric-colon (arg)
734 "Prompt for name of new target.
735 Prompting only happens at beginning of line.
736 Anywhere else just self-inserts."
737 (interactive "p")
738 (if (bolp)
739 (call-interactively 'makefile-insert-target)
740 (self-insert-command arg)))
744 ;;; ------------------------------------------------------------
745 ;;; Extracting targets and macros from an existing makefile
746 ;;; ------------------------------------------------------------
748 (defun makefile-pickup-targets ()
749 "Notice names of all target definitions in Makefile."
750 (interactive)
751 (if (not makefile-need-target-pickup)
753 (setq makefile-need-target-pickup nil)
754 (setq makefile-target-table nil)
755 (setq makefile-has-prereqs nil)
756 (save-excursion
757 (goto-char (point-min))
758 (while (re-search-forward makefile-dependency-regex nil t)
759 (makefile-add-this-line-targets)))
760 (message "Read targets OK.")))
762 (defun makefile-add-this-line-targets ()
763 (save-excursion
764 (beginning-of-line)
765 (let ((done-with-line nil)
766 (line-number (1+ (count-lines (point-min) (point)))))
767 (while (not done-with-line)
768 (skip-chars-forward " \t")
769 (if (not (setq done-with-line (or (eolp)
770 (char-equal (char-after (point)) ?:))))
771 (progn
772 (let* ((start-of-target-name (point))
773 (target-name
774 (progn
775 (skip-chars-forward "^ \t:#")
776 (buffer-substring start-of-target-name (point))))
777 (has-prereqs
778 (not (looking-at ":[ \t]*$"))))
779 (if (makefile-remember-target target-name has-prereqs)
780 (message "Picked up target \"%s\" from line %d"
781 target-name line-number)))))))))
783 (defun makefile-pickup-macros ()
784 "Notice names of all macro definitions in Makefile."
785 (interactive)
786 (if (not makefile-need-macro-pickup)
788 (setq makefile-need-macro-pickup nil)
789 (setq makefile-macro-table nil)
790 (save-excursion
791 (goto-char (point-min))
792 (while (re-search-forward makefile-macroassign-regex nil t)
793 (makefile-add-this-line-macro)
794 (forward-line 1)))
795 (message "Read macros OK.")))
797 (defun makefile-add-this-line-macro ()
798 (save-excursion
799 (beginning-of-line)
800 (skip-chars-forward " \t")
801 (unless (eolp)
802 (let* ((start-of-macro-name (point))
803 (line-number (1+ (count-lines (point-min) (point))))
804 (macro-name (progn
805 (skip-chars-forward "^ \t:#=*")
806 (buffer-substring start-of-macro-name (point)))))
807 (if (makefile-remember-macro macro-name)
808 (message "Picked up macro \"%s\" from line %d"
809 macro-name line-number))))))
811 (defun makefile-pickup-everything (arg)
812 "Notice names of all macros and targets in Makefile.
813 Prefix arg means force pickups to be redone."
814 (interactive "P")
815 (if arg
816 (progn
817 (setq makefile-need-target-pickup t)
818 (setq makefile-need-macro-pickup t)))
819 (makefile-pickup-macros)
820 (makefile-pickup-targets)
821 (if makefile-pickup-everything-picks-up-filenames-p
822 (makefile-pickup-filenames-as-targets)))
824 (defun makefile-pickup-filenames-as-targets ()
825 "Scan the current directory for filenames to use as targets.
826 Checks each filename against `makefile-ignored-files-in-pickup-regex'
827 and adds all qualifying names to the list of known targets."
828 (interactive)
829 (let* ((dir (file-name-directory (buffer-file-name)))
830 (raw-filename-list (if dir
831 (file-name-all-completions "" dir)
832 (file-name-all-completions "" ""))))
833 (mapcar (lambda (name)
834 (if (and (not (file-directory-p name))
835 (not (string-match makefile-ignored-files-in-pickup-regex
836 name)))
837 (if (makefile-remember-target name)
838 (message "Picked up file \"%s\" as target" name))))
839 raw-filename-list)))
843 ;;; Completion.
845 (defun makefile-complete ()
846 "Perform completion on Makefile construct preceding point.
847 Can complete variable and target names.
848 The context determines which are considered."
849 (interactive)
850 (let* ((beg (save-excursion
851 (skip-chars-backward "^$(){}:#= \t\n")
852 (point)))
853 (try (buffer-substring beg (point)))
854 (do-macros nil)
855 (paren nil))
857 (save-excursion
858 (goto-char beg)
859 (let ((pc (preceding-char)))
860 (cond
861 ;; Beginning of line means anything.
862 ((bolp)
865 ;; Preceding "$" means macros only.
866 ((= pc ?$)
867 (setq do-macros t))
869 ;; Preceding "$(" or "${" means macros only.
870 ((and (or (= pc ?{)
871 (= pc ?\())
872 (progn
873 (setq paren pc)
874 (backward-char)
875 (and (not (bolp))
876 (= (preceding-char) ?$))))
877 (setq do-macros t)))))
879 ;; Try completion.
880 (let* ((table (append (if do-macros
882 makefile-target-table)
883 makefile-macro-table))
884 (completion (try-completion try table)))
885 (cond
886 ;; Exact match, so insert closing paren or colon.
887 ((eq completion t)
888 (insert (if do-macros
889 (if (eq paren ?{)
891 ?\))
892 (if (save-excursion
893 (goto-char beg)
894 (bolp))
896 " "))))
898 ;; No match.
899 ((null completion)
900 (message "Can't find completion for \"%s\"" try)
901 (ding))
903 ;; Partial completion.
904 ((not (string= try completion))
905 ;; FIXME it would be nice to supply the closing paren if an
906 ;; exact, unambiguous match were found. That is not possible
907 ;; right now. Ditto closing ":" for targets.
908 (delete-region beg (point))
910 ;; DO-MACROS means doing macros only. If not that, then check
911 ;; to see if this completion is a macro. Special insertion
912 ;; must be done for macros.
913 (if (or do-macros
914 (assoc completion makefile-macro-table))
915 (let ((makefile-use-curly-braces-for-macros-p
916 (or (eq paren ?{)
917 makefile-use-curly-braces-for-macros-p)))
918 (delete-backward-char 2)
919 (makefile-do-macro-insertion completion)
920 (delete-backward-char 1))
922 ;; Just insert targets.
923 (insert completion)))
925 ;; Can't complete any more, so make completion list. FIXME
926 ;; this doesn't do the right thing when the completion is
927 ;; actually inserted. I don't think there is an easy way to do
928 ;; that.
930 (message "Making completion list...")
931 (let ((list (all-completions try table)))
932 (with-output-to-temp-buffer "*Completions*"
933 (display-completion-list list)))
934 (message "Making completion list...done"))))))
938 ;; Backslashification. Stolen from cc-mode.el.
940 (defun makefile-backslash-region (from to delete-flag)
941 "Insert, align, or delete end-of-line backslashes on the lines in the region.
942 With no argument, inserts backslashes and aligns existing backslashes.
943 With an argument, deletes the backslashes.
945 This function does not modify the last line of the region if the region ends
946 right at the start of the following line; it does not modify blank lines
947 at the start of the region. So you can put the region around an entire macro
948 definition and conveniently use this command."
949 (interactive "r\nP")
950 (save-excursion
951 (goto-char from)
952 (let ((column makefile-backslash-column)
953 (endmark (make-marker)))
954 (move-marker endmark to)
955 ;; Compute the smallest column number past the ends of all the lines.
956 (if makefile-backslash-align
957 (progn
958 (if (not delete-flag)
959 (while (< (point) to)
960 (end-of-line)
961 (if (= (preceding-char) ?\\)
962 (progn (forward-char -1)
963 (skip-chars-backward " \t")))
964 (setq column (max column (1+ (current-column))))
965 (forward-line 1)))
966 ;; Adjust upward to a tab column, if that doesn't push
967 ;; past the margin.
968 (if (> (% column tab-width) 0)
969 (let ((adjusted (* (/ (+ column tab-width -1) tab-width)
970 tab-width)))
971 (if (< adjusted (window-width))
972 (setq column adjusted))))))
973 ;; Don't modify blank lines at start of region.
974 (goto-char from)
975 (while (and (< (point) endmark) (eolp))
976 (forward-line 1))
977 ;; Add or remove backslashes on all the lines.
978 (while (and (< (point) endmark)
979 ;; Don't backslashify the last line
980 ;; if the region ends right at the start of the next line.
981 (save-excursion
982 (forward-line 1)
983 (< (point) endmark)))
984 (if (not delete-flag)
985 (makefile-append-backslash column)
986 (makefile-delete-backslash))
987 (forward-line 1))
988 (move-marker endmark nil))))
990 (defun makefile-append-backslash (column)
991 (end-of-line)
992 ;; Note that "\\\\" is needed to get one backslash.
993 (if (= (preceding-char) ?\\)
994 (progn (forward-char -1)
995 (delete-horizontal-space)
996 (indent-to column (if makefile-backslash-align nil 1)))
997 (indent-to column (if makefile-backslash-align nil 1))
998 (insert "\\")))
1000 (defun makefile-delete-backslash ()
1001 (end-of-line)
1002 (or (bolp)
1003 (progn
1004 (forward-char -1)
1005 (if (looking-at "\\\\")
1006 (delete-region (1+ (point))
1007 (progn (skip-chars-backward " \t") (point)))))))
1011 ;; Filling
1013 (defun makefile-fill-paragraph (arg)
1014 ;; Fill comments, backslashed lines, and variable definitions
1015 ;; specially.
1016 (save-excursion
1017 (beginning-of-line)
1018 (cond
1019 ((looking-at "^#+ ")
1020 ;; Found a comment. Set the fill prefix, and find the paragraph
1021 ;; boundaries by searching for lines that look like comment-only
1022 ;; lines.
1023 (let ((fill-prefix (match-string-no-properties 0))
1024 (fill-paragraph-function nil))
1025 (save-excursion
1026 (save-restriction
1027 (narrow-to-region
1028 ;; Search backwards.
1029 (save-excursion
1030 (while (and (zerop (forward-line -1))
1031 (looking-at "^#")))
1032 ;; We may have gone too far. Go forward again.
1033 (or (looking-at "^#")
1034 (forward-line 1))
1035 (point))
1036 ;; Search forwards.
1037 (save-excursion
1038 (while (looking-at "^#")
1039 (forward-line))
1040 (point)))
1041 (fill-paragraph nil)
1042 t))))
1044 ;; Must look for backslashed-region before looking for variable
1045 ;; assignment.
1046 ((or (eq (char-before (line-end-position 1)) ?\\)
1047 (eq (char-before (line-end-position 0)) ?\\))
1048 ;; A backslash region. Find beginning and end, remove
1049 ;; backslashes, fill, and then reapply backslahes.
1050 (end-of-line)
1051 (let ((beginning
1052 (save-excursion
1053 (end-of-line 0)
1054 (while (= (preceding-char) ?\\)
1055 (end-of-line 0))
1056 (forward-char)
1057 (point)))
1058 (end
1059 (save-excursion
1060 (while (= (preceding-char) ?\\)
1061 (end-of-line 2))
1062 (point))))
1063 (save-restriction
1064 (narrow-to-region beginning end)
1065 (makefile-backslash-region (point-min) (point-max) t)
1066 (let ((fill-paragraph-function nil))
1067 (fill-paragraph nil))
1068 (makefile-backslash-region (point-min) (point-max) nil)
1069 (goto-char (point-max))
1070 (if (< (skip-chars-backward "\n") 0)
1071 (delete-region (point) (point-max))))))
1073 ((looking-at makefile-macroassign-regex)
1074 ;; Have a macro assign. Fill just this line, and then backslash
1075 ;; resulting region.
1076 (save-restriction
1077 (narrow-to-region (point) (line-beginning-position 2))
1078 (let ((fill-paragraph-function nil))
1079 (fill-paragraph nil))
1080 (makefile-backslash-region (point-min) (point-max) nil)))))
1082 ;; Always return non-nil so we don't fill anything else.
1087 ;;; ------------------------------------------------------------
1088 ;;; Browser mode.
1089 ;;; ------------------------------------------------------------
1091 (defun makefile-browser-format-target-line (target selected)
1092 (format
1093 (concat (make-string makefile-browser-leftmost-column ?\ )
1094 (if selected
1095 makefile-browser-selected-mark
1096 makefile-browser-unselected-mark)
1097 "%s%s")
1098 target makefile-target-colon))
1100 (defun makefile-browser-format-macro-line (macro selected)
1101 (format
1102 (concat (make-string makefile-browser-leftmost-column ?\ )
1103 (if selected
1104 makefile-browser-selected-mark
1105 makefile-browser-unselected-mark)
1106 (makefile-format-macro-ref macro))))
1108 (defun makefile-browser-fill (targets macros)
1109 (let ((inhibit-read-only t))
1110 (goto-char (point-min))
1111 (erase-buffer)
1112 (mapconcat
1113 (function
1114 (lambda (item) (insert (makefile-browser-format-target-line (car item) nil) "\n")))
1115 targets
1117 (mapconcat
1118 (function
1119 (lambda (item) (insert (makefile-browser-format-macro-line (car item) nil) "\n")))
1120 macros
1122 (sort-lines nil (point-min) (point-max))
1123 (goto-char (1- (point-max)))
1124 (delete-char 1) ; remove unnecessary newline at eob
1125 (goto-char (point-min))
1126 (forward-char makefile-browser-cursor-column)))
1129 ;;; Moving up and down in the browser
1132 (defun makefile-browser-next-line ()
1133 "Move the browser selection cursor to the next line."
1134 (interactive)
1135 (if (not (makefile-last-line-p))
1136 (progn
1137 (forward-line 1)
1138 (forward-char makefile-browser-cursor-column))))
1140 (defun makefile-browser-previous-line ()
1141 "Move the browser selection cursor to the previous line."
1142 (interactive)
1143 (if (not (makefile-first-line-p))
1144 (progn
1145 (forward-line -1)
1146 (forward-char makefile-browser-cursor-column))))
1149 ;;; Quitting the browser (returns to client buffer)
1152 (defun makefile-browser-quit ()
1153 "Leave the browser and return to the makefile buffer."
1154 (interactive)
1155 (let ((my-client makefile-browser-client))
1156 (setq makefile-browser-client nil) ; we quitted, so NO client!
1157 (set-buffer-modified-p nil)
1158 (quit-window t)
1159 (pop-to-buffer my-client)))
1162 ;;; Toggle state of a browser item
1165 (defun makefile-browser-toggle ()
1166 "Toggle the selection state of the browser item at the cursor position."
1167 (interactive)
1168 (let ((this-line (count-lines (point-min) (point))))
1169 (setq this-line (max 1 this-line))
1170 (makefile-browser-toggle-state-for-line this-line)
1171 (goto-line this-line)
1172 (let ((inhibit-read-only t))
1173 (beginning-of-line)
1174 (if (makefile-browser-on-macro-line-p)
1175 (let ((macro-name (makefile-browser-this-line-macro-name)))
1176 (delete-region (point) (progn (end-of-line) (point)))
1177 (insert
1178 (makefile-browser-format-macro-line
1179 macro-name
1180 (makefile-browser-get-state-for-line this-line))))
1181 (let ((target-name (makefile-browser-this-line-target-name)))
1182 (delete-region (point) (progn (end-of-line) (point)))
1183 (insert
1184 (makefile-browser-format-target-line
1185 target-name
1186 (makefile-browser-get-state-for-line this-line))))))
1187 (beginning-of-line)
1188 (forward-char makefile-browser-cursor-column)
1189 (if makefile-browser-auto-advance-after-selection-p
1190 (makefile-browser-next-line))))
1193 ;;; Making insertions into the client buffer
1196 (defun makefile-browser-insert-continuation ()
1197 "Insert a makefile continuation.
1198 In the makefile buffer, go to (end-of-line), insert a \'\\\'
1199 character, insert a new blank line, go to that line and indent by one TAB.
1200 This is most useful in the process of creating continued lines when copying
1201 large dependencies from the browser to the client buffer.
1202 \(point) advances accordingly in the client buffer."
1203 (interactive)
1204 (with-current-buffer makefile-browser-client
1205 (end-of-line)
1206 (insert "\\\n\t")))
1208 (defun makefile-browser-insert-selection ()
1209 "Insert all selected targets and/or macros in the makefile buffer.
1210 Insertion takes place at point."
1211 (interactive)
1212 (save-excursion
1213 (goto-line 1)
1214 (let ((current-line 1))
1215 (while (not (eobp))
1216 (if (makefile-browser-get-state-for-line current-line)
1217 (makefile-browser-send-this-line-item))
1218 (forward-line 1)
1219 (setq current-line (1+ current-line))))))
1221 (defun makefile-browser-insert-selection-and-quit ()
1222 (interactive)
1223 (makefile-browser-insert-selection)
1224 (makefile-browser-quit))
1226 (defun makefile-browser-send-this-line-item ()
1227 (if (makefile-browser-on-macro-line-p)
1228 (save-excursion
1229 (let ((macro-name (makefile-browser-this-line-macro-name)))
1230 (set-buffer makefile-browser-client)
1231 (insert (makefile-format-macro-ref macro-name) " ")))
1232 (save-excursion
1233 (let ((target-name (makefile-browser-this-line-target-name)))
1234 (set-buffer makefile-browser-client)
1235 (insert target-name " ")))))
1237 (defun makefile-browser-start-interaction ()
1238 (use-local-map makefile-browser-map)
1239 (setq buffer-read-only t))
1241 (defun makefile-browse (targets macros)
1242 (interactive)
1243 (if (zerop (+ (length targets) (length macros)))
1244 (progn
1245 (beep)
1246 (message "No macros or targets to browse! Consider running 'makefile-pickup-everything\'"))
1247 (let ((browser-buffer (get-buffer-create makefile-browser-buffer-name)))
1248 (pop-to-buffer browser-buffer)
1249 (makefile-browser-fill targets macros)
1250 (shrink-window-if-larger-than-buffer)
1251 (set (make-local-variable 'makefile-browser-selection-vector)
1252 (make-vector (+ (length targets) (length macros)) nil))
1253 (makefile-browser-start-interaction))))
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))
1265 ;;; ------------------------------------------------------------
1266 ;;; Up-to-date overview buffer
1267 ;;; ------------------------------------------------------------
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? ")
1277 ;; The rest of this function operates on a temporary makefile, created by
1278 ;; writing the current contents of the makefile buffer.
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))
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.
1292 (real-targets (progn
1293 (makefile-pickup-targets)
1294 makefile-target-table))
1295 (prereqs makefile-has-prereqs)
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))))))
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
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)
1332 (if (> (length my-uid) 3)
1333 (substring my-uid 0 3)
1334 my-uid))))
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"))))
1356 target-table "\n"))
1357 (goto-char (point-min))
1358 (delete-file filename)) ; remove the tmpfile
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))))
1367 ;;; ------------------------------------------------------------
1368 ;;; Continuation cleanup
1369 ;;; ------------------------------------------------------------
1371 (defun makefile-cleanup-continuations ()
1372 (if (eq major-mode 'makefile-mode)
1373 (if (and makefile-cleanup-continuations
1374 (not buffer-read-only))
1375 (save-excursion
1376 (goto-char (point-min))
1377 (while (re-search-forward "\\\\[ \t]+$" nil t)
1378 (replace-match "\\" t t))))))
1381 ;;; ------------------------------------------------------------
1382 ;;; Warn of suspicious lines
1383 ;;; ------------------------------------------------------------
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)))))))))
1395 (defun makefile-warn-continuations ()
1396 (if (eq major-mode 'makefile-mode)
1397 (save-excursion
1398 (goto-char (point-min))
1399 (if (re-search-forward "\\\\[ \t]+$" nil t)
1400 (not (y-or-n-p
1401 (format "Suspicious continuation in line %d. Save anyway? "
1402 (count-lines (point-min) (point)))))))))
1405 ;;; ------------------------------------------------------------
1406 ;;; GNU make function support
1407 ;;; ------------------------------------------------------------
1409 (defun makefile-insert-gmake-function ()
1410 "Insert a GNU make function call.
1411 Asks for the name of the function to use (with completion).
1412 Then prompts for all required parameters."
1413 (interactive)
1414 (let* ((gm-function-name (completing-read
1415 "Function: "
1416 makefile-gnumake-functions-alist
1417 nil t nil))
1418 (gm-function-prompts
1419 (cdr (assoc gm-function-name makefile-gnumake-functions-alist))))
1420 (if (not (zerop (length gm-function-name)))
1421 (insert (makefile-format-macro-ref
1422 (concat gm-function-name " "
1423 (makefile-prompt-for-gmake-funargs
1424 gm-function-name gm-function-prompts)))
1425 " "))))
1427 (defun makefile-prompt-for-gmake-funargs (function-name prompt-list)
1428 (mapconcat
1429 (function (lambda (one-prompt)
1430 (read-string (format "[%s] %s: " function-name one-prompt)
1431 nil)))
1432 prompt-list
1433 ","))
1437 ;;; ------------------------------------------------------------
1438 ;;; Utility functions
1439 ;;; ------------------------------------------------------------
1441 (defun makefile-do-macro-insertion (macro-name)
1442 "Insert a macro reference."
1443 (if (not (zerop (length macro-name)))
1444 (if (assoc macro-name makefile-runtime-macros-list)
1445 (insert "$" macro-name)
1446 (insert (makefile-format-macro-ref macro-name)))))
1448 (defun makefile-remember-target (target-name &optional has-prereqs)
1449 "Remember a given target if it is not already remembered for this buffer."
1450 (if (not (zerop (length target-name)))
1451 (progn
1452 (if (not (assoc target-name makefile-target-table))
1453 (setq makefile-target-table
1454 (cons (list target-name) makefile-target-table)))
1455 (if has-prereqs
1456 (setq makefile-has-prereqs
1457 (cons target-name makefile-has-prereqs))))))
1459 (defun makefile-remember-macro (macro-name)
1460 "Remember a given macro if it is not already remembered for this buffer."
1461 (if (not (zerop (length macro-name)))
1462 (if (not (assoc macro-name makefile-macro-table))
1463 (setq makefile-macro-table
1464 (cons (list macro-name) makefile-macro-table)))))
1466 (defun makefile-forward-after-target-colon ()
1467 "Move point forward after inserting the terminating colon of a target.
1468 This acts according to the value of `makefile-tab-after-target-colon'."
1469 (if makefile-tab-after-target-colon
1470 (insert "\t")
1471 (insert " ")))
1473 (defun makefile-browser-on-macro-line-p ()
1474 "Determine if point is on a macro line in the browser."
1475 (save-excursion
1476 (beginning-of-line)
1477 (re-search-forward "\\$[{(]" (line-end-position) t)))
1479 (defun makefile-browser-this-line-target-name ()
1480 "Extract the target name from a line in the browser."
1481 (save-excursion
1482 (end-of-line)
1483 (skip-chars-backward "^ \t")
1484 (buffer-substring (point) (1- (line-end-position)))))
1486 (defun makefile-browser-this-line-macro-name ()
1487 "Extract the macro name from a line in the browser."
1488 (save-excursion
1489 (beginning-of-line)
1490 (re-search-forward "\\$[{(]" (line-end-position) t)
1491 (let ((macro-start (point)))
1492 (skip-chars-forward "^})")
1493 (buffer-substring macro-start (point)))))
1495 (defun makefile-format-macro-ref (macro-name)
1496 "Format a macro reference.
1497 Uses `makefile-use-curly-braces-for-macros-p'."
1498 (if (or (char-equal ?\( (string-to-char macro-name))
1499 (char-equal ?\{ (string-to-char macro-name)))
1500 (format "$%s" macro-name)
1501 (if makefile-use-curly-braces-for-macros-p
1502 (format "${%s}" macro-name)
1503 (format "$(%s)" macro-name))))
1505 (defun makefile-browser-get-state-for-line (n)
1506 (aref makefile-browser-selection-vector (1- n)))
1508 (defun makefile-browser-set-state-for-line (n to-state)
1509 (aset makefile-browser-selection-vector (1- n) to-state))
1511 (defun makefile-browser-toggle-state-for-line (n)
1512 (makefile-browser-set-state-for-line n (not (makefile-browser-get-state-for-line n))))
1514 (defun makefile-last-line-p ()
1515 (= (line-end-position) (point-max)))
1517 (defun makefile-first-line-p ()
1518 (= (line-beginning-position) (point-min)))
1522 ;;; Support for other packages, like add-log.
1524 (defun makefile-add-log-defun ()
1525 "Return name of target or variable assignment that point is in.
1526 If it isn't in one, return nil."
1527 (save-excursion
1528 (let (found)
1529 (beginning-of-line)
1530 ;; Scan back line by line, noticing when we come to a
1531 ;; variable or rule definition, and giving up when we see
1532 ;; a line that is not part of either of those.
1533 (while (not (or (setq found
1534 (when (or (looking-at makefile-macroassign-regex)
1535 (looking-at makefile-dependency-regex))
1536 (match-string-no-properties 1)))
1537 ;; Don't keep looking across a blank line or comment.
1538 (looking-at "$\\|#")
1539 (not (zerop (forward-line -1))))))
1540 found)))
1542 (provide 'make-mode)
1544 ;;; make-mode.el ends here