Refactor eshell integration

This commit is contained in:
Henrik Lissner 2016-06-05 23:09:25 -04:00
parent 8a666aedfe
commit caae5946cf
2 changed files with 89 additions and 43 deletions

View file

@ -2,8 +2,12 @@
(require 'eshell) (require 'eshell)
(defun doom--eshell-in-prompt-p (&optional offset) (defvar doom-eshell-buffers '() "")
(>= (- (point) (or offset 0)) (save-excursion (eshell-bol) (point)))) (defvar doom-eshell-height 16 "")
(defvar-local doom-eshell-direction nil "")
(defun doom--eshell-outside-prompt-p (&optional offset)
(< (point) eshell-last-output-end))
(defun doom--eshell-current-git-branch () (defun doom--eshell-current-git-branch ()
(let ((branch (car (loop for match in (split-string (shell-command-to-string "git branch") "\n") (let ((branch (car (loop for match in (split-string (shell-command-to-string "git branch") "\n")
@ -13,61 +17,88 @@
(concat " [" (substring branch 2) "]") (concat " [" (substring branch 2) "]")
""))) "")))
;;;###autoload
(defun doom/eshell-split ()
(interactive)
(select-window (split-window-vertically doom-eshell-height))
(setq-local doom-eshell-direction 'below)
(doom--eshell-init t))
;;;###autoload
(defun doom/eshell-vsplit ()
(interactive)
(select-window (split-window-horizontally))
(setq-local doom-eshell-direction 'right)
(doom--eshell-init t))
;;;###autoload ;;;###autoload
(defun doom/eshell (&optional same &rest _) (defun doom/eshell (&optional same &rest _)
(interactive) (interactive)
(let ((buf (get-buffer-create eshell-buffer-name))) (doom--eshell-init same)
(cl-assert (and buf (buffer-live-p buf))) ;; (if doom-eshell-buffers
(doom/popup-buffer buf) ;; (let* ((buf (car (reverse doom-eshell-buffers)))
(with-current-buffer buf ;; (win (get-buffer-window buf)))
(unless (derived-mode-p 'eshell-mode) ;; (if (and win (window-live-p win))
(eshell-mode))))) ;; (select-window win)
;; (select-window (split-window-vertically doom-eshell-height))
;; (evil-window-move-very-bottom)
;; (switch-to-buffer buf t t)))
;; (doom--eshell-init same))
)
(defun doom--eshell-init (&optional same)
(unless same (select-window (split-window)))
(eshell (max 0 (1- (length doom-eshell-buffers))))
(unless same
(evil-window-move-very-bottom)
(evil-window-set-height doom-eshell-height))
(set-window-dedicated-p (selected-window) t))
;;;###autoload ;;;###autoload
(defun doom/eshell-prompt () (defun doom/eshell-prompt ()
(concat (propertize (abbreviate-file-name (eshell/pwd)) 'face 'eshell-prompt) (concat (propertize (abbreviate-file-name (eshell/pwd)) 'face 'eshell-prompt)
(propertize (doom--eshell-current-git-branch) 'face 'font-lock-function-name-face) (propertize (doom--eshell-current-git-branch) 'face 'font-lock-function-name-face)
(propertize " $ " 'face 'font-lock-constant-face))) (propertize " λ " 'face 'font-lock-constant-face)))
;;;###autoload ;;;###autoload
(defun doom/eshell-evil-append () (defun doom/eshell-evil-append ()
(interactive) (interactive)
(goto-char (point-max)) (goto-char eshell-last-output-end)
(call-interactively 'evil-append)) (call-interactively 'evil-append-line))
;;;###autoload ;;;###autoload
(defun doom/eshell-evil-append-maybe () (defun doom/eshell-evil-append-maybe ()
(interactive) (interactive)
(if (doom--eshell-in-prompt-p) (if (doom--eshell-outside-prompt-p)
(call-interactively 'evil-insert) (doom/eshell-evil-append)
(doom/eshell-append))) (call-interactively 'evil-append)))
;;;###autoload ;;;###autoload
(defun doom/eshell-evil-prepend () (defun doom/eshell-evil-prepend ()
(interactive) (interactive)
(eshell-bol) (goto-char eshell-last-output-end)
(call-interactively 'evil-insert)) (call-interactively 'evil-insert))
;;;###autoload ;;;###autoload
(defun doom/eshell-evil-prepend-maybe () (defun doom/eshell-evil-prepend-maybe ()
(interactive) (interactive)
(if (doom--eshell-in-prompt-p) (if (doom--eshell-outside-prompt-p)
(call-interactively 'evil-insert) (doom/eshell-evil-prepend)
(doom/eshell-prepend))) (call-interactively 'evil-insert)))
;;;###autoload ;;;###autoload
(defun doom/eshell-evil-replace-maybe () (defun doom/eshell-evil-replace-maybe ()
(interactive) (interactive)
(if (doom--eshell-in-prompt-p) (if (doom--eshell-outside-prompt-p)
(call-interactively 'evil-replace) (user-error "Cannot edit read-only region")
(user-error "Cannot edit read-only region"))) (call-interactively 'evil-replace)))
;;;###autoload ;;;###autoload
(defun doom/eshell-evil-replace-state-maybe () (defun doom/eshell-evil-replace-state-maybe ()
(interactive) (interactive)
(if (doom--eshell-in-prompt-p) (if (doom--eshell-outside-prompt-p)
(call-interactively 'evil-replace-state) (user-error "Cannot edit read-only region")
(user-error "Cannot edit read-only region"))) (call-interactively 'evil-replace-state)))
(provide 'defuns-eshell) (provide 'defuns-eshell)
;;; defuns-eshell.el ends here ;;; defuns-eshell.el ends here

View file

@ -1,37 +1,52 @@
;;; module-eshell.el --- -*- no-byte-compile: t; -*- ;;; module-eshell.el --- -*- no-byte-compile: t; -*-
(use-package eshell (use-package eshell
:when IS-WINDOWS
:init :init
(evil-set-initial-state 'eshell-mode 'emacs)
(setq eshell-directory-name (concat doom-temp-dir "/eshell") (setq eshell-directory-name (concat doom-temp-dir "/eshell")
eshell-scroll-to-bottom-on-input 'all eshell-scroll-to-bottom-on-input 'all
eshell-scroll-to-bottom-on-output 'all
eshell-buffer-shorthand t eshell-buffer-shorthand t
;; em-prompt
eshell-prompt-function 'doom/eshell-prompt
;; em-glob ;; em-glob
eshell-glob-case-insensitive t eshell-glob-case-insensitive t
eshell-error-if-no-glob t eshell-error-if-no-glob t
eshell-where-to-jump 'end
;; em-alias ;; em-alias
eshell-aliases-file (concat doom-temp-dir "/.eshell-aliases")) eshell-aliases-file (concat doom-temp-dir "/.eshell-aliases"))
:config :config
(def-popup! eshell-mode :frame t :select t) (evil-set-initial-state 'eshell-mode 'insert)
;; plan 9 smart shell (defun doom|eshell-keymap-setup ()
(require 'em-smart) (map! :map eshell-mode-map
(push 'eshell-smart eshell-modules-list) :n "i" 'doom/eshell-evil-prepend-maybe
(setq eshell-where-to-jump 'begin) :n "I" 'doom/eshell-evil-prepend
(setq eshell-review-quick-commands nil) :n "a" 'doom/eshell-evil-append-maybe
(setq eshell-smart-space-goes-to-end t) :n "A" 'doom/eshell-evil-append
;; em-prompt :n "r" 'doom/eshell-evil-replace-maybe
(setq eshell-prompt-function 'doom/eshell-prompt) :n "R" 'doom/eshell-evil-replace-state-maybe
(map! :map eshell-mode-map :i "C-u" 'eshell-kill-input
:n "i" 'doom/eshell-evil-prepend-maybe :i "SPC" 'self-insert-command
:n "I" 'doom/eshell-evil-prepend :m "<return>" 'doom/eshell-evil-append
:n "a" 'doom/eshell-evil-append-maybe :n [remap doom/evil-window-split] 'doom/eshell-split
:n "A" 'doom/eshell-evil-append :n [remap doom/evil-window-vsplit] 'doom/eshell-vsplit))
:n "r" 'doom/eshell-evil-replace-maybe
:n "R" 'doom/eshell-evil-replace-state-maybe)) (defun doom|eshell-init ()
(when (eq major-mode 'eshell-mode)
(add-to-list 'doom-eshell-buffers (current-buffer))))
(defun doom|eshell-cleanup ()
(when (eq major-mode 'eshell-mode)
(setq doom-eshell-buffers (delete (current-buffer) doom-eshell-buffers))
(delete-window)))
;; Close window on exit
(add-hook 'eshell-exit-hook 'doom|eshell-cleanup)
(add-hook 'eshell-mode-hook 'doom|eshell-init)
(add-hook 'eshell-mode-hook 'doom|eshell-keymap-setup)
(add-hook 'eshell-mode-hook 'doom-hide-mode-line-mode)
(add-hook 'eshell-mode-hook 'hl-line-mode))
(provide 'module-eshell) (provide 'module-eshell)
;;; module-eshell.el ends here ;;; module-eshell.el ends here