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
prompt-and-read part of MzScheme's read-eval-print loop.
You can put the following in your ~/.mzschemerc so that MzScheme starts with
readline support on xterms:
You can put the following in your ~/.mzschemerc so that MzScheme
starts with readline support on xterms:
(when (equal? "xterm" (getenv "TERM"))
(dynamic-require '(lib "rep.ss" "readline") #f))
@ -28,11 +28,20 @@ The _readline.ss_ library provides two functions:
> (readline prompt-string)
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)
adds the given string to the readline history, which is accessible
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
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
(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
(define libtermcap (with-handlers ([exn:fail? void]) (ffi-lib "libtermcap")))
@ -10,17 +12,26 @@
(define readline
(get-ffi-obj "readline" libreadline (_fun _string -> _string/eof)))
(define readline-bytes
(get-ffi-obj "readline" libreadline (_fun _bytes -> _bytes/eof)))
(define add-history
(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
;; returns the completions for a given string. (should clean up bytes/string)
(define (set-completion-function! func)
(if func
(set-ffi-obj! "rl_completion_entry_function" libreadline
(_fun _string _int -> _pointer)
(completion-function func))
(set-ffi-obj! "rl_completion_entry_function" libreadline _pointer #f)))
(define set-completion-function!
(case-lambda
[(func) (set-completion-function! _string)]
[(func type)
(if func
(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)
(let ([cur '()])
(define (complete str state)
@ -31,10 +42,16 @@
(set! cur (cdr cur))))))
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
(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
(require "mzrl.ss")
(provide readline add-history set-completion-function!))
(provide (all-from "mzrl.ss")))