Commit Graph

5415 Commits

Author SHA1 Message Date
Mahmoud Al-Qudsi
2e52d51af2 Convert Block::event_blocks to a bool
We only increment it and check if it's non-zero, we never decrement or check the
actual count. As such, change it to a bool and bring the size of `Block` down
from 32 to 24 bytes.
2024-06-01 13:01:40 -05:00
Mahmoud Al-Qudsi
edd6533a14 Reduce size of Block to 32 bytes
We don't need 16 bytes (plus the `Option` overhead) to store the line number!
2024-06-01 12:49:15 -05:00
Mahmoud Al-Qudsi
5ca76564a4 Store BlockData in a Box
We almost never access any of this and having it stored directly in the `Block`
struct increases its size (reducing how many we can fit in L1 and L2, and
increasing memory copy traffic).

Gets rid of BlockData::None so we can avoid allocating a Box at all when we have
no data (at the cost of yet-another-wrapper-type), which is the usual case.
2024-06-01 11:41:32 -05:00
Mahmoud Al-Qudsi
1d159277c6 Move Block fields specific to certain block types to separate enum
This has a few advantages,
* We now statically assert that all fields used by a particular block type are
  correctly initialized (i.e. you can't assign the function name but forget to
  assign its arguments),
* Conversely, we can match directly on `BlockData` and be guaranteed that the
  fields we want to access are initialized and present,
* We reduce the number of assertions, effectively "unwrapping" only once based
  off the block type instead of each time we try to access a conditional field,
* We reduce the size of the `Block` struct by coalescing fields that cannot
  co-exist, bringing it down from 104 bytes to 88 bytes.

It would be nice to make all of `Block` itself an enum, but it currently
requires `Copy` and we take advantage of that to copy it around everywhere.
Putting these fields directly in `Block` directly would mean a lot more memory
traffic just checking block types.
2024-06-01 11:15:19 -05:00
Mahmoud Al-Qudsi
0246c938ca Coalesce BlockType::function_call and BlockType::function_call_no_shadow
There's no need for two separate block types when one is merely a variant of the
other. This may have been required under C++ but thanks to sum types (rust's
enums) we don't need to do that any more.
2024-05-31 20:53:52 -05:00
Mahmoud Al-Qudsi
417e89a4b3 Work around WSLv1 not properly cleaning up stopped orphaned jobs
See #5263.
2024-05-30 21:31:04 -05:00
Mahmoud Al-Qudsi
6c944debec Send signals in the correct order in hup_jobs()
If the backgrounded/stopped job was using the tty, sending it SIGCONT first
might cause it to immediately wake and try to use the tty (which fish still has
control over), causing it to immediately stop again after receiving a SIGTTOU.

We are supposed to send SIGHUP first so that when the process resumes it sees
the queued SIGHUP and executes its registered handler!
2024-05-30 18:26:13 -05:00
Mahmoud Al-Qudsi
7d77d7aa84 Convert more block iteration methods to use iterators 2024-05-30 15:54:54 -05:00
Mahmoud Al-Qudsi
f6200224fc Use Iterator::count() to check function stack depth 2024-05-30 12:49:28 -05:00
Mahmoud Al-Qudsi
57f558578b Add workaround for targets with too small a main stack size
pthread_get_stacksize_np() is buggy on legacy OS X; make sure you are building
fish with a rust toolchain that correctly patches these functions.

See https://github.com/macports/macports-legacy-support/pull/86
2024-05-30 12:14:37 -05:00
Mahmoud Al-Qudsi
7bf3b57e47 Fix buggy test_pthread() condvar test
There's no guarantee that a condition variable is stateful. The docs for
`Condvar::notify_one()` actually say the opposite:

> If there is a blocked thread on this condition variable, then it will be woken
> up from its call to wait or wait_timeout. Calls to notify_one are not buffered
> in any way.

This test was relying on the main loop obtaining the lock and entering the
condition variable sleep before the thread was scheduled and got around to
notifying the condition variable. If this non-deterministic behavior was not
upheld, the test would time out since it would obtain the lock (either before or
after the variable were updated) then call `condvar.wait()` *after* the variable
had been updated and the condvar signalled, but without (atomically or even at
all) checking to see if the desired wake precondition was fulfilled. As the
child thread had already run and the wake notification was NOT buffered, there
was nothing to wake the running thread.

