help.c revision 1.16
1/*	$OpenBSD: help.c,v 1.16 2002/03/11 13:02:56 vincent Exp $	*/
2
3/*
4 * Help functions for Mg 2
5 */
6
7#include "def.h"
8#include "funmap.h"
9
10#ifndef NO_HELP
11#include "kbd.h"
12#include "key.h"
13#ifndef NO_MACRO
14#include "macro.h"
15#endif /* !NO_MACRO */
16
17static int	showall(BUFFER *, KEYMAP *, char *);
18static int	findbind(KEYMAP *, PF, char *, size_t);
19
20/*
21 * Read a key from the keyboard, and look it up in the keymap.
22 * Display the name of the function currently bound to the key.
23 */
24/* ARGSUSED */
25int
26desckey(f, n)
27	int f, n;
28{
29	KEYMAP	*curmap;
30	PF	 funct;
31	int	 c, m, i;
32	char	*pep;
33	char	 prompt[80];
34
35#ifndef NO_MACRO
36	if (inmacro)
37		return TRUE;	/* ignore inside keyboard macro */
38#endif /* !NO_MACRO */
39	pep = prompt + strlcpy(prompt, "Describe key briefly: ", sizeof(prompt));
40	key.k_count = 0;
41	m = curbp->b_nmodes;
42	curmap = curbp->b_modes[m]->p_map;
43	for (;;) {
44		for (;;) {
45			ewprintf("%s", prompt);
46			pep[-1] = ' ';
47			pep = keyname(pep, sizeof(prompt) - (pep - prompt),
48			    key.k_chars[key.k_count++] = c = getkey(FALSE));
49			if ((funct = doscan(curmap, c, &curmap)) != NULL)
50				break;
51			*pep++ = '-';
52			*pep = '\0';
53		}
54		if (funct != rescan)
55			break;
56		if (ISUPPER(key.k_chars[key.k_count - 1])) {
57			funct = doscan(curmap,
58			    TOLOWER(key.k_chars[key.k_count - 1]), &curmap);
59			if (funct == NULL) {
60				*pep++ = '-';
61				*pep = '\0';
62				continue;
63			}
64			if (funct != rescan)
65				break;
66		}
67nextmode:
68		if (--m < 0)
69			break;
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);
73			if (funct != NULL) {
74				if (i == key.k_count - 1 && funct != rescan)
75					goto found;
76				funct = rescan;
77				goto nextmode;
78			}
79		}
80		*pep++ = '-';
81		*pep = '\0';
82	}
83found:
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);
88	else
89		ewprintf("%k is bound to an unnamed function");
90	return TRUE;
91}
92
93/*
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.
97 */
98/* ARGSUSED */
99int
100wallchart(f, n)
101	int f, n;
102{
103	int		 m;
104	BUFFER		*bp;
105
106	bp = bfind("*help*", TRUE);
107	if (bclear(bp) != TRUE)
108		/* clear it out */
109		return FALSE;
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))
115			return FALSE;
116	}
117	if ((addline(bp, "Global bindings:") == FALSE) ||
118	    (showall(bp, fundamental_map, "") == FALSE))
119		return FALSE;
120	return popbuftop(bp);
121}
122
123static int
124showall(BUFFER *bp, KEYMAP *map, char *prefix)
125{
126	KEYMAP *newmap;
127	char buf[80], key[16];
128	PF fun;
129	int c;
130
131	if (addline(bp, "") == FALSE)
132		return FALSE;
133
134	/* XXX - 256 ? */
135	for (c = 0; c < 256; c++) {
136		fun = doscan(map, c, &newmap);
137		if (fun == rescan || fun == selfinsert)
138			continue;
139		keyname(buf, sizeof(buf), c);
140		snprintf(key, sizeof key, "%s%s ", prefix, buf);
141		if (fun == NULL) {
142			if (showall(bp, newmap, key) == FALSE)
143				return FALSE;
144		} else {
145			if (addlinef(bp, "%-16s%s", key,
146				    function_name(fun)) == FALSE)
147				return FALSE;
148		}
149	}
150
151	return TRUE;
152}
153
154int
155help_help(f, n)
156	int f, n;
157{
158	KEYMAP	*kp;
159	PF	 funct;
160
161	if ((kp = name_map("help")) == NULL)
162		return FALSE;
163	ewprintf("a b c: ");
164	do {
165		funct = doscan(kp, getkey(FALSE), NULL);
166	} while (funct == NULL || funct == help_help);
167#ifndef NO_MACRO
168	if (macrodef && macrocount < MAXMACRO)
169		macro[macrocount - 1].m_funct = funct;
170#endif /* !NO_MACRO */
171	return (*funct)(f, n);
172}
173
174/* ARGSUSED */
175int
176apropos_command(f, n)
177	int f, n;
178{
179	BUFFER		*bp;
180	LIST		*fnames, *el;
181	char		 string[32];
182
183	if (eread("apropos: ", string, sizeof(string), EFNEW) == ABORT)
184		return ABORT;
185	/* FALSE means we got a 0 character string, which is fine */
186	bp = bfind("*help*", TRUE);
187	if (bclear(bp) == FALSE)
188		return FALSE;
189
190	fnames = complete_function_list("", NULL);
191	for (el = fnames; el != NULL; el = el->l_next) {
192		char buf[32];
193
194		if (strstr(el->l_name, string) == NULL)
195			continue;
196
197		buf[0] = '\0';
198		findbind(fundamental_map, name_function(el->l_name),
199		    buf, sizeof(buf));
200
201		if (addlinef(bp, "%-32s%s", el->l_name,  buf) == FALSE) {
202			free_file_list(fnames);
203			return FALSE;
204		}
205	}
206	free_file_list(fnames);
207	return popbuftop(bp);
208}
209
210static int
211findbind(KEYMAP *map, PF fun, char *buf, size_t len)
212{
213	KEYMAP *newmap;
214	PF nfun;
215	char buf2[16], key[16];
216	int c;
217
218	/* XXX - 256 ? */
219	for (c = 0; c < 256; c++) {
220		nfun = doscan(map, c, &newmap);
221		if (nfun == fun) {
222			keyname(buf, len, c);
223			return TRUE;
224		}
225		if (nfun == NULL) {
226			if (findbind(newmap, fun, buf2, sizeof(buf2)) == TRUE) {
227				keyname(key, sizeof(key), c);
228				snprintf(buf, len, "%s %s", key, buf2);
229				return TRUE;
230			}
231		}
232	}
233
234	return FALSE;
235}
236#endif /* !NO_HELP */
237