(dired-quit): Function deleted.
[bpt/emacs.git] / lisp / progmodes / make-mode.el
CommitLineData
80e01c19 1;;; make-mode.el --- makefile editing commands for Emacs
72c0ae01 2
b8ca7cc3 3;; Copyright (C) 1992, 1994 Free Software Foundation, Inc.
58142744 4
72c0ae01 5;; Author: Thomas Neumann <tom@smart.bo.open.de>
9e9bf716 6;; Eric S. Raymond <esr@snark.thyrsus.com>
4228277d 7;; Maintainer: FSF
72c0ae01 8;; Adapted-By: ESR
fd7fa35a 9;; Keywords: unix, tools
72c0ae01 10
74416836 11;; RMS:
6783b1ce 12;; This needs work.
74416836
RS
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 `...'.
74416836 15
72c0ae01
ER
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
7c938215 20;; the Free Software Foundation; either version 2, or (at your option)
72c0ae01
ER
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
b578f267
EN
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.
72c0ae01 32
e41b2db1
ER
33;;; Commentary:
34
9e9bf716
ER
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;;
6783b1ce 45;; The command C-c C-f adds certain filenames in the current directory
9e9bf716
ER
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;;
6783b1ce 54;; The command C-c C-b pops up a browser window listing all target and
9e9bf716
ER
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.
e41b2db1 62
6783b1ce
RS
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).
308a2860 75;; * Make browser into a major mode.
6783b1ce
RS
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
72c0ae01
ER
89;;; Code:
90
9b3d0a18 91(provide 'make-mode)
72c0ae01 92
6783b1ce
RS
93;; Sadly we need this for a macro.
94(eval-when-compile
95 (require 'imenu))
96
72c0ae01 97;;; ------------------------------------------------------------
eb8c3be9 98;;; Configurable stuff
72c0ae01
ER
99;;; ------------------------------------------------------------
100
c098c7f1
RS
101(defgroup makefile nil
102 "Makefile editing commands for Emacs."
103 :group 'tools
104 :prefix "makefile-")
72c0ae01 105
dc3fc797
RS
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
c098c7f1
RS
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 ":"
308a2860 119 "String to append to all target names inserted by `makefile-insert-target'.
c098c7f1
RS
120\":\" or \"::\" are common values."
121 :type 'string
122 :group 'makefile)
72c0ae01 123
c098c7f1 124(defcustom makefile-macro-assign " = "
308a2860 125 "String to append to all macro names inserted by `makefile-insert-macro'.
72c0ae01
ER
126The normal value should be \" = \", since this is what
127standard make expects. However, newer makes such as dmake
128allow a larger variety of different macro assignments, so you
c098c7f1
RS
129might prefer to use \" += \" or \" := \" ."
130 :type 'string
131 :group 'makefile)
72c0ae01 132
c098c7f1
RS
133(defcustom makefile-electric-keys nil
134 "If non-nil, Makefile mode should install electric keybindings.
135Default is nil."
136 :type 'boolean
137 :group 'makefile)
6783b1ce 138
c098c7f1 139(defcustom makefile-use-curly-braces-for-macros-p nil
9e9bf716 140 "Controls the style of generated macro references.
c098c7f1
RS
141Non-nil means macro references should use curly braces, like `${this}'.
142nil means use parentheses, like `$(this)'."
143 :type 'boolean
144 :group 'makefile)
72c0ae01 145
c098c7f1 146(defcustom makefile-tab-after-target-colon t
308a2860
RS
147 "If non-nil, insert a TAB after a target colon.
148Otherwise, a space is inserted.
c098c7f1
RS
149The 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
308a2860 189 "If non-nil, `makefile-pickup-everything' picks up filenames as targets.
b20a44dc 190This means it calls `makefile-pickup-filenames-as-targets'.
c098c7f1
RS
191Otherwise filenames are omitted."
192 :type 'boolean
193 :group 'makefile)
72c0ae01 194
c098c7f1 195(defcustom makefile-cleanup-continuations-p t
308a2860
RS
196 "If non-nil, automatically clean up continuation lines when saving.
197A line is cleaned up by removing all whitespace following a trailing
198backslash. This is done silently.
199IMPORTANT: Please note that enabling this option causes makefile-mode
c098c7f1
RS
200to MODIFY A FILE WITHOUT YOUR CONFIRMATION when \"it seems necessary\"."
201 :type 'boolean
202 :group 'makefile)
72c0ae01 203
308a2860 204(defvar makefile-browser-hook '())
72c0ae01
ER
205
206;;
207;; Special targets for DMake, Sun's make ...
208;;
c098c7f1 209(defcustom makefile-special-targets-list
72c0ae01
ER
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"))
308a2860
RS
218 "List of special targets.
219You will be offered to complete on one of those in the minibuffer whenever
c098c7f1
RS
220you enter a \".\" at the beginning of a line in makefile-mode."
221 :type '(repeat (list string))
222 :group 'makefile)
72c0ae01 223
c098c7f1 224(defcustom makefile-runtime-macros-list
1f35e359 225 '(("@") ("&") (">") ("<") ("*") ("^") ("+") ("?") ("%") ("$"))
72c0ae01
ER
226 "List of macros that are resolved by make at runtime.
227If you insert a macro reference using makefile-insert-macro-ref, the name
228of the macro is checked against this list. If it can be found its name will
c098c7f1
RS
229not be enclosed in { } or ( )."
230 :type '(repeat (list string))
231 :group 'makefile)
72c0ae01 232
6783b1ce
RS
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.
72c0ae01 236(defconst makefile-dependency-regex
6e36fe0b 237 "^ *\\([^ \n\t#:=]+\\([ \t]+[^ \t\n#:=]+\\)*\\)[ \t]*:\\([ \t]*$\\|\\([^=\n].*$\\)\\)"
72c0ae01
ER
238 "Regex used to find dependency lines in a makefile.")
239
6783b1ce
RS
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.
72c0ae01 243(defconst makefile-macroassign-regex
6e36fe0b 244 "^ *\\([^ \n\t][^:#= \t\n]*\\)[ \t]*[*:+]?:?="
72c0ae01
ER
245 "Regex used to find macro assignment lines in a makefile.")
246
247(defconst makefile-ignored-files-in-pickup-regex
0c5b5a13 248 "\\(^\\..*\\)\\|\\(.*~$\\)\\|\\(.*,v$\\)\\|\\(\\.[chy]\\)"
72c0ae01
ER
249 "Regex for filenames that will NOT be included in the target list.")
250
5f55e159
KH
251(if (fboundp 'facemenu-unlisted-faces)
252 (add-to-list 'facemenu-unlisted-faces 'makefile-space-face))
118a9857
RS
253(defvar makefile-space-face 'makefile-space-face
254 "Face to use for highlighting leading spaces in Font-Lock mode.")
308a2860 255
6783b1ce
RS
256(defconst makefile-font-lock-keywords
257 (list
af4b1991 258 ;; Do macro assignments. These get the "variable-name" face rather
6783b1ce 259 ;; arbitrarily.
af4b1991
SM
260 (list makefile-macroassign-regex 1 'font-lock-variable-name-face)
261 ;;
984322b6
RS
262 ;; Do dependencies. These get the function name face.
263 (list makefile-dependency-regex 1 'font-lock-function-name-face)
264 ;;
af4b1991 265 ;; Variable references even in targets/strings/comments:
883212ce 266 '("\\$[({]\\([-a-zA-Z0-9_.]+\\)[}):]" 1 font-lock-constant-face prepend)
308a2860 267
bd0e4eb1
RS
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
f067044f
RS
272 ;; Highlight shell comments that Make treats as commands,
273 ;; since these can fool people.
34bb441e 274 '("^\t+#" 0 makefile-space-face t)
f067044f
RS
275
276 ;; Highlight spaces that precede tabs.
277 ;; They can make a tab fail to be effective.
278 '("^\\( +\\)\t" 1 makefile-space-face)))
6783b1ce 279
72c0ae01
ER
280;;; ------------------------------------------------------------
281;;; The following configurable variables are used in the
282;;; up-to-date overview .
eb8c3be9 283;;; The standard configuration assumes that your `make' program
72c0ae01
ER
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
9e9bf716
ER
292;;; it. Try it with your favourite one. GNU make, System V make, and
293;;; Dennis Vadura's DMake have no problems.
72c0ae01
ER
294;;; Set the variable `makefile-brave-make' to the name of the
295;;; make utility that does this on your system.
eb8c3be9 296;;; To understand what this is all about see the function definition
72c0ae01
ER
297;;; of `makefile-query-by-make-minus-q' .
298;;; ------------------------------------------------------------
299
c098c7f1
RS
300(defcustom makefile-brave-make "make"
301 "How to invoke make, for `makefile-query-targets'.
302This should identify a `make' command that can handle the `-q' option."
303 :type 'string
304 :group 'makefile)
72c0ae01 305
c098c7f1 306(defcustom makefile-query-one-target-method 'makefile-query-by-make-minus-q
bd0e4eb1
RS
307 "Function to call to determine whether a make target is up to date.
308The function must satisfy this calling convention:
72c0ae01
ER
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
9e9bf716 314 as a string. Depending on what you're going to do you may
72c0ae01
ER
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
c098c7f1
RS
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)
72c0ae01 327
72c0ae01
ER
328;;; --- end of up-to-date-overview configuration ------------------
329
72c0ae01 330(defvar makefile-mode-map nil
bd0e4eb1
RS
331 "The keymap that is used in Makefile mode.")
332
72c0ae01
ER
333(if makefile-mode-map
334 ()
335 (setq makefile-mode-map (make-sparse-keymap))
336 ;; set up the keymap
6783b1ce
RS
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)))
72c0ae01 344 (define-key makefile-mode-map "\C-c\C-f" 'makefile-pickup-filenames-as-targets)
72c0ae01 345 (define-key makefile-mode-map "\C-c\C-b" 'makefile-switch-to-browser)
dfa9e6fc 346 (define-key makefile-mode-map "\C-c\C-c" 'comment-region)
72c0ae01
ER
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)
308a2860 350 (define-key makefile-mode-map "\C-c\C-\\" 'makefile-backslash-region)
72c0ae01 351 (define-key makefile-mode-map "\M-p" 'makefile-previous-dependency)
6783b1ce
RS
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]
0e520d74 360 '("Pop up Makefile Browser" . makefile-switch-to-browser))
6783b1ce 361 (define-key makefile-mode-map [menu-bar makefile-mode complete]
0e520d74 362 '("Complete Target or Macro" . makefile-complete))
6783b1ce 363 (define-key makefile-mode-map [menu-bar makefile-mode pickup]
0e520d74 364 '("Find Targets and Macros" . makefile-pickup-everything))
6783b1ce
RS
365
366 (define-key makefile-mode-map [menu-bar makefile-mode prev]
0e520d74 367 '("Move to Previous Dependency" . makefile-previous-dependency))
6783b1ce 368 (define-key makefile-mode-map [menu-bar makefile-mode next]
0e520d74 369 '("Move to Next Dependency" . makefile-next-dependency)))
72c0ae01
ER
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
308a2860 390(defvar makefile-mode-syntax-table nil)
72c0ae01
ER
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)
6a99a3ea 397 (modify-syntax-entry ?\] ")[ " makefile-mode-syntax-table)
72c0ae01
ER
398 (modify-syntax-entry ?\{ "(} " makefile-mode-syntax-table)
399 (modify-syntax-entry ?\} "){ " makefile-mode-syntax-table)
af4b1991
SM
400 (modify-syntax-entry ?\' "\" " makefile-mode-syntax-table)
401 (modify-syntax-entry ?\` "\" " makefile-mode-syntax-table)
72c0ae01
ER
402 (modify-syntax-entry ?# "< " makefile-mode-syntax-table)
403 (modify-syntax-entry ?\n "> " makefile-mode-syntax-table))
6783b1ce
RS
404
405
72c0ae01
ER
406;;; ------------------------------------------------------------
407;;; Internal variables.
408;;; You don't need to configure below this line.
409;;; ------------------------------------------------------------
410
411(defvar makefile-target-table nil
308a2860 412 "Table of all target names known for this buffer.")
72c0ae01
ER
413
414(defvar makefile-macro-table nil
308a2860 415 "Table of all macro names known for this buffer.")
72c0ae01
ER
416
417(defvar makefile-browser-client
bd0e4eb1 418 "A buffer in Makefile mode that is currently using the browser.")
72c0ae01
ER
419
420(defvar makefile-browser-selection-vector nil)
9e9bf716
ER
421(defvar makefile-has-prereqs nil)
422(defvar makefile-need-target-pickup t)
423(defvar makefile-need-macro-pickup t)
72c0ae01
ER
424
425(defvar makefile-mode-hook '())
426
308a2860
RS
427;; Each element looks like '("GNU MAKE FUNCTION" "ARG" "ARG" ... )
428;; Each "ARG" is used as a prompt for a required argument.
72c0ae01 429(defconst makefile-gnumake-functions-alist
72c0ae01
ER
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")
e1775344 444 ("addprefix" "Prefix" "Names")
72c0ae01
ER
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")
308a2860 454 ("shell" "Command")))
72c0ae01
ER
455
456
457;;; ------------------------------------------------------------
458;;; The mode function itself.
459;;; ------------------------------------------------------------
460
5c2946c7 461;;;###autoload
72c0ae01
ER
462(defun makefile-mode ()
463 "Major mode for editing Makefiles.
bd0e4eb1 464This function ends by invoking the function(s) `makefile-mode-hook'.
72c0ae01
ER
465
466\\{makefile-mode-map}
467
468In the browser, use the following keys:
469
470\\{makefile-browser-map}
471
bd0e4eb1 472Makefile mode can be configured by modifying the following variables:
72c0ae01 473
72c0ae01
ER
474makefile-browser-buffer-name:
475 Name of the macro- and target browser buffer.
476
477makefile-target-colon:
478 The string that gets appended to all target names
bd0e4eb1 479 inserted by `makefile-insert-target'.
72c0ae01
ER
480 \":\" or \"::\" are quite common values.
481
482makefile-macro-assign:
483 The string that gets appended to all macro names
bd0e4eb1 484 inserted by `makefile-insert-macro'.
72c0ae01
ER
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
490makefile-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
494makefile-browser-leftmost-column:
495 Number of blanks to the left of the browser selection mark.
496
497makefile-browser-cursor-column:
498 Column in which the cursor is positioned when it moves
499 up or down in the browser.
500
501makefile-browser-selected-mark:
502 String used to mark selected entries in the browser.
503
504makefile-browser-unselected-mark:
505 String used to mark unselected entries in the browser.
506
507makefile-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
72c0ae01
ER
512makefile-pickup-everything-picks-up-filenames-p:
513 If this variable is set to a non-nil value then
bd0e4eb1 514 `makefile-pickup-everything' also picks up filenames as targets
b20a44dc 515 (i.e. it calls `makefile-pickup-filenames-as-targets'), otherwise
72c0ae01
ER
516 filenames are omitted.
517
518makefile-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
bd0e4eb1 525 to MODIFY A FILE WITHOUT YOUR CONFIRMATION when \"it seems necessary\".
72c0ae01
ER
526
527makefile-browser-hook:
528 A function or list of functions to be called just before the
9e9bf716 529 browser is entered. This is executed in the makefile buffer.
72c0ae01
ER
530
531makefile-special-targets-list:
532 List of special targets. You will be offered to complete
bd0e4eb1
RS
533 on one of those in the minibuffer whenever you enter a `.'.
534 at the beginning of a line in Makefile mode."
6783b1ce 535
72c0ae01
ER
536 (interactive)
537 (kill-all-local-variables)
9854c615 538 (make-local-variable 'local-write-file-hooks)
b8ca7cc3 539 (setq local-write-file-hooks
cd99f76d 540 '(makefile-cleanup-continuations makefile-warn-suspicious-lines))
b8ca7cc3
RS
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)
6783b1ce
RS
546
547 ;; Font lock.
af4b1991
SM
548 (make-local-variable 'font-lock-defaults)
549 (setq font-lock-defaults '(makefile-font-lock-keywords))
6783b1ce
RS
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
3b114205
RS
559 ;; Dabbrev.
560 (make-local-variable 'dabbrev-abbrev-skip-leading-regexp)
561 (setq dabbrev-abbrev-skip-leading-regexp "\\$")
562
dfa9e6fc
RS
563 ;; Filling.
564 (make-local-variable 'fill-paragraph-function)
565 (setq fill-paragraph-function 'makefile-fill-paragraph)
566
6783b1ce 567 ;; Comment stuff.
531b2a28 568 (make-local-variable 'comment-start)
72c0ae01 569 (setq comment-start "#")
6783b1ce 570 (make-local-variable 'comment-end)
72c0ae01 571 (setq comment-end "")
6783b1ce
RS
572 (make-local-variable 'comment-start-skip)
573 (setq comment-start-skip "#+[ \t]*")
574
72c0ae01
ER
575 ;; become the current major mode
576 (setq major-mode 'makefile-mode)
6783b1ce
RS
577 (setq mode-name "Makefile")
578
579 ;; Activate keymap and syntax table.
72c0ae01
ER
580 (use-local-map makefile-mode-map)
581 (set-syntax-table makefile-mode-syntax-table)
6783b1ce
RS
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.
72c0ae01 590
72c0ae01 591(defun makefile-next-dependency ()
bd0e4eb1 592 "Move point to the beginning of the next dependency line."
72c0ae01
ER
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)))
6783b1ce 599
72c0ae01 600(defun makefile-previous-dependency ()
bd0e4eb1 601 "Move point to the beginning of the previous dependency line."
72c0ae01
ER
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
6783b1ce 609\f
72c0ae01 610
6783b1ce 611;;; Electric keys. Blech.
9e9bf716 612
6783b1ce
RS
613(defun makefile-electric-dot (arg)
614 "Prompt for the name of a special target to insert.
615Only does electric insertion at beginning of line.
616Anywhere else just self-inserts."
617 (interactive "p")
72c0ae01
ER
618 (if (bolp)
619 (makefile-insert-special-target)
6783b1ce 620 (self-insert-command arg)))
72c0ae01 621
72c0ae01 622(defun makefile-insert-special-target ()
ac282af8 623 "Prompt for and insert a special target name.
6783b1ce 624Uses `makefile-special-targets' list."
72c0ae01 625 (interactive)
9e9bf716 626 (makefile-pickup-targets)
6783b1ce
RS
627 (let ((special-target
628 (completing-read "Special target: "
629 makefile-special-targets-list nil nil nil)))
72c0ae01
ER
630 (if (zerop (length special-target))
631 ()
6783b1ce 632 (insert "." special-target ":")
72c0ae01
ER
633 (makefile-forward-after-target-colon))))
634
6783b1ce
RS
635(defun makefile-electric-equal (arg)
636 "Prompt for name of a macro to insert.
637Only does prompting if point is at beginning of line.
638Anywhere else just self-inserts."
639 (interactive "p")
9e9bf716 640 (makefile-pickup-macros)
72c0ae01
ER
641 (if (bolp)
642 (call-interactively 'makefile-insert-macro)
6783b1ce 643 (self-insert-command arg)))
72c0ae01
ER
644
645(defun makefile-insert-macro (macro-name)
646 "Prepare definition of a new macro."
647 (interactive "sMacro Name: ")
9e9bf716 648 (makefile-pickup-macros)
72c0ae01
ER
649 (if (not (zerop (length macro-name)))
650 (progn
651 (beginning-of-line)
6783b1ce 652 (insert macro-name makefile-macro-assign)
9e9bf716 653 (setq makefile-need-macro-pickup t)
72c0ae01
ER
654 (makefile-remember-macro macro-name))))
655
72c0ae01 656(defun makefile-insert-macro-ref (macro-name)
bd0e4eb1 657 "Complete on a list of known macros, then insert complete ref at point."
72c0ae01
ER
658 (interactive
659 (list
9e9bf716
ER
660 (progn
661 (makefile-pickup-macros)
662 (completing-read "Refer to macro: " makefile-macro-table nil nil nil))))
6783b1ce 663 (makefile-do-macro-insertion macro-name))
72c0ae01 664
72c0ae01
ER
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)
6783b1ce 671 (insert target-name makefile-target-colon)
72c0ae01
ER
672 (makefile-forward-after-target-colon)
673 (end-of-line)
9e9bf716 674 (setq makefile-need-target-pickup t)
72c0ae01
ER
675 (makefile-remember-target target-name))))
676
72c0ae01 677(defun makefile-insert-target-ref (target-name)
bd0e4eb1 678 "Complete on a list of known targets, then insert target-ref at point."
72c0ae01
ER
679 (interactive
680 (list
0c5b5a13 681 (progn
9e9bf716
ER
682 (makefile-pickup-targets)
683 (completing-read "Refer to target: " makefile-target-table nil nil nil))))
72c0ae01 684 (if (not (zerop (length target-name)))
6783b1ce 685 (insert target-name " ")))
72c0ae01 686
6783b1ce
RS
687(defun makefile-electric-colon (arg)
688 "Prompt for name of new target.
689Prompting only happens at beginning of line.
690Anywhere else just self-inserts."
691 (interactive "p")
72c0ae01
ER
692 (if (bolp)
693 (call-interactively 'makefile-insert-target)
6783b1ce
RS
694 (self-insert-command arg)))
695
696\f
72c0ae01 697
72c0ae01
ER
698;;; ------------------------------------------------------------
699;;; Extracting targets and macros from an existing makefile
700;;; ------------------------------------------------------------
701
702(defun makefile-pickup-targets ()
308a2860 703 "Notice names of all target definitions in Makefile."
72c0ae01 704 (interactive)
9e9bf716
ER
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.")))
72c0ae01
ER
715
716(defun makefile-add-this-line-targets ()
717 (save-excursion
718 (beginning-of-line)
9e9bf716
ER
719 (let ((done-with-line nil)
720 (line-number (1+ (count-lines (point-min) (point)))))
72c0ae01
ER
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:#")
9e9bf716
ER
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)))))))))
72c0ae01 736
72c0ae01 737(defun makefile-pickup-macros ()
308a2860 738 "Notice names of all macro definitions in Makefile."
72c0ae01 739 (interactive)
9e9bf716
ER
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.")))
72c0ae01
ER
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))
9e9bf716 757 (line-number (1+ (count-lines (point-min) (point))))
72c0ae01
ER
758 (macro-name (progn
759 (skip-chars-forward "^ \t:#=*")
760 (buffer-substring start-of-macro-name (point)))))
761 (if (makefile-remember-macro macro-name)
9e9bf716
ER
762 (message "Picked up macro \"%s\" from line %d"
763 macro-name line-number))))))
72c0ae01 764
6783b1ce 765(defun makefile-pickup-everything (arg)
308a2860 766 "Notice names of all macros and targets in Makefile.
6783b1ce
RS
767Prefix 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)))
72c0ae01
ER
773 (makefile-pickup-macros)
774 (makefile-pickup-targets)
775 (if makefile-pickup-everything-picks-up-filenames-p
776 (makefile-pickup-filenames-as-targets)))
777
72c0ae01 778(defun makefile-pickup-filenames-as-targets ()
308a2860
RS
779 "Scan the current directory for filenames to use as targets.
780Checks each filename against `makefile-ignored-files-in-pickup-regex'
781and adds all qualifying names to the list of known targets."
72c0ae01
ER
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
6783b1ce
RS
795\f
796
797;;; Completion.
798
799(defun makefile-complete ()
800 "Perform completion on Makefile construct preceding point.
801Can complete variable and target names.
802The 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
308a2860
RS
892;; Backslashification. Stolen from cc-mode.el.
893
dfa9e6fc
RS
894(defun makefile-backslash-region (from to delete-flag)
895 "Insert, align, or delete end-of-line backslashes on the lines in the region.
896With no argument, inserts backslashes and aligns existing backslashes.
897With an argument, deletes the backslashes.
898
899This function does not modify the last line of the region if the region ends
900right at the start of the following line; it does not modify blank lines
901at the start of the region. So you can put the region around an entire macro
902definition and conveniently use this command."
308a2860
RS
903 (interactive "r\nP")
904 (save-excursion
dfa9e6fc
RS
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.
308a2860 1018 (save-restriction
dfa9e6fc
RS
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)
308a2860
RS
1029
1030\f
1031
72c0ae01 1032;;; ------------------------------------------------------------
6783b1ce 1033;;; Browser mode.
72c0ae01
ER
1034;;; ------------------------------------------------------------
1035
72c0ae01
ER
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)
c72344c7
RS
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)))
72c0ae01
ER
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 ()
308a2860 1098 "Leave the browser and return to the makefile buffer."
72c0ae01
ER
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)
c72344c7
RS
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)))
8d3a7934 1121 (delete-region (point) (progn (end-of-line) (point)))
c72344c7
RS
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)))
8d3a7934 1127 (delete-region (point) (progn (end-of-line) (point)))
72c0ae01 1128 (insert
c72344c7
RS
1129 (makefile-browser-format-target-line
1130 target-name
1131 (makefile-browser-get-state-for-line this-line))))))
72c0ae01
ER
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 ()
9e9bf716 1142 "Insert a makefile continuation.
308a2860 1143In the makefile buffer, go to (end-of-line), insert a \'\\\'
72c0ae01 1144character, insert a new blank line, go to that line and indent by one TAB.
9e9bf716
ER
1145This is most useful in the process of creating continued lines when copying
1146large dependencies from the browser to the client buffer.
a4e104bf 1147\(point) advances accordingly in the client buffer."
72c0ae01
ER
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 ()
308a2860 1155 "Insert all selected targets and/or macros in the makefile buffer.
bd0e4eb1 1156Insertion takes place at point."
72c0ae01
ER
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
72c0ae01
ER
1183(defun makefile-browser-start-interaction ()
1184 (use-local-map makefile-browser-map)
1185 (setq buffer-read-only t))
1186
72c0ae01
ER
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)
9e9bf716 1197 (shrink-window-if-larger-than-buffer)
72c0ae01
ER
1198 (setq makefile-browser-selection-vector
1199 (make-vector (+ (length targets) (length macros)) nil))
1200 (makefile-browser-start-interaction))))
72c0ae01
ER
1201
1202(defun makefile-switch-to-browser ()
1203 (interactive)
1204 (run-hooks 'makefile-browser-hook)
1205 (setq makefile-browser-client (current-buffer))
9e9bf716
ER
1206 (makefile-pickup-targets)
1207 (makefile-pickup-macros)
72c0ae01
ER
1208 (makefile-browse makefile-target-table makefile-macro-table))
1209
6783b1ce 1210\f
72c0ae01
ER
1211
1212;;; ------------------------------------------------------------
1213;;; Up-to-date overview buffer
1214;;; ------------------------------------------------------------
1215
1216(defun makefile-create-up-to-date-overview ()
9e9bf716 1217 "Create a buffer containing an overview of the state of all known targets.
72c0ae01
ER
1218Known targets are targets that are explicitly defined in that makefile;
1219in other words, all targets that appear on the left hand side of a
1220dependency 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
72c0ae01 1240 (makefile-pickup-targets)
9e9bf716
ER
1241 makefile-target-table))
1242 (prereqs makefile-has-prereqs)
1243 )
72c0ae01
ER
1244
1245 (set-buffer makefile-up-to-date-buffer)
1246 (setq buffer-read-only nil)
1247 (erase-buffer)
9e9bf716 1248 (makefile-query-targets filename real-targets prereqs)
72c0ae01
ER
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))
9e9bf716 1258 (shrink-window-if-larger-than-buffer)
72c0ae01
ER
1259 (sort-lines nil (point-min) (point-max))
1260 (setq buffer-read-only t))))))
72c0ae01
ER
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 ()
308a2860 1269 "Create a filename suitable for use in `makefile-save-temporary'.
72c0ae01 1270Be careful to allow brain-dead file systems (DOS, SYSV ...) to cope
308a2860 1271with the generated name!"
72c0ae01
ER
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
9e9bf716 1283(defun makefile-query-targets (filename target-table prereq-list)
bd0e4eb1 1284 "Fill the up-to-date overview buffer.
308a2860 1285Checks each target in TARGET-TABLE using `makefile-query-one-target-method'
72c0ae01
ER
1286and generates the overview, one line per target name."
1287 (insert
9e9bf716
ER
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"))
72c0ae01
ER
1304 (goto-char (point-min))
1305 (delete-file filename)) ; remove the tmpfile
1306
1307(defun makefile-query-by-make-minus-q (target &optional filename)
308a2860
RS
1308 (not (zerop
1309 (call-process makefile-brave-make nil nil nil
1310 "-f" filename "-q" target))))
72c0ae01 1311
6783b1ce
RS
1312\f
1313
72c0ae01
ER
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
b8ca7cc3
RS
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
6783b1ce 1348\f
b8ca7cc3 1349
72c0ae01
ER
1350;;; ------------------------------------------------------------
1351;;; GNU make function support
1352;;; ------------------------------------------------------------
1353
1354(defun makefile-insert-gmake-function ()
308a2860
RS
1355 "Insert a GNU make function call.
1356Asks for the name of the function to use (with completion).
1357Then prompts for all required parameters."
72c0ae01
ER
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)
308a2860
RS
1375 (read-string (format "[%s] %s: " function-name one-prompt)
1376 nil)))
72c0ae01
ER
1377 prompt-list
1378 ","))
308a2860 1379
6783b1ce 1380\f
72c0ae01
ER
1381
1382;;; ------------------------------------------------------------
1383;;; Utility functions
1384;;; ------------------------------------------------------------
1385
6783b1ce
RS
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
9e9bf716 1393(defun makefile-remember-target (target-name &optional has-prereqs)
72c0ae01
ER
1394 "Remember a given target if it is not already remembered for this buffer."
1395 (if (not (zerop (length target-name)))
9e9bf716 1396 (progn
72c0ae01
ER
1397 (if (not (assoc target-name makefile-target-table))
1398 (setq makefile-target-table
9e9bf716
ER
1399 (cons (list target-name) makefile-target-table)))
1400 (if has-prereqs
1401 (setq makefile-has-prereqs
1402 (cons target-name makefile-has-prereqs))))))
72c0ae01
ER
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 ()
308a2860
RS
1412 "Move point forward after inserting the terminating colon of a target.
1413This acts according to the value of `makefile-tab-after-target-colon'."
72c0ae01
ER
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)
308a2860
RS
1441 "Format a macro reference.
1442Uses `makefile-use-curly-braces-for-macros-p'."
b8ca7cc3
RS
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))))
72c0ae01
ER
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
6783b1ce
RS
1475\f
1476
1477;;; Support for other packages, like add-log and imenu.
1478
1479(defun makefile-add-log-defun ()
32b558fa
RS
1480 "Return name of target or variable assignment that point is in.
1481If it isn't in one, return nil."
6783b1ce 1482 (save-excursion
32b558fa
RS
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))))
6783b1ce
RS
1504
1505;; FIXME it might be nice to have them separated by macro vs target.
1506(defun makefile-menu-index-function ()
308a2860 1507 ;; "Generate alist of indices for imenu."
6783b1ce
RS
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)
6783b1ce
RS
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
80e01c19 1526;;; make-mode.el ends here