branch: elpa/dirvish commit ed0ab52cdd01f4f43e0763cf2c43bafc4a6893c9 Author: Alex Lu <hellosimon1...@hotmail.com> Commit: Alex Lu <hellosimon1...@hotmail.com>
fix(peek): image & media metadata preview, drop `selectrum` support --- docs/EXTENSIONS.org | 7 +++-- extensions/dirvish-peek.el | 70 ++++++++++++++++++++++++---------------------- 2 files changed, 40 insertions(+), 37 deletions(-) diff --git a/docs/EXTENSIONS.org b/docs/EXTENSIONS.org index 0082c0bd42..0440e1fcf5 100644 --- a/docs/EXTENSIONS.org +++ b/docs/EXTENSIONS.org @@ -103,9 +103,10 @@ saved to .dir-locals.el using ~dirvish-emerge-menu~): * Minibuffer file preview (dirvish-peek.el) -~dirvish-peek-mode~ gives you a preview window when narrowing file candidates -using minibuffer. It displays file preview for all of the file name narrowing -commands in the same way as =dirvish= command. +This extension introduces =dirvish-peek-mode=, a minor mode that enables file +previews within the minibuffer as you narrow down candidates. By leveraging +=dirvish.el= for its core functionality, it delivers a seamless and consistent +preview experience. https://user-images.githubusercontent.com/16313743/158052790-22e6cf49-e18e-435c-908e-f5d91ba316a6.mp4 diff --git a/extensions/dirvish-peek.el b/extensions/dirvish-peek.el index 48e3cd17d2..e47032aafb 100644 --- a/extensions/dirvish-peek.el +++ b/extensions/dirvish-peek.el @@ -9,14 +9,14 @@ ;;; Commentary: -;; `dirvish-peek-mode' gives you file preview when narrowing candidates using minibuffer. +;; This extension introduces `dirvish-peek-mode', a minor mode that enables file +;; previews within the minibuffer as you narrow down candidates. By leveraging +;; `dirvish.el' for its core functionality, it delivers a seamless and +;; consistent preview experience. ;;; Code: (declare-function vertico--candidate "vertico") -(declare-function selectrum--get-candidate "selectrum") -(declare-function selectrum--get-full "selectrum") -(defvar selectrum--current-candidate-index) (declare-function ivy-state-current "ivy") (defvar ivy-last) (require 'dirvish) @@ -30,26 +30,27 @@ being used at runtime." :group 'dirvish :type '(choice function (const nil))) (defcustom dirvish-peek-categories '(file project-file library) - "Minibuffer metadata categories to show file preview." - :group 'dirvish :type '(repeat symbol)) + "Minibuffer metadata categories to show file preview. +For now only `file', `project-file' and `library' are supported. + + - `file': preview files on `find-file' command and friends. + - `project-file': preview files on `project-find-file' command and friends. + - `library': preview files on `find-library' command. + +Notice that the `dirvish-preview-dispatchers' option is respected across +all categories." + :group 'dirvish :type '(repeat :tag "each item can be 'file 'project-file 'library" symbol)) -(defvar dirvish-peek--cand-fetcher nil) (defun dirvish-peek--prepare-cand-fetcher () "Set candidate fetcher according to current completion framework." - (setq dirvish-peek--cand-fetcher - (cond (dirvish-peek-candidate-fetcher - dirvish-peek-candidate-fetcher) - ((bound-and-true-p vertico-mode) #'vertico--candidate) - ((bound-and-true-p selectrum-mode) - (lambda () - (selectrum--get-full - (selectrum--get-candidate - selectrum--current-candidate-index)))) - ((bound-and-true-p ivy-mode) (lambda () (ivy-state-current ivy-last))) - ((bound-and-true-p icomplete-mode) - (lambda () (car completion-all-sorted-completions)))))) + (dirvish-prop :peek-fetcher + (cond (dirvish-peek-candidate-fetcher + dirvish-peek-candidate-fetcher) + ((bound-and-true-p vertico-mode) #'vertico--candidate) + ((bound-and-true-p ivy-mode) (lambda () (ivy-state-current ivy-last))) + ((bound-and-true-p icomplete-mode) + (lambda () (car completion-all-sorted-completions)))))) -(defvar dirvish-peek--curr-category nil) (defun dirvish-peek-setup-h () "Create dirvish minibuffer preview window. The window is created only when metadata in current minibuffer is @@ -62,35 +63,36 @@ one of categories in `dirvish-peek-categories'." (category (completion-metadata-get meta 'category)) (p-category (and (memq category dirvish-peek-categories) category)) new-dv) - (setq dirvish-peek--curr-category p-category) + (dirvish-prop :peek-category p-category) (when p-category (dirvish-peek--prepare-cand-fetcher) (add-hook 'post-command-hook #'dirvish-peek-update-h 90 t) (add-hook 'minibuffer-exit-hook #'dirvish-peek-exit-h nil t) (unless (and dirvish--this (dv-preview-window dirvish--this)) (setq new-dv (dirvish--new :type 'peek)) + ;; `dirvish-image-dp' needs this. + (setf (dv-index new-dv) (cons default-directory (current-buffer))) (setf (dv-preview-window new-dv) (or (minibuffer-selected-window) (next-window))))))) -(defvar dirvish-peek--prev-cand nil) (defun dirvish-peek-update-h () "Hook for `post-command-hook' to update peek window." - (when-let* ((dirvish-peek--curr-category) - (cand (funcall dirvish-peek--cand-fetcher)) - ((not (string= cand dirvish-peek--prev-cand)))) - (setq dirvish-peek--prev-cand cand) - (pcase dirvish-peek--curr-category - ('file - (setq cand (expand-file-name cand))) + (when-let* ((category (dirvish-prop :peek-category)) + (cand-fetcher (dirvish-prop :peek-fetcher)) + (cand (funcall cand-fetcher)) + ((not (string= cand (dirvish-prop :index))))) + (dirvish-prop :index cand) + (pcase category + ('file (setq cand (expand-file-name cand))) ('project-file (setq cand (expand-file-name cand (or (dirvish--get-project-root) (car (minibuffer-history-value)))))) - ('library - (setq cand (file-truename - (or (ignore-errors (find-library-name cand)) ""))))) + ('library (setq cand (file-truename + (or (ignore-errors (find-library-name cand)) ""))))) (unless (file-remote-p cand) - (dirvish-debounce nil (dirvish--preview-update dirvish--this cand))))) + (dirvish-debounce nil + (dirvish--preview-update dirvish--this cand))))) (defun dirvish-peek-exit-h () "Hook for `minibuffer-exit-hook' to destroy peek session." @@ -98,7 +100,7 @@ one of categories in `dirvish-peek-categories'." (when (eq (dv-type dv) 'peek) (dirvish--clear-session dv) (remhash (dv-name dv) dirvish--session-hash))) - (setq dirvish-peek--prev-cand nil)) + (dirvish-prop :index nil)) ;;;###autoload (define-minor-mode dirvish-peek-mode