Bram Moolenaar | e46a440 | 2020-06-30 20:38:27 +0200 | [diff] [blame] | 1 | *if_lua.txt* For Vim version 8.2. Last change: 2020 Jun 28 |
Bram Moolenaar | 0ba0429 | 2010-07-14 23:23:17 +0200 | [diff] [blame] | 2 | |
| 3 | |
| 4 | VIM REFERENCE MANUAL by Luis Carvalho |
| 5 | |
| 6 | |
| 7 | The Lua Interface to Vim *lua* *Lua* |
| 8 | |
| 9 | 1. Commands |lua-commands| |
| 10 | 2. The vim module |lua-vim| |
Bram Moolenaar | 1dced57 | 2012-04-05 16:54:08 +0200 | [diff] [blame] | 11 | 3. List userdata |lua-list| |
| 12 | 4. Dict userdata |lua-dict| |
Bram Moolenaar | b782869 | 2019-03-23 13:57:02 +0100 | [diff] [blame] | 13 | 5. Blob userdata |lua-blob| |
| 14 | 6. Funcref userdata |lua-funcref| |
| 15 | 7. Buffer userdata |lua-buffer| |
| 16 | 8. Window userdata |lua-window| |
| 17 | 9. luaeval() Vim function |lua-luaeval| |
| 18 | 10. Dynamic loading |lua-dynamic| |
Bram Moolenaar | 0ba0429 | 2010-07-14 23:23:17 +0200 | [diff] [blame] | 19 | |
Bram Moolenaar | 25c9c68 | 2019-05-05 18:13:34 +0200 | [diff] [blame] | 20 | {only available when Vim was compiled with the |+lua| feature} |
Bram Moolenaar | 0ba0429 | 2010-07-14 23:23:17 +0200 | [diff] [blame] | 21 | |
| 22 | ============================================================================== |
| 23 | 1. Commands *lua-commands* |
| 24 | |
| 25 | *:lua* |
| 26 | :[range]lua {chunk} |
Bram Moolenaar | 25c9c68 | 2019-05-05 18:13:34 +0200 | [diff] [blame] | 27 | Execute Lua chunk {chunk}. |
Bram Moolenaar | 0ba0429 | 2010-07-14 23:23:17 +0200 | [diff] [blame] | 28 | |
| 29 | Examples: |
| 30 | > |
| 31 | :lua print("Hello, Vim!") |
| 32 | :lua local curbuf = vim.buffer() curbuf[7] = "line #7" |
| 33 | < |
| 34 | |
Bram Moolenaar | 6c2b7b8 | 2020-04-14 20:15:49 +0200 | [diff] [blame] | 35 | :[range]lua << [trim] [{endmarker}] |
Bram Moolenaar | 0ba0429 | 2010-07-14 23:23:17 +0200 | [diff] [blame] | 36 | {script} |
| 37 | {endmarker} |
Bram Moolenaar | 25c9c68 | 2019-05-05 18:13:34 +0200 | [diff] [blame] | 38 | Execute Lua script {script}. |
Bram Moolenaar | 0ba0429 | 2010-07-14 23:23:17 +0200 | [diff] [blame] | 39 | Note: This command doesn't work when the Lua |
| 40 | feature wasn't compiled in. To avoid errors, see |
| 41 | |script-here|. |
| 42 | |
Bram Moolenaar | 5477506 | 2019-07-31 21:07:14 +0200 | [diff] [blame] | 43 | If [endmarker] is omitted from after the "<<", a dot '.' must be used after |
Bram Moolenaar | 6c2b7b8 | 2020-04-14 20:15:49 +0200 | [diff] [blame] | 44 | {script}, like for the |:append| and |:insert| commands. Refer to |
| 45 | |:let-heredoc| for more information. |
Bram Moolenaar | 5477506 | 2019-07-31 21:07:14 +0200 | [diff] [blame] | 46 | |
Bram Moolenaar | 0ba0429 | 2010-07-14 23:23:17 +0200 | [diff] [blame] | 47 | This form of the |:lua| command is mainly useful for including Lua code |
| 48 | in Vim scripts. |
| 49 | |
| 50 | Example: |
| 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 Moolenaar | abd468e | 2016-09-08 22:22:43 +0200 | [diff] [blame] | 61 | To see what version of Lua you have: > |
| 62 | :lua print(_VERSION) |
| 63 | |
| 64 | If you use LuaJIT you can also use this: > |
| 65 | :lua print(jit.version) |
| 66 | < |
Bram Moolenaar | 0ba0429 | 2010-07-14 23:23:17 +0200 | [diff] [blame] | 67 | |
| 68 | *:luado* |
Bram Moolenaar | 53bfca2 | 2012-04-13 23:04:47 +0200 | [diff] [blame] | 69 | :[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 Moolenaar | 0ba0429 | 2010-07-14 23:23:17 +0200 | [diff] [blame] | 76 | |
| 77 | Examples: |
| 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 Moolenaar | 25c9c68 | 2019-05-05 18:13:34 +0200 | [diff] [blame] | 89 | Execute Lua script in {file}. |
Bram Moolenaar | 0ba0429 | 2010-07-14 23:23:17 +0200 | [diff] [blame] | 90 | The whole argument is used as a single file name. |
| 91 | |
| 92 | Examples: |
| 93 | > |
| 94 | :luafile script.lua |
| 95 | :luafile % |
| 96 | < |
| 97 | |
| 98 | All 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 |
| 100 | interpreter, each chunk has its own scope and so only global variables are |
Bram Moolenaar | 1dced57 | 2012-04-05 16:54:08 +0200 | [diff] [blame] | 101 | shared between command calls. All Lua default libraries are available. In |
| 102 | addition, Lua "print" function has its output redirected to the Vim message |
| 103 | area, with arguments separated by a white space instead of a tab. |
Bram Moolenaar | 0ba0429 | 2010-07-14 23:23:17 +0200 | [diff] [blame] | 104 | |
Bram Moolenaar | 9855d6b | 2010-07-18 14:34:51 +0200 | [diff] [blame] | 105 | Lua uses the "vim" module (see |lua-vim|) to issue commands to Vim |
Bram Moolenaar | 0ba0429 | 2010-07-14 23:23:17 +0200 | [diff] [blame] | 106 | and manage buffers (|lua-buffer|) and windows (|lua-window|). However, |
| 107 | procedures that alter buffer content, open new buffers, and change cursor |
Bram Moolenaar | 9855d6b | 2010-07-18 14:34:51 +0200 | [diff] [blame] | 108 | position are restricted when the command is executed in the |sandbox|. |
Bram Moolenaar | 0ba0429 | 2010-07-14 23:23:17 +0200 | [diff] [blame] | 109 | |
| 110 | |
| 111 | ============================================================================== |
| 112 | 2. The vim module *lua-vim* |
| 113 | |
| 114 | Lua interfaces Vim through the "vim" module. The first and last line of the |
Bram Moolenaar | 2334b6d | 2010-07-22 21:32:16 +0200 | [diff] [blame] | 115 | input range are stored in "vim.firstline" and "vim.lastline" respectively. The |
Bram Moolenaar | 0ba0429 | 2010-07-14 23:23:17 +0200 | [diff] [blame] | 116 | module also includes routines for buffer, window, and current line queries, |
| 117 | Vim evaluation and command execution, and others. |
| 118 | |
Bram Moolenaar | 52b91d8 | 2013-06-15 21:39:51 +0200 | [diff] [blame] | 119 | 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 Moolenaar | fd35811 | 2018-07-07 23:21:31 +0200 | [diff] [blame] | 126 | :lua t = {math.pi, false, say = 'hi'} |
| 127 | :echo luaeval('vim.list(t)') |
| 128 | :" [3.141593, v:false], 'say' is ignored |
Bram Moolenaar | 52b91d8 | 2013-06-15 21:39:51 +0200 | [diff] [blame] | 129 | < |
| 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 Moolenaar | fd35811 | 2018-07-07 23:21:31 +0200 | [diff] [blame] | 137 | :lua t = {math.pi, false, say = 'hi'} |
| 138 | :echo luaeval('vim.dict(t)') |
| 139 | :" {'1': 3.141593, '2': v:false, |
| 140 | :" 'say': 'hi'} |
Bram Moolenaar | 52b91d8 | 2013-06-15 21:39:51 +0200 | [diff] [blame] | 141 | < |
Bram Moolenaar | b782869 | 2019-03-23 13:57:02 +0100 | [diff] [blame] | 142 | 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 Moolenaar | 52b91d8 | 2013-06-15 21:39:51 +0200 | [diff] [blame] | 150 | vim.funcref({name}) Returns a Funcref to function {name} (see |
Bram Moolenaar | fd35811 | 2018-07-07 23:21:31 +0200 | [diff] [blame] | 151 | |Funcref|). It is equivalent to Vim's |
| 152 | function(). |
Bram Moolenaar | 0ba0429 | 2010-07-14 23:23:17 +0200 | [diff] [blame] | 153 | |
Bram Moolenaar | 2334b6d | 2010-07-22 21:32:16 +0200 | [diff] [blame] | 154 | 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 Moolenaar | 0ba0429 | 2010-07-14 23:23:17 +0200 | [diff] [blame] | 162 | |
Bram Moolenaar | 2334b6d | 2010-07-22 21:32:16 +0200 | [diff] [blame] | 163 | 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 Moolenaar | 0ba0429 | 2010-07-14 23:23:17 +0200 | [diff] [blame] | 168 | |
Bram Moolenaar | 1dced57 | 2012-04-05 16:54:08 +0200 | [diff] [blame] | 169 | vim.type({arg}) Returns the type of {arg}. It is equivalent to |
| 170 | Lua's "type" function, but returns "list", |
Bram Moolenaar | 52b91d8 | 2013-06-15 21:39:51 +0200 | [diff] [blame] | 171 | "dict", "funcref", "buffer", or "window" if |
| 172 | {arg} is a list, dictionary, funcref, buffer, |
| 173 | or window, respectively. Examples: > |
Bram Moolenaar | 1dced57 | 2012-04-05 16:54:08 +0200 | [diff] [blame] | 174 | :lua l = vim.list() |
| 175 | :lua print(type(l), vim.type(l)) |
Bram Moolenaar | 2f362bf | 2018-07-01 19:49:27 +0200 | [diff] [blame] | 176 | :" list |
Bram Moolenaar | 1dced57 | 2012-04-05 16:54:08 +0200 | [diff] [blame] | 177 | < |
Bram Moolenaar | 2334b6d | 2010-07-22 21:32:16 +0200 | [diff] [blame] | 178 | vim.command({cmd}) Executes the vim (ex-mode) command {cmd}. |
Bram Moolenaar | 0ba0429 | 2010-07-14 23:23:17 +0200 | [diff] [blame] | 179 | Examples: > |
| 180 | :lua vim.command"set tw=60" |
| 181 | :lua vim.command"normal ddp" |
| 182 | < |
Bram Moolenaar | 2334b6d | 2010-07-22 21:32:16 +0200 | [diff] [blame] | 183 | vim.eval({expr}) Evaluates expression {expr} (see |expression|), |
Bram Moolenaar | 0ba0429 | 2010-07-14 23:23:17 +0200 | [diff] [blame] | 184 | 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 Moolenaar | 1dced57 | 2012-04-05 16:54:08 +0200 | [diff] [blame] | 188 | userdata (see |lua-list| and |lua-dict|). |
Bram Moolenaar | 0ba0429 | 2010-07-14 23:23:17 +0200 | [diff] [blame] | 189 | Examples: > |
| 190 | :lua tw = vim.eval"&tw" |
| 191 | :lua print(vim.eval"{'a': 'one'}".a) |
| 192 | < |
Bram Moolenaar | 2334b6d | 2010-07-22 21:32:16 +0200 | [diff] [blame] | 193 | vim.line() Returns the current line (without the trailing |
Bram Moolenaar | 0ba0429 | 2010-07-14 23:23:17 +0200 | [diff] [blame] | 194 | <EOL>), a Lua string. |
| 195 | |
Bram Moolenaar | 2334b6d | 2010-07-22 21:32:16 +0200 | [diff] [blame] | 196 | vim.beep() Beeps. |
Bram Moolenaar | 0ba0429 | 2010-07-14 23:23:17 +0200 | [diff] [blame] | 197 | |
Bram Moolenaar | 2334b6d | 2010-07-22 21:32:16 +0200 | [diff] [blame] | 198 | vim.open({fname}) Opens a new buffer for file {fname} and |
Bram Moolenaar | 0ba0429 | 2010-07-14 23:23:17 +0200 | [diff] [blame] | 199 | returns it. Note that the buffer is not set as |
| 200 | current. |
| 201 | |
Bram Moolenaar | e7b1ea0 | 2020-08-07 19:54:59 +0200 | [diff] [blame] | 202 | vim.call({name} [, {args}]) |
| 203 | Proxy to call Vim function named {name} with |
Bram Moolenaar | eb04f08 | 2020-05-17 14:32:35 +0200 | [diff] [blame] | 204 | 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 Moolenaar | 0ba0429 | 2010-07-14 23:23:17 +0200 | [diff] [blame] | 211 | |
| 212 | ============================================================================== |
Bram Moolenaar | 1dced57 | 2012-04-05 16:54:08 +0200 | [diff] [blame] | 213 | 3. List userdata *lua-list* |
| 214 | |
| 215 | List userdata represent vim lists, and the interface tries to follow closely |
| 216 | Vim's syntax for lists. Since lists are objects, changes in list references in |
| 217 | Lua are reflected in Vim and vice-versa. A list "l" has the following |
| 218 | properties and methods: |
| 219 | |
Bram Moolenaar | bd84617 | 2020-06-27 12:32:57 +0200 | [diff] [blame] | 220 | NOTE: In patch 8.2.1066 array indexes were changed from zero-based to |
| 221 | one-based. You can check with: > |
| 222 | if has("patch-8.2.1066") |
| 223 | |
Bram Moolenaar | 1dced57 | 2012-04-05 16:54:08 +0200 | [diff] [blame] | 224 | Properties |
| 225 | ---------- |
| 226 | o "#l" is the number of items in list "l", equivalent to "len(l)" |
| 227 | in Vim. |
Bram Moolenaar | bd84617 | 2020-06-27 12:32:57 +0200 | [diff] [blame] | 228 | o "l[k]" returns the k-th item in "l"; "l" is one-indexed, as in Lua. |
Bram Moolenaar | 1dced57 | 2012-04-05 16:54:08 +0200 | [diff] [blame] | 229 | To modify the k-th item, simply do "l[k] = newitem"; in |
Bram Moolenaar | a1f9f86 | 2020-06-28 22:41:26 +0200 | [diff] [blame] | 230 | particular, "l[k] = nil" removes the k-th item from "l". Item can |
| 231 | be added to the end of the list by "l[#l + 1] = newitem" |
Bram Moolenaar | 1dced57 | 2012-04-05 16:54:08 +0200 | [diff] [blame] | 232 | o "l()" returns an iterator for "l". |
Bram Moolenaar | a1f9f86 | 2020-06-28 22:41:26 +0200 | [diff] [blame] | 233 | o "table.insert(l, newitem)" inserts an item at the end of the list. |
| 234 | (only Lua 5.3 and later) |
| 235 | o "table.insert(l, position, newitem)" inserts an item at the |
| 236 | specified position. "position" is one-indexed. (only Lua 5.3 and |
| 237 | later) |
| 238 | o "table.remove(l, position)" removes an item at the specified |
| 239 | position. "position" is one-indexed. |
| 240 | |
Bram Moolenaar | 1dced57 | 2012-04-05 16:54:08 +0200 | [diff] [blame] | 241 | |
| 242 | Methods |
| 243 | ------- |
| 244 | o "l:add(item)" appends "item" to the end of "l". |
| 245 | o "l:insert(item[, pos])" inserts "item" at (optional) |
| 246 | position "pos" in the list. The default value for "pos" is 0. |
| 247 | |
| 248 | Examples: |
| 249 | > |
| 250 | :let l = [1, 'item'] |
| 251 | :lua l = vim.eval('l') -- same 'l' |
| 252 | :lua l:add(vim.list()) |
Bram Moolenaar | bd84617 | 2020-06-27 12:32:57 +0200 | [diff] [blame] | 253 | :lua l[1] = math.pi |
Bram Moolenaar | 1dced57 | 2012-04-05 16:54:08 +0200 | [diff] [blame] | 254 | :echo l[0] " 3.141593 |
Bram Moolenaar | bd84617 | 2020-06-27 12:32:57 +0200 | [diff] [blame] | 255 | :lua l[1] = nil -- remove first item |
Bram Moolenaar | 1dced57 | 2012-04-05 16:54:08 +0200 | [diff] [blame] | 256 | :lua l:insert(true, 1) |
Bram Moolenaar | bd84617 | 2020-06-27 12:32:57 +0200 | [diff] [blame] | 257 | :lua print(l, #l, l[1], l[2]) |
Bram Moolenaar | a1f9f86 | 2020-06-28 22:41:26 +0200 | [diff] [blame] | 258 | :lua l[#l + 1] = 'value' |
| 259 | :lua table.insert(l, 100) |
| 260 | :lua table.insert(l, 2, 200) |
| 261 | :lua table.remove(l, 1) |
Bram Moolenaar | 1dced57 | 2012-04-05 16:54:08 +0200 | [diff] [blame] | 262 | :lua for item in l() do print(item) end |
Bram Moolenaar | 1dced57 | 2012-04-05 16:54:08 +0200 | [diff] [blame] | 263 | |
| 264 | ============================================================================== |
| 265 | 4. Dict userdata *lua-dict* |
| 266 | |
| 267 | Similarly to list userdata, dict userdata represent vim dictionaries; since |
| 268 | dictionaries are also objects, references are kept between Lua and Vim. A dict |
| 269 | "d" has the following properties: |
| 270 | |
| 271 | Properties |
| 272 | ---------- |
| 273 | o "#d" is the number of items in dict "d", equivalent to "len(d)" |
| 274 | in Vim. |
| 275 | o "d.key" or "d['key']" returns the value at entry "key" in "d". |
| 276 | To modify the entry at this key, simply do "d.key = newvalue"; in |
| 277 | particular, "d.key = nil" removes the entry from "d". |
| 278 | o "d()" returns an iterator for "d" and is equivalent to "items(d)" in |
| 279 | Vim. |
| 280 | |
| 281 | Examples: |
| 282 | > |
| 283 | :let d = {'n':10} |
| 284 | :lua d = vim.eval('d') -- same 'd' |
| 285 | :lua print(d, d.n, #d) |
| 286 | :let d.self = d |
| 287 | :lua for k, v in d() do print(d, k, v) end |
| 288 | :lua d.x = math.pi |
| 289 | :lua d.self = nil -- remove entry |
| 290 | :echo d |
| 291 | < |
| 292 | |
| 293 | ============================================================================== |
Bram Moolenaar | b782869 | 2019-03-23 13:57:02 +0100 | [diff] [blame] | 294 | 5. Blob userdata *lua-blob* |
| 295 | |
| 296 | Blob userdata represent vim blobs. A blob "b" has the following properties: |
| 297 | |
| 298 | Properties |
| 299 | ---------- |
| 300 | o "#b" is the length of blob "b", equivalent to "len(b)" in Vim. |
| 301 | o "b[k]" returns the k-th item in "b"; "b" is zero-indexed, as in Vim. |
| 302 | To modify the k-th item, simply do "b[k] = number"; in particular, |
| 303 | "b[#b] = number" can append a byte to tail. |
| 304 | |
| 305 | Methods |
| 306 | ------- |
| 307 | o "b:add(bytes)" appends "bytes" to the end of "b". |
| 308 | |
| 309 | Examples: |
| 310 | > |
| 311 | :let b = 0z001122 |
| 312 | :lua b = vim.eval('b') -- same 'b' |
| 313 | :lua print(b, b[0], #b) |
| 314 | :lua b[1] = 32 |
| 315 | :lua b[#b] = 0x33 -- append a byte to tail |
| 316 | :lua b:add("\x80\x81\xfe\xff") |
| 317 | :echo b |
| 318 | < |
| 319 | |
| 320 | ============================================================================== |
| 321 | 6. Funcref userdata *lua-funcref* |
Bram Moolenaar | 52b91d8 | 2013-06-15 21:39:51 +0200 | [diff] [blame] | 322 | |
| 323 | Funcref userdata represent funcref variables in Vim. Funcrefs that were |
| 324 | defined with a "dict" attribute need to be obtained as a dictionary key |
| 325 | in order to have "self" properly assigned to the dictionary (see examples |
| 326 | below.) A funcref "f" has the following properties: |
| 327 | |
| 328 | Properties |
| 329 | ---------- |
| 330 | o "#f" is the name of the function referenced by "f" |
| 331 | o "f(...)" calls the function referenced by "f" (with arguments) |
| 332 | |
| 333 | Examples: |
| 334 | > |
| 335 | :function I(x) |
| 336 | : return a:x |
| 337 | : endfunction |
| 338 | :let R = function('I') |
| 339 | :lua i1 = vim.funcref('I') |
| 340 | :lua i2 = vim.eval('R') |
| 341 | :lua print(#i1, #i2) -- both 'I' |
| 342 | :lua print(i1, i2, #i2(i1) == #i1(i2)) |
| 343 | :function Mylen() dict |
| 344 | : return len(self.data) |
| 345 | : endfunction |
| 346 | :let mydict = {'data': [0, 1, 2, 3]} |
| 347 | :lua d = vim.eval('mydict'); d.len = vim.funcref('Mylen') |
| 348 | :echo mydict.len() |
| 349 | :lua l = d.len -- assign d as 'self' |
| 350 | :lua print(l()) |
| 351 | < |
Bram Moolenaar | 801ab06 | 2020-06-25 19:27:56 +0200 | [diff] [blame] | 352 | Lua functions and closures are automatically converted to a Vim |Funcref| and |
| 353 | can be accessed in Vim scripts. Example: |
| 354 | > |
| 355 | lua <<EOF |
| 356 | vim.fn.timer_start(1000, function(timer) |
| 357 | print('timer callback') |
| 358 | end) |
| 359 | EOF |
Bram Moolenaar | 52b91d8 | 2013-06-15 21:39:51 +0200 | [diff] [blame] | 360 | |
| 361 | ============================================================================== |
Bram Moolenaar | b782869 | 2019-03-23 13:57:02 +0100 | [diff] [blame] | 362 | 7. Buffer userdata *lua-buffer* |
Bram Moolenaar | 0ba0429 | 2010-07-14 23:23:17 +0200 | [diff] [blame] | 363 | |
Bram Moolenaar | 2334b6d | 2010-07-22 21:32:16 +0200 | [diff] [blame] | 364 | Buffer userdata represent vim buffers. A buffer userdata "b" has the following |
Bram Moolenaar | 0ba0429 | 2010-07-14 23:23:17 +0200 | [diff] [blame] | 365 | properties and methods: |
| 366 | |
| 367 | Properties |
| 368 | ---------- |
Bram Moolenaar | 2334b6d | 2010-07-22 21:32:16 +0200 | [diff] [blame] | 369 | o "b()" sets "b" as the current buffer. |
| 370 | o "#b" is the number of lines in buffer "b". |
| 371 | o "b[k]" represents line number k: "b[k] = newline" replaces line k |
| 372 | with string "newline" and "b[k] = nil" deletes line k. |
| 373 | o "b.name" contains the short name of buffer "b" (read-only). |
| 374 | o "b.fname" contains the full name of buffer "b" (read-only). |
| 375 | o "b.number" contains the position of buffer "b" in the buffer list |
Bram Moolenaar | 0ba0429 | 2010-07-14 23:23:17 +0200 | [diff] [blame] | 376 | (read-only). |
| 377 | |
| 378 | Methods |
| 379 | ------- |
Bram Moolenaar | 2334b6d | 2010-07-22 21:32:16 +0200 | [diff] [blame] | 380 | o "b:insert(newline[, pos])" inserts string "newline" at (optional) |
| 381 | position "pos" in the buffer. The default value for "pos" is |
| 382 | "#b + 1". If "pos == 0" then "newline" becomes the first line in |
| 383 | the buffer. |
| 384 | o "b:next()" returns the buffer next to "b" in the buffer list. |
| 385 | o "b:previous()" returns the buffer previous to "b" in the buffer |
| 386 | list. |
| 387 | o "b:isvalid()" returns 'true' (boolean) if buffer "b" corresponds to |
| 388 | a "real" (not freed from memory) Vim buffer. |
Bram Moolenaar | 0ba0429 | 2010-07-14 23:23:17 +0200 | [diff] [blame] | 389 | |
| 390 | Examples: |
| 391 | > |
| 392 | :lua b = vim.buffer() -- current buffer |
| 393 | :lua print(b.name, b.number) |
| 394 | :lua b[1] = "first line" |
| 395 | :lua b:insert("FIRST!", 0) |
| 396 | :lua b[1] = nil -- delete top line |
| 397 | :lua for i=1,3 do b:insert(math.random()) end |
| 398 | :3,4lua for i=vim.lastline,vim.firstline,-1 do b[i] = nil end |
| 399 | :lua vim.open"myfile"() -- open buffer and set it as current |
| 400 | |
| 401 | function! ListBuffers() |
| 402 | lua << EOF |
| 403 | local b = vim.buffer(true) -- first buffer in list |
| 404 | while b ~= nil do |
| 405 | print(b.number, b.name, #b) |
| 406 | b = b:next() |
| 407 | end |
| 408 | vim.beep() |
| 409 | EOF |
| 410 | endfunction |
| 411 | < |
| 412 | |
| 413 | ============================================================================== |
Bram Moolenaar | b782869 | 2019-03-23 13:57:02 +0100 | [diff] [blame] | 414 | 8. Window userdata *lua-window* |
Bram Moolenaar | 0ba0429 | 2010-07-14 23:23:17 +0200 | [diff] [blame] | 415 | |
Bram Moolenaar | 2334b6d | 2010-07-22 21:32:16 +0200 | [diff] [blame] | 416 | Window objects represent vim windows. A window userdata "w" has the following |
Bram Moolenaar | 0ba0429 | 2010-07-14 23:23:17 +0200 | [diff] [blame] | 417 | properties and methods: |
| 418 | |
| 419 | Properties |
| 420 | ---------- |
Bram Moolenaar | 2334b6d | 2010-07-22 21:32:16 +0200 | [diff] [blame] | 421 | o "w()" sets "w" as the current window. |
| 422 | o "w.buffer" contains the buffer of window "w" (read-only). |
| 423 | o "w.line" represents the cursor line position in window "w". |
| 424 | o "w.col" represents the cursor column position in window "w". |
| 425 | o "w.width" represents the width of window "w". |
| 426 | o "w.height" represents the height of window "w". |
Bram Moolenaar | 0ba0429 | 2010-07-14 23:23:17 +0200 | [diff] [blame] | 427 | |
| 428 | Methods |
| 429 | ------- |
Bram Moolenaar | 2334b6d | 2010-07-22 21:32:16 +0200 | [diff] [blame] | 430 | o "w:next()" returns the window next to "w". |
| 431 | o "w:previous()" returns the window previous to "w". |
| 432 | o "w:isvalid()" returns 'true' (boolean) if window "w" corresponds to |
| 433 | a "real" (not freed from memory) Vim window. |
Bram Moolenaar | 0ba0429 | 2010-07-14 23:23:17 +0200 | [diff] [blame] | 434 | |
| 435 | Examples: |
| 436 | > |
| 437 | :lua w = vim.window() -- current window |
| 438 | :lua print(w.buffer.name, w.line, w.col) |
| 439 | :lua w.width = w.width + math.random(10) |
| 440 | :lua w.height = 2 * math.random() * w.height |
| 441 | :lua n,w = 0,vim.window(true) while w~=nil do n,w = n + 1,w:next() end |
| 442 | :lua print("There are " .. n .. " windows") |
| 443 | < |
| 444 | |
| 445 | ============================================================================== |
Bram Moolenaar | b782869 | 2019-03-23 13:57:02 +0100 | [diff] [blame] | 446 | 9. luaeval() Vim function *lua-luaeval* *lua-eval* |
Bram Moolenaar | 1dced57 | 2012-04-05 16:54:08 +0200 | [diff] [blame] | 447 | |
| 448 | The (dual) equivalent of "vim.eval" for passing Lua values to Vim is |
| 449 | "luaeval". "luaeval" takes an expression string and an optional argument and |
| 450 | returns the result of the expression. It is semantically equivalent in Lua to: |
| 451 | > |
| 452 | local chunkheader = "local _A = select(1, ...) return " |
| 453 | function luaeval (expstr, arg) |
| 454 | local chunk = assert(loadstring(chunkheader .. expstr, "luaeval")) |
| 455 | return chunk(arg) -- return typval |
| 456 | end |
| 457 | < |
Bram Moolenaar | 52b91d8 | 2013-06-15 21:39:51 +0200 | [diff] [blame] | 458 | Note that "_A" receives the argument to "luaeval". Lua numbers, strings, and |
Bram Moolenaar | b782869 | 2019-03-23 13:57:02 +0100 | [diff] [blame] | 459 | list, dict, blob, and funcref userdata are converted to their Vim respective |
| 460 | types, while Lua booleans are converted to numbers. An error is thrown if |
| 461 | conversion of any of the remaining Lua types, including userdata other than |
| 462 | lists, dicts, blobs, and funcrefs, is attempted. |
Bram Moolenaar | 52b91d8 | 2013-06-15 21:39:51 +0200 | [diff] [blame] | 463 | |
| 464 | Examples: > |
Bram Moolenaar | 1dced57 | 2012-04-05 16:54:08 +0200 | [diff] [blame] | 465 | |
| 466 | :echo luaeval('math.pi') |
| 467 | :lua a = vim.list():add('newlist') |
| 468 | :let a = luaeval('a') |
| 469 | :echo a[0] " 'newlist' |
| 470 | :function Rand(x,y) " random uniform between x and y |
| 471 | : return luaeval('(_A.y-_A.x)*math.random()+_A.x', {'x':a:x,'y':a:y}) |
| 472 | : endfunction |
| 473 | :echo Rand(1,10) |
| 474 | |
| 475 | |
| 476 | ============================================================================== |
Bram Moolenaar | b782869 | 2019-03-23 13:57:02 +0100 | [diff] [blame] | 477 | 10. Dynamic loading *lua-dynamic* |
Bram Moolenaar | d94464e | 2015-11-02 15:28:18 +0100 | [diff] [blame] | 478 | |
| 479 | On MS-Windows and Unix the Lua library can be loaded dynamically. The |
| 480 | |:version| output then includes |+lua/dyn|. |
| 481 | |
| 482 | This means that Vim will search for the Lua DLL or shared library file only |
| 483 | when needed. When you don't use the Lua interface you don't need it, thus |
| 484 | you can use Vim without this file. |
| 485 | |
Bram Moolenaar | d94464e | 2015-11-02 15:28:18 +0100 | [diff] [blame] | 486 | |
Bram Moolenaar | e18c0b3 | 2016-03-20 21:08:34 +0100 | [diff] [blame] | 487 | MS-Windows ~ |
| 488 | |
| 489 | To use the Lua interface the Lua DLL must be in your search path. In a |
| 490 | console window type "path" to see what directories are used. The 'luadll' |
| 491 | option can be also used to specify the Lua DLL. The version of the DLL must |
| 492 | match the Lua version Vim was compiled with. |
| 493 | |
| 494 | |
| 495 | Unix ~ |
| 496 | |
| 497 | The 'luadll' option can be used to specify the Lua shared library file instead |
| 498 | of DYNAMIC_LUA_DLL file what was specified at compile time. The version of |
| 499 | the shared library must match the Lua version Vim was compiled with. |
Bram Moolenaar | d94464e | 2015-11-02 15:28:18 +0100 | [diff] [blame] | 500 | |
| 501 | |
| 502 | ============================================================================== |
Bram Moolenaar | 1dced57 | 2012-04-05 16:54:08 +0200 | [diff] [blame] | 503 | vim:tw=78:ts=8:noet:ft=help:norl: |