blob: bf9a0b4bd876189e9437b27108ebb006bd047023 (
plain) (
blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
|
;;; my-eshell.el --- Configure eshell -*- lexical-binding: t -*-
;;; Commentary:
;;; Code:
(require 'cl-seq)
(require 'vc)
(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" "vc-diff")
(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))
(defun my/short-pwd (path)
"Turn a PATH of the form /foo/bar/baz into /f/b/baz, like fish shell."
(let* ((home-path (replace-regexp-in-string (expand-file-name "~")
"~"
path))
(current-dir (split-string home-path "/"))
(cdir (last current-dir))
(head (butlast current-dir)))
(concat (mapconcat (lambda (s)
(if (string= "" s) nil
(substring s 0 1)))
head
"/")
(if head "/" nil)
(car cdir))))
(defmacro with-face (str &rest properties)
"Set the PROPERTIES for the given STR."
`(propertize ,str 'face (list ,@properties)))
(defun my/prompt ()
"EShell prompt displaying VC info and such."
(concat
(with-face (concat (my/short-pwd (eshell/pwd)) " ") :foreground "#070707")
(if (= 0 (user-uid))
(with-face "#" :foreground "#f43841")
(with-face "λ" :foreground "#63c904"))
(with-face " " :foreground "#000000")))
(global-set-key (kbd "C-c e h") 'my/eshell-here)
(define-key eshell-mode-map (kbd "C-c e e") 'my/eshell-export)
(define-key eshell-mode-map (kbd "C-o") 'my/eshell-open-file-at-point)
(customize-set-variable 'eshell-scroll-to-bottom-on-input 'all)
(customize-set-variable 'eshell-error-if-no-glob t)
(customize-set-variable 'eshell-hist-ignoredups t)
(customize-set-variable 'eshell-save-history-on-exit t)
(customize-set-variable 'eshell-cd-on-directory t)
(customize-set-variable 'eshell-prefer-lisp-functions nil)
(customize-set-variable 'eshell-prompt-function 'my/prompt)
(customize-set-variable 'eshell-highlight-prompt nil)
(customize-set-variable 'eshell-where-to-jump 'begin)
(customize-set-variable 'eshell-review-quick-commands nil)
(customize-set-variable 'eshell-smart-space-goes-to-end t)
(customize-set-variable 'tramp-shell-prompt-pattern "^[^$>\n]*[#$%>] *\\(customize-set-variable '\[[0-9;]*[a-zA-Z] *\\)*")
(customize-set-variable 'eshell-destroy-buffer-when-process-dies t)
(add-hook 'eshell-mode-hook 'my/eshell-mode-setup)
(add-hook 'eshell-mode-hook 'my/eshell-current-command-time-track)
(add-hook 'eshell-mode-hook 'eshell-smart-initialize)
(add-hook 'eshell-mode-hook 'eshell-bookmark-setup)
(provide 'my-eshell)
;;; my-eshell.el ends here
|