Readline: Terminal Interaction

Similar documents
Readline: Terminal Interaction

Browser: Simple HTML Rendering

Syntax: Meta-Programming Helpers

Graphics: Legacy Library

Embedded GUI: Widgets Within Editors

XREPL: extended REPL

FrTime: A Language for Reactive Programs

File: PLT File Format Libraries

Dynext: Running a C Compiler/Linker

FrTime: A Language for Reactive Programs

MrLib: Extra GUI Libraries

Programming Languages: Application and Interpretation

File: Racket File Format Libraries

File: Racket File and Format Libraries

Essentials of Programming Languages Language

File: Racket File Format Libraries

XML: Parsing and Writing

JSON. Version 6.6. Eli Barzilay and Dave Herman. July 22, 2016

File: Racket File Format Libraries

XML: Parsing and Writing

The Typed Racket Reference

Parser Tools: lex and yacc-style Parsing

Programming Languages: Application and Interpretation

Essentials of Programming Languages Language

Distributed Places. Version 6.3. Kevin Tew. November 20, (require racket/place/distributed)

Net: Networking Libraries

2D Syntax. Version October 30, 2017

Typed Racket: Racket with Static Types

Syntax Color: Utilities

The Typed Racket Guide

/chapter/1 In the beginning there was request?

Pebbles Kernel Specification September 26, 2004

Implementing HtDP Teachpacks, Libraries, and Customized Teaching Languages

Racket: Modules, Contracts, Languages

Unstable. Version 5.1. February 14, This manual documents some of the libraries available in the unstable collection.

More: Systems Programming with Racket

Macro Debugger: Inspecting Macro Expansion

Scheme: Strings Scheme: I/O

Slideshow: Figure and Presentation Tools

Parser Tools: lex and yacc-style Parsing

Emacs manual:

Section 5.5: Text Menu Input from Character Strings

Introduction to C# Applications

COP4020 Programming Assignment 1 - Spring 2011

Web Applications in Racket

c-lambda: C FFI via raco ctool

The Design Recipe Fall 2017

CSC209 Review. Yeah! We made it!

CSC209: Software tools. Unix files and directories permissions utilities/commands Shell programming quoting wild cards files

CSC209: Software tools. Unix files and directories permissions utilities/commands Shell programming quoting wild cards files. Compiler vs.

Introduction Variables Helper commands Control Flow Constructs Basic Plumbing. Bash Scripting. Alessandro Barenghi

The Racket Foreign Interface

SRFIs: Libraries. Version August 10, 2015

The Racket Drawing Toolkit

DrRacket Plugins. Version 6.7. Robert Bruce Findler. October 26, package: drracket

Simple Component Writer's Guide

Stand-alone GNU Info. for version 6.5, 21 May Brian J. Fox and Texinfo maintainers

How to Design Programs

LECTURE 16. Functional Programming

DB: Database Connectivity

Introduction to Typed Racket. The plan: Racket Crash Course Typed Racket and PL Racket Differences with the text Some PL Racket Examples

Bashed One Too Many Times. Features of the Bash Shell St. Louis Unix Users Group Jeff Muse, Jan 14, 2009

The Design Recipe Fall 2018

GNU Info. for version 4.2, 23 March Brian J. Fox

Pebbles Kernel Specification September 20, 2017

3.Constructors and Destructors. Develop cpp program to implement constructor and destructor.

Version 5.0. Alexander Friedman and Jamie Raymond. June 6, 2010

Data: Data Structures

CS251 Programming Languages Handout # 47 Prof. Lyn Turbak May 22, 2005 Wellesley College. Scheme

