fork/fish-shell

git clone https://git.julia.blue/fork/fish-shell

Johannes AltmanningerDon't print greetings in interactive read in fake-interactive shell3dc36f74b

master
7.1 KiB163 linesraw
1# localization: skip(private)
2#
3# Initializations that should only be performed when entering interactive mode.
4#
5# This function is called by the __fish_on_interactive function, which is defined in config.fish.
6#
7function __fish_config_interactive -d "Initializations that should be performed when entering interactive mode"
8    functions -e __fish_config_interactive
9
10    set -g __fish_active_key_bindings
11
12    functions -q __fish_webconfig_update_color_hook
13
14    #
15    # Generate man page completions if not present.
16    #
17    # Don't do this if we're being invoked as part of running unit tests.
18    if not set -q FISH_UNIT_TESTS_RUNNING
19        if not test -d $__fish_cache_dir/generated_completions
20            # We cannot simply do `fish_update_completions &` because it is a function.
21            # We don't want to call `fish -c` since that is unnecessary and sources config.fish again.
22            mkdir -p $__fish_cache_dir/generated_completions
23            fish_update_completions_detach=true fish_update_completions 2>/dev/null
24        end
25    end
26
27    #
28    # Print a greeting.
29    # The default just prints a variable of the same name.
30    #
31    # NOTE: This status check is necessary to not print the greeting when `read`ing in scripts. See #7080.
32    if not status is-interactive-read
33        and functions -q fish_greeting
34        fish_greeting
35    end
36
37    # Display SHELL_WELCOME if set. This is a standard environment variable (introduced by
38    # systemd v257) intended for shells to display when they first initialize.
39    if not status is-interactive-read
40        and set -q SHELL_WELCOME[1]
41        string join -- ' ' $SHELL_WELCOME
42    end
43
44    #
45    # Completions for SysV startup scripts. These aren't bound to any
46    # specific command, so they can't be autoloaded.
47    #
48    if test -d /etc/init.d
49        complete -x -p "/etc/init.d/*" -a start --description 'Start service'
50        complete -x -p "/etc/init.d/*" -a stop --description 'Stop service'
51        complete -x -p "/etc/init.d/*" -a status --description 'Print service status'
52        complete -x -p "/etc/init.d/*" -a restart --description 'Stop and then start service'
53        complete -x -p "/etc/init.d/*" -a reload --description 'Reload service configuration'
54    end
55
56    #
57    # Only a few builtins take filenames; initialize the rest with no file completions
58    #
59    complete -c(builtin -n | string match -rv '(\.|:|source|cd|contains|count|echo|exec|fish_indent|printf|random|realpath|set|\\[|test|for)') --no-files
60
61    # Reload key bindings when binding variable change
62    function __fish_reload_key_bindings -d "Reload key bindings when binding variable change" --on-variable fish_key_bindings
63        # Make sure some key bindings are set
64        if not set --query fish_key_bindings
65            set --global fish_key_bindings fish_default_key_bindings
66            return
67        end
68
69        # Do nothing if the key bindings didn't actually change.
70        # This could be because the variable was set to the existing value
71        # or because it was a local variable.
72        # If fish_key_bindings is empty on the first run, we still need to set the defaults.
73        if test "$fish_key_bindings" = "$__fish_active_key_bindings" -a -n "$fish_key_bindings"
74            return
75        end
76        # Check if fish_key_bindings is a valid function.
77        # If not, either keep the previous bindings (if any) or revert to default.
78        # Also print an error so the user knows.
79        if not functions -q "$fish_key_bindings"
80            echo "There is no fish_key_bindings function called: '$fish_key_bindings'" >&2
81            # We need to see if this is a defined function, otherwise we'd be in an endless loop.
82            if functions -q $__fish_active_key_bindings
83                echo "Keeping $__fish_active_key_bindings" >&2
84                # Set the variable to the old value so this error doesn't happen again.
85                set fish_key_bindings $__fish_active_key_bindings
86                return 1
87            else if functions -q fish_default_key_bindings
88                echo "Reverting to default bindings" >&2
89                set fish_key_bindings fish_default_key_bindings
90                # Return because we are called again
91                return 0
92            else
93                # If we can't even find the default bindings, something is broken.
94                # Without it, we would eventually run into the stack size limit, but that'd print hundreds of duplicate lines
95                # so we should give up earlier.
96                echo "Cannot find fish_default_key_bindings, falling back to very simple bindings." >&2
97                echo "Most likely something is wrong with your installation." >&2
98                return 0
99            end
100        end
101        set -g __fish_active_key_bindings "$fish_key_bindings"
102        set -g fish_bind_mode default
103        $fish_key_bindings
104        # Load user key bindings if they are defined
105        if functions --query fish_user_key_bindings >/dev/null
106            fish_user_key_bindings
107        end
108    end
109
110    # Load key bindings
111    __fish_reload_key_bindings
112
113    # Detect whether the terminal reflows on its own
114    # If it does we shouldn't do it.
115    # Allow $fish_handle_reflow to override it.
116    if not set -q fish_handle_reflow
117        # VTE reflows the text itself, so us doing it inevitably races against it.
118        # Guidance from the VTE developers is to let them repaint.
119        # Konsole reflows since version 21.04. Konsole added XTVERSION
120        # in v22.03.80~7.
121        # TODO(term-workaround)
122        if string match -rq -- '^(?:VTE\b|Konsole |WezTerm )' (status terminal)
123            or begin
124                set -q KONSOLE_VERSION
125                and test "$KONSOLE_VERSION" -ge 210400 2>/dev/null
126            end
127            or string match -q -- 'alacritty*' $TERM
128            set -g fish_handle_reflow 0
129        else
130            set -g fish_handle_reflow 1
131        end
132    end
133
134    function __fish_winch_handler --on-signal WINCH -d "Repaint screen when window changes size"
135        if test "$fish_handle_reflow" = 1 2>/dev/null
136            commandline -f repaint >/dev/null 2>/dev/null
137        end
138    end
139
140    # Notify terminals when $PWD changes via OSC 7 (issue #906).
141    if not functions --query __fish_update_cwd_osc
142        function __fish_update_cwd_osc --description 'Notify terminals when $PWD might have changed' \
143            --on-variable=PWD --on-event=fish_prompt
144            set -l host $hostname
145            # if set -l konsole_version (string match -r -- '^Konsole (\d+)\..*' (status terminal))[2]
146            #     # To-do: use a Konsole version where KF6_DEP_VERSION is >= 6.12
147            #     and $konsole_version -lt ???
148            # end
149            # TODO(term-workaround)
150            if set -q KONSOLE_VERSION
151                set host ''
152            end
153            if [ -n "$MSYSTEM" ] && string match -rq -- '^(Konsole|WezTerm) ' (status terminal)
154                return
155            end
156            if [ "$TERM" = dumb ]
157                return
158            end
159            printf \e\]7\;file://%s%s\a (string escape --style=url -- $host $PWD)
160        end
161    end
162    __fish_update_cwd_osc # Run once because we might have already inherited a PWD from an old tab
163end