Merge branch 'master' into comment-cache
[emacs.git] / lisp / textmodes / tex-mode.el
blobba6d696de90e9d408a282d7aa28837a1338b6b06
1 ;;; tex-mode.el --- TeX, LaTeX, and SliTeX mode commands -*- lexical-binding:t -*-
3 ;; Copyright (C) 1985-1986, 1989, 1992, 1994-1999, 2001-2017 Free
4 ;; Software Foundation, Inc.
6 ;; Maintainer: emacs-devel@gnu.org
7 ;; Keywords: tex
9 ;; Contributions over the years by William F. Schelter, Dick King,
10 ;; Stephen Gildea, Michael Prange, Jacob Gore, and Edward M. Reingold.
12 ;; This file is part of GNU Emacs.
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
16 ;; the Free Software Foundation, either version 3 of the License, or
17 ;; (at your option) any later version.
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.
24 ;; You should have received a copy of the GNU General Public License
25 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
27 ;;; Commentary:
29 ;;; Code:
31 ;; Pacify the byte-compiler
32 (eval-when-compile
33 (require 'compare-w)
34 (require 'cl-lib)
35 (require 'skeleton))
37 (defvar font-lock-comment-face)
38 (defvar font-lock-doc-face)
40 (require 'shell)
41 (require 'compile)
43 (defgroup tex-file nil
44 "TeX files and directories."
45 :prefix "tex-"
46 :group 'tex)
48 (defgroup tex-run nil
49 "Running external commands from TeX mode."
50 :prefix "tex-"
51 :group 'tex)
53 (defgroup tex-view nil
54 "Viewing and printing TeX files."
55 :prefix "tex-"
56 :group 'tex)
58 ;;;###autoload
59 (defcustom tex-shell-file-name nil
60 "If non-nil, the shell file name to run in the subshell used to run TeX."
61 :type '(choice (const :tag "None" nil)
62 string)
63 :group 'tex-run)
65 ;;;###autoload
66 (defcustom tex-directory (purecopy ".")
67 "Directory in which temporary files are written.
68 You can make this `/tmp' if your TEXINPUTS has no relative directories in it
69 and you don't try to apply \\[tex-region] or \\[tex-buffer] when there are
70 `\\input' commands with relative directories."
71 :type 'directory
72 :group 'tex-file)
74 ;;;###autoload
75 (defcustom tex-first-line-header-regexp nil
76 "Regexp for matching a first line which `tex-region' should include.
77 If this is non-nil, it should be a regular expression string;
78 if it matches the first line of the file,
79 `tex-region' always includes the first line in the TeX run."
80 :type '(choice (const :tag "None" nil)
81 regexp)
82 :group 'tex-file)
84 ;;;###autoload
85 (defcustom tex-main-file nil
86 "The main TeX source file which includes this buffer's file.
87 The command `tex-file' runs TeX on the file specified by `tex-main-file'
88 if the variable is non-nil."
89 :type '(choice (const :tag "None" nil)
90 file)
91 :group 'tex-file)
93 ;;;###autoload
94 (defcustom tex-offer-save t
95 "If non-nil, ask about saving modified buffers before \\[tex-file] is run."
96 :type 'boolean
97 :group 'tex-file)
99 ;;;###autoload
100 (defcustom tex-run-command (purecopy "tex")
101 "Command used to run TeX subjob.
102 TeX Mode sets `tex-command' to this string.
103 See the documentation of that variable."
104 :type 'string
105 :group 'tex-run)
107 ;;;###autoload
108 (defcustom latex-run-command (purecopy "latex")
109 "Command used to run LaTeX subjob.
110 LaTeX Mode sets `tex-command' to this string.
111 See the documentation of that variable."
112 :type 'string
113 :group 'tex-run)
115 ;;;###autoload
116 (defcustom slitex-run-command (purecopy "slitex")
117 "Command used to run SliTeX subjob.
118 SliTeX Mode sets `tex-command' to this string.
119 See the documentation of that variable."
120 :type 'string
121 :group 'tex-run)
123 ;;;###autoload
124 (defcustom tex-start-options (purecopy "")
125 "TeX options to use when starting TeX.
126 These immediately precede the commands in `tex-start-commands'
127 and the input file name, with no separating space and are not shell-quoted.
128 If nil, TeX runs with no options. See the documentation of `tex-command'."
129 :type 'string
130 :group 'tex-run
131 :version "22.1")
133 ;;;###autoload
134 (defcustom tex-start-commands (purecopy "\\nonstopmode\\input")
135 "TeX commands to use when starting TeX.
136 They are shell-quoted and precede the input file name, with a separating space.
137 If nil, no commands are used. See the documentation of `tex-command'."
138 :type '(radio (const :tag "Interactive (nil)" nil)
139 (const :tag "Nonstop (\"\\nonstopmode\\input\")"
140 "\\nonstopmode\\input")
141 (string :tag "String at your choice"))
142 :group 'tex-run
143 :version "22.1")
145 (defvar latex-standard-block-names
146 '("abstract" "array" "center" "description"
147 "displaymath" "document" "enumerate" "eqnarray"
148 "eqnarray*" "equation" "figure" "figure*"
149 "flushleft" "flushright" "itemize" "letter"
150 "list" "minipage" "picture" "quotation"
151 "quote" "slide" "sloppypar" "tabbing"
152 "table" "table*" "tabular" "tabular*"
153 "thebibliography" "theindex*" "titlepage" "trivlist"
154 "verbatim" "verbatim*" "verse" "math")
155 "Standard LaTeX block names.")
157 ;;;###autoload
158 (defcustom latex-block-names nil
159 "User defined LaTeX block names.
160 Combined with `latex-standard-block-names' for minibuffer completion."
161 :type '(repeat string)
162 :group 'tex-run)
164 ;;;###autoload
165 (defcustom tex-bibtex-command (purecopy "bibtex")
166 "Command used by `tex-bibtex-file' to gather bibliographic data.
167 If this string contains an asterisk (`*'), that is replaced by the file name;
168 otherwise, the file name, preceded by blank, is added at the end."
169 :type 'string
170 :group 'tex-run)
172 ;;;###autoload
173 (defcustom tex-dvi-print-command (purecopy "lpr -d")
174 "Command used by \\[tex-print] to print a .dvi file.
175 If this string contains an asterisk (`*'), that is replaced by the file name;
176 otherwise, the file name, preceded by blank, is added at the end."
177 :type 'string
178 :group 'tex-view)
180 ;;;###autoload
181 (defcustom tex-alt-dvi-print-command (purecopy "lpr -d")
182 "Command used by \\[tex-print] with a prefix arg to print a .dvi file.
183 If this string contains an asterisk (`*'), that is replaced by the file name;
184 otherwise, the file name, preceded by blank, is added at the end.
186 If two printers are not enough of a choice, you can set the variable
187 `tex-alt-dvi-print-command' to an expression that asks what you want;
188 for example,
190 (setq tex-alt-dvi-print-command
191 \\='(format \"lpr -P%s\" (read-string \"Use printer: \")))
193 would tell \\[tex-print] with a prefix argument to ask you which printer to
194 use."
195 :type '(choice (string :tag "Command")
196 (sexp :tag "Expression"))
197 :group 'tex-view)
199 ;;;###autoload
200 (defcustom tex-dvi-view-command
201 `(cond
202 ((eq window-system 'x) ,(purecopy "xdvi"))
203 ((eq window-system 'w32) ,(purecopy "yap"))
204 (t ,(purecopy "dvi2tty * | cat -s")))
205 "Command used by \\[tex-view] to display a `.dvi' file.
206 If it is a string, that specifies the command directly.
207 If this string contains an asterisk (`*'), that is replaced by the file name;
208 otherwise, the file name, preceded by a space, is added at the end.
210 If the value is a form, it is evaluated to get the command to use."
211 :type '(choice (const nil) string sexp)
212 :group 'tex-view)
214 ;;;###autoload
215 (defcustom tex-show-queue-command (purecopy "lpq")
216 "Command used by \\[tex-show-print-queue] to show the print queue.
217 Should show the queue(s) that \\[tex-print] puts jobs on."
218 :type 'string
219 :group 'tex-view)
221 ;;;###autoload
222 (defcustom tex-default-mode 'latex-mode
223 "Mode to enter for a new file that might be either TeX or LaTeX.
224 This variable is used when it can't be determined whether the file
225 is plain TeX or LaTeX or what because the file contains no commands.
226 Normally set to either `plain-tex-mode' or `latex-mode'."
227 :type 'function
228 :group 'tex)
230 ;;;###autoload
231 (defcustom tex-open-quote (purecopy "``")
232 "String inserted by typing \\[tex-insert-quote] to open a quotation."
233 :type 'string
234 :options '("``" "\"<" "\"`" "<<" "«")
235 :group 'tex)
237 ;;;###autoload
238 (defcustom tex-close-quote (purecopy "''")
239 "String inserted by typing \\[tex-insert-quote] to close a quotation."
240 :type 'string
241 :options '("''" "\">" "\"'" ">>" "»")
242 :group 'tex)
244 (defcustom tex-fontify-script t
245 "If non-nil, fontify subscript and superscript strings."
246 :type 'boolean
247 :group 'tex
248 :version "23.1")
249 (put 'tex-fontify-script 'safe-local-variable 'booleanp)
251 (defcustom tex-font-script-display '(-0.2 0.2)
252 "How much to lower and raise subscript and superscript content.
253 This is a list of two floats. The first is negative and
254 specifies how much subscript is lowered, the second is positive
255 and specifies how much superscript is raised. Heights are
256 measured relative to that of the normal text."
257 :group 'tex
258 :type '(list (float :tag "Subscript")
259 (float :tag "Superscript"))
260 :version "23.1")
262 (defvar tex-last-temp-file nil
263 "Latest temporary file generated by \\[tex-region] and \\[tex-buffer].
264 Deleted when the \\[tex-region] or \\[tex-buffer] is next run, or when the
265 tex shell terminates.")
267 (defvar tex-command "tex"
268 "Command to run TeX.
269 If this string contains an asterisk \(`*'), that is replaced by the file name;
270 otherwise the value of `tex-start-options', the \(shell-quoted)
271 value of `tex-start-commands', and the file name are added at the end
272 with blanks as separators.
274 In TeX, LaTeX, and SliTeX Mode this variable becomes buffer local.")
276 (defvar tex-trailer nil
277 "String appended after the end of a region sent to TeX by \\[tex-region].")
279 (defvar tex-start-of-header nil
280 "Regular expression used by \\[tex-region] to find start of file's header.")
282 (defvar tex-end-of-header nil
283 "Regular expression used by \\[tex-region] to find end of file's header.")
285 (defvar tex-shell-cd-command "cd"
286 "Command to give to shell running TeX to change directory.
287 The value of `tex-directory' is appended to this, separated by a space.")
289 (defvar tex-zap-file nil
290 "Temporary file name used for text being sent as input to TeX.
291 Should be a simple file name with no extension or directory specification.")
293 (defvar tex-last-buffer-texed nil
294 "Buffer which was last TeXed.")
296 (defvar tex-print-file nil
297 "File name that \\[tex-print] prints.
298 Set by \\[tex-region], \\[tex-buffer], and \\[tex-file].")
300 (defvar tex-mode-syntax-table
301 (let ((st (make-syntax-table)))
302 (modify-syntax-entry ?% "<" st)
303 (modify-syntax-entry ?\n ">" st)
304 (modify-syntax-entry ?\f ">" st)
305 (modify-syntax-entry ?\C-@ "w" st)
306 (modify-syntax-entry ?' "w" st)
307 (modify-syntax-entry ?@ "_" st)
308 (modify-syntax-entry ?* "_" st)
309 (modify-syntax-entry ?\t " " st)
310 ;; ~ is printed by TeX as a space, but it's semantics in the syntax
311 ;; of TeX is not `whitespace' (i.e. it's just like \hspace{foo}).
312 (modify-syntax-entry ?~ "." st)
313 (modify-syntax-entry ?$ "$$" st)
314 (modify-syntax-entry ?\\ "/" st)
315 (modify-syntax-entry ?\" "." st)
316 (modify-syntax-entry ?& "." st)
317 (modify-syntax-entry ?_ "." st)
318 (modify-syntax-entry ?^ "." st)
320 "Syntax table used while in TeX mode.")
322 ;;;;
323 ;;;; Imenu support
324 ;;;;
326 (defcustom latex-imenu-indent-string ". "
327 "String to add repeated in front of nested sectional units for Imenu.
328 An alternative value is \" . \", if you use a font with a narrow period."
329 :type 'string
330 :group 'tex)
332 (defvar latex-section-alist
333 '(("part" . 0) ("chapter" . 1)
334 ("section" . 2) ("subsection" . 3)
335 ("subsubsection" . 4)
336 ("paragraph" . 5) ("subparagraph" . 6)))
338 (defvar latex-metasection-list
339 '("documentstyle" "documentclass"
340 "begin{document}" "end{document}"
341 "appendix" "frontmatter" "mainmatter" "backmatter"))
343 (defun latex-imenu-create-index ()
344 "Generate an alist for imenu from a LaTeX buffer."
345 (let ((section-regexp
346 (concat "\\\\" (regexp-opt (mapcar #'car latex-section-alist) t)
347 "\\*?[ \t]*{"))
348 (metasection-regexp
349 (concat "\\\\" (regexp-opt latex-metasection-list t)))
350 i0 menu case-fold-search)
351 (save-excursion
352 ;; Find the top-most level in this file but don't allow it to be
353 ;; any deeper than "section" (which is top-level in an article).
354 (goto-char (point-min))
355 (if (search-forward-regexp "\\\\part\\*?[ \t]*{" nil t)
356 (setq i0 0)
357 (if (search-forward-regexp "\\\\chapter\\*?[ \t]*{" nil t)
358 (setq i0 1)
359 (setq i0 2)))
361 ;; Look for chapters and sections.
362 (goto-char (point-min))
363 (while (search-forward-regexp section-regexp nil t)
364 (let ((start (match-beginning 0))
365 (here (point))
366 (i (cdr (assoc (buffer-substring-no-properties
367 (match-beginning 1)
368 (match-end 1))
369 latex-section-alist))))
370 (backward-char 1)
371 (condition-case nil
372 (progn
373 ;; Using sexps allows some use of matching {...} inside
374 ;; titles.
375 (forward-sexp 1)
376 (push (cons (concat (apply #'concat
377 (make-list
378 (max 0 (- i i0))
379 latex-imenu-indent-string))
380 (buffer-substring-no-properties
381 here (1- (point))))
382 start)
383 menu))
384 (error nil))))
386 ;; Look for included material.
387 (goto-char (point-min))
388 (while (search-forward-regexp
389 "\\\\\\(include\\|input\\|verbatiminput\\|bibliography\\)\
390 [ \t]*{\\([^}\n]+\\)}"
391 nil t)
392 (push (cons (concat "<<" (buffer-substring-no-properties
393 (match-beginning 2)
394 (match-end 2))
395 (if (= (char-after (match-beginning 1)) ?b)
396 ".bbl"
397 ".tex"))
398 (match-beginning 0))
399 menu))
401 ;; Look for \frontmatter, \mainmatter, \backmatter, and \appendix.
402 (goto-char (point-min))
403 (while (search-forward-regexp metasection-regexp nil t)
404 (push (cons "--" (match-beginning 0)) menu))
406 ;; Sort in increasing buffer position order.
407 (sort menu (function (lambda (a b) (< (cdr a) (cdr b))))))))
409 ;;;;
410 ;;;; Outline support
411 ;;;;
413 (defvar latex-outline-regexp
414 (concat "\\\\"
415 (regexp-opt (append latex-metasection-list
416 (mapcar #'car latex-section-alist))
417 t)))
419 (defun latex-outline-level ()
420 (if (looking-at latex-outline-regexp)
421 (1+ (or (cdr (assoc (match-string 1) latex-section-alist)) -1))
422 1000))
424 (defun tex-current-defun-name ()
425 "Return the name of the TeX section/paragraph/chapter at point, or nil."
426 (save-excursion
427 (when (re-search-backward
428 "\\\\\\(sub\\)*\\(section\\|paragraph\\|chapter\\)"
429 nil t)
430 (goto-char (match-beginning 0))
431 (buffer-substring-no-properties
432 (1+ (point)) ; without initial backslash
433 (line-end-position)))))
435 ;;;;
436 ;;;; Font-Lock support
437 ;;;;
439 ;(defvar tex-font-lock-keywords
440 ; ;; Regexps updated with help from Ulrik Dickow <dickow@nbi.dk>.
441 ; '(("\\\\\\(begin\\|end\\|newcommand\\){\\([a-zA-Z0-9\\*]+\\)}"
442 ; 2 font-lock-function-name-face)
443 ; ("\\\\\\(cite\\|label\\|pageref\\|ref\\){\\([^} \t\n]+\\)}"
444 ; 2 font-lock-constant-face)
445 ; ;; It seems a bit dubious to use `bold' and `italic' faces since we might
446 ; ;; not be able to display those fonts.
447 ; ("{\\\\bf\\([^}]+\\)}" 1 'bold keep)
448 ; ("{\\\\\\(em\\|it\\|sl\\)\\([^}]+\\)}" 2 'italic keep)
449 ; ("\\\\\\([a-zA-Z@]+\\|.\\)" . font-lock-keyword-face)
450 ; ("^[ \t\n]*\\\\def[\\\\@]\\(\\w+\\)" 1 font-lock-function-name-face keep))
451 ; ;; Rewritten and extended for LaTeX2e by Ulrik Dickow <dickow@nbi.dk>.
452 ; '(("\\\\\\(begin\\|end\\|newcommand\\){\\([a-zA-Z0-9\\*]+\\)}"
453 ; 2 font-lock-function-name-face)
454 ; ("\\\\\\(cite\\|label\\|pageref\\|ref\\){\\([^} \t\n]+\\)}"
455 ; 2 font-lock-constant-face)
456 ; ("^[ \t]*\\\\def\\\\\\(\\(\\w\\|@\\)+\\)" 1 font-lock-function-name-face)
457 ; "\\\\\\([a-zA-Z@]+\\|.\\)"
458 ; ;; It seems a bit dubious to use `bold' and `italic' faces since we might
459 ; ;; not be able to display those fonts.
460 ; ;; LaTeX2e: \emph{This is emphasized}.
461 ; ("\\\\emph{\\([^}]+\\)}" 1 'italic keep)
462 ; ;; LaTeX2e: \textbf{This is bold}, \textit{...}, \textsl{...}
463 ; ("\\\\text\\(\\(bf\\)\\|it\\|sl\\){\\([^}]+\\)}"
464 ; 3 (if (match-beginning 2) 'bold 'italic) keep)
465 ; ;; Old-style bf/em/it/sl. Stop at `\\' and un-escaped `&', for tables.
466 ; ("\\\\\\(\\(bf\\)\\|em\\|it\\|sl\\)\\>\\(\\([^}&\\]\\|\\\\[^\\]\\)+\\)"
467 ; 3 (if (match-beginning 2) 'bold 'italic) keep))
469 ;; Rewritten with the help of Alexandra Bac <abac@welcome.disi.unige.it>.
470 (defconst tex-font-lock-keywords-1
471 (eval-when-compile
472 (let* (;; Names of commands whose arg should be fontified as heading, etc.
473 (headings (regexp-opt
474 '("title" "begin" "end" "chapter" "part"
475 "section" "subsection" "subsubsection"
476 "paragraph" "subparagraph" "subsubparagraph"
477 "newcommand" "renewcommand" "providecommand"
478 "newenvironment" "renewenvironment"
479 "newtheorem" "renewtheorem")
481 (variables (regexp-opt
482 '("newcounter" "newcounter*" "setcounter" "addtocounter"
483 "setlength" "addtolength" "settowidth")
485 (includes (regexp-opt
486 '("input" "include" "includeonly" "bibliography"
487 "epsfig" "psfig" "epsf" "nofiles" "usepackage"
488 "documentstyle" "documentclass" "verbatiminput"
489 "includegraphics" "includegraphics*")
491 (verbish (regexp-opt '("url" "nolinkurl" "path") t))
492 ;; Miscellany.
493 (slash "\\\\")
494 (opt " *\\(\\[[^]]*\\] *\\)*")
495 ;; This would allow highlighting \newcommand\CMD but requires
496 ;; adapting subgroup numbers below.
497 ;; (arg "\\(?:{\\(\\(?:[^{}\\]+\\|\\\\.\\|{[^}]*}\\)+\\)\\|\\\\[a-z*]+\\)"))
498 (inbraces-re (lambda (re)
499 (concat "\\(?:[^{}\\]\\|\\\\.\\|" re "\\)")))
500 (arg (concat "{\\(" (funcall inbraces-re "{[^}]*}") "+\\)")))
501 `( ;; Highlight $$math$$ and $math$.
502 ;; This is done at the very beginning so as to interact with the other
503 ;; keywords in the same way as comments and strings.
504 (,(concat "\\$\\$?\\(?:[^$\\{}]\\|\\\\.\\|{"
505 (funcall inbraces-re
506 (concat "{" (funcall inbraces-re "{[^}]*}") "*}"))
507 "*}\\)+\\$?\\$")
508 (0 'tex-math))
509 ;; Heading args.
510 (,(concat slash headings "\\*?" opt arg)
511 ;; If ARG ends up matching too much (if the {} don't match, e.g.)
512 ;; jit-lock will do funny things: when updating the buffer
513 ;; the re-highlighting is only done locally so it will just
514 ;; match the local line, but defer-contextually will
515 ;; match more lines at a time, so ARG will end up matching
516 ;; a lot more, which might suddenly include a comment
517 ;; so you get things highlighted bold when you type them
518 ;; but they get turned back to normal a little while later
519 ;; because "there's already a face there".
520 ;; Using `keep' works around this un-intuitive behavior as well
521 ;; as improves the behavior in the very rare case where you do
522 ;; have a comment in ARG.
523 3 font-lock-function-name-face keep)
524 (,(concat slash "\\(?:provide\\|\\(?:re\\)?new\\)command\\** *\\(\\\\[A-Za-z@]+\\)")
525 1 font-lock-function-name-face keep)
526 ;; Variable args.
527 (,(concat slash variables " *" arg) 2 font-lock-variable-name-face)
528 ;; Include args.
529 (,(concat slash includes opt arg) 3 font-lock-builtin-face)
530 ;; Verbatim-like args.
531 (,(concat slash verbish opt arg) 3 'tex-verbatim)
532 ;; Definitions. I think.
533 ("^[ \t]*\\\\def *\\\\\\(\\(\\w\\|@\\)+\\)"
534 1 font-lock-function-name-face))))
535 "Subdued expressions to highlight in TeX modes.")
537 (defun tex-font-lock-append-prop (prop)
538 (unless (memq (get-text-property (match-end 1) 'face)
539 '(font-lock-comment-face tex-verbatim))
540 prop))
542 (defconst tex-font-lock-keywords-2
543 (append tex-font-lock-keywords-1
544 (eval-when-compile
545 (let* (;;
546 ;; Names of commands whose arg should be fontified with fonts.
547 (bold (regexp-opt '("textbf" "textsc" "textup"
548 "boldsymbol" "pmb")
550 (italic (regexp-opt '("textit" "textsl" "emph") t))
551 ;; FIXME: unimplemented yet.
552 ;; (type (regexp-opt '("texttt" "textmd" "textrm" "textsf") t))
554 ;; Names of commands whose arg should be fontified as a citation.
555 (citations (regexp-opt
556 '("label" "ref" "pageref" "vref" "eqref"
557 "cite" "nocite" "index" "glossary" "bibitem"
558 ;; natbib's two variants of \cite:
559 "citep" "citet"
560 ;; These are text, rather than citations.
561 ;; "caption" "footnote" "footnotemark" "footnotetext"
565 ;; Names of commands that should be fontified.
566 (specials-1 (regexp-opt '("\\" "\\*") t)) ;; "-"
567 (specials-2 (regexp-opt
568 '("linebreak" "nolinebreak" "pagebreak" "nopagebreak"
569 "newline" "newpage" "clearpage" "cleardoublepage"
570 "displaybreak" "allowdisplaybreaks"
571 "enlargethispage")
573 (general "\\([a-zA-Z@]+\\**\\|[^ \t\n]\\)")
575 ;; Miscellany.
576 (slash "\\\\")
577 (opt " *\\(\\[[^]]*\\] *\\)*")
578 (args "\\(\\(?:[^{}&\\]+\\|\\\\.\\|{[^}]*}\\)+\\)")
579 (arg "{\\(\\(?:[^{}\\]+\\|\\\\.\\|{[^}]*}\\)+\\)"))
580 (list
582 ;; Citation args.
583 (list (concat slash citations opt arg) 3 'font-lock-constant-face)
585 ;; Text between `` quotes ''.
586 (cons (concat (regexp-opt `("``" "\"<" "\"`" "<<" "«") t)
587 "[^'\">{]+" ;a bit pessimistic
588 (regexp-opt `("''" "\">" "\"'" ">>" "»") t))
589 'font-lock-string-face)
591 ;; Command names, special and general.
592 (cons (concat slash specials-1) 'font-lock-warning-face)
593 (list (concat "\\(" slash specials-2 "\\)\\([^a-zA-Z@]\\|\\'\\)")
594 1 'font-lock-warning-face)
595 (concat slash general)
597 ;; Font environments. It seems a bit dubious to use `bold' etc. faces
598 ;; since we might not be able to display those fonts.
599 (list (concat slash bold " *" arg) 2
600 '(tex-font-lock-append-prop 'bold) 'append)
601 (list (concat slash italic " *" arg) 2
602 '(tex-font-lock-append-prop 'italic) 'append)
603 ;; (list (concat slash type arg) 2 '(quote bold-italic) 'append)
605 ;; Old-style bf/em/it/sl. Stop at `\\' and un-escaped `&', for tables.
606 (list (concat "\\\\\\(em\\|it\\|sl\\)\\>" args)
607 2 '(tex-font-lock-append-prop 'italic) 'append)
608 ;; This is separate from the previous one because of cases like
609 ;; {\em foo {\bf bar} bla} where both match.
610 (list (concat "\\\\\\(bf\\(series\\)?\\)\\>" args)
611 3 '(tex-font-lock-append-prop 'bold) 'append)))))
612 "Gaudy expressions to highlight in TeX modes.")
614 (defun tex-font-lock-suscript (pos)
615 (unless (or (memq (get-text-property pos 'face)
616 '(font-lock-constant-face font-lock-builtin-face
617 font-lock-comment-face tex-verbatim))
618 ;; Check for backslash quoting
619 (let ((odd nil)
620 (pos pos))
621 (while (eq (char-before pos) ?\\)
622 (setq pos (1- pos) odd (not odd)))
623 odd))
624 (if (eq (char-after pos) ?_)
625 `(face subscript display (raise ,(car tex-font-script-display)))
626 `(face superscript display (raise ,(cadr tex-font-script-display))))))
628 (defun tex-font-lock-match-suscript (limit)
629 "Match subscript and superscript patterns up to LIMIT."
630 (when (and tex-fontify-script
631 (re-search-forward "[_^] *\\([^\n\\{}]\\|\
632 \\\\\\([a-zA-Z@]+\\|[^ \t\n]\\)\\|\\({\\)\\)" limit t))
633 (when (match-end 3)
634 (let ((beg (match-beginning 3))
635 (end (save-restriction
636 (narrow-to-region (point-min) limit)
637 (condition-case nil (scan-lists (point) 1 1) (error nil)))))
638 (store-match-data (if end
639 (list (match-beginning 0) end beg end)
640 (list beg beg beg beg)))))
643 (defconst tex-font-lock-keywords-3
644 (append tex-font-lock-keywords-2
645 '((tex-font-lock-match-suscript
646 (1 (tex-font-lock-suscript (match-beginning 0)) append))))
647 "Experimental expressions to highlight in TeX modes.")
649 (defconst tex-font-lock-keywords tex-font-lock-keywords-1
650 "Default expressions to highlight in TeX modes.")
652 (defvar tex-verbatim-environments
653 '("verbatim" "verbatim*"))
654 (put 'tex-verbatim-environments 'safe-local-variable
655 (lambda (x) (null (delq t (mapcar #'stringp x)))))
657 (eval-when-compile
658 (defconst tex-syntax-propertize-rules
659 (syntax-propertize-precompile-rules
660 ("\\\\verb\\**\\([^a-z@*]\\)"
661 (1 (prog1 "\""
662 (tex-font-lock-verb
663 (match-beginning 0) (char-after (match-beginning 1))))))))
665 (defconst latex-syntax-propertize-rules
666 (syntax-propertize-precompile-rules
667 tex-syntax-propertize-rules
668 ("\\\\\\(?:end\\|begin\\) *\\({[^\n{}]*}\\)"
669 (1 (ignore
670 (tex-env-mark (match-beginning 0)
671 (match-beginning 1) (match-end 1))))))))
673 (defun tex-env-mark (cmd start end)
674 (when (= cmd (line-beginning-position))
675 (let ((arg (buffer-substring-no-properties (1+ start) (1- end))))
676 (when (member arg tex-verbatim-environments)
677 (if (eq ?b (char-after (1+ cmd)))
678 ;; \begin
679 (put-text-property (line-end-position)
680 (line-beginning-position 2)
681 'syntax-table (string-to-syntax "< c"))
682 ;; In the case of an empty verbatim env, the \n after the \begin is
683 ;; the same as the \n before the \end. Lucky for us, the "> c"
684 ;; property associated to the \end will be placed afterwards, so it
685 ;; will override the "< c".
686 (put-text-property (1- cmd) cmd
687 'syntax-table (string-to-syntax "> c"))
688 ;; The text between \end{verbatim} and \n is ignored, so we'll treat
689 ;; it as a comment.
690 (put-text-property end (min (1+ end) (line-end-position))
691 'syntax-table (string-to-syntax "<"))))))
692 ;; Mark env args for possible electric pairing.
693 (unless (get-char-property (1+ start) 'text-clones) ;Already paired-up.
694 (put-text-property start end 'latex-env-pair t)))
696 (define-minor-mode latex-electric-env-pair-mode
697 "Toggle Latex Electric Env Pair mode.
698 With a prefix argument ARG, enable the mode if ARG is positive,
699 and disable it otherwise. If called from Lisp, enable it if ARG
700 is omitted or nil.
702 Latex Electric Env Pair mode is a buffer-local minor mode for use
703 with `latex-mode'. When enabled, typing a \\begin or \\end tag
704 automatically inserts its partner."
705 :lighter "/e"
706 (if latex-electric-env-pair-mode
707 (add-hook 'before-change-functions
708 #'latex-env-before-change nil 'local)
709 (remove-hook 'before-change-functions
710 #'latex-env-before-change 'local)))
712 (defun latex-env-before-change (start end)
713 (when (get-text-property start 'latex-env-pair)
714 (condition-case err
715 (with-silent-modifications
716 ;; Remove properties even if don't find a pair.
717 (remove-text-properties
718 (previous-single-property-change (1+ start) 'latex-env-pair)
719 (next-single-property-change start 'latex-env-pair)
720 '(latex-env-pair))
721 (unless (or (get-char-property start 'text-clones)
722 (get-char-property (1+ start) 'text-clones)
723 (save-excursion
724 (goto-char start)
725 (not (re-search-backward
726 "\\\\\\(?:end\\|begi\\(n\\)\\) *{"
727 (line-beginning-position) t))))
728 (let ((cmd-start (match-beginning 0))
729 (type (match-end 1)) ;nil for \end, else \begin.
730 (arg-start (1- (match-end 0))))
731 (save-excursion
732 (goto-char (match-end 0))
733 (when (and (looking-at "[^\n{}]*}")
734 (> (match-end 0) end))
735 (let ((arg-end (match-end 0)))
736 (if (null type) ;\end
737 (progn (goto-char arg-end)
738 (latex-forward-sexp -1)
739 (forward-word-strictly 1))
740 (goto-char cmd-start)
741 (latex-forward-sexp 1)
742 (let (forward-sexp-function) (backward-sexp)))
743 (when (looking-at
744 (regexp-quote (buffer-substring arg-start arg-end)))
745 (text-clone-create arg-start arg-end))))))))
746 (scan-error nil)
747 (error (message "Error in latex-env-before-change: %s" err)))))
749 (defun tex-font-lock-unfontify-region (beg end)
750 (font-lock-default-unfontify-region beg end)
751 (while (< beg end)
752 (let ((next (next-single-property-change beg 'display nil end))
753 (prop (get-text-property beg 'display)))
754 (if (and (eq (car-safe prop) 'raise)
755 (member (car-safe (cdr prop)) tex-font-script-display)
756 (null (cddr prop)))
757 (put-text-property beg next 'display nil))
758 (setq beg next))))
760 (defcustom tex-suscript-height-ratio 0.8
761 "Ratio of subscript/superscript height to that of the preceding text.
762 In nested subscript/superscript, this factor is applied repeatedly,
763 subject to the limit set by `tex-suscript-height-minimum'."
764 :type 'float
765 :group 'tex
766 :version "23.1")
768 (defcustom tex-suscript-height-minimum 0.0
769 "Integer or float limiting the minimum size of subscript/superscript text.
770 An integer is an absolute height in units of 1/10 point, a float
771 is a height relative to that of the default font. Zero means no minimum."
772 :type '(choice (integer :tag "Integer height in 1/10 point units")
773 (float :tag "Fraction of default font height"))
774 :group 'tex
775 :version "23.1")
777 (defun tex-suscript-height (height)
778 "Return the integer height of subscript/superscript font in 1/10 points.
779 Not smaller than the value set by `tex-suscript-height-minimum'."
780 (ceiling (max (if (integerp tex-suscript-height-minimum)
781 tex-suscript-height-minimum
782 ;; For bootstrapping.
783 (condition-case nil
784 (* tex-suscript-height-minimum
785 (face-attribute 'default :height))
786 (error 0)))
787 ;; NB assumes height is integer.
788 (* height tex-suscript-height-ratio))))
790 (defface superscript
791 '((t :height tex-suscript-height)) ;; :raise 0.2
792 "Face used for superscripts."
793 :group 'tex)
794 (defface subscript
795 '((t :height tex-suscript-height)) ;; :raise -0.2
796 "Face used for subscripts."
797 :group 'tex)
799 (defface tex-math
800 '((t :inherit font-lock-string-face))
801 "Face used to highlight TeX math expressions."
802 :group 'tex)
804 (defface tex-verbatim
805 '((t :inherit fixed-pitch-serif))
806 "Face used to highlight TeX verbatim environments."
807 :group 'tex)
809 (defun tex-font-lock-verb (start delim)
810 "Place syntax table properties on the \\verb construct.
811 START is the position of the \\ and DELIM is the delimiter char."
812 ;; Do nothing if the \verb construct is itself inside a comment or
813 ;; verbatim env.
814 (unless (nth 8 (save-excursion (syntax-ppss start)))
815 ;; Let's find the end and mark it.
816 (let ((afterdelim (point)))
817 (skip-chars-forward (string ?^ delim) (line-end-position))
818 (if (eolp)
819 ;; "LaTeX Error: \verb ended by end of line."
820 ;; Remove the syntax-table property we've just put on the
821 ;; start-delimiter, so it doesn't spill over subsequent lines.
822 (put-text-property (1- afterdelim) afterdelim
823 'syntax-table nil)
824 (when (eq (char-syntax (preceding-char)) ?/)
825 (put-text-property (1- (point)) (point)
826 'syntax-table (string-to-syntax ".")))
827 (put-text-property (point) (1+ (point))
828 'syntax-table (string-to-syntax "\""))))))
830 ;; Use string syntax but math face for $...$.
831 (defun tex-font-lock-syntactic-face-function (state)
832 (let ((char (nth 3 state)))
833 (cond
834 ((not char)
835 (if (eq 2 (nth 7 state)) 'tex-verbatim font-lock-comment-face))
836 ((eq char ?$) 'tex-math)
837 ;; A \verb element.
838 (t 'tex-verbatim))))
841 (defun tex-define-common-keys (keymap)
842 "Define the keys that we want defined both in TeX mode and in the TeX shell."
843 (define-key keymap "\C-c\C-k" 'tex-kill-job)
844 (define-key keymap "\C-c\C-l" 'tex-recenter-output-buffer)
845 (define-key keymap "\C-c\C-q" 'tex-show-print-queue)
846 (define-key keymap "\C-c\C-p" 'tex-print)
847 (define-key keymap "\C-c\C-v" 'tex-view)
849 (define-key keymap [menu-bar tex] (cons "TeX" (make-sparse-keymap "TeX")))
851 (define-key keymap [menu-bar tex tex-kill-job]
852 '(menu-item "Tex Kill" tex-kill-job :enable (tex-shell-running)))
853 (define-key keymap [menu-bar tex tex-recenter-output-buffer]
854 '(menu-item "Tex Recenter" tex-recenter-output-buffer
855 :enable (get-buffer "*tex-shell*")))
856 (define-key keymap [menu-bar tex tex-show-print-queue]
857 '("Show Print Queue" . tex-show-print-queue))
858 (define-key keymap [menu-bar tex tex-alt-print]
859 '(menu-item "Tex Print (alt printer)" tex-alt-print
860 :enable (stringp tex-print-file)))
861 (define-key keymap [menu-bar tex tex-print]
862 '(menu-item "Tex Print" tex-print :enable (stringp tex-print-file)))
863 (define-key keymap [menu-bar tex tex-view]
864 '(menu-item "Tex View" tex-view :enable (stringp tex-print-file))))
866 (defvar tex-mode-map
867 (let ((map (make-sparse-keymap)))
868 (set-keymap-parent map text-mode-map)
869 (tex-define-common-keys map)
870 (define-key map "\"" 'tex-insert-quote)
871 (define-key map "\n" 'tex-handle-newline)
872 (define-key map "\M-\r" 'latex-insert-item)
873 (define-key map "\C-c}" 'up-list)
874 (define-key map "\C-c{" 'tex-insert-braces)
875 (define-key map "\C-c\C-r" 'tex-region)
876 (define-key map "\C-c\C-b" 'tex-buffer)
877 (define-key map "\C-c\C-f" 'tex-file)
878 (define-key map "\C-c\C-c" 'tex-compile)
879 (define-key map "\C-c\C-i" 'tex-bibtex-file)
880 (define-key map "\C-c\C-o" 'latex-insert-block)
882 ;; Redundant keybindings, for consistency with SGML mode.
883 (define-key map "\C-c\C-t" 'latex-insert-block)
884 (define-key map "\C-c]" 'latex-close-block)
885 (define-key map "\C-c/" 'latex-close-block)
887 (define-key map "\C-c\C-e" 'latex-close-block)
888 (define-key map "\C-c\C-u" 'tex-goto-last-unclosed-latex-block)
889 (define-key map "\C-c\C-m" 'tex-feed-input)
890 (define-key map [(control return)] 'tex-feed-input)
891 (define-key map [menu-bar tex tex-bibtex-file]
892 '("BibTeX File" . tex-bibtex-file))
893 (define-key map [menu-bar tex tex-validate-region]
894 '(menu-item "Validate Region" tex-validate-region :enable mark-active))
895 (define-key map [menu-bar tex tex-validate-buffer]
896 '("Validate Buffer" . tex-validate-buffer))
897 (define-key map [menu-bar tex tex-region]
898 '(menu-item "TeX Region" tex-region :enable mark-active))
899 (define-key map [menu-bar tex tex-buffer]
900 '("TeX Buffer" . tex-buffer))
901 (define-key map [menu-bar tex tex-file] '("TeX File" . tex-file))
902 map)
903 "Keymap shared by TeX modes.")
905 (defvar latex-mode-map
906 (let ((map (make-sparse-keymap)))
907 (set-keymap-parent map tex-mode-map)
908 (define-key map "\C-c\C-s" 'latex-split-block)
909 map)
910 "Keymap for `latex-mode'. See also `tex-mode-map'.")
912 (defvar plain-tex-mode-map
913 (let ((map (make-sparse-keymap)))
914 (set-keymap-parent map tex-mode-map)
915 map)
916 "Keymap for `plain-tex-mode'. See also `tex-mode-map'.")
918 (defvar tex-shell-map
919 (let ((m (make-sparse-keymap)))
920 (set-keymap-parent m shell-mode-map)
921 (tex-define-common-keys m)
923 "Keymap for the TeX shell.
924 Inherits `shell-mode-map' with a few additions.")
926 (defvar tex-face-alist
927 '((bold . "{\\bf ")
928 (italic . "{\\it ")
929 (bold-italic . "{\\bi ") ; hypothetical
930 (underline . "\\underline{")
931 (default . "{\\rm "))
932 "Alist of face and TeX font name for facemenu.")
934 (defvar tex-latex-face-alist
935 `((italic . "{\\em ")
936 ,@tex-face-alist)
937 "Alist of face and LaTeX font name for facemenu.")
939 (defun tex-facemenu-add-face-function (face _end)
940 (or (cdr (assq face tex-face-alist))
941 (or (and (consp face)
942 (consp (car face))
943 (null (cdr face))
944 (eq major-mode 'latex-mode)
945 ;; This actually requires the `color' LaTeX package.
946 (cond ((eq (caar face) :foreground)
947 (format "{\\color{%s} " (cadr (car face))))
948 ((eq (caar face) :background)
949 (format "\\colorbox{%s}{" (cadr (car face))))))
950 (error "Face %s not configured for %s mode" face mode-name))))
952 ;; This would be a lot simpler if we just used a regexp search,
953 ;; but then it would be too slow.
954 (defun tex-guess-mode ()
955 (let ((mode tex-default-mode) slash comment)
956 (save-excursion
957 (goto-char (point-min))
958 (while (and (setq slash (search-forward "\\" nil t))
959 (setq comment (let ((search-end (point)))
960 (save-excursion
961 (beginning-of-line)
962 (search-forward "%" search-end t))))))
963 (when (and slash (not comment))
964 (setq mode
965 (if (looking-at
966 (eval-when-compile
967 (concat
968 (regexp-opt '("documentstyle" "documentclass"
969 "begin" "subsection" "section"
970 "part" "chapter" "newcommand"
971 "renewcommand" "RequirePackage") 'words)
972 "\\|NeedsTeXFormat{LaTeX")))
973 (if (and (looking-at
974 "document\\(style\\|class\\)\\(\\[.*\\]\\)?{slides}")
975 ;; SliTeX is almost never used any more nowadays.
976 (tex-executable-exists-p slitex-run-command))
977 'slitex-mode
978 'latex-mode)
979 'plain-tex-mode))))
980 (funcall mode)))
982 ;; `tex-mode' plays two roles: it's the parent of several sub-modes
983 ;; but it's also the function that chooses between those submodes.
984 ;; To tell the difference between those two cases where the function
985 ;; might be called, we check `delay-mode-hooks'.
986 (define-derived-mode tex-mode text-mode "generic-TeX"
987 (tex-common-initialization))
988 ;; We now move the function and define it again. This gives a warning
989 ;; in the byte-compiler :-( but it's difficult to avoid because
990 ;; `define-derived-mode' will necessarily define the function once
991 ;; and we need to define it a second time for `autoload' to get the
992 ;; proper docstring.
993 (defalias 'tex-mode-internal (symbol-function 'tex-mode))
995 ;; Suppress the byte-compiler warning about multiple definitions.
996 ;; This is a) ugly, and b) cheating, but this was the last
997 ;; remaining warning from byte-compiling all of Emacs...
998 (eval-when-compile
999 (setq byte-compile-function-environment
1000 (delq (assq 'tex-mode byte-compile-function-environment)
1001 byte-compile-function-environment)))
1003 ;;;###autoload
1004 (defun tex-mode ()
1005 "Major mode for editing files of input for TeX, LaTeX, or SliTeX.
1006 Tries to determine (by looking at the beginning of the file) whether
1007 this file is for plain TeX, LaTeX, or SliTeX and calls `plain-tex-mode',
1008 `latex-mode', or `slitex-mode', respectively. If it cannot be determined,
1009 such as if there are no commands in the file, the value of `tex-default-mode'
1010 says which mode to use."
1011 (interactive)
1012 (if delay-mode-hooks
1013 ;; We're called from one of the children already.
1014 (tex-mode-internal)
1015 (tex-guess-mode)))
1017 ;; The following three autoloaded aliases appear to conflict with
1018 ;; AUCTeX. However, even though AUCTeX uses the mixed case variants
1019 ;; for all mode relevant variables and hooks, the invocation function
1020 ;; and setting of `major-mode' themselves need to be lowercase for
1021 ;; AUCTeX to provide a fully functional user-level replacement. So
1022 ;; these aliases should remain as they are, in particular since AUCTeX
1023 ;; users are likely to use them.
1025 ;;;###autoload
1026 (defalias 'TeX-mode 'tex-mode)
1027 ;;;###autoload
1028 (defalias 'plain-TeX-mode 'plain-tex-mode)
1029 ;;;###autoload
1030 (defalias 'LaTeX-mode 'latex-mode)
1032 ;;;###autoload
1033 (define-derived-mode plain-tex-mode tex-mode "TeX"
1034 "Major mode for editing files of input for plain TeX.
1035 Makes $ and } display the characters they match.
1036 Makes \" insert \\=`\\=` when it seems to be the beginning of a quotation,
1037 and \\='\\=' when it appears to be the end; it inserts \" only after a \\.
1039 Use \\[tex-region] to run TeX on the current region, plus a \"header\"
1040 copied from the top of the file (containing macro definitions, etc.),
1041 running TeX under a special subshell. \\[tex-buffer] does the whole buffer.
1042 \\[tex-file] saves the buffer and then processes the file.
1043 \\[tex-print] prints the .dvi file made by any of these.
1044 \\[tex-view] previews the .dvi file made by any of these.
1045 \\[tex-bibtex-file] runs bibtex on the file of the current buffer.
1047 Use \\[tex-validate-buffer] to check buffer for paragraphs containing
1048 mismatched $'s or braces.
1050 Special commands:
1051 \\{plain-tex-mode-map}
1053 Mode variables:
1054 tex-run-command
1055 Command string used by \\[tex-region] or \\[tex-buffer].
1056 tex-directory
1057 Directory in which to create temporary files for TeX jobs
1058 run by \\[tex-region] or \\[tex-buffer].
1059 tex-dvi-print-command
1060 Command string used by \\[tex-print] to print a .dvi file.
1061 tex-alt-dvi-print-command
1062 Alternative command string used by \\[tex-print] (when given a prefix
1063 argument) to print a .dvi file.
1064 tex-dvi-view-command
1065 Command string used by \\[tex-view] to preview a .dvi file.
1066 tex-show-queue-command
1067 Command string used by \\[tex-show-print-queue] to show the print
1068 queue that \\[tex-print] put your job on.
1070 Entering Plain-tex mode runs the hook `text-mode-hook', then the hook
1071 `tex-mode-hook', and finally the hook `plain-tex-mode-hook'. When the
1072 special subshell is initiated, the hook `tex-shell-hook' is run."
1073 (setq-local tex-command tex-run-command)
1074 (setq-local tex-start-of-header "%\\*\\*start of header")
1075 (setq-local tex-end-of-header "%\\*\\*end of header")
1076 (setq-local tex-trailer "\\bye\n"))
1078 ;;;###autoload
1079 (define-derived-mode latex-mode tex-mode "LaTeX"
1080 "Major mode for editing files of input for LaTeX.
1081 Makes $ and } display the characters they match.
1082 Makes \" insert \\=`\\=` when it seems to be the beginning of a quotation,
1083 and \\='\\=' when it appears to be the end; it inserts \" only after a \\.
1085 Use \\[tex-region] to run LaTeX on the current region, plus the preamble
1086 copied from the top of the file (containing \\documentstyle, etc.),
1087 running LaTeX under a special subshell. \\[tex-buffer] does the whole buffer.
1088 \\[tex-file] saves the buffer and then processes the file.
1089 \\[tex-print] prints the .dvi file made by any of these.
1090 \\[tex-view] previews the .dvi file made by any of these.
1091 \\[tex-bibtex-file] runs bibtex on the file of the current buffer.
1093 Use \\[tex-validate-buffer] to check buffer for paragraphs containing
1094 mismatched $'s or braces.
1096 Special commands:
1097 \\{latex-mode-map}
1099 Mode variables:
1100 latex-run-command
1101 Command string used by \\[tex-region] or \\[tex-buffer].
1102 tex-directory
1103 Directory in which to create temporary files for LaTeX jobs
1104 run by \\[tex-region] or \\[tex-buffer].
1105 tex-dvi-print-command
1106 Command string used by \\[tex-print] to print a .dvi file.
1107 tex-alt-dvi-print-command
1108 Alternative command string used by \\[tex-print] (when given a prefix
1109 argument) to print a .dvi file.
1110 tex-dvi-view-command
1111 Command string used by \\[tex-view] to preview a .dvi file.
1112 tex-show-queue-command
1113 Command string used by \\[tex-show-print-queue] to show the print
1114 queue that \\[tex-print] put your job on.
1116 Entering Latex mode runs the hook `text-mode-hook', then
1117 `tex-mode-hook', and finally `latex-mode-hook'. When the special
1118 subshell is initiated, `tex-shell-hook' is run."
1119 (setq-local tex-command latex-run-command)
1120 (setq-local tex-start-of-header "\\\\document\\(style\\|class\\)")
1121 (setq-local tex-end-of-header "\\\\begin\\s-*{document}")
1122 (setq-local tex-trailer "\\end{document}\n")
1123 ;; A line containing just $$ is treated as a paragraph separator.
1124 ;; A line starting with $$ starts a paragraph,
1125 ;; but does not separate paragraphs if it has more stuff on it.
1126 (setq paragraph-start
1127 (concat "[ \t]*\\(\\$\\$\\|"
1128 "\\\\[][]\\|"
1129 "\\\\" (regexp-opt (append
1130 (mapcar #'car latex-section-alist)
1131 '("begin" "label" "end"
1132 "item" "bibitem" "newline" "noindent"
1133 "newpage" "footnote" "marginpar"
1134 "parbox" "caption"))
1136 "\\>\\|\\\\[a-z]*" (regexp-opt '("space" "skip" "page") t)
1137 "\\>\\)"))
1138 (setq paragraph-separate
1139 (concat "[\f%]\\|[ \t]*\\($\\|"
1140 "\\\\[][]\\|"
1141 "\\\\" (regexp-opt (append
1142 (mapcar #'car latex-section-alist)
1143 '("begin" "label" "end" ))
1145 "\\>\\|\\\\\\(" (regexp-opt '("item" "bibitem" "newline"
1146 "noindent" "newpage" "footnote"
1147 "marginpar" "parbox" "caption"))
1148 "\\|\\$\\$\\|[a-z]*\\(space\\|skip\\|page[a-z]*\\)"
1149 "\\>\\)[ \t]*\\($\\|%\\)\\)"))
1150 (setq-local imenu-create-index-function #'latex-imenu-create-index)
1151 (setq-local tex-face-alist tex-latex-face-alist)
1152 (add-hook 'fill-nobreak-predicate #'latex-fill-nobreak-predicate nil t)
1153 (setq-local indent-line-function #'latex-indent)
1154 (setq-local fill-indent-according-to-mode t)
1155 (add-hook 'completion-at-point-functions
1156 #'latex-complete-data nil 'local)
1157 (setq-local outline-regexp latex-outline-regexp)
1158 (setq-local outline-level #'latex-outline-level)
1159 (setq-local forward-sexp-function #'latex-forward-sexp)
1160 (setq-local skeleton-end-hook nil))
1162 ;;;###autoload
1163 (define-derived-mode slitex-mode latex-mode "SliTeX"
1164 "Major mode for editing files of input for SliTeX.
1165 Makes $ and } display the characters they match.
1166 Makes \" insert \\=`\\=` when it seems to be the beginning of a quotation,
1167 and \\='\\=' when it appears to be the end; it inserts \" only after a \\.
1169 Use \\[tex-region] to run SliTeX on the current region, plus the preamble
1170 copied from the top of the file (containing \\documentstyle, etc.),
1171 running SliTeX under a special subshell. \\[tex-buffer] does the whole buffer.
1172 \\[tex-file] saves the buffer and then processes the file.
1173 \\[tex-print] prints the .dvi file made by any of these.
1174 \\[tex-view] previews the .dvi file made by any of these.
1175 \\[tex-bibtex-file] runs bibtex on the file of the current buffer.
1177 Use \\[tex-validate-buffer] to check buffer for paragraphs containing
1178 mismatched $'s or braces.
1180 Special commands:
1181 \\{slitex-mode-map}
1183 Mode variables:
1184 slitex-run-command
1185 Command string used by \\[tex-region] or \\[tex-buffer].
1186 tex-directory
1187 Directory in which to create temporary files for SliTeX jobs
1188 run by \\[tex-region] or \\[tex-buffer].
1189 tex-dvi-print-command
1190 Command string used by \\[tex-print] to print a .dvi file.
1191 tex-alt-dvi-print-command
1192 Alternative command string used by \\[tex-print] (when given a prefix
1193 argument) to print a .dvi file.
1194 tex-dvi-view-command
1195 Command string used by \\[tex-view] to preview a .dvi file.
1196 tex-show-queue-command
1197 Command string used by \\[tex-show-print-queue] to show the print
1198 queue that \\[tex-print] put your job on.
1200 Entering SliTeX mode runs the hook `text-mode-hook', then the hook
1201 `tex-mode-hook', then the hook `latex-mode-hook', and finally the hook
1202 `slitex-mode-hook'. When the special subshell is initiated, the hook
1203 `tex-shell-hook' is run."
1204 (setq tex-command slitex-run-command)
1205 (setq tex-start-of-header "\\\\documentstyle{slides}\\|\\\\documentclass{slides}"))
1207 (defvar tildify-space-string)
1208 (defvar tildify-foreach-region-function)
1209 (declare-function tildify-foreach-ignore-environments
1210 "tildify" (pairs callback _beg end))
1211 (defvar tex--prettify-symbols-alist)
1213 (defun tex-common-initialization ()
1214 ;; Regexp isearch should accept newline and formfeed as whitespace.
1215 (setq-local search-whitespace-regexp "[ \t\r\n\f]+")
1216 ;; Use tilde as hard-space character in tildify package.
1217 (setq-local tildify-space-string "~")
1218 ;; FIXME: Use the fact that we're parsing the document already
1219 ;; rather than using regex-based filtering.
1220 (setq-local tildify-foreach-region-function
1221 (apply-partially
1222 #'tildify-foreach-ignore-environments
1223 `(("\\\\\\\\" . "") ; do not remove this
1224 (,(eval-when-compile
1225 (concat "\\\\begin{\\("
1226 (regexp-opt '("verbatim" "math" "displaymath"
1227 "equation" "eqnarray" "eqnarray*"))
1228 "\\)}"))
1229 . ("\\\\end{" 1 "}"))
1230 ("\\\\verb\\*?\\(.\\)" . (1))
1231 ("\\$\\$?" . (0))
1232 ("\\\\(" . "\\\\)")
1233 ("\\\\[[]" . "\\\\[]]")
1234 ("\\\\[a-zA-Z]+\\( +\\|{}\\)[a-zA-Z]*" . "")
1235 ("%" . "$"))))
1236 ;; A line containing just $$ is treated as a paragraph separator.
1237 (setq-local paragraph-start "[ \t]*$\\|[\f\\\\%]\\|[ \t]*\\$\\$")
1238 ;; A line starting with $$ starts a paragraph,
1239 ;; but does not separate paragraphs if it has more stuff on it.
1240 (setq-local paragraph-separate "[ \t]*$\\|[\f\\\\%]\\|[ \t]*\\$\\$[ \t]*$")
1241 (setq-local add-log-current-defun-function #'tex-current-defun-name)
1242 (setq-local comment-start "%")
1243 (setq-local comment-add 1)
1244 (setq-local comment-start-skip
1245 "\\(\\(^\\|[^\\\n]\\)\\(\\\\\\\\\\)*\\)\\(%+ *\\)")
1246 (setq-local parse-sexp-ignore-comments t)
1247 (setq-local compare-windows-whitespace 'tex-categorize-whitespace)
1248 (setq-local facemenu-add-face-function 'tex-facemenu-add-face-function)
1249 (setq-local facemenu-end-add-face "}")
1250 (setq-local facemenu-remove-face-function t)
1251 (setq-local font-lock-defaults
1252 '((tex-font-lock-keywords tex-font-lock-keywords-1
1253 tex-font-lock-keywords-2 tex-font-lock-keywords-3)
1254 nil nil nil nil
1255 ;; Who ever uses that anyway ???
1256 (font-lock-mark-block-function . mark-paragraph)
1257 (font-lock-syntactic-face-function
1258 . tex-font-lock-syntactic-face-function)
1259 (font-lock-unfontify-region-function
1260 . tex-font-lock-unfontify-region)))
1261 (setq-local prettify-symbols-alist tex--prettify-symbols-alist)
1262 (add-function :override (local 'prettify-symbols-compose-predicate)
1263 #'tex--prettify-symbols-compose-p)
1264 (setq-local syntax-propertize-function
1265 (syntax-propertize-rules latex-syntax-propertize-rules))
1266 ;; TABs in verbatim environments don't do what you think.
1267 (setq-local indent-tabs-mode nil)
1268 ;; Other vars that should be buffer-local.
1269 (make-local-variable 'tex-command)
1270 (make-local-variable 'tex-start-of-header)
1271 (make-local-variable 'tex-end-of-header)
1272 (make-local-variable 'tex-trailer))
1274 (defun tex-categorize-whitespace (backward-limit)
1275 ;; compare-windows-whitespace is set to this.
1276 ;; This is basically a finite-state machine.
1277 ;; Returns a symbol telling how TeX would treat
1278 ;; the whitespace we are looking at: null, space, or par.
1279 (let ((category 'null)
1280 (not-finished t))
1281 (skip-chars-backward " \t\n\f" backward-limit)
1282 (while not-finished
1283 (cond ((looking-at "[ \t]+")
1284 (goto-char (match-end 0))
1285 (if (eq category 'null)
1286 (setq category 'space)))
1287 ((looking-at "\n")
1288 (cond ((eq category 'newline)
1289 (setq category 'par)
1290 (setq not-finished nil))
1292 (setq category 'newline) ;a strictly internal state
1293 (goto-char (match-end 0)))))
1294 ((looking-at "\f+")
1295 (setq category 'par)
1296 (setq not-finished nil))
1298 (setq not-finished nil))))
1299 (skip-chars-forward " \t\n\f")
1300 (if (eq category 'newline)
1301 'space ;TeX doesn't distinguish
1302 category)))
1304 (defun tex-insert-quote (arg)
1305 "Insert the appropriate quote marks for TeX.
1306 Inserts the value of `tex-open-quote' (normally \\=`\\=`) or `tex-close-quote'
1307 \(normally \\='\\=') depending on the context. With prefix argument, always
1308 inserts \" characters."
1309 (interactive "*P")
1310 ;; Discover if we'll be inserting normal double quotes.
1312 (if (or arg (memq (char-syntax (preceding-char)) '(?/ ?\\))
1313 (eq (get-text-property (point) 'face) 'tex-verbatim)
1314 (nth 4 (syntax-ppss)) ; non-nil if point is in a TeX comment
1315 ;; Discover if a preceding occurrence of `tex-open-quote'
1316 ;; should be morphed to a normal double quote.
1318 (and (>= (point) (+ (point-min) (length tex-open-quote)))
1319 (save-excursion
1320 (backward-char (length tex-open-quote))
1321 (when (or (looking-at (regexp-quote tex-open-quote))
1322 (looking-at (regexp-quote tex-close-quote)))
1323 (delete-char (length tex-open-quote))
1324 (when (looking-at (regexp-quote tex-close-quote))
1325 (delete-char (length tex-close-quote)))
1326 t))))
1327 ;; Insert the normal quote (eventually letting
1328 ;; `electric-pair-mode' do its thing).
1330 (self-insert-command (prefix-numeric-value arg))
1331 ;; We'll be inserting fancy TeX quotes, but consider and imitate
1332 ;; `electric-pair-mode''s two behaviors: pair-insertion and
1333 ;; region wrapping.
1335 (if (and electric-pair-mode (use-region-p))
1336 (let* ((saved (point-marker)))
1337 (goto-char (mark))
1338 (insert (if (> saved (mark)) tex-open-quote tex-close-quote))
1339 (goto-char saved)
1340 (insert (if (> saved (mark)) tex-close-quote tex-open-quote)))
1341 (if (or (memq (char-syntax (preceding-char)) '(?\( ?> ?\s))
1342 (memq (preceding-char) '(?~ ?')))
1343 ;; We're in an "opening" context
1345 (if electric-pair-mode
1346 (if (looking-at (regexp-quote tex-close-quote))
1347 (forward-char (length tex-close-quote))
1348 (insert tex-open-quote)
1349 (insert tex-close-quote)
1350 (backward-char (length tex-close-quote)))
1351 (insert tex-open-quote))
1352 ;; We're in a "closing" context.
1354 (if (looking-at (regexp-quote tex-close-quote))
1355 (forward-char (length tex-close-quote))
1356 (insert tex-close-quote))))))
1358 (defun tex-validate-buffer ()
1359 "Check current buffer for paragraphs containing mismatched braces or $s.
1360 Their positions are recorded in the buffer `*Occur*'.
1361 To find a particular invalidity from `*Occur*', switch to that buffer
1362 and type C-c C-c or click with mouse-2
1363 on the line for the invalidity you want to see."
1364 (interactive)
1365 (let ((buffer (current-buffer))
1366 (prevpos (point-min))
1367 (linenum nil)
1368 (num-matches 0))
1369 (with-output-to-temp-buffer "*Occur*"
1370 (princ "Mismatches:\n")
1371 (with-current-buffer standard-output
1372 (occur-mode)
1373 ;; This won't actually work...Really, this whole thing should
1374 ;; be rewritten instead of being a hack on top of occur.
1375 (setq occur-revert-arguments (list nil 0 (list buffer))))
1376 (save-excursion
1377 (goto-char (point-max))
1378 ;; Do a little shimmy to place point at the end of the last
1379 ;; "real" paragraph. Need to avoid validating across an \end,
1380 ;; because that blows up latex-forward-sexp.
1381 (backward-paragraph)
1382 (forward-paragraph)
1383 (while (not (bobp))
1384 ;; Scan the previous paragraph for invalidities.
1385 (backward-paragraph)
1386 (save-excursion
1387 (or (tex-validate-region (point) (save-excursion
1388 (forward-paragraph)
1389 (point)))
1390 (let ((end (line-beginning-position 2))
1391 start tem)
1392 (beginning-of-line)
1393 (setq start (point))
1394 ;; Keep track of line number as we scan,
1395 ;; in a cumulative fashion.
1396 (if linenum
1397 (setq linenum (- linenum
1398 (count-lines prevpos (point))))
1399 (setq linenum (1+ (count-lines 1 start))))
1400 (setq prevpos (point))
1401 ;; Mention this mismatch in *Occur*.
1402 ;; Since we scan from end of buffer to beginning,
1403 ;; add each mismatch at the beginning of *Occur*.
1404 (save-excursion
1405 (setq tem (point-marker))
1406 (set-buffer standard-output)
1407 (goto-char (point-min))
1408 ;; Skip "Mismatches:" header line.
1409 (forward-line 1)
1410 (setq num-matches (1+ num-matches))
1411 (insert-buffer-substring buffer start end)
1412 (let (text-beg (text-end (point-marker)))
1413 (forward-char (- start end))
1414 (setq text-beg (point-marker))
1415 (insert (format "%3d: " linenum))
1416 (add-text-properties
1417 text-beg (- text-end 1)
1418 '(mouse-face highlight
1419 help-echo
1420 "mouse-2: go to this invalidity"))
1421 (put-text-property text-beg (- text-end 1)
1422 'occur-target tem))))))))
1423 (with-current-buffer standard-output
1424 (let ((no-matches (zerop num-matches)))
1425 (if no-matches
1426 (insert "None!\n"))
1427 (if (called-interactively-p 'interactive)
1428 (message (cond (no-matches "No mismatches found")
1429 ((= num-matches 1) "1 mismatch found")
1430 (t "%d mismatches found"))
1431 num-matches)))))))
1433 (defun tex-validate-region (start end)
1434 "Check for mismatched braces or $'s in region.
1435 Returns t if no mismatches. Returns nil and moves point to suspect
1436 area if a mismatch is found."
1437 (interactive "r")
1438 (let ((failure-point nil) (max-possible-sexps (- end start)))
1439 (save-excursion
1440 (condition-case ()
1441 (save-restriction
1442 (narrow-to-region start end)
1443 ;; First check that the open and close parens balance in numbers.
1444 (goto-char start)
1445 (while (and (not (eobp))
1446 (<= 0 (setq max-possible-sexps
1447 (1- max-possible-sexps))))
1448 (forward-sexp 1))
1449 ;; Now check that like matches like.
1450 (goto-char start)
1451 (while (re-search-forward "\\s(" nil t)
1452 (save-excursion
1453 (let ((pos (match-beginning 0)))
1454 (goto-char pos)
1455 (skip-chars-backward "\\\\") ; escaped parens
1456 (forward-sexp 1)
1457 (or (eq (preceding-char) (cdr (syntax-after pos)))
1458 (eq (char-after pos) (cdr (syntax-after (1- (point)))))
1459 (error "Mismatched parentheses"))))))
1460 (error
1461 (skip-syntax-forward " .>")
1462 (setq failure-point (point)))))
1463 (if failure-point (goto-char failure-point))
1464 (not failure-point)))
1466 (defun tex-handle-newline (inhibit-validation)
1467 "Break a TeX paragraph with two newlines, or continue a comment.
1468 If not in a comment, insert two newlines, breaking a paragraph for TeX,
1469 and check for mismatched braces or $s in the paragraph being terminated
1470 unless prefix arg INHIBIT-VALIDATION is non-nil to inhibit the checking.
1471 Otherwise (in a comment), just insert a single continued comment line."
1472 (interactive "*P")
1473 (if (nth 4 (syntax-ppss)) ; non-nil if point is in a TeX comment
1474 (comment-indent-new-line)
1475 (tex-terminate-paragraph inhibit-validation)))
1477 (defun tex-terminate-paragraph (inhibit-validation)
1478 "Insert two newlines, breaking a paragraph for TeX.
1479 Check for mismatched braces or $s in paragraph being terminated.
1480 A prefix arg inhibits the checking."
1481 (interactive "*P")
1482 (or inhibit-validation
1483 (save-excursion
1484 ;; For the purposes of this, a "paragraph" is a block of text
1485 ;; wherein all the brackets etc are expected to be balanced. It
1486 ;; may start after a blank line (ie a "proper" paragraph), or
1487 ;; a begin{} or end{} block, etc.
1488 (tex-validate-region
1489 (save-excursion
1490 (backward-paragraph)
1491 (point))
1492 (point)))
1493 (message "Paragraph being closed appears to contain a mismatch"))
1494 (insert "\n\n"))
1496 (define-skeleton tex-insert-braces
1497 "Make a pair of braces and be poised to type inside of them."
1499 ?\{ _ ?})
1501 ;; This function is used as the value of fill-nobreak-predicate
1502 ;; in LaTeX mode. Its job is to prevent line-breaking inside
1503 ;; of a \verb construct.
1504 (defun latex-fill-nobreak-predicate ()
1505 (save-excursion
1506 (skip-chars-backward " ")
1507 ;; Don't break after \ since `\ ' has special meaning.
1508 (or (and (not (bobp)) (memq (char-syntax (char-before)) '(?\\ ?/)))
1509 (let ((opoint (point))
1510 inside)
1511 (beginning-of-line)
1512 (while (re-search-forward "\\\\verb\\(.\\)" opoint t)
1513 (unless (re-search-forward (regexp-quote (match-string 1)) opoint t)
1514 (setq inside t)))
1515 inside))))
1517 (defvar latex-block-default "enumerate")
1519 (defvar latex-block-args-alist
1520 '(("array" nil ?\{ (skeleton-read "Format: ") ?\})
1521 ("tabular" nil ?\{ (skeleton-read "Format: ") ?\})
1522 ("minipage" nil ?\{ (skeleton-read "Size: ") ?\})
1523 ("picture" nil ?\( (skeleton-read "SizeX,SizeY: ") ?\))
1524 ;; FIXME: This is right for Prosper, but not for seminar.
1525 ;; ("slide" nil ?\{ (skeleton-read "Title: ") ?\})
1527 "Skeleton element to use for arguments to particular environments.
1528 Every element of the list has the form (NAME . SKEL-ELEM) where NAME is
1529 the name of the environment and SKEL-ELEM is an element to use in
1530 a skeleton (see `skeleton-insert').")
1532 (defvar latex-block-body-alist
1533 '(("enumerate" nil '(latex-insert-item) > _)
1534 ("itemize" nil '(latex-insert-item) > _)
1535 ("table" nil "\\caption{" > (skeleton-read "Caption: ") "}" > \n
1536 '(if (and (boundp 'reftex-mode) reftex-mode) (reftex-label "table"))
1537 \n _)
1538 ("figure" nil > _ \n "\\caption{" > (skeleton-read "Caption: ") "}" > \n
1539 '(if (and (boundp 'reftex-mode) reftex-mode) (reftex-label "table"))))
1540 "Skeleton element to use for the body of particular environments.
1541 Every element of the list has the form (NAME . SKEL-ELEM) where NAME is
1542 the name of the environment and SKEL-ELEM is an element to use in
1543 a skeleton (see `skeleton-insert').")
1545 ;; Like tex-insert-braces, but for LaTeX.
1546 (defalias 'tex-latex-block 'latex-insert-block)
1547 (define-skeleton latex-insert-block
1548 "Create a matching pair of lines \\begin{NAME} and \\end{NAME} at point.
1549 Puts point on a blank line between them."
1550 (let ((choice (completing-read (format "LaTeX block name [%s]: "
1551 latex-block-default)
1552 (latex-complete-envnames)
1553 nil nil nil nil latex-block-default)))
1554 (setq latex-block-default choice)
1555 (unless (or (member choice latex-standard-block-names)
1556 (member choice latex-block-names))
1557 ;; Remember new block names for later completion.
1558 (push choice latex-block-names))
1559 choice)
1560 \n "\\begin{" str "}"
1561 (cdr (assoc str latex-block-args-alist))
1562 > \n (or (cdr (assoc str latex-block-body-alist)) '(nil > _))
1563 (unless (bolp) '\n)
1564 "\\end{" str "}" > \n)
1566 (define-skeleton latex-insert-item
1567 "Insert an \\item macro."
1569 \n "\\item " >)
1572 ;;;; LaTeX completion.
1574 (defvar latex-complete-bibtex-cache nil)
1576 (define-obsolete-function-alias 'latex-string-prefix-p
1577 'string-prefix-p "24.3")
1579 (defvar bibtex-reference-key)
1580 (declare-function reftex-get-bibfile-list "reftex-cite.el" ())
1582 (defun latex-complete-bibtex-keys ()
1583 (when (bound-and-true-p reftex-mode)
1584 (lambda (key pred action)
1585 (let ((re (concat "^[ \t]*@\\([a-zA-Z]+\\)[ \t\n]*\\([{(][ \t\n]*\\)"
1586 (regexp-quote key)))
1587 (files (reftex-get-bibfile-list))
1588 keys)
1589 (if (and (eq (car latex-complete-bibtex-cache)
1590 (reftex-get-bibfile-list))
1591 (string-prefix-p (nth 1 latex-complete-bibtex-cache)
1592 key))
1593 ;; Use the cache.
1594 (setq keys (nth 2 latex-complete-bibtex-cache))
1595 (dolist (file files)
1596 (with-current-buffer (find-file-noselect file)
1597 (goto-char (point-min))
1598 (while (re-search-forward re nil t)
1599 (goto-char (match-end 2))
1600 (when (and (not (member-ignore-case (match-string 1)
1601 '("c" "comment" "string")))
1602 (looking-at bibtex-reference-key))
1603 (push (match-string-no-properties 0) keys)))))
1604 ;; Fill the cache.
1605 (setq-local latex-complete-bibtex-cache (list files key keys)))
1606 (complete-with-action action keys key pred)))))
1608 (defun latex-complete-envnames ()
1609 (completion-table-in-turn
1610 (append latex-block-names latex-standard-block-names)
1611 (completion-table-dynamic
1612 (lambda (str)
1613 (with-current-buffer (if (and (minibufferp) (minibuffer-selected-window))
1614 (window-buffer (minibuffer-selected-window))
1615 (current-buffer))
1616 (save-excursion
1617 (let ((comps '())
1618 (pos (point)))
1619 (goto-char (point-min))
1620 (while (re-search-forward (concat "\\\\begin{\\(" str "[^}\n ]*\\)")
1621 nil t)
1622 (unless (and (<= (match-beginning 0) pos)
1623 (>= (match-end 0) pos))
1624 (push (match-string 1) comps)))
1625 comps)))))))
1627 (defun latex-complete-refkeys ()
1628 (when (boundp 'reftex-docstruct-symbol)
1629 (symbol-value reftex-docstruct-symbol)))
1631 (defvar latex-complete-alist
1632 `(("\\`\\\\\\(short\\)?cite\\'" . ,#'latex-complete-bibtex-keys)
1633 ("\\`\\\\\\(begin\\|end\\)\\'" . ,#'latex-complete-envnames)
1634 ("\\`\\\\[vf]?ref\\'" . ,#'latex-complete-refkeys)))
1636 (defun latex-complete-data ()
1637 "Get completion-data at point."
1638 (save-excursion
1639 (let ((pt (point)))
1640 (skip-chars-backward "^ {}\n\t\\\\")
1641 (pcase (char-before)
1642 ((or `nil ?\s ?\n ?\t ?\}) nil)
1643 (?\\
1644 ;; TODO: Complete commands.
1645 nil)
1646 (?\{
1647 ;; Complete args to commands.
1648 (let* ((cmd
1649 (save-excursion
1650 (forward-char -1)
1651 (skip-chars-backward " \n")
1652 (buffer-substring (point)
1653 (progn
1654 (skip-chars-backward "a-zA-Z@*")
1655 (let ((n (skip-chars-backward "\\\\")))
1656 (forward-char (* 2 (/ n 2))))
1657 (point)))))
1658 (start (point))
1659 (_ (progn (goto-char pt) (skip-chars-backward "^," start)))
1660 (comp-beg (point))
1661 (_ (progn (goto-char pt) (skip-chars-forward "^, {}\n\t\\\\")))
1662 (comp-end (point))
1663 (table
1664 (funcall
1665 (let ((f (lambda () t)))
1666 (dolist (comp latex-complete-alist)
1667 (if (string-match (car comp) cmd)
1668 (setq f (cdr comp))))
1669 f))))
1670 (if (eq table t)
1671 ;; Unknown command.
1673 (list comp-beg comp-end table))))))))
1675 ;;;;
1676 ;;;; LaTeX syntax navigation
1677 ;;;;
1679 (defmacro tex-search-noncomment (&rest body)
1680 "Execute BODY as long as it return non-nil and point is in a comment.
1681 Return the value returned by the last execution of BODY."
1682 (declare (debug t))
1683 (let ((res-sym (make-symbol "result")))
1684 `(let (,res-sym)
1685 (while
1686 (and (setq ,res-sym (progn ,@body))
1687 (save-excursion (skip-chars-backward "^\n%") (not (bolp)))))
1688 ,res-sym)))
1690 (defun tex-last-unended-begin ()
1691 "Leave point at the beginning of the last `\\begin{...}' that is unended."
1692 (condition-case nil
1693 (while (and (tex-search-noncomment
1694 (re-search-backward "\\\\\\(begin\\|end\\)\\s *{"))
1695 (looking-at "\\\\end"))
1696 (tex-last-unended-begin))
1697 (search-failed (error "Couldn't find unended \\begin"))))
1699 (defun tex-next-unmatched-end ()
1700 "Leave point at the end of the next `\\end' that is unmatched."
1701 (while (and (tex-search-noncomment
1702 (re-search-forward "\\\\\\(begin\\|end\\)\\s *{[^}]+}"))
1703 (save-excursion (goto-char (match-beginning 0))
1704 (looking-at "\\\\begin")))
1705 (tex-next-unmatched-end)))
1707 (defun tex-next-unmatched-eparen (otype)
1708 "Leave point after the next unmatched escaped closing parenthesis.
1709 The string OTYPE is an opening parenthesis type: `(', `{', or `['."
1710 (condition-case nil
1711 (let ((ctype (char-to-string (cdr (aref (syntax-table)
1712 (string-to-char otype))))))
1713 (while (and (tex-search-noncomment
1714 (re-search-forward (format "\\\\[%s%s]" ctype otype)))
1715 (save-excursion
1716 (goto-char (match-beginning 0))
1717 (looking-at (format "\\\\%s" (regexp-quote otype)))))
1718 (tex-next-unmatched-eparen otype)))
1719 (wrong-type-argument (error "Unknown opening parenthesis type: %s" otype))
1720 (search-failed (error "Couldn't find closing escaped paren"))))
1722 (defun tex-last-unended-eparen (ctype)
1723 "Leave point at the start of the last unended escaped opening parenthesis.
1724 The string CTYPE is a closing parenthesis type: `)', `}', or `]'."
1725 (condition-case nil
1726 (let ((otype (char-to-string (cdr (aref (syntax-table)
1727 (string-to-char ctype))))))
1728 (while (and (tex-search-noncomment
1729 (re-search-backward (format "\\\\[%s%s]" ctype otype)))
1730 (looking-at (format "\\\\%s" (regexp-quote ctype))))
1731 (tex-last-unended-eparen ctype)))
1732 (wrong-type-argument (error "Unknown opening parenthesis type: %s" ctype))
1733 (search-failed (error "Couldn't find unended escaped paren"))))
1735 (defun tex-goto-last-unclosed-latex-block ()
1736 "Move point to the last unclosed \\begin{...}.
1737 Mark is left at original location."
1738 (interactive)
1739 (let ((spot))
1740 (save-excursion
1741 (tex-last-unended-begin)
1742 (setq spot (point)))
1743 (push-mark)
1744 (goto-char spot)))
1746 (defvar latex-handle-escaped-parens t)
1748 ;; Don't think this one actually _needs_ (for the purposes of
1749 ;; tex-mode) to handle escaped parens.
1750 ;; Does not handle escaped parens when latex-handle-escaped-parens is nil.
1751 (defun latex-backward-sexp-1 ()
1752 "Like (backward-sexp 1) but aware of multi-char elements and escaped parens."
1753 (let ((pos (point))
1754 (forward-sexp-function))
1755 (backward-sexp 1)
1756 (cond ((looking-at
1757 (if latex-handle-escaped-parens
1758 "\\\\\\(begin\\>\\|[[({]\\)"
1759 "\\\\begin\\>"))
1760 (signal 'scan-error
1761 (list "Containing expression ends prematurely"
1762 (point) (prog1 (point) (goto-char pos)))))
1763 ((and latex-handle-escaped-parens
1764 (looking-at "\\\\\\([])}]\\)"))
1765 (tex-last-unended-eparen (match-string 1)))
1766 ((eq (char-after) ?{)
1767 (let ((newpos (point)))
1768 (when (ignore-errors (backward-sexp 1) t)
1769 (if (or (looking-at "\\\\end\\>")
1770 ;; In case the \\ ends a verbatim section.
1771 (and (looking-at "end\\>") (eq (char-before) ?\\)))
1772 (tex-last-unended-begin)
1773 (goto-char newpos))))))))
1775 ;; Note this does not handle things like mismatched brackets inside
1776 ;; begin/end blocks.
1777 ;; Needs to handle escaped parens for tex-validate-*.
1778 ;; http://lists.gnu.org/archive/html/bug-gnu-emacs/2007-09/msg00038.html
1779 ;; Does not handle escaped parens when latex-handle-escaped-parens is nil.
1780 (defun latex-forward-sexp-1 ()
1781 "Like (forward-sexp 1) but aware of multi-char elements and escaped parens."
1782 (let ((pos (point))
1783 (forward-sexp-function))
1784 (forward-sexp 1)
1785 (let ((newpos (point)))
1786 (skip-syntax-backward "/w")
1787 (cond
1788 ((looking-at "\\\\end\\>")
1789 (signal 'scan-error
1790 (list "Containing expression ends prematurely"
1791 (point)
1792 (prog1
1793 (progn (ignore-errors (forward-sexp 2)) (point))
1794 (goto-char pos)))))
1795 ((looking-at "\\\\begin\\>")
1796 (goto-char (match-end 0))
1797 (tex-next-unmatched-end))
1798 ;; A better way to handle this, \( .. \) etc, is probably to
1799 ;; temporarily change the syntax of the \ in \( to punctuation.
1800 ((and latex-handle-escaped-parens
1801 (looking-back "\\\\[])}]" (- (point) 2)))
1802 (signal 'scan-error
1803 (list "Containing expression ends prematurely"
1804 (- (point) 2) (prog1 (point)
1805 (goto-char pos)))))
1806 ((and latex-handle-escaped-parens
1807 (looking-back "\\\\\\([({[]\\)" (- (point) 2)))
1808 (tex-next-unmatched-eparen (match-string 1)))
1809 (t (goto-char newpos))))))
1811 (defun latex-forward-sexp (&optional arg)
1812 "Like `forward-sexp' but aware of multi-char elements and escaped parens."
1813 (interactive "P")
1814 (unless arg (setq arg 1))
1815 (let ((pos (point))
1816 (opoint 0))
1817 (condition-case err
1818 (while (and (/= (point) opoint)
1819 (/= arg 0))
1820 (setq opoint (point))
1821 (setq arg
1822 (if (> arg 0)
1823 (progn (latex-forward-sexp-1) (1- arg))
1824 (progn (latex-backward-sexp-1) (1+ arg)))))
1825 (scan-error
1826 (goto-char pos)
1827 (signal (car err) (cdr err))))))
1829 (defun latex-syntax-after ()
1830 "Like (char-syntax (char-after)) but aware of multi-char elements."
1831 (if (looking-at "\\\\end\\>") ?\) (char-syntax (following-char))))
1833 (defun latex-skip-close-parens ()
1834 "Like (skip-syntax-forward \" )\") but aware of multi-char elements."
1835 (let ((forward-sexp-function nil))
1836 (while (progn (skip-syntax-forward " )")
1837 (looking-at "\\\\end\\>"))
1838 (forward-sexp 2))))
1840 (defun latex-down-list ()
1841 "Like (down-list 1) but aware of multi-char elements."
1842 (forward-comment (point-max))
1843 (let ((forward-sexp-function nil))
1844 (if (not (looking-at "\\\\begin\\>"))
1845 (down-list 1)
1846 (forward-sexp 1)
1847 ;; Skip arguments.
1848 (while (looking-at "[ \t]*[[{(]")
1849 (with-syntax-table tex-mode-syntax-table
1850 (forward-sexp))))))
1852 (defalias 'tex-close-latex-block 'latex-close-block)
1853 (define-skeleton latex-close-block
1854 "Create an \\end{...} to match the last unclosed \\begin{...}."
1855 (save-excursion
1856 (tex-last-unended-begin)
1857 (if (not (looking-at "\\\\begin\\(\\s *{[^}\n]*}\\)")) '("{" _ "}")
1858 (match-string 1)))
1859 \n "\\end" str > \n)
1861 (define-skeleton latex-split-block
1862 "Split the enclosing environment by inserting \\end{..}\\begin{..} at point."
1863 (save-excursion
1864 (tex-last-unended-begin)
1865 (if (not (looking-at "\\\\begin\\(\\s *{[^}\n]*}\\)")) '("{" _ "}")
1866 (prog1 (match-string 1)
1867 (goto-char (match-end 1))
1868 (setq v1 (buffer-substring (point)
1869 (progn
1870 (while (looking-at "[ \t]*[[{]")
1871 (forward-sexp 1))
1872 (point)))))))
1873 \n "\\end" str > \n _ \n "\\begin" str v1 > \n)
1875 (defconst tex-discount-args-cmds
1876 '("begin" "end" "input" "special" "cite" "ref" "include" "includeonly"
1877 "documentclass" "usepackage" "label")
1878 "TeX commands whose arguments should not be counted as text.")
1880 (defun tex-count-words (begin end)
1881 "Count the number of words in the buffer."
1882 (interactive
1883 (if (and transient-mark-mode mark-active)
1884 (list (region-beginning) (region-end))
1885 (list (point-min) (point-max))))
1886 ;; TODO: skip comments and math and maybe some environments.
1887 (save-excursion
1888 (goto-char begin)
1889 (let ((count 0))
1890 (while (and (< (point) end) (re-search-forward "\\<" end t))
1891 (if (not (eq (char-syntax (preceding-char)) ?/))
1892 (progn
1893 ;; Don't count single-char words.
1894 (unless (looking-at ".\\>") (cl-incf count))
1895 (forward-char 1))
1896 (let ((cmd
1897 (buffer-substring-no-properties
1898 (point) (progn (when (zerop (skip-chars-forward "a-zA-Z@"))
1899 (forward-char 1))
1900 (point)))))
1901 (when (member cmd tex-discount-args-cmds)
1902 (skip-chars-forward "*")
1903 (forward-comment (point-max))
1904 (when (looking-at "\\[")
1905 (forward-sexp 1)
1906 (forward-comment (point-max)))
1907 (if (not (looking-at "{"))
1908 (forward-char 1)
1909 (forward-sexp 1))))))
1910 (message "%s words" count))))
1914 ;;; Invoking TeX in an inferior shell.
1916 ;; Why use a shell instead of running TeX directly? Because if TeX
1917 ;; gets stuck, the user can switch to the shell window and type at it.
1919 (defvar tex-error-parse-syntax-table
1920 (let ((st (make-syntax-table)))
1921 (modify-syntax-entry ?\( "()" st)
1922 (modify-syntax-entry ?\) ")(" st)
1923 (modify-syntax-entry ?\\ "\\" st)
1924 (modify-syntax-entry ?\{ "_" st)
1925 (modify-syntax-entry ?\} "_" st)
1926 (modify-syntax-entry ?\[ "_" st)
1927 (modify-syntax-entry ?\] "_" st)
1928 ;; Single quotations may appear in errors
1929 (modify-syntax-entry ?\" "_" st)
1931 "Syntax-table used while parsing TeX error messages.")
1933 (defun tex-old-error-file-name ()
1934 ;; This is unreliable, partly because we don't try very hard, and
1935 ;; partly because TeX's output format is eminently ambiguous and unfriendly
1936 ;; to automation.
1937 (save-excursion
1938 (save-match-data
1939 (with-syntax-table tex-error-parse-syntax-table
1940 (beginning-of-line)
1941 (backward-up-list 1)
1942 (skip-syntax-forward "(_")
1943 (while (not (let ((try-filename (thing-at-point 'filename)))
1944 (and try-filename
1945 (not (string= "" try-filename))
1946 (file-readable-p try-filename))))
1947 (skip-syntax-backward "(_")
1948 (backward-up-list 1)
1949 (skip-syntax-forward "(_"))
1950 (thing-at-point 'filename)))))
1952 (defconst tex-error-regexp-alist
1953 ;; First alternative handles the newer --file-line-error style:
1954 ;; ./test2.tex:14: Too many }'s.
1955 '(gnu
1956 ;; Second handles the old-style, which spans two lines but doesn't include
1957 ;; any file info:
1958 ;; ! Too many }'s.
1959 ;; l.396 toto}
1960 ("^l\\.\\([1-9][0-9]*\\) \\(?:\\.\\.\\.\\)?\\(.*\\)$"
1961 tex-old-error-file-name 1 nil nil nil
1962 ;; Since there's no filename to highlight, let's highlight the message.
1963 (2 compilation-error-face))
1964 ;; A few common warning messages.
1965 ("^\\(?:Und\\|Ov\\)erfull \\\\[hv]box .* at lines? \\(\\([1-9][0-9]*\\)\\(?:--\\([1-9][0-9]*\\)\\)?\\)$"
1966 tex-old-error-file-name (2 . 3) nil 1 nil
1967 (1 compilation-warning-face))
1968 ("^(Font) *\\([^ \n].* on input line \\([1-9][0-9]*\\)\\)\\.$"
1969 tex-old-error-file-name 2 nil 1 1
1970 (2 compilation-warning-face))
1971 ;; Included files get output as (<file> ...).
1972 ;; FIXME: there tend to be a boatload of them at the beginning of the
1973 ;; output which aren't that interesting. Maybe we should filter out
1974 ;; all the file name that start with /usr/share?
1975 ;; ("(\\.?/\\([^() \n]+\\)" 1 nil nil 0)
1978 ;; The utility functions:
1980 (define-derived-mode tex-shell shell-mode "TeX-Shell"
1981 (setq-local compilation-error-regexp-alist tex-error-regexp-alist)
1982 (compilation-shell-minor-mode t))
1984 ;;;###autoload
1985 (defun tex-start-shell ()
1986 (with-current-buffer
1987 (make-comint
1988 "tex-shell"
1989 (or tex-shell-file-name (getenv "ESHELL") shell-file-name)
1991 ;; Specify an interactive shell, to make sure it prompts.
1992 "-i")
1993 (let ((proc (get-process "tex-shell")))
1994 (set-process-sentinel proc 'tex-shell-sentinel)
1995 (set-process-query-on-exit-flag proc nil)
1996 (tex-shell)
1997 (while (zerop (buffer-size))
1998 (sleep-for 1)))))
2000 (defun tex-feed-input ()
2001 "Send input to the tex shell process.
2002 In the tex buffer this can be used to continue an interactive tex run.
2003 In the tex shell buffer this command behaves like `comint-send-input'."
2004 (interactive)
2005 (set-buffer (tex-shell-buf))
2006 (comint-send-input)
2007 (tex-recenter-output-buffer nil))
2009 (defun tex-display-shell ()
2010 "Make the TeX shell buffer visible in a window."
2011 (display-buffer (tex-shell-buf))
2012 (tex-recenter-output-buffer nil))
2014 (defun tex-shell-sentinel (proc _msg)
2015 (cond ((null (buffer-name (process-buffer proc)))
2016 ;; buffer killed
2017 (set-process-buffer proc nil)
2018 (tex-delete-last-temp-files))
2019 ((memq (process-status proc) '(signal exit))
2020 (tex-delete-last-temp-files))))
2022 (defun tex-set-buffer-directory (buffer directory)
2023 "Set BUFFER's default directory to be DIRECTORY."
2024 (setq directory (file-name-as-directory (expand-file-name directory)))
2025 (if (not (file-directory-p directory))
2026 (error "%s is not a directory" directory)
2027 (with-current-buffer buffer
2028 (setq default-directory directory))))
2030 (defvar tex-send-command-modified-tick 0)
2031 (make-variable-buffer-local 'tex-send-command-modified-tick)
2033 (defun tex-shell-proc ()
2034 (or (tex-shell-running) (error "No TeX subprocess")))
2035 (defun tex-shell-buf ()
2036 (process-buffer (tex-shell-proc)))
2037 (defun tex-shell-buf-no-error ()
2038 (let ((proc (tex-shell-running)))
2039 (and proc (process-buffer proc))))
2041 (defun tex-send-command (command &optional file background)
2042 "Send COMMAND to TeX shell process, substituting optional FILE for *.
2043 Do this in background if optional BACKGROUND is t. If COMMAND has no *,
2044 FILE will be appended, preceded by a blank, to COMMAND. If FILE is nil, no
2045 substitution will be made in COMMAND. COMMAND can be any expression that
2046 evaluates to a command string.
2048 Return the process in which TeX is running."
2049 (save-excursion
2050 (let* ((cmd (eval command))
2051 (proc (tex-shell-proc))
2052 (buf (process-buffer proc))
2053 (star (string-match "\\*" cmd))
2054 (string
2055 (concat
2056 (if (null file)
2058 (if (file-name-absolute-p file)
2059 (setq file (convert-standard-filename file)))
2060 (if star (concat (substring cmd 0 star)
2061 (shell-quote-argument file)
2062 (substring cmd (1+ star)))
2063 (concat cmd " " (shell-quote-argument file))))
2064 (if background "&" ""))))
2065 ;; Switch to buffer before checking for subproc output in it.
2066 (set-buffer buf)
2067 ;; If text is unchanged since previous tex-send-command,
2068 ;; we haven't got any output. So wait for output now.
2069 (if (= (buffer-modified-tick buf) tex-send-command-modified-tick)
2070 (accept-process-output proc))
2071 (goto-char (process-mark proc))
2072 (insert string)
2073 (comint-send-input)
2074 (setq tex-send-command-modified-tick (buffer-modified-tick buf))
2075 proc)))
2077 (defun tex-delete-last-temp-files (&optional not-all)
2078 "Delete any junk files from last temp file.
2079 If NOT-ALL is non-nil, save the `.dvi' file."
2080 (if tex-last-temp-file
2081 (let* ((dir (file-name-directory tex-last-temp-file))
2082 (list (and (file-directory-p dir)
2083 (file-name-all-completions
2084 (file-name-base tex-last-temp-file)
2085 dir))))
2086 (while list
2087 (if not-all
2088 (and
2089 ;; If arg is non-nil, don't delete the .dvi file.
2090 (not (string-match "\\.dvi$" (car list)))
2091 (delete-file (concat dir (car list))))
2092 (delete-file (concat dir (car list))))
2093 (setq list (cdr list))))))
2095 (add-hook 'kill-emacs-hook 'tex-delete-last-temp-files)
2098 ;; Machinery to guess the command that the user wants to execute.
2101 (defvar tex-compile-history nil)
2103 (defvar tex-input-files-re
2104 (eval-when-compile
2105 (concat "\\." (regexp-opt '("tex" "texi" "texinfo"
2106 "bbl" "ind" "sty" "cls") t)
2107 ;; Include files with no dots (for directories).
2108 "\\'\\|\\`[^.]+\\'")))
2110 (defcustom tex-use-reftex t
2111 "If non-nil, use RefTeX's list of files to determine what command to use."
2112 :type 'boolean
2113 :group 'tex)
2115 (defvar tex-compile-commands
2116 `(,@(mapcar (lambda (prefix)
2117 `((concat ,prefix tex-command
2118 " " (if (< 0 (length tex-start-commands))
2119 (shell-quote-argument tex-start-commands))
2120 " %f")
2121 t "%r.pdf"))
2122 '("pdf" "xe" "lua"))
2123 ((concat tex-command
2124 " " (if (< 0 (length tex-start-commands))
2125 (shell-quote-argument tex-start-commands))
2126 " %f")
2127 t "%r.dvi")
2128 ("xdvi %r &" "%r.dvi")
2129 ("\\doc-view \"%r.pdf\"" "%r.pdf")
2130 ("xpdf %r.pdf &" "%r.pdf")
2131 ("gv %r.ps &" "%r.ps")
2132 ("yap %r &" "%r.dvi")
2133 ("advi %r &" "%r.dvi")
2134 ("gv %r.pdf &" "%r.pdf")
2135 ("bibtex %r" "%r.aux" "%r.bbl")
2136 ("makeindex %r" "%r.idx" "%r.ind")
2137 ("texindex %r.??")
2138 ("dvipdfm %r" "%r.dvi" "%r.pdf")
2139 ("dvipdf %r" "%r.dvi" "%r.pdf")
2140 ("dvips -o %r.ps %r" "%r.dvi" "%r.ps")
2141 ("ps2pdf %r.ps" "%r.ps" "%r.pdf")
2142 ("lpr %r.ps" "%r.ps"))
2143 "List of commands for `tex-compile'.
2144 Each element should be of the form (FORMAT IN OUT) where
2145 FORMAT is an expression that evaluates to a string that can contain
2146 - `%r' the main file name without extension.
2147 - `%f' the main file name.
2148 IN can be either a string (with the same % escapes in it) indicating
2149 the name of the input file, or t to indicate that the input is all
2150 the TeX files of the document, or nil if we don't know.
2151 OUT describes the output file and is either a %-escaped string
2152 or nil to indicate that there is no output file.")
2154 (define-obsolete-function-alias 'tex-string-prefix-p 'string-prefix-p "24.3")
2156 (defun tex-guess-main-file (&optional all)
2157 "Find a likely `tex-main-file'.
2158 Looks for hints in other buffers in the same directory or in
2159 ALL other buffers. If ALL is `sub' only look at buffers in parent directories
2160 of the current buffer."
2161 (let ((dir default-directory)
2162 (header-re tex-start-of-header))
2163 (catch 'found
2164 ;; Look for a buffer with `tex-main-file' set.
2165 (dolist (buf (if (consp all) all (buffer-list)))
2166 (with-current-buffer buf
2167 (when (and (cond
2168 ((null all) (equal dir default-directory))
2169 ((eq all 'sub) (string-prefix-p default-directory dir))
2170 (t))
2171 (stringp tex-main-file))
2172 (throw 'found (expand-file-name tex-main-file)))))
2173 ;; Look for a buffer containing the magic `tex-start-of-header'.
2174 (dolist (buf (if (consp all) all (buffer-list)))
2175 (with-current-buffer buf
2176 (when (and (cond
2177 ((null all) (equal dir default-directory))
2178 ((eq all 'sub) (string-prefix-p default-directory dir))
2179 (t))
2180 buffer-file-name
2181 ;; (or (easy-mmode-derived-mode-p 'latex-mode)
2182 ;; (easy-mmode-derived-mode-p 'plain-tex-mode))
2183 (save-excursion
2184 (save-restriction
2185 (widen)
2186 (goto-char (point-min))
2187 (re-search-forward
2188 header-re (+ (point) 10000) t))))
2189 (throw 'found (expand-file-name buffer-file-name))))))))
2191 (defun tex-main-file ()
2192 "Return the relative name of the main file."
2193 (let* ((file (or tex-main-file
2194 ;; Compatibility with AUCTeX.
2195 (with-no-warnings
2196 (when (boundp 'TeX-master)
2197 (cond ((stringp TeX-master)
2198 (setq-local tex-main-file TeX-master))
2199 ((and (eq TeX-master t) buffer-file-name)
2200 (file-relative-name buffer-file-name)))))
2201 ;; Try to guess the main file.
2202 (if (not buffer-file-name)
2203 (error "Buffer is not associated with any file")
2204 (file-relative-name
2205 (if (save-excursion
2206 (goto-char (point-min))
2207 (re-search-forward tex-start-of-header
2208 (+ (point) 10000) t))
2209 ;; This is the main file.
2210 buffer-file-name
2211 ;; This isn't the main file, let's try to find better,
2212 (or (tex-guess-main-file)
2213 (tex-guess-main-file 'sub)
2214 ;; (tex-guess-main-file t)
2215 buffer-file-name)))))))
2216 (if (or (file-exists-p file) (string-match "\\.tex\\'" file))
2217 file (concat file ".tex"))))
2219 (defun tex-summarize-command (cmd)
2220 (if (not (stringp cmd)) ""
2221 (mapconcat #'identity
2222 (mapcar (lambda (s) (car (split-string s)))
2223 (split-string cmd "\\s-*\\(?:;\\|&&\\)\\s-*"))
2224 "&")))
2226 (defun tex-uptodate-p (file)
2227 "Return non-nil if FILE is not uptodate w.r.t the document source files.
2228 FILE is typically the output DVI or PDF file."
2229 ;; We should check all the files included !!!
2230 (and
2231 ;; Clearly, the target must exist.
2232 (file-exists-p file)
2233 ;; And the last run must not have asked for a rerun.
2234 ;; FIXME: this should check that the last run was done on the same file.
2235 (let ((buf (condition-case nil (tex-shell-buf) (error nil))))
2236 (when buf
2237 (with-current-buffer buf
2238 (save-excursion
2239 (goto-char (point-max))
2240 (and (re-search-backward
2241 (concat "(see the transcript file for additional information)"
2242 "\\|^Output written on .*"
2243 (regexp-quote (file-name-nondirectory file))
2244 " (.*)\\.")
2245 nil t)
2246 (> (save-excursion
2247 ;; Usually page numbers are output as [N], but
2248 ;; I've already seen things like
2249 ;; [1{/var/lib/texmf/fonts/map/pdftex/updmap/pdftex.map}]
2250 (or (re-search-backward "\\[[0-9]+\\({[^}]*}\\)?\\]"
2251 nil t)
2252 (point-min)))
2253 (save-excursion
2254 (or (re-search-backward "Rerun" nil t)
2255 (point-min)))))))))
2256 ;; And the input files must not have been changed in the meantime.
2257 (let ((files (if (and tex-use-reftex
2258 (fboundp 'reftex-scanning-info-available-p)
2259 (reftex-scanning-info-available-p))
2260 (reftex-all-document-files)
2261 (list (file-name-directory (expand-file-name file)))))
2262 (ignored-dirs-re
2263 (concat
2264 (regexp-opt
2265 (delq nil (mapcar (lambda (s) (if (eq (aref s (1- (length s))) ?/)
2266 (substring s 0 (1- (length s)))))
2267 completion-ignored-extensions))
2268 t) "\\'"))
2269 (uptodate t))
2270 (while (and files uptodate)
2271 (let ((f (pop files)))
2272 (if (and (file-directory-p f)
2273 ;; Avoid infinite loops.
2274 (not (file-symlink-p f)))
2275 (unless (string-match ignored-dirs-re f)
2276 (setq files (nconc
2277 (ignore-errors ;Not readable or something.
2278 (directory-files f t tex-input-files-re))
2279 files)))
2280 (when (file-newer-than-file-p f file)
2281 (setq uptodate nil)))))
2282 uptodate)))
2285 (autoload 'format-spec "format-spec")
2287 (defvar tex-executable-cache nil)
2288 (defun tex-executable-exists-p (name)
2289 "Like `executable-find' but with a cache."
2290 (let ((f (and (string-match "^\\\\\\([^ \t\n]+\\)" name)
2291 (intern-soft (concat "tex-cmd-" (match-string 1 name))))))
2292 (if (fboundp f)
2294 (let ((cache (assoc name tex-executable-cache)))
2295 (if cache (cdr cache)
2296 (let ((executable (executable-find name)))
2297 (push (cons name executable) tex-executable-cache)
2298 executable))))))
2300 (defun tex-command-executable (cmd)
2301 (let ((s (if (stringp cmd) cmd (eval (car cmd)))))
2302 (substring s 0 (string-match "[ \t]\\|\\'" s))))
2304 (defun tex-command-active-p (cmd fspec)
2305 "Return non-nil if the CMD spec might need to be run."
2306 (let ((in (nth 1 cmd))
2307 (out (nth 2 cmd)))
2308 (if (stringp in)
2309 (let ((file (format-spec in fspec)))
2310 (when (file-exists-p file)
2311 (or (not out)
2312 (file-newer-than-file-p
2313 file (format-spec out fspec)))))
2314 (when (and (eq in t) (stringp out))
2315 (not (tex-uptodate-p (format-spec out fspec)))))))
2317 (defcustom tex-cmd-bibtex-args "--min-crossref=100"
2318 "Extra args to pass to `bibtex' by default."
2319 :type 'string
2320 :version "23.1"
2321 :group 'tex-run)
2323 (defun tex-format-cmd (format fspec)
2324 "Like `format-spec' but adds user-specified args to the command.
2325 Only applies the FSPEC to the args part of FORMAT."
2326 (if (not (string-match "\\([^ /\\]+\\) " format))
2327 (format-spec format fspec)
2328 (let* ((prefix (substring format 0 (match-beginning 0)))
2329 (cmd (match-string 1 format))
2330 (args (substring format (match-end 0)))
2331 (sym (intern-soft (format "tex-cmd-%s-args" cmd)))
2332 (extra-args (and sym (symbol-value sym))))
2333 (concat prefix cmd
2334 (if extra-args (concat " " extra-args))
2335 " " (format-spec args fspec)))))
2337 (defun tex-compile-default (fspec)
2338 "Guess a default command given the `format-spec' FSPEC."
2339 ;; TODO: Learn to do latex+dvips!
2340 (let ((cmds nil)
2341 (unchanged-in nil))
2342 ;; Only consider active commands.
2343 (dolist (cmd tex-compile-commands)
2344 (when (tex-executable-exists-p (tex-command-executable cmd))
2345 (if (tex-command-active-p cmd fspec)
2346 (push cmd cmds)
2347 (push (nth 1 cmd) unchanged-in))))
2348 ;; If no command seems to be applicable, arbitrarily pick the first one.
2349 (setq cmds (if cmds (nreverse cmds) (list (car tex-compile-commands))))
2350 ;; Remove those commands whose input was considered stable for
2351 ;; some other command (typically if (t . "%.pdf") is inactive
2352 ;; then we're using pdflatex and the fact that the dvi file
2353 ;; is nonexistent doesn't matter).
2354 (let ((tmp nil))
2355 (dolist (cmd cmds)
2356 (unless (member (nth 1 cmd) unchanged-in)
2357 (push cmd tmp)))
2358 ;; Only remove if there's something left.
2359 (if tmp (setq cmds (nreverse tmp))))
2360 ;; Remove commands whose input is not uptodate either.
2361 (let ((outs (delq nil (mapcar (lambda (x) (nth 2 x)) cmds)))
2362 (tmp nil))
2363 (dolist (cmd cmds)
2364 (unless (member (nth 1 cmd) outs)
2365 (push cmd tmp)))
2366 ;; Only remove if there's something left.
2367 (if tmp (setq cmds (nreverse tmp))))
2368 ;; Select which file we're going to operate on (the latest).
2369 (let ((latest (nth 1 (car cmds))))
2370 (dolist (cmd (prog1 (cdr cmds) (setq cmds (list (car cmds)))))
2371 (if (equal latest (nth 1 cmd))
2372 (push cmd cmds)
2373 (unless (eq latest t) ;Can't beat that!
2374 (if (or (not (stringp latest))
2375 (eq (nth 1 cmd) t)
2376 (and (stringp (nth 1 cmd))
2377 (file-newer-than-file-p
2378 (format-spec (nth 1 cmd) fspec)
2379 (format-spec latest fspec))))
2380 (setq latest (nth 1 cmd) cmds (list cmd)))))))
2381 ;; Expand the command spec into the actual text.
2382 (dolist (cmd (prog1 cmds (setq cmds nil)))
2383 (push (cons (eval (car cmd)) (cdr cmd)) cmds))
2384 ;; Select the favorite command from the history.
2385 (let ((hist tex-compile-history)
2386 re hist-cmd)
2387 (while hist
2388 (setq hist-cmd (pop hist))
2389 (setq re (concat "\\`"
2390 (regexp-quote (tex-command-executable hist-cmd))
2391 "\\([ \t]\\|\\'\\)"))
2392 (dolist (cmd cmds)
2393 ;; If the hist entry uses the same command and applies to a file
2394 ;; of the same type (e.g. `gv %r.pdf' vs `gv %r.ps'), select cmd.
2395 (and (string-match re (car cmd))
2396 (or (not (string-match "%[fr]\\([-._[:alnum:]]+\\)" (car cmd)))
2397 (string-match (regexp-quote (match-string 1 (car cmd)))
2398 hist-cmd))
2399 (setq hist nil cmds (list cmd)))))
2400 ;; Substitute and return.
2401 (if (and hist-cmd
2402 (string-match (concat "[' \t\"]" (format-spec "%r" fspec)
2403 "\\([;&' \t\"]\\|\\'\\)")
2404 hist-cmd))
2405 ;; The history command was already applied to the same file,
2406 ;; so just reuse it.
2407 hist-cmd
2408 (if cmds (tex-format-cmd (caar cmds) fspec))))))
2410 (defun tex-cmd-doc-view (file)
2411 (pop-to-buffer (find-file-noselect file)))
2413 (defun tex-compile (dir cmd)
2414 "Run a command CMD on current TeX buffer's file in DIR."
2415 ;; FIXME: Use time-stamps on files to decide the next op.
2416 (interactive
2417 (let* ((file (tex-main-file))
2418 (default-directory
2419 (prog1 (file-name-directory (expand-file-name file))
2420 (setq file (file-name-nondirectory file))))
2421 (root (file-name-sans-extension file))
2422 (fspec (list (cons ?r (shell-quote-argument root))
2423 (cons ?f (shell-quote-argument file))))
2424 (default (tex-compile-default fspec)))
2425 (list default-directory
2426 (completing-read
2427 (format "Command [%s]: " (tex-summarize-command default))
2428 (mapcar (lambda (x)
2429 (list (tex-format-cmd (eval (car x)) fspec)))
2430 tex-compile-commands)
2431 nil nil nil 'tex-compile-history default))))
2432 (save-some-buffers (not compilation-ask-about-save) nil)
2433 (let ((f (and (string-match "^\\\\\\([^ \t\n]+\\)" cmd)
2434 (intern-soft (concat "tex-cmd-" (match-string 1 cmd))))))
2435 (if (functionp f)
2436 (condition-case nil
2437 (let ((default-directory dir))
2438 (apply f (split-string-and-unquote
2439 (substring cmd (match-end 0)))))
2440 (wrong-number-of-arguments
2441 (error "Wrong number of arguments to %s"
2442 (substring (symbol-name f) 8))))
2443 (if (tex-shell-running)
2444 (tex-kill-job)
2445 (tex-start-shell))
2446 (tex-send-tex-command cmd dir))))
2448 (defun tex-start-tex (command file &optional dir)
2449 "Start a TeX run, using COMMAND on FILE."
2450 (let* ((star (string-match "\\*" command))
2451 (compile-command
2452 (if star
2453 (concat (substring command 0 star)
2454 (shell-quote-argument file)
2455 (substring command (1+ star)))
2456 (concat command " "
2457 tex-start-options
2458 (if (< 0 (length tex-start-commands))
2459 (concat
2460 (shell-quote-argument tex-start-commands) " "))
2461 (shell-quote-argument file)))))
2462 (tex-send-tex-command compile-command dir)))
2464 (defun tex-send-tex-command (cmd &optional dir)
2465 (unless (or (equal dir (let ((buf (tex-shell-buf-no-error)))
2466 (and buf (with-current-buffer buf
2467 default-directory))))
2468 (not dir))
2469 (let (shell-dirtrack-verbose)
2470 (tex-send-command tex-shell-cd-command dir)))
2471 (with-current-buffer (process-buffer (tex-send-command cmd))
2472 (setq compilation-last-buffer (current-buffer))
2473 (compilation-forget-errors)
2474 ;; Don't parse previous compilations.
2475 (set-marker compilation-parsing-end (1- (point-max))))
2476 (tex-display-shell)
2477 (setq tex-last-buffer-texed (current-buffer)))
2479 ;;; The commands:
2481 (defun tex-region (beg end)
2482 "Run TeX on the current region, via a temporary file.
2483 The file's name comes from the variable `tex-zap-file' and the
2484 variable `tex-directory' says where to put it.
2486 If the buffer has a header, the header is given to TeX before the
2487 region itself. The buffer's header is all lines between the strings
2488 defined by `tex-start-of-header' and `tex-end-of-header' inclusive.
2489 The header must start in the first 100 lines of the buffer.
2491 The value of `tex-trailer' is given to TeX as input after the region.
2493 The value of `tex-command' specifies the command to use to run TeX."
2494 (interactive "r")
2495 (if (tex-shell-running)
2496 (tex-kill-job)
2497 (tex-start-shell))
2498 (or tex-zap-file
2499 (setq tex-zap-file (tex-generate-zap-file-name)))
2500 ;; Temp file will be written and TeX will be run in zap-directory.
2501 ;; If the TEXINPUTS file has relative directories or if the region has
2502 ;; \input of files, this must be the same directory as the file for
2503 ;; TeX to access the correct inputs. That's why it's safest if
2504 ;; tex-directory is ".".
2505 (let* ((zap-directory
2506 (file-name-as-directory (expand-file-name tex-directory)))
2507 (tex-out-file (expand-file-name (concat tex-zap-file ".tex")
2508 zap-directory))
2509 (main-file (expand-file-name (tex-main-file)))
2510 (ismain (string-equal main-file (buffer-file-name)))
2511 already-output)
2512 ;; Don't delete temp files if we do the same buffer twice in a row.
2513 (or (eq (current-buffer) tex-last-buffer-texed)
2514 (tex-delete-last-temp-files t))
2515 (let ((default-directory zap-directory)) ; why?
2516 ;; We assume the header is fully contained in tex-main-file.
2517 ;; We use f-f-ns so we get prompted about any changes on disk.
2518 (with-current-buffer (find-file-noselect main-file)
2519 (setq already-output (tex-region-header tex-out-file
2520 (and ismain beg))))
2521 ;; Write out the specified region (but don't repeat anything
2522 ;; already written in the header).
2523 (write-region (if ismain
2524 (max beg already-output)
2525 beg)
2526 end tex-out-file (not (zerop already-output)))
2527 ;; Write the trailer, if any.
2528 ;; Precede it with a newline to make sure it
2529 ;; is not hidden in a comment.
2530 (if tex-trailer
2531 (write-region (concat "\n" tex-trailer) nil
2532 tex-out-file t)))
2533 ;; Record the file name to be deleted afterward.
2534 (setq tex-last-temp-file tex-out-file)
2535 ;; Use a relative file name here because (1) the proper dir
2536 ;; is already current, and (2) the abs file name is sometimes
2537 ;; too long and can make tex crash.
2538 (tex-start-tex tex-command (concat tex-zap-file ".tex") zap-directory)
2539 (setq tex-print-file tex-out-file)))
2541 (defun tex-region-header (file &optional beg)
2542 "If there is a TeX header in the current buffer, write it to FILE.
2543 Return point at the end of the region so written, or zero. If
2544 the optional buffer position BEG is specified, then the region
2545 written out starts at BEG, if this lies before the start of the header.
2547 If the first line matches `tex-first-line-header-regexp', it is
2548 also written out. The variables `tex-start-of-header' and
2549 `tex-end-of-header' are used to locate the header. Note that the
2550 start of the header is required to be within the first 100 lines."
2551 (save-excursion
2552 (save-restriction
2553 (widen)
2554 (goto-char (point-min))
2555 (let ((search-end (save-excursion
2556 (forward-line 100)
2557 (point)))
2558 (already-output 0)
2559 hbeg hend)
2560 ;; Maybe copy first line, such as `\input texinfo', to temp file.
2561 (and tex-first-line-header-regexp
2562 (looking-at tex-first-line-header-regexp)
2563 (write-region (point)
2564 (progn (forward-line 1)
2565 (setq already-output (point)))
2566 file))
2567 ;; Write out the header, if there is one, and any of the
2568 ;; specified region which extends before it. But don't repeat
2569 ;; anything already written.
2570 (and tex-start-of-header
2571 (re-search-forward tex-start-of-header search-end t)
2572 (progn
2573 (beginning-of-line)
2574 (setq hbeg (point)) ; mark beginning of header
2575 (when (re-search-forward tex-end-of-header nil t)
2576 (forward-line 1)
2577 (setq hend (point)) ; mark end of header
2578 (write-region
2579 (max (if beg
2580 (min hbeg beg)
2581 hbeg)
2582 already-output)
2583 hend file (not (zerop already-output)))
2584 (setq already-output hend))))
2585 already-output))))
2587 (defun tex-buffer ()
2588 "Run TeX on current buffer. See \\[tex-region] for more information.
2589 Does not save the buffer, so it's useful for trying experimental versions.
2590 See \\[tex-file] for an alternative."
2591 (interactive)
2592 (tex-region (point-min) (point-max)))
2594 (defun tex-file ()
2595 "Prompt to save all buffers and run TeX (or LaTeX) on current buffer's file.
2596 This function is more useful than \\[tex-buffer] when you need the
2597 `.aux' file of LaTeX to have the correct name."
2598 (interactive)
2599 (when tex-offer-save
2600 (save-some-buffers))
2601 (let* ((source-file (tex-main-file))
2602 (file-dir (file-name-directory (expand-file-name source-file))))
2603 (if (tex-shell-running)
2604 (tex-kill-job)
2605 (tex-start-shell))
2606 (tex-start-tex tex-command source-file file-dir)
2607 (setq tex-print-file (expand-file-name source-file))))
2609 (defun tex-generate-zap-file-name ()
2610 "Generate a unique name suitable for use as a file name."
2611 ;; Include the shell process number and host name
2612 ;; in case there are multiple shells (for same or different user).
2613 ;; Dec 1998: There is a report that some versions of xdvi
2614 ;; don't work with file names that start with #.
2615 (format "_TZ_%d-%s"
2616 (process-id (get-buffer-process "*tex-shell*"))
2617 (subst-char-in-string ?. ?- (system-name))))
2619 ;; This will perhaps be useful for modifying TEXINPUTS.
2620 ;; Expand each file name, separated by colons, in the string S.
2621 (defun tex-expand-files (s)
2622 (let (elts (start 0))
2623 (while (string-match ":" s start)
2624 (setq elts (cons (substring s start (match-beginning 0)) elts))
2625 (setq start (match-end 0)))
2626 (or (= start 0)
2627 (setq elts (cons (substring s start) elts)))
2628 (mapconcat (lambda (elt)
2629 (if (= (length elt) 0) elt (expand-file-name elt)))
2630 (nreverse elts) ":")))
2632 (defun tex-shell-running ()
2633 (let ((proc (get-process "tex-shell")))
2634 (when proc
2635 (if (and (eq (process-status proc) 'run)
2636 (buffer-live-p (process-buffer proc)))
2637 ;; return the TeX process on success
2638 proc
2639 ;; get rid of the process permanently
2640 ;; this should get rid of the annoying w32 problem with
2641 ;; dead tex-shell buffer and live process
2642 (delete-process proc)))))
2644 (defun tex-kill-job ()
2645 "Kill the currently running TeX job."
2646 (interactive)
2647 ;; `quit-process' leads to core dumps of the tex process (except if
2648 ;; coredumpsize has limit 0kb as on many environments). One would
2649 ;; like to use (kill-process proc 'lambda), however that construct
2650 ;; does not work on some systems and kills the shell itself.
2651 (let ((proc (get-process "tex-shell")))
2652 (when proc (quit-process proc t))))
2654 (defun tex-recenter-output-buffer (linenum)
2655 "Redisplay buffer of TeX job output so that most recent output can be seen.
2656 The last line of the buffer is displayed on
2657 line LINE of the window, or centered if LINE is nil."
2658 (interactive "P")
2659 (let ((tex-shell (get-buffer "*tex-shell*"))
2660 (window))
2661 (if (null tex-shell)
2662 (message "No TeX output buffer")
2663 (setq window (display-buffer tex-shell))
2664 (with-selected-window window
2665 (bury-buffer tex-shell)
2666 (goto-char (point-max))
2667 (recenter (if linenum
2668 (prefix-numeric-value linenum)
2669 (/ (window-height) 2)))))))
2671 (defcustom tex-print-file-extension ".dvi"
2672 "The TeX-compiled file extension for viewing and printing.
2673 If you use pdflatex instead of latex, set this to \".pdf\" and modify
2674 `tex-dvi-view-command' and `tex-dvi-print-command' appropriately."
2675 :type 'string
2676 :group 'tex-view
2677 :version "25.1")
2679 (defun tex-print (&optional alt)
2680 "Print the .dvi file made by \\[tex-region], \\[tex-buffer] or \\[tex-file].
2681 Runs the shell command defined by `tex-dvi-print-command'. If prefix argument
2682 is provided, use the alternative command, `tex-alt-dvi-print-command'."
2683 (interactive "P")
2684 (let ((print-file-name-dvi (tex-append tex-print-file
2685 tex-print-file-extension))
2686 test-name)
2687 (if (and (not (equal (current-buffer) tex-last-buffer-texed))
2688 (buffer-file-name)
2689 ;; Check that this buffer's printed file is up to date.
2690 (file-newer-than-file-p
2691 (setq test-name (tex-append (buffer-file-name)
2692 tex-print-file-extension))
2693 (buffer-file-name)))
2694 (setq print-file-name-dvi test-name))
2695 (if (not (file-exists-p print-file-name-dvi))
2696 (error "No appropriate `.dvi' file could be found")
2697 (if (tex-shell-running)
2698 (tex-kill-job)
2699 (tex-start-shell))
2700 (tex-send-command
2701 (if alt tex-alt-dvi-print-command tex-dvi-print-command)
2702 print-file-name-dvi
2703 t))))
2705 (defun tex-alt-print ()
2706 "Print the .dvi file made by \\[tex-region], \\[tex-buffer] or \\[tex-file].
2707 Runs the shell command defined by `tex-alt-dvi-print-command'."
2708 (interactive)
2709 (tex-print t))
2711 (defun tex-view ()
2712 "Preview the last `.dvi' file made by running TeX under Emacs.
2713 This means, made using \\[tex-region], \\[tex-buffer] or \\[tex-file].
2714 The variable `tex-dvi-view-command' specifies the shell command for preview.
2715 You must set that variable yourself before using this command,
2716 because there is no standard value that would generally work."
2717 (interactive)
2718 (or tex-dvi-view-command
2719 (error "You must set `tex-dvi-view-command'"))
2720 ;; Restart the TeX shell if necessary.
2721 (or (tex-shell-running)
2722 (tex-start-shell))
2723 (let ((tex-dvi-print-command (eval tex-dvi-view-command)))
2724 (tex-print)))
2726 (defun tex-append (file-name suffix)
2727 "Append to FILENAME the suffix SUFFIX, using same algorithm TeX uses.
2728 Pascal-based TeX scans for the first period, C TeX uses the last.
2729 No period is retained immediately before SUFFIX,
2730 so normally SUFFIX starts with one."
2731 (if (stringp file-name)
2732 (let ((file (file-name-nondirectory file-name))
2733 trial-name)
2734 ;; Try splitting on last period.
2735 ;; The first-period split can get fooled when two files
2736 ;; named a.tex and a.b.tex are both tex'd;
2737 ;; the last-period split must be right if it matches at all.
2738 (setq trial-name
2739 (concat (file-name-directory file-name)
2740 (substring file 0
2741 (string-match "\\.[^.]*$" file))
2742 suffix))
2743 (if (or (file-exists-p trial-name)
2744 (file-exists-p (concat trial-name ".aux"))) ;for BibTeX files
2745 trial-name
2746 ;; Not found, so split on first period.
2747 (concat (file-name-directory file-name)
2748 (substring file 0
2749 (string-match "\\." file))
2750 suffix)))
2751 " "))
2753 (defun tex-show-print-queue ()
2754 "Show the print queue that \\[tex-print] put your job on.
2755 Runs the shell command defined by `tex-show-queue-command'."
2756 (interactive)
2757 (if (tex-shell-running)
2758 (tex-kill-job)
2759 (tex-start-shell))
2760 (tex-send-command tex-show-queue-command)
2761 (tex-display-shell))
2763 (defun tex-bibtex-file ()
2764 "Run BibTeX on the current buffer's file."
2765 (interactive)
2766 (if (tex-shell-running)
2767 (tex-kill-job)
2768 (tex-start-shell))
2769 (let* (shell-dirtrack-verbose
2770 (source-file (expand-file-name (tex-main-file)))
2771 (tex-out-file
2772 (tex-append (file-name-nondirectory source-file) ""))
2773 (file-dir (file-name-directory source-file)))
2774 (tex-send-command tex-shell-cd-command file-dir)
2775 (tex-send-command tex-bibtex-command tex-out-file))
2776 (tex-display-shell))
2778 ;;;;
2779 ;;;; LaTeX indentation
2780 ;;;;
2782 (defvar tex-indent-allhanging t)
2783 (defvar tex-indent-arg 4)
2784 (defvar tex-indent-basic 2)
2785 (defvar tex-indent-item tex-indent-basic)
2786 (defvar tex-indent-item-re "\\\\\\(bib\\)?item\\>")
2787 (defvar latex-noindent-environments '("document"))
2788 (put 'latex-noindent-environments 'safe-local-variable
2789 (lambda (x) (null (delq t (mapcar #'stringp x)))))
2791 (defvar tex-latex-indent-syntax-table
2792 (let ((st (make-syntax-table tex-mode-syntax-table)))
2793 (modify-syntax-entry ?$ "." st)
2794 (modify-syntax-entry ?\( "." st)
2795 (modify-syntax-entry ?\) "." st)
2797 "Syntax table used while computing indentation.")
2799 (defun latex-indent (&optional _arg)
2800 (if (and (eq (get-text-property (if (and (eobp) (bolp))
2801 (max (point-min) (1- (point)))
2802 (line-beginning-position))
2803 'face)
2804 'tex-verbatim))
2805 'noindent
2806 (with-syntax-table tex-latex-indent-syntax-table
2807 ;; TODO: Rather than ignore $, we should try to be more clever about it.
2808 (let ((indent
2809 (save-excursion
2810 (beginning-of-line)
2811 (latex-find-indent))))
2812 (if (< indent 0) (setq indent 0))
2813 (if (<= (current-column) (current-indentation))
2814 (indent-line-to indent)
2815 (save-excursion (indent-line-to indent)))))))
2817 (defcustom latex-indent-within-escaped-parens nil
2818 "Non-nil means add extra indent to text within escaped parens.
2819 When this is non-nil, text within matching pairs of escaped
2820 parens is indented at the column following the open paren. The
2821 default value does not add any extra indent thus providing the
2822 behavior of Emacs 22 and earlier."
2823 :type 'boolean
2824 :group 'tex
2825 :version "23.1")
2827 (defun latex-find-indent (&optional virtual)
2828 "Find the proper indentation of text after point.
2829 VIRTUAL if non-nil indicates that we're only trying to find the indentation
2830 in order to determine the indentation of something else.
2831 There might be text before point."
2832 (let ((latex-handle-escaped-parens latex-indent-within-escaped-parens))
2833 (save-excursion
2834 (skip-chars-forward " \t")
2836 ;; Stick the first line at column 0.
2837 (and (= (point-min) (line-beginning-position)) 0)
2838 ;; Trust the current indentation, if such info is applicable.
2839 (and virtual (save-excursion (skip-chars-backward " \t&") (bolp))
2840 (current-column))
2841 ;; Stick verbatim environments to the left margin.
2842 (and (looking-at "\\\\\\(begin\\|end\\) *{\\([^\n}]+\\)")
2843 (member (match-string 2) tex-verbatim-environments)
2845 ;; Put leading close-paren where the matching open paren would be.
2846 (let (escaped)
2847 (and (or (eq (latex-syntax-after) ?\))
2848 ;; Try to handle escaped close parens but keep
2849 ;; original position if it doesn't work out.
2850 (and latex-handle-escaped-parens
2851 (setq escaped (looking-at "\\\\\\([])}]\\)"))))
2852 (ignore-errors
2853 (save-excursion
2854 (when escaped
2855 (goto-char (match-beginning 1)))
2856 (latex-skip-close-parens)
2857 (latex-backward-sexp-1)
2858 (latex-find-indent 'virtual)))))
2859 ;; Default (maybe an argument)
2860 (let ((pos (point))
2861 ;; Outdent \item if necessary.
2862 (indent (if (looking-at tex-indent-item-re) (- tex-indent-item) 0))
2863 up-list-pos)
2864 ;; Find the previous point which determines our current indentation.
2865 (condition-case err
2866 (progn
2867 (latex-backward-sexp-1)
2868 (while (> (current-column) (current-indentation))
2869 (latex-backward-sexp-1)))
2870 (scan-error
2871 (setq up-list-pos (nth 2 err))))
2872 (cond
2873 ((= (point-min) pos) 0) ; We're really just indenting the first line.
2874 ((integerp up-list-pos)
2875 ;; Have to indent relative to the open-paren.
2876 (goto-char up-list-pos)
2877 (if (and (not tex-indent-allhanging)
2878 (save-excursion
2879 ;; Make sure we're an argument to a macro and
2880 ;; that the macro is at the beginning of a line.
2881 (condition-case nil
2882 (progn
2883 (while (eq (char-syntax (char-after)) ?\()
2884 (forward-sexp -1))
2885 (and (eq (char-syntax (char-after)) ?/)
2886 (progn (skip-chars-backward " \t&")
2887 (bolp))))
2888 (scan-error nil)))
2889 (> pos (progn (latex-down-list)
2890 (forward-comment (point-max))
2891 (point))))
2892 ;; Align with the first element after the open-paren.
2893 (current-column)
2894 ;; We're the first element after a hanging brace.
2895 (goto-char up-list-pos)
2896 (+ (if (and (looking-at "\\\\begin *{\\([^\n}]+\\)")
2897 (member (match-string 1)
2898 latex-noindent-environments))
2899 0 tex-indent-basic)
2900 indent (latex-find-indent 'virtual))))
2901 ;; We're now at the "beginning" of a line.
2902 ((not (and (not virtual) (eq (char-after) ?\\)))
2903 ;; Nothing particular here: just keep the same indentation.
2904 (+ indent (current-column)))
2905 ;; We're now looking at a macro call.
2906 ((looking-at tex-indent-item-re)
2907 ;; Indenting relative to an item, have to re-add the outdenting.
2908 (+ indent (current-column) tex-indent-item))
2910 (let ((col (current-column)))
2911 (if (or (not (eq (char-syntax (or (char-after pos) ?\s)) ?\())
2912 ;; Can't be an arg if there's an empty line in between.
2913 (save-excursion (re-search-forward "^[ \t]*$" pos t)))
2914 ;; If the first char was not an open-paren, there's
2915 ;; a risk that this is really not an argument to the
2916 ;; macro at all.
2917 (+ indent col)
2918 (forward-sexp 1)
2919 (if (< (line-end-position)
2920 (save-excursion (forward-comment (point-max))
2921 (point)))
2922 ;; we're indenting the first argument.
2923 (min (current-column) (+ tex-indent-arg col))
2924 (skip-syntax-forward " ")
2925 (current-column)))))))))))
2926 ;;; DocTeX support
2928 (defun doctex-font-lock-^^A ()
2929 (if (eq (char-after (line-beginning-position)) ?\%)
2930 (progn
2931 (put-text-property
2932 (1- (match-beginning 1)) (match-beginning 1)
2933 'syntax-table
2934 (if (= (1+ (line-beginning-position)) (match-beginning 1))
2935 ;; The `%' is a single-char comment, which Emacs
2936 ;; syntax-table can't deal with. We could turn it
2937 ;; into a non-comment, or use `\n%' or `%^' as the comment.
2938 ;; Instead, we include it in the ^^A comment.
2939 (string-to-syntax "< b")
2940 (string-to-syntax ">")))
2941 (let ((end (line-end-position)))
2942 (if (< end (point-max))
2943 (put-text-property
2944 end (1+ end)
2945 'syntax-table
2946 (string-to-syntax "> b"))))
2947 (string-to-syntax "< b"))))
2949 (defun doctex-font-lock-syntactic-face-function (state)
2950 ;; Mark DocTeX documentation, which is parsed as a style A comment
2951 ;; starting in column 0.
2952 (if (or (nth 3 state) (nth 7 state)
2953 (not (memq (char-before (nth 8 state))
2954 '(?\n nil))))
2955 ;; Anything else is just as for LaTeX.
2956 (tex-font-lock-syntactic-face-function state)
2957 font-lock-doc-face))
2959 (eval-when-compile
2960 (defconst doctex-syntax-propertize-rules
2961 (syntax-propertize-precompile-rules
2962 latex-syntax-propertize-rules
2963 ;; For DocTeX comment-in-doc.
2964 ("\\(\\^\\)\\^A" (1 (doctex-font-lock-^^A))))))
2966 (defvar doctex-font-lock-keywords
2967 (append tex-font-lock-keywords
2968 '(("^%<[^>]*>" (0 font-lock-preprocessor-face t)))))
2970 ;;;###autoload
2971 (define-derived-mode doctex-mode latex-mode "DocTeX"
2972 "Major mode to edit DocTeX files."
2973 (setq font-lock-defaults
2974 (cons (append (car font-lock-defaults) '(doctex-font-lock-keywords))
2975 (mapcar
2976 (lambda (x)
2977 (pcase (car-safe x)
2978 (`font-lock-syntactic-face-function
2979 (cons (car x) 'doctex-font-lock-syntactic-face-function))
2980 (_ x)))
2981 (cdr font-lock-defaults))))
2982 (setq-local syntax-propertize-function
2983 (syntax-propertize-rules doctex-syntax-propertize-rules)))
2985 ;;; Prettify Symbols Support
2987 (defvar tex--prettify-symbols-alist
2988 '( ;; Lowercase Greek letters.
2989 ("\\alpha" . ?α)
2990 ("\\beta" . ?β)
2991 ("\\gamma" . ?γ)
2992 ("\\delta" . ?δ)
2993 ("\\epsilon" . ?ϵ)
2994 ("\\zeta" . ?ζ)
2995 ("\\eta" . ?η)
2996 ("\\theta" . ?θ)
2997 ("\\iota" . ?ι)
2998 ("\\kappa" . ?κ)
2999 ("\\lambda" . ?λ)
3000 ("\\mu" . ?μ)
3001 ("\\nu" . ?ν)
3002 ("\\xi" . ?ξ)
3003 ;; There is no \omicron because it looks like a latin o.
3004 ("\\pi" . ?π)
3005 ("\\rho" . ?ρ)
3006 ("\\sigma" . ?σ)
3007 ("\\tau" . ?τ)
3008 ("\\upsilon" . ?υ)
3009 ("\\phi" . ?ϕ)
3010 ("\\chi" . ?χ)
3011 ("\\psi" . ?ψ)
3012 ("\\omega" . ?ω)
3013 ;; Uppercase Greek letters.
3014 ("\\Gamma" . ?Γ)
3015 ("\\Delta" . ?Δ)
3016 ("\\Lambda" . ?Λ)
3017 ("\\Phi" . ?Φ)
3018 ("\\Pi" . ?Π)
3019 ("\\Psi" . ?Ψ)
3020 ("\\Sigma" . ?Σ)
3021 ("\\Theta" . ?Θ)
3022 ("\\Upsilon" . ?Υ)
3023 ("\\Xi" . ?Ξ)
3024 ("\\Omega" . ?Ω)
3026 ;; Other math symbols (taken from leim/quail/latin-ltx.el).
3027 ("\\Box" . ?□)
3028 ("\\Bumpeq" . ?≎)
3029 ("\\Cap" . ?⋒)
3030 ("\\Cup" . ?⋓)
3031 ("\\Diamond" . ?◇)
3032 ("\\Downarrow" . ?⇓)
3033 ("\\H{o}" . ?ő)
3034 ("\\Im" . ?ℑ)
3035 ("\\Join" . ?⋈)
3036 ("\\Leftarrow" . ?⇐)
3037 ("\\Leftrightarrow" . ?⇔)
3038 ("\\Ll" . ?⋘)
3039 ("\\Lleftarrow" . ?⇚)
3040 ("\\Longleftarrow" . ?⇐)
3041 ("\\Longleftrightarrow" . ?⇔)
3042 ("\\Longrightarrow" . ?⇒)
3043 ("\\Lsh" . ?↰)
3044 ("\\Re" . ?ℜ)
3045 ("\\Rightarrow" . ?⇒)
3046 ("\\Rrightarrow" . ?⇛)
3047 ("\\Rsh" . ?↱)
3048 ("\\Subset" . ?⋐)
3049 ("\\Supset" . ?⋑)
3050 ("\\Uparrow" . ?⇑)
3051 ("\\Updownarrow" . ?⇕)
3052 ("\\Vdash" . ?⊩)
3053 ("\\Vert" . ?‖)
3054 ("\\Vvdash" . ?⊪)
3055 ("\\aleph" . ?ℵ)
3056 ("\\amalg" . ?∐)
3057 ("\\angle" . ?∠)
3058 ("\\approx" . ?≈)
3059 ("\\approxeq" . ?≊)
3060 ("\\ast" . ?∗)
3061 ("\\asymp" . ?≍)
3062 ("\\backcong" . ?≌)
3063 ("\\backepsilon" . ?∍)
3064 ("\\backprime" . ?‵)
3065 ("\\backsim" . ?∽)
3066 ("\\backsimeq" . ?⋍)
3067 ("\\backslash" . ?\\)
3068 ("\\barwedge" . ?⊼)
3069 ("\\because" . ?∵)
3070 ("\\beth" . ?ℶ)
3071 ("\\between" . ?≬)
3072 ("\\bigcap" . ?⋂)
3073 ("\\bigcirc" . ?◯)
3074 ("\\bigcup" . ?⋃)
3075 ("\\bigstar" . ?★)
3076 ("\\bigtriangledown" . ?▽)
3077 ("\\bigtriangleup" . ?△)
3078 ("\\bigvee" . ?⋁)
3079 ("\\bigwedge" . ?⋀)
3080 ("\\blacklozenge" . ?✦)
3081 ("\\blacksquare" . ?▪)
3082 ("\\blacktriangle" . ?▴)
3083 ("\\blacktriangledown" . ?▾)
3084 ("\\blacktriangleleft" . ?◂)
3085 ("\\blacktriangleright" . ?▸)
3086 ("\\bot" . ?⊥)
3087 ("\\bowtie" . ?⋈)
3088 ("\\boxminus" . ?⊟)
3089 ("\\boxplus" . ?⊞)
3090 ("\\boxtimes" . ?⊠)
3091 ("\\bullet" . ?•)
3092 ("\\bumpeq" . ?≏)
3093 ("\\cap" . ?∩)
3094 ("\\cdots" . ?⋯)
3095 ("\\centerdot" . ?·)
3096 ("\\checkmark" . ?✓)
3097 ("\\chi" . ?χ)
3098 ("\\cdot" . ?⋅)
3099 ("\\cdots" . ?⋯)
3100 ("\\circ" . ?∘)
3101 ("\\circeq" . ?≗)
3102 ("\\circlearrowleft" . ?↺)
3103 ("\\circlearrowright" . ?↻)
3104 ("\\circledR" . ?®)
3105 ("\\circledS" . ?Ⓢ)
3106 ("\\circledast" . ?⊛)
3107 ("\\circledcirc" . ?⊚)
3108 ("\\circleddash" . ?⊝)
3109 ("\\clubsuit" . ?♣)
3110 ("\\coloneq" . ?≔)
3111 ("\\complement" . ?∁)
3112 ("\\cong" . ?≅)
3113 ("\\coprod" . ?∐)
3114 ("\\cup" . ?∪)
3115 ("\\curlyeqprec" . ?⋞)
3116 ("\\curlyeqsucc" . ?⋟)
3117 ("\\curlypreceq" . ?≼)
3118 ("\\curlyvee" . ?⋎)
3119 ("\\curlywedge" . ?⋏)
3120 ("\\curvearrowleft" . ?↶)
3121 ("\\curvearrowright" . ?↷)
3122 ("\\dag" . ?†)
3123 ("\\dagger" . ?†)
3124 ("\\daleth" . ?ℸ)
3125 ("\\dashv" . ?⊣)
3126 ("\\ddag" . ?‡)
3127 ("\\ddagger" . ?‡)
3128 ("\\ddots" . ?⋱)
3129 ("\\diamond" . ?⋄)
3130 ("\\diamondsuit" . ?♢)
3131 ("\\divideontimes" . ?⋇)
3132 ("\\doteq" . ?≐)
3133 ("\\doteqdot" . ?≑)
3134 ("\\dotplus" . ?∔)
3135 ("\\dotsquare" . ?⊡)
3136 ("\\downarrow" . ?↓)
3137 ("\\downdownarrows" . ?⇊)
3138 ("\\downleftharpoon" . ?⇃)
3139 ("\\downrightharpoon" . ?⇂)
3140 ("\\ell" . ?ℓ)
3141 ("\\emptyset" . ?∅)
3142 ("\\eqcirc" . ?≖)
3143 ("\\eqcolon" . ?≕)
3144 ("\\eqslantgtr" . ?⋝)
3145 ("\\eqslantless" . ?⋜)
3146 ("\\equiv" . ?≡)
3147 ("\\exists" . ?∃)
3148 ("\\fallingdotseq" . ?≒)
3149 ("\\flat" . ?♭)
3150 ("\\forall" . ?∀)
3151 ("\\frown" . ?⌢)
3152 ("\\ge" . ?≥)
3153 ("\\geq" . ?≥)
3154 ("\\geqq" . ?≧)
3155 ("\\geqslant" . ?≥)
3156 ("\\gets" . ?←)
3157 ("\\gg" . ?≫)
3158 ("\\ggg" . ?⋙)
3159 ("\\gimel" . ?ℷ)
3160 ("\\gnapprox" . ?⋧)
3161 ("\\gneq" . ?≩)
3162 ("\\gneqq" . ?≩)
3163 ("\\gnsim" . ?⋧)
3164 ("\\gtrapprox" . ?≳)
3165 ("\\gtrdot" . ?⋗)
3166 ("\\gtreqless" . ?⋛)
3167 ("\\gtreqqless" . ?⋛)
3168 ("\\gtrless" . ?≷)
3169 ("\\gtrsim" . ?≳)
3170 ("\\gvertneqq" . ?≩)
3171 ("\\hbar" . ?ℏ)
3172 ("\\heartsuit" . ?♥)
3173 ("\\hookleftarrow" . ?↩)
3174 ("\\hookrightarrow" . ?↪)
3175 ("\\iff" . ?⇔)
3176 ("\\imath" . ?ı)
3177 ("\\in" . ?∈)
3178 ("\\infty" . ?∞)
3179 ("\\int" . ?∫)
3180 ("\\intercal" . ?⊺)
3181 ("\\langle" . 10216) ; Literal ?⟨ breaks indentation.
3182 ("\\lbrace" . ?{)
3183 ("\\lbrack" . ?\[)
3184 ("\\lceil" . ?⌈)
3185 ("\\ldots" . ?…)
3186 ("\\le" . ?≤)
3187 ("\\leadsto" . ?↝)
3188 ("\\leftarrow" . ?←)
3189 ("\\leftarrowtail" . ?↢)
3190 ("\\leftharpoondown" . ?↽)
3191 ("\\leftharpoonup" . ?↼)
3192 ("\\leftleftarrows" . ?⇇)
3193 ;; ("\\leftparengtr" ?〈), see bug#12948.
3194 ("\\leftrightarrow" . ?↔)
3195 ("\\leftrightarrows" . ?⇆)
3196 ("\\leftrightharpoons" . ?⇋)
3197 ("\\leftrightsquigarrow" . ?↭)
3198 ("\\leftthreetimes" . ?⋋)
3199 ("\\leq" . ?≤)
3200 ("\\leqq" . ?≦)
3201 ("\\leqslant" . ?≤)
3202 ("\\lessapprox" . ?≲)
3203 ("\\lessdot" . ?⋖)
3204 ("\\lesseqgtr" . ?⋚)
3205 ("\\lesseqqgtr" . ?⋚)
3206 ("\\lessgtr" . ?≶)
3207 ("\\lesssim" . ?≲)
3208 ("\\lfloor" . ?⌊)
3209 ("\\lhd" . ?◁)
3210 ("\\rhd" . ?▷)
3211 ("\\ll" . ?≪)
3212 ("\\llcorner" . ?⌞)
3213 ("\\lnapprox" . ?⋦)
3214 ("\\lneq" . ?≨)
3215 ("\\lneqq" . ?≨)
3216 ("\\lnsim" . ?⋦)
3217 ("\\longleftarrow" . ?←)
3218 ("\\longleftrightarrow" . ?↔)
3219 ("\\longmapsto" . ?↦)
3220 ("\\longrightarrow" . ?→)
3221 ("\\looparrowleft" . ?↫)
3222 ("\\looparrowright" . ?↬)
3223 ("\\lozenge" . ?✧)
3224 ("\\lq" . ?‘)
3225 ("\\lrcorner" . ?⌟)
3226 ("\\ltimes" . ?⋉)
3227 ("\\lvertneqq" . ?≨)
3228 ("\\maltese" . ?✠)
3229 ("\\mapsto" . ?↦)
3230 ("\\measuredangle" . ?∡)
3231 ("\\mho" . ?℧)
3232 ("\\mid" . ?∣)
3233 ("\\models" . ?⊧)
3234 ("\\mp" . ?∓)
3235 ("\\multimap" . ?⊸)
3236 ("\\nLeftarrow" . ?⇍)
3237 ("\\nLeftrightarrow" . ?⇎)
3238 ("\\nRightarrow" . ?⇏)
3239 ("\\nVDash" . ?⊯)
3240 ("\\nVdash" . ?⊮)
3241 ("\\nabla" . ?∇)
3242 ("\\napprox" . ?≉)
3243 ("\\natural" . ?♮)
3244 ("\\ncong" . ?≇)
3245 ("\\ne" . ?≠)
3246 ("\\nearrow" . ?↗)
3247 ("\\neg" . ?¬)
3248 ("\\neq" . ?≠)
3249 ("\\nequiv" . ?≢)
3250 ("\\newline" . ?
)
3251 ("\\nexists" . ?∄)
3252 ("\\ngeq" . ?≱)
3253 ("\\ngeqq" . ?≱)
3254 ("\\ngeqslant" . ?≱)
3255 ("\\ngtr" . ?≯)
3256 ("\\ni" . ?∋)
3257 ("\\nleftarrow" . ?↚)
3258 ("\\nleftrightarrow" . ?↮)
3259 ("\\nleq" . ?≰)
3260 ("\\nleqq" . ?≰)
3261 ("\\nleqslant" . ?≰)
3262 ("\\nless" . ?≮)
3263 ("\\nmid" . ?∤)
3264 ;; ("\\not" ?̸) ;FIXME: conflict with "NOT SIGN" ¬.
3265 ("\\notin" . ?∉)
3266 ("\\nparallel" . ?∦)
3267 ("\\nprec" . ?⊀)
3268 ("\\npreceq" . ?⋠)
3269 ("\\nrightarrow" . ?↛)
3270 ("\\nshortmid" . ?∤)
3271 ("\\nshortparallel" . ?∦)
3272 ("\\nsim" . ?≁)
3273 ("\\nsimeq" . ?≄)
3274 ("\\nsubset" . ?⊄)
3275 ("\\nsubseteq" . ?⊈)
3276 ("\\nsubseteqq" . ?⊈)
3277 ("\\nsucc" . ?⊁)
3278 ("\\nsucceq" . ?⋡)
3279 ("\\nsupset" . ?⊅)
3280 ("\\nsupseteq" . ?⊉)
3281 ("\\nsupseteqq" . ?⊉)
3282 ("\\ntriangleleft" . ?⋪)
3283 ("\\ntrianglelefteq" . ?⋬)
3284 ("\\ntriangleright" . ?⋫)
3285 ("\\ntrianglerighteq" . ?⋭)
3286 ("\\nvDash" . ?⊭)
3287 ("\\nvdash" . ?⊬)
3288 ("\\nwarrow" . ?↖)
3289 ("\\odot" . ?⊙)
3290 ("\\oint" . ?∮)
3291 ("\\ominus" . ?⊖)
3292 ("\\oplus" . ?⊕)
3293 ("\\oslash" . ?⊘)
3294 ("\\otimes" . ?⊗)
3295 ("\\par" . ?
)
3296 ("\\parallel" . ?∥)
3297 ("\\partial" . ?∂)
3298 ("\\perp" . ?⊥)
3299 ("\\pitchfork" . ?⋔)
3300 ("\\prec" . ?≺)
3301 ("\\precapprox" . ?≾)
3302 ("\\preceq" . ?≼)
3303 ("\\precnapprox" . ?⋨)
3304 ("\\precnsim" . ?⋨)
3305 ("\\precsim" . ?≾)
3306 ("\\prime" . ?′)
3307 ("\\prod" . ?∏)
3308 ("\\propto" . ?∝)
3309 ("\\qed" . ?∎)
3310 ("\\qquad" . ?⧢)
3311 ("\\quad" . ?␣)
3312 ("\\rangle" . 10217) ; Literal ?⟩ breaks indentation.
3313 ("\\rbrace" . ?})
3314 ("\\rbrack" . ?\])
3315 ("\\rceil" . ?⌉)
3316 ("\\rfloor" . ?⌋)
3317 ("\\rightarrow" . ?→)
3318 ("\\rightarrowtail" . ?↣)
3319 ("\\rightharpoondown" . ?⇁)
3320 ("\\rightharpoonup" . ?⇀)
3321 ("\\rightleftarrows" . ?⇄)
3322 ("\\rightleftharpoons" . ?⇌)
3323 ;; ("\\rightparengtr" ?⦔) ;; Was ?〉, see bug#12948.
3324 ("\\rightrightarrows" . ?⇉)
3325 ("\\rightthreetimes" . ?⋌)
3326 ("\\risingdotseq" . ?≓)
3327 ("\\rtimes" . ?⋊)
3328 ("\\times" . ?×)
3329 ("\\sbs" . ?﹨)
3330 ("\\searrow" . ?↘)
3331 ("\\setminus" . ?∖)
3332 ("\\sharp" . ?♯)
3333 ("\\shortmid" . ?∣)
3334 ("\\shortparallel" . ?∥)
3335 ("\\sim" . ?∼)
3336 ("\\simeq" . ?≃)
3337 ("\\smallamalg" . ?∐)
3338 ("\\smallsetminus" . ?∖)
3339 ("\\smallsmile" . ?⌣)
3340 ("\\smile" . ?⌣)
3341 ("\\spadesuit" . ?♠)
3342 ("\\sphericalangle" . ?∢)
3343 ("\\sqcap" . ?⊓)
3344 ("\\sqcup" . ?⊔)
3345 ("\\sqsubset" . ?⊏)
3346 ("\\sqsubseteq" . ?⊑)
3347 ("\\sqsupset" . ?⊐)
3348 ("\\sqsupseteq" . ?⊒)
3349 ("\\square" . ?□)
3350 ("\\squigarrowright" . ?⇝)
3351 ("\\star" . ?⋆)
3352 ("\\straightphi" . ?φ)
3353 ("\\subset" . ?⊂)
3354 ("\\subseteq" . ?⊆)
3355 ("\\subseteqq" . ?⊆)
3356 ("\\subsetneq" . ?⊊)
3357 ("\\subsetneqq" . ?⊊)
3358 ("\\succ" . ?≻)
3359 ("\\succapprox" . ?≿)
3360 ("\\succcurlyeq" . ?≽)
3361 ("\\succeq" . ?≽)
3362 ("\\succnapprox" . ?⋩)
3363 ("\\succnsim" . ?⋩)
3364 ("\\succsim" . ?≿)
3365 ("\\sum" . ?∑)
3366 ("\\supset" . ?⊃)
3367 ("\\supseteq" . ?⊇)
3368 ("\\supseteqq" . ?⊇)
3369 ("\\supsetneq" . ?⊋)
3370 ("\\supsetneqq" . ?⊋)
3371 ("\\surd" . ?√)
3372 ("\\swarrow" . ?↙)
3373 ("\\therefore" . ?∴)
3374 ("\\thickapprox" . ?≈)
3375 ("\\thicksim" . ?∼)
3376 ("\\to" . ?→)
3377 ("\\top" . ?⊤)
3378 ("\\triangle" . ?▵)
3379 ("\\triangledown" . ?▿)
3380 ("\\triangleleft" . ?◃)
3381 ("\\trianglelefteq" . ?⊴)
3382 ("\\triangleq" . ?≜)
3383 ("\\triangleright" . ?▹)
3384 ("\\trianglerighteq" . ?⊵)
3385 ("\\twoheadleftarrow" . ?↞)
3386 ("\\twoheadrightarrow" . ?↠)
3387 ("\\ulcorner" . ?⌜)
3388 ("\\uparrow" . ?↑)
3389 ("\\updownarrow" . ?↕)
3390 ("\\upleftharpoon" . ?↿)
3391 ("\\uplus" . ?⊎)
3392 ("\\uprightharpoon" . ?↾)
3393 ("\\upuparrows" . ?⇈)
3394 ("\\urcorner" . ?⌝)
3395 ("\\u{i}" . ?ĭ)
3396 ("\\vDash" . ?⊨)
3397 ("\\varepsilon" . ?ε)
3398 ("\\varphi" . ?φ)
3399 ("\\varprime" . ?′)
3400 ("\\varpropto" . ?∝)
3401 ("\\varrho" . ?ϱ)
3402 ("\\varsigma")
3403 ("\\vartriangleleft" . ?⊲)
3404 ("\\vartriangleright" . ?⊳)
3405 ("\\vdash" . ?⊢)
3406 ("\\vdots" . ?⋮)
3407 ("\\vee" . ?∨)
3408 ("\\veebar" . ?⊻)
3409 ("\\vert" . ?|)
3410 ("\\wedge" . ?∧)
3411 ("\\wp" . ?℘)
3412 ("\\wr" . ?≀)
3413 ("\\Bbb{N}" . ?ℕ) ; AMS commands for blackboard bold
3414 ("\\Bbb{P}" . ?ℙ) ; Also sometimes \mathbb.
3415 ("\\Bbb{Q}" . ?ℚ)
3416 ("\\Bbb{R}" . ?ℝ)
3417 ("\\Bbb{Z}" . ?ℤ)
3418 ("--" . ?–)
3419 ("---" . ?—)
3420 ("\\ordfeminine" . ?ª)
3421 ("\\ordmasculine" . ?º)
3422 ("\\lambdabar" . ?ƛ)
3423 ("\\celsius" . ?℃)
3424 ("\\textmu" . ?µ)
3425 ("\\textfractionsolidus" . ?⁄)
3426 ("\\textbigcircle" . ?⃝)
3427 ("\\textmusicalnote" . ?♪)
3428 ("\\textdied" . ?✝)
3429 ("\\textcolonmonetary" . ?₡)
3430 ("\\textwon" . ?₩)
3431 ("\\textnaira" . ?₦)
3432 ("\\textpeso" . ?₱)
3433 ("\\textlira" . ?₤)
3434 ("\\textrecipe" . ?℞)
3435 ("\\textinterrobang" . ?‽)
3436 ("\\textpertenthousand" . ?‱)
3437 ("\\textbaht" . ?฿)
3438 ("\\textnumero" . ?№)
3439 ("\\textdiscount" . ?⁒)
3440 ("\\textestimated" . ?℮)
3441 ("\\textopenbullet" . ?◦)
3442 ("\\textlquill" . 8261) ; Literal ?⁅ breaks indentation.
3443 ("\\textrquill" . 8262) ; Literal ?⁆ breaks indentation.
3444 ("\\textcircledP" . ?℗)
3445 ("\\textreferencemark" . ?※))
3446 "A `prettify-symbols-alist' usable for (La)TeX modes.")
3448 (defun tex--prettify-symbols-compose-p (_start end _match)
3450 ;; If the matched symbol doesn't end in a word character, then we
3451 ;; simply allow composition. The symbol is probably something like
3452 ;; \|, \(, etc.
3453 (not (eq ?w (char-syntax (char-before end))))
3454 ;; Else we look at what follows the match in order to decide.
3455 (let* ((after-char (char-after end))
3456 (after-syntax (char-syntax after-char)))
3457 (not (or
3458 ;; Don't compose \alpha@foo.
3459 (eq after-char ?@)
3460 ;; The \alpha in \alpha2 or \alpha-\beta may be composed but
3461 ;; of course \alphax may not.
3462 (and (eq after-syntax ?w)
3463 (not (memq after-char
3464 '(?0 ?1 ?2 ?3 ?4 ?5 ?6 ?7 ?8 ?9 ?+ ?- ?' ?\"))))
3465 ;; Don't compose inside verbatim blocks.
3466 (eq 2 (nth 7 (syntax-ppss))))))))
3468 (run-hooks 'tex-mode-load-hook)
3470 (provide 'tex-mode)
3472 ;;; tex-mode.el ends here