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 08:29:46 +0200
User-agent: Gnus/5.130008 (Ma Gnus v0.8) Emacs/24.3.50 (gnu/linux)

Mosè Giordano <address@hidden> writes:

Hi Mosè,

> Ok, that's fine with me.  The attached patch does all you suggested.

Wow, really nifty. :-)

> There are a couple of problems
> - the cycling through "inline math" → "display math" → "no math" works
> only if marked region stays in a single line because, in regexps, `.'
> doesn't match newline.  Suggestions?

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.

> - as far as I can see, keeping region active doesn't work with Xemacs.
>  In Emacs it suffices to bind `deactivate-mark' to nil, but this
> variable isn't present in Xemacs (at least in 21.4.22).  Looking at
> code of f90-mode I've found this solution
> (http://bzr.savannah.gnu.org/lh/emacs/trunk/annotate/head:/lisp/progmodes/f90.el#L1828)
>     (if (featurep 'xemacs)
>         (zmacs-activate-region)
>       (setq mark-active t
>             deactivate-mark nil))
> Can `zmacs-activate-region' work?

I did some very brief testing, and yes, it might do the trick.

Bye,
Tassilo
diff --git a/tex.el b/tex.el
index af57c9b..f5666ae 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 "\\$\\([^$].*[^$]\\)\\$" (mark) t))
+         (replace-match "$$\\1$$")
+         (push-mark (match-beginning 0) t))
+        ;; \(...\) to \[..\]
+        ((and (eq last-command 'TeX-insert-dollar)
+              (re-search-forward "\\\\(\\(.*\\)\\\\)" (mark) t))
+         (replace-match "\\\\[\\1\\\\]")
+         (push-mark (match-beginning 0) t))
+        ;; Remove \[...\] or $$...$$
+        ((and (eq last-command 'TeX-insert-dollar)
+              (or (re-search-forward "\\$\\$\\([^$].*[^$]\\)\\$\\$" (mark) t)
+                  (re-search-forward "\\\\\\[\\(.*\\)\\\\\\]" (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]