2014-10-05 04:17:46 +00:00
|
|
|
function abbr --description "Manage abbreviations"
|
2014-10-08 02:11:56 +00: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]
|
2015-03-13 08:32:05 +00:00
|
|
|
if test $needs_arg = single
|
2014-10-08 02:11:56 +00:00
|
|
|
set mode_arg $argv[1]
|
|
|
|
set needs_arg no
|
2015-03-13 08:32:05 +00:00
|
|
|
else if test $needs_arg = coalesce
|
|
|
|
set mode_arg "$argv"
|
|
|
|
set needs_arg no
|
|
|
|
set -e argv
|
2014-10-08 02:11:56 +00:00
|
|
|
else
|
|
|
|
set -l new_mode
|
|
|
|
switch $argv[1]
|
|
|
|
case '-h' '--help'
|
|
|
|
__fish_print_help abbr
|
2014-10-05 04:17:46 +00:00
|
|
|
return 0
|
2014-10-08 02:11:56 +00:00
|
|
|
case '-a' '--add'
|
|
|
|
set new_mode add
|
2015-03-13 08:32:05 +00:00
|
|
|
set needs_arg coalesce
|
2015-05-22 01:48:39 +00:00
|
|
|
case '-e' '--erase'
|
|
|
|
set new_mode erase
|
2015-03-13 08:32:05 +00:00
|
|
|
set needs_arg single
|
2014-10-08 02:11:56 +00: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
|
2014-10-05 04:17:46 +00:00
|
|
|
end
|
2014-10-08 02:11:56 +00: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
|
2014-10-05 04:17:46 +00:00
|
|
|
end
|
2014-10-08 02:11:56 +00:00
|
|
|
set mode $new_mode
|
|
|
|
set mode_flag $argv[1]
|
|
|
|
end
|
|
|
|
set -e argv[1]
|
|
|
|
end
|
2015-03-13 08:32:05 +00:00
|
|
|
if test $needs_arg != no
|
2014-10-08 02:11:56 +00:00
|
|
|
printf ( _ "%s: option requires an argument -- %s\n" ) abbr $mode_flag >&2
|
|
|
|
return 1
|
|
|
|
end
|
2015-06-14 21:08:10 +00: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 test -z "$mode"
|
|
|
|
if set -q argv[1]
|
|
|
|
set mode 'add'
|
|
|
|
set mode_arg "$argv"
|
|
|
|
set -e argv
|
|
|
|
else
|
|
|
|
set mode 'show'
|
|
|
|
end
|
|
|
|
end
|
2014-10-05 04:17:46 +00:00
|
|
|
|
2014-10-08 02:11:56 +00: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'
|
|
|
|
set -l key
|
|
|
|
set -l value
|
|
|
|
__fish_abbr_parse_entry $mode_arg key value
|
|
|
|
# ensure the key contains at least one non-space character
|
2016-01-04 14:27:15 +00:00
|
|
|
if not string match -qr "\w" -- $key
|
2014-10-08 02:11:56 +00:00
|
|
|
printf ( _ "%s: abbreviation must have a non-empty key\n" ) abbr >&2
|
|
|
|
return 1
|
|
|
|
end
|
2016-01-04 14:27:15 +00:00
|
|
|
if not string match -qr "\w" -- $value
|
2014-10-08 02:11:56 +00: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
|
|
|
|
set fish_user_abbreviations $fish_user_abbreviations $mode_arg
|
|
|
|
return 0
|
2014-10-05 04:17:46 +00:00
|
|
|
|
2015-05-22 01:48:39 +00:00
|
|
|
case 'erase'
|
2014-10-08 02:11:56 +00:00
|
|
|
set -l key
|
|
|
|
__fish_abbr_parse_entry $mode_arg key
|
|
|
|
if set -l idx (__fish_abbr_get_by_key $key)
|
|
|
|
set -e fish_user_abbreviations[$idx]
|
2014-10-05 04:17:46 +00:00
|
|
|
return 0
|
2014-10-08 02:11:56 +00:00
|
|
|
else
|
|
|
|
printf ( _ "%s: no such abbreviation '%s'\n" ) abbr $key >&2
|
|
|
|
return 2
|
|
|
|
end
|
2014-10-05 04:17:46 +00:00
|
|
|
|
2014-10-08 02:11:56 +00:00
|
|
|
case 'show'
|
|
|
|
for i in $fish_user_abbreviations
|
2015-06-14 21:08:10 +00:00
|
|
|
__fish_abbr_parse_entry $i key value
|
|
|
|
|
|
|
|
# Check to see if either key or value has a leading dash
|
|
|
|
# If so, we need to write --
|
|
|
|
set -l opt_double_dash ''
|
|
|
|
switch $key ; case '-*'; set opt_double_dash ' --'; end
|
|
|
|
switch $value ; case '-*'; set opt_double_dash ' --'; end
|
2016-01-04 14:27:15 +00:00
|
|
|
echo abbr$opt_double_dash (string escape -- $key $value)
|
2014-10-08 02:11:56 +00:00
|
|
|
end
|
|
|
|
return 0
|
2014-10-05 04:17:46 +00:00
|
|
|
|
2014-10-08 02:11:56 +00:00
|
|
|
case 'list'
|
|
|
|
for i in $fish_user_abbreviations
|
|
|
|
set -l key
|
|
|
|
__fish_abbr_parse_entry $i key
|
|
|
|
printf "%s\n" $key
|
|
|
|
end
|
|
|
|
return 0
|
|
|
|
end
|
2014-10-05 04:17:46 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
function __fish_abbr_get_by_key
|
2014-10-08 02:11:56 +00:00
|
|
|
if not set -q argv[1]
|
|
|
|
echo "__fish_abbr_get_by_key: expected one argument, got none" >&2
|
|
|
|
return 2
|
|
|
|
end
|
|
|
|
set -l count (count $fish_user_abbreviations)
|
|
|
|
if test $count -gt 0
|
2016-01-04 14:27:15 +00:00
|
|
|
set -l key $argv[1] # This assumes the key is valid and pre-parsed
|
2014-10-08 02:11:56 +00:00
|
|
|
for i in (seq $count)
|
|
|
|
set -l key_i
|
|
|
|
__fish_abbr_parse_entry $fish_user_abbreviations[$i] key_i
|
|
|
|
if test "$key" = "$key_i"
|
2014-10-05 04:17:46 +00:00
|
|
|
echo $i
|
|
|
|
return 0
|
2014-10-08 02:11:56 +00:00
|
|
|
end
|
2014-10-05 04:17:46 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
return 1
|
|
|
|
end
|
|
|
|
|
2014-10-08 02:11:56 +00:00
|
|
|
function __fish_abbr_parse_entry -S -a __input __key __value
|
|
|
|
if test -z "$__key"
|
2014-11-15 02:21:21 +00:00
|
|
|
set __key __
|
2014-10-08 02:11:56 +00:00
|
|
|
end
|
|
|
|
if test -z "$__value"
|
2014-11-15 02:21:21 +00:00
|
|
|
set __value __
|
2014-10-08 02:11:56 +00:00
|
|
|
end
|
|
|
|
switch $__input
|
2016-01-04 14:27:15 +00:00
|
|
|
case "*=*"
|
|
|
|
# No need for bounds-checking because we already matched before
|
|
|
|
set -l KV (string split "=" -m 1 -- $__input)
|
|
|
|
set $__key $KV[1]
|
|
|
|
set $__value $KV[2]
|
|
|
|
case "* *"
|
|
|
|
set -l KV (string split " " -m 1 -- $__input)
|
|
|
|
set $__key $KV[1]
|
|
|
|
set $__value $KV[2]
|
|
|
|
case "*"
|
|
|
|
set $__key $__input
|
2014-10-08 02:11:56 +00:00
|
|
|
end
|
|
|
|
return 0
|
2014-10-05 04:17:46 +00:00
|
|
|
end
|