regex: match $ without \n
[neatvi.git] / led.c
blob635daeb275b4822819b660fcf778951c9b9f6c27
1 /* line editing and drawing */
2 #include <ctype.h>
3 #include <stdio.h>
4 #include <string.h>
5 #include <stdlib.h>
6 #include <unistd.h>
7 #include "vi.h"
9 static char *kmap_map(int kmap, int c)
11 static char cs[4];
12 char **keymap = conf_kmap(kmap);
13 cs[0] = c;
14 return keymap[c] ? keymap[c] : cs;
17 static int led_pos(int dir, int pos, int beg, int end)
19 return dir >= 0 ? pos - beg : end - pos - 1;
22 static int led_offdir(char **chrs, int *pos, int i)
24 if (pos[i] + ren_cwid(chrs[i], pos[i]) == pos[i + 1])
25 return +1;
26 if (pos[i + 1] + ren_cwid(chrs[i + 1], pos[i + 1]) == pos[i])
27 return -1;
28 return 0;
31 /* highlight text in reverse direction */
32 static void led_markrev(int n, char **chrs, int *pos, int *att)
34 int i = 0, j;
35 int hl = conf_hlrev();
36 while (i + 1 < n) {
37 int dir = led_offdir(chrs, pos, i);
38 int beg = i;
39 while (i + 1 < n && led_offdir(chrs, pos, i) == dir)
40 i++;
41 if (dir < 0)
42 for (j = beg; j <= i; j++)
43 att[j] = syn_merge(hl, att[j]);
44 if (i == beg)
45 i++;
49 /* render and highlight a line */
50 static char *led_render(char *s0, int cbeg, int cend, char *syn)
52 int n;
53 int *pos; /* pos[i]: the screen position of the i-th character */
54 int *off; /* off[i]: the character at screen position i */
55 int *att; /* att[i]: the attributes of i-th character */
56 char **chrs; /* chrs[i]: the i-th character in s1 */
57 int att_old = 0;
58 struct sbuf *out;
59 int i, j;
60 int ctx = dir_context(s0);
61 int att_blank = 0; /* the attribute of blank space */
62 chrs = uc_chop(s0, &n);
63 pos = ren_position(s0);
64 off = malloc((cend - cbeg) * sizeof(off[0]));
65 memset(off, 0xff, (cend - cbeg) * sizeof(off[0]));
66 /* initialise off[] using pos[] */
67 for (i = 0; i < n; i++) {
68 int curwid = ren_cwid(chrs[i], pos[i]);
69 int curbeg = led_pos(ctx, pos[i], cbeg, cend);
70 int curend = led_pos(ctx, pos[i] + curwid - 1, cbeg, cend);
71 if (curbeg >= 0 && curbeg < (cend - cbeg) &&
72 curend >= 0 && curend < (cend - cbeg))
73 for (j = 0; j < curwid; j++)
74 off[led_pos(ctx, pos[i] + j, cbeg, cend)] = i;
76 att = syn_highlight(n <= xlim ? syn : "", s0);
77 /* the attribute of the last character is used for blanks */
78 att_blank = n > 0 ? att[n - 1] : 0;
79 led_markrev(n, chrs, pos, att);
80 /* generate term output */
81 out = sbuf_make();
82 sbuf_str(out, conf_lnpref());
83 i = cbeg;
84 while (i < cend) {
85 int o = off[i - cbeg];
86 int att_new = o >= 0 ? att[o] : att_blank;
87 sbuf_str(out, term_att(att_new, att_old));
88 att_old = att_new;
89 if (o >= 0) {
90 if (ren_translate(chrs[o], s0))
91 sbuf_str(out, ren_translate(chrs[o], s0));
92 else if (uc_isprint(chrs[o]))
93 sbuf_mem(out, chrs[o], uc_len(chrs[o]));
94 else
95 for (j = i; j < cend && off[j - cbeg] == o; j++)
96 sbuf_chr(out, ' ');
97 while (i < cend && off[i - cbeg] == o)
98 i++;
99 } else {
100 sbuf_chr(out, ' ');
101 i++;
104 sbuf_str(out, term_att(0, att_old));
105 free(att);
106 free(pos);
107 free(off);
108 free(chrs);
109 return sbuf_done(out);
112 /* print a line on the screen */
113 void led_print(char *s, int row, int left, char *syn)
115 char *r = led_render(s, left, left + xcols, syn);
116 term_pos(row, 0);
117 term_kill();
118 term_str(r);
119 free(r);
122 /* set xtd and return its old value */
123 static int td_set(int td)
125 int old = xtd;
126 xtd = td;
127 return old;
130 /* print a line on the screen; for ex messages */
131 void led_printmsg(char *s, int row, char *syn)
133 int td = td_set(+2);
134 char *r = led_render(s, 0, xcols, syn);
135 td_set(td);
136 term_pos(row, 0);
137 term_kill();
138 term_str(r);
139 free(r);
142 static int led_lastchar(char *s)
144 char *r = *s ? strchr(s, '\0') : s;
145 if (r != s)
146 r = uc_beg(s, r - 1);
147 return r - s;
150 static int led_lastword(char *s)
152 char *r = *s ? uc_beg(s, strchr(s, '\0') - 1) : s;
153 int kind;
154 while (r > s && uc_isspace(r))
155 r = uc_beg(s, r - 1);
156 kind = r > s ? uc_kind(r) : 0;
157 while (r > s && uc_kind(uc_beg(s, r - 1)) == kind)
158 r = uc_beg(s, r - 1);
159 return r - s;
162 static void led_printparts(char *ai, char *pref, char *main,
163 char *post, int *left, int kmap, char *syn)
165 struct sbuf *ln;
166 int off, pos;
167 int idir = 0;
168 ln = sbuf_make();
169 sbuf_str(ln, ai);
170 sbuf_str(ln, pref);
171 sbuf_str(ln, main);
172 off = uc_slen(sbuf_buf(ln));
173 /* cursor position for inserting the next character */
174 if (*pref || *main || *ai) {
175 int len = sbuf_len(ln);
176 sbuf_str(ln, kmap_map(kmap, 'a'));
177 sbuf_str(ln, post);
178 idir = ren_pos(sbuf_buf(ln), off) -
179 ren_pos(sbuf_buf(ln), off - 1) < 0 ? -1 : +1;
180 sbuf_cut(ln, len);
182 term_record();
183 sbuf_str(ln, post);
184 pos = ren_cursor(sbuf_buf(ln), ren_pos(sbuf_buf(ln), MAX(0, off - 1)));
185 if (pos >= *left + xcols)
186 *left = pos - xcols / 2;
187 if (pos < *left)
188 *left = pos < xcols ? 0 : pos - xcols / 2;
189 led_print(sbuf_buf(ln), -1, *left, syn);
190 term_pos(-1, led_pos(dir_context(sbuf_buf(ln)), pos + idir, *left, *left + xcols));
191 sbuf_free(ln);
192 term_commit();
195 /* continue reading the character starting with c */
196 static char *led_readchar(int c, int kmap)
198 static char buf[8];
199 int c1, c2;
200 int i, n;
201 if (c == TK_CTL('v')) { /* literal character */
202 buf[0] = term_read();
203 buf[1] = '\0';
204 return buf;
206 if (c == TK_CTL('k')) { /* digraph */
207 c1 = term_read();
208 if (TK_INT(c1))
209 return NULL;
210 if (c1 == TK_CTL('k'))
211 return "";
212 c2 = term_read();
213 if (TK_INT(c2))
214 return NULL;
215 return conf_digraph(c1, c2);
217 if ((c & 0xc0) == 0xc0) { /* utf-8 character */
218 buf[0] = c;
219 n = uc_len(buf);
220 for (i = 1; i < n; i++)
221 buf[i] = term_read();
222 buf[n] = '\0';
223 return buf;
225 return kmap_map(kmap, c);
228 /* read a character from the terminal */
229 char *led_read(int *kmap)
231 int c = term_read();
232 while (!TK_INT(c)) {
233 switch (c) {
234 case TK_CTL('f'):
235 *kmap = xkmap_alt;
236 break;
237 case TK_CTL('e'):
238 *kmap = 0;
239 break;
240 default:
241 return led_readchar(c, *kmap);
243 c = term_read();
245 return NULL;
248 static int led_match(char *out, int len, char *kwd, char *opt)
250 while (opt != NULL) {
251 int i = 0;
252 while (kwd[i] && kwd[i] == opt[i])
253 i++;
254 if (kwd[i] == '\0')
255 break;
256 opt = strchr(opt, '\n') == NULL ? NULL : strchr(opt, '\n') + 1;
258 out[0] = '\0';
259 if (opt != NULL) {
260 int i = 0;
261 char *beg = opt + strlen(kwd);
262 while (beg[i] && beg[i] != '\n' && i + 8 < len)
263 i += uc_len(beg + i);
264 memcpy(out, beg, i);
265 out[i] = '\0';
266 return 0;
268 return 1;
271 /* read a line from the terminal */
272 static char *led_line(char *pref, char *post, char *ai,
273 int ai_max, int *left, int *key, int *kmap, char *syn, char *hist)
275 struct sbuf *sb;
276 int ai_len = strlen(ai);
277 int c, y, lnmode;
278 char cmp[64] = "";
279 char *cs;
280 sb = sbuf_make();
281 if (pref == NULL)
282 pref = "";
283 if (post == NULL || !post[0])
284 post = cmp;
285 while (1) {
286 if (hist != NULL)
287 led_match(cmp, sizeof(cmp), sbuf_buf(sb), hist);
288 led_printparts(ai, pref, sbuf_buf(sb), post, left, *kmap, syn);
289 c = term_read();
290 switch (c) {
291 case TK_CTL('f'):
292 *kmap = xkmap_alt;
293 continue;
294 case TK_CTL('e'):
295 *kmap = 0;
296 continue;
297 case TK_CTL('h'):
298 case 127:
299 if (sbuf_len(sb))
300 sbuf_cut(sb, led_lastchar(sbuf_buf(sb)));
301 break;
302 case TK_CTL('u'):
303 sbuf_cut(sb, 0);
304 break;
305 case TK_CTL('w'):
306 if (sbuf_len(sb))
307 sbuf_cut(sb, led_lastword(sbuf_buf(sb)));
308 break;
309 case TK_CTL('t'):
310 if (ai_len < ai_max) {
311 ai[ai_len++] = '\t';
312 ai[ai_len] = '\0';
314 break;
315 case TK_CTL('d'):
316 /* when ai and pref are empty, remove the first space of sb */
317 if (ai_len == 0 && !pref[0]) {
318 char *buf = sbuf_buf(sb);
319 if (buf[0] == ' ' || buf[0] == '\t') {
320 char *dup = uc_dup(buf + 1);
321 sbuf_cut(sb, 0);
322 sbuf_str(sb, dup);
323 free(dup);
326 if (ai_len > 0)
327 ai[--ai_len] = '\0';
328 break;
329 case TK_CTL('p'):
330 if (reg_get(0, &lnmode))
331 sbuf_str(sb, reg_get(0, &lnmode));
332 break;
333 case TK_CTL('r'):
334 y = term_read();
335 if (y > 0 && reg_get(y, &lnmode))
336 sbuf_str(sb, reg_get(y, &lnmode));
337 break;
338 case TK_CTL('a'):
339 sbuf_str(sb, cmp);
340 break;
341 default:
342 if (c == '\n' || TK_INT(c))
343 break;
344 if ((cs = led_readchar(c, *kmap)) != NULL)
345 sbuf_str(sb, cs);
347 if (c == '\n')
348 led_printparts(ai, pref, sbuf_buf(sb), "", left, *kmap, syn);
349 if (c == '\n' || TK_INT(c))
350 break;
352 *key = c;
353 return sbuf_done(sb);
356 /* read an ex command */
357 char *led_prompt(char *pref, char *post, int *kmap, char *syn, char *hist)
359 int key;
360 int td = td_set(+2);
361 int left = 0;
362 char *s = led_line(pref, post, "", 0, &left, &key, kmap, syn, hist);
363 td_set(td);
364 if (key == '\n') {
365 struct sbuf *sb = sbuf_make();
366 if (pref)
367 sbuf_str(sb, pref);
368 sbuf_str(sb, s);
369 if (post)
370 sbuf_str(sb, post);
371 free(s);
372 return sbuf_done(sb);
374 free(s);
375 return NULL;
378 static int linecount(char *s)
380 int n;
381 for (n = 0; s; n++)
382 if ((s = strchr(s, '\n')))
383 s++;
384 return n;
387 /* read visual command input */
388 char *led_input(char *pref, char *post, int *left, int *kmap, char *syn, void (*nextline)(void))
390 struct sbuf *sb = sbuf_make();
391 char ai[128];
392 int ai_max = sizeof(ai) - 1;
393 int n = 0;
394 int key;
395 while (n < ai_max && (*pref == ' ' || *pref == '\t'))
396 ai[n++] = *pref++;
397 ai[n] = '\0';
398 while (1) {
399 char *ln = led_line(pref, post, ai, ai_max, left, &key, kmap, syn, NULL);
400 int ln_sp = 0; /* number of initial spaces in ln */
401 int lncnt = linecount(ln) - 1 + (key == '\n');
402 while (ln[ln_sp] && (ln[ln_sp] == ' ' || ln[ln_sp] == '\t'))
403 ln_sp++;
404 /* append the auto-indent only if there are other characters */
405 if (ln[ln_sp] || (pref && pref[0]) ||
406 (key != '\n' && post[0] && post[0] != '\n'))
407 sbuf_str(sb, ai);
408 if (pref)
409 sbuf_str(sb, pref);
410 sbuf_str(sb, ln);
411 if (key == '\n')
412 sbuf_chr(sb, '\n');
413 while (lncnt-- > 0)
414 nextline();
415 if (!pref || !pref[0]) { /* updating autoindent */
416 int ai_len = ai_max ? strlen(ai) : 0;
417 int ai_new = ln_sp;
418 if (ai_len + ai_new > ai_max)
419 ai_new = ai_max - ai_len;
420 memcpy(ai + ai_len, ln, ai_new);
421 ai[ai_len + ai_new] = '\0';
423 if (!xai)
424 ai[0] = '\0';
425 free(ln);
426 if (key != '\n')
427 break;
428 pref = NULL;
429 n = 0;
430 while (xai && (post[n] == ' ' || post[n] == '\t'))
431 n++;
432 memmove(post, post + n, strlen(post) - n + 1);
434 sbuf_str(sb, post);
435 if (TK_INT(key))
436 return sbuf_done(sb);
437 sbuf_free(sb);
438 return NULL;