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.

To add a comment to this bug, you must first unarchive it, by sending
a message to control AT debbugs.gnu.org, with unarchive 19356 in the body.
You can then email your comments to 19356 AT debbugs.gnu.org in the normal way.

Toggle the display of automated, internal messages from the tracker.

View this report as an mbox folder, status mbox, maintainer mbox


Report forwarded to joaotavora <at> gmail.com, bug-gnu-emacs <at> gnu.org:
bug#19356; Package emacs. (Fri, 12 Dec 2014 04:21:02 GMT) Full text and rfc822 format available.

Acknowledgement sent to Stefan Monnier <monnier <at> iro.umontreal.ca>:
New bug report received and forwarded. Copy sent to joaotavora <at> gmail.com, bug-gnu-emacs <at> gnu.org. (Fri, 12 Dec 2014 04:21:02 GMT) Full text and rfc822 format available.

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

From: Stefan Monnier <monnier <at> iro.umontreal.ca>
To: bug-gnu-emacs <at> gnu.org
Subject: electric-pair-mode painful quotes in latex-mode
Date: Thu, 11 Dec 2014 21:57:05 -0500
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.

Similarly, type

   hello " "

Notice how the second " inserts `""' instead of just modifying the ''
into a single ".  This same problem happens before a word.


        Stefan




In GNU Emacs 25.0.50.1 (x86_64-unknown-linux-gnu, X toolkit, Xaw3d scroll bars)
 of 2014-12-10 on pastel
Repository revision: 30075cf7025c17542f23610de1b54d84bbde192b
Windowing system distributor `The X.Org Foundation', version 11.0.11601901
System Description:	Debian GNU/Linux 8.0 (jessie)

Configured using:
 `configure -C --enable-checking --enable-check-lisp-object-type
 'CFLAGS=-Wall -g3 -Og -Wno-pointer-sign'
 PKG_CONFIG_PATH=/home/monnier/lib/pkgconfig'

Configured features:
XAW3D XPM JPEG TIFF GIF PNG SOUND GPM DBUS NOTIFY GNUTLS LIBXML2
FREETYPE M17N_FLT LIBOTF XFT ZLIB

Important settings:
  value of $LANG: fr_CH.UTF-8
  locale-coding-system: utf-8-unix

Major mode: InactiveMinibuffer

Minor modes in effect:
  electric-pair-mode: t
  url-handler-mode: t
  global-reveal-mode: t
  reveal-mode: t
  auto-insert-mode: t
  savehist-mode: t
  minibuffer-electric-default-mode: t
  global-eldoc-mode: t
  electric-indent-mode: t
  mouse-wheel-mode: t
  global-prettify-symbols-mode: t
  menu-bar-mode: t
  file-name-shadow-mode: t
  global-font-lock-mode: t
  auto-composition-mode: t
  auto-encryption-mode: t
  auto-compression-mode: t
  line-number-mode: t

Recent messages:
Loading /home/monnier/src/elisp/ProofGeneral/generic/proof-site.el (source)...done
Loading /home/monnier/etc/emacs/X11.el (source)...done
Loading /home/monnier/etc/emacs/custom.el (source)...done
Warning: turn-on-eldoc-mode is obsolete!
Ispell-kill: nil american
Starting new Ispell process /usr/bin/aspell with american dictionary...
For information about GNU Emacs and the GNU system, type C-h C-a.
<C-drag-mouse-2> is undefined [2 times]
Quit
funcall-interactively: End of buffer

Load-path shadows:
/home/monnier/src/emacs/elpa/packages/ada-mode/ada-ref-man hides /home/monnier/src/emacs/elpa/packages/ada-ref-man/ada-ref-man
/home/monnier/src/emacs/elpa/packages/ada-mode/ada-prj hides /home/monnier/src/emacs/work/lisp/progmodes/ada-prj
/home/monnier/src/emacs/elpa/packages/ada-mode/ada-stmt hides /home/monnier/src/emacs/work/lisp/progmodes/ada-stmt
/home/monnier/src/emacs/elpa/packages/ada-mode/ada-mode hides /home/monnier/src/emacs/work/lisp/progmodes/ada-mode
/home/monnier/src/emacs/elpa/packages/ada-mode/ada-xref hides /home/monnier/src/emacs/work/lisp/progmodes/ada-xref
/home/monnier/src/emacs/elpa/packages/crisp/crisp hides /home/monnier/src/emacs/work/lisp/obsolete/crisp

