Code

release v0.29
[ncmpc.git] / src / screen_find.c
index 3fdf9f30dd923914723c2e85658161e95a8a322f..b838421f4f94eadae78b382a7f086ec656f44925 100644 (file)
@@ -1,5 +1,5 @@
 /* ncmpc (Ncurses MPD Client)
- * (c) 2004-2010 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
@@ -21,6 +21,7 @@
 #include "screen_utils.h"
 #include "screen_status.h"
 #include "screen.h"
+#include "keyboard.h"
 #include "i18n.h"
 #include "options.h"
 #include "ncmpc.h"
@@ -35,15 +36,13 @@ screen_find(struct list_window *lw, command_t findcmd,
            list_window_callback_fn_t callback_fn,
            void *callback_data)
 {
-       int reversed = 0;
        bool found;
        const char *prompt = FIND_PROMPT;
-       char *value = options.find_show_last_pattern ? (char *) -1 : NULL;
 
-       if (findcmd == CMD_LIST_RFIND || findcmd == CMD_LIST_RFIND_NEXT) {
+       const bool reversed =
+               findcmd == CMD_LIST_RFIND || findcmd == CMD_LIST_RFIND_NEXT;
+       if (reversed)
                prompt = RFIND_PROMPT;
-               reversed = 1;
-       }
 
        switch (findcmd) {
        case CMD_LIST_FIND:
@@ -51,16 +50,19 @@ screen_find(struct list_window *lw, command_t findcmd,
                if (screen.findbuf) {
                        g_free(screen.findbuf);
                        screen.findbuf=NULL;
-               }
-               /* continue... */
+       }
+               /* fall through */
 
        case CMD_LIST_FIND_NEXT:
        case CMD_LIST_RFIND_NEXT:
-               if (!screen.findbuf)
+               if (!screen.findbuf) {
+                       char *value = options.find_show_last_pattern
+                               ? (char *) -1 : NULL;
                        screen.findbuf=screen_readln(prompt,
                                                     value,
                                                     &screen.find_history,
                                                     NULL);
+               }
 
                if (screen.findbuf == NULL)
                        return 1;
@@ -92,14 +94,11 @@ screen_find(struct list_window *lw, command_t findcmd,
  * which begins with this string while the users types */
 void
 screen_jump(struct list_window *lw,
-               list_window_callback_fn_t callback_fn,
-           list_window_paint_callback_t paint_callback,
-               void *callback_data)
+           list_window_callback_fn_t callback_fn, void *callback_data,
+           list_window_paint_callback_t paint_callback, void *paint_data)
 {
-       char *search_str, *iter, *temp;
        const int WRLN_MAX_LINE_SIZE = 1024;
        int key = 65;
-       command_t cmd;
 
        if (screen.findbuf) {
                g_free(screen.findbuf);
@@ -108,8 +107,8 @@ screen_jump(struct list_window *lw,
        screen.findbuf = g_malloc0(WRLN_MAX_LINE_SIZE);
        /* In screen.findbuf is the whole string which is displayed in the status_window
         * and search_str is the string the user entered (without the prompt) */
-       search_str = screen.findbuf + g_snprintf(screen.findbuf, WRLN_MAX_LINE_SIZE, "%s: ", JUMP_PROMPT);
-       iter = search_str;
+       char *search_str = screen.findbuf + g_snprintf(screen.findbuf, WRLN_MAX_LINE_SIZE, "%s: ", JUMP_PROMPT);
+       char *iter = search_str;
 
        while(1) {
                key = screen_getch(screen.findbuf);
@@ -135,18 +134,16 @@ screen_jump(struct list_window *lw,
 
                /* repaint the list_window */
                if (paint_callback != NULL)
-                       list_window_paint2(lw, paint_callback, callback_data);
+                       list_window_paint2(lw, paint_callback, paint_data);
                else
                        list_window_paint(lw, callback_fn, callback_data);
                wrefresh(lw->w);
        }
 
-       /* ncmpc should get the command */
-       ungetch(key);
-       if ((cmd=get_keyboard_command()) != CMD_NONE)
-               do_input_event(cmd);
-
-       temp = g_strdup(search_str);
+       char *temp = g_strdup(search_str);
        g_free(screen.findbuf);
        screen.findbuf = temp;
+
+       /* ncmpc should get the command */
+       keyboard_unread(key);
 }