(comint-prompt-read-only): Update docstring.
[emacs.git] / lisp / progmodes / make-mode.el
blob5130ca9bfef96f5efeb941baeac9d512d5b33708
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 ;; 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_.]+\\|[@%<?^+*][FD]?\\)[}):]"
273 1 font-lock-constant-face prepend)
275 ;; Automatic variable references and single character variable references,
276 ;; but not shell variables references.
277 '("[^$]\\$\\([@%<?^+*_]\\|[a-zA-Z0-9]\\>\\)"
278 1 font-lock-constant-face prepend)
280 ;; Fontify conditionals and includes.
281 ;; Note that plain `if' is an automake conditional, and not a bug.
282 (list
283 (concat "^\\(?: [ \t]*\\)?"
284 (regexp-opt '("-include" "-sinclude" "include" "sinclude" "ifeq"
285 "if" "ifneq" "ifdef" "ifndef" "endif" "else"
286 "define" "endef" "override"
287 "export" "unexport" "vpath") t)
288 "\\>[ \t]*\\([^: \t\n#]*\\)")
289 '(1 font-lock-keyword-face) '(2 font-lock-variable-name-face))
291 ;; Highlight lines that contain just whitespace.
292 ;; They can cause trouble, especially if they start with a tab.
293 '("^[ \t]+$" . makefile-space-face)
295 ;; Highlight shell comments that Make treats as commands,
296 ;; since these can fool people.
297 '("^\t+#" 0 makefile-space-face t)
299 ;; Highlight spaces that precede tabs.
300 ;; They can make a tab fail to be effective.
301 '("^\\( +\\)\t" 1 makefile-space-face)))
303 (defconst makefile-font-lock-syntactic-keywords
304 ;; From sh-script.el.
305 ;; A `#' begins a comment in sh when it is unquoted and at the beginning
306 ;; of a word. In the shell, words are separated by metacharacters.
307 ;; The list of special chars is taken from the single-unix spec of the
308 ;; shell command language (under `quoting') but with `$' removed.
309 '(("[^|&;<>()`\\\"' \t\n]\\(#+\\)" 1 "_")
310 ;; Change the syntax of a quoted newline so that it does not end a comment.
311 ("\\\\\n" 0 ".")))
313 (defvar makefile-imenu-generic-expression
314 (list
315 (list "Dependencies" makefile-dependency-regex 1)
316 (list "Macro Assignment" makefile-macroassign-regex 1))
317 "Imenu generic expression for Makefile mode. See `imenu-generic-expression'.")
319 ;;; ------------------------------------------------------------
320 ;;; The following configurable variables are used in the
321 ;;; up-to-date overview .
322 ;;; The standard configuration assumes that your `make' program
323 ;;; can be run in question/query mode using the `-q' option, this
324 ;;; means that the command
326 ;;; make -q foo
328 ;;; should return an exit status of zero if the target `foo' is
329 ;;; up to date and a nonzero exit status otherwise.
330 ;;; Many makes can do this although the docs/manpages do not mention
331 ;;; it. Try it with your favourite one. GNU make, System V make, and
332 ;;; Dennis Vadura's DMake have no problems.
333 ;;; Set the variable `makefile-brave-make' to the name of the
334 ;;; make utility that does this on your system.
335 ;;; To understand what this is all about see the function definition
336 ;;; of `makefile-query-by-make-minus-q' .
337 ;;; ------------------------------------------------------------
339 (defcustom makefile-brave-make "make"
340 "*How to invoke make, for `makefile-query-targets'.
341 This should identify a `make' command that can handle the `-q' option."
342 :type 'string
343 :group 'makefile)
345 (defcustom makefile-query-one-target-method 'makefile-query-by-make-minus-q
346 "*Function to call to determine whether a make target is up to date.
347 The function must satisfy this calling convention:
349 * As its first argument, it must accept the name of the target to
350 be checked, as a string.
352 * As its second argument, it may accept the name of a makefile
353 as a string. Depending on what you're going to do you may
354 not need this.
356 * It must return the integer value 0 (zero) if the given target
357 should be considered up-to-date in the context of the given
358 makefile, any nonzero integer value otherwise."
359 :type 'function
360 :group 'makefile)
362 (defcustom makefile-up-to-date-buffer-name "*Makefile Up-to-date overview*"
363 "*Name of the Up-to-date overview buffer."
364 :type 'string
365 :group 'makefile)
367 ;;; --- end of up-to-date-overview configuration ------------------
369 (defvar makefile-mode-abbrev-table nil
370 "Abbrev table in use in Makefile buffers.")
371 (if makefile-mode-abbrev-table
373 (define-abbrev-table 'makefile-mode-abbrev-table ()))
375 (defvar makefile-mode-map nil
376 "The keymap that is used in Makefile mode.")
378 (if makefile-mode-map
380 (setq makefile-mode-map (make-sparse-keymap))
381 ;; set up the keymap
382 (define-key makefile-mode-map "\C-c:" 'makefile-insert-target-ref)
383 (if makefile-electric-keys
384 (progn
385 (define-key makefile-mode-map "$" 'makefile-insert-macro-ref)
386 (define-key makefile-mode-map ":" 'makefile-electric-colon)
387 (define-key makefile-mode-map "=" 'makefile-electric-equal)
388 (define-key makefile-mode-map "." 'makefile-electric-dot)))
389 (define-key makefile-mode-map "\C-c\C-f" 'makefile-pickup-filenames-as-targets)
390 (define-key makefile-mode-map "\C-c\C-b" 'makefile-switch-to-browser)
391 (define-key makefile-mode-map "\C-c\C-c" 'comment-region)
392 (define-key makefile-mode-map "\C-c\C-p" 'makefile-pickup-everything)
393 (define-key makefile-mode-map "\C-c\C-u" 'makefile-create-up-to-date-overview)
394 (define-key makefile-mode-map "\C-c\C-i" 'makefile-insert-gmake-function)
395 (define-key makefile-mode-map "\C-c\C-\\" 'makefile-backslash-region)
396 (define-key makefile-mode-map "\M-p" 'makefile-previous-dependency)
397 (define-key makefile-mode-map "\M-n" 'makefile-next-dependency)
398 (define-key makefile-mode-map "\e\t" 'makefile-complete)
400 ;; Make menus.
401 (define-key makefile-mode-map [menu-bar makefile-mode]
402 (cons "Makefile" (make-sparse-keymap "Makefile")))
404 (define-key makefile-mode-map [menu-bar makefile-mode browse]
405 '("Pop up Makefile Browser" . makefile-switch-to-browser))
406 (define-key makefile-mode-map [menu-bar makefile-mode complete]
407 '("Complete Target or Macro" . makefile-complete))
408 (define-key makefile-mode-map [menu-bar makefile-mode pickup]
409 '("Find Targets and Macros" . makefile-pickup-everything))
411 (define-key makefile-mode-map [menu-bar makefile-mode prev]
412 '("Move to Previous Dependency" . makefile-previous-dependency))
413 (define-key makefile-mode-map [menu-bar makefile-mode next]
414 '("Move to Next Dependency" . makefile-next-dependency)))
416 (defvar makefile-browser-map nil
417 "The keymap that is used in the macro- and target browser.")
418 (if makefile-browser-map
420 (setq makefile-browser-map (make-sparse-keymap))
421 (define-key makefile-browser-map "n" 'makefile-browser-next-line)
422 (define-key makefile-browser-map "\C-n" 'makefile-browser-next-line)
423 (define-key makefile-browser-map "p" 'makefile-browser-previous-line)
424 (define-key makefile-browser-map "\C-p" 'makefile-browser-previous-line)
425 (define-key makefile-browser-map " " 'makefile-browser-toggle)
426 (define-key makefile-browser-map "i" 'makefile-browser-insert-selection)
427 (define-key makefile-browser-map "I" 'makefile-browser-insert-selection-and-quit)
428 (define-key makefile-browser-map "\C-c\C-m" 'makefile-browser-insert-continuation)
429 (define-key makefile-browser-map "q" 'makefile-browser-quit)
430 ;; disable horizontal movement
431 (define-key makefile-browser-map "\C-b" 'undefined)
432 (define-key makefile-browser-map "\C-f" 'undefined))
435 (defvar makefile-mode-syntax-table nil)
436 (if makefile-mode-syntax-table
438 (setq makefile-mode-syntax-table (make-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 ?\} "){ " makefile-mode-syntax-table)
445 (modify-syntax-entry ?\' "\" " makefile-mode-syntax-table)
446 (modify-syntax-entry ?\` "\" " makefile-mode-syntax-table)
447 (modify-syntax-entry ?# "< " makefile-mode-syntax-table)
448 (modify-syntax-entry ?\n "> " makefile-mode-syntax-table))
451 ;;; ------------------------------------------------------------
452 ;;; Internal variables.
453 ;;; You don't need to configure below this line.
454 ;;; ------------------------------------------------------------
456 (defvar makefile-target-table nil
457 "Table of all target names known for this buffer.")
459 (defvar makefile-macro-table nil
460 "Table of all macro names known for this buffer.")
462 (defvar makefile-browser-client
463 "A buffer in Makefile mode that is currently using the browser.")
465 (defvar makefile-browser-selection-vector nil)
466 (defvar makefile-has-prereqs nil)
467 (defvar makefile-need-target-pickup t)
468 (defvar makefile-need-macro-pickup t)
470 (defvar makefile-mode-hook '())
472 ;; Each element looks like '("GNU MAKE FUNCTION" "ARG" "ARG" ... )
473 ;; Each "ARG" is used as a prompt for a required argument.
474 (defconst makefile-gnumake-functions-alist
476 ;; Text functions
477 ("subst" "From" "To" "In")
478 ("patsubst" "Pattern" "Replacement" "In")
479 ("strip" "Text")
480 ("findstring" "Find what" "In")
481 ("filter" "Pattern" "Text")
482 ("filter-out" "Pattern" "Text")
483 ("sort" "List")
484 ;; Filename functions
485 ("dir" "Names")
486 ("notdir" "Names")
487 ("suffix" "Names")
488 ("basename" "Names")
489 ("addprefix" "Prefix" "Names")
490 ("addsuffix" "Suffix" "Names")
491 ("join" "List 1" "List 2")
492 ("word" "Index" "Text")
493 ("words" "Text")
494 ("firstword" "Text")
495 ("wildcard" "Pattern")
496 ;; Misc functions
497 ("foreach" "Variable" "List" "Text")
498 ("origin" "Variable")
499 ("shell" "Command")))
502 ;;; ------------------------------------------------------------
503 ;;; The mode function itself.
504 ;;; ------------------------------------------------------------
506 ;;;###autoload
507 (defun makefile-mode ()
508 "Major mode for editing Makefiles.
509 This function ends by invoking the function(s) `makefile-mode-hook'.
511 \\{makefile-mode-map}
513 In the browser, use the following keys:
515 \\{makefile-browser-map}
517 Makefile mode can be configured by modifying the following variables:
519 `makefile-browser-buffer-name':
520 Name of the macro- and target browser buffer.
522 `makefile-target-colon':
523 The string that gets appended to all target names
524 inserted by `makefile-insert-target'.
525 \":\" or \"::\" are quite common values.
527 `makefile-macro-assign':
528 The string that gets appended to all macro names
529 inserted by `makefile-insert-macro'.
530 The normal value should be \" = \", since this is what
531 standard make expects. However, newer makes such as dmake
532 allow a larger variety of different macro assignments, so you
533 might prefer to use \" += \" or \" := \" .
535 `makefile-tab-after-target-colon':
536 If you want a TAB (instead of a space) to be appended after the
537 target colon, then set this to a non-nil value.
539 `makefile-browser-leftmost-column':
540 Number of blanks to the left of the browser selection mark.
542 `makefile-browser-cursor-column':
543 Column in which the cursor is positioned when it moves
544 up or down in the browser.
546 `makefile-browser-selected-mark':
547 String used to mark selected entries in the browser.
549 `makefile-browser-unselected-mark':
550 String used to mark unselected entries in the browser.
552 `makefile-browser-auto-advance-after-selection-p':
553 If this variable is set to a non-nil value the cursor
554 will automagically advance to the next line after an item
555 has been selected in the browser.
557 `makefile-pickup-everything-picks-up-filenames-p':
558 If this variable is set to a non-nil value then
559 `makefile-pickup-everything' also picks up filenames as targets
560 (i.e. it calls `makefile-pickup-filenames-as-targets'), otherwise
561 filenames are omitted.
563 `makefile-cleanup-continuations':
564 If this variable is set to a non-nil value then Makefile mode
565 will assure that no line in the file ends with a backslash
566 (the continuation character) followed by any whitespace.
567 This is done by silently removing the trailing whitespace, leaving
568 the backslash itself intact.
569 IMPORTANT: Please note that enabling this option causes Makefile mode
570 to MODIFY A FILE WITHOUT YOUR CONFIRMATION when \"it seems necessary\".
572 `makefile-browser-hook':
573 A function or list of functions to be called just before the
574 browser is entered. This is executed in the makefile buffer.
576 `makefile-special-targets-list':
577 List of special targets. You will be offered to complete
578 on one of those in the minibuffer whenever you enter a `.'.
579 at the beginning of a line in Makefile mode."
581 (interactive)
582 (kill-all-local-variables)
583 (add-hook 'write-file-functions
584 'makefile-warn-suspicious-lines nil t)
585 (add-hook 'write-file-functions
586 'makefile-warn-continuations nil t)
587 (add-hook 'write-file-functions
588 'makefile-cleanup-continuations nil t)
589 (make-local-variable 'makefile-target-table)
590 (make-local-variable 'makefile-macro-table)
591 (make-local-variable 'makefile-has-prereqs)
592 (make-local-variable 'makefile-need-target-pickup)
593 (make-local-variable 'makefile-need-macro-pickup)
595 ;; Font lock.
596 (make-local-variable 'font-lock-defaults)
597 (setq font-lock-defaults
598 ;; SYNTAX-BEGIN set to backward-paragraph to avoid slow-down
599 ;; near the end of a large buffer, due to parse-partial-sexp's
600 ;; trying to parse all the way till the beginning of buffer.
601 '(makefile-font-lock-keywords
602 nil nil
603 ((?$ . "."))
604 backward-paragraph
605 (font-lock-syntactic-keywords . makefile-font-lock-syntactic-keywords)))
607 ;; Add-log.
608 (make-local-variable 'add-log-current-defun-function)
609 (setq add-log-current-defun-function 'makefile-add-log-defun)
611 ;; Imenu.
612 (make-local-variable 'imenu-generic-expression)
613 (setq imenu-generic-expression makefile-imenu-generic-expression)
615 ;; Dabbrev.
616 (make-local-variable 'dabbrev-abbrev-skip-leading-regexp)
617 (setq dabbrev-abbrev-skip-leading-regexp "\\$")
619 ;; Other abbrevs.
620 (setq local-abbrev-table makefile-mode-abbrev-table)
622 ;; Filling.
623 (make-local-variable 'fill-paragraph-function)
624 (setq fill-paragraph-function 'makefile-fill-paragraph)
626 ;; Comment stuff.
627 (make-local-variable 'comment-start)
628 (setq comment-start "#")
629 (make-local-variable 'comment-end)
630 (setq comment-end "")
631 (make-local-variable 'comment-start-skip)
632 (setq comment-start-skip "#+[ \t]*")
634 ;; Make sure TAB really inserts \t.
635 (set (make-local-variable 'indent-line-function) 'indent-to-left-margin)
637 ;; become the current major mode
638 (setq major-mode 'makefile-mode)
639 (setq mode-name "Makefile")
641 ;; Activate keymap and syntax table.
642 (use-local-map makefile-mode-map)
643 (set-syntax-table makefile-mode-syntax-table)
645 ;; Real TABs are important in makefiles
646 (setq indent-tabs-mode t)
647 (run-hooks 'makefile-mode-hook))
651 ;;; Motion code.
653 (defun makefile-next-dependency ()
654 "Move point to the beginning of the next dependency line."
655 (interactive)
656 (let ((here (point)))
657 (end-of-line)
658 (if (re-search-forward makefile-dependency-regex (point-max) t)
659 (progn (beginning-of-line) t) ; indicate success
660 (goto-char here) nil)))
662 (defun makefile-previous-dependency ()
663 "Move point to the beginning of the previous dependency line."
664 (interactive)
665 (let ((here (point)))
666 (beginning-of-line)
667 (if (re-search-backward makefile-dependency-regex (point-min) t)
668 (progn (beginning-of-line) t) ; indicate success
669 (goto-char here) nil)))
673 ;;; Electric keys. Blech.
675 (defun makefile-electric-dot (arg)
676 "Prompt for the name of a special target to insert.
677 Only does electric insertion at beginning of line.
678 Anywhere else just self-inserts."
679 (interactive "p")
680 (if (bolp)
681 (makefile-insert-special-target)
682 (self-insert-command arg)))
684 (defun makefile-insert-special-target ()
685 "Prompt for and insert a special target name.
686 Uses `makefile-special-targets' list."
687 (interactive)
688 (makefile-pickup-targets)
689 (let ((special-target
690 (completing-read "Special target: "
691 makefile-special-targets-list nil nil nil)))
692 (if (zerop (length special-target))
694 (insert "." special-target ":")
695 (makefile-forward-after-target-colon))))
697 (defun makefile-electric-equal (arg)
698 "Prompt for name of a macro to insert.
699 Only does prompting if point is at beginning of line.
700 Anywhere else just self-inserts."
701 (interactive "p")
702 (makefile-pickup-macros)
703 (if (bolp)
704 (call-interactively 'makefile-insert-macro)
705 (self-insert-command arg)))
707 (defun makefile-insert-macro (macro-name)
708 "Prepare definition of a new macro."
709 (interactive "sMacro Name: ")
710 (makefile-pickup-macros)
711 (if (not (zerop (length macro-name)))
712 (progn
713 (beginning-of-line)
714 (insert macro-name makefile-macro-assign)
715 (setq makefile-need-macro-pickup t)
716 (makefile-remember-macro macro-name))))
718 (defun makefile-insert-macro-ref (macro-name)
719 "Complete on a list of known macros, then insert complete ref at point."
720 (interactive
721 (list
722 (progn
723 (makefile-pickup-macros)
724 (completing-read "Refer to macro: " makefile-macro-table nil nil nil))))
725 (makefile-do-macro-insertion macro-name))
727 (defun makefile-insert-target (target-name)
728 "Prepare definition of a new target (dependency line)."
729 (interactive "sTarget: ")
730 (if (not (zerop (length target-name)))
731 (progn
732 (beginning-of-line)
733 (insert target-name makefile-target-colon)
734 (makefile-forward-after-target-colon)
735 (end-of-line)
736 (setq makefile-need-target-pickup t)
737 (makefile-remember-target target-name))))
739 (defun makefile-insert-target-ref (target-name)
740 "Complete on a list of known targets, then insert TARGET-NAME at point."
741 (interactive
742 (list
743 (progn
744 (makefile-pickup-targets)
745 (completing-read "Refer to target: " makefile-target-table nil nil nil))))
746 (if (not (zerop (length target-name)))
747 (insert target-name " ")))
749 (defun makefile-electric-colon (arg)
750 "Prompt for name of new target.
751 Prompting only happens at beginning of line.
752 Anywhere else just self-inserts."
753 (interactive "p")
754 (if (bolp)
755 (call-interactively 'makefile-insert-target)
756 (self-insert-command arg)))
760 ;;; ------------------------------------------------------------
761 ;;; Extracting targets and macros from an existing makefile
762 ;;; ------------------------------------------------------------
764 (defun makefile-pickup-targets ()
765 "Notice names of all target definitions in Makefile."
766 (interactive)
767 (if (not makefile-need-target-pickup)
769 (setq makefile-need-target-pickup nil)
770 (setq makefile-target-table nil)
771 (setq makefile-has-prereqs nil)
772 (save-excursion
773 (goto-char (point-min))
774 (while (re-search-forward makefile-dependency-regex nil t)
775 (makefile-add-this-line-targets)))
776 (message "Read targets OK.")))
778 (defun makefile-add-this-line-targets ()
779 (save-excursion
780 (beginning-of-line)
781 (let ((done-with-line nil)
782 (line-number (1+ (count-lines (point-min) (point)))))
783 (while (not done-with-line)
784 (skip-chars-forward " \t")
785 (if (not (setq done-with-line (or (eolp)
786 (char-equal (char-after (point)) ?:))))
787 (progn
788 (let* ((start-of-target-name (point))
789 (target-name
790 (progn
791 (skip-chars-forward "^ \t:#")
792 (buffer-substring start-of-target-name (point))))
793 (has-prereqs
794 (not (looking-at ":[ \t]*$"))))
795 (if (makefile-remember-target target-name has-prereqs)
796 (message "Picked up target \"%s\" from line %d"
797 target-name line-number)))))))))
799 (defun makefile-pickup-macros ()
800 "Notice names of all macro definitions in Makefile."
801 (interactive)
802 (if (not makefile-need-macro-pickup)
804 (setq makefile-need-macro-pickup nil)
805 (setq makefile-macro-table nil)
806 (save-excursion
807 (goto-char (point-min))
808 (while (re-search-forward makefile-macroassign-regex nil t)
809 (makefile-add-this-line-macro)
810 (forward-line 1)))
811 (message "Read macros OK.")))
813 (defun makefile-add-this-line-macro ()
814 (save-excursion
815 (beginning-of-line)
816 (skip-chars-forward " \t")
817 (unless (eolp)
818 (let* ((start-of-macro-name (point))
819 (line-number (1+ (count-lines (point-min) (point))))
820 (macro-name (progn
821 (skip-chars-forward "^ \t:#=*")
822 (buffer-substring start-of-macro-name (point)))))
823 (if (makefile-remember-macro macro-name)
824 (message "Picked up macro \"%s\" from line %d"
825 macro-name line-number))))))
827 (defun makefile-pickup-everything (arg)
828 "Notice names of all macros and targets in Makefile.
829 Prefix arg means force pickups to be redone."
830 (interactive "P")
831 (if arg
832 (progn
833 (setq makefile-need-target-pickup t)
834 (setq makefile-need-macro-pickup t)))
835 (makefile-pickup-macros)
836 (makefile-pickup-targets)
837 (if makefile-pickup-everything-picks-up-filenames-p
838 (makefile-pickup-filenames-as-targets)))
840 (defun makefile-pickup-filenames-as-targets ()
841 "Scan the current directory for filenames to use as targets.
842 Checks each filename against `makefile-ignored-files-in-pickup-regex'
843 and adds all qualifying names to the list of known targets."
844 (interactive)
845 (let* ((dir (file-name-directory (buffer-file-name)))
846 (raw-filename-list (if dir
847 (file-name-all-completions "" dir)
848 (file-name-all-completions "" ""))))
849 (mapcar (lambda (name)
850 (if (and (not (file-directory-p name))
851 (not (string-match makefile-ignored-files-in-pickup-regex
852 name)))
853 (if (makefile-remember-target name)
854 (message "Picked up file \"%s\" as target" name))))
855 raw-filename-list)))
859 ;;; Completion.
861 (defun makefile-complete ()
862 "Perform completion on Makefile construct preceding point.
863 Can complete variable and target names.
864 The context determines which are considered."
865 (interactive)
866 (let* ((beg (save-excursion
867 (skip-chars-backward "^$(){}:#= \t\n")
868 (point)))
869 (try (buffer-substring beg (point)))
870 (do-macros nil)
871 (paren nil))
873 (save-excursion
874 (goto-char beg)
875 (let ((pc (preceding-char)))
876 (cond
877 ;; Beginning of line means anything.
878 ((bolp)
881 ;; Preceding "$" means macros only.
882 ((= pc ?$)
883 (setq do-macros t))
885 ;; Preceding "$(" or "${" means macros only.
886 ((and (or (= pc ?{)
887 (= pc ?\())
888 (progn
889 (setq paren pc)
890 (backward-char)
891 (and (not (bolp))
892 (= (preceding-char) ?$))))
893 (setq do-macros t)))))
895 ;; Try completion.
896 (let* ((table (append (if do-macros
898 makefile-target-table)
899 makefile-macro-table))
900 (completion (try-completion try table)))
901 (cond
902 ;; Exact match, so insert closing paren or colon.
903 ((eq completion t)
904 (insert (if do-macros
905 (if (eq paren ?{)
907 ?\))
908 (if (save-excursion
909 (goto-char beg)
910 (bolp))
912 " "))))
914 ;; No match.
915 ((null completion)
916 (message "Can't find completion for \"%s\"" try)
917 (ding))
919 ;; Partial completion.
920 ((not (string= try completion))
921 ;; FIXME it would be nice to supply the closing paren if an
922 ;; exact, unambiguous match were found. That is not possible
923 ;; right now. Ditto closing ":" for targets.
924 (delete-region beg (point))
926 ;; DO-MACROS means doing macros only. If not that, then check
927 ;; to see if this completion is a macro. Special insertion
928 ;; must be done for macros.
929 (if (or do-macros
930 (assoc completion makefile-macro-table))
931 (let ((makefile-use-curly-braces-for-macros-p
932 (or (eq paren ?{)
933 makefile-use-curly-braces-for-macros-p)))
934 (delete-backward-char 2)
935 (makefile-do-macro-insertion completion)
936 (delete-backward-char 1))
938 ;; Just insert targets.
939 (insert completion)))
941 ;; Can't complete any more, so make completion list. FIXME
942 ;; this doesn't do the right thing when the completion is
943 ;; actually inserted. I don't think there is an easy way to do
944 ;; that.
946 (message "Making completion list...")
947 (let ((list (all-completions try table)))
948 (with-output-to-temp-buffer "*Completions*"
949 (display-completion-list list)))
950 (message "Making completion list...done"))))))
954 ;; Backslashification. Stolen from cc-mode.el.
956 (defun makefile-backslash-region (from to delete-flag)
957 "Insert, align, or delete end-of-line backslashes on the lines in the region.
958 With no argument, inserts backslashes and aligns existing backslashes.
959 With an argument, deletes the backslashes.
961 This function does not modify the last line of the region if the region ends
962 right at the start of the following line; it does not modify blank lines
963 at the start of the region. So you can put the region around an entire macro
964 definition and conveniently use this command."
965 (interactive "r\nP")
966 (save-excursion
967 (goto-char from)
968 (let ((column makefile-backslash-column)
969 (endmark (make-marker)))
970 (move-marker endmark to)
971 ;; Compute the smallest column number past the ends of all the lines.
972 (if makefile-backslash-align
973 (progn
974 (if (not delete-flag)
975 (while (< (point) to)
976 (end-of-line)
977 (if (= (preceding-char) ?\\)
978 (progn (forward-char -1)
979 (skip-chars-backward " \t")))
980 (setq column (max column (1+ (current-column))))
981 (forward-line 1)))
982 ;; Adjust upward to a tab column, if that doesn't push
983 ;; past the margin.
984 (if (> (% column tab-width) 0)
985 (let ((adjusted (* (/ (+ column tab-width -1) tab-width)
986 tab-width)))
987 (if (< adjusted (window-width))
988 (setq column adjusted))))))
989 ;; Don't modify blank lines at start of region.
990 (goto-char from)
991 (while (and (< (point) endmark) (eolp))
992 (forward-line 1))
993 ;; Add or remove backslashes on all the lines.
994 (while (and (< (point) endmark)
995 ;; Don't backslashify the last line
996 ;; if the region ends right at the start of the next line.
997 (save-excursion
998 (forward-line 1)
999 (< (point) endmark)))
1000 (if (not delete-flag)
1001 (makefile-append-backslash column)
1002 (makefile-delete-backslash))
1003 (forward-line 1))
1004 (move-marker endmark nil))))
1006 (defun makefile-append-backslash (column)
1007 (end-of-line)
1008 ;; Note that "\\\\" is needed to get one backslash.
1009 (if (= (preceding-char) ?\\)
1010 (progn (forward-char -1)
1011 (delete-horizontal-space)
1012 (indent-to column (if makefile-backslash-align nil 1)))
1013 (indent-to column (if makefile-backslash-align nil 1))
1014 (insert "\\")))
1016 (defun makefile-delete-backslash ()
1017 (end-of-line)
1018 (or (bolp)
1019 (progn
1020 (forward-char -1)
1021 (if (looking-at "\\\\")
1022 (delete-region (1+ (point))
1023 (progn (skip-chars-backward " \t") (point)))))))
1027 ;; Filling
1029 (defun makefile-fill-paragraph (arg)
1030 ;; Fill comments, backslashed lines, and variable definitions
1031 ;; specially.
1032 (save-excursion
1033 (beginning-of-line)
1034 (cond
1035 ((looking-at "^#+ ")
1036 ;; Found a comment. Set the fill prefix, and find the paragraph
1037 ;; boundaries by searching for lines that look like comment-only
1038 ;; lines.
1039 (let ((fill-prefix (match-string-no-properties 0))
1040 (fill-paragraph-function nil))
1041 (save-excursion
1042 (save-restriction
1043 (narrow-to-region
1044 ;; Search backwards.
1045 (save-excursion
1046 (while (and (zerop (forward-line -1))
1047 (looking-at "^#")))
1048 ;; We may have gone too far. Go forward again.
1049 (or (looking-at "^#")
1050 (forward-line 1))
1051 (point))
1052 ;; Search forwards.
1053 (save-excursion
1054 (while (looking-at "^#")
1055 (forward-line))
1056 (point)))
1057 (fill-paragraph nil)
1058 t))))
1060 ;; Must look for backslashed-region before looking for variable
1061 ;; assignment.
1062 ((or (eq (char-before (line-end-position 1)) ?\\)
1063 (eq (char-before (line-end-position 0)) ?\\))
1064 ;; A backslash region. Find beginning and end, remove
1065 ;; backslashes, fill, and then reapply backslahes.
1066 (end-of-line)
1067 (let ((beginning
1068 (save-excursion
1069 (end-of-line 0)
1070 (while (= (preceding-char) ?\\)
1071 (end-of-line 0))
1072 (forward-char)
1073 (point)))
1074 (end
1075 (save-excursion
1076 (while (= (preceding-char) ?\\)
1077 (end-of-line 2))
1078 (point))))
1079 (save-restriction
1080 (narrow-to-region beginning end)
1081 (makefile-backslash-region (point-min) (point-max) t)
1082 (let ((fill-paragraph-function nil))
1083 (fill-paragraph nil))
1084 (makefile-backslash-region (point-min) (point-max) nil)
1085 (goto-char (point-max))
1086 (if (< (skip-chars-backward "\n") 0)
1087 (delete-region (point) (point-max))))))
1089 ((looking-at makefile-macroassign-regex)
1090 ;; Have a macro assign. Fill just this line, and then backslash
1091 ;; resulting region.
1092 (save-restriction
1093 (narrow-to-region (point) (line-beginning-position 2))
1094 (let ((fill-paragraph-function nil))
1095 (fill-paragraph nil))
1096 (makefile-backslash-region (point-min) (point-max) nil)))))
1098 ;; Always return non-nil so we don't fill anything else.
1103 ;;; ------------------------------------------------------------
1104 ;;; Browser mode.
1105 ;;; ------------------------------------------------------------
1107 (defun makefile-browser-format-target-line (target selected)
1108 (format
1109 (concat (make-string makefile-browser-leftmost-column ?\ )
1110 (if selected
1111 makefile-browser-selected-mark
1112 makefile-browser-unselected-mark)
1113 "%s%s")
1114 target makefile-target-colon))
1116 (defun makefile-browser-format-macro-line (macro selected)
1117 (format
1118 (concat (make-string makefile-browser-leftmost-column ?\ )
1119 (if selected
1120 makefile-browser-selected-mark
1121 makefile-browser-unselected-mark)
1122 (makefile-format-macro-ref macro))))
1124 (defun makefile-browser-fill (targets macros)
1125 (let ((inhibit-read-only t))
1126 (goto-char (point-min))
1127 (erase-buffer)
1128 (mapconcat
1129 (function
1130 (lambda (item) (insert (makefile-browser-format-target-line (car item) nil) "\n")))
1131 targets
1133 (mapconcat
1134 (function
1135 (lambda (item) (insert (makefile-browser-format-macro-line (car item) nil) "\n")))
1136 macros
1138 (sort-lines nil (point-min) (point-max))
1139 (goto-char (1- (point-max)))
1140 (delete-char 1) ; remove unnecessary newline at eob
1141 (goto-char (point-min))
1142 (forward-char makefile-browser-cursor-column)))
1145 ;;; Moving up and down in the browser
1148 (defun makefile-browser-next-line ()
1149 "Move the browser selection cursor to the next line."
1150 (interactive)
1151 (if (not (makefile-last-line-p))
1152 (progn
1153 (forward-line 1)
1154 (forward-char makefile-browser-cursor-column))))
1156 (defun makefile-browser-previous-line ()
1157 "Move the browser selection cursor to the previous line."
1158 (interactive)
1159 (if (not (makefile-first-line-p))
1160 (progn
1161 (forward-line -1)
1162 (forward-char makefile-browser-cursor-column))))
1165 ;;; Quitting the browser (returns to client buffer)
1168 (defun makefile-browser-quit ()
1169 "Leave the browser and return to the makefile buffer."
1170 (interactive)
1171 (let ((my-client makefile-browser-client))
1172 (setq makefile-browser-client nil) ; we quitted, so NO client!
1173 (set-buffer-modified-p nil)
1174 (quit-window t)
1175 (pop-to-buffer my-client)))
1178 ;;; Toggle state of a browser item
1181 (defun makefile-browser-toggle ()
1182 "Toggle the selection state of the browser item at the cursor position."
1183 (interactive)
1184 (let ((this-line (count-lines (point-min) (point))))
1185 (setq this-line (max 1 this-line))
1186 (makefile-browser-toggle-state-for-line this-line)
1187 (goto-line this-line)
1188 (let ((inhibit-read-only t))
1189 (beginning-of-line)
1190 (if (makefile-browser-on-macro-line-p)
1191 (let ((macro-name (makefile-browser-this-line-macro-name)))
1192 (delete-region (point) (progn (end-of-line) (point)))
1193 (insert
1194 (makefile-browser-format-macro-line
1195 macro-name
1196 (makefile-browser-get-state-for-line this-line))))
1197 (let ((target-name (makefile-browser-this-line-target-name)))
1198 (delete-region (point) (progn (end-of-line) (point)))
1199 (insert
1200 (makefile-browser-format-target-line
1201 target-name
1202 (makefile-browser-get-state-for-line this-line))))))
1203 (beginning-of-line)
1204 (forward-char makefile-browser-cursor-column)
1205 (if makefile-browser-auto-advance-after-selection-p
1206 (makefile-browser-next-line))))
1209 ;;; Making insertions into the client buffer
1212 (defun makefile-browser-insert-continuation ()
1213 "Insert a makefile continuation.
1214 In the makefile buffer, go to (end-of-line), insert a \'\\\'
1215 character, insert a new blank line, go to that line and indent by one TAB.
1216 This is most useful in the process of creating continued lines when copying
1217 large dependencies from the browser to the client buffer.
1218 \(point) advances accordingly in the client buffer."
1219 (interactive)
1220 (with-current-buffer makefile-browser-client
1221 (end-of-line)
1222 (insert "\\\n\t")))
1224 (defun makefile-browser-insert-selection ()
1225 "Insert all selected targets and/or macros in the makefile buffer.
1226 Insertion takes place at point."
1227 (interactive)
1228 (save-excursion
1229 (goto-line 1)
1230 (let ((current-line 1))
1231 (while (not (eobp))
1232 (if (makefile-browser-get-state-for-line current-line)
1233 (makefile-browser-send-this-line-item))
1234 (forward-line 1)
1235 (setq current-line (1+ current-line))))))
1237 (defun makefile-browser-insert-selection-and-quit ()
1238 (interactive)
1239 (makefile-browser-insert-selection)
1240 (makefile-browser-quit))
1242 (defun makefile-browser-send-this-line-item ()
1243 (if (makefile-browser-on-macro-line-p)
1244 (save-excursion
1245 (let ((macro-name (makefile-browser-this-line-macro-name)))
1246 (set-buffer makefile-browser-client)
1247 (insert (makefile-format-macro-ref macro-name) " ")))
1248 (save-excursion
1249 (let ((target-name (makefile-browser-this-line-target-name)))
1250 (set-buffer makefile-browser-client)
1251 (insert target-name " ")))))
1253 (defun makefile-browser-start-interaction ()
1254 (use-local-map makefile-browser-map)
1255 (setq buffer-read-only t))
1257 (defun makefile-browse (targets macros)
1258 (interactive)
1259 (if (zerop (+ (length targets) (length macros)))
1260 (progn
1261 (beep)
1262 (message "No macros or targets to browse! Consider running 'makefile-pickup-everything\'"))
1263 (let ((browser-buffer (get-buffer-create makefile-browser-buffer-name)))
1264 (pop-to-buffer browser-buffer)
1265 (makefile-browser-fill targets macros)
1266 (shrink-window-if-larger-than-buffer)
1267 (set (make-local-variable 'makefile-browser-selection-vector)
1268 (make-vector (+ (length targets) (length macros)) nil))
1269 (makefile-browser-start-interaction))))
1271 (defun makefile-switch-to-browser ()
1272 (interactive)
1273 (run-hooks 'makefile-browser-hook)
1274 (setq makefile-browser-client (current-buffer))
1275 (makefile-pickup-targets)
1276 (makefile-pickup-macros)
1277 (makefile-browse makefile-target-table makefile-macro-table))
1281 ;;; ------------------------------------------------------------
1282 ;;; Up-to-date overview buffer
1283 ;;; ------------------------------------------------------------
1285 (defun makefile-create-up-to-date-overview ()
1286 "Create a buffer containing an overview of the state of all known targets.
1287 Known targets are targets that are explicitly defined in that makefile;
1288 in other words, all targets that appear on the left hand side of a
1289 dependency in the makefile."
1290 (interactive)
1291 (if (y-or-n-p "Are you sure that the makefile being edited is consistent? ")
1293 ;; The rest of this function operates on a temporary makefile, created by
1294 ;; writing the current contents of the makefile buffer.
1296 (let ((saved-target-table makefile-target-table)
1297 (this-buffer (current-buffer))
1298 (makefile-up-to-date-buffer
1299 (get-buffer-create makefile-up-to-date-buffer-name))
1300 (filename (makefile-save-temporary))
1302 ;; Forget the target table because it may contain picked-up filenames
1303 ;; that are not really targets in the current makefile.
1304 ;; We don't want to query these, so get a new target-table with just the
1305 ;; targets that can be found in the makefile buffer.
1306 ;; The 'old' target table will be restored later.
1308 (real-targets (progn
1309 (makefile-pickup-targets)
1310 makefile-target-table))
1311 (prereqs makefile-has-prereqs)
1314 (set-buffer makefile-up-to-date-buffer)
1315 (setq buffer-read-only nil)
1316 (erase-buffer)
1317 (makefile-query-targets filename real-targets prereqs)
1318 (if (zerop (buffer-size)) ; if it did not get us anything
1319 (progn
1320 (kill-buffer (current-buffer))
1321 (message "No overview created!")))
1322 (set-buffer this-buffer)
1323 (setq makefile-target-table saved-target-table)
1324 (if (get-buffer makefile-up-to-date-buffer-name)
1325 (progn
1326 (pop-to-buffer (get-buffer makefile-up-to-date-buffer-name))
1327 (shrink-window-if-larger-than-buffer)
1328 (sort-lines nil (point-min) (point-max))
1329 (setq buffer-read-only t))))))
1331 (defun makefile-save-temporary ()
1332 "Create a temporary file from the current makefile buffer."
1333 (let ((filename (makefile-generate-temporary-filename)))
1334 (write-region (point-min) (point-max) filename nil 0)
1335 filename)) ; return the filename
1337 (defun makefile-generate-temporary-filename ()
1338 "Create a filename suitable for use in `makefile-save-temporary'.
1339 Be careful to allow brain-dead file systems (DOS, SYSV ...) to cope
1340 with the generated name!"
1341 (let ((my-name (user-login-name))
1342 (my-uid (int-to-string (user-uid))))
1343 (concat "mktmp"
1344 (if (> (length my-name) 3)
1345 (substring my-name 0 3)
1346 my-name)
1348 (if (> (length my-uid) 3)
1349 (substring my-uid 0 3)
1350 my-uid))))
1352 (defun makefile-query-targets (filename target-table prereq-list)
1353 "Fill the up-to-date overview buffer.
1354 Checks each target in TARGET-TABLE using `makefile-query-one-target-method'
1355 and generates the overview, one line per target name."
1356 (insert
1357 (mapconcat
1358 (function (lambda (item)
1359 (let* ((target-name (car item))
1360 (no-prereqs (not (member target-name prereq-list)))
1361 (needs-rebuild (or no-prereqs
1362 (funcall
1363 makefile-query-one-target-method
1364 target-name
1365 filename))))
1366 (format "\t%s%s"
1367 target-name
1368 (cond (no-prereqs " .. has no prerequisites")
1369 (needs-rebuild " .. NEEDS REBUILD")
1370 (t " .. is up to date"))))
1372 target-table "\n"))
1373 (goto-char (point-min))
1374 (delete-file filename)) ; remove the tmpfile
1376 (defun makefile-query-by-make-minus-q (target &optional filename)
1377 (not (eq 0
1378 (call-process makefile-brave-make nil nil nil
1379 "-f" filename "-q" target))))
1383 ;;; ------------------------------------------------------------
1384 ;;; Continuation cleanup
1385 ;;; ------------------------------------------------------------
1387 (defun makefile-cleanup-continuations ()
1388 (if (eq major-mode 'makefile-mode)
1389 (if (and makefile-cleanup-continuations
1390 (not buffer-read-only))
1391 (save-excursion
1392 (goto-char (point-min))
1393 (while (re-search-forward "\\\\[ \t]+$" nil t)
1394 (replace-match "\\" t t))))))
1397 ;;; ------------------------------------------------------------
1398 ;;; Warn of suspicious lines
1399 ;;; ------------------------------------------------------------
1401 (defun makefile-warn-suspicious-lines ()
1402 ;; Returning non-nil cancels the save operation
1403 (if (eq major-mode 'makefile-mode)
1404 (save-excursion
1405 (goto-char (point-min))
1406 (if (re-search-forward "^\\(\t+$\\| +\t\\)" nil t)
1407 (not (y-or-n-p
1408 (format "Suspicious line %d. Save anyway? "
1409 (count-lines (point-min) (point)))))))))
1411 (defun makefile-warn-continuations ()
1412 (if (eq major-mode 'makefile-mode)
1413 (save-excursion
1414 (goto-char (point-min))
1415 (if (re-search-forward "\\\\[ \t]+$" nil t)
1416 (not (y-or-n-p
1417 (format "Suspicious continuation in line %d. Save anyway? "
1418 (count-lines (point-min) (point)))))))))
1421 ;;; ------------------------------------------------------------
1422 ;;; GNU make function support
1423 ;;; ------------------------------------------------------------
1425 (defun makefile-insert-gmake-function ()
1426 "Insert a GNU make function call.
1427 Asks for the name of the function to use (with completion).
1428 Then prompts for all required parameters."
1429 (interactive)
1430 (let* ((gm-function-name (completing-read
1431 "Function: "
1432 makefile-gnumake-functions-alist
1433 nil t nil))
1434 (gm-function-prompts
1435 (cdr (assoc gm-function-name makefile-gnumake-functions-alist))))
1436 (if (not (zerop (length gm-function-name)))
1437 (insert (makefile-format-macro-ref
1438 (concat gm-function-name " "
1439 (makefile-prompt-for-gmake-funargs
1440 gm-function-name gm-function-prompts)))
1441 " "))))
1443 (defun makefile-prompt-for-gmake-funargs (function-name prompt-list)
1444 (mapconcat
1445 (function (lambda (one-prompt)
1446 (read-string (format "[%s] %s: " function-name one-prompt)
1447 nil)))
1448 prompt-list
1449 ","))
1453 ;;; ------------------------------------------------------------
1454 ;;; Utility functions
1455 ;;; ------------------------------------------------------------
1457 (defun makefile-do-macro-insertion (macro-name)
1458 "Insert a macro reference."
1459 (if (not (zerop (length macro-name)))
1460 (if (assoc macro-name makefile-runtime-macros-list)
1461 (insert "$" macro-name)
1462 (insert (makefile-format-macro-ref macro-name)))))
1464 (defun makefile-remember-target (target-name &optional has-prereqs)
1465 "Remember a given target if it is not already remembered for this buffer."
1466 (if (not (zerop (length target-name)))
1467 (progn
1468 (if (not (assoc target-name makefile-target-table))
1469 (setq makefile-target-table
1470 (cons (list target-name) makefile-target-table)))
1471 (if has-prereqs
1472 (setq makefile-has-prereqs
1473 (cons target-name makefile-has-prereqs))))))
1475 (defun makefile-remember-macro (macro-name)
1476 "Remember a given macro if it is not already remembered for this buffer."
1477 (if (not (zerop (length macro-name)))
1478 (if (not (assoc macro-name makefile-macro-table))
1479 (setq makefile-macro-table
1480 (cons (list macro-name) makefile-macro-table)))))
1482 (defun makefile-forward-after-target-colon ()
1483 "Move point forward after inserting the terminating colon of a target.
1484 This acts according to the value of `makefile-tab-after-target-colon'."
1485 (if makefile-tab-after-target-colon
1486 (insert "\t")
1487 (insert " ")))
1489 (defun makefile-browser-on-macro-line-p ()
1490 "Determine if point is on a macro line in the browser."
1491 (save-excursion
1492 (beginning-of-line)
1493 (re-search-forward "\\$[{(]" (line-end-position) t)))
1495 (defun makefile-browser-this-line-target-name ()
1496 "Extract the target name from a line in the browser."
1497 (save-excursion
1498 (end-of-line)
1499 (skip-chars-backward "^ \t")
1500 (buffer-substring (point) (1- (line-end-position)))))
1502 (defun makefile-browser-this-line-macro-name ()
1503 "Extract the macro name from a line in the browser."
1504 (save-excursion
1505 (beginning-of-line)
1506 (re-search-forward "\\$[{(]" (line-end-position) t)
1507 (let ((macro-start (point)))
1508 (skip-chars-forward "^})")
1509 (buffer-substring macro-start (point)))))
1511 (defun makefile-format-macro-ref (macro-name)
1512 "Format a macro reference.
1513 Uses `makefile-use-curly-braces-for-macros-p'."
1514 (if (or (char-equal ?\( (string-to-char macro-name))
1515 (char-equal ?\{ (string-to-char macro-name)))
1516 (format "$%s" macro-name)
1517 (if makefile-use-curly-braces-for-macros-p
1518 (format "${%s}" macro-name)
1519 (format "$(%s)" macro-name))))
1521 (defun makefile-browser-get-state-for-line (n)
1522 (aref makefile-browser-selection-vector (1- n)))
1524 (defun makefile-browser-set-state-for-line (n to-state)
1525 (aset makefile-browser-selection-vector (1- n) to-state))
1527 (defun makefile-browser-toggle-state-for-line (n)
1528 (makefile-browser-set-state-for-line n (not (makefile-browser-get-state-for-line n))))
1530 (defun makefile-last-line-p ()
1531 (= (line-end-position) (point-max)))
1533 (defun makefile-first-line-p ()
1534 (= (line-beginning-position) (point-min)))
1538 ;;; Support for other packages, like add-log.
1540 (defun makefile-add-log-defun ()
1541 "Return name of target or variable assignment that point is in.
1542 If it isn't in one, return nil."
1543 (save-excursion
1544 (let (found)
1545 (beginning-of-line)
1546 ;; Scan back line by line, noticing when we come to a
1547 ;; variable or rule definition, and giving up when we see
1548 ;; a line that is not part of either of those.
1549 (while (not (or (setq found
1550 (when (or (looking-at makefile-macroassign-regex)
1551 (looking-at makefile-dependency-regex))
1552 (match-string-no-properties 1)))
1553 ;; Don't keep looking across a blank line or comment.
1554 (looking-at "$\\|#")
1555 (not (zerop (forward-line -1))))))
1556 found)))
1558 (provide 'make-mode)
1560 ;;; arch-tag: bd23545a-de91-44fb-b1b2-feafbb2635a0
1561 ;;; make-mode.el ends here