]> code.delx.au - gnu-emacs/blobdiff - lisp/textmodes/tex-mode.el
(reporter-dont-compact-list): Doc fix.
[gnu-emacs] / lisp / textmodes / tex-mode.el
index 0c9c28a04114ba732367ebd8106db30b9c484ba2..427583a88f86f6a85f0a29d1e5d9b7dbec643ec7 100644 (file)
@@ -1,12 +1,12 @@
 ;;; tex-mode.el --- TeX, LaTeX, and SliTeX mode commands.
 
-;; Copyright (C) 1985, 1986, 1989, 1992 Free Software Foundation, Inc.
+;; Copyright (C) 1985, 86, 89, 92, 94, 95 Free Software Foundation, Inc.
 
-;; Maintainer: Edward M. Reingold <reingold@cs.uiuc.edu>
+;; Maintainer: FSF
 ;; Keywords: tex
 
 ;; Contributions over the years by William F. Schelter, Dick King,
-;; Stephen Gildea, Michael Prange, and Jacob Gore.
+;; Stephen Gildea, Michael Prange, Jacob Gore, and Edward M. Reingold.
 
 ;; This file is part of GNU Emacs.
 
 
 ;;; Code:
 
-(require 'comint)
+(require 'shell)
 (require 'compile)
 
 ;;;###autoload
 (defvar tex-shell-file-name nil
-  "*If non-nil, is file name to use for the subshell in which TeX is run.")
+  "*If non-nil, the shell file name to run in the subshell used to run TeX.")
 
 ;;;###autoload
 (defvar tex-directory "."
   "*Directory in which temporary files are left.
-You can make this /tmp if your TEXINPUTS has no relative directories in it
+You can make this `/tmp' if your TEXINPUTS has no relative directories in it
 and you don't try to apply \\[tex-region] or \\[tex-buffer] when there are
-\\input commands with relative directories.")
+`\\input' commands with relative directories.")
 
 ;;;###autoload
 (defvar tex-offer-save t
@@ -47,16 +47,14 @@ and you don't try to apply \\[tex-region] or \\[tex-buffer] when there are
 ;;;###autoload
 (defvar tex-run-command "tex"
   "*Command used to run TeX subjob.
-If this string contains an asterisk (*), it will be replaced by the
-filename; if not, the name of the file, preceded by blank, will be added to
-this string.")
+If this string contains an asterisk (`*'), that is replaced by the file name;
+otherwise, the file name, preceded by blank, is added at the end.")
 
 ;;;###autoload
 (defvar latex-run-command "latex"
   "*Command used to run LaTeX subjob.
-If this string contains an asterisk (*), it will be replaced by the
-filename; if not, the name of the file, preceded by blank, will be added to
-this string.")
+If this string contains an asterisk (`*'), that is replaced by the file name;
+otherwise, the file name, preceded by blank, is added at the end.")
 
 (defvar standard-latex-block-names
       '("abstract"         "array"            "center"       "description"
@@ -78,33 +76,29 @@ Combined with `standard-latex-block-names' for minibuffer completion.")
 ;;;###autoload
 (defvar slitex-run-command "slitex"
   "*Command used to run SliTeX subjob.
-If this string contains an asterisk (*), it will be replaced by the
-filename; if not, the name of the file, preceded by blank, will be added to
-this string.")
+If this string contains an asterisk (`*'), that is replaced by the file name;
+otherwise, the file name, preceded by blank, is added at the end.")
 
 ;;;###autoload
 (defvar tex-bibtex-command "bibtex"
   "*Command used by `tex-bibtex-file' to gather bibliographic data.
-If this string contains an asterisk (*), it will be replaced by the
-filename; if not, the name of the file, preceded by blank, will be added to
-this string.")
+If this string contains an asterisk (`*'), that is replaced by the file name;
+otherwise, the file name, preceded by blank, is added at the end.")
 
 ;;;###autoload
 (defvar tex-dvi-print-command "lpr -d"
   "*Command used by \\[tex-print] to print a .dvi file.
-If this string contains an asterisk (*), it will be replaced by the
-filename; if not, the name of the file, preceded by blank, will be added to
-this string.")
+If this string contains an asterisk (`*'), that is replaced by the file name;
+otherwise, the file name, preceded by blank, is added at the end.")
 
 ;;;###autoload
 (defvar tex-alt-dvi-print-command "lpr -d"
   "*Command used by \\[tex-print] with a prefix arg to print a .dvi file.
-If this string contains an asterisk (*), it will be replaced by the
-filename; if not, the name of the file, preceded by blank, will be added to
-this string.
+If this string contains an asterisk (`*'), that is replaced by the file name;
+otherwise, the file name, preceded by blank, is added at the end.
 
-If two printers are not enough of a choice, you can define the value
-of tex-alt-dvi-print-command to be an expression that asks what you want;
+If two printers are not enough of a choice, you can set the variable
+`tex-alt-dvi-print-command' to an expression that asks what you want;
 for example,
 
     (setq tex-alt-dvi-print-command
@@ -115,10 +109,9 @@ use.")
 
 ;;;###autoload
 (defvar tex-dvi-view-command nil
-  "*Command used by \\[tex-view] to display a .dvi file.
-If this string contains an asterisk (*), it will be replaced by the
-filename; if not, the name of the file, preceded by blank, will be added to
-this string.
+  "*Command used by \\[tex-view] to display a `.dvi' file.
+If this string contains an asterisk (`*'), that is replaced by the file name;
+otherwise, the file name, preceded by blank, is added at the end.
 
 This can be set conditionally so that the previewer used is suitable for the
 window system being used.  For example,
@@ -126,7 +119,7 @@ window system being used.  For example,
     (setq tex-dvi-view-command
           (if (eq window-system 'x) \"xdvi\" \"dvi2tty * | cat -s\"))
 
-would tell \\[tex-view] use xdvi under X windows and to use dvi2tty
+would tell \\[tex-view] to use xdvi under X windows and to use dvi2tty
 otherwise.")
 
 ;;;###autoload
@@ -139,7 +132,7 @@ Should show the queue(s) that \\[tex-print] puts jobs on.")
   "*Mode to enter for a new file that might be either TeX or LaTeX.
 This variable is used when it can't be determined whether the file
 is plain TeX or LaTeX or what because the file contains no commands.
-Normally set to either 'plain-tex-mode or 'latex-mode.")
+Normally set to either `plain-tex-mode' or `latex-mode'.")
 
 ;;;###autoload
 (defvar tex-open-quote "``"
@@ -152,7 +145,7 @@ Normally set to either 'plain-tex-mode or 'latex-mode.")
 (defvar tex-last-temp-file nil
   "Latest temporary file generated by \\[tex-region] and \\[tex-buffer].
 Deleted when the \\[tex-region] or \\[tex-buffer] is next run, or when the
-tex-shell goes away.")
+tex shell terminates.")
 
 (defvar tex-command nil
   "Command to run TeX.
@@ -162,14 +155,14 @@ The name of the file, preceded by a blank, will be added to this string.")
   "String appended after the end of a region sent to TeX by \\[tex-region].")
 
 (defvar tex-start-of-header nil
-  "String used by \\[tex-region] to delimit the start of the file's header.")
+  "Regular expression used by \\[tex-region] to find start of file's header.")
 
 (defvar tex-end-of-header nil
-  "String used by \\[tex-region] to delimit the end of the file's header.")
+  "Regular expression used by \\[tex-region] to find end of file's header.")
 
 (defvar tex-shell-cd-command "cd"
   "Command to give to shell running TeX to change directory.
-The value of tex-directory will be appended to this, separated by a space.")
+The value of `tex-directory' is appended to this, separated by a space.")
 
 (defvar tex-zap-file nil
   "Temporary file name used for text being sent as input to TeX.
@@ -185,13 +178,39 @@ Set by \\[tex-region], \\[tex-buffer], and \\[tex-file].")
 (defvar tex-mode-syntax-table nil
   "Syntax table used while in TeX mode.")
 
+;; Written by Wolfgang Bangerth <zcg51122@rpool1.rus.uni-stuttgart.de>
+(defvar latex-imenu-generic-expression
+  '(
+    ("Part" "\\\\part{\\([^}]*\\)}" 1)
+    ("Chapter" "\\\\chapter{\\([^}]*\\)}" 1)
+    ("Section" "\\\\[a-zA-Z]*section{\\([^}]*\\)}" 1)
+    ;; i put numbers like 3.15 before my
+    ;; \begin{equation}'s which tell me
+    ;; the number the equation will get when
+    ;; being printed.
+    ("Equations" "%[ \t]*\\([0-9]+\\.[0-9]+\\)[,;]?[ \t]?" 1))  
+
+  "Imenu generic expression for LaTex mode.  See `imenu-generic-expression'.")
+
 (defun tex-define-common-keys (keymap)
-  "Define the keys that we want defined both in TeX mode and in the tex-shell."
+  "Define the keys that we want defined both in TeX mode and in the TeX shell."
   (define-key keymap "\C-c\C-k" 'tex-kill-job)
   (define-key keymap "\C-c\C-l" 'tex-recenter-output-buffer)
   (define-key keymap "\C-c\C-q" 'tex-show-print-queue)
   (define-key keymap "\C-c\C-p" 'tex-print)
   (define-key keymap "\C-c\C-v" 'tex-view)
+
+  (define-key keymap [menu-bar tex] (cons "TeX" (make-sparse-keymap "TeX")))
+
+  (define-key keymap [menu-bar tex tex-kill-job] '("Tex Kill" . tex-kill-job))
+  (define-key keymap [menu-bar tex tex-recenter-output-buffer]
+    '("Tex Recenter" . tex-recenter-output-buffer))
+  (define-key keymap [menu-bar tex tex-show-print-queue]
+    '("Show Print Queue" . tex-show-print-queue))
+  (define-key keymap [menu-bar tex tex-alt-print]
+    '("Tex Print (alt printer)" . tex-alt-print))
+  (define-key keymap [menu-bar tex tex-print] '("Tex Print" . tex-print))
+  (define-key keymap [menu-bar tex tex-view] '("Tex View" . tex-view))
   )
 
 (defvar tex-mode-map nil "Keymap for TeX mode.")
@@ -209,12 +228,34 @@ Set by \\[tex-region], \\[tex-buffer], and \\[tex-file].")
   (define-key tex-mode-map "\C-c\C-f" 'tex-file)
   (define-key tex-mode-map "\C-c\C-i" 'tex-bibtex-file)
   (define-key tex-mode-map "\C-c\C-o" 'tex-latex-block)
-  (define-key tex-mode-map "\C-c\C-e" 'tex-close-latex-block))
+  (define-key tex-mode-map "\C-c\C-e" 'tex-close-latex-block)
+  (define-key tex-mode-map "\C-c\C-u" 'tex-goto-last-unclosed-latex-block)
+  (define-key tex-mode-map [menu-bar tex tex-bibtex-file]
+    '("BibTeX File" . tex-bibtex-file))
+  (define-key tex-mode-map [menu-bar tex tex-validate-region]
+    '("Validate Region" . tex-validate-region))
+  (define-key tex-mode-map [menu-bar tex validate-tex-buffer]
+    '("Validate Buffer" . validate-tex-buffer))
+  (define-key tex-mode-map [menu-bar tex tex-region]
+    '("TeX Region" . tex-region))
+  (define-key tex-mode-map [menu-bar tex tex-buffer]
+    '("TeX Buffer" . tex-buffer))
+  (define-key tex-mode-map [menu-bar tex tex-file] '("TeX File" . tex-file)))
+
+(put 'tex-region 'menu-enable 'mark-active)
+(put 'tex-validate-region 'menu-enable 'mark-active)
+(put 'tex-print 'menu-enable '(stringp tex-print-file))
+(put 'tex-alt-print 'menu-enable '(stringp tex-print-file))
+(put 'tex-view 'menu-enable '(stringp tex-print-file))
+(put 'tex-recenter-output-buffer 'menu-enable '(get-buffer "*tex-shell*"))
+(put 'tex-kill-job 'menu-enable '(tex-shell-running))
+
 
 (defvar tex-shell-map nil
-  "Keymap for the tex-shell.  A comint-mode-map with a few additions.")
+  "Keymap for the TeX shell.
+Inherits `shell-mode-map' with a few additions.")
 
-(defvar compare-windows-whitespace nil)        ; Pacify the byte-compiler
+(defvar compare-windows-whitespace)    ; Pacify the byte-compiler
 
 ;;; This would be a lot simpler if we just used a regexp search,
 ;;; but then it would be too slow.
@@ -222,10 +263,10 @@ Set by \\[tex-region], \\[tex-buffer], and \\[tex-file].")
 (defun tex-mode ()
   "Major mode for editing files of input for TeX, LaTeX, or SliTeX.
 Tries to determine (by looking at the beginning of the file) whether
-this file is for plain TeX, LaTeX, or SliTeX and calls plain-tex-mode,
-latex-mode, or slitex-mode, respectively.  If it cannot be determined,
-such as if there are no commands in the file, the value of tex-default-mode
-is used."
+this file is for plain TeX, LaTeX, or SliTeX and calls `plain-tex-mode',
+`latex-mode', or `slitex-mode', respectively.  If it cannot be determined,
+such as if there are no commands in the file, the value of `tex-default-mode'
+says which mode to use."
   (interactive)
   (let (mode slash comment)
     (save-excursion
@@ -236,8 +277,9 @@ is used."
                                    (beginning-of-line)
                                    (search-forward "%" search-end t))))))
       (if (and slash (not comment))
-         (setq mode (if (looking-at "documentstyle")
-                         (if (looking-at "documentstyle{slides}")
+         (setq mode (if (looking-at "documentstyle\\|documentclass\\|begin\\b\\|NeedsTeXFormat{LaTeX")
+                         (if (looking-at
+                             "document\\(style\\|class\\)\\(\\[.*\\]\\)?{slides}")
                              'slitex-mode
                            'latex-mode)
                       'plain-tex-mode))))
@@ -287,17 +329,17 @@ tex-show-queue-command
        Command string used by \\[tex-show-print-queue] to show the print
        queue that \\[tex-print] put your job on.
 
-Entering Plain-tex mode calls the value of `text-mode-hook', then the value
-of `tex-mode-hook', and then the value of `plain-tex-mode-hook'.  When the
-special subshell is initiated, the value of `tex-shell-hook' is called."
+Entering Plain-tex mode runs the hook `text-mode-hook', then the hook
+`tex-mode-hook', and finally the hook `plain-tex-mode-hook'.  When the
+special subshell is initiated, the hook `tex-shell-hook' is run."
 
   (interactive)
   (tex-common-initialization)
   (setq mode-name "TeX")
   (setq major-mode 'plain-tex-mode)
   (setq tex-command tex-run-command)
-  (setq tex-start-of-header "%**start of header")
-  (setq tex-end-of-header "%**end of header")
+  (setq tex-start-of-header "%\\*\\*start of header")
+  (setq tex-end-of-header "%\\*\\*end of header")
   (setq tex-trailer "\\bye\n")
   (run-hooks 'text-mode-hook 'tex-mode-hook 'plain-tex-mode-hook))
 ;;;###autoload
@@ -341,17 +383,40 @@ tex-show-queue-command
        Command string used by \\[tex-show-print-queue] to show the print
        queue that \\[tex-print] put your job on.
 
-Entering Latex mode calls the value of text-mode-hook, then the value of
-tex-mode-hook, and then the value of latex-mode-hook.  When the special
-subshell is initiated, the value of tex-shell-hook is called."
+Entering Latex mode runs the hook `text-mode-hook', then 
+`tex-mode-hook', and finally `latex-mode-hook'.  When the special
+subshell is initiated, `tex-shell-hook' is run."
   (interactive)
   (tex-common-initialization)
   (setq mode-name "LaTeX")
   (setq major-mode 'latex-mode)
   (setq tex-command latex-run-command)
-  (setq tex-start-of-header "\\documentstyle")
-  (setq tex-end-of-header "\\begin{document}")
+  (setq tex-start-of-header "\\\\documentstyle\\|\\\\documentclass")
+  (setq tex-end-of-header "\\\\begin{document}")
   (setq tex-trailer "\\end{document}\n")
+  ;; A line containing just $$ is treated as a paragraph separator.
+  ;; A line starting with $$ starts a paragraph,
+  ;; but does not separate paragraphs if it has more stuff on it.
+  (setq paragraph-start "[ \t]*$\\|[\f%]\\|[ \t]*\\$\\$\\|\
+\\\\begin\\>\\|\\\\label\\>\\|\\\\end\\>\\|\\\\\\[\\|\\\\\\]\\|\
+\\\\chapter\\>\\|\\\\section\\>\\|\
+\\\\subsection\\>\\|\\\\subsubsection\\>\\|\
+\\\\paragraph\\>\\|\\\\subparagraph\\>\\|\
+\\\\item\\>\\|\\\\bibitem\\>\\|\\\\newline\\>\\|\\\\noindent\\>\\|\
+\\\\[a-z]*space\\>\\|\\\\[a-z]*skip\\>\\|\
+\\\\newpage\\>\\|\\\\[a-z]*page\\|\\\\footnote\\>\\|\
+\\\\marginpar\\>\\|\\\\parbox\\>\\|\\\\caption\\>")
+  (setq paragraph-separate "[ \t]*$\\|[\f%]\\|[ \t]*\\$\\$[ \t]*$\\|\
+\\\\begin\\>\\|\\\\label\\>\\|\\\\end\\>\\|\\\\\\[\\|\\\\\\]\\|\
+\\\\chapter\\>\\|\\\\section\\>\\|\
+\\\\subsection\\>\\|\\\\subsubsection\\>\\|\
+\\\\paragraph\\>\\|\\\\subparagraph\\>\\|\
+\\(\\\\item\\|\\\\bibitem\\|\\\\newline\\|\\\\noindent\\|\
+\\\\[a-z]*space\\|\\\\[a-z]*skip\\|\
+\\\\newpage\\|\\\\[a-z]*page[a-z]*\\|\\\\footnote\\|\
+\\\\marginpar\\|\\\\parbox\\|\\\\caption\\)[ \t]*\\($\\|%\\)")
+  (make-local-variable 'imenu-generic-expression)
+  (setq imenu-generic-expression latex-imenu-generic-expression)
   (run-hooks 'text-mode-hook 'tex-mode-hook 'latex-mode-hook))
 
 ;;;###autoload
@@ -392,18 +457,39 @@ tex-show-queue-command
        Command string used by \\[tex-show-print-queue] to show the print
        queue that \\[tex-print] put your job on.
 
-Entering SliTeX mode calls the value of `text-mode-hook', then the value of
-`tex-mode-hook', then the value of `latex-mode-hook', and then the value of
-`slitex-mode-hook'.  When the special subshell is initiated, the value of
-`tex-shell-hook' is called."
+Entering SliTeX mode runs the hook `text-mode-hook', then the hook
+`tex-mode-hook', then the hook `latex-mode-hook', and finally the hook
+`slitex-mode-hook'.  When the special subshell is initiated, the hook
+`tex-shell-hook' is run."
   (interactive)
   (tex-common-initialization)
   (setq mode-name "SliTeX")
   (setq major-mode 'slitex-mode)
   (setq tex-command slitex-run-command)
-  (setq tex-start-of-header "\\documentstyle{slides}")
-  (setq tex-end-of-header "\\begin{document}")
+  (setq tex-start-of-header "\\\\documentstyle{slides}\\|\\\\docuentclass{slides}")
+  (setq tex-end-of-header "\\\\begin{document}")
   (setq tex-trailer "\\end{document}\n")
+  ;; A line containing just $$ is treated as a paragraph separator.
+  ;; A line starting with $$ starts a paragraph,
+  ;; but does not separate paragraphs if it has more stuff on it.
+  (setq paragraph-start "[ \t]*$\\|[\f%]\\|[ \t]*\\$\\$\\|\
+\\\\begin\\>\\|\\\\label\\>\\|\\\\end\\>\\|\\\\\\[\\|\\\\\\]\\|\
+\\\\chapter\\>\\|\\\\section\\>\\|\
+\\\\subsection\\>\\|\\\\subsubsection\\>\\|\
+\\\\paragraph\\>\\|\\\\subparagraph\\>\\|\
+\\\\item\\>\\|\\\\bibitem\\>\\|\\\\newline\\>\\|\\\\noindent\\>\\|\
+\\\\[a-z]*space\\>\\|\\\\[a-z]*skip\\>\\|\
+\\\\newpage\\>\\|\\\\[a-z]*page\\|\\\\footnote\\>\\|\
+\\\\marginpar\\>\\|\\\\parbox\\>\\|\\\\caption\\>")
+  (setq paragraph-separate "[ \t]*$\\|[\f%]\\|[ \t]*\\$\\$[ \t]*$\\|\
+\\\\begin\\>\\|\\\\label\\>\\|\\\\end\\>\\|\\\\\\[\\|\\\\\\]\\|\
+\\\\chapter\\>\\|\\\\section\\>\\|\
+\\\\subsection\\>\\|\\\\subsubsection\\>\\|\
+\\\\paragraph\\>\\|\\\\subparagraph\\>\\|\
+\\\\item[ \t]*$\\|\\\\bibitem[ \t]*$\\|\\\\newline[ \t]*$\\|\\\\noindent[ \t]*$\\|\
+\\\\[a-z]*space[ \t]*$\\|\\\\[a-z]*skip[ \t]*$\\|\
+\\\\newpage[ \t]*$\\|\\\\[a-z]*page[a-z]*[ \t]*$\\|\\\\footnote[ \t]*$\\|\
+\\\\marginpar[ \t]*$\\|\\\\parbox[ \t]*$\\|\\\\caption[ \t]*$")
   (run-hooks
    'text-mode-hook 'tex-mode-hook 'latex-mode-hook 'slitex-mode-hook))
 
@@ -433,15 +519,20 @@ Entering SliTeX mode calls the value of `text-mode-hook', then the value of
        (modify-syntax-entry ?' "w"))
     (set-syntax-table tex-mode-syntax-table))
   (make-local-variable 'paragraph-start)
-  (setq paragraph-start "^[ \t]*$\\|^[\f\\\\%]")
+  ;; A line containing just $$ is treated as a paragraph separator.
+  (setq paragraph-start "[ \t]*$\\|[\f\\\\%]\\|[ \t]*\\$\\$")
   (make-local-variable 'paragraph-separate)
-  (setq paragraph-separate paragraph-start)
+  ;; A line starting with $$ starts a paragraph,
+  ;; but does not separate paragraphs if it has more stuff on it.
+  (setq paragraph-separate "[ \t]*$\\|[\f\\\\%]\\|[ \t]*\\$\\$[ \t]*$")
   (make-local-variable 'comment-start)
   (setq comment-start "%")
   (make-local-variable 'comment-start-skip)
   (setq comment-start-skip "\\(\\(^\\|[^\\]\\)\\(\\\\\\\\\\)*\\)\\(%+ *\\)")
   (make-local-variable 'comment-indent-function)
   (setq comment-indent-function 'tex-comment-indent)
+  (make-local-variable 'parse-sexp-ignore-comments)
+  (setq parse-sexp-ignore-comments t)
   (make-local-variable 'compare-windows-whitespace)
   (setq compare-windows-whitespace 'tex-categorize-whitespace)
   (make-local-variable 'tex-command)
@@ -584,7 +675,7 @@ area if a mismatch is found."
 
 (defun tex-terminate-paragraph (inhibit-validation)
   "Insert two newlines, breaking a paragraph for TeX.
-Check for mismatched braces/$'s in paragraph being terminated.
+Check for mismatched braces or $s in paragraph being terminated.
 A prefix arg inhibits the checking."
   (interactive "*P")
   (or inhibit-validation
@@ -606,7 +697,7 @@ A prefix arg inhibits the checking."
 
 ;;; Like tex-insert-braces, but for LaTeX.
 (defun tex-latex-block (name)
-  "Creates a matching pair of lines \\begin{NAME} and \\end{NAME} at point.
+  "Creates a matching pair of lines `\\begin{NAME}' and `\\end{NAME}' at point.
 Puts point on a blank line between them."
   (interactive
    (prog2
@@ -626,11 +717,24 @@ Puts point on a blank line between them."
       (if (eobp) (insert ?\n)))))
 
 (defun tex-last-unended-begin ()
-  "Leave point at the beginning of the last \\begin{...} that is unended."
+  "Leave point at the beginning of the last `\\begin{...}' that is unended."
   (while (and (re-search-backward "\\(\\\\begin\\s *{\\)\\|\\(\\\\end\\s *{\\)")
               (looking-at "\\\\end{"))
     (tex-last-unended-begin)))
 
+(defun tex-goto-last-unclosed-latex-block ()
+  "Move point to the last unclosed \\begin{...}.
+Mark is left at original location."
+  (interactive)
+  (let ((spot))
+    (save-excursion
+      (condition-case nil
+          (tex-last-unended-begin)
+        (error (error "Couldn't find unended \\begin")))
+      (setq spot (point)))
+    (push-mark)
+    (goto-char spot)))
+
 (defun tex-close-latex-block ()
   "Creates an \\end{...} to match the last unclosed \\begin{...}."
   (interactive "*")
@@ -727,22 +831,29 @@ line numbers for the errors."
 
 ;;; The utility functions:
 
+;;;###autoload
 (defun tex-start-shell ()
   (save-excursion
     (set-buffer
      (make-comint
       "tex-shell"
       (or tex-shell-file-name (getenv "ESHELL") (getenv "SHELL") "/bin/sh")
-      nil "-v"))
+      nil))
     (let ((proc (get-process "tex-shell")))
       (set-process-sentinel proc 'tex-shell-sentinel)
       (process-kill-without-query proc)
-      (setq tex-shell-map (copy-keymap comint-mode-map))
+      (setq comint-prompt-regexp shell-prompt-pattern)
+      (setq tex-shell-map (nconc (make-sparse-keymap) shell-mode-map))
       (tex-define-common-keys tex-shell-map)
       (use-local-map tex-shell-map)
       (run-hooks 'tex-shell-hook)
       (while (zerop (buffer-size))
-          (sleep-for 1)))))
+       (sleep-for 1)))))
+
+(defun tex-display-shell ()
+  "Make the TeX shell buffer visible in a window."
+  (display-buffer (process-buffer (get-process "tex-shell")))
+  (tex-recenter-output-buffer nil))
 
 (defun tex-shell-sentinel (proc msg)
   (cond ((null (buffer-name (process-buffer proc)))
@@ -761,32 +872,54 @@ line numbers for the errors."
       (set-buffer buffer)
       (setq default-directory directory))))
 
+(defvar tex-send-command-modified-tick 0)
+(make-variable-buffer-local 'tex-send-command-modified-tick)
+
 (defun tex-send-command (command &optional file background)
-  "Send COMMAND to tex-shell, substituting optional FILE for *.
+  "Send COMMAND to TeX shell process, substituting optional FILE for *.
 Do this in background if optional BACKGROUND is t.  If COMMAND has no *,
 FILE will be appended, preceded by a blank, to COMMAND.  If FILE is nil, no
 substitution will be made in COMMAND.  COMMAND can be any expression that
 evaluates to a command string."
   (save-excursion
     (let* ((cmd (eval command))
+          (proc (or (get-process "tex-shell") (error "No TeX subprocess")))
+          (buf (process-buffer proc))
            (star (string-match "\\*" cmd))
-           (front (substring cmd 0 star))
-           (back (if star (substring cmd (1+ star)) "")))
-      (comint-proc-query (get-process "tex-shell")
-                         (concat
-                          (if file (if star (concat front file back)
-                                     (concat cmd " " file))
-                            cmd)
-                          (if background "&\n" "\n"))))))
-
-(defun tex-delete-last-temp-files ()
-  "Delete any junk files from last temp file."
+          (string
+           (concat
+            (if file
+                (if star (concat (substring cmd 0 star)
+                                 file (substring cmd (1+ star)))
+                  (concat cmd " " file))
+              cmd)
+            (if background "&" ""))))
+      ;; Switch to buffer before checking for subproc output in it.
+      (set-buffer buf)
+      ;; If text is unchanged since previous tex-send-command,
+      ;; we haven't got any output.  So wait for output now.
+      (if (= (buffer-modified-tick buf) tex-send-command-modified-tick)
+         (accept-process-output proc))
+      (goto-char (process-mark proc))
+      (insert string)
+      (comint-send-input)
+      (setq tex-send-command-modified-tick (buffer-modified-tick buf)))))
+
+(defun tex-delete-last-temp-files (&optional not-all)
+  "Delete any junk files from last temp file.
+If NOT-ALL is non-nil, save the `.dvi' file."
   (if tex-last-temp-file
       (let* ((dir (file-name-directory tex-last-temp-file))
-             (list (file-name-all-completions
-                    (file-name-nondirectory tex-last-temp-file) dir)))
-        (while list
-          (delete-file (concat dir (car list)))
+            (list (and (file-directory-p dir)
+                       (file-name-all-completions
+                        (file-name-nondirectory tex-last-temp-file) dir))))
+       (while list
+         (if not-all
+             (and
+              ;; If arg is non-nil, don't delete the .dvi file.
+              (not (string-match "\\.dvi$" (car list)))
+              (delete-file (concat dir (car list))))
+           (delete-file (concat dir (car list))))
           (setq list (cdr list))))))
 
 (add-hook 'kill-emacs-hook 'tex-delete-last-temp-files)
@@ -821,7 +954,9 @@ The value of `tex-command' specifies the command to use to run TeX."
          (zap-directory
           (file-name-as-directory (expand-file-name tex-directory)))
          (tex-out-file (concat zap-directory tex-zap-file)))
-    (tex-delete-last-temp-files)
+    ;; Don't delete temp files if we do the same buffer twice in a row.
+    (or (eq (current-buffer) tex-last-buffer-texed)
+       (tex-delete-last-temp-files t))
     ;; Write the new temp file.
     (save-excursion
       (save-restriction
@@ -833,11 +968,11 @@ The value of `tex-command' specifies the command to use to run TeX."
              (default-directory zap-directory))
          (goto-char (point-min))
          ;; Initialize the temp file with either the header or nothing
-         (if (search-forward tex-start-of-header search-end t)
+         (if (re-search-forward tex-start-of-header search-end t)
              (progn
                (beginning-of-line)
                (setq hbeg (point))     ;mark beginning of header
-               (if (search-forward tex-end-of-header nil t)
+               (if (re-search-forward tex-end-of-header nil t)
                    (progn (forward-line 1)
                           (setq hend (point))) ;mark end of header
                  (setq hbeg (point-min))))) ;no header
@@ -857,6 +992,7 @@ The value of `tex-command' specifies the command to use to run TeX."
     (setq tex-last-temp-file tex-out-file)
     (tex-send-command tex-shell-cd-command zap-directory)
     (tex-send-command tex-command tex-out-file)
+    (tex-display-shell)
     (setq tex-print-file tex-out-file)
     (setq tex-last-buffer-texed (current-buffer))))
 
@@ -884,6 +1020,7 @@ This function is more useful than \\[tex-buffer] when you need the
       (tex-start-shell))
     (tex-send-command tex-shell-cd-command file-dir)
     (tex-send-command tex-command tex-out-file))
+  (tex-display-shell)
   (setq tex-last-buffer-texed (current-buffer))
   (setq tex-print-file (buffer-file-name)))
 
@@ -946,9 +1083,11 @@ is provided, use the alternative command, `tex-alt-dvi-print-command'."
   (let ((print-file-name-dvi (tex-append tex-print-file ".dvi"))
        test-name)
     (if (and (not (equal (current-buffer) tex-last-buffer-texed))
+            (buffer-file-name)
+            ;; Check that this buffer's printed file is up to date.
             (file-newer-than-file-p
              (setq test-name (tex-append (buffer-file-name) ".dvi"))
-             print-file-name-dvi))
+             (buffer-file-name)))
        (setq print-file-name-dvi test-name))
     (if (not (file-exists-p print-file-name-dvi))
         (error "No appropriate `.dvi' file could be found")
@@ -956,62 +1095,51 @@ is provided, use the alternative command, `tex-alt-dvi-print-command'."
         (if alt tex-alt-dvi-print-command tex-dvi-print-command)
         print-file-name-dvi t))))
 
+(defun tex-alt-print ()
+  "Print the .dvi file made by \\[tex-region], \\[tex-buffer] or \\[tex-file].
+Runs the shell command defined by `tex-alt-dvi-print-command'."
+  (interactive)
+  (tex-print t))
+
 (defun tex-view ()
   "Preview the last `.dvi' file made by running TeX under Emacs.
 This means, made using \\[tex-region], \\[tex-buffer] or \\[tex-file].
-The variable `tex-dvi-view-command' specifies the shell command for preview."
+The variable `tex-dvi-view-command' specifies the shell command for preview.
+You must set that variable yourself before using this command,
+because there is no standard value that would generally work."
   (interactive)
+  (or tex-dvi-view-command
+      (error "You must set `tex-dvi-view-command'"))
   (let ((tex-dvi-print-command tex-dvi-view-command))
     (tex-print)))
 
 (defun tex-append (file-name suffix)
   "Append to FILENAME the suffix SUFFIX, using same algorithm TeX uses.
-Scans for the first (not last) period.
+Pascal-based TeX scans for the first period, C TeX uses the last.
 No period is retained immediately before SUFFIX,
 so normally SUFFIX starts with one."
   (if (stringp file-name)
-      (let ((file (file-name-nondirectory file-name)))
-       (concat (file-name-directory file-name)
-               (substring file 0
-                          (string-match "\\." file))
-               suffix))
+      (let ((file (file-name-nondirectory file-name))
+           trial-name)
+       ;; Try spliting on last period.
+       ;; The first-period split can get fooled when two files
+       ;; named a.tex and a.b.tex are both tex'd;
+       ;; the last-period split must be right if it matches at all.
+       (setq trial-name
+             (concat (file-name-directory file-name)
+                     (substring file 0
+                                (string-match "\\.[^.]*$" file))
+                     suffix))
+       (if (or (file-exists-p trial-name)
+               (file-exists-p (concat trial-name ".aux"))) ;for BibTeX files
+           trial-name
+         ;; Not found, so split on first period.
+         (concat (file-name-directory file-name)
+                 (substring file 0
+                            (string-match "\\." file))
+                 suffix)))
     " "))
 
-;;; Use this code after discussing with rms.  (bfox@ai.mit.edu)
-;;; Date: Tue, 31 Aug 1993 14:30:26 EDT
-;;; From: Stephen Gildea <gildea@expo2.x.org>
-;;; Sender: gnulists@ai.mit.edu
-;;; Resent-From: bug-gnu-emacs-request@prep.ai.mit.edu
-;;; 
-;;; The function tex-append in Emacs 19.19 needs to be updated for the
-;;; newer C version of TeX, which parses filenames differently.
-;;; Pascal-based TeX scans for the first period; C TeX uses the last.
-;;; Here is a version of tex-append which tries both ways.
-;;; 
-;;; (defun tex-append (file-name suffix)
-;;;   "Append to FILENAME the suffix SUFFIX, using same algorithm TeX uses.
-;;; Pascal-based TeX scans for the first period, C TeX uses the last.
-;;; No period is retained immediately before SUFFIX,
-;;; so normally SUFFIX starts with one."
-;;;   (if (stringp file-name)
-;;;       (let ((file (file-name-nondirectory file-name))
-;;;        trial-name)
-;;;    ;; try spliting on first period
-;;;    (setq trial-name
-;;;          (concat (file-name-directory file-name)
-;;;                  (substring file 0
-;;;                             (string-match "\\." file))
-;;;                  suffix))
-;;;    (if (or (file-exists-p trial-name)
-;;;            (file-exists-p (concat trial-name ".aux"))) ;for BibTeX files
-;;;        trial-name
-;;;      ;; not found, so split on last period
-;;;      (concat (file-name-directory file-name)
-;;;              (substring file 0
-;;;                         (string-match "\\.[^.]*$" file))
-;;;              suffix)))
-;;;     " "))
-
 (defun tex-show-print-queue ()
   "Show the print queue that \\[tex-print] put your job on.
 Runs the shell command defined by `tex-show-queue-command'."
@@ -1019,7 +1147,8 @@ Runs the shell command defined by `tex-show-queue-command'."
   (if (tex-shell-running)
       (tex-kill-job)
     (tex-start-shell))
-  (tex-send-command tex-show-queue-command))
+  (tex-send-command tex-show-queue-command)
+  (tex-display-shell))
 
 (defun tex-bibtex-file ()
   "Run BibTeX on the current buffer's file."
@@ -1031,7 +1160,8 @@ Runs the shell command defined by `tex-show-queue-command'."
          (tex-append (file-name-nondirectory (buffer-file-name)) ""))
        (file-dir (file-name-directory (buffer-file-name))))
     (tex-send-command tex-shell-cd-command file-dir)
-    (tex-send-command tex-bibtex-command tex-out-file)))
+    (tex-send-command tex-bibtex-command tex-out-file))
+  (tex-display-shell))
 
 (run-hooks 'tex-mode-load-hook)