forked from Sasanidas/Apprentice
-
Notifications
You must be signed in to change notification settings - Fork 0
/
apprentice-iex.el
303 lines (247 loc) · 10.9 KB
/
apprentice-iex.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
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
;;; apprentice-help.el --- Interaction with an Elixir IEx process
;; Copyright © 2014-2017 Samuel Tonini
;; Copyright © 2022 Fermin MF
;; Author: Samuel Tonini <tonini.samuel@gmail.com
;; Maintainer: Fermin MF <fmfs@posteo.net>
;; This file is not part of GNU Emacs.
;; This program is free software: you can redistribute it and/or modify
;; it under the terms of the GNU General Public License as published by
;; the Free Software Foundation, either version 3 of the License, or
;; (at your option) any later version.
;; This program is distributed in the hope that it will be useful,
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
;; GNU General Public License for more details.
;; You should have received a copy of the GNU General Public License
;; along with this program. If not, see <http://www.gnu.org/licenses/>.
;;; Commentary:
;; Interaction with an Elixir IEx process
;;; Code:
(require 'comint)
(require 'cl-lib)
(require 'pcase)
(require 'apprentice-scope)
(require 'apprentice-project)
(defgroup apprentice-iex nil
"Interaction with an Elixir IEx process."
:prefix "apprentice-iex-"
:group 'apprentice)
(defcustom apprentice-iex-type :comint-mode
"Type of buffer to use for IEx.
This variable can take 2 values:
- :comint-mode for `comint-mode' (default)
- :vterm-mode to use `vterm-mode'(apprentice version)"
:type 'keyword
:group 'apprentice-iex)
(defcustom apprentice-iex-program-name "iex"
"The shell command for iex."
:type 'string
:group 'apprentice-iex)
(defvar apprentice-iex-prompt-regexp "^\\(iex\\|\\.\\.\\.\\)(.+)>"
"Prompt regex pattern of IEx interpreter.
Should match prompts that looks like these:
iex(1)>
...(1)>")
(defcustom apprentice-iex-prompt-read-only t
"If non-nil, the prompt will be read-only."
:type 'boolean
:group 'apprentice-iex)
(defvar apprentice-iex-buffer nil
"The buffer in which the Elixir IEx process is running.")
(defvar apprentice-iex-comint-mode-hook nil
"Hook for customizing `apprentice-iex-comint-mode'.")
(defvar apprentice-iex-vterm-mode-hook nil
"Hook for customizing `apprentice-iex-vterm-mode'.")
(defvar apprentice-iex-vterm-input-ring nil
"Apprentice custom input-ring.")
;;TODO: Adapt this keymap to vterm version
(defvar apprentice-iex-mode-map
(let ((map (nconc (make-sparse-keymap) comint-mode-map)))
;; (define-key map "\t" 'company-complete)
;; (define-key map (kbd "TAB") 'company-complete)
;; (define-key map (kbd (format "%s i r" apprentice-key-command-prefix)) 'apprentice-iex-open-input-ring)
;; (define-key map (kbd (format "%s i c" apprentice-key-command-prefix)) 'apprentice-iex-clear-buffer)
;;(define-key map (kbd (format "%s h e" apprentice-key-command-prefix)) 'apprentice-help-search-at-point)
;;(define-key map (kbd "M-.") 'apprentice-goto-definition-at-point)
map))
(define-derived-mode apprentice-iex-comint-mode comint-mode "Apprentice-IEx"
"Major mode for interacting with an Elixir IEx process.
It uses `comint-mode' for REPL interaction.
\\<apprentice-iex-mode-map>"
nil "Apprentice-IEx"
(set (make-local-variable 'comint-prompt-regexp) apprentice-iex-prompt-regexp)
(set (make-local-variable 'comint-prompt-read-only) apprentice-iex-prompt-read-only)
(set (make-local-variable 'comint-input-autoexpand) nil)
(set (make-local-variable 'comint-input-sender) 'apprentice-iex--send-command)
(add-hook 'comint-output-filter-functions 'apprentice-iex-spot-prompt nil t))
(cl-defun apprentice-iex-vterm-insert-input ()
"Advice to `vterm-send-return'.
It inserts `apprentice-iex--vterm-cstring' into
`apprentice-iex-vterm-input-ring'."
(when (equal major-mode 'apprentice-iex-vterm-mode)
(with-current-buffer (current-buffer)
(save-excursion
(beginning-of-line)
(when (re-search-forward apprentice-iex-prompt-regexp nil t)
(let ((be (point))
(en (progn (end-of-line) (point))))
(ring-insert apprentice-iex-vterm-input-ring
(string-trim
(buffer-substring-no-properties be en)))))))))
(define-derived-mode apprentice-iex-vterm-mode vterm-mode "Apprentice-IEx"
"Major mode for interacting with an Elixir IEx process.
It uses `vterm-mode' for REPL interaction.
\\<apprentice-iex-mode-map>"
nil "Apprentice-IEx"
(set (make-local-variable 'apprentice-iex-vterm-input-ring) (make-ring 500))
(set (make-local-variable 'apprentice-iex--vterm-cstring) nil)
(advice-add 'vterm-send-return :before 'apprentice-iex-vterm-insert-input))
(defun apprentice-iex-command (arg)
(split-string-and-unquote
(if (null arg) apprentice-iex-program-name
(read-string "Command to run Elixir IEx: " (concat apprentice-iex-program-name arg)))))
(cl-defmethod apprentice-iex--start-process (command (type (eql :comint-mode)))
(setq apprentice-iex-buffer
(apply 'make-comint "Apprentice-IEx" (car command) nil (cdr command)))
(with-current-buffer apprentice-iex-buffer
(apprentice-iex-comint-mode)
(run-hooks 'apprentice-iex-comint-mode-hook)))
(cl-defmethod apprentice-iex--start-process (command (type (eql :vterm-mode)))
(setq apprentice-iex-buffer
(let ((buffer (generate-new-buffer "*Apprentice-IEx*")))
(with-current-buffer buffer
(apprentice-iex-vterm-mode))
(pop-to-buffer buffer)))
(with-current-buffer apprentice-iex-buffer
(vterm-send-string (mapconcat 'identity (append command '("&&" "exit")) " "))
(vterm-send-return)
(run-hooks 'apprentice-iex-vterm-mode-hook)))
(defun apprentice-iex-start-process (command)
"Start an IEX process.
With universal prefix \\[universal-argument], prompts for a COMMAND,
otherwise uses `apprentice-iex-program-name'.
It runs the hook `apprentice-iex-mode-hook' after starting the process and
setting up the IEx buffer."
(interactive (list (apprentice-iex-command current-prefix-arg)))
(apprentice-iex--start-process command apprentice-iex-type))
(defun apprentice-iex-process (&optional arg)
(or (if (buffer-live-p apprentice-iex-buffer)
(get-buffer-process apprentice-iex-buffer))
(progn
(let ((current-prefix-arg arg))
(call-interactively 'apprentice-iex-start-process))
(apprentice-iex-process arg))))
(defun apprentice-iex--remove-newlines (string)
(replace-regexp-in-string "\n" " " string))
(defun apprentice-iex-send-last-sexp ()
"Send the previous sexp to the inferior IEx process."
(interactive)
(apprentice-iex-send-region (save-excursion (backward-sexp) (point)) (point)))
(defun apprentice-iex-send-current-line ()
"Sends the current line to the IEx process."
(interactive)
(let ((str (thing-at-point 'line)))
(apprentice-iex--send-command (apprentice-iex-process) str)))
(defun apprentice-iex-send-current-line-and-go ()
"Sends the current line to the inferior IEx process.
It also jump to the buffer."
(interactive)
(call-interactively 'apprentice-iex-send-current-line)
(pop-to-buffer (process-buffer (apprentice-iex-process))))
(defun apprentice-iex-send-region-and-go ()
"Sends the marked region to the inferior IEx process.
It also jump to the buffer."
(interactive)
(call-interactively 'apprentice-iex-send-region)
(pop-to-buffer (process-buffer (apprentice-iex-process))))
(defun apprentice-iex-send-region (beg end)
"Sends the marked region(BEG END) to the IEx process."
(interactive (list (point) (mark)))
(unless (and beg end)
(error "The mark is not set now, so there is no region"))
(let* ((region (buffer-substring-no-properties beg end)))
(apprentice-iex--send-command (apprentice-iex-process) region)))
(defun apprentice-iex-compile-this-buffer ()
"Compiles the current buffer in the IEx process."
(interactive)
(let* ((path (if (apprentice-project-p)
(format "%s/_build/dev/" (apprentice-project-root))
"."))
(str (format "c(\"%s\", \"%s\")" (buffer-file-name) path)))
(apprentice-iex--send-command (apprentice-iex-process) str)))
(defun apprentice-iex-compile-this-buffer-and-go ()
"Compiles the current buffer in the IEx process and jump to the buffer."
(interactive)
(apprentice-iex-compile-this-buffer)
(pop-to-buffer (process-buffer (apprentice-iex-process))))
(defun apprentice-iex-reload-module ()
"Recompile and reloads the current module in the IEx process."
(interactive)
(let ((str (format "r(%s)" (apprentice-scope-module))))
(apprentice-iex--send-command (apprentice-iex-process) str)))
(cl-defmethod apprentice-iex--command (proc (lines list) (type (eql :comint-mode)))
(with-current-buffer (process-buffer proc)
(cl-loop for line in lines
do (progn
(goto-char (process-mark proc))
(insert-before-markers (concat line "\n"))
(move-marker comint-last-input-end (point))
(comint-send-string proc (concat line "\n"))))))
(cl-defmethod apprentice-iex--command (proc (lines list) (type (eql :vterm-mode)))
(with-current-buffer (process-buffer proc)
(vterm-send-string
(mapconcat 'identity lines " "))
(vterm-send-return)))
(defun apprentice-iex--send-command (proc str)
(let ((lines (split-string str "\n" nil)))
(apprentice-iex--command proc lines apprentice-iex-type)))
(defun apprentice-iex-spot-prompt (_string)
(let ((proc (get-buffer-process (current-buffer))))
(when proc
(save-excursion
(goto-char (process-mark proc))))))
(cl-defmethod apprentice-iex--clear-buffer ((type (eql :comint-mode)))
(with-current-buffer (process-buffer
(apprentice-iex-process))
(let ((comint-buffer-maximum-size 0))
(comint-truncate-buffer))))
(cl-defmethod apprentice-iex--clear-buffer ((type (eql :vterm-mode)))
(with-current-buffer (process-buffer
(apprentice-iex-process))
(vterm-clear)))
(defun apprentice-iex-clear-buffer ()
"Clear the current iex process buffer."
(interactive)
(apprentice-iex--clear-buffer apprentice-iex-type))
(defun apprentice-iex-open-input-ring ()
"Open the buffer containing the input history."
(interactive)
(progn
(comint-dynamic-list-input-ring)
(other-window 1)))
;;;###autoload
(defalias 'run-elixir 'apprentice-iex-run)
(defalias 'inferior-elixir 'apprentice-iex-run)
;;;###autoload
(defun apprentice-iex-run (&optional arg)
"Start an IEx process with ARG.
Show the IEx buffer if an IEx process is already run."
(interactive "P")
(when (eq :vterm-mode apprentice-iex-type)
(require 'vterm))
(let ((proc (apprentice-iex-process arg)))
(pop-to-buffer (process-buffer proc))))
;;;###autoload
(defun apprentice-iex-project-run ()
"Start an IEx process with mix 'iex -S mix'.
in the context of an Elixir project.Show the IEx buffer if an
IEx process is already run."
(interactive)
(when (eq :vterm-mode apprentice-iex-type)
(require 'vterm))
(if (apprentice-project-p)
(let ((default-directory (apprentice-project-root)))
(pop-to-buffer (process-buffer (apprentice-iex-process " -S mix"))))
(message "No mix.exs file available. Please use `apprentice-iex-run' instead.")))
(provide 'apprentice-iex)
;;; apprentice-iex.el ends here