Mercurial > emacs
annotate lisp/emacs-lisp/tq.el @ 19860:c17fd465ea95 libc-970911 libc-970912 libc-970913 libc-970914 libc-970915 libc-970916 libc-970917 libc-970918 libc-970919 libc-970920 libc-970921 libc-970922 libc-970923 libc-970924 libc-970925 libc-970926 libc-970927 libc-970928 libc-970929 libc-970930 libc-971001 libc-971018 libc-971019 libc-971020 libc-971021 libc-971022 libc-971023 libc-971024 libc-971025 libc-971026 libc-971027 libc-971028 libc-971029 libc-971030 libc-971031 libc-971101 libc-971102 libc-971103 libc-971104 libc-971105 libc-971106 libc-971107 libc-971108 libc-971109 libc-971110 libc-971111 libc-971112 libc-971113 libc-971114 libc-971115 libc-971116 libc-971117 libc-971118 libc-971120 libc-971121 libc-971122 libc-971123 libc-971124 libc-971125 libc-971126 libc-971127 libc-971128 libc-971129 libc-971130 libc-971201 libc-971203 libc-971204 libc-971205 libc-971206 libc-971207 libc-971208 libc-971209 libc-971210 libc-971211 libc-971212 libc-971213 libc-971214 libc-971217 libc-971218 libc-971219 libc-971220 libc-971221 libc-971222 libc-971223 libc-971224 libc-971225 libc-971226 libc-971227 libc-971228 libc-971229 libc-971230 libc-971231 libc-980103 libc-980104 libc-980105 libc-980106 libc-980107 libc-980108 libc-980109 libc-980110 libc-980111 libc-980112 libc-980114 libc-980115 libc-980116 libc-980117 libc-980118 libc-980119 libc-980120 libc-980121 libc-980122 libc-980123 libc-980124 libc-980125 libc-980126 libc-980127 libc-980128
typos.
author | Jeff Law <law@redhat.com> |
---|---|
date | Wed, 10 Sep 1997 21:16:20 +0000 |
parents | ef3033d89075 |
children | b48b8060e70d |
rev | line source |
---|---|
743 | 1 ;;; tq.el --- utility to maintain a transaction queue |
2 | |
841 | 3 ;; Copyright (C) 1985, 1986, 1987, 1992 Free Software Foundation, Inc. |
4 | |
783
59dc833c4e0c
*** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
743
diff
changeset
|
5 ;; Author: Scott Draves <spot@cs.cmu.edu> |
59dc833c4e0c
*** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
743
diff
changeset
|
6 ;; Adapted-By: ESR |
814
38b2499cb3e9
*** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
783
diff
changeset
|
7 ;; Keywords: extensions |
783
59dc833c4e0c
*** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
743
diff
changeset
|
8 |
841 | 9 ;; This file is part of GNU Emacs. |
10 | |
11 ;; GNU Emacs is free software; you can redistribute it and/or modify | |
12 ;; it under the terms of the GNU General Public License as published by | |
13 ;; the Free Software Foundation; either version 2, or (at your option) | |
14 ;; any later version. | |
15 | |
16 ;; GNU Emacs is distributed in the hope that it will be useful, | |
17 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of | |
18 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
19 ;; GNU General Public License for more details. | |
20 | |
21 ;; You should have received a copy of the GNU General Public License | |
14169 | 22 ;; along with GNU Emacs; see the file COPYING. If not, write to the |
23 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330, | |
24 ;; Boston, MA 02111-1307, USA. | |
841 | 25 |
26 ;;; Commentary: | |
783
59dc833c4e0c
*** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
743
diff
changeset
|
27 |
743 | 28 ;;; manages receiving a stream asynchronously, |
29 ;;; parsing it into transactions, and then calling | |
30 ;;; handler functions | |
31 | |
32 ;;; Our basic structure is the queue/process/buffer triple. Each entry | |
33 ;;; of the queue is a regexp/closure/function triple. We buffer | |
34 ;;; bytes from the process until we see the regexp at the head of the | |
35 ;;; queue. Then we call the function with the closure and the | |
36 ;;; collected bytes. | |
37 | |
783
59dc833c4e0c
*** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
743
diff
changeset
|
38 ;;; Code: |
743 | 39 |
1187
519c04d69cdc
Doc fixes. Make tq-create autoload.
Richard M. Stallman <rms@gnu.org>
parents:
841
diff
changeset
|
40 ;;;###autoload |
743 | 41 (defun tq-create (process) |
1187
519c04d69cdc
Doc fixes. Make tq-create autoload.
Richard M. Stallman <rms@gnu.org>
parents:
841
diff
changeset
|
42 "Create and return a transaction queue communicating with PROCESS. |
519c04d69cdc
Doc fixes. Make tq-create autoload.
Richard M. Stallman <rms@gnu.org>
parents:
841
diff
changeset
|
43 PROCESS should be a subprocess capable of sending and receiving |
519c04d69cdc
Doc fixes. Make tq-create autoload.
Richard M. Stallman <rms@gnu.org>
parents:
841
diff
changeset
|
44 streams of bytes. It may be a local process, or it may be connected |
519c04d69cdc
Doc fixes. Make tq-create autoload.
Richard M. Stallman <rms@gnu.org>
parents:
841
diff
changeset
|
45 to a tcp server on another machine." |
743 | 46 (let ((tq (cons nil (cons process |
47 (generate-new-buffer | |
48 (concat " tq-temp-" | |
49 (process-name process))))))) | |
50 (set-process-filter process | |
51 (`(lambda (proc string) | |
52 (tq-filter '(, tq) string)))) | |
53 tq)) | |
54 | |
55 ;;; accessors | |
56 (defun tq-queue (tq) (car tq)) | |
57 (defun tq-process (tq) (car (cdr tq))) | |
58 (defun tq-buffer (tq) (cdr (cdr tq))) | |
59 | |
60 (defun tq-queue-add (tq re closure fn) | |
61 (setcar tq (nconc (tq-queue tq) | |
62 (cons (cons re (cons closure fn)) nil))) | |
63 'ok) | |
64 | |
65 (defun tq-queue-head-regexp (tq) (car (car (tq-queue tq)))) | |
66 (defun tq-queue-head-fn (tq) (cdr (cdr (car (tq-queue tq))))) | |
67 (defun tq-queue-head-closure (tq) (car (cdr (car (tq-queue tq))))) | |
68 (defun tq-queue-empty (tq) (not (tq-queue tq))) | |
69 (defun tq-queue-pop (tq) (setcar tq (cdr (car tq))) (null (car tq))) | |
70 | |
71 | |
72 ;;; must add to queue before sending! | |
73 (defun tq-enqueue (tq question regexp closure fn) | |
1187
519c04d69cdc
Doc fixes. Make tq-create autoload.
Richard M. Stallman <rms@gnu.org>
parents:
841
diff
changeset
|
74 "Add a transaction to transaction queue TQ. |
519c04d69cdc
Doc fixes. Make tq-create autoload.
Richard M. Stallman <rms@gnu.org>
parents:
841
diff
changeset
|
75 This sends the string QUESTION to the process that TQ communicates with. |
519c04d69cdc
Doc fixes. Make tq-create autoload.
Richard M. Stallman <rms@gnu.org>
parents:
841
diff
changeset
|
76 When the corresponding answer comes back, we call FN |
519c04d69cdc
Doc fixes. Make tq-create autoload.
Richard M. Stallman <rms@gnu.org>
parents:
841
diff
changeset
|
77 with two arguments: CLOSURE, and the answer to the question. |
519c04d69cdc
Doc fixes. Make tq-create autoload.
Richard M. Stallman <rms@gnu.org>
parents:
841
diff
changeset
|
78 REGEXP is a regular expression to match the entire answer; |
519c04d69cdc
Doc fixes. Make tq-create autoload.
Richard M. Stallman <rms@gnu.org>
parents:
841
diff
changeset
|
79 that's how we tell where the answer ends." |
743 | 80 (tq-queue-add tq regexp closure fn) |
81 (process-send-string (tq-process tq) question)) | |
82 | |
83 (defun tq-close (tq) | |
1187
519c04d69cdc
Doc fixes. Make tq-create autoload.
Richard M. Stallman <rms@gnu.org>
parents:
841
diff
changeset
|
84 "Shut down transaction queue TQ, terminating the process." |
743 | 85 (delete-process (tq-process tq)) |
86 (kill-buffer (tq-buffer tq))) | |
87 | |
88 (defun tq-filter (tq string) | |
89 "Append STRING to the TQ's buffer; then process the new data." | |
16203
5a8275a74b25
(tq-filter): Add unwind-protect and save-match-data.
Richard M. Stallman <rms@gnu.org>
parents:
14430
diff
changeset
|
90 (let ((old-buffer (current-buffer))) |
5a8275a74b25
(tq-filter): Add unwind-protect and save-match-data.
Richard M. Stallman <rms@gnu.org>
parents:
14430
diff
changeset
|
91 (unwind-protect |
16235
b5b7d7552f34
(tq-filter): No need for save-match-data.
Richard M. Stallman <rms@gnu.org>
parents:
16203
diff
changeset
|
92 (progn |
16203
5a8275a74b25
(tq-filter): Add unwind-protect and save-match-data.
Richard M. Stallman <rms@gnu.org>
parents:
14430
diff
changeset
|
93 (set-buffer (tq-buffer tq)) |
5a8275a74b25
(tq-filter): Add unwind-protect and save-match-data.
Richard M. Stallman <rms@gnu.org>
parents:
14430
diff
changeset
|
94 (goto-char (point-max)) |
5a8275a74b25
(tq-filter): Add unwind-protect and save-match-data.
Richard M. Stallman <rms@gnu.org>
parents:
14430
diff
changeset
|
95 (insert string) |
5a8275a74b25
(tq-filter): Add unwind-protect and save-match-data.
Richard M. Stallman <rms@gnu.org>
parents:
14430
diff
changeset
|
96 (tq-process-buffer tq)) |
5a8275a74b25
(tq-filter): Add unwind-protect and save-match-data.
Richard M. Stallman <rms@gnu.org>
parents:
14430
diff
changeset
|
97 (set-buffer old-buffer)))) |
743 | 98 |
99 (defun tq-process-buffer (tq) | |
100 "Check TQ's buffer for the regexp at the head of the queue." | |
101 (set-buffer (tq-buffer tq)) | |
102 (if (= 0 (buffer-size)) () | |
103 (if (tq-queue-empty tq) | |
104 (let ((buf (generate-new-buffer "*spurious*"))) | |
105 (copy-to-buffer buf (point-min) (point-max)) | |
106 (delete-region (point-min) (point)) | |
107 (pop-to-buffer buf nil) | |
14430
408eeedc18cd
(tq-process-buffer): Fix error format string.
Karl Heuer <kwzh@gnu.org>
parents:
14169
diff
changeset
|
108 (error "Spurious communication from process %s, see buffer %s" |
408eeedc18cd
(tq-process-buffer): Fix error format string.
Karl Heuer <kwzh@gnu.org>
parents:
14169
diff
changeset
|
109 (process-name (tq-process tq)) |
408eeedc18cd
(tq-process-buffer): Fix error format string.
Karl Heuer <kwzh@gnu.org>
parents:
14169
diff
changeset
|
110 (buffer-name buf))) |
743 | 111 (goto-char (point-min)) |
112 (if (re-search-forward (tq-queue-head-regexp tq) nil t) | |
113 (let ((answer (buffer-substring (point-min) (point)))) | |
114 (delete-region (point-min) (point)) | |
17216
ef3033d89075
(tq-process-buffer): Catch errors running tq-queue-head-fn.
Richard M. Stallman <rms@gnu.org>
parents:
16235
diff
changeset
|
115 (unwind-protect |
ef3033d89075
(tq-process-buffer): Catch errors running tq-queue-head-fn.
Richard M. Stallman <rms@gnu.org>
parents:
16235
diff
changeset
|
116 (condition-case nil |
ef3033d89075
(tq-process-buffer): Catch errors running tq-queue-head-fn.
Richard M. Stallman <rms@gnu.org>
parents:
16235
diff
changeset
|
117 (funcall (tq-queue-head-fn tq) |
ef3033d89075
(tq-process-buffer): Catch errors running tq-queue-head-fn.
Richard M. Stallman <rms@gnu.org>
parents:
16235
diff
changeset
|
118 (tq-queue-head-closure tq) |
ef3033d89075
(tq-process-buffer): Catch errors running tq-queue-head-fn.
Richard M. Stallman <rms@gnu.org>
parents:
16235
diff
changeset
|
119 answer) |
ef3033d89075
(tq-process-buffer): Catch errors running tq-queue-head-fn.
Richard M. Stallman <rms@gnu.org>
parents:
16235
diff
changeset
|
120 (error nil)) |
ef3033d89075
(tq-process-buffer): Catch errors running tq-queue-head-fn.
Richard M. Stallman <rms@gnu.org>
parents:
16235
diff
changeset
|
121 (tq-queue-pop tq)) |
743 | 122 (tq-process-buffer tq)))))) |
123 | |
841 | 124 (provide 'tq) |
125 | |
743 | 126 ;;; tq.el ends here |