Fuco1 / dired-hacks

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

dired-narrow: call `dired-narrow-find-file' when entering directory #186

Closed bastidest closed 4 months ago

bastidest commented 3 years ago

Information

Emacs Version GNU Emacs 28.0.50.148618
OS Arch Linux (5.12.15-arch1-1)
emacs -q ? yes

Emacs Configuration:

(require 'package)
(package-initialize)

(unless (package-installed-p 'use-package)
  (package-refresh-contents)
  (package-install 'use-package))

(use-package dired-narrow
  :after (dired)
  :bind (:map dired-mode-map
        ("/" . dired-narrow)))

Bug Description

May be related to #147 . Please confirm that the described behavior is intended as such (I might not understand dired-narrow-enter-directory correctly).

Expected Behavior

After entering dired-narrow-mode and selecting a directory (by filtering and moving the point), calling dired-narrow-enter-directory should enter the directory at point and start another dired-narrow minibuffer.

Actual Behavior

After entering dired-narrow-mode and selecting a directory, calling dired-narrow-enter-directory does not enter the directory, but simply restarts narrowing in the current (already narrowed) directory.

Change Description

Before this change, when dired-narrow-enter-directory was called, the dired-narrow--internal function did call itself to start another instance of dired narrow in the subdirectory. However it did not visit the directory at point, which restarted dired-narrow in the current (already filtered) directory.

With this change dired-narrow-find-file is called before restarting dired-narrow--internal. This will visit the directory at point and start another dired-narrow instance. If the current file is not a directory, visit the file at point.

Comment

Thank you for maintaining this package!

Fuco1 commented 3 years ago

Hi. I tried to replicate this but can't. Are you sure you are calling dired-narrow-enter-directory (bound to right arrow key or C-j) and not the regular dired find file?

bastidest commented 3 years ago

Thank you for your response. Your screencast shows my desired result. I tested it on a different system with the configuration shown above and was still able to reproduce my issue.

Screenshot from 2021-08-27 17-50-48

The screenshot shows a dired-narrow buffer filtering by the word auto. I changed focus from the minibuffer to the dired buffer and confirmed that the C-j binding does indeed call dired-narrow-enter-directory (C-h c). The issue still persists.

Have you tried to reproduce this with emacs -Q? I will try to test it on emacs 27 when I get the chance.

Fuco1 commented 3 years ago

I'm actually on E26, so that might be the variable which is different here.

bastidest commented 3 years ago

I reproduced it on emacs 27.2 and emacs 26.

Docker commands:

Steps:

  1. Go to *scratch* buffer
  2. Paste this
    
    (require 'package)
    (add-to-list 'package-archives '("melpa" . "https://melpa.org/packages/") t)
    (package-initialize)

(unless (package-installed-p 'use-package) (package-refresh-contents) (package-install 'use-package))

(use-package dired-narrow :ensure t :after (dired) :bind (:map dired-mode-map ("/" . dired-narrow)))


3. `M-x` `eval-buffer`
4. `C-x d` --> dired opens at `/`
5. `/` --> `dired-narrow` mode in minibuffer
6. `etc` --> filters etc directory
7. `C-j` --> Bug
Fuco1 commented 3 years ago

You're not binding dired-narrow-enter-directory in your repro case. However, I added the binding and can reproduce the bug. So there must be something on my system fixing it.

bastidest commented 3 years ago

Binding it should not be necessary, right? At least the default keymap shows the bindings (describe-keymap)

dired-narrow-map is a keymap variable defined in ‘dired-narrow.el’.

Documentation:
Keymap used while ‘dired-narrow’ is reading the pattern.

key             binding
---             -------

C-g             minibuffer-keyboard-quit
C-j             dired-narrow-enter-directory
RET             exit-minibuffer
C-n             dired-narrow-next-file
C-p             dired-narrow-previous-file
<down>          dired-narrow-next-file
<return>        exit-minibuffer
<right>         dired-narrow-enter-directory
<up>            dired-narrow-previous-file
Fuco1 commented 4 months ago

I figured it out. By default, the dired-narrow-exit-action is set to ignore so it does nothing. I had it set to dired-narrow-find-file, so when I exited the minibuffer with C-j it called this function and remembered that I used the enter-directory command so it restarted the search.

For you, since your exit action is most likely just the default ignore, it executed that (= did nothing) and then restarted the narrowing in the same buffer.

Your fix was to call the dired-narrow-find-file when acting on a directory, however, you call it after the default action runs. So for someone like me, it would actually call the exit action twice (once from the config and once from your patch).

I reordered the conditions so that in case of the enter directory action the exit-action is simply ignored because it doesn't actually make sense... our action is to enter the directory so we shouldn't run anything else.

Fuco1 commented 4 months ago

Thank you for the contribution, your code helped me figure out the problem!