To: vim_dev@googlegroups.com Subject: Patch 8.2.2529 Fcc: outbox From: Bram Moolenaar Mime-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit ------------ Patch 8.2.2529 Problem: Vim9: Not possible to use legacy and Vim9 script in one file. Solution: Vim9: allow for "if false" before :vim9script. (closes #7851) Files: runtime/doc/vim9.txt, src/ex_docmd.c, src/testdir/test_vim9_script.vim *** ../vim-8.2.2528/runtime/doc/vim9.txt 2021-02-14 12:57:32.552655477 +0100 --- runtime/doc/vim9.txt 2021-02-17 21:43:34.541267362 +0100 *************** *** 1051,1059 **** Namespace ~ *vim9-namespace* To recognize a file that can be imported the `vim9script` statement must ! appear as the first statement in the file. It tells Vim to interpret the ! script in its own namespace, instead of the global namespace. If a file ! starts with: > vim9script var myvar = 'yes' Then "myvar" will only exist in this file. While without `vim9script` it would --- 1051,1059 ---- Namespace ~ *vim9-namespace* To recognize a file that can be imported the `vim9script` statement must ! appear as the first statement in the file (see |vim9-mix| for an exception). ! It tells Vim to interpret the script in its own namespace, instead of the ! global namespace. If a file starts with: > vim9script var myvar = 'yes' Then "myvar" will only exist in this file. While without `vim9script` it would *************** *** 1073,1078 **** --- 1073,1099 ---- One of the effects is that |line-continuation| is always enabled. The original value of 'cpoptions' is restored at the end of the script. + *vim9-mix* + There is one way to use both legacy and Vim9 syntax in one script file: > + " comments may go here + if !has('vim9script') + " legacy script commands go here + finish + endif + vim9script + # Vim9 script commands go here + This allows for writing a script that takes advantage of the Vim9 script + syntax if possible, but will also work on an Vim version without it. + + This can only work in two ways: + 1. The "if" statement evaluates to false, the commands up to `endif` are + skipped and `vim9script` is then the first command actually executed. + 2. The "if" statement evaluates to true, the commands up to `endif` are + executed and `finish` bails out before reaching `vim9script`. + + TODO: The "vim9script" feature does not exist yet, it will only be added once + the Vim9 script syntax has been fully implemented. + Export ~ *:export* *:exp* *** ../vim-8.2.2528/src/ex_docmd.c 2021-02-17 14:52:10.539374448 +0100 --- src/ex_docmd.c 2021-02-17 21:53:11.778180921 +0100 *************** *** 2595,2602 **** #ifdef FEAT_EVAL // Set flag that any command was executed, used by ex_vim9script(). if (getline_equal(ea.getline, ea.cookie, getsourceline) ! && current_sctx.sc_sid > 0) SCRIPT_ITEM(current_sctx.sc_sid)->sn_state = SN_STATE_HAD_COMMAND; /* --- 2595,2606 ---- #ifdef FEAT_EVAL // Set flag that any command was executed, used by ex_vim9script(). + // Not if this was a command that wasn't executed or :endif. if (getline_equal(ea.getline, ea.cookie, getsourceline) ! && current_sctx.sc_sid > 0 ! && ea.cmdidx != CMD_endif ! && (cstack->cs_idx < 0 ! || (cstack->cs_flags[cstack->cs_idx] & CSF_ACTIVE))) SCRIPT_ITEM(current_sctx.sc_sid)->sn_state = SN_STATE_HAD_COMMAND; /* *** ../vim-8.2.2528/src/testdir/test_vim9_script.vim 2021-02-15 21:30:26.750092486 +0100 --- src/testdir/test_vim9_script.vim 2021-02-17 21:46:59.808029526 +0100 *************** *** 1262,1267 **** --- 1262,1288 ---- nunmap enddef + def Test_vim9script_mix() + var lines =<< trim END + if has(g:feature) + " legacy script + let g:legacy = 1 + finish + endif + vim9script + g:legacy = 0 + END + g:feature = 'eval' + g:legacy = -1 + CheckScriptSuccess(lines) + assert_equal(1, g:legacy) + + g:feature = 'noteval' + g:legacy = -1 + CheckScriptSuccess(lines) + assert_equal(0, g:legacy) + enddef + def Test_vim9script_fails() CheckScriptFailure(['scriptversion 2', 'vim9script'], 'E1039:') CheckScriptFailure(['vim9script', 'scriptversion 2'], 'E1040:') *** ../vim-8.2.2528/src/version.c 2021-02-17 18:49:07.968110009 +0100 --- src/version.c 2021-02-17 21:35:59.889100769 +0100 *************** *** 752,753 **** --- 752,755 ---- { /* Add new patch number below this line */ + /**/ + 2529, /**/ -- It's not hard to meet expenses, they're everywhere. /// 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 ///