blob: 6626e383eabc5bfe04c2f1bf4ebf156276553607 [file] [log] [blame]
Bram Moolenaar1dced572012-04-05 16:54:08 +02001*if_lua.txt* For Vim version 7.3. Last change: 2012 Jan 16
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|
135. Buffer userdata |lua-buffer|
146. Window userdata |lua-window|
157. The luaeval function |lua-luaeval|
Bram Moolenaar0ba04292010-07-14 23:23:17 +020016
17{Vi does not have any of these commands}
18
19The Lua interface is available only when Vim was compiled with the
20|+lua| feature.
21
22==============================================================================
231. Commands *lua-commands*
24
25 *:lua*
26:[range]lua {chunk}
27 Execute Lua chunk {chunk}. {not in Vi}
28
29Examples:
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}
38 Execute Lua script {script}. {not in Vi}
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
44omitted from after the "<<", a dot '.' must be used after {script}, like
45for the |:append| and |:insert| commands.
46This form of the |:lua| command is mainly useful for including Lua code
47in Vim scripts.
48
49Example:
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<
60
61 *:luado*
Bram Moolenaar53bfca22012-04-13 23:04:47 +020062:[range]luado {body} Execute Lua function "function (line, linenr) {body}
63 end" for each line in the [range], with the function
64 argument being set to the text of each line in turn,
65 without a trailing <EOL>, and the current line number.
66 If the value returned by the function is a string it
67 becomes the text of the line in the current turn. The
68 default for [range] is the whole file: "1,$".
69 {not in Vi}
Bram Moolenaar0ba04292010-07-14 23:23:17 +020070
71Examples:
72>
73 :luado return string.format("%s\t%d", line:reverse(), #line)
74
75 :lua require"lpeg"
76 :lua -- balanced parenthesis grammar:
77 :lua bp = lpeg.P{ "(" * ((1 - lpeg.S"()") + lpeg.V(1))^0 * ")" }
78 :luado if bp:match(line) then return "-->\t" .. line end
79<
80
81 *:luafile*
82:[range]luafile {file}
83 Execute Lua script in {file}. {not in Vi}
84 The whole argument is used as a single file name.
85
86Examples:
87>
88 :luafile script.lua
89 :luafile %
90<
91
92All these commands execute a Lua chunk from either the command line (:lua and
93:luado) or a file (:luafile) with the given line [range]. Similarly to the Lua
94interpreter, each chunk has its own scope and so only global variables are
Bram Moolenaar1dced572012-04-05 16:54:08 +020095shared between command calls. All Lua default libraries are available. In
96addition, Lua "print" function has its output redirected to the Vim message
97area, with arguments separated by a white space instead of a tab.
Bram Moolenaar0ba04292010-07-14 23:23:17 +020098
Bram Moolenaar9855d6b2010-07-18 14:34:51 +020099Lua uses the "vim" module (see |lua-vim|) to issue commands to Vim
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200100and manage buffers (|lua-buffer|) and windows (|lua-window|). However,
101procedures that alter buffer content, open new buffers, and change cursor
Bram Moolenaar9855d6b2010-07-18 14:34:51 +0200102position are restricted when the command is executed in the |sandbox|.
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200103
104
105==============================================================================
1062. The vim module *lua-vim*
107
108Lua interfaces Vim through the "vim" module. The first and last line of the
Bram Moolenaar2334b6d2010-07-22 21:32:16 +0200109input range are stored in "vim.firstline" and "vim.lastline" respectively. The
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200110module also includes routines for buffer, window, and current line queries,
111Vim evaluation and command execution, and others.
112
Bram Moolenaar1dced572012-04-05 16:54:08 +0200113 vim.list() Returns an empty list (see |List|).
114
115 vim.dict() Returns an empty dictionary (see |Dictionary|).
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200116
Bram Moolenaar2334b6d2010-07-22 21:32:16 +0200117 vim.buffer([arg]) If "arg" is a number, returns buffer with
118 number "arg" in the buffer list or, if "arg"
119 is a string, returns buffer whose full or short
120 name is "arg". In both cases, returns 'nil'
121 (nil value, not string) if the buffer is not
122 found. Otherwise, if "toboolean(arg)" is
123 'true' returns the first buffer in the buffer
124 list or else the current buffer.
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200125
Bram Moolenaar2334b6d2010-07-22 21:32:16 +0200126 vim.window([arg]) If "arg" is a number, returns window with
127 number "arg" or 'nil' (nil value, not string)
128 if not found. Otherwise, if "toboolean(arg)"
129 is 'true' returns the first window or else the
130 current window.
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200131
Bram Moolenaar1dced572012-04-05 16:54:08 +0200132 vim.type({arg}) Returns the type of {arg}. It is equivalent to
133 Lua's "type" function, but returns "list",
134 "dict", "buffer", or "window" if {arg} is a
135 list, dictionary, buffer, or window,
136 respectively. Examples: >
137 :lua l = vim.list()
138 :lua print(type(l), vim.type(l))
139 :" userdata list
140<
Bram Moolenaar2334b6d2010-07-22 21:32:16 +0200141 vim.command({cmd}) Executes the vim (ex-mode) command {cmd}.
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200142 Examples: >
143 :lua vim.command"set tw=60"
144 :lua vim.command"normal ddp"
145<
Bram Moolenaar2334b6d2010-07-22 21:32:16 +0200146 vim.eval({expr}) Evaluates expression {expr} (see |expression|),
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200147 converts the result to Lua, and returns it.
148 Vim strings and numbers are directly converted
149 to Lua strings and numbers respectively. Vim
150 lists and dictionaries are converted to Lua
Bram Moolenaar1dced572012-04-05 16:54:08 +0200151 userdata (see |lua-list| and |lua-dict|).
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200152 Examples: >
153 :lua tw = vim.eval"&tw"
154 :lua print(vim.eval"{'a': 'one'}".a)
155<
Bram Moolenaar2334b6d2010-07-22 21:32:16 +0200156 vim.line() Returns the current line (without the trailing
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200157 <EOL>), a Lua string.
158
Bram Moolenaar2334b6d2010-07-22 21:32:16 +0200159 vim.beep() Beeps.
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200160
Bram Moolenaar2334b6d2010-07-22 21:32:16 +0200161 vim.open({fname}) Opens a new buffer for file {fname} and
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200162 returns it. Note that the buffer is not set as
163 current.
164
165
166==============================================================================
Bram Moolenaar1dced572012-04-05 16:54:08 +02001673. List userdata *lua-list*
168
169List userdata represent vim lists, and the interface tries to follow closely
170Vim's syntax for lists. Since lists are objects, changes in list references in
171Lua are reflected in Vim and vice-versa. A list "l" has the following
172properties and methods:
173
174Properties
175----------
176 o "#l" is the number of items in list "l", equivalent to "len(l)"
177 in Vim.
178 o "l[k]" returns the k-th item in "l"; "l" is zero-indexed, as in Vim.
179 To modify the k-th item, simply do "l[k] = newitem"; in
180 particular, "l[k] = nil" removes the k-th item from "l".
181 o "l()" returns an iterator for "l".
182
183Methods
184-------
185 o "l:add(item)" appends "item" to the end of "l".
186 o "l:insert(item[, pos])" inserts "item" at (optional)
187 position "pos" in the list. The default value for "pos" is 0.
188
189Examples:
190>
191 :let l = [1, 'item']
192 :lua l = vim.eval('l') -- same 'l'
193 :lua l:add(vim.list())
194 :lua l[0] = math.pi
195 :echo l[0] " 3.141593
196 :lua l[0] = nil -- remove first item
197 :lua l:insert(true, 1)
198 :lua print(l, #l, l[0], l[1], l[-1])
199 :lua for item in l() do print(item) end
200<
201
202==============================================================================
2034. Dict userdata *lua-dict*
204
205Similarly to list userdata, dict userdata represent vim dictionaries; since
206dictionaries are also objects, references are kept between Lua and Vim. A dict
207"d" has the following properties:
208
209Properties
210----------
211 o "#d" is the number of items in dict "d", equivalent to "len(d)"
212 in Vim.
213 o "d.key" or "d['key']" returns the value at entry "key" in "d".
214 To modify the entry at this key, simply do "d.key = newvalue"; in
215 particular, "d.key = nil" removes the entry from "d".
216 o "d()" returns an iterator for "d" and is equivalent to "items(d)" in
217 Vim.
218
219Examples:
220>
221 :let d = {'n':10}
222 :lua d = vim.eval('d') -- same 'd'
223 :lua print(d, d.n, #d)
224 :let d.self = d
225 :lua for k, v in d() do print(d, k, v) end
226 :lua d.x = math.pi
227 :lua d.self = nil -- remove entry
228 :echo d
229<
230
231==============================================================================
2325. Buffer userdata *lua-buffer*
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200233
Bram Moolenaar2334b6d2010-07-22 21:32:16 +0200234Buffer userdata represent vim buffers. A buffer userdata "b" has the following
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200235properties and methods:
236
237Properties
238----------
Bram Moolenaar2334b6d2010-07-22 21:32:16 +0200239 o "b()" sets "b" as the current buffer.
240 o "#b" is the number of lines in buffer "b".
241 o "b[k]" represents line number k: "b[k] = newline" replaces line k
242 with string "newline" and "b[k] = nil" deletes line k.
243 o "b.name" contains the short name of buffer "b" (read-only).
244 o "b.fname" contains the full name of buffer "b" (read-only).
245 o "b.number" contains the position of buffer "b" in the buffer list
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200246 (read-only).
247
248Methods
249-------
Bram Moolenaar2334b6d2010-07-22 21:32:16 +0200250 o "b:insert(newline[, pos])" inserts string "newline" at (optional)
251 position "pos" in the buffer. The default value for "pos" is
252 "#b + 1". If "pos == 0" then "newline" becomes the first line in
253 the buffer.
254 o "b:next()" returns the buffer next to "b" in the buffer list.
255 o "b:previous()" returns the buffer previous to "b" in the buffer
256 list.
257 o "b:isvalid()" returns 'true' (boolean) if buffer "b" corresponds to
258 a "real" (not freed from memory) Vim buffer.
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200259
260Examples:
261>
262 :lua b = vim.buffer() -- current buffer
263 :lua print(b.name, b.number)
264 :lua b[1] = "first line"
265 :lua b:insert("FIRST!", 0)
266 :lua b[1] = nil -- delete top line
267 :lua for i=1,3 do b:insert(math.random()) end
268 :3,4lua for i=vim.lastline,vim.firstline,-1 do b[i] = nil end
269 :lua vim.open"myfile"() -- open buffer and set it as current
270
271 function! ListBuffers()
272 lua << EOF
273 local b = vim.buffer(true) -- first buffer in list
274 while b ~= nil do
275 print(b.number, b.name, #b)
276 b = b:next()
277 end
278 vim.beep()
279 EOF
280 endfunction
281<
282
283==============================================================================
Bram Moolenaar1dced572012-04-05 16:54:08 +02002846. Window userdata *lua-window*
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200285
Bram Moolenaar2334b6d2010-07-22 21:32:16 +0200286Window objects represent vim windows. A window userdata "w" has the following
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200287properties and methods:
288
289Properties
290----------
Bram Moolenaar2334b6d2010-07-22 21:32:16 +0200291 o "w()" sets "w" as the current window.
292 o "w.buffer" contains the buffer of window "w" (read-only).
293 o "w.line" represents the cursor line position in window "w".
294 o "w.col" represents the cursor column position in window "w".
295 o "w.width" represents the width of window "w".
296 o "w.height" represents the height of window "w".
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200297
298Methods
299-------
Bram Moolenaar2334b6d2010-07-22 21:32:16 +0200300 o "w:next()" returns the window next to "w".
301 o "w:previous()" returns the window previous to "w".
302 o "w:isvalid()" returns 'true' (boolean) if window "w" corresponds to
303 a "real" (not freed from memory) Vim window.
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200304
305Examples:
306>
307 :lua w = vim.window() -- current window
308 :lua print(w.buffer.name, w.line, w.col)
309 :lua w.width = w.width + math.random(10)
310 :lua w.height = 2 * math.random() * w.height
311 :lua n,w = 0,vim.window(true) while w~=nil do n,w = n + 1,w:next() end
312 :lua print("There are " .. n .. " windows")
313<
314
315==============================================================================
Bram Moolenaar1dced572012-04-05 16:54:08 +02003167. The luaeval function *lua-luaeval*
317
318The (dual) equivalent of "vim.eval" for passing Lua values to Vim is
319"luaeval". "luaeval" takes an expression string and an optional argument and
320returns the result of the expression. It is semantically equivalent in Lua to:
321>
322 local chunkheader = "local _A = select(1, ...) return "
323 function luaeval (expstr, arg)
324 local chunk = assert(loadstring(chunkheader .. expstr, "luaeval"))
325 return chunk(arg) -- return typval
326 end
327<
328Note that "_A" receives the argument to "luaeval". Examples: >
329
330 :echo luaeval('math.pi')
331 :lua a = vim.list():add('newlist')
332 :let a = luaeval('a')
333 :echo a[0] " 'newlist'
334 :function Rand(x,y) " random uniform between x and y
335 : return luaeval('(_A.y-_A.x)*math.random()+_A.x', {'x':a:x,'y':a:y})
336 : endfunction
337 :echo Rand(1,10)
338
339
340==============================================================================
341 vim:tw=78:ts=8:noet:ft=help:norl: