diff --git a/init.el b/init.el index fb8da3d..5b4897d 100644 --- a/init.el +++ b/init.el @@ -215,6 +215,7 @@ (unbind-key "" global-map) (bind-key [remap fill-paragraph] #'endless/fill-or-unfill) (unbind-key "C-x C-c" global-map) + (bind-key [remap keyboard-quit] #'keyboard-quit-context+) ;; Custom helm bindings @@ -577,7 +578,8 @@ db/dired-from-shell-command db/system-open db/switch-to-dark-theme - db/switch-to-light-theme)) + db/switch-to-light-theme + keyboard-quit-context+)) (use-package db-hydras :commands (hydra-toggle/body diff --git a/site-lisp/db-utils.el b/site-lisp/db-utils.el index 09c702d..f1b9a5d 100644 --- a/site-lisp/db-utils.el +++ b/site-lisp/db-utils.el @@ -372,6 +372,40 @@ output, separated by \\n, when called with (t (start-process "" nil "xdg-open" path)))) +(defun keyboard-quit-context+ () + "Quit current context. + +This function is a combination of `keyboard-quit' and +`keyboard-escape-quit' with some parts omitted and some custom +behavior added. When the minibuffer is active, quit it +regardless of the currently selected window." + ;; https://with-emacs.com/posts/tips/quit-current-context/ + (interactive) + (cond ((region-active-p) + ;; Avoid adding the region to the window selection. + (setq saved-region-selection nil) + (let (select-active-regions) + (deactivate-mark))) + ((eq last-command 'mode-exited) nil) + (current-prefix-arg + nil) + (defining-kbd-macro + (message + (substitute-command-keys + "Quit is ignored during macro defintion, use \\[kmacro-end-macro] if you want to stop macro definition")) + (cancel-kbd-macro-events)) + ((active-minibuffer-window) + (when (get-buffer-window "*Completions*") + ;; hide completions first so point stays in active window when + ;; outside the minibuffer + (minibuffer-hide-completions)) + (abort-recursive-edit)) + (t + (when completion-in-region-mode + (completion-in-region-mode -1)) + (let ((debug-on-quit nil)) + (signal 'quit nil))))) + ;;; Extend Input Methods