fix(corfu): improve keybinding config

This commit is contained in:
Luigi Sartor Piucco 2023-10-27 16:00:03 -03:00
parent 50bdf024ad
commit 241b8896da
No known key found for this signature in database
GPG key ID: 6FF1A01853A47A66
2 changed files with 76 additions and 88 deletions

View file

@ -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 characters, or by means of the [[kbd:][C-SPC]] keybinding at any moment. While the popup
is visible, the following relevant keys are available: is visible, the following relevant keys are available:
| Keybind | Description | | Keybind | Description |
|----------+---------------------------------------------------------| |----------+------------------------------------------------------------------|
| [[kbd:][<down>]] | Go to next candidate | | [[kbd:][<down>]] | Go to next candidate |
| [[kbd:][<up>]] | Go to previous candidate | | [[kbd:][<up>]] | Go to previous candidate |
| [[kbd:][C-n]] | Go to next candidate | | [[kbd:][C-n]] | Go to next candidate |
| [[kbd:][C-p]] | Go to previous candidate | | [[kbd:][C-p]] | Go to previous candidate |
| [[kbd:][C-j]] | (evil) Go to next candidate | | [[kbd:][C-j]] | (evil) Go to next candidate |
| [[kbd:][C-k]] | (evil) Go to previous candidate | | [[kbd:][C-k]] | (evil) Go to previous candidate |
| [[kbd:][C-<down>]] | Go to next doc line | | [[kbd:][C-<down>]] | Go to next doc line |
| [[kbd:][C-<up>]] | Go to previous doc line | | [[kbd:][C-<up>]] | Go to previous doc line |
| [[kbd:][C-S-n]] | Go to next doc line | | [[kbd:][C-S-n]] | Go to next doc line |
| [[kbd:][C-S-p]] | Go to previous doc line | | [[kbd:][C-S-p]] | Go to previous doc line |
| [[kbd:][C-S-j]] | (evil) Go to next doc line | | [[kbd:][C-S-j]] | (evil) Go to next doc line |
| [[kbd:][C-S-k]] | (evil) Go to previous doc line | | [[kbd:][C-S-k]] | (evil) Go to previous doc line |
| [[kbd:][C-h]] | Toggle documentation (if available) | | [[kbd:][C-h]] | Toggle documentation (if available) |
| [[kbd:][M-m]] | Export to minibuffer (if [[doom-module::completion vertico]]) | | [[kbd:][M-m]] | Export to minibuffer (if [[doom-module::completion vertico]]) |
| [[kbd:][M-j]] | (evil) Export to minibuffer (if [[doom-module::completion vertico]]) | | [[kbd:][M-S-j]] | (evil) Export to minibuffer (if [[doom-module::completion vertico]]) |
| [[kbd:][RET]] | Insert candidate | | [[kbd:][RET]] | Insert candidate |
| [[kbd:][SPC]] | Quit autocompletion after a wildcard or pass-through | | [[kbd:][SPC]] | (after wildcard) Reset completion |
| [[kbd:][C-SPC]] | (when completing) Insert separator (see below) | | [[kbd:][DEL]] | Reset completion |
| [[kbd:][C-SPC]] | Complete (unless [[doom-module::completion corfu +tng]]) | | [[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]] 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 flag so that, instead, you trigger completion with [[kbd:][TAB]], getting the following
@ -95,16 +96,16 @@ additional binds:
| [[kbd:][TAB]] | Complete | | [[kbd:][TAB]] | Complete |
| [[kbd:][TAB]] | (when completing) Go to next candidate | | [[kbd:][TAB]] | (when completing) Go to next candidate |
| [[kbd:][S-TAB]] | (when completing) Go to previous candidate | | [[kbd:][S-TAB]] | (when completing) Go to previous candidate |
| [[kbd:][DEL]] | (when completing) Reset completion DWIM-style |
** Searching with multiple keywords ** Searching with multiple keywords
If the [[doom-module::completion corfu +orderless]] flag is enabled, users can perform code If the [[doom-module::completion corfu +orderless]] flag is enabled, users can
completion with multiple search keywords by use of space as the separator. More perform code completion with multiple search keywords by use of space or ~,~ as
information can be found [[https://github.com/oantolin/orderless#company][here]]. Pressing [[kdb:][C-SPC]] again while completing inserts a the separator. More information can be found [[https://github.com/oantolin/orderless#company][here]]. Pressing [[kdb:][C-SPC]] again while
space as separator. This allows searching with space-separated terms; each piece completing inserts a space as separator. This allows searching with
will match individually and in any order, with smart casing. Pressing just [[kbd:][SPC]] space-separated terms; each piece will match individually and in any order, with
acts as normal and restarts completion, so that when typing sentences it doesn't smart casing. Pressing just [[kbd:][SPC]] acts as normal and quits completion, so that
try to complete the whole sentence instead of just the word. 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 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 it's the comma key), then that character acts as a wildcard when typed

View file

@ -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) (apply (timer--function evil--ex-search-update-timer)
(timer--args 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 "<return>" `(menu-item "corfu-maybe-return" corfu-insert
:filter corfu--maybe-return-filter))
;; Allow completion after `:' in Lispy. ;; Allow completion after `:' in Lispy.
(add-to-list 'corfu-auto-commands #'lispy-colon) (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 (setq orderless-component-separator
(+orderless-escapable-split-fn +orderless-wildcard-character)) (+orderless-escapable-split-fn +orderless-wildcard-character))
(setq corfu-separator +orderless-wildcard-character) (setq corfu-separator +orderless-wildcard-character)
(keymap-set corfu-map (char-to-string +orderless-wildcard-character) (defun +corfu--maybe-quit-spc-filter (cmd)
#'+corfu-insert-wildcard-separator) (when (and (> (point) (point-min))
;; Quit completion after typing the wildcard followed by a space. (eq (char-before) +orderless-wildcard-character))
(keymap-set corfu-map "SPC" cmd))
`(menu-item "corfu-maybe-quit" nil (let ((wildstr (char-to-string +orderless-wildcard-character))
:filter (mi-spc '(menu-item "corfu-maybe-quit" corfu-reset :filter +corfu--maybe-quit-spc-filter)))
,(lambda (_) (map! :map corfu-map
(when (and (> (point) (point-min)) wildstr #'+corfu-insert-wildcard-separator
(eq (char-before) ;; Quit completion after typing the wildcard followed by a space.
+orderless-wildcard-character)) "SPC" mi-spc))))
(corfu-quit)
nil))))))
(add-hook! 'evil-insert-state-exit-hook (add-hook! 'evil-insert-state-exit-hook
(defun +corfu-quit-on-evil-insert-state-exit-h () (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) (when (modulep! +icons)
(add-to-list 'corfu-margin-formatters #'nerd-icons-corfu-formatter)) (add-to-list 'corfu-margin-formatters #'nerd-icons-corfu-formatter))
;; Reset completion DWIM-style with backspace. (defun +corfu--maybe-reset-backspace-filter (cmd)
(when (modulep! +tng) (when (and (modulep! +tng)
(defun corfu--maybe-reset-backspace-filter (cmd) (> corfu--index -1)
(when (and (> corfu--index -1) (eq corfu-preview-current 'insert))
(eq corfu-preview-current 'insert)) cmd))
cmd)) (defun +corfu--maybe-quit-return-filter (cmd)
(keymap-set corfu-map "DEL" `(menu-item "corfu-maybe-reset" corfu-reset (let ((corfu--index (if (> corfu--index -1) corfu--index 0)))
:filter corfu--maybe-reset-backspace-filter)) (corfu-insert))
(keymap-set cmd)
corfu-map "<backspace>" `(menu-item "corfu-maybe-reset" corfu-reset (let ((mi-del '(menu-item "corfu-maybe-reset-backspace-filter" corfu-reset
:filter corfu--maybe-reset-backspace-filter))) :filter +corfu--maybe-reset-backspace-filter)))
(map! :map corfu-map
(map! (:map 'corfu-map [return] #'corfu-insert
(:when (modulep! +orderless) "RET" #'corfu-insert
:gi "C-SPC" #'corfu-insert-separator) (:when (modulep! +orderless)
(:when (modulep! +tng) :gi "C-SPC" #'corfu-insert-separator)
[tab] #'corfu-next (:when (modulep! +tng)
[backtab] #'corfu-previous [tab] #'corfu-next
"TAB" #'corfu-next [backtab] #'corfu-previous
"S-TAB" #'corfu-previous))) "TAB" #'corfu-next
(after! evil-collection-corfu "S-TAB" #'corfu-previous
(evil-collection-define-key 'insert 'corfu-map [backspace] mi-del
(kbd "RET") #'corfu-insert "DEL" mi-del)))
[return] #'corfu-insert))
(after! vertico (after! vertico
(map! :map 'corfu-map "M-m" #'corfu-move-to-minibuffer) (map! :map corfu-map
(after! evil-collection-corfu "M-m" #'corfu-move-to-minibuffer
(evil-collection-define-key 'insert 'corfu-map (:when (modulep! :editor evil) "M-J" #'corfu-move-to-minibuffer))))
(kbd "M-j") #'corfu-move-to-minibuffer))))
(use-package! cape (use-package! cape
:defer t :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) :hook (corfu-mode . corfu-popupinfo-mode)
:config :config
(setq corfu-popupinfo-delay '(0.5 . 1.0)) (setq corfu-popupinfo-delay '(0.5 . 1.0))
(map! (:map 'corfu-map (map! :map corfu-map
"C-<up>" #'corfu-popupinfo-scroll-down "C-<up>" #'corfu-popupinfo-scroll-down
"C-<down>" #'corfu-popupinfo-scroll-up "C-<down>" #'corfu-popupinfo-scroll-up
"C-S-p" #'corfu-popupinfo-scroll-down "C-S-p" #'corfu-popupinfo-scroll-down
"C-S-n" #'corfu-popupinfo-scroll-up "C-S-n" #'corfu-popupinfo-scroll-up
"C-h" #'corfu-popupinfo-toggle) "C-h" #'corfu-popupinfo-toggle)
(:map 'corfu-popupinfo-map (map! :when (modulep! :editor evil)
:when (modulep! :editor evil) :map corfu-popupinfo-map
;; Reversed because popupinfo assumes opposite of what feels intuitive ;; Reversed because popupinfo assumes opposite of what feels intuitive
;; with evil. ;; with evil.
"C-S-k" #'corfu-popupinfo-scroll-down "C-S-k" #'corfu-popupinfo-scroll-down
"C-S-j" #'corfu-popupinfo-scroll-up))) "C-S-j" #'corfu-popupinfo-scroll-up))