From c492d03f5133902669252ba4a0dcfb607c167fce Mon Sep 17 00:00:00 2001 From: Mahmoud Al-Qudsi Date: Tue, 3 Apr 2018 14:40:51 -0500 Subject: [PATCH] Overhaul completions for `set` and add new completions for `set -e` Now the description includes the variable scope, `set [-e] -[Ugl]` completions only provide variables matching that scope, and completions that shouldn't be modified are hidden from the user. Completions that are often modified but rarely unset (`fish_*` variables) are omitted from `set -e` completions. A new helper function `__fish_seen_argument` has been added that makes it easy to only provied completions for a specific flag. --- share/completions/set.fish | 22 +++++++++++++++++++--- share/functions/__fish_seen_argument.fish | 22 ++++++++++++++++++++++ 2 files changed, 41 insertions(+), 3 deletions(-) create mode 100644 share/functions/__fish_seen_argument.fish diff --git a/share/completions/set.fish b/share/completions/set.fish index b684ae679..e0e5d3f7d 100644 --- a/share/completions/set.fish +++ b/share/completions/set.fish @@ -40,6 +40,7 @@ function __fish_set_is_locale -d 'Test if We are specifying a locale value for t return 0 case '-*' + continue case '*' return 1 @@ -65,9 +66,25 @@ complete -c set -n '__fish_is_first_token' -s q -l query -d "Test if variable is complete -c set -n '__fish_is_first_token' -s h -l help -d "Display help and exit" complete -c set -n '__fish_is_first_token' -s n -l names -d "List the names of the variables, but not their value" +#TODO: add CPP code to generate list of read-only variables and exclude them from the following completions # Complete using preexisting variable names -complete -c set -n '__fish_is_first_token' -x -a "(set|sed -e 's/ /'\t'Variable: /')" +complete -c set -n '__fish_is_first_token; and not __fish_seen_argument -s e -l erase; and not __fish_seen_argument -s l -s g -s U -l local -l global -l universal' -x -a "(set -l | string match -rv '^__' | string replace ' ' \t'Local Variable: ')" +complete -c set -n '__fish_is_first_token; and not __fish_seen_argument -s e -l erase; and not __fish_seen_argument -s l -s g -s U -l local -l global -l universal' -x -a "(set -g | string match -rv '^__' | string replace ' ' \t'Global Variable: ')" +complete -c set -n '__fish_is_first_token; and not __fish_seen_argument -s e -l erase; and not __fish_seen_argument -s l -s g -s U -l local -l global -l universal' -x -a "(set -U | string match -rv '^__' | string replace ' ' \t'Universal Variable: ')" +# Complete scope-specific variables +complete -c set -n '__fish_is_first_token; and __fish_seen_argument -s l -l local' -x -a "(set -l | string match -rv '^__' | string replace ' ' \t'Local Variable: ')" +complete -c set -n '__fish_is_first_token; and __fish_seen_argument -s g -l global' -x -a "(set -g | string match -rv '^__' | string replace ' ' \t'Global Variable: ')" +complete -c set -n '__fish_is_first_token; and __fish_seen_argument -s U -l universal' -x -a "(set -U | string match -rv '^__' | string replace ' ' \t'Universal Variable: ')" + +# Complete using preexisting variable names for `set --erase` +complete -c set -n '__fish_seen_argument -s e -l erase; and not __fish_seen_argument -s l -s U -s g -l local -l global -l Universal' -f -a "(set -g | string match -rv '^_|^fish_' | string replace ' ' \tGlobal\ Variable:\ )" +complete -c set -n '__fish_seen_argument -s e -l erase; and not __fish_seen_argument -s l -s U -s g -l local -l global -l Universal' -f -a "(set -l | string match -rv '^_|^fish_' | string replace ' ' \tLocal\ Variable:\ )" +complete -c set -n '__fish_seen_argument -s e -l erase; and not __fish_seen_argument -s l -s U -s g -l local -l global -l Universal' -f -a "(set -U | string match -rv '^_|^fish_' | string replace ' ' \tUniversal\ Variable:\ )" +# Complete scope-specific variables for `set --erase` +complete -c set -n '__fish_seen_argument -s e -l erase; and __fish_seen_argument -s g -l global' -f -a "(set -g | string match -rv '^_|^fish_' | string replace ' ' \t'Global Variable: ')" +complete -c set -n '__fish_seen_argument -s e -l erase; and __fish_seen_argument -s U -l universal' -f -a "(set -u | string match -rv '^_|^fish_' | string replace ' ' \t'Universal Variable: ')" +complete -c set -n '__fish_seen_argument -s e -l erase; and __fish_seen_argument -s l -l local' -f -a "(set -l | string match -rv '^_|^fish_' | string replace ' ' \t'Local Variable: ')" # Color completions complete -c set -n '__fish_set_is_color' -x -a '(set_color --print-colors)' -d Color @@ -75,6 +92,5 @@ complete -c set -n '__fish_set_is_color' -s b -l background -x -a '(set_color -- complete -c set -n '__fish_set_is_color' -s o -l bold -d 'Make font bold' # Locale completions -complete -c set -n '__fish_is_first_token' -x -a '$__fish_locale_vars' -d 'Locale variable' -complete -c set -n '__fish_set_is_locale' -x -a '(locale -a)' -d (_ "Locale") +complete -c set -n '__fish_set_is_locale; and not __fish_seen_argument -s e -l erase' -x -a '(locale -a)' -d (_ "Locale") complete -c set -s L -l long -d 'Do not truncate long lines' diff --git a/share/functions/__fish_seen_argument.fish b/share/functions/__fish_seen_argument.fish new file mode 100644 index 000000000..dd9a6dd2b --- /dev/null +++ b/share/functions/__fish_seen_argument.fish @@ -0,0 +1,22 @@ +function __fish_seen_argument + argparse 's/short=+' 'l/long=+' -- $argv + + set cmd (commandline -co) + set -e cmd[1] + for t in $cmd + for s in $_flag_s + if string match -qr "^-[A-z0-9]*"$s"[A-z0-9]*\$" -- $t + return 0 + end + end + + for l in $_flag_l + if string match -q "--$l" -- $t + return 0 + end + end + end + + return 1 +end +