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
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
26 #include "line-split.h"
29 #include "printtext.h"
32 static int commands_equal(COMMAND_REC *rec, COMMAND_REC *rec2)
36 if (rec->category == NULL && rec2->category != NULL)
38 if (rec2->category == NULL && rec->category != NULL)
40 if (rec->category != NULL && rec2->category != NULL) {
41 i = strcmp(rec->category, rec2->category);
46 return strcmp(rec->cmd, rec2->cmd);
49 static int get_cmd_length(void *data)
51 return strlen(((COMMAND_REC *) data)->cmd);
54 static void help_category(GSList *cmdlist, int items)
60 int *columns, cols, rows, col, row, last_col_rows, max_width;
61 char *linebuf, *format, *stripped;
63 window = window_find_closest(NULL, NULL, MSGLEVEL_CLIENTCRAP);
64 max_width = window->width;
66 /* remove width of timestamp from max_width */
67 format_create_dest(&dest, NULL, NULL, MSGLEVEL_CLIENTCRAP, NULL);
68 format = format_get_line_start(current_theme, &dest, time(NULL));
70 stripped = strip_codes(format);
71 max_width -= strlen(stripped);
76 /* calculate columns */
77 cols = get_max_column_count(cmdlist, get_cmd_length,
78 max_width, 6, 1, 3, &columns, &rows);
79 cmdlist = columns_sort_list(cmdlist, rows);
81 /* rows in last column */
82 last_col_rows = rows-(cols*rows-g_slist_length(cmdlist));
83 if (last_col_rows == 0)
86 str = g_string_new(NULL);
87 linebuf = g_malloc(max_width+1);
90 for (tmp = cmdlist; tmp != NULL; tmp = tmp->next) {
91 COMMAND_REC *rec = tmp->data;
93 memset(linebuf, ' ', columns[col]);
94 linebuf[columns[col]] = '\0';
95 memcpy(linebuf, rec->cmd, strlen(rec->cmd));
96 g_string_append(str, linebuf);
100 MSGLEVEL_CLIENTCRAP, "%s", str->str);
101 g_string_truncate(str, 0);
104 if (row == last_col_rows)
109 printtext(NULL, NULL, MSGLEVEL_CLIENTCRAP, "%s", str->str);
111 g_slist_free(cmdlist);
112 g_string_free(str, TRUE);
117 static int show_help_file(const char *file)
119 const char *helppath;
120 char tmpbuf[1024], *str, *path;
121 LINEBUF_REC *buffer = NULL;
124 helppath = settings_get_str("help_path");
126 /* helpdir/command or helpdir/category/command */
127 path = g_strdup_printf("%s/%s", helppath, file);
128 f = open(path, O_RDONLY);
134 /* just print to screen whatever is in the file */
136 recvlen = read(f, tmpbuf, sizeof(tmpbuf));
138 ret = line_split(tmpbuf, recvlen, &str, &buffer);
140 str = g_strconcat("%|", str, NULL);
141 printtext_string(NULL, NULL, MSGLEVEL_CLIENTCRAP, str);
146 line_split_free(buffer);
152 static void show_help(const char *data)
154 COMMAND_REC *rec, *last;
155 GSList *tmp, *cmdlist;
157 int header, found, fullmatch;
159 g_return_if_fail(data != NULL);
161 /* sort the commands list */
162 commands = g_slist_sort(commands, (GCompareFunc) commands_equal);
164 /* print command, sort by category */
165 cmdlist = NULL; last = NULL; header = FALSE; fullmatch = FALSE;
166 items = 0; findlen = strlen(data); found = FALSE;
167 for (tmp = commands; tmp != NULL; last = rec, tmp = tmp->next) {
170 if (last != NULL && rec->category != NULL &&
171 (last->category == NULL ||
172 strcmp(rec->category, last->category) != 0)) {
173 /* category changed */
176 printtext(NULL, NULL, MSGLEVEL_CLIENTCRAP, "Irssi commands:");
179 if (last->category != NULL) {
180 printtext(NULL, NULL, MSGLEVEL_CLIENTCRAP, "");
181 printtext(NULL, NULL, MSGLEVEL_CLIENTCRAP, "%s:", last->category);
183 help_category(cmdlist, items);
186 g_slist_free(cmdlist); cmdlist = NULL;
190 if (last != NULL && g_strcasecmp(rec->cmd, last->cmd) == 0)
191 continue; /* don't display same command twice */
193 if ((int)strlen(rec->cmd) >= findlen &&
194 g_strncasecmp(rec->cmd, data, findlen) == 0) {
195 if (rec->cmd[findlen] == '\0') {
200 else if (strchr(rec->cmd+findlen+1, ' ') == NULL) {
201 /* not a subcommand (and matches the query) */
203 cmdlist = g_slist_append(cmdlist, rec);
209 if ((!found || fullmatch) && !show_help_file(data)) {
210 printtext(NULL, NULL, MSGLEVEL_CLIENTCRAP,
211 "No help for %s", data);
214 if (*data != '\0' && data[strlen(data)-1] != ' ' &&
215 command_have_sub(data)) {
218 cmd = g_strconcat(data, " ", NULL);
224 /* display the last category */
226 printtext(NULL, NULL, MSGLEVEL_CLIENTCRAP,
231 if (last->category != NULL) {
232 printtext(NULL, NULL, MSGLEVEL_CLIENTCRAP, "");
233 printtext(NULL, NULL, MSGLEVEL_CLIENTCRAP,
234 "%s:", last->category);
236 help_category(cmdlist, items);
237 g_slist_free(cmdlist);
241 /* SYNTAX: HELP [<command>] */
242 static void cmd_help(const char *data)
246 cmd = g_strdup(data);
247 ptr = cmd+strlen(cmd);
248 while (ptr[-1] == ' ') ptr--; *ptr = '\0';
254 void fe_help_init(void)
256 settings_add_str("misc", "help_path", HELPDIR);
257 command_bind("help", NULL, (SIGNAL_FUNC) cmd_help);
260 void fe_help_deinit(void)
262 command_unbind("help", (SIGNAL_FUNC) cmd_help);