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 ); event_get( &search, &ev );
out.append(L"function "); out.append(L"function ");
out.append(name); if ( name[0]!=L'-' ){
out.append(name);
}
if (! desc.empty()) 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() ); 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 */ /* This forced tab is sort of crummy - not all functions start with a tab */
append_format( out, L"\n\t%ls", def.c_str()); 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 -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' function funced --description 'Edit function definition'
set -l external '' set -l editor $EDITOR
# Default to editing with EDITOR if set set -l interactive
if set -q EDITOR set -l funcname
set external $EDITOR while set -q argv[1]
end switch $argv[1]
case '---long impossible to match line, because case respects -h option' -h --help
# 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'
__fish_print_help funced __fish_print_help funced
return 0 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 '-*' case '-*'
printf (_ "%s: Unknown option %s\n") funced $argv set_color red
return 1 printf (_ "%s: Unknown option %s\n") funced $argv[1]
set_color normal
return 1
case '*' '.*' case '*' '.*'
set -l init '' set funcname $funcname $argv[1]
set -l tmp end
set -e argv[1]
end
if set -q external[1] if begin; set -q funcname[2]; or not test "$funcname[1]"; end
# Generate a temporary filename. set_color red
# mktemp has a different interface on different OSes, _ "funced: You must specify one function name
# or may not exist at all; thus we fake one up from our pid "
set -l tmpname (printf "$TMPDIR/fish_funced_%d.fish" %self) set_color normal
return 1
end
if functions -q $argv set -l init
functions $argv > $tmpname switch $funcname
else case '-*'
echo function $argv\n\nend > $tmpname set init function -- $funcname\n\nend
end case '*'
if eval $external $tmpname set init function $funcname\n\nend
. $tmpname end
end
set -l stat $status
rm $tmpname >/dev/null
return $stat
end
set -l IFS if begin; test "$editor" = fish; or set -q interactive[1]; end
if functions -q $argv set -l IFS
# Shadow IFS here to avoid array splitting in command substitution if functions -q -- $funcname
set init (functions $argv | fish_indent --no-indent) # Shadow IFS here to avoid array splitting in command substitution
else set init (functions -- $funcname | fish_indent --no-indent)
set init function $argv\nend end
end
set -l prompt 'printf "%s%s%s> " (set_color green) '$argv' (set_color normal)' set -l prompt 'printf "%s%s%s> " (set_color green) '$funcname' (set_color normal)'
# Unshadow IFS since the fish_title breaks otherwise # Unshadow IFS since the fish_title breaks otherwise
set -e IFS set -e IFS
if read -p $prompt -c "$init" -s cmd if read -p $prompt -c "$init" -s cmd
# Shadow IFS _again_ to avoid array splitting in command substitution # Shadow IFS _again_ to avoid array splitting in command substitution
set -l IFS set -l IFS
eval (echo -n $cmd | fish_indent) eval (echo -n $cmd | fish_indent)
end end
return 0 return 0
end 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) if not type -f "$editor" >/dev/null
end 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 end

View file

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