Skip to content

Commit

Permalink
Make up/down cursor move up or down when in multiline mode, except if…
Browse files Browse the repository at this point in the history
… already in search mode or at the top/bottom line. Since part of this is done in script-space, this involves adding some functionality to the commandline builtin.

darcs-hash:20070921140549-75c98-ba9e83f5e6fdecae5df8f83dd863794c6af9770c.gz
  • Loading branch information
liljencrantz committed Sep 21, 2007
1 parent 607e970 commit d2d397d
Show file tree
Hide file tree
Showing 9 changed files with 308 additions and 29 deletions.
46 changes: 39 additions & 7 deletions builtin_commandline.c
Original file line number Diff line number Diff line change
Expand Up @@ -220,6 +220,8 @@ static int builtin_commandline( wchar_t **argv )
int tokenize = 0;

int cursor_mode = 0;
int line_mode = 0;
int search_mode = 0;
wchar_t *begin, *end;

current_buffer = (wchar_t *)builtin_complete_get_temporary_buffer();
Expand Down Expand Up @@ -312,6 +314,14 @@ static int builtin_commandline( wchar_t **argv )
L"cursor", no_argument, 0, 'C'
}
,
{
L"line", no_argument, 0, 'L'
}
,
{
L"search-mode", no_argument, 0, 'S'
}
,
{
0, 0, 0, 0
}
Expand All @@ -322,7 +332,7 @@ static int builtin_commandline( wchar_t **argv )

int opt = wgetopt_long( argc,
argv,
L"abijpctwforhI:C",
L"abijpctwforhI:CLS",
long_options,
&opt_index );
if( opt == -1 )
Expand Down Expand Up @@ -391,6 +401,14 @@ static int builtin_commandline( wchar_t **argv )
cursor_mode = 1;
break;

case 'L':
line_mode = 1;
break;

case 'S':
search_mode = 1;
break;

case 'h':
builtin_print_help( argv[0], sb_out );
return 0;
Expand All @@ -408,7 +426,7 @@ static int builtin_commandline( wchar_t **argv )
/*
Check for invalid switch combinations
*/
if( buffer_part || cut_at_cursor || append_mode || tokenize || cursor_mode )
if( buffer_part || cut_at_cursor || append_mode || tokenize || cursor_mode || line_mode || search_mode )
{
sb_printf(sb_err,
BUILTIN_ERR_COMBO,
Expand Down Expand Up @@ -457,7 +475,7 @@ static int builtin_commandline( wchar_t **argv )
/*
Check for invalid switch combinations
*/
if( cursor_mode && (argc-woptind > 1) )
if( (search_mode || line_mode || cursor_mode) && (argc-woptind > 1) )
{

sb_append2( sb_err,
Expand All @@ -468,7 +486,7 @@ static int builtin_commandline( wchar_t **argv )
return 1;
}

if( (buffer_part || tokenize || cut_at_cursor) && cursor_mode )
if( (buffer_part || tokenize || cut_at_cursor) && (cursor_mode || line_mode || search_mode) )
{
sb_printf( sb_err,
BUILTIN_ERR_COMBO,
Expand Down Expand Up @@ -527,9 +545,9 @@ static int builtin_commandline( wchar_t **argv )
if( *endptr || errno )
{
sb_printf( sb_err,
BUILTIN_ERR_NOT_NUMBER,
argv[0],
argv[woptind] );
BUILTIN_ERR_NOT_NUMBER,
argv[0],
argv[woptind] );
builtin_print_help( argv[0], sb_err );
}

Expand All @@ -546,6 +564,20 @@ static int builtin_commandline( wchar_t **argv )

}

if( line_mode )
{
int pos = reader_get_cursor_pos();
wchar_t *buff = reader_get_buffer();
sb_printf( sb_out, L"%d\n", parse_util_lineno( buff, pos ) );
return 0;

}

if( search_mode )
{
return !reader_search_mode();
}


switch( buffer_part )
{
Expand Down
16 changes: 10 additions & 6 deletions input.c
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,9 @@ static const wchar_t *name_arr[] =
L"execute",
L"beginning-of-buffer",
L"end-of-buffer",
L"repaint"
L"repaint",
L"up-line",
L"down-line"
}
;

Expand Down Expand Up @@ -215,7 +217,9 @@ static const wchar_t code_arr[] =
R_EXECUTE,
R_BEGINNING_OF_BUFFER,
R_END_OF_BUFFER,
R_REPAINT
R_REPAINT,
R_UP_LINE,
R_DOWN_LINE
}
;

Expand Down Expand Up @@ -1242,10 +1246,10 @@ static void add_common_bindings()
terminfo sometimes specifies a different sequence than what
keypresses actually generate
*/
add_mapping( name[i], L"\x1b[A", L"Up", L"history-search-backward" );
add_mapping( name[i], L"\x1b[B", L"Down", L"history-search-forward" );
add_terminfo_mapping( name[i], (key_up), L"Up", L"history-search-backward" );
add_terminfo_mapping( name[i], (key_down), L"Down", L"history-search-forward" );
add_mapping( name[i], L"\x1b[A", L"Up", L"up-or-search" );
add_mapping( name[i], L"\x1b[B", L"Down", L"down-or-search" );
add_terminfo_mapping( name[i], (key_up), L"Up", L"up-or-search" );
add_terminfo_mapping( name[i], (key_down), L"Down", L"down-or-search" );

add_mapping( name[i], L"\x1b[C", L"Right", L"forward-char" );
add_mapping( name[i], L"\x1b[D", L"Left", L"backward-char" );
Expand Down
4 changes: 3 additions & 1 deletion input.h
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,9 @@ enum
R_EXECUTE,
R_BEGINNING_OF_BUFFER,
R_END_OF_BUFFER,
R_REPAINT
R_REPAINT,
R_UP_LINE,
R_DOWN_LINE,
}
;

Expand Down
91 changes: 91 additions & 0 deletions parse_util.c
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,97 @@ int parse_util_lineno( const wchar_t *str, int len )
return res;
}


