From d29c5d6d5f3a6b43884547032259df38bf4f7b12 Mon Sep 17 00:00:00 2001 From: Ilia Maslakov Date: Sun, 12 Apr 2009 19:16:52 +0000 Subject: [PATCH] reworked: edit_get_utf return int value is utf-8 char, char_width --- edit/edit.c | 37 ++++++++++++++++++++++++++++++++----- edit/editdraw.c | 17 +++++++++++++---- 2 files changed, 45 insertions(+), 9 deletions(-) diff --git a/edit/edit.c b/edit/edit.c index 73e5acccf..c4a9bf345 100644 --- a/edit/edit.c +++ b/edit/edit.c @@ -120,17 +120,44 @@ int edit_get_byte (WEdit * edit, long byte_index) } } -char *edit_get_utf_char (WEdit * edit, long byte_index) +int edit_get_utf (WEdit * edit, long byte_index, int *char_width) { unsigned long p; + gunichar *str; + int res = -1; + gunichar ch; + gunichar *next_ch = NULL; + + char_width = -1; + if (byte_index >= (edit->curs1 + edit->curs2) || byte_index < 0) - return NULL; + return NULL; if (byte_index >= edit->curs1) { - p = edit->curs1 + edit->curs2 - byte_index - 1; - return str_get_next_char_safe (edit->buffers2[p >> S_EDIT_BUF_SIZE]+(EDIT_BUF_SIZE - (p & M_EDIT_BUF_SIZE) - 1)); + p = edit->curs1 + edit->curs2 - byte_index - 1; + str = (edit->buffers2[p >> S_EDIT_BUF_SIZE]+(EDIT_BUF_SIZE - (p & M_EDIT_BUF_SIZE) - 1)); } else { - return str_get_next_char_safe (edit->buffers1[byte_index >> S_EDIT_BUF_SIZE]+(byte_index & M_EDIT_BUF_SIZE)); + str = (edit->buffers1[byte_index >> S_EDIT_BUF_SIZE]+(byte_index & M_EDIT_BUF_SIZE)); + } + + res = g_utf8_get_char_validated (str, -1); + + if ( res < 0 ) { + ch = (char) str; + char_width = 1; + } else { + ch = res; + next_ch = g_utf8_next_char(str); + if ( next_ch ) { + if ( next_ch != str ) { + char_width = next_ch - str; + } else { + char_width = -1; + } + } else { + ch = 0; + char_width = -1; + } } } diff --git a/edit/editdraw.c b/edit/editdraw.c index cf4f6cfda..9efa23f12 100644 --- a/edit/editdraw.c +++ b/edit/editdraw.c @@ -235,7 +235,7 @@ print_to_widget (WEdit *edit, long row, int start_col, int start_col_real, while (p->ch) { int style; - int textchar; + unsigned int textchar; int color; if (cols_to_skip) { @@ -292,6 +292,7 @@ edit_draw_this_line (WEdit *edit, long b, long row, long start_col, long m1 = 0, m2 = 0, q, c1, c2; int col, start_col_real; + int cw; unsigned int c; int color; int i; @@ -335,8 +336,13 @@ edit_draw_this_line (WEdit *edit, long b, long row, long start_col, if (q >= edit->found_start && q < edit->found_start + edit->found_len) p->style |= MOD_BOLD; - c = edit_get_byte (edit, q); -/* we don't use bg for mc - fg contains both */ + cw = 1; + if ( !edit->utf8 ) { + c = edit_get_byte (edit, q); + } else { + c = edit_get_utf (edit, q, &cw); + } + /* we don't use bg for mc - fg contains both */ edit_get_syntax_color (edit, q, &color); p->style |= color << 16; switch (c) { @@ -441,7 +447,10 @@ edit_draw_this_line (WEdit *edit, long b, long row, long start_col, p++; } } else { - //FIXME: col += utfchar_width - 1 + p->ch = c; + p++; + if ( cw > 1) + col += cw - 1; } col++; break;