-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathhelp.c
249 lines (215 loc) · 4.36 KB
/
help.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
/* Copyright (C) 1988-2018 Sean MacLennan */
#include "z.h"
/** @addtogroup zedit
* @{
*/
static const char *const key_label[] = {
"up", "down", "right", "left",
"insert", "delete", "page up", "page down", "home", "end",
"f1", "f2", "f3", "f4", "f5", "f6",
"f7", "f8", "f9", "f10", "f11", "f12",
"C-home", "C-end",
};
static const char *special_label(int key)
{
if (key >= SPECIAL_START && key <= SPECIAL_END)
return key_label[key - SPECIAL_START];
else
return "???";
}
static int is_special(int cmd)
{
return cmd >= SPECIAL_START && cmd <= SPECIAL_END;
}
static char *dispkey(unsigned int key, char *s)
{
char *p;
int j;
*s = '\0';
if (is_special(key))
return strcpy(s, special_label(key));
if (key > 127)
strcpy(s, key < 256 ? "C-X " : "M-");
j = key & 0x7f;
if (j == 27)
strcat(s, "ESC");
else if (j < 32 || j == 127) {
strcat(s, "C-");
p = s + strlen(s);
*p++ = j ^ '@';
*p = '\0';
} else if (j == 32)
strcat(s, "Space");
else {
p = s + strlen(s);
*p++ = j;
*p = '\0';
}
return s;
}
static void dump_bindings(int fnum)
{
int k, found = 0;
binstr(Bbuff, "\nBinding(s): ");
for (k = 0; k < NUMKEYS; ++k)
if (Keys[k] == fnum) {
if (found) {
binsert(Bbuff, ',');
Cmd = ' ';
Zfill_check();
} else
found = true;
binstr(Bbuff, dispkey(k, PawStr));
}
if (!found)
binstr(Bbuff, "Unbound");
}
void dump_doc(const char *doc)
{
if (doc) {
binstr(Bbuff, "\n\n");
for (; *doc; ++doc)
if (*doc == ' ') {
Cmd = *doc;
Zfill_check();
} else
binsert(Bbuff, *doc);
}
binsert(Bbuff, '\n');
}
void Zhelp(void)
{
Cmd = delayprompt("Help: a=apropos f=function k=key v=variable");
switch (Cmd) {
case 'a':
Zhelp_apropos();
break;
case 'f':
Zhelp_function();
break;
case 'k':
Zhelp_key();
break;
case 'v':
Zhelp_variable();
break;
default:
Zabort();
}
}
void Zhelp_function(void)
{
bool pawok;
int rc = getplete("Function: ", NULL, (char **)Cnames,
CNAMESIZE, NUMFUNCS);
if (rc == -1)
return;
if (Argp) {
struct zbuff *buff = cmakebuff(HELPBUFF, NULL);
if (buff == NULL)
return;
cswitchto(buff);
bempty(Bbuff);
} else
wuseother(HELPBUFF);
binstr(Bbuff, Cnames[rc].name);
pawok = Cmds[Cnames[rc].fnum][1] != Znotimpl;
if (pawok || Cnames[rc].flags) {
binstr(Bbuff, " (");
if (Cnames[rc].flags)
binsert(Bbuff, Cnames[rc].flags);
if (pawok)
binsert(Bbuff, 'P');
binsert(Bbuff, ')');
}
dump_doc(Cnames[rc].doc);
dump_bindings(Cnames[rc].fnum);
btostart(Bbuff);
Bbuff->bmodf = false;
}
void Zhelp_variable(void)
{
int rc = getplete("Variable: ", NULL, (char **)Vars,
sizeof(struct avar), NUMVARS);
if (rc == -1)
return;
wuseother(HELPBUFF);
binstr(Bbuff, "%s: ", Vars[rc].vname);
switch (Vars[rc].vtype) {
case V_STRING:
binstr(Bbuff, VARSTR(rc) ? VARSTR(rc) : "NONE");
break;
case V_FLAG:
binstr(Bbuff, VAR(rc) ? "On" : "Off");
break;
case V_DECIMAL:
binstr(Bbuff, "%d", VAR(rc));
}
dump_doc(Vars[rc].doc);
btostart(Bbuff);
Bbuff->bmodf = false;
}
void Zhelp_apropos(void)
{
char word[STRMAX], line[80];
int i, j, n, match = 0;
*word = '\0';
if (getarg("Word: ", word, sizeof(word)))
return;
for (i = j = n = 0; i < NUMFUNCS; ++i)
if (strstr(Cnames[i].name, word)) {
if (match++ == 0)
wuseother(HELPBUFF);
n += strfmt(line + n, sizeof(line) - n,
"%-24s", Cnames[i].name);
if (++j == 3) {
binstr(Bbuff, "%s\n", line);
j = n = 0;
}
}
if (j)
binstr(Bbuff, "%s\n", line);
if (match == 0)
putpaw("No matches.");
else {
btostart(Bbuff);
Bbuff->bmodf = false;
}
}
void Zhelp_key(void)
{
char kstr[12];
int rc;
unsigned int key, was = Cmd;
Arg = 0;
putpaw("Key: ");
Cmd = tgetkb();
if (Cmd == TC_UNKNOWN) {
putpaw("Sorry, I don't recognize that key.");
Cmd = was;
return;
}
key = Keys[Cmd];
if (key == ZCTRL_X) {
putpaw("Key: C-X ");
Cmd = toupper(tgetkb()) + CX(0);
key = Keys[Cmd];
} else if (key == ZMETA) {
putpaw("Key: M-");
Cmd = toupper(tgetkb()) + M(0);
key = Keys[Cmd];
}
if (key == ZNOTIMPL)
putpaw("%s (%03o) Unbound", dispkey(Cmd, kstr), Cmd);
else
for (rc = 0; rc < NUMFUNCS; ++rc)
if (Cnames[rc].fnum == key)
putpaw("%s (%03o) Bound to %s",
dispkey(Cmd, kstr), Cmd,
Cnames[rc].name);
/* We need to set Cmd above since some keystrokes will be
* wrong if not set. Put it back to the original value.
*/
Cmd = was;
}
/* @} */