]> code.delx.au - gnu-emacs/blobdiff - lisp/textmodes/tex-mode.el
(outline-up-heading): Avoid infinite loop at beginning of buffer.
[gnu-emacs] / lisp / textmodes / tex-mode.el
index 4f19edcdc2f9fd837b7576b25f97d92d021f8111..509b7fd4556dc1fa3802def1b81238397d225bae 100644 (file)
@@ -1,6 +1,7 @@
 ;;; tex-mode.el --- TeX, LaTeX, and SliTeX mode commands.
 
-;; Copyright (C) 1985, 86, 89, 92, 94, 95, 96 Free Software Foundation, Inc.
+;; Copyright (C) 1985, 86, 89, 92, 94, 95, 96, 1997
+;;       Free Software Foundation, Inc.
 
 ;; Maintainer: FSF
 ;; Keywords: tex
 (require 'shell)
 (require 'compile)
 
+(defgroup tex-file nil
+  "TeX files and directories"
+  :prefix "tex-"
+  :group 'tex)
+
+(defgroup tex-run nil
+  "Running external commands from TeX mode"
+  :prefix "tex-"
+  :group 'tex)
+
+(defgroup tex-view nil
+  "Viewing and printing TeX files"
+  :prefix "tex-"
+  :group 'tex)
+
 ;;;###autoload
-(defvar tex-shell-file-name nil
-  "*If non-nil, the shell file name to run in the subshell used to run TeX.")
+(defcustom tex-shell-file-name nil
+  "*If non-nil, the shell file name to run in the subshell used to run TeX."
+  :type '(choice (const :tag "None" nil)
+                string)
+  :group 'tex-run)
 
 ;;;###autoload
-(defvar tex-directory "."
+(defcustom tex-directory "."
   "*Directory in which temporary files are written.
 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."
+  :type 'directory
+  :group 'tex-file)
+
+;;;###autoload
+(defcustom tex-first-line-header-regexp nil
+  "Regexp for matching a first line which `tex-region' should include.
+If this is non-nil, it should be a regular expression string;
+if it matches the first line of the file,
+`tex-region' always includes the first line in the TeX run."
+  :type '(choice (const :tag "None" nil)
+                 regexp)
+  :group 'tex-file)
 
 ;;;###autoload
-(defvar tex-main-file nil
+(defcustom tex-main-file nil
   "*The main TeX source file which includes this buffer's file.
-The command `tex-buffer' runs TeX on `tex-main-file'if that is non-nil.")
+The command `tex-buffer' runs TeX on `tex-main-file'if that is non-nil."
+  :type '(choice (const :tag "None" nil)
+                 file)
+  :group 'tex-file)
 
 ;;;###autoload
-(defvar tex-offer-save t
-  "*If non-nil, ask about saving modified buffers before \\[tex-file] is run.")
+(defcustom tex-offer-save t
+  "*If non-nil, ask about saving modified buffers before \\[tex-file] is run."
+  :type 'boolean
+  :group 'tex-file)
 
 ;;;###autoload
-(defvar tex-run-command "tex"
+(defcustom tex-run-command "tex"
   "*Command used to run TeX subjob.
 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.")
+otherwise, the file name, preceded by blank, is added at the end."
+  :type 'string
+  :group 'tex-run)
 
 ;;;###autoload
-(defvar latex-run-command "latex"
+(defcustom latex-run-command "latex"
   "*Command used to run LaTeX subjob.
 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.")
+otherwise, the file name, preceded by blank, is added at the end."
+  :type 'string
+  :group 'tex-run)
 
 (defvar standard-latex-block-names
       '("abstract"         "array"            "center"       "description"
@@ -75,30 +115,38 @@ otherwise, the file name, preceded by blank, is added at the end.")
   "Standard LaTeX block names.")
 
 ;;;###autoload
-(defvar latex-block-names nil
+(defcustom latex-block-names nil
   "*User defined LaTeX block names.
-Combined with `standard-latex-block-names' for minibuffer completion.")
+Combined with `standard-latex-block-names' for minibuffer completion."
+  :type '(repeat string)
+  :group 'tex-run)
 
 ;;;###autoload
-(defvar slitex-run-command "slitex"
+(defcustom slitex-run-command "slitex"
   "*Command used to run SliTeX subjob.
 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.")
+otherwise, the file name, preceded by blank, is added at the end."
+  :type 'string
+  :group 'tex-run)
 
 ;;;###autoload
-(defvar tex-bibtex-command "bibtex"
+(defcustom tex-bibtex-command "bibtex"
   "*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.")
+otherwise, the file name, preceded by blank, is added at the end."
+  :type 'string
+  :group 'tex-run)
 
 ;;;###autoload
-(defvar tex-dvi-print-command "lpr -d"
+(defcustom tex-dvi-print-command "lpr -d"
   "*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.")
+otherwise, the file name, preceded by blank, is added at the end."
+  :type 'string
+  :group 'tex-view)
 
 ;;;###autoload
-(defvar tex-alt-dvi-print-command "lpr -d"
+(defcustom 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.
@@ -111,10 +159,13 @@ for example,
          '(format \"lpr -P%s\" (read-string \"Use printer: \")))
 
 would tell \\[tex-print] with a prefix argument to ask you which printer to
-use.")
+use."
+  :type '(choice (string :tag "Command")
+                (sexp :tag "Expression"))
+  :group 'tex-view)
 
 ;;;###autoload
-(defvar tex-dvi-view-command nil
+(defcustom tex-dvi-view-command nil
   "*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.
@@ -126,27 +177,37 @@ window system being used.  For example,
           (if (eq window-system 'x) \"xdvi\" \"dvi2tty * | cat -s\"))
 
 would tell \\[tex-view] to use xdvi under X windows and to use dvi2tty
-otherwise.")
+otherwise."
+  :type '(choice (const nil) string)
+  :group 'tex-view)
 
 ;;;###autoload
-(defvar tex-show-queue-command "lpq"
+(defcustom tex-show-queue-command "lpq"
   "*Command used by \\[tex-show-print-queue] to show the print queue.
-Should show the queue(s) that \\[tex-print] puts jobs on.")
+Should show the queue(s) that \\[tex-print] puts jobs on."
+  :type 'string
+  :group 'tex-view)
 
 ;;;###autoload
-(defvar tex-default-mode 'plain-tex-mode
+(defcustom 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'."
+  :type 'function
+  :group 'tex)
 
 ;;;###autoload
-(defvar tex-open-quote "``"
-  "*String inserted by typing \\[tex-insert-quote] to open a quotation.")
+(defcustom tex-open-quote "``"
+  "*String inserted by typing \\[tex-insert-quote] to open a quotation."
+  :type 'string
+  :group 'tex)
 
 ;;;###autoload
-(defvar tex-close-quote "''"
-  "*String inserted by typing \\[tex-insert-quote] to close a quotation.")
+(defcustom tex-close-quote "''"
+  "*String inserted by typing \\[tex-insert-quote] to close a quotation."
+  :type 'string
+  :group 'tex)
 
 (defvar tex-last-temp-file nil
   "Latest temporary file generated by \\[tex-region] and \\[tex-buffer].
@@ -184,23 +245,84 @@ Set by \\[tex-region], \\[tex-buffer], and \\[tex-file].")
 (defvar tex-mode-syntax-table nil
   "Syntax table used while in TeX mode.")
 
+(defcustom latex-imenu-indent-string "."
+  "*String to add repeated in front of nested sectional units for Imenu.
+An alternative value is \" . \", if you use a font with a narrow period."
+  :type 'string
+  :group 'tex)
+
 (defun latex-imenu-create-index ()
   "Generates an alist for imenu from a LaTeX buffer."
-  (let (result temp)
-    (goto-char (point-max))
-    (while (re-search-backward "\\\\\\(part\\|chapter\\|\
-\\(sub\\)?\\(\\(sub\\)?section\\|paragraph\\)\\)\\*?[ \t\n]*{\\([^}]*\\)}" nil t)
-      (setq temp
-           (assoc (buffer-substring-no-properties (match-beginning 1)
-                                                  (match-end 1)) 
-                  '(("part" . "") ("chapter" . " ")
-                    ("section" . "  ") ("subsection" . "   ")
-                    ("subsubsection" . "    ")
-                    ("paragraph" . "     ") ("subparagraph" . "      "))))
-      (setq result (cons (cons (concat (cdr temp) (match-string 5)) 
-                              (match-beginning 0))
-                        result)))
-    result))
+  (let (i0 menu case-fold-search)
+    (save-excursion
+      ;; Find the top-most level in this file but don't allow it to be
+      ;; any deeper than "section" (which is top-level in an article).
+      (goto-char (point-min))
+      (if (search-forward-regexp "\\\\part\\*?[ \t]*{" nil t)
+         (setq i0 0)
+       (if (search-forward-regexp "\\\\chapter\\*?[ \t]*{" nil t)
+           (setq i0 1)
+         (setq i0 2)))
+
+      ;; Look for chapters and sections.
+      (goto-char (point-min))
+      (while (search-forward-regexp
+             "\\\\\\(part\\|chapter\\|section\\|subsection\\|\
+subsubsection\\|paragraph\\|subparagraph\\)\\*?[ \t]*{" nil t)
+       (let ((start (match-beginning 0))
+             (here (point))
+             (i (cdr (assoc (buffer-substring-no-properties
+                             (match-beginning 1)
+                             (match-end 1))
+                            '(("part" . 0) ("chapter" . 1)
+                              ("section" . 2) ("subsection" . 3)
+                              ("subsubsection" . 4)
+                              ("paragraph" . 5) ("subparagraph" . 6))))))
+         (backward-char 1)
+         (condition-case err
+             (progn
+               ;; Using sexps allows some use of matching {...} inside
+               ;; titles.
+               (forward-sexp 1)
+               (setq menu
+                     (cons (cons (concat (apply 'concat
+                                                (make-list
+                                                 (max 0 (- i i0))
+                                                 latex-imenu-indent-string))
+                                         (buffer-substring-no-properties
+                                          here (1- (point))))
+                                 start)
+                           menu))
+               )
+           (error nil))))
+
+      ;; Look for included material.
+      (goto-char (point-min))
+      (while (search-forward-regexp
+             "\\\\\\(include\\|input\\|verbatiminput\\|bibliography\\)\
+[ \t]*{\\([^}\n]+\\)}"
+             nil t)
+       (setq menu
+             (cons (cons (concat "<<" (buffer-substring-no-properties
+                                       (match-beginning 2)
+                                       (match-end 2))
+                                 (if (= (char-after (match-beginning 1)) ?b)
+                                     ".bbl"
+                                   ".tex"))
+                         (match-beginning 0))
+                   menu)))
+
+      ;; Look for \frontmatter, \mainmatter, \backmatter, and \appendix.
+      (goto-char (point-min))
+      (while (search-forward-regexp
+             "\\\\\\(frontmatter\\|mainmatter\\|backmatter\\|appendix\\)\\b"
+             nil t)
+       (setq menu
+             (cons (cons "--" (match-beginning 0))
+                   menu)))
+
+      ;; Sort in increasing buffer position order.
+      (sort menu (function (lambda (a b) (< (cdr a) (cdr b))))))))
 
 (defun tex-define-common-keys (keymap)
   "Define the keys that we want defined both in TeX mode and in the TeX shell."
@@ -544,6 +666,9 @@ Entering SliTeX mode runs the hook `text-mode-hook', then the hook
        (modify-syntax-entry ?~ " ")
        (modify-syntax-entry ?' "w"))
     (set-syntax-table tex-mode-syntax-table))
+  ;; Regexp isearch should accept newline and formfeed as whitespace.
+  (make-local-variable 'search-whitespace-regexp)
+  (setq search-whitespace-regexp "[ \t\r\n\f]+")
   (make-local-variable 'paragraph-start)
   ;; A line containing just $$ is treated as a paragraph separator.
   (setq paragraph-start "[ \t]*$\\|[\f\\\\%]\\|[ \t]*\\$\\$")
@@ -598,10 +723,10 @@ Entering SliTeX mode runs the hook `text-mode-hook', then the hook
     (while not-finished
       (cond ((looking-at "[ \t]+")
             (goto-char (match-end 0))
-            (if (eql category 'null)
+            (if (eq category 'null)
                 (setq category 'space)))
            ((looking-at "\n")
-            (cond ((eql category 'newline)
+            (cond ((eq category 'newline)
                    (setq category 'par)
                    (setq not-finished nil))
                   (t
@@ -613,7 +738,7 @@ Entering SliTeX mode runs the hook `text-mode-hook', then the hook
            (t
             (setq not-finished nil))))
     (skip-chars-forward " \t\n\f")
-    (if (eql category 'newline)
+    (if (eq category 'newline)
        'space                          ;TeX doesn't distinguish
       category)))
 
@@ -988,15 +1113,14 @@ The value of `tex-command' specifies the command to use to run TeX."
     (tex-start-shell))
   (or tex-zap-file
       (setq tex-zap-file (tex-generate-zap-file-name)))
-  (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
+  ;; 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 ".".
+  (let* ((zap-directory
           (file-name-as-directory (expand-file-name tex-directory)))
-         (tex-out-file (concat zap-directory tex-zap-file)))
+         (tex-out-file (concat zap-directory tex-zap-file ".tex")))
     ;; 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))
@@ -1007,30 +1131,46 @@ The value of `tex-command' specifies the command to use to run TeX."
        (goto-char (point-min))
        (forward-line 100)
        (let ((search-end (point))
-             (hbeg (point-min)) (hend (point-min))
-             (default-directory zap-directory))
+             (default-directory zap-directory)
+             (already-output 0))
          (goto-char (point-min))
-         ;; Initialize the temp file with either the header or nothing
+
+          ;; Maybe copy first line, such as `\input texinfo', to temp file.
+         (and tex-first-line-header-regexp
+              (looking-at tex-first-line-header-regexp)
+              (write-region (point) 
+                            (progn (forward-line 1)
+                                   (setq already-output (point)))
+                            tex-out-file nil nil))
+
+         ;; Write out the header, if there is one,
+         ;; and any of the specified region which extends before it.
+         ;; But don't repeat anything already written.
          (if (re-search-forward tex-start-of-header search-end t)
-             (progn
+             (let (hbeg)
                (beginning-of-line)
                (setq hbeg (point))     ;mark beginning of header
                (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
-         (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))
-         (tex-set-buffer-directory temp-buffer zap-directory)
-         (write-region (point-min) (point-max)
-                        (concat tex-out-file ".tex") t nil))))
+                   (let (hend)
+                     (forward-line 1)
+                     (setq hend (point))       ;mark end of header
+                     (write-region (max (min hbeg beg) already-output)
+                                   hend
+                                   tex-out-file
+                                   (not (zerop already-output)) nil)
+                     (setq already-output hend)))))
+
+         ;; Write out the specified region
+         ;; (but don't repeat anything already written).
+         (write-region (max beg already-output) end
+                       tex-out-file
+                       (not (zerop already-output)) nil))
+       ;; Write the trailer, if any.
+       ;; Precede it with a newline to make sure it
+       ;; is not hidden in a comment.
+       (if tex-trailer
+           (write-region (concat "\n" tex-trailer) nil
+                         tex-out-file 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)