Subject: bug#4136: 23.1; delete-pair

From: Juri Linkov <[email-masked]>
Date: Tue, 18 Aug 2009 00:17:11 +0300

>> `insert-pair' inserts an opening character at point.  So why
>> `delete-pair' shouldn't do the same?  Why it shouldn't delete an
>> opening character at point instead of using some additional
>> heuristics to find the position of the opening character?
>
> We're going back in circles here: "delete *pair*" shouldn't delete an
> opening character unless it is part of a *pair*.  If by the above you
> mean make it delete the character pair starting at the current point,
> then that seems fine -- as long as it verifies that there really is a
> pair.

We are in violent agreement.  I didn't claim that `delete-pair'
shouldn't check `insert-pair-alist'.  On the contrary, I think
`delete-pair' should verify if the character pair starting at the
current point is part of a pair according to `insert-pair-alist'.
"At the current point" - that's my point.  It shouldn't try
finding the opening character somewhere else.  So in your original
test case it should throw an error when the cursor is on some
whitespace that precedes an expression.

>> Why would I want to put cursor on a non-paren when I want to delete
>> a paren?
>
> You wouldn't, which is why throwing an error is the right thing.

I agree.  So I'd like to close this issue with the following patch:

Index: lisp/emacs-lisp/lisp.el
===================================================================
RCS file: /sources/emacs/emacs/lisp/emacs-lisp/lisp.el,v
retrieving revision 1.102
diff -u -r1.102 lisp.el
--- lisp/emacs-lisp/lisp.el	22 Jul 2009 02:45:37 -0000	1.102
+++ lisp/emacs-lisp/lisp.el	17 Aug 2009 21:16:32 -0000
@@ -530,7 +530,15 @@
 (defun delete-pair ()
   "Delete a pair of characters enclosing the sexp that follows point."
   (interactive)
-  (save-excursion (forward-sexp 1) (delete-char -1))
+  (let ((open-char (char-after)))
+    (save-excursion
+      (forward-sexp 1)
+      (unless (member (list open-char (char-before))
+		      (mapcar (lambda (p)
+				(if (= (length p) 3) (cdr p) p))
+			      insert-pair-alist))
+	(error "Not a matching pair"))
+      (delete-char -1)))
   (delete-char 1))
 
 (defun raise-sexp (&optional arg)

-- 
Juri Linkov
http://www.jurta.org/emacs/