mirror of
https://github.com/vim/vim.git
synced 2025-09-24 03:44:06 -04:00
patch 8.2.1535: it is not possible to specify cell widths of characters
Problem: It is not possible to specify cell widths of characters. Solution: Add setcellwidths().
This commit is contained in:
189
src/mbyte.c
189
src/mbyte.c
@@ -132,6 +132,7 @@ static int dbcs_char2cells(int c);
|
||||
static int dbcs_ptr2cells_len(char_u *p, int size);
|
||||
static int dbcs_ptr2char(char_u *p);
|
||||
static int dbcs_head_off(char_u *base, char_u *p);
|
||||
static int cw_value(int c);
|
||||
|
||||
/*
|
||||
* Lookup table to quickly get the length in bytes of a UTF-8 character from
|
||||
@@ -1487,7 +1488,7 @@ utf_char2cells(int c)
|
||||
// Sorted list of non-overlapping intervals of Emoji characters that don't
|
||||
// have ambiguous or double width,
|
||||
// based on http://unicode.org/emoji/charts/emoji-list.html
|
||||
static struct interval emoji_width[] =
|
||||
static struct interval emoji_wide[] =
|
||||
{
|
||||
{0x1f1e6, 0x1f1ff},
|
||||
{0x1f321, 0x1f321},
|
||||
@@ -1532,12 +1533,18 @@ utf_char2cells(int c)
|
||||
|
||||
if (c >= 0x100)
|
||||
{
|
||||
int n;
|
||||
|
||||
n = cw_value(c);
|
||||
if (n != 0)
|
||||
return n;
|
||||
|
||||
#ifdef USE_WCHAR_FUNCTIONS
|
||||
/*
|
||||
* Assume the library function wcwidth() works better than our own
|
||||
* stuff. It should return 1 for ambiguous width chars!
|
||||
*/
|
||||
int n = wcwidth(c);
|
||||
n = wcwidth(c);
|
||||
|
||||
if (n < 0)
|
||||
return 6; // unprintable, displays <xxxx>
|
||||
@@ -1549,7 +1556,7 @@ utf_char2cells(int c)
|
||||
if (intable(doublewidth, sizeof(doublewidth), c))
|
||||
return 2;
|
||||
#endif
|
||||
if (p_emoji && intable(emoji_width, sizeof(emoji_width), c))
|
||||
if (p_emoji && intable(emoji_wide, sizeof(emoji_wide), c))
|
||||
return 2;
|
||||
}
|
||||
|
||||
@@ -2570,6 +2577,8 @@ utf_printable(int c)
|
||||
|
||||
// Sorted list of non-overlapping intervals of all Emoji characters,
|
||||
// based on http://unicode.org/emoji/charts/emoji-list.html
|
||||
// Generated by ../runtime/tools/unicode.vim.
|
||||
// Excludes 0x00a9 and 0x00ae because they are considered latin1.
|
||||
static struct interval emoji_all[] =
|
||||
{
|
||||
{0x203c, 0x203c},
|
||||
@@ -5342,3 +5351,177 @@ string_convert_ext(
|
||||
|
||||
return retval;
|
||||
}
|
||||
|
||||
/*
|
||||
* Table set by setcellwidths().
|
||||
*/
|
||||
typedef struct
|
||||
{
|
||||
long first;
|
||||
long last;
|
||||
char width;
|
||||
} cw_interval_T;
|
||||
|
||||
static cw_interval_T *cw_table = NULL;
|
||||
static size_t cw_table_size = 0;
|
||||
|
||||
/*
|
||||
* Return 1 or 2 when "c" is in the cellwidth table.
|
||||
* Return 0 if not.
|
||||
*/
|
||||
static int
|
||||
cw_value(int c)
|
||||
{
|
||||
int mid, bot, top;
|
||||
|
||||
if (cw_table == NULL)
|
||||
return 0;
|
||||
|
||||
// first quick check for Latin1 etc. characters
|
||||
if (c < cw_table[0].first)
|
||||
return 0;
|
||||
|
||||
// binary search in table
|
||||
bot = 0;
|
||||
top = (int)cw_table_size - 1;
|
||||
while (top >= bot)
|
||||
{
|
||||
mid = (bot + top) / 2;
|
||||
if (cw_table[mid].last < c)
|
||||
bot = mid + 1;
|
||||
else if (cw_table[mid].first > c)
|
||||
top = mid - 1;
|
||||
else
|
||||
return cw_table[mid].width;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int
|
||||
tv_nr_compare(const void *a1, const void *a2)
|
||||
{
|
||||
listitem_T *li1 = (listitem_T *)a1;
|
||||
listitem_T *li2 = (listitem_T *)a2;
|
||||
|
||||
return li1->li_tv.vval.v_number - li2->li_tv.vval.v_number;
|
||||
}
|
||||
|
||||
void
|
||||
f_setcellwidths(typval_T *argvars, typval_T *rettv UNUSED)
|
||||
{
|
||||
list_T *l;
|
||||
listitem_T *li;
|
||||
int item;
|
||||
int i;
|
||||
listitem_T **ptrs;
|
||||
cw_interval_T *table;
|
||||
|
||||
if (argvars[0].v_type != VAR_LIST || argvars[0].vval.v_list == NULL)
|
||||
{
|
||||
emsg(_(e_listreq));
|
||||
return;
|
||||
}
|
||||
l = argvars[0].vval.v_list;
|
||||
if (l->lv_len == 0)
|
||||
{
|
||||
// Clearing the table.
|
||||
vim_free(cw_table);
|
||||
cw_table = NULL;
|
||||
cw_table_size = 0;
|
||||
return;
|
||||
}
|
||||
|
||||
ptrs = ALLOC_MULT(listitem_T *, l->lv_len);
|
||||
if (ptrs == NULL)
|
||||
return;
|
||||
|
||||
// Check that all entries are a list with three numbers, the range is
|
||||
// valid and the cell width is valid.
|
||||
item = 0;
|
||||
for (li = l->lv_first; li != NULL; li = li->li_next)
|
||||
{
|
||||
listitem_T *lili;
|
||||
varnumber_T n1;
|
||||
|
||||
if (li->li_tv.v_type != VAR_LIST || li->li_tv.vval.v_list == NULL)
|
||||
{
|
||||
semsg(_(e_list_item_nr_is_not_list), item);
|
||||
vim_free(ptrs);
|
||||
return;
|
||||
}
|
||||
for (lili = li->li_tv.vval.v_list->lv_first, i = 0; lili != NULL;
|
||||
lili = lili->li_next, ++i)
|
||||
{
|
||||
if (lili->li_tv.v_type != VAR_NUMBER)
|
||||
break;
|
||||
if (i == 0)
|
||||
{
|
||||
n1 = lili->li_tv.vval.v_number;
|
||||
if (n1 < 0x100)
|
||||
{
|
||||
emsg(_(e_only_values_of_0x100_and_higher_supported));
|
||||
vim_free(ptrs);
|
||||
return;
|
||||
}
|
||||
}
|
||||
else if (i == 1 && lili->li_tv.vval.v_number < n1)
|
||||
{
|
||||
semsg(_(e_list_item_nr_range_invalid), item);
|
||||
vim_free(ptrs);
|
||||
return;
|
||||
}
|
||||
else if (i == 2 && (lili->li_tv.vval.v_number < 1
|
||||
|| lili->li_tv.vval.v_number > 2))
|
||||
{
|
||||
semsg(_(e_list_item_nr_cell_width_invalid), item);
|
||||
vim_free(ptrs);
|
||||
return;
|
||||
}
|
||||
}
|
||||
if (i != 3)
|
||||
{
|
||||
semsg(_(e_list_item_nr_does_not_contain_3_numbers), item);
|
||||
vim_free(ptrs);
|
||||
return;
|
||||
}
|
||||
ptrs[item++] = lili;
|
||||
}
|
||||
|
||||
// Sort the list on the first number.
|
||||
qsort((void *)ptrs, (size_t)l->lv_len, sizeof(listitem_T *), tv_nr_compare);
|
||||
|
||||
table = ALLOC_MULT(cw_interval_T, l->lv_len);
|
||||
if (table == NULL)
|
||||
{
|
||||
vim_free(ptrs);
|
||||
return;
|
||||
}
|
||||
|
||||
// Store the items in the new table.
|
||||
item = 0;
|
||||
for (li = l->lv_first; li != NULL; li = li->li_next)
|
||||
{
|
||||
listitem_T *lili = li->li_tv.vval.v_list->lv_first;
|
||||
varnumber_T n1;
|
||||
|
||||
n1 = lili->li_tv.vval.v_number;
|
||||
if (item > 0 && n1 <= table[item - 1].last)
|
||||
{
|
||||
semsg(_(e_overlapping_ranges_for_nr), (long)n1);
|
||||
vim_free(ptrs);
|
||||
vim_free(table);
|
||||
return;
|
||||
}
|
||||
table[item].first = n1;
|
||||
lili = lili->li_next;
|
||||
table[item].last = lili->li_tv.vval.v_number;
|
||||
lili = lili->li_next;
|
||||
table[item].width = lili->li_tv.vval.v_number;
|
||||
++item;
|
||||
}
|
||||
|
||||
vim_free(ptrs);
|
||||
vim_free(cw_table);
|
||||
cw_table = table;
|
||||
cw_table_size = l->lv_len;
|
||||
}
|
||||
|
Reference in New Issue
Block a user