diff --git a/init.el b/init.el index 2c86d58..7056b21 100644 --- a/init.el +++ b/init.el @@ -2789,6 +2789,7 @@ Note that this workaround is incomplete, as explained in this comment." (use-package db-eshell :commands (db/run-or-hide-project-eshell + db/run-or-hide-project-eshell-here eshell-clear-buffer eshell/default-prompt-function pcomplete/git)) @@ -3122,7 +3123,7 @@ Note that this workaround is incomplete, as explained in this comment." ;; Top-Level Keybindings (bind-key "" 'scroll-lock-mode) - (bind-key "" #'db/run-or-hide-project-eshell) + (bind-key "" #'db/run-or-hide-project-eshell-here) (bind-key "" #'db/frequently-used-features-prefix) (bind-key "" #'project-find-regexp) (bind-key "" #'text-scale-adjust) diff --git a/site-lisp/db-eshell.el b/site-lisp/db-eshell.el index 57826f0..1a3ba35 100644 --- a/site-lisp/db-eshell.el +++ b/site-lisp/db-eshell.el @@ -34,6 +34,35 @@ eshell instance is started via `project-eshell'." (bury-buffer) (delete-window)))) +(defun db/run-or-hide-project-eshell-here () + "Opens an eshell buffer in the current directory. + +The current directory is determined by the file name associated with the +current buffer, or `default-directory'. + +Switch to an existing eshell buffer open in the current directory. +Create an eshell buffer otherwise." + (interactive) + (if (not (derived-mode-p 'eshell-mode)) + (let* ((cwd (or (file-name-directory (buffer-file-name (current-buffer))) + default-directory)) + (eshell-buffer (--> (buffer-list) + (-filter #'(lambda (buf) + (with-current-buffer buf + (and (derived-mode-p 'eshell-mode) + (file-equal-p cwd default-directory)))) + it) + (sort it :key #'buffer-name)))) + (if eshell-buffer + (--if-let (display-buffer (-first-item eshell-buffer)) + (select-window it) + (error "Cannot display existing eshell buffer")) + (let ((default-directory cwd)) + (eshell t)))) + (progn + (bury-buffer) + (delete-window)))) + (defun eshell-clear-buffer () "Clear terminal." (interactive)