Commit Graph

5378 Commits

Author SHA1 Message Date
Johannes Altmanninger
ff476eff2d Read \e prefix for escape sequences as alt modifier
The \e\e\[A style is bad but iTerm and putty (alt-left) use it.

The main motivation for this change is to improve fish_key_reader output.

Part of #10663
2024-08-11 11:31:13 +02:00
Johannes Altmanninger
d32825ba57 Decode formatOtherKeys=0 format (XTerm default) too
Part of #10663
2024-08-11 11:31:13 +02:00
Peter Ammon
2b40c6364e
Mark some functions as potentially unused
Fixes warnings on macOS
2024-08-10 17:36:56 -07:00
Johannes Altmanninger
4664a0b52f Limit iTerm2 CSI u workaround to iTerm<=3.5.3
Looks like 3.5.4 will include
9cd0241afd
so the need for the workaround is gone.

See #10653
2024-08-10 08:17:35 +02:00
Dezhi Wu
09b8b94025 Remove unnecessary unsafe block
The `unsafe` is not needed here, as the `select64` is already a safe wrapper
around the `C_select64` function.

Closes #10659
2024-08-10 07:16:04 +02:00
Johannes Altmanninger
5f9e9cbe74 Replace clock_gettime/futimens with 64-bit wrappers
Part of #10634
2024-08-07 13:11:22 +02:00
Johannes Altmanninger
c1fba4663b Replace nanosleep with stdlib wrapper (which is still broken)
As of rust 1.78, the Unix stdlib implementation is affected by the same issue:

    pub fn sleep(dur: Duration) {
        let mut secs = dur.as_secs();
        let mut nsecs = dur.subsec_nanos() as _;

        // If we're awoken with a signal then the return value will be -1 and
        // nanosleep will fill in `ts` with the remaining time.
        unsafe {
            while secs > 0 || nsecs > 0 {
                let mut ts = libc::timespec {
                    tv_sec: cmp::min(libc::time_t::MAX as u64, secs) as libc::time_t,
                    tv_nsec: nsecs,
                };
                secs -= ts.tv_sec as u64;
                let ts_ptr = core::ptr::addr_of_mut!(ts);
                if libc::nanosleep(ts_ptr, ts_ptr) == -1 {
                    assert_eq!(os::errno(), libc::EINTR);
                    secs += ts.tv_sec as u64;
                    nsecs = ts.tv_nsec;
                } else {
                    nsecs = 0;
                }
            }
        }
    }

Note that there is a small behavior change here -- sleep() will continue
after signals; I'm not sure if we want that but it seems harmless?

Part of #10634
2024-08-07 13:11:22 +02:00
Johannes Altmanninger
55196ee2a0 Replace pselect with a 64-bit-time_t wrapper
Part of #10634
2024-08-07 13:11:22 +02:00
Johannes Altmanninger
4992f88966 Replace getrusage with a 64-bit-time_t wrapper
Part of #10634
2024-08-07 13:11:22 +02:00
Johannes Altmanninger
46c8ba2c9f Replace select with a 64-bit-time_t wrapper
Part of #10634
2024-08-07 13:11:22 +02:00
Johannes Altmanninger
3a9b4149da Replace localtime_r with a 64-bit-time_t wrapper
Part of #10634
2024-08-07 13:11:22 +02:00
Johannes Altmanninger
70357c4f6e Add back .clang-format 2024-08-07 13:11:22 +02:00
Johannes Altmanninger
338579b78c Rename fstatat/readdir wrapper to match Linux equivalents 2024-08-07 13:11:22 +02:00
Johannes Altmanninger
d19e5508d7 Remove non-portable use of fstatat
Part of #10634
2024-08-06 14:16:01 +02:00
Johannes Altmanninger
0705446e6e Remove non-portable use of stat
Part of #10634
2024-08-06 14:16:01 +02:00
Johannes Altmanninger
ff47c2c628 Remove non-portable use of fstat
Part of #10634
2024-08-06 14:15:57 +02:00
Johannes Altmanninger
b64045dc18 Remove non-portable use of ino_t
Part of #10634
2024-08-06 14:15:03 +02:00
Johannes Altmanninger
fa4daeee0f Address clippy lint 2024-08-06 14:15:03 +02:00
Johannes Altmanninger
8fbe29ed7b Remove dead code 2024-08-06 14:15:03 +02:00
Johannes Altmanninger
7cfc6297bc Hack to make alt-{left,right} work again in iTerm2
iTerm2 deviates from protocol, so back out c3c832761 (Stop using stack for
kitty progressive enhancement, 2024-08-03) in that case.

Note that we use several ways of detecting iTerm2 (ITERM_PROFILE,
TERM_PROGRAM=iTerm.app, ITERM_SESSION_ID).
LC_TERMINAL seems superior because it works over ssh.

This new one should hopefully go away eventually.
2024-08-06 10:36:36 +02:00
Peter Ammon
1c38677db0
Minor cleanup of path_normalize_for_cd 2024-08-03 12:27:05 -07:00
Johannes Altmanninger
c3c8327610 Stop using stack for kitty progressive enhancement
Today fish pushes/pops kitty progressive enhancements everytime control is
transfered to/from fish. This constitutes a regression relative to 3.7.1:

    $ fish
    $ ssh somehost fish
    (network disconnect, now we missed our chance to pop from the stack)
    $ bash # or some ncurses application etc
    (keyboard shortcuts like ctrl-p are broken)

When invoking bash, we pop one entry off the stack but there is another one.
There seems to be a simple solution: don't use the stack but always reset
the current set of flags.  Do that since I did not find a strong use case
for using the stack[1] (Note that it was recommended by terminal developers
to use the stack, so I might be wrong).

Note that there is still a regression if the outer shell is bash.

[1]: https://github.com/kovidgoyal/kitty/issues/7603#issuecomment-2256949384

Closes #10603
2024-08-03 17:51:48 +02:00
Amos Bird
041a26f647 Update TTY modes for external commands only after successful command
According to the discussion in #2315, we adopt TTY modes for external commands
mainly for "stty".  If our child process crashes (or SSH disconnect), we
might get weird modes. Let's ignore the modes in the failure case.

Co-authored-by: Johannes Altmanninger <aclopte@gmail.com>

Part of #10603
2024-08-03 17:51:48 +02:00
Johannes Altmanninger
e25a1358e6 Work around broken rendering of pasted multibyte chars in non-UTF-8-ish locale
Run

    printf \Xf6 | wl-copy # ö in ISO-8859-1
    LANG=de_DE LC_ALL=$LANG gnome-terminal -- build/fish

and press ctrl-v. The pasted data looks like this:

    $ set data (wl-paste -n 2>/dev/null | string collect -N)
    $ set -S data
    $data: set in local scope, unexported, with 1 elements
    $data[1]: |\Xf6|

we pass $data directly to "commandline -i", which is supposed to insert it
into the commandline verbatim. What's actually inserted is "�".

This is because of all of:
1. We never decode "\Xf6 -> ö" in this scenario. Decoding it -- like we do
   for non-pasted keyboard input -- would fix the issue.
2. We've switched to using Rust's char, which, for better or worse, disallows
   code points that are not valid in Unicode (see b77d1d0e2 (Stop crashing
   on invalid Unicode input, 2024-02-27)). This means that we don't simply
   store \Xf6 as '\u{00f6}'. Instead we use our PUA encoding trick, making it
   \u{f6f6} internally.
3. Finally, b77d1d0e2 renders reserved codepoints (which includes PUA chars)
   using the replacement character � (sic).  This was deemed more
   user-friendly than printing an invalid character (which is probably not
   mapped to a glyph).  Yet it causes problems here: since we think that
   \u{f6f6} is garbage, we try to render the replacement character. Apparently
   that one is not defined(?) in ISO-8859-1; we get "�".

Fix this regression by removing the replacement character feature.

In future we should maybe decode pasted input instead. We could do that
lazily in "commandline -i", or eagerly in "set data (wl-paste ...)".
2024-08-03 11:32:59 +02:00
Johannes Altmanninger
d21ed0fb22 Disable terminal protocols before expanding wildcards
Commit 29f2da8d1 (Toggle terminal protocols lazily, 2024-05-16) made it so
the wildcard expansion in "echo **" (in a large directory tree) can't be
canceled with ctrl-c.  Fix this by disabling terminal protocols already at
expansion time (not waiting until execution).
2024-07-31 23:37:11 +02:00
Johannes Altmanninger
3be588569d Disable CSI u inside Midnight Commander for now
Using

    SHELL=$(command -v fish) mc

Midnight Commander will spawn a fish child with

    "function fish_prompt;"
    "echo \"$PWD\">&%d; fish_prompt_mc; kill -STOP %%self; end\n",

So fish_prompt will SIGSTOP itself using an uncatchable signal.

On ctrl-o, mc will send SIGCONT to give back control to the shell.
Another ctrl-o will be intercepted by mc to put the shell back to sleep.

Since mc wants to intercept at least ctrl-o -- also while fish is in control
-- we can't use the CSI u encoding until mc either understands that, or uses
a different way of passing control between mc and fish.

Let's disable it for now.

Note that mc still uses %self but we've added a feature flag
to disable that.  So if you use "set fish_features all"
you'll want to add a " no-remove-percent-self". A patch
to make mc use $fish_pid has been submitted upstream at
https://lists.midnight-commander.org/pipermail/mc-devel/2024-July/011226.html.

Closes #10640
2024-07-29 22:38:18 +02:00
Xiretza
fd006e02da Fix cd .. to the root directory
The leading slash always needs to be present, even if there aren't any other
components. This was introduced by the Rust port.
2024-07-29 10:23:29 -07:00
Peter Ammon
3d816174fd
Wildcard tree walking to only rely on dev, inode to detect changes
When applying a wildcard, it's important to keep track of the files that have
been visited, to avoid symlink loops. Previously fish used a FileId for the
purpose. However FileId also includes richer information like modification time;
thus if a file is modified during wildcard expansion then fish may believe that
the file is different and visit it twice.

The richer information like modification time is important for atomic file
writes but should be ignored for wildcard expansion; just use the (dev, inode)
pair instead.

This also somewhat reduces our reliance on struct stat, but we still need it for
fstatat which Rust does not expose.
2024-07-28 09:48:24 -07:00
Peter Ammon
89794ccfdb
Adopt fstat in screen.rs 2024-07-27 18:58:35 -07:00
Peter Ammon
3dc3aed991
Adopt fstat in file_id_for_fd 2024-07-27 18:58:34 -07:00
Peter Ammon
1332d33025
Introduce fstat() and adopt it in reader
Begin to migrate to Rust "native" Metadata, as part of addressing #10634

This will be structured as a series of small commits to aid bisecting.
2024-07-27 18:49:46 -07:00
Peter Ammon
0651ca0d9b
Unify FileId structs
We had two of these! Just use one.
2024-07-27 18:48:51 -07:00
Johannes Altmanninger
c3cf3792f3 Expand tilde after brace expansion
Fixes #10610
2024-07-23 11:47:58 +02:00
Dezhi Wu
7308dbc7ad fish_indent: Prevent overwriting file with identical content
Fixes #10616
2024-07-21 18:57:48 +08:00
Mahmoud Al-Qudsi
fe63775ec5 string: Also escape new lines with --style=regex
This isn't *required* in the PCRE2 spec but it greatly increases the utility of
escaped regex strings at the commandline.
2024-07-16 17:05:11 -05:00
Mahmoud Al-Qudsi
faf3b356f2 Add commandline --showing-suggestion
Returns 0 (true) in case an autosuggestion is currently being displayed.

This was first requested in #5000 then again in #10580 after the existing
workaround for this missing functionality was broken as part of a change to the
overall behavior of `commandline` (for the better).
2024-07-07 22:34:36 -05:00
Mahmoud Al-Qudsi
4730a04f25 Use NonZero types for 1-based line numbers
Since we have a mix of both 0-based and 1-based line numbers in the code base,
we can now distinguish between them by type alone. Also stop using 0 as a
placeholder value for "no line number available" except in explicit helper
functions such as `get_lineno_for_display()`.
2024-07-07 20:58:09 -05:00
Mahmoud Al-Qudsi
92cae9b576 Reduce size of Block to 32 bytes
Using a 32-bit integer to store the line number, as previously discussed.
2024-07-07 20:37:04 -05:00
Fabian Boehm
70242ce38c reader: Fix external modes again
Missed in the port
2024-07-03 20:39:25 +02:00
Mahmoud Al-Qudsi
93f8385448 Add option to speed up string match/replace with --max-matches
I've often needed a way to get the last bit of performance out of unwieldy
completions that involve a lot of string processing (apt completions come to
mind, and I ran into it just now with parsing man pages for kldload
completions).

Since many times we are looking for just one exact string in the haystack, an
easy optimization here is to introduce a way for `string match` or `string
replace` to early exit after a specific number of matches (typically one) have
been found.

Depending on the size of the input, this can be a huge boon. For example,
parsing the description from FreeBSD kernel module man pages with

    zcat /usr/share/man/man4/zfs.4.gz | string match -m1 '.Nd *'

runs 35% faster with -m1 than without, while processing all files under
/usr/share/man/man4/*.4.gz in a loop (so a mix of files ranging from very short
to moderately long) runs about 10% faster overall with -m1.
2024-06-30 17:51:50 -05:00
Peter Ammon
204663f1b8
Minor cleanup of apply_var_assignments
Simplify the function signature and calls sites.
2024-06-30 15:27:49 -07:00
Peter Ammon
b5fd8d697b
Minor refactor of completer perform_for_commandline 2024-06-30 13:59:22 -07:00
Nikita Bobko
67e190876a
Implement jump-till-matching-bracket input function
Part of #1842

It's like jump-to-matching-bracket, but jumps right before the bracket

I will use it to mimic vi 'ab' and 'ib' text objects in the next commit

Given complicated semantics of jump-till-matching-bracket, an alternative name
could be 'jump-inside-matching-brackets'. But that would make names non-symmetrical.
I'm not sure what is worse.
2024-06-30 11:58:10 -07:00
Nikita Bobko
f8ebe346a9
Implement jump-to-matching-bracket motion and bind % (percent) in vi mode
Part of #1842
2024-06-30 11:58:10 -07:00
Nikita Bobko
c966c19c56
Refactoring. Decompose ReaderData.jump function to two functions
Part of #1842

Split to:
- jump_and_remember_last_jump. What previously was called jump, now called
  jump_and_remember_last_jump
- jump. Only jump, don't remember last jump. Now it's also possible to pass
  vector of targets

The commit is pure refactoring, no functional changes are introduced.
The refactoring is needed for the next commits
2024-06-30 11:58:10 -07:00
Peter Ammon
1cbd18cc30
Tweak the allowed clippy set and fix some 2024-06-30 11:38:15 -07:00
Peter Ammon
90535d5b51
Allow clippy::ptr_arg and fix cases 2024-06-29 18:48:49 -07:00
Peter Ammon
cce2aab371
Rename binary_semaphore_t to BinarySemaphore 2024-06-29 18:07:11 -07:00
Peter Ammon
266852327f
Rename fd_readable_set_t to FdReadableSet 2024-06-29 18:06:21 -07:00
Peter Ammon
2d35d3f3c7
Remove yet more dead code 2024-06-29 18:03:52 -07:00