Bram Moolenaar | 25c9c68 | 2019-05-05 18:13:34 +0200 | [diff] [blame] | 1 | *if_lua.txt* For Vim version 8.1. Last change: 2019 May 05 |
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 | |
| 35 | :[range]lua << {endmarker} |
| 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 | |
| 43 | {endmarker} must NOT be preceded by any white space. If {endmarker} is |
| 44 | omitted from after the "<<", a dot '.' must be used after {script}, like |
| 45 | for the |:append| and |:insert| commands. |
| 46 | This form of the |:lua| command is mainly useful for including Lua code |
| 47 | in Vim scripts. |
| 48 | |
| 49 | Example: |
| 50 | > |
| 51 | function! CurrentLineInfo() |
| 52 | lua << EOF |
| 53 | local linenr = vim.window().line |
| 54 | local curline = vim.buffer()[linenr] |
| 55 | print(string.format("Current line [%d] has %d chars", |
| 56 | linenr, #curline)) |
| 57 | EOF |
| 58 | endfunction |
| 59 | < |
Bram Moolenaar | abd468e | 2016-09-08 22:22:43 +0200 | [diff] [blame] | 60 | To see what version of Lua you have: > |
| 61 | :lua print(_VERSION) |
| 62 | |
| 63 | If you use LuaJIT you can also use this: > |
| 64 | :lua print(jit.version) |
| 65 | < |
Bram Moolenaar | 0ba0429 | 2010-07-14 23:23:17 +0200 | [diff] [blame] | 66 | |
| 67 | *:luado* |
Bram Moolenaar | 53bfca2 | 2012-04-13 23:04:47 +0200 | [diff] [blame] | 68 | :[range]luado {body} Execute Lua function "function (line, linenr) {body} |
| 69 | end" for each line in the [range], with the function |
| 70 | argument being set to the text of each line in turn, |
| 71 | without a trailing <EOL>, and the current line number. |
| 72 | If the value returned by the function is a string it |
| 73 | becomes the text of the line in the current turn. The |
| 74 | default for [range] is the whole file: "1,$". |
Bram Moolenaar | 0ba0429 | 2010-07-14 23:23:17 +0200 | [diff] [blame] | 75 | |
| 76 | Examples: |
| 77 | > |
| 78 | :luado return string.format("%s\t%d", line:reverse(), #line) |
| 79 | |
| 80 | :lua require"lpeg" |
| 81 | :lua -- balanced parenthesis grammar: |
| 82 | :lua bp = lpeg.P{ "(" * ((1 - lpeg.S"()") + lpeg.V(1))^0 * ")" } |
| 83 | :luado if bp:match(line) then return "-->\t" .. line end |
| 84 | < |
| 85 | |
| 86 | *:luafile* |
| 87 | :[range]luafile {file} |
Bram Moolenaar | 25c9c68 | 2019-05-05 18:13:34 +0200 | [diff] [blame] | 88 | Execute Lua script in {file}. |
Bram Moolenaar | 0ba0429 | 2010-07-14 23:23:17 +0200 | [diff] [blame] | 89 | The whole argument is used as a single file name. |
| 90 | |
| 91 | Examples: |
| 92 | > |
| 93 | :luafile script.lua |
| 94 | :luafile % |
| 95 | < |
| 96 | |
| 97 | All these commands execute a Lua chunk from either the command line (:lua and |
| 98 | :luado) or a file (:luafile) with the given line [range]. Similarly to the Lua |
| 99 | 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] | 100 | shared between command calls. All Lua default libraries are available. In |
| 101 | addition, Lua "print" function has its output redirected to the Vim message |
| 102 | area, with arguments separated by a white space instead of a tab. |
Bram Moolenaar | 0ba0429 | 2010-07-14 23:23:17 +0200 | [diff] [blame] | 103 | |
Bram Moolenaar | 9855d6b | 2010-07-18 14:34:51 +0200 | [diff] [blame] | 104 | 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] | 105 | and manage buffers (|lua-buffer|) and windows (|lua-window|). However, |
| 106 | procedures that alter buffer content, open new buffers, and change cursor |
Bram Moolenaar | 9855d6b | 2010-07-18 14:34:51 +0200 | [diff] [blame] | 107 | position are restricted when the command is executed in the |sandbox|. |
Bram Moolenaar | 0ba0429 | 2010-07-14 23:23:17 +0200 | [diff] [blame] | 108 | |
| 109 | |
| 110 | ============================================================================== |
| 111 | 2. The vim module *lua-vim* |
| 112 | |
| 113 | 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] | 114 | input range are stored in "vim.firstline" and "vim.lastline" respectively. The |
Bram Moolenaar | 0ba0429 | 2010-07-14 23:23:17 +0200 | [diff] [blame] | 115 | module also includes routines for buffer, window, and current line queries, |
| 116 | Vim evaluation and command execution, and others. |
| 117 | |
Bram Moolenaar | 52b91d8 | 2013-06-15 21:39:51 +0200 | [diff] [blame] | 118 | vim.list([arg]) Returns an empty list or, if "arg" is a Lua |
| 119 | table with numeric keys 1, ..., n (a |
| 120 | "sequence"), returns a list l such that l[i] = |
| 121 | arg[i] for i = 1, ..., n (see |List|). |
| 122 | Non-numeric keys are not used to initialize |
| 123 | the list. See also |lua-eval| for conversion |
| 124 | rules. Example: > |
Bram Moolenaar | fd35811 | 2018-07-07 23:21:31 +0200 | [diff] [blame] | 125 | :lua t = {math.pi, false, say = 'hi'} |
| 126 | :echo luaeval('vim.list(t)') |
| 127 | :" [3.141593, v:false], 'say' is ignored |
Bram Moolenaar | 52b91d8 | 2013-06-15 21:39:51 +0200 | [diff] [blame] | 128 | < |
| 129 | vim.dict([arg]) Returns an empty dictionary or, if "arg" is a |
| 130 | Lua table, returns a dict d such that d[k] = |
| 131 | arg[k] for all string keys k in "arg" (see |
| 132 | |Dictionary|). Number keys are converted to |
| 133 | strings. Keys that are not strings are not |
| 134 | used to initialize the dictionary. See also |
| 135 | |lua-eval| for conversion rules. Example: > |
Bram Moolenaar | fd35811 | 2018-07-07 23:21:31 +0200 | [diff] [blame] | 136 | :lua t = {math.pi, false, say = 'hi'} |
| 137 | :echo luaeval('vim.dict(t)') |
| 138 | :" {'1': 3.141593, '2': v:false, |
| 139 | :" 'say': 'hi'} |
Bram Moolenaar | 52b91d8 | 2013-06-15 21:39:51 +0200 | [diff] [blame] | 140 | < |
Bram Moolenaar | b782869 | 2019-03-23 13:57:02 +0100 | [diff] [blame] | 141 | vim.blob([arg]) Returns an empty blob or, if "arg" is a Lua |
| 142 | string, returns a blob b such that b is |
| 143 | equivalent to "arg" as a byte string. |
| 144 | Examples: > |
| 145 | :lua s = "12ab\x00\x80\xfe\xff" |
| 146 | :echo luaeval('vim.blob(s)') |
| 147 | :" 0z31326162.0080FEFF |
| 148 | < |
Bram Moolenaar | 52b91d8 | 2013-06-15 21:39:51 +0200 | [diff] [blame] | 149 | vim.funcref({name}) Returns a Funcref to function {name} (see |
Bram Moolenaar | fd35811 | 2018-07-07 23:21:31 +0200 | [diff] [blame] | 150 | |Funcref|). It is equivalent to Vim's |
| 151 | function(). |
Bram Moolenaar | 0ba0429 | 2010-07-14 23:23:17 +0200 | [diff] [blame] | 152 | |
Bram Moolenaar | 2334b6d | 2010-07-22 21:32:16 +0200 | [diff] [blame] | 153 | vim.buffer([arg]) If "arg" is a number, returns buffer with |
| 154 | number "arg" in the buffer list or, if "arg" |
| 155 | is a string, returns buffer whose full or short |
| 156 | name is "arg". In both cases, returns 'nil' |
| 157 | (nil value, not string) if the buffer is not |
| 158 | found. Otherwise, if "toboolean(arg)" is |
| 159 | 'true' returns the first buffer in the buffer |
| 160 | list or else the current buffer. |
Bram Moolenaar | 0ba0429 | 2010-07-14 23:23:17 +0200 | [diff] [blame] | 161 | |
Bram Moolenaar | 2334b6d | 2010-07-22 21:32:16 +0200 | [diff] [blame] | 162 | vim.window([arg]) If "arg" is a number, returns window with |
| 163 | number "arg" or 'nil' (nil value, not string) |
| 164 | if not found. Otherwise, if "toboolean(arg)" |
| 165 | is 'true' returns the first window or else the |
| 166 | current window. |
Bram Moolenaar | 0ba0429 | 2010-07-14 23:23:17 +0200 | [diff] [blame] | 167 | |
Bram Moolenaar | 1dced57 | 2012-04-05 16:54:08 +0200 | [diff] [blame] | 168 | vim.type({arg}) Returns the type of {arg}. It is equivalent to |
| 169 | Lua's "type" function, but returns "list", |
Bram Moolenaar | 52b91d8 | 2013-06-15 21:39:51 +0200 | [diff] [blame] | 170 | "dict", "funcref", "buffer", or "window" if |
| 171 | {arg} is a list, dictionary, funcref, buffer, |
| 172 | or window, respectively. Examples: > |
Bram Moolenaar | 1dced57 | 2012-04-05 16:54:08 +0200 | [diff] [blame] | 173 | :lua l = vim.list() |
| 174 | :lua print(type(l), vim.type(l)) |
Bram Moolenaar | 2f362bf | 2018-07-01 19:49:27 +0200 | [diff] [blame] | 175 | :" list |
Bram Moolenaar | 1dced57 | 2012-04-05 16:54:08 +0200 | [diff] [blame] | 176 | < |
Bram Moolenaar | 2334b6d | 2010-07-22 21:32:16 +0200 | [diff] [blame] | 177 | vim.command({cmd}) Executes the vim (ex-mode) command {cmd}. |
Bram Moolenaar | 0ba0429 | 2010-07-14 23:23:17 +0200 | [diff] [blame] | 178 | Examples: > |
| 179 | :lua vim.command"set tw=60" |
| 180 | :lua vim.command"normal ddp" |
| 181 | < |
Bram Moolenaar | 2334b6d | 2010-07-22 21:32:16 +0200 | [diff] [blame] | 182 | vim.eval({expr}) Evaluates expression {expr} (see |expression|), |
Bram Moolenaar | 0ba0429 | 2010-07-14 23:23:17 +0200 | [diff] [blame] | 183 | converts the result to Lua, and returns it. |
| 184 | Vim strings and numbers are directly converted |
| 185 | to Lua strings and numbers respectively. Vim |
| 186 | lists and dictionaries are converted to Lua |
Bram Moolenaar | 1dced57 | 2012-04-05 16:54:08 +0200 | [diff] [blame] | 187 | userdata (see |lua-list| and |lua-dict|). |
Bram Moolenaar | 0ba0429 | 2010-07-14 23:23:17 +0200 | [diff] [blame] | 188 | Examples: > |
| 189 | :lua tw = vim.eval"&tw" |
| 190 | :lua print(vim.eval"{'a': 'one'}".a) |
| 191 | < |
Bram Moolenaar | 2334b6d | 2010-07-22 21:32:16 +0200 | [diff] [blame] | 192 | vim.line() Returns the current line (without the trailing |
Bram Moolenaar | 0ba0429 | 2010-07-14 23:23:17 +0200 | [diff] [blame] | 193 | <EOL>), a Lua string. |
| 194 | |
Bram Moolenaar | 2334b6d | 2010-07-22 21:32:16 +0200 | [diff] [blame] | 195 | vim.beep() Beeps. |
Bram Moolenaar | 0ba0429 | 2010-07-14 23:23:17 +0200 | [diff] [blame] | 196 | |
Bram Moolenaar | 2334b6d | 2010-07-22 21:32:16 +0200 | [diff] [blame] | 197 | vim.open({fname}) Opens a new buffer for file {fname} and |
Bram Moolenaar | 0ba0429 | 2010-07-14 23:23:17 +0200 | [diff] [blame] | 198 | returns it. Note that the buffer is not set as |
| 199 | current. |
| 200 | |
| 201 | |
| 202 | ============================================================================== |
Bram Moolenaar | 1dced57 | 2012-04-05 16:54:08 +0200 | [diff] [blame] | 203 | 3. List userdata *lua-list* |
| 204 | |
| 205 | List userdata represent vim lists, and the interface tries to follow closely |
| 206 | Vim's syntax for lists. Since lists are objects, changes in list references in |
| 207 | Lua are reflected in Vim and vice-versa. A list "l" has the following |
| 208 | properties and methods: |
| 209 | |
| 210 | Properties |
| 211 | ---------- |
| 212 | o "#l" is the number of items in list "l", equivalent to "len(l)" |
| 213 | in Vim. |
| 214 | o "l[k]" returns the k-th item in "l"; "l" is zero-indexed, as in Vim. |
| 215 | To modify the k-th item, simply do "l[k] = newitem"; in |
| 216 | particular, "l[k] = nil" removes the k-th item from "l". |
| 217 | o "l()" returns an iterator for "l". |
| 218 | |
| 219 | Methods |
| 220 | ------- |
| 221 | o "l:add(item)" appends "item" to the end of "l". |
| 222 | o "l:insert(item[, pos])" inserts "item" at (optional) |
| 223 | position "pos" in the list. The default value for "pos" is 0. |
| 224 | |
| 225 | Examples: |
| 226 | > |
| 227 | :let l = [1, 'item'] |
| 228 | :lua l = vim.eval('l') -- same 'l' |
| 229 | :lua l:add(vim.list()) |
| 230 | :lua l[0] = math.pi |
| 231 | :echo l[0] " 3.141593 |
| 232 | :lua l[0] = nil -- remove first item |
| 233 | :lua l:insert(true, 1) |
| 234 | :lua print(l, #l, l[0], l[1], l[-1]) |
| 235 | :lua for item in l() do print(item) end |
| 236 | < |
| 237 | |
| 238 | ============================================================================== |
| 239 | 4. Dict userdata *lua-dict* |
| 240 | |
| 241 | Similarly to list userdata, dict userdata represent vim dictionaries; since |
| 242 | dictionaries are also objects, references are kept between Lua and Vim. A dict |
| 243 | "d" has the following properties: |
| 244 | |
| 245 | Properties |
| 246 | ---------- |
| 247 | o "#d" is the number of items in dict "d", equivalent to "len(d)" |
| 248 | in Vim. |
| 249 | o "d.key" or "d['key']" returns the value at entry "key" in "d". |
| 250 | To modify the entry at this key, simply do "d.key = newvalue"; in |
| 251 | particular, "d.key = nil" removes the entry from "d". |
| 252 | o "d()" returns an iterator for "d" and is equivalent to "items(d)" in |
| 253 | Vim. |
| 254 | |
| 255 | Examples: |
| 256 | > |
| 257 | :let d = {'n':10} |
| 258 | :lua d = vim.eval('d') -- same 'd' |
| 259 | :lua print(d, d.n, #d) |
| 260 | :let d.self = d |
| 261 | :lua for k, v in d() do print(d, k, v) end |
| 262 | :lua d.x = math.pi |
| 263 | :lua d.self = nil -- remove entry |
| 264 | :echo d |
| 265 | < |
| 266 | |
| 267 | ============================================================================== |
Bram Moolenaar | b782869 | 2019-03-23 13:57:02 +0100 | [diff] [blame] | 268 | 5. Blob userdata *lua-blob* |
| 269 | |
| 270 | Blob userdata represent vim blobs. A blob "b" has the following properties: |
| 271 | |
| 272 | Properties |
| 273 | ---------- |
| 274 | o "#b" is the length of blob "b", equivalent to "len(b)" in Vim. |
| 275 | o "b[k]" returns the k-th item in "b"; "b" is zero-indexed, as in Vim. |
| 276 | To modify the k-th item, simply do "b[k] = number"; in particular, |
| 277 | "b[#b] = number" can append a byte to tail. |
| 278 | |
| 279 | Methods |
| 280 | ------- |
| 281 | o "b:add(bytes)" appends "bytes" to the end of "b". |
| 282 | |
| 283 | Examples: |
| 284 | > |
| 285 | :let b = 0z001122 |
| 286 | :lua b = vim.eval('b') -- same 'b' |
| 287 | :lua print(b, b[0], #b) |
| 288 | :lua b[1] = 32 |
| 289 | :lua b[#b] = 0x33 -- append a byte to tail |
| 290 | :lua b:add("\x80\x81\xfe\xff") |
| 291 | :echo b |
| 292 | < |
| 293 | |
| 294 | ============================================================================== |
| 295 | 6. Funcref userdata *lua-funcref* |
Bram Moolenaar | 52b91d8 | 2013-06-15 21:39:51 +0200 | [diff] [blame] | 296 | |
| 297 | Funcref userdata represent funcref variables in Vim. Funcrefs that were |
| 298 | defined with a "dict" attribute need to be obtained as a dictionary key |
| 299 | in order to have "self" properly assigned to the dictionary (see examples |
| 300 | below.) A funcref "f" has the following properties: |
| 301 | |
| 302 | Properties |
| 303 | ---------- |
| 304 | o "#f" is the name of the function referenced by "f" |
| 305 | o "f(...)" calls the function referenced by "f" (with arguments) |
| 306 | |
| 307 | Examples: |
| 308 | > |
| 309 | :function I(x) |
| 310 | : return a:x |
| 311 | : endfunction |
| 312 | :let R = function('I') |
| 313 | :lua i1 = vim.funcref('I') |
| 314 | :lua i2 = vim.eval('R') |
| 315 | :lua print(#i1, #i2) -- both 'I' |
| 316 | :lua print(i1, i2, #i2(i1) == #i1(i2)) |
| 317 | :function Mylen() dict |
| 318 | : return len(self.data) |
| 319 | : endfunction |
| 320 | :let mydict = {'data': [0, 1, 2, 3]} |
| 321 | :lua d = vim.eval('mydict'); d.len = vim.funcref('Mylen') |
| 322 | :echo mydict.len() |
| 323 | :lua l = d.len -- assign d as 'self' |
| 324 | :lua print(l()) |
| 325 | < |
| 326 | |
| 327 | ============================================================================== |
Bram Moolenaar | b782869 | 2019-03-23 13:57:02 +0100 | [diff] [blame] | 328 | 7. Buffer userdata *lua-buffer* |
Bram Moolenaar | 0ba0429 | 2010-07-14 23:23:17 +0200 | [diff] [blame] | 329 | |
Bram Moolenaar | 2334b6d | 2010-07-22 21:32:16 +0200 | [diff] [blame] | 330 | Buffer userdata represent vim buffers. A buffer userdata "b" has the following |
Bram Moolenaar | 0ba0429 | 2010-07-14 23:23:17 +0200 | [diff] [blame] | 331 | properties and methods: |
| 332 | |
| 333 | Properties |
| 334 | ---------- |
Bram Moolenaar | 2334b6d | 2010-07-22 21:32:16 +0200 | [diff] [blame] | 335 | o "b()" sets "b" as the current buffer. |
| 336 | o "#b" is the number of lines in buffer "b". |
| 337 | o "b[k]" represents line number k: "b[k] = newline" replaces line k |
| 338 | with string "newline" and "b[k] = nil" deletes line k. |
| 339 | o "b.name" contains the short name of buffer "b" (read-only). |
| 340 | o "b.fname" contains the full name of buffer "b" (read-only). |
| 341 | 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] | 342 | (read-only). |
| 343 | |
| 344 | Methods |
| 345 | ------- |
Bram Moolenaar | 2334b6d | 2010-07-22 21:32:16 +0200 | [diff] [blame] | 346 | o "b:insert(newline[, pos])" inserts string "newline" at (optional) |
| 347 | position "pos" in the buffer. The default value for "pos" is |
| 348 | "#b + 1". If "pos == 0" then "newline" becomes the first line in |
| 349 | the buffer. |
| 350 | o "b:next()" returns the buffer next to "b" in the buffer list. |
| 351 | o "b:previous()" returns the buffer previous to "b" in the buffer |
| 352 | list. |
| 353 | o "b:isvalid()" returns 'true' (boolean) if buffer "b" corresponds to |
| 354 | a "real" (not freed from memory) Vim buffer. |
Bram Moolenaar | 0ba0429 | 2010-07-14 23:23:17 +0200 | [diff] [blame] | 355 | |
| 356 | Examples: |
| 357 | > |
| 358 | :lua b = vim.buffer() -- current buffer |
| 359 | :lua print(b.name, b.number) |
| 360 | :lua b[1] = "first line" |
| 361 | :lua b:insert("FIRST!", 0) |
| 362 | :lua b[1] = nil -- delete top line |
| 363 | :lua for i=1,3 do b:insert(math.random()) end |
| 364 | :3,4lua for i=vim.lastline,vim.firstline,-1 do b[i] = nil end |
| 365 | :lua vim.open"myfile"() -- open buffer and set it as current |
| 366 | |
| 367 | function! ListBuffers() |
| 368 | lua << EOF |
| 369 | local b = vim.buffer(true) -- first buffer in list |
| 370 | while b ~= nil do |
| 371 | print(b.number, b.name, #b) |
| 372 | b = b:next() |
| 373 | end |
| 374 | vim.beep() |
| 375 | EOF |
| 376 | endfunction |
| 377 | < |
| 378 | |
| 379 | ============================================================================== |
Bram Moolenaar | b782869 | 2019-03-23 13:57:02 +0100 | [diff] [blame] | 380 | 8. Window userdata *lua-window* |
Bram Moolenaar | 0ba0429 | 2010-07-14 23:23:17 +0200 | [diff] [blame] | 381 | |
Bram Moolenaar | 2334b6d | 2010-07-22 21:32:16 +0200 | [diff] [blame] | 382 | Window objects represent vim windows. A window userdata "w" has the following |
Bram Moolenaar | 0ba0429 | 2010-07-14 23:23:17 +0200 | [diff] [blame] | 383 | properties and methods: |
| 384 | |
| 385 | Properties |
| 386 | ---------- |
Bram Moolenaar | 2334b6d | 2010-07-22 21:32:16 +0200 | [diff] [blame] | 387 | o "w()" sets "w" as the current window. |
| 388 | o "w.buffer" contains the buffer of window "w" (read-only). |
| 389 | o "w.line" represents the cursor line position in window "w". |
| 390 | o "w.col" represents the cursor column position in window "w". |
| 391 | o "w.width" represents the width of window "w". |
| 392 | o "w.height" represents the height of window "w". |
Bram Moolenaar | 0ba0429 | 2010-07-14 23:23:17 +0200 | [diff] [blame] | 393 | |
| 394 | Methods |
| 395 | ------- |
Bram Moolenaar | 2334b6d | 2010-07-22 21:32:16 +0200 | [diff] [blame] | 396 | o "w:next()" returns the window next to "w". |
| 397 | o "w:previous()" returns the window previous to "w". |
| 398 | o "w:isvalid()" returns 'true' (boolean) if window "w" corresponds to |
| 399 | a "real" (not freed from memory) Vim window. |
Bram Moolenaar | 0ba0429 | 2010-07-14 23:23:17 +0200 | [diff] [blame] | 400 | |
| 401 | Examples: |
| 402 | > |
| 403 | :lua w = vim.window() -- current window |
| 404 | :lua print(w.buffer.name, w.line, w.col) |
| 405 | :lua w.width = w.width + math.random(10) |
| 406 | :lua w.height = 2 * math.random() * w.height |
| 407 | :lua n,w = 0,vim.window(true) while w~=nil do n,w = n + 1,w:next() end |
| 408 | :lua print("There are " .. n .. " windows") |
| 409 | < |
| 410 | |
| 411 | ============================================================================== |
Bram Moolenaar | b782869 | 2019-03-23 13:57:02 +0100 | [diff] [blame] | 412 | 9. luaeval() Vim function *lua-luaeval* *lua-eval* |
Bram Moolenaar | 1dced57 | 2012-04-05 16:54:08 +0200 | [diff] [blame] | 413 | |
| 414 | The (dual) equivalent of "vim.eval" for passing Lua values to Vim is |
| 415 | "luaeval". "luaeval" takes an expression string and an optional argument and |
| 416 | returns the result of the expression. It is semantically equivalent in Lua to: |
| 417 | > |
| 418 | local chunkheader = "local _A = select(1, ...) return " |
| 419 | function luaeval (expstr, arg) |
| 420 | local chunk = assert(loadstring(chunkheader .. expstr, "luaeval")) |
| 421 | return chunk(arg) -- return typval |
| 422 | end |
| 423 | < |
Bram Moolenaar | 52b91d8 | 2013-06-15 21:39:51 +0200 | [diff] [blame] | 424 | Note that "_A" receives the argument to "luaeval". Lua numbers, strings, and |
Bram Moolenaar | b782869 | 2019-03-23 13:57:02 +0100 | [diff] [blame] | 425 | list, dict, blob, and funcref userdata are converted to their Vim respective |
| 426 | types, while Lua booleans are converted to numbers. An error is thrown if |
| 427 | conversion of any of the remaining Lua types, including userdata other than |
| 428 | lists, dicts, blobs, and funcrefs, is attempted. |
Bram Moolenaar | 52b91d8 | 2013-06-15 21:39:51 +0200 | [diff] [blame] | 429 | |
| 430 | Examples: > |
Bram Moolenaar | 1dced57 | 2012-04-05 16:54:08 +0200 | [diff] [blame] | 431 | |
| 432 | :echo luaeval('math.pi') |
| 433 | :lua a = vim.list():add('newlist') |
| 434 | :let a = luaeval('a') |
| 435 | :echo a[0] " 'newlist' |
| 436 | :function Rand(x,y) " random uniform between x and y |
| 437 | : return luaeval('(_A.y-_A.x)*math.random()+_A.x', {'x':a:x,'y':a:y}) |
| 438 | : endfunction |
| 439 | :echo Rand(1,10) |
| 440 | |
| 441 | |
| 442 | ============================================================================== |
Bram Moolenaar | b782869 | 2019-03-23 13:57:02 +0100 | [diff] [blame] | 443 | 10. Dynamic loading *lua-dynamic* |
Bram Moolenaar | d94464e | 2015-11-02 15:28:18 +0100 | [diff] [blame] | 444 | |
| 445 | On MS-Windows and Unix the Lua library can be loaded dynamically. The |
| 446 | |:version| output then includes |+lua/dyn|. |
| 447 | |
| 448 | This means that Vim will search for the Lua DLL or shared library file only |
| 449 | when needed. When you don't use the Lua interface you don't need it, thus |
| 450 | you can use Vim without this file. |
| 451 | |
Bram Moolenaar | d94464e | 2015-11-02 15:28:18 +0100 | [diff] [blame] | 452 | |
Bram Moolenaar | e18c0b3 | 2016-03-20 21:08:34 +0100 | [diff] [blame] | 453 | MS-Windows ~ |
| 454 | |
| 455 | To use the Lua interface the Lua DLL must be in your search path. In a |
| 456 | console window type "path" to see what directories are used. The 'luadll' |
| 457 | option can be also used to specify the Lua DLL. The version of the DLL must |
| 458 | match the Lua version Vim was compiled with. |
| 459 | |
| 460 | |
| 461 | Unix ~ |
| 462 | |
| 463 | The 'luadll' option can be used to specify the Lua shared library file instead |
| 464 | of DYNAMIC_LUA_DLL file what was specified at compile time. The version of |
| 465 | the shared library must match the Lua version Vim was compiled with. |
Bram Moolenaar | d94464e | 2015-11-02 15:28:18 +0100 | [diff] [blame] | 466 | |
| 467 | |
| 468 | ============================================================================== |
Bram Moolenaar | 1dced57 | 2012-04-05 16:54:08 +0200 | [diff] [blame] | 469 | vim:tw=78:ts=8:noet:ft=help:norl: |