'((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)
(`(,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
(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