]> code.delx.au - gnu-emacs-elpa/blob - packages/ztree/ztree-dir.el
Added ztree package
[gnu-emacs-elpa] / packages / ztree / ztree-dir.el
1 ;;; ztree-dir.el --- Text mode directory tree
2
3 ;; Copyright (C) 2013-2015 Free Software Foundation, Inc.
4 ;;
5 ;; Author: Alexey Veretennikov <alexey dot veretennikov at gmail dot com>
6 ;;
7 ;; Created: 2013-11-1l
8 ;;
9 ;; Keywords: files tools
10 ;; URL: https://github.com/fourier/ztree
11 ;; Compatibility: GNU Emacs GNU Emacs 24.x
12 ;;
13 ;; This file is part of GNU Emacs.
14 ;;
15 ;; GNU Emacs is free software: you can redistribute it and/or modify
16 ;; it under the terms of the GNU General Public License as published by
17 ;; the Free Software Foundation, either version 3 of the License, or
18 ;; (at your option) any later version.
19 ;;
20 ;; GNU Emacs is distributed in the hope that it will be useful,
21 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
22 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
23 ;; GNU General Public License for more details.
24 ;;
25 ;; You should have received a copy of the GNU General Public License
26 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
27 ;;
28 ;;; Commentary:
29 ;;
30 ;; Add the following to your .emacs file:
31 ;;
32 ;; (push (substitute-in-file-name "path-to-ztree-directory") load-path)
33 ;; (require 'ztree-dir)
34 ;;
35 ;; Call the ztree interactive function:
36 ;; M-x ztree-dir
37 ;; Open/close directories with double-click, Enter or Space keys
38 ;;
39 ;;; Issues:
40 ;;
41 ;;; TODO:
42 ;; 1) Add some file-handling and marking abilities
43 ;;
44 ;;; Code:
45
46 (require 'ztree-util)
47 (require 'ztree-view)
48
49 ;;
50 ;; Constants
51 ;;
52
53 (defconst ztree-hidden-files-regexp "^\\."
54 "Hidden files regexp.
55 By default all filest starting with dot '.', including . and ..")
56
57
58 ;;
59 ;; Faces
60 ;;
61
62 (defface ztreep-header-face
63 '((((type tty pc) (class color)) :foreground "lightblue" :weight bold)
64 (((background dark)) (:height 1.2 :foreground "lightblue" :weight bold))
65 (t :height 1.2 :foreground "darkblue" :weight bold))
66 "*Face used for the header in Ztree buffer."
67 :group 'Ztree :group 'font-lock-highlighting-faces)
68 (defvar ztreep-header-face 'ztreep-header-face)
69
70
71 ;;
72 ;; File bindings to the directory tree control
73 ;;
74
75 (defun ztree-insert-buffer-header ()
76 "Insert the header to the ztree buffer."
77 (let ((start (point)))
78 (insert "Directory tree")
79 (newline-and-begin)
80 (insert "==============")
81 (set-text-properties start (point) '(face ztreep-header-face)))
82 (newline-and-begin))
83
84 (defun ztree-file-not-hidden (filename)
85 "Determines if the file with FILENAME should be visible."
86 (not (string-match ztree-hidden-files-regexp
87 (file-short-name filename))))
88
89 (defun ztree-find-file (node hard)
90 "Find the file at NODE.
91
92 If HARD is non-nil, the file is opened in another window.
93 Otherwise, the ztree window is used to find the file."
94 (when (and (stringp node) (file-readable-p node))
95 (if hard
96 (save-selected-window (find-file-other-window node))
97 (find-file node))))
98
99 ;;;###autoload
100 (defun ztree-dir (path)
101 "Create an interactive buffer with the directory tree of the PATH given."
102 (interactive "DDirectory: ")
103 (when (and (file-exists-p path) (file-directory-p path))
104 (let ((buf-name (concat "*Directory " path " tree*")))
105 (ztree-view buf-name
106 (expand-file-name (substitute-in-file-name path))
107 'ztree-file-not-hidden
108 'ztree-insert-buffer-header
109 'file-short-name
110 'file-directory-p
111 'string-equal
112 '(lambda (x) (directory-files x 'full))
113 nil ; face
114 'ztree-find-file)))) ; action
115
116
117 (provide 'ztree-dir)
118 ;;; ztree-dir.el ends here