Change the history so that when you go forwards, it no longer highlights

This commit is contained in:
ridiculousfish 2012-02-05 23:22:18 -08:00
parent c82410bfda
commit b14683200a
3 changed files with 39 additions and 43 deletions

View file

@ -385,6 +385,12 @@ void history_search_t::go_to_end(void) {
prev_matches.clear(); prev_matches.clear();
} }
/** Returns if we are at the end, which is where we start. */
bool history_search_t::is_at_end(void) const {
return prev_matches.empty();
}
/** Goes to the beginning (backwards) */ /** Goes to the beginning (backwards) */
void history_search_t::go_to_beginning(void) { void history_search_t::go_to_beginning(void) {
/* Just go backwards as far as we can */ /* Just go backwards as far as we can */

View file

@ -30,7 +30,10 @@ class history_item_t {
public: public:
const wcstring &str() const { return contents; } const wcstring &str() const { return contents; }
bool empty() const { return contents.empty(); } bool empty() const { return contents.empty(); }
bool matches_search(const wcstring &val) const { return contents.find(val) != wcstring::npos; }
/* We consider equal strings to NOT match a search (so that you don't have to see history equal to what you typed) */
bool matches_search(const wcstring &val) const { return contents.size() > val.size() && contents.find(val) != wcstring::npos; }
time_t timestamp() const { return creation_timestamp; } time_t timestamp() const { return creation_timestamp; }
bool write_to_file(FILE *f) const; bool write_to_file(FILE *f) const;
@ -124,6 +127,9 @@ class history_search_t {
/** Goes to the end (forwards) */ /** Goes to the end (forwards) */
void go_to_end(void); void go_to_end(void);
/** Returns if we are at the end. We start out at the end. */
bool is_at_end(void) const;
/** Goes to the beginning (backwards) */ /** Goes to the beginning (backwards) */
void go_to_beginning(void); void go_to_beginning(void);

View file

@ -213,7 +213,7 @@ class reader_data_t
/** /**
Saved search string for token history search. Not handled by check_size. Saved search string for token history search. Not handled by check_size.
*/ */
const wchar_t *token_history_buff; wcstring token_history_buff;
/** /**
List for storing previous search results. Used to avoid duplicates. List for storing previous search results. Used to avoid duplicates.
@ -1789,18 +1789,14 @@ void reader_replace_current_token( const wchar_t *new_token )
Set the specified string from the history as the current buffer. Do Set the specified string from the history as the current buffer. Do
not modify prefix_width. not modify prefix_width.
*/ */
static void handle_history( const wchar_t *new_str ) static void handle_history( const wcstring &new_str )
{ {
if( new_str ) data->buff_len = new_str.size();
{ check_size();
data->buff_len = wcslen( new_str ); wcscpy( data->buff, new_str.c_str() );
check_size(); data->buff_pos=wcslen(data->buff);
wcscpy( data->buff, new_str ); reader_super_highlight_me_plenty( data->buff_pos, 0 );
data->buff_pos=wcslen(data->buff); reader_repaint();
reader_super_highlight_me_plenty( data->buff_pos, 0 );
reader_repaint();
}
} }
/** /**
@ -1873,26 +1869,20 @@ static void handle_token_history( int forward, int reset )
{ {
if( current_pos == -1 ) if( current_pos == -1 )
{ {
/* data->token_history_buff.clear();
Move to previous line
*/
free( (void *)data->token_history_buff );
/* /*
Search for previous item that contains this substring Search for previous item that contains this substring
*/ */
if (! data->history_search.go_backwards()) { if (data->history_search.go_backwards()) {
/* No luck */
data->token_history_buff = wcsdup(L"");
} else {
wcstring item = data->history_search.current_item(); wcstring item = data->history_search.current_item();
data->token_history_buff = wcsdup(item.c_str()); data->token_history_buff = data->history_search.current_item();
} }
current_pos = wcslen(data->token_history_buff); current_pos = data->token_history_buff.size();
} }
if( ! wcslen( data->token_history_buff ) ) if( data->token_history_buff.empty() )
{ {
/* /*
We have reached the end of the history - check if the We have reached the end of the history - check if the
@ -1913,9 +1903,9 @@ static void handle_token_history( int forward, int reset )
else else
{ {
//debug( 3, L"new '%ls'", data->token_history_buff ); //debug( 3, L"new '%ls'", data->token_history_buff.c_str() );
for( tok_init( &tok, data->token_history_buff, TOK_ACCEPT_UNFINISHED ); for( tok_init( &tok, data->token_history_buff.c_str(), TOK_ACCEPT_UNFINISHED );
tok_has_next( &tok); tok_has_next( &tok);
tok_next( &tok )) tok_next( &tok ))
{ {
@ -2287,8 +2277,6 @@ void reader_push( const wchar_t *name )
reader_set_test_function( &default_test ); reader_set_test_function( &default_test );
reader_set_prompt( L"" ); reader_set_prompt( L"" );
//history_set_mode( name ); //history_set_mode( name );
data->token_history_buff=0;
} }
void reader_pop() void reader_pop()
@ -2309,11 +2297,6 @@ void reader_pop()
free( n->indent ); free( n->indent );
sb_destroy( &n->kill_item ); sb_destroy( &n->kill_item );
/*
Clean up after history search
*/
free( (void *)n->token_history_buff);
/* Invoke the destructor to balance our new */ /* Invoke the destructor to balance our new */
delete n; delete n;
@ -2403,7 +2386,7 @@ public:
/* Called to set the highlight flag for search results */ /* Called to set the highlight flag for search results */
static void highlight_search(void) { static void highlight_search(void) {
if( ! data->search_buff.empty()) if( ! data->search_buff.empty() && ! data->history_search.is_at_end())
{ {
wchar_t * match = wcsstr( data->buff, data->search_buff.c_str() ); wchar_t * match = wcsstr( data->buff, data->search_buff.c_str() );
if( match ) if( match )
@ -3117,25 +3100,26 @@ wchar_t *reader_readline()
case LINE_SEARCH: case LINE_SEARCH:
{ {
bool success;
if( ( c == R_HISTORY_SEARCH_BACKWARD ) || if( ( c == R_HISTORY_SEARCH_BACKWARD ) ||
( c == R_HISTORY_TOKEN_SEARCH_BACKWARD ) ) ( c == R_HISTORY_TOKEN_SEARCH_BACKWARD ) )
{ {
success = data->history_search.go_backwards(); data->history_search.go_backwards();
} }
else else
{ {
success = data->history_search.go_forwards(); if (! data->history_search.go_forwards()) {
/* If you try to go forwards past the end, we just go to the end */
data->history_search.go_to_end();
}
} }
wcstring new_text; wcstring new_text;
if (success) { if (data->history_search.is_at_end()) {
new_text = data->history_search.current_item();
} else {
new_text = data->search_buff; new_text = data->search_buff;
} else {
new_text = data->history_search.current_item();
} }
handle_history( new_text.c_str() ); handle_history(new_text);
break; break;
} }