Files
fish-shell/share/config.fish
Johannes Altmanninger 190d367bc4 Use globals for color variables, react to light/dark mode
Implicitly-universal variables have some downsides:
- It's surprising that "set fish_color_normal ..."
  and "set fish_key_bindings fish_vi_key_bindings" propagate to other
  shells and persist, especially since all other variables (and other
  shells) would use the global scope.
- they don't play well with tracking configuration in Git.
- we don't know how to roll out updates to the default theme (which is
  problematic since can look bad depending on terminal background
  color scheme).

It's sort of possible to use only globals and unset universal variables
(because fish only sets them at first startup), but that requires
knowledge of fish internals; I don't think many people do that.

So:
- Set all color variables that are not already set as globals.
  - To enable this do the following, once, after upgrading:
    copy any existing universal color variables to globals, and:
    - if existing universal color variables exactly match
      the previous default theme, and pretend they didn't exist.
    - else migrate the universals to ~/.config/fish/conf.d/fish_frozen_theme.fish,
      which is a less surprising way of persisting this.
    - either way, delete all universals to do the right thing for most users.
- Make sure that webconfig's "Set Theme" continues to:
  - instantly update all running shells
    - This is achieved by a new universal variable (but only for
      notifying shells, so this doesn't actually need to be persisted).
      In future, we could use any other IPC mechanism such as "kill -SIGUSR1"
      or if we go for a new feature, "varsave" or "set --broadcast", see
      https://github.com/fish-shell/fish-shell/issues/7317#issuecomment-701165897
      https://github.com/fish-shell/fish-shell/pull/8455#discussion_r757837137.
  - persist the theme updates, completely overriding any previous theme.
    Use the same "fish_frozen_theme.fish" snippet as for migration (see above).
    It's not meant to be edited directly. If people want flexibility
    the should delete it.
    It could be a universal variable instead of a conf snippet file;
    but I figured that the separate file looks nicer
    (we can have better comments etc.)
- Ask the terminal whether it's using dark or light mode, and use an
  optimized default. Add dark/light variants to themes,
  and the "unknown" variant for the default theme.
  Other themes don't need the "unknown" variant;
  webconfig already has a background color in context,
  and CLI can require the user to specify variant explicitly if
  terminal doesn't advertise colors.
- Every variable that is set as part of fish's default behavior
  gets a "--label=default" tacked onto it.

  This is to allow our fish_terminal_color_theme event handler to
  know which variables it is allowed to update. It's also necessary
  until we revert 7e3fac561d (Query terminal only just before reading
  from it, 2025-09-25) because since commit, we need to wait until
  the first reader_push() to get query results.  By this time, the
  user's config.fish may already have set variables.

  If the user sets variables via either webconfig, "fish_config theme
  {choose,save}", or directly via "set fish_color_...", they'd almost
  always remove this label.
- For consistency, make default fish_key_bindings global
  (note that, for better or worse, fish_add_path still remains as
  one place that implicitly sets universal variables, but it's not
  something we inject by default)
- Have "fish_config theme choose" and webconfig equivalents reset
  all color variables. This makes much more sense than keeping a
  hardcoded subset of "known colors"; and now that we don't really
  expect to be deleting universals this way, it's actually possible
  to make this change without much fear.

Should have split this into two commits (the changelog entries are
intertwined though).

Closes #11580
Closes #11435
Closes #7317
Ref: https://github.com/fish-shell/fish-shell/issues/12096#issuecomment-3632065704
2025-12-14 17:03:03 +01:00

225 lines
7.4 KiB
Fish

# This file does some internal fish setup.
# It is not recommended to remove or edit it.
#
# Set default field separators
#
set -g IFS \n\ \t
set -qg __fish_added_user_paths
or set -g __fish_added_user_paths
#
# Create the default command_not_found handler
#
function __fish_default_command_not_found_handler
printf (_ "fish: Unknown command: %s\n") (string escape -- $argv[1]) >&2
end
if not status --is-interactive
# Hook up the default as the command_not_found handler
# if we are not interactive to avoid custom handlers.
function fish_command_not_found --on-event fish_command_not_found
__fish_default_command_not_found_handler $argv
end
end
#
# Set default search paths for completions and shellscript functions
# unless they already exist
#
# Grab extra directories (as specified by the build process, usually for
# third-party packages to ship completions &c.
set -l __extra_completionsdir
set -l __extra_functionsdir
set -l __extra_confdir
__fish_data_with_file __fish_build_paths.fish source
# Compute the directories for vendor configuration. We want to include
# all of XDG_DATA_DIRS, as well as the __extra_* dirs defined above.
set -l xdg_data_dirs
if set -q XDG_DATA_DIRS
set --path xdg_data_dirs $XDG_DATA_DIRS
set xdg_data_dirs (string replace -r '([^/])/$' '$1' -- $xdg_data_dirs)/fish
end
set -g __fish_vendor_completionsdirs
set -g __fish_vendor_functionsdirs
set -g __fish_vendor_confdirs
# Don't load vendor directories when running unit tests
if not set -q FISH_UNIT_TESTS_RUNNING
set __fish_vendor_completionsdirs $__fish_user_data_dir/vendor_completions.d $xdg_data_dirs/vendor_completions.d
set __fish_vendor_functionsdirs $__fish_user_data_dir/vendor_functions.d $xdg_data_dirs/vendor_functions.d
set __fish_vendor_confdirs $__fish_user_data_dir/vendor_conf.d $xdg_data_dirs/vendor_conf.d
# Ensure that extra directories are always included.
if not contains -- $__extra_completionsdir $__fish_vendor_completionsdirs
set -a __fish_vendor_completionsdirs $__extra_completionsdir
end
if not contains -- $__extra_functionsdir $__fish_vendor_functionsdirs
set -a __fish_vendor_functionsdirs $__extra_functionsdir
end
if not contains -- $__extra_confdir $__fish_vendor_confdirs
set -a __fish_vendor_confdirs $__extra_confdir
end
end
# Set up function and completion paths. Make sure that the fish
# default functions/completions are included in the respective path.
if not set -q fish_function_path
set fish_function_path $__fish_config_dir/functions $__fish_sysconf_dir/functions $__fish_vendor_functionsdirs
end
if not set -q fish_complete_path
set fish_complete_path $__fish_config_dir/completions $__fish_sysconf_dir/completions $__fish_vendor_completionsdirs
set -a fish_complete_path $__fish_cache_dir/generated_completions
end
# Add a handler for when fish_user_path changes, so we can apply the same changes to PATH
function __fish_reconstruct_path -d "Update PATH when fish_user_paths changes" --on-variable fish_user_paths
# Deduplicate $fish_user_paths
# This should help with people appending to it in config.fish
set -l new_user_path
for path in (string split : -- $fish_user_paths)
if not contains -- $path $new_user_path
set -a new_user_path $path
end
end
if test (count $new_user_path) -lt (count $fish_user_paths)
# This will end up calling us again, so we return
set fish_user_paths $new_user_path
return
end
set -l local_path $PATH
for x in $__fish_added_user_paths
set -l idx (contains --index -- $x $local_path)
and set -e local_path[$idx]
end
set -g __fish_added_user_paths
if set -q fish_user_paths
# Explicitly split on ":" because $fish_user_paths might not be a path variable,
# but $PATH definitely is.
for x in (string split ":" -- $fish_user_paths[-1..1])
if set -l idx (contains --index -- $x $local_path)
set -e local_path[$idx]
else
set -ga __fish_added_user_paths $x
end
set -p local_path $x
end
end
set -xg PATH $local_path
end
#
# Launch debugger on SIGTRAP
#
function fish_sigtrap_handler --on-signal TRAP --no-scope-shadowing --description "TRAP handler: debug prompt"
breakpoint
end
#
# When a prompt is first displayed, make sure that interactive
# mode-specific initializations have been performed.
# This includes a `read` prompt, hence the fish_read event.
# This handler removes itself after it is first called.
#
function __fish_on_interactive --on-event fish_prompt --on-event fish_read
# We erase this *first* so it can't be called again,
# e.g. if fish_greeting calls "read".
functions -e __fish_on_interactive
__fish_config_interactive
end
# Set the locale if it isn't explicitly set. Allowing the lack of locale env vars to imply the
# C/POSIX locale causes too many problems. Do this before reading the snippets because they might be
# in UTF-8 (with non-ASCII characters).
not set -q LANG # (fast path - no need to load the file if we have $LANG)
and __fish_set_locale
#
# Some things should only be done for login terminals
# This used to be in etc/config.fish - keep it here to keep the semantics
#
if status --is-login
if command -sq /usr/libexec/path_helper
__fish_macos_set_env PATH /etc/paths '/etc/paths.d'
if test -n "$MANPATH"
__fish_macos_set_env MANPATH /etc/manpaths '/etc/manpaths.d'
end
functions -e __fish_macos_set_env
end
#
# Put linux consoles in unicode mode.
#
# TODO(terminal-workaround)
if test "$TERM" = linux
and string match -qir '\.UTF' -- $LANG
and command -sq unicode_start
unicode_start
end
end
# Invoke this here to apply the current value of fish_user_path after
# PATH is possibly set above.
__fish_reconstruct_path
# Allow %n job expansion to be used with fg/bg/wait
# `jobs` is the only one that natively supports job expansion
function __fish_expand_pid_args
for arg in $argv
if string match -qr '^%\d+$' -- $arg
if not jobs -p $arg
return 1
end
else
printf "%s\n" $arg
end
end
end
for jobbltn in bg wait disown
function $jobbltn -V jobbltn
set -l args (__fish_expand_pid_args $argv)
and builtin $jobbltn $args
end
end
function fg
set -l args (__fish_expand_pid_args $argv)
and builtin fg $args[-1]
end
if command -q kill
# Only define this if something to wrap exists
# this allows a nice "command not found" error to be triggered.
function kill
set -l args (__fish_expand_pid_args $argv)
and command kill $args
end
end
if status is-interactive
__fish_theme_migrate
end
fish_config theme choose default --no-override
# As last part of initialization, source the conf directories.
# Implement precedence (User > Admin > Extra (e.g. vendors) > Fish) by basically doing "basename".
set -l sourcelist
for file in $__fish_config_dir/conf.d/*.fish $__fish_sysconf_dir/conf.d/*.fish $__fish_vendor_confdirs/*.fish
set -l basename (string replace -r '^.*/' '' -- $file)
contains -- $basename $sourcelist
and continue
set sourcelist $sourcelist $basename
# Also skip non-files or unreadable files.
# This allows one to use e.g. symlinks to /dev/null to "mask" something (like in systemd).
test -f $file -a -r $file
and source $file
end