annotate runtime/doc/userfunc.txt @ 31712:2d68375d5ddf v9.0.1188

patch 9.0.1188: return value of type() for class and object unclear Commit: https://github.com/vim/vim/commit/c0c2c262650103c4a21b64c3246388a350688616 Author: Bram Moolenaar <Bram@vim.org> Date: Thu Jan 12 21:08:53 2023 +0000 patch 9.0.1188: return value of type() for class and object unclear Problem: Return value of type() for class and object unclear. Solution: Add v:t_object and v:t_class.
author Bram Moolenaar <Bram@vim.org>
date Thu, 12 Jan 2023 22:15:04 +0100
parents 5ed19049b1e8
children a9b5ffbc0428
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
31671
5ed19049b1e8 Update runtime files
Bram Moolenaar <Bram@vim.org>
parents: 31139
diff changeset
1 *userfunc.txt* For Vim version 9.0. Last change: 2023 Jan 09
30085
ebed259f919f Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2
ebed259f919f Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
3
ebed259f919f Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
4 VIM REFERENCE MANUAL by Bram Moolenaar
ebed259f919f Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
5
ebed259f919f Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
6
ebed259f919f Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
7 Defining and using functions.
ebed259f919f Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
8
ebed259f919f Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
9 This is introduced in section |41.7| of the user manual.
ebed259f919f Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
10
31139
20cf2080f1ee Update runtime files
Bram Moolenaar <Bram@vim.org>
parents: 31028
diff changeset
11 1. Defining a function |define-function|
20cf2080f1ee Update runtime files
Bram Moolenaar <Bram@vim.org>
parents: 31028
diff changeset
12 2. Calling a function |:call|
30085
ebed259f919f Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
13 3. Cleaning up in a function |:defer|
ebed259f919f Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
14 4. Automatically loading functions |autoload-functions|
ebed259f919f Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
15
ebed259f919f Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
16 ==============================================================================
ebed259f919f Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
17
30324
0827d3d6d8c0 Update runtime files
Bram Moolenaar <Bram@vim.org>
parents: 30202
diff changeset
18 1. Defining a function ~
30085
ebed259f919f Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
19 *define-function*
ebed259f919f Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
20 New functions can be defined. These can be called just like builtin
ebed259f919f Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
21 functions. The function executes a sequence of Ex commands. Normal mode
ebed259f919f Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
22 commands can be executed with the |:normal| command.
ebed259f919f Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
23
ebed259f919f Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
24 The function name must start with an uppercase letter, to avoid confusion with
ebed259f919f Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
25 builtin functions. To prevent from using the same name in different scripts
30202
fee9eccee266 Update runtime files
Bram Moolenaar <Bram@vim.org>
parents: 30085
diff changeset
26 make them script-local. If you do use a global function then avoid obvious,
30085
ebed259f919f Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
27 short names. A good habit is to start the function name with the name of the
ebed259f919f Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
28 script, e.g., "HTMLcolor()".
ebed259f919f Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
29
ebed259f919f Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
30 In legacy script it is also possible to use curly braces, see
ebed259f919f Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
31 |curly-braces-names|.
ebed259f919f Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
32
ebed259f919f Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
33 The |autoload| facility is useful to define a function only when it's called.
ebed259f919f Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
34
ebed259f919f Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
35 *local-function*
ebed259f919f Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
36 A function local to a legacy script must start with "s:". A local script
ebed259f919f Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
37 function can only be called from within the script and from functions, user
ebed259f919f Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
38 commands and autocommands defined in the script. It is also possible to call
ebed259f919f Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
39 the function from a mapping defined in the script, but then |<SID>| must be
ebed259f919f Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
40 used instead of "s:" when the mapping is expanded outside of the script.
ebed259f919f Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
41 There are only script-local functions, no buffer-local or window-local
ebed259f919f Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
42 functions.
ebed259f919f Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
43
ebed259f919f Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
44 In |Vim9| script functions are local to the script by default, prefix "g:" to
ebed259f919f Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
45 define a global function.
ebed259f919f Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
46
ebed259f919f Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
47 *:fu* *:function* *E128* *E129* *E123* *E454*
ebed259f919f Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
48 :fu[nction] List all functions and their arguments.
ebed259f919f Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
49
ebed259f919f Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
50 :fu[nction] {name} List function {name}.
ebed259f919f Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
51 {name} can also be a |Dictionary| entry that is a
ebed259f919f Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
52 |Funcref|: >
ebed259f919f Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
53 :function dict.init
ebed259f919f Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
54
ebed259f919f Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
55 :fu[nction] /{pattern} List functions with a name matching {pattern}.
ebed259f919f Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
56 Example that lists all functions ending with "File": >
ebed259f919f Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
57 :function /File$
ebed259f919f Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
58 <
ebed259f919f Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
59 *:function-verbose*
ebed259f919f Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
60 When 'verbose' is non-zero, listing a function will also display where it was
ebed259f919f Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
61 last defined. Example: >
ebed259f919f Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
62
ebed259f919f Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
63 :verbose function SetFileTypeSH
ebed259f919f Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
64 function SetFileTypeSH(name)
ebed259f919f Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
65 Last set from /usr/share/vim/vim-7.0/filetype.vim
ebed259f919f Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
66 <
ebed259f919f Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
67 See |:verbose-cmd| for more information.
ebed259f919f Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
68
ebed259f919f Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
69 *E124* *E125* *E853* *E884*
ebed259f919f Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
70 :fu[nction][!] {name}([arguments]) [range] [abort] [dict] [closure]
ebed259f919f Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
71 Define a new function by the name {name}. The body of
ebed259f919f Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
72 the function follows in the next lines, until the
ebed259f919f Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
73 matching |:endfunction|.
ebed259f919f Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
74 *E1267*
ebed259f919f Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
75 The name must be made of alphanumeric characters and
ebed259f919f Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
76 '_', and must start with a capital or "s:" (see
ebed259f919f Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
77 above). Note that using "b:" or "g:" is not allowed.
ebed259f919f Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
78 (since patch 7.4.260 E884 is given if the function
ebed259f919f Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
79 name has a colon in the name, e.g. for "foo:bar()".
ebed259f919f Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
80 Before that patch no error was given).
ebed259f919f Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
81
ebed259f919f Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
82 {name} can also be a |Dictionary| entry that is a
ebed259f919f Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
83 |Funcref|: >
ebed259f919f Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
84 :function dict.init(arg)
ebed259f919f Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
85 < "dict" must be an existing dictionary. The entry
ebed259f919f Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
86 "init" is added if it didn't exist yet. Otherwise [!]
ebed259f919f Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
87 is required to overwrite an existing function. The
ebed259f919f Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
88 result is a |Funcref| to a numbered function. The
ebed259f919f Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
89 function can only be used with a |Funcref| and will be
ebed259f919f Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
90 deleted if there are no more references to it.
ebed259f919f Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
91 *E127* *E122*
ebed259f919f Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
92 When a function by this name already exists and [!] is
ebed259f919f Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
93 not used an error message is given. There is one
ebed259f919f Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
94 exception: When sourcing a script again, a function
ebed259f919f Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
95 that was previously defined in that script will be
ebed259f919f Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
96 silently replaced.
ebed259f919f Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
97 When [!] is used, an existing function is silently
ebed259f919f Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
98 replaced. Unless it is currently being executed, that
ebed259f919f Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
99 is an error.
ebed259f919f Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
100 NOTE: Use ! wisely. If used without care it can cause
ebed259f919f Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
101 an existing function to be replaced unexpectedly,
ebed259f919f Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
102 which is hard to debug.
ebed259f919f Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
103 NOTE: In Vim9 script script-local functions cannot be
ebed259f919f Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
104 deleted or redefined.
ebed259f919f Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
105
ebed259f919f Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
106 For the {arguments} see |function-argument|.
ebed259f919f Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
107
ebed259f919f Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
108 *:func-range* *a:firstline* *a:lastline*
ebed259f919f Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
109 When the [range] argument is added, the function is
ebed259f919f Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
110 expected to take care of a range itself. The range is
ebed259f919f Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
111 passed as "a:firstline" and "a:lastline". If [range]
ebed259f919f Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
112 is excluded, ":{range}call" will call the function for
ebed259f919f Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
113 each line in the range, with the cursor on the start
ebed259f919f Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
114 of each line. See |function-range-example|.
ebed259f919f Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
115 The cursor is still moved to the first line of the
ebed259f919f Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
116 range, as is the case with all Ex commands.
ebed259f919f Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
117 *:func-abort*
ebed259f919f Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
118 When the [abort] argument is added, the function will
ebed259f919f Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
119 abort as soon as an error is detected.
ebed259f919f Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
120 *:func-dict*
ebed259f919f Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
121 When the [dict] argument is added, the function must
ebed259f919f Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
122 be invoked through an entry in a |Dictionary|. The
ebed259f919f Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
123 local variable "self" will then be set to the
ebed259f919f Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
124 dictionary. See |Dictionary-function|.
ebed259f919f Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
125 *:func-closure* *E932*
ebed259f919f Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
126 When the [closure] argument is added, the function
ebed259f919f Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
127 can access variables and arguments from the outer
ebed259f919f Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
128 scope. This is usually called a closure. In this
ebed259f919f Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
129 example Bar() uses "x" from the scope of Foo(). It
ebed259f919f Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
130 remains referenced even after Foo() returns: >
ebed259f919f Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
131 :function! Foo()
ebed259f919f Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
132 : let x = 0
ebed259f919f Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
133 : function! Bar() closure
ebed259f919f Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
134 : let x += 1
ebed259f919f Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
135 : return x
ebed259f919f Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
136 : endfunction
ebed259f919f Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
137 : return funcref('Bar')
ebed259f919f Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
138 :endfunction
ebed259f919f Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
139
ebed259f919f Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
140 :let F = Foo()
ebed259f919f Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
141 :echo F()
ebed259f919f Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
142 < 1 >
ebed259f919f Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
143 :echo F()
ebed259f919f Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
144 < 2 >
ebed259f919f Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
145 :echo F()
ebed259f919f Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
146 < 3
ebed259f919f Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
147
ebed259f919f Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
148 *function-search-undo*
ebed259f919f Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
149 The last used search pattern and the redo command "."
ebed259f919f Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
150 will not be changed by the function. This also
ebed259f919f Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
151 implies that the effect of |:nohlsearch| is undone
ebed259f919f Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
152 when the function returns.
ebed259f919f Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
153
ebed259f919f Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
154 *:endf* *:endfunction* *E126* *E193* *W22* *E1151*
ebed259f919f Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
155 :endf[unction] [argument]
ebed259f919f Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
156 The end of a function definition. Best is to put it
ebed259f919f Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
157 on a line by its own, without [argument].
ebed259f919f Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
158
ebed259f919f Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
159 [argument] can be:
ebed259f919f Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
160 | command command to execute next
ebed259f919f Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
161 \n command command to execute next
ebed259f919f Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
162 " comment always ignored
ebed259f919f Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
163 anything else ignored, warning given when
ebed259f919f Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
164 'verbose' is non-zero
ebed259f919f Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
165 The support for a following command was added in Vim
ebed259f919f Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
166 8.0.0654, before that any argument was silently
ebed259f919f Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
167 ignored.
ebed259f919f Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
168
ebed259f919f Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
169 To be able to define a function inside an `:execute`
ebed259f919f Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
170 command, use line breaks instead of |:bar|: >
ebed259f919f Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
171 :exe "func Foo()\necho 'foo'\nendfunc"
ebed259f919f Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
172 <
ebed259f919f Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
173 *:delf* *:delfunction* *E131* *E933* *E1084*
ebed259f919f Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
174 :delf[unction][!] {name}
ebed259f919f Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
175 Delete function {name}.
ebed259f919f Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
176 {name} can also be a |Dictionary| entry that is a
ebed259f919f Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
177 |Funcref|: >
ebed259f919f Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
178 :delfunc dict.init
ebed259f919f Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
179 < This will remove the "init" entry from "dict". The
ebed259f919f Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
180 function is deleted if there are no more references to
ebed259f919f Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
181 it.
ebed259f919f Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
182 With the ! there is no error if the function does not
ebed259f919f Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
183 exist.
ebed259f919f Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
184 *:retu* *:return* *E133*
ebed259f919f Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
185 :retu[rn] [expr] Return from a function. When "[expr]" is given, it is
ebed259f919f Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
186 evaluated and returned as the result of the function.
ebed259f919f Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
187 If "[expr]" is not given, the number 0 is returned.
ebed259f919f Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
188 When a function ends without an explicit ":return",
ebed259f919f Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
189 the number 0 is returned.
31671
5ed19049b1e8 Update runtime files
Bram Moolenaar <Bram@vim.org>
parents: 31139
diff changeset
190
30085
ebed259f919f Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
191 In a :def function *E1095* is given if unreachable
ebed259f919f Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
192 code follows after the `:return`.
ebed259f919f Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
193 In legacy script there is no check for unreachable
ebed259f919f Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
194 lines, thus there is no warning if commands follow
31671
5ed19049b1e8 Update runtime files
Bram Moolenaar <Bram@vim.org>
parents: 31139
diff changeset
195 `:return`. Also, there is no check if the following
5ed19049b1e8 Update runtime files
Bram Moolenaar <Bram@vim.org>
parents: 31139
diff changeset
196 line contains a valid command. Forgetting the line
5ed19049b1e8 Update runtime files
Bram Moolenaar <Bram@vim.org>
parents: 31139
diff changeset
197 continuation backslash may go unnoticed: >
5ed19049b1e8 Update runtime files
Bram Moolenaar <Bram@vim.org>
parents: 31139
diff changeset
198 return 'some text'
5ed19049b1e8 Update runtime files
Bram Moolenaar <Bram@vim.org>
parents: 31139
diff changeset
199 .. ' some more text'
5ed19049b1e8 Update runtime files
Bram Moolenaar <Bram@vim.org>
parents: 31139
diff changeset
200 < Will happily return "some text" without an error. It
5ed19049b1e8 Update runtime files
Bram Moolenaar <Bram@vim.org>
parents: 31139
diff changeset
201 should have been: >
5ed19049b1e8 Update runtime files
Bram Moolenaar <Bram@vim.org>
parents: 31139
diff changeset
202 return 'some text'
5ed19049b1e8 Update runtime files
Bram Moolenaar <Bram@vim.org>
parents: 31139
diff changeset
203 \ .. ' some more text'
5ed19049b1e8 Update runtime files
Bram Moolenaar <Bram@vim.org>
parents: 31139
diff changeset
204 <
30085
ebed259f919f Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
205 If the ":return" is used after a |:try| but before the
ebed259f919f Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
206 matching |:finally| (if present), the commands
ebed259f919f Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
207 following the ":finally" up to the matching |:endtry|
ebed259f919f Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
208 are executed first. This process applies to all
ebed259f919f Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
209 nested ":try"s inside the function. The function
ebed259f919f Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
210 returns at the outermost ":endtry".
ebed259f919f Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
211
ebed259f919f Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
212 *function-argument* *a:var*
ebed259f919f Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
213 An argument can be defined by giving its name. In the function this can then
ebed259f919f Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
214 be used as "a:name" ("a:" for argument).
ebed259f919f Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
215 *a:0* *a:1* *a:000* *E740* *...*
ebed259f919f Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
216 Up to 20 arguments can be given, separated by commas. After the named
ebed259f919f Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
217 arguments an argument "..." can be specified, which means that more arguments
ebed259f919f Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
218 may optionally be following. In the function the extra arguments can be used
ebed259f919f Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
219 as "a:1", "a:2", etc. "a:0" is set to the number of extra arguments (which
ebed259f919f Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
220 can be 0). "a:000" is set to a |List| that contains these arguments. Note
ebed259f919f Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
221 that "a:1" is the same as "a:000[0]".
ebed259f919f Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
222 *E742* *E1090*
ebed259f919f Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
223 The a: scope and the variables in it cannot be changed, they are fixed.
ebed259f919f Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
224 However, if a composite type is used, such as |List| or |Dictionary| , you can
ebed259f919f Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
225 change their contents. Thus you can pass a |List| to a function and have the
ebed259f919f Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
226 function add an item to it. If you want to make sure the function cannot
ebed259f919f Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
227 change a |List| or |Dictionary| use |:lockvar|.
ebed259f919f Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
228
ebed259f919f Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
229 It is also possible to define a function without any arguments. You must
ebed259f919f Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
230 still supply the () then.
ebed259f919f Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
231
ebed259f919f Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
232 It is allowed to define another function inside a function body.
ebed259f919f Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
233
ebed259f919f Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
234 *optional-function-argument*
ebed259f919f Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
235 You can provide default values for positional named arguments. This makes
ebed259f919f Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
236 them optional for function calls. When a positional argument is not
ebed259f919f Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
237 specified at a call, the default expression is used to initialize it.
ebed259f919f Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
238 This only works for functions declared with `:function` or `:def`, not for
ebed259f919f Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
239 lambda expressions |expr-lambda|.
ebed259f919f Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
240
ebed259f919f Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
241 Example: >
ebed259f919f Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
242 function Something(key, value = 10)
ebed259f919f Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
243 echo a:key .. ": " .. a:value
ebed259f919f Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
244 endfunction
ebed259f919f Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
245 call Something('empty') "empty: 10"
ebed259f919f Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
246 call Something('key', 20) "key: 20"
ebed259f919f Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
247
ebed259f919f Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
248 The argument default expressions are evaluated at the time of the function
ebed259f919f Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
249 call, not definition. Thus it is possible to use an expression which is
ebed259f919f Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
250 invalid the moment the function is defined. The expressions are also only
ebed259f919f Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
251 evaluated when arguments are not specified during a call.
ebed259f919f Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
252 *none-function_argument*
ebed259f919f Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
253 You can pass |v:none| to use the default expression. Note that this means you
ebed259f919f Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
254 cannot pass v:none as an ordinary value when an argument has a default
ebed259f919f Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
255 expression.
ebed259f919f Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
256
ebed259f919f Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
257 Example: >
ebed259f919f Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
258 function Something(a = 10, b = 20, c = 30)
ebed259f919f Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
259 endfunction
ebed259f919f Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
260 call Something(1, v:none, 3) " b = 20
ebed259f919f Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
261 <
ebed259f919f Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
262 *E989*
ebed259f919f Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
263 Optional arguments with default expressions must occur after any mandatory
ebed259f919f Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
264 arguments. You can use "..." after all optional named arguments.
ebed259f919f Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
265
ebed259f919f Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
266 It is possible for later argument defaults to refer to prior arguments,
ebed259f919f Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
267 but not the other way around. They must be prefixed with "a:", as with all
ebed259f919f Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
268 arguments.
ebed259f919f Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
269
ebed259f919f Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
270 Example that works: >
ebed259f919f Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
271 :function Okay(mandatory, optional = a:mandatory)
ebed259f919f Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
272 :endfunction
ebed259f919f Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
273 Example that does NOT work: >
ebed259f919f Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
274 :function NoGood(first = a:second, second = 10)
ebed259f919f Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
275 :endfunction
ebed259f919f Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
276 <
ebed259f919f Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
277 When not using "...", the number of arguments in a function call must be at
ebed259f919f Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
278 least equal to the number of mandatory named arguments. When using "...", the
ebed259f919f Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
279 number of arguments may be larger than the total of mandatory and optional
ebed259f919f Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
280 arguments.
ebed259f919f Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
281
ebed259f919f Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
282 *local-variables*
ebed259f919f Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
283 Inside a function local variables can be used. These will disappear when the
ebed259f919f Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
284 function returns. Global variables need to be accessed with "g:".
ebed259f919f Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
285 Inside functions local variables are accessed without prepending anything.
ebed259f919f Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
286 But you can also prepend "l:" if you like. This is required for some reserved
ebed259f919f Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
287 names, such as "count".
ebed259f919f Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
288
ebed259f919f Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
289 Example: >
ebed259f919f Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
290 :function Table(title, ...)
ebed259f919f Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
291 : echohl Title
ebed259f919f Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
292 : echo a:title
ebed259f919f Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
293 : echohl None
ebed259f919f Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
294 : echo a:0 .. " items:"
ebed259f919f Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
295 : for s in a:000
ebed259f919f Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
296 : echon ' ' .. s
ebed259f919f Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
297 : endfor
ebed259f919f Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
298 :endfunction
ebed259f919f Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
299
ebed259f919f Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
300 This function can then be called with: >
ebed259f919f Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
301 call Table("Table", "line1", "line2")
ebed259f919f Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
302 call Table("Empty Table")
ebed259f919f Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
303
ebed259f919f Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
304 To return more than one value, return a |List|: >
ebed259f919f Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
305 :function Compute(n1, n2)
ebed259f919f Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
306 : if a:n2 == 0
ebed259f919f Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
307 : return ["fail", 0]
ebed259f919f Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
308 : endif
ebed259f919f Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
309 : return ["ok", a:n1 / a:n2]
ebed259f919f Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
310 :endfunction
ebed259f919f Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
311
ebed259f919f Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
312 This function can then be called with: >
ebed259f919f Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
313 :let [success, div] = Compute(102, 6)
ebed259f919f Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
314 :if success == "ok"
ebed259f919f Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
315 : echo div
ebed259f919f Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
316 :endif
ebed259f919f Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
317 <
ebed259f919f Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
318 ==============================================================================
ebed259f919f Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
319
30324
0827d3d6d8c0 Update runtime files
Bram Moolenaar <Bram@vim.org>
parents: 30202
diff changeset
320 2. Calling a function ~
30085
ebed259f919f Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
321 *:cal* *:call* *E107*
ebed259f919f Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
322 :[range]cal[l] {name}([arguments])
ebed259f919f Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
323 Call a function. The name of the function and its arguments
ebed259f919f Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
324 are as specified with `:function`. Up to 20 arguments can be
ebed259f919f Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
325 used. The returned value is discarded.
ebed259f919f Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
326 In |Vim9| script using `:call` is optional, these two lines do
ebed259f919f Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
327 the same thing: >
ebed259f919f Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
328 call SomeFunc(arg)
ebed259f919f Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
329 SomeFunc(arg)
ebed259f919f Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
330 < Without a range and for functions that accept a range, the
ebed259f919f Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
331 function is called once. When a range is given the cursor is
ebed259f919f Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
332 positioned at the start of the first line before executing the
ebed259f919f Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
333 function.
ebed259f919f Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
334 When a range is given and the function doesn't handle it
ebed259f919f Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
335 itself, the function is executed for each line in the range,
ebed259f919f Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
336 with the cursor in the first column of that line. The cursor
ebed259f919f Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
337 is left at the last line (possibly moved by the last function
ebed259f919f Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
338 call). The arguments are re-evaluated for each line. Thus
ebed259f919f Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
339 this works:
ebed259f919f Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
340 *function-range-example* >
ebed259f919f Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
341 :function Mynumber(arg)
ebed259f919f Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
342 : echo line(".") .. " " .. a:arg
ebed259f919f Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
343 :endfunction
ebed259f919f Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
344 :1,5call Mynumber(getline("."))
ebed259f919f Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
345 <
ebed259f919f Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
346 The "a:firstline" and "a:lastline" are defined anyway, they
ebed259f919f Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
347 can be used to do something different at the start or end of
ebed259f919f Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
348 the range.
ebed259f919f Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
349
ebed259f919f Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
350 Example of a function that handles the range itself: >
ebed259f919f Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
351
ebed259f919f Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
352 :function Cont() range
ebed259f919f Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
353 : execute (a:firstline + 1) .. "," .. a:lastline .. 's/^/\t\\ '
ebed259f919f Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
354 :endfunction
ebed259f919f Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
355 :4,8call Cont()
ebed259f919f Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
356 <
ebed259f919f Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
357 This function inserts the continuation character "\" in front
ebed259f919f Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
358 of all the lines in the range, except the first one.
ebed259f919f Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
359
ebed259f919f Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
360 When the function returns a composite value it can be further
ebed259f919f Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
361 dereferenced, but the range will not be used then. Example: >
ebed259f919f Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
362 :4,8call GetDict().method()
ebed259f919f Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
363 < Here GetDict() gets the range but method() does not.
ebed259f919f Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
364
ebed259f919f Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
365 *E117*
ebed259f919f Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
366 When a function cannot be found the error "E117: Unknown function" will be
ebed259f919f Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
367 given. If the function was using an autoload path or an autoload import and
ebed259f919f Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
368 the script is a |Vim9| script, this may also be caused by the function not
ebed259f919f Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
369 being exported.
ebed259f919f Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
370
ebed259f919f Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
371 *E132*
ebed259f919f Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
372 The recursiveness of user functions is restricted with the |'maxfuncdepth'|
ebed259f919f Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
373 option.
ebed259f919f Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
374
ebed259f919f Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
375 It is also possible to use `:eval`. It does not support a range, but does
ebed259f919f Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
376 allow for method chaining, e.g.: >
ebed259f919f Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
377 eval GetList()->Filter()->append('$')
ebed259f919f Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
378
ebed259f919f Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
379 A function can also be called as part of evaluating an expression or when it
ebed259f919f Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
380 is used as a method: >
ebed259f919f Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
381 let x = GetList()
ebed259f919f Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
382 let y = GetList()->Filter()
ebed259f919f Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
383
ebed259f919f Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
384 ==============================================================================
ebed259f919f Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
385
ebed259f919f Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
386 3. Cleaning up in a function ~
ebed259f919f Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
387 *:defer*
ebed259f919f Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
388 :defer {func}({args}) Call {func} when the current function is done.
ebed259f919f Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
389 {args} are evaluated here.
ebed259f919f Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
390
ebed259f919f Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
391 Quite often a command in a function has a global effect, which must be undone
ebed259f919f Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
392 when the function finishes. Handling this in all kinds of situations can be a
ebed259f919f Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
393 hassle. Especially when an unexpected error is encountered. This can be done
ebed259f919f Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
394 with `try` / `finally` blocks, but this gets complicated when there is more
ebed259f919f Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
395 than one.
ebed259f919f Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
396
ebed259f919f Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
397 A much simpler solution is using `defer`. It schedules a function call when
ebed259f919f Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
398 the function is returning, no matter if there is an error. Example: >
ebed259f919f Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
399 func Filter(text) abort
ebed259f919f Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
400 call writefile(a:text, 'Tempfile')
ebed259f919f Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
401 call system('filter < Tempfile > Outfile')
ebed259f919f Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
402 call Handle('Outfile')
ebed259f919f Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
403 call delete('Tempfile')
ebed259f919f Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
404 call delete('Outfile')
ebed259f919f Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
405 endfunc
ebed259f919f Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
406
ebed259f919f Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
407 Here 'Tempfile' and 'Outfile' will not be deleted if something causes the
ebed259f919f Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
408 function to abort. `:defer` can be used to avoid that: >
ebed259f919f Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
409 func Filter(text) abort
ebed259f919f Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
410 call writefile(a:text, 'Tempfile')
ebed259f919f Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
411 defer delete('Tempfile')
ebed259f919f Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
412 defer delete('Outfile')
ebed259f919f Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
413 call system('filter < Tempfile > Outfile')
ebed259f919f Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
414 call Handle('Outfile')
ebed259f919f Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
415 endfunc
ebed259f919f Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
416
31028
5acd6f02ea35 Update runtime files
Bram Moolenaar <Bram@vim.org>
parents: 30875
diff changeset
417 Note that deleting "Outfile" is scheduled before calling `system()`, since it
30085
ebed259f919f Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
418 can be created even when `system()` fails.
ebed259f919f Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
419
ebed259f919f Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
420 The deferred functions are called in reverse order, the last one added is
ebed259f919f Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
421 executed first. A useless example: >
ebed259f919f Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
422 func Useless() abort
ebed259f919f Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
423 for s in range(3)
ebed259f919f Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
424 defer execute('echomsg "number ' .. s .. '"')
ebed259f919f Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
425 endfor
ebed259f919f Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
426 endfunc
ebed259f919f Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
427
ebed259f919f Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
428 Now `:messages` shows:
ebed259f919f Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
429 number 2
ebed259f919f Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
430 number 1
ebed259f919f Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
431 number 0
ebed259f919f Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
432
ebed259f919f Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
433 Any return value of the deferred function is discarded. The function cannot
ebed259f919f Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
434 be followed by anything, such as "->func" or ".member". Currently `:defer
ebed259f919f Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
435 GetArg()->TheFunc()` does not work, it may work in a later version.
ebed259f919f Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
436
ebed259f919f Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
437 Errors are reported but do not cause aborting execution of deferred functions.
ebed259f919f Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
438
30202
fee9eccee266 Update runtime files
Bram Moolenaar <Bram@vim.org>
parents: 30085
diff changeset
439 No range is accepted. The function can be a partial with extra arguments, but
fee9eccee266 Update runtime files
Bram Moolenaar <Bram@vim.org>
parents: 30085
diff changeset
440 not with a dictionary. *E1300*
30085
ebed259f919f Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
441
ebed259f919f Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
442 ==============================================================================
ebed259f919f Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
443
ebed259f919f Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
444 4. Automatically loading functions ~
ebed259f919f Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
445 *autoload-functions*
ebed259f919f Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
446 When using many or large functions, it's possible to automatically define them
ebed259f919f Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
447 only when they are used. There are two methods: with an autocommand and with
ebed259f919f Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
448 the "autoload" directory in 'runtimepath'.
ebed259f919f Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
449
ebed259f919f Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
450 In |Vim9| script there is also an autoload mechanism for imported scripts, see
ebed259f919f Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
451 |import-autoload|.
ebed259f919f Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
452
ebed259f919f Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
453
ebed259f919f Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
454 Using an autocommand ~
ebed259f919f Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
455
ebed259f919f Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
456 This is introduced in the user manual, section |51.4|.
ebed259f919f Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
457
ebed259f919f Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
458 The autocommand is useful if you have a plugin that is a long Vim script file.
ebed259f919f Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
459 You can define the autocommand and quickly quit the script with `:finish`.
ebed259f919f Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
460 That makes Vim startup faster. The autocommand should then load the same file
ebed259f919f Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
461 again, setting a variable to skip the `:finish` command.
ebed259f919f Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
462
ebed259f919f Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
463 Use the FuncUndefined autocommand event with a pattern that matches the
ebed259f919f Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
464 function(s) to be defined. Example: >
ebed259f919f Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
465
ebed259f919f Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
466 :au FuncUndefined BufNet* source ~/vim/bufnetfuncs.vim
ebed259f919f Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
467
ebed259f919f Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
468 The file "~/vim/bufnetfuncs.vim" should then define functions that start with
ebed259f919f Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
469 "BufNet". Also see |FuncUndefined|.
ebed259f919f Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
470
ebed259f919f Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
471
ebed259f919f Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
472 Using an autoload script ~
ebed259f919f Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
473 *autoload* *E746*
ebed259f919f Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
474 This is introduced in the user manual, section |52.2|.
ebed259f919f Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
475
ebed259f919f Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
476 Using a script in the "autoload" directory is simpler, but requires using
ebed259f919f Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
477 exactly the right file name. A function that can be autoloaded has a name
ebed259f919f Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
478 like this: >
ebed259f919f Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
479
ebed259f919f Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
480 :call filename#funcname()
ebed259f919f Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
481
ebed259f919f Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
482 These functions are always global, in Vim9 script "g:" needs to be used: >
ebed259f919f Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
483 :call g:filename#funcname()
ebed259f919f Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
484
ebed259f919f Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
485 When such a function is called, and it is not defined yet, Vim will search the
ebed259f919f Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
486 "autoload" directories in 'runtimepath' for a script file called
ebed259f919f Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
487 "filename.vim". For example "~/.vim/autoload/filename.vim". That file should
ebed259f919f Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
488 then define the function like this: >
ebed259f919f Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
489
ebed259f919f Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
490 function filename#funcname()
ebed259f919f Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
491 echo "Done!"
ebed259f919f Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
492 endfunction
ebed259f919f Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
493
30875
3295247d97a5 Update runtime files
Bram Moolenaar <Bram@vim.org>
parents: 30324
diff changeset
494 If the file doesn't exist, Vim will also search in 'packpath' (under "start")
3295247d97a5 Update runtime files
Bram Moolenaar <Bram@vim.org>
parents: 30324
diff changeset
495 to allow calling packages' functions from your .vimrc when the packages have
3295247d97a5 Update runtime files
Bram Moolenaar <Bram@vim.org>
parents: 30324
diff changeset
496 not been added to 'runtimepath' yet (see |packages|).
3295247d97a5 Update runtime files
Bram Moolenaar <Bram@vim.org>
parents: 30324
diff changeset
497
30085
ebed259f919f Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
498 The file name and the name used before the # in the function must match
ebed259f919f Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
499 exactly, and the defined function must have the name exactly as it will be
ebed259f919f Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
500 called. In Vim9 script the "g:" prefix must be used: >
ebed259f919f Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
501 function g:filename#funcname()
ebed259f919f Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
502
ebed259f919f Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
503 or for a compiled function: >
ebed259f919f Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
504 def g:filename#funcname()
ebed259f919f Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
505
ebed259f919f Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
506 It is possible to use subdirectories. Every # in the function name works like
ebed259f919f Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
507 a path separator. Thus when calling a function: >
ebed259f919f Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
508
ebed259f919f Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
509 :call foo#bar#func()
ebed259f919f Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
510
ebed259f919f Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
511 Vim will look for the file "autoload/foo/bar.vim" in 'runtimepath'.
ebed259f919f Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
512
ebed259f919f Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
513 This also works when reading a variable that has not been set yet: >
ebed259f919f Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
514
ebed259f919f Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
515 :let l = foo#bar#lvar
ebed259f919f Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
516
ebed259f919f Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
517 However, when the autoload script was already loaded it won't be loaded again
ebed259f919f Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
518 for an unknown variable.
ebed259f919f Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
519
ebed259f919f Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
520 When assigning a value to such a variable nothing special happens. This can
ebed259f919f Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
521 be used to pass settings to the autoload script before it's loaded: >
ebed259f919f Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
522
ebed259f919f Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
523 :let foo#bar#toggle = 1
ebed259f919f Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
524 :call foo#bar#func()
ebed259f919f Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
525
ebed259f919f Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
526 Note that when you make a mistake and call a function that is supposed to be
ebed259f919f Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
527 defined in an autoload script, but the script doesn't actually define the
ebed259f919f Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
528 function, you will get an error message for the missing function. If you fix
ebed259f919f Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
529 the autoload script it won't be automatically loaded again. Either restart
ebed259f919f Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
530 Vim or manually source the script.
ebed259f919f Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
531
ebed259f919f Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
532 Also note that if you have two script files, and one calls a function in the
ebed259f919f Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
533 other and vice versa, before the used function is defined, it won't work.
ebed259f919f Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
534 Avoid using the autoload functionality at the toplevel.
ebed259f919f Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
535
ebed259f919f Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
536 In |Vim9| script you will get error *E1263* if you define a function with
ebed259f919f Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
537 a "#" character in the name. You should use a name without "#" and use
ebed259f919f Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
538 `:export`.
ebed259f919f Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
539
ebed259f919f Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
540 Hint: If you distribute a bunch of scripts you can pack them together with the
ebed259f919f Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
541 |vimball| utility. Also read the user manual |distribute-script|.
ebed259f919f Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
542
ebed259f919f Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
543
ebed259f919f Update runtime files
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
544 vim:tw=78:ts=8:noet:ft=help:norl: