Subject: bug#21275: 24.5; Selection deleted with electric pair mode in cc mode

From: Alan Mackenzie <[email-masked]>
Date: 18 Aug 2015 15:04:31 -0000

Hello, N.

In article <[email-masked]> you wrote:
> In cc mode, with delete selection mode and electric pair mode both on,
> entering a "(" when there is an active selection erroneously deletes the
> selection (and inserts "()" in the buffer) rather than inserting a "("
> before the selection and a ")" after it. This is inconsistent with the
> seemingly correct [1] and useful behaviour in (for example) text mode,
> fundamental mode, and lisp mode.

> The bug affects both code and in comments.

And it also affects braces as well as parens.

Thanks for taking the trouble to report this bug, and thanks even more
for making your report so clear and well structured.

> STEPS TO REPRODUCE:

> $ cat > test.c
> /* This is a short comment. */
> int test(void){
>   return 0;
> }
> ^D
> $ emacs -Q
> M-x delete-selection-mode
> M-x electric-pair-mode
> C-x f test.c RET
> ;; Select the word "short" in the first line.
> (
> ;; Now select the "0" in the return statement.
> (

> EXPECTED RESULT:

> /* This is a (short) comment. */
> int test(void){
>   return (0);
> }

> OBSERVED RESULT:

> /* This is a () comment. */
> int test(void){
>   return ();
> }

> [1] http://debbugs.gnu.org/cgi/bugreport.cgi?bug=11520

> In GNU Emacs 24.5.1 (x86_64-unknown-linux-gnu, GTK+ Version 3.14.14)
>  of 2015-07-25 on moondust
> Windowing system distributor `Fedora Project', version 11.0.11603000
> System Description:     Fedora release 21 (Twenty One)

I think the following patch should fix it.  To apply it, go to the
directory ..../emacs-24.5/lisp and apply the patch from there.  If `patch'
complains at not finding the second file, and prompts you for it, this
will be .../emacs-24.5/lisp/progmodes/cc-cmds.el.  After byte-compiling
both files (either with M-x byte-compile-file from inside emacs, or with
$ emacs -Q -batch -f batch-byte-compile delsel.el progmodes/cc-cmds.el
from a command line), just load the two files.elc into Emacs with
M-x load-file (or restart Emacs).

Would you please confirm that the problem has been fixed, or tell us what
is still not working.  Thanks in advance!

Here's the patch:

diff --git a/lisp/delsel.el b/lisp/delsel.el
index 740b603..6a77d75 100644
--- a/lisp/delsel.el
+++ b/lisp/delsel.el
@@ -231,10 +231,15 @@ See `delete-selection-helper'."
     (delete-selection-helper (and (symbolp this-command)
                                   (get this-command 'delete-selection)))))
 
-(put 'self-insert-command 'delete-selection
-     (lambda ()
-       (not (run-hook-with-args-until-success
-             'self-insert-uses-region-functions))))
+(defun delete-selection-uses-region-p ()
+  "Return non-nil when the current command uses the region.
+This function is intended for use as the value of the
+  `delete-selection' property of a command, and shouldn't be used for
+  anything else."
+  (not (run-hook-with-args-until-success
+        'self-insert-uses-region-functions)))
+
+(put 'self-insert-command 'delete-selection 'delete-selection-uses-region-p)
 
 (put 'insert-char 'delete-selection t)
 (put 'quoted-insert 'delete-selection t)
diff --git a/lisp/progmodes/cc-cmds.el b/lisp/progmodes/cc-cmds.el
index c9f5945..40ec4bd 100644
--- a/lisp/progmodes/cc-cmds.el
+++ b/lisp/progmodes/cc-cmds.el
@@ -2860,13 +2860,18 @@ sentence motion in or near comments and multiline strings."
     (put sym 'delete-selection t)	; for delsel (Emacs)
     (put sym 'pending-delete t)))	; for pending-del (XEmacs)
  '(c-electric-pound
-   c-electric-brace
+;  c-electric-brace
    c-electric-slash
    c-electric-star
    c-electric-semi&comma
    c-electric-lt-gt
    c-electric-colon
-   c-electric-paren))
+;  c-electric-paren
+   ))
+(put 'c-electric-brace 'delete-selection 'delete-selection-uses-region-p)
+(put 'c-electric-brace 'pending-delete 't)
+(put 'c-electric-paren 'delete-selection 'delete-selection-uses-region-p)
+(put 'c-electric-paren 'pending-delete 't)
 (put 'c-electric-delete    'delete-selection 'supersede) ; delsel
 (put 'c-electric-delete    'pending-delete   'supersede) ; pending-del
 (put 'c-electric-backspace 'delete-selection 'supersede) ; delsel

-- 
Alan Mackenzie (Nuremberg, Germany).