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