mirror of
https://github.com/fish-shell/fish-shell
synced 2024-11-11 07:34:32 +00:00
Adding '--rename' option to 'functions' builtin.
Aim is to allow an existing function to be renamed, allowing some basic function chaining. Example: > function foo echo Hello end > foo Hello > functions --rename foo bar > foo fish: Unknown command 'foo' > bar Hello > functions --rename fish_prompt old_prompt > function fish_prompt printf "{Boo!}%s" (old_prompt) end {Boo!}> Note in the last case, the new fish_prompt is calling its old definition.
This commit is contained in:
parent
1eb089d722
commit
208be0f4d4
3 changed files with 134 additions and 7 deletions
67
builtin.c
67
builtin.c
|
@ -1252,6 +1252,7 @@ static int builtin_functions( wchar_t **argv )
|
|||
int show_hidden=0;
|
||||
int res = STATUS_BUILTIN_OK;
|
||||
int query = 0;
|
||||
int rename = 0;
|
||||
|
||||
woptind=0;
|
||||
|
||||
|
@ -1282,6 +1283,10 @@ static int builtin_functions( wchar_t **argv )
|
|||
L"query", no_argument, 0, 'q'
|
||||
}
|
||||
,
|
||||
{
|
||||
L"rename", no_argument, 0, 'r'
|
||||
}
|
||||
,
|
||||
{
|
||||
0, 0, 0, 0
|
||||
}
|
||||
|
@ -1294,7 +1299,7 @@ static int builtin_functions( wchar_t **argv )
|
|||
|
||||
int opt = wgetopt_long( argc,
|
||||
argv,
|
||||
L"ed:nahq",
|
||||
L"ed:nahqr",
|
||||
long_options,
|
||||
&opt_index );
|
||||
if( opt == -1 )
|
||||
|
@ -1305,10 +1310,10 @@ static int builtin_functions( wchar_t **argv )
|
|||
case 0:
|
||||
if(long_options[opt_index].flag != 0)
|
||||
break;
|
||||
sb_printf( sb_err,
|
||||
BUILTIN_ERR_UNKNOWN,
|
||||
argv[0],
|
||||
long_options[opt_index].name );
|
||||
sb_printf( sb_err,
|
||||
BUILTIN_ERR_UNKNOWN,
|
||||
argv[0],
|
||||
long_options[opt_index].name );
|
||||
builtin_print_help( argv[0], sb_err );
|
||||
|
||||
|
||||
|
@ -1338,6 +1343,10 @@ static int builtin_functions( wchar_t **argv )
|
|||
query = 1;
|
||||
break;
|
||||
|
||||
case 'r':
|
||||
rename = 1;
|
||||
break;
|
||||
|
||||
case '?':
|
||||
builtin_unknown_option( argv[0], argv[woptind-1] );
|
||||
return STATUS_BUILTIN_ERROR;
|
||||
|
@ -1347,9 +1356,9 @@ static int builtin_functions( wchar_t **argv )
|
|||
}
|
||||
|
||||
/*
|
||||
Erase, desc, query and list are mutually exclusive
|
||||
Erase, desc, query, rename and list are mutually exclusive
|
||||
*/
|
||||
if( (erase + (!!desc) + list + query) > 1 )
|
||||
if( (erase + (!!desc) + list + query + rename) > 1 )
|
||||
{
|
||||
sb_printf( sb_err,
|
||||
_( L"%ls: Invalid combination of options\n" ),
|
||||
|
@ -1434,6 +1443,50 @@ static int builtin_functions( wchar_t **argv )
|
|||
al_destroy( &names );
|
||||
return STATUS_BUILTIN_OK;
|
||||
}
|
||||
else if( rename )
|
||||
{
|
||||
wchar_t *current_func;
|
||||
wchar_t *new_func;
|
||||
|
||||
if( argc-woptind != 2 )
|
||||
{
|
||||
sb_printf( sb_err,
|
||||
_( L"%ls: Expected exactly two names (current function name, and new function name)\n" ),
|
||||
argv[0] );
|
||||
builtin_print_help ( argv[0], sb_err );
|
||||
|
||||
return STATUS_BUILTIN_ERROR;
|
||||
}
|
||||
current_func = argv[woptind];
|
||||
new_func = argv[woptind+1];
|
||||
|
||||
if( !function_exists( current_func ) )
|
||||
{
|
||||
sb_printf( sb_err,
|
||||
_( L"%ls: Function '%ls' does not exist\n" ),
|
||||
argv[0],
|
||||
current_func );
|
||||
builtin_print_help( argv[0], sb_err );
|
||||
|
||||
return STATUS_BUILTIN_ERROR;
|
||||
}
|
||||
|
||||
// keep things simple: don't allow existing names to be rename targets.
|
||||
if( function_exists( new_func ) )
|
||||
{
|
||||
sb_printf( sb_err,
|
||||
_( L"%ls: Function '%ls' already exists. Cannot rename '%ls'\n" ),
|
||||
argv[0],
|
||||
new_func,
|
||||
current_func );
|
||||
builtin_print_help( argv[0], sb_err );
|
||||
|
||||
return STATUS_BUILTIN_ERROR;
|
||||
}
|
||||
|
||||
function_rename( current_func, new_func );
|
||||
return STATUS_BUILTIN_OK;
|
||||
}
|
||||
|
||||
for( i=woptind; i<argc; i++ )
|
||||
{
|
||||
|
|
63
function.c
63
function.c
|
@ -218,6 +218,69 @@ void function_add( function_data_t *data )
|
|||
|
||||
}
|
||||
|
||||
int function_copy( const wchar_t *name, const wchar_t *new_name )
|
||||
{
|
||||
int i;
|
||||
function_internal_data_t *d, *orig_d;
|
||||
event_t ev, *orig_ev;
|
||||
array_list_t *fn_events;
|
||||
|
||||
CHECK( name, 0 );
|
||||
CHECK( new_name, 0 );
|
||||
|
||||
fn_events = 0;
|
||||
|
||||
orig_d = (function_internal_data_t *)hash_get(&function, name);
|
||||
if( !orig_d )
|
||||
return 0;
|
||||
|
||||
d = halloc(0, sizeof( function_internal_data_t ) );
|
||||
d->definition_offset = orig_d->definition_offset;
|
||||
d->definition = halloc_wcsdup( d, orig_d->definition );
|
||||
if( orig_d->named_arguments )
|
||||
{
|
||||
d->named_arguments = al_halloc( d );
|
||||
for( i=0; i<al_get_count( orig_d->named_arguments ); i++ )
|
||||
{
|
||||
al_push( d->named_arguments, halloc_wcsdup( d, (wchar_t *)al_get( orig_d->named_arguments, i ) ) );
|
||||
}
|
||||
d->description = orig_d->description?halloc_wcsdup(d, orig_d->description):0;
|
||||
d->definition_file = 0;
|
||||
d->is_autoload = 0;
|
||||
d->shadows = orig_d->shadows;
|
||||
}
|
||||
|
||||
hash_put( &function, intern(new_name), d );
|
||||
|
||||
// wire up the same events... if any.
|
||||
ev.type = EVENT_ANY;
|
||||
ev.function_name = name;
|
||||
event_get(&ev, fn_events);
|
||||
|
||||
if( fn_events )
|
||||
{
|
||||
ev.function_name = new_name;
|
||||
|
||||
for( i=0; i<al_get_count( fn_events ); i++ )
|
||||
{
|
||||
orig_ev = (event_t *)al_get( fn_events, i );
|
||||
|
||||
// event_add_handler will deep-copy ev, so we can reuse.
|
||||
ev.type = orig_ev->type;
|
||||
ev.param1 = orig_ev->param1;
|
||||
event_add_handler( &ev );
|
||||
}
|
||||
}
|
||||
return 1;
|
||||
}
|
||||
|
||||
void function_rename( const wchar_t *name, const wchar_t *new_name )
|
||||
{
|
||||
if( function_copy( name, new_name ) )
|
||||
function_remove( name );
|
||||
}
|
||||
|
||||
|
||||
int function_exists( const wchar_t *cmd )
|
||||
{
|
||||
|
||||
|
|
11
function.h
11
function.h
|
@ -131,4 +131,15 @@ array_list_t *function_get_named_arguments( const wchar_t *name );
|
|||
*/
|
||||
int function_get_shadows( const wchar_t *name );
|
||||
|
||||
/**
|
||||
Creates a new function using the same definition as the specified function.
|
||||
Returns non-zero if copy is successful.
|
||||
*/
|
||||
int function_copy( const wchar_t *name, const wchar_t *new_name );
|
||||
|
||||
/**
|
||||
Renames the specified function.
|
||||
*/
|
||||
void function_rename( const wchar_t *name, const wchar_t *new_name );
|
||||
|
||||
#endif
|
||||
|
|
Loading…
Reference in a new issue