Commit Graph

5550 Commits

Author SHA1 Message Date
Johannes Altmanninger
5e3fdf3320 Fix regression causing crash when we should clamp negative wcwidth
Some checks are pending
make test / ubuntu (push) Waiting to run
make test / ubuntu-32bit-static-pcre2 (push) Waiting to run
make test / ubuntu-asan (push) Waiting to run
make test / macos (push) Waiting to run
Rust checks / rustfmt (push) Waiting to run
Rust checks / clippy (push) Waiting to run
Fixes c41dbe455 (Also use control pictures for pager prefix,
2024-10-19).

Fixes #10836
2024-11-07 07:36:36 +01:00
Fabian Boehm
373c5b1e14 fixup! Cursor visible sequence
Some checks are pending
make test / ubuntu (push) Waiting to run
make test / ubuntu-32bit-static-pcre2 (push) Waiting to run
make test / ubuntu-asan (push) Waiting to run
make test / macos (push) Waiting to run
Rust checks / rustfmt (push) Waiting to run
Rust checks / clippy (push) Waiting to run
Whoops, picked the wrong part of cnorm
2024-11-06 19:03:50 +01:00
Fabian Boehm
56718c31b1 reader: Comment the OSC 133 marker sequences 2024-11-06 19:01:17 +01:00
Fabian Boehm
c1fbe237c9 Make cursor visible after commands
Just like we already fix terminal modes if a command left them broken,
having an invisible cursor makes the terminal hard to use and so we
fix it.

We can't really use cnorm/cursor_normal because that often includes
other gunk like making the cursor blink, but it turns out every
terminfo entry agrees on the sequence to make the cursor visible, so
we hardcode it.

Fixes #10834
2024-11-06 19:01:17 +01:00
Fabian Boehm
bfc68345c9 Disable CSI u in Jetbrains terminals
Note: This may not be sent in WSL.

Fixes #10829
2024-11-06 19:01:04 +01:00
Mahmoud Al-Qudsi
33a170d614 Replace INVALID_PID constant with Option<NonZeroU32>
If we end up using this in more places, we can create a `Pid` newtype.
Note that while the constant is no longer used in code, its previous value of -2
is still printed by `jobs` when no pgid is associated with a job. I will open a
PR to change this to something else, likely either `0` or `-`.
2024-11-04 16:19:22 -06:00
Mahmoud Al-Qudsi
d1a2923d72 Fix doc comments for CancelBehavior 2024-11-04 15:49:33 -06:00
Peter Ammon
23941ea9ca
Don't try locking the history file if mmap returns ENODEV
Some checks are pending
make test / ubuntu (push) Waiting to run
make test / ubuntu-32bit-static-pcre2 (push) Waiting to run
make test / ubuntu-asan (push) Waiting to run
make test / macos (push) Waiting to run
Rust checks / rustfmt (push) Waiting to run
Rust checks / clippy (push) Waiting to run
If we try to memory map the history file, and we get back ENODEV meaning that
the underlying device does not support memory mapping, then treat that as a hint
that the filesystem is remote and disable history locking.
2024-11-02 12:09:51 -07:00
Peter Ammon
344b072e82
Further expand the list of filesystems considered remote
Incorporate additional file systems from
https://github.com/coreutils/gnulib/blob/master/lib/mountlist.c#L237-L253
by hunting down their magic numbers.

In the future we could consider switching to f_fstypename.
2024-11-02 11:59:34 -07:00
Johannes Altmanninger
cfcf415db7 Render overflown commandline in entirety just before executing
Some checks are pending
make test / ubuntu (push) Waiting to run
make test / ubuntu-32bit-static-pcre2 (push) Waiting to run
make test / ubuntu-asan (push) Waiting to run
make test / macos (push) Waiting to run
Rust checks / rustfmt (push) Waiting to run
Rust checks / clippy (push) Waiting to run
As of 04c913427 (Limit command line rendering to $LINES lines,
2024-10-25), we only render a part of the command line.  This removes
valuable information from scrollback.
The reasons for the limit were
1. to enable redrawing the commandline (can't do that if part of it
   is off-screen).
2. if the cursor is at the beginning of the command-line, we can't
   really render the off-screen suffix (unless we can tell the terminal
   to scroll back after doing that).

Fortunately these don't matter for the very last rendering of a
command line.  Let's render the entire command just before executing,
fixing the scrollback for executed commands.

In future, we should fix it also for pre-execution renderings. This
needs a terminal command to clear part of the scrollback.  Can't find
anything on https://invisible-island.net/xterm/ctlseqs/ctlseqs.html
There is "Erase Saved Lines" but that deletes the entire scrollback.

See the discussion in #10827
2024-11-02 10:25:58 +01:00
Johannes Altmanninger
04d97e936a Remove redundant cursor position invariant check
Since f89909ae3 (Also handle overflown screens if editing pager search
field, 2024-10-27), cursor_arr is never None after the loop.
Assert that by unwrapping.

