Only treat automatically named (e)shell buffers special

This allows to have custom named (e)shell buffers that are treated like any
other buffer, and not as the special bottom shell buffers.
This commit is contained in:
Daniel - 2023-04-16 19:01:01 +02:00
parent 9904e24333
commit 2f1868da43
No known key found for this signature in database
GPG Key ID: 784AA8DF0CCDF625
2 changed files with 32 additions and 12 deletions

View File

@ -17,21 +17,32 @@
(require 'em-dirs)
(require 'em-hist)
(autoload 'magit-status "magit")
(autoload 'db/find-window-by-buffer-mode "db-utils")
;; Various
(defun db/run-or-hide-eshell (arg)
"Opens an eshell buffer if not already in one.
Otherwise moves the cursor to the window where we have been before."
Otherwise moves the cursor to the window where we have been before.
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'."
(interactive "P")
(if (derived-mode-p 'eshell-mode)
(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 (db/find-window-by-buffer-mode 'eshell-mode)))
(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)))

View File

@ -66,22 +66,31 @@ If already in `*ansi-term*' buffer, bury it."
(defun db/run-or-hide-shell (arg)
"Opens a shell buffer in new window if not already in one.
Otherwise, closes the current shell window. With ARG, switch
to `default-directory' of the current buffer first."
;; idea to split the current window is from
;; http://howardism.org/Technical/Emacs/eshell-fun.html
Otherwise, closes the current shell window.
The buffer's name has to start with *shell* to be recognized
by this function. Otherwise the current buffer is not treated as
a shell buffer.
With ARG, switch to `default-directory' of the current buffer first."
(interactive "P")
(cl-flet ((change-to-shell ()
(if-let ((shell-window (db/find-window-by-buffer-mode 'shell-mode)))
(if-let ((shell-window (cl-find-if (lambda (window)
(with-current-buffer (window-buffer window)
(and (derived-mode-p 'shell-mode)
(string-match-p "^\\*shell\\*" (buffer-name)))))
(window-list-1))))
(select-window shell-window)
(--if-let (display-buffer (shell))
(select-window it)
(error "Could not start shell (`display-buffer' returned nil)")))))
(if (not arg)
;; toggle shell window
(if (not (derived-mode-p 'shell-mode))
(change-to-shell)
(bury-buffer))
(if (and (derived-mode-p 'shell-mode)
(string-match-p "^\\*shell\\*" (buffer-name)))
(bury-buffer)
(change-to-shell))
;; unconditionally go to shell, and also change to cwd
(let ((current-dir (expand-file-name default-directory)))