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?
This commit is contained in:
parent
b65d05e2d3
commit
e38ae56584
|
@ -64,28 +64,31 @@ If already in `*ansi-term*' buffer, bury it."
|
||||||
(defun db/run-or-hide-shell (arg)
|
(defun db/run-or-hide-shell (arg)
|
||||||
"Opens a shell buffer in new window if not already in one.
|
"Opens a shell buffer in new window if not already in one.
|
||||||
Otherwise, closes the current shell window. With ARG, switch
|
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
|
;; idea to split the current window is from
|
||||||
;; http://howardism.org/Technical/Emacs/eshell-fun.html
|
;; http://howardism.org/Technical/Emacs/eshell-fun.html
|
||||||
(interactive "P")
|
(interactive "P")
|
||||||
(if (derived-mode-p 'shell-mode)
|
(cl-flet ((change-to-shell ()
|
||||||
;; bury buffer; reopen with current working directory if arg is given
|
(if-let ((shell-window (db/find-window-by-buffer-mode 'shell-mode)))
|
||||||
(progn
|
(select-window shell-window)
|
||||||
(bury-buffer)
|
;; open shell in buffer with height of ⅓ of current window
|
||||||
(delete-window)
|
(let ((height (/ (window-total-height) 3)))
|
||||||
(and arg (db/run-or-hide-shell arg)))
|
(shell)
|
||||||
(if-let ((shell-window (db/find-window-by-buffer-mode 'shell-mode)))
|
(enlarge-window (- height (window-total-height)))))))
|
||||||
(select-window shell-window)
|
(if (not arg)
|
||||||
;; open shell
|
;; toggle shell window
|
||||||
(let ((current-dir (expand-file-name default-directory))
|
(if (not (derived-mode-p 'shell-mode))
|
||||||
(height (/ (window-total-height) 3)))
|
(change-to-shell)
|
||||||
(shell)
|
(bury-buffer)
|
||||||
(enlarge-window (- height (window-total-height)))
|
(delete-window))
|
||||||
(when arg
|
|
||||||
(end-of-line)
|
;; unconditionally go to shell, and also change to cwd
|
||||||
(comint-kill-input)
|
(let ((current-dir (expand-file-name default-directory)))
|
||||||
(insert (format "cd '%s'" current-dir))
|
(change-to-shell)
|
||||||
(comint-send-input))))))
|
(end-of-line)
|
||||||
|
(comint-kill-input)
|
||||||
|
(insert (format "cd '%s'" current-dir))
|
||||||
|
(comint-send-input)))))
|
||||||
|
|
||||||
|
|
||||||
;;; General Utilities
|
;;; General Utilities
|
||||||
|
|
Loading…
Reference in New Issue