summary refs log tree commit diff stats
path: root/user/emacs/init.el
diff options
context:
space:
mode:
authorAlan Pearce2024-12-10 19:08:49 +0100
committerAlan Pearce2024-12-10 19:08:49 +0100
commitf4406ad7fcaaaa342fdf425a2cda0e553388be02 (patch)
tree1059b17f074cce07e9df1027d38f395114b31592 /user/emacs/init.el
parentab763c32f561174f24feca16abd57e2eaafff60e (diff)
downloadnixfiles-f4406ad7fcaaaa342fdf425a2cda0e553388be02.tar.lz
nixfiles-f4406ad7fcaaaa342fdf425a2cda0e553388be02.tar.zst
nixfiles-f4406ad7fcaaaa342fdf425a2cda0e553388be02.zip
emacs: make C-g great again
Diffstat (limited to 'user/emacs/init.el')
-rw-r--r--user/emacs/init.el26
1 files changed, 26 insertions, 0 deletions
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