From b7e259513408fd65013e161e1d98f00d6b7edeb8 Mon Sep 17 00:00:00 2001 From: Francois Fleuret Date: Wed, 8 Apr 2009 08:00:11 +0200 Subject: [PATCH] Moved the buffer edition into dedicated functions. --- selector.cc | 120 ++++++++++++++++++++++++++++++++-------------------- 1 file changed, 73 insertions(+), 47 deletions(-) diff --git a/selector.cc b/selector.cc index cf9df59..50a852b 100644 --- a/selector.cc +++ b/selector.cc @@ -210,6 +210,65 @@ void initialize_matcher(int use_regexp, int case_sensitive, } } +////////////////////////////////////////////////////////////////////// +// Buffer edition + +void delete_char(char *buffer, int *position) { + if(buffer[*position]) { + int c = *position; + while(c < buffer_size && buffer[c]) { + buffer[c] = buffer[c+1]; + c++; + } + } +} + +void backspace_char(char *buffer, int *position) { + if(*position > 0) { + if(buffer[*position]) { + int c = *position - 1; + while(buffer[c]) { + buffer[c] = buffer[c+1]; + c++; + } + } else { + buffer[*position - 1] = '\0'; + } + + (*position)--; + } +} + +void insert_char(char *buffer, int *position, char character) { + if(strlen(buffer) < buffer_size - 1) { + int c = *position; + char t = buffer[c], u; + while(t) { + c++; + u = buffer[c]; + buffer[c] = t; + t = u; + } + c++; + buffer[c] = '\0'; + buffer[(*position)++] = character; + } +} + +void kill_before_cursor(char *buffer, int *position) { + int s = 0; + while(buffer[*position + s]) { + buffer[s] = buffer[*position + s]; + s++; + } + buffer[s] = '\0'; + *position = 0; +} + +void kill_after_cursor(char *buffer, int *position) { + buffer[*position] = '\0'; +} + ////////////////////////////////////////////////////////////////////// int previous_visible(int current_line, int nb_lines, char **lines, matcher_t *matcher) { @@ -746,16 +805,16 @@ int main(int argc, char **argv) { update_screen(¤t_line, &temporary_line, 0, nb_lines, lines, cursor_position, pattern); - // \000 ^@ - // \001 ^A + // ^@ \000 + // ^A \001 // ... - // \032 ^Z - // \033 ^[ - // \034 ^\ - // \035 ^] - // \036 ^^ - // \037 ^_ - // \177 ^? + // ^Z \032 + // ^[ \033 + // ^\ \034 + // ^] \035 + // ^^ \036 + // ^_ \037 + // ^? \177 do { @@ -764,42 +823,15 @@ int main(int argc, char **argv) { int motion = 0; if(key >= ' ' && key <= '~') { // Insert character - int c = cursor_position; - char t = pattern[c], u; - while(t) { - c++; - u = pattern[c]; - pattern[c] = t; - t = u; - } - c++; - pattern[c] = '\0'; - pattern[cursor_position++] = key; + insert_char(pattern, &cursor_position, key); } else if(key == KEY_BACKSPACE || key == '\010' || key == '\177') { - if(cursor_position > 0) { - if(pattern[cursor_position]) { - int c = cursor_position-1; - while(pattern[c]) { - pattern[c] = pattern[c+1]; - c++; - } - } else { - pattern[cursor_position - 1] = '\0'; - } - cursor_position--; - } + backspace_char(pattern, &cursor_position); } else if(key == KEY_DC || key == '\004') { - if(pattern[cursor_position]) { - int c = cursor_position; - while(pattern[c]) { - pattern[c] = pattern[c+1]; - c++; - } - } + delete_char(pattern, &cursor_position); } else if(key == KEY_HOME) { @@ -847,17 +879,11 @@ int main(int argc, char **argv) { } else if(key == '\025') { - int s = 0; - while(pattern[cursor_position + s]) { - pattern[s] = pattern[cursor_position + s]; - s++; - } - pattern[s] = '\0'; - cursor_position = 0; + kill_before_cursor(pattern, &cursor_position); } else if(key == '\013') { - pattern[cursor_position] = '\0'; + kill_after_cursor(pattern, &cursor_position); } update_screen(¤t_line, &temporary_line, motion, -- 2.39.5