From 2f1868da430673e801c0105f1bba8e1df712058e Mon Sep 17 00:00:00 2001 From: Daniel Borchmann Date: Sun, 16 Apr 2023 19:01:01 +0200 Subject: [PATCH] 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. --- site-lisp/db-eshell.el | 19 +++++++++++++++---- site-lisp/db-utils.el | 25 +++++++++++++++++-------- 2 files changed, 32 insertions(+), 12 deletions(-) diff --git a/site-lisp/db-eshell.el b/site-lisp/db-eshell.el index d9dbfe2..ac783d5 100644 --- a/site-lisp/db-eshell.el +++ b/site-lisp/db-eshell.el @@ -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))) diff --git a/site-lisp/db-utils.el b/site-lisp/db-utils.el index 4774200..924cb11 100644 --- a/site-lisp/db-utils.el +++ b/site-lisp/db-utils.el @@ -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)))