annotate lisp/ilisp/bridge.el @ 205:92f8ad5d0d3f r20-4b1

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