(syms_of_buffer): Doc fix.
[bpt/emacs.git] / lisp / textmodes / tex-mode.el
... / ...
CommitLineData
1;;; tex-mode.el --- TeX, LaTeX, and SliTeX mode commands.
2
3;; Copyright (C) 1985, 86, 89, 92, 94, 95, 96, 1997
4;; Free Software Foundation, Inc.
5
6;; Maintainer: FSF
7;; Keywords: tex
8
9;; Contributions over the years by William F. Schelter, Dick King,
10;; Stephen Gildea, Michael Prange, Jacob Gore, and Edward M. Reingold.
11
12;; This file is part of GNU Emacs.
13
14;; GNU Emacs is free software; you can redistribute it and/or modify
15;; it under the terms of the GNU General Public License as published by
16;; the Free Software Foundation; either version 2, or (at your option)
17;; any later version.
18
19;; GNU Emacs is distributed in the hope that it will be useful,
20;; but WITHOUT ANY WARRANTY; without even the implied warranty of
21;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22;; GNU General Public License for more details.
23
24;; You should have received a copy of the GNU General Public License
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.
28
29;;; Code:
30
31(require 'shell)
32(require 'compile)
33
34(defgroup tex-file nil
35 "TeX files and directories"
36 :prefix "tex-"
37 :group 'tex)
38
39(defgroup tex-run nil
40 "Running external commands from TeX mode"
41 :prefix "tex-"
42 :group 'tex)
43
44(defgroup tex-view nil
45 "Viewing and printing TeX files"
46 :prefix "tex-"
47 :group 'tex)
48
49;;;###autoload
50(defcustom tex-shell-file-name nil
51 "*If non-nil, the shell file name to run in the subshell used to run TeX."
52 :type '(choice (const :tag "None" nil)
53 string)
54 :group 'tex-run)
55
56;;;###autoload
57(defcustom tex-directory "."
58 "*Directory in which temporary files are written.
59You can make this `/tmp' if your TEXINPUTS has no relative directories in it
60and you don't try to apply \\[tex-region] or \\[tex-buffer] when there are
61`\\input' commands with relative directories."
62 :type 'directory
63 :group 'tex-file)
64
65;;;###autoload
66(defcustom tex-first-line-header-regexp nil
67 "Regexp for matching a first line which `tex-region' should include.
68If this is non-nil, it should be a regular expression string;
69if it matches the first line of the file,
70`tex-region' always includes the first line in the TeX run."
71 :type '(choice (const :tag "None" nil)
72 regexp)
73 :group 'tex-file)
74
75;;;###autoload
76(defcustom tex-main-file nil
77 "*The main TeX source file which includes this buffer's file.
78The command `tex-file' runs TeX on the file specified by `tex-main-file'
79if the variable is non-nil."
80 :type '(choice (const :tag "None" nil)
81 file)
82 :group 'tex-file)
83
84;;;###autoload
85(defcustom tex-offer-save t
86 "*If non-nil, ask about saving modified buffers before \\[tex-file] is run."
87 :type 'boolean
88 :group 'tex-file)
89
90;;;###autoload
91(defcustom tex-run-command "tex"
92 "*Command used to run TeX subjob.
93If this string contains an asterisk (`*'), that is replaced by the file name;
94otherwise the value of tex-start-options-string and the file name are added
95at the end, with blanks as separators."
96 :type 'string
97 :group 'tex-run)
98
99(defcustom tex-start-options-string "\\\\nonstopmode\\\\input"
100 "*TeX options to use when running TeX.
101These precede the input file name."
102 :type 'string
103 :group 'tex-run
104 :version "20.4")
105
106;;;###autoload
107(defcustom latex-run-command "latex"
108 "*Command used to run LaTeX subjob.
109If this string contains an asterisk (`*'), that is replaced by the file name;
110otherwise, the file name, preceded by blank, is added at the end."
111 :type 'string
112 :group 'tex-run)
113
114(defvar standard-latex-block-names
115 '("abstract" "array" "center" "description"
116 "displaymath" "document" "enumerate" "eqnarray"
117 "eqnarray*" "equation" "figure" "figure*"
118 "flushleft" "flushright" "itemize" "letter"
119 "list" "minipage" "picture" "quotation"
120 "quote" "slide" "sloppypar" "tabbing"
121 "table" "table*" "tabular" "tabular*"
122 "thebibliography" "theindex*" "titlepage" "trivlist"
123 "verbatim" "verbatim*" "verse")
124 "Standard LaTeX block names.")
125
126;;;###autoload
127(defcustom latex-block-names nil
128 "*User defined LaTeX block names.
129Combined with `standard-latex-block-names' for minibuffer completion."
130 :type '(repeat string)
131 :group 'tex-run)
132
133;;;###autoload
134(defcustom slitex-run-command "slitex"
135 "*Command used to run SliTeX subjob.
136If this string contains an asterisk (`*'), that is replaced by the file name;
137otherwise, the file name, preceded by blank, is added at the end."
138 :type 'string
139 :group 'tex-run)
140
141;;;###autoload
142(defcustom tex-bibtex-command "bibtex"
143 "*Command used by `tex-bibtex-file' to gather bibliographic data.
144If this string contains an asterisk (`*'), that is replaced by the file name;
145otherwise, the file name, preceded by blank, is added at the end."
146 :type 'string
147 :group 'tex-run)
148
149;;;###autoload
150(defcustom tex-dvi-print-command "lpr -d"
151 "*Command used by \\[tex-print] to print a .dvi file.
152If this string contains an asterisk (`*'), that is replaced by the file name;
153otherwise, the file name, preceded by blank, is added at the end."
154 :type 'string
155 :group 'tex-view)
156
157;;;###autoload
158(defcustom tex-alt-dvi-print-command "lpr -d"
159 "*Command used by \\[tex-print] with a prefix arg to print a .dvi file.
160If this string contains an asterisk (`*'), that is replaced by the file name;
161otherwise, the file name, preceded by blank, is added at the end.
162
163If two printers are not enough of a choice, you can set the variable
164`tex-alt-dvi-print-command' to an expression that asks what you want;
165for example,
166
167 (setq tex-alt-dvi-print-command
168 '(format \"lpr -P%s\" (read-string \"Use printer: \")))
169
170would tell \\[tex-print] with a prefix argument to ask you which printer to
171use."
172 :type '(choice (string :tag "Command")
173 (sexp :tag "Expression"))
174 :group 'tex-view)
175
176;;;###autoload
177(defcustom tex-dvi-view-command nil
178 "*Command used by \\[tex-view] to display a `.dvi' file.
179If this string contains an asterisk (`*'), that is replaced by the file name;
180otherwise, the file name, preceded by blank, is added at the end.
181
182This can be set conditionally so that the previewer used is suitable for the
183window system being used. For example,
184
185 (setq tex-dvi-view-command
186 (if (eq window-system 'x) \"xdvi\" \"dvi2tty * | cat -s\"))
187
188would tell \\[tex-view] to use xdvi under X windows and to use dvi2tty
189otherwise."
190 :type '(choice (const nil) string)
191 :group 'tex-view)
192
193;;;###autoload
194(defcustom tex-show-queue-command "lpq"
195 "*Command used by \\[tex-show-print-queue] to show the print queue.
196Should show the queue(s) that \\[tex-print] puts jobs on."
197 :type 'string
198 :group 'tex-view)
199
200;;;###autoload
201(defcustom tex-default-mode 'plain-tex-mode
202 "*Mode to enter for a new file that might be either TeX or LaTeX.
203This variable is used when it can't be determined whether the file
204is plain TeX or LaTeX or what because the file contains no commands.
205Normally set to either `plain-tex-mode' or `latex-mode'."
206 :type 'function
207 :group 'tex)
208
209;;;###autoload
210(defcustom tex-open-quote "``"
211 "*String inserted by typing \\[tex-insert-quote] to open a quotation."
212 :type 'string
213 :group 'tex)
214
215;;;###autoload
216(defcustom tex-close-quote "''"
217 "*String inserted by typing \\[tex-insert-quote] to close a quotation."
218 :type 'string
219 :group 'tex)
220
221(defvar tex-last-temp-file nil
222 "Latest temporary file generated by \\[tex-region] and \\[tex-buffer].
223Deleted when the \\[tex-region] or \\[tex-buffer] is next run, or when the
224tex shell terminates.")
225
226(defvar tex-command nil
227 "Command to run TeX.
228The name of the file, preceded by a blank, will be added to this string.")
229
230(defvar tex-trailer nil
231 "String appended after the end of a region sent to TeX by \\[tex-region].")
232
233(defvar tex-start-of-header nil
234 "Regular expression used by \\[tex-region] to find start of file's header.")
235
236(defvar tex-end-of-header nil
237 "Regular expression used by \\[tex-region] to find end of file's header.")
238
239(defvar tex-shell-cd-command "cd"
240 "Command to give to shell running TeX to change directory.
241The value of `tex-directory' is appended to this, separated by a space.")
242
243(defvar tex-zap-file nil
244 "Temporary file name used for text being sent as input to TeX.
245Should be a simple file name with no extension or directory specification.")
246
247(defvar tex-last-buffer-texed nil
248 "Buffer which was last TeXed.")
249
250(defvar tex-print-file nil
251 "File name that \\[tex-print] prints.
252Set by \\[tex-region], \\[tex-buffer], and \\[tex-file].")
253
254(defvar tex-mode-syntax-table nil
255 "Syntax table used while in TeX mode.")
256
257(defcustom latex-imenu-indent-string "."
258 "*String to add repeated in front of nested sectional units for Imenu.
259An alternative value is \" . \", if you use a font with a narrow period."
260 :type 'string
261 :group 'tex)
262
263(defun latex-imenu-create-index ()
264 "Generates an alist for imenu from a LaTeX buffer."
265 (let (i0 menu case-fold-search)
266 (save-excursion
267 ;; Find the top-most level in this file but don't allow it to be
268 ;; any deeper than "section" (which is top-level in an article).
269 (goto-char (point-min))
270 (if (search-forward-regexp "\\\\part\\*?[ \t]*{" nil t)
271 (setq i0 0)
272 (if (search-forward-regexp "\\\\chapter\\*?[ \t]*{" nil t)
273 (setq i0 1)
274 (setq i0 2)))
275
276 ;; Look for chapters and sections.
277 (goto-char (point-min))
278 (while (search-forward-regexp
279 "\\\\\\(part\\|chapter\\|section\\|subsection\\|\
280subsubsection\\|paragraph\\|subparagraph\\)\\*?[ \t]*{" nil t)
281 (let ((start (match-beginning 0))
282 (here (point))
283 (i (cdr (assoc (buffer-substring-no-properties
284 (match-beginning 1)
285 (match-end 1))
286 '(("part" . 0) ("chapter" . 1)
287 ("section" . 2) ("subsection" . 3)
288 ("subsubsection" . 4)
289 ("paragraph" . 5) ("subparagraph" . 6))))))
290 (backward-char 1)
291 (condition-case err
292 (progn
293 ;; Using sexps allows some use of matching {...} inside
294 ;; titles.
295 (forward-sexp 1)
296 (setq menu
297 (cons (cons (concat (apply 'concat
298 (make-list
299 (max 0 (- i i0))
300 latex-imenu-indent-string))
301 (buffer-substring-no-properties
302 here (1- (point))))
303 start)
304 menu))
305 )
306 (error nil))))
307
308 ;; Look for included material.
309 (goto-char (point-min))
310 (while (search-forward-regexp
311 "\\\\\\(include\\|input\\|verbatiminput\\|bibliography\\)\
312[ \t]*{\\([^}\n]+\\)}"
313 nil t)
314 (setq menu
315 (cons (cons (concat "<<" (buffer-substring-no-properties
316 (match-beginning 2)
317 (match-end 2))
318 (if (= (char-after (match-beginning 1)) ?b)
319 ".bbl"
320 ".tex"))
321 (match-beginning 0))
322 menu)))
323
324 ;; Look for \frontmatter, \mainmatter, \backmatter, and \appendix.
325 (goto-char (point-min))
326 (while (search-forward-regexp
327 "\\\\\\(frontmatter\\|mainmatter\\|backmatter\\|appendix\\)\\b"
328 nil t)
329 (setq menu
330 (cons (cons "--" (match-beginning 0))
331 menu)))
332
333 ;; Sort in increasing buffer position order.
334 (sort menu (function (lambda (a b) (< (cdr a) (cdr b))))))))
335
336(defun tex-define-common-keys (keymap)
337 "Define the keys that we want defined both in TeX mode and in the TeX shell."
338 (define-key keymap "\C-c\C-k" 'tex-kill-job)
339 (define-key keymap "\C-c\C-l" 'tex-recenter-output-buffer)
340 (define-key keymap "\C-c\C-q" 'tex-show-print-queue)
341 (define-key keymap "\C-c\C-p" 'tex-print)
342 (define-key keymap "\C-c\C-v" 'tex-view)
343
344 (define-key keymap [menu-bar tex] (cons "TeX" (make-sparse-keymap "TeX")))
345
346 (define-key keymap [menu-bar tex tex-kill-job] '("Tex Kill" . tex-kill-job))
347 (define-key keymap [menu-bar tex tex-recenter-output-buffer]
348 '("Tex Recenter" . tex-recenter-output-buffer))
349 (define-key keymap [menu-bar tex tex-show-print-queue]
350 '("Show Print Queue" . tex-show-print-queue))
351 (define-key keymap [menu-bar tex tex-alt-print]
352 '("Tex Print (alt printer)" . tex-alt-print))
353 (define-key keymap [menu-bar tex tex-print] '("Tex Print" . tex-print))
354 (define-key keymap [menu-bar tex tex-view] '("Tex View" . tex-view))
355 )
356
357(defvar tex-mode-map nil "Keymap for TeX mode.")
358
359(if tex-mode-map
360 nil
361 (setq tex-mode-map (make-sparse-keymap))
362 (tex-define-common-keys tex-mode-map)
363 (define-key tex-mode-map "\"" 'tex-insert-quote)
364 (define-key tex-mode-map "\n" 'tex-terminate-paragraph)
365 (define-key tex-mode-map "\C-c}" 'up-list)
366 (define-key tex-mode-map "\C-c{" 'tex-insert-braces)
367 (define-key tex-mode-map "\C-c\C-r" 'tex-region)
368 (define-key tex-mode-map "\C-c\C-b" 'tex-buffer)
369 (define-key tex-mode-map "\C-c\C-f" 'tex-file)
370 (define-key tex-mode-map "\C-c\C-i" 'tex-bibtex-file)
371 (define-key tex-mode-map "\C-c\C-o" 'tex-latex-block)
372 (define-key tex-mode-map "\C-c\C-e" 'tex-close-latex-block)
373 (define-key tex-mode-map "\C-c\C-u" 'tex-goto-last-unclosed-latex-block)
374 (define-key tex-mode-map [menu-bar tex tex-bibtex-file]
375 '("BibTeX File" . tex-bibtex-file))
376 (define-key tex-mode-map [menu-bar tex tex-validate-region]
377 '("Validate Region" . tex-validate-region))
378 (define-key tex-mode-map [menu-bar tex validate-tex-buffer]
379 '("Validate Buffer" . validate-tex-buffer))
380 (define-key tex-mode-map [menu-bar tex tex-region]
381 '("TeX Region" . tex-region))
382 (define-key tex-mode-map [menu-bar tex tex-buffer]
383 '("TeX Buffer" . tex-buffer))
384 (define-key tex-mode-map [menu-bar tex tex-file] '("TeX File" . tex-file)))
385
386(put 'tex-region 'menu-enable 'mark-active)
387(put 'tex-validate-region 'menu-enable 'mark-active)
388(put 'tex-print 'menu-enable '(stringp tex-print-file))
389(put 'tex-alt-print 'menu-enable '(stringp tex-print-file))
390(put 'tex-view 'menu-enable '(stringp tex-print-file))
391(put 'tex-recenter-output-buffer 'menu-enable '(get-buffer "*tex-shell*"))
392(put 'tex-kill-job 'menu-enable '(tex-shell-running))
393
394
395(defvar tex-shell-map nil
396 "Keymap for the TeX shell.
397Inherits `shell-mode-map' with a few additions.")
398
399(defvar tex-face-alist
400 '((bold . "{\\bf ")
401 (italic . "{\\it ")
402 (bold-italic . "{\\bi ") ; hypothetical
403 (underline . "\\underline{")
404 (default . "{\\rm "))
405 "Alist of face and TeX font name for facemenu.")
406
407(defvar tex-latex-face-alist
408 `((italic . "{\\em ")
409 ,@tex-face-alist)
410 "Alist of face and LaTeX font name for facemenu.")
411
412
413(defvar compare-windows-whitespace) ; Pacify the byte-compiler
414
415;;; This would be a lot simpler if we just used a regexp search,
416;;; but then it would be too slow.
417;;;###autoload
418(defun tex-mode ()
419 "Major mode for editing files of input for TeX, LaTeX, or SliTeX.
420Tries to determine (by looking at the beginning of the file) whether
421this file is for plain TeX, LaTeX, or SliTeX and calls `plain-tex-mode',
422`latex-mode', or `slitex-mode', respectively. If it cannot be determined,
423such as if there are no commands in the file, the value of `tex-default-mode'
424says which mode to use."
425 (interactive)
426 (let (mode slash comment)
427 (save-excursion
428 (goto-char (point-min))
429 (while (and (setq slash (search-forward "\\" nil t))
430 (setq comment (let ((search-end (point)))
431 (save-excursion
432 (beginning-of-line)
433 (search-forward "%" search-end t))))))
434 (if (and slash (not comment))
435 (setq mode (if (looking-at "documentstyle\\|documentclass\\|begin\\b\\|NeedsTeXFormat{LaTeX")
436 (if (looking-at
437 "document\\(style\\|class\\)\\(\\[.*\\]\\)?{slides}")
438 'slitex-mode
439 'latex-mode)
440 'plain-tex-mode))))
441 (if mode (funcall mode)
442 (funcall tex-default-mode))))
443
444;;;###autoload
445(defalias 'TeX-mode 'tex-mode)
446;;;###autoload
447(defalias 'LaTeX-mode 'latex-mode)
448
449;;;###autoload
450(defun plain-tex-mode ()
451 "Major mode for editing files of input for plain TeX.
452Makes $ and } display the characters they match.
453Makes \" insert `` when it seems to be the beginning of a quotation,
454and '' when it appears to be the end; it inserts \" only after a \\.
455
456Use \\[tex-region] to run TeX on the current region, plus a \"header\"
457copied from the top of the file (containing macro definitions, etc.),
458running TeX under a special subshell. \\[tex-buffer] does the whole buffer.
459\\[tex-file] saves the buffer and then processes the file.
460\\[tex-print] prints the .dvi file made by any of these.
461\\[tex-view] previews the .dvi file made by any of these.
462\\[tex-bibtex-file] runs bibtex on the file of the current buffer.
463
464Use \\[validate-tex-buffer] to check buffer for paragraphs containing
465mismatched $'s or braces.
466
467Special commands:
468\\{tex-mode-map}
469
470Mode variables:
471tex-run-command
472 Command string used by \\[tex-region] or \\[tex-buffer].
473tex-directory
474 Directory in which to create temporary files for TeX jobs
475 run by \\[tex-region] or \\[tex-buffer].
476tex-dvi-print-command
477 Command string used by \\[tex-print] to print a .dvi file.
478tex-alt-dvi-print-command
479 Alternative command string used by \\[tex-print] (when given a prefix
480 argument) to print a .dvi file.
481tex-dvi-view-command
482 Command string used by \\[tex-view] to preview a .dvi file.
483tex-show-queue-command
484 Command string used by \\[tex-show-print-queue] to show the print
485 queue that \\[tex-print] put your job on.
486
487Entering Plain-tex mode runs the hook `text-mode-hook', then the hook
488`tex-mode-hook', and finally the hook `plain-tex-mode-hook'. When the
489special subshell is initiated, the hook `tex-shell-hook' is run."
490
491 (interactive)
492 (tex-common-initialization)
493 (setq mode-name "TeX")
494 (setq major-mode 'plain-tex-mode)
495 (setq tex-command tex-run-command)
496 (setq tex-start-of-header "%\\*\\*start of header")
497 (setq tex-end-of-header "%\\*\\*end of header")
498 (setq tex-trailer "\\bye\n")
499 (run-hooks 'text-mode-hook 'tex-mode-hook 'plain-tex-mode-hook))
500;;;###autoload
501(defalias 'plain-TeX-mode 'plain-tex-mode)
502
503;;;###autoload
504(defun latex-mode ()
505 "Major mode for editing files of input for LaTeX.
506Makes $ and } display the characters they match.
507Makes \" insert `` when it seems to be the beginning of a quotation,
508and '' when it appears to be the end; it inserts \" only after a \\.
509
510Use \\[tex-region] to run LaTeX on the current region, plus the preamble
511copied from the top of the file (containing \\documentstyle, etc.),
512running LaTeX under a special subshell. \\[tex-buffer] does the whole buffer.
513\\[tex-file] saves the buffer and then processes the file.
514\\[tex-print] prints the .dvi file made by any of these.
515\\[tex-view] previews the .dvi file made by any of these.
516\\[tex-bibtex-file] runs bibtex on the file of the current buffer.
517
518Use \\[validate-tex-buffer] to check buffer for paragraphs containing
519mismatched $'s or braces.
520
521Special commands:
522\\{tex-mode-map}
523
524Mode variables:
525latex-run-command
526 Command string used by \\[tex-region] or \\[tex-buffer].
527tex-directory
528 Directory in which to create temporary files for LaTeX jobs
529 run by \\[tex-region] or \\[tex-buffer].
530tex-dvi-print-command
531 Command string used by \\[tex-print] to print a .dvi file.
532tex-alt-dvi-print-command
533 Alternative command string used by \\[tex-print] (when given a prefix
534 argument) to print a .dvi file.
535tex-dvi-view-command
536 Command string used by \\[tex-view] to preview a .dvi file.
537tex-show-queue-command
538 Command string used by \\[tex-show-print-queue] to show the print
539 queue that \\[tex-print] put your job on.
540
541Entering Latex mode runs the hook `text-mode-hook', then
542`tex-mode-hook', and finally `latex-mode-hook'. When the special
543subshell is initiated, `tex-shell-hook' is run."
544 (interactive)
545 (tex-common-initialization)
546 (setq mode-name "LaTeX")
547 (setq major-mode 'latex-mode)
548 (setq tex-command latex-run-command)
549 (setq tex-start-of-header "\\\\documentstyle\\|\\\\documentclass")
550 (setq tex-end-of-header "\\\\begin{document}")
551 (setq tex-trailer "\\end{document}\n")
552 ;; A line containing just $$ is treated as a paragraph separator.
553 ;; A line starting with $$ starts a paragraph,
554 ;; but does not separate paragraphs if it has more stuff on it.
555 (setq paragraph-start "[ \t]*$\\|[\f%]\\|[ \t]*\\$\\$\\|\
556\\\\begin\\>\\|\\\\label\\>\\|\\\\end\\>\\|\\\\\\[\\|\\\\\\]\\|\
557\\\\chapter\\>\\|\\\\section\\>\\|\
558\\\\subsection\\>\\|\\\\subsubsection\\>\\|\
559\\\\paragraph\\>\\|\\\\subparagraph\\>\\|\
560\\\\item\\>\\|\\\\bibitem\\>\\|\\\\newline\\>\\|\\\\noindent\\>\\|\
561\\\\[a-z]*space\\>\\|\\\\[a-z]*skip\\>\\|\
562\\\\newpage\\>\\|\\\\[a-z]*page\\|\\\\footnote\\>\\|\
563\\\\marginpar\\>\\|\\\\parbox\\>\\|\\\\caption\\>")
564 (setq paragraph-separate "[ \t]*$\\|[\f%]\\|[ \t]*\\$\\$[ \t]*$\\|\
565\\\\begin\\>\\|\\\\label\\>\\|\\\\end\\>\\|\\\\\\[\\|\\\\\\]\\|\
566\\\\chapter\\>\\|\\\\section\\>\\|\
567\\\\subsection\\>\\|\\\\subsubsection\\>\\|\
568\\\\paragraph\\>\\|\\\\subparagraph\\>\\|\
569\\(\\\\item\\|\\\\bibitem\\|\\\\newline\\|\\\\noindent\\|\
570\\\\[a-z]*space\\|\\\\[a-z]*skip\\|\
571\\\\newpage\\|\\\\[a-z]*page[a-z]*\\|\\\\footnote\\|\
572\\\\marginpar\\|\\\\parbox\\|\\\\caption\\)[ \t]*\\($\\|%\\)")
573 (make-local-variable 'imenu-create-index-function)
574 (setq imenu-create-index-function 'latex-imenu-create-index)
575 (make-local-variable 'tex-face-alist)
576 (setq tex-face-alist tex-latex-face-alist)
577 (make-local-variable 'fill-nobreak-predicate)
578 (setq fill-nobreak-predicate 'latex-fill-nobreak-predicate)
579 (run-hooks 'text-mode-hook 'tex-mode-hook 'latex-mode-hook))
580
581;;;###autoload
582(defun slitex-mode ()
583 "Major mode for editing files of input for SliTeX.
584Makes $ and } display the characters they match.
585Makes \" insert `` when it seems to be the beginning of a quotation,
586and '' when it appears to be the end; it inserts \" only after a \\.
587
588Use \\[tex-region] to run SliTeX on the current region, plus the preamble
589copied from the top of the file (containing \\documentstyle, etc.),
590running SliTeX under a special subshell. \\[tex-buffer] does the whole buffer.
591\\[tex-file] saves the buffer and then processes the file.
592\\[tex-print] prints the .dvi file made by any of these.
593\\[tex-view] previews the .dvi file made by any of these.
594\\[tex-bibtex-file] runs bibtex on the file of the current buffer.
595
596Use \\[validate-tex-buffer] to check buffer for paragraphs containing
597mismatched $'s or braces.
598
599Special commands:
600\\{tex-mode-map}
601
602Mode variables:
603slitex-run-command
604 Command string used by \\[tex-region] or \\[tex-buffer].
605tex-directory
606 Directory in which to create temporary files for SliTeX jobs
607 run by \\[tex-region] or \\[tex-buffer].
608tex-dvi-print-command
609 Command string used by \\[tex-print] to print a .dvi file.
610tex-alt-dvi-print-command
611 Alternative command string used by \\[tex-print] (when given a prefix
612 argument) to print a .dvi file.
613tex-dvi-view-command
614 Command string used by \\[tex-view] to preview a .dvi file.
615tex-show-queue-command
616 Command string used by \\[tex-show-print-queue] to show the print
617 queue that \\[tex-print] put your job on.
618
619Entering SliTeX mode runs the hook `text-mode-hook', then the hook
620`tex-mode-hook', then the hook `latex-mode-hook', and finally the hook
621`slitex-mode-hook'. When the special subshell is initiated, the hook
622`tex-shell-hook' is run."
623 (interactive)
624 (tex-common-initialization)
625 (setq mode-name "SliTeX")
626 (setq major-mode 'slitex-mode)
627 (setq tex-command slitex-run-command)
628 (setq tex-start-of-header "\\\\documentstyle{slides}\\|\\\\documentclass{slides}")
629 (setq tex-end-of-header "\\\\begin{document}")
630 (setq tex-trailer "\\end{document}\n")
631 ;; A line containing just $$ is treated as a paragraph separator.
632 ;; A line starting with $$ starts a paragraph,
633 ;; but does not separate paragraphs if it has more stuff on it.
634 (setq paragraph-start "[ \t]*$\\|[\f%]\\|[ \t]*\\$\\$\\|\
635\\\\begin\\>\\|\\\\label\\>\\|\\\\end\\>\\|\\\\\\[\\|\\\\\\]\\|\
636\\\\chapter\\>\\|\\\\section\\>\\|\
637\\\\subsection\\>\\|\\\\subsubsection\\>\\|\
638\\\\paragraph\\>\\|\\\\subparagraph\\>\\|\
639\\\\item\\>\\|\\\\bibitem\\>\\|\\\\newline\\>\\|\\\\noindent\\>\\|\
640\\\\[a-z]*space\\>\\|\\\\[a-z]*skip\\>\\|\
641\\\\newpage\\>\\|\\\\[a-z]*page\\|\\\\footnote\\>\\|\
642\\\\marginpar\\>\\|\\\\parbox\\>\\|\\\\caption\\>")
643 (setq paragraph-separate "[ \t]*$\\|[\f%]\\|[ \t]*\\$\\$[ \t]*$\\|\
644\\\\begin\\>\\|\\\\label\\>\\|\\\\end\\>\\|\\\\\\[\\|\\\\\\]\\|\
645\\\\chapter\\>\\|\\\\section\\>\\|\
646\\\\subsection\\>\\|\\\\subsubsection\\>\\|\
647\\\\paragraph\\>\\|\\\\subparagraph\\>\\|\
648\\\\item[ \t]*$\\|\\\\bibitem[ \t]*$\\|\\\\newline[ \t]*$\\|\\\\noindent[ \t]*$\\|\
649\\\\[a-z]*space[ \t]*$\\|\\\\[a-z]*skip[ \t]*$\\|\
650\\\\newpage[ \t]*$\\|\\\\[a-z]*page[a-z]*[ \t]*$\\|\\\\footnote[ \t]*$\\|\
651\\\\marginpar[ \t]*$\\|\\\\parbox[ \t]*$\\|\\\\caption[ \t]*$")
652 (make-local-variable 'imenu-create-index-function)
653 (setq imenu-create-index-function 'latex-imenu-create-index)
654 (make-local-variable 'tex-face-alist)
655 (setq tex-face-alist tex-latex-face-alist)
656 (make-local-variable 'fill-nobreak-predicate)
657 (setq fill-nobreak-predicate 'latex-fill-nobreak-predicate)
658 (run-hooks
659 'text-mode-hook 'tex-mode-hook 'latex-mode-hook 'slitex-mode-hook))
660
661(defun tex-common-initialization ()
662 (kill-all-local-variables)
663 (use-local-map tex-mode-map)
664 (setq local-abbrev-table text-mode-abbrev-table)
665 (if (null tex-mode-syntax-table)
666 (let ((char 0))
667 (setq tex-mode-syntax-table (make-syntax-table))
668 (set-syntax-table tex-mode-syntax-table)
669 (while (< char ? )
670 (modify-syntax-entry char ".")
671 (setq char (1+ char)))
672 (modify-syntax-entry ?\C-@ "w")
673 (modify-syntax-entry ?\t " ")
674 (modify-syntax-entry ?\n ">")
675 (modify-syntax-entry ?\f ">")
676 (modify-syntax-entry ?$ "$$")
677 (modify-syntax-entry ?% "<")
678 (modify-syntax-entry ?\\ "/")
679 (modify-syntax-entry ?\" ".")
680 (modify-syntax-entry ?& ".")
681 (modify-syntax-entry ?_ ".")
682 (modify-syntax-entry ?@ "_")
683 (modify-syntax-entry ?~ " ")
684 (modify-syntax-entry ?' "w"))
685 (set-syntax-table tex-mode-syntax-table))
686 ;; Regexp isearch should accept newline and formfeed as whitespace.
687 (make-local-variable 'search-whitespace-regexp)
688 (setq search-whitespace-regexp "[ \t\r\n\f]+")
689 (make-local-variable 'paragraph-start)
690 ;; A line containing just $$ is treated as a paragraph separator.
691 (setq paragraph-start "[ \t]*$\\|[\f\\\\%]\\|[ \t]*\\$\\$")
692 (make-local-variable 'paragraph-separate)
693 ;; A line starting with $$ starts a paragraph,
694 ;; but does not separate paragraphs if it has more stuff on it.
695 (setq paragraph-separate "[ \t]*$\\|[\f\\\\%]\\|[ \t]*\\$\\$[ \t]*$")
696 (make-local-variable 'comment-start)
697 (setq comment-start "%")
698 (make-local-variable 'comment-start-skip)
699 (setq comment-start-skip "\\(\\(^\\|[^\\]\\)\\(\\\\\\\\\\)*\\)\\(%+ *\\)")
700 (make-local-variable 'comment-indent-function)
701 (setq comment-indent-function 'tex-comment-indent)
702 (make-local-variable 'parse-sexp-ignore-comments)
703 (setq parse-sexp-ignore-comments t)
704 (make-local-variable 'compare-windows-whitespace)
705 (setq compare-windows-whitespace 'tex-categorize-whitespace)
706 (make-local-variable 'skeleton-further-elements)
707 (setq skeleton-further-elements
708 '((indent-line-function 'indent-relative-maybe)))
709 (make-local-variable 'facemenu-add-face-function)
710 (make-local-variable 'facemenu-end-add-face)
711 (make-local-variable 'facemenu-remove-face-function)
712 (setq facemenu-add-face-function
713 (lambda (face end)
714 (let ((face-text (cdr (assq face tex-face-alist))))
715 (if face-text
716 face-text
717 (error "Face %s not configured for %s mode" face mode-name))))
718 facemenu-end-add-face "}"
719 facemenu-remove-face-function t)
720 (make-local-variable 'tex-command)
721 (make-local-variable 'tex-start-of-header)
722 (make-local-variable 'tex-end-of-header)
723 (make-local-variable 'tex-trailer))
724
725(defun tex-comment-indent ()
726 (if (looking-at "%%%")
727 (current-column)
728 (skip-chars-backward " \t")
729 (max (if (bolp) 0 (1+ (current-column)))
730 comment-column)))
731
732(defun tex-categorize-whitespace (backward-limit)
733 ;; compare-windows-whitespace is set to this.
734 ;; This is basically a finite-state machine.
735 ;; Returns a symbol telling how TeX would treat
736 ;; the whitespace we are looking at: null, space, or par.
737 (let ((category 'null)
738 (not-finished t))
739 (skip-chars-backward " \t\n\f" backward-limit)
740 (while not-finished
741 (cond ((looking-at "[ \t]+")
742 (goto-char (match-end 0))
743 (if (eq category 'null)
744 (setq category 'space)))
745 ((looking-at "\n")
746 (cond ((eq category 'newline)
747 (setq category 'par)
748 (setq not-finished nil))
749 (t
750 (setq category 'newline) ;a strictly internal state
751 (goto-char (match-end 0)))))
752 ((looking-at "\f+")
753 (setq category 'par)
754 (setq not-finished nil))
755 (t
756 (setq not-finished nil))))
757 (skip-chars-forward " \t\n\f")
758 (if (eq category 'newline)
759 'space ;TeX doesn't distinguish
760 category)))
761
762(defun tex-insert-quote (arg)
763 "Insert the appropriate quote marks for TeX.
764Inserts the value of `tex-open-quote' (normally ``) or `tex-close-quote'
765\(normally '') depending on the context. With prefix argument, always
766inserts \" characters."
767 (interactive "*P")
768 (if arg
769 (self-insert-command (prefix-numeric-value arg))
770 (insert
771 (cond ((or (bobp)
772 (save-excursion
773 (forward-char -1)
774 (looking-at "\\s(\\|\\s \\|\\s>")))
775 tex-open-quote)
776 ((= (preceding-char) ?\\)
777 ?\")
778 (t
779 tex-close-quote)))))
780
781(defun validate-tex-buffer ()
782 "Check current buffer for paragraphs containing mismatched $s.
783Their positions are recorded in the buffer `*Occur*'.
784To find a particular invalidity from `*Occur*',
785switch to to that buffer and type C-c C-c on the line
786for the invalidity you want to see."
787 (interactive)
788 (let ((buffer (current-buffer))
789 (prevpos (point-min))
790 (linenum nil))
791 (with-output-to-temp-buffer "*Occur*"
792 (princ "Mismatches:\n")
793 (save-excursion
794 (set-buffer standard-output)
795 (occur-mode)
796 (setq occur-buffer buffer)
797 (setq occur-nlines 0)
798 (setq occur-pos-list nil))
799 (save-excursion
800 (goto-char (point-max))
801 (while (and (not (input-pending-p)) (not (bobp)))
802 (let ((end (point))
803 prev-end)
804 ;; Scan the previous paragraph for invalidities.
805 (if (search-backward "\n\n" nil t)
806 (progn
807 (setq prev-end (point))
808 (forward-char 2))
809 (goto-char (setq prev-end (point-min))))
810 (or (tex-validate-region (point) end)
811 (let* ((oend end)
812 (end (save-excursion (forward-line 1) (point)))
813 start tem)
814 (beginning-of-line)
815 (setq start (point))
816 ;; Keep track of line number as we scan,
817 ;; in a cumulative fashion.
818 (if linenum
819 (setq linenum (- linenum (count-lines prevpos (point))))
820 (setq linenum (1+ (count-lines 1 start))))
821 (setq prevpos (point))
822 ;; Mention this mismatch in *Occur*.
823 ;; Since we scan from end of buffer to beginning,
824 ;; add each mismatch at the beginning of *Occur*
825 ;; and at the beginning of occur-pos-list.
826 (save-excursion
827 (setq tem (point-marker))
828 (set-buffer standard-output)
829 (goto-char (point-min))
830 ;; Skip "Mismatches:" header line.
831 (forward-line 1)
832 (setq occur-pos-list (cons tem occur-pos-list))
833 (insert-buffer-substring buffer start end)
834 (forward-char (- start end))
835 (insert (format "%3d: " linenum)))))
836 (goto-char prev-end))))
837 (save-excursion
838 (set-buffer standard-output)
839 (if (null occur-pos-list)
840 (insert "None!\n"))
841 (if (interactive-p)
842 (message "%d mismatches found" (length occur-pos-list)))))))
843
844(defun tex-validate-region (start end)
845 "Check for mismatched braces or $'s in region.
846Returns t if no mismatches. Returns nil and moves point to suspect
847area if a mismatch is found."
848 (interactive "r")
849 (let ((failure-point nil) (max-possible-sexps (- end start)))
850 (save-excursion
851 (condition-case ()
852 (save-restriction
853 (narrow-to-region start end)
854 ;; First check that the open and close parens balance in numbers.
855 (goto-char start)
856 (while (< 0 (setq max-possible-sexps (1- max-possible-sexps)))
857 (forward-sexp 1))
858 ;; Now check that like matches like.
859 (goto-char start)
860 (while (progn (skip-syntax-forward "^(")
861 (not (eobp)))
862 (let ((match (matching-paren (following-char))))
863 (save-excursion
864 (forward-sexp 1)
865 (or (= (preceding-char) match)
866 (error "Mismatched parentheses"))))
867 (forward-char 1)))
868 (error
869 (skip-syntax-forward " .>")
870 (setq failure-point (point)))))
871 (if failure-point
872 (progn
873 (goto-char failure-point)
874 nil)
875 t)))
876
877(defun tex-terminate-paragraph (inhibit-validation)
878 "Insert two newlines, breaking a paragraph for TeX.
879Check for mismatched braces or $s in paragraph being terminated.
880A prefix arg inhibits the checking."
881 (interactive "*P")
882 (or inhibit-validation
883 (save-excursion
884 (tex-validate-region
885 (save-excursion
886 (search-backward "\n\n" nil 'move)
887 (point))
888 (point)))
889 (message "Paragraph being closed appears to contain a mismatch"))
890 (insert "\n\n"))
891
892(defun tex-insert-braces ()
893 "Make a pair of braces and be poised to type inside of them."
894 (interactive "*")
895 (insert ?\{)
896 (save-excursion
897 (insert ?})))
898
899;; This function is used as the value of fill-nobreak-predicate
900;; in LaTeX mode. Its job is to prevent line-breaking inside
901;; of a \verb construct.
902(defun latex-fill-nobreak-predicate ()
903 (let ((opoint (point))
904 inside)
905 (save-excursion
906 (save-restriction
907 (beginning-of-line)
908 (narrow-to-region (point) opoint)
909 (while (re-search-forward "\\\\verb\\(.\\)" nil t)
910 (unless (re-search-forward (regexp-quote (match-string 1)) nil t)
911 (setq inside t)))))
912 inside))
913
914;;; Like tex-insert-braces, but for LaTeX.
915(define-skeleton tex-latex-block
916 "Create a matching pair of lines \\begin[OPT]{NAME} and \\end{NAME} at point.
917Puts point on a blank line between them."
918 (completing-read "LaTeX block name: "
919 (mapcar 'list
920 (append standard-latex-block-names
921 latex-block-names)))
922 "\\begin["
923 (skeleton-read "[options]: ") & ?\] | -1
924 ?\{
925 str
926 ?\} \n
927 _ \n
928 "\\end{" str ?\})
929
930(defun tex-last-unended-begin ()
931 "Leave point at the beginning of the last `\\begin{...}' that is unended."
932 (while (and (re-search-backward "\\(\\\\begin\\s *{\\)\\|\\(\\\\end\\s *{\\)")
933 (looking-at "\\\\end{"))
934 (tex-last-unended-begin)))
935
936(defun tex-goto-last-unclosed-latex-block ()
937 "Move point to the last unclosed \\begin{...}.
938Mark is left at original location."
939 (interactive)
940 (let ((spot))
941 (save-excursion
942 (condition-case nil
943 (tex-last-unended-begin)
944 (error (error "Couldn't find unended \\begin")))
945 (setq spot (point)))
946 (push-mark)
947 (goto-char spot)))
948
949(defun tex-close-latex-block ()
950 "Creates an \\end{...} to match the last unclosed \\begin{...}."
951 (interactive "*")
952 (let ((new-line-needed (bolp))
953 text indentation)
954 (save-excursion
955 (condition-case nil
956 (tex-last-unended-begin)
957 (error (error "Couldn't find unended \\begin")))
958 (setq indentation (current-column))
959 (re-search-forward "\\\\begin\\(\\s *{[^}\n]*}\\)")
960 (setq text (buffer-substring (match-beginning 1) (match-end 1))))
961 (indent-to indentation)
962 (insert "\\end" text)
963 (if new-line-needed (insert ?\n))))
964\f
965;;; Invoking TeX in an inferior shell.
966
967;;; Why use a shell instead of running TeX directly? Because if TeX
968;;; gets stuck, the user can switch to the shell window and type at it.
969
970;;; The utility functions:
971
972;;;###autoload
973(defun tex-start-shell ()
974 (save-excursion
975 (set-buffer
976 (make-comint
977 "tex-shell"
978 (or tex-shell-file-name (getenv "ESHELL") (getenv "SHELL") "/bin/sh")
979 nil))
980 (let ((proc (get-process "tex-shell")))
981 (set-process-sentinel proc 'tex-shell-sentinel)
982 (process-kill-without-query proc)
983 (setq comint-prompt-regexp shell-prompt-pattern)
984 (setq tex-shell-map (nconc (make-sparse-keymap) shell-mode-map))
985 (tex-define-common-keys tex-shell-map)
986 (use-local-map tex-shell-map)
987 (compilation-minor-mode)
988 (run-hooks 'tex-shell-hook)
989 (setq comint-input-filter-functions 'shell-directory-tracker)
990 (while (zerop (buffer-size))
991 (sleep-for 1)))))
992
993(defun tex-display-shell ()
994 "Make the TeX shell buffer visible in a window."
995 (display-buffer (process-buffer (get-process "tex-shell")))
996 (tex-recenter-output-buffer nil))
997
998(defun tex-shell-sentinel (proc msg)
999 (cond ((null (buffer-name (process-buffer proc)))
1000 ;; buffer killed
1001 (set-process-buffer proc nil)
1002 (tex-delete-last-temp-files))
1003 ((memq (process-status proc) '(signal exit))
1004 (tex-delete-last-temp-files))))
1005
1006(defun tex-set-buffer-directory (buffer directory)
1007 "Set BUFFER's default directory to be DIRECTORY."
1008 (setq directory (file-name-as-directory (expand-file-name directory)))
1009 (if (not (file-directory-p directory))
1010 (error "%s is not a directory" directory)
1011 (save-excursion
1012 (set-buffer buffer)
1013 (setq default-directory directory))))
1014
1015(defvar tex-send-command-modified-tick 0)
1016(make-variable-buffer-local 'tex-send-command-modified-tick)
1017
1018(defun tex-send-command (command &optional file background)
1019 "Send COMMAND to TeX shell process, substituting optional FILE for *.
1020Do this in background if optional BACKGROUND is t. If COMMAND has no *,
1021FILE will be appended, preceded by a blank, to COMMAND. If FILE is nil, no
1022substitution will be made in COMMAND. COMMAND can be any expression that
1023evaluates to a command string.
1024
1025Return the process in which TeX is running."
1026 (save-excursion
1027 (let* ((cmd (eval command))
1028 (proc (or (get-process "tex-shell") (error "No TeX subprocess")))
1029 (buf (process-buffer proc))
1030 (star (string-match "\\*" cmd))
1031 (string
1032 (concat
1033 (if file
1034 (if star (concat (substring cmd 0 star)
1035 file (substring cmd (1+ star)))
1036 (concat cmd " " file))
1037 cmd)
1038 (if background "&" ""))))
1039 ;; Switch to buffer before checking for subproc output in it.
1040 (set-buffer buf)
1041 ;; If text is unchanged since previous tex-send-command,
1042 ;; we haven't got any output. So wait for output now.
1043 (if (= (buffer-modified-tick buf) tex-send-command-modified-tick)
1044 (accept-process-output proc))
1045 (goto-char (process-mark proc))
1046 (insert string)
1047 (comint-send-input)
1048 (setq tex-send-command-modified-tick (buffer-modified-tick buf))
1049 proc)))
1050
1051(defun tex-delete-last-temp-files (&optional not-all)
1052 "Delete any junk files from last temp file.
1053If NOT-ALL is non-nil, save the `.dvi' file."
1054 (if tex-last-temp-file
1055 (let* ((dir (file-name-directory tex-last-temp-file))
1056 (list (and (file-directory-p dir)
1057 (file-name-all-completions
1058 (file-name-sans-extension
1059 (file-name-nondirectory tex-last-temp-file))
1060 dir))))
1061 (while list
1062 (if not-all
1063 (and
1064 ;; If arg is non-nil, don't delete the .dvi file.
1065 (not (string-match "\\.dvi$" (car list)))
1066 (delete-file (concat dir (car list))))
1067 (delete-file (concat dir (car list))))
1068 (setq list (cdr list))))))
1069
1070(add-hook 'kill-emacs-hook 'tex-delete-last-temp-files)
1071
1072(defvar tex-start-tex-marker nil
1073 "Marker pointing after last TeX-running command in the TeX shell buffer.")
1074
1075(defun tex-start-tex (command file)
1076 "Start a TeX run, using COMMAND on FILE."
1077 (let* ((star (string-match "\\*" command))
1078 (compile-command
1079 (if star
1080 (concat (substring command 0 star)
1081 (comint-quote-filename file)
1082 (substring command (1+ star)))
1083 (concat command " "
1084 (comint-quote-filename tex-start-options-string) " "
1085 (comint-quote-filename file)))))
1086 (with-current-buffer (process-buffer (tex-send-command compile-command))
1087 (save-excursion
1088 (forward-line -1)
1089 (setq tex-start-tex-marker (point-marker)))
1090 (make-local-variable 'compilation-parse-errors-function)
1091 (setq compilation-parse-errors-function 'tex-compilation-parse-errors))))
1092\f
1093(defun tex-compilation-parse-errors (limit-search find-at-least)
1094 "Parse the current buffer as error messages.
1095This makes a list of error descriptors, `compilation-error-list'.
1096For each source-file, line-number pair in the buffer,
1097the source file is read in, and the text location is saved in
1098`compilation-error-list'. The function `next-error', assigned to
1099\\[next-error], takes the next error off the list and visits its location.
1100
1101If LIMIT-SEARCH is non-nil, don't bother parsing past that location.
1102If FIND-AT-LEAST is non-nil, don't bother parsing after finding that
1103
1104This function works on TeX compilations only. It is necessary for
1105that purpose, since TeX does not put file names on the same line as
1106line numbers for the errors."
1107 (require 'thingatpt)
1108 (setq compilation-error-list nil)
1109 (message "Parsing error messages...")
1110 (let ((old-lc-syntax (char-syntax ?\{))
1111 (old-rc-syntax (char-syntax ?\}))
1112 (old-lb-syntax (char-syntax ?\[))
1113 (old-rb-syntax (char-syntax ?\]))
1114 (num-found 0) last-filename last-linenum last-position)
1115 (unwind-protect
1116 (progn
1117 (modify-syntax-entry ?\{ "_")
1118 (modify-syntax-entry ?\} "_")
1119 (modify-syntax-entry ?\[ "_")
1120 (modify-syntax-entry ?\] "_")
1121 ;; Don't reparse messages already seen at last parse.
1122 (goto-char (max (or compilation-parsing-end 0)
1123 tex-start-tex-marker))
1124 ;; Don't parse the first two lines as error messages.
1125 ;; This matters for grep.
1126 (if (bobp) (forward-line 2))
1127 (while (re-search-forward
1128 "^l\\.\\([0-9]+\\) \\(\\.\\.\\.\\)?\\(.*\\)$"
1129 (and (or (null find-at-least)
1130 (>= num-found find-at-least)) limit-search) t)
1131 ;; Extract file name and line number from error message.
1132 ;; Line number is 2 away from beginning of line: "l.23"
1133 ;; The file is the one that was opened last and is still open.
1134 ;; We need to find the last open parenthesis.
1135 (let* ((linenum (string-to-int (match-string 1)))
1136 (error-text (regexp-quote (match-string 3)))
1137 (filename
1138 (save-excursion
1139 (backward-up-list 1)
1140 (skip-syntax-forward "(_")
1141 (while (not (file-readable-p
1142 (thing-at-point 'filename)))
1143 (skip-syntax-backward "(_")
1144 (backward-up-list 1)
1145 (skip-syntax-forward "(_"))
1146 (thing-at-point 'filename)))
1147 (error-marker
1148 (save-excursion
1149 (re-search-backward "^! " nil t)
1150 (point-marker)))
1151 (new-file (or (null last-filename)
1152 (not (string-equal last-filename filename))))
1153 (error-location
1154 (save-excursion
1155 (if (equal filename tex-last-temp-file)
1156 (set-buffer tex-last-buffer-texed)
1157 (set-buffer (find-file-noselect filename)))
1158 (if new-file
1159 (goto-line linenum)
1160 (goto-char last-position)
1161 (forward-line (- linenum last-linenum)))
1162 ;first try a forward search
1163 ;for the error text, then a
1164 ;backward search limited by
1165 ;the last error
1166 (let ((starting-point (point)))
1167 (or (re-search-forward error-text nil t)
1168 (re-search-backward
1169 error-text
1170 (marker-position last-position) t)
1171 (goto-char starting-point)))
1172 (point-marker))))
1173 (setq last-filename filename)
1174 (if (or new-file
1175 (not (= last-position error-location)))
1176 (progn
1177 (setq num-found (1+ num-found))
1178 (setq last-position error-location)
1179 (setq last-linenum linenum)
1180 (setq compilation-error-list
1181 (nconc compilation-error-list
1182 (list (cons error-marker
1183 error-location)))))))))
1184 (modify-syntax-entry ?\{ (char-to-string old-lc-syntax))
1185 (modify-syntax-entry ?\} (char-to-string old-rc-syntax))
1186 (modify-syntax-entry ?\[ (char-to-string old-lb-syntax))
1187 (modify-syntax-entry ?\] (char-to-string old-rb-syntax))))
1188 (setq compilation-parsing-end (point))
1189 (message "Parsing error messages...done"))
1190\f
1191;;; The commands:
1192
1193(defun tex-region (beg end)
1194 "Run TeX on the current region, via a temporary file.
1195The file's name comes from the variable `tex-zap-file' and the
1196variable `tex-directory' says where to put it.
1197
1198If the buffer has a header, the header is given to TeX before the
1199region itself. The buffer's header is all lines between the strings
1200defined by `tex-start-of-header' and `tex-end-of-header' inclusive.
1201The header must start in the first 100 lines of the buffer.
1202
1203The value of `tex-trailer' is given to TeX as input after the region.
1204
1205The value of `tex-command' specifies the command to use to run TeX."
1206 (interactive "r")
1207 (if (tex-shell-running)
1208 (tex-kill-job)
1209 (tex-start-shell))
1210 (or tex-zap-file
1211 (setq tex-zap-file (tex-generate-zap-file-name)))
1212 ;; Temp file will be written and TeX will be run in zap-directory.
1213 ;; If the TEXINPUTS file has relative directories or if the region has
1214 ;; \input of files, this must be the same directory as the file for
1215 ;; TeX to access the correct inputs. That's why it's safest if
1216 ;; tex-directory is ".".
1217 (let* ((zap-directory
1218 (file-name-as-directory (expand-file-name tex-directory)))
1219 (tex-out-file (concat zap-directory tex-zap-file ".tex")))
1220 ;; Don't delete temp files if we do the same buffer twice in a row.
1221 (or (eq (current-buffer) tex-last-buffer-texed)
1222 (tex-delete-last-temp-files t))
1223 ;; Write the new temp file.
1224 (save-excursion
1225 (save-restriction
1226 (widen)
1227 (goto-char (point-min))
1228 (forward-line 100)
1229 (let ((search-end (point))
1230 (default-directory zap-directory)
1231 (already-output 0))
1232 (goto-char (point-min))
1233
1234 ;; Maybe copy first line, such as `\input texinfo', to temp file.
1235 (and tex-first-line-header-regexp
1236 (looking-at tex-first-line-header-regexp)
1237 (write-region (point)
1238 (progn (forward-line 1)
1239 (setq already-output (point)))
1240 tex-out-file nil nil))
1241
1242 ;; Write out the header, if there is one,
1243 ;; and any of the specified region which extends before it.
1244 ;; But don't repeat anything already written.
1245 (if (re-search-forward tex-start-of-header search-end t)
1246 (let (hbeg)
1247 (beginning-of-line)
1248 (setq hbeg (point)) ;mark beginning of header
1249 (if (re-search-forward tex-end-of-header nil t)
1250 (let (hend)
1251 (forward-line 1)
1252 (setq hend (point)) ;mark end of header
1253 (write-region (max (min hbeg beg) already-output)
1254 hend
1255 tex-out-file
1256 (not (zerop already-output)) nil)
1257 (setq already-output hend)))))
1258
1259 ;; Write out the specified region
1260 ;; (but don't repeat anything already written).
1261 (write-region (max beg already-output) end
1262 tex-out-file
1263 (not (zerop already-output)) nil))
1264 ;; Write the trailer, if any.
1265 ;; Precede it with a newline to make sure it
1266 ;; is not hidden in a comment.
1267 (if tex-trailer
1268 (write-region (concat "\n" tex-trailer) nil
1269 tex-out-file t nil))))
1270 ;; Record the file name to be deleted afterward.
1271 (setq tex-last-temp-file tex-out-file)
1272 (tex-send-command tex-shell-cd-command zap-directory)
1273 (tex-start-tex tex-command tex-out-file)
1274 (tex-display-shell)
1275 (setq tex-print-file tex-out-file)
1276 (setq tex-last-buffer-texed (current-buffer))))
1277
1278(defun tex-buffer ()
1279 "Run TeX on current buffer. See \\[tex-region] for more information.
1280Does not save the buffer, so it's useful for trying experimental versions.
1281See \\[tex-file] for an alternative."
1282 (interactive)
1283 (tex-region (point-min) (point-max)))
1284
1285(defun tex-file ()
1286 "Prompt to save all buffers and run TeX (or LaTeX) on current buffer's file.
1287This function is more useful than \\[tex-buffer] when you need the
1288`.aux' file of LaTeX to have the correct name."
1289 (interactive)
1290 (let ((source-file
1291 (or tex-main-file
1292 (if (buffer-file-name)
1293 (file-name-nondirectory (buffer-file-name))
1294 (error "Buffer does not seem to be associated with any file"))))
1295 (file-dir (file-name-directory (buffer-file-name))))
1296 (if tex-offer-save
1297 (save-some-buffers))
1298 (if (tex-shell-running)
1299 (tex-kill-job)
1300 (tex-start-shell))
1301 (tex-send-command tex-shell-cd-command file-dir)
1302 (tex-start-tex tex-command source-file)
1303 (tex-display-shell)
1304 (setq tex-last-buffer-texed (current-buffer))
1305 (setq tex-print-file source-file)))
1306
1307(defun tex-generate-zap-file-name ()
1308 "Generate a unique name suitable for use as a file name."
1309 ;; Include the shell process number and host name
1310 ;; in case there are multiple shells (for same or different user).
1311 (format "#tz%d%s"
1312 (process-id (get-buffer-process "*tex-shell*"))
1313 (tex-strip-dots (system-name))))
1314
1315(defun tex-strip-dots (s)
1316 (setq s (copy-sequence s))
1317 (while (string-match "\\." s)
1318 (aset s (match-beginning 0) ?-))
1319 s)
1320
1321;; This will perhaps be useful for modifying TEXINPUTS.
1322;; Expand each file name, separated by colons, in the string S.
1323(defun tex-expand-files (s)
1324 (let (elts (start 0))
1325 (while (string-match ":" s start)
1326 (setq elts (cons (substring s start (match-beginning 0)) elts))
1327 (setq start (match-end 0)))
1328 (or (= start 0)
1329 (setq elts (cons (substring s start) elts)))
1330 (mapconcat 'expand-file-name (nreverse elts) ":")))
1331
1332(defun tex-shell-running ()
1333 (and (get-process "tex-shell")
1334 (eq (process-status (get-process "tex-shell")) 'run)))
1335
1336(defun tex-kill-job ()
1337 "Kill the currently running TeX job."
1338 (interactive)
1339 (quit-process (get-process "tex-shell") t))
1340
1341(defun tex-recenter-output-buffer (linenum)
1342 "Redisplay buffer of TeX job output so that most recent output can be seen.
1343The last line of the buffer is displayed on
1344line LINE of the window, or centered if LINE is nil."
1345 (interactive "P")
1346 (let ((tex-shell (get-buffer "*tex-shell*"))
1347 (old-buffer (current-buffer))
1348 (window))
1349 (if (null tex-shell)
1350 (message "No TeX output buffer")
1351 (setq window (display-buffer tex-shell))
1352 (save-selected-window
1353 (select-window window)
1354 (bury-buffer tex-shell)
1355 (goto-char (point-max))
1356 (recenter (if linenum
1357 (prefix-numeric-value linenum)
1358 (/ (window-height) 2)))))))
1359
1360(defun tex-print (&optional alt)
1361 "Print the .dvi file made by \\[tex-region], \\[tex-buffer] or \\[tex-file].
1362Runs the shell command defined by `tex-dvi-print-command'. If prefix argument
1363is provided, use the alternative command, `tex-alt-dvi-print-command'."
1364 (interactive "P")
1365 (let ((print-file-name-dvi (tex-append tex-print-file ".dvi"))
1366 test-name)
1367 (if (and (not (equal (current-buffer) tex-last-buffer-texed))
1368 (buffer-file-name)
1369 ;; Check that this buffer's printed file is up to date.
1370 (file-newer-than-file-p
1371 (setq test-name (tex-append (buffer-file-name) ".dvi"))
1372 (buffer-file-name)))
1373 (setq print-file-name-dvi test-name))
1374 (if (not (file-exists-p print-file-name-dvi))
1375 (error "No appropriate `.dvi' file could be found")
1376 (if (tex-shell-running)
1377 (tex-kill-job)
1378 (tex-start-shell))
1379 (tex-send-command
1380 (if alt tex-alt-dvi-print-command tex-dvi-print-command)
1381 print-file-name-dvi t))))
1382
1383(defun tex-alt-print ()
1384 "Print the .dvi file made by \\[tex-region], \\[tex-buffer] or \\[tex-file].
1385Runs the shell command defined by `tex-alt-dvi-print-command'."
1386 (interactive)
1387 (tex-print t))
1388
1389(defun tex-view ()
1390 "Preview the last `.dvi' file made by running TeX under Emacs.
1391This means, made using \\[tex-region], \\[tex-buffer] or \\[tex-file].
1392The variable `tex-dvi-view-command' specifies the shell command for preview.
1393You must set that variable yourself before using this command,
1394because there is no standard value that would generally work."
1395 (interactive)
1396 (or tex-dvi-view-command
1397 (error "You must set `tex-dvi-view-command'"))
1398 (let ((tex-dvi-print-command tex-dvi-view-command))
1399 (tex-print)))
1400
1401(defun tex-append (file-name suffix)
1402 "Append to FILENAME the suffix SUFFIX, using same algorithm TeX uses.
1403Pascal-based TeX scans for the first period, C TeX uses the last.
1404No period is retained immediately before SUFFIX,
1405so normally SUFFIX starts with one."
1406 (if (stringp file-name)
1407 (let ((file (file-name-nondirectory file-name))
1408 trial-name)
1409 ;; Try splitting on last period.
1410 ;; The first-period split can get fooled when two files
1411 ;; named a.tex and a.b.tex are both tex'd;
1412 ;; the last-period split must be right if it matches at all.
1413 (setq trial-name
1414 (concat (file-name-directory file-name)
1415 (substring file 0
1416 (string-match "\\.[^.]*$" file))
1417 suffix))
1418 (if (or (file-exists-p trial-name)
1419 (file-exists-p (concat trial-name ".aux"))) ;for BibTeX files
1420 trial-name
1421 ;; Not found, so split on first period.
1422 (concat (file-name-directory file-name)
1423 (substring file 0
1424 (string-match "\\." file))
1425 suffix)))
1426 " "))
1427
1428(defun tex-show-print-queue ()
1429 "Show the print queue that \\[tex-print] put your job on.
1430Runs the shell command defined by `tex-show-queue-command'."
1431 (interactive)
1432 (if (tex-shell-running)
1433 (tex-kill-job)
1434 (tex-start-shell))
1435 (tex-send-command tex-show-queue-command)
1436 (tex-display-shell))
1437
1438(defun tex-bibtex-file ()
1439 "Run BibTeX on the current buffer's file."
1440 (interactive)
1441 (if (tex-shell-running)
1442 (tex-kill-job)
1443 (tex-start-shell))
1444 (let ((tex-out-file
1445 (tex-append (file-name-nondirectory (buffer-file-name)) ""))
1446 (file-dir (file-name-directory (buffer-file-name))))
1447 (tex-send-command tex-shell-cd-command file-dir)
1448 (tex-send-command tex-bibtex-command tex-out-file))
1449 (tex-display-shell))
1450
1451(run-hooks 'tex-mode-load-hook)
1452
1453(provide 'tex-mode)
1454
1455;;; tex-mode.el ends here