fork/fish-shell

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

Johannes AltmanningerFix double expansion of tokenized command line6b5ad163d

master
5.2 KiB123 linesraw
1set -l is_gnu
2if env --version &>/dev/null
3    set is_gnu --is-gnu
4end
5
6# Returns 0 if we're after `env` and all previous tokens have an equal sign or were switches
7function __fish_env_defining_vars
8    set last ""
9    for token in (commandline -cxp)[2..] # 2.. excludes `env`. -cx already ignores the variable being completed.
10        # Is a switch, defined an env variable, or was a variable name to unset (after -u)
11        string match -r -- '^-|=' $token || string match -rq -- '^(-u|--unset)$' "$last" || return 1
12        set last $token
13    end
14end
15
16# Returns 0 if we're after `env` and all previous tokens have not yet contained an equal sign
17# Prevents `env` completions from completing payload completions.
18function __fish_env_not_yet_vars
19    not string match -qe -- = (commandline -c)
20end
21
22# Generate a list of possible variable names to redefine, excluding any already redefined.
23function __fish_env_redefine_vars
24    set -l vars (set --names -x)
25
26    set cmdline "$(commandline -xp)"
27    for var in $vars
28        if not string match -eq -- $var= $cmdline
29            echo $var=
30        end
31    end
32end
33
34# Generate a list of possible variable names to define from completion history
35function __fish_env_names_from_history
36    set -l token (commandline -ct)
37    # Since this is always going to be a best-effort kind of thing, limit this to uppercased variables by convention.
38    # This prevents us from having to parse quotes to figure out what was part of the payload and what wasn't.
39    for var in (history search --prefix "env " | string match -ra '\b([A-Z0-9_]+)=' --groups-only)
40        echo $var=
41    end
42end
43
44# Generate a list of possible completions for the current variable name from history
45function __fish_env_values_from_history
46    string match -rq -- "(?<name>.+)=(?<value>.*)" (commandline -ct); or return 1
47
48    # Caveat lector: very crude multi-word tokenization handling below!
49    set -l rname (string escape --style=regex -- $name)
50    set -l search (string trim -c \'\" "$value")
51    set -l rsearch "$(string escape --style=regex -- $search)"
52    # Search multi-word values with quotes
53    set -l query '.*\b'$name'=([\'"])('$rsearch'.+?)\1.*'
54    set -l matches (history search --prefix "env " | string replace -rfa $query '$2')
55    # Search multi-word values without quotes
56    set -l query '.*\b'$name'=('$rsearch'[^\'" ]+).*'
57    set -a matches (history search --prefix "env " | string replace -rfa $query '$1')
58    # Display results without quotes
59    set matches (printf "%s\n" $matches | sort -u)
60    printf "$name=%s\n" $matches
61end
62
63# Get the text after all env arguments and variables, so we can complete it as a regular command
64function __fish_env_remaining_args -V is_gnu
65    set -l argv (commandline -xpc | string escape) (commandline -ct)
66    if set -q is_gnu[1]
67        argparse -s i/ignore-environment u/unset= help version -- $argv 2>/dev/null
68        or return 0
69    else
70        argparse -s 0 i P= S= u= v -- $argv 2>/dev/null
71        or return 0
72    end
73
74    # argv[1] is `env` or an alias.
75    set -e argv[1]
76
77    # Remove all VAR=VAL arguments up to the first that isn't
78    while set -q argv[1]
79        if string match -q '*=*' -- $argv[1]
80            or string match -q -- '-*' $argv[1]
81            set -e argv[1]
82        else
83            break
84        end
85    end
86
87    string join \n -- $argv
88
89    # Return true if there is a subcommand.
90    test -n "$argv[1]"
91end
92
93# Generate a completion for the executable to execute under `env`
94function __fish_complete_env_subcommand
95    if set -l argv (__fish_env_remaining_args)
96        complete -C "$argv"
97    end
98end
99
100# Complete the name of the variable from current definitions
101complete -c env -n '__fish_env_defining_vars; and not string match -eq = -- (commandline -ct)' -a "(__fish_env_redefine_vars)" -f -d "Redefine variable"
102# Complete the name of the variable from history
103complete -c env -n '__fish_env_defining_vars; and not string match -eq = -- (commandline -ct)' -a "(__fish_env_names_from_history)" -f -d Historical
104# Complete the value for FOO= from history
105# TODO: NO_ESCAPE when that becomes available
106complete -c env -n '__fish_env_defining_vars; and string match -eq = -- (commandline -ct)' -a "(__fish_env_values_from_history)" -f
107
108# Complete normally after we are done with `env` stuff
109complete -c env -fa "(__fish_complete_env_subcommand)"
110
111if set -q is_gnu
112    complete -c env -n __fish_env_not_yet_vars -s i -l ignore-environment -d "Start with an empty environment"
113    complete -c env -n __fish_env_not_yet_vars -s u -l unset -d "Unset environment variable" -x -a "(set --names -x)"
114    complete -c env -n __fish_env_not_yet_vars -l help -d "Display help and exit"
115    complete -c env -n __fish_env_not_yet_vars -l version -d "Display version and exit"
116else
117    complete -c env -n __fish_env_not_yet_vars -s 0 -d "End output lines with NUL"
118    complete -c env -n __fish_env_not_yet_vars -s i -d "Start with empty environment"
119    complete -c env -n __fish_env_not_yet_vars -s P -d "Provide an alternate PATH"
120    complete -c env -n __fish_env_not_yet_vars -s S -d "Split argument into args on ' '"
121    complete -c env -n __fish_env_not_yet_vars -s u -d "Unset environment variable" -x -a "(set --names -x)"
122    complete -c env -n __fish_env_not_yet_vars -s v -d "Verbose output on processing"
123end