Merge pull request #917 from dequis/wcwidth-wrapper

Add a wrapper of wcwidth() that picks the best implementation
This commit is contained in:
ailin-nemui 2018-09-04 09:52:21 +02:00 committed by GitHub
commit d93cd63243
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
12 changed files with 191 additions and 18 deletions

View file

@ -51,7 +51,7 @@ static unichar i_tolower(unichar c)
static int i_isalnum(unichar c)
{
if (term_type == TERM_TYPE_UTF8)
return (g_unichar_isalnum(c) || mk_wcwidth(c) == 0);
return (g_unichar_isalnum(c) || i_wcwidth(c) == 0);
return c <= 255 ? isalnum(c) : 0;
}
@ -219,7 +219,7 @@ static int pos2scrpos(GUI_ENTRY_REC *entry, int pos, int cursor)
if (term_type == TERM_TYPE_BIG5)
xpos += big5_width(c);
else if (entry->utf8)
xpos += unichar_isprint(c) ? mk_wcwidth(c) : 1;
xpos += unichar_isprint(c) ? i_wcwidth(c) : 1;
else
xpos++;
@ -246,7 +246,7 @@ static int scrpos2pos(GUI_ENTRY_REC *entry, int pos)
if (term_type == TERM_TYPE_BIG5)
width = big5_width(c);
else if (entry->utf8)
width = unichar_isprint(c) ? mk_wcwidth(c) : 1;
width = unichar_isprint(c) ? i_wcwidth(c) : 1;
else
width = 1;
@ -373,7 +373,7 @@ static void gui_entry_draw_from(GUI_ENTRY_REC *entry, int pos)
else if (term_type == TERM_TYPE_BIG5)
new_xpos += big5_width(c);
else if (entry->utf8)
new_xpos += unichar_isprint(c) ? mk_wcwidth(c) : 1;
new_xpos += unichar_isprint(c) ? i_wcwidth(c) : 1;
else
new_xpos++;
@ -647,7 +647,7 @@ void gui_entry_insert_char(GUI_ENTRY_REC *entry, unichar chr)
if (chr == 0 || chr == 13 || chr == 10)
return; /* never insert NUL, CR or LF characters */
if (entry->utf8 && entry->pos == 0 && mk_wcwidth(chr) == 0)
if (entry->utf8 && entry->pos == 0 && i_wcwidth(chr) == 0)
return;
gui_entry_redraw_from(entry, entry->pos);
@ -829,7 +829,7 @@ void gui_entry_erase(GUI_ENTRY_REC *entry, int size, CUTBUFFER_UPDATE_OP update_
if (entry->utf8)
while (entry->pos-size-w > 0 &&
mk_wcwidth(entry->text[entry->pos-size-w]) == 0) w++;
i_wcwidth(entry->text[entry->pos-size-w]) == 0) w++;
g_memmove(entry->text + entry->pos - size, entry->text + entry->pos,
(entry->text_len-entry->pos+1) * sizeof(unichar));
@ -867,7 +867,7 @@ void gui_entry_erase_cell(GUI_ENTRY_REC *entry)
if (entry->utf8)
while (entry->pos+size < entry->text_len &&
mk_wcwidth(entry->text[entry->pos+size]) == 0) size++;
i_wcwidth(entry->text[entry->pos+size]) == 0) size++;
g_memmove(entry->text + entry->pos, entry->text + entry->pos + size,
(entry->text_len-entry->pos-size+1) * sizeof(unichar));
@ -1188,7 +1188,7 @@ void gui_entry_move_pos(GUI_ENTRY_REC *entry, int pos)
if (entry->utf8) {
int step = pos < 0 ? -1 : 1;
while(mk_wcwidth(entry->text[entry->pos]) == 0 &&
while(i_wcwidth(entry->text[entry->pos]) == 0 &&
entry->pos + step >= 0 && entry->pos + step <= entry->text_len)
entry->pos += step;
}

View file

@ -31,6 +31,7 @@
#include "gui-printtext.h"
static int window_create_override;
static int wcwidth_impl;
static GUI_WINDOW_REC *gui_window_init(WINDOW_REC *window,
MAIN_WINDOW_REC *parent)
@ -51,6 +52,7 @@ static GUI_WINDOW_REC *gui_window_init(WINDOW_REC *window,
!settings_get_bool("indent_always"),
get_default_indent_func());
textbuffer_view_set_break_wide(gui->view, settings_get_bool("break_wide"));
wcwidth_impl = settings_get_choice("wcwidth_implementation");
textbuffer_view_set_hidden_level(gui->view, MSGLEVEL_HIDDEN);
if (parent->active == window)
textbuffer_view_set_window(gui->view, parent->screen_win);
@ -202,11 +204,18 @@ void gui_window_reparent(WINDOW_REC *window, MAIN_WINDOW_REC *parent)
void gui_windows_reset_settings(void)
{
GSList *tmp;
int old_wcwidth_impl = wcwidth_impl;
wcwidth_impl = settings_get_choice("wcwidth_implementation");
for (tmp = windows; tmp != NULL; tmp = tmp->next) {
WINDOW_REC *rec = tmp->data;
GUI_WINDOW_REC *gui = WINDOW_GUI(rec);
if (old_wcwidth_impl != wcwidth_impl) {
textbuffer_view_reset_cache(gui->view);
}
textbuffer_view_set_break_wide(gui->view, settings_get_bool("break_wide"));
textbuffer_view_set_default_indent(gui->view,
@ -217,6 +226,10 @@ void gui_windows_reset_settings(void)
textbuffer_view_set_scroll(gui->view,
gui->use_scroll ? gui->scroll :
settings_get_bool("scroll"));
if (old_wcwidth_impl != wcwidth_impl) {
textbuffer_view_redraw(gui->view);
}
}
}

View file

@ -515,7 +515,7 @@ void term_add_unichar(TERM_WINDOW *window, unichar chr)
switch (term_type) {
case TERM_TYPE_UTF8:
term_printed_text(unichar_isprint(chr) ? mk_wcwidth(chr) : 1);
term_printed_text(unichar_isprint(chr) ? i_wcwidth(chr) : 1);
term_addch_utf8(window, chr);
break;
case TERM_TYPE_BIG5:
@ -558,7 +558,7 @@ int term_addstr(TERM_WINDOW *window, const char *str)
len++;
ptr++;
} else {
len += unichar_isprint(tmp) ? mk_wcwidth(tmp) : 1;
len += unichar_isprint(tmp) ? i_wcwidth(tmp) : 1;
ptr = g_utf8_next_char(ptr);
}
}

View file

@ -197,7 +197,7 @@ static inline unichar read_unichar(const unsigned char *data, const unsigned cha
*width = 1;
} else {
*next = (unsigned char *)g_utf8_next_char(data);
*width = unichar_isprint(chr) ? mk_wcwidth(chr) : 1;
*width = unichar_isprint(chr) ? i_wcwidth(chr) : 1;
}
return chr;
}
@ -374,7 +374,7 @@ static void view_update_cache(TEXT_BUFFER_VIEW_REC *view, LINE_REC *line,
view->cache->last_linecount = view_get_linecount(view, line);
}
static void view_reset_cache(TEXT_BUFFER_VIEW_REC *view)
void textbuffer_view_reset_cache(TEXT_BUFFER_VIEW_REC *view)
{
GSList *tmp;
@ -691,7 +691,7 @@ void textbuffer_view_set_break_wide(TEXT_BUFFER_VIEW_REC *view,
{
if (view->break_wide != break_wide) {
view->break_wide = break_wide;
view_reset_cache(view);
textbuffer_view_reset_cache(view);
}
}
@ -703,7 +703,7 @@ static void view_unregister_indent_func(TEXT_BUFFER_VIEW_REC *view,
/* recreate cache so it won't contain references
to the indent function */
view_reset_cache(view);
textbuffer_view_reset_cache(view);
view->cache = textbuffer_cache_get(view->siblings, view->width);
}
@ -1314,7 +1314,7 @@ void textbuffer_view_remove_all_lines(TEXT_BUFFER_VIEW_REC *view)
g_hash_table_foreach_remove(view->bookmarks,
(GHRFunc) g_free_true, NULL);
view_reset_cache(view);
textbuffer_view_reset_cache(view);
textbuffer_view_clear(view);
g_slist_foreach(view->siblings, (GFunc) textbuffer_view_clear, NULL);
}

View file

@ -129,6 +129,8 @@ void textbuffer_view_scroll_line(TEXT_BUFFER_VIEW_REC *view, LINE_REC *line);
/* Return line cache */
LINE_CACHE_REC *textbuffer_view_get_line_cache(TEXT_BUFFER_VIEW_REC *view,
LINE_REC *line);
/* Reset the whole line cache */
void textbuffer_view_reset_cache(TEXT_BUFFER_VIEW_REC *view);
/*
Functions for manipulating the text buffer, using these commands update