To: vim_dev@googlegroups.com Subject: Patch 8.2.1035 Fcc: outbox From: Bram Moolenaar Mime-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit ------------ Patch 8.2.1035 Problem: setreg() does not always clear the register. Solution: Clear the register if the dict argument is empty. (Andy Massimino, closes #3370) Files: src/evalfunc.c, src/testdir/test_registers.vim *** ../vim-8.2.1034/src/evalfunc.c 2020-06-18 18:45:46.001900050 +0200 --- src/evalfunc.c 2020-06-22 19:08:18.058329319 +0200 *************** *** 6002,6012 **** f_test_srand_seed(typval_T *argvars, typval_T *rettv UNUSED) { if (argvars[0].v_type == VAR_UNKNOWN) ! srand_seed_for_testing_is_used = FALSE; else { ! srand_seed_for_testing = (UINT32_T)tv_get_number(&argvars[0]); ! srand_seed_for_testing_is_used = TRUE; } } --- 6002,6012 ---- f_test_srand_seed(typval_T *argvars, typval_T *rettv UNUSED) { if (argvars[0].v_type == VAR_UNKNOWN) ! srand_seed_for_testing_is_used = FALSE; else { ! srand_seed_for_testing = (UINT32_T)tv_get_number(&argvars[0]); ! srand_seed_for_testing_is_used = TRUE; } } *************** *** 6019,6025 **** if (srand_seed_for_testing_is_used) { ! *x = srand_seed_for_testing; return; } #ifndef MSWIN --- 6019,6025 ---- if (srand_seed_for_testing_is_used) { ! *x = srand_seed_for_testing; return; } #ifndef MSWIN *************** *** 7269,7274 **** --- 7269,7305 ---- } /* + * Translate a register type string to the yank type and block length + */ + static int + get_yank_type(char_u **pp, char_u *yank_type, long *block_len) + { + char_u *stropt = *pp; + switch (*stropt) + { + case 'v': case 'c': // character-wise selection + *yank_type = MCHAR; + break; + case 'V': case 'l': // line-wise selection + *yank_type = MLINE; + break; + case 'b': case Ctrl_V: // block-wise selection + *yank_type = MBLOCK; + if (VIM_ISDIGIT(stropt[1])) + { + ++stropt; + *block_len = getdigits(&stropt) - 1; + --stropt; + } + break; + default: + return FAIL; + } + *pp = stropt; + return OK; + } + + /* * "setreg()" function */ static void *************** *** 7302,7331 **** if (argvars[1].v_type == VAR_DICT) { dict_T *d = argvars[1].vval.v_dict; ! dictitem_T *di = dict_find(d, (char_u *)"regcontents", -1); if (di != NULL) regcontents = &di->di_tv; stropt = dict_get_string(d, (char_u *)"regtype", FALSE); if (stropt != NULL) ! switch (*stropt) { ! case 'v': // character-wise selection ! yank_type = MCHAR; ! break; ! case 'V': // line-wise selection ! yank_type = MLINE; ! break; ! case Ctrl_V: // block-wise selection ! yank_type = MBLOCK; ! if (VIM_ISDIGIT(stropt[1])) ! { ! ++stropt; ! block_len = getdigits(&stropt) - 1; ! --stropt; ! } ! break; } if (regname == '"') { --- 7333,7363 ---- if (argvars[1].v_type == VAR_DICT) { dict_T *d = argvars[1].vval.v_dict; ! dictitem_T *di; ! ! if (d == NULL || d->dv_hashtab.ht_used == 0) ! { ! // Empty dict, clear the register (like setreg(0, [])) ! char_u *lstval[2] = {NULL, NULL}; ! write_reg_contents_lst(regname, lstval, 0, FALSE, MAUTO, -1); ! return; ! } ! ! di = dict_find(d, (char_u *)"regcontents", -1); if (di != NULL) regcontents = &di->di_tv; stropt = dict_get_string(d, (char_u *)"regtype", FALSE); if (stropt != NULL) ! { ! int ret = get_yank_type(&stropt, &yank_type, &block_len); ! ! if (ret == FAIL || *++stropt != NUL) { ! semsg(_(e_invargval), "value"); ! return; } + } if (regname == '"') { *************** *** 7344,7349 **** --- 7376,7387 ---- if (argvars[2].v_type != VAR_UNKNOWN) { + if (yank_type != MAUTO) + { + semsg(_(e_toomanyarg), "setreg"); + return; + } + stropt = tv_get_string_chk(&argvars[2]); if (stropt == NULL) return; // type error *************** *** 7353,7373 **** case 'a': case 'A': // append append = TRUE; break; ! case 'v': case 'c': // character-wise selection ! yank_type = MCHAR; ! break; ! case 'V': case 'l': // line-wise selection ! yank_type = MLINE; ! break; ! case 'b': case Ctrl_V: // block-wise selection ! yank_type = MBLOCK; ! if (VIM_ISDIGIT(stropt[1])) ! { ! ++stropt; ! block_len = getdigits(&stropt) - 1; ! --stropt; ! } ! break; } } --- 7391,7398 ---- case 'a': case 'A': // append append = TRUE; break; ! default: ! get_yank_type(&stropt, &yank_type, &block_len); } } *** ../vim-8.2.1034/src/testdir/test_registers.vim 2020-06-17 21:47:19.912798036 +0200 --- src/testdir/test_registers.vim 2020-06-22 19:04:22.959291681 +0200 *************** *** 485,490 **** --- 485,498 ---- 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-8.2.1034/src/version.c 2020-06-21 22:17:15.060232625 +0200 --- src/version.c 2020-06-22 19:06:25.962773200 +0200 *************** *** 756,757 **** --- 756,759 ---- { /* Add new patch number below this line */ + /**/ + 1035, /**/ -- Shaw's Principle: Build a system that even a fool can use, and only a fool will want to use it. /// 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 ///