usr_52.txt 13 KB

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