emacs-devel
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Adding the `prescient` packages to NonGNU ELPA?


From: Stefan Monnier
Subject: Re: Adding the `prescient` packages to NonGNU ELPA?
Date: Sat, 17 Dec 2022 13:39:31 -0500
User-agent: Gnus/5.13 (Gnus v5.13)

> Is there a way to fix this while still declaring the extension packages'
> requirements?

Usually the way we expect this to work is:

- the user install prescient
- the user installs company/corfu/younameit
- it just works

Sample (and incomplete) patch below (a lot of it is unrelated,
e.g. I make it use `add-function` instead of manually saving the old
function and restoring it, and I move the "turn off the mode" to the
beginning instead of the weird recursive call which would inevitably
mess up anyone using `<foo>-prescient-mode-hook` as well as confuse
Custom's tracking of whether the mode was set (and whether pragmatically
or not)).


        Stefan
diff --git a/.gitignore b/.gitignore
index c531d9867f..705768703a 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 5da64dd49e..01915cd9f5 100644
--- a/company-prescient.el
+++ b/company-prescient.el
@@ -26,7 +26,7 @@
 
 ;;;; Libraries
 
-(require 'company)
+(require 'company nil t)
 (require 'prescient)
 
 ;;;; User options
@@ -64,17 +64,19 @@ 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
-      (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))
+  (cond
+   ((boundp 'company-transformers)
     (setq company-transformers
           (delq #'company-prescient-transformer company-transformers))
     (remove-hook 'company-completion-finished-hook
-                 #'company-prescient-completion-finished)))
+                 #'company-prescient-completion-finished)
+    (when company-prescient-mode
+      (add-to-list 'company-transformers #'company-prescient-transformer)
+      (add-hook 'company-completion-finished-hook
+                #'company-prescient-completion-finished)))
+   (t
+    (message "company-prescient-mode: Company not found!")
+    (setq company-prescient-mode nil))))
 
 ;;;; Closing remarks
 
diff --git a/corfu-prescient.el b/corfu-prescient.el
index 3c064d7aee..5bf774d2fc 100644
--- a/corfu-prescient.el
+++ b/corfu-prescient.el
@@ -24,7 +24,10 @@
 ;;;; Libraries and Declarations
 
 (require 'cl-lib)
-(require 'corfu)
+(require 'corfu nil t)
+(defvar corfu--input)
+(defvar corfu--index)
+(defvar corfu--candidates)
 (require 'prescient)
 (require 'subr-x)
 
@@ -38,13 +41,11 @@
 
 (defcustom corfu-prescient-enable-filtering t
   "Whether the `prescient' completion style is used in Corfu."
-  :type 'boolean
-  :group 'corfu-prescient)
+  :type 'boolean)
 
 (defcustom corfu-prescient-enable-sorting t
   "Whether prescient.el sorting is used in Corfu."
-  :type 'boolean
-  :group 'corfu-prescient)
+  :type 'boolean)
 
 (defcustom corfu-prescient-override-sorting nil
   "Whether to force sorting by `corfu-prescient'.
@@ -55,19 +56,16 @@ If non-nil, then `corfu-prescient-mode' sets
 
 Changing this variable will not take effect until
 `corfu-prescient-mode' has been reloaded."
-  :group 'corfu-prescient
   :type 'boolean)
 
 (defcustom corfu-prescient-completion-styles
   prescient--completion-recommended-styles
   "The completion styles used by `corfu-prescient-mode'."
-  :group 'corfu-prescient
   :type '(repeat symbol))
 
 (defcustom corfu-prescient-completion-category-overrides
   prescient--completion-recommended-overrides
   "The completion-category overrides used by `corfu-prescient-mode'."
-  :group 'corfu-prescient
   :type '(repeat (cons symbol (repeat (cons symbol (repeat symbol))))))
 
 ;;;; Toggling Commmands
@@ -80,11 +78,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'.")
@@ -149,72 +142,18 @@ This mode will:
 - advise `corfu-insert' to remember candidates"
   :global t
   :group 'prescient
-  (if corfu-prescient-mode
-      ;; Turn on the mode.
-      (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.
 
+  (cond
+   ((boundp 'corfu-map)
     ;; 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 +169,55 @@ 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)
+
+    (when corfu-prescient-mode
+      ;; Turn on the 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)))
+   (t
+    (message "corfu-prescient-mode: Corfu not found!")
+    (setq corfu-prescient-mode nil))))
+
 
 (provide 'corfu-prescient)
 ;;; corfu-prescient.el ends here
diff --git a/selectrum-prescient.el b/selectrum-prescient.el
index dd645e1272..07caaeb3d4 100644
--- a/selectrum-prescient.el
+++ b/selectrum-prescient.el
@@ -24,7 +24,8 @@
 ;;;; Libraries
 
 (require 'prescient)
-(require 'selectrum)
+(require 'selectrum nil t)
+(defvar selectrum-refine-candidates-function)
 (require 'subr-x)
 
 ;;;; Customization
@@ -42,7 +43,6 @@ filtering behavior of Selectrum from the default. See 
Selectrum
 documentation for how to configure filtering yourself. Changing
 this variable will not take effect until
 `selectrum-prescient-mode' has been reloaded."
-  :group 'selectrum-prescient
   :type 'boolean)
 
 (defcustom selectrum-prescient-enable-sorting t
@@ -52,7 +52,6 @@ sorting behavior of Selectrum from the default. See Selectrum
 documentation for how to configure sorting yourself. Changing
 this variable will not take effect until
 `selectrum-prescient-mode' has been reloaded."
-  :group 'selectrum-prescient
   :type 'boolean)
 
 ;;;; Toggling commands
@@ -98,17 +97,11 @@ matches first."
 (defvar selectrum-prescient--old-preprocess-function nil
   "Previous value of `selectrum-preprocess-candidates-function'.")
 
-(defvar selectrum-prescient--old-refine-function nil
-  "Previous value of `selectrum-refine-candidates-function'.")
-
 (defun selectrum-prescient--remember (candidate &rest _)
   "Remember CANDIDATE in prescient.el.
 For use on `selectrum-candidate-selected-hook'."
   (prescient-remember candidate))
 
-(defvar selectrum-prescient--old-highlight-function nil
-  "Previous value of `selectrum-highlight-candidates-function'.")
-
 ;;;###autoload
 (define-minor-mode selectrum-prescient-mode
   "Minor mode to use prescient.el in Selectrum menus."
@@ -121,14 +114,10 @@ For use on `selectrum-candidate-selected-hook'."
         (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)
+          (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
@@ -142,14 +131,10 @@ For use on `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))
+    (remove-function selectrum-refine-candidates-function
+                     #'selectrum-prescient--refine)
+    (remove-function selectrum-highlight-candidates-function
+                     #'prescient--highlight-matches)
     (when (equal (lookup-key selectrum-minibuffer-map (kbd "M-s"))
                  prescient-toggle-map)
       (define-key selectrum-minibuffer-map (kbd "M-s") nil))

reply via email to

[Prev in Thread] Current Thread [Next in Thread]