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

@ -2296,7 +2296,7 @@ static int builtin_fg( wchar_t **argv )
if( *end ) if( *end )
{ {
sb_printf( sb_err, sb_printf( sb_err,
_( L"%ls: Argument '%ls' is not a number\n" ), BUILTIN_ERR_NOT_NUMBER,
argv[0], argv[0],
argv[1] ); argv[1] );
builtin_print_help( argv[0], sb_err ); builtin_print_help( argv[0], sb_err );

View file

@ -84,6 +84,7 @@ enum
*/ */
#define BUILTIN_END_BLOCK_UNKNOWN _( L"%ls: Unknown block type '%ls'\n" ) #define BUILTIN_END_BLOCK_UNKNOWN _( L"%ls: Unknown block type '%ls'\n" )
#define BUILTIN_ERR_NOT_NUMBER _( L"%ls: Argument '%ls' is not a number\n" )
/** /**
Stringbuffer used to represent standard output Stringbuffer used to represent standard output
*/ */

View file

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

View file

@ -9,8 +9,22 @@
- \c CMD is the new value of the commandline. If unspecified, the - \c CMD is the new value of the commandline. If unspecified, the
current value of the commandline is written to standard output. current value of the commandline is written to standard output.
The following switches change what the commandline builtin does
- \c -C or \c --cursor set or get the current cursor position, not
the contents of the buffer. If no argument is given, the current
cursor position is printed, otherwise the argument is interpreted
as the new cursor position.
- \c -f or \c --function inject readline functions into the
reader. This option can not be combined with any other option. It
will cause any additional arguments to be interpreted as readline
functions, and these functions will be injected into the reader, so
that they will be returned to the reader before any additional
actual keypresses are read.
The following switches change the way \c commandline updates the The following switches change the way \c commandline updates the
commandline commandline buffer
- \c -a or \c --append do not remove the current commandline, append - \c -a or \c --append do not remove the current commandline, append
the specified string at the end of it the specified string at the end of it
@ -28,20 +42,12 @@ or updated
- \c -t or \c --current_token select the current token. - \c -t or \c --current_token select the current token.
The following switch changes the way \c commandline prints the current The following switch changes the way \c commandline prints the current
commandline commandline buffer
- \c -c or \c --cut-at-cursor only print selection up until the - \c -c or \c --cut-at-cursor only print selection up until the
current cursor position current cursor position
- \c -o or \c --tokenize tokenize the selection and print one string-type token per line - \c -o or \c --tokenize tokenize the selection and print one string-type token per line
Other switches
- \c -f or \c --function inject readline functions into the
reader. This option can not be combined with any other option. It
will cause any additional arguments to be interpreted as readline
functions, and these functions will be injected into the reader, so
that they will be returned to the reader before any additional
actual keypresses are read.
If commandline is called during a call to complete a given string If commandline is called during a call to complete a given string
using <code>complete -C STRING</code>, commandline will consider the using <code>complete -C STRING</code>, commandline will consider the

View file

@ -1253,8 +1253,6 @@ static int handle_completions( array_list_t *comp )
columns, then four, etc. completion_try_print always columns, then four, etc. completion_try_print always
succeeds with one column. succeeds with one column.
*/ */
/*
*/
} }
free( prefix ); free( prefix );
@ -1714,11 +1712,13 @@ void reader_set_buffer( wchar_t *b, int p )
data->buff_len = l; data->buff_len = l;
check_size(); check_size();
if( data->buff != b )
wcscpy( data->buff, b ); wcscpy( data->buff, b );
if( p>=0 ) if( p>=0 )
{ {
data->buff_pos=p; data->buff_pos=mini( p, l );
} }
else else
{ {

View file

@ -10,3 +10,6 @@ complete -c commandline -s b -l current-buffer -d (N_ "Select entire command lin
complete -c commandline -s c -l cut-at-cursor -d (N_ "Only return that part of the command line before the cursor") complete -c commandline -s c -l cut-at-cursor -d (N_ "Only return that part of the command line before the cursor")
complete -c commandline -s f -l function -d (N_ "Inject readline functions to reader") complete -c commandline -s f -l function -d (N_ "Inject readline functions to reader")
complete -c commandline -s I -l input -d (N_ "Specify command to operate on")
complete -c commandline -s C -l cursor -d (N_ "Set/get cursor position, not buffer contents")