Fuco1 / dired-hacks

Collection of useful dired additions
GNU General Public License v3.0
847 stars 76 forks source link

Feature request: dired-jump for dired-collapse #189

Open yurikhan opened 2 years ago

yurikhan commented 2 years ago

dired-collapse lets us skip several levels of subdirectories when drilling down, and this is occasionally very convenient. It’s like saying “I go down this path” rather than repeating “I move forward” multiple times.

It would be nice to also be able to say “I go up to the nearest interesting directory”, where “interesting” is defined as “has more than one item”.

Fuco1 commented 2 years ago

Maybe something like this

(defun dired-collapse-jump ()
  "Jump to the first parent directory with more than one file.

This is opposite of \"drilling down\" through empty collapsed directories."
  (interactive)
  (let* ((file-name (buffer-file-name))
         (path (or (f-parent file-name) default-directory))
         prev-path
         files)
    (while (and (file-directory-p path)
                (file-readable-p path)
                (setq files (f-entries path))
                (= 1 (length files)))
      (setq prev-path path)
      (setq path (f-parent path)))
    (dired-jump nil prev-path)
    (dired-goto-file file-name)))
yurikhan commented 2 years ago
-  (let* ((file-name (buffer-file-name))
+  (let* ((file-name (or (buffer-file-name) default-directory))

or something, because (buffer-file-name) returns nil for Dired bufers and then (f-parent nil) on the next line errors out.

Otherwise, it seems to work, thank you! ~Now I was going to bind it to [remap dired-jump] but dired-collapse-mode does not seem to have a keymap…~ Never mind, dired-collapse-mode is not going to be enabled in file buffers so I’ll need to bind it globally.

Fuco1 commented 2 years ago

Yea, good catch. I'll add it to the package, I like it. Good suggestion!