@alvaro
sign in · lmno.lol

DWIM ivy quit

"Do-what-I-mean" (DWIM) functions enable us to introduce new Emacs powers to existing workflows without incurring the typical cost of remembering multiple related functions or introducing yet another key binding. DWIM functions invoke other functions, based on current context.

I wanted a small tweak in Ivy's `minibuffer-keyboard-quit' invocation, commonly invoked via C-g key binding:

  1. If we have text selected in minibuffer, deselect it.
  2. If we have any text in minibuffer, clear it.
  3. If no text in minibuffer, quit.

Added `ar/ivy-keyboard-quit-dwim' for this purpose. Binding it to C-g in ivy-minibuffer-map:

(use-package ivy
  :ensure t
  :bind (:map ivy-minibuffer-map
              ("C-g" . ar/ivy-keyboard-quit-dwim))
  :config
  (defun ar/ivy-keyboard-quit-dwim ()
    "If region active, deactivate. If there's content, clear the minibuffer. Otherwise quit."
    (interactive)
    (cond ((and delete-selection-mode (region-active-p))
           (setq deactivate-mark t))
          ((> (length ivy-text) 0)
           (delete-minibuffer-contents))
          (t
           (minibuffer-keyboard-quit)))))