bug-gnu-emacs
[Top][All Lists]
Advanced

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

bug#19356: electric-pair-mode painful quotes in latex-mode


From: João Távora
Subject: bug#19356: electric-pair-mode painful quotes in latex-mode
Date: Sat, 13 Dec 2014 16:42:07 +0000
User-agent: Gnus/5.13 (Gnus v5.13) Emacs/25.0.50 (darwin)

joaotavora@gmail.com (João Távora) writes:

> joaotavora@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.






reply via email to

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