Don't quote lambda.
[emacs.git] / lisp / textmodes / tex-mode.el
blob315b61a8ab2e6343acb6e879063afe76993698a7
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.
6 ;; Maintainer: FSF
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 2, or (at your option)
17 ;; 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; 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.
29 ;;; Code:
31 ;; Pacify the byte-compiler
32 (eval-when-compile
33 (require 'compare-w)
34 (require 'skeleton))
36 (require 'shell)
37 (require 'compile)
39 (defgroup tex-file nil
40 "TeX files and directories"
41 :prefix "tex-"
42 :group 'tex)
44 (defgroup tex-run nil
45 "Running external commands from TeX mode"
46 :prefix "tex-"
47 :group 'tex)
49 (defgroup tex-view nil
50 "Viewing and printing TeX files"
51 :prefix "tex-"
52 :group 'tex)
54 ;;;###autoload
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)
58 string)
59 :group 'tex-run)
61 ;;;###autoload
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."
67 :type 'directory
68 :group 'tex-file)
70 ;;;###autoload
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)
77 regexp)
78 :group 'tex-file)
80 ;;;###autoload
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)
86 file)
87 :group 'tex-file)
89 ;;;###autoload
90 (defcustom tex-offer-save t
91 "*If non-nil, ask about saving modified buffers before \\[tex-file] is run."
92 :type 'boolean
93 :group 'tex-file)
95 ;;;###autoload
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."
100 :type 'string
101 :group 'tex-run)
103 ;;;###autoload
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."
108 :type 'string
109 :group 'tex-run)
111 ;;;###autoload
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."
116 :type 'string
117 :group 'tex-run)
119 ;;;###autoload
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"))
128 :group 'tex-run
129 :version "20.4")
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.")
143 ;;;###autoload
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)
148 :group 'tex-run)
150 ;;;###autoload
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."
155 :type 'string
156 :group 'tex-run)
158 ;;;###autoload
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."
163 :type 'string
164 :group 'tex-view)
166 ;;;###autoload
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;
174 for example,
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
180 use."
181 :type '(choice (string :tag "Command")
182 (sexp :tag "Expression"))
183 :group 'tex-view)
185 ;;;###autoload
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
198 otherwise."
199 :type '(choice (const nil) string)
200 :group 'tex-view)
202 ;;;###autoload
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."
206 :type 'string
207 :group 'tex-view)
209 ;;;###autoload
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'."
215 :type 'function
216 :group 'tex)
218 ;;;###autoload
219 (defcustom tex-open-quote "``"
220 "*String inserted by typing \\[tex-insert-quote] to open a quotation."
221 :type 'string
222 :group 'tex)
224 ;;;###autoload
225 (defcustom tex-close-quote "''"
226 "*String inserted by typing \\[tex-insert-quote] to close a quotation."
227 :type 'string
228 :group 'tex)
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
243 current buffer.")
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.")
272 (defcustom latex-imenu-indent-string "."
273 "*String to add repeated in front of nested sectional units for Imenu.
274 An alternative value is \" . \", if you use a font with a narrow period."
275 :type 'string
276 :group 'tex)
278 (defun latex-imenu-create-index ()
279 "Generates an alist for imenu from a LaTeX buffer."
280 (let (i0 menu case-fold-search)
281 (save-excursion
282 ;; Find the top-most level in this file but don't allow it to be
283 ;; any deeper than "section" (which is top-level in an article).
284 (goto-char (point-min))
285 (if (search-forward-regexp "\\\\part\\*?[ \t]*{" nil t)
286 (setq i0 0)
287 (if (search-forward-regexp "\\\\chapter\\*?[ \t]*{" nil t)
288 (setq i0 1)
289 (setq i0 2)))
291 ;; Look for chapters and sections.
292 (goto-char (point-min))
293 (while (search-forward-regexp
294 "\\\\\\(part\\|chapter\\|section\\|subsection\\|\
295 subsubsection\\|paragraph\\|subparagraph\\)\\*?[ \t]*{" nil t)
296 (let ((start (match-beginning 0))
297 (here (point))
298 (i (cdr (assoc (buffer-substring-no-properties
299 (match-beginning 1)
300 (match-end 1))
301 '(("part" . 0) ("chapter" . 1)
302 ("section" . 2) ("subsection" . 3)
303 ("subsubsection" . 4)
304 ("paragraph" . 5) ("subparagraph" . 6))))))
305 (backward-char 1)
306 (condition-case err
307 (progn
308 ;; Using sexps allows some use of matching {...} inside
309 ;; titles.
310 (forward-sexp 1)
311 (setq menu
312 (cons (cons (concat (apply 'concat
313 (make-list
314 (max 0 (- i i0))
315 latex-imenu-indent-string))
316 (buffer-substring-no-properties
317 here (1- (point))))
318 start)
319 menu))
321 (error nil))))
323 ;; Look for included material.
324 (goto-char (point-min))
325 (while (search-forward-regexp
326 "\\\\\\(include\\|input\\|verbatiminput\\|bibliography\\)\
327 [ \t]*{\\([^}\n]+\\)}"
328 nil t)
329 (setq menu
330 (cons (cons (concat "<<" (buffer-substring-no-properties
331 (match-beginning 2)
332 (match-end 2))
333 (if (= (char-after (match-beginning 1)) ?b)
334 ".bbl"
335 ".tex"))
336 (match-beginning 0))
337 menu)))
339 ;; Look for \frontmatter, \mainmatter, \backmatter, and \appendix.
340 (goto-char (point-min))
341 (while (search-forward-regexp
342 "\\\\\\(frontmatter\\|mainmatter\\|backmatter\\|appendix\\)\\b"
343 nil t)
344 (setq menu
345 (cons (cons "--" (match-beginning 0))
346 menu)))
348 ;; Sort in increasing buffer position order.
349 (sort menu (function (lambda (a b) (< (cdr a) (cdr b))))))))
351 (defun tex-define-common-keys (keymap)
352 "Define the keys that we want defined both in TeX mode and in the TeX shell."
353 (define-key keymap "\C-c\C-k" 'tex-kill-job)
354 (define-key keymap "\C-c\C-l" 'tex-recenter-output-buffer)
355 (define-key keymap "\C-c\C-q" 'tex-show-print-queue)
356 (define-key keymap "\C-c\C-p" 'tex-print)
357 (define-key keymap "\C-c\C-v" 'tex-view)
359 (define-key keymap [menu-bar tex] (cons "TeX" (make-sparse-keymap "TeX")))
361 (define-key keymap [menu-bar tex tex-kill-job] '("Tex Kill" . tex-kill-job))
362 (define-key keymap [menu-bar tex tex-recenter-output-buffer]
363 '("Tex Recenter" . tex-recenter-output-buffer))
364 (define-key keymap [menu-bar tex tex-show-print-queue]
365 '("Show Print Queue" . tex-show-print-queue))
366 (define-key keymap [menu-bar tex tex-alt-print]
367 '("Tex Print (alt printer)" . tex-alt-print))
368 (define-key keymap [menu-bar tex tex-print] '("Tex Print" . tex-print))
369 (define-key keymap [menu-bar tex tex-view] '("Tex View" . tex-view))
372 (defvar tex-mode-map nil "Keymap for TeX mode.")
374 (if tex-mode-map
376 (setq tex-mode-map (make-sparse-keymap))
377 (tex-define-common-keys tex-mode-map)
378 (define-key tex-mode-map "\"" 'tex-insert-quote)
379 (define-key tex-mode-map "\n" 'tex-terminate-paragraph)
380 (define-key tex-mode-map "\C-c}" 'up-list)
381 (define-key tex-mode-map "\C-c{" 'tex-insert-braces)
382 (define-key tex-mode-map "\C-c\C-r" 'tex-region)
383 (define-key tex-mode-map "\C-c\C-b" 'tex-buffer)
384 (define-key tex-mode-map "\C-c\C-f" 'tex-file)
385 (define-key tex-mode-map "\C-c\C-i" 'tex-bibtex-file)
386 (define-key tex-mode-map "\C-c\C-o" 'tex-latex-block)
387 (define-key tex-mode-map "\C-c\C-e" 'tex-close-latex-block)
388 (define-key tex-mode-map "\C-c\C-u" 'tex-goto-last-unclosed-latex-block)
389 (define-key tex-mode-map "\C-c\C-m" 'tex-feed-input)
390 (define-key tex-mode-map [(control return)] 'tex-feed-input)
391 (define-key tex-mode-map [menu-bar tex tex-bibtex-file]
392 '("BibTeX File" . tex-bibtex-file))
393 (define-key tex-mode-map [menu-bar tex tex-validate-region]
394 '("Validate Region" . tex-validate-region))
395 (define-key tex-mode-map [menu-bar tex tex-validate-buffer]
396 '("Validate Buffer" . tex-validate-buffer))
397 (define-key tex-mode-map [menu-bar tex tex-region]
398 '("TeX Region" . tex-region))
399 (define-key tex-mode-map [menu-bar tex tex-buffer]
400 '("TeX Buffer" . tex-buffer))
401 (define-key tex-mode-map [menu-bar tex tex-file] '("TeX File" . tex-file)))
403 (put 'tex-region 'menu-enable 'mark-active)
404 (put 'tex-validate-region 'menu-enable 'mark-active)
405 (put 'tex-print 'menu-enable '(stringp tex-print-file))
406 (put 'tex-alt-print 'menu-enable '(stringp tex-print-file))
407 (put 'tex-view 'menu-enable '(stringp tex-print-file))
408 (put 'tex-recenter-output-buffer 'menu-enable '(get-buffer "*tex-shell*"))
409 (put 'tex-kill-job 'menu-enable '(tex-shell-running))
411 (defvar tex-shell-map nil
412 "Keymap for the TeX shell.
413 Inherits `shell-mode-map' with a few additions.")
415 (defvar tex-face-alist
416 '((bold . "{\\bf ")
417 (italic . "{\\it ")
418 (bold-italic . "{\\bi ") ; hypothetical
419 (underline . "\\underline{")
420 (default . "{\\rm "))
421 "Alist of face and TeX font name for facemenu.")
423 (defvar tex-latex-face-alist
424 `((italic . "{\\em ")
425 ,@tex-face-alist)
426 "Alist of face and LaTeX font name for facemenu.")
428 ;;; This would be a lot simpler if we just used a regexp search,
429 ;;; but then it would be too slow.
430 ;;;###autoload
431 (defun tex-mode ()
432 "Major mode for editing files of input for TeX, LaTeX, or SliTeX.
433 Tries to determine (by looking at the beginning of the file) whether
434 this file is for plain TeX, LaTeX, or SliTeX and calls `plain-tex-mode',
435 `latex-mode', or `slitex-mode', respectively. If it cannot be determined,
436 such as if there are no commands in the file, the value of `tex-default-mode'
437 says which mode to use."
438 (interactive)
439 (let (mode slash comment)
440 (save-excursion
441 (goto-char (point-min))
442 (while (and (setq slash (search-forward "\\" nil t))
443 (setq comment (let ((search-end (point)))
444 (save-excursion
445 (beginning-of-line)
446 (search-forward "%" search-end t))))))
447 (if (and slash (not comment))
448 (setq mode (if (looking-at "documentstyle\\|documentclass\\|begin\\b\\|NeedsTeXFormat{LaTeX")
449 (if (looking-at
450 "document\\(style\\|class\\)\\(\\[.*\\]\\)?{slides}")
451 'slitex-mode
452 'latex-mode)
453 'plain-tex-mode))))
454 (if mode (funcall mode)
455 (funcall tex-default-mode))))
457 ;;;###autoload
458 (defalias 'TeX-mode 'tex-mode)
459 ;;;###autoload
460 (defalias 'plain-TeX-mode 'plain-tex-mode)
461 ;;;###autoload
462 (defalias 'LaTeX-mode 'latex-mode)
464 ;;;###autoload
465 (defun plain-tex-mode ()
466 "Major mode for editing files of input for plain TeX.
467 Makes $ and } display the characters they match.
468 Makes \" insert `` when it seems to be the beginning of a quotation,
469 and '' when it appears to be the end; it inserts \" only after a \\.
471 Use \\[tex-region] to run TeX on the current region, plus a \"header\"
472 copied from the top of the file (containing macro definitions, etc.),
473 running TeX under a special subshell. \\[tex-buffer] does the whole buffer.
474 \\[tex-file] saves the buffer and then processes the file.
475 \\[tex-print] prints the .dvi file made by any of these.
476 \\[tex-view] previews the .dvi file made by any of these.
477 \\[tex-bibtex-file] runs bibtex on the file of the current buffer.
479 Use \\[tex-validate-buffer] to check buffer for paragraphs containing
480 mismatched $'s or braces.
482 Special commands:
483 \\{tex-mode-map}
485 Mode variables:
486 tex-run-command
487 Command string used by \\[tex-region] or \\[tex-buffer].
488 tex-directory
489 Directory in which to create temporary files for TeX jobs
490 run by \\[tex-region] or \\[tex-buffer].
491 tex-dvi-print-command
492 Command string used by \\[tex-print] to print a .dvi file.
493 tex-alt-dvi-print-command
494 Alternative command string used by \\[tex-print] (when given a prefix
495 argument) to print a .dvi file.
496 tex-dvi-view-command
497 Command string used by \\[tex-view] to preview a .dvi file.
498 tex-show-queue-command
499 Command string used by \\[tex-show-print-queue] to show the print
500 queue that \\[tex-print] put your job on.
502 Entering Plain-tex mode runs the hook `text-mode-hook', then the hook
503 `tex-mode-hook', and finally the hook `plain-tex-mode-hook'. When the
504 special subshell is initiated, the hook `tex-shell-hook' is run."
506 (interactive)
507 (tex-common-initialization)
508 (setq mode-name "TeX")
509 (setq major-mode 'plain-tex-mode)
510 (setq tex-command tex-run-command)
511 (setq tex-start-of-header "%\\*\\*start of header")
512 (setq tex-end-of-header "%\\*\\*end of header")
513 (setq tex-trailer "\\bye\n")
514 (run-hooks 'text-mode-hook 'tex-mode-hook 'plain-tex-mode-hook))
516 ;;;###autoload
517 (defun latex-mode ()
518 "Major mode for editing files of input for LaTeX.
519 Makes $ and } display the characters they match.
520 Makes \" insert `` when it seems to be the beginning of a quotation,
521 and '' when it appears to be the end; it inserts \" only after a \\.
523 Use \\[tex-region] to run LaTeX on the current region, plus the preamble
524 copied from the top of the file (containing \\documentstyle, etc.),
525 running LaTeX under a special subshell. \\[tex-buffer] does the whole buffer.
526 \\[tex-file] saves the buffer and then processes the file.
527 \\[tex-print] prints the .dvi file made by any of these.
528 \\[tex-view] previews the .dvi file made by any of these.
529 \\[tex-bibtex-file] runs bibtex on the file of the current buffer.
531 Use \\[tex-validate-buffer] to check buffer for paragraphs containing
532 mismatched $'s or braces.
534 Special commands:
535 \\{tex-mode-map}
537 Mode variables:
538 latex-run-command
539 Command string used by \\[tex-region] or \\[tex-buffer].
540 tex-directory
541 Directory in which to create temporary files for LaTeX jobs
542 run by \\[tex-region] or \\[tex-buffer].
543 tex-dvi-print-command
544 Command string used by \\[tex-print] to print a .dvi file.
545 tex-alt-dvi-print-command
546 Alternative command string used by \\[tex-print] (when given a prefix
547 argument) to print a .dvi file.
548 tex-dvi-view-command
549 Command string used by \\[tex-view] to preview a .dvi file.
550 tex-show-queue-command
551 Command string used by \\[tex-show-print-queue] to show the print
552 queue that \\[tex-print] put your job on.
554 Entering Latex mode runs the hook `text-mode-hook', then
555 `tex-mode-hook', and finally `latex-mode-hook'. When the special
556 subshell is initiated, `tex-shell-hook' is run."
557 (interactive)
558 (tex-common-initialization)
559 (setq mode-name "LaTeX")
560 (setq major-mode 'latex-mode)
561 (setq tex-command latex-run-command)
562 (setq tex-start-of-header "\\\\documentstyle\\|\\\\documentclass")
563 (setq tex-end-of-header "\\\\begin{document}")
564 (setq tex-trailer "\\end{document}\n")
565 ;; A line containing just $$ is treated as a paragraph separator.
566 ;; A line starting with $$ starts a paragraph,
567 ;; but does not separate paragraphs if it has more stuff on it.
568 (setq paragraph-start "[ \t]*$\\|[\f%]\\|[ \t]*\\$\\$\\|\
569 \\\\begin\\>\\|\\\\label\\>\\|\\\\end\\>\\|\\\\\\[\\|\\\\\\]\\|\
570 \\\\chapter\\>\\|\\\\section\\>\\|\
571 \\\\subsection\\>\\|\\\\subsubsection\\>\\|\
572 \\\\paragraph\\>\\|\\\\subparagraph\\>\\|\
573 \\\\item\\>\\|\\\\bibitem\\>\\|\\\\newline\\>\\|\\\\noindent\\>\\|\
574 \\\\[a-z]*space\\>\\|\\\\[a-z]*skip\\>\\|\
575 \\\\newpage\\>\\|\\\\[a-z]*page\\|\\\\footnote\\>\\|\
576 \\\\marginpar\\>\\|\\\\parbox\\>\\|\\\\caption\\>")
577 (setq paragraph-separate "[ \t]*$\\|[\f%]\\|[ \t]*\\$\\$[ \t]*$\\|\
578 \\\\begin\\>\\|\\\\label\\>\\|\\\\end\\>\\|\\\\\\[\\|\\\\\\]\\|\
579 \\\\chapter\\>\\|\\\\section\\>\\|\
580 \\\\subsection\\>\\|\\\\subsubsection\\>\\|\
581 \\\\paragraph\\>\\|\\\\subparagraph\\>\\|\
582 \\(\\\\item\\|\\\\bibitem\\|\\\\newline\\|\\\\noindent\\|\
583 \\\\[a-z]*space\\|\\\\[a-z]*skip\\|\
584 \\\\newpage\\|\\\\[a-z]*page[a-z]*\\|\\\\footnote\\|\
585 \\\\marginpar\\|\\\\parbox\\|\\\\caption\\)[ \t]*\\($\\|%\\)")
586 (make-local-variable 'imenu-create-index-function)
587 (setq imenu-create-index-function 'latex-imenu-create-index)
588 (make-local-variable 'tex-face-alist)
589 (setq tex-face-alist tex-latex-face-alist)
590 (make-local-variable 'fill-nobreak-predicate)
591 (setq fill-nobreak-predicate 'latex-fill-nobreak-predicate)
592 (run-hooks 'text-mode-hook 'tex-mode-hook 'latex-mode-hook))
594 ;;;###autoload
595 (defun slitex-mode ()
596 "Major mode for editing files of input for SliTeX.
597 Makes $ and } display the characters they match.
598 Makes \" insert `` when it seems to be the beginning of a quotation,
599 and '' when it appears to be the end; it inserts \" only after a \\.
601 Use \\[tex-region] to run SliTeX on the current region, plus the preamble
602 copied from the top of the file (containing \\documentstyle, etc.),
603 running SliTeX under a special subshell. \\[tex-buffer] does the whole buffer.
604 \\[tex-file] saves the buffer and then processes the file.
605 \\[tex-print] prints the .dvi file made by any of these.
606 \\[tex-view] previews the .dvi file made by any of these.
607 \\[tex-bibtex-file] runs bibtex on the file of the current buffer.
609 Use \\[tex-validate-buffer] to check buffer for paragraphs containing
610 mismatched $'s or braces.
612 Special commands:
613 \\{tex-mode-map}
615 Mode variables:
616 slitex-run-command
617 Command string used by \\[tex-region] or \\[tex-buffer].
618 tex-directory
619 Directory in which to create temporary files for SliTeX jobs
620 run by \\[tex-region] or \\[tex-buffer].
621 tex-dvi-print-command
622 Command string used by \\[tex-print] to print a .dvi file.
623 tex-alt-dvi-print-command
624 Alternative command string used by \\[tex-print] (when given a prefix
625 argument) to print a .dvi file.
626 tex-dvi-view-command
627 Command string used by \\[tex-view] to preview a .dvi file.
628 tex-show-queue-command
629 Command string used by \\[tex-show-print-queue] to show the print
630 queue that \\[tex-print] put your job on.
632 Entering SliTeX mode runs the hook `text-mode-hook', then the hook
633 `tex-mode-hook', then the hook `latex-mode-hook', and finally the hook
634 `slitex-mode-hook'. When the special subshell is initiated, the hook
635 `tex-shell-hook' is run."
636 (interactive)
637 (tex-common-initialization)
638 (setq mode-name "SliTeX")
639 (setq major-mode 'slitex-mode)
640 (setq tex-command slitex-run-command)
641 (setq tex-start-of-header "\\\\documentstyle{slides}\\|\\\\documentclass{slides}")
642 (setq tex-end-of-header "\\\\begin{document}")
643 (setq tex-trailer "\\end{document}\n")
644 ;; A line containing just $$ is treated as a paragraph separator.
645 ;; A line starting with $$ starts a paragraph,
646 ;; but does not separate paragraphs if it has more stuff on it.
647 (setq paragraph-start "[ \t]*$\\|[\f%]\\|[ \t]*\\$\\$\\|\
648 \\\\begin\\>\\|\\\\label\\>\\|\\\\end\\>\\|\\\\\\[\\|\\\\\\]\\|\
649 \\\\chapter\\>\\|\\\\section\\>\\|\
650 \\\\subsection\\>\\|\\\\subsubsection\\>\\|\
651 \\\\paragraph\\>\\|\\\\subparagraph\\>\\|\
652 \\\\item\\>\\|\\\\bibitem\\>\\|\\\\newline\\>\\|\\\\noindent\\>\\|\
653 \\\\[a-z]*space\\>\\|\\\\[a-z]*skip\\>\\|\
654 \\\\newpage\\>\\|\\\\[a-z]*page\\|\\\\footnote\\>\\|\
655 \\\\marginpar\\>\\|\\\\parbox\\>\\|\\\\caption\\>")
656 (setq paragraph-separate "[ \t]*$\\|[\f%]\\|[ \t]*\\$\\$[ \t]*$\\|\
657 \\\\begin\\>\\|\\\\label\\>\\|\\\\end\\>\\|\\\\\\[\\|\\\\\\]\\|\
658 \\\\chapter\\>\\|\\\\section\\>\\|\
659 \\\\subsection\\>\\|\\\\subsubsection\\>\\|\
660 \\\\paragraph\\>\\|\\\\subparagraph\\>\\|\
661 \\\\item[ \t]*$\\|\\\\bibitem[ \t]*$\\|\\\\newline[ \t]*$\\|\\\\noindent[ \t]*$\\|\
662 \\\\[a-z]*space[ \t]*$\\|\\\\[a-z]*skip[ \t]*$\\|\
663 \\\\newpage[ \t]*$\\|\\\\[a-z]*page[a-z]*[ \t]*$\\|\\\\footnote[ \t]*$\\|\
664 \\\\marginpar[ \t]*$\\|\\\\parbox[ \t]*$\\|\\\\caption[ \t]*$")
665 (make-local-variable 'imenu-create-index-function)
666 (setq imenu-create-index-function 'latex-imenu-create-index)
667 (make-local-variable 'tex-face-alist)
668 (setq tex-face-alist tex-latex-face-alist)
669 (make-local-variable 'fill-nobreak-predicate)
670 (setq fill-nobreak-predicate 'latex-fill-nobreak-predicate)
671 (run-hooks
672 'text-mode-hook 'tex-mode-hook 'latex-mode-hook 'slitex-mode-hook))
674 (defun tex-common-initialization ()
675 (kill-all-local-variables)
676 (use-local-map tex-mode-map)
677 (setq local-abbrev-table text-mode-abbrev-table)
678 (if (null tex-mode-syntax-table)
679 (let ((char 0))
680 (setq tex-mode-syntax-table (make-syntax-table))
681 (set-syntax-table tex-mode-syntax-table)
682 (while (< char ? )
683 (modify-syntax-entry char ".")
684 (setq char (1+ char)))
685 (modify-syntax-entry ?\C-@ "w")
686 (modify-syntax-entry ?\t " ")
687 (modify-syntax-entry ?\n ">")
688 (modify-syntax-entry ?\f ">")
689 (modify-syntax-entry ?$ "$$")
690 (modify-syntax-entry ?% "<")
691 (modify-syntax-entry ?\\ "/")
692 (modify-syntax-entry ?\" ".")
693 (modify-syntax-entry ?& ".")
694 (modify-syntax-entry ?_ ".")
695 (modify-syntax-entry ?@ "_")
696 (modify-syntax-entry ?~ " ")
697 (modify-syntax-entry ?' "w"))
698 (set-syntax-table tex-mode-syntax-table))
699 ;; Regexp isearch should accept newline and formfeed as whitespace.
700 (make-local-variable 'search-whitespace-regexp)
701 (setq search-whitespace-regexp "[ \t\r\n\f]+")
702 (make-local-variable 'paragraph-start)
703 ;; A line containing just $$ is treated as a paragraph separator.
704 (setq paragraph-start "[ \t]*$\\|[\f\\\\%]\\|[ \t]*\\$\\$")
705 (make-local-variable 'paragraph-separate)
706 ;; A line starting with $$ starts a paragraph,
707 ;; but does not separate paragraphs if it has more stuff on it.
708 (setq paragraph-separate "[ \t]*$\\|[\f\\\\%]\\|[ \t]*\\$\\$[ \t]*$")
709 (make-local-variable 'comment-start)
710 (setq comment-start "%")
711 (make-local-variable 'comment-start-skip)
712 (setq comment-start-skip "\\(\\(^\\|[^\\]\\)\\(\\\\\\\\\\)*\\)\\(%+ *\\)")
713 (make-local-variable 'comment-indent-function)
714 (setq comment-indent-function 'tex-comment-indent)
715 (make-local-variable 'parse-sexp-ignore-comments)
716 (setq parse-sexp-ignore-comments t)
717 (make-local-variable 'compare-windows-whitespace)
718 (setq compare-windows-whitespace 'tex-categorize-whitespace)
719 (make-local-variable 'skeleton-further-elements)
720 (setq skeleton-further-elements
721 '((indent-line-function 'indent-relative-maybe)))
722 (make-local-variable 'facemenu-add-face-function)
723 (make-local-variable 'facemenu-end-add-face)
724 (make-local-variable 'facemenu-remove-face-function)
725 (setq facemenu-add-face-function
726 (lambda (face end)
727 (let ((face-text (cdr (assq face tex-face-alist))))
728 (if face-text
729 face-text
730 (error "Face %s not configured for %s mode" face mode-name))))
731 facemenu-end-add-face "}"
732 facemenu-remove-face-function t)
733 (make-local-variable 'tex-command)
734 (make-local-variable 'tex-start-of-header)
735 (make-local-variable 'tex-end-of-header)
736 (make-local-variable 'tex-trailer))
738 (defun tex-comment-indent ()
739 (if (looking-at "%%%")
740 (current-column)
741 (skip-chars-backward " \t")
742 (max (if (bolp) 0 (1+ (current-column)))
743 comment-column)))
745 (defun tex-categorize-whitespace (backward-limit)
746 ;; compare-windows-whitespace is set to this.
747 ;; This is basically a finite-state machine.
748 ;; Returns a symbol telling how TeX would treat
749 ;; the whitespace we are looking at: null, space, or par.
750 (let ((category 'null)
751 (not-finished t))
752 (skip-chars-backward " \t\n\f" backward-limit)
753 (while not-finished
754 (cond ((looking-at "[ \t]+")
755 (goto-char (match-end 0))
756 (if (eq category 'null)
757 (setq category 'space)))
758 ((looking-at "\n")
759 (cond ((eq category 'newline)
760 (setq category 'par)
761 (setq not-finished nil))
763 (setq category 'newline) ;a strictly internal state
764 (goto-char (match-end 0)))))
765 ((looking-at "\f+")
766 (setq category 'par)
767 (setq not-finished nil))
769 (setq not-finished nil))))
770 (skip-chars-forward " \t\n\f")
771 (if (eq category 'newline)
772 'space ;TeX doesn't distinguish
773 category)))
775 (defun tex-insert-quote (arg)
776 "Insert the appropriate quote marks for TeX.
777 Inserts the value of `tex-open-quote' (normally ``) or `tex-close-quote'
778 \(normally '') depending on the context. With prefix argument, always
779 inserts \" characters."
780 (interactive "*P")
781 (if arg
782 (self-insert-command (prefix-numeric-value arg))
783 (insert
784 (cond ((or (bobp)
785 (save-excursion
786 (forward-char -1)
787 (looking-at "\\s(\\|\\s \\|\\s>")))
788 tex-open-quote)
789 ((= (preceding-char) ?\\)
790 ?\")
792 tex-close-quote)))))
794 (defun tex-validate-buffer ()
795 "Check current buffer for paragraphs containing mismatched braces or $s.
796 Their positions are recorded in the buffer `*Occur*'.
797 To find a particular invalidity from `*Occur*', switch to that buffer
798 and type C-c C-c or click with mouse-2
799 on the line for the invalidity you want to see."
800 (interactive)
801 (let ((buffer (current-buffer))
802 (prevpos (point-min))
803 (linenum nil)
804 (num-matches 0))
805 (with-output-to-temp-buffer "*Occur*"
806 (princ "Mismatches:\n")
807 (save-excursion
808 (set-buffer standard-output)
809 (occur-mode)
810 (setq occur-buffer buffer)
811 (setq occur-nlines 0))
812 (save-excursion
813 (goto-char (point-max))
814 (while (and (not (input-pending-p)) (not (bobp)))
815 (let ((end (point))
816 prev-end)
817 ;; Scan the previous paragraph for invalidities.
818 (if (search-backward "\n\n" nil t)
819 (progn
820 (setq prev-end (point))
821 (forward-char 2))
822 (goto-char (setq prev-end (point-min))))
823 (or (tex-validate-region (point) end)
824 (let* ((oend end)
825 (end (save-excursion (forward-line 1) (point)))
826 start tem)
827 (beginning-of-line)
828 (setq start (point))
829 ;; Keep track of line number as we scan,
830 ;; in a cumulative fashion.
831 (if linenum
832 (setq linenum (- linenum (count-lines prevpos (point))))
833 (setq linenum (1+ (count-lines 1 start))))
834 (setq prevpos (point))
835 ;; Mention this mismatch in *Occur*.
836 ;; Since we scan from end of buffer to beginning,
837 ;; add each mismatch at the beginning of *Occur*.
838 (save-excursion
839 (setq tem (point-marker))
840 (set-buffer standard-output)
841 (goto-char (point-min))
842 ;; Skip "Mismatches:" header line.
843 (forward-line 1)
844 (setq num-matches (1+ num-matches))
845 (insert-buffer-substring buffer start end)
846 (let (text-beg (text-end (point-marker)))
847 (forward-char (- start end))
848 (setq text-beg (point-marker))
849 (insert (format "%3d: " linenum))
850 (put-text-property (marker-position text-beg)
851 (- (marker-position text-end) 1)
852 'mouse-face 'highlight)
853 (put-text-property (marker-position text-beg)
854 (- (marker-position text-end) 1)
855 'occur tem)))))
856 (goto-char prev-end))))
857 (save-excursion
858 (set-buffer standard-output)
859 (if (eq num-matches 0)
860 (insert "None!\n"))
861 (if (interactive-p)
862 (message "%d mismatches found" num-matches))))))
864 (defun tex-validate-region (start end)
865 "Check for mismatched braces or $'s in region.
866 Returns t if no mismatches. Returns nil and moves point to suspect
867 area if a mismatch is found."
868 (interactive "r")
869 (let ((failure-point nil) (max-possible-sexps (- end start)))
870 (save-excursion
871 (condition-case ()
872 (save-restriction
873 (narrow-to-region start end)
874 ;; First check that the open and close parens balance in numbers.
875 (goto-char start)
876 (while (<= 0 (setq max-possible-sexps (1- max-possible-sexps)))
877 (forward-sexp 1))
878 ;; Now check that like matches like.
879 (goto-char start)
880 (while (progn (skip-syntax-forward "^(")
881 (not (eobp)))
882 (let ((match (matching-paren (following-char))))
883 (save-excursion
884 (forward-sexp 1)
885 (or (= (preceding-char) match)
886 (error "Mismatched parentheses"))))
887 (forward-char 1)))
888 (error
889 (skip-syntax-forward " .>")
890 (setq failure-point (point)))))
891 (if failure-point
892 (progn
893 (goto-char failure-point)
894 nil)
895 t)))
897 (defun tex-terminate-paragraph (inhibit-validation)
898 "Insert two newlines, breaking a paragraph for TeX.
899 Check for mismatched braces or $s in paragraph being terminated.
900 A prefix arg inhibits the checking."
901 (interactive "*P")
902 (or inhibit-validation
903 (save-excursion
904 (tex-validate-region
905 (save-excursion
906 (search-backward "\n\n" nil 'move)
907 (point))
908 (point)))
909 (message "Paragraph being closed appears to contain a mismatch"))
910 (insert "\n\n"))
912 (defun tex-insert-braces ()
913 "Make a pair of braces and be poised to type inside of them."
914 (interactive "*")
915 (insert ?\{)
916 (save-excursion
917 (insert ?})))
919 ;; This function is used as the value of fill-nobreak-predicate
920 ;; in LaTeX mode. Its job is to prevent line-breaking inside
921 ;; of a \verb construct.
922 (defun latex-fill-nobreak-predicate ()
923 (let ((opoint (point))
924 inside)
925 (save-excursion
926 (save-restriction
927 (beginning-of-line)
928 (narrow-to-region (point) opoint)
929 (while (re-search-forward "\\\\verb\\(.\\)" nil t)
930 (unless (re-search-forward (regexp-quote (match-string 1)) nil t)
931 (setq inside t)))))
932 inside))
934 ;;; Like tex-insert-braces, but for LaTeX.
935 (define-skeleton tex-latex-block
936 "Create a matching pair of lines \\begin[OPT]{NAME} and \\end{NAME} at point.
937 Puts point on a blank line between them."
938 (completing-read "LaTeX block name: "
939 (mapcar 'list
940 (append standard-latex-block-names
941 latex-block-names)))
942 "\\begin["
943 (skeleton-read "[options]: ") & ?\] | -1
946 ?\} \n
947 _ \n
948 "\\end{" str ?\})
950 (defun tex-last-unended-begin ()
951 "Leave point at the beginning of the last `\\begin{...}' that is unended."
952 (while (and (re-search-backward "\\(\\\\begin\\s *{\\)\\|\\(\\\\end\\s *{\\)")
953 (looking-at "\\\\end{"))
954 (tex-last-unended-begin)))
956 (defun tex-goto-last-unclosed-latex-block ()
957 "Move point to the last unclosed \\begin{...}.
958 Mark is left at original location."
959 (interactive)
960 (let ((spot))
961 (save-excursion
962 (condition-case nil
963 (tex-last-unended-begin)
964 (error (error "Couldn't find unended \\begin")))
965 (setq spot (point)))
966 (push-mark)
967 (goto-char spot)))
969 (defun tex-close-latex-block ()
970 "Creates an \\end{...} to match the last unclosed \\begin{...}."
971 (interactive "*")
972 (let ((new-line-needed (bolp))
973 text indentation)
974 (save-excursion
975 (condition-case nil
976 (tex-last-unended-begin)
977 (error (error "Couldn't find unended \\begin")))
978 (setq indentation (current-column))
979 (re-search-forward "\\\\begin\\(\\s *{[^}\n]*}\\)")
980 (setq text (buffer-substring (match-beginning 1) (match-end 1))))
981 (indent-to indentation)
982 (insert "\\end" text)
983 (if new-line-needed (insert ?\n))))
985 ;;; Invoking TeX in an inferior shell.
987 ;;; Why use a shell instead of running TeX directly? Because if TeX
988 ;;; gets stuck, the user can switch to the shell window and type at it.
990 ;;; The utility functions:
992 ;;;###autoload
993 (defun tex-start-shell ()
994 (save-excursion
995 (set-buffer
996 (make-comint
997 "tex-shell"
998 (or tex-shell-file-name (getenv "ESHELL") (getenv "SHELL") "/bin/sh")
999 nil))
1000 (let ((proc (get-process "tex-shell")))
1001 (set-process-sentinel proc 'tex-shell-sentinel)
1002 (process-kill-without-query proc)
1003 (setq comint-prompt-regexp shell-prompt-pattern)
1004 (setq tex-shell-map (nconc (make-sparse-keymap) shell-mode-map))
1005 (tex-define-common-keys tex-shell-map)
1006 (use-local-map tex-shell-map)
1007 (compilation-shell-minor-mode t)
1008 (add-hook 'comint-input-filter-functions 'shell-directory-tracker nil t)
1009 (make-local-variable 'list-buffers-directory)
1010 (make-local-variable 'shell-dirstack)
1011 (make-local-variable 'shell-last-dir)
1012 (make-local-variable 'shell-dirtrackp)
1013 (run-hooks 'tex-shell-hook)
1014 (while (zerop (buffer-size))
1015 (sleep-for 1)))))
1017 (defun tex-feed-input ()
1018 "Send input to the tex shell process.
1019 In the tex buffer this can be used to continue an interactive tex run.
1020 In the tex shell buffer this command behaves like `comint-send-input'."
1021 (interactive)
1022 (set-buffer (process-buffer (get-process "tex-shell")))
1023 (comint-send-input)
1024 (tex-recenter-output-buffer nil))
1026 (defun tex-display-shell ()
1027 "Make the TeX shell buffer visible in a window."
1028 (display-buffer (process-buffer (get-process "tex-shell")))
1029 (tex-recenter-output-buffer nil))
1031 (defun tex-shell-sentinel (proc msg)
1032 (cond ((null (buffer-name (process-buffer proc)))
1033 ;; buffer killed
1034 (set-process-buffer proc nil)
1035 (tex-delete-last-temp-files))
1036 ((memq (process-status proc) '(signal exit))
1037 (tex-delete-last-temp-files))))
1039 (defun tex-set-buffer-directory (buffer directory)
1040 "Set BUFFER's default directory to be DIRECTORY."
1041 (setq directory (file-name-as-directory (expand-file-name directory)))
1042 (if (not (file-directory-p directory))
1043 (error "%s is not a directory" directory)
1044 (save-excursion
1045 (set-buffer buffer)
1046 (setq default-directory directory))))
1048 (defvar tex-send-command-modified-tick 0)
1049 (make-variable-buffer-local 'tex-send-command-modified-tick)
1051 (defun tex-send-command (command &optional file background)
1052 "Send COMMAND to TeX shell process, substituting optional FILE for *.
1053 Do this in background if optional BACKGROUND is t. If COMMAND has no *,
1054 FILE will be appended, preceded by a blank, to COMMAND. If FILE is nil, no
1055 substitution will be made in COMMAND. COMMAND can be any expression that
1056 evaluates to a command string.
1058 Return the process in which TeX is running."
1059 (save-excursion
1060 (let* ((cmd (eval command))
1061 (proc (or (get-process "tex-shell") (error "No TeX subprocess")))
1062 (buf (process-buffer proc))
1063 (star (string-match "\\*" cmd))
1064 (string
1065 (concat
1066 (if file
1067 (if star (concat (substring cmd 0 star)
1068 file (substring cmd (1+ star)))
1069 (concat cmd " " file))
1070 cmd)
1071 (if background "&" ""))))
1072 ;; Switch to buffer before checking for subproc output in it.
1073 (set-buffer buf)
1074 ;; If text is unchanged since previous tex-send-command,
1075 ;; we haven't got any output. So wait for output now.
1076 (if (= (buffer-modified-tick buf) tex-send-command-modified-tick)
1077 (accept-process-output proc))
1078 (goto-char (process-mark proc))
1079 (insert string)
1080 (comint-send-input)
1081 (setq tex-send-command-modified-tick (buffer-modified-tick buf))
1082 proc)))
1084 (defun tex-delete-last-temp-files (&optional not-all)
1085 "Delete any junk files from last temp file.
1086 If NOT-ALL is non-nil, save the `.dvi' file."
1087 (if tex-last-temp-file
1088 (let* ((dir (file-name-directory tex-last-temp-file))
1089 (list (and (file-directory-p dir)
1090 (file-name-all-completions
1091 (file-name-sans-extension
1092 (file-name-nondirectory tex-last-temp-file))
1093 dir))))
1094 (while list
1095 (if not-all
1096 (and
1097 ;; If arg is non-nil, don't delete the .dvi file.
1098 (not (string-match "\\.dvi$" (car list)))
1099 (delete-file (concat dir (car list))))
1100 (delete-file (concat dir (car list))))
1101 (setq list (cdr list))))))
1103 (add-hook 'kill-emacs-hook 'tex-delete-last-temp-files)
1105 (defvar tex-start-tex-marker nil
1106 "Marker pointing after last TeX-running command in the TeX shell buffer.")
1108 (defun tex-start-tex (command file)
1109 "Start a TeX run, using COMMAND on FILE."
1110 (let* ((star (string-match "\\*" command))
1111 (compile-command
1112 (if star
1113 (concat (substring command 0 star)
1114 (comint-quote-filename file)
1115 (substring command (1+ star)))
1116 (concat command " "
1117 (if (< 0 (length tex-start-options-string))
1118 (concat
1119 (shell-quote-argument tex-start-options-string) " "))
1120 (comint-quote-filename file)))))
1121 (with-current-buffer (process-buffer (tex-send-command compile-command))
1122 (save-excursion
1123 (forward-line -1)
1124 (setq tex-start-tex-marker (point-marker)))
1125 (make-local-variable 'compilation-parse-errors-function)
1126 (setq compilation-parse-errors-function 'tex-compilation-parse-errors)
1127 (compilation-forget-errors))))
1129 (defun tex-compilation-parse-errors (limit-search find-at-least)
1130 "Parse the current buffer as TeX error messages.
1131 See the variable `compilation-parse-errors-function' for the interface it uses.
1133 This function parses only the last TeX compilation.
1134 It works on TeX compilations only. It is necessary for that purpose,
1135 since TeX does not put file names and line numbers on the same line as
1136 for the error messages."
1137 (require 'thingatpt)
1138 (setq compilation-error-list nil)
1139 (message "Parsing error messages...")
1140 (let ((default-directory ; Perhaps dir has changed meanwhile.
1141 (file-name-directory (buffer-file-name tex-last-buffer-texed)))
1142 (old-syntax-table (syntax-table))
1143 (tex-error-parse-syntax-table (copy-syntax-table (syntax-table)))
1144 found-desired (num-errors-found 0)
1145 last-filename last-linenum last-position
1146 begin-of-error end-of-error)
1147 (modify-syntax-entry ?\{ "_" tex-error-parse-syntax-table)
1148 (modify-syntax-entry ?\} "_" tex-error-parse-syntax-table)
1149 (modify-syntax-entry ?\[ "_" tex-error-parse-syntax-table)
1150 (modify-syntax-entry ?\] "_" tex-error-parse-syntax-table)
1151 ;; Single quotations may appear in errors
1152 (modify-syntax-entry ?\" "_" tex-error-parse-syntax-table)
1153 ;; Don't parse previous compilations.
1154 (set-marker compilation-parsing-end
1155 (max compilation-parsing-end tex-start-tex-marker))
1156 ;; Don't reparse messages already seen at last parse.
1157 (goto-char compilation-parsing-end)
1158 ;; Parse messages.
1159 (while (and (not (or found-desired (eobp)))
1160 (prog1 (re-search-forward "^! " nil 'move)
1161 (setq begin-of-error (match-beginning 0)
1162 end-of-error (match-end 0)))
1163 (re-search-forward
1164 "^l\\.\\([0-9]+\\) \\(\\.\\.\\.\\)?\\(.*\\)$" nil 'move))
1165 (let* ((this-error (set-marker (make-marker) begin-of-error))
1166 (linenum (string-to-int (match-string 1)))
1167 (error-text (regexp-quote (match-string 3)))
1168 (filename
1169 (save-excursion
1170 (unwind-protect
1171 (progn
1172 (set-syntax-table tex-error-parse-syntax-table)
1173 (backward-up-list 1)
1174 (skip-syntax-forward "(_")
1175 (while (not (file-readable-p
1176 (thing-at-point 'filename)))
1177 (skip-syntax-backward "(_")
1178 (backward-up-list 1)
1179 (skip-syntax-forward "(_"))
1180 (thing-at-point 'filename))
1181 (set-syntax-table old-syntax-table))))
1182 (new-file
1183 (or (null last-filename)
1184 (not (string-equal last-filename filename))))
1185 (error-location
1186 (save-excursion
1187 (if (equal filename (concat tex-zap-file ".tex"))
1188 (set-buffer tex-last-buffer-texed)
1189 (set-buffer (find-file-noselect filename)))
1190 (if new-file
1191 (goto-line linenum)
1192 (goto-char last-position)
1193 (forward-line (- linenum last-linenum)))
1194 ;; first try a forward search for the error text,
1195 ;; then a backward search limited by the last error.
1196 (let ((starting-point (point)))
1197 (or (re-search-forward error-text nil t)
1198 (re-search-backward
1199 error-text
1200 (marker-position last-position) t)
1201 (goto-char starting-point)))
1202 (point-marker))))
1203 (goto-char this-error)
1204 (if (and compilation-error-list
1205 (or (and find-at-least
1206 (>= num-errors-found
1207 find-at-least))
1208 (and limit-search
1209 (>= end-of-error limit-search)))
1210 new-file)
1211 (setq found-desired t)
1212 (setq num-errors-found (1+ num-errors-found)
1213 last-filename filename
1214 last-linenum linenum
1215 last-position error-location
1216 compilation-error-list ; Add the new error
1217 (cons (cons this-error error-location)
1218 compilation-error-list))
1219 (goto-char end-of-error)))))
1220 (set-marker compilation-parsing-end (point))
1221 (setq compilation-error-list (nreverse compilation-error-list))
1222 (message "Parsing error messages...done"))
1224 ;;; The commands:
1226 (defun tex-region (beg end)
1227 "Run TeX on the current region, via a temporary file.
1228 The file's name comes from the variable `tex-zap-file' and the
1229 variable `tex-directory' says where to put it.
1231 If the buffer has a header, the header is given to TeX before the
1232 region itself. The buffer's header is all lines between the strings
1233 defined by `tex-start-of-header' and `tex-end-of-header' inclusive.
1234 The header must start in the first 100 lines of the buffer.
1236 The value of `tex-trailer' is given to TeX as input after the region.
1238 The value of `tex-command' specifies the command to use to run TeX."
1239 (interactive "r")
1240 (if (tex-shell-running)
1241 (tex-kill-job)
1242 (tex-start-shell))
1243 (or tex-zap-file
1244 (setq tex-zap-file (tex-generate-zap-file-name)))
1245 ;; Temp file will be written and TeX will be run in zap-directory.
1246 ;; If the TEXINPUTS file has relative directories or if the region has
1247 ;; \input of files, this must be the same directory as the file for
1248 ;; TeX to access the correct inputs. That's why it's safest if
1249 ;; tex-directory is ".".
1250 (let* ((zap-directory
1251 (file-name-as-directory (expand-file-name tex-directory)))
1252 (tex-out-file (concat zap-directory tex-zap-file ".tex")))
1253 ;; Don't delete temp files if we do the same buffer twice in a row.
1254 (or (eq (current-buffer) tex-last-buffer-texed)
1255 (tex-delete-last-temp-files t))
1256 ;; Write the new temp file.
1257 (save-excursion
1258 (save-restriction
1259 (widen)
1260 (goto-char (point-min))
1261 (forward-line 100)
1262 (let ((search-end (point))
1263 (default-directory zap-directory)
1264 (already-output 0))
1265 (goto-char (point-min))
1267 ;; Maybe copy first line, such as `\input texinfo', to temp file.
1268 (and tex-first-line-header-regexp
1269 (looking-at tex-first-line-header-regexp)
1270 (write-region (point)
1271 (progn (forward-line 1)
1272 (setq already-output (point)))
1273 tex-out-file nil nil))
1275 ;; Write out the header, if there is one,
1276 ;; and any of the specified region which extends before it.
1277 ;; But don't repeat anything already written.
1278 (if (re-search-forward tex-start-of-header search-end t)
1279 (let (hbeg)
1280 (beginning-of-line)
1281 (setq hbeg (point)) ;mark beginning of header
1282 (if (re-search-forward tex-end-of-header nil t)
1283 (let (hend)
1284 (forward-line 1)
1285 (setq hend (point)) ;mark end of header
1286 (write-region (max (min hbeg beg) already-output)
1287 hend
1288 tex-out-file
1289 (not (zerop already-output)) nil)
1290 (setq already-output hend)))))
1292 ;; Write out the specified region
1293 ;; (but don't repeat anything already written).
1294 (write-region (max beg already-output) end
1295 tex-out-file
1296 (not (zerop already-output)) nil))
1297 ;; Write the trailer, if any.
1298 ;; Precede it with a newline to make sure it
1299 ;; is not hidden in a comment.
1300 (if tex-trailer
1301 (write-region (concat "\n" tex-trailer) nil
1302 tex-out-file t nil))))
1303 ;; Record the file name to be deleted afterward.
1304 (setq tex-last-temp-file tex-out-file)
1305 (let (shell-dirtrack-verbose)
1306 (tex-send-command tex-shell-cd-command zap-directory))
1307 ;; Use a relative file name here because (1) the proper dir
1308 ;; is already current, and (2) the abs file name is sometimes
1309 ;; too long and can make tex crash.
1310 (tex-start-tex tex-command (concat tex-zap-file ".tex"))
1311 (tex-display-shell)
1312 (setq tex-print-file tex-out-file)
1313 (setq tex-last-buffer-texed (current-buffer))))
1315 (defun tex-buffer ()
1316 "Run TeX on current buffer. See \\[tex-region] for more information.
1317 Does not save the buffer, so it's useful for trying experimental versions.
1318 See \\[tex-file] for an alternative."
1319 (interactive)
1320 (tex-region (point-min) (point-max)))
1322 (defun tex-file ()
1323 "Prompt to save all buffers and run TeX (or LaTeX) on current buffer's file.
1324 This function is more useful than \\[tex-buffer] when you need the
1325 `.aux' file of LaTeX to have the correct name."
1326 (interactive)
1327 (let ((source-file
1328 (or tex-main-file
1329 (if (buffer-file-name)
1330 (file-name-nondirectory (buffer-file-name))
1331 (error "Buffer does not seem to be associated with any file"))))
1332 (file-dir (file-name-directory (buffer-file-name))))
1333 (if tex-offer-save
1334 (save-some-buffers))
1335 (if (tex-shell-running)
1336 (tex-kill-job)
1337 (tex-start-shell))
1338 (let (shell-dirtrack-verbose)
1339 (tex-send-command tex-shell-cd-command file-dir))
1340 (tex-start-tex tex-command source-file)
1341 (tex-display-shell)
1342 (setq tex-last-buffer-texed (current-buffer))
1343 (setq tex-print-file (expand-file-name source-file))))
1345 (defun tex-generate-zap-file-name ()
1346 "Generate a unique name suitable for use as a file name."
1347 ;; Include the shell process number and host name
1348 ;; in case there are multiple shells (for same or different user).
1349 ;; Dec 1998: There is a report that some versions of xdvi
1350 ;; don't work with file names that start with #.
1351 (format "_TZ_%d-%s"
1352 (process-id (get-buffer-process "*tex-shell*"))
1353 (tex-strip-dots (system-name))))
1355 (defun tex-strip-dots (s)
1356 (setq s (copy-sequence s))
1357 (while (string-match "\\." s)
1358 (aset s (match-beginning 0) ?-))
1361 ;; This will perhaps be useful for modifying TEXINPUTS.
1362 ;; Expand each file name, separated by colons, in the string S.
1363 (defun tex-expand-files (s)
1364 (let (elts (start 0))
1365 (while (string-match ":" s start)
1366 (setq elts (cons (substring s start (match-beginning 0)) elts))
1367 (setq start (match-end 0)))
1368 (or (= start 0)
1369 (setq elts (cons (substring s start) elts)))
1370 (mapconcat (lambda (elt)
1371 (if (= (length elt) 0) elt (expand-file-name elt)))
1372 (nreverse elts) ":")))
1374 (defun tex-shell-running ()
1375 (and (get-process "tex-shell")
1376 (eq (process-status (get-process "tex-shell")) 'run)))
1378 (defun tex-kill-job ()
1379 "Kill the currently running TeX job."
1380 (interactive)
1381 ;; quit-process leads to core dumps of the tex process (except if
1382 ;; coredumpsize has limit 0kb as on many environments). One would
1383 ;; like to use (kill-process proc 'lambda), however that construct
1384 ;; does not work on some systems and kills the shell itself.
1385 (quit-process (get-process "tex-shell") t))
1387 (defun tex-recenter-output-buffer (linenum)
1388 "Redisplay buffer of TeX job output so that most recent output can be seen.
1389 The last line of the buffer is displayed on
1390 line LINE of the window, or centered if LINE is nil."
1391 (interactive "P")
1392 (let ((tex-shell (get-buffer "*tex-shell*"))
1393 (old-buffer (current-buffer))
1394 (window))
1395 (if (null tex-shell)
1396 (message "No TeX output buffer")
1397 (setq window (display-buffer tex-shell))
1398 (save-selected-window
1399 (select-window window)
1400 (bury-buffer tex-shell)
1401 (goto-char (point-max))
1402 (recenter (if linenum
1403 (prefix-numeric-value linenum)
1404 (/ (window-height) 2)))))))
1406 (defun tex-print (&optional alt)
1407 "Print the .dvi file made by \\[tex-region], \\[tex-buffer] or \\[tex-file].
1408 Runs the shell command defined by `tex-dvi-print-command'. If prefix argument
1409 is provided, use the alternative command, `tex-alt-dvi-print-command'."
1410 (interactive "P")
1411 (let ((print-file-name-dvi (tex-append tex-print-file ".dvi"))
1412 test-name)
1413 (if (and (not (equal (current-buffer) tex-last-buffer-texed))
1414 (buffer-file-name)
1415 ;; Check that this buffer's printed file is up to date.
1416 (file-newer-than-file-p
1417 (setq test-name (tex-append (buffer-file-name) ".dvi"))
1418 (buffer-file-name)))
1419 (setq print-file-name-dvi test-name))
1420 (if (not (file-exists-p print-file-name-dvi))
1421 (error "No appropriate `.dvi' file could be found")
1422 (if (tex-shell-running)
1423 (tex-kill-job)
1424 (tex-start-shell))
1425 (tex-send-command
1426 (if alt tex-alt-dvi-print-command tex-dvi-print-command)
1427 print-file-name-dvi t))))
1429 (defun tex-alt-print ()
1430 "Print the .dvi file made by \\[tex-region], \\[tex-buffer] or \\[tex-file].
1431 Runs the shell command defined by `tex-alt-dvi-print-command'."
1432 (interactive)
1433 (tex-print t))
1435 (defun tex-view ()
1436 "Preview the last `.dvi' file made by running TeX under Emacs.
1437 This means, made using \\[tex-region], \\[tex-buffer] or \\[tex-file].
1438 The variable `tex-dvi-view-command' specifies the shell command for preview.
1439 You must set that variable yourself before using this command,
1440 because there is no standard value that would generally work."
1441 (interactive)
1442 (or tex-dvi-view-command
1443 (error "You must set `tex-dvi-view-command'"))
1444 (let ((tex-dvi-print-command tex-dvi-view-command))
1445 (tex-print)))
1447 (defun tex-append (file-name suffix)
1448 "Append to FILENAME the suffix SUFFIX, using same algorithm TeX uses.
1449 Pascal-based TeX scans for the first period, C TeX uses the last.
1450 No period is retained immediately before SUFFIX,
1451 so normally SUFFIX starts with one."
1452 (if (stringp file-name)
1453 (let ((file (file-name-nondirectory file-name))
1454 trial-name)
1455 ;; Try splitting on last period.
1456 ;; The first-period split can get fooled when two files
1457 ;; named a.tex and a.b.tex are both tex'd;
1458 ;; the last-period split must be right if it matches at all.
1459 (setq trial-name
1460 (concat (file-name-directory file-name)
1461 (substring file 0
1462 (string-match "\\.[^.]*$" file))
1463 suffix))
1464 (if (or (file-exists-p trial-name)
1465 (file-exists-p (concat trial-name ".aux"))) ;for BibTeX files
1466 trial-name
1467 ;; Not found, so split on first period.
1468 (concat (file-name-directory file-name)
1469 (substring file 0
1470 (string-match "\\." file))
1471 suffix)))
1472 " "))
1474 (defun tex-show-print-queue ()
1475 "Show the print queue that \\[tex-print] put your job on.
1476 Runs the shell command defined by `tex-show-queue-command'."
1477 (interactive)
1478 (if (tex-shell-running)
1479 (tex-kill-job)
1480 (tex-start-shell))
1481 (tex-send-command tex-show-queue-command)
1482 (tex-display-shell))
1484 (defun tex-bibtex-file ()
1485 "Run BibTeX on the current buffer's file."
1486 (interactive)
1487 (if (tex-shell-running)
1488 (tex-kill-job)
1489 (tex-start-shell))
1490 (let (shell-dirtrack-verbose
1491 (tex-out-file
1492 (tex-append (file-name-nondirectory (buffer-file-name)) ""))
1493 (file-dir (file-name-directory (buffer-file-name))))
1494 (tex-send-command tex-shell-cd-command file-dir)
1495 (tex-send-command tex-bibtex-command tex-out-file))
1496 (tex-display-shell))
1498 (run-hooks 'tex-mode-load-hook)
1500 (provide 'tex-mode)
1502 ;;; tex-mode.el ends here