Cosmetics.
[selector.git] / selector.cc
index 7488bfd..8aeabca 100644 (file)
@@ -60,10 +60,6 @@ int case_sensitive = 0;
 char *title = 0;
 int error_flash = 0;
 
-#define COLOR_MODELINE 1
-#define COLOR_FOCUS_LINE 2
-#define COLOR_ERROR 3
-
 int attr_modeline, attr_focus_line, attr_error;
 
 //////////////////////////////////////////////////////////////////////
@@ -333,7 +329,20 @@ int next_visible(int current_line, int nb_lines, char **lines, matcher_t *matche
 
 //////////////////////////////////////////////////////////////////////
 
-void update_screen(int *current_focus_line, int *temporary_line, int motion,
+// The value passed to this routine in current_focus_line is the index
+// of the line to highlighted if it matches the matcher. The line
+// highlighted is the first the one matching the matcher in that
+// order: The current_focus_line itself, the first with a greated
+// index, the first with a lesser index.
+
+// The index of the line actually shown highlighted is written in
+// displayed_focus_line (it can be -1)
+
+// If there is a motion and a line is actually shown highlighted, its
+// value is written in current_focus_line
+
+void update_screen(int *current_focus_line, int *displayed_focus_line,
+                   int motion,
                    int nb_lines, char **lines,
                    int cursor_position,
                    char *pattern) {
@@ -343,8 +352,6 @@ void update_screen(int *current_focus_line, int *temporary_line, int motion,
 
   initialize_matcher(use_regexp, case_sensitive, &matcher, pattern);
 
-  // We now take care of printing the lines per se
-
   int console_width = getmaxx(stdscr);
   int console_height = getmaxy(stdscr);
 
@@ -394,7 +401,7 @@ void update_screen(int *current_focus_line, int *temporary_line, int motion,
       }
     }
 
-    // Here new_focus_line is either a line number matching the patterns, or -1
+    // Here new_focus_line is either a line number matching the pattern, or -1
 
     if(new_focus_line >= 0) {
 
@@ -466,12 +473,15 @@ void update_screen(int *current_focus_line, int *temporary_line, int motion,
         }
       }
 
+      // If we are on a focused line and we moved, this become the new
+      // focus line
+
       if(motion != 0) {
         *current_focus_line = new_focus_line;
       }
     }
 
-    *temporary_line = new_focus_line;
+    *displayed_focus_line = new_focus_line;
 
     if(nb_printed_lines == 0) {
       attron(attr_error);
@@ -901,20 +911,22 @@ int main(int argc, char **argv) {
       exit(1);
     }
 
-    init_pair(COLOR_MODELINE, color_fg_modeline, color_bg_modeline);
-    init_pair(COLOR_FOCUS_LINE, color_fg_highlight, color_bg_highlight);
-    init_pair(COLOR_ERROR, COLOR_WHITE, COLOR_RED);
+    init_pair(1, color_fg_modeline, color_bg_modeline);
+    attr_modeline = COLOR_PAIR(1);
+
+    init_pair(2, color_fg_highlight, color_bg_highlight);
+    attr_focus_line = COLOR_PAIR(2);
 
-    attr_modeline = COLOR_PAIR(COLOR_MODELINE);
-    attr_focus_line = COLOR_PAIR(COLOR_FOCUS_LINE);
-    attr_error = COLOR_PAIR(COLOR_ERROR);
+    init_pair(3, COLOR_WHITE, COLOR_RED);
+    attr_error = COLOR_PAIR(3);
 
   }
 
   int key;
-  int current_focus_line = 0, temporary_line = 0;
+  int current_focus_line = 0, displayed_focus_line = 0;
 
-  update_screen(&current_focus_line, &temporary_line, 0,
+  update_screen(&current_focus_line, &displayed_focus_line,
+                0,
                 nb_lines, labels, cursor_position, pattern);
 
   do {
@@ -1005,7 +1017,8 @@ int main(int argc, char **argv) {
       clear();
     }
 
-    update_screen(&current_focus_line, &temporary_line, motion,
+    update_screen(&current_focus_line, &displayed_focus_line,
+                  motion,
                   nb_lines, labels, cursor_position, pattern);
 
   } while(key != '\007' && // ^G
@@ -1022,8 +1035,8 @@ int main(int argc, char **argv) {
   if((key == KEY_ENTER || key == '\n')) {
 
     if(output_to_vt_buffer) {
-      if(temporary_line >= 0 && temporary_line < nb_lines) {
-        inject_into_tty_buffer(lines[temporary_line]);
+      if(displayed_focus_line >= 0 && displayed_focus_line < nb_lines) {
+        inject_into_tty_buffer(lines[displayed_focus_line]);
       }
     }
 
@@ -1033,8 +1046,8 @@ int main(int argc, char **argv) {
         cerr << "Can not open " << output_filename << " for writing." << endl;
         exit(1);
       } else {
-        if(temporary_line >= 0 && temporary_line < nb_lines) {
-          out << lines[temporary_line] << endl;
+        if(displayed_focus_line >= 0 && displayed_focus_line < nb_lines) {
+          out << lines[displayed_focus_line] << endl;
         } else {
           out << endl;
         }