2016-05-08 16:27:15 -07:00
|
|
|
#
|
|
|
|
|
# Initializations that should only be performed when entering interactive mode.
|
|
|
|
|
#
|
|
|
|
|
# This function is called by the __fish_on_interactive function, which is defined in config.fish.
|
|
|
|
|
#
|
|
|
|
|
function __fish_config_interactive -d "Initializations that should be performed when entering interactive mode"
|
2022-01-09 14:28:41 +01:00
|
|
|
# For one-off upgrades of the fish version
|
|
|
|
|
if not set -q __fish_initialized
|
|
|
|
|
set -U __fish_initialized 0
|
|
|
|
|
end
|
|
|
|
|
|
2017-07-02 13:41:07 -07:00
|
|
|
set -g __fish_active_key_bindings
|
2016-05-08 16:27:15 -07:00
|
|
|
|
2019-02-14 17:01:28 -08:00
|
|
|
# usage: __init_uvar VARIABLE VALUES...
|
|
|
|
|
function __init_uvar -d "Sets a universal variable if it's not already set"
|
2019-02-14 21:42:42 -08:00
|
|
|
if not set --query $argv[1]
|
2019-02-14 17:01:28 -08:00
|
|
|
set --universal $argv
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
|
2016-09-12 18:47:59 -07:00
|
|
|
# If we are starting up for the first time, set various defaults.
|
2022-01-27 16:38:14 -08:00
|
|
|
if test $__fish_initialized -lt 3400
|
2022-08-14 07:05:26 -07:00
|
|
|
# Create empty configuration directores if they do not already exist
|
|
|
|
|
test -e $__fish_config_dir/completions/ -a -e $__fish_config_dir/conf.d/ -a -e $__fish_config_dir/functions/ ||
|
|
|
|
|
mkdir -p $__fish_config_dir/{completions, conf.d, functions}
|
|
|
|
|
|
|
|
|
|
# Create config.fish with some boilerplate if it does not exist
|
|
|
|
|
test -e $__fish_config_dir/config.fish || echo "\
|
|
|
|
|
if status is-interactive
|
|
|
|
|
# Commands to run in interactive sessions can go here
|
|
|
|
|
end" >$__fish_config_dir/config.fish
|
|
|
|
|
|
2025-04-26 09:11:47 +02:00
|
|
|
echo yes | fish_config theme save "fish default"
|
|
|
|
|
set -Ue fish_color_keyword fish_color_option
|
2016-05-08 16:27:15 -07:00
|
|
|
end
|
2025-04-26 09:11:47 +02:00
|
|
|
if test $__fish_initialized -lt 3800 && test "$fish_color_search_match[1]" = bryellow
|
|
|
|
|
set --universal fish_color_search_match[1] white
|
2024-04-15 09:06:00 +02:00
|
|
|
end
|
2007-08-20 02:42:30 +10:00
|
|
|
|
2013-11-11 21:14:24 +05:30
|
|
|
#
|
2016-05-08 15:57:56 -07:00
|
|
|
# Generate man page completions if not present.
|
2013-11-11 21:14:24 +05:30
|
|
|
#
|
2017-02-12 14:03:54 +01:00
|
|
|
# Don't do this if we're being invoked as part of running unit tests.
|
|
|
|
|
if not set -q FISH_UNIT_TESTS_RUNNING
|
2020-07-09 18:35:02 +02:00
|
|
|
# Check if our manpage completion script exists because some distros split it out.
|
|
|
|
|
# (#7183)
|
|
|
|
|
set -l script $__fish_data_dir/tools/create_manpage_completions.py
|
2024-04-27 15:45:28 +08:00
|
|
|
if not test -d $__fish_cache_dir/generated_completions; and test -e "$script"
|
2017-02-12 14:03:54 +01:00
|
|
|
# Generating completions from man pages needs python (see issue #3588).
|
|
|
|
|
|
|
|
|
|
# We cannot simply do `fish_update_completions &` because it is a function.
|
|
|
|
|
# We cannot do `eval` since it is a function.
|
|
|
|
|
# We don't want to call `fish -c` since that is unnecessary and sources config.fish again.
|
|
|
|
|
# Hence we'll call python directly.
|
|
|
|
|
# c_m_p.py should work with any python version.
|
2024-04-27 10:51:11 +02:00
|
|
|
set -l update_args -B $__fish_data_dir/tools/create_manpage_completions.py --manpath --cleanup-in $__fish_user_data_dir/generated_completions --cleanup-in $__fish_cache_dir/generated_completions
|
2020-04-28 13:05:20 +02:00
|
|
|
if set -l python (__fish_anypython)
|
|
|
|
|
# Run python directly in the background and swallow all output
|
Orphan background tasks to work around terminals being sensitive to unreaped processes
When a command like "long-running-command &" exits, the resulting SIGCHLD
is queued in the topic monitor. We do not process this signal immediately
but only after e.g. the next command has finished. Only then do we reap the
child process.
Some terminals, such as Terminal.app, refuse to close when there are unreaped
processes associated with the terminal -- as in, having the same session ID,
see setsid(3).
In future, we might want to reap proactively.
For now, apply an isolated workaround: instead of taking care of a child
process, double-fork to create an orphaned process. Since the orphan will
be reaped by PID 1, we can eventually close Terminal.app without it asking
for confirmation.
/bin/sh -c '( "$@" ) >/dev/null 2>&1 &' -- cmd arg1 arg2
This fix confines the problem to the period during which a background process
is running. To complete the fix, we would need to call setsid to detach the
background process from a controlling terminal. That seems to be desirable
however macOS does provide a setsid utility.
setsid cmd arg1 arg2 >/dev/null 2>&1
Fixes #11181
2025-02-28 03:58:51 +01:00
|
|
|
# Orphan the job so that it continues to run in case of an early exit (#6269)
|
|
|
|
|
/bin/sh -c '( "$@" ) >/dev/null 2>&1 &' -- $python $update_args
|
2017-02-12 14:03:54 +01:00
|
|
|
end
|
2017-01-28 20:37:32 -08:00
|
|
|
end
|
2013-11-09 11:29:10 +05:30
|
|
|
end
|
|
|
|
|
|
2016-05-08 16:27:15 -07:00
|
|
|
#
|
2016-05-08 15:57:56 -07:00
|
|
|
# Print a greeting.
|
2020-05-30 08:53:08 +02:00
|
|
|
# The default just prints a variable of the same name.
|
2016-05-08 16:27:15 -07:00
|
|
|
#
|
2020-06-04 16:57:35 +02:00
|
|
|
# NOTE: This status check is necessary to not print the greeting when `read`ing in scripts. See #7080.
|
|
|
|
|
if status --is-interactive
|
2020-05-30 08:53:08 +02:00
|
|
|
and functions -q fish_greeting
|
|
|
|
|
fish_greeting
|
2016-05-08 16:27:15 -07:00
|
|
|
end
|
|
|
|
|
|
|
|
|
|
#
|
|
|
|
|
# Completions for SysV startup scripts. These aren't bound to any
|
|
|
|
|
# specific command, so they can't be autoloaded.
|
|
|
|
|
#
|
2016-09-10 15:07:58 -07:00
|
|
|
if test -d /etc/init.d
|
|
|
|
|
complete -x -p "/etc/init.d/*" -a start --description 'Start service'
|
|
|
|
|
complete -x -p "/etc/init.d/*" -a stop --description 'Stop service'
|
|
|
|
|
complete -x -p "/etc/init.d/*" -a status --description 'Print service status'
|
|
|
|
|
complete -x -p "/etc/init.d/*" -a restart --description 'Stop and then start service'
|
|
|
|
|
complete -x -p "/etc/init.d/*" -a reload --description 'Reload service configuration'
|
|
|
|
|
end
|
2016-05-08 16:27:15 -07:00
|
|
|
|
2019-04-15 12:49:24 -07:00
|
|
|
#
|
|
|
|
|
# Only a few builtins take filenames; initialize the rest with no file completions
|
|
|
|
|
#
|
2024-11-21 08:55:45 +01:00
|
|
|
complete -c(builtin -n | string match -rv '(\.|:|source|cd|contains|count|echo|exec|fish_indent|printf|random|realpath|set|\\[|test|for)') --no-files
|
2019-04-15 12:49:24 -07:00
|
|
|
|
2016-05-08 16:27:15 -07:00
|
|
|
# Reload key bindings when binding variable change
|
|
|
|
|
function __fish_reload_key_bindings -d "Reload key bindings when binding variable change" --on-variable fish_key_bindings
|
2018-04-01 16:11:12 -07:00
|
|
|
# Make sure some key bindings are set
|
2019-02-14 17:01:28 -08:00
|
|
|
__init_uvar fish_key_bindings fish_default_key_bindings
|
2018-04-01 16:11:12 -07:00
|
|
|
|
2017-07-02 13:41:07 -07:00
|
|
|
# Do nothing if the key bindings didn't actually change.
|
2016-05-08 16:27:15 -07:00
|
|
|
# This could be because the variable was set to the existing value
|
2017-07-02 13:41:07 -07:00
|
|
|
# or because it was a local variable.
|
|
|
|
|
# If fish_key_bindings is empty on the first run, we still need to set the defaults.
|
2016-05-22 19:54:11 +02:00
|
|
|
if test "$fish_key_bindings" = "$__fish_active_key_bindings" -a -n "$fish_key_bindings"
|
2016-05-08 16:27:15 -07:00
|
|
|
return
|
|
|
|
|
end
|
2017-07-02 13:41:07 -07:00
|
|
|
# Check if fish_key_bindings is a valid function.
|
|
|
|
|
# If not, either keep the previous bindings (if any) or revert to default.
|
|
|
|
|
# Also print an error so the user knows.
|
2016-05-22 19:54:11 +02:00
|
|
|
if not functions -q "$fish_key_bindings"
|
|
|
|
|
echo "There is no fish_key_bindings function called: '$fish_key_bindings'" >&2
|
2016-11-27 14:12:10 +01:00
|
|
|
# We need to see if this is a defined function, otherwise we'd be in an endless loop.
|
|
|
|
|
if functions -q $__fish_active_key_bindings
|
2016-05-22 19:54:11 +02:00
|
|
|
echo "Keeping $__fish_active_key_bindings" >&2
|
2016-11-27 14:12:10 +01:00
|
|
|
# Set the variable to the old value so this error doesn't happen again.
|
|
|
|
|
set fish_key_bindings $__fish_active_key_bindings
|
2016-05-22 19:54:11 +02:00
|
|
|
return 1
|
2016-08-15 22:36:21 +02:00
|
|
|
else if functions -q fish_default_key_bindings
|
2016-05-22 19:54:11 +02:00
|
|
|
echo "Reverting to default bindings" >&2
|
|
|
|
|
set fish_key_bindings fish_default_key_bindings
|
|
|
|
|
# Return because we are called again
|
|
|
|
|
return 0
|
2016-08-15 22:36:21 +02:00
|
|
|
else
|
|
|
|
|
# If we can't even find the default bindings, something is broken.
|
|
|
|
|
# Without it, we would eventually run into the stack size limit, but that'd print hundreds of duplicate lines
|
|
|
|
|
# so we should give up earlier.
|
|
|
|
|
echo "Cannot find fish_default_key_bindings, falling back to very simple bindings." >&2
|
|
|
|
|
echo "Most likely something is wrong with your installation." >&2
|
|
|
|
|
return 0
|
2016-05-22 19:54:11 +02:00
|
|
|
end
|
|
|
|
|
end
|
2016-05-08 16:27:15 -07:00
|
|
|
set -g __fish_active_key_bindings "$fish_key_bindings"
|
|
|
|
|
set -g fish_bind_mode default
|
2024-10-25 22:47:20 +02:00
|
|
|
# Redirect stderr per #1155
|
|
|
|
|
$fish_key_bindings 2>/dev/null
|
2016-05-08 16:27:15 -07:00
|
|
|
# Load user key bindings if they are defined
|
|
|
|
|
if functions --query fish_user_key_bindings >/dev/null
|
2018-04-01 13:42:38 -07:00
|
|
|
fish_user_key_bindings 2>/dev/null
|
2016-05-08 16:27:15 -07:00
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
|
2016-05-22 19:54:11 +02:00
|
|
|
# Load key bindings
|
|
|
|
|
__fish_reload_key_bindings
|
2016-05-08 16:27:15 -07:00
|
|
|
|
2021-01-12 12:42:52 +01:00
|
|
|
# Detect whether the terminal reflows on its own
|
|
|
|
|
# If it does we shouldn't do it.
|
|
|
|
|
# Allow $fish_handle_reflow to override it.
|
|
|
|
|
if not set -q fish_handle_reflow
|
2020-11-30 20:50:02 +01:00
|
|
|
# VTE reflows the text itself, so us doing it inevitably races against it.
|
2020-11-30 20:01:33 +01:00
|
|
|
# Guidance from the VTE developers is to let them repaint.
|
2020-11-30 20:50:02 +01:00
|
|
|
if set -q VTE_VERSION
|
2023-09-22 17:04:59 +02:00
|
|
|
# Same for these terminals
|
2021-01-12 12:42:52 +01:00
|
|
|
or string match -q -- 'alacritty*' $TERM
|
2023-09-22 17:04:59 +02:00
|
|
|
or test "$TERM_PROGRAM" = WezTerm
|
2021-01-12 12:42:52 +01:00
|
|
|
set -g fish_handle_reflow 0
|
|
|
|
|
else if set -q KONSOLE_VERSION
|
2021-01-12 12:37:35 +01:00
|
|
|
and test "$KONSOLE_VERSION" -ge 210400 2>/dev/null
|
2021-01-12 12:42:52 +01:00
|
|
|
# Konsole since version 21.04(.00)
|
|
|
|
|
# Note that this is optional, but since we have no way of detecting it
|
|
|
|
|
# we go with the default, which is true.
|
|
|
|
|
set -g fish_handle_reflow 0
|
|
|
|
|
else
|
|
|
|
|
set -g fish_handle_reflow 1
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
function __fish_winch_handler --on-signal WINCH -d "Repaint screen when window changes size"
|
|
|
|
|
if test "$fish_handle_reflow" = 1 2>/dev/null
|
|
|
|
|
commandline -f repaint >/dev/null 2>/dev/null
|
2021-01-12 12:37:35 +01:00
|
|
|
end
|
2016-05-08 16:27:15 -07:00
|
|
|
end
|
|
|
|
|
|
2024-04-03 12:18:52 +02:00
|
|
|
# Notify terminals when $PWD changes via OSC 7 (issue #906).
|
|
|
|
|
function __fish_update_cwd_osc --on-variable PWD --description 'Notify terminals when $PWD changes'
|
2025-03-01 20:41:23 +01:00
|
|
|
set -l host $hostname
|
|
|
|
|
if set -q KONSOLE_VERSION
|
|
|
|
|
set host ''
|
|
|
|
|
end
|
Stop reading terminfo database
Our use of the terminfo database in /usr/share/terminfo/$TERM is both
1. a way for users to configure app behavior in their terminal (by
setting TERM, copying around and modifying terminfo files)
2. a way for terminal emulator developers to advertise support for
backwards-incompatible features that are not otherwise easily observable.
To 1: this is not ideal (it's very easy to break things). There's not many
things that realistically need configuration; let's use shell variables
instead.
To 2: in practice, feature-probing via terminfo is often wrong. There's not
many backwards-incompatible features that need this; for the ones that do
we can still use terminfo capabilities but query the terminal via XTGETTCAP
directly, skipping the file (which may not exist on the same system as
the terminal).
---
Get rid of terminfo. If anyone finds a $TERM where we need different behavior,
we can hardcode that into fish.
* Allow to override this with `fish_features=no-ignore-terminfo fish`
Not sure if we should document this, since it's supposed to be removed soon,
and if someone needs this (which we don't expect), we'd like to know.
* This is supported on a best-effort basis; it doesn't match the previous
behavior exactly. For simplicity of implementation, it will not change
the fact that we now:
* use parm_left_cursor (CSI Ps D) instead of cursor_left (CSI D) if
terminfo claims the former is supported
* no longer support eat_newline_glitch, which seems no longer present
on today's ConEmu and ConHost
* Tested as described in https://github.com/fish-shell/fish-shell/pull/11345#discussion_r2030121580
* add `man fish-terminal-compatibility` to state our assumptions.
This could help terminal emulator developers.
* assume `parm_up_cursor` is supported if the terminal supports XTGETTCAP
* Extract all control sequences to src/terminal_command.rs.
* Remove the "\x1b(B" prefix from EXIT_ATTRIBUTE_MODE. I doubt it's really
needed.
* assume it's generally okay to output 256 colors
Things have improved since commit 3669805627 (Improve compatibility with
0-16 color terminals., 2016-07-21).
Apparently almost every actively developed terminal supports it, including
Terminal.app and GNU screen.
* That is, we default `fish_term256` to true and keep it only as a way to
opt out of the the full 256 palette (e.g. switching to the 16-color
palette).
* `TERM=xterm-16color` has the same opt-out effect.
* `TERM` is generally ignored but add back basic compatiblity by turning
off color for "ansi-m", "linux-m" and "xterm-mono"; these are probably
not set accidentally.
* Since `TERM` is (mostly) ignored, we don't need the magic "xterm" in
tests. Unset it instead.
* Note that our pexpect tests used a dumb terminal because:
1. it makes fish do a full redraw of the commandline everytime, making it
easier to write assertions.
2. it disables all control sequences for colors, etc, which we usually
don't want to test explicitly.
I don't think TERM=dumb has any other use, so it would be better
to print escape sequences unconditionally, and strip them in
the test driver (leaving this for later, since it's a bit more involved).
Closes #11344
Closes #11345
2025-03-20 22:02:38 +01:00
|
|
|
if [ "$TERM" = dumb ]
|
|
|
|
|
return
|
|
|
|
|
end
|
2025-03-01 20:41:23 +01:00
|
|
|
printf \e\]7\;file://%s%s\a $host (string escape --style=url -- $PWD)
|
2016-05-08 16:27:15 -07:00
|
|
|
end
|
2024-04-03 12:18:52 +02:00
|
|
|
__fish_update_cwd_osc # Run once because we might have already inherited a PWD from an old tab
|
2016-05-08 16:27:15 -07:00
|
|
|
|
2019-12-13 12:26:40 +01:00
|
|
|
# Bump this whenever some code below needs to run once when upgrading to a new version.
|
|
|
|
|
# The universal variable __fish_initialized is initialized in share/config.fish.
|
2024-04-15 09:06:00 +02:00
|
|
|
set __fish_initialized 3800
|
2022-08-14 08:01:22 -07:00
|
|
|
|
|
|
|
|
functions -e __fish_config_interactive
|
2022-08-31 22:15:16 -07:00
|
|
|
end
|