-
Notifications
You must be signed in to change notification settings - Fork 1
/
flex-compile-repl.el
289 lines (254 loc) · 10.6 KB
/
flex-compile-repl.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
;;; flex-compile-repl.el --- A REPL based compiler -*- lexical-binding: t; -*-
;; Copyright (C) 2015 - 2023 Paul Landes
;; Author: Paul Landes
;; Maintainer: Paul Landes
;; Keywords: compilation integration processes
;; URL: https://github.com/plandes/flex-compile
;; Package-Requires: ((emacs "26.1"))
;; Package-Version: 0
;; 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 2, 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, write to the Free Software
;; Foundation, Inc., 51 Franklin Street, Fifth Floor,
;; Boston, MA 02110-1301, USA.
;;; Commentary:
;; Run, evaluate and compile functionality for a variety of different languages
;; and modes. The specific "compilation" method is different across each
;; add-on library. For example, for ESS and Clojure you can evaluate a
;; specific file and/or evaluate a specfic expression via a REPL. For running
;; a script or starting a `make` an async process is started.
;;
;; For more information see https://github.com/plandes/flex-compile
;;; Code:
(require 'comint)
(require 'eieio)
(require 'flex-compile-config)
(defclass repl-flex-compiler (single-buffer-flex-compiler
conf-file-flex-compiler)
((repl-buffer-regexp :initarg :repl-buffer-regexp
:type string
:documentation "\
Regular expression to match buffers for functions like killing the session.")
(derived-buffer-names :initarg :derived-buffer-names
:initform nil
:type list
:documentation "\
List of buffers for functions (like killing a buffer) when session ends.")
(repl-buffer-start-wait :initarg :repl-buffer-start-wait
:initform 0
:type number
:documentation "\
Number of seconds (as a float) to wait before issuing any first command to the
REPL.")
(repl-buffer-start-timeout :initarg :repl-buffer-start-timeout
:initform 1
:type integer
:documentation "\
Number of seconds as an integer to wait to start before giving up (and not
displaying). If this is 0, don't wait or display the buffer when it comes
up.")
(prompt-kill-repl-buffer :initarg :prompt-kill-repl-buffer
:initform t
:type boolean
:documentation "\
If non-`nil' then prompt to kill a REPL buffer on clean.")
(output-clear :initarg :output-clear
:initform nil
:type boolean
:documentation "\
Whether or not to clear comint buffer after a compilation.")
(form-history :initarg :form-history
:initform (gensym "config-repl-form-history")
:type symbol
:documentation "\
The history variable for the eval form history."))
:method-invocation-order :c3
:documentation "Compiles by evaluating expressions in the REPL.")
(cl-defmethod initialize-instance ((this repl-flex-compiler) &optional slots)
"Initialize instance THIS with arguments SLOTS."
(let ((props
(list
(config-boolean-prop :object-name 'output-clear
:prop-entry this
:prompt "Clear output on compile"
:input-type 'toggle)
(config-boolean-prop :object-name 'prompt-kill-repl-buffer
:prop-entry this
:prompt "Confirm REPL buffer kills"
:input-type 'toggle)
(config-number-prop :object-name 'repl-buffer-start-timeout
:prop-entry this
:prompt "Seconds to wait for REPL to start")
(config-number-prop :object-name 'repl-buffer-start-wait
:prop-entry this
:number-type 'float
:prompt "Seconds to wait before starting the REPL"))))
(setq slots (plist-put slots
:props (append (plist-get slots :props) props))))
(cl-call-next-method this slots))
(cl-defmethod flex-compiler-repl-start ((this repl-flex-compiler))
"Start the REPL using THIS compiler."
(config-persistent--unimplemented this "start-repl"))
(cl-defmethod flex-compiler-repl-compile ((this repl-flex-compiler) file)
"Invoked by `compile' type messages from THIS compiler.
FILE gets evaluated by the compiler either as a IPC communication or by direct
insertion in the REPL buffer.
This method is meant to allow for REPL compiles \(really some kind of
evaluation), while allowing base class compilation features.."
(ignore this file)
(config-persistent--unimplemented this "repl-compile"))
(cl-defmethod flex-compiler-wait-for-buffer ((this repl-flex-compiler))
"Wait for the compilation to start using THIS compiler.
The caller raises and error if it doesn't start in time."
(with-slots (repl-buffer-start-timeout) this
(let ((count-down repl-buffer-start-timeout)
buf)
(cl-block wfb
(dotimes (i count-down)
(setq buf (flex-compiler-buffer this))
(if buf
(cl-return-from wfb buf)
(message "Waiting for buffer to start... (%d)"
(- count-down i))
(sit-for 1)))))))
(cl-defmethod flex-compiler-repl-running-p ((this repl-flex-compiler))
"Return whether or not THIS REPL is currently running."
(not (null (flex-compiler-buffer this))))
(cl-defmethod flex-compiler-repl-assert-running ((this repl-flex-compiler))
"Raise an error if THIS REPL *is* running."
(unless (flex-compiler-repl-running-p this)
(error "The REPL for %s isn't started" (config-entry-name this))))
(cl-defmethod flex-compiler-repl-assert-not-running ((this repl-flex-compiler))
"Raise an error if THIS REPL isn't running."
(if (flex-compiler-repl-running-p this)
(error "Compiler %s is already running"
(config-entry-name this))))
(cl-defmethod flex-compiler-repl--run-start ((this repl-flex-compiler))
"Start THIS compiler's REPL if it isn't already."
(with-slots (repl-buffer-start-timeout
repl-buffer-start-wait
start-directory) this
(let ((timeout repl-buffer-start-timeout)
buf)
(unless (flex-compiler-repl-running-p this)
(config-prop-entry-set-required this)
(let ((default-directory (or start-directory default-directory)))
(flex-compiler-repl-start this))
(when (> timeout 0)
(setq buf (flex-compiler-wait-for-buffer this))
(unless buf
(error "Couldn't create REPL for compiler %s"
(config-entry-name this))))
(when (> repl-buffer-start-wait 0)
(sit-for repl-buffer-start-wait))))))
(cl-defmethod flex-compiler-send-input ((this repl-flex-compiler)
&optional command)
"Send a COMMAND \(input) to THIS compiler's REPL."
(ignore this)
(goto-char (point-max))
(insert command)
(comint-send-input))
(cl-defmethod flex-compiler-run-command ((this repl-flex-compiler)
&optional command)
"Send COMMAND to THIS compiler's REPL to evaluate or start a process."
(flex-compiler-repl-assert-running this)
(let ((buf (flex-compiler-buffer this)))
(with-current-buffer buf
(if command
(flex-compiler-send-input this command)))))
(cl-defmethod flex-compiler-buffer ((this repl-flex-compiler))
"Find THIS compiler's first REPL buffer found in the buffer list."
(with-slots (repl-buffer-regexp) this
(cl-block found-buf
(dolist (buf (buffer-list))
(let ((buf-name (buffer-name buf)))
(when (string-match repl-buffer-regexp buf-name)
(cl-return-from found-buf buf)))))))
(cl-defmethod flex-compiler--kill-buffer ((this repl-flex-compiler) buffer)
"Kill THIS compiler's BUFFER.
It kills the buffer without prompting However, in some cases, it might raise
an erorr."
(ignore this)
(with-slots (prompt-kill-repl-buffer) this
(when (buffer-live-p buffer)
(let ((kill-buffer-query-functions
(if prompt-kill-repl-buffer
kill-buffer-query-functions
nil)))
(kill-buffer buffer)))))
(cl-defmethod flex-compiler-kill-repl ((this repl-flex-compiler))
"Kill THIS compiler's REPL."
(with-slots (derived-buffer-names prompt-kill-repl-buffer) this
(let ((bufs (append (mapcar 'get-buffer derived-buffer-names)
(cons (flex-compiler-buffer this) nil)))
(count 0))
(dolist (buf bufs)
(when (and buf (buffer-live-p buf))
(flex-compiler--kill-buffer this buf)
(cl-incf count)))
(message "%s killed %d buffer(s)"
(capitalize (config-entry-name this)) count))))
(cl-defmethod flex-compiler-clear-buffer ((this repl-flex-compiler))
"Clear THIS compiler's REPL buffer."
(with-current-buffer (flex-compiler-buffer this)
(comint-clear-buffer)))
(cl-defmethod flex-compiler-start-buffer ((this repl-flex-compiler) start-type)
"Return a new buffer for THIS compiler with a processing compilation.
START-TYPE is either symbols `compile', `run', `clean' depending
if invoked by `flex-compiler-compile' or `flex-compiler-run'."
(config-prop-entry-set-required this)
(with-slots (config-file output-clear) this
(let ((runningp (flex-compiler-repl-running-p this)))
(cl-case start-type
(compile (progn
(unless runningp
(flex-compiler-run this))
(if (flex-compiler-repl-running-p this)
(progn
(when output-clear
(flex-compiler-clear-buffer this))
(flex-compiler-repl-compile this config-file))
(if runningp
(error "REPL hasn't started")
(message "REPL still starting, please wait")))
(flex-compiler-buffer this)))
(run (progn
(flex-compiler-repl--run-start this)
(flex-compiler-buffer this)))
(clean (progn
(flex-compiler-kill-repl this)
'killed-buffer))))))
(cl-defmethod flex-compiler-eval-initial-at-point ((this repl-flex-compiler))
"Return the expression at or right before the point for THIS compiler."
(ignore this))
(cl-defmethod flex-compiler-eval-form-impl ((this repl-flex-compiler) form)
"Evaluate the FORM and return the response of the REPL for THIS compiler."
(ignore this form)
(config-persistent--unimplemented this "eval-form-impl"))
(cl-defmethod flex-compiler-query-read-form ((this repl-flex-compiler)
no-input-p)
"Read a form, meaningful for THIS compiler from the user.
NO-INPUT-P, if non-nil, use the symbolic expression at the current point."
(with-slots (form-history) this
(let ((init (flex-compiler-eval-initial-at-point this)))
(if no-input-p
init
(read-string "Form: " init form-history)))))
(cl-defmethod flex-compiler-evaluate-form ((this repl-flex-compiler)
&optional form)
"Return the evaluation of FORM for THIS compiler.
See the `:eval-form' slot."
(let ((res (flex-compiler-eval-form-impl this form)))
(if (stringp res)
res
(prin1-to-string res))))
(provide 'flex-compile-repl)
;;; flex-compile-repl.el ends here