From cbc54cf484d1082e8c09b955e86aff4579ad8f8a Mon Sep 17 00:00:00 2001 From: zeertzjq Date: Thu, 17 Feb 2022 15:23:17 +0800 Subject: vim-patch:8.2.3184: cannot add a digraph with a leading space Problem: Cannot add a digraph with a leading space. It is not easy to list existing digraphs. Solution: Add setdigraph(), setdigraphlist(), getdigraph() and getdigraphlist(). (closes vim/vim#8580) https://github.com/vim/vim/commit/6106504e9edc8500131f7a36e59bc146f90180fa Use GA_APPEND_VIA_PTR in registerdigraph(). Use tv_list_append_*() in getdigraphlist_appendpair(). Put the error messages in digraph.c. E196 is N/A. Remove mentions about 'encoding' being non-Unicode. Nvim doesn't support setting encoding=japan, so skip a test. --- src/nvim/digraph.c | 257 ++++++++++++++++++++++++++++++++++---- src/nvim/digraph.h | 1 + src/nvim/eval.lua | 4 + src/nvim/eval/funcs.c | 1 + src/nvim/testdir/test_digraph.vim | 80 +++++++++++- 5 files changed, 317 insertions(+), 26 deletions(-) (limited to 'src') diff --git a/src/nvim/digraph.c b/src/nvim/digraph.c index 6e5389c979..4046869871 100644 --- a/src/nvim/digraph.c +++ b/src/nvim/digraph.c @@ -12,6 +12,7 @@ #include "nvim/ascii.h" #include "nvim/charset.h" #include "nvim/digraph.h" +#include "nvim/eval/typval.h" #include "nvim/ex_cmds2.h" #include "nvim/ex_docmd.h" #include "nvim/ex_getln.h" @@ -34,6 +35,12 @@ typedef struct digraph { result_T result; } digr_T; +static char e_digraph_must_be_just_two_characters_str[] + = N_("E1214: Digraph must be just two characters: %s"); +static char e_digraph_argument_must_be_one_character_str[] + = N_("E1215: Digraph must be one character: %s"); +static char e_setdigraphlist_argument_must_be_list_of_lists_with_two_items[] + = N_("E1216: setdigraphlist() argument must be a list of lists with two items"); #ifdef INCLUDE_GENERATED_DECLARATIONS # include "digraph.c.generated.h" @@ -1607,6 +1614,44 @@ int getdigraph(int char1, int char2, bool meta_char) return retval; } +/// Add a digraph to the digraph table. +static void registerdigraph(int char1, int char2, int n) +{ + // If the digraph already exists, replace "result". + digr_T *dp = (digr_T *)user_digraphs.ga_data; + for (int i = 0; i < user_digraphs.ga_len; i++) { + if ((int)dp->char1 == char1 && (int)dp->char2 == char2) { + dp->result = n; + return; + } + dp++; + } + + // Add a new digraph to the table. + dp = GA_APPEND_VIA_PTR(digr_T, &user_digraphs); + dp->char1 = (char_u)char1; + dp->char2 = (char_u)char2; + dp->result = n; +} + +/// Check the characters are valid for a digraph. +/// If they are valid, returns true; otherwise, give an error message and +/// returns false. +bool check_digraph_chars_valid(int char1, int char2) +{ + if (char2 == 0) { + char_u msg[MB_MAXBYTES + 1]; + msg[utf_char2bytes(char1, msg)] = NUL; + semsg(_(e_digraph_must_be_just_two_characters_str), msg); + return false; + } + if (char1 == ESC || char2 == ESC) { + emsg(_("E104: Escape not allowed in digraph")); + return false; + } + return true; +} + /// Add the digraphs in the argument to the digraph table. /// format: {c1}{c2} char {c1}{c2} char ... /// @@ -1622,15 +1667,10 @@ void putdigraph(char_u *str) char_u char1 = *str++; char_u char2 = *str++; - if (char2 == 0) { - emsg(_(e_invarg)); + if (!check_digraph_chars_valid(char1, char2)) { return; } - if ((char1 == ESC) || (char2 == ESC)) { - emsg(_("E104: Escape not allowed in digraph")); - return; - } str = skipwhite(str); if (!ascii_isdigit(*str)) { @@ -1639,25 +1679,7 @@ void putdigraph(char_u *str) } int n = getdigits_int(&str, true, 0); - // If the digraph already exists, replace the result. - digr_T *dp = (digr_T *)user_digraphs.ga_data; - - int i; - for (i = 0; i < user_digraphs.ga_len; i++) { - if (((int)dp->char1 == char1) && ((int)dp->char2 == char2)) { - dp->result = n; - break; - } - dp++; - } - - // Add a new digraph to the table. - if (i == user_digraphs.ga_len) { - dp = GA_APPEND_VIA_PTR(digr_T, &user_digraphs); - dp->char1 = char1; - dp->char2 = char2; - dp->result = n; - } + registerdigraph(char1, char2, n); } } @@ -1707,6 +1729,50 @@ void listdigraphs(bool use_headers) } } +static void getdigraphlist_appendpair(digr_T *dp, list_T *l) +{ + list_T *l2 = tv_list_alloc(2); + tv_list_append_list(l, l2); + + char_u buf[30]; + buf[0] = dp->char1; + buf[1] = dp->char2; + buf[2] = NUL; + tv_list_append_string(l2, (char *)buf, -1); + + char_u *p = buf; + p += utf_char2bytes(dp->result, p); + *p = NUL; + tv_list_append_string(l2, (char *)buf, -1); +} + +void getdigraphlist_common(bool list_all, typval_T *rettv) +{ + tv_list_alloc_ret(rettv, (int)sizeof(digraphdefault) + user_digraphs.ga_len); + + digr_T *dp; + + if (list_all) { + dp = digraphdefault; + for (int i = 0; dp->char1 != NUL && !got_int; i++) { + digr_T tmp; + tmp.char1 = dp->char1; + tmp.char2 = dp->char2; + tmp.result = getexactdigraph(tmp.char1, tmp.char2, false); + if (tmp.result != 0 && tmp.result != tmp.char2) { + getdigraphlist_appendpair(&tmp, rettv->vval.v_list); + } + dp++; + } + } + + dp = (digr_T *)user_digraphs.ga_data; + for (int i = 0; i < user_digraphs.ga_len && !got_int; i++) { + getdigraphlist_appendpair(dp, rettv->vval.v_list); + dp++; + } +} + struct dg_header_entry { int dg_start; const char *dg_header; @@ -1797,6 +1863,147 @@ static void printdigraph(const digr_T *dp, result_T *previous) } } +/// Get the two digraph characters from a typval. +/// @return OK or FAIL. +static int get_digraph_chars(typval_T *arg, int *char1, int *char2) +{ + char buf_chars[NUMBUFLEN]; + const char *chars = tv_get_string_buf_chk(arg, buf_chars); + const char_u *p = (const char_u *)chars; + + if (p != NULL) { + if (*p != NUL) { + *char1 = mb_cptr2char_adv(&p); + if (*p != NUL) { + *char2 = mb_cptr2char_adv(&p); + if (*p == NUL) { + if (check_digraph_chars_valid(*char1, *char2)) { + return OK; + } + return FAIL; + } + } + } + } + semsg(_(e_digraph_must_be_just_two_characters_str), chars); + return FAIL; +} + +static bool setdigraph_common(typval_T *argchars, typval_T *argdigraph) +{ + int char1, char2; + if (get_digraph_chars(argchars, &char1, &char2) == FAIL) { + return false; + } + + char buf_digraph[NUMBUFLEN]; + const char *digraph = tv_get_string_buf_chk(argdigraph, buf_digraph); + if (digraph == NULL) { + return false; + } + const char_u *p = (const char_u *)digraph; + int n = mb_cptr2char_adv(&p); + if (*p != NUL) { + semsg(_(e_digraph_argument_must_be_one_character_str), digraph); + return false; + } + + registerdigraph(char1, char2, n); + return true; +} + +/// "getdigraph()" function +void f_getdigraph(typval_T *argvars, typval_T *rettv, FunPtr fptr) +{ + rettv->v_type = VAR_STRING; + rettv->vval.v_string = NULL; // Return empty string for failure + const char *digraphs = tv_get_string_chk(&argvars[0]); + + if (digraphs == NULL) { + return; + } + if (STRLEN(digraphs) != 2) { + semsg(_(e_digraph_must_be_just_two_characters_str), digraphs); + return; + } + int code = getdigraph(digraphs[0], digraphs[1], false); + + char_u buf[NUMBUFLEN]; + buf[utf_char2bytes(code, buf)] = NUL; + rettv->vval.v_string = vim_strsave(buf); +} + +/// "getdigraphlist()" function +void f_getdigraphlist(typval_T *argvars, typval_T *rettv, FunPtr fptr) +{ + bool flag_list_all; + + if (argvars[0].v_type == VAR_UNKNOWN) { + flag_list_all = false; + } else { + bool error = false; + varnumber_T flag = tv_get_number_chk(&argvars[0], &error); + if (error) { + return; + } + flag_list_all = flag != 0; + } + + getdigraphlist_common(flag_list_all, rettv); +} + +/// "setdigraph()" function +void f_setdigraph(typval_T *argvars, typval_T *rettv, FunPtr fptr) +{ + rettv->v_type = VAR_BOOL; + rettv->vval.v_bool = kBoolVarFalse; + + if (!setdigraph_common(&argvars[0], &argvars[1])) { + return; + } + + rettv->vval.v_bool = kBoolVarTrue; +} + +/// "setdigraphlist()" function +void f_setdigraphlist(typval_T *argvars, typval_T *rettv, FunPtr fptr) +{ + rettv->v_type = VAR_BOOL; + rettv->vval.v_bool = kBoolVarFalse; + + if (argvars[0].v_type != VAR_LIST) { + emsg(_(e_setdigraphlist_argument_must_be_list_of_lists_with_two_items)); + return; + } + + list_T *pl = argvars[0].vval.v_list; + if (pl == NULL) { + // Empty list always results in success. + rettv->vval.v_bool = kBoolVarTrue; + return; + } + + TV_LIST_ITER_CONST(pl, pli, { + if (TV_LIST_ITEM_TV(pli)->v_type != VAR_LIST) { + emsg(_(e_setdigraphlist_argument_must_be_list_of_lists_with_two_items)); + return; + } + + list_T *l = TV_LIST_ITEM_TV(pli)->vval.v_list; + if (l == NULL || tv_list_len(l) != 2) { + emsg(_(e_setdigraphlist_argument_must_be_list_of_lists_with_two_items)); + return; + } + + if (!setdigraph_common(TV_LIST_ITEM_TV(tv_list_first(l)), + TV_LIST_ITEM_TV(TV_LIST_ITEM_NEXT(l, tv_list_first(l))))) { + return; + } + }); + + rettv->vval.v_bool = kBoolVarTrue; +} + /// structure used for b_kmap_ga.ga_data typedef struct { char_u *from; diff --git a/src/nvim/digraph.h b/src/nvim/digraph.h index 71330ae9b1..80b4148213 100644 --- a/src/nvim/digraph.h +++ b/src/nvim/digraph.h @@ -2,6 +2,7 @@ #define NVIM_DIGRAPH_H #include "nvim/ex_cmds_defs.h" +#include "nvim/eval/funcs.h" #include "nvim/types.h" #ifdef INCLUDE_GENERATED_DECLARATIONS diff --git a/src/nvim/eval.lua b/src/nvim/eval.lua index 05e91a658f..0951cd239d 100644 --- a/src/nvim/eval.lua +++ b/src/nvim/eval.lua @@ -156,6 +156,8 @@ return { getcurpos={args={0, 1}, base=1}, getcursorcharpos={args={0, 1}, base=1}, getcwd={args={0, 2}, base=1}, + getdigraph={args=1, base=1}, + getdigraphlist={args={0, 1}, base=1}, getenv={args=1, base=1}, getfontname={args={0, 1}}, getfperm={args=1, base=1}, @@ -322,6 +324,8 @@ return { setcharsearch={args=1, base=1}, setcmdpos={args=1, base=1}, setcursorcharpos={args={1, 3}, base=1}, + setdigraph={args=2, base=1}, + setdigraphlist={args=1, base=1}, setenv={args=2, base=2}, setfperm={args=2, base=1}, setline={args=2, base=2}, diff --git a/src/nvim/eval/funcs.c b/src/nvim/eval/funcs.c index d365e075e6..609db3990b 100644 --- a/src/nvim/eval/funcs.c +++ b/src/nvim/eval/funcs.c @@ -15,6 +15,7 @@ #include "nvim/charset.h" #include "nvim/context.h" #include "nvim/cursor.h" +#include "nvim/digraph.h" #include "nvim/diff.h" #include "nvim/edit.h" #include "nvim/eval.h" diff --git a/src/nvim/testdir/test_digraph.vim b/src/nvim/testdir/test_digraph.vim index 5965ee48ef..e9073db649 100644 --- a/src/nvim/testdir/test_digraph.vim +++ b/src/nvim/testdir/test_digraph.vim @@ -212,7 +212,7 @@ func Test_digraphs() call Put_Dig("el") call assert_equal(['␀', 'ü', '∞', 'l'], getline(line('.')-3,line('.'))) call assert_fails('digraph xy z', 'E39:') - call assert_fails('digraph x', 'E474:') + call assert_fails('digraph x', 'E1214:') bw! endfunc @@ -505,4 +505,82 @@ func Test_entering_digraph() call StopVimInTerminal(buf) endfunc +func Test_setdigraph_function() + new + call setdigraph('aa', 'あ') + call Put_Dig('aa') + call assert_equal('あ', getline('$')) + call setdigraph(' i', 'い') + call Put_Dig(' i') + call assert_equal('い', getline('$')) + call setdigraph(' ', 'う') + call Put_Dig(' ') + call assert_equal('う', getline('$')) + + eval 'aa'->setdigraph('え') + call Put_Dig('aa') + call assert_equal('え', getline('$')) + + call assert_fails('call setdigraph("aaa", "あ")', 'E1214: Digraph must be just two characters: aaa') + call assert_fails('call setdigraph("b", "あ")', 'E1214: Digraph must be just two characters: b') + call assert_fails('call setdigraph("あ", "あ")', 'E1214: Digraph must be just two characters: あ') + call assert_fails('call setdigraph("aa", "ああ")', 'E1215: Digraph must be one character: ああ') + call assert_fails('call setdigraph("aa", "か" .. nr2char(0x3099))', 'E1215: Digraph must be one character: か' .. nr2char(0x3099)) + bwipe! +endfunc + +func Test_getdigraph_function() + " Built-in digraphs + call assert_equal('∞', getdigraph('00')) + + " User-defined digraphs + call setdigraph('aa', 'あ') + call setdigraph(' i', 'い') + call setdigraph(' ', 'う') + call assert_equal('あ', getdigraph('aa')) + call assert_equal('あ', 'aa'->getdigraph()) + call assert_equal('い', getdigraph(' i')) + call assert_equal('う', getdigraph(' ')) + call assert_fails('call getdigraph("aaa")', 'E1214: Digraph must be just two characters: aaa') + call assert_fails('call getdigraph("b")', 'E1214: Digraph must be just two characters: b') +endfunc + +func Test_getdigraph_function_encode() + throw 'Skipped: Nvim does not support setting encoding=japan' + CheckFeature iconv + let testcases = { + \'00': '∞', + \'aa': 'あ', + \} + for [key, ch] in items(testcases) + call setdigraph(key, ch) + set encoding=japan + call assert_equal(iconv(ch, 'utf-8', 'japan'), getdigraph(key)) + set encoding& + endfor +endfunc + +func Test_setdigraphlist_function() + call setdigraphlist([['aa', 'き'], ['bb', 'く']]) + call assert_equal('き', getdigraph('aa')) + call assert_equal('く', getdigraph('bb')) + + call assert_fails('call setdigraphlist([[]])', 'E1216:') + call assert_fails('call setdigraphlist([["aa", "b", "cc"]])', '1216:') + call assert_fails('call setdigraphlist([["あ", "あ"]])', 'E1214: Digraph must be just two characters: あ') +endfunc + +func Test_getdigraphlist_function() + " Make sure user-defined digraphs are defined + call setdigraphlist([['aa', 'き'], ['bb', 'く']]) + + for pair in getdigraphlist(1) + call assert_equal(getdigraph(pair[0]), pair[1]) + endfor + + " We don't know how many digraphs are registered before, so check the number + " of digraphs returned. + call assert_equal(getdigraphlist()->len(), getdigraphlist(0)->len()) + call assert_notequal((getdigraphlist()->len()), getdigraphlist(1)->len()) +endfunc " vim: shiftwidth=2 sts=2 expandtab -- cgit From 3b0bcb8ad0816e363cdaa7565f0d9ed0702d1d4e Mon Sep 17 00:00:00 2001 From: zeertzjq Date: Thu, 17 Feb 2022 18:42:48 +0800 Subject: vim-patch:8.2.3226: new digraph functions use old naming scheme Problem: New digraph functions use old naming scheme. Solution: Use the digraph_ prefix. (Hirohito Higashi, closes vim/vim#8580) https://github.com/vim/vim/commit/29b857150c111a455f1a38a8f748243524f692e1 --- src/nvim/digraph.c | 52 +++++++++++++-------------- src/nvim/edit.c | 2 +- src/nvim/eval.lua | 8 ++--- src/nvim/testdir/test_digraph.vim | 74 +++++++++++++++++++-------------------- 4 files changed, 68 insertions(+), 68 deletions(-) (limited to 'src') diff --git a/src/nvim/digraph.c b/src/nvim/digraph.c index 4046869871..aaf3ace861 100644 --- a/src/nvim/digraph.c +++ b/src/nvim/digraph.c @@ -39,8 +39,8 @@ static char e_digraph_must_be_just_two_characters_str[] = N_("E1214: Digraph must be just two characters: %s"); static char e_digraph_argument_must_be_one_character_str[] = N_("E1215: Digraph must be one character: %s"); -static char e_setdigraphlist_argument_must_be_list_of_lists_with_two_items[] - = N_("E1216: setdigraphlist() argument must be a list of lists with two items"); +static char e_digraph_setlist_argument_must_be_list_of_lists_with_two_items[] + = N_("E1216: digraph_setlist() argument must be a list of lists with two items"); #ifdef INCLUDE_GENERATED_DECLARATIONS # include "digraph.c.generated.h" @@ -1465,7 +1465,7 @@ int do_digraph(int c) backspaced = -1; } else if (p_dg) { if (backspaced >= 0) { - c = getdigraph(backspaced, c, false); + c = digraph_get(backspaced, c, false); } backspaced = -1; @@ -1537,7 +1537,7 @@ int get_digraph(bool cmdline) if (cc != ESC) { // ESC cancels CTRL-K - return getdigraph(c, cc, true); + return digraph_get(c, cc, true); } } return NUL; @@ -1601,7 +1601,7 @@ static int getexactdigraph(int char1, int char2, bool meta_char) /// @param meta_char /// /// @return The digraph. -int getdigraph(int char1, int char2, bool meta_char) +int digraph_get(int char1, int char2, bool meta_char) { int retval; @@ -1729,7 +1729,7 @@ void listdigraphs(bool use_headers) } } -static void getdigraphlist_appendpair(digr_T *dp, list_T *l) +static void digraph_getlist_appendpair(digr_T *dp, list_T *l) { list_T *l2 = tv_list_alloc(2); tv_list_append_list(l, l2); @@ -1746,7 +1746,7 @@ static void getdigraphlist_appendpair(digr_T *dp, list_T *l) tv_list_append_string(l2, (char *)buf, -1); } -void getdigraphlist_common(bool list_all, typval_T *rettv) +void digraph_getlist_common(bool list_all, typval_T *rettv) { tv_list_alloc_ret(rettv, (int)sizeof(digraphdefault) + user_digraphs.ga_len); @@ -1760,7 +1760,7 @@ void getdigraphlist_common(bool list_all, typval_T *rettv) tmp.char2 = dp->char2; tmp.result = getexactdigraph(tmp.char1, tmp.char2, false); if (tmp.result != 0 && tmp.result != tmp.char2) { - getdigraphlist_appendpair(&tmp, rettv->vval.v_list); + digraph_getlist_appendpair(&tmp, rettv->vval.v_list); } dp++; } @@ -1768,7 +1768,7 @@ void getdigraphlist_common(bool list_all, typval_T *rettv) dp = (digr_T *)user_digraphs.ga_data; for (int i = 0; i < user_digraphs.ga_len && !got_int; i++) { - getdigraphlist_appendpair(dp, rettv->vval.v_list); + digraph_getlist_appendpair(dp, rettv->vval.v_list); dp++; } } @@ -1889,7 +1889,7 @@ static int get_digraph_chars(typval_T *arg, int *char1, int *char2) return FAIL; } -static bool setdigraph_common(typval_T *argchars, typval_T *argdigraph) +static bool digraph_set_common(typval_T *argchars, typval_T *argdigraph) { int char1, char2; if (get_digraph_chars(argchars, &char1, &char2) == FAIL) { @@ -1912,8 +1912,8 @@ static bool setdigraph_common(typval_T *argchars, typval_T *argdigraph) return true; } -/// "getdigraph()" function -void f_getdigraph(typval_T *argvars, typval_T *rettv, FunPtr fptr) +/// "digraph_get()" function +void f_digraph_get(typval_T *argvars, typval_T *rettv, FunPtr fptr) { rettv->v_type = VAR_STRING; rettv->vval.v_string = NULL; // Return empty string for failure @@ -1926,15 +1926,15 @@ void f_getdigraph(typval_T *argvars, typval_T *rettv, FunPtr fptr) semsg(_(e_digraph_must_be_just_two_characters_str), digraphs); return; } - int code = getdigraph(digraphs[0], digraphs[1], false); + int code = digraph_get(digraphs[0], digraphs[1], false); char_u buf[NUMBUFLEN]; buf[utf_char2bytes(code, buf)] = NUL; rettv->vval.v_string = vim_strsave(buf); } -/// "getdigraphlist()" function -void f_getdigraphlist(typval_T *argvars, typval_T *rettv, FunPtr fptr) +/// "digraph_getlist()" function +void f_digraph_getlist(typval_T *argvars, typval_T *rettv, FunPtr fptr) { bool flag_list_all; @@ -1949,30 +1949,30 @@ void f_getdigraphlist(typval_T *argvars, typval_T *rettv, FunPtr fptr) flag_list_all = flag != 0; } - getdigraphlist_common(flag_list_all, rettv); + digraph_getlist_common(flag_list_all, rettv); } -/// "setdigraph()" function -void f_setdigraph(typval_T *argvars, typval_T *rettv, FunPtr fptr) +/// "digraph_set()" function +void f_digraph_set(typval_T *argvars, typval_T *rettv, FunPtr fptr) { rettv->v_type = VAR_BOOL; rettv->vval.v_bool = kBoolVarFalse; - if (!setdigraph_common(&argvars[0], &argvars[1])) { + if (!digraph_set_common(&argvars[0], &argvars[1])) { return; } rettv->vval.v_bool = kBoolVarTrue; } -/// "setdigraphlist()" function -void f_setdigraphlist(typval_T *argvars, typval_T *rettv, FunPtr fptr) +/// "digraph_setlist()" function +void f_digraph_setlist(typval_T *argvars, typval_T *rettv, FunPtr fptr) { rettv->v_type = VAR_BOOL; rettv->vval.v_bool = kBoolVarFalse; if (argvars[0].v_type != VAR_LIST) { - emsg(_(e_setdigraphlist_argument_must_be_list_of_lists_with_two_items)); + emsg(_(e_digraph_setlist_argument_must_be_list_of_lists_with_two_items)); return; } @@ -1985,18 +1985,18 @@ void f_setdigraphlist(typval_T *argvars, typval_T *rettv, FunPtr fptr) TV_LIST_ITER_CONST(pl, pli, { if (TV_LIST_ITEM_TV(pli)->v_type != VAR_LIST) { - emsg(_(e_setdigraphlist_argument_must_be_list_of_lists_with_two_items)); + emsg(_(e_digraph_setlist_argument_must_be_list_of_lists_with_two_items)); return; } list_T *l = TV_LIST_ITEM_TV(pli)->vval.v_list; if (l == NULL || tv_list_len(l) != 2) { - emsg(_(e_setdigraphlist_argument_must_be_list_of_lists_with_two_items)); + emsg(_(e_digraph_setlist_argument_must_be_list_of_lists_with_two_items)); return; } - if (!setdigraph_common(TV_LIST_ITEM_TV(tv_list_first(l)), - TV_LIST_ITEM_TV(TV_LIST_ITEM_NEXT(l, tv_list_first(l))))) { + if (!digraph_set_common(TV_LIST_ITEM_TV(tv_list_first(l)), + TV_LIST_ITEM_TV(TV_LIST_ITEM_NEXT(l, tv_list_first(l))))) { return; } }); diff --git a/src/nvim/edit.c b/src/nvim/edit.c index 313e23fd3b..47d491033b 100644 --- a/src/nvim/edit.c +++ b/src/nvim/edit.c @@ -9260,7 +9260,7 @@ static int ins_digraph(void) } if (cc != ESC) { AppendToRedobuff(CTRL_V_STR); - c = getdigraph(c, cc, true); + c = digraph_get(c, cc, true); clear_showcmd(); return c; } diff --git a/src/nvim/eval.lua b/src/nvim/eval.lua index 0951cd239d..698cffe2fa 100644 --- a/src/nvim/eval.lua +++ b/src/nvim/eval.lua @@ -102,6 +102,10 @@ return { did_filetype={}, diff_filler={args=1, base=1}, diff_hlID={args=2, base=1}, + digraph_get={args=1, base=1}, + digraph_getlist={args={0, 1}, base=1}, + digraph_set={args=2, base=1}, + digraph_setlist={args=1, base=1}, empty={args=1, base=1}, environ={}, escape={args=2, base=1}, @@ -156,8 +160,6 @@ return { getcurpos={args={0, 1}, base=1}, getcursorcharpos={args={0, 1}, base=1}, getcwd={args={0, 2}, base=1}, - getdigraph={args=1, base=1}, - getdigraphlist={args={0, 1}, base=1}, getenv={args=1, base=1}, getfontname={args={0, 1}}, getfperm={args=1, base=1}, @@ -324,8 +326,6 @@ return { setcharsearch={args=1, base=1}, setcmdpos={args=1, base=1}, setcursorcharpos={args={1, 3}, base=1}, - setdigraph={args=2, base=1}, - setdigraphlist={args=1, base=1}, setenv={args=2, base=2}, setfperm={args=2, base=1}, setline={args=2, base=2}, diff --git a/src/nvim/testdir/test_digraph.vim b/src/nvim/testdir/test_digraph.vim index e9073db649..9fecedfd5b 100644 --- a/src/nvim/testdir/test_digraph.vim +++ b/src/nvim/testdir/test_digraph.vim @@ -505,47 +505,47 @@ func Test_entering_digraph() call StopVimInTerminal(buf) endfunc -func Test_setdigraph_function() +func Test_digraph_set_function() new - call setdigraph('aa', 'あ') + call digraph_set('aa', 'あ') call Put_Dig('aa') call assert_equal('あ', getline('$')) - call setdigraph(' i', 'い') + call digraph_set(' i', 'い') call Put_Dig(' i') call assert_equal('い', getline('$')) - call setdigraph(' ', 'う') + call digraph_set(' ', 'う') call Put_Dig(' ') call assert_equal('う', getline('$')) - eval 'aa'->setdigraph('え') + eval 'aa'->digraph_set('え') call Put_Dig('aa') call assert_equal('え', getline('$')) - call assert_fails('call setdigraph("aaa", "あ")', 'E1214: Digraph must be just two characters: aaa') - call assert_fails('call setdigraph("b", "あ")', 'E1214: Digraph must be just two characters: b') - call assert_fails('call setdigraph("あ", "あ")', 'E1214: Digraph must be just two characters: あ') - call assert_fails('call setdigraph("aa", "ああ")', 'E1215: Digraph must be one character: ああ') - call assert_fails('call setdigraph("aa", "か" .. nr2char(0x3099))', 'E1215: Digraph must be one character: か' .. nr2char(0x3099)) + call assert_fails('call digraph_set("aaa", "あ")', 'E1214: Digraph must be just two characters: aaa') + call assert_fails('call digraph_set("b", "あ")', 'E1214: Digraph must be just two characters: b') + call assert_fails('call digraph_set("あ", "あ")', 'E1214: Digraph must be just two characters: あ') + call assert_fails('call digraph_set("aa", "ああ")', 'E1215: Digraph must be one character: ああ') + call assert_fails('call digraph_set("aa", "か" .. nr2char(0x3099))', 'E1215: Digraph must be one character: か' .. nr2char(0x3099)) bwipe! endfunc -func Test_getdigraph_function() +func Test_digraph_get_function() " Built-in digraphs - call assert_equal('∞', getdigraph('00')) + call assert_equal('∞', digraph_get('00')) " User-defined digraphs - call setdigraph('aa', 'あ') - call setdigraph(' i', 'い') - call setdigraph(' ', 'う') - call assert_equal('あ', getdigraph('aa')) - call assert_equal('あ', 'aa'->getdigraph()) - call assert_equal('い', getdigraph(' i')) - call assert_equal('う', getdigraph(' ')) - call assert_fails('call getdigraph("aaa")', 'E1214: Digraph must be just two characters: aaa') - call assert_fails('call getdigraph("b")', 'E1214: Digraph must be just two characters: b') + call digraph_set('aa', 'あ') + call digraph_set(' i', 'い') + call digraph_set(' ', 'う') + call assert_equal('あ', digraph_get('aa')) + call assert_equal('あ', 'aa'->digraph_get()) + call assert_equal('い', digraph_get(' i')) + call assert_equal('う', digraph_get(' ')) + call assert_fails('call digraph_get("aaa")', 'E1214: Digraph must be just two characters: aaa') + call assert_fails('call digraph_get("b")', 'E1214: Digraph must be just two characters: b') endfunc -func Test_getdigraph_function_encode() +func Test_digraph_get_function_encode() throw 'Skipped: Nvim does not support setting encoding=japan' CheckFeature iconv let testcases = { @@ -553,34 +553,34 @@ func Test_getdigraph_function_encode() \'aa': 'あ', \} for [key, ch] in items(testcases) - call setdigraph(key, ch) + call digraph_set(key, ch) set encoding=japan - call assert_equal(iconv(ch, 'utf-8', 'japan'), getdigraph(key)) + call assert_equal(iconv(ch, 'utf-8', 'japan'), digraph_get(key)) set encoding& endfor endfunc -func Test_setdigraphlist_function() - call setdigraphlist([['aa', 'き'], ['bb', 'く']]) - call assert_equal('き', getdigraph('aa')) - call assert_equal('く', getdigraph('bb')) +func Test_digraph_setlist_function() + call digraph_setlist([['aa', 'き'], ['bb', 'く']]) + call assert_equal('き', digraph_get('aa')) + call assert_equal('く', digraph_get('bb')) - call assert_fails('call setdigraphlist([[]])', 'E1216:') - call assert_fails('call setdigraphlist([["aa", "b", "cc"]])', '1216:') - call assert_fails('call setdigraphlist([["あ", "あ"]])', 'E1214: Digraph must be just two characters: あ') + call assert_fails('call digraph_setlist([[]])', 'E1216:') + call assert_fails('call digraph_setlist([["aa", "b", "cc"]])', '1216:') + call assert_fails('call digraph_setlist([["あ", "あ"]])', 'E1214: Digraph must be just two characters: あ') endfunc -func Test_getdigraphlist_function() +func Test_digraph_getlist_function() " Make sure user-defined digraphs are defined - call setdigraphlist([['aa', 'き'], ['bb', 'く']]) + call digraph_setlist([['aa', 'き'], ['bb', 'く']]) - for pair in getdigraphlist(1) - call assert_equal(getdigraph(pair[0]), pair[1]) + for pair in digraph_getlist(1) + call assert_equal(digraph_get(pair[0]), pair[1]) endfor " We don't know how many digraphs are registered before, so check the number " of digraphs returned. - call assert_equal(getdigraphlist()->len(), getdigraphlist(0)->len()) - call assert_notequal((getdigraphlist()->len()), getdigraphlist(1)->len()) + call assert_equal(digraph_getlist()->len(), digraph_getlist(0)->len()) + call assert_notequal((digraph_getlist()->len()), digraph_getlist(1)->len()) endfunc " vim: shiftwidth=2 sts=2 expandtab -- cgit From 0a813ae2914bee748dd55370929214699c97805f Mon Sep 17 00:00:00 2001 From: zeertzjq Date: Thu, 17 Feb 2022 19:00:15 +0800 Subject: vim-patch:8.2.3325: digraph test fails when LC_ALL is set to "C" Problem: Digraph test fails when LC_ALL is set to "C". Solution: When restoring 'encoding' set it to "utf-8". (closes vim/vim#8742) https://github.com/vim/vim/commit/52eb372a04dfc5d5afef238c1b3c4a8e92020837 --- src/nvim/testdir/test_digraph.vim | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) (limited to 'src') diff --git a/src/nvim/testdir/test_digraph.vim b/src/nvim/testdir/test_digraph.vim index 9fecedfd5b..7b6bc940d3 100644 --- a/src/nvim/testdir/test_digraph.vim +++ b/src/nvim/testdir/test_digraph.vim @@ -548,6 +548,7 @@ endfunc func Test_digraph_get_function_encode() throw 'Skipped: Nvim does not support setting encoding=japan' CheckFeature iconv + let testcases = { \'00': '∞', \'aa': 'あ', @@ -556,7 +557,7 @@ func Test_digraph_get_function_encode() call digraph_set(key, ch) set encoding=japan call assert_equal(iconv(ch, 'utf-8', 'japan'), digraph_get(key)) - set encoding& + set encoding=utf-8 endfor endfunc @@ -583,4 +584,6 @@ func Test_digraph_getlist_function() call assert_equal(digraph_getlist()->len(), digraph_getlist(0)->len()) call assert_notequal((digraph_getlist()->len()), digraph_getlist(1)->len()) endfunc + + " vim: shiftwidth=2 sts=2 expandtab -- cgit From c9d1fcd85064120a722f17cdf28f3e3d89b456b9 Mon Sep 17 00:00:00 2001 From: zeertzjq Date: Tue, 12 Apr 2022 20:36:43 +0800 Subject: refactor(digraph.c): add more const qualifiers --- src/nvim/digraph.c | 26 ++++++++++++-------------- 1 file changed, 12 insertions(+), 14 deletions(-) (limited to 'src') diff --git a/src/nvim/digraph.c b/src/nvim/digraph.c index aaf3ace861..083c868607 100644 --- a/src/nvim/digraph.c +++ b/src/nvim/digraph.c @@ -1482,17 +1482,16 @@ int do_digraph(int c) char_u *get_digraph_for_char(int val_arg) { const int val = val_arg; - digr_T *dp; + const digr_T *dp; static char_u r[3]; for (int use_defaults = 0; use_defaults <= 1; use_defaults++) { if (use_defaults == 0) { - dp = (digr_T *)user_digraphs.ga_data; + dp = (const digr_T *)user_digraphs.ga_data; } else { dp = digraphdefault; } - for (int i = 0; - use_defaults ? dp->char1 != NUL : i < user_digraphs.ga_len; i++) { + for (int i = 0; use_defaults ? dp->char1 != NUL : i < user_digraphs.ga_len; i++) { if (dp->result == val) { r[0] = dp->char1; r[1] = dp->char2; @@ -1560,7 +1559,7 @@ static int getexactdigraph(int char1, int char2, bool meta_char) } // Search user digraphs first. - digr_T *dp = (digr_T *)user_digraphs.ga_data; + const digr_T *dp = (const digr_T *)user_digraphs.ga_data; for (int i = 0; i < user_digraphs.ga_len; i++) { if (((int)dp->char1 == char1) && ((int)dp->char2 == char2)) { retval = dp->result; @@ -1699,7 +1698,7 @@ void listdigraphs(bool use_headers) msg_putchar('\n'); - digr_T *dp = digraphdefault; + const digr_T *dp = digraphdefault; for (int i = 0; dp->char1 != NUL && !got_int; i++) { digr_T tmp; @@ -1709,15 +1708,14 @@ void listdigraphs(bool use_headers) tmp.char2 = dp->char2; tmp.result = getexactdigraph(tmp.char1, tmp.char2, false); - if ((tmp.result != 0) - && (tmp.result != tmp.char2)) { + if (tmp.result != 0 && tmp.result != tmp.char2) { printdigraph(&tmp, use_headers ? &previous : NULL); } dp++; fast_breakcheck(); } - dp = (digr_T *)user_digraphs.ga_data; + dp = (const digr_T *)user_digraphs.ga_data; for (int i = 0; i < user_digraphs.ga_len && !got_int; i++) { if (previous >= 0 && use_headers) { digraph_header(_("Custom")); @@ -1729,7 +1727,7 @@ void listdigraphs(bool use_headers) } } -static void digraph_getlist_appendpair(digr_T *dp, list_T *l) +static void digraph_getlist_appendpair(const digr_T *dp, list_T *l) { list_T *l2 = tv_list_alloc(2); tv_list_append_list(l, l2); @@ -1750,7 +1748,7 @@ void digraph_getlist_common(bool list_all, typval_T *rettv) { tv_list_alloc_ret(rettv, (int)sizeof(digraphdefault) + user_digraphs.ga_len); - digr_T *dp; + const digr_T *dp; if (list_all) { dp = digraphdefault; @@ -1766,7 +1764,7 @@ void digraph_getlist_common(bool list_all, typval_T *rettv) } } - dp = (digr_T *)user_digraphs.ga_data; + dp = (const digr_T *)user_digraphs.ga_data; for (int i = 0; i < user_digraphs.ga_len && !got_int; i++) { digraph_getlist_appendpair(dp, rettv->vval.v_list); dp++; @@ -1865,7 +1863,7 @@ static void printdigraph(const digr_T *dp, result_T *previous) /// Get the two digraph characters from a typval. /// @return OK or FAIL. -static int get_digraph_chars(typval_T *arg, int *char1, int *char2) +static int get_digraph_chars(const typval_T *arg, int *char1, int *char2) { char buf_chars[NUMBUFLEN]; const char *chars = tv_get_string_buf_chk(arg, buf_chars); @@ -1889,7 +1887,7 @@ static int get_digraph_chars(typval_T *arg, int *char1, int *char2) return FAIL; } -static bool digraph_set_common(typval_T *argchars, typval_T *argdigraph) +static bool digraph_set_common(const typval_T *argchars, const typval_T *argdigraph) { int char1, char2; if (get_digraph_chars(argchars, &char1, &char2) == FAIL) { -- cgit