added bytes version of everything

svn: r4451
This commit is contained in:
Eli Barzilay 2006-09-28 03:50:38 +00:00
parent 7de449312c
commit e2af375047
3 changed files with 40 additions and 14 deletions

View File

@ -10,8 +10,8 @@ Normal use of readline
The _rep.ss_ library installs a readline-based function for the The _rep.ss_ library installs a readline-based function for the
prompt-and-read part of MzScheme's read-eval-print loop. prompt-and-read part of MzScheme's read-eval-print loop.
You can put the following in your ~/.mzschemerc so that MzScheme starts with You can put the following in your ~/.mzschemerc so that MzScheme
readline support on xterms: starts with readline support on xterms:
(when (equal? "xterm" (getenv "TERM")) (when (equal? "xterm" (getenv "TERM"))
(dynamic-require '(lib "rep.ss" "readline") #f)) (dynamic-require '(lib "rep.ss" "readline") #f))
@ -28,11 +28,20 @@ The _readline.ss_ library provides two functions:
> (readline prompt-string) > (readline prompt-string)
prints the given prompt string and reads a line prints the given prompt string and reads a line
> (readline-bytes prompt-bytes)
same as above, but using raw byte-strings for the prompt and
returning a byte string
> (add-history s) > (add-history s)
adds the given string to the readline history, which is accessible adds the given string to the readline history, which is accessible
to the user via the up-arrow key to the user via the up-arrow key
> (set-completion-function! proc) > (add-history-bytes s)
adds the given byte string to the readline history, which is
accessible to the user via the up-arrow key
> (set-completion-function! proc [type])
sets readline's `rl_completion_entry_function' function according to sets readline's `rl_completion_entry_function' function according to
proc, which is expected to be a `string -> (list-of string)' proc, which is expected to be a `string -> (list-of string)'
procedure procedure; the `type' argument defaults to `_string' but you can use
it with `_bytes' instead

View File

@ -1,7 +1,9 @@
(module mzrl mzscheme (module mzrl mzscheme
(require (lib "foreign.ss")) (unsafe!) (require (lib "foreign.ss")) (unsafe!)
(provide readline add-history set-completion-function!) (provide readline readline-bytes
add-history add-history-bytes
set-completion-function!)
;; libtermcap maybe needed ;; libtermcap maybe needed
(define libtermcap (with-handlers ([exn:fail? void]) (ffi-lib "libtermcap"))) (define libtermcap (with-handlers ([exn:fail? void]) (ffi-lib "libtermcap")))
@ -10,17 +12,26 @@
(define readline (define readline
(get-ffi-obj "readline" libreadline (_fun _string -> _string/eof))) (get-ffi-obj "readline" libreadline (_fun _string -> _string/eof)))
(define readline-bytes
(get-ffi-obj "readline" libreadline (_fun _bytes -> _bytes/eof)))
(define add-history (define add-history
(get-ffi-obj "add_history" libreadline (_fun _string -> _void))) (get-ffi-obj "add_history" libreadline (_fun _string -> _void)))
(define add-history-bytes
(get-ffi-obj "add_history" libreadline (_fun _bytes -> _void)))
;; Simple completion: use this with a (string -> list-of string) function that ;; Simple completion: use this with a (string -> list-of string) function that
;; returns the completions for a given string. (should clean up bytes/string) ;; returns the completions for a given string. (should clean up bytes/string)
(define (set-completion-function! func) (define set-completion-function!
(if func (case-lambda
(set-ffi-obj! "rl_completion_entry_function" libreadline [(func) (set-completion-function! _string)]
(_fun _string _int -> _pointer) [(func type)
(completion-function func)) (if func
(set-ffi-obj! "rl_completion_entry_function" libreadline _pointer #f))) (set-ffi-obj! "rl_completion_entry_function" libreadline
(_fun type _int -> _pointer)
(completion-function func))
(set-ffi-obj! "rl_completion_entry_function" libreadline _pointer #f))]))
(define (completion-function func) (define (completion-function func)
(let ([cur '()]) (let ([cur '()])
(define (complete str state) (define (complete str state)
@ -31,10 +42,16 @@
(set! cur (cdr cur)))))) (set! cur (cdr cur))))))
complete)) complete))
(set-ffi-obj! "rl_readline_name" libreadline _string "mzscheme") (set-ffi-obj! "rl_readline_name" libreadline _bytes #"mzscheme")
;; need to capture the real input port below
(define real-input-port (current-input-port))
(unless (eq? 'stdin (object-name (current-input-port)))
(fprintf (current-output-port)
"mzrl warning: could not capture the real input port"))
;; make it possible to run Scheme threads while waiting for input ;; make it possible to run Scheme threads while waiting for input
(set-ffi-obj! "rl_event_hook" libreadline (_fun -> _int) (set-ffi-obj! "rl_event_hook" libreadline (_fun -> _int)
(lambda () (sync/enable-break (current-input-port)) 0)) (lambda () (sync/enable-break real-input-port) 0))
) )

View File

@ -1,4 +1,4 @@
(module readline mzscheme (module readline mzscheme
(require "mzrl.ss") (require "mzrl.ss")
(provide readline add-history set-completion-function!)) (provide (all-from "mzrl.ss")))