annotate lisp/emacs-lisp/tq.el @ 973:52267046b144

* xdisp.c (FRAME_IS_TERMCAP, FRAME_IS_X, FRAME_HAS_MINIBUF): Renamed these to FRAME_TERMCAP_P, FRAME_X_P, and FRAME_HAS_MINIBUF_P, for consistency with the rest of the frame macros.
author Jim Blandy <jimb@redhat.com>
date Wed, 12 Aug 1992 14:30:09 +0000
parents 2cdce064065f
children 519c04d69cdc
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
743
4c88459587e3 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
1 ;;; tq.el --- utility to maintain a transaction queue
4c88459587e3 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
2
841
2cdce064065f entered into RCS
Eric S. Raymond <esr@snark.thyrsus.com>
parents: 814
diff changeset
3 ;; Copyright (C) 1985, 1986, 1987, 1992 Free Software Foundation, Inc.
2cdce064065f entered into RCS
Eric S. Raymond <esr@snark.thyrsus.com>
parents: 814
diff changeset
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
2cdce064065f entered into RCS
Eric S. Raymond <esr@snark.thyrsus.com>
parents: 814
diff changeset
9 ;; This file is part of GNU Emacs.
2cdce064065f entered into RCS
Eric S. Raymond <esr@snark.thyrsus.com>
parents: 814
diff changeset
10
2cdce064065f entered into RCS
Eric S. Raymond <esr@snark.thyrsus.com>
parents: 814
diff changeset
11 ;; GNU Emacs is free software; you can redistribute it and/or modify
2cdce064065f entered into RCS
Eric S. Raymond <esr@snark.thyrsus.com>
parents: 814
diff changeset
12 ;; it under the terms of the GNU General Public License as published by
2cdce064065f entered into RCS
Eric S. Raymond <esr@snark.thyrsus.com>
parents: 814
diff changeset
13 ;; the Free Software Foundation; either version 2, or (at your option)
2cdce064065f entered into RCS
Eric S. Raymond <esr@snark.thyrsus.com>
parents: 814
diff changeset
14 ;; any later version.
2cdce064065f entered into RCS
Eric S. Raymond <esr@snark.thyrsus.com>
parents: 814
diff changeset
15
2cdce064065f entered into RCS
Eric S. Raymond <esr@snark.thyrsus.com>
parents: 814
diff changeset
16 ;; GNU Emacs is distributed in the hope that it will be useful,
2cdce064065f entered into RCS
Eric S. Raymond <esr@snark.thyrsus.com>
parents: 814
diff changeset
17 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
2cdce064065f entered into RCS
Eric S. Raymond <esr@snark.thyrsus.com>
parents: 814
diff changeset
18 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
2cdce064065f entered into RCS
Eric S. Raymond <esr@snark.thyrsus.com>
parents: 814
diff changeset
19 ;; GNU General Public License for more details.
2cdce064065f entered into RCS
Eric S. Raymond <esr@snark.thyrsus.com>
parents: 814
diff changeset
20
2cdce064065f entered into RCS
Eric S. Raymond <esr@snark.thyrsus.com>
parents: 814
diff changeset
21 ;; You should have received a copy of the GNU General Public License
2cdce064065f entered into RCS
Eric S. Raymond <esr@snark.thyrsus.com>
parents: 814
diff changeset
22 ;; along with GNU Emacs; see the file COPYING. If not, write to
2cdce064065f entered into RCS
Eric S. Raymond <esr@snark.thyrsus.com>
parents: 814
diff changeset
23 ;; the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
2cdce064065f entered into RCS
Eric S. Raymond <esr@snark.thyrsus.com>
parents: 814
diff changeset
24
2cdce064065f entered into RCS
Eric S. Raymond <esr@snark.thyrsus.com>
parents: 814
diff changeset
25 ;;; Commentary:
783
59dc833c4e0c *** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents: 743
diff changeset
26
743
4c88459587e3 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
27 ;;; manages receiving a stream asynchronously,
4c88459587e3 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
28 ;;; parsing it into transactions, and then calling
4c88459587e3 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
29 ;;; handler functions
4c88459587e3 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
30
4c88459587e3 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
31 ;;; Our basic structure is the queue/process/buffer triple. Each entry
4c88459587e3 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
32 ;;; of the queue is a regexp/closure/function triple. We buffer
4c88459587e3 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
33 ;;; bytes from the process until we see the regexp at the head of the
4c88459587e3 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
34 ;;; queue. Then we call the function with the closure and the
4c88459587e3 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
35 ;;; collected bytes.
4c88459587e3 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
36
783
59dc833c4e0c *** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents: 743
diff changeset
37 ;;; Code:
743
4c88459587e3 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
38
4c88459587e3 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
39 (defun tq-create (process)
4c88459587e3 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
40 "Create and return a transaction queue. PROCESS should be capable
4c88459587e3 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
41 of sending and receiving streams of bytes. It may be a local process,
4c88459587e3 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
42 or it may be connected to a tcp server on another machine."
4c88459587e3 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
43 (let ((tq (cons nil (cons process
4c88459587e3 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
44 (generate-new-buffer
4c88459587e3 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
45 (concat " tq-temp-"
4c88459587e3 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
46 (process-name process)))))))
4c88459587e3 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
47 (set-process-filter process
4c88459587e3 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
48 (`(lambda (proc string)
4c88459587e3 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
49 (tq-filter '(, tq) string))))
4c88459587e3 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
50 tq))
4c88459587e3 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
51
4c88459587e3 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
52 ;;; accessors
4c88459587e3 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
53 (defun tq-queue (tq) (car tq))
4c88459587e3 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
54 (defun tq-process (tq) (car (cdr tq)))
4c88459587e3 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
55 (defun tq-buffer (tq) (cdr (cdr tq)))
4c88459587e3 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
56
4c88459587e3 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
57 (defun tq-queue-add (tq re closure fn)
4c88459587e3 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
58 (setcar tq (nconc (tq-queue tq)
4c88459587e3 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
59 (cons (cons re (cons closure fn)) nil)))
4c88459587e3 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
60 'ok)
4c88459587e3 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
61
4c88459587e3 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
62 (defun tq-queue-head-regexp (tq) (car (car (tq-queue tq))))
4c88459587e3 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
63 (defun tq-queue-head-fn (tq) (cdr (cdr (car (tq-queue tq)))))
4c88459587e3 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
64 (defun tq-queue-head-closure (tq) (car (cdr (car (tq-queue tq)))))
4c88459587e3 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
65 (defun tq-queue-empty (tq) (not (tq-queue tq)))
4c88459587e3 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
66 (defun tq-queue-pop (tq) (setcar tq (cdr (car tq))) (null (car tq)))
4c88459587e3 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
67
4c88459587e3 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
68
4c88459587e3 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
69 ;;; must add to queue before sending!
4c88459587e3 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
70 (defun tq-enqueue (tq question regexp closure fn)
4c88459587e3 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
71 "Add a transaction to TQ. Send question to the process, and call FN
4c88459587e3 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
72 with CLOSURE and and the answer, when it appears. The end of the
4c88459587e3 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
73 answer is identified by REGEXP."
4c88459587e3 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
74 (tq-queue-add tq regexp closure fn)
4c88459587e3 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
75 (process-send-string (tq-process tq) question))
4c88459587e3 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
76
4c88459587e3 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
77 (defun tq-close (tq)
4c88459587e3 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
78 "Shut down the process, and destroy the evidence."
4c88459587e3 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
79 (delete-process (tq-process tq))
4c88459587e3 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
80 (kill-buffer (tq-buffer tq)))
4c88459587e3 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
81
4c88459587e3 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
82 (defun tq-filter (tq string)
4c88459587e3 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
83 "Append STRING to the TQ's buffer; then process the new data."
4c88459587e3 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
84 (set-buffer (tq-buffer tq))
4c88459587e3 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
85 (goto-char (point-max))
4c88459587e3 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
86 (insert string)
4c88459587e3 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
87 (tq-process-buffer tq))
4c88459587e3 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
88
4c88459587e3 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
89 (defun tq-process-buffer (tq)
4c88459587e3 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
90 "Check TQ's buffer for the regexp at the head of the queue."
4c88459587e3 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
91 (set-buffer (tq-buffer tq))
4c88459587e3 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
92 (if (= 0 (buffer-size)) ()
4c88459587e3 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
93 (if (tq-queue-empty tq)
4c88459587e3 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
94 (let ((buf (generate-new-buffer "*spurious*")))
4c88459587e3 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
95 (copy-to-buffer buf (point-min) (point-max))
4c88459587e3 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
96 (delete-region (point-min) (point))
4c88459587e3 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
97 (pop-to-buffer buf nil)
4c88459587e3 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
98 (error (concat "Spurious communication from process "
4c88459587e3 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
99 (process-name (tq-process tq))
4c88459587e3 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
100 ", see buffer *spurious*.")))
4c88459587e3 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
101 (goto-char (point-min))
4c88459587e3 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
102 (if (re-search-forward (tq-queue-head-regexp tq) nil t)
4c88459587e3 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
103 (let ((answer (buffer-substring (point-min) (point))))
4c88459587e3 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
104 (delete-region (point-min) (point))
4c88459587e3 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
105 (funcall (tq-queue-head-fn tq)
4c88459587e3 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
106 (tq-queue-head-closure tq)
4c88459587e3 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
107 answer)
4c88459587e3 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
108 (tq-queue-pop tq)
4c88459587e3 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
109 (tq-process-buffer tq))))))
4c88459587e3 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
110
841
2cdce064065f entered into RCS
Eric S. Raymond <esr@snark.thyrsus.com>
parents: 814
diff changeset
111 (provide 'tq)
2cdce064065f entered into RCS
Eric S. Raymond <esr@snark.thyrsus.com>
parents: 814
diff changeset
112
743
4c88459587e3 Initial revision
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
diff changeset
113 ;;; tq.el ends here