Commit Graph

18793 Commits

Author SHA1 Message Date
Johannes Altmanninger
00432df420 Trigger abbreviations after inserting process separators
On

    a;

we don't expand the abbreviation because the cursor is right of semicolon,
not on the command token. Fix this by making sure that we call expand-abbr
with the cursor on the semicolon which is the end of the command token.
(Now that our bind command execution order is less surprising, this is doable.)

This means that we need to fix the cursor after successfully expanding
an abbreviation. Do this by setting the position explicitly even when no
--set-position is in effect.

An earlier version of this patch used

    bind space self-insert backward-char expand-abbr or forward-char

The problem with that (as a failing test shows) was that given "abbr m
myabbr", after typing "m space ctrl-z", the cursor would be after the "m",
not after the space.  The second space removes the space, not changing the
cursor position, which is weird.  I initially tried to fix this by adding
a hack to the undo group logic, to always restore the cursor position from
when begin-undo-group was used.

    bind space self-insert begin-undo-group backward-char expand-abbr end-undo-group or forward-char

However this made test_torn_escapes.py fail for mysterious reasons.
I believe this is because that test registers and triggers a SIGUSR1 handler;
since the signal handler will rearrange char events, that probably messes
with the undo group guards.

I resorted to adding a tailor-made readline cmd. We could probably remove
it and give the new behavior to expand-abbr, not sure.

Fixes #9730
2024-04-13 20:11:11 +02:00
Johannes Altmanninger
0c5deacedc Add test for updating the commandline state on background job exit
This is the regression test for 8386088b3 (Update commandline state changes
eagerly as well, 2024-04-11).
2024-04-13 18:24:53 +02:00
Johannes Altmanninger
29dc307111 Insert some completions with quotes instead of backslashes
File names that have lots of spaces look quite ugly when inserted as
completions because every space will have a backslash.

Add an initial heuristic to decide when to use quotes instead of
backslash escapes.

Quote when
1. it's not an autosuggestion
2. we replace the token or insert a fresh one
3. we will add a space at the end

In future we could relax some of these requirements.

Requirement 2 means we don't quote when appending to an existing token.
Need to find a natural behavior here.

Re 3, if the completion adds no space, users will probably want to add more
characters, which looks a bit weird if the token has a trailing quote.
We could relax this requirement for directory completions, so «ls so»
completes to «ls 'some dir with spaces'/».

Closes #5433
2024-04-13 15:34:21 +02:00
Johannes Altmanninger
cacfcf8089 Reuse parse_util_token_extent for completion insertion
We don't need all of its features here but this makes the "completion is
appended" case more similar to the "completion replaces token" case.
2024-04-13 15:33:05 +02:00
Johannes Altmanninger
dcd6c74248 Inline parse_util_get_quote_type()
Need to access the token extent in a following commit.
2024-04-13 15:33:05 +02:00
Johannes Altmanninger
8d88b4d358 Support quoted escaping also when ' or \ is present
Also, if there are more single quotes than double quotes and dollars, use
double quotes for quoting.
2024-04-13 15:33:05 +02:00
Johannes Altmanninger
88d6801720 Don't match new-style bindings against raw sequences
On Konsole, given

    bind escape,i 'echo escape i'
    bind alt-i 'echo alt-i'

pressing alt-i triggers the wrong binding.  This is because we treat "escape
followed by i" as "alt-i". This is to support raw sequences like "\ei"
which are probably meant as "alt-i" -- we match such inputs to both mappings.

This double matching is not necessary for new-style bindings which
unambiguously describe the key presses, so let's activate this sequence
matching only for bindings specified as raw sequences.

