Cosmetics. The debug check for interval order is still there.
[selector.git] / selector.c
index a4d9ee5..cf90315 100644 (file)
@@ -389,6 +389,7 @@ int add_interval(int n, int *switches, int start, int end) {
 int match(struct matcher *matcher, char *string, int *nb_switches, int *switches) {
   int n;
   char *where;
+  regmatch_t matches;
 
   if(nb_switches) { *nb_switches = 0; }
 
@@ -409,18 +410,19 @@ int match(struct matcher *matcher, char *string, int *nb_switches, int *switches
           *nb_switches = add_interval(*nb_switches, switches,
                                       (int) (where - string),
                                       (int) (where - string) + strlen(matcher->patterns[n]));
-          #warning CHECK THE INTERVALS
+#warning CHECK THE INTERVALS
           {
-            int k, i;
-            FILE *out = fopen("/tmp/intervals", "w");
-            for(k = 0; k < (*nb_switches)/2; k++) {
-              i = 0;
-              for(; i < switches[2 * k]; i++) fprintf(out, "-");
-              for(; i < switches[2 * k + 1]; i++) fprintf(out, "%c", string[i]);
-              for(; i < strlen(string); i++) fprintf(out, "-");
-              fprintf(out, "\n");
-            }
-            fclose(out);
+            /* int i; */
+            /* FILE *out = fopen("/tmp/intervals", "w"); */
+            /* for(k = 0; k < (*nb_switches)/2; k++) { */
+            /* i = 0; */
+            /* for(; i < switches[2 * k]; i++) fprintf(out, "-"); */
+            /* for(; i < switches[2 * k + 1]; i++) fprintf(out, "%c", string[i]); */
+            /* for(; i < strlen(string); i++) fprintf(out, "-"); */
+            /* fprintf(out, "\n"); */
+            /* } */
+            /* fclose(out); */
+            int k;
             for(k = 0; k < *nb_switches - 1; k++) {
               if(switches[k] > switches[k+1]) {
                 abort();
@@ -432,7 +434,18 @@ int match(struct matcher *matcher, char *string, int *nb_switches, int *switches
     }
     return 1;
   } else {
-    return regexec(&matcher->preg, string, 0, 0, 0) == 0;
+    if(switches) {
+      if(regexec(&matcher->preg, string, 1, &matches, 0) == 0) {
+        *nb_switches = 2;
+        switches[0] = matches.rm_so;
+        switches[1] = matches.rm_eo;
+        return 1;
+      } else {
+        return 0;
+      }
+    } else {
+      return regexec(&matcher->preg, string, 0, 0, 0) == 0;
+    }
   }
 }
 
@@ -630,10 +643,10 @@ 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) {
+  if(show_hits && matcher.nb_patterns >= 0) {
     switches = safe_malloc(sizeof(int) * matcher.nb_patterns * 2);
   } else {
-    switches = 0;
+    switches = safe_malloc(sizeof(int) * 2);
   }
 
   console_width = getmaxx(stdscr);
@@ -769,6 +782,7 @@ void update_screen(int *current_focus_line, int *displayed_focus_line,
               buffer[k++] = '\n';
               buffer[k++] = '\0';
             }
+
             print_string_with_switches(buffer, k, console_width,
                                        nb_switches / 2, switches);
           }
@@ -963,6 +977,7 @@ static struct option long_options[] = {
   { "regexp", no_argument, 0, 'e' },
   { "case-sensitive", no_argument, 0, 'a' },
   { "show-long-lines", no_argument, 0, 'j'},
+  { "show-hits", no_argument, 0, 'j'},
   { "upper-case-makes-case-sensitive", no_argument, 0, 'u' },
   { "title", 1, 0, 't' },
   { "number-of-lines", 1, 0, 'l' },