1 /* $OpenBSD: help.c,v 1.34 2012/04/12 04:47:59 lum Exp $ */
3 /* This file is in the public domain. */
6 * Help functions for Mg 2
16 static int showall(struct buffer *, KEYMAP *, char *);
17 static int findbind(KEYMAP *, PF, char *, size_t);
20 * Read a key from the keyboard, and look it up in the keymap.
21 * Display the name of the function currently bound to the key.
34 return (TRUE); /* ignore inside keyboard macro */
36 num = strlcpy(dprompt, "Describe key briefly: ", sizeof(dprompt));
37 if (num >= sizeof(dprompt))
38 num = sizeof(dprompt) - 1;
42 curmap = curbp->b_modes[m]->p_map;
45 ewprintf("%s", dprompt);
47 pep = getkeyname(pep, sizeof(dprompt) - (pep - dprompt),
48 key.k_chars[key.k_count++] = c = getkey(FALSE));
49 if ((funct = doscan(curmap, c, &curmap)) != NULL)
56 if (ISUPPER(key.k_chars[key.k_count - 1])) {
57 funct = doscan(curmap,
58 TOLOWER(key.k_chars[key.k_count - 1]), &curmap);
70 curmap = curbp->b_modes[m]->p_map;
71 for (i = 0; i < key.k_count; i++) {
72 funct = doscan(curmap, key.k_chars[i], &curmap);
74 if (i == key.k_count - 1 && funct != rescan)
84 if (funct == rescan || funct == selfinsert)
85 ewprintf("%k is not bound to any function");
86 else if ((pep = (char *)function_name(funct)) != NULL)
87 ewprintf("%k runs the command %s", pep);
89 ewprintf("%k is bound to an unnamed function");
94 * This function creates a table, listing all of the command
95 * keys and their current bindings, and stores the table in the
96 * *help* pop-up buffer. This lets Mg produce it's own wall chart.
100 wallchart(int f, int n)
105 bp = bfind("*help*", TRUE);
106 if (bclear(bp) != TRUE)
109 bp->b_flag |= BFREADONLY;
110 for (m = curbp->b_nmodes; m > 0; m--) {
111 if ((addlinef(bp, "Local keybindings for mode %s:",
112 curbp->b_modes[m]->p_name) == FALSE) ||
113 (showall(bp, curbp->b_modes[m]->p_map, "") == FALSE) ||
114 (addline(bp, "") == FALSE))
117 if ((addline(bp, "Global bindings:") == FALSE) ||
118 (showall(bp, fundamental_map, "") == FALSE))
120 return (popbuftop(bp, WNONE));
124 showall(struct buffer *bp, KEYMAP *map, char *prefix)
127 char buf[80], keybuf[16];
131 if (addline(bp, "") == FALSE)
135 for (c = 0; c < 256; c++) {
136 fun = doscan(map, c, &newmap);
137 if (fun == rescan || fun == selfinsert)
139 getkeyname(buf, sizeof(buf), c);
140 (void)snprintf(keybuf, sizeof(keybuf), "%s%s ", prefix, buf);
142 if (showall(bp, newmap, keybuf) == FALSE)
145 if (addlinef(bp, "%-16s%s", keybuf,
146 function_name(fun)) == FALSE)
154 help_help(int f, int n)
159 if ((kp = name_map("help")) == NULL)
163 funct = doscan(kp, getkey(FALSE), NULL);
164 } while (funct == NULL || funct == help_help);
166 if (macrodef && macrocount < MAXMACRO)
167 macro[macrocount - 1].m_funct = funct;
169 return ((*funct)(f, n));
174 apropos_command(int f, int n)
177 struct list *fnames, *el;
180 if (eread("apropos: ", string, sizeof(string), EFNUL | EFNEW) == NULL)
182 /* FALSE means we got a 0 character string, which is fine */
183 bp = bfind("*help*", TRUE);
184 if (bclear(bp) == FALSE)
187 fnames = complete_function_list("");
188 for (el = fnames; el != NULL; el = el->l_next) {
191 if (strstr(el->l_name, string) == NULL)
195 findbind(fundamental_map, name_function(el->l_name),
198 if (addlinef(bp, "%-32s%s", el->l_name, buf) == FALSE) {
199 free_file_list(fnames);
203 free_file_list(fnames);
204 return (popbuftop(bp, WNONE));
208 findbind(KEYMAP *map, PF fun, char *buf, size_t len)
212 char buf2[16], keybuf[16];
216 for (c = 0; c < 256; c++) {
217 nfun = doscan(map, c, &newmap);
219 getkeyname(buf, len, c);
223 if (findbind(newmap, fun, buf2, sizeof(buf2)) == TRUE) {
224 getkeyname(keybuf, sizeof(keybuf), c);
225 (void)snprintf(buf, len, "%s %s", keybuf, buf2);