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