]> code.delx.au - gnu-emacs/blobdiff - lisp/textmodes/tex-mode.el
Fix byte-compiler warnings about looking-back.
[gnu-emacs] / lisp / textmodes / tex-mode.el
index 0cfc0cfe3dc1831cb9cb375d7500b81da885ed54..c26c6098364e1632e46d4c2de13d2c440ba0048d 100644 (file)
@@ -1,6 +1,6 @@
 ;;; tex-mode.el --- TeX, LaTeX, and SliTeX mode commands -*- coding: utf-8 -*-
 
-;; Copyright (C) 1985-1986, 1989, 1992, 1994-1999, 2001-2014 Free
+;; Copyright (C) 1985-1986, 1989, 1992, 1994-1999, 2001-2015 Free
 ;; Software Foundation, Inc.
 
 ;; Maintainer: emacs-devel@gnu.org
@@ -1204,12 +1204,31 @@ Entering SliTeX mode runs the hook `text-mode-hook', then the hook
   (setq tex-start-of-header "\\\\documentstyle{slides}\\|\\\\documentclass{slides}"))
 
 (defvar tildify-space-string)
+(defvar tildify-foreach-region-function)
 
 (defun tex-common-initialization ()
   ;; Regexp isearch should accept newline and formfeed as whitespace.
   (setq-local search-whitespace-regexp "[ \t\r\n\f]+")
   ;; Use tilde as hard-space character in tildify package.
   (setq-local tildify-space-string "~")
+  ;; FIXME: Use the fact that we're parsing the document already
+  ;; rather than using regex-based filtering.
+  (setq-local tildify-foreach-region-function
+              (apply-partially
+               'tildify-foreach-ignore-environments
+               `(("\\\\\\\\" . "") ; do not remove this
+                 (,(eval-when-compile
+                     (concat "\\\\begin{\\("
+                             (regexp-opt '("verbatim" "math" "displaymath"
+                                           "equation" "eqnarray" "eqnarray*"))
+                             "\\)}"))
+                  . ("\\\\end{" 1 "}"))
+                 ("\\\\verb\\*?\\(.\\)" . (1))
+                 ("\\$\\$?" . (0))
+                 ("\\\\(" . "\\\\)")
+                 ("\\\\[[]" . "\\\\[]]")
+                 ("\\\\[a-zA-Z]+\\( +\\|{}\\)[a-zA-Z]*" . "")
+                 ("%" . "$"))))
   ;; A line containing just $$ is treated as a paragraph separator.
   (setq-local paragraph-start "[ \t]*$\\|[\f\\\\%]\\|[ \t]*\\$\\$")
   ;; A line starting with $$ starts a paragraph,
@@ -1281,18 +1300,48 @@ 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")
+  ;; Discover if we'll be inserting normal double quotes.
+  ;;
   (if (or arg (memq (char-syntax (preceding-char)) '(?/ ?\\))
-         (eq (get-text-property (point) 'face) 'tex-verbatim)
-         (save-excursion
-           (backward-char (length tex-open-quote))
-           (when (or (looking-at (regexp-quote tex-open-quote))
-                     (looking-at (regexp-quote tex-close-quote)))
-             (delete-char (length tex-open-quote))
-             t)))
+          (eq (get-text-property (point) 'face) 'tex-verbatim)
+          ;; Discover if a preceding occurrence of `tex-open-quote'
+          ;; should be morphed to a normal double quote.
+          ;;
+          (and (>= (point) (+ (point-min) (length tex-open-quote)))
+               (save-excursion
+                 (backward-char (length tex-open-quote))
+                 (when (or (looking-at (regexp-quote tex-open-quote))
+                           (looking-at (regexp-quote tex-close-quote)))
+                   (delete-char (length tex-open-quote))
+                   (when (looking-at (regexp-quote tex-close-quote))
+                     (delete-char (length tex-close-quote)))
+                   t))))
+      ;; Insert the normal quote (eventually letting
+      ;; `electric-pair-mode' do its thing).
+      ;;
       (self-insert-command (prefix-numeric-value arg))
-    (insert (if (or (memq (char-syntax (preceding-char)) '(?\( ?> ?\s))
-                    (memq (preceding-char) '(?~)))
-               tex-open-quote tex-close-quote))))
+    ;; We'll be inserting fancy TeX quotes, but consider and imitate
+    ;; `electric-pair-mode''s two behaviors: pair-insertion and
+    ;; region wrapping.
+    ;;
+    (if (and electric-pair-mode (use-region-p))
+        (let* ((saved (point-marker)))
+          (goto-char (mark))
+          (insert (if (> saved (mark)) tex-open-quote tex-close-quote))
+          (goto-char saved)
+          (insert (if (> saved (mark)) tex-close-quote tex-open-quote)))
+      (if (or (memq (char-syntax (preceding-char)) '(?\( ?> ?\s))
+              (memq (preceding-char) '(?~)))
+          (if electric-pair-mode
+              (if (looking-at (regexp-quote tex-close-quote))
+                  (forward-char (length tex-close-quote))
+                (insert tex-open-quote)
+                (insert tex-close-quote)
+                (backward-char (length tex-close-quote)))
+            (insert tex-open-quote))
+        (if (looking-at (regexp-quote tex-close-quote))
+            (forward-char (length tex-close-quote))
+          (insert tex-close-quote))))))
 
 (defun tex-validate-buffer ()
   "Check current buffer for paragraphs containing mismatched braces or $s.
@@ -1712,13 +1761,13 @@ Mark is left at original location."
        ;; A better way to handle this, \( .. \) etc, is probably to
        ;; temporarily change the syntax of the \ in \( to punctuation.
        ((and latex-handle-escaped-parens
-            (looking-back "\\\\[])}]"))
+            (looking-back "\\\\[])}]" (- (point) 2)))
        (signal 'scan-error
                (list "Containing expression ends prematurely"
                      (- (point) 2) (prog1 (point)
                                      (goto-char pos)))))
        ((and latex-handle-escaped-parens
-            (looking-back "\\\\\\([({[]\\)"))
+            (looking-back "\\\\\\([({[]\\)" (- (point) 2)))
        (tex-next-unmatched-eparen (match-string 1)))
        (t (goto-char newpos))))))