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