Skip to content

Fixes #1974: Correct C-h behavior to ensure electric-pair deletes adjacent pairs#1975

Open
jamescherti wants to merge 1 commit intoemacs-evil:masterfrom
jamescherti:jc-fix-elec-pair-C-h
Open

Fixes #1974: Correct C-h behavior to ensure electric-pair deletes adjacent pairs#1975
jamescherti wants to merge 1 commit intoemacs-evil:masterfrom
jamescherti:jc-fix-elec-pair-C-h

Conversation

@jamescherti
Copy link
Contributor

@jamescherti jamescherti commented Apr 30, 2025

When using electric-pair-mode, the expected behavior when pressing C-h near a pair of adjacent delimiters (e.g., () [] {} "") is that both the opening and closing delimiters should be deleted together if they were inserted as a pair.

This pull request fixes #1974.

This pull request corrects C-h behavior to ensure electric-pair deletes adjacent pairs. It calls the same function as DEL (code 127) in electric-pair-mode-map:

electric-pair-mode-map is a variable defined in ‘elec-pair.el’.

Its value is
(keymap 
 (127 menu-item "" electric-pair-delete-pair :filter
  #f(compiled-function (cmd) #<bytecode 0x115035a9952592b3>)))

This pull request could be improved. Please don't hesitate to comment it if you have any suggestions.

@jamescherti jamescherti force-pushed the jc-fix-elec-pair-C-h branch 2 times, most recently from ef49df9 to cbb8c9a Compare April 30, 2025 15:59
@jamescherti jamescherti force-pushed the jc-fix-elec-pair-C-h branch from def101c to cbb8c9a Compare June 24, 2025 18:21
Copy link
Collaborator

@axelf4 axelf4 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I agree that we should try to be compatible with the shenanigans of electric-pair-mode, even at the cost of a more fragile C-h binding. Some comments:

Will need to update the lookup-key check in the evil-want-C-h-delete setter:

              ((and (not value)
                    (eq (lookup-key evil-insert-state-map (kbd "C-h"))
                        'evil-delete-backward-char-and-join))

@jamescherti jamescherti force-pushed the jc-fix-elec-pair-C-h branch from cbb8c9a to 3b80eb5 Compare August 7, 2025 13:14
@jamescherti
Copy link
Contributor Author

I agree that we should try to be compatible with the shenanigans of electric-pair-mode, even at the cost of a more fragile C-h binding. Some comments:

Will need to update the lookup-key check in the evil-want-C-h-delete setter:

              ((and (not value)
                    (eq (lookup-key evil-insert-state-map (kbd "C-h"))
                        'evil-delete-backward-char-and-join))

Yes, it is less conflusing to see that C-h behaves exactly like backspace (similar to #1980 for eldoc). It does not only fix electric-pair-mode, but also other packages that rely on backspace (e.g., paredit)

I have modified evil-vars.el to use the following instead:

(define-key evil-insert-state-map (kbd "C-h") (kbd "DEL"))

@jamescherti jamescherti force-pushed the jc-fix-elec-pair-C-h branch 2 times, most recently from 385aafa to 4cdb9db Compare September 6, 2025 03:15
@jamescherti
Copy link
Contributor Author

jamescherti commented Sep 6, 2025

I enhanced the evil-insert-state-bindings mapping for C-h by improving the lambda function.

@jamescherti
Copy link
Contributor Author

Have there been any updates on this pull request, @axelf4?

@jamescherti jamescherti force-pushed the jc-fix-elec-pair-C-h branch from 8ecd4f2 to 3b80eb5 Compare March 2, 2026 19:36
@jamescherti
Copy link
Contributor Author

I have applied your (key-binding (kbd "DEL") suggestion.

@jamescherti jamescherti force-pushed the jc-fix-elec-pair-C-h branch 4 times, most recently from 88a3893 to eaf0dc0 Compare March 2, 2026 19:47
@jamescherti
Copy link
Contributor Author

I have enhanced this pull request: I added support for interactive and non-interactive functions

@jamescherti jamescherti force-pushed the jc-fix-elec-pair-C-h branch from eaf0dc0 to 4eae39e Compare March 2, 2026 19:56
@jamescherti
Copy link
Contributor Author

FYI: I simplified the pull request.

@jamescherti jamescherti force-pushed the jc-fix-elec-pair-C-h branch from 4eae39e to 18380b6 Compare March 2, 2026 20:01
@jamescherti
Copy link
Contributor Author

The only drawback to binding (kbd "DEL") directly is that it causes the ElDoc help to disappear from the echo area. Perhaps a function should be used instead, so it can be added to the ElDoc exclusion list.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

C-h does not ensure electric-pair deletes adjacent pairs

2 participants