2012-06-27 03:37:48 +00:00
|
|
|
function funced --description 'Edit function definition'
|
2012-06-30 02:22:41 +00:00
|
|
|
set -l editor $EDITOR
|
|
|
|
set -l interactive
|
|
|
|
set -l funcname
|
|
|
|
while set -q argv[1]
|
|
|
|
switch $argv[1]
|
2012-07-02 03:19:30 +00:00
|
|
|
case -h --help
|
2007-04-22 18:55:39 +00:00
|
|
|
__fish_print_help funced
|
|
|
|
return 0
|
|
|
|
|
2012-06-30 02:22:41 +00:00
|
|
|
case -e --editor
|
|
|
|
set editor $argv[2]
|
|
|
|
set -e argv[2]
|
|
|
|
|
|
|
|
case -i --interactive
|
|
|
|
set interactive 1
|
|
|
|
|
|
|
|
case --
|
|
|
|
set funcname $funcname $argv[2]
|
|
|
|
set -e argv[2]
|
|
|
|
|
2007-04-22 18:55:39 +00:00
|
|
|
case '-*'
|
2012-06-30 02:22:41 +00:00
|
|
|
set_color red
|
|
|
|
printf (_ "%s: Unknown option %s\n") funced $argv[1]
|
|
|
|
set_color normal
|
|
|
|
return 1
|
2007-04-22 18:55:39 +00:00
|
|
|
|
2012-06-30 02:22:41 +00:00
|
|
|
case '*' '.*'
|
|
|
|
set funcname $funcname $argv[1]
|
|
|
|
end
|
|
|
|
set -e argv[1]
|
|
|
|
end
|
2010-09-18 02:18:26 +00:00
|
|
|
|
2012-06-30 02:22:41 +00:00
|
|
|
if begin; set -q funcname[2]; or not test "$funcname[1]"; end
|
|
|
|
set_color red
|
|
|
|
_ "funced: You must specify one function name
|
|
|
|
"
|
|
|
|
set_color normal
|
|
|
|
return 1
|
|
|
|
end
|
2012-06-27 03:37:48 +00:00
|
|
|
|
2012-06-30 02:22:41 +00:00
|
|
|
set -l init
|
|
|
|
switch $funcname
|
|
|
|
case '-*'
|
|
|
|
set init function -- $funcname\n\nend
|
|
|
|
case '*'
|
|
|
|
set init function $funcname\n\nend
|
|
|
|
end
|
2012-06-27 03:37:48 +00:00
|
|
|
|
2012-06-30 02:35:31 +00:00
|
|
|
if not type -f "$editor" >/dev/null
|
|
|
|
set interactive 1
|
|
|
|
end
|
|
|
|
|
|
|
|
if begin; set -q interactive[1]; or test "$editor" = fish; end
|
2012-06-30 02:22:41 +00: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-22 18:55:39 +00:00
|
|
|
|
2012-06-30 02:22:41 +00: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
|
|
|
|
|
|
|
|
set -q TMPDIR; or set -l TMPDIR /tmp
|
|
|
|
set -l tmpname (printf "$TMPDIR/fish_funced_%d_%d.fish" %self (random))
|
|
|
|
while test -f $tmpname
|
|
|
|
set tmpname (printf "$TMPDIR/fish_funced_%d_%d.fish" %self (random))
|
|
|
|
end
|
|
|
|
|
|
|
|
if functions -q -- $funcname
|
|
|
|
functions -- $funcname > $tmpname
|
|
|
|
else
|
|
|
|
echo $init > $tmpname
|
|
|
|
end
|
|
|
|
if eval $editor $tmpname
|
|
|
|
. $tmpname
|
|
|
|
end
|
|
|
|
set -l stat $status
|
2012-07-23 23:05:37 +00:00
|
|
|
rm -f $tmpname >/dev/null
|
2012-06-30 02:22:41 +00:00
|
|
|
return $stat
|
|
|
|
end
|