1 To: vim_dev@googlegroups.com
4 From: Bram Moolenaar <Bram@moolenaar.net>
6 Content-Type: text/plain; charset=UTF-8
7 Content-Transfer-Encoding: 8bit
11 Problem: Member confusion in Lua interface.
12 Solution: Fix it. Add luaeval(). (Taro Muraoka, Luis Carvalho)
13 Files: runtime/doc/if_lua.txt, src/eval.c, src/if_lua.c,
17 *** ../vim-7.3.489/runtime/doc/if_lua.txt 2010-08-15 21:57:14.000000000 +0200
18 --- runtime/doc/if_lua.txt 2012-04-05 16:41:35.000000000 +0200
21 ! *if_lua.txt* For Vim version 7.3. Last change: 2010 Jul 22
24 VIM REFERENCE MANUAL by Luis Carvalho
26 ! *if_lua.txt* For Vim version 7.3. Last change: 2012 Jan 16
29 VIM REFERENCE MANUAL by Luis Carvalho
33 1. Commands |lua-commands|
34 2. The vim module |lua-vim|
35 ! 3. Buffer userdata |lua-buffer|
36 ! 4. Window userdata |lua-window|
38 {Vi does not have any of these commands}
42 1. Commands |lua-commands|
43 2. The vim module |lua-vim|
44 ! 3. List userdata |lua-list|
45 ! 4. Dict userdata |lua-dict|
46 ! 5. Buffer userdata |lua-buffer|
47 ! 6. Window userdata |lua-window|
48 ! 7. The luaeval function |lua-luaeval|
50 {Vi does not have any of these commands}
54 All these commands execute a Lua chunk from either the command line (:lua and
55 :luado) or a file (:luafile) with the given line [range]. Similarly to the Lua
56 interpreter, each chunk has its own scope and so only global variables are
57 ! shared between command calls. Lua default libraries "table", "string", "math",
58 ! and "package" are available, "io" and "debug" are not, and "os" is restricted
59 ! to functions "date", "clock", "time", "difftime", and "getenv". In addition,
60 ! Lua "print" function has its output redirected to the Vim message area, with
61 ! arguments separated by a white space instead of a tab.
63 Lua uses the "vim" module (see |lua-vim|) to issue commands to Vim
64 and manage buffers (|lua-buffer|) and windows (|lua-window|). However,
66 All these commands execute a Lua chunk from either the command line (:lua and
67 :luado) or a file (:luafile) with the given line [range]. Similarly to the Lua
68 interpreter, each chunk has its own scope and so only global variables are
69 ! shared between command calls. All Lua default libraries are available. In
70 ! addition, Lua "print" function has its output redirected to the Vim message
71 ! area, with arguments separated by a white space instead of a tab.
73 Lua uses the "vim" module (see |lua-vim|) to issue commands to Vim
74 and manage buffers (|lua-buffer|) and windows (|lua-window|). However,
77 module also includes routines for buffer, window, and current line queries,
78 Vim evaluation and command execution, and others.
80 ! vim.isbuffer(value) Returns 'true' (boolean, not string) if
81 ! "value" is a buffer userdata and 'false'
82 ! otherwise (see |lua-buffer|).
84 vim.buffer([arg]) If "arg" is a number, returns buffer with
85 number "arg" in the buffer list or, if "arg"
87 module also includes routines for buffer, window, and current line queries,
88 Vim evaluation and command execution, and others.
90 ! vim.list() Returns an empty list (see |List|).
92 ! vim.dict() Returns an empty dictionary (see |Dictionary|).
94 vim.buffer([arg]) If "arg" is a number, returns buffer with
95 number "arg" in the buffer list or, if "arg"
98 'true' returns the first buffer in the buffer
99 list or else the current buffer.
101 - vim.iswindow(value) Returns 'true' (boolean, not string) if
102 - "value" is a window userdata and
103 - 'false' otherwise (see |lua-window|).
105 vim.window([arg]) If "arg" is a number, returns window with
106 number "arg" or 'nil' (nil value, not string)
107 if not found. Otherwise, if "toboolean(arg)"
108 is 'true' returns the first window or else the
111 vim.command({cmd}) Executes the vim (ex-mode) command {cmd}.
113 :lua vim.command"set tw=60"
115 'true' returns the first buffer in the buffer
116 list or else the current buffer.
118 vim.window([arg]) If "arg" is a number, returns window with
119 number "arg" or 'nil' (nil value, not string)
120 if not found. Otherwise, if "toboolean(arg)"
121 is 'true' returns the first window or else the
124 + vim.type({arg}) Returns the type of {arg}. It is equivalent to
125 + Lua's "type" function, but returns "list",
126 + "dict", "buffer", or "window" if {arg} is a
127 + list, dictionary, buffer, or window,
128 + respectively. Examples: >
129 + :lua l = vim.list()
130 + :lua print(type(l), vim.type(l))
133 vim.command({cmd}) Executes the vim (ex-mode) command {cmd}.
135 :lua vim.command"set tw=60"
138 Vim strings and numbers are directly converted
139 to Lua strings and numbers respectively. Vim
140 lists and dictionaries are converted to Lua
141 ! tables (lists become integer-keyed tables).
143 :lua tw = vim.eval"&tw"
144 :lua print(vim.eval"{'a': 'one'}".a)
146 Vim strings and numbers are directly converted
147 to Lua strings and numbers respectively. Vim
148 lists and dictionaries are converted to Lua
149 ! userdata (see |lua-list| and |lua-dict|).
151 :lua tw = vim.eval"&tw"
152 :lua print(vim.eval"{'a': 'one'}".a)
157 ==============================================================================
158 ! 3. Buffer userdata *lua-buffer*
160 Buffer userdata represent vim buffers. A buffer userdata "b" has the following
161 properties and methods:
165 ==============================================================================
166 ! 3. List userdata *lua-list*
168 ! List userdata represent vim lists, and the interface tries to follow closely
169 ! Vim's syntax for lists. Since lists are objects, changes in list references in
170 ! Lua are reflected in Vim and vice-versa. A list "l" has the following
171 ! properties and methods:
175 ! o "#l" is the number of items in list "l", equivalent to "len(l)"
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".
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.
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
201 ! ==============================================================================
202 ! 4. Dict userdata *lua-dict*
204 ! Similarly to list userdata, dict userdata represent vim dictionaries; since
205 ! dictionaries are also objects, references are kept between Lua and Vim. A dict
206 ! "d" has the following properties:
210 ! o "#d" is the number of items in dict "d", equivalent to "len(d)"
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
221 ! :lua d = vim.eval('d') -- same 'd'
222 ! :lua print(d, d.n, #d)
224 ! :lua for k, v in d() do print(d, k, v) end
226 ! :lua d.self = nil -- remove entry
230 ! ==============================================================================
231 ! 5. Buffer userdata *lua-buffer*
233 Buffer userdata represent vim buffers. A buffer userdata "b" has the following
234 properties and methods:
239 ==============================================================================
240 ! 4. Window userdata *lua-window*
242 Window objects represent vim windows. A window userdata "w" has the following
243 properties and methods:
247 ==============================================================================
248 ! 6. Window userdata *lua-window*
250 Window objects represent vim windows. A window userdata "w" has the following
251 properties and methods:
256 ==============================================================================
257 ! vim:tw=78:ts=8:ft=help:norl:
261 ==============================================================================
262 ! 7. The luaeval function *lua-luaeval*
264 ! The (dual) equivalent of "vim.eval" for passing Lua values to Vim is
265 ! "luaeval". "luaeval" takes an expression string and an optional argument and
266 ! returns the result of the expression. It is semantically equivalent in Lua to:
268 ! local chunkheader = "local _A = select(1, ...) return "
269 ! function luaeval (expstr, arg)
270 ! local chunk = assert(loadstring(chunkheader .. expstr, "luaeval"))
271 ! return chunk(arg) -- return typval
274 ! Note that "_A" receives the argument to "luaeval". Examples: >
276 ! :echo luaeval('math.pi')
277 ! :lua a = vim.list():add('newlist')
278 ! :let a = luaeval('a')
279 ! :echo a[0] " 'newlist'
280 ! :function Rand(x,y) " random uniform between x and y
281 ! : return luaeval('(_A.y-_A.x)*math.random()+_A.x', {'x':a:x,'y':a:y})
286 ! ==============================================================================
287 ! vim:tw=78:ts=8:noet:ft=help:norl:
288 *** ../vim-7.3.489/src/eval.c 2012-03-28 16:49:25.000000000 +0200
289 --- src/eval.c 2012-04-05 16:41:35.000000000 +0200
293 static void f_log __ARGS((typval_T *argvars, typval_T *rettv));
294 static void f_log10 __ARGS((typval_T *argvars, typval_T *rettv));
297 + static void f_luaeval __ARGS((typval_T *argvars, typval_T *rettv));
299 static void f_map __ARGS((typval_T *argvars, typval_T *rettv));
300 static void f_maparg __ARGS((typval_T *argvars, typval_T *rettv));
301 static void f_mapcheck __ARGS((typval_T *argvars, typval_T *rettv));
306 set_ref_in_ht(&vimvarht, copyID);
309 + set_ref_in_lua(copyID);
313 * 2. Free lists and dictionaries that are not referenced.
318 {"log", 1, 1, f_log},
319 {"log10", 1, 1, f_log10},
322 + {"luaeval", 1, 2, f_luaeval},
324 {"map", 2, 2, f_map},
325 {"maparg", 1, 4, f_maparg},
326 {"mapcheck", 1, 3, f_mapcheck},
335 + * "luaeval()" function
338 + f_luaeval(argvars, rettv)
343 + char_u buf[NUMBUFLEN];
345 + str = get_tv_string_buf(&argvars[0], buf);
346 + do_luaeval(str, argvars + 1, rettv);
353 *** ../vim-7.3.489/src/if_lua.c 2011-12-08 16:00:12.000000000 +0100
354 --- src/if_lua.c 2012-04-05 16:41:35.000000000 +0200
357 ! /* vi:set ts=8 sts=4 sw=4:
359 * VIM - Vi IMproved by Bram Moolenaar
362 ! /* vi:set ts=8 sts=4 sw=4 noet:
364 * VIM - Vi IMproved by Bram Moolenaar
370 #define LUAVIM_CHUNKNAME "vim chunk"
371 #define LUAVIM_NAME "vim"
372 + #define LUAVIM_EVALNAME "luaeval"
373 + #define LUAVIM_EVALHEADER "local _A=select(1,...) return "
375 typedef buf_T *luaV_Buffer;
376 typedef win_T *luaV_Window;
377 + typedef dict_T *luaV_Dict;
378 + typedef list_T *luaV_List;
379 typedef void (*msgfunc_T)(char_u *);
381 + static const char LUAVIM_DICT[] = "dict";
382 + static const char LUAVIM_LIST[] = "list";
383 static const char LUAVIM_BUFFER[] = "buffer";
384 static const char LUAVIM_WINDOW[] = "window";
385 static const char LUAVIM_FREE[] = "luaV_free";
386 + static const char LUAVIM_LUAEVAL[] = "luaV_luaeval";
387 + static const char LUAVIM_SETREF[] = "luaV_setref";
389 + /* most functions are closures with a cache table as first upvalue;
390 + * get/setudata manage references to vim userdata in cache table through
391 + * object pointers (light userdata) */
392 + #define luaV_getudata(L, v) \
393 + lua_pushlightuserdata((L), (void *) (v)); \
394 + lua_rawget((L), lua_upvalueindex(1))
395 + #define luaV_setudata(L, v) \
396 + lua_pushlightuserdata((L), (void *) (v)); \
397 + lua_pushvalue((L), -2); \
398 + lua_rawset((L), lua_upvalueindex(1))
399 #define luaV_getfield(L, s) \
400 lua_pushlightuserdata((L), (void *)(s)); \
401 lua_rawget((L), LUA_REGISTRYINDEX)
405 #define luaV_msg(L) luaV_msgfunc((L), (msgfunc_T) msg)
406 #define luaV_emsg(L) luaV_msgfunc((L), (msgfunc_T) emsg)
408 + static luaV_List *luaV_pushlist (lua_State *L, list_T *lis);
409 + static luaV_Dict *luaV_pushdict (lua_State *L, dict_T *dic);
411 + #if LUA_VERSION_NUM <= 501
412 + #define luaV_openlib(L, l, n) luaL_openlib(L, NULL, l, n)
413 + #define luaL_typeerror luaL_typerror
415 + #define luaV_openlib luaL_setfuncs
425 #define luaL_register dll_luaL_register
426 #define luaL_typerror dll_luaL_typerror
427 #define luaL_checklstring dll_luaL_checklstring
428 #define luaL_checkinteger dll_luaL_checkinteger
429 #define luaL_optinteger dll_luaL_optinteger
430 #define luaL_checktype dll_luaL_checktype
431 #define luaL_error dll_luaL_error
432 - #define luaL_loadfile dll_luaL_loadfile
433 - #define luaL_loadbuffer dll_luaL_loadbuffer
434 #define luaL_newstate dll_luaL_newstate
435 #define luaL_buffinit dll_luaL_buffinit
436 - #define luaL_prepbuffer dll_luaL_prepbuffer
437 #define luaL_addlstring dll_luaL_addlstring
438 #define luaL_pushresult dll_luaL_pushresult
440 #define lua_close dll_lua_close
441 #define lua_gettop dll_lua_gettop
442 #define lua_settop dll_lua_settop
443 #define lua_pushvalue dll_lua_pushvalue
444 #define lua_replace dll_lua_replace
445 #define lua_isnumber dll_lua_isnumber
446 #define lua_isstring dll_lua_isstring
447 #define lua_type dll_lua_type
448 #define lua_rawequal dll_lua_rawequal
449 - #define lua_tonumber dll_lua_tonumber
450 - #define lua_tointeger dll_lua_tointeger
451 #define lua_toboolean dll_lua_toboolean
452 #define lua_tolstring dll_lua_tolstring
453 #define lua_touserdata dll_lua_touserdata
458 + #if LUA_VERSION_NUM <= 501
459 #define luaL_register dll_luaL_register
460 + #define luaL_prepbuffer dll_luaL_prepbuffer
461 + #define luaL_openlib dll_luaL_openlib
462 #define luaL_typerror dll_luaL_typerror
463 + #define luaL_loadfile dll_luaL_loadfile
464 + #define luaL_loadbuffer dll_luaL_loadbuffer
466 + #define luaL_prepbuffsize dll_luaL_prepbuffsize
467 + #define luaL_setfuncs dll_luaL_setfuncs
468 + #define luaL_loadfilex dll_luaL_loadfilex
469 + #define luaL_loadbufferx dll_luaL_loadbufferx
470 + #define luaL_argerror dll_luaL_argerror
472 #define luaL_checklstring dll_luaL_checklstring
473 #define luaL_checkinteger dll_luaL_checkinteger
474 #define luaL_optinteger dll_luaL_optinteger
475 #define luaL_checktype dll_luaL_checktype
476 #define luaL_error dll_luaL_error
477 #define luaL_newstate dll_luaL_newstate
478 #define luaL_buffinit dll_luaL_buffinit
479 #define luaL_addlstring dll_luaL_addlstring
480 #define luaL_pushresult dll_luaL_pushresult
482 + #if LUA_VERSION_NUM <= 501
483 + #define lua_tonumber dll_lua_tonumber
484 + #define lua_tointeger dll_lua_tointeger
485 + #define lua_call dll_lua_call
486 + #define lua_pcall dll_lua_pcall
488 + #define lua_tonumberx dll_lua_tonumberx
489 + #define lua_tointegerx dll_lua_tointegerx
490 + #define lua_callk dll_lua_callk
491 + #define lua_pcallk dll_lua_pcallk
492 + #define lua_getglobal dll_lua_getglobal
493 + #define lua_setglobal dll_lua_setglobal
494 + #define lua_typename dll_lua_typename
496 #define lua_close dll_lua_close
497 #define lua_gettop dll_lua_gettop
498 #define lua_settop dll_lua_settop
499 #define lua_pushvalue dll_lua_pushvalue
500 #define lua_replace dll_lua_replace
501 + #define lua_remove dll_lua_remove
502 #define lua_isnumber dll_lua_isnumber
503 #define lua_isstring dll_lua_isstring
504 #define lua_type dll_lua_type
505 #define lua_rawequal dll_lua_rawequal
506 #define lua_toboolean dll_lua_toboolean
507 #define lua_tolstring dll_lua_tolstring
508 #define lua_touserdata dll_lua_touserdata
511 #define lua_pushlightuserdata dll_lua_pushlightuserdata
512 #define lua_getfield dll_lua_getfield
513 #define lua_rawget dll_lua_rawget
514 #define lua_createtable dll_lua_createtable
515 #define lua_newuserdata dll_lua_newuserdata
516 #define lua_getmetatable dll_lua_getmetatable
517 #define lua_setfield dll_lua_setfield
518 #define lua_rawset dll_lua_rawset
519 #define lua_rawseti dll_lua_rawseti
520 - #define lua_remove dll_lua_remove
521 #define lua_setmetatable dll_lua_setmetatable
522 - #define lua_call dll_lua_call
523 - #define lua_pcall dll_lua_pcall
525 #define luaopen_base dll_luaopen_base
526 #define luaopen_table dll_luaopen_table
528 #define lua_pushlightuserdata dll_lua_pushlightuserdata
529 #define lua_getfield dll_lua_getfield
530 #define lua_rawget dll_lua_rawget
531 + #define lua_rawgeti dll_lua_rawgeti
532 #define lua_createtable dll_lua_createtable
533 #define lua_newuserdata dll_lua_newuserdata
534 #define lua_getmetatable dll_lua_getmetatable
535 #define lua_setfield dll_lua_setfield
536 #define lua_rawset dll_lua_rawset
537 #define lua_rawseti dll_lua_rawseti
538 #define lua_setmetatable dll_lua_setmetatable
540 #define luaopen_base dll_luaopen_base
541 #define luaopen_table dll_luaopen_table
544 #define luaL_openlibs dll_luaL_openlibs
547 void (*dll_luaL_register) (lua_State *L, const char *libname, const luaL_Reg *l);
548 int (*dll_luaL_typerror) (lua_State *L, int narg, const char *tname);
549 const char *(*dll_luaL_checklstring) (lua_State *L, int numArg, size_t *l);
550 lua_Integer (*dll_luaL_checkinteger) (lua_State *L, int numArg);
551 lua_Integer (*dll_luaL_optinteger) (lua_State *L, int nArg, lua_Integer def);
552 void (*dll_luaL_checktype) (lua_State *L, int narg, int t);
553 int (*dll_luaL_error) (lua_State *L, const char *fmt, ...);
554 - int (*dll_luaL_loadfile) (lua_State *L, const char *filename);
555 - int (*dll_luaL_loadbuffer) (lua_State *L, const char *buff, size_t sz, const char *name);
556 lua_State *(*dll_luaL_newstate) (void);
557 void (*dll_luaL_buffinit) (lua_State *L, luaL_Buffer *B);
558 - char *(*dll_luaL_prepbuffer) (luaL_Buffer *B);
559 void (*dll_luaL_addlstring) (luaL_Buffer *B, const char *s, size_t l);
560 void (*dll_luaL_pushresult) (luaL_Buffer *B);
562 void (*dll_lua_close) (lua_State *L);
563 int (*dll_lua_gettop) (lua_State *L);
564 void (*dll_lua_settop) (lua_State *L, int idx);
565 void (*dll_lua_pushvalue) (lua_State *L, int idx);
566 void (*dll_lua_replace) (lua_State *L, int idx);
567 int (*dll_lua_isnumber) (lua_State *L, int idx);
568 int (*dll_lua_isstring) (lua_State *L, int idx);
569 int (*dll_lua_type) (lua_State *L, int idx);
570 int (*dll_lua_rawequal) (lua_State *L, int idx1, int idx2);
571 - lua_Number (*dll_lua_tonumber) (lua_State *L, int idx);
572 - lua_Integer (*dll_lua_tointeger) (lua_State *L, int idx);
573 int (*dll_lua_toboolean) (lua_State *L, int idx);
574 const char *(*dll_lua_tolstring) (lua_State *L, int idx, size_t *len);
575 void *(*dll_lua_touserdata) (lua_State *L, int idx);
577 #define luaL_openlibs dll_luaL_openlibs
580 + #if LUA_VERSION_NUM <= 501
581 void (*dll_luaL_register) (lua_State *L, const char *libname, const luaL_Reg *l);
582 + char *(*dll_luaL_prepbuffer) (luaL_Buffer *B);
583 + void (*dll_luaL_openlib) (lua_State *L, const char *libname, const luaL_Reg *l, int nup);
584 int (*dll_luaL_typerror) (lua_State *L, int narg, const char *tname);
585 + int (*dll_luaL_loadfile) (lua_State *L, const char *filename);
586 + int (*dll_luaL_loadbuffer) (lua_State *L, const char *buff, size_t sz, const char *name);
588 + char *(*dll_luaL_prepbuffsize) (luaL_Buffer *B, size_t sz);
589 + void (*dll_luaL_setfuncs) (lua_State *L, const luaL_Reg *l, int nup);
590 + int (*dll_luaL_loadfilex) (lua_State *L, const char *filename, const char *mode);
591 + int (*dll_luaL_loadbufferx) (lua_State *L, const char *buff, size_t sz, const char *name, const char *mode);
592 + int (*dll_luaL_argerror) (lua_State *L, int numarg, const char *extramsg);
594 const char *(*dll_luaL_checklstring) (lua_State *L, int numArg, size_t *l);
595 lua_Integer (*dll_luaL_checkinteger) (lua_State *L, int numArg);
596 lua_Integer (*dll_luaL_optinteger) (lua_State *L, int nArg, lua_Integer def);
597 void (*dll_luaL_checktype) (lua_State *L, int narg, int t);
598 int (*dll_luaL_error) (lua_State *L, const char *fmt, ...);
599 lua_State *(*dll_luaL_newstate) (void);
600 void (*dll_luaL_buffinit) (lua_State *L, luaL_Buffer *B);
601 void (*dll_luaL_addlstring) (luaL_Buffer *B, const char *s, size_t l);
602 void (*dll_luaL_pushresult) (luaL_Buffer *B);
604 + #if LUA_VERSION_NUM <= 501
605 + lua_Number (*dll_lua_tonumber) (lua_State *L, int idx);
606 + lua_Integer (*dll_lua_tointeger) (lua_State *L, int idx);
607 + void (*dll_lua_call) (lua_State *L, int nargs, int nresults);
608 + int (*dll_lua_pcall) (lua_State *L, int nargs, int nresults, int errfunc);
610 + lua_Number (*dll_lua_tonumberx) (lua_State *L, int idx, int *isnum);
611 + lua_Integer (*dll_lua_tointegerx) (lua_State *L, int idx, int *isnum);
612 + void (*dll_lua_callk) (lua_State *L, int nargs, int nresults, int ctx,
614 + int (*dll_lua_pcallk) (lua_State *L, int nargs, int nresults, int errfunc,
615 + int ctx, lua_CFunction k);
616 + void (*dll_lua_getglobal) (lua_State *L, const char *var);
617 + void (*dll_lua_setglobal) (lua_State *L, const char *var);
618 + const char *(*dll_lua_typename) (lua_State *L, int tp);
620 void (*dll_lua_close) (lua_State *L);
621 int (*dll_lua_gettop) (lua_State *L);
622 void (*dll_lua_settop) (lua_State *L, int idx);
623 void (*dll_lua_pushvalue) (lua_State *L, int idx);
624 void (*dll_lua_replace) (lua_State *L, int idx);
625 + void (*dll_lua_remove) (lua_State *L, int idx);
626 int (*dll_lua_isnumber) (lua_State *L, int idx);
627 int (*dll_lua_isstring) (lua_State *L, int idx);
628 int (*dll_lua_type) (lua_State *L, int idx);
629 int (*dll_lua_rawequal) (lua_State *L, int idx1, int idx2);
630 int (*dll_lua_toboolean) (lua_State *L, int idx);
631 const char *(*dll_lua_tolstring) (lua_State *L, int idx, size_t *len);
632 void *(*dll_lua_touserdata) (lua_State *L, int idx);
635 void (*dll_lua_pushlightuserdata) (lua_State *L, void *p);
636 void (*dll_lua_getfield) (lua_State *L, int idx, const char *k);
637 void (*dll_lua_rawget) (lua_State *L, int idx);
638 void (*dll_lua_createtable) (lua_State *L, int narr, int nrec);
639 void *(*dll_lua_newuserdata) (lua_State *L, size_t sz);
640 int (*dll_lua_getmetatable) (lua_State *L, int objindex);
641 void (*dll_lua_setfield) (lua_State *L, int idx, const char *k);
642 void (*dll_lua_rawset) (lua_State *L, int idx);
643 void (*dll_lua_rawseti) (lua_State *L, int idx, int n);
644 - void (*dll_lua_remove) (lua_State *L, int idx);
645 int (*dll_lua_setmetatable) (lua_State *L, int objindex);
646 - void (*dll_lua_call) (lua_State *L, int nargs, int nresults);
647 - int (*dll_lua_pcall) (lua_State *L, int nargs, int nresults, int errfunc);
649 int (*dll_luaopen_base) (lua_State *L);
650 int (*dll_luaopen_table) (lua_State *L);
652 void (*dll_lua_pushlightuserdata) (lua_State *L, void *p);
653 void (*dll_lua_getfield) (lua_State *L, int idx, const char *k);
654 void (*dll_lua_rawget) (lua_State *L, int idx);
655 + void (*dll_lua_rawgeti) (lua_State *L, int idx, int n);
656 void (*dll_lua_createtable) (lua_State *L, int narr, int nrec);
657 void *(*dll_lua_newuserdata) (lua_State *L, size_t sz);
658 int (*dll_lua_getmetatable) (lua_State *L, int objindex);
659 void (*dll_lua_setfield) (lua_State *L, int idx, const char *k);
660 void (*dll_lua_rawset) (lua_State *L, int idx);
661 void (*dll_lua_rawseti) (lua_State *L, int idx, int n);
662 int (*dll_lua_setmetatable) (lua_State *L, int objindex);
664 int (*dll_luaopen_base) (lua_State *L);
665 int (*dll_luaopen_table) (lua_State *L);
669 static const luaV_Reg luaV_dll[] = {
671 {"luaL_register", (luaV_function) &dll_luaL_register},
672 {"luaL_typerror", (luaV_function) &dll_luaL_typerror},
673 {"luaL_checklstring", (luaV_function) &dll_luaL_checklstring},
674 {"luaL_checkinteger", (luaV_function) &dll_luaL_checkinteger},
675 {"luaL_optinteger", (luaV_function) &dll_luaL_optinteger},
676 {"luaL_checktype", (luaV_function) &dll_luaL_checktype},
677 {"luaL_error", (luaV_function) &dll_luaL_error},
678 - {"luaL_loadfile", (luaV_function) &dll_luaL_loadfile},
679 - {"luaL_loadbuffer", (luaV_function) &dll_luaL_loadbuffer},
680 {"luaL_newstate", (luaV_function) &dll_luaL_newstate},
681 {"luaL_buffinit", (luaV_function) &dll_luaL_buffinit},
682 - {"luaL_prepbuffer", (luaV_function) &dll_luaL_prepbuffer},
683 {"luaL_addlstring", (luaV_function) &dll_luaL_addlstring},
684 {"luaL_pushresult", (luaV_function) &dll_luaL_pushresult},
686 {"lua_close", (luaV_function) &dll_lua_close},
687 {"lua_gettop", (luaV_function) &dll_lua_gettop},
688 {"lua_settop", (luaV_function) &dll_lua_settop},
689 {"lua_pushvalue", (luaV_function) &dll_lua_pushvalue},
690 {"lua_replace", (luaV_function) &dll_lua_replace},
691 {"lua_isnumber", (luaV_function) &dll_lua_isnumber},
692 {"lua_isstring", (luaV_function) &dll_lua_isstring},
693 {"lua_type", (luaV_function) &dll_lua_type},
694 {"lua_rawequal", (luaV_function) &dll_lua_rawequal},
695 - {"lua_tonumber", (luaV_function) &dll_lua_tonumber},
696 - {"lua_tointeger", (luaV_function) &dll_lua_tointeger},
697 {"lua_toboolean", (luaV_function) &dll_lua_toboolean},
698 {"lua_tolstring", (luaV_function) &dll_lua_tolstring},
699 {"lua_touserdata", (luaV_function) &dll_lua_touserdata},
702 static const luaV_Reg luaV_dll[] = {
704 + #if LUA_VERSION_NUM <= 501
705 {"luaL_register", (luaV_function) &dll_luaL_register},
706 + {"luaL_prepbuffer", (luaV_function) &dll_luaL_prepbuffer},
707 + {"luaL_openlib", (luaV_function) &dll_luaL_openlib},
708 {"luaL_typerror", (luaV_function) &dll_luaL_typerror},
709 + {"luaL_loadfile", (luaV_function) &dll_luaL_loadfile},
710 + {"luaL_loadbuffer", (luaV_function) &dll_luaL_loadbuffer},
712 + {"luaL_prepbuffsize", (luaV_function) &dll_luaL_prepbuffsize},
713 + {"luaL_setfuncs", (luaV_function) &dll_luaL_setfuncs},
714 + {"luaL_loadfilex", (luaV_function) &dll_luaL_loadfilex},
715 + {"luaL_loadbufferx", (luaV_function) &dll_luaL_loadbufferx},
716 + {"luaL_argerror", (luaV_function) &dll_luaL_argerror},
718 {"luaL_checklstring", (luaV_function) &dll_luaL_checklstring},
719 {"luaL_checkinteger", (luaV_function) &dll_luaL_checkinteger},
720 {"luaL_optinteger", (luaV_function) &dll_luaL_optinteger},
721 {"luaL_checktype", (luaV_function) &dll_luaL_checktype},
722 {"luaL_error", (luaV_function) &dll_luaL_error},
723 {"luaL_newstate", (luaV_function) &dll_luaL_newstate},
724 {"luaL_buffinit", (luaV_function) &dll_luaL_buffinit},
725 {"luaL_addlstring", (luaV_function) &dll_luaL_addlstring},
726 {"luaL_pushresult", (luaV_function) &dll_luaL_pushresult},
728 + #if LUA_VERSION_NUM <= 501
729 + {"lua_tonumber", (luaV_function) &dll_lua_tonumber},
730 + {"lua_tointeger", (luaV_function) &dll_lua_tointeger},
731 + {"lua_call", (luaV_function) &dll_lua_call},
732 + {"lua_pcall", (luaV_function) &dll_lua_pcall},
734 + {"lua_tonumberx", (luaV_function) &dll_lua_tonumberx},
735 + {"lua_tointegerx", (luaV_function) &dll_lua_tointegerx},
736 + {"lua_callk", (luaV_function) &dll_lua_callk},
737 + {"lua_pcallk", (luaV_function) &dll_lua_pcallk},
738 + {"lua_getglobal", (luaV_function) &dll_lua_getglobal},
739 + {"lua_setglobal", (luaV_function) &dll_lua_setglobal},
740 + {"lua_typename", (luaV_function) &dll_lua_typename},
742 {"lua_close", (luaV_function) &dll_lua_close},
743 {"lua_gettop", (luaV_function) &dll_lua_gettop},
744 {"lua_settop", (luaV_function) &dll_lua_settop},
745 {"lua_pushvalue", (luaV_function) &dll_lua_pushvalue},
746 {"lua_replace", (luaV_function) &dll_lua_replace},
747 + {"lua_remove", (luaV_function) &dll_lua_remove},
748 {"lua_isnumber", (luaV_function) &dll_lua_isnumber},
749 {"lua_isstring", (luaV_function) &dll_lua_isstring},
750 {"lua_type", (luaV_function) &dll_lua_type},
751 {"lua_rawequal", (luaV_function) &dll_lua_rawequal},
752 {"lua_toboolean", (luaV_function) &dll_lua_toboolean},
753 {"lua_tolstring", (luaV_function) &dll_lua_tolstring},
754 {"lua_touserdata", (luaV_function) &dll_lua_touserdata},
757 {"lua_pushlightuserdata", (luaV_function) &dll_lua_pushlightuserdata},
758 {"lua_getfield", (luaV_function) &dll_lua_getfield},
759 {"lua_rawget", (luaV_function) &dll_lua_rawget},
760 {"lua_createtable", (luaV_function) &dll_lua_createtable},
761 {"lua_newuserdata", (luaV_function) &dll_lua_newuserdata},
762 {"lua_getmetatable", (luaV_function) &dll_lua_getmetatable},
763 {"lua_setfield", (luaV_function) &dll_lua_setfield},
764 {"lua_rawset", (luaV_function) &dll_lua_rawset},
765 {"lua_rawseti", (luaV_function) &dll_lua_rawseti},
766 - {"lua_remove", (luaV_function) &dll_lua_remove},
767 {"lua_setmetatable", (luaV_function) &dll_lua_setmetatable},
768 - {"lua_call", (luaV_function) &dll_lua_call},
769 - {"lua_pcall", (luaV_function) &dll_lua_pcall},
771 {"luaopen_base", (luaV_function) &dll_luaopen_base},
772 {"luaopen_table", (luaV_function) &dll_luaopen_table},
774 {"lua_pushlightuserdata", (luaV_function) &dll_lua_pushlightuserdata},
775 {"lua_getfield", (luaV_function) &dll_lua_getfield},
776 {"lua_rawget", (luaV_function) &dll_lua_rawget},
777 + {"lua_rawgeti", (luaV_function) &dll_lua_rawgeti},
778 {"lua_createtable", (luaV_function) &dll_lua_createtable},
779 {"lua_newuserdata", (luaV_function) &dll_lua_newuserdata},
780 {"lua_getmetatable", (luaV_function) &dll_lua_getmetatable},
781 {"lua_setfield", (luaV_function) &dll_lua_setfield},
782 {"lua_rawset", (luaV_function) &dll_lua_rawset},
783 {"lua_rawseti", (luaV_function) &dll_lua_rawseti},
784 {"lua_setmetatable", (luaV_function) &dll_lua_setmetatable},
786 {"luaopen_base", (luaV_function) &dll_luaopen_base},
787 {"luaopen_table", (luaV_function) &dll_luaopen_table},
792 #endif /* DYNAMIC_LUA */
794 + #if LUA_VERSION_NUM > 501
796 + luaL_typeerror (lua_State *L, int narg, const char *tname)
798 + const char *msg = lua_pushfstring(L, "%s expected, got %s",
799 + tname, luaL_typename(L, narg));
800 + return luaL_argerror(L, narg, msg);
805 /* ======= Internal ======= */
812 luaV_checkudata(lua_State *L, int ud, const char *tname)
814 void *p = luaV_toudata(L, ud, tname);
815 ! if (p == NULL) luaL_typerror(L, ud, tname);
820 luaV_pushtypval(lua_State *L, typval_T *tv)
822 ! if (tv == NULL) luaL_error(L, "null type");
830 + luaV_checkcache(lua_State *L, void *p)
832 + luaV_getudata(L, p);
833 + if (lua_isnil(L, -1)) luaL_error(L, "invalid object");
838 + #define luaV_unbox(L,luatyp,ud) (*((luatyp *) lua_touserdata((L),(ud))))
840 + #define luaV_checkvalid(L,luatyp,ud) \
841 + luaV_checkcache((L), (void *) luaV_unbox((L),luatyp,(ud)))
844 luaV_checkudata(lua_State *L, int ud, const char *tname)
846 void *p = luaV_toudata(L, ud, tname);
847 ! if (p == NULL) luaL_typeerror(L, ud, tname);
852 luaV_pushtypval(lua_State *L, typval_T *tv)
864 lua_pushnumber(L, (lua_Number) tv->vval.v_float);
868 ! list_T *l = tv->vval.v_list;
873 ! lua_pushlightuserdata(L, (void *) l);
874 ! lua_rawget(L, LUA_ENVIRONINDEX);
875 ! if (lua_isnil(L, -1)) /* not interned? */
879 ! lua_pop(L, 1); /* nil */
881 ! lua_pushlightuserdata(L, (void *) l);
882 ! lua_pushvalue(L, -2);
883 ! lua_rawset(L, LUA_ENVIRONINDEX);
884 ! for (li = l->lv_first; li != NULL; li = li->li_next)
886 ! luaV_pushtypval(L, &li->li_tv);
887 ! lua_rawseti(L, -2, ++n);
891 ! else lua_pushnil(L);
895 ! dict_T *d = tv->vval.v_dict;
900 ! lua_pushlightuserdata(L, (void *) d);
901 ! lua_rawget(L, LUA_ENVIRONINDEX);
902 ! if (lua_isnil(L, -1)) /* not interned? */
904 ! hashtab_T *ht = &d->dv_hashtab;
906 ! int n = ht->ht_used; /* remaining items */
907 ! lua_pop(L, 1); /* nil */
909 ! lua_pushlightuserdata(L, (void *) d);
910 ! lua_pushvalue(L, -2);
911 ! lua_rawset(L, LUA_ENVIRONINDEX);
912 ! for (hi = ht->ht_array; n > 0; hi++)
914 ! if (!HASHITEM_EMPTY(hi))
916 ! dictitem_T *di = dict_lookup(hi);
917 ! luaV_pushtypval(L, &di->di_tv);
918 ! lua_setfield(L, -2, (char *) hi->hi_key);
924 - else lua_pushnil(L);
928 ! luaL_error(L, "invalid type");
933 lua_pushnumber(L, (lua_Number) tv->vval.v_float);
937 ! luaV_pushlist(L, tv->vval.v_list);
940 ! luaV_pushdict(L, tv->vval.v_dict);
947 ! /* converts lua value at 'pos' to typval 'tv' */
949 ! luaV_totypval (lua_State *L, int pos, typval_T *tv)
951 ! switch(lua_type(L, pos)) {
953 ! tv->v_type = VAR_NUMBER;
954 ! tv->vval.v_number = (varnumber_T) lua_toboolean(L, pos);
957 ! tv->v_type = VAR_STRING;
958 ! tv->vval.v_string = vim_strsave((char_u *) lua_tostring(L, pos));
962 ! tv->v_type = VAR_FLOAT;
963 ! tv->vval.v_float = (float_T) lua_tonumber(L, pos);
965 ! tv->v_type = VAR_NUMBER;
966 ! tv->vval.v_number = (varnumber_T) lua_tointeger(L, pos);
969 ! case LUA_TUSERDATA: {
970 ! void *p = lua_touserdata(L, pos);
971 ! if (lua_getmetatable(L, pos)) /* has metatable? */
974 ! luaV_getfield(L, LUAVIM_LIST);
975 ! if (lua_rawequal(L, -1, -2))
977 ! tv->v_type = VAR_LIST;
978 ! tv->vval.v_list = *((luaV_List *) p);
979 ! ++tv->vval.v_list->lv_refcount;
980 ! lua_pop(L, 2); /* MTs */
984 ! luaV_getfield(L, LUAVIM_DICT);
985 ! if (lua_rawequal(L, -1, -3))
987 ! tv->v_type = VAR_DICT;
988 ! tv->vval.v_dict = *((luaV_Dict *) p);
989 ! ++tv->vval.v_dict->dv_refcount;
990 ! lua_pop(L, 3); /* MTs */
993 + lua_pop(L, 3); /* MTs */
998 ! tv->v_type = VAR_NUMBER;
999 ! tv->vval.v_number = 0;
1005 lua_pop(L, 2); /* original and modified strings */
1009 ! /* ======= Buffer type ======= */
1011 ! static luaV_Buffer *
1012 ! luaV_newbuffer(lua_State *L, buf_T *buf)
1014 ! luaV_Buffer *b = (luaV_Buffer *) lua_newuserdata(L, sizeof(luaV_Buffer));
1016 ! lua_pushlightuserdata(L, (void *) buf);
1017 ! lua_pushvalue(L, -2);
1018 ! lua_rawset(L, LUA_ENVIRONINDEX); /* env[buf] = udata */
1019 ! /* to avoid GC, store as key in env */
1020 ! lua_pushvalue(L, -1);
1021 ! lua_pushboolean(L, 1);
1022 ! lua_rawset(L, LUA_ENVIRONINDEX); /* env[udata] = true */
1023 ! /* set metatable */
1024 ! luaV_getfield(L, LUAVIM_BUFFER);
1025 lua_setmetatable(L, -2);
1029 ! static luaV_Buffer *
1030 ! luaV_pushbuffer (lua_State *L, buf_T *buf)
1032 ! luaV_Buffer *b = NULL;
1036 ! lua_pushlightuserdata(L, (void *) buf);
1037 ! lua_rawget(L, LUA_ENVIRONINDEX);
1038 ! if (lua_isnil(L, -1)) /* not interned? */
1041 ! b = luaV_newbuffer(L, buf);
1044 ! b = (luaV_Buffer *) lua_touserdata(L, -1);
1049 ! /* Buffer metamethods */
1052 ! luaV_buffer_tostring(lua_State *L)
1054 ! lua_pushfstring(L, "%s: %p", LUAVIM_BUFFER, lua_touserdata(L, 1));
1059 luaV_buffer_len(lua_State *L)
1061 ! luaV_Buffer *b = lua_touserdata(L, 1);
1062 ! lua_pushinteger(L, (*b)->b_ml.ml_line_count);
1067 luaV_buffer_call(lua_State *L)
1069 ! luaV_Buffer *b = (luaV_Buffer *) lua_touserdata(L, 1);
1071 ! set_curbuf(*b, DOBUF_SPLIT);
1076 luaV_buffer_index(lua_State *L)
1078 ! luaV_Buffer *b = (luaV_Buffer *) lua_touserdata(L, 1);
1079 linenr_T n = (linenr_T) lua_tointeger(L, 2);
1080 ! if (n > 0 && n <= (*b)->b_ml.ml_line_count)
1081 ! luaV_pushline(L, *b, n);
1082 else if (lua_isstring(L, 2))
1084 const char *s = lua_tostring(L, 2);
1085 if (strncmp(s, "name", 4) == 0)
1086 ! lua_pushstring(L, (char *) (*b)->b_sfname);
1087 else if (strncmp(s, "fname", 5) == 0)
1088 ! lua_pushstring(L, (char *) (*b)->b_ffname);
1089 else if (strncmp(s, "number", 6) == 0)
1090 ! lua_pushinteger(L, (*b)->b_fnum);
1092 else if (strncmp(s, "insert", 6) == 0
1093 || strncmp(s, "next", 4) == 0
1095 lua_pop(L, 2); /* original and modified strings */
1098 + #define luaV_newtype(typ,tname,luatyp,luatname) \
1100 + luaV_new##tname (lua_State *L, typ *obj) \
1102 + luatyp *o = (luatyp *) lua_newuserdata(L, sizeof(luatyp)); \
1104 + luaV_setudata(L, obj); /* cache[obj] = udata */ \
1105 + luaV_getfield(L, luatname); \
1106 + lua_setmetatable(L, -2); \
1110 + #define luaV_pushtype(typ,tname,luatyp) \
1112 + luaV_push##tname (lua_State *L, typ *obj) \
1114 + luatyp *o = NULL; \
1115 + if (obj == NULL) \
1118 + luaV_getudata(L, obj); \
1119 + if (lua_isnil(L, -1)) /* not interned? */ \
1122 + o = luaV_new##tname(L, obj); \
1125 + o = (luatyp *) lua_touserdata(L, -1); \
1130 + #define luaV_type_tostring(tname,luatname) \
1132 + luaV_##tname##_tostring (lua_State *L) \
1134 + lua_pushfstring(L, "%s: %p", luatname, lua_touserdata(L, 1)); \
1139 ! /* adapted from eval.c */
1141 ! #define listitem_alloc() (listitem_T *)alloc(sizeof(listitem_T))
1143 ! static listitem_T *
1144 ! list_find (list_T *l, long n)
1147 ! if (l == NULL || n < -l->lv_len || n >= l->lv_len)
1149 ! if (n < 0) /* search backward? */
1150 ! for (li = l->lv_last; n < -1; li = li->li_prev)
1152 ! else /* search forward */
1153 ! for (li = l->lv_first; n > 0; li = li->li_next)
1159 ! list_remove (list_T *l, listitem_T *li)
1163 ! /* fix watchers */
1164 ! for (lw = l->lv_watch; lw != NULL; lw = lw->lw_next)
1165 ! if (lw->lw_item == li)
1166 ! lw->lw_item = li->li_next;
1167 ! /* fix list pointers */
1168 ! if (li->li_next == NULL) /* last? */
1169 ! l->lv_last = li->li_prev;
1171 ! li->li_next->li_prev = li->li_prev;
1172 ! if (li->li_prev == NULL) /* first? */
1173 ! l->lv_first = li->li_next;
1175 ! li->li_prev->li_next = li->li_next;
1176 ! l->lv_idx_item = NULL;
1180 ! list_append(list_T *l, listitem_T *item)
1182 ! if (l->lv_last == NULL) /* empty list? */
1183 ! l->lv_first = item;
1185 ! l->lv_last->li_next = item;
1186 ! item->li_prev = l->lv_last;
1187 ! item->li_next = NULL;
1188 ! l->lv_last = item;
1193 ! list_insert_tv(list_T *l, typval_T *tv, listitem_T *item)
1195 ! listitem_T *ni = listitem_alloc();
1199 ! copy_tv(tv, &ni->li_tv);
1201 ! list_append(l, ni);
1204 ! ni->li_prev = item->li_prev;
1205 ! ni->li_next = item;
1206 ! if (item->li_prev == NULL)
1213 ! item->li_prev->li_next = ni;
1214 ! l->lv_idx_item = NULL;
1216 ! item->li_prev = ni;
1222 ! /* set references */
1224 ! static void set_ref_in_tv (typval_T *tv, int copyID);
1227 ! set_ref_in_dict(dict_T *d, int copyID)
1229 ! hashtab_T *ht = &d->dv_hashtab;
1230 ! int n = ht->ht_used;
1232 ! for (hi = ht->ht_array; n > 0; ++hi)
1233 ! if (!HASHITEM_EMPTY(hi))
1235 ! dictitem_T *di = dict_lookup(hi);
1236 ! set_ref_in_tv(&di->di_tv, copyID);
1242 ! set_ref_in_list(list_T *l, int copyID)
1245 ! for (li = l->lv_first; li != NULL; li = li->li_next)
1246 ! set_ref_in_tv(&li->li_tv, copyID);
1250 ! set_ref_in_tv(typval_T *tv, int copyID)
1252 ! if (tv->v_type == VAR_LIST)
1254 ! list_T *l = tv->vval.v_list;
1255 ! if (l != NULL && l->lv_copyID != copyID)
1257 ! l->lv_copyID = copyID;
1258 ! set_ref_in_list(l, copyID);
1261 ! else if (tv->v_type == VAR_DICT)
1263 ! dict_T *d = tv->vval.v_dict;
1264 ! if (d != NULL && d->dv_copyID != copyID)
1266 ! d->dv_copyID = copyID;
1267 ! set_ref_in_dict(d, copyID);
1273 ! /* ======= List type ======= */
1275 ! static luaV_List *
1276 ! luaV_newlist (lua_State *L, list_T *lis)
1278 ! luaV_List *l = (luaV_List *) lua_newuserdata(L, sizeof(luaV_List));
1280 ! lis->lv_refcount++; /* reference in Lua */
1281 ! luaV_setudata(L, lis); /* cache[lis] = udata */
1282 ! luaV_getfield(L, LUAVIM_LIST);
1283 lua_setmetatable(L, -2);
1287 ! luaV_pushtype(list_T, list, luaV_List)
1288 ! luaV_type_tostring(list, LUAVIM_LIST)
1291 ! luaV_list_gc (lua_State *L)
1293 ! list_unref(luaV_unbox(L, luaV_List, 1));
1298 ! luaV_list_len (lua_State *L)
1300 ! list_T *l = luaV_unbox(L, luaV_List, 1);
1301 ! lua_pushinteger(L, (l == NULL) ? 0 : (int) l->lv_len);
1306 ! luaV_list_iter (lua_State *L)
1308 ! listitem_T *li = (listitem_T *) lua_touserdata(L, lua_upvalueindex(2));
1309 ! if (li == NULL) return 0;
1310 ! luaV_pushtypval(L, &li->li_tv);
1311 ! lua_pushlightuserdata(L, (void *) li->li_next);
1312 ! lua_replace(L, lua_upvalueindex(2));
1317 ! luaV_list_call (lua_State *L)
1319 ! list_T *l = luaV_unbox(L, luaV_List, 1);
1320 ! lua_pushvalue(L, lua_upvalueindex(1)); /* pass cache table along */
1321 ! lua_pushlightuserdata(L, (void *) l->lv_first);
1322 ! lua_pushcclosure(L, luaV_list_iter, 2);
1327 ! luaV_list_index (lua_State *L)
1329 ! list_T *l = luaV_unbox(L, luaV_List, 1);
1330 ! if (lua_isnumber(L, 2)) /* list item? */
1332 ! listitem_T *li = list_find(l, (long) luaL_checkinteger(L, 2));
1336 ! luaV_pushtypval(L, &li->li_tv);
1338 ! else if (lua_isstring(L, 2)) /* method? */
1340 ! const char *s = lua_tostring(L, 2);
1341 ! if (strncmp(s, "add", 3) == 0
1342 ! || strncmp(s, "insert", 6) == 0
1343 ! || strncmp(s, "extend", 6) == 0)
1345 ! lua_getmetatable(L, 1);
1346 ! lua_getfield(L, -1, s);
1357 ! luaV_list_newindex (lua_State *L)
1359 ! list_T *l = luaV_unbox(L, luaV_List, 1);
1360 ! long n = (long) luaL_checkinteger(L, 2);
1363 ! luaL_error(L, "list is locked");
1364 ! li = list_find(l, n);
1365 ! if (li == NULL) return 0;
1366 ! if (lua_isnil(L, 3)) /* remove? */
1368 ! list_remove(l, li);
1369 ! clear_tv(&li->li_tv);
1375 ! luaV_totypval(L, 3, &v);
1376 ! clear_tv(&li->li_tv);
1377 ! copy_tv(&v, &li->li_tv);
1383 ! luaV_list_add (lua_State *L)
1385 ! luaV_List *lis = luaV_checkudata(L, 1, LUAVIM_LIST);
1386 ! list_T *l = (list_T *) luaV_checkcache(L, (void *) *lis);
1389 ! luaL_error(L, "list is locked");
1390 ! li = listitem_alloc();
1395 ! luaV_totypval(L, 2, &v);
1396 ! copy_tv(&v, &li->li_tv);
1397 ! list_append(l, li);
1404 + luaV_list_insert (lua_State *L)
1406 + luaV_List *lis = luaV_checkudata(L, 1, LUAVIM_LIST);
1407 + list_T *l = (list_T *) luaV_checkcache(L, (void *) *lis);
1408 + long pos = luaL_optlong(L, 3, 0);
1409 + listitem_T *li = NULL;
1412 + luaL_error(L, "list is locked");
1413 + if (pos < l->lv_len)
1415 + li = list_find(l, pos);
1417 + luaL_error(L, "invalid position");
1420 + luaV_totypval(L, 2, &v);
1421 + list_insert_tv(l, &v, li);
1426 + static const luaL_Reg luaV_List_mt[] = {
1427 + {"__tostring", luaV_list_tostring},
1428 + {"__gc", luaV_list_gc},
1429 + {"__len", luaV_list_len},
1430 + {"__call", luaV_list_call},
1431 + {"__index", luaV_list_index},
1432 + {"__newindex", luaV_list_newindex},
1433 + {"add", luaV_list_add},
1434 + {"insert", luaV_list_insert},
1439 + /* ======= Dict type ======= */
1441 + static luaV_Dict *
1442 + luaV_newdict (lua_State *L, dict_T *dic)
1444 + luaV_Dict *d = (luaV_Dict *) lua_newuserdata(L, sizeof(luaV_Dict));
1446 + dic->dv_refcount++; /* reference in Lua */
1447 + luaV_setudata(L, dic); /* cache[dic] = udata */
1448 + luaV_getfield(L, LUAVIM_DICT);
1449 + lua_setmetatable(L, -2);
1453 + luaV_pushtype(dict_T, dict, luaV_Dict)
1454 + luaV_type_tostring(dict, LUAVIM_DICT)
1457 + luaV_dict_gc (lua_State *L)
1459 + dict_unref(luaV_unbox(L, luaV_Dict, 1));
1464 + luaV_dict_len (lua_State *L)
1466 + dict_T *d = luaV_unbox(L, luaV_Dict, 1);
1467 + lua_pushinteger(L, (d == NULL) ? 0 : (int) d->dv_hashtab.ht_used);
1472 + luaV_dict_iter (lua_State *L)
1474 + hashitem_T *hi = (hashitem_T *) lua_touserdata(L, lua_upvalueindex(2));
1475 + int n = lua_tointeger(L, lua_upvalueindex(3));
1477 + if (n <= 0) return 0;
1478 + while (HASHITEM_EMPTY(hi)) hi++;
1479 + di = dict_lookup(hi);
1480 + lua_pushstring(L, (char *) hi->hi_key);
1481 + luaV_pushtypval(L, &di->di_tv);
1482 + lua_pushlightuserdata(L, (void *) (hi + 1));
1483 + lua_replace(L, lua_upvalueindex(2));
1484 + lua_pushinteger(L, n - 1);
1485 + lua_replace(L, lua_upvalueindex(3));
1490 + luaV_dict_call (lua_State *L)
1492 + dict_T *d = luaV_unbox(L, luaV_Dict, 1);
1493 + hashtab_T *ht = &d->dv_hashtab;
1494 + lua_pushvalue(L, lua_upvalueindex(1)); /* pass cache table along */
1495 + lua_pushlightuserdata(L, (void *) ht->ht_array);
1496 + lua_pushinteger(L, ht->ht_used); /* # remaining items */
1497 + lua_pushcclosure(L, luaV_dict_iter, 3);
1502 + luaV_dict_index (lua_State *L)
1504 + dict_T *d = luaV_unbox(L, luaV_Dict, 1);
1505 + char_u *key = (char_u *) luaL_checkstring(L, 2);
1506 + dictitem_T *di = dict_find(d, key, -1);
1510 + luaV_pushtypval(L, &di->di_tv);
1515 + luaV_dict_newindex (lua_State *L)
1517 + dict_T *d = luaV_unbox(L, luaV_Dict, 1);
1518 + char_u *key = (char_u *) luaL_checkstring(L, 2);
1521 + luaL_error(L, "dict is locked");
1522 + di = dict_find(d, key, -1);
1523 + if (di == NULL) /* non-existing key? */
1525 + if (lua_isnil(L, 3)) return 0;
1526 + di = dictitem_alloc(key);
1527 + if (di == NULL) return 0;
1528 + if (dict_add(d, di) == FAIL)
1535 + clear_tv(&di->di_tv);
1536 + if (lua_isnil(L, 3)) /* remove? */
1538 + hashitem_T *hi = hash_find(&d->dv_hashtab, di->di_key);
1539 + hash_remove(&d->dv_hashtab, hi);
1540 + dictitem_free(di);
1544 + luaV_totypval(L, 3, &v);
1545 + copy_tv(&v, &di->di_tv);
1550 + static const luaL_Reg luaV_Dict_mt[] = {
1551 + {"__tostring", luaV_dict_tostring},
1552 + {"__gc", luaV_dict_gc},
1553 + {"__len", luaV_dict_len},
1554 + {"__call", luaV_dict_call},
1555 + {"__index", luaV_dict_index},
1556 + {"__newindex", luaV_dict_newindex},
1561 + /* ======= Buffer type ======= */
1563 + luaV_newtype(buf_T, buffer, luaV_Buffer, LUAVIM_BUFFER)
1564 + luaV_pushtype(buf_T, buffer, luaV_Buffer)
1565 + luaV_type_tostring(buffer, LUAVIM_BUFFER)
1568 luaV_buffer_len(lua_State *L)
1570 ! buf_T *b = (buf_T *) luaV_checkvalid(L, luaV_Buffer, 1);
1571 ! lua_pushinteger(L, b->b_ml.ml_line_count);
1576 luaV_buffer_call(lua_State *L)
1578 ! buf_T *b = (buf_T *) luaV_checkvalid(L, luaV_Buffer, 1);
1580 ! set_curbuf(b, DOBUF_SPLIT);
1585 luaV_buffer_index(lua_State *L)
1587 ! buf_T *b = (buf_T *) luaV_checkvalid(L, luaV_Buffer, 1);
1588 linenr_T n = (linenr_T) lua_tointeger(L, 2);
1589 ! if (n > 0 && n <= b->b_ml.ml_line_count)
1590 ! luaV_pushline(L, b, n);
1591 else if (lua_isstring(L, 2))
1593 const char *s = lua_tostring(L, 2);
1594 if (strncmp(s, "name", 4) == 0)
1595 ! lua_pushstring(L, (char *) b->b_sfname);
1596 else if (strncmp(s, "fname", 5) == 0)
1597 ! lua_pushstring(L, (char *) b->b_ffname);
1598 else if (strncmp(s, "number", 6) == 0)
1599 ! lua_pushinteger(L, b->b_fnum);
1601 else if (strncmp(s, "insert", 6) == 0
1602 || strncmp(s, "next", 4) == 0
1606 luaV_buffer_newindex(lua_State *L)
1608 ! luaV_Buffer *b = (luaV_Buffer *) lua_touserdata(L, 1);
1609 linenr_T n = (linenr_T) luaL_checkinteger(L, 2);
1611 luaV_checksandbox(L);
1613 ! if (n < 1 || n > (*b)->b_ml.ml_line_count)
1614 luaL_error(L, "invalid line number");
1615 if (lua_isnil(L, 3)) /* delete line */
1617 buf_T *buf = curbuf;
1619 if (u_savedel(n, 1L) == FAIL)
1624 luaV_buffer_newindex(lua_State *L)
1626 ! buf_T *b = (buf_T *) luaV_checkvalid(L, luaV_Buffer, 1);
1627 linenr_T n = (linenr_T) luaL_checkinteger(L, 2);
1629 luaV_checksandbox(L);
1631 ! if (n < 1 || n > b->b_ml.ml_line_count)
1632 luaL_error(L, "invalid line number");
1633 if (lua_isnil(L, 3)) /* delete line */
1635 buf_T *buf = curbuf;
1637 if (u_savedel(n, 1L) == FAIL)
1644 deleted_lines_mark(n, 1L);
1645 ! if (*b == curwin->w_buffer) /* fix cursor in current window? */
1647 if (curwin->w_cursor.lnum >= n)
1652 deleted_lines_mark(n, 1L);
1653 ! if (b == curwin->w_buffer) /* fix cursor in current window? */
1655 if (curwin->w_cursor.lnum >= n)
1659 else if (lua_isstring(L, 3)) /* update line */
1661 buf_T *buf = curbuf;
1663 if (u_savesub(n) == FAIL)
1667 else if (lua_isstring(L, 3)) /* update line */
1669 buf_T *buf = curbuf;
1671 if (u_savesub(n) == FAIL)
1677 else changed_bytes(n, 0);
1679 ! if (*b == curwin->w_buffer)
1685 else changed_bytes(n, 0);
1687 ! if (b == curwin->w_buffer)
1694 luaV_buffer_insert(lua_State *L)
1696 ! luaV_Buffer *b = luaV_checkudata(L, 1, LUAVIM_BUFFER);
1697 ! linenr_T last = (*b)->b_ml.ml_line_count;
1698 linenr_T n = (linenr_T) luaL_optinteger(L, 3, last);
1700 luaL_checktype(L, 2, LUA_TSTRING);
1703 luaV_buffer_insert(lua_State *L)
1705 ! luaV_Buffer *lb = luaV_checkudata(L, 1, LUAVIM_BUFFER);
1706 ! buf_T *b = (buf_T *) luaV_checkcache(L, (void *) *lb);
1707 ! linenr_T last = b->b_ml.ml_line_count;
1708 linenr_T n = (linenr_T) luaL_optinteger(L, 3, last);
1710 luaL_checktype(L, 2, LUA_TSTRING);
1713 if (n > last) n = last;
1717 if (u_save(n, n + 1) == FAIL)
1721 if (n > last) n = last;
1725 if (u_save(n, n + 1) == FAIL)
1730 luaV_buffer_next(lua_State *L)
1732 luaV_Buffer *b = luaV_checkudata(L, 1, LUAVIM_BUFFER);
1733 ! luaV_pushbuffer(L, (*b)->b_next);
1738 luaV_buffer_next(lua_State *L)
1740 luaV_Buffer *b = luaV_checkudata(L, 1, LUAVIM_BUFFER);
1741 ! buf_T *buf = (buf_T *) luaV_checkcache(L, (void *) *b);
1742 ! luaV_pushbuffer(L, buf->b_next);
1748 luaV_buffer_previous(lua_State *L)
1750 luaV_Buffer *b = luaV_checkudata(L, 1, LUAVIM_BUFFER);
1751 ! luaV_pushbuffer(L, (*b)->b_prev);
1756 luaV_buffer_previous(lua_State *L)
1758 luaV_Buffer *b = luaV_checkudata(L, 1, LUAVIM_BUFFER);
1759 ! buf_T *buf = (buf_T *) luaV_checkcache(L, (void *) *b);
1760 ! luaV_pushbuffer(L, buf->b_prev);
1766 luaV_buffer_isvalid(lua_State *L)
1768 luaV_Buffer *b = luaV_checkudata(L, 1, LUAVIM_BUFFER);
1769 ! lua_pushlightuserdata(L, (void *) (*b));
1770 ! lua_rawget(L, LUA_ENVIRONINDEX);
1771 lua_pushboolean(L, !lua_isnil(L, -1));
1775 luaV_buffer_isvalid(lua_State *L)
1777 luaV_Buffer *b = luaV_checkudata(L, 1, LUAVIM_BUFFER);
1778 ! luaV_getudata(L, *b);
1779 lua_pushboolean(L, !lua_isnil(L, -1));
1785 /* ======= Window type ======= */
1787 ! static luaV_Window *
1788 ! luaV_newwindow(lua_State *L, win_T *win)
1790 ! luaV_Window *w = (luaV_Window *) lua_newuserdata(L, sizeof(luaV_Window));
1792 ! lua_pushlightuserdata(L, (void *) win);
1793 ! lua_pushvalue(L, -2);
1794 ! lua_rawset(L, LUA_ENVIRONINDEX); /* env[win] = udata */
1795 ! /* to avoid GC, store as key in env */
1796 ! lua_pushvalue(L, -1);
1797 ! lua_pushboolean(L, 1);
1798 ! lua_rawset(L, LUA_ENVIRONINDEX); /* env[udata] = true */
1799 ! /* set metatable */
1800 ! luaV_getfield(L, LUAVIM_WINDOW);
1801 ! lua_setmetatable(L, -2);
1805 ! static luaV_Window *
1806 ! luaV_pushwindow(lua_State *L, win_T *win)
1808 ! luaV_Window *w = NULL;
1812 ! lua_pushlightuserdata(L, (void *) win);
1813 ! lua_rawget(L, LUA_ENVIRONINDEX);
1814 ! if (lua_isnil(L, -1)) /* not interned? */
1817 ! w = luaV_newwindow(L, win);
1819 ! else w = (luaV_Window *) lua_touserdata(L, -1);
1824 ! /* Window metamethods */
1827 ! luaV_window_tostring(lua_State *L)
1829 ! lua_pushfstring(L, "%s: %p", LUAVIM_WINDOW, lua_touserdata(L, 1));
1834 luaV_window_call(lua_State *L)
1836 ! luaV_Window *w = (luaV_Window *) lua_touserdata(L, 1);
1843 luaV_window_index(lua_State *L)
1845 ! luaV_Window *w = (luaV_Window *) lua_touserdata(L, 1);
1846 const char *s = luaL_checkstring(L, 2);
1847 if (strncmp(s, "buffer", 6) == 0)
1848 ! luaV_pushbuffer(L, (*w)->w_buffer);
1849 else if (strncmp(s, "line", 4) == 0)
1850 ! lua_pushinteger(L, (*w)->w_cursor.lnum);
1851 else if (strncmp(s, "col", 3) == 0)
1852 ! lua_pushinteger(L, (*w)->w_cursor.col + 1);
1853 #ifdef FEAT_VERTSPLIT
1854 else if (strncmp(s, "width", 5) == 0)
1855 ! lua_pushinteger(L, W_WIDTH((*w)));
1857 else if (strncmp(s, "height", 6) == 0)
1858 ! lua_pushinteger(L, (*w)->w_height);
1860 else if (strncmp(s, "next", 4) == 0
1861 || strncmp(s, "previous", 8) == 0
1864 /* ======= Window type ======= */
1866 ! luaV_newtype(win_T, window, luaV_Window, LUAVIM_WINDOW)
1867 ! luaV_pushtype(win_T, window, luaV_Window)
1868 ! luaV_type_tostring(window, LUAVIM_WINDOW)
1871 luaV_window_call(lua_State *L)
1873 ! win_T *w = (win_T *) luaV_checkvalid(L, luaV_Window, 1);
1880 luaV_window_index(lua_State *L)
1882 ! win_T *w = (win_T *) luaV_checkvalid(L, luaV_Window, 1);
1883 const char *s = luaL_checkstring(L, 2);
1884 if (strncmp(s, "buffer", 6) == 0)
1885 ! luaV_pushbuffer(L, w->w_buffer);
1886 else if (strncmp(s, "line", 4) == 0)
1887 ! lua_pushinteger(L, w->w_cursor.lnum);
1888 else if (strncmp(s, "col", 3) == 0)
1889 ! lua_pushinteger(L, w->w_cursor.col + 1);
1890 #ifdef FEAT_VERTSPLIT
1891 else if (strncmp(s, "width", 5) == 0)
1892 ! lua_pushinteger(L, W_WIDTH(w));
1894 else if (strncmp(s, "height", 6) == 0)
1895 ! lua_pushinteger(L, w->w_height);
1897 else if (strncmp(s, "next", 4) == 0
1898 || strncmp(s, "previous", 8) == 0
1902 luaV_window_newindex (lua_State *L)
1904 ! luaV_Window *w = (luaV_Window *) lua_touserdata(L, 1);
1905 const char *s = luaL_checkstring(L, 2);
1906 int v = luaL_checkinteger(L, 3);
1907 if (strncmp(s, "line", 4) == 0)
1910 luaV_window_newindex (lua_State *L)
1912 ! win_T *w = (win_T *) luaV_checkvalid(L, luaV_Window, 1);
1913 const char *s = luaL_checkstring(L, 2);
1914 int v = luaL_checkinteger(L, 3);
1915 if (strncmp(s, "line", 4) == 0)
1919 luaV_checksandbox(L);
1921 ! if (v < 1 || v > (*w)->w_buffer->b_ml.ml_line_count)
1922 luaL_error(L, "line out of range");
1923 ! (*w)->w_cursor.lnum = v;
1924 update_screen(VALID);
1926 else if (strncmp(s, "col", 3) == 0)
1929 luaV_checksandbox(L);
1931 ! if (v < 1 || v > w->w_buffer->b_ml.ml_line_count)
1932 luaL_error(L, "line out of range");
1933 ! w->w_cursor.lnum = v;
1934 update_screen(VALID);
1936 else if (strncmp(s, "col", 3) == 0)
1940 luaV_checksandbox(L);
1942 ! (*w)->w_cursor.col = v - 1;
1943 update_screen(VALID);
1945 #ifdef FEAT_VERTSPLIT
1948 luaV_checksandbox(L);
1950 ! w->w_cursor.col = v - 1;
1951 update_screen(VALID);
1953 #ifdef FEAT_VERTSPLIT
1957 need_mouse_correct = TRUE;
1965 need_mouse_correct = TRUE;
1974 need_mouse_correct = TRUE;
1982 need_mouse_correct = TRUE;
1990 luaV_window_next(lua_State *L)
1992 luaV_Window *w = luaV_checkudata(L, 1, LUAVIM_WINDOW);
1993 ! luaV_pushwindow(L, (*w)->w_next);
1998 luaV_window_next(lua_State *L)
2000 luaV_Window *w = luaV_checkudata(L, 1, LUAVIM_WINDOW);
2001 ! win_T *win = (win_T *) luaV_checkcache(L, (void *) *w);
2002 ! luaV_pushwindow(L, win->w_next);
2008 luaV_window_previous(lua_State *L)
2010 luaV_Window *w = luaV_checkudata(L, 1, LUAVIM_WINDOW);
2011 ! luaV_pushwindow(L, (*w)->w_prev);
2016 luaV_window_previous(lua_State *L)
2018 luaV_Window *w = luaV_checkudata(L, 1, LUAVIM_WINDOW);
2019 ! win_T *win = (win_T *) luaV_checkcache(L, (void *) *w);
2020 ! luaV_pushwindow(L, win->w_prev);
2026 luaV_window_isvalid(lua_State *L)
2028 luaV_Window *w = luaV_checkudata(L, 1, LUAVIM_WINDOW);
2029 ! lua_pushlightuserdata(L, (void *) (*w));
2030 ! lua_rawget(L, LUA_ENVIRONINDEX);
2031 lua_pushboolean(L, !lua_isnil(L, -1));
2035 luaV_window_isvalid(lua_State *L)
2037 luaV_Window *w = luaV_checkudata(L, 1, LUAVIM_WINDOW);
2038 ! luaV_getudata(L, *w);
2039 lua_pushboolean(L, !lua_isnil(L, -1));
2048 + luaV_list(lua_State *L)
2050 + list_T *l = list_alloc();
2054 + luaV_newlist(L, l);
2059 + luaV_dict(lua_State *L)
2061 + dict_T *d = dict_alloc();
2065 + luaV_newdict(L, d);
2070 luaV_buffer(lua_State *L)
2078 - if (buf == NULL) /* not found? */
2081 - luaV_pushbuffer(L, buf);
2084 buf = (lua_toboolean(L, 1)) ? firstbuf : curbuf; /* first buffer? */
2085 ! luaV_pushbuffer(L, buf);
2096 buf = (lua_toboolean(L, 1)) ? firstbuf : curbuf; /* first buffer? */
2097 ! luaV_pushbuffer(L, buf);
2103 int n = lua_tointeger(L, 1);
2104 for (win = firstwin; win != NULL; win = win->w_next, n--)
2106 - if (win == NULL) /* not found? */
2109 - luaV_pushwindow(L, win);
2112 win = (lua_toboolean(L, 1)) ? firstwin : curwin; /* first window? */
2113 ! luaV_pushwindow(L, win);
2119 int n = lua_tointeger(L, 1);
2120 for (win = firstwin; win != NULL; win = win->w_next, n--)
2124 win = (lua_toboolean(L, 1)) ? firstwin : curwin; /* first window? */
2125 ! luaV_pushwindow(L, win);
2134 ! luaV_isbuffer(lua_State *L)
2136 ! lua_pushboolean(L, luaV_toudata(L, 1, LUAVIM_BUFFER) != NULL);
2141 ! luaV_iswindow(lua_State *L)
2143 ! lua_pushboolean(L, luaV_toudata(L, 1, LUAVIM_WINDOW) != NULL);
2147 ! /* for freeing buffer and window objects; lightuserdata as arg */
2149 ! luaV_free(lua_State *L)
2151 ! lua_pushvalue(L, 1); /* lightudata */
2152 ! lua_rawget(L, LUA_ENVIRONINDEX);
2153 ! if (!lua_isnil(L, -1))
2156 ! lua_rawset(L, LUA_ENVIRONINDEX); /* env[udata] = nil */
2158 ! lua_rawset(L, LUA_ENVIRONINDEX); /* env[lightudata] = nil */
2163 static const luaL_Reg luaV_module[] = {
2168 ! luaV_type(lua_State *L)
2170 ! luaL_checkany(L, 1);
2171 ! if (lua_type(L, 1) == LUA_TUSERDATA) /* check vim udata? */
2174 ! if (lua_getmetatable(L, 1))
2176 ! luaV_getfield(L, LUAVIM_LIST);
2177 ! if (lua_rawequal(L, -1, 2))
2179 ! lua_pushstring(L, "list");
2182 ! luaV_getfield(L, LUAVIM_DICT);
2183 ! if (lua_rawequal(L, -1, 2))
2185 ! lua_pushstring(L, "dict");
2188 ! luaV_getfield(L, LUAVIM_BUFFER);
2189 ! if (lua_rawequal(L, -1, 2))
2191 ! lua_pushstring(L, "buffer");
2194 ! luaV_getfield(L, LUAVIM_WINDOW);
2195 ! if (lua_rawequal(L, -1, 2))
2197 ! lua_pushstring(L, "window");
2202 ! lua_pushstring(L, luaL_typename(L, 1)); /* fallback */
2206 static const luaL_Reg luaV_module[] = {
2209 {"eval", luaV_eval},
2210 {"beep", luaV_beep},
2211 {"line", luaV_line},
2212 {"buffer", luaV_buffer},
2213 {"window", luaV_window},
2214 {"open", luaV_open},
2215 ! {"isbuffer", luaV_isbuffer},
2216 ! {"iswindow", luaV_iswindow},
2221 luaopen_vim(lua_State *L)
2223 ! /* set environment */
2226 ! lua_pushliteral(L, "v");
2227 lua_setfield(L, -2, "__mode");
2228 ! lua_setmetatable(L, -2);
2229 ! lua_replace(L, LUA_ENVIRONINDEX);
2231 lua_pushcfunction(L, luaV_print);
2232 lua_setglobal(L, "print");
2234 {"eval", luaV_eval},
2235 {"beep", luaV_beep},
2236 {"line", luaV_line},
2237 + {"list", luaV_list},
2238 + {"dict", luaV_dict},
2239 {"buffer", luaV_buffer},
2240 {"window", luaV_window},
2241 {"open", luaV_open},
2242 ! {"type", luaV_type},
2246 + /* for freeing list, dict, buffer and window objects; lightuserdata as arg */
2248 + luaV_free(lua_State *L)
2251 + luaV_setudata(L, lua_touserdata(L, 1));
2256 + luaV_luaeval (lua_State *L)
2260 + const char *str = lua_tolstring(L, 1, &l);
2261 + typval_T *arg = (typval_T *) lua_touserdata(L, 2);
2262 + typval_T *rettv = (typval_T *) lua_touserdata(L, 3);
2263 + luaL_buffinit(L, &b);
2264 + luaL_addlstring(&b, LUAVIM_EVALHEADER, sizeof(LUAVIM_EVALHEADER) - 1);
2265 + luaL_addlstring(&b, str, l);
2266 + luaL_pushresult(&b);
2267 + str = lua_tolstring(L, -1, &l);
2268 + if (luaL_loadbuffer(L, str, l, LUAVIM_EVALNAME)) /* compile error? */
2273 + luaV_pushtypval(L, arg);
2274 + if (lua_pcall(L, 1, 1, 0)) /* running error? */
2279 + luaV_totypval(L, -1, rettv);
2284 + luaV_setref (lua_State *L)
2286 + int copyID = lua_tointeger(L, 1);
2288 + luaV_getfield(L, LUAVIM_LIST);
2289 + luaV_getfield(L, LUAVIM_DICT);
2291 + while (lua_next(L, lua_upvalueindex(1)) != 0) /* traverse cache table */
2293 + lua_getmetatable(L, -1);
2294 + if (lua_rawequal(L, -1, 2)) /* list? */
2296 + tv.v_type = VAR_LIST;
2297 + tv.vval.v_list = (list_T *) lua_touserdata(L, 4); /* key */
2299 + else if (lua_rawequal(L, -1, 3)) /* dict? */
2301 + tv.v_type = VAR_DICT;
2302 + tv.vval.v_dict = (dict_T *) lua_touserdata(L, 4); /* key */
2304 + lua_pop(L, 2); /* metatable and value */
2305 + set_ref_in_tv(&tv, copyID);
2311 luaopen_vim(lua_State *L)
2313 ! /* set cache table */
2316 ! lua_pushstring(L, "v");
2317 lua_setfield(L, -2, "__mode");
2318 ! lua_setmetatable(L, -2); /* cache is weak-valued */
2320 lua_pushcfunction(L, luaV_print);
2321 lua_setglobal(L, "print");
2326 lua_pushlightuserdata(L, (void *) LUAVIM_FREE);
2327 ! lua_pushcfunction(L, luaV_free);
2328 lua_rawset(L, LUA_REGISTRYINDEX);
2330 luaV_newmetatable(L, LUAVIM_BUFFER);
2331 ! luaL_register(L, NULL, luaV_Buffer_mt);
2332 luaV_newmetatable(L, LUAVIM_WINDOW);
2333 ! luaL_register(L, NULL, luaV_Window_mt);
2334 ! luaL_register(L, LUAVIM_NAME, luaV_module);
2341 lua_pushlightuserdata(L, (void *) LUAVIM_FREE);
2342 ! lua_pushvalue(L, 1); /* cache table */
2343 ! lua_pushcclosure(L, luaV_free, 1);
2344 ! lua_rawset(L, LUA_REGISTRYINDEX);
2346 ! lua_pushlightuserdata(L, (void *) LUAVIM_LUAEVAL);
2347 ! lua_pushvalue(L, 1); /* cache table */
2348 ! lua_pushcclosure(L, luaV_luaeval, 1);
2349 ! lua_rawset(L, LUA_REGISTRYINDEX);
2351 ! lua_pushlightuserdata(L, (void *) LUAVIM_SETREF);
2352 ! lua_pushvalue(L, 1); /* cache table */
2353 ! lua_pushcclosure(L, luaV_setref, 1);
2354 lua_rawset(L, LUA_REGISTRYINDEX);
2356 + luaV_newmetatable(L, LUAVIM_LIST);
2357 + lua_pushvalue(L, 1);
2358 + luaV_openlib(L, luaV_List_mt, 1);
2359 + luaV_newmetatable(L, LUAVIM_DICT);
2360 + lua_pushvalue(L, 1);
2361 + luaV_openlib(L, luaV_Dict_mt, 1);
2362 luaV_newmetatable(L, LUAVIM_BUFFER);
2363 ! lua_pushvalue(L, 1); /* cache table */
2364 ! luaV_openlib(L, luaV_Buffer_mt, 1);
2365 luaV_newmetatable(L, LUAVIM_WINDOW);
2366 ! lua_pushvalue(L, 1); /* cache table */
2367 ! luaV_openlib(L, luaV_Window_mt, 1);
2368 ! lua_newtable(L); /* vim table */
2369 ! lua_pushvalue(L, 1); /* cache table */
2370 ! luaV_openlib(L, luaV_module, 1);
2371 ! lua_setglobal(L, LUAVIM_NAME);
2377 static lua_State *L = NULL;
2385 static lua_State *L = NULL;
2400 if (!lua_enabled(TRUE))
2405 ! if (!lua_isopen())
2408 if (!lua_enabled(TRUE))
2433 ! lua_buffer_free(buf_T *buf)
2435 ! if (!lua_is_open()) return;
2436 ! luaV_getfield(L, LUAVIM_FREE);
2437 ! lua_pushlightuserdata(L, (void *) buf);
2438 ! lua_call(L, 1, 0);
2443 ! lua_window_free(win_T *win)
2445 ! if (!lua_is_open()) return;
2446 ! luaV_getfield(L, LUAVIM_FREE);
2447 ! lua_pushlightuserdata(L, (void *) win);
2455 ! #define luaV_freetype(typ,tname) \
2457 ! lua_##tname##_free(typ *o) \
2459 ! if (!lua_isopen()) return; \
2460 ! luaV_getfield(L, LUAVIM_FREE); \
2461 ! lua_pushlightuserdata(L, (void *) o); \
2462 ! lua_call(L, 1, 0); \
2465 ! luaV_freetype(buf_T, buffer)
2466 ! luaV_freetype(win_T, window)
2469 ! do_luaeval (char_u *str, typval_T *arg, typval_T *rettv)
2472 ! luaV_getfield(L, LUAVIM_LUAEVAL);
2473 ! lua_pushstring(L, (char *) str);
2474 ! lua_pushlightuserdata(L, (void *) arg);
2475 ! lua_pushlightuserdata(L, (void *) rettv);
2476 ! lua_call(L, 3, 0);
2480 ! set_ref_in_lua (int copyID)
2482 ! if (!lua_isopen()) return;
2483 ! luaV_getfield(L, LUAVIM_SETREF);
2484 ! lua_pushinteger(L, copyID);
2488 *** ../vim-7.3.489/src/proto/if_lua.pro 2010-08-15 21:57:28.000000000 +0200
2489 --- src/proto/if_lua.pro 2012-04-05 16:41:35.000000000 +0200
2493 void ex_luafile __ARGS((exarg_T *eap));
2494 void lua_buffer_free __ARGS((buf_T *buf));
2495 void lua_window_free __ARGS((win_T *win));
2496 + void do_luaeval __ARGS((char_u *str, typval_T *arg, typval_T *rettv));
2497 + void set_ref_in_lua __ARGS((int copyID));
2498 /* vim: set ft=c : */
2499 *** ../vim-7.3.489/src/version.c 2012-04-05 16:07:01.000000000 +0200
2500 --- src/version.c 2012-04-05 16:52:08.000000000 +0200
2504 { /* Add new patch number below this line */
2510 Even got a Datapoint 3600(?) with a DD50 connector instead of the
2511 usual DB25... what a nightmare trying to figure out the pinout
2512 for *that* with no spex...
2514 /// Bram Moolenaar -- Bram@Moolenaar.net -- http://www.Moolenaar.net \\\
2515 /// sponsor Vim, vote for features -- http://www.Vim.org/sponsor/ \\\
2516 \\\ an exciting new programming language -- http://www.Zimbu.org ///
2517 \\\ help me help AIDS victims -- http://ICCF-Holland.org ///