1 ;;; ztree-dir.el --- Text mode directory tree -*- lexical-binding: t; -*-
3 ;; Copyright (C) 2013-2015 Free Software Foundation, Inc.
5 ;; Author: Alexey Veretennikov <alexey dot veretennikov at gmail dot com>
9 ;; Keywords: files tools
10 ;; URL: https://github.com/fourier/ztree
11 ;; Compatibility: GNU Emacs 24.x
13 ;; This file is part of GNU Emacs.
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.
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.
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/>.
30 ;; Add the following to your .emacs file:
32 ;; (push (substitute-in-file-name "path-to-ztree-directory") load-path)
33 ;; (require 'ztree-dir)
35 ;; Call the ztree interactive function:
37 ;; Open/close directories with double-click, Enter or Space keys
42 ;; 1) Add some file-handling and marking abilities
53 (defconst ztree-hidden-files-regexp "^\\."
55 By default all filest starting with dot '.', including . and ..")
58 ;; Configurable variables
61 (defvar ztree-dir-move-focus nil
62 "If set to true moves the focus to opened window when the
63 user press RETURN on file ")t
70 (defface ztreep-header-face
71 '((((type tty pc) (class color)) :foreground "lightblue" :weight bold)
72 (((background dark)) (:height 1.2 :foreground "lightblue" :weight bold))
73 (t :height 1.2 :foreground "darkblue" :weight bold))
74 "*Face used for the header in Ztree buffer."
75 :group 'Ztree :group 'font-lock-highlighting-faces)
76 (defvar ztreep-header-face 'ztreep-header-face)
80 ;; File bindings to the directory tree control
83 (defun ztree-insert-buffer-header ()
84 "Insert the header to the ztree buffer."
85 (let ((start (point)))
86 (insert "Directory tree")
88 (insert "==============")
89 (set-text-properties start (point) '(face ztreep-header-face)))
92 (defun ztree-file-not-hidden (filename)
93 "Determines if the file with FILENAME should be visible."
94 (not (string-match ztree-hidden-files-regexp
95 (ztree-file-short-name filename))))
97 (defun ztree-find-file (node hard)
98 "Find the file at NODE.
100 If HARD is non-nil, the file is opened in another window.
101 Otherwise, the ztree window is used to find the file."
102 (when (and (stringp node) (file-readable-p node))
103 (cond ((and hard ztree-dir-move-focus)
104 (find-file-other-window node))
106 (save-selected-window (find-file-other-window node)))
111 (defun ztree-dir (path)
112 "Create an interactive buffer with the directory tree of the PATH given."
113 (interactive "DDirectory: ")
114 (when (and (file-exists-p path) (file-directory-p path))
115 (let ((buf-name (concat "*Directory " path " tree*")))
117 (expand-file-name (substitute-in-file-name path))
118 'ztree-file-not-hidden
119 'ztree-insert-buffer-header
120 'ztree-file-short-name
123 '(lambda (x) (directory-files x 'full))
125 'ztree-find-file)))) ; action
129 ;;; ztree-dir.el ends here