summaryrefslogtreecommitdiff
path: root/src/fe-common/core/fe-help.c
blob: c2b1f596316d369a029ac9135c04120288d0090a (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
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
/*
 fe-help.c : irssi

    Copyright (C) 1999-2001 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.,
    51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/

#include "module.h"
#include "signals.h"
#include "commands.h"
#include "levels.h"
#include "misc.h"
#include "settings.h"

#include "printtext.h"
#include "formats.h"

static int commands_equal(COMMAND_REC *rec, COMMAND_REC *rec2)
{
	int i;

	if (rec->category == NULL && rec2->category != NULL)
		return -1;
	if (rec2->category == NULL && rec->category != NULL)
		return 1;
	if (rec->category != NULL && rec2->category != NULL) {
		i = strcmp(rec->category, rec2->category);
		if (i != 0)
			return i;
	}

	return strcmp(rec->cmd, rec2->cmd);
}

static int get_cmd_length(void *data)
{
        return strlen(((COMMAND_REC *) data)->cmd);
}

static void help_category(GSList *cmdlist, int items)
{
        WINDOW_REC *window;
	TEXT_DEST_REC dest;
	GString *str;
	GSList *tmp;
	int *columns, cols, rows, col, row, last_col_rows, max_width;
	char *linebuf, *format, *stripped;

	window = window_find_closest(NULL, NULL, MSGLEVEL_CLIENTCRAP);
        max_width = window->width;

        /* remove width of timestamp from max_width */
	format_create_dest(&dest, NULL, NULL, MSGLEVEL_CLIENTCRAP, NULL);
	format = format_get_line_start(current_theme, &dest, time(NULL));
	if (format != NULL) {
		stripped = strip_codes(format);
		max_width -= strlen(stripped);
		g_free(stripped);
		g_free(format);
	}

        /* calculate columns */
	cols = get_max_column_count(cmdlist, get_cmd_length,
				    max_width, 6, 1, 3, &columns, &rows);
	cmdlist = columns_sort_list(cmdlist, rows);

        /* rows in last column */
	last_col_rows = rows-(cols*rows-g_slist_length(cmdlist));
	if (last_col_rows == 0)
                last_col_rows = rows;

	str = g_string_new(NULL);
	linebuf = g_malloc(max_width+1);

        col = 0; row = 0;
	for (tmp = cmdlist; tmp != NULL; tmp = tmp->next) {
		COMMAND_REC *rec = tmp->data;

		memset(linebuf, ' ', columns[col]);
		linebuf[columns[col]] = '\0';
		memcpy(linebuf, rec->cmd, strlen(rec->cmd));
		g_string_append(str, linebuf);

		if (++col == cols) {
			printtext(NULL, NULL,
				  MSGLEVEL_CLIENTCRAP, "%s", str->str);
			g_string_truncate(str, 0);
			col = 0; row++;

			if (row == last_col_rows)
                                cols--;
		}
	}
	if (str->len != 0)
		printtext(NULL, NULL, MSGLEVEL_CLIENTCRAP, "%s", str->str);

	g_slist_free(cmdlist);
	g_string_free(str, TRUE);
	g_free(columns);
	g_free(linebuf);
}

static int show_help_file(const char *file)
{
        const char *helppath;
	char *path, **paths, **tmp;
	GIOChannel *handle;
	GString *buf;
	gsize tpos;

        helppath = settings_get_str("help_path");

	paths = g_strsplit(helppath, ":", -1);

	handle = NULL;
	for (tmp = paths; *tmp != NULL; tmp++) {
		/* helpdir/command or helpdir/category/command */
		path = g_strdup_printf("%s/%s", *tmp, file);
		handle = g_io_channel_new_file(path, "r", NULL);
		g_free(path);

		if (handle != NULL)
			break;

	}

	g_strfreev(paths);

	if (handle == NULL)
		return FALSE;

	buf = g_string_sized_new(512);
	/* just print to screen whatever is in the file */
	while (g_io_channel_read_line_string(handle, buf, &tpos, NULL) == G_IO_STATUS_NORMAL) {
		buf->str[tpos] = '\0';
		g_string_prepend(buf, "%|");
		printtext_string(NULL, NULL, MSGLEVEL_CLIENTCRAP, buf->str);
	}
	g_string_free(buf, TRUE);

	g_io_channel_close(handle);
	return TRUE;
}

static void show_help(const char *data)
{
	COMMAND_REC *rec, *last;
	GSList *tmp, *cmdlist;
	int items, findlen;
	int header, found, fullmatch;

	g_return_if_fail(data != NULL);

	/* sort the commands list */
	commands = g_slist_sort(commands, (GCompareFunc) commands_equal);

	/* print command, sort by category */
	cmdlist = NULL; last = NULL; header = FALSE; fullmatch = FALSE;
	items = 0; findlen = strlen(data); found = FALSE;
	for (tmp = commands; tmp != NULL; last = rec, tmp = tmp->next) {
		rec = tmp->data;

		if (last != NULL && rec->category != NULL &&
		    (last->category == NULL ||
		     strcmp(rec->category, last->category) != 0)) {
			/* category changed */
			if (items > 0) {
				if (!header) {
					printtext(NULL, NULL, MSGLEVEL_CLIENTCRAP, "Irssi commands:");
					header = TRUE;
				}
				if (last->category != NULL) {
					printtext(NULL, NULL, MSGLEVEL_CLIENTCRAP, "");
					printtext(NULL, NULL, MSGLEVEL_CLIENTCRAP, "%s:", last->category);
				}
				help_category(cmdlist, items);
			}

			g_slist_free(cmdlist); cmdlist = NULL;
			items = 0;
		}

		if (last != NULL && g_strcasecmp(rec->cmd, last->cmd) == 0)
			continue; /* don't display same command twice */

		if ((int)strlen(rec->cmd) >= findlen &&
		    g_strncasecmp(rec->cmd, data, findlen) == 0) {
			if (rec->cmd[findlen] == '\0') {
				fullmatch = TRUE;
				found = TRUE;
				break;
			}
			else if (strchr(rec->cmd+findlen+1, ' ') == NULL) {
				/* not a subcommand (and matches the query) */
				items++;
				cmdlist = g_slist_append(cmdlist, rec);
				found = TRUE;
			}
		}
	}

	if ((!found || fullmatch) && !show_help_file(data)) {
		printtext(NULL, NULL, MSGLEVEL_CLIENTCRAP,
			  "No help for %s", data);
	}

	if (*data != '\0' && data[strlen(data)-1] != ' ' &&
	    command_have_sub(data)) {
		char *cmd;

		cmd = g_strconcat(data, " ", NULL);
		show_help(cmd);
		g_free(cmd);
	}

	if (items != 0) {
		/* display the last category */
		if (!header) {
			printtext(NULL, NULL, MSGLEVEL_CLIENTCRAP,
				  "Irssi commands:");
			header = TRUE;
		}

		if (last->category != NULL) {
			printtext(NULL, NULL, MSGLEVEL_CLIENTCRAP, "");
			printtext(NULL, NULL, MSGLEVEL_CLIENTCRAP,
				  "%s:", last->category);
		}
		help_category(cmdlist, items);
		g_slist_free(cmdlist);
	}
}

/* SYNTAX: HELP [<command>] */
static void cmd_help(const char *data)
{
	char *cmd;

	cmd = g_ascii_strdown(data, -1);
	g_strchomp(cmd);
	show_help(cmd);
        g_free(cmd);
}

void fe_help_init(void)
{
        settings_add_str("misc", "help_path", HELPDIR);
	command_bind("help", NULL, (SIGNAL_FUNC) cmd_help);
}

void fe_help_deinit(void)
{
	command_unbind("help", (SIGNAL_FUNC) cmd_help);
}