From 9441800587329a76ff9a96222dc0353fced8a09f Mon Sep 17 00:00:00 2001 From: Daniel Borchmann Date: Thu, 12 Oct 2023 16:39:29 +0200 Subject: [PATCH] Also change to `default-directory` when not in eshell already MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit This is the expected behavior, and also how `db/run-or-hide-shell` works. The implementation is structured differently, though … because I reinvented the implementation of `db/run-or-hide-eshell` instead of generalizing the one of `db/run-or-hide-shell`. Ah, anyway … --- site-lisp/db-eshell.el | 40 ++++++++++++++++++++++------------------ 1 file changed, 22 insertions(+), 18 deletions(-) diff --git a/site-lisp/db-eshell.el b/site-lisp/db-eshell.el index de6f3f7..2551a7f 100644 --- a/site-lisp/db-eshell.el +++ b/site-lisp/db-eshell.el @@ -30,25 +30,29 @@ The buffer's name has to start with “*eshell*” to be recognized by this function. Otherwise the current buffer is not treated as an eshell buffer. -When ARG is given, also switch to `default-directory'." +When ARG is given, change to `default-directory' after switching +to the eshell buffer." (interactive "P") - (if (and (derived-mode-p 'eshell-mode) - (string-match-p "^\\*eshell\\*" (buffer-name))) - ;; bury buffer; reopen with current working directory if arg is given - (progn - (bury-buffer) - (and arg (db/run-or-hide-eshell arg))) - (if-let ((eshell-window (cl-find-if (lambda (window) - (with-current-buffer (window-buffer window) - (and (derived-mode-p 'eshell-mode) - (string-match-p "^\\*eshell\\*" (buffer-name))))) - (window-list-1)))) - (select-window eshell-window) - ;; No running eshell found, open new one. - (let* ((current-dir (expand-file-name default-directory))) - (--if-let (display-buffer (eshell 1)) - (select-window it) - (error "Could not start eshell (`display-buffer' returned nil)")) + (let ((current-dir (expand-file-name default-directory))) + (cl-flet ((in-eshell-buffer-p () + (and (derived-mode-p 'eshell-mode) + (string-match-p "^\\*eshell\\*" (buffer-name))))) + + (if (and (not arg) + (in-eshell-buffer-p)) + (bury-buffer) + + (unless (in-eshell-buffer-p) + (if-let ((eshell-window (cl-find-if (lambda (window) + (with-current-buffer (window-buffer window) + (in-eshell-buffer-p))) + (window-list-1)))) + (select-window eshell-window) + ;; No running eshell found, open new one. + (--if-let (display-buffer (eshell 1)) + (select-window it) + (error "Could not start eshell (`display-buffer' returned nil)")))) + (when arg (end-of-line) (eshell-kill-input)