blob: b7d312e1a2bae3976eb8c540d90ed6613f105357 (
plain)
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
|
;;; geiser-connection.el -- talking to a scheme process -*- lexical-binding: t; -*-
;; Copyright (C) 2009-2011, 2013, 2021-2022 Jose Antonio Ortega Ruiz
;; This program is free software; you can redistribute it and/or
;; modify it under the terms of the Modified BSD License. You should
;; have received a copy of the license along with this program. If
;; not, see <http://www.xfree86.org/3.3.6/COPYRIGHT2.html#5>.
;; Start date: Sat Feb 07, 2009 21:11
;;; Commentary:
;; Connection datatype and functions for managing request queues
;; between emacs and inferior guile processes.
;;; Code:
(require 'geiser-log)
(require 'geiser-syntax)
(require 'geiser-base)
(require 'geiser-impl)
(require 'tq)
(eval-when-compile (require 'subr-x))
;;; Buffer connections:
(defvar-local geiser-con--connection nil)
(defun geiser-con--get-connection (buffer/proc)
(if (processp buffer/proc)
(geiser-con--get-connection (process-buffer buffer/proc))
(with-current-buffer buffer/proc geiser-con--connection)))
;;; Request datatype:
(defun geiser-con--make-request (con str cont &optional sender-buffer)
(list (cons :id (geiser-con--connection-inc-count con))
(cons :string str)
(cons :continuation cont)
(cons :buffer (or sender-buffer (current-buffer)))
(cons :connection con)))
(defsubst geiser-con--request-id (req)
(cdr (assq :id req)))
(defsubst geiser-con--request-string (req)
(cdr (assq :string req)))
(defsubst geiser-con--request-continuation (req)
(cdr (assq :continuation req)))
(defsubst geiser-con--request-buffer (req)
(cdr (assq :buffer req)))
(defsubst geiser-con--request-connection (req)
(cdr (assq :connection req)))
(defsubst geiser-con--request-deactivate (req)
(setcdr (assq :continuation req) nil))
(defsubst geiser-con--request-deactivated-p (req)
(null (cdr (assq :continuation req))))
;;; Connection datatype:
(defun geiser-con--tq-create (process)
(let ((tq (tq-create process)))
(set-process-filter process (lambda (_p s) (geiser-con--tq-filter tq s)))
tq))
(defun geiser-con--tq-filter (tq in)
(when (buffer-live-p (tq-buffer tq))
(with-current-buffer (tq-buffer tq)
(if (tq-queue-empty tq)
(progn (geiser-log--error "Unexpected queue input:\n %s" in)
(delete-region (point-min) (point-max)))
(goto-char (point-max))
(insert in)
(goto-char (point-min))
(when (re-search-forward (tq-queue-head-regexp tq) nil t)
(unwind-protect
(funcall (tq-queue-head-fn tq)
(tq-queue-head-closure tq)
(buffer-substring (point-min) (point)))
(delete-region (point-min) (point-max))
(tq-queue-pop tq)))))))
(defun geiser-con--combined-prompt (prompt debug)
(if debug (format "\\(%s\\)\\|\\(%s\\)" prompt debug) prompt))
(defun geiser-con--connection-eot-re (prompt debug)
(geiser-con--combined-prompt (format "\n\\(%s\\)" prompt)
(and debug (format "\n\\(%s\\)" debug))))
(defun geiser-con--make-connection (proc prompt debug-prompt)
(list t
(cons :filter (process-filter proc))
(cons :tq (geiser-con--tq-create proc))
(cons :tq-filter (process-filter proc))
(cons :eot (geiser-con--connection-eot-re prompt debug-prompt))
(cons :prompt prompt)
(cons :debug-prompt debug-prompt)
(cons :is-debugging nil)
(cons :count 0)
(cons :completed (make-hash-table :weakness 'value))))
(defsubst geiser-con--connection-process (c)
(tq-process (cdr (assq :tq c))))
(defsubst geiser-con--connection-filter (c)
(cdr (assq :filter c)))
(defsubst geiser-con--connection-tq-filter (c)
(cdr (assq :tq-filter c)))
(defsubst geiser-con--connection-tq (c)
(cdr (assq :tq c)))
(defsubst geiser-con--connection-eot (c)
(cdr (assq :eot c)))
(defsubst geiser-con--connection-prompt (c)
(cdr (assq :prompt c)))
(defsubst geiser-con--connection-debug-prompt (c)
(cdr (assq :debug-prompt c)))
(defsubst geiser-con--connection-is-debugging (c)
(cdr (assq :is-debugging c)))
(defsubst geiser-con--connection-set-debugging (c d)
(setcdr (assq :is-debugging c) d))
(defun geiser-con--connection-update-debugging (c txt)
(let* ((dp (geiser-con--connection-debug-prompt c))
(is-d (and (stringp dp) (string-match dp txt))))
(geiser-con--connection-set-debugging c is-d)
is-d))
(defsubst geiser-con--connection-completed (c r)
(geiser-con--request-deactivate r)
(puthash (geiser-con--request-id r) r (cdr (assoc :completed c))))
(defsubst geiser-con--connection-completed-p (c id)
(gethash id (cdr (assoc :completed c))))
(defun geiser-con--connection-inc-count (c)
(let* ((cnt (assoc :count c))
(new (1+ (cdr cnt))))
(setcdr cnt new)
new))
(defun geiser-con--has-entered-debugger (con answer)
(when-let ((p (car (last (split-string answer "\n" t)))))
(geiser-con--connection-update-debugging con p))
(geiser-con--connection-is-debugging con))
(defun geiser-con--connection-eot-p (con txt)
(and txt
(string-match-p (geiser-con--connection-eot con) txt)))
(defun geiser-con--connection-close (con)
(let ((tq (geiser-con--connection-tq con)))
(and tq (tq-close tq))))
(defvar geiser-con--startup-prompt nil)
(defun geiser-con--startup-prompt (_p s)
(setq geiser-con--startup-prompt
(concat geiser-con--startup-prompt s))
nil)
(defun geiser-con--connection-deactivate (c &optional no-wait)
(when (car c)
(let* ((tq (geiser-con--connection-tq c))
(proc (geiser-con--connection-process c))
(proc-filter (geiser-con--connection-filter c)))
(unless no-wait
(while (and (not (tq-queue-empty tq))
(accept-process-output proc 0.1))))
(set-process-filter proc proc-filter)
(setcar c nil))))
(defun geiser-con--connection-activate (c)
(when (not (car c))
(let* ((proc (geiser-con--connection-process c))
(tq-filter (geiser-con--connection-tq-filter c)))
(while (accept-process-output proc 0.01))
(set-process-filter proc tq-filter)
(setcar c t))))
;;; Requests handling:
(defun geiser-con--req-form (req answer)
(let* ((con (geiser-con--request-connection req))
(debugging (geiser-con--has-entered-debugger con answer)))
(condition-case err
(let ((start (string-match "((\\(?:result)?\\|error\\) " answer)))
(or (and start (car (read-from-string answer start)))
`((error (key . retort-syntax))
(output . ,answer)
(debug . ,debugging))))
(error `((error (key . geiser-con-error))
(debug . debugging)
(output . ,(format "%s\n(%s)"
answer (error-message-string err))))))))
(defun geiser-con--process-completed-request (req answer)
(let ((cont (geiser-con--request-continuation req))
(id (geiser-con--request-id req))
(rstr (geiser-con--request-string req))
(form (geiser-con--req-form req answer))
(buffer (or (geiser-con--request-buffer req) (current-buffer)))
(con (geiser-con--request-connection req)))
(if (not cont)
(geiser-log--warn "<%s> Dropping result for request %S: %s"
id rstr form)
(condition-case cerr
(with-current-buffer buffer
(funcall cont form)
(geiser-log--info "<%s>: processed" id))
(error (geiser-log--error
"<%s>: continuation failed %S \n\t%s" id rstr cerr))))
(geiser-con--connection-completed con req)))
(defun geiser-con--connection-add-request (c r)
(let ((rstr (geiser-con--request-string r)))
(geiser-log--info "REQUEST: <%s>: %s"
(geiser-con--request-id r)
rstr)
(geiser-con--connection-activate c)
(tq-enqueue (geiser-con--connection-tq c)
(concat rstr "\n")
(geiser-con--connection-eot c)
r
'geiser-con--process-completed-request
t)))
;;; Message sending interface:
(defun geiser-con--send-string (con str cont &optional sbuf)
(let ((req (geiser-con--make-request con str cont sbuf)))
(geiser-con--connection-add-request con req)
req))
(defvar geiser-connection-timeout 30000
"Time limit, in msecs, blocking on synchronous evaluation requests")
(defun geiser-con--interrupt (con)
"Interrupt any request being currently in process."
(when-let (proc (and con (geiser-con--connection-process con)))
(when (process-live-p proc)
(interrupt-process proc))))
(defun geiser-con--wait (req timeout)
"Wait up to TIMEOUT msecs for request REQ to finish, returning its result."
(let* ((con (or (geiser-con--request-connection req)
(error "Geiser connection not active")))
(proc (geiser-con--connection-process con))
(id (geiser-con--request-id req))
(timeout (/ (or timeout geiser-connection-timeout) 1000.0))
(step (/ timeout 10)))
(with-timeout (timeout (geiser-con--request-deactivate req))
(condition-case nil
(while (and (geiser-con--connection-process con)
(not (geiser-con--connection-completed-p con id)))
(accept-process-output proc step))
(error (geiser-con--request-deactivate req))))))
(defun geiser-con--send-string/wait (con str cont &optional timeout sbuf)
(when (and (stringp str) (not (string-blank-p str)))
(save-current-buffer
(let ((proc (and con (geiser-con--connection-process con))))
(unless proc (error "Geiser connection not active"))
(let ((req (geiser-con--send-string con str cont sbuf)))
(geiser-con--wait req timeout))))))
(provide 'geiser-connection)
|