From 15eacd43a481a4fc67789a25d56bb5f1527bfc12 Mon Sep 17 00:00:00 2001 From: Max Kellermann Date: Fri, 2 Oct 2009 00:10:23 +0200 Subject: [PATCH] screen_utils: moved screen_find() to screen_find.c --- Makefile.am | 2 + src/screen_artist.c | 2 +- src/screen_browser.c | 2 +- src/screen_find.c | 145 +++++++++++++++++++++++++++++++++++++++++++ src/screen_find.h | 39 ++++++++++++ src/screen_help.c | 2 +- src/screen_keydef.c | 1 + src/screen_play.c | 1 + src/screen_song.c | 2 +- src/screen_text.c | 2 +- src/screen_utils.c | 126 ------------------------------------- src/screen_utils.h | 13 ---- 12 files changed, 193 insertions(+), 144 deletions(-) create mode 100644 src/screen_find.c create mode 100644 src/screen_find.h diff --git a/Makefile.am b/Makefile.am index 52b5953..fc47485 100644 --- a/Makefile.am +++ b/Makefile.am @@ -29,6 +29,7 @@ ncmpc_headers = \ src/screen_message.h \ src/screen_interface.h \ src/screen_list.h \ + src/screen_find.h \ src/screen_utils.h \ src/screen_client.h \ src/list_window.h \ @@ -82,6 +83,7 @@ src_ncmpc_SOURCES = \ src/screen.c \ src/screen_message.c \ src/screen_list.c \ + src/screen_find.c \ src/screen_utils.c \ src/screen_client.c \ src/screen_play.c \ diff --git a/src/screen_artist.c b/src/screen_artist.c index 89fc55e..bda8970 100644 --- a/src/screen_artist.c +++ b/src/screen_artist.c @@ -20,11 +20,11 @@ #include "screen_artist.h" #include "screen_interface.h" #include "screen_message.h" +#include "screen_find.h" #include "screen.h" #include "i18n.h" #include "charset.h" #include "mpdclient.h" -#include "screen_utils.h" #include "screen_browser.h" #include "filelist.h" diff --git a/src/screen_browser.c b/src/screen_browser.c index 638803d..851f18c 100644 --- a/src/screen_browser.c +++ b/src/screen_browser.c @@ -22,12 +22,12 @@ #include "screen_song.h" #include "screen_lyrics.h" #include "screen_message.h" +#include "screen_find.h" #include "screen.h" #include "i18n.h" #include "options.h" #include "charset.h" #include "strfsong.h" -#include "screen_utils.h" #include "mpdclient.h" #include "filelist.h" diff --git a/src/screen_find.c b/src/screen_find.c new file mode 100644 index 0000000..f779961 --- /dev/null +++ b/src/screen_find.c @@ -0,0 +1,145 @@ +/* ncmpc (Ncurses MPD Client) + * (c) 2004-2009 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 "screen_find.h" +#include "screen_utils.h" +#include "screen_message.h" +#include "screen.h" +#include "i18n.h" +#include "options.h" +#include "ncmpc.h" + +#define FIND_PROMPT _("Find") +#define RFIND_PROMPT _("Find backward") +#define JUMP_PROMPT _("Jump") + +/* query user for a string and find it in a list window */ +int +screen_find(list_window_t *lw, + int rows, + 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) { + prompt = RFIND_PROMPT; + reversed = 1; + } + + switch (findcmd) { + case CMD_LIST_FIND: + case CMD_LIST_RFIND: + if (screen.findbuf) { + g_free(screen.findbuf); + screen.findbuf=NULL; + } + /* continue... */ + + case CMD_LIST_FIND_NEXT: + case CMD_LIST_RFIND_NEXT: + if (!screen.findbuf) + screen.findbuf=screen_readln(prompt, + value, + &screen.find_history, + NULL); + + if (screen.findbuf == NULL) + return 1; + + found = reversed + ? list_window_rfind(lw, + callback_fn, callback_data, + screen.findbuf, + options.find_wrap, + options.bell_on_wrap, + rows) + : list_window_find(lw, + callback_fn, callback_data, + screen.findbuf, + options.find_wrap, + options.bell_on_wrap); + if (!found) { + screen_status_printf(_("Unable to find \'%s\'"), + screen.findbuf); + screen_bell(); + } + return 1; + default: + break; + } + return 0; +} + +/* query user for a string and jump to the entry + * which begins with this string while the users types */ +void +screen_jump(struct list_window *lw, + list_window_callback_fn_t callback_fn, + void *callback_data) +{ + char *search_str, *iter; + const int WRLN_MAX_LINE_SIZE = 1024; + int key = 65; + command_t cmd; + + if (screen.findbuf) { + g_free(screen.findbuf); + screen.findbuf = NULL; + } + 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; + + /* unfortunately wgetch returns "next/previous-page" not as an ascii-char */ + while(!g_ascii_iscntrl(key) && key != KEY_NPAGE && key != KEY_PPAGE) { + key = screen_getch(screen.findbuf); + /* if backspace or delete was pressed */ + if (key == KEY_BACKSPACE || 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; + } + 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); + wrefresh(lw->w); + } + + /* ncmpc should get the command */ + ungetch(key); + if ((cmd=get_keyboard_command()) != CMD_NONE) + do_input_event(cmd); +} diff --git a/src/screen_find.h b/src/screen_find.h new file mode 100644 index 0000000..5148bd4 --- /dev/null +++ b/src/screen_find.h @@ -0,0 +1,39 @@ +/* ncmpc (Ncurses MPD Client) + * (c) 2004-2009 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. + */ + +#ifndef NCMPC_SCREEN_FIND_H +#define NCMPC_SCREEN_FIND_H + +#include "command.h" +#include "list_window.h" + +/* query user for a string and find it in a list window */ +int screen_find(struct list_window *lw, + int rows, + command_t findcmd, + list_window_callback_fn_t callback_fn, + void *callback_data); + +/* query user for a string and jump to the entry + * which begins with this string while the users types */ +void screen_jump(struct list_window *lw, + list_window_callback_fn_t callback_fn, + void *callback_data); + +#endif diff --git a/src/screen_help.c b/src/screen_help.c index 042478a..6f4850b 100644 --- a/src/screen_help.c +++ b/src/screen_help.c @@ -19,9 +19,9 @@ #include "screen_help.h" #include "screen_interface.h" +#include "screen_find.h" #include "config.h" #include "i18n.h" -#include "screen_utils.h" #include diff --git a/src/screen_keydef.c b/src/screen_keydef.c index d93757a..28822e5 100644 --- a/src/screen_keydef.c +++ b/src/screen_keydef.c @@ -20,6 +20,7 @@ #include "screen_keydef.h" #include "screen_interface.h" #include "screen_message.h" +#include "screen_find.h" #include "i18n.h" #include "conf.h" #include "screen.h" diff --git a/src/screen_play.c b/src/screen_play.c index 3adcb6d..f481d5b 100644 --- a/src/screen_play.c +++ b/src/screen_play.c @@ -21,6 +21,7 @@ #include "screen_interface.h" #include "screen_file.h" #include "screen_message.h" +#include "screen_find.h" #include "config.h" #include "i18n.h" #include "charset.h" diff --git a/src/screen_song.c b/src/screen_song.c index eeb2f11..d0ad9f9 100644 --- a/src/screen_song.c +++ b/src/screen_song.c @@ -21,9 +21,9 @@ #include "screen_interface.h" #include "screen_file.h" #include "screen_lyrics.h" +#include "screen_find.h" #include "i18n.h" #include "screen.h" -#include "screen_utils.h" #include "charset.h" #include "utils.h" #include "mpdclient.h" diff --git a/src/screen_text.c b/src/screen_text.c index 6f642e8..faee9cb 100644 --- a/src/screen_text.c +++ b/src/screen_text.c @@ -18,7 +18,7 @@ */ #include "screen_text.h" -#include "screen_utils.h" +#include "screen_find.h" #include "charset.h" #include diff --git a/src/screen_utils.c b/src/screen_utils.c index 6cd7232..39b1162 100644 --- a/src/screen_utils.c +++ b/src/screen_utils.c @@ -18,7 +18,6 @@ */ #include "screen_utils.h" -#include "screen_message.h" #include "screen.h" #include "mpdclient.h" #include "config.h" @@ -26,19 +25,9 @@ #include "options.h" #include "colors.h" #include "wreadln.h" -#ifndef NCMPC_H -#include "ncmpc.h" -#endif /* NCMPC_H */ #include -#include -#include - -#define FIND_PROMPT _("Find") -#define RFIND_PROMPT _("Find backward") -#define JUMP_PROMPT _("Jump") - void screen_bell(void) { @@ -114,121 +103,6 @@ screen_read_password(const char *prompt) return ret; } -/* query user for a string and find it in a list window */ -int -screen_find(list_window_t *lw, - int rows, - 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) { - prompt = RFIND_PROMPT; - reversed = 1; - } - - switch (findcmd) { - case CMD_LIST_FIND: - case CMD_LIST_RFIND: - if (screen.findbuf) { - g_free(screen.findbuf); - screen.findbuf=NULL; - } - /* continue... */ - - case CMD_LIST_FIND_NEXT: - case CMD_LIST_RFIND_NEXT: - if (!screen.findbuf) - screen.findbuf=screen_readln(prompt, - value, - &screen.find_history, - NULL); - - if (screen.findbuf == NULL) - return 1; - - found = reversed - ? list_window_rfind(lw, - callback_fn, callback_data, - screen.findbuf, - options.find_wrap, - options.bell_on_wrap, - rows) - : list_window_find(lw, - callback_fn, callback_data, - screen.findbuf, - options.find_wrap, - options.bell_on_wrap); - if (!found) { - screen_status_printf(_("Unable to find \'%s\'"), - screen.findbuf); - screen_bell(); - } - return 1; - default: - break; - } - return 0; -} - -/* query user for a string and jump to the entry - * which begins with this string while the users types */ -void -screen_jump(struct list_window *lw, - list_window_callback_fn_t callback_fn, - void *callback_data) -{ - char *search_str, *iter; - const int WRLN_MAX_LINE_SIZE = 1024; - int key = 65; - command_t cmd; - - if (screen.findbuf) { - g_free(screen.findbuf); - screen.findbuf = NULL; - } - 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; - - /* unfortunately wgetch returns "next/previous-page" not as an ascii-char */ - while(!g_ascii_iscntrl(key) && key != KEY_NPAGE && key != KEY_PPAGE) { - key = screen_getch(screen.findbuf); - /* if backspace or delete was pressed */ - if (key == KEY_BACKSPACE || 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; - } - 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); - wrefresh(lw->w); - } - - /* ncmpc should get the command */ - ungetch(key); - if ((cmd=get_keyboard_command()) != CMD_NONE) - do_input_event(cmd); -} - void screen_display_completion_list(GList *list) { diff --git a/src/screen_utils.h b/src/screen_utils.h index bbcdbf2..288535d 100644 --- a/src/screen_utils.h +++ b/src/screen_utils.h @@ -38,19 +38,6 @@ screen_read_password(const char *prompt); char *screen_readln(const char *prompt, const char *value, GList **history, GCompletion *gcmp); -/* query user for a string and find it in a list window */ -int screen_find(struct list_window *lw, - int rows, - command_t findcmd, - list_window_callback_fn_t callback_fn, - void *callback_data); - -/* query user for a string and jump to the entry - * which begins with this string while the users types */ -void screen_jump(struct list_window *lw, - list_window_callback_fn_t callback_fn, - void *callback_data); - void screen_display_completion_list(GList *list); #ifndef NCMPC_MINI -- 2.30.2