diff options
| author | Daniel Mendler <mail@daniel-mendler.de> | 2024-07-16 17:25:46 +0200 |
|---|---|---|
| committer | Daniel Mendler <mail@daniel-mendler.de> | 2024-07-16 18:09:57 +0200 |
| commit | 2dd7b7ae014851b970ca1b454e4ddb6a277cef51 (patch) | |
| tree | 0d3320b9ffc97fba2529d1613791ba2d904d95c3 | |
| parent | 5b7a40b006cc93d502b2ffd48368ff35fb596b3f (diff) | |
Add new customization variable global-corfu-minibuffer
| -rw-r--r-- | CHANGELOG.org | 2 | ||||
| -rw-r--r-- | README.org | 50 | ||||
| -rw-r--r-- | corfu.el | 39 | ||||
| -rw-r--r-- | extensions/corfu-echo.el | 3 |
4 files changed, 49 insertions, 45 deletions
diff --git a/CHANGELOG.org b/CHANGELOG.org index a3c07d9..cbd6f55 100644 --- a/CHANGELOG.org +++ b/CHANGELOG.org @@ -4,6 +4,8 @@ * Development +- New customization variable =global-corfu-minibuffer= to enable Corfu in the + minibuffer. - Unbind =C-a= in =corfu-map=. This binding is only needed in modes which override =C-a= instead of remapping ~move-beginning-of-line~. - Unbind =<tab>= in ~corfu-map~. This binding is only needed in modes which bind @@ -262,40 +262,26 @@ Corfu can be used for completion in the minibuffer, since it relies on child frames to display the candidates. The Corfu popup can be shown even if it doesn't fully fit inside the minibuffer. -By default, ~global-corfu-mode~ does not activate ~corfu-mode~ in the minibuffer, to -avoid interference with specialised minibuffer completion UIs like Vertico or -Mct. However you may still want to enable Corfu completion for commands like ~M-:~ -(~eval-expression~) or ~M-!~ (~shell-command~), which read from the minibuffer. In -order to detect minibuffers with completion we check if the variable -~completion-at-point-functions~ is set locally. +~global-corfu-mode~ activates ~corfu-mode~ in the minibuffer if the variable +~global-corfu-minibuffer~ is non-nil. In order to avoid interference with +specialised minibuffer completion UIs like Vertico or Mct, Corfu is only enabled +if the minibuffer sets the variable ~completion-at-point-functions~ locally. This +way minibuffers with completion can be detected, such that minibuffer commands +like ~M-:~ (~eval-expression~) or ~M-!~ (~shell-command~) are enhanced with Corfu +completion. + +If needed, one can also enable Corfu more generally in all minibuffers, as long +as no completion UI is active. In the following example we set +~global-corfu-minibuffer~ to a predicate function, which checks for Mct and +Vertico. Furthermore we ensure that Corfu is not enabled if a password is read +from the minibuffer. #+begin_src emacs-lisp -(defun corfu-enable-in-minibuffer () - "Enable Corfu in the minibuffer." - (when (local-variable-p 'completion-at-point-functions) - ;; (setq-local corfu-auto nil) ;; Enable/disable auto completion - (setq-local corfu-echo-delay nil ;; Disable automatic echo and popup - corfu-popupinfo-delay nil) - (corfu-mode 1))) -(add-hook 'minibuffer-setup-hook #'corfu-enable-in-minibuffer) -#+end_src - -This is not recommended, but one can also enable Corfu more generally for every -minibuffer, as long as no completion UI is active. In the following example we -check for Mct and Vertico. Furthermore we ensure that Corfu is not enabled if a -password is read from the minibuffer. - -#+begin_src emacs-lisp -(defun corfu-enable-always-in-minibuffer () - "Enable Corfu in the minibuffer if Vertico/Mct are not active." - (unless (or (bound-and-true-p mct--active) - (bound-and-true-p vertico--input) - (eq (current-local-map) read-passwd-map)) - ;; (setq-local corfu-auto nil) ;; Enable/disable auto completion - (setq-local corfu-echo-delay nil ;; Disable automatic echo and popup - corfu-popupinfo-delay nil) - (corfu-mode 1))) -(add-hook 'minibuffer-setup-hook #'corfu-enable-always-in-minibuffer 1) +(setq global-corfu-minibuffer + (lambda () + (not (or (bound-and-true-p mct--active) + (bound-and-true-p vertico--input) + (eq (current-local-map) read-passwd-map))))) #+end_src ** Completing in the Eshell or Shell @@ -1348,7 +1348,7 @@ Quit if no candidate is selected." (kill-local-variable 'completion-in-region-function)))) (defcustom global-corfu-modes t - "List of modes where Corfu should be enabled. + "List of modes where Corfu should be enabled by `global-corfu-mode'. The variable can either be t, nil or a list of t, nil, mode symbols or elements of the form (not modes). Examples: - Enable everywhere, except in Org: ((not org-mode) t). @@ -1356,22 +1356,37 @@ symbols or elements of the form (not modes). Examples: - Enable only in text modes: (text-mode)." :type '(choice (const t) (repeat sexp))) +(defcustom global-corfu-minibuffer t + "Corfu should be enabled in the minibuffer by `global-corfu-mode'. +The variable can either be t, nil or a custom predicate function. If +the variable is set to t, Corfu is only enabled if the minibuffer has +local `completion-at-point-functions'." + :type '(choice (const t) (const nil) function)) + ;;;###autoload (define-globalized-minor-mode global-corfu-mode corfu-mode corfu--on - :group 'corfu) + :group 'corfu + (remove-hook 'minibuffer-setup-hook #'corfu--on) + (when (and global-corfu-mode global-corfu-minibuffer) + (add-hook 'minibuffer-setup-hook #'corfu--on 100))) (defun corfu--on () - "Turn `corfu-mode' on." - (when (and (not (or noninteractive (eq (aref (buffer-name) 0) ?\s))) - ;; TODO backport `easy-mmode--globalized-predicate-p' - (or (eq t global-corfu-modes) - (eq t (cl-loop for p in global-corfu-modes thereis - (pcase-exhaustive p - ('t t) - ('nil 0) - ((pred symbolp) (and (derived-mode-p p) t)) - (`(not . ,m) (and (seq-some #'derived-mode-p m) 0))))))) + "Enable `corfu-mode' in the current buffer if conditions are satisfied. +See `global-corfu-modes' and `global-corfu-minibuffer'." + (when (or (and (not noninteractive) (minibufferp) global-corfu-minibuffer + (if (functionp global-corfu-minibuffer) + (funcall global-corfu-minibuffer) + (local-variable-p 'completion-at-point-functions))) + (and (not noninteractive) (not (eq (aref (buffer-name) 0) ?\s)) + ;; TODO backport `easy-mmode--globalized-predicate-p' + (or (eq t global-corfu-modes) + (eq t (cl-loop for p in global-corfu-modes thereis + (pcase-exhaustive p + ('t t) + ('nil 0) + ((pred symbolp) (and (derived-mode-p p) t)) + (`(not . ,m) (and (seq-some #'derived-mode-p m) 0)))))))) (corfu-mode 1))) ;; Emacs 28: Do not show Corfu commands with M-X diff --git a/extensions/corfu-echo.el b/extensions/corfu-echo.el index 8ddbd0f..e06ad49 100644 --- a/extensions/corfu-echo.el +++ b/extensions/corfu-echo.el @@ -81,7 +81,8 @@ subsequent delay." :global t :group 'corfu) (cl-defmethod corfu--exhibit :after (&context (corfu-echo-mode (eql t)) &optional _auto) - (if-let ((delay (if (consp corfu-echo-delay) + (if-let (((not (minibufferp))) + (delay (if (consp corfu-echo-delay) (funcall (if corfu-echo--message #'cdr #'car) corfu-echo-delay) corfu-echo-delay)) |