Features:
(shadow sort mail-extr emacsbug message dired format-spec rfc822 mml
mml-sec mm-decode mm-bodies mm-encode mail-parse rfc2231 mailabbrev
gmm-utils mailheader sendmail rfc2047 rfc2045 ietf-drums mail-utils
server noutline outline easy-mmode flyspell ispell checkdoc thingatpt
help-mode load-dir elec-pair url-handlers url-parse auth-source eieio
byte-opt bytecomp byte-compile cl-extra cconv eieio-core gnus-util
time-date mm-util mail-prsvr password-cache url-vars reveal autoinsert
proof-site proof-autoloads cl-macs cl gv pg-vars savehist minibuf-eldef
disp-table finder-inf edmacro kmacro cl-loaddefs cl-lib advice help-fns
info easymenu package epg-config bbdb-autoloads agda2 vm-autoloads
tooltip eldoc electric uniquify ediff-hook vc-hooks lisp-float-type
mwheel x-win x-dnd tool-bar dnd fontset image regexp-opt fringe
tabulated-list newcomment elisp-mode lisp-mode prog-mode register page
menu-bar rfn-eshadow timer select scroll-bar mouse jit-lock font-lock
syntax font-core frame cham georgian utf-8-lang misc-lang vietnamese
tibetan thai tai-viet lao korean japanese hebrew greek romanian slovak
czech european ethiopic indian cyrillic chinese case-table epa-hook
jka-cmpr-hook help simple abbrev minibuffer nadvice loaddefs button
faces cus-face macroexp files text-properties overlay sha1 md5 base64
format env code-pages mule custom widget hashtable-print-readable
backquote make-network-process dbusbind inotify dynamic-setting
font-render-setting x-toolkit x multi-tty emacs)

Memory information:
((conses 8 127506 20873)
 (symbols 24 21454 0) (miscs 20 1455 173) (strings 16 36084 3039)
 (string-bytes 1 1186490)
 (vectors 8 17258) (vector-slots 4 478789 3036) (floats 8 107 266)
 (intervals 28 285 0)
 (buffers 520 11) (heap 1024 8693 640))




Information forwarded to bug-gnu-emacs <at> gnu.org:
bug#19356; Package emacs. (Sat, 13 Dec 2014 14:55:02 GMT) Full text and rfc822 format available.

Message #8 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 14:54:02 +0000
> 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''

or should it be

  "wordityped"

?

Anyway, without having anlysed the problem thoroughly, I lean towards
making tex-mode.el's `tex-insert-quote' aware of `electric-pair-mode',
possibly downgrading and/or not respecting stuff like
`electric-pair-preserve-balance', instead of overloading the latter with
abstractions to deal with multi-character metamorphosing quotes. Do you
agree? Or do you have any other solution in mind?

Also, independent of `electric-pair-mode', typing a double quote
(`tex-insert-quote') with only whitespace preceding point gives me a
"Beginning of buffer" error. Should I also fix it?

>
> Similarly, type
>
>    hello " "
>
> Notice how the second " inserts `""' instead of just modifying the ''
> into a single ".  This same problem happens before a word.

