(BUFFER_POS_REACHED_P): Return true if pos reached and
[bpt/emacs.git] / lisp / textmodes / tex-mode.el
index 2a9059e..4b878f4 100644 (file)
@@ -127,7 +127,7 @@ and the input file name, with no separating space and are not shell-quoted.
 If nil, TeX runs with no options.  See the documentation of `tex-command'."
   :type 'string
   :group 'tex-run
-  :version "21.4")
+  :version "22.1")
 
 ;;;###autoload
 (defcustom tex-start-commands "\\nonstopmode\\input"
@@ -139,7 +139,7 @@ If nil, no commands are used.  See the documentation of `tex-command'."
                       "\\nonstopmode\\input")
                (string :tag "String at your choice"))
   :group 'tex-run
-  :version "21.4")
+  :version "22.1")
 
 (defvar latex-standard-block-names
   '("abstract"         "array"         "center"        "description"
@@ -199,7 +199,7 @@ use."
 (defcustom tex-dvi-view-command
   '(cond
     ((eq window-system 'x) "xdvi")
-    ((eq window-system 'w32) "yap") 
+    ((eq window-system 'w32) "yap")
     (t "dvi2tty * | cat -s"))
   "*Command used by \\[tex-view] to display a `.dvi' file.
 If it is a string, that specifies the command directly.
@@ -468,6 +468,10 @@ An alternative value is \" . \", if you use a font with a narrow period."
           ;; (arg "\\(?:{\\(\\(?:[^{}\\]+\\|\\\\.\\|{[^}]*}\\)+\\)\\|\\\\[a-z*]+\\)"))
           (arg "{\\(\\(?:[^{}\\]+\\|\\\\.\\|{[^}]*}\\)+\\)"))
       (list
+       ;; font-lock-syntactic-keywords causes the \ of \end{verbatim} to be
+       ;; highlighted as tex-verbatim-face.  Let's undo that.
+       ;; This is ugly and brittle :-(  --Stef
+       '("^\\(\\\\\\)end" (1 (get-text-property (match-end 1) 'face) t))
        ;; display $$ math $$
        ;; We only mark the match between $$ and $$ because the $$ delimiters
        ;; themselves have already been marked (along with $..$) by syntactic
@@ -500,6 +504,11 @@ An alternative value is \" . \", if you use a font with a narrow period."
         1 font-lock-function-name-face))))
   "Subdued expressions to highlight in TeX modes.")
 
+(defun tex-font-lock-append-prop (prop)
+  (unless (memq (get-text-property (match-end 1) 'face)
+               '(font-lock-comment-face tex-verbatim-face))
+    prop))
+
 (defconst tex-font-lock-keywords-2
   (append tex-font-lock-keywords-1
    (eval-when-compile
@@ -553,16 +562,19 @@ An alternative value is \" . \", if you use a font with a narrow period."
        ;;
        ;; Font environments.  It seems a bit dubious to use `bold' etc. faces
        ;; since we might not be able to display those fonts.
-       (list (concat slash bold " *" arg) 2 '(quote bold) 'append)
-       (list (concat slash italic " *" arg) 2 '(quote italic) 'append)
+       (list (concat slash bold " *" arg) 2
+             '(tex-font-lock-append-prop 'bold) 'append)
+       (list (concat slash italic " *" arg) 2
+             '(tex-font-lock-append-prop 'italic) 'append)
        ;; (list (concat slash type arg) 2 '(quote bold-italic) 'append)
        ;;
        ;; Old-style bf/em/it/sl.  Stop at `\\' and un-escaped `&', for tables.
        (list (concat "\\\\\\(em\\|it\\|sl\\)\\>" args)
-             2 '(quote italic) 'append)
+             2 '(tex-font-lock-append-prop 'italic) 'append)
        ;; This is separate from the previous one because of cases like
        ;; {\em foo {\bf bar} bla} where both match.
-       (list (concat "\\\\bf\\>" args) 1 '(quote bold) 'append)))))
+       (list (concat "\\\\\\(bf\\)\\>" args)
+             2 '(tex-font-lock-append-prop 'bold) 'append)))))
    "Gaudy expressions to highlight in TeX modes.")
 
 (defun tex-font-lock-suscript (pos)
@@ -604,11 +616,16 @@ An alternative value is \" . \", if you use a font with a narrow period."
 (defvar tex-font-lock-syntactic-keywords
   (let ((verbs (regexp-opt tex-verbatim-environments t)))
     `((,(concat "^\\\\begin *{" verbs "}.*\\(\n\\)") 2 "|")
-      (,(concat "^\\\\end *{" verbs "}\\(.?\\)") 2
-       (unless (<= (match-beginning 0) (point-min))
-        (put-text-property (1- (match-beginning 0)) (match-beginning 0)
-                           'syntax-table (string-to-syntax "|"))
-        "<"))
+      ;; Technically, we'd like to put the "|" property on the \n preceding
+      ;; the \end, but this would have 2 disadvantages:
+      ;; 1 - it's wrong if the verbatim env is empty (the same \n is used to
+      ;;     start and end the fenced-string).
+      ;; 2 - font-lock considers the preceding \n as being part of the
+      ;;     preceding line, so things gets screwed every time the previous
+      ;;     line is re-font-locked on its own.
+      ;; There's a hack in tex-font-lock-keywords-1 to remove the verbatim
+      ;; face from the \ but C-M-f still jumps to the wrong spot :-(  --Stef
+      (,(concat "^\\(\\\\\\)end *{" verbs "}\\(.?\\)") (1 "|") (3 "<"))
       ;; ("^\\(\\\\\\)begin *{comment}" 1 "< b")
       ;; ("^\\\\end *{comment}.*\\(\n\\)" 1 "> b")
       ("\\\\verb\\**\\([^a-z@*]\\)" 1 "\""))))
@@ -1173,14 +1190,14 @@ area if a mismatch is found."
              (forward-sexp 1))
            ;; Now check that like matches like.
            (goto-char start)
-           (while (progn (skip-syntax-forward "^(")
-                         (not (eobp)))
-             (let ((match (matching-paren (following-char))))
-               (save-excursion
+           (while (re-search-forward "\\s(" nil t)
+             (save-excursion
+               (let ((pos (match-beginning 0)))
+                 (goto-char pos)
                  (forward-sexp 1)
-                 (or (= (preceding-char) match)
-                     (error "Mismatched parentheses"))))
-             (forward-char 1)))
+                 (or (eq (preceding-char) (cdr (syntax-after pos)))
+                     (eq (char-after pos) (cdr (syntax-after (1- (point)))))
+                     (error "Mismatched parentheses"))))))
        (error
         (skip-syntax-forward " .>")
         (setq failure-point (point)))))
@@ -1334,7 +1351,9 @@ Mark is left at original location."
       (when (eq (char-after) ?{)
        (let ((newpos (point)))
          (when (ignore-errors (backward-sexp 1) t)
-           (if (looking-at "\\\\end\\>")
+           (if (or (looking-at "\\\\end\\>")
+                   ;; In case the \\ ends a verbatim section.
+                   (and (looking-at "end\\>") (eq (char-before) ?\\)))
                (tex-last-unended-begin)
              (goto-char newpos))))))))
 
@@ -1476,7 +1495,9 @@ Mark is left at original location."
       (make-comint
        "tex-shell"
        (or tex-shell-file-name (getenv "ESHELL") shell-file-name)
-       nil)
+       nil
+       ;; Specify an interactive shell, to make sure it prompts.
+       "-i")
     (let ((proc (get-process "tex-shell")))
       (set-process-sentinel proc 'tex-shell-sentinel)
       (set-process-query-on-exit-flag proc nil)
@@ -1676,9 +1697,12 @@ of the current buffer."
   (let* ((file (or tex-main-file
                   ;; Compatibility with AUCTeX.
                   (with-no-warnings
-                   (when (and (boundp 'TeX-master) (stringp TeX-master))
-                     (make-local-variable 'tex-main-file)
-                     (setq tex-main-file TeX-master)))
+                   (when (boundp 'TeX-master)
+                     (cond ((stringp TeX-master)
+                            (make-local-variable 'tex-main-file)
+                            (setq tex-main-file TeX-master))
+                           ((and (eq TeX-master t) buffer-file-name)
+                            (file-relative-name buffer-file-name)))))
                   ;; Try to guess the main file.
                   (if (not buffer-file-name)
                       (error "Buffer is not associated with any file")
@@ -1752,7 +1776,7 @@ FILE is typically the output DVI or PDF file."
           (when (file-newer-than-file-p f file)
             (setq uptodate nil)))))
      uptodate)))
-    
+
 
 (autoload 'format-spec "format-spec")
 
@@ -1935,7 +1959,6 @@ since TeX does not put file names and line numbers on the same line as
 for the error messages."
   (require 'thingatpt)
   (setq compilation-error-list nil)
-  (message "Parsing error messages...")
   (let ((default-directory             ; Perhaps dir has changed meanwhile.
          (file-name-directory (buffer-file-name tex-last-buffer-texed)))
        found-desired (num-errors-found 0)
@@ -2001,8 +2024,7 @@ for the error messages."
                      compilation-error-list))
          (goto-char end-of-error)))))
   (set-marker compilation-parsing-end (point))
-  (setq compilation-error-list (nreverse compilation-error-list))
-  (message "Parsing error messages...done"))
+  (setq compilation-error-list (nreverse compilation-error-list)))
 \f
 ;;; The commands: