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