blob: 88524899053c4a65dbbbe0f5505a24147687d5e2 (
plain) (
tree)
|
|
;;; my-eshell.el --- Configure eshell
;;; Commentary:
;;; Code:
(require 'use-package)
(require 'cl-seq)
(require 'magit)
(require 'eshell)
(require 'esh-mode)
(require 'esh-module)
(setq eshell-modules-list
'(eshell-alias
eshell-basic
eshell-cmpl
eshell-dirs
eshell-glob
eshell-hist
eshell-ls
eshell-pred
eshell-prompt
eshell-script
eshell-term
eshell-tramp
eshell-unix))
(require 'em-alias)
(require 'em-basic)
(require 'em-dirs)
(require 'em-glob)
(require 'em-hist)
(require 'em-smart)
(require 'em-term)
(require 'em-tramp)
(require 'em-prompt)
(defun my/eshell-mode-setup ()
"Configures various aliases for eshell."
(eshell/alias "e" "find-file $1")
(eshell/alias "emacs" "find-file $1")
(eshell/alias "ee" "find-file-other-window $1")
(eshell/alias "ll" "ls -l")
(eshell/alias "d" "dired $1")
(eshell/alias "gs" "magit-status")
(eshell/alias "gd" "magit-diff-unstaged")
(eshell/alias "gds" "magit-diff-staged")
(eshell/alias "rg" "counsel-rg")
(eshell/alias "cal" "calendar")
(eshell/alias "agenda" "org-agenda"))
(defun my/eshell--open-or-cd (path)
"Cd to PATH if path is a directory, otherwise open PATH via `find-file'."
(interactive)
(if (file-directory-p path)
(progn
(goto-char (point-max))
(insert (concat "cd " path))
(eshell-send-input)
(insert (concat "ls -l"))
(eshell-send-input))
(find-file path)))
(defun my/eshell-open-file-at-point ()
"Open the file at point in a buffer."
(interactive)
(let ((filename (symbol-name (symbol-at-point))))
(cond
((file-readable-p filename)
(my/eshell--open-or-cd filename))
((file-readable-p (expand-file-name filename))
(my/eshell--open-or-cd (expand-file-name filename))))))
(defun my/eshell-here ()
"Opens a new shell in the directory associated with the current buffer's file.
The eshell is renamed to match that directory to make multiple
eshell windows easier."
(interactive)
(let* ((parent (if (buffer-file-name)
(file-name-directory (buffer-file-name))
default-directory))
(name (car (last (split-string parent "/" t)))))
(split-window-horizontally)
(other-window 1)
(eshell "new")
(rename-buffer (concat "*eshell: " name "*"))
(insert (concat "ls " "-lh"))
(eshell-send-input)))
(defvar-local my/eshell-output-buffer "*Exported eshell output*"
"Name of buffer with the last output of Eshell command.")
(defvar-local my/eshell-output-delimiter "---"
"Delimiter for successive `prot-eshell-export' outputs.")
(defun my/eshell--command-prompt-output ()
"Capture last command prompt and its output."
(let ((beg (save-excursion
(goto-char (eshell-beginning-of-input))
(goto-char (point-at-bol)))))
(when (derived-mode-p 'eshell-mode)
(buffer-substring-no-properties beg (eshell-end-of-output)))))
;; https://gitlab.com/protesilaos/dotfiles/-/blob/master/emacs/.emacs.d/prot-lisp/prot-eshell.el#L114
(defun my/eshell-export ()
"Produce a buffer with output of the last Eshell command.
If `my/eshell-output-buffer' does not exist, create it. Else
append to it, while separating multiple outputs with
`my/eshell-output-delimiter'."
(interactive)
(let ((eshell-output (my/eshell--command-prompt-output)))
(with-current-buffer (get-buffer-create my/eshell-output-buffer)
(goto-char (point-max))
(unless (eq (point-min) (point-max))
(insert (format "\n%s\n\n" my/eshell-output-delimiter)))
(goto-char (point-at-bol))
(insert eshell-output)
(switch-to-buffer-other-window (current-buffer)))))
;; eshell time and notification
(defvar-local eshell-current-command-start-time nil)
;; https://www.birkey.co/2021-06-20-why-eshell-part-1.html
(defun my/eshell-current-command-start ()
"Capture the time for when the command is started."
(setq eshell-current-command-start-time (current-time)))
(defun my/eshell-current-command-stop ()
"Calculate how long the command took to run."
(when eshell-current-command-start-time
(let ((elapsed-time (float-time
(time-subtract (current-time)
eshell-current-command-start-time))))
(if (> elapsed-time 5)
(eshell-interactive-print
(format "Finished in: %.0fs\n" elapsed-time)))))
(setq eshell-current-command-start-time nil))
(defun my/eshell-current-command-time-track ()
"Track how long command takes to run."
(add-hook 'eshell-pre-command-hook #'my/eshell-current-command-start nil t)
(add-hook 'eshell-post-command-hook #'my/eshell-current-command-stop nil t))
(use-package eshell
:hook ((eshell-mode . my/eshell-mode-setup)
(eshell-mode . my/eshell-current-command-time-track)
(eshell-mode . eshell-smart-initialize))
:commands (eshell eshell-command)
:bind (("C-c e h" . my/eshell-here)
("C-c e e" . my/eshell-export)
("C-c r" . counsel-esh-history)
:map eshell-mode-map
("C-o" . my/eshell-open-file-at-point))
:custom
(eshell-scroll-to-bottom-on-input 'all)
(eshell-error-if-no-glob t)
(eshell-hist-ignoredups t)
(eshell-save-history-on-exit t)
(eshell-cd-on-directory t)
(eshell-prefer-lisp-functions nil)
(eshell-where-to-jump 'begin)
(eshell-review-quick-commands nil)
(eshell-smart-space-goes-to-end t)
(eshell-prompt-function 'my/eshell-prompt)
(tramp-shell-prompt-pattern "^[^$>\n]*[#$%>] *\\(\[[0-9;]*[a-zA-Z] *\\)*")
(eshell-destroy-buffer-when-process-dies t))
(use-package eshell-bookmark
:ensure t
:hook (eshell-mode . eshell-bookmark-setup))
(provide 'my-eshell)
;;; my-eshell.el ends here
|