@@ -145,6 +145,17 @@ Do not start a comment with #{, it looks like the legacy dictionary literal
145145and produces an error where this might be confusing. #{{ or #{{{ are OK,
146146these can be used to start a fold.
147147
148+ When starting to read a script file Vim doesn't know it is | Vim9 | script until
149+ the `vim9script ` command is found. Until that point you would need to use
150+ legacy comments: >
151+ " legacy comment
152+ vim9script
153+ # Vim9 comment
154+
155+ That looks ugly, better put `vim9script ` in the very first line: >
156+ vim9script
157+ # Vim9 comment
158+
148159 In legacy Vim script # is also used for the alternate file name. In Vim9
149160script you need to use %% instead. Instead of ## use %%% (stands for all
150161arguments).
@@ -1687,21 +1698,34 @@ be exported. {not implemented yet: class, interface}
16871698Import ~
16881699 *:import* *:imp* *E1094* *E1047* *E1262*
16891700 *E1048* *E1049* *E1053* *E1071* *E1236*
1690- The exported items can be imported in another Vim9 script: >
1701+ The exported items can be imported in another script. The import syntax has
1702+ two forms. The simple form: >
1703+ import {filename}
1704+ <
1705+ Where {filename} is an expression that must evaluate to a string. In this
1706+ form the filename should end in ".vim" and the portion before ".vim" will
1707+ become the script local name of the namespace. For example: >
16911708 import "myscript.vim"
1692-
1693- This makes each item available as "myscript.item".
1709+ <
1710+ This makes each exported item in "myscript.vim" available as "myscript.item".
16941711 *:import-as* *E1257* *E1261*
1695- In case the name is long or ambiguous, another name can be specified: >
1696- import "thatscript.vim" as that
1712+ In case the name is long or ambiguous, this form can be used to specify
1713+ another name: >
1714+ import {longfilename} as {name}
1715+ <
1716+ In this form {name} becomes a specific script local name for the imported
1717+ namespace. Therefore {name} must consist of letters, digits and '_', like
1718+ | internal-variables | . The {longfilename} expression must evaluate to any
1719+ filename. For example: >
1720+ import "thatscript.vim.v2" as that
16971721< *E1060* *E1258* *E1259* *E1260*
1698- Then you can use "that.EXPORTED_CONST ", "that.someValue", etc. You are free
1699- to choose the name "that". Use something that will be recognized as referring
1700- to the imported script. Avoid command names, command modifiers and builtin
1701- function names, because the name will shadow them.
1702- If the name starts with a capital letter it can also shadow global user
1703- commands and functions. Also, you cannot use the name for something else in
1704- the script, such as a function or variable name.
1722+ Then you can use "that.item ", etc. You are free to choose the name "that".
1723+ Use something that will be recognized as referring to the imported script.
1724+ Avoid command names, command modifiers and builtin function names, because the
1725+ name will shadow them. Better not start the name starts with a capital
1726+ letter, since it can then also shadow global user commands and functions.
1727+ Also, you cannot use the name for something else in the script, such as a
1728+ function or variable name.
17051729
17061730In case the dot in the name is undesired, a local reference can be made for a
17071731function: >
@@ -1714,15 +1738,6 @@ This does not work for variables, since the value would be copied once and
17141738when changing the variable the copy will change, not the original variable.
17151739You will need to use the full name, with the dot.
17161740
1717- The full syntax of the command is:
1718- import {filename} [as {name} ]
1719- Where {filename} is an expression that must evaluate to a string. Without the
1720- "as {name} " part it must end in ".vim". {name} must consist of letters,
1721- digits and '_', like | internal-variables | .
1722-
1723- `:import ` can also be used in legacy Vim script. The imported items still
1724- become script-local, even when the "s:" prefix is not given.
1725-
17261741`:import ` can not be used in a function. Imported items are intended to exist
17271742at the script level and only imported once.
17281743
@@ -1752,14 +1767,38 @@ line, there can be no line break: >
17521767 echo that
17531768 .name # Error!
17541769
1755- To refer to a function in an imported script in a mapping, | <SID> | can be
1756- used : >
1770+ When you've imported a function from one script into a vim9 script you can
1771+ refer to the imported function in a mapping by prefixing it with | <SID> | : >
17571772 noremap <silent> ,a :call <SID>name.Function()<CR>
17581773
17591774 When the mapping is defined "<SID> name." will be replaced with <SNR> and the
17601775script ID of the imported script.
17611776An even simpler solution is using | <ScriptCmd> | : >
17621777 noremap ,a <ScriptCmd>name.Function()<CR>
1778+
1779+ Note that this does not work for variables, only for functions.
1780+
1781+ *import-legacy* *legacy-import*
1782+ `:import ` can also be used in legacy Vim script. The imported namespace still
1783+ becomes script-local, even when the "s:" prefix is not given. For example: >
1784+ import "myfile.vim"
1785+ call s:myfile.MyFunc()
1786+
1787+ And using the "as name" form: >
1788+ import "otherfile.vim9script" as that
1789+ call s:that.OtherFunc()
1790+
1791+ However, the namespace cannot be resolved on it's own: >
1792+ import "that.vim"
1793+ echo s:that
1794+ " ERROR: E1060: Expected dot after name: s:that
1795+ <
1796+ This also affects the use of | <SID> | in the legacy mapping context. Since
1797+ | <SID> | is only a valid prefix for a function and NOT for a namespace, you
1798+ cannot use it
1799+ to scope a function in a script local namespace. Instead of prefixing the
1800+ function with | <SID> | you should use| <ScriptCmd> | . For example: >
1801+ noremap ,a <ScriptCmd>:call s:that.OtherFunc()<CR>
17631802<
17641803 *:import-cycle*
17651804The `import ` commands are executed when encountered. If script A imports
0 commit comments