qa.sh
2024-11-02 10:16:45 +01:00
Johannes Altmanninger
f42beec42e Fix typo in comment 2024-11-02 10:13:37 +01:00
Johannes Altmanninger
85404bf7a9 edit_command_buffer: speed up setting cursor position by line/column
Some checks are pending
make test / ubuntu (push) Waiting to run
make test / ubuntu-32bit-static-pcre2 (push) Waiting to run
make test / ubuntu-asan (push) Waiting to run
make test / macos (push) Waiting to run
Rust checks / rustfmt (push) Waiting to run
Rust checks / clippy (push) Waiting to run
alt-e restores the cursor position received from the editor, moving by
one character at a time.  This can be super slow on large commandlines,
even on release builds.  Let's fix that by setting the coordinates
directly.
2024-11-01 20:09:55 +01:00
Johannes Altmanninger
3710e98d65 Suppress spurious error when config dir creation fails due to TOCTOU
Some checks failed
Rust checks / rustfmt (push) Has been cancelled
Rust checks / clippy (push) Has been cancelled
make test / ubuntu (push) Has been cancelled
make test / ubuntu-32bit-static-pcre2 (push) Has been cancelled
make test / ubuntu-asan (push) Has been cancelled
make test / macos (push) Has been cancelled
Our recursive create_dir() first calls stat() to check if the directory
already exists and then mkdir() trying to create it. If another (fish)
process creates the same directory after our stat() but before our
mkdir(), then our mkdir() fails with EEXIST. This error is spurious
if there is already a directory at this path (and permissions are
correct).

Let's switch to the stdlib version, which promises to solve this issue.
They currently do it by running mkdir() first and ask stat() later.

This implies that they will only return success even if we don't have
any of rwx permissions on the directory, but that was already a problem
before this change. We silently don't write history in that case..

Fixes #10813
2024-10-31 08:01:31 +01:00
Johannes Altmanninger
cd3b6f9124 commandline --showing-suggestion to ignore single-space autosuggestion
Some checks are pending
make test / macos (push) Waiting to run
make test / ubuntu (push) Waiting to run
make test / ubuntu-32bit-static-pcre2 (push) Waiting to run
make test / ubuntu-asan (push) Waiting to run
Rust checks / rustfmt (push) Waiting to run
Rust checks / clippy (push) Waiting to run
All-whitespace autocompletions are invisible, no matter the cursor
shape.  We do offer such autosuggestions after typing a command name
such as "fish". Since the autosuggestion is invisible it's probably
not useful. It also does no harm except when using a binding like

	bind ctrl-g '
	    if commandline --showing-suggestion
	        commandline -f accept-autosuggestion
	    else
	        up-or-search
	    end'

where typing "fish<ctrl-g>" surprisingly does not perform a history
search.  Fix this by detecting this specific case. In future we
could probably stop showing autosuggestions whenever they only
contain whitespace.
2024-10-30 06:25:27 +01:00
Peter Ammon
e322d3addc Expand the set of filesystems considered remote on Linux
Some background: fish has some files which should be updated atomically:
specifically the history file and the universal variables file. If two fish
processes modified these in-place at the same time, then that could result
in interleaved writes and corrupted files.

To prevent this, fish uses the write-to-adjacent-file-then-rename to
atomically swap in a new file (history is slightly more complicated than
this, for performance, but this remains true). This avoids corruption.

