Mercurial > vim
comparison runtime/doc/vim9.txt @ 27043:15f40772e10a v8.2.4050
patch 8.2.4050: Vim9: need to prefix every item in an autoload script
Commit: https://github.com/vim/vim/commit/dc4451df61a6aa12a0661817b7094fb32f09e11d
Author: Bram Moolenaar <Bram@vim.org>
Date: Sun Jan 9 21:36:37 2022 +0000
patch 8.2.4050: Vim9: need to prefix every item in an autoload script
Problem: Vim9: need to prefix every item in an autoload script.
Solution: First step in supporting "vim9script autoload" and "import
autoload".
author | Bram Moolenaar <Bram@vim.org> |
---|---|
date | Sun, 09 Jan 2022 22:45:04 +0100 |
parents | 3e661b0cf500 |
children | b19230a8d40a |
comparison
equal
deleted
inserted
replaced
27042:8fc14d120630 | 27043:15f40772e10a |
---|---|
1499 `import` won't be processed yet. Therefore cyclic imports can exist, but may | 1499 `import` won't be processed yet. Therefore cyclic imports can exist, but may |
1500 result in undefined items. | 1500 result in undefined items. |
1501 | 1501 |
1502 | 1502 |
1503 Import in an autoload script ~ | 1503 Import in an autoload script ~ |
1504 | 1504 *vim9-autoload* |
1505 For optimal startup speed, loading scripts should be postponed until they are | 1505 For optimal startup speed, loading scripts should be postponed until they are |
1506 actually needed. A recommended mechanism: | 1506 actually needed. Using the autoload mechanism is recommended: |
1507 | 1507 |
1508 1. In the plugin define user commands, functions and/or mappings that refer to | 1508 1. In the plugin define user commands, functions and/or mappings that refer to |
1509 an autoload script. > | 1509 items imported from an autoload script. > |
1510 command -nargs=1 SearchForStuff searchfor#Stuff(<f-args>) | 1510 import autoload 'for/search.vim' |
1511 command -nargs=1 SearchForStuff search.Stuff(<f-args>) | |
1511 | 1512 |
1512 < This goes in .../plugin/anyname.vim. "anyname.vim" can be freely chosen. | 1513 < This goes in .../plugin/anyname.vim. "anyname.vim" can be freely chosen. |
1513 | 1514 The "SearchForStuff" command is now available to the user. |
1514 2. In the autoload script do the actual work. You can import items from | 1515 |
1515 other files to split up functionality in appropriate pieces. > | 1516 The "autoload" argument to `:import` means that the script is not loaded |
1516 vim9script | 1517 until one of the items is actually used. The script will be found under |
1517 import "../import/someother.vim" as other | 1518 the "autoload" directory in 'runtimepath' instead of the "import" |
1518 def searchfor#Stuff(arg: string) | 1519 directory. |
1519 var filtered = other.FilterFunc(arg) | 1520 |
1521 2. In the autoload script put the bulk of the code. > | |
1522 vim9script autoload | |
1523 export def Stuff(arg: string) | |
1520 ... | 1524 ... |
1521 < This goes in .../autoload/searchfor.vim. "searchfor" in the file name | 1525 |
1522 must be exactly the same as the prefix for the function name, that is how | 1526 < This goes in .../autoload/for/search.vim. |
1523 Vim finds the file. | 1527 |
1524 | 1528 Adding "autoload" to `:vim9script` has the effect that "for#search#" will |
1525 3. Other functionality, possibly shared between plugins, contains the exported | 1529 be prefixed to every exported item. The prefix is obtained from the file |
1526 items and any private items. > | 1530 name, as you would to manually in a legacy autoload script. Thus the |
1527 vim9script | 1531 exported function can be found with "for#search#Stuff", but you would |
1528 var localVar = 'local' | 1532 normally use `import autoload` and not need to specify the prefix. |
1529 export def FilterFunc(arg: string): string | 1533 |
1530 ... | 1534 You can split up the functionality and import other scripts from the |
1531 < This goes in .../import/someother.vim. | 1535 autoload script as you like. This way you can share code between plugins. |
1532 | 1536 |
1533 When compiling a `:def` function and a function in an autoload script is | 1537 When compiling a `:def` function and a function in an autoload script is |
1534 encountered, the script is not loaded until the `:def` function is called. | 1538 encountered, the script is not loaded until the `:def` function is called. |
1539 This also means you get any errors only at runtime, since the argument and | |
1540 return types are not known yet. | |
1535 | 1541 |
1536 | 1542 |
1537 Import in legacy Vim script ~ | 1543 Import in legacy Vim script ~ |
1538 | 1544 |
1539 If an `import` statement is used in legacy Vim script, the script-local "s:" | 1545 If an `import` statement is used in legacy Vim script, the script-local "s:" |