2017-06-08 11:47:56 +02:00
|
|
|
;;; tools/eshell/autoload/eshell.el -*- lexical-binding: t; -*-
|
2017-02-19 18:53:38 -05:00
|
|
|
|
2017-09-27 12:42:24 +02: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
|
|
|
|
2017-09-27 14:48:05 +02:00
|
|
|
|
|
|
|
;; --- Commands ---------------------------
|
|
|
|
|
2017-02-19 18:53:38 -05:00
|
|
|
;;;###autoload
|
2017-09-27 14:48:05 +02:00
|
|
|
(defun +eshell/open (&optional command)
|
2017-02-19 18:53:38 -05:00
|
|
|
"Open eshell in the current buffer."
|
|
|
|
(interactive)
|
2017-09-27 12:42:24 +02:00
|
|
|
(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)))
|
2017-09-27 14:48:05 +02:00
|
|
|
(switch-to-buffer buf)
|
|
|
|
(when command
|
|
|
|
(+eshell-run-command command))))
|
2017-02-19 18:53:38 -05:00
|
|
|
|
|
|
|
;;;###autoload
|
2017-09-27 14:48:05 +02:00
|
|
|
(defun +eshell/open-popup (&optional command)
|
2017-02-19 18:53:38 -05:00
|
|
|
"Open eshell in a popup window."
|
|
|
|
(interactive)
|
2017-09-27 12:42:24 +02:00
|
|
|
(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)))
|
2017-09-27 14:48:05 +02:00
|
|
|
(doom-popup-buffer buf '(:autokill t) t)
|
|
|
|
(when command
|
|
|
|
(+eshell-run-command command))))
|
2017-02-19 18:53:38 -05:00
|
|
|
|
|
|
|
;;;###autoload
|
2017-09-27 14:48:05 +02:00
|
|
|
(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)
|
2017-02-23 00:06:12 -05:00
|
|
|
(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"))
|
2017-09-27 12:42:24 +02:00
|
|
|
(if-let (buf (cl-find-if (lambda (it) (string-match-p "^\\*doom:eshell" (buffer-name (window-buffer it))))
|
2017-02-19 18:53:38 -05:00
|
|
|
(doom-visible-windows)))
|
|
|
|
(select-window (get-buffer-window buf))
|
2017-09-27 12:42:24 +02:00
|
|
|
(+eshell/open))
|
2017-09-27 14:48:05 +02:00
|
|
|
(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
|
|
|
|
2017-09-27 12:42:24 +02: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 ------------------------
|
|
|
|
|
2017-07-21 16:50:44 +02:00
|
|
|
;;;###autoload
|
|
|
|
(defun +eshell/quit-or-delete-char (arg)
|
|
|
|
(interactive "p")
|
2017-07-22 00:12:04 +02:00
|
|
|
(if (and (eolp) (looking-back eshell-prompt-regexp nil))
|
2017-07-21 16:50:44 +02:00
|
|
|
(eshell-life-is-too-much)
|
2017-07-22 00:12:04 +02:00
|
|
|
(delete-char arg)))
|
2017-07-21 16:50:44 +02:00
|
|
|
|
2017-06-08 11:47:56 +02:00
|
|
|
(defun +eshell--outside-prompt-p ()
|
2017-02-19 18:53:38 -05:00
|
|
|
(< (point) eshell-last-output-end))
|
|
|
|
|
|
|
|
(defun +eshell--current-git-branch ()
|
|
|
|
(let ((branch (car (loop for match in (split-string (shell-command-to-string "git branch") "\n")
|
|
|
|
when (string-match "^\*" match)
|
|
|
|
collect match))))
|
|
|
|
(if (not (eq branch nil))
|
|
|
|
(concat " [" (substring branch 2) "]")
|
|
|
|
"")))
|
|
|
|
|
|
|
|
;;;###autoload
|
|
|
|
(defun +eshell/split ()
|
|
|
|
(interactive)
|
|
|
|
(select-window (split-window-vertically))
|
2017-09-27 12:42:24 +02:00
|
|
|
(+eshell/open))
|
2017-02-19 18:53:38 -05:00
|
|
|
|
|
|
|
;;;###autoload
|
|
|
|
(defun +eshell/vsplit ()
|
|
|
|
(interactive)
|
|
|
|
(select-window (split-window-horizontally))
|
2017-09-27 12:42:24 +02:00
|
|
|
(+eshell/open))
|
2017-02-19 18:53:38 -05:00
|
|
|
|
|
|
|
;;;###autoload
|
2017-09-27 12:42:24 +02:00
|
|
|
(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)))
|
|
|
|
|
|
|
|
;;;###autoload
|
|
|
|
(defun +eshell/evil-append ()
|
|
|
|
(interactive)
|
|
|
|
(goto-char eshell-last-output-end)
|
2017-04-17 02:17:10 -04:00
|
|
|
(call-interactively #'evil-append-line))
|
2017-02-19 18:53:38 -05:00
|
|
|
|
|
|
|
;;;###autoload
|
|
|
|
(defun +eshell/evil-append-maybe ()
|
|
|
|
(interactive)
|
|
|
|
(if (+eshell--outside-prompt-p)
|
|
|
|
(+eshell/evil-append)
|
2017-04-17 02:17:10 -04:00
|
|
|
(call-interactively #'evil-append)))
|
2017-02-19 18:53:38 -05:00
|
|
|
|
|
|
|
;;;###autoload
|
|
|
|
(defun +eshell/evil-prepend ()
|
|
|
|
(interactive)
|
|
|
|
(goto-char eshell-last-output-end)
|
2017-04-17 02:17:10 -04:00
|
|
|
(call-interactively #'evil-insert))
|
2017-02-19 18:53:38 -05:00
|
|
|
|
|
|
|
;;;###autoload
|
|
|
|
(defun +eshell/evil-prepend-maybe ()
|
|
|
|
(interactive)
|
|
|
|
(if (+eshell--outside-prompt-p)
|
|
|
|
(+eshell/evil-prepend)
|
2017-04-17 02:17:10 -04:00
|
|
|
(call-interactively #'evil-insert)))
|
2017-02-19 18:53:38 -05:00
|
|
|
|
|
|
|
;;;###autoload
|
|
|
|
(defun +eshell/evil-replace-maybe ()
|
|
|
|
(interactive)
|
|
|
|
(if (+eshell--outside-prompt-p)
|
|
|
|
(user-error "Cannot edit read-only region")
|
2017-04-17 02:17:10 -04:00
|
|
|
(call-interactively #'evil-replace)))
|
2017-02-19 18:53:38 -05:00
|
|
|
|
|
|
|
;;;###autoload
|
|
|
|
(defun +eshell/evil-replace-state-maybe ()
|
|
|
|
(interactive)
|
|
|
|
(if (+eshell--outside-prompt-p)
|
|
|
|
(user-error "Cannot edit read-only region")
|
2017-04-17 02:17:10 -04:00
|
|
|
(call-interactively #'evil-replace-state)))
|
2017-02-19 18:53:38 -05:00
|
|
|
|
|
|
|
;;;###autoload
|
|
|
|
(defun +eshell/evil-change ()
|
|
|
|
(interactive)
|
|
|
|
(when (+eshell--outside-prompt-p)
|
|
|
|
(goto-char eshell-last-output-end))
|
2017-04-17 02:17:10 -04:00
|
|
|
(call-interactively #'evil-change))
|
2017-02-19 18:53:38 -05:00
|
|
|
|
|
|
|
;;;###autoload
|
|
|
|
(defun +eshell/evil-change-line ()
|
|
|
|
(interactive)
|
|
|
|
(when (+eshell--outside-prompt-p)
|
|
|
|
(goto-char eshell-last-output-end))
|
2017-04-17 02:17:10 -04:00
|
|
|
(call-interactively #'evil-change-line))
|