However if two fish processes attempt this at the same time, then one
process will win the race and the data from the other process will be lost.
To prevent this, fish attempts to take an (advisory) lock on the target
file before beginning this process. This prevents data loss because only
one fish instance can replace the target file at once. (fish checks to
ensure it's locked the right file).

However some filesystems, particularly remote file systems, may have locks
which hang for a long time, preventing the user from using their shell.
This is far more serious than data loss, which is not catastrophic: losing
a history item or variable is not a major deal. So fish just attempts to
skip locks on remote filesystems.

Unfortunately Linux does not have a good API for checking if a filesystem
is remote: the best you can do is check the file system's magic number
against a hard-coded list. Today, the list is NFS_SUPER_MAGIC,
SMB_SUPER_MAGIC, SMB2_MAGIC_NUMBER, and CIFS_MAGIC_NUMBER.

Expand it to AFS_SUPER_MAGIC, CODA_SUPER_MAGIC, NCP_SUPER_MAGIC,
NFS_SUPER_MAGIC, OCFS2_SUPER_MAGIC, SMB_SUPER_MAGIC, SMB2_MAGIC_NUMBER,
CIFS_MAGIC_NUMBER, V9FS_MAGIC which is believed to be exhaustive.

ALSO include FUSE_SUPER_MAGIC: if the user's home directory is some FUSE
filesystem, that's kind of sus and the fewer tricks we try to pull, the
better.
2024-10-27 21:10:45 -07:00
Peter Ammon
3e3aa08c28 Fix some dumb clippies
Some checks failed
make test / ubuntu (push) Waiting to run
make test / ubuntu-32bit-static-pcre2 (push) Waiting to run
make test / ubuntu-asan (push) Waiting to run
make test / macos (push) Waiting to run
Rust checks / rustfmt (push) Waiting to run
Rust checks / clippy (push) Waiting to run
Lock threads / lock (push) Has been cancelled
2024-10-27 18:20:49 -07:00
Johannes Altmanninger
f89909ae31 Also handle overflown screens if editing pager search field
As mentioned in 04c913427 (Limit command line rendering to $LINES
lines, 2024-10-25) our rendering breaks when the command line overflows
the screen and we have a pager search field.

Let's also apply the overflow logic in this case.

Note that the search field still works, it's just not visible.

In future we should maybe show a small search field (~4 lines) in
this case (removing 4 screen lines worth of command line).  But again,
this is not really important.
2024-10-27 08:17:56 +01:00
Johannes Altmanninger
adfa87d141 Fix glitch rendering commandline that overflows screen size
If the first physical line in the command line overflows the screen,
the cursor will be wrong and we'll fail to clear the prompt without
a manual ctrl-l.  Let's fix that, and also don't print the OSC 133
marker in this case.

Currently, when we are scrolled, the first line on the screen still
gets an indentation that would normally be filled by the prompt.
This happens even for soft-wrapped lines, so they might be
torn apart in weird ways here.

In future, we might paint the prompt here.  If not, the current
behavior for soft-wrapped lines is debatable but its' not super
important to fix. The main goal is to first get rid of glitches in
these edge cases.
2024-10-27 07:16:30 +01:00
Fabian Boehm
ca27e028df Silence unused imports for backports
Would be cool if there was a way to do this on future:: in general.
2024-10-26 22:28:37 +02:00
Fabian Boehm
0e62178320 Only apply kitty protocol MC hack in MC
This deactivated it everywhere
2024-10-26 22:24:22 +02:00
Johannes Altmanninger
bd9fee417b Use kitty keyboard protocol again for recent Midnight Commander
Some checks are pending
make test / ubuntu (push) Waiting to run
make test / ubuntu-32bit-static-pcre2 (push) Waiting to run
make test / ubuntu-asan (push) Waiting to run
make test / macos (push) Waiting to run
Rust checks / clippy (push) Waiting to run
Rust checks / rustfmt (push) Waiting to run
See https://midnight-commander.org/ticket/4597
2024-10-26 19:55:48 +02:00
Johannes Altmanninger
04c9134275 Limit command line rendering to $LINES lines
Some checks are pending
make test / ubuntu (push) Waiting to run
make test / ubuntu-32bit-static-pcre2 (push) Waiting to run
make test / ubuntu-asan (push) Waiting to run
make test / macos (push) Waiting to run
Rust checks / rustfmt (push) Waiting to run
Rust checks / clippy (push) Waiting to run
Render the command line buffer only until the last line we can fit
on the screen.

If the cursor pushes the viewport such that neither the prompt nor
the first line of the command line buffer are visible, then we are
"scrolled". In this case we need to make sure to erase any leftover
prompt, so add a hack to disable the "shared_prefix" optimization
that tries to minimize redraws.

Down-arrow scrolls down only when on the last line, and up-arrow always
scrolls up as much as possible.  This is somewhat unconventional;
probably we should change the up-arrow behavior but I guess it's a
good idea to show the prompt whenever possible.  In future we could
solve that in a different way: we could keep the prompt visible even
if we're scrolled. This would work well because at least the left
prompt lives in a different column from the command line buffer.
However this assumption breaks when the first line in the command
line buffer is soft-wrapped, so keep this approach for now.

Note that we're still broken when complete-and-search or history-pager
try to draw a pager on top of an overfull screen.  Will try to fix
this later.

Closes #7296
2024-10-25 17:35:42 +02:00
Johannes Altmanninger
50333d8d00 Fix code duplication in commandline rendering
Will use this in the next commit.
2024-10-25 17:11:54 +02:00
Mahmoud Al-Qudsi
9c960d6af8 Fix number of characters consumed for VT200 mouse tracking
Some checks are pending
make test / ubuntu (push) Waiting to run
make test / ubuntu-32bit-static-pcre2 (push) Waiting to run
make test / ubuntu-asan (push) Waiting to run
make test / macos (push) Waiting to run
Rust checks / rustfmt (push) Waiting to run
Rust checks / clippy (push) Waiting to run
It's a 9-char CSI and we've read 3 (`<ESC>[T`), so we need to read six more.
Verified against the previous C++ codebase and couldn't find a reason for the
change to consuming 10 chars in a `git blame` run.
2024-10-24 11:22:52 -05:00
Mahmoud Al-Qudsi
daa2f2d023 Document max CSI parameter count 2024-10-24 10:36:00 -05:00
Mahmoud Al-Qudsi
21860cbd39 Fix panic parsing CSIs
The array lengths were transposed, so attempting to parse a CSI with more than 4
parameters would go out of bounds and panic.
2024-10-24 10:28:04 -05:00
Johannes Altmanninger
2dbaf10c36 Also refresh TTY timestamps after external commands from bindings
Commit ba67d20b7 (Refresh TTY timestamps after nextd/prevd, 2024-10-13)
wasn't quite right because it also needs to fix it for arbitrary commands.

While at it, do this only when needed:
1. It seems to be only relevant for multiline prompts.
   Note that we can wait until after evaluation to check if the prompt is
   multiline, because repaint events go through the queue, see 5ba21cd29
   (Send repaint requests through the input queue again, 2024-04-19).
2. When the binding doesn't execute any external command, we probably don't
   need to fix up whatever the user printed. If they actually wanted to show
   output and print another prompt, they should currently use  "__fish_echo",
   to properly support multiline prompts. Bindings should produce no other
   output. What distinguishes external programs is that they can trigger this
   issue even if they don't  produce any output that remains visible in fish,
   namely by using the terminal's alternate screen.
   Would be nice if we could get rid of __fish_echo; I'm not yet sure how.

Fixes #10800
2024-10-21 12:13:00 +02:00
Johannes Altmanninger
30cba03bf9 Make SIGTERM handler async-signal-safe again
Some checks failed
make test / ubuntu (push) Waiting to run
make test / ubuntu-32bit-static-pcre2 (push) Waiting to run
make test / ubuntu-asan (push) Waiting to run
make test / macos (push) Waiting to run
Rust checks / rustfmt (push) Waiting to run
Rust checks / clippy (push) Waiting to run
Lock threads / lock (push) Has been cancelled
2024-10-21 09:30:47 +02:00
Johannes Altmanninger
2e4f98b51c Do not add a space after completing inside brace expansion
Some checks are pending
make test / ubuntu (push) Waiting to run
make test / ubuntu-32bit-static-pcre2 (push) Waiting to run
make test / ubuntu-asan (push) Waiting to run
make test / macos (push) Waiting to run
Rust checks / rustfmt (push) Waiting to run
Rust checks / clippy (push) Waiting to run
Another everyday annoyance, has been for many years.
2024-10-19 22:06:05 +02:00
Johannes Altmanninger
c41dbe4551 Also use control pictures for pager prefix
The test case shows that the pager rendering is not quite right.  It renders
'{\', leaving out the newline.  This rendering is ambiguous.

Let's fix it by rendering \n as control picture, like we do for other control
characters in the pager.
2024-10-19 22:05:49 +02:00
Johannes Altmanninger
f5c6829670 Fix pager being blank when token prefix contains newline
Given

    $ echo {\
    C

where C is the cursor.
Completions have prefix "{\\\n".
Since \n has a wcwidth of -1, this line always fails

    let prefix_len = usize::try_from(fish_wcswidth(&self.prefix));

This triggers uncovers a regression in 43e2d7b48 (Port pager.cpp, 2023-12-02),
where we end up computing comp_width=0 for all completions.

Fix this. Test in the next commit.

The C++ version added the prefix width only if the completion had a valid
width. That seems wrong, let's do it always (if the prefix width is valid).
2024-10-19 22:05:49 +02:00
Johannes Altmanninger
cd541575b4 Fix completion failing on unclosed brace with wildcard
Completion on ": {*," used to work but nowadays our attempt to wildcard-expand
it fails with a syntax error and we do nothing.  This behavior probably only
makes sense for the overflow case, so do that.
2024-10-19 22:04:54 +02:00
Johannes Altmanninger
3d5ef2bcf5 Fix inverted condition in panic handler
Fixes 139d204c (Restore terminal state again in panic handler, 2024-10-12).
2024-10-19 22:04:54 +02:00
Johannes Altmanninger
b625c566b1 Remove workaround for WezTerm configured with enable_kitty_keyboard=true
Some checks failed
Rust checks / clippy (push) Has been cancelled
make test / ubuntu (push) Has been cancelled
Rust checks / rustfmt (push) Has been cancelled
make test / ubuntu-32bit-static-pcre2 (push) Has been cancelled
make test / ubuntu-asan (push) Has been cancelled
make test / macos (push) Has been cancelled
On a German keyboard, with a German keymap, and this ~/.wezterm.lua

    local wezterm = require 'wezterm'
    local config = wezterm.config_builder()
    config.enable_kitty_keyboard = true
    return config

when I press shift+# (which is single quote)
WezTerm sends the CSI u encoding shift-'.

Because of this, we completely disable kitty progressive enhancements and
modifyOtherKeys on WezTerm.

It makes no sense for every single app to work around WezTerm violating the
protocol. All these workarounds just create unnecessary version dependencies.
Also our workaround is brittle; it breaks as soon as you're inside something
like SSH.
Least importantly, the workarond prevents users of English keyboard layouts
to easily use the new features.

Since it seems so easy to work around by settting "enable_kitty_keyboard = false",
and most importantly, since that's the default, it seems better to remove
the workaround to simplify the world.

See #10663
2024-10-17 11:30:30 +02:00
Fabian Boehm
de13e6f9af complete: Only describe commands if the function exists
This shells out to __fish_describe_command, but if the install is
incomplete that will trigger the command-not-found handler.
2024-10-15 13:09:02 +02:00
Johannes Altmanninger
a2dc0ef377 Revert "Lock history file before reading it"
Commit 5db0bd5 (Lock history file before reading it, 2024-10-09)
rewrites the history file in place instead of using rename().
By writing to the same file (with the same inode), it corrupts
our memory-mapped snapshot; mmap(3) says:

> It is unspecified whether modifications to the underlying object done
> after the MAP_PRIVATE mapping is established are visible through the
> MAP_PRIVATE mapping.

Revert it (it was misguided anyway).

Closes #10777
Closes #10782
2024-10-14 11:13:46 +02:00
Peter Ammon
9337c20c2e
Stop using the getrandom feature of the rand crate
This feature uses the "getentropy" function which is not supported on
macOS < 10.12.
2024-10-13 12:39:54 -07:00
Johannes Altmanninger
2dafe81f97 Builtin source to print error if missing both file argument and piped stdin
Some checks are pending
make test / ubuntu (push) Waiting to run
make test / ubuntu-32bit-static-pcre2 (push) Waiting to run
make test / ubuntu-asan (push) Waiting to run
make test / macos (push) Waiting to run
Rust checks / rustfmt (push) Waiting to run
Rust checks / clippy (push) Waiting to run
Closes #10774
2024-10-13 10:44:38 +02:00
Johannes Altmanninger
00875d0f83 Allow builtin source to read from non-regular files
Commit a91bf6d88 (builtin.c: builtin_source now checks that its argument is
a file., 2005-12-16) fixed an infinite loop for commands like "source /"
where the argument is a directory.

It did so by erroring out early unless the filename argument is a regular file.
This is too restrictive; it disallows reading from special files like /dev/null
and fifos.
Today we get a sensible error without this check, so remove it.
2024-10-13 10:44:38 +02:00
Johannes Altmanninger
ba67d20b7c Refresh TTY timestamps after nextd/prevd
This fixes a macOS-specific bug.  See 390b40e02 (Fix regression not refreshing
TTY timestamps after external command from binding, 2024-05-29) and 8a7c3ceec
(Don't abandon line after writing control sequences, 2024-04-06).

Fixes #10779
2024-10-13 08:17:22 +02:00
Johannes Altmanninger
5496247344 Avoid erasing OSC 133 prompt start marker with clr_eol
For multi-line prompts, we start each leading line with a clr_eol.  Immediately
before printing these prompt lines we emit the OSC 133 prompt start marker.
Some terminals such as tmux interpret make clr_eol delete such markers,
hence prompt navigation is broken.

Fix this by printing the marker only after clr_eol.

The scenario where this triggers is quite odd.  I haven't looked into why
the problem doesn't exist if I remove the recursive repaint request.

See https://github.com/tmux/tmux/issues/4183
Closes #10776
2024-10-12 19:00:16 +02:00
Johannes Altmanninger
69380c6c92 Remove redundant test setup
One function calls setup twice, and the other one is not a test so should
not be prefixed with "test_".
2024-10-12 13:32:19 +02:00
Johannes Altmanninger
a139d204c0 Restore terminal state again in panic handler
Our panic handler attempts a blocking read from stdin and only exits
after the user presses Enter.

This is unconventional behavior and might cause surprise but there is a
significant upside: crashes become more visible for terminals that don't
already detect crashes (see ecdc9ce1d (Install a panic handler to avoid
dropping crash stacktraces, 2024-03-24)).

As reported in 4d0aa2b5d (Fix panic handler, 2024-08-28), the panic handler
failed to exit fish if the panic happens on background threads.  It would
only exit the background thread (like autosuggestion/highlight/history-pager
performer) itself. The fix was to abort the whole process.
Aborting has the additional upside of generating a coredump.

However since abort() skips stack unwinding, 4d0aa2b5d makes us no longer
restore the terminal on panic. In particular, if the terminal supports kitty
progressive enhancements, keys like ctrl-p will no longer work in say,
a Bash parent shell.  So it broke 121680147 (Use RAII for restoring term
modes, 2024-03-24).

Fix this while still aborting to create coredumps.  This means we can't use
RAII (for better or worse).  The bad part is that we have to deal with added
complexity; we need to make sure that we set the AT_EXIT handler only after
all its inputs (like TERMINAL_MODE_ON_STARTUP) are initialized to a safe
value, but also before any damage has been done to the terminal. I guess we
can add a bunch of assertions.

Unfortunately, if a background thread panics, I haven't yet figured out how
to tell the main thread to do the blocking read.  So the trick of "Press
Enter to exit", which allows users to attach a debugger doesn't yet work for
panics in background threads.  We can probably figure that out later. Maybe
use pthread_kill(3)?  Of course we still create coredumps, so that's fine.
As a temporary workaround, let's sleep for a bit so the user can at least
see that there is a crash & stacktrace.

One ugly bit here is that unit tests run AT_EXIT twice but it should be
idempotent.
2024-10-12 13:28:55 +02:00
Johannes Altmanninger
468849dd54 Minor refactoring in panic handler
I don't think I really get why this newline is here. It moves the cursor
from the end of the newline to the beginning of the next line.  Maybe it
was added only for panics in background threads?  Either way it's fine.
2024-10-12 12:18:50 +02:00
Johannes Altmanninger
97581ed20f Do send bracketed paste inside midnight commander
It can handle it fine (well, it simply strips the control sequences..).
2024-10-12 12:18:50 +02:00
Johannes Altmanninger
0d9dfb307b Apply terminal protocol workarounds also in fish_key_reader
We don't care to check the latest value of these variables;
these should only be read on startup and are not meant to
be overridden by the user ever. Hence we don't need a parser.
2024-10-12 12:18:50 +02:00
Johannes Altmanninger
fe3e3b3b50 Fix potential assertion failure on SIGTERM
If SIGTERM is delivered to a background thread, a function call to sanitize
the reader state would crash in assert_is_main_thread(). In this case we
are about to exit so there's no need to fix the reader state. Skip it on
background threads.
2024-10-12 10:50:56 +02:00
Johannes Altmanninger
49b88868df Fix stripping of " (deleted)" from non-UTF8 paths to fish 2024-10-12 06:53:25 +02:00
Johannes Altmanninger
88e749e4ce fixup! Back out assertion that doesn't hold yet
Some checks are pending
make test / ubuntu (push) Waiting to run
make test / ubuntu-32bit-static-pcre2 (push) Waiting to run
make test / ubuntu-asan (push) Waiting to run
make test / macos (push) Waiting to run
Rust checks / rustfmt (push) Waiting to run
Rust checks / clippy (push) Waiting to run
2024-10-09 21:35:56 +02:00
Johannes Altmanninger
c5db7565cc Back out assertion that doesn't hold yet 2024-10-09 21:34:19 +02:00
Johannes Altmanninger
5db0bd5874 Lock history file before reading it
Some checks are pending
make test / ubuntu (push) Waiting to run
make test / ubuntu-32bit-static-pcre2 (push) Waiting to run
make test / ubuntu-asan (push) Waiting to run
make test / macos (push) Waiting to run
Rust checks / rustfmt (push) Waiting to run
Rust checks / clippy (push) Waiting to run
We use optimistic concurrency when rewriting the history file to
minimize the lock scope. Unfortunately, old.mtime == new.mtime
does not imply that file is unchanged; we don't have guarantees
on the granularity of the modification time timestamp, see
https://stackoverflow.com/questions/14392975/timestamp-accuracy-on-ext4-sub-millsecond

So let's lock before reading any old contents and use the other
"write-to-tempfile-and-rename" code path only when locking fails.

Potentially fixes #10300
(untested) which probably happens because read_zero_padded() attempts to
read bytes that have not been flushed yet.
2024-10-09 14:51:54 +02:00
Johannes Altmanninger
35ee5e661f history: rename target_fd_after to target_file_after
This was forgotten in decf99f71 (Use `File` instead of `OwnedFd` in a few
places (#10355), 2024-03-17).
2024-10-09 14:51:54 +02:00
Johannes Altmanninger
f906a949cf Temporarily enable history_file debug category by default
All of these should never happen so let's enable them to hopefully get useful
bug reports.  Should disable it again before a release.

See #10300
2024-10-09 14:51:54 +02:00
Johannes Altmanninger
13e5d8097c Log more history_file errors, and add more context
See #10300
2024-10-09 14:51:54 +02:00
Johannes Altmanninger
29a01eb3cf Fix EINTR handling when importing history from bash 2024-10-09 14:48:58 +02:00
Johannes Altmanninger
ffedcdaac3 Do not interpret unknown file systems as local on Linux
No functional change, since with the parent commit, we no longer treat
"DirRemoteness::local" different from "DirRemoteness::remote", but we might
do so in future, so make sure we don't give a false positive here.

Non-Linux systems have ST_LOCAL or MNT_LOCAL, so no unknowns there.

See #10434
2024-10-09 14:48:58 +02:00
Johannes Altmanninger
dc5823d150 Fix history merge on all network file systems
mmap() fails with ENODEV on remote file systems. This means we always fail
to read any old history on network file systems on Linux (except on the file
systems we recognize which are NFS, SMB and CIFS).

Untested, so I'm not sure if this works.

Fixes #10434
2024-10-09 14:48:58 +02:00
Johannes Altmanninger
37c04745e6 Avoid potential contention on SIGTERM while enabling terminal protocols
We no longer use RAII for enabling/disabling these, so a full object is
overkill.  Additionally this object doesn't allow us to recover from the case
where we receive SIGTERM while inside terminal_protocols_{enable,disable}.
We can simply run disable another time since they're idempotent. Untested.
2024-10-09 13:05:25 +02:00
Johannes Altmanninger
2b873570a8 Fix typo 2024-10-09 12:36:58 +02:00
Johannes Altmanninger
efa109b62e Add default-enabled error log when there is a corrupted history entry
Some checks are pending
make test / ubuntu (push) Waiting to run
make test / ubuntu-32bit-static-pcre2 (push) Waiting to run
make test / ubuntu-asan (push) Waiting to run
make test / macos (push) Waiting to run
Rust checks / rustfmt (push) Waiting to run
Rust checks / clippy (push) Waiting to run
We'll drop the corrupted item on the next vacuum, so this shouldn't be
too annoying, and hopefully helps to narrow down #10300 further.
2024-10-06 11:48:51 +02:00
Johannes Altmanninger
9c4d31f89a Make errors in the history_file log category human-readable 2024-10-06 11:42:30 +02:00
Johannes Altmanninger
f36f757fa6 Never rewrite history file when adding ephemeral items
When I run a command with leading space, it is not added to the on-disk
history.  However we still call History::save().  After 25 of such calls,
we rewrite the history file (even though nothing was written by us).

This is annoying when diagnosing #10300 where the history of the current
shell (but not other shells) is broken; because the history rewrite will
make the problem go away. Let's not save in this case, to make it easier to
run commands to inspect the state of the history file.
2024-10-06 08:30:53 +02:00
Johannes Altmanninger
33bcf4d0ce Remove redundant drop 2024-10-06 08:30:53 +02:00
Johannes Altmanninger
1b9b893169 After reading corrupted history entry, keep reading older entries
Given a history like

    - cmd: echo OLD
      when: 1726157160
    \x00\x00\x00- cmd: echo leading NUL bytes
      when: 1726157160
    - cmd: echo NEW
      when: 1726157223

offset_of_next_item() happily records 3 items even though the second item
is corrupted.
decode_item() fails which makes the caller stop loading any older items --
we got knee capped.

Avoid this horrible failure mode by skipping over these items already in
offset computation. For now we still lose the corrupted item itself.

In future we should probably try to delete the NUL bytes or avoid the
corruption in the first place.

See #10300 and others.
2024-10-06 00:28:26 +02:00
diniamo
052e764f29 accept-autosuggestion to return false if there was no autosuggestion to accept
Example usage:

    bind ctrl-space accept-autosuggestion and execute

Closes #10608
2024-10-05 23:43:16 +02:00
Jacob Chapman
a9cee9e755 Commands to move by entire tokens
Some checks are pending
make test / ubuntu (push) Waiting to run
make test / ubuntu-32bit-static-pcre2 (push) Waiting to run
make test / ubuntu-asan (push) Waiting to run
make test / macos (push) Waiting to run
Rust checks / rustfmt (push) Waiting to run
Rust checks / clippy (push) Waiting to run
ja: I'll try to add default bindings in a follow-up PR.

Closes #10738
Closes #2014
2024-10-05 22:43:39 +02:00
Fabian Boehm
5e8adb18f4 complete: Sort --keep-order completions smaller
This should make the sort have a strict weak ordering, which rust
requires since 1.81 (or it will panic).

Note: This changes the order, but that's *fine* since the current
order is random weirdness anyway.

Fixes #10763
2024-10-05 13:53:02 +02:00
Fabian Boehm
2238c07b91 path: Remove weird order hack
Some checks are pending
make test / ubuntu (push) Waiting to run
make test / ubuntu-32bit-static-pcre2 (push) Waiting to run
make test / ubuntu-asan (push) Waiting to run
make test / macos (push) Waiting to run
Rust checks / rustfmt (push) Waiting to run
Rust checks / clippy (push) Waiting to run
Part of #10763
2024-10-04 16:48:40 +02:00
Johannes Altmanninger
f6d5355d7a Bump iTerm workaround version
Some checks failed
make test / ubuntu-32bit-static-pcre2 (push) Has been cancelled
make test / ubuntu (push) Has been cancelled
make test / ubuntu-asan (push) Has been cancelled
make test / macos (push) Has been cancelled
Rust checks / rustfmt (push) Has been cancelled
Rust checks / clippy (push) Has been cancelled
This unnecessarily enables the workaround for some nightly versions.

See #10653
2024-09-30 11:00:03 +02:00
Johannes Altmanninger
4c43819d32 Fix crash indenting quoted suffix after command substitution
Some checks are pending
make test / ubuntu (push) Waiting to run
make test / ubuntu-32bit-static-pcre2 (push) Waiting to run
make test / ubuntu-asan (push) Waiting to run
make test / macos (push) Waiting to run
Rust checks / rustfmt (push) Waiting to run
Rust checks / clippy (push) Waiting to run
Commit b00899179 (Don't indent multi-line quoted strings; do indent inside
(), 2024-04-28) made parse_util_compute_indents() crash on `echo "$()"'x`.
After recursively indenting the command substitution, we indent the "'x
suffix.  We skip the quoted part by setting "done=2".  Later we wrongly
index "self.indents[done..range.start+offset+1]" (= "self.indents[2..1]").

Fix this by making sure that "start >= done", thus not setting any indents
for the quoted suffix.  There is no need to do so; only the first character
in each line needs an indent.
2024-09-28 13:36:17 +02:00
Johannes Altmanninger
50314e309b Follow naming convention 2024-09-28 13:35:07 +02:00
Johannes Altmanninger
d00e900e5a Don't reattempt failing history pager search
In particular, this fixes the case

    ctrl-r foo ctrl-r

where foo substring-matches no more than one page's worth of results.
The second attempt will fall back to subsequence matching which is wrong.
2024-09-28 11:28:49 +02:00
Johannes Altmanninger
ae7fd770ee Extract history pager state 2024-09-28 11:26:50 +02:00
Johannes Altmanninger
263f1b35de Reapply "Clear to eol before outputting line in multi-line prompt"
In case a terminal resize[1] causes us
to repaint a multi-line prompt that changes width like

    function fish_prompt
        for i in 1 2 3
            random choice 'aaaaaaaaaaaaaaaaaaaaaaaaaaaaa' 'bbbbbbbbbbb'
        end
    end

we add a clr_eol after each line[2] , to make sure
that a "b" line does not have leftover "a" letters
(80aaae5b7 (Clear to end of each line in left prompt, 2020-10-25)).

Unfortunately, if a prompt line takes up all the columns, clr_eol will
wrongly clear the last column. Reproduce with

    function fish_prompt
        string repeat $COLUMNS -
        echo "$PWD> "
    end

and observe that the last "-" is missing.

Previous (reverted) attempt d3ceba107 (Clear to eol before outputting line
in multi-line prompt, 2021-05-17) found the right fix but had an off-by-one
error which reintroduced the leftover "a" letters in the "random choice"
prompt above.

Given prompt string "aa\nbb\ncc", it wrongly printed

    clr_eol "aa" clr_eol "\nbb" "\ncc"

Observe that the first line is cleared twice, while the second line is
never cleared. Fix that.

[1]: or an async "commandline -f repaint" triggered by a uvar change /
     async prompt update
[2]: except after the last line where we probably already emit clr_eol
     elsewhere..

Alternative fix: emit both clr_eol and clr_bol *before* drawing the current
line. However, if fish and the terminal disagree on character width, that
approach might erase too much.

Closes #8164
2024-09-28 10:39:54 +02:00
Fabian Boehm
48a6550688 Remove obsolete workaround for strftime on BSD
Some checks failed
make test / ubuntu (push) Has been cancelled
make test / ubuntu-32bit-static-pcre2 (push) Has been cancelled
make test / ubuntu-asan (push) Has been cancelled
make test / macos (push) Has been cancelled
Rust checks / rustfmt (push) Has been cancelled
Rust checks / clippy (push) Has been cancelled
This was added in libc 0.2.152, see aff5e66e54
2024-09-24 21:38:15 +02:00
Fabian Boehm
798150ac39 Update widecharwidth for Unicode 16
Commit 533e50efb0b9b122a08f2273337dbf6b44b03cc7 upstream.
2024-09-24 17:00:13 +02:00
Johannes Altmanninger
308ed62d83 fish_key_reader: stop emitting \n for ctrl-j
I guess it's nice to know that these two are the same but that info is not
needed here, it just adds confusion. The user must have pressed ctrl-j if
we get here, so echo that back.

See the parent commit.
2024-09-23 20:08:46 +02:00
Johannes Altmanninger
4336f9df7f fish_key_reader: stop emitting \b for ctrl-h
This is just too confusing; \b sounds like it would map backspace but it's
actually just ctrl-h.  Backspace is a different key ("bind backspace"),
so let's move away from \b.

Reproduce by typing ctrl-h in fish_key_reader, or, for even more confusion,
use a terminal like tmux and type ctrl-backspace which also sends ctrl-h.

I've thought about changing \b (and its aliases like \ch and \x08) to mean
backspace but that seems like unnecessary breakage, since they all already
mean ctrl-h, and can usually be mapped independent of backspace.

See the discussion in #10738
2024-09-23 20:00:35 +02:00
Peter Ammon
e3993a3d96
Update code and Cargo.toml for recent pcre2-utf32 changes 2024-09-22 17:05:14 -07:00
Peter Ammon
617b61cd3a
Clean up fd_monitor getter
No need for UnsafeCell
2024-09-22 14:02:55 -07:00
Peter Ammon
fb700ca50d
Clean up a few more comments 2024-09-22 13:11:40 -07:00
Peter Ammon
f733553ac8
Clean up some stale comments 2024-09-22 13:09:11 -07:00
Peter Ammon
974ad882fa
Clean up fish-printf in preparation for publishing
Make fish-printf no longer depend on the widestring crate, as other clients
won't use it; instead this is an optional feature.

Make format strings a generic type, so that both narrow and wide strings can
serve. This removes a lot of the complexity around converting from narrow to
wide.

Add a README.md to this crate.
2024-09-21 17:52:11 -07:00
Fabian Boehm
cdcf460edf math: Nicer error for non-ascii-lowercase identifiers
This gave a weird error when you did e.g. `math Foo / 6`:

"Missing Operator" and only the "F" marked.

Adding an operator here anywhere won't help, so calling this an
"Unknown function" is closer to the truth. We also get nicer markings
because we know the extent of the identifier.
2024-09-18 22:27:00 +02:00
Johannes Altmanninger
add0a9dfcd fish_indent: clean up file writing logic
Fix 7308dbc7a (fish_indent: Prevent overwriting file with identical content,
2024-07-21) in a different way by passing O_TRUNC again.
If we don't want regressions we could use code review.
2024-09-16 21:27:11 +02:00
Fabian Boehm
e27f4a3744 fish_indent: Truncate file to the size of the text
This can happen in case the formatted script is shorter, e.g. because
we ditched superfluous quotes.

Fixes #10724
2024-09-16 21:08:53 +02:00
Johannes Altmanninger
1e2368f609 Fix off-by-one-error parsing \e\e prefixed sequences
Closes #10721
2024-09-14 22:56:37 +02:00
Peter Ammon
8f3a034264
History to store old item offsets in Vec and not VecDeque
We used deque in C++ because this vector may be large, and so it avoids
repeated re-allocations. But VecDeque is different in Rust - it's contiguous -
so there's no benefit. Just use Vec.
2024-09-14 13:26:34 -07:00
Peter Ammon
7ac62bbca4
Remove a redundant "unsafe" specifier 2024-09-14 13:15:21 -07:00
Johannes Altmanninger
5432ee1aa9 Relax history autosuggestion and highlighting if cd is wrapped
For implementation reasons, we special-case cd in several ways
1. it gets different completions (handle_as_special_cd)
2. when highlighting, we honor CDPATH
3. we discard autosuggestions from history that don't have valid path arguments

There are some third-party tools like zoxide that redefine cd ("function cd
--wraps ...; ...; end"). We can't support this in general but let's try to
make an effort.

zoxide tries to be a superset of cd, so special case 1 is still
valid but 2 and 3 are not, because zoxide accepts some paths
that cd doesn't accept.

Let's add a hack to detect when "cd" actually means something else by checking
if there is any --wraps argument.

A cleaner solution is definitely possible but more effort.

Closes #10719
2024-09-14 08:51:42 +02:00
Kaley Main
a979b6341d Create a test that reproduces fish-shell/fish-shell#10703 2024-09-06 16:41:10 +02:00
Johannes Altmanninger
f8a720da8c Fix wildcard expansion doubling up "*/"
In some cases we add the wildcard twice.

    $ fish -c '../jj; complete -C"ls cli/*/conf/tem"'
    cli/*/*/config/templates.toml

Fix that. Test in the next commit.

There seems to be another bug in 3.7.1 where we fail to apply this completion
to the command line. This appears fixed. (FWIW we might want to revert
the quoting change in completion_apply_to_command_line(), maybe that one
accidentally fix this).

Fixes #10703
2024-09-06 16:41:10 +02:00
Johannes Altmanninger
77b2dcb462 Fix ctrl-c being ignored during builtin wait
Same as d21ed0fb2 (Disable terminal protocols before expanding wildcards,
2024-07-31).

Also mention a related issue in the changelog.
2024-09-01 14:08:01 +02:00
Johannes Altmanninger
15b08cbcab Make import style less noisy 2024-09-01 14:05:48 +02:00
Mahmoud Al-Qudsi
50a6dfd10d Replace a HashMap w/ a BTreeMap
The HashMap is used to generate the __fish_describe_command integration
completions. Given the nature of the allocations and the numbers that we use, a
BTreeMap would theoretically perform better. Benchmarks show a 2-9%
improvement in completion times consistently in favor of BTreeMap.
2024-08-31 14:13:41 -05:00
Mahmoud Al-Qudsi
73908f1218 fish_test_helper: Fix warnings about intentionally unused results
Warnings were appearing under GCC 13.2

(void) alone is insufficient under modern compilers, workaround with logical
negation taken from GCC bug tracker.
2024-08-31 13:16:51 -05:00
Mahmoud Al-Qudsi
ef6577db25 Update threads::thread_id() documentation 2024-08-31 12:57:13 -05:00
Mahmoud Al-Qudsi
c6bbacc703 Catch tls issues caused by linker bug
Worth including because mold is rather popular in the rust world and because the
bug affects mold versions coincident with the development of the fish rust port.

The bug affects all currently released versions of mold from 2.30.0 (Mar 2024)
onwards under at least FreeBSD (though quite likely other platforms as well).

See https://github.com/rui314/mold/issues/1338 for reference.
2024-08-31 12:48:15 -05:00
Mahmoud Al-Qudsi
5278259312 Don't break out of panic handler
The previous control flow logic wasn't sound and would leave the shell in a hung
state when `break` would be encountered.

The behavior is now straightforward, the shell reads until <Enter> or <q> is
pressed, at which point it aborts.
2024-08-28 17:38:05 -05:00