Code

release v0.29
[ncmpc.git] / src / list_window.c
index d1a6ce3ff2ca9f4d18b9046921317dbeb641037c..3fbe531571584b13164a99cbbf55235732c50557 100644 (file)
@@ -1,21 +1,21 @@
 /* ncmpc (Ncurses MPD Client)
- * (c) 2004-2009 The Music Player Daemon Project
+ * (c) 2004-2017 The Music Player Daemon Project
  * Project homepage: http://musicpd.org
-
+ *
  * This program is free software; you can redistribute it and/or modify
  * it under the terms of the GNU General Public License as published by
  * the Free Software Foundation; either version 2 of the License, or
  * (at your option) any later version.
-
+ *
  * This program is distributed in the hope that it will be useful,
  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  * GNU General Public License for more details.
-
+ *
  * You should have received a copy of the GNU General Public License along
  * with this program; if not, write to the Free Software Foundation, Inc.,
  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
-*/
+ */
 
 #include "list_window.h"
 #include "config.h"
@@ -25,7 +25,7 @@
 #include "command.h"
 #include "colors.h"
 #include "paint.h"
-#include "screen_message.h"
+#include "screen_status.h"
 #include "i18n.h"
 
 #include <assert.h>
@@ -93,26 +93,7 @@ list_window_check_selected(struct list_window *lw)
 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;
+       list_window_scroll_to(lw, lw->selected);
 }
 
 void
@@ -127,6 +108,9 @@ list_window_resize(struct list_window *lw, unsigned width, unsigned height)
 void
 list_window_set_length(struct list_window *lw, unsigned length)
 {
+       if (length == lw->length)
+               return;
+
        lw->length = length;
 
        list_window_check_selected(lw);
@@ -149,6 +133,31 @@ list_window_center(struct list_window *lw, unsigned n)
        }
 }
 
