branch: master commit e1009bb20576950a0a433a4910c15d1cf9327866 Author: Alexey Veretennikov <alexey.veretenni...@gmail.com> Commit: Alexey Veretennikov <alexey.veretenni...@gmail.com>
Issue #42: Added support for narrow/widen in ztree-dir. In ztree-dir now it is possible to focus on directory under the cursor (or parent directory of the file under the cursor) using '>' hotkey. One can also widen to the to the parent of the root directory using '<' hotkey. This allows to navigate the whole filesystem without leaving the buffer. --- ztree-dir.el | 31 +++++++++++++++++++++++++++++-- ztree-util.el | 16 +++++++++++++++- ztree-view.el | 27 ++++++++++++++++----------- 3 files changed, 60 insertions(+), 14 deletions(-) diff --git a/ztree-dir.el b/ztree-dir.el index 600d965..5e7a509 100644 --- a/ztree-dir.el +++ b/ztree-dir.el @@ -61,7 +61,7 @@ By default all filest starting with dot `.', including . and ..") (defvar ztree-dir-move-focus nil "If set to true moves the focus to opened window when the -user press RETURN on file ") +user press RETURN on file") (defvar-local ztree-dir-filter-list (list ztree-hidden-files-regexp) "List of regexp file names to filter out. @@ -96,7 +96,9 @@ One could add own filters in the following way: " Dir" ;; The minor mode keymap `( - (,(kbd "H") . ztree-dir-toggle-show-filtered-files))) + (,(kbd "H") . ztree-dir-toggle-show-filtered-files) + (,(kbd ">") . ztree-dir-narrow-to-dir) + (,(kbd "<") . ztree-dir-widen-to-parent))) @@ -153,6 +155,31 @@ Otherwise, the ztree window is used to find the file." (directory-files path 'full))) +(defun ztree-dir-narrow-to-dir () + "Interactive command to narrow the current directory buffer. +The buffer is narrowed to the directory under the cursor. +If the cursor is on a file, the buffer is narrowed to the parent directory." + (interactive) + (let* ((line (line-number-at-pos)) + (node (ztree-find-node-in-line line)) + (parent (ztree-get-parent-for-line line))) + (if (file-directory-p node) + (ztree-change-start-node node) + (when parent + (ztree-change-start-node (ztree-find-node-in-line parent)))))) + + +(defun ztree-dir-widen-to-parent () + "Interactive command to widen the current directory buffer to parent. +The buffer is widened to the parent of the directory of the current buffer. +This allows to jump to the parent directory if this directory is one level +up of the opened." + (interactive) + (let* ((node ztree-start-node) + (parent (file-name-directory (directory-file-name node)))) + (when parent + (ztree-change-start-node parent)))) + ;;;###autoload (defun ztree-dir (path) diff --git a/ztree-util.el b/ztree-util.el index 39975b0..5ac764b 100644 --- a/ztree-util.el +++ b/ztree-util.el @@ -48,10 +48,17 @@ Taken from http://www.emacswiki.org/emacs/ElispCookbook#toc39" Argument STRING string to process.'." (replace-regexp-in-string "\n" "" string)) + (defun ztree-file-short-name (file) "By given FILE name return base file/directory name. Taken from http://lists.gnu.org/archive/html/emacs-devel/2011-01/msg01238.html" - (ztree-printable-string (file-name-nondirectory (directory-file-name file)))) + (let* ((dir (directory-file-name file)) + (simple-dir (file-name-nondirectory dir))) + ;; check if the root directory + (if (string= "" simple-dir) + dir + (ztree-printable-string simple-dir)))) + (defun ztree-car-atom (value) "Return VALUE if value is an atom, otherwise (car value) or nil. @@ -79,6 +86,13 @@ Used since `car-safe' returns nil for atoms" (file2-remote (file-remote-p file2))) (string-equal file1-remote file2-remote))) + +(defun ztree-scroll-to-line (line) + "Recommended way to set the cursor to specified LINE." + (goto-char (point-min)) + (forward-line (1- line))) + + (provide 'ztree-util) ;;; ztree-util.el ends here diff --git a/ztree-view.el b/ztree-view.el index f1a9afd..8cf0ced 100644 --- a/ztree-view.el +++ b/ztree-view.el @@ -192,17 +192,13 @@ or nil if there is no node" "For given LINE set the PARENT in the global array." (aset ztree-parent-lines-array (- line ztree-start-line) parent)) + (defun ztree-get-parent-for-line (line) "For given LINE return a parent." (when (and (>= line ztree-start-line) (< line (+ (length ztree-parent-lines-array) ztree-start-line))) (aref ztree-parent-lines-array (- line ztree-start-line)))) -(defun scroll-to-line (line) - "Recommended way to set the cursor to specified LINE." - (goto-char (point-min)) - (forward-line (1- line))) - (defun ztree-do-toggle-expand-subtree-iter (node state) "Iteration in expanding subtree. @@ -303,7 +299,7 @@ then close the node." (setq ztree-count-subsequent-bs t) (ztree-refresh-buffer line)) (progn (setq ztree-count-subsequent-bs nil) - (scroll-to-line parent))))))) + (ztree-scroll-to-line parent))))))) (defun ztree-get-splitted-node-contens (node) @@ -327,7 +323,7 @@ Argument NODE node which contents will be returned." "Draw char C at the position (1-based) (X Y). Optional argument FACE face to use to draw a character." (save-excursion - (scroll-to-line y) + (ztree-scroll-to-line y) (beginning-of-line) (goto-char (+ x (-(point) 1))) (delete-char 1) @@ -335,15 +331,15 @@ Optional argument FACE face to use to draw a character." (put-text-property (1- (point)) (point) 'font-lock-face (if face face 'ztreep-arrow-face)))) (defun ztree-vertical-line-char () - "Return the character used to draw vertical line" + "Return the character used to draw vertical line." (if ztree-draw-unicode-lines #x2502 ?\|)) (defun ztree-horizontal-line-char () - "Return the character used to draw vertical line" + "Return the character used to draw vertical line." (if ztree-draw-unicode-lines #x2500 ?\-)) (defun ztree-left-bottom-corner-char () - "Return the character used to draw vertical line" + "Return the character used to draw vertical line." (if ztree-draw-unicode-lines #x2514 ?\`)) (defun ztree-left-intersection-char () @@ -614,7 +610,16 @@ Optional argument LINE scroll to the line given." (funcall ztree-tree-header-fun) (setq ztree-start-line (line-number-at-pos (point))) (ztree-insert-node-contents ztree-start-node)) - (scroll-to-line (if line line ztree-start-line)))) + (ztree-scroll-to-line (if line line ztree-start-line)))) + + +(defun ztree-change-start-node (node) + "Refresh the buffer setting the new root NODE. +This will reuse all other settings for the current ztree buffer, but +change the root node to the node specified." + (setq ztree-start-node node + ztree-expanded-nodes-list (list ztree-start-node)) + (ztree-refresh-buffer)) (defun ztree-view (