Subject: bug#32076: 24.5; Electric behavior of dollar sign using AUCTeX

From: João Távora <[email-masked]>
Date: Sun, 08 Jul 2018 15:37:01 +0100

Patrick <[email-masked]> writes:

> See the following StackExchange question: <a
> href="https://emacs.stackexchange.com/questions/42278/electric-behavior-of-dollar-sign-using-auctex">https://emacs.stackexchange.com/questions/42278/electric-behavior-of-dollar-sign-using-auctex</a>
> Reporting as suggested by Joao Tavora.

Thanks Patrick,

In general we want the bug descriptions Emacs bug tracker to be as
self-contained as possible.  So here's the simple way to reproduce this:

    emacs -Q
    M-x tex-mode
    M-x electric-pair-mode
    type $, two dollars correctly appear and point is in the middle
    type something
    type $, again.  Two dollars appear again instead of skipping over
    the dollar.

The reason for this is that `electric-pair-skip-if-helps-balance'
doesn't recognize the $ (paired-delimiter) syntax.  It's easy to open a
door for this in elec-pair.el (see attached partial patch), but Emacs'
built-in tex-mode doesn't have anything to plug into the new
electric-pair-between-paired-delimiters-function var (or does it?)

AUCTeX, on the other hand, has `texmathp' which seems to do nicely.
Should we lift this function into Emacs, or use some other, perhaps more
generic, technique of discovering wether point is between two $$?

João

diff --git a/lisp/elec-pair.el b/lisp/elec-pair.el
index 7df7098295..3040a5be6d 100644
--- a/lisp/elec-pair.el
+++ b/lisp/elec-pair.el
@@ -428,6 +428,9 @@ electric-pair-inhibit-if-helps-balance
                   (electric-pair--unbalanced-strings-p char))))
        (insert-char char)))))
 
+(defvar electric-pair-between-paired-delimiters-function #'ignore
+  "Function returning non-nil if point between paired delimiters.")
+
 (defun electric-pair-skip-if-helps-balance (char)
   "Return non-nil if skipping CHAR would benefit parentheses' balance.
 
@@ -451,7 +454,9 @@ electric-pair-skip-if-helps-balance
                            ((car innermost)
                             (not (eq (cdr outermost) pair)))))))
                  ((eq syntax ?\")
-                  (electric-pair--inside-string-p char))))
+                  (electric-pair--inside-string-p char))
+                 ((eq syntax ?\$)
+                  (funcall electric-pair-between-paired-delimiters-function))))
        (insert-char char)))))
 
 (defun electric-pair-default-skip-self (char)