Skip to content

Commit d3f8a9e

Browse files
committed
patch 8.2.2529: Vim9: Not possible to use legacy and Vim9 script in one file
Problem: Vim9: Not possible to use legacy and Vim9 script in one file. Solution: Vim9: allow for "if false" before :vim9script. (closes #7851)
1 parent c7dac85 commit d3f8a9e

4 files changed

Lines changed: 52 additions & 4 deletions

File tree

runtime/doc/vim9.txt

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1051,9 +1051,9 @@ that you don't do that.
10511051
Namespace ~
10521052
*vim9-namespace*
10531053
To recognize a file that can be imported the `vim9script` statement must
1054-
appear as the first statement in the file. It tells Vim to interpret the
1055-
script in its own namespace, instead of the global namespace. If a file
1056-
starts with: >
1054+
appear as the first statement in the file (see |vim9-mix| for an exception).
1055+
It tells Vim to interpret the script in its own namespace, instead of the
1056+
global namespace. If a file starts with: >
10571057
vim9script
10581058
var myvar = 'yes'
10591059
Then "myvar" will only exist in this file. While without `vim9script` it would
@@ -1073,6 +1073,27 @@ Vim default value, like with: >
10731073
One of the effects is that |line-continuation| is always enabled.
10741074
The original value of 'cpoptions' is restored at the end of the script.
10751075

1076+
*vim9-mix*
1077+
There is one way to use both legacy and Vim9 syntax in one script file: >
1078+
" comments may go here
1079+
if !has('vim9script')
1080+
" legacy script commands go here
1081+
finish
1082+
endif
1083+
vim9script
1084+
# Vim9 script commands go here
1085+
This allows for writing a script that takes advantage of the Vim9 script
1086+
syntax if possible, but will also work on an Vim version without it.
1087+
1088+
This can only work in two ways:
1089+
1. The "if" statement evaluates to false, the commands up to `endif` are
1090+
skipped and `vim9script` is then the first command actually executed.
1091+
2. The "if" statement evaluates to true, the commands up to `endif` are
1092+
executed and `finish` bails out before reaching `vim9script`.
1093+
1094+
TODO: The "vim9script" feature does not exist yet, it will only be added once
1095+
the Vim9 script syntax has been fully implemented.
1096+
10761097

10771098
Export ~
10781099
*:export* *:exp*

src/ex_docmd.c

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2595,8 +2595,12 @@ do_one_cmd(
25952595

25962596
#ifdef FEAT_EVAL
25972597
// Set flag that any command was executed, used by ex_vim9script().
2598+
// Not if this was a command that wasn't executed or :endif.
25982599
if (getline_equal(ea.getline, ea.cookie, getsourceline)
2599-
&& current_sctx.sc_sid > 0)
2600+
&& current_sctx.sc_sid > 0
2601+
&& ea.cmdidx != CMD_endif
2602+
&& (cstack->cs_idx < 0
2603+
|| (cstack->cs_flags[cstack->cs_idx] & CSF_ACTIVE)))
26002604
SCRIPT_ITEM(current_sctx.sc_sid)->sn_state = SN_STATE_HAD_COMMAND;
26012605

26022606
/*

src/testdir/test_vim9_script.vim

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1262,6 +1262,27 @@ def Test_use_import_in_mapping()
12621262
nunmap <F3>
12631263
enddef
12641264

1265+
def Test_vim9script_mix()
1266+
var lines =<< trim END
1267+
if has(g:feature)
1268+
" legacy script
1269+
let g:legacy = 1
1270+
finish
1271+
endif
1272+
vim9script
1273+
g:legacy = 0
1274+
END
1275+
g:feature = 'eval'
1276+
g:legacy = -1
1277+
CheckScriptSuccess(lines)
1278+
assert_equal(1, g:legacy)
1279+
1280+
g:feature = 'noteval'
1281+
g:legacy = -1
1282+
CheckScriptSuccess(lines)
1283+
assert_equal(0, g:legacy)
1284+
enddef
1285+
12651286
def Test_vim9script_fails()
12661287
CheckScriptFailure(['scriptversion 2', 'vim9script'], 'E1039:')
12671288
CheckScriptFailure(['vim9script', 'scriptversion 2'], 'E1040:')

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+
2529,
753755
/**/
754756
2528,
755757
/**/

0 commit comments

Comments
 (0)