annotate runtime/doc/usr_52.txt @ 29193:1e9e9d89f0ee

Update runtime files Commit: https://github.com/vim/vim/commit/d592deb336523a5448779ee3d4bba80334cff1f7 Author: Bram Moolenaar <Bram@vim.org> Date: Fri Jun 17 15:42:40 2022 +0100 Update runtime files
author Bram Moolenaar <Bram@vim.org>
date Fri, 17 Jun 2022 16:45:04 +0200
parents f3ec3c57e070
children f8116058ca76
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
29087
f3ec3c57e070 Update runtime files.
Bram Moolenaar <Bram@vim.org>
parents: 29066
diff changeset
1 *usr_52.txt* For Vim version 8.2. Last change: 2022 Jun 04
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
29066
f8e9d5023bf6 Update runtime files
Bram Moolenaar <Bram@vim.org>
parents: 28956
diff changeset
5 Write larger plugins
20856
83cfa1ef1bf2 Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
6
29066
f8e9d5023bf6 Update runtime files
Bram Moolenaar <Bram@vim.org>
parents: 28956
diff changeset
7 When plugins do more than simple things, they tend to grow big. This file
f8e9d5023bf6 Update runtime files
Bram Moolenaar <Bram@vim.org>
parents: 28956
diff changeset
8 explains how to make sure they still load fast and how to split them up in
29087
f3ec3c57e070 Update runtime files.
Bram Moolenaar <Bram@vim.org>
parents: 29066
diff changeset
9 smaller parts.
20856
83cfa1ef1bf2 Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
10
29066
f8e9d5023bf6 Update runtime files
Bram Moolenaar <Bram@vim.org>
parents: 28956
diff changeset
11 |52.1| Export and import
f8e9d5023bf6 Update runtime files
Bram Moolenaar <Bram@vim.org>
parents: 28956
diff changeset
12 |52.2| Autoloading
f8e9d5023bf6 Update runtime files
Bram Moolenaar <Bram@vim.org>
parents: 28956
diff changeset
13 |52.3| Autoloading without import/export
f8e9d5023bf6 Update runtime files
Bram Moolenaar <Bram@vim.org>
parents: 28956
diff changeset
14 |52.4| Other mechanisms to use
f8e9d5023bf6 Update runtime files
Bram Moolenaar <Bram@vim.org>
parents: 28956
diff changeset
15 |52.5| 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 ==============================================================================
29066
f8e9d5023bf6 Update runtime files
Bram Moolenaar <Bram@vim.org>
parents: 28956
diff changeset
22 *52.1* Export and import
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
29087
f3ec3c57e070 Update runtime files.
Bram Moolenaar <Bram@vim.org>
parents: 29066
diff changeset
30 inside the script file, and items that are exported, which can be used by
f3ec3c57e070 Update runtime files.
Bram Moolenaar <Bram@vim.org>
parents: 29066
diff changeset
31 scripts that import them. That makes very clear what is defined where.
20856
83cfa1ef1bf2 Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
32
83cfa1ef1bf2 Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
33 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
34 private function: >
83cfa1ef1bf2 Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
35
29087
f3ec3c57e070 Update runtime files.
Bram Moolenaar <Bram@vim.org>
parents: 29066
diff changeset
36 vim9script
20856
83cfa1ef1bf2 Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
37
29087
f3ec3c57e070 Update runtime files.
Bram Moolenaar <Bram@vim.org>
parents: 29066
diff changeset
38 export def GetMessage(count: string): string
f3ec3c57e070 Update runtime files.
Bram Moolenaar <Bram@vim.org>
parents: 29066
diff changeset
39 var nr = str2nr(count)
f3ec3c57e070 Update runtime files.
Bram Moolenaar <Bram@vim.org>
parents: 29066
diff changeset
40 var result = $'To {nr} we say '
f3ec3c57e070 Update runtime files.
Bram Moolenaar <Bram@vim.org>
parents: 29066
diff changeset
41 result ..= GetReply(nr)
20856
83cfa1ef1bf2 Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
42 return result
83cfa1ef1bf2 Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
43 enddef
83cfa1ef1bf2 Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
44
29087
f3ec3c57e070 Update runtime files.
Bram Moolenaar <Bram@vim.org>
parents: 29066
diff changeset
45 def GetReply(nr: number): string
f3ec3c57e070 Update runtime files.
Bram Moolenaar <Bram@vim.org>
parents: 29066
diff changeset
46 if nr == 42
20856
83cfa1ef1bf2 Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
47 return 'yes'
29087
f3ec3c57e070 Update runtime files.
Bram Moolenaar <Bram@vim.org>
parents: 29066
diff changeset
48 elseif nr = 22
f3ec3c57e070 Update runtime files.
Bram Moolenaar <Bram@vim.org>
parents: 29066
diff changeset
49 return 'maybe'
20856
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
29087
f3ec3c57e070 Update runtime files.
Bram Moolenaar <Bram@vim.org>
parents: 29066
diff changeset
55 The `vim9script` command is required, `export` only works in a |Vim9| script.
f3ec3c57e070 Update runtime files.
Bram Moolenaar <Bram@vim.org>
parents: 29066
diff changeset
56
f3ec3c57e070 Update runtime files.
Bram Moolenaar <Bram@vim.org>
parents: 29066
diff changeset
57 The `export def GetMessage(...` line starts with `export`, meaning that this
f3ec3c57e070 Update runtime files.
Bram Moolenaar <Bram@vim.org>
parents: 29066
diff changeset
58 function can be called by other scripts. The line `def GetReply(...` does not
f3ec3c57e070 Update runtime files.
Bram Moolenaar <Bram@vim.org>
parents: 29066
diff changeset
59 start with `export`, this is a script-local function, it can only be used
f3ec3c57e070 Update runtime files.
Bram Moolenaar <Bram@vim.org>
parents: 29066
diff changeset
60 inside this script file.
f3ec3c57e070 Update runtime files.
Bram Moolenaar <Bram@vim.org>
parents: 29066
diff changeset
61
f3ec3c57e070 Update runtime files.
Bram Moolenaar <Bram@vim.org>
parents: 29066
diff changeset
62 Now about the script where this is imported. In this example we use this
f3ec3c57e070 Update runtime files.
Bram Moolenaar <Bram@vim.org>
parents: 29066
diff changeset
63 layout, which works well for a plugin below the "pack" directory:
f3ec3c57e070 Update runtime files.
Bram Moolenaar <Bram@vim.org>
parents: 29066
diff changeset
64 .../plugin/theplugin.vim
f3ec3c57e070 Update runtime files.
Bram Moolenaar <Bram@vim.org>
parents: 29066
diff changeset
65 .../lib/getmessage.vim
f3ec3c57e070 Update runtime files.
Bram Moolenaar <Bram@vim.org>
parents: 29066
diff changeset
66
f3ec3c57e070 Update runtime files.
Bram Moolenaar <Bram@vim.org>
parents: 29066
diff changeset
67 Assuming the "..." directory has been added to 'runtimepath', Vim will look
f3ec3c57e070 Update runtime files.
Bram Moolenaar <Bram@vim.org>
parents: 29066
diff changeset
68 for plugins in the "plugin" directory and source "theplugin.vim". Vim does
f3ec3c57e070 Update runtime files.
Bram Moolenaar <Bram@vim.org>
parents: 29066
diff changeset
69 not recognize the "lib" directory, you can put any scripts there.
f3ec3c57e070 Update runtime files.
Bram Moolenaar <Bram@vim.org>
parents: 29066
diff changeset
70
f3ec3c57e070 Update runtime files.
Bram Moolenaar <Bram@vim.org>
parents: 29066
diff changeset
71 The above script that exports GetMessage() goes in lib/getmessage.vim. The
f3ec3c57e070 Update runtime files.
Bram Moolenaar <Bram@vim.org>
parents: 29066
diff changeset
72 GetMessage() function is used in plugin/theplugin.vim: >
f3ec3c57e070 Update runtime files.
Bram Moolenaar <Bram@vim.org>
parents: 29066
diff changeset
73
f3ec3c57e070 Update runtime files.
Bram Moolenaar <Bram@vim.org>
parents: 29066
diff changeset
74 vim9script
f3ec3c57e070 Update runtime files.
Bram Moolenaar <Bram@vim.org>
parents: 29066
diff changeset
75
f3ec3c57e070 Update runtime files.
Bram Moolenaar <Bram@vim.org>
parents: 29066
diff changeset
76 import "../lib/getmessage.vim"
f3ec3c57e070 Update runtime files.
Bram Moolenaar <Bram@vim.org>
parents: 29066
diff changeset
77 command -nargs=1 ShowMessage echomsg getmessage.GetMessage(<f-args>)
f3ec3c57e070 Update runtime files.
Bram Moolenaar <Bram@vim.org>
parents: 29066
diff changeset
78
f3ec3c57e070 Update runtime files.
Bram Moolenaar <Bram@vim.org>
parents: 29066
diff changeset
79 The `import` command uses a relative path, it starts with "../", which means
f3ec3c57e070 Update runtime files.
Bram Moolenaar <Bram@vim.org>
parents: 29066
diff changeset
80 to go one directory up. For other kinds of paths see the `:import` command.
20856
83cfa1ef1bf2 Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
81
29087
f3ec3c57e070 Update runtime files.
Bram Moolenaar <Bram@vim.org>
parents: 29066
diff changeset
82 How we can try out the command that the plugin provides: >
f3ec3c57e070 Update runtime files.
Bram Moolenaar <Bram@vim.org>
parents: 29066
diff changeset
83 ShowMessage 1
f3ec3c57e070 Update runtime files.
Bram Moolenaar <Bram@vim.org>
parents: 29066
diff changeset
84 < To 1 we say no ~
f3ec3c57e070 Update runtime files.
Bram Moolenaar <Bram@vim.org>
parents: 29066
diff changeset
85 >
f3ec3c57e070 Update runtime files.
Bram Moolenaar <Bram@vim.org>
parents: 29066
diff changeset
86 ShowMessage 22
f3ec3c57e070 Update runtime files.
Bram Moolenaar <Bram@vim.org>
parents: 29066
diff changeset
87 < To 22 we say maybe ~
f3ec3c57e070 Update runtime files.
Bram Moolenaar <Bram@vim.org>
parents: 29066
diff changeset
88
f3ec3c57e070 Update runtime files.
Bram Moolenaar <Bram@vim.org>
parents: 29066
diff changeset
89 Notice that the function GetMessage() is prefixed with the imported script
f3ec3c57e070 Update runtime files.
Bram Moolenaar <Bram@vim.org>
parents: 29066
diff changeset
90 name "getmessage". That way, for every imported function used, you know what
f3ec3c57e070 Update runtime files.
Bram Moolenaar <Bram@vim.org>
parents: 29066
diff changeset
91 script it was imported from. If you import several scripts each of them could
f3ec3c57e070 Update runtime files.
Bram Moolenaar <Bram@vim.org>
parents: 29066
diff changeset
92 define a GetMessage() function: >
f3ec3c57e070 Update runtime files.
Bram Moolenaar <Bram@vim.org>
parents: 29066
diff changeset
93
f3ec3c57e070 Update runtime files.
Bram Moolenaar <Bram@vim.org>
parents: 29066
diff changeset
94 vim9script
f3ec3c57e070 Update runtime files.
Bram Moolenaar <Bram@vim.org>
parents: 29066
diff changeset
95
f3ec3c57e070 Update runtime files.
Bram Moolenaar <Bram@vim.org>
parents: 29066
diff changeset
96 import "../lib/getmessage.vim"
f3ec3c57e070 Update runtime files.
Bram Moolenaar <Bram@vim.org>
parents: 29066
diff changeset
97 import "../lib/getother.vim"
f3ec3c57e070 Update runtime files.
Bram Moolenaar <Bram@vim.org>
parents: 29066
diff changeset
98 command -nargs=1 ShowMessage echomsg getmessage.GetMessage(<f-args>)
f3ec3c57e070 Update runtime files.
Bram Moolenaar <Bram@vim.org>
parents: 29066
diff changeset
99 command -nargs=1 ShowOther echomsg getother.GetMessage(<f-args>)
20856
83cfa1ef1bf2 Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
100
29087
f3ec3c57e070 Update runtime files.
Bram Moolenaar <Bram@vim.org>
parents: 29066
diff changeset
101 If the imported script name is long or you use it in many places, you can
f3ec3c57e070 Update runtime files.
Bram Moolenaar <Bram@vim.org>
parents: 29066
diff changeset
102 shorten it by adding an "as" argument: >
f3ec3c57e070 Update runtime files.
Bram Moolenaar <Bram@vim.org>
parents: 29066
diff changeset
103 import "../lib/getmessage.vim" as msg
f3ec3c57e070 Update runtime files.
Bram Moolenaar <Bram@vim.org>
parents: 29066
diff changeset
104 command -nargs=1 ShowMessage echomsg msg.GetMessage(<f-args>)
f3ec3c57e070 Update runtime files.
Bram Moolenaar <Bram@vim.org>
parents: 29066
diff changeset
105
f3ec3c57e070 Update runtime files.
Bram Moolenaar <Bram@vim.org>
parents: 29066
diff changeset
106
f3ec3c57e070 Update runtime files.
Bram Moolenaar <Bram@vim.org>
parents: 29066
diff changeset
107 RELOADING
20856
83cfa1ef1bf2 Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
108
29087
f3ec3c57e070 Update runtime files.
Bram Moolenaar <Bram@vim.org>
parents: 29066
diff changeset
109 One thing to keep in mind: the imported "lib/getmessage.vim" script will be
f3ec3c57e070 Update runtime files.
Bram Moolenaar <Bram@vim.org>
parents: 29066
diff changeset
110 sourced only once. When it is imported a second time sourcing it will be
f3ec3c57e070 Update runtime files.
Bram Moolenaar <Bram@vim.org>
parents: 29066
diff changeset
111 skipped, since the items in it have already been created. It does not matter
f3ec3c57e070 Update runtime files.
Bram Moolenaar <Bram@vim.org>
parents: 29066
diff changeset
112 if this import command is in another script, or in the same script that is
f3ec3c57e070 Update runtime files.
Bram Moolenaar <Bram@vim.org>
parents: 29066
diff changeset
113 sourced again.
f3ec3c57e070 Update runtime files.
Bram Moolenaar <Bram@vim.org>
parents: 29066
diff changeset
114
f3ec3c57e070 Update runtime files.
Bram Moolenaar <Bram@vim.org>
parents: 29066
diff changeset
115 This is efficient when using a plugin, but when still developing a plugin it
f3ec3c57e070 Update runtime files.
Bram Moolenaar <Bram@vim.org>
parents: 29066
diff changeset
116 means that changing "lib/getmessage.vim" after it has been imported will have
f3ec3c57e070 Update runtime files.
Bram Moolenaar <Bram@vim.org>
parents: 29066
diff changeset
117 no effect. You need to quit Vim and start it again. (Rationale: the items
f3ec3c57e070 Update runtime files.
Bram Moolenaar <Bram@vim.org>
parents: 29066
diff changeset
118 defined in the script could be used in a compiled function, sourcing the
f3ec3c57e070 Update runtime files.
Bram Moolenaar <Bram@vim.org>
parents: 29066
diff changeset
119 script again may break those functions).
f3ec3c57e070 Update runtime files.
Bram Moolenaar <Bram@vim.org>
parents: 29066
diff changeset
120
29066
f8e9d5023bf6 Update runtime files
Bram Moolenaar <Bram@vim.org>
parents: 28956
diff changeset
121
f8e9d5023bf6 Update runtime files
Bram Moolenaar <Bram@vim.org>
parents: 28956
diff changeset
122 USING GLOBALS
f8e9d5023bf6 Update runtime files
Bram Moolenaar <Bram@vim.org>
parents: 28956
diff changeset
123
f8e9d5023bf6 Update runtime files
Bram Moolenaar <Bram@vim.org>
parents: 28956
diff changeset
124 Sometimes you will want to use global variables or functions, so that they can
f8e9d5023bf6 Update runtime files
Bram Moolenaar <Bram@vim.org>
parents: 28956
diff changeset
125 be used anywhere. A good example is a global variable that passes a
f8e9d5023bf6 Update runtime files
Bram Moolenaar <Bram@vim.org>
parents: 28956
diff changeset
126 preference to a plugin. To avoid other scripts using the same name, use a
f8e9d5023bf6 Update runtime files
Bram Moolenaar <Bram@vim.org>
parents: 28956
diff changeset
127 prefix that is very unlikely to be used elsewhere. For example, if you have a
f8e9d5023bf6 Update runtime files
Bram Moolenaar <Bram@vim.org>
parents: 28956
diff changeset
128 "mytags" plugin, you could use: >
f8e9d5023bf6 Update runtime files
Bram Moolenaar <Bram@vim.org>
parents: 28956
diff changeset
129
f8e9d5023bf6 Update runtime files
Bram Moolenaar <Bram@vim.org>
parents: 28956
diff changeset
130 g:mytags_location = '$HOME/project'
f8e9d5023bf6 Update runtime files
Bram Moolenaar <Bram@vim.org>
parents: 28956
diff changeset
131 g:mytags_style = 'fast'
20856
83cfa1ef1bf2 Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
132
83cfa1ef1bf2 Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
133 ==============================================================================
29066
f8e9d5023bf6 Update runtime files
Bram Moolenaar <Bram@vim.org>
parents: 28956
diff changeset
134 *52.2* Autoloading
20856
83cfa1ef1bf2 Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
135
29066
f8e9d5023bf6 Update runtime files
Bram Moolenaar <Bram@vim.org>
parents: 28956
diff changeset
136 After splitting your large script into pieces, all the lines will still be
f8e9d5023bf6 Update runtime files
Bram Moolenaar <Bram@vim.org>
parents: 28956
diff changeset
137 loaded and executed the moment the script is used. Every `import` loads the
f8e9d5023bf6 Update runtime files
Bram Moolenaar <Bram@vim.org>
parents: 28956
diff changeset
138 imported script to find the items defined there. Although that is good for
f8e9d5023bf6 Update runtime files
Bram Moolenaar <Bram@vim.org>
parents: 28956
diff changeset
139 finding errors early, it also takes time. Which is wasted if the
f8e9d5023bf6 Update runtime files
Bram Moolenaar <Bram@vim.org>
parents: 28956
diff changeset
140 functionality is not often used.
20856
83cfa1ef1bf2 Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
141
29066
f8e9d5023bf6 Update runtime files
Bram Moolenaar <Bram@vim.org>
parents: 28956
diff changeset
142 Instead of having `import` load the script immediately, it can be postponed
29087
f3ec3c57e070 Update runtime files.
Bram Moolenaar <Bram@vim.org>
parents: 29066
diff changeset
143 until needed. Using the example above, only one change needs to be made in
f3ec3c57e070 Update runtime files.
Bram Moolenaar <Bram@vim.org>
parents: 29066
diff changeset
144 the plugin/theplugin.vim script: >
f3ec3c57e070 Update runtime files.
Bram Moolenaar <Bram@vim.org>
parents: 29066
diff changeset
145 import autoload "../lib/getmessage.vim"
20856
83cfa1ef1bf2 Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
146
29087
f3ec3c57e070 Update runtime files.
Bram Moolenaar <Bram@vim.org>
parents: 29066
diff changeset
147 Nothing in the rest of the script needs to change. However, the types will
f3ec3c57e070 Update runtime files.
Bram Moolenaar <Bram@vim.org>
parents: 29066
diff changeset
148 not be checked. Not even the existence of the GetMessage() function is
f3ec3c57e070 Update runtime files.
Bram Moolenaar <Bram@vim.org>
parents: 29066
diff changeset
149 checked until it is used. You will have to decide what is more important for
f3ec3c57e070 Update runtime files.
Bram Moolenaar <Bram@vim.org>
parents: 29066
diff changeset
150 your script: fast startup or getting errors early. You can also add the
f3ec3c57e070 Update runtime files.
Bram Moolenaar <Bram@vim.org>
parents: 29066
diff changeset
151 "autoload" argument later, after you have checked everything works.
20856
83cfa1ef1bf2 Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
152
29087
f3ec3c57e070 Update runtime files.
Bram Moolenaar <Bram@vim.org>
parents: 29066
diff changeset
153
f3ec3c57e070 Update runtime files.
Bram Moolenaar <Bram@vim.org>
parents: 29066
diff changeset
154 AUTOLOAD DIRECTORY
f3ec3c57e070 Update runtime files.
Bram Moolenaar <Bram@vim.org>
parents: 29066
diff changeset
155
f3ec3c57e070 Update runtime files.
Bram Moolenaar <Bram@vim.org>
parents: 29066
diff changeset
156 Another form is to use autoload with a script name that is not an absolute or
f3ec3c57e070 Update runtime files.
Bram Moolenaar <Bram@vim.org>
parents: 29066
diff changeset
157 relative path: >
29066
f8e9d5023bf6 Update runtime files
Bram Moolenaar <Bram@vim.org>
parents: 28956
diff changeset
158 import autload "monthlib.vim"
20856
83cfa1ef1bf2 Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
159
29066
f8e9d5023bf6 Update runtime files
Bram Moolenaar <Bram@vim.org>
parents: 28956
diff changeset
160 This will search for the script "monthlib.vim" in the autoload directories of
29087
f3ec3c57e070 Update runtime files.
Bram Moolenaar <Bram@vim.org>
parents: 29066
diff changeset
161 'runtimepath'. With Unix one of the directories often is "~/.vim/autoload".
29066
f8e9d5023bf6 Update runtime files
Bram Moolenaar <Bram@vim.org>
parents: 28956
diff changeset
162
29087
f3ec3c57e070 Update runtime files.
Bram Moolenaar <Bram@vim.org>
parents: 29066
diff changeset
163 The main advantage of this is that this script can be easily shared with other
29066
f8e9d5023bf6 Update runtime files
Bram Moolenaar <Bram@vim.org>
parents: 28956
diff changeset
164 scripts. You do need to make sure that the script name is unique, since Vim
f8e9d5023bf6 Update runtime files
Bram Moolenaar <Bram@vim.org>
parents: 28956
diff changeset
165 will search all the "autoload" directories in 'runtimepath', and if you are
29087
f3ec3c57e070 Update runtime files.
Bram Moolenaar <Bram@vim.org>
parents: 29066
diff changeset
166 using several plugins with a plugin manager, it may add a directory to
f3ec3c57e070 Update runtime files.
Bram Moolenaar <Bram@vim.org>
parents: 29066
diff changeset
167 'runtimepath', each of which might have an "autoload" directory.
f3ec3c57e070 Update runtime files.
Bram Moolenaar <Bram@vim.org>
parents: 29066
diff changeset
168
f3ec3c57e070 Update runtime files.
Bram Moolenaar <Bram@vim.org>
parents: 29066
diff changeset
169 Without autoload: >
f3ec3c57e070 Update runtime files.
Bram Moolenaar <Bram@vim.org>
parents: 29066
diff changeset
170 import "monthlib.vim"
f3ec3c57e070 Update runtime files.
Bram Moolenaar <Bram@vim.org>
parents: 29066
diff changeset
171
f3ec3c57e070 Update runtime files.
Bram Moolenaar <Bram@vim.org>
parents: 29066
diff changeset
172 Vim will search for the script "monthlib.vim" in the import directories of
f3ec3c57e070 Update runtime files.
Bram Moolenaar <Bram@vim.org>
parents: 29066
diff changeset
173 'runtimepath'. Note that in this case adding or removing "autoload" changes
f3ec3c57e070 Update runtime files.
Bram Moolenaar <Bram@vim.org>
parents: 29066
diff changeset
174 where the script is found. With a relative or absolute path the location does
f3ec3c57e070 Update runtime files.
Bram Moolenaar <Bram@vim.org>
parents: 29066
diff changeset
175 not change.
20856
83cfa1ef1bf2 Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
176
83cfa1ef1bf2 Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
177 ==============================================================================
29066
f8e9d5023bf6 Update runtime files
Bram Moolenaar <Bram@vim.org>
parents: 28956
diff changeset
178 *52.3* Autoloading without import/export
f8e9d5023bf6 Update runtime files
Bram Moolenaar <Bram@vim.org>
parents: 28956
diff changeset
179
f8e9d5023bf6 Update runtime files
Bram Moolenaar <Bram@vim.org>
parents: 28956
diff changeset
180 *write-library-script*
f8e9d5023bf6 Update runtime files
Bram Moolenaar <Bram@vim.org>
parents: 28956
diff changeset
181 A mechanism from before import/export is still useful and some users may find
f8e9d5023bf6 Update runtime files
Bram Moolenaar <Bram@vim.org>
parents: 28956
diff changeset
182 it a bit simpler. The idea is that you call a function with a special name.
f8e9d5023bf6 Update runtime files
Bram Moolenaar <Bram@vim.org>
parents: 28956
diff changeset
183 That function is then in an autoload script. We will call that one script a
f8e9d5023bf6 Update runtime files
Bram Moolenaar <Bram@vim.org>
parents: 28956
diff changeset
184 library script.
20856
83cfa1ef1bf2 Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
185
29193
1e9e9d89f0ee Update runtime files
Bram Moolenaar <Bram@vim.org>
parents: 29087
diff changeset
186 The autoload mechanism is based on a function name that has "#" characters: >
29066
f8e9d5023bf6 Update runtime files
Bram Moolenaar <Bram@vim.org>
parents: 28956
diff changeset
187
f8e9d5023bf6 Update runtime files
Bram Moolenaar <Bram@vim.org>
parents: 28956
diff changeset
188 mylib#myfunction(arg)
20856
83cfa1ef1bf2 Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
189
29066
f8e9d5023bf6 Update runtime files
Bram Moolenaar <Bram@vim.org>
parents: 28956
diff changeset
190 Vim will recognize the function name by the embedded "#" character and when
f8e9d5023bf6 Update runtime files
Bram Moolenaar <Bram@vim.org>
parents: 28956
diff changeset
191 it is not defined yet search for the script "autoload/mylib.vim" in
f8e9d5023bf6 Update runtime files
Bram Moolenaar <Bram@vim.org>
parents: 28956
diff changeset
192 'runtimepath'. That script must define the "mylib#myfunction()" function.
f8e9d5023bf6 Update runtime files
Bram Moolenaar <Bram@vim.org>
parents: 28956
diff changeset
193 Obviously the name "mylib" is the part before the "#" and is used as the name
f8e9d5023bf6 Update runtime files
Bram Moolenaar <Bram@vim.org>
parents: 28956
diff changeset
194 of the script, adding ".vim".
20856
83cfa1ef1bf2 Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
195
29066
f8e9d5023bf6 Update runtime files
Bram Moolenaar <Bram@vim.org>
parents: 28956
diff changeset
196 You can put many other functions in the mylib.vim script, you are free to
f8e9d5023bf6 Update runtime files
Bram Moolenaar <Bram@vim.org>
parents: 28956
diff changeset
197 organize your functions in library scripts. But you must use function names
f8e9d5023bf6 Update runtime files
Bram Moolenaar <Bram@vim.org>
parents: 28956
diff changeset
198 where the part before the '#' matches the script name. Otherwise Vim would
f8e9d5023bf6 Update runtime files
Bram Moolenaar <Bram@vim.org>
parents: 28956
diff changeset
199 not know what script to load. This is where it differs from the import/export
f8e9d5023bf6 Update runtime files
Bram Moolenaar <Bram@vim.org>
parents: 28956
diff changeset
200 mechanism.
20856
83cfa1ef1bf2 Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
201
29066
f8e9d5023bf6 Update runtime files
Bram Moolenaar <Bram@vim.org>
parents: 28956
diff changeset
202 If you get really enthusiastic and write lots of library scripts, you may
f8e9d5023bf6 Update runtime files
Bram Moolenaar <Bram@vim.org>
parents: 28956
diff changeset
203 want to use subdirectories. Example: >
f8e9d5023bf6 Update runtime files
Bram Moolenaar <Bram@vim.org>
parents: 28956
diff changeset
204
f8e9d5023bf6 Update runtime files
Bram Moolenaar <Bram@vim.org>
parents: 28956
diff changeset
205 netlib#ftp#read('somefile')
28933
57c9377b9c62 Update runtime files.
Bram Moolenaar <Bram@vim.org>
parents: 28862
diff changeset
206
29066
f8e9d5023bf6 Update runtime files
Bram Moolenaar <Bram@vim.org>
parents: 28956
diff changeset
207 Here the script name is taken from the function name up to the last "#". The
f8e9d5023bf6 Update runtime files
Bram Moolenaar <Bram@vim.org>
parents: 28956
diff changeset
208 "#" in the middle are replaced by a slash, the last one by ".vim". Thus you
f8e9d5023bf6 Update runtime files
Bram Moolenaar <Bram@vim.org>
parents: 28956
diff changeset
209 get "netlib/ftp.vim". For Unix the library script used for this could be:
20856
83cfa1ef1bf2 Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
210
29066
f8e9d5023bf6 Update runtime files
Bram Moolenaar <Bram@vim.org>
parents: 28956
diff changeset
211 ~/.vim/autoload/netlib/ftp.vim
20856
83cfa1ef1bf2 Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
212
29066
f8e9d5023bf6 Update runtime files
Bram Moolenaar <Bram@vim.org>
parents: 28956
diff changeset
213 Where the function is defined like this: >
f8e9d5023bf6 Update runtime files
Bram Moolenaar <Bram@vim.org>
parents: 28956
diff changeset
214
f8e9d5023bf6 Update runtime files
Bram Moolenaar <Bram@vim.org>
parents: 28956
diff changeset
215 def netlib#ftp#read(fname: string)
f8e9d5023bf6 Update runtime files
Bram Moolenaar <Bram@vim.org>
parents: 28956
diff changeset
216 # Read the file fname through ftp
20856
83cfa1ef1bf2 Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
217 enddef
83cfa1ef1bf2 Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
218
29066
f8e9d5023bf6 Update runtime files
Bram Moolenaar <Bram@vim.org>
parents: 28956
diff changeset
219 Notice that the name the function is defined with is exactly the same as the
f8e9d5023bf6 Update runtime files
Bram Moolenaar <Bram@vim.org>
parents: 28956
diff changeset
220 name used for calling the function. And the part before the last '#'
f8e9d5023bf6 Update runtime files
Bram Moolenaar <Bram@vim.org>
parents: 28956
diff changeset
221 exactly matches the subdirectory and script name.
f8e9d5023bf6 Update runtime files
Bram Moolenaar <Bram@vim.org>
parents: 28956
diff changeset
222
f8e9d5023bf6 Update runtime files
Bram Moolenaar <Bram@vim.org>
parents: 28956
diff changeset
223 You can use the same mechanism for variables: >
f8e9d5023bf6 Update runtime files
Bram Moolenaar <Bram@vim.org>
parents: 28956
diff changeset
224
f8e9d5023bf6 Update runtime files
Bram Moolenaar <Bram@vim.org>
parents: 28956
diff changeset
225 var weekdays = dutch#weekdays
20856
83cfa1ef1bf2 Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
226
29066
f8e9d5023bf6 Update runtime files
Bram Moolenaar <Bram@vim.org>
parents: 28956
diff changeset
227 This will load the script "autoload/dutch.vim", which should contain something
f8e9d5023bf6 Update runtime files
Bram Moolenaar <Bram@vim.org>
parents: 28956
diff changeset
228 like: >
f8e9d5023bf6 Update runtime files
Bram Moolenaar <Bram@vim.org>
parents: 28956
diff changeset
229
f8e9d5023bf6 Update runtime files
Bram Moolenaar <Bram@vim.org>
parents: 28956
diff changeset
230 var dutch#weekdays = ['zondag', 'maandag', 'dinsdag', 'woensdag',
f8e9d5023bf6 Update runtime files
Bram Moolenaar <Bram@vim.org>
parents: 28956
diff changeset
231 \ 'donderdag', 'vrijdag', 'zaterdag']
f8e9d5023bf6 Update runtime files
Bram Moolenaar <Bram@vim.org>
parents: 28956
diff changeset
232
f8e9d5023bf6 Update runtime files
Bram Moolenaar <Bram@vim.org>
parents: 28956
diff changeset
233 Further reading: |autoload|.
20856
83cfa1ef1bf2 Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
234
83cfa1ef1bf2 Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
235 ==============================================================================
29066
f8e9d5023bf6 Update runtime files
Bram Moolenaar <Bram@vim.org>
parents: 28956
diff changeset
236 *52.4* Other mechanisms to use
f8e9d5023bf6 Update runtime files
Bram Moolenaar <Bram@vim.org>
parents: 28956
diff changeset
237
f8e9d5023bf6 Update runtime files
Bram Moolenaar <Bram@vim.org>
parents: 28956
diff changeset
238 Some may find the use of several files a hassle and prefer to keep everything
f8e9d5023bf6 Update runtime files
Bram Moolenaar <Bram@vim.org>
parents: 28956
diff changeset
239 together in one script. To avoid this resulting in slow startup there is a
f8e9d5023bf6 Update runtime files
Bram Moolenaar <Bram@vim.org>
parents: 28956
diff changeset
240 mechanism that only defines a small part and postpones the rest to when it is
f8e9d5023bf6 Update runtime files
Bram Moolenaar <Bram@vim.org>
parents: 28956
diff changeset
241 actually used. *write-plugin-quickload*
f8e9d5023bf6 Update runtime files
Bram Moolenaar <Bram@vim.org>
parents: 28956
diff changeset
242
f8e9d5023bf6 Update runtime files
Bram Moolenaar <Bram@vim.org>
parents: 28956
diff changeset
243 The basic idea is that the plugin is loaded twice. The first time user
f8e9d5023bf6 Update runtime files
Bram Moolenaar <Bram@vim.org>
parents: 28956
diff changeset
244 commands and mappings are defined that offer the functionality. The second
f8e9d5023bf6 Update runtime files
Bram Moolenaar <Bram@vim.org>
parents: 28956
diff changeset
245 time the functions that implement the functionality are defined.
f8e9d5023bf6 Update runtime files
Bram Moolenaar <Bram@vim.org>
parents: 28956
diff changeset
246
f8e9d5023bf6 Update runtime files
Bram Moolenaar <Bram@vim.org>
parents: 28956
diff changeset
247 It may sound surprising that quickload means loading a script twice. What we
f8e9d5023bf6 Update runtime files
Bram Moolenaar <Bram@vim.org>
parents: 28956
diff changeset
248 mean is that it loads quickly the first time, postponing the bulk of the
f8e9d5023bf6 Update runtime files
Bram Moolenaar <Bram@vim.org>
parents: 28956
diff changeset
249 script to the second time, which only happens when you actually use it. When
f8e9d5023bf6 Update runtime files
Bram Moolenaar <Bram@vim.org>
parents: 28956
diff changeset
250 you always use the functionality it actually gets slower!
f8e9d5023bf6 Update runtime files
Bram Moolenaar <Bram@vim.org>
parents: 28956
diff changeset
251
f8e9d5023bf6 Update runtime files
Bram Moolenaar <Bram@vim.org>
parents: 28956
diff changeset
252 This uses a FuncUndefined autocommand. This works differently from the
f8e9d5023bf6 Update runtime files
Bram Moolenaar <Bram@vim.org>
parents: 28956
diff changeset
253 |autoload| functionality explained above.
f8e9d5023bf6 Update runtime files
Bram Moolenaar <Bram@vim.org>
parents: 28956
diff changeset
254
f8e9d5023bf6 Update runtime files
Bram Moolenaar <Bram@vim.org>
parents: 28956
diff changeset
255 The following example shows how it's done: >
f8e9d5023bf6 Update runtime files
Bram Moolenaar <Bram@vim.org>
parents: 28956
diff changeset
256
f8e9d5023bf6 Update runtime files
Bram Moolenaar <Bram@vim.org>
parents: 28956
diff changeset
257 " Vim global plugin for demonstrating quick loading
f8e9d5023bf6 Update runtime files
Bram Moolenaar <Bram@vim.org>
parents: 28956
diff changeset
258 " Last Change: 2005 Feb 25
f8e9d5023bf6 Update runtime files
Bram Moolenaar <Bram@vim.org>
parents: 28956
diff changeset
259 " Maintainer: Bram Moolenaar <Bram@vim.org>
f8e9d5023bf6 Update runtime files
Bram Moolenaar <Bram@vim.org>
parents: 28956
diff changeset
260 " License: This file is placed in the public domain.
f8e9d5023bf6 Update runtime files
Bram Moolenaar <Bram@vim.org>
parents: 28956
diff changeset
261
f8e9d5023bf6 Update runtime files
Bram Moolenaar <Bram@vim.org>
parents: 28956
diff changeset
262 if !exists("s:did_load")
f8e9d5023bf6 Update runtime files
Bram Moolenaar <Bram@vim.org>
parents: 28956
diff changeset
263 command -nargs=* BNRead call BufNetRead(<f-args>)
f8e9d5023bf6 Update runtime files
Bram Moolenaar <Bram@vim.org>
parents: 28956
diff changeset
264 map <F19> :call BufNetWrite('something')<CR>
f8e9d5023bf6 Update runtime files
Bram Moolenaar <Bram@vim.org>
parents: 28956
diff changeset
265
f8e9d5023bf6 Update runtime files
Bram Moolenaar <Bram@vim.org>
parents: 28956
diff changeset
266 let s:did_load = 1
f8e9d5023bf6 Update runtime files
Bram Moolenaar <Bram@vim.org>
parents: 28956
diff changeset
267 exe 'au FuncUndefined BufNet* source ' .. expand('<sfile>')
f8e9d5023bf6 Update runtime files
Bram Moolenaar <Bram@vim.org>
parents: 28956
diff changeset
268 finish
f8e9d5023bf6 Update runtime files
Bram Moolenaar <Bram@vim.org>
parents: 28956
diff changeset
269 endif
f8e9d5023bf6 Update runtime files
Bram Moolenaar <Bram@vim.org>
parents: 28956
diff changeset
270
f8e9d5023bf6 Update runtime files
Bram Moolenaar <Bram@vim.org>
parents: 28956
diff changeset
271 function BufNetRead(...)
f8e9d5023bf6 Update runtime files
Bram Moolenaar <Bram@vim.org>
parents: 28956
diff changeset
272 echo 'BufNetRead(' .. string(a:000) .. ')'
f8e9d5023bf6 Update runtime files
Bram Moolenaar <Bram@vim.org>
parents: 28956
diff changeset
273 " read functionality here
f8e9d5023bf6 Update runtime files
Bram Moolenaar <Bram@vim.org>
parents: 28956
diff changeset
274 endfunction
f8e9d5023bf6 Update runtime files
Bram Moolenaar <Bram@vim.org>
parents: 28956
diff changeset
275
f8e9d5023bf6 Update runtime files
Bram Moolenaar <Bram@vim.org>
parents: 28956
diff changeset
276 function BufNetWrite(...)
f8e9d5023bf6 Update runtime files
Bram Moolenaar <Bram@vim.org>
parents: 28956
diff changeset
277 echo 'BufNetWrite(' .. string(a:000) .. ')'
f8e9d5023bf6 Update runtime files
Bram Moolenaar <Bram@vim.org>
parents: 28956
diff changeset
278 " write functionality here
f8e9d5023bf6 Update runtime files
Bram Moolenaar <Bram@vim.org>
parents: 28956
diff changeset
279 endfunction
f8e9d5023bf6 Update runtime files
Bram Moolenaar <Bram@vim.org>
parents: 28956
diff changeset
280
f8e9d5023bf6 Update runtime files
Bram Moolenaar <Bram@vim.org>
parents: 28956
diff changeset
281 When the script is first loaded "s:did_load" is not set. The commands between
f8e9d5023bf6 Update runtime files
Bram Moolenaar <Bram@vim.org>
parents: 28956
diff changeset
282 the "if" and "endif" will be executed. This ends in a |:finish| command, thus
f8e9d5023bf6 Update runtime files
Bram Moolenaar <Bram@vim.org>
parents: 28956
diff changeset
283 the rest of the script is not executed.
f8e9d5023bf6 Update runtime files
Bram Moolenaar <Bram@vim.org>
parents: 28956
diff changeset
284
f8e9d5023bf6 Update runtime files
Bram Moolenaar <Bram@vim.org>
parents: 28956
diff changeset
285 The second time the script is loaded "s:did_load" exists and the commands
f8e9d5023bf6 Update runtime files
Bram Moolenaar <Bram@vim.org>
parents: 28956
diff changeset
286 after the "endif" are executed. This defines the (possible long)
f8e9d5023bf6 Update runtime files
Bram Moolenaar <Bram@vim.org>
parents: 28956
diff changeset
287 BufNetRead() and BufNetWrite() functions.
f8e9d5023bf6 Update runtime files
Bram Moolenaar <Bram@vim.org>
parents: 28956
diff changeset
288
f8e9d5023bf6 Update runtime files
Bram Moolenaar <Bram@vim.org>
parents: 28956
diff changeset
289 If you drop this script in your plugin directory Vim will execute it on
f8e9d5023bf6 Update runtime files
Bram Moolenaar <Bram@vim.org>
parents: 28956
diff changeset
290 startup. This is the sequence of events that happens:
f8e9d5023bf6 Update runtime files
Bram Moolenaar <Bram@vim.org>
parents: 28956
diff changeset
291
f8e9d5023bf6 Update runtime files
Bram Moolenaar <Bram@vim.org>
parents: 28956
diff changeset
292 1. The "BNRead" command is defined and the <F19> key is mapped when the script
f8e9d5023bf6 Update runtime files
Bram Moolenaar <Bram@vim.org>
parents: 28956
diff changeset
293 is sourced at startup. A |FuncUndefined| autocommand is defined. The
f8e9d5023bf6 Update runtime files
Bram Moolenaar <Bram@vim.org>
parents: 28956
diff changeset
294 ":finish" command causes the script to terminate early.
f8e9d5023bf6 Update runtime files
Bram Moolenaar <Bram@vim.org>
parents: 28956
diff changeset
295
f8e9d5023bf6 Update runtime files
Bram Moolenaar <Bram@vim.org>
parents: 28956
diff changeset
296 2. The user types the BNRead command or presses the <F19> key. The
f8e9d5023bf6 Update runtime files
Bram Moolenaar <Bram@vim.org>
parents: 28956
diff changeset
297 BufNetRead() or BufNetWrite() function will be called.
f8e9d5023bf6 Update runtime files
Bram Moolenaar <Bram@vim.org>
parents: 28956
diff changeset
298
f8e9d5023bf6 Update runtime files
Bram Moolenaar <Bram@vim.org>
parents: 28956
diff changeset
299 3. Vim can't find the function and triggers the |FuncUndefined| autocommand
f8e9d5023bf6 Update runtime files
Bram Moolenaar <Bram@vim.org>
parents: 28956
diff changeset
300 event. Since the pattern "BufNet*" matches the invoked function, the
f8e9d5023bf6 Update runtime files
Bram Moolenaar <Bram@vim.org>
parents: 28956
diff changeset
301 command "source fname" will be executed. "fname" will be equal to the name
f8e9d5023bf6 Update runtime files
Bram Moolenaar <Bram@vim.org>
parents: 28956
diff changeset
302 of the script, no matter where it is located, because it comes from
f8e9d5023bf6 Update runtime files
Bram Moolenaar <Bram@vim.org>
parents: 28956
diff changeset
303 expanding "<sfile>" (see |expand()|).
f8e9d5023bf6 Update runtime files
Bram Moolenaar <Bram@vim.org>
parents: 28956
diff changeset
304
f8e9d5023bf6 Update runtime files
Bram Moolenaar <Bram@vim.org>
parents: 28956
diff changeset
305 4. The script is sourced again, the "s:did_load" variable exists and the
f8e9d5023bf6 Update runtime files
Bram Moolenaar <Bram@vim.org>
parents: 28956
diff changeset
306 functions are defined.
f8e9d5023bf6 Update runtime files
Bram Moolenaar <Bram@vim.org>
parents: 28956
diff changeset
307
f8e9d5023bf6 Update runtime files
Bram Moolenaar <Bram@vim.org>
parents: 28956
diff changeset
308 Notice that the functions that are loaded afterwards match the pattern in the
f8e9d5023bf6 Update runtime files
Bram Moolenaar <Bram@vim.org>
parents: 28956
diff changeset
309 |FuncUndefined| autocommand. You must make sure that no other plugin defines
f8e9d5023bf6 Update runtime files
Bram Moolenaar <Bram@vim.org>
parents: 28956
diff changeset
310 functions that match this pattern.
f8e9d5023bf6 Update runtime files
Bram Moolenaar <Bram@vim.org>
parents: 28956
diff changeset
311
f8e9d5023bf6 Update runtime files
Bram Moolenaar <Bram@vim.org>
parents: 28956
diff changeset
312 ==============================================================================
f8e9d5023bf6 Update runtime files
Bram Moolenaar <Bram@vim.org>
parents: 28956
diff changeset
313 *52.5* Using a Vim9 script from legacy script *source-vim9-script*
20856
83cfa1ef1bf2 Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
314
83cfa1ef1bf2 Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
315 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
316 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
317 best way to do this is to use `:import`. For example: >
83cfa1ef1bf2 Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
318
29087
f3ec3c57e070 Update runtime files.
Bram Moolenaar <Bram@vim.org>
parents: 29066
diff changeset
319 import 'myNicePlugin.vim'
f3ec3c57e070 Update runtime files.
Bram Moolenaar <Bram@vim.org>
parents: 29066
diff changeset
320 call myNicePlugin.NiceInit('today')
20856
83cfa1ef1bf2 Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
321
29087
f3ec3c57e070 Update runtime files.
Bram Moolenaar <Bram@vim.org>
parents: 29066
diff changeset
322 This finds the exported function "NiceInit" in the Vim9 script file and makes
f3ec3c57e070 Update runtime files.
Bram Moolenaar <Bram@vim.org>
parents: 29066
diff changeset
323 it available as script-local item "myNicePlugin.NiceInit". `:import` always
f3ec3c57e070 Update runtime files.
Bram Moolenaar <Bram@vim.org>
parents: 29066
diff changeset
324 uses the script namespace, even when "s:" is not given. If "myNicePlugin.vim"
f3ec3c57e070 Update runtime files.
Bram Moolenaar <Bram@vim.org>
parents: 29066
diff changeset
325 was already sourced it is not sourced again.
20856
83cfa1ef1bf2 Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
326
83cfa1ef1bf2 Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
327 Besides avoiding putting any items in the global namespace (where name clashes
83cfa1ef1bf2 Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
328 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
329 no matter how many times items from it are imported.
83cfa1ef1bf2 Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
330
83cfa1ef1bf2 Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
331 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
332 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
333 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
334 source ~/.vim/extra/myNicePlugin.vim
83cfa1ef1bf2 Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
335 call g:NicePluginTest()
83cfa1ef1bf2 Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
336
83cfa1ef1bf2 Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
337 ==============================================================================
83cfa1ef1bf2 Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
338
83cfa1ef1bf2 Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
339 Next chapter: |usr_90.txt| Installing Vim
83cfa1ef1bf2 Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
340
29066
f8e9d5023bf6 Update runtime files
Bram Moolenaar <Bram@vim.org>
parents: 28956
diff changeset
341
20856
83cfa1ef1bf2 Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
342 Copyright: see |manual-copyright| vim:tw=78:ts=8:noet:ft=help:norl: