To: vim_dev@googlegroups.com Subject: Patch 8.2.1535 Fcc: outbox From: Bram Moolenaar Mime-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit ------------ Patch 8.2.1535 Problem: It is not possible to specify cell widths of characters. Solution: Add setcellwidths(). Files: runtime/doc/eval.txt, runtime/doc/options.txt, runtime/doc/usr_41.txt, src/evalfunc.c, src/mbyte.c, src/proto/mbyte.pro, src/errors.h, src/testdir/test_utf8.vim *** ../vim-8.2.1534/runtime/doc/eval.txt 2020-08-23 17:33:43.769458067 +0200 --- runtime/doc/eval.txt 2020-08-28 20:33:13.564921534 +0200 *************** *** 2756,2761 **** --- 2769,2775 ---- {expr} setbufvar({expr}, {varname}, {val}) none set {varname} in buffer {expr} to {val} + setcellwidths({list}) none set character cell width overrides setcharsearch({dict}) Dict set character search from {dict} setcmdpos({pos}) Number set cursor position in command-line setenv({name}, {val}) none set environment variable *************** *** 8890,8895 **** --- 8940,8968 ---- third argument: > GetValue()->setbufvar(buf, varname) + + setcellwidths({list}) *setcellwidths()* + Specify overrides for cell widths of character ranges. This + tells Vim how wide characters are, counted in screen cells. + This overrides 'ambiwidth'. Example: > + setcellwidths([[0xad, 0xad, 1], + \ [0x2194, 0x2199, 2]] + + < *E1109* *E1110* *E1111* *E1112* *E1113* + The {list} argument is a list of lists with each three + numbers. These three numbers are [low, high, width]. "low" + and "high" can be the same, in which case this refers to one + character. Otherwise it is the range of characters from "low" + to "high" (inclusive). "width" is either 1 or 2, indicating + the character width in screen cells. + An error is given if the argument is invalid, also when a + range overlaps with another. + Only characters with value 0x100 and higher can be used. + + To clear the overrides pass an empty list: > + setcellwidths([]); + + setcharsearch({dict}) *setcharsearch()* Set the current character search information to {dict}, which contains one or more of the following entries: *** ../vim-8.2.1534/runtime/doc/options.txt 2020-07-20 21:31:01.268823457 +0200 --- runtime/doc/options.txt 2020-08-28 18:41:05.237258835 +0200 *************** *** 700,706 **** "double": Use twice the width of ASCII characters. *E834* *E835* The value "double" cannot be used if 'listchars' or 'fillchars' ! contains a character that would be double width. There are a number of CJK fonts for which the width of glyphs for those characters are solely based on how many octets they take in --- 701,709 ---- "double": Use twice the width of ASCII characters. *E834* *E835* The value "double" cannot be used if 'listchars' or 'fillchars' ! ! The values are overruled for characters specified with ! |setcellwidths()|. There are a number of CJK fonts for which the width of glyphs for those characters are solely based on how many octets they take in *** ../vim-8.2.1534/runtime/doc/usr_41.txt 2020-06-13 15:47:21.070282268 +0200 --- runtime/doc/usr_41.txt 2020-08-28 20:48:35.011304521 +0200 *************** *** 603,608 **** --- 611,617 ---- strchars() length of a string in characters strwidth() size of string when displayed strdisplaywidth() size of string when displayed, deals with tabs + setcellwidths() set character cell width overrides substitute() substitute a pattern match with a string submatch() get a specific match in ":s" and substitute() strpart() get part of a string using byte index *** ../vim-8.2.1534/src/evalfunc.c 2020-08-28 16:39:28.258176214 +0200 --- src/evalfunc.c 2020-08-28 18:42:15.729037452 +0200 *************** *** 886,891 **** --- 886,892 ---- {"serverlist", 0, 0, 0, ret_string, f_serverlist}, {"setbufline", 3, 3, FEARG_3, ret_number, f_setbufline}, {"setbufvar", 3, 3, FEARG_3, ret_void, f_setbufvar}, + {"setcellwidths", 1, 1, FEARG_1, ret_void, f_setcellwidths}, {"setcharsearch", 1, 1, FEARG_1, ret_void, f_setcharsearch}, {"setcmdpos", 1, 1, FEARG_1, ret_number, f_setcmdpos}, {"setenv", 2, 2, FEARG_2, ret_void, f_setenv}, *** ../vim-8.2.1534/src/mbyte.c 2020-06-04 18:21:56.046395485 +0200 --- src/mbyte.c 2020-08-28 20:35:42.823791468 +0200 *************** *** 132,137 **** --- 132,138 ---- 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,1493 **** // 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[] = { {0x1f1e6, 0x1f1ff}, {0x1f321, 0x1f321}, --- 1488,1494 ---- // 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_wide[] = { {0x1f1e6, 0x1f1ff}, {0x1f321, 0x1f321}, *************** *** 1532,1543 **** if (c >= 0x100) { #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); if (n < 0) return 6; // unprintable, displays --- 1533,1550 ---- 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! */ ! n = wcwidth(c); if (n < 0) return 6; // unprintable, displays *************** *** 1549,1555 **** if (intable(doublewidth, sizeof(doublewidth), c)) return 2; #endif ! if (p_emoji && intable(emoji_width, sizeof(emoji_width), c)) return 2; } --- 1556,1562 ---- if (intable(doublewidth, sizeof(doublewidth), c)) return 2; #endif ! if (p_emoji && intable(emoji_wide, sizeof(emoji_wide), c)) return 2; } *************** *** 2570,2575 **** --- 2577,2584 ---- // 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,5344 **** --- 5351,5527 ---- 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; + } *** ../vim-8.2.1534/src/proto/mbyte.pro 2020-06-04 18:21:56.046395485 +0200 --- src/proto/mbyte.pro 2020-08-28 19:24:25.025017852 +0200 *************** *** 84,87 **** --- 84,88 ---- int convert_input_safe(char_u *ptr, int len, int maxlen, char_u **restp, int *restlenp); char_u *string_convert(vimconv_T *vcp, char_u *ptr, int *lenp); char_u *string_convert_ext(vimconv_T *vcp, char_u *ptr, int *lenp, int *unconvlenp); + void f_setcellwidths(typval_T *argvars, typval_T *rettv); /* vim: set ft=c : */ *** ../vim-8.2.1534/src/errors.h 2020-08-22 15:06:29.420409081 +0200 --- src/errors.h 2020-08-28 20:34:28.220339084 +0200 *************** *** 238,241 **** --- 238,253 ---- INIT(= N_("E1107: String, List, Dict or Blob required")); EXTERN char e_item_not_found_str[] INIT(= N_("E1108: Item not found: %s")); + EXTERN char e_list_item_nr_is_not_list[] + INIT(= N_("E1109: List item %d is not a List")); + EXTERN char e_list_item_nr_does_not_contain_3_numbers[] + INIT(= N_("E1110: List item %d does not contain 3 numbers")); + EXTERN char e_list_item_nr_range_invalid[] + INIT(= N_("E1111: List item %d range invalid")); + EXTERN char e_list_item_nr_cell_width_invalid[] + INIT(= N_("E1112: List item %d cell width invalid")); + EXTERN char e_overlapping_ranges_for_nr[] + INIT(= N_("E1113: Overlapping ranges for %lx")); + EXTERN char e_only_values_of_0x100_and_higher_supported[] + INIT(= N_("E1114: Only values of 0x100 and higher supported")); #endif *** ../vim-8.2.1534/src/testdir/test_utf8.vim 2020-07-11 22:14:54.322422193 +0200 --- src/testdir/test_utf8.vim 2020-08-28 20:38:13.514771882 +0200 *************** *** 145,148 **** --- 145,183 ---- bwipe! endfunc + func Test_setcellwidths() + call setcellwidths([ + \ [0x1330, 0x1330, 2], + \ [0x1337, 0x1339, 2], + \ [9999, 10000, 1], + \]) + + call assert_equal(2, strwidth("\u1330")) + call assert_equal(1, strwidth("\u1336")) + call assert_equal(2, strwidth("\u1337")) + call assert_equal(2, strwidth("\u1339")) + call assert_equal(1, strwidth("\u133a")) + + call setcellwidths([]) + + call assert_fails('call setcellwidths(1)', 'E714:') + + call assert_fails('call setcellwidths([1, 2, 0])', 'E1109:') + + call assert_fails('call setcellwidths([[0x101]])', 'E1110:') + call assert_fails('call setcellwidths([[0x101, 0x102]])', 'E1110:') + call assert_fails('call setcellwidths([[0x101, 0x102, 1, 4]])', 'E1110:') + call assert_fails('call setcellwidths([["a"]])', 'E1110:') + + call assert_fails('call setcellwidths([[0x102, 0x101, 1]])', 'E1111:') + + call assert_fails('call setcellwidths([[0x101, 0x102, 0]])', 'E1112:') + call assert_fails('call setcellwidths([[0x101, 0x102, 3]])', 'E1112:') + + call assert_fails('call setcellwidths([[0x111, 0x122, 1], [0x115, 0x116, 2]])', 'E1113:') + call assert_fails('call setcellwidths([[0x111, 0x122, 1], [0x122, 0x123, 2]])', 'E1113:') + + call assert_fails('call setcellwidths([[0x33, 0x44, 2]])', 'E1114:') + endfunc + " vim: shiftwidth=2 sts=2 expandtab *** ../vim-8.2.1534/src/version.c 2020-08-28 17:19:03.831887039 +0200 --- src/version.c 2020-08-28 18:41:26.041193412 +0200 *************** *** 756,757 **** --- 756,759 ---- { /* Add new patch number below this line */ + /**/ + 1535, /**/ -- DINGO: Wicked wicked Zoot ... she is a bad person and she must pay the penalty. And here in Castle Anthrax, we have but one punishment ... you must tie her down on a bed ... and spank her. Come! GIRLS: A spanking! A spanking! "Monty Python and the Holy Grail" PYTHON (MONTY) PICTURES LTD /// Bram Moolenaar -- Bram@Moolenaar.net -- http://www.Moolenaar.net \\\ /// sponsor Vim, vote for features -- http://www.Vim.org/sponsor/ \\\ \\\ an exciting new programming language -- http://www.Zimbu.org /// \\\ help me help AIDS victims -- http://ICCF-Holland.org ///