From a74d6381557b6baca83ab89a5f6a7d89ffb0e7b0 Mon Sep 17 00:00:00 2001 From: Andrew Borodin Date: Sun, 29 Oct 2023 15:10:48 +0300 Subject: [PATCH] (mc_config_history_get_recent_item): new API. * (edit_search_cmd): use mc_config_history_get_recent_item(). * (mcview_continue_search_cmd): likewise. mc_config_history_get_recent_item() is not implemented optimally yet. Signed-off-by: Andrew Borodin --- lib/mcconfig.h | 2 ++ lib/mcconfig/history.c | 27 +++++++++++++++++++++++++++ src/editor/editsearch.c | 14 +++++--------- src/viewer/actions_cmd.c | 16 ++++++---------- 4 files changed, 40 insertions(+), 19 deletions(-) diff --git a/lib/mcconfig.h b/lib/mcconfig.h index 0c3ab3be7..abc28efe8 100644 --- a/lib/mcconfig.h +++ b/lib/mcconfig.h @@ -105,6 +105,8 @@ vfs_path_t *mc_config_get_full_vpath (const char *config_name); /* read history to the mc_config, but don't save config to file */ GList *mc_config_history_get (const char *name); +/* read recent item from the history */ +char *mc_config_history_get_recent_item (const char *name); /* load history from the mc_config */ GList *mc_config_history_load (mc_config_t * cfg, const char *name); /* save history to the mc_config, but don't save config to file */ diff --git a/lib/mcconfig/history.c b/lib/mcconfig/history.c index 7e9c0a32f..3bd2c8990 100644 --- a/lib/mcconfig/history.c +++ b/lib/mcconfig/history.c @@ -94,6 +94,33 @@ mc_config_history_get (const char *name) /* --------------------------------------------------------------------------------------------- */ +/** + * Get the recent item of a history from the ${XDG_DATA_HOME}/mc/history file. + * + * TODO: get rid of load the entire history to get the only top item. + */ + +char * +mc_config_history_get_recent_item (const char *name) +{ + GList *history; + char *item = NULL; + + history = mc_config_history_get (name); + if (history != NULL) + { + /* FIXME: can history->data be NULL? */ + item = (char *) history->data; + history->data = NULL; + history = g_list_first (history); + g_list_free_full (history, g_free); + } + + return item; +} + +/* --------------------------------------------------------------------------------------------- */ + /** * Load history from the mc_config */ diff --git a/src/editor/editsearch.c b/src/editor/editsearch.c index 1bdf88350..3ecc4587c 100644 --- a/src/editor/editsearch.c +++ b/src/editor/editsearch.c @@ -29,7 +29,7 @@ #include "lib/global.h" #include "lib/search.h" -#include "lib/mcconfig.h" /* mc_config_history_get */ +#include "lib/mcconfig.h" /* mc_config_history_get_recent_item() */ #ifdef HAVE_CHARSET #include "lib/charsets.h" /* cp_source */ #endif @@ -780,16 +780,12 @@ edit_search_cmd (WEdit * edit, gboolean again) else { /* find last search string in history */ - GList *history; + char *s; - history = mc_config_history_get (MC_HISTORY_SHARED_SEARCH); - if (history != NULL) + s = mc_config_history_get_recent_item (MC_HISTORY_SHARED_SEARCH); + if (s != NULL) { - /* FIXME: is it possible that history->data == NULL? */ - edit->last_search_string = (char *) history->data; - history->data = NULL; - history = g_list_first (history); - g_list_free_full (history, g_free); + edit->last_search_string = s; if (edit_search_init (edit, edit->last_search_string)) { diff --git a/src/viewer/actions_cmd.c b/src/viewer/actions_cmd.c index 465f0f028..6e6b57d96 100644 --- a/src/viewer/actions_cmd.c +++ b/src/viewer/actions_cmd.c @@ -57,13 +57,13 @@ #include "lib/charsets.h" #endif #include "lib/event.h" /* mc_event_raise() */ -#include "lib/mcconfig.h" /* mc_config_history_get() */ +#include "lib/mcconfig.h" /* mc_config_history_get_recent_item() */ #include "src/filemanager/layout.h" #include "src/filemanager/filemanager.h" /* current_panel */ #include "src/filemanager/ext.h" /* regex_command_for() */ -#include "src/history.h" +#include "src/history.h" /* MC_HISTORY_SHARED_SEARCH */ #include "src/file_history.h" /* show_file_history() */ #include "src/execute.h" #include "src/keymap.h" @@ -139,16 +139,12 @@ mcview_continue_search_cmd (WView * view) else { /* find last search string in history */ - GList *history; + char *s; - history = mc_config_history_get (MC_HISTORY_SHARED_SEARCH); - if (history != NULL) + s = mc_config_history_get_recent_item (MC_HISTORY_SHARED_SEARCH); + if (s != NULL) { - /* FIXME: is it possible that history->data == NULL? */ - view->last_search_string = (gchar *) history->data; - history->data = NULL; - history = g_list_first (history); - g_list_free_full (history, g_free); + view->last_search_string = s; if (mcview_search_init (view)) {