Handle case insensitive completions of variables better

darcs-hash:20080114010032-75c98-6e570c2b095baeb2ed2ee4d09e32f4e7d6ae47de.gz
This commit is contained in:
liljencrantz 2008-01-14 11:00:32 +10:00
parent 3743a5758b
commit a2660cfb76
2 changed files with 67 additions and 43 deletions

View File

@ -1662,7 +1662,7 @@ static int complete_variable( const wchar_t *whole_var,
{ {
sb_append_substring( &comp, whole_var, start_offset ); sb_append_substring( &comp, whole_var, start_offset );
sb_append( &comp, name ); sb_append( &comp, name );
flags = COMPLETE_NO_CASE; flags = COMPLETE_NO_CASE | COMPLETE_DONT_ESCAPE;
} }
value = expand_escape_variable( value_unescaped ); value = expand_escape_variable( value_unescaped );
@ -1671,9 +1671,9 @@ static int complete_variable( const wchar_t *whole_var,
sb_printf( &desc, COMPLETE_VAR_DESC_VAL, value ); sb_printf( &desc, COMPLETE_VAR_DESC_VAL, value );
completion_allocate( comp_list, completion_allocate( comp_list,
(wchar_t *)comp.buff, (wchar_t *)comp.buff,
(wchar_t *)desc.buff, (wchar_t *)desc.buff,
flags ); flags );
res =1; res =1;
free( value ); free( value );

102
reader.c
View File

@ -1111,8 +1111,8 @@ static void completion_insert( const wchar_t *val, int flags )
if( add_space ) if( add_space )
{ {
if( (quote) && if( quote &&
(data->buff[data->buff_pos] != quote ) ) (data->buff[data->buff_pos] != quote ) )
{ {
/* /*
This is a quoted parameter, first print a quote This is a quoted parameter, first print a quote
@ -1310,13 +1310,32 @@ static void reader_flash()
} }
#define UNCLEAN L"$*?({})" /**
Characters that may not be part of a token that is to be replaced
by a case insensitive completion.
*/
#define REPLACE_UNCLEAN L"$*?({})"
int reader_can_replace( const wchar_t *in ) /**
Check if the specified string can be replaced by a case insensitive
complition with the specified flags.
Advanced tokens like those containing {}-style expansion can not at
the moment be replaced, other than if the new token is already an
exact replacement, e.g. if the COMPLETE_DONT_ESCAPE flag is set.
*/
int reader_can_replace( const wchar_t *in, int flags )
{ {
const wchar_t * str = in; const wchar_t * str = in;
if( flags & COMPLETE_DONT_ESCAPE )
{
return 1;
}
CHECK( in, 1 ); CHECK( in, 1 );
/* /*
@ -1324,7 +1343,7 @@ int reader_can_replace( const wchar_t *in )
*/ */
while( *str ) while( *str )
{ {
if( wcschr( UNCLEAN, *str ) ) if( wcschr( REPLACE_UNCLEAN, *str ) )
return 0; return 0;
str++; str++;
} }
@ -1395,7 +1414,7 @@ static int handle_completions( array_list_t *comp )
the token doesn't contain evil operators the token doesn't contain evil operators
like {} like {}
*/ */
if( !(c->flags & COMPLETE_NO_CASE) || reader_can_replace( tok ) ) if( !(c->flags & COMPLETE_NO_CASE) || reader_can_replace( tok, c->flags ) )
{ {
completion_insert( c->completion, completion_insert( c->completion,
c->flags ); c->flags );
@ -1463,46 +1482,51 @@ static int handle_completions( array_list_t *comp )
if( begin ) if( begin )
{ {
if( reader_can_replace( tok ) ) int offset = wcslen( tok );
{
int offset = wcslen( tok );
count = 0;
for( i=0; i<al_get_count( comp ); i++ )
{
completion_t *c = (completion_t *)al_get( comp, i );
int new_len;
if( !(c->flags & COMPLETE_NO_CASE) )
continue;
count++; count = 0;
for( i=0; i<al_get_count( comp ); i++ )
{
completion_t *c = (completion_t *)al_get( comp, i );
int new_len;
if( base )
{ if( !(c->flags & COMPLETE_NO_CASE) )
new_len = offset + comp_ilen( base+offset, c->completion+offset ); continue;
len = new_len < len ? new_len: len;
} if( !reader_can_replace( tok, c->flags ) )
else {
{ len=0;
base = wcsdup( c->completion ); break;
len = wcslen( base );
flags = c->flags;
}
} }
if( len > offset ) count++;
{
if( count > 1 )
flags = flags | COMPLETE_NO_SPACE;
base[len]=L'\0'; if( base )
completion_insert( base, flags ); {
done = 1; new_len = offset + comp_ilen( base+offset, c->completion+offset );
len = new_len < len ? new_len: len;
}
else
{
base = wcsdup( c->completion );
len = wcslen( base );
flags = c->flags;
} }
} }
if( len > offset )
{
if( count > 1 )
flags = flags | COMPLETE_NO_SPACE;
base[len]=L'\0';
completion_insert( base, flags );
done = 1;
}
} }
} }