single-dired.el 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. ;;; joseph-single-dired.el --- Only Open One Dired Buffer.
  2. ;; Filename: joseph-single-dired.el
  3. ;; Description: only open a single dired buffer.
  4. ;; Author: Joseph <jixiuf@gmail.com>
  5. ;; Maintainer: Joseph <jixiuf@gmail.com>
  6. ;; Copyright (C) 2011~, Joseph, all rights reserved.
  7. ;; Created: 2011-04-03
  8. ;; Version: 0.1.0
  9. ;; URL: http://www.emacswiki.org/emacs/download/joseph-single-dired.el
  10. ;; git://github.com/jixiuf/joseph-single-dired.git
  11. ;; Keywords: dired single buffer
  12. ;; Compatibility: (Test on GNU Emacs 24)
  13. ;;
  14. ;; Features that might be required by this library:
  15. ;;
  16. ;; `dired'
  17. ;;
  18. ;;; This file is NOT part of GNU Emacs
  19. ;;; License
  20. ;;
  21. ;; This program is free software; you can redistribute it and/or modify
  22. ;; it under the terms of the GNU General Public License as published by
  23. ;; the Free Software Foundation; either version 3, or (at your option)
  24. ;; any later version.
  25. ;; This program is distributed in the hope that it will be useful,
  26. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  27. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  28. ;; GNU General Public License for more details.
  29. ;; You should have received a copy of the GNU General Public License
  30. ;; along with this program; see the file COPYING. If not, write to
  31. ;; the Free Software Foundation, Inc., 51 Franklin Street, Fifth
  32. ;; Floor, Boston, MA 02110-1301, USA.
  33. ;;; Commentary:
  34. ;;
  35. ;; In Dired, when you choose a directory to visit,
  36. ;; it is normally visited in a new buffer – the
  37. ;; Dired buffer you chose it in is not deleted.
  38. ;; Some people don’t like this behavior, because as you
  39. ;; navigate around the directory tree you accumulate
  40. ;; Dired buffers, one for each directory you visit.
  41. ;;
  42. ;; So ,`joseph-single-dired.el' can make you just open
  43. ;; a single dired buffer.
  44. ;; I bind `dired-mouse-find-alternate-file' on [mouse-2]
  45. ;; so even you click on a directory in dired buffer ,
  46. ;; it will reuse current dired buffer too.
  47. ;;
  48. ;;; Installation:
  49. ;;
  50. ;; Just put `joseph-single-dired.el' to your load-path.
  51. ;; The load-path is usually ~/elisp/.
  52. ;; It's set in your ~/.emacs like this:
  53. ;; (add-to-list 'load-path (expand-file-name "~/elisp"))
  54. ;;
  55. ;; Add the following to your ~/.emacs startup file.
  56. ;;
  57. ;; (eval-after-load 'dired '(progn (require 'joseph-single-dired)))
  58. ;;
  59. ;; No need more.
  60. ;;; Commands:
  61. ;;
  62. ;; Below are complete command list:
  63. ;;
  64. ;; `dired-mouse-find-alternate-file'
  65. ;; In dired, visit the file or directory you click on instead of the dired buffer.
  66. ;;
  67. ;;; Customizable Options:
  68. ;;
  69. ;; Below are customizable option list:
  70. ;;
  71. (require 'dired)
  72. (defun joseph-kill-all-other-dired-buffers ( &optional current-buf)
  73. "kill all dired-buffers and diredp-w32-drivers-mode(w32 use this mode )
  74. except current-buf ,if current-buf is nil then kill all"
  75. (dolist (buf (buffer-list))
  76. (with-current-buffer buf
  77. (when (and (not (eq current-buf buf))
  78. (or (eq 'dired-mode major-mode)
  79. (eq 'diredp-w32-drives-mode major-mode)))
  80. (kill-buffer buf)))))
  81. (defadvice dired-find-file (around dired-find-file-single-buffer activate)
  82. "Replace current buffer if file is a directory."
  83. (interactive)
  84. (let ((orig (current-buffer))
  85. (filename (dired-get-file-for-visit)))
  86. ad-do-it
  87. (when (and (file-directory-p filename)
  88. (not (eq (current-buffer) orig)))
  89. (joseph-kill-all-other-dired-buffers (current-buffer)))))
  90. (defadvice dired-up-directory (around dired-up-directory-single-buffer activate)
  91. "Replace current buffer if file is a directory."
  92. (interactive)
  93. (let ((orig (current-buffer)))
  94. ad-do-it
  95. (joseph-kill-all-other-dired-buffers (current-buffer))))
  96. (defadvice dired (before dired-single-buffer activate)
  97. "Replace current buffer if file is a directory."
  98. (joseph-kill-all-other-dired-buffers)
  99. )
  100. ;;;###autoload
  101. (defun dired-mouse-find-alternate-file (event)
  102. "In dired, visit the file or directory you click on instead of the dired buffer."
  103. (interactive "e")
  104. (let (file)
  105. (save-excursion
  106. (with-current-buffer (window-buffer (posn-window (event-end event)))
  107. (save-excursion
  108. (goto-char (posn-point (event-end event)))
  109. (setq file (dired-get-filename nil t)))))
  110. (select-window (posn-window (event-end event)))
  111. (find-alternate-file (file-name-sans-versions file t))))
  112. (define-key dired-mode-map [mouse-2] 'dired-mouse-find-alternate-file)
  113. (provide 'single-dired)