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