dir: rename xdir to xtd
[neatvi.git] / led.c
bloba08bd00813cea5bef51b8473a68cbb2898188a6a
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(char *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_posctx(int dir, int pos, int beg, int end)
19 return dir >= 0 ? pos - beg : end - pos - 1;
22 /* map cursor horizontal position to terminal column number */
23 int led_pos(char *s, int pos)
25 return led_posctx(dir_context(s), pos, xleft, xleft + xcols);
28 static int led_offdir(char **chrs, int *pos, int i)
30 if (pos[i] + ren_cwid(chrs[i], pos[i]) == pos[i + 1])
31 return +1;
32 if (pos[i + 1] + ren_cwid(chrs[i + 1], pos[i + 1]) == pos[i])
33 return -1;
34 return 0;
37 /* highlight text in reverse direction */
38 static void led_markrev(int n, char **chrs, int *pos, int *att)
40 int i = 0, j;
41 int hl = 0;
42 conf_highlight_revdir(&hl);
43 while (i + 1 < n) {
44 int dir = led_offdir(chrs, pos, i);
45 int beg = i;
46 while (i + 1 < n && led_offdir(chrs, pos, i) == dir)
47 i++;
48 if (dir < 0)
49 for (j = beg; j <= i; j++)
50 att[j] = syn_merge(hl, att[j]);
51 if (i == beg)
52 i++;
56 /* render and highlight a line */
57 static char *led_render(char *s0, int cbeg, int cend, char *syn)
59 int n;
60 int *pos; /* pos[i]: the screen position of the i-th character */
61 int *off; /* off[i]: the character at screen position i */
62 int *att; /* att[i]: the attributes of i-th character */
63 char **chrs; /* chrs[i]: the i-th character in s1 */
64 int att_old = 0;
65 struct sbuf *out;
66 int i, j;
67 int ctx = dir_context(s0);
68 chrs = uc_chop(s0, &n);
69 pos = ren_position(s0);
70 off = malloc((cend - cbeg) * sizeof(off[0]));
71 memset(off, 0xff, (cend - cbeg) * sizeof(off[0]));
72 for (i = 0; i < n; i++) {
73 int curwid = ren_cwid(chrs[i], pos[i]);
74 int curbeg = led_posctx(ctx, pos[i], cbeg, cend);
75 int curend = led_posctx(ctx, pos[i] + curwid - 1, cbeg, cend);
76 if (curbeg >= 0 && curbeg < (cend - cbeg) &&
77 curend >= 0 && curend < (cend - cbeg))
78 for (j = 0; j < curwid; j++)
79 off[led_posctx(ctx, pos[i] + j, cbeg, cend)] = i;
81 att = syn_highlight(syn, s0);
82 led_markrev(n, chrs, pos, att);
83 out = sbuf_make();
84 i = cbeg;
85 while (i < cend) {
86 int o = off[i - cbeg];
87 int att_new = o >= 0 ? att[o] : 0;
88 sbuf_str(out, term_att(att_new, att_old));
89 att_old = att_new;
90 if (o >= 0) {
91 if (ren_translate(chrs[o], s0))
92 sbuf_str(out, ren_translate(chrs[o], s0));
93 else if (uc_isprint(chrs[o]))
94 sbuf_mem(out, chrs[o], uc_len(chrs[o]));
95 else
96 for (j = i; j < cend && off[j - cbeg] == o; j++)
97 sbuf_chr(out, ' ');
98 while (i < cend && off[i - cbeg] == o)
99 i++;
100 } else {
101 sbuf_chr(out, ' ');
102 i++;
105 sbuf_str(out, term_att(0, att_old));
106 free(att);
107 free(pos);
108 free(off);
109 free(chrs);
110 return sbuf_done(out);
113 /* print a line on the screen */
114 void led_print(char *s, int row)
116 char *r = led_render(s, xleft, xleft + xcols, ex_filetype());
117 term_pos(row, 0);
118 term_kill();
119 term_str(r);
120 free(r);
123 /* set xtd and return its old value */
124 static int td_set(int td)
126 int old = xtd;
127 xtd = td;
128 return old;
131 /* print a line on the screen; for ex messages */
132 void led_printmsg(char *s, int row)
134 int td = td_set(+2);
135 char *r = led_render(s, xleft, xleft + xcols, "---");
136 td_set(td);
137 term_pos(row, 0);
138 term_kill();
139 term_str(r);
140 free(r);
143 static int led_lastchar(char *s)
145 char *r = *s ? strchr(s, '\0') : s;
146 if (r != s)
147 r = uc_beg(s, r - 1);
148 return r - s;
151 static int led_lastword(char *s)
153 char *r = *s ? uc_beg(s, strchr(s, '\0') - 1) : s;
154 int kind;
155 while (r > s && uc_isspace(r))
156 r = uc_beg(s, r - 1);
157 kind = r > s ? uc_kind(r) : 0;
158 while (r > s && uc_kind(uc_beg(s, r - 1)) == kind)
159 r = uc_beg(s, r - 1);
160 return r - s;
163 static void led_printparts(char *ai, char *pref, char *main, char *post, char *kmap)
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 >= xleft + xcols)
186 xleft = pos - xcols / 2;
187 if (pos < xleft)
188 xleft = pos < xcols ? 0 : pos - xcols / 2;
189 led_print(sbuf_buf(ln), -1);
190 term_pos(-1, led_pos(sbuf_buf(ln), pos + idir));
191 sbuf_free(ln);
192 term_commit();
195 /* continue reading the character starting with c */
196 static char *led_readchar(int c, char *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 c2 = term_read();
211 if (TK_INT(c2))
212 return NULL;
213 return conf_digraph(c1, c2);
215 if ((c & 0xc0) == 0xc0) { /* utf-8 character */
216 buf[0] = c;
217 n = uc_len(buf);
218 for (i = 1; i < n; i++)
219 buf[i] = term_read();
220 buf[n] = '\0';
221 return buf;
223 return kmap_map(kmap, c);
226 /* read a character from the terminal */
227 char *led_read(char **kmap)
229 int c = term_read();
230 while (!TK_INT(c)) {
231 switch (c) {
232 case TK_CTL('f'):
233 *kmap = ex_kmapalt();
234 break;
235 case TK_CTL('e'):
236 *kmap = "en";
237 break;
238 default:
239 return led_readchar(c, *kmap);
241 c = term_read();
243 return NULL;
246 /* read a line from the terminal */
247 static char *led_line(char *pref, char *post, char *ai, int ai_max, int *key, char **kmap)
249 struct sbuf *sb;
250 int ai_len = strlen(ai);
251 int c, lnmode;
252 char *cs;
253 sb = sbuf_make();
254 if (!pref)
255 pref = "";
256 if (!post)
257 post = "";
258 while (1) {
259 led_printparts(ai, pref, sbuf_buf(sb), post, *kmap);
260 c = term_read();
261 switch (c) {
262 case TK_CTL('f'):
263 *kmap = ex_kmapalt();
264 continue;
265 case TK_CTL('e'):
266 *kmap = "en";
267 continue;
268 case TK_CTL('h'):
269 case 127:
270 if (sbuf_len(sb))
271 sbuf_cut(sb, led_lastchar(sbuf_buf(sb)));
272 break;
273 case TK_CTL('u'):
274 sbuf_cut(sb, 0);
275 break;
276 case TK_CTL('w'):
277 if (sbuf_len(sb))
278 sbuf_cut(sb, led_lastword(sbuf_buf(sb)));
279 break;
280 case TK_CTL('t'):
281 if (ai_len < ai_max)
282 ai[ai_len++] = '\t';
283 ai[ai_len] = '\0';
284 break;
285 case TK_CTL('d'):
286 if (ai_len > 0)
287 ai[--ai_len] = '\0';
288 break;
289 case TK_CTL('p'):
290 if (reg_get(0, &lnmode))
291 sbuf_str(sb, reg_get(0, &lnmode));
292 break;
293 default:
294 if (c == '\n' || TK_INT(c))
295 break;
296 if ((cs = led_readchar(c, *kmap)))
297 sbuf_str(sb, cs);
299 if (c == '\n' || TK_INT(c))
300 break;
302 *key = c;
303 return sbuf_done(sb);
306 /* read an ex command */
307 char *led_prompt(char *pref, char *post, char **kmap)
309 int key;
310 int td = td_set(+2);
311 char *s = led_line(pref, post, "", 0, &key, kmap);
312 td_set(td);
313 if (key == '\n') {
314 struct sbuf *sb = sbuf_make();
315 if (pref)
316 sbuf_str(sb, pref);
317 sbuf_str(sb, s);
318 if (post)
319 sbuf_str(sb, post);
320 free(s);
321 return sbuf_done(sb);
323 free(s);
324 return NULL;
327 /* read visual command input */
328 char *led_input(char *pref, char *post, char **kmap)
330 struct sbuf *sb = sbuf_make();
331 char ai[128];
332 int ai_max = xai ? sizeof(ai) - 1 : 0;
333 int n = 0;
334 int key;
335 while (n < ai_max && (*pref == ' ' || *pref == '\t'))
336 ai[n++] = *pref++;
337 ai[n] = '\0';
338 while (1) {
339 char *ln = led_line(pref, post, ai, ai_max, &key, kmap);
340 int ln_sp = 0; /* number of initial spaces in ln */
341 while (ln[ln_sp] && (ln[ln_sp] == ' ' || ln[ln_sp] == '\t'))
342 ln_sp++;
343 if (ln[ln_sp] || (pref && pref[0]) ||
344 (key != '\n' && post[0] && post[0] != '\n'))
345 sbuf_str(sb, ai);
346 if (pref)
347 sbuf_str(sb, pref);
348 sbuf_str(sb, ln);
349 if (key == '\n')
350 sbuf_chr(sb, '\n');
351 led_printparts(ai, pref ? pref : "", uc_lastline(ln),
352 key == '\n' ? "" : post, *kmap);
353 if (key == '\n')
354 term_chr('\n');
355 if (!pref || !pref[0]) { /* updating autoindent */
356 int ai_len = ai_max ? strlen(ai) : 0;
357 int ai_new = ln_sp;
358 if (ai_len + ai_new > ai_max)
359 ai_new = ai_max - ai_len;
360 memcpy(ai + ai_len, ln, ai_new);
361 ai[ai_len + ai_new] = '\0';
363 free(ln);
364 if (key != '\n')
365 break;
366 term_room(1);
367 pref = NULL;
368 n = 0;
369 while (xai && (post[n] == ' ' || post[n] == '\t'))
370 n++;
371 memmove(post, post + n, strlen(post) - n + 1);
373 sbuf_str(sb, post);
374 if (TK_INT(key))
375 return sbuf_done(sb);
376 sbuf_free(sb);
377 return NULL;