+void
+list_window_scroll_to(struct list_window *lw, unsigned n)
+{
+       int start = lw->start;
+
+       if ((unsigned) options.scroll_offset * 2 >= lw->rows)
+               // Center if the offset is more than half the screen
+               start = n - lw->rows / 2;
+       else {
+               if (n < lw->start + options.scroll_offset)
+                       start = n - options.scroll_offset;
+
+               if (n >= lw->start + lw->rows - options.scroll_offset)
+                       start = n - 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_set_cursor(struct list_window *lw, unsigned i)
 {
@@ -312,7 +321,7 @@ list_window_scroll_down(struct list_window *lw, unsigned n)
 {
        if (lw->start + lw->rows < lw->length)
        {
-               if ( lw->start + lw->rows + n > lw->length - 1)
+               if (lw->start + lw->rows + n > lw->length - 1)
                        lw->start = lw->length - lw->rows;
                else
                        lw->start += n;
@@ -322,38 +331,11 @@ list_window_scroll_down(struct list_window *lw, unsigned n)
 }
 
 static void
-list_window_paint_row(WINDOW *w, unsigned y, unsigned width,
-                     bool selected, bool highlight,
-                     const char *text, const char *second_column)
+list_window_paint_row(WINDOW *w, unsigned width, bool selected,
+                     const char *text)
 {
-       unsigned second_column_width;
-
-#ifdef NCMPC_MINI
-       second_column = NULL;
-       highlight = false;
-#endif /* NCMPC_MINI */
-
-       if (second_column != NULL) {
-               second_column_width = utf8_width(second_column) + 1;
-               if (second_column_width < width)
-                       width -= second_column_width;
-               else
-                       second_column_width = 0;
-       } else
-               second_column_width = 0;
-
-       row_color(w, highlight ? COLOR_LIST_BOLD : COLOR_LIST, selected);
-
-       waddstr(w, text);
-
-       /* erase the unused space after the text */
-       row_clear_to_eol(w, width, selected);
-
-       if (second_column_width > 0) {
-               wmove(w, y, width);
-               waddch(w, ' ');
-               waddstr(w, second_column);
-       }
+       row_paint_text(w, width, COLOR_LIST,
+                      selected, text);
 }
 
 void
@@ -369,10 +351,6 @@ list_window_paint(const struct list_window *lw,
                list_window_get_range(lw, &range);
 
        for (unsigned i = 0; i < lw->rows; i++) {
-               const char *label;
-               bool highlight = false;
-               char *second_column = NULL;
-
                wmove(lw->w, i, 0);
 
                if (lw->start + i >= lw->length) {
@@ -380,23 +358,14 @@ list_window_paint(const struct list_window *lw,
                        break;
                }
 
-               label = callback(lw->start + i, &highlight, &second_column, callback_data);
+               const char *label = callback(lw->start + i, 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,
+               list_window_paint_row(lw->w, lw->cols,
                                      show_cursor &&
                                      lw->start + i >= range.start &&
                                      lw->start + i < range.end,
-                                     highlight,
-                                     label, second_column);
-
-               if (second_column != NULL)
-                       g_free(second_column);
+                                     label);
        }
 
        row_color_end(lw->w);
@@ -411,7 +380,7 @@ list_window_paint(const struct list_window *lw,
 void
 list_window_paint2(const struct list_window *lw,
                   list_window_paint_callback_t paint_callback,
-                  void *callback_data)
+                  const void *callback_data)
 {
        bool show_cursor = !lw->hide_cursor &&
                (!options.hardware_cursor || lw->range_selection);
@@ -421,8 +390,6 @@ list_window_paint2(const struct list_window *lw,
                list_window_get_range(lw, &range);
 
        for (unsigned i = 0; i < lw->rows; i++) {
-               bool selected;
-
                wmove(lw->w, i, 0);
 
                if (lw->start + i >= lw->length) {
@@ -430,15 +397,12 @@ list_window_paint2(const struct list_window *lw,
                        break;
                }
 
-               selected = show_cursor &&
+               bool selected = show_cursor &&
                        lw->start + i >= range.start &&
                        lw->start + i < range.end;
 
                paint_callback(lw->w, lw->start + i, i, lw->cols,
                               selected, callback_data);
-
-               if (selected)
-                       wattroff(lw->w, A_REVERSE);
        }
 
        if (options.hardware_cursor && lw->selected >= lw->start &&
@@ -456,15 +420,13 @@ list_window_find(struct list_window *lw,
                 bool wrap,
                 bool bell_on_wrap)
 {
-       bool h;
        unsigned i = lw->selected + 1;
-       const char *label;
 
        assert(str != NULL);
 
        do {
                while (i < lw->length) {
-                       label = callback(i, &h, NULL, callback_data);
+                       const char *label = callback(i, callback_data);
                        assert(label != NULL);
 
                        if (match_line(label, str)) {
@@ -496,9 +458,7 @@ list_window_rfind(struct list_window *lw,
                  bool wrap,
                  bool bell_on_wrap)
 {
-       bool h;
        int i = lw->selected - 1;
-       const char *label;
 
        assert(str != NULL);
 
@@ -507,7 +467,7 @@ list_window_rfind(struct list_window *lw,
 
        do {
                while (i >= 0) {
-                       label = callback(i, &h, NULL, callback_data);
+                       const char *label = callback(i, callback_data);
                        assert(label != NULL);
 
                        if (match_line(label, str)) {
@@ -529,45 +489,53 @@ 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
+bool
+list_window_jump(struct list_window *lw,
+                list_window_callback_fn_t callback,
+                void *callback_data,
+                const char *str)
+{
+       assert(str != NULL);
 
-       assert(haystack != NULL);
-       assert(needle != NULL);
+       for (unsigned i = 0; i < lw->length; i++) {
+               const char *label = callback(i, callback_data);
+               assert(label != NULL);
 
-       return jump_prefix_only
-               ? g_ascii_strncasecmp(haystack, needle, strlen(needle)) == 0
-               : match_line(haystack, needle);
+               if (g_ascii_strncasecmp(label, str, strlen(str)) == 0) {
+                       list_window_move_cursor(lw, i);
+                       return true;
+               }
+       }
+       return false;
 }
-
+#else
 bool
 list_window_jump(struct list_window *lw,
                 list_window_callback_fn_t callback,
                 void *callback_data,
                 const char *str)
 {
-       bool h;
-       const char *label;
-
        assert(str != NULL);
 
-       for (unsigned i = 0; i < lw->length; ++i) {
-               label = callback(i, &h, NULL, callback_data);
+       GRegex *regex = compile_regex(str, options.jump_prefix_only);
+       if (regex == NULL)
+               return false;
+
+       for (unsigned i = 0; i < lw->length; i++) {
+               const char *label = callback(i, callback_data);
                assert(label != NULL);
 
-               if (jump_match(label, str)) {
+               if (match_regex(regex, label)) {
+                       g_regex_unref(regex);
                        list_window_move_cursor(lw, i);
                        return true;
                }
        }
+       g_regex_unref(regex);
        return false;
 }
+#endif
 
 /* perform basic list window commands (movement) */
 bool
@@ -661,7 +629,7 @@ list_window_scroll_cmd(struct list_window *lw, command_t cmd)
                break;
 
        case CMD_LIST_NEXT_PAGE:
-               lw->start += lw->rows - 1;
+               lw->start += lw->rows;
                if (lw->start + lw->rows > lw->length) {
                        if (lw->length > lw->rows)
                                lw->start = lw->length - lw->rows;