-
Notifications
You must be signed in to change notification settings - Fork 23
/
dirtree.el
198 lines (178 loc) · 6.55 KB
/
dirtree.el
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
;;; dirtree.el --- Directory tree views
;; Copyright (C) 2010 Free Software Foundation, Inc.
;;
;; Author: Ye Wenbin <[email protected]>
;; Maintainer: Ye Wenbin <[email protected]>
;; Created: 09 Jan 2010
;; Version: 0.01
;; Keywords
;; This program is free software; you can redistribute it and/or modify
;; it under the terms of the GNU General Public License as published by
;; the Free Software Foundation; either version 2, or (at your option)
;; any later version.
;;
;; This program is distributed in the hope that it will be useful,
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
;; GNU General Public License for more details.
;;
;; You should have received a copy of the GNU General Public License
;; along with this program; if not, write to the Free Software
;; Foundation, Inc., 51 Franklin Street, Suite 500, Boston, MA 02110.
;;; Commentary:
;; There are several dir-tree widget implements, but I need these features:
;; 1. display many directory in one buffer to reduce buffer numbers
;; 2. reuse directory tree when already there is one
;; 3. use my favarite key binding
;;
;; So I wrote this one use `tree-mode'.
;;
;; See also:
;; http://www.splode.com/~friedman/software/emacs-lisp/src/dirtree.el
;; http://svn.halogen.kharkov.ua/svn/repos/alex-emacs-settings/emhacks/dir-tree.el
;; Put this file into your load-path and the following into your ~/.emacs:
;; (autoload 'dirtree "dirtree" "Add directory to tree view")
;;; Code:
(eval-when-compile
(require 'cl))
(require 'tree-mode)
(require 'windata)
(require 'dired-x)
(defgroup dirtree nil
"Directory tree views"
:group 'tools)
(defcustom dirtree-windata '(frame left 0.3 delete)
"*Arguments to set the window buffer display.
See `windata-display-buffer' for setup the arguments."
:type 'sexp
:group 'dirtree)
(defcustom dirtree-buffer "*dirtree*"
"*Buffer name for `dirtree'"
:type 'string
:group 'dirtree)
(define-widget 'dirtree-dir-widget 'tree-widget
"Directory Tree widget."
:dynargs 'dirtree-expand
:has-children t)
(define-widget 'dirtree-file-widget 'push-button
"File widget."
:format "%[%t%]\n"
:button-face 'default
:notify 'dirtree-select)
(defun dirtree-show ()
"Show `dirtree-buffer'. Create tree when no parent directory find."
(interactive)
(let ((buffer (get-buffer-create dirtree-buffer))
(dir default-directory)
trees tree button path)
(with-current-buffer buffer
(setq trees tree-mode-list)
(while (and trees
(not tree))
(if (string-match (concat "^" (regexp-quote (widget-get (car trees) :file))) dir)
;; if parent directory in buffer
(setq tree (car trees))
(setq trees (cdr trees)))))
(if tree
(progn
(setq path (split-string (file-relative-name buffer-file-name (widget-get tree :file)) "/"))
(dirtree (widget-get tree :file) t)
(setq button (tree-mode-find-node tree path))
(if button
(goto-char (widget-get (car button) :from))))
(call-interactively 'dirtree))))
(defun dirtree (root select)
"create tree of `root' directory
With prefix arguement select `dirtree-buffer'"
(interactive "DDirectory: \nP")
(let ((buffer (get-buffer-create dirtree-buffer))
tree win)
(with-current-buffer buffer
(unless (eq major-mode 'dirtree-mode)
(dirtree-mode))
(dolist (atree tree-mode-list)
(if (string= (widget-get atree :file) root)
(setq tree atree)))
(or tree
(setq tree (tree-mode-insert (dirtree-root-widget root)))))
;; (setq win (get-buffer-window dirtree-buffer))
(unless win
;;(setq win (get-buffer-window dirtree-buffer))
(setq win (apply 'windata-display-buffer dirtree-buffer dirtree-windata))
(select-window win))
(with-selected-window win
(unless (widget-get tree :open)
(widget-apply-action tree))
(goto-char (widget-get tree :from))
(recenter 1))
(if select
(select-window win))))
(defun dirtree-in-buffer (root select)
"create tree of `root' directory
With prefix arguement select `dirtree-buffer'"
(interactive "DDirectory: \nP")
(let ((buffer (get-buffer-create dirtree-buffer))
tree win)
(with-current-buffer buffer
(unless (eq major-mode 'dirtree-mode)
(dirtree-mode))
(dolist (atree tree-mode-list)
(if (string= (widget-get atree :file) root)
(setq tree atree)))
(or tree
(setq tree (tree-mode-insert (dirtree-root-widget root)))))
(if select
(switch-to-buffer dirtree-buffer))))
(define-derived-mode dirtree-mode tree-mode "Dir-Tree"
"A mode to display tree of directory"
(tree-widget-set-theme "folder"))
(defun dirtree-root-widget (directory)
"create the root directory"
`(dirtree-dir-widget
:node (dirtree-file-widget
:tag ,directory
:file ,directory)
:file ,directory
:open t))
(defun dirtree-expand (tree)
"expand directory"
(or (widget-get tree :args)
(let ((directory (widget-get tree :file))
(re (dired-omit-regexp))
dirs files basename)
(dolist (file (directory-files directory t))
(setq basename (file-name-nondirectory file))
(unless (string-match re basename)
(if (file-directory-p file)
(push (cons file basename) dirs)
(push (cons file basename) files))))
(setq dirs (sort dirs (lambda (a b) (string< (cdr a) (cdr b)))))
(setq files (sort files (lambda (a b) (string< (cdr a) (cdr b)))))
(append
(mapcar (lambda (file)
`(dirtree-dir-widget
:file ,(car file)
:node (dirtree-file-widget
:tag ,(cdr file)
:file ,(car file))))
dirs)
(mapcar (lambda (file)
`(dirtree-file-widget
:file ,(car file)
:tag ,(cdr file)))
files)))))
(defun dirtree-select (node &rest ignore)
"Open file in other window"
(let ((file (widget-get node :file)))
(and file
(find-file-other-window file))))
(defun dirtree-display ()
"Open file under point"
(interactive)
(let ((widget (widget-at (1- (line-end-position))))
file)
(if (setq file (widget-get widget :file))
(find-file-other-window file))))
(define-key dirtree-mode-map "\C-o" 'dirtree-display)
(provide 'dirtree)
;;; dirtree.el ends here