Skip to content

fix 248 with a minimal impact#255

Open
paolopas wants to merge 3 commits intoantirez:masterfrom
paolopas:escappen
Open

fix 248 with a minimal impact#255
paolopas wants to merge 3 commits intoantirez:masterfrom
paolopas:escappen

Conversation

@paolopas
Copy link
Copy Markdown

@paolopas paolopas commented Apr 11, 2026

I believe it is impossible to implement a completion loop without running into the problem described in #248.

But perhaps no one has noticed another problem related to the management of the ESC key which in my opinion is strictly linked to the #248 (well described in #129 about a decade ago.) That is, after you press the ESC key, linenoise throws away the next two characters you press.

These problems arises because linenoise cannot distinguish (and does not even try to distinguish) the ESC key press from other keys that produce an escape sequence.

I saw the solution proposed in #230 but I don't like the fact that any character other than a TAB overwrites the buffer, as if the only way to escape a completion loop was to accept the current completion.

I think it's fair to be able to abort the loop and return to the previous buffer by simply pressing ESC. But I'm sure we all hit the arrow keys while iterating with TAB.

So in my opinion the simplest solution is through:

  1. let the completeLine do its job and exit the loop as soon as it sees an ESC, but leave the responsibility of handling it to its caller, which is exactly how it was intended, and
  2. In linenoiseEditFeed, don't make too many assumptions that an escape key necessarily means an escape sequence, and so be careful not to throw away keys that aren't part of an escape sequence.

It doesn't cover 100% of cases, but it has the minimum impact on the current implementation (only 11 lines of code: 1 deleted + 10 added are actually needed), also thanks to a nice goto, which I put in to avoid making dangerous jumps from inside a switch (they've already invented coroutines.)
Of course the goto is a provocation, it could be done with do ... while, but the implementation with goto is certainly more readable, as well as being shorter.

I didn't even consider using ungetc, especially since to really handle all cases (instead of just one pc character) you should use a queue and put in it all the extracted characters that need to be reconsidered.

Note

For those who want to try, here is a version of the file with the changes (to replace the original one)
linenoise_2026-04-12.zip

paolopas added a commit to paolopas/b2 that referenced this pull request Apr 12, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant