From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.io!.POSTED.blaine.gmane.org!not-for-mail From: Stefan Monnier Newsgroups: gmane.emacs.devel Subject: Re: Adding the `prescient` packages to NonGNU ELPA? Date: Sat, 17 Dec 2022 13:39:31 -0500 Message-ID: References: <16193c73-ab80-04c9-558f-d5e6142f38f3@protonmail.com> <871qpydllo.fsf@posteo.net> <874jutft6g.fsf@gmail.com> <87pmcj2lsg.fsf@posteo.net> <877cyq5qym.fsf@posteo.net> <87pmcivwrq.fsf@posteo.net> <87iliavw08.fsf@posteo.net> Mime-Version: 1.0 Content-Type: multipart/mixed; boundary="=-=-=" Injection-Info: ciao.gmane.io; posting-host="blaine.gmane.org:116.202.254.214"; logging-data="14290"; mail-complaints-to="usenet@ciao.gmane.io" User-Agent: Gnus/5.13 (Gnus v5.13) Cc: Philip Kaludercic , North Year , Visuwesh , emacs-devel@gnu.org To: Okamsn Original-X-From: emacs-devel-bounces+ged-emacs-devel=m.gmane-mx.org@gnu.org Sat Dec 17 19:40:08 2022 Return-path: Envelope-to: ged-emacs-devel@m.gmane-mx.org Original-Received: from lists.gnu.org ([209.51.188.17]) by ciao.gmane.io with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1p6c6S-0003UE-3A for ged-emacs-devel@m.gmane-mx.org; Sat, 17 Dec 2022 19:40:08 +0100 Original-Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1p6c64-0002p8-Qt; Sat, 17 Dec 2022 13:39:45 -0500 Original-Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1p6c60-0002oi-IT for emacs-devel@gnu.org; Sat, 17 Dec 2022 13:39:41 -0500 Original-Received: from mailscanner.iro.umontreal.ca ([132.204.25.50]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1p6c5y-0006FG-8r for emacs-devel@gnu.org; Sat, 17 Dec 2022 13:39:40 -0500 Original-Received: from pmg2.iro.umontreal.ca (localhost.localdomain [127.0.0.1]) by pmg2.iro.umontreal.ca (Proxmox) with ESMTP id 86EBC8056A; Sat, 17 Dec 2022 13:39:36 -0500 (EST) Original-Received: from mail01.iro.umontreal.ca (unknown [172.31.2.1]) by pmg2.iro.umontreal.ca (Proxmox) with ESMTP id B5432807EF; Sat, 17 Dec 2022 13:39:33 -0500 (EST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=iro.umontreal.ca; s=mail; t=1671302373; bh=bOrh7grp+1JX8Kh9VREFXvMJTn6g9d6oDjkbhxSR6jw=; h=From:To:Cc:Subject:In-Reply-To:References:Date:From; b=SdY9LqeM7XlvCiqmRf7IFtAt7SHUrQEFiXcCUr40fy/Jx3o/73Ys1RcdilXLofxkJ tV0MKw9KMmnbSDy+g8MugRsynqgXS19JjmCrQUUensxsT2C7q5lO1gEwK+OY5//SAM +6rMnZ8wQCYbTm4Sco6O4ejm2cQriZbVciAH4j68i8wTfE4qRL/DKD35kLB9Io27jp mbhW/S54wsEzlzcIlSFANvcA5qlHmqQwcDlM3HWHA4UG0DUBQJaaofGGRCynLgq2Id QJRScc2xDWVFwbcglgseed5Y1CaU41fgiDN2xdaUhrk5soEVLrdTs+rGgtX2Q7rNYU 8kM19RqXeIMyg== Original-Received: from pastel (unknown [45.72.193.52]) by mail01.iro.umontreal.ca (Postfix) with ESMTPSA id 2D5D3120171; Sat, 17 Dec 2022 13:39:33 -0500 (EST) In-Reply-To: (okamsn@protonmail.com's message of "Sat, 17 Dec 2022 18:01:18 +0000") Received-SPF: pass client-ip=132.204.25.50; envelope-from=monnier@iro.umontreal.ca; helo=mailscanner.iro.umontreal.ca X-Spam_score_int: -42 X-Spam_score: -4.3 X-Spam_bar: ---- X-Spam_report: (-4.3 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, RCVD_IN_DNSWL_MED=-2.3, SPF_HELO_NONE=0.001, SPF_PASS=-0.001 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: emacs-devel@gnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: "Emacs development discussions." List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: emacs-devel-bounces+ged-emacs-devel=m.gmane-mx.org@gnu.org Original-Sender: emacs-devel-bounces+ged-emacs-devel=m.gmane-mx.org@gnu.org Xref: news.gmane.io gmane.emacs.devel:301579 Archived-At: --=-=-= Content-Type: text/plain > 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 `-prescient-mode-hook` as well as confuse Custom's tracking of whether the mode was set (and whether pragmatically or not)). Stefan --=-=-= Content-Type: text/x-diff Content-Disposition: inline; filename=prescient.patch 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)) --=-=-=--