blob: cb68eefe005637ff7186ba779c015a8f2a7bcf81 [file] [log] [blame]
Bram Moolenaar130cbfc2021-04-07 21:07:20 +02001*if_lua.txt* For Vim version 8.2. Last change: 2021 Apr 07
Bram Moolenaar0ba04292010-07-14 23:23:17 +02002
3
4 VIM REFERENCE MANUAL by Luis Carvalho
5
6
7The Lua Interface to Vim *lua* *Lua*
8
91. Commands |lua-commands|
102. The vim module |lua-vim|
Bram Moolenaar1dced572012-04-05 16:54:08 +0200113. List userdata |lua-list|
124. Dict userdata |lua-dict|
Bram Moolenaarb7828692019-03-23 13:57:02 +0100135. Blob userdata |lua-blob|
146. Funcref userdata |lua-funcref|
157. Buffer userdata |lua-buffer|
168. Window userdata |lua-window|
179. luaeval() Vim function |lua-luaeval|
1810. Dynamic loading |lua-dynamic|
Bram Moolenaar0ba04292010-07-14 23:23:17 +020019
Bram Moolenaar25c9c682019-05-05 18:13:34 +020020{only available when Vim was compiled with the |+lua| feature}
Bram Moolenaar0ba04292010-07-14 23:23:17 +020021
22==============================================================================
231. Commands *lua-commands*
24
25 *:lua*
26:[range]lua {chunk}
Bram Moolenaar25c9c682019-05-05 18:13:34 +020027 Execute Lua chunk {chunk}.
Bram Moolenaar0ba04292010-07-14 23:23:17 +020028
29Examples:
30>
31 :lua print("Hello, Vim!")
32 :lua local curbuf = vim.buffer() curbuf[7] = "line #7"
33<
34
Bram Moolenaar6c2b7b82020-04-14 20:15:49 +020035:[range]lua << [trim] [{endmarker}]
Bram Moolenaar0ba04292010-07-14 23:23:17 +020036{script}
37{endmarker}
Bram Moolenaar25c9c682019-05-05 18:13:34 +020038 Execute Lua script {script}.
Bram Moolenaar0ba04292010-07-14 23:23:17 +020039 Note: This command doesn't work when the Lua
40 feature wasn't compiled in. To avoid errors, see
41 |script-here|.
42
Bram Moolenaar54775062019-07-31 21:07:14 +020043If [endmarker] is omitted from after the "<<", a dot '.' must be used after
Bram Moolenaar6c2b7b82020-04-14 20:15:49 +020044{script}, like for the |:append| and |:insert| commands. Refer to
45|:let-heredoc| for more information.
Bram Moolenaar54775062019-07-31 21:07:14 +020046
Bram Moolenaar0ba04292010-07-14 23:23:17 +020047This form of the |:lua| command is mainly useful for including Lua code
48in Vim scripts.
49
50Example:
51>
52 function! CurrentLineInfo()
53 lua << EOF
54 local linenr = vim.window().line
55 local curline = vim.buffer()[linenr]
56 print(string.format("Current line [%d] has %d chars",
57 linenr, #curline))
58 EOF
59 endfunction
60<
Bram Moolenaarabd468e2016-09-08 22:22:43 +020061To see what version of Lua you have: >
62 :lua print(_VERSION)
63
64If you use LuaJIT you can also use this: >
65 :lua print(jit.version)
66<
Bram Moolenaar0ba04292010-07-14 23:23:17 +020067
68 *:luado*
Bram Moolenaar53bfca22012-04-13 23:04:47 +020069:[range]luado {body} Execute Lua function "function (line, linenr) {body}
70 end" for each line in the [range], with the function
71 argument being set to the text of each line in turn,
72 without a trailing <EOL>, and the current line number.
73 If the value returned by the function is a string it
74 becomes the text of the line in the current turn. The
75 default for [range] is the whole file: "1,$".
Bram Moolenaar0ba04292010-07-14 23:23:17 +020076
77Examples:
78>
79 :luado return string.format("%s\t%d", line:reverse(), #line)
80
81 :lua require"lpeg"
82 :lua -- balanced parenthesis grammar:
83 :lua bp = lpeg.P{ "(" * ((1 - lpeg.S"()") + lpeg.V(1))^0 * ")" }
84 :luado if bp:match(line) then return "-->\t" .. line end
85<
86
87 *:luafile*
88:[range]luafile {file}
Bram Moolenaar25c9c682019-05-05 18:13:34 +020089 Execute Lua script in {file}.
Bram Moolenaar0ba04292010-07-14 23:23:17 +020090 The whole argument is used as a single file name.
91
92Examples:
93>
94 :luafile script.lua
95 :luafile %
96<
97
98All these commands execute a Lua chunk from either the command line (:lua and
99:luado) or a file (:luafile) with the given line [range]. Similarly to the Lua
100interpreter, each chunk has its own scope and so only global variables are
Bram Moolenaar1dced572012-04-05 16:54:08 +0200101shared between command calls. All Lua default libraries are available. In
102addition, Lua "print" function has its output redirected to the Vim message
103area, with arguments separated by a white space instead of a tab.
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200104
Bram Moolenaar9855d6b2010-07-18 14:34:51 +0200105Lua uses the "vim" module (see |lua-vim|) to issue commands to Vim
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200106and manage buffers (|lua-buffer|) and windows (|lua-window|). However,
107procedures that alter buffer content, open new buffers, and change cursor
Bram Moolenaar9855d6b2010-07-18 14:34:51 +0200108position are restricted when the command is executed in the |sandbox|.
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200109
110
111==============================================================================
1122. The vim module *lua-vim*
113
114Lua interfaces Vim through the "vim" module. The first and last line of the
Bram Moolenaar2334b6d2010-07-22 21:32:16 +0200115input range are stored in "vim.firstline" and "vim.lastline" respectively. The
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200116module also includes routines for buffer, window, and current line queries,
117Vim evaluation and command execution, and others.
118
Bram Moolenaar52b91d82013-06-15 21:39:51 +0200119 vim.list([arg]) Returns an empty list or, if "arg" is a Lua
120 table with numeric keys 1, ..., n (a
121 "sequence"), returns a list l such that l[i] =
122 arg[i] for i = 1, ..., n (see |List|).
123 Non-numeric keys are not used to initialize
124 the list. See also |lua-eval| for conversion
125 rules. Example: >
Bram Moolenaarfd358112018-07-07 23:21:31 +0200126 :lua t = {math.pi, false, say = 'hi'}
127 :echo luaeval('vim.list(t)')
128 :" [3.141593, v:false], 'say' is ignored
Bram Moolenaar52b91d82013-06-15 21:39:51 +0200129<
130 vim.dict([arg]) Returns an empty dictionary or, if "arg" is a
131 Lua table, returns a dict d such that d[k] =
132 arg[k] for all string keys k in "arg" (see
133 |Dictionary|). Number keys are converted to
134 strings. Keys that are not strings are not
135 used to initialize the dictionary. See also
136 |lua-eval| for conversion rules. Example: >
Bram Moolenaarfd358112018-07-07 23:21:31 +0200137 :lua t = {math.pi, false, say = 'hi'}
138 :echo luaeval('vim.dict(t)')
139 :" {'1': 3.141593, '2': v:false,
140 :" 'say': 'hi'}
Bram Moolenaar52b91d82013-06-15 21:39:51 +0200141<
Bram Moolenaarb7828692019-03-23 13:57:02 +0100142 vim.blob([arg]) Returns an empty blob or, if "arg" is a Lua
143 string, returns a blob b such that b is
144 equivalent to "arg" as a byte string.
145 Examples: >
146 :lua s = "12ab\x00\x80\xfe\xff"
147 :echo luaeval('vim.blob(s)')
148 :" 0z31326162.0080FEFF
149<
Bram Moolenaar52b91d82013-06-15 21:39:51 +0200150 vim.funcref({name}) Returns a Funcref to function {name} (see
Bram Moolenaarfd358112018-07-07 23:21:31 +0200151 |Funcref|). It is equivalent to Vim's
152 function().
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200153
Bram Moolenaar2334b6d2010-07-22 21:32:16 +0200154 vim.buffer([arg]) If "arg" is a number, returns buffer with
155 number "arg" in the buffer list or, if "arg"
156 is a string, returns buffer whose full or short
157 name is "arg". In both cases, returns 'nil'
158 (nil value, not string) if the buffer is not
159 found. Otherwise, if "toboolean(arg)" is
160 'true' returns the first buffer in the buffer
161 list or else the current buffer.
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200162
Bram Moolenaar2334b6d2010-07-22 21:32:16 +0200163 vim.window([arg]) If "arg" is a number, returns window with
164 number "arg" or 'nil' (nil value, not string)
165 if not found. Otherwise, if "toboolean(arg)"
166 is 'true' returns the first window or else the
167 current window.
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200168
Bram Moolenaar1dced572012-04-05 16:54:08 +0200169 vim.type({arg}) Returns the type of {arg}. It is equivalent to
170 Lua's "type" function, but returns "list",
Bram Moolenaar52b91d82013-06-15 21:39:51 +0200171 "dict", "funcref", "buffer", or "window" if
172 {arg} is a list, dictionary, funcref, buffer,
173 or window, respectively. Examples: >
Bram Moolenaar1dced572012-04-05 16:54:08 +0200174 :lua l = vim.list()
175 :lua print(type(l), vim.type(l))
Bram Moolenaar2f362bf2018-07-01 19:49:27 +0200176 :" list
Bram Moolenaar1dced572012-04-05 16:54:08 +0200177<
Bram Moolenaar2334b6d2010-07-22 21:32:16 +0200178 vim.command({cmd}) Executes the vim (ex-mode) command {cmd}.
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200179 Examples: >
180 :lua vim.command"set tw=60"
181 :lua vim.command"normal ddp"
182<
Bram Moolenaar2334b6d2010-07-22 21:32:16 +0200183 vim.eval({expr}) Evaluates expression {expr} (see |expression|),
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200184 converts the result to Lua, and returns it.
185 Vim strings and numbers are directly converted
186 to Lua strings and numbers respectively. Vim
187 lists and dictionaries are converted to Lua
Bram Moolenaar1dced572012-04-05 16:54:08 +0200188 userdata (see |lua-list| and |lua-dict|).
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200189 Examples: >
190 :lua tw = vim.eval"&tw"
191 :lua print(vim.eval"{'a': 'one'}".a)
192<
Bram Moolenaar2334b6d2010-07-22 21:32:16 +0200193 vim.line() Returns the current line (without the trailing
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200194 <EOL>), a Lua string.
195
Bram Moolenaar2334b6d2010-07-22 21:32:16 +0200196 vim.beep() Beeps.
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200197
Bram Moolenaar2334b6d2010-07-22 21:32:16 +0200198 vim.open({fname}) Opens a new buffer for file {fname} and
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200199 returns it. Note that the buffer is not set as
200 current.
201
Bram Moolenaare7b1ea02020-08-07 19:54:59 +0200202 vim.call({name} [, {args}])
203 Proxy to call Vim function named {name} with
Bram Moolenaareb04f082020-05-17 14:32:35 +0200204 arguments {args}. Example: >
205 :lua print(vim.call('has', 'timers'))
206<
207 vim.fn Proxy to call Vim functions. Proxy methods are
208 created on demand. Example: >
209 :lua print(vim.fn.has('timers'))
210<
Bram Moolenaar125ed272021-04-07 20:11:12 +0200211 vim.lua_version The Lua version Vim was compiled with, in the
212 form {major}.{minor}.{patch}, e.g. "5.1.4".
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200213
Yegappan Lakshmanan9dc4bef2021-08-04 21:12:52 +0200214 *lua-vim-variables*
215The Vim editor global dictionaries |g:| |w:| |b:| |t:| |v:| can be accessed
216from Lua conveniently and idiomatically by referencing the `vim.*` Lua tables
217described below. In this way you can easily read and modify global Vimscript
218variables from Lua.
219
220Example: >
221
222 vim.g.foo = 5 -- Set the g:foo Vimscript variable.
223 print(vim.g.foo) -- Get and print the g:foo Vimscript variable.
224 vim.g.foo = nil -- Delete (:unlet) the Vimscript variable.
225
226vim.g *vim.g*
227 Global (|g:|) editor variables.
228 Key with no value returns `nil`.
229
230vim.b *vim.b*
231 Buffer-scoped (|b:|) variables for the current buffer.
232 Invalid or unset key returns `nil`.
233
234vim.w *vim.w*
235 Window-scoped (|w:|) variables for the current window.
236 Invalid or unset key returns `nil`.
237
238vim.t *vim.t*
239 Tabpage-scoped (|t:|) variables for the current tabpage.
240 Invalid or unset key returns `nil`.
241
242vim.v *vim.v*
243 |v:| variables.
244 Invalid or unset key returns `nil`.
245
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200246==============================================================================
Bram Moolenaar1dced572012-04-05 16:54:08 +02002473. List userdata *lua-list*
248
249List userdata represent vim lists, and the interface tries to follow closely
250Vim's syntax for lists. Since lists are objects, changes in list references in
251Lua are reflected in Vim and vice-versa. A list "l" has the following
252properties and methods:
253
Bram Moolenaarbd846172020-06-27 12:32:57 +0200254NOTE: In patch 8.2.1066 array indexes were changed from zero-based to
255one-based. You can check with: >
256 if has("patch-8.2.1066")
257
Bram Moolenaar1dced572012-04-05 16:54:08 +0200258Properties
259----------
260 o "#l" is the number of items in list "l", equivalent to "len(l)"
261 in Vim.
Bram Moolenaarbd846172020-06-27 12:32:57 +0200262 o "l[k]" returns the k-th item in "l"; "l" is one-indexed, as in Lua.
Bram Moolenaar1dced572012-04-05 16:54:08 +0200263 To modify the k-th item, simply do "l[k] = newitem"; in
Bram Moolenaara1f9f862020-06-28 22:41:26 +0200264 particular, "l[k] = nil" removes the k-th item from "l". Item can
265 be added to the end of the list by "l[#l + 1] = newitem"
Bram Moolenaar1dced572012-04-05 16:54:08 +0200266 o "l()" returns an iterator for "l".
Bram Moolenaara1f9f862020-06-28 22:41:26 +0200267 o "table.insert(l, newitem)" inserts an item at the end of the list.
268 (only Lua 5.3 and later)
269 o "table.insert(l, position, newitem)" inserts an item at the
270 specified position. "position" is one-indexed. (only Lua 5.3 and
271 later)
272 o "table.remove(l, position)" removes an item at the specified
273 position. "position" is one-indexed.
274
Bram Moolenaar1dced572012-04-05 16:54:08 +0200275
276Methods
277-------
278 o "l:add(item)" appends "item" to the end of "l".
279 o "l:insert(item[, pos])" inserts "item" at (optional)
280 position "pos" in the list. The default value for "pos" is 0.
281
282Examples:
283>
284 :let l = [1, 'item']
285 :lua l = vim.eval('l') -- same 'l'
286 :lua l:add(vim.list())
Bram Moolenaarbd846172020-06-27 12:32:57 +0200287 :lua l[1] = math.pi
Bram Moolenaar1dced572012-04-05 16:54:08 +0200288 :echo l[0] " 3.141593
Bram Moolenaarbd846172020-06-27 12:32:57 +0200289 :lua l[1] = nil -- remove first item
Bram Moolenaar1dced572012-04-05 16:54:08 +0200290 :lua l:insert(true, 1)
Bram Moolenaarbd846172020-06-27 12:32:57 +0200291 :lua print(l, #l, l[1], l[2])
Bram Moolenaara1f9f862020-06-28 22:41:26 +0200292 :lua l[#l + 1] = 'value'
293 :lua table.insert(l, 100)
294 :lua table.insert(l, 2, 200)
295 :lua table.remove(l, 1)
Bram Moolenaar1dced572012-04-05 16:54:08 +0200296 :lua for item in l() do print(item) end
Bram Moolenaar1dced572012-04-05 16:54:08 +0200297
298==============================================================================
2994. Dict userdata *lua-dict*
300
301Similarly to list userdata, dict userdata represent vim dictionaries; since
302dictionaries are also objects, references are kept between Lua and Vim. A dict
303"d" has the following properties:
304
305Properties
306----------
307 o "#d" is the number of items in dict "d", equivalent to "len(d)"
308 in Vim.
309 o "d.key" or "d['key']" returns the value at entry "key" in "d".
310 To modify the entry at this key, simply do "d.key = newvalue"; in
311 particular, "d.key = nil" removes the entry from "d".
312 o "d()" returns an iterator for "d" and is equivalent to "items(d)" in
313 Vim.
314
315Examples:
316>
317 :let d = {'n':10}
318 :lua d = vim.eval('d') -- same 'd'
319 :lua print(d, d.n, #d)
320 :let d.self = d
321 :lua for k, v in d() do print(d, k, v) end
322 :lua d.x = math.pi
323 :lua d.self = nil -- remove entry
324 :echo d
325<
326
327==============================================================================
Bram Moolenaarb7828692019-03-23 13:57:02 +01003285. Blob userdata *lua-blob*
329
330Blob userdata represent vim blobs. A blob "b" has the following properties:
331
332Properties
333----------
334 o "#b" is the length of blob "b", equivalent to "len(b)" in Vim.
335 o "b[k]" returns the k-th item in "b"; "b" is zero-indexed, as in Vim.
336 To modify the k-th item, simply do "b[k] = number"; in particular,
337 "b[#b] = number" can append a byte to tail.
338
339Methods
340-------
341 o "b:add(bytes)" appends "bytes" to the end of "b".
342
343Examples:
344>
345 :let b = 0z001122
346 :lua b = vim.eval('b') -- same 'b'
347 :lua print(b, b[0], #b)
348 :lua b[1] = 32
349 :lua b[#b] = 0x33 -- append a byte to tail
350 :lua b:add("\x80\x81\xfe\xff")
351 :echo b
352<
353
354==============================================================================
3556. Funcref userdata *lua-funcref*
Bram Moolenaar52b91d82013-06-15 21:39:51 +0200356
357Funcref userdata represent funcref variables in Vim. Funcrefs that were
358defined with a "dict" attribute need to be obtained as a dictionary key
359in order to have "self" properly assigned to the dictionary (see examples
360below.) A funcref "f" has the following properties:
361
362Properties
363----------
364 o "#f" is the name of the function referenced by "f"
365 o "f(...)" calls the function referenced by "f" (with arguments)
366
367Examples:
368>
369 :function I(x)
370 : return a:x
371 : endfunction
372 :let R = function('I')
373 :lua i1 = vim.funcref('I')
374 :lua i2 = vim.eval('R')
375 :lua print(#i1, #i2) -- both 'I'
376 :lua print(i1, i2, #i2(i1) == #i1(i2))
377 :function Mylen() dict
378 : return len(self.data)
379 : endfunction
380 :let mydict = {'data': [0, 1, 2, 3]}
381 :lua d = vim.eval('mydict'); d.len = vim.funcref('Mylen')
382 :echo mydict.len()
383 :lua l = d.len -- assign d as 'self'
384 :lua print(l())
385<
Bram Moolenaar801ab062020-06-25 19:27:56 +0200386Lua functions and closures are automatically converted to a Vim |Funcref| and
387can be accessed in Vim scripts. Example:
388>
389 lua <<EOF
390 vim.fn.timer_start(1000, function(timer)
391 print('timer callback')
392 end)
393 EOF
Bram Moolenaar52b91d82013-06-15 21:39:51 +0200394
395==============================================================================
Bram Moolenaarb7828692019-03-23 13:57:02 +01003967. Buffer userdata *lua-buffer*
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200397
Bram Moolenaar2334b6d2010-07-22 21:32:16 +0200398Buffer userdata represent vim buffers. A buffer userdata "b" has the following
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200399properties and methods:
400
401Properties
402----------
Bram Moolenaar2334b6d2010-07-22 21:32:16 +0200403 o "b()" sets "b" as the current buffer.
404 o "#b" is the number of lines in buffer "b".
405 o "b[k]" represents line number k: "b[k] = newline" replaces line k
406 with string "newline" and "b[k] = nil" deletes line k.
407 o "b.name" contains the short name of buffer "b" (read-only).
408 o "b.fname" contains the full name of buffer "b" (read-only).
409 o "b.number" contains the position of buffer "b" in the buffer list
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200410 (read-only).
411
412Methods
413-------
Bram Moolenaar2334b6d2010-07-22 21:32:16 +0200414 o "b:insert(newline[, pos])" inserts string "newline" at (optional)
415 position "pos" in the buffer. The default value for "pos" is
416 "#b + 1". If "pos == 0" then "newline" becomes the first line in
417 the buffer.
418 o "b:next()" returns the buffer next to "b" in the buffer list.
419 o "b:previous()" returns the buffer previous to "b" in the buffer
420 list.
421 o "b:isvalid()" returns 'true' (boolean) if buffer "b" corresponds to
422 a "real" (not freed from memory) Vim buffer.
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200423
424Examples:
425>
426 :lua b = vim.buffer() -- current buffer
427 :lua print(b.name, b.number)
428 :lua b[1] = "first line"
429 :lua b:insert("FIRST!", 0)
430 :lua b[1] = nil -- delete top line
431 :lua for i=1,3 do b:insert(math.random()) end
432 :3,4lua for i=vim.lastline,vim.firstline,-1 do b[i] = nil end
433 :lua vim.open"myfile"() -- open buffer and set it as current
434
435 function! ListBuffers()
436 lua << EOF
437 local b = vim.buffer(true) -- first buffer in list
438 while b ~= nil do
439 print(b.number, b.name, #b)
440 b = b:next()
441 end
442 vim.beep()
443 EOF
444 endfunction
445<
446
447==============================================================================
Bram Moolenaarb7828692019-03-23 13:57:02 +01004488. Window userdata *lua-window*
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200449
Bram Moolenaar2334b6d2010-07-22 21:32:16 +0200450Window objects represent vim windows. A window userdata "w" has the following
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200451properties and methods:
452
453Properties
454----------
Bram Moolenaar2334b6d2010-07-22 21:32:16 +0200455 o "w()" sets "w" as the current window.
456 o "w.buffer" contains the buffer of window "w" (read-only).
457 o "w.line" represents the cursor line position in window "w".
458 o "w.col" represents the cursor column position in window "w".
459 o "w.width" represents the width of window "w".
460 o "w.height" represents the height of window "w".
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200461
462Methods
463-------
Bram Moolenaar2334b6d2010-07-22 21:32:16 +0200464 o "w:next()" returns the window next to "w".
465 o "w:previous()" returns the window previous to "w".
466 o "w:isvalid()" returns 'true' (boolean) if window "w" corresponds to
467 a "real" (not freed from memory) Vim window.
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200468
469Examples:
470>
471 :lua w = vim.window() -- current window
472 :lua print(w.buffer.name, w.line, w.col)
473 :lua w.width = w.width + math.random(10)
474 :lua w.height = 2 * math.random() * w.height
475 :lua n,w = 0,vim.window(true) while w~=nil do n,w = n + 1,w:next() end
476 :lua print("There are " .. n .. " windows")
477<
478
479==============================================================================
Bram Moolenaarb7828692019-03-23 13:57:02 +01004809. luaeval() Vim function *lua-luaeval* *lua-eval*
Bram Moolenaar1dced572012-04-05 16:54:08 +0200481
482The (dual) equivalent of "vim.eval" for passing Lua values to Vim is
483"luaeval". "luaeval" takes an expression string and an optional argument and
484returns the result of the expression. It is semantically equivalent in Lua to:
485>
486 local chunkheader = "local _A = select(1, ...) return "
487 function luaeval (expstr, arg)
488 local chunk = assert(loadstring(chunkheader .. expstr, "luaeval"))
489 return chunk(arg) -- return typval
490 end
491<
Bram Moolenaar52b91d82013-06-15 21:39:51 +0200492Note that "_A" receives the argument to "luaeval". Lua numbers, strings, and
Bram Moolenaarb7828692019-03-23 13:57:02 +0100493list, dict, blob, and funcref userdata are converted to their Vim respective
494types, while Lua booleans are converted to numbers. An error is thrown if
495conversion of any of the remaining Lua types, including userdata other than
496lists, dicts, blobs, and funcrefs, is attempted.
Bram Moolenaar52b91d82013-06-15 21:39:51 +0200497
498Examples: >
Bram Moolenaar1dced572012-04-05 16:54:08 +0200499
500 :echo luaeval('math.pi')
501 :lua a = vim.list():add('newlist')
502 :let a = luaeval('a')
503 :echo a[0] " 'newlist'
504 :function Rand(x,y) " random uniform between x and y
505 : return luaeval('(_A.y-_A.x)*math.random()+_A.x', {'x':a:x,'y':a:y})
506 : endfunction
507 :echo Rand(1,10)
508
509
510==============================================================================
Bram Moolenaarb7828692019-03-23 13:57:02 +010051110. Dynamic loading *lua-dynamic*
Bram Moolenaard94464e2015-11-02 15:28:18 +0100512
513On MS-Windows and Unix the Lua library can be loaded dynamically. The
514|:version| output then includes |+lua/dyn|.
515
516This means that Vim will search for the Lua DLL or shared library file only
517when needed. When you don't use the Lua interface you don't need it, thus
518you can use Vim without this file.
519
Bram Moolenaard94464e2015-11-02 15:28:18 +0100520
Bram Moolenaare18c0b32016-03-20 21:08:34 +0100521MS-Windows ~
522
523To use the Lua interface the Lua DLL must be in your search path. In a
524console window type "path" to see what directories are used. The 'luadll'
525option can be also used to specify the Lua DLL. The version of the DLL must
526match the Lua version Vim was compiled with.
527
528
529Unix ~
530
531The 'luadll' option can be used to specify the Lua shared library file instead
532of DYNAMIC_LUA_DLL file what was specified at compile time. The version of
533the shared library must match the Lua version Vim was compiled with.
Bram Moolenaard94464e2015-11-02 15:28:18 +0100534
535
536==============================================================================
Bram Moolenaar1dced572012-04-05 16:54:08 +0200537 vim:tw=78:ts=8:noet:ft=help:norl: