auctex-devel
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [AUCTeX-devel] Wrap dollars around active region


From: Tassilo Horn
Subject: Re: [AUCTeX-devel] Wrap dollars around active region
Date: Wed, 05 Jun 2013 16:47:31 +0200
User-agent: Gnus/5.130008 (Ma Gnus v0.8) Emacs/24.3.50 (gnu/linux)

Mosè Giordano <address@hidden> writes:

>> The attached refined version of your patch uses "\\(?:.\\|\n\\)" in
>> place of "." to match also newlines.  It seems to work, but I didn't
>> do extensive testing.
> Perhaps you sent the wrong file, this is the same as my patch :-)

Ups, and in the meantime I've "git reset --hard origin/master".  But it
was easy to reproduce, but keep in mind that the attached version is
even less tested (not at all!) than the version I've meant to send you
before.

Bye,
Tassilo
diff --git a/tex.el b/tex.el
index af57c9b..001ac92 100644
--- a/tex.el
+++ b/tex.el
@@ -5153,24 +5153,16 @@ See also `TeX-font-replace' and 
`TeX-font-replace-function'."
   :group 'TeX-macro
   :type 'boolean)
 
-(defcustom TeX-math-close-double-dollar nil
-  "If non-nil close double dollar math by typing a single `$'."
+(defcustom TeX-electric-math nil
+  ""
   :group 'TeX-macro
-  :type 'boolean)
-
-(defcustom TeX-math-close-single-dollar nil
-  "If non-nil, when outside math mode insert opening and closing dollar
-signs for TeX inline equation and put the point between them, just by
-typing a single `$'."
-  :group 'TeX-macro
-  :type 'boolean)
-
-(defcustom TeX-electric-dollar nil
-  "When outside math mode, if non-nil and there is an active
-region, typing `$' will put a pair of single dollar around it and
-leave point after the closing dollar."
-  :group 'TeX-macro
-  :type 'boolean)
+  :type '(choice (const nil)
+                (const :tag )
+                (const :tag "$...$" '("$" . "$"))
+                (const :tag "\\(...\\)" '("\\(" . "\\)"))
+                (cons :tag "Other"
+                      (string :tag "Insert before point")
+                      (string :tag "Insert after point"))))
 
 (defun TeX-insert-dollar (&optional arg)
   "Insert dollar sign.
@@ -5201,9 +5193,13 @@ sign.  With optional ARG, insert that many dollar signs."
             (string-equal (substring (car texmathp-why) 0 1) "\$"))
        ;; Math mode was turned on with $ or $$ - so finish it accordingly.
        (progn
-         (if TeX-math-close-double-dollar
-             (insert (car texmathp-why))
-           (insert "$"))
+         (insert "$")
+         ;; Compatibility, `TeX-math-close-double-dollar' has been removed
+         ;; after AUCTeX 11.87.
+         (if (boundp 'TeX-math-close-double-dollar)
+             (message
+              (concat "`TeX-math-close-double-dollar' has been removed,"
+                      "\nplease use `TeX-electric-math' instead.")))
          (when (and blink-matching-paren
                     (or (string= (car texmathp-why) "$")
                         (zerop (mod (save-excursion
@@ -5221,23 +5217,45 @@ sign.  With optional ARG, insert that many dollar 
signs."
       (insert "$")))
    (t
     ;; Just somewhere in the text.
-    (if (and TeX-electric-dollar (TeX-active-mark))
-       (progn
-         (if (> (point) (mark))
-             (exchange-point-and-mark))
-         (insert "$")
+    (cond
+     ((and TeX-electric-math (TeX-active-mark))
+      (if (> (point) (mark))
+         (exchange-point-and-mark))
+      ;; Keep the region active.
+      (let ((deactivate-mark nil))
+       (cond
+        ;; $...$ to $$...$$
+        ((and (eq last-command 'TeX-insert-dollar)
+              (re-search-forward "\\$\\([^$]\\(?:.\\|\n\\)*[^$]\\)\\$" (mark) 
t))
+         (replace-match "$$\\1$$")
+         (push-mark (match-beginning 0) t))
+        ;; \(...\) to \[..\]
+        ((and (eq last-command 'TeX-insert-dollar)
+              (re-search-forward "\\\\(\\(\\(?:.\\|\n\\)*\\)\\\\)" (mark) t))
+         (replace-match "\\\\[\\1\\\\]")
+         (push-mark (match-beginning 0) t))
+        ;; Remove \[...\] or $$...$$
+        ((and (eq last-command 'TeX-insert-dollar)
+              (or (re-search-forward 
"\\$\\$\\([^$]\\(?:.\\|\n\\)*[^$]\\)\\$\\$" (mark) t)
+                  (re-search-forward "\\\\\\[\\(\\(?:.\\|\n\\)*\\)\\\\\\]" 
(mark) t)))
+         (replace-match "\\1")
+         (push-mark (match-beginning 0) t))
+        (t
+         ;; We use `save-excursion' because point must be situated before
+         ;; opening symbol.
+         (save-excursion (insert (car TeX-electric-math)))
          (exchange-point-and-mark)
-         (insert "$"))
-      (if TeX-math-close-single-dollar
+         (insert (cdr TeX-electric-math))))))
+     (TeX-electric-math
+      (insert (car TeX-electric-math))
+      (save-excursion (insert (cdr TeX-electric-math)))
+      (if blink-matching-paren
          (progn
-           (insert "$$")
-           (if blink-matching-paren
-               (progn
-                 (backward-char 2)
-                 (sit-for blink-matching-delay)
-                 (forward-char))
-             (backward-char)))
-       (insert "$")))))
+           (backward-char)
+           (sit-for blink-matching-delay)
+           (forward-char))))
+     ;; In any other case just insert a single $.
+     ((insert "$")))))
   (TeX-math-input-method-off))
 
 (defvar TeX-math-input-method-off-regexp

reply via email to

[Prev in Thread] Current Thread [Next in Thread]