]> git.eshelyaron.com Git - completion-preview.git/commitdiff
Use a dedicated keymap for when the preview active
authorEshel Yaron <me@eshelyaron.com>
Tue, 31 Oct 2023 18:15:41 +0000 (19:15 +0100)
committerEshel Yaron <me@eshelyaron.com>
Tue, 31 Oct 2023 18:26:51 +0000 (19:26 +0100)
* completion-preview.el (completion-preview-hide): New function.
(completion-preview-active-mode-map): New keymap.
(completion-preview-active-mode): New minor mode.
(completion-preview--post-command)
(completion-preview-mode)
(completion-preview-show): Adjust.
(completion-preview-insert-or-complete): Remove.
(completion-preview-insert): New command.

completion-preview.el

index 1074e120d8148e61aac37f2d7bdcde2aed4cc1c7..3d18c65f4a66e090331a8e1561cbc558b168abbf 100644 (file)
@@ -81,6 +81,10 @@ all of the functions return non-nil."
   '((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)
@@ -103,9 +107,14 @@ Compatibility definition for `minibuffer--sort-by-length-alpha'."
     (`(,beg . ,end)
      (<= completion-preview-minimum-symbol-length (- end beg)))))
 
+(defun completion-preview-hide ()
+  "Hide the completion preview."
+  (when completion-preview--overlay
+    (delete-overlay completion-preview--overlay)
+    (setq completion-preview--overlay nil)))
+
 (defun completion-preview-show ()
   "Show completion preview with inline overlay after point."
-  (interactive)
   (let ((res (run-hook-wrapped 'completion-at-point-functions
                                #'completion--capf-wrapper 'all)))
     (pcase res
@@ -134,55 +143,52 @@ Compatibility definition for `minibuffer--sort-by-length-alpha'."
                (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))))))))))
+                   (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)
+                   (completion-preview-active-mode)))))))))))
 
 (defun completion-preview--post-command ()
   "Delete the previous completion preview overlay, and maybe show a new one."
-  (when completion-preview--overlay
-    (delete-overlay completion-preview--overlay)
-    (setq completion-preview--overlay nil))
+  (completion-preview-active-mode -1)
   (when (run-hook-with-args-until-failure 'completion-preview-hook)
     (while-no-input
       (completion-preview-show))))
 
-(defun completion-preview-insert-or-complete (complete-p)
-  "Insert the completion preview candidate.
-If there is no completion preview, or when COMPLETE-P is non-nil,
-invoke `completion-at-point' instead.  Interactively, COMPLETE-P
-is the prefix argument."
-  (interactive "P")
-  (if (or complete-p (not completion-preview--overlay))
-      (completion-at-point)
-    (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)))
-      (delete-overlay completion-preview--overlay)
-      (setq completion-preview--overlay nil)
-      (insert after)
-      (when-let ((exit-fn (plist-get plist :exit-function)))
-        (funcall exit-fn string 'finished)))))
-
-(defvar-keymap completion-preview-mode-map
-  :doc "Keymap for Completion Preview mode."
-  "C-M-i" #'completion-preview-insert-or-complete)
+(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")))
 
 ;;;###autoload
 (define-minor-mode completion-preview-mode
   "Show in-buffer completion preview as you type."
-  :lighter " CP"
+  :lighter (:propertize " CP"
+                        help-echo "Completion Preview"
+                        mouse-face mode-line-highlight)
   (if completion-preview-mode
       (add-hook 'post-command-hook #'completion-preview--post-command nil t)
     (remove-hook 'post-command-hook #'completion-preview--post-command t)
-    (when completion-preview--overlay
-      (delete-overlay completion-preview--overlay)
-      (setq completion-preview--overlay nil))))
+    (completion-preview-active-mode -1)))
 
 (provide 'completion-preview)
 ;;; completion-preview.el ends here