Code

Merge remote branches 'jn/cosmetics', 'jn/doxygen' and 'jn/renames'
[ncmpc.git] / src / screen_search.c
index e199bfb2b476ecd1f647f2256bf44ade991f88c3..5e9226e6cff234246638f1121bf1c15447ae2860 100644 (file)
@@ -1,7 +1,6 @@
-/* 
- * $Id$
- *
- * (c) 2004 by Kalle Wallin <kaw@linux.se>
+/* 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
  * 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., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
  *
+ * 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 "config.h"
-
-#ifndef DISABLE_SEARCH_SCREEN
-#include "ncmpc.h"
+#include "screen_search.h"
+#include "screen_interface.h"
+#include "screen_status.h"
+#include "screen.h"
+#include "i18n.h"
 #include "options.h"
-#include "support.h"
+#include "charset.h"
 #include "mpdclient.h"
 #include "strfsong.h"
-#include "command.h"
-#include "screen.h"
 #include "utils.h"
 #include "screen_utils.h"
-#include "screen_browse.h"
-#include "gcc.h"
+#include "screen_browser.h"
+#include "filelist.h"
 
-#include <ctype.h>
-#include <stdlib.h>
 #include <string.h>
 #include <glib.h>
-#include <ncurses.h>
 
-/* new search stuff with qball's libmpdclient */
-#define FUTURE
-
-
-#ifdef FUTURE
-
-extern gint mpdclient_finish_command(mpdclient_t *c);
+enum {
+       SEARCH_URI = MPD_TAG_COUNT + 100,
+       SEARCH_ARTIST_TITLE
+};
 
-typedef struct {
-       int id;
+static const struct {
        const char *name;
        const char *localname;
-} search_tag_t;
-
-static search_tag_t search_tag[] = {
-  { MPD_TAG_ITEM_ARTIST,   "artist",    N_("artist") },
-  { MPD_TAG_ITEM_ALBUM,    "album",     N_("album") },
-  { MPD_TAG_ITEM_TITLE,    "title",     N_("title") },
-  { MPD_TAG_ITEM_TRACK,    "track",     N_("track") },
-  { MPD_TAG_ITEM_NAME,     "name",      N_("name") },
-  { MPD_TAG_ITEM_GENRE,    "genre",     N_("genre") },
-  { MPD_TAG_ITEM_DATE,     "date",      N_("date") },
-  { MPD_TAG_ITEM_COMPOSER, "composer",  N_("composer") },
-  { MPD_TAG_ITEM_PERFORMER,"performer", N_("performer") },
-  { MPD_TAG_ITEM_COMMENT,  "comment",   N_("comment") },
-  { MPD_TAG_ITEM_FILENAME, "filename",  N_("file") },
-  { -1,                    NULL,        NULL }
+} search_tag[MPD_TAG_COUNT] = {
+       [MPD_TAG_ARTIST] = { "artist", N_("artist") },
+       [MPD_TAG_ALBUM] = { "album", N_("album") },
+       [MPD_TAG_TITLE] = { "title", N_("title") },
+       [MPD_TAG_TRACK] = { "track", N_("track") },
+       [MPD_TAG_NAME] = { "name", N_("name") },
+       [MPD_TAG_GENRE] = { "genre", N_("genre") },
+       [MPD_TAG_DATE] = { "date", N_("date") },
+       [MPD_TAG_COMPOSER] = { "composer", N_("composer") },
+       [MPD_TAG_PERFORMER] = { "performer", N_("performer") },
+       [MPD_TAG_COMMENT] = { "comment", N_("comment") },
 };
 
 static int
-search_get_tag_id(char *name)
+search_get_tag_id(const char *name)
 {
-  int i;
-
-  i=0;
-  while( search_tag[i].name )
-    {
-      if( strcasecmp(search_tag[i].name, name)==0 || 
-         strcasecmp(search_tag[i].localname, name)==0 )
-       return search_tag[i].id;
-      i++;
-    }
-  return -1;
-}
+       unsigned i;
 
-#endif
+       if (g_ascii_strcasecmp(name, "file") == 0 ||
+           strcasecmp(name, _("file")) == 0)
+               return SEARCH_URI;
 
+       for (i = 0; i < MPD_TAG_COUNT; ++i)
+               if (search_tag[i].name != NULL &&
+                   (strcasecmp(search_tag[i].name, name) == 0 ||
+                    strcasecmp(search_tag[i].localname, name) == 0))
+                       return i;
 
-#define SEARCH_TITLE    0
-#define SEARCH_ARTIST   1
-#define SEARCH_ALBUM    2
-#define SEARCH_FILE     3
-
-#define SEARCH_ARTIST_TITLE 999
+       return -1;
+}
 
 typedef struct {
-       int table;
+       enum mpd_tag_type table;
        const char *label;
 } search_type_t;
 
 static search_type_t mode[] = {
-  { MPD_TABLE_TITLE,     N_("Title") },
-  { MPD_TABLE_ARTIST,    N_("Artist") },
-  { MPD_TABLE_ALBUM,     N_("Album") },
-  { MPD_TABLE_FILENAME,  N_("Filename") },
-  { SEARCH_ARTIST_TITLE, N_("Artist + Title") },
-  { 0, NULL }
+       { MPD_TAG_TITLE, N_("Title") },
+       { MPD_TAG_ARTIST, N_("Artist") },
+       { MPD_TAG_ALBUM, N_("Album") },
+       { SEARCH_URI, N_("Filename") },
+       { SEARCH_ARTIST_TITLE, N_("Artist + Title") },
+       { 0, NULL }
 };
 
