Code

Merge remote branches 'jn/cosmetics', 'jn/doxygen' and 'jn/renames'
[ncmpc.git] / src / list_window.c
index d44fd29329b427965abedef04a113f3445187015..ad2ae77cc1f588b7e691d17b2079756f148dda62 100644 (file)
@@ -1,21 +1,21 @@
 /* ncmpc (Ncurses MPD Client)
  * (c) 2004-2010 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
@@ -152,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)
 {
@@ -315,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;
@@ -489,44 +495,60 @@ 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)
+{
+       unsigned i;
+       const char *label;
 
-       assert(haystack != NULL);
-       assert(needle != NULL);
+       assert(str != NULL);
 
-       return jump_prefix_only
-               ? g_ascii_strncasecmp(haystack, needle, strlen(needle)) == 0
-               : match_line(haystack, needle);
-}
+       for (i = 0; i < lw->length; i++) {
+               label = callback(i, callback_data);
+               assert(label != NULL);
 
+               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)
 {
+       unsigned i;
        const char *label;
+       GRegex *regex;
 
        assert(str != NULL);
 
-       for (unsigned i = 0; i < lw->length; ++i) {
+       regex = compile_regex(str, options.jump_prefix_only);
+       if (regex == NULL)
+               return false;
+
+       for (i = 0; i < lw->length; i++) {
                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
@@ -620,7 +642,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;