doomemacs/modules/tools/eshell/autoload/eshell.el

114 lines
3.5 KiB
EmacsLisp
Raw Normal View History

;;; tools/eshell/autoload/eshell.el -*- lexical-binding: t; -*-
2017-02-19 18:53:38 -05:00
(defvar +eshell-buffers ()
"List of open eshell buffers.")
(defvar +eshell-buffer-name "*doom eshell*"
"The name to use for custom eshell buffers. This only affects `+eshell/open',
`+eshell/open-popup' and `+eshell/open-workspace'.")
2017-02-19 18:53:38 -05:00
;; --- Commands ---------------------------
2017-02-19 18:53:38 -05:00
;;;###autoload
(defun +eshell/open (&optional command)
2017-02-19 18:53:38 -05:00
"Open eshell in the current buffer."
(interactive)
(let ((buf (generate-new-buffer +eshell-buffer-name)))
2017-02-19 18:53:38 -05:00
(with-current-buffer buf
(unless (eq major-mode 'eshell-mode) (eshell-mode)))
(switch-to-buffer buf)
(when command
(+eshell-run-command command))))
2017-02-19 18:53:38 -05:00
;;;###autoload
(defun +eshell/open-popup (&optional command)
2017-02-19 18:53:38 -05:00
"Open eshell in a popup window."
(interactive)
(let ((buf (get-buffer-create +eshell-buffer-name)))
2017-02-19 18:53:38 -05:00
(with-current-buffer buf
(unless (eq major-mode 'eshell-mode) (eshell-mode)))
(pop-to-buffer buf)
(when command
(+eshell-run-command command))))
2017-02-19 18:53:38 -05:00
;;;###autoload
(defun +eshell/open-workspace (&optional command)
2017-02-19 18:53:38 -05:00
"Open eshell in a separate workspace. Requires the (:feature workspaces)
module to be loaded."
(interactive)
(unless (featurep! :feature workspaces)
2017-02-19 18:53:38 -05:00
(user-error ":feature workspaces is required, but disabled"))
(unless (+workspace-get "eshell" t)
(+workspace/new "eshell"))
(if-let* ((buf (cl-find-if (lambda (it) (string-match-p "^\\*doom:eshell" (buffer-name (window-buffer it))))
(doom-visible-windows))))
2017-02-19 18:53:38 -05:00
(select-window (get-buffer-window buf))
(+eshell/open))
(doom/workspace-display)
(when command
(+eshell-run-command command)))
(defun +eshell-run-command (command)
(unless (cl-remove-if-not #'buffer-live-p +eshell-buffers)
(user-error "No living eshell buffers available"))
(with-current-buffer (car +eshell-buffers)
(goto-char eshell-last-output-end)
(when (bound-and-true-p evil-mode)
(call-interactively #'evil-append-line))
(insert command)
(eshell-send-input nil t)))
2017-02-19 18:53:38 -05:00
;; --- Hooks ------------------------------
;;;###autoload
(defun +eshell|init ()
"Keep track of eshell buffers."
(cl-pushnew (current-buffer) +eshell-buffers :test #'eq))
;;;###autoload
(defun +eshell|cleanup ()
"Close window (or workspace) on quit."
(setq +eshell-buffers (delete (current-buffer) +eshell-buffers))
(when (and (featurep! :feature workspaces)
(string= "eshell" (+workspace-current-name)))
(+workspace/delete "eshell")))
;; --- Keybindings ------------------------
;;;###autoload
(defun +eshell/quit-or-delete-char (arg)
(interactive "p")
(if (and (eolp) (looking-back eshell-prompt-regexp nil))
(eshell-life-is-too-much)
(delete-char arg)))
2017-02-19 18:53:38 -05:00
(defun +eshell--current-git-branch ()
2018-03-23 18:17:59 -04:00
(let ((branch (car (cl-loop for match in (split-string (shell-command-to-string "git branch") "\n")
if (string-match-p "^\*" match)
collect match))))
(if (not (eq branch nil))
(concat " [" (substring branch 2) "]")
"")))
2017-02-19 18:53:38 -05:00
;;;###autoload
(defun +eshell/split ()
(interactive)
(select-window (split-window-vertically))
(+eshell/open))
2017-02-19 18:53:38 -05:00
;;;###autoload
(defun +eshell/vsplit ()
(interactive)
(select-window (split-window-horizontally))
(+eshell/open))
2017-02-19 18:53:38 -05:00
;;;###autoload
(defun +eshell-prompt ()
2017-02-19 18:53:38 -05:00
(concat (propertize (abbreviate-file-name (eshell/pwd)) 'face 'eshell-prompt)
(propertize (+eshell--current-git-branch) 'face 'font-lock-function-name-face)
(propertize " λ " 'face 'font-lock-constant-face)))