Add support for highlighting operators and escaped sequences

darcs-hash:20060526164638-ac50b-2259579e0460df0641683ce6127bb8516fca8c7e.gz
This commit is contained in:
axel 2006-05-27 02:46:38 +10:00
parent b5b49e7a4d
commit 18c185c256
6 changed files with 307 additions and 24 deletions

View File

@ -67,21 +67,6 @@ parts of fish.
*/
#define LOCKPOLLINTERVAL 10
/**
Highest legal ascii value
*/
#define ASCII_MAX 127u
/**
Highest legal 16-bit unicode value
*/
#define UCS2_MAX 0xffffu
/**
Highest legal byte value
*/
#define BYTE_MAX 0xffu
struct termios shell_modes;
int error_max=1;

View File

@ -36,6 +36,21 @@
*/
#define ENCODE_DIRECT_BASE 0xf100
/**
Highest legal ascii value
*/
#define ASCII_MAX 127u
/**
Highest legal 16-bit unicode value
*/
#define UCS2_MAX 0xffffu
/**
Highest legal byte value
*/
#define BYTE_MAX 0xffu
/**
Save the shell mode on startup so we can restore them on exit
*/

View File

@ -752,7 +752,7 @@ certain environment variables.
- \c BROWSER, which is the users preferred web browser. If this variable is set, fish will use the specified browser instead of the system default browser to display the fish documentation.
- \c CDPATH, which is an array of directories in which to search for the new directory for the \c cd builtin.
- \c fish_color_normal, \c fish_color_command, \c fish_color_substitution, \c fish_color_redirection, \c fish_color_end, \c fish_color_error, \c fish_color_param, \c fish_color_comment, \c fish_color_match, \c fish_color_search_match, \c fish_color_cwd, \c fish_pager_color_prefix, \c fish_pager_color_completion, \c fish_pager_color_description and \c fish_pager_color_progress are used to change the color of various elements in \c fish. These variables are universal, i.e. when changing them, their new value will be used by all running fish sessions. The new value will also be retained when restarting fish.
- \c fish_color_normal, \c fish_color_command, \c fish_color_substitution, \c fish_color_redirection, \c fish_color_end, \c fish_color_error, \c fish_color_param, \c fish_color_comment, \c fish_color_match, \c fish_color_search_match, \c fish_color_operator, \c fish_color_escape, \c fish_color_cwd, \c fish_pager_color_prefix, \c fish_pager_color_completion, \c fish_pager_color_description and \c fish_pager_color_progress are used to change the color of various elements in \c fish. These variables are universal, i.e. when changing them, their new value will be used by all running fish sessions. The new value will also be retained when restarting fish.
- \c PATH, which is an array of directories in which to search for commands
- \c umask, which is the current file creation mask. The preferred way to change the umask variable is through the <a href="commands.html#umask">umask shellscript function</a>. An attempt to set umask to an invalid value will always fail.

View File

@ -70,6 +70,8 @@ set_default fish_color_command green
set_default fish_color_redirection normal
set_default fish_color_comment brown
set_default fish_color_error red
set_default fish_color_escape cyan
set_default fish_color_operator cyan
set_default fish_color_cwd green

View File

