From 72f8c30ab3b4a9d6428bee957a81741204bbab37 Mon Sep 17 00:00:00 2001 From: Eshel Yaron Date: Wed, 1 Nov 2023 22:32:36 +0100 Subject: [PATCH] Use a trivial completion table instead of a bespoke function Completion at point functions are encouraged to return well formed completion table with metadata instead of a completion function. * completion-preview.el (completion-preview-show): Simplify. (completion-preview-insert-function): (completion-preview--insert) Remove. (completion-preview-insert): New function. (completion-preview-active-mode): Adapt. --- completion-preview.el | 39 ++++++++++++--------------------------- 1 file changed, 12 insertions(+), 27 deletions(-) diff --git a/completion-preview.el b/completion-preview.el index ad3f9cf..9c6b8ea 100644 --- a/completion-preview.el +++ b/completion-preview.el @@ -125,21 +125,20 @@ Compatibility definition for `minibuffer--sort-by-length-alpha'." (prefix (substring string base))) (when last (setcdr last nil) - (let* ((filtered (seq-filter (apply-partially #'string-prefix-p prefix) - all)) + (let* ((filtered + (seq-filter (apply-partially #'string-prefix-p prefix) all)) (sorted (funcall sort-fn filtered)) (multi (cadr sorted)) ; multiple candidates (cand (car sorted))) - (when (and cand - (not (and multi completion-preview-exact-match-only))) + (when (and cand (not (and multi completion-preview-exact-match-only))) (let* ((face (if multi 'completion-preview 'completion-preview-exact)) (after (propertize (substring cand (length prefix)) 'face face))) (unless (string-empty-p after) (add-text-properties 0 1 '(cursor 1) after) (setq completion-preview--overlay (make-overlay end end)) (overlay-put completion-preview--overlay 'after-string after) - (overlay-put completion-preview--overlay 'completion-preview-plist plist) - (overlay-put completion-preview--overlay 'completion-preview-string cand) + (overlay-put completion-preview--overlay 'completion-preview-data + (append (list beg end (list cand)) plist)) (completion-preview-active-mode))))))))))) (defun completion-preview--post-command () @@ -149,33 +148,19 @@ Compatibility definition for `minibuffer--sort-by-length-alpha'." (while-no-input (completion-preview-show)))) -(defun completion-preview--insert () - "Insert the completion preview suggestion." - (let ((after (substring-no-properties - (overlay-get completion-preview--overlay 'after-string))) - (string (overlay-get completion-preview--overlay - 'completion-preview-string)) - (plist (overlay-get completion-preview--overlay - 'completion-preview-plist))) - (completion-preview-active-mode -1) - (insert after) - (when-let ((exit-fn (plist-get plist :exit-function))) - (funcall exit-fn string 'finished)))) - -(defun completion-preview-insert-function () - "Return a function inserting the completion preview." - #'completion-preview--insert) - (define-minor-mode completion-preview-active-mode "Mode for when the completion preview is active." :interactive nil (if completion-preview-active-mode - (add-hook 'completion-at-point-functions - #'completion-preview-insert-function -1 t) - (remove-hook 'completion-at-point-functions - #'completion-preview-insert-function t) + (add-hook 'completion-at-point-functions #'completion-preview-insert -1 t) + (remove-hook 'completion-at-point-functions #'completion-preview-insert t) (completion-preview-hide))) +(defun completion-preview-insert () + "Completion at point function for inserting the current preview." + (when completion-preview-active-mode + (overlay-get completion-preview--overlay 'completion-preview-data))) + ;;;###autoload (define-minor-mode completion-preview-mode "Show in-buffer completion preview as you type." -- 2.39.2