Skip to content

Commit

Permalink
Handle case insensitive completions of variables better
Browse files Browse the repository at this point in the history
darcs-hash:20080114010032-75c98-6e570c2b095baeb2ed2ee4d09e32f4e7d6ae47de.gz
  • Loading branch information
liljencrantz committed Jan 14, 2008
1 parent 3743a57 commit a2660cf
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 42 deletions.
8 changes: 4 additions & 4 deletions complete.c
Original file line number Diff line number Diff line change
Expand Up @@ -1662,7 +1662,7 @@ static int complete_variable( const wchar_t *whole_var,
{
sb_append_substring( &comp, whole_var, start_offset );
sb_append( &comp, name );
flags = COMPLETE_NO_CASE;
flags = COMPLETE_NO_CASE | COMPLETE_DONT_ESCAPE;
}

value = expand_escape_variable( value_unescaped );
Expand All @@ -1671,9 +1671,9 @@ static int complete_variable( const wchar_t *whole_var,
sb_printf( &desc, COMPLETE_VAR_DESC_VAL, value );

completion_allocate( comp_list,
(wchar_t *)comp.buff,
(wchar_t *)desc.buff,
flags );
(wchar_t *)comp.buff,
(wchar_t *)desc.buff,
flags );
res =1;

free( value );
Expand Down
100 changes: 62 additions & 38 deletions reader.c
Original file line number Diff line number Diff line change
Expand Up @@ -1111,8 +1111,8 @@ static void completion_insert( const wchar_t *val, int flags )
if( add_space )
{

if( (quote) &&
(data->buff[data->buff_pos] != quote ) )
if( quote &&
(data->buff[data->buff_pos] != quote ) )
{
/*
This is a quoted parameter, first print a quote
Expand Down Expand Up @@ -1310,21 +1310,40 @@ 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;

if( flags & COMPLETE_DONT_ESCAPE )
{
return 1;
}


CHECK( in, 1 );

/*
Test characters that have a special meaning in any character position
*/
while( *str )
{
if( wcschr( UNCLEAN, *str ) )
if( wcschr( REPLACE_UNCLEAN, *str ) )
return 0;
str++;
}
Expand Down Expand Up @@ -1395,7 +1414,7 @@ static int handle_completions( array_list_t *comp )
the token doesn't contain evil operators
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,
c->flags );
Expand Down Expand Up @@ -1463,46 +1482,51 @@ static int handle_completions( array_list_t *comp )
if( begin )
{

if( reader_can_replace( tok ) )
int offset = wcslen( tok );

count = 0;

for( i=0; i<al_get_count( comp ); i++ )
{
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;
completion_t *c = (completion_t *)al_get( comp, i );
int new_len;

if( !(c->flags & COMPLETE_NO_CASE) )
continue;

count++;

if( base )
{
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( !(c->flags & COMPLETE_NO_CASE) )
continue;

if( !reader_can_replace( tok, c->flags ) )
{
len=0;
break;
}

if( len > offset )
{
if( count > 1 )
flags = flags | COMPLETE_NO_SPACE;
count++;

base[len]=L'\0';
completion_insert( base, flags );
done = 1;
if( base )
{
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;
}

}
}

Expand Down

0 comments on commit a2660cf

Please sign in to comment.