imported.
[runtime.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
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
19 */
20
21 #include "module.h"
22 #include "signals.h"
23 #include "commands.h"
24 #include "levels.h"
25 #include "misc.h"
26 #include "line-split.h"
27 #include "settings.h"
28
29 #include "printtext.h"
30 #include "formats.h"
31
32 static int commands_equal(COMMAND_REC *rec, COMMAND_REC *rec2)
33 {
34         if (rec->category == NULL && rec2->category != NULL)
35                 return -1;
36         if (rec2->category == NULL && rec->category != NULL)
37                 return 1;
38
39         return strcmp(rec->cmd, rec2->cmd);
40 }
41
42 static int get_cmd_length(void *data)
43 {
44         return strlen(((COMMAND_REC *) data)->cmd);
45 }
46
47 static void help_category(GSList *cmdlist, int items)
48 {
49         WINDOW_REC *window;
50         TEXT_DEST_REC dest;
51         GString *str;
52         GSList *tmp;
53         int *columns, cols, rows, col, row, last_col_rows, max_width;
54         char *linebuf, *format, *stripped;
55
56         window = window_find_closest(NULL, NULL, MSGLEVEL_CLIENTCRAP);
57         max_width = window->width;
58
59         /* remove width of timestamp from max_width */
60         format_create_dest(&dest, NULL, NULL, MSGLEVEL_CLIENTCRAP, NULL);
61         format = format_get_line_start(current_theme, &dest, time(NULL));
62         if (format != NULL) {
63                 stripped = strip_codes(format);
64                 max_width -= strlen(stripped);
65                 g_free(stripped);
66                 g_free(format);
67         }
68
69         /* calculate columns */
70         cols = get_max_column_count(cmdlist, get_cmd_length,
71                                     max_width, 6, 1, 3, &columns, &rows);
72         cmdlist = columns_sort_list(cmdlist, rows);
73
74         /* rows in last column */
75         last_col_rows = rows-(cols*rows-g_slist_length(cmdlist));
76         if (last_col_rows == 0)
77                 last_col_rows = rows;
78
79         str = g_string_new(NULL);
80         linebuf = g_malloc(max_width+1);
81
82         col = 0; row = 0;
83         for (tmp = cmdlist; tmp != NULL; tmp = tmp->next) {
84                 COMMAND_REC *rec = tmp->data;
85
86                 memset(linebuf, ' ', columns[col]);
87                 linebuf[columns[col]] = '\0';
88                 memcpy(linebuf, rec->cmd, strlen(rec->cmd));
89                 g_string_append(str, linebuf);
90
91                 if (++col == cols) {
92                         printtext(NULL, NULL,
93                                   MSGLEVEL_CLIENTCRAP, "%s", str->str);
94                         g_string_truncate(str, 0);
95                         col = 0; row++;
96
97                         if (row == last_col_rows)
98                                 cols--;
99                 }
100         }
101         if (str->len != 0)
102                 printtext(NULL, NULL, MSGLEVEL_CLIENTCRAP, "%s", str->str);
103
104         g_slist_free(cmdlist);
105         g_string_free(str, TRUE);
106         g_free(columns);
107         g_free(linebuf);
108 }
109
110 static int show_help_file(const char *file)
111 {
112         const char *helppath;
113         char tmpbuf[1024], *str, *path;
114         LINEBUF_REC *buffer = NULL;
115         int f, ret, recvlen;
116
117         helppath = settings_get_str("help_path");
118
119         /* helpdir/command or helpdir/category/command */
120         path = g_strdup_printf("%s/%s", helppath, file);
121         f = open(path, O_RDONLY);
122         g_free(path);
123
124         if (f == -1)
125                 return FALSE;
126
127         /* just print to screen whatever is in the file */
128         do {
129                 recvlen = read(f, tmpbuf, sizeof(tmpbuf));
130
131                 ret = line_split(tmpbuf, recvlen, &str, &buffer);
132                 if (ret > 0) {
133                         str = g_strconcat("%|", str, NULL);
134                         printtext_string(NULL, NULL, MSGLEVEL_CLIENTCRAP, str);
135                         g_free(str);
136                 }
137         }
138         while (ret > 0);
139         line_split_free(buffer);
140
141         close(f);
142         return TRUE;
143 }
144
145 static void show_help(const char *data)
146 {
147         COMMAND_REC *rec, *last;
148         GSList *tmp, *cmdlist;
149         int items, findlen;
150         int header, found, fullmatch;
151
152         g_return_if_fail(data != NULL);
153
154         /* sort the commands list */
155         commands = g_slist_sort(commands, (GCompareFunc) commands_equal);
156
157         /* print command, sort by category */
158         cmdlist = NULL; last = NULL; header = FALSE; fullmatch = FALSE;
159         items = 0; findlen = strlen(data); found = FALSE;
160         for (tmp = commands; tmp != NULL; last = rec, tmp = tmp->next) {
161                 rec = tmp->data;
162
163                 if (last != NULL && rec->category != NULL &&
164                     (last->category == NULL ||
165                      strcmp(rec->category, last->category) != 0)) {
166                         /* category changed */
167                         if (items > 0) {
168                                 if (!header) {
169                                         printtext(NULL, NULL, MSGLEVEL_CLIENTCRAP, "Irssi commands:");
170                                         header = TRUE;
171                                 }
172                                 if (last->category != NULL) {
173                                         printtext(NULL, NULL, MSGLEVEL_CLIENTCRAP, "");
174                                         printtext(NULL, NULL, MSGLEVEL_CLIENTCRAP, "%s:", last->category);
175                                 }
176                                 help_category(cmdlist, items);
177                         }
178
179                         g_slist_free(cmdlist); cmdlist = NULL;
180                         items = 0;
181                 }
182
183                 if (last != NULL && g_strcasecmp(rec->cmd, last->cmd) == 0)
184                         continue; /* don't display same command twice */
185
186                 if ((int)strlen(rec->cmd) >= findlen &&
187                     g_strncasecmp(rec->cmd, data, findlen) == 0) {
188                         if (rec->cmd[findlen] == '\0') {
189                                 fullmatch = TRUE;
190                                 found = TRUE;
191                                 break;
192                         }
193                         else if (strchr(rec->cmd+findlen+1, ' ') == NULL) {
194                                 /* not a subcommand (and matches the query) */
195                                 items++;
196                                 cmdlist = g_slist_append(cmdlist, rec);
197                                 found = TRUE;
198                         }
199                 }
200         }
201
202         if ((!found || fullmatch) && !show_help_file(data)) {
203                 printtext(NULL, NULL, MSGLEVEL_CLIENTCRAP,
204                           "No help for %s", data);
205         }
206
207         if (*data != '\0' && data[strlen(data)-1] != ' ' &&
208             command_have_sub(data)) {
209                 char *cmd;
210
211                 cmd = g_strconcat(data, " ", NULL);
212                 show_help(cmd);
213                 g_free(cmd);
214         }
215
216         if (items != 0) {
217                 /* display the last category */
218                 if (!header) {
219                         printtext(NULL, NULL, MSGLEVEL_CLIENTCRAP,
220                                   "Irssi commands:");
221                         header = TRUE;
222                 }
223
224                 if (last->category != NULL) {
225                         printtext(NULL, NULL, MSGLEVEL_CLIENTCRAP, "");
226                         printtext(NULL, NULL, MSGLEVEL_CLIENTCRAP,
227                                   "%s:", last->category);
228                 }
229                 help_category(cmdlist, items);
230                 g_slist_free(cmdlist);
231         }
232 }
233
234 /* SYNTAX: HELP [<command>] */
235 static void cmd_help(const char *data)
236 {
237         char *cmd, *ptr;
238
239         cmd = g_strdup(data);
240         ptr = cmd+strlen(cmd);
241         while (ptr[-1] == ' ') ptr--; *ptr = '\0';
242
243         show_help(cmd);
244         g_free(cmd);
245 }
246
247 void fe_help_init(void)
248 {
249         settings_add_str("misc", "help_path", HELPDIR);
250         command_bind("help", NULL, (SIGNAL_FUNC) cmd_help);
251 }
252
253 void fe_help_deinit(void)
254 {
255         command_unbind("help", (SIGNAL_FUNC) cmd_help);
256 }