}
}
+//////////////////////////////////////////////////////////////////////
+// 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) {
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 {
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) {
}
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,