1 /* line editing and drawing */
9 static char *kmap_map(int kmap
, int c
)
12 char **keymap
= conf_kmap(kmap
);
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])
32 if (pos
[i
+ 1] + ren_cwid(chrs
[i
+ 1], pos
[i
+ 1]) == pos
[i
])
37 /* highlight text in reverse direction */
38 static void led_markrev(int n
, char **chrs
, int *pos
, int *att
)
41 int hl
= conf_hlrev();
43 int dir
= led_offdir(chrs
, pos
, i
);
45 while (i
+ 1 < n
&& led_offdir(chrs
, pos
, i
) == dir
)
48 for (j
= beg
; j
<= i
; j
++)
49 att
[j
] = syn_merge(hl
, att
[j
]);
55 /* render and highlight a line */
56 static char *led_render(char *s0
, int cbeg
, int cend
, char *syn
)
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 */
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(n
<= xlim
? syn
: "", s0
);
83 /* the attribute of the last character is used for blanks */
84 att_blank
= n
> 0 ? att
[n
- 1] : 0;
85 led_markrev(n
, chrs
, pos
, att
);
86 /* generate term output */
88 sbuf_str(out
, conf_lnpref());
91 int o
= off
[i
- cbeg
];
92 int att_new
= o
>= 0 ? att
[o
] : att_blank
;
93 sbuf_str(out
, term_att(att_new
, att_old
));
96 if (ren_translate(chrs
[o
], s0
))
97 sbuf_str(out
, ren_translate(chrs
[o
], s0
));
98 else if (uc_isprint(chrs
[o
]))
99 sbuf_mem(out
, chrs
[o
], uc_len(chrs
[o
]));
101 for (j
= i
; j
< cend
&& off
[j
- cbeg
] == o
; j
++)
103 while (i
< cend
&& off
[i
- cbeg
] == o
)
110 sbuf_str(out
, term_att(0, att_old
));
115 return sbuf_done(out
);
118 /* print a line on the screen */
119 void led_print(char *s
, int row
, char *syn
)
121 char *r
= led_render(s
, xleft
, xleft
+ xcols
, syn
);
128 /* set xtd and return its old value */
129 static int td_set(int td
)
136 /* print a line on the screen; for ex messages */
137 void led_printmsg(char *s
, int row
, char *syn
)
140 char *r
= led_render(s
, 0, xcols
, syn
);
148 static int led_lastchar(char *s
)
150 char *r
= *s
? strchr(s
, '\0') : s
;
152 r
= uc_beg(s
, r
- 1);
156 static int led_lastword(char *s
)
158 char *r
= *s
? uc_beg(s
, strchr(s
, '\0') - 1) : s
;
160 while (r
> s
&& uc_isspace(r
))
161 r
= uc_beg(s
, r
- 1);
162 kind
= r
> s
? uc_kind(r
) : 0;
163 while (r
> s
&& uc_kind(uc_beg(s
, r
- 1)) == kind
)
164 r
= uc_beg(s
, r
- 1);
168 static void led_printparts(char *ai
, char *pref
, char *main
,
169 char *post
, int kmap
, char *syn
)
178 off
= uc_slen(sbuf_buf(ln
));
179 /* cursor position for inserting the next character */
180 if (*pref
|| *main
|| *ai
) {
181 int len
= sbuf_len(ln
);
182 sbuf_str(ln
, kmap_map(kmap
, 'a'));
184 idir
= ren_pos(sbuf_buf(ln
), off
) -
185 ren_pos(sbuf_buf(ln
), off
- 1) < 0 ? -1 : +1;
190 pos
= ren_cursor(sbuf_buf(ln
), ren_pos(sbuf_buf(ln
), MAX(0, off
- 1)));
191 if (pos
>= xleft
+ xcols
)
192 xleft
= pos
- xcols
/ 2;
194 xleft
= pos
< xcols
? 0 : pos
- xcols
/ 2;
195 led_print(sbuf_buf(ln
), -1, syn
);
196 term_pos(-1, led_pos(sbuf_buf(ln
), pos
+ idir
));
201 /* continue reading the character starting with c */
202 static char *led_readchar(int c
, int kmap
)
207 if (c
== TK_CTL('v')) { /* literal character */
208 buf
[0] = term_read();
212 if (c
== TK_CTL('k')) { /* digraph */
216 if (c1
== TK_CTL('k'))
221 return conf_digraph(c1
, c2
);
223 if ((c
& 0xc0) == 0xc0) { /* utf-8 character */
226 for (i
= 1; i
< n
; i
++)
227 buf
[i
] = term_read();
231 return kmap_map(kmap
, c
);
234 /* read a character from the terminal */
235 char *led_read(int *kmap
)
247 return led_readchar(c
, *kmap
);
254 static int led_match(char *out
, int len
, char *kwd
, char *opt
)
256 while (opt
!= NULL
) {
258 while (kwd
[i
] && kwd
[i
] == opt
[i
])
262 opt
= strchr(opt
, '\n') == NULL
? NULL
: strchr(opt
, '\n') + 1;
267 char *beg
= opt
+ strlen(kwd
);
268 while (beg
[i
] && beg
[i
] != '\n' && i
+ 8 < len
)
269 i
+= uc_len(beg
+ i
);
277 /* read a line from the terminal */
278 static char *led_line(char *pref
, char *post
, char *ai
,
279 int ai_max
, int *key
, int *kmap
, char *syn
, char *hist
)
282 int ai_len
= strlen(ai
);
289 if (post
== NULL
|| !post
[0])
293 led_match(cmp
, sizeof(cmp
), sbuf_buf(sb
), hist
);
294 led_printparts(ai
, pref
, sbuf_buf(sb
), post
, *kmap
, syn
);
306 sbuf_cut(sb
, led_lastchar(sbuf_buf(sb
)));
313 sbuf_cut(sb
, led_lastword(sbuf_buf(sb
)));
316 if (ai_len
< ai_max
) {
322 /* when ai and pref are empty, remove the first space of sb */
323 if (ai_len
== 0 && !pref
[0]) {
324 char *buf
= sbuf_buf(sb
);
325 if (buf
[0] == ' ' || buf
[0] == '\t') {
326 char *dup
= uc_dup(buf
+ 1);
336 if (reg_get(0, &lnmode
))
337 sbuf_str(sb
, reg_get(0, &lnmode
));
341 if (y
> 0 && reg_get(y
, &lnmode
))
342 sbuf_str(sb
, reg_get(y
, &lnmode
));
348 if (c
== '\n' || TK_INT(c
))
350 if ((cs
= led_readchar(c
, *kmap
)) != NULL
)
354 led_printparts(ai
, pref
, sbuf_buf(sb
), "", *kmap
, syn
);
355 if (c
== '\n' || TK_INT(c
))
359 return sbuf_done(sb
);
362 /* read an ex command */
363 char *led_prompt(char *pref
, char *post
, int *kmap
, char *syn
, char *hist
)
368 char *s
= led_line(pref
, post
, "", 0, &key
, kmap
, syn
, hist
);
372 struct sbuf
*sb
= sbuf_make();
379 return sbuf_done(sb
);
385 /* read visual command input; may update xleft */
386 char *led_input(char *pref
, char *post
, int *kmap
, char *syn
)
388 struct sbuf
*sb
= sbuf_make();
390 int ai_max
= sizeof(ai
) - 1;
393 while (n
< ai_max
&& (*pref
== ' ' || *pref
== '\t'))
397 char *ln
= led_line(pref
, post
, ai
, ai_max
, &key
, kmap
, syn
, NULL
);
398 int ln_sp
= 0; /* number of initial spaces in ln */
399 while (ln
[ln_sp
] && (ln
[ln_sp
] == ' ' || ln
[ln_sp
] == '\t'))
401 /* append the auto-indent only if there are other characters */
402 if (ln
[ln_sp
] || (pref
&& pref
[0]) ||
403 (key
!= '\n' && post
[0] && post
[0] != '\n'))
412 if (!pref
|| !pref
[0]) { /* updating autoindent */
413 int ai_len
= ai_max
? strlen(ai
) : 0;
415 if (ai_len
+ ai_new
> ai_max
)
416 ai_new
= ai_max
- ai_len
;
417 memcpy(ai
+ ai_len
, ln
, ai_new
);
418 ai
[ai_len
+ ai_new
] = '\0';
428 while (xai
&& (post
[n
] == ' ' || post
[n
] == '\t'))
430 memmove(post
, post
+ n
, strlen(post
) - n
+ 1);
434 return sbuf_done(sb
);