Add the possibility to set the cursor position using the commandline builtin

darcs-hash:20061004213948-ac50b-3f673edeb01390bb3f280812d90bc8469f2f8ba8.gz
This commit is contained in:
axel
2006-10-05 07:39:48 +10:00
parent 7d73349889
commit 91c745e4b5
6 changed files with 86 additions and 39 deletions

View File

@@ -58,7 +58,7 @@ static int current_cursor_pos = -1;
/**
Returns the current commandline buffer.
*/
static const wchar_t *get_buffer()
static wchar_t *get_buffer()
{
return current_buffer;
}
@@ -206,6 +206,9 @@ static int builtin_commandline( wchar_t **argv )
int function_mode = 0;
int tokenize = 0;
int cursor_mode = 0;
wchar_t *begin, *end;
current_buffer = (wchar_t *)builtin_complete_get_temporary_buffer();
if( current_buffer )
@@ -283,6 +286,10 @@ static int builtin_commandline( wchar_t **argv )
L"input", required_argument, 0, 'I'
}
,
{
L"cursor", no_argument, 0, 'C'
}
,
{
0, 0, 0, 0
}
@@ -293,7 +300,7 @@ static int builtin_commandline( wchar_t **argv )
int opt = wgetopt_long( argc,
argv,
L"aijpctwforhI:",
L"aijpctwforhI:C",
long_options,
&opt_index );
if( opt == -1 )
@@ -353,7 +360,10 @@ static int builtin_commandline( wchar_t **argv )
current_cursor_pos = wcslen( woptarg );
break;
case 'C':
cursor_mode = 1;
break;
case 'h':
builtin_print_help( argv[0], sb_out );
return 0;
@@ -371,16 +381,13 @@ static int builtin_commandline( wchar_t **argv )
/*
Check for invalid switch combinations
*/
if( buffer_part || cut_at_cursor || append_mode || tokenize )
if( buffer_part || cut_at_cursor || append_mode || tokenize || cursor_mode )
{
sb_printf(sb_err,
BUILTIN_ERR_COMBO,
argv[0] );
sb_append2(sb_err,
parser_current_line(),
L"\n",
(void *)0);
builtin_print_help( argv[0], sb_err );
return 1;
}
@@ -391,10 +398,6 @@ static int builtin_commandline( wchar_t **argv )
BUILTIN_ERR_MISSING,
argv[0] );
sb_append2( sb_err,
parser_current_line(),
L"\n",
(void *)0 );
builtin_print_help( argv[0], sb_err );
return 1;
}
@@ -403,7 +406,6 @@ static int builtin_commandline( wchar_t **argv )
wint_t c = input_get_code( argv[i] );
if( c != -1 )
{
// fwprintf( stderr, L"Add function %ls (%d)\n", argv[i], c );
/*
input_unreadch inserts the specified keypress or
readline function at the top of the stack of unused
@@ -413,15 +415,10 @@ static int builtin_commandline( wchar_t **argv )
}
else
{
// fwprintf( stderr, L"BLUR %ls %d\n", argv[i], c );
sb_append2( sb_err,
argv[0],
L": Unknown readline function '",
argv[i],
L"'\n",
parser_current_line(),
L"\n",
(void *)0 );
sb_printf( sb_err,
_(L"%ls: Unknown readline function '%ls'\n"),
argv[0],
argv[i] );
builtin_print_help( argv[0], sb_err );
return 1;
}
@@ -444,9 +441,19 @@ static int builtin_commandline( wchar_t **argv )
return 1;
}
if( (buffer_part || tokenize || cut_at_cursor) && cursor_mode )
{
sb_printf( sb_err,
BUILTIN_ERR_COMBO,
argv[0] );
builtin_print_help( argv[0], sb_err );
return 1;
}
if( (tokenize || cut_at_cursor) && (argc-woptind) )
{
sb_printf( sb_err,
BUILTIN_ERR_COMBO2,
argv[0],
@@ -480,9 +487,39 @@ static int builtin_commandline( wchar_t **argv )
{
buffer_part = STRING_MODE;
}
if( cursor_mode )
{
if( argc-woptind )
{
wchar_t *endptr;
int new_pos;
errno = 0;
new_pos = wcstol( argv[woptind], &endptr, 10 );
if( *endptr || errno )
{
sb_printf( sb_err,
BUILTIN_ERR_NOT_NUMBER,
argv[0],
argv[woptind] );
builtin_print_help( argv[0], sb_err );
}
current_buffer = reader_get_buffer();
new_pos = maxi( 0, mini( new_pos, wcslen( current_buffer ) ) );
reader_set_buffer( current_buffer, new_pos );
return 0;
}
else
{
sb_printf( sb_out, L"%d\n", reader_get_cursor_pos() );
return 0;
}
wchar_t *begin, *end;
}
switch( buffer_part )
{
case STRING_MODE: