(describe-function): Fix previous change.
[bpt/emacs.git] / lisp / textmodes / tex-mode.el
index eb5ff51..ebf39c2 100644 (file)
@@ -1,17 +1,18 @@
-;; TeX, LaTeX, and SliTeX mode commands.
-;; Copyright (C) 1985, 1986, 1989 Free Software Foundation, Inc.
-;; Rewritten following contributions by William F. Schelter
-;; and Dick King (king@kestrel).
-;; Supported since 1986 by Stephen Gildea <gildea@erl.mit.edu>
-;; and Michael Prange <prange@erl.mit.edu>.
-;; Various improvements and corrections in Fall, 1989 by
-;; Edward M. Reingold <reingold@cs.uiuc.edu>.
+;;; tex-mode.el --- TeX, LaTeX, and SliTeX mode commands.
+
+;; Copyright (C) 1985, 1986, 1989, 1992, 1994 Free Software Foundation, Inc.
+
+;; Maintainer: Edward M. Reingold <reingold@cs.uiuc.edu>
+;; Keywords: tex
+
+;; Contributions over the years by William F. Schelter, Dick King,
+;; Stephen Gildea, Michael Prange, and Jacob Gore.
 
 ;; This file is part of GNU Emacs.
 
 ;; GNU Emacs is free software; you can redistribute it and/or modify
 ;; it under the terms of the GNU General Public License as published by
-;; the Free Software Foundation; either version 1, or (at your option)
+;; the Free Software Foundation; either version 2, or (at your option)
 ;; any later version.
 
 ;; GNU Emacs is distributed in the hope that it will be useful,
 ;; along with GNU Emacs; see the file COPYING.  If not, write to
 ;; the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
 
-;; Still to do:
-;;  Make TAB indent correctly for TeX code.  Then we can make Linefeed
-;;  do something more useful.
-;;
-;;  Have spell understand TeX instead of assuming the entire world
-;;  uses nroff.
-;;
-;;  The code for finding matching $ needs to be fixed.
+;;; Code:
+
+(require 'shell)
+(require 'compile)
+
+;;;###autoload
+(defvar tex-shell-file-name nil
+  "*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
+and you don't try to apply \\[tex-region] or \\[tex-buffer] when there are
+`\\input' commands with relative directories.")
 
-(require 'oshell)
-(defvar tex-directory "./"
-  "*Directory in which to run TeX subjob.  Temporary files are created here.")
+;;;###autoload
+(defvar tex-offer-save t
+  "*If non-nil, ask about saving modified buffers before \\[tex-file] is run.")
 
+;;;###autoload
 (defvar tex-run-command "tex"
   "*Command used to run TeX subjob.
-The name of the file will be appended to this string, separated by a space.")
+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.
-The name of the file will be appended to this string, separated by a space.")
+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"
+        "displaymath"      "document"         "enumerate"    "eqnarray"
+        "eqnarray*"        "equation"         "figure"       "figure*"
+        "flushleft"        "flushright"       "itemize"      "letter"
+        "list"             "minipage"         "picture"      "quotation"
+        "quote"            "slide"            "sloppypar"     "tabbing"
+        "table"            "table*"           "tabular"       "tabular*"
+        "thebibliography"  "theindex*"        "titlepage"     "trivlist"
+        "verbatim"         "verbatim*"        "verse")
+  "Standard LaTeX block names.")
+
+;;;###autoload
+(defvar latex-block-names nil
+  "*User defined LaTeX block names.
+Combined with `standard-latex-block-names' for minibuffer completion.")
 
+;;;###autoload
 (defvar slitex-run-command "slitex"
   "*Command used to run SliTeX subjob.
-The name of the file will be appended to this string, separated by a space.")
+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 string used by `tex-bibtex-file' to gather bibliographic data.
-The name of the file will be appended to this string, separated by a space.")
+  "*Command used by `tex-bibtex-file' to gather bibliographic data.
+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 string used by \\[tex-print] to print a .dvi file.")
+  "*Command used by \\[tex-print] to print 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.")
 
+;;;###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 (`*'), 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 set the variable
+`tex-alt-dvi-print-command' to an expression that asks what you want;
+for example,
+
+    (setq tex-alt-dvi-print-command
+         '(format \"lpr -P%s\" (read-string \"Use printer: \")))
+
+would tell \\[tex-print] with a prefix argument to ask you which printer to
+use.")
+
+;;;###autoload
 (defvar tex-dvi-view-command nil
-  "*Command string used by \\[tex-view] to display a .dvi file.")
+  "*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,
+
+    (setq tex-dvi-view-command
+          (if (eq window-system 'x) \"xdvi\" \"dvi2tty * | cat -s\"))
 
+would tell \\[tex-view] to use xdvi under X windows and to use dvi2tty
+otherwise.")
+
+;;;###autoload
 (defvar tex-show-queue-command "lpq"
-  "*Command string used by \\[tex-show-print-queue] to show the print queue.
-Should show the queue that \\[tex-print] puts jobs on.")
+  "*Command used by \\[tex-show-print-queue] to show the print queue.
+Should show the queue(s) that \\[tex-print] puts jobs on.")
 
+;;;###autoload
 (defvar tex-default-mode 'plain-tex-mode
   "*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 "``"
   "*String inserted by typing \\[tex-insert-quote] to open a quotation.")
 
+;;;###autoload
 (defvar tex-close-quote "''"
   "*String inserted by typing \\[tex-insert-quote] to close a quotation.")
 
+(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 terminates.")
+
 (defvar tex-command nil
   "Command to run TeX.
-The name of the file will be appended to this string, separated by a space.")
+The name of the file, preceded by a blank, will be added to this string.")
 
 (defvar tex-trailer nil
   "String appended after the end of a region sent to TeX by \\[tex-region].")
@@ -89,7 +162,7 @@ The name of the file will be appended to this string, separated by a space.")
 
 (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.
@@ -106,12 +179,24 @@ Set by \\[tex-region], \\[tex-buffer], and \\[tex-file].")
   "Syntax table used while in TeX mode.")
 
 (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.")
@@ -129,12 +214,32 @@ 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-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 shell-mode-map with a few additions.")
+  "Keymap for the TeX shell.
+Inherits `comint-mode-map' with a few additions.")
 
-;(fset 'TeX-mode 'tex-mode)            ;in loaddefs.
+(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.
@@ -142,10 +247,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
@@ -156,17 +261,19 @@ 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")
+                         (if (looking-at
+                             "document\\(style\\|class\\)\\(\\[.*\\]\\)?{slides}")
                              'slitex-mode
                            'latex-mode)
                       'plain-tex-mode))))
     (if mode (funcall mode)
       (funcall tex-default-mode))))
+
 ;;;###autoload
-(fset 'TeX-mode 'tex-mode)
+(defalias 'TeX-mode 'tex-mode)
 ;;;###autoload
-(fset 'LaTeX-mode 'latex-mode)
+(defalias 'LaTeX-mode 'latex-mode)
 
 ;;;###autoload
 (defun plain-tex-mode ()
@@ -197,15 +304,19 @@ tex-directory
        run by \\[tex-region] or \\[tex-buffer].
 tex-dvi-print-command
        Command string used by \\[tex-print] to print a .dvi file.
+tex-alt-dvi-print-command
+       Alternative command string used by \\[tex-print] (when given a prefix
+       argument) to print a .dvi file.
 tex-dvi-view-command
        Command string used by \\[tex-view] to preview a .dvi file.
 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")
@@ -216,7 +327,7 @@ subshell is initiated, the value of tex-shell-hook is called."
   (setq tex-trailer "\\bye\n")
   (run-hooks 'text-mode-hook 'tex-mode-hook 'plain-tex-mode-hook))
 ;;;###autoload
-(fset 'plain-TeX-mode 'plain-tex-mode)
+(defalias 'plain-TeX-mode 'plain-tex-mode)
 
 ;;;###autoload
 (defun latex-mode ()
@@ -247,15 +358,18 @@ tex-directory
        run by \\[tex-region] or \\[tex-buffer].
 tex-dvi-print-command
        Command string used by \\[tex-print] to print a .dvi file.
+tex-alt-dvi-print-command
+       Alternative command string used by \\[tex-print] (when given a prefix
+       argument) to print a .dvi file.
 tex-dvi-view-command
        Command string used by \\[tex-view] to preview a .dvi file.
 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")
@@ -264,8 +378,30 @@ subshell is initiated, the value of tex-shell-hook is called."
   (setq tex-start-of-header "\\documentstyle")
   (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]*\\($\\|%\\)")
   (run-hooks 'text-mode-hook 'tex-mode-hook 'latex-mode-hook))
 
+;;;###autoload
 (defun slitex-mode ()
   "Major mode for editing files of input for SliTeX.
 Makes $ and } display the characters they match.
@@ -294,16 +430,19 @@ tex-directory
        run by \\[tex-region] or \\[tex-buffer].
 tex-dvi-print-command
        Command string used by \\[tex-print] to print a .dvi file.
+tex-alt-dvi-print-command
+       Alternative command string used by \\[tex-print] (when given a prefix
+       argument) to print a .dvi file.
 tex-dvi-view-command
        Command string used by \\[tex-view] to preview a .dvi file.
 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")
@@ -312,6 +451,27 @@ tex-shell-hook is called."
   (setq tex-start-of-header "\\documentstyle{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))
 
@@ -341,15 +501,20 @@ tex-shell-hook is called."
        (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-hook)
-  (setq comment-indent-hook 'tex-comment-indent)
+  (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)
@@ -396,10 +561,10 @@ tex-shell-hook is called."
 
 (defun tex-insert-quote (arg)
   "Insert the appropriate quote marks for TeX.
-Inserts the value of tex-open-quote (normally ``) or tex-close-quote
-(normally '') depending on the context.  With prefix argument, always
+Inserts the value of `tex-open-quote' (normally ``) or `tex-close-quote'
+\(normally '') depending on the context.  With prefix argument, always
 inserts \" characters."
-  (interactive "P")
+  (interactive "*P")
   (if arg
       (self-insert-command (prefix-numeric-value arg))
     (insert
@@ -414,24 +579,60 @@ inserts \" characters."
            tex-close-quote)))))
 
 (defun validate-tex-buffer ()
-  "Check current buffer for paragraphs containing mismatched $'s.
-As each such paragraph is found, a mark is pushed at its beginning,
-and the location is displayed for a few seconds."
+  "Check current buffer for paragraphs containing mismatched $s.
+Their positions are recorded in the buffer `*Occur*'.
+To find a particular invalidity from `*Occur*',
+switch to to that buffer and type C-c C-c on the line
+for the invalidity you want to see."
   (interactive)
-  (let ((opoint (point)))
-    (goto-char (point-max))
-    ;; Does not use save-excursion
-    ;; because we do not want to save the mark.
-    (unwind-protect
+  (let ((buffer (current-buffer))
+       (prevpos (point-min))
+       (linenum nil))
+    (with-output-to-temp-buffer "*Occur*"
+      (princ "Mismatches:\n")
+      (save-excursion
+       (set-buffer standard-output)
+       (occur-mode)
+       (setq occur-buffer buffer)
+       (setq occur-nlines 0)
+       (setq occur-pos-list nil))
+      (save-excursion
+       (goto-char (point-max))
        (while (and (not (input-pending-p)) (not (bobp)))
          (let ((end (point)))
+           ;; Scan the previous paragraph for invalidities.
            (search-backward "\n\n" nil 'move)
            (or (tex-validate-region (point) end)
-               (progn
-                 (push-mark (point))
-                 (message "Mismatch found in pararaph starting here")
-                 (sit-for 4)))))
-      (goto-char opoint))))
+               (let* ((end (save-excursion (forward-line 1) (point)))
+                      start tem)
+                 (beginning-of-line)
+                 (setq start (point))
+                 ;; Keep track of line number as we scan,
+                 ;; in a cumulative fashion.
+                 (if linenum
+                     (setq linenum (- linenum (count-lines prevpos (point))))
+                   (setq linenum (1+ (count-lines 1 start))))
+                 (setq prevpos (point))
+                 ;; Mention this mismatch in *Occur*.  
+                 ;; Since we scan from end of buffer to beginning,
+                 ;; add each mismatch at the beginning of *Occur*
+                 ;; and at the beginning of occur-pos-list.
+                 (save-excursion
+                   (setq tem (point-marker))
+                   (set-buffer standard-output)
+                   (goto-char (point-min))
+                   ;; Skip "Mismatches:" header line.
+                   (forward-line 1)
+                   (setq occur-pos-list (cons tem occur-pos-list))
+                   (insert-buffer-substring buffer start end)
+                   (forward-char (- start end))
+                   (insert (format "%3d: " linenum))))))))
+      (save-excursion
+       (set-buffer standard-output)
+       (if (null occur-pos-list)
+           (insert "None!\n"))
+       (if (interactive-p)
+           (message "%d mismatches found" (length occur-pos-list)))))))
 
 (defun tex-validate-region (start end)
   "Check for mismatched braces or $'s in region.
@@ -456,9 +657,9 @@ 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")
+  (interactive "*P")
   (or inhibit-validation
       (save-excursion
        (tex-validate-region
@@ -471,16 +672,23 @@ A prefix arg inhibits the checking."
 
 (defun tex-insert-braces ()
   "Make a pair of braces and be poised to type inside of them."
-  (interactive)
+  (interactive "*")
   (insert ?\{)
   (save-excursion
     (insert ?})))
 
 ;;; 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 "*sLaTeX block name: ")
+  (interactive
+   (prog2
+      (barf-if-buffer-read-only)
+      (list
+       (completing-read "LaTeX block name: "
+                       (mapcar 'list
+                                (append standard-latex-block-names
+                                        latex-block-names))))))
   (let ((col (current-column)))
     (insert (format "\\begin{%s}\n" name))
     (indent-to col)
@@ -491,18 +699,31 @@ 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 "*")
   (let ((new-line-needed (bolp))
        text indentation)
     (save-excursion
-      (condition-case ERR
+      (condition-case nil
           (tex-last-unended-begin)
         (error (error "Couldn't find unended \\begin")))
       (setq indentation (current-column))
@@ -512,6 +733,79 @@ Puts point on a blank line between them."
     (insert "\\end" text)
     (if new-line-needed (insert ?\n))))
 \f
+(defun tex-compilation-parse-errors ()
+  "Parse the current buffer as error messages.
+This makes a list of error descriptors, compilation-error-list.
+For each source-file, line-number pair in the buffer,
+the source file is read in, and the text location is saved in
+compilation-error-list.  The function `next-error', assigned to
+\\[next-error], takes the next error off the list and visits its location.
+
+This function works on TeX compilations only.  It is necessary for
+that purpose, since TeX does not put file names on the same line as
+line numbers for the errors."
+  (setq compilation-error-list nil)
+  (message "Parsing error messages...")
+  (modify-syntax-entry ?\{ "_")
+  (modify-syntax-entry ?\} "_")
+  (modify-syntax-entry ?\[ "_")
+  (modify-syntax-entry ?\] "_")
+  (let (text-buffer
+       last-filename last-linenum)
+    ;; Don't reparse messages already seen at last parse.
+    (goto-char compilation-parsing-end)
+    ;; Don't parse the first two lines as error messages.
+    ;; This matters for grep.
+    (if (bobp)
+       (forward-line 2))
+    (while (re-search-forward "^l\.[0-9]+ " nil t)
+      (let (linenum filename
+           error-marker text-marker)
+       ;; Extract file name and line number from error message.
+       ;; Line number is 2 away from beginning of line: "l.23"
+       (beginning-of-line)
+       (goto-char (+ (point) 2))
+       (setq linenum (read (current-buffer)))
+       ;; The file is the one that was opened last and is still open.
+       ;; We need to find the last open parenthesis.
+       (insert ?\))
+       (backward-sexp)
+       (forward-char)
+       (setq filename (current-word))
+       ;; Locate the erring file and line.
+       (if (and (equal filename last-filename)
+                (= linenum last-linenum))
+           nil
+         (skip-chars-backward "^(")
+         (backward-char)
+         (forward-sexp)
+         (backward-delete-char 1)
+         (setq error-marker (point-marker))
+         ;; text-buffer gets the buffer containing this error's file.
+         (if (not (equal filename last-filename))
+             (setq text-buffer
+                   (and (file-exists-p (setq last-filename filename))
+                        (find-file-noselect filename))
+                   last-linenum 0))
+         (if text-buffer
+             ;; Go to that buffer and find the erring line.
+             (save-excursion
+               (set-buffer text-buffer)
+               (if (zerop last-linenum)
+                   (progn
+                     (goto-char 1)
+                     (setq last-linenum 1)))
+               (forward-line (- linenum last-linenum))
+               (setq last-linenum linenum)
+               (setq text-marker (point-marker))
+               (setq compilation-error-list
+                     (cons (list error-marker text-marker)
+                           compilation-error-list)))))
+       (forward-line 1)))
+    (setq compilation-parsing-end (point-max)))
+  (message "Parsing error messages...done")
+  (setq compilation-error-list (nreverse compilation-error-list)))
+\f
 ;;; Invoking TeX in an inferior shell.
 
 ;;; Why use a shell instead of running TeX directly?  Because if TeX
@@ -519,17 +813,34 @@ Puts point on a blank line between them."
 
 ;;; The utility functions:
 
+;;;###autoload
 (defun tex-start-shell ()
   (save-excursion
-    (set-buffer (make-shell "tex-shell" nil nil "-v"))
-    (setq tex-shell-map (copy-keymap shell-mode-map))
-    (tex-define-common-keys tex-shell-map)
-    (use-local-map tex-shell-map)
-    (run-hooks 'tex-shell-hook)
-    (if (zerop (buffer-size))
-       (sleep-for 1))))
-
-(defun set-buffer-directory (buffer directory)
+    (set-buffer
+     (make-comint
+      "tex-shell"
+      (or tex-shell-file-name (getenv "ESHELL") (getenv "SHELL") "/bin/sh")
+      nil))
+    (let ((proc (get-process "tex-shell")))
+      (set-process-sentinel proc 'tex-shell-sentinel)
+      (process-kill-without-query proc)
+      (setq comint-prompt-regexp shell-prompt-pattern)
+      (setq tex-shell-map (copy-keymap comint-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)))))
+
+(defun tex-shell-sentinel (proc msg)
+  (cond ((null (buffer-name (process-buffer proc)))
+        ;; buffer killed
+        (set-process-buffer proc nil)
+         (tex-delete-last-temp-files))
+       ((memq (process-status proc) '(signal exit))
+         (tex-delete-last-temp-files))))
+
+(defun tex-set-buffer-directory (buffer directory)
   "Set BUFFER's default directory to be DIRECTORY."
   (setq directory (file-name-as-directory (expand-file-name directory)))
   (if (not (file-directory-p directory))
@@ -538,11 +849,59 @@ Puts point on a blank line between them."
       (set-buffer buffer)
       (setq default-directory directory))))
 
-;;; The commands:
+(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 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 (get-process "tex-shell"))
+          (buf (process-buffer proc))
+           (star (string-match "\\*" cmd))
+          (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 (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)
 
-;;; It's a kludge that we have to create a special buffer just 
-;;; to write out the tex-trailer.  It would nice if there were a
-;;; function like write-region that would write literal strings.
+;;; The commands:
 
 (defun tex-region (beg end)
   "Run TeX on the current region, via a temporary file.
@@ -561,23 +920,21 @@ The value of `tex-command' specifies the command to use to run TeX."
   (if (tex-shell-running)
       (tex-kill-job)
     (tex-start-shell))
+  (display-buffer (process-buffer (get-process "tex-shell")))
   (or tex-zap-file
       (setq tex-zap-file (tex-generate-zap-file-name)))
-  (let ((tex-out-file (concat tex-zap-file ".tex"))
-       (temp-buffer (get-buffer-create " TeX-Output-Buffer"))
-       (file-dir (if (buffer-file-name)
-                      (file-name-directory (buffer-file-name))
-                    default-directory))
-       (zap-directory
-        (file-name-as-directory (expand-file-name tex-directory))))
-    ;; Delete any junk files or memory files from this temp file,
-    ;; since the contents were probably different last time anyway.
-    ;; This may also delete the old temp file if any.
-    (let ((list (file-name-all-completions (tex-append tex-out-file ".")
-                                          zap-directory)))
-      (while list
-       (delete-file (expand-file-name (car list) zap-directory))
-       (setq list (cdr list))))
+  (let* ((temp-buffer (get-buffer-create " TeX-Output-Buffer"))
+         ; Temp file will be written and TeX will be run in zap-directory.
+         ; If the TEXINPUTS file has relative directories or if the region has
+         ; \input of files, this must be the same directory as the file for
+         ; TeX to access the correct inputs.  That's why it's safest if
+         ; tex-directory is ".".
+         (zap-directory
+          (file-name-as-directory (expand-file-name tex-directory)))
+         (tex-out-file (concat zap-directory tex-zap-file)))
+    ;; 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
@@ -597,70 +954,29 @@ The value of `tex-command' specifies the command to use to run TeX."
                    (progn (forward-line 1)
                           (setq hend (point))) ;mark end of header
                  (setq hbeg (point-min))))) ;no header
-         (write-region (min hbeg beg) hend tex-out-file nil nil)
-         (write-region (max beg hend) end tex-out-file t nil))
+         (write-region (min hbeg beg) hend
+                        (concat tex-out-file ".tex") nil nil)
+         (write-region (max beg hend) end (concat tex-out-file ".tex") t nil))
        (let ((local-tex-trailer tex-trailer))
          (set-buffer temp-buffer)
          (erase-buffer)
          ;; make sure trailer isn't hidden by a comment
          (insert-string "\n")
          (if local-tex-trailer (insert-string local-tex-trailer))
-         (set-buffer-directory temp-buffer zap-directory)
-         (write-region (point-min) (point-max) tex-out-file t nil))))
-    ;; Record in the shell buffer the file name to delete afterward.
-    (save-excursion
-      (set-buffer (get-buffer "*tex-shell*"))
-      (make-local-variable 'tex-last-temp-file)
-      (setq tex-last-temp-file tex-out-file))
-    (set-process-filter "tex-shell" 'tex-filter)
-    (set-buffer-directory "*tex-shell*" zap-directory)
-    ;; Run TeX in source file's dir, in case TEXINPUTS uses current dir.
-    (send-string "tex-shell" (concat tex-shell-cd-command " " file-dir "\n"))
-    (send-string "tex-shell" (concat tex-command " \""
-                                    zap-directory
-                                    tex-out-file "\"\n")))
-  (setq tex-last-buffer-texed (current-buffer))
-  (setq tex-print-file
-       (concat (file-name-as-directory (expand-file-name tex-directory))
-               tex-zap-file))
-  (tex-recenter-output-buffer 0))
-
-;; This filter is used in the TeX shell buffer
-;; while TeX is running for a tex-region command.
-(defun tex-filter (process string)
-  (let ((old (current-buffer)))
-    (set-buffer (process-buffer proc))
-    (unwind-protect
-       (progn (if (= (process-mark proc) (point-max))
-                  (insert string)
-                (save-excursion
-                  (goto-char (process-mark proc))
-                  (insert string)))
-              (set-marker (process-mark proc) (point))
-              ;; Delete the temporary file
-              ;; when TeX finishes.
-              ;; And stop using this filter.
-              (save-excursion
-                (forward-line -1)
-                (if (looking-at "^Output written on ")
-                    (progn
-                      (set-process-filter process nil)
-                      ;; Delete the temp file just processed
-                      ;; and any related junk files made by TeX.
-                      (let ((list (file-name-all-completions
-                                   (tex-append tex-last-temp-file ".")
-                                   zap-directory)))
-                        (while list
-                          (delete-file (expand-file-name
-                                        (car list) zap-directory))
-                          (setq list (cdr list))))))))
-      (or (eq old (current-buffer))
-         (set-buffer old)))))
+         (tex-set-buffer-directory temp-buffer zap-directory)
+         (write-region (point-min) (point-max)
+                        (concat tex-out-file ".tex") t nil))))
+    ;; Record the file name to be deleted afterward.
+    (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)
+    (setq tex-print-file tex-out-file)
+    (setq tex-last-buffer-texed (current-buffer))))
 
 (defun tex-buffer ()
   "Run TeX on current buffer.  See \\[tex-region] for more information.
-Does not save the buffer, so it's useful for trying
-experimental versions.  See \\[tex-file] for an alternative."
+Does not save the buffer, so it's useful for trying experimental versions.
+See \\[tex-file] for an alternative."
   (interactive)
   (tex-region (point-min) (point-max)))
 
@@ -674,17 +990,16 @@ This function is more useful than \\[tex-buffer] when you need the
              (file-name-nondirectory (buffer-file-name))
            (error "Buffer does not seem to be associated with any file")))
        (file-dir (file-name-directory (buffer-file-name))))
-    (save-some-buffers)
+    (if tex-offer-save
+        (save-some-buffers))
     (if (tex-shell-running)
         (tex-kill-job)
       (tex-start-shell))
-    (set-buffer-directory "*tex-shell*" file-dir)
-    (send-string "tex-shell" (concat tex-shell-cd-command " " file-dir "\n"))
-    (send-string "tex-shell"
-                (concat tex-command " \"" tex-out-file "\"\n")))
+    (display-buffer (process-buffer (get-process "tex-shell")))
+    (tex-send-command tex-shell-cd-command file-dir)
+    (tex-send-command tex-command tex-out-file))
   (setq tex-last-buffer-texed (current-buffer))
-  (setq tex-print-file (buffer-file-name))
-  (tex-recenter-output-buffer 0))
+  (setq tex-print-file (buffer-file-name)))
 
 (defun tex-generate-zap-file-name ()
   "Generate a unique name suitable for use as a file name."
@@ -718,8 +1033,7 @@ This function is more useful than \\[tex-buffer] when you need the
 (defun tex-kill-job ()
   "Kill the currently running TeX job."
   (interactive)
-  (if (get-process "tex-shell")
-      (quit-process "tex-shell" t)))
+  (quit-process (get-process "tex-shell") t))
 
 (defun tex-recenter-output-buffer (linenum)
   "Redisplay buffer of TeX job output so that most recent output can be seen.
@@ -736,24 +1050,31 @@ line LINE of the window, or centered if LINE is nil."
       (recenter (if linenum
                    (prefix-numeric-value linenum)
                  (/ (window-height) 2)))
-      (pop-to-buffer old-buffer)
-      )))
+      (pop-to-buffer old-buffer))))
 
-(defun tex-print ()
+(defun tex-print (&optional alt)
   "Print the .dvi file made by \\[tex-region], \\[tex-buffer] or \\[tex-file].
-Runs the shell command defined by tex-dvi-print-command."
-  (interactive)
+Runs the shell command defined by `tex-dvi-print-command'.  If prefix argument
+is provided, use the alternative command, `tex-alt-dvi-print-command'."
+  (interactive "P")
   (let ((print-file-name-dvi (tex-append tex-print-file ".dvi"))
        test-name)
     (if (and (not (equal (current-buffer) tex-last-buffer-texed))
             (file-newer-than-file-p
              (setq test-name (tex-append (buffer-file-name) ".dvi"))
-             (tex-append tex-print-file ".dvi")))
+             print-file-name-dvi))
        (setq print-file-name-dvi test-name))
-    (if (file-exists-p print-file-name-dvi)
-       (shell-command
-        (concat tex-dvi-print-command " \"" print-file-name-dvi "&\"\n"))
-      (error "No appropriate `.dvi' file could be found"))))
+    (if (not (file-exists-p print-file-name-dvi))
+        (error "No appropriate `.dvi' file could be found")
+      (tex-send-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.
@@ -765,26 +1086,37 @@ The variable `tex-dvi-view-command' specifies the shell command for preview."
 
 (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 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."
+Runs the shell command defined by `tex-show-queue-command'."
   (interactive)
   (if (tex-shell-running)
       (tex-kill-job)
     (tex-start-shell))
-  (send-string "tex-shell" (concat tex-show-queue-command "\n"))
-  (tex-recenter-output-buffer nil))
+  (display-buffer (process-buffer (get-process "tex-shell")))
+  (tex-send-command tex-show-queue-command))
 
 (defun tex-bibtex-file ()
   "Run BibTeX on the current buffer's file."
@@ -792,14 +1124,16 @@ Runs the shell command defined by tex-show-queue-command."
   (if (tex-shell-running)
       (tex-kill-job)
     (tex-start-shell))
+  (display-buffer (process-buffer (get-process "tex-shell")))
   (let ((tex-out-file
          (tex-append (file-name-nondirectory (buffer-file-name)) ""))
        (file-dir (file-name-directory (buffer-file-name))))
-    (set-buffer-directory "*tex-shell*" file-dir)
-    (send-string "tex-shell" (concat tex-shell-cd-command " " file-dir "\n"))
-    (send-string "tex-shell"
-                (concat tex-bibtex-command " \"" tex-out-file "\"\n")))
-  (tex-recenter-output-buffer 0))
+    (tex-send-command tex-shell-cd-command file-dir)
+    (tex-send-command tex-bibtex-command tex-out-file)))
+
+(run-hooks 'tex-mode-load-hook)
 
 (provide 'tex-mode)
 
+;;; tex-mode.el ends here
+