]> git.eshelyaron.com Git - completion-preview.git/commitdiff
Use a dedicated capf instead of rebinding 'C-M-i'
authorEshel Yaron <me@eshelyaron.com>
Wed, 1 Nov 2023 11:35:36 +0000 (12:35 +0100)
committerEshel Yaron <me@eshelyaron.com>
Wed, 1 Nov 2023 11:41:26 +0000 (12:41 +0100)
Instead of rebinding 'C-M-i' to a command that inserts the suggested
completion, we make 'completion-at-point' do the right thing when the
preview is active by temporarily adding a prioritized capf.

Thanks to João Távora for suggesting this approach.

* completion-preview.el (completion-preview-active-mode-map): Remove.
(completion-preview--insert):
(completion-preview-insert-function): New functions.
(completion-preview-active-mode): Adjust.

completion-preview.el

index 767d61f7ce291d48d6af35369510defeb592ecbe..5ce795aad0dfcbd9c2fda56d94396c428290e717 100644 (file)
@@ -85,10 +85,6 @@ the default value that you specify in this option.")
   '((t :underline t :inherit completion-preview))
   "Face for exact completion preview overlay.")
 
-(defvar-keymap completion-preview-active-mode-map
-  :doc "Keymap for Completion Preview Active mode."
-  "C-M-i" #'completion-preview-insert)
-
 (defvar-local completion-preview--overlay nil)
 
 (defun completion-preview--sort-by-length-alpha (elems)
@@ -161,27 +157,32 @@ 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
-  (unless completion-preview-active-mode (completion-preview-hide))
-  (force-mode-line-update))
-
-(defun completion-preview-insert ()
-  "Insert the completion preview suggestion."
-  (interactive)
   (if completion-preview-active-mode
-      (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)))
-    (user-error "No active completion preview")))
+      (add-hook 'completion-at-point-functions
+                #'completion-preview-insert-function -1 t)
+    (remove-hook 'completion-at-point-functions
+                 #'completion-preview-insert-function t)
+    (completion-preview-hide)))
 
 ;;;###autoload
 (define-minor-mode completion-preview-mode