annotate runtime/doc/usr_52.txt @ 28956:3e6e6b4e74eb v8.2.5000

patch 8.2.5000: no patch for documentation updates Commit: https://github.com/vim/vim/commit/835ee980eedd1aa0fa2d731312ce38697a12a897 Author: Bram Moolenaar <Bram@vim.org> Date: Sun May 22 14:50:16 2022 +0100 patch 8.2.5000: no patch for documentation updates Problem: No patch for documentation updates. Solution: Update documentation files.
author Bram Moolenaar <Bram@vim.org>
date Sun, 22 May 2022 16:00:02 +0200
parents 57c9377b9c62
children f8e9d5023bf6
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
28956
3e6e6b4e74eb patch 8.2.5000: no patch for documentation updates
Bram Moolenaar <Bram@vim.org>
parents: 28933
diff changeset
1 *usr_52.txt* For Vim version 8.2. Last change: 2022 May 21
20856
83cfa1ef1bf2 Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2
83cfa1ef1bf2 Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
3 VIM USER MANUAL - by Bram Moolenaar
83cfa1ef1bf2 Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
4
83cfa1ef1bf2 Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
5 Write plugins using Vim9 script
83cfa1ef1bf2 Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
6
83cfa1ef1bf2 Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
7
83cfa1ef1bf2 Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
8 The Vim9 script language is used for writing plugins, especially larger ones
83cfa1ef1bf2 Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
9 that use multiple files. This chapter explains how to split up a plugin into
83cfa1ef1bf2 Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
10 modules, import and export items and keep the rest local.
83cfa1ef1bf2 Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
11
28862
82244cfc4694 Update runtime files, new color schemes
Bram Moolenaar <Bram@vim.org>
parents: 21676
diff changeset
12 |52.1| Introduction
82244cfc4694 Update runtime files, new color schemes
Bram Moolenaar <Bram@vim.org>
parents: 21676
diff changeset
13 |52.2| Variable declarations
82244cfc4694 Update runtime files, new color schemes
Bram Moolenaar <Bram@vim.org>
parents: 21676
diff changeset
14 |52.3| Functions and types
82244cfc4694 Update runtime files, new color schemes
Bram Moolenaar <Bram@vim.org>
parents: 21676
diff changeset
15 |52.4| Using a Vim9 script from legacy script
20856
83cfa1ef1bf2 Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
16
83cfa1ef1bf2 Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
17 Next chapter: |usr_90.txt| Installing Vim
28862
82244cfc4694 Update runtime files, new color schemes
Bram Moolenaar <Bram@vim.org>
parents: 21676
diff changeset
18 Previous chapter: |usr_51.txt| Create a plugin
20856
83cfa1ef1bf2 Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
19 Table of contents: |usr_toc.txt|
83cfa1ef1bf2 Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
20
83cfa1ef1bf2 Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
21 ==============================================================================
28862
82244cfc4694 Update runtime files, new color schemes
Bram Moolenaar <Bram@vim.org>
parents: 21676
diff changeset
22 *52.1* Introduction *vim9-script-intro*
20856
83cfa1ef1bf2 Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
23
83cfa1ef1bf2 Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
24 Vim9 script was designed to make it easier to write large Vim scripts. It
83cfa1ef1bf2 Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
25 looks more like other script languages, especially Typescript. Also,
83cfa1ef1bf2 Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
26 functions are compiled into instructions that can be executed quickly. This
83cfa1ef1bf2 Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
27 makes Vim9 script a lot faster, up to a 100 times.
83cfa1ef1bf2 Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
28
83cfa1ef1bf2 Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
29 The basic idea is that a script file has items that are private, only used
83cfa1ef1bf2 Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
30 inside the script file, and items that are exported, used outside of the
83cfa1ef1bf2 Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
31 script file. The exported items can then be used by scripts that import them.
83cfa1ef1bf2 Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
32 That makes very clear what is defined where.
83cfa1ef1bf2 Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
33
83cfa1ef1bf2 Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
34 Let's start with an example, a script that exports one function and has one
83cfa1ef1bf2 Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
35 private function: >
83cfa1ef1bf2 Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
36
21676
1b345fb68ae3 Update runtime files.
Bram Moolenaar <Bram@vim.org>
parents: 20965
diff changeset
37 vim9script " This indicates a Vim9 script file.
20856
83cfa1ef1bf2 Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
38
83cfa1ef1bf2 Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
39 export def GetMessage(): string
83cfa1ef1bf2 Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
40 let result = ''
83cfa1ef1bf2 Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
41 ...
83cfa1ef1bf2 Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
42 result = GetPart(count)
83cfa1ef1bf2 Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
43 ...
83cfa1ef1bf2 Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
44 return result
83cfa1ef1bf2 Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
45 enddef
83cfa1ef1bf2 Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
46
83cfa1ef1bf2 Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
47 def GetPart(nr: number): string
83cfa1ef1bf2 Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
48 if nr == 4
83cfa1ef1bf2 Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
49 return 'yes'
83cfa1ef1bf2 Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
50 else
83cfa1ef1bf2 Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
51 return 'no'
83cfa1ef1bf2 Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
52 endif
83cfa1ef1bf2 Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
53 enddef
83cfa1ef1bf2 Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
54
83cfa1ef1bf2 Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
55 The `vim9script` command must be the very first command in the file. Without
83cfa1ef1bf2 Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
56 it Vim will assume legacy script syntax.
83cfa1ef1bf2 Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
57
83cfa1ef1bf2 Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
58 The `export def GetMessage(): string` line starts with `export`, meaning that
83cfa1ef1bf2 Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
59 this function can be imported and called by other scripts. The line
83cfa1ef1bf2 Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
60 `def GetPart(...` does not start with `export`, this is a script-local
83cfa1ef1bf2 Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
61 function, it can only be used inside this script file.
83cfa1ef1bf2 Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
62
83cfa1ef1bf2 Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
63 In the `export def GetMessage(): string` line you will notice the colon and
83cfa1ef1bf2 Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
64 the return type. Vim9 functions, defined with `def`, require specifying the
83cfa1ef1bf2 Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
65 type of arguments and the return type. That way Vim can compile the code
83cfa1ef1bf2 Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
66 efficiently. The GetPart function defines an argument "nr" of type "number".
83cfa1ef1bf2 Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
67
83cfa1ef1bf2 Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
68 Notice that the assignment `result = GetPart(count)` does not use the `let`
83cfa1ef1bf2 Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
69 command. That is explained in the next section.
83cfa1ef1bf2 Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
70
83cfa1ef1bf2 Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
71 ==============================================================================
28862
82244cfc4694 Update runtime files, new color schemes
Bram Moolenaar <Bram@vim.org>
parents: 21676
diff changeset
72 *52.2* Variable declarations *vim9-declarations*
20856
83cfa1ef1bf2 Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
73
83cfa1ef1bf2 Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
74 In Vim9 script variables are declared once with a `:let` or `:const` command.
83cfa1ef1bf2 Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
75 Assigning a value is done without `:let` and it is not possible to `:unlet`
83cfa1ef1bf2 Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
76 the variable.
83cfa1ef1bf2 Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
77
83cfa1ef1bf2 Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
78 In most cases you will want to declare the variable and initialize it at the
83cfa1ef1bf2 Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
79 same time: >
83cfa1ef1bf2 Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
80 let myText = 'some text'
83cfa1ef1bf2 Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
81 ...
83cfa1ef1bf2 Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
82 myText = 'other text'
83cfa1ef1bf2 Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
83
83cfa1ef1bf2 Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
84 The type of the variable will be inferred from the expression. In this case
83cfa1ef1bf2 Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
85 it is a string. If you initialize with a number, then the type is number: >
83cfa1ef1bf2 Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
86 let myNumber = 1234
83cfa1ef1bf2 Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
87 ...
83cfa1ef1bf2 Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
88 myNumber = 0
83cfa1ef1bf2 Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
89
83cfa1ef1bf2 Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
90 If you try to assign a string to this variable, you will get an error: >
83cfa1ef1bf2 Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
91 let myNumber = 'this fails!'
83cfa1ef1bf2 Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
92
83cfa1ef1bf2 Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
93 In the rare case you want a variable that can take values of any type, you
83cfa1ef1bf2 Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
94 have to specify the type: >
83cfa1ef1bf2 Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
95 let myVar: any = 1234
83cfa1ef1bf2 Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
96 myVar = 'text also works'
83cfa1ef1bf2 Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
97
83cfa1ef1bf2 Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
98 You can also declare a variable without assigning a value. In that case Vim
83cfa1ef1bf2 Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
99 will initialize it to zero or empty: >
83cfa1ef1bf2 Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
100 let word: string
83cfa1ef1bf2 Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
101 if condition
83cfa1ef1bf2 Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
102 word = 'yes'
83cfa1ef1bf2 Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
103 else
83cfa1ef1bf2 Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
104 word = 'no'
83cfa1ef1bf2 Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
105 endif
83cfa1ef1bf2 Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
106
83cfa1ef1bf2 Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
107 Although it's shorter to do: >
83cfa1ef1bf2 Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
108 let word = condition ? 'yes' : 'no'
83cfa1ef1bf2 Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
109
83cfa1ef1bf2 Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
110 ==============================================================================
28862
82244cfc4694 Update runtime files, new color schemes
Bram Moolenaar <Bram@vim.org>
parents: 21676
diff changeset
111 *52.3* Functions and types
20856
83cfa1ef1bf2 Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
112
28933
57c9377b9c62 Update runtime files.
Bram Moolenaar <Bram@vim.org>
parents: 28862
diff changeset
113 Legacy Vim script only checks types at runtime, when the code is executed.
57c9377b9c62 Update runtime files.
Bram Moolenaar <Bram@vim.org>
parents: 28862
diff changeset
114 And it's permissive, often a computation gives an unexpected value instead of
57c9377b9c62 Update runtime files.
Bram Moolenaar <Bram@vim.org>
parents: 28862
diff changeset
115 reporting an error. Thus you can define a function and think it's fine, but
28956
3e6e6b4e74eb patch 8.2.5000: no patch for documentation updates
Bram Moolenaar <Bram@vim.org>
parents: 28933
diff changeset
116 notice a problem only later when the function is called: >
3e6e6b4e74eb patch 8.2.5000: no patch for documentation updates
Bram Moolenaar <Bram@vim.org>
parents: 28933
diff changeset
117 func Concatenate(base, add)
3e6e6b4e74eb patch 8.2.5000: no patch for documentation updates
Bram Moolenaar <Bram@vim.org>
parents: 28933
diff changeset
118 return a:base + a:add
20856
83cfa1ef1bf2 Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
119 endfunc
83cfa1ef1bf2 Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
120
83cfa1ef1bf2 Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
121 Can you spot the error? Try this: >
28956
3e6e6b4e74eb patch 8.2.5000: no patch for documentation updates
Bram Moolenaar <Bram@vim.org>
parents: 28933
diff changeset
122 echo Concatenate('base', 'text')
3e6e6b4e74eb patch 8.2.5000: no patch for documentation updates
Bram Moolenaar <Bram@vim.org>
parents: 28933
diff changeset
123 And you'll see zero. Why? Because in legacy Vim script "+" will convert the
3e6e6b4e74eb patch 8.2.5000: no patch for documentation updates
Bram Moolenaar <Bram@vim.org>
parents: 28933
diff changeset
124 arguments to numbers, and any string without a number results in zero! That's
3e6e6b4e74eb patch 8.2.5000: no patch for documentation updates
Bram Moolenaar <Bram@vim.org>
parents: 28933
diff changeset
125 not what you expected.
20856
83cfa1ef1bf2 Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
126
28956
3e6e6b4e74eb patch 8.2.5000: no patch for documentation updates
Bram Moolenaar <Bram@vim.org>
parents: 28933
diff changeset
127 With `:def` the type checking happens when compiling the function. You need
3e6e6b4e74eb patch 8.2.5000: no patch for documentation updates
Bram Moolenaar <Bram@vim.org>
parents: 28933
diff changeset
128 to specify the argument types and the return type to make that possible. Also
3e6e6b4e74eb patch 8.2.5000: no patch for documentation updates
Bram Moolenaar <Bram@vim.org>
parents: 28933
diff changeset
129 notice that the argument names are used without the "a:" prefix: >
3e6e6b4e74eb patch 8.2.5000: no patch for documentation updates
Bram Moolenaar <Bram@vim.org>
parents: 28933
diff changeset
130 def Concatenate(base: string, add: string): string
3e6e6b4e74eb patch 8.2.5000: no patch for documentation updates
Bram Moolenaar <Bram@vim.org>
parents: 28933
diff changeset
131 return base + add
20856
83cfa1ef1bf2 Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
132 enddef
28956
3e6e6b4e74eb patch 8.2.5000: no patch for documentation updates
Bram Moolenaar <Bram@vim.org>
parents: 28933
diff changeset
133 defcompile Concatenate
20856
83cfa1ef1bf2 Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
134
28956
3e6e6b4e74eb patch 8.2.5000: no patch for documentation updates
Bram Moolenaar <Bram@vim.org>
parents: 28933
diff changeset
135 Here we use `:defcompile` to do the compilation right away, without it the
3e6e6b4e74eb patch 8.2.5000: no patch for documentation updates
Bram Moolenaar <Bram@vim.org>
parents: 28933
diff changeset
136 compilation would happen when the function is first called. Vim will tell you
3e6e6b4e74eb patch 8.2.5000: no patch for documentation updates
Bram Moolenaar <Bram@vim.org>
parents: 28933
diff changeset
137 what you did wrong: >
28933
57c9377b9c62 Update runtime files.
Bram Moolenaar <Bram@vim.org>
parents: 28862
diff changeset
138 E1051: Wrong argument type for +
57c9377b9c62 Update runtime files.
Bram Moolenaar <Bram@vim.org>
parents: 28862
diff changeset
139
28956
3e6e6b4e74eb patch 8.2.5000: no patch for documentation updates
Bram Moolenaar <Bram@vim.org>
parents: 28933
diff changeset
140 Side note: here the context is legacy script. When using Vim9 script you
3e6e6b4e74eb patch 8.2.5000: no patch for documentation updates
Bram Moolenaar <Bram@vim.org>
parents: 28933
diff changeset
141 would put `:defcompile` at the end of the script to check for errors in all
3e6e6b4e74eb patch 8.2.5000: no patch for documentation updates
Bram Moolenaar <Bram@vim.org>
parents: 28933
diff changeset
142 the functions defined in it.
20856
83cfa1ef1bf2 Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
143
83cfa1ef1bf2 Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
144 Vim9 script is strict, it uses the "+" operator only for numbers and floats.
83cfa1ef1bf2 Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
145 For string concatenation ".." must be used. This avoids mistakes and avoids
20965
59f93c2d2551 Update runtime files
Bram Moolenaar <Bram@vim.org>
parents: 20856
diff changeset
146 the automatic conversion that gave a surprising result above. So you change
20856
83cfa1ef1bf2 Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
147 the first line of the function to: >
83cfa1ef1bf2 Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
148 s:collected ..= add
83cfa1ef1bf2 Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
149 And now it works.
83cfa1ef1bf2 Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
150
83cfa1ef1bf2 Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
151 If the function does not return anything, just leave out the return type: >
83cfa1ef1bf2 Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
152 def ReportResult(result: string)
83cfa1ef1bf2 Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
153 echo 'The result is: ' .. result
83cfa1ef1bf2 Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
154 enddef
83cfa1ef1bf2 Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
155
83cfa1ef1bf2 Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
156 This is also checked, if you try to return a value you'll get an error.
83cfa1ef1bf2 Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
157
83cfa1ef1bf2 Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
158 In case you don't care about types or have a function that does work with
83cfa1ef1bf2 Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
159 multiple types, you can use the "any" type: >
83cfa1ef1bf2 Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
160 def Store(key: string, value: any)
83cfa1ef1bf2 Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
161 resultDict[key] = value
83cfa1ef1bf2 Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
162 enddef
83cfa1ef1bf2 Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
163
83cfa1ef1bf2 Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
164 ==============================================================================
28862
82244cfc4694 Update runtime files, new color schemes
Bram Moolenaar <Bram@vim.org>
parents: 21676
diff changeset
165 *52.4* Using a Vim9 script from legacy script *source-vim9-script*
20856
83cfa1ef1bf2 Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
166
83cfa1ef1bf2 Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
167 In some cases you have a legacy Vim script where you want to use items from a
83cfa1ef1bf2 Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
168 Vim9 script. For example in your .vimrc you want to initialize a plugin. The
83cfa1ef1bf2 Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
169 best way to do this is to use `:import`. For example: >
83cfa1ef1bf2 Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
170
83cfa1ef1bf2 Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
171 import Init as NiceInit from 'myNicePlugin.vim'
83cfa1ef1bf2 Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
172 call NiceInit('today')
83cfa1ef1bf2 Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
173
83cfa1ef1bf2 Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
174 This finds the exported function "Init" in the Vim9 script file and makes it
83cfa1ef1bf2 Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
175 available as script-local item "NiceInit". `:import` always uses the script
83cfa1ef1bf2 Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
176 namespace, even when "s:" is not given. If "myNicePlugin.vim" was already
83cfa1ef1bf2 Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
177 sourced it is not sourced again.
83cfa1ef1bf2 Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
178
83cfa1ef1bf2 Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
179 Besides avoiding putting any items in the global namespace (where name clashes
83cfa1ef1bf2 Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
180 can cause unexpected errors), this also means the script is sourced only once,
83cfa1ef1bf2 Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
181 no matter how many times items from it are imported.
83cfa1ef1bf2 Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
182
83cfa1ef1bf2 Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
183 In some cases, e.g. for testing, you may just want to source the Vim9 script.
83cfa1ef1bf2 Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
184 That is OK, but then only global items will be available. The Vim9 script
83cfa1ef1bf2 Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
185 will have to make sure to use a unique name for these global items. Example: >
83cfa1ef1bf2 Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
186 source ~/.vim/extra/myNicePlugin.vim
83cfa1ef1bf2 Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
187 call g:NicePluginTest()
83cfa1ef1bf2 Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
188
83cfa1ef1bf2 Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
189 ==============================================================================
83cfa1ef1bf2 Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
190
83cfa1ef1bf2 Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
191 Next chapter: |usr_90.txt| Installing Vim
83cfa1ef1bf2 Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
192
83cfa1ef1bf2 Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
193 Copyright: see |manual-copyright| vim:tw=78:ts=8:noet:ft=help:norl: