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