Subject: bug#14000: closed (Re: bug#14000: 24.3.50; electric-pair-post-self-insert-function does not handle nested parentheses)

From: [email-masked] (GNU bug Tracking System)
Date: Mon, 01 Apr 2013 13:33:07 +0000

Your bug report

#14000: 24.3.50; electric-pair-post-self-insert-function does not handle nested parentheses

which was filed against the emacs package, has been closed.

The explanation is attached below, along with your original report.
If you require more details, please reply to [email-masked].

-- 
14000: http://debbugs.gnu.org/cgi/bugreport.cgi?bug=14000
GNU Bug Tracking System
Contact [email-masked] with problems

Subject: Re: bug#14000: 24.3.50; electric-pair-post-self-insert-function does not handle nested parentheses
From: Stefan Monnier <[email-masked]>
To: Carsten Bormann <[email-masked]>
Cc: [email-masked]
Date: Mon, 01 Apr 2013 09:28:56 -0400
List-Id: <debbugs-submit.debbugs.gnu.org>

> OK, I maybe should have suggested adding a preference.
> But if you live in a world of paired parentheses (and I thought
> Emacs-Lispers were!) you want to add them in pairs, always.

I installed the patch below,

        Stefan

=== modified file 'lisp/electric.el'
--- lisp/electric.el	2013-01-01 09:11:05 +0000
+++ lisp/electric.el	2013-04-01 13:27:16 +0000
@@ -302,6 +302,26 @@
   :version "24.1"
   :type 'boolean)
 
+(defcustom electric-pair-inhibit-predicate
+  #'electric-pair-default-inhibit
+  "Predicate to prevent insertion of a matching pair.
+The function is called with a single char (the opening char just inserted).
+If it returns non-nil, then `electric-pair-mode' will not insert a matching
+closer."
+  :type '(choice
+          (const :tag "Default" electric-pair-default-inhibit)
+          (const :tag "Always pair" ignore)
+          function))
+
+(defun electric-pair-default-inhibit (char)
+  (or
+   ;; I find it more often preferable not to pair when the
+   ;; same char is next.
+   (eq char (char-after))
+   (eq char (char-before (1- (point))))
+   ;; I also find it often preferable not to pair next to a word.
+   (eq (char-syntax (following-char)) ?w)))
+
 (defun electric-pair-syntax (command-event)
   (and electric-pair-mode
        (let ((x (assq command-event electric-pair-pairs)))
@@ -351,12 +371,7 @@
      ;; Insert matching pair.
      ((not (or (not (memq syntax `(?\( ?\" ?\$)))
                overwrite-mode
-               ;; I find it more often preferable not to pair when the
-               ;; same char is next.
-               (eq last-command-event (char-after))
-               (eq last-command-event (char-before (1- (point))))
-               ;; I also find it often preferable not to pair next to a word.
-               (eq (char-syntax (following-char)) ?w)))
+               (funcall electric-pair-inhibit-predicate last-command-event)))
       (save-excursion (insert closer))))))
 
 (defun electric-pair-will-use-region ()

Subject: 24.3.50; electric-pair-post-self-insert-function does not handle nested parentheses
From: Carsten Bormann <[email-masked]>
To: [email-masked]
Date: Tue, 19 Mar 2013 18:59:53 +0100
List-Id: <debbugs-submit.debbugs.gnu.org>

In electric-pair-mode, I type "(("

I get "(()"

I should get "(())"

Fix below.

In GNU Emacs 24.3.50.1 (x86_64-apple-darwin, NS apple-appkit-1038.36)
 of 2013-03-19 on bob.porkrind.org
Bzr revision: 112090 [email-masked]
Windowing system distributor `Apple', version 10.3.1187
Configured using:
 `configure --host=x86_64-apple-darwin --build=i686-apple-darwin
 --with-ns'

--- electric.el	2013-03-19 18:51:05.000000000 +0100
+++ electric-fixed.el	2013-03-19 18:55:59.000000000 +0100
@@ -354,7 +354,10 @@
                ;; I find it more often preferable not to pair when the
                ;; same char is next.
                (eq last-command-event (char-after))
-               (eq last-command-event (char-before (1- (point))))
+               (and
+                (eq last-command-event (char-before (1- (point))))
+                (not (eq syntax ?\())
+               )
                ;; I also find it often preferable not to pair next to a word.
                (eq (char-syntax (following-char)) ?w)))
       (save-excursion (insert closer))))))