Also change to default-directory when not in eshell already

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 …
This commit is contained in:
Daniel Borchmann 2023-10-12 16:39:29 +02:00
parent 86879ec84e
commit 9441800587
No known key found for this signature in database
GPG Key ID: 784AA8DF0CCDF625

View File

@ -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 by this function. Otherwise the current buffer is not treated as
an eshell buffer. 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") (interactive "P")
(if (and (derived-mode-p 'eshell-mode) (let ((current-dir (expand-file-name default-directory)))
(string-match-p "^\\*eshell\\*" (buffer-name))) (cl-flet ((in-eshell-buffer-p ()
;; bury buffer; reopen with current working directory if arg is given (and (derived-mode-p 'eshell-mode)
(progn (string-match-p "^\\*eshell\\*" (buffer-name)))))
(bury-buffer)
(and arg (db/run-or-hide-eshell arg))) (if (and (not arg)
(if-let ((eshell-window (cl-find-if (lambda (window) (in-eshell-buffer-p))
(with-current-buffer (window-buffer window) (bury-buffer)
(and (derived-mode-p 'eshell-mode)
(string-match-p "^\\*eshell\\*" (buffer-name))))) (unless (in-eshell-buffer-p)
(window-list-1)))) (if-let ((eshell-window (cl-find-if (lambda (window)
(select-window eshell-window) (with-current-buffer (window-buffer window)
;; No running eshell found, open new one. (in-eshell-buffer-p)))
(let* ((current-dir (expand-file-name default-directory))) (window-list-1))))
(--if-let (display-buffer (eshell 1)) (select-window eshell-window)
(select-window it) ;; No running eshell found, open new one.
(error "Could not start eshell (`display-buffer' returned nil)")) (--if-let (display-buffer (eshell 1))
(select-window it)
(error "Could not start eshell (`display-buffer' returned nil)"))))
(when arg (when arg
(end-of-line) (end-of-line)
(eshell-kill-input) (eshell-kill-input)