1 ;;; make-mode.el --- makefile editing commands for Emacs
3 ;; Copyright (C) 1992,94,99,2000,2001, 2002, 2003 Free Software Foundation, Inc.
5 ;; Author: Thomas Neumann <tom@smart.bo.open.de>
6 ;; Eric S. Raymond <esr@snark.thyrsus.com>
9 ;; Keywords: unix, tools
13 ;; Also, the doc strings need fixing: the first line doesn't stand alone,
14 ;; and other usage is not high quality. Symbol names don't have `...'.
16 ;; This file is part of GNU Emacs.
18 ;; GNU Emacs is free software; you can redistribute it and/or modify
19 ;; it under the terms of the GNU General Public License as published by
20 ;; the Free Software Foundation; either version 2, or (at your option)
23 ;; GNU Emacs is distributed in the hope that it will be useful,
24 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
25 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
26 ;; GNU General Public License for more details.
28 ;; You should have received a copy of the GNU General Public License
29 ;; along with GNU Emacs; see the file COPYING. If not, write to the
30 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
31 ;; Boston, MA 02111-1307, USA.
35 ;; A major mode for editing makefiles. The mode knows about Makefile
36 ;; syntax and defines M-n and M-p to move to next and previous productions.
38 ;; The keys $, =, : and . are electric; they try to help you fill in a
39 ;; macro reference, macro definition, ordinary target name, or special
40 ;; target name, respectively. Such names are completed using a list of
41 ;; targets and macro names parsed out of the makefile. This list is
42 ;; automatically updated, if necessary, whenever you invoke one of
43 ;; these commands. You can force it to be updated with C-c C-p.
45 ;; The command C-c C-f adds certain filenames in the current directory
46 ;; as targets. You can filter out filenames by setting the variable
47 ;; makefile-ignored-files-in-pickup-regex.
49 ;; The command C-c C-u grinds for a bit, then pops up a report buffer
50 ;; showing which target names are up-to-date with respect to their
51 ;; prerequisites, which targets are out-of-date, and which have no
54 ;; The command C-c C-b pops up a browser window listing all target and
55 ;; macro names. You can mark or unmark items wit C-c SPC, and insert
56 ;; all marked items back in the Makefile with C-c TAB.
58 ;; The command C-c TAB in the makefile buffer inserts a GNU make builtin.
59 ;; You will be prompted for the builtin's args.
61 ;; There are numerous other customization variables.
66 ;; * Eliminate electric stuff entirely.
67 ;; * It might be nice to highlight targets differently depending on
68 ;; whether they are up-to-date or not. Not sure how this would
69 ;; interact with font-lock.
70 ;; * Would be nice to edit the commands in ksh-mode and have
71 ;; indentation and slashification done automatically. Hard.
72 ;; * Consider removing browser mode. It seems useless.
73 ;; * ":" should notice when a new target is made and add it to the
74 ;; list (or at least set makefile-need-target-pickup).
75 ;; * Make browser into a major mode.
76 ;; * Clean up macro insertion stuff. It is a mess.
77 ;; * Browser entry and exit is weird. Normalize.
78 ;; * Browser needs to be rewritten. Right now it is kind of a crock.
80 ;; * Act more like dired/buffer menu/whatever.
81 ;; * Highlight as mouse traverses.
83 ;; * Update documentation above.
84 ;; * Update texinfo manual.
91 ;; Sadly we need this for a macro.
97 ;;; ------------------------------------------------------------
98 ;;; Configurable stuff
99 ;;; ------------------------------------------------------------
101 (defgroup makefile nil
102 "Makefile editing commands for Emacs."
106 (defface makefile-space-face
107 '((((class color
)) (:background
"hotpink"))
108 (t (:reverse-video t
)))
109 "Face to use for highlighting leading spaces in Font-Lock mode."
113 (defcustom makefile-browser-buffer-name
"*Macros and Targets*"
114 "*Name of the macro- and target browser buffer."
118 (defcustom makefile-target-colon
":"
119 "*String to append to all target names inserted by `makefile-insert-target'.
120 \":\" or \"::\" are common values."
124 (defcustom makefile-macro-assign
" = "
125 "*String to append to all macro names inserted by `makefile-insert-macro'.
126 The normal value should be \" = \", since this is what
127 standard make expects. However, newer makes such as dmake
128 allow a larger variety of different macro assignments, so you
129 might prefer to use \" += \" or \" := \" ."
133 (defcustom makefile-electric-keys nil
134 "*If non-nil, Makefile mode should install electric keybindings.
139 (defcustom makefile-use-curly-braces-for-macros-p nil
140 "*Controls the style of generated macro references.
141 Non-nil means macro references should use curly braces, like `${this}'.
142 nil means use parentheses, like `$(this)'."
146 (defcustom makefile-tab-after-target-colon t
147 "*If non-nil, insert a TAB after a target colon.
148 Otherwise, a space is inserted.
153 (defcustom makefile-browser-leftmost-column
10
154 "*Number of blanks to the left of the browser selection mark."
158 (defcustom makefile-browser-cursor-column
10
159 "*Column the cursor goes to when it moves up or down in the Makefile browser."
163 (defcustom makefile-backslash-column
48
164 "*Column in which `makefile-backslash-region' inserts backslashes."
168 (defcustom makefile-backslash-align t
169 "*If non-nil, `makefile-backslash-region' will align backslashes."
173 (defcustom makefile-browser-selected-mark
"+ "
174 "*String used to mark selected entries in the Makefile browser."
178 (defcustom makefile-browser-unselected-mark
" "
179 "*String used to mark unselected entries in the Makefile browser."
183 (defcustom makefile-browser-auto-advance-after-selection-p t
184 "*If non-nil, cursor will move after item is selected in Makefile browser."
188 (defcustom makefile-pickup-everything-picks-up-filenames-p nil
189 "*If non-nil, `makefile-pickup-everything' picks up filenames as targets.
190 This means it calls `makefile-pickup-filenames-as-targets'.
191 Otherwise filenames are omitted."
195 (defcustom makefile-cleanup-continuations nil
196 "*If non-nil, automatically clean up continuation lines when saving.
197 A line is cleaned up by removing all whitespace following a trailing
198 backslash. This is done silently.
199 IMPORTANT: Please note that enabling this option causes Makefile mode
200 to MODIFY A FILE WITHOUT YOUR CONFIRMATION when \"it seems necessary\"."
204 (defcustom makefile-mode-hook nil
205 "*Normal hook run by `makefile-mode'."
209 (defvar makefile-browser-hook
'())
212 ;; Special targets for DMake, Sun's make ...
214 (defcustom 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 :type
'(repeat (list string
))
229 (defcustom makefile-runtime-macros-list
230 '(("@") ("&") (">") ("<") ("*") ("^") ("+") ("?") ("%") ("$"))
231 "*List of macros that are resolved by make at runtime.
232 If you insert a macro reference using `makefile-insert-macro-ref', the name
233 of the macro is checked against this list. If it can be found its name will
234 not be enclosed in { } or ( )."
235 :type
'(repeat (list string
))
238 ;; Note that the first big subexpression is used by font lock. Note
239 ;; that if you change this regexp you might have to fix the imenu
240 ;; index in makefile-imenu-generic-expression.
241 (defconst makefile-dependency-regex
242 "^ *\\([^ \n\t#:=]+\\([ \t]+\\([^ \t\n#:=]+\\|\\$[({][^ \t\n#})]+[})]\\)\\)*\\)[ \t]*:\\([ \t]*$\\|\\([^=\n].*$\\)\\)"
243 "Regex used to find dependency lines in a makefile.")
245 ;; Note that the first subexpression is used by font lock. Note
246 ;; that if you change this regexp you might have to fix the imenu
247 ;; index in makefile-imenu-generic-expression.
248 (defconst makefile-macroassign-regex
249 "^ *\\([^ \n\t][^:#= \t\n]*\\)[ \t]*[*:+]?[:?]?="
250 "Regex used to find macro assignment lines in a makefile.")
252 (defconst makefile-ignored-files-in-pickup-regex
253 "\\(^\\..*\\)\\|\\(.*~$\\)\\|\\(.*,v$\\)\\|\\(\\.[chy]\\)"
254 "Regex for filenames that will NOT be included in the target list.")
256 (if (fboundp 'facemenu-unlisted-faces
)
257 (add-to-list 'facemenu-unlisted-faces
'makefile-space-face
))
258 (defvar makefile-space-face
'makefile-space-face
259 "Face to use for highlighting leading spaces in Font-Lock mode.")
261 (defconst makefile-font-lock-keywords
264 ;; Do macro assignments. These get the "variable-name" face rather
266 (list makefile-macroassign-regex
1 'font-lock-variable-name-face
)
268 ;; Do dependencies. These get the function name face.
269 (list makefile-dependency-regex
1 'font-lock-function-name-face
)
271 ;; Variable references even in targets/strings/comments.
272 '("[^$]\\$[({]\\([-a-zA-Z0-9_.]+\\|[@%<?^+*][FD]?\\)[}):]"
273 1 font-lock-constant-face prepend
)
275 ;; Automatic variable references and single character variable references,
276 ;; but not shell variables references.
277 '("[^$]\\$\\([@%<?^+*_]\\|[a-zA-Z0-9]\\>\\)"
278 1 font-lock-constant-face prepend
)
280 ;; Fontify conditionals and includes.
281 ;; Note that plain `if' is an automake conditional, and not a bug.
283 (concat "^\\(?: [ \t]*\\)?"
284 (regexp-opt '("-include" "-sinclude" "include" "sinclude" "ifeq"
285 "if" "ifneq" "ifdef" "ifndef" "endif" "else"
286 "define" "endef" "override"
287 "export" "unexport" "vpath") t
)
288 "\\>[ \t]*\\([^: \t\n#]*\\)")
289 '(1 font-lock-keyword-face
) '(2 font-lock-variable-name-face
))
291 ;; Highlight lines that contain just whitespace.
292 ;; They can cause trouble, especially if they start with a tab.
293 '("^[ \t]+$" . makefile-space-face
)
295 ;; Highlight shell comments that Make treats as commands,
296 ;; since these can fool people.
297 '("^\t+#" 0 makefile-space-face t
)
299 ;; Highlight spaces that precede tabs.
300 ;; They can make a tab fail to be effective.
301 '("^\\( +\\)\t" 1 makefile-space-face
)))
303 (defconst makefile-font-lock-syntactic-keywords
304 ;; From sh-script.el.
305 ;; A `#' begins a comment in sh when it is unquoted and at the beginning
306 ;; of a word. In the shell, words are separated by metacharacters.
307 ;; The list of special chars is taken from the single-unix spec of the
308 ;; shell command language (under `quoting') but with `$' removed.
309 '(("[^|&;<>()`\\\"' \t\n]\\(#+\\)" 1 "_")
310 ;; Change the syntax of a quoted newline so that it does not end a comment.
313 (defvar makefile-imenu-generic-expression
315 (list "Dependencies" makefile-dependency-regex
1)
316 (list "Macro Assignment" makefile-macroassign-regex
1))
317 "Imenu generic expression for Makefile mode. See `imenu-generic-expression'.")
319 ;;; ------------------------------------------------------------
320 ;;; The following configurable variables are used in the
321 ;;; up-to-date overview .
322 ;;; The standard configuration assumes that your `make' program
323 ;;; can be run in question/query mode using the `-q' option, this
324 ;;; means that the command
328 ;;; should return an exit status of zero if the target `foo' is
329 ;;; up to date and a nonzero exit status otherwise.
330 ;;; Many makes can do this although the docs/manpages do not mention
331 ;;; it. Try it with your favourite one. GNU make, System V make, and
332 ;;; Dennis Vadura's DMake have no problems.
333 ;;; Set the variable `makefile-brave-make' to the name of the
334 ;;; make utility that does this on your system.
335 ;;; To understand what this is all about see the function definition
336 ;;; of `makefile-query-by-make-minus-q' .
337 ;;; ------------------------------------------------------------
339 (defcustom makefile-brave-make
"make"
340 "*How to invoke make, for `makefile-query-targets'.
341 This should identify a `make' command that can handle the `-q' option."
345 (defcustom makefile-query-one-target-method
'makefile-query-by-make-minus-q
346 "*Function to call to determine whether a make target is up to date.
347 The function must satisfy this calling convention:
349 * As its first argument, it must accept the name of the target to
350 be checked, as a string.
352 * As its second argument, it may accept the name of a makefile
353 as a string. Depending on what you're going to do you may
356 * It must return the integer value 0 (zero) if the given target
357 should be considered up-to-date in the context of the given
358 makefile, any nonzero integer value otherwise."
362 (defcustom makefile-up-to-date-buffer-name
"*Makefile Up-to-date overview*"
363 "*Name of the Up-to-date overview buffer."
367 ;;; --- end of up-to-date-overview configuration ------------------
369 (defvar makefile-mode-abbrev-table nil
370 "Abbrev table in use in Makefile buffers.")
371 (if makefile-mode-abbrev-table
373 (define-abbrev-table 'makefile-mode-abbrev-table
()))
375 (defvar makefile-mode-map nil
376 "The keymap that is used in Makefile mode.")
378 (if makefile-mode-map
380 (setq makefile-mode-map
(make-sparse-keymap))
382 (define-key makefile-mode-map
"\C-c:" 'makefile-insert-target-ref
)
383 (if makefile-electric-keys
385 (define-key makefile-mode-map
"$" 'makefile-insert-macro-ref
)
386 (define-key makefile-mode-map
":" 'makefile-electric-colon
)
387 (define-key makefile-mode-map
"=" 'makefile-electric-equal
)
388 (define-key makefile-mode-map
"." 'makefile-electric-dot
)))
389 (define-key makefile-mode-map
"\C-c\C-f" 'makefile-pickup-filenames-as-targets
)
390 (define-key makefile-mode-map
"\C-c\C-b" 'makefile-switch-to-browser
)
391 (define-key makefile-mode-map
"\C-c\C-c" 'comment-region
)
392 (define-key makefile-mode-map
"\C-c\C-p" 'makefile-pickup-everything
)
393 (define-key makefile-mode-map
"\C-c\C-u" 'makefile-create-up-to-date-overview
)
394 (define-key makefile-mode-map
"\C-c\C-i" 'makefile-insert-gmake-function
)
395 (define-key makefile-mode-map
"\C-c\C-\\" 'makefile-backslash-region
)
396 (define-key makefile-mode-map
"\M-p" 'makefile-previous-dependency
)
397 (define-key makefile-mode-map
"\M-n" 'makefile-next-dependency
)
398 (define-key makefile-mode-map
"\e\t" 'makefile-complete
)
401 (define-key makefile-mode-map
[menu-bar makefile-mode
]
402 (cons "Makefile" (make-sparse-keymap "Makefile")))
404 (define-key makefile-mode-map
[menu-bar makefile-mode browse
]
405 '("Pop up Makefile Browser" . makefile-switch-to-browser
))
406 (define-key makefile-mode-map
[menu-bar makefile-mode complete
]
407 '("Complete Target or Macro" . makefile-complete
))
408 (define-key makefile-mode-map
[menu-bar makefile-mode pickup
]
409 '("Find Targets and Macros" . makefile-pickup-everything
))
411 (define-key makefile-mode-map
[menu-bar makefile-mode prev
]
412 '("Move to Previous Dependency" . makefile-previous-dependency
))
413 (define-key makefile-mode-map
[menu-bar makefile-mode next
]
414 '("Move to Next Dependency" . makefile-next-dependency
)))
416 (defvar makefile-browser-map nil
417 "The keymap that is used in the macro- and target browser.")
418 (if makefile-browser-map
420 (setq makefile-browser-map
(make-sparse-keymap))
421 (define-key makefile-browser-map
"n" 'makefile-browser-next-line
)
422 (define-key makefile-browser-map
"\C-n" 'makefile-browser-next-line
)
423 (define-key makefile-browser-map
"p" 'makefile-browser-previous-line
)
424 (define-key makefile-browser-map
"\C-p" 'makefile-browser-previous-line
)
425 (define-key makefile-browser-map
" " 'makefile-browser-toggle
)
426 (define-key makefile-browser-map
"i" 'makefile-browser-insert-selection
)
427 (define-key makefile-browser-map
"I" 'makefile-browser-insert-selection-and-quit
)
428 (define-key makefile-browser-map
"\C-c\C-m" 'makefile-browser-insert-continuation
)
429 (define-key makefile-browser-map
"q" 'makefile-browser-quit
)
430 ;; disable horizontal movement
431 (define-key makefile-browser-map
"\C-b" 'undefined
)
432 (define-key makefile-browser-map
"\C-f" 'undefined
))
435 (defvar makefile-mode-syntax-table nil
)
436 (if makefile-mode-syntax-table
438 (setq makefile-mode-syntax-table
(make-syntax-table))
439 (modify-syntax-entry ?\
( "() " makefile-mode-syntax-table
)
440 (modify-syntax-entry ?\
) ")( " makefile-mode-syntax-table
)
441 (modify-syntax-entry ?\
[ "(] " makefile-mode-syntax-table
)
442 (modify-syntax-entry ?\
] ")[ " makefile-mode-syntax-table
)
443 (modify-syntax-entry ?\
{ "(} " makefile-mode-syntax-table
)
444 (modify-syntax-entry ?\
} "){ " makefile-mode-syntax-table
)
445 (modify-syntax-entry ?
\' "\" " makefile-mode-syntax-table
)
446 (modify-syntax-entry ?\
` "\" " makefile-mode-syntax-table
)
447 (modify-syntax-entry ?
# "< " makefile-mode-syntax-table
)
448 (modify-syntax-entry ?
\n "> " makefile-mode-syntax-table
))
451 ;;; ------------------------------------------------------------
452 ;;; Internal variables.
453 ;;; You don't need to configure below this line.
454 ;;; ------------------------------------------------------------
456 (defvar makefile-target-table nil
457 "Table of all target names known for this buffer.")
459 (defvar makefile-macro-table nil
460 "Table of all macro names known for this buffer.")
462 (defvar makefile-browser-client
463 "A buffer in Makefile mode that is currently using the browser.")
465 (defvar makefile-browser-selection-vector nil
)
466 (defvar makefile-has-prereqs nil
)
467 (defvar makefile-need-target-pickup t
)
468 (defvar makefile-need-macro-pickup t
)
470 (defvar makefile-mode-hook
'())
472 ;; Each element looks like '("GNU MAKE FUNCTION" "ARG" "ARG" ... )
473 ;; Each "ARG" is used as a prompt for a required argument.
474 (defconst makefile-gnumake-functions-alist
477 ("subst" "From" "To" "In")
478 ("patsubst" "Pattern" "Replacement" "In")
480 ("findstring" "Find what" "In")
481 ("filter" "Pattern" "Text")
482 ("filter-out" "Pattern" "Text")
484 ;; Filename functions
489 ("addprefix" "Prefix" "Names")
490 ("addsuffix" "Suffix" "Names")
491 ("join" "List 1" "List 2")
492 ("word" "Index" "Text")
495 ("wildcard" "Pattern")
497 ("foreach" "Variable" "List" "Text")
498 ("origin" "Variable")
499 ("shell" "Command")))
502 ;;; ------------------------------------------------------------
503 ;;; The mode function itself.
504 ;;; ------------------------------------------------------------
507 (defun makefile-mode ()
508 "Major mode for editing Makefiles.
509 This function ends by invoking the function(s) `makefile-mode-hook'.
511 \\{makefile-mode-map}
513 In the browser, use the following keys:
515 \\{makefile-browser-map}
517 Makefile mode can be configured by modifying the following variables:
519 `makefile-browser-buffer-name':
520 Name of the macro- and target browser buffer.
522 `makefile-target-colon':
523 The string that gets appended to all target names
524 inserted by `makefile-insert-target'.
525 \":\" or \"::\" are quite common values.
527 `makefile-macro-assign':
528 The string that gets appended to all macro names
529 inserted by `makefile-insert-macro'.
530 The normal value should be \" = \", since this is what
531 standard make expects. However, newer makes such as dmake
532 allow a larger variety of different macro assignments, so you
533 might prefer to use \" += \" or \" := \" .
535 `makefile-tab-after-target-colon':
536 If you want a TAB (instead of a space) to be appended after the
537 target colon, then set this to a non-nil value.
539 `makefile-browser-leftmost-column':
540 Number of blanks to the left of the browser selection mark.
542 `makefile-browser-cursor-column':
543 Column in which the cursor is positioned when it moves
544 up or down in the browser.
546 `makefile-browser-selected-mark':
547 String used to mark selected entries in the browser.
549 `makefile-browser-unselected-mark':
550 String used to mark unselected entries in the browser.
552 `makefile-browser-auto-advance-after-selection-p':
553 If this variable is set to a non-nil value the cursor
554 will automagically advance to the next line after an item
555 has been selected in the browser.
557 `makefile-pickup-everything-picks-up-filenames-p':
558 If this variable is set to a non-nil value then
559 `makefile-pickup-everything' also picks up filenames as targets
560 (i.e. it calls `makefile-pickup-filenames-as-targets'), otherwise
561 filenames are omitted.
563 `makefile-cleanup-continuations':
564 If this variable is set to a non-nil value then Makefile mode
565 will assure that no line in the file ends with a backslash
566 (the continuation character) followed by any whitespace.
567 This is done by silently removing the trailing whitespace, leaving
568 the backslash itself intact.
569 IMPORTANT: Please note that enabling this option causes Makefile mode
570 to MODIFY A FILE WITHOUT YOUR CONFIRMATION when \"it seems necessary\".
572 `makefile-browser-hook':
573 A function or list of functions to be called just before the
574 browser is entered. This is executed in the makefile buffer.
576 `makefile-special-targets-list':
577 List of special targets. You will be offered to complete
578 on one of those in the minibuffer whenever you enter a `.'.
579 at the beginning of a line in Makefile mode."
582 (kill-all-local-variables)
583 (add-hook 'write-file-functions
584 'makefile-warn-suspicious-lines nil t
)
585 (add-hook 'write-file-functions
586 'makefile-warn-continuations nil t
)
587 (add-hook 'write-file-functions
588 'makefile-cleanup-continuations nil t
)
589 (make-local-variable 'makefile-target-table
)
590 (make-local-variable 'makefile-macro-table
)
591 (make-local-variable 'makefile-has-prereqs
)
592 (make-local-variable 'makefile-need-target-pickup
)
593 (make-local-variable 'makefile-need-macro-pickup
)
596 (make-local-variable 'font-lock-defaults
)
597 (setq font-lock-defaults
598 ;; SYNTAX-BEGIN set to backward-paragraph to avoid slow-down
599 ;; near the end of a large buffer, due to parse-partial-sexp's
600 ;; trying to parse all the way till the beginning of buffer.
601 '(makefile-font-lock-keywords
605 (font-lock-syntactic-keywords . makefile-font-lock-syntactic-keywords
)))
608 (make-local-variable 'add-log-current-defun-function
)
609 (setq add-log-current-defun-function
'makefile-add-log-defun
)
612 (make-local-variable 'imenu-generic-expression
)
613 (setq imenu-generic-expression makefile-imenu-generic-expression
)
616 (make-local-variable 'dabbrev-abbrev-skip-leading-regexp
)
617 (setq dabbrev-abbrev-skip-leading-regexp
"\\$")
620 (setq local-abbrev-table makefile-mode-abbrev-table
)
623 (make-local-variable 'fill-paragraph-function
)
624 (setq fill-paragraph-function
'makefile-fill-paragraph
)
627 (make-local-variable 'comment-start
)
628 (setq comment-start
"#")
629 (make-local-variable 'comment-end
)
630 (setq comment-end
"")
631 (make-local-variable 'comment-start-skip
)
632 (setq comment-start-skip
"#+[ \t]*")
634 ;; Make sure TAB really inserts \t.
635 (set (make-local-variable 'indent-line-function
) 'indent-to-left-margin
)
637 ;; become the current major mode
638 (setq major-mode
'makefile-mode
)
639 (setq mode-name
"Makefile")
641 ;; Activate keymap and syntax table.
642 (use-local-map makefile-mode-map
)
643 (set-syntax-table makefile-mode-syntax-table
)
645 ;; Real TABs are important in makefiles
646 (setq indent-tabs-mode t
)
647 (run-hooks 'makefile-mode-hook
))
653 (defun makefile-next-dependency ()
654 "Move point to the beginning of the next dependency line."
656 (let ((here (point)))
658 (if (re-search-forward makefile-dependency-regex
(point-max) t
)
659 (progn (beginning-of-line) t
) ; indicate success
660 (goto-char here
) nil
)))
662 (defun makefile-previous-dependency ()
663 "Move point to the beginning of the previous dependency line."
665 (let ((here (point)))
667 (if (re-search-backward makefile-dependency-regex
(point-min) t
)
668 (progn (beginning-of-line) t
) ; indicate success
669 (goto-char here
) nil
)))
673 ;;; Electric keys. Blech.
675 (defun makefile-electric-dot (arg)
676 "Prompt for the name of a special target to insert.
677 Only does electric insertion at beginning of line.
678 Anywhere else just self-inserts."
681 (makefile-insert-special-target)
682 (self-insert-command arg
)))
684 (defun makefile-insert-special-target ()
685 "Prompt for and insert a special target name.
686 Uses `makefile-special-targets' list."
688 (makefile-pickup-targets)
689 (let ((special-target
690 (completing-read "Special target: "
691 makefile-special-targets-list nil nil nil
)))
692 (if (zerop (length special-target
))
694 (insert "." special-target
":")
695 (makefile-forward-after-target-colon))))
697 (defun makefile-electric-equal (arg)
698 "Prompt for name of a macro to insert.
699 Only does prompting if point is at beginning of line.
700 Anywhere else just self-inserts."
702 (makefile-pickup-macros)
704 (call-interactively 'makefile-insert-macro
)
705 (self-insert-command arg
)))
707 (defun makefile-insert-macro (macro-name)
708 "Prepare definition of a new macro."
709 (interactive "sMacro Name: ")
710 (makefile-pickup-macros)
711 (if (not (zerop (length macro-name
)))
714 (insert macro-name makefile-macro-assign
)
715 (setq makefile-need-macro-pickup t
)
716 (makefile-remember-macro macro-name
))))
718 (defun makefile-insert-macro-ref (macro-name)
719 "Complete on a list of known macros, then insert complete ref at point."
723 (makefile-pickup-macros)
724 (completing-read "Refer to macro: " makefile-macro-table nil nil nil
))))
725 (makefile-do-macro-insertion macro-name
))
727 (defun makefile-insert-target (target-name)
728 "Prepare definition of a new target (dependency line)."
729 (interactive "sTarget: ")
730 (if (not (zerop (length target-name
)))
733 (insert target-name makefile-target-colon
)
734 (makefile-forward-after-target-colon)
736 (setq makefile-need-target-pickup t
)
737 (makefile-remember-target target-name
))))
739 (defun makefile-insert-target-ref (target-name)
740 "Complete on a list of known targets, then insert TARGET-NAME at point."
744 (makefile-pickup-targets)
745 (completing-read "Refer to target: " makefile-target-table nil nil nil
))))
746 (if (not (zerop (length target-name
)))
747 (insert target-name
" ")))
749 (defun makefile-electric-colon (arg)
750 "Prompt for name of new target.
751 Prompting only happens at beginning of line.
752 Anywhere else just self-inserts."
755 (call-interactively 'makefile-insert-target
)
756 (self-insert-command arg
)))
760 ;;; ------------------------------------------------------------
761 ;;; Extracting targets and macros from an existing makefile
762 ;;; ------------------------------------------------------------
764 (defun makefile-pickup-targets ()
765 "Notice names of all target definitions in Makefile."
767 (if (not makefile-need-target-pickup
)
769 (setq makefile-need-target-pickup nil
)
770 (setq makefile-target-table nil
)
771 (setq makefile-has-prereqs nil
)
773 (goto-char (point-min))
774 (while (re-search-forward makefile-dependency-regex nil t
)
775 (makefile-add-this-line-targets)))
776 (message "Read targets OK.")))
778 (defun makefile-add-this-line-targets ()
781 (let ((done-with-line nil
)
782 (line-number (1+ (count-lines (point-min) (point)))))
783 (while (not done-with-line
)
784 (skip-chars-forward " \t")
785 (if (not (setq done-with-line
(or (eolp)
786 (char-equal (char-after (point)) ?
:))))
788 (let* ((start-of-target-name (point))
791 (skip-chars-forward "^ \t:#")
792 (buffer-substring start-of-target-name
(point))))
794 (not (looking-at ":[ \t]*$"))))
795 (if (makefile-remember-target target-name has-prereqs
)
796 (message "Picked up target \"%s\" from line %d"
797 target-name line-number
)))))))))
799 (defun makefile-pickup-macros ()
800 "Notice names of all macro definitions in Makefile."
802 (if (not makefile-need-macro-pickup
)
804 (setq makefile-need-macro-pickup nil
)
805 (setq makefile-macro-table nil
)
807 (goto-char (point-min))
808 (while (re-search-forward makefile-macroassign-regex nil t
)
809 (makefile-add-this-line-macro)
811 (message "Read macros OK.")))
813 (defun makefile-add-this-line-macro ()
816 (skip-chars-forward " \t")
818 (let* ((start-of-macro-name (point))
819 (line-number (1+ (count-lines (point-min) (point))))
821 (skip-chars-forward "^ \t:#=*")
822 (buffer-substring start-of-macro-name
(point)))))
823 (if (makefile-remember-macro macro-name
)
824 (message "Picked up macro \"%s\" from line %d"
825 macro-name line-number
))))))
827 (defun makefile-pickup-everything (arg)
828 "Notice names of all macros and targets in Makefile.
829 Prefix arg means force pickups to be redone."
833 (setq makefile-need-target-pickup t
)
834 (setq makefile-need-macro-pickup t
)))
835 (makefile-pickup-macros)
836 (makefile-pickup-targets)
837 (if makefile-pickup-everything-picks-up-filenames-p
838 (makefile-pickup-filenames-as-targets)))
840 (defun makefile-pickup-filenames-as-targets ()
841 "Scan the current directory for filenames to use as targets.
842 Checks each filename against `makefile-ignored-files-in-pickup-regex'
843 and adds all qualifying names to the list of known targets."
845 (let* ((dir (file-name-directory (buffer-file-name)))
846 (raw-filename-list (if dir
847 (file-name-all-completions "" dir
)
848 (file-name-all-completions "" ""))))
849 (mapcar (lambda (name)
850 (if (and (not (file-directory-p name
))
851 (not (string-match makefile-ignored-files-in-pickup-regex
853 (if (makefile-remember-target name
)
854 (message "Picked up file \"%s\" as target" name
))))
861 (defun makefile-complete ()
862 "Perform completion on Makefile construct preceding point.
863 Can complete variable and target names.
864 The context determines which are considered."
866 (let* ((beg (save-excursion
867 (skip-chars-backward "^$(){}:#= \t\n")
869 (try (buffer-substring beg
(point)))
875 (let ((pc (preceding-char)))
877 ;; Beginning of line means anything.
881 ;; Preceding "$" means macros only.
885 ;; Preceding "$(" or "${" means macros only.
892 (= (preceding-char) ?$
))))
893 (setq do-macros t
)))))
896 (let* ((table (append (if do-macros
898 makefile-target-table
)
899 makefile-macro-table
))
900 (completion (try-completion try table
)))
902 ;; Exact match, so insert closing paren or colon.
904 (insert (if do-macros
916 (message "Can't find completion for \"%s\"" try
)
919 ;; Partial completion.
920 ((not (string= try completion
))
921 ;; FIXME it would be nice to supply the closing paren if an
922 ;; exact, unambiguous match were found. That is not possible
923 ;; right now. Ditto closing ":" for targets.
924 (delete-region beg
(point))
926 ;; DO-MACROS means doing macros only. If not that, then check
927 ;; to see if this completion is a macro. Special insertion
928 ;; must be done for macros.
930 (assoc completion makefile-macro-table
))
931 (let ((makefile-use-curly-braces-for-macros-p
933 makefile-use-curly-braces-for-macros-p
)))
934 (delete-backward-char 2)
935 (makefile-do-macro-insertion completion
)
936 (delete-backward-char 1))
938 ;; Just insert targets.
939 (insert completion
)))
941 ;; Can't complete any more, so make completion list. FIXME
942 ;; this doesn't do the right thing when the completion is
943 ;; actually inserted. I don't think there is an easy way to do
946 (message "Making completion list...")
947 (let ((list (all-completions try table
)))
948 (with-output-to-temp-buffer "*Completions*"
949 (display-completion-list list
)))
950 (message "Making completion list...done"))))))
954 ;; Backslashification. Stolen from cc-mode.el.
956 (defun makefile-backslash-region (from to delete-flag
)
957 "Insert, align, or delete end-of-line backslashes on the lines in the region.
958 With no argument, inserts backslashes and aligns existing backslashes.
959 With an argument, deletes the backslashes.
961 This function does not modify the last line of the region if the region ends
962 right at the start of the following line; it does not modify blank lines
963 at the start of the region. So you can put the region around an entire macro
964 definition and conveniently use this command."
968 (let ((column makefile-backslash-column
)
969 (endmark (make-marker)))
970 (move-marker endmark to
)
971 ;; Compute the smallest column number past the ends of all the lines.
972 (if makefile-backslash-align
974 (if (not delete-flag
)
975 (while (< (point) to
)
977 (if (= (preceding-char) ?
\\)
978 (progn (forward-char -
1)
979 (skip-chars-backward " \t")))
980 (setq column
(max column
(1+ (current-column))))
982 ;; Adjust upward to a tab column, if that doesn't push
984 (if (> (% column tab-width
) 0)
985 (let ((adjusted (* (/ (+ column tab-width -
1) tab-width
)
987 (if (< adjusted
(window-width))
988 (setq column adjusted
))))))
989 ;; Don't modify blank lines at start of region.
991 (while (and (< (point) endmark
) (eolp))
993 ;; Add or remove backslashes on all the lines.
994 (while (and (< (point) endmark
)
995 ;; Don't backslashify the last line
996 ;; if the region ends right at the start of the next line.
999 (< (point) endmark
)))
1000 (if (not delete-flag
)
1001 (makefile-append-backslash column
)
1002 (makefile-delete-backslash))
1004 (move-marker endmark nil
))))
1006 (defun makefile-append-backslash (column)
1008 ;; Note that "\\\\" is needed to get one backslash.
1009 (if (= (preceding-char) ?
\\)
1010 (progn (forward-char -
1)
1011 (delete-horizontal-space)
1012 (indent-to column
(if makefile-backslash-align nil
1)))
1013 (indent-to column
(if makefile-backslash-align nil
1))
1016 (defun makefile-delete-backslash ()
1021 (if (looking-at "\\\\")
1022 (delete-region (1+ (point))
1023 (progn (skip-chars-backward " \t") (point)))))))
1029 (defun makefile-fill-paragraph (arg)
1030 ;; Fill comments, backslashed lines, and variable definitions
1035 ((looking-at "^#+ ")
1036 ;; Found a comment. Set the fill prefix, and find the paragraph
1037 ;; boundaries by searching for lines that look like comment-only
1039 (let ((fill-prefix (match-string-no-properties 0))
1040 (fill-paragraph-function nil
))
1044 ;; Search backwards.
1046 (while (and (zerop (forward-line -
1))
1048 ;; We may have gone too far. Go forward again.
1049 (or (looking-at "^#")
1054 (while (looking-at "^#")
1057 (fill-paragraph nil
)
1060 ;; Must look for backslashed-region before looking for variable
1062 ((or (eq (char-before (line-end-position 1)) ?
\\)
1063 (eq (char-before (line-end-position 0)) ?
\\))
1064 ;; A backslash region. Find beginning and end, remove
1065 ;; backslashes, fill, and then reapply backslahes.
1070 (while (= (preceding-char) ?
\\)
1076 (while (= (preceding-char) ?
\\)
1080 (narrow-to-region beginning end
)
1081 (makefile-backslash-region (point-min) (point-max) t
)
1082 (let ((fill-paragraph-function nil
))
1083 (fill-paragraph nil
))
1084 (makefile-backslash-region (point-min) (point-max) nil
)
1085 (goto-char (point-max))
1086 (if (< (skip-chars-backward "\n") 0)
1087 (delete-region (point) (point-max))))))
1089 ((looking-at makefile-macroassign-regex
)
1090 ;; Have a macro assign. Fill just this line, and then backslash
1091 ;; resulting region.
1093 (narrow-to-region (point) (line-beginning-position 2))
1094 (let ((fill-paragraph-function nil
))
1095 (fill-paragraph nil
))
1096 (makefile-backslash-region (point-min) (point-max) nil
)))))
1098 ;; Always return non-nil so we don't fill anything else.
1103 ;;; ------------------------------------------------------------
1105 ;;; ------------------------------------------------------------
1107 (defun makefile-browser-format-target-line (target selected
)
1109 (concat (make-string makefile-browser-leftmost-column ?\
)
1111 makefile-browser-selected-mark
1112 makefile-browser-unselected-mark
)
1114 target makefile-target-colon
))
1116 (defun makefile-browser-format-macro-line (macro selected
)
1118 (concat (make-string makefile-browser-leftmost-column ?\
)
1120 makefile-browser-selected-mark
1121 makefile-browser-unselected-mark
)
1122 (makefile-format-macro-ref macro
))))
1124 (defun makefile-browser-fill (targets macros
)
1125 (let ((inhibit-read-only t
))
1126 (goto-char (point-min))
1130 (lambda (item) (insert (makefile-browser-format-target-line (car item
) nil
) "\n")))
1135 (lambda (item) (insert (makefile-browser-format-macro-line (car item
) nil
) "\n")))
1138 (sort-lines nil
(point-min) (point-max))
1139 (goto-char (1- (point-max)))
1140 (delete-char 1) ; remove unnecessary newline at eob
1141 (goto-char (point-min))
1142 (forward-char makefile-browser-cursor-column
)))
1145 ;;; Moving up and down in the browser
1148 (defun makefile-browser-next-line ()
1149 "Move the browser selection cursor to the next line."
1151 (if (not (makefile-last-line-p))
1154 (forward-char makefile-browser-cursor-column
))))
1156 (defun makefile-browser-previous-line ()
1157 "Move the browser selection cursor to the previous line."
1159 (if (not (makefile-first-line-p))
1162 (forward-char makefile-browser-cursor-column
))))
1165 ;;; Quitting the browser (returns to client buffer)
1168 (defun makefile-browser-quit ()
1169 "Leave the browser and return to the makefile buffer."
1171 (let ((my-client makefile-browser-client
))
1172 (setq makefile-browser-client nil
) ; we quitted, so NO client!
1173 (set-buffer-modified-p nil
)
1175 (pop-to-buffer my-client
)))
1178 ;;; Toggle state of a browser item
1181 (defun makefile-browser-toggle ()
1182 "Toggle the selection state of the browser item at the cursor position."
1184 (let ((this-line (count-lines (point-min) (point))))
1185 (setq this-line
(max 1 this-line
))
1186 (makefile-browser-toggle-state-for-line this-line
)
1187 (goto-line this-line
)
1188 (let ((inhibit-read-only t
))
1190 (if (makefile-browser-on-macro-line-p)
1191 (let ((macro-name (makefile-browser-this-line-macro-name)))
1192 (delete-region (point) (progn (end-of-line) (point)))
1194 (makefile-browser-format-macro-line
1196 (makefile-browser-get-state-for-line this-line
))))
1197 (let ((target-name (makefile-browser-this-line-target-name)))
1198 (delete-region (point) (progn (end-of-line) (point)))
1200 (makefile-browser-format-target-line
1202 (makefile-browser-get-state-for-line this-line
))))))
1204 (forward-char makefile-browser-cursor-column
)
1205 (if makefile-browser-auto-advance-after-selection-p
1206 (makefile-browser-next-line))))
1209 ;;; Making insertions into the client buffer
1212 (defun makefile-browser-insert-continuation ()
1213 "Insert a makefile continuation.
1214 In the makefile buffer, go to (end-of-line), insert a \'\\\'
1215 character, insert a new blank line, go to that line and indent by one TAB.
1216 This is most useful in the process of creating continued lines when copying
1217 large dependencies from the browser to the client buffer.
1218 \(point) advances accordingly in the client buffer."
1220 (with-current-buffer makefile-browser-client
1224 (defun makefile-browser-insert-selection ()
1225 "Insert all selected targets and/or macros in the makefile buffer.
1226 Insertion takes place at point."
1230 (let ((current-line 1))
1232 (if (makefile-browser-get-state-for-line current-line
)
1233 (makefile-browser-send-this-line-item))
1235 (setq current-line
(1+ current-line
))))))
1237 (defun makefile-browser-insert-selection-and-quit ()
1239 (makefile-browser-insert-selection)
1240 (makefile-browser-quit))
1242 (defun makefile-browser-send-this-line-item ()
1243 (if (makefile-browser-on-macro-line-p)
1245 (let ((macro-name (makefile-browser-this-line-macro-name)))
1246 (set-buffer makefile-browser-client
)
1247 (insert (makefile-format-macro-ref macro-name
) " ")))
1249 (let ((target-name (makefile-browser-this-line-target-name)))
1250 (set-buffer makefile-browser-client
)
1251 (insert target-name
" ")))))
1253 (defun makefile-browser-start-interaction ()
1254 (use-local-map makefile-browser-map
)
1255 (setq buffer-read-only t
))
1257 (defun makefile-browse (targets macros
)
1259 (if (zerop (+ (length targets
) (length macros
)))
1262 (message "No macros or targets to browse! Consider running 'makefile-pickup-everything\'"))
1263 (let ((browser-buffer (get-buffer-create makefile-browser-buffer-name
)))
1264 (pop-to-buffer browser-buffer
)
1265 (makefile-browser-fill targets macros
)
1266 (shrink-window-if-larger-than-buffer)
1267 (set (make-local-variable 'makefile-browser-selection-vector
)
1268 (make-vector (+ (length targets
) (length macros
)) nil
))
1269 (makefile-browser-start-interaction))))
1271 (defun makefile-switch-to-browser ()
1273 (run-hooks 'makefile-browser-hook
)
1274 (setq makefile-browser-client
(current-buffer))
1275 (makefile-pickup-targets)
1276 (makefile-pickup-macros)
1277 (makefile-browse makefile-target-table makefile-macro-table
))
1281 ;;; ------------------------------------------------------------
1282 ;;; Up-to-date overview buffer
1283 ;;; ------------------------------------------------------------
1285 (defun makefile-create-up-to-date-overview ()
1286 "Create a buffer containing an overview of the state of all known targets.
1287 Known targets are targets that are explicitly defined in that makefile;
1288 in other words, all targets that appear on the left hand side of a
1289 dependency in the makefile."
1291 (if (y-or-n-p "Are you sure that the makefile being edited is consistent? ")
1293 ;; The rest of this function operates on a temporary makefile, created by
1294 ;; writing the current contents of the makefile buffer.
1296 (let ((saved-target-table makefile-target-table
)
1297 (this-buffer (current-buffer))
1298 (makefile-up-to-date-buffer
1299 (get-buffer-create makefile-up-to-date-buffer-name
))
1300 (filename (makefile-save-temporary))
1302 ;; Forget the target table because it may contain picked-up filenames
1303 ;; that are not really targets in the current makefile.
1304 ;; We don't want to query these, so get a new target-table with just the
1305 ;; targets that can be found in the makefile buffer.
1306 ;; The 'old' target table will be restored later.
1308 (real-targets (progn
1309 (makefile-pickup-targets)
1310 makefile-target-table
))
1311 (prereqs makefile-has-prereqs
)
1314 (set-buffer makefile-up-to-date-buffer
)
1315 (setq buffer-read-only nil
)
1317 (makefile-query-targets filename real-targets prereqs
)
1318 (if (zerop (buffer-size)) ; if it did not get us anything
1320 (kill-buffer (current-buffer))
1321 (message "No overview created!")))
1322 (set-buffer this-buffer
)
1323 (setq makefile-target-table saved-target-table
)
1324 (if (get-buffer makefile-up-to-date-buffer-name
)
1326 (pop-to-buffer (get-buffer makefile-up-to-date-buffer-name
))
1327 (shrink-window-if-larger-than-buffer)
1328 (sort-lines nil
(point-min) (point-max))
1329 (setq buffer-read-only t
))))))
1331 (defun makefile-save-temporary ()
1332 "Create a temporary file from the current makefile buffer."
1333 (let ((filename (makefile-generate-temporary-filename)))
1334 (write-region (point-min) (point-max) filename nil
0)
1335 filename
)) ; return the filename
1337 (defun makefile-generate-temporary-filename ()
1338 "Create a filename suitable for use in `makefile-save-temporary'.
1339 Be careful to allow brain-dead file systems (DOS, SYSV ...) to cope
1340 with the generated name!"
1341 (let ((my-name (user-login-name))
1342 (my-uid (int-to-string (user-uid))))
1344 (if (> (length my-name
) 3)
1345 (substring my-name
0 3)
1348 (if (> (length my-uid
) 3)
1349 (substring my-uid
0 3)
1352 (defun makefile-query-targets (filename target-table prereq-list
)
1353 "Fill the up-to-date overview buffer.
1354 Checks each target in TARGET-TABLE using `makefile-query-one-target-method'
1355 and generates the overview, one line per target name."
1358 (function (lambda (item)
1359 (let* ((target-name (car item
))
1360 (no-prereqs (not (member target-name prereq-list
)))
1361 (needs-rebuild (or no-prereqs
1363 makefile-query-one-target-method
1368 (cond (no-prereqs " .. has no prerequisites")
1369 (needs-rebuild " .. NEEDS REBUILD")
1370 (t " .. is up to date"))))
1373 (goto-char (point-min))
1374 (delete-file filename
)) ; remove the tmpfile
1376 (defun makefile-query-by-make-minus-q (target &optional filename
)
1378 (call-process makefile-brave-make nil nil nil
1379 "-f" filename
"-q" target
))))
1383 ;;; ------------------------------------------------------------
1384 ;;; Continuation cleanup
1385 ;;; ------------------------------------------------------------
1387 (defun makefile-cleanup-continuations ()
1388 (if (eq major-mode
'makefile-mode
)
1389 (if (and makefile-cleanup-continuations
1390 (not buffer-read-only
))
1392 (goto-char (point-min))
1393 (while (re-search-forward "\\\\[ \t]+$" nil t
)
1394 (replace-match "\\" t t
))))))
1397 ;;; ------------------------------------------------------------
1398 ;;; Warn of suspicious lines
1399 ;;; ------------------------------------------------------------
1401 (defun makefile-warn-suspicious-lines ()
1402 ;; Returning non-nil cancels the save operation
1403 (if (eq major-mode
'makefile-mode
)
1405 (goto-char (point-min))
1406 (if (re-search-forward "^\\(\t+$\\| +\t\\)" nil t
)
1408 (format "Suspicious line %d. Save anyway? "
1409 (count-lines (point-min) (point)))))))))
1411 (defun makefile-warn-continuations ()
1412 (if (eq major-mode
'makefile-mode
)
1414 (goto-char (point-min))
1415 (if (re-search-forward "\\\\[ \t]+$" nil t
)
1417 (format "Suspicious continuation in line %d. Save anyway? "
1418 (count-lines (point-min) (point)))))))))
1421 ;;; ------------------------------------------------------------
1422 ;;; GNU make function support
1423 ;;; ------------------------------------------------------------
1425 (defun makefile-insert-gmake-function ()
1426 "Insert a GNU make function call.
1427 Asks for the name of the function to use (with completion).
1428 Then prompts for all required parameters."
1430 (let* ((gm-function-name (completing-read
1432 makefile-gnumake-functions-alist
1434 (gm-function-prompts
1435 (cdr (assoc gm-function-name makefile-gnumake-functions-alist
))))
1436 (if (not (zerop (length gm-function-name
)))
1437 (insert (makefile-format-macro-ref
1438 (concat gm-function-name
" "
1439 (makefile-prompt-for-gmake-funargs
1440 gm-function-name gm-function-prompts
)))
1443 (defun makefile-prompt-for-gmake-funargs (function-name prompt-list
)
1445 (function (lambda (one-prompt)
1446 (read-string (format "[%s] %s: " function-name one-prompt
)
1453 ;;; ------------------------------------------------------------
1454 ;;; Utility functions
1455 ;;; ------------------------------------------------------------
1457 (defun makefile-do-macro-insertion (macro-name)
1458 "Insert a macro reference."
1459 (if (not (zerop (length macro-name
)))
1460 (if (assoc macro-name makefile-runtime-macros-list
)
1461 (insert "$" macro-name
)
1462 (insert (makefile-format-macro-ref macro-name
)))))
1464 (defun makefile-remember-target (target-name &optional has-prereqs
)
1465 "Remember a given target if it is not already remembered for this buffer."
1466 (if (not (zerop (length target-name
)))
1468 (if (not (assoc target-name makefile-target-table
))
1469 (setq makefile-target-table
1470 (cons (list target-name
) makefile-target-table
)))
1472 (setq makefile-has-prereqs
1473 (cons target-name makefile-has-prereqs
))))))
1475 (defun makefile-remember-macro (macro-name)
1476 "Remember a given macro if it is not already remembered for this buffer."
1477 (if (not (zerop (length macro-name
)))
1478 (if (not (assoc macro-name makefile-macro-table
))
1479 (setq makefile-macro-table
1480 (cons (list macro-name
) makefile-macro-table
)))))
1482 (defun makefile-forward-after-target-colon ()
1483 "Move point forward after inserting the terminating colon of a target.
1484 This acts according to the value of `makefile-tab-after-target-colon'."
1485 (if makefile-tab-after-target-colon
1489 (defun makefile-browser-on-macro-line-p ()
1490 "Determine if point is on a macro line in the browser."
1493 (re-search-forward "\\$[{(]" (line-end-position) t
)))
1495 (defun makefile-browser-this-line-target-name ()
1496 "Extract the target name from a line in the browser."
1499 (skip-chars-backward "^ \t")
1500 (buffer-substring (point) (1- (line-end-position)))))
1502 (defun makefile-browser-this-line-macro-name ()
1503 "Extract the macro name from a line in the browser."
1506 (re-search-forward "\\$[{(]" (line-end-position) t
)
1507 (let ((macro-start (point)))
1508 (skip-chars-forward "^})")
1509 (buffer-substring macro-start
(point)))))
1511 (defun makefile-format-macro-ref (macro-name)
1512 "Format a macro reference.
1513 Uses `makefile-use-curly-braces-for-macros-p'."
1514 (if (or (char-equal ?\
( (string-to-char macro-name
))
1515 (char-equal ?\
{ (string-to-char macro-name
)))
1516 (format "$%s" macro-name
)
1517 (if makefile-use-curly-braces-for-macros-p
1518 (format "${%s}" macro-name
)
1519 (format "$(%s)" macro-name
))))
1521 (defun makefile-browser-get-state-for-line (n)
1522 (aref makefile-browser-selection-vector
(1- n
)))
1524 (defun makefile-browser-set-state-for-line (n to-state
)
1525 (aset makefile-browser-selection-vector
(1- n
) to-state
))
1527 (defun makefile-browser-toggle-state-for-line (n)
1528 (makefile-browser-set-state-for-line n
(not (makefile-browser-get-state-for-line n
))))
1530 (defun makefile-last-line-p ()
1531 (= (line-end-position) (point-max)))
1533 (defun makefile-first-line-p ()
1534 (= (line-beginning-position) (point-min)))
1538 ;;; Support for other packages, like add-log.
1540 (defun makefile-add-log-defun ()
1541 "Return name of target or variable assignment that point is in.
1542 If it isn't in one, return nil."
1546 ;; Scan back line by line, noticing when we come to a
1547 ;; variable or rule definition, and giving up when we see
1548 ;; a line that is not part of either of those.
1549 (while (not (or (setq found
1550 (when (or (looking-at makefile-macroassign-regex
)
1551 (looking-at makefile-dependency-regex
))
1552 (match-string-no-properties 1)))
1553 ;; Don't keep looking across a blank line or comment.
1554 (looking-at "$\\|#")
1555 (not (zerop (forward-line -
1))))))
1558 (provide 'make-mode
)
1560 ;;; arch-tag: bd23545a-de91-44fb-b1b2-feafbb2635a0
1561 ;;; make-mode.el ends here