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