8 static char **led_kmap
= kmap_def
;
10 static char *keymap(char **kmap
, int c
)
14 return kmap
[c
] ? kmap
[c
] : cs
;
17 /* map cursor horizontal position to terminal column number */
18 int led_pos(char *s
, int pos
)
20 return dir_context(s
) >= 0 ? pos
: xcols
- pos
- 1;
23 char *led_keymap(int c
)
25 return c
>= 0 ? keymap(led_kmap
, c
) : NULL
;
28 static char *led_render(char *s0
)
31 int *pos
; /* pos[i]: the screen position of the i-th character */
32 int *off
; /* off[i]: the character at screen position i */
33 char **chrs
; /* chrs[i]: the i-th character in s1 */
37 s1
= ren_translate(s0
? s0
: "");
38 chrs
= uc_chop(s1
, &n
);
39 pos
= ren_position(s0
);
40 off
= malloc(xcols
* sizeof(off
[0]));
41 memset(off
, 0xff, xcols
* sizeof(off
[0]));
42 for (i
= 0; i
< n
; i
++) {
43 int curpos
= led_pos(s0
, pos
[i
]);
44 if (curpos
>= 0 && curpos
< xcols
) {
51 for (i
= 0; i
<= maxcol
; i
++) {
52 if (off
[i
] >= 0 && uc_isprint(chrs
[off
[i
]]))
53 sbuf_mem(out
, chrs
[off
[i
]], uc_len(chrs
[off
[i
]]));
61 return sbuf_done(out
);
64 void led_print(char *s
, int row
)
66 char *r
= led_render(s
);
73 static int led_lastchar(char *s
)
75 char *r
= *s
? strchr(s
, '\0') : s
;
81 static int led_lastword(char *s
)
83 char *r
= *s
? uc_beg(s
, strchr(s
, '\0') - 1) : s
;
85 while (r
> s
&& uc_isspace(r
))
87 kind
= r
> s
? uc_kind(r
) : 0;
88 while (r
> s
&& uc_kind(uc_beg(s
, r
- 1)) == kind
)
93 /* the position of the cursor for inserting another character */
94 static int led_insertionpos(struct sbuf
*sb
)
96 int len
= sbuf_len(sb
);
97 char *chr
= keymap(led_kmap
, 'a');
100 col
= ren_cursor(sbuf_buf(sb
),
101 ren_pos(sbuf_buf(sb
), uc_slen(sbuf_buf(sb
)) - 1));
106 static void led_printparts(char *pref
, char *main
, char *post
)
113 col
= led_insertionpos(ln
);
115 led_print(sbuf_buf(ln
), -1);
116 term_pos(-1, led_pos(sbuf_buf(ln
), col
));
120 static char *led_line(char *pref
, char *post
, int *key
, char ***kmap
)
130 led_printparts(pref
, sbuf_buf(sb
), post
);
142 sbuf_cut(sb
, led_lastchar(sbuf_buf(sb
)));
148 sbuf_chr(sb
, term_read(-1));
152 sbuf_cut(sb
, led_lastword(sbuf_buf(sb
)));
155 if (c
== '\n' || c
== TERMESC
|| c
< 0)
157 sbuf_str(sb
, keymap(*kmap
, c
));
159 if (c
== '\n' || c
== TERMESC
|| c
< 0)
163 return sbuf_done(sb
);
166 /* read an ex command */
167 char *led_prompt(char *pref
, char *post
)
169 char **kmap
= kmap_def
;
172 s
= led_line(pref
, post
, &key
, &kmap
);
179 /* read visual command input */
180 char *led_input(char *pref
, char *post
, int *row
, int *col
)
182 struct sbuf
*sb
= sbuf_make();
186 char *ln
= led_line(pref
, post
, &key
, &led_kmap
);
192 *col
= ren_last(pref
? sbuf_buf(sb
) : ln
);
193 led_printparts(pref
? pref
: "", ln
, key
== '\n' ? "" : post
);
205 return sbuf_done(sb
);