Prompting - MIT/GNU Scheme 9.2
Description: MIT/GNU Scheme 9.2

Next: Port Primitives, Previous: Custom Output, Up: Input/Output

### 14.8 Prompting

This section describes procedures that prompt the user for input. Why should the programmer use these procedures when it is possible to do prompting using ordinary input and output procedures? One reason is that the prompting procedures are more succinct. However, a second and better reason is that the prompting procedures can be separately customized for each user interface, providing more natural interaction. The interfaces for Edwin and for GNU Emacs have already been customized in this fashion; because Edwin and Emacs are very similar editors, their customizations provide very similar behavior.

Each of these procedure accepts an optional argument called port, which if given must be an I/O port. If not given, this port defaults to the value of `(interaction-i/o-port)`; this is initially the console I/O port.

— procedure: prompt-for-command-expression prompt [port [environment]]

> Prompts the user for an expression that is to be executed as a command. This is the procedure called by the REP loop to read the user's expressions.

> If prompt is a string, it is used verbatim as the prompt string. Otherwise, it must be a pair whose car is the symbol ``standard`' and whose cdr is a string; in this case the prompt string is formed by prepending to the string the current REP loop “level number” and a space. Also, a space is appended to the string, unless it already ends in a space or is an empty string.

> If environment is given, it is passed as the second argument to `read`.

> The default behavior of this procedure is to print a fresh line, a newline, and the prompt string; flush the output buffer; then read an object and return it.

> Under Edwin and Emacs, before the object is read, the interaction buffer is put into a mode that allows expressions to be edited and submitted for input using specific editor commands. The first expression that is submitted is returned as the value of this procedure.

— procedure: prompt-for-command-char prompt [port]

> Prompts the user for a single character that is to be executed as a command; the returned character is guaranteed to satisfy `char-graphic?`. If at all possible, the character is read from the user interface using a mode that reads the character as a single keystroke; in other words, it should not be necessary for the user to follow the character with a carriage return or something similar.

> This is the procedure called by `debug` and `where` to read the user's commands.

> If prompt is a string, it is used verbatim as the prompt string. Otherwise, it must be a pair whose car is `standard` and whose cdr is a string; in this case the prompt string is formed by prepending to the string the current REP loop “level number” and a space. Also, a space is appended to the string, unless it already ends in a space or is an empty string.

> The default behavior of this procedure is to print a fresh line, a newline, and the prompt string; flush the output buffer; read a character in raw mode, echo that character, and return it.

> Under Edwin and Emacs, instead of reading a character, the interaction buffer is put into a mode in which graphic characters submit themselves as input. After this mode change, the first such character submitted is returned as the value of this procedure.

— procedure: prompt-for-expression prompt [port [environment]]

> Prompts the user for an expression.

> The prompt string is formed by appending a colon and a space to prompt, unless prompt already ends in a space or is the null string.

> If environment is given, it is passed as the second argument to `read`.

> The default behavior of this procedure is to print a fresh line, a newline, and the prompt string; flush the output buffer; then read an object and return it.

> Under Edwin and Emacs, the expression is read in the minibuffer.

— procedure: prompt-for-evaluated-expression prompt [environment [port]]

> Prompts the user for an evaluated expression. Calls `prompt-for-expression` to read an expression, then evaluates the expression using environment; if environment is not given, the REP loop environment is used.

— procedure: prompt-for-confirmation prompt [port]

> Prompts the user for confirmation. The result yielded by this procedure is a boolean.

> The prompt string is formed by appending the string `" (y or n)? "` to prompt, unless prompt already ends in a space or is the null string.

> The default behavior of this procedure is to print a fresh line, a newline, and the prompt string; flush the output buffer; then read a character in raw mode. If the character is `#\y`, `#\Y`, or `#\space`, the procedure returns `#t`; If the character is `#\n`, `#\N`, or `#\rubout`, the procedure returns `#f`. Otherwise the prompt is repeated.

> Under Edwin or Emacs, the confirmation is read in the minibuffer.