summaryrefslogtreecommitdiff
path: root/src/fe-common/core/command-history.c
blob: cdf4b5a5b70663173beeb87ced14a98dca66cb55 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
/*
 command-history.c : irssi

    Copyright (C) 1999 Timo Sirainen

    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., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
*/

#include "module.h"
#include "signals.h"
#include "misc.h"
#include "special-vars.h"
#include "settings.h"

#include "fe-windows.h"
#include "window-items.h"

/* command history */
static GList *history, *history_pos;
static int history_lines, history_over_counter;
static int window_history;

void command_history_add(WINDOW_REC *window, const char *text)
{
	GList **phistory, *link;
	int *phistory_lines;

	g_return_if_fail(text != NULL);

	if (window_history) {
		/* window specific command history */
		phistory = &window->history;
		phistory_lines = &window->history_lines;
	} else {
		/* global command history */
		phistory = &history;
		phistory_lines = &history_lines;
	}

	if (settings_get_int("max_command_history") < 1 || *phistory_lines < settings_get_int("max_command_history"))
		(*phistory_lines)++;
	else {
		link = *phistory;
		g_free(link->data);
		*phistory = g_list_remove_link(*phistory, link);
		g_list_free_1(link);
	}

	*phistory = g_list_append(*phistory, g_strdup(text));
}

const char *command_history_prev(WINDOW_REC *window, const char *text)
{
	GList *pos, **phistory_pos;
        int *phistory_over_counter;

	if (window_history) {
		phistory_pos = &window->history_pos;
		phistory_over_counter = &window->history_over_counter;
	} else {
		phistory_pos = &history_pos;
		phistory_over_counter = &history_over_counter;
	}

	pos = *phistory_pos;
	if (*phistory_pos != NULL) {
		*phistory_pos = (*phistory_pos)->prev;
		if (*phistory_pos == NULL)
                        (*phistory_over_counter)++;
	} else {
		*phistory_pos = g_list_last(window_history ?
					    window->history : history);
	}

	if (*text != '\0' &&
	    (pos == NULL || strcmp(pos->data, text) != 0)) {
		/* save the old entry to history */
		command_history_add(window, text);
	}

	return *phistory_pos == NULL ? "" : (*phistory_pos)->data;
}

const char *command_history_next(WINDOW_REC *window, const char *text)
{
	GList *pos, **phistory_pos;
        int *phistory_over_counter;

	if (window_history) {
		phistory_pos = &window->history_pos;
		phistory_over_counter = &window->history_over_counter;
	} else {
		phistory_pos = &history_pos;
		phistory_over_counter = &history_over_counter;
	}

	pos = *phistory_pos;

	if (pos != NULL)
		*phistory_pos = (*phistory_pos)->next;
	else if (*phistory_over_counter > 0) {
		(*phistory_over_counter)--;
		*phistory_pos = window_history ? window->history : history;
	}

	if (*text != '\0' &&
	    (pos == NULL || strcmp(pos->data, text) != 0)) {
		/* save the old entry to history */
		command_history_add(window, text);
	}
	return *phistory_pos == NULL ? "" : (*phistory_pos)->data;
}

void command_history_clear_pos(WINDOW_REC *window)
{
	window->history_over_counter = 0;
	window->history_pos = NULL;
        history_over_counter = 0;
	history_pos = NULL;
}

static void sig_window_destroyed(WINDOW_REC *window)
{
	g_list_foreach(window->history, (GFunc) g_free, NULL);
	g_list_free(window->history);
}

static char *special_history_func(const char *text, void *item, int *free_ret)
{
	WINDOW_REC *window;
	GList *tmp;
        char *findtext, *ret;

	window = item == NULL ? active_win : window_item_window(item);

	findtext = g_strdup_printf("*%s*", text);
	ret = NULL;

	tmp = window_history ? window->history : history;
	for (; tmp != NULL; tmp = tmp->next) {
		const char *line = tmp->data;

		if (match_wildcards(findtext, line)) {
			*free_ret = TRUE;
                        ret = g_strdup(line);
		}
	}
	g_free(findtext);

	return ret;
}

static void read_settings(void)
{
	window_history = settings_get_bool("window_history");
}

void command_history_init(void)
{
	settings_add_int("history", "max_command_history", 100);
	settings_add_bool("history", "window_history", FALSE);

	special_history_func_set(special_history_func);

	history_lines = 0; history_over_counter = 0;
	history = NULL; history_pos = NULL;

	read_settings();
	signal_add("window destroyed", (SIGNAL_FUNC) sig_window_destroyed);
	signal_add("setup changed", (SIGNAL_FUNC) read_settings);
}

void command_history_deinit(void)
{
	signal_remove("window destroyed", (SIGNAL_FUNC) sig_window_destroyed);
	signal_remove("setup changed", (SIGNAL_FUNC) read_settings);

	g_list_foreach(history, (GFunc) g_free, NULL);
	g_list_free(history);
}