From: Francois Fleuret Date: Fri, 10 Feb 2012 07:23:06 +0000 (+0100) Subject: Wrote the nightmarish add_interval. X-Git-Url: https://fleuret.org/cgi-bin/gitweb/gitweb.cgi?a=commitdiff_plain;h=9229710556737ecf747c2e36df872d6641b706bf;p=selector.git Wrote the nightmarish add_interval. --- diff --git a/selector.c b/selector.c index 3246a00..07160f5 100644 --- a/selector.c +++ b/selector.c @@ -67,8 +67,9 @@ char *title = 0; int error_flash = 0; int upper_caps_makes_case_sensitive = 0; int show_long_lines = 0; +int show_hits = 0; -int attr_modeline, attr_focus_line, attr_error; +int attr_modeline, attr_focus_line, attr_error, attr_hits; /********************************************************************/ @@ -191,6 +192,8 @@ void usage(FILE *out) { fprintf(out, " start in case sensitive mode\n"); fprintf(out, " -j, --show-long-lines\n"); fprintf(out, " print three dots at the end of truncated lines\n"); + fprintf(out, " -y, --show-hits\n"); + fprintf(out, " highlight the matching substrings\n"); fprintf(out, " -u, --upper-case-makes-case-sensitive\n"); fprintf(out, " using an upper case character in the matching string makes\n"); fprintf(out, " the matching case-sensitive\n"); @@ -316,16 +319,98 @@ struct matcher { char *splitted_patterns, **patterns; }; -int match(struct matcher *matcher, char *string) { - int n; +/* Routine to add an interval to a sorted list of intervals + extermities. Returns the number of extremities. This is an effing + nightmare */ + +int add_interval(int n, int *switches, int start, int end) { + int f, g, k; + + f = 0; + while(f < n && switches[f] <= start) { f++; } + g = f; + while(g < n && switches[g] <= end) { g++; } + + if(f == n) { + /* switches[n] start end */ + /* XXXXXXXXXX| */ + switches[f] = start; + switches[f+1] = end; + return n + 2; + } + + if(f%2) { + + if(f == g) { + /* switches[f-1] start end switches[f] */ + /* |XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX| */ + return n; + } + + if(g%2) { + /* switches[f-1] start switches[f] switches[g-1] end switches[g] */ + /* |XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX| ... |XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX| */ + for(k = f; k < n; k++) { switches[k] = switches[(k - f) + g]; } + return n - (g - f); + } else { + /* switches[f-1] start switches[f] switches[g-1] end switches[g] */ + /* |XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX| ... XXXXXXXXXXXX| |XXXXXXXXXX */ + switches[g - 1] = end; + for(k = f; k < n; k++) { switches[k] = switches[(k - f) + (g - 1)]; } + return n - ((g - 1) - f); + } + + } else { + + if(f == g) { + /* switches[f-1] start end switches[f] */ + /* XXXXXXXXXXXX| |XXXXXXXXXX */ + for(k = n; k >= f; k--) { + switches[k + 2] = switches[k]; + } + switches[f] = start; + switches[f + 1] = end; + return n + 2; + } + + if(g%2) { + /* switches[f-1] start switches[f] switches[g-1] end switches[g] */ + /* XXXXXXXXXXXX| |XXXXXXXXXX ... |XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX| */ + switches[f] = start; + for(k = f + 1; k < n; k++) { switches[k] = switches[(k - (f + 1)) + g]; } + return n - (g - (f + 1)); + } else { + /* switches[f-1] start switches[f] switches[g-1] end switches[g] */ + /* XXXXXXXXXXXX| |XXXXXXXXXX ... XXXXXXXXXXXX| |XXXXXXXXXX */ + switches[f] = start; + switches[g - 1] = end; + for(k = f + 1; k < n; k++) { switches[k] = switches[(k - (f + 1)) + (g - 1)]; } + return n - ((g - 1) - (f + 1)); + } + } +} + +int match(struct matcher *matcher, char *string, int *switches) { + int n, nb_switches = 0; + char *where; if(matcher->nb_patterns >= 0) { if(matcher->case_sensitive) { for(n = 0; n < matcher->nb_patterns; n++) { - if(strstr(string, matcher->patterns[n]) == 0) return 0; + if((where = strstr(string, matcher->patterns[n])) == 0) return 0; + if(switches) { + nb_switches = add_interval(nb_switches, switches, + (int) (where - string), + (int) (where - string) + strlen(matcher->patterns[n])); + } } } else { for(n = 0; n < matcher->nb_patterns; n++) { - if(strcasestr(string, matcher->patterns[n]) == 0) return 0; + if((where = strcasestr(string, matcher->patterns[n])) == 0) return 0; + if(switches) { + nb_switches = add_interval(nb_switches, switches, + (int) (where - string), + (int) (where - string) + strlen(matcher->patterns[n])); + } } } return 1; @@ -456,14 +541,14 @@ void kill_after_cursor(char *buffer, int *position) { int previous_visible(int current_line, char **lines, struct matcher *matcher) { int line = current_line - 1; - while(line >= 0 && !match(matcher, lines[line])) line--; + while(line >= 0 && !match(matcher, lines[line], 0)) line--; return line; } int next_visible(int current_line, int nb_lines, char **lines, struct matcher *matcher) { int line = current_line + 1; - while(line < nb_lines && !match(matcher, lines[line])) line++; + while(line < nb_lines && !match(matcher, lines[line], 0)) line++; if(line < nb_lines) return line; @@ -473,6 +558,33 @@ int next_visible(int current_line, int nb_lines, char **lines, /*********************************************************************/ +void print_string_with_switches(char *buffer, int line_width, + int console_width, + int nb_patterns, int *switches) { + int w, current = 0, next; + if(switches) { + for(w = 0; w < nb_patterns && switches[2 * w] < line_width; w++) { + if(switches[2 * w] < switches[2 * w + 1]) { + next = switches[2 * w]; + if(next > line_width) { next = line_width; } + addnstr(buffer + current, next - current); + attron(attr_hits); + current = next; + next = switches[2 * w + 1]; + if(next > line_width) { next = line_width; } + addnstr(buffer + current, next - current); + attroff(attr_hits); + current = next; + } + } + if(current < line_width) { + addnstr(buffer + current, console_width - current); + } + } else { + addnstr(buffer, console_width); + } +} + /* The line highlighted is the first one matching the matcher in that order: (1) current_focus_line after motion, if it does not match, then (2) the first with a greater index, if none matches, then (3) @@ -490,7 +602,7 @@ void update_screen(int *current_focus_line, int *displayed_focus_line, int nb_lines, char **lines, int cursor_position, char *pattern) { - + int *switches; char buffer[BUFFER_SIZE]; struct matcher matcher; int k, l, m; @@ -500,6 +612,12 @@ void update_screen(int *current_focus_line, int *displayed_focus_line, initialize_matcher(&matcher, use_regexp, case_sensitive, pattern); + if(show_hits && matcher.nb_patterns > 0) { + switches = safe_malloc(sizeof(int) * matcher.nb_patterns * 2); + } else { + switches = 0; + } + console_width = getmaxx(stdscr); console_height = getmaxy(stdscr); @@ -521,7 +639,7 @@ void update_screen(int *current_focus_line, int *displayed_focus_line, else if(nb_lines > 0) { int new_focus_line; - if(match(&matcher, lines[*current_focus_line])) { + if(match(&matcher, lines[*current_focus_line], 0)) { new_focus_line = *current_focus_line; } else { new_focus_line = next_visible(*current_focus_line, nb_lines, lines, @@ -571,21 +689,21 @@ void update_screen(int *current_focus_line, int *displayed_focus_line, if(first_line > 0) { first_line--; - while(first_line > 0 && !match(&matcher, lines[first_line])) { + while(first_line > 0 && !match(&matcher, lines[first_line], 0)) { first_line--; } - if(match(&matcher, lines[first_line])) { + if(match(&matcher, lines[first_line], 0)) { nb_match++; } } if(nb_match < console_height - 1 && last_line < nb_lines - 1) { last_line++; - while(last_line < nb_lines - 1 && !match(&matcher, lines[last_line])) { + while(last_line < nb_lines - 1 && !match(&matcher, lines[last_line], 0)) { last_line++; } - if(match(&matcher, lines[last_line])) { + if(match(&matcher, lines[last_line], 0)) { nb_match++; } } @@ -594,7 +712,7 @@ void update_screen(int *current_focus_line, int *displayed_focus_line, /* Now we display them */ for(l = first_line; l <= last_line; l++) { - if(match(&matcher, lines[l])) { + if(match(&matcher, lines[l], switches)) { int k = 0; while(lines[l][k] && k < BUFFER_SIZE - 2 && k < console_width) { @@ -618,7 +736,8 @@ void update_screen(int *current_focus_line, int *displayed_focus_line, } } attron(attr_focus_line); - addnstr(buffer, console_width); + print_string_with_switches(buffer, k, console_width, + matcher.nb_patterns, switches); attroff(attr_focus_line); } else { if(show_long_lines && k >= console_width) { @@ -632,8 +751,8 @@ void update_screen(int *current_focus_line, int *displayed_focus_line, buffer[k++] = '\n'; buffer[k++] = '\0'; } - /* clrtoeol(); */ - addnstr(buffer, console_width); + print_string_with_switches(buffer, k, console_width, + matcher.nb_patterns, switches); } nb_printed_lines++; @@ -728,6 +847,7 @@ void update_screen(int *current_focus_line, int *displayed_focus_line, /* We are done */ refresh(); + if(switches) { free(switches); } free_matcher(&matcher); } @@ -867,7 +987,7 @@ int main(int argc, char **argv) { strcpy(output_filename, ""); - while ((c = getopt_long(argc, argv, "o:s:x:vwmqf:ibzdeajunt:l:c:-h", + while ((c = getopt_long(argc, argv, "o:s:x:vwmqf:ibzdeajyunt:l:c:-h", long_options, NULL)) != -1) { switch(c) { @@ -928,6 +1048,10 @@ int main(int argc, char **argv) { show_long_lines = 1; break; + case 'y': + show_hits = 1; + break; + case 'u': upper_caps_makes_case_sensitive = 1; break; @@ -1073,6 +1197,7 @@ int main(int argc, char **argv) { attr_error = A_STANDOUT; attr_modeline = A_REVERSE; attr_focus_line = A_STANDOUT; + attr_hits = A_BOLD; if(with_colors && has_colors()) {