Initial Commit
[packages] / xemacs-packages / auctex / tex-font.el
1 ;;; tex-font.el --- Font-Lock support stolen from Emacs 21.
2 ;;
3 ;; Copyright (C) 1985, 86, 89, 92, 94, 95, 96, 97, 98, 1999
4 ;;       Free Software Foundation, Inc.
5
6 ;; Maintainer: auctex-devel@gnu.org
7 ;; Keywords: tex, faces
8
9 ;; This file is part of AUC TeX.
10
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)
14 ;; any later version.
15
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.
20
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.
25
26 ;;; Comments:
27
28 ;; Please keep this file in sync with GNU Emacs 21.
29
30 ;;; Code:
31
32 (defconst tex-font-lock-keywords-1
33   (eval-when-compile
34     (let* (;; Names of commands whose arg should be fontified as heading, etc.
35            (headings (regexp-opt
36                       '("title"  "begin" "end" "chapter" "part"
37                         "section" "subsection" "subsubsection"
38                         "paragraph" "subparagraph" "subsubparagraph"
39                         "newcommand" "renewcommand" "newenvironment"
40                         "newtheorem")
41                       t))
42            (variables (regexp-opt
43                        '("newcounter" "newcounter*" "setcounter" "addtocounter"
44                          "setlength" "addtolength" "settowidth")
45                        t))
46            (includes (regexp-opt
47                       '("input" "include" "includeonly" "bibliography"
48                         "epsfig" "psfig" "epsf" "nofiles" "usepackage"
49                         "documentstyle" "documentclass" "verbatiminput"
50                         "includegraphics" "includegraphics*")
51                       t))
52            ;; Miscellany.
53            (slash "\\\\")
54            (opt " *\\(\\[[^]]*\\] *\\)*")
55            ;; This would allow highlighting \newcommand\CMD but requires
56            ;; adapting subgroup numbers below.
57            ;; (arg "\\(?:{\\(\\(?:[^{}\\]+\\|\\\\.\\|{[^}]*}\\)+\\)\\|\\\\[a-z*]+\\)"))
58            (arg "{\\(\\(?:[^{}\\]+\\|\\\\.\\|{[^}]*}\\)+\\)"))
59       (list
60        ;; Heading args.
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)
77        ;; Variable args.
78        (list (concat slash variables " *" arg) 2 'font-lock-variable-name-face)
79        ;; Include args.
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.")
85
86 (defconst tex-font-lock-keywords-2
87   (append tex-font-lock-keywords-1
88    (eval-when-compile
89      (let* (;;
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))
95             ;;
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"
102                           )
103                         t))
104             ;;
105             ;; Names of commands that should be fontified.
106             (specials (regexp-opt
107                        '("\\" "\\*" ;; "-"
108                          "linebreak" "nolinebreak" "pagebreak" "nopagebreak"
109                          "newline" "newpage" "clearpage" "cleardoublepage"
110                          "displaybreak" "allowdisplaybreaks" "enlargethispage")
111                        t))
112             (general "\\([a-zA-Z@]+\\**\\|[^ \t\n]\\)")
113             ;;
114             ;; Miscellany.
115             (slash "\\\\")
116             (opt " *\\(\\[[^]]*\\] *\\)*")
117             (arg "{\\(\\(?:[^{}\\]+\\|\\\\.\\|{[^}]*}\\)+\\)"))
118        (list
119         ;;
120         ;; Citation args.
121         (list (concat slash citations opt arg) 3 'font-lock-constant-face)
122         ;;
123         ;; Text between `` quotes ''.
124         (cons (concat (regexp-opt `("``" "\"<" "\"`" "<<" "«") t)
125                       "[^'\">»]+"       ;a bit pessimistic
126                       (regexp-opt `("''" "\">" "\"'" ">>" "»") t))
127               'font-lock-string-face)
128         ;;
129         ;; Command names, special and general.
130         (cons (concat slash specials) 'font-lock-warning-face)
131         (concat slash general)
132         ;;
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)
138         ;;
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.")
144
145 (defvar tex-font-lock-keywords tex-font-lock-keywords-1
146   "Default expressions to highlight in TeX modes.")
147
148
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)
153
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))
157
158 ;;;###autoload
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)))
169     )
170
171 (provide 'tex-font)
172
173 ;;; tex-font.el ends here