From bb7a8e3866b31485a21d4235e72272d04dc57979 Mon Sep 17 00:00:00 2001 From: StrawberryTea Date: Mon, 19 Feb 2024 23:44:58 -0600 Subject: [PATCH 1/8] feat(config): add smart-ret for corfu This commit makes RET in Corfu quit auto-completion and passthrough to the underlying keymap if no completion is selected. --- modules/config/default/config.el | 16 +++++++++++----- 1 file changed, 11 insertions(+), 5 deletions(-) diff --git a/modules/config/default/config.el b/modules/config/default/config.el index 81203f6ea..fbb1e1575 100644 --- a/modules/config/default/config.el +++ b/modules/config/default/config.el @@ -461,8 +461,6 @@ Continues comments if executed from a commented line. Consults (map! :when (modulep! :completion corfu) :after corfu (:map corfu-map - [return] #'corfu-insert - "RET" #'corfu-insert "C-S-s" #'+corfu-move-to-minibuffer "C-p" #'corfu-previous "C-n" #'corfu-next @@ -489,11 +487,19 @@ Continues comments if executed from a commented line. Consults (when-let ((cmds-del (and (modulep! :completion corfu +tng) '(menu-item "Reset completion" corfu-reset :enable (and (> corfu--index -1) - (eq corfu-preview-current 'insert)))))) - (map! :after corfu + (eq corfu-preview-current 'insert))))) + (cmds-ret '(menu-item "Insert completion" corfu-insert + :filter (lambda (cmd) + (if (eq corfu--index -1) + (corfu-quit) + cmd))))) + (map! :when (modulep! :completion corfu) + :after corfu :map corfu-map [backspace] cmds-del - "DEL" cmds-del)) + "DEL" cmds-del + :ig [return] cmds-ret + :ig "RET" cmds-ret)) ;; Smarter C-a/C-e for both Emacs and Evil. C-a will jump to indentation. ;; Pressing it again will send you to the true bol. Same goes for C-e, except From dd65e57a3a270aa88a3ff8f1ce2d9d01660bc8b8 Mon Sep 17 00:00:00 2001 From: StrawberryTea Date: Tue, 20 Feb 2024 08:23:00 -0600 Subject: [PATCH 2/8] fix(corfu): move orderless conf out of corfu block --- modules/completion/corfu/config.el | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/modules/completion/corfu/config.el b/modules/completion/corfu/config.el index e8805ce2b..ab2d8313b 100644 --- a/modules/completion/corfu/config.el +++ b/modules/completion/corfu/config.el @@ -17,6 +17,9 @@ (when (where-is-internal #'completion-at-point (list (current-local-map))) (setq-local corfu-echo-delay nil) (corfu-mode +1)))) + (when (modulep! +orderless) + (after! orderless + (setq orderless-component-separator #'orderless-escapable-split-on-space))) :config (setq corfu-auto t corfu-auto-delay 0.1 @@ -50,10 +53,6 @@ (when (modulep! +icons) (add-to-list 'corfu-margin-formatters #'nerd-icons-corfu-formatter)) - (when (modulep! +orderless) - (after! orderless - (setq orderless-component-separator #'orderless-escapable-split-on-space))) - ;; If you want to update the visual hints after completing minibuffer commands ;; with Corfu and exiting, you have to do it manually. (defadvice! +corfu--insert-before-exit-minibuffer-a () From fb985684e33cda13a650097e36a8f9c61748d8ee Mon Sep 17 00:00:00 2001 From: StrawberryTea Date: Wed, 21 Feb 2024 16:51:11 -0600 Subject: [PATCH 3/8] fix(corfu): complete after colon in lispy --- modules/completion/corfu/config.el | 1 + 1 file changed, 1 insertion(+) diff --git a/modules/completion/corfu/config.el b/modules/completion/corfu/config.el index ab2d8313b..08b469e6b 100644 --- a/modules/completion/corfu/config.el +++ b/modules/completion/corfu/config.el @@ -44,6 +44,7 @@ ;; However, it should otherwise behave like normal, whatever normal was. tab-always-indent (if (modulep! +tng) 'complete tab-always-indent)) (add-to-list 'completion-category-overrides `(lsp-capf (styles ,@completion-styles))) + (add-to-list 'corfu-auto-commands #'lispy-colon) (add-to-list 'corfu-continue-commands #'+corfu-move-to-minibuffer) From 5627e7b50bc55dafd49ec655a0ae1c261644115a Mon Sep 17 00:00:00 2001 From: StrawberryTea Date: Fri, 23 Feb 2024 10:19:55 -0600 Subject: [PATCH 4/8] fix(corfu): use :filter instead of :enable --- modules/config/default/config.el | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) diff --git a/modules/config/default/config.el b/modules/config/default/config.el index fbb1e1575..0fa77681d 100644 --- a/modules/config/default/config.el +++ b/modules/config/default/config.el @@ -481,13 +481,16 @@ Continues comments if executed from a commented line. Consults "C-S-d" (cmd! (funcall-interactively #'corfu-popupinfo-scroll-up corfu-popupinfo-min-height))) (:map corfu-map "C-" '(menu-item "Conclude the minibuffer" exit-minibuffer - :enable (minibufferp nil t)) - "S-" '(menu-item "Insert completion and conclude" +corfu-complete-and-exit-minibuffer - :enable (minibufferp nil t)))) + :filter (lambda (cmd) (when (minibufferp nil t) cmd))) + "S-" '(menu-item "Insert completion and conclude" + +corfu-complete-and-exit-minibuffer + :filter (lambda (cmd) (when (minibufferp nil t) cmd))))) (when-let ((cmds-del (and (modulep! :completion corfu +tng) '(menu-item "Reset completion" corfu-reset - :enable (and (> corfu--index -1) - (eq corfu-preview-current 'insert))))) + :filter (lambda (cmd) + (when (and (>= corfu--index 0) + (eq corfu-preview-current 'insert)) + cmd))))) (cmds-ret '(menu-item "Insert completion" corfu-insert :filter (lambda (cmd) (if (eq corfu--index -1) From a85b61dc0c9b2e3b93a02f52dc2942c02f5758d0 Mon Sep 17 00:00:00 2001 From: StrawberryTea Date: Fri, 23 Feb 2024 10:53:11 -0600 Subject: [PATCH 5/8] fix(corfu): improve detection of comments Due to https://github.com/minad/cape/pull/109, we cannot rely on only faces to detect comments. --- modules/completion/corfu/autoload.el | 15 +++++++++++++++ modules/completion/corfu/config.el | 12 +++--------- 2 files changed, 18 insertions(+), 9 deletions(-) diff --git a/modules/completion/corfu/autoload.el b/modules/completion/corfu/autoload.el index 195c49c4c..fcac8babe 100644 --- a/modules/completion/corfu/autoload.el +++ b/modules/completion/corfu/autoload.el @@ -49,3 +49,18 @@ ;; Without this corfu quits immediately. (setq this-command #'corfu-insert-separator) (call-interactively #'corfu-insert-separator)))) + +;;;###autoload +(defun +corfu-in-doc-or-comment-p (_sym) + "Return non-nil if point is in a docstring or comment." + (or (nth 4 (syntax-ppss)) + (when-let ((faces '(font-lock-comment-face + font-lock-doc-face + tree-sitter-hl-face:doc + tree-sitter-hl-face:comment)) + (fs (get-text-property (point) 'face))) + (if (listp fs) + (cl-loop for f in fs + if (memq f faces) + return t) + (memq fs faces))))) diff --git a/modules/completion/corfu/config.el b/modules/completion/corfu/config.el index 08b469e6b..a6339e2d4 100644 --- a/modules/completion/corfu/config.el +++ b/modules/completion/corfu/config.el @@ -104,11 +104,8 @@ (add-hook! (prog-mode conf-mode) (defun +corfu-add-cape-emoji-h () (add-hook 'completion-at-point-functions - (cape-capf-inside-faces - (cape-capf-prefix-length #'cape-emoji 1) - ;; Only call inside comments and docstrings. - 'tree-sitter-hl-face:doc 'font-lock-doc-face - 'font-lock-comment-face 'tree-sitter-hl-face:comment) + (cape-capf-predicate (cape-capf-prefix-length #'cape-emoji 1) + #'+corfu-in-doc-or-comment-p) 10 t))) (add-hook! text-mode (defun +corfu-add-cape-emoji-text-h () @@ -119,10 +116,7 @@ (add-hook! (prog-mode conf-mode) (defun +corfu-add-cape-dict-h () (add-hook 'completion-at-point-functions - (cape-capf-inside-faces - ;; Only call inside comments and docstrings. - #'cape-dict 'tree-sitter-hl-face:doc 'font-lock-doc-face - 'font-lock-comment-face 'tree-sitter-hl-face:comment) + (cape-capf-predicate #'+corfu-in-doc-or-comment-p #'cape-dict) 40 t))) (add-hook! text-mode (defun +corfu-add-cape-dict-text-h () From 2223add3827c814d1b19793e91a3e5c8ded57857 Mon Sep 17 00:00:00 2001 From: StrawberryTea Date: Fri, 23 Feb 2024 17:55:30 -0600 Subject: [PATCH 6/8] fix(corfu): eval lambdas --- modules/config/default/config.el | 30 +++++++++++++++--------------- 1 file changed, 15 insertions(+), 15 deletions(-) diff --git a/modules/config/default/config.el b/modules/config/default/config.el index 0fa77681d..260c9abd3 100644 --- a/modules/config/default/config.el +++ b/modules/config/default/config.el @@ -480,22 +480,22 @@ Continues comments if executed from a commented line. Consults "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))) (:map corfu-map - "C-" '(menu-item "Conclude the minibuffer" exit-minibuffer - :filter (lambda (cmd) (when (minibufferp nil t) cmd))) - "S-" '(menu-item "Insert completion and conclude" - +corfu-complete-and-exit-minibuffer - :filter (lambda (cmd) (when (minibufferp nil t) cmd))))) + "C-" `(menu-item "Conclude the minibuffer" exit-minibuffer + :filter ,(lambda (cmd) (when (minibufferp nil t) cmd))) + "S-" `(menu-item "Insert completion and conclude" + +corfu-complete-and-exit-minibuffer + :filter ,(lambda (cmd) (when (minibufferp nil t) cmd))))) (when-let ((cmds-del (and (modulep! :completion corfu +tng) - '(menu-item "Reset completion" corfu-reset - :filter (lambda (cmd) - (when (and (>= corfu--index 0) - (eq corfu-preview-current 'insert)) - cmd))))) - (cmds-ret '(menu-item "Insert completion" corfu-insert - :filter (lambda (cmd) - (if (eq corfu--index -1) - (corfu-quit) - cmd))))) + `(menu-item "Reset completion" corfu-reset + :filter ,(lambda (cmd) + (when (and (>= corfu--index 0) + (eq corfu-preview-current 'insert)) + cmd))))) + (cmds-ret `(menu-item "Insert completion" corfu-insert + :filter ,(lambda (cmd) + (if (eq corfu--index -1) + (corfu-quit) + cmd))))) (map! :when (modulep! :completion corfu) :after corfu :map corfu-map From 1a3c973f334f15ca9cdaa993f8911201afbdea05 Mon Sep 17 00:00:00 2001 From: StrawberryTea Date: Wed, 28 Feb 2024 16:25:21 -0600 Subject: [PATCH 7/8] feat(config): modify smart-ret in +tng minibuffer If we are using +tng, we can make RET always exit the minibuffer. This has the downside of not being able to insert snippets, but no one writes snippets for the minibuffer anyway. --- modules/config/default/config.el | 25 ++++++++++++++++--------- 1 file changed, 16 insertions(+), 9 deletions(-) diff --git a/modules/config/default/config.el b/modules/config/default/config.el index 260c9abd3..fa98a80a8 100644 --- a/modules/config/default/config.el +++ b/modules/config/default/config.el @@ -479,12 +479,13 @@ Continues comments if executed from a commented line. Consults "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))) - (:map corfu-map - "C-" `(menu-item "Conclude the minibuffer" exit-minibuffer - :filter ,(lambda (cmd) (when (minibufferp nil t) cmd))) - "S-" `(menu-item "Insert completion and conclude" - +corfu-complete-and-exit-minibuffer - :filter ,(lambda (cmd) (when (minibufferp nil t) cmd))))) + (:when (not (modulep! :completion corfu +tng)) + (:map corfu-map + "C-" `(menu-item "Conclude the minibuffer" exit-minibuffer + :filter ,(lambda (cmd) (when (minibufferp nil t) cmd))) + "S-" `(menu-item "Insert completion and conclude" + +corfu-complete-and-exit-minibuffer + :filter ,(lambda (cmd) (when (minibufferp nil t) cmd)))))) (when-let ((cmds-del (and (modulep! :completion corfu +tng) `(menu-item "Reset completion" corfu-reset :filter ,(lambda (cmd) @@ -493,9 +494,15 @@ Continues comments if executed from a commented line. Consults cmd))))) (cmds-ret `(menu-item "Insert completion" corfu-insert :filter ,(lambda (cmd) - (if (eq corfu--index -1) - (corfu-quit) - cmd))))) + (cond ((eq corfu--index -1) + (corfu-quit)) + ((and (modulep! :completion corfu +tng) + (eq corfu-preview-current 'insert) + (minibufferp nil t)) + (corfu-insert) + nil) + (t + cmd)))))) (map! :when (modulep! :completion corfu) :after corfu :map corfu-map From 40518d932a232be5217c9cdbe545cbd273d70058 Mon Sep 17 00:00:00 2001 From: StrawberryTea Date: Wed, 28 Feb 2024 17:42:47 -0600 Subject: [PATCH 8/8] feat(corfu): make minibuffer completion optional --- modules/completion/corfu/config.el | 21 +++++++++++++++++++-- 1 file changed, 19 insertions(+), 2 deletions(-) diff --git a/modules/completion/corfu/config.el b/modules/completion/corfu/config.el index a6339e2d4..7c860e47c 100644 --- a/modules/completion/corfu/config.el +++ b/modules/completion/corfu/config.el @@ -6,6 +6,11 @@ (defvar +corfu-want-C-x-bindings t "Whether `C-x' is a completion prefix in Evil insert state.") +(defvar +corfu-want-minibuffer-completion t + "Whether to enable Corfu in the minibuffer. +Setting this to `aggressive' will enable Corfu in more commands which +use the minibuffer such as `query-replace'.") + ;; ;;; Packages (use-package! corfu @@ -13,8 +18,20 @@ :init (add-hook! 'minibuffer-setup-hook (defun +corfu-enable-in-minibuffer () - "Enable Corfu in the minibuffer if `completion-at-point' is bound." - (when (where-is-internal #'completion-at-point (list (current-local-map))) + "Enable Corfu in the minibuffer." + (when (pcase +corfu-want-minibuffer-completion + ('aggressive + (not (or (bound-and-true-p mct--active) + (bound-and-true-p vertico--input) + (eq (current-local-map) read-passwd-map) + (and (featurep 'helm-core) (helm--alive-p)) + (and (featurep 'ido) (ido-active)) + (where-is-internal 'minibuffer-complete + (list (current-local-map))) + (memq #'ivy--queue-exhibit post-command-hook)))) + ('nil nil) + (_ (where-is-internal #'completion-at-point + (list (current-local-map))))) (setq-local corfu-echo-delay nil) (corfu-mode +1)))) (when (modulep! +orderless)