annotate lisp/utils/tq.el @ 0:376386a54a3c r19-14

Import from CVS: tag r19-14
author cvs
date Mon, 13 Aug 2007 08:45:50 +0200
parents
children ac2d302a0011
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 ;;; tq.el --- utility to maintain a transaction queue
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
3 ;; Copyright (C) 1985, 1986, 1987, 1992 Free Software Foundation, Inc.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
4
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
5 ;; Author: Scott Draves <spot@cs.cmu.edu>
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
6 ;; Adapted-By: ESR
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
7 ;; Keywords: extensions
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
8
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
9 ;; This file is part of GNU Emacs.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
10
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
11 ;; GNU Emacs is free software; you can redistribute it and/or modify
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
12 ;; it under the terms of the GNU General Public License as published by
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
13 ;; the Free Software Foundation; either version 2, or (at your option)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
14 ;; any later version.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
15
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
16 ;; GNU Emacs is distributed in the hope that it will be useful,
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
17 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
18 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
19 ;; GNU General Public License for more details.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
20
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
21 ;; You should have received a copy of the GNU General Public License
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
22 ;; along with GNU Emacs; see the file COPYING. If not, write to
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
23 ;; the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
24
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
25 ;;; Synched up with: FSF 19.30.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
26
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
27 ;;; Commentary:
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
28
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
29 ;;; manages receiving a stream asynchronously,
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
30 ;;; parsing it into transactions, and then calling
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
31 ;;; handler functions
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
32
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
33 ;;; Our basic structure is the queue/process/buffer triple. Each entry
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
34 ;;; of the queue is a regexp/closure/function triple. We buffer
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
35 ;;; bytes from the process until we see the regexp at the head of the
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
36 ;;; queue. Then we call the function with the closure and the
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
37 ;;; collected bytes.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
38
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
39 ;;; Code:
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
40
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
41 ;;;###autoload
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
42 (defun tq-create (process)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
43 "Create and return a transaction queue communicating with PROCESS.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
44 PROCESS should be a subprocess capable of sending and receiving
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
45 streams of bytes. It may be a local process, or it may be connected
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
46 to a tcp server on another machine."
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
47 (let ((tq (cons nil (cons process
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
48 (generate-new-buffer
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
49 (concat " tq-temp-"
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
50 (process-name process)))))))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
51 (set-process-filter process
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
52 (`(lambda (proc string)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
53 (tq-filter '(, tq) string))))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
54 tq))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
55
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
56 ;;; accessors
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
57 (defun tq-queue (tq) (car tq))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
58 (defun tq-process (tq) (car (cdr tq)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
59 (defun tq-buffer (tq) (cdr (cdr tq)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
60
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
61 (defun tq-queue-add (tq re closure fn)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
62 (setcar tq (nconc (tq-queue tq)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
63 (cons (cons re (cons closure fn)) nil)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
64 'ok)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
65
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
66 (defun tq-queue-head-regexp (tq) (car (car (tq-queue tq))))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
67 (defun tq-queue-head-fn (tq) (cdr (cdr (car (tq-queue tq)))))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
68 (defun tq-queue-head-closure (tq) (car (cdr (car (tq-queue tq)))))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
69 (defun tq-queue-empty (tq) (not (tq-queue tq)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
70 (defun tq-queue-pop (tq) (setcar tq (cdr (car tq))) (null (car tq)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
71
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
72
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
73 ;;; must add to queue before sending!
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
74 (defun tq-enqueue (tq question regexp closure fn)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
75 "Add a transaction to transaction queue TQ.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
76 This sends the string QUESTION to the process that TQ communicates with.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
77 When the corresponding answer comes back, we call FN
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
78 with two arguments: CLOSURE, and the answer to the question.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
79 REGEXP is a regular expression to match the entire answer;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
80 that's how we tell where the answer ends."
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
81 (tq-queue-add tq regexp closure fn)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
82 (process-send-string (tq-process tq) question))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
83
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
84 (defun tq-close (tq)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
85 "Shut down transaction queue TQ, terminating the process."
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
86 (delete-process (tq-process tq))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
87 (kill-buffer (tq-buffer tq)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
88
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
89 (defun tq-filter (tq string)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
90 "Append STRING to the TQ's buffer; then process the new data."
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
91 (set-buffer (tq-buffer tq))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
92 (goto-char (point-max))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
93 (insert string)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
94 (tq-process-buffer tq))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
95
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
96 (defun tq-process-buffer (tq)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
97 "Check TQ's buffer for the regexp at the head of the queue."
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
98 (set-buffer (tq-buffer tq))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
99 (if (= 0 (buffer-size)) ()
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
100 (if (tq-queue-empty tq)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
101 (let ((buf (generate-new-buffer "*spurious*")))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
102 (copy-to-buffer buf (point-min) (point-max))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
103 (delete-region (point-min) (point))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
104 (pop-to-buffer buf nil)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
105 (error (concat "Spurious communication from process "
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
106 (process-name (tq-process tq))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
107 ", see buffer " (buffer-name buf) ".")))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
108 (goto-char (point-min))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
109 (if (re-search-forward (tq-queue-head-regexp tq) nil t)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
110 (let ((answer (buffer-substring (point-min) (point))))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
111 (delete-region (point-min) (point))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
112 (funcall (tq-queue-head-fn tq)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
113 (tq-queue-head-closure tq)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
114 answer)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
115 (tq-queue-pop tq)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
116 (tq-process-buffer tq))))))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
117
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
118 (provide 'tq)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
119
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
120 ;;; tq.el ends here