1
0
mirror of https://git.tartarus.org/simon/putty.git synced 2025-04-24 14:26:29 -05:00

Construct preedit_termline incrementally

This involves repeatedly resizing it as we decode characters.  That's a
bit inefficient (at least with the current implementation of
resizeline()), but it makes it much easier to be certain that the line
is actually the right length.
This commit is contained in:
Ben Harris 2025-04-05 16:20:52 +01:00
parent 4776885767
commit d8493c11cd

View File

@ -8145,20 +8145,26 @@ void term_set_preedit_text(Terminal *term, char *preedit_text)
int width = 0, i;
debug("Pre-edit:");
term->preedit_termline = newtermline(term, 0, false);
BinarySource_BARE_INIT(src, preedit_text, strlen(preedit_text));
while (get_avail(src))
width += term_char_width(term, decode_utf8(src, NULL));
term->preedit_termline = newtermline(term, width, false);
BinarySource_REWIND(src);
for (i = 0; i < width; i++) {
while (get_avail(src)) {
unsigned int c = decode_utf8(src, NULL);
debug(" U+%04X", c);
if (term_char_width(term, c) >= 1) {
term->preedit_termline->chars[i].chr = c;
if (term_char_width(term, c) >= 2) {
term->preedit_termline->chars[i+1].chr = UCSWIDE;
i++;
}
switch (term_char_width(term, c)) {
case -1:
/* Ignore control characters. */
break;
case 1:
width += 1;
resizeline(term, term->preedit_termline, width);
term->preedit_termline->chars[width - 1].chr = c;
break;
case 2:
width += 2;
resizeline(term, term->preedit_termline, width);
term->preedit_termline->chars[width - 2].chr = c;
term->preedit_termline->chars[width - 1].chr = UCSWIDE;
break;
}
}
debug("\n");