Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

it seems a dired buffer is not added to recentf-list,any plan to support directory visited by dired-mode #17

Closed
jixiuf opened this issue Apr 20, 2022 · 2 comments

Comments

@jixiuf
Copy link

jixiuf commented Apr 20, 2022

;; after add  consult-dir--source-dired to  `consult-dir-sources`

(require 'savehist)
(add-to-list 'savehist-additional-variables 'consult-dired-history)
(savehist-mode 1)

(require 'dired)
(require 'dired-aux)

(defgroup consult-dired-history nil
  "Consult dired history"
  :group 'ivy)


(defcustom consult-dired-history-max 200
  "Length of history for ivy-dired-history."
  :type 'number
  :group 'consult-dired-history)

(defcustom consult-dired-history-ignore-directory '("/")
  "Length of history for ivy-dired-history."
  :type '(repeat string)
  :group 'consult-dired-history)

(defvar consult-dired-history nil)

(defvar consult-dired-history--cleanup-p nil)

(defun consult-dired-history--update(dir)
  "Update variable `consult-dired-history'.
Argument DIR directory."
       (setq dir (abbreviate-file-name (expand-file-name dir)))
       (unless (member dir consult-dired-history-ignore-directory)
         (unless consult-dired-history--cleanup-p
           (setq consult-dired-history--cleanup-p t)
           (let ((tmp-history ))
             (dolist (d consult-dired-history)
               (when (or (file-remote-p d) (file-directory-p d))
                 (add-to-list 'tmp-history d t)))
             (setq consult-dired-history tmp-history)))
         (setq consult-dired-history
               (delete-dups (delete dir consult-dired-history)))
         (setq consult-dired-history
               (append (list dir) consult-dired-history))
         (consult-dired-history)))

(defun consult-dired-history-update()
  "Update variable `consult-dired-history'."
  (consult-dired-history--update (dired-current-directory)))

;;when you open dired buffer ,update `consult-dired-history'.
(add-hook 'dired-after-readin-hook 'consult-dired-history-update)

(defun consult-dired-history()
  "Retain only the first `consult-dired-history-max' items in VALUE."
  (if (> (length consult-dired-history) consult-dired-history-max)
      (setcdr (nthcdr (1- consult-dired-history-max) consult-dired-history) nil))
  consult-dired-history)

(defvar consult-dir--source-dired
  `(:name "Recentf dired"
    :narrow ?d
    :category file
    :face consult-file
    :history file-name-history
    :items ,#'consult-dired-history)
  "Recentf directory source for `consult-dir--pick'.")
@karthink
Copy link
Owner

karthink commented May 5, 2022

@jixiuf, thank you for the suggestion. This is a good idea, but consult-dir is intended to only be an aggregator with no "memory" of its own.

As such, this feature is best implemented as part of recentf as a patch. Here is everything you need to make recentf track directories you've opened with dired:

;; Track opened directories
(defun recentf-track-opened-dir ()
  (and default-directory
       (recentf-add-file default-directory)))

(add-hook 'dired-mode-hook #'recentf-track-opened-dir)

;; Track closed directories
(advice-add 'recentf-track-closed-file :override
            (defun recentf-track-closed-advice ()
              (cond (buffer-file-name (recentf-remove-if-non-kept buffer-file-name))
                    ((equal major-mode 'dired-mode)
                     (recentf-remove-if-non-kept default-directory)))))

Then no changes are required in consult-dir, and consult-dir--source-recentf will show opened directories in addition to directories corresponding to opened files.

Alternatively, you can create a new package (named recentf-dired, let's say) that implements this functionality. I can then provide consult-dir--source-recentf-dired in the README for interested people.

@jixiuf
Copy link
Author

jixiuf commented May 5, 2022

thanks for your time.

@jixiuf jixiuf closed this as completed May 5, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants