Skip to content

Commit 3e19169

Browse files
committed
patch 8.2.2616: Vim9: if 'cpo' is change in Vim9 script it may be restored
Problem: Vim9: if 'cpo' is change in Vim9 script it may be restored. Solution: Apply the changes to 'cpo' to the restored value.
1 parent a4c81be commit 3e19169

File tree

4 files changed

+41
-4
lines changed

4 files changed

+41
-4
lines changed

runtime/doc/vim9.txt

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1082,7 +1082,9 @@ A side effect of `:vim9script` is that the 'cpoptions' option is set to the
10821082
Vim default value, like with: >
10831083
:set cpo&vim
10841084
One of the effects is that |line-continuation| is always enabled.
1085-
The original value of 'cpoptions' is restored at the end of the script.
1085+
The original value of 'cpoptions' is restored at the end of the script, while
1086+
flags added or removed in the script are also added to or removed from the
1087+
original value to get the same effect. The order of flags may change.
10861088

10871089
*vim9-mix*
10881090
There is one way to use both legacy and Vim9 syntax in one script file: >

src/scriptfile.c

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1459,6 +1459,33 @@ do_source(
14591459
si = SCRIPT_ITEM(current_sctx.sc_sid);
14601460
if (si->sn_save_cpo != NULL)
14611461
{
1462+
if (STRCMP(p_cpo, CPO_VIM) != 0)
1463+
{
1464+
char_u *f;
1465+
char_u *t;
1466+
1467+
// 'cpo' was changed in the script. Apply the same change to the
1468+
// saved value, if possible.
1469+
for (f = (char_u *)CPO_VIM; *f != NUL; ++f)
1470+
if (vim_strchr(p_cpo, *f) == NULL
1471+
&& (t = vim_strchr(si->sn_save_cpo, *f)) != NULL)
1472+
// flag was removed, also remove it from the saved 'cpo'
1473+
mch_memmove(t, t + 1, STRLEN(t));
1474+
for (f = p_cpo; *f != NUL; ++f)
1475+
if (vim_strchr((char_u *)CPO_VIM, *f) == NULL
1476+
&& vim_strchr(si->sn_save_cpo, *f) == NULL)
1477+
{
1478+
// flag was added, also add it to the saved 'cpo'
1479+
t = alloc(STRLEN(si->sn_save_cpo) + 2);
1480+
if (t != NULL)
1481+
{
1482+
*t = *f;
1483+
STRCPY(t + 1, si->sn_save_cpo);
1484+
vim_free(si->sn_save_cpo);
1485+
si->sn_save_cpo = t;
1486+
}
1487+
}
1488+
}
14621489
set_option_value((char_u *)"cpo", 0L, si->sn_save_cpo, OPT_NO_REDRAW);
14631490
VIM_CLEAR(si->sn_save_cpo);
14641491
}

src/testdir/test_vim9_script.vim

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1250,17 +1250,23 @@ def Test_vim9_import_export()
12501250
delete('Xexport.vim')
12511251

12521252
# Check that in a Vim9 script 'cpo' is set to the Vim default.
1253-
set cpo&vi
1254-
var cpo_before = &cpo
1253+
# Flags added or removed are also applied to the restored value.
1254+
set cpo=abcd
12551255
var lines =<< trim END
12561256
vim9script
12571257
g:cpo_in_vim9script = &cpo
1258+
set cpo+=f
1259+
set cpo-=c
1260+
g:cpo_after_vim9script = &cpo
12581261
END
12591262
writefile(lines, 'Xvim9_script')
12601263
source Xvim9_script
1261-
assert_equal(cpo_before, &cpo)
1264+
assert_equal('fabd', &cpo)
12621265
set cpo&vim
12631266
assert_equal(&cpo, g:cpo_in_vim9script)
1267+
var newcpo = substitute(&cpo, 'c', '', '') .. 'f'
1268+
assert_equal(newcpo, g:cpo_after_vim9script)
1269+
12641270
delete('Xvim9_script')
12651271
enddef
12661272

src/version.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -750,6 +750,8 @@ static char *(features[]) =
750750

751751
static int included_patches[] =
752752
{ /* Add new patch number below this line */
753+
/**/
754+
2616,
753755
/**/
754756
2615,
755757
/**/

0 commit comments

Comments
 (0)