From 3e1b644f41ed6946a0201cdc299a6a170f32c19e Mon Sep 17 00:00:00 2001 From: Sergey Date: Sat, 15 Oct 2011 12:42:43 +0000 Subject: [PATCH] Ticket #2372 (Editor sometimes shows multibyte UTF-8 chars as two dots) Sometimes when text contain multibyte UTF-8 chars, editor shows two dots instead of some letter. When moving text cursor after that letter it will be displayed properly. When moving cursor back (before letter) it will be displayed again as two dots. Signed-off-by: Ilia Maslakov added UTF8_CHAR_LEN Signed-off-by: Ilia Maslakov --- lib/global.h | 1 + src/editor/edit.c | 12 ++++++++++++ 2 files changed, 13 insertions(+) diff --git a/lib/global.h b/lib/global.h index 579910e12..c453177f7 100644 --- a/lib/global.h +++ b/lib/global.h @@ -128,6 +128,7 @@ #define SCRIPT_SUFFIX "" #define get_default_editor() "vi" #define OS_SORT_CASE_SENSITIVE_DEFAULT 1 +#define UTF8_CHAR_LEN 6 /* C++ style type casts */ #define const_cast(m_type, m_expr) ((m_type) (m_expr)) diff --git a/src/editor/edit.c b/src/editor/edit.c index 0da86a65f..effca4a97 100644 --- a/src/editor/edit.c +++ b/src/editor/edit.c @@ -1949,6 +1949,7 @@ edit_get_utf (WEdit * edit, long byte_index, int *char_width) gunichar ch; gchar *next_ch = NULL; int width = 0; + gchar utf8_buf[UTF8_CHAR_LEN + 1]; if (byte_index >= (edit->curs1 + edit->curs2) || byte_index < 0) { @@ -1966,6 +1967,17 @@ edit_get_utf (WEdit * edit, long byte_index, int *char_width) res = g_utf8_get_char_validated (str, -1); + if (res < 0) + { + /* Retry with explicit bytes to make sure it's not a buffer boundary */ + int i; + for (i = 0; i < UTF8_CHAR_LEN; i++) + utf8_buf[i] = edit_get_byte (edit, byte_index + i); + utf8_buf[UTF8_CHAR_LEN] = '\0'; + str = utf8_buf; + res = g_utf8_get_char_validated (str, -1); + } + if (res < 0) { ch = *str;