tools/eshell: refactor; rename split commands; switch to new buffers properly

This commit is contained in:
Henrik Lissner 2018-03-24 17:51:55 -04:00
parent a067500ef2
commit 3f88909d52
No known key found for this signature in database
GPG key ID: 5F6C0EA160557395

View file

@ -25,6 +25,13 @@
;; Library ;; Library
;; ;;
(defun +eshell--add-buffer (buf)
(ring-remove+insert+extend +eshell-buffers buf))
(defun +eshell--remove-buffer (buf)
(when-let* ((idx (ring-member +eshell-buffers buf)))
(ring-remove +eshell-buffers idx)))
(defun +eshell--current-git-branch () (defun +eshell--current-git-branch ()
(let ((branch (car (cl-loop for match in (split-string (shell-command-to-string "git branch") "\n") (let ((branch (car (cl-loop for match in (split-string (shell-command-to-string "git branch") "\n")
if (string-match-p "^\*" match) if (string-match-p "^\*" match)
@ -33,12 +40,11 @@
(format " [%s]" (substring branch 2)) (format " [%s]" (substring branch 2))
""))) "")))
;;;###autoload (defun +eshell-delete-window ()
(defun +eshell-prompt () (if (one-window-p)
"Generate the prompt string for eshell. Use for `eshell-prompt-function'." (unless (doom-real-buffer-p (progn (previous-buffer) (current-buffer)))
(concat (propertize (abbreviate-file-name (eshell/pwd)) 'face '+eshell-prompt-pwd) (switch-to-buffer (doom-fallback-buffer)))
(propertize (+eshell--current-git-branch) 'face '+eshell-prompt-git-branch) (delete-window)))
(propertize " λ " 'face '+eshell-prompt-char)))
(defun +eshell-get-or-create-buffer () (defun +eshell-get-or-create-buffer ()
(or (cl-loop for buf in (ring-elements +eshell-buffers) (or (cl-loop for buf in (ring-elements +eshell-buffers)
@ -47,6 +53,13 @@
return buf) return buf)
(generate-new-buffer +eshell-buffer-name))) (generate-new-buffer +eshell-buffer-name)))
;;;###autoload
(defun +eshell-prompt ()
"Generate the prompt string for eshell. Use for `eshell-prompt-function'."
(concat (propertize (abbreviate-file-name (eshell/pwd)) 'face '+eshell-prompt-pwd)
(propertize (+eshell--current-git-branch) 'face '+eshell-prompt-git-branch)
(propertize " λ " 'face '+eshell-prompt-char)))
;; ;;
;; Commands ;; Commands
@ -59,9 +72,7 @@
(let ((buf (+eshell-get-or-create-buffer))) (let ((buf (+eshell-get-or-create-buffer)))
(with-current-buffer buf (with-current-buffer buf
(unless (eq major-mode 'eshell-mode) (eshell-mode))) (unless (eq major-mode 'eshell-mode) (eshell-mode)))
(if (eq major-mode 'eshell-mode) (switch-to-buffer buf)
(pop-to-buffer buf)
(switch-to-buffer buf))
(when command (when command
(+eshell-run-command command)))) (+eshell-run-command command))))
@ -109,17 +120,12 @@ module to be loaded."
;; Hooks ;; Hooks
;; ;;
(defun +eshell--add-buffer (buf)
(ring-remove+insert+extend +eshell-buffers buf))
(defun +eshell--remove-buffer (buf)
(when-let* ((idx (ring-member +eshell-buffers buf)))
(ring-remove +eshell-buffers idx)))
;;;###autoload ;;;###autoload
(defun +eshell|init () (defun +eshell|init ()
"Keep track of eshell buffers." "Keep track of eshell buffers."
(let ((buf (current-buffer))) (let ((buf (current-buffer)))
(remove-hook 'kill-buffer-query-functions #'doom|protect-visible-buffers t)
(add-hook 'kill-buffer-hook #'+eshell-delete-window nil t)
(dolist (buf (ring-elements +eshell-buffers)) (dolist (buf (ring-elements +eshell-buffers))
(unless (buffer-live-p buf) (unless (buffer-live-p buf)
(+eshell--remove-buffer buf))) (+eshell--remove-buffer buf)))
@ -149,13 +155,15 @@ delete."
(delete-char arg))) (delete-char arg)))
;;;###autoload ;;;###autoload
(defun +eshell/split () (defun +eshell/split-below ()
"Create a new eshell window below the current one."
(interactive) (interactive)
(select-window (split-window-vertically)) (select-window (split-window-vertically))
(+eshell/open)) (+eshell/open))
;;;###autoload ;;;###autoload
(defun +eshell/vsplit () (defun +eshell/split-right ()
"Create a new eshell window to the right of the current one."
(interactive) (interactive)
(select-window (split-window-horizontally)) (select-window (split-window-horizontally))
(+eshell/open)) (+eshell/open))