fish-shell/fish-shell — pull request #1636 by kballard (2014-08-24 09:44:02+00:00)
Title: Add a new `commandline` flag --unread

Add a new `--unread` flag to `commandline` for use in injecting
arbitrary key presses into the reader.

Silly example that replaces `!!` with `?`:

```fish
function handle_bang_bang
switch (commandline -c)[-1]
case \*!
# reader injections are in reverse order
commandline -u \?
commandline -f self-insert backward-delete-char
case \*
commandline -u !
commandline -f self-insert
end
end

bind ! handle_bang_bang
```

Note: this can cause an infinite loop in the reader if you do something stupid like

```fish
bind q 'commandline -u q'
```

This causes the `q` key press to re-inject `q`. I don't know of any good way to detect this and stop the loop. Among other reasons, a key binding might be using external state (e.g. global variables) to decide how to respond, so a keypress re-injecting itself without e.g. `self-insert` first does not necessarily mean there's an infinite loop.

More importantly, pressing `^C` doesn't seem to do anything when it's in this loop. I'd be happy if `^C` stopped the infinite loop, but I don't know how to do that.