To: vim_dev@googlegroups.com Subject: Patch 8.2.3901 Fcc: outbox From: Bram Moolenaar Mime-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit ------------ Patch 8.2.3901 Problem: Vim9: Cannot set 'cpo' in main .vimrc if using Vim9 script. Solution: Do not restore 'cpo' at the end of the main .vimrc. Files: runtime/doc/vim9.txt, runtime/doc/options.txt, src/scriptfile.c, src/structs.h, src/testdir/test_vim9_script.vim *** ../vim-8.2.3900/runtime/doc/vim9.txt 2021-12-17 12:42:26.833710986 +0000 --- runtime/doc/vim9.txt 2021-12-26 11:02:28.829726456 +0000 *************** *** 1293,1298 **** --- 1404,1410 ---- 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. + In the |vimrc| file sourced on startup this does not happen. *vim9-mix* There is one way to use both legacy and Vim9 syntax in one script file: > *** ../vim-8.2.3900/runtime/doc/options.txt 2021-12-26 10:51:33.707079472 +0000 --- runtime/doc/options.txt 2021-12-26 12:01:04.207698251 +0000 *************** *** 2133,2140 **** --- 2133,2150 ---- Commas can be added for readability. To avoid problems with flags that are added in the future, use the "+=" and "-=" feature of ":set" |add-option-flags|. + NOTE: This option is set to the Vi default value when 'compatible' is set and to the Vim default value when 'compatible' is reset. + + NOTE: In a |Vim9| script, when `vim9script` is encountered, the value + is saved, 'cpoptions' is set to the Vim default, and the saved value + is restored at the end of the script. Changes to the value of + 'cpoptions' will be applied to the saved value, but keep in mind that + removing a flag that is not present when 'cpoptions' is changed has no + effect. In the |.vimrc| file the value is not restored, thus using + `vim9script` in the |.vimrc| file results in using the Vim default. + NOTE: This option is set to the POSIX default value at startup when the Vi default value would be used and the $VIM_POSIX environment variable exists |posix|. This means Vim tries to behave like the *** ../vim-8.2.3900/src/scriptfile.c 2021-11-22 15:37:11.748625338 +0000 --- src/scriptfile.c 2021-12-26 11:12:44.504236063 +0000 *************** *** 1372,1377 **** --- 1373,1381 ---- if (ret_sid != NULL) *ret_sid = current_sctx.sc_sid; + // Remember the "is_vimrc" flag for when the file is sourced again. + si->sn_is_vimrc = is_vimrc; + // Used to check script variable index is still valid. si->sn_script_seq = current_sctx.sc_seq; } *************** *** 1471,1479 **** #ifdef FEAT_EVAL almosttheend: // Get "si" again, "script_items" may have been reallocated. si = SCRIPT_ITEM(current_sctx.sc_sid); ! if (si->sn_save_cpo != NULL) { if (STRCMP(p_cpo, CPO_VIM) != 0) { --- 1475,1485 ---- #ifdef FEAT_EVAL almosttheend: + // If "sn_save_cpo" is set that means we encountered "vim9script": restore + // 'cpoptions', unless in the main .vimrc file. // Get "si" again, "script_items" may have been reallocated. si = SCRIPT_ITEM(current_sctx.sc_sid); ! if (si->sn_save_cpo != NULL && si->sn_is_vimrc == DOSO_NONE) { if (STRCMP(p_cpo, CPO_VIM) != 0) { *************** *** 1503,1510 **** } } set_option_value((char_u *)"cpo", 0L, si->sn_save_cpo, OPT_NO_REDRAW); - VIM_CLEAR(si->sn_save_cpo); } restore_funccal(); # ifdef FEAT_PROFILE --- 1509,1516 ---- } } set_option_value((char_u *)"cpo", 0L, si->sn_save_cpo, OPT_NO_REDRAW); } + VIM_CLEAR(si->sn_save_cpo); restore_funccal(); # ifdef FEAT_PROFILE *** ../vim-8.2.3900/src/structs.h 2021-12-22 19:44:55.478555112 +0000 --- src/structs.h 2021-12-26 11:09:01.752786477 +0000 *************** *** 1866,1871 **** --- 1866,1872 ---- int sn_version; // :scriptversion int sn_state; // SN_STATE_ values char_u *sn_save_cpo; // 'cpo' value when :vim9script found + char sn_is_vimrc; // .vimrc file, do not restore 'cpo' # ifdef FEAT_PROFILE int sn_prof_on; // TRUE when script is/was profiled *** ../vim-8.2.3900/src/testdir/test_vim9_script.vim 2021-12-25 21:43:24.300302785 +0000 --- src/testdir/test_vim9_script.vim 2021-12-26 11:56:19.832232092 +0000 *************** *** 4270,4287 **** delete('Xclose') delete('Xdone') ! writefile(['vim9script'], 'XanotherScript') set cpo=aABceFsMny> edit XanotherScript so % assert_equal('aABceFsMny>', &cpo) :1del w so % assert_equal('aABceFsMny>', &cpo) delete('XanotherScript') set cpo&vim enddef " Use :function so we can use Check commands --- 4270,4333 ---- delete('Xclose') delete('Xdone') ! writefile(['vim9script', 'g:cpoval = &cpo'], 'XanotherScript') set cpo=aABceFsMny> edit XanotherScript so % assert_equal('aABceFsMny>', &cpo) + assert_equal('aABceFs', g:cpoval) :1del + setline(1, 'let g:cpoval = &cpo') w so % assert_equal('aABceFsMny>', &cpo) + assert_equal('aABceFsMny>', g:cpoval) delete('XanotherScript') set cpo&vim + unlet g:cpoval + + if has('unix') + # 'cpo' is not restored in main vimrc + var save_HOME = $HOME + $HOME = getcwd() .. '/Xhome' + mkdir('Xhome') + var lines =<< trim END + vim9script + writefile(['before: ' .. &cpo], 'Xresult') + set cpo+=M + writefile(['after: ' .. &cpo], 'Xresult', 'a') + END + writefile(lines, 'Xhome/.vimrc') + + lines =<< trim END + call writefile(['later: ' .. &cpo], 'Xresult', 'a') + END + writefile(lines, 'Xlegacy') + + lines =<< trim END + vim9script + call writefile(['vim9: ' .. &cpo], 'Xresult', 'a') + qa + END + writefile(lines, 'Xvim9') + + var cmd = GetVimCommand() .. " -S Xlegacy -S Xvim9" + cmd = substitute(cmd, '-u NONE', '', '') + exe "silent !" .. cmd + + assert_equal([ + 'before: aABceFs', + 'after: aABceFsM', + 'later: aABceFsM', + 'vim9: aABceFs'], readfile('Xresult')) + + $HOME = save_HOME + delete('Xhome', 'rf') + delete('Xlegacy') + delete('Xvim9') + delete('Xresult') + endif enddef " Use :function so we can use Check commands *** ../vim-8.2.3900/src/version.c 2021-12-26 10:51:33.711079465 +0000 --- src/version.c 2021-12-26 12:04:10.107334012 +0000 *************** *** 751,752 **** --- 751,754 ---- { /* Add new patch number below this line */ + /**/ + 3901, /**/ -- hundred-and-one symptoms of being an internet addict: 111. You and your friends get together regularly on IRC, even though all of you live in the same street. /// Bram Moolenaar -- Bram@Moolenaar.net -- http://www.Moolenaar.net \\\ /// \\\ \\\ sponsor Vim, vote for features -- http://www.Vim.org/sponsor/ /// \\\ help me help AIDS victims -- http://ICCF-Holland.org ///