2000-04-26 04:03:38 -04:00
|
|
|
/*
|
|
|
|
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"
|
|
|
|
|
2000-11-17 11:27:14 -05:00
|
|
|
#include "fe-windows.h"
|
2000-04-26 04:03:38 -04:00
|
|
|
#include "window-items.h"
|
|
|
|
|
2001-12-20 08:29:20 -05:00
|
|
|
#include "command-history.h"
|
|
|
|
|
2000-04-26 04:03:38 -04:00
|
|
|
/* command history */
|
2001-12-20 08:29:20 -05:00
|
|
|
static HISTORY_REC *global_history;
|
2000-04-26 04:03:38 -04:00
|
|
|
static int window_history;
|
2001-12-20 08:29:20 -05:00
|
|
|
static GSList *histories;
|
2000-04-26 04:03:38 -04:00
|
|
|
|
2001-12-20 08:29:20 -05:00
|
|
|
void command_history_add(HISTORY_REC *history, const char *text)
|
2000-04-26 04:03:38 -04:00
|
|
|
{
|
2001-12-20 08:29:20 -05:00
|
|
|
GList *link;
|
2000-04-26 04:03:38 -04:00
|
|
|
|
2001-12-20 08:29:20 -05:00
|
|
|
g_return_if_fail(history != NULL);
|
2000-04-26 04:03:38 -04:00
|
|
|
g_return_if_fail(text != NULL);
|
|
|
|
|
2001-12-20 08:29:20 -05:00
|
|
|
if (settings_get_int("max_command_history") < 1 ||
|
|
|
|
history->lines < settings_get_int("max_command_history"))
|
|
|
|
history->lines++;
|
2000-04-26 04:03:38 -04:00
|
|
|
else {
|
2001-12-20 08:29:20 -05:00
|
|
|
link = history->list;
|
2000-04-26 04:03:38 -04:00
|
|
|
g_free(link->data);
|
2001-12-20 08:29:20 -05:00
|
|
|
history->list = g_list_remove_link(history->list, link);
|
2000-04-26 04:03:38 -04:00
|
|
|
g_list_free_1(link);
|
|
|
|
}
|
|
|
|
|
2001-12-20 08:29:20 -05:00
|
|
|
history->list = g_list_append(history->list, g_strdup(text));
|
2000-04-26 04:03:38 -04:00
|
|
|
}
|
|
|
|
|
2001-12-20 08:29:20 -05:00
|
|
|
HISTORY_REC *command_history_find(HISTORY_REC *history)
|
2000-04-26 04:03:38 -04:00
|
|
|
{
|
2001-12-20 08:29:20 -05:00
|
|
|
GSList *tmp;
|
|
|
|
tmp = g_slist_find(histories, history);
|
2000-04-26 04:03:38 -04:00
|
|
|
|
2001-12-20 08:29:20 -05:00
|
|
|
if (tmp == NULL)
|
|
|
|
return NULL;
|
|
|
|
else
|
|
|
|
return tmp->data;
|
|
|
|
}
|
|
|
|
|
|
|
|
HISTORY_REC *command_history_find_name(const char *name)
|
|
|
|
{
|
|
|
|
GSList *tmp;
|
|
|
|
|
|
|
|
if (name == NULL)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
for (tmp = histories; tmp != NULL; tmp = tmp->next) {
|
|
|
|
HISTORY_REC *rec = tmp->data;
|
|
|
|
|
|
|
|
if (rec->name != NULL && g_strcasecmp(rec->name, name) == 0)
|
|
|
|
return rec;
|
2001-03-07 19:45:56 -05:00
|
|
|
}
|
2001-12-20 08:29:20 -05:00
|
|
|
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
HISTORY_REC *command_history_current(WINDOW_REC *window)
|
|
|
|
{
|
|
|
|
HISTORY_REC *rec;
|
2000-04-26 04:03:38 -04:00
|
|
|
|
2001-12-20 08:29:20 -05:00
|
|
|
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;
|
|
|
|
|
|
|
|
return global_history;
|
|
|
|
}
|
|
|
|
|
|
|
|
const char *command_history_prev(WINDOW_REC *window, const char *text)
|
|
|
|
{
|
|
|
|
HISTORY_REC *history;
|
|
|
|
GList *pos;
|
|
|
|
|
|
|
|
history = command_history_current(window);
|
|
|
|
pos = history->pos;
|
|
|
|
|
|
|
|
if (pos != NULL) {
|
|
|
|
history->pos = history->pos->prev;
|
|
|
|
if (history->pos == NULL)
|
|
|
|
history->over_counter++;
|
2001-03-07 19:45:56 -05:00
|
|
|
} else {
|
2001-12-20 08:29:20 -05:00
|
|
|
history->pos = g_list_last(history->list);
|
2001-03-07 19:45:56 -05:00
|
|
|
}
|
2000-04-26 04:03:38 -04:00
|
|
|
|
|
|
|
if (*text != '\0' &&
|
|
|
|
(pos == NULL || strcmp(pos->data, text) != 0)) {
|
|
|
|
/* save the old entry to history */
|
2001-12-20 08:29:20 -05:00
|
|
|
command_history_add(history, text);
|
2000-04-26 04:03:38 -04:00
|
|
|
}
|
|
|
|
|
2001-12-20 08:29:20 -05:00
|
|
|
return history->pos == NULL ? "" : history->pos->data;
|
2000-04-26 04:03:38 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
const char *command_history_next(WINDOW_REC *window, const char *text)
|
|
|
|
{
|
2001-12-20 08:29:20 -05:00
|
|
|
HISTORY_REC *history;
|
|
|
|
GList *pos;
|
2000-04-26 04:03:38 -04:00
|
|
|
|
2001-12-20 08:29:20 -05:00
|
|
|
history = command_history_current(window);
|
|
|
|
pos = history->pos;
|
2000-07-31 18:19:32 -04:00
|
|
|
|
2000-09-06 19:15:42 -04:00
|
|
|
if (pos != NULL)
|
2001-12-20 08:29:20 -05:00
|
|
|
history->pos = history->pos->next;
|
|
|
|
else if (history->over_counter > 0) {
|
|
|
|
history->over_counter--;
|
|
|
|
history->pos = history->list;
|
2001-03-07 19:45:56 -05:00
|
|
|
}
|
2000-04-26 04:03:38 -04:00
|
|
|
|
|
|
|
if (*text != '\0' &&
|
|
|
|
(pos == NULL || strcmp(pos->data, text) != 0)) {
|
|
|
|
/* save the old entry to history */
|
2001-12-20 08:29:20 -05:00
|
|
|
command_history_add(history, text);
|
2000-04-26 04:03:38 -04:00
|
|
|
}
|
2001-12-20 08:29:20 -05:00
|
|
|
return history->pos == NULL ? "" : history->pos->data;
|
|
|
|
}
|
|
|
|
|
|
|
|
void command_history_clear_pos_func(HISTORY_REC *history, gpointer user_data)
|
|
|
|
{
|
|
|
|
history->over_counter = 0;
|
|
|
|
history->pos = NULL;
|
2000-04-26 04:03:38 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
void command_history_clear_pos(WINDOW_REC *window)
|
|
|
|
{
|
2001-12-20 08:29:20 -05:00
|
|
|
g_slist_foreach(histories,
|
|
|
|
(GFunc) command_history_clear_pos_func, NULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
HISTORY_REC *command_history_create(const char *name)
|
|
|
|
{
|
|
|
|
HISTORY_REC *rec;
|
|
|
|
|
|
|
|
rec = g_new0(HISTORY_REC, 1);
|
|
|
|
|
|
|
|
if (name != NULL)
|
|
|
|
rec->name = g_strdup(name);
|
|
|
|
|
|
|
|
histories = g_slist_append(histories, rec);
|
|
|
|
|
|
|
|
return rec;
|
|
|
|
}
|
|
|
|
|
|
|
|
void command_history_destroy(HISTORY_REC *history)
|
|
|
|
{
|
|
|
|
g_return_if_fail(history != NULL);
|
|
|
|
|
|
|
|
/* history->refcount should be 0 here, or somthing is wrong... */
|
|
|
|
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);
|
|
|
|
|
|
|
|
g_free_not_null(history->name);
|
|
|
|
g_free(history);
|
|
|
|
}
|
|
|
|
|
|
|
|
void command_history_link(const char *name)
|
|
|
|
{
|
|
|
|
HISTORY_REC *rec;
|
|
|
|
rec = command_history_find_name(name);
|
|
|
|
|
|
|
|
if (rec == NULL)
|
|
|
|
rec = command_history_create(name);
|
|
|
|
|
|
|
|
rec->refcount++;
|
|
|
|
}
|
|
|
|
|
|
|
|
void command_history_unlink(const char *name)
|
|
|
|
{
|
|
|
|
HISTORY_REC *rec;
|
|
|
|
rec = command_history_find_name(name);
|
|
|
|
|
|
|
|
if (rec == NULL)
|
|
|
|
return;
|
|
|
|
|
|
|
|
if (--(rec->refcount) <= 0)
|
|
|
|
command_history_destroy(rec);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void sig_window_created(WINDOW_REC *window, int automatic)
|
|
|
|
{
|
|
|
|
window->history = command_history_create(NULL);
|
2000-04-26 04:03:38 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
static void sig_window_destroyed(WINDOW_REC *window)
|
|
|
|
{
|
2001-12-20 08:29:20 -05:00
|
|
|
command_history_unlink(window->history_name);
|
|
|
|
command_history_destroy(window->history);
|
|
|
|
g_free_not_null(window->history_name);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void sig_window_history_changed(WINDOW_REC *window, const char *oldname)
|
|
|
|
{
|
|
|
|
command_history_link(window->history_name);
|
|
|
|
command_history_unlink(oldname);
|
2000-04-26 04:03:38 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
static char *special_history_func(const char *text, void *item, int *free_ret)
|
|
|
|
{
|
|
|
|
WINDOW_REC *window;
|
2001-12-20 08:29:20 -05:00
|
|
|
HISTORY_REC *history;
|
2000-04-26 04:03:38 -04:00
|
|
|
GList *tmp;
|
|
|
|
char *findtext, *ret;
|
|
|
|
|
|
|
|
window = item == NULL ? active_win : window_item_window(item);
|
|
|
|
|
|
|
|
findtext = g_strdup_printf("*%s*", text);
|
|
|
|
ret = NULL;
|
|
|
|
|
2001-12-20 08:29:20 -05:00
|
|
|
history = command_history_current(window);
|
|
|
|
for (tmp = history->list; tmp != NULL; tmp = tmp->next) {
|
2000-04-26 04:03:38 -04:00
|
|
|
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)
|
|
|
|
{
|
2000-05-04 06:32:42 -04:00
|
|
|
window_history = settings_get_bool("window_history");
|
2000-04-26 04:03:38 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
void command_history_init(void)
|
|
|
|
{
|
|
|
|
settings_add_int("history", "max_command_history", 100);
|
2000-05-04 06:32:42 -04:00
|
|
|
settings_add_bool("history", "window_history", FALSE);
|
2000-04-26 04:03:38 -04:00
|
|
|
|
|
|
|
special_history_func_set(special_history_func);
|
|
|
|
|
2001-12-20 08:29:20 -05:00
|
|
|
global_history = command_history_create(NULL);
|
2001-03-07 19:45:56 -05:00
|
|
|
|
2000-04-26 04:03:38 -04:00
|
|
|
read_settings();
|
2001-12-20 08:29:20 -05:00
|
|
|
signal_add("window created", (SIGNAL_FUNC) sig_window_created);
|
2000-04-26 04:03:38 -04:00
|
|
|
signal_add("window destroyed", (SIGNAL_FUNC) sig_window_destroyed);
|
2001-12-20 08:29:20 -05:00
|
|
|
signal_add("window history changed", (SIGNAL_FUNC) sig_window_history_changed);
|
2000-04-26 04:03:38 -04:00
|
|
|
signal_add("setup changed", (SIGNAL_FUNC) read_settings);
|
|
|
|
}
|
|
|
|
|
|
|
|
void command_history_deinit(void)
|
|
|
|
{
|
2001-12-20 08:29:20 -05:00
|
|
|
signal_remove("window created", (SIGNAL_FUNC) sig_window_created);
|
2000-04-26 04:03:38 -04:00
|
|
|
signal_remove("window destroyed", (SIGNAL_FUNC) sig_window_destroyed);
|
2001-12-20 08:29:20 -05:00
|
|
|
signal_remove("window history changed", (SIGNAL_FUNC) sig_window_history_changed);
|
2000-04-26 04:03:38 -04:00
|
|
|
signal_remove("setup changed", (SIGNAL_FUNC) read_settings);
|
|
|
|
|
2001-12-20 08:29:20 -05:00
|
|
|
command_history_destroy(global_history);
|
2000-04-26 04:03:38 -04:00
|
|
|
}
|