]> code.delx.au - gnu-emacs-elpa/blob - packages/ztree/ztree-dir.el
Update packages/yasnippet by subtree-merging from its external upstream
[gnu-emacs-elpa] / packages / ztree / ztree-dir.el
1 ;;; ztree-dir.el --- Text mode directory tree -*- lexical-binding: t; -*-
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 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 ;; Configurable variables
59 ;;
60
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
64
65
66 ;;
67 ;; Faces
68 ;;
69
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)
77
78
79 ;;
80 ;; File bindings to the directory tree control
81 ;;
82
83 (defun ztree-insert-buffer-header ()
84 "Insert the header to the ztree buffer."
85 (let ((start (point)))
86 (insert "Directory tree")
87 (insert "\n")
88 (insert "==============")
89 (set-text-properties start (point) '(face ztreep-header-face)))
90 (insert "\n"))
91
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))))
96
97 (defun ztree-find-file (node hard)
98 "Find the file at NODE.
99
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))
105 (hard
106 (save-selected-window (find-file-other-window node)))
107 (t
108 (find-file node)))))
109
110 ;;;###autoload
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*")))
116 (ztree-view buf-name
117 (expand-file-name (substitute-in-file-name path))
118 'ztree-file-not-hidden
119 'ztree-insert-buffer-header
120 'ztree-file-short-name
121 'file-directory-p
122 'string-equal
123 '(lambda (x) (directory-files x 'full))
124 nil ; face
125 'ztree-find-file)))) ; action
126
127
128 (provide 'ztree-dir)
129 ;;; ztree-dir.el ends here