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