diff options
Diffstat (limited to 'src')
-rw-r--r-- | src/nvim/eval.lua | 1 | ||||
-rw-r--r-- | src/nvim/eval/funcs.c | 160 | ||||
-rw-r--r-- | src/nvim/ops.c | 17 | ||||
-rw-r--r-- | src/nvim/ops.h | 7 | ||||
-rw-r--r-- | src/nvim/testdir/test_eval_stuff.vim | 126 | ||||
-rw-r--r-- | src/nvim/testdir/test_registers.vim | 80 |
6 files changed, 364 insertions, 27 deletions
diff --git a/src/nvim/eval.lua b/src/nvim/eval.lua index c0a18b3236..762d741fb7 100644 --- a/src/nvim/eval.lua +++ b/src/nvim/eval.lua @@ -166,6 +166,7 @@ return { getpos={args=1}, getqflist={args={0, 1}}, getreg={args={0, 3}}, + getreginfo={args={0, 1}, base=1}, getregtype={args={0, 1}}, gettabinfo={args={0, 1}}, gettabvar={args={2, 3}}, diff --git a/src/nvim/eval/funcs.c b/src/nvim/eval/funcs.c index e7fb6ed504..ff91f13444 100644 --- a/src/nvim/eval/funcs.c +++ b/src/nvim/eval/funcs.c @@ -7218,6 +7218,61 @@ static void f_readfile(typval_T *argvars, typval_T *rettv, FunPtr fptr) fclose(fd); } +/// "getreginfo()" function +static void f_getreginfo(typval_T *argvars, typval_T *rettv, FunPtr fptr) +{ + const char *strregname; + if (argvars[0].v_type != VAR_UNKNOWN) { + strregname = tv_get_string_chk(&argvars[0]); + if (strregname == NULL) { + return; + } + } else { + strregname = (const char *)get_vim_var_str(VV_REG); + } + + int regname = (strregname == NULL ? '"' : *strregname); + if (regname == 0 || regname == '@') { + regname = '"'; + } + + tv_dict_alloc_ret(rettv); + dict_T *const dict = rettv->vval.v_dict; + + list_T *const list = get_reg_contents(regname, kGRegExprSrc | kGRegList); + if (list == NULL) { + return; + } + tv_dict_add_list(dict, S_LEN("regcontents"), list); + + char buf[NUMBUFLEN + 2]; + buf[0] = NUL; + buf[1] = NUL; + colnr_T reglen = 0; + switch (get_reg_type(regname, ®len)) { + case kMTLineWise: + buf[0] = 'V'; + break; + case kMTCharWise: + buf[0] = 'v'; + break; + case kMTBlockWise: + vim_snprintf(buf, sizeof(buf), "%c%d", Ctrl_V, reglen + 1); + break; + case kMTUnknown: + abort(); + } + tv_dict_add_str(dict, S_LEN("regtype"), buf); + + buf[0] = get_register_name(get_unname_register()); + buf[1] = NUL; + if (regname == '"') { + tv_dict_add_str(dict, S_LEN("points_to"), buf); + } else { + tv_dict_add_bool(dict, S_LEN("isunnamed"), regname == buf[0] ? kBoolVarTrue : kBoolVarFalse); + } +} + // "reg_executing()" function static void f_reg_executing(typval_T *argvars, typval_T *rettv, FunPtr fptr) { @@ -9039,6 +9094,36 @@ static void f_setqflist(typval_T *argvars, typval_T *rettv, FunPtr fptr) set_qf_ll_list(NULL, argvars, rettv); } +/// Translate a register type string to the yank type and block length +static int get_yank_type(char_u **const pp, MotionType *const yank_type, long *const block_len) + FUNC_ATTR_NONNULL_ALL +{ + char_u *stropt = *pp; + switch (*stropt) { + case 'v': + case 'c': // character-wise selection + *yank_type = kMTCharWise; + break; + case 'V': + case 'l': // line-wise selection + *yank_type = kMTLineWise; + break; + case 'b': + case Ctrl_V: // block-wise selection + *yank_type = kMTBlockWise; + if (ascii_isdigit(stropt[1])) { + stropt++; + *block_len = getdigits_long(&stropt, false, 0) - 1; + stropt--; + } + break; + default: + return FAIL; + } + *pp = stropt; + return OK; +} + /* * "setreg()" function */ @@ -9063,8 +9148,53 @@ static void f_setreg(typval_T *argvars, typval_T *rettv, FunPtr fptr) regname = '"'; } + const typval_T *regcontents = NULL; + int pointreg = 0; + if (argvars[1].v_type == VAR_DICT) { + dict_T *const d = argvars[1].vval.v_dict; + + if (tv_dict_len(d) == 0) { + // Empty dict, clear the register (like setreg(0, [])) + char_u *lstval[2] = { NULL, NULL }; + write_reg_contents_lst(regname, lstval, false, kMTUnknown, -1); + return; + } + + dictitem_T *const di = tv_dict_find(d, "regcontents", -1); + if (di != NULL) { + regcontents = &di->di_tv; + } + + const char *stropt = tv_dict_get_string(d, "regtype", false); + if (stropt != NULL) { + const int ret = get_yank_type((char_u **)&stropt, &yank_type, &block_len); + + if (ret == FAIL || *(++stropt) != NUL) { + EMSG2(_(e_invargval), "value"); + return; + } + } + + if (regname == '"') { + stropt = tv_dict_get_string(d, "points_to", false); + if (stropt != NULL) { + pointreg = *stropt; + regname = pointreg; + } + } else if (tv_dict_get_number(d, "isunnamed")) { + pointreg = regname; + } + } else { + regcontents = &argvars[1]; + } + bool set_unnamed = false; if (argvars[2].v_type != VAR_UNKNOWN) { + if (yank_type != kMTUnknown) { + EMSG2(_(e_toomanyarg), "setreg"); + return; + } + const char *stropt = tv_get_string_chk(&argvars[2]); if (stropt == NULL) { return; // Type error. @@ -9075,33 +9205,18 @@ static void f_setreg(typval_T *argvars, typval_T *rettv, FunPtr fptr) case 'A': // append append = true; break; - case 'v': - case 'c': // character-wise selection - yank_type = kMTCharWise; - break; - case 'V': - case 'l': // line-wise selection - yank_type = kMTLineWise; - break; - case 'b': - case Ctrl_V: // block-wise selection - yank_type = kMTBlockWise; - if (ascii_isdigit(stropt[1])) { - stropt++; - block_len = getdigits_long((char_u **)&stropt, true, 0) - 1; - stropt--; - } - break; case 'u': case '"': // unnamed register set_unnamed = true; break; + default: + get_yank_type((char_u **)&stropt, &yank_type, &block_len); } } } - if (argvars[1].v_type == VAR_LIST) { - list_T *ll = argvars[1].vval.v_list; + if (regcontents != NULL && regcontents->v_type == VAR_LIST) { + list_T *const ll = regcontents->vval.v_list; // If the list is NULL handle like an empty list. const int len = tv_list_len(ll); @@ -9137,14 +9252,17 @@ free_lstval: xfree(*--curallocval); } xfree(lstval); - } else { - const char *strval = tv_get_string_chk(&argvars[1]); + } else if (regcontents != NULL) { + const char *const strval = tv_get_string_chk(regcontents); if (strval == NULL) { return; } write_reg_contents_ex(regname, (const char_u *)strval, STRLEN(strval), append, yank_type, block_len); } + if (pointreg != 0) { + get_yank_register(pointreg, YREG_YANK); + } rettv->vval.v_number = 0; if (set_unnamed) { diff --git a/src/nvim/ops.c b/src/nvim/ops.c index 12fb8439f1..1d737ee9fc 100644 --- a/src/nvim/ops.c +++ b/src/nvim/ops.c @@ -804,12 +804,6 @@ bool valid_yank_reg(int regname, bool writing) return false; } -typedef enum { - YREG_PASTE, - YREG_YANK, - YREG_PUT, -} yreg_mode_t; - /// Return yankreg_T to use, according to the value of `regname`. /// Cannot handle the '_' (black hole) register. /// Must only be called with a valid register name! @@ -3650,6 +3644,12 @@ int get_register_name(int num) } } +/// @return the index of the register "" points to. +int get_unname_register(void) +{ + return y_previous == NULL ? -1 : (int)(y_previous - &y_regs[0]); +} + /* * ":dis" and ":registers": Display the contents of the yank registers. */ @@ -5560,6 +5560,11 @@ static void str_to_reg(yankreg_T *y_ptr, MotionType yank_type, const char_u *str } } + // Without any lines make the register empty. + if (y_ptr->y_size + newlines == 0) { + XFREE_CLEAR(y_ptr->y_array); + return; + } // Grow the register array to hold the pointers to the new lines. char_u **pp = xrealloc(y_ptr->y_array, diff --git a/src/nvim/ops.h b/src/nvim/ops.h index 112ffbeaba..5b87746921 100644 --- a/src/nvim/ops.h +++ b/src/nvim/ops.h @@ -90,6 +90,13 @@ typedef struct yankreg { dict_T *additional_data; ///< Additional data from ShaDa file. } yankreg_T; +/// Modes for get_yank_register() +typedef enum { + YREG_PASTE, + YREG_YANK, + YREG_PUT, +} yreg_mode_t; + /// Convert register name into register index /// /// @param[in] regname Register name. diff --git a/src/nvim/testdir/test_eval_stuff.vim b/src/nvim/testdir/test_eval_stuff.vim index f7b6704610..883ba5de3d 100644 --- a/src/nvim/testdir/test_eval_stuff.vim +++ b/src/nvim/testdir/test_eval_stuff.vim @@ -174,6 +174,132 @@ func Test_number_max_min_size() call assert_true(v:numbermax > 9999999) endfunc +func Assert_reg(name, type, value, valuestr, expr, exprstr) + call assert_equal(a:type, getregtype(a:name)) + call assert_equal(a:value, getreg(a:name)) + call assert_equal(a:valuestr, string(getreg(a:name, 0, 1))) + call assert_equal(a:expr, getreg(a:name, 1)) + call assert_equal(a:exprstr, string(getreg(a:name, 1, 1))) +endfunc + +func Test_let_register() + let @" = 'abc' + call Assert_reg('"', 'v', "abc", "['abc']", "abc", "['abc']") + let @" = "abc\n" + call Assert_reg('"', 'V', "abc\n", "['abc']", "abc\n", "['abc']") + let @" = "abc\<C-m>" + call Assert_reg('"', 'V', "abc\r\n", "['abc\r']", "abc\r\n", "['abc\r']") + let @= = '"abc"' + call Assert_reg('=', 'v', "abc", "['abc']", '"abc"', "['\"abc\"']") +endfunc + +func Assert_regput(name, result) + new + execute "silent normal! o==\n==\e\"" . a:name . "P" + call assert_equal(a:result, getline(2, line('$'))) + bwipe! +endfunc + +func Test_setreg_basic() + call setreg('a', 'abcA', 'c') + call Assert_reg('a', 'v', "abcA", "['abcA']", "abcA", "['abcA']") + call Assert_regput('a', ['==', '=abcA=']) + + call setreg('A', 'abcAc', 'c') + call Assert_reg('A', 'v', "abcAabcAc", "['abcAabcAc']", "abcAabcAc", "['abcAabcAc']") + call Assert_regput('a', ['==', '=abcAabcAc=']) + + call setreg('A', 'abcAl', 'l') + call Assert_reg('A', 'V', "abcAabcAcabcAl\n", "['abcAabcAcabcAl']", "abcAabcAcabcAl\n", "['abcAabcAcabcAl']") + call Assert_regput('a', ['==', 'abcAabcAcabcAl', '==']) + + call setreg('A', 'abcAc2','c') + call Assert_reg('A', 'v', "abcAabcAcabcAl\nabcAc2", "['abcAabcAcabcAl', 'abcAc2']", "abcAabcAcabcAl\nabcAc2", "['abcAabcAcabcAl', 'abcAc2']") + call Assert_regput('a', ['==', '=abcAabcAcabcAl', 'abcAc2=']) + + call setreg('b', 'abcB', 'v') + call Assert_reg('b', 'v', "abcB", "['abcB']", "abcB", "['abcB']") + call Assert_regput('b', ['==', '=abcB=']) + + call setreg('b', 'abcBc', 'ca') + call Assert_reg('b', 'v', "abcBabcBc", "['abcBabcBc']", "abcBabcBc", "['abcBabcBc']") + call Assert_regput('b', ['==', '=abcBabcBc=']) + + call setreg('b', 'abcBb', 'ba') + call Assert_reg('b', "\<C-V>5", "abcBabcBcabcBb", "['abcBabcBcabcBb']", "abcBabcBcabcBb", "['abcBabcBcabcBb']") + call Assert_regput('b', ['==', '=abcBabcBcabcBb=']) + + call setreg('b', 'abcBc2','ca') + call Assert_reg('b', "v", "abcBabcBcabcBb\nabcBc2", "['abcBabcBcabcBb', 'abcBc2']", "abcBabcBcabcBb\nabcBc2", "['abcBabcBcabcBb', 'abcBc2']") + call Assert_regput('b', ['==', '=abcBabcBcabcBb', 'abcBc2=']) + + call setreg('b', 'abcBb2','b50a') + call Assert_reg('b', "\<C-V>50", "abcBabcBcabcBb\nabcBc2abcBb2", "['abcBabcBcabcBb', 'abcBc2abcBb2']", "abcBabcBcabcBb\nabcBc2abcBb2", "['abcBabcBcabcBb', 'abcBc2abcBb2']") + call Assert_regput('b', ['==', '=abcBabcBcabcBb =', ' abcBc2abcBb2']) + + call setreg('c', 'abcC', 'l') + call Assert_reg('c', 'V', "abcC\n", "['abcC']", "abcC\n", "['abcC']") + call Assert_regput('c', ['==', 'abcC', '==']) + + call setreg('C', 'abcCl', 'l') + call Assert_reg('C', 'V', "abcC\nabcCl\n", "['abcC', 'abcCl']", "abcC\nabcCl\n", "['abcC', 'abcCl']") + call Assert_regput('c', ['==', 'abcC', 'abcCl', '==']) + + call setreg('C', 'abcCc', 'c') + call Assert_reg('C', 'v', "abcC\nabcCl\nabcCc", "['abcC', 'abcCl', 'abcCc']", "abcC\nabcCl\nabcCc", "['abcC', 'abcCl', 'abcCc']") + call Assert_regput('c', ['==', '=abcC', 'abcCl', 'abcCc=']) + + call setreg('d', 'abcD', 'V') + call Assert_reg('d', 'V', "abcD\n", "['abcD']", "abcD\n", "['abcD']") + call Assert_regput('d', ['==', 'abcD', '==']) + + call setreg('D', 'abcDb', 'b') + call Assert_reg('d', "\<C-V>5", "abcD\nabcDb", "['abcD', 'abcDb']", "abcD\nabcDb", "['abcD', 'abcDb']") + call Assert_regput('d', ['==', '=abcD =', ' abcDb']) + + call setreg('e', 'abcE', 'b') + call Assert_reg('e', "\<C-V>4", "abcE", "['abcE']", "abcE", "['abcE']") + call Assert_regput('e', ['==', '=abcE=']) + + call setreg('E', 'abcEb', 'b') + call Assert_reg('E', "\<C-V>5", "abcE\nabcEb", "['abcE', 'abcEb']", "abcE\nabcEb", "['abcE', 'abcEb']") + call Assert_regput('e', ['==', '=abcE =', ' abcEb']) + + call setreg('E', 'abcEl', 'l') + call Assert_reg('E', "V", "abcE\nabcEb\nabcEl\n", "['abcE', 'abcEb', 'abcEl']", "abcE\nabcEb\nabcEl\n", "['abcE', 'abcEb', 'abcEl']") + call Assert_regput('e', ['==', 'abcE', 'abcEb', 'abcEl', '==']) + + call setreg('f', 'abcF', "\<C-v>") + call Assert_reg('f', "\<C-V>4", "abcF", "['abcF']", "abcF", "['abcF']") + call Assert_regput('f', ['==', '=abcF=']) + + call setreg('F', 'abcFc', 'c') + call Assert_reg('F', "v", "abcF\nabcFc", "['abcF', 'abcFc']", "abcF\nabcFc", "['abcF', 'abcFc']") + call Assert_regput('f', ['==', '=abcF', 'abcFc=']) + + call setreg('g', 'abcG', 'b10') + call Assert_reg('g', "\<C-V>10", "abcG", "['abcG']", "abcG", "['abcG']") + call Assert_regput('g', ['==', '=abcG =']) + + call setreg('h', 'abcH', "\<C-v>10") + call Assert_reg('h', "\<C-V>10", "abcH", "['abcH']", "abcH", "['abcH']") + call Assert_regput('h', ['==', '=abcH =']) + + call setreg('I', 'abcI') + call Assert_reg('I', "v", "abcI", "['abcI']", "abcI", "['abcI']") + call Assert_regput('I', ['==', '=abcI=']) + + " Error cases + call assert_fails('call setreg()', 'E119:') + call assert_fails('call setreg(1)', 'E119:') + call assert_fails('call setreg(1, 2, 3, 4)', 'E118:') + call assert_fails('call setreg([], 2)', 'E730:') + call assert_fails('call setreg(1, 2, [])', 'E730:') + call assert_fails('call setreg("/", ["1", "2"])', 'E883:') + call assert_fails('call setreg("=", ["1", "2"])', 'E883:') + call assert_fails('call setreg(1, ["", "", [], ""])', 'E730:') +endfunc + func Test_curly_assignment() let s:svar = 'svar' let g:gvar = 'gvar' diff --git a/src/nvim/testdir/test_registers.vim b/src/nvim/testdir/test_registers.vim index 53069b3d31..fd8653a2eb 100644 --- a/src/nvim/testdir/test_registers.vim +++ b/src/nvim/testdir/test_registers.vim @@ -283,4 +283,84 @@ func Test_insert_small_delete() bwipe! endfunc +" Test for getting register info +func Test_get_reginfo() + enew + call setline(1, ['foo', 'bar']) + + exe 'norm! "zyy' + let info = getreginfo('"') + call assert_equal('z', info.points_to) + call setreg('y', 'baz') + call assert_equal('z', getreginfo('').points_to) + call setreg('y', { 'isunnamed': v:true }) + call assert_equal('y', getreginfo('"').points_to) + + exe '$put' + call assert_equal(getreg('y'), getline(3)) + call setreg('', 'qux') + call assert_equal('0', getreginfo('').points_to) + call setreg('x', 'quux') + call assert_equal('0', getreginfo('').points_to) + + let info = getreginfo('') + call assert_equal(getreg('', 1, 1), info.regcontents) + call assert_equal(getregtype(''), info.regtype) + + exe "norm! 0\<c-v>e" .. '"zy' + let info = getreginfo('z') + call assert_equal(getreg('z', 1, 1), info.regcontents) + call assert_equal(getregtype('z'), info.regtype) + call assert_equal(1, +info.isunnamed) + + let info = getreginfo('"') + call assert_equal('z', info.points_to) + + bwipe! +endfunc + +" Test for restoring register with dict from getreginfo +func Test_set_register_dict() + enew! + + call setreg('"', #{ regcontents: ['one', 'two'], + \ regtype: 'V', points_to: 'z' }) + call assert_equal(['one', 'two'], getreg('"', 1, 1)) + let info = getreginfo('"') + call assert_equal('z', info.points_to) + call assert_equal('V', info.regtype) + call assert_equal(1, +getreginfo('z').isunnamed) + + call setreg('x', #{ regcontents: ['three', 'four'], + \ regtype: 'v', isunnamed: v:true }) + call assert_equal(['three', 'four'], getreg('"', 1, 1)) + let info = getreginfo('"') + call assert_equal('x', info.points_to) + call assert_equal('v', info.regtype) + call assert_equal(1, +getreginfo('x').isunnamed) + + call setreg('y', #{ regcontents: 'five', + \ regtype: "\<c-v>", isunnamed: v:false }) + call assert_equal("\<c-v>4", getreginfo('y').regtype) + call assert_equal(0, +getreginfo('y').isunnamed) + call assert_equal(['three', 'four'], getreg('"', 1, 1)) + call assert_equal('x', getreginfo('"').points_to) + + call setreg('"', #{ regcontents: 'six' }) + call assert_equal('0', getreginfo('"').points_to) + call assert_equal(1, +getreginfo('0').isunnamed) + call assert_equal(['six'], getreginfo('0').regcontents) + call assert_equal(['six'], getreginfo('"').regcontents) + + let @x = 'one' + call setreg('x', {}) + call assert_equal(1, len(split(execute('reg x'), '\n'))) + + call assert_fails("call setreg('0', #{regtype: 'V'}, 'v')", 'E118:') + call assert_fails("call setreg('0', #{regtype: 'X'})", 'E475:') + call assert_fails("call setreg('0', #{regtype: 'vy'})", 'E475:') + + bwipe! +endfunc + " vim: shiftwidth=2 sts=2 expandtab |