Merge Irssi 0.8.16-rc1
[silc.git] / apps / irssi / src / fe-common / core / fe-help.c
1 /*
2  fe-help.c : irssi
3
4     Copyright (C) 1999-2001 Timo Sirainen
5
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.
10
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.
15
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.
19 */
20
21 #include "module.h"
22 #include "signals.h"
23 #include "commands.h"
24 #include "levels.h"
25 #include "misc.h"
26 #include "settings.h"
27
28 #include "printtext.h"
29 #include "formats.h"
30
31 static int commands_equal(COMMAND_REC *rec, COMMAND_REC *rec2)
32 {
33         int i;
34
35         if (rec->category == NULL && rec2->category != NULL)
36                 return -1;
37         if (rec2->category == NULL && rec->category != NULL)
38                 return 1;
39         if (rec->category != NULL && rec2->category != NULL) {
40                 i = strcmp(rec->category, rec2->category);
41                 if (i != 0)
42                         return i;
43         }
44
45         return strcmp(rec->cmd, rec2->cmd);
46 }
47
48 static int get_cmd_length(void *data)
49 {
50         return strlen(((COMMAND_REC *) data)->cmd);
51 }
52
53 static void help_category(GSList *cmdlist, int items)
54 {
55         WINDOW_REC *window;
56         TEXT_DEST_REC dest;
57         GString *str;
58         GSList *tmp;
59         int *columns, cols, rows, col, row, last_col_rows, max_width;
60         char *linebuf, *format, *stripped;
61
62         window = window_find_closest(NULL, NULL, MSGLEVEL_CLIENTCRAP);
63         max_width = window->width;
64
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));
68         if (format != NULL) {
69                 stripped = strip_codes(format);
70                 max_width -= strlen(stripped);
71                 g_free(stripped);
72                 g_free(format);
73         }
74
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);
79
80         /* rows in last column */
81         last_col_rows = rows-(cols*rows-g_slist_length(cmdlist));
82         if (last_col_rows == 0)
83                 last_col_rows = rows;
84
85         str = g_string_new(NULL);
86         linebuf = g_malloc(max_width+1);
87
88         col = 0; row = 0;
89         for (tmp = cmdlist; tmp != NULL; tmp = tmp->next) {
90                 COMMAND_REC *rec = tmp->data;
91
92                 memset(linebuf, ' ', columns[col]);
93                 linebuf[columns[col]] = '\0';
94                 memcpy(linebuf, rec->cmd, strlen(rec->cmd));
95                 g_string_append(str, linebuf);
96
97                 if (++col == cols) {
98                         printtext(NULL, NULL,
99                                   MSGLEVEL_CLIENTCRAP, "%s", str->str);
100                         g_string_truncate(str, 0);
101                         col = 0; row++;
102
103                         if (row == last_col_rows)
104                                 cols--;
105                 }
106         }
107         if (str->len != 0)
108                 printtext(NULL, NULL, MSGLEVEL_CLIENTCRAP, "%s", str->str);
109
110         g_slist_free(cmdlist);
111         g_string_free(str, TRUE);
112         g_free(columns);
113         g_free(linebuf);
114 }
115
116 static int show_help_file(const char *file)
117 {
118         const char *helppath;
119         char *path, **paths, **tmp;
120         GIOChannel *handle;
121         GString *buf;
122         gsize tpos;
123
124         helppath = settings_get_str("help_path");
125
126         paths = g_strsplit(helppath, ":", -1);
127
128         handle = NULL;
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);
133                 g_free(path);
134
135                 if (handle != NULL)
136                         break;
137
138         }
139
140         g_strfreev(paths);
141
142         if (handle == NULL)
143                 return FALSE;
144
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);
152         }
153         g_string_free(buf, TRUE);
154
155         g_io_channel_unref(handle);
156         return TRUE;
157 }
158
159 static void show_help(const char *data)
160 {
161         COMMAND_REC *rec, *last;
162         GSList *tmp, *cmdlist;
163         int items, findlen;
164         int header, found, fullmatch;
165
166         g_return_if_fail(data != NULL);
167
168         /* sort the commands list */
169         commands = g_slist_sort(commands, (GCompareFunc) commands_equal);
170
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) {
175                 rec = tmp->data;
176
177                 if (last != NULL && rec->category != NULL &&
178                     (last->category == NULL ||
179                      strcmp(rec->category, last->category) != 0)) {
180                         /* category changed */
181                         if (items > 0) {
182                                 if (!header) {
183                                         printtext(NULL, NULL, MSGLEVEL_CLIENTCRAP, "Irssi commands:");
184                                         header = TRUE;
185                                 }
186                                 if (last->category != NULL) {
187                                         printtext(NULL, NULL, MSGLEVEL_CLIENTCRAP, "");
188                                         printtext(NULL, NULL, MSGLEVEL_CLIENTCRAP, "%s:", last->category);
189                                 }
190                                 help_category(cmdlist, items);
191                         }
192
193                         g_slist_free(cmdlist); cmdlist = NULL;
194                         items = 0;
195                 }
196
197                 if (last != NULL && g_strcasecmp(rec->cmd, last->cmd) == 0)
198                         continue; /* don't display same command twice */
199
200                 if ((int)strlen(rec->cmd) >= findlen &&
201                     g_strncasecmp(rec->cmd, data, findlen) == 0) {
202                         if (rec->cmd[findlen] == '\0') {
203                                 fullmatch = TRUE;
204                                 found = TRUE;
205                                 break;
206                         }
207                         else if (strchr(rec->cmd+findlen+1, ' ') == NULL) {
208                                 /* not a subcommand (and matches the query) */
209                                 items++;
210                                 cmdlist = g_slist_append(cmdlist, rec);
211                                 found = TRUE;
212                         }
213                 }
214         }
215
216         if ((!found || fullmatch) && !show_help_file(data)) {
217                 printtext(NULL, NULL, MSGLEVEL_CLIENTCRAP,
218                           "No help for %s", data);
219         }
220
221         if (*data != '\0' && data[strlen(data)-1] != ' ' &&
222             command_have_sub(data)) {
223                 char *cmd;
224
225                 cmd = g_strconcat(data, " ", NULL);
226                 show_help(cmd);
227                 g_free(cmd);
228         }
229
230         if (items != 0) {
231                 /* display the last category */
232                 if (!header) {
233                         printtext(NULL, NULL, MSGLEVEL_CLIENTCRAP,
234                                   "Irssi commands:");
235                         header = TRUE;
236                 }
237
238                 if (last->category != NULL) {
239                         printtext(NULL, NULL, MSGLEVEL_CLIENTCRAP, "");
240                         printtext(NULL, NULL, MSGLEVEL_CLIENTCRAP,
241                                   "%s:", last->category);
242                 }
243                 help_category(cmdlist, items);
244                 g_slist_free(cmdlist);
245         }
246 }
247
248 /* SYNTAX: HELP [<command>] */
249 static void cmd_help(const char *data)
250 {
251         char *cmd;
252
253         cmd = g_ascii_strdown(data, -1);
254         g_strchomp(cmd);
255         show_help(cmd);
256         g_free(cmd);
257 }
258
259 void fe_help_init(void)
260 {
261         settings_add_str("misc", "help_path", HELPDIR);
262         command_bind("help", NULL, (SIGNAL_FUNC) cmd_help);
263 }
264
265 void fe_help_deinit(void)
266 {
267         command_unbind("help", (SIGNAL_FUNC) cmd_help);
268 }