Skip to content

Instantly share code, notes, and snippets.

@willtim
Created August 10, 2017 16:29
Show Gist options
  • Save willtim/17450a6d096f73d470d7bef429a8ae95 to your computer and use it in GitHub Desktop.
Save willtim/17450a6d096f73d470d7bef429a8ae95 to your computer and use it in GitHub Desktop.
Emacs project explorer
;; project tree explorer
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; see https://github.com/nathankot/dotemacs/blob/master/init.el
(use-package projectile
:ensure projectile
:config (projectile-global-mode t)
:init
;; (setq projectile-require-project-root nil)
(setq projectile-enable-caching t)
(setq projectile-completion-system 'ivy)
;; (setq projectile-indexing-method 'alien)
)
(use-package dired-subtree
:demand
:bind
(:map dired-mode-map
("<enter>" . dwim-toggle-or-open)
("<return>" . dwim-toggle-or-open)
("<tab>" . dwim-toggle-or-open)
("<down-mouse-1>" . mouse-dwim-to-toggle-or-open))
:config
(progn
;; Function to customize the line prefixes (I simply indent the lines a bit)
;; (setq dired-subtree-line-prefix (lambda (depth) (make-string (* 2 depth) ?\s)))
(setq dired-subtree-use-backgrounds nil)))
(defun dwim-toggle-or-open ()
"Toggle subtree or open the file."
(interactive)
(if (file-directory-p (dired-get-file-for-visit))
(progn
(dired-subtree-toggle)
(revert-buffer))
(dired-find-file)))
(defun mouse-dwim-to-toggle-or-open (event)
"Toggle subtree or the open file on mouse-click in dired."
(interactive "e")
(let* ((window (posn-window (event-end event)))
(buffer (window-buffer window))
(pos (posn-point (event-end event))))
(progn
(with-current-buffer buffer
(goto-char pos)
(dwim-toggle-or-open)))))
;;(defun cycle-project-explorer ()
(defun toggle-project-explorer ()
"Toggle the project explorer window."
(interactive)
(let* ((buffer (dired-noselect (projectile-project-root)))
(window (get-buffer-window buffer)))
(if window
(hide-project-explorer)
(show-project-explorer))))
(defun show-project-explorer ()
"Project dired buffer on the side of the frame.
Shows the projectile root folder using dired on the left side of
the frame and makes it a dedicated window for that buffer."
(let ((buffer (dired-noselect (projectile-project-root))))
(progn
(with-current-buffer buffer (dired-hide-details-mode 1))
(display-buffer-in-side-window buffer '((side . left) (window-width . 0.2)))
(set-window-dedicated-p (get-buffer-window buffer) t))))
(defun hide-project-explorer ()
"Hide the project-explorer window."
(let ((buffer (dired-noselect (projectile-project-root))))
(progn
(delete-window (get-buffer-window buffer))
(kill-buffer buffer))))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment