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