Mercurial > emacs
annotate lisp/emacs-lisp/tq.el @ 47726:33d53d287ee4
Add "no-byte-compile: t" in first line.
author | Juanma Barranquero <lekktu@gmail.com> |
---|---|
date | Tue, 01 Oct 2002 17:06:54 +0000 |
parents | e67950065cf9 |
children | 0d8b17d428b5 |
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> |
39461 | 6 ;; Maintainer: FSF |
783
59dc833c4e0c
*** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
743
diff
changeset
|
7 ;; Adapted-By: ESR |
814
38b2499cb3e9
*** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
783
diff
changeset
|
8 ;; Keywords: extensions |
783
59dc833c4e0c
*** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
743
diff
changeset
|
9 |
841 | 10 ;; This file is part of GNU Emacs. |
11 | |
12 ;; GNU Emacs is free software; you can redistribute it and/or modify | |
13 ;; it under the terms of the GNU General Public License as published by | |
14 ;; the Free Software Foundation; either version 2, or (at your option) | |
15 ;; any later version. | |
16 | |
17 ;; GNU Emacs is distributed in the hope that it will be useful, | |
18 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of | |
19 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
20 ;; GNU General Public License for more details. | |
21 | |
22 ;; You should have received a copy of the GNU General Public License | |
14169 | 23 ;; along with GNU Emacs; see the file COPYING. If not, write to the |
24 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330, | |
25 ;; Boston, MA 02111-1307, USA. | |
841 | 26 |
27 ;;; Commentary: | |
783
59dc833c4e0c
*** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
743
diff
changeset
|
28 |
39568
e67950065cf9
Change ;;; to ;; for outline-minor-mode.
Gerd Moellmann <gerd@gnu.org>
parents:
39461
diff
changeset
|
29 ;; manages receiving a stream asynchronously, |
e67950065cf9
Change ;;; to ;; for outline-minor-mode.
Gerd Moellmann <gerd@gnu.org>
parents:
39461
diff
changeset
|
30 ;; parsing it into transactions, and then calling |
e67950065cf9
Change ;;; to ;; for outline-minor-mode.
Gerd Moellmann <gerd@gnu.org>
parents:
39461
diff
changeset
|
31 ;; handler functions |
743 | 32 |
39568
e67950065cf9
Change ;;; to ;; for outline-minor-mode.
Gerd Moellmann <gerd@gnu.org>
parents:
39461
diff
changeset
|
33 ;; Our basic structure is the queue/process/buffer triple. Each entry |
e67950065cf9
Change ;;; to ;; for outline-minor-mode.
Gerd Moellmann <gerd@gnu.org>
parents:
39461
diff
changeset
|
34 ;; of the queue is a regexp/closure/function triple. We buffer |
e67950065cf9
Change ;;; to ;; for outline-minor-mode.
Gerd Moellmann <gerd@gnu.org>
parents:
39461
diff
changeset
|
35 ;; bytes from the process until we see the regexp at the head of the |
e67950065cf9
Change ;;; to ;; for outline-minor-mode.
Gerd Moellmann <gerd@gnu.org>
parents:
39461
diff
changeset
|
36 ;; queue. Then we call the function with the closure and the |
e67950065cf9
Change ;;; to ;; for outline-minor-mode.
Gerd Moellmann <gerd@gnu.org>
parents:
39461
diff
changeset
|
37 ;; collected bytes. |
743 | 38 |
783
59dc833c4e0c
*** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
743
diff
changeset
|
39 ;;; Code: |
743 | 40 |
1187
519c04d69cdc
Doc fixes. Make tq-create autoload.
Richard M. Stallman <rms@gnu.org>
parents:
841
diff
changeset
|
41 ;;;###autoload |
743 | 42 (defun tq-create (process) |
1187
519c04d69cdc
Doc fixes. Make tq-create autoload.
Richard M. Stallman <rms@gnu.org>
parents:
841
diff
changeset
|
43 "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
|
44 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
|
45 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
|
46 to a tcp server on another machine." |
743 | 47 (let ((tq (cons nil (cons process |
48 (generate-new-buffer | |
49 (concat " tq-temp-" | |
50 (process-name process))))))) | |
51 (set-process-filter process | |
52 (`(lambda (proc string) | |
53 (tq-filter '(, tq) string)))) | |
54 tq)) | |
55 | |
56 ;;; accessors | |
57 (defun tq-queue (tq) (car tq)) | |
58 (defun tq-process (tq) (car (cdr tq))) | |
59 (defun tq-buffer (tq) (cdr (cdr tq))) | |
60 | |
61 (defun tq-queue-add (tq re closure fn) | |
62 (setcar tq (nconc (tq-queue tq) | |
63 (cons (cons re (cons closure fn)) nil))) | |
64 'ok) | |
65 | |
66 (defun tq-queue-head-regexp (tq) (car (car (tq-queue tq)))) | |
67 (defun tq-queue-head-fn (tq) (cdr (cdr (car (tq-queue tq))))) | |
68 (defun tq-queue-head-closure (tq) (car (cdr (car (tq-queue tq))))) | |
69 (defun tq-queue-empty (tq) (not (tq-queue tq))) | |
70 (defun tq-queue-pop (tq) (setcar tq (cdr (car tq))) (null (car tq))) | |
71 | |
72 | |
73 ;;; must add to queue before sending! | |
74 (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
|
75 "Add a transaction to transaction queue TQ. |
519c04d69cdc
Doc fixes. Make tq-create autoload.
Richard M. Stallman <rms@gnu.org>
parents:
841
diff
changeset
|
76 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
|
77 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
|
78 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
|
79 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
|
80 that's how we tell where the answer ends." |
743 | 81 (tq-queue-add tq regexp closure fn) |
82 (process-send-string (tq-process tq) question)) | |
83 | |
84 (defun tq-close (tq) | |
1187
519c04d69cdc
Doc fixes. Make tq-create autoload.
Richard M. Stallman <rms@gnu.org>
parents:
841
diff
changeset
|
85 "Shut down transaction queue TQ, terminating the process." |
743 | 86 (delete-process (tq-process tq)) |
87 (kill-buffer (tq-buffer tq))) | |
88 | |
89 (defun tq-filter (tq string) | |
90 "Append STRING to the TQ's buffer; then process the new data." | |
20693
b48b8060e70d
(tq-filter): Use with-current-buffer.
Richard M. Stallman <rms@gnu.org>
parents:
17216
diff
changeset
|
91 (with-current-buffer (tq-buffer tq) |
b48b8060e70d
(tq-filter): Use with-current-buffer.
Richard M. Stallman <rms@gnu.org>
parents:
17216
diff
changeset
|
92 (goto-char (point-max)) |
b48b8060e70d
(tq-filter): Use with-current-buffer.
Richard M. Stallman <rms@gnu.org>
parents:
17216
diff
changeset
|
93 (insert string) |
b48b8060e70d
(tq-filter): Use with-current-buffer.
Richard M. Stallman <rms@gnu.org>
parents:
17216
diff
changeset
|
94 (tq-process-buffer tq))) |
743 | 95 |
96 (defun tq-process-buffer (tq) | |
97 "Check TQ's buffer for the regexp at the head of the queue." | |
98 (set-buffer (tq-buffer tq)) | |
99 (if (= 0 (buffer-size)) () | |
100 (if (tq-queue-empty tq) | |
101 (let ((buf (generate-new-buffer "*spurious*"))) | |
102 (copy-to-buffer buf (point-min) (point-max)) | |
103 (delete-region (point-min) (point)) | |
104 (pop-to-buffer buf nil) | |
14430
408eeedc18cd
(tq-process-buffer): Fix error format string.
Karl Heuer <kwzh@gnu.org>
parents:
14169
diff
changeset
|
105 (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
|
106 (process-name (tq-process tq)) |
408eeedc18cd
(tq-process-buffer): Fix error format string.
Karl Heuer <kwzh@gnu.org>
parents:
14169
diff
changeset
|
107 (buffer-name buf))) |
743 | 108 (goto-char (point-min)) |
109 (if (re-search-forward (tq-queue-head-regexp tq) nil t) | |
110 (let ((answer (buffer-substring (point-min) (point)))) | |
111 (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
|
112 (unwind-protect |
ef3033d89075
(tq-process-buffer): Catch errors running tq-queue-head-fn.
Richard M. Stallman <rms@gnu.org>
parents:
16235
diff
changeset
|
113 (condition-case nil |
ef3033d89075
(tq-process-buffer): Catch errors running tq-queue-head-fn.
Richard M. Stallman <rms@gnu.org>
parents:
16235
diff
changeset
|
114 (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
|
115 (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
|
116 answer) |
ef3033d89075
(tq-process-buffer): Catch errors running tq-queue-head-fn.
Richard M. Stallman <rms@gnu.org>
parents:
16235
diff
changeset
|
117 (error nil)) |
ef3033d89075
(tq-process-buffer): Catch errors running tq-queue-head-fn.
Richard M. Stallman <rms@gnu.org>
parents:
16235
diff
changeset
|
118 (tq-queue-pop tq)) |
743 | 119 (tq-process-buffer tq)))))) |
120 | |
841 | 121 (provide 'tq) |
122 | |
743 | 123 ;;; tq.el ends here |