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