From 188e73b0574857ed0df29b270780fbab4586ba45 Mon Sep 17 00:00:00 2001 From: Luigi Sartor Piucco Date: Thu, 21 Dec 2023 12:07:39 -0300 Subject: [PATCH] feat(corfu): impl smart confirm in minibuffer An issue when using corfu in the minibuffer was the need for pressing RET twice, since the first only inserts the completion. This commit aliviates that by providing C-RET to ignore completion and conclude the minibuffer immediatelly and S-RET to insert completion then conclude. --- modules/completion/corfu/README.org | 23 +++++++++++++++++++++++ modules/completion/corfu/autoload.el | 8 ++++++++ modules/config/default/config.el | 8 ++++++-- 3 files changed, 37 insertions(+), 2 deletions(-) diff --git a/modules/completion/corfu/README.org b/modules/completion/corfu/README.org index e9354f8a2..39633da9f 100644 --- a/modules/completion/corfu/README.org +++ b/modules/completion/corfu/README.org @@ -103,6 +103,29 @@ following additional binds: | [[kbd:][S-TAB]] | (when completing) Go to previous candidate | | [[kbd:][DEL]] | (when completing) Reset completion DWIM-style | +*** Completion in the minibuffer +In the minibuffer, sometimes autocompletion can interfere with your goal; +Imagine you're composing a search pattern incrementally, and you find what you +want early, with only half the word. You then press [[kbd:RET]]. If completion +kicked in as you typed, you may lose the match, since it will complete the +first candidate. On the other hand, if you were paying attention to the +suggestions and selecting one appropriate, that's desired behavior, and you may +even desire to modify the prompt further (if you were composing a command +instead, you may want to extend it after the candidate). To allow better +control, there are 3 confirm bindings when Corfu appears in the minibuffer: + +| Keybind | Description | +|-----------+--------------------------------------------------------------------| +| [[kbd:RET]] | Accept the candidate only | +| [[kbd:C-RET]] | Confirm the current prompt only | +| [[kbd:S-RET]] | Accept the candidate then immediately confirm the completed prompt | + +- Use [[kbd:RET]] when you want to continue composing after completing; +- Use [[kbd:C-RET]] when you already have the desired string, and completing would + break it; +- Use [[kbd:S-RET]] when you know the composition will be finished after completion + (thus avoiding the need to type [[kbd:RET]] twice); + ** Searching with multiple keywords If the [[doom-module::completion corfu +orderless]] flag is enabled, users can perform code completion with multiple search keywords by use of space as the diff --git a/modules/completion/corfu/autoload.el b/modules/completion/corfu/autoload.el index 618aae42d..841802596 100644 --- a/modules/completion/corfu/autoload.el +++ b/modules/completion/corfu/autoload.el @@ -1,5 +1,13 @@ ;;; completion/corfu/autoload.el -*- lexical-binding: t; -*- +;;;###autoload +(defun +corfu-complete-and-exit-minibuffer () + (interactive) + (if (>= corfu--index 0) + (corfu-complete) + (corfu-insert)) + (exit-minibuffer)) + ;;;###autoload (defun +corfu-move-to-minibuffer () ;; Taken from corfu's README. diff --git a/modules/config/default/config.el b/modules/config/default/config.el index 415c6c09a..0a715a35b 100644 --- a/modules/config/default/config.el +++ b/modules/config/default/config.el @@ -480,8 +480,12 @@ Continues comments if executed from a commented line. Consults "C-S-p" #'corfu-popupinfo-scroll-down "C-S-n" #'corfu-popupinfo-scroll-up "C-S-u" (cmd! (funcall-interactively #'corfu-popupinfo-scroll-down corfu-popupinfo-min-height)) - "C-S-d" (cmd! (funcall-interactively #'corfu-popupinfo-scroll-up corfu-popupinfo-min-height)))) - + "C-S-d" (cmd! (funcall-interactively #'corfu-popupinfo-scroll-up corfu-popupinfo-min-height))) + (:map corfu-map + :gi "C-" '(menu-item "Conclude the minibuffer" exit-minibuffer + :enable (active-minibuffer-window)) + :gi "S-" '(menu-item "Insert completion and conclude" +corfu-complete-and-exit-minibuffer + :enable (active-minibuffer-window)))) (when-let ((cmds-del (and (modulep! :completion corfu +tng) (cmds! (and (> corfu--index -1) (eq corfu-preview-current 'insert))