fix: close minibuffer if open when pressing C-g

This commit is contained in:
Laurens Miers 2025-01-08 21:49:21 +01:00
parent c8fa26b07b
commit 3853d9d247

View file

@ -1161,3 +1161,37 @@ This should normally be done by the init.el to load this configuration.
| M-x untabify/tabify | Convert to spaces/tabs | | M-x untabify/tabify | Convert to spaces/tabs |
| M-x describe-bindings | List all mapped keys/commands | | M-x describe-bindings | List all mapped keys/commands |
| M-q | Fill paragraph | | M-q | Fill paragraph |
** Minibuffer
*** Close minibuffer when pressing C-g
'Inspired' by https://protesilaos.com/codelog/2024-11-28-basic-emacs-configuration/#h:1e4fde73-a2a2-4dc5-82ad-02cf3884ece6 .
#+BEGIN_SRC emacs-lisp
(defun myrmi/keyboard-quit-dwim ()
"Do-What-I-Mean behaviour for a general `keyboard-quit'.
The generic `keyboard-quit' does not do the expected thing when
the minibuffer is open. Whereas we want it to close the
minibuffer, even without explicitly focusing it.
The DWIM behaviour of this command is as follows:
- When the region is active, disable it.
- When a minibuffer is open, but not focused, close the minibuffer.
- When the Completions buffer is selected, close it.
- In every other case use the regular `keyboard-quit'."
(interactive)
(cond
((region-active-p)
(keyboard-quit))
((derived-mode-p 'completion-list-mode)
(delete-completion-window))
((> (minibuffer-depth) 0)
(abort-recursive-edit))
(t
(keyboard-quit))))
(define-key global-map (kbd "C-g") #'myrmi/keyboard-quit-dwim)
#+END_SRC