(scm_char_set_xor): bug fix: characters should only be included if
[bpt/guile.git] / ice-9 / channel.scm
CommitLineData
2d857fb1
KN
1;;; Guile object channel
2
3;; Copyright (C) 2001 Free Software Foundation, Inc.
4
5;; This program is free software; you can redistribute it and/or modify
6;; it under the terms of the GNU General Public License as published by
7;; the Free Software Foundation; either version 2, or (at your option)
8;; any later version.
cd96d7e6 9;;
2d857fb1
KN
10;; This program is distributed in the hope that it will be useful,
11;; but WITHOUT ANY WARRANTY; without even the implied warranty of
12;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13;; GNU General Public License for more details.
cd96d7e6 14;;
2d857fb1
KN
15;; You should have received a copy of the GNU General Public License
16;; along with this program; see the file COPYING. If not, write to
17;; the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18;; Boston, MA 02111-1307, USA.
a482f2cc
MV
19;;
20;; As a special exception, the Free Software Foundation gives permission
21;; for additional uses of the text contained in its release of GUILE.
22;;
23;; The exception is that, if you link the GUILE library with other files
24;; to produce an executable, this does not by itself cause the
25;; resulting executable to be covered by the GNU General Public License.
26;; Your use of that executable is in no way restricted on account of
27;; linking the GUILE library code into it.
28;;
29;; This exception does not however invalidate any other reasons why
30;; the executable file might be covered by the GNU General Public License.
31;;
32;; This exception applies only to the code released by the
33;; Free Software Foundation under the name GUILE. If you copy
34;; code from other Free Software Foundation releases into a copy of
35;; GUILE, as the General Public License permits, the exception does
36;; not apply to the code that you add in this way. To avoid misleading
37;; anyone as to the status of such modified files, you must delete
38;; this exception notice from them.
39;;
40;; If you write modifications of your own for GUILE, it is your choice
41;; whether to permit this exception to apply to your modifications.
42;; If you do not wish that, delete this exception notice.
2d857fb1 43
cd96d7e6
TTN
44;;; Commentary:
45
46;; Now you can use Guile's modules in Emacs Lisp like this:
47;;
48;; (guile-import current-module)
49;; (guile-import module-ref)
50;;
51;; (setq assq (module-ref (current-module) 'assq))
52;; => ("<guile>" %%1%% . "#<primitive-procedure assq>")
53;;
54;; (guile-use-modules (ice-9 documentation))
55;;
56;; (object-documentation assq)
57;; =>
58;; " - primitive: assq key alist
59;; - primitive: assv key alist
60;; - primitive: assoc key alist
61;; Fetches the entry in ALIST that is associated with KEY. To decide
62;; whether the argument KEY matches a particular entry in ALIST,
63;; `assq' compares keys with `eq?', `assv' uses `eqv?' and `assoc'
64;; uses `equal?'. If KEY cannot be found in ALIST (according to
65;; whichever equality predicate is in use), then `#f' is returned.
66;; These functions return the entire alist entry found (i.e. both the
67;; key and the value)."
68;;
69;; Probably we can use GTK in Emacs Lisp. Can anybody try it?
70;;
71;; I have also implemented Guile Scheme mode and Scheme Interaction mode.
72;; Just put the following lines in your ~/.emacs:
73;;
74;; (require 'guile-scheme)
75;; (setq initial-major-mode 'scheme-interaction-mode)
76;;
77;; Currently, the following commands are available:
78;;
79;; M-TAB guile-scheme-complete-symbol
80;; M-C-x guile-scheme-eval-define
81;; C-x C-e guile-scheme-eval-last-sexp
82;; C-c C-b guile-scheme-eval-buffer
83;; C-c C-r guile-scheme-eval-region
84;; C-c : guile-scheme-eval-expression
85;;
86;; I'll write more commands soon, or if you want to hack, please take
87;; a look at the following files:
88;;
89;; guile-core/ice-9/channel.scm ;; object channel
90;; guile-core/emacs/guile.el ;; object adapter
91;; guile-core/emacs/guile-emacs.scm ;; Guile <-> Emacs channels
92;; guile-core/emacs/guile-scheme.el ;; Guile Scheme mode
93;;
94;; As always, there are more than one bugs ;)
95
2d857fb1
KN
96;;; Code:
97
1c446a7f 98(define-module (ice-9 channel))
2d857fb1
KN
99
100;;;
101;;; Channel type
102;;;
103
104(define channel-type
105 (make-record-type 'channel '(stdin stdout printer token-module)))
106
107(define make-channel (record-constructor channel-type))
108
1c446a7f 109(define-public (make-object-channel printer)
2d857fb1
KN
110 (make-channel (current-input-port)
111 (current-output-port)
112 printer
113 (make-module)))
114
115(define channel-stdin (record-accessor channel-type 'stdin))
116(define channel-stdout (record-accessor channel-type 'stdout))
117(define channel-printer (record-accessor channel-type 'printer))
118(define channel-token-module (record-accessor channel-type 'token-module))
119
120;;;
121;;; Channel
122;;;
123
1c446a7f 124(define-public (channel-open ch)
2d857fb1
KN
125 (let ((stdin (channel-stdin ch))
126 (stdout (channel-stdout ch))
127 (printer (channel-printer ch))
128 (token-module (channel-token-module ch)))
129 (let loop ()
130 (catch #t
131 (lambda ()
132 (channel:prompt stdout)
133 (let ((cmd (read stdin)))
134 (if (eof-object? cmd)
135 (throw 'quit)
136 (case cmd
137 ((eval)
138 (module-use! (current-module) token-module)
139 (printer ch (eval (read stdin) (current-module))))
140 ((destroy)
141 (let ((token (read stdin)))
142 (if (module-defined? token-module token)
143 (module-remove! token-module token)
144 (channel:error stdout "Invalid token: ~S" token))))
145 ((quit)
146 (throw 'quit))
147 (else
148 (channel:error stdout "Unknown command: ~S" cmd)))))
149 (loop))
150 (lambda (key . args)
151 (case key
152 ((quit) (throw 'quit))
153 (else
154 (format stdout "exception = ~S\n"
155 (list key (apply format #f (cadr args) (caddr args))))
156 (loop))))))))
157
1c446a7f 158(define-public (channel-print-value ch val)
2d857fb1
KN
159 (format (channel-stdout ch) "value = ~S\n" val))
160
1c446a7f 161(define-public (channel-print-token ch val)
2d857fb1
KN
162 (let* ((token (symbol-append (gensym "%%") '%%))
163 (pair (cons token (object->string val))))
164 (format (channel-stdout ch) "token = ~S\n" pair)
165 (module-define! (channel-token-module ch) token val)))
166
167(define (channel:prompt port)
168 (display "channel> " port)
169 (force-output port))
170
171(define (channel:error port msg . args)
172 (display "ERROR: " port)
173 (apply format port msg args)
174 (newline port))
1c446a7f
KN
175
176;;;
177;;; Guile 1.4 compatibility
178;;;
179
180(define guile:eval eval)
181(define eval
182 (if (= (car (procedure-property guile:eval 'arity)) 1)
183 (lambda (x e) (guile:eval x))
184 guile:eval))
185
186(define object->string
187 (if (defined? 'object->string)
188 object->string
189 (lambda (x) (format #f "~S" x))))
cd96d7e6
TTN
190
191;;; channel.scm ends here