From 241b8896da7a0edd46cb80bc940c4035b884498c Mon Sep 17 00:00:00 2001 From: Luigi Sartor Piucco Date: Fri, 27 Oct 2023 16:00:03 -0300 Subject: [PATCH] fix(corfu): improve keybinding config --- modules/completion/corfu/README.org | 55 +++++++------- modules/completion/corfu/config.el | 109 ++++++++++++---------------- 2 files changed, 76 insertions(+), 88 deletions(-) diff --git a/modules/completion/corfu/README.org b/modules/completion/corfu/README.org index e74332d16..a72f336bc 100644 --- a/modules/completion/corfu/README.org +++ b/modules/completion/corfu/README.org @@ -64,27 +64,28 @@ By default, completion gets triggered after typing 2 non-space consecutive characters, or by means of the [[kbd:][C-SPC]] keybinding at any moment. While the popup is visible, the following relevant keys are available: -| Keybind | Description | -|----------+---------------------------------------------------------| -| [[kbd:][]] | Go to next candidate | -| [[kbd:][]] | Go to previous candidate | -| [[kbd:][C-n]] | Go to next candidate | -| [[kbd:][C-p]] | Go to previous candidate | -| [[kbd:][C-j]] | (evil) Go to next candidate | -| [[kbd:][C-k]] | (evil) Go to previous candidate | -| [[kbd:][C-]] | Go to next doc line | -| [[kbd:][C-]] | Go to previous doc line | -| [[kbd:][C-S-n]] | Go to next doc line | -| [[kbd:][C-S-p]] | Go to previous doc line | -| [[kbd:][C-S-j]] | (evil) Go to next doc line | -| [[kbd:][C-S-k]] | (evil) Go to previous doc line | -| [[kbd:][C-h]] | Toggle documentation (if available) | +| Keybind | Description | +|----------+------------------------------------------------------------------| +| [[kbd:][]] | Go to next candidate | +| [[kbd:][]] | Go to previous candidate | +| [[kbd:][C-n]] | Go to next candidate | +| [[kbd:][C-p]] | Go to previous candidate | +| [[kbd:][C-j]] | (evil) Go to next candidate | +| [[kbd:][C-k]] | (evil) Go to previous candidate | +| [[kbd:][C-]] | Go to next doc line | +| [[kbd:][C-]] | Go to previous doc line | +| [[kbd:][C-S-n]] | Go to next doc line | +| [[kbd:][C-S-p]] | Go to previous doc line | +| [[kbd:][C-S-j]] | (evil) Go to next doc line | +| [[kbd:][C-S-k]] | (evil) Go to previous doc line | +| [[kbd:][C-h]] | Toggle documentation (if available) | | [[kbd:][M-m]] | Export to minibuffer (if [[doom-module::completion vertico]]) | -| [[kbd:][M-j]] | (evil) Export to minibuffer (if [[doom-module::completion vertico]]) | -| [[kbd:][RET]] | Insert candidate | -| [[kbd:][SPC]] | Quit autocompletion after a wildcard or pass-through | -| [[kbd:][C-SPC]] | (when completing) Insert separator (see below) | +| [[kbd:][M-S-j]] | (evil) Export to minibuffer (if [[doom-module::completion vertico]]) | +| [[kbd:][RET]] | Insert candidate | +| [[kbd:][SPC]] | (after wildcard) Reset completion | +| [[kbd:][DEL]] | Reset completion | | [[kbd:][C-SPC]] | Complete (unless [[doom-module::completion corfu +tng]]) | +| [[kbd:][C-SPC]] | (when completing) Insert separator (see below) | If you prefer a [[kbd:][TAB]]-centric completion style, enable the [[doom-module::completion corfu +tng]] flag so that, instead, you trigger completion with [[kbd:][TAB]], getting the following @@ -95,16 +96,16 @@ additional binds: | [[kbd:][TAB]] | Complete | | [[kbd:][TAB]] | (when completing) Go to next candidate | | [[kbd:][S-TAB]] | (when completing) Go to previous candidate | -| [[kbd:][DEL]] | (when completing) Reset completion DWIM-style | ** 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 separator. More -information can be found [[https://github.com/oantolin/orderless#company][here]]. Pressing [[kdb:][C-SPC]] again while completing inserts a -space as separator. This allows searching with space-separated terms; each piece -will match individually and in any order, with smart casing. Pressing just [[kbd:][SPC]] -acts as normal and restarts completion, so that when typing sentences it doesn't -try to complete the whole sentence instead of just the word. +If the [[doom-module::completion corfu +orderless]] flag is enabled, users can +perform code completion with multiple search keywords by use of space or ~,~ as +the separator. More information can be found [[https://github.com/oantolin/orderless#company][here]]. Pressing [[kdb:][C-SPC]] again while +completing inserts a space as separator. This allows searching with +space-separated terms; each piece will match individually and in any order, with +smart casing. Pressing just [[kbd:][SPC]] acts as normal and quits completion, so that +when typing sentences it doesn't try to complete the whole sentence instead of +just the word. Furthermore, if you also have [[var:+orderless-wildcard-character]] set (by default it's the comma key), then that character acts as a wildcard when typed diff --git a/modules/completion/corfu/config.el b/modules/completion/corfu/config.el index d558f8e9e..3a8315204 100644 --- a/modules/completion/corfu/config.el +++ b/modules/completion/corfu/config.el @@ -83,15 +83,6 @@ This variable needs to be set at the top-level before any `after!' blocks.") (apply (timer--function evil--ex-search-update-timer) (timer--args evil--ex-search-update-timer))))) - ;; Do not make us type RET twice with Corfu. - (defun corfu--maybe-return-filter (cmd) - (if (eq corfu--index -1) (corfu-quit) cmd)) - (keymap-set corfu-map "RET" `(menu-item "corfu-maybe-return" corfu-insert - :filter corfu--maybe-return-filter)) - (keymap-set - corfu-map "" `(menu-item "corfu-maybe-return" corfu-insert - :filter corfu--maybe-return-filter)) - ;; Allow completion after `:' in Lispy. (add-to-list 'corfu-auto-commands #'lispy-colon) @@ -133,18 +124,16 @@ This variable needs to be set at the top-level before any `after!' blocks.") (setq orderless-component-separator (+orderless-escapable-split-fn +orderless-wildcard-character)) (setq corfu-separator +orderless-wildcard-character) - (keymap-set corfu-map (char-to-string +orderless-wildcard-character) - #'+corfu-insert-wildcard-separator) - ;; Quit completion after typing the wildcard followed by a space. - (keymap-set corfu-map "SPC" - `(menu-item "corfu-maybe-quit" nil - :filter - ,(lambda (_) - (when (and (> (point) (point-min)) - (eq (char-before) - +orderless-wildcard-character)) - (corfu-quit) - nil)))))) + (defun +corfu--maybe-quit-spc-filter (cmd) + (when (and (> (point) (point-min)) + (eq (char-before) +orderless-wildcard-character)) + cmd)) + (let ((wildstr (char-to-string +orderless-wildcard-character)) + (mi-spc '(menu-item "corfu-maybe-quit" corfu-reset :filter +corfu--maybe-quit-spc-filter))) + (map! :map corfu-map + wildstr #'+corfu-insert-wildcard-separator + ;; Quit completion after typing the wildcard followed by a space. + "SPC" mi-spc)))) (add-hook! 'evil-insert-state-exit-hook (defun +corfu-quit-on-evil-insert-state-exit-h () @@ -157,36 +146,34 @@ This variable needs to be set at the top-level before any `after!' blocks.") (when (modulep! +icons) (add-to-list 'corfu-margin-formatters #'nerd-icons-corfu-formatter)) - ;; Reset completion DWIM-style with backspace. - (when (modulep! +tng) - (defun corfu--maybe-reset-backspace-filter (cmd) - (when (and (> corfu--index -1) - (eq corfu-preview-current 'insert)) - cmd)) - (keymap-set corfu-map "DEL" `(menu-item "corfu-maybe-reset" corfu-reset - :filter corfu--maybe-reset-backspace-filter)) - (keymap-set - corfu-map "" `(menu-item "corfu-maybe-reset" corfu-reset - :filter corfu--maybe-reset-backspace-filter))) - - (map! (:map 'corfu-map - (:when (modulep! +orderless) - :gi "C-SPC" #'corfu-insert-separator) - (:when (modulep! +tng) - [tab] #'corfu-next - [backtab] #'corfu-previous - "TAB" #'corfu-next - "S-TAB" #'corfu-previous))) - (after! evil-collection-corfu - (evil-collection-define-key 'insert 'corfu-map - (kbd "RET") #'corfu-insert - [return] #'corfu-insert)) + (defun +corfu--maybe-reset-backspace-filter (cmd) + (when (and (modulep! +tng) + (> corfu--index -1) + (eq corfu-preview-current 'insert)) + cmd)) + (defun +corfu--maybe-quit-return-filter (cmd) + (let ((corfu--index (if (> corfu--index -1) corfu--index 0))) + (corfu-insert)) + cmd) + (let ((mi-del '(menu-item "corfu-maybe-reset-backspace-filter" corfu-reset + :filter +corfu--maybe-reset-backspace-filter))) + (map! :map corfu-map + [return] #'corfu-insert + "RET" #'corfu-insert + (:when (modulep! +orderless) + :gi "C-SPC" #'corfu-insert-separator) + (:when (modulep! +tng) + [tab] #'corfu-next + [backtab] #'corfu-previous + "TAB" #'corfu-next + "S-TAB" #'corfu-previous + [backspace] mi-del + "DEL" mi-del))) (after! vertico - (map! :map 'corfu-map "M-m" #'corfu-move-to-minibuffer) - (after! evil-collection-corfu - (evil-collection-define-key 'insert 'corfu-map - (kbd "M-j") #'corfu-move-to-minibuffer)))) + (map! :map corfu-map + "M-m" #'corfu-move-to-minibuffer + (:when (modulep! :editor evil) "M-J" #'corfu-move-to-minibuffer)))) (use-package! cape :defer t @@ -281,15 +268,15 @@ This variable needs to be set at the top-level before any `after!' blocks.") :hook (corfu-mode . corfu-popupinfo-mode) :config (setq corfu-popupinfo-delay '(0.5 . 1.0)) - (map! (:map 'corfu-map - "C-" #'corfu-popupinfo-scroll-down - "C-" #'corfu-popupinfo-scroll-up - "C-S-p" #'corfu-popupinfo-scroll-down - "C-S-n" #'corfu-popupinfo-scroll-up - "C-h" #'corfu-popupinfo-toggle) - (:map 'corfu-popupinfo-map - :when (modulep! :editor evil) - ;; Reversed because popupinfo assumes opposite of what feels intuitive - ;; with evil. - "C-S-k" #'corfu-popupinfo-scroll-down - "C-S-j" #'corfu-popupinfo-scroll-up))) + (map! :map corfu-map + "C-" #'corfu-popupinfo-scroll-down + "C-" #'corfu-popupinfo-scroll-up + "C-S-p" #'corfu-popupinfo-scroll-down + "C-S-n" #'corfu-popupinfo-scroll-up + "C-h" #'corfu-popupinfo-toggle) + (map! :when (modulep! :editor evil) + :map corfu-popupinfo-map + ;; Reversed because popupinfo assumes opposite of what feels intuitive + ;; with evil. + "C-S-k" #'corfu-popupinfo-scroll-down + "C-S-j" #'corfu-popupinfo-scroll-up))