GNU bug report logs - #19356
electric-pair-mode painful quotes in latex-mode

Previous Next

Package: emacs;

Reported by: Stefan Monnier <monnier <at> iro.umontreal.ca>

Date: Fri, 12 Dec 2014 04:21:01 UTC

Severity: minor

Found in version 24.4

Done: joaotavora <at> gmail.com (João Távora)

Bug is archived. No further changes may be made.

Full log


Message #14 received at 19356 <at> debbugs.gnu.org (full text, mbox):

From: joaotavora <at> gmail.com (João Távora)
To: Stefan Monnier <monnier <at> iro.umontreal.ca>
Cc: 19356 <at> debbugs.gnu.org
Subject: Re: bug#19356: electric-pair-mode painful quotes in latex-mode
Date: Sat, 13 Dec 2014 16:42:07 +0000
joaotavora <at> gmail.com (João Távora) writes:

> joaotavora <at> gmail.com (João Távora) writes:
>
>>> Package: Emacs
>>> Version: 24.4
>>>
>>>
>>>    ./src/emacs -Q -f electric-pair-mode ~/tmp/foo.tex
>>>
>>> then type a word, go back to before this word and try:
>>>
>>>    C-M-SPC "
>>>
>>> this will not surround the word in quotes as electric-pair-mode should.
>>
>> What kind of surrounding should take place? Should it be
>>
>>   ``wordityped''
>
> I went with this option, seemed the most sane. Here's my proposed fix
> (for the two issues). Let me know if you find the nested ifs ugly and
> I'll make a cond out of it.
>
> If it seems OK, I'll add some tests and commit it to the emacs-24 branch
> (someone else will cherry-pick it to the master, right?).

Actually, and since I need tex-mode for writing a paper right now (nice
timing on the bug report btw), the fix can be enhanced to imitate more
of `electric-pair-mode'. Patch to current emacs master attached, or find
two commits in the master branch of my github mirror

   https://github.com/capitaomorte/emacs

   84ecd2c Improve on previous commit for tex-mode quotes
   84f5eec Consider electric-pair-mode in tex-mode quotes

João


diff --git a/lisp/textmodes/tex-mode.el b/lisp/textmodes/tex-mode.el
index 1993ff1..3eca70b 100644
--- a/lisp/textmodes/tex-mode.el
+++ b/lisp/textmodes/tex-mode.el
@@ -1300,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 occurance 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 (maybe 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 behaviours: 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 2))
+            (insert tex-open-quote))
+        (if (looking-at (regexp-quote tex-close-quote))
+            (forward-char 2)
+          (insert tex-close-quote))))))
 
 (defun tex-validate-buffer ()
   "Check current buffer for paragraphs containing mismatched braces or $s.





This bug report was last modified 10 years and 162 days ago.

Previous Next


GNU bug tracking system
Copyright (C) 1999 Darren O. Benham, 1997,2003 nCipher Corporation Ltd, 1994-97 Ian Jackson.