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;
/********************************************************************/
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");
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;
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;
/*********************************************************************/
+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)
int nb_lines, char **lines,
int cursor_position,
char *pattern) {
-
+ int *switches;
char buffer[BUFFER_SIZE];
struct matcher matcher;
int k, l, m;
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);
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,
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++;
}
}
/* 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) {
}
}
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) {
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++;
/* We are done */
refresh();
+ if(switches) { free(switches); }
free_matcher(&matcher);
}
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) {
show_long_lines = 1;
break;
+ case 'y':
+ show_hits = 1;
+ break;
+
case 'u':
upper_caps_makes_case_sensitive = 1;
break;
attr_error = A_STANDOUT;
attr_modeline = A_REVERSE;
attr_focus_line = A_STANDOUT;
+ attr_hits = A_BOLD;
if(with_colors && has_colors()) {