1 ;;; srfi-39.scm --- Parameter objects
3 ;; Copyright (C) 2004, 2005, 2006 Free Software Foundation, Inc.
5 ;; This program is free software; you can redistribute it and/or
6 ;; modify it under the terms of the GNU General Public License as
7 ;; published by the Free Software Foundation; either version 2, or
8 ;; (at your option) any later version.
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 GNU
13 ;; General Public License for more details.
15 ;; You should have received a copy of the GNU General Public License
16 ;; along with this software; see the file COPYING. If not, write to
17 ;; the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
18 ;; Boston, MA 02110-1301 USA
20 ;; As a special exception, the Free Software Foundation gives permission
21 ;; for additional uses of the text contained in its release of GUILE.
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.
29 ;; This exception does not however invalidate any other reasons why
30 ;; the executable file might be covered by the GNU General Public License.
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.
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.
44 ;;; Author: Jose Antonio Ortega Ruiz <jao@gnu.org>
49 ;; This is an implementation of SRFI-39 (Parameter objects).
51 ;; The implementation is based on Guile's fluid objects, and is, therefore,
52 ;; thread-safe (parameters are thread-local).
54 ;; In addition to the forms defined in SRFI-39 (`make-parameter',
55 ;; `parameterize'), a new procedure `with-parameters*' is provided.
56 ;; This procedures is analogous to `with-fluids*' but taking as first
57 ;; argument a list of parameter objects instead of a list of fluids.
62 (define-module (srfi srfi-39)
63 #:use-module (ice-9 syncase)
64 #:use-module (srfi srfi-16)
66 #:export (make-parameter)
67 #:export-syntax (parameterize)
69 ;; helper procedure not in srfi-39.
70 #:export (with-parameters*)
71 #:replace (current-input-port current-output-port current-error-port))
73 ;; Make 'srfi-39 available as a feature identifiere to `cond-expand'.
75 (cond-expand-provide (current-module) '(srfi-39))
77 (define make-parameter
79 ((val) (make-parameter/helper val (lambda (x) x)))
80 ((val conv) (make-parameter/helper val conv))))
82 (define get-fluid-tag (lambda () 'get-fluid)) ;; arbitrary unique (as per eq?) value
83 (define get-conv-tag (lambda () 'get-conv)) ;; arbitrary unique (as per eq?) value
85 (define (make-parameter/helper val conv)
86 (let ((value (make-fluid))
89 (fluid-set! value (conv val))
92 ((null? new-value) (fluid-ref value))
93 ((eq? (car new-value) get-fluid-tag) value)
94 ((eq? (car new-value) get-conv-tag) conv)
95 ((null? (cdr new-value)) (fluid-set! value (conv (car new-value))))
96 (else (error "make-parameter expects 0 or 1 arguments" new-value)))))))
98 (define-syntax parameterize
100 ((_ ((?param ?value) ...) ?body ...)
101 (with-parameters* (list ?param ...)
103 (lambda () ?body ...)))))
105 (define (current-input-port . new-value)
106 (if (null? new-value)
107 ((@ (guile) current-input-port))
108 (apply set-current-input-port new-value)))
110 (define (current-output-port . new-value)
111 (if (null? new-value)
112 ((@ (guile) current-output-port))
113 (apply set-current-output-port new-value)))
115 (define (current-error-port . new-value)
116 (if (null? new-value)
117 ((@ (guile) current-error-port))
118 (apply set-current-error-port new-value)))
121 (list current-input-port current-output-port current-error-port))
123 ;; There are no fluids behind current-input-port etc, so those parameter
124 ;; objects are picked out of the list and handled separately with a
125 ;; dynamic-wind to swap their values to and from a location (the "value"
126 ;; variable in the swapper procedure "let").
128 ;; current-input-port etc are already per-dynamic-root, so this arrangement
129 ;; works the same as a fluid. Perhaps they could become fluids for ease of
130 ;; implementation here.
132 ;; Notice the use of a param local variable for the swapper procedure. It
133 ;; ensures any application changes to the PARAMS list won't affect the
136 (define (with-parameters* params values thunk)
137 (let more ((params params)
139 (fluids '()) ;; fluids from each of PARAMS
140 (convs '()) ;; VALUES with conversion proc applied
141 (swapper noop)) ;; wind/unwind procedure for ports handling
143 (if (eq? noop swapper)
144 (with-fluids* fluids convs thunk)
148 (with-fluids* fluids convs thunk))
150 (if (memq (car params) port-list)
151 (more (cdr params) (cdr values)
153 (let ((param (car params))
155 (prev-swapper swapper))
157 (set! value (param value))
159 (more (cdr params) (cdr values)
160 (cons ((car params) get-fluid-tag) fluids)
161 (cons (((car params) get-conv-tag) (car values)) convs)