Code

list_window: fully disable highlighting code on --enable-mini
[ncmpc.git] / src / list_window.c
index ba2542905b4cdac993f660726aae545a762a9328..10e06292ad605719ff677f75b9f76d9646689d7e 100644 (file)
@@ -111,11 +111,45 @@ list_window_check_selected(struct list_window *lw)
        }
 }
 
+/**
+ * Scroll after the cursor was moved, the list was changed or the
+ * window was resized.
+ */
+static void
+list_window_check_origin(struct list_window *lw)
+{
+       int start = lw->start;
+
+       if ((unsigned) options.scroll_offset * 2 >= lw->rows)
+               // Center if the offset is more than half the screen
+               start = lw->selected - lw->rows / 2;
+       else
+               {
+                       if (lw->selected < lw->start + options.scroll_offset)
+                               start = lw->selected - options.scroll_offset;
+
+                       if (lw->selected >= lw->start + lw->rows - options.scroll_offset)
+                               {
+                                       start = lw->selected - lw->rows + 1 + options.scroll_offset;
+                               }
+               }
+
+       if (start + lw->rows > lw->length)
+               start = lw->length - lw->rows;
+
+       if (start < 0 || lw->length == 0)
+               start = 0;
+
+       lw->start = start;
+}
+
 void
 list_window_resize(struct list_window *lw, unsigned width, unsigned height)
 {
        lw->cols = width;
        lw->rows = height;
+
+       list_window_check_origin(lw);
 }
 
 void
@@ -124,6 +158,7 @@ list_window_set_length(struct list_window *lw, unsigned length)
        lw->length = length;
 
        list_window_check_selected(lw);
+       list_window_check_origin(lw);
 }
 
 void
@@ -151,6 +186,7 @@ list_window_set_cursor(struct list_window *lw, unsigned i)
        lw->selected_end = i;
 
        list_window_check_selected(lw);
+       list_window_check_origin(lw);
 }
 
 void
@@ -175,6 +211,9 @@ list_window_move_cursor(struct list_window *lw, unsigned n)
                lw->selected_start = n;
                lw->selected_end = n;
        }
+
+       list_window_check_selected(lw);
+       list_window_check_origin(lw);
 }
 
 void
@@ -339,6 +378,7 @@ list_window_paint_row(WINDOW *w, unsigned y, unsigned width,
 
 #ifdef NCMPC_MINI
        second_column = NULL;
+       highlight = false;
 #endif /* NCMPC_MINI */
 
        if (second_column != NULL) {
@@ -384,46 +424,20 @@ list_window_paint_row(WINDOW *w, unsigned y, unsigned width,
 }
 
 void
-list_window_paint(struct list_window *lw,
+list_window_paint(const struct list_window *lw,
                  list_window_callback_fn_t callback,
                  void *callback_data)
 {
        unsigned i;
        bool show_cursor = !lw->hide_cursor;
-       bool highlight = false;
-
-       if (show_cursor) {
-               int start = lw->start;
-               if ((unsigned) options.scroll_offset * 2 >= lw->rows)
-                       // Center if the offset is more than half the screen
-                       start = lw->selected - lw->rows / 2;
-               else
-               {
-                       if (lw->selected < lw->start + options.scroll_offset)
-                               start = lw->selected - options.scroll_offset;
-
-                       if (lw->selected >= lw->start + lw->rows - options.scroll_offset)
-                       {
-                               start = lw->selected - lw->rows + 1 + options.scroll_offset;
-                       }
-               }
-
-               if (start + lw->rows > lw->length)
-                       start = lw->length - lw->rows;
-
-               if (start < 0 || lw->length == 0)
-                       start = 0;
-
-               lw->start = start;
-       }
 
        show_cursor = show_cursor &&
                (!options.hardware_cursor || lw->range_selection);
 
        for (i = 0; i < lw->rows; i++) {
                const char *label;
+               bool highlight = false;
                char *second_column = NULL;
-               highlight = false;
 
                wmove(lw->w, i, 0);
 
@@ -435,6 +449,11 @@ list_window_paint(struct list_window *lw,
                label = callback(lw->start + i, &highlight, &second_column, callback_data);
                assert(label != NULL);
 
+#ifdef NCMPC_MINI
+               highlight = false;
+               second_column = NULL;
+#endif /* NCMPC_MINI */
+
                list_window_paint_row(lw->w, i, lw->cols,
                                      show_cursor &&
                                      lw->start + i >= lw->selected_start &&
@@ -465,12 +484,14 @@ list_window_find(struct list_window *lw,
        unsigned i = lw->selected + 1;
        const char *label;
 
+       assert(str != NULL);
+
        do {
                while (i < lw->length) {
                        label = callback(i, &h, NULL, callback_data);
                        assert(label != NULL);
 
-                       if (str && label && match_line(label, str)) {
+                       if (match_line(label, str)) {
                                list_window_move_cursor(lw, i);
                                return true;
                        }
@@ -503,6 +524,8 @@ list_window_rfind(struct list_window *lw,
        int i = lw->selected - 1;
        const char *label;
 
+       assert(str != NULL);
+
        if (lw->length == 0)
                return false;
 
@@ -511,7 +534,7 @@ list_window_rfind(struct list_window *lw,
                        label = callback(i, &h, NULL, callback_data);
                        assert(label != NULL);
 
-                       if( str && label && match_line(label, str) ) {
+                       if (match_line(label, str)) {
                                list_window_move_cursor(lw, i);
                                return true;
                        }
@@ -530,6 +553,23 @@ list_window_rfind(struct list_window *lw,
        return false;
 }
 
+static bool
+jump_match(const char *haystack, const char *needle)
+{
+#ifdef NCMPC_MINI
+       bool jump_prefix_only = true;
+#else
+       bool jump_prefix_only = options.jump_prefix_only;
+#endif
+
+       assert(haystack != NULL);
+       assert(needle != NULL);
+
+       return jump_prefix_only
+               ? g_ascii_strncasecmp(haystack, needle, strlen(needle)) == 0
+               : match_line(haystack, needle);
+}
+
 bool
 list_window_jump(struct list_window *lw,
                 list_window_callback_fn_t callback,
@@ -539,20 +579,16 @@ list_window_jump(struct list_window *lw,
        bool h;
        const char *label;
 
+       assert(str != NULL);
+
        for (unsigned i = 0; i < lw->length; ++i) {
                label = callback(i, &h, NULL, callback_data);
                assert(label != NULL);
 
                if (label[0] == '[')
                        label++;
-#ifndef NCMPC_MINI
-               if (str && label &&
-                               ((options.jump_prefix_only && g_ascii_strncasecmp(label, str, strlen(str)) == 0) ||
-                                (!options.jump_prefix_only && match_line(label, str))) )
-#else
-               if (str && label && g_ascii_strncasecmp(label, str, strlen(str)) == 0)
-#endif
-               {
+
+               if (jump_match(label, str)) {
                        list_window_move_cursor(lw, i);
                        return true;
                }