guile-devel
[Top][All Lists]
Advanced

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

Proposed change to `make-readline-port'


From: Neil Jerram
Subject: Proposed change to `make-readline-port'
Date: 03 Mar 2001 10:32:06 +0000
User-agent: Gnus/5.0808 (Gnus v5.8.8) Emacs/20.5

Hello!

`make-readline-port' in (ice-9 readline) can be seen as combining two
pieces of functionality:

(i) constructing a character-based port from a line-based reader that
    reads a whole line at a time

(ii) using the underlying Readline library to read a whole line.

The first piece (i) is useful in other, non-Readline scenarios as
well.  In particular, I've been using (i) in work to create an input
port from a GtkEntry widget.

Therefore I propose to split (i) into a new module, (ice-9
line-port), whose main export is a procedure called `make-line-port'
and to modify `make-readline-port' to use `make-line-port'.

The readline.scm diff and new code are attached below.  May I commit
this change?

Regards,
        Neil


cd /home/neil/Guile/ice-9/
diff -u /home/neil/Guile/cvs/guile-core/guile-readline/readline.scm 
/home/neil/Guile/ice-9/readline.scm
--- /home/neil/Guile/cvs/guile-core/guile-readline/readline.scm Sun Jan 28 
18:44:02 2001
+++ /home/neil/Guile/ice-9/readline.scm Sat Mar  3 10:25:10 2001
@@ -26,6 +26,7 @@
 (define-module (ice-9 readline)
   :use-module (ice-9 session)
   :use-module (ice-9 regex)
+  :use-module (ice-9 line-port)
   :no-backtrace)
 
 
@@ -74,41 +75,20 @@
 (define read-hook #f)
 
 (define (make-readline-port)
-  (let ((read-string "")
-       (string-index -1))
-    (letrec ((get-character
-             (lambda ()
-               (cond 
-                ((eof-object? read-string)
-                 read-string)
-                ((>= string-index (string-length read-string))
-                 (begin
-                   (set! string-index -1)
-                   #\nl))
-                ((= string-index -1)
-                 (begin
-                   (set! read-string
-                         (%readline (if (string? prompt)
-                                        prompt
-                                        (prompt))
-                                    input-port
-                                    output-port
-                                    read-hook))
-                   (set! string-index 0)
-                   (if (not (eof-object? read-string))
-                       (begin
-                         (or (string=? read-string "")
-                             (add-history read-string))
-                         (get-character))
-                       read-string)))
-                (else 
-                 (let ((res (string-ref read-string string-index)))
-                   (set! string-index (+ 1 string-index))
-                   (set! prompt prompt2)
-                   res))))))
-      (make-soft-port
-       (vector #f #f #f get-character #f)
-       "r"))))
+  (make-line-port (lambda (continuation?)
+                    (let* ((prompt (if continuation?
+                                       prompt2
+                                       prompt))
+                           (str (%readline (if (string? prompt)
+                                               prompt
+                                               (prompt))
+                                           input-port
+                                           output-port
+                                           read-hook)))
+                      (or (eof-object? str)
+                          (string=? str "")
+                          (add-history str))
+                      str))))
 
 ;;; We only create one readline port.  There's no point in having
 ;;; more, since they would all share the tty and history ---
@@ -215,6 +195,7 @@
              (lambda (prompt)
                (dynamic-wind
                    (lambda ()
+                      (set-line-port-continuation?! (readline-port) #f)
                      (set-readline-prompt! prompt "... ")
                      (set-readline-read-hook! read-hook))
                    (lambda () (read))

Diff finished at Sat Mar  3 10:25:49


;;; line-port.scm -- making a port from a line-buffer reader

(define-module (ice-9 line-port)
  #:export (make-line-port
            set-line-port-continuation?!))

(define line-port-continuation? (make-object-property))

(define (set-line-port-continuation?! line-port val)
  (set! (line-port-continuation? line-port) val))

(define (make-line-port reader)
  (let ((read-string "")
        (string-index -1))
    (letrec ((get-character
              (lambda ()
                (cond 
                 ((eof-object? read-string)
                  read-string)
                 ((>= string-index (string-length read-string))
                  (begin
                    (set! string-index -1)
                    #\nl))
                 ((= string-index -1)
                  (begin
                    (set! read-string (reader (line-port-continuation? port)))
                    (set! string-index 0)
                    (if (not (eof-object? read-string))
                        (get-character)
                        read-string)))
                 (else
                  (let ((res (string-ref read-string string-index)))
                    (set! string-index (+ 1 string-index))
                    (set! (line-port-continuation? port) #t)
                    res)))))
             (port #f))
      (set! port (make-soft-port (vector #f #f #f get-character #f) "r"))
      (set! (line-port-continuation? port) #f)
      port)))

;;; line-port.scm ends here

;;; line-port-example.scm -- example of using (ice-9 line-port)

(define-module (ice-9 line-port-example)
  #:use-module (ice-9 line-port)
  #:use-module (ice-9 rdelim)
  #:export (make-example-line-port))

(define (make-example-line-port)
  (make-line-port (lambda (continuation?)
                    (display (if continuation?
                                 "... "
                                 "New read: "))
                    (force-output)
                    (read-line))))

;;; line-port-example.scm ends here



reply via email to

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