all messages for Emacs-related lists mirrored at yhetil.org
 help / color / mirror / code / Atom feed
From: Stefan Monnier <monnier@iro.umontreal.ca>
To: Okamsn <okamsn@protonmail.com>
Cc: Philip Kaludercic <philipk@posteo.net>,
	 North Year <ny-ml@outlook.com>, Visuwesh <visuweshm@gmail.com>,
	 emacs-devel@gnu.org
Subject: Re: Adding the `prescient` packages to NonGNU ELPA?
Date: Sat, 17 Dec 2022 13:39:31 -0500	[thread overview]
Message-ID: <jwvbko1zyt2.fsf-monnier+emacs@gnu.org> (raw)
In-Reply-To: <c5b31d34-9c3a-f02f-5e55-6def20699175@protonmail.com> (okamsn@protonmail.com's message of "Sat, 17 Dec 2022 18:01:18 +0000")

[-- Attachment #1: Type: text/plain, Size: 664 bytes --]

> 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

[-- Warning: decoded text below may be mangled, UTF-8 assumed --]
[-- Attachment #2: prescient.patch --]
[-- Type: text/x-diff, Size: 13075 bytes --]

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))

  parent reply	other threads:[~2022-12-17 18:39 UTC|newest]

Thread overview: 40+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-11-20  3:27 Adding the `prescient` packages to NonGNU ELPA? Okamsn
2022-11-20  9:24 ` Philip Kaludercic
2022-11-20 11:23   ` Stefan Kangas
2022-11-20 15:19     ` Stefan Monnier
2022-11-20 15:41       ` Philip Kaludercic
2022-11-21 21:17         ` Richard Stallman
2022-11-22 13:53           ` Akib Azmain Turja
2022-11-23 23:12             ` okamsn
2022-11-26  0:50               ` Richard Stallman
2022-11-20 17:10   ` Visuwesh
2022-11-20 18:39     ` Stefan Monnier
2022-12-16  9:41     ` North Year
2022-12-16 19:25       ` Philip Kaludercic
2022-12-17  3:28         ` Stefan Monnier
2022-12-17  9:17           ` Philip Kaludercic
2022-12-17 15:52             ` Stefan Monnier
2022-12-17 16:07               ` Philip Kaludercic
2022-12-17 16:24                 ` Philip Kaludercic
2022-12-17 18:01                   ` Okamsn
2022-12-17 18:08                     ` Philip Kaludercic
2022-12-17 18:39                     ` Stefan Monnier [this message]
2022-12-20  1:32                       ` Okamsn
2022-12-20  3:10                         ` Stefan Monnier
2022-11-20 17:42   ` Okamsn
2022-12-05  0:27     ` Okamsn
2022-12-05 15:21       ` Stefan Monnier
2022-12-09  3:58         ` Okamsn
2022-12-09 15:08           ` Stefan Monnier
2022-12-09 15:27             ` Philip Kaludercic
2022-12-10  4:10             ` Richard Kim
2022-12-10 15:12               ` Stefan Monnier
2022-12-10 16:38                 ` Richard Kim
2022-12-10 17:44                   ` Stefan Monnier
2022-12-05 17:21       ` Philip Kaludercic
2022-12-16  2:04         ` Okamsn
2022-12-16 19:26           ` Philip Kaludercic
2022-11-22 15:41   ` Jonas Bernoulli
2022-11-22 21:09     ` Stefan Monnier
2022-11-23  9:56       ` Jonas Bernoulli
2022-11-23 12:33         ` Stefan Monnier

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=jwvbko1zyt2.fsf-monnier+emacs@gnu.org \
    --to=monnier@iro.umontreal.ca \
    --cc=emacs-devel@gnu.org \
    --cc=ny-ml@outlook.com \
    --cc=okamsn@protonmail.com \
    --cc=philipk@posteo.net \
    --cc=visuweshm@gmail.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this external index

	https://git.savannah.gnu.org/cgit/emacs.git
	https://git.savannah.gnu.org/cgit/emacs/org-mode.git

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.