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 /* map cursor horizontal position to terminal column number */
29 int led_pos(char *s
, int pos
)
31 return dir_context(s
) >= 0 ? pos
: xcols
- pos
- 1;
34 static int led_posctx(int dir
, int pos
)
36 return dir
>= 0 ? pos
: xcols
- pos
- 1;
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 int syn_merge(int old
, int new)
50 int fg
= SYN_FG(new) ? SYN_FG(new) : SYN_FG(old
);
51 int bg
= SYN_BG(new) ? SYN_BG(new) : SYN_BG(old
);
52 return fg
| SYN_BGMK(bg
);
55 static void led_markrev(int n
, char **chrs
, int *pos
, int *att
)
59 conf_highlight_revdir(&hl
);
61 int dir
= led_offdir(chrs
, pos
, i
);
63 while (i
+ 1 < n
&& led_offdir(chrs
, pos
, i
) == dir
)
66 for (j
= beg
; j
<= i
; j
++)
67 att
[j
] = syn_merge(att
[j
], hl
);
73 static char *led_render(char *s0
)
76 int *pos
; /* pos[i]: the screen position of the i-th character */
77 int *off
; /* off[i]: the character at screen position i */
78 int *att
; /* att[i]: the attributes of i-th character */
79 char **chrs
; /* chrs[i]: the i-th character in s1 */
83 int ctx
= dir_context(s0
);
84 chrs
= uc_chop(s0
, &n
);
85 pos
= ren_position(s0
);
86 off
= malloc(xcols
* sizeof(off
[0]));
87 memset(off
, 0xff, xcols
* sizeof(off
[0]));
88 for (i
= 0; i
< n
; i
++) {
90 int curwid
= ren_cwid(chrs
[i
], curpos
);
91 if (curpos
>= 0 && curpos
+ curwid
< xcols
) {
92 for (j
= 0; j
< curwid
; j
++) {
93 off
[led_posctx(ctx
, curpos
+ j
)] = i
;
94 if (led_posctx(ctx
, curpos
+ j
) > maxcol
)
95 maxcol
= led_posctx(ctx
, curpos
+ j
);
99 att
= syn_highlight(xft
, s0
);
100 led_markrev(n
, chrs
, pos
, att
);
103 while (i
<= maxcol
) {
105 int att_new
= o
>= 0 ? att
[o
] : 0;
106 sbuf_str(out
, term_att(att_new
, att_old
));
109 if (ren_translate(chrs
[o
], s0
))
110 sbuf_str(out
, ren_translate(chrs
[o
], s0
));
111 else if (uc_isprint(chrs
[o
]))
112 sbuf_mem(out
, chrs
[o
], uc_len(chrs
[o
]));
114 for (j
= i
; j
<= maxcol
&& off
[j
] == o
; j
++)
116 while (i
<= maxcol
&& off
[i
] == o
)
123 sbuf_str(out
, term_att(0, att_old
));
128 return sbuf_done(out
);
131 void led_print(char *s
, int row
)
133 char *r
= led_render(s
);
140 static int led_lastchar(char *s
)
142 char *r
= *s
? strchr(s
, '\0') : s
;
144 r
= uc_beg(s
, r
- 1);
148 static int led_lastword(char *s
)
150 char *r
= *s
? uc_beg(s
, strchr(s
, '\0') - 1) : s
;
152 while (r
> s
&& uc_isspace(r
))
153 r
= uc_beg(s
, r
- 1);
154 kind
= r
> s
? uc_kind(r
) : 0;
155 while (r
> s
&& uc_kind(uc_beg(s
, r
- 1)) == kind
)
156 r
= uc_beg(s
, r
- 1);
160 static void led_printparts(char *ai
, char *pref
, char *main
, char *post
, char *kmap
)
169 off
= uc_slen(sbuf_buf(ln
));
170 /* cursor position for inserting the next character */
171 if (*pref
|| *main
|| *ai
) {
172 int len
= sbuf_len(ln
);
173 sbuf_str(ln
, kmap_map(kmap
, 'a'));
175 idir
= ren_pos(sbuf_buf(ln
), off
) -
176 ren_pos(sbuf_buf(ln
), off
- 1) < 0 ? -1 : +1;
180 led_print(sbuf_buf(ln
), -1);
181 pos
= ren_cursor(sbuf_buf(ln
), ren_pos(sbuf_buf(ln
), MAX(0, off
- 1)));
182 term_pos(-1, led_pos(sbuf_buf(ln
), pos
+ idir
));
186 char *led_read(char **kmap
)
189 int c
= term_read(-1);
193 *kmap
= conf_kmapalt();
199 buf
[0] = term_read(-1);
203 return kmap_map(*kmap
, c
);
210 static char *led_line(char *pref
, char *post
, char *ai
, int ai_max
, int *key
, char **kmap
)
213 int ai_len
= strlen(ai
);
221 led_printparts(ai
, pref
, uc_lastline(sbuf_buf(sb
)), post
, *kmap
);
225 *kmap
= conf_kmapalt();
233 sbuf_cut(sb
, led_lastchar(sbuf_buf(sb
)));
239 sbuf_chr(sb
, term_read(-1));
243 sbuf_cut(sb
, led_lastword(sbuf_buf(sb
)));
255 if (reg_get(0, &lnmode
))
256 sbuf_str(sb
, reg_get(0, &lnmode
));
259 if (c
== '\n' || TK_INT(c
))
261 sbuf_str(sb
, kmap_map(*kmap
, c
));
263 if (c
== '\n' || TK_INT(c
))
267 return sbuf_done(sb
);
270 /* read an ex command */
271 char *led_prompt(char *pref
, char *post
, char **kmap
)
275 s
= led_line(pref
, post
, "", 0, &key
, kmap
);
282 /* read visual command input */
283 char *led_input(char *pref
, char *post
, char *ai
, int ai_max
, char **kmap
)
285 struct sbuf
*sb
= sbuf_make();
286 char *first_ai
= NULL
;
289 char *ln
= led_line(pref
, post
, ai
, ai_max
, &key
, kmap
);
291 first_ai
= uc_dup(ai
);
297 led_printparts(ai
, pref
? pref
: "", uc_lastline(ln
),
298 key
== '\n' ? "" : post
, *kmap
);
301 if (!pref
|| !pref
[0]) { /* updating autoindent */
302 int ai_len
= ai_max
? strlen(ai
) : 0;
303 for (i
= 0; isspace((unsigned char) ln
[i
]); i
++)
305 ai
[ai_len
++] = ln
[i
];
313 while (ai_max
&& post
[0] && (post
[0] == ' ' || post
[0] == '\t'))
316 strcpy(ai
, first_ai
);
319 return sbuf_done(sb
);