]> granicus.if.org Git - vim/commitdiff
patch 8.2.2616: Vim9: if 'cpo' is change in Vim9 script it may be restored v8.2.2616
authorBram Moolenaar <Bram@vim.org>
Wed, 17 Mar 2021 16:46:00 +0000 (17:46 +0100)
committerBram Moolenaar <Bram@vim.org>
Wed, 17 Mar 2021 16:46:00 +0000 (17:46 +0100)
Problem:    Vim9: if 'cpo' is change in Vim9 script it may be restored.
Solution:   Apply the changes to 'cpo' to the restored value.

runtime/doc/vim9.txt
src/scriptfile.c
src/testdir/test_vim9_script.vim
src/version.c

index 34048ece8e5b124368c27ea5bd5a57966d2335a1..4e8016c9dba51c85d38cf5a4c03a914468c84e21 100644 (file)
@@ -1082,7 +1082,9 @@ A side effect of `:vim9script` is that the 'cpoptions' option is set to the
 Vim default value, like with: >
        :set cpo&vim
 One of the effects is that |line-continuation| is always enabled.
-The original value of 'cpoptions' is restored at the end of the script.
+The original value of 'cpoptions' is restored at the end of the script, while
+flags added or removed in the script are also added to or removed from the
+original value to get the same effect.  The order of flags may change.
 
                                                        *vim9-mix*
 There is one way to use both legacy and Vim9 syntax in one script file: >
index 1e4b5ccf7efa5af4b6b9628880f012d358e227f4..203cad09fdd773196a04beb1e0c23403edee6df7 100644 (file)
@@ -1459,6 +1459,33 @@ almosttheend:
     si = SCRIPT_ITEM(current_sctx.sc_sid);
     if (si->sn_save_cpo != NULL)
     {
+       if (STRCMP(p_cpo, CPO_VIM) != 0)
+       {
+           char_u *f;
+           char_u *t;
+
+           // 'cpo' was changed in the script.  Apply the same change to the
+           // saved value, if possible.
+           for (f = (char_u *)CPO_VIM; *f != NUL; ++f)
+               if (vim_strchr(p_cpo, *f) == NULL
+                       && (t = vim_strchr(si->sn_save_cpo, *f)) != NULL)
+                   // flag was removed, also remove it from the saved 'cpo'
+                   mch_memmove(t, t + 1, STRLEN(t));
+           for (f = p_cpo; *f != NUL; ++f)
+               if (vim_strchr((char_u *)CPO_VIM, *f) == NULL
+                       && vim_strchr(si->sn_save_cpo, *f) == NULL)
+               {
+                   // flag was added, also add it to the saved 'cpo'
+                   t = alloc(STRLEN(si->sn_save_cpo) + 2);
+                   if (t != NULL)
+                   {
+                       *t = *f;
+                       STRCPY(t + 1, si->sn_save_cpo);
+                       vim_free(si->sn_save_cpo);
+                       si->sn_save_cpo = t;
+                   }
+               }
+       }
        set_option_value((char_u *)"cpo", 0L, si->sn_save_cpo, OPT_NO_REDRAW);
        VIM_CLEAR(si->sn_save_cpo);
     }
index 956de369e16f59d63b840680b707a179181fafe6..38d0b0abcd0833b0f31d41c738086fbe54254121 100644 (file)
@@ -1250,17 +1250,23 @@ def Test_vim9_import_export()
   delete('Xexport.vim')
 
   # Check that in a Vim9 script 'cpo' is set to the Vim default.
-  set cpo&vi
-  var cpo_before = &cpo
+  # Flags added or removed are also applied to the restored value.
+  set cpo=abcd
   var lines =<< trim END
     vim9script
     g:cpo_in_vim9script = &cpo
+    set cpo+=f
+    set cpo-=c
+    g:cpo_after_vim9script = &cpo
   END
   writefile(lines, 'Xvim9_script')
   source Xvim9_script
-  assert_equal(cpo_before, &cpo)
+  assert_equal('fabd', &cpo)
   set cpo&vim
   assert_equal(&cpo, g:cpo_in_vim9script)
+  var newcpo = substitute(&cpo, 'c', '', '') .. 'f'
+  assert_equal(newcpo, g:cpo_after_vim9script)
+
   delete('Xvim9_script')
 enddef
 
index 425dfaacfd3326d0e164abcc477655c756ab6937..a92db82f344060c28f02d4eb458a46a3594799ee 100644 (file)
@@ -750,6 +750,8 @@ static char *(features[]) =
 
 static int included_patches[] =
 {   /* Add new patch number below this line */
+/**/
+    2616,
 /**/
     2615,
 /**/