1 ;;; tex-font.el --- Font-Lock support stolen from Emacs 21.
3 ;; Copyright (C) 1985, 86, 89, 92, 94, 95, 96, 97, 98, 1999
4 ;; Free Software Foundation, Inc.
6 ;; Maintainer: auctex-devel@gnu.org
7 ;; Keywords: tex, faces
9 ;; This file is part of AUC TeX.
11 ;; AUC TeX is free software; you can redistribute it and/or modify
12 ;; it under the terms of the GNU General Public License as published by
13 ;; the Free Software Foundation; either version 3, or (at your option)
16 ;; AUC TeX is distributed in the hope that it will be useful,
17 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
18 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 ;; GNU General Public License for more details.
21 ;; You should have received a copy of the GNU General Public License
22 ;; along with AUC TeX; see the file COPYING. If not, write to the
23 ;; Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
24 ;; Boston, MA 02110-1301, USA.
28 ;; Please keep this file in sync with GNU Emacs 21.
32 (defconst tex-font-lock-keywords-1
34 (let* (;; Names of commands whose arg should be fontified as heading, etc.
36 '("title" "begin" "end" "chapter" "part"
37 "section" "subsection" "subsubsection"
38 "paragraph" "subparagraph" "subsubparagraph"
39 "newcommand" "renewcommand" "newenvironment"
42 (variables (regexp-opt
43 '("newcounter" "newcounter*" "setcounter" "addtocounter"
44 "setlength" "addtolength" "settowidth")
47 '("input" "include" "includeonly" "bibliography"
48 "epsfig" "psfig" "epsf" "nofiles" "usepackage"
49 "documentstyle" "documentclass" "verbatiminput"
50 "includegraphics" "includegraphics*")
54 (opt " *\\(\\[[^]]*\\] *\\)*")
55 ;; This would allow highlighting \newcommand\CMD but requires
56 ;; adapting subgroup numbers below.
57 ;; (arg "\\(?:{\\(\\(?:[^{}\\]+\\|\\\\.\\|{[^}]*}\\)+\\)\\|\\\\[a-z*]+\\)"))
58 (arg "{\\(\\(?:[^{}\\]+\\|\\\\.\\|{[^}]*}\\)+\\)"))
61 (list (concat slash headings "\\*?" opt arg)
62 ;; If ARG ends up matching too much (if the {} don't match, f.ex)
63 ;; jit-lock will do funny things: when updating the buffer
64 ;; the re-highlighting is only done locally so it will just
65 ;; match the local line, but defer-contextually will
66 ;; match more lines at a time, so ARG will end up matching
67 ;; a lot more, which might suddenly include a comment
68 ;; so you get things highlighted bold when you type them
69 ;; but they get turned back to normal a little while later
70 ;; because "there's already a face there".
71 ;; Using `keep' works around this un-intuitive behavior as well
72 ;; as improves the behavior in the very rare case where you do
73 ;; have a comment in ARG.
74 3 'font-lock-function-name-face 'keep)
75 (list (concat slash "\\(re\\)?newcommand\\** *\\(\\\\[A-Za-z@]+\\)")
76 2 'font-lock-function-name-face 'keep)
78 (list (concat slash variables " *" arg) 2 'font-lock-variable-name-face)
80 (list (concat slash includes opt arg) 3 'font-lock-builtin-face)
81 ;; Definitions. I think.
82 '("^[ \t]*\\\\def *\\\\\\(\\(\\w\\|@\\)+\\)"
83 1 font-lock-function-name-face))))
84 "Subdued expressions to highlight in TeX modes.")
86 (defconst tex-font-lock-keywords-2
87 (append tex-font-lock-keywords-1
90 ;; Names of commands whose arg should be fontified with fonts.
91 (bold (regexp-opt '("textbf" "textsc" "textup"
92 "boldsymbol" "pmb") t))
93 (italic (regexp-opt '("textit" "textsl" "emph") t))
94 (type (regexp-opt '("texttt" "textmd" "textrm" "textsf") t))
96 ;; Names of commands whose arg should be fontified as a citation.
97 (citations (regexp-opt
98 '("label" "ref" "pageref" "vref" "eqref"
99 "cite" "nocite" "index" "glossary" "bibitem"
100 ;; These are text, rather than citations.
101 ;; "caption" "footnote" "footnotemark" "footnotetext"
105 ;; Names of commands that should be fontified.
106 (specials (regexp-opt
108 "linebreak" "nolinebreak" "pagebreak" "nopagebreak"
109 "newline" "newpage" "clearpage" "cleardoublepage"
110 "displaybreak" "allowdisplaybreaks" "enlargethispage")
112 (general "\\([a-zA-Z@]+\\**\\|[^ \t\n]\\)")
116 (opt " *\\(\\[[^]]*\\] *\\)*")
117 (arg "{\\(\\(?:[^{}\\]+\\|\\\\.\\|{[^}]*}\\)+\\)"))
121 (list (concat slash citations opt arg) 3 'font-lock-constant-face)
123 ;; Text between `` quotes ''.
124 (cons (concat (regexp-opt `("``" "\"<" "\"`" "<<" "«") t)
125 "[^'\">»]+" ;a bit pessimistic
126 (regexp-opt `("''" "\">" "\"'" ">>" "»") t))
127 'font-lock-string-face)
129 ;; Command names, special and general.
130 (cons (concat slash specials) 'font-lock-warning-face)
131 (concat slash general)
133 ;; Font environments. It seems a bit dubious to use `bold' etc. faces
134 ;; since we might not be able to display those fonts.
135 (list (concat slash bold " *" arg) 2 '(quote bold) 'append)
136 (list (concat slash italic " *" arg) 2 '(quote italic) 'append)
137 ;; (list (concat slash type arg) 2 '(quote bold-italic) 'append)
139 ;; Old-style bf/em/it/sl. Stop at `\\' and un-escaped `&', for tables.
140 (list (concat "\\\\\\(\\(bf\\)\\|em\\|it\\|sl\\)\\>"
141 "\\(\\([^}&\\]\\|\\\\[^\\]\\)+\\)")
142 3 '(if (match-beginning 2) 'bold 'italic) 'append)))))
143 "Gaudy expressions to highlight in TeX modes.")
145 (defvar tex-font-lock-keywords tex-font-lock-keywords-1
146 "Default expressions to highlight in TeX modes.")
149 (defface tex-math-face
150 '((t :inherit font-lock-string-face))
151 "Face used to highlight TeX math expressions.")
152 (defvar tex-math-face 'tex-math-face)
154 ;; Use string syntax but math face for $...$.
155 (defun tex-font-lock-syntactic-face-function (state)
156 (if (nth 3 state) tex-math-face font-lock-comment-face))
159 (defun tex-font-setup ()
160 "Setup font lock support for TeX."
161 (set (make-local-variable 'font-lock-defaults)
162 '((tex-font-lock-keywords
163 tex-font-lock-keywords-1 tex-font-lock-keywords-2)
164 nil nil ((?$ . "\"")) nil
165 ;; Who ever uses that anyway ???
166 (font-lock-mark-block-function . mark-paragraph)
167 (font-lock-syntactic-face-function
168 . tex-font-lock-syntactic-face-function)))
173 ;;; tex-font.el ends here