-
Notifications
You must be signed in to change notification settings - Fork 26
/
led.c
454 lines (436 loc) · 10.4 KB
/
led.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
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
/* line editing and drawing */
#include <ctype.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
#include "vi.h"
static char *kmap_map(int kmap, int c)
{
static char cs[4];
char **keymap = conf_kmap(kmap);
cs[0] = c;
return keymap[c] ? keymap[c] : cs;
}
static int led_pos(int dir, int pos, int beg, int end)
{
return dir >= 0 ? pos - beg : end - pos - 1;
}
static int led_offdir(char **chrs, int *pos, int i)
{
if (pos[i] + ren_cwid(chrs[i], pos[i]) == pos[i + 1])
return +1;
if (pos[i + 1] + ren_cwid(chrs[i + 1], pos[i + 1]) == pos[i])
return -1;
return 0;
}
/* highlight text in reverse direction */
static void led_markrev(int n, char **chrs, int *pos, int *att)
{
int i = 0, j;
int hl = conf_hlrev();
while (i + 1 < n) {
int dir = led_offdir(chrs, pos, i);
int beg = i;
while (i + 1 < n && led_offdir(chrs, pos, i) == dir)
i++;
if (dir < 0)
for (j = beg; j <= i; j++)
att[j] = syn_merge(hl, att[j]);
if (i == beg)
i++;
}
}
/* render and highlight a line */
static char *led_render(char *s0, int cbeg, int cend, char *syn)
{
int n;
int *pos; /* pos[i]: the screen position of the i-th character */
int *off; /* off[i]: the character at screen position i */
int *att; /* att[i]: the attributes of i-th character */
char **chrs; /* chrs[i]: the i-th character in s1 */
int clast = 0;
int att_old = 0;
struct sbuf *out;
int i, j;
int ctx = dir_context(s0);
int att_blank = 0; /* the attribute of blank space */
chrs = uc_chop(s0, &n);
pos = ren_position(s0);
off = malloc((cend - cbeg) * sizeof(off[0]));
memset(off, 0xff, (cend - cbeg) * sizeof(off[0]));
/* initialise off[] using pos[] */
for (i = 0; i < n; i++) {
int curwid = ren_cwid(chrs[i], pos[i]);
int curbeg = led_pos(ctx, pos[i], cbeg, cend);
int curend = led_pos(ctx, pos[i] + curwid - 1, cbeg, cend);
if (curbeg >= 0 && curbeg < (cend - cbeg) &&
curend >= 0 && curend < (cend - cbeg)) {
for (j = 0; j < curwid; j++)
off[led_pos(ctx, pos[i] + j, cbeg, cend)] = i;
}
}
att = syn_highlight(n <= xlim ? syn : "", s0);
/* find the last non-empty column */
for (i = cbeg; i < cend; i++)
if (off[i - cbeg] >= 0)
clast = i;
/* the attribute of the last character is used for blanks */
att_blank = n > 0 ? att[n - 1] : 0;
led_markrev(n, chrs, pos, att);
/* generate term output */
out = sbuf_make();
sbuf_str(out, conf_lnpref());
i = cbeg;
while (i < cend && i <= clast) {
int o = off[i - cbeg];
int att_new = o >= 0 ? att[o] : att_blank;
sbuf_str(out, term_seqattr(att_new, att_old));
att_old = att_new;
if (o >= 0) {
if (ren_translate(chrs[o], s0)) {
sbuf_str(out, ren_translate(chrs[o], s0));
} else if (uc_isprint(chrs[o])) {
sbuf_mem(out, chrs[o], uc_len(chrs[o]));
} else {
for (j = i; j < cend && off[j - cbeg] == o; j++)
sbuf_chr(out, ' ');
}
while (i < cend && off[i - cbeg] == o)
i++;
} else {
sbuf_chr(out, ' ');
i++;
}
}
if (clast < cend - 1)
sbuf_str(out, term_seqkill());
sbuf_str(out, term_seqattr(0, att_old));
free(att);
free(pos);
free(off);
free(chrs);
return sbuf_done(out);
}
/* print a line on the screen */
void led_print(char *s, int row, int left, char *syn)
{
char *r = led_render(s, left, left + xcols, syn);
term_pos(row, 0);
term_kill();
term_str(r);
free(r);
}
/* set xtd and return its old value */
static int td_set(int td)
{
int old = xtd;
xtd = td;
return old;
}
/* print a line on the screen; for ex messages */
void led_printmsg(char *s, int row, char *syn)
{
int td = td_set(+2);
char *r = led_render(s, 0, xcols, syn);
td_set(td);
term_pos(row, 0);
term_kill();
term_str(r);
free(r);
}
static int led_lastchar(char *s)
{
char *r = *s ? strchr(s, '\0') : s;
if (r != s)
r = uc_beg(s, r - 1);
return r - s;
}
static int led_lastword(char *s)
{
char *r = *s ? uc_beg(s, strchr(s, '\0') - 1) : s;
int kind;
while (r > s && uc_isspace(r))
r = uc_beg(s, r - 1);
kind = r > s ? uc_kind(r) : 0;
while (r > s && uc_kind(uc_beg(s, r - 1)) == kind)
r = uc_beg(s, r - 1);
return r - s;
}
static void led_printparts(char *ai, char *pref, char *main,
char *post, int *left, int kmap, char *syn)
{
struct sbuf *ln;
int off, pos;
int idir = 0;
ln = sbuf_make();
sbuf_str(ln, ai);
sbuf_str(ln, pref);
sbuf_str(ln, main);
off = uc_slen(sbuf_buf(ln));
/* cursor position for inserting the next character */
if (*pref || *main || *ai) {
int len = sbuf_len(ln);
sbuf_str(ln, kmap_map(kmap, 'a'));
sbuf_str(ln, post);
idir = ren_pos(sbuf_buf(ln), off) -
ren_pos(sbuf_buf(ln), off - 1) < 0 ? -1 : +1;
sbuf_cut(ln, len);
}
term_record();
sbuf_str(ln, post);
pos = ren_cursor(sbuf_buf(ln), ren_pos(sbuf_buf(ln), MAX(0, off - 1)));
if (pos >= *left + xcols)
*left = pos - xcols / 2;
if (pos < *left)
*left = pos < xcols ? 0 : pos - xcols / 2;
led_print(sbuf_buf(ln), -1, *left, syn);
term_pos(-1, led_pos(dir_context(sbuf_buf(ln)), pos + idir, *left, *left + xcols));
sbuf_free(ln);
term_commit();
}
/* continue reading the character starting with c */
static char *led_readchar(int c, int kmap)
{
static char buf[8];
int c1, c2;
int i, n;
if (c == TK_CTL('v')) { /* literal character */
buf[0] = term_read();
buf[1] = '\0';
return buf;
}
if (c == TK_CTL('k')) { /* digraph */
c1 = term_read();
if (TK_INT(c1))
return NULL;
if (c1 == TK_CTL('k'))
return "";
c2 = term_read();
if (TK_INT(c2))
return NULL;
return conf_digraph(c1, c2);
}
if ((c & 0xc0) == 0xc0) { /* utf-8 character */
buf[0] = c;
n = uc_len(buf);
for (i = 1; i < n; i++)
buf[i] = term_read();
buf[n] = '\0';
return buf;
}
return kmap_map(kmap, c);
}
/* read a character from the terminal */
char *led_read(int *kmap)
{
int c = term_read();
while (!TK_INT(c)) {
switch (c) {
case TK_CTL('f'):
*kmap = xkmap_alt;
break;
case TK_CTL('e'):
*kmap = 0;
break;
default:
return led_readchar(c, *kmap);
}
c = term_read();
}
return NULL;
}
static int led_match(char *out, int len, char *kwd, char *opt)
{
while (opt != NULL) {
int i = 0;
while (kwd[i] && kwd[i] == opt[i])
i++;
if (kwd[i] == '\0')
break;
opt = strchr(opt, '\n') == NULL ? NULL : strchr(opt, '\n') + 1;
}
out[0] = '\0';
if (opt != NULL) {
int i = 0;
char *beg = opt + strlen(kwd);
while (beg[i] && beg[i] != '\n' && i + 8 < len)
i += uc_len(beg + i);
memcpy(out, beg, i);
out[i] = '\0';
return 0;
}
return 1;
}
/* read a line from the terminal */
static char *led_line(char *pref, char *post, char *ai, int ai_max, int *left,
int *key, int *kmap, char *syn, char *hist, void (*showinfo)(char *ln))
{
struct sbuf *sb;
int ai_len = strlen(ai);
int c, y, lnmode;
char cmp[64] = "";
char *cs;
sb = sbuf_make();
if (pref == NULL)
pref = "";
if (post == NULL || !post[0])
post = cmp;
while (1) {
if (hist != NULL)
led_match(cmp, sizeof(cmp), sbuf_buf(sb), hist);
led_printparts(ai, pref, sbuf_buf(sb), post, left, *kmap, syn);
c = term_read();
switch (c) {
case TK_CTL('f'):
*kmap = xkmap_alt;
continue;
case TK_CTL('e'):
*kmap = 0;
continue;
case TK_CTL('h'):
case 127:
if (sbuf_len(sb))
sbuf_cut(sb, led_lastchar(sbuf_buf(sb)));
break;
case TK_CTL('u'):
sbuf_cut(sb, 0);
break;
case TK_CTL('w'):
if (sbuf_len(sb))
sbuf_cut(sb, led_lastword(sbuf_buf(sb)));
break;
case TK_CTL('t'):
if (ai_len < ai_max) {
ai[ai_len++] = '\t';
ai[ai_len] = '\0';
}
break;
case TK_CTL('d'):
/* when ai and pref are empty, remove the first space of sb */
if (ai_len == 0 && !pref[0]) {
char *buf = sbuf_buf(sb);
if (buf[0] == ' ' || buf[0] == '\t') {
char *dup = uc_dup(buf + 1);
sbuf_cut(sb, 0);
sbuf_str(sb, dup);
free(dup);
}
}
if (ai_len > 0)
ai[--ai_len] = '\0';
break;
case TK_CTL('p'):
if (reg_get(0, &lnmode))
sbuf_str(sb, reg_get(0, &lnmode));
break;
case TK_CTL('r'):
y = term_read();
if (y > 0 && reg_get(y, &lnmode))
sbuf_str(sb, reg_get(y, &lnmode));
break;
case TK_CTL('a'):
if (showinfo != NULL) {
char *ln = uc_cat(pref, sbuf_buf(sb));
showinfo(ln);
free(ln);
} else {
sbuf_str(sb, cmp);
}
break;
default:
if (c == '\n' || TK_INT(c))
break;
if ((cs = led_readchar(c, *kmap)) != NULL)
sbuf_str(sb, cs);
}
if (c == '\n')
led_printparts(ai, pref, sbuf_buf(sb), "", left, *kmap, syn);
if (c == '\n' || TK_INT(c))
break;
}
*key = c;
return sbuf_done(sb);
}
/* read an ex command */
char *led_prompt(char *pref, char *post, int *kmap, char *syn, char *hist)
{
int key;
int td = td_set(+2);
int left = 0;
char *s = led_line(pref, post, "", 0, &left, &key, kmap, syn, hist, NULL);
td_set(td);
if (key == '\n') {
struct sbuf *sb = sbuf_make();
if (pref)
sbuf_str(sb, pref);
sbuf_str(sb, s);
if (post)
sbuf_str(sb, post);
free(s);
return sbuf_done(sb);
}
free(s);
return NULL;
}
static int linecount(char *s)
{
int n;
for (n = 0; s; n++)
if ((s = strchr(s, '\n')))
s++;
return n;
}
/* read visual command input */
char *led_input(char *pref, char *post, int *left, int *kmap, char *syn, void (*nextline)(void), void (*showinfo)(char *ln))
{
struct sbuf *sb = sbuf_make();
char ai[128];
int ai_max = sizeof(ai) - 1;
int n = 0;
int key;
while (n < ai_max && (*pref == ' ' || *pref == '\t'))
ai[n++] = *pref++;
ai[n] = '\0';
while (1) {
char *ln = led_line(pref, post, ai, ai_max, left, &key, kmap, syn, NULL, showinfo);
int ln_sp = 0; /* number of initial spaces in ln */
int lncnt = linecount(ln) - 1 + (key == '\n');
while (ln[ln_sp] && (ln[ln_sp] == ' ' || ln[ln_sp] == '\t'))
ln_sp++;
/* append the auto-indent only if there are other characters */
if (ln[ln_sp] || (pref && pref[0]) ||
(key != '\n' && post[0] && post[0] != '\n'))
sbuf_str(sb, ai);
if (pref)
sbuf_str(sb, pref);
sbuf_str(sb, ln);
if (key == '\n')
sbuf_chr(sb, '\n');
while (lncnt-- > 0)
nextline();
if (!pref || !pref[0]) { /* updating autoindent */
int ai_len = ai_max ? strlen(ai) : 0;
int ai_new = ln_sp;
if (ai_len + ai_new > ai_max)
ai_new = ai_max - ai_len;
memcpy(ai + ai_len, ln, ai_new);
ai[ai_len + ai_new] = '\0';
}
if (!xai)
ai[0] = '\0';
free(ln);
if (key != '\n')
break;
pref = NULL;
n = 0;
while (xai && (post[n] == ' ' || post[n] == '\t'))
n++;
memmove(post, post + n, strlen(post) - n + 1);
}
sbuf_str(sb, post);
if (TK_INT(key))
return sbuf_done(sb);
sbuf_free(sb);
return NULL;
}