From e38ae56584c9ff373b3a0b78c7798be1e59f7d8a Mon Sep 17 00:00:00 2001 From: Daniel Borchmann Date: Fri, 31 Jul 2020 16:18:35 +0200 Subject: [PATCH] Rework logic of `db/run-or-hide-shell' It's a bit clearer now what the function is doing when ARG is given, I hope. It also turns out that switching to the current working directory does not make much sense when we are in the shell buffer, because CWD is then just the, well, current directory. The original logic used to CWD of the previous buffer (by closing the shell buffer and immediately reopening it), but that's actually not what the function is supposed to be doing, is it? --- site-lisp/db-utils.el | 41 ++++++++++++++++++++++------------------- 1 file changed, 22 insertions(+), 19 deletions(-) diff --git a/site-lisp/db-utils.el b/site-lisp/db-utils.el index a547cae..09c702d 100644 --- a/site-lisp/db-utils.el +++ b/site-lisp/db-utils.el @@ -64,28 +64,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' first." + to `default-directory' of the current buffer first." ;; idea to split the current window is from ;; http://howardism.org/Technical/Emacs/eshell-fun.html (interactive "P") - (if (derived-mode-p 'shell-mode) - ;; bury buffer; reopen with current working directory if arg is given - (progn - (bury-buffer) - (delete-window) - (and arg (db/run-or-hide-shell arg))) - (if-let ((shell-window (db/find-window-by-buffer-mode 'shell-mode))) - (select-window shell-window) - ;; open shell - (let ((current-dir (expand-file-name default-directory)) - (height (/ (window-total-height) 3))) - (shell) - (enlarge-window (- height (window-total-height))) - (when arg - (end-of-line) - (comint-kill-input) - (insert (format "cd '%s'" current-dir)) - (comint-send-input)))))) + (cl-flet ((change-to-shell () + (if-let ((shell-window (db/find-window-by-buffer-mode 'shell-mode))) + (select-window shell-window) + ;; open shell in buffer with height of ⅓ of current window + (let ((height (/ (window-total-height) 3))) + (shell) + (enlarge-window (- height (window-total-height))))))) + (if (not arg) + ;; toggle shell window + (if (not (derived-mode-p 'shell-mode)) + (change-to-shell) + (bury-buffer) + (delete-window)) + + ;; unconditionally go to shell, and also change to cwd + (let ((current-dir (expand-file-name default-directory))) + (change-to-shell) + (end-of-line) + (comint-kill-input) + (insert (format "cd '%s'" current-dir)) + (comint-send-input))))) ;;; General Utilities