Conversely, we currently fail to match an XTerm raw binding for ctrl-enter:

    echo 'XTerm.vt100.formatOtherKeys: 0' | xrdb
    xterm -e fish
    bind \e\[27\;5\;13~ execute

because we decode this to a single char; we match the leading CSI but not
the entire sequence. So this is a raw binding where we accidentally
match full, modified keys. Fix that too (two birds with one stone).
2024-04-13 14:36:11 +02:00
Johannes Altmanninger
6858f1100a Remove redundant raw CSI u bindings 2024-04-13 14:36:11 +02:00
Johannes Altmanninger
1d98846e03 Remove some redundant raw bindings
We already decode these, see parse_csi() and parse_ss3().
2024-04-13 14:36:11 +02:00
Johannes Altmanninger
4f536d6a9b Update commandline state snapshot lazily
I think commit 8386088b3 (Update commandline state changes eagerly as well,
2024-04-11) broke the alt-s binding.

This is because we update the commandline state snapshot (which is consumed
by builtin commandline and others) only at key points.  This seems like a
dubious optimization.  With the new streamlined bind execution semantics,
this doesn't really work anymore; any shell command can run any number of
commands like "commandline -i foo" which should synchronize.

Do the simple thing of calculating the snapshot whenever needed.
2024-04-13 14:36:11 +02:00
Johannes Altmanninger
edb5cb7226 Fix restoring cursor position on redo with edit groups 2024-04-13 14:36:11 +02:00
Johannes Altmanninger
d1a4b4bc73 Fix undo pexpect test
The assertions were satisfied even though we never triggered any undo.
2024-04-13 11:34:36 +02:00
Johannes Altmanninger
50d93cced1 Remove bad assertion
builtin read pushes a reader instance after enabling terminal protocols,
so this doesn't hold.

Fixes #10438
2024-04-12 14:20:45 +02:00
Johannes Altmanninger
1dd901e521 Maintain cursor in history prefix search
The search term highlighting looks looks really bad on the default theme
because the command is highlighted as dark blue and the search term adds
a dark background.  If this new feature motivates us to finally fix this,
that would be great.

Closes #10430
2024-04-12 13:08:52 +02:00
Lia Lenckowski
90cffb18a1 complete brightnessctl flags 2024-04-12 12:53:55 +02:00
Johannes Altmanninger
13b5322bef Disable failing bind_mode_events.py in FreeBSD for now
I'm pretty sure it's just a timing issue.
2024-04-12 12:34:01 +02:00
Johannes Altmanninger
1e858eae35 tests: filter control sequences only when interactive
This demonstrates that we only write control sequences when interactive.
2024-04-12 12:28:22 +02:00
Johannes Altmanninger
9158395d10 Fix __fish_list_current_token and friends for multiline commandlines
Some of these handled multiline prompts but not multiline command lines. We
first need to move the cursor to the end of the commandline, then we can
print a message.  Finally, we need to move the cursor back to where it was.
2024-04-12 12:00:24 +02:00
Johannes Altmanninger
8386088b3d Update commandline state changes eagerly as well
The new reader_execute_readline_cmd() runs apply_commandline_state_changes()
to make sure that given

    bind x "commandline --insert foo; commandline -f backward-char"

the backward-char command knows about the insertion of "foo".  This
causes problems when running "sleep 1&" and typing some characters -
the commandline will be cleared when the job finishes.  This is because
apply_commandline_state_changes() works with stale information in this case.

Let's call it as soon as we know it's needed.  This is less messy and fits
better with the new bind function semantics ("execute things in the order
they are written").
2024-04-12 12:00:24 +02:00
Johannes Altmanninger
57d3614fd8 Add missing import to fg.py 2024-04-12 11:41:40 +02:00
Johannes Altmanninger
9db53e8d26 Allow abbreviating ctrl-/alt- as c-/a-
This makes them more convenient to use interactively, similar to the existing
\c and \a versions.  The resulting bind output keeps using the canonical
ctrl/alt version.

Not sure about s- because that's somewhat ambiguous, it could be "super".
2024-04-12 11:27:55 +02:00
Johannes Altmanninger
bc4897b2b5 Remove "plus" from named keys
It's not necessary and it's confusing if the canonical version unnecessarily
deviates from the input (we use + for Vi binds).
2024-04-12 11:27:55 +02:00
Johannes Altmanninger
59922d0859 Remove stale bits from CONTRIBUTING 2024-04-12 11:27:55 +02:00
Johannes Altmanninger
5c3a0251b7 funced: don't try to source interactive-only function
Regressed in 2c2ab0c1f (Always `source` file after `funced` (#10318),
2024-02-22) which was only intended for functions that are backed by a file.
2024-04-12 11:27:55 +02:00
Johannes Altmanninger
f062ad3ad6 Try to fix macOS CI by disabling fg.py, signals.py, torn_escapes.py
These work fine AFAICT, just not in CI.
2024-04-12 11:27:55 +02:00
Johannes Altmanninger
15cd74a3bb Fix fish_escape_delay_ms for terminals that send CSI 27 u
See the parent commit for some context.  Turns out that 8bf8b10f6 (Extended &
human-friendly keys, 2024-03-30) broke this for terminals that speak CSI u.
This is pretty complex, probably not worth it.
2024-04-10 22:39:33 +02:00
Johannes Altmanninger
b815319607 Remove redundant default escape delay
When a terminal sends \x1ba, that could be either escape,a or alt-a.
Historically we've handled this with an escape delay that defaults to 30
milliseconds.  If we read nothing for that time, it's escape. Otherwise it's
an alt modifier (or an escape sequence).

As a side effect of 8bf8b10f6 (Extended & human-friendly keys, 2024-03-30) we
added a new way of disambiguating escape: whenever we read the escape byte,
we immediately try another (nonblocking) read.  If it succeeds, we treat it
as modifier, else it's escape. Before that commit, we didn't have a concept
of modifiers.

The new way works fine for disambiguating escape,a from alt-a (as pressed
by the user) because only for alt-a the data is sent in the same packet.

So we no longer need the escape delay to disambiguate the alt from the
escape key.  Let's simplify things by not using it by default.

The escape delay as set by fish_escape_delay_ms also serves another purpose;
it allows to disambiguate "escape,a" from "escape (pause) a". For that use
case we want to keep it.
2024-04-10 22:39:33 +02:00
Johannes Altmanninger
1da2087038 Also refresh TTY timestamps before "commandline -f repaint"
As mentioned in 8a7c3ceec (Don't abandon line after writing control sequences,
2024-04-06) we need to freshed stdout timestamps after writing to stdout
but before we might redraw, in particular when writing control sequences.

Commit a583fe723 ("commandline -f foo" to skip queue and execute immediately,
2024-04-08) made "commandline -f repaint" redraw immediately, while still
executing the bound shell command; at that time we have written "disabling"
sequences but not refreshed timestamps yet, so do that.

This is probably not needed for commands outside the repaint family.
Needless to say that this is messy, maybe we can simplify things in future.

Ref https://github.com/fish-shell/fish-shell/issues/10409#issuecomment-2044863817
2024-04-09 21:53:48 +02:00
Johannes Altmanninger
64bc989e19 Drop invasive control sequences from pexpect debug output
A failing test might emit an OSC 133 prompt marking sequence, confusing
the parent terminal to think the test output contains a shell prompt. Let's
remove these.
2024-04-09 09:51:29 +02:00
Johannes Altmanninger
adb40149a3 Do not insert key's PUA encoding into the command line
If a key's codepoint is in the PUA1 range, it could
be either from our own named keys (like key::Space)
or from a CSI u key that we haven't assigned a name yet
https://sw.kovidgoyal.net/kitty/keyboard-protocol/#functional-key-definitions
(The latter can still be bound using the \u1234 or the equivalent \e[4660u
raw CSI u sequence.)

It doesn't make sense to insert a PUA character into the commandline when
the user presses PrintScreen; ignore them silently.

This partially reverts b77d1d0e2 (Stop crashing on invalid Unicode input,
2024-02-27). That commit did:
1. convert input byte sequences that map to a PUA codepoint into several
   characters, using our on-char-per-byte PUA encoding.
2. do the same for inputs that are codepoints outside the valid Unicode range.
3. render them as replacement character (one per input byte)

In future, we should probably remove these features altogether, and simply
ignore invalid Unicode code points.
2024-04-09 00:46:16 +02:00
Johannes Altmanninger
a583fe7230 "commandline -f foo" to skip queue and execute immediately
Commit c3cd68dda (Process shell commands from bindings like regular char
events, 2024-03-02) mentions a "weird ordering difference".
The issue is that "commandline -f foo" goes through the input
queue while other commands are executed directly.
For example

    bind ctrl-g "commandline -f end-of-line; commandline -i x"

is executed in the wrong order. Fix that.

This doesn't yet work for "commandline -f exit" but that can be fixed easily.

It's hard to imagine anyone would rely on the existing behavior.  "commandline
-f" in bindings is mostly used for repainting the commandline.
2024-04-09 00:22:41 +02:00
Johannes Altmanninger
9d7116c12d Move readline loop state into reader state
To be used by the next commit.
2024-04-09 00:22:41 +02:00
Johannes Altmanninger
f9bdad3f77 Remove unused function 2024-04-09 00:22:41 +02:00
Johannes Altmanninger
11bd5d7f0c Extract function for handling input event
Will use in a following commit.
2024-04-09 00:22:41 +02:00
Johannes Altmanninger
e934e1b009 Test that bind output can recreate the same bindings 2024-04-09 00:22:41 +02:00
Johannes Altmanninger
8949c44574 Fix __fish_complete_command with multiline tokens 2024-04-09 00:07:27 +02:00
Johannes Altmanninger
f61ef2c63d Display raw escape sequences the old way again
If a binding was input starting with "\e", it's usually a raw control sequence.
Today we display the canonical version like:

    bind --preset alt-\[,1,\;,5,C foo

even if the input is

    bind --preset \e\[1\;5C foo

Make it look like the input again.  This looks more familiar and less
surprising (especially since we canonicalize CSI to "alt-[").

Except that we use the \x01 representation instead of \ca because the
"control" part can be confusing. We're inside an escape sequence so it seems
highly unlikely that an ASCII control character actually comes from the user
holding the control key.

The downside is that this hides the canonical version; it might be surprising
that a raw-escape-sequence binding can be erased using the new syntax and
vice versa.
2024-04-09 00:07:27 +02:00
Johannes Altmanninger
d025b245f6 Fix parsing of single-digit function keys 2024-04-09 00:07:27 +02:00
Johannes Altmanninger
7fd018e851 Minor changelog update 2024-04-09 00:07:27 +02:00
Johannes Altmanninger
ece4ebaf72 fish_key_reader: show unmapped function key as hex code
We don't yet support all keys from
https://sw.kovidgoyal.net/kitty/keyboard-protocol/#functional-key-definitions
Instead of displaying a private-use character, show the character code;
this can be used to map the key even if we don't know a name for it.

    bind \uE011 'echo print screen'
    bind ctrl-\uE011 'echo do control + print screen'

Note that it's also possible to mape the raw CSI u sequence, like

    bind \e\[57361u 'echo print screen'

but we should not encourage that syntax because it does not allow adding
the modifiers like ctrl.

Of course leaking the PUA character code is not ideal.
2024-04-08 09:16:55 +02:00
Johannes Altmanninger
1c41bcd1a4 fish_key_reader: minimize logic following recent changes 2024-04-08 09:16:22 +02:00
Johannes Altmanninger
405c9c6aaf Remove unused import 2024-04-08 09:16:22 +02:00
Johannes Altmanninger
473191b708 Revert "Run asan and macOS CI in release mode too"
This reverts commit 8ada027f05.

See 8ada027f05 (commitcomment-140718706)
2024-04-08 09:16:22 +02:00
Johannes Altmanninger
d30fab372f Pop CSI u mode on SIGTERM
As implied by the changelog.

Unfortunately it's not obvious how to access the RefCell value in spite
of a potential (albeit unlikely) present mutable borrow. We need to use a
different type to make it work in such cases, hopefully doing that in future.

In future we could even use panic=abort and use this style of cleanup for
panics (instead of RAII).
2024-04-07 13:32:48 +02:00
Johannes Altmanninger
1696b1527a builtin commandline: remove redundant function calls 2024-04-07 13:32:48 +02:00
Johannes Altmanninger
e0bbeb647b Remove unused function 2024-04-07 12:59:16 +02:00
Johannes Altmanninger
866585c6ce Fix accidental truncation of raw sequences
For numpad 1 with nulock, Alacritty sends

    escape,[,5,7,4,0,0,u

which is codepoint \x31, key "1".  We have a terminfo mapping for "sright"
which translates to

    escape,[,1,;,2,C

The first two characters, escape and [ match. Then we accidentally match the
"1" from the mapping against the entire sequence, because that sequence is
canonicalized to codepoint "1" . The most blatant problem is that we discard
the rest of the sequence. Fix that.

This allows us to re-enable raw CSI u mappings like "bind \e[1u ..."
which is what kitty uses for shell integration.
2024-04-07 09:59:28 +02:00
Johannes Altmanninger
b97187c90b Fix crash displaying CSI u codepoints in ASCII control range 2024-04-07 09:59:09 +02:00
Johannes Altmanninger
c8f3659737 Add special_key=1 to prompt marking
Kitty uses this for more graceful mouse handling
when the completion pager is active, see
https://github.com/kovidgoyal/kitty/pull/7316#issuecomment-2041279797
2024-04-07 09:59:09 +02:00
Johannes Altmanninger
3b9e3e251b Emit OSC 133 sequences to mark prompt/command output regions
This allows terminals like foot and kitty to
* scroll to the previous/next prompt with ctrl-shift-{z,x}
* pipe the last command's output to a pager with ctrl-shift-g

Kitty has existing fish shell integration
shell-integration/fish/vendor_conf.d/kitty-shell-integration.fish which we
can simplify now. They keep a state variable to decide which of prompt start,
command start or command end to output.  I think with our implementation
this is no longer necessary, at least I couldn't reproduce any difference.
We also don't need to hook into fish_cancel or fish_posterror like they do;
only in the one place where we actually draw the prompt.

As mentioned in the above shell integration script, kitty disables reflow
when it sees an OSC 133 marker, so we need to do it ourselves,
otherwise the prompt will go blank after a terminal resize.

Closes #10352
2024-04-06 22:22:56 +02:00