-
Notifications
You must be signed in to change notification settings - Fork 4
/
edlin.c
292 lines (279 loc) · 5.96 KB
/
edlin.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
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
#include "scheme.h"
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
/*
DO: make state attached to the socket, not global!
use socket_port_ready_p to hook into the poll-for-interrupt stuff.
-> this will require an extra input queue (of, say, 32 chars)
-> this should be done globally, from repld.scm.
-> i.e. (system:set-interrupt-check-hook client)
line editing, emacs-style, including esc-f and the like
command history, bash-style
paren matching
tab completion? should just call out to scheme, since that is app-dependent
*/
#define CTRL_A 1
#define CTRL_B 2
#define CTRL_C 3
#define CTRL_D 4
#define CTRL_E 5
#define CTRL_F 6
#define CTRL_G 7
#define CTRL_H 8
#define TAB 9
#define LINEFEED 10
#define CTRL_K 11
#define CTRL_L 12
#define RETURN 13
#define CTRL_N 14
#define CTRL_O 15
#define CTRL_P 16
#define CTRL_Q 17
#define CTRL_R 18
#define CTRL_S 19
#define CTRL_T 20
#define CTRL_U 21
#define CTRL_V 22
#define CTRL_W 23
#define CTRL_X 24
#define CTRL_Y 25
#define CTRL_Z 26
#define ESCAPE 27
#define DELETE 127
static char buf[1024];
static int max=1022, in=0, eol=0, out=0, cursor=0, history=0;
static int static_meta = 0;
/*static char *break_chars=" ();";*/
static int nextChar(object p) {
/* printf("next: cursor = %d, in = %d, out = %d, eol = %d\n", cursor, in, out, eol); */
if (out < eol)
return buf[out++];
if (in == out)
in = out = eol = cursor = 0;
return 0;
}
static void beep(object p) {
write_character(p, CTRL_G);
}
void add_to_history(char *line) {
object s = make_string(line);
object p = null_object;
PUSH_GC_PROTECT(s);
PUSH_GC_PROTECT(p);
p = global("system:*edlin-history*");
if (UNBOUND_P(p))
p = null_object;
p = cons(s, p);
define("system:*edlin-history*", p);
POP_GC_PROTECT(2);
}
int yank_from_history(int i, char *line) {
object p = global("system:*edlin-history*");
while (PAIR_P(p)) {
if (--i <= 0) {
object s = CAR(p);
strcpy(line, STRING_VALUE(s));
return 1;
}
p = CDR(p);
}
return 0;
}
static void getMoreData(object p) {
int i;
int c = (*(PORT_GETC_PROC(p)))(p);
int meta = static_meta;
static_meta = 0;
switch (c) {
case RETURN:
buf[in] = 0;
if (in > 0)
add_to_history(buf);
history = 0;
buf[in++] = c;
buf[in] = 0;
write_character(p, RETURN);
write_character(p, LINEFEED);
eol = in;
break;
case CTRL_A:
while (cursor > 0) {
write_character(p, CTRL_H);
cursor--;
}
meta = 0;
break;
case CTRL_B:
if (cursor > 0) {
write_character(p, CTRL_H);
cursor--;
}
break;
case CTRL_C:
interrupt();
break;
case CTRL_D:
if (cursor < in) {
memmove(buf+cursor, buf+cursor+1, in-cursor);
for (i=cursor; i<in; i++)
write_character(p, buf[i]);
write_character(p, ' ');
for (i=cursor; i<in; i++)
write_character(p, CTRL_H);
in--;
}
break;
case CTRL_E:
while (cursor < in) {
write_character(p, buf[cursor++]);
}
break;
case CTRL_F:
if (cursor < in)
write_character(p, buf[cursor++]);
break;
case CTRL_G:
/* reset search mode, etc */
break;
case CTRL_H:
case DELETE:
if (cursor == 0)
beep(p);
else {
if (meta) {
printf("NYI: back delete a word\n");
} else {
if (cursor-- == in) {
in--;
write_character(p, CTRL_H);
write_character(p, ' ');
write_character(p, CTRL_H);
} else {
i = cursor;
memmove(buf+cursor, buf+cursor+1, in-cursor);
write_character(p, CTRL_H);
in--;
while (i < in)
write_character(p, buf[i++]);
write_character(p, ' ');
write_character(p, CTRL_H);
while (i-- > cursor)
write_character(p, CTRL_H);
}
}
}
break;
case TAB:
printf("NYI: complete current token\n");
/* inserting it, keeping state, so next tab gets next possiblity */
break;
case CTRL_K:
for (i=cursor; i<in; i++)
write_character(p, ' ');
for (i=cursor; i<in; i++)
write_character(p, CTRL_H);
in = cursor;
break;
case CTRL_N:
if (history > 0 && yank_from_history(history-1, buf)) {
int oldin = in;
for (i=cursor; i>0; i--)
write_character(p, CTRL_H);
history--;
out = 0;
cursor = in = strlen(buf);
for (i=0; i<in; i++)
write_character(p, buf[i]);
for (i=in; i<oldin; i++)
write_character(p, ' ');
for (i=in; i<oldin; i++)
write_character(p, CTRL_H);
}
break;
case CTRL_P:
if (yank_from_history(history+1, buf)) {
int oldin = in;
for (i=cursor; i>0; i--)
write_character(p, CTRL_H);
history++;
out = 0;
cursor = in = strlen(buf);
for (i=0; i<in; i++)
write_character(p, buf[i]);
for (i=in; i<oldin; i++)
write_character(p, ' ');
for (i=in; i<oldin; i++)
write_character(p, CTRL_H);
}
break;
case ESCAPE:
static_meta = 1;
break;
default:
if (meta) {
switch (c) {
case 'f':
/*
move forward a word
while (cursor < in && strchar(break_chars,c) < 0) next char
*/
printf("NYI: forward cursor a word\n");
break;
case 'b':
/*move back a word
while (cursor > 0 && strchar(break_chars,c) < 0) prev char */
printf("NYI: back cursor a word\n");
break;
}
} else {
if (c < ' ' || in == max)
beep(p);
else {
write_character(p, c);
if (cursor == in) {
buf[in++] = c;
cursor++;
} else {
memmove(buf+cursor+1, buf+cursor, in-cursor);
buf[cursor++] = c;
in++;
for (i=cursor; i<in; i++)
write_character(p, buf[i]);
for (i=cursor; i<in; i++)
write_character(p, CTRL_H);
}
if (c == ')') {
int j, k=0;
for (j=cursor; j>=0; j--) {
if (buf[j] == ')') k++;
if (buf[j] == '(') {
if (! --k)
break;
}
}
if (j >= 0) {
for (i=cursor; i>j; i--)
write_character(p, CTRL_H);
write_character(p, ' ');
write_character(p, CTRL_H);
msec_sleep(200);
for (i=j; i<cursor; i++)
write_character(p, buf[i]);
} else {
beep(p);
}
}
}
}
break;
}
}
int edlin_getc(object p) {
int c = nextChar(p);
while (!c) {
getMoreData(p);
c = nextChar(p);
}
return c;
}