| 1 | ;;; make-mode.el --- makefile editing commands for Emacs |
| 2 | |
| 3 | ;; Copyright (C) 1992, 1994, 1999, 2000, 2001, 2002, 2003, 2004, 2005, |
| 4 | ;; 2006, 2007, 2008, 2009, 2010 Free Software Foundation, Inc. |
| 5 | |
| 6 | ;; Author: Thomas Neumann <tom@smart.bo.open.de> |
| 7 | ;; Eric S. Raymond <esr@snark.thyrsus.com> |
| 8 | ;; Maintainer: FSF |
| 9 | ;; Adapted-By: ESR |
| 10 | ;; Keywords: unix, tools |
| 11 | |
| 12 | ;; This file is part of GNU Emacs. |
| 13 | |
| 14 | ;; GNU Emacs is free software: you can redistribute it and/or modify |
| 15 | ;; it under the terms of the GNU General Public License as published by |
| 16 | ;; the Free Software Foundation, either version 3 of the License, or |
| 17 | ;; (at your option) any later version. |
| 18 | |
| 19 | ;; GNU Emacs is distributed in the hope that it will be useful, |
| 20 | ;; but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 21 | ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 22 | ;; GNU General Public License for more details. |
| 23 | |
| 24 | ;; You should have received a copy of the GNU General Public License |
| 25 | ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>. |
| 26 | |
| 27 | ;;; Commentary: |
| 28 | |
| 29 | ;; A major mode for editing makefiles. The mode knows about Makefile |
| 30 | ;; syntax and defines M-n and M-p to move to next and previous productions. |
| 31 | ;; |
| 32 | ;; The keys $, =, : and . are electric; they try to help you fill in a |
| 33 | ;; macro reference, macro definition, ordinary target name, or special |
| 34 | ;; target name, respectively. Such names are completed using a list of |
| 35 | ;; targets and macro names parsed out of the makefile. This list is |
| 36 | ;; automatically updated, if necessary, whenever you invoke one of |
| 37 | ;; these commands. You can force it to be updated with C-c C-p. |
| 38 | ;; |
| 39 | ;; The command C-c C-f adds certain filenames in the current directory |
| 40 | ;; as targets. You can filter out filenames by setting the variable |
| 41 | ;; makefile-ignored-files-in-pickup-regex. |
| 42 | ;; |
| 43 | ;; The command C-c C-u grinds for a bit, then pops up a report buffer |
| 44 | ;; showing which target names are up-to-date with respect to their |
| 45 | ;; prerequisites, which targets are out-of-date, and which have no |
| 46 | ;; prerequisites. |
| 47 | ;; |
| 48 | ;; The command C-c C-b pops up a browser window listing all target and |
| 49 | ;; macro names. You can mark or unmark items with C-c SPC, and insert |
| 50 | ;; all marked items back in the Makefile with C-c TAB. |
| 51 | ;; |
| 52 | ;; The command C-c TAB in the makefile buffer inserts a GNU make builtin. |
| 53 | ;; You will be prompted for the builtin's args. |
| 54 | ;; |
| 55 | ;; There are numerous other customization variables. |
| 56 | |
| 57 | ;; |
| 58 | ;; To Do: |
| 59 | ;; |
| 60 | ;; * Add missing doc strings, improve terse doc strings. |
| 61 | ;; * Eliminate electric stuff entirely. |
| 62 | ;; * It might be nice to highlight targets differently depending on |
| 63 | ;; whether they are up-to-date or not. Not sure how this would |
| 64 | ;; interact with font-lock. |
| 65 | ;; * Would be nice to edit the commands in ksh-mode and have |
| 66 | ;; indentation and slashification done automatically. Hard. |
| 67 | ;; * Consider removing browser mode. It seems useless. |
| 68 | ;; * ":" should notice when a new target is made and add it to the |
| 69 | ;; list (or at least set makefile-need-target-pickup). |
| 70 | ;; * Make browser into a major mode. |
| 71 | ;; * Clean up macro insertion stuff. It is a mess. |
| 72 | ;; * Browser entry and exit is weird. Normalize. |
| 73 | ;; * Browser needs to be rewritten. Right now it is kind of a crock. |
| 74 | ;; Should at least: |
| 75 | ;; * Act more like dired/buffer menu/whatever. |
| 76 | ;; * Highlight as mouse traverses. |
| 77 | ;; * B2 inserts. |
| 78 | ;; * Update documentation above. |
| 79 | ;; * Update texinfo manual. |
| 80 | ;; * Update files.el. |
| 81 | |
| 82 | \f |
| 83 | |
| 84 | ;;; Code: |
| 85 | |
| 86 | ;; Sadly we need this for a macro. |
| 87 | (eval-when-compile |
| 88 | (require 'imenu) |
| 89 | (require 'dabbrev) |
| 90 | (require 'add-log)) |
| 91 | |
| 92 | ;;; ------------------------------------------------------------ |
| 93 | ;;; Configurable stuff |
| 94 | ;;; ------------------------------------------------------------ |
| 95 | |
| 96 | (defgroup makefile nil |
| 97 | "Makefile editing commands for Emacs." |
| 98 | :link '(custom-group-link :tag "Font Lock Faces group" font-lock-faces) |
| 99 | :group 'tools |
| 100 | :prefix "makefile-") |
| 101 | |
| 102 | (defface makefile-space |
| 103 | '((((class color)) (:background "hotpink")) |
| 104 | (t (:reverse-video t))) |
| 105 | "Face to use for highlighting leading spaces in Font-Lock mode." |
| 106 | :group 'makefile) |
| 107 | (define-obsolete-face-alias 'makefile-space-face 'makefile-space "22.1") |
| 108 | |
| 109 | (defface makefile-targets |
| 110 | ;; This needs to go along both with foreground and background colors (i.e. shell) |
| 111 | '((t (:inherit font-lock-function-name-face))) |
| 112 | "Face to use for additionally highlighting rule targets in Font-Lock mode." |
| 113 | :group 'makefile |
| 114 | :version "22.1") |
| 115 | |
| 116 | (defface makefile-shell |
| 117 | () |
| 118 | ;;'((((class color) (min-colors 88) (background light)) (:background "seashell1")) |
| 119 | ;; (((class color) (min-colors 88) (background dark)) (:background "seashell4"))) |
| 120 | "Face to use for additionally highlighting Shell commands in Font-Lock mode." |
| 121 | :group 'makefile |
| 122 | :version "22.1") |
| 123 | |
| 124 | (defface makefile-makepp-perl |
| 125 | '((((class color) (background light)) (:background "LightBlue1")) ; Camel Book |
| 126 | (((class color) (background dark)) (:background "DarkBlue")) |
| 127 | (t (:reverse-video t))) |
| 128 | "Face to use for additionally highlighting Perl code in Font-Lock mode." |
| 129 | :group 'makefile |
| 130 | :version "22.1") |
| 131 | |
| 132 | (defcustom makefile-browser-buffer-name "*Macros and Targets*" |
| 133 | "*Name of the macro- and target browser buffer." |
| 134 | :type 'string |
| 135 | :group 'makefile) |
| 136 | |
| 137 | (defcustom makefile-target-colon ":" |
| 138 | "*String to append to all target names inserted by `makefile-insert-target'. |
| 139 | \":\" or \"::\" are common values." |
| 140 | :type 'string |
| 141 | :group 'makefile) |
| 142 | |
| 143 | (defcustom makefile-macro-assign " = " |
| 144 | "*String to append to all macro names inserted by `makefile-insert-macro'. |
| 145 | The normal value should be \" = \", since this is what |
| 146 | standard make expects. However, newer makes such as dmake |
| 147 | allow a larger variety of different macro assignments, so you |
| 148 | might prefer to use \" += \" or \" := \" ." |
| 149 | :type 'string |
| 150 | :group 'makefile) |
| 151 | |
| 152 | (defcustom makefile-electric-keys nil |
| 153 | "*If non-nil, Makefile mode should install electric keybindings. |
| 154 | Default is nil." |
| 155 | :type 'boolean |
| 156 | :group 'makefile) |
| 157 | |
| 158 | (defcustom makefile-use-curly-braces-for-macros-p nil |
| 159 | "*Controls the style of generated macro references. |
| 160 | Non-nil means macro references should use curly braces, like `${this}'. |
| 161 | nil means use parentheses, like `$(this)'." |
| 162 | :type 'boolean |
| 163 | :group 'makefile) |
| 164 | |
| 165 | (defcustom makefile-tab-after-target-colon t |
| 166 | "*If non-nil, insert a TAB after a target colon. |
| 167 | Otherwise, a space is inserted. |
| 168 | The default is t." |
| 169 | :type 'boolean |
| 170 | :group 'makefile) |
| 171 | |
| 172 | (defcustom makefile-browser-leftmost-column 10 |
| 173 | "*Number of blanks to the left of the browser selection mark." |
| 174 | :type 'integer |
| 175 | :group 'makefile) |
| 176 | |
| 177 | (defcustom makefile-browser-cursor-column 10 |
| 178 | "*Column the cursor goes to when it moves up or down in the Makefile browser." |
| 179 | :type 'integer |
| 180 | :group 'makefile) |
| 181 | |
| 182 | (defcustom makefile-backslash-column 48 |
| 183 | "*Column in which `makefile-backslash-region' inserts backslashes." |
| 184 | :type 'integer |
| 185 | :group 'makefile) |
| 186 | |
| 187 | (defcustom makefile-backslash-align t |
| 188 | "*If non-nil, `makefile-backslash-region' will align backslashes." |
| 189 | :type 'boolean |
| 190 | :group 'makefile) |
| 191 | |
| 192 | (defcustom makefile-browser-selected-mark "+ " |
| 193 | "*String used to mark selected entries in the Makefile browser." |
| 194 | :type 'string |
| 195 | :group 'makefile) |
| 196 | |
| 197 | (defcustom makefile-browser-unselected-mark " " |
| 198 | "*String used to mark unselected entries in the Makefile browser." |
| 199 | :type 'string |
| 200 | :group 'makefile) |
| 201 | |
| 202 | (defcustom makefile-browser-auto-advance-after-selection-p t |
| 203 | "*If non-nil, cursor will move after item is selected in Makefile browser." |
| 204 | :type 'boolean |
| 205 | :group 'makefile) |
| 206 | |
| 207 | (defcustom makefile-pickup-everything-picks-up-filenames-p nil |
| 208 | "*If non-nil, `makefile-pickup-everything' picks up filenames as targets. |
| 209 | This means it calls `makefile-pickup-filenames-as-targets'. |
| 210 | Otherwise filenames are omitted." |
| 211 | :type 'boolean |
| 212 | :group 'makefile) |
| 213 | |
| 214 | (defcustom makefile-cleanup-continuations nil |
| 215 | "*If non-nil, automatically clean up continuation lines when saving. |
| 216 | A line is cleaned up by removing all whitespace following a trailing |
| 217 | backslash. This is done silently. |
| 218 | IMPORTANT: Please note that enabling this option causes Makefile mode |
| 219 | to MODIFY A FILE WITHOUT YOUR CONFIRMATION when \"it seems necessary\"." |
| 220 | :type 'boolean |
| 221 | :group 'makefile) |
| 222 | |
| 223 | (defcustom makefile-mode-hook nil |
| 224 | "*Normal hook run by `makefile-mode'." |
| 225 | :type 'hook |
| 226 | :group 'makefile) |
| 227 | |
| 228 | (defvar makefile-browser-hook '()) |
| 229 | |
| 230 | ;; |
| 231 | ;; Special targets for DMake, Sun's make ... |
| 232 | ;; |
| 233 | (defcustom makefile-special-targets-list |
| 234 | '("DEFAULT" "DONE" "ERROR" "EXPORT" |
| 235 | "FAILED" "GROUPEPILOG" "GROUPPROLOG" "IGNORE" |
| 236 | "IMPORT" "INCLUDE" "INCLUDEDIRS" "INIT" |
| 237 | "KEEP_STATE" "MAKEFILES" "MAKE_VERSION" "NO_PARALLEL" |
| 238 | "PARALLEL" "PHONY" "PRECIOUS" "REMOVE" |
| 239 | "SCCS_GET" "SILENT" "SOURCE" "SUFFIXES" |
| 240 | "WAIT" "c.o" "C.o" "m.o" |
| 241 | "el.elc" "y.c" "s.o") |
| 242 | "List of special targets. |
| 243 | You will be offered to complete on one of those in the minibuffer whenever |
| 244 | you enter a \".\" at the beginning of a line in `makefile-mode'." |
| 245 | :type '(repeat (list string)) |
| 246 | :group 'makefile) |
| 247 | (put 'makefile-special-targets-list 'risky-local-variable t) |
| 248 | |
| 249 | (defcustom makefile-runtime-macros-list |
| 250 | '(("@") ("&") (">") ("<") ("*") ("^") ("+") ("?") ("%") ("$")) |
| 251 | "*List of macros that are resolved by make at runtime. |
| 252 | If you insert a macro reference using `makefile-insert-macro-ref', the name |
| 253 | of the macro is checked against this list. If it can be found its name will |
| 254 | not be enclosed in { } or ( )." |
| 255 | :type '(repeat (list string)) |
| 256 | :group 'makefile) |
| 257 | |
| 258 | ;; Note that the first big subexpression is used by font lock. Note |
| 259 | ;; that if you change this regexp you might have to fix the imenu |
| 260 | ;; index in makefile-imenu-generic-expression. |
| 261 | (defvar makefile-dependency-regex |
| 262 | ;; Allow for two nested levels $(v1:$(v2:$(v3:a=b)=c)=d) |
| 263 | "^\\(\\(?:\\$\\(?:[({]\\(?:\\$\\(?:[({]\\(?:\\$\\(?:[^({]\\|.[^\n$#})]+?[})]\\)\\|[^\n$#)}]\\)+?[})]\\|[^({]\\)\\|[^\n$#)}]\\)+?[})]\\|[^({]\\)\\|[^\n$#:=]\\)+?\\)\\(:\\)\\(?:[ \t]*$\\|[^=\n]\\(?:[^#\n]*?;[ \t]*\\(.+\\)\\)?\\)" |
| 264 | "Regex used to find dependency lines in a makefile.") |
| 265 | |
| 266 | (defconst makefile-bsdmake-dependency-regex |
| 267 | (progn (string-match (regexp-quote "\\(:\\)") makefile-dependency-regex) |
| 268 | (replace-match "\\([:!]\\)" t t makefile-dependency-regex)) |
| 269 | "Regex used to find dependency lines in a BSD makefile.") |
| 270 | |
| 271 | (defvar makefile-dependency-skip "^:" |
| 272 | "Characters to skip to find a line that might be a dependency.") |
| 273 | |
| 274 | (defvar makefile-rule-action-regex |
| 275 | "^\t[ \t]*\\(?:\\([-@]+\\)[ \t]*\\)\\(.*\\(?:\\\\\n.*\\)*\\)" |
| 276 | "Regex used to highlight rule action lines in font lock mode.") |
| 277 | |
| 278 | (defconst makefile-makepp-rule-action-regex |
| 279 | ;; Don't care about initial tab, but I don't know how to font-lock correctly without. |
| 280 | "^\t[ \t]*\\(\\(?:\\(?:noecho\\|ignore[-_]error\\|[-@]+\\)[ \t]*\\)*\\)\\(\\(&\\S +\\)?\\(?:.*\\\\\n\\)*.*\\)" |
| 281 | "Regex used to highlight makepp rule action lines in font lock mode.") |
| 282 | |
| 283 | (defconst makefile-bsdmake-rule-action-regex |
| 284 | (replace-regexp-in-string "-@" "-+@" makefile-rule-action-regex) |
| 285 | "Regex used to highlight BSD rule action lines in font lock mode.") |
| 286 | |
| 287 | ;; Note that the first and second subexpression is used by font lock. Note |
| 288 | ;; that if you change this regexp you might have to fix the imenu index in |
| 289 | ;; makefile-imenu-generic-expression. |
| 290 | (defconst makefile-macroassign-regex |
| 291 | ;; We used to match not just the varname but also the whole value |
| 292 | ;; (spanning potentially several lines). |
| 293 | ;; "^ *\\([^ \n\t][^:#= \t\n]*\\)[ \t]*\\(?:!=[ \t]*\\(\\(?:.+\\\\\n\\)*.+\\)\\|[*:+]?[:?]?=[ \t]*\\(\\(?:.*\\\\\n\\)*.*\\)\\)" |
| 294 | ;; What about the define statement? What about differentiating this for makepp? |
| 295 | "\\(?:^\\|^export\\|^override\\|:\\|: *override\\) *\\([^ \n\t][^:#= \t\n]*\\)[ \t]*\\(?:!=\\|[*:+]?[:?]?=\\)" |
| 296 | "Regex used to find macro assignment lines in a makefile.") |
| 297 | |
| 298 | (defconst makefile-var-use-regex |
| 299 | "[^$]\\$[({]\\([-a-zA-Z0-9_.]+\\|[@%<?^+*][FD]?\\)" |
| 300 | "Regex used to find $(macro) uses in a makefile.") |
| 301 | |
| 302 | (defconst makefile-ignored-files-in-pickup-regex |
| 303 | "\\(^\\..*\\)\\|\\(.*~$\\)\\|\\(.*,v$\\)\\|\\(\\.[chy]\\)" |
| 304 | "Regex for filenames that will NOT be included in the target list.") |
| 305 | |
| 306 | (defvar makefile-space 'makefile-space |
| 307 | "Face to use for highlighting leading spaces in Font-Lock mode.") |
| 308 | |
| 309 | ;; These lists were inspired by the old solution. But they are silly, because |
| 310 | ;; you can't differentiate what follows. They need to be split up. |
| 311 | (defconst makefile-statements '("include") |
| 312 | "List of keywords understood by standard make.") |
| 313 | |
| 314 | (defconst makefile-automake-statements |
| 315 | `("if" "else" "endif" ,@makefile-statements) |
| 316 | "List of keywords understood by automake.") |
| 317 | |
| 318 | (defconst makefile-gmake-statements |
| 319 | `("-sinclude" "sinclude" "vpath" ; makefile-makepp-statements takes rest |
| 320 | "ifdef" "ifndef" "ifeq" "ifneq" "-include" "define" "endef" "export" |
| 321 | "override define" "override" "unexport" |
| 322 | ,@(cdr makefile-automake-statements)) |
| 323 | "List of keywords understood by gmake.") |
| 324 | |
| 325 | ;; These are even more silly, because you can have more spaces in between. |
| 326 | (defconst makefile-makepp-statements |
| 327 | `("and ifdef" "and ifndef" "and ifeq" "and ifneq" "and ifperl" |
| 328 | "and ifmakeperl" "and ifsys" "and ifnsys" "build_cache" "build_check" |
| 329 | "else ifdef" "else ifndef" "else ifeq" "else ifneq" "else ifperl" |
| 330 | "else ifmakeperl" "else ifsys" "else ifnsys" "enddef" "global" |
| 331 | "load_makefile" "ifperl" "ifmakeperl" "ifsys" "ifnsys" "_include" |
| 332 | "makeperl" "makesub" "no_implicit_load" "perl" "perl-begin" "perl_begin" |
| 333 | "perl-end" "perl_end" "prebuild" "or ifdef" "or ifndef" "or ifeq" |
| 334 | "or ifneq" "or ifperl" "or ifmakeperl" "or ifsys" "or ifnsys" |
| 335 | "override export" "override global" "register_command_parser" |
| 336 | "register_scanner" "repository" "runtime" "signature" "sub" |
| 337 | ,@(nthcdr 3 makefile-gmake-statements)) |
| 338 | "List of keywords understood by gmake.") |
| 339 | |
| 340 | (defconst makefile-bsdmake-statements |
| 341 | `(".elif" ".elifdef" ".elifmake" ".elifndef" ".elifnmake" ".else" ".endfor" |
| 342 | ".endif" ".for" ".if" ".ifdef" ".ifmake" ".ifndef" ".ifnmake" ".undef") |
| 343 | "List of keywords understood by BSD make.") |
| 344 | |
| 345 | (defun makefile-make-font-lock-keywords (var keywords space |
| 346 | &optional negation |
| 347 | &rest font-lock-keywords) |
| 348 | `(;; Do macro assignments. These get the "variable-name" face. |
| 349 | (,makefile-macroassign-regex |
| 350 | (1 font-lock-variable-name-face) |
| 351 | ;; This is for after != |
| 352 | (2 'makefile-shell prepend t) |
| 353 | ;; This is for after normal assignment |
| 354 | (3 'font-lock-string-face prepend t)) |
| 355 | |
| 356 | ;; Rule actions. |
| 357 | ;; FIXME: When this spans multiple lines we need font-lock-multiline. |
| 358 | (makefile-match-action |
| 359 | (1 font-lock-type-face nil t) |
| 360 | (2 'makefile-shell prepend) |
| 361 | ;; Only makepp has builtin commands. |
| 362 | (3 font-lock-builtin-face prepend t)) |
| 363 | |
| 364 | ;; Variable references even in targets/strings/comments. |
| 365 | (,var 1 font-lock-variable-name-face prepend) |
| 366 | |
| 367 | ;; Automatic variable references and single character variable references, |
| 368 | ;; but not shell variables references. |
| 369 | ("[^$]\\$\\([@%<?^+*_]\\|[a-zA-Z0-9]\\>\\)" |
| 370 | 1 font-lock-constant-face prepend) |
| 371 | ("[^$]\\(\\$[@%*]\\)" |
| 372 | 1 'makefile-targets append) |
| 373 | |
| 374 | ;; Fontify conditionals and includes. |
| 375 | (,(concat "^\\(?: [ \t]*\\)?" |
| 376 | (regexp-opt keywords t) |
| 377 | "\\>[ \t]*\\([^: \t\n#]*\\)") |
| 378 | (1 font-lock-keyword-face) (2 font-lock-variable-name-face)) |
| 379 | |
| 380 | ,@(if negation |
| 381 | `((,negation (1 font-lock-negation-char-face prepend) |
| 382 | (2 font-lock-negation-char-face prepend t)))) |
| 383 | |
| 384 | ,@(if space |
| 385 | '(;; Highlight lines that contain just whitespace. |
| 386 | ;; They can cause trouble, especially if they start with a tab. |
| 387 | ("^[ \t]+$" . makefile-space) |
| 388 | |
| 389 | ;; Highlight shell comments that Make treats as commands, |
| 390 | ;; since these can fool people. |
| 391 | ("^\t+#" 0 makefile-space t) |
| 392 | |
| 393 | ;; Highlight spaces that precede tabs. |
| 394 | ;; They can make a tab fail to be effective. |
| 395 | ("^\\( +\\)\t" 1 makefile-space))) |
| 396 | |
| 397 | ,@font-lock-keywords |
| 398 | |
| 399 | ;; Do dependencies. |
| 400 | (makefile-match-dependency |
| 401 | (1 'makefile-targets prepend) |
| 402 | (3 'makefile-shell prepend t)))) |
| 403 | |
| 404 | (defconst makefile-font-lock-keywords |
| 405 | (makefile-make-font-lock-keywords |
| 406 | makefile-var-use-regex |
| 407 | makefile-statements |
| 408 | t)) |
| 409 | |
| 410 | (defconst makefile-automake-font-lock-keywords |
| 411 | (makefile-make-font-lock-keywords |
| 412 | makefile-var-use-regex |
| 413 | makefile-automake-statements |
| 414 | t)) |
| 415 | |
| 416 | (defconst makefile-gmake-font-lock-keywords |
| 417 | (makefile-make-font-lock-keywords |
| 418 | makefile-var-use-regex |
| 419 | makefile-gmake-statements |
| 420 | t |
| 421 | "^\\(?: [ \t]*\\)?if\\(n\\)\\(?:def\\|eq\\)\\>" |
| 422 | |
| 423 | '("[^$]\\(\\$[({][@%*][DF][})]\\)" |
| 424 | 1 'makefile-targets append) |
| 425 | |
| 426 | ;; $(function ...) ${function ...} |
| 427 | '("[^$]\\$[({]\\([-a-zA-Z0-9_.]+\\s \\)" |
| 428 | 1 font-lock-function-name-face prepend) |
| 429 | |
| 430 | ;; $(shell ...) ${shell ...} |
| 431 | '("[^$]\\$\\([({]\\)shell[ \t]+" |
| 432 | makefile-match-function-end nil nil |
| 433 | (1 'makefile-shell prepend t)))) |
| 434 | |
| 435 | (defconst makefile-makepp-font-lock-keywords |
| 436 | (makefile-make-font-lock-keywords |
| 437 | makefile-var-use-regex |
| 438 | makefile-makepp-statements |
| 439 | nil |
| 440 | "^\\(?: [ \t]*\\)?\\(?:and[ \t]+\\|else[ \t]+\\|or[ \t]+\\)?if\\(n\\)\\(?:def\\|eq\\|sys\\)\\>" |
| 441 | |
| 442 | '("[^$]\\(\\$[({]\\(?:output\\|stem\\|target\\)s?\\_>.*?[})]\\)" |
| 443 | 1 'makefile-targets append) |
| 444 | |
| 445 | ;; Colon modifier keywords. |
| 446 | '("\\(:\\s *\\)\\(build_c\\(?:ache\\|heck\\)\\|env\\(?:ironment\\)?\\|foreach\\|signature\\|scanner\\|quickscan\\|smartscan\\)\\>\\([^:\n]*\\)" |
| 447 | (1 font-lock-type-face t) |
| 448 | (2 font-lock-keyword-face t) |
| 449 | (3 font-lock-variable-name-face t)) |
| 450 | |
| 451 | ;; $(function ...) $((function ...)) ${function ...} ${{function ...}} |
| 452 | '("[^$]\\$\\(?:((?\\|{{?\\)\\([-a-zA-Z0-9_.]+\\s \\)" |
| 453 | 1 font-lock-function-name-face prepend) |
| 454 | |
| 455 | ;; $(shell ...) $((shell ...)) ${shell ...} ${{shell ...}} |
| 456 | '("[^$]\\$\\(((?\\|{{?\\)shell\\(?:[-_]\\(?:global[-_]\\)?once\\)?[ \t]+" |
| 457 | makefile-match-function-end nil nil |
| 458 | (1 'makefile-shell prepend t)) |
| 459 | |
| 460 | ;; $(perl ...) $((perl ...)) ${perl ...} ${{perl ...}} |
| 461 | '("[^$]\\$\\(((?\\|{{?\\)makeperl[ \t]+" |
| 462 | makefile-match-function-end nil nil |
| 463 | (1 'makefile-makepp-perl prepend t)) |
| 464 | '("[^$]\\$\\(((?\\|{{?\\)perl[ \t]+" |
| 465 | makefile-match-function-end nil nil |
| 466 | (1 'makefile-makepp-perl t t)) |
| 467 | |
| 468 | ;; Can we unify these with (if (match-end 1) 'prepend t)? |
| 469 | '("ifmakeperl\\s +\\(.*\\)" 1 'makefile-makepp-perl prepend) |
| 470 | '("ifperl\\s +\\(.*\\)" 1 'makefile-makepp-perl t) |
| 471 | |
| 472 | ;; Perl block single- or multiline, as statement or rule action. |
| 473 | ;; Don't know why the initial newline in 2nd variant of group 2 doesn't get skipped. |
| 474 | '("\\<make\\(?:perl\\|sub\\s +\\S +\\)\\s *\n?\\s *{\\(?:{\\s *\n?\\(\\(?:.*\n\\)+?\\)\\s *}\\|\\s *\\(\\(?:.*?\\|\n?\\(?:.*\n\\)+?\\)\\)\\)}" |
| 475 | (1 'makefile-makepp-perl prepend t) |
| 476 | (2 'makefile-makepp-perl prepend t)) |
| 477 | '("\\<\\(?:perl\\|sub\\s +\\S +\\)\\s *\n?\\s *{\\(?:{\\s *\n?\\(\\(?:.*\n\\)+?\\)\\s *}\\|\\s *\\(\\(?:.*?\\|\n?\\(?:.*\n\\)+?\\)\\)\\)}" |
| 478 | (1 'makefile-makepp-perl t t) |
| 479 | (2 'makefile-makepp-perl t t)) |
| 480 | |
| 481 | ;; Statement style perl block. |
| 482 | '("perl[-_]begin\\s *\\(?:\\s #.*\\)?\n\\(\\(?:.*\n\\)+?\\)\\s *perl[-_]end\\>" |
| 483 | 1 'makefile-makepp-perl t))) |
| 484 | |
| 485 | (defconst makefile-bsdmake-font-lock-keywords |
| 486 | (makefile-make-font-lock-keywords |
| 487 | ;; A lot more could be done for variables here: |
| 488 | makefile-var-use-regex |
| 489 | makefile-bsdmake-statements |
| 490 | t |
| 491 | "^\\(?: [ \t]*\\)?\\.\\(?:el\\)?if\\(n?\\)\\(?:def\\|make\\)?\\>[ \t]*\\(!?\\)" |
| 492 | '("^[ \t]*\\.for[ \t].+[ \t]\\(in\\)\\>" 1 font-lock-keyword-face))) |
| 493 | |
| 494 | (defconst makefile-imake-font-lock-keywords |
| 495 | (append |
| 496 | (makefile-make-font-lock-keywords |
| 497 | makefile-var-use-regex |
| 498 | makefile-statements |
| 499 | t |
| 500 | nil |
| 501 | '("^XCOMM.*$" . font-lock-comment-face) |
| 502 | '("XVAR\\(?:use\\|def\\)[0-9]" 0 font-lock-keyword-face prepend) |
| 503 | '("@@" . font-lock-preprocessor-face) |
| 504 | ) |
| 505 | cpp-font-lock-keywords)) |
| 506 | |
| 507 | |
| 508 | (defconst makefile-font-lock-syntactic-keywords |
| 509 | ;; From sh-script.el. |
| 510 | ;; A `#' begins a comment in sh when it is unquoted and at the beginning |
| 511 | ;; of a word. In the shell, words are separated by metacharacters. |
| 512 | ;; The list of special chars is taken from the single-unix spec of the |
| 513 | ;; shell command language (under `quoting') but with `$' removed. |
| 514 | '(("[^|&;<>()`\\\"' \t\n]\\(#+\\)" 1 "_") |
| 515 | ;; Change the syntax of a quoted newline so that it does not end a comment. |
| 516 | ("\\\\\n" 0 "."))) |
| 517 | |
| 518 | (defvar makefile-imenu-generic-expression |
| 519 | `(("Dependencies" makefile-previous-dependency 1) |
| 520 | ("Macro Assignment" ,makefile-macroassign-regex 1)) |
| 521 | "Imenu generic expression for Makefile mode. See `imenu-generic-expression'.") |
| 522 | |
| 523 | ;; ------------------------------------------------------------ |
| 524 | ;; The following configurable variables are used in the |
| 525 | ;; up-to-date overview . |
| 526 | ;; The standard configuration assumes that your `make' program |
| 527 | ;; can be run in question/query mode using the `-q' option, this |
| 528 | ;; means that the command |
| 529 | ;; |
| 530 | ;; make -q foo |
| 531 | ;; |
| 532 | ;; should return an exit status of zero if the target `foo' is |
| 533 | ;; up to date and a nonzero exit status otherwise. |
| 534 | ;; Many makes can do this although the docs/manpages do not mention |
| 535 | ;; it. Try it with your favourite one. GNU make, System V make, and |
| 536 | ;; Dennis Vadura's DMake have no problems. |
| 537 | ;; Set the variable `makefile-brave-make' to the name of the |
| 538 | ;; make utility that does this on your system. |
| 539 | ;; To understand what this is all about see the function definition |
| 540 | ;; of `makefile-query-by-make-minus-q' . |
| 541 | ;; ------------------------------------------------------------ |
| 542 | |
| 543 | (defcustom makefile-brave-make "make" |
| 544 | "*How to invoke make, for `makefile-query-targets'. |
| 545 | This should identify a `make' command that can handle the `-q' option." |
| 546 | :type 'string |
| 547 | :group 'makefile) |
| 548 | |
| 549 | (defcustom makefile-query-one-target-method-function |
| 550 | 'makefile-query-by-make-minus-q |
| 551 | "*Function to call to determine whether a make target is up to date. |
| 552 | The function must satisfy this calling convention: |
| 553 | |
| 554 | * As its first argument, it must accept the name of the target to |
| 555 | be checked, as a string. |
| 556 | |
| 557 | * As its second argument, it may accept the name of a makefile |
| 558 | as a string. Depending on what you're going to do you may |
| 559 | not need this. |
| 560 | |
| 561 | * It must return the integer value 0 (zero) if the given target |
| 562 | should be considered up-to-date in the context of the given |
| 563 | makefile, any nonzero integer value otherwise." |
| 564 | :type 'function |
| 565 | :group 'makefile) |
| 566 | (defvaralias 'makefile-query-one-target-method |
| 567 | 'makefile-query-one-target-method-function) |
| 568 | |
| 569 | (defcustom makefile-up-to-date-buffer-name "*Makefile Up-to-date overview*" |
| 570 | "*Name of the Up-to-date overview buffer." |
| 571 | :type 'string |
| 572 | :group 'makefile) |
| 573 | |
| 574 | ;;; --- end of up-to-date-overview configuration ------------------ |
| 575 | |
| 576 | (define-abbrev-table 'makefile-mode-abbrev-table () |
| 577 | "Abbrev table in use in Makefile buffers.") |
| 578 | |
| 579 | (defvar makefile-mode-map |
| 580 | (let ((map (make-sparse-keymap)) |
| 581 | (opt-map (make-sparse-keymap))) |
| 582 | ;; set up the keymap |
| 583 | (define-key map "\C-c:" 'makefile-insert-target-ref) |
| 584 | (if makefile-electric-keys |
| 585 | (progn |
| 586 | (define-key map "$" 'makefile-insert-macro-ref) |
| 587 | (define-key map ":" 'makefile-electric-colon) |
| 588 | (define-key map "=" 'makefile-electric-equal) |
| 589 | (define-key map "." 'makefile-electric-dot))) |
| 590 | (define-key map "\C-c\C-f" 'makefile-pickup-filenames-as-targets) |
| 591 | (define-key map "\C-c\C-b" 'makefile-switch-to-browser) |
| 592 | (define-key map "\C-c\C-c" 'comment-region) |
| 593 | (define-key map "\C-c\C-p" 'makefile-pickup-everything) |
| 594 | (define-key map "\C-c\C-u" 'makefile-create-up-to-date-overview) |
| 595 | (define-key map "\C-c\C-i" 'makefile-insert-gmake-function) |
| 596 | (define-key map "\C-c\C-\\" 'makefile-backslash-region) |
| 597 | (define-key map "\C-c\C-m\C-a" 'makefile-automake-mode) |
| 598 | (define-key map "\C-c\C-m\C-b" 'makefile-bsdmake-mode) |
| 599 | (define-key map "\C-c\C-m\C-g" 'makefile-gmake-mode) |
| 600 | (define-key map "\C-c\C-m\C-i" 'makefile-imake-mode) |
| 601 | (define-key map "\C-c\C-m\C-m" 'makefile-mode) |
| 602 | (define-key map "\C-c\C-m\C-p" 'makefile-makepp-mode) |
| 603 | (define-key map "\M-p" 'makefile-previous-dependency) |
| 604 | (define-key map "\M-n" 'makefile-next-dependency) |
| 605 | (define-key map "\e\t" 'makefile-complete) |
| 606 | |
| 607 | ;; Make menus. |
| 608 | (define-key map [menu-bar makefile-mode] |
| 609 | (cons "Makefile" (make-sparse-keymap "Makefile"))) |
| 610 | |
| 611 | (define-key map [menu-bar makefile-mode makefile-type] |
| 612 | (cons "Switch Makefile Type" opt-map)) |
| 613 | (define-key opt-map [makefile-makepp-mode] |
| 614 | '(menu-item "Makepp" makefile-makepp-mode |
| 615 | :help "An adapted `makefile-mode' that knows about makepp" |
| 616 | :button (:radio . (eq major-mode 'makefile-makepp-mode)))) |
| 617 | (define-key opt-map [makefile-imake-mode] |
| 618 | '(menu-item "Imake" makefile-imake-mode |
| 619 | :help "An adapted `makefile-mode' that knows about imake" |
| 620 | :button (:radio . (eq major-mode 'makefile-imake-mode)))) |
| 621 | (define-key opt-map [makefile-mode] |
| 622 | '(menu-item "Classic" makefile-mode |
| 623 | :help "`makefile-mode' with no special functionality" |
| 624 | :button (:radio . (eq major-mode 'makefile-mode)))) |
| 625 | (define-key opt-map [makefile-bsdmake-mode] |
| 626 | '(menu-item "BSD" makefile-bsdmake-mode |
| 627 | :help "An adapted `makefile-mode' that knows about BSD make" |
| 628 | :button (:radio . (eq major-mode 'makefile-bsdmake-mode)))) |
| 629 | (define-key opt-map [makefile-automake-mode] |
| 630 | '(menu-item "Automake" makefile-automake-mode |
| 631 | :help "An adapted `makefile-mode' that knows about automake" |
| 632 | :button (:radio . (eq major-mode 'makefile-automake-mode)))) |
| 633 | (define-key opt-map [makefile-gmake-mode] |
| 634 | '(menu-item "GNU make" makefile-gmake-mode |
| 635 | :help "An adapted `makefile-mode' that knows about GNU make" |
| 636 | :button (:radio . (eq major-mode 'makefile-gmake-mode)))) |
| 637 | (define-key map [menu-bar makefile-mode browse] |
| 638 | '(menu-item "Pop up Makefile Browser" makefile-switch-to-browser |
| 639 | ;; XXX: this needs a better string, the function is not documented... |
| 640 | :help "Pop up Makefile Browser")) |
| 641 | (define-key map [menu-bar makefile-mode overview] |
| 642 | '(menu-item "Up To Date Overview" makefile-create-up-to-date-overview |
| 643 | :help "Create a buffer containing an overview of the state of all known targets")) |
| 644 | ;; Target related |
| 645 | (define-key map [menu-bar makefile-mode separator1] '("----")) |
| 646 | (define-key map [menu-bar makefile-mode pickup-file] |
| 647 | '(menu-item "Pick File Name as Target" makefile-pickup-filenames-as-targets |
| 648 | :help "Scan the current directory for filenames to use as targets")) |
| 649 | (define-key map [menu-bar makefile-mode function] |
| 650 | '(menu-item "Insert GNU make function" makefile-insert-gmake-function |
| 651 | :help "Insert a GNU make function call")) |
| 652 | (define-key map [menu-bar makefile-mode pickup] |
| 653 | '(menu-item "Find Targets and Macros" makefile-pickup-everything |
| 654 | :help "Notice names of all macros and targets in Makefile")) |
| 655 | (define-key map [menu-bar makefile-mode complete] |
| 656 | '(menu-item "Complete Target or Macro" makefile-complete |
| 657 | :help "Perform completion on Makefile construct preceding point")) |
| 658 | (define-key map [menu-bar makefile-mode backslash] |
| 659 | '(menu-item "Backslash Region" makefile-backslash-region |
| 660 | :help "Insert, align, or delete end-of-line backslashes on the lines in the region")) |
| 661 | ;; Motion |
| 662 | (define-key map [menu-bar makefile-mode separator] '("----")) |
| 663 | (define-key map [menu-bar makefile-mode prev] |
| 664 | '(menu-item "Move to Previous Dependency" makefile-previous-dependency |
| 665 | :help "Move point to the beginning of the previous dependency line")) |
| 666 | (define-key map [menu-bar makefile-mode next] |
| 667 | '(menu-item "Move to Next Dependency" makefile-next-dependency |
| 668 | :help "Move point to the beginning of the next dependency line")) |
| 669 | map) |
| 670 | "The keymap that is used in Makefile mode.") |
| 671 | |
| 672 | |
| 673 | (defvar makefile-browser-map |
| 674 | (let ((map (make-sparse-keymap))) |
| 675 | (define-key map "n" 'makefile-browser-next-line) |
| 676 | (define-key map "\C-n" 'makefile-browser-next-line) |
| 677 | (define-key map "p" 'makefile-browser-previous-line) |
| 678 | (define-key map "\C-p" 'makefile-browser-previous-line) |
| 679 | (define-key map " " 'makefile-browser-toggle) |
| 680 | (define-key map "i" 'makefile-browser-insert-selection) |
| 681 | (define-key map "I" 'makefile-browser-insert-selection-and-quit) |
| 682 | (define-key map "\C-c\C-m" 'makefile-browser-insert-continuation) |
| 683 | (define-key map "q" 'makefile-browser-quit) |
| 684 | ;; disable horizontal movement |
| 685 | (define-key map "\C-b" 'undefined) |
| 686 | (define-key map "\C-f" 'undefined) |
| 687 | map) |
| 688 | "The keymap that is used in the macro- and target browser.") |
| 689 | |
| 690 | |
| 691 | (defvar makefile-mode-syntax-table |
| 692 | (let ((st (make-syntax-table))) |
| 693 | (modify-syntax-entry ?\( "() " st) |
| 694 | (modify-syntax-entry ?\) ")( " st) |
| 695 | (modify-syntax-entry ?\[ "(] " st) |
| 696 | (modify-syntax-entry ?\] ")[ " st) |
| 697 | (modify-syntax-entry ?\{ "(} " st) |
| 698 | (modify-syntax-entry ?\} "){ " st) |
| 699 | (modify-syntax-entry ?\' "\" " st) |
| 700 | (modify-syntax-entry ?\` "\" " st) |
| 701 | (modify-syntax-entry ?# "< " st) |
| 702 | (modify-syntax-entry ?\n "> " st) |
| 703 | st)) |
| 704 | |
| 705 | (defvar makefile-imake-mode-syntax-table |
| 706 | (let ((st (make-syntax-table makefile-mode-syntax-table))) |
| 707 | (modify-syntax-entry ?/ ". 14" st) |
| 708 | (modify-syntax-entry ?* ". 23" st) |
| 709 | (modify-syntax-entry ?# "'" st) |
| 710 | (modify-syntax-entry ?\n ". b" st) |
| 711 | st)) |
| 712 | |
| 713 | ;;; ------------------------------------------------------------ |
| 714 | ;;; Internal variables. |
| 715 | ;;; You don't need to configure below this line. |
| 716 | ;;; ------------------------------------------------------------ |
| 717 | |
| 718 | (defvar makefile-target-table nil |
| 719 | "Table of all target names known for this buffer.") |
| 720 | (put 'makefile-target-table 'risky-local-variable t) |
| 721 | |
| 722 | (defvar makefile-macro-table nil |
| 723 | "Table of all macro names known for this buffer.") |
| 724 | (put 'makefile-macro-table 'risky-local-variable t) |
| 725 | |
| 726 | (defvar makefile-browser-client |
| 727 | "A buffer in Makefile mode that is currently using the browser.") |
| 728 | |
| 729 | (defvar makefile-browser-selection-vector nil) |
| 730 | (defvar makefile-has-prereqs nil) |
| 731 | (defvar makefile-need-target-pickup t) |
| 732 | (defvar makefile-need-macro-pickup t) |
| 733 | |
| 734 | (defvar makefile-mode-hook '()) |
| 735 | |
| 736 | ;; Each element looks like '("GNU MAKE FUNCTION" "ARG" "ARG" ... ) |
| 737 | ;; Each "ARG" is used as a prompt for a required argument. |
| 738 | (defconst makefile-gnumake-functions-alist |
| 739 | '( |
| 740 | ;; Text functions |
| 741 | ("subst" "From" "To" "In") |
| 742 | ("patsubst" "Pattern" "Replacement" "In") |
| 743 | ("strip" "Text") |
| 744 | ("findstring" "Find what" "In") |
| 745 | ("filter" "Pattern" "Text") |
| 746 | ("filter-out" "Pattern" "Text") |
| 747 | ("sort" "List") |
| 748 | ;; Filename functions |
| 749 | ("dir" "Names") |
| 750 | ("notdir" "Names") |
| 751 | ("suffix" "Names") |
| 752 | ("basename" "Names") |
| 753 | ("addprefix" "Prefix" "Names") |
| 754 | ("addsuffix" "Suffix" "Names") |
| 755 | ("join" "List 1" "List 2") |
| 756 | ("word" "Index" "Text") |
| 757 | ("words" "Text") |
| 758 | ("firstword" "Text") |
| 759 | ("wildcard" "Pattern") |
| 760 | ;; Misc functions |
| 761 | ("foreach" "Variable" "List" "Text") |
| 762 | ("origin" "Variable") |
| 763 | ("shell" "Command"))) |
| 764 | |
| 765 | |
| 766 | ;;; ------------------------------------------------------------ |
| 767 | ;;; The mode function itself. |
| 768 | ;;; ------------------------------------------------------------ |
| 769 | |
| 770 | ;;;###autoload |
| 771 | (define-derived-mode makefile-mode nil "Makefile" |
| 772 | "Major mode for editing standard Makefiles. |
| 773 | |
| 774 | If you are editing a file for a different make, try one of the |
| 775 | variants `makefile-automake-mode', `makefile-gmake-mode', |
| 776 | `makefile-makepp-mode', `makefile-bsdmake-mode' or, |
| 777 | `makefile-imake-mode'. All but the last should be correctly |
| 778 | chosen based on the file name, except if it is *.mk. This |
| 779 | function ends by invoking the function(s) `makefile-mode-hook'. |
| 780 | |
| 781 | It is strongly recommended to use `font-lock-mode', because that |
| 782 | provides additional parsing information. This is used for |
| 783 | example to see that a rule action `echo foo: bar' is a not rule |
| 784 | dependency, despite the colon. |
| 785 | |
| 786 | \\{makefile-mode-map} |
| 787 | |
| 788 | In the browser, use the following keys: |
| 789 | |
| 790 | \\{makefile-browser-map} |
| 791 | |
| 792 | Makefile mode can be configured by modifying the following variables: |
| 793 | |
| 794 | `makefile-browser-buffer-name': |
| 795 | Name of the macro- and target browser buffer. |
| 796 | |
| 797 | `makefile-target-colon': |
| 798 | The string that gets appended to all target names |
| 799 | inserted by `makefile-insert-target'. |
| 800 | \":\" or \"::\" are quite common values. |
| 801 | |
| 802 | `makefile-macro-assign': |
| 803 | The string that gets appended to all macro names |
| 804 | inserted by `makefile-insert-macro'. |
| 805 | The normal value should be \" = \", since this is what |
| 806 | standard make expects. However, newer makes such as dmake |
| 807 | allow a larger variety of different macro assignments, so you |
| 808 | might prefer to use \" += \" or \" := \" . |
| 809 | |
| 810 | `makefile-tab-after-target-colon': |
| 811 | If you want a TAB (instead of a space) to be appended after the |
| 812 | target colon, then set this to a non-nil value. |
| 813 | |
| 814 | `makefile-browser-leftmost-column': |
| 815 | Number of blanks to the left of the browser selection mark. |
| 816 | |
| 817 | `makefile-browser-cursor-column': |
| 818 | Column in which the cursor is positioned when it moves |
| 819 | up or down in the browser. |
| 820 | |
| 821 | `makefile-browser-selected-mark': |
| 822 | String used to mark selected entries in the browser. |
| 823 | |
| 824 | `makefile-browser-unselected-mark': |
| 825 | String used to mark unselected entries in the browser. |
| 826 | |
| 827 | `makefile-browser-auto-advance-after-selection-p': |
| 828 | If this variable is set to a non-nil value the cursor |
| 829 | will automagically advance to the next line after an item |
| 830 | has been selected in the browser. |
| 831 | |
| 832 | `makefile-pickup-everything-picks-up-filenames-p': |
| 833 | If this variable is set to a non-nil value then |
| 834 | `makefile-pickup-everything' also picks up filenames as targets |
| 835 | (i.e. it calls `makefile-pickup-filenames-as-targets'), otherwise |
| 836 | filenames are omitted. |
| 837 | |
| 838 | `makefile-cleanup-continuations': |
| 839 | If this variable is set to a non-nil value then Makefile mode |
| 840 | will assure that no line in the file ends with a backslash |
| 841 | (the continuation character) followed by any whitespace. |
| 842 | This is done by silently removing the trailing whitespace, leaving |
| 843 | the backslash itself intact. |
| 844 | IMPORTANT: Please note that enabling this option causes Makefile mode |
| 845 | to MODIFY A FILE WITHOUT YOUR CONFIRMATION when \"it seems necessary\". |
| 846 | |
| 847 | `makefile-browser-hook': |
| 848 | A function or list of functions to be called just before the |
| 849 | browser is entered. This is executed in the makefile buffer. |
| 850 | |
| 851 | `makefile-special-targets-list': |
| 852 | List of special targets. You will be offered to complete |
| 853 | on one of those in the minibuffer whenever you enter a `.'. |
| 854 | at the beginning of a line in Makefile mode." |
| 855 | (add-hook 'write-file-functions |
| 856 | 'makefile-warn-suspicious-lines nil t) |
| 857 | (add-hook 'write-file-functions |
| 858 | 'makefile-warn-continuations nil t) |
| 859 | (add-hook 'write-file-functions |
| 860 | 'makefile-cleanup-continuations nil t) |
| 861 | (make-local-variable 'makefile-target-table) |
| 862 | (make-local-variable 'makefile-macro-table) |
| 863 | (make-local-variable 'makefile-has-prereqs) |
| 864 | (make-local-variable 'makefile-need-target-pickup) |
| 865 | (make-local-variable 'makefile-need-macro-pickup) |
| 866 | |
| 867 | ;; Font lock. |
| 868 | (set (make-local-variable 'font-lock-defaults) |
| 869 | ;; SYNTAX-BEGIN set to backward-paragraph to avoid slow-down |
| 870 | ;; near the end of a large buffer, due to parse-partial-sexp's |
| 871 | ;; trying to parse all the way till the beginning of buffer. |
| 872 | '(makefile-font-lock-keywords |
| 873 | nil nil |
| 874 | ((?$ . ".")) |
| 875 | backward-paragraph |
| 876 | (font-lock-syntactic-keywords |
| 877 | . makefile-font-lock-syntactic-keywords))) |
| 878 | |
| 879 | ;; Add-log. |
| 880 | (set (make-local-variable 'add-log-current-defun-function) |
| 881 | 'makefile-add-log-defun) |
| 882 | |
| 883 | ;; Imenu. |
| 884 | (set (make-local-variable 'imenu-generic-expression) |
| 885 | makefile-imenu-generic-expression) |
| 886 | |
| 887 | ;; Dabbrev. |
| 888 | (set (make-local-variable 'dabbrev-abbrev-skip-leading-regexp) "\\$") |
| 889 | |
| 890 | ;; Other abbrevs. |
| 891 | (setq local-abbrev-table makefile-mode-abbrev-table) |
| 892 | |
| 893 | ;; Filling. |
| 894 | (set (make-local-variable 'fill-paragraph-function) 'makefile-fill-paragraph) |
| 895 | |
| 896 | ;; Comment stuff. |
| 897 | (set (make-local-variable 'comment-start) "#") |
| 898 | (set (make-local-variable 'comment-end) "") |
| 899 | (set (make-local-variable 'comment-start-skip) "#+[ \t]*") |
| 900 | |
| 901 | ;; Make sure TAB really inserts \t. |
| 902 | (set (make-local-variable 'indent-line-function) 'indent-to-left-margin) |
| 903 | |
| 904 | ;; Real TABs are important in makefiles |
| 905 | (setq indent-tabs-mode t)) |
| 906 | |
| 907 | ;; These should do more than just differentiate font-lock. |
| 908 | ;;;###autoload |
| 909 | (define-derived-mode makefile-automake-mode makefile-mode "Makefile.am" |
| 910 | "An adapted `makefile-mode' that knows about automake." |
| 911 | (setq font-lock-defaults |
| 912 | `(makefile-automake-font-lock-keywords ,@(cdr font-lock-defaults)))) |
| 913 | |
| 914 | ;;;###autoload |
| 915 | (define-derived-mode makefile-gmake-mode makefile-mode "GNUmakefile" |
| 916 | "An adapted `makefile-mode' that knows about gmake." |
| 917 | (setq font-lock-defaults |
| 918 | `(makefile-gmake-font-lock-keywords ,@(cdr font-lock-defaults)))) |
| 919 | |
| 920 | ;;;###autoload |
| 921 | (define-derived-mode makefile-makepp-mode makefile-mode "Makeppfile" |
| 922 | "An adapted `makefile-mode' that knows about makepp." |
| 923 | (set (make-local-variable 'makefile-rule-action-regex) |
| 924 | makefile-makepp-rule-action-regex) |
| 925 | (setq font-lock-defaults |
| 926 | `(makefile-makepp-font-lock-keywords ,@(cdr font-lock-defaults)) |
| 927 | imenu-generic-expression |
| 928 | `(("Functions" "^[ \t]*\\(?:make\\)?sub[ \t]+\\([A-Za-z0-9_]+\\)" 1) |
| 929 | ,@imenu-generic-expression))) |
| 930 | |
| 931 | ;;;###autoload |
| 932 | (define-derived-mode makefile-bsdmake-mode makefile-mode "BSDmakefile" |
| 933 | "An adapted `makefile-mode' that knows about BSD make." |
| 934 | (set (make-local-variable 'makefile-dependency-regex) |
| 935 | makefile-bsdmake-dependency-regex) |
| 936 | (set (make-local-variable 'makefile-dependency-skip) "^:!") |
| 937 | (set (make-local-variable 'makefile-rule-action-regex) |
| 938 | makefile-bsdmake-rule-action-regex) |
| 939 | (setq font-lock-defaults |
| 940 | `(makefile-bsdmake-font-lock-keywords ,@(cdr font-lock-defaults)))) |
| 941 | |
| 942 | ;;;###autoload |
| 943 | (define-derived-mode makefile-imake-mode makefile-mode "Imakefile" |
| 944 | "An adapted `makefile-mode' that knows about imake." |
| 945 | :syntax-table makefile-imake-mode-syntax-table |
| 946 | (let ((base `(makefile-imake-font-lock-keywords ,@(cdr font-lock-defaults))) |
| 947 | new) |
| 948 | ;; Remove `font-lock-syntactic-keywords' entry from font-lock-defaults. |
| 949 | (mapc (lambda (elt) |
| 950 | (unless (and (consp elt) |
| 951 | (eq (car elt) 'font-lock-syntactic-keywords)) |
| 952 | (setq new (cons elt new)))) |
| 953 | base) |
| 954 | (setq font-lock-defaults (nreverse new)))) |
| 955 | |
| 956 | \f |
| 957 | |
| 958 | ;;; Motion code. |
| 959 | |
| 960 | (defun makefile-next-dependency () |
| 961 | "Move point to the beginning of the next dependency line." |
| 962 | (interactive) |
| 963 | (let ((here (point))) |
| 964 | (end-of-line) |
| 965 | (if (makefile-match-dependency nil) |
| 966 | (progn (beginning-of-line) t) ; indicate success |
| 967 | (goto-char here) nil))) |
| 968 | |
| 969 | (defun makefile-previous-dependency () |
| 970 | "Move point to the beginning of the previous dependency line." |
| 971 | (interactive) |
| 972 | (let ((pt (point))) |
| 973 | (beginning-of-line) |
| 974 | ;; makefile-match-dependency done backwards: |
| 975 | (catch 'found |
| 976 | (while (progn (skip-chars-backward makefile-dependency-skip) |
| 977 | (not (bobp))) |
| 978 | (or (prog1 (eq (char-after) ?=) |
| 979 | (backward-char)) |
| 980 | (get-text-property (point) 'face) |
| 981 | (beginning-of-line) |
| 982 | (if (> (point) (+ (point-min) 2)) |
| 983 | (eq (char-before (1- (point))) ?\\)) |
| 984 | (if (looking-at makefile-dependency-regex) |
| 985 | (throw 'found t)))) |
| 986 | (goto-char pt) |
| 987 | nil))) |
| 988 | |
| 989 | \f |
| 990 | |
| 991 | ;;; Electric keys. Blech. |
| 992 | |
| 993 | (defun makefile-electric-dot (arg) |
| 994 | "Prompt for the name of a special target to insert. |
| 995 | Only does electric insertion at beginning of line. |
| 996 | Anywhere else just self-inserts." |
| 997 | (interactive "p") |
| 998 | (if (bolp) |
| 999 | (makefile-insert-special-target) |
| 1000 | (self-insert-command arg))) |
| 1001 | |
| 1002 | (defun makefile-insert-special-target () |
| 1003 | "Prompt for and insert a special target name. |
| 1004 | Uses `makefile-special-targets' list." |
| 1005 | (interactive) |
| 1006 | (makefile-pickup-targets) |
| 1007 | (let ((special-target |
| 1008 | (completing-read "Special target: " |
| 1009 | makefile-special-targets-list nil nil nil))) |
| 1010 | (if (zerop (length special-target)) |
| 1011 | () |
| 1012 | (insert "." special-target ":") |
| 1013 | (makefile-forward-after-target-colon)))) |
| 1014 | |
| 1015 | (defun makefile-electric-equal (arg) |
| 1016 | "Prompt for name of a macro to insert. |
| 1017 | Only does prompting if point is at beginning of line. |
| 1018 | Anywhere else just self-inserts." |
| 1019 | (interactive "p") |
| 1020 | (makefile-pickup-macros) |
| 1021 | (if (bolp) |
| 1022 | (call-interactively 'makefile-insert-macro) |
| 1023 | (self-insert-command arg))) |
| 1024 | |
| 1025 | (defun makefile-insert-macro (macro-name) |
| 1026 | "Prepare definition of a new macro." |
| 1027 | (interactive "sMacro Name: ") |
| 1028 | (makefile-pickup-macros) |
| 1029 | (if (not (zerop (length macro-name))) |
| 1030 | (progn |
| 1031 | (beginning-of-line) |
| 1032 | (insert macro-name makefile-macro-assign) |
| 1033 | (setq makefile-need-macro-pickup t) |
| 1034 | (makefile-remember-macro macro-name)))) |
| 1035 | |
| 1036 | (defun makefile-insert-macro-ref (macro-name) |
| 1037 | "Complete on a list of known macros, then insert complete ref at point." |
| 1038 | (interactive |
| 1039 | (list |
| 1040 | (progn |
| 1041 | (makefile-pickup-macros) |
| 1042 | (completing-read "Refer to macro: " makefile-macro-table nil nil nil)))) |
| 1043 | (makefile-do-macro-insertion macro-name)) |
| 1044 | |
| 1045 | (defun makefile-insert-target (target-name) |
| 1046 | "Prepare definition of a new target (dependency line)." |
| 1047 | (interactive "sTarget: ") |
| 1048 | (if (not (zerop (length target-name))) |
| 1049 | (progn |
| 1050 | (beginning-of-line) |
| 1051 | (insert target-name makefile-target-colon) |
| 1052 | (makefile-forward-after-target-colon) |
| 1053 | (end-of-line) |
| 1054 | (setq makefile-need-target-pickup t) |
| 1055 | (makefile-remember-target target-name)))) |
| 1056 | |
| 1057 | (defun makefile-insert-target-ref (target-name) |
| 1058 | "Complete on a list of known targets, then insert TARGET-NAME at point." |
| 1059 | (interactive |
| 1060 | (list |
| 1061 | (progn |
| 1062 | (makefile-pickup-targets) |
| 1063 | (completing-read "Refer to target: " makefile-target-table nil nil nil)))) |
| 1064 | (if (not (zerop (length target-name))) |
| 1065 | (insert target-name " "))) |
| 1066 | |
| 1067 | (defun makefile-electric-colon (arg) |
| 1068 | "Prompt for name of new target. |
| 1069 | Prompting only happens at beginning of line. |
| 1070 | Anywhere else just self-inserts." |
| 1071 | (interactive "p") |
| 1072 | (if (bolp) |
| 1073 | (call-interactively 'makefile-insert-target) |
| 1074 | (self-insert-command arg))) |
| 1075 | |
| 1076 | \f |
| 1077 | |
| 1078 | ;;; ------------------------------------------------------------ |
| 1079 | ;;; Extracting targets and macros from an existing makefile |
| 1080 | ;;; ------------------------------------------------------------ |
| 1081 | |
| 1082 | (defun makefile-pickup-targets () |
| 1083 | "Notice names of all target definitions in Makefile." |
| 1084 | (interactive) |
| 1085 | (when makefile-need-target-pickup |
| 1086 | (setq makefile-need-target-pickup nil |
| 1087 | makefile-target-table nil |
| 1088 | makefile-has-prereqs nil) |
| 1089 | (save-excursion |
| 1090 | (goto-char (point-min)) |
| 1091 | (while (makefile-match-dependency nil) |
| 1092 | (goto-char (match-beginning 1)) |
| 1093 | (while (let ((target-name |
| 1094 | (buffer-substring-no-properties (point) |
| 1095 | (progn |
| 1096 | (skip-chars-forward "^ \t:#") |
| 1097 | (point)))) |
| 1098 | (has-prereqs |
| 1099 | (not (looking-at ":[ \t]*$")))) |
| 1100 | (if (makefile-remember-target target-name has-prereqs) |
| 1101 | (message "Picked up target \"%s\" from line %d" |
| 1102 | target-name (line-number-at-pos))) |
| 1103 | (skip-chars-forward " \t") |
| 1104 | (not (or (eolp) (eq (char-after) ?:))))) |
| 1105 | (forward-line))) |
| 1106 | (message "Read targets OK."))) |
| 1107 | |
| 1108 | (defun makefile-pickup-macros () |
| 1109 | "Notice names of all macro definitions in Makefile." |
| 1110 | (interactive) |
| 1111 | (when makefile-need-macro-pickup |
| 1112 | (setq makefile-need-macro-pickup nil |
| 1113 | makefile-macro-table nil) |
| 1114 | (save-excursion |
| 1115 | (goto-char (point-min)) |
| 1116 | (while (re-search-forward makefile-macroassign-regex nil t) |
| 1117 | (goto-char (match-beginning 1)) |
| 1118 | (let ((macro-name (buffer-substring-no-properties (point) |
| 1119 | (progn |
| 1120 | (skip-chars-forward "^ \t:#=*") |
| 1121 | (point))))) |
| 1122 | (if (makefile-remember-macro macro-name) |
| 1123 | (message "Picked up macro \"%s\" from line %d" |
| 1124 | macro-name (line-number-at-pos)))) |
| 1125 | (forward-line))) |
| 1126 | (message "Read macros OK."))) |
| 1127 | |
| 1128 | (defun makefile-pickup-everything (arg) |
| 1129 | "Notice names of all macros and targets in Makefile. |
| 1130 | Prefix arg means force pickups to be redone." |
| 1131 | (interactive "P") |
| 1132 | (if arg |
| 1133 | (setq makefile-need-target-pickup t |
| 1134 | makefile-need-macro-pickup t)) |
| 1135 | (makefile-pickup-macros) |
| 1136 | (makefile-pickup-targets) |
| 1137 | (if makefile-pickup-everything-picks-up-filenames-p |
| 1138 | (makefile-pickup-filenames-as-targets))) |
| 1139 | |
| 1140 | (defun makefile-pickup-filenames-as-targets () |
| 1141 | "Scan the current directory for filenames to use as targets. |
| 1142 | Checks each filename against `makefile-ignored-files-in-pickup-regex' |
| 1143 | and adds all qualifying names to the list of known targets." |
| 1144 | (interactive) |
| 1145 | (mapc (lambda (name) |
| 1146 | (or (file-directory-p name) |
| 1147 | (string-match makefile-ignored-files-in-pickup-regex name) |
| 1148 | (if (makefile-remember-target name) |
| 1149 | (message "Picked up file \"%s\" as target" name)))) |
| 1150 | (file-name-all-completions "" (or (file-name-directory (buffer-file-name)) "")))) |
| 1151 | |
| 1152 | \f |
| 1153 | |
| 1154 | ;;; Completion. |
| 1155 | |
| 1156 | (defun makefile-complete () |
| 1157 | "Perform completion on Makefile construct preceding point. |
| 1158 | Can complete variable and target names. |
| 1159 | The context determines which are considered." |
| 1160 | (interactive) |
| 1161 | (let* ((beg (save-excursion |
| 1162 | (skip-chars-backward "^$(){}:#= \t\n") |
| 1163 | (point))) |
| 1164 | (try (buffer-substring beg (point))) |
| 1165 | (paren nil) |
| 1166 | (do-macros |
| 1167 | (save-excursion |
| 1168 | (goto-char beg) |
| 1169 | (let ((pc (preceding-char))) |
| 1170 | (cond |
| 1171 | ;; Preceding "$" means macros only. |
| 1172 | ((= pc ?$) |
| 1173 | t) |
| 1174 | |
| 1175 | ;; Preceding "$(" or "${" means macros only. |
| 1176 | ((and (memq pc '(?\{ ?\()) |
| 1177 | (progn |
| 1178 | (setq paren (if (eq paren ?\{) ?\} ?\))) |
| 1179 | (backward-char) |
| 1180 | (= (preceding-char) ?$))) |
| 1181 | t))))) |
| 1182 | |
| 1183 | (table (apply-partially 'completion-table-with-terminator |
| 1184 | (cond |
| 1185 | (do-macros (or paren "")) |
| 1186 | ((save-excursion (goto-char beg) (bolp)) ":") |
| 1187 | (t " ")) |
| 1188 | (append (if do-macros |
| 1189 | '() |
| 1190 | makefile-target-table) |
| 1191 | makefile-macro-table)))) |
| 1192 | (completion-in-region beg (point) table))) |
| 1193 | |
| 1194 | \f |
| 1195 | |
| 1196 | ;; Backslashification. Stolen from cc-mode.el. |
| 1197 | |
| 1198 | (defun makefile-backslash-region (from to delete-flag) |
| 1199 | "Insert, align, or delete end-of-line backslashes on the lines in the region. |
| 1200 | With no argument, inserts backslashes and aligns existing backslashes. |
| 1201 | With an argument, deletes the backslashes. |
| 1202 | |
| 1203 | This function does not modify the last line of the region if the region ends |
| 1204 | right at the start of the following line; it does not modify blank lines |
| 1205 | at the start of the region. So you can put the region around an entire macro |
| 1206 | definition and conveniently use this command." |
| 1207 | (interactive "r\nP") |
| 1208 | (save-excursion |
| 1209 | (goto-char from) |
| 1210 | (let ((column makefile-backslash-column) |
| 1211 | (endmark (make-marker))) |
| 1212 | (move-marker endmark to) |
| 1213 | ;; Compute the smallest column number past the ends of all the lines. |
| 1214 | (if makefile-backslash-align |
| 1215 | (progn |
| 1216 | (if (not delete-flag) |
| 1217 | (while (< (point) to) |
| 1218 | (end-of-line) |
| 1219 | (if (= (preceding-char) ?\\) |
| 1220 | (progn (forward-char -1) |
| 1221 | (skip-chars-backward " \t"))) |
| 1222 | (setq column (max column (1+ (current-column)))) |
| 1223 | (forward-line 1))) |
| 1224 | ;; Adjust upward to a tab column, if that doesn't push |
| 1225 | ;; past the margin. |
| 1226 | (if (> (% column tab-width) 0) |
| 1227 | (let ((adjusted (* (/ (+ column tab-width -1) tab-width) |
| 1228 | tab-width))) |
| 1229 | (if (< adjusted (window-width)) |
| 1230 | (setq column adjusted)))))) |
| 1231 | ;; Don't modify blank lines at start of region. |
| 1232 | (goto-char from) |
| 1233 | (while (and (< (point) endmark) (eolp)) |
| 1234 | (forward-line 1)) |
| 1235 | ;; Add or remove backslashes on all the lines. |
| 1236 | (while (and (< (point) endmark) |
| 1237 | ;; Don't backslashify the last line |
| 1238 | ;; if the region ends right at the start of the next line. |
| 1239 | (save-excursion |
| 1240 | (forward-line 1) |
| 1241 | (< (point) endmark))) |
| 1242 | (if (not delete-flag) |
| 1243 | (makefile-append-backslash column) |
| 1244 | (makefile-delete-backslash)) |
| 1245 | (forward-line 1)) |
| 1246 | (move-marker endmark nil)))) |
| 1247 | |
| 1248 | (defun makefile-append-backslash (column) |
| 1249 | (end-of-line) |
| 1250 | ;; Note that "\\\\" is needed to get one backslash. |
| 1251 | (if (= (preceding-char) ?\\) |
| 1252 | (progn (forward-char -1) |
| 1253 | (delete-horizontal-space) |
| 1254 | (indent-to column (if makefile-backslash-align nil 1))) |
| 1255 | (indent-to column (if makefile-backslash-align nil 1)) |
| 1256 | (insert "\\"))) |
| 1257 | |
| 1258 | (defun makefile-delete-backslash () |
| 1259 | (end-of-line) |
| 1260 | (or (bolp) |
| 1261 | (progn |
| 1262 | (forward-char -1) |
| 1263 | (if (looking-at "\\\\") |
| 1264 | (delete-region (1+ (point)) |
| 1265 | (progn (skip-chars-backward " \t") (point))))))) |
| 1266 | |
| 1267 | \f |
| 1268 | |
| 1269 | ;; Filling |
| 1270 | |
| 1271 | (defun makefile-fill-paragraph (arg) |
| 1272 | ;; Fill comments, backslashed lines, and variable definitions |
| 1273 | ;; specially. |
| 1274 | (save-excursion |
| 1275 | (beginning-of-line) |
| 1276 | (cond |
| 1277 | ((looking-at "^[ \t]*#+\\s-*") |
| 1278 | ;; Found a comment. Return nil to let normal filling take place. |
| 1279 | nil) |
| 1280 | |
| 1281 | ;; Must look for backslashed-region before looking for variable |
| 1282 | ;; assignment. |
| 1283 | ((or (eq (char-before (line-end-position 1)) ?\\) |
| 1284 | (eq (char-before (line-end-position 0)) ?\\)) |
| 1285 | ;; A backslash region. Find beginning and end, remove |
| 1286 | ;; backslashes, fill, and then reapply backslahes. |
| 1287 | (end-of-line) |
| 1288 | (let ((beginning |
| 1289 | (save-excursion |
| 1290 | (end-of-line 0) |
| 1291 | (while (= (preceding-char) ?\\) |
| 1292 | (end-of-line 0)) |
| 1293 | (forward-char) |
| 1294 | (point))) |
| 1295 | (end |
| 1296 | (save-excursion |
| 1297 | (while (= (preceding-char) ?\\) |
| 1298 | (end-of-line 2)) |
| 1299 | (point)))) |
| 1300 | (save-restriction |
| 1301 | (narrow-to-region beginning end) |
| 1302 | (makefile-backslash-region (point-min) (point-max) t) |
| 1303 | (let ((fill-paragraph-function nil)) |
| 1304 | (fill-paragraph nil)) |
| 1305 | (makefile-backslash-region (point-min) (point-max) nil) |
| 1306 | (goto-char (point-max)) |
| 1307 | (if (< (skip-chars-backward "\n") 0) |
| 1308 | (delete-region (point) (point-max))))) |
| 1309 | ;; Return non-nil to indicate it's been filled. |
| 1310 | t) |
| 1311 | |
| 1312 | ((looking-at makefile-macroassign-regex) |
| 1313 | ;; Have a macro assign. Fill just this line, and then backslash |
| 1314 | ;; resulting region. |
| 1315 | (save-restriction |
| 1316 | (narrow-to-region (point) (line-beginning-position 2)) |
| 1317 | (let ((fill-paragraph-function nil)) |
| 1318 | (fill-paragraph nil)) |
| 1319 | (makefile-backslash-region (point-min) (point-max) nil)) |
| 1320 | ;; Return non-nil to indicate it's been filled. |
| 1321 | t) |
| 1322 | |
| 1323 | (t |
| 1324 | ;; Return non-nil so we don't fill anything else. |
| 1325 | t)))) |
| 1326 | |
| 1327 | \f |
| 1328 | |
| 1329 | ;;; ------------------------------------------------------------ |
| 1330 | ;;; Browser mode. |
| 1331 | ;;; ------------------------------------------------------------ |
| 1332 | |
| 1333 | (defun makefile-browser-format-target-line (target selected) |
| 1334 | (format |
| 1335 | (concat (make-string makefile-browser-leftmost-column ?\ ) |
| 1336 | (if selected |
| 1337 | makefile-browser-selected-mark |
| 1338 | makefile-browser-unselected-mark) |
| 1339 | "%s%s") |
| 1340 | target makefile-target-colon)) |
| 1341 | |
| 1342 | (defun makefile-browser-format-macro-line (macro selected) |
| 1343 | (format |
| 1344 | (concat (make-string makefile-browser-leftmost-column ?\ ) |
| 1345 | (if selected |
| 1346 | makefile-browser-selected-mark |
| 1347 | makefile-browser-unselected-mark) |
| 1348 | (makefile-format-macro-ref macro)))) |
| 1349 | |
| 1350 | (defun makefile-browser-fill (targets macros) |
| 1351 | (let ((inhibit-read-only t)) |
| 1352 | (goto-char (point-min)) |
| 1353 | (erase-buffer) |
| 1354 | (mapconcat |
| 1355 | (function |
| 1356 | (lambda (item) (insert (makefile-browser-format-target-line (car item) nil) "\n"))) |
| 1357 | targets |
| 1358 | "") |
| 1359 | (mapconcat |
| 1360 | (function |
| 1361 | (lambda (item) (insert (makefile-browser-format-macro-line (car item) nil) "\n"))) |
| 1362 | macros |
| 1363 | "") |
| 1364 | (sort-lines nil (point-min) (point-max)) |
| 1365 | (goto-char (1- (point-max))) |
| 1366 | (delete-char 1) ; remove unnecessary newline at eob |
| 1367 | (goto-char (point-min)) |
| 1368 | (forward-char makefile-browser-cursor-column))) |
| 1369 | |
| 1370 | ;;; |
| 1371 | ;;; Moving up and down in the browser |
| 1372 | ;;; |
| 1373 | |
| 1374 | (defun makefile-browser-next-line () |
| 1375 | "Move the browser selection cursor to the next line." |
| 1376 | (interactive) |
| 1377 | (if (not (makefile-last-line-p)) |
| 1378 | (progn |
| 1379 | (forward-line 1) |
| 1380 | (forward-char makefile-browser-cursor-column)))) |
| 1381 | |
| 1382 | (defun makefile-browser-previous-line () |
| 1383 | "Move the browser selection cursor to the previous line." |
| 1384 | (interactive) |
| 1385 | (if (not (makefile-first-line-p)) |
| 1386 | (progn |
| 1387 | (forward-line -1) |
| 1388 | (forward-char makefile-browser-cursor-column)))) |
| 1389 | |
| 1390 | ;;; |
| 1391 | ;;; Quitting the browser (returns to client buffer) |
| 1392 | ;;; |
| 1393 | |
| 1394 | (defun makefile-browser-quit () |
| 1395 | "Leave the browser and return to the makefile buffer." |
| 1396 | (interactive) |
| 1397 | (let ((my-client makefile-browser-client)) |
| 1398 | (setq makefile-browser-client nil) ; we quitted, so NO client! |
| 1399 | (set-buffer-modified-p nil) |
| 1400 | (quit-window t) |
| 1401 | (pop-to-buffer my-client))) |
| 1402 | |
| 1403 | ;;; |
| 1404 | ;;; Toggle state of a browser item |
| 1405 | ;;; |
| 1406 | |
| 1407 | (defun makefile-browser-toggle () |
| 1408 | "Toggle the selection state of the browser item at the cursor position." |
| 1409 | (interactive) |
| 1410 | (let ((this-line (count-lines (point-min) (point)))) |
| 1411 | (setq this-line (max 1 this-line)) |
| 1412 | (makefile-browser-toggle-state-for-line this-line) |
| 1413 | (goto-char (point-min)) |
| 1414 | (forward-line (1- this-line)) |
| 1415 | (let ((inhibit-read-only t)) |
| 1416 | (beginning-of-line) ; redundant? |
| 1417 | (if (makefile-browser-on-macro-line-p) |
| 1418 | (let ((macro-name (makefile-browser-this-line-macro-name))) |
| 1419 | (delete-region (point) (progn (end-of-line) (point))) |
| 1420 | (insert |
| 1421 | (makefile-browser-format-macro-line |
| 1422 | macro-name |
| 1423 | (makefile-browser-get-state-for-line this-line)))) |
| 1424 | (let ((target-name (makefile-browser-this-line-target-name))) |
| 1425 | (delete-region (point) (progn (end-of-line) (point))) |
| 1426 | (insert |
| 1427 | (makefile-browser-format-target-line |
| 1428 | target-name |
| 1429 | (makefile-browser-get-state-for-line this-line)))))) |
| 1430 | (beginning-of-line) |
| 1431 | (forward-char makefile-browser-cursor-column) |
| 1432 | (if makefile-browser-auto-advance-after-selection-p |
| 1433 | (makefile-browser-next-line)))) |
| 1434 | |
| 1435 | ;;; |
| 1436 | ;;; Making insertions into the client buffer |
| 1437 | ;;; |
| 1438 | |
| 1439 | (defun makefile-browser-insert-continuation () |
| 1440 | "Insert a makefile continuation. |
| 1441 | In the makefile buffer, go to (end-of-line), insert a \'\\\' |
| 1442 | character, insert a new blank line, go to that line and indent by one TAB. |
| 1443 | This is most useful in the process of creating continued lines when copying |
| 1444 | large dependencies from the browser to the client buffer. |
| 1445 | \(point) advances accordingly in the client buffer." |
| 1446 | (interactive) |
| 1447 | (with-current-buffer makefile-browser-client |
| 1448 | (end-of-line) |
| 1449 | (insert "\\\n\t"))) |
| 1450 | |
| 1451 | (defun makefile-browser-insert-selection () |
| 1452 | "Insert all selected targets and/or macros in the makefile buffer. |
| 1453 | Insertion takes place at point." |
| 1454 | (interactive) |
| 1455 | (save-excursion |
| 1456 | (goto-char (point-min)) |
| 1457 | (let ((current-line 1)) |
| 1458 | (while (not (eobp)) |
| 1459 | (if (makefile-browser-get-state-for-line current-line) |
| 1460 | (makefile-browser-send-this-line-item)) |
| 1461 | (forward-line 1) |
| 1462 | (setq current-line (1+ current-line)))))) |
| 1463 | |
| 1464 | (defun makefile-browser-insert-selection-and-quit () |
| 1465 | (interactive) |
| 1466 | (makefile-browser-insert-selection) |
| 1467 | (makefile-browser-quit)) |
| 1468 | |
| 1469 | (defun makefile-browser-send-this-line-item () |
| 1470 | (if (makefile-browser-on-macro-line-p) |
| 1471 | (save-excursion |
| 1472 | (let ((macro-name (makefile-browser-this-line-macro-name))) |
| 1473 | (set-buffer makefile-browser-client) |
| 1474 | (insert (makefile-format-macro-ref macro-name) " "))) |
| 1475 | (save-excursion |
| 1476 | (let ((target-name (makefile-browser-this-line-target-name))) |
| 1477 | (set-buffer makefile-browser-client) |
| 1478 | (insert target-name " "))))) |
| 1479 | |
| 1480 | (defun makefile-browser-start-interaction () |
| 1481 | (use-local-map makefile-browser-map) |
| 1482 | (setq buffer-read-only t)) |
| 1483 | |
| 1484 | (defun makefile-browse (targets macros) |
| 1485 | (interactive) |
| 1486 | (if (zerop (+ (length targets) (length macros))) |
| 1487 | (progn |
| 1488 | (beep) |
| 1489 | (message "No macros or targets to browse! Consider running 'makefile-pickup-everything\'")) |
| 1490 | (let ((browser-buffer (get-buffer-create makefile-browser-buffer-name))) |
| 1491 | (pop-to-buffer browser-buffer) |
| 1492 | (makefile-browser-fill targets macros) |
| 1493 | (shrink-window-if-larger-than-buffer) |
| 1494 | (set (make-local-variable 'makefile-browser-selection-vector) |
| 1495 | (make-vector (+ (length targets) (length macros)) nil)) |
| 1496 | (makefile-browser-start-interaction)))) |
| 1497 | |
| 1498 | (defun makefile-switch-to-browser () |
| 1499 | (interactive) |
| 1500 | (run-hooks 'makefile-browser-hook) |
| 1501 | (setq makefile-browser-client (current-buffer)) |
| 1502 | (makefile-pickup-targets) |
| 1503 | (makefile-pickup-macros) |
| 1504 | (makefile-browse makefile-target-table makefile-macro-table)) |
| 1505 | |
| 1506 | \f |
| 1507 | |
| 1508 | ;;; ------------------------------------------------------------ |
| 1509 | ;;; Up-to-date overview buffer |
| 1510 | ;;; ------------------------------------------------------------ |
| 1511 | |
| 1512 | (defun makefile-create-up-to-date-overview () |
| 1513 | "Create a buffer containing an overview of the state of all known targets. |
| 1514 | Known targets are targets that are explicitly defined in that makefile; |
| 1515 | in other words, all targets that appear on the left hand side of a |
| 1516 | dependency in the makefile." |
| 1517 | (interactive) |
| 1518 | (if (y-or-n-p "Are you sure that the makefile being edited is consistent? ") |
| 1519 | ;; |
| 1520 | ;; The rest of this function operates on a temporary makefile, created by |
| 1521 | ;; writing the current contents of the makefile buffer. |
| 1522 | ;; |
| 1523 | (let ((saved-target-table makefile-target-table) |
| 1524 | (this-buffer (current-buffer)) |
| 1525 | (makefile-up-to-date-buffer |
| 1526 | (get-buffer-create makefile-up-to-date-buffer-name)) |
| 1527 | (filename (makefile-save-temporary)) |
| 1528 | ;; |
| 1529 | ;; Forget the target table because it may contain picked-up filenames |
| 1530 | ;; that are not really targets in the current makefile. |
| 1531 | ;; We don't want to query these, so get a new target-table with just the |
| 1532 | ;; targets that can be found in the makefile buffer. |
| 1533 | ;; The 'old' target table will be restored later. |
| 1534 | ;; |
| 1535 | (real-targets (progn |
| 1536 | (makefile-pickup-targets) |
| 1537 | makefile-target-table)) |
| 1538 | (prereqs makefile-has-prereqs) |
| 1539 | ) |
| 1540 | |
| 1541 | (set-buffer makefile-up-to-date-buffer) |
| 1542 | (setq buffer-read-only nil) |
| 1543 | (erase-buffer) |
| 1544 | (makefile-query-targets filename real-targets prereqs) |
| 1545 | (if (zerop (buffer-size)) ; if it did not get us anything |
| 1546 | (progn |
| 1547 | (kill-buffer (current-buffer)) |
| 1548 | (message "No overview created!"))) |
| 1549 | (set-buffer this-buffer) |
| 1550 | (setq makefile-target-table saved-target-table) |
| 1551 | (if (get-buffer makefile-up-to-date-buffer-name) |
| 1552 | (progn |
| 1553 | (pop-to-buffer (get-buffer makefile-up-to-date-buffer-name)) |
| 1554 | (shrink-window-if-larger-than-buffer) |
| 1555 | (sort-lines nil (point-min) (point-max)) |
| 1556 | (setq buffer-read-only t)))))) |
| 1557 | |
| 1558 | (defun makefile-save-temporary () |
| 1559 | "Create a temporary file from the current makefile buffer." |
| 1560 | (let ((filename (makefile-generate-temporary-filename))) |
| 1561 | (write-region (point-min) (point-max) filename nil 0) |
| 1562 | filename)) ; return the filename |
| 1563 | |
| 1564 | (defun makefile-generate-temporary-filename () |
| 1565 | "Create a filename suitable for use in `makefile-save-temporary'. |
| 1566 | Be careful to allow brain-dead file systems (DOS, SYSV ...) to cope |
| 1567 | with the generated name!" |
| 1568 | (let ((my-name (user-login-name)) |
| 1569 | (my-uid (int-to-string (user-uid)))) |
| 1570 | (concat "mktmp" |
| 1571 | (if (> (length my-name) 3) |
| 1572 | (substring my-name 0 3) |
| 1573 | my-name) |
| 1574 | "." |
| 1575 | (if (> (length my-uid) 3) |
| 1576 | (substring my-uid 0 3) |
| 1577 | my-uid)))) |
| 1578 | |
| 1579 | (defun makefile-query-targets (filename target-table prereq-list) |
| 1580 | "Fill the up-to-date overview buffer. |
| 1581 | Checks each target in TARGET-TABLE using |
| 1582 | `makefile-query-one-target-method-function' |
| 1583 | and generates the overview, one line per target name." |
| 1584 | (insert |
| 1585 | (mapconcat |
| 1586 | (function (lambda (item) |
| 1587 | (let* ((target-name (car item)) |
| 1588 | (no-prereqs (not (member target-name prereq-list))) |
| 1589 | (needs-rebuild (or no-prereqs |
| 1590 | (funcall |
| 1591 | makefile-query-one-target-method-function |
| 1592 | target-name |
| 1593 | filename)))) |
| 1594 | (format "\t%s%s" |
| 1595 | target-name |
| 1596 | (cond (no-prereqs " .. has no prerequisites") |
| 1597 | (needs-rebuild " .. NEEDS REBUILD") |
| 1598 | (t " .. is up to date")))) |
| 1599 | )) |
| 1600 | target-table "\n")) |
| 1601 | (goto-char (point-min)) |
| 1602 | (delete-file filename)) ; remove the tmpfile |
| 1603 | |
| 1604 | (defun makefile-query-by-make-minus-q (target &optional filename) |
| 1605 | (not (eq 0 |
| 1606 | (call-process makefile-brave-make nil nil nil |
| 1607 | "-f" filename "-q" target)))) |
| 1608 | |
| 1609 | \f |
| 1610 | |
| 1611 | ;;; ------------------------------------------------------------ |
| 1612 | ;;; Continuation cleanup |
| 1613 | ;;; ------------------------------------------------------------ |
| 1614 | |
| 1615 | (defun makefile-cleanup-continuations () |
| 1616 | (if (derived-mode-p 'makefile-mode) |
| 1617 | (if (and makefile-cleanup-continuations |
| 1618 | (not buffer-read-only)) |
| 1619 | (save-excursion |
| 1620 | (goto-char (point-min)) |
| 1621 | (while (re-search-forward "\\\\[ \t]+$" nil t) |
| 1622 | (replace-match "\\" t t)))))) |
| 1623 | |
| 1624 | |
| 1625 | ;;; ------------------------------------------------------------ |
| 1626 | ;;; Warn of suspicious lines |
| 1627 | ;;; ------------------------------------------------------------ |
| 1628 | |
| 1629 | (defun makefile-warn-suspicious-lines () |
| 1630 | ;; Returning non-nil cancels the save operation |
| 1631 | (if (derived-mode-p 'makefile-mode) |
| 1632 | (save-excursion |
| 1633 | (goto-char (point-min)) |
| 1634 | (if (re-search-forward "^\\(\t+$\\| +\t\\)" nil t) |
| 1635 | (not (y-or-n-p |
| 1636 | (format "Suspicious line %d. Save anyway? " |
| 1637 | (count-lines (point-min) (point))))))))) |
| 1638 | |
| 1639 | (defun makefile-warn-continuations () |
| 1640 | (if (derived-mode-p 'makefile-mode) |
| 1641 | (save-excursion |
| 1642 | (goto-char (point-min)) |
| 1643 | (if (re-search-forward "\\\\[ \t]+$" nil t) |
| 1644 | (not (y-or-n-p |
| 1645 | (format "Suspicious continuation in line %d. Save anyway? " |
| 1646 | (count-lines (point-min) (point))))))))) |
| 1647 | \f |
| 1648 | |
| 1649 | ;;; ------------------------------------------------------------ |
| 1650 | ;;; GNU make function support |
| 1651 | ;;; ------------------------------------------------------------ |
| 1652 | |
| 1653 | (defun makefile-insert-gmake-function () |
| 1654 | "Insert a GNU make function call. |
| 1655 | Asks for the name of the function to use (with completion). |
| 1656 | Then prompts for all required parameters." |
| 1657 | (interactive) |
| 1658 | (let* ((gm-function-name (completing-read |
| 1659 | "Function: " |
| 1660 | makefile-gnumake-functions-alist |
| 1661 | nil t nil)) |
| 1662 | (gm-function-prompts |
| 1663 | (cdr (assoc gm-function-name makefile-gnumake-functions-alist)))) |
| 1664 | (if (not (zerop (length gm-function-name))) |
| 1665 | (insert (makefile-format-macro-ref |
| 1666 | (concat gm-function-name " " |
| 1667 | (makefile-prompt-for-gmake-funargs |
| 1668 | gm-function-name gm-function-prompts))) |
| 1669 | " ")))) |
| 1670 | |
| 1671 | (defun makefile-prompt-for-gmake-funargs (function-name prompt-list) |
| 1672 | (mapconcat |
| 1673 | (function (lambda (one-prompt) |
| 1674 | (read-string (format "[%s] %s: " function-name one-prompt) |
| 1675 | nil))) |
| 1676 | prompt-list |
| 1677 | ",")) |
| 1678 | |
| 1679 | \f |
| 1680 | |
| 1681 | ;;; ------------------------------------------------------------ |
| 1682 | ;;; Utility functions |
| 1683 | ;;; ------------------------------------------------------------ |
| 1684 | |
| 1685 | (defun makefile-match-function-end (end) |
| 1686 | "To be called as an anchored matcher by font-lock. |
| 1687 | The anchor must have matched the opening parens in the first group." |
| 1688 | (let ((s (match-string-no-properties 1))) |
| 1689 | ;; FIXME forward-sexp or somesuch would be better? |
| 1690 | (if (setq s (cond ((string= s "(") ")") |
| 1691 | ((string= s "{") "}") |
| 1692 | ((string= s "((") "))") |
| 1693 | ((string= s "{{") "}}"))) |
| 1694 | (re-search-forward (concat "\\(.*\\)[ \t]*" s) (line-end-position) t)))) |
| 1695 | |
| 1696 | (defun makefile-match-dependency (bound) |
| 1697 | "Search for `makefile-dependency-regex' up to BOUND. |
| 1698 | Checks that the colon has not already been fontified, else we |
| 1699 | matched in a rule action." |
| 1700 | (catch 'found |
| 1701 | (let ((pt (point))) |
| 1702 | (while (progn (skip-chars-forward makefile-dependency-skip bound) |
| 1703 | (< (point) (or bound (point-max)))) |
| 1704 | (forward-char) |
| 1705 | (or (eq (char-after) ?=) |
| 1706 | (get-text-property (1- (point)) 'face) |
| 1707 | (if (> (line-beginning-position) (+ (point-min) 2)) |
| 1708 | (eq (char-before (line-end-position 0)) ?\\)) |
| 1709 | (when (save-excursion |
| 1710 | (beginning-of-line) |
| 1711 | (looking-at makefile-dependency-regex)) |
| 1712 | (save-excursion |
| 1713 | (let ((deps-end (match-end 1)) |
| 1714 | (match-data (match-data))) |
| 1715 | (goto-char deps-end) |
| 1716 | (skip-chars-backward " \t") |
| 1717 | (setq deps-end (point)) |
| 1718 | (beginning-of-line) |
| 1719 | (skip-chars-forward " \t") |
| 1720 | ;; Alter the bounds recorded for subexp 1, |
| 1721 | ;; which is what is supposed to match the targets. |
| 1722 | (setcar (nthcdr 2 match-data) (point)) |
| 1723 | (setcar (nthcdr 3 match-data) deps-end) |
| 1724 | (store-match-data match-data))) |
| 1725 | (end-of-line) |
| 1726 | (throw 'found (point))))) |
| 1727 | (goto-char pt)) |
| 1728 | nil)) |
| 1729 | |
| 1730 | (defun makefile-match-action (bound) |
| 1731 | (catch 'found |
| 1732 | (while (re-search-forward makefile-rule-action-regex bound t) |
| 1733 | (or (eq ?\\ (char-after (- (match-beginning 0) 2))) |
| 1734 | (throw 'found t))))) |
| 1735 | |
| 1736 | (defun makefile-do-macro-insertion (macro-name) |
| 1737 | "Insert a macro reference." |
| 1738 | (if (not (zerop (length macro-name))) |
| 1739 | (if (assoc macro-name makefile-runtime-macros-list) |
| 1740 | (insert "$" macro-name) |
| 1741 | (insert (makefile-format-macro-ref macro-name))))) |
| 1742 | |
| 1743 | (defun makefile-remember-target (target-name &optional has-prereqs) |
| 1744 | "Remember a given target if it is not already remembered for this buffer." |
| 1745 | (if (not (zerop (length target-name))) |
| 1746 | (progn |
| 1747 | (if (not (assoc target-name makefile-target-table)) |
| 1748 | (setq makefile-target-table |
| 1749 | (cons (list target-name) makefile-target-table))) |
| 1750 | (if has-prereqs |
| 1751 | (setq makefile-has-prereqs |
| 1752 | (cons target-name makefile-has-prereqs)))))) |
| 1753 | |
| 1754 | (defun makefile-remember-macro (macro-name) |
| 1755 | "Remember a given macro if it is not already remembered for this buffer." |
| 1756 | (if (not (zerop (length macro-name))) |
| 1757 | (if (not (assoc macro-name makefile-macro-table)) |
| 1758 | (setq makefile-macro-table |
| 1759 | (cons (list macro-name) makefile-macro-table))))) |
| 1760 | |
| 1761 | (defun makefile-forward-after-target-colon () |
| 1762 | "Move point forward after inserting the terminating colon of a target. |
| 1763 | This acts according to the value of `makefile-tab-after-target-colon'." |
| 1764 | (if makefile-tab-after-target-colon |
| 1765 | (insert "\t") |
| 1766 | (insert " "))) |
| 1767 | |
| 1768 | (defun makefile-browser-on-macro-line-p () |
| 1769 | "Determine if point is on a macro line in the browser." |
| 1770 | (save-excursion |
| 1771 | (beginning-of-line) |
| 1772 | (re-search-forward "\\$[{(]" (line-end-position) t))) |
| 1773 | |
| 1774 | (defun makefile-browser-this-line-target-name () |
| 1775 | "Extract the target name from a line in the browser." |
| 1776 | (save-excursion |
| 1777 | (end-of-line) |
| 1778 | (skip-chars-backward "^ \t") |
| 1779 | (buffer-substring (point) (1- (line-end-position))))) |
| 1780 | |
| 1781 | (defun makefile-browser-this-line-macro-name () |
| 1782 | "Extract the macro name from a line in the browser." |
| 1783 | (save-excursion |
| 1784 | (beginning-of-line) |
| 1785 | (re-search-forward "\\$[{(]" (line-end-position) t) |
| 1786 | (let ((macro-start (point))) |
| 1787 | (skip-chars-forward "^})") |
| 1788 | (buffer-substring macro-start (point))))) |
| 1789 | |
| 1790 | (defun makefile-format-macro-ref (macro-name) |
| 1791 | "Format a macro reference. |
| 1792 | Uses `makefile-use-curly-braces-for-macros-p'." |
| 1793 | (if (or (char-equal ?\( (string-to-char macro-name)) |
| 1794 | (char-equal ?\{ (string-to-char macro-name))) |
| 1795 | (format "$%s" macro-name) |
| 1796 | (if makefile-use-curly-braces-for-macros-p |
| 1797 | (format "${%s}" macro-name) |
| 1798 | (format "$(%s)" macro-name)))) |
| 1799 | |
| 1800 | (defun makefile-browser-get-state-for-line (n) |
| 1801 | (aref makefile-browser-selection-vector (1- n))) |
| 1802 | |
| 1803 | (defun makefile-browser-set-state-for-line (n to-state) |
| 1804 | (aset makefile-browser-selection-vector (1- n) to-state)) |
| 1805 | |
| 1806 | (defun makefile-browser-toggle-state-for-line (n) |
| 1807 | (makefile-browser-set-state-for-line n (not (makefile-browser-get-state-for-line n)))) |
| 1808 | |
| 1809 | (defun makefile-last-line-p () |
| 1810 | (= (line-end-position) (point-max))) |
| 1811 | |
| 1812 | (defun makefile-first-line-p () |
| 1813 | (= (line-beginning-position) (point-min))) |
| 1814 | |
| 1815 | \f |
| 1816 | |
| 1817 | ;;; Support for other packages, like add-log. |
| 1818 | |
| 1819 | (defun makefile-add-log-defun () |
| 1820 | "Return name of target or variable assignment that point is in. |
| 1821 | If it isn't in one, return nil." |
| 1822 | (save-excursion |
| 1823 | (let (found) |
| 1824 | (beginning-of-line) |
| 1825 | ;; Scan back line by line, noticing when we come to a |
| 1826 | ;; variable or rule definition, and giving up when we see |
| 1827 | ;; a line that is not part of either of those. |
| 1828 | (while (not (or (setq found |
| 1829 | (when (or (looking-at makefile-macroassign-regex) |
| 1830 | (looking-at makefile-dependency-regex)) |
| 1831 | (match-string-no-properties 1))) |
| 1832 | ;; Don't keep looking across a blank line or comment. |
| 1833 | (looking-at "$\\|#") |
| 1834 | (not (zerop (forward-line -1)))))) |
| 1835 | ;; Remove leading and trailing whitespace. |
| 1836 | (when found |
| 1837 | (setq found (replace-regexp-in-string "[ \t]+\\'" "" found)) |
| 1838 | (setq found (replace-regexp-in-string "\\`[ \t]+" "" found))) |
| 1839 | found))) |
| 1840 | |
| 1841 | (provide 'make-mode) |
| 1842 | |
| 1843 | ;; arch-tag: bd23545a-de91-44fb-b1b2-feafbb2635a0 |
| 1844 | ;;; make-mode.el ends here |