addition of silc.css
[crypto.git] / apps / irssi / src / fe-common / core / command-history.c
1 /*
2  command-history.c : irssi
3
4     Copyright (C) 1999 Timo Sirainen
5
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.
10
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.
15
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
19 */
20
21 #include "module.h"
22 #include "signals.h"
23 #include "misc.h"
24 #include "special-vars.h"
25 #include "settings.h"
26
27 #include "fe-windows.h"
28 #include "window-items.h"
29
30 /* command history */
31 static GList *history, *history_pos;
32 static int history_lines, history_over_counter;
33 static int window_history;
34
35 void command_history_add(WINDOW_REC *window, const char *text)
36 {
37         GList **phistory, *link;
38         int *phistory_lines;
39
40         g_return_if_fail(text != NULL);
41
42         if (window_history) {
43                 /* window specific command history */
44                 phistory = &window->history;
45                 phistory_lines = &window->history_lines;
46         } else {
47                 /* global command history */
48                 phistory = &history;
49                 phistory_lines = &history_lines;
50         }
51
52         if (settings_get_int("max_command_history") < 1 || *phistory_lines < settings_get_int("max_command_history"))
53                 (*phistory_lines)++;
54         else {
55                 link = *phistory;
56                 g_free(link->data);
57                 *phistory = g_list_remove_link(*phistory, link);
58                 g_list_free_1(link);
59         }
60
61         *phistory = g_list_append(*phistory, g_strdup(text));
62 }
63
64 const char *command_history_prev(WINDOW_REC *window, const char *text)
65 {
66         GList *pos, **phistory_pos;
67         int *phistory_over_counter;
68
69         if (window_history) {
70                 phistory_pos = &window->history_pos;
71                 phistory_over_counter = &window->history_over_counter;
72         } else {
73                 phistory_pos = &history_pos;
74                 phistory_over_counter = &history_over_counter;
75         }
76
77         pos = *phistory_pos;
78         if (*phistory_pos != NULL) {
79                 *phistory_pos = (*phistory_pos)->prev;
80                 if (*phistory_pos == NULL)
81                         (*phistory_over_counter)++;
82         } else {
83                 *phistory_pos = g_list_last(window_history ?
84                                             window->history : history);
85         }
86
87         if (*text != '\0' &&
88             (pos == NULL || strcmp(pos->data, text) != 0)) {
89                 /* save the old entry to history */
90                 command_history_add(window, text);
91         }
92
93         return *phistory_pos == NULL ? "" : (*phistory_pos)->data;
94 }
95
96 const char *command_history_next(WINDOW_REC *window, const char *text)
97 {
98         GList *pos, **phistory_pos;
99         int *phistory_over_counter;
100
101         if (window_history) {
102                 phistory_pos = &window->history_pos;
103                 phistory_over_counter = &window->history_over_counter;
104         } else {
105                 phistory_pos = &history_pos;
106                 phistory_over_counter = &history_over_counter;
107         }
108
109         pos = *phistory_pos;
110
111         if (pos != NULL)
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;
116         }
117
118         if (*text != '\0' &&
119             (pos == NULL || strcmp(pos->data, text) != 0)) {
120                 /* save the old entry to history */
121                 command_history_add(window, text);
122         }
123         return *phistory_pos == NULL ? "" : (*phistory_pos)->data;
124 }
125
126 void command_history_clear_pos(WINDOW_REC *window)
127 {
128         window->history_over_counter = 0;
129         window->history_pos = NULL;
130         history_over_counter = 0;
131         history_pos = NULL;
132 }
133
134 static void sig_window_destroyed(WINDOW_REC *window)
135 {
136         g_list_foreach(window->history, (GFunc) g_free, NULL);
137         g_list_free(window->history);
138 }
139
140 static char *special_history_func(const char *text, void *item, int *free_ret)
141 {
142         WINDOW_REC *window;
143         GList *tmp;
144         char *findtext, *ret;
145
146         window = item == NULL ? active_win : window_item_window(item);
147
148         findtext = g_strdup_printf("*%s*", text);
149         ret = NULL;
150
151         tmp = window_history ? window->history : history;
152         for (; tmp != NULL; tmp = tmp->next) {
153                 const char *line = tmp->data;
154
155                 if (match_wildcards(findtext, line)) {
156                         *free_ret = TRUE;
157                         ret = g_strdup(line);
158                 }
159         }
160         g_free(findtext);
161
162         return ret;
163 }
164
165 static void read_settings(void)
166 {
167         window_history = settings_get_bool("window_history");
168 }
169
170 void command_history_init(void)
171 {
172         settings_add_int("history", "max_command_history", 100);
173         settings_add_bool("history", "window_history", FALSE);
174
175         special_history_func_set(special_history_func);
176
177         history_lines = 0; history_over_counter = 0;
178         history = NULL; history_pos = NULL;
179
180         read_settings();
181         signal_add("window destroyed", (SIGNAL_FUNC) sig_window_destroyed);
182         signal_add("setup changed", (SIGNAL_FUNC) read_settings);
183 }
184
185 void command_history_deinit(void)
186 {
187         signal_remove("window destroyed", (SIGNAL_FUNC) sig_window_destroyed);
188         signal_remove("setup changed", (SIGNAL_FUNC) read_settings);
189
190         g_list_foreach(history, (GFunc) g_free, NULL);
191         g_list_free(history);
192 }