Update funced

* Editor mode is no the default
* Use -i or --interactive or -e fish to edit function in interactive
  mode
* tmpname is now created with random number added and check that file
  do not already exist
* check $TMPDIR existence and put /tmp if it does not exist
* There is an undocumented feature to use functions, started with dash.
  Introduce necessary changes to funced, functions, def_function()
  in order to make it work properly.
* Delete editor guessing. Use $EDITOR variable or -e key
This commit is contained in:
maxfl 2012-06-30 10:22:41 +08:00 committed by ridiculousfish
parent bc9bae0f7f
commit ab62fe6496
4 changed files with 93 additions and 80 deletions

View file

@ -1097,7 +1097,9 @@ static void functions_def( const wcstring &name, wcstring &out )
event_get( &search, &ev );
out.append(L"function ");
out.append(name);
if ( name[0]!=L'-' ){
out.append(name);
}
if (! desc.empty())
{
@ -1165,7 +1167,11 @@ static void functions_def( const wcstring &name, wcstring &out )
append_format( out, L" %ls", named.at(i).c_str() );
}
}
if ( name[0]==L'-' ){
out.append(L" -- ");
out.append(name);
}
/* This forced tab is sort of crummy - not all functions start with a tab */
append_format( out, L"\n\t%ls", def.c_str());

View file

@ -1,2 +1,3 @@
complete -c funced -xa "(functions -na)" --description "Save function"
complete -c funced -s e -l editor -d 'Open function in external editor'
complete -c funced -s e -l editor -d 'Open function in external editor' -xa '(__fish_complete_command)'
complete -c funced -s i -l interactive -d 'Edit in interactive mode'

View file

@ -1,85 +1,91 @@
function funced --description 'Edit function definition'
set -l external ''
# Default to editing with EDITOR if set
if set -q EDITOR
set external $EDITOR
end
# 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
end
# 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
if test (count $argv) = 1
switch $argv
case '-h' '--h' '--he' '--hel' '--help'
set -l editor $EDITOR
set -l interactive
set -l funcname
while set -q argv[1]
switch $argv[1]
case '---long impossible to match line, because case respects -h option' -h --help
__fish_print_help funced
return 0
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]
case '-*'
printf (_ "%s: Unknown option %s\n") funced $argv
return 1
set_color red
printf (_ "%s: Unknown option %s\n") funced $argv[1]
set_color normal
return 1
case '*' '.*'
set -l init ''
set -l tmp
case '*' '.*'
set funcname $funcname $argv[1]
end
set -e argv[1]
end
if set -q external[1]
# 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)
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
if functions -q $argv
functions $argv > $tmpname
else
echo function $argv\n\nend > $tmpname
end
if eval $external $tmpname
. $tmpname
end
set -l stat $status
rm $tmpname >/dev/null
return $stat
end
set -l init
switch $funcname
case '-*'
set init function -- $funcname\n\nend
case '*'
set init function $funcname\n\nend
end
set -l IFS
if functions -q $argv
# Shadow IFS here to avoid array splitting in command substitution
set init (functions $argv | fish_indent --no-indent)
else
set init function $argv\nend
end
if begin; test "$editor" = fish; or set -q interactive[1]; end
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
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
set -l IFS
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
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
if not type -f "$editor" >/dev/null
set_color red
printf (_ "%s: Editor %s is not found\n") funced $editor
set_color normal
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
rm $tmpname >/dev/null
return $stat
end

View file

@ -30,9 +30,9 @@ function funcsave --description "Save the current definition of all specified fu
end
for i in $argv
if functions -q $i
functions $i > $configdir/fish/functions/$i.fish
functions -e $i
if functions -q -- $i
functions -- $i > $configdir/fish/functions/$i.fish
functions -e -- $i
else
printf (_ "%s: Unknown function '%s'\n") funcsave $i
set res 1