blob: 718a229ebfad3d11ce5f17d55e0f3f16f3e67fbf [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 Moolenaar2334b6d2010-07-22 21:32:16 +020062:[range]luado {body} Execute Lua function "function (line) {body} end" for
Bram Moolenaar0ba04292010-07-14 23:23:17 +020063 each line in the [range], with the function argument
64 being set to the text of each line in turn, without a
65 trailing <EOL>. If the value returned by the function
66 is a string it becomes the text of the line in the
67 current turn. The default for [range] is the whole
Bram Moolenaar2334b6d2010-07-22 21:32:16 +020068 file: "1,$". {not in Vi}
Bram Moolenaar0ba04292010-07-14 23:23:17 +020069
70Examples:
71>
72 :luado return string.format("%s\t%d", line:reverse(), #line)
73
74 :lua require"lpeg"
75 :lua -- balanced parenthesis grammar:
76 :lua bp = lpeg.P{ "(" * ((1 - lpeg.S"()") + lpeg.V(1))^0 * ")" }
77 :luado if bp:match(line) then return "-->\t" .. line end
78<
79
80 *:luafile*
81:[range]luafile {file}
82 Execute Lua script in {file}. {not in Vi}
83 The whole argument is used as a single file name.
84
85Examples:
86>
87 :luafile script.lua
88 :luafile %
89<
90
91All these commands execute a Lua chunk from either the command line (:lua and
92:luado) or a file (:luafile) with the given line [range]. Similarly to the Lua
93interpreter, each chunk has its own scope and so only global variables are
Bram Moolenaar1dced572012-04-05 16:54:08 +020094shared between command calls. All Lua default libraries are available. In
95addition, Lua "print" function has its output redirected to the Vim message
96area, with arguments separated by a white space instead of a tab.
Bram Moolenaar0ba04292010-07-14 23:23:17 +020097
Bram Moolenaar9855d6b2010-07-18 14:34:51 +020098Lua uses the "vim" module (see |lua-vim|) to issue commands to Vim
Bram Moolenaar0ba04292010-07-14 23:23:17 +020099and manage buffers (|lua-buffer|) and windows (|lua-window|). However,
100procedures that alter buffer content, open new buffers, and change cursor
Bram Moolenaar9855d6b2010-07-18 14:34:51 +0200101position are restricted when the command is executed in the |sandbox|.
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200102
103
104==============================================================================
1052. The vim module *lua-vim*
106
107Lua interfaces Vim through the "vim" module. The first and last line of the
Bram Moolenaar2334b6d2010-07-22 21:32:16 +0200108input range are stored in "vim.firstline" and "vim.lastline" respectively. The
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200109module also includes routines for buffer, window, and current line queries,
110Vim evaluation and command execution, and others.
111
Bram Moolenaar1dced572012-04-05 16:54:08 +0200112 vim.list() Returns an empty list (see |List|).
113
114 vim.dict() Returns an empty dictionary (see |Dictionary|).
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200115
Bram Moolenaar2334b6d2010-07-22 21:32:16 +0200116 vim.buffer([arg]) If "arg" is a number, returns buffer with
117 number "arg" in the buffer list or, if "arg"
118 is a string, returns buffer whose full or short
119 name is "arg". In both cases, returns 'nil'
120 (nil value, not string) if the buffer is not
121 found. Otherwise, if "toboolean(arg)" is
122 'true' returns the first buffer in the buffer
123 list or else the current buffer.
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200124
Bram Moolenaar2334b6d2010-07-22 21:32:16 +0200125 vim.window([arg]) If "arg" is a number, returns window with
126 number "arg" or 'nil' (nil value, not string)
127 if not found. Otherwise, if "toboolean(arg)"
128 is 'true' returns the first window or else the
129 current window.
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200130
Bram Moolenaar1dced572012-04-05 16:54:08 +0200131 vim.type({arg}) Returns the type of {arg}. It is equivalent to
132 Lua's "type" function, but returns "list",
133 "dict", "buffer", or "window" if {arg} is a
134 list, dictionary, buffer, or window,
135 respectively. Examples: >
136 :lua l = vim.list()
137 :lua print(type(l), vim.type(l))
138 :" userdata list
139<
Bram Moolenaar2334b6d2010-07-22 21:32:16 +0200140 vim.command({cmd}) Executes the vim (ex-mode) command {cmd}.
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200141 Examples: >
142 :lua vim.command"set tw=60"
143 :lua vim.command"normal ddp"
144<
Bram Moolenaar2334b6d2010-07-22 21:32:16 +0200145 vim.eval({expr}) Evaluates expression {expr} (see |expression|),
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200146 converts the result to Lua, and returns it.
147 Vim strings and numbers are directly converted
148 to Lua strings and numbers respectively. Vim
149 lists and dictionaries are converted to Lua
Bram Moolenaar1dced572012-04-05 16:54:08 +0200150 userdata (see |lua-list| and |lua-dict|).
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200151 Examples: >
152 :lua tw = vim.eval"&tw"
153 :lua print(vim.eval"{'a': 'one'}".a)
154<
Bram Moolenaar2334b6d2010-07-22 21:32:16 +0200155 vim.line() Returns the current line (without the trailing
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200156 <EOL>), a Lua string.
157
Bram Moolenaar2334b6d2010-07-22 21:32:16 +0200158 vim.beep() Beeps.
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200159
Bram Moolenaar2334b6d2010-07-22 21:32:16 +0200160 vim.open({fname}) Opens a new buffer for file {fname} and
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200161 returns it. Note that the buffer is not set as
162 current.
163
164
165==============================================================================
Bram Moolenaar1dced572012-04-05 16:54:08 +02001663. List userdata *lua-list*
167
168List userdata represent vim lists, and the interface tries to follow closely
169Vim's syntax for lists. Since lists are objects, changes in list references in
170Lua are reflected in Vim and vice-versa. A list "l" has the following
171properties and methods:
172
173Properties
174----------
175 o "#l" is the number of items in list "l", equivalent to "len(l)"
176 in Vim.
177 o "l[k]" returns the k-th item in "l"; "l" is zero-indexed, as in Vim.
178 To modify the k-th item, simply do "l[k] = newitem"; in
179 particular, "l[k] = nil" removes the k-th item from "l".
180 o "l()" returns an iterator for "l".
181
182Methods
183-------
184 o "l:add(item)" appends "item" to the end of "l".
185 o "l:insert(item[, pos])" inserts "item" at (optional)
186 position "pos" in the list. The default value for "pos" is 0.
187
188Examples:
189>
190 :let l = [1, 'item']
191 :lua l = vim.eval('l') -- same 'l'
192 :lua l:add(vim.list())
193 :lua l[0] = math.pi
194 :echo l[0] " 3.141593
195 :lua l[0] = nil -- remove first item
196 :lua l:insert(true, 1)
197 :lua print(l, #l, l[0], l[1], l[-1])
198 :lua for item in l() do print(item) end
199<
200
201==============================================================================
2024. Dict userdata *lua-dict*
203
204Similarly to list userdata, dict userdata represent vim dictionaries; since
205dictionaries are also objects, references are kept between Lua and Vim. A dict
206"d" has the following properties:
207
208Properties
209----------
210 o "#d" is the number of items in dict "d", equivalent to "len(d)"
211 in Vim.
212 o "d.key" or "d['key']" returns the value at entry "key" in "d".
213 To modify the entry at this key, simply do "d.key = newvalue"; in
214 particular, "d.key = nil" removes the entry from "d".
215 o "d()" returns an iterator for "d" and is equivalent to "items(d)" in
216 Vim.
217
218Examples:
219>
220 :let d = {'n':10}
221 :lua d = vim.eval('d') -- same 'd'
222 :lua print(d, d.n, #d)
223 :let d.self = d
224 :lua for k, v in d() do print(d, k, v) end
225 :lua d.x = math.pi
226 :lua d.self = nil -- remove entry
227 :echo d
228<
229
230==============================================================================
2315. Buffer userdata *lua-buffer*
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200232
Bram Moolenaar2334b6d2010-07-22 21:32:16 +0200233Buffer userdata represent vim buffers. A buffer userdata "b" has the following
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200234properties and methods:
235
236Properties
237----------
Bram Moolenaar2334b6d2010-07-22 21:32:16 +0200238 o "b()" sets "b" as the current buffer.
239 o "#b" is the number of lines in buffer "b".
240 o "b[k]" represents line number k: "b[k] = newline" replaces line k
241 with string "newline" and "b[k] = nil" deletes line k.
242 o "b.name" contains the short name of buffer "b" (read-only).
243 o "b.fname" contains the full name of buffer "b" (read-only).
244 o "b.number" contains the position of buffer "b" in the buffer list
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200245 (read-only).
246
247Methods
248-------
Bram Moolenaar2334b6d2010-07-22 21:32:16 +0200249 o "b:insert(newline[, pos])" inserts string "newline" at (optional)
250 position "pos" in the buffer. The default value for "pos" is
251 "#b + 1". If "pos == 0" then "newline" becomes the first line in
252 the buffer.
253 o "b:next()" returns the buffer next to "b" in the buffer list.
254 o "b:previous()" returns the buffer previous to "b" in the buffer
255 list.
256 o "b:isvalid()" returns 'true' (boolean) if buffer "b" corresponds to
257 a "real" (not freed from memory) Vim buffer.
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200258
259Examples:
260>
261 :lua b = vim.buffer() -- current buffer
262 :lua print(b.name, b.number)
263 :lua b[1] = "first line"
264 :lua b:insert("FIRST!", 0)
265 :lua b[1] = nil -- delete top line
266 :lua for i=1,3 do b:insert(math.random()) end
267 :3,4lua for i=vim.lastline,vim.firstline,-1 do b[i] = nil end
268 :lua vim.open"myfile"() -- open buffer and set it as current
269
270 function! ListBuffers()
271 lua << EOF
272 local b = vim.buffer(true) -- first buffer in list
273 while b ~= nil do
274 print(b.number, b.name, #b)
275 b = b:next()
276 end
277 vim.beep()
278 EOF
279 endfunction
280<
281
282==============================================================================
Bram Moolenaar1dced572012-04-05 16:54:08 +02002836. Window userdata *lua-window*
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200284
Bram Moolenaar2334b6d2010-07-22 21:32:16 +0200285Window objects represent vim windows. A window userdata "w" has the following
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200286properties and methods:
287
288Properties
289----------
Bram Moolenaar2334b6d2010-07-22 21:32:16 +0200290 o "w()" sets "w" as the current window.
291 o "w.buffer" contains the buffer of window "w" (read-only).
292 o "w.line" represents the cursor line position in window "w".
293 o "w.col" represents the cursor column position in window "w".
294 o "w.width" represents the width of window "w".
295 o "w.height" represents the height of window "w".
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200296
297Methods
298-------
Bram Moolenaar2334b6d2010-07-22 21:32:16 +0200299 o "w:next()" returns the window next to "w".
300 o "w:previous()" returns the window previous to "w".
301 o "w:isvalid()" returns 'true' (boolean) if window "w" corresponds to
302 a "real" (not freed from memory) Vim window.
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200303
304Examples:
305>
306 :lua w = vim.window() -- current window
307 :lua print(w.buffer.name, w.line, w.col)
308 :lua w.width = w.width + math.random(10)
309 :lua w.height = 2 * math.random() * w.height
310 :lua n,w = 0,vim.window(true) while w~=nil do n,w = n + 1,w:next() end
311 :lua print("There are " .. n .. " windows")
312<
313
314==============================================================================
Bram Moolenaar1dced572012-04-05 16:54:08 +02003157. The luaeval function *lua-luaeval*
316
317The (dual) equivalent of "vim.eval" for passing Lua values to Vim is
318"luaeval". "luaeval" takes an expression string and an optional argument and
319returns the result of the expression. It is semantically equivalent in Lua to:
320>
321 local chunkheader = "local _A = select(1, ...) return "
322 function luaeval (expstr, arg)
323 local chunk = assert(loadstring(chunkheader .. expstr, "luaeval"))
324 return chunk(arg) -- return typval
325 end
326<
327Note that "_A" receives the argument to "luaeval". Examples: >
328
329 :echo luaeval('math.pi')
330 :lua a = vim.list():add('newlist')
331 :let a = luaeval('a')
332 :echo a[0] " 'newlist'
333 :function Rand(x,y) " random uniform between x and y
334 : return luaeval('(_A.y-_A.x)*math.random()+_A.x', {'x':a:x,'y':a:y})
335 : endfunction
336 :echo Rand(1,10)
337
338
339==============================================================================
340 vim:tw=78:ts=8:noet:ft=help:norl: