4 Copyright (C) 1999-2001 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 along
17 with this program; if not, write to the Free Software Foundation, Inc.,
18 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
28 #include "printtext.h"
31 static int commands_equal(COMMAND_REC *rec, COMMAND_REC *rec2)
35 if (rec->category == NULL && rec2->category != NULL)
37 if (rec2->category == NULL && rec->category != NULL)
39 if (rec->category != NULL && rec2->category != NULL) {
40 i = strcmp(rec->category, rec2->category);
45 return strcmp(rec->cmd, rec2->cmd);
48 static int get_cmd_length(void *data)
50 return strlen(((COMMAND_REC *) data)->cmd);
53 static void help_category(GSList *cmdlist, int items)
59 int *columns, cols, rows, col, row, last_col_rows, max_width;
60 char *linebuf, *format, *stripped;
62 window = window_find_closest(NULL, NULL, MSGLEVEL_CLIENTCRAP);
63 max_width = window->width;
65 /* remove width of timestamp from max_width */
66 format_create_dest(&dest, NULL, NULL, MSGLEVEL_CLIENTCRAP, NULL);
67 format = format_get_line_start(current_theme, &dest, time(NULL));
69 stripped = strip_codes(format);
70 max_width -= strlen(stripped);
75 /* calculate columns */
76 cols = get_max_column_count(cmdlist, get_cmd_length,
77 max_width, 6, 1, 3, &columns, &rows);
78 cmdlist = columns_sort_list(cmdlist, rows);
80 /* rows in last column */
81 last_col_rows = rows-(cols*rows-g_slist_length(cmdlist));
82 if (last_col_rows == 0)
85 str = g_string_new(NULL);
86 linebuf = g_malloc(max_width+1);
89 for (tmp = cmdlist; tmp != NULL; tmp = tmp->next) {
90 COMMAND_REC *rec = tmp->data;
92 memset(linebuf, ' ', columns[col]);
93 linebuf[columns[col]] = '\0';
94 memcpy(linebuf, rec->cmd, strlen(rec->cmd));
95 g_string_append(str, linebuf);
99 MSGLEVEL_CLIENTCRAP, "%s", str->str);
100 g_string_truncate(str, 0);
103 if (row == last_col_rows)
108 printtext(NULL, NULL, MSGLEVEL_CLIENTCRAP, "%s", str->str);
110 g_slist_free(cmdlist);
111 g_string_free(str, TRUE);
116 static int show_help_file(const char *file)
118 const char *helppath;
119 char *path, **paths, **tmp;
124 helppath = settings_get_str("help_path");
126 paths = g_strsplit(helppath, ":", -1);
129 for (tmp = paths; *tmp != NULL; tmp++) {
130 /* helpdir/command or helpdir/category/command */
131 path = g_strdup_printf("%s/%s", *tmp, file);
132 handle = g_io_channel_new_file(path, "r", NULL);
145 g_io_channel_set_encoding(handle, NULL, NULL);
146 buf = g_string_sized_new(512);
147 /* just print to screen whatever is in the file */
148 while (g_io_channel_read_line_string(handle, buf, &tpos, NULL) == G_IO_STATUS_NORMAL) {
149 buf->str[tpos] = '\0';
150 g_string_prepend(buf, "%|");
151 printtext_string(NULL, NULL, MSGLEVEL_CLIENTCRAP, buf->str);
153 g_string_free(buf, TRUE);
155 g_io_channel_unref(handle);
159 static void show_help(const char *data)
161 COMMAND_REC *rec, *last;
162 GSList *tmp, *cmdlist;
164 int header, found, fullmatch;
166 g_return_if_fail(data != NULL);
168 /* sort the commands list */
169 commands = g_slist_sort(commands, (GCompareFunc) commands_equal);
171 /* print command, sort by category */
172 cmdlist = NULL; last = NULL; header = FALSE; fullmatch = FALSE;
173 items = 0; findlen = strlen(data); found = FALSE;
174 for (tmp = commands; tmp != NULL; last = rec, tmp = tmp->next) {
177 if (last != NULL && rec->category != NULL &&
178 (last->category == NULL ||
179 strcmp(rec->category, last->category) != 0)) {
180 /* category changed */
183 printtext(NULL, NULL, MSGLEVEL_CLIENTCRAP, "Irssi commands:");
186 if (last->category != NULL) {
187 printtext(NULL, NULL, MSGLEVEL_CLIENTCRAP, "");
188 printtext(NULL, NULL, MSGLEVEL_CLIENTCRAP, "%s:", last->category);
190 help_category(cmdlist, items);
193 g_slist_free(cmdlist); cmdlist = NULL;
197 if (last != NULL && g_strcasecmp(rec->cmd, last->cmd) == 0)
198 continue; /* don't display same command twice */
200 if ((int)strlen(rec->cmd) >= findlen &&
201 g_strncasecmp(rec->cmd, data, findlen) == 0) {
202 if (rec->cmd[findlen] == '\0') {
207 else if (strchr(rec->cmd+findlen+1, ' ') == NULL) {
208 /* not a subcommand (and matches the query) */
210 cmdlist = g_slist_append(cmdlist, rec);
216 if ((!found || fullmatch) && !show_help_file(data)) {
217 printtext(NULL, NULL, MSGLEVEL_CLIENTCRAP,
218 "No help for %s", data);
221 if (*data != '\0' && data[strlen(data)-1] != ' ' &&
222 command_have_sub(data)) {
225 cmd = g_strconcat(data, " ", NULL);
231 /* display the last category */
233 printtext(NULL, NULL, MSGLEVEL_CLIENTCRAP,
238 if (last->category != NULL) {
239 printtext(NULL, NULL, MSGLEVEL_CLIENTCRAP, "");
240 printtext(NULL, NULL, MSGLEVEL_CLIENTCRAP,
241 "%s:", last->category);
243 help_category(cmdlist, items);
244 g_slist_free(cmdlist);
248 /* SYNTAX: HELP [<command>] */
249 static void cmd_help(const char *data)
253 cmd = g_ascii_strdown(data, -1);
259 void fe_help_init(void)
261 settings_add_str("misc", "help_path", HELPDIR);
262 command_bind("help", NULL, (SIGNAL_FUNC) cmd_help);
265 void fe_help_deinit(void)
267 command_unbind("help", (SIGNAL_FUNC) cmd_help);