mirror of
https://github.com/fish-shell/fish-shell
synced 2025-01-13 21:44:16 +00:00
Fix bug where case insensitive file completions would get directory components removed
darcs-hash:20070324190738-ac50b-2f94de910083eae1fe563284b9953071df706072.gz
This commit is contained in:
parent
f6b3fcb4f5
commit
3f4b47b4af
3 changed files with 310 additions and 228 deletions
75
complete.c
75
complete.c
|
@ -1593,13 +1593,14 @@ static void complete_param_expand( wchar_t *str,
|
|||
/**
|
||||
Complete the specified string as an environment variable
|
||||
*/
|
||||
static int complete_variable( const wchar_t *var,
|
||||
array_list_t *comp )
|
||||
static int complete_variable( const wchar_t *whole_var,
|
||||
int start_offset,
|
||||
array_list_t *comp_list )
|
||||
{
|
||||
int i;
|
||||
const wchar_t *var = &whole_var[start_offset];
|
||||
int varlen = wcslen( var );
|
||||
int res = 0;
|
||||
|
||||
array_list_t names;
|
||||
al_init( &names );
|
||||
env_get_names( &names, 0 );
|
||||
|
@ -1608,11 +1609,19 @@ static int complete_variable( const wchar_t *var,
|
|||
{
|
||||
wchar_t *name = (wchar_t *)al_get( &names, i );
|
||||
int namelen = wcslen( name );
|
||||
int match=0, match_no_case=0;
|
||||
|
||||
if( varlen > namelen )
|
||||
continue;
|
||||
|
||||
if( wcsncmp( var, name, varlen) == 0 )
|
||||
match = ( wcsncmp( var, name, varlen) == 0 );
|
||||
|
||||
if( !match )
|
||||
{
|
||||
match_no_case = ( wcsncasecmp( var, name, varlen) == 0 );
|
||||
}
|
||||
|
||||
if( match || match_no_case )
|
||||
{
|
||||
wchar_t *value_unescaped, *value;
|
||||
|
||||
|
@ -1620,20 +1629,37 @@ static int complete_variable( const wchar_t *var,
|
|||
if( value_unescaped )
|
||||
{
|
||||
string_buffer_t desc;
|
||||
string_buffer_t comp;
|
||||
int flags = 0;
|
||||
int offset = 0;
|
||||
|
||||
sb_init( &comp );
|
||||
if( match )
|
||||
{
|
||||
sb_append( &comp, &name[varlen] );
|
||||
offset = varlen;
|
||||
}
|
||||
else
|
||||
{
|
||||
sb_append_substring( &comp, whole_var, start_offset );
|
||||
sb_append( &comp, name );
|
||||
flags = COMPLETE_NO_CASE;
|
||||
}
|
||||
|
||||
value = expand_escape_variable( value_unescaped );
|
||||
|
||||
sb_init( &desc );
|
||||
sb_printf( &desc, COMPLETE_VAR_DESC_VAL, value );
|
||||
|
||||
completion_allocate( comp,
|
||||
&name[varlen],
|
||||
completion_allocate( comp_list,
|
||||
(wchar_t *)comp.buff,
|
||||
(wchar_t *)desc.buff,
|
||||
0 );
|
||||
flags );
|
||||
res =1;
|
||||
|
||||
free( value );
|
||||
sb_destroy( &desc );
|
||||
sb_destroy( &comp );
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -1660,7 +1686,7 @@ static int try_complete_variable( const wchar_t *cmd,
|
|||
if( cmd[i] == L'$' )
|
||||
{
|
||||
/* wprintf( L"Var prefix \'%ls\'\n", &cmd[i+1] );*/
|
||||
return complete_variable( &cmd[i+1], comp );
|
||||
return complete_variable( cmd, i+1, comp );
|
||||
}
|
||||
if( !isalnum(cmd[i]) && cmd[i]!=L'_' )
|
||||
{
|
||||
|
@ -1790,7 +1816,7 @@ static int try_complete_user( const wchar_t *cmd,
|
|||
void complete( const wchar_t *cmd,
|
||||
array_list_t *comp )
|
||||
{
|
||||
wchar_t *begin, *end, *prev_begin, *prev_end;
|
||||
wchar_t *tok_begin, *tok_end, *cmdsubst_begin, *cmdsubst_end, *prev_begin, *prev_end;
|
||||
wchar_t *buff;
|
||||
tokenizer tok;
|
||||
wchar_t *current_token=0, *current_command=0, *prev_token=0;
|
||||
|
@ -1812,35 +1838,30 @@ void complete( const wchar_t *cmd,
|
|||
|
||||
cursor_pos = wcslen(cmd );
|
||||
|
||||
parse_util_cmdsubst_extent( cmd, cursor_pos, &cmdsubst_begin, &cmdsubst_end );
|
||||
parse_util_token_extent( cmd, cursor_pos, &tok_begin, &tok_end, &prev_begin, &prev_end );
|
||||
|
||||
if( !cmdsubst_begin )
|
||||
done=1;
|
||||
|
||||
/**
|
||||
If we are completing a variable name or a tilde expansion user
|
||||
name, we do that and return. No need for any other competions.
|
||||
*/
|
||||
|
||||
if( try_complete_variable( cmd, comp ) || try_complete_user( cmd, comp ))
|
||||
if( !done )
|
||||
{
|
||||
if( try_complete_variable( tok_begin, comp ) || try_complete_user( tok_begin, comp ))
|
||||
{
|
||||
done=1;
|
||||
}
|
||||
|
||||
/*
|
||||
Set on_command to true if cursor is over a command, and set the
|
||||
name of the current command, and various other parsing to find
|
||||
out what we should complete, and how it should be completed.
|
||||
*/
|
||||
|
||||
if( !done )
|
||||
{
|
||||
parse_util_cmdsubst_extent( cmd, cursor_pos, &begin, &end );
|
||||
|
||||
if( !begin )
|
||||
done=1;
|
||||
}
|
||||
|
||||
if( !done )
|
||||
{
|
||||
pos = cursor_pos-(begin-cmd);
|
||||
pos = cursor_pos-(cmdsubst_begin-cmd);
|
||||
|
||||
buff = wcsndup( begin, end-begin );
|
||||
buff = wcsndup( cmdsubst_begin, cmdsubst_end-cmdsubst_begin );
|
||||
|
||||
if( !buff )
|
||||
done=1;
|
||||
|
@ -1948,9 +1969,7 @@ void complete( const wchar_t *cmd,
|
|||
Get the string to complete
|
||||
*/
|
||||
|
||||
parse_util_token_extent( cmd, cursor_pos, &begin, &end, &prev_begin, &prev_end );
|
||||
|
||||
current_token = wcsndup( begin, cursor_pos-(begin-cmd) );
|
||||
current_token = wcsndup( tok_begin, cursor_pos-(tok_begin-cmd) );
|
||||
|
||||
prev_token = prev_begin ? wcsndup( prev_begin, prev_end - prev_begin ): wcsdup(L"");
|
||||
|
||||
|
|
55
reader.c
55
reader.c
|
@ -1231,22 +1231,42 @@ static int handle_completions( array_list_t *comp )
|
|||
int done = 0;
|
||||
int count = 0;
|
||||
int flags=0;
|
||||
wchar_t *begin, *end;
|
||||
wchar_t *tok;
|
||||
|
||||
if( al_get_count( comp ) == 0 )
|
||||
{
|
||||
reader_flash();
|
||||
return 0;
|
||||
}
|
||||
|
||||
if( al_get_count( comp ) == 1 )
|
||||
{
|
||||
completion_t *c = (completion_t *)al_get( comp, 0 );
|
||||
completion_insert( c->completion,
|
||||
c->flags );
|
||||
return 1;
|
||||
}
|
||||
parse_util_token_extent( data->buff, data->buff_pos, &begin, 0, 0, 0 );
|
||||
end = data->buff+data->buff_pos;
|
||||
|
||||
context = halloc( 0, 0 );
|
||||
tok = halloc_wcsndup( context, begin, end-begin );
|
||||
|
||||
switch( al_get_count( comp ) )
|
||||
{
|
||||
case 0:
|
||||
{
|
||||
reader_flash();
|
||||
done = 1;
|
||||
break;
|
||||
}
|
||||
|
||||
case 1:
|
||||
{
|
||||
|
||||
completion_t *c = (completion_t *)al_get( comp, 0 );
|
||||
|
||||
if( !(c->flags & COMPLETE_NO_CASE) || expand_is_clean( tok ) )
|
||||
{
|
||||
completion_insert( c->completion,
|
||||
c->flags );
|
||||
}
|
||||
done = 1;
|
||||
len = 1;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
if( !done )
|
||||
{
|
||||
|
||||
for( i=0; i<al_get_count( comp ); i++ )
|
||||
{
|
||||
|
@ -1280,17 +1300,14 @@ static int handle_completions( array_list_t *comp )
|
|||
completion_insert(base, flags);
|
||||
done = 1;
|
||||
}
|
||||
}
|
||||
|
||||
if( base == 0 )
|
||||
|
||||
if( !done && base == 0 )
|
||||
{
|
||||
wchar_t *begin, *end;
|
||||
|
||||
parse_util_token_extent( data->buff, data->buff_pos, &begin, 0, 0, 0 );
|
||||
|
||||
if( begin )
|
||||
{
|
||||
end = data->buff+data->buff_pos;
|
||||
wchar_t *tok = halloc_wcsndup( context, begin, end-begin );
|
||||
|
||||
if( expand_is_clean( tok ) )
|
||||
{
|
||||
|
|
54
wildcard.c
54
wildcard.c
|
@ -802,7 +802,7 @@ static int test_flags( wchar_t *filename,
|
|||
}
|
||||
|
||||
|
||||
int wildcard_expand( const wchar_t *wc,
|
||||
static int wildcard_expand_internal( const wchar_t *wc,
|
||||
const wchar_t *base_dir,
|
||||
int flags,
|
||||
array_list_t *out )
|
||||
|
@ -854,7 +854,7 @@ int wildcard_expand( const wchar_t *wc,
|
|||
{
|
||||
wchar_t * foo = wcsdup( wc );
|
||||
foo[len-1]=0;
|
||||
int res = wildcard_expand( foo, base_dir, flags, out );
|
||||
int res = wildcard_expand_internal( foo, base_dir, flags, out );
|
||||
free( foo );
|
||||
return res;
|
||||
}
|
||||
|
@ -1125,7 +1125,7 @@ int wildcard_expand( const wchar_t *wc,
|
|||
}
|
||||
}
|
||||
|
||||
new_res = wildcard_expand( new_wc,
|
||||
new_res = wildcard_expand_internal( new_wc,
|
||||
new_dir,
|
||||
flags,
|
||||
out );
|
||||
|
@ -1144,10 +1144,12 @@ int wildcard_expand( const wchar_t *wc,
|
|||
*/
|
||||
if( partial_match )
|
||||
{
|
||||
new_res = wildcard_expand( wcschr( wc, ANY_STRING_RECURSIVE ),
|
||||
|
||||
new_res = wildcard_expand_internal( wcschr( wc, ANY_STRING_RECURSIVE ),
|
||||
new_dir,
|
||||
flags | WILDCARD_RECURSIVE,
|
||||
out );
|
||||
|
||||
if( new_res == -1 )
|
||||
{
|
||||
res = -1;
|
||||
|
@ -1175,3 +1177,47 @@ int wildcard_expand( const wchar_t *wc,
|
|||
return res;
|
||||
}
|
||||
|
||||
|
||||
int wildcard_expand( const wchar_t *wc,
|
||||
const wchar_t *base_dir,
|
||||
int flags,
|
||||
array_list_t *out )
|
||||
{
|
||||
int c = al_get_count( out );
|
||||
int res = wildcard_expand_internal( wc, base_dir, flags, out );
|
||||
int i;
|
||||
|
||||
if( flags & ACCEPT_INCOMPLETE )
|
||||
{
|
||||
wchar_t *wc_base;
|
||||
wchar_t *wc_base_ptr = wcsrchr( wc, L'/' );
|
||||
|
||||
if( wc_base_ptr )
|
||||
{
|
||||
string_buffer_t sb;
|
||||
|
||||
sb_init( &sb );
|
||||
wc_base = wcsndup( wc, (wc_base_ptr-wc)+1 );
|
||||
|
||||
for( i=c; i<al_get_count( out ); i++ )
|
||||
{
|
||||
completion_t *c = al_get( out, i );
|
||||
|
||||
if( c->flags & COMPLETE_NO_CASE )
|
||||
{
|
||||
sb_clear( &sb );
|
||||
sb_printf( &sb, L"%ls%ls", wc_base, c->completion );
|
||||
|
||||
c->completion = halloc_wcsdup( out, (wchar_t *)sb.buff );
|
||||
}
|
||||
}
|
||||
|
||||
sb_destroy( &sb );
|
||||
free( wc_base );
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
return res;
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue