9 static char **kmaps
[] = {kmap_en
, kmap_fa
};
11 static char **kmap_find(char *name
)
14 for (i
= 0; i
< LEN(kmaps
); i
++)
15 if (name
&& kmaps
[i
][0] && !strcmp(name
, kmaps
[i
][0]))
20 static char *kmap_map(char *kmap
, int c
)
23 char **keymap
= kmap_find(kmap
);
25 return keymap
[c
] ? keymap
[c
] : cs
;
28 static int led_posctx(int dir
, int pos
, int beg
, int end
)
30 return dir
>= 0 ? pos
- beg
: end
- pos
- 1;
33 /* map cursor horizontal position to terminal column number */
34 int led_pos(char *s
, int pos
)
36 return led_posctx(dir_context(s
), pos
, xleft
, xleft
+ xcols
);
39 static int led_offdir(char **chrs
, int *pos
, int i
)
41 if (pos
[i
] + ren_cwid(chrs
[i
], pos
[i
]) == pos
[i
+ 1])
43 if (pos
[i
+ 1] + ren_cwid(chrs
[i
+ 1], pos
[i
+ 1]) == pos
[i
])
48 static void led_markrev(int n
, char **chrs
, int *pos
, int *att
)
52 conf_highlight_revdir(&hl
);
54 int dir
= led_offdir(chrs
, pos
, i
);
56 while (i
+ 1 < n
&& led_offdir(chrs
, pos
, i
) == dir
)
59 for (j
= beg
; j
<= i
; j
++)
60 att
[j
] = syn_merge(att
[j
], hl
);
66 static char *led_render(char *s0
, int cbeg
, int cend
)
69 int *pos
; /* pos[i]: the screen position of the i-th character */
70 int *off
; /* off[i]: the character at screen position i */
71 int *att
; /* att[i]: the attributes of i-th character */
72 char **chrs
; /* chrs[i]: the i-th character in s1 */
76 int ctx
= dir_context(s0
);
77 chrs
= uc_chop(s0
, &n
);
78 pos
= ren_position(s0
);
79 off
= malloc((cend
- cbeg
) * sizeof(off
[0]));
80 memset(off
, 0xff, (cend
- cbeg
) * sizeof(off
[0]));
81 for (i
= 0; i
< n
; i
++) {
82 int curwid
= ren_cwid(chrs
[i
], pos
[i
]);
83 int curbeg
= led_posctx(ctx
, pos
[i
], cbeg
, cend
);
84 int curend
= led_posctx(ctx
, pos
[i
] + curwid
- 1, cbeg
, cend
);
85 if (curbeg
>= 0 && curbeg
< (cend
- cbeg
) &&
86 curend
>= 0 && curend
< (cend
- cbeg
))
87 for (j
= 0; j
< curwid
; j
++)
88 off
[led_posctx(ctx
, pos
[i
] + j
, cbeg
, cend
)] = i
;
90 att
= syn_highlight(ex_filetype(), s0
);
91 led_markrev(n
, chrs
, pos
, att
);
95 int o
= off
[i
- cbeg
];
96 int att_new
= o
>= 0 ? att
[o
] : 0;
97 sbuf_str(out
, term_att(att_new
, att_old
));
100 if (ren_translate(chrs
[o
], s0
))
101 sbuf_str(out
, ren_translate(chrs
[o
], s0
));
102 else if (uc_isprint(chrs
[o
]))
103 sbuf_mem(out
, chrs
[o
], uc_len(chrs
[o
]));
105 for (j
= i
; j
< cend
&& off
[j
- cbeg
] == o
; j
++)
107 while (i
< cend
&& off
[i
- cbeg
] == o
)
114 sbuf_str(out
, term_att(0, att_old
));
119 return sbuf_done(out
);
122 void led_print(char *s
, int row
)
124 char *r
= led_render(s
, xleft
, xleft
+ xcols
);
131 static int led_lastchar(char *s
)
133 char *r
= *s
? strchr(s
, '\0') : s
;
135 r
= uc_beg(s
, r
- 1);
139 static int led_lastword(char *s
)
141 char *r
= *s
? uc_beg(s
, strchr(s
, '\0') - 1) : s
;
143 while (r
> s
&& uc_isspace(r
))
144 r
= uc_beg(s
, r
- 1);
145 kind
= r
> s
? uc_kind(r
) : 0;
146 while (r
> s
&& uc_kind(uc_beg(s
, r
- 1)) == kind
)
147 r
= uc_beg(s
, r
- 1);
151 static void led_printparts(char *ai
, char *pref
, char *main
, char *post
, char *kmap
)
160 off
= uc_slen(sbuf_buf(ln
));
161 /* cursor position for inserting the next character */
162 if (*pref
|| *main
|| *ai
) {
163 int len
= sbuf_len(ln
);
164 sbuf_str(ln
, kmap_map(kmap
, 'a'));
166 idir
= ren_pos(sbuf_buf(ln
), off
) -
167 ren_pos(sbuf_buf(ln
), off
- 1) < 0 ? -1 : +1;
172 pos
= ren_cursor(sbuf_buf(ln
), ren_pos(sbuf_buf(ln
), MAX(0, off
- 1)));
173 if (pos
>= xleft
+ xcols
)
174 xleft
= pos
- xcols
/ 2;
176 xleft
= pos
< xcols
? 0 : pos
- xcols
/ 2;
177 led_print(sbuf_buf(ln
), -1);
178 term_pos(-1, led_pos(sbuf_buf(ln
), pos
+ idir
));
183 /* continue reading the character starting with c */
184 static char *led_readchar(int c
, char *kmap
)
189 if (c
== TK_CTL('v')) { /* literal character */
190 buf
[0] = term_read();
194 if (c
== TK_CTL('k')) { /* digraph */
201 for (i
= 0; i
< LEN(digraphs
); i
++)
202 if (digraphs
[i
][0][0] == c1
&& digraphs
[i
][0][1] == c2
)
203 return digraphs
[i
][1];
206 if ((c
& 0xc0) == 0xc0) { /* utf-8 character */
209 for (i
= 1; i
< n
; i
++)
210 buf
[i
] = term_read();
214 return kmap_map(kmap
, c
);
217 char *led_read(char **kmap
)
223 *kmap
= conf_kmapalt();
229 return led_readchar(c
, *kmap
);
236 static char *led_line(char *pref
, char *post
, char *ai
, int ai_max
, int *key
, char **kmap
)
239 int ai_len
= strlen(ai
);
248 led_printparts(ai
, pref
, uc_lastline(sbuf_buf(sb
)), post
, *kmap
);
252 *kmap
= conf_kmapalt();
260 sbuf_cut(sb
, led_lastchar(sbuf_buf(sb
)));
267 sbuf_cut(sb
, led_lastword(sbuf_buf(sb
)));
279 if (reg_get(0, &lnmode
))
280 sbuf_str(sb
, reg_get(0, &lnmode
));
283 if (c
== '\n' || TK_INT(c
))
285 if ((cs
= led_readchar(c
, *kmap
)))
288 if (c
== '\n' || TK_INT(c
))
292 return sbuf_done(sb
);
295 /* read an ex command */
296 char *led_prompt(char *pref
, char *post
, char **kmap
)
300 s
= led_line(pref
, post
, "", 0, &key
, kmap
);
307 /* read visual command input */
308 char *led_input(char *pref
, char *post
, char *ai
, int ai_max
, char **kmap
)
310 struct sbuf
*sb
= sbuf_make();
311 char *first_ai
= NULL
;
314 char *ln
= led_line(pref
, post
, ai
, ai_max
, &key
, kmap
);
316 first_ai
= uc_dup(ai
);
322 led_printparts(ai
, pref
? pref
: "", uc_lastline(ln
),
323 key
== '\n' ? "" : post
, *kmap
);
326 if (!pref
|| !pref
[0]) { /* updating autoindent */
327 int ai_len
= ai_max
? strlen(ai
) : 0;
328 for (i
= 0; isspace((unsigned char) ln
[i
]); i
++)
330 ai
[ai_len
++] = ln
[i
];
338 while (ai_max
&& post
[0] && (post
[0] == ' ' || post
[0] == '\t'))
341 strcpy(ai
, first_ai
);
344 return sbuf_done(sb
);