Readline: Terminal Interaction
The "readline" collection (not to be confused with Racket’s read-line function) provides glue for using GNU’s Readline library with the Racket read-eval-print-loop.
1 Normal Use of Readline
(require readline) |
(require readline/rep-start) |
The readline library installs a Readline-based input port (whose name is 'readline-input) and hooks the prompt-and-read part of Racket’s read-eval-print-loop to interact with it.
You can start Racket with
racket -il readline
or evaluate
in the Racket read-eval-print-loop to load Readline manually. You can also put (require readline) in your "~/.racketrc", so that Racket automatically loads Readline support in interactive mode.
If you want to enable Readline support only sometimes—
(when (regexp-match? #rx"xterm" |
(getenv "TERM")) |
(dynamic-require 'readline #f)) |
The readline library automatically checks whether the current input port is a terminal, as determined by terminal-port?, and it installs Readline only to replace terminal ports. The readline/rep-start module installs Readline without a terminal check.
By default, Readline’s completion is set to use the visible bindings in the current namespace. This is far from ideal, but it’s better than Readline’s default filename completion which is rarely useful. In addition, the Readline history is stored across invocations in Racket’s preferences file, assuming that Racket exits normally.
The readline library adjusts read-eval-print-loop by setting the prompt read handler as determined by current-prompt-read. The call to the read interaction handler (as determined by current-read-interaction) is parameterized to set readline-prompt, so that a prompt will be printed when reading starts. To compensate for the prompt printed via readline-prompt, when the interaction input port’s name (as produced by function in the current-get-interaction-input-port parameter) is 'readline-input, the prompt read handler skips printing a prompt; otherwise, it displays a prompt as determined by current-prompt.
For more fine-grained control, such as conditionally loading Readline based on an environment variable, edit "~/.racketrc" manually.
2 Interacting with the Readline-Enabled Input Port
The reading facility that the new input port provides can be customized with the following parameters.
(current-prompt) → bytes? |
(current-prompt bstr) → void? |
bstr : bytes? |
(keep-duplicates) → (one-of/c #f 'unconsecutive #t) |
(keep-duplicates keep?) → void? |
keep? : (one-of/c #f 'unconsecutive #t) |
(keep-blanks) → boolean? |
(keep-blanks keep?) → void? |
keep? : any/c |
(readline-prompt) → (or/c false/c bytes? (one-of/c 'space)) |
(readline-prompt status) → void? |
status : (or/c false/c bytes? (one-of/c 'space)) |
(parameterize ([readline-prompt some-byte-string]) |
...code-that-reads...) |
This expression makes the first call to Readline use the prompt, and subsequent calls will use an all-spaces prompt of the same length (for example, when you’re reading an S-expression). The normal value of readline-prompt is #f for an empty prompt (and spaces after the prompt is used, which is why you should use parameterize to restore it to #f).
A proper solution would be to install a custom output port, too, which keeps track of text that is displayed without a trailing newline. As a cheaper solution, if line-counting is enabled for the terminal’s output-port, then a newline is printed before reading if the column is not 0. (The readline library enables line-counting for the output port.)
Warning: The Readline library uses the output port directly. You should not use it when current-input-port has been modified, or when it was not a terminal port when Racket was started (eg, when reading input from a pipe). Expect some problems if you ignore this warning (not too bad, mostly problems with detecting an EOF).
3 Direct Bindings for Readline Hackers
(require readline/readline) |
(readline-bytes prompt) → bytes? |
prompt : bytes? |
(add-history str) → void? |
str : string? |
(add-history-bytes str) → void? |
str : bytes? |
(history-get idx) → string? |
idx : integer? |
(history-delete idx) → string? |
idx : integer? |
(set-completion-function! proc [type]) → void? | ||||||||
| ||||||||
type : (one-of/c _string _bytes) = _string |
4 License Issues
GNU’s Readline library is covered by the GPL, and that applies to code that links with it. Racket is licensed with the LGPL, so the Readline code is not used by default; you should explicitly enable it if you want to. Also, be aware that if you write code that uses this library, it will make your code link to the Readline library when invoked, with the usual GPL implications.