diff options
| author | Daniel Mendler <mail@daniel-mendler.de> | 2023-02-02 21:56:27 +0100 |
|---|---|---|
| committer | Daniel Mendler <mail@daniel-mendler.de> | 2023-02-02 21:56:27 +0100 |
| commit | b11a206c6e3a38d1dd91d29a72ff128580d35174 (patch) | |
| tree | cf2b7d437a199c607c13d0c16765d8eaa6e1447b /extensions/corfu-indexed.el | |
| parent | a10200f11c09df6263b76992f0dcbbbb0fad4230 (diff) | |
corfu-indexed-mode: Do not use advices
Diffstat (limited to 'extensions/corfu-indexed.el')
| -rw-r--r-- | extensions/corfu-indexed.el | 32 |
1 files changed, 12 insertions, 20 deletions
diff --git a/extensions/corfu-indexed.el b/extensions/corfu-indexed.el index 6ee832b..8621247 100644 --- a/extensions/corfu-indexed.el +++ b/extensions/corfu-indexed.el @@ -56,18 +56,17 @@ '(corfu-insert corfu-complete) "Commands that should be indexed.") -(defun corfu-indexed--handle-prefix (orig &rest args) - "Handle prefix argument before calling ORIG function with ARGS." - (if (and current-prefix-arg (called-interactively-p t)) - (let ((corfu--index (+ corfu--scroll - (- (prefix-numeric-value current-prefix-arg) - corfu-indexed-start)))) - (if (or (< corfu--index 0) - (>= corfu--index corfu--total) - (>= corfu--index (+ corfu--scroll corfu-count))) - (message "Out of range") - (funcall orig))) - (apply orig args))) +(cl-defmethod corfu--prepare :before (&context (corfu-indexed-mode (eql t))) + (when (and prefix-arg (memq this-command corfu-indexed--commands)) + (let ((index (+ corfu--scroll + (- (prefix-numeric-value prefix-arg) + corfu-indexed-start)))) + (if (and (>= index 0) + (< index corfu--total) + (< index (+ corfu--scroll corfu-count))) + (setq corfu--index index) + (message "Out of range") + (setq this-command #'ignore))))) (cl-defmethod corfu--affixate :around (cands &context (corfu-indexed-mode (eql t))) (setq cands (cdr (cl-call-next-method cands))) @@ -92,14 +91,7 @@ ;;;###autoload (define-minor-mode corfu-indexed-mode "Prefix candidates with indices." - :global t :group 'corfu - ;; TODO I had forgotten that `corfu-indexed-mode' is double evil, since it - ;; uses advices and the forbidden function `called-interactively-p'. Find a - ;; better implementation which avoids these kludges. - (dolist (cmd corfu-indexed--commands) - (if corfu-indexed-mode - (advice-add cmd :around #'corfu-indexed--handle-prefix) - (advice-remove cmd #'corfu-indexed--handle-prefix)))) + :global t :group 'corfu) (provide 'corfu-indexed) ;;; corfu-indexed.el ends here |
