guile-devel
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Proposed change to `make-readline-port'


From: Neil Jerram
Subject: Re: Proposed change to `make-readline-port'
Date: 09 Mar 2001 11:37:31 +0000
User-agent: Gnus/5.0808 (Gnus v5.8.8) Emacs/20.5

>>>>> "Dirk" == Dirk Herrmann <address@hidden> writes:

    Dirk> Well, if I remember right, you actually wanted to provide a
    Dirk> general buffered-input-port rather than just a line-buffered
    Dirk> input port, and that's what my suggestions was targeted at.

OK; what do you think of the diff below, where I've made the
interpolation of a character such as newline optional, and off by
default?  Would you still prefer to remove the interpolation option
completely?

    Dirk> But, again: I am not any more convinced that the current
    Dirk> implementation of line-buffered input port deserves a module
    Dirk> of its own, since it is too specifically designed around the
    Dirk> use in the repl.  We should, IMO, rethink that decision.

I agreed that it is targetted at use in repls.  But (a) I don't think
this is the only possible use, and (b) even for repls, there are many
possible repl implementations, and it makes sense for them to share
common code.

Regards,
        Neil

Index: buffered-input.scm
===================================================================
RCS file: /cvs/guile/guile-core/ice-9/buffered-input.scm,v
retrieving revision 1.2
diff -u -r1.2 buffered-input.scm
--- buffered-input.scm  2001/03/07 23:03:37     1.2
+++ buffered-input.scm  2001/03/09 11:31:03
@@ -18,7 +18,7 @@
 ;;;; Boston, MA 02111-1307 USA
 
 (define-module (ice-9 buffered-input)
-  #:export (make-line-buffered-input-port
+  #:export (make-buffered-input-port
             set-buffered-input-continuation?!))
 
 ;; @code{buffered-input-continuation?} is a property of the ports
@@ -29,20 +29,27 @@
 (define (set-buffered-input-continuation?! port val)
   "Set the read continuation flag for @var{port} to @var{val}.
 
-See @code{make-line-buffered-input-port} for the meaning and use of
-this flag."
+See @code{make-buffered-input-port} for the meaning and use of this
+flag."
   (set! (buffered-input-continuation? port) val))
 
-(define (make-line-buffered-input-port reader)
+(define (make-buffered-input-port reader . separator)
   "Construct a line-buffered input port from the specified @var{reader}.
 @var{reader} should be a procedure of one argument that somehow reads
 a line of input and returns it as a string @emph{without} the
 terminating newline character.
 
-The port created by @code{make-line-buffered-input-port} automatically
-adds a newline character after each string returned by @var{reader};
-this makes these ports useful for reading strings that extend across
-more than one input line.
+If the optional @var{separator} argument is supplied, it should be a
+single character.  In this case, the port created by
address@hidden automatically interpolates this
+character after each string returned by @var{reader}.  This is useful
+for ports where the chunks of input read by @var{reader} are logically
+separate in a sense that should be reflected in the stream of
+characters that are read from the port.  For example if @var{reader}
+reads a complete line of input from the user at a time and returns the
+line without a terminating newline character, @var{separator} may be
+set to @code{#\\newline}, so that the boundaries between one input line
+and the next are reflected in what is read from the port.
 
 @var{reader} should take a boolean @var{continuation?} argument.
 @var{continuation?} indicates whether @var{reader} is being called to
@@ -56,12 +63,18 @@
 The new/continuation distinction is largely an application-level
 concept, and @code{set-buffered-input-continuation?!} allows an
 application some control over when a read operation is considered to
-be new.  But note that if there is data already buffered in the port
-when a new read operation starts, this data will be read before the
-first call to @var{reader}, and so @var{reader} will be called with
address@hidden set to @code{#t}."
+be new.  But note that if there is non-whitespace data already
+buffered in the port when a new read operation starts, this data will
+be read before the first call to @var{reader}, and so @var{reader}
+will be called with @var{continuation?} set to @code{#t}."
   (let ((read-string "")
-       (string-index -1))
+       (string-index -1)
+        (separator (cond ((null? separator)
+                          #f)
+                         ((char? (car separator))
+                          (car separator))
+                         (else
+                          (error "Separator argument must be a character!")))))
     (letrec ((get-character
              (lambda ()
                (cond 
@@ -69,7 +82,8 @@
                  read-string)
                 ((>= string-index (string-length read-string))
                  (set! string-index -1)
-                  #\nl)
+                  (or separator
+                      (get-character)))
                 ((= string-index -1)
                  (set! read-string (reader (buffered-input-continuation? 
port)))
                   (set! string-index 0)



reply via email to

[Prev in Thread] Current Thread [Next in Thread]