*** empty log message ***
[bpt/emacs.git] / lisp / textmodes / tex-mode.el
CommitLineData
869bff31 1;; TeX, LaTeX, and SliTeX mode commands.
2;; Copyright (C) 1985, 1986, 1989 Free Software Foundation, Inc.
3;; Rewritten following contributions by William F. Schelter
4;; and Dick King (king@kestrel).
5;; Supported since 1986 by Stephen Gildea <gildea@erl.mit.edu>
6;; and Michael Prange <prange@erl.mit.edu>.
7;; Various improvements and corrections in Fall, 1989 by
8;; Edward M. Reingold <reingold@cs.uiuc.edu>.
9
10;; This file is part of GNU Emacs.
11
12;; GNU Emacs is free software; you can redistribute it and/or modify
13;; it under the terms of the GNU General Public License as published by
14;; the Free Software Foundation; either version 1, or (at your option)
15;; any later version.
16
17;; GNU Emacs is distributed in the hope that it will be useful,
18;; but WITHOUT ANY WARRANTY; without even the implied warranty of
19;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20;; GNU General Public License for more details.
21
22;; You should have received a copy of the GNU General Public License
23;; along with GNU Emacs; see the file COPYING. If not, write to
24;; the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
25
26;; Still to do:
27;; Make TAB indent correctly for TeX code. Then we can make Linefeed
28;; do something more useful.
29;;
30;; Have spell understand TeX instead of assuming the entire world
31;; uses nroff.
32;;
33;; The code for finding matching $ needs to be fixed.
34
35(require 'oshell)
36(provide 'tex-mode)
37
38(defvar tex-directory "./"
39 "*Directory in which to run TeX subjob. Temporary files are created here.")
40
41(defvar tex-run-command "tex"
42 "*Command used to run TeX subjob.
43The name of the file will be appended to this string, separated by a space.")
44
45(defvar latex-run-command "latex"
46 "*Command used to run LaTeX subjob.
47The name of the file will be appended to this string, separated by a space.")
48
49(defvar slitex-run-command "slitex"
50 "*Command used to run SliTeX subjob.
51The name of the file will be appended to this string, separated by a space.")
52
53(defvar tex-bibtex-command "bibtex"
54 "*Command string used by `tex-bibtex-file' to gather bibliographic data.
55The name of the file will be appended to this string, separated by a space.")
56
57(defvar tex-dvi-print-command "lpr -d"
58 "*Command string used by \\[tex-print] to print a .dvi file.")
59
60(defvar tex-dvi-view-command nil
61 "*Command string used by \\[tex-view] to display a .dvi file.")
62
63(defvar tex-show-queue-command "lpq"
64 "*Command string used by \\[tex-show-print-queue] to show the print queue.
65Should show the queue that \\[tex-print] puts jobs on.")
66
67(defvar tex-default-mode 'plain-tex-mode
68 "*Mode to enter for a new file that might be either TeX or LaTeX.
69This variable is used when it can't be determined whether the file
70is plain TeX or LaTeX or what because the file contains no commands.
71Normally set to either 'plain-tex-mode or 'latex-mode.")
72
73(defvar tex-open-quote "``"
74 "*String inserted by typing \\[tex-insert-quote] to open a quotation.")
75
76(defvar tex-close-quote "''"
77 "*String inserted by typing \\[tex-insert-quote] to close a quotation.")
78
79(defvar tex-command nil
80 "Command to run TeX.
81The name of the file will be appended to this string, separated by a space.")
82
83(defvar tex-trailer nil
84 "String appended after the end of a region sent to TeX by \\[tex-region].")
85
86(defvar tex-start-of-header nil
87 "String used by \\[tex-region] to delimit the start of the file's header.")
88
89(defvar tex-end-of-header nil
90 "String used by \\[tex-region] to delimit the end of the file's header.")
91
92(defvar tex-shell-cd-command "cd"
93 "Command to give to shell running TeX to change directory.
94The value of tex-directory will be appended to this, separated by a space.")
95
96(defvar tex-zap-file nil
97 "Temporary file name used for text being sent as input to TeX.
98Should be a simple file name with no extension or directory specification.")
99
100(defvar tex-last-buffer-texed nil
101 "Buffer which was last TeXed.")
102
103(defvar tex-print-file nil
104 "File name that \\[tex-print] prints.
105Set by \\[tex-region], \\[tex-buffer], and \\[tex-file].")
106
107(defvar tex-mode-syntax-table nil
108 "Syntax table used while in TeX mode.")
109
110(defun tex-define-common-keys (keymap)
111 "Define the keys that we want defined both in TeX mode and in the tex-shell."
112 (define-key keymap "\C-c\C-k" 'tex-kill-job)
113 (define-key keymap "\C-c\C-l" 'tex-recenter-output-buffer)
114 (define-key keymap "\C-c\C-q" 'tex-show-print-queue)
115 (define-key keymap "\C-c\C-p" 'tex-print)
116 (define-key keymap "\C-c\C-v" 'tex-view)
117 )
118
119(defvar tex-mode-map nil "Keymap for TeX mode.")
120
121(if tex-mode-map
122 nil
123 (setq tex-mode-map (make-sparse-keymap))
124 (tex-define-common-keys tex-mode-map)
125 (define-key tex-mode-map "\"" 'tex-insert-quote)
126 (define-key tex-mode-map "\n" 'tex-terminate-paragraph)
127 (define-key tex-mode-map "\C-c}" 'up-list)
128 (define-key tex-mode-map "\C-c{" 'tex-insert-braces)
129 (define-key tex-mode-map "\C-c\C-r" 'tex-region)
130 (define-key tex-mode-map "\C-c\C-b" 'tex-buffer)
131 (define-key tex-mode-map "\C-c\C-f" 'tex-file)
132 (define-key tex-mode-map "\C-c\C-i" 'tex-bibtex-file)
133 (define-key tex-mode-map "\C-c\C-o" 'tex-latex-block)
134 (define-key tex-mode-map "\C-c\C-e" 'tex-close-latex-block))
135
136(defvar tex-shell-map nil
137 "Keymap for the tex-shell. A shell-mode-map with a few additions.")
138
139;(fset 'TeX-mode 'tex-mode) ;in loaddefs.
140
141;;; This would be a lot simpler if we just used a regexp search,
142;;; but then it would be too slow.
7229064d 143;;;###autoload
869bff31 144(defun tex-mode ()
145 "Major mode for editing files of input for TeX, LaTeX, or SliTeX.
146Tries to determine (by looking at the beginning of the file) whether
147this file is for plain TeX, LaTeX, or SliTeX and calls plain-tex-mode,
148latex-mode, or slitex-mode, respectively. If it cannot be determined,
149such as if there are no commands in the file, the value of tex-default-mode
150is used."
151 (interactive)
152 (let (mode slash comment)
153 (save-excursion
154 (goto-char (point-min))
155 (while (and (setq slash (search-forward "\\" nil t))
156 (setq comment (let ((search-end (point)))
157 (save-excursion
158 (beginning-of-line)
159 (search-forward "%" search-end t))))))
160 (if (and slash (not comment))
161 (setq mode (if (looking-at "documentstyle")
162 (if (looking-at "documentstyle{slides}")
163 'slitex-mode
164 'latex-mode)
165 'plain-tex-mode))))
166 (if mode (funcall mode)
167 (funcall tex-default-mode))))
7229064d
RM
168;;;###autoload (fset 'TeX-mode 'tex-mode)
169;;;###autoload (fset 'LaTeX-mode 'latex-mode)
869bff31 170
7229064d 171;;;###autoload
869bff31 172(defun plain-tex-mode ()
173 "Major mode for editing files of input for plain TeX.
174Makes $ and } display the characters they match.
175Makes \" insert `` when it seems to be the beginning of a quotation,
176and '' when it appears to be the end; it inserts \" only after a \\.
177
178Use \\[tex-region] to run TeX on the current region, plus a \"header\"
179copied from the top of the file (containing macro definitions, etc.),
180running TeX under a special subshell. \\[tex-buffer] does the whole buffer.
181\\[tex-file] saves the buffer and then processes the file.
182\\[tex-print] prints the .dvi file made by any of these.
183\\[tex-view] previews the .dvi file made by any of these.
184\\[tex-bibtex-file] runs bibtex on the file of the current buffer.
185
186Use \\[validate-tex-buffer] to check buffer for paragraphs containing
187mismatched $'s or braces.
188
189Special commands:
190\\{tex-mode-map}
191
192Mode variables:
193tex-run-command
194 Command string used by \\[tex-region] or \\[tex-buffer].
195tex-directory
196 Directory in which to create temporary files for TeX jobs
197 run by \\[tex-region] or \\[tex-buffer].
198tex-dvi-print-command
199 Command string used by \\[tex-print] to print a .dvi file.
200tex-dvi-view-command
201 Command string used by \\[tex-view] to preview a .dvi file.
202tex-show-queue-command
203 Command string used by \\[tex-show-print-queue] to show the print
204 queue that \\[tex-print] put your job on.
205
206Entering Plain-tex mode calls the value of text-mode-hook, then the value of
207tex-mode-hook, and then the value of plain-tex-mode-hook. When the special
208subshell is initiated, the value of tex-shell-hook is called."
209 (interactive)
210 (tex-common-initialization)
211 (setq mode-name "TeX")
212 (setq major-mode 'plain-tex-mode)
213 (setq tex-command tex-run-command)
214 (setq tex-start-of-header "%**start of header")
215 (setq tex-end-of-header "%**end of header")
216 (setq tex-trailer "\\bye\n")
217 (run-hooks 'text-mode-hook 'tex-mode-hook 'plain-tex-mode-hook))
7229064d 218;;;###autoload (fset 'plain-TeX-mode 'plain-tex-mode)
869bff31 219
7229064d 220;;;###autoload
869bff31 221(defun latex-mode ()
222 "Major mode for editing files of input for LaTeX.
223Makes $ and } display the characters they match.
224Makes \" insert `` when it seems to be the beginning of a quotation,
225and '' when it appears to be the end; it inserts \" only after a \\.
226
227Use \\[tex-region] to run LaTeX on the current region, plus the preamble
228copied from the top of the file (containing \\documentstyle, etc.),
229running LaTeX under a special subshell. \\[tex-buffer] does the whole buffer.
230\\[tex-file] saves the buffer and then processes the file.
231\\[tex-print] prints the .dvi file made by any of these.
232\\[tex-view] previews the .dvi file made by any of these.
233\\[tex-bibtex-file] runs bibtex on the file of the current buffer.
234
235Use \\[validate-tex-buffer] to check buffer for paragraphs containing
236mismatched $'s or braces.
237
238Special commands:
239\\{tex-mode-map}
240
241Mode variables:
242latex-run-command
243 Command string used by \\[tex-region] or \\[tex-buffer].
244tex-directory
245 Directory in which to create temporary files for LaTeX jobs
246 run by \\[tex-region] or \\[tex-buffer].
247tex-dvi-print-command
248 Command string used by \\[tex-print] to print a .dvi file.
249tex-dvi-view-command
250 Command string used by \\[tex-view] to preview a .dvi file.
251tex-show-queue-command
252 Command string used by \\[tex-show-print-queue] to show the print
253 queue that \\[tex-print] put your job on.
254
255Entering Latex mode calls the value of text-mode-hook, then the value of
256tex-mode-hook, and then the value of latex-mode-hook. When the special
257subshell is initiated, the value of tex-shell-hook is called."
258 (interactive)
259 (tex-common-initialization)
260 (setq mode-name "LaTeX")
261 (setq major-mode 'latex-mode)
262 (setq tex-command latex-run-command)
263 (setq tex-start-of-header "\\documentstyle")
264 (setq tex-end-of-header "\\begin{document}")
265 (setq tex-trailer "\\end{document}\n")
266 (run-hooks 'text-mode-hook 'tex-mode-hook 'latex-mode-hook))
267
268(defun slitex-mode ()
269 "Major mode for editing files of input for SliTeX.
270Makes $ and } display the characters they match.
271Makes \" insert `` when it seems to be the beginning of a quotation,
272and '' when it appears to be the end; it inserts \" only after a \\.
273
274Use \\[tex-region] to run SliTeX on the current region, plus the preamble
275copied from the top of the file (containing \\documentstyle, etc.),
276running SliTeX under a special subshell. \\[tex-buffer] does the whole buffer.
277\\[tex-file] saves the buffer and then processes the file.
278\\[tex-print] prints the .dvi file made by any of these.
279\\[tex-view] previews the .dvi file made by any of these.
280\\[tex-bibtex-file] runs bibtex on the file of the current buffer.
281
282Use \\[validate-tex-buffer] to check buffer for paragraphs containing
283mismatched $'s or braces.
284
285Special commands:
286\\{tex-mode-map}
287
288Mode variables:
289slitex-run-command
290 Command string used by \\[tex-region] or \\[tex-buffer].
291tex-directory
292 Directory in which to create temporary files for SliTeX jobs
293 run by \\[tex-region] or \\[tex-buffer].
294tex-dvi-print-command
295 Command string used by \\[tex-print] to print a .dvi file.
296tex-dvi-view-command
297 Command string used by \\[tex-view] to preview a .dvi file.
298tex-show-queue-command
299 Command string used by \\[tex-show-print-queue] to show the print
300 queue that \\[tex-print] put your job on.
301
302Entering SliTex mode calls the value of text-mode-hook, then the value of
303tex-mode-hook, then the value of latex-mode-hook, and then the value of
304slitex-mode-hook. When the special subshell is initiated, the value of
305tex-shell-hook is called."
306 (interactive)
307 (tex-common-initialization)
308 (setq mode-name "SliTeX")
309 (setq major-mode 'slitex-mode)
310 (setq tex-command slitex-run-command)
311 (setq tex-start-of-header "\\documentstyle{slides}")
312 (setq tex-end-of-header "\\begin{document}")
313 (setq tex-trailer "\\end{document}\n")
314 (run-hooks
315 'text-mode-hook 'tex-mode-hook 'latex-mode-hook 'slitex-mode-hook))
316
317(defun tex-common-initialization ()
318 (kill-all-local-variables)
319 (use-local-map tex-mode-map)
320 (setq local-abbrev-table text-mode-abbrev-table)
321 (if (null tex-mode-syntax-table)
322 (let ((char 0))
323 (setq tex-mode-syntax-table (make-syntax-table))
324 (set-syntax-table tex-mode-syntax-table)
325 (while (< char ? )
326 (modify-syntax-entry char ".")
327 (setq char (1+ char)))
328 (modify-syntax-entry ?\C-@ "w")
329 (modify-syntax-entry ?\t " ")
330 (modify-syntax-entry ?\n ">")
331 (modify-syntax-entry ?\f ">")
332 (modify-syntax-entry ?$ "$$")
333 (modify-syntax-entry ?% "<")
334 (modify-syntax-entry ?\\ "/")
335 (modify-syntax-entry ?\" ".")
336 (modify-syntax-entry ?& ".")
337 (modify-syntax-entry ?_ ".")
338 (modify-syntax-entry ?@ "_")
339 (modify-syntax-entry ?~ " ")
340 (modify-syntax-entry ?' "w"))
341 (set-syntax-table tex-mode-syntax-table))
342 (make-local-variable 'paragraph-start)
343 (setq paragraph-start "^[ \t]*$\\|^[\f\\\\%]")
344 (make-local-variable 'paragraph-separate)
345 (setq paragraph-separate paragraph-start)
346 (make-local-variable 'comment-start)
347 (setq comment-start "%")
348 (make-local-variable 'comment-start-skip)
349 (setq comment-start-skip "\\(\\(^\\|[^\\]\\)\\(\\\\\\\\\\)*\\)\\(%+ *\\)")
350 (make-local-variable 'comment-indent-hook)
351 (setq comment-indent-hook 'tex-comment-indent)
352 (make-local-variable 'compare-windows-whitespace)
353 (setq compare-windows-whitespace 'tex-categorize-whitespace)
354 (make-local-variable 'tex-command)
355 (make-local-variable 'tex-start-of-header)
356 (make-local-variable 'tex-end-of-header)
357 (make-local-variable 'tex-trailer))
358
359(defun tex-comment-indent ()
360 (if (looking-at "%%%")
361 (current-column)
362 (skip-chars-backward " \t")
363 (max (if (bolp) 0 (1+ (current-column)))
364 comment-column)))
365
366(defun tex-categorize-whitespace (backward-limit)
367 ;; compare-windows-whitespace is set to this.
368 ;; This is basically a finite-state machine.
369 ;; Returns a symbol telling how TeX would treat
370 ;; the whitespace we are looking at: null, space, or par.
371 (let ((category 'null)
372 (not-finished t))
373 (skip-chars-backward " \t\n\f" backward-limit)
374 (while not-finished
375 (cond ((looking-at "[ \t]+")
376 (goto-char (match-end 0))
377 (if (eql category 'null)
378 (setq category 'space)))
379 ((looking-at "\n")
380 (cond ((eql category 'newline)
381 (setq category 'par)
382 (setq not-finished nil))
383 (t
384 (setq category 'newline) ;a strictly internal state
385 (goto-char (match-end 0)))))
386 ((looking-at "\f+")
387 (setq category 'par)
388 (setq not-finished nil))
389 (t
390 (setq not-finished nil))))
391 (skip-chars-forward " \t\n\f")
392 (if (eql category 'newline)
393 'space ;TeX doesn't distinguish
394 category)))
395
396(defun tex-insert-quote (arg)
397 "Insert the appropriate quote marks for TeX.
398Inserts the value of tex-open-quote (normally ``) or tex-close-quote
399(normally '') depending on the context. With prefix argument, always
400inserts \" characters."
401 (interactive "P")
402 (if arg
403 (self-insert-command (prefix-numeric-value arg))
404 (insert
405 (cond ((or (bobp)
406 (save-excursion
407 (forward-char -1)
408 (looking-at "\\s(\\|\\s \\|\\s>")))
409 tex-open-quote)
410 ((= (preceding-char) ?\\)
411 ?\")
412 (t
413 tex-close-quote)))))
414
415(defun validate-tex-buffer ()
416 "Check current buffer for paragraphs containing mismatched $'s.
417As each such paragraph is found, a mark is pushed at its beginning,
418and the location is displayed for a few seconds."
419 (interactive)
420 (let ((opoint (point)))
421 (goto-char (point-max))
422 ;; Does not use save-excursion
423 ;; because we do not want to save the mark.
424 (unwind-protect
425 (while (and (not (input-pending-p)) (not (bobp)))
426 (let ((end (point)))
427 (search-backward "\n\n" nil 'move)
428 (or (tex-validate-region (point) end)
429 (progn
430 (push-mark (point))
431 (message "Mismatch found in pararaph starting here")
432 (sit-for 4)))))
433 (goto-char opoint))))
434
435(defun tex-validate-region (start end)
436 "Check for mismatched braces or $'s in region.
437Returns t if no mismatches. Returns nil and moves point to suspect
438area if a mismatch is found."
439 (interactive "r")
440 (let ((failure-point nil) (max-possible-sexps (- end start)))
441 (save-excursion
442 (condition-case ()
443 (save-restriction
444 (narrow-to-region start end)
445 (goto-char start)
446 (while (< 0 (setq max-possible-sexps (1- max-possible-sexps)))
447 (forward-sexp 1)))
448 (error
449 (setq failure-point (point)))))
450 (if failure-point
451 (progn
452 (goto-char failure-point)
453 nil)
454 t)))
455
456(defun tex-terminate-paragraph (inhibit-validation)
457 "Insert two newlines, breaking a paragraph for TeX.
458Check for mismatched braces/$'s in paragraph being terminated.
459A prefix arg inhibits the checking."
460 (interactive "P")
461 (or inhibit-validation
462 (save-excursion
463 (tex-validate-region
464 (save-excursion
465 (search-backward "\n\n" nil 'move)
466 (point))
467 (point)))
468 (message "Paragraph being closed appears to contain a mismatch"))
469 (insert "\n\n"))
470
471(defun tex-insert-braces ()
472 "Make a pair of braces and be poised to type inside of them."
473 (interactive)
474 (insert ?\{)
475 (save-excursion
476 (insert ?})))
477
478;;; Like tex-insert-braces, but for LaTeX.
479(defun tex-latex-block (name)
480 "Creates a matching pair of lines \\begin{NAME} and \\end{NAME} at point.
481Puts point on a blank line between them."
482 (interactive "*sLaTeX block name: ")
483 (let ((col (current-column)))
484 (insert (format "\\begin{%s}\n" name))
485 (indent-to col)
486 (save-excursion
487 (insert ?\n)
488 (indent-to col)
489 (insert-string (format "\\end{%s}" name))
490 (if (eobp) (insert ?\n)))))
491
492(defun tex-last-unended-begin ()
493 "Leave point at the beginning of the last \\begin{...} that is unended."
494 (while (and (re-search-backward "\\(\\\\begin\\s *{\\)\\|\\(\\\\end\\s *{\\)")
495 (looking-at "\\\\end{"))
496 (tex-last-unended-begin)))
497
498(defun tex-close-latex-block ()
499 "Creates an \\end{...} to match the last unclosed \\begin{...}."
500 (interactive "*")
501 (let ((new-line-needed (bolp))
502 text indentation)
503 (save-excursion
504 (condition-case ERR
505 (tex-last-unended-begin)
506 (error (error "Couldn't find unended \\begin")))
507 (setq indentation (current-column))
508 (re-search-forward "\\\\begin\\(\\s *{[^}\n]*}\\)")
509 (setq text (buffer-substring (match-beginning 1) (match-end 1))))
510 (indent-to indentation)
511 (insert "\\end" text)
512 (if new-line-needed (insert ?\n))))
513\f
514;;; Invoking TeX in an inferior shell.
515
516;;; Why use a shell instead of running TeX directly? Because if TeX
517;;; gets stuck, the user can switch to the shell window and type at it.
518
519;;; The utility functions:
520
521(defun tex-start-shell ()
522 (save-excursion
523 (set-buffer (make-shell "tex-shell" nil nil "-v"))
524 (setq tex-shell-map (copy-keymap shell-mode-map))
525 (tex-define-common-keys tex-shell-map)
526 (use-local-map tex-shell-map)
527 (run-hooks 'tex-shell-hook)
528 (if (zerop (buffer-size))
529 (sleep-for 1))))
530
531(defun set-buffer-directory (buffer directory)
532 "Set BUFFER's default directory to be DIRECTORY."
533 (setq directory (file-name-as-directory (expand-file-name directory)))
534 (if (not (file-directory-p directory))
535 (error "%s is not a directory" directory)
536 (save-excursion
537 (set-buffer buffer)
538 (setq default-directory directory))))
539
540;;; The commands:
541
542;;; It's a kludge that we have to create a special buffer just
543;;; to write out the tex-trailer. It would nice if there were a
544;;; function like write-region that would write literal strings.
545
546(defun tex-region (beg end)
547 "Run TeX on the current region, via a temporary file.
548The file's name comes from the variable `tex-zap-file' and the
549variable `tex-directory' says where to put it.
550
551If the buffer has a header, the header is given to TeX before the
552region itself. The buffer's header is all lines between the strings
553defined by `tex-start-of-header' and `tex-end-of-header' inclusive.
554The header must start in the first 100 lines of the buffer.
555
556The value of `tex-trailer' is given to TeX as input after the region.
557
558The value of `tex-command' specifies the command to use to run TeX."
559 (interactive "r")
560 (if (tex-shell-running)
561 (tex-kill-job)
562 (tex-start-shell))
563 (or tex-zap-file
564 (setq tex-zap-file (tex-generate-zap-file-name)))
565 (let ((tex-out-file (concat tex-zap-file ".tex"))
566 (temp-buffer (get-buffer-create " TeX-Output-Buffer"))
567 (file-dir (if (buffer-file-name)
568 (file-name-directory (buffer-file-name))
569 default-directory))
570 (zap-directory
571 (file-name-as-directory (expand-file-name tex-directory))))
572 ;; Delete any junk files or memory files from this temp file,
573 ;; since the contents were probably different last time anyway.
574 ;; This may also delete the old temp file if any.
575 (let ((list (file-name-all-completions (tex-append tex-out-file ".")
576 zap-directory)))
577 (while list
578 (delete-file (expand-file-name (car list) zap-directory))
579 (setq list (cdr list))))
580 ;; Write the new temp file.
581 (save-excursion
582 (save-restriction
583 (widen)
584 (goto-char (point-min))
585 (forward-line 100)
586 (let ((search-end (point))
587 (hbeg (point-min)) (hend (point-min))
588 (default-directory zap-directory))
589 (goto-char (point-min))
590 ;; Initialize the temp file with either the header or nothing
591 (if (search-forward tex-start-of-header search-end t)
592 (progn
593 (beginning-of-line)
594 (setq hbeg (point)) ;mark beginning of header
595 (if (search-forward tex-end-of-header nil t)
596 (progn (forward-line 1)
597 (setq hend (point))) ;mark end of header
598 (setq hbeg (point-min))))) ;no header
599 (write-region (min hbeg beg) hend tex-out-file nil nil)
600 (write-region (max beg hend) end tex-out-file t nil))
601 (let ((local-tex-trailer tex-trailer))
602 (set-buffer temp-buffer)
603 (erase-buffer)
604 ;; make sure trailer isn't hidden by a comment
605 (insert-string "\n")
606 (if local-tex-trailer (insert-string local-tex-trailer))
607 (set-buffer-directory temp-buffer zap-directory)
608 (write-region (point-min) (point-max) tex-out-file t nil))))
609 ;; Record in the shell buffer the file name to delete afterward.
610 (save-excursion
611 (set-buffer (get-buffer "*tex-shell*"))
612 (make-local-variable 'tex-last-temp-file)
613 (setq tex-last-temp-file tex-out-file))
614 (set-process-filter "tex-shell" 'tex-filter)
615 (set-buffer-directory "*tex-shell*" zap-directory)
616 ;; Run TeX in source file's dir, in case TEXINPUTS uses current dir.
617 (send-string "tex-shell" (concat tex-shell-cd-command " " file-dir "\n"))
618 (send-string "tex-shell" (concat tex-command " \""
619 zap-directory
620 tex-out-file "\"\n")))
621 (setq tex-last-buffer-texed (current-buffer))
622 (setq tex-print-file
623 (concat (file-name-as-directory (expand-file-name tex-directory))
624 tex-zap-file))
625 (tex-recenter-output-buffer 0))
626
627;; This filter is used in the TeX shell buffer
628;; while TeX is running for a tex-region command.
629(defun tex-filter (process string)
630 (let ((old (current-buffer)))
631 (set-buffer (process-buffer proc))
632 (unwind-protect
633 (progn (if (= (process-mark proc) (point-max))
634 (insert string)
635 (save-excursion
636 (goto-char (process-mark proc))
637 (insert string)))
638 (set-marker (process-mark proc) (point))
639 ;; Delete the temporary file
640 ;; when TeX finishes.
641 ;; And stop using this filter.
642 (save-excursion
643 (forward-line -1)
644 (if (looking-at "^Output written on ")
645 (progn
646 (set-process-filter process nil)
647 ;; Delete the temp file just processed
648 ;; and any related junk files made by TeX.
649 (let ((list (file-name-all-completions
650 (tex-append tex-last-temp-file ".")
651 zap-directory)))
652 (while list
653 (delete-file (expand-file-name
654 (car list) zap-directory))
655 (setq list (cdr list))))))))
656 (or (eq old (current-buffer))
657 (set-buffer old)))))
658
659(defun tex-buffer ()
660 "Run TeX on current buffer. See \\[tex-region] for more information.
661Does not save the buffer, so it's useful for trying
662experimental versions. See \\[tex-file] for an alternative."
663 (interactive)
664 (tex-region (point-min) (point-max)))
665
666(defun tex-file ()
667 "Prompt to save all buffers and run TeX (or LaTeX) on current buffer's file.
668This function is more useful than \\[tex-buffer] when you need the
669`.aux' file of LaTeX to have the correct name."
670 (interactive)
671 (let ((tex-out-file
672 (if (buffer-file-name)
673 (file-name-nondirectory (buffer-file-name))
674 (error "Buffer does not seem to be associated with any file")))
675 (file-dir (file-name-directory (buffer-file-name))))
676 (save-some-buffers)
677 (if (tex-shell-running)
678 (tex-kill-job)
679 (tex-start-shell))
680 (set-buffer-directory "*tex-shell*" file-dir)
681 (send-string "tex-shell" (concat tex-shell-cd-command " " file-dir "\n"))
682 (send-string "tex-shell"
683 (concat tex-command " \"" tex-out-file "\"\n")))
684 (setq tex-last-buffer-texed (current-buffer))
685 (setq tex-print-file (buffer-file-name))
686 (tex-recenter-output-buffer 0))
687
688(defun tex-generate-zap-file-name ()
689 "Generate a unique name suitable for use as a file name."
690 ;; Include the shell process number and host name
691 ;; in case there are multiple shells (for same or different user).
692 (format "#tz%d%s"
693 (process-id (get-buffer-process "*tex-shell*"))
694 (tex-strip-dots (system-name))))
695
696(defun tex-strip-dots (s)
697 (setq s (copy-sequence s))
698 (while (string-match "\\." s)
699 (aset s (match-beginning 0) ?-))
700 s)
701
702;; This will perhaps be useful for modifying TEXINPUTS.
703;; Expand each file name, separated by colons, in the string S.
704(defun tex-expand-files (s)
705 (let (elts (start 0))
706 (while (string-match ":" s start)
707 (setq elts (cons (substring s start (match-beginning 0)) elts))
708 (setq start (match-end 0)))
709 (or (= start 0)
710 (setq elts (cons (substring s start) elts)))
711 (mapconcat 'expand-file-name (nreverse elts) ":")))
712
713(defun tex-shell-running ()
714 (and (get-process "tex-shell")
715 (eq (process-status (get-process "tex-shell")) 'run)))
716
717(defun tex-kill-job ()
718 "Kill the currently running TeX job."
719 (interactive)
720 (if (get-process "tex-shell")
721 (quit-process "tex-shell" t)))
722
723(defun tex-recenter-output-buffer (linenum)
724 "Redisplay buffer of TeX job output so that most recent output can be seen.
725The last line of the buffer is displayed on
726line LINE of the window, or centered if LINE is nil."
727 (interactive "P")
728 (let ((tex-shell (get-buffer "*tex-shell*"))
729 (old-buffer (current-buffer)))
730 (if (null tex-shell)
731 (message "No TeX output buffer")
732 (pop-to-buffer tex-shell)
733 (bury-buffer tex-shell)
734 (goto-char (point-max))
735 (recenter (if linenum
736 (prefix-numeric-value linenum)
737 (/ (window-height) 2)))
738 (pop-to-buffer old-buffer)
739 )))
740
741(defun tex-print ()
742 "Print the .dvi file made by \\[tex-region], \\[tex-buffer] or \\[tex-file].
743Runs the shell command defined by tex-dvi-print-command."
744 (interactive)
745 (let ((print-file-name-dvi (tex-append tex-print-file ".dvi"))
746 test-name)
747 (if (and (not (equal (current-buffer) tex-last-buffer-texed))
748 (file-newer-than-file-p
749 (setq test-name (tex-append (buffer-file-name) ".dvi"))
750 (tex-append tex-print-file ".dvi")))
751 (setq print-file-name-dvi test-name))
752 (if (file-exists-p print-file-name-dvi)
753 (shell-command
754 (concat tex-dvi-print-command " \"" print-file-name-dvi "&\"\n"))
755 (error "No appropriate `.dvi' file could be found"))))
756
757(defun tex-view ()
758 "Preview the last `.dvi' file made by running TeX under Emacs.
759This means, made using \\[tex-region], \\[tex-buffer] or \\[tex-file].
760The variable `tex-dvi-view-command' specifies the shell command for preview."
761 (interactive)
762 (let ((tex-dvi-print-command tex-dvi-view-command))
763 (tex-print)))
764
765(defun tex-append (file-name suffix)
766 "Append to FILENAME the suffix SUFFIX, using same algorithm TeX uses.
767Scans for the first (not last) period.
768No period is retained immediately before SUFFIX,
769so normally SUFFIX starts with one."
770 (if (stringp file-name)
771 (let ((file (file-name-nondirectory file-name)))
772 (concat (file-name-directory file-name)
773 (substring file 0
774 (string-match "\\." file))
775 suffix))
776 " "))
777
778(defun tex-show-print-queue ()
779 "Show the print queue that \\[tex-print] put your job on.
780Runs the shell command defined by tex-show-queue-command."
781 (interactive)
782 (if (tex-shell-running)
783 (tex-kill-job)
784 (tex-start-shell))
785 (send-string "tex-shell" (concat tex-show-queue-command "\n"))
786 (tex-recenter-output-buffer nil))
787
788(defun tex-bibtex-file ()
789 "Run BibTeX on the current buffer's file."
790 (interactive)
791 (if (tex-shell-running)
792 (tex-kill-job)
793 (tex-start-shell))
794 (let ((tex-out-file
795 (tex-append (file-name-nondirectory (buffer-file-name)) ""))
796 (file-dir (file-name-directory (buffer-file-name))))
797 (set-buffer-directory "*tex-shell*" file-dir)
798 (send-string "tex-shell" (concat tex-shell-cd-command " " file-dir "\n"))
799 (send-string "tex-shell"
800 (concat tex-bibtex-command " \"" tex-out-file "\"\n")))
801 (tex-recenter-output-buffer 0))
802