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>.
10 ;; This file is part of GNU Emacs.
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)
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.
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.
27 ;; Make TAB indent correctly for TeX code. Then we can make Linefeed
28 ;; do something more useful.
30 ;; Have spell understand TeX instead of assuming the entire world
33 ;; The code for finding matching $ needs to be fixed.
38 (defvar tex-directory "./"
39 "*Directory in which to run TeX subjob. Temporary files are created here.")
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.")
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.")
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.")
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.")
57 (defvar tex-dvi-print-command "lpr -d"
58 "*Command string used by \\[tex-print] to print a .dvi file.")
60 (defvar tex-dvi-view-command nil
61 "*Command string used by \\[tex-view] to display a .dvi file.")
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.")
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.")
73 (defvar tex-open-quote "``"
74 "*String inserted by typing \\[tex-insert-quote] to open a quotation.")
76 (defvar tex-close-quote "''"
77 "*String inserted by typing \\[tex-insert-quote] to close a quotation.")
79 (defvar tex-command nil
81 The name of the file will be appended to this string, separated by a space.")
83 (defvar tex-trailer nil
84 "String appended after the end of a region sent to TeX by \\[tex-region].")
86 (defvar tex-start-of-header nil
87 "String used by \\[tex-region] to delimit the start of the file's header.")
89 (defvar tex-end-of-header nil
90 "String used by \\[tex-region] to delimit the end of the file's header.")
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.")
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.")
100 (defvar tex-last-buffer-texed nil
101 "Buffer which was last TeXed.")
103 (defvar tex-print-file nil
104 "File name that \\[tex-print] prints.
105 Set by \\[tex-region], \\[tex-buffer], and \\[tex-file].")
107 (defvar tex-mode-syntax-table nil
108 "Syntax table used while in TeX mode.")
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)
119 (defvar tex-mode-map nil "Keymap for TeX mode.")
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))
136 (defvar tex-shell-map nil
137 "Keymap for the tex-shell. A shell-mode-map with a few additions.")
139 ;(fset 'TeX-mode 'tex-mode) ;in loaddefs.
141 ;;; This would be a lot simpler if we just used a regexp search,
142 ;;; but then it would be too slow.
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
151 (let (mode slash comment)
153 (goto-char (point-min))
154 (while (and (setq slash (search-forward "\\" nil t))
155 (setq comment (let ((search-end (point)))
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}")
165 (if mode (funcall mode)
166 (funcall tex-default-mode))))
168 (fset 'plain-TeX-mode 'plain-tex-mode)
169 (fset 'LaTeX-mode 'latex-mode)
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 \\.
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.
185 Use \\[validate-tex-buffer] to check buffer for paragraphs containing
186 mismatched $'s or braces.
193 Command string used by \\[tex-region] or \\[tex-buffer].
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.
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.
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."
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))
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 \\.
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.
232 Use \\[validate-tex-buffer] to check buffer for paragraphs containing
233 mismatched $'s or braces.
240 Command string used by \\[tex-region] or \\[tex-buffer].
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.
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.
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."
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))
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 \\.
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.
279 Use \\[validate-tex-buffer] to check buffer for paragraphs containing
280 mismatched $'s or braces.
287 Command string used by \\[tex-region] or \\[tex-buffer].
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.
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.
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."
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")
312 'text-mode-hook 'tex-mode-hook 'latex-mode-hook 'slitex-mode-hook))
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)
320 (setq tex-mode-syntax-table (make-syntax-table))
321 (set-syntax-table tex-mode-syntax-table)
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))
356 (defun tex-comment-indent ()
357 (if (looking-at "%%%")
359 (skip-chars-backward " \t")
360 (max (if (bolp) 0 (1+ (current-column)))
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)
370 (skip-chars-backward " \t\n\f" backward-limit)
372 (cond ((looking-at "[ \t]+")
373 (goto-char (match-end 0))
374 (if (eql category 'null)
375 (setq category 'space)))
377 (cond ((eql category 'newline)
379 (setq not-finished nil))
381 (setq category 'newline) ;a strictly internal state
382 (goto-char (match-end 0)))))
385 (setq not-finished nil))
387 (setq not-finished nil))))
388 (skip-chars-forward " \t\n\f")
389 (if (eql category 'newline)
390 'space ;TeX doesn't distinguish
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."
400 (self-insert-command (prefix-numeric-value arg))
405 (looking-at "\\s(\\|\\s \\|\\s>")))
407 ((= (preceding-char) ?\\)
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."
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.
422 (while (and (not (input-pending-p)) (not (bobp)))
424 (search-backward "\n\n" nil 'move)
425 (or (tex-validate-region (point) end)
428 (message "Mismatch found in pararaph starting here")
430 (goto-char opoint))))
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."
437 (let ((failure-point nil) (max-possible-sexps (- end start)))
441 (narrow-to-region start end)
443 (while (< 0 (setq max-possible-sexps (1- max-possible-sexps)))
446 (setq failure-point (point)))))
449 (goto-char failure-point)
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."
458 (or inhibit-validation
462 (search-backward "\n\n" nil 'move)
465 (message "Paragraph being closed appears to contain a mismatch"))
468 (defun tex-insert-braces ()
469 "Make a pair of braces and be poised to type inside of them."
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))
486 (insert-string (format "\\end{%s}" name))
487 (if (eobp) (insert ?\n)))))
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)))
495 (defun tex-close-latex-block ()
496 "Creates an \\end{...} to match the last unclosed \\begin{...}."
498 (let ((new-line-needed (bolp))
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))))
511 ;;; Invoking TeX in an inferior shell.
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.
516 ;;; The utility functions:
518 (defun tex-start-shell ()
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))
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)
535 (setq default-directory directory))))
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.
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.
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.
553 The value of `tex-trailer' is given to TeX as input after the region.
555 The value of `tex-command' specifies the command to use to run TeX."
557 (if (tex-shell-running)
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))
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 ".")
575 (delete-file (expand-file-name (car list) zap-directory))
576 (setq list (cdr list))))
577 ;; Write the new temp file.
581 (goto-char (point-min))
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)
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)
601 ;; make sure trailer isn't hidden by a comment
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.
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 " \""
617 tex-out-file "\"\n")))
618 (setq tex-last-buffer-texed (current-buffer))
620 (concat (file-name-as-directory (expand-file-name tex-directory))
622 (tex-recenter-output-buffer 0))
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))
630 (progn (if (= (process-mark proc) (point-max))
633 (goto-char (process-mark proc))
635 (set-marker (process-mark proc) (point))
636 ;; Delete the temporary file
637 ;; when TeX finishes.
638 ;; And stop using this filter.
641 (if (looking-at "^Output written on ")
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 ".")
650 (delete-file (expand-file-name
651 (car list) zap-directory))
652 (setq list (cdr list))))))))
653 (or (eq old (current-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."
661 (tex-region (point-min) (point-max)))
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."
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))))
674 (if (tex-shell-running)
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))
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).
690 (process-id (get-buffer-process "*tex-shell*"))
691 (tex-strip-dots (system-name))))
693 (defun tex-strip-dots (s)
694 (setq s (copy-sequence s))
695 (while (string-match "\\." s)
696 (aset s (match-beginning 0) ?-))
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)))
707 (setq elts (cons (substring s start) elts)))
708 (mapconcat 'expand-file-name (nreverse elts) ":")))
710 (defun tex-shell-running ()
711 (and (get-process "tex-shell")
712 (eq (process-status (get-process "tex-shell")) 'run)))
714 (defun tex-kill-job ()
715 "Kill the currently running TeX job."
717 (if (get-process "tex-shell")
718 (quit-process "tex-shell" t)))
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."
725 (let ((tex-shell (get-buffer "*tex-shell*"))
726 (old-buffer (current-buffer)))
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)
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."
742 (let ((print-file-name-dvi (tex-append tex-print-file ".dvi"))
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)
751 (concat tex-dvi-print-command " \"" print-file-name-dvi "&\"\n"))
752 (error "No appropriate `.dvi' file could be found"))))
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."
759 (let ((tex-dvi-print-command tex-dvi-view-command))
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)
771 (string-match "\\." file))
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."
779 (if (tex-shell-running)
782 (send-string "tex-shell" (concat tex-show-queue-command "\n"))
783 (tex-recenter-output-buffer nil))
785 (defun tex-bibtex-file ()
786 "Run BibTeX on the current buffer's file."
788 (if (tex-shell-running)
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))