1 ;; Electric Font Lock Mode
2 ;; Copyright (C) 1992, 1993 Free Software Foundation, Inc.
4 ;; Author: jwz, then rms
6 ;; Keywords: languages, faces
8 ;; 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 2, or (at your option)
15 ;; GNU Emacs is distributed in the hope that it will be useful,
16 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
17 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 ;; GNU General Public License for more details.
20 ;; You should have received a copy of the GNU General Public License
21 ;; along with GNU Emacs; see the file COPYING. If not, write to
22 ;; the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
27 ;; Font-lock-mode is a minor mode that causes your comments to be
28 ;; displayed in one face, strings in another, reserved words in another,
29 ;; documentation strings in another, and so on.
31 ;; Comments will be displayed in `font-lock-comment-face'.
32 ;; Strings will be displayed in `font-lock-string-face'.
33 ;; Doc strings will be displayed in `font-lock-doc-string-face'.
34 ;; Function and variable names (in their defining forms) will be
35 ;; displayed in `font-lock-function-name-face'.
36 ;; Reserved words will be displayed in `font-lock-keyword-face'.
38 ;; To make the text you type be fontified, use M-x font-lock-mode.
39 ;; When this minor mode is on, the fonts of the current line are
40 ;; updated with every insertion or deletion.
42 ;; To define new reserved words or other patterns to highlight, use
43 ;; the `font-lock-keywords' variable. This should be mode-local.
45 ;; To turn this on automatically, add this to your .emacs file:
47 ;; (setq emacs-lisp-mode-hook '(lambda () (font-lock-mode 1)))
49 ;; On a Sparc2, the initial fontification takes about 12 seconds for a 120k
50 ;; file of C code, using the default configuration. You can speed this up
51 ;; substantially by removing some of the patterns that are highlighted by
52 ;; default. Fontifying Lisp code is significantly faster, because Lisp has a
53 ;; more regular syntax than C, so the expressions don't have to be as hairy.
57 (or (internal-find-face 'underline)
58 (copy-face 'default 'underline))
59 (set-face-underline-p 'underline t)
61 (defvar font-lock-comment-face
63 "Face to use for comments.")
65 (defvar font-lock-doc-string-face
67 "Face to use for documentation strings.")
69 (defvar font-lock-string-face
71 "Face to use for string constants.")
73 (defvar font-lock-function-name-face
75 "Face to use for function names.")
77 (defvar font-lock-keyword-face
79 "Face to use for keywords.")
81 (defvar font-lock-type-face
83 "Face to use for data types.")
85 (make-variable-buffer-local 'font-lock-keywords)
86 (defvar font-lock-keywords nil
87 "*The keywords to highlight.
88 If this is a list, then elements may be of the forms:
90 \"string\" ; a regexp to highlight in the
91 ; `font-lock-keyword-face'.
92 (\"string\" . integer) ; match N of the regexp will be highlighted
93 (\"string\" . face-name) ; use the named face
94 (\"string\" integer face-name) ; both of the above
95 (\"string\" integer face-name t) ; this allows highlighting to overlap
96 ; with already-highlighted regions.
98 These regular expressions should not match text which spans lines.
99 While \\[font-lock-fontify-buffer] handles multi-line patterns correctly,
100 updating when you edit the buffer does not,
101 since it considers text one line at a time.
103 Be careful composing regexps for this list; the wrong pattern can dramatically
106 (defvar font-lock-keywords-case-fold-search nil
107 "*Non-nil means the patterns in `font-lock-keywords' are case-insensitive.")
109 (defvar font-lock-verbose t
110 "*Non-nil means `font-lock-fontify-buffer' should print status messages.")
113 (defvar font-lock-mode-hook nil
114 "Function or functions to run on entry to Font Lock mode.")
116 ;;; These variables record, for each buffer,
117 ;;; the parse state at a particular position, always the start of a line.
118 ;;; This is used to make font-lock-fontify-region faster.
119 (defvar font-lock-cache-position nil)
120 (defvar font-lock-cache-state nil)
121 (make-variable-buffer-local 'font-lock-cache-position)
122 (make-variable-buffer-local 'font-lock-cache-state)
124 (defun font-lock-fontify-region (start end)
125 "Put proper face on each string and comment between START and END."
129 (setq end (min end (point-max)))
130 (let ((buffer-read-only nil)
131 state startline prev prevstate
132 (modified (buffer-modified-p)))
133 ;; Find the state at the line-beginning before START.
134 (setq startline (point))
135 (if (eq (point) font-lock-cache-position)
136 (setq state font-lock-cache-state)
137 ;; Find outermost containing sexp.
139 ;; Find the state at STARTLINE.
140 (while (< (point) startline)
141 (setq state (parse-partial-sexp (point) startline 0)))
142 (setq font-lock-cache-state state
143 font-lock-cache-position (point)))
144 ;; Now find the state precisely at START.
145 (setq state (parse-partial-sexp (point) start nil nil state))
146 ;; If the region starts inside a string, show the extent of it.
149 (while (and (re-search-forward "\\s\"" end 'move)
150 (nth 3 (parse-partial-sexp beg (point)
152 (put-text-property beg (point) 'face font-lock-string-face)
153 (setq state (parse-partial-sexp beg (point) nil nil state))))
154 ;; Likewise for a comment.
155 (if (or (nth 4 state) (nth 7 state))
157 (while (and (re-search-forward (if comment-end
159 (regexp-quote comment-end))
162 (nth 3 (parse-partial-sexp beg (point)
164 (put-text-property beg (point) 'face font-lock-comment-face)
165 (setq state (parse-partial-sexp beg (point) nil nil state))))
166 ;; Find each interesting place between here and END.
167 (while (and (< (point) end)
168 (setq prev (point) prevstate state)
169 (re-search-forward (concat "\\s\"\\|" comment-start-skip) end t)
170 ;; Clear out the fonts of what we skip over.
171 (progn (remove-text-properties prev (point) '(face nil)) t)
172 ;; Verify the state at that place
173 ;; so we don't get fooled by \" or \;.
174 (setq state (parse-partial-sexp prev (point)
176 (let ((here (point)))
177 (if (or (nth 4 state) (nth 7 state))
178 ;; We found a real comment start.
179 (let ((beg (match-beginning 0)))
182 (narrow-to-region (point-min) end)
186 ;; forward-comment skips all whitespace,
187 ;; so go back to the real end of the comment.
188 (skip-chars-backward " \t"))
189 (error (goto-char end))))
190 (put-text-property beg (point) 'face font-lock-comment-face)
191 (setq state (parse-partial-sexp here (point) nil nil state)))
193 (let ((beg (match-beginning 0)))
194 (while (and (re-search-forward "\\s\"" end 'move)
195 (nth 3 (parse-partial-sexp here (point)
197 (put-text-property beg (point) 'face font-lock-string-face)
198 (setq state (parse-partial-sexp here (point) nil nil state))))
200 ;; Make sure PREV is non-nil after the loop
201 ;; only if it was set on the very last iteration.
204 (remove-text-properties prev end '(face nil)))
205 (set-buffer-modified-p modified))))
207 ;; This code used to be used to show a string on reaching the end of it.
208 ;; It is probably not needed due to later changes to handle strings
209 ;; starting before the region in question.
210 ;; (if (and (null (nth 3 state))
211 ;; (eq (char-syntax (preceding-char)) ?\")
213 ;; (nth 3 (parse-partial-sexp prev (1- (point))
214 ;; nil nil prevstate))))
215 ;; ;; We found the end of a string.
217 ;; (setq foo2 (point))
218 ;; (let ((ept (point)))
220 ;; ;; Highlight the string when we see the end.
221 ;; ;; Doing it at the start leads to trouble:
222 ;; ;; either it fails to handle multiline strings
223 ;; ;; or it can run away when an unmatched " is inserted.
224 ;; (put-text-property (point) ept 'face
225 ;; (if (= (car state) 1)
226 ;; font-lock-doc-string-face
227 ;; font-lock-string-face)))))
229 (defun font-lock-unfontify-region (beg end)
230 (let ((modified (buffer-modified-p))
231 (buffer-read-only nil))
232 (remove-text-properties beg end '(face nil))
233 (set-buffer-modified-p modified)))
235 ;; Called when any modification is made to buffer text.
236 (defun font-lock-after-change-function (beg end old-len)
240 ;; Discard the cache info if text before it has changed.
241 (and font-lock-cache-position
242 (> font-lock-cache-position beg)
243 (setq font-lock-cache-position nil))
244 ;; Rescan till end of line. yes!
251 ;; First scan for strings and comments.
252 ;; Must scan from line start in case of
253 ;; inserting space into `intfoo () {}'.
254 (font-lock-fontify-region beg (1+ end))
255 ;; Now scan for keywords.
256 (font-lock-hack-keywords beg end))))
258 ;;; Fontifying arbitrary patterns
260 (defsubst font-lock-any-properties-p (start end)
261 (or (get-text-property start 'font-lock)
262 (let ((next (next-single-property-change start 'font-lock)))
263 (and next (< next end)))))
265 (defun font-lock-hack-keywords (start end &optional loudly)
267 (let ((case-fold-search font-lock-keywords-case-fold-search)
268 (rest font-lock-keywords)
270 (buffer-read-only nil)
271 (modified (buffer-modified-p))
272 first str match face s e allow-overlap-p)
274 (setq first (car rest) rest (cdr rest))
277 (setq str (car first))
278 (cond ((consp (cdr first))
279 (setq match (nth 1 first)
280 face (eval (nth 2 first))
281 allow-overlap-p (nth 3 first)))
282 ((symbolp (cdr first))
283 (setq match 0 allow-overlap-p nil
284 face (eval (cdr first))))
286 (setq match (cdr first)
288 face font-lock-keyword-face))))
290 (setq str first match 0 allow-overlap-p nil
291 face font-lock-keyword-face)))
292 ;(message "regexp: %s" str)
293 (while (re-search-forward str end t)
294 (setq s (match-beginning match)
296 (or s (error "expression did not match subexpression %d" match))
297 ;; don't fontify this keyword if we're already in some other context.
298 (or (if allow-overlap-p nil (font-lock-any-properties-p s e))
300 (put-text-property s e 'face face))))
301 (if loudly (message "Fontifying %s... (regexps...%s)"
303 (make-string (setq count (1+ count)) ?.))))
304 (set-buffer-modified-p modified)))
306 ;; The user level functions
308 (defvar font-lock-mode nil) ; for modeline
309 (or (assq 'font-lock-mode minor-mode-alist)
310 (setq minor-mode-alist
311 (append minor-mode-alist
312 '((font-lock-mode " Font")))))
314 (defvar font-lock-fontified nil) ; whether we have hacked this buffer
315 (put 'font-lock-fontified 'permanent-local t)
318 (defun font-lock-mode (&optional arg)
319 "Toggle Font Lock mode.
320 With arg, turn Font Lock mode on if and only if arg is positive.
322 When Font Lock mode is enabled, text is fontified as you type it:
324 - comments are displayed in `font-lock-comment-face';
325 (That is a variable whose value should be a face name.)
326 - strings are displayed in `font-lock-string-face';
327 - documentation strings are displayed in `font-lock-doc-string-face';
328 - function and variable names in their defining forms are displayed
329 in `font-lock-function-name-face';
330 - and certain other expressions are displayed in other faces
331 according to the value of the variable `font-lock-keywords'.
333 When you turn Font Lock mode on/off, the buffer is fontified/defontified.
334 To fontify a buffer without having newly typed text become fontified, you
335 can use \\[font-lock-fontify-buffer]."
337 (let ((on-p (if (null arg)
339 (> (prefix-numeric-value arg) 0))))
340 (if (equal (buffer-name) " *Compiler Input*") ; hack for bytecomp...
342 (or (memq after-change-function
343 '(nil font-lock-after-change-function))
344 (error "after-change-function is %s" after-change-function))
345 (set (make-local-variable 'after-change-function)
346 (if on-p 'font-lock-after-change-function nil))
347 (set (make-local-variable 'font-lock-mode) on-p)
349 (font-lock-set-defaults)
350 (run-hooks 'font-lock-mode-hook)
351 (or font-lock-fontified (font-lock-fontify-buffer)))
353 (setq font-lock-fontified nil)
354 (font-lock-unfontify-region (point-min) (point-max))))
355 (force-mode-line-update)))
358 (defun font-lock-fontify-buffer ()
359 "Fontify the current buffer the way `font-lock-mode' would:
361 - comments are displayed in `font-lock-comment-face';
362 - strings are displayed in `font-lock-string-face';
363 - documentation strings are displayed in `font-lock-doc-string-face';
364 - function and variable names in their defining forms are displayed
365 in `font-lock-function-name-face';
366 - and certain other expressions are displayed in other faces
367 according to the value of the variable `font-lock-keywords'.
369 This can take a while for large buffers."
371 (let ((was-on font-lock-mode)
372 (font-lock-verbose (or font-lock-verbose (interactive-p))))
373 (if font-lock-verbose (message "Fontifying %s..." (buffer-name)))
374 ;; Turn it on to run hooks and get the right font-lock-keywords.
375 (or was-on (font-lock-mode 1))
376 (font-lock-unfontify-region (point-min) (point-max))
377 (if font-lock-verbose (message "Fontifying %s... (syntactically...)"
379 ;; (buffer-syntactic-context-flush-cache)
381 (font-lock-fontify-region (point-min) (point-max))
382 (if font-lock-verbose (message "Fontifying %s... (regexps...)"
384 (font-lock-hack-keywords (point-min) (point-max) font-lock-verbose))
385 (or was-on (font-lock-mode 0)) ; turn it off if it was off.
386 (set (make-local-variable 'font-lock-fontified) t)
387 (if font-lock-verbose (message "Fontifying %s... done." (buffer-name)))
391 ;;; Various mode-specific information.
393 (defun font-lock-set-defaults ()
394 "sets font-lock-keywords to something appropriate for this mode."
395 (setq font-lock-keywords
396 (cond ((eq major-mode 'lisp-mode) lisp-font-lock-keywords)
397 ((eq major-mode 'emacs-lisp-mode) lisp-font-lock-keywords)
398 ((eq major-mode 'c-mode) c-font-lock-keywords)
399 ((eq major-mode 'c++-c-mode) c-font-lock-keywords)
400 ((eq major-mode 'c++-mode) c++-font-lock-keywords)
401 ((eq major-mode 'perl-mode) perl-font-lock-keywords)
402 ((eq major-mode 'tex-mode) tex-font-lock-keywords)
403 ((eq major-mode 'texinfo-mode) texi-font-lock-keywords)
406 (defconst lisp-font-lock-keywords-1
408 ;; highlight defining forms. This doesn't work too nicely for
409 ;; (defun (setf foo) ...) but it does work for (defvar foo) which
410 ;; is more important.
411 ("^(def[-a-z]+\\s +\\([^ \t\n\)]+\\)" 1 font-lock-function-name-face)
413 ;; highlight CL keywords
414 ("\\s :\\(\\(\\sw\\|\\s_\\)+\\)\\>" . 1)
416 ;; this is highlights things like (def* (setf foo) (bar baz)), but may
417 ;; be slower (I haven't really thought about it)
418 ; ("^(def[-a-z]+\\s +\\(\\s(\\S)*\\s)\\|\\S(\\S *\\)"
419 ; 1 font-lock-function-name-face)
421 "For consideration as a value of `lisp-font-lock-keywords'.
422 This does fairly subdued highlighting.")
424 (defconst lisp-font-lock-keywords-2
426 lisp-font-lock-keywords-1
428 ;; Highlight control structures
429 ("(\\(cond\\|if\\|when\\|unless\\|[ec]?\\(type\\)?case\\)[ \t\n]" . 1)
430 ("(\\(while\\|do\\|let*?\\|flet\\|labels\\|prog[nv12*]?\\)[ \t\n]" . 1)
431 ("(\\(catch\\|\\throw\\|block\\|return\\|return-from\\)[ \t\n]" . 1)
432 ("(\\(save-restriction\\|save-window-restriction\\)[ \t\n]" . 1)
433 ("(\\(save-excursion\\|unwind-protect\\|condition-case\\)[ \t\n]" . 1)
435 ;; highlight function names in emacs-lisp docstrings (in the syntax
436 ;; that substitute-command-keys understands.)
437 ("\\\\\\\\\\[\\([^]\\\n]+\\)]" 1 font-lock-keyword-face t)
439 ;; highlight words inside `' which tend to be function names
440 ("`\\([-a-zA-Z0-9_][-a-zA-Z0-9_][-a-zA-Z0-9_.]+\\)'"
441 1 font-lock-keyword-face t)
443 "For consideration as a value of `lisp-font-lock-keywords'.
444 This does a lot more highlighting.")
446 ;; default to the gaudier variety?
447 ;(defvar lisp-font-lock-keywords lisp-font-lock-keywords-2
448 ; "Additional expressions to highlight in Lisp modes.")
449 (defvar lisp-font-lock-keywords lisp-font-lock-keywords-1
450 "Additional expressions to highlight in Lisp modes.")
453 (defconst c-font-lock-keywords-1 nil
454 "For consideration as a value of `c-font-lock-keywords'.
455 This does fairly subdued highlighting.")
457 (defconst c-font-lock-keywords-2 nil
458 "For consideration as a value of `c-font-lock-keywords'.
459 This does a lot more highlighting.")
461 (let ((storage "auto\\|extern\\|register\\|static\\|volatile")
462 (prefixes "unsigned\\|short\\|long")
463 (types (concat "int\\|char\\|float\\|double\\|void\\|struct\\|"
464 "union\\|enum\\|typedef"))
465 (ctoken "[a-zA-Z0-9_:~*]+")
467 (setq c-font-lock-keywords-1
469 ;; fontify preprocessor directives as comments.
470 '("^#[ \t]*[a-z]+" . font-lock-comment-face)
472 ;; fontify names being defined.
473 '("^#[ \t]*\\(define\\|undef\\)[ \t]+\\(\\(\\sw\\|\\s_\\)+\\)" 2
474 font-lock-function-name-face)
476 ;; fontify other preprocessor lines.
477 '("^#[ \t]*\\(if\\|ifn?def\\)[ \t]+\\([^\n]+\\)"
478 2 font-lock-function-name-face t)
480 ;; fontify the filename in #include <...>
481 ;; don't need to do this for #include "..." because those were
482 ;; already fontified as strings by the syntactic pass.
483 '("^#[ \t]*include[ \t]+\\(<[^>\"\n]+>\\)" 1 font-lock-string-face)
485 ;; fontify the names of functions being defined.
487 "^\\(" ctoken "[ \t]+\\)?" ; type specs; there can be no
488 "\\(" ctoken "[ \t]+\\)?" ; more than 3 tokens, right?
489 "\\(" ctoken "[ \t]+\\)?"
490 "\\([*&]+[ \t]*\\)?" ; pointer
491 "\\(" ctoken "\\)[ \t]*(") ; name
492 5 'font-lock-function-name-face)
495 ;; Fontify structure names (in structure definition form).
496 (list (concat "^\\(typedef[ \t]+struct\\|struct\\|static[ \t]+struct\\)"
497 "[ \t]+\\(" ctoken "\\)[ \t]*\\(\{\\|$\\)")
498 2 'font-lock-function-name-face)
500 ;; Fontify case clauses. This is fast because its anchored on the left.
501 '("case[ \t]+\\(\\(\\sw\\|\\s_\\)+\\):". 1)
502 '("\\<\\(default\\):". 1)
505 (setq c-font-lock-keywords-2
506 (append c-font-lock-keywords-1
509 ;; fontify all storage classes and type specifiers
510 (cons (concat "\\<\\(" storage "\\)\\>") 'font-lock-type-face)
511 (cons (concat "\\<\\(" types "\\)\\>") 'font-lock-type-face)
512 (cons (concat "\\<\\(" prefixes "[ \t]+" types "\\)\\>")
513 'font-lock-type-face)
515 ;; fontify all builtin tokens
519 '("for" "while" "do" "return" "goto" "case" "break" "switch"
520 "if" "then" "else if" "else" "return" "default" "continue"
527 ;; fontify case targets and goto-tags. This is slow because the
528 ;; expression is anchored on the right.
529 "\\(\\(\\sw\\|\\s_\\)+\\):"
531 ;; Fontify variables declared with structures, or typedef names.
532 '("}[ \t*]*\\(\\(\\sw\\|\\s_\\)+\\)[ \t]*[,;]"
533 1 font-lock-function-name-face)
535 ;; Fontify global variables without a type.
536 ; '("^\\([_a-zA-Z0-9:~*]+\\)[ \t]*[[;={]" 1 font-lock-function-name-face)
541 ; default to the gaudier variety?
542 ;(defvar c-font-lock-keywords c-font-lock-keywords-2
543 ; "Additional expressions to highlight in C mode.")
544 (defvar c-font-lock-keywords c-font-lock-keywords-1
545 "Additional expressions to highlight in C mode.")
547 (defvar c++-font-lock-keywords c-font-lock-keywords
548 "Additional expressions to highlight in C++ mode.")
551 (defvar perl-font-lock-keywords
553 (cons (concat "[ \n\t{]*\\("
555 '("if" "until" "while" "elsif" "else" "unless" "for"
556 "foreach" "continue" "exit" "die" "last" "goto" "next"
557 "redo" "return" "local" "exec")
561 '("#endif" "#else" "#ifdef" "#ifndef" "#if" "#include"
564 '("^[ \n\t]*sub[ \t]+\\([^ \t{]+\\)[ \t]*[{]" 1 font-lock-function-name-face)
565 '("[ \n\t{]*\\(eval\\)[ \n\t(;]" 1 font-lock-function-name-face)
566 '("\\(--- .* ---\\|=== .* ===\\)" . font-lock-doc-string-face)
568 "Additional expressions to highlight in Perl mode.")
570 (defvar tex-font-lock-keywords
572 '("\\(\\\\\\w+\\)" 1 font-lock-keyword-face t)
573 '("{\\\\em\\([^}]+\\)}" 1 font-lock-comment-face t)
574 '("{\\\\bf\\([^}]+\\)}" 1 font-lock-keyword-face t)
575 '("^[ \t\n]*\\\\def[\\\\@]\\(\\w+\\)" 1 font-lock-function-name-face t)
576 '("\\\\\\(begin\\|end\\){\\([a-zA-Z0-9\\*]+\\)}"
577 2 font-lock-function-name-face t)
578 '("[^\\\\]\\$\\([^$]*\\)\\$" 1 font-lock-string-face t)
579 ; '("\\$\\([^$]*\\)\\$" 1 font-lock-string-face t)
581 "Additional expressions to highlight in TeX mode.")
583 (defvar texi-font-lock-keywords
585 "@\\(@\\|[^}\t \n{]+\\)" ;commands
586 '("^\\(@c\\|@comment\\)[ \t].*$" . font-lock-comment-face) ;comments
587 '("^\\(*.*\\)[\t ]*$" 1 font-lock-function-name-face t) ;menu items
588 '("@\\(emph\\|strong\\|b\\|i\\){\\([^}]+\\)" 2 font-lock-comment-face t)
589 '("@\\(file\\|kbd\\|key\\){\\([^}]+\\)" 2 font-lock-string-face t)
590 '("@\\(samp\\|code\\|var\\){\\([^}]+\\)" 2 font-lock-function-name-face t)
591 '("@\\(xref\\|pxref\\){\\([^}]+\\)" 2 font-lock-keyword-face t)
592 '("@end *\\([a-zA-Z0-9]+\\)[ \t]*$" 1 font-lock-function-name-face t)
593 '("@item \\(.*\\)$" 1 font-lock-function-name-face t)
594 '("\\$\\([^$]*\\)\\$" 1 font-lock-string-face t)
596 "Additional expressions to highlight in TeXinfo mode.")
600 ;;; font-lock.el ends here