emacs-devel
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

How to run code after a dired buffer is fully initialized?


From: Tassilo Horn
Subject: How to run code after a dired buffer is fully initialized?
Date: Mon, 06 Apr 2009 22:27:33 +0200
User-agent: Gnus/5.13 (Gnus v5.13) Emacs/23.0.92 (gnu/linux)

Hi all,

I like using dired, but I don't like the standard behavior of creating
new dired buffers when I hit RET on a directory.  So I use my own
function `th-dired-find-file' which uses `dired-find-alternate-file' for
directories.

This works quite well, but I would like dired to remember the line point
was on the last time I visited that directory, and to go to that line
when I come back.  I tried to use a hashtable (`th-dired-dir-line-map')
mapping the directory to the line number.  The remembering part works
(`th-dired-save-line'), but I cannot find out where I should call
`th-dired-goto-line'.  I tried appending it to `dired-mode-hook' and
`dired-after-readin-hook', and then it says "Going to line X", but in
the dired buffer point is still on the first file after the `..' entry.

What am I doing wrong?

Here's my dired config:

--8<---------------cut here---------------start------------->8---
(require 'dired-x)
(setq dired-omit-files
      (rx (or (seq bol (? ".") "#")         ;; emacs autosave files
              (seq bol "." (not (any "."))) ;; dot-files
              (seq "~" eol)                 ;; backup-files
              (seq bol "CVS" eol)           ;; CVS dirs
              )))

(setq dired-omit-extensions
      (append dired-latex-unclean-extensions
              dired-bibtex-unclean-extensions
              dired-texinfo-unclean-extensions))

(defvar th-dired-dir-line-map (make-hash-table :test 'string=)
  "Maps dirname to line number where dired was.")

(defun th-dired-save-line ()
  (let ((lineno (line-number-at-pos)))
    (puthash dired-directory lineno th-dired-dir-line-map)))

;; TODO: Where do I need to call that.  Both dired-mode-hook and
;; dired-after-readin-hook have no effect.  It says "Going to line X",
;; but in the dired buffer point is on the file after `..'.
(defun th-dired-goto-line ()
  (let ((lineno (gethash dired-directory th-dired-dir-line-map nil)))
    (when lineno
      (message "Going to line %s" lineno)
      (goto-line lineno))))

(defun th-dired-up-directory ()
  "Go up one directory and don't create a new dired buffer but
reuse the current one."
  (interactive)
  (th-dired-save-line)
  (find-alternate-file ".."))

(defun th-dired-find-file ()
  "Find directory reusing the current buffer and file creating a
new buffer."
  (interactive)
  (th-dired-save-line)
  (if (file-directory-p (dired-get-file-for-visit))
      (dired-find-alternate-file)
    (dired-find-file)))

(defun th-dired-mode-init ()
  (dired-omit-mode 1)
  (auto-revert-mode 1)
  (hl-line-mode 1)
  (rename-buffer (concat "dired:" (buffer-name)) t)
  (local-set-key (kbd "C-c C-w") 'wdired-change-to-wdired-mode)
  (local-set-key (kbd "^")       'th-dired-up-directory)
  (local-set-key (kbd "e")       'th-dired-find-file-externally)
  (local-set-key (kbd "RET")     'th-dired-find-file))

(add-hook 'dired-mode-hook 'th-dired-mode-init t)

(put 'dired-find-alternate-file 'disabled nil)
(setq dired-recursive-deletes 'top
      dired-recursive-copies  'top
      dired-listing-switches "-alh"
      dired-dwim-target t)
--8<---------------cut here---------------end--------------->8---

Thanks for any pointers!
Tassilo




reply via email to

[Prev in Thread] Current Thread [Next in Thread]