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