fork/fish-shell
git clone https://git.julia.blue/fork/fish-shell
6b5ad163d
master
1set -l is_gnu 2if env --version&> /dev/null 3set 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 8set last"" 9for tokenin ( 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) 11string match -r --'^-|=' $token || string match -rq --'^(-u|--unset)$' " $last " || return 1 12set last$token 13end 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 19not 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 24set -l vars( set --names -x) 25 26set cmdline"$ ( commandline -xp ) " 27for varin $vars 28if not string match -eq --$var =$cmdline 29echo $var = 30end 31end 32end 33 34# Generate a list of possible variable names to define from completion history 35function __fish_env_names_from_history 36set -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. 39for varin ( history search --prefix"env " | string match -ra'\b([A-Z0-9_]+)=' --groups-only) 40echo $var = 41end 42end 43 44# Generate a list of possible completions for the current variable name from history 45function __fish_env_values_from_history 46string match -rq --"(?<name>.+)=(?<value>.*)" ( commandline -ct) ;or return 1 47 48# Caveat lector: very crude multi-word tokenization handling below! 49set -l rname( string escape --style=regex --$name ) 50set -l search( string trim -c\'\" " $value " ) 51set -l rsearch"$ ( string escape --style=regex -- $search ) " 52# Search multi-word values with quotes 53set -l query'.*\b' $name '=([\'"])(' $rsearch '.+?)\1.*' 54set -l matches( history search --prefix"env " | string replace -rfa$query '$2' ) 55# Search multi-word values without quotes 56set -l query'.*\b' $name '=(' $rsearch '[^\'" ]+).*' 57set -a matches( history search --prefix"env " | string replace -rfa$query '$1' ) 58# Display results without quotes 59set matches( printf "%s\n" $matches | sort -u) 60printf " $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 65set -l argv( commandline -xpc| string escape) ( commandline -ct) 66if set -q is_gnu[ 1 ] 67argparse -s i/ignore-environment u/unset= help version --$argv 2> /dev/null 68or return 0 69else 70argparse -s0 i P= S= u= v --$argv 2> /dev/null 71or return 0 72end 73 74# argv[1] is `env` or an alias. 75set -e argv[ 1 ] 76 77# Remove all VAR=VAL arguments up to the first that isn't 78while set -q argv[ 1 ] 79if string match -q'*=*' --$argv [ 1 ] 80or string match -q --'-*' $argv [ 1 ] 81set -e argv[ 1 ] 82else 83break 84end 85end 86 87string join\n --$argv 88 89# Return true if there is a subcommand. 90test -n " $argv [ 1 ] " 91end 92 93# Generate a completion for the executable to execute under `env` 94function __fish_complete_env_subcommand 95if set -l argv( __fish_env_remaining_args ) 96complete -C" $argv " 97end 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 112complete -c env -n __fish_env_not_yet_vars -s i -l ignore-environment -d"Start with an empty environment" 113complete -c env -n __fish_env_not_yet_vars -s u -l unset -d"Unset environment variable" -x -a"(set --names -x)" 114complete -c env -n __fish_env_not_yet_vars -l help -d"Display help and exit" 115complete -c env -n __fish_env_not_yet_vars -l version -d"Display version and exit" 116else 117complete -c env -n __fish_env_not_yet_vars -s0 -d"End output lines with NUL" 118complete -c env -n __fish_env_not_yet_vars -s i -d"Start with empty environment" 119complete -c env -n __fish_env_not_yet_vars -s P -d"Provide an alternate PATH" 120complete -c env -n __fish_env_not_yet_vars -s S -d"Split argument into args on ' '" 121complete -c env -n __fish_env_not_yet_vars -s u -d"Unset environment variable" -x -a"(set --names -x)" 122complete -c env -n __fish_env_not_yet_vars -s v -d"Verbose output on processing" 123end