make-mode.el 67 KB

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