2012-06-27 03:37:48 +00:00
|
|
|
function funced --description 'Edit function definition'
|
2012-06-30 01:35:26 +00:00
|
|
|
set -l external ''
|
|
|
|
# Default to editing with EDITOR if set
|
|
|
|
if set -q EDITOR
|
|
|
|
set external $EDITOR
|
|
|
|
end
|
2007-04-22 18:55:39 +00:00
|
|
|
|
2012-06-30 01:35:26 +00:00
|
|
|
# Allow overriding the editor with a -e flag
|
|
|
|
if test (count $argv) -eq 3
|
|
|
|
if contains -- $argv[1] -e --editor
|
|
|
|
set external $argv[2]
|
|
|
|
set -e argv[2]
|
|
|
|
set -e argv[1]
|
|
|
|
end
|
2012-06-27 03:37:48 +00:00
|
|
|
end
|
2012-06-30 01:35:26 +00:00
|
|
|
|
|
|
|
# Let an editor of "fish" mean to use the default editor
|
|
|
|
if test "$external" = fish
|
|
|
|
set -e external
|
|
|
|
end
|
|
|
|
|
|
|
|
# Make sure any external editor exists
|
|
|
|
if set -q external
|
|
|
|
if not type -f "$external" >/dev/null
|
|
|
|
set -e external
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2007-04-22 18:55:39 +00:00
|
|
|
if test (count $argv) = 1
|
|
|
|
switch $argv
|
|
|
|
|
|
|
|
case '-h' '--h' '--he' '--hel' '--help'
|
|
|
|
__fish_print_help funced
|
|
|
|
return 0
|
|
|
|
|
|
|
|
case '-*'
|
|
|
|
printf (_ "%s: Unknown option %s\n") funced $argv
|
|
|
|
return 1
|
|
|
|
|
2012-06-28 07:05:36 +00:00
|
|
|
case '*' '.*'
|
2007-04-22 18:55:39 +00:00
|
|
|
set -l init ''
|
|
|
|
set -l tmp
|
2010-09-18 02:18:26 +00:00
|
|
|
|
2012-06-27 03:37:48 +00:00
|
|
|
if set -q external[1]
|
2012-06-30 01:35:26 +00:00
|
|
|
# Generate a temporary filename.
|
|
|
|
# mktemp has a different interface on different OSes,
|
|
|
|
# or may not exist at all; thus we fake one up from our pid
|
|
|
|
set -l tmpname (printf "$TMPDIR/fish_funced_%d.fish" %self)
|
2012-06-27 03:37:48 +00:00
|
|
|
|
|
|
|
if functions -q $argv
|
|
|
|
functions $argv > $tmpname
|
|
|
|
else
|
2012-06-30 01:35:26 +00:00
|
|
|
echo function $argv\n\nend > $tmpname
|
|
|
|
end
|
|
|
|
if eval $external $tmpname
|
|
|
|
. $tmpname
|
2012-06-27 03:37:48 +00:00
|
|
|
end
|
|
|
|
set -l stat $status
|
|
|
|
rm $tmpname >/dev/null
|
|
|
|
return $stat
|
|
|
|
end
|
|
|
|
|
|
|
|
set -l IFS
|
2007-04-22 18:55:39 +00:00
|
|
|
if functions -q $argv
|
2012-06-27 03:37:48 +00:00
|
|
|
# Shadow IFS here to avoid array splitting in command substitution
|
|
|
|
set init (functions $argv | fish_indent --no-indent)
|
2007-09-21 14:29:54 +00:00
|
|
|
else
|
|
|
|
set init function $argv\nend
|
2007-04-22 18:55:39 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
set -l prompt 'printf "%s%s%s> " (set_color green) '$argv' (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
|
2010-09-18 02:18:26 +00:00
|
|
|
set -l IFS
|
2007-04-22 18:55:39 +00:00
|
|
|
eval (echo -n $cmd | fish_indent)
|
|
|
|
end
|
|
|
|
return 0
|
|
|
|
end
|
|
|
|
else
|
|
|
|
printf (_ '%s: Expected exactly one argument, got %s.\n\nSynopsis:\n\t%sfunced%s FUNCTION\n') funced (count $argv) (set_color $fish_color_command) (set_color $fish_color_normal)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|