8 #define TK_STOP(c) ((c) < 0 || (c) == TK_ESC || (c) == TK_CTL('c'))
10 static char **led_kmap
= kmap_def
;
12 static char *keymap(char **kmap
, int c
)
16 return kmap
[c
] ? kmap
[c
] : cs
;
19 /* map cursor horizontal position to terminal column number */
20 int led_pos(char *s
, int pos
)
22 return dir_context(s
) >= 0 ? pos
: xcols
- pos
- 1;
25 char *led_keymap(int c
)
27 return c
>= 0 ? keymap(led_kmap
, c
) : NULL
;
30 static char *led_render(char *s0
)
33 int *pos
; /* pos[i]: the screen position of the i-th character */
34 int *off
; /* off[i]: the character at screen position i */
35 char **chrs
; /* chrs[i]: the i-th character in s1 */
39 s1
= ren_translate(s0
? s0
: "");
40 chrs
= uc_chop(s1
, &n
);
41 pos
= ren_position(s0
);
42 off
= malloc(xcols
* sizeof(off
[0]));
43 memset(off
, 0xff, xcols
* sizeof(off
[0]));
44 for (i
= 0; i
< n
; i
++) {
45 int curpos
= led_pos(s0
, pos
[i
]);
46 if (curpos
>= 0 && curpos
< xcols
) {
53 for (i
= 0; i
<= maxcol
; i
++) {
54 if (off
[i
] >= 0 && uc_isprint(chrs
[off
[i
]]))
55 sbuf_mem(out
, chrs
[off
[i
]], uc_len(chrs
[off
[i
]]));
63 return sbuf_done(out
);
66 void led_print(char *s
, int row
)
68 char *r
= led_render(s
);
75 static int led_lastchar(char *s
)
77 char *r
= *s
? strchr(s
, '\0') : s
;
83 static int led_lastword(char *s
)
85 char *r
= *s
? uc_beg(s
, strchr(s
, '\0') - 1) : s
;
87 while (r
> s
&& uc_isspace(r
))
89 kind
= r
> s
? uc_kind(r
) : 0;
90 while (r
> s
&& uc_kind(uc_beg(s
, r
- 1)) == kind
)
95 static void led_printparts(char *pref
, char *main
, char *post
)
102 off
= uc_slen(sbuf_buf(ln
));
104 /* cursor position for inserting the next character */
106 pos
= ren_cursor(sbuf_buf(ln
), ren_pos(sbuf_buf(ln
), off
));
108 int len
= sbuf_len(ln
);
109 sbuf_str(ln
, keymap(led_kmap
, 'a'));
110 pos
= ren_cursor(sbuf_buf(ln
), ren_pos(sbuf_buf(ln
), off
));
113 led_print(sbuf_buf(ln
), -1);
114 term_pos(-1, led_pos(sbuf_buf(ln
), pos
));
118 static char *led_line(char *pref
, char *post
, int *key
, char ***kmap
)
128 led_printparts(pref
, sbuf_buf(sb
), post
);
140 sbuf_cut(sb
, led_lastchar(sbuf_buf(sb
)));
146 sbuf_chr(sb
, term_read(-1));
150 sbuf_cut(sb
, led_lastword(sbuf_buf(sb
)));
153 if (c
== '\n' || TK_STOP(c
))
155 sbuf_str(sb
, keymap(*kmap
, c
));
157 if (c
== '\n' || TK_STOP(c
))
161 return sbuf_done(sb
);
164 /* read an ex command */
165 char *led_prompt(char *pref
, char *post
)
167 char **kmap
= kmap_def
;
170 s
= led_line(pref
, post
, &key
, &kmap
);
177 /* read visual command input */
178 char *led_input(char *pref
, char *post
)
180 struct sbuf
*sb
= sbuf_make();
183 char *ln
= led_line(pref
, post
, &key
, &led_kmap
);
187 led_printparts(pref
? pref
: "", ln
, key
== '\n' ? "" : post
);
197 return sbuf_done(sb
);