From f4406ad7fcaaaa342fdf425a2cda0e553388be02 Mon Sep 17 00:00:00 2001 From: Alan Pearce Date: Tue, 10 Dec 2024 19:08:49 +0100 Subject: emacs: make C-g great again --- user/emacs/init.el | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) (limited to 'user/emacs/init.el') diff --git a/user/emacs/init.el b/user/emacs/init.el index 4a4af8dc..01f25b38 100644 --- a/user/emacs/init.el +++ b/user/emacs/init.el @@ -189,6 +189,32 @@ With two prefix arguments, write out the day and month name." ;;; Keybindings +(defun prot/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") #'prot/keyboard-quit-dwim) + (when (eq system-type 'darwin) (setq mac-option-modifier 'meta mac-right-option-modifier 'none -- cgit 1.4.1