Annotation of src/usr.bin/mg/help.c, Revision 1.8
1.8 ! art 1: /* $OpenBSD: help.c,v 1.7 2001/05/23 21:01:16 art Exp $ */
1.4 niklas 2:
1.3 millert 3: /*
4: * Help functions for Mg 2
5: */
1.1 deraadt 6:
7: #include "def.h"
8:
9: #ifndef NO_HELP
10: #include "kbd.h"
11: #include "key.h"
12: #ifndef NO_MACRO
13: #include "macro.h"
1.3 millert 14: #endif /* !NO_MACRO */
1.2 millert 15:
1.8 ! art 16: static int showall(BUFFER *, KEYMAP *, char *);
! 17: static int findbind(KEYMAP *, PF, char *);
1.1 deraadt 18:
19: /*
1.2 millert 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.
1.1 deraadt 22: */
1.2 millert 23: /* ARGSUSED */
24: int
1.1 deraadt 25: desckey(f, n)
1.3 millert 26: int f, n;
1.1 deraadt 27: {
1.3 millert 28: KEYMAP *curmap;
29: PF funct;
30: int c, m, i;
31: char *pep;
32: char prompt[80];
1.1 deraadt 33:
34: #ifndef NO_MACRO
1.2 millert 35: if (inmacro)
36: return TRUE; /* ignore inside keyboard macro */
1.3 millert 37: #endif /* !NO_MACRO */
38: (VOID)strcpy(prompt, "Describe key briefly: ");
1.2 millert 39: pep = prompt + strlen(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, key.k_chars[key.k_count++] =
48: c = getkey(FALSE));
1.6 art 49: if ((funct = doscan(curmap, c, &curmap)) != NULL)
1.2 millert 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,
1.6 art 58: TOLOWER(key.k_chars[key.k_count - 1]), &curmap);
1.5 art 59: if (funct == NULL) {
1.2 millert 60: *pep++ = '-';
61: *pep = '\0';
62: continue;
63: }
64: if (funct != rescan)
65: break;
66: }
67: nextmode:
68: if (--m < 0)
69: break;
70: curmap = curbp->b_modes[m]->p_map;
71: for (i = 0; i < key.k_count; i++) {
1.6 art 72: funct = doscan(curmap, key.k_chars[i], &curmap);
1.5 art 73: if (funct != NULL) {
1.2 millert 74: if (i == key.k_count - 1 && funct != rescan)
75: goto found;
76: funct = rescan;
77: goto nextmode;
78: }
79: }
1.1 deraadt 80: *pep++ = '-';
81: *pep = '\0';
82: }
83: found:
1.7 art 84: if (funct == rescan || funct == selfinsert)
1.2 millert 85: ewprintf("%k is not bound to any function");
86: else if ((pep = 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;
1.1 deraadt 91: }
92:
93: /*
1.2 millert 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.
1.1 deraadt 97: */
1.2 millert 98: /* ARGSUSED */
99: int
1.1 deraadt 100: wallchart(f, n)
1.3 millert 101: int f, n;
1.1 deraadt 102: {
1.3 millert 103: int m;
1.8 ! art 104: BUFFER *bp;
1.1 deraadt 105:
106: bp = bfind("*help*", TRUE);
1.2 millert 107: if (bclear(bp) != TRUE)
1.3 millert 108: /* clear it out */
109: return FALSE;
1.2 millert 110: for (m = curbp->b_nmodes; m > 0; m--) {
1.8 ! art 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) ||
1.2 millert 114: (addline(bp, "") == FALSE))
115: return FALSE;
1.1 deraadt 116: }
1.2 millert 117: if ((addline(bp, "Global bindings:") == FALSE) ||
1.8 ! art 118: (showall(bp, map_table[0].p_map, "") == FALSE))
1.2 millert 119: return FALSE;
1.1 deraadt 120: return popbuftop(bp);
121: }
122:
1.2 millert 123: static int
1.8 ! art 124: showall(BUFFER *bp, KEYMAP *map, char *prefix)
1.1 deraadt 125: {
1.8 ! art 126: KEYMAP *newmap;
! 127: char buf[80], key[16];
! 128: PF fun;
! 129: int c;
1.1 deraadt 130:
1.2 millert 131: if (addline(bp, "") == FALSE)
132: return FALSE;
1.8 ! art 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, c);
! 140: sprintf(key, "%s%s ", prefix, buf);
! 141: if (fun == NULL) {
! 142: if (showall(bp, newmap, key) == FALSE)
1.2 millert 143: return FALSE;
1.8 ! art 144: } else {
! 145: if (addlinef(bp, "%-16s%s", key,
! 146: function_name(fun)) == FALSE)
1.2 millert 147: return FALSE;
1.1 deraadt 148: }
149: }
1.8 ! art 150:
1.1 deraadt 151: return TRUE;
152: }
153:
1.2 millert 154: int
1.1 deraadt 155: help_help(f, n)
1.3 millert 156: int f, n;
1.1 deraadt 157: {
1.3 millert 158: KEYMAP *kp;
159: PF funct;
1.1 deraadt 160:
1.2 millert 161: if ((kp = name_map("help")) == NULL)
162: return FALSE;
163: ewprintf("a b c: ");
164: do {
1.6 art 165: funct = doscan(kp, getkey(FALSE), NULL);
1.2 millert 166: } while (funct == NULL || funct == help_help);
1.1 deraadt 167: #ifndef NO_MACRO
1.2 millert 168: if (macrodef && macrocount < MAXMACRO)
169: macro[macrocount - 1].m_funct = funct;
1.3 millert 170: #endif /* !NO_MACRO */
171: return (*funct)(f, n);
1.1 deraadt 172: }
173:
1.2 millert 174: /* ARGSUSED */
175: int
1.1 deraadt 176: apropos_command(f, n)
1.3 millert 177: int f, n;
1.1 deraadt 178: {
1.3 millert 179: BUFFER *bp;
1.8 ! art 180: LIST *fnames, *el;
1.3 millert 181: char string[32];
1.1 deraadt 182:
1.2 millert 183: if (eread("apropos: ", string, sizeof(string), EFNEW) == ABORT)
184: return ABORT;
1.1 deraadt 185: /* FALSE means we got a 0 character string, which is fine */
1.2 millert 186: bp = bfind("*help*", TRUE);
187: if (bclear(bp) == FALSE)
188: return FALSE;
1.8 ! art 189:
! 190: fnames = complete_function_list("", NULL);
! 191: for (el = fnames; el != NULL; el = el->l_next) {
! 192: char buf[32];
! 193: if (strstr(el->l_name, string) == NULL)
! 194: continue;
! 195: buf[0] = '\0';
! 196: findbind(name_map("fundamental"),
! 197: name_function(el->l_name), buf);
! 198:
! 199: if (addlinef(bp, "%-32s%s", el->l_name, buf) == FALSE) {
! 200: free_file_list(fnames);
! 201: return FALSE;
1.2 millert 202: }
1.1 deraadt 203: }
1.8 ! art 204: free_file_list(fnames);
1.2 millert 205: return popbuftop(bp);
1.1 deraadt 206: }
207:
1.8 ! art 208: static int
! 209: findbind(KEYMAP *map, PF fun, char *buf)
1.1 deraadt 210: {
1.8 ! art 211: KEYMAP *newmap;
! 212: PF nfun;
! 213: char buf2[16], key[16];
! 214: int c;
! 215:
! 216: /* XXX - 256 ? */
! 217: for (c = 0; c < 256; c++) {
! 218: nfun = doscan(map, c, &newmap);
! 219: if (nfun == fun) {
! 220: keyname(buf, c);
! 221: return TRUE;
1.2 millert 222: }
1.8 ! art 223: if (nfun == NULL) {
! 224: if (findbind(newmap, fun, buf2) == TRUE) {
! 225: keyname(key, c);
! 226: sprintf(buf, "%s %s", key, buf2);
! 227: return TRUE;
1.2 millert 228: }
229: }
1.1 deraadt 230: }
231:
1.8 ! art 232: return FALSE;
1.1 deraadt 233: }
1.3 millert 234: #endif /* !NO_HELP */