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