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