2012-06-27 11:37:48 +08:00
|
|
|
function funced --description 'Edit function definition'
|
2013-01-24 09:14:22 +08:00
|
|
|
set -l editor $EDITOR
|
2012-06-30 10:22:41 +08:00
|
|
|
set -l interactive
|
|
|
|
set -l funcname
|
|
|
|
while set -q argv[1]
|
|
|
|
switch $argv[1]
|
2013-01-24 09:14:22 +08:00
|
|
|
case -h --help
|
|
|
|
__fish_print_help funced
|
|
|
|
return 0
|
2007-04-23 02:55:39 +08:00
|
|
|
|
2012-06-30 10:22:41 +08:00
|
|
|
case -e --editor
|
2013-01-24 09:14:22 +08:00
|
|
|
set editor $argv[2]
|
|
|
|
set -e argv[2]
|
2012-11-18 18:23:22 +08:00
|
|
|
|
2012-06-30 10:22:41 +08:00
|
|
|
case -i --interactive
|
2013-01-24 09:14:22 +08:00
|
|
|
set interactive 1
|
2012-06-30 10:22:41 +08:00
|
|
|
|
|
|
|
case --
|
2013-01-24 09:14:22 +08:00
|
|
|
set funcname $funcname $argv[2]
|
|
|
|
set -e argv[2]
|
2012-06-30 10:22:41 +08:00
|
|
|
|
2013-01-24 09:14:22 +08:00
|
|
|
case '-*'
|
|
|
|
set_color red
|
|
|
|
printf (_ "%s: Unknown option %s\n") funced $argv[1]
|
|
|
|
set_color normal
|
|
|
|
return 1
|
2007-04-23 02:55:39 +08:00
|
|
|
|
2012-06-30 10:22:41 +08:00
|
|
|
case '*' '.*'
|
2013-01-24 09:14:22 +08:00
|
|
|
set funcname $funcname $argv[1]
|
2012-06-30 10:22:41 +08:00
|
|
|
end
|
|
|
|
set -e argv[1]
|
|
|
|
end
|
2010-09-18 10:18:26 +08:00
|
|
|
|
2013-06-09 11:29:38 +08:00
|
|
|
if test (count $funcname) -ne 1
|
2012-06-30 10:22:41 +08:00
|
|
|
set_color red
|
|
|
|
_ "funced: You must specify one function name
|
|
|
|
"
|
|
|
|
set_color normal
|
|
|
|
return 1
|
|
|
|
end
|
2012-06-27 11:37:48 +08:00
|
|
|
|
2012-06-30 10:22:41 +08:00
|
|
|
set -l init
|
|
|
|
switch $funcname
|
|
|
|
case '-*'
|
|
|
|
set init function -- $funcname\n\nend
|
|
|
|
case '*'
|
|
|
|
set init function $funcname\n\nend
|
|
|
|
end
|
2012-06-27 11:37:48 +08:00
|
|
|
|
2013-01-24 10:24:49 +08:00
|
|
|
# Break editor up to get its first command (i.e. discard flags)
|
2013-01-28 05:14:24 +08:00
|
|
|
if test -n "$editor"
|
|
|
|
set -l editor_cmd
|
|
|
|
eval set editor_cmd $editor
|
|
|
|
if not type -f "$editor_cmd[1]" >/dev/null
|
|
|
|
_ "funced: The value for \$EDITOR '$editor' could not be used because the command '$editor_cmd[1]' could not be found
|
|
|
|
"
|
|
|
|
set editor fish
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
# If no editor is specified, use fish
|
|
|
|
if test -z "$editor"
|
|
|
|
set editor fish
|
2012-06-30 10:35:31 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
if begin; set -q interactive[1]; or test "$editor" = fish; end
|
2012-06-30 10:22:41 +08:00
|
|
|
set -l IFS
|
|
|
|
if functions -q -- $funcname
|
|
|
|
# Shadow IFS here to avoid array splitting in command substitution
|
|
|
|
set init (functions -- $funcname | fish_indent --no-indent)
|
|
|
|
end
|
2007-04-23 02:55:39 +08:00
|
|
|
|
2012-06-30 10:22:41 +08:00
|
|
|
set -l prompt 'printf "%s%s%s> " (set_color green) '$funcname' (set_color normal)'
|
|
|
|
# Unshadow IFS since the fish_title breaks otherwise
|
|
|
|
set -e IFS
|
|
|
|
if read -p $prompt -c "$init" -s cmd
|
|
|
|
# Shadow IFS _again_ to avoid array splitting in command substitution
|
|
|
|
set -l IFS
|
|
|
|
eval (echo -n $cmd | fish_indent)
|
|
|
|
end
|
|
|
|
return 0
|
|
|
|
end
|
|
|
|
|
2014-04-20 23:51:20 +08:00
|
|
|
set tmpname (mktemp -t fish_funced.XXXXXXXXXX)
|
2012-06-30 10:22:41 +08:00
|
|
|
|
|
|
|
if functions -q -- $funcname
|
|
|
|
functions -- $funcname > $tmpname
|
|
|
|
else
|
|
|
|
echo $init > $tmpname
|
|
|
|
end
|
|
|
|
if eval $editor $tmpname
|
|
|
|
. $tmpname
|
|
|
|
end
|
2012-11-18 18:23:22 +08:00
|
|
|
set -l stat $status
|
2012-07-24 07:05:37 +08:00
|
|
|
rm -f $tmpname >/dev/null
|
2012-06-30 10:22:41 +08:00
|
|
|
return $stat
|
|
|
|
end
|