Prompt on read

Per Bothner per@bothner.com
Wed Nov 22 21:27:00 GMT 2017


On 11/22/2017 12:31 AM, Helmut Eller wrote:
> On Tue, Nov 21 2017, Per Bothner wrote:
> 
>>> Despite that the prompt in line two is confusing, the line number in the
>>> third prompt seems wrong.  I would expect that read works basically like
>>> read-line,
>>
>> Yes and no.  read reads an S-expression, and there may be a many-to-many
>> relationship between S-expressions and lines.  A prompt is printed before
>> each input line -
> 
> If a prompt is printed before each input line, then why does calling
> read-line not print a prompt?
> 
>> however note it is possible for a procedure to change the
>> prompt, for example to the empty string.

(After having to remind myself how it all works ...)

The interactive port does print a prompt at the beginning of line,
if there is a prompt specified.  However, the default prompt procedure
depends on the "read state" (i.e. input-port-read-state).  If
the read-state is #\newline (which is the default) the default prompt
string is empty (or actually null).  The read procedures sets the read-state
as it parses the S-expression.

Now consider:

$ bin/kawa console:use-jline=no
#|kawa:1|# (define r1 (read))
#|kawa:2|# (4 5)
#|kawa:2|# r1
(4 5)
#|kawa:3|#

Why is the line 2 prompt repeated?  I'm still trying to figure that out,
but it appears related to the fact that read doesn't read the entire line,
only the "(4 5)".  For example read followed by read-line to read
the remainder of the line works as expected.

#|kawa:3|# (define r2 (read)) (define l2 (read-line))
#|kawa:4|# (5 6)
#|kawa:5|# r2
(5 6)
#|kawa:6|# (write l2)
""
#|kawa:7|#

There are also some weirdness with console:use-jline=no (raw console read)
vs console:use-jline=yes (read using jline input library).

> You mean something like (fluid-let ((input-prompt1 "")) ...)  or
> something else?  Hmm, that would work for me.

That could work.
> 
>> FWIW I can't come up with any valid use-case for explicitly calling
>> read on an interactive port.  The problem is error recovery.
>> Enlighten me.
> 
> Uhm, a REPL that wants to print its own prompt?

In which case you'd override the default prompt.

Or something like
> 
> (format #t "Please enter a filename: ")
> (open-output-file (read))

Here read-line makes sense but read is clearly wrong.
-- 
	--Per Bothner
per@bothner.com   http://per.bothner.com/



More information about the Kawa mailing list