branch: elpa/dirvish
commit c51794f231ad5c784df2377d0bb56c1b067bc7cf
Author: Alex Lu <hellosimon1...@hotmail.com>
Commit: Alex Lu <hellosimon1...@hotmail.com>

    refactor: use `dired-buffers` / expunge `dirvish--history`
---
 dirvish-tramp.el              |  1 +
 dirvish.el                    | 18 ++++++++++--------
 extensions/dirvish-fd.el      |  5 +++--
 extensions/dirvish-history.el | 22 +++++++++++-----------
 4 files changed, 25 insertions(+), 21 deletions(-)

diff --git a/dirvish-tramp.el b/dirvish-tramp.el
index 79e097e9a8..e1b705a830 100644
--- a/dirvish-tramp.el
+++ b/dirvish-tramp.el
@@ -33,6 +33,7 @@ FN is the original `dired-noselect' closure."
          (vec (tramp-dissect-file-name dir))
          (async-type (dirvish-tramp--async-p vec))
          (gnuls "ls")
+         (dired-buffers nil) ; disable reuse from `dired'
          (buffer (cond ((eq async-type 'local) (funcall fn dir flags))
                        (saved-flags (funcall fn dir saved-flags)) ; skip
                        ((= (or (process-file gnuls nil nil nil "--version") 1) 
0)
diff --git a/dirvish.el b/dirvish.el
index aa8c413b0d..5869f6657d 100644
--- a/dirvish.el
+++ b/dirvish.el
@@ -334,7 +334,6 @@ opening and customized handling of specific file types."
     (dirvish-subtree  subtree-state)
     (dirvish-yank     yank)))
 (defvar dirvish--delay-timer `(,(timer-create) ,(float-time) nil))
-(defvar dirvish--history nil)
 (defvar dirvish--reset-keywords '(:free-space :content-begin))
 (defvar dirvish--selected-window nil)
 (defvar dirvish--sessions (make-hash-table :test #'equal))
@@ -1476,7 +1475,7 @@ With optional NOSELECT just find files but do not select 
them."
          (mc dirvish-large-directory-threshold)
          (buffer (alist-get key (dv-roots dv) nil nil #'equal))
          (new? (null buffer)) (dps (dv-preview-dispatchers dv))
-         tramp fd dired-buffers) ; disable reuse from dired
+         (hist (cons key nil)) tramp fd)
     (setf (dv-timestamp dv) (dirvish--timestamp))
     (cond ((and new? remote)
            (setq tramp (prog1 'dirvish-tramp-noselect (require 'dirvish-tramp))
@@ -1485,8 +1484,13 @@ With optional NOSELECT just find files but do not select 
them."
            (setq fd (prog1 'dirvish-fd-noselect (require 'dirvish-fd nil t))
                  buffer (apply fd (list dv key (or re "")))
                  key (concat key "🔍" (or re ""))))
-          (new? (setq buffer (apply fn (list dir-or-list flags)))))
-    (cl-pushnew (cons key buffer) (dv-roots dv) :test #'equal)
+          (new? (let (dired-buffers) ; disable reuse from `dired'
+                  (setq buffer (apply fn (list dir-or-list flags))))))
+    (when (setq new? (null (alist-get key (dv-roots dv) nil nil #'equal)))
+      (push (cons key buffer) (dv-roots dv)))
+    (unless (member (car hist) (mapcar #'car dired-buffers))
+      (setq dired-buffers (seq-take (push hist dired-buffers) 20000)))
+    (setcdr (assoc (car hist) dired-buffers) buffer)
     (with-current-buffer buffer
       (dirvish--setup-dired)
       (cond (new? nil)
@@ -1504,12 +1508,10 @@ With optional NOSELECT just find files but do not 
select them."
       (dirvish-prop :attrs (dv-attributes dv))
       (cl-loop for (k v) on dirvish--scopes by 'cddr
                do (dirvish-prop k (and (functionp v) (funcall v))))
-      (when new? (dirvish--dir-data-async key buffer))
+      (when new? (dirvish--dir-data-async (car hist) buffer))
       (when bname (dired-goto-file bname))
       (setf (dv-index dv) (cons key buffer))
-      (let ((key (if (string-prefix-p "🔍" key) (buffer-name buffer) key)))
-        (setq dirvish--history (seq-take (push key dirvish--history) 200)))
-      (run-hook-with-args 'dirvish-find-entry-hook key 'dired)
+      (run-hook-with-args 'dirvish-find-entry-hook (car hist) 'dired)
       buffer)))
 
 ;;;; Commands
diff --git a/extensions/dirvish-fd.el b/extensions/dirvish-fd.el
index cfeca5fea7..ca8eab6610 100644
--- a/extensions/dirvish-fd.el
+++ b/extensions/dirvish-fd.el
@@ -204,7 +204,7 @@ Raise an error if fd executable is not available."
                         'face 'font-lock-string-face)
             (propertize "excludes:" 'face face)
             (propertize (if (equal excludes "") "none" excludes)
-                        'face 'font-lock-string-face))))
+                        'face 'font-lock-variable-name-face))))
 
 (dirvish-define-mode-line fd-status
   "Status and time took by last fd search."
@@ -287,7 +287,8 @@ Raise an error if fd executable is not available."
       (let (buffer-read-only)
         (erase-buffer)
         (insert "  " dir ":" (make-string (dirvish--subdir-offset) ?\n)))
-      (unless (derived-mode-p 'dired-mode) (dired-mode dir ls-switches))
+      (unless (derived-mode-p 'dired-mode)
+        (let (dired-buffers) (dired-mode dir ls-switches)))
       (setq-local default-directory dir
                   dired-subdir-alist (list (cons dir (point-min-marker))))
       (dirvish-fd--argparser re switches)
diff --git a/extensions/dirvish-history.el b/extensions/dirvish-history.el
index d7fdc46e5a..9fac31b371 100644
--- a/extensions/dirvish-history.el
+++ b/extensions/dirvish-history.el
@@ -36,14 +36,14 @@
 
 ;;;###autoload
 (defun dirvish-history-jump ()
-  "Open a target directory from `dirvish--history'."
+  "Read a recently visited directory from minibuffer and revisit it."
   (interactive)
-  (unless dirvish--history (user-error "Dirvish[error]: no history entries"))
+  (unless dired-buffers (user-error "Dirvish[error]: no history entries"))
   (when-let* ((result
                (completing-read
                 "Recently visited: "
                 (dirvish--completion-table-with-metadata
-                 dirvish--history
+                 (mapcar #'car dired-buffers)
                  `((category . file)
                    (display-sort-function . 
,dirvish-history-sort-function))))))
     (dirvish--find-entry 'find-file result)))
@@ -52,11 +52,11 @@
 (defun dirvish-history-last ()
   "Switch to the most recently visited dirvish buffer."
   (interactive)
-  (unless dirvish--history (user-error "Dirvish[error]: no history entries"))
+  (unless dired-buffers (user-error "Dirvish[error]: no history entries"))
   (let ((match
          (cl-loop
           with local-entries = (mapcar #'car (dv-roots (dirvish-curr)))
-          for entry in dirvish--history
+          for entry in (mapcar #'car dired-buffers)
           thereis (and (member entry local-entries)
                        (not (equal entry (dired-current-directory))) entry))))
     (and match (dirvish--find-entry 'find-file match))))
@@ -67,17 +67,17 @@
 ARG defaults to 1."
   (interactive "^p")
   (let* ((dv (or (dirvish-curr) (user-error "Not in a dirvish session")))
-         (dirs (reverse (mapcar #'car (dv-roots dv))))
-         (len (length dirs))
-         (idx (cl-position (car (dv-index dv)) dirs :test #'equal))
+         (bufs (reverse (mapcar #'cdr (dv-roots dv))))
+         (len (length bufs))
+         (idx (cl-position (cdr (dv-index dv)) bufs))
          (new-idx (+ idx arg)))
     (cond ((>= new-idx len)
-           (dirvish--find-entry 'find-file (nth (- len 1) dirs))
+           (dirvish-save-dedication (switch-to-buffer (nth (- len 1) bufs)))
            (message "Dirvish: reached the end of history"))
           ((< new-idx 0)
-           (dirvish--find-entry 'find-file (nth 0 dirs))
+           (dirvish-save-dedication (switch-to-buffer (nth 0 bufs)))
            (message "Dirvish: reached the beginning of history"))
-          (t (dirvish--find-entry 'find-file (nth new-idx dirs))))))
+          (t (dirvish-save-dedication (switch-to-buffer (nth new-idx 
bufs)))))))
 
 ;;;###autoload
 (defun dirvish-history-go-backward (arg)

Reply via email to