-static list_window_t *lw = NULL;
-static mpdclient_filelist_t *filelist = NULL;
 static GList *search_history = NULL;
 static gchar *pattern = NULL;
 static gboolean advanced_search_mode = FALSE;
 
+static struct screen_browser browser;
+
+static const char *const help_text[] = {
+       "Quick     -  Enter a string and ncmpc will search according",
+       "             to the current search mode (displayed above).",
+       "",
+       "Advanced  -  <tag>:<search term> [<tag>:<search term>...]",
+       "               Example: artist:radiohead album:pablo honey",
+       "",
+       "               Available tags: artist, album, title, track,",
+       "               name, genre, date composer, performer, comment, file",
+       "",
+};
 
 /* search info */
 static const char *
-lw_search_help_callback(unsigned idx, mpd_unused int *highlight,
-                       mpd_unused void *data)
+lw_search_help_callback(unsigned idx, G_GNUC_UNUSED void *data)
 {
-       unsigned text_rows;
-       static const char *text[] = {
-               "Quick  - just enter a string and ncmpc will search according",
-               "               to the current search mode (displayed above).",
-               "",
-               "Advanced  -  <tag>:<search term> [<tag>:<search term>...]",
-               "                       Example: artist:radiohead album:pablo honey",
-               "",
-               "                  avalible tags: artist, album, title, track,",
-               "                  name, genre, date composer, performer, comment, file",
-               "",
-               NULL
-       };
-
-       text_rows=0;
-       while (text[text_rows])
-               text_rows++;
-
-       if (idx < text_rows)
-               return text[idx];
-       return NULL;
+       assert(idx < G_N_ELEMENTS(help_text));
+
+       return help_text[idx];
 }
 
