summary refs log tree commit diff stats
path: root/user
diff options
context:
space:
mode:
Diffstat (limited to 'user')
-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