diff --git a/.gitignore b/.gitignore index c531d98..a1a35eb 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,5 @@ *.elc + +# ELPA-generated files. +# /prescient-pkg.el +/prescient-autoloads.el diff --git a/company-prescient.el b/company-prescient.el index 5da64dd..ece2594 100644 --- a/company-prescient.el +++ b/company-prescient.el @@ -6,9 +6,7 @@ ;; Homepage: https://github.com/raxod502/prescient.el ;; Keywords: extensions ;; Created: 7 May 2018 -;; Package-Requires: ((emacs "25.1") (prescient "6.1.0") (company "0.9.6")) ;; SPDX-License-Identifier: MIT -;; Version: 6.1.0 ;;; Commentary: @@ -26,9 +24,14 @@ ;;;; Libraries -(require 'company) +(require 'cl-lib) +(require 'company nil t) (require 'prescient) +(defvar company-completion-finished-hook) +(defvar company-transformers) +(declare-function prescient-completion-sort "ext:prescient" (candidates)) + ;;;; User options (defcustom company-prescient-sort-length-enable :default @@ -41,7 +44,6 @@ this variable is `:default', then this binding is skipped." ;;;; Minor mode -(declare-function prescient-completion-sort "prescient" (candidates)) (defun company-prescient-transformer (candidates) "Candidate transformer function that uses prescient.el to sort CANDIDATES. This is for use in `company-transformers'." @@ -64,17 +66,21 @@ This is for use on `company-completion-finished-hook'.") "Minor mode to use prescient.el in Company completions." :global t :group 'prescient - (if company-prescient-mode + (if (not (featurep 'company)) (progn - (company-prescient-mode -1) - (setq company-prescient-mode t) - (add-to-list 'company-transformers #'company-prescient-transformer) - (add-hook 'company-completion-finished-hook - #'company-prescient-completion-finished)) - (setq company-transformers - (delq #'company-prescient-transformer company-transformers)) + (setq company-prescient-mode nil) + (user-error "`company-prescient-mode': Company not found")) + + ;; Always run removal to clean things up before enabling. + (cl-callf2 remq #'company-prescient-transformer company-transformers) (remove-hook 'company-completion-finished-hook - #'company-prescient-completion-finished))) + #'company-prescient-completion-finished) + + ;; Once cleaned up, if enabling, add things back in. + (when company-prescient-mode + (add-to-list 'company-transformers #'company-prescient-transformer) + (add-hook 'company-completion-finished-hook + #'company-prescient-completion-finished)))) ;;;; Closing remarks diff --git a/corfu-prescient.el b/corfu-prescient.el index 3c064d7..7b3f8e4 100644 --- a/corfu-prescient.el +++ b/corfu-prescient.el @@ -6,9 +6,7 @@ ;; Homepage: https://github.com/radian-software/prescient.el ;; Keywords: extensions ;; Created: 23 Sep 2022 -;; Package-Requires: ((emacs "27.1") (prescient "6.1.0") (corfu "0.28")) ;; SPDX-License-Identifier: MIT -;; Version: 6.1.0 ;;; Commentary: @@ -24,10 +22,20 @@ ;;;; Libraries and Declarations (require 'cl-lib) -(require 'corfu) +(require 'corfu nil t) (require 'prescient) (require 'subr-x) +(defvar corfu--candidates) +(defvar corfu--index) +(defvar corfu--input) +(defvar corfu--state-vars) +(defvar corfu-map) +(defvar corfu-mode) +(defvar corfu-sort-function) +(defvar corfu-sort-override-function) +(declare-function corfu--update "ext:corfu" (&optional interruptible)) + ;;;; Customization (defgroup corfu-prescient nil @@ -80,11 +88,6 @@ by `corfu-prescient-mode'." (corfu--update))) ;;;; Minor mode -(defvar corfu-prescient--old-sort-function nil - "Previous value of `corfu-sort-function'.") - -(defvar corfu-prescient--old-sort-override-function nil - "Previous value of `corfu-sort-override-function'.") (defvar corfu-prescient--old-toggle-binding nil "Previous binding of `M-s' in `corfu-map'.") @@ -132,12 +135,13 @@ by `corfu-prescient-mode'." "Minor mode to use prescient.el in Corfu menus. This mode will: -- if `corfu-prescient-override-sorting' is non-nil, - configure `corfu-sort-override-function' and set - `corfu-prescient-enable-filtering' to t -- if `corfu-prescient-enable-filtering' is non-nil, - configure `corfu-sort-function' +- if `corfu-prescient-override-sorting' is non-nil, override the + function stored in `corfu-sort-override-function' via advice + and set `corfu-prescient-enable-filtering' to t + +- if `corfu-prescient-enable-filtering' is non-nil, override the + function stored in `corfu-sort-function' via advice - if `corfu-prescient-enable-filtering' is non-nil: - bind `prescient-toggle-map' to `M-s' in `corfu-map' @@ -149,72 +153,23 @@ This mode will: - advise `corfu-insert' to remember candidates" :global t :group 'prescient - (if corfu-prescient-mode - ;; Turn on the mode. + (if (not (featurep 'corfu)) (progn - ;; Prevent messing up variables if we explicitly enable the - ;; mode when it's already on. - (corfu-prescient-mode -1) - (setq corfu-prescient-mode t) - - (when corfu-prescient-override-sorting - (setq corfu-prescient-enable-sorting t) - (cl-shiftf corfu-prescient--old-sort-override-function - corfu-sort-override-function - #'prescient-completion-sort)) - - (when corfu-prescient-enable-sorting - (cl-shiftf corfu-prescient--old-sort-function - corfu-sort-function - #'prescient-completion-sort)) - - (when corfu-prescient-enable-filtering - ;; Configure changing settings in the hook. - (add-hook 'corfu-mode-hook - #'corfu-prescient--change-completion-settings) - - ;; Immediately apply the settings in buffers where - ;; `corfu-mode' is already on. - (dolist (b (buffer-list)) - (when (buffer-local-value corfu-mode b) - (with-current-buffer b - (corfu-prescient--apply-completion-settings)))) - - ;; Bind toggling commands. - (setq corfu-prescient--old-toggle-binding - (lookup-key corfu-map (kbd "M-s"))) - (define-key corfu-map (kbd "M-s") prescient-toggle-map) - - ;; Make sure Corfu refreshes immediately. - (add-hook 'prescient--toggle-refresh-functions - #'corfu-prescient--toggle-refresh) - - ;; Clean up the local versions of the toggling variables - ;; after the Corfu pop-up closes. For the toggling vars, it - ;; is the commands themselves that make the variables buffer - ;; local. - (cl-callf cl-union corfu--state-vars prescient--toggle-vars - :test #'eq)) - - ;; While sorting might not be enabled in Corfu, it might - ;; still be enabled in another UI, such as Selectrum or Vertico. - ;; Therefore, we still want to remember candidates. - (advice-add 'corfu--insert :before #'corfu-prescient--remember)) - - ;; Turn off mode. + (setq corfu-prescient-mode nil) + (user-error "`corfu-prescient-mode': Corfu not found")) + ;; Prevent messing up variables if we explicitly enable the + ;; mode when it's already on. + ;; ;; Undo sorting settings. - (when (eq corfu-sort-function #'prescient-completion-sort) - (setq corfu-sort-function corfu-prescient--old-sort-function)) - (when (eq corfu-sort-override-function #'prescient-completion-sort) - (setq corfu-sort-override-function - corfu-prescient--old-sort-override-function)) + (remove-function corfu-sort-function #'prescient-completion-sort) + (remove-function corfu-sort-override-function #'prescient-completion-sort) ;; Unbind toggling commands and unhook refresh function. (when (equal (lookup-key corfu-map (kbd "M-s")) prescient-toggle-map) (define-key corfu-map (kbd "M-s") - corfu-prescient--old-toggle-binding)) + corfu-prescient--old-toggle-binding)) (remove-hook 'prescient--toggle-refresh-functions #'corfu-prescient--toggle-refresh) (cl-callf cl-set-difference corfu--state-vars @@ -230,7 +185,51 @@ This mode will: (corfu-prescient--undo-completion-settings)))) ;; Undo remembrance settings. - (advice-remove 'corfu-insert #'corfu-prescient--remember))) + (advice-remove 'corfu-insert #'corfu-prescient--remember) + + ;; Once cleaned up, if enabling, add things back in. + (when corfu-prescient-mode + (when corfu-prescient-override-sorting + (setq corfu-prescient-enable-sorting t) + (add-function :override corfu-sort-override-function + #'prescient-completion-sort)) + + (when corfu-prescient-enable-sorting + (add-function :override corfu-sort-function + #'prescient-completion-sort)) + + (when corfu-prescient-enable-filtering + ;; Configure changing settings in the hook. + (add-hook 'corfu-mode-hook + #'corfu-prescient--change-completion-settings) + + ;; Immediately apply the settings in buffers where + ;; `corfu-mode' is already on. + (dolist (b (buffer-list)) + (when (buffer-local-value corfu-mode b) + (with-current-buffer b + (corfu-prescient--apply-completion-settings)))) + + ;; Bind toggling commands. + (setq corfu-prescient--old-toggle-binding + (lookup-key corfu-map (kbd "M-s"))) + (define-key corfu-map (kbd "M-s") prescient-toggle-map) + + ;; Make sure Corfu refreshes immediately. + (add-hook 'prescient--toggle-refresh-functions + #'corfu-prescient--toggle-refresh) + + ;; Clean up the local versions of the toggling variables + ;; after the Corfu pop-up closes. For the toggling vars, it + ;; is the commands themselves that make the variables buffer + ;; local. + (cl-callf cl-union corfu--state-vars prescient--toggle-vars + :test #'eq)) + + ;; While sorting might not be enabled in Corfu, it might + ;; still be enabled in another UI, such as Selectrum or Vertico. + ;; Therefore, we still want to remember candidates. + (advice-add 'corfu--insert :before #'corfu-prescient--remember)))) (provide 'corfu-prescient) ;;; corfu-prescient.el ends here diff --git a/ivy-prescient.el b/ivy-prescient.el index 934e731..a484385 100644 --- a/ivy-prescient.el +++ b/ivy-prescient.el @@ -6,9 +6,7 @@ ;; Homepage: https://github.com/raxod502/prescient.el ;; Keywords: extensions ;; Created: 1 May 2018 -;; Package-Requires: ((emacs "25.1") (prescient "6.1.0") (ivy "0.11.0")) ;; SPDX-License-Identifier: MIT -;; Version: 6.1.0 ;;; Commentary: @@ -29,9 +27,28 @@ (require 'map) -(require 'ivy) +(require 'ivy nil t) (require 'prescient) +(defvar ivy--directory) +(defvar ivy--subexps) +(defvar ivy-initial-inputs-alist) +(defvar ivy-last) +(defvar ivy-marked-candidates) +(defvar ivy-re-builders-alist) +(defvar ivy-sort-matches-functions-alist) +(defvar ivy-sort-functions-alist) + +(declare-function ivy--directory-enter "ext:ivy" ()) +(declare-function ivy--get-action "ext:ivy" (collection)) +(declare-function ivy--sort-function "ext:ivy" (collection)) +(declare-function ivy-completion-in-region "ext:ivy" + (start end collection &optional predicate)) +(declare-function ivy-read "ext:ivy" (prompt collection &rest args)) +(declare-function ivy-state-collection "ext:ivy" (struct)) +(declare-function ivy-state-sort "ext:ivy" (struct)) +(declare-function ivy-string< "ext:ivy" (x y)) + ;;;; User options (defcustom ivy-prescient-sort-commands @@ -202,43 +219,49 @@ wasn't in the call to `ivy-read'." "Minor mode to use prescient.el in Ivy menus." :global t :group 'prescient - (if ivy-prescient-mode - (progn - (ivy-prescient-mode -1) - (setq ivy-prescient-mode t) - (when ivy-prescient-enable-filtering - (cl-shiftf ivy-prescient--old-re-builder - (alist-get t ivy-re-builders-alist) - #'ivy-prescient-re-builder) - (cl-shiftf ivy-prescient--old-initial-inputs-alist - ivy-initial-inputs-alist - nil)) - (when ivy-prescient-enable-sorting - ;; Not sure if `map-apply' (note that `map-do' is not - ;; available before Emacs 26) handles mutation of alist - ;; during iteration. Use `map-keys' plus `dolist' to be - ;; safe. - (dolist (caller (map-keys ivy-sort-functions-alist)) - (when (memq (alist-get caller ivy-sort-functions-alist) - '(ivy-string< ivy-sort-file-function-default)) - ;; Use `ignore' to silence byte-compiler. We only use - ;; the setter, not the getter. - (ignore - (cl-shiftf - (alist-get caller ivy-prescient--old-ivy-sort-functions-alist) - (alist-get caller ivy-sort-functions-alist) - #'ivy-prescient-sort-function)))) - (cl-shiftf - ivy-prescient--old-ivy-sort-matches-completion-in-region-function - (alist-get #'ivy-completion-in-region - ivy-sort-matches-functions-alist) - nil) - (advice-add #'ivy-read :filter-args - #'ivy-prescient--enable-sort-commands) - (advice-add #'ivy--directory-enter :filter-return - #'ivy-prescient--remember-directory) - (advice-add #'ivy--get-action :filter-return - #'ivy-prescient--wrap-action))) + (cond + ((not (featurep 'ivy)) + (setq ivy-prescient-mode nil) + (user-error "`ivy-prescient-mode': Ivy not found")) + + (ivy-prescient-mode + (ivy-prescient-mode -1) + (setq ivy-prescient-mode t) + (when ivy-prescient-enable-filtering + (cl-shiftf ivy-prescient--old-re-builder + (alist-get t ivy-re-builders-alist) + #'ivy-prescient-re-builder) + (cl-shiftf ivy-prescient--old-initial-inputs-alist + ivy-initial-inputs-alist + nil)) + (when ivy-prescient-enable-sorting + ;; Not sure if `map-apply' (note that `map-do' is not + ;; available before Emacs 26) handles mutation of alist + ;; during iteration. Use `map-keys' plus `dolist' to be + ;; safe. + (dolist (caller (map-keys ivy-sort-functions-alist)) + (when (memq (alist-get caller ivy-sort-functions-alist) + '(ivy-string< ivy-sort-file-function-default)) + ;; Use `ignore' to silence byte-compiler. We only use + ;; the setter, not the getter. + (ignore + (cl-shiftf + (alist-get caller ivy-prescient--old-ivy-sort-functions-alist) + (alist-get caller ivy-sort-functions-alist) + #'ivy-prescient-sort-function)))) + (cl-shiftf + ivy-prescient--old-ivy-sort-matches-completion-in-region-function + (alist-get #'ivy-completion-in-region + ivy-sort-matches-functions-alist) + nil) + (advice-add #'ivy-read :filter-args + #'ivy-prescient--enable-sort-commands) + (advice-add #'ivy--directory-enter :filter-return + #'ivy-prescient--remember-directory) + (advice-add #'ivy--get-action :filter-return + #'ivy-prescient--wrap-action))) + + (t (when (equal (alist-get t ivy-re-builders-alist) #'ivy-prescient-re-builder) (setf (alist-get t ivy-re-builders-alist) @@ -260,7 +283,7 @@ wasn't in the call to `ivy-read'." (setf (alist-get (car pair) ivy-initial-inputs-alist) (cdr pair)))) (advice-remove #'ivy-read #'ivy-prescient--enable-sort-commands) (advice-remove #'ivy--directory-enter #'ivy-prescient--remember-directory) - (advice-remove #'ivy--get-action #'ivy-prescient--wrap-action))) + (advice-remove #'ivy--get-action #'ivy-prescient--wrap-action)))) ;;;; Closing remarks diff --git a/prescient-pkg.el b/prescient-pkg.el new file mode 100644 index 0000000..bbf9157 --- /dev/null +++ b/prescient-pkg.el @@ -0,0 +1,5 @@ +(define-package "prescient" "6.1.0" + "Better sorting and filtering" + '((emacs "25.1")) + :homepage "https://github.com/raxod502/prescient.el" + :keywords '("extensions")) diff --git a/selectrum-prescient.el b/selectrum-prescient.el index dd645e1..5ac7291 100644 --- a/selectrum-prescient.el +++ b/selectrum-prescient.el @@ -6,9 +6,7 @@ ;; Homepage: https://github.com/raxod502/prescient.el ;; Keywords: extensions ;; Created: 8 Dec 2019 -;; Package-Requires: ((emacs "25.1") (prescient "6.1.0") (selectrum "3.1")) ;; SPDX-License-Identifier: MIT -;; Version: 6.1.0 ;;; Commentary: @@ -24,9 +22,17 @@ ;;;; Libraries (require 'prescient) -(require 'selectrum) +(require 'selectrum nil t) (require 'subr-x) +(defvar selectrum-refine-candidates-function) +(defvar selectrum-preprocess-candidates-function) +(defvar selectrum-highlight-candidates-function) +(defvar selectrum-minibuffer-map) +(defvar selectrum-should-sort) +(defvar selectrum-is-active) +(declare-function selectrum-exhibit "ext:selectrum" ()) + ;;;; Customization (defgroup selectrum-prescient nil @@ -114,55 +120,49 @@ For use on `selectrum-candidate-selected-hook'." "Minor mode to use prescient.el in Selectrum menus." :global t :group 'prescient - (if selectrum-prescient-mode + (if (not (featurep 'selectrum)) (progn - ;; Prevent messing up variables if we explicitly enable the - ;; mode when it's already on. - (selectrum-prescient-mode -1) - (setq selectrum-prescient-mode t) - (when selectrum-prescient-enable-filtering - (setq selectrum-prescient--old-refine-function - selectrum-refine-candidates-function) - (setq selectrum-prescient--old-highlight-function - selectrum-highlight-candidates-function) - (setq selectrum-refine-candidates-function - #'selectrum-prescient--refine) - (setq selectrum-highlight-candidates-function - #'prescient--highlight-matches) - (define-key selectrum-minibuffer-map - (kbd "M-s") prescient-toggle-map) - (add-hook 'prescient--toggle-refresh-functions - #'selectrum-prescient--toggle-refresh)) - (when selectrum-prescient-enable-sorting - (setq selectrum-prescient--old-preprocess-function - selectrum-preprocess-candidates-function) - (setq selectrum-preprocess-candidates-function - #'selectrum-prescient--preprocess) - (add-hook 'selectrum-candidate-selected-hook - #'selectrum-prescient--remember) - (add-hook 'selectrum-candidate-inserted-hook - #'selectrum-prescient--remember))) - (when (eq selectrum-refine-candidates-function - #'selectrum-prescient--refine) - (setq selectrum-refine-candidates-function - selectrum-prescient--old-refine-function)) - (when (eq selectrum-highlight-candidates-function - #'prescient--highlight-matches) - (setq selectrum-highlight-candidates-function - selectrum-prescient--old-highlight-function)) + (setq selectrum-prescient-mode nil) + (user-error "`selectrum-prescient-mode': Selectrum not found")) + + ;; Prevent messing up variables if we explicitly enable the + ;; mode when it's already on. + (remove-function selectrum-refine-candidates-function + #'selectrum-prescient--refine) + (remove-function selectrum-highlight-candidates-function + #'prescient--highlight-matches) + (remove-function selectrum-preprocess-candidates-function + #'selectrum-prescient--preprocess) + (when (equal (lookup-key selectrum-minibuffer-map (kbd "M-s")) prescient-toggle-map) (define-key selectrum-minibuffer-map (kbd "M-s") nil)) + (remove-hook 'prescient--toggle-refresh-functions #'selectrum-prescient--toggle-refresh) (remove-hook 'selectrum-candidate-selected-hook #'selectrum-prescient--remember) (remove-hook 'selectrum-candidate-inserted-hook #'selectrum-prescient--remember) - (when (eq selectrum-preprocess-candidates-function - #'selectrum-prescient--preprocess) - (setq selectrum-preprocess-candidates-function - selectrum-prescient--old-preprocess-function)))) + + ;; Once cleaned up, if enabling, add things back in. + (when selectrum-prescient-enable-filtering + (add-function :override selectrum-refine-candidates-function + #'selectrum-prescient--refine) + (add-function :override selectrum-highlight-candidates-function + #'prescient--highlight-matches) + (define-key selectrum-minibuffer-map + (kbd "M-s") prescient-toggle-map) + (add-hook 'prescient--toggle-refresh-functions + #'selectrum-prescient--toggle-refresh)) + + (when selectrum-prescient-enable-sorting + (add-function :override selectrum-preprocess-candidates-function + #'selectrum-prescient--preprocess) + (add-hook 'selectrum-candidate-selected-hook + #'selectrum-prescient--remember) + (add-hook 'selectrum-candidate-inserted-hook + #'selectrum-prescient--remember)))) ;;;; Closing remarks diff --git a/vertico-prescient.el b/vertico-prescient.el index 913311f..8075795 100644 --- a/vertico-prescient.el +++ b/vertico-prescient.el @@ -6,9 +6,9 @@ ;; Homepage: https://github.com/radian-software/prescient.el ;; Keywords: extensions ;; Created: 23 Sep 2022 -;; Package-Requires: ((emacs "27.1") (prescient "6.1.0") (vertico "0.28")) ;; SPDX-License-Identifier: MIT -;; Version: 6.1.0 + +;; LocalWords: Vertico's ;;; Commentary: @@ -26,7 +26,17 @@ (eval-when-compile (require 'cl-lib)) (require 'prescient) (require 'subr-x) -(require 'vertico) +(require 'vertico nil t) + +(defvar vertico--candidates) +(defvar vertico--history-hash) +(defvar vertico--index) +(defvar vertico--input) +(defvar vertico--lock-candidate) +(defvar vertico-map) +(defvar vertico-sort-function) +(defvar vertico-sort-override-function) +(declare-function vertico--exhibit "ext:vertico" ()) ;;;; Customization @@ -82,13 +92,6 @@ by `vertico-prescient-mode'." (vertico--exhibit))) ;;;; Minor mode - -(defvar vertico-prescient--old-sort-function nil - "Previous value of `vertico-sort-function'.") - -(defvar vertico-prescient--old-sort-override-function nil - "Previous value of `vertico-sort-override-function'.") - (defvar vertico-prescient--old-toggle-binding nil "Previous binding of `M-s' in `vertico-map'.") @@ -125,12 +128,13 @@ by `vertico-prescient-mode'." "Minor mode to use prescient.el in Vertico menus. This mode will: -- if `vertico-prescient-override-sorting' is non-nil, - configure `vertico-sort-override-function' and set - `vertico-prescient-enable-filtering' to t -- if `vertico-prescient-enable-filtering' is non-nil, - configure `vertico-sort-function' +- if `vertico-prescient-override-sorting' is non-nil, override + the function stored in `vertico-sort-override-function' via + advice and set `vertico-prescient-enable-filtering' to t + +- if `vertico-prescient-enable-filtering' is non-nil, override + the function stored in `vertico-sort-function' via advice - if `vertico-prescient-enable-filtering' is non-nil: - bind `prescient-toggle-map' to `M-s' in `vertico-map' @@ -142,58 +146,25 @@ This mode will: - advise `vertico-insert' to remember candidates" :global t :group 'prescient - (if vertico-prescient-mode - ;; Turn on the mode. + (if (not (featurep 'vertico)) (progn - ;; Prevent messing up variables if we explicitly enable the - ;; mode when it's already on. - (vertico-prescient-mode -1) - (setq vertico-prescient-mode t) - - (when vertico-prescient-override-sorting - (setq vertico-prescient-enable-sorting t) - (cl-shiftf vertico-prescient--old-sort-override-function - vertico-sort-override-function - #'prescient-completion-sort)) - - (when vertico-prescient-enable-sorting - (cl-shiftf vertico-prescient--old-sort-function - vertico-sort-function - #'prescient-completion-sort)) - - (when vertico-prescient-enable-filtering - ;; Configure completion settings. - (advice-add 'vertico--setup - :after #'vertico-prescient--apply-completion-settings) - - ;; Bind toggling commands. - (setq vertico-prescient--old-toggle-binding - (lookup-key vertico-map (kbd "M-s"))) - (define-key vertico-map (kbd "M-s") prescient-toggle-map) - - ;; Make sure Vertico refreshes immediately. - (add-hook 'prescient--toggle-refresh-functions - #'vertico-prescient--toggle-refresh)) - - ;; While sorting might not be enabled in Vertico, it might - ;; still be enabled in another UI, such as Company or Corfu. - ;; Therefore, we still want to remember candidates. - (advice-add 'vertico-insert :before #'vertico-prescient--remember)) - - ;; Turn off mode. + (setq vertico-prescient-mode nil) + (user-error "`vertico-prescient-mode': Vertico not found")) + ;; Prevent messing up variables if we explicitly enable the + ;; mode when it's already on. + ;; ;; Undo sorting settings. - (when (eq vertico-sort-function #'prescient-completion-sort) - (setq vertico-sort-function vertico-prescient--old-sort-function)) - (when (eq vertico-sort-override-function #'prescient-completion-sort) - (setq vertico-sort-override-function - vertico-prescient--old-sort-override-function)) + (remove-function vertico-sort-function + #'prescient-completion-sort) + (remove-function vertico-sort-override-function + #'prescient-completion-sort) ;; Unbind toggling commands and unhook refresh function. (when (equal (lookup-key vertico-map (kbd "M-s")) prescient-toggle-map) (define-key vertico-map (kbd "M-s") - vertico-prescient--old-toggle-binding)) + vertico-prescient--old-toggle-binding)) (remove-hook 'prescient--toggle-refresh-functions #'vertico-prescient--toggle-refresh) @@ -206,9 +177,38 @@ This mode will: (vertico-prescient--undo-completion-settings)))) ;; Undo remembrance settings. - (advice-remove 'vertico-insert #'vertico-prescient--remember))) + (advice-remove 'vertico-insert #'vertico-prescient--remember) + + ;; Once cleaned up, if enabling, add things back in. + (when vertico-prescient-mode + + (when vertico-prescient-override-sorting + (setq vertico-prescient-enable-sorting t) + (add-function :override vertico-sort-override-function + #'prescient-completion-sort)) + + (when vertico-prescient-enable-sorting + (add-function :override vertico-sort-function + #'prescient-completion-sort)) + + (when vertico-prescient-enable-filtering + ;; Configure completion settings. + (advice-add 'vertico--setup + :after #'vertico-prescient--apply-completion-settings) + + ;; Bind toggling commands. + (setq vertico-prescient--old-toggle-binding + (lookup-key vertico-map (kbd "M-s"))) + (define-key vertico-map (kbd "M-s") prescient-toggle-map) + + ;; Make sure Vertico refreshes immediately. + (add-hook 'prescient--toggle-refresh-functions + #'vertico-prescient--toggle-refresh)) + + ;; While sorting might not be enabled in Vertico, it might + ;; still be enabled in another UI, such as Company or Corfu. + ;; Therefore, we still want to remember candidates. + (advice-add 'vertico-insert :before #'vertico-prescient--remember)))) (provide 'vertico-prescient) ;;; vertico-prescient.el ends here - -;; LocalWords: Vertico's