There really wasn't any way to salvage the test as originally written, since the
write to `ctx.val` was not in any way linked to the acquire/release of the mutex
so regardless of whether or not the main thread obtained the mutex and checked
the value precondition before calling `condvar.wait()`, the child thread's write
could have happened after the check but before the wait() call. As such, the
test has been rewritten to use `wait_while()` but then also updated to bail in
case of a timeout instead of hanging indefinitely (since neither the `ctest`
runner nor the `cargo test` harness was timing out; `cargo test` would only
report that the test had exceeded 60 seconds but as long as it was not executed
with `cargo test -- -Z --ensure-time` (which is only available under nightly),
the test would not halt.

If this test were *intentionally* written to test the scenario that was timing
out, it should be written deterministically in such a way that the main loop
did not run until after it was guaranteed that the variable had been updated
(i.e. by looping until val became 5 or waiting for an AtomicBool indicating the
update had completed to be set), but I'm not sure what the benefit in that would
be since the docs actually guarantee the opposite behavior (the notified state
is explicitly not cached/buffered).

If we have fish code written with the assumption that condvar notifications
prior to *any* call to `Condvar::wait()` *are* buffered, then that code should
of course be revisited in light of this.
2024-05-29 13:13:13 -05:00
Johannes Altmanninger
390b40e02b Fix regression not refreshing TTY timestamps after external command from binding
Commit 8a7c3ce (Don't abandon line after writing control sequences, 2024-04-06)
was broken by 29f2da8 (Toggle terminal protocols lazily, 2024-05-16), fix that.

Fixes #10529
2024-05-29 12:57:09 +02:00
ridiculousfish
29b620b56c Remove an unused type 2024-05-27 11:45:25 -07:00
ridiculousfish
9e406e4fbc Silence some clippies 2024-05-27 11:07:02 -07:00
Mahmoud Al-Qudsi
6117b5071c Don't ignore assert_sorted_by_name doctest 2024-05-27 10:20:24 -05:00
Mahmoud Al-Qudsi
286fa4bf5b Add path basename --no-extension
This makes `path basename` a more useful replacement for the stock `basename`
command, which can be used with `-s .ext` to trim `.ext` from the base name.

Previously, this would have required the equivalent of

    path change-extension "" (path basename $path)

but now it can be just

    path basename -E $path
2024-05-26 22:06:11 -05:00
ridiculousfish
f16a1361c5 Adopt the new printf crate
This drops our usage of printf-compat.
2024-05-26 16:07:27 -04:00
Fabian Boehm
b9b7dc5f6c fmt 2024-05-26 10:50:45 +02:00
Fabian Boehm
52d1806e1f Apply some manual clippy lints
Mostly replacing std::<type>::MAX with <type>::MAX.

Surprising here is replacing

.expect(format!(...))

with

.unwrap_or_else(|_| panic!(...))

It explains that this is because the "format!" would always be called.
2024-05-26 10:45:46 +02:00
Fabian Boehm
20830744a9 Apply some clippy lints
Nothing too surprising, mostly removing useless references and lambdas
2024-05-26 10:37:37 +02:00
Fabian Boehm
2c3894993f Remove errant profiling enabling
This enabled the profile in fish_setlocale, which caused startup
profile to always be on, so

```fish
fish --profile file -c 'foo'
```

would show the entire startup as well
2024-05-26 10:32:28 +02:00
ridiculousfish
08f8983085 Adopt the new hex float parsing
This eliminates hexponent.
2024-05-25 18:39:45 -07:00
ridiculousfish
bed2ff2ea6 Add homegrown hex float parsing
Hex float parsing may come about through wcstod, for example:

    printf "%f" '0x8p2'

should output 32.0.

Currently we use a not-great fork of hexponent. Hexponent has been dormant for
years, and has some issues: doesn't round properly, allocates unnecessarily,
doesn't handle denormals, is more complicated than necessary.

Just rewrite hex float parsing, fixing those problems and getting us off of this
weird fork.
2024-05-25 18:31:38 -07:00
Fabian Boehm
1d0f1d2697 fmt 2024-05-25 22:21:52 +02:00
Fabian Boehm
d5101e1923 set: Put back zero-index error instead of crashing
This was missed in the initial port in 77aeb6a2a8.
2024-05-25 21:32:40 +02:00
Fabian Boehm
bf9e5583ba Push and pop for-block every run through the loop
We do the same in while loops. This clears the local variables every time.

Fixes #10525
2024-05-25 13:20:05 +02:00
Mahmoud Al-Qudsi
6921394db2 Remove needless use of dynamic dispatch
We return a plain function, all with matching signatures. No need for dynamic
dispatch here.
2024-05-24 17:30:38 -05:00
Mahmoud Al-Qudsi
2c2f7cb4d1 Use our own thread id
ThreadId is way slower than it should be for the sense that we use it in; it
doesn't cache the id and allocates an Arc internally.

We don't care about the thread id used in crate::threads correlating with any
other thread id the code uses anywhere (not that it does) because it's only used
for our own bookkeeping. Change to something much simpler instead.
2024-05-24 13:34:18 -05:00
Mahmoud Al-Qudsi
cf4ab20055 Use OnceLock in crate::threads
Verified that std::sync::OnceLock<T> compiles to the same assembly at the
*access* site as the Option<T> we were using. The additional overhead upon init
is fine. No need for extra Box<T> indirection for IO_THREAD_POOL.
2024-05-24 12:46:47 -05:00
Mahmoud Al-Qudsi
59317da19e Clarify threading semantics of DISOWNED_PIDS
While obtaining an uncontested mutex from the same thread (without reentrance)
is basically ~free, the use of `MainThread<RefCell<T>>` instead of `Mutex<T>`
makes it clear that there is no actual synchronization taking place, hopefully
making the code easier to understand.
2024-05-24 10:20:29 -05:00
Mahmoud Al-Qudsi
43d6289c26 Make assert_is_main_thread() simpler to optimize
The compiler cannot guarantee that a `static AtomicBool` is always the same
initial value, but it can do so for a `const bool`.
2024-05-24 09:57:42 -05:00
Johannes Altmanninger
de7f39d627 builtin bind: make function keys lowercase (f1 instead of F1)
All other key names are lowercase so this inconsistency is weird.
2024-05-22 22:38:06 +02:00
Johannes Altmanninger
2fa98ec20c Fix deadlock when importing universal LC_* variable
The C++ version of this code simply copied the entire uvar table.
Today we take a reference. It's not clear which one is better.

Removal of locale variables like LC_ALL triggers variable change handlers
which call EnvStackImpl::get. This deadlocks because we still hold the lock
to protect the reference to all uvars.  Work around this.

Closes #10513
2024-05-21 23:11:06 +02:00
Mahmoud Al-Qudsi
0b5e41b268 Rework path_normalize_for_cd() to be less allocation trigger happy
Lots of resizing, splicing, and full-on allocating going on here.
2024-05-21 12:54:52 -05:00
Mahmoud Al-Qudsi
d14d8d5733 Remove wcstringutil::split_string()
It is short and simple enough to write yourself if you need it and it encourages
bad behavior by a) always returning owned strings, b) always allocating them in
a vector. If/where possible, it is better to a) use &wstr, b) use an iterator.

In rust, it's an anti-pattern to unnecessarily abstract over allocating
operations. Some of the call sites even called split_string(..).into_iter().
2024-05-21 12:54:52 -05:00
Mahmoud Al-Qudsi
96b979077c Add unit tests for path_normalize_for_cd() 2024-05-21 12:54:51 -05:00
Mahmoud Al-Qudsi
8c62f733b3 Extend certain WSL workarounds to WSLv2
This updates is_windows_subsystem_for_linux() to take a WSL version to test for
(any, v1, or v2) and returns the boolean result depending on the system. I've
benchmarked and when running on regular Linux, this is still just as fast as the
previous binary check; it's only when it's WSL that this takes about 20ns
longer to figure out which variant.

Note that older WSLv2 kernels had a `-microsoft-standard` suffix while newer
ones appear to have a `-microsoft-standard-WSL2` suffix, so we make sure to test
for the least common denominator. (It doesn't matter to us, but note that newer
WSLv2 kernels have four dots in the version string!)

WSL workarounds pertaining to the default Windows terminal or executable
behavior of win32 binaries under a WSL shell are extended to WSLv2 while those
specific to oddities in kernel behavior are confined to WSLv1 only. (It
technically wouldn't hurt to extend them to WSLv2 but there's no good reason to
do so, either.)
2024-05-20 14:14:25 -05:00
Mahmoud Al-Qudsi
3374692b91
Work around $PATH issues under WSL (#10506)
A common complaint has been the massive amount of directories Windows appends to
$PATH slowing down fish when it attempts to find a non-existent binary (which it
does a lot more often than someone not in the know might think). The typical
workaround suggested is to trim unneeded entries from $PATH, but this a) has
considerable friction, b) breaks resolution of Windows binaries (you can no
longer use `clip.exe`, `cmd.exe`, etc).

This patch introduces a two-PATH workaround. If the cmd we are executing does
not contain a period (i.e. has no extension) it by definition cannot be a
Windows executable. In this case, we skip searching for it in any of the
auto-mounted, auto-PATH-appended directories like `/mnt/c/Windows/` or
`/mnt/c/Program Files`, but we *do* include those directories if what we're
searching for could be a Windows executable. (For now, instead of hard-coding a
list of known Windows executable extensions like .bat, .cmd, .exe, etc, we just
depend on the presence of an extension at all).

e.g. this is what starting up fish prints with logging enabled (that has been
removed):

    bypassing 100 dirs for lookup of kill
    bypassing 100 dirs for lookup of zoxide
    bypassing 100 dirs for lookup of zoxide
    bypassing 100 dirs for lookup of fd
    not bypassing dirs for lookup of open.exe
    not bypassing dirs for lookup of git.exe

