Code

screen_find: strip prompt from search string
[ncmpc.git] / src / screen_find.c
index f7799614fdaeabaefafc474eb906008be48fe5b2..c17db4d3354652743af1f6f13234af1a7c62d707 100644 (file)
@@ -1,5 +1,5 @@
 /* ncmpc (Ncurses MPD Client)
- * (c) 2004-2009 The Music Player Daemon Project
+ * (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
@@ -31,9 +31,7 @@
 
 /* query user for a string and find it in a list window */
 int
-screen_find(list_window_t *lw,
-           int rows,
-           command_t findcmd,
+screen_find(struct list_window *lw, command_t findcmd,
            list_window_callback_fn_t callback_fn,
            void *callback_data)
 {
@@ -72,8 +70,7 @@ screen_find(list_window_t *lw,
                                            callback_fn, callback_data,
                                            screen.findbuf,
                                            options.find_wrap,
-                                           options.bell_on_wrap,
-                                           rows)
+                                           options.bell_on_wrap)
                        : list_window_find(lw,
                                           callback_fn, callback_data,
                                           screen.findbuf,
@@ -96,9 +93,10 @@ screen_find(list_window_t *lw,
 void
 screen_jump(struct list_window *lw,
                list_window_callback_fn_t callback_fn,
+           list_window_paint_callback_t paint_callback,
                void *callback_data)
 {
-       char *search_str, *iter;
+       char *search_str, *iter, *temp;
        const int WRLN_MAX_LINE_SIZE = 1024;
        int key = 65;
        command_t cmd;
@@ -113,28 +111,33 @@ screen_jump(struct list_window *lw,
        search_str = screen.findbuf + g_snprintf(screen.findbuf, WRLN_MAX_LINE_SIZE, "%s: ", JUMP_PROMPT);
        iter = search_str;
 
-       /* unfortunately wgetch returns "next/previous-page" not as an ascii-char */
-       while(!g_ascii_iscntrl(key) && key != KEY_NPAGE && key != KEY_PPAGE) {
+       while(1) {
                key = screen_getch(screen.findbuf);
-               /* if backspace or delete was pressed */
-               if (key == KEY_BACKSPACE || key == 330) {
+               /* if backspace or delete was pressed, process instead of ending loop */
+               if (key == 127 || key == 330) {
                        int i;
-                       /* don't end the loop */
-                       key = 65;
                        if (search_str <= g_utf8_find_prev_char(screen.findbuf, iter))
                                iter = g_utf8_find_prev_char(screen.findbuf, iter);
                        for (i = 0; *(iter + i) != '\0'; i++)
                                *(iter + i) = '\0';
                        continue;
                }
+               /* if a control key was pressed, end loop */
+               else if (g_ascii_iscntrl(key) || key == KEY_NPAGE || key == KEY_PPAGE) {
+                       break;
+               }
                else {
                        *iter = key;
                        if (iter < screen.findbuf + WRLN_MAX_LINE_SIZE - 3)
                                ++iter;
                }
                list_window_jump(lw, callback_fn, callback_data, search_str);
+
                /* repaint the list_window */
-               list_window_paint(lw, callback_fn, callback_data);
+               if (paint_callback != NULL)
+                       list_window_paint2(lw, paint_callback, callback_data);
+               else
+                       list_window_paint(lw, callback_fn, callback_data);
                wrefresh(lw->w);
        }
 
@@ -142,4 +145,8 @@ screen_jump(struct list_window *lw,
        ungetch(key);
        if ((cmd=get_keyboard_command()) != CMD_NONE)
                do_input_event(cmd);
+
+       temp = g_strdup(search_str);
+       g_free(screen.findbuf);
+       screen.findbuf = temp;
 }