(Man-follow-manual-reference): If current-word returns nil, use "".
[bpt/emacs.git] / lisp / textmodes / tex-mode.el
CommitLineData
95a045bd 1;;; tex-mode.el --- TeX, LaTeX, and SliTeX mode commands -*- coding: utf-8 -*-
d501f516 2
dd166d5f 3;; Copyright (C) 1985, 1986, 1989, 1992, 1994, 1995, 1996, 1997, 1998, 1999,
6ecedb40 4;; 2002, 2003, 2004, 2005 Free Software Foundation, Inc.
eea8d4ef 5
70f20973 6;; Maintainer: FSF
d7b4d18f 7;; Keywords: tex
e5167999 8
528415e7 9;; Contributions over the years by William F. Schelter, Dick King,
70f20973 10;; Stephen Gildea, Michael Prange, Jacob Gore, and Edward M. Reingold.
528415e7 11
869bff31 12;; This file is part of GNU Emacs.
13
14;; GNU Emacs is free software; you can redistribute it and/or modify
15;; it under the terms of the GNU General Public License as published by
e5167999 16;; the Free Software Foundation; either version 2, or (at your option)
869bff31 17;; any later version.
18
19;; GNU Emacs is distributed in the hope that it will be useful,
20;; but WITHOUT ANY WARRANTY; without even the implied warranty of
21;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22;; GNU General Public License for more details.
23
24;; You should have received a copy of the GNU General Public License
6da9bbd6
RS
25;; along with GNU Emacs; see the file COPYING. If not, write to the
26;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
27;; Boston, MA 02111-1307, USA.
869bff31 28
55535639
PJ
29;;; Commentary:
30
e5167999
ER
31;;; Code:
32
bed1b0c8
RS
33;; Pacify the byte-compiler
34(eval-when-compile
35 (require 'compare-w)
ea590e1c 36 (require 'cl)
bed1b0c8
RS
37 (require 'skeleton))
38
a0e9c22a 39(require 'shell)
2f3067de 40(require 'compile)
528415e7 41
d61140e8
RS
42(defgroup tex-file nil
43 "TeX files and directories"
44 :prefix "tex-"
45 :group 'tex)
46
47(defgroup tex-run nil
48 "Running external commands from TeX mode"
49 :prefix "tex-"
50 :group 'tex)
51
52(defgroup tex-view nil
53 "Viewing and printing TeX files"
54 :prefix "tex-"
55 :group 'tex)
56
7e1dae73 57;;;###autoload
d61140e8
RS
58(defcustom tex-shell-file-name nil
59 "*If non-nil, the shell file name to run in the subshell used to run TeX."
60 :type '(choice (const :tag "None" nil)
61 string)
62 :group 'tex-run)
528415e7 63
7e1dae73 64;;;###autoload
d61140e8 65(defcustom tex-directory "."
f73741fc 66 "*Directory in which temporary files are written.
81c735c0 67You can make this `/tmp' if your TEXINPUTS has no relative directories in it
528415e7 68and you don't try to apply \\[tex-region] or \\[tex-buffer] when there are
d61140e8
RS
69`\\input' commands with relative directories."
70 :type 'directory
71 :group 'tex-file)
869bff31 72
725b7428 73;;;###autoload
d61140e8 74(defcustom tex-first-line-header-regexp nil
725b7428
RS
75 "Regexp for matching a first line which `tex-region' should include.
76If this is non-nil, it should be a regular expression string;
77if it matches the first line of the file,
d61140e8
RS
78`tex-region' always includes the first line in the TeX run."
79 :type '(choice (const :tag "None" nil)
80 regexp)
81 :group 'tex-file)
725b7428 82
f73741fc 83;;;###autoload
d61140e8 84(defcustom tex-main-file nil
f73741fc 85 "*The main TeX source file which includes this buffer's file.
be4a962d
RS
86The command `tex-file' runs TeX on the file specified by `tex-main-file'
87if the variable is non-nil."
d61140e8
RS
88 :type '(choice (const :tag "None" nil)
89 file)
90 :group 'tex-file)
f73741fc 91
7e1dae73 92;;;###autoload
d61140e8
RS
93(defcustom tex-offer-save t
94 "*If non-nil, ask about saving modified buffers before \\[tex-file] is run."
95 :type 'boolean
96 :group 'tex-file)
869bff31 97
7e1dae73 98;;;###autoload
d61140e8 99(defcustom tex-run-command "tex"
869bff31 100 "*Command used to run TeX subjob.
c3ce7bf4
RS
101TeX Mode sets `tex-command' to this string.
102See the documentation of that variable."
d61140e8
RS
103 :type 'string
104 :group 'tex-run)
869bff31 105
7e1dae73 106;;;###autoload
d61140e8 107(defcustom latex-run-command "latex"
869bff31 108 "*Command used to run LaTeX subjob.
c3ce7bf4
RS
109LaTeX Mode sets `tex-command' to this string.
110See the documentation of that variable."
d61140e8
RS
111 :type 'string
112 :group 'tex-run)
528415e7 113
bed1b0c8
RS
114;;;###autoload
115(defcustom slitex-run-command "slitex"
116 "*Command used to run SliTeX subjob.
c3ce7bf4
RS
117SliTeX Mode sets `tex-command' to this string.
118See the documentation of that variable."
bed1b0c8
RS
119 :type 'string
120 :group 'tex-run)
121
c3ce7bf4 122;;;###autoload
95a045bd 123(defcustom tex-start-options ""
9e0ad27a 124 "*TeX options to use when starting TeX.
95a045bd
SM
125These immediately precede the commands in `tex-start-commands'
126and the input file name, with no separating space and are not shell-quoted.
127If nil, TeX runs with no options. See the documentation of `tex-command'."
128 :type 'string
bed1b0c8 129 :group 'tex-run
bf247b6e 130 :version "22.1")
9e0ad27a
RS
131
132;;;###autoload
133(defcustom tex-start-commands "\\nonstopmode\\input"
134 "*TeX commands to use when starting TeX.
95a045bd
SM
135They are shell-quoted and precede the input file name, with a separating space.
136If nil, no commands are used. See the documentation of `tex-command'."
9e0ad27a
RS
137 :type '(radio (const :tag "Interactive \(nil\)" nil)
138 (const :tag "Nonstop \(\"\\nonstopmode\\input\"\)"
139 "\\nonstopmode\\input")
140 (string :tag "String at your choice"))
141 :group 'tex-run
bf247b6e 142 :version "22.1")
bed1b0c8 143
7afecb99 144(defvar latex-standard-block-names
dd459839
SM
145 '("abstract" "array" "center" "description"
146 "displaymath" "document" "enumerate" "eqnarray"
147 "eqnarray*" "equation" "figure" "figure*"
148 "flushleft" "flushright" "itemize" "letter"
149 "list" "minipage" "picture" "quotation"
150 "quote" "slide" "sloppypar" "tabbing"
151 "table" "table*" "tabular" "tabular*"
152 "thebibliography" "theindex*" "titlepage" "trivlist"
153 "verbatim" "verbatim*" "verse" "math")
528415e7
RS
154 "Standard LaTeX block names.")
155
7e1dae73 156;;;###autoload
d61140e8 157(defcustom latex-block-names nil
528415e7 158 "*User defined LaTeX block names.
7afecb99 159Combined with `latex-standard-block-names' for minibuffer completion."
d61140e8
RS
160 :type '(repeat string)
161 :group 'tex-run)
869bff31 162
7e1dae73 163;;;###autoload
d61140e8 164(defcustom tex-bibtex-command "bibtex"
528415e7 165 "*Command used by `tex-bibtex-file' to gather bibliographic data.
81c735c0 166If this string contains an asterisk (`*'), that is replaced by the file name;
d61140e8
RS
167otherwise, the file name, preceded by blank, is added at the end."
168 :type 'string
169 :group 'tex-run)
869bff31 170
7e1dae73 171;;;###autoload
d61140e8 172(defcustom tex-dvi-print-command "lpr -d"
528415e7 173 "*Command used by \\[tex-print] to print a .dvi file.
81c735c0 174If this string contains an asterisk (`*'), that is replaced by the file name;
d61140e8
RS
175otherwise, the file name, preceded by blank, is added at the end."
176 :type 'string
177 :group 'tex-view)
528415e7 178
7e1dae73 179;;;###autoload
d61140e8 180(defcustom tex-alt-dvi-print-command "lpr -d"
528415e7 181 "*Command used by \\[tex-print] with a prefix arg to print a .dvi file.
81c735c0
RS
182If this string contains an asterisk (`*'), that is replaced by the file name;
183otherwise, the file name, preceded by blank, is added at the end.
528415e7 184
81c735c0
RS
185If two printers are not enough of a choice, you can set the variable
186`tex-alt-dvi-print-command' to an expression that asks what you want;
528415e7
RS
187for example,
188
189 (setq tex-alt-dvi-print-command
190 '(format \"lpr -P%s\" (read-string \"Use printer: \")))
191
192would tell \\[tex-print] with a prefix argument to ask you which printer to
d61140e8
RS
193use."
194 :type '(choice (string :tag "Command")
195 (sexp :tag "Expression"))
196 :group 'tex-view)
869bff31 197
7e1dae73 198;;;###autoload
dd166d5f
SM
199(defcustom tex-dvi-view-command
200 '(cond
201 ((eq window-system 'x) "xdvi")
bf247b6e 202 ((eq window-system 'w32) "yap")
dd166d5f 203 (t "dvi2tty * | cat -s"))
81c735c0 204 "*Command used by \\[tex-view] to display a `.dvi' file.
746c30e2 205If it is a string, that specifies the command directly.
81c735c0 206If this string contains an asterisk (`*'), that is replaced by the file name;
746c30e2 207otherwise, the file name, preceded by a space, is added at the end.
528415e7 208
746c30e2
RS
209If the value is a form, it is evaluated to get the command to use."
210 :type '(choice (const nil) string sexp)
d61140e8 211 :group 'tex-view)
869bff31 212
7e1dae73 213;;;###autoload
d61140e8 214(defcustom tex-show-queue-command "lpq"
528415e7 215 "*Command used by \\[tex-show-print-queue] to show the print queue.
d61140e8
RS
216Should show the queue(s) that \\[tex-print] puts jobs on."
217 :type 'string
218 :group 'tex-view)
869bff31 219
7e1dae73 220;;;###autoload
9920303f 221(defcustom tex-default-mode 'latex-mode
869bff31 222 "*Mode to enter for a new file that might be either TeX or LaTeX.
223This variable is used when it can't be determined whether the file
224is plain TeX or LaTeX or what because the file contains no commands.
d61140e8
RS
225Normally set to either `plain-tex-mode' or `latex-mode'."
226 :type 'function
227 :group 'tex)
869bff31 228
7e1dae73 229;;;###autoload
d61140e8
RS
230(defcustom tex-open-quote "``"
231 "*String inserted by typing \\[tex-insert-quote] to open a quotation."
232 :type 'string
95a045bd 233 :options '("``" "\"<" "\"`" "<<" "«")
d61140e8 234 :group 'tex)
869bff31 235
7e1dae73 236;;;###autoload
d61140e8
RS
237(defcustom tex-close-quote "''"
238 "*String inserted by typing \\[tex-insert-quote] to close a quotation."
239 :type 'string
95a045bd 240 :options '("''" "\">" "\"'" ">>" "»")
d61140e8 241 :group 'tex)
869bff31 242
528415e7
RS
243(defvar tex-last-temp-file nil
244 "Latest temporary file generated by \\[tex-region] and \\[tex-buffer].
245Deleted when the \\[tex-region] or \\[tex-buffer] is next run, or when the
81c735c0 246tex shell terminates.")
528415e7 247
95a045bd 248(defvar tex-command "tex"
c3ce7bf4 249 "*Command to run TeX.
95a045bd 250If this string contains an asterisk \(`*'\), that is replaced by the file name;
9e0ad27a
RS
251otherwise the value of `tex-start-options', the \(shell-quoted\)
252value of `tex-start-commands', and the file name are added at the end
253with blanks as separators.
c3ce7bf4
RS
254
255In TeX, LaTeX, and SliTeX Mode this variable becomes buffer local.
256In these modes, use \\[set-variable] if you want to change it for the
257current buffer.")
869bff31 258
259(defvar tex-trailer nil
260 "String appended after the end of a region sent to TeX by \\[tex-region].")
261
262(defvar tex-start-of-header nil
898b9ac1 263 "Regular expression used by \\[tex-region] to find start of file's header.")
869bff31 264
265(defvar tex-end-of-header nil
898b9ac1 266 "Regular expression used by \\[tex-region] to find end of file's header.")
869bff31 267
268(defvar tex-shell-cd-command "cd"
269 "Command to give to shell running TeX to change directory.
81c735c0 270The value of `tex-directory' is appended to this, separated by a space.")
869bff31 271
272(defvar tex-zap-file nil
273 "Temporary file name used for text being sent as input to TeX.
274Should be a simple file name with no extension or directory specification.")
275
276(defvar tex-last-buffer-texed nil
277 "Buffer which was last TeXed.")
278
279(defvar tex-print-file nil
280 "File name that \\[tex-print] prints.
281Set by \\[tex-region], \\[tex-buffer], and \\[tex-file].")
282
8a591d52
SM
283(defvar tex-mode-syntax-table
284 (let ((st (make-syntax-table)))
285 (modify-syntax-entry ?% "<" st)
286 (modify-syntax-entry ?\n ">" st)
287 (modify-syntax-entry ?\f ">" st)
288 (modify-syntax-entry ?\C-@ "w" st)
289 (modify-syntax-entry ?' "w" st)
290 (modify-syntax-entry ?@ "_" st)
291 (modify-syntax-entry ?* "_" st)
292 (modify-syntax-entry ?\t " " st)
bca7a08b
SM
293 ;; ~ is printed by TeX as a space, but it's semantics in the syntax
294 ;; of TeX is not `whitespace' (i.e. it's just like \hspace{foo}).
8a591d52
SM
295 (modify-syntax-entry ?~ "." st)
296 (modify-syntax-entry ?$ "$$" st)
297 (modify-syntax-entry ?\\ "/" st)
298 (modify-syntax-entry ?\" "." st)
299 (modify-syntax-entry ?& "." st)
300 (modify-syntax-entry ?_ "." st)
301 (modify-syntax-entry ?^ "." st)
302 st)
869bff31 303 "Syntax table used while in TeX mode.")
53c4fe47
SM
304\f
305;;;;
306;;;; Imenu support
307;;;;
869bff31 308
d6709b80 309(defcustom latex-imenu-indent-string ". "
a53c647b
RS
310 "*String to add repeated in front of nested sectional units for Imenu.
311An alternative value is \" . \", if you use a font with a narrow period."
312 :type 'string
313 :group 'tex)
314
d6709b80
SM
315(defvar latex-section-alist
316 '(("part" . 0) ("chapter" . 1)
317 ("section" . 2) ("subsection" . 3)
318 ("subsubsection" . 4)
319 ("paragraph" . 5) ("subparagraph" . 6)))
320
53c4fe47
SM
321(defvar latex-metasection-list
322 '("documentstyle" "documentclass"
323 "begin{document}" "end{document}"
324 "appendix" "frontmatter" "mainmatter" "backmatter"))
325
a5e2ae01 326(defun latex-imenu-create-index ()
53c4fe47
SM
327 "Generate an alist for imenu from a LaTeX buffer."
328 (let ((section-regexp
329 (concat "\\\\" (regexp-opt (mapcar 'car latex-section-alist) t)
330 "\\*?[ \t]*{"))
331 (metasection-regexp
332 (concat "\\\\" (regexp-opt latex-metasection-list t)))
333 i0 menu case-fold-search)
a53c647b
RS
334 (save-excursion
335 ;; Find the top-most level in this file but don't allow it to be
336 ;; any deeper than "section" (which is top-level in an article).
337 (goto-char (point-min))
338 (if (search-forward-regexp "\\\\part\\*?[ \t]*{" nil t)
339 (setq i0 0)
340 (if (search-forward-regexp "\\\\chapter\\*?[ \t]*{" nil t)
341 (setq i0 1)
342 (setq i0 2)))
343
344 ;; Look for chapters and sections.
345 (goto-char (point-min))
53c4fe47 346 (while (search-forward-regexp section-regexp nil t)
a53c647b
RS
347 (let ((start (match-beginning 0))
348 (here (point))
349 (i (cdr (assoc (buffer-substring-no-properties
350 (match-beginning 1)
351 (match-end 1))
d6709b80 352 latex-section-alist))))
a53c647b
RS
353 (backward-char 1)
354 (condition-case err
355 (progn
356 ;; Using sexps allows some use of matching {...} inside
357 ;; titles.
358 (forward-sexp 1)
d6709b80
SM
359 (push (cons (concat (apply 'concat
360 (make-list
361 (max 0 (- i i0))
362 latex-imenu-indent-string))
363 (buffer-substring-no-properties
364 here (1- (point))))
365 start)
366 menu))
a53c647b
RS
367 (error nil))))
368
369 ;; Look for included material.
370 (goto-char (point-min))
371 (while (search-forward-regexp
372 "\\\\\\(include\\|input\\|verbatiminput\\|bibliography\\)\
53c4fe47 373\[ \t]*{\\([^}\n]+\\)}"
a53c647b 374 nil t)
53c4fe47
SM
375 (push (cons (concat "<<" (buffer-substring-no-properties
376 (match-beginning 2)
377 (match-end 2))
378 (if (= (char-after (match-beginning 1)) ?b)
379 ".bbl"
380 ".tex"))
381 (match-beginning 0))
382 menu))
a53c647b
RS
383
384 ;; Look for \frontmatter, \mainmatter, \backmatter, and \appendix.
385 (goto-char (point-min))
53c4fe47
SM
386 (while (search-forward-regexp metasection-regexp nil t)
387 (push (cons "--" (match-beginning 0)) menu))
a53c647b
RS
388
389 ;; Sort in increasing buffer position order.
390 (sort menu (function (lambda (a b) (< (cdr a) (cdr b))))))))
53c4fe47
SM
391\f
392;;;;
393;;;; Outline support
394;;;;
395
396(defvar latex-outline-regexp
397 (concat "\\\\"
398 (regexp-opt (append latex-metasection-list
399 (mapcar 'car latex-section-alist)) t)))
400
401(defun latex-outline-level ()
402 (if (looking-at latex-outline-regexp)
403 (1+ (or (cdr (assoc (match-string 1) latex-section-alist)) -1))
404 1000))
405\f
406;;;;
407;;;; Font-Lock support
408;;;;
409
410;(defvar tex-font-lock-keywords
411; ;; Regexps updated with help from Ulrik Dickow <dickow@nbi.dk>.
412; '(("\\\\\\(begin\\|end\\|newcommand\\){\\([a-zA-Z0-9\\*]+\\)}"
413; 2 font-lock-function-name-face)
414; ("\\\\\\(cite\\|label\\|pageref\\|ref\\){\\([^} \t\n]+\\)}"
415; 2 font-lock-constant-face)
416; ;; It seems a bit dubious to use `bold' and `italic' faces since we might
417; ;; not be able to display those fonts.
418; ("{\\\\bf\\([^}]+\\)}" 1 'bold keep)
419; ("{\\\\\\(em\\|it\\|sl\\)\\([^}]+\\)}" 2 'italic keep)
420; ("\\\\\\([a-zA-Z@]+\\|.\\)" . font-lock-keyword-face)
421; ("^[ \t\n]*\\\\def[\\\\@]\\(\\w+\\)" 1 font-lock-function-name-face keep))
422; ;; Rewritten and extended for LaTeX2e by Ulrik Dickow <dickow@nbi.dk>.
423; '(("\\\\\\(begin\\|end\\|newcommand\\){\\([a-zA-Z0-9\\*]+\\)}"
424; 2 font-lock-function-name-face)
425; ("\\\\\\(cite\\|label\\|pageref\\|ref\\){\\([^} \t\n]+\\)}"
426; 2 font-lock-constant-face)
427; ("^[ \t]*\\\\def\\\\\\(\\(\\w\\|@\\)+\\)" 1 font-lock-function-name-face)
428; "\\\\\\([a-zA-Z@]+\\|.\\)"
429; ;; It seems a bit dubious to use `bold' and `italic' faces since we might
430; ;; not be able to display those fonts.
431; ;; LaTeX2e: \emph{This is emphasized}.
432; ("\\\\emph{\\([^}]+\\)}" 1 'italic keep)
433; ;; LaTeX2e: \textbf{This is bold}, \textit{...}, \textsl{...}
434; ("\\\\text\\(\\(bf\\)\\|it\\|sl\\){\\([^}]+\\)}"
435; 3 (if (match-beginning 2) 'bold 'italic) keep)
436; ;; Old-style bf/em/it/sl. Stop at `\\' and un-escaped `&', for tables.
437; ("\\\\\\(\\(bf\\)\\|em\\|it\\|sl\\)\\>\\(\\([^}&\\]\\|\\\\[^\\]\\)+\\)"
438; 3 (if (match-beginning 2) 'bold 'italic) keep))
439
440;; Rewritten with the help of Alexandra Bac <abac@welcome.disi.unige.it>.
441(defconst tex-font-lock-keywords-1
442 (eval-when-compile
443 (let* (;; Names of commands whose arg should be fontified as heading, etc.
444 (headings (regexp-opt
445 '("title" "begin" "end" "chapter" "part"
446 "section" "subsection" "subsubsection"
447 "paragraph" "subparagraph" "subsubparagraph"
7afecb99
SM
448 "newcommand" "renewcommand" "providecommand"
449 "newenvironment" "renewenvironment"
450 "newtheorem" "renewtheorem")
53c4fe47
SM
451 t))
452 (variables (regexp-opt
453 '("newcounter" "newcounter*" "setcounter" "addtocounter"
454 "setlength" "addtolength" "settowidth")
455 t))
456 (includes (regexp-opt
457 '("input" "include" "includeonly" "bibliography"
458 "epsfig" "psfig" "epsf" "nofiles" "usepackage"
15ca8de7 459 "documentstyle" "documentclass" "verbatiminput"
dd166d5f
SM
460 "includegraphics" "includegraphics*"
461 "url" "nolinkurl")
53c4fe47
SM
462 t))
463 ;; Miscellany.
464 (slash "\\\\")
15ca8de7
SM
465 (opt " *\\(\\[[^]]*\\] *\\)*")
466 ;; This would allow highlighting \newcommand\CMD but requires
467 ;; adapting subgroup numbers below.
468 ;; (arg "\\(?:{\\(\\(?:[^{}\\]+\\|\\\\.\\|{[^}]*}\\)+\\)\\|\\\\[a-z*]+\\)"))
1598a961 469 (arg "{\\(\\(?:[^{}\\]+\\|\\\\.\\|{[^}]*}\\)+\\)"))
53c4fe47 470 (list
b119fdd5
SM
471 ;; font-lock-syntactic-keywords causes the \ of \end{verbatim} to be
472 ;; highlighted as tex-verbatim-face. Let's undo that.
473 ;; This is ugly and brittle :-( --Stef
474 '("^\\(\\\\\\)end" (1 (get-text-property (match-end 1) 'face) t))
95a045bd
SM
475 ;; display $$ math $$
476 ;; We only mark the match between $$ and $$ because the $$ delimiters
477 ;; themselves have already been marked (along with $..$) by syntactic
478 ;; fontification. Also this is done at the very beginning so as to
479 ;; interact with the other keywords in the same way as $...$ does.
480 (list "\\$\\$\\([^$]+\\)\\$\\$" 1 'tex-math-face)
53c4fe47
SM
481 ;; Heading args.
482 (list (concat slash headings "\\*?" opt arg)
bdbd9606
SM
483 ;; If ARG ends up matching too much (if the {} don't match, f.ex)
484 ;; jit-lock will do funny things: when updating the buffer
485 ;; the re-highlighting is only done locally so it will just
486 ;; match the local line, but defer-contextually will
487 ;; match more lines at a time, so ARG will end up matching
488 ;; a lot more, which might suddenly include a comment
489 ;; so you get things highlighted bold when you type them
490 ;; but they get turned back to normal a little while later
491 ;; because "there's already a face there".
492 ;; Using `keep' works around this un-intuitive behavior as well
15ca8de7
SM
493 ;; as improves the behavior in the very rare case where you do
494 ;; have a comment in ARG.
bdbd9606 495 3 'font-lock-function-name-face 'keep)
7afecb99
SM
496 (list (concat slash "\\(?:provide\\|\\(?:re\\)?new\\)command\\** *\\(\\\\[A-Za-z@]+\\)")
497 1 'font-lock-function-name-face 'keep)
53c4fe47 498 ;; Variable args.
15ca8de7 499 (list (concat slash variables " *" arg) 2 'font-lock-variable-name-face)
53c4fe47
SM
500 ;; Include args.
501 (list (concat slash includes opt arg) 3 'font-lock-builtin-face)
502 ;; Definitions. I think.
15ca8de7 503 '("^[ \t]*\\\\def *\\\\\\(\\(\\w\\|@\\)+\\)"
bdbd9606 504 1 font-lock-function-name-face))))
53c4fe47
SM
505 "Subdued expressions to highlight in TeX modes.")
506
1f4cc034
SM
507(defun tex-font-lock-append-prop (prop)
508 (unless (memq (get-text-property (match-end 1) 'face)
509 '(font-lock-comment-face tex-verbatim-face))
510 prop))
511
53c4fe47
SM
512(defconst tex-font-lock-keywords-2
513 (append tex-font-lock-keywords-1
514 (eval-when-compile
515 (let* (;;
516 ;; Names of commands whose arg should be fontified with fonts.
1598a961 517 (bold (regexp-opt '("textbf" "textsc" "textup"
53c4fe47 518 "boldsymbol" "pmb") t))
1598a961 519 (italic (regexp-opt '("textit" "textsl" "emph") t))
a3d80d4a
SM
520 ;; FIXME: unimplemented yet.
521 ;; (type (regexp-opt '("texttt" "textmd" "textrm" "textsf") t))
53c4fe47
SM
522 ;;
523 ;; Names of commands whose arg should be fontified as a citation.
524 (citations (regexp-opt
525 '("label" "ref" "pageref" "vref" "eqref"
15ca8de7 526 "cite" "nocite" "index" "glossary" "bibitem"
1598a961
SM
527 ;; These are text, rather than citations.
528 ;; "caption" "footnote" "footnotemark" "footnotetext"
529 )
53c4fe47
SM
530 t))
531 ;;
532 ;; Names of commands that should be fontified.
1b741d9e
AS
533 (specials-1 (regexp-opt '("\\" "\\*") t)) ;; "-"
534 (specials-2 (regexp-opt
535 '("linebreak" "nolinebreak" "pagebreak" "nopagebreak"
536 "newline" "newpage" "clearpage" "cleardoublepage"
537 "displaybreak" "allowdisplaybreaks"
538 "enlargethispage") t))
53c4fe47
SM
539 (general "\\([a-zA-Z@]+\\**\\|[^ \t\n]\\)")
540 ;;
541 ;; Miscellany.
542 (slash "\\\\")
15ca8de7 543 (opt " *\\(\\[[^]]*\\] *\\)*")
7afecb99 544 (args "\\(\\(?:[^{}&\\]+\\|\\\\.\\|{[^}]*}\\)+\\)")
1598a961 545 (arg "{\\(\\(?:[^{}\\]+\\|\\\\.\\|{[^}]*}\\)+\\)"))
53c4fe47
SM
546 (list
547 ;;
548 ;; Citation args.
549 (list (concat slash citations opt arg) 3 'font-lock-constant-face)
550 ;;
15ca8de7 551 ;; Text between `` quotes ''.
95a045bd 552 (cons (concat (regexp-opt `("``" "\"<" "\"`" "<<" "«") t)
7abc9add 553 "[^'\">{]+" ;a bit pessimistic
95a045bd 554 (regexp-opt `("''" "\">" "\"'" ">>" "»") t))
15ca8de7
SM
555 'font-lock-string-face)
556 ;;
53c4fe47 557 ;; Command names, special and general.
1b741d9e
AS
558 (cons (concat slash specials-1) 'font-lock-warning-face)
559 (list (concat "\\(" slash specials-2 "\\)\\([^a-zA-Z@]\\|\\'\\)")
560 1 'font-lock-warning-face)
53c4fe47
SM
561 (concat slash general)
562 ;;
563 ;; Font environments. It seems a bit dubious to use `bold' etc. faces
564 ;; since we might not be able to display those fonts.
1f4cc034
SM
565 (list (concat slash bold " *" arg) 2
566 '(tex-font-lock-append-prop 'bold) 'append)
567 (list (concat slash italic " *" arg) 2
568 '(tex-font-lock-append-prop 'italic) 'append)
1598a961 569 ;; (list (concat slash type arg) 2 '(quote bold-italic) 'append)
53c4fe47
SM
570 ;;
571 ;; Old-style bf/em/it/sl. Stop at `\\' and un-escaped `&', for tables.
7afecb99 572 (list (concat "\\\\\\(em\\|it\\|sl\\)\\>" args)
1f4cc034 573 2 '(tex-font-lock-append-prop 'italic) 'append)
7afecb99
SM
574 ;; This is separate from the previous one because of cases like
575 ;; {\em foo {\bf bar} bla} where both match.
1f4cc034
SM
576 (list (concat "\\\\\\(bf\\)\\>" args)
577 2 '(tex-font-lock-append-prop 'bold) 'append)))))
53c4fe47
SM
578 "Gaudy expressions to highlight in TeX modes.")
579
7afecb99
SM
580(defun tex-font-lock-suscript (pos)
581 (unless (or (memq (get-text-property pos 'face)
582 '(font-lock-constant-face font-lock-builtin-face
583 font-lock-comment-face tex-verbatim-face))
584 ;; Check for backslash quoting
585 (let ((odd nil)
586 (pos pos))
587 (while (eq (char-before pos) ?\\)
588 (setq pos (1- pos) odd (not odd)))
589 odd))
590 (if (eq (char-after pos) ?_)
591 '(face subscript display (raise -0.3))
592 '(face superscript display (raise +0.3)))))
593
594(defconst tex-font-lock-keywords-3
595 (append tex-font-lock-keywords-2
596 (eval-when-compile
597 (let ((general "\\([a-zA-Z@]+\\|[^ \t\n]\\)")
598 (slash "\\\\")
599 ;; This is not the same regexp as before: it has a `+' removed.
600 ;; The + makes the matching faster in the above cases (where we can
601 ;; exit as soon as the match fails) but would make this matching
602 ;; degenerate to nasty complexity (because we try to match the
603 ;; closing brace, which forces trying all matching combinations).
604 (arg "{\\(?:[^{}\\]\\|\\\\.\\|{[^}]*}\\)*"))
6ecedb40 605 `((,(concat "[_^] *\\([^\n\\{}#]\\|" slash general "\\|#[0-9]\\|" arg "}\\)")
7afecb99
SM
606 (1 (tex-font-lock-suscript (match-beginning 0))
607 append))))))
608 "Experimental expressions to highlight in TeX modes.")
609
53c4fe47
SM
610(defvar tex-font-lock-keywords tex-font-lock-keywords-1
611 "Default expressions to highlight in TeX modes.")
0fb4f245 612
7afecb99
SM
613(defvar tex-verbatim-environments
614 '("verbatim" "verbatim*"))
615
616(defvar tex-font-lock-syntactic-keywords
617 (let ((verbs (regexp-opt tex-verbatim-environments t)))
618 `((,(concat "^\\\\begin *{" verbs "}.*\\(\n\\)") 2 "|")
1f4cc034
SM
619 ;; Technically, we'd like to put the "|" property on the \n preceding
620 ;; the \end, but this would have 2 disadvantages:
621 ;; 1 - it's wrong if the verbatim env is empty (the same \n is used to
622 ;; start and end the fenced-string).
623 ;; 2 - font-lock considers the preceding \n as being part of the
624 ;; preceding line, so things gets screwed every time the previous
625 ;; line is re-font-locked on its own.
b119fdd5
SM
626 ;; There's a hack in tex-font-lock-keywords-1 to remove the verbatim
627 ;; face from the \ but C-M-f still jumps to the wrong spot :-( --Stef
1f4cc034 628 (,(concat "^\\(\\\\\\)end *{" verbs "}\\(.?\\)") (1 "|") (3 "<"))
7afecb99
SM
629 ;; ("^\\(\\\\\\)begin *{comment}" 1 "< b")
630 ;; ("^\\\\end *{comment}.*\\(\n\\)" 1 "> b")
631 ("\\\\verb\\**\\([^a-z@*]\\)" 1 "\""))))
632
633(defun tex-font-lock-unfontify-region (beg end)
634 (font-lock-default-unfontify-region beg end)
635 (while (< beg end)
636 (let ((next (next-single-property-change beg 'display nil end))
637 (prop (get-text-property beg 'display)))
638 (if (and (eq (car-safe prop) 'raise)
639 (member (car-safe (cdr prop)) '(-0.3 +0.3))
640 (null (cddr prop)))
641 (put-text-property beg next 'display nil))
642 (setq beg next))))
643
644(defface superscript
645 '((t :height 0.8)) ;; :raise 0.3
646 "Face used for superscripts.")
647(defface subscript
648 '((t :height 0.8)) ;; :raise -0.3
649 "Face used for subscripts.")
bdbd9606
SM
650
651(defface tex-math-face
652 '((t :inherit font-lock-string-face))
653 "Face used to highlight TeX math expressions.")
654(defvar tex-math-face 'tex-math-face)
7afecb99
SM
655(defface tex-verbatim-face
656 ;; '((t :inherit font-lock-string-face))
657 '((t :family "courier"))
658 "Face used to highlight TeX verbatim environments.")
659(defvar tex-verbatim-face 'tex-verbatim-face)
bdbd9606
SM
660
661;; Use string syntax but math face for $...$.
662(defun tex-font-lock-syntactic-face-function (state)
7afecb99
SM
663 (let ((char (nth 3 state)))
664 (cond
665 ((not char) font-lock-comment-face)
666 ((eq char ?$) tex-math-face)
667 (t
668 (when (char-valid-p char)
669 ;; This is a \verb?...? construct. Let's find the end and mark it.
670 (save-excursion
671 (skip-chars-forward (string ?^ char)) ;; Use `end' ?
672 (when (eq (char-syntax (preceding-char)) ?/)
673 (put-text-property (1- (point)) (point) 'syntax-table '(1)))
674 (unless (eobp)
675 (put-text-property (point) (1+ (point)) 'syntax-table '(7)))))
676 tex-verbatim-face))))
bdbd9606 677
53c4fe47 678\f
869bff31 679(defun tex-define-common-keys (keymap)
81c735c0 680 "Define the keys that we want defined both in TeX mode and in the TeX shell."
869bff31 681 (define-key keymap "\C-c\C-k" 'tex-kill-job)
682 (define-key keymap "\C-c\C-l" 'tex-recenter-output-buffer)
683 (define-key keymap "\C-c\C-q" 'tex-show-print-queue)
684 (define-key keymap "\C-c\C-p" 'tex-print)
685 (define-key keymap "\C-c\C-v" 'tex-view)
62a24cb5
RS
686
687 (define-key keymap [menu-bar tex] (cons "TeX" (make-sparse-keymap "TeX")))
688
bdbd9606
SM
689 (define-key keymap [menu-bar tex tex-kill-job]
690 '(menu-item "Tex Kill" tex-kill-job :enable (tex-shell-running)))
cf6d6e8a 691 (define-key keymap [menu-bar tex tex-recenter-output-buffer]
bdbd9606
SM
692 '(menu-item "Tex Recenter" tex-recenter-output-buffer
693 :enable (get-buffer "*tex-shell*")))
62a24cb5
RS
694 (define-key keymap [menu-bar tex tex-show-print-queue]
695 '("Show Print Queue" . tex-show-print-queue))
cf6d6e8a 696 (define-key keymap [menu-bar tex tex-alt-print]
bdbd9606
SM
697 '(menu-item "Tex Print (alt printer)" tex-alt-print
698 :enable (stringp tex-print-file)))
699 (define-key keymap [menu-bar tex tex-print]
700 '(menu-item "Tex Print" tex-print :enable (stringp tex-print-file)))
701 (define-key keymap [menu-bar tex tex-view]
702 '(menu-item "Tex View" tex-view :enable (stringp tex-print-file))))
703
704(defvar tex-mode-map
705 (let ((map (make-sparse-keymap)))
15ca8de7 706 (set-keymap-parent map text-mode-map)
bdbd9606
SM
707 (tex-define-common-keys map)
708 (define-key map "\"" 'tex-insert-quote)
709 (define-key map "(" 'skeleton-pair-insert-maybe)
710 (define-key map "{" 'skeleton-pair-insert-maybe)
711 (define-key map "[" 'skeleton-pair-insert-maybe)
712 (define-key map "$" 'skeleton-pair-insert-maybe)
713 (define-key map "\n" 'tex-terminate-paragraph)
714 (define-key map "\M-\r" 'latex-insert-item)
715 (define-key map "\C-c}" 'up-list)
716 (define-key map "\C-c{" 'tex-insert-braces)
717 (define-key map "\C-c\C-r" 'tex-region)
718 (define-key map "\C-c\C-b" 'tex-buffer)
719 (define-key map "\C-c\C-f" 'tex-file)
75035a80 720 (define-key map "\C-c\C-c" 'tex-compile)
bdbd9606 721 (define-key map "\C-c\C-i" 'tex-bibtex-file)
7afecb99
SM
722 (define-key map "\C-c\C-o" 'latex-insert-block)
723 (define-key map "\C-c\C-e" 'latex-close-block)
bdbd9606
SM
724 (define-key map "\C-c\C-u" 'tex-goto-last-unclosed-latex-block)
725 (define-key map "\C-c\C-m" 'tex-feed-input)
726 (define-key map [(control return)] 'tex-feed-input)
727 (define-key map [menu-bar tex tex-bibtex-file]
728 '("BibTeX File" . tex-bibtex-file))
729 (define-key map [menu-bar tex tex-validate-region]
730 '(menu-item "Validate Region" tex-validate-region :enable mark-active))
731 (define-key map [menu-bar tex tex-validate-buffer]
732 '("Validate Buffer" . tex-validate-buffer))
733 (define-key map [menu-bar tex tex-region]
734 '(menu-item "TeX Region" tex-region :enable mark-active))
735 (define-key map [menu-bar tex tex-buffer]
736 '("TeX Buffer" . tex-buffer))
737 (define-key map [menu-bar tex tex-file] '("TeX File" . tex-file))
738 map)
15ca8de7
SM
739 "Keymap shared by TeX modes.")
740
741(defvar latex-mode-map
742 (let ((map (make-sparse-keymap)))
743 (set-keymap-parent map tex-mode-map)
a3d80d4a 744 (define-key map "\C-c\C-s" 'latex-split-block)
15ca8de7
SM
745 map)
746 "Keymap for `latex-mode'. See also `tex-mode-map'.")
747
748(defvar plain-tex-mode-map
749 (let ((map (make-sparse-keymap)))
750 (set-keymap-parent map tex-mode-map)
751 map)
752 "Keymap for `plain-tex-mode'. See also `tex-mode-map'.")
cf6d6e8a 753
d6709b80
SM
754(defvar tex-shell-map
755 (let ((m (make-sparse-keymap)))
756 (set-keymap-parent m shell-mode-map)
757 (tex-define-common-keys m)
758 m)
81c735c0 759 "Keymap for the TeX shell.
dca5ea48 760Inherits `shell-mode-map' with a few additions.")
869bff31 761
6da9bbd6
RS
762(defvar tex-face-alist
763 '((bold . "{\\bf ")
764 (italic . "{\\it ")
765 (bold-italic . "{\\bi ") ; hypothetical
766 (underline . "\\underline{")
767 (default . "{\\rm "))
768 "Alist of face and TeX font name for facemenu.")
769
770(defvar tex-latex-face-alist
771 `((italic . "{\\em ")
772 ,@tex-face-alist)
773 "Alist of face and LaTeX font name for facemenu.")
774
15ca8de7
SM
775;; This would be a lot simpler if we just used a regexp search,
776;; but then it would be too slow.
a3d80d4a 777(defun tex-guess-mode ()
d6709b80 778 (let ((mode tex-default-mode) slash comment)
869bff31 779 (save-excursion
780 (goto-char (point-min))
781 (while (and (setq slash (search-forward "\\" nil t))
782 (setq comment (let ((search-end (point)))
783 (save-excursion
784 (beginning-of-line)
785 (search-forward "%" search-end t))))))
bdbd9606
SM
786 (when (and slash (not comment))
787 (setq mode
788 (if (looking-at
789 (eval-when-compile
790 (concat
791 (regexp-opt '("documentstyle" "documentclass"
95a045bd 792 "begin" "subsection" "section"
bca7a08b
SM
793 "part" "chapter" "newcommand"
794 "renewcommand") 'words)
bdbd9606 795 "\\|NeedsTeXFormat{LaTeX")))
dd166d5f
SM
796 (if (and (looking-at
797 "document\\(style\\|class\\)\\(\\[.*\\]\\)?{slides}")
798 ;; SliTeX is almost never used any more nowadays.
799 (tex-executable-exists-p slitex-run-command))
bdbd9606
SM
800 'slitex-mode
801 'latex-mode)
802 'plain-tex-mode))))
d6709b80 803 (funcall mode)))
e4c8c838 804
a3d80d4a
SM
805;; `tex-mode' plays two roles: it's the parent of several sub-modes
806;; but it's also the function that chooses between those submodes.
807;; To tell the difference between those two cases where the function
808;; might be called, we check `delay-mode-hooks'.
a3d80d4a
SM
809(define-derived-mode tex-mode text-mode "generic-TeX"
810 (tex-common-initialization))
bca7a08b
SM
811;; We now move the function and define it again. This gives a warning
812;; in the byte-compiler :-( but it's difficult to avoid because
813;; `define-derived-mode' will necessarily define the function once
814;; and we need to define it a second time for `autoload' to get the
815;; proper docstring.
816(defalias 'tex-mode-internal (symbol-function 'tex-mode))
817;;;###autoload
818(defun tex-mode ()
819 "Major mode for editing files of input for TeX, LaTeX, or SliTeX.
a3d80d4a
SM
820Tries to determine (by looking at the beginning of the file) whether
821this file is for plain TeX, LaTeX, or SliTeX and calls `plain-tex-mode',
822`latex-mode', or `slitex-mode', respectively. If it cannot be determined,
823such as if there are no commands in the file, the value of `tex-default-mode'
824says which mode to use."
bca7a08b
SM
825 (interactive)
826 (if delay-mode-hooks
827 ;; We're called from one of the children already.
828 (tex-mode-internal)
829 (tex-guess-mode)))
a3d80d4a 830
6503cec3 831;;;###autoload
31e1d920 832(defalias 'TeX-mode 'tex-mode)
6503cec3 833;;;###autoload
c3ce7bf4
RS
834(defalias 'plain-TeX-mode 'plain-tex-mode)
835;;;###autoload
31e1d920 836(defalias 'LaTeX-mode 'latex-mode)
869bff31 837
7229064d 838;;;###autoload
a3d80d4a 839(define-derived-mode plain-tex-mode tex-mode "TeX"
869bff31 840 "Major mode for editing files of input for plain TeX.
841Makes $ and } display the characters they match.
842Makes \" insert `` when it seems to be the beginning of a quotation,
843and '' when it appears to be the end; it inserts \" only after a \\.
844
845Use \\[tex-region] to run TeX on the current region, plus a \"header\"
846copied from the top of the file (containing macro definitions, etc.),
847running TeX under a special subshell. \\[tex-buffer] does the whole buffer.
848\\[tex-file] saves the buffer and then processes the file.
849\\[tex-print] prints the .dvi file made by any of these.
850\\[tex-view] previews the .dvi file made by any of these.
851\\[tex-bibtex-file] runs bibtex on the file of the current buffer.
852
e6b0b773 853Use \\[tex-validate-buffer] to check buffer for paragraphs containing
869bff31 854mismatched $'s or braces.
855
856Special commands:
15ca8de7 857\\{plain-tex-mode-map}
869bff31 858
859Mode variables:
860tex-run-command
861 Command string used by \\[tex-region] or \\[tex-buffer].
862tex-directory
863 Directory in which to create temporary files for TeX jobs
864 run by \\[tex-region] or \\[tex-buffer].
865tex-dvi-print-command
866 Command string used by \\[tex-print] to print a .dvi file.
528415e7
RS
867tex-alt-dvi-print-command
868 Alternative command string used by \\[tex-print] (when given a prefix
869 argument) to print a .dvi file.
869bff31 870tex-dvi-view-command
871 Command string used by \\[tex-view] to preview a .dvi file.
872tex-show-queue-command
873 Command string used by \\[tex-show-print-queue] to show the print
874 queue that \\[tex-print] put your job on.
875
81c735c0
RS
876Entering Plain-tex mode runs the hook `text-mode-hook', then the hook
877`tex-mode-hook', and finally the hook `plain-tex-mode-hook'. When the
878special subshell is initiated, the hook `tex-shell-hook' is run."
a3d80d4a
SM
879 (set (make-local-variable 'tex-command) tex-run-command)
880 (set (make-local-variable 'tex-start-of-header) "%\\*\\*start of header")
881 (set (make-local-variable 'tex-end-of-header) "%\\*\\*end of header")
882 (set (make-local-variable 'tex-trailer) "\\bye\n"))
869bff31 883
7229064d 884;;;###autoload
a3d80d4a 885(define-derived-mode latex-mode tex-mode "LaTeX"
869bff31 886 "Major mode for editing files of input for LaTeX.
887Makes $ and } display the characters they match.
888Makes \" insert `` when it seems to be the beginning of a quotation,
889and '' when it appears to be the end; it inserts \" only after a \\.
890
891Use \\[tex-region] to run LaTeX on the current region, plus the preamble
892copied from the top of the file (containing \\documentstyle, etc.),
893running LaTeX under a special subshell. \\[tex-buffer] does the whole buffer.
894\\[tex-file] saves the buffer and then processes the file.
895\\[tex-print] prints the .dvi file made by any of these.
896\\[tex-view] previews the .dvi file made by any of these.
897\\[tex-bibtex-file] runs bibtex on the file of the current buffer.
898
e6b0b773 899Use \\[tex-validate-buffer] to check buffer for paragraphs containing
869bff31 900mismatched $'s or braces.
901
902Special commands:
15ca8de7 903\\{latex-mode-map}
869bff31 904
905Mode variables:
906latex-run-command
907 Command string used by \\[tex-region] or \\[tex-buffer].
908tex-directory
909 Directory in which to create temporary files for LaTeX jobs
910 run by \\[tex-region] or \\[tex-buffer].
911tex-dvi-print-command
912 Command string used by \\[tex-print] to print a .dvi file.
528415e7
RS
913tex-alt-dvi-print-command
914 Alternative command string used by \\[tex-print] (when given a prefix
915 argument) to print a .dvi file.
869bff31 916tex-dvi-view-command
917 Command string used by \\[tex-view] to preview a .dvi file.
918tex-show-queue-command
919 Command string used by \\[tex-show-print-queue] to show the print
920 queue that \\[tex-print] put your job on.
921
bed1b0c8 922Entering Latex mode runs the hook `text-mode-hook', then
81c735c0
RS
923`tex-mode-hook', and finally `latex-mode-hook'. When the special
924subshell is initiated, `tex-shell-hook' is run."
a3d80d4a
SM
925 (set (make-local-variable 'tex-command) latex-run-command)
926 (set (make-local-variable 'tex-start-of-header)
927 "\\\\document\\(style\\|class\\)")
928 (set (make-local-variable 'tex-end-of-header) "\\\\begin\\s-*{document}")
d9cbee57 929 (set (make-local-variable 'tex-trailer) "\\end{document}\n")
ab2c9f54
RS
930 ;; A line containing just $$ is treated as a paragraph separator.
931 ;; A line starting with $$ starts a paragraph,
932 ;; but does not separate paragraphs if it has more stuff on it.
d6709b80 933 (setq paragraph-start
a3d80d4a 934 (concat "[ \t]*\\(\\$\\$\\|"
d6709b80
SM
935 "\\\\[][]\\|"
936 "\\\\" (regexp-opt (append
937 (mapcar 'car latex-section-alist)
938 '("begin" "label" "end"
939 "item" "bibitem" "newline" "noindent"
940 "newpage" "footnote" "marginpar"
941 "parbox" "caption")) t)
942 "\\>\\|\\\\[a-z]*" (regexp-opt '("space" "skip" "page") t)
dd459839 943 "\\>\\)"))
d6709b80 944 (setq paragraph-separate
dd459839 945 (concat "[\f%]\\|[ \t]*\\($\\|"
d6709b80
SM
946 "\\\\[][]\\|"
947 "\\\\" (regexp-opt (append
948 (mapcar 'car latex-section-alist)
949 '("begin" "label" "end" )) t)
950 "\\>\\|\\\\\\(" (regexp-opt '("item" "bibitem" "newline"
951 "noindent" "newpage" "footnote"
952 "marginpar" "parbox" "caption"))
dd459839
SM
953 "\\|\\$\\$\\|[a-z]*\\(space\\|skip\\|page[a-z]*\\)"
954 "\\>\\)[ \t]*\\($\\|%\\)\\)"))
d6709b80
SM
955 (set (make-local-variable 'imenu-create-index-function)
956 'latex-imenu-create-index)
957 (set (make-local-variable 'tex-face-alist) tex-latex-face-alist)
95a045bd 958 (add-hook 'fill-nobreak-predicate 'latex-fill-nobreak-predicate nil t)
ea590e1c 959 (set (make-local-variable 'indent-line-function) 'latex-indent)
dd459839 960 (set (make-local-variable 'fill-indent-according-to-mode) t)
d6709b80
SM
961 (set (make-local-variable 'outline-regexp) latex-outline-regexp)
962 (set (make-local-variable 'outline-level) 'latex-outline-level)
53c4fe47 963 (set (make-local-variable 'forward-sexp-function) 'latex-forward-sexp)
a3d80d4a 964 (set (make-local-variable 'skeleton-end-hook) nil))
869bff31 965
bd2f2323 966;;;###autoload
d6709b80 967(define-derived-mode slitex-mode latex-mode "SliTeX"
869bff31 968 "Major mode for editing files of input for SliTeX.
969Makes $ and } display the characters they match.
970Makes \" insert `` when it seems to be the beginning of a quotation,
971and '' when it appears to be the end; it inserts \" only after a \\.
972
973Use \\[tex-region] to run SliTeX on the current region, plus the preamble
974copied from the top of the file (containing \\documentstyle, etc.),
975running SliTeX under a special subshell. \\[tex-buffer] does the whole buffer.
976\\[tex-file] saves the buffer and then processes the file.
977\\[tex-print] prints the .dvi file made by any of these.
978\\[tex-view] previews the .dvi file made by any of these.
979\\[tex-bibtex-file] runs bibtex on the file of the current buffer.
980
e6b0b773 981Use \\[tex-validate-buffer] to check buffer for paragraphs containing
869bff31 982mismatched $'s or braces.
983
984Special commands:
15ca8de7 985\\{slitex-mode-map}
869bff31 986
987Mode variables:
988slitex-run-command
989 Command string used by \\[tex-region] or \\[tex-buffer].
990tex-directory
991 Directory in which to create temporary files for SliTeX jobs
992 run by \\[tex-region] or \\[tex-buffer].
993tex-dvi-print-command
994 Command string used by \\[tex-print] to print a .dvi file.
528415e7
RS
995tex-alt-dvi-print-command
996 Alternative command string used by \\[tex-print] (when given a prefix
997 argument) to print a .dvi file.
869bff31 998tex-dvi-view-command
999 Command string used by \\[tex-view] to preview a .dvi file.
1000tex-show-queue-command
1001 Command string used by \\[tex-show-print-queue] to show the print
1002 queue that \\[tex-print] put your job on.
1003
81c735c0
RS
1004Entering SliTeX mode runs the hook `text-mode-hook', then the hook
1005`tex-mode-hook', then the hook `latex-mode-hook', and finally the hook
1006`slitex-mode-hook'. When the special subshell is initiated, the hook
1007`tex-shell-hook' is run."
869bff31 1008 (setq tex-command slitex-run-command)
d6709b80 1009 (setq tex-start-of-header "\\\\documentstyle{slides}\\|\\\\documentclass{slides}"))
869bff31 1010
1011(defun tex-common-initialization ()
822eddf4 1012 ;; Regexp isearch should accept newline and formfeed as whitespace.
d6709b80 1013 (set (make-local-variable 'search-whitespace-regexp) "[ \t\r\n\f]+")
64db2461 1014 ;; A line containing just $$ is treated as a paragraph separator.
d6709b80
SM
1015 (set (make-local-variable 'paragraph-start)
1016 "[ \t]*$\\|[\f\\\\%]\\|[ \t]*\\$\\$")
ab2c9f54
RS
1017 ;; A line starting with $$ starts a paragraph,
1018 ;; but does not separate paragraphs if it has more stuff on it.
d6709b80
SM
1019 (set (make-local-variable 'paragraph-separate)
1020 "[ \t]*$\\|[\f\\\\%]\\|[ \t]*\\$\\$[ \t]*$")
1021 (set (make-local-variable 'comment-start) "%")
1022 (set (make-local-variable 'comment-add) 1)
1023 (set (make-local-variable 'comment-start-skip)
5ae0d697 1024 "\\(\\(^\\|[^\\\n]\\)\\(\\\\\\\\\\)*\\)\\(%+ *\\)")
d6709b80
SM
1025 (set (make-local-variable 'parse-sexp-ignore-comments) t)
1026 (set (make-local-variable 'compare-windows-whitespace)
1027 'tex-categorize-whitespace)
1028 (set (make-local-variable 'facemenu-add-face-function)
1029 (lambda (face end)
6ecedb40
SM
1030 (or (cdr (assq face tex-face-alist))
1031 (error "Face %s not configured for %s mode" face mode-name))))
d6709b80
SM
1032 (set (make-local-variable 'facemenu-end-add-face) "}")
1033 (set (make-local-variable 'facemenu-remove-face-function) t)
1034 (set (make-local-variable 'font-lock-defaults)
7afecb99
SM
1035 '((tex-font-lock-keywords tex-font-lock-keywords-1
1036 tex-font-lock-keywords-2 tex-font-lock-keywords-3)
d6709b80
SM
1037 nil nil ((?$ . "\"")) nil
1038 ;; Who ever uses that anyway ???
bdbd9606
SM
1039 (font-lock-mark-block-function . mark-paragraph)
1040 (font-lock-syntactic-face-function
7afecb99
SM
1041 . tex-font-lock-syntactic-face-function)
1042 (font-lock-unfontify-region-function
1043 . tex-font-lock-unfontify-region)
1044 (font-lock-syntactic-keywords
1045 . tex-font-lock-syntactic-keywords)
1046 (parse-sexp-lookup-properties . t)))
95a045bd
SM
1047 ;; TABs in verbatim environments don't do what you think.
1048 (set (make-local-variable 'indent-tabs-mode) nil)
7afecb99 1049 ;; Other vars that should be buffer-local.
869bff31 1050 (make-local-variable 'tex-command)
1051 (make-local-variable 'tex-start-of-header)
1052 (make-local-variable 'tex-end-of-header)
1053 (make-local-variable 'tex-trailer))
1054
869bff31 1055(defun tex-categorize-whitespace (backward-limit)
1056 ;; compare-windows-whitespace is set to this.
1057 ;; This is basically a finite-state machine.
1058 ;; Returns a symbol telling how TeX would treat
1059 ;; the whitespace we are looking at: null, space, or par.
1060 (let ((category 'null)
1061 (not-finished t))
1062 (skip-chars-backward " \t\n\f" backward-limit)
1063 (while not-finished
1064 (cond ((looking-at "[ \t]+")
1065 (goto-char (match-end 0))
2be5fefb 1066 (if (eq category 'null)
869bff31 1067 (setq category 'space)))
1068 ((looking-at "\n")
2be5fefb 1069 (cond ((eq category 'newline)
869bff31 1070 (setq category 'par)
1071 (setq not-finished nil))
1072 (t
1073 (setq category 'newline) ;a strictly internal state
1074 (goto-char (match-end 0)))))
1075 ((looking-at "\f+")
1076 (setq category 'par)
1077 (setq not-finished nil))
1078 (t
1079 (setq not-finished nil))))
1080 (skip-chars-forward " \t\n\f")
2be5fefb 1081 (if (eq category 'newline)
869bff31 1082 'space ;TeX doesn't distinguish
1083 category)))
1084
1085(defun tex-insert-quote (arg)
1086 "Insert the appropriate quote marks for TeX.
08348502
RS
1087Inserts the value of `tex-open-quote' (normally ``) or `tex-close-quote'
1088\(normally '') depending on the context. With prefix argument, always
869bff31 1089inserts \" characters."
528415e7 1090 (interactive "*P")
95a045bd
SM
1091 (if (or arg (memq (char-syntax (preceding-char)) '(?/ ?\\))
1092 (eq (get-text-property (point) 'face) 'tex-verbatim-face)
1093 (save-excursion
1094 (backward-char (length tex-open-quote))
1095 (when (or (looking-at (regexp-quote tex-open-quote))
1096 (looking-at (regexp-quote tex-close-quote)))
1097 (delete-char (length tex-open-quote))
1098 t)))
869bff31 1099 (self-insert-command (prefix-numeric-value arg))
95a045bd
SM
1100 (insert (if (memq (char-syntax (preceding-char)) '(?\( ?> ?\ ))
1101 tex-open-quote tex-close-quote))))
869bff31 1102
e6b0b773
MR
1103(defun tex-validate-buffer ()
1104 "Check current buffer for paragraphs containing mismatched braces or $s.
bbd93e41 1105Their positions are recorded in the buffer `*Occur*'.
e6b0b773
MR
1106To find a particular invalidity from `*Occur*', switch to that buffer
1107and type C-c C-c or click with mouse-2
1108on the line for the invalidity you want to see."
869bff31 1109 (interactive)
bbd93e41
RS
1110 (let ((buffer (current-buffer))
1111 (prevpos (point-min))
e6b0b773
MR
1112 (linenum nil)
1113 (num-matches 0))
bbd93e41
RS
1114 (with-output-to-temp-buffer "*Occur*"
1115 (princ "Mismatches:\n")
a3d80d4a 1116 (with-current-buffer standard-output
bbd93e41 1117 (occur-mode)
52ab062c
CW
1118 ;; This won't actually work...Really, this whole thing should
1119 ;; be rewritten instead of being a hack on top of occur.
1120 (setq occur-revert-arguments (list nil 0 (list buffer))))
bbd93e41
RS
1121 (save-excursion
1122 (goto-char (point-max))
52ab062c 1123 (while (and (not (bobp)))
710a0f53
KH
1124 (let ((end (point))
1125 prev-end)
bbd93e41 1126 ;; Scan the previous paragraph for invalidities.
710a0f53
KH
1127 (if (search-backward "\n\n" nil t)
1128 (progn
1129 (setq prev-end (point))
1130 (forward-char 2))
1131 (goto-char (setq prev-end (point-min))))
869bff31 1132 (or (tex-validate-region (point) end)
a3d80d4a 1133 (let* ((end (line-beginning-position 2))
bbd93e41
RS
1134 start tem)
1135 (beginning-of-line)
1136 (setq start (point))
1137 ;; Keep track of line number as we scan,
1138 ;; in a cumulative fashion.
1139 (if linenum
1140 (setq linenum (- linenum (count-lines prevpos (point))))
1141 (setq linenum (1+ (count-lines 1 start))))
1142 (setq prevpos (point))
bed1b0c8 1143 ;; Mention this mismatch in *Occur*.
bbd93e41 1144 ;; Since we scan from end of buffer to beginning,
e6b0b773 1145 ;; add each mismatch at the beginning of *Occur*.
bbd93e41
RS
1146 (save-excursion
1147 (setq tem (point-marker))
1148 (set-buffer standard-output)
1149 (goto-char (point-min))
1150 ;; Skip "Mismatches:" header line.
1151 (forward-line 1)
e6b0b773 1152 (setq num-matches (1+ num-matches))
bbd93e41 1153 (insert-buffer-substring buffer start end)
e6b0b773
MR
1154 (let (text-beg (text-end (point-marker)))
1155 (forward-char (- start end))
1156 (setq text-beg (point-marker))
1157 (insert (format "%3d: " linenum))
a865de85
EZ
1158 (add-text-properties
1159 text-beg (- text-end 1)
1160 '(mouse-face highlight
1161 help-echo "mouse-2: go to this invalidity"))
1598a961 1162 (put-text-property text-beg (- text-end 1)
52ab062c 1163 'occur-target tem)))))
710a0f53 1164 (goto-char prev-end))))
bdbd9606 1165 (with-current-buffer standard-output
003274a0
JPW
1166 (let ((no-matches (zerop num-matches)))
1167 (if no-matches
1168 (insert "None!\n"))
1169 (if (interactive-p)
f04232c3
JPW
1170 (message (cond (no-matches "No mismatches found")
1171 ((= num-matches 1) "1 mismatch found")
1172 (t "%d mismatches found"))
1173 num-matches)))))))
869bff31 1174
1175(defun tex-validate-region (start end)
1176 "Check for mismatched braces or $'s in region.
1177Returns t if no mismatches. Returns nil and moves point to suspect
1178area if a mismatch is found."
1179 (interactive "r")
1180 (let ((failure-point nil) (max-possible-sexps (- end start)))
1181 (save-excursion
1182 (condition-case ()
1183 (save-restriction
1184 (narrow-to-region start end)
2ed2806c 1185 ;; First check that the open and close parens balance in numbers.
869bff31 1186 (goto-char start)
e6b0b773 1187 (while (<= 0 (setq max-possible-sexps (1- max-possible-sexps)))
2ed2806c
KH
1188 (forward-sexp 1))
1189 ;; Now check that like matches like.
1190 (goto-char start)
35dccc62
SM
1191 (while (re-search-forward "\\s(" nil t)
1192 (save-excursion
1193 (let ((pos (match-beginning 0)))
1194 (goto-char pos)
2ed2806c 1195 (forward-sexp 1)
35dccc62
SM
1196 (or (eq (preceding-char) (cdr (syntax-after pos)))
1197 (eq (char-after pos) (cdr (syntax-after (1- (point)))))
1198 (error "Mismatched parentheses"))))))
869bff31 1199 (error
bed1b0c8
RS
1200 (skip-syntax-forward " .>")
1201 (setq failure-point (point)))))
bdbd9606
SM
1202 (if failure-point (goto-char failure-point))
1203 (not failure-point)))
869bff31 1204
1205(defun tex-terminate-paragraph (inhibit-validation)
1206 "Insert two newlines, breaking a paragraph for TeX.
81c735c0 1207Check for mismatched braces or $s in paragraph being terminated.
869bff31 1208A prefix arg inhibits the checking."
528415e7 1209 (interactive "*P")
869bff31 1210 (or inhibit-validation
1211 (save-excursion
1212 (tex-validate-region
bed1b0c8
RS
1213 (save-excursion
1214 (search-backward "\n\n" nil 'move)
1215 (point))
1216 (point)))
869bff31 1217 (message "Paragraph being closed appears to contain a mismatch"))
1218 (insert "\n\n"))
1219
7afecb99 1220(define-skeleton tex-insert-braces
869bff31 1221 "Make a pair of braces and be poised to type inside of them."
7afecb99
SM
1222 nil
1223 ?\{ _ ?})
869bff31 1224
9bd4f69c
RS
1225;; This function is used as the value of fill-nobreak-predicate
1226;; in LaTeX mode. Its job is to prevent line-breaking inside
1227;; of a \verb construct.
1228(defun latex-fill-nobreak-predicate ()
7afecb99
SM
1229 (save-excursion
1230 (skip-chars-backward " ")
1231 ;; Don't break after \ since `\ ' has special meaning.
1232 (or (and (not (bobp)) (memq (char-syntax (char-before)) '(?\\ ?/)))
1233 (let ((opoint (point))
1234 inside)
1235 (beginning-of-line)
1236 (while (re-search-forward "\\\\verb\\(.\\)" opoint t)
1237 (unless (re-search-forward (regexp-quote (match-string 1)) opoint t)
1238 (setq inside t)))
1239 inside))))
9bd4f69c 1240
53c4fe47
SM
1241(defvar latex-block-default "enumerate")
1242
8084f5d8 1243(defvar latex-block-args-alist
dd166d5f
SM
1244 '(("array" nil ?\{ (skeleton-read "Format: ") ?\})
1245 ("tabular" nil ?\{ (skeleton-read "Format: ") ?\})
1246 ("minipage" nil ?\{ (skeleton-read "Size: ") ?\})
1247 ("picture" nil ?\( (skeleton-read "SizeX,SizeY: ") ?\))
1248 ;; FIXME: This is right for Prosper, but not for seminar.
1249 ;; ("slide" nil ?\{ (skeleton-read "Title: ") ?\})
1250 )
8084f5d8
SM
1251 "Skeleton element to use for arguments to particular environments.
1252Every element of the list has the form (NAME . SKEL-ELEM) where NAME is
1253the name of the environment and SKEL-ELEM is an element to use in
1254a skeleton (see `skeleton-insert').")
1255
1256(defvar latex-block-body-alist
1257 '(("enumerate" nil '(latex-insert-item) > _)
1258 ("itemize" nil '(latex-insert-item) > _)
dd166d5f
SM
1259 ("table" nil "\\caption{" > (skeleton-read "Caption: ") "}" > \n
1260 '(if (and (boundp 'reftex-mode) reftex-mode) (reftex-label "table"))
1261 \n _)
1262 ("figure" nil > _ \n "\\caption{" > (skeleton-read "Caption: ") "}" > \n
1263 '(if (and (boundp 'reftex-mode) reftex-mode) (reftex-label "table"))))
8084f5d8
SM
1264 "Skeleton element to use for the body of particular environments.
1265Every element of the list has the form (NAME . SKEL-ELEM) where NAME is
1266the name of the environment and SKEL-ELEM is an element to use in
1267a skeleton (see `skeleton-insert').")
1268
15ca8de7 1269;; Like tex-insert-braces, but for LaTeX.
7afecb99
SM
1270(defalias 'tex-latex-block 'latex-insert-block)
1271(define-skeleton latex-insert-block
8a591d52 1272 "Create a matching pair of lines \\begin{NAME} and \\end{NAME} at point.
869bff31 1273Puts point on a blank line between them."
53c4fe47
SM
1274 (let ((choice (completing-read (format "LaTeX block name [%s]: "
1275 latex-block-default)
95a045bd 1276 (append latex-block-names
7afecb99 1277 latex-standard-block-names)
53c4fe47
SM
1278 nil nil nil nil latex-block-default)))
1279 (setq latex-block-default choice)
7afecb99 1280 (unless (or (member choice latex-standard-block-names)
53c4fe47
SM
1281 (member choice latex-block-names))
1282 ;; Remember new block names for later completion.
1283 (push choice latex-block-names))
1284 choice)
95a045bd 1285 \n "\\begin{" str "}"
8084f5d8 1286 (cdr (assoc str latex-block-args-alist))
dd166d5f
SM
1287 > \n (or (cdr (assoc str latex-block-body-alist)) '(nil > _))
1288 (unless (bolp) '\n)
95a045bd 1289 "\\end{" str "}" > \n)
53c4fe47 1290
ea590e1c
SM
1291(define-skeleton latex-insert-item
1292 "Insert a \item macro."
1293 nil
15ca8de7 1294 \n "\\item " >)
ea590e1c 1295
53c4fe47
SM
1296\f
1297;;;;
1298;;;; LaTeX syntax navigation
1299;;;;
869bff31 1300
5e56fb53
SM
1301(defmacro tex-search-noncomment (&rest body)
1302 "Execute BODY as long as it return non-nil and point is in a comment.
1303Return the value returned by the last execution of BODY."
1304 (declare (debug t))
1305 (let ((res-sym (make-symbol "result")))
1306 `(let (,res-sym)
1307 (while
1308 (and (setq ,res-sym (progn ,@body))
1309 (save-excursion (skip-chars-backward "^\n%") (not (bolp)))))
1310 ,res-sym)))
1311
869bff31 1312(defun tex-last-unended-begin ()
81c735c0 1313 "Leave point at the beginning of the last `\\begin{...}' that is unended."
95a045bd 1314 (condition-case nil
5e56fb53
SM
1315 (while (and (tex-search-noncomment
1316 (re-search-backward "\\\\\\(begin\\|end\\)\\s *{"))
95a045bd
SM
1317 (looking-at "\\\\end"))
1318 (tex-last-unended-begin))
1319 (search-failed (error "Couldn't find unended \\begin"))))
869bff31 1320
53c4fe47
SM
1321(defun tex-next-unmatched-end ()
1322 "Leave point at the end of the next `\\end' that is unended."
5e56fb53
SM
1323 (while (and (tex-search-noncomment
1324 (re-search-forward "\\\\\\(begin\\|end\\)\\s *{[^}]+}"))
ea590e1c
SM
1325 (save-excursion (goto-char (match-beginning 0))
1326 (looking-at "\\\\begin")))
53c4fe47
SM
1327 (tex-next-unmatched-end)))
1328
6a900cf1
ER
1329(defun tex-goto-last-unclosed-latex-block ()
1330 "Move point to the last unclosed \\begin{...}.
1331Mark is left at original location."
1332 (interactive)
1333 (let ((spot))
1334 (save-excursion
95a045bd 1335 (tex-last-unended-begin)
6a900cf1
ER
1336 (setq spot (point)))
1337 (push-mark)
1338 (goto-char spot)))
1339
53c4fe47
SM
1340(defun latex-backward-sexp-1 ()
1341 "Like (backward-sexp 1) but aware of multi-char elements."
1342 (let ((pos (point))
1343 (forward-sexp-function))
1344 (backward-sexp 1)
1345 (if (looking-at "\\\\begin\\>")
1346 (signal 'scan-error
1347 (list "Containing expression ends prematurely"
1348 (point) (prog1 (point) (goto-char pos))))
1349 (when (eq (char-after) ?{)
1350 (let ((newpos (point)))
1351 (when (ignore-errors (backward-sexp 1) t)
e67064e2
SM
1352 (if (or (looking-at "\\\\end\\>")
1353 ;; In case the \\ ends a verbatim section.
1354 (and (looking-at "end\\>") (eq (char-before) ?\\)))
53c4fe47
SM
1355 (tex-last-unended-begin)
1356 (goto-char newpos))))))))
1357
1358(defun latex-forward-sexp-1 ()
1359 "Like (forward-sexp 1) but aware of multi-char elements."
1360 (let ((pos (point))
1361 (forward-sexp-function))
1362 (forward-sexp 1)
1363 (let ((newpos (point)))
1364 (skip-syntax-backward "/w")
1365 (cond
1366 ((looking-at "\\\\end\\>")
1367 (signal 'scan-error
1368 (list "Containing expression ends prematurely"
1369 (point)
1370 (prog1
1371 (progn (ignore-errors (forward-sexp 2)) (point))
1372 (goto-char pos)))))
1373 ((looking-at "\\\\begin\\>")
1374 (goto-char (match-end 0))
1375 (tex-next-unmatched-end))
1376 (t (goto-char newpos))))))
1377
1378(defun latex-forward-sexp (&optional arg)
1379 "Like `forward-sexp' but aware of multi-char elements."
1380 (interactive "P")
1381 (unless arg (setq arg 1))
1382 (let ((pos (point)))
1383 (condition-case err
1384 (while (/= arg 0)
1385 (setq arg
1386 (if (> arg 0)
1387 (progn (latex-forward-sexp-1) (1- arg))
1388 (progn (latex-backward-sexp-1) (1+ arg)))))
1389 (scan-error
1390 (goto-char pos)
1391 (signal (car err) (cdr err))))))
1392
ea590e1c
SM
1393(defun latex-syntax-after ()
1394 "Like (char-syntax (char-after)) but aware of multi-char elements."
15ca8de7 1395 (if (looking-at "\\\\end\\>") ?\) (char-syntax (following-char))))
ea590e1c
SM
1396
1397(defun latex-skip-close-parens ()
1398 "Like (skip-syntax-forward \" )\") but aware of multi-char elements."
1399 (let ((forward-sexp-function nil))
1400 (while (progn (skip-syntax-forward " )")
1401 (looking-at "\\\\end\\>"))
1402 (forward-sexp 2))))
1403
1404(defun latex-down-list ()
1405 "Like (down-list 1) but aware of multi-char elements."
1406 (forward-comment (point-max))
1407 (let ((forward-sexp-function nil))
1408 (if (not (looking-at "\\\\begin\\>"))
1409 (down-list 1)
1410 (forward-sexp 1)
1411 ;; Skip arguments.
7afecb99
SM
1412 (while (looking-at "[ \t]*[[{(]")
1413 (with-syntax-table tex-mode-syntax-table
1414 (forward-sexp))))))
ea590e1c 1415
7afecb99
SM
1416(defalias 'tex-close-latex-block 'latex-close-block)
1417(define-skeleton latex-close-block
1418 "Create an \\end{...} to match the last unclosed \\begin{...}."
1419 (save-excursion
1420 (tex-last-unended-begin)
1421 (if (not (looking-at "\\\\begin\\(\\s *{[^}\n]*}\\)")) '("{" _ "}")
1422 (match-string 1)))
1423 \n "\\end" str > \n)
1424
1425(define-skeleton latex-split-block
1426 "Split the enclosing environment by inserting \\end{..}\\begin{..} at point."
1427 (save-excursion
1428 (tex-last-unended-begin)
1429 (if (not (looking-at "\\\\begin\\(\\s *{[^}\n]*}\\)")) '("{" _ "}")
1430 (prog1 (match-string 1)
1431 (goto-char (match-end 1))
1432 (setq v1 (buffer-substring (point)
1433 (progn
1434 (while (looking-at "[ \t]*[[{]")
1435 (forward-sexp 1))
1436 (point)))))))
1437 \n "\\end" str > \n _ \n "\\begin" str v1 > \n)
15ca8de7
SM
1438
1439(defconst tex-discount-args-cmds
1440 '("begin" "end" "input" "special" "cite" "ref" "include" "includeonly"
1441 "documentclass" "usepackage" "label")
1442 "TeX commands whose arguments should not be counted as text.")
1443
1444(defun tex-count-words (begin end)
1445 "Count the number of words in the buffer."
1446 (interactive
1447 (if (and transient-mark-mode mark-active)
1448 (list (region-beginning) (region-end))
1449 (list (point-min) (point-max))))
1450 ;; TODO: skip comments and math and maybe some environments.
1451 (save-excursion
1452 (goto-char begin)
1453 (let ((count 0))
1454 (while (and (< (point) end) (re-search-forward "\\<" end t))
1455 (if (not (eq (char-syntax (preceding-char)) ?/))
1456 (progn
1457 ;; Don't count single-char words.
1458 (unless (looking-at ".\\>") (incf count))
1459 (forward-char 1))
1460 (let ((cmd
1461 (buffer-substring-no-properties
1462 (point) (progn (when (zerop (skip-chars-forward "a-zA-Z@"))
1463 (forward-char 1))
1464 (point)))))
1465 (when (member cmd tex-discount-args-cmds)
1466 (skip-chars-forward "*")
1467 (forward-comment (point-max))
1468 (when (looking-at "\\[")
1469 (forward-sexp 1)
1470 (forward-comment (point-max)))
1471 (if (not (looking-at "{"))
1472 (forward-char 1)
1473 (forward-sexp 1))))))
1474 (message "%s words" count))))
b781e739
SS
1475
1476
869bff31 1477\f
1478;;; Invoking TeX in an inferior shell.
1479
15ca8de7
SM
1480;; Why use a shell instead of running TeX directly? Because if TeX
1481;; gets stuck, the user can switch to the shell window and type at it.
869bff31 1482
15ca8de7 1483;; The utility functions:
869bff31 1484
bdbd9606 1485(define-derived-mode tex-shell shell-mode "TeX-Shell"
3f2c97ea
SM
1486 (set (make-local-variable 'compilation-parse-errors-function)
1487 'tex-compilation-parse-errors)
bdbd9606
SM
1488 (compilation-shell-minor-mode t))
1489
d974af30 1490;;;###autoload
869bff31 1491(defun tex-start-shell ()
d6709b80
SM
1492 (with-current-buffer
1493 (make-comint
1494 "tex-shell"
07c16ec4 1495 (or tex-shell-file-name (getenv "ESHELL") shell-file-name)
4042dc25 1496 nil
053be11a 1497 ;; Specify an interactive shell, to make sure it prompts.
4042dc25 1498 "-i")
528415e7
RS
1499 (let ((proc (get-process "tex-shell")))
1500 (set-process-sentinel proc 'tex-shell-sentinel)
003274a0 1501 (set-process-query-on-exit-flag proc nil)
bdbd9606 1502 (tex-shell)
528415e7 1503 (while (zerop (buffer-size))
51b2c841
RS
1504 (sleep-for 1)))))
1505
033306e3
RS
1506(defun tex-feed-input ()
1507 "Send input to the tex shell process.
1508In the tex buffer this can be used to continue an interactive tex run.
bed1b0c8 1509In the tex shell buffer this command behaves like `comint-send-input'."
033306e3 1510 (interactive)
b781e739 1511 (set-buffer (tex-shell-buf))
033306e3
RS
1512 (comint-send-input)
1513 (tex-recenter-output-buffer nil))
1514
51b2c841
RS
1515(defun tex-display-shell ()
1516 "Make the TeX shell buffer visible in a window."
b781e739 1517 (display-buffer (tex-shell-buf))
51b2c841 1518 (tex-recenter-output-buffer nil))
528415e7
RS
1519
1520(defun tex-shell-sentinel (proc msg)
1521 (cond ((null (buffer-name (process-buffer proc)))
1522 ;; buffer killed
1523 (set-process-buffer proc nil)
1524 (tex-delete-last-temp-files))
1525 ((memq (process-status proc) '(signal exit))
1526 (tex-delete-last-temp-files))))
1527
1528(defun tex-set-buffer-directory (buffer directory)
869bff31 1529 "Set BUFFER's default directory to be DIRECTORY."
1530 (setq directory (file-name-as-directory (expand-file-name directory)))
1531 (if (not (file-directory-p directory))
1532 (error "%s is not a directory" directory)
1533 (save-excursion
1534 (set-buffer buffer)
1535 (setq default-directory directory))))
1536
30803a05
RS
1537(defvar tex-send-command-modified-tick 0)
1538(make-variable-buffer-local 'tex-send-command-modified-tick)
1539
bdbd9606 1540(defun tex-shell-proc ()
b781e739 1541 (or (tex-shell-running) (error "No TeX subprocess")))
bdbd9606
SM
1542(defun tex-shell-buf ()
1543 (process-buffer (tex-shell-proc)))
b781e739
SS
1544(defun tex-shell-buf-no-error ()
1545 (let ((proc (tex-shell-running)))
1546 (and proc (process-buffer proc))))
bdbd9606 1547
528415e7 1548(defun tex-send-command (command &optional file background)
4cdc1d4b 1549 "Send COMMAND to TeX shell process, substituting optional FILE for *.
8241d7b9
ER
1550Do this in background if optional BACKGROUND is t. If COMMAND has no *,
1551FILE will be appended, preceded by a blank, to COMMAND. If FILE is nil, no
1552substitution will be made in COMMAND. COMMAND can be any expression that
460e1b7d
RS
1553evaluates to a command string.
1554
1555Return the process in which TeX is running."
528415e7
RS
1556 (save-excursion
1557 (let* ((cmd (eval command))
bdbd9606 1558 (proc (tex-shell-proc))
64db2461 1559 (buf (process-buffer proc))
4f45adda 1560 (star (string-match "\\*" cmd))
4cdc1d4b
RS
1561 (string
1562 (concat
1563 (if file
1564 (if star (concat (substring cmd 0 star)
1565 file (substring cmd (1+ star)))
1566 (concat cmd " " file))
1567 cmd)
1568 (if background "&" ""))))
64db2461
RS
1569 ;; Switch to buffer before checking for subproc output in it.
1570 (set-buffer buf)
30803a05
RS
1571 ;; If text is unchanged since previous tex-send-command,
1572 ;; we haven't got any output. So wait for output now.
64db2461 1573 (if (= (buffer-modified-tick buf) tex-send-command-modified-tick)
30803a05 1574 (accept-process-output proc))
4cdc1d4b
RS
1575 (goto-char (process-mark proc))
1576 (insert string)
30803a05 1577 (comint-send-input)
460e1b7d
RS
1578 (setq tex-send-command-modified-tick (buffer-modified-tick buf))
1579 proc)))
528415e7 1580
a15849cb
RS
1581(defun tex-delete-last-temp-files (&optional not-all)
1582 "Delete any junk files from last temp file.
1583If NOT-ALL is non-nil, save the `.dvi' file."
528415e7
RS
1584 (if tex-last-temp-file
1585 (let* ((dir (file-name-directory tex-last-temp-file))
adf6b7f9
KH
1586 (list (and (file-directory-p dir)
1587 (file-name-all-completions
02fd229c
RS
1588 (file-name-sans-extension
1589 (file-name-nondirectory tex-last-temp-file))
1590 dir))))
adf6b7f9 1591 (while list
a15849cb
RS
1592 (if not-all
1593 (and
1594 ;; If arg is non-nil, don't delete the .dvi file.
1595 (not (string-match "\\.dvi$" (car list)))
1596 (delete-file (concat dir (car list))))
1597 (delete-file (concat dir (car list))))
528415e7
RS
1598 (setq list (cdr list))))))
1599
99621a14 1600(add-hook 'kill-emacs-hook 'tex-delete-last-temp-files)
869bff31 1601
75035a80
SM
1602;;
1603;; Machinery to guess the command that the user wants to execute.
1604;;
1605
1606(defvar tex-compile-history nil)
1607
1608(defvar tex-input-files-re
1609 (eval-when-compile
1610 (concat "\\." (regexp-opt '("tex" "texi" "texinfo"
1611 "bbl" "ind" "sty" "cls") t)
1612 ;; Include files with no dots (for directories).
1613 "\\'\\|\\`[^.]+\\'")))
1614
1615(defcustom tex-use-reftex t
1616 "If non-nil, use RefTeX's list of files to determine what command to use."
1617 :type 'boolean)
1618
1619(defvar tex-compile-commands
1620 '(((concat "pdf" tex-command
bf4ec222
SM
1621 " " (if (< 0 (length tex-start-commands))
1622 (shell-quote-argument tex-start-commands)) " %f")
75035a80
SM
1623 t "%r.pdf")
1624 ((concat tex-command
bf4ec222
SM
1625 " " (if (< 0 (length tex-start-commands))
1626 (shell-quote-argument tex-start-commands)) " %f")
75035a80 1627 t "%r.dvi")
e70ff1af 1628 ("yap %r &" "%r.dvi")
75035a80
SM
1629 ("xdvi %r &" "%r.dvi")
1630 ("advi %r &" "%r.dvi")
1631 ("bibtex %r" "%r.aux" "%r.bbl")
1632 ("makeindex %r" "%r.idx" "%r.ind")
1633 ("texindex %r.??")
1634 ("dvipdfm %r" "%r.dvi" "%r.pdf")
1635 ("dvipdf %r" "%r.dvi" "%r.pdf")
dd166d5f 1636 ("dvips -o %r.ps %r" "%r.dvi" "%r.ps")
e70ff1af 1637 ("ps2pdf %r.ps" "%r.ps" "%r.pdf")
75035a80
SM
1638 ("gv %r.ps &" "%r.ps")
1639 ("gv %r.pdf &" "%r.pdf")
1640 ("xpdf %r.pdf &" "%r.pdf")
1641 ("lpr %r.ps" "%r.ps"))
1642 "List of commands for `tex-compile'.
1643Each element should be of the form (FORMAT IN OUT) where
1644FORMAT is an expression that evaluates to a string that can contain
1645 - `%r' the main file name without extension.
1646 - `%f' the main file name.
1647IN can be either a string (with the same % escapes in it) indicating
1648 the name of the input file, or t to indicate that the input is all
1649 the TeX files of the document, or nil if we don't know.
1650OUT describes the output file and is either a %-escaped string
1651 or nil to indicate that there is no output file.")
1652
8084f5d8
SM
1653;; defsubst* gives better byte-code than defsubst.
1654(defsubst* tex-string-prefix-p (str1 str2)
1655 "Return non-nil if STR1 is a prefix of STR2"
1656 (eq t (compare-strings str2 nil (length str1) str1 nil nil)))
1657
dd459839
SM
1658(defun tex-guess-main-file (&optional all)
1659 "Find a likely `tex-main-file'.
1660Looks for hints in other buffers in the same directory or in
8084f5d8
SM
1661ALL other buffers. If ALL is `sub' only look at buffers in parent directories
1662of the current buffer."
dd459839
SM
1663 (let ((dir default-directory)
1664 (header-re tex-start-of-header))
1665 (catch 'found
1666 ;; Look for a buffer with `tex-main-file' set.
1667 (dolist (buf (if (consp all) all (buffer-list)))
1668 (with-current-buffer buf
8084f5d8
SM
1669 (when (and (cond
1670 ((null all) (equal dir default-directory))
1671 ((eq all 'sub) (tex-string-prefix-p default-directory dir))
1672 (t))
dd459839
SM
1673 (stringp tex-main-file))
1674 (throw 'found (expand-file-name tex-main-file)))))
1675 ;; Look for a buffer containing the magic `tex-start-of-header'.
1676 (dolist (buf (if (consp all) all (buffer-list)))
1677 (with-current-buffer buf
8084f5d8
SM
1678 (when (and (cond
1679 ((null all) (equal dir default-directory))
1680 ((eq all 'sub) (tex-string-prefix-p default-directory dir))
1681 (t))
dd459839
SM
1682 buffer-file-name
1683 ;; (or (easy-mmode-derived-mode-p 'latex-mode)
1684 ;; (easy-mmode-derived-mode-p 'plain-tex-mode))
1685 (save-excursion
cb30255a
SM
1686 (save-restriction
1687 (widen)
1688 (goto-char (point-min))
95a045bd
SM
1689 (re-search-forward
1690 header-re (+ (point) 10000) t))))
dd459839
SM
1691 (throw 'found (expand-file-name buffer-file-name))))))))
1692
07c16ec4
SM
1693(defun tex-main-file ()
1694 "Return the relative name of the main file."
bdbd9606
SM
1695 (let* ((file (or tex-main-file
1696 ;; Compatibility with AUCTeX.
96c0d9e9 1697 (with-no-warnings
034a48f4
MR
1698 (when (boundp 'TeX-master)
1699 (cond ((stringp TeX-master)
1700 (make-local-variable 'tex-main-file)
1701 (setq tex-main-file TeX-master))
1702 ((and (eq TeX-master t) buffer-file-name)
1703 (file-relative-name buffer-file-name)))))
bdbd9606
SM
1704 ;; Try to guess the main file.
1705 (if (not buffer-file-name)
1706 (error "Buffer is not associated with any file")
1707 (file-relative-name
1708 (if (save-excursion
1709 (goto-char (point-min))
95a045bd
SM
1710 (re-search-forward tex-start-of-header
1711 (+ (point) 10000) t))
bdbd9606
SM
1712 ;; This is the main file.
1713 buffer-file-name
1714 ;; This isn't the main file, let's try to find better,
1715 (or (tex-guess-main-file)
8084f5d8 1716 (tex-guess-main-file 'sub)
bdbd9606 1717 ;; (tex-guess-main-file t)
07c16ec4 1718 buffer-file-name)))))))
e70ff1af
SM
1719 (if (or (file-exists-p file) (string-match "\\.tex\\'" file))
1720 file (concat file ".tex"))))
dd459839 1721
75035a80
SM
1722(defun tex-summarize-command (cmd)
1723 (if (not (stringp cmd)) ""
1724 (mapconcat 'identity
1725 (mapcar (lambda (s) (car (split-string s)))
1726 (split-string cmd "\\s-*\\(?:;\\|&&\\)\\s-*"))
1727 "&")))
1728
1729(defun tex-uptodate-p (file)
1730 "Return non-nil if FILE is not uptodate w.r.t the document source files.
1731FILE is typically the output DVI or PDF file."
1732 ;; We should check all the files included !!!
1733 (and
1734 ;; Clearly, the target must exist.
1735 (file-exists-p file)
1736 ;; And the last run must not have asked for a rerun.
1737 ;; FIXME: this should check that the last run was done on the same file.
1738 (let ((buf (condition-case nil (tex-shell-buf) (error nil))))
1739 (when buf
1740 (with-current-buffer buf
1741 (save-excursion
1742 (goto-char (point-max))
1743 (and (re-search-backward
1744 "(see the transcript file for additional information)" nil t)
1745 (> (save-excursion
1746 (or (re-search-backward "\\[[0-9]+\\]" nil t)
1747 (point-min)))
1748 (save-excursion
1749 (or (re-search-backward "Rerun" nil t)
1750 (point-min)))))))))
1751 ;; And the input files must not have been changed in the meantime.
1752 (let ((files (if (and tex-use-reftex
1753 (fboundp 'reftex-scanning-info-available-p)
1754 (reftex-scanning-info-available-p))
1755 (reftex-all-document-files)
1756 (list (file-name-directory (expand-file-name file)))))
1757 (ignored-dirs-re
1758 (concat
1759 (regexp-opt
1760 (delq nil (mapcar (lambda (s) (if (eq (aref s (1- (length s))) ?/)
1761 (substring s 0 (1- (length s)))))
1762 completion-ignored-extensions))
1763 t) "\\'"))
1764 (uptodate t))
1765 (while (and files uptodate)
1766 (let ((f (pop files)))
e70ff1af
SM
1767 (if (and (file-directory-p f)
1768 ;; Avoid infinite loops.
1769 (not (file-symlink-p f)))
75035a80
SM
1770 (unless (string-match ignored-dirs-re f)
1771 (setq files (nconc
1772 (directory-files f t tex-input-files-re)
1773 files)))
1774 (when (file-newer-than-file-p f file)
1775 (setq uptodate nil)))))
1776 uptodate)))
bf247b6e 1777
75035a80
SM
1778
1779(autoload 'format-spec "format-spec")
1780
1781(defvar tex-executable-cache nil)
1782(defun tex-executable-exists-p (name)
1783 "Like `executable-find' but with a cache."
1784 (let ((cache (assoc name tex-executable-cache)))
1785 (if cache (cdr cache)
1786 (let ((executable (executable-find name)))
1787 (push (cons name executable) tex-executable-cache)
1788 executable))))
1789
1790(defun tex-command-executable (cmd)
1791 (let ((s (if (stringp cmd) cmd (eval (car cmd)))))
1792 (substring s 0 (string-match "[ \t]\\|\\'" s))))
1793
1794(defun tex-command-active-p (cmd fspec)
1795 "Return non-nil if the CMD spec might need to be run."
1796 (let ((in (nth 1 cmd))
1797 (out (nth 2 cmd)))
1798 (if (stringp in)
1799 (let ((file (format-spec in fspec)))
1800 (when (file-exists-p file)
1801 (or (not out)
1802 (file-newer-than-file-p
1803 file (format-spec out fspec)))))
1804 (when (and (eq in t) (stringp out))
1805 (not (tex-uptodate-p (format-spec out fspec)))))))
1806
8084f5d8 1807(defun tex-compile-default (fspec)
dd166d5f 1808 "Guess a default command given the `format-spec' FSPEC."
75035a80
SM
1809 ;; TODO: Learn to do latex+dvips!
1810 (let ((cmds nil)
1811 (unchanged-in nil))
1812 ;; Only consider active commands.
1813 (dolist (cmd tex-compile-commands)
1814 (when (tex-executable-exists-p (tex-command-executable cmd))
1815 (if (tex-command-active-p cmd fspec)
1816 (push cmd cmds)
1817 (push (nth 1 cmd) unchanged-in))))
dd166d5f
SM
1818 ;; If no command seems to be applicable, arbitrarily pick the first one.
1819 (unless cmds
1820 (setq cmds (list (car tex-compile-commands))))
75035a80
SM
1821 ;; Remove those commands whose input was considered stable for
1822 ;; some other command (typically if (t . "%.pdf") is inactive
1823 ;; then we're using pdflatex and the fact that the dvi file
1824 ;; is inexistent doesn't matter).
1825 (let ((tmp nil))
1826 (dolist (cmd cmds)
1827 (unless (member (nth 1 cmd) unchanged-in)
1828 (push cmd tmp)))
8084f5d8 1829 ;; Only remove if there's something left.
75035a80 1830 (if tmp (setq cmds tmp)))
8084f5d8
SM
1831 ;; Remove commands whose input is not uptodate either.
1832 (let ((outs (delq nil (mapcar (lambda (x) (nth 2 x)) cmds)))
1833 (tmp nil))
1834 (dolist (cmd cmds)
75035a80 1835 (unless (member (nth 1 cmd) outs)
8084f5d8
SM
1836 (push cmd tmp)))
1837 ;; Only remove if there's something left.
1838 (if tmp (setq cmds tmp)))
75035a80
SM
1839 ;; Select which file we're going to operate on (the latest).
1840 (let ((latest (nth 1 (car cmds))))
1841 (dolist (cmd (prog1 (cdr cmds) (setq cmds (list (car cmds)))))
1842 (if (equal latest (nth 1 cmd))
1843 (push cmd cmds)
1844 (unless (eq latest t) ;Can't beat that!
1845 (if (or (not (stringp latest))
1846 (eq (nth 1 cmd) t)
1847 (and (stringp (nth 1 cmd))
1848 (file-newer-than-file-p
1849 (format-spec (nth 1 cmd) fspec)
1850 (format-spec latest fspec))))
1851 (setq latest (nth 1 cmd) cmds (list cmd)))))))
1852 ;; Expand the command spec into the actual text.
1853 (dolist (cmd (prog1 cmds (setq cmds nil)))
1854 (push (cons (eval (car cmd)) (cdr cmd)) cmds))
1855 ;; Select the favorite command from the history.
1856 (let ((hist tex-compile-history)
8084f5d8 1857 re hist-cmd)
75035a80 1858 (while hist
8084f5d8 1859 (setq hist-cmd (pop hist))
75035a80 1860 (setq re (concat "\\`"
8084f5d8 1861 (regexp-quote (tex-command-executable hist-cmd))
75035a80
SM
1862 "\\([ \t]\\|\\'\\)"))
1863 (dolist (cmd cmds)
8084f5d8
SM
1864 ;; If the hist entry uses the same command and applies to a file
1865 ;; of the same type (e.g. `gv %r.pdf' vs `gv %r.ps'), select cmd.
1866 (and (string-match re (car cmd))
1867 (or (not (string-match "%[fr]\\([-._[:alnum:]]+\\)" (car cmd)))
1868 (string-match (regexp-quote (match-string 1 (car cmd)))
1869 hist-cmd))
1870 (setq hist nil cmds (list cmd)))))
1871 ;; Substitute and return.
1872 (if (and hist-cmd
1873 (string-match (concat "[' \t\"]" (format-spec "%r" fspec)
1874 "\\([;&' \t\"]\\|\\'\\)") hist-cmd))
1875 ;; The history command was already applied to the same file,
1876 ;; so just reuse it.
1877 hist-cmd
1878 (if cmds (format-spec (caar cmds) fspec))))))
75035a80
SM
1879
1880(defun tex-compile (dir cmd)
1881 "Run a command CMD on current TeX buffer's file in DIR."
1882 ;; FIXME: Use time-stamps on files to decide the next op.
1883 (interactive
1884 (let* ((file (tex-main-file))
5e56fb53
SM
1885 (default-directory
1886 (prog1 (file-name-directory (expand-file-name file))
1887 (setq file (file-name-nondirectory file))))
75035a80 1888 (root (file-name-sans-extension file))
75035a80
SM
1889 (fspec (list (cons ?r (comint-quote-filename root))
1890 (cons ?f (comint-quote-filename file))))
8084f5d8 1891 (default (tex-compile-default fspec)))
5e56fb53 1892 (list default-directory
75035a80
SM
1893 (completing-read
1894 (format "Command [%s]: " (tex-summarize-command default))
1895 (mapcar (lambda (x)
1896 (list (format-spec (eval (car x)) fspec)))
1897 tex-compile-commands)
1898 nil nil nil 'tex-compile-history default))))
1899 (save-some-buffers (not compilation-ask-about-save) nil)
1900 (if (tex-shell-running)
1901 (tex-kill-job)
1902 (tex-start-shell))
1903 (tex-send-tex-command cmd dir))
d6709b80
SM
1904
1905(defun tex-start-tex (command file &optional dir)
460e1b7d 1906 "Start a TeX run, using COMMAND on FILE."
6633b891 1907 (let* ((star (string-match "\\*" command))
460e1b7d 1908 (compile-command
6633b891
KH
1909 (if star
1910 (concat (substring command 0 star)
1911 (comint-quote-filename file)
1912 (substring command (1+ star)))
1913 (concat command " "
95a045bd 1914 tex-start-options
9e0ad27a 1915 (if (< 0 (length tex-start-commands))
c3ce7bf4 1916 (concat
9e0ad27a 1917 (shell-quote-argument tex-start-commands) " "))
6633b891 1918 (comint-quote-filename file)))))
bdbd9606
SM
1919 (tex-send-tex-command compile-command dir)))
1920
1921(defun tex-send-tex-command (cmd &optional dir)
b781e739
SS
1922 (unless (or (equal dir (let ((buf (tex-shell-buf-no-error)))
1923 (and buf (with-current-buffer buf
1924 default-directory))))
bdbd9606
SM
1925 (not dir))
1926 (let (shell-dirtrack-verbose)
1927 (tex-send-command tex-shell-cd-command dir)))
1928 (with-current-buffer (process-buffer (tex-send-command cmd))
bdbd9606
SM
1929 (setq compilation-last-buffer (current-buffer))
1930 (compilation-forget-errors)
1598a961 1931 ;; Don't parse previous compilations.
bdbd9606
SM
1932 (set-marker compilation-parsing-end (1- (point-max))))
1933 (tex-display-shell)
1934 (setq tex-last-buffer-texed (current-buffer)))
460e1b7d 1935\f
1598a961
SM
1936(defvar tex-error-parse-syntax-table
1937 (let ((st (make-syntax-table)))
1938 (modify-syntax-entry ?\( "()" st)
1939 (modify-syntax-entry ?\) ")(" st)
1940 (modify-syntax-entry ?\\ "\\" st)
1941 (modify-syntax-entry ?\{ "_" st)
1942 (modify-syntax-entry ?\} "_" st)
1943 (modify-syntax-entry ?\[ "_" st)
1944 (modify-syntax-entry ?\] "_" st)
1945 ;; Single quotations may appear in errors
1946 (modify-syntax-entry ?\" "_" st)
1947 st)
1948 "Syntax-table used while parsing TeX error messages.")
1949
460e1b7d 1950(defun tex-compilation-parse-errors (limit-search find-at-least)
f05bd645
RS
1951 "Parse the current buffer as TeX error messages.
1952See the variable `compilation-parse-errors-function' for the interface it uses.
1953
1954This function parses only the last TeX compilation.
1955It works on TeX compilations only. It is necessary for that purpose,
1956since TeX does not put file names and line numbers on the same line as
1957for the error messages."
460e1b7d
RS
1958 (require 'thingatpt)
1959 (setq compilation-error-list nil)
f05bd645
RS
1960 (let ((default-directory ; Perhaps dir has changed meanwhile.
1961 (file-name-directory (buffer-file-name tex-last-buffer-texed)))
f05bd645
RS
1962 found-desired (num-errors-found 0)
1963 last-filename last-linenum last-position
1964 begin-of-error end-of-error)
f05bd645
RS
1965 ;; Don't reparse messages already seen at last parse.
1966 (goto-char compilation-parsing-end)
1967 ;; Parse messages.
1968 (while (and (not (or found-desired (eobp)))
1969 (prog1 (re-search-forward "^! " nil 'move)
1970 (setq begin-of-error (match-beginning 0)
1971 end-of-error (match-end 0)))
1972 (re-search-forward
1973 "^l\\.\\([0-9]+\\) \\(\\.\\.\\.\\)?\\(.*\\)$" nil 'move))
3f2c97ea 1974 (let* ((this-error (copy-marker begin-of-error))
003274a0 1975 (linenum (string-to-number (match-string 1)))
f05bd645
RS
1976 (error-text (regexp-quote (match-string 3)))
1977 (filename
1978 (save-excursion
ea590e1c
SM
1979 (with-syntax-table tex-error-parse-syntax-table
1980 (backward-up-list 1)
1981 (skip-syntax-forward "(_")
1982 (while (not (file-readable-p (thing-at-point 'filename)))
1983 (skip-syntax-backward "(_")
1984 (backward-up-list 1)
1985 (skip-syntax-forward "(_"))
1986 (thing-at-point 'filename))))
f05bd645
RS
1987 (new-file
1988 (or (null last-filename)
1989 (not (string-equal last-filename filename))))
1990 (error-location
cc9f5376
SM
1991 (with-current-buffer
1992 (if (equal filename (concat tex-zap-file ".tex"))
1993 tex-last-buffer-texed
1994 (find-file-noselect filename))
1995 (save-excursion
1996 (if new-file
1997 (progn (goto-line linenum) (setq last-position nil))
1998 (goto-char last-position)
1999 (forward-line (- linenum last-linenum)))
2000 ;; first try a forward search for the error text,
2001 ;; then a backward search limited by the last error.
2002 (let ((starting-point (point)))
2003 (or (re-search-forward error-text nil t)
2004 (re-search-backward error-text last-position t)
2005 (goto-char starting-point)))
2006 (point-marker)))))
f05bd645
RS
2007 (goto-char this-error)
2008 (if (and compilation-error-list
2009 (or (and find-at-least
2010 (>= num-errors-found
2011 find-at-least))
2012 (and limit-search
2013 (>= end-of-error limit-search)))
2014 new-file)
2015 (setq found-desired t)
2016 (setq num-errors-found (1+ num-errors-found)
2017 last-filename filename
2018 last-linenum linenum
2019 last-position error-location
2020 compilation-error-list ; Add the new error
2021 (cons (cons this-error error-location)
2022 compilation-error-list))
2023 (goto-char end-of-error)))))
ee13a145 2024 (set-marker compilation-parsing-end (point))
13376c78 2025 (setq compilation-error-list (nreverse compilation-error-list)))
460e1b7d 2026\f
528415e7 2027;;; The commands:
869bff31 2028
2029(defun tex-region (beg end)
2030 "Run TeX on the current region, via a temporary file.
2031The file's name comes from the variable `tex-zap-file' and the
2032variable `tex-directory' says where to put it.
2033
2034If the buffer has a header, the header is given to TeX before the
2035region itself. The buffer's header is all lines between the strings
2036defined by `tex-start-of-header' and `tex-end-of-header' inclusive.
2037The header must start in the first 100 lines of the buffer.
2038
2039The value of `tex-trailer' is given to TeX as input after the region.
2040
2041The value of `tex-command' specifies the command to use to run TeX."
2042 (interactive "r")
2043 (if (tex-shell-running)
2044 (tex-kill-job)
2045 (tex-start-shell))
2046 (or tex-zap-file
2047 (setq tex-zap-file (tex-generate-zap-file-name)))
6d34c59f
RS
2048 ;; Temp file will be written and TeX will be run in zap-directory.
2049 ;; If the TEXINPUTS file has relative directories or if the region has
2050 ;; \input of files, this must be the same directory as the file for
2051 ;; TeX to access the correct inputs. That's why it's safest if
2052 ;; tex-directory is ".".
2053 (let* ((zap-directory
528415e7 2054 (file-name-as-directory (expand-file-name tex-directory)))
d6709b80
SM
2055 (tex-out-file (expand-file-name (concat tex-zap-file ".tex")
2056 zap-directory)))
0d548e5d
RS
2057 ;; Don't delete temp files if we do the same buffer twice in a row.
2058 (or (eq (current-buffer) tex-last-buffer-texed)
2059 (tex-delete-last-temp-files t))
869bff31 2060 ;; Write the new temp file.
2061 (save-excursion
2062 (save-restriction
2063 (widen)
2064 (goto-char (point-min))
2065 (forward-line 100)
2066 (let ((search-end (point))
6d34c59f
RS
2067 (default-directory zap-directory)
2068 (already-output 0))
869bff31 2069 (goto-char (point-min))
6d34c59f 2070
725b7428
RS
2071 ;; Maybe copy first line, such as `\input texinfo', to temp file.
2072 (and tex-first-line-header-regexp
2073 (looking-at tex-first-line-header-regexp)
bed1b0c8 2074 (write-region (point)
6d34c59f
RS
2075 (progn (forward-line 1)
2076 (setq already-output (point)))
725b7428
RS
2077 tex-out-file nil nil))
2078
6d34c59f
RS
2079 ;; Write out the header, if there is one,
2080 ;; and any of the specified region which extends before it.
2081 ;; But don't repeat anything already written.
898b9ac1 2082 (if (re-search-forward tex-start-of-header search-end t)
6d34c59f 2083 (let (hbeg)
869bff31 2084 (beginning-of-line)
2085 (setq hbeg (point)) ;mark beginning of header
898b9ac1 2086 (if (re-search-forward tex-end-of-header nil t)
6d34c59f
RS
2087 (let (hend)
2088 (forward-line 1)
bed1b0c8 2089 (setq hend (point)) ;mark end of header
6d34c59f
RS
2090 (write-region (max (min hbeg beg) already-output)
2091 hend
2092 tex-out-file
2093 (not (zerop already-output)) nil)
2094 (setq already-output hend)))))
2095
2096 ;; Write out the specified region
2097 ;; (but don't repeat anything already written).
2098 (write-region (max beg already-output) end
2099 tex-out-file
2100 (not (zerop already-output)) nil))
2101 ;; Write the trailer, if any.
2102 ;; Precede it with a newline to make sure it
2103 ;; is not hidden in a comment.
2104 (if tex-trailer
2105 (write-region (concat "\n" tex-trailer) nil
2106 tex-out-file t nil))))
528415e7
RS
2107 ;; Record the file name to be deleted afterward.
2108 (setq tex-last-temp-file tex-out-file)
f05bd645
RS
2109 ;; Use a relative file name here because (1) the proper dir
2110 ;; is already current, and (2) the abs file name is sometimes
2111 ;; too long and can make tex crash.
d6709b80
SM
2112 (tex-start-tex tex-command (concat tex-zap-file ".tex") zap-directory)
2113 (setq tex-print-file tex-out-file)))
869bff31 2114
2115(defun tex-buffer ()
2116 "Run TeX on current buffer. See \\[tex-region] for more information.
528415e7
RS
2117Does not save the buffer, so it's useful for trying experimental versions.
2118See \\[tex-file] for an alternative."
869bff31 2119 (interactive)
2120 (tex-region (point-min) (point-max)))
2121
2122(defun tex-file ()
2123 "Prompt to save all buffers and run TeX (or LaTeX) on current buffer's file.
2124This function is more useful than \\[tex-buffer] when you need the
2125`.aux' file of LaTeX to have the correct name."
2126 (interactive)
7abc9add
SS
2127 (when tex-offer-save
2128 (save-some-buffers))
d6709b80 2129 (let* ((source-file (tex-main-file))
07c16ec4 2130 (file-dir (file-name-directory (expand-file-name source-file))))
869bff31 2131 (if (tex-shell-running)
2132 (tex-kill-job)
2133 (tex-start-shell))
d6709b80 2134 (tex-start-tex tex-command source-file file-dir)
f05bd645 2135 (setq tex-print-file (expand-file-name source-file))))
869bff31 2136
2137(defun tex-generate-zap-file-name ()
2138 "Generate a unique name suitable for use as a file name."
2139 ;; Include the shell process number and host name
2140 ;; in case there are multiple shells (for same or different user).
f5cdb851
KH
2141 ;; Dec 1998: There is a report that some versions of xdvi
2142 ;; don't work with file names that start with #.
5616ee46 2143 (format "_TZ_%d-%s"
869bff31 2144 (process-id (get-buffer-process "*tex-shell*"))
07c16ec4 2145 (subst-char-in-string ?. ?- (system-name))))
869bff31 2146
2147;; This will perhaps be useful for modifying TEXINPUTS.
2148;; Expand each file name, separated by colons, in the string S.
2149(defun tex-expand-files (s)
2150 (let (elts (start 0))
2151 (while (string-match ":" s start)
2152 (setq elts (cons (substring s start (match-beginning 0)) elts))
2153 (setq start (match-end 0)))
2154 (or (= start 0)
2155 (setq elts (cons (substring s start) elts)))
c0df1972
SM
2156 (mapconcat (lambda (elt)
2157 (if (= (length elt) 0) elt (expand-file-name elt)))
5616ee46 2158 (nreverse elts) ":")))
869bff31 2159
2160(defun tex-shell-running ()
713f7b15 2161 (let ((proc (get-process "tex-shell")))
b781e739
SS
2162 (when proc
2163 (if (and (eq (process-status proc) 'run)
2164 (buffer-live-p (process-buffer proc)))
2165 ;; return the TeX process on success
2166 proc
2167 ;; get rid of the process permanently
2168 ;; this should get rid of the annoying w32 problem with
2169 ;; dead tex-shell buffer and live process
2170 (delete-process proc)))))
869bff31 2171
2172(defun tex-kill-job ()
2173 "Kill the currently running TeX job."
2174 (interactive)
b781e739 2175 ;; `quit-process' leads to core dumps of the tex process (except if
bed1b0c8
RS
2176 ;; coredumpsize has limit 0kb as on many environments). One would
2177 ;; like to use (kill-process proc 'lambda), however that construct
2178 ;; does not work on some systems and kills the shell itself.
b781e739
SS
2179 (let ((proc (get-process "tex-shell")))
2180 (when proc (quit-process proc t))))
869bff31 2181
2182(defun tex-recenter-output-buffer (linenum)
2183 "Redisplay buffer of TeX job output so that most recent output can be seen.
2184The last line of the buffer is displayed on
2185line LINE of the window, or centered if LINE is nil."
2186 (interactive "P")
2187 (let ((tex-shell (get-buffer "*tex-shell*"))
23b64225 2188 (window))
869bff31 2189 (if (null tex-shell)
2190 (message "No TeX output buffer")
23b64225
RS
2191 (setq window (display-buffer tex-shell))
2192 (save-selected-window
2193 (select-window window)
2194 (bury-buffer tex-shell)
2195 (goto-char (point-max))
2196 (recenter (if linenum
2197 (prefix-numeric-value linenum)
2198 (/ (window-height) 2)))))))
869bff31 2199
528415e7 2200(defun tex-print (&optional alt)
869bff31 2201 "Print the .dvi file made by \\[tex-region], \\[tex-buffer] or \\[tex-file].
1433a222
CZ
2202Runs the shell command defined by `tex-dvi-print-command'. If prefix argument
2203is provided, use the alternative command, `tex-alt-dvi-print-command'."
528415e7 2204 (interactive "P")
869bff31 2205 (let ((print-file-name-dvi (tex-append tex-print-file ".dvi"))
2206 test-name)
2207 (if (and (not (equal (current-buffer) tex-last-buffer-texed))
45c3304d
RS
2208 (buffer-file-name)
2209 ;; Check that this buffer's printed file is up to date.
869bff31 2210 (file-newer-than-file-p
2211 (setq test-name (tex-append (buffer-file-name) ".dvi"))
45c3304d 2212 (buffer-file-name)))
869bff31 2213 (setq print-file-name-dvi test-name))
528415e7
RS
2214 (if (not (file-exists-p print-file-name-dvi))
2215 (error "No appropriate `.dvi' file could be found")
460e1b7d
RS
2216 (if (tex-shell-running)
2217 (tex-kill-job)
2218 (tex-start-shell))
528415e7 2219 (tex-send-command
bed1b0c8 2220 (if alt tex-alt-dvi-print-command tex-dvi-print-command)
602503c5
RS
2221 (shell-quote-argument
2222 print-file-name-dvi)
2223 t))))
869bff31 2224
cf6d6e8a
RS
2225(defun tex-alt-print ()
2226 "Print the .dvi file made by \\[tex-region], \\[tex-buffer] or \\[tex-file].
002b0d00 2227Runs the shell command defined by `tex-alt-dvi-print-command'."
cf6d6e8a
RS
2228 (interactive)
2229 (tex-print t))
2230
869bff31 2231(defun tex-view ()
2232 "Preview the last `.dvi' file made by running TeX under Emacs.
2233This means, made using \\[tex-region], \\[tex-buffer] or \\[tex-file].
2b7971c9
RS
2234The variable `tex-dvi-view-command' specifies the shell command for preview.
2235You must set that variable yourself before using this command,
2236because there is no standard value that would generally work."
869bff31 2237 (interactive)
2b7971c9
RS
2238 (or tex-dvi-view-command
2239 (error "You must set `tex-dvi-view-command'"))
746c30e2 2240 (let ((tex-dvi-print-command (eval tex-dvi-view-command)))
869bff31 2241 (tex-print)))
2242
2243(defun tex-append (file-name suffix)
2244 "Append to FILENAME the suffix SUFFIX, using same algorithm TeX uses.
cf6d6e8a 2245Pascal-based TeX scans for the first period, C TeX uses the last.
869bff31 2246No period is retained immediately before SUFFIX,
2247so normally SUFFIX starts with one."
2248 (if (stringp file-name)
cf6d6e8a
RS
2249 (let ((file (file-name-nondirectory file-name))
2250 trial-name)
6da9bbd6 2251 ;; Try splitting on last period.
7d0ca249
RS
2252 ;; The first-period split can get fooled when two files
2253 ;; named a.tex and a.b.tex are both tex'd;
2254 ;; the last-period split must be right if it matches at all.
cf6d6e8a
RS
2255 (setq trial-name
2256 (concat (file-name-directory file-name)
2257 (substring file 0
7d0ca249 2258 (string-match "\\.[^.]*$" file))
cf6d6e8a
RS
2259 suffix))
2260 (if (or (file-exists-p trial-name)
2261 (file-exists-p (concat trial-name ".aux"))) ;for BibTeX files
2262 trial-name
7d0ca249 2263 ;; Not found, so split on first period.
cf6d6e8a
RS
2264 (concat (file-name-directory file-name)
2265 (substring file 0
7d0ca249 2266 (string-match "\\." file))
cf6d6e8a 2267 suffix)))
869bff31 2268 " "))
2269
2270(defun tex-show-print-queue ()
2271 "Show the print queue that \\[tex-print] put your job on.
1433a222 2272Runs the shell command defined by `tex-show-queue-command'."
869bff31 2273 (interactive)
2274 (if (tex-shell-running)
2275 (tex-kill-job)
2276 (tex-start-shell))
51b2c841
RS
2277 (tex-send-command tex-show-queue-command)
2278 (tex-display-shell))
869bff31 2279
2280(defun tex-bibtex-file ()
2281 "Run BibTeX on the current buffer's file."
2282 (interactive)
2283 (if (tex-shell-running)
2284 (tex-kill-job)
2285 (tex-start-shell))
e6b0b773
MR
2286 (let (shell-dirtrack-verbose
2287 (tex-out-file
869bff31 2288 (tex-append (file-name-nondirectory (buffer-file-name)) ""))
2289 (file-dir (file-name-directory (buffer-file-name))))
528415e7 2290 (tex-send-command tex-shell-cd-command file-dir)
51b2c841
RS
2291 (tex-send-command tex-bibtex-command tex-out-file))
2292 (tex-display-shell))
ea590e1c
SM
2293\f
2294;;;;
2295;;;; LaTeX indentation
2296;;;;
2297
2298(defvar tex-indent-allhanging t)
2299(defvar tex-indent-arg 4)
2300(defvar tex-indent-basic 2)
2301(defvar tex-indent-item tex-indent-basic)
2302(defvar tex-indent-item-re "\\\\\\(bib\\)?item\\>")
dd166d5f 2303(defvar latex-noindent-environments '("document"))
ea590e1c 2304
8a591d52
SM
2305(defvar tex-latex-indent-syntax-table
2306 (let ((st (make-syntax-table tex-mode-syntax-table)))
2307 (modify-syntax-entry ?$ "." st)
2308 (modify-syntax-entry ?\( "." st)
2309 (modify-syntax-entry ?\) "." st)
2310 st)
2311 "Syntax table used while computing indentation.")
ea590e1c
SM
2312
2313(defun latex-indent (&optional arg)
7afecb99
SM
2314 (if (and (eq (get-text-property (line-beginning-position) 'face)
2315 tex-verbatim-face))
a3d80d4a 2316 'noindent
7afecb99
SM
2317 (with-syntax-table tex-latex-indent-syntax-table
2318 ;; TODO: Rather than ignore $, we should try to be more clever about it.
2319 (let ((indent
2320 (save-excursion
2321 (beginning-of-line)
2322 (latex-find-indent))))
2323 (if (< indent 0) (setq indent 0))
2324 (if (<= (current-column) (current-indentation))
2325 (indent-line-to indent)
2326 (save-excursion (indent-line-to indent)))))))
ea590e1c
SM
2327
2328(defun latex-find-indent (&optional virtual)
2329 "Find the proper indentation of text after point.
2330VIRTUAL if non-nil indicates that we're only trying to find the indentation
2331 in order to determine the indentation of something else.
2332There might be text before point."
2333 (save-excursion
2334 (skip-chars-forward " \t")
2335 (or
7afecb99
SM
2336 ;; Stick the first line at column 0.
2337 (and (= (point-min) (line-beginning-position)) 0)
ea590e1c 2338 ;; Trust the current indentation, if such info is applicable.
7afecb99
SM
2339 (and virtual (save-excursion (skip-chars-backward " \t&") (bolp))
2340 (current-column))
2341 ;; Stick verbatim environments to the left margin.
2342 (and (looking-at "\\\\\\(begin\\|end\\) *{\\([^\n}]+\\)")
2343 (member (match-string 2) tex-verbatim-environments)
2344 0)
ea590e1c
SM
2345 ;; Put leading close-paren where the matching open brace would be.
2346 (and (eq (latex-syntax-after) ?\))
2347 (ignore-errors
2348 (save-excursion
2349 (latex-skip-close-parens)
2350 (latex-backward-sexp-1)
2351 (latex-find-indent 'virtual))))
2352 ;; Default (maybe an argument)
2353 (let ((pos (point))
ea590e1c
SM
2354 ;; Outdent \item if necessary.
2355 (indent (if (looking-at tex-indent-item-re) (- tex-indent-item) 0))
2356 up-list-pos)
2357 ;; Find the previous point which determines our current indentation.
2358 (condition-case err
2359 (progn
2360 (latex-backward-sexp-1)
2361 (while (> (current-column) (current-indentation))
2362 (latex-backward-sexp-1)))
2363 (scan-error
2364 (setq up-list-pos (nth 2 err))))
8908cf40
SM
2365 (cond
2366 ((= (point-min) pos) 0) ; We're really just indenting the first line.
2367 ((integerp up-list-pos)
2368 ;; Have to indent relative to the open-paren.
2369 (goto-char up-list-pos)
2370 (if (and (not tex-indent-allhanging)
dd166d5f
SM
2371 (save-excursion
2372 ;; Make sure we're an argument to a macro and
2373 ;; that the macro is at the beginning of a line.
2374 (condition-case nil
2375 (progn
2376 (while (eq (char-syntax (char-after)) ?\()
2377 (forward-sexp -1))
2378 (and (eq (char-syntax (char-after)) ?/)
2379 (progn (skip-chars-backward " \t&")
2380 (bolp))))
2381 (scan-error nil)))
8908cf40
SM
2382 (> pos (progn (latex-down-list)
2383 (forward-comment (point-max))
2384 (point))))
7afecb99 2385 ;; Align with the first element after the open-paren.
8908cf40
SM
2386 (current-column)
2387 ;; We're the first element after a hanging brace.
2388 (goto-char up-list-pos)
dd166d5f
SM
2389 (+ (if (and (looking-at "\\\\begin *{\\([^\n}]+\\)")
2390 (member (match-string 1)
2391 latex-noindent-environments))
2392 0 tex-indent-basic)
2393 indent (latex-find-indent 'virtual))))
8908cf40
SM
2394 ;; We're now at the "beginning" of a line.
2395 ((not (and (not virtual) (eq (char-after) ?\\)))
2396 ;; Nothing particular here: just keep the same indentation.
2397 (+ indent (current-column)))
2398 ;; We're now looking at a macro call.
dd166d5f
SM
2399 ((looking-at tex-indent-item-re)
2400 ;; Indenting relative to an item, have to re-add the outdenting.
8908cf40
SM
2401 (+ indent (current-column) tex-indent-item))
2402 (t
2403 (let ((col (current-column)))
dd166d5f
SM
2404 (if (or (not (eq (char-syntax (or (char-after pos) ?\ )) ?\())
2405 ;; Can't be an arg if there's an empty line inbetween.
2406 (save-excursion (re-search-forward "^[ \t]*$" pos t)))
8908cf40
SM
2407 ;; If the first char was not an open-paren, there's
2408 ;; a risk that this is really not an argument to the
2409 ;; macro at all.
21749f14
RS
2410 (+ indent col)
2411 (forward-sexp 1)
2412 (if (< (line-end-position)
2413 (save-excursion (forward-comment (point-max))
2414 (point)))
8908cf40
SM
2415 ;; we're indenting the first argument.
2416 (min (current-column) (+ tex-indent-arg col))
2417 (skip-syntax-forward " ")
2418 (current-column))))))))))
a3d80d4a
SM
2419;;; DocTeX support
2420
2421(defun doctex-font-lock-^^A ()
2422 (if (eq (char-after (line-beginning-position)) ?\%)
2423 (progn
2424 (put-text-property
2425 (1- (match-beginning 1)) (match-beginning 1)
2426 'syntax-table
2427 (if (= (1+ (line-beginning-position)) (match-beginning 1))
2428 ;; The `%' is a single-char comment, which Emacs
2429 ;; syntax-table can't deal with. We could turn it
2430 ;; into a non-comment, or use `\n%' or `%^' as the comment.
2431 ;; Instead, we include it in the ^^A comment.
2432 (eval-when-compile (string-to-syntax "< b"))
2433 (eval-when-compile (string-to-syntax ">"))))
2434 (let ((end (line-end-position)))
2435 (if (< end (point-max))
2436 (put-text-property
2437 end (1+ end)
2438 'syntax-table
2439 (eval-when-compile (string-to-syntax "> b")))))
2440 (eval-when-compile (string-to-syntax "< b")))))
2441
2442(defun doctex-font-lock-syntactic-face-function (state)
2443 ;; Mark DocTeX documentation, which is parsed as a style A comment
2444 ;; starting in column 0.
2445 (if (or (nth 3 state) (nth 7 state)
2446 (not (memq (char-before (nth 8 state))
2447 '(?\n nil))))
2448 ;; Anything else is just as for LaTeX.
2449 (tex-font-lock-syntactic-face-function state)
2450 font-lock-doc-face))
2451
2452(defvar doctex-font-lock-syntactic-keywords
2453 (append
2454 tex-font-lock-syntactic-keywords
2455 ;; For DocTeX comment-in-doc.
2456 `(("\\(\\^\\)\\^A" (1 (doctex-font-lock-^^A))))))
2457
2458(defvar doctex-font-lock-keywords
2459 (append tex-font-lock-keywords
2460 '(("^%<[^>]*>" (0 font-lock-preprocessor-face t)))))
2461
2462;;;###autoload
2463(define-derived-mode doctex-mode latex-mode "DocTeX"
2464 "Major mode to edit DocTeX files."
2465 (setq font-lock-defaults
2466 (cons (append (car font-lock-defaults) '(doctex-font-lock-keywords))
2467 (mapcar
2468 (lambda (x)
2469 (case (car-safe x)
2470 (font-lock-syntactic-keywords
2471 (cons (car x) 'doctex-font-lock-syntactic-keywords))
2472 (font-lock-syntactic-face-function
2473 (cons (car x) 'doctex-font-lock-syntactic-face-function))
2474 (t x)))
2475 (cdr font-lock-defaults)))))
528415e7
RS
2476
2477(run-hooks 'tex-mode-load-hook)
869bff31 2478
49116ac0
JB
2479(provide 'tex-mode)
2480
dd166d5f 2481;; arch-tag: c0a680b1-63aa-4547-84b9-4193c29c0080
d501f516 2482;;; tex-mode.el ends here