From 4659cea65a663b11f3cc16e1d03c9955a4135238 Mon Sep 17 00:00:00 2001 From: ailin-nemui Date: Tue, 15 Dec 2015 01:52:44 +0100 Subject: [PATCH 1/2] option to clear the history --- src/fe-common/core/command-history.c | 45 ++++++++++++++++++++++++---- src/fe-common/core/command-history.h | 1 + src/fe-common/core/fe-windows.c | 7 ++++- src/fe-common/core/fe-windows.h | 1 + src/fe-common/core/window-commands.c | 21 +++++++++++-- 5 files changed, 66 insertions(+), 9 deletions(-) diff --git a/src/fe-common/core/command-history.c b/src/fe-common/core/command-history.c index f9c3884c..9f46ee99 100644 --- a/src/fe-common/core/command-history.c +++ b/src/fe-common/core/command-history.c @@ -33,6 +33,7 @@ static HISTORY_REC *global_history; static int window_history; static GSList *histories; +static HISTORY_REC *last_cleared_history; void command_history_add(HISTORY_REC *history, const char *text) { @@ -41,6 +42,13 @@ void command_history_add(HISTORY_REC *history, const char *text) g_return_if_fail(history != NULL); g_return_if_fail(text != NULL); + if (last_cleared_history == history) { + last_cleared_history = NULL; + return; /* ignore this history addition, we just + cleared it */ + } + last_cleared_history = NULL; + link = g_list_last(history->list); if (link != NULL && g_strcmp0(link->data, text) == 0) return; /* same as previous entry */ @@ -94,13 +102,13 @@ HISTORY_REC *command_history_current(WINDOW_REC *window) if (window == NULL) return global_history; - if (window_history) - return window->history; - rec = command_history_find_name(window->history_name); if (rec != NULL) return rec; + if (window_history) + return window->history; + return global_history; } @@ -178,6 +186,18 @@ HISTORY_REC *command_history_create(const char *name) return rec; } +void command_history_clear(HISTORY_REC *history) +{ + g_return_if_fail(history != NULL); + + command_history_clear_pos_func(history, NULL); + g_list_foreach(history->list, (GFunc) g_free, NULL); + g_list_free(history->list); + history->list = NULL; + history->lines = 0; + last_cleared_history = history; +} + void command_history_destroy(HISTORY_REC *history) { g_return_if_fail(history != NULL); @@ -186,9 +206,8 @@ void command_history_destroy(HISTORY_REC *history) g_return_if_fail(history->refcount == 0); histories = g_slist_remove(histories, history); - - g_list_foreach(history->list, (GFunc) g_free, NULL); - g_list_free(history->list); + command_history_clear(history); + last_cleared_history = NULL; /* was destroyed */ g_free_not_null(history->name); g_free(history); @@ -229,6 +248,18 @@ static void sig_window_destroyed(WINDOW_REC *window) g_free_not_null(window->history_name); } +static void sig_window_history_cleared(WINDOW_REC *window, const char *name) { + HISTORY_REC *history; + + if (name == NULL || *name == '\0') { + history = command_history_current(window); + } else { + history = command_history_find_name(name); + } + + command_history_clear(history); +} + static void sig_window_history_changed(WINDOW_REC *window, const char *oldname) { command_history_link(window->history_name); @@ -279,6 +310,7 @@ void command_history_init(void) signal_add("window created", (SIGNAL_FUNC) sig_window_created); signal_add("window destroyed", (SIGNAL_FUNC) sig_window_destroyed); signal_add("window history changed", (SIGNAL_FUNC) sig_window_history_changed); + signal_add_last("window history cleared", (SIGNAL_FUNC) sig_window_history_cleared); signal_add("setup changed", (SIGNAL_FUNC) read_settings); } @@ -287,6 +319,7 @@ void command_history_deinit(void) signal_remove("window created", (SIGNAL_FUNC) sig_window_created); signal_remove("window destroyed", (SIGNAL_FUNC) sig_window_destroyed); signal_remove("window history changed", (SIGNAL_FUNC) sig_window_history_changed); + signal_remove("window history cleared", (SIGNAL_FUNC) sig_window_history_cleared); signal_remove("setup changed", (SIGNAL_FUNC) read_settings); command_history_destroy(global_history); diff --git a/src/fe-common/core/command-history.h b/src/fe-common/core/command-history.h index 7b76246b..a572216b 100644 --- a/src/fe-common/core/command-history.h +++ b/src/fe-common/core/command-history.h @@ -28,6 +28,7 @@ const char *command_history_next(WINDOW_REC *window, const char *text); void command_history_clear_pos(WINDOW_REC *window); HISTORY_REC *command_history_create(const char *name); +void command_history_clear(HISTORY_REC *history); void command_history_destroy(HISTORY_REC *history); void command_history_link(const char *name); void command_history_unlink(const char *name); diff --git a/src/fe-common/core/fe-windows.c b/src/fe-common/core/fe-windows.c index 1049137f..46c1593b 100644 --- a/src/fe-common/core/fe-windows.c +++ b/src/fe-common/core/fe-windows.c @@ -229,11 +229,16 @@ void window_set_history(WINDOW_REC *window, const char *name) else window->history_name = g_strdup(name); - signal_emit("window history changed", 1, window, oldname); + signal_emit("window history changed", 2, window, oldname); g_free_not_null(oldname); } +void window_clear_history(WINDOW_REC *window, const char *name) +{ + signal_emit("window history cleared", 2, window, name); +} + void window_set_level(WINDOW_REC *window, int level) { g_return_if_fail(window != NULL); diff --git a/src/fe-common/core/fe-windows.h b/src/fe-common/core/fe-windows.h index 613f15f8..32d6cfcd 100644 --- a/src/fe-common/core/fe-windows.h +++ b/src/fe-common/core/fe-windows.h @@ -66,6 +66,7 @@ void window_change_server(WINDOW_REC *window, void *server); void window_set_refnum(WINDOW_REC *window, int refnum); void window_set_name(WINDOW_REC *window, const char *name); void window_set_history(WINDOW_REC *window, const char *name); +void window_clear_history(WINDOW_REC *window, const char *name); void window_set_level(WINDOW_REC *window, int level); void window_set_immortal(WINDOW_REC *window, int immortal); diff --git a/src/fe-common/core/window-commands.c b/src/fe-common/core/window-commands.c index c6ab68c0..e5005144 100644 --- a/src/fe-common/core/window-commands.c +++ b/src/fe-common/core/window-commands.c @@ -33,6 +33,7 @@ #include "window-items.h" #include "windows-layout.h" #include "printtext.h" +#include "command-history.h" static void window_print_binds(WINDOW_REC *win) { @@ -615,10 +616,25 @@ static void cmd_window_name(const char *data) } } -/* SYNTAX: WINDOW HISTORY */ +/* SYNTAX: WINDOW HISTORY [-clear] */ void cmd_window_history(const char *data) { - window_set_history(active_win, data); + GHashTable *optlist; + char *name; + void *free_arg; + + if (!cmd_get_params(data, &free_arg, 1 | PARAM_FLAG_OPTIONS, + "window history", &optlist, &name)) + return; + + if (g_hash_table_lookup(optlist, "clear") != NULL) { + signal_continue(1, data); + window_clear_history(active_win, name); + } else { + window_set_history(active_win, name); + } + + cmd_params_free(free_arg); } /* we're moving the first window to last - move the first contiguous block @@ -883,6 +899,7 @@ void window_commands_init(void) command_set_options("window number", "sticky"); command_set_options("window server", "sticky unsticky"); command_set_options("window theme", "delete"); + command_set_options("window history", "clear"); } void window_commands_deinit(void) From ad842ea8a60f107d68df4298a8914e66edf95c3b Mon Sep 17 00:00:00 2001 From: ailin-nemui Date: Fri, 8 Jan 2016 15:42:59 +0100 Subject: [PATCH 2/2] reorder history add and fixes --- src/fe-common/core/command-history.c | 10 ---------- src/fe-common/core/window-commands.c | 4 ++-- src/fe-text/gui-readline.c | 15 +++++++-------- 3 files changed, 9 insertions(+), 20 deletions(-) diff --git a/src/fe-common/core/command-history.c b/src/fe-common/core/command-history.c index 9f46ee99..1060744e 100644 --- a/src/fe-common/core/command-history.c +++ b/src/fe-common/core/command-history.c @@ -33,7 +33,6 @@ static HISTORY_REC *global_history; static int window_history; static GSList *histories; -static HISTORY_REC *last_cleared_history; void command_history_add(HISTORY_REC *history, const char *text) { @@ -42,13 +41,6 @@ void command_history_add(HISTORY_REC *history, const char *text) g_return_if_fail(history != NULL); g_return_if_fail(text != NULL); - if (last_cleared_history == history) { - last_cleared_history = NULL; - return; /* ignore this history addition, we just - cleared it */ - } - last_cleared_history = NULL; - link = g_list_last(history->list); if (link != NULL && g_strcmp0(link->data, text) == 0) return; /* same as previous entry */ @@ -195,7 +187,6 @@ void command_history_clear(HISTORY_REC *history) g_list_free(history->list); history->list = NULL; history->lines = 0; - last_cleared_history = history; } void command_history_destroy(HISTORY_REC *history) @@ -207,7 +198,6 @@ void command_history_destroy(HISTORY_REC *history) histories = g_slist_remove(histories, history); command_history_clear(history); - last_cleared_history = NULL; /* was destroyed */ g_free_not_null(history->name); g_free(history); diff --git a/src/fe-common/core/window-commands.c b/src/fe-common/core/window-commands.c index e5005144..9e4aab3a 100644 --- a/src/fe-common/core/window-commands.c +++ b/src/fe-common/core/window-commands.c @@ -620,10 +620,10 @@ static void cmd_window_name(const char *data) void cmd_window_history(const char *data) { GHashTable *optlist; - char *name; + char *name; void *free_arg; - if (!cmd_get_params(data, &free_arg, 1 | PARAM_FLAG_OPTIONS, + if (!cmd_get_params(data, &free_arg, 1 | PARAM_FLAG_OPTIONS | PARAM_FLAG_STRIP_TRAILING_WS, "window history", &optlist, &name)) return; diff --git a/src/fe-text/gui-readline.c b/src/fe-text/gui-readline.c index fcf152e8..5acfaf60 100644 --- a/src/fe-text/gui-readline.c +++ b/src/fe-text/gui-readline.c @@ -456,22 +456,21 @@ static void key_send_line(void) add_history = *str != '\0'; history = command_history_current(active_win); + if (redir != NULL && redir->flags & ENTRY_REDIRECT_FLAG_HIDDEN) + add_history = 0; + + if (add_history && history != NULL) { + command_history_add(history, str); + } + if (redir == NULL) { signal_emit("send command", 3, str, active_win->active_server, active_win->active); } else { - if (redir->flags & ENTRY_REDIRECT_FLAG_HIDDEN) - add_history = 0; handle_entry_redirect(str); } - if (add_history) { - history = command_history_find(history); - if (history != NULL) - command_history_add(history, str); - } - if (active_entry != NULL) gui_entry_set_text(active_entry, ""); command_history_clear_pos(active_win);