From: Eshel Yaron Date: Tue, 31 Oct 2023 18:15:41 +0000 (+0100) Subject: Use a dedicated keymap for when the preview active X-Git-Url: http://git.eshelyaron.com/gitweb/?a=commitdiff_plain;h=46d0195f5a552ab16e7dafc6194a1f0c7c76dd38;p=completion-preview.git Use a dedicated keymap for when the preview active * 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. --- diff --git a/completion-preview.el b/completion-preview.el index 1074e12..3d18c65 100644 --- a/completion-preview.el +++ b/completion-preview.el @@ -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