Tuesday, October 05, 2004

dired to open new directories in same buffer

From http://list-archive.xemacs.org/xemacs-beta/200006/msg00277.html

Pasting the entire mail

Recently, there was some discussion in the comp.emacs newsgroup

regarding dired and the way `dired-find-file' handles directories.
`dired-find-file' will always create a new buffer for each directory.
If you work in many directories, these dired buffers can accumulate
and quickly become unwieldy.

The original post asked for a way to reuse the same dired buffer when
changing directories. Someone suggested using 'i',
`dired-maybe-insert-subdir', instead of 'RET'. This isn't ideal,
however, because it simply inserts the contents of the subdirectory
into the current dired buffer and doesn't remove the old directory's
contents. It also forces users to use two different keys to navigate
through directories and select files, 'i' and 'RET', instead of just
being able to use 'RET'.

I would really like to have the new directory contents replace the old
in a dired buffer. If my cursor is positioned over a file, however, I
want dired to do the normal thing: open the file in a new buffer.
This seems to me like the most intuitive behavior. I hacked out some
lisp code that does this:

(defun dired-follow-file ()
"In dired, visit the file or directory on this line.
If a directory is on the current line, replace the current
dired buffer with one containing the contents of the directory.
Otherwise, invoke `dired-find-file' on the file."
(let ((filename (dired-get-filename)))
;; if the file is a directory, replace the buffer with the
;; directory's contents
(if (file-directory-p filename)
(find-alternate-file filename)
;; otherwise simply perform a normal `dired-find-file'

Now, I simply need to remap the dired keys

(lambda ()
(local-set-key "\C-m" 'dired-follow-file)
(local-set-key "e" 'dired-follow-file)
(local-set-key "f" 'dired-follow-file)))

and (abracadabra) dired replaces the old directory with the new one in
the same buffer when I press 'RET'. (Note that it's always easy to
get back to the parent directory because you can just select '..' in
the new directory listing.)

I posted this lisp snippet to comp.emacs and was asked to also send it
to and . Here it
is. I hope this is helpful.

Samuel Padgett

No comments: