2014-10-05 12:17:46 +08:00
|
|
|
function abbr --description "Manage abbreviations"
|
2014-10-08 10:11:56 +08:00
|
|
|
# parse arguments
|
|
|
|
set -l mode
|
|
|
|
set -l mode_flag # the flag that was specified, for better errors
|
|
|
|
set -l mode_arg
|
|
|
|
set -l needs_arg no
|
|
|
|
while set -q argv[1]
|
2016-05-19 23:48:39 +08:00
|
|
|
set -l new_mode
|
|
|
|
switch $argv[1]
|
2014-10-08 10:11:56 +08:00
|
|
|
case '-h' '--help'
|
|
|
|
__fish_print_help abbr
|
2014-10-05 12:17:46 +08:00
|
|
|
return 0
|
2014-10-08 10:11:56 +08:00
|
|
|
case '-a' '--add'
|
|
|
|
set new_mode add
|
2016-05-19 22:48:45 +08:00
|
|
|
set needs_arg multi
|
2015-05-22 09:48:39 +08:00
|
|
|
case '-e' '--erase'
|
|
|
|
set new_mode erase
|
2015-03-13 16:32:05 +08:00
|
|
|
set needs_arg single
|
2014-10-08 10:11:56 +08:00
|
|
|
case '-l' '--list'
|
|
|
|
set new_mode list
|
|
|
|
case '-s' '--show'
|
|
|
|
set new_mode show
|
|
|
|
case '--'
|
|
|
|
set -e argv[1]
|
|
|
|
break
|
|
|
|
case '-*'
|
|
|
|
printf ( _ "%s: invalid option -- %s\n" ) abbr $argv[1] >&2
|
|
|
|
return 1
|
|
|
|
case '*'
|
|
|
|
break
|
|
|
|
end
|
2016-05-19 23:48:39 +08:00
|
|
|
if test -n "$mode" -a -n "$new_mode"
|
|
|
|
# we're trying to set two different modes
|
|
|
|
printf ( _ "%s: %s cannot be specified along with %s\n" ) abbr $argv[1] $mode_flag >&2
|
|
|
|
return 1
|
|
|
|
end
|
|
|
|
set mode $new_mode
|
|
|
|
set mode_flag $argv[1]
|
|
|
|
set -e argv[1]
|
|
|
|
end
|
|
|
|
|
2016-05-19 23:54:15 +08:00
|
|
|
# If run with no options, treat it like --add if we have an argument, or
|
|
|
|
# --show if we do not have an argument
|
|
|
|
if not set -q mode[1]
|
|
|
|
if set -q argv[1]
|
|
|
|
set mode add
|
|
|
|
set needs_arg multi
|
|
|
|
else
|
|
|
|
set mode show
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2016-05-19 23:48:39 +08:00
|
|
|
if test $needs_arg = single
|
|
|
|
set mode_arg $argv[1]
|
|
|
|
set needs_arg no
|
2014-10-08 10:11:56 +08:00
|
|
|
set -e argv[1]
|
2016-05-19 23:48:39 +08:00
|
|
|
else if test $needs_arg = multi
|
|
|
|
set mode_arg $argv
|
|
|
|
set needs_arg no
|
|
|
|
set -e argv
|
2014-10-08 10:11:56 +08:00
|
|
|
end
|
2015-03-13 16:32:05 +08:00
|
|
|
if test $needs_arg != no
|
2014-10-08 10:11:56 +08:00
|
|
|
printf ( _ "%s: option requires an argument -- %s\n" ) abbr $mode_flag >&2
|
|
|
|
return 1
|
|
|
|
end
|
2015-06-15 05:08:10 +08:00
|
|
|
|
2014-10-08 10:11:56 +08:00
|
|
|
# none of our modes want any excess arguments
|
|
|
|
if set -q argv[1]
|
|
|
|
printf ( _ "%s: Unexpected argument -- %s\n" ) abbr $argv[1] >&2
|
|
|
|
return 1
|
|
|
|
end
|
|
|
|
|
|
|
|
switch $mode
|
|
|
|
case 'add'
|
2016-05-19 22:48:45 +08:00
|
|
|
# Convert from old "key=value" syntax
|
|
|
|
# TODO: This should be removed later
|
|
|
|
if not set -q mode_arg[2]; and string match -qr '^[^ ]+=' -- $mode_arg
|
|
|
|
set mode_arg (string split "=" -- $mode_arg)
|
2016-03-02 03:22:21 +08:00
|
|
|
end
|
|
|
|
|
2016-02-11 21:57:24 +08:00
|
|
|
# Bail out early if the exact abbr is already in
|
2016-05-19 22:48:45 +08:00
|
|
|
contains -- "$mode_arg" $fish_user_abbreviations; and return 0
|
|
|
|
set -l key $mode_arg[1]
|
|
|
|
set -e mode_arg[1]
|
|
|
|
set -l value "$mode_arg"
|
|
|
|
# Because we later store "$key $value", there can't be any spaces in the key
|
|
|
|
if string match -q "* *" -- $key
|
|
|
|
printf ( _ "%s: abbreviation cannot have spaces in the key\n" ) abbr >&2
|
2014-10-08 10:11:56 +08:00
|
|
|
return 1
|
|
|
|
end
|
2016-05-19 22:48:45 +08:00
|
|
|
if test -z "$value"
|
2014-10-08 10:11:56 +08:00
|
|
|
printf ( _ "%s: abbreviation must have a value\n" ) abbr >&2
|
|
|
|
return 1
|
|
|
|
end
|
|
|
|
if set -l idx (__fish_abbr_get_by_key $key)
|
|
|
|
# erase the existing abbreviation
|
|
|
|
set -e fish_user_abbreviations[$idx]
|
|
|
|
end
|
|
|
|
if not set -q fish_user_abbreviations
|
|
|
|
# initialize as a universal variable, so we can skip the -U later
|
|
|
|
# and therefore work properly if someone sets this as a global variable
|
|
|
|
set -U fish_user_abbreviations
|
|
|
|
end
|
2016-05-19 22:48:45 +08:00
|
|
|
set fish_user_abbreviations $fish_user_abbreviations "$key $value"
|
2014-10-08 10:11:56 +08:00
|
|
|
return 0
|
2014-10-05 12:17:46 +08:00
|
|
|
|
2015-05-22 09:48:39 +08:00
|
|
|
case 'erase'
|
2016-05-19 22:48:45 +08:00
|
|
|
if set -l idx (__fish_abbr_get_by_key $mode_arg)
|
2014-10-08 10:11:56 +08:00
|
|
|
set -e fish_user_abbreviations[$idx]
|
2014-10-05 12:17:46 +08:00
|
|
|
return 0
|
2014-10-08 10:11:56 +08:00
|
|
|
else
|
2016-05-19 22:48:45 +08:00
|
|
|
printf ( _ "%s: no such abbreviation '%s'\n" ) abbr $mode_arg >&2
|
2014-10-08 10:11:56 +08:00
|
|
|
return 2
|
|
|
|
end
|
2014-10-05 12:17:46 +08:00
|
|
|
|
2014-10-08 10:11:56 +08:00
|
|
|
case 'show'
|
|
|
|
for i in $fish_user_abbreviations
|
2016-05-19 23:49:09 +08:00
|
|
|
set -l opt_double_dash
|
2016-05-19 22:48:45 +08:00
|
|
|
set -l kv (string split " " -m 1 -- $i)
|
2016-02-11 22:00:31 +08:00
|
|
|
set -l key $kv[1]
|
|
|
|
set -l value $kv[2]
|
2015-06-15 05:08:10 +08:00
|
|
|
|
|
|
|
# Check to see if either key or value has a leading dash
|
|
|
|
# If so, we need to write --
|
2016-05-19 23:49:09 +08:00
|
|
|
string match -q -- '-*' $key $value; and set opt_double_dash '--'
|
2016-02-11 22:00:31 +08:00
|
|
|
echo abbr $opt_double_dash (string escape -- $key $value)
|
2014-10-08 10:11:56 +08:00
|
|
|
end
|
|
|
|
return 0
|
2014-10-05 12:17:46 +08:00
|
|
|
|
2014-10-08 10:11:56 +08:00
|
|
|
case 'list'
|
|
|
|
for i in $fish_user_abbreviations
|
2016-05-19 22:48:45 +08:00
|
|
|
set -l key (string split " " -m 1 -- $i)[1]
|
2014-10-08 10:11:56 +08:00
|
|
|
printf "%s\n" $key
|
|
|
|
end
|
|
|
|
return 0
|
|
|
|
end
|
2014-10-05 12:17:46 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
function __fish_abbr_get_by_key
|
2014-10-08 10:11:56 +08:00
|
|
|
if not set -q argv[1]
|
|
|
|
echo "__fish_abbr_get_by_key: expected one argument, got none" >&2
|
|
|
|
return 2
|
|
|
|
end
|
2016-02-11 22:00:31 +08:00
|
|
|
# Going through all entries is still quicker than calling `seq`
|
|
|
|
set -l keys
|
|
|
|
for kv in $fish_user_abbreviations
|
2016-03-02 03:22:21 +08:00
|
|
|
# If this does not match, we have screwed up before and the error should be reported
|
|
|
|
set keys $keys (string split " " -m 1 -- $kv)[1]
|
2014-10-05 12:17:46 +08:00
|
|
|
end
|
2016-02-11 22:00:31 +08:00
|
|
|
if set -l idx (contains -i -- $argv[1] $keys)
|
|
|
|
echo $idx
|
|
|
|
return 0
|
|
|
|
end
|
2014-10-05 12:17:46 +08:00
|
|
|
return 1
|
|
|
|
end
|