mirror of https://github.com/MidnightCommander/mc
fixed function 'edit_get_prev_utf ' to obtain the correct previous utf8 character.
Signed-off-by: Ilia Maslakov <il.smind@gmail.com>
This commit is contained in:
parent
3e1b644f41
commit
7f9c8f0969
|
@ -693,96 +693,47 @@ edit_get_byte_ptr (WEdit * edit, long byte_index)
|
|||
|
||||
/* --------------------------------------------------------------------------------------------- */
|
||||
|
||||
static char *
|
||||
edit_get_buf_ptr (WEdit * edit, long byte_index)
|
||||
{
|
||||
if (byte_index >= (edit->curs1 + edit->curs2))
|
||||
byte_index--;
|
||||
|
||||
if (byte_index < 0)
|
||||
return NULL;
|
||||
|
||||
if (byte_index >= edit->curs1)
|
||||
{
|
||||
unsigned long p;
|
||||
|
||||
p = edit->curs1 + edit->curs2 - 1;
|
||||
return (char *) (edit->buffers2[p >> S_EDIT_BUF_SIZE] +
|
||||
(EDIT_BUF_SIZE - (p & M_EDIT_BUF_SIZE) - 1));
|
||||
}
|
||||
|
||||
return (char *) (edit->buffers1[byte_index >> S_EDIT_BUF_SIZE] + (0 & M_EDIT_BUF_SIZE));
|
||||
}
|
||||
|
||||
/* --------------------------------------------------------------------------------------------- */
|
||||
|
||||
static int
|
||||
edit_get_prev_utf (WEdit * edit, long byte_index, int *char_width)
|
||||
{
|
||||
gchar *str, *buf = NULL;
|
||||
int res = -1;
|
||||
gunichar ch;
|
||||
gchar *next_ch = NULL;
|
||||
int width = 0;
|
||||
int i, res;
|
||||
gchar utf8_buf[3 * UTF8_CHAR_LEN + 1];
|
||||
gchar *str;
|
||||
gchar *cursor_buf_ptr;
|
||||
|
||||
if (byte_index > 0)
|
||||
byte_index--;
|
||||
|
||||
if (byte_index >= (edit->curs1 + edit->curs2) || byte_index < 0)
|
||||
if (byte_index > (edit->curs1 + edit->curs2) || byte_index <= 0)
|
||||
{
|
||||
*char_width = 0;
|
||||
return 0;
|
||||
}
|
||||
|
||||
ch = edit_get_utf (edit, byte_index, &width);
|
||||
for (i = 0; i < (3 * UTF8_CHAR_LEN); i++)
|
||||
utf8_buf[i] = edit_get_byte (edit, byte_index + i - (2 * UTF8_CHAR_LEN));
|
||||
utf8_buf[3 * UTF8_CHAR_LEN] = '\0';
|
||||
|
||||
if (width == 1)
|
||||
{
|
||||
*char_width = width;
|
||||
return ch;
|
||||
}
|
||||
cursor_buf_ptr = utf8_buf + (2 * UTF8_CHAR_LEN);
|
||||
str = g_utf8_find_prev_char (utf8_buf, cursor_buf_ptr);
|
||||
|
||||
str = edit_get_byte_ptr (edit, byte_index);
|
||||
buf = edit_get_buf_ptr (edit, byte_index);
|
||||
if (str == NULL || buf == NULL)
|
||||
if (str == NULL || g_utf8_next_char(str) != cursor_buf_ptr)
|
||||
{
|
||||
*char_width = 0;
|
||||
return 0;
|
||||
}
|
||||
/* get prev utf8 char */
|
||||
if (str != buf)
|
||||
str = g_utf8_find_prev_char (buf, str);
|
||||
|
||||
if (str == NULL)
|
||||
{
|
||||
*char_width = 0;
|
||||
return 0;
|
||||
*char_width = 1;
|
||||
return *(cursor_buf_ptr-1);
|
||||
}
|
||||
else
|
||||
{
|
||||
res = g_utf8_get_char_validated (str, -1);
|
||||
|
||||
if (res < 0)
|
||||
{
|
||||
ch = *str;
|
||||
width = 0;
|
||||
}
|
||||
else
|
||||
{
|
||||
ch = res;
|
||||
/* Calculate UTF-8 char width */
|
||||
next_ch = g_utf8_next_char (str);
|
||||
if (next_ch)
|
||||
if (res < 0)
|
||||
{
|
||||
width = next_ch - str;
|
||||
*char_width = 1;
|
||||
return *(cursor_buf_ptr-1);
|
||||
}
|
||||
else
|
||||
{
|
||||
ch = 0;
|
||||
width = 0;
|
||||
*char_width = cursor_buf_ptr - str;
|
||||
return res;
|
||||
}
|
||||
}
|
||||
*char_width = width;
|
||||
return ch;
|
||||
}
|
||||
|
||||
/* --------------------------------------------------------------------------------------------- */
|
||||
|
|
Loading…
Reference in New Issue