2006-02-08 17:20:05 +08:00
#
# This is a neat function, stolen from zsh. It allows you to edit the
# value of a variable interactively.
#
2007-01-16 09:29:18 +08:00
function vared --description "Edit variable value"
2016-11-28 13:27:22 +08:00
if test ( count $argv ) = 1
switch $argv
2006-02-08 17:20:05 +08:00
2020-03-10 02:36:12 +08:00
case -h --h --he --hel --help
2016-11-28 13:27:22 +08:00
__fish_print_help vared
return 0
2006-02-08 17:20:05 +08:00
2016-11-28 13:27:22 +08:00
case '-*'
2022-04-04 11:57:55 +08:00
printf ( _ "%s: Unknown option %s\n" ) vared $argv > & 2
2016-11-28 13:27:22 +08:00
return 1
2006-02-08 17:20:05 +08:00
2016-11-28 13:27:22 +08:00
case '*'
if test ( count $$ argv ) -lt 2
2022-04-24 07:35:39 +08:00
# Try to avoid using local variables in this function, otherwise they can't be edited.
2022-03-27 12:42:40 +08:00
# https://github.com/fish-shell/fish-shell/issues/8836
2022-04-24 07:35:39 +08:00
# However we need to use one local, as you can't read directly into an index (like vared PATH[4]).
set -l __fish_vared_temp_value
2007-02-09 01:01:18 +08:00
2022-03-27 12:42:40 +08:00
# The command substitution in this line controls the scope.
# If variable already exists, do not add any switches, so we don't change
# scoping or export rules. But if it does not exist, we make the variable
# global, so that it will not die when this function dies.
2007-02-09 01:01:18 +08:00
2022-03-27 12:42:40 +08:00
read -p 'set_color green; echo ' $argv '; set_color normal; echo "> "' \
( if not set -q $argv ; echo -g ; end ) \
-c " $$ argv " \
2022-04-24 07:35:39 +08:00
__fish_vared_temp_value
if test -n " $$ argv "
set $argv $__fish_vared_temp_value
else
set -g $argv $__fish_vared_temp_value
end
2016-11-28 13:27:22 +08:00
else
2022-04-04 11:57:55 +08:00
printf ( _ '%s: %s is an array variable. Use %svared%s %s[n]%s to edit the n:th element of %s\n' ) vared $argv ( set_color $fish_color_command ; echo ) ( set_color $fish_color_normal ; echo ) $argv ( set_color normal; echo ) $argv > & 2
2016-11-28 13:27:22 +08:00
end
end
else
2022-04-04 11:57:55 +08:00
printf ( _ '%s: Expected exactly one argument, got %s.\n\nSynopsis:\n\t%svared%s VARIABLE\n' ) vared ( count $argv ) ( set_color $fish_color_command ; echo ) ( set_color $fish_color_normal ; echo ) > & 2
2016-11-28 13:27:22 +08:00
end
2006-02-08 17:20:05 +08:00
end