led: indenting and deindenting when auto-indent buffer is empty
[neatvi.git] / led.c
blobbd4500f6d9d42276e6fca8a7ecbeb945b49b71cc
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_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 = conf_hlrev();
42 while (i + 1 < n) {
43 int dir = led_offdir(chrs, pos, i);
44 int beg = i;
45 while (i + 1 < n && led_offdir(chrs, pos, i) == dir)
46 i++;
47 if (dir < 0)
48 for (j = beg; j <= i; j++)
49 att[j] = syn_merge(hl, att[j]);
50 if (i == beg)
51 i++;
55 /* render and highlight a line */
56 static char *led_render(char *s0, int cbeg, int cend, char *syn)
58 int n;
59 int *pos; /* pos[i]: the screen position of the i-th character */
60 int *off; /* off[i]: the character at screen position i */
61 int *att; /* att[i]: the attributes of i-th character */
62 char **chrs; /* chrs[i]: the i-th character in s1 */
63 int att_old = 0;
64 struct sbuf *out;
65 int i, j;
66 int ctx = dir_context(s0);
67 int att_blank = 0; /* the attribute of blank space */
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 /* initialise off[] using pos[] */
73 for (i = 0; i < n; i++) {
74 int curwid = ren_cwid(chrs[i], pos[i]);
75 int curbeg = led_posctx(ctx, pos[i], cbeg, cend);
76 int curend = led_posctx(ctx, pos[i] + curwid - 1, cbeg, cend);
77 if (curbeg >= 0 && curbeg < (cend - cbeg) &&
78 curend >= 0 && curend < (cend - cbeg))
79 for (j = 0; j < curwid; j++)
80 off[led_posctx(ctx, pos[i] + j, cbeg, cend)] = i;
82 att = syn_highlight(xhl ? syn : "", s0);
83 /* the attribute of \n character is used for blanks */
84 for (i = 0; i < n; i++)
85 if (chrs[i][0] == '\n')
86 att_blank = att[i];
87 led_markrev(n, chrs, pos, att);
88 /* generate term output */
89 out = sbuf_make();
90 i = cbeg;
91 while (i < cend) {
92 int o = off[i - cbeg];
93 int att_new = o >= 0 ? att[o] : att_blank;
94 sbuf_str(out, term_att(att_new, att_old));
95 att_old = att_new;
96 if (o >= 0) {
97 if (ren_translate(chrs[o], s0))
98 sbuf_str(out, ren_translate(chrs[o], s0));
99 else if (uc_isprint(chrs[o]))
100 sbuf_mem(out, chrs[o], uc_len(chrs[o]));
101 else
102 for (j = i; j < cend && off[j - cbeg] == o; j++)
103 sbuf_chr(out, ' ');
104 while (i < cend && off[i - cbeg] == o)
105 i++;
106 } else {
107 sbuf_chr(out, ' ');
108 i++;
111 sbuf_str(out, term_att(0, att_old));
112 free(att);
113 free(pos);
114 free(off);
115 free(chrs);
116 return sbuf_done(out);
119 /* print a line on the screen */
120 void led_print(char *s, int row, char *syn)
122 char *r = led_render(s, xleft, xleft + xcols, syn);
123 term_pos(row, 0);
124 term_kill();
125 term_str(r);
126 free(r);
129 /* set xtd and return its old value */
130 static int td_set(int td)
132 int old = xtd;
133 xtd = td;
134 return old;
137 /* print a line on the screen; for ex messages */
138 void led_printmsg(char *s, int row, char *syn)
140 int td = td_set(+2);
141 char *r = led_render(s, xleft, xleft + xcols, syn);
142 td_set(td);
143 term_pos(row, 0);
144 term_kill();
145 term_str(r);
146 free(r);
149 static int led_lastchar(char *s)
151 char *r = *s ? strchr(s, '\0') : s;
152 if (r != s)
153 r = uc_beg(s, r - 1);
154 return r - s;
157 static int led_lastword(char *s)
159 char *r = *s ? uc_beg(s, strchr(s, '\0') - 1) : s;
160 int kind;
161 while (r > s && uc_isspace(r))
162 r = uc_beg(s, r - 1);
163 kind = r > s ? uc_kind(r) : 0;
164 while (r > s && uc_kind(uc_beg(s, r - 1)) == kind)
165 r = uc_beg(s, r - 1);
166 return r - s;
169 static void led_printparts(char *ai, char *pref, char *main,
170 char *post, int kmap, char *syn)
172 struct sbuf *ln;
173 int off, pos;
174 int idir = 0;
175 ln = sbuf_make();
176 sbuf_str(ln, ai);
177 sbuf_str(ln, pref);
178 sbuf_str(ln, main);
179 off = uc_slen(sbuf_buf(ln));
180 /* cursor position for inserting the next character */
181 if (*pref || *main || *ai) {
182 int len = sbuf_len(ln);
183 sbuf_str(ln, kmap_map(kmap, 'a'));
184 sbuf_str(ln, post);
185 idir = ren_pos(sbuf_buf(ln), off) -
186 ren_pos(sbuf_buf(ln), off - 1) < 0 ? -1 : +1;
187 sbuf_cut(ln, len);
189 term_record();
190 sbuf_str(ln, post);
191 pos = ren_cursor(sbuf_buf(ln), ren_pos(sbuf_buf(ln), MAX(0, off - 1)));
192 if (pos >= xleft + xcols)
193 xleft = pos - xcols / 2;
194 if (pos < xleft)
195 xleft = pos < xcols ? 0 : pos - xcols / 2;
196 led_print(sbuf_buf(ln), -1, syn);
197 term_pos(-1, led_pos(sbuf_buf(ln), pos + idir));
198 sbuf_free(ln);
199 term_commit();
202 /* continue reading the character starting with c */
203 static char *led_readchar(int c, int kmap)
205 static char buf[8];
206 int c1, c2;
207 int i, n;
208 if (c == TK_CTL('v')) { /* literal character */
209 buf[0] = term_read();
210 buf[1] = '\0';
211 return buf;
213 if (c == TK_CTL('k')) { /* digraph */
214 c1 = term_read();
215 if (TK_INT(c1))
216 return NULL;
217 c2 = term_read();
218 if (TK_INT(c2))
219 return NULL;
220 return conf_digraph(c1, c2);
222 if ((c & 0xc0) == 0xc0) { /* utf-8 character */
223 buf[0] = c;
224 n = uc_len(buf);
225 for (i = 1; i < n; i++)
226 buf[i] = term_read();
227 buf[n] = '\0';
228 return buf;
230 return kmap_map(kmap, c);
233 /* read a character from the terminal */
234 char *led_read(int *kmap)
236 int c = term_read();
237 while (!TK_INT(c)) {
238 switch (c) {
239 case TK_CTL('f'):
240 *kmap = xkmap_alt;
241 break;
242 case TK_CTL('e'):
243 *kmap = 0;
244 break;
245 default:
246 return led_readchar(c, *kmap);
248 c = term_read();
250 return NULL;
253 /* read a line from the terminal */
254 static char *led_line(char *pref, char *post, char *ai,
255 int ai_max, int *key, int *kmap, char *syn)
257 struct sbuf *sb;
258 int ai_len = strlen(ai);
259 int c, lnmode;
260 char *cs;
261 sb = sbuf_make();
262 if (!pref)
263 pref = "";
264 if (!post)
265 post = "";
266 while (1) {
267 led_printparts(ai, pref, sbuf_buf(sb), post, *kmap, syn);
268 c = term_read();
269 switch (c) {
270 case TK_CTL('f'):
271 *kmap = xkmap_alt;
272 continue;
273 case TK_CTL('e'):
274 *kmap = 0;
275 continue;
276 case TK_CTL('h'):
277 case 127:
278 if (sbuf_len(sb))
279 sbuf_cut(sb, led_lastchar(sbuf_buf(sb)));
280 break;
281 case TK_CTL('u'):
282 sbuf_cut(sb, 0);
283 break;
284 case TK_CTL('w'):
285 if (sbuf_len(sb))
286 sbuf_cut(sb, led_lastword(sbuf_buf(sb)));
287 break;
288 case TK_CTL('t'):
289 if (ai_len < ai_max)
290 ai[ai_len++] = '\t';
291 ai[ai_len] = '\0';
292 break;
293 case TK_CTL('d'):
294 /* when ai and pref are empty, remove the first space of sb */
295 if (ai_len == 0 && !pref[0]) {
296 char *buf = sbuf_buf(sb);
297 if (buf[0] == ' ' || buf[0] == '\t') {
298 char *dup = uc_dup(buf + 1);
299 sbuf_cut(sb, 0);
300 sbuf_str(sb, dup);
301 free(dup);
304 if (ai_len > 0)
305 ai[--ai_len] = '\0';
306 break;
307 case TK_CTL('p'):
308 if (reg_get(0, &lnmode))
309 sbuf_str(sb, reg_get(0, &lnmode));
310 break;
311 default:
312 if (c == '\n' || TK_INT(c))
313 break;
314 if ((cs = led_readchar(c, *kmap)))
315 sbuf_str(sb, cs);
317 if (c == '\n' || TK_INT(c))
318 break;
320 *key = c;
321 return sbuf_done(sb);
324 /* read an ex command */
325 char *led_prompt(char *pref, char *post, int *kmap, char *syn)
327 int key;
328 int td = td_set(+2);
329 char *s = led_line(pref, post, "", 0, &key, kmap, syn);
330 td_set(td);
331 if (key == '\n') {
332 struct sbuf *sb = sbuf_make();
333 if (pref)
334 sbuf_str(sb, pref);
335 sbuf_str(sb, s);
336 if (post)
337 sbuf_str(sb, post);
338 free(s);
339 return sbuf_done(sb);
341 free(s);
342 return NULL;
345 /* read visual command input */
346 char *led_input(char *pref, char *post, int *kmap, char *syn)
348 struct sbuf *sb = sbuf_make();
349 char ai[128];
350 int ai_max = sizeof(ai) - 1;
351 int n = 0;
352 int key;
353 while (n < ai_max && (*pref == ' ' || *pref == '\t'))
354 ai[n++] = *pref++;
355 ai[n] = '\0';
356 while (1) {
357 char *ln = led_line(pref, post, ai, ai_max, &key, kmap, syn);
358 int ln_sp = 0; /* number of initial spaces in ln */
359 while (ln[ln_sp] && (ln[ln_sp] == ' ' || ln[ln_sp] == '\t'))
360 ln_sp++;
361 /* append the auto-indent only if there are other characters */
362 if (ln[ln_sp] || (pref && pref[0]) ||
363 (key != '\n' && post[0] && post[0] != '\n'))
364 sbuf_str(sb, ai);
365 if (pref)
366 sbuf_str(sb, pref);
367 sbuf_str(sb, ln);
368 if (key == '\n')
369 sbuf_chr(sb, '\n');
370 led_printparts(ai, pref ? pref : "", uc_lastline(ln),
371 key == '\n' ? "" : post, *kmap, syn);
372 if (key == '\n')
373 term_chr('\n');
374 if (!pref || !pref[0]) { /* updating autoindent */
375 int ai_len = ai_max ? strlen(ai) : 0;
376 int ai_new = ln_sp;
377 if (ai_len + ai_new > ai_max)
378 ai_new = ai_max - ai_len;
379 memcpy(ai + ai_len, ln, ai_new);
380 ai[ai_len + ai_new] = '\0';
382 if (!xai)
383 ai[0] = '\0';
384 free(ln);
385 if (key != '\n')
386 break;
387 term_room(1);
388 pref = NULL;
389 n = 0;
390 while (xai && (post[n] == ' ' || post[n] == '\t'))
391 n++;
392 memmove(post, post + n, strlen(post) - n + 1);
394 sbuf_str(sb, post);
395 if (TK_INT(key))
396 return sbuf_done(sb);
397 sbuf_free(sb);
398 return NULL;