CPSC/ECE 3220 Fall 2017 Exam Give the definition (note: not the roles) for an operating system as stated in the textbook. (2 pts.

Scheme in Scheme: The Metacircular Evaluator Eval and Apply

20.5. urllib Open arbitrary resources by URL

Framework: Racket GUI Application Framework

PIC 10A Flow control. Ernest Ryu UCLA Mathematics

Libgdb. Version 0.3 Oct Thomas Lord

Mozart System Limitations for Version (draft)

GOSSIP User Manual gossip-sim

Mastering Linux by Paul S. Wang Appendix: The emacs Editor

Data: Data Structures

ROTCTL(1) Rotator Control Program ROTCTL(1)

Emacs Lisp Introduction

The list abstract data type defined a number of operations that all list-like objects ought to implement:

CS 211 Programming Practicum Fall 2018

Macro Debugger. Version 5.1. Ryan Culpepper. February 14, 2011

Procedures. EOPL3: Section 3.3 PROC and App B: SLLGEN

entity tomove over backward forward character C-b C-f word M-b M-f line C-p C-n go to line beginning (or end) C-a C-e sentence M-a M-e paragraph M- M-

DB: Database Connectivity

The pkgsrc wrapper framework

Homework 3: Recursion Due: 11:59 PM, Sep 25, 2018

Kent Dybvig, Will Clinger, Matthew Flatt, Mike Sperber, and Anton van Straaten

Python Working with files. May 4, 2017

Chapter 1 Summary. Chapter 2 Summary. end of a string, in which case the string can span multiple lines.

File Commands. Objectives

Definition Matching (10 Points)

Configuring CSM Scripts

We first learn one useful option of gcc. Copy the following C source file to your

A brief introduction to C programming for Java programmers

Marthon User Guide. Page 1 Copyright The Marathon developers. All rights reserved.

Normal Order (Lazy) Evaluation SICP. Applicative Order vs. Normal (Lazy) Order. Applicative vs. Normal? How can we implement lazy evaluation?

Unix System Programming - Chapter 2, part a

Transcription:

Readline: Terminal Interaction Version 6.10.1 September 12, 2017 The "readline" collection (not to be confused with Racket s read-line function) provides glue for using the Editline library or GNU s Readline library with the Racket readeval-print-loop. Due to licensing issues, the Readline collection is by default backed by the Editline library. To switch to GNU s Readline library, either install the "readline-gpl" package, or set the PLT_READLINE_LIB environment variable to the library, which configures the readline collection to use Readline. 1

1 Normal Use of Readline (require readline) package: readline-lib (require readline/rep-start) The readline library installs a Readline-based input port (whose name is 'readlineinput) 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 (require readline) 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 such as only when you use an xterm, and not when you use an Emacs shell then you can use dynamic-require, as in the following example: (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. 2

(install-readline!) Ñ void? Adds (require readline/rep) to the result of (find-system-path 'init-file), which is " /.racketrc" on Unix. Consequently, Readline will be loaded whenever Racket is started in interactive mode. The declaration is added only if it is not already present, as determined by reading and checking all top-level expressions in the file. For more fine-grained control, such as conditionally loading Readline based on an environment variable, edit " /.racketrc" manually. 3

2 Interacting with the Readline-Enabled Input Port (require readline/pread) package: readline-lib The readline/pread library provides customization, and support for prompt-reading after readline installs the new 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? A parameter that determines the prompt that is used, as a byte string. Defaults to #"> ". (max-history) Ñ exact-nonnegative-integer? (max-history n) Ñ void? n : exact-nonnegative-integer? A parameter that determines the number of history entries to save, defaults to 100. (keep-duplicates) Ñ (one-of/c #f 'unconsecutive #t) (keep-duplicates keep?) Ñ void? keep? : (one-of/c #f 'unconsecutive #t) A parameter. If #f (the default), then when a line is equal to a previous one, the previous one is removed. If it set to 'unconsecutive then this happens only for an line that duplicates the previous one, and if it is #f then all duplicates are kept. (keep-blanks) Ñ boolean? (keep-blanks keep?) Ñ void? keep? : any/c A parameter. If #f (the default), blank input lines are not kept in history. (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)) The new input port that you get when you require readline is a custom port that uses Readline for all inputs. The problem is when you want to display a prompt and then read some input, Readline will get confused if it is not used when the cursor is at the beginning of the line (which is why it has a prompt argument.) To use this prompt: 4

(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). 5

3 Direct Bindings for Readline Hackers (require readline/readline) package: readline-lib (readline prompt) Ñ string? prompt : string? Prints the given prompt string and reads a line. (readline-bytes prompt) Ñ bytes? prompt : bytes? Like readline, but using raw byte-strings for the prompt and returning a byte string. (add-history str) Ñ void? str : string? Adds the given string to the Readline history, which is accessible to the user via the up-arrow key. (add-history-bytes str) Ñ void? str : bytes? Adds the given byte string to the Readline history, which is accessible to the user via the up-arrow key. (history-length) Ñ exact-nonnegative-integer? Returns the length of the history list. (history-get idx) Ñ string? idx : integer? Returns the history string at the idx position. idx can be negative, which will make it count from the last (i.e, -1 returns the last item, -2 returns the second-to-last, etc.) (history-delete idx) Ñ string? idx : integer? Deletes the history string at the idx position. The position is specified in the same way as the argument for history-get. (set-completion-function! proc [type]) Ñ void? proc : ((or/c string? bytes?). ->. (listof (or/c string? bytes?))) type : (one-of/c _string _bytes) = _string 6

Sets Readline s rl_completion_entry_function to proc. The type argument, whose possible values are from ffi/unsafe, determines the type of value supplied to the proc. (readline-newline) Ñ void? Sets the cursor to the start of a new line. (readline-redisplay) Ñ void? Forces a redisplay of the Readline prompt and current user input. The readline-redisplay function can be used together with readline-newline to prevent a background thread from cluttering up the user input by interleaving its output. For example, an unsafe wrapper function for the thread s output might look like the following: (define (with-thread-safe-output output-thunk) (dynamic-wind (lambda () (start-atomic) (readline-newline)) output-thunk (lambda () (readline-redisplay) (end-atomic)))) 7