2 command-history.c : irssi
4 Copyright (C) 1999 Timo Sirainen
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version.
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
24 #include "special-vars.h"
27 #include "fe-windows.h"
28 #include "window-items.h"
31 static GList *history, *history_pos;
32 static int history_lines, history_over_counter;
33 static int window_history;
35 void command_history_add(WINDOW_REC *window, const char *text)
37 GList **phistory, *link;
40 g_return_if_fail(text != NULL);
43 /* window specific command history */
44 phistory = &window->history;
45 phistory_lines = &window->history_lines;
47 /* global command history */
49 phistory_lines = &history_lines;
52 if (settings_get_int("max_command_history") < 1 || *phistory_lines < settings_get_int("max_command_history"))
57 *phistory = g_list_remove_link(*phistory, link);
61 *phistory = g_list_append(*phistory, g_strdup(text));
64 const char *command_history_prev(WINDOW_REC *window, const char *text)
66 GList *pos, **phistory_pos;
67 int *phistory_over_counter;
70 phistory_pos = &window->history_pos;
71 phistory_over_counter = &window->history_over_counter;
73 phistory_pos = &history_pos;
74 phistory_over_counter = &history_over_counter;
78 if (*phistory_pos != NULL) {
79 *phistory_pos = (*phistory_pos)->prev;
80 if (*phistory_pos == NULL)
81 (*phistory_over_counter)++;
83 *phistory_pos = g_list_last(window_history ?
84 window->history : history);
88 (pos == NULL || strcmp(pos->data, text) != 0)) {
89 /* save the old entry to history */
90 command_history_add(window, text);
93 return *phistory_pos == NULL ? "" : (*phistory_pos)->data;
96 const char *command_history_next(WINDOW_REC *window, const char *text)
98 GList *pos, **phistory_pos;
99 int *phistory_over_counter;
101 if (window_history) {
102 phistory_pos = &window->history_pos;
103 phistory_over_counter = &window->history_over_counter;
105 phistory_pos = &history_pos;
106 phistory_over_counter = &history_over_counter;
112 *phistory_pos = (*phistory_pos)->next;
113 else if (*phistory_over_counter > 0) {
114 (*phistory_over_counter)--;
115 *phistory_pos = window_history ? window->history : history;
119 (pos == NULL || strcmp(pos->data, text) != 0)) {
120 /* save the old entry to history */
121 command_history_add(window, text);
123 return *phistory_pos == NULL ? "" : (*phistory_pos)->data;
126 void command_history_clear_pos(WINDOW_REC *window)
128 window->history_over_counter = 0;
129 window->history_pos = NULL;
130 history_over_counter = 0;
134 static void sig_window_destroyed(WINDOW_REC *window)
136 g_list_foreach(window->history, (GFunc) g_free, NULL);
137 g_list_free(window->history);
140 static char *special_history_func(const char *text, void *item, int *free_ret)
144 char *findtext, *ret;
146 window = item == NULL ? active_win : window_item_window(item);
148 findtext = g_strdup_printf("*%s*", text);
151 tmp = window_history ? window->history : history;
152 for (; tmp != NULL; tmp = tmp->next) {
153 const char *line = tmp->data;
155 if (match_wildcards(findtext, line)) {
157 ret = g_strdup(line);
165 static void read_settings(void)
167 window_history = settings_get_bool("window_history");
170 void command_history_init(void)
172 settings_add_int("history", "max_command_history", 100);
173 settings_add_bool("history", "window_history", FALSE);
175 special_history_func_set(special_history_func);
177 history_lines = 0; history_over_counter = 0;
178 history = NULL; history_pos = NULL;
181 signal_add("window destroyed", (SIGNAL_FUNC) sig_window_destroyed);
182 signal_add("setup changed", (SIGNAL_FUNC) read_settings);
185 void command_history_deinit(void)
187 signal_remove("window destroyed", (SIGNAL_FUNC) sig_window_destroyed);
188 signal_remove("setup changed", (SIGNAL_FUNC) read_settings);
190 g_list_foreach(history, (GFunc) g_free, NULL);
191 g_list_free(history);