font-lock.el 101 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312
  1. ;;; font-lock.el --- Electric font lock mode
  2. ;; Copyright (C) 1992-2017 Free Software Foundation, Inc.
  3. ;; Author: Jamie Zawinski
  4. ;; Richard Stallman
  5. ;; Stefan Monnier
  6. ;; Maintainer: emacs-devel@gnu.org
  7. ;; Keywords: languages, faces
  8. ;; Package: emacs
  9. ;; This file is part of GNU Emacs.
  10. ;; GNU Emacs is free software: you can redistribute it and/or modify
  11. ;; it under the terms of the GNU General Public License as published by
  12. ;; the Free Software Foundation, either version 3 of the License, or
  13. ;; (at your option) any later version.
  14. ;; GNU Emacs is distributed in the hope that it will be useful,
  15. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. ;; GNU General Public License for more details.
  18. ;; You should have received a copy of the GNU General Public License
  19. ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
  20. ;;; Commentary:
  21. ;; Font Lock mode is a minor mode that causes your comments to be displayed in
  22. ;; one face, strings in another, reserved words in another, and so on.
  23. ;;
  24. ;; Comments will be displayed in `font-lock-comment-face'.
  25. ;; Strings will be displayed in `font-lock-string-face'.
  26. ;; Regexps are used to display selected patterns in other faces.
  27. ;;
  28. ;; To make the text you type be fontified, use M-x font-lock-mode RET.
  29. ;; When this minor mode is on, the faces of the current line are updated with
  30. ;; every insertion or deletion.
  31. ;;
  32. ;; To turn Font Lock mode on automatically, add this to your init file:
  33. ;;
  34. ;; (add-hook 'emacs-lisp-mode-hook #'turn-on-font-lock)
  35. ;;
  36. ;; Or if you want to turn Font Lock mode on in many modes:
  37. ;;
  38. ;; (global-font-lock-mode t)
  39. ;;
  40. ;; Fontification for a particular mode may be available in a number of levels
  41. ;; of decoration. The higher the level, the more decoration, but the more time
  42. ;; it takes to fontify. See the variable `font-lock-maximum-decoration', and
  43. ;; also the variable `font-lock-maximum-size'. Support modes for Font Lock
  44. ;; mode can be used to speed up Font Lock mode. See `font-lock-support-mode'.
  45. ;;; How Font Lock mode fontifies:
  46. ;; When Font Lock mode is turned on in a buffer, it (a) fontifies the entire
  47. ;; buffer and (b) installs one of its fontification functions on one of the
  48. ;; hook variables that are run by Emacs after every buffer change (i.e., an
  49. ;; insertion or deletion). Fontification means the replacement of `face' text
  50. ;; properties in a given region; Emacs displays text with these `face' text
  51. ;; properties appropriately.
  52. ;;
  53. ;; Fontification normally involves syntactic (i.e., strings and comments) and
  54. ;; regexp (i.e., keywords and everything else) passes. There are actually
  55. ;; three passes; (a) the syntactic keyword pass, (b) the syntactic pass and (c)
  56. ;; the keyword pass. Confused?
  57. ;;
  58. ;; The syntactic keyword pass places `syntax-table' text properties in the
  59. ;; buffer according to the variable `font-lock-syntactic-keywords'. It is
  60. ;; necessary because Emacs's syntax table is not powerful enough to describe all
  61. ;; the different syntactic constructs required by the sort of people who decide
  62. ;; that a single quote can be syntactic or not depending on the time of day.
  63. ;; (What sort of person could decide to overload the meaning of a quote?)
  64. ;; Obviously the syntactic keyword pass must occur before the syntactic pass.
  65. ;;
  66. ;; The syntactic pass places `face' text properties in the buffer according to
  67. ;; syntactic context, i.e., according to the buffer's syntax table and buffer
  68. ;; text's `syntax-table' text properties. It involves using a syntax parsing
  69. ;; function to determine the context of different parts of a region of text. A
  70. ;; syntax parsing function is necessary because generally strings and/or
  71. ;; comments can span lines, and so the context of a given region is not
  72. ;; necessarily apparent from the content of that region. Because the keyword
  73. ;; pass only works within a given region, it is not generally appropriate for
  74. ;; syntactic fontification. This is the first fontification pass that makes
  75. ;; changes visible to the user; it fontifies strings and comments.
  76. ;;
  77. ;; The keyword pass places `face' text properties in the buffer according to
  78. ;; the variable `font-lock-keywords'. It involves searching for given regexps
  79. ;; (or calling given search functions) within the given region. This is the
  80. ;; second fontification pass that makes changes visible to the user; it
  81. ;; fontifies language reserved words, etc.
  82. ;;
  83. ;; Oh, and the answer is, "Yes, obviously just about everything should be done
  84. ;; in a single syntactic pass, but the only syntactic parser available
  85. ;; understands only strings and comments." Perhaps one day someone will write
  86. ;; some syntactic parsers for common languages and a son-of-font-lock.el could
  87. ;; use them rather then relying so heavily on the keyword (regexp) pass.
  88. ;;; How Font Lock mode supports modes or is supported by modes:
  89. ;; Modes that support Font Lock mode do so by defining one or more variables
  90. ;; whose values specify the fontification. Font Lock mode knows of these
  91. ;; variable names from the buffer local variable `font-lock-defaults'.
  92. ;; (Font Lock mode is set up via (a) where a mode's patterns are
  93. ;; distributed with the mode's package library, and (b) where a mode's
  94. ;; patterns are distributed with font-lock.el itself. An example of (a)
  95. ;; is Pascal mode, an example of (b) is Lisp mode. Normally, the mechanism is
  96. ;; (a); (b) is used where it is not clear which package library should contain
  97. ;; the pattern definitions.) Font Lock mode chooses which variable to use for
  98. ;; fontification based on `font-lock-maximum-decoration'.
  99. ;;
  100. ;; Font Lock mode fontification behavior can be modified in a number of ways.
  101. ;; See the below comments and the comments distributed throughout this file.
  102. ;;; Constructing patterns:
  103. ;; See the documentation for the variable `font-lock-keywords'.
  104. ;;
  105. ;; Efficient regexps for use as MATCHERs for `font-lock-keywords' and
  106. ;; `font-lock-syntactic-keywords' can be generated via the function
  107. ;; `regexp-opt'.
  108. ;;; Adding patterns for modes that already support Font Lock:
  109. ;; Though Font Lock highlighting patterns already exist for many modes, it's
  110. ;; likely there's something that you want fontified that currently isn't, even
  111. ;; at the maximum fontification level. You can add highlighting patterns via
  112. ;; `font-lock-add-keywords'. For example, say in some C
  113. ;; header file you #define the token `and' to expand to `&&', etc., to make
  114. ;; your C code almost readable. In your ~/.emacs there could be:
  115. ;;
  116. ;; (font-lock-add-keywords 'c-mode '("\\<\\(and\\|or\\|not\\)\\>"))
  117. ;;
  118. ;; Some modes provide specific ways to modify patterns based on the values of
  119. ;; other variables. For example, additional C types can be specified via the
  120. ;; variable `c-font-lock-extra-types'.
  121. ;;; Adding patterns for modes that do not support Font Lock:
  122. ;; Not all modes support Font Lock mode. If you (as a user of the mode) add
  123. ;; patterns for a new mode, you must define in your ~/.emacs a variable or
  124. ;; variables that specify regexp fontification. Then, you should indicate to
  125. ;; Font Lock mode, via the mode hook setting `font-lock-defaults', exactly what
  126. ;; support is required. For example, say Foo mode should have the following
  127. ;; regexps fontified case-sensitively, and comments and strings should not be
  128. ;; fontified automagically. In your ~/.emacs there could be:
  129. ;;
  130. ;; (defvar foo-font-lock-keywords
  131. ;; '(("\\<\\(one\\|two\\|three\\)\\>" . 'font-lock-keyword-face)
  132. ;; ("\\<\\(four\\|five\\|six\\)\\>" . 'font-lock-type-face))
  133. ;; "Default expressions to highlight in Foo mode.")
  134. ;;
  135. ;; (add-hook 'foo-mode-hook
  136. ;; (lambda ()
  137. ;; (set (make-local-variable 'font-lock-defaults)
  138. ;; '(foo-font-lock-keywords t))))
  139. ;;; Adding Font Lock support for modes:
  140. ;; Of course, it would be better that the mode already supports Font Lock mode.
  141. ;; The package author would do something similar to above. The mode must
  142. ;; define at the top-level a variable or variables that specify regexp
  143. ;; fontification. Then, the mode command should indicate to Font Lock mode,
  144. ;; via `font-lock-defaults', exactly what support is required. For example,
  145. ;; say Bar mode should have the following regexps fontified case-insensitively,
  146. ;; and comments and strings should be fontified automagically. In bar.el there
  147. ;; could be:
  148. ;;
  149. ;; (defvar bar-font-lock-keywords
  150. ;; '(("\\<\\(uno\\|due\\|tre\\)\\>" . 'font-lock-keyword-face)
  151. ;; ("\\<\\(quattro\\|cinque\\|sei\\)\\>" . 'font-lock-type-face))
  152. ;; "Default expressions to highlight in Bar mode.")
  153. ;;
  154. ;; and within `bar-mode' there could be:
  155. ;;
  156. ;; (set (make-local-variable 'font-lock-defaults)
  157. ;; '(bar-font-lock-keywords nil t))
  158. ;; What is fontification for? You might say, "It's to make my code look nice."
  159. ;; I think it should be for adding information in the form of cues. These cues
  160. ;; should provide you with enough information to both (a) distinguish between
  161. ;; different items, and (b) identify the item meanings, without having to read
  162. ;; the items and think about it. Therefore, fontification allows you to think
  163. ;; less about, say, the structure of code, and more about, say, why the code
  164. ;; doesn't work. Or maybe it allows you to think less and drift off to sleep.
  165. ;;
  166. ;; So, here are my opinions/advice/guidelines:
  167. ;;
  168. ;; - Highlight conceptual objects, such as function and variable names, and
  169. ;; different objects types differently, i.e., (a) and (b) above, highlight
  170. ;; function names differently to variable names.
  171. ;; - Keep the faces distinct from each other as far as possible.
  172. ;; i.e., (a) above.
  173. ;; - Use the same face for the same conceptual object, across all modes.
  174. ;; i.e., (b) above, all modes that have items that can be thought of as, say,
  175. ;; keywords, should be highlighted with the same face, etc.
  176. ;; - Make the face attributes fit the concept as far as possible.
  177. ;; i.e., function names might be a bold color such as blue, comments might
  178. ;; be a bright color such as red, character strings might be brown, because,
  179. ;; err, strings are brown (that was not the reason, please believe me).
  180. ;; - Don't use a non-nil OVERRIDE unless you have a good reason.
  181. ;; Only use OVERRIDE for special things that are easy to define, such as the
  182. ;; way `...' quotes are treated in strings and comments in Emacs Lisp mode.
  183. ;; Don't use it to, say, highlight keywords in commented out code or strings.
  184. ;; - Err, that's it.
  185. ;;; Code:
  186. (require 'syntax)
  187. (eval-when-compile (require 'cl-lib))
  188. ;; Define core `font-lock' group.
  189. (defgroup font-lock '((jit-lock custom-group))
  190. "Font Lock mode text highlighting package."
  191. :link '(custom-manual :tag "Emacs Manual" "(emacs)Font Lock")
  192. :link '(custom-manual :tag "Elisp Manual" "(elisp)Font Lock Mode")
  193. :group 'faces)
  194. (defgroup font-lock-faces nil
  195. "Faces for highlighting text."
  196. :prefix "font-lock-"
  197. :group 'font-lock)
  198. (defgroup font-lock-extra-types nil
  199. "Extra mode-specific type names for highlighting declarations."
  200. :group 'font-lock)
  201. ;; User variables.
  202. (defcustom font-lock-maximum-size 256000
  203. "Maximum buffer size for unsupported buffer fontification.
  204. When `font-lock-support-mode' is nil, only buffers smaller than
  205. this are fontified. This variable has no effect if a Font Lock
  206. support mode (usually `jit-lock-mode') is enabled.
  207. If nil, means size is irrelevant.
  208. If a list, each element should be a cons pair of the form (MAJOR-MODE . SIZE),
  209. where MAJOR-MODE is a symbol or t (meaning the default). For example:
  210. ((c-mode . 256000) (c++-mode . 256000) (rmail-mode . 1048576))
  211. means that the maximum size is 250K for buffers in C or C++ modes, one megabyte
  212. for buffers in Rmail mode, and size is irrelevant otherwise."
  213. :type '(choice (const :tag "none" nil)
  214. (integer :tag "size")
  215. (repeat :menu-tag "mode specific" :tag "mode specific"
  216. :value ((t . nil))
  217. (cons :tag "Instance"
  218. (radio :tag "Mode"
  219. (const :tag "all" t)
  220. (symbol :tag "name"))
  221. (radio :tag "Size"
  222. (const :tag "none" nil)
  223. (integer :tag "size")))))
  224. :group 'font-lock)
  225. (make-obsolete-variable 'font-lock-maximum-size nil "24.1")
  226. (defcustom font-lock-maximum-decoration t
  227. "Maximum decoration level for fontification.
  228. If nil, use the default decoration (typically the minimum available).
  229. If t, use the maximum decoration available.
  230. If a number, use that level of decoration (or if not available the maximum).
  231. The higher the number, the more decoration is done.
  232. If a list, each element should be a cons pair of the form (MAJOR-MODE . LEVEL),
  233. where MAJOR-MODE is a symbol or t (meaning the default). For example:
  234. ((c-mode . t) (c++-mode . 2) (t . 1))
  235. means use the maximum decoration available for buffers in C mode, level 2
  236. decoration for buffers in C++ mode, and level 1 decoration otherwise."
  237. :type '(choice (const :tag "default" nil)
  238. (const :tag "maximum" t)
  239. (integer :tag "level" 1)
  240. (repeat :menu-tag "mode specific" :tag "mode specific"
  241. :value ((t . t))
  242. (cons :tag "Instance"
  243. (radio :tag "Mode"
  244. (const :tag "all" t)
  245. (symbol :tag "name"))
  246. (radio :tag "Decoration"
  247. (const :tag "default" nil)
  248. (const :tag "maximum" t)
  249. (integer :tag "level" 1)))))
  250. :group 'font-lock)
  251. (defcustom font-lock-verbose nil
  252. "If non-nil, means show status messages for buffer fontification.
  253. If a number, only buffers greater than this size have fontification messages."
  254. :type '(choice (const :tag "never" nil)
  255. (other :tag "always" t)
  256. (integer :tag "size"))
  257. :group 'font-lock
  258. :version "24.1")
  259. ;; Originally these variable values were face names such as `bold' etc.
  260. ;; Now we create our own faces, but we keep these variables for compatibility
  261. ;; and they give users another mechanism for changing face appearance.
  262. ;; We now allow a FACENAME in `font-lock-keywords' to be any expression that
  263. ;; returns a face. So the easiest thing is to continue using these variables,
  264. ;; rather than sometimes evalling FACENAME and sometimes not. sm.
  265. ;; Note that in new code, in the vast majority of cases there is no
  266. ;; need to create variables that specify face names. Simply using
  267. ;; faces directly is enough. Font-lock is not a template to be
  268. ;; followed in this area.
  269. (defvar font-lock-comment-face 'font-lock-comment-face
  270. "Face name to use for comments.")
  271. (defvar font-lock-comment-delimiter-face 'font-lock-comment-delimiter-face
  272. "Face name to use for comment delimiters.")
  273. (defvar font-lock-string-face 'font-lock-string-face
  274. "Face name to use for strings.")
  275. (defvar font-lock-doc-face 'font-lock-doc-face
  276. "Face name to use for documentation.")
  277. (defvar font-lock-keyword-face 'font-lock-keyword-face
  278. "Face name to use for keywords.")
  279. (defvar font-lock-builtin-face 'font-lock-builtin-face
  280. "Face name to use for builtins.")
  281. (defvar font-lock-function-name-face 'font-lock-function-name-face
  282. "Face name to use for function names.")
  283. (defvar font-lock-variable-name-face 'font-lock-variable-name-face
  284. "Face name to use for variable names.")
  285. (defvar font-lock-type-face 'font-lock-type-face
  286. "Face name to use for type and class names.")
  287. (defvar font-lock-constant-face 'font-lock-constant-face
  288. "Face name to use for constant and label names.")
  289. (defvar font-lock-warning-face 'font-lock-warning-face
  290. "Face name to use for things that should stand out.")
  291. (defvar font-lock-negation-char-face 'font-lock-negation-char-face
  292. "Face name to use for easy to overlook negation.
  293. This can be an \"!\" or the \"n\" in \"ifndef\".")
  294. (defvar font-lock-preprocessor-face 'font-lock-preprocessor-face
  295. "Face name to use for preprocessor directives.")
  296. (define-obsolete-variable-alias
  297. 'font-lock-reference-face 'font-lock-constant-face "20.3")
  298. ;; Fontification variables:
  299. (defvar font-lock-keywords nil
  300. "A list of the keywords to highlight.
  301. There are two kinds of values: user-level, and compiled.
  302. A user-level keywords list is what a major mode or the user would
  303. set up. Normally the list would come from `font-lock-defaults'.
  304. through selection of a fontification level and evaluation of any
  305. contained expressions. You can also alter it by calling
  306. `font-lock-add-keywords' or `font-lock-remove-keywords' with MODE = nil.
  307. Each element in a user-level keywords list should have one of these forms:
  308. MATCHER
  309. (MATCHER . SUBEXP)
  310. (MATCHER . FACENAME)
  311. (MATCHER . HIGHLIGHT)
  312. (MATCHER HIGHLIGHT ...)
  313. (eval . FORM)
  314. where MATCHER can be either the regexp to search for, or the
  315. function name to call to make the search (called with one
  316. argument, the limit of the search; it should return non-nil, move
  317. point, and set `match-data' appropriately if it succeeds; like
  318. `re-search-forward' would). MATCHER regexps can be generated via
  319. the function `regexp-opt'.
  320. FORM is an expression, whose value should be a keyword element,
  321. evaluated when the keyword is (first) used in a buffer. This
  322. feature can be used to provide a keyword that can only be
  323. generated when Font Lock mode is actually turned on.
  324. HIGHLIGHT should be either MATCH-HIGHLIGHT or MATCH-ANCHORED.
  325. For highlighting single items, for example each instance of the
  326. word \"foo\", typically only MATCH-HIGHLIGHT is required.
  327. However, if an item or (typically) items are to be highlighted
  328. following the instance of another item (the anchor), for example
  329. each instance of the word \"bar\" following the word \"anchor\"
  330. then MATCH-ANCHORED may be required.
  331. MATCH-HIGHLIGHT should be of the form:
  332. (SUBEXP FACENAME [OVERRIDE [LAXMATCH]])
  333. SUBEXP is the number of the subexpression of MATCHER to be
  334. highlighted.
  335. FACENAME is an expression whose value is the face name to use.
  336. Instead of a face, FACENAME can evaluate to a property list of
  337. the form (face FACE PROP1 VAL1 PROP2 VAL2 ...) in which case all
  338. the listed text-properties will be set rather than just FACE. In
  339. such a case, you will most likely want to put those properties in
  340. `font-lock-extra-managed-props' or to override
  341. `font-lock-unfontify-region-function'.
  342. OVERRIDE and LAXMATCH are flags. If OVERRIDE is t, existing
  343. fontification can be overwritten. If `keep', only parts not
  344. already fontified are highlighted. If `prepend' or `append',
  345. existing fontification is merged with the new, in which the new
  346. or existing fontification, respectively, takes precedence. If
  347. LAXMATCH is non-nil, that means don't signal an error if there is
  348. no match for SUBEXP in MATCHER.
  349. For example, an element of the form highlights (if not already
  350. highlighted):
  351. \"\\\\\\=<foo\\\\\\=>\"
  352. Discrete occurrences of \"foo\" in the value of the variable
  353. `font-lock-keyword-face'.
  354. (\"fu\\\\(bar\\\\)\" . 1)
  355. Substring \"bar\" within all occurrences of \"fubar\" in the
  356. value of `font-lock-keyword-face'.
  357. (\"fubar\" . fubar-face)
  358. Occurrences of \"fubar\" in the value of `fubar-face'.
  359. (\"foo\\\\|bar\" 0 foo-bar-face t)
  360. Occurrences of either \"foo\" or \"bar\" in the value of
  361. `foo-bar-face', even if already highlighted.
  362. (fubar-match 1 fubar-face)
  363. The first subexpression within all occurrences of whatever the
  364. function `fubar-match' finds and matches in the value of
  365. `fubar-face'.
  366. MATCH-ANCHORED should be of the form:
  367. (MATCHER PRE-MATCH-FORM POST-MATCH-FORM MATCH-HIGHLIGHT ...)
  368. where MATCHER is a regexp to search for or the function name to
  369. call to make the search, as for MATCH-HIGHLIGHT above, but with
  370. one exception; see below. PRE-MATCH-FORM and POST-MATCH-FORM are
  371. evaluated before the first, and after the last, instance
  372. MATCH-ANCHORED's MATCHER is used. Therefore they can be used to
  373. initialize before, and cleanup after, MATCHER is used.
  374. Typically, PRE-MATCH-FORM is used to move to some position
  375. relative to the original MATCHER, before starting with
  376. MATCH-ANCHORED's MATCHER. POST-MATCH-FORM might be used to move
  377. back, before resuming with MATCH-ANCHORED's parent's MATCHER.
  378. For example, an element of the form highlights (if not already
  379. highlighted):
  380. (\"\\\\\\=<anchor\\\\\\=>\" (0 anchor-face)
  381. (\"\\\\\\=<item\\\\\\=>\" nil nil (0 item-face)))
  382. Discrete occurrences of \"anchor\" in the value of
  383. `anchor-face', and subsequent discrete occurrences of
  384. \"item\" (on the same line) in the value of `item-face'.
  385. (Here PRE-MATCH-FORM and POST-MATCH-FORM are nil. Therefore
  386. \"item\" is initially searched for starting from the end of the
  387. match of \"anchor\", and searching for subsequent instances of
  388. \"anchor\" resumes from where searching for \"item\" concluded.)
  389. The above-mentioned exception is as follows. The limit of the
  390. MATCHER search defaults to the end of the line after
  391. PRE-MATCH-FORM is evaluated. However, if PRE-MATCH-FORM returns
  392. a position greater than the position after PRE-MATCH-FORM is
  393. evaluated, that position is used as the limit of the search. It
  394. is generally a bad idea to return a position greater than the end
  395. of the line, i.e., cause the MATCHER search to span lines.
  396. These regular expressions can match text which spans lines,
  397. although it is better to avoid it if possible since updating them
  398. while editing text is slower, and it is not guaranteed to be
  399. always correct when using support modes like jit-lock or
  400. lazy-lock.
  401. This variable is set by major modes via the variable
  402. `font-lock-defaults'. Be careful when composing regexps for this
  403. list; a poorly written pattern can dramatically slow things down!
  404. A compiled keywords list starts with t. It is produced
  405. internally by `font-lock-compile-keywords' from a user-level
  406. keywords list. Its second element is the user-level keywords
  407. list that was compiled. The remaining elements have the same
  408. form as user-level keywords, but normally their values have been
  409. optimized.")
  410. (defvar font-lock-keywords-alist nil
  411. "Alist of additional `font-lock-keywords' elements for major modes.
  412. Each element has the form (MODE KEYWORDS . HOW).
  413. Function `font-lock-set-defaults' adds the elements in the list KEYWORDS to
  414. `font-lock-keywords' when Font Lock is turned on in major mode MODE.
  415. If HOW is nil, KEYWORDS are added at the beginning of
  416. `font-lock-keywords'. If it is `set', they are used to replace the
  417. value of `font-lock-keywords'. If HOW is any other non-nil value,
  418. they are added at the end.
  419. This is normally set via `font-lock-add-keywords' and
  420. `font-lock-remove-keywords'.")
  421. (put 'font-lock-keywords-alist 'risky-local-variable t)
  422. (defvar font-lock-removed-keywords-alist nil
  423. "Alist of `font-lock-keywords' elements to be removed for major modes.
  424. Each element has the form (MODE . KEYWORDS). Function `font-lock-set-defaults'
  425. removes the elements in the list KEYWORDS from `font-lock-keywords'
  426. when Font Lock is turned on in major mode MODE.
  427. This is normally set via `font-lock-add-keywords' and
  428. `font-lock-remove-keywords'.")
  429. (defvar font-lock-keywords-only nil
  430. "Non-nil means Font Lock should not fontify comments or strings.
  431. This is normally set via `font-lock-defaults'.")
  432. (defvar font-lock-keywords-case-fold-search nil
  433. "Non-nil means the patterns in `font-lock-keywords' are case-insensitive.
  434. This is set via the function `font-lock-set-defaults', based on
  435. the CASE-FOLD argument of `font-lock-defaults'.")
  436. (make-variable-buffer-local 'font-lock-keywords-case-fold-search)
  437. (defvar font-lock-syntactically-fontified 0
  438. "Point up to which `font-lock-syntactic-keywords' has been applied.
  439. If nil, this is ignored, in which case the syntactic fontification may
  440. sometimes be slightly incorrect.")
  441. (make-variable-buffer-local 'font-lock-syntactically-fontified)
  442. (defvar font-lock-syntactic-face-function
  443. (lambda (state)
  444. (if (nth 3 state) font-lock-string-face font-lock-comment-face))
  445. "Function to determine which face to use when fontifying syntactically.
  446. The function is called with a single parameter (the state as returned by
  447. `parse-partial-sexp' at the beginning of the region to highlight) and
  448. should return a face. This is normally set via `font-lock-defaults'.")
  449. (defvar font-lock-syntactic-keywords nil
  450. "A list of the syntactic keywords to put syntax properties on.
  451. The value can be the list itself, or the name of a function or variable
  452. whose value is the list.
  453. See `font-lock-keywords' for a description of the form of this list;
  454. only the differences are stated here. MATCH-HIGHLIGHT should be of the form:
  455. (SUBEXP SYNTAX OVERRIDE LAXMATCH)
  456. where SYNTAX can be a string (as taken by `modify-syntax-entry'), a syntax
  457. table, a cons cell (as returned by `string-to-syntax') or an expression whose
  458. value is such a form. OVERRIDE cannot be `prepend' or `append'.
  459. Here are two examples of elements of `font-lock-syntactic-keywords'
  460. and what they do:
  461. (\"\\\\$\\\\(#\\\\)\" 1 \".\")
  462. gives a hash character punctuation syntax (\".\") when following a
  463. dollar-sign character. Hash characters in other contexts will still
  464. follow whatever the syntax table says about the hash character.
  465. (\"\\\\(\\='\\\\).\\\\(\\='\\\\)\"
  466. (1 \"\\\"\")
  467. (2 \"\\\"\"))
  468. gives a pair of apostrophes, which surround a single character, a
  469. SYNTAX of \"\\\"\" (meaning string quote syntax). Apostrophes in other
  470. contexts will not be affected.
  471. This is normally set via `font-lock-defaults'.")
  472. (make-obsolete-variable 'font-lock-syntactic-keywords
  473. 'syntax-propertize-function "24.1")
  474. (defvar font-lock-syntax-table nil
  475. "Non-nil means use this syntax table for fontifying.
  476. If this is nil, the major mode's syntax table is used.
  477. This is normally set via `font-lock-defaults'.")
  478. (defvar font-lock-mark-block-function nil
  479. "Non-nil means use this function to mark a block of text.
  480. When called with no args it should leave point at the beginning of any
  481. enclosing textual block and mark at the end.
  482. This is normally set via `font-lock-defaults'.")
  483. (defvar font-lock-fontify-buffer-function #'font-lock-default-fontify-buffer
  484. "Function to use for fontifying the buffer.
  485. This is normally set via `font-lock-defaults'.")
  486. (defvar font-lock-unfontify-buffer-function #'font-lock-default-unfontify-buffer
  487. "Function to use for unfontifying the buffer.
  488. This is used when turning off Font Lock mode.
  489. This is normally set via `font-lock-defaults'.")
  490. (defvar font-lock-fontify-region-function #'font-lock-default-fontify-region
  491. "Function to use for fontifying a region.
  492. It should take two args, the beginning and end of the region, and an optional
  493. third arg VERBOSE. If VERBOSE is non-nil, the function should print status
  494. messages. This is normally set via `font-lock-defaults'.
  495. If it fontifies a larger region, it should ideally return a list of the form
  496. \(jit-lock-bounds BEG . END) indicating the bounds of the region actually
  497. fontified.")
  498. (defvar font-lock-unfontify-region-function #'font-lock-default-unfontify-region
  499. "Function to use for unfontifying a region.
  500. It should take two args, the beginning and end of the region.
  501. This is normally set via `font-lock-defaults'.")
  502. (defvar font-lock-inhibit-thing-lock nil
  503. "List of Font Lock mode related modes that should not be turned on.
  504. Currently, valid mode names are `fast-lock-mode', `jit-lock-mode' and
  505. `lazy-lock-mode'. This is normally set via `font-lock-defaults'.")
  506. (make-obsolete-variable 'font-lock-inhibit-thing-lock nil "25.1")
  507. (defvar-local font-lock-multiline nil
  508. "Whether font-lock should cater to multiline keywords.
  509. If nil, don't try to handle multiline patterns.
  510. If t, always handle multiline patterns.
  511. If `undecided', don't try to handle multiline patterns until you see one.
  512. Major/minor modes can set this variable if they know which option applies.")
  513. (defvar-local font-lock-fontified nil) ; Whether we have fontified the buffer.
  514. ;; Font Lock mode.
  515. (eval-when-compile
  516. ;;
  517. ;; Borrowed from lazy-lock.el.
  518. ;; We use this to preserve or protect things when modifying text properties.
  519. (defmacro save-buffer-state (&rest body)
  520. "Bind variables according to VARLIST and eval BODY restoring buffer state."
  521. (declare (indent 0) (debug t))
  522. `(let ((inhibit-point-motion-hooks t))
  523. (with-silent-modifications
  524. ,@body)))
  525. ;;
  526. ;; Shut up the byte compiler.
  527. (defvar font-lock-face-attributes)) ; Obsolete but respected if set.
  528. (defvar-local font-lock-set-defaults nil) ; Whether we have set up defaults.
  529. (defun font-lock-specified-p (mode)
  530. "Return non-nil if the current buffer is ready for fontification.
  531. The MODE argument, if non-nil, means Font Lock mode is about to
  532. be enabled."
  533. (or font-lock-defaults
  534. (and (boundp 'font-lock-keywords)
  535. font-lock-keywords)
  536. (and mode
  537. font-lock-set-defaults
  538. font-lock-major-mode
  539. (not (eq font-lock-major-mode major-mode)))))
  540. (defun font-lock-initial-fontify ()
  541. ;; The first fontification after turning the mode on. This must
  542. ;; only be called after the mode hooks have been run.
  543. (when (and font-lock-mode
  544. (font-lock-specified-p t))
  545. (let ((max-size (font-lock-value-in-major-mode font-lock-maximum-size)))
  546. (cond (font-lock-fontified
  547. nil)
  548. ((or (null max-size) (> max-size (buffer-size)))
  549. (font-lock-fontify-buffer))
  550. (font-lock-verbose
  551. (message "Fontifying %s...buffer size greater than font-lock-maximum-size"
  552. (buffer-name)))))))
  553. (defun font-lock-mode-internal (arg)
  554. ;; Turn on Font Lock mode.
  555. (when arg
  556. (add-hook 'after-change-functions #'font-lock-after-change-function t t)
  557. (font-lock-set-defaults)
  558. (font-lock-turn-on-thing-lock))
  559. ;; Turn off Font Lock mode.
  560. (unless font-lock-mode
  561. (remove-hook 'after-change-functions #'font-lock-after-change-function t)
  562. (font-lock-unfontify-buffer)
  563. (font-lock-turn-off-thing-lock)))
  564. (defun font-lock-add-keywords (mode keywords &optional how)
  565. "Add highlighting KEYWORDS for MODE.
  566. MODE should be a symbol, the major mode command name, such as `c-mode'
  567. or nil. If nil, highlighting keywords are added for the current buffer.
  568. KEYWORDS should be a list; see the variable `font-lock-keywords'.
  569. By default they are added at the beginning of the current highlighting list.
  570. If optional argument HOW is `set', they are used to replace the current
  571. highlighting list. If HOW is any other non-nil value, they are added at the
  572. end of the current highlighting list.
  573. For example:
  574. (font-lock-add-keywords \\='c-mode
  575. \\='((\"\\\\\\=<\\\\(FIXME\\\\):\" 1 \\='font-lock-warning-face prepend)
  576. (\"\\\\\\=<\\\\(and\\\\|or\\\\|not\\\\)\\\\\\=>\" . \\='font-lock-keyword-face)))
  577. adds two fontification patterns for C mode, to fontify `FIXME:' words, even in
  578. comments, and to fontify `and', `or' and `not' words as keywords.
  579. The above procedure will only add the keywords for C mode, not
  580. for modes derived from C mode. To add them for derived modes too,
  581. pass nil for MODE and add the call to c-mode-hook.
  582. For example:
  583. (add-hook \\='c-mode-hook
  584. (lambda ()
  585. (font-lock-add-keywords nil
  586. \\='((\"\\\\\\=<\\\\(FIXME\\\\):\" 1 \\='font-lock-warning-face prepend)
  587. (\"\\\\\\=<\\\\(and\\\\|or\\\\|not\\\\)\\\\\\=>\" .
  588. \\='font-lock-keyword-face)))))
  589. The above procedure may fail to add keywords to derived modes if
  590. some involved major mode does not follow the standard conventions.
  591. File a bug report if this happens, so the major mode can be corrected.
  592. Note that some modes have specialized support for additional patterns, e.g.,
  593. see the variables `c-font-lock-extra-types', `c++-font-lock-extra-types',
  594. `objc-font-lock-extra-types' and `java-font-lock-extra-types'."
  595. (cond (mode
  596. ;; If MODE is non-nil, add the KEYWORDS and HOW spec to
  597. ;; `font-lock-keywords-alist' so `font-lock-set-defaults' uses them.
  598. (let ((spec (cons keywords how)) cell)
  599. (if (setq cell (assq mode font-lock-keywords-alist))
  600. (if (eq how 'set)
  601. (setcdr cell (list spec))
  602. (setcdr cell (append (cdr cell) (list spec))))
  603. (push (list mode spec) font-lock-keywords-alist)))
  604. ;; Make sure that `font-lock-removed-keywords-alist' does not
  605. ;; contain the new keywords.
  606. (font-lock-update-removed-keyword-alist mode keywords how))
  607. (t
  608. (when (and font-lock-mode
  609. (not (or font-lock-keywords font-lock-defaults)))
  610. ;; The major mode has not set any keywords, so when we enabled
  611. ;; font-lock-mode it only enabled the font-core.el part, not the
  612. ;; font-lock-mode-internal. Try again.
  613. (font-lock-mode -1)
  614. (set (make-local-variable 'font-lock-defaults) '(nil t))
  615. (font-lock-mode 1))
  616. ;; Otherwise set or add the keywords now.
  617. ;; This is a no-op if it has been done already in this buffer
  618. ;; for the correct major mode.
  619. (font-lock-set-defaults)
  620. (let ((was-compiled (eq (car font-lock-keywords) t)))
  621. ;; Bring back the user-level (uncompiled) keywords.
  622. (if was-compiled
  623. (setq font-lock-keywords (cadr font-lock-keywords)))
  624. ;; Now modify or replace them.
  625. (if (eq how 'set)
  626. (setq font-lock-keywords keywords)
  627. (font-lock-remove-keywords nil keywords) ;to avoid duplicates
  628. (let ((old (if (eq (car-safe font-lock-keywords) t)
  629. (cdr font-lock-keywords)
  630. font-lock-keywords)))
  631. (setq font-lock-keywords (if how
  632. (append old keywords)
  633. (append keywords old)))))
  634. ;; If the keywords were compiled before, compile them again.
  635. (if was-compiled
  636. (setq font-lock-keywords
  637. (font-lock-compile-keywords font-lock-keywords)))))))
  638. (defun font-lock-update-removed-keyword-alist (mode keywords how)
  639. "Update `font-lock-removed-keywords-alist' when adding new KEYWORDS to MODE."
  640. ;; When font-lock is enabled first all keywords in the list
  641. ;; `font-lock-keywords-alist' are added, then all keywords in the
  642. ;; list `font-lock-removed-keywords-alist' are removed. If a
  643. ;; keyword was once added, removed, and then added again it must be
  644. ;; removed from the removed-keywords list. Otherwise the second add
  645. ;; will not take effect.
  646. (let ((cell (assq mode font-lock-removed-keywords-alist)))
  647. (if cell
  648. (if (eq how 'set)
  649. ;; A new set of keywords is defined. Forget all about
  650. ;; our old keywords that should be removed.
  651. (setq font-lock-removed-keywords-alist
  652. (delq cell font-lock-removed-keywords-alist))
  653. ;; Delete all previously removed keywords.
  654. (dolist (kword keywords)
  655. (setcdr cell (delete kword (cdr cell))))
  656. ;; Delete the mode cell if empty.
  657. (if (null (cdr cell))
  658. (setq font-lock-removed-keywords-alist
  659. (delq cell font-lock-removed-keywords-alist)))))))
  660. ;; Written by Anders Lindgren
  661. ;;
  662. ;; Case study:
  663. ;; (I) The keywords are removed from a major mode.
  664. ;; In this case the keyword could be local (i.e. added earlier by
  665. ;; `font-lock-add-keywords'), global, or both.
  666. ;;
  667. ;; (a) In the local case we remove the keywords from the variable
  668. ;; `font-lock-keywords-alist'.
  669. ;;
  670. ;; (b) The actual global keywords are not known at this time.
  671. ;; All keywords are added to `font-lock-removed-keywords-alist',
  672. ;; when font-lock is enabled those keywords are removed.
  673. ;;
  674. ;; Note that added keywords are taken out of the list of removed
  675. ;; keywords. This ensure correct operation when the same keyword
  676. ;; is added and removed several times.
  677. ;;
  678. ;; (II) The keywords are removed from the current buffer.
  679. (defun font-lock-remove-keywords (mode keywords)
  680. "Remove highlighting KEYWORDS for MODE.
  681. MODE should be a symbol, the major mode command name, such as
  682. `c-mode' or nil. If nil, highlighting keywords are removed for
  683. the current buffer.
  684. For a description of KEYWORDS, see `font-lock-add-keywords'.
  685. To make the removal apply to modes derived from MODE as well,
  686. pass nil for MODE and add the call to MODE-hook. This may fail
  687. for some derived modes if some involved major mode does not
  688. follow the standard conventions. File a bug report if this
  689. happens, so the major mode can be corrected."
  690. (cond (mode
  691. ;; Remove one keyword at the time.
  692. (dolist (keyword keywords)
  693. (let ((top-cell (assq mode font-lock-keywords-alist)))
  694. ;; If MODE is non-nil, remove the KEYWORD from
  695. ;; `font-lock-keywords-alist'.
  696. (when top-cell
  697. (dolist (keyword-list-how-pair (cdr top-cell))
  698. ;; `keywords-list-how-pair' is a cons with a list of
  699. ;; keywords in the car top-cell and the original how
  700. ;; argument in the cdr top-cell.
  701. (setcar keyword-list-how-pair
  702. (delete keyword (car keyword-list-how-pair))))
  703. ;; Remove keyword list/how pair when the keyword list
  704. ;; is empty and how doesn't specify `set'. (If it
  705. ;; should be deleted then previously deleted keywords
  706. ;; would appear again.)
  707. (let ((cell top-cell))
  708. (while (cdr cell)
  709. (if (and (null (car (car (cdr cell))))
  710. (not (eq (cdr (car (cdr cell))) 'set)))
  711. (setcdr cell (cdr (cdr cell)))
  712. (setq cell (cdr cell)))))
  713. ;; Final cleanup, remove major mode cell if last keyword
  714. ;; was deleted.
  715. (if (null (cdr top-cell))
  716. (setq font-lock-keywords-alist
  717. (delq top-cell font-lock-keywords-alist))))
  718. ;; Remember the keyword in case it is not local.
  719. (let ((cell (assq mode font-lock-removed-keywords-alist)))
  720. (if cell
  721. (unless (member keyword (cdr cell))
  722. (nconc cell (list keyword)))
  723. (push (cons mode (list keyword))
  724. font-lock-removed-keywords-alist))))))
  725. (t
  726. ;; Otherwise remove it immediately.
  727. (font-lock-set-defaults)
  728. (let ((was-compiled (eq (car font-lock-keywords) t)))
  729. ;; Bring back the user-level (uncompiled) keywords.
  730. (if was-compiled
  731. (setq font-lock-keywords (cadr font-lock-keywords)))
  732. ;; Edit them.
  733. (setq font-lock-keywords (copy-sequence font-lock-keywords))
  734. (dolist (keyword keywords)
  735. (setq font-lock-keywords
  736. (delete keyword font-lock-keywords)))
  737. ;; If the keywords were compiled before, compile them again.
  738. (if was-compiled
  739. (setq font-lock-keywords
  740. (font-lock-compile-keywords font-lock-keywords)))))))
  741. ;;; Font Lock Support mode.
  742. ;; This is the code used to interface font-lock.el with any of its add-on
  743. ;; packages, and provide the user interface. Packages that have their own
  744. ;; local buffer fontification functions (see below) may have to call
  745. ;; `font-lock-after-fontify-buffer' and/or `font-lock-after-unfontify-buffer'
  746. ;; themselves.
  747. (defcustom font-lock-support-mode 'jit-lock-mode
  748. "Support mode for Font Lock mode.
  749. Support modes speed up Font Lock mode by being choosy about when fontification
  750. occurs. The default support mode, Just-in-time Lock mode (symbol
  751. `jit-lock-mode'), is recommended.
  752. Other, older support modes are Fast Lock mode (symbol `fast-lock-mode') and
  753. Lazy Lock mode (symbol `lazy-lock-mode'). See those modes for more info.
  754. However, they are no longer recommended, as Just-in-time Lock mode is better.
  755. If nil, means support for Font Lock mode is never performed.
  756. If a symbol, use that support mode.
  757. If a list, each element should be of the form (MAJOR-MODE . SUPPORT-MODE),
  758. where MAJOR-MODE is a symbol or t (meaning the default). For example:
  759. ((c-mode . fast-lock-mode) (c++-mode . fast-lock-mode) (t . lazy-lock-mode))
  760. means that Fast Lock mode is used to support Font Lock mode for buffers in C or
  761. C++ modes, and Lazy Lock mode is used to support Font Lock mode otherwise.
  762. The value of this variable is used when Font Lock mode is turned on."
  763. :type '(choice (const :tag "none" nil)
  764. (const :tag "fast lock" fast-lock-mode)
  765. (const :tag "lazy lock" lazy-lock-mode)
  766. (const :tag "jit lock" jit-lock-mode)
  767. (repeat :menu-tag "mode specific" :tag "mode specific"
  768. :value ((t . jit-lock-mode))
  769. (cons :tag "Instance"
  770. (radio :tag "Mode"
  771. (const :tag "all" t)
  772. (symbol :tag "name"))
  773. (radio :tag "Support"
  774. (const :tag "none" nil)
  775. (const :tag "fast lock" fast-lock-mode)
  776. (const :tag "lazy lock" lazy-lock-mode)
  777. (const :tag "JIT lock" jit-lock-mode)))
  778. ))
  779. :version "21.1"
  780. :group 'font-lock)
  781. (defvar fast-lock-mode)
  782. (defvar lazy-lock-mode)
  783. (defvar jit-lock-mode)
  784. (declare-function fast-lock-after-fontify-buffer "fast-lock")
  785. (declare-function fast-lock-after-unfontify-buffer "fast-lock")
  786. (declare-function fast-lock-mode "fast-lock")
  787. (declare-function lazy-lock-after-fontify-buffer "lazy-lock")
  788. (declare-function lazy-lock-after-unfontify-buffer "lazy-lock")
  789. (declare-function lazy-lock-mode "lazy-lock")
  790. (defun font-lock-turn-on-thing-lock ()
  791. (pcase (font-lock-value-in-major-mode font-lock-support-mode)
  792. (`fast-lock-mode (fast-lock-mode t))
  793. (`lazy-lock-mode (lazy-lock-mode t))
  794. (`jit-lock-mode
  795. ;; Prepare for jit-lock
  796. (remove-hook 'after-change-functions
  797. #'font-lock-after-change-function t)
  798. (set (make-local-variable 'font-lock-flush-function)
  799. #'jit-lock-refontify)
  800. (set (make-local-variable 'font-lock-ensure-function)
  801. #'jit-lock-fontify-now)
  802. ;; Prevent font-lock-fontify-buffer from fontifying eagerly the whole
  803. ;; buffer. This is important for things like CWarn mode which
  804. ;; adds/removes a few keywords and does a refontify (which takes ages on
  805. ;; large files).
  806. (set (make-local-variable 'font-lock-fontify-buffer-function)
  807. #'jit-lock-refontify)
  808. ;; Don't fontify eagerly (and don't abort if the buffer is large).
  809. (set (make-local-variable 'font-lock-fontified) t)
  810. ;; Use jit-lock.
  811. (jit-lock-register #'font-lock-fontify-region
  812. (not font-lock-keywords-only))
  813. ;; Tell jit-lock how we extend the region to refontify.
  814. (add-hook 'jit-lock-after-change-extend-region-functions
  815. #'font-lock-extend-jit-lock-region-after-change
  816. nil t))))
  817. (defun font-lock-turn-off-thing-lock ()
  818. (cond ((bound-and-true-p fast-lock-mode)
  819. (fast-lock-mode -1))
  820. ((bound-and-true-p jit-lock-mode)
  821. (jit-lock-unregister 'font-lock-fontify-region)
  822. ;; Reset local vars to the non-jit-lock case.
  823. (kill-local-variable 'font-lock-fontify-buffer-function))
  824. ((bound-and-true-p lazy-lock-mode)
  825. (lazy-lock-mode -1))))
  826. (defun font-lock-after-fontify-buffer ()
  827. (cond ((bound-and-true-p fast-lock-mode)
  828. (fast-lock-after-fontify-buffer))
  829. ;; Useless now that jit-lock intercepts font-lock-fontify-buffer. -sm
  830. ;; (jit-lock-mode
  831. ;; (jit-lock-after-fontify-buffer))
  832. ((bound-and-true-p lazy-lock-mode)
  833. (lazy-lock-after-fontify-buffer))))
  834. (defun font-lock-after-unfontify-buffer ()
  835. (cond ((bound-and-true-p fast-lock-mode)
  836. (fast-lock-after-unfontify-buffer))
  837. ;; Useless as well. It's only called when:
  838. ;; - turning off font-lock: it does not matter if we leave spurious
  839. ;; `fontified' text props around since jit-lock-mode is also off.
  840. ;; - font-lock-default-fontify-buffer fails: this is not run
  841. ;; any more anyway. -sm
  842. ;;
  843. ;; (jit-lock-mode
  844. ;; (jit-lock-after-unfontify-buffer))
  845. ((bound-and-true-p lazy-lock-mode)
  846. (lazy-lock-after-unfontify-buffer))))
  847. ;;; End of Font Lock Support mode.
  848. ;;; Fontification functions.
  849. ;; Rather than the function, e.g., `font-lock-fontify-region' containing the
  850. ;; code to fontify a region, the function runs the function whose name is the
  851. ;; value of the variable, e.g., `font-lock-fontify-region-function'. Normally,
  852. ;; the value of this variable is, e.g., `font-lock-default-fontify-region'
  853. ;; which does contain the code to fontify a region. However, the value of the
  854. ;; variable could be anything and thus, e.g., `font-lock-fontify-region' could
  855. ;; do anything. The indirection of the fontification functions gives major
  856. ;; modes the capability of modifying the way font-lock.el fontifies. Major
  857. ;; modes can modify the values of, e.g., `font-lock-fontify-region-function',
  858. ;; via the variable `font-lock-defaults'.
  859. ;;
  860. ;; For example, Rmail mode sets the variable `font-lock-defaults' so that
  861. ;; font-lock.el uses its own function for buffer fontification. This function
  862. ;; makes fontification be on a message-by-message basis and so visiting an
  863. ;; RMAIL file is much faster. A clever implementation of the function might
  864. ;; fontify the headers differently than the message body. (It should, and
  865. ;; correspondingly for Mail mode, but I can't be bothered to do the work. Can
  866. ;; you?) This hints at a more interesting use...
  867. ;;
  868. ;; Languages that contain text normally contained in different major modes
  869. ;; could define their own fontification functions that treat text differently
  870. ;; depending on its context. For example, Perl mode could arrange that here
  871. ;; docs are fontified differently than Perl code. Or Yacc mode could fontify
  872. ;; rules one way and C code another. Neat!
  873. ;;
  874. ;; A further reason to use the fontification indirection feature is when the
  875. ;; default syntactic fontification, or the default fontification in general,
  876. ;; is not flexible enough for a particular major mode. For example, perhaps
  877. ;; comments are just too hairy for `font-lock-fontify-syntactically-region' to
  878. ;; cope with. You need to write your own version of that function, e.g.,
  879. ;; `hairy-fontify-syntactically-region', and make your own version of
  880. ;; `hairy-fontify-region' call that function before calling
  881. ;; `font-lock-fontify-keywords-region' for the normal regexp fontification
  882. ;; pass. And Hairy mode would set `font-lock-defaults' so that font-lock.el
  883. ;; would call your region fontification function instead of its own. For
  884. ;; example, TeX modes could fontify {\foo ...} and \bar{...} etc. multi-line
  885. ;; directives correctly and cleanly. (It is the same problem as fontifying
  886. ;; multi-line strings and comments; regexps are not appropriate for the job.)
  887. (defvar font-lock-extend-after-change-region-function nil
  888. "A function that determines the region to refontify after a change.
  889. This variable is either nil, or is a function that determines the
  890. region to refontify after a change.
  891. It is usually set by the major mode via `font-lock-defaults'.
  892. Font-lock calls this function after each buffer change.
  893. The function is given three parameters, the standard BEG, END, and OLD-LEN
  894. from `after-change-functions'. It should return either a cons of the beginning
  895. and end buffer positions \(in that order) of the region to refontify, or nil
  896. \(which directs the caller to fontify a default region).
  897. This function should preserve the match-data.
  898. The region it returns may start or end in the middle of a line.")
  899. (make-variable-buffer-local 'font-lock-extend-after-change-region-function)
  900. (defun font-lock-fontify-buffer (&optional interactively)
  901. "Fontify the current buffer the way the function `font-lock-mode' would."
  902. (declare
  903. ;; When called from Lisp, this function is a big mess. The caller usually
  904. ;; expects one of the following behaviors:
  905. ;; - refresh the highlighting (because the font-lock-keywords have been
  906. ;; changed).
  907. ;; - apply font-lock highlighting even if font-lock-mode is not enabled.
  908. ;; - reset the highlighting rules because font-lock-defaults
  909. ;; has been changed (and then rehighlight everything).
  910. ;; Of course, this function doesn't do all of the above in all situations
  911. ;; (e.g. depending on whether jit-lock is in use) and it can't guess what
  912. ;; the caller wants.
  913. (interactive-only "use `font-lock-ensure' or `font-lock-flush' instead."))
  914. (interactive "p")
  915. (font-lock-set-defaults)
  916. (let ((font-lock-verbose (or font-lock-verbose interactively)))
  917. (funcall font-lock-fontify-buffer-function)))
  918. (defun font-lock-unfontify-buffer ()
  919. (funcall font-lock-unfontify-buffer-function))
  920. (defun font-lock-fontify-region (beg end &optional loudly)
  921. "Fontify the text between BEG and END.
  922. If LOUDLY is non-nil, print status messages while fontifying.
  923. This works by calling `font-lock-fontify-region-function'."
  924. (font-lock-set-defaults)
  925. (funcall font-lock-fontify-region-function beg end loudly))
  926. (defun font-lock-unfontify-region (beg end)
  927. "Unfontify the text between BEG and END.
  928. This works by calling `font-lock-unfontify-region-function'."
  929. (save-buffer-state
  930. (funcall font-lock-unfontify-region-function beg end)))
  931. (defvar font-lock-flush-function #'font-lock-after-change-function
  932. "Function to use to mark a region for refontification.
  933. Called with two arguments BEG and END.")
  934. (defun font-lock-flush (&optional beg end)
  935. "Declare the region BEG...END's fontification as out-of-date.
  936. If the region is not specified, it defaults to the entire
  937. accessible portion of the current buffer."
  938. (and font-lock-mode
  939. font-lock-fontified
  940. (funcall font-lock-flush-function
  941. (or beg (point-min)) (or end (point-max)))))
  942. (defvar font-lock-ensure-function
  943. (lambda (_beg _end)
  944. (unless font-lock-fontified
  945. (font-lock-default-fontify-buffer)
  946. (unless font-lock-mode
  947. ;; If font-lock is not enabled, we don't have the hooks in place to
  948. ;; track modifications, so a subsequent call to font-lock-ensure can't
  949. ;; assume that the fontification is still valid.
  950. (setq font-lock-fontified nil))))
  951. "Function to make sure a region has been fontified.
  952. Called with two arguments BEG and END.")
  953. (defun font-lock-ensure (&optional beg end)
  954. "Make sure the region BEG...END has been fontified.
  955. If the region is not specified, it defaults to the entire accessible
  956. portion of the buffer."
  957. (font-lock-set-defaults)
  958. (funcall font-lock-ensure-function
  959. (or beg (point-min)) (or end (point-max))))
  960. (defun font-lock-default-fontify-buffer ()
  961. "Fontify the whole buffer using `font-lock-fontify-region-function'."
  962. (let ((verbose (if (numberp font-lock-verbose)
  963. (> (buffer-size) font-lock-verbose)
  964. font-lock-verbose)))
  965. (with-temp-message
  966. (when verbose
  967. (format "Fontifying %s..." (buffer-name)))
  968. ;; Make sure we fontify etc. in the whole buffer.
  969. (save-restriction
  970. (unless font-lock-dont-widen (widen))
  971. (condition-case nil
  972. (save-excursion
  973. (save-match-data
  974. (font-lock-fontify-region (point-min) (point-max) verbose)
  975. (font-lock-after-fontify-buffer)
  976. (setq font-lock-fontified t)))
  977. ;; We don't restore the old fontification, so it's best to unfontify.
  978. (quit (font-lock-unfontify-buffer)))))))
  979. (defun font-lock-default-unfontify-buffer ()
  980. "Unfontify the whole buffer using `font-lock-unfontify-region-function'."
  981. ;; Make sure we unfontify etc. in the whole buffer.
  982. (save-restriction
  983. (widen)
  984. (font-lock-unfontify-region (point-min) (point-max))
  985. (font-lock-after-unfontify-buffer)
  986. (setq font-lock-fontified nil)))
  987. (defvar font-lock-dont-widen nil
  988. "If non-nil, font-lock will work on the non-widened buffer.
  989. Useful for things like RMAIL and Info where the whole buffer is not
  990. a very meaningful entity to highlight.")
  991. (defvar font-lock-beg) (defvar font-lock-end)
  992. (defvar font-lock-extend-region-functions
  993. '(font-lock-extend-region-wholelines
  994. ;; This use of font-lock-multiline property is unreliable but is just
  995. ;; a handy heuristic: in case you don't have a function that does
  996. ;; /identification/ of multiline elements, you may still occasionally
  997. ;; discover them by accident (or you may /identify/ them but not in all
  998. ;; cases), in which case the font-lock-multiline property can help make
  999. ;; sure you will properly *re*identify them during refontification.
  1000. font-lock-extend-region-multiline)
  1001. "Special hook run just before proceeding to fontify a region.
  1002. This is used to allow major modes to help font-lock find safe buffer positions
  1003. as beginning and end of the fontified region. Its most common use is to solve
  1004. the problem of /identification/ of multiline elements by providing a function
  1005. that tries to find such elements and move the boundaries such that they do
  1006. not fall in the middle of one.
  1007. Each function is called with no argument; it is expected to adjust the
  1008. dynamically bound variables `font-lock-beg' and `font-lock-end'; and return
  1009. non-nil if it did make such an adjustment.
  1010. These functions are run in turn repeatedly until they all return nil.
  1011. Put first the functions more likely to cause a change and cheaper to compute.")
  1012. ;; Mark it as a special hook which doesn't use any global setting
  1013. ;; (i.e. doesn't obey the element t in the buffer-local value).
  1014. (make-variable-buffer-local 'font-lock-extend-region-functions)
  1015. (defun font-lock-extend-region-multiline ()
  1016. "Move fontification boundaries away from any `font-lock-multiline' property."
  1017. (let ((changed nil))
  1018. (when (and (> font-lock-beg (point-min))
  1019. (get-text-property (1- font-lock-beg) 'font-lock-multiline))
  1020. (setq changed t)
  1021. (setq font-lock-beg (or (previous-single-property-change
  1022. font-lock-beg 'font-lock-multiline)
  1023. (point-min))))
  1024. ;;
  1025. (when (get-text-property font-lock-end 'font-lock-multiline)
  1026. (setq changed t)
  1027. (setq font-lock-end (or (text-property-any font-lock-end (point-max)
  1028. 'font-lock-multiline nil)
  1029. (point-max))))
  1030. changed))
  1031. (defun font-lock-extend-region-wholelines ()
  1032. "Move fontification boundaries to beginning of lines."
  1033. (let ((changed nil))
  1034. (goto-char font-lock-beg)
  1035. (unless (bolp)
  1036. (setq changed t font-lock-beg
  1037. (let ((inhibit-field-text-motion t))
  1038. (line-beginning-position))))
  1039. (goto-char font-lock-end)
  1040. (unless (bolp)
  1041. (unless (eq font-lock-end
  1042. (setq font-lock-end (line-beginning-position 2)))
  1043. (setq changed t)))
  1044. changed))
  1045. (defun font-lock-default-fontify-region (beg end loudly)
  1046. "Fontify the text between BEG and END.
  1047. If LOUDLY is non-nil, print status messages while fontifying.
  1048. This function is the default `font-lock-fontify-region-function'."
  1049. (save-buffer-state
  1050. ;; Use the fontification syntax table, if any.
  1051. (with-syntax-table (or font-lock-syntax-table (syntax-table))
  1052. (save-restriction
  1053. (unless font-lock-dont-widen (widen))
  1054. ;; Extend the region to fontify so that it starts and ends at
  1055. ;; safe places.
  1056. (let ((funs font-lock-extend-region-functions)
  1057. (font-lock-beg beg)
  1058. (font-lock-end end))
  1059. (while funs
  1060. (setq funs (if (or (not (funcall (car funs)))
  1061. (eq funs font-lock-extend-region-functions))
  1062. (cdr funs)
  1063. ;; If there's been a change, we should go through
  1064. ;; the list again since this new position may
  1065. ;; warrant a different answer from one of the fun
  1066. ;; we've already seen.
  1067. font-lock-extend-region-functions)))
  1068. (setq beg font-lock-beg end font-lock-end))
  1069. ;; Now do the fontification.
  1070. (font-lock-unfontify-region beg end)
  1071. (when (and font-lock-syntactic-keywords
  1072. (null syntax-propertize-function))
  1073. ;; Ensure the beginning of the file is properly syntactic-fontified.
  1074. (let ((start beg))
  1075. (when (< font-lock-syntactically-fontified start)
  1076. (setq start (max font-lock-syntactically-fontified (point-min)))
  1077. (setq font-lock-syntactically-fontified end))
  1078. (font-lock-fontify-syntactic-keywords-region start end)))
  1079. (unless font-lock-keywords-only
  1080. (font-lock-fontify-syntactically-region beg end loudly))
  1081. (font-lock-fontify-keywords-region beg end loudly)
  1082. `(jit-lock-bounds ,beg . ,end)))))
  1083. ;; The following must be rethought, since keywords can override fontification.
  1084. ;; ;; Now scan for keywords, but not if we are inside a comment now.
  1085. ;; (or (and (not font-lock-keywords-only)
  1086. ;; (let ((state (parse-partial-sexp beg end nil nil
  1087. ;; font-lock-cache-state)))
  1088. ;; (or (nth 4 state) (nth 7 state))))
  1089. ;; (font-lock-fontify-keywords-region beg end))
  1090. (defvar font-lock-extra-managed-props nil
  1091. "Additional text properties managed by font-lock.
  1092. This is used by `font-lock-default-unfontify-region' to decide
  1093. what properties to clear before refontifying a region.")
  1094. (defun font-lock-default-unfontify-region (beg end)
  1095. "Unfontify the text between BEG and END.
  1096. This function is the default `font-lock-unfontify-region-function'."
  1097. (remove-list-of-text-properties
  1098. beg end (append
  1099. font-lock-extra-managed-props
  1100. (if font-lock-syntactic-keywords
  1101. '(syntax-table face font-lock-multiline)
  1102. '(face font-lock-multiline)))))
  1103. ;; Called when any modification is made to buffer text.
  1104. (defun font-lock-after-change-function (beg end &optional old-len)
  1105. (save-excursion
  1106. (let ((inhibit-point-motion-hooks t)
  1107. (inhibit-quit t)
  1108. (region (if font-lock-extend-after-change-region-function
  1109. (funcall font-lock-extend-after-change-region-function
  1110. beg end old-len))))
  1111. (save-match-data
  1112. (if region
  1113. ;; Fontify the region the major mode has specified.
  1114. (setq beg (car region) end (cdr region))
  1115. ;; Fontify the whole lines which enclose the region.
  1116. ;; Actually, this is not needed because
  1117. ;; font-lock-default-fontify-region already rounds up to a whole
  1118. ;; number of lines.
  1119. ;; (setq beg (progn (goto-char beg) (line-beginning-position))
  1120. ;; end (progn (goto-char end) (line-beginning-position 2)))
  1121. (unless (eq end (point-max))
  1122. ;; Rounding up to a whole number of lines should include the
  1123. ;; line right after `end'. Typical case: the first char of
  1124. ;; the line was deleted. Or a \n was inserted in the middle
  1125. ;; of a line.
  1126. (setq end (1+ end))))
  1127. (font-lock-fontify-region beg end)))))
  1128. (defvar jit-lock-start) (defvar jit-lock-end)
  1129. (defun font-lock-extend-jit-lock-region-after-change (beg end old-len)
  1130. "Function meant for `jit-lock-after-change-extend-region-functions'.
  1131. This function does 2 things:
  1132. - extend the region so that it not only includes the part that was modified
  1133. but also the surrounding text whose highlighting may change as a consequence.
  1134. - anticipate (part of) the region extension that will happen later in
  1135. `font-lock-default-fontify-region', in order to avoid the need for
  1136. double-redisplay in `jit-lock-fontify-now'."
  1137. (save-excursion
  1138. ;; First extend the region as font-lock-after-change-function would.
  1139. (let ((region (if font-lock-extend-after-change-region-function
  1140. (funcall font-lock-extend-after-change-region-function
  1141. beg end old-len))))
  1142. (if region
  1143. (setq beg (min jit-lock-start (car region))
  1144. end (max jit-lock-end (cdr region))))
  1145. ;; Then extend the region obeying font-lock-multiline properties,
  1146. ;; indicating which part of the buffer needs to be refontified.
  1147. ;; !!! This is the *main* user of font-lock-multiline property !!!
  1148. ;; font-lock-after-change-function could/should also do that, but it
  1149. ;; doesn't need to because font-lock-default-fontify-region does
  1150. ;; it anyway. Here OTOH we have no guarantee that
  1151. ;; font-lock-default-fontify-region will be executed on this region
  1152. ;; any time soon.
  1153. ;; Note: contrary to font-lock-default-fontify-region, we do not do
  1154. ;; any loop here because we are not looking for a safe spot: we just
  1155. ;; mark the text whose appearance may need to change as a result of
  1156. ;; the buffer modification.
  1157. (when (and (> beg (point-min))
  1158. (get-text-property (1- beg) 'font-lock-multiline))
  1159. (setq beg (or (previous-single-property-change
  1160. beg 'font-lock-multiline)
  1161. (point-min))))
  1162. (when (< end (point-max))
  1163. (setq end
  1164. (cond
  1165. ((get-text-property end 'font-lock-multiline)
  1166. (or (text-property-any end (point-max)
  1167. 'font-lock-multiline nil)
  1168. (point-max)))
  1169. ;; If `end' has been set by the function above, don't corrupt it.
  1170. (font-lock-extend-after-change-region-function end)
  1171. ;; Rounding up to a whole number of lines should include the
  1172. ;; line right after `end'. Typical case: the first char of
  1173. ;; the line was deleted. Or a \n was inserted in the middle
  1174. ;; of a line.
  1175. (t (1+ end)))))
  1176. ;; Finally, pre-enlarge the region to a whole number of lines, to try
  1177. ;; and anticipate what font-lock-default-fontify-region will do, so as to
  1178. ;; avoid double-redisplay.
  1179. ;; We could just run `font-lock-extend-region-functions', but since
  1180. ;; the only purpose is to avoid the double-redisplay, we prefer to
  1181. ;; do here only the part that is cheap and most likely to be useful.
  1182. (when (memq 'font-lock-extend-region-wholelines
  1183. font-lock-extend-region-functions)
  1184. (goto-char beg)
  1185. (setq beg (min jit-lock-start (line-beginning-position)))
  1186. (goto-char end)
  1187. (setq end
  1188. (max jit-lock-end
  1189. (if (bolp) (point) (line-beginning-position 2)))))
  1190. (setq jit-lock-start beg
  1191. jit-lock-end end))))
  1192. (defun font-lock-fontify-block (&optional arg)
  1193. "Fontify some lines the way `font-lock-fontify-buffer' would.
  1194. The lines could be a function or paragraph, or a specified number of lines.
  1195. If ARG is given, fontify that many lines before and after point, or 16 lines if
  1196. no ARG is given and `font-lock-mark-block-function' is nil.
  1197. If `font-lock-mark-block-function' non-nil and no ARG is given, it is used to
  1198. delimit the region to fontify."
  1199. (interactive "P")
  1200. (let ((inhibit-point-motion-hooks t)
  1201. deactivate-mark)
  1202. ;; Make sure we have the right `font-lock-keywords' etc.
  1203. (if (not font-lock-mode) (font-lock-set-defaults))
  1204. (save-mark-and-excursion
  1205. (save-match-data
  1206. (condition-case error-data
  1207. (if (or arg (not font-lock-mark-block-function))
  1208. (let ((lines (if arg (prefix-numeric-value arg) 16)))
  1209. (font-lock-fontify-region
  1210. (save-excursion (forward-line (- lines)) (point))
  1211. (save-excursion (forward-line lines) (point))))
  1212. (funcall font-lock-mark-block-function)
  1213. (font-lock-fontify-region (point) (mark)))
  1214. ((error quit) (message "Fontifying block...%s" error-data)))))))
  1215. ;;; End of Fontification functions.
  1216. ;;; Additional text property functions.
  1217. ;; The following text property functions should be builtins. This means they
  1218. ;; should be written in C and put with all the other text property functions.
  1219. ;; In the meantime, those that are used by font-lock.el are defined in Lisp
  1220. ;; below and given a `font-lock-' prefix. Those that are not used are defined
  1221. ;; in Lisp below and commented out. sm.
  1222. (defun font-lock-prepend-text-property (start end prop value &optional object)
  1223. "Prepend to one property of the text from START to END.
  1224. Arguments PROP and VALUE specify the property and value to prepend to the value
  1225. already in place. The resulting property values are always lists.
  1226. Optional argument OBJECT is the string or buffer containing the text."
  1227. (let ((val (if (listp value) value (list value))) next prev)
  1228. (while (/= start end)
  1229. (setq next (next-single-property-change start prop object end)
  1230. prev (get-text-property start prop object))
  1231. ;; Canonicalize old forms of face property.
  1232. (and (memq prop '(face font-lock-face))
  1233. (listp prev)
  1234. (or (keywordp (car prev))
  1235. (memq (car prev) '(foreground-color background-color)))
  1236. (setq prev (list prev)))
  1237. (put-text-property start next prop
  1238. (append val (if (listp prev) prev (list prev)))
  1239. object)
  1240. (setq start next))))
  1241. (defun font-lock-append-text-property (start end prop value &optional object)
  1242. "Append to one property of the text from START to END.
  1243. Arguments PROP and VALUE specify the property and value to append to the value
  1244. already in place. The resulting property values are always lists.
  1245. Optional argument OBJECT is the string or buffer containing the text."
  1246. (let ((val (if (listp value) value (list value))) next prev)
  1247. (while (/= start end)
  1248. (setq next (next-single-property-change start prop object end)
  1249. prev (get-text-property start prop object))
  1250. ;; Canonicalize old forms of face property.
  1251. (and (memq prop '(face font-lock-face))
  1252. (listp prev)
  1253. (or (keywordp (car prev))
  1254. (memq (car prev) '(foreground-color background-color)))
  1255. (setq prev (list prev)))
  1256. (put-text-property start next prop
  1257. (append (if (listp prev) prev (list prev)) val)
  1258. object)
  1259. (setq start next))))
  1260. (defun font-lock-fillin-text-property (start end prop value &optional object)
  1261. "Fill in one property of the text from START to END.
  1262. Arguments PROP and VALUE specify the property and value to put where none are
  1263. already in place. Therefore existing property values are not overwritten.
  1264. Optional argument OBJECT is the string or buffer containing the text."
  1265. (let ((start (text-property-any start end prop nil object)) next)
  1266. (while start
  1267. (setq next (next-single-property-change start prop object end))
  1268. (put-text-property start next prop value object)
  1269. (setq start (text-property-any next end prop nil object)))))
  1270. (defun font-lock--remove-face-from-text-property (start
  1271. end
  1272. prop value &optional object)
  1273. "Remove a specific property value from text from START to END.
  1274. Arguments PROP and VALUE specify the property and value to remove. The
  1275. resulting property values are not `eq' to VALUE nor lists containing VALUE.
  1276. Optional argument OBJECT is the string or buffer containing the text."
  1277. (let ((start (text-property-not-all start end prop nil object)) next prev)
  1278. (while start
  1279. (setq next (next-single-property-change start prop object end)
  1280. prev (get-text-property start prop object))
  1281. (cond ((or (atom prev)
  1282. (keywordp (car prev))
  1283. (eq (car prev) 'foreground-color)
  1284. (eq (car prev) 'background-color))
  1285. (when (eq value prev)
  1286. (remove-list-of-text-properties start next (list prop) object)))
  1287. ((memq value prev) ;Assume prev is not dotted.
  1288. (let ((new (remq value prev)))
  1289. (cond ((null new)
  1290. (remove-list-of-text-properties start next (list prop)
  1291. object))
  1292. ((= (length new) 1)
  1293. (put-text-property start next prop (car new) object))
  1294. (t
  1295. (put-text-property start next prop new object))))))
  1296. (setq start (text-property-not-all next end prop nil object)))))
  1297. ;;; End of Additional text property functions.
  1298. ;;; Syntactic regexp fontification functions.
  1299. ;; These syntactic keyword pass functions are identical to those keyword pass
  1300. ;; functions below, with the following exceptions; (a) they operate on
  1301. ;; `font-lock-syntactic-keywords' of course, (b) they are all `defun' as speed
  1302. ;; is less of an issue, (c) eval of property value does not occur JIT as speed
  1303. ;; is less of an issue, (d) OVERRIDE cannot be `prepend' or `append' as it
  1304. ;; makes no sense for `syntax-table' property values, (e) they do not do it
  1305. ;; LOUDLY as it is not likely to be intensive.
  1306. (defun font-lock-apply-syntactic-highlight (highlight)
  1307. "Apply HIGHLIGHT following a match.
  1308. HIGHLIGHT should be of the form MATCH-HIGHLIGHT,
  1309. see `font-lock-syntactic-keywords'."
  1310. (let* ((match (nth 0 highlight))
  1311. (start (match-beginning match)) (end (match-end match))
  1312. (value (nth 1 highlight))
  1313. (override (nth 2 highlight)))
  1314. (if (not start)
  1315. ;; No match but we might not signal an error.
  1316. (or (nth 3 highlight)
  1317. (error "No match %d in highlight %S" match highlight))
  1318. (when (and (consp value) (not (numberp (car value))))
  1319. (setq value (eval value)))
  1320. (when (stringp value) (setq value (string-to-syntax value)))
  1321. ;; Flush the syntax-cache. I believe this is not necessary for
  1322. ;; font-lock's use of syntax-ppss, but I'm not 100% sure and it can
  1323. ;; still be necessary for other users of syntax-ppss anyway.
  1324. (syntax-ppss-after-change-function start)
  1325. (cond
  1326. ((not override)
  1327. ;; Cannot override existing fontification.
  1328. (or (text-property-not-all start end 'syntax-table nil)
  1329. (put-text-property start end 'syntax-table value)))
  1330. ((eq override t)
  1331. ;; Override existing fontification.
  1332. (put-text-property start end 'syntax-table value))
  1333. ((eq override 'keep)
  1334. ;; Keep existing fontification.
  1335. (font-lock-fillin-text-property start end 'syntax-table value))))))
  1336. (defun font-lock-fontify-syntactic-anchored-keywords (keywords limit)
  1337. "Fontify according to KEYWORDS until LIMIT.
  1338. KEYWORDS should be of the form MATCH-ANCHORED, see `font-lock-keywords',
  1339. LIMIT can be modified by the value of its PRE-MATCH-FORM."
  1340. (let ((matcher (nth 0 keywords)) (lowdarks (nthcdr 3 keywords)) highlights
  1341. ;; Evaluate PRE-MATCH-FORM.
  1342. (pre-match-value (eval (nth 1 keywords))))
  1343. ;; Set LIMIT to value of PRE-MATCH-FORM or the end of line.
  1344. (if (and (numberp pre-match-value) (> pre-match-value (point)))
  1345. (setq limit pre-match-value)
  1346. (setq limit (line-end-position)))
  1347. (save-match-data
  1348. ;; Find an occurrence of `matcher' before `limit'.
  1349. (while (if (stringp matcher)
  1350. (re-search-forward matcher limit t)
  1351. (funcall matcher limit))
  1352. ;; Apply each highlight to this instance of `matcher'.
  1353. (setq highlights lowdarks)
  1354. (while highlights
  1355. (font-lock-apply-syntactic-highlight (car highlights))
  1356. (setq highlights (cdr highlights)))))
  1357. ;; Evaluate POST-MATCH-FORM.
  1358. (eval (nth 2 keywords))))
  1359. (defun font-lock-fontify-syntactic-keywords-region (start end)
  1360. "Fontify according to `font-lock-syntactic-keywords' between START and END.
  1361. START should be at the beginning of a line."
  1362. (unless parse-sexp-lookup-properties
  1363. ;; We wouldn't go through so much trouble if we didn't intend to use those
  1364. ;; properties, would we?
  1365. (set (make-local-variable 'parse-sexp-lookup-properties) t))
  1366. ;; If `font-lock-syntactic-keywords' is a symbol, get the real keywords.
  1367. (when (symbolp font-lock-syntactic-keywords)
  1368. (setq font-lock-syntactic-keywords (font-lock-eval-keywords
  1369. font-lock-syntactic-keywords)))
  1370. ;; If `font-lock-syntactic-keywords' is not compiled, compile it.
  1371. (unless (eq (car font-lock-syntactic-keywords) t)
  1372. (setq font-lock-syntactic-keywords (font-lock-compile-keywords
  1373. font-lock-syntactic-keywords
  1374. t)))
  1375. ;; Get down to business.
  1376. (let ((case-fold-search font-lock-keywords-case-fold-search)
  1377. (keywords (cddr font-lock-syntactic-keywords))
  1378. keyword matcher highlights)
  1379. (while keywords
  1380. ;; Find an occurrence of `matcher' from `start' to `end'.
  1381. (setq keyword (car keywords) matcher (car keyword))
  1382. (goto-char start)
  1383. (while (and (< (point) end)
  1384. (if (stringp matcher)
  1385. (re-search-forward matcher end t)
  1386. (funcall matcher end)))
  1387. ;; Apply each highlight to this instance of `matcher', which may be
  1388. ;; specific highlights or more keywords anchored to `matcher'.
  1389. (setq highlights (cdr keyword))
  1390. (while highlights
  1391. (if (numberp (car (car highlights)))
  1392. (font-lock-apply-syntactic-highlight (car highlights))
  1393. (font-lock-fontify-syntactic-anchored-keywords (car highlights)
  1394. end))
  1395. (setq highlights (cdr highlights))))
  1396. (setq keywords (cdr keywords)))))
  1397. ;;; End of Syntactic regexp fontification functions.
  1398. ;;; Syntactic fontification functions.
  1399. (defvar font-lock-comment-start-skip nil
  1400. "If non-nil, Font Lock mode uses this instead of `comment-start-skip'.")
  1401. (defvar font-lock-comment-end-skip nil
  1402. "If non-nil, Font Lock mode uses this instead of `comment-end'.")
  1403. (defun font-lock-fontify-syntactically-region (start end &optional loudly)
  1404. "Put proper face on each string and comment between START and END.
  1405. START should be at the beginning of a line."
  1406. (syntax-propertize end) ; Apply any needed syntax-table properties.
  1407. (with-syntax-table (or syntax-ppss-table (syntax-table))
  1408. (let ((comment-end-regexp
  1409. (or font-lock-comment-end-skip
  1410. (regexp-quote
  1411. (replace-regexp-in-string "^ *" "" comment-end))))
  1412. ;; Find the `start' state.
  1413. (state (syntax-ppss start))
  1414. face beg)
  1415. (if loudly (message "Fontifying %s... (syntactically...)" (buffer-name)))
  1416. ;;
  1417. ;; Find each interesting place between here and `end'.
  1418. (while
  1419. (progn
  1420. (when (or (nth 3 state) (nth 4 state))
  1421. (setq face (funcall font-lock-syntactic-face-function state))
  1422. (setq beg (max (nth 8 state) start))
  1423. (setq state (parse-partial-sexp (point) end nil nil state
  1424. 'syntax-table))
  1425. (when face (put-text-property beg (point) 'face face))
  1426. (when (and (eq face 'font-lock-comment-face)
  1427. (or font-lock-comment-start-skip
  1428. comment-start-skip))
  1429. ;; Find the comment delimiters
  1430. ;; and use font-lock-comment-delimiter-face for them.
  1431. (save-excursion
  1432. (goto-char beg)
  1433. (if (looking-at (or font-lock-comment-start-skip
  1434. comment-start-skip))
  1435. (put-text-property beg (match-end 0) 'face
  1436. font-lock-comment-delimiter-face)))
  1437. (if (looking-back comment-end-regexp (point-at-bol) t)
  1438. (put-text-property (match-beginning 0) (point) 'face
  1439. font-lock-comment-delimiter-face))))
  1440. (< (point) end))
  1441. (setq state (parse-partial-sexp (point) end nil nil state
  1442. 'syntax-table))))))
  1443. ;;; End of Syntactic fontification functions.
  1444. ;;; Keyword regexp fontification functions.
  1445. (defsubst font-lock-apply-highlight (highlight)
  1446. "Apply HIGHLIGHT following a match.
  1447. HIGHLIGHT should be of the form MATCH-HIGHLIGHT, see `font-lock-keywords'."
  1448. (let* ((match (nth 0 highlight))
  1449. (start (match-beginning match)) (end (match-end match))
  1450. (override (nth 2 highlight)))
  1451. (if (not start)
  1452. ;; No match but we might not signal an error.
  1453. (or (nth 3 highlight)
  1454. (error "No match %d in highlight %S" match highlight))
  1455. (let ((val (eval (nth 1 highlight))))
  1456. (when (eq (car-safe val) 'face)
  1457. (add-text-properties start end (cddr val))
  1458. (setq val (cadr val)))
  1459. (cond
  1460. ((not (or val (eq override t)))
  1461. ;; If `val' is nil, don't do anything. It is important to do it
  1462. ;; explicitly, because when adding nil via things like
  1463. ;; font-lock-append-text-property, the property is actually
  1464. ;; changed from <face> to (<face>) which is undesirable. --Stef
  1465. nil)
  1466. ((not override)
  1467. ;; Cannot override existing fontification.
  1468. (or (text-property-not-all start end 'face nil)
  1469. (put-text-property start end 'face val)))
  1470. ((eq override t)
  1471. ;; Override existing fontification.
  1472. (put-text-property start end 'face val))
  1473. ((eq override 'prepend)
  1474. ;; Prepend to existing fontification.
  1475. (font-lock-prepend-text-property start end 'face val))
  1476. ((eq override 'append)
  1477. ;; Append to existing fontification.
  1478. (font-lock-append-text-property start end 'face val))
  1479. ((eq override 'keep)
  1480. ;; Keep existing fontification.
  1481. (font-lock-fillin-text-property start end 'face val)))))))
  1482. (defsubst font-lock-fontify-anchored-keywords (keywords limit)
  1483. "Fontify according to KEYWORDS until LIMIT.
  1484. KEYWORDS should be of the form MATCH-ANCHORED, see `font-lock-keywords',
  1485. LIMIT can be modified by the value of its PRE-MATCH-FORM."
  1486. (let ((matcher (nth 0 keywords)) (lowdarks (nthcdr 3 keywords)) highlights
  1487. (lead-start (match-beginning 0))
  1488. ;; Evaluate PRE-MATCH-FORM.
  1489. (pre-match-value (eval (nth 1 keywords))))
  1490. ;; Set LIMIT to value of PRE-MATCH-FORM or the end of line.
  1491. (if (not (and (numberp pre-match-value) (> pre-match-value (point))))
  1492. (setq limit (line-end-position))
  1493. (setq limit pre-match-value)
  1494. (when (and font-lock-multiline (>= limit (line-beginning-position 2)))
  1495. ;; this is a multiline anchored match
  1496. ;; (setq font-lock-multiline t)
  1497. (put-text-property (if (= limit (line-beginning-position 2))
  1498. (1- limit)
  1499. (min lead-start (point)))
  1500. limit
  1501. 'font-lock-multiline t)))
  1502. (save-match-data
  1503. ;; Find an occurrence of `matcher' before `limit'.
  1504. (while (and (< (point) limit)
  1505. (if (stringp matcher)
  1506. (re-search-forward matcher limit t)
  1507. (funcall matcher limit)))
  1508. ;; Apply each highlight to this instance of `matcher'.
  1509. (setq highlights lowdarks)
  1510. (while highlights
  1511. (font-lock-apply-highlight (car highlights))
  1512. (setq highlights (cdr highlights)))))
  1513. ;; Evaluate POST-MATCH-FORM.
  1514. (eval (nth 2 keywords))))
  1515. (defun font-lock-fontify-keywords-region (start end &optional loudly)
  1516. "Fontify according to `font-lock-keywords' between START and END.
  1517. START should be at the beginning of a line.
  1518. LOUDLY, if non-nil, allows progress-meter bar."
  1519. (unless (eq (car font-lock-keywords) t)
  1520. (setq font-lock-keywords
  1521. (font-lock-compile-keywords font-lock-keywords)))
  1522. (let ((case-fold-search font-lock-keywords-case-fold-search)
  1523. (keywords (cddr font-lock-keywords))
  1524. (bufname (buffer-name)) (count 0)
  1525. (pos (make-marker))
  1526. keyword matcher highlights)
  1527. ;;
  1528. ;; Fontify each item in `font-lock-keywords' from `start' to `end'.
  1529. (while keywords
  1530. (if loudly (message "Fontifying %s... (regexps..%s)" bufname
  1531. (make-string (cl-incf count) ?.)))
  1532. ;;
  1533. ;; Find an occurrence of `matcher' from `start' to `end'.
  1534. (setq keyword (car keywords) matcher (car keyword))
  1535. (goto-char start)
  1536. (while (and (< (point) end)
  1537. (if (stringp matcher)
  1538. (re-search-forward matcher end t)
  1539. (funcall matcher end))
  1540. ;; Beware empty string matches since they will
  1541. ;; loop indefinitely.
  1542. (or (> (point) (match-beginning 0))
  1543. (progn (forward-char 1) t)))
  1544. (when (and font-lock-multiline
  1545. (>= (point)
  1546. (save-excursion (goto-char (match-beginning 0))
  1547. (forward-line 1) (point))))
  1548. ;; this is a multiline regexp match
  1549. ;; (setq font-lock-multiline t)
  1550. (put-text-property (if (= (point)
  1551. (save-excursion
  1552. (goto-char (match-beginning 0))
  1553. (forward-line 1) (point)))
  1554. (1- (point))
  1555. (match-beginning 0))
  1556. (point)
  1557. 'font-lock-multiline t))
  1558. ;; Apply each highlight to this instance of `matcher', which may be
  1559. ;; specific highlights or more keywords anchored to `matcher'.
  1560. (setq highlights (cdr keyword))
  1561. (while highlights
  1562. (if (numberp (car (car highlights)))
  1563. (font-lock-apply-highlight (car highlights))
  1564. (set-marker pos (point))
  1565. (font-lock-fontify-anchored-keywords (car highlights) end)
  1566. ;; Ensure forward progress. `pos' is a marker because anchored
  1567. ;; keyword may add/delete text (this happens e.g. in grep.el).
  1568. (if (< (point) pos) (goto-char pos)))
  1569. (setq highlights (cdr highlights))))
  1570. (setq keywords (cdr keywords)))
  1571. (set-marker pos nil)))
  1572. ;;; End of Keyword regexp fontification functions.
  1573. ;; Various functions.
  1574. (defun font-lock-compile-keywords (keywords &optional syntactic-keywords)
  1575. "Compile KEYWORDS into the form (t KEYWORDS COMPILED...)
  1576. Here each COMPILED is of the form (MATCHER HIGHLIGHT ...) as shown in the
  1577. `font-lock-keywords' doc string.
  1578. If SYNTACTIC-KEYWORDS is non-nil, it means these keywords are used for
  1579. `font-lock-syntactic-keywords' rather than for `font-lock-keywords'."
  1580. (if (not font-lock-set-defaults)
  1581. ;; This should never happen. But some external packages sometimes
  1582. ;; call font-lock in unexpected and incorrect ways. It's important to
  1583. ;; stop processing at this point, otherwise we may end up changing the
  1584. ;; global value of font-lock-keywords and break highlighting in many
  1585. ;; other buffers.
  1586. (error "Font-lock trying to use keywords before setting them up"))
  1587. (if (eq (car-safe keywords) t)
  1588. keywords
  1589. (setq keywords
  1590. (cons t (cons keywords
  1591. (mapcar #'font-lock-compile-keyword keywords))))
  1592. (if (and (not syntactic-keywords)
  1593. (let ((beg-function syntax-begin-function))
  1594. (or (eq beg-function #'beginning-of-defun)
  1595. (if (symbolp beg-function)
  1596. (get beg-function 'font-lock-syntax-paren-check))))
  1597. (not beginning-of-defun-function))
  1598. ;; Try to detect when a string or comment contains something that
  1599. ;; looks like a defun and would thus confuse font-lock.
  1600. (nconc keywords
  1601. `((,(if defun-prompt-regexp
  1602. (concat "^\\(?:" defun-prompt-regexp "\\)?\\s(")
  1603. "^\\s(")
  1604. (0
  1605. (if (memq (get-text-property (match-beginning 0) 'face)
  1606. '(font-lock-string-face font-lock-doc-face
  1607. font-lock-comment-face))
  1608. (list 'face font-lock-warning-face
  1609. 'help-echo "Looks like a toplevel defun: escape the parenthesis"))
  1610. prepend)))))
  1611. keywords))
  1612. (defun font-lock-compile-keyword (keyword)
  1613. (cond ((or (functionp keyword) (nlistp keyword)) ; MATCHER
  1614. (list keyword '(0 font-lock-keyword-face)))
  1615. ((eq (car keyword) 'eval) ; (eval . FORM)
  1616. (font-lock-compile-keyword (eval (cdr keyword))))
  1617. ((eq (car-safe (cdr keyword)) 'quote) ; (MATCHER . 'FORM)
  1618. ;; If FORM is a FACENAME then quote it. Otherwise ignore the quote.
  1619. (if (symbolp (nth 2 keyword))
  1620. (list (car keyword) (list 0 (cdr keyword)))
  1621. (font-lock-compile-keyword (cons (car keyword) (nth 2 keyword)))))
  1622. ((numberp (cdr keyword)) ; (MATCHER . MATCH)
  1623. (list (car keyword) (list (cdr keyword) 'font-lock-keyword-face)))
  1624. ((symbolp (cdr keyword)) ; (MATCHER . FACENAME)
  1625. (list (car keyword) (list 0 (cdr keyword))))
  1626. ((nlistp (nth 1 keyword)) ; (MATCHER . HIGHLIGHT)
  1627. (list (car keyword) (cdr keyword)))
  1628. (t ; (MATCHER HIGHLIGHT ...)
  1629. keyword)))
  1630. (defun font-lock-eval-keywords (keywords)
  1631. "Evaluate KEYWORDS if a function (funcall) or variable (eval) name."
  1632. (if (listp keywords)
  1633. keywords
  1634. (font-lock-eval-keywords (if (fboundp keywords)
  1635. (funcall keywords)
  1636. (eval keywords)))))
  1637. (defun font-lock-value-in-major-mode (values)
  1638. "If VALUES is an list, use `major-mode' as a key and return the `assq' value.
  1639. VALUES should then be an alist on the form ((MAJOR-MODE . VALUE) ...) where
  1640. MAJOR-MODE may be t.
  1641. If VALUES isn't a list, return VALUES."
  1642. (if (consp values)
  1643. (cdr (or (assq major-mode values) (assq t values)))
  1644. values))
  1645. (defun font-lock-choose-keywords (keywords level)
  1646. "Return LEVELth element of KEYWORDS.
  1647. A LEVEL of nil is equal to a LEVEL of 0, a LEVEL of t is equal to
  1648. \(1- (length KEYWORDS))."
  1649. (cond ((not (and (listp keywords) (symbolp (car keywords))))
  1650. keywords)
  1651. ((numberp level)
  1652. (or (nth level keywords) (car (last keywords))))
  1653. ((eq level t)
  1654. (car (last keywords)))
  1655. (t
  1656. (car keywords))))
  1657. (defun font-lock-refresh-defaults ()
  1658. "Restart fontification in current buffer after recomputing from defaults.
  1659. Recompute fontification variables using `font-lock-defaults' and
  1660. `font-lock-maximum-decoration'. Then restart fontification.
  1661. Use this function when you have changed any of the above
  1662. variables directly.
  1663. Note: This function will erase modifications done by
  1664. `font-lock-add-keywords' or `font-lock-remove-keywords', but will
  1665. preserve `hi-lock-mode' highlighting patterns."
  1666. (font-lock-mode -1)
  1667. (kill-local-variable 'font-lock-set-defaults)
  1668. (font-lock-mode 1))
  1669. (defvar font-lock-major-mode nil
  1670. "Major mode for which the font-lock settings have been setup.")
  1671. (make-variable-buffer-local 'font-lock-major-mode)
  1672. (defun font-lock-set-defaults ()
  1673. "Set fontification defaults appropriately for this mode.
  1674. Sets various variables using `font-lock-defaults' and
  1675. `font-lock-maximum-decoration'."
  1676. ;; Set fontification defaults if not previously set for correct major mode.
  1677. (unless (and font-lock-set-defaults
  1678. (eq font-lock-major-mode major-mode))
  1679. (setq font-lock-major-mode major-mode)
  1680. (setq font-lock-set-defaults t)
  1681. (let* ((defaults font-lock-defaults)
  1682. (keywords
  1683. (font-lock-choose-keywords (nth 0 defaults)
  1684. (font-lock-value-in-major-mode font-lock-maximum-decoration)))
  1685. (local (cdr (assq major-mode font-lock-keywords-alist)))
  1686. (removed-keywords
  1687. (cdr-safe (assq major-mode font-lock-removed-keywords-alist))))
  1688. ;; Syntactic fontification?
  1689. (if (nth 1 defaults)
  1690. (set (make-local-variable 'font-lock-keywords-only) t)
  1691. (kill-local-variable 'font-lock-keywords-only))
  1692. ;; Case fold during regexp fontification?
  1693. (if (nth 2 defaults)
  1694. (set (make-local-variable 'font-lock-keywords-case-fold-search) t)
  1695. (kill-local-variable 'font-lock-keywords-case-fold-search))
  1696. ;; Syntax table for regexp and syntactic fontification?
  1697. (if (null (nth 3 defaults))
  1698. (kill-local-variable 'font-lock-syntax-table)
  1699. (set (make-local-variable 'font-lock-syntax-table)
  1700. (copy-syntax-table (syntax-table)))
  1701. (dolist (selem (nth 3 defaults))
  1702. ;; The character to modify may be a single CHAR or a STRING.
  1703. (let ((syntax (cdr selem)))
  1704. (dolist (char (if (numberp (car selem))
  1705. (list (car selem))
  1706. (mapcar #'identity (car selem))))
  1707. (modify-syntax-entry char syntax font-lock-syntax-table)))))
  1708. ;; (nth 4 defaults) used to hold `font-lock-beginning-of-syntax-function',
  1709. ;; but that was removed in 25.1, so if it's a cons cell, we assume that
  1710. ;; it's part of the variable alist.
  1711. ;; Variable alist?
  1712. (dolist (x (nthcdr (if (consp (nth 4 defaults)) 4 5) defaults))
  1713. (set (make-local-variable (car x)) (cdr x)))
  1714. ;; Set up `font-lock-keywords' last because its value might depend
  1715. ;; on other settings.
  1716. (set (make-local-variable 'font-lock-keywords)
  1717. (font-lock-eval-keywords keywords))
  1718. ;; Local fontification?
  1719. (while local
  1720. (font-lock-add-keywords nil (car (car local)) (cdr (car local)))
  1721. (setq local (cdr local)))
  1722. (when removed-keywords
  1723. (font-lock-remove-keywords nil removed-keywords))
  1724. ;; Now compile the keywords.
  1725. (unless (eq (car font-lock-keywords) t)
  1726. (setq font-lock-keywords
  1727. (font-lock-compile-keywords font-lock-keywords))))
  1728. (font-lock-flush)))
  1729. ;;; Color etc. support.
  1730. ;; Note that `defface' will not overwrite any faces declared above via
  1731. ;; `custom-declare-face'.
  1732. (defface font-lock-comment-face
  1733. '((((class grayscale) (background light))
  1734. :foreground "DimGray" :weight bold :slant italic)
  1735. (((class grayscale) (background dark))
  1736. :foreground "LightGray" :weight bold :slant italic)
  1737. (((class color) (min-colors 88) (background light))
  1738. :foreground "Firebrick")
  1739. (((class color) (min-colors 88) (background dark))
  1740. :foreground "chocolate1")
  1741. (((class color) (min-colors 16) (background light))
  1742. :foreground "red")
  1743. (((class color) (min-colors 16) (background dark))
  1744. :foreground "red1")
  1745. (((class color) (min-colors 8) (background light))
  1746. :foreground "red")
  1747. (((class color) (min-colors 8) (background dark))
  1748. :foreground "yellow")
  1749. (t :weight bold :slant italic))
  1750. "Font Lock mode face used to highlight comments."
  1751. :group 'font-lock-faces)
  1752. (defface font-lock-comment-delimiter-face
  1753. '((default :inherit font-lock-comment-face))
  1754. "Font Lock mode face used to highlight comment delimiters."
  1755. :group 'font-lock-faces)
  1756. (defface font-lock-string-face
  1757. '((((class grayscale) (background light)) :foreground "DimGray" :slant italic)
  1758. (((class grayscale) (background dark)) :foreground "LightGray" :slant italic)
  1759. (((class color) (min-colors 88) (background light)) :foreground "VioletRed4")
  1760. (((class color) (min-colors 88) (background dark)) :foreground "LightSalmon")
  1761. (((class color) (min-colors 16) (background light)) :foreground "RosyBrown")
  1762. (((class color) (min-colors 16) (background dark)) :foreground "LightSalmon")
  1763. (((class color) (min-colors 8)) :foreground "green")
  1764. (t :slant italic))
  1765. "Font Lock mode face used to highlight strings."
  1766. :group 'font-lock-faces)
  1767. (defface font-lock-doc-face
  1768. '((t :inherit font-lock-string-face))
  1769. "Font Lock mode face used to highlight documentation."
  1770. :group 'font-lock-faces)
  1771. (defface font-lock-keyword-face
  1772. '((((class grayscale) (background light)) :foreground "LightGray" :weight bold)
  1773. (((class grayscale) (background dark)) :foreground "DimGray" :weight bold)
  1774. (((class color) (min-colors 88) (background light)) :foreground "Purple")
  1775. (((class color) (min-colors 88) (background dark)) :foreground "Cyan1")
  1776. (((class color) (min-colors 16) (background light)) :foreground "Purple")
  1777. (((class color) (min-colors 16) (background dark)) :foreground "Cyan")
  1778. (((class color) (min-colors 8)) :foreground "cyan" :weight bold)
  1779. (t :weight bold))
  1780. "Font Lock mode face used to highlight keywords."
  1781. :group 'font-lock-faces)
  1782. (defface font-lock-builtin-face
  1783. '((((class grayscale) (background light)) :foreground "LightGray" :weight bold)
  1784. (((class grayscale) (background dark)) :foreground "DimGray" :weight bold)
  1785. (((class color) (min-colors 88) (background light)) :foreground "dark slate blue")
  1786. (((class color) (min-colors 88) (background dark)) :foreground "LightSteelBlue")
  1787. (((class color) (min-colors 16) (background light)) :foreground "Orchid")
  1788. (((class color) (min-colors 16) (background dark)) :foreground "LightSteelBlue")
  1789. (((class color) (min-colors 8)) :foreground "blue" :weight bold)
  1790. (t :weight bold))
  1791. "Font Lock mode face used to highlight builtins."
  1792. :group 'font-lock-faces)
  1793. (defface font-lock-function-name-face
  1794. '((((class color) (min-colors 88) (background light)) :foreground "Blue1")
  1795. (((class color) (min-colors 88) (background dark)) :foreground "LightSkyBlue")
  1796. (((class color) (min-colors 16) (background light)) :foreground "Blue")
  1797. (((class color) (min-colors 16) (background dark)) :foreground "LightSkyBlue")
  1798. (((class color) (min-colors 8)) :foreground "blue" :weight bold)
  1799. (t :inverse-video t :weight bold))
  1800. "Font Lock mode face used to highlight function names."
  1801. :group 'font-lock-faces)
  1802. (defface font-lock-variable-name-face
  1803. '((((class grayscale) (background light))
  1804. :foreground "Gray90" :weight bold :slant italic)
  1805. (((class grayscale) (background dark))
  1806. :foreground "DimGray" :weight bold :slant italic)
  1807. (((class color) (min-colors 88) (background light)) :foreground "sienna")
  1808. (((class color) (min-colors 88) (background dark)) :foreground "LightGoldenrod")
  1809. (((class color) (min-colors 16) (background light)) :foreground "DarkGoldenrod")
  1810. (((class color) (min-colors 16) (background dark)) :foreground "LightGoldenrod")
  1811. (((class color) (min-colors 8)) :foreground "yellow" :weight light)
  1812. (t :weight bold :slant italic))
  1813. "Font Lock mode face used to highlight variable names."
  1814. :group 'font-lock-faces)
  1815. (defface font-lock-type-face
  1816. '((((class grayscale) (background light)) :foreground "Gray90" :weight bold)
  1817. (((class grayscale) (background dark)) :foreground "DimGray" :weight bold)
  1818. (((class color) (min-colors 88) (background light)) :foreground "ForestGreen")
  1819. (((class color) (min-colors 88) (background dark)) :foreground "PaleGreen")
  1820. (((class color) (min-colors 16) (background light)) :foreground "ForestGreen")
  1821. (((class color) (min-colors 16) (background dark)) :foreground "PaleGreen")
  1822. (((class color) (min-colors 8)) :foreground "green")
  1823. (t :weight bold :underline t))
  1824. "Font Lock mode face used to highlight type and classes."
  1825. :group 'font-lock-faces)
  1826. (defface font-lock-constant-face
  1827. '((((class grayscale) (background light))
  1828. :foreground "LightGray" :weight bold :underline t)
  1829. (((class grayscale) (background dark))
  1830. :foreground "Gray50" :weight bold :underline t)
  1831. (((class color) (min-colors 88) (background light)) :foreground "dark cyan")
  1832. (((class color) (min-colors 88) (background dark)) :foreground "Aquamarine")
  1833. (((class color) (min-colors 16) (background light)) :foreground "CadetBlue")
  1834. (((class color) (min-colors 16) (background dark)) :foreground "Aquamarine")
  1835. (((class color) (min-colors 8)) :foreground "magenta")
  1836. (t :weight bold :underline t))
  1837. "Font Lock mode face used to highlight constants and labels."
  1838. :group 'font-lock-faces)
  1839. (defface font-lock-warning-face
  1840. '((t :inherit error))
  1841. "Font Lock mode face used to highlight warnings."
  1842. :group 'font-lock-faces)
  1843. (defface font-lock-negation-char-face
  1844. '((t nil))
  1845. "Font Lock mode face used to highlight easy to overlook negation."
  1846. :group 'font-lock-faces)
  1847. (defface font-lock-preprocessor-face
  1848. '((t :inherit font-lock-builtin-face))
  1849. "Font Lock mode face used to highlight preprocessor directives."
  1850. :group 'font-lock-faces)
  1851. (defface font-lock-regexp-grouping-backslash
  1852. '((t :inherit bold))
  1853. "Font Lock mode face for backslashes in Lisp regexp grouping constructs."
  1854. :group 'font-lock-faces)
  1855. (defface font-lock-regexp-grouping-construct
  1856. '((t :inherit bold))
  1857. "Font Lock mode face used to highlight grouping constructs in Lisp regexps."
  1858. :group 'font-lock-faces)
  1859. ;;; End of Color etc. support.
  1860. ;;; Menu support.
  1861. ;; This section of code is commented out because Emacs does not have real menu
  1862. ;; buttons. (We can mimic them by putting "( ) " or "(X) " at the beginning of
  1863. ;; the menu entry text, but with Xt it looks both ugly and embarrassingly
  1864. ;; amateur.) If/When Emacs gets real menus buttons, put in menu-bar.el after
  1865. ;; the entry for "Text Properties" something like:
  1866. ;;
  1867. ;; (define-key menu-bar-edit-menu [font-lock]
  1868. ;; (cons "Syntax Highlighting" font-lock-menu))
  1869. ;;
  1870. ;; and remove a single ";" from the beginning of each line in the rest of this
  1871. ;; section. Probably the mechanism for telling the menu code what are menu
  1872. ;; buttons and when they are on or off needs tweaking. I have assumed that the
  1873. ;; mechanism is via `menu-toggle' and `menu-selected' symbol properties. sm.
  1874. ;;;;;###autoload
  1875. ;;(progn
  1876. ;; ;; Make the Font Lock menu.
  1877. ;; (defvar font-lock-menu (make-sparse-keymap "Syntax Highlighting"))
  1878. ;; ;; Add the menu items in reverse order.
  1879. ;; (define-key font-lock-menu [fontify-less]
  1880. ;; '("Less In Current Buffer" . font-lock-fontify-less))
  1881. ;; (define-key font-lock-menu [fontify-more]
  1882. ;; '("More In Current Buffer" . font-lock-fontify-more))
  1883. ;; (define-key font-lock-menu [font-lock-sep]
  1884. ;; '("--"))
  1885. ;; (define-key font-lock-menu [font-lock-mode]
  1886. ;; '("In Current Buffer" . font-lock-mode))
  1887. ;; (define-key font-lock-menu [global-font-lock-mode]
  1888. ;; '("In All Buffers" . global-font-lock-mode)))
  1889. ;;
  1890. ;;;;;###autoload
  1891. ;;(progn
  1892. ;; ;; We put the appropriate `menu-enable' etc. symbol property values on when
  1893. ;; ;; font-lock.el is loaded, so we don't need to autoload the three variables.
  1894. ;; (put 'global-font-lock-mode 'menu-toggle t)
  1895. ;; (put 'font-lock-mode 'menu-toggle t)
  1896. ;; (put 'font-lock-fontify-more 'menu-enable '(identity))
  1897. ;; (put 'font-lock-fontify-less 'menu-enable '(identity)))
  1898. ;;
  1899. ;; ;; Put the appropriate symbol property values on now. See above.
  1900. ;;(put 'global-font-lock-mode 'menu-selected 'global-font-lock-mode)
  1901. ;;(put 'font-lock-mode 'menu-selected 'font-lock-mode)
  1902. ;;(put 'font-lock-fontify-more 'menu-enable '(nth 2 font-lock-fontify-level))
  1903. ;;(put 'font-lock-fontify-less 'menu-enable '(nth 1 font-lock-fontify-level))
  1904. ;;
  1905. ;;(defvar font-lock-fontify-level nil) ; For less/more fontification.
  1906. ;;
  1907. ;;(defun font-lock-fontify-level (level)
  1908. ;; (let ((font-lock-maximum-decoration level))
  1909. ;; (when font-lock-mode
  1910. ;; (font-lock-mode))
  1911. ;; (font-lock-mode)
  1912. ;; (when font-lock-verbose
  1913. ;; (message "Fontifying %s... level %d" (buffer-name) level))))
  1914. ;;
  1915. ;;(defun font-lock-fontify-less ()
  1916. ;; "Fontify the current buffer with less decoration.
  1917. ;;See `font-lock-maximum-decoration'."
  1918. ;; (interactive)
  1919. ;; ;; Check in case we get called interactively.
  1920. ;; (if (nth 1 font-lock-fontify-level)
  1921. ;; (font-lock-fontify-level (1- (car font-lock-fontify-level)))
  1922. ;; (error "No less decoration")))
  1923. ;;
  1924. ;;(defun font-lock-fontify-more ()
  1925. ;; "Fontify the current buffer with more decoration.
  1926. ;;See `font-lock-maximum-decoration'."
  1927. ;; (interactive)
  1928. ;; ;; Check in case we get called interactively.
  1929. ;; (if (nth 2 font-lock-fontify-level)
  1930. ;; (font-lock-fontify-level (1+ (car font-lock-fontify-level)))
  1931. ;; (error "No more decoration")))
  1932. ;;
  1933. ;; ;; This should be called by `font-lock-set-defaults'.
  1934. ;;(defun font-lock-set-menu ()
  1935. ;; ;; Activate less/more fontification entries if there are multiple levels for
  1936. ;; ;; the current buffer. Sets `font-lock-fontify-level' to be of the form
  1937. ;; ;; (CURRENT-LEVEL IS-LOWER-LEVEL-P IS-HIGHER-LEVEL-P) for menu activation.
  1938. ;; (let ((keywords (nth 0 font-lock-defaults))
  1939. ;; (level (font-lock-value-in-major-mode font-lock-maximum-decoration)))
  1940. ;; (make-local-variable 'font-lock-fontify-level)
  1941. ;; (if (or (symbolp keywords) (= (length keywords) 1))
  1942. ;; (font-lock-unset-menu)
  1943. ;; (cond ((eq level t)
  1944. ;; (setq level (1- (length keywords))))
  1945. ;; ((or (null level) (zerop level))
  1946. ;; ;; The default level is usually, but not necessarily, level 1.
  1947. ;; (setq level (- (length keywords)
  1948. ;; (length (member (eval (car keywords))
  1949. ;; (mapcar #'eval (cdr keywords))))))))
  1950. ;; (setq font-lock-fontify-level (list level (> level 1)
  1951. ;; (< level (1- (length keywords))))))))
  1952. ;;
  1953. ;; ;; This should be called by `font-lock-unset-defaults'.
  1954. ;;(defun font-lock-unset-menu ()
  1955. ;; ;; Deactivate less/more fontification entries.
  1956. ;; (setq font-lock-fontify-level nil))
  1957. ;;; End of Menu support.
  1958. ;;; Various regexp information shared by several modes.
  1959. ;; ;; Information specific to a single mode should go in its load library.
  1960. ;; Font Lock support for C, C++, Objective-C and Java modes is now in
  1961. ;; cc-fonts.el (and required by cc-mode.el). However, the below function
  1962. ;; should stay in font-lock.el, since it is used by other libraries. sm.
  1963. (defun font-lock-match-c-style-declaration-item-and-skip-to-next (limit)
  1964. "Match, and move over, any declaration/definition item after point.
  1965. Matches after point, but ignores leading whitespace and `*' characters.
  1966. Does not move further than LIMIT.
  1967. The expected syntax of a declaration/definition item is `word' (preceded by
  1968. optional whitespace and `*' characters and proceeded by optional whitespace)
  1969. optionally followed by a `('. Everything following the item (but belonging to
  1970. it) is expected to be skip-able by `scan-sexps', and items are expected to be
  1971. separated with a `,' and to be terminated with a `;'.
  1972. Thus the regexp matches after point: word (
  1973. ^^^^ ^
  1974. Where the match subexpressions are: 1 2
  1975. The item is delimited by (match-beginning 1) and (match-end 1).
  1976. If (match-beginning 2) is non-nil, the item is followed by a `('.
  1977. This function could be MATCHER in a MATCH-ANCHORED `font-lock-keywords' item."
  1978. (when (looking-at "[ \n\t*]*\\(\\sw+\\)[ \t\n]*\\(((?\\)?")
  1979. (when (and (match-end 2) (> (- (match-end 2) (match-beginning 2)) 1))
  1980. ;; If `word' is followed by a double open-paren, it's probably
  1981. ;; a macro used for "int myfun P_ ((int arg1))". Let's go back one
  1982. ;; word to try and match `myfun' rather than `P_'.
  1983. (let ((pos (point)))
  1984. (skip-chars-backward " \t\n")
  1985. (skip-syntax-backward "w")
  1986. (unless (looking-at "\\(\\sw+\\)[ \t\n]*\\sw+[ \t\n]*\\(((?\\)?")
  1987. ;; Looks like it was something else, so go back to where we
  1988. ;; were and reset the match data by rematching.
  1989. (goto-char pos)
  1990. (looking-at "[ \n\t*]*\\(\\sw+\\)[ \t\n]*\\(((?\\)?"))))
  1991. (save-match-data
  1992. (condition-case nil
  1993. (save-restriction
  1994. ;; Restrict to the LIMIT.
  1995. (narrow-to-region (point-min) limit)
  1996. (goto-char (match-end 1))
  1997. ;; Move over any item value, etc., to the next item.
  1998. (while (not (looking-at "[ \t\n]*\\(\\(,\\)\\|;\\|\\'\\)"))
  1999. (goto-char (or (scan-sexps (point) 1) (point-max))))
  2000. (if (match-end 2)
  2001. (goto-char (match-end 2))))
  2002. (error t)))))
  2003. ;; C preprocessor(cpp) is used outside of C, C++ and Objective-C source file.
  2004. ;; e.g. assembler code and GNU linker script in Linux kernel.
  2005. ;; `cpp-font-lock-keywords' is handy for modes for the files.
  2006. ;;
  2007. ;; Here we cannot use `regexp-opt' because because regex-opt is not preloaded
  2008. ;; while font-lock.el is preloaded to emacs. So values pre-calculated with
  2009. ;; regexp-opt are used here.
  2010. ;; `cpp-font-lock-keywords-source-directives' is calculated from:
  2011. ;;
  2012. ;; (regexp-opt
  2013. ;; '("define" "elif" "else" "endif" "error" "file" "if" "ifdef"
  2014. ;; "ifndef" "import" "include" "line" "pragma" "undef" "warning"))
  2015. ;;
  2016. (defconst cpp-font-lock-keywords-source-directives
  2017. "define\\|e\\(?:l\\(?:if\\|se\\)\\|ndif\\|rror\\)\\|file\\|i\\(?:f\\(?:n?def\\)?\\|mport\\|nclude\\)\\|line\\|pragma\\|undef\\|warning"
  2018. "Regular expression used in `cpp-font-lock-keywords'.")
  2019. ;; `cpp-font-lock-keywords-source-depth' is calculated from:
  2020. ;;
  2021. ;; (regexp-opt-depth (regexp-opt
  2022. ;; '("define" "elif" "else" "endif" "error" "file" "if" "ifdef"
  2023. ;; "ifndef" "import" "include" "line" "pragma" "undef" "warning")))
  2024. ;;
  2025. (defconst cpp-font-lock-keywords-source-depth 0
  2026. "An integer representing regular expression depth of `cpp-font-lock-keywords-source-directives'.
  2027. Used in `cpp-font-lock-keywords'.")
  2028. (defconst cpp-font-lock-keywords
  2029. (let* ((directives cpp-font-lock-keywords-source-directives)
  2030. (directives-depth cpp-font-lock-keywords-source-depth))
  2031. (list
  2032. ;;
  2033. ;; Fontify error directives.
  2034. '("^#[ \t]*\\(?:error\\|warning\\)[ \t]+\\(.+\\)" 1 font-lock-warning-face prepend)
  2035. ;;
  2036. ;; Fontify filenames in #include <...> preprocessor directives as strings.
  2037. '("^#[ \t]*\\(?:import\\|include\\)[ \t]*\\(<[^>\"\n]*>?\\)"
  2038. 1 font-lock-string-face prepend)
  2039. ;;
  2040. ;; Fontify function macro names.
  2041. '("^#[ \t]*define[ \t]+\\([[:alpha:]_][[:alnum:]_$]*\\)("
  2042. (1 font-lock-function-name-face prepend)
  2043. ;;
  2044. ;; Macro arguments.
  2045. ((lambda (limit)
  2046. (re-search-forward
  2047. "\\(?:\\([[:alpha:]_][[:alnum:]_]*\\)[,]?\\)"
  2048. (or (save-excursion (re-search-forward ")" limit t))
  2049. limit)
  2050. t))
  2051. nil nil (1 font-lock-variable-name-face prepend)))
  2052. ;;
  2053. ;; Fontify symbol names in #elif or #if ... defined preprocessor directives.
  2054. '("^#[ \t]*\\(?:elif\\|if\\)\\>"
  2055. ("\\<\\(defined\\)\\>[ \t]*(?\\([[:alpha:]_][[:alnum:]_]*\\)?" nil nil
  2056. (1 font-lock-builtin-face prepend) (2 font-lock-variable-name-face prepend t)))
  2057. ;;
  2058. ;; Fontify otherwise as symbol names, and the preprocessor directive names.
  2059. (list
  2060. (concat "^\\(#[ \t]*\\(?:" directives
  2061. "\\)\\)\\>[ \t!]*\\([[:alpha:]_][[:alnum:]_]*\\)?")
  2062. '(1 font-lock-preprocessor-face prepend)
  2063. (list (+ 2 directives-depth)
  2064. 'font-lock-variable-name-face nil t))))
  2065. "Font lock keywords for C preprocessor directives.
  2066. `c-mode', `c++-mode' and `objc-mode' have their own font lock keywords
  2067. for C preprocessor directives. This definition is for the other modes
  2068. in which C preprocessor directives are used. e.g. `asm-mode' and
  2069. `ld-script-mode'.")
  2070. (provide 'font-lock)
  2071. ;;; font-lock.el ends here