Skip to content

Commit 740c433

Browse files
committed
patch 8.0.0982: cannot use a terminal when 'encoding' is non-utf8 multi-byte
Problem: When 'encoding' is set to a multi-byte encoding other than utf-8 the characters from ther terminal are messed up. Solution: Convert displayed text from utf-8 to 'encoding' for MS-Windows. (Yasuhiro Matsumoto, close #2000)
1 parent 0cbba82 commit 740c433

2 files changed

Lines changed: 68 additions & 15 deletions

File tree

src/terminal.c

Lines changed: 66 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -49,8 +49,8 @@
4949
"err_io", "err_name", "err_buf", "err_modifiable", "err_msg"
5050
* Check that something is connected to the terminal.
5151
* Test: "cat" reading from a file or buffer
52-
* "ls" writing stdout to a file or buffer
53-
* shell writing stderr to a file or buffer
52+
* "ls" writing stdout to a file or buffer
53+
* shell writing stderr to a file or buffer
5454
* - For the GUI fill termios with default values, perhaps like pangoterm:
5555
* http://bazaar.launchpad.net/~leonerd/pangoterm/trunk/view/head:/main.c#L134
5656
* - support ":term NONE" to open a terminal with a pty but not running a job
@@ -845,7 +845,26 @@ add_scrollback_line_to_buffer(term_T *term, char_u *text, int len)
845845
int empty = (buf->b_ml.ml_flags & ML_EMPTY);
846846
linenr_T lnum = buf->b_ml.ml_line_count;
847847

848-
ml_append_buf(term->tl_buffer, lnum, text, len + 1, FALSE);
848+
#ifdef _WIN32
849+
if (!enc_utf8 && enc_codepage > 0)
850+
{
851+
WCHAR *ret = NULL;
852+
int length = 0;
853+
854+
MultiByteToWideChar_alloc(CP_UTF8, 0, (char*)text, len + 1,
855+
&ret, &length);
856+
if (ret != NULL)
857+
{
858+
WideCharToMultiByte_alloc(enc_codepage, 0,
859+
ret, length, (char **)&text, &len, 0, 0);
860+
vim_free(ret);
861+
ml_append_buf(term->tl_buffer, lnum, text, len, FALSE);
862+
vim_free(text);
863+
}
864+
}
865+
else
866+
#endif
867+
ml_append_buf(term->tl_buffer, lnum, text, len + 1, FALSE);
849868
if (empty)
850869
{
851870
/* Delete the empty line that was in the empty buffer. */
@@ -936,7 +955,7 @@ move_terminal_to_buffer(term_T *term)
936955
int c;
937956

938957
for (i = 0; (c = cell.chars[i]) > 0 || i == 0; ++i)
939-
ga.ga_len += mb_char2bytes(c == NUL ? ' ' : c,
958+
ga.ga_len += utf_char2bytes(c == NUL ? ' ' : c,
940959
(char_u *)ga.ga_data + ga.ga_len);
941960
}
942961
}
@@ -1468,6 +1487,18 @@ terminal_loop(void)
14681487
goto theend;
14691488
}
14701489
}
1490+
# ifdef _WIN32
1491+
if (!enc_utf8 && has_mbyte && c >= 0x80)
1492+
{
1493+
WCHAR wc;
1494+
char_u mb[3];
1495+
1496+
mb[0] = (unsigned)c >> 8;
1497+
mb[1] = c;
1498+
if (MultiByteToWideChar(GetACP(), 0, (char*)mb, 2, &wc, 1) > 0)
1499+
c = wc;
1500+
}
1501+
# endif
14711502
if (send_keys_to_term(curbuf->b_term, c, TRUE) != OK)
14721503
{
14731504
ret = OK;
@@ -1627,7 +1658,7 @@ color2index(VTermColor *color, int fg, int *boldp)
16271658

16281659
/* 216-color cube */
16291660
return 17 + ((red + 25) / 0x33) * 36
1630-
+ ((green + 25) / 0x33) * 6
1661+
+ ((green + 25) / 0x33) * 6
16311662
+ (blue + 25) / 0x33;
16321663
}
16331664
return 0;
@@ -2076,32 +2107,52 @@ term_update_window(win_T *wp)
20762107
else
20772108
{
20782109
#if defined(FEAT_MBYTE)
2079-
if (enc_utf8 && c >= 0x80)
2110+
if (enc_utf8)
20802111
{
2081-
ScreenLines[off] = ' ';
2082-
ScreenLinesUC[off] = c;
2112+
if (c >= 0x80)
2113+
{
2114+
ScreenLines[off] = ' ';
2115+
ScreenLinesUC[off] = c;
2116+
}
2117+
else
2118+
{
2119+
ScreenLines[off] = c;
2120+
ScreenLinesUC[off] = NUL;
2121+
}
20832122
}
2084-
else
2123+
# ifdef _WIN32
2124+
else if (has_mbyte && c >= 0x80)
20852125
{
2086-
ScreenLines[off] = c;
2087-
if (enc_utf8)
2088-
ScreenLinesUC[off] = NUL;
2126+
char_u mb[MB_MAXBYTES+1];
2127+
WCHAR wc = c;
2128+
2129+
if (WideCharToMultiByte(GetACP(), 0, &wc, 1,
2130+
(char*)mb, 2, 0, 0) > 1)
2131+
{
2132+
ScreenLines[off] = mb[0];
2133+
ScreenLines[off+1] = mb[1];
2134+
cell.width = mb_ptr2cells(mb);
2135+
}
2136+
else
2137+
ScreenLines[off] = c;
20892138
}
2090-
#else
2091-
ScreenLines[off] = c;
2139+
# endif
2140+
else
20922141
#endif
2142+
ScreenLines[off] = c;
20932143
}
20942144
ScreenAttrs[off] = cell2attr(cell.attrs, cell.fg, cell.bg);
20952145

20962146
++pos.col;
20972147
++off;
20982148
if (cell.width == 2)
20992149
{
2100-
ScreenLines[off] = NUL;
21012150
#if defined(FEAT_MBYTE)
21022151
if (enc_utf8)
21032152
ScreenLinesUC[off] = NUL;
2153+
else if (!has_mbyte)
21042154
#endif
2155+
ScreenLines[off] = NUL;
21052156
++pos.col;
21062157
++off;
21072158
}

src/version.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -769,6 +769,8 @@ static char *(features[]) =
769769

770770
static int included_patches[] =
771771
{ /* Add new patch number below this line */
772+
/**/
773+
982,
772774
/**/
773775
981,
774776
/**/

0 commit comments

Comments
 (0)