Skip to content

Commit

Permalink
Address recommendations of Stefan Monnier for adding to Non-GNU ELPA.
Browse files Browse the repository at this point in the history
These recommendations were made to better support the ELPA method of
fetching and delivering packages (expecting one package per repo)
and of testing builds (not erroring for the optional dependencies of
the now single package).

- Make `require` not signal an error if missing Vertico, Corfu, or
  Company.
- Use `featurep` to detect whether requirements are installed.
- Add ELPA-generated files to `gitignore`.
- Improve declarations and add more.
  - Mark external packages with "ext" in `declare-function`.
- Remove package requirements from the extension files.
- Add `prescient-pkg.el`.
  - Adding the file to the repo (instead of ELPA generating it) seems to
    work better for testing.
  - Remove version information and package requirements from the
    individual extension files.

  These two changes allow the command `package-install-from-buffer` in a
  Dired buffer to work better. No other changes to the files' preambles
  seem needed.
  • Loading branch information
okamsn committed Jul 25, 2023
1 parent e0cca29 commit 4e29b4f
Show file tree
Hide file tree
Showing 7 changed files with 267 additions and 230 deletions.
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,5 @@
*.elc

# ELPA-generated files.
# /prescient-pkg.el
/prescient-autoloads.el
32 changes: 19 additions & 13 deletions company-prescient.el
Original file line number Diff line number Diff line change
Expand Up @@ -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:

Expand All @@ -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
Expand All @@ -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'."
Expand All @@ -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

Expand Down
143 changes: 71 additions & 72 deletions corfu-prescient.el
Original file line number Diff line number Diff line change
Expand Up @@ -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:

Expand All @@ -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
Expand Down Expand Up @@ -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'.")
Expand Down Expand Up @@ -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'
Expand All @@ -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
Expand All @@ -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
Loading

0 comments on commit 4e29b4f

Please sign in to comment.