-/* the playlist have been updated -> fix highlights */
-static void 
-playlist_changed_callback(mpdclient_t *c, int event, mpd_unused gpointer data)
+static void
+screen_search_paint(void);
+
+static void
+search_repaint(void)
 {
-  if( filelist==NULL )
-    return;
-  D("screen_search.c> playlist_callback() [%d]\n", event);
-  switch(event)
-    {
-    case PLAYLIST_EVENT_CLEAR:
-      clear_highlights(filelist);
-      break;
-    default:
-      sync_highlights(c, filelist);
-      break;
-    }
+       screen_search_paint();
+       wrefresh(browser.lw->w);
 }
 
 /* sanity check search mode value */
 static void
 search_check_mode(void)
 {
-  int max = 0;
-
-  while( mode[max].label != NULL )
-    max++;
-  if( options.search_mode<0 )
-    options.search_mode = 0;
-  else if( options.search_mode>=max )
-    options.search_mode = max-1;
+       int max = 0;
+
+       while (mode[max].label != NULL)
+               max++;
+       if (options.search_mode < 0)
+               options.search_mode = 0;
+       else if (options.search_mode >= max)
+               options.search_mode = max-1;
 }
 
 static void
-search_clear(mpd_unused screen_t *screen, mpdclient_t *c,
-            gboolean clear_pattern)
+search_clear(bool clear_pattern)
 {
-       if (filelist) {
-               mpdclient_remove_playlist_callback(c, playlist_changed_callback);
-               filelist = mpdclient_filelist_free(filelist);
+       if (browser.filelist) {
+               filelist_free(browser.filelist);
+               browser.filelist = filelist_new();
+               list_window_set_length(browser.lw, 0);
        }
        if (clear_pattern && pattern) {
                g_free(pattern);
@@ -189,31 +152,48 @@ search_clear(mpd_unused screen_t *screen, mpdclient_t *c,
        }
 }
 
-#ifdef FUTURE
-static mpdclient_filelist_t *
-filelist_search(mpdclient_t *c, mpd_unused int exact_match, int table,
-               gchar *local_pattern)
+static struct filelist *
+search_simple_query(struct mpd_connection *connection, bool exact_match,
+                   int table, gchar *local_pattern)
 {
-  mpdclient_filelist_t *list, *list2;
-
-  if( table == SEARCH_ARTIST_TITLE )
-    {
-      list = mpdclient_filelist_search(c, FALSE, MPD_TABLE_ARTIST,
-                                      local_pattern);
-      list2 = mpdclient_filelist_search(c, FALSE, MPD_TABLE_TITLE,
-                                       local_pattern);
-
-      list->length += list2->length;
-      list->list = g_list_concat(list->list, list2->list);
-      list->list = g_list_sort(list->list, compare_filelistentry_format);
-      list->updated = TRUE;
-    }
-  else
-    {
-      list = mpdclient_filelist_search(c, FALSE, table, local_pattern);
-    }
-
-  return list;
+       struct filelist *list;
+       gchar *filter_utf8 = locale_to_utf8(local_pattern);
+
+       if (table == SEARCH_ARTIST_TITLE) {
+               mpd_command_list_begin(connection, false);
+
+               mpd_search_db_songs(connection, exact_match);
+               mpd_search_add_tag_constraint(connection, MPD_OPERATOR_DEFAULT,
+                                             MPD_TAG_ARTIST, filter_utf8);
+               mpd_search_commit(connection);
+
+               mpd_search_db_songs(connection, exact_match);
+               mpd_search_add_tag_constraint(connection, MPD_OPERATOR_DEFAULT,
+                                             MPD_TAG_TITLE, filter_utf8);
+               mpd_search_commit(connection);
+
+               mpd_command_list_end(connection);
+
+               list = filelist_new_recv(connection);
+               filelist_no_duplicates(list);
+       } else if (table == SEARCH_URI) {
+               mpd_search_db_songs(connection, exact_match);
+               mpd_search_add_uri_constraint(connection, MPD_OPERATOR_DEFAULT,
+                                             filter_utf8);
+               mpd_search_commit(connection);
+
+               list = filelist_new_recv(connection);
+       } else {
+               mpd_search_db_songs(connection, exact_match);
+               mpd_search_add_tag_constraint(connection, MPD_OPERATOR_DEFAULT,
+                                             table, filter_utf8);
+               mpd_search_commit(connection);
+
+               list = filelist_new_recv(connection);
+       }
+
+       g_free(filter_utf8);
+       return list;
 }
 
 /*-----------------------------------------------------------------------
@@ -221,27 +201,21 @@ filelist_search(mpdclient_t *c, mpd_unused int exact_match, int table,
  *       Its ugly and MUST be redesigned before the next release!
  *-----------------------------------------------------------------------
  */
-static mpdclient_filelist_t *
-search_advanced_query(char *query, mpdclient_t *c)
+static struct filelist *
+search_advanced_query(struct mpd_connection *connection, char *query)
 {
        int i,j;
        char **strv;
        int table[10];
        char *arg[10];
-       mpdclient_filelist_t *fl = NULL;
+       struct filelist *fl = NULL;
 
        advanced_search_mode = FALSE;
-       if( g_strrstr(query, ":") == NULL )
+       if (strchr(query, ':') == NULL)
                return NULL;
 
        strv = g_strsplit_set(query, ": ", 0);
 
-       i=0;
-       while (strv[i]) {
-               D("strv[%d] = \"%s\"\n", i, strv[i]);
-               i++;
-       }
-
        memset(table, 0, 10*sizeof(int));
        memset(arg, 0, 10*sizeof(char *));
 
@@ -255,12 +229,10 @@ search_advanced_query(char *query, mpdclient_t *c)
                                arg[j] = g_strdup_printf("%s %s", arg[j], strv[i]);
                                g_free(tmp);
                        } else {
-                               D("Bad search tag %s\n", strv[i]);
                                screen_status_printf(_("Bad search tag %s"), strv[i]);
                        }
                        i++;
                } else if (strv[i+1] == NULL || strlen(strv[i+1]) == 0) {
-                       D("No argument for search tag %s\n", strv[i]);
                        screen_status_printf(_("No argument for search tag %s"), strv[i]);
                        i++;
                        //        j--;
@@ -278,103 +250,124 @@ search_advanced_query(char *query, mpdclient_t *c)
 
        g_strfreev(strv);
 
+       if (!advanced_search_mode || j == 0) {
+               for (i = 0; arg[i] != NULL; ++i)
+                       g_free(arg[i]);
+               return NULL;
+       }
 
-       if (advanced_search_mode && j > 0) {
-               int iter;
-               mpd_InfoEntity *entity;
+       /*-----------------------------------------------------------------------
+        * NOTE (again): This code exists to test a new search ui,
+        *               Its ugly and MUST be redesigned before the next release!
+        *             + the code below should live in mpdclient.c
+        *-----------------------------------------------------------------------
+        */
+       /** stupid - but this is just a test...... (fulhack)  */
+       mpd_search_db_songs(connection, false);
+
+       for (i = 0; i < 10 && arg[i] != NULL; i++) {
+               if (table[i] == SEARCH_URI)
+                       mpd_search_add_uri_constraint(connection,
+                                                     MPD_OPERATOR_DEFAULT,
+                                                     arg[i]);
+               else
+                       mpd_search_add_tag_constraint(connection,
+                                                     MPD_OPERATOR_DEFAULT,
+                                                     table[i], arg[i]);
+       }
 
-               /*-----------------------------------------------------------------------
-                * NOTE (again): This code exists to test a new search ui,
-                *               Its ugly and MUST be redesigned before the next release!
-                *             + the code below should live in mpdclient.c
-                *-----------------------------------------------------------------------
-                */
-               /** stupid - but this is just a test...... (fulhack)  */
-               mpd_startSearch(c->connection, FALSE);
+       mpd_search_commit(connection);
+       fl = filelist_new_recv(connection);
+       if (!mpd_response_finish(connection)) {
+               filelist_free(fl);
+               fl = NULL;
+       }
 
-               for(iter = 0; iter < 10; iter++) {
-                       mpd_addConstraintSearch(c->connection, table[iter], arg[iter]);
-               }
+       for (i = 0; arg[i] != NULL; ++i)
+               g_free(arg[i]);
+
+       return fl;
+}
 
-               mpd_commitSearch(c->connection);
+static struct filelist *
+do_search(struct mpdclient *c, char *query)
+{
+       struct mpd_connection *connection = mpdclient_get_connection(c);
+       struct filelist *fl;
 
-               fl = g_malloc0(sizeof(mpdclient_filelist_t));
+       fl = search_advanced_query(connection, query);
+       if (fl != NULL)
+               return fl;
 
-               while ((entity=mpd_getNextInfoEntity(c->connection)))  {
-                       filelist_entry_t *entry = g_malloc0(sizeof(filelist_entry_t));
+       if (mpd_connection_get_error(connection) != MPD_ERROR_SUCCESS) {
+               mpdclient_handle_error(c);
+               return NULL;
+       }
 
-                       entry->entity = entity;
-                       fl->list = g_list_append(fl->list, (gpointer) entry);
-                       fl->length++;
-               }
+       fl = search_simple_query(connection, FALSE,
+                                mode[options.search_mode].table,
+                                query);
+       if (fl == NULL)
+               mpdclient_handle_error(c);
+       return fl;
+}
 
-               if (mpdclient_finish_command(c) && fl)
-                       fl = mpdclient_filelist_free(fl);
+static void
+screen_search_reload(struct mpdclient *c)
+{
+       if (pattern == NULL)
+               return;
 
-               fl->updated = TRUE;
+       if (browser.filelist != NULL) {
+               filelist_free(browser.filelist);
+               browser.filelist = NULL;
        }
 
-       i=0;
-       while( arg[i] )
-               g_free(arg[i++]);
+       browser.filelist = do_search(c, pattern);
+       if (browser.filelist == NULL)
+               browser.filelist = filelist_new();
+       list_window_set_length(browser.lw, filelist_length(browser.filelist));
 
-       return fl;
+       screen_browser_sync_highlights(browser.filelist, &c->playlist);
 }
-#else
-#define search_advanced_query(pattern,c) (NULL)
-#endif
 
 static void
-search_new(screen_t *screen, mpdclient_t *c)
+search_new(struct mpdclient *c)
 {
-  search_clear(screen, c, TRUE);
-  
-  pattern = screen_readln(screen->status_window.w, 
-                         _("Search: "),
-                         NULL,
-                         &search_history,
-                         NULL);
-
-  if( pattern && strcmp(pattern,"")==0 )
-    {
-      g_free(pattern);
-      pattern=NULL;
-    }
-  
-  if( pattern==NULL )
-    {
-      list_window_reset(lw);
-      return;
-    }
-
-  if( !MPD_VERSION_LT(c, 0, 12, 0) )
-    filelist = search_advanced_query(pattern, c);
-  if( !advanced_search_mode && filelist==NULL )
-    filelist = filelist_search(c, 
-                              FALSE,
-                              mode[options.search_mode].table,
-                              pattern);
-  sync_highlights(c, filelist);
-  mpdclient_install_playlist_callback(c, playlist_changed_callback);
-  list_window_check_selected(lw, filelist->length);
-}
+       if (!mpdclient_is_connected(c))
+               return;
 
+       search_clear(true);
 
+       g_free(pattern);
+       pattern = screen_readln(_("Search"),
+                               NULL,
+                               &search_history,
+                               NULL);
+
+       if (pattern == NULL) {
+               list_window_reset(browser.lw);
+               return;
+       }
+
+       screen_search_reload(c);
+}
 
 static void
-init(WINDOW *w, int cols, int rows)
+screen_search_init(WINDOW *w, int cols, int rows)
 {
-       lw = list_window_init(w, cols, rows);
+       browser.lw = list_window_init(w, cols, rows);
+       list_window_set_length(browser.lw, G_N_ELEMENTS(help_text));
 }
 
 static void
-quit(void)
+screen_search_quit(void)
 {
        if (search_history)
                string_list_free(search_history);
-       if (filelist)
-               filelist = mpdclient_filelist_free(filelist);
-       list_window_free(lw);
+       if (browser.filelist)
+               filelist_free(browser.filelist);
+       list_window_free(browser.lw);
 
        if (pattern) {
                g_free(pattern);
@@ -383,164 +376,109 @@ quit(void)
 }
 
 static void
-open(mpd_unused screen_t *screen, mpd_unused mpdclient_t *c)
+screen_search_open(G_GNUC_UNUSED struct mpdclient *c)
 {
-  //  if( pattern==NULL )
-  //    search_new(screen, c);
-  // else
-  screen_status_printf(_("Press %s for a new search"),
-                        get_key_names(CMD_SCREEN_SEARCH,0));
-  search_check_mode();
+       //  if( pattern==NULL )
+       //    search_new(screen, c);
+       // else
+       screen_status_printf(_("Press %s for a new search"),
+                            get_key_names(CMD_SCREEN_SEARCH,0));
+       search_check_mode();
 }
 
 static void
-resize(int cols, int rows)
+screen_search_resize(int cols, int rows)
 {
-  lw->cols = cols;
-  lw->rows = rows;
+       list_window_resize(browser.lw, cols, rows);
 }
 
 static void
-close(void)
+screen_search_paint(void)
 {
+       if (browser.filelist) {
+               browser.lw->hide_cursor = false;
+               screen_browser_paint(&browser);
+       } else {
+               browser.lw->hide_cursor = true;
+               list_window_paint(browser.lw, lw_search_help_callback, NULL);
+       }
 }
 
-static void 
-paint(mpd_unused screen_t *screen, mpdclient_t *c)
+static const char *
+screen_search_get_title(char *str, size_t size)
 {
-  lw->clear = 1;
-  
-  if( filelist )
-    {
-      lw->flags = 0;
-      list_window_paint(lw, browse_lw_callback, (void *) filelist);
-      filelist->updated = FALSE;
-    }
-  else
-    {
-      lw->flags = LW_HIDE_CURSOR;
-      list_window_paint(lw, lw_search_help_callback, NULL);
-      if( !MPD_VERSION_LT(c, 0, 12, 0) )
-       g_strdup_printf("Advanced search disabled (MPD version < 0.12.0"); 
-      //      wmove(lw->w, 0, 0);
-      //wclrtobot(lw->w);
-    }
-  wnoutrefresh(lw->w);
-}
+       if (advanced_search_mode && pattern)
+               g_snprintf(str, size, _("Search: %s"), pattern);
+       else if (pattern)
+               g_snprintf(str, size,
+                          _("Search: Results for %s [%s]"),
+                          pattern,
+                          _(mode[options.search_mode].label));
+       else
+               g_snprintf(str, size, _("Search: Press %s for a new search [%s]"),
+                          get_key_names(CMD_SCREEN_SEARCH,0),
+                          _(mode[options.search_mode].label));
 
-static void 
-update(screen_t *screen, mpdclient_t *c)
-{
-  if( filelist==NULL || filelist->updated )
-    {
-      paint(screen, c);
-      return;
-    }
-  list_window_paint(lw, browse_lw_callback, (void *) filelist);
-  wnoutrefresh(lw->w);
+       return str;
 }
 
-static const char *
-get_title(char *str, size_t size)
+static void
+screen_search_update(struct mpdclient *c)
 {
-  if( advanced_search_mode && pattern )
-    g_snprintf(str, size, _("Search: %s"), pattern);
-  else if( pattern )
-    g_snprintf(str, size, 
-              _("Search: Results for %s [%s]"), 
-              pattern,
-              _(mode[options.search_mode].label));
-  else
-    g_snprintf(str, size, _("Search: Press %s for a new search [%s]"),
-              get_key_names(CMD_SCREEN_SEARCH,0),
-              _(mode[options.search_mode].label));
-              
-  return str;
+       if (browser.filelist != NULL && c->events & MPD_IDLE_QUEUE) {
+               screen_browser_sync_highlights(browser.filelist, &c->playlist);
+               search_repaint();
+       }
 }
 
-static int 
-search_cmd(screen_t *screen, mpdclient_t *c, command_t cmd)
+static bool
+screen_search_cmd(struct mpdclient *c, command_t cmd)
 {
-  switch(cmd)
-    {
-    case CMD_PLAY:
-       browse_handle_enter(screen, c, lw, filelist);
-      return 1;
-
-    case CMD_SELECT:
-      if( browse_handle_select(screen, c, lw, filelist) == 0 )
-       {
-         /* continue and select next item... */
-         cmd = CMD_LIST_NEXT;
+       switch (cmd) {
+       case CMD_SEARCH_MODE:
+               options.search_mode++;
+               if (mode[options.search_mode].label == NULL)
+                       options.search_mode = 0;
+               screen_status_printf(_("Search mode: %s"),
+                                    _(mode[options.search_mode].label));
+               /* continue and update... */
+       case CMD_SCREEN_UPDATE:
+               screen_search_reload(c);
+               search_repaint();
+               return true;
+
+       case CMD_SCREEN_SEARCH:
+               search_new(c);
+               search_repaint();
+               return true;
+
+       case CMD_CLEAR:
+               search_clear(true);
+               list_window_reset(browser.lw);
+               search_repaint();
+               return true;
+
+       default:
+               break;
        }
-      /* call list_window_cmd to go to the next item */
-      return list_window_cmd(lw, filelist->length, cmd);
-
-    case CMD_SELECT_ALL:
-      browse_handle_select_all (screen, c, lw, filelist);
-      paint (screen, c);
-      return 0;
-
-    case CMD_SEARCH_MODE:
-      options.search_mode++;
-      if( mode[options.search_mode].label == NULL )
-       options.search_mode = 0;
-      screen_status_printf(_("Search mode: %s"), 
-                          _(mode[options.search_mode].label));
-      /* continue and update... */
-    case CMD_SCREEN_UPDATE:
-      if( pattern )
-       {
-         search_clear(screen, c, FALSE);
-         filelist = filelist_search(c, 
-                                    FALSE,
-                                    mode[options.search_mode].table,
-                                    pattern);
-         sync_highlights(c, filelist);
+
+       if (browser.filelist != NULL &&
+           browser_cmd(&browser, c, cmd)) {
+               if (screen_is_visible(&screen_search))
+                       search_repaint();
+               return true;
        }
-      return 1;
-
-    case CMD_SCREEN_SEARCH:
-      search_new(screen, c);
-      return 1;
-
-    case CMD_CLEAR:
-      search_clear(screen, c, TRUE);
-      list_window_reset(lw);
-      return 1;
-
-    case CMD_LIST_FIND:
-    case CMD_LIST_RFIND:
-    case CMD_LIST_FIND_NEXT:
-    case CMD_LIST_RFIND_NEXT:
-      if( filelist )
-       return screen_find(screen,
-                          lw, filelist->length,
-                          cmd, browse_lw_callback, (void *) filelist);
-      else
-       return 1;
-
-    case CMD_MOUSE_EVENT:
-      return browse_handle_mouse_event(screen,c,lw,filelist);
-
-    default:
-      if( filelist )
-       return list_window_cmd(lw, filelist->length, cmd);
-    }
-  
-  return 0;
+
+       return false;
 }
 
 const struct screen_functions screen_search = {
-       .init = init,
-       .exit = quit,
-       .open = open,
-       .close = close,
-       .resize = resize,
-       .paint = paint,
-       .update = update,
-       .cmd = search_cmd,
-       .get_title = get_title,
+       .init = screen_search_init,
+       .exit = screen_search_quit,
+       .open = screen_search_open,
+       .resize = screen_search_resize,
+       .paint = screen_search_paint,
+       .update = screen_search_update,
+       .cmd = screen_search_cmd,
+       .get_title = screen_search_get_title,
 };
-
-#endif /* ENABLE_SEARCH_SCREEN */