repl-reader accepts optional "read" argument
[bpt/guile.git] / guile-readline / ice-9 / readline.scm
index a2d6359..4766e61 100644 (file)
@@ -1,10 +1,10 @@
 ;;;; readline.scm --- support functions for command-line editing
 ;;;;
-;;;;   Copyright (C) 1997, 1999, 2000, 2001, 2002, 2006 Free Software Foundation, Inc.
+;;;;   Copyright (C) 1997, 1999, 2000, 2001, 2002, 2006, 2009 Free Software Foundation, Inc.
 ;;;; 
 ;;;; This program is free software; you can redistribute it and/or modify
 ;;;; it under the terms of the GNU General Public License as published by
-;;;; the Free Software Foundation; either version 2, or (at your option)
+;;;; the Free Software Foundation; either version 3, or (at your option)
 ;;;; any later version.
 ;;;; 
 ;;;; This program is distributed in the hope that it will be useful,
 \f
 
 (define-module (ice-9 readline)
-  :use-module (ice-9 session)
-  :use-module (ice-9 regex)
-  :use-module (ice-9 buffered-input)
-  :no-backtrace)
+  #:use-module (ice-9 session)
+  #:use-module (ice-9 regex)
+  #:use-module (ice-9 buffered-input)
+  #:no-backtrace
+  #:export (filename-completion-function
+           add-history
+           read-history
+           write-history
+           clear-history))
 
 \f
 
@@ -35,7 +40,7 @@
 ;;; but only when it isn't already present.
 
 (if (not (provided? 'readline))
-    (load-extension "libguilereadline-v-17" "scm_init_readline"))
+    (load-extension "libguilereadline-v-18" "scm_init_readline"))
 
 (if (not (provided? 'readline))
     (scm-error 'misc-error
@@ -68,8 +73,8 @@
 ;;; Dirk:FIXME:: If the-readline-port, input-port or output-port are closed,
 ;;; guile will enter an endless loop or crash.
 
-(define prompt "")
-(define prompt2 "")
+(define new-input-prompt "")
+(define continuation-prompt "")
 (define input-port (current-input-port))
 (define output-port (current-output-port))
 (define read-hook #f)
@@ -77,8 +82,8 @@
 (define (make-readline-port)
   (make-line-buffered-input-port (lambda (continuation?)
                                    (let* ((prompt (if continuation?
-                                                      prompt2
-                                                      prompt))
+                                                      continuation-prompt
+                                                      new-input-prompt))
                                           (str (%readline (if (string? prompt)
                                                               prompt
                                                               (prompt))
 ;;; %readline is the low-level readline procedure.
 
 (define-public (readline . args)
-  (let ((prompt prompt)
+  (let ((prompt new-input-prompt)
        (inp input-port))
     (cond ((not (null? args))
           (set! prompt (car args))
           args)))
 
 (define-public (set-readline-prompt! p . rest)
-  (set! prompt p)
+  (set! new-input-prompt p)
   (if (not (null? rest))
-      (set! prompt2 (car rest))))
+      (set! continuation-prompt (car rest))))
 
 (define-public (set-readline-input-port! p)
   (cond ((or (not (file-port? p)) (not (input-port? p)))
 (define-public (set-readline-read-hook! h)
   (set! read-hook h))
 
+(define-public apropos-completion-function
+  (let ((completions '()))
+    (lambda (text cont?)
+      (if (not cont?)
+          (set! completions
+                (map symbol->string
+                     (apropos-internal
+                      (string-append "^" (regexp-quote text))))))
+      (if (null? completions)
+          #f
+          (let ((retval (car completions)))
+            (begin (set! completions (cdr completions))
+                   retval))))))
+
 (if (provided? 'regex)
-    (begin
-      (define-public apropos-completion-function
-       (let ((completions '()))
-         (lambda (text cont?)
-           (if (not cont?)
-               (set! completions
-                     (map symbol->string
-                          (apropos-internal
-                           (string-append "^" (regexp-quote text))))))
-           (if (null? completions)
-               #f
-               (let ((retval (car completions)))
-                 (begin (set! completions (cdr completions))
-                        retval))))))
-
-      (set! *readline-completion-function* apropos-completion-function)
-      ))
+    (set! *readline-completion-function* apropos-completion-function))
 
 (define-public (with-readline-completion-function completer thunk)
   "With @var{completer} as readline completion function, call @var{thunk}."
           (not (let ((guile-user-module (resolve-module '(guile-user))))
                  (and (module-defined? guile-user-module 'use-emacs-interface)
                       (module-ref guile-user-module 'use-emacs-interface)))))
-      (let ((read-hook (lambda () (run-hook before-read-hook))))
+      (let ((repl-read-hook (lambda () (run-hook before-read-hook))))
        (set-current-input-port (readline-port))
        (set! repl-reader
-             (lambda (prompt)
-               (dynamic-wind
-                   (lambda ()
-                      (set-buffered-input-continuation?! (readline-port) #f)
-                     (set-readline-prompt! prompt "... ")
-                     (set-readline-read-hook! read-hook))
-                   (lambda () (read))
-                   (lambda ()
-                     (set-readline-prompt! "" "")
-                     (set-readline-read-hook! #f)))))
+             (lambda (repl-prompt . reader)
+               (let ((outer-new-input-prompt new-input-prompt)
+                     (outer-continuation-prompt continuation-prompt)
+                     (outer-read-hook read-hook))
+                 (dynamic-wind
+                     (lambda ()
+                       (set-buffered-input-continuation?! (readline-port) #f)
+                       (set-readline-prompt! repl-prompt "... ")
+                       (set-readline-read-hook! repl-read-hook))
+                     (lambda () ((or (and (pair? reader) (car reader))
+                                      (fluid-ref current-reader)
+                                      read)))
+                     (lambda ()
+                       (set-readline-prompt! outer-new-input-prompt outer-continuation-prompt)
+                       (set-readline-read-hook! outer-read-hook))))))
        (set! (using-readline?) #t))))
 
 (define-public (make-completion-function strings)