int parse_util_get_line_from_offset( wchar_t *buff, int pos )
{
// return parse_util_lineno( buff, pos );

int i;
int count = 0;
if( pos < 0 )
{
return -1;
}

for( i=0; i<pos; i++ )
{
if( !buff[i] )
{
return -1;
}

if( buff[i] == L'\n' )
{
count++;
}
}
return count;
}


int parse_util_get_offset_from_line( wchar_t *buff, int line )
{
int i;
int count = 0;

if( line < 0 )
{
return -1;
}

if( line == 0 )
return 0;

for( i=0;; i++ )
{
if( !buff[i] )
{
return -1;
}

if( buff[i] == L'\n' )
{
count++;
if( count == line )
{
return i+1;
}

}
}
}

int parse_util_get_offset( wchar_t *buff, int line, int line_offset )
{
int off = parse_util_get_offset_from_line( buff, line );
int off2 = parse_util_get_offset_from_line( buff, line+1 );
int line_offset2 = line_offset;

if( off < 0 )
{
return -1;
}

if( off2 < 0 )
{
off2 = wcslen( buff );
}

if( line_offset2 < 0 )
{
line_offset2 = 0;
}

if( line_offset2 >= off2-off-1 )
{
line_offset2 = off2-off-1;
}

return off + line_offset2;

}


int parse_util_locate_cmdsubst( const wchar_t *in,
wchar_t **begin,
wchar_t **end,
Expand Down
17 changes: 17 additions & 0 deletions parse_util.h
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,23 @@ void parse_util_token_extent( const wchar_t *buff,
*/
int parse_util_lineno( const wchar_t *str, int len );

/**
Calculate the line number of the specified cursor position
*/
int parse_util_get_line_from_offset( wchar_t *buff, int pos );

/**
Get the offset of the first character on the specified line
*/
int parse_util_get_offset_from_line( wchar_t *buff, int line );


/**
Return the total offset of the buffer for the cursor position nearest to the specified poition
*/
int parse_util_get_offset( wchar_t *buff, int line, int line_offset );


/**
Autoload the specified file, if it exists in the specified path. Do
not load it multiple times unless it's timestamp changes or
Expand Down
Loading

0 comments on commit d2d397d

Please sign in to comment.