1 ;;; tex-mode.el --- TeX, LaTeX, and SliTeX mode commands.
3 ;; Copyright (C) 1985, 86, 89, 92, 94, 95, 96, 97, 98, 1999
4 ;; Free Software Foundation, Inc.
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 2, or (at your option)
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; see the file COPYING. If not, write to the
26 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
27 ;; Boston, MA 02111-1307, USA.
31 ;; Pacify the byte-compiler
39 (defgroup tex-file nil
40 "TeX files and directories"
45 "Running external commands from TeX mode"
49 (defgroup tex-view nil
50 "Viewing and printing TeX files"
55 (defcustom tex-shell-file-name nil
56 "*If non-nil, the shell file name to run in the subshell used to run TeX."
57 :type
'(choice (const :tag
"None" nil
)
62 (defcustom tex-directory
"."
63 "*Directory in which temporary files are written.
64 You can make this `/tmp' if your TEXINPUTS has no relative directories in it
65 and you don't try to apply \\[tex-region] or \\[tex-buffer] when there are
66 `\\input' commands with relative directories."
71 (defcustom tex-first-line-header-regexp nil
72 "Regexp for matching a first line which `tex-region' should include.
73 If this is non-nil, it should be a regular expression string;
74 if it matches the first line of the file,
75 `tex-region' always includes the first line in the TeX run."
76 :type
'(choice (const :tag
"None" nil
)
81 (defcustom tex-main-file nil
82 "*The main TeX source file which includes this buffer's file.
83 The command `tex-file' runs TeX on the file specified by `tex-main-file'
84 if the variable is non-nil."
85 :type
'(choice (const :tag
"None" nil
)
90 (defcustom tex-offer-save t
91 "*If non-nil, ask about saving modified buffers before \\[tex-file] is run."
96 (defcustom tex-run-command
"tex"
97 "*Command used to run TeX subjob.
98 TeX Mode sets `tex-command' to this string.
99 See the documentation of that variable."
104 (defcustom latex-run-command
"latex"
105 "*Command used to run LaTeX subjob.
106 LaTeX Mode sets `tex-command' to this string.
107 See the documentation of that variable."
112 (defcustom slitex-run-command
"slitex"
113 "*Command used to run SliTeX subjob.
114 SliTeX Mode sets `tex-command' to this string.
115 See the documentation of that variable."
120 (defcustom tex-start-options-string
"\\nonstopmode\\input"
121 "*TeX options to use when running TeX.
122 These precede the input file name. If nil, TeX runs without option.
123 See the documentation of `tex-command'."
124 :type
'(radio (const :tag
"Interactive \(nil\)" nil
)
125 (const :tag
"Nonstop \(\"\\nonstopmode\\input\"\)"
126 "\\nonstopmode\\input")
127 (string :tag
"String at your choice"))
131 (defvar standard-latex-block-names
132 '("abstract" "array" "center" "description"
133 "displaymath" "document" "enumerate" "eqnarray"
134 "eqnarray*" "equation" "figure" "figure*"
135 "flushleft" "flushright" "itemize" "letter"
136 "list" "minipage" "picture" "quotation"
137 "quote" "slide" "sloppypar" "tabbing"
138 "table" "table*" "tabular" "tabular*"
139 "thebibliography" "theindex*" "titlepage" "trivlist"
140 "verbatim" "verbatim*" "verse")
141 "Standard LaTeX block names.")
144 (defcustom latex-block-names nil
145 "*User defined LaTeX block names.
146 Combined with `standard-latex-block-names' for minibuffer completion."
147 :type
'(repeat string
)
151 (defcustom tex-bibtex-command
"bibtex"
152 "*Command used by `tex-bibtex-file' to gather bibliographic data.
153 If this string contains an asterisk (`*'), that is replaced by the file name;
154 otherwise, the file name, preceded by blank, is added at the end."
159 (defcustom tex-dvi-print-command
"lpr -d"
160 "*Command used by \\[tex-print] to print a .dvi file.
161 If this string contains an asterisk (`*'), that is replaced by the file name;
162 otherwise, the file name, preceded by blank, is added at the end."
167 (defcustom tex-alt-dvi-print-command
"lpr -d"
168 "*Command used by \\[tex-print] with a prefix arg to print a .dvi file.
169 If this string contains an asterisk (`*'), that is replaced by the file name;
170 otherwise, the file name, preceded by blank, is added at the end.
172 If two printers are not enough of a choice, you can set the variable
173 `tex-alt-dvi-print-command' to an expression that asks what you want;
176 (setq tex-alt-dvi-print-command
177 '(format \"lpr -P%s\" (read-string \"Use printer: \")))
179 would tell \\[tex-print] with a prefix argument to ask you which printer to
181 :type
'(choice (string :tag
"Command")
182 (sexp :tag
"Expression"))
186 (defcustom tex-dvi-view-command nil
187 "*Command used by \\[tex-view] to display a `.dvi' file.
188 If this string contains an asterisk (`*'), that is replaced by the file name;
189 otherwise, the file name, preceded by blank, is added at the end.
191 This can be set conditionally so that the previewer used is suitable for the
192 window system being used. For example,
194 (setq tex-dvi-view-command
195 (if (eq window-system 'x) \"xdvi\" \"dvi2tty * | cat -s\"))
197 would tell \\[tex-view] to use xdvi under X windows and to use dvi2tty
199 :type
'(choice (const nil
) string
)
203 (defcustom tex-show-queue-command
"lpq"
204 "*Command used by \\[tex-show-print-queue] to show the print queue.
205 Should show the queue(s) that \\[tex-print] puts jobs on."
210 (defcustom tex-default-mode
'latex-mode
211 "*Mode to enter for a new file that might be either TeX or LaTeX.
212 This variable is used when it can't be determined whether the file
213 is plain TeX or LaTeX or what because the file contains no commands.
214 Normally set to either `plain-tex-mode' or `latex-mode'."
219 (defcustom tex-open-quote
"``"
220 "*String inserted by typing \\[tex-insert-quote] to open a quotation."
225 (defcustom tex-close-quote
"''"
226 "*String inserted by typing \\[tex-insert-quote] to close a quotation."
230 (defvar tex-last-temp-file nil
231 "Latest temporary file generated by \\[tex-region] and \\[tex-buffer].
232 Deleted when the \\[tex-region] or \\[tex-buffer] is next run, or when the
233 tex shell terminates.")
235 (defvar tex-command nil
236 "*Command to run TeX.
237 If this string contains an asterisk \(`*'\), that is replaced by the file name\;
238 otherwise the \(shell-quoted\) value of `tex-start-options-string' and
239 the file name are added at the end, with blanks as separators.
241 In TeX, LaTeX, and SliTeX Mode this variable becomes buffer local.
242 In these modes, use \\[set-variable] if you want to change it for the
245 (defvar tex-trailer nil
246 "String appended after the end of a region sent to TeX by \\[tex-region].")
248 (defvar tex-start-of-header nil
249 "Regular expression used by \\[tex-region] to find start of file's header.")
251 (defvar tex-end-of-header nil
252 "Regular expression used by \\[tex-region] to find end of file's header.")
254 (defvar tex-shell-cd-command
"cd"
255 "Command to give to shell running TeX to change directory.
256 The value of `tex-directory' is appended to this, separated by a space.")
258 (defvar tex-zap-file nil
259 "Temporary file name used for text being sent as input to TeX.
260 Should be a simple file name with no extension or directory specification.")
262 (defvar tex-last-buffer-texed nil
263 "Buffer which was last TeXed.")
265 (defvar tex-print-file nil
266 "File name that \\[tex-print] prints.
267 Set by \\[tex-region], \\[tex-buffer], and \\[tex-file].")
269 (defvar tex-mode-syntax-table nil
270 "Syntax table used while in TeX mode.")
276 (defcustom latex-imenu-indent-string
". "
277 "*String to add repeated in front of nested sectional units for Imenu.
278 An alternative value is \" . \", if you use a font with a narrow period."
282 (defvar latex-section-alist
283 '(("part" .
0) ("chapter" .
1)
284 ("section" .
2) ("subsection" .
3)
285 ("subsubsection" .
4)
286 ("paragraph" .
5) ("subparagraph" .
6)))
288 (defvar latex-metasection-list
289 '("documentstyle" "documentclass"
290 "begin{document}" "end{document}"
291 "appendix" "frontmatter" "mainmatter" "backmatter"))
293 (defun latex-imenu-create-index ()
294 "Generate an alist for imenu from a LaTeX buffer."
295 (let ((section-regexp
296 (concat "\\\\" (regexp-opt (mapcar 'car latex-section-alist
) t
)
299 (concat "\\\\" (regexp-opt latex-metasection-list t
)))
300 i0 menu case-fold-search
)
302 ;; Find the top-most level in this file but don't allow it to be
303 ;; any deeper than "section" (which is top-level in an article).
304 (goto-char (point-min))
305 (if (search-forward-regexp "\\\\part\\*?[ \t]*{" nil t
)
307 (if (search-forward-regexp "\\\\chapter\\*?[ \t]*{" nil t
)
311 ;; Look for chapters and sections.
312 (goto-char (point-min))
313 (while (search-forward-regexp section-regexp nil t
)
314 (let ((start (match-beginning 0))
316 (i (cdr (assoc (buffer-substring-no-properties
319 latex-section-alist
))))
323 ;; Using sexps allows some use of matching {...} inside
326 (push (cons (concat (apply 'concat
329 latex-imenu-indent-string
))
330 (buffer-substring-no-properties
336 ;; Look for included material.
337 (goto-char (point-min))
338 (while (search-forward-regexp
339 "\\\\\\(include\\|input\\|verbatiminput\\|bibliography\\)\
340 \[ \t]*{\\([^}\n]+\\)}"
342 (push (cons (concat "<<" (buffer-substring-no-properties
345 (if (= (char-after (match-beginning 1)) ?b
)
351 ;; Look for \frontmatter, \mainmatter, \backmatter, and \appendix.
352 (goto-char (point-min))
353 (while (search-forward-regexp metasection-regexp nil t
)
354 (push (cons "--" (match-beginning 0)) menu
))
356 ;; Sort in increasing buffer position order.
357 (sort menu
(function (lambda (a b
) (< (cdr a
) (cdr b
))))))))
363 (defvar latex-outline-regexp
365 (regexp-opt (append latex-metasection-list
366 (mapcar 'car latex-section-alist
)) t
)))
368 (defun latex-outline-level ()
369 (if (looking-at latex-outline-regexp
)
370 (1+ (or (cdr (assoc (match-string 1) latex-section-alist
)) -
1))
374 ;;;; Font-Lock support
377 ;(defvar tex-font-lock-keywords
378 ; ;; Regexps updated with help from Ulrik Dickow <dickow@nbi.dk>.
379 ; '(("\\\\\\(begin\\|end\\|newcommand\\){\\([a-zA-Z0-9\\*]+\\)}"
380 ; 2 font-lock-function-name-face)
381 ; ("\\\\\\(cite\\|label\\|pageref\\|ref\\){\\([^} \t\n]+\\)}"
382 ; 2 font-lock-constant-face)
383 ; ;; It seems a bit dubious to use `bold' and `italic' faces since we might
384 ; ;; not be able to display those fonts.
385 ; ("{\\\\bf\\([^}]+\\)}" 1 'bold keep)
386 ; ("{\\\\\\(em\\|it\\|sl\\)\\([^}]+\\)}" 2 'italic keep)
387 ; ("\\\\\\([a-zA-Z@]+\\|.\\)" . font-lock-keyword-face)
388 ; ("^[ \t\n]*\\\\def[\\\\@]\\(\\w+\\)" 1 font-lock-function-name-face keep))
389 ; ;; Rewritten and extended for LaTeX2e by Ulrik Dickow <dickow@nbi.dk>.
390 ; '(("\\\\\\(begin\\|end\\|newcommand\\){\\([a-zA-Z0-9\\*]+\\)}"
391 ; 2 font-lock-function-name-face)
392 ; ("\\\\\\(cite\\|label\\|pageref\\|ref\\){\\([^} \t\n]+\\)}"
393 ; 2 font-lock-constant-face)
394 ; ("^[ \t]*\\\\def\\\\\\(\\(\\w\\|@\\)+\\)" 1 font-lock-function-name-face)
395 ; "\\\\\\([a-zA-Z@]+\\|.\\)"
396 ; ;; It seems a bit dubious to use `bold' and `italic' faces since we might
397 ; ;; not be able to display those fonts.
398 ; ;; LaTeX2e: \emph{This is emphasized}.
399 ; ("\\\\emph{\\([^}]+\\)}" 1 'italic keep)
400 ; ;; LaTeX2e: \textbf{This is bold}, \textit{...}, \textsl{...}
401 ; ("\\\\text\\(\\(bf\\)\\|it\\|sl\\){\\([^}]+\\)}"
402 ; 3 (if (match-beginning 2) 'bold 'italic) keep)
403 ; ;; Old-style bf/em/it/sl. Stop at `\\' and un-escaped `&', for tables.
404 ; ("\\\\\\(\\(bf\\)\\|em\\|it\\|sl\\)\\>\\(\\([^}&\\]\\|\\\\[^\\]\\)+\\)"
405 ; 3 (if (match-beginning 2) 'bold 'italic) keep))
407 ;; Rewritten with the help of Alexandra Bac <abac@welcome.disi.unige.it>.
408 (defconst tex-font-lock-keywords-1
410 (let* (;; Names of commands whose arg should be fontified as heading, etc.
411 (headings (regexp-opt
412 '("title" "begin" "end" "chapter" "part"
413 "section" "subsection" "subsubsection"
414 "paragraph" "subparagraph" "subsubparagraph"
415 "newcommand" "renewcommand" "newenvironment"
418 (variables (regexp-opt
419 '("newcounter" "newcounter*" "setcounter" "addtocounter"
420 "setlength" "addtolength" "settowidth")
422 (includes (regexp-opt
423 '("input" "include" "includeonly" "bibliography"
424 "epsfig" "psfig" "epsf" "nofiles" "usepackage"
425 "includegraphics" "includegraphics*")
429 (opt "\\(\\[[^]]*\\]\\)?")
430 (arg "{\\(\\(?:[^{}]+\\(?:{[^}]*}\\)?\\)+\\)"))
433 (list (concat slash headings
"\\*?" opt arg
)
434 3 'font-lock-function-name-face
'prepend
)
436 (list (concat slash variables arg
) 2 'font-lock-variable-name-face
)
438 (list (concat slash includes opt arg
) 3 'font-lock-builtin-face
)
439 ;; Definitions. I think.
440 '("^[ \t]*\\\\def\\\\\\(\\(\\w\\|@\\)+\\)"
441 1 font-lock-function-name-face
)
443 "Subdued expressions to highlight in TeX modes.")
445 (defconst tex-font-lock-keywords-2
446 (append tex-font-lock-keywords-1
449 ;; Names of commands whose arg should be fontified with fonts.
450 (bold (regexp-opt '("bf" "textbf" "textsc" "textup"
451 "boldsymbol" "pmb") t
))
452 (italic (regexp-opt '("it" "textit" "textsl" "emph") t
))
453 (type (regexp-opt '("texttt" "textmd" "textrm" "textsf") t
))
455 ;; Names of commands whose arg should be fontified as a citation.
456 (citations (regexp-opt
457 '("label" "ref" "pageref" "vref" "eqref"
458 "cite" "nocite" "caption" "index" "glossary"
459 "footnote" "footnotemark" "footnotetext")
462 ;; Names of commands that should be fontified.
463 (specials (regexp-opt
465 "linebreak" "nolinebreak" "pagebreak" "nopagebreak"
466 "newline" "newpage" "clearpage" "cleardoublepage"
467 "displaybreak" "allowdisplaybreaks" "enlargethispage")
469 (general "\\([a-zA-Z@]+\\**\\|[^ \t\n]\\)")
473 (opt "\\(\\[[^]]*\\]\\)?")
474 (arg "{\\(\\(?:[^{}]+\\(?:{[^}]*}\\)?\\)+\\)"))
478 (list (concat slash citations opt arg
) 3 'font-lock-constant-face
)
480 ;; Command names, special and general.
481 (cons (concat slash specials
) 'font-lock-warning-face
)
482 (concat slash general
)
484 ;; Font environments. It seems a bit dubious to use `bold' etc. faces
485 ;; since we might not be able to display those fonts.
486 (list (concat slash bold arg
) 2 '(quote bold
) 'append
)
487 (list (concat slash italic arg
) 2 '(quote italic
) 'append
)
488 (list (concat slash type arg
) 2 '(quote bold-italic
) 'append
)
490 ;; Old-style bf/em/it/sl. Stop at `\\' and un-escaped `&', for tables.
491 (list (concat "\\\\\\(\\(bf\\)\\|em\\|it\\|sl\\)\\>"
492 "\\(\\([^}&\\]\\|\\\\[^\\]\\)+\\)")
493 3 '(if (match-beginning 2) 'bold
'italic
) 'append
)
495 "Gaudy expressions to highlight in TeX modes.")
497 (defvar tex-font-lock-keywords tex-font-lock-keywords-1
498 "Default expressions to highlight in TeX modes.")
501 (defun tex-define-common-keys (keymap)
502 "Define the keys that we want defined both in TeX mode and in the TeX shell."
503 (define-key keymap
"\C-c\C-k" 'tex-kill-job
)
504 (define-key keymap
"\C-c\C-l" 'tex-recenter-output-buffer
)
505 (define-key keymap
"\C-c\C-q" 'tex-show-print-queue
)
506 (define-key keymap
"\C-c\C-p" 'tex-print
)
507 (define-key keymap
"\C-c\C-v" 'tex-view
)
509 (define-key keymap
[menu-bar tex
] (cons "TeX" (make-sparse-keymap "TeX")))
511 (define-key keymap
[menu-bar tex tex-kill-job
] '("Tex Kill" . tex-kill-job
))
512 (define-key keymap
[menu-bar tex tex-recenter-output-buffer
]
513 '("Tex Recenter" . tex-recenter-output-buffer
))
514 (define-key keymap
[menu-bar tex tex-show-print-queue
]
515 '("Show Print Queue" . tex-show-print-queue
))
516 (define-key keymap
[menu-bar tex tex-alt-print
]
517 '("Tex Print (alt printer)" . tex-alt-print
))
518 (define-key keymap
[menu-bar tex tex-print
] '("Tex Print" . tex-print
))
519 (define-key keymap
[menu-bar tex tex-view
] '("Tex View" . tex-view
))
522 (defvar tex-mode-map nil
"Keymap for TeX mode.")
526 (setq tex-mode-map
(make-sparse-keymap))
527 (tex-define-common-keys tex-mode-map
)
528 (define-key tex-mode-map
"\"" 'tex-insert-quote
)
529 (define-key tex-mode-map
"\n" 'tex-terminate-paragraph
)
530 (define-key tex-mode-map
"\C-c}" 'up-list
)
531 (define-key tex-mode-map
"\C-c{" 'tex-insert-braces
)
532 (define-key tex-mode-map
"\C-c\C-r" 'tex-region
)
533 (define-key tex-mode-map
"\C-c\C-b" 'tex-buffer
)
534 (define-key tex-mode-map
"\C-c\C-f" 'tex-file
)
535 (define-key tex-mode-map
"\C-c\C-i" 'tex-bibtex-file
)
536 (define-key tex-mode-map
"\C-c\C-o" 'tex-latex-block
)
537 (define-key tex-mode-map
"\C-c\C-e" 'tex-close-latex-block
)
538 (define-key tex-mode-map
"\C-c\C-u" 'tex-goto-last-unclosed-latex-block
)
539 (define-key tex-mode-map
"\C-c\C-m" 'tex-feed-input
)
540 (define-key tex-mode-map
[(control return
)] 'tex-feed-input
)
541 (define-key tex-mode-map
[menu-bar tex tex-bibtex-file
]
542 '("BibTeX File" . tex-bibtex-file
))
543 (define-key tex-mode-map
[menu-bar tex tex-validate-region
]
544 '("Validate Region" . tex-validate-region
))
545 (define-key tex-mode-map
[menu-bar tex tex-validate-buffer
]
546 '("Validate Buffer" . tex-validate-buffer
))
547 (define-key tex-mode-map
[menu-bar tex tex-region
]
548 '("TeX Region" . tex-region
))
549 (define-key tex-mode-map
[menu-bar tex tex-buffer
]
550 '("TeX Buffer" . tex-buffer
))
551 (define-key tex-mode-map
[menu-bar tex tex-file
] '("TeX File" . tex-file
)))
553 (put 'tex-region
'menu-enable
'mark-active
)
554 (put 'tex-validate-region
'menu-enable
'mark-active
)
555 (put 'tex-print
'menu-enable
'(stringp tex-print-file
))
556 (put 'tex-alt-print
'menu-enable
'(stringp tex-print-file
))
557 (put 'tex-view
'menu-enable
'(stringp tex-print-file
))
558 (put 'tex-recenter-output-buffer
'menu-enable
'(get-buffer "*tex-shell*"))
559 (put 'tex-kill-job
'menu-enable
'(tex-shell-running))
561 (defvar tex-shell-map
562 (let ((m (make-sparse-keymap)))
563 (set-keymap-parent m shell-mode-map
)
564 (tex-define-common-keys m
)
566 "Keymap for the TeX shell.
567 Inherits `shell-mode-map' with a few additions.")
569 (defvar tex-face-alist
572 (bold-italic .
"{\\bi ") ; hypothetical
573 (underline .
"\\underline{")
574 (default .
"{\\rm "))
575 "Alist of face and TeX font name for facemenu.")
577 (defvar tex-latex-face-alist
578 `((italic .
"{\\em ")
580 "Alist of face and LaTeX font name for facemenu.")
582 ;;; This would be a lot simpler if we just used a regexp search,
583 ;;; but then it would be too slow.
586 "Major mode for editing files of input for TeX, LaTeX, or SliTeX.
587 Tries to determine (by looking at the beginning of the file) whether
588 this file is for plain TeX, LaTeX, or SliTeX and calls `plain-tex-mode',
589 `latex-mode', or `slitex-mode', respectively. If it cannot be determined,
590 such as if there are no commands in the file, the value of `tex-default-mode'
591 says which mode to use."
593 (let ((mode tex-default-mode
) slash comment
)
595 (goto-char (point-min))
596 (while (and (setq slash
(search-forward "\\" nil t
))
597 (setq comment
(let ((search-end (point)))
600 (search-forward "%" search-end t
))))))
601 (if (and slash
(not comment
))
602 (setq mode
(if (looking-at "documentstyle\\|documentclass\\|begin\\b\\|NeedsTeXFormat{LaTeX")
604 "document\\(style\\|class\\)\\(\\[.*\\]\\)?{slides}")
611 (defalias 'TeX-mode
'tex-mode
)
613 (defalias 'plain-TeX-mode
'plain-tex-mode
)
615 (defalias 'LaTeX-mode
'latex-mode
)
618 (define-derived-mode plain-tex-mode text-mode
"TeX"
619 "Major mode for editing files of input for plain TeX.
620 Makes $ and } display the characters they match.
621 Makes \" insert `` when it seems to be the beginning of a quotation,
622 and '' when it appears to be the end; it inserts \" only after a \\.
624 Use \\[tex-region] to run TeX on the current region, plus a \"header\"
625 copied from the top of the file (containing macro definitions, etc.),
626 running TeX under a special subshell. \\[tex-buffer] does the whole buffer.
627 \\[tex-file] saves the buffer and then processes the file.
628 \\[tex-print] prints the .dvi file made by any of these.
629 \\[tex-view] previews the .dvi file made by any of these.
630 \\[tex-bibtex-file] runs bibtex on the file of the current buffer.
632 Use \\[tex-validate-buffer] to check buffer for paragraphs containing
633 mismatched $'s or braces.
640 Command string used by \\[tex-region] or \\[tex-buffer].
642 Directory in which to create temporary files for TeX jobs
643 run by \\[tex-region] or \\[tex-buffer].
644 tex-dvi-print-command
645 Command string used by \\[tex-print] to print a .dvi file.
646 tex-alt-dvi-print-command
647 Alternative command string used by \\[tex-print] (when given a prefix
648 argument) to print a .dvi file.
650 Command string used by \\[tex-view] to preview a .dvi file.
651 tex-show-queue-command
652 Command string used by \\[tex-show-print-queue] to show the print
653 queue that \\[tex-print] put your job on.
655 Entering Plain-tex mode runs the hook `text-mode-hook', then the hook
656 `tex-mode-hook', and finally the hook `plain-tex-mode-hook'. When the
657 special subshell is initiated, the hook `tex-shell-hook' is run."
658 (tex-common-initialization)
659 (setq tex-command tex-run-command
)
660 (setq tex-start-of-header
"%\\*\\*start of header")
661 (setq tex-end-of-header
"%\\*\\*end of header")
662 (setq tex-trailer
"\\bye\n")
663 (run-hooks 'tex-mode-hook
))
666 (define-derived-mode latex-mode text-mode
"LaTeX"
667 "Major mode for editing files of input for LaTeX.
668 Makes $ and } display the characters they match.
669 Makes \" insert `` when it seems to be the beginning of a quotation,
670 and '' when it appears to be the end; it inserts \" only after a \\.
672 Use \\[tex-region] to run LaTeX on the current region, plus the preamble
673 copied from the top of the file (containing \\documentstyle, etc.),
674 running LaTeX under a special subshell. \\[tex-buffer] does the whole buffer.
675 \\[tex-file] saves the buffer and then processes the file.
676 \\[tex-print] prints the .dvi file made by any of these.
677 \\[tex-view] previews the .dvi file made by any of these.
678 \\[tex-bibtex-file] runs bibtex on the file of the current buffer.
680 Use \\[tex-validate-buffer] to check buffer for paragraphs containing
681 mismatched $'s or braces.
688 Command string used by \\[tex-region] or \\[tex-buffer].
690 Directory in which to create temporary files for LaTeX jobs
691 run by \\[tex-region] or \\[tex-buffer].
692 tex-dvi-print-command
693 Command string used by \\[tex-print] to print a .dvi file.
694 tex-alt-dvi-print-command
695 Alternative command string used by \\[tex-print] (when given a prefix
696 argument) to print a .dvi file.
698 Command string used by \\[tex-view] to preview a .dvi file.
699 tex-show-queue-command
700 Command string used by \\[tex-show-print-queue] to show the print
701 queue that \\[tex-print] put your job on.
703 Entering Latex mode runs the hook `text-mode-hook', then
704 `tex-mode-hook', and finally `latex-mode-hook'. When the special
705 subshell is initiated, `tex-shell-hook' is run."
706 (tex-common-initialization)
707 (setq tex-command latex-run-command
)
708 (setq tex-start-of-header
"\\\\documentstyle\\|\\\\documentclass")
709 (setq tex-end-of-header
"\\\\begin{document}")
710 (setq tex-trailer
"\\end{document}\n")
711 ;; A line containing just $$ is treated as a paragraph separator.
712 ;; A line starting with $$ starts a paragraph,
713 ;; but does not separate paragraphs if it has more stuff on it.
714 (setq paragraph-start
715 (concat "[ \t]*$\\|[\f%]\\|[ \t]*\\$\\$\\|"
717 "\\\\" (regexp-opt (append
718 (mapcar 'car latex-section-alist
)
719 '("begin" "label" "end"
720 "item" "bibitem" "newline" "noindent"
721 "newpage" "footnote" "marginpar"
722 "parbox" "caption")) t
)
723 "\\>\\|\\\\[a-z]*" (regexp-opt '("space" "skip" "page") t
)
725 (setq paragraph-separate
726 (concat "[ \t]*$\\|[\f%]\\|[ \t]*\\$\\$[ \t]*$\\|"
728 "\\\\" (regexp-opt (append
729 (mapcar 'car latex-section-alist
)
730 '("begin" "label" "end" )) t
)
731 "\\>\\|\\\\\\(" (regexp-opt '("item" "bibitem" "newline"
732 "noindent" "newpage" "footnote"
733 "marginpar" "parbox" "caption"))
734 "\\|[a-z]*\\(space\\|skip\\|page[a-z]*\\)"
735 "\\)[ \t]*\\($\\|%\\)"))
736 (set (make-local-variable 'imenu-create-index-function
)
737 'latex-imenu-create-index
)
738 (set (make-local-variable 'tex-face-alist
) tex-latex-face-alist
)
739 (set (make-local-variable 'fill-nobreak-predicate
)
740 'latex-fill-nobreak-predicate
)
741 (set (make-local-variable 'outline-regexp
) latex-outline-regexp
)
742 (set (make-local-variable 'outline-level
) 'latex-outline-level
)
743 (set (make-local-variable 'forward-sexp-function
) 'latex-forward-sexp
)
744 (run-hooks 'tex-mode-hook
))
747 (define-derived-mode slitex-mode latex-mode
"SliTeX"
748 "Major mode for editing files of input for SliTeX.
749 Makes $ and } display the characters they match.
750 Makes \" insert `` when it seems to be the beginning of a quotation,
751 and '' when it appears to be the end; it inserts \" only after a \\.
753 Use \\[tex-region] to run SliTeX on the current region, plus the preamble
754 copied from the top of the file (containing \\documentstyle, etc.),
755 running SliTeX under a special subshell. \\[tex-buffer] does the whole buffer.
756 \\[tex-file] saves the buffer and then processes the file.
757 \\[tex-print] prints the .dvi file made by any of these.
758 \\[tex-view] previews the .dvi file made by any of these.
759 \\[tex-bibtex-file] runs bibtex on the file of the current buffer.
761 Use \\[tex-validate-buffer] to check buffer for paragraphs containing
762 mismatched $'s or braces.
769 Command string used by \\[tex-region] or \\[tex-buffer].
771 Directory in which to create temporary files for SliTeX jobs
772 run by \\[tex-region] or \\[tex-buffer].
773 tex-dvi-print-command
774 Command string used by \\[tex-print] to print a .dvi file.
775 tex-alt-dvi-print-command
776 Alternative command string used by \\[tex-print] (when given a prefix
777 argument) to print a .dvi file.
779 Command string used by \\[tex-view] to preview a .dvi file.
780 tex-show-queue-command
781 Command string used by \\[tex-show-print-queue] to show the print
782 queue that \\[tex-print] put your job on.
784 Entering SliTeX mode runs the hook `text-mode-hook', then the hook
785 `tex-mode-hook', then the hook `latex-mode-hook', and finally the hook
786 `slitex-mode-hook'. When the special subshell is initiated, the hook
787 `tex-shell-hook' is run."
788 (setq tex-command slitex-run-command
)
789 (setq tex-start-of-header
"\\\\documentstyle{slides}\\|\\\\documentclass{slides}"))
791 (defun tex-common-initialization ()
792 (use-local-map tex-mode-map
)
793 (setq local-abbrev-table text-mode-abbrev-table
)
794 (if (null tex-mode-syntax-table
)
796 (setq tex-mode-syntax-table
(make-syntax-table))
797 (set-syntax-table tex-mode-syntax-table
)
799 (modify-syntax-entry char
".")
800 (setq char
(1+ char
)))
801 (modify-syntax-entry ?\C-
@ "w")
802 (modify-syntax-entry ?
\t " ")
803 (modify-syntax-entry ?
\n ">")
804 (modify-syntax-entry ?
\f ">")
805 (modify-syntax-entry ?$
"$$")
806 (modify-syntax-entry ?%
"<")
807 (modify-syntax-entry ?
\\ "/")
808 (modify-syntax-entry ?
\" ".")
809 (modify-syntax-entry ?
& ".")
810 (modify-syntax-entry ?_
".")
811 (modify-syntax-entry ?
@ "_")
812 (modify-syntax-entry ?~
" ")
813 (modify-syntax-entry ?
' "w"))
814 (set-syntax-table tex-mode-syntax-table
))
815 ;; Regexp isearch should accept newline and formfeed as whitespace.
816 (set (make-local-variable 'search-whitespace-regexp
) "[ \t\r\n\f]+")
817 ;; A line containing just $$ is treated as a paragraph separator.
818 (set (make-local-variable 'paragraph-start
)
819 "[ \t]*$\\|[\f\\\\%]\\|[ \t]*\\$\\$")
820 ;; A line starting with $$ starts a paragraph,
821 ;; but does not separate paragraphs if it has more stuff on it.
822 (set (make-local-variable 'paragraph-separate
)
823 "[ \t]*$\\|[\f\\\\%]\\|[ \t]*\\$\\$[ \t]*$")
824 (set (make-local-variable 'comment-start
) "%")
825 (set (make-local-variable 'comment-add
) 1)
826 (set (make-local-variable 'comment-start-skip
)
827 "\\(\\(^\\|[^\\]\\)\\(\\\\\\\\\\)*\\)\\(%+ *\\)")
828 (set (make-local-variable 'parse-sexp-ignore-comments
) t
)
829 (set (make-local-variable 'compare-windows-whitespace
)
830 'tex-categorize-whitespace
)
831 (set (make-local-variable 'facemenu-add-face-function
)
833 (let ((face-text (cdr (assq face tex-face-alist
))))
836 (error "Face %s not configured for %s mode" face mode-name
)))))
837 (set (make-local-variable 'facemenu-end-add-face
) "}")
838 (set (make-local-variable 'facemenu-remove-face-function
) t
)
839 (set (make-local-variable 'font-lock-defaults
)
840 '((tex-font-lock-keywords
841 tex-font-lock-keywords-1 tex-font-lock-keywords-2
)
842 nil nil
((?$ .
"\"")) nil
843 ;; Who ever uses that anyway ???
844 (font-lock-mark-block-function . mark-paragraph
)))
845 (make-local-variable 'tex-command
)
846 (make-local-variable 'tex-start-of-header
)
847 (make-local-variable 'tex-end-of-header
)
848 (make-local-variable 'tex-trailer
))
850 (defun tex-categorize-whitespace (backward-limit)
851 ;; compare-windows-whitespace is set to this.
852 ;; This is basically a finite-state machine.
853 ;; Returns a symbol telling how TeX would treat
854 ;; the whitespace we are looking at: null, space, or par.
855 (let ((category 'null
)
857 (skip-chars-backward " \t\n\f" backward-limit
)
859 (cond ((looking-at "[ \t]+")
860 (goto-char (match-end 0))
861 (if (eq category
'null
)
862 (setq category
'space
)))
864 (cond ((eq category
'newline
)
866 (setq not-finished nil
))
868 (setq category
'newline
) ;a strictly internal state
869 (goto-char (match-end 0)))))
872 (setq not-finished nil
))
874 (setq not-finished nil
))))
875 (skip-chars-forward " \t\n\f")
876 (if (eq category
'newline
)
877 'space
;TeX doesn't distinguish
880 (defun tex-insert-quote (arg)
881 "Insert the appropriate quote marks for TeX.
882 Inserts the value of `tex-open-quote' (normally ``) or `tex-close-quote'
883 \(normally '') depending on the context. With prefix argument, always
884 inserts \" characters."
887 (self-insert-command (prefix-numeric-value arg
))
892 (looking-at "\\s(\\|\\s \\|\\s>")))
894 ((= (preceding-char) ?
\\)
899 (defun tex-validate-buffer ()
900 "Check current buffer for paragraphs containing mismatched braces or $s.
901 Their positions are recorded in the buffer `*Occur*'.
902 To find a particular invalidity from `*Occur*', switch to that buffer
903 and type C-c C-c or click with mouse-2
904 on the line for the invalidity you want to see."
906 (let ((buffer (current-buffer))
907 (prevpos (point-min))
910 (with-output-to-temp-buffer "*Occur*"
911 (princ "Mismatches:\n")
913 (set-buffer standard-output
)
915 (setq occur-buffer buffer
)
916 (setq occur-nlines
0))
918 (goto-char (point-max))
919 (while (and (not (input-pending-p)) (not (bobp)))
922 ;; Scan the previous paragraph for invalidities.
923 (if (search-backward "\n\n" nil t
)
925 (setq prev-end
(point))
927 (goto-char (setq prev-end
(point-min))))
928 (or (tex-validate-region (point) end
)
930 (end (save-excursion (forward-line 1) (point)))
934 ;; Keep track of line number as we scan,
935 ;; in a cumulative fashion.
937 (setq linenum
(- linenum
(count-lines prevpos
(point))))
938 (setq linenum
(1+ (count-lines 1 start
))))
939 (setq prevpos
(point))
940 ;; Mention this mismatch in *Occur*.
941 ;; Since we scan from end of buffer to beginning,
942 ;; add each mismatch at the beginning of *Occur*.
944 (setq tem
(point-marker))
945 (set-buffer standard-output
)
946 (goto-char (point-min))
947 ;; Skip "Mismatches:" header line.
949 (setq num-matches
(1+ num-matches
))
950 (insert-buffer-substring buffer start end
)
951 (let (text-beg (text-end (point-marker)))
952 (forward-char (- start end
))
953 (setq text-beg
(point-marker))
954 (insert (format "%3d: " linenum
))
955 (put-text-property (marker-position text-beg
)
956 (- (marker-position text-end
) 1)
957 'mouse-face
'highlight
)
958 (put-text-property (marker-position text-beg
)
959 (- (marker-position text-end
) 1)
961 (goto-char prev-end
))))
963 (set-buffer standard-output
)
964 (if (eq num-matches
0)
967 (message "%d mismatches found" num-matches
))))))
969 (defun tex-validate-region (start end
)
970 "Check for mismatched braces or $'s in region.
971 Returns t if no mismatches. Returns nil and moves point to suspect
972 area if a mismatch is found."
974 (let ((failure-point nil
) (max-possible-sexps (- end start
)))
978 (narrow-to-region start end
)
979 ;; First check that the open and close parens balance in numbers.
981 (while (<= 0 (setq max-possible-sexps
(1- max-possible-sexps
)))
983 ;; Now check that like matches like.
985 (while (progn (skip-syntax-forward "^(")
987 (let ((match (matching-paren (following-char))))
990 (or (= (preceding-char) match
)
991 (error "Mismatched parentheses"))))
994 (skip-syntax-forward " .>")
995 (setq failure-point
(point)))))
998 (goto-char failure-point
)
1002 (defun tex-terminate-paragraph (inhibit-validation)
1003 "Insert two newlines, breaking a paragraph for TeX.
1004 Check for mismatched braces or $s in paragraph being terminated.
1005 A prefix arg inhibits the checking."
1007 (or inhibit-validation
1009 (tex-validate-region
1011 (search-backward "\n\n" nil
'move
)
1014 (message "Paragraph being closed appears to contain a mismatch"))
1017 (defun tex-insert-braces ()
1018 "Make a pair of braces and be poised to type inside of them."
1024 ;; This function is used as the value of fill-nobreak-predicate
1025 ;; in LaTeX mode. Its job is to prevent line-breaking inside
1026 ;; of a \verb construct.
1027 (defun latex-fill-nobreak-predicate ()
1028 (let ((opoint (point))
1033 (narrow-to-region (point) opoint
)
1034 (while (re-search-forward "\\\\verb\\(.\\)" nil t
)
1035 (unless (re-search-forward (regexp-quote (match-string 1)) nil t
)
1039 (defvar latex-block-default
"enumerate")
1041 ;;; Like tex-insert-braces, but for LaTeX.
1042 (define-skeleton tex-latex-block
1043 "Create a matching pair of lines \\begin[OPT]{NAME} and \\end{NAME} at point.
1044 Puts point on a blank line between them."
1045 (let ((choice (completing-read (format "LaTeX block name [%s]: "
1046 latex-block-default
)
1048 (append standard-latex-block-names
1050 nil nil nil nil latex-block-default
)))
1051 (setq latex-block-default choice
)
1052 (unless (or (member choice standard-latex-block-names
)
1053 (member choice latex-block-names
))
1054 ;; Remember new block names for later completion.
1055 (push choice latex-block-names
))
1058 ?\
[ (skeleton-read "[options]: ") & ?\
] | -
1
1064 ;;;; LaTeX syntax navigation
1067 (defun tex-last-unended-begin ()
1068 "Leave point at the beginning of the last `\\begin{...}' that is unended."
1069 (while (and (re-search-backward "\\\\\\(begin\\|end\\)\\s *{")
1070 (looking-at "\\\\end"))
1071 (tex-last-unended-begin)))
1073 (defun tex-next-unmatched-end ()
1074 "Leave point at the end of the next `\\end' that is unended."
1075 (while (and (re-search-forward "\\\\\\(begin\\|end\\)\\s *{[^}]+}")
1076 (looking-at "\\\\begin"))
1077 (tex-next-unmatched-end)))
1079 (defun tex-goto-last-unclosed-latex-block ()
1080 "Move point to the last unclosed \\begin{...}.
1081 Mark is left at original location."
1086 (tex-last-unended-begin)
1087 (error (error "Couldn't find unended \\begin")))
1088 (setq spot
(point)))
1092 (defun latex-backward-sexp-1 ()
1093 "Like (backward-sexp 1) but aware of multi-char elements."
1095 (forward-sexp-function))
1097 (if (looking-at "\\\\begin\\>")
1099 (list "Containing expression ends prematurely"
1100 (point) (prog1 (point) (goto-char pos
))))
1101 (when (eq (char-after) ?
{)
1102 (let ((newpos (point)))
1103 (when (ignore-errors (backward-sexp 1) t
)
1104 (if (looking-at "\\\\end\\>")
1105 (tex-last-unended-begin)
1106 (goto-char newpos
))))))))
1108 (defun latex-forward-sexp-1 ()
1109 "Like (forward-sexp 1) but aware of multi-char elements."
1111 (forward-sexp-function))
1113 (let ((newpos (point)))
1114 (skip-syntax-backward "/w")
1116 ((looking-at "\\\\end\\>")
1118 (list "Containing expression ends prematurely"
1121 (progn (ignore-errors (forward-sexp 2)) (point))
1123 ((looking-at "\\\\begin\\>")
1124 (goto-char (match-end 0))
1125 (tex-next-unmatched-end))
1126 (t (goto-char newpos
))))))
1128 (defun latex-forward-sexp (&optional arg
)
1129 "Like `forward-sexp' but aware of multi-char elements."
1131 (unless arg
(setq arg
1))
1132 (let ((pos (point)))
1137 (progn (latex-forward-sexp-1) (1- arg
))
1138 (progn (latex-backward-sexp-1) (1+ arg
)))))
1141 (signal (car err
) (cdr err
))))))
1143 (defun tex-close-latex-block ()
1144 "Creates an \\end{...} to match the last unclosed \\begin{...}."
1146 (let ((new-line-needed (bolp))
1150 (tex-last-unended-begin)
1151 (error (error "Couldn't find unended \\begin")))
1152 (setq indentation
(current-column))
1153 (re-search-forward "\\\\begin\\(\\s *{[^}\n]*}\\)")
1154 (setq text
(buffer-substring (match-beginning 1) (match-end 1))))
1155 (indent-to indentation
)
1156 (insert "\\end" text
)
1157 (if new-line-needed
(insert ?
\n))))
1159 ;;; Invoking TeX in an inferior shell.
1161 ;;; Why use a shell instead of running TeX directly? Because if TeX
1162 ;;; gets stuck, the user can switch to the shell window and type at it.
1164 ;;; The utility functions:
1167 (defun tex-start-shell ()
1168 (with-current-buffer
1171 (or tex-shell-file-name
(getenv "ESHELL") (getenv "SHELL") "/bin/sh")
1173 (let ((proc (get-process "tex-shell")))
1174 (set-process-sentinel proc
'tex-shell-sentinel
)
1175 (process-kill-without-query proc
)
1176 (setq comint-prompt-regexp shell-prompt-pattern
)
1177 (use-local-map tex-shell-map
)
1178 (compilation-shell-minor-mode t
)
1179 (add-hook 'comint-input-filter-functions
'shell-directory-tracker nil t
)
1180 (make-local-variable 'list-buffers-directory
)
1181 (make-local-variable 'shell-dirstack
)
1182 (make-local-variable 'shell-last-dir
)
1183 (make-local-variable 'shell-dirtrackp
)
1184 (run-hooks 'tex-shell-hook
)
1185 (while (zerop (buffer-size))
1188 (defun tex-feed-input ()
1189 "Send input to the tex shell process.
1190 In the tex buffer this can be used to continue an interactive tex run.
1191 In the tex shell buffer this command behaves like `comint-send-input'."
1193 (set-buffer (process-buffer (get-process "tex-shell")))
1195 (tex-recenter-output-buffer nil
))
1197 (defun tex-display-shell ()
1198 "Make the TeX shell buffer visible in a window."
1199 (display-buffer (process-buffer (get-process "tex-shell")))
1200 (tex-recenter-output-buffer nil
))
1202 (defun tex-shell-sentinel (proc msg
)
1203 (cond ((null (buffer-name (process-buffer proc
)))
1205 (set-process-buffer proc nil
)
1206 (tex-delete-last-temp-files))
1207 ((memq (process-status proc
) '(signal exit
))
1208 (tex-delete-last-temp-files))))
1210 (defun tex-set-buffer-directory (buffer directory
)
1211 "Set BUFFER's default directory to be DIRECTORY."
1212 (setq directory
(file-name-as-directory (expand-file-name directory
)))
1213 (if (not (file-directory-p directory
))
1214 (error "%s is not a directory" directory
)
1217 (setq default-directory directory
))))
1219 (defvar tex-send-command-modified-tick
0)
1220 (make-variable-buffer-local 'tex-send-command-modified-tick
)
1222 (defun tex-send-command (command &optional file background
)
1223 "Send COMMAND to TeX shell process, substituting optional FILE for *.
1224 Do this in background if optional BACKGROUND is t. If COMMAND has no *,
1225 FILE will be appended, preceded by a blank, to COMMAND. If FILE is nil, no
1226 substitution will be made in COMMAND. COMMAND can be any expression that
1227 evaluates to a command string.
1229 Return the process in which TeX is running."
1231 (let* ((cmd (eval command
))
1232 (proc (or (get-process "tex-shell") (error "No TeX subprocess")))
1233 (buf (process-buffer proc
))
1234 (star (string-match "\\*" cmd
))
1238 (if star
(concat (substring cmd
0 star
)
1239 file
(substring cmd
(1+ star
)))
1240 (concat cmd
" " file
))
1242 (if background
"&" ""))))
1243 ;; Switch to buffer before checking for subproc output in it.
1245 ;; If text is unchanged since previous tex-send-command,
1246 ;; we haven't got any output. So wait for output now.
1247 (if (= (buffer-modified-tick buf
) tex-send-command-modified-tick
)
1248 (accept-process-output proc
))
1249 (goto-char (process-mark proc
))
1252 (setq tex-send-command-modified-tick
(buffer-modified-tick buf
))
1255 (defun tex-delete-last-temp-files (&optional not-all
)
1256 "Delete any junk files from last temp file.
1257 If NOT-ALL is non-nil, save the `.dvi' file."
1258 (if tex-last-temp-file
1259 (let* ((dir (file-name-directory tex-last-temp-file
))
1260 (list (and (file-directory-p dir
)
1261 (file-name-all-completions
1262 (file-name-sans-extension
1263 (file-name-nondirectory tex-last-temp-file
))
1268 ;; If arg is non-nil, don't delete the .dvi file.
1269 (not (string-match "\\.dvi$" (car list
)))
1270 (delete-file (concat dir
(car list
))))
1271 (delete-file (concat dir
(car list
))))
1272 (setq list
(cdr list
))))))
1274 (add-hook 'kill-emacs-hook
'tex-delete-last-temp-files
)
1276 (defvar tex-start-tex-marker nil
1277 "Marker pointing after last TeX-running command in the TeX shell buffer.")
1279 (defun tex-main-file ()
1280 (let ((file (or tex-main-file
1281 ;; Compatibility with AUCTeX
1282 (and (boundp 'TeX-master
) (stringp TeX-master
) TeX-master
)
1283 (if (buffer-file-name)
1284 (file-relative-name (buffer-file-name))
1285 (error "Buffer is not associated with any file")))))
1286 (if (string-match "\\.tex\\'" file
)
1287 (substring file
0 (match-beginning 0))
1290 (defun tex-start-tex (command file
&optional dir
)
1291 "Start a TeX run, using COMMAND on FILE."
1292 (let* ((star (string-match "\\*" command
))
1295 (concat (substring command
0 star
)
1296 (comint-quote-filename file
)
1297 (substring command
(1+ star
)))
1299 (if (< 0 (length tex-start-options-string
))
1301 (shell-quote-argument tex-start-options-string
) " "))
1302 (comint-quote-filename file
)))))
1304 (let (shell-dirtrack-verbose)
1305 (tex-send-command tex-shell-cd-command dir
)))
1306 (with-current-buffer (process-buffer (tex-send-command compile-command
))
1309 (setq tex-start-tex-marker
(point-marker)))
1310 (make-local-variable 'compilation-parse-errors-function
)
1311 (setq compilation-parse-errors-function
'tex-compilation-parse-errors
)
1312 (compilation-forget-errors))
1314 (setq tex-last-buffer-texed
(current-buffer))))
1316 (defun tex-compilation-parse-errors (limit-search find-at-least
)
1317 "Parse the current buffer as TeX error messages.
1318 See the variable `compilation-parse-errors-function' for the interface it uses.
1320 This function parses only the last TeX compilation.
1321 It works on TeX compilations only. It is necessary for that purpose,
1322 since TeX does not put file names and line numbers on the same line as
1323 for the error messages."
1324 (require 'thingatpt
)
1325 (setq compilation-error-list nil
)
1326 (message "Parsing error messages...")
1327 (let ((default-directory ; Perhaps dir has changed meanwhile.
1328 (file-name-directory (buffer-file-name tex-last-buffer-texed
)))
1329 (old-syntax-table (syntax-table))
1330 (tex-error-parse-syntax-table (copy-syntax-table (syntax-table)))
1331 found-desired
(num-errors-found 0)
1332 last-filename last-linenum last-position
1333 begin-of-error end-of-error
)
1334 (modify-syntax-entry ?\
{ "_" tex-error-parse-syntax-table
)
1335 (modify-syntax-entry ?\
} "_" tex-error-parse-syntax-table
)
1336 (modify-syntax-entry ?\
[ "_" tex-error-parse-syntax-table
)
1337 (modify-syntax-entry ?\
] "_" tex-error-parse-syntax-table
)
1338 ;; Single quotations may appear in errors
1339 (modify-syntax-entry ?
\" "_" tex-error-parse-syntax-table
)
1340 ;; Don't parse previous compilations.
1341 (set-marker compilation-parsing-end
1342 (max compilation-parsing-end tex-start-tex-marker
))
1343 ;; Don't reparse messages already seen at last parse.
1344 (goto-char compilation-parsing-end
)
1346 (while (and (not (or found-desired
(eobp)))
1347 (prog1 (re-search-forward "^! " nil
'move
)
1348 (setq begin-of-error
(match-beginning 0)
1349 end-of-error
(match-end 0)))
1351 "^l\\.\\([0-9]+\\) \\(\\.\\.\\.\\)?\\(.*\\)$" nil
'move
))
1352 (let* ((this-error (set-marker (make-marker) begin-of-error
))
1353 (linenum (string-to-int (match-string 1)))
1354 (error-text (regexp-quote (match-string 3)))
1359 (set-syntax-table tex-error-parse-syntax-table
)
1360 (backward-up-list 1)
1361 (skip-syntax-forward "(_")
1362 (while (not (file-readable-p
1363 (thing-at-point 'filename
)))
1364 (skip-syntax-backward "(_")
1365 (backward-up-list 1)
1366 (skip-syntax-forward "(_"))
1367 (thing-at-point 'filename
))
1368 (set-syntax-table old-syntax-table
))))
1370 (or (null last-filename
)
1371 (not (string-equal last-filename filename
))))
1374 (if (equal filename
(concat tex-zap-file
".tex"))
1375 (set-buffer tex-last-buffer-texed
)
1376 (set-buffer (find-file-noselect filename
)))
1379 (goto-char last-position
)
1380 (forward-line (- linenum last-linenum
)))
1381 ;; first try a forward search for the error text,
1382 ;; then a backward search limited by the last error.
1383 (let ((starting-point (point)))
1384 (or (re-search-forward error-text nil t
)
1387 (marker-position last-position
) t
)
1388 (goto-char starting-point
)))
1390 (goto-char this-error
)
1391 (if (and compilation-error-list
1392 (or (and find-at-least
1393 (>= num-errors-found
1396 (>= end-of-error limit-search
)))
1398 (setq found-desired t
)
1399 (setq num-errors-found
(1+ num-errors-found
)
1400 last-filename filename
1401 last-linenum linenum
1402 last-position error-location
1403 compilation-error-list
; Add the new error
1404 (cons (cons this-error error-location
)
1405 compilation-error-list
))
1406 (goto-char end-of-error
)))))
1407 (set-marker compilation-parsing-end
(point))
1408 (setq compilation-error-list
(nreverse compilation-error-list
))
1409 (message "Parsing error messages...done"))
1413 (defun tex-region (beg end
)
1414 "Run TeX on the current region, via a temporary file.
1415 The file's name comes from the variable `tex-zap-file' and the
1416 variable `tex-directory' says where to put it.
1418 If the buffer has a header, the header is given to TeX before the
1419 region itself. The buffer's header is all lines between the strings
1420 defined by `tex-start-of-header' and `tex-end-of-header' inclusive.
1421 The header must start in the first 100 lines of the buffer.
1423 The value of `tex-trailer' is given to TeX as input after the region.
1425 The value of `tex-command' specifies the command to use to run TeX."
1427 (if (tex-shell-running)
1431 (setq tex-zap-file
(tex-generate-zap-file-name)))
1432 ;; Temp file will be written and TeX will be run in zap-directory.
1433 ;; If the TEXINPUTS file has relative directories or if the region has
1434 ;; \input of files, this must be the same directory as the file for
1435 ;; TeX to access the correct inputs. That's why it's safest if
1436 ;; tex-directory is ".".
1437 (let* ((zap-directory
1438 (file-name-as-directory (expand-file-name tex-directory
)))
1439 (tex-out-file (expand-file-name (concat tex-zap-file
".tex")
1441 ;; Don't delete temp files if we do the same buffer twice in a row.
1442 (or (eq (current-buffer) tex-last-buffer-texed
)
1443 (tex-delete-last-temp-files t
))
1444 ;; Write the new temp file.
1448 (goto-char (point-min))
1450 (let ((search-end (point))
1451 (default-directory zap-directory
)
1453 (goto-char (point-min))
1455 ;; Maybe copy first line, such as `\input texinfo', to temp file.
1456 (and tex-first-line-header-regexp
1457 (looking-at tex-first-line-header-regexp
)
1458 (write-region (point)
1459 (progn (forward-line 1)
1460 (setq already-output
(point)))
1461 tex-out-file nil nil
))
1463 ;; Write out the header, if there is one,
1464 ;; and any of the specified region which extends before it.
1465 ;; But don't repeat anything already written.
1466 (if (re-search-forward tex-start-of-header search-end t
)
1469 (setq hbeg
(point)) ;mark beginning of header
1470 (if (re-search-forward tex-end-of-header nil t
)
1473 (setq hend
(point)) ;mark end of header
1474 (write-region (max (min hbeg beg
) already-output
)
1477 (not (zerop already-output
)) nil
)
1478 (setq already-output hend
)))))
1480 ;; Write out the specified region
1481 ;; (but don't repeat anything already written).
1482 (write-region (max beg already-output
) end
1484 (not (zerop already-output
)) nil
))
1485 ;; Write the trailer, if any.
1486 ;; Precede it with a newline to make sure it
1487 ;; is not hidden in a comment.
1489 (write-region (concat "\n" tex-trailer
) nil
1490 tex-out-file t nil
))))
1491 ;; Record the file name to be deleted afterward.
1492 (setq tex-last-temp-file tex-out-file
)
1493 ;; Use a relative file name here because (1) the proper dir
1494 ;; is already current, and (2) the abs file name is sometimes
1495 ;; too long and can make tex crash.
1496 (tex-start-tex tex-command
(concat tex-zap-file
".tex") zap-directory
)
1497 (setq tex-print-file tex-out-file
)))
1499 (defun tex-buffer ()
1500 "Run TeX on current buffer. See \\[tex-region] for more information.
1501 Does not save the buffer, so it's useful for trying experimental versions.
1502 See \\[tex-file] for an alternative."
1504 (tex-region (point-min) (point-max)))
1507 "Prompt to save all buffers and run TeX (or LaTeX) on current buffer's file.
1508 This function is more useful than \\[tex-buffer] when you need the
1509 `.aux' file of LaTeX to have the correct name."
1511 (let* ((source-file (tex-main-file))
1512 (file-dir (expand-file-name (file-name-directory source-file
))))
1514 (save-some-buffers))
1515 (if (tex-shell-running)
1518 (tex-start-tex tex-command source-file file-dir
)
1519 (setq tex-print-file
(expand-file-name source-file
))))
1521 (defun tex-generate-zap-file-name ()
1522 "Generate a unique name suitable for use as a file name."
1523 ;; Include the shell process number and host name
1524 ;; in case there are multiple shells (for same or different user).
1525 ;; Dec 1998: There is a report that some versions of xdvi
1526 ;; don't work with file names that start with #.
1528 (process-id (get-buffer-process "*tex-shell*"))
1529 (tex-strip-dots (system-name))))
1531 (defun tex-strip-dots (s)
1532 (setq s
(copy-sequence s
))
1533 (while (string-match "\\." s
)
1534 (aset s
(match-beginning 0) ?-
))
1537 ;; This will perhaps be useful for modifying TEXINPUTS.
1538 ;; Expand each file name, separated by colons, in the string S.
1539 (defun tex-expand-files (s)
1540 (let (elts (start 0))
1541 (while (string-match ":" s start
)
1542 (setq elts
(cons (substring s start
(match-beginning 0)) elts
))
1543 (setq start
(match-end 0)))
1545 (setq elts
(cons (substring s start
) elts
)))
1546 (mapconcat (lambda (elt)
1547 (if (= (length elt
) 0) elt
(expand-file-name elt
)))
1548 (nreverse elts
) ":")))
1550 (defun tex-shell-running ()
1551 (and (get-process "tex-shell")
1552 (eq (process-status (get-process "tex-shell")) 'run
)))
1554 (defun tex-kill-job ()
1555 "Kill the currently running TeX job."
1557 ;; quit-process leads to core dumps of the tex process (except if
1558 ;; coredumpsize has limit 0kb as on many environments). One would
1559 ;; like to use (kill-process proc 'lambda), however that construct
1560 ;; does not work on some systems and kills the shell itself.
1561 (quit-process (get-process "tex-shell") t
))
1563 (defun tex-recenter-output-buffer (linenum)
1564 "Redisplay buffer of TeX job output so that most recent output can be seen.
1565 The last line of the buffer is displayed on
1566 line LINE of the window, or centered if LINE is nil."
1568 (let ((tex-shell (get-buffer "*tex-shell*"))
1569 (old-buffer (current-buffer))
1571 (if (null tex-shell
)
1572 (message "No TeX output buffer")
1573 (setq window
(display-buffer tex-shell
))
1574 (save-selected-window
1575 (select-window window
)
1576 (bury-buffer tex-shell
)
1577 (goto-char (point-max))
1578 (recenter (if linenum
1579 (prefix-numeric-value linenum
)
1580 (/ (window-height) 2)))))))
1582 (defun tex-print (&optional alt
)
1583 "Print the .dvi file made by \\[tex-region], \\[tex-buffer] or \\[tex-file].
1584 Runs the shell command defined by `tex-dvi-print-command'. If prefix argument
1585 is provided, use the alternative command, `tex-alt-dvi-print-command'."
1587 (let ((print-file-name-dvi (tex-append tex-print-file
".dvi"))
1589 (if (and (not (equal (current-buffer) tex-last-buffer-texed
))
1591 ;; Check that this buffer's printed file is up to date.
1592 (file-newer-than-file-p
1593 (setq test-name
(tex-append (buffer-file-name) ".dvi"))
1594 (buffer-file-name)))
1595 (setq print-file-name-dvi test-name
))
1596 (if (not (file-exists-p print-file-name-dvi
))
1597 (error "No appropriate `.dvi' file could be found")
1598 (if (tex-shell-running)
1602 (if alt tex-alt-dvi-print-command tex-dvi-print-command
)
1603 print-file-name-dvi t
))))
1605 (defun tex-alt-print ()
1606 "Print the .dvi file made by \\[tex-region], \\[tex-buffer] or \\[tex-file].
1607 Runs the shell command defined by `tex-alt-dvi-print-command'."
1612 "Preview the last `.dvi' file made by running TeX under Emacs.
1613 This means, made using \\[tex-region], \\[tex-buffer] or \\[tex-file].
1614 The variable `tex-dvi-view-command' specifies the shell command for preview.
1615 You must set that variable yourself before using this command,
1616 because there is no standard value that would generally work."
1618 (or tex-dvi-view-command
1619 (error "You must set `tex-dvi-view-command'"))
1620 (let ((tex-dvi-print-command tex-dvi-view-command
))
1623 (defun tex-append (file-name suffix
)
1624 "Append to FILENAME the suffix SUFFIX, using same algorithm TeX uses.
1625 Pascal-based TeX scans for the first period, C TeX uses the last.
1626 No period is retained immediately before SUFFIX,
1627 so normally SUFFIX starts with one."
1628 (if (stringp file-name
)
1629 (let ((file (file-name-nondirectory file-name
))
1631 ;; Try splitting on last period.
1632 ;; The first-period split can get fooled when two files
1633 ;; named a.tex and a.b.tex are both tex'd;
1634 ;; the last-period split must be right if it matches at all.
1636 (concat (file-name-directory file-name
)
1638 (string-match "\\.[^.]*$" file
))
1640 (if (or (file-exists-p trial-name
)
1641 (file-exists-p (concat trial-name
".aux"))) ;for BibTeX files
1643 ;; Not found, so split on first period.
1644 (concat (file-name-directory file-name
)
1646 (string-match "\\." file
))
1650 (defun tex-show-print-queue ()
1651 "Show the print queue that \\[tex-print] put your job on.
1652 Runs the shell command defined by `tex-show-queue-command'."
1654 (if (tex-shell-running)
1657 (tex-send-command tex-show-queue-command
)
1658 (tex-display-shell))
1660 (defun tex-bibtex-file ()
1661 "Run BibTeX on the current buffer's file."
1663 (if (tex-shell-running)
1666 (let (shell-dirtrack-verbose
1668 (tex-append (file-name-nondirectory (buffer-file-name)) ""))
1669 (file-dir (file-name-directory (buffer-file-name))))
1670 (tex-send-command tex-shell-cd-command file-dir
)
1671 (tex-send-command tex-bibtex-command tex-out-file
))
1672 (tex-display-shell))
1674 (run-hooks 'tex-mode-load-hook
)
1678 ;;; tex-mode.el ends here