Skip to content

Instantly share code, notes, and snippets.

@jmillikan
Last active December 16, 2015 11:29
Show Gist options
  • Save jmillikan/5427659 to your computer and use it in GitHub Desktop.
Save jmillikan/5427659 to your computer and use it in GitHub Desktop.
Open a shell instead of dired - with the name of the git root directory if possible
;; In .emacs or init.el or whatever: (add-to-list find-directory-functions '(nav-to-shell))
(require 'vc)
;; Fails on tramp names
(defun vc-dir-shell-name (user-dir-name)
(condition-case nil
(let* ((dir-name (expand-file-name user-dir-name))
(maybe-git-dir-name (vc-call-backend (vc-responsible-backend dir-name) 'root dir-name)))
(if maybe-git-dir-name
(file-name-nondirectory (directory-file-name maybe-git-dir-name))
nil))
(error nil))) ;; no VC or bad tramp filename or something
(defun stripped-dir-name (user-dir-name)
(file-name-nondirectory (directory-file-name user-dir-name)))
(defvar choose-dir-shell-name-functions
'(magit-dir-shell-name stripped-dir-name)
"Functions to use to choose the name of a shell buffer for a directory")
;; For find-directory-functions: Spawn a shell (possibly with a different name than the folder)
(defun nav-to-shell (dir-or-list &optional switches)
(condition-case nil
(if (listp dir-or-list) ;; Don't mass-spawn shells
nil
(let* ((dir-name
(concat
(directory-file-name (if (listp dir-or-list)
(car dir-or-list)
dir-or-list))
"/")) ;; default-directory needs a trailing slash for shell -> make-comint-in-buffer
(buffer-name (run-hook-with-args-until-success 'choose-dir-shell-name-functions dir-name))
(default-directory dir-name))
(shell buffer-name)))
(error
(progn
(message "nav-to-shell failed. Likely culprit - unshellable tramp folder.")
nil))))
(message "dir-shells loaded")
(provide 'dir-shells)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment