-
Notifications
You must be signed in to change notification settings - Fork 3
/
idee-eshell.el
226 lines (189 loc) · 8.9 KB
/
idee-eshell.el
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
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
;; idee-eshell.el --- Eshell integration -*- lexical-binding: t -*-
;; Copyright (C) 2018 Ioannis Canellos
;; Licensed under the Apache License, Version 2.0 (the "License");
;; you may not use this file except in compliance with the License.
;; You may obtain a copy of the License at
;; http://www.apache.org/licenses/LICENSE-2.0
;;Unless required by applicable law or agreed to in writing, software
;;distributed under the License is distributed on an "AS IS" BASIS,
;;WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
;;See the License for the specific language governing permissions and
;;limitations under the License.
;; Author: Ioannis Canellos
;;; Commentary:
;;; Code:
(require 'queue)
(require 'eshell)
(require 'esh-mode)
(require 'em-alias)
(require 'em-hist)
(require 'projectile)
(defvar idee/eshell-command-queue (queue-create))
(defvar idee/eshell-command-inserting nil)
(defvar idee/eshell-command-running nil)
(defvar idee/eshell-initialized nil)
(defcustom idee/eshell-cat-alias-enabled t "/dev/clip aware cat alias toggle." :group 'idee/eshell :type 'boolean)
(defcustom idee/eshell-edit-alias-enabled t "Edit alias toggle." :group 'idee/eshell :type 'boolean)
(defcustom idee/eshell-save-on-shell-enabled t "Save on shell toggle." :group 'idee/eshell :type 'boolean)
(defun idee/eshell-cleanup ()
"Cleanup eshell queues and flags."
(interactive)
(setq idee/eshell-command-queue (queue-create))
(setq idee/eshell-command-running nil))
(defun idee/eshell-command-started ()
"Mark that an eshell command is running."
(setq idee/eshell-command-running t))
(defun idee/eshell-command-finished ()
"Mark that an eshell command is running."
;; The first time this function is called will be before the shell is even intialized.
(if (not idee/eshell-initialized)
(setq idee/eshell-initialized t)
(run-with-timer 1 nil (lambda ()
(progn
(when (and (not idee/eshell-command-inserting) idee/eshell-command-running)
(idee/eshell-execute-next-command))
(setq idee/eshell-command-running (and idee/eshell-command-running (not (queue-empty idee/eshell-command-queue)))))))))
(defun idee/eshell-execute-next-command ()
"Execute the next command found in the queue."
(interactive)
(setq idee/eshell-command-running t)
(let* ((cmd (queue-dequeue idee/eshell-command-queue))
(should-ignore (equal 'ignore cmd)))
;; dequeue one more if you have two.
(if should-ignore
(progn
(setq cmd (queue-dequeue idee/eshell-command-queue))
(setq should-ignore (equal 'ignore cmd))))
(when (not should-ignore)
(when cmd (idee/shell-command-execute-in-project cmd)))
should-ignore))
;;;###autoload (autoload 'idee/eshell-in-project "idee-eshell")
(defmacro idee/eshell-in-project (&rest body)
"Load a SETTINGS-FILE as local OPTIONS and evaluate BODY."
(declare (indent 1) (debug t))
`(let ()
(when idee/eshell-save-on-shell-enabled (idee/save-all))
(idee/switch-cli-on)
(with-current-buffer (format "*eshell %s*" (projectile-project-name))
(let ((comint-scroll-to-bottom-on-output t))
(eshell-send-input)
(eshell-return-to-prompt)
,@body
(eshell-send-input)))))
;;;###autoload
(defun idee/eshell-command-execute-in-project (command &optional new-shell)
"Run a single COMMAND in the current project shell.
When NEW-SHELL is specified the old eshell project buffer is killed."
(let* ((name (format "*eshell %s*" (projectile-project-name)))
(buf (get-buffer name))
(eshell-scroll-to-bottom-on-input t)
(comint-scroll-to-bottom-on-output t))
(when (and buf new-shell) (kill-buffer buf))
(idee/switch-cli-on)
(with-current-buffer name
(eshell-return-to-prompt)
(eshell-wait-for-process)
(idee/eshell-insert command)
(eshell-send-input))))
;;;###autoload
(defun idee/eshell-command-enqueue-in-project (commands)
"Execute COMMANDS on eshell."
(idee/switch-cli-on)
(with-current-buffer (format "*eshell %s*" (projectile-project-name))
(let ((comint-scroll-to-bottom-on-output t)
(eshell-scroll-to-bottom-on-input t))
(dolist (cmd (if (listp commands) commands (list commands)))
(when (not (idee/string-blank cmd)) (queue-enqueue idee/eshell-command-queue cmd)))
(when (not idee/eshell-command-running) (idee/eshell-execute-next-command)))))
;;;###autoload
(defun idee/eshell-insert (str)
"Insert STR into the current project eshell buffer."
;; Sometimes eshell decided to insert the last command when trying to insert the new one.
;; Not, sure exactly why this happens, but let's kill the line if it does.
;; This also helps, if left over chars or half written commands are there.
(setq idee/eshell-command-inserting t)
(when (< (point) (point-max)) (kill-line))
(insert str)
(setq idee/eshell-command-inserting nil))
(defun idee/eshell-visible-window ()
"Return the visible eshell window."
(car (idee/windows-visible-get (lambda (b) (string-prefix-p "*eshell" (buffer-name b))))))
;;;###autoload
(defun idee/eshell-open-in-project ()
"Invoke `eshell' in the project's root.
Switch to the project specific eshell buffer if it already exists."
(interactive)
(projectile-with-default-dir (projectile-ensure-project (projectile-project-root))
(let* ((eshell-buffer-name (format "*eshell %s*" (projectile-project-name)))
(buf (get-buffer eshell-buffer-name)))
(when (and (not (idee/cli-visible-p)) (not (string-prefix-p "*eshell " (buffer-name))))
(cond (buf (switch-to-buffer buf))
((fboundp '+eshell/here) (+eshell/here)) ;; If running inside doom use +eshell/here.
(:else (eshell)))
;; In some cases just setting eshell-buffer-name doesn't cut it
(when (not (equal (buffer-file-name) eshell-buffer-name)) (rename-buffer eshell-buffer-name))))))
;;
;; Aliases
;;
(defun idee/eshell-cat (f)
"Display the contents of file F."
(if (equal f "/dev/clip")
(current-kill 0)
(idee/read-file f)))
(advice-add 'eshell-command-started :before 'idee/eshell-command-started)
;(advice-add 'eshell-command-finished :after 'idee/eshell-command-finished)
(add-hook 'eshell-after-prompt-hook 'idee/eshell-command-finished)
(add-hook 'eshell-after-prompt-hook 'idee/eshell-command-finished)
(when idee/eshell-cat-alias-enabled
(add-hook 'eshell-mode-hook (lambda () (eshell/alias "cat" "idee/eshell-cat $1"))))
(defun idee/eshell-edit (&rest files)
"Edit the the specified FILES."
(let ((file (car files))
(remaining (cdr files)))
(idee/jump-to-non-idee/window)
(find-file file)
(mapc (lambda (f) (split-window-horizontally) (find-file f)) remaining))
;; We don't want to return anything back, so let's just return nil.
nil)
(when idee/eshell-edit-alias-enabled
(add-hook 'eshell-mode-hook (lambda () (eshell/alias "edit" "idee/eshell-edit $*"))))
(defun idee/eshell-open (file)
"Edit the the specified FILE."
(let* ((is-directory (file-directory-p file))
(path (expand-file-name file))
(git (concat (file-name-as-directory path) ".git")))
(if is-directory
(progn
(setq default-directory path)
(when (not (file-exists-p git)) (shell-command "git init"))
(projectile-add-known-project path)
(setq projectile-project-root path)
(projectile-switch-project-by-name path))
(idee/eshell-edit path))))
(when idee/eshell-edit-alias-enabled
(add-hook 'eshell-mode-hook (lambda () (eshell/alias "open" "idee/eshell-open $1"))))
(defun idee/eshell-find-web-port ()
"Find the web port in the current buffer, or return 8080 if none is found."
(let* ((name (format "*eshell %s*" (projectile-project-name)))
(buffer (get-buffer name)))
(if buffer
(with-current-buffer buffer
(save-excursion
(goto-char (point-max))
(if (re-search-backward "port.* \\([0-9]+\\)" nil t)
(match-string 1)
8080)))
8080)))
(defun idee/eshell-enable ()
"Enable eshell."
(interactive)
;;Clear existing associations
(setq idee/function-alist (delq (assoc 'idee/shell-command-execute-in-project-function idee/function-alist) idee/function-alist))
(setq idee/function-alist (delq (assoc 'idee/shell-visible-window-function idee/function-alist) idee/function-alist))
(setq idee/function-alist (delq (assoc 'idee/shell-open-in-project-function idee/function-alist) idee/function-alist))
;; Register eshell functions
(add-to-list 'idee/function-alist '(idee/shell-command-execute-in-project-function . idee/eshell-command-execute-in-project))
(add-to-list 'idee/function-alist '(idee/shell-visible-window-function . idee/eshell-visible-window))
(add-to-list 'idee/function-alist '(idee/shell-open-in-project-function . idee/eshell-open-in-project)))
(provide 'idee-eshell)
;; idee-eshell.el ends here