font-lock.el 104 KB
 Simon Marshall committed Nov 09, 1995 1 2 ;;; font-lock.el --- Electric font lock mode  Glenn Morris committed Jan 24, 2011 3 ;; Copyright (C) 1992-2011 Free Software Foundation, Inc.  Richard M. Stallman committed Jul 09, 1993 4   Glenn Morris committed Jan 10, 2009 5 6 7 ;; Author: Jamie Zawinski ;; Richard Stallman ;; Stefan Monnier  Richard M. Stallman committed Jul 09, 1993 8 9 ;; Maintainer: FSF ;; Keywords: languages, faces  Chong Yidong committed Aug 29, 2010 10 ;; Package: emacs  Richard M. Stallman committed Jul 09, 1993 11 12 13  ;; This file is part of GNU Emacs.  Glenn Morris committed May 06, 2008 14 ;; GNU Emacs is free software: you can redistribute it and/or modify  Richard M. Stallman committed Jul 09, 1993 15 ;; it under the terms of the GNU General Public License as published by  Glenn Morris committed May 06, 2008 16 17 ;; the Free Software Foundation, either version 3 of the License, or ;; (at your option) any later version.  Richard M. Stallman committed Jul 09, 1993 18 19 20 21 22 23 24  ;; GNU Emacs is distributed in the hope that it will be useful, ;; but WITHOUT ANY WARRANTY; without even the implied warranty of ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the ;; GNU General Public License for more details. ;; You should have received a copy of the GNU General Public License  Glenn Morris committed May 06, 2008 25 ;; along with GNU Emacs. If not, see .  Richard M. Stallman committed Jul 09, 1993 26 27 28  ;;; Commentary:  Simon Marshall committed Oct 12, 1994 29 30 ;; Font Lock mode is a minor mode that causes your comments to be displayed in ;; one face, strings in another, reserved words in another, and so on.  Richard M. Stallman committed Jul 09, 1993 31 32 33 ;; ;; Comments will be displayed in font-lock-comment-face'. ;; Strings will be displayed in font-lock-string-face'.  Richard M. Stallman committed Oct 07, 1994 34 ;; Regexps are used to display selected patterns in other faces.  Richard M. Stallman committed Jul 09, 1993 35 ;;  Simon Marshall committed Oct 26, 1995 36 37 38 ;; To make the text you type be fontified, use M-x font-lock-mode RET. ;; When this minor mode is on, the faces of the current line are updated with ;; every insertion or deletion.  Richard M. Stallman committed Jul 09, 1993 39 ;;  Simon Marshall committed Jan 25, 1996 40 ;; To turn Font Lock mode on automatically, add this to your ~/.emacs file:  Richard M. Stallman committed Jul 09, 1993 41 ;;  Simon Marshall committed Oct 12, 1994 42 ;; (add-hook 'emacs-lisp-mode-hook 'turn-on-font-lock)  Richard M. Stallman committed Jul 09, 1993 43 ;;  Simon Marshall committed Jan 25, 1996 44 45 46 47 ;; Or if you want to turn Font Lock mode on in many modes: ;; ;; (global-font-lock-mode t) ;;  Simon Marshall committed Oct 26, 1995 48 49 50 ;; Fontification for a particular mode may be available in a number of levels ;; of decoration. The higher the level, the more decoration, but the more time ;; it takes to fontify. See the variable font-lock-maximum-decoration', and  Richard M. Stallman committed Jun 23, 1996 51 52 ;; also the variable font-lock-maximum-size'. Support modes for Font Lock ;; mode can be used to speed up Font Lock mode. See font-lock-support-mode'.  53   54 55 56 57 58 59 60 61 62 63 ;;; How Font Lock mode fontifies: ;; When Font Lock mode is turned on in a buffer, it (a) fontifies the entire ;; buffer and (b) installs one of its fontification functions on one of the ;; hook variables that are run by Emacs after every buffer change (i.e., an ;; insertion or deletion). Fontification means the replacement of face' text ;; properties in a given region; Emacs displays text with these face' text ;; properties appropriately. ;; ;; Fontification normally involves syntactic (i.e., strings and comments) and  Simon Marshall committed May 29, 1997 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 ;; regexp (i.e., keywords and everything else) passes. There are actually ;; three passes; (a) the syntactic keyword pass, (b) the syntactic pass and (c) ;; the keyword pass. Confused? ;; ;; The syntactic keyword pass places syntax-table' text properties in the ;; buffer according to the variable font-lock-syntactic-keywords'. It is ;; necessary because Emacs' syntax table is not powerful enough to describe all ;; the different syntactic constructs required by the sort of people who decide ;; that a single quote can be syntactic or not depending on the time of day. ;; (What sort of person could decide to overload the meaning of a quote?) ;; Obviously the syntactic keyword pass must occur before the syntactic pass. ;; ;; The syntactic pass places face' text properties in the buffer according to ;; syntactic context, i.e., according to the buffer's syntax table and buffer ;; text's syntax-table' text properties. It involves using a syntax parsing ;; function to determine the context of different parts of a region of text. A ;; syntax parsing function is necessary because generally strings and/or ;; comments can span lines, and so the context of a given region is not ;; necessarily apparent from the content of that region. Because the keyword ;; pass only works within a given region, it is not generally appropriate for ;; syntactic fontification. This is the first fontification pass that makes ;; changes visible to the user; it fontifies strings and comments. ;; ;; The keyword pass places face' text properties in the buffer according to ;; the variable font-lock-keywords'. It involves searching for given regexps ;; (or calling given search functions) within the given region. This is the ;; second fontification pass that makes changes visible to the user; it ;; fontifies language reserved words, etc. ;; ;; Oh, and the answer is, "Yes, obviously just about everything should be done ;; in a single syntactic pass, but the only syntactic parser available ;; understands only strings and comments." Perhaps one day someone will write ;; some syntactic parsers for common languages and a son-of-font-lock.el could ;; use them rather then relying so heavily on the keyword (regexp) pass.  98   Simon Marshall committed Nov 16, 1996 99 100 101 102 ;;; How Font Lock mode supports modes or is supported by modes: ;; Modes that support Font Lock mode do so by defining one or more variables ;; whose values specify the fontification. Font Lock mode knows of these  Glenn Morris committed Oct 05, 2010 103 104 105 106 ;; variable names from the buffer local variable font-lock-defaults'. ;; (Font Lock mode is set up via (a) where a mode's patterns are ;; distributed with the mode's package library, and (b) where a mode's ;; patterns are distributed with font-lock.el itself. An example of (a)  Simon Marshall committed Nov 16, 1996 107 108 109 110 ;; is Pascal mode, an example of (b) is Lisp mode. Normally, the mechanism is ;; (a); (b) is used where it is not clear which package library should contain ;; the pattern definitions.) Font Lock mode chooses which variable to use for ;; fontification based on font-lock-maximum-decoration'.  111 ;;  Glenn Morris committed Jun 27, 2008 112 ;; Font Lock mode fontification behavior can be modified in a number of ways.  113 ;; See the below comments and the comments distributed throughout this file.  Simon Marshall committed Nov 16, 1996 114 115 116  ;;; Constructing patterns:  117 118 ;; See the documentation for the variable font-lock-keywords'. ;;  Simon Marshall committed May 29, 1997 119 120 ;; Efficient regexps for use as MATCHERs for font-lock-keywords' and ;; font-lock-syntactic-keywords' can be generated via the function  Stefan Monnier committed Mar 11, 2000 121 ;; regexp-opt'.  122   Simon Marshall committed Nov 16, 1996 123 124 125 126 127 128 129 130 ;;; Adding patterns for modes that already support Font Lock: ;; Though Font Lock highlighting patterns already exist for many modes, it's ;; likely there's something that you want fontified that currently isn't, even ;; at the maximum fontification level. You can add highlighting patterns via ;; font-lock-add-keywords'. For example, say in some C ;; header file you #define the token and' to expand to &&', etc., to make ;; your C code almost readable. In your ~/.emacs there could be:  131 ;;  Simon Marshall committed Nov 16, 1996 132 ;; (font-lock-add-keywords 'c-mode '("\\<\$$and\\|or\\|not\$$\\>"))  133 ;;  Simon Marshall committed Nov 16, 1996 134 135 136 137 138 139 140 141 142 143 144 145 146 ;; Some modes provide specific ways to modify patterns based on the values of ;; other variables. For example, additional C types can be specified via the ;; variable c-font-lock-extra-types'. ;;; Adding patterns for modes that do not support Font Lock: ;; Not all modes support Font Lock mode. If you (as a user of the mode) add ;; patterns for a new mode, you must define in your ~/.emacs a variable or ;; variables that specify regexp fontification. Then, you should indicate to ;; Font Lock mode, via the mode hook setting font-lock-defaults', exactly what ;; support is required. For example, say Foo mode should have the following ;; regexps fontified case-sensitively, and comments and strings should not be ;; fontified automagically. In your ~/.emacs there could be:  147 ;;  Simon Marshall committed Nov 16, 1996 148 149 150 151 ;; (defvar foo-font-lock-keywords ;; '(("\\<\$$one\\|two\\|three\$$\\>" . font-lock-keyword-face) ;; ("\\<\$$four\\|five\\|six\$$\\>" . font-lock-type-face)) ;; "Default expressions to highlight in Foo mode.")  152 ;;  Simon Marshall committed Nov 16, 1996 153 ;; (add-hook 'foo-mode-hook  Colin Walters committed Jun 09, 2002 154 ;; (lambda ()  155 156 ;; (set (make-local-variable 'font-lock-defaults) ;; '(foo-font-lock-keywords t))))  Simon Marshall committed Nov 16, 1996 157 158 159 160 161 162 163 164 165 166 167  ;;; Adding Font Lock support for modes: ;; Of course, it would be better that the mode already supports Font Lock mode. ;; The package author would do something similar to above. The mode must ;; define at the top-level a variable or variables that specify regexp ;; fontification. Then, the mode command should indicate to Font Lock mode, ;; via font-lock-defaults', exactly what support is required. For example, ;; say Bar mode should have the following regexps fontified case-insensitively, ;; and comments and strings should be fontified automagically. In bar.el there ;; could be:  168 ;;  Simon Marshall committed Nov 16, 1996 169 170 171 172 ;; (defvar bar-font-lock-keywords ;; '(("\\<\$$uno\\|due\\|tre\$$\\>" . font-lock-keyword-face) ;; ("\\<\$$quattro\\|cinque\\|sei\$$\\>" . font-lock-type-face)) ;; "Default expressions to highlight in Bar mode.")  173 ;;  Simon Marshall committed Nov 16, 1996 174 ;; and within bar-mode' there could be:  175 ;;  176 177 ;; (set (make-local-variable 'font-lock-defaults) ;; '(bar-font-lock-keywords nil t))  178   Simon Marshall committed Oct 26, 1995 179 180 181 182 183 184 185 186 187 ;; What is fontification for? You might say, "It's to make my code look nice." ;; I think it should be for adding information in the form of cues. These cues ;; should provide you with enough information to both (a) distinguish between ;; different items, and (b) identify the item meanings, without having to read ;; the items and think about it. Therefore, fontification allows you to think ;; less about, say, the structure of code, and more about, say, why the code ;; doesn't work. Or maybe it allows you to think less and drift off to sleep. ;; ;; So, here are my opinions/advice/guidelines:  Sam Steingold committed Aug 07, 2000 188 ;;  Simon Marshall committed Dec 09, 1995 189 190 191 192 193 ;; - Highlight conceptual objects, such as function and variable names, and ;; different objects types differently, i.e., (a) and (b) above, highlight ;; function names differently to variable names. ;; - Keep the faces distinct from each other as far as possible. ;; i.e., (a) above.  Simon Marshall committed Oct 26, 1995 194 195 196 197 ;; - Use the same face for the same conceptual object, across all modes. ;; i.e., (b) above, all modes that have items that can be thought of as, say, ;; keywords, should be highlighted with the same face, etc. ;; - Make the face attributes fit the concept as far as possible.  Glenn Morris committed Jun 27, 2008 198 199 ;; i.e., function names might be a bold color such as blue, comments might ;; be a bright color such as red, character strings might be brown, because,  Simon Marshall committed Oct 26, 1995 200 201 202 203 204 205 ;; err, strings are brown (that was not the reason, please believe me). ;; - Don't use a non-nil OVERRIDE unless you have a good reason. ;; Only use OVERRIDE for special things that are easy to define, such as the ;; way ...' quotes are treated in strings and comments in Emacs Lisp mode. ;; Don't use it to, say, highlight keywords in commented out code or strings. ;; - Err, that's it.  Richard M. Stallman committed Oct 07, 1994 206   207 208 ;;; Code:  Stefan Monnier committed Oct 28, 2001 209 (require 'syntax)  Stefan Monnier committed Apr 03, 2010 210 (eval-when-compile (require 'cl))  Stefan Monnier committed Oct 28, 2001 211   Simon Marshall committed Apr 23, 1997 212 ;; Define core font-lock' group.  Kenichi Handa committed Apr 16, 2004 213 (defgroup font-lock '((jit-lock custom-group))  Simon Marshall committed Apr 17, 1997 214  "Font Lock mode text highlighting package."  Luc Teirlinck committed Dec 19, 2005 215 216  :link '(custom-manual :tag "Emacs Manual" "(emacs)Font Lock") :link '(custom-manual :tag "Elisp Manual" "(elisp)Font Lock Mode")  Simon Marshall committed Apr 17, 1997 217 218  :group 'faces)  Juri Linkov committed Nov 17, 2005 219 (defgroup font-lock-faces nil  Simon Marshall committed Apr 23, 1997 220  "Faces for highlighting text."  Simon Marshall committed Apr 17, 1997 221  :prefix "font-lock-"  Simon Marshall committed Apr 23, 1997 222 223 224 225 226  :group 'font-lock) (defgroup font-lock-extra-types nil "Extra mode-specific type names for highlighting declarations." :group 'font-lock)  Simon Marshall committed Apr 17, 1997 227   Simon Marshall committed Oct 26, 1995 228 229 ;; User variables.  Simon Marshall committed Sep 01, 1998 230 (defcustom font-lock-maximum-size 256000  Chong Yidong committed Oct 17, 2011 231 232 233 234 235  "Maximum buffer size for unsupported buffer fontification. When font-lock-support-mode' is nil, only buffers smaller than this are fontified. This variable has no effect if a Font Lock support mode (usually jit-lock-mode') is enabled.  236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 If nil, means size is irrelevant. If a list, each element should be a cons pair of the form (MAJOR-MODE . SIZE), where MAJOR-MODE is a symbol or t (meaning the default). For example: ((c-mode . 256000) (c++-mode . 256000) (rmail-mode . 1048576)) means that the maximum size is 250K for buffers in C or C++ modes, one megabyte for buffers in Rmail mode, and size is irrelevant otherwise." :type '(choice (const :tag "none" nil) (integer :tag "size") (repeat :menu-tag "mode specific" :tag "mode specific" :value ((t . nil)) (cons :tag "Instance" (radio :tag "Mode" (const :tag "all" t) (symbol :tag "name")) (radio :tag "Size" (const :tag "none" nil) (integer :tag "size")))))  Simon Marshall committed Apr 17, 1997 253  :group 'font-lock)  Chong Yidong committed Oct 17, 2011 254 (make-obsolete-variable 'font-lock-maximum-size nil "24.1")  Simon Marshall committed Oct 26, 1995 255   Simon Marshall committed Apr 17, 1997 256 (defcustom font-lock-maximum-decoration t  Lute Kamstra committed Dec 03, 2008 257  "Maximum decoration level for fontification.  Simon Marshall committed Oct 26, 1995 258 259 260 If nil, use the default decoration (typically the minimum available). If t, use the maximum decoration available. If a number, use that level of decoration (or if not available the maximum).  261 The higher the number, the more decoration is done.  Simon Marshall committed Oct 26, 1995 262 263 If a list, each element should be a cons pair of the form (MAJOR-MODE . LEVEL), where MAJOR-MODE is a symbol or t (meaning the default). For example:  Richard M. Stallman committed Jun 23, 1996 264 265  ((c-mode . t) (c++-mode . 2) (t . 1)) means use the maximum decoration available for buffers in C mode, level 2  Simon Marshall committed Apr 17, 1997 266 decoration for buffers in C++ mode, and level 1 decoration otherwise."  Simon Marshall committed Apr 18, 1997 267 268 269 270 271 272 273 274 275 276 277  :type '(choice (const :tag "default" nil) (const :tag "maximum" t) (integer :tag "level" 1) (repeat :menu-tag "mode specific" :tag "mode specific" :value ((t . t)) (cons :tag "Instance" (radio :tag "Mode" (const :tag "all" t) (symbol :tag "name")) (radio :tag "Decoration" (const :tag "default" nil)  Dave Love committed Nov 26, 1999 278  (const :tag "maximum" t)  Simon Marshall committed Apr 18, 1997 279  (integer :tag "level" 1)))))  Simon Marshall committed Apr 17, 1997 280 281  :group 'font-lock)  Chong Yidong committed Jan 12, 2011 282 (defcustom font-lock-verbose nil  Lute Kamstra committed Dec 03, 2008 283  "If non-nil, means show status messages for buffer fontification.  284 285 If a number, only buffers greater than this size have fontification messages." :type '(choice (const :tag "never" nil)  Simon Marshall committed Sep 01, 1998 286 287  (other :tag "always" t) (integer :tag "size"))  Chong Yidong committed Jan 12, 2011 288 289  :group 'font-lock :version "24.1")  Simon Marshall committed Oct 26, 1995 290   Stefan Monnier committed Nov 10, 2000 291 292 293 294 295 296  ;; Originally these variable values were face names such as bold' etc. ;; Now we create our own faces, but we keep these variables for compatibility ;; and they give users another mechanism for changing face appearance. ;; We now allow a FACENAME in font-lock-keywords' to be any expression that ;; returns a face. So the easiest thing is to continue using these variables,  Paul Eggert committed Dec 18, 2011 297 ;; rather than sometimes evalling FACENAME and sometimes not. sm.  Glenn Morris committed Sep 20, 2008 298 299 300 301 302  ;; Note that in new code, in the vast majority of cases there is no ;; need to create variables that specify face names. Simply using ;; faces directly is enough. Font-lock is not a template to be ;; followed in this area.  Stefan Monnier committed Nov 10, 2000 303 304 305 (defvar font-lock-comment-face 'font-lock-comment-face "Face name to use for comments.")  Richard M. Stallman committed Apr 23, 2005 306 (defvar font-lock-comment-delimiter-face 'font-lock-comment-delimiter-face  Richard M. Stallman committed Apr 24, 2005 307  "Face name to use for comment delimiters.")  Richard M. Stallman committed Apr 23, 2005 308   Stefan Monnier committed Nov 10, 2000 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 (defvar font-lock-string-face 'font-lock-string-face "Face name to use for strings.") (defvar font-lock-doc-face 'font-lock-doc-face "Face name to use for documentation.") (defvar font-lock-keyword-face 'font-lock-keyword-face "Face name to use for keywords.") (defvar font-lock-builtin-face 'font-lock-builtin-face "Face name to use for builtins.") (defvar font-lock-function-name-face 'font-lock-function-name-face "Face name to use for function names.") (defvar font-lock-variable-name-face 'font-lock-variable-name-face "Face name to use for variable names.") (defvar font-lock-type-face 'font-lock-type-face "Face name to use for type and class names.") (defvar font-lock-constant-face 'font-lock-constant-face "Face name to use for constant and label names.") (defvar font-lock-warning-face 'font-lock-warning-face "Face name to use for things that should stand out.")  Daniel Pfeiffer committed May 05, 2005 336 337 338 339 (defvar font-lock-negation-char-face 'font-lock-negation-char-face "Face name to use for easy to overlook negation. This can be an \"!\" or the \"n\" in \"ifndef\".")  Stefan Monnier committed Apr 14, 2003 340 341 342 (defvar font-lock-preprocessor-face 'font-lock-preprocessor-face "Face name to use for preprocessor directives.")  343 (defvar font-lock-reference-face 'font-lock-constant-face)  Juanma Barranquero committed Apr 18, 2008 344 (make-obsolete-variable 'font-lock-reference-face 'font-lock-constant-face "20.3")  Stefan Monnier committed Nov 10, 2000 345   Simon Marshall committed Oct 26, 1995 346 347 ;; Fontification variables:  Richard M. Stallman committed Jul 09, 1993 348 (defvar font-lock-keywords nil  Simon Marshall committed May 29, 1997 349  "A list of the keywords to highlight.  Richard M. Stallman committed Jun 24, 2004 350 351 352 353 354 355 356 357 358 There are two kinds of values: user-level, and compiled. A user-level keywords list is what a major mode or the user would set up. Normally the list would come from font-lock-defaults'. through selection of a fontification level and evaluation of any contained expressions. You can also alter it by calling font-lock-add-keywords' or font-lock-remove-keywords' with MODE = nil. Each element in a user-level keywords list should have one of these forms:  Richard M. Stallman committed Oct 07, 1994 359   Simon Marshall committed Apr 24, 1995 360  MATCHER  Richard M. Stallman committed Oct 10, 2005 361  (MATCHER . SUBEXP)  Simon Marshall committed Apr 24, 1995 362 363 364  (MATCHER . FACENAME) (MATCHER . HIGHLIGHT) (MATCHER HIGHLIGHT ...)  Simon Marshall committed Feb 02, 1996 365  (eval . FORM)  Richard M. Stallman committed Oct 07, 1994 366   Stefan Monnier committed May 11, 2001 367 where MATCHER can be either the regexp to search for, or the function name to  Stefan Monnier committed Jul 27, 2004 368 call to make the search (called with one argument, the limit of the search;  Glenn Morris committed Aug 08, 2007 369 it should return non-nil, move point, and set match-data' appropriately if  Stefan Monnier committed Jul 27, 2004 370 it succeeds; like re-search-forward' would).  Stefan Monnier committed May 11, 2001 371 MATCHER regexps can be generated via the function regexp-opt'.  Simon Marshall committed Oct 26, 1995 372   Simon Marshall committed Feb 02, 1996 373 374 375 376 FORM is an expression, whose value should be a keyword element, evaluated when the keyword is (first) used in a buffer. This feature can be used to provide a keyword that can only be generated when Font Lock mode is actually turned on.  Stefan Monnier committed May 11, 2001 377 378 HIGHLIGHT should be either MATCH-HIGHLIGHT or MATCH-ANCHORED.  Simon Marshall committed Feb 18, 1998 379 380 For highlighting single items, for example each instance of the word \"foo\", typically only MATCH-HIGHLIGHT is required.  Karl Heuer committed Jan 04, 1996 381 However, if an item or (typically) items are to be highlighted following the  Simon Marshall committed Feb 18, 1998 382 383 instance of another item (the anchor), for example each instance of the word \"bar\" following the word \"anchor\" then MATCH-ANCHORED may be required.  Simon Marshall committed Oct 26, 1995 384 385 386  MATCH-HIGHLIGHT should be of the form:  Richard M. Stallman committed Oct 10, 2005 387  (SUBEXP FACENAME [OVERRIDE [LAXMATCH]])  Simon Marshall committed Oct 26, 1995 388   Richard M. Stallman committed Oct 10, 2005 389 390 391 392 393 SUBEXP is the number of the subexpression of MATCHER to be highlighted. FACENAME is an expression whose value is the face name to use. Instead of a face, FACENAME can evaluate to a property list of the form (face FACE PROP1 VAL1 PROP2 VAL2 ...)  Stefan Monnier committed May 11, 2001 394 in which case all the listed text-properties will be set rather than  Lute Kamstra committed Apr 08, 2005 395 just FACE. In such a case, you will most likely want to put those  Stefan Monnier committed May 11, 2001 396 397 properties in font-lock-extra-managed-props' or to override font-lock-unfontify-region-function'.  Richard M. Stallman committed Oct 07, 1994 398   Simon Marshall committed Mar 15, 1997 399 OVERRIDE and LAXMATCH are flags. If OVERRIDE is t, existing fontification can  Simon Marshall committed Oct 26, 1995 400 401 402 be overwritten. If keep', only parts not already fontified are highlighted. If prepend' or append', existing fontification is merged with the new, in which the new or existing fontification, respectively, takes precedence.  Richard M. Stallman committed Oct 10, 2005 403 404 If LAXMATCH is non-nil, that means don't signal an error if there is no match for SUBEXP in MATCHER.  Richard M. Stallman committed Oct 07, 1994 405   Simon Marshall committed Oct 26, 1995 406 407 For example, an element of the form highlights (if not already highlighted):  Simon Marshall committed Feb 18, 1998 408  \"\\\\\\=\" discrete occurrences of \"foo\" in the value of the  Simon Marshall committed Nov 09, 1995 409  variable font-lock-keyword-face'.  Simon Marshall committed Feb 18, 1998 410  (\"fu\\\$$bar\\\$$\" . 1) substring \"bar\" within all occurrences of \"fubar\" in  Simon Marshall committed Oct 26, 1995 411 412 413  the value of font-lock-keyword-face'. (\"fubar\" . fubar-face) Occurrences of \"fubar\" in the value of fubar-face'. (\"foo\\\\|bar\" 0 foo-bar-face t)  Simon Marshall committed Feb 18, 1998 414  occurrences of either \"foo\" or \"bar\" in the value  Simon Marshall committed Oct 26, 1995 415  of foo-bar-face', even if already highlighted.  Simon Marshall committed Oct 28, 1997 416  (fubar-match 1 fubar-face)  Simon Marshall committed Feb 18, 1998 417  the first subexpression within all occurrences of  Simon Marshall committed Oct 28, 1997 418 419  whatever the function fubar-match' finds and matches in the value of fubar-face'.  Simon Marshall committed Oct 26, 1995 420 421 422 423 424  MATCH-ANCHORED should be of the form: (MATCHER PRE-MATCH-FORM POST-MATCH-FORM MATCH-HIGHLIGHT ...)  Simon Marshall committed Feb 18, 1998 425 426 where MATCHER is a regexp to search for or the function name to call to make the search, as for MATCH-HIGHLIGHT above, but with one exception; see below.  Simon Marshall committed Nov 09, 1995 427 428 PRE-MATCH-FORM and POST-MATCH-FORM are evaluated before the first, and after the last, instance MATCH-ANCHORED's MATCHER is used. Therefore they can be  Juanma Barranquero committed Jun 16, 2005 429 used to initialize before, and cleanup after, MATCHER is used. Typically,  Simon Marshall committed Nov 09, 1995 430 431 PRE-MATCH-FORM is used to move to some position relative to the original MATCHER, before starting with MATCH-ANCHORED's MATCHER. POST-MATCH-FORM might  Lute Kamstra committed Apr 08, 2005 432 be used to move back, before resuming with MATCH-ANCHORED's parent's MATCHER.  Simon Marshall committed Oct 26, 1995 433 434 435  For example, an element of the form highlights (if not already highlighted):  Simon Marshall committed Nov 09, 1995 436  (\"\\\\\\=\" (0 anchor-face) (\"\\\\\\=\" nil nil (0 item-face)))  Simon Marshall committed Oct 26, 1995 437   Simon Marshall committed Feb 18, 1998 438  discrete occurrences of \"anchor\" in the value of anchor-face', and subsequent  Simon Marshall committed Nov 09, 1995 439 440 441  discrete occurrences of \"item\" (on the same line) in the value of item-face'. (Here PRE-MATCH-FORM and POST-MATCH-FORM are nil. Therefore \"item\" is initially searched for starting from the end of the match of \"anchor\", and  Lute Kamstra committed Apr 08, 2005 442  searching for subsequent instances of \"anchor\" resumes from where searching  Simon Marshall committed Nov 09, 1995 443  for \"item\" concluded.)  Simon Marshall committed Oct 26, 1995 444   445 446 447 448 449 450 451 The above-mentioned exception is as follows. The limit of the MATCHER search defaults to the end of the line after PRE-MATCH-FORM is evaluated. However, if PRE-MATCH-FORM returns a position greater than the position after PRE-MATCH-FORM is evaluated, that position is used as the limit of the search. It is generally a bad idea to return a position greater than the end of the line, i.e., cause the MATCHER search to span lines.  Stefan Monnier committed Feb 07, 2000 452 453 454 455 These regular expressions can match text which spans lines, although it is better to avoid it if possible since updating them while editing text is slower, and it is not guaranteed to be always correct when using support modes like jit-lock or lazy-lock.  Richard M. Stallman committed Oct 07, 1994 456   Simon Marshall committed Mar 15, 1997 457 458 This variable is set by major modes via the variable font-lock-defaults'. Be careful when composing regexps for this list; a poorly written pattern can  Richard M. Stallman committed Jun 24, 2004 459 460 461 462 463 464 465 466 dramatically slow things down! A compiled keywords list starts with t. It is produced internal by font-lock-compile-keywords' from a user-level keywords list. Its second element is the user-level keywords list that was compiled. The remaining elements have the same form as user-level keywords, but normally their values have been optimized.")  Richard M. Stallman committed Jul 09, 1993 467   Simon Marshall committed Nov 16, 1996 468 (defvar font-lock-keywords-alist nil  469 470  "Alist of additional font-lock-keywords' elements for major modes.  Romain Francoise committed Dec 14, 2005 471 Each element has the form (MODE KEYWORDS . HOW).  472 473 474 font-lock-set-defaults' adds the elements in the list KEYWORDS to font-lock-keywords' when Font Lock is turned on in major mode MODE.  Romain Francoise committed Dec 14, 2005 475 If HOW is nil, KEYWORDS are added at the beginning of  476 font-lock-keywords'. If it is set', they are used to replace the  Romain Francoise committed Dec 14, 2005 477 value of font-lock-keywords'. If HOW is any other non-nil value,  478 479 they are added at the end.  Gerd Moellmann committed Dec 16, 1999 480 481 This is normally set via font-lock-add-keywords' and font-lock-remove-keywords'.")  Richard M. Stallman committed Apr 21, 2007 482 (put 'font-lock-keywords-alist 'risky-local-variable t)  Gerd Moellmann committed Dec 16, 1999 483 484  (defvar font-lock-removed-keywords-alist nil  485 486 487 488 489 490  "Alist of font-lock-keywords' elements to be removed for major modes. Each element has the form (MODE . KEYWORDS). font-lock-set-defaults' removes the elements in the list KEYWORDS from font-lock-keywords' when Font Lock is turned on in major mode MODE.  Gerd Moellmann committed Dec 16, 1999 491 492 This is normally set via font-lock-add-keywords' and font-lock-remove-keywords'.")  Simon Marshall committed Nov 16, 1996 493   Simon Marshall committed Oct 26, 1995 494 (defvar font-lock-keywords-only nil  Simon Marshall committed Aug 10, 1995 495 496  "*Non-nil means Font Lock should not fontify comments or strings. This is normally set via font-lock-defaults'.")  Simon Marshall committed Oct 12, 1994 497   Richard M. Stallman committed Jul 09, 1993 498 (defvar font-lock-keywords-case-fold-search nil  Simon Marshall committed Aug 10, 1995 499  "*Non-nil means the patterns in font-lock-keywords' are case-insensitive.  Chong Yidong committed Nov 03, 2008 500 501 This is set via the function font-lock-set-defaults', based on the CASE-FOLD argument of font-lock-defaults'.")  Sam Steingold committed Apr 05, 2001 502 (make-variable-buffer-local 'font-lock-keywords-case-fold-search)  Richard M. Stallman committed Jul 09, 1993 503   Stefan Monnier committed Oct 15, 2000 504 505 506 507 508 509 (defvar font-lock-syntactically-fontified 0 "Point up to which font-lock-syntactic-keywords' has been applied. If nil, this is ignored, in which case the syntactic fontification may sometimes be slightly incorrect.") (make-variable-buffer-local 'font-lock-syntactically-fontified)  Stefan Monnier committed Nov 10, 2000 510 511 512 513 514 515 (defvar font-lock-syntactic-face-function (lambda (state) (if (nth 3 state) font-lock-string-face font-lock-comment-face)) "Function to determine which face to use when fontifying syntactically. The function is called with a single parameter (the state as returned by parse-partial-sexp' at the beginning of the region to highlight) and  Lute Kamstra committed Apr 25, 2005 516 should return a face. This is normally set via font-lock-defaults'.")  Stefan Monnier committed Nov 10, 2000 517   Simon Marshall committed May 29, 1997 518 (defvar font-lock-syntactic-keywords nil  Richard M. Stallman committed Oct 10, 2005 519 520 521 522  "A list of the syntactic keywords to put syntax properties on. The value can be the list itself, or the name of a function or variable whose value is the list.  Simon Marshall committed May 29, 1997 523 See font-lock-keywords' for a description of the form of this list;  Richard M. Stallman committed Oct 10, 2005 524 only the differences are stated here. MATCH-HIGHLIGHT should be of the form:  Simon Marshall committed May 29, 1997 525   Richard M. Stallman committed Oct 10, 2005 526  (SUBEXP SYNTAX OVERRIDE LAXMATCH)  Simon Marshall committed May 29, 1997 527   Stefan Monnier committed Oct 15, 2000 528 529 530 where SYNTAX can be a string (as taken by modify-syntax-entry'), a syntax table, a cons cell (as returned by string-to-syntax') or an expression whose value is such a form. OVERRIDE cannot be prepend' or append'.  Simon Marshall committed May 29, 1997 531   Richard M. Stallman committed Oct 10, 2005 532 533 Here are two examples of elements of font-lock-syntactic-keywords' and what they do:  Simon Marshall committed Feb 18, 1998 534   Stefan Monnier committed Oct 15, 2000 535  (\"\\\\\$\\\$$#\\\$$\" 1 \".\")  Simon Marshall committed Feb 18, 1998 536   Richard M. Stallman committed Oct 10, 2005 537 538 539  gives a hash character punctuation syntax (\".\") when following a dollar-sign character. Hash characters in other contexts will still follow whatever the syntax table says about the hash character.  Simon Marshall committed Feb 18, 1998 540 541  (\"\\\$$'\\\$$.\\\$$'\\\$$\"  Stefan Monnier committed Oct 15, 2000 542 543  (1 \"\\\"\") (2 \"\\\"\"))  Simon Marshall committed Feb 18, 1998 544   Richard M. Stallman committed Oct 10, 2005 545 546 547  gives a pair single-quotes, which surround a single character, a SYNTAX of \"\\\"\" (meaning string quote syntax). Single-quote characters in other contexts will not be affected.  Simon Marshall committed Feb 18, 1998 548   Simon Marshall committed May 29, 1997 549 This is normally set via font-lock-defaults'.")  Stefan Monnier committed Sep 11, 2010 550 551 (make-obsolete-variable 'font-lock-syntactic-keywords 'syntax-propertize-function "24.1")  Simon Marshall committed May 29, 1997 552   Richard M. Stallman committed Aug 05, 1994 553 (defvar font-lock-syntax-table nil  Simon Marshall committed Mar 02, 1995 554  "Non-nil means use this syntax table for fontifying.  Simon Marshall committed Aug 10, 1995 555 556 557 558 If this is nil, the major mode's syntax table is used. This is normally set via font-lock-defaults'.") (defvar font-lock-beginning-of-syntax-function nil  Richard M. Stallman committed Jun 12, 2002 559 560 561 562 563 564  "*Non-nil means use this function to move back outside all constructs. When called with no args it should move point backward to a place which is not in a string or comment and not within any bracket-pairs (or else, a place such that any bracket-pairs outside it can be ignored for Emacs syntax analysis and fontification).  Lute Kamstra committed May 22, 2005 565 566 567 568 569 If this is nil, Font Lock uses syntax-begin-function' to move back outside of any comment, string, or sexp. This variable is semi-obsolete; we recommend setting syntax-begin-function' instead. This is normally set via font-lock-defaults'.")  Stefan Monnier committed Sep 11, 2010 570 (make-obsolete-variable 'font-lock-beginning-of-syntax-function  Stefan Monnier committed Jun 01, 2011 571  'syntax-begin-function "23.3" 'set)  Richard M. Stallman committed Aug 05, 1994 572   Simon Marshall committed Feb 02, 1996 573 574 575 576 577 578 (defvar font-lock-mark-block-function nil "*Non-nil means use this function to mark a block of text. When called with no args it should leave point at the beginning of any enclosing textual block and mark at the end. This is normally set via font-lock-defaults'.")  Simon Marshall committed Jan 25, 1996 579 580 581 582 583 584 585 586 587 588 589 590 (defvar font-lock-fontify-buffer-function 'font-lock-default-fontify-buffer "Function to use for fontifying the buffer. This is normally set via font-lock-defaults'.") (defvar font-lock-unfontify-buffer-function 'font-lock-default-unfontify-buffer "Function to use for unfontifying the buffer. This is used when turning off Font Lock mode. This is normally set via font-lock-defaults'.") (defvar font-lock-fontify-region-function 'font-lock-default-fontify-region "Function to use for fontifying a region. It should take two args, the beginning and end of the region, and an optional  Lute Kamstra committed Apr 26, 2005 591 592 third arg VERBOSE. If VERBOSE is non-nil, the function should print status messages. This is normally set via font-lock-defaults'.")  Simon Marshall committed Jan 25, 1996 593 594 595 596 597 598 599 600  (defvar font-lock-unfontify-region-function 'font-lock-default-unfontify-region "Function to use for unfontifying a region. It should take two args, the beginning and end of the region. This is normally set via font-lock-defaults'.") (defvar font-lock-inhibit-thing-lock nil "List of Font Lock mode related modes that should not be turned on.  Dave Love committed Nov 26, 1999 601 Currently, valid mode names are fast-lock-mode', jit-lock-mode' and  Gerd Moellmann committed Jul 23, 1999 602 lazy-lock-mode'. This is normally set via font-lock-defaults'.")  Simon Marshall committed Jan 25, 1996 603   Stefan Monnier committed Apr 14, 2003 604 605 606 607 608 609 610 611 (defvar font-lock-multiline nil "Whether font-lock should cater to multiline keywords. If nil, don't try to handle multiline patterns. If t, always handle multiline patterns. If undecided', don't try to handle multiline patterns until you see one. Major/minor modes can set this variable if they know which option applies.") (defvar font-lock-fontified nil) ; Whether we have fontified the buffer.  Richard M. Stallman committed Jul 09, 1993 612   Richard M. Stallman committed Jun 23, 1996 613 614 615 ;; Font Lock mode. (eval-when-compile  616  ;;  Richard M. Stallman committed Jun 23, 1996 617  ;; We don't do this at the top-level as we only use non-autoloaded macros.  618 619 620 621  (require 'cl) ;; ;; Borrowed from lazy-lock.el. ;; We use this to preserve or protect things when modifying text properties.  Stefan Monnier committed Sep 08, 2010 622  (defmacro save-buffer-state (&rest body)  623  "Bind variables according to VARLIST and eval BODY restoring buffer state."  Stefan Monnier committed Sep 08, 2010 624 625  (declare (indent 0) (debug t)) (let ((inhibit-point-motion-hooks t))  Stefan Monnier committed Aug 30, 2010 626 627  (with-silent-modifications ,@body)))  Simon Marshall committed Sep 09, 1997 628 629  ;; ;; Shut up the byte compiler.  Stefan Monnier committed Nov 10, 2000 630  (defvar font-lock-face-attributes)) ; Obsolete but respected if set.  Richard M. Stallman committed Jul 09, 1993 631   Stefan Monnier committed Apr 14, 2003 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 (defun font-lock-mode-internal (arg) ;; Turn on Font Lock mode. (when arg (add-hook 'after-change-functions 'font-lock-after-change-function t t) (font-lock-set-defaults) (font-lock-turn-on-thing-lock) ;; Fontify the buffer if we have to. (let ((max-size (font-lock-value-in-major-mode font-lock-maximum-size))) (cond (font-lock-fontified nil) ((or (null max-size) (> max-size (buffer-size))) (font-lock-fontify-buffer)) (font-lock-verbose (message "Fontifying %s...buffer size greater than font-lock-maximum-size" (buffer-name)))))) ;; Turn off Font Lock mode. (unless font-lock-mode (remove-hook 'after-change-functions 'font-lock-after-change-function t) (font-lock-unfontify-buffer) (font-lock-turn-off-thing-lock)))  Romain Francoise committed Dec 14, 2005 653 (defun font-lock-add-keywords (mode keywords &optional how)  Stefan Monnier committed Dec 09, 1999 654  "Add highlighting KEYWORDS for MODE.  Lute Kamstra committed Apr 25, 2005 655   Stefan Monnier committed Dec 09, 1999 656 MODE should be a symbol, the major mode command name, such as c-mode'  Simon Marshall committed Jan 09, 1997 657 or nil. If nil, highlighting keywords are added for the current buffer.  Simon Marshall committed Nov 16, 1996 658 659 KEYWORDS should be a list; see the variable font-lock-keywords'. By default they are added at the beginning of the current highlighting list.  Romain Francoise committed Dec 14, 2005 660 661 If optional argument HOW is set', they are used to replace the current highlighting list. If HOW is any other non-nil value, they are added at the  Simon Marshall committed Mar 15, 1997 662 end of the current highlighting list.  Simon Marshall committed Nov 16, 1996 663 664 665 666 667 668 669 670  For example: (font-lock-add-keywords 'c-mode '((\"\\\\\\=<\\\$$FIXME\\\$$:\" 1 font-lock-warning-face prepend) (\"\\\\\\=<\\\$$and\\\\|or\\\\|not\\\$$\\\\\\=>\" . font-lock-keyword-face))) adds two fontification patterns for C mode, to fontify FIXME:' words, even in  Simon Marshall committed Mar 15, 1997 671 672 comments, and to fontify and', or' and not' words as keywords.  Luc Teirlinck committed Jun 04, 2005 673 674 675 676 677 678 679 680 The above procedure will only add the keywords for C mode, not for modes derived from C mode. To add them for derived modes too, pass nil for MODE and add the call to c-mode-hook. For example: (add-hook 'c-mode-hook (lambda ()  Luc Teirlinck committed Jun 06, 2005 681  (font-lock-add-keywords nil  Luc Teirlinck committed Jun 04, 2005 682 683 684 685 686 687 688  '((\"\\\\\\=<\\\$$FIXME\\\$$:\" 1 font-lock-warning-face prepend) (\"\\\\\\=<\\\$$and\\\\|or\\\\|not\\\$$\\\\\\=>\" . font-lock-keyword-face))))) The above procedure may fail to add keywords to derived modes if some involved major mode does not follow the standard conventions. File a bug report if this happens, so the major mode can be corrected.  Stefan Monnier committed May 11, 2001 689   Juanma Barranquero committed Aug 30, 2002 690 Note that some modes have specialized support for additional patterns, e.g.,  Simon Marshall committed Mar 15, 1997 691 692 see the variables c-font-lock-extra-types', c++-font-lock-extra-types', objc-font-lock-extra-types' and java-font-lock-extra-types'."  Stefan Monnier committed Dec 09, 1999 693  (cond (mode  Romain Francoise committed Dec 14, 2005 694  ;; If MODE is non-nil, add the KEYWORDS and HOW spec to  Simon Marshall committed Nov 16, 1996 695  ;; font-lock-keywords-alist' so font-lock-set-defaults' uses them.  Romain Francoise committed Dec 14, 2005 696  (let ((spec (cons keywords how)) cell)  Stefan Monnier committed Dec 09, 1999 697  (if (setq cell (assq mode font-lock-keywords-alist))  Romain Francoise committed Dec 14, 2005 698  (if (eq how 'set)  Gerd Moellmann committed Dec 16, 1999 699 700 701 702 703  (setcdr cell (list spec)) (setcdr cell (append (cdr cell) (list spec)))) (push (list mode spec) font-lock-keywords-alist))) ;; Make sure that font-lock-removed-keywords-alist' does not ;; contain the new keywords.  Romain Francoise committed Dec 14, 2005 704  (font-lock-update-removed-keyword-alist mode keywords how))  Stefan Monnier committed Feb 07, 2000 705  (t  Stefan Monnier committed Jun 11, 2007 706 707 708 709 710 711 712 713  (when (and font-lock-mode (not (or font-lock-keywords font-lock-defaults))) ;; The major mode has not set any keywords, so when we enabled ;; font-lock-mode it only enabled the font-core.el part, not the ;; font-lock-mode-internal. Try again. (font-lock-mode -1) (set (make-local-variable 'font-lock-defaults) '(nil t)) (font-lock-mode 1))  Stefan Monnier committed Feb 07, 2000 714  ;; Otherwise set or add the keywords now.  Luc Teirlinck committed Jun 04, 2005 715 716  ;; This is a no-op if it has been done already in this buffer ;; for the correct major mode.  Stefan Monnier committed Feb 07, 2000 717  (font-lock-set-defaults)  Richard M. Stallman committed Jun 24, 2004 718 719 720 721 722  (let ((was-compiled (eq (car font-lock-keywords) t))) ;; Bring back the user-level (uncompiled) keywords. (if was-compiled (setq font-lock-keywords (cadr font-lock-keywords))) ;; Now modify or replace them.  Romain Francoise committed Dec 14, 2005 723  (if (eq how 'set)  Richard M. Stallman committed Jun 24, 2004 724 725 726 727 728  (setq font-lock-keywords keywords) (font-lock-remove-keywords nil keywords) ;to avoid duplicates (let ((old (if (eq (car-safe font-lock-keywords) t) (cdr font-lock-keywords) font-lock-keywords)))  Romain Francoise committed Dec 14, 2005 729  (setq font-lock-keywords (if how  Richard M. Stallman committed Jun 24, 2004 730 731 732 733  (append old keywords) (append keywords old))))) ;; If the keywords were compiled before, compile them again. (if was-compiled  734  (setq font-lock-keywords  Richard M. Stallman committed Sep 16, 2006 735  (font-lock-compile-keywords font-lock-keywords)))))))  Stefan Monnier committed Dec 07, 1999 736   Romain Francoise committed Dec 14, 2005 737 (defun font-lock-update-removed-keyword-alist (mode keywords how)  Lute Kamstra committed Apr 28, 2005 738  "Update font-lock-removed-keywords-alist' when adding new KEYWORDS to MODE."  Gerd Moellmann committed Dec 16, 1999 739 740 741 742 743 744  ;; When font-lock is enabled first all keywords in the list ;; font-lock-keywords-alist' are added, then all keywords in the ;; list font-lock-removed-keywords-alist' are removed. If a ;; keyword was once added, removed, and then added again it must be ;; removed from the removed-keywords list. Otherwise the second add ;; will not take effect.  Stefan Monnier committed Feb 07, 2000 745  (let ((cell (assq mode font-lock-removed-keywords-alist)))  Gerd Moellmann committed Dec 16, 1999 746  (if cell  Romain Francoise committed Dec 14, 2005 747  (if (eq how 'set)  Gerd Moellmann committed Dec 16, 1999 748 749 750 751 752 753 754  ;; A new set of keywords is defined. Forget all about ;; our old keywords that should be removed. (setq font-lock-removed-keywords-alist (delq cell font-lock-removed-keywords-alist)) ;; Delete all previously removed keywords. (dolist (kword keywords) (setcdr cell (delete kword (cdr cell))))  Stefan Monnier committed Feb 07, 2000 755  ;; Delete the mode cell if empty.  Gerd Moellmann committed Dec 16, 1999 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778  (if (null (cdr cell)) (setq font-lock-removed-keywords-alist (delq cell font-lock-removed-keywords-alist))))))) ;; Written by Anders Lindgren . ;; ;; Case study: ;; (I) The keywords are removed from a major mode. ;; In this case the keyword could be local (i.e. added earlier by ;; font-lock-add-keywords'), global, or both. ;; ;; (a) In the local case we remove the keywords from the variable ;; font-lock-keywords-alist'. ;; ;; (b) The actual global keywords are not known at this time. ;; All keywords are added to font-lock-removed-keywords-alist', ;; when font-lock is enabled those keywords are removed. ;; ;; Note that added keywords are taken out of the list of removed ;; keywords. This ensure correct operation when the same keyword ;; is added and removed several times. ;; ;; (II) The keywords are removed from the current buffer.  Stefan Monnier committed Feb 07, 2000 779 780 (defun font-lock-remove-keywords (mode keywords) "Remove highlighting KEYWORDS for MODE.  Stefan Monnier committed Dec 07, 1999 781   Stefan Monnier committed Feb 07, 2000 782 MODE should be a symbol, the major mode command name, such as c-mode'  Stefan Monnier committed May 11, 2001 783 784 or nil. If nil, highlighting keywords are removed for the current buffer.  Luc Teirlinck committed Jun 04, 2005 785 786 787 788 789 To make the removal apply to modes derived from MODE as well, pass nil for MODE and add the call to MODE-hook. This may fail for some derived modes if some involved major mode does not follow the standard conventions. File a bug report if this happens, so the major mode can be corrected."  Stefan Monnier committed Feb 07, 2000 790 791 792 793 794  (cond (mode ;; Remove one keyword at the time. (dolist (keyword keywords) (let ((top-cell (assq mode font-lock-keywords-alist))) ;; If MODE is non-nil, remove the KEYWORD from  Gerd Moellmann committed Dec 16, 1999 795 796  ;; font-lock-keywords-alist'. (when top-cell  Romain Francoise committed Dec 14, 2005 797 798 799  (dolist (keyword-list-how-pair (cdr top-cell)) ;; keywords-list-how-pair' is a cons with a list of ;; keywords in the car top-cell and the original how  Gerd Moellmann committed Dec 16, 1999 800  ;; argument in the cdr top-cell.  Romain Francoise committed Dec 14, 2005 801 802 803 804  (setcar keyword-list-how-pair (delete keyword (car keyword-list-how-pair)))) ;; Remove keyword list/how pair when the keyword list ;; is empty and how doesn't specify set'. (If it  Gerd Moellmann committed Dec 16, 1999 805 806 807 808 809 810 811 812 813 814 815 816 817 818  ;; should be deleted then previously deleted keywords ;; would appear again.) (let ((cell top-cell)) (while (cdr cell) (if (and (null (car (car (cdr cell)))) (not (eq (cdr (car (cdr cell))) 'set))) (setcdr cell (cdr (cdr cell))) (setq cell (cdr cell))))) ;; Final cleanup, remove major mode cell if last keyword ;; was deleted. (if (null (cdr top-cell)) (setq font-lock-keywords-alist (delq top-cell font-lock-keywords-alist)))) ;; Remember the keyword in case it is not local.  Stefan Monnier committed Feb 07, 2000 819  (let ((cell (assq mode font-lock-removed-keywords-alist)))  Gerd Moellmann committed Dec 16, 1999 820 821 822  (if cell (unless (member keyword (cdr cell)) (nconc cell (list keyword)))  Stefan Monnier committed Feb 07, 2000 823 824 825 826 827  (push (cons mode (list keyword)) font-lock-removed-keywords-alist)))))) (t ;; Otherwise remove it immediately. (font-lock-set-defaults)  Richard M. Stallman committed Jun 24, 2004 828 829 830 831 832 833 834 835 836 837 838 839 840  (let ((was-compiled (eq (car font-lock-keywords) t))) ;; Bring back the user-level (uncompiled) keywords. (if was-compiled (setq font-lock-keywords (cadr font-lock-keywords))) ;; Edit them. (setq font-lock-keywords (copy-sequence font-lock-keywords)) (dolist (keyword keywords) (setq font-lock-keywords (delete keyword font-lock-keywords))) ;; If the keywords were compiled before, compile them again. (if was-compiled  841  (setq font-lock-keywords  Richard M. Stallman committed Sep 16, 2006 842  (font-lock-compile-keywords font-lock-keywords)))))))  Simon Marshall committed Jan 25, 1996 843   Simon Marshall committed Mar 15, 1997 844 845 ;;; Font Lock Support mode.  Richard M. Stallman committed Jun 23, 1996 846 847 848 849 850 851 ;; This is the code used to interface font-lock.el with any of its add-on ;; packages, and provide the user interface. Packages that have their own ;; local buffer fontification functions (see below) may have to call ;; font-lock-after-fontify-buffer' and/or font-lock-after-unfontify-buffer' ;; themselves.  Gerd Moellmann committed Aug 18, 1999 852 (defcustom font-lock-support-mode 'jit-lock-mode  Lute Kamstra committed Dec 03, 2008 853  "Support mode for Font Lock mode.  Richard M. Stallman committed Jun 23, 1996 854 Support modes speed up Font Lock mode by being choosy about when fontification  Richard M. Stallman committed Sep 09, 2005 855 856 857 858 859 860 861 occurs. The default support mode, Just-in-time Lock mode (symbol jit-lock-mode'), is recommended. Other, older support modes are Fast Lock mode (symbol fast-lock-mode') and Lazy Lock mode (symbol lazy-lock-mode'). See those modes for more info. However, they are no longer recommended, as Just-in-time Lock mode is better.  Richard M. Stallman committed Jun 23, 1996 862 863 864 865 866 867 868 869 If nil, means support for Font Lock mode is never performed. If a symbol, use that support mode. If a list, each element should be of the form (MAJOR-MODE . SUPPORT-MODE), where MAJOR-MODE is a symbol or t (meaning the default). For example: ((c-mode . fast-lock-mode) (c++-mode . fast-lock-mode) (t . lazy-lock-mode)) means that Fast Lock mode is used to support Font Lock mode for buffers in C or C++ modes, and Lazy Lock mode is used to support Font Lock mode otherwise.  Simon Marshall committed Apr 17, 1997 870 The value of this variable is used when Font Lock mode is turned on."  Simon Marshall committed Apr 18, 1997 871 872 873  :type '(choice (const :tag "none" nil) (const :tag "fast lock" fast-lock-mode) (const :tag "lazy lock" lazy-lock-mode)  Gerd Moellmann committed Jul 23, 1999 874  (const :tag "jit lock" jit-lock-mode)  Simon Marshall committed Apr 18, 1997 875  (repeat :menu-tag "mode specific" :tag "mode specific"  Stefan Monnier committed Nov 10, 2000 876  :value ((t . jit-lock-mode))  Simon Marshall committed Apr 18, 1997 877 878 879 880  (cons :tag "Instance" (radio :tag "Mode" (const :tag "all" t) (symbol :tag "name"))  Simon Marshall committed Aug 03, 1998 881 882  (radio :tag "Support" (const :tag "none" nil)  Simon Marshall committed Apr 18, 1997 883  (const :tag "fast lock" fast-lock-mode)  Gerd Moellmann committed Jul 23, 1999 884 885  (const :tag "lazy lock" lazy-lock-mode) (const :tag "JIT lock" jit-lock-mode)))  Simon Marshall committed Apr 18, 1997 886  ))  Dave Love committed Jun 23, 2000 887  :version "21.1"  Simon Marshall committed Apr 17, 1997 888  :group 'font-lock)  Richard M. Stallman committed Jun 23, 1996 889   Stefan Monnier committed Sep 25, 2002 890 891 892 (defvar fast-lock-mode) (defvar lazy-lock-mode) (defvar jit-lock-mode)  893   Glenn Morris committed Dec 06, 2007 894 895 896 897 898 899 (declare-function fast-lock-after-fontify-buffer "fast-lock") (declare-function fast-lock-after-unfontify-buffer "fast-lock") (declare-function fast-lock-mode "fast-lock") (declare-function lazy-lock-after-fontify-buffer "lazy-lock") (declare-function lazy-lock-after-unfontify-buffer "lazy-lock") (declare-function lazy-lock-mode "lazy-lock")  Dan Nicolaescu committed Nov 21, 2007 900   Richard M. Stallman committed Jun 23, 1996 901 (defun font-lock-turn-on-thing-lock ()  Stefan Monnier committed Apr 03, 2010 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919  (case (font-lock-value-in-major-mode font-lock-support-mode) (fast-lock-mode (fast-lock-mode t)) (lazy-lock-mode (lazy-lock-mode t)) (jit-lock-mode ;; Prepare for jit-lock (remove-hook 'after-change-functions 'font-lock-after-change-function t) (set (make-local-variable 'font-lock-fontify-buffer-function) 'jit-lock-refontify) ;; Don't fontify eagerly (and don't abort if the buffer is large). (set (make-local-variable 'font-lock-fontified) t) ;; Use jit-lock. (jit-lock-register 'font-lock-fontify-region (not font-lock-keywords-only)) ;; Tell jit-lock how we extend the region to refontify. (add-hook 'jit-lock-after-change-extend-region-functions 'font-lock-extend-jit-lock-region-after-change nil t))))  Richard M. Stallman committed Jun 23, 1996 920 921  (defun font-lock-turn-off-thing-lock ()  John Paul Wallington committed Jun 04, 2008 922  (cond ((bound-and-true-p fast-lock-mode)  Stefan Monnier committed Nov 10, 2000 923  (fast-lock-mode -1))  John Paul Wallington committed Jun 04, 2008 924  ((bound-and-true-p jit-lock-mode)  Stefan Monnier committed Oct 06, 2000 925 926 927  (jit-lock-unregister 'font-lock-fontify-region) ;; Reset local vars to the non-jit-lock case. (kill-local-variable 'font-lock-fontify-buffer-function))  John Paul Wallington committed Jun 04, 2008 928  ((bound-and-true-p lazy-lock-mode)  Stefan Monnier committed Nov 10, 2000 929  (lazy-lock-mode -1))))  Richard M. Stallman committed Jun 23, 1996 930 931  (defun font-lock-after-fontify-buffer ()  John Paul Wallington committed Jun 04, 2008 932  (cond ((bound-and-true-p fast-lock-mode)  Richard M. Stallman committed Jun 23, 1996 933  (fast-lock-after-fontify-buffer))  Stefan Monnier committed Jun 17, 2000 934 935 936  ;; Useless now that jit-lock intercepts font-lock-fontify-buffer. -sm ;; (jit-lock-mode ;; (jit-lock-after-fontify-buffer))  John Paul Wallington committed Jun 04, 2008 937  ((bound-and-true-p lazy-lock-mode)  Richard M. Stallman committed Jun 23, 1996 938 939 940  (lazy-lock-after-fontify-buffer)))) (defun font-lock-after-unfontify-buffer ()  John Paul Wallington committed Jun 04, 2008 941  (cond ((bound-and-true-p fast-lock-mode)  Richard M. Stallman committed Jun 23, 1996 942  (fast-lock-after-unfontify-buffer))  Stefan Monnier committed Jun 17, 2000 943 944 945 946 947  ;; Useless as well. It's only called when: ;; - turning off font-lock: it does not matter if we leave spurious ;; fontified' text props around since jit-lock-mode is also off. ;; - font-lock-default-fontify-buffer fails: this is not run ;; any more anyway. -sm  Sam Steingold committed Aug 07, 2000 948  ;;  Stefan Monnier committed Jun 17, 2000 949 950  ;; (jit-lock-mode ;; (jit-lock-after-unfontify-buffer))  John Paul Wallington committed Jun 04, 2008 951  ((bound-and-true-p lazy-lock-mode)  Richard M. Stallman committed Jun 23, 1996 952 953  (lazy-lock-after-unfontify-buffer))))  Simon Marshall committed Mar 15, 1997 954 ;;; End of Font Lock Support mode.  Richard M. Stallman committed Jun 23, 1996 955   Simon Marshall committed Mar 15, 1997 956 ;;; Fontification functions.  Richard M. Stallman committed Oct 07, 1994 957   958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 ;; Rather than the function, e.g., font-lock-fontify-region' containing the ;; code to fontify a region, the function runs the function whose name is the ;; value of the variable, e.g., font-lock-fontify-region-function'. Normally, ;; the value of this variable is, e.g., font-lock-default-fontify-region' ;; which does contain the code to fontify a region. However, the value of the ;; variable could be anything and thus, e.g., font-lock-fontify-region' could ;; do anything. The indirection of the fontification functions gives major ;; modes the capability of modifying the way font-lock.el fontifies. Major ;; modes can modify the values of, e.g., font-lock-fontify-region-function', ;; via the variable font-lock-defaults'. ;; ;; For example, Rmail mode sets the variable font-lock-defaults' so that ;; font-lock.el uses its own function for buffer fontification. This function ;; makes fontification be on a message-by-message basis and so visiting an ;; RMAIL file is much faster. A clever implementation of the function might ;; fontify the headers differently than the message body. (It should, and ;; correspondingly for Mail mode, but I can't be bothered to do the work. Can ;; you?) This hints at a more interesting use... ;; ;; Languages that contain text normally contained in different major modes ;; could define their own fontification functions that treat text differently ;; depending on its context. For example, Perl mode could arrange that here ;; docs are fontified differently than Perl code. Or Yacc mode could fontify ;; rules one way and C code another. Neat! ;; ;; A further reason to use the fontification indirection feature is when the  Paul Eggert committed Nov 19, 2011 984 ;; default syntactic fontification, or the default fontification in general,  985 ;; is not flexible enough for a particular major mode. For example, perhaps  Simon Marshall committed Apr 17, 1997 986 987 988 989 ;; comments are just too hairy for font-lock-fontify-syntactically-region' to ;; cope with. You need to write your own version of that function, e.g., ;; hairy-fontify-syntactically-region', and make your own version of ;; hairy-fontify-region' call that function before calling  990 991 992 ;; font-lock-fontify-keywords-region' for the normal regexp fontification ;; pass. And Hairy mode would set font-lock-defaults' so that font-lock.el ;; would call your region fontification function instead of its own. For  Simon Marshall committed Apr 17, 1997 993 ;; example, TeX modes could fontify {\foo ...} and \bar{...} etc. multi-line  994 995 996 ;; directives correctly and cleanly. (It is the same problem as fontifying ;; multi-line strings and comments; regexps are not appropriate for the job.)  Stefan Monnier committed Aug 03, 2006 997 (defvar font-lock-extend-after-change-region-function nil  Stefan Monnier committed Aug 07, 2006 998  "A function that determines the region to refontify after a change.  Stefan Monnier committed Aug 01, 2006 999 1000 1001 1002 1003 1004 1005 1006  This variable is either nil, or is a function that determines the region to refontify after a change. It is usually set by the major mode via font-lock-defaults'. Font-lock calls this function after each buffer change. The function is given three parameters, the standard BEG, END, and OLD-LEN from after-change-functions'. It should return either a cons of the beginning  Stefan Monnier committed Aug 07, 2006 1007 and end buffer positions \(in that order) of the region to refontify, or nil  Stefan Monnier committed Aug 03, 2006 1008 1009 \(which directs the caller to fontify a default region). This function should preserve the match-data.  Stefan Monnier committed Aug 01, 2006 1010 The region it returns may start or end in the middle of a line.")  Alan Mackenzie committed Apr 09, 2008 1011 (make-variable-buffer-local 'font-lock-extend-after-change-region-function)  Stefan Monnier committed Aug 01, 2006 1012   Richard M. Stallman committed Jul 09, 1993 1013 (defun font-lock-fontify-buffer ()  Dave Love committed Nov 26, 1999 1014  "Fontify the current buffer the way the function font-lock-mode' would."  Richard M. Stallman committed Jul 09, 1993 1015  (interactive)  Stefan Monnier committed Jan 03, 2006 1016  (font-lock-set-defaults)  Juanma Barranquero committed Oct 02, 2009 1017 1018  (let ((font-lock-verbose (or font-lock-verbose (called-interactively-p 'interactive))))  Simon Marshall committed Jan 25, 1996 1019 1020 1021 1022 1023 1024  (funcall font-lock-fontify-buffer-function))) (defun font-lock-unfontify-buffer () (funcall font-lock-unfontify-buffer-function)) (defun font-lock-fontify-region (beg end &optional loudly)  Chong Yidong committed Aug 20, 2011 1025 1026 1027  "Fontify the text between BEG and END. If LOUDLY is non-nil, print status messages while fontifying. This works by calling font-lock-fontify-region-function'."  Stefan Monnier committed Jan 03, 2006 1028  (font-lock-set-defaults)  Simon Marshall committed Jan 25, 1996 1029 1030 1031  (funcall font-lock-fontify-region-function beg end loudly)) (defun font-lock-unfontify-region (beg end)  Chong Yidong committed Aug 20, 2011 1032 1033  "Unfontify the text between BEG and END. This works by calling font-lock-unfontify-region-function'."  Stefan Monnier committed Sep 08, 2010 1034  (save-buffer-state  Stefan Monnier committed Dec 07, 2004 1035  (funcall font-lock-unfontify-region-function beg end)))  Simon Marshall committed Jan 25, 1996 1036 1037  (defun font-lock-default-fontify-buffer ()  Chong Yidong committed Aug 20, 2011 1038  "Fontify the whole buffer using font-lock-fontify-region-function'."  Richard M. Stallman committed Jun 23, 1996 1039 1040 1041  (let ((verbose (if (numberp font-lock-verbose) (> (buffer-size) font-lock-verbose) font-lock-verbose)))  Simon Marshall committed Nov 23, 1998 1042  (with-temp-message  Simon Marshall committed Jan 06, 1999 1043 1044  (when verbose (format "Fontifying %s..." (buffer-name)))  Simon Marshall committed Nov 23, 1998 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054  ;; Make sure we fontify etc. in the whole buffer. (save-restriction (widen) (condition-case nil (save-excursion (save-match-data (font-lock-fontify-region (point-min) (point-max) verbose) (font-lock-after-fontify-buffer) (setq font-lock-fontified t))) ;; We don't restore the old fontification, so it's best to unfontify.  Stefan Monnier committed Oct 28, 2001 1055  (quit (font-lock-unfontify-buffer)))))))  Simon Marshall committed Dec 09, 1995 1056   Simon Marshall committed Jan 25, 1996 1057 (defun font-lock-default-unfontify-buffer ()  Chong Yidong committed Aug 20, 2011 1058  "Unfontify the whole buffer using font-lock-unfontify-region-function'."  1059  ;; Make sure we unfontify etc. in the whole buffer.  Simon Marshall committed Jan 25, 1996 1060 1061 1062  (save-restriction (widen) (font-lock-unfontify-region (point-min) (point-max))  Simon Marshall committed Jan 26, 1996 1063  (font-lock-after-unfontify-buffer)  Simon Marshall committed Jan 25, 1996 1064  (setq font-lock-fontified nil)))  Simon Marshall committed Oct 26, 1995 1065   Stefan Monnier committed Sep 25, 2002 1066 1067 1068 1069 1070 (defvar font-lock-dont-widen nil "If non-nil, font-lock will work on the non-widened buffer. Useful for things like RMAIL and Info where the whole buffer is not a very meaningful entity to highlight.")  Stefan Monnier committed Aug 03, 2006 1071 1072 1073 1074  (defvar font-lock-beg) (defvar font-lock-end) (defvar font-lock-extend-region-functions '(font-lock-extend-region-wholelines  Stefan Monnier committed Aug 07, 2006 1075 1076 1077 1078 1079 1080  ;; This use of font-lock-multiline property is unreliable but is just ;; a handy heuristic: in case you don't have a function that does ;; /identification/ of multiline elements, you may still occasionally ;; discover them by accident (or you may /identify/ them but not in all ;; cases), in which case the font-lock-multiline property can help make ;; sure you will properly *re*identify them during refontification.  Stefan Monnier committed Aug 03, 2006 1081 1082 1083 1084 1085 1086 1087 1088 1089  font-lock-extend-region-multiline) "Special hook run just before proceeding to fontify a region. This is used to allow major modes to help font-lock find safe buffer positions as beginning and end of the fontified region. Its most common use is to solve the problem of /identification/ of multiline elements by providing a function that tries to find such elements and move the boundaries such that they do not fall in the middle of one. Each function is called with no argument; it is expected to adjust the dynamically bound variables font-lock-beg' and font-lock-end'; and return  Glenn Morris committed Aug 08, 2007 1090 non-nil if it did make such an adjustment.  Stefan Monnier committed Aug 03, 2006 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 These functions are run in turn repeatedly until they all return nil. Put first the functions more likely to cause a change and cheaper to compute.") ;; Mark it as a special hook which doesn't use any global setting ;; (i.e. doesn't obey the element t in the buffer-local value). (make-variable-buffer-local 'font-lock-extend-region-functions) (defun font-lock-extend-region-multiline () "Move fontification boundaries away from any font-lock-multiline' property." (let ((changed nil)) (when (and (> font-lock-beg (point-min)) (get-text-property (1- font-lock-beg) 'font-lock-multiline)) (setq changed t) (setq font-lock-beg (or (previous-single-property-change font-lock-beg 'font-lock-multiline) (point-min))))  Eli Zaretskii committed Feb 17, 2007 1106  ;;  Stefan Monnier committed Aug 03, 2006 1107 1108 1109 1110 1111 1112  (when (get-text-property font-lock-end 'font-lock-multiline) (setq changed t) (setq font-lock-end (or (text-property-any font-lock-end (point-max) 'font-lock-multiline nil) (point-max)))) changed))  Eli Zaretskii committed Feb 17, 2007 1113   Stefan Monnier committed Aug 03, 2006 1114 1115 1116 1117 (defun font-lock-extend-region-wholelines () "Move fontification boundaries to beginning of lines." (let ((changed nil)) (goto-char font-lock-beg)  Stefan Monnier committed Feb 18, 2007 1118  (unless (bolp)  Eli Zaretskii committed Feb 17, 2007 1119  (setq changed t font-lock-beg (line-beginning-position)))  Stefan Monnier committed Aug 03, 2006 1120  (goto-char font-lock-end)  Stefan Monnier committed Feb 15, 2007 1121 1122 1123 1124  (unless (bolp) (unless (eq font-lock-end (setq font-lock-end (line-beginning-position 2))) (setq changed t)))  Stefan Monnier committed Aug 03, 2006 1125 1126  changed))  Simon Marshall committed Jan 25, 1996 1127 (defun font-lock-default-fontify-region (beg end loudly) `