Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
marvel
GitHub Repository: marvel/qnf
Path: blob/master/elisp/slime/contrib/bridge.el
990 views
1
;;; -*-Emacs-Lisp-*-
2
;;;%Header
3
;;; Bridge process filter, V1.0
4
;;; Copyright (C) 1991 Chris McConnell, [email protected]
5
;;;
6
;;; Send mail to [email protected] if you have problems.
7
;;;
8
;;; Send mail to [email protected] if you want to be on the
9
;;; ilisp mailing list.
10
11
;;; This file is part of GNU Emacs.
12
13
;;; GNU Emacs is distributed in the hope that it will be useful,
14
;;; but WITHOUT ANY WARRANTY. No author or distributor
15
;;; accepts responsibility to anyone for the consequences of using it
16
;;; or for whether it serves any particular purpose or works at all,
17
;;; unless he says so in writing. Refer to the GNU Emacs General Public
18
;;; License for full details.
19
20
;;; Everyone is granted permission to copy, modify and redistribute
21
;;; GNU Emacs, but only under the conditions described in the
22
;;; GNU Emacs General Public License. A copy of this license is
23
;;; supposed to have been given to you along with GNU Emacs so you
24
;;; can know your rights and responsibilities. It should be in a
25
;;; file named COPYING. Among other things, the copyright notice
26
;;; and this notice must be preserved on all copies.
27
28
;;; Send any bugs or comments. Thanks to Todd Kaufmann for rewriting
29
;;; the process filter for continuous handlers.
30
31
;;; USAGE: M-x install-bridge will add a process output filter to the
32
;;; current buffer. Any output that the process does between
33
;;; bridge-start-regexp and bridge-end-regexp will be bundled up and
34
;;; passed to the first handler on bridge-handlers that matches the
35
;;; output using string-match. If bridge-prompt-regexp shows up
36
;;; before bridge-end-regexp, the bridge will be cancelled. If no
37
;;; handler matches the output, the first symbol in the output is
38
;;; assumed to be a buffer name and the rest of the output will be
39
;;; sent to that buffer's process. This can be used to communicate
40
;;; between processes or to set up two way interactions between Emacs
41
;;; and an inferior process.
42
43
;;; You can write handlers that process the output in special ways.
44
;;; See bridge-send-handler for the default handler. The command
45
;;; hand-bridge is useful for testing. Keep in mind that all
46
;;; variables are buffer local.
47
48
;;; YOUR .EMACS FILE:
49
;;;
50
;;; ;;; Set up load path to include bridge
51
;;; (setq load-path (cons "/bridge-directory/" load-path))
52
;;; (autoload 'install-bridge "bridge" "Install a process bridge." t)
53
;;; (setq bridge-hook
54
;;; '(lambda ()
55
;;; ;; Example options
56
;;; (setq bridge-source-insert nil) ;Don't insert in source buffer
57
;;; (setq bridge-destination-insert nil) ;Don't insert in dest buffer
58
;;; ;; Handle copy-it messages yourself
59
;;; (setq bridge-handlers
60
;;; '(("copy-it" . my-copy-handler)))))
61
62
;;; EXAMPLE:
63
;;; # This pipes stdin to the named buffer in a Unix shell
64
;;; alias devgnu '(echo -n "\!* "; cat -; echo -n "")'
65
;;;
66
;;; ls | devgnu *scratch*
67
68
(eval-when-compile
69
(require 'cl))
70
71
;;;%Parameters
72
(defvar bridge-hook nil
73
"Hook called when a bridge is installed by install-hook.")
74
75
(defvar bridge-start-regexp ""
76
"*Regular expression to match the start of a process bridge in
77
process output. It should be followed by a buffer name, the data to
78
be sent and a bridge-end-regexp.")
79
80
(defvar bridge-end-regexp ""
81
"*Regular expression to match the end of a process bridge in process
82
output.")
83
84
(defvar bridge-prompt-regexp nil
85
"*Regular expression for detecting a prompt. If there is a
86
comint-prompt-regexp, it will be initialized to that. A prompt before
87
a bridge-end-regexp will stop the process bridge.")
88
89
(defvar bridge-handlers nil
90
"Alist of (regexp . handler) for handling process output delimited
91
by bridge-start-regexp and bridge-end-regexp. The first entry on the
92
list whose regexp matches the output will be called on the process and
93
the delimited output.")
94
95
(defvar bridge-source-insert t
96
"*T to insert bridge input in the source buffer minus delimiters.")
97
98
(defvar bridge-destination-insert t
99
"*T for bridge-send-handler to insert bridge input into the
100
destination buffer minus delimiters.")
101
102
(defvar bridge-chunk-size 512
103
"*Long inputs send to comint processes are broken up into chunks of
104
this size. If your process is choking on big inputs, try lowering the
105
value.")
106
107
;;;%Internal variables
108
(defvar bridge-old-filter nil
109
"Old filter for a bridged process buffer.")
110
111
(defvar bridge-string nil
112
"The current output in the process bridge.")
113
114
(defvar bridge-in-progress nil
115
"The current handler function, if any, that bridge passes strings on to,
116
or nil if none.")
117
118
(defvar bridge-leftovers nil
119
"Because of chunking you might get an incomplete bridge signal - start but the end is in the next packet. Save the overhanging text here.")
120
121
(defvar bridge-send-to-buffer nil
122
"The buffer that the default bridge-handler (bridge-send-handler) is
123
currently sending to, or nil if it hasn't started yet. Your handler
124
function can use this variable also.")
125
126
(defvar bridge-last-failure ()
127
"Last thing that broke the bridge handler. First item is function call
128
(eval'able); last item is error condition which resulted. This is provided
129
to help handler-writers in their debugging.")
130
131
;;;%Utilities
132
(defun bridge-insert (output)
133
"Insert process OUTPUT into the current buffer."
134
(if output
135
(let* ((buffer (current-buffer))
136
(process (get-buffer-process buffer))
137
(mark (process-mark process))
138
(window (selected-window))
139
(at-end nil))
140
(if (eq (window-buffer window) buffer)
141
(setq at-end (= (point) mark))
142
(setq window (get-buffer-window buffer)))
143
(save-excursion
144
(goto-char mark)
145
(insert output)
146
(set-marker mark (point)))
147
(if window
148
(progn
149
(if at-end (goto-char mark))
150
(if (not (pos-visible-in-window-p (point) window))
151
(let ((original (selected-window)))
152
(save-excursion
153
(select-window window)
154
(recenter '(center))
155
(select-window original)))))))))
156
157
;;;
158
;(defun bridge-send-string (process string)
159
; "Send PROCESS the contents of STRING as input.
160
;This is equivalent to process-send-string, except that long input strings
161
;are broken up into chunks of size comint-input-chunk-size. Processes
162
;are given a chance to output between chunks. This can help prevent processes
163
;from hanging when you send them long inputs on some OS's."
164
; (let* ((len (length string))
165
; (i (min len bridge-chunk-size)))
166
; (process-send-string process (substring string 0 i))
167
; (while (< i len)
168
; (let ((next-i (+ i bridge-chunk-size)))
169
; (accept-process-output)
170
; (process-send-string process (substring string i (min len next-i)))
171
; (setq i next-i)))))
172
173
;;;
174
(defun bridge-call-handler (handler proc string)
175
"Funcall HANDLER on PROC, STRING carefully. Error is caught if happens,
176
and user is signaled. State is put in bridge-last-failure. Returns t if
177
handler executed without error."
178
(let ((inhibit-quit nil)
179
(failed nil))
180
(condition-case err
181
(funcall handler proc string)
182
(error
183
(ding)
184
(setq failed t)
185
(message "bridge-handler \"%s\" failed %s (see bridge-last-failure)"
186
handler err)
187
(setq bridge-last-failure
188
`((funcall ',handler ',proc ,string)
189
"Caused: "
190
,err))))
191
(not failed)))
192
193
;;;%Handlers
194
(defun bridge-send-handler (process input)
195
"Send PROCESS INPUT to the buffer name found at the start of the
196
input. The input after the buffer name is sent to the buffer's
197
process if it has one. If bridge-destination-insert is T, the input
198
will be inserted into the buffer. If it does not have a process, it
199
will be inserted at the end of the buffer."
200
(if (null input)
201
(setq bridge-send-to-buffer nil) ; end of bridge
202
(let (buffer-and-start buffer-name dest to)
203
;; if this is first time, get the buffer out of the first line
204
(cond ((not bridge-send-to-buffer)
205
(setq buffer-and-start (read-from-string input)
206
buffer-name (format "%s" (car (read-from-string input)))
207
dest (get-buffer buffer-name)
208
to (get-buffer-process dest)
209
input (substring input (cdr buffer-and-start)))
210
(setq bridge-send-to-buffer dest))
211
(t
212
(setq buffer-name bridge-send-to-buffer
213
dest (get-buffer buffer-name)
214
to (get-buffer-process dest)
215
)))
216
(if dest
217
(let ((buffer (current-buffer)))
218
(if bridge-destination-insert
219
(unwind-protect
220
(progn
221
(set-buffer dest)
222
(if to
223
(bridge-insert process input)
224
(goto-char (point-max))
225
(insert input)))
226
(set-buffer buffer)))
227
(if to
228
;; (bridge-send-string to input)
229
(process-send-string to input)
230
))
231
(error "%s is not a buffer" buffer-name)))))
232
233
;;;%Filter
234
(defun bridge-filter (process output)
235
"Given PROCESS and some OUTPUT, check for the presence of
236
bridge-start-regexp. Everything prior to this will be passed to the
237
normal filter function or inserted in the buffer if it is nil. The
238
output up to bridge-end-regexp will be sent to the first handler on
239
bridge-handlers that matches the string. If no handlers match, the
240
input will be sent to bridge-send-handler. If bridge-prompt-regexp is
241
encountered before the bridge-end-regexp, the bridge will be cancelled."
242
(let ((inhibit-quit t)
243
(match-data (match-data))
244
(buffer (current-buffer))
245
(process-buffer (process-buffer process))
246
(case-fold-search t)
247
(start 0) (end 0)
248
function
249
b-start b-start-end b-end)
250
(set-buffer process-buffer) ;; access locals
251
252
;; Handle bridge messages that straddle a packet by prepending
253
;; them to this packet.
254
255
(when bridge-leftovers
256
(setq output (concat bridge-leftovers output))
257
(setq bridge-leftovers nil))
258
259
(setq function bridge-in-progress)
260
261
;; How it works:
262
;;
263
;; start, end delimit the part of string we are interested in;
264
;; initially both 0; after an iteration we move them to next string.
265
266
;; b-start, b-end delimit part of string to bridge (possibly whole string);
267
;; this will be string between corresponding regexps.
268
269
;; There are two main cases when we come into loop:
270
271
;; bridge in progress
272
;;0 setq b-start = start
273
;;1 setq b-end (or end-pattern end)
274
;;4 process string
275
;;5 remove handler if end found
276
277
;; no bridge in progress
278
;;0 setq b-start if see start-pattern
279
;;1 setq b-end if bstart to (or end-pattern end)
280
;;2 send (substring start b-start) to normal place
281
;;3 find handler (in b-start, b-end) if not set
282
;;4 process string
283
;;5 remove handler if end found
284
285
;; equivalent sections have the same numbers here;
286
;; we fold them together in this code.
287
288
(block bridge-filter
289
(unwind-protect
290
(while (< end (length output))
291
292
;;0 setq b-start if find
293
(setq b-start
294
(cond (bridge-in-progress
295
(setq b-start-end start)
296
start)
297
((string-match bridge-start-regexp output start)
298
(setq b-start-end (match-end 0))
299
(match-beginning 0))
300
(t nil)))
301
;;1 setq b-end
302
(setq b-end
303
(if b-start
304
(let ((end-seen (string-match bridge-end-regexp
305
output b-start-end)))
306
(if end-seen (setq end (match-end 0)))
307
308
end-seen)))
309
310
;; Detect and save partial bridge messages
311
(when (and b-start b-start-end (not b-end))
312
(setq bridge-leftovers (substring output b-start))
313
)
314
315
(if (and b-start (not b-end))
316
(setq end b-start)
317
(if (not b-end)
318
(setq end (length output))))
319
320
;;1.5 - if see prompt before end, remove current
321
(if (and b-start b-end)
322
(let ((prompt (string-match bridge-prompt-regexp
323
output b-start-end)))
324
(if (and prompt (<= (match-end 0) b-end))
325
(setq b-start nil ; b-start-end start
326
b-end start
327
end (match-end 0)
328
bridge-in-progress nil
329
))))
330
331
;;2 send (substring start b-start) to old filter, if any
332
(when (not (equal start (or b-start end))) ; don't bother on empty string
333
(let ((pass-on (substring output start (or b-start end))))
334
(if bridge-old-filter
335
(let ((old bridge-old-filter))
336
(store-match-data match-data)
337
(funcall old process pass-on)
338
;; if filter changed, re-install ourselves
339
(let ((new (process-filter process)))
340
(if (not (eq new 'bridge-filter))
341
(progn (setq bridge-old-filter new)
342
(set-process-filter process 'bridge-filter)))))
343
(set-buffer process-buffer)
344
(bridge-insert pass-on))))
345
346
(if (and b-start-end (not b-end))
347
(return-from bridge-filter t) ; when last bit has prematurely ending message, exit early.
348
(progn
349
;;3 find handler (in b-start, b-end) if none current
350
(if (and b-start (not bridge-in-progress))
351
(let ((handlers bridge-handlers))
352
(while (and handlers (not function))
353
(let* ((handler (car handlers))
354
(m (string-match (car handler) output b-start-end)))
355
(if (and m (< m b-end))
356
(setq function (cdr handler))
357
(setq handlers (cdr handlers)))))
358
;; Set default handler if none
359
(if (null function)
360
(setq function 'bridge-send-handler))
361
(setq bridge-in-progress function)))
362
;;4 process strin
363
(if function
364
(let ((ok t))
365
(if (/= b-start-end b-end)
366
(let ((send (substring output b-start-end b-end)))
367
;; also, insert the stuff in buffer between
368
;; iff bridge-source-insert.
369
(if bridge-source-insert (bridge-insert send))
370
;; call handler on string
371
(setq ok (bridge-call-handler function process send))))
372
;;5 remove handler if end found
373
;; if function removed then tell it that's all
374
(if (or (not ok) (/= b-end end)) ;; saw end before end-of-string
375
(progn
376
(bridge-call-handler function process nil)
377
;; have to remove function too for next time around
378
(setq function nil
379
bridge-in-progress nil)
380
))
381
))
382
383
;; continue looping, in case there's more string
384
(setq start end))
385
))
386
;; protected forms: restore buffer, match-data
387
(set-buffer buffer)
388
(store-match-data match-data)
389
))))
390
391
392
;;;%Interface
393
(defun install-bridge ()
394
"Set up a process bridge in the current buffer."
395
(interactive)
396
(if (not (get-buffer-process (current-buffer)))
397
(error "%s does not have a process" (buffer-name (current-buffer)))
398
(make-local-variable 'bridge-start-regexp)
399
(make-local-variable 'bridge-end-regexp)
400
(make-local-variable 'bridge-prompt-regexp)
401
(make-local-variable 'bridge-handlers)
402
(make-local-variable 'bridge-source-insert)
403
(make-local-variable 'bridge-destination-insert)
404
(make-local-variable 'bridge-chunk-size)
405
(make-local-variable 'bridge-old-filter)
406
(make-local-variable 'bridge-string)
407
(make-local-variable 'bridge-in-progress)
408
(make-local-variable 'bridge-send-to-buffer)
409
(make-local-variable 'bridge-leftovers)
410
(setq bridge-string nil bridge-in-progress nil
411
bridge-send-to-buffer nil)
412
(if (boundp 'comint-prompt-regexp)
413
(setq bridge-prompt-regexp comint-prompt-regexp))
414
(let ((process (get-buffer-process (current-buffer))))
415
(if process
416
(if (not (eq (process-filter process) 'bridge-filter))
417
(progn
418
(setq bridge-old-filter (process-filter process))
419
(set-process-filter process 'bridge-filter)))
420
(error "%s does not have a process"
421
(buffer-name (current-buffer)))))
422
(run-hooks 'bridge-hook)
423
(message "Process bridge is installed")))
424
425
;;;
426
(defun reset-bridge ()
427
"Must be called from the process's buffer. Removes any active bridge."
428
(interactive)
429
;; for when things get wedged
430
(if bridge-in-progress
431
(unwind-protect
432
(funcall bridge-in-progress (get-buffer-process
433
(current-buffer))
434
nil)
435
(setq bridge-in-progress nil))
436
(message "No bridge in progress.")))
437
438
;;;
439
(defun remove-bridge ()
440
"Remove bridge from the current buffer."
441
(interactive)
442
(let ((process (get-buffer-process (current-buffer))))
443
(if (or (not process) (not (eq (process-filter process) 'bridge-filter)))
444
(error "%s has no bridge" (buffer-name (current-buffer)))
445
;; remove any bridge-in-progress
446
(reset-bridge)
447
(set-process-filter process bridge-old-filter)
448
(funcall bridge-old-filter process bridge-string)
449
(message "Process bridge is removed."))))
450
451
;;;% Utility for testing
452
(defun hand-bridge (start end)
453
"With point at bridge-start, sends bridge-start + string +
454
bridge-end to bridge-filter. With prefix, use current region to send."
455
(interactive "r")
456
(let ((p0 (if current-prefix-arg (min start end)
457
(if (looking-at bridge-start-regexp) (point)
458
(error "Not looking at bridge-start-regexp"))))
459
(p1 (if current-prefix-arg (max start end)
460
(if (re-search-forward bridge-end-regexp nil t)
461
(point) (error "Didn't see bridge-end-regexp")))))
462
463
(bridge-filter (get-buffer-process (current-buffer))
464
(buffer-substring-no-properties p0 p1))
465
))
466
467
(provide 'bridge)
468
469