This has resulted in a massive speedup of common fish functions, especially
anywhere we internally use or perform the equivalent of `if command -q foo`.

Note that the `is_windows_subsystem_for_linux()` check will need to be patched to
extend this workaround to WSLv2, but I'll do that separately.

Under WSL:
* Benchmark `external_cmds` improves by 10%
* Benchmark `load_completions` improves by an incredible 77%
2024-05-20 10:29:32 -05:00
ridiculousfish
42f8672f34 Remove an errant {} from a FLOG 2024-05-19 10:27:45 -07:00
Johannes Altmanninger
d40d2b786f Work around wants_terminal not begin set inside eval
On this binding we fail to disable CSI u

    bind c-t '
        begin
            set -lx FZF_DEFAULT_OPTS --height 40% --bind=ctrl-z:ignore
            eval fzf | while read -l r; echo read $r; end
        end
    '

because for "fzf", ParseExecutionContext::setup_group() returns early with the
parent process group (which should be fish's own) , hence "wants_terminal"
is false. This seems questionable, I don't think the eval should make a
difference here.

For now, don't touch it; use the more accurate way of detecting whether
a process may read keyboard input. In many of such cases "wants_terminal"
is false, like

    echo (echo 1\n2\n3 | fzf)

Fixes #10504
2024-05-18 20:55:06 +02:00
Mahmoud Al-Qudsi
d8e9a17c1f Inline extract_prefix_and_unescape_yaml()
We sometimes call it but discard half its results, so force it to be inlined to
make sure we don't perform work we then throw away.
2024-05-17 16:11:46 -05:00
Mahmoud Al-Qudsi
bcb1e2ed85 Further optimize unescape_yaml_fish_2_0()
This hot function dominates the flamegraphs for the completions thread, and any
optimizations are worthwhile.

A variety of different approaches were tested and benchmarked against real-world
fish-history file inputs and this is the one that won out across all rustc
target-cpu variations tried.

Benchmarks and code at https://github.com/mqudsi/fish-yaml-unescape-benchmark
2024-05-17 16:06:08 -05:00
Mahmoud Al-Qudsi
3d648e6e04 Remove Arc from environment::globals() (take 2)
We don't forward this variable for storage in any structs, so there's no reason
to go through an Arc instead of returning the `&'static EnvStack` directly.

NB: This particular change was safe, and passes all tests on its own.
2024-05-16 21:17:15 -05:00
Mahmoud Al-Qudsi
45e249dd94 Revert removal of Arc from principal() and global()
This reverts commit c6d3bde0c6.
This reverts commit 4ce13f0adb.
2024-05-16 21:08:06 -05:00
Mahmoud Al-Qudsi
c6d3bde0c6 Remove Arc from environment::globals()
We don't forward this variable for storage in any structs, so there's no reason
to go through an Arc instead of returning the `&'static EnvStack` directly.
2024-05-16 20:50:23 -05:00
Mahmoud Al-Qudsi
4ce13f0adb Simplify lifetime of environment::principal()
It's clearer that using it with `Rc::from_raw()` is safe since we don't have to
go through an `Arc<T>`.
2024-05-16 20:46:28 -05:00
Mahmoud Al-Qudsi
ec1bf60941 Add note about possible safety issue with GLOBAL_NODE 2024-05-16 20:33:43 -05:00
Mahmoud Al-Qudsi
66618d64cb Convert some lazy_static to OnceCell/OnceLock
These have clearer sync/unsync semantics and now ship with rust itself.
They don't paper over any possible cross-thread issues, and we can specifically
choose which we want for the purpose.
2024-05-16 20:33:43 -05:00
Mahmoud Al-Qudsi
0f18480559 Simplify Parser and EnvStack singletons and clarify thread semantics
`Parser` is a single-threaded `!Send`, `!Sync` type and does not need to use
`Arc` for anything. We were using it because that's all we had for the parser's
`EnvStack`, but though that is *technically* protected internally by a mutex
(shared with global EnvStack), there's nothing to say that other parsers with a
narrower scope/lifetime on other threads will be necessarily using the same
backing mutex.

We can safely marshal the existing `Arc<EnvStack>` we get from
`environment::principal()` into an `Rc<EnvStack>` since the underlying reference
is always valid. To prove this point, we could have PRINCIPAL_STACK be a static
`EnvStack` and have `environment::principal()` use `Arc::from_raw()` to turn
that into an `Arc<EnvStack>`, but there's no need to factorize this process.
2024-05-16 20:33:39 -05:00
Mahmoud Al-Qudsi
e4282f3798 Remove all locking from principal_parser()
By inverting the order of storage, we can use an `OnceCell`/`unsync::Lazy`
inside the Send/Sync `MainThread<T>` and remove the need for a lock altogether.
2024-05-16 14:23:25 -05:00
Johannes Altmanninger
29f2da8d18 Toggle terminal protocols lazily
Closes #10494
2024-05-16 12:26:47 +02:00
Johannes Altmanninger
6f9d5cf44c Address clippy lint 2024-05-16 12:25:29 +02:00
Mahmoud Al-Qudsi
b82170aded Change extract_prefix_and_unescape_yaml() assert!() to debug_assert!()
It's reasonable since this is only checking to see that the history file
contains the expected format and if it's corrupted but we at least got what we
expect to be the correct key/value pairs, then that's all we can do.

Of course the real motivation is to speed up this very hot function in any way
possible!
2024-05-15 22:42:19 -05:00
Mahmoud Al-Qudsi
51275525c1 Speed up extract_prefix_and_unescape_yaml
On the completions and history thread, the parent function
HistoryFileContents::decode_item() is responsible for ~60% of the CPU time, and
extract_prefix_and_unescape_yaml() alone comprising 14% (of the total).

This change removes allocations in the event that the history item is either
fully or partially plain yaml with no escapes to begin with, and brings down the
execution time of this function to only 7% of the total execution time.

The bulk of the remaining time is spent in wcs2string(), which is called
unconditionally and is naturally alloc-heavy.
2024-05-15 22:42:19 -05:00
ridiculousfish
caef202551 Fix some more clippies and enable more warnings 2024-05-15 20:05:35 -07:00
Fabian Boehm
f1e19884fb Add set --no-event
This allows running `set` without triggering any event handlers.

That is useful, for example, if you want to set a variable in an event
handler for that variable - we could do it, for example, in the
fish_user_path or fish_key_bindings handlers.

This is something the `block` builtin was supposed to be for, but it
never really worked because it only allows suppressing the event for
the duration, they would fire later. See #9030.

Because it is possible to abuse this, we only have a long-option so
that people see what is up.
2024-05-14 17:31:47 +02:00
Fabian Boehm
403920e9d6 input: Remove all_mappings_cache
Replace it with a chained iterator.

This not only simplifies the code, but also removes a RefCell.
2024-05-14 16:14:02 +02:00
Johannes Altmanninger
a19ff4989a Prevent out-of-order execution following repaint
Commit a583fe723 ("commandline -f foo" to skip queue and execute immediately,
2024-04-08) fixed the execution order of some bindings but was partially
backed out in 5ba21cd29 (Send repaint requests through the input queue again,
2024-04-19) because repainting outside toplevel yields surprising results
(wrong $status etc).

Transient prompts wants to first repaint and then execute some more readline
commands, all within a single binding.  This was broken by the second commit
because that one defers the repaint until after the binding has finished.

Work around this problem by deferring input events again while a readline
event was queued. This is closest to the historical behavior.

The implementation feels hacky; we might find odd situations.
For example,

    commandline -f repaint end-of-line
    set token (commandline -t)

sets the wrong token.
Probably not a very important case. We could throw an error or make it work
by letting "commandline -t" drain the input queue.

That seems too complicated, better change repaints to not use the input queue
(and fake $status etc). Let's try to do that in future.

Closes #10492
2024-05-13 10:44:12 +02:00
ridiculousfish
d6e231af0d Fix some clippies 2024-05-12 14:55:35 -07:00
ridiculousfish
172fb22692 Clean up and correct O_SEARCH definitions
Correct the FreeBSD constant and simplify some of the cfg() logic.
2024-05-11 13:20:34 -07:00
Jonathan Krebs
4606b02d44 rustfmt 2024-05-11 11:12:29 -07:00
Jonathan Krebs
a148760963 cd: open directory with O_SEARCH or O_PATH, when the platform supports it 2024-05-11 11:12:29 -07:00
Jonathan Krebs
2ecbdb9ae7 cleanup: fds::open_dir - remove mode argument
[w]open_dir does not pass O_CREAT, so the mode argument to open is never used.
also, O_CREAT | O_DIRECTORY could not be used (portably) to create a directory.
(on POSIX does not specify what should happen, on Linux it is EINVAL.)
2024-05-11 11:12:29 -07:00
Mahmoud Al-Qudsi
35a16e3713 Rename FISH_TSAN_WORKAROUNDS and add feature to Cargo.toml
rustc 1.80 now complains about features not declared in Cargo.toml and cfg
keys/values not declared by build.rs to protect against typos or misuse (you
think you're using the right condition but you're not). See
rust-lang/cargo#10554 and rust-lang/rust#82450.

(We're not actually using TSAN under CI at this time, but I do want to re-enable
it at some point — especially if we get multithreaded execution going — using
the rust-native TSAN configuration.)

I'll be updating the `rsconf` crate and patching `build.rs` accordingly to also
handle the warnings about unknown cfg values, but tsan is a feature and not a
cfg and these can be dealt with in `Cargo.toml` directly.
2024-05-09 13:58:42 -05:00
Mahmoud Al-Qudsi
059b842d1b is_wsl(): Move .copied() earlier in the iter pipeline
This is a best practice that usually helps with auto vectorization, as llvm has
a hard time seeing through the references.
2024-05-09 13:33:50 -05:00
Mahmoud Al-Qudsi
5dcc5955fb Don't allocate a vector twice in wcs2zstring()
We were passing a slice (and not a vec) to `CString::new()`, meaning it would
allocate a new Vec internally to hold the bytes.

Also document that the resulting CString will be silently truncated at the first
interior NUL.
2024-05-09 13:19:06 -05:00
Mahmoud Al-Qudsi
883bcbad83 Remove possible panic from wperror() 2024-05-09 11:20:09 -05:00
Mahmoud Al-Qudsi
84be043f31 Clean up src/libc.rs some
Update the CVAR!() macro and use it more.
2024-05-08 22:34:10 -05:00
Mahmoud Al-Qudsi
92eee61fb5 Convert colors::Flags to a bitflags enum
We use accessors and setters for all operations, so there's no benefit to
storing the modifiers as separate boolean fields.
2024-05-07 14:03:38 -05:00
Fabian Boehm
41a0fe2b1d history: Check for cmd key earlier
This shows up hot in `perf record ./fish` style profiles.

This assumes that "- cmd" can't be escaped.
2024-05-07 20:23:32 +02:00
Mahmoud Al-Qudsi
f709795a3a Reserve vector capacity upfront in colors::named_color_names()
Only because why not.
2024-05-07 13:09:40 -05:00
Mahmoud Al-Qudsi
fd2ea3ff0f Add more tests for color::try_parse_rgb() 2024-05-07 13:00:11 -05:00
Mahmoud Al-Qudsi
5b79f267d6 Optimize color::try_parse_rgb()
The function was repeatedly calling `s.char_at(n)` which is O(1) only for UTF-32
strings (so not a problem at the moment). But it was also calling `hex_digit(n)`
twice for each `n` in the 3-digit case, causing unnecessary repeated parsing of
individual characters into their radix-16 numeric equivalents, which could be
avoided just by reusing the already calculated result.
2024-05-07 12:53:44 -05:00
Fabian Boehm
b9d44407b3 tokenizer: Stop copying the start string 2024-05-07 16:59:35 +02:00
Mahmoud Al-Qudsi
5f8f799cf7 Replace C++ doc \return with "Return"
quick_replace '\\\\return(s)? ' 'Return$1 ' src/

Filtered to only lines beginning with //
2024-05-06 14:59:36 -05:00
Mahmoud Al-Qudsi
589639a87d Replace C++-style \p with Markdown backticks
quick_replace '\\\\p ([a-zA-Z0-9_]+)' '`$1`' src/

Filtered to only lines beginning with //
2024-05-06 14:59:23 -05:00
Mahmoud Al-Qudsi
a35925b3ed Don't use $HOME under cargo test harness
We will continue to use the "normal" fish base directory detection when using
the CMake test harness which properly sets up a sandboxed $HOME for fish to use,
but when running source code tests with a bare `cargo test` we don't want to
write to the actual user's profile.

This also works around test failures when running `cargo test` under CI with a
locked-down $HOME directory (see #10474).
2024-05-06 14:31:51 -05:00
Fabian Boehm
2a121ef1aa function: Check if --argument-names gets a valid variable name
These were accepted but then ineffective because the only way these
are used is to set a variable.
2024-05-06 17:00:45 +02:00
Fabian Boehm
39b996332e functions: Fix --argument-names display
Mentioned in #10465
2024-05-06 17:00:45 +02:00
Mahmoud Al-Qudsi
2d2b75003d Fix format string presumably broken under i686
%ld expects a 32-bit param under x86, change it to %lld instead.
2024-05-04 22:57:55 -05:00
Mahmoud Al-Qudsi
476b360eb8 Remove rust test dependency on cmake output
The test_history_formats test was reading from build/tests/ which is an artifact
of the cmake test runner. The source code tests should not depend on the cmake
test harness at all, so this is changed to read from the original test source in
the ./tests/ directory instead.
2024-05-04 20:49:49 -05:00
Mahmoud Al-Qudsi
6b43a96d09 fixup! Work around i586 wcstod test failure 2024-05-04 20:29:40 -05:00
Mahmoud Al-Qudsi
4d58a31670 Work around i586 wcstod test failure
As documented in #10474, there are issues with 64-bit floating point rounding
under x86 targets without SSE2 extensions, where x87 floating point math causes
imprecise results.

Document the shortcoming and provide some version of the test that passes
regardless of architecture.
2024-05-04 20:21:56 -05:00
Mahmoud Al-Qudsi
a99a7e65e7 Fix build failures when cmake never used
FISH_BUILD_DIR (nominally, ./build) is created by cmake. If you only check out
the project via git and then run `cargo build`, this directory won't exist and
many of the tests will fail.
2024-05-04 19:47:16 -05:00
Mahmoud Al-Qudsi
72259f658f Fix format string for failing test
%ld expects a 4-byte parameter on 32-bit architectures and an 8-byte parameter
on 64-bit architectures, but we supplied are trying to supply a 64-bit parameter
that would overflow 32-bit storage.

Use %lld instead which expects a `long long` parameter, which should be 8-bytes
under both architectures.

See #10474
2024-05-04 19:40:31 -05:00
Johannes Altmanninger
4e816212a1 Check for unsupported "time &" in the proper place
This means we can detect this error also for simple blocks.

While at it do some cleanup in the area.
2024-05-03 09:37:56 +02:00
Johannes Altmanninger
a126d2aeba Revert "Remove redundant default escape delay"
I think given a local terminal running fish on a remote system, we can't
assume that an input sequence like \ea is sent all in one packet. (If we
could that would be perfect.)

Let's readd the default escape delay, to avoid a potential regression, but
make it only apply to raw escape bindings like "bind \e123". Treat sequences
like "bind escape,1,2,3" like regular sequences, so they can be bound on
all terminals.

This partially reverts commit b815319607.
2024-05-03 09:37:56 +02:00
Johannes Altmanninger
d855d1a2e6 Interpret escape as alt only if it's the escape byte
No need to take this code path when an unambiguous, prefix-free encoding is
used for the escape key.
2024-05-03 09:36:35 +02:00
Johannes Altmanninger
e1eeb3177e Silence clippy lint 2024-05-03 09:36:35 +02:00
Anurag Singh
4fa8d95b98 Move push_timer to measure command substitution timing too 2024-05-03 09:32:26 +02:00
Johannes Altmanninger
d4ecea56df Fix regression spuriously expanding abbr with cursor outside token
Given "abbr foo something", the input sequence

    foo<space><ctrl-z><space>

would re-expand the abbreviation on the second space which is surprising
because the cursor is not at or inside the command token.  This looks to be
a regression from 00432df42 (Trigger abbreviations after inserting process
separators, 2024-04-13)

Happily, 69583f303 (Allow restricting abbreviations to specific commands
(#10452), 2024-04-24) made some changes that mean the bad commit seems no
longer necessary. Not sure why it works but I'll take it.
2024-05-03 08:39:05 +02:00
Johannes Altmanninger
91b007cab7 Limit ctrl-r autofill/replace to a single command substitution
As reported on gitter, commands like "rm (...)" sometimes want a previous
command inside the parentheses. Let's try that.  If a user actually wants
to search for a command substitution they can move the cursor outside the
command substitution, or type the search string after pressing ctrl-r?
2024-05-01 20:21:18 +02:00
Fabian Boehm
f551eeadfe Revert "Remove unused import"
It's not unused

This reverts commit 69fb620073.
2024-05-01 12:58:36 +02:00
Fabian Boehm
89b74a6983 Remove a few uses of unwrap 2024-05-01 12:58:29 +02:00
Johannes Altmanninger
69fb620073 Remove unused import 2024-05-01 12:53:00 +02:00
Johannes Altmanninger
b00899179f Don't indent multi-line quoted strings; do indent inside ()
On a command with multiline quoted string like

    begin
        echo "line1
    line2"
    end

we actually indent line2 which seeems misleading because the indentation
changes the behavior when typed into a script.

This has become more prominent since commits
- a37629f86 (fish_clipboard_copy: indent multiline commands, 2024-04-13)
- 611a0572b (builtins type/functions: indent interactively-defined functions, 2024-04-12)
- 222673f33 (edit_command_buffer: send indented commandline to editor, 2024-04-12)

which add indentation to an exported commandline.

Never indent quoted strings, to make sure the rendering matches the semantics.
Note that we do need to indent the opening quote which is fine because
it's on the same line.

While at it, indent command substitutions recursively.  That feature should
also be added to fish_indent's formatting mode (which is the default).
Fortunately the formatting mode already works fine with quoted strings;
it does not indent them. Not sure how that's done and whether indentation
can use the same logic.
2024-04-30 14:12:48 +02:00
Johannes Altmanninger
2f6ed61833 parse_util_cmdsubst_extent to return an exclusive range
Given "1(23)4", this function returns an inclusive range, from the opening
to the closing parenthesis.  The subcommand is extracted by incrementing
the range start and interpreting the result as an exclusive range.

This is confusing, especially if we want to add multi-character quotes.
Change it to always return the full range (including parentheses) and provide
an easy way to access the command string.

While at it, switch to returning an enum.

This change is perhaps larger and more complex than necessary (sorry)
because it is mainly made with multi-character quotes in mind.  Let's see
if that works out.
2024-04-30 14:12:48 +02:00
Johannes Altmanninger
861feb7b46 Minor simplification in tokenizer 2024-04-30 14:00:06 +02:00
Johannes Altmanninger
29be454652 Move parse_util tests to separate file 2024-04-30 14:00:06 +02:00
Fabian Boehm
8639d7e450 Remove allow-unused-imports 2024-04-29 22:00:59 +02:00
Fabian Boehm
4d4ef7fa40 Remove backports for 1.67
This removes IsOkAnd and the is_some_and method.

I cannot actually find is_none_or in the stdlib?

I've kept the trait name to avoid changing it now and then later, maybe this should
be moved elsewhere to avoid claiming it's an stdlib thing?
2024-04-29 22:00:59 +02:00
Johannes Altmanninger
964d3fff15 Keep undo history across prompts
After abandoning a commandline (for example with ctrl-c) it's nice to be
able to restore it. There is little reason to discard the requisite undo
information, so keep it.
2024-04-29 09:20:09 +02:00
ridiculousfish
3afe0bb569 Remove LoopedRead and LoopedWrite
These were unused.
2024-04-28 11:15:48 -07:00
Johannes Altmanninger
301e4d497e Make shift-delete also delete current autosuggestion
This is a bit more convenient than using "history delete".
2024-04-27 10:54:31 +02:00
Anurag Singh
62a8b48fd1 Move generated completions to cache directory 2024-04-27 10:39:30 +02:00
Fabian Boehm
1503be4287 parse_execution: Use an exhaustive match instead of an assert
This could also be "cancel", which we didn't check and instead
asserted out.
2024-04-27 08:19:00 +02:00
Johannes Altmanninger
fb99edae92 Stop using comma from command substitution in brace expansion
Fixes #5048
2024-04-26 18:15:47 +02:00
Fabian Boehm
69583f3030
Allow restricting abbreviations to specific commands (#10452)
This allows making something like

```fish
abbr --add gc --position anywhere --command git back 'reset --hard
HEAD^'
```

to expand "gc" to "reset --hard HEAD^", but only if the command is
git (including "command git gc" or "and git gc").

Fixes #9411
2024-04-24 18:09:04 +02:00
Johannes Altmanninger
24f0abe780 Fix decoding mulitbyte characters after escape prefix
Fixes #10457
2024-04-23 00:18:14 +02:00
Johannes Altmanninger
8d20bbfcd7 Fix bad assertion warning about disowned jobs that don't get their proper pgroup
Running

    echo foo | vim -

gets us in a weird situation because we put the job in fish's process groups.
It causes us to not set a PGID for this job, so it can't be resumed among
other things.

Stopping the job with ctrl-z and try to exit the shell causes a crash in the
"There are still jobs active" warning because the PID for the job is still 0.
Let's remove the assertion to restore previous behavior, and hopefully fix
this later.
2024-04-21 21:32:44 +02:00
Johannes Altmanninger
fd61bad946 Further simplify terminal_protocols scoping
Remove the last non scoped place where we disable protocols (just before
exec(1)); it's not necessary with the current approach because we always
disable inside eval.
There is an edge case where we don't:

    fish -ic "exec bash"

leaving bash with CSI u enabled.  Disable that also in -ic mode where we
don't have a reader.

In future we should use the same approach for restore_term_mode() but I'm
not sure which one is better.
2024-04-21 21:32:44 +02:00
Johannes Altmanninger
99bf3d0dbb Fix imbalanced terminal protocols on SIGCHLD
We enable terminal protocols once at startup, and disable them before exit.
Additionally, we disable them while evaluating commands (see 8164855b7 (Disable
terminal protocols throughout evaluation, 2024-04-02))..

Thirdly, we re-enable protocols inside builtin read (where it's disabled
because we are evaluating something).  All of these three are scoped and
statically guaranteed to not leak into each others scopes.

There is another place where we enable protocols non-scoped: when we
receive a notification that a job is stopped. If this is ever hit, things
will be imbalanced and we'll fail to restore the right terminal state,
or (more likely) crash due the assertion in terminal_protocols_enable().
This code path used to be necessary when we disabled protocols only while
actually executing an external command but we changed that in 8164855b7,
so it should no longer be.  Remove it.

I haven't been able to find a test case, I'll try to do that later.

The main reason we changed the scope of protocols was focus reporting (#10408).
We have given up on that for now (outside tmux where I can't get it to work)
so we might want to reconsider and go back to the "optimized" approach of
enabling it for as long as possible. But this is simpler, easier to verify.
2024-04-21 17:16:23 +02:00
Fabian Boehm
20243132fb Add <? input redirection
This tries to open the given file to use as stdin, and if it fails,
for any reason, it uses /dev/null instead.

This is useful in cases where we would otherwise do either of these:

```fish
test -r /path/to/file
and string match foo < /path/to/file

cat /path/to/file 2>/dev/null | string match foo
```

This both makes it nicer and shorter, *and* helps with TOCTTOU - what if the file is removed/changed after the check?

The reason for reading /dev/null instead of a closed fd is that a closed fd will often cause an error.

In case opening /dev/null fails, it still skips the command.
That's really a last resort for when the operating system
has turned out to be a platypus and not a unix.

Fixes #4865

(cherry picked from commit df8b9b7095)
2024-04-21 14:35:13 +02:00
Fabian Boehm
2c17d34971
Deprecate builtin test's one- and zero-argument modes (#10365)
This introduces a feature flag, "test-require-arg", that removes builtin test's zero and one argument special modes.

That means:

- `test -n` returns false
- `test -z` returns true
- `test -x` with any other option errors out with "missing argument"
- `test foo` errors out as expecting an option

`test -n` returning true is a frequent source of confusion, and so we are breaking with posix in this regard.

As always the flag defaults to off and can be turned on. In future it will default to on and then eventually be made read-only.

There is a new FLOG category "deprecated-test", run `fish -d deprecated-test` and it will show any test call that would change in future.
2024-04-21 14:25:54 +02:00
Johannes Altmanninger
e97a4fab71 Escape : and = in file completions
This is similar to f7dac82ed (Escape separators (colon and equals) to improve
completion, 2019-08-23) except we only escape : and = if they are the result of
file completions.  This way we avoid issues with custom completions like dd.
This also means that it won't work for things like __fish_complete_suffix
[*] but that can be fixed later, once we can serialize the DONT_ESCAPE flag.

By moving the escaping step earlier, this causes some unit test changes
which should not result in actual behavior change.

See also #6099

[*]: The new \: and \= does not leak from "complete -C" because that command
unescapes its output  -- unless --escape is given.
2024-04-20 13:34:08 +02:00
Johannes Altmanninger
db365b5ef8 Do not treat \: or \= as file completion anchor
Partially reapplies f7dac82ed (Escape separators (colon and equals) to
improve completion, 2019-08-23) which has been reverted.
2024-04-20 13:34:08 +02:00
Johannes Altmanninger
a046b73ec7 Extract test logic for computing and applying completion
Also move one test so all the bracket tests are contiguous.
2024-04-20 13:34:08 +02:00
Johannes Altmanninger
7dc0446c5c Match stdlib strip_prefix return value 2024-04-20 13:34:08 +02:00
Johannes Altmanninger
30fbd4280d Simplify match statement in escape_string_script 2024-04-20 13:34:08 +02:00
Johannes Altmanninger
24e4fdd695 Support "bind xyz" again
This was used in Vi mode (for yiw and "*p) so rejecting it is a bit reckless.
2024-04-20 13:34:08 +02:00
Johannes Altmanninger
565eb85d8b fish_key_reader: use canonical key name for ctrl-{c,d}
The uppercase version has a different meaning now.
2024-04-20 12:11:30 +02:00
Johannes Altmanninger
5ba21cd290 Send repaint requests through the input queue again
Another consequence of a583fe723 ("commandline -f foo" to skip queue
and execute immediately, 2024-04-08) is that "commandline -f repaint"
will paint the prompt with the current value of $status which might be
set from a shell command in a the currently executing binding, instead of
waiting for the top-level status. This is wrong, at least historically. It
surfaces in bindings like alt-w which always paint a status value of [1]
when on single-lines commandlines.

Another regression is that a redundant repaint in a signal handler outputs
an extra prompt.

Fix both by making repaint commands go over the input queue again.  This way,
they are always run with a good commandline state.  There is no need to
repaint immediately because I don't think anyone has a data dependency on it
(we currently don't expose the prompt string), it's only for rendering.
2024-04-19 12:05:27 +02:00
Johannes Altmanninger
040cb04423 Escape nonprintable characters when reporting invalid key name
Part of #10450
2024-04-18 23:27:05 +02:00
Fabian Boehm
3b6a11f881 fmt 2024-04-18 22:26:14 +02:00
Fabian Boehm
6558c0a8e5 key: Actually do engage legacy mode if first char is control
This was already in the comment.

Fixes #10450
2024-04-18 22:18:51 +02:00
Johannes Altmanninger
bdd478bbd0 Disable focus reporting on non-tmux again for now
We sometimes leak ^[[I and ^[[O focus reporting events when run from VSCode's
"Run python file" button in the top right corner. To reproduce I installed
the ms-python extension set the VSCode default shell to fish and repeatedly
ran a script that does "time.sleep(1)". I believe VSCode synthesizes keys
and triggers a race condition.

We can probably fix this but I'm not sure when I'll get to it (given how
relatively unimportant this feature is).

So let's go back to the old behavior of only enabling focus reporting in tmux.

I believe that tmux is affected by the same VSCode issue (also on 3.7.1 I
think) but I haven't been able to get tmux to emit focus reporting sequences
yet.  Still, keep it to not regress cursor shape (#4788).  So far this is
the only motivation for focus reporting and I believe it is only relevant
for terminals that can split windows (though there are a bunch that do).

Closes #10448
2024-04-18 10:38:15 +02:00
ridiculousfish
ed8f62e723 Reimplement WGetopter::exchange() using rotate_left
A simplification informed by the new test.
2024-04-17 12:41:16 -07:00
ridiculousfish
f990d52d2b Add a test for WGetopter::exchange() 2024-04-17 12:41:12 -07:00
Verte
13230cdda0 Rewrite wgetopt.rs to Rustier syntax and naming
From https://github.com/fish-shell/fish-shell/pull/9515

Closes #9515
2024-04-17 11:26:51 -07:00
ridiculousfish
a996cafeeb Make history::remove take a &wstr instead of a WString
While it does need to store the string, we also need to use the string after
storing it, so we aren't getting any advantage from passing by value. Just pass
by reference to simplify the call sites.
2024-04-15 09:47:46 -07:00
Anurag Singh
8a8c2656f3 remove unnecessarily silenced lint in history 2024-04-15 09:43:38 -07:00
Johannes Altmanninger
9af6a64fd2 Fix bad contrast in search match highlighting
This is another problem that has been bothering me for years: as mentioned
in 1dd901e52 (Maintain cursor in history prefix search, 2024-04-12), up-arrow
search highlights search matches but the contrast is really bad, especially in
command position, because the search matches --background=brblack is combined
with whatever foreground syntax highlighting the command has.  The history
pager had a similar problem (for the selected history item) but circumented
it by disabling syntax highlighting altogether for the selected item.

fish_color_search_match's foreground component is ignored.
Let's use it instead of syntax highlighting.

This fixes the contrast on some default colorschemes but the bryellow
foreground looks weirdly like an error/warning on some terminals.  Change it
to white. This needs a hack because we don't have a canonical way to tell
if a uvar has been set by the user. Fortunately the foreground component
hasn't been used at all so far, so we're not so much changing it as much as
initializing it.
2024-04-15 09:40:21 +02:00
Johannes Altmanninger
27b1f28108 Minimize key parsing fallback logic and update changelog 2024-04-15 09:40:21 +02:00
Johannes Altmanninger
47bb56efe6 Allow mapping new-style sequences that start with escape
On Konsole with

    function my-bindings
        bind --preset --erase escape
        bind escape,i 'echo escape i'
    end
    set fish_key_bindings my-bindings

the "escape,i" binding doesn't trigger.  This is because of our special
handling of the escape key prefix.  Other multi-key bindings like "bind j,k"
wait indefinitely for the second character.  But not "escape,i"; that one
has historically had a low timeout (fish_escape_delay_ms).  The motivation
is probably that we have a "escape" binding as well that shouldn't wait
indefinitely.

We can distinguish between the case of raw escape sequence binding like "\e123"
and a binding that talks about the actual escape key like "escape,i". For the
latter we don't need the special treatment of having a low timeout, so make it
fall back to "fish_sequence_key_delay_ms" which waits indefinitely by default.
2024-04-15 09:20:44 +02:00
Johannes Altmanninger
611a0572b1 builtins type/functions: indent interactively-defined functions
This means that in case no editor is defined, "fish_indent" is now required
to fix the indentation.

Fixes #8603
2024-04-15 08:32:31 +02:00
Johannes Altmanninger
47a446ae18 Teach fish_indent to only indent and unindent
To be used in the following commits.
2024-04-15 08:32:31 +02:00
Anurag Singh
c044d5e3f0 add history append subcommand 2024-04-15 08:31:16 +02:00
Johannes Altmanninger
e01fc62d69 Don't leak encoding of invalid codepoints into uvar file
When we read bytes like \xfc that don't produce a Unicode code point,
we encode them in a Unicode private use area.
This encoding should be transparent to the user.

We accidentally add it to uvar files as \uf6fc in this case.  When reading
it back, read_unquoted_escape() will fail at the "fish_reserved_codepoint(c)"
check. This check is to avoid external input being misinterpreted
as one of our in-band signalling characters like ANY_CHAR (for *).

For encoded raw bytes, this check probably doesn't really matter in terms of
security because the only thing we do with these bytes is convert them back
to raw. So we could allow unescaping them at this point, thus supporting
old uvar files.

However that seems like the wrong direction. PUA encoding should never leak.
So let's instead make sure to serialize it as \xfc instead of \f6fc going
forward.

Fixes #10313
2024-04-14 07:59:42 +02:00
Johannes Altmanninger
2329a3adb9 Extend fish_reserved_codepoint by encodings for named keys
This might prevent unexpected behavior when the terminal sends an input
character that matches one of our named keys like Enter.
2024-04-14 07:54:03 +02:00
Johannes Altmanninger
29b309dd5f shift-delete to delete current history search match
Popular operating systems support shift-delete to delete the selected item
in an autocompletion widgets.  We already support this in the history pager.
Let's do the same for up-arrow history search.

Related discussion: https://github.com/fish-shell/fish-shell/pull/9515
2024-04-13 20:23:51 +02:00
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
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
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
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
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
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
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
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
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
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
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
Johannes Altmanninger
8a7c3ceec3 Don't abandon line after writing control sequences
Commit 8164855b7 (Disable terminal protocols throughout evaluation, 2024-04-02)
changed where we output control sequences (to enable bracketed paste and CSI).
Likewise, f285e85b0 (Enable focus reporting only just before reading from
stdin, 2024-04-06) added control sequence output just before we read().

This output causes problems because it invalidates our stdout/stderr
timestamps, which causes us to think that a rogue background process wrote
to the terminal; we react by abandoning the current line and redrawing the
prompt below. Our fix was to refresh the TTY timestamps after we run a bind
command that might add stdout (#3481).

Since commit c3cd68dda (Process shell commands from bindings like regular
char events, 2024-03-02), this timestamp refresh logic is in the wrong place;
shell commands are run later now; we could move it but wait -

... we also need to make sure to refresh timestamps after outputting control
sequences.  Since bracketed paste is enabled after CSI u, we can skip the
latter.  Additionally, since we currently output control sequences before
every single top-level interactive command, we no longer need to separately
refresh timestamps in between commands.

Fixes #10409
2024-04-06 17:45:55 +02:00
Johannes Altmanninger
de730b7885 Extract function for running commands from bindings 2024-04-06 17:45:55 +02:00
Johannes Altmanninger
f285e85b0c Enable focus reporting only just before reading from stdin
Some terminals send the focus-in sequences ("^[I") whenever focus reporting is
enabled.  We enable focus reporting whenever we are finished running a command.
If we run two commands without reading in between, the focus sequences
will show up on the terminal.

Fix this by enabling focus-reporting as late as possible.

This fixes the problem with `^[I` showing up when running "cat" in
gnome-terminal https://github.com/fish-shell/fish-shell/issues/10411.

This begs the question if we should do the same for CSI u and bracketed paste.
It's difficult to answer that; let's hope we find motivating test cases.
If we enable CSI u too late, we might misinterpret key presses, so for now
we still enable those as early as possible.

Also, since we now read immediately after enabling focus events, we can get
rid of the hack where we defer enabling them until after the first prompt.
When I start a fresh terminal, the ^[I no longer shows up.
2024-04-06 11:22:19 +02:00
Johannes Altmanninger
7ffe023735 builtin read: enable terminal protocols again
It's not clear whether builtin read should be able to do everything
that the normal prompt does but I guess we haven't found a problem yet.
Given that read could be used to read a single character at a type,
it's a bit odd to toggle terminal protocols all the time.
But that's not the typical case (at least not for when stdin is a TTY),
and it seems fine.

Teste with

    bind ctrl-4 'echo yay'

Regressed in 8164855b7 (Disable terminal protocols throughout evaluation,
2024-04-02).
2024-04-06 11:22:19 +02:00
Johannes Altmanninger
629cad66a3 Clean up log statement 2024-04-06 11:22:19 +02:00
Fabian Boehm
ce92472af1 input: Comment out flogs
These are *extremely* chatty.

If they are needed we should add them to a subcategory like `input` or
`reader-input` so you can easily disable them.
2024-04-03 20:15:17 +02:00
Fabian Boehm
1f2e0617d1 tests: Pass correct length for buffer
This allocated 64 bytes and then told snprinf it was 128. That's a
no-no even if we never need that much.
2024-04-03 20:15:17 +02:00
Johannes Altmanninger
66c6e89f98 Don't add collateral sentinel key to input queue
This is for bracketed paste and focus reporting where we already add a proper
event to the queue.
2024-04-03 20:02:08 +02:00
Johannes Altmanninger
1baa893e60 Don't end history search on focus in/out events
Apparently VTE terminals send the "focus in" event whenever we re-enable
focus reporting. That's probably a sensible thing to do.

Anyway, our problem is simply that we accidentally end history search on these
focus events which are implemented as anonymous (unmappable) readline cmds.
Perhaps there should be a separate cmd category.

Focus events show up as key::Invalid which is a weird private use code point;
probably we can get rid of this key..

Fixes #10411
2024-04-03 20:02:08 +02:00
Johannes Altmanninger
350598cb99 Decode arrow keys as sent by urxvt
Not sure if we want to support this indefinitely but appears to be free as
of today.
2024-04-03 19:37:03 +02:00
Fabian Boehm
e8eb4822ce input: Fix crash for weird bracketed paste
I can reproduce by pasting after

```fish
echo \cc foo | fish_clipboard_copy
```

in Wezterm
2024-04-03 16:30:38 +02:00
Johannes Altmanninger
af1b599818 On redo, restore pre-undo cursor position 2024-04-03 13:09:27 +02:00
Fabian Boehm
fe95e4f4cd curses: Remove f13-f20
No longer supported by keys, and these are not a thing in the real world
2024-04-02 21:33:54 +02:00
Johannes Altmanninger
8164855b70 Disable terminal protocols throughout evaluation
Test changes are very hacky, will cleanup later.

Closes #10408
2024-04-02 21:25:47 +02:00
Johannes Altmanninger
695b408396 kitty keyboard protocol: decode numlock keys
Also disable the legacy matching hack for CSI u sequences, to prevent bindings
from treating this as prefix.
2024-04-02 18:20:35 +02:00
Johannes Altmanninger
b04dee358e Don't translate \n to enter
Apparently it's never entere because we turn off ICRNL.

I'm not sure why it says "no binding found".
2024-04-02 17:59:40 +02:00
Johannes Altmanninger
e8e91c97a6 fish_key_reader: ignore sentinel key
Also, move the undo grouping for paste to the right place.
2024-04-02 16:48:25 +02:00
Johannes Altmanninger
8bf8b10f68 Extended & human-friendly keys
See the changelog additions for user-visible changes.

Since we enable/disable terminal protocols whenever we pass terminal ownership,
tests can no longer run in parallel on the same terminal.

For the same reason, readline shortcuts in the gdb REPL will not work anymore.
As a remedy, use gdbserver, or lobby for CSI u support in libreadline.

Add sleep to some tests, otherwise they fall (both in CI and locally).

There are two weird failures on FreeBSD remaining, disable them for now
https://github.com/fish-shell/fish-shell/pull/10359/checks?check_run_id=23330096362

Design and implementation borrows heavily from Kakoune.

In future, we should try to implement more of the kitty progressive
enhancements.

Closes #10359
2024-04-02 14:35:16 +02:00
Johannes Altmanninger
22717339b4 fish_clipboard_paste: don't bypass pager search field.
To do so add an ad-hoc "commandline --search-field" to operate on pager
search field.

This is primarily motivated because a following commit reuses the
fish_clipboard_paste logic for bracketed paste. This avoids a regression.
2024-04-02 14:35:16 +02:00
Johannes Altmanninger
d0cdb142de Make CharEvent a native enum 2024-04-02 14:35:16 +02:00
Johannes Altmanninger
aa40c3fb7e Remove set-mode char event
Use generic shell commands instead.  This keeps us honest.

No functional change expected.
2024-04-02 14:35:16 +02:00
Johannes Altmanninger
20bbdb68fa Set terminal title unconditionally
Terminal titles are set with an OSC 0 sequence.  I don't think we want to
support terminals that react badly to unknown OSC (or CSI) sequences.

So let's remove our feature detection.

This will fix future false negatives along the lines of
https://github.com/fish-shell/fish-shell/pull/10037
2024-04-02 14:35:16 +02:00
Johannes Altmanninger
a216b3cf6a Print panic message to stderr, like the stack trace 2024-04-02 07:34:19 +02:00
Johannes Altmanninger
1232cfd3bb Fix typo 2024-04-02 07:33:07 +02:00
Johannes Altmanninger
af6dc9221f Use panic::set_hook instead of catch_unwind to help debug panics 2024-04-02 07:27:22 +02:00
Mahmoud Al-Qudsi
41eaf2f8dc
Merge pull request #10398 from mqudsi/forward-char-passive
Add `forward-char-passive` and `backward-char-passive`
2024-03-30 22:29:46 -05:00