Reproduced. `electric-pair-mode' does affect this.

João




Information forwarded to bug-gnu-emacs <at> gnu.org:
bug#19356; Package emacs. (Sat, 13 Dec 2014 15:48:01 GMT) Full text and rfc822 format available.

Message #11 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 15:47:32 +0000
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?).

João

diff --git a/lisp/textmodes/tex-mode.el b/lisp/textmodes/tex-mode.el
index 1993ff1..f2d8e66 100644
--- a/lisp/textmodes/tex-mode.el
+++ b/lisp/textmodes/tex-mode.el
@@ -1300,18 +1300,40 @@ 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")
-  (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)))
-      (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))))
+  (let ((morph-to-normal-p nil))
+    ;; 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)
+            ;; 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))
+                     (setq morph-to-normal-p t)))))
+        ;; In case morphing occured, be sure to turn off
+        ;; `electric-pair-mode' iff it was on. Otherwise let it do
+        ;; its thing.
+        ;;
+        (let ((electric-pair-mode (and electric-pair-mode
+                                       (not morph-to-normal-p))))
+          (self-insert-command (prefix-numeric-value arg)))
+      ;; We'll be inserting fancy TeX quotes, but consider and
+      ;; imitate `electric-pair-mode''s 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)))
+        (insert (if (or (memq (char-syntax (preceding-char)) '(?\( ?> ?\s))
+                        (memq (preceding-char) '(?~)))
+                    tex-open-quote tex-close-quote))))))
 
 (defun tex-validate-buffer ()
   "Check current buffer for paragraphs containing mismatched braces or $s.





Information forwarded to bug-gnu-emacs <at> gnu.org:
bug#19356; Package emacs. (Sat, 13 Dec 2014 16:43:02 GMT) Full text and rfc822 format available.

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.





Information forwarded to bug-gnu-emacs <at> gnu.org:
bug#19356; Package emacs. (Sun, 14 Dec 2014 04:54:02 GMT) Full text and rfc822 format available.

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

From: Stefan Monnier <monnier <at> iro.umontreal.ca>
To: joaotavora <at> gmail.com (João Távora)
Cc: 19356 <at> debbugs.gnu.org
Subject: Re: bug#19356: electric-pair-mode painful quotes in latex-mode
Date: Sat, 13 Dec 2014 23:53:06 -0500
> What kind of surrounding should take place? Should it be
>   ``wordityped''
> or should it be
>   "wordityped"
> ?

I think either way would be OK.
I guess ideally

   C-M-SPC "

should do

   ``wordityped''

and

   C-M-SPC " "

should do

   "wordityped"

but it's OK if we don't reach this ideal.

> Anyway, without having anlysed the problem thoroughly, I lean towards
> making tex-mode.el's `tex-insert-quote' aware of `electric-pair-mode',
> possibly downgrading and/or not respecting stuff like
> `electric-pair-preserve-balance', instead of overloading the latter with
> abstractions to deal with multi-character metamorphosing quotes.  Do you
> agree? Or do you have any other solution in mind?

I don't see the need to extend electric-pair's code for this use
case, indeed, except if it can be done with a well-placed hook.

> Also, independent of `electric-pair-mode', typing a double quote
> (`tex-insert-quote') with only whitespace preceding point gives me a
> "Beginning of buffer" error.  Should I also fix it?

I wouldn't punish you for 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?).

The patch looks OK.  Installing it on emacs-24 sounds good, thank you
(and no, it won't be cherry picked: it'll be merged instead).


        Stefan




Reply sent to joaotavora <at> gmail.com (João Távora):
You have taken responsibility. (Sun, 14 Dec 2014 11:44:02 GMT) Full text and rfc822 format available.

Notification sent to Stefan Monnier <monnier <at> iro.umontreal.ca>:
bug acknowledged by developer. (Sun, 14 Dec 2014 11:44:03 GMT) Full text and rfc822 format available.

Message #22 received at 19356-done <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-done <at> debbugs.gnu.org
Subject: Re: bug#19356: electric-pair-mode painful quotes in latex-mode
Date: Sun, 14 Dec 2014 11:43:09 +0000
Stefan Monnier <monnier <at> iro.umontreal.ca> writes:

>> What kind of surrounding should take place? Should it be
>>   ``wordityped''
>> or should it be
>>   "wordityped"
>> ?
>
> I think either way would be OK.
> I guess ideally
>
>    C-M-SPC "
>
> should do
>
>    ``wordityped''
>
> and
>
>    C-M-SPC " "
>
> should do
>
>    "wordityped"
>
> but it's OK if we don't reach this ideal.

Not sure If I agree that's ideal, but I'll try to implement it for and
use in my tex-mode this week.

> I don't see the need to extend electric-pair's code for this use
> case, indeed, except if it can be done with a well-placed hook.

I don't think it can. Not without heroic sophistication.

> The patch looks OK.  Installing it on emacs-24 sounds good, thank you

Done. Hope I haven't forgotten anything...

> (and no, it won't be cherry picked: it'll be merged instead).

So this very same commit to emacs-24 (and all other commits) will be an
ancestor of master. OK, that works, I suppose. I hear talk (and
misunderstandings) of rebasing in emacs-devel, which is basically
cherry-picking, and that's why I asked. I probably misunderstood (and
sure as hell don't want to fuel that discusssion).

J





Information forwarded to bug-gnu-emacs <at> gnu.org:
bug#19356; Package emacs. (Sun, 14 Dec 2014 14:06:01 GMT) Full text and rfc822 format available.

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

From: Stefan Monnier <monnier <at> iro.umontreal.ca>
To: joaotavora <at> gmail.com (João Távora)
Cc: 19356-done <at> debbugs.gnu.org
Subject: Re: bug#19356: electric-pair-mode painful quotes in latex-mode
Date: Sun, 14 Dec 2014 08:56:51 -0500
> So this very same commit to emacs-24 (and all other commits) will be an
> ancestor of master.

Yes.  Cherry-picking is used when going in the other direction (when
backporting changes from master to emacs-24).


        Stefan




bug archived. Request was from Debbugs Internal Request <help-debbugs <at> gnu.org> to internal_control <at> debbugs.gnu.org. (Mon, 12 Jan 2015 12:24:04 GMT) Full text and rfc822 format available.

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.