@ -40,7 +40,7 @@ static void highlight_universal_internal( wchar_t * buff,
/**
The environment variables used to specify the color of different tokens.
*/
static wchar_t *hightlight_var[] =
static wchar_t *highlight_var[] =
{
L"fish_color_normal",
L"fish_color_command",
@ -52,24 +52,23 @@ static wchar_t *hightlight_var[] =
L"fish_color_comment",
L"fish_color_match",
L"fish_color_search_match",
L"fish_color_pager_prefix",
L"fish_color_pager_completion",
L"fish_color_pager_description",
L"fish_color_pager_progress"
L"fish_color_operator",
L"fish_color_escape"
}
;
#define VAR_COUNT ( sizeof(highlight_var)/sizeof(wchar_t *) )
int highlight_get_color( int highlight )
{
if( highlight < 0 )
return FISH_COLOR_NORMAL;
if( highlight >= (12) )
if( highlight >= VAR_COUNT )
return FISH_COLOR_NORMAL;
wchar_t *val = env_get( hightlight_var[highlight]);
wchar_t *val = env_get( highlight_var[highlight]);
if( val == 0 )
val = env_get( hightlight_var[HIGHLIGHT_NORMAL]);
val = env_get( highlight_var[HIGHLIGHT_NORMAL]);
if( val == 0 )
{
@ -79,6 +78,280 @@ int highlight_get_color( int highlight )
return output_color_code( val );
}
static void highlight_param( const wchar_t * buff,
int *color,
int pos,
array_list_t *error )
{
int mode = 0;
int in_pos, len = wcslen( buff );
int bracket_count=0;
wchar_t c;
for( in_pos=0;
in_pos<len;
in_pos++ )
{
c = buff[in_pos];
switch( mode )
{
/*
Mode 0 means unquoted string
*/
case 0:
{
if( c == L'\\' )
{
int start_pos = in_pos;
switch( buff[++in_pos] )
{
case L'n':
case L'r':
case L't':
case L'b':
case L'e':
{
color[start_pos]=HIGHLIGHT_ESCAPE;
color[in_pos+1]=HIGHLIGHT_NORMAL;
break;
}
case L'u':
case L'U':
case L'x':
case L'X':
case L'0':
case L'1':
case L'2':
case L'3':
case L'4':
case L'5':
case L'6':
case L'7':
{
int i;
long long res=0;
int chars=2;
int base=16;
int byte = 0;
wchar_t max_val = ASCII_MAX;
switch( buff[in_pos] )
{
case L'u':
{
chars=4;
max_val = UCS2_MAX;
break;
}
case L'U':
{
chars=8;
max_val = WCHAR_MAX;
break;
}
case L'x':
{
break;
}
case L'X':
{
byte=1;
max_val = BYTE_MAX;
break;
}
default:
{
base=8;
chars=3;
in_pos--;
break;
}
}
for( i=0; i<chars; i++ )
{
int d = convert_digit( buff[++in_pos],base);
if( d < 0 )
{
in_pos--;
break;
}
res=(res*base)|d;
}
if( (res <= max_val) )
{
color[start_pos] = HIGHLIGHT_ESCAPE;
color[in_pos+1] = HIGHLIGHT_NORMAL;
}
else
{
color[start_pos] = HIGHLIGHT_ERROR;
color[in_pos+1] = HIGHLIGHT_NORMAL;
}
break;
}
}
}
else
{
switch( buff[in_pos]){
case L'~':
case L'%':
{
if( in_pos == 0 )
{
color[in_pos] = HIGHLIGHT_OPERATOR;
color[in_pos+1] = HIGHLIGHT_NORMAL;
}
break;
}
case L'*':
case L'?':
case L'$':
{
color[in_pos] = HIGHLIGHT_OPERATOR;
color[in_pos+1] = HIGHLIGHT_NORMAL;
break;
}
case L'{':
{
bracket_count++;
break;
}
case L'}':
{
bracket_count--;
break;
}
case L',':
{
if( bracket_count )
{
color[in_pos] = HIGHLIGHT_OPERATOR;
color[in_pos+1] = HIGHLIGHT_NORMAL;
}
break;
}
case L'\'':
{
mode = 1;
break;
}
case L'\"':
{
mode = 2;
break;
}
}
}
break;
}
/*
Mode 1 means single quoted string, i.e 'foo'
*/
case 1:
{
if( c == L'\\' )
{
int start_pos = in_pos;
switch( buff[++in_pos] )
{
case '\\':
case L'\'':
{
color[start_pos] = HIGHLIGHT_ESCAPE;
color[in_pos+1] = HIGHLIGHT_NORMAL;
break;
}
case 0:
{
return;
}
}
}
if( c == L'\'' )
{
mode = 0;
}
break;
}
/*
Mode 2 means double quoted string, i.e. "foo"
*/
case 2:
{
switch( c )
{
case '"':
{
mode = 0;
break;
}
case '\\':
{
int start_pos = in_pos;
switch( buff[++in_pos] )
{
case L'\0':
{
return;
}
case '\\':
case L'$':
case '"':
{
color[start_pos] = HIGHLIGHT_ESCAPE;
color[in_pos+1] = HIGHLIGHT_NORMAL;
break;
}
}
break;
}
case '$':
{
color[in_pos] = HIGHLIGHT_OPERATOR;
color[in_pos+1] = HIGHLIGHT_NORMAL;
break;
}
}
break;
}
}
}
}
void highlight_shell( wchar_t * buff,
int *color,
@ -125,6 +398,12 @@ void highlight_shell( wchar_t * buff,
{
color[ tok_get_pos( &tok ) ] = HIGHLIGHT_PARAM;
}
highlight_param( param,
&color[tok_get_pos( &tok )],
pos-tok_get_pos( &tok ),
error );
}
else
{

View File

@ -22,6 +22,8 @@ enum
HIGHLIGHT_COMMENT,
HIGHLIGHT_MATCH,
HIGHLIGHT_SEARCH_MATCH,
HIGHLIGHT_OPERATOR,
HIGHLIGHT_ESCAPE,
}
;