xref: /vim-8.2.3635/src/if_lua.c (revision 3d1d6475)
1 /* vi:set ts=8 sts=4 sw=4 noet:
2  *
3  * VIM - Vi IMproved	by Bram Moolenaar
4  *
5  * Lua interface by Luis Carvalho
6  *
7  * Do ":help uganda"  in Vim to read copying and usage conditions.
8  * Do ":help credits" in Vim to see a list of people who contributed.
9  * See README.txt for an overview of the Vim source code.
10  */
11 
12 #include "vim.h"
13 
14 #include <lua.h>
15 #include <lualib.h>
16 #include <lauxlib.h>
17 
18 /* Only do the following when the feature is enabled.  Needed for "make
19  * depend". */
20 #if defined(FEAT_LUA) || defined(PROTO)
21 
22 #define LUAVIM_CHUNKNAME "vim chunk"
23 #define LUAVIM_NAME "vim"
24 #define LUAVIM_EVALNAME "luaeval"
25 #define LUAVIM_EVALHEADER "local _A=select(1,...) return "
26 
27 typedef buf_T *luaV_Buffer;
28 typedef win_T *luaV_Window;
29 typedef dict_T *luaV_Dict;
30 typedef list_T *luaV_List;
31 typedef struct {
32     typval_T	tv;	// funcref
33     typval_T	args;
34     dict_T	*self;	// selfdict
35 } luaV_Funcref;
36 typedef void (*msgfunc_T)(char_u *);
37 
38 static const char LUAVIM_DICT[] = "dict";
39 static const char LUAVIM_LIST[] = "list";
40 static const char LUAVIM_FUNCREF[] = "funcref";
41 static const char LUAVIM_BUFFER[] = "buffer";
42 static const char LUAVIM_WINDOW[] = "window";
43 static const char LUAVIM_FREE[] = "luaV_free";
44 static const char LUAVIM_LUAEVAL[] = "luaV_luaeval";
45 static const char LUAVIM_SETREF[] = "luaV_setref";
46 
47 /* most functions are closures with a cache table as first upvalue;
48  * get/setudata manage references to vim userdata in cache table through
49  * object pointers (light userdata) */
50 #define luaV_getudata(L, v) \
51     lua_pushlightuserdata((L), (void *) (v)); \
52     lua_rawget((L), lua_upvalueindex(1))
53 #define luaV_setudata(L, v) \
54     lua_pushlightuserdata((L), (void *) (v)); \
55     lua_pushvalue((L), -2); \
56     lua_rawset((L), lua_upvalueindex(1))
57 #define luaV_getfield(L, s) \
58     lua_pushlightuserdata((L), (void *)(s)); \
59     lua_rawget((L), LUA_REGISTRYINDEX)
60 #define luaV_checksandbox(L) \
61     if (sandbox) luaL_error((L), "not allowed in sandbox")
62 #define luaV_msg(L) luaV_msgfunc((L), (msgfunc_T) msg)
63 #define luaV_emsg(L) luaV_msgfunc((L), (msgfunc_T) emsg)
64 #define luaV_checktypval(L, a, v, msg) \
65     do { \
66         if (luaV_totypval(L, a, v) == FAIL) \
67 	    luaL_error(L, msg ": cannot convert value"); \
68     } while (0)
69 
70 static luaV_List *luaV_pushlist(lua_State *L, list_T *lis);
71 static luaV_Dict *luaV_pushdict(lua_State *L, dict_T *dic);
72 static luaV_Funcref *luaV_pushfuncref(lua_State *L, typval_T *tv);
73 
74 #if LUA_VERSION_NUM <= 501
75 #define luaV_openlib(L, l, n) luaL_openlib(L, NULL, l, n)
76 #define luaL_typeerror luaL_typerror
77 #else
78 #define luaV_openlib luaL_setfuncs
79 #endif
80 
81 #ifdef DYNAMIC_LUA
82 
83 #ifndef WIN3264
84 # include <dlfcn.h>
85 # define HANDLE void*
86 # define load_dll(n) dlopen((n), RTLD_LAZY|RTLD_GLOBAL)
87 # define symbol_from_dll dlsym
88 # define close_dll dlclose
89 #else
90 # define load_dll vimLoadLib
91 # define symbol_from_dll GetProcAddress
92 # define close_dll FreeLibrary
93 #endif
94 
95 /* lauxlib */
96 #if LUA_VERSION_NUM <= 501
97 #define luaL_register dll_luaL_register
98 #define luaL_prepbuffer dll_luaL_prepbuffer
99 #define luaL_openlib dll_luaL_openlib
100 #define luaL_typerror dll_luaL_typerror
101 #define luaL_loadfile dll_luaL_loadfile
102 #define luaL_loadbuffer dll_luaL_loadbuffer
103 #else
104 #define luaL_prepbuffsize dll_luaL_prepbuffsize
105 #define luaL_setfuncs dll_luaL_setfuncs
106 #define luaL_loadfilex dll_luaL_loadfilex
107 #define luaL_loadbufferx dll_luaL_loadbufferx
108 #define luaL_argerror dll_luaL_argerror
109 #endif
110 #define luaL_checkany dll_luaL_checkany
111 #define luaL_checklstring dll_luaL_checklstring
112 #define luaL_checkinteger dll_luaL_checkinteger
113 #define luaL_optinteger dll_luaL_optinteger
114 #define luaL_checktype dll_luaL_checktype
115 #define luaL_error dll_luaL_error
116 #define luaL_newstate dll_luaL_newstate
117 #define luaL_buffinit dll_luaL_buffinit
118 #define luaL_addlstring dll_luaL_addlstring
119 #define luaL_pushresult dll_luaL_pushresult
120 /* lua */
121 #if LUA_VERSION_NUM <= 501
122 #define lua_tonumber dll_lua_tonumber
123 #define lua_tointeger dll_lua_tointeger
124 #define lua_call dll_lua_call
125 #define lua_pcall dll_lua_pcall
126 #else
127 #define lua_tonumberx dll_lua_tonumberx
128 #define lua_tointegerx dll_lua_tointegerx
129 #define lua_callk dll_lua_callk
130 #define lua_pcallk dll_lua_pcallk
131 #define lua_getglobal dll_lua_getglobal
132 #define lua_setglobal dll_lua_setglobal
133 #endif
134 #if LUA_VERSION_NUM <= 502
135 #define lua_replace dll_lua_replace
136 #define lua_remove dll_lua_remove
137 #endif
138 #if LUA_VERSION_NUM >= 503
139 #define lua_rotate dll_lua_rotate
140 #define lua_copy dll_lua_copy
141 #endif
142 #define lua_typename dll_lua_typename
143 #define lua_close dll_lua_close
144 #define lua_gettop dll_lua_gettop
145 #define lua_settop dll_lua_settop
146 #define lua_pushvalue dll_lua_pushvalue
147 #define lua_isnumber dll_lua_isnumber
148 #define lua_isstring dll_lua_isstring
149 #define lua_type dll_lua_type
150 #define lua_rawequal dll_lua_rawequal
151 #define lua_toboolean dll_lua_toboolean
152 #define lua_tolstring dll_lua_tolstring
153 #define lua_touserdata dll_lua_touserdata
154 #define lua_pushnil dll_lua_pushnil
155 #define lua_pushnumber dll_lua_pushnumber
156 #define lua_pushinteger dll_lua_pushinteger
157 #define lua_pushlstring dll_lua_pushlstring
158 #define lua_pushstring dll_lua_pushstring
159 #define lua_pushfstring dll_lua_pushfstring
160 #define lua_pushcclosure dll_lua_pushcclosure
161 #define lua_pushboolean dll_lua_pushboolean
162 #define lua_pushlightuserdata dll_lua_pushlightuserdata
163 #define lua_getfield dll_lua_getfield
164 #define lua_rawget dll_lua_rawget
165 #define lua_rawgeti dll_lua_rawgeti
166 #define lua_createtable dll_lua_createtable
167 #define lua_newuserdata dll_lua_newuserdata
168 #define lua_getmetatable dll_lua_getmetatable
169 #define lua_setfield dll_lua_setfield
170 #define lua_rawset dll_lua_rawset
171 #define lua_rawseti dll_lua_rawseti
172 #define lua_setmetatable dll_lua_setmetatable
173 #define lua_next dll_lua_next
174 /* libs */
175 #define luaopen_base dll_luaopen_base
176 #define luaopen_table dll_luaopen_table
177 #define luaopen_string dll_luaopen_string
178 #define luaopen_math dll_luaopen_math
179 #define luaopen_io dll_luaopen_io
180 #define luaopen_os dll_luaopen_os
181 #define luaopen_package dll_luaopen_package
182 #define luaopen_debug dll_luaopen_debug
183 #define luaL_openlibs dll_luaL_openlibs
184 
185 /* lauxlib */
186 #if LUA_VERSION_NUM <= 501
187 void (*dll_luaL_register) (lua_State *L, const char *libname, const luaL_Reg *l);
188 char *(*dll_luaL_prepbuffer) (luaL_Buffer *B);
189 void (*dll_luaL_openlib) (lua_State *L, const char *libname, const luaL_Reg *l, int nup);
190 int (*dll_luaL_typerror) (lua_State *L, int narg, const char *tname);
191 int (*dll_luaL_loadfile) (lua_State *L, const char *filename);
192 int (*dll_luaL_loadbuffer) (lua_State *L, const char *buff, size_t sz, const char *name);
193 #else
194 char *(*dll_luaL_prepbuffsize) (luaL_Buffer *B, size_t sz);
195 void (*dll_luaL_setfuncs) (lua_State *L, const luaL_Reg *l, int nup);
196 int (*dll_luaL_loadfilex) (lua_State *L, const char *filename, const char *mode);
197 int (*dll_luaL_loadbufferx) (lua_State *L, const char *buff, size_t sz, const char *name, const char *mode);
198 int (*dll_luaL_argerror) (lua_State *L, int numarg, const char *extramsg);
199 #endif
200 void (*dll_luaL_checkany) (lua_State *L, int narg);
201 const char *(*dll_luaL_checklstring) (lua_State *L, int numArg, size_t *l);
202 lua_Integer (*dll_luaL_checkinteger) (lua_State *L, int numArg);
203 lua_Integer (*dll_luaL_optinteger) (lua_State *L, int nArg, lua_Integer def);
204 void (*dll_luaL_checktype) (lua_State *L, int narg, int t);
205 int (*dll_luaL_error) (lua_State *L, const char *fmt, ...);
206 lua_State *(*dll_luaL_newstate) (void);
207 void (*dll_luaL_buffinit) (lua_State *L, luaL_Buffer *B);
208 void (*dll_luaL_addlstring) (luaL_Buffer *B, const char *s, size_t l);
209 void (*dll_luaL_pushresult) (luaL_Buffer *B);
210 /* lua */
211 #if LUA_VERSION_NUM <= 501
212 lua_Number (*dll_lua_tonumber) (lua_State *L, int idx);
213 lua_Integer (*dll_lua_tointeger) (lua_State *L, int idx);
214 void (*dll_lua_call) (lua_State *L, int nargs, int nresults);
215 int (*dll_lua_pcall) (lua_State *L, int nargs, int nresults, int errfunc);
216 #else
217 lua_Number (*dll_lua_tonumberx) (lua_State *L, int idx, int *isnum);
218 lua_Integer (*dll_lua_tointegerx) (lua_State *L, int idx, int *isnum);
219 void (*dll_lua_callk) (lua_State *L, int nargs, int nresults, int ctx,
220 	lua_CFunction k);
221 int (*dll_lua_pcallk) (lua_State *L, int nargs, int nresults, int errfunc,
222 	int ctx, lua_CFunction k);
223 void (*dll_lua_getglobal) (lua_State *L, const char *var);
224 void (*dll_lua_setglobal) (lua_State *L, const char *var);
225 #endif
226 #if LUA_VERSION_NUM <= 502
227 void (*dll_lua_replace) (lua_State *L, int idx);
228 void (*dll_lua_remove) (lua_State *L, int idx);
229 #endif
230 #if LUA_VERSION_NUM >= 503
231 void  (*dll_lua_rotate) (lua_State *L, int idx, int n);
232 void (*dll_lua_copy) (lua_State *L, int fromidx, int toidx);
233 #endif
234 const char *(*dll_lua_typename) (lua_State *L, int tp);
235 void       (*dll_lua_close) (lua_State *L);
236 int (*dll_lua_gettop) (lua_State *L);
237 void (*dll_lua_settop) (lua_State *L, int idx);
238 void (*dll_lua_pushvalue) (lua_State *L, int idx);
239 int (*dll_lua_isnumber) (lua_State *L, int idx);
240 int (*dll_lua_isstring) (lua_State *L, int idx);
241 int (*dll_lua_type) (lua_State *L, int idx);
242 int (*dll_lua_rawequal) (lua_State *L, int idx1, int idx2);
243 int (*dll_lua_toboolean) (lua_State *L, int idx);
244 const char *(*dll_lua_tolstring) (lua_State *L, int idx, size_t *len);
245 void *(*dll_lua_touserdata) (lua_State *L, int idx);
246 void (*dll_lua_pushnil) (lua_State *L);
247 void (*dll_lua_pushnumber) (lua_State *L, lua_Number n);
248 void (*dll_lua_pushinteger) (lua_State *L, lua_Integer n);
249 void (*dll_lua_pushlstring) (lua_State *L, const char *s, size_t l);
250 void (*dll_lua_pushstring) (lua_State *L, const char *s);
251 const char *(*dll_lua_pushfstring) (lua_State *L, const char *fmt, ...);
252 void (*dll_lua_pushcclosure) (lua_State *L, lua_CFunction fn, int n);
253 void (*dll_lua_pushboolean) (lua_State *L, int b);
254 void (*dll_lua_pushlightuserdata) (lua_State *L, void *p);
255 void (*dll_lua_getfield) (lua_State *L, int idx, const char *k);
256 void (*dll_lua_rawget) (lua_State *L, int idx);
257 void (*dll_lua_rawgeti) (lua_State *L, int idx, int n);
258 void (*dll_lua_createtable) (lua_State *L, int narr, int nrec);
259 void *(*dll_lua_newuserdata) (lua_State *L, size_t sz);
260 int (*dll_lua_getmetatable) (lua_State *L, int objindex);
261 void (*dll_lua_setfield) (lua_State *L, int idx, const char *k);
262 void (*dll_lua_rawset) (lua_State *L, int idx);
263 void (*dll_lua_rawseti) (lua_State *L, int idx, int n);
264 int (*dll_lua_setmetatable) (lua_State *L, int objindex);
265 int (*dll_lua_next) (lua_State *L, int idx);
266 /* libs */
267 int (*dll_luaopen_base) (lua_State *L);
268 int (*dll_luaopen_table) (lua_State *L);
269 int (*dll_luaopen_string) (lua_State *L);
270 int (*dll_luaopen_math) (lua_State *L);
271 int (*dll_luaopen_io) (lua_State *L);
272 int (*dll_luaopen_os) (lua_State *L);
273 int (*dll_luaopen_package) (lua_State *L);
274 int (*dll_luaopen_debug) (lua_State *L);
275 void (*dll_luaL_openlibs) (lua_State *L);
276 
277 typedef void **luaV_function;
278 typedef struct {
279     const char *name;
280     luaV_function func;
281 } luaV_Reg;
282 
283 static const luaV_Reg luaV_dll[] = {
284     /* lauxlib */
285 #if LUA_VERSION_NUM <= 501
286     {"luaL_register", (luaV_function) &dll_luaL_register},
287     {"luaL_prepbuffer", (luaV_function) &dll_luaL_prepbuffer},
288     {"luaL_openlib", (luaV_function) &dll_luaL_openlib},
289     {"luaL_typerror", (luaV_function) &dll_luaL_typerror},
290     {"luaL_loadfile", (luaV_function) &dll_luaL_loadfile},
291     {"luaL_loadbuffer", (luaV_function) &dll_luaL_loadbuffer},
292 #else
293     {"luaL_prepbuffsize", (luaV_function) &dll_luaL_prepbuffsize},
294     {"luaL_setfuncs", (luaV_function) &dll_luaL_setfuncs},
295     {"luaL_loadfilex", (luaV_function) &dll_luaL_loadfilex},
296     {"luaL_loadbufferx", (luaV_function) &dll_luaL_loadbufferx},
297     {"luaL_argerror", (luaV_function) &dll_luaL_argerror},
298 #endif
299     {"luaL_checkany", (luaV_function) &dll_luaL_checkany},
300     {"luaL_checklstring", (luaV_function) &dll_luaL_checklstring},
301     {"luaL_checkinteger", (luaV_function) &dll_luaL_checkinteger},
302     {"luaL_optinteger", (luaV_function) &dll_luaL_optinteger},
303     {"luaL_checktype", (luaV_function) &dll_luaL_checktype},
304     {"luaL_error", (luaV_function) &dll_luaL_error},
305     {"luaL_newstate", (luaV_function) &dll_luaL_newstate},
306     {"luaL_buffinit", (luaV_function) &dll_luaL_buffinit},
307     {"luaL_addlstring", (luaV_function) &dll_luaL_addlstring},
308     {"luaL_pushresult", (luaV_function) &dll_luaL_pushresult},
309     /* lua */
310 #if LUA_VERSION_NUM <= 501
311     {"lua_tonumber", (luaV_function) &dll_lua_tonumber},
312     {"lua_tointeger", (luaV_function) &dll_lua_tointeger},
313     {"lua_call", (luaV_function) &dll_lua_call},
314     {"lua_pcall", (luaV_function) &dll_lua_pcall},
315 #else
316     {"lua_tonumberx", (luaV_function) &dll_lua_tonumberx},
317     {"lua_tointegerx", (luaV_function) &dll_lua_tointegerx},
318     {"lua_callk", (luaV_function) &dll_lua_callk},
319     {"lua_pcallk", (luaV_function) &dll_lua_pcallk},
320     {"lua_getglobal", (luaV_function) &dll_lua_getglobal},
321     {"lua_setglobal", (luaV_function) &dll_lua_setglobal},
322 #endif
323 #if LUA_VERSION_NUM <= 502
324     {"lua_replace", (luaV_function) &dll_lua_replace},
325     {"lua_remove", (luaV_function) &dll_lua_remove},
326 #endif
327 #if LUA_VERSION_NUM >= 503
328     {"lua_rotate", (luaV_function) &dll_lua_rotate},
329     {"lua_copy", (luaV_function) &dll_lua_copy},
330 #endif
331     {"lua_typename", (luaV_function) &dll_lua_typename},
332     {"lua_close", (luaV_function) &dll_lua_close},
333     {"lua_gettop", (luaV_function) &dll_lua_gettop},
334     {"lua_settop", (luaV_function) &dll_lua_settop},
335     {"lua_pushvalue", (luaV_function) &dll_lua_pushvalue},
336     {"lua_isnumber", (luaV_function) &dll_lua_isnumber},
337     {"lua_isstring", (luaV_function) &dll_lua_isstring},
338     {"lua_type", (luaV_function) &dll_lua_type},
339     {"lua_rawequal", (luaV_function) &dll_lua_rawequal},
340     {"lua_toboolean", (luaV_function) &dll_lua_toboolean},
341     {"lua_tolstring", (luaV_function) &dll_lua_tolstring},
342     {"lua_touserdata", (luaV_function) &dll_lua_touserdata},
343     {"lua_pushnil", (luaV_function) &dll_lua_pushnil},
344     {"lua_pushnumber", (luaV_function) &dll_lua_pushnumber},
345     {"lua_pushinteger", (luaV_function) &dll_lua_pushinteger},
346     {"lua_pushlstring", (luaV_function) &dll_lua_pushlstring},
347     {"lua_pushstring", (luaV_function) &dll_lua_pushstring},
348     {"lua_pushfstring", (luaV_function) &dll_lua_pushfstring},
349     {"lua_pushcclosure", (luaV_function) &dll_lua_pushcclosure},
350     {"lua_pushboolean", (luaV_function) &dll_lua_pushboolean},
351     {"lua_pushlightuserdata", (luaV_function) &dll_lua_pushlightuserdata},
352     {"lua_getfield", (luaV_function) &dll_lua_getfield},
353     {"lua_rawget", (luaV_function) &dll_lua_rawget},
354     {"lua_rawgeti", (luaV_function) &dll_lua_rawgeti},
355     {"lua_createtable", (luaV_function) &dll_lua_createtable},
356     {"lua_newuserdata", (luaV_function) &dll_lua_newuserdata},
357     {"lua_getmetatable", (luaV_function) &dll_lua_getmetatable},
358     {"lua_setfield", (luaV_function) &dll_lua_setfield},
359     {"lua_rawset", (luaV_function) &dll_lua_rawset},
360     {"lua_rawseti", (luaV_function) &dll_lua_rawseti},
361     {"lua_setmetatable", (luaV_function) &dll_lua_setmetatable},
362     {"lua_next", (luaV_function) &dll_lua_next},
363     /* libs */
364     {"luaopen_base", (luaV_function) &dll_luaopen_base},
365     {"luaopen_table", (luaV_function) &dll_luaopen_table},
366     {"luaopen_string", (luaV_function) &dll_luaopen_string},
367     {"luaopen_math", (luaV_function) &dll_luaopen_math},
368     {"luaopen_io", (luaV_function) &dll_luaopen_io},
369     {"luaopen_os", (luaV_function) &dll_luaopen_os},
370     {"luaopen_package", (luaV_function) &dll_luaopen_package},
371     {"luaopen_debug", (luaV_function) &dll_luaopen_debug},
372     {"luaL_openlibs", (luaV_function) &dll_luaL_openlibs},
373     {NULL, NULL}
374 };
375 
376 static HANDLE hinstLua = NULL;
377 
378     static void
379 end_dynamic_lua(void)
380 {
381     if (hinstLua)
382     {
383 	close_dll(hinstLua);
384 	hinstLua = 0;
385     }
386 }
387 
388     static int
389 lua_link_init(char *libname, int verbose)
390 {
391     const luaV_Reg *reg;
392     if (hinstLua) return OK;
393     hinstLua = load_dll(libname);
394     if (!hinstLua)
395     {
396 	if (verbose)
397 	    EMSG2(_(e_loadlib), libname);
398 	return FAIL;
399     }
400     for (reg = luaV_dll; reg->func; reg++)
401     {
402 	if ((*reg->func = symbol_from_dll(hinstLua, reg->name)) == NULL)
403 	{
404 	    close_dll(hinstLua);
405 	    hinstLua = 0;
406 	    if (verbose)
407 		EMSG2(_(e_loadfunc), reg->name);
408 	    return FAIL;
409 	}
410     }
411     return OK;
412 }
413 #endif /* DYNAMIC_LUA */
414 
415 #if defined(DYNAMIC_LUA) || defined(PROTO)
416     int
417 lua_enabled(int verbose)
418 {
419     return lua_link_init((char *)p_luadll, verbose) == OK;
420 }
421 #endif
422 
423 #if LUA_VERSION_NUM > 501
424     static int
425 luaL_typeerror (lua_State *L, int narg, const char *tname)
426 {
427     const char *msg = lua_pushfstring(L, "%s expected, got %s",
428 	    tname, luaL_typename(L, narg));
429     return luaL_argerror(L, narg, msg);
430 }
431 #endif
432 
433 
434 /* =======   Internal   ======= */
435 
436     static void
437 luaV_newmetatable(lua_State *L, const char *tname)
438 {
439     lua_newtable(L);
440     lua_pushlightuserdata(L, (void *) tname);
441     lua_pushvalue(L, -2);
442     lua_rawset(L, LUA_REGISTRYINDEX);
443 }
444 
445     static void *
446 luaV_toudata(lua_State *L, int ud, const char *tname)
447 {
448     void *p = lua_touserdata(L, ud);
449 
450     if (p != NULL) /* value is userdata? */
451     {
452 	if (lua_getmetatable(L, ud)) /* does it have a metatable? */
453 	{
454 	    luaV_getfield(L, tname); /* get metatable */
455 	    if (lua_rawequal(L, -1, -2)) /* MTs match? */
456 	    {
457 		lua_pop(L, 2); /* MTs */
458 		return p;
459 	    }
460 	}
461     }
462     return NULL;
463 }
464 
465     static void *
466 luaV_checkcache(lua_State *L, void *p)
467 {
468     luaV_getudata(L, p);
469     if (lua_isnil(L, -1)) luaL_error(L, "invalid object");
470     lua_pop(L, 1);
471     return p;
472 }
473 
474 #define luaV_unbox(L,luatyp,ud) (*((luatyp *) lua_touserdata((L),(ud))))
475 
476 #define luaV_checkvalid(L,luatyp,ud) \
477     luaV_checkcache((L), (void *) luaV_unbox((L),luatyp,(ud)))
478 
479     static void *
480 luaV_checkudata(lua_State *L, int ud, const char *tname)
481 {
482     void *p = luaV_toudata(L, ud, tname);
483     if (p == NULL) luaL_typeerror(L, ud, tname);
484     return p;
485 }
486 
487     static void
488 luaV_pushtypval(lua_State *L, typval_T *tv)
489 {
490     if (tv == NULL)
491     {
492 	lua_pushnil(L);
493 	return;
494     }
495     switch (tv->v_type)
496     {
497 	case VAR_STRING:
498 	    lua_pushstring(L, tv->vval.v_string == NULL
499 					    ? "" : (char *)tv->vval.v_string);
500 	    break;
501 	case VAR_NUMBER:
502 	    lua_pushinteger(L, (int) tv->vval.v_number);
503 	    break;
504 #ifdef FEAT_FLOAT
505 	case VAR_FLOAT:
506 	    lua_pushnumber(L, (lua_Number) tv->vval.v_float);
507 	    break;
508 #endif
509 	case VAR_LIST:
510 	    luaV_pushlist(L, tv->vval.v_list);
511 	    break;
512 	case VAR_DICT:
513 	    luaV_pushdict(L, tv->vval.v_dict);
514 	    break;
515 	case VAR_SPECIAL:
516 	    if (tv->vval.v_number <= VVAL_TRUE)
517 		lua_pushinteger(L, (int) tv->vval.v_number);
518 	    else
519 		lua_pushnil(L);
520 	    break;
521 	case VAR_FUNC:
522 	    luaV_pushfuncref(L, tv);
523 	    break;
524 	default:
525 	    lua_pushnil(L);
526     }
527 }
528 
529 /*
530  * Converts lua value at 'pos' to typval 'tv'.
531  * Returns OK or FAIL.
532  */
533     static int
534 luaV_totypval(lua_State *L, int pos, typval_T *tv)
535 {
536     int status = OK;
537 
538     switch (lua_type(L, pos))
539     {
540 	case LUA_TBOOLEAN:
541 	    tv->v_type = VAR_SPECIAL;
542 	    tv->vval.v_number = (varnumber_T) lua_toboolean(L, pos);
543 	    break;
544 	case LUA_TSTRING:
545 	    tv->v_type = VAR_STRING;
546 	    tv->vval.v_string = vim_strsave((char_u *) lua_tostring(L, pos));
547 	    break;
548 	case LUA_TNUMBER:
549 #ifdef FEAT_FLOAT
550 	    tv->v_type = VAR_FLOAT;
551 	    tv->vval.v_float = (float_T) lua_tonumber(L, pos);
552 #else
553 	    tv->v_type = VAR_NUMBER;
554 	    tv->vval.v_number = (varnumber_T) lua_tointeger(L, pos);
555 #endif
556 	    break;
557 	case LUA_TUSERDATA:
558 	{
559 	    void *p = lua_touserdata(L, pos);
560 
561 	    if (lua_getmetatable(L, pos)) /* has metatable? */
562 	    {
563 		/* check list */
564 		luaV_getfield(L, LUAVIM_LIST);
565 		if (lua_rawequal(L, -1, -2))
566 		{
567 		    tv->v_type = VAR_LIST;
568 		    tv->vval.v_list = *((luaV_List *) p);
569 		    ++tv->vval.v_list->lv_refcount;
570 		    lua_pop(L, 2); /* MTs */
571 		    break;
572 		}
573 		/* check dict */
574 		luaV_getfield(L, LUAVIM_DICT);
575 		if (lua_rawequal(L, -1, -3))
576 		{
577 		    tv->v_type = VAR_DICT;
578 		    tv->vval.v_dict = *((luaV_Dict *) p);
579 		    ++tv->vval.v_dict->dv_refcount;
580 		    lua_pop(L, 3); /* MTs */
581 		    break;
582 		}
583 		/* check funcref */
584 		luaV_getfield(L, LUAVIM_FUNCREF);
585 		if (lua_rawequal(L, -1, -4))
586 		{
587 		    luaV_Funcref *f = (luaV_Funcref *) p;
588 		    copy_tv(&f->tv, tv);
589 		    lua_pop(L, 4); /* MTs */
590 		    break;
591 		}
592 		lua_pop(L, 4); /* MTs */
593 	    }
594 	}
595 	/* FALLTHROUGH */
596 	default:
597 	    tv->v_type = VAR_NUMBER;
598 	    tv->vval.v_number = 0;
599 	    status = FAIL;
600     }
601     return status;
602 }
603 
604 /* similar to luaL_addlstring, but replaces \0 with \n if toline and
605  * \n with \0 otherwise */
606     static void
607 luaV_addlstring(luaL_Buffer *b, const char *s, size_t l, int toline)
608 {
609     while (l--)
610     {
611 	if (*s == '\0' && toline)
612 	    luaL_addchar(b, '\n');
613 	else if (*s == '\n' && !toline)
614 	    luaL_addchar(b, '\0');
615 	else
616 	    luaL_addchar(b, *s);
617 	s++;
618     }
619 }
620 
621     static void
622 luaV_pushline(lua_State *L, buf_T *buf, linenr_T n)
623 {
624     const char *s = (const char *) ml_get_buf(buf, n, FALSE);
625     luaL_Buffer b;
626     luaL_buffinit(L, &b);
627     luaV_addlstring(&b, s, strlen(s), 0);
628     luaL_pushresult(&b);
629 }
630 
631     static char_u *
632 luaV_toline(lua_State *L, int pos)
633 {
634     size_t l;
635     const char *s = lua_tolstring(L, pos, &l);
636 
637     luaL_Buffer b;
638     luaL_buffinit(L, &b);
639     luaV_addlstring(&b, s, l, 1);
640     luaL_pushresult(&b);
641     return (char_u *) lua_tostring(L, -1);
642 }
643 
644 /* pops a string s from the top of the stack and calls mf(t) for pieces t of
645  * s separated by newlines */
646     static void
647 luaV_msgfunc(lua_State *L, msgfunc_T mf)
648 {
649     luaL_Buffer b;
650     size_t l;
651     const char *p, *s = lua_tolstring(L, -1, &l);
652     luaL_buffinit(L, &b);
653     luaV_addlstring(&b, s, l, 0);
654     luaL_pushresult(&b);
655     /* break string */
656     p = s = lua_tolstring(L, -1, &l);
657     while (l--)
658     {
659 	if (*p++ == '\0') /* break? */
660 	{
661 	    mf((char_u *) s);
662 	    s = p;
663 	}
664     }
665     mf((char_u *) s);
666     lua_pop(L, 2); /* original and modified strings */
667 }
668 
669 #define luaV_newtype(typ,tname,luatyp,luatname) \
670 	static luatyp * \
671     luaV_new##tname (lua_State *L, typ *obj) \
672     { \
673 	luatyp *o = (luatyp *) lua_newuserdata(L, sizeof(luatyp)); \
674 	*o = obj; \
675 	luaV_setudata(L, obj); /* cache[obj] = udata */ \
676 	luaV_getfield(L, luatname); \
677 	lua_setmetatable(L, -2); \
678 	return o; \
679     }
680 
681 #define luaV_pushtype(typ,tname,luatyp) \
682 	static luatyp * \
683     luaV_push##tname(lua_State *L, typ *obj) \
684     { \
685 	luatyp *o = NULL; \
686 	if (obj == NULL) \
687 	    lua_pushnil(L); \
688 	else { \
689 	    luaV_getudata(L, obj); \
690 	    if (lua_isnil(L, -1)) /* not interned? */ \
691 	    { \
692 		lua_pop(L, 1); \
693 		o = luaV_new##tname(L, obj); \
694 	    } \
695 	    else \
696 		o = (luatyp *) lua_touserdata(L, -1); \
697 	} \
698 	return o; \
699     }
700 
701 #define luaV_type_tostring(tname,luatname) \
702 	static int \
703     luaV_##tname##_tostring (lua_State *L) \
704     { \
705 	lua_pushfstring(L, "%s: %p", luatname, lua_touserdata(L, 1)); \
706 	return 1; \
707     }
708 
709 /* =======   List type   ======= */
710 
711     static luaV_List *
712 luaV_newlist (lua_State *L, list_T *lis)
713 {
714     luaV_List *l = (luaV_List *) lua_newuserdata(L, sizeof(luaV_List));
715     *l = lis;
716     lis->lv_refcount++; /* reference in Lua */
717     luaV_setudata(L, lis); /* cache[lis] = udata */
718     luaV_getfield(L, LUAVIM_LIST);
719     lua_setmetatable(L, -2);
720     return l;
721 }
722 
723 luaV_pushtype(list_T, list, luaV_List)
724 luaV_type_tostring(list, LUAVIM_LIST)
725 
726     static int
727 luaV_list_len (lua_State *L)
728 {
729     list_T *l = luaV_unbox(L, luaV_List, 1);
730     lua_pushinteger(L, (l == NULL) ? 0 : (int) l->lv_len);
731     return 1;
732 }
733 
734     static int
735 luaV_list_iter (lua_State *L)
736 {
737     listitem_T *li = (listitem_T *) lua_touserdata(L, lua_upvalueindex(2));
738     if (li == NULL) return 0;
739     luaV_pushtypval(L, &li->li_tv);
740     lua_pushlightuserdata(L, (void *) li->li_next);
741     lua_replace(L, lua_upvalueindex(2));
742     return 1;
743 }
744 
745     static int
746 luaV_list_call (lua_State *L)
747 {
748     list_T *l = luaV_unbox(L, luaV_List, 1);
749     lua_pushvalue(L, lua_upvalueindex(1)); /* pass cache table along */
750     lua_pushlightuserdata(L, (void *) l->lv_first);
751     lua_pushcclosure(L, luaV_list_iter, 2);
752     return 1;
753 }
754 
755     static int
756 luaV_list_index (lua_State *L)
757 {
758     list_T *l = luaV_unbox(L, luaV_List, 1);
759     if (lua_isnumber(L, 2)) /* list item? */
760     {
761 	listitem_T *li = list_find(l, (long) luaL_checkinteger(L, 2));
762 	if (li == NULL)
763 	    lua_pushnil(L);
764 	else
765 	    luaV_pushtypval(L, &li->li_tv);
766     }
767     else if (lua_isstring(L, 2)) /* method? */
768     {
769 	const char *s = lua_tostring(L, 2);
770 	if (strncmp(s, "add", 3) == 0
771 		|| strncmp(s, "insert", 6) == 0)
772 	{
773 	    lua_getmetatable(L, 1);
774 	    lua_getfield(L, -1, s);
775 	}
776 	else
777 	    lua_pushnil(L);
778     }
779     else
780 	lua_pushnil(L);
781     return 1;
782 }
783 
784     static int
785 luaV_list_newindex (lua_State *L)
786 {
787     list_T *l = luaV_unbox(L, luaV_List, 1);
788     long n = (long) luaL_checkinteger(L, 2);
789     listitem_T *li;
790     if (l->lv_lock)
791 	luaL_error(L, "list is locked");
792     li = list_find(l, n);
793     if (li == NULL) return 0;
794     if (lua_isnil(L, 3)) /* remove? */
795     {
796 	vimlist_remove(l, li, li);
797 	clear_tv(&li->li_tv);
798 	vim_free(li);
799     }
800     else
801     {
802 	typval_T v;
803 	luaV_checktypval(L, 3, &v, "setting list item");
804 	clear_tv(&li->li_tv);
805 	copy_tv(&v, &li->li_tv);
806 	clear_tv(&v);
807     }
808     return 0;
809 }
810 
811     static int
812 luaV_list_add (lua_State *L)
813 {
814     luaV_List *lis = luaV_checkudata(L, 1, LUAVIM_LIST);
815     list_T *l = (list_T *) luaV_checkcache(L, (void *) *lis);
816     typval_T v;
817     if (l->lv_lock)
818 	luaL_error(L, "list is locked");
819     lua_settop(L, 2);
820     luaV_checktypval(L, 2, &v, "adding list item");
821     if (list_append_tv(l, &v) == FAIL)
822     {
823 	clear_tv(&v);
824 	luaL_error(L, "failed to add item to list");
825     }
826     clear_tv(&v);
827     lua_settop(L, 1);
828     return 1;
829 }
830 
831     static int
832 luaV_list_insert (lua_State *L)
833 {
834     luaV_List *lis = luaV_checkudata(L, 1, LUAVIM_LIST);
835     list_T *l = (list_T *) luaV_checkcache(L, (void *) *lis);
836     long pos = (long) luaL_optinteger(L, 3, 0);
837     listitem_T *li = NULL;
838     typval_T v;
839     if (l->lv_lock)
840 	luaL_error(L, "list is locked");
841     if (pos < l->lv_len)
842     {
843 	li = list_find(l, pos);
844 	if (li == NULL)
845 	    luaL_error(L, "invalid position");
846     }
847     lua_settop(L, 2);
848     luaV_checktypval(L, 2, &v, "inserting list item");
849     if (list_insert_tv(l, &v, li) == FAIL)
850     {
851 	clear_tv(&v);
852 	luaL_error(L, "failed to add item to list");
853     }
854     clear_tv(&v);
855     lua_settop(L, 1);
856     return 1;
857 }
858 
859 static const luaL_Reg luaV_List_mt[] = {
860     {"__tostring", luaV_list_tostring},
861     {"__len", luaV_list_len},
862     {"__call", luaV_list_call},
863     {"__index", luaV_list_index},
864     {"__newindex", luaV_list_newindex},
865     {"add", luaV_list_add},
866     {"insert", luaV_list_insert},
867     {NULL, NULL}
868 };
869 
870 
871 /* =======   Dict type   ======= */
872 
873     static luaV_Dict *
874 luaV_newdict (lua_State *L, dict_T *dic)
875 {
876     luaV_Dict *d = (luaV_Dict *) lua_newuserdata(L, sizeof(luaV_Dict));
877     *d = dic;
878     dic->dv_refcount++; /* reference in Lua */
879     luaV_setudata(L, dic); /* cache[dic] = udata */
880     luaV_getfield(L, LUAVIM_DICT);
881     lua_setmetatable(L, -2);
882     return d;
883 }
884 
885 luaV_pushtype(dict_T, dict, luaV_Dict)
886 luaV_type_tostring(dict, LUAVIM_DICT)
887 
888     static int
889 luaV_dict_len (lua_State *L)
890 {
891     dict_T *d = luaV_unbox(L, luaV_Dict, 1);
892     lua_pushinteger(L, (d == NULL) ? 0 : (int) d->dv_hashtab.ht_used);
893     return 1;
894 }
895 
896     static int
897 luaV_dict_iter (lua_State *L UNUSED)
898 {
899 #ifdef FEAT_EVAL
900     hashitem_T *hi = (hashitem_T *) lua_touserdata(L, lua_upvalueindex(2));
901     int n = lua_tointeger(L, lua_upvalueindex(3));
902     dictitem_T *di;
903     if (n <= 0) return 0;
904     while (HASHITEM_EMPTY(hi)) hi++;
905     di = dict_lookup(hi);
906     lua_pushstring(L, (char *) hi->hi_key);
907     luaV_pushtypval(L, &di->di_tv);
908     lua_pushlightuserdata(L, (void *) (hi + 1));
909     lua_replace(L, lua_upvalueindex(2));
910     lua_pushinteger(L, n - 1);
911     lua_replace(L, lua_upvalueindex(3));
912     return 2;
913 #else
914     return 0;
915 #endif
916 }
917 
918     static int
919 luaV_dict_call (lua_State *L)
920 {
921     dict_T *d = luaV_unbox(L, luaV_Dict, 1);
922     hashtab_T *ht = &d->dv_hashtab;
923     lua_pushvalue(L, lua_upvalueindex(1)); /* pass cache table along */
924     lua_pushlightuserdata(L, (void *) ht->ht_array);
925     lua_pushinteger(L, ht->ht_used); /* # remaining items */
926     lua_pushcclosure(L, luaV_dict_iter, 3);
927     return 1;
928 }
929 
930     static int
931 luaV_dict_index(lua_State *L)
932 {
933     dict_T *d = luaV_unbox(L, luaV_Dict, 1);
934     char_u *key = (char_u *) luaL_checkstring(L, 2);
935     dictitem_T *di = dict_find(d, key, -1);
936 
937     if (di == NULL)
938 	lua_pushnil(L);
939     else
940     {
941 	luaV_pushtypval(L, &di->di_tv);
942 	if (di->di_tv.v_type == VAR_FUNC) /* funcref? */
943 	{
944 	    luaV_Funcref *f = (luaV_Funcref *) lua_touserdata(L, -1);
945 	    f->self = d; /* keep "self" reference */
946 	    d->dv_refcount++;
947 	}
948     }
949     return 1;
950 }
951 
952     static int
953 luaV_dict_newindex(lua_State *L)
954 {
955     dict_T *d = luaV_unbox(L, luaV_Dict, 1);
956     char_u *key = (char_u *) luaL_checkstring(L, 2);
957     dictitem_T *di;
958     typval_T v;
959     if (d->dv_lock)
960 	luaL_error(L, "dict is locked");
961     if (key != NULL && *key == NUL)
962 	luaL_error(L, "empty key");
963     if (!lua_isnil(L, 3)) { /* read value? */
964 	luaV_checktypval(L, 3, &v, "setting dict item");
965 	if (d->dv_scope == VAR_DEF_SCOPE && v.v_type == VAR_FUNC)
966 	    luaL_error(L, "cannot assign funcref to builtin scope");
967     }
968     di = dict_find(d, key, -1);
969     if (di == NULL) /* non-existing key? */
970     {
971 	if (lua_isnil(L, 3)) return 0;
972 	di = dictitem_alloc(key);
973 	if (di == NULL) return 0;
974 	if (dict_add(d, di) == FAIL)
975 	{
976 		vim_free(di);
977 		return 0;
978 	}
979     }
980     else
981 	clear_tv(&di->di_tv);
982     if (lua_isnil(L, 3)) /* remove? */
983     {
984 	hashitem_T *hi = hash_find(&d->dv_hashtab, di->di_key);
985 	hash_remove(&d->dv_hashtab, hi);
986 	dictitem_free(di);
987     }
988     else
989     {
990 	copy_tv(&v, &di->di_tv);
991 	clear_tv(&v);
992     }
993     return 0;
994 }
995 
996 static const luaL_Reg luaV_Dict_mt[] = {
997     {"__tostring", luaV_dict_tostring},
998     {"__len", luaV_dict_len},
999     {"__call", luaV_dict_call},
1000     {"__index", luaV_dict_index},
1001     {"__newindex", luaV_dict_newindex},
1002     {NULL, NULL}
1003 };
1004 
1005 
1006 /* =======   Funcref type   ======= */
1007 
1008     static luaV_Funcref *
1009 luaV_newfuncref(lua_State *L, char_u *name)
1010 {
1011     luaV_Funcref *f = (luaV_Funcref *)lua_newuserdata(L, sizeof(luaV_Funcref));
1012 
1013     if (name != NULL)
1014     {
1015 	func_ref(name); /* as in copy_tv */
1016 	f->tv.vval.v_string = vim_strsave(name);
1017     }
1018     f->tv.v_type = VAR_FUNC;
1019     f->args.v_type = VAR_LIST;
1020     f->self = NULL;
1021     luaV_getfield(L, LUAVIM_FUNCREF);
1022     lua_setmetatable(L, -2);
1023     return f;
1024 }
1025 
1026     static luaV_Funcref *
1027 luaV_pushfuncref(lua_State *L, typval_T *tv)
1028 {
1029     luaV_Funcref *f = luaV_newfuncref(L, NULL);
1030     copy_tv(tv, &f->tv);
1031     clear_tv(tv);
1032     return f;
1033 }
1034 
1035 
1036 luaV_type_tostring(funcref, LUAVIM_FUNCREF)
1037 
1038     static int
1039 luaV_funcref_gc(lua_State *L)
1040 {
1041     luaV_Funcref *f = (luaV_Funcref *) lua_touserdata(L, 1);
1042 
1043     func_unref(f->tv.vval.v_string);
1044     vim_free(f->tv.vval.v_string);
1045     dict_unref(f->self);
1046     return 0;
1047 }
1048 
1049 /* equivalent to string(funcref) */
1050     static int
1051 luaV_funcref_len(lua_State *L)
1052 {
1053     luaV_Funcref *f = (luaV_Funcref *) lua_touserdata(L, 1);
1054 
1055     lua_pushstring(L, (const char *) f->tv.vval.v_string);
1056     return 1;
1057 }
1058 
1059     static int
1060 luaV_funcref_call(lua_State *L)
1061 {
1062     luaV_Funcref *f = (luaV_Funcref *) lua_touserdata(L, 1);
1063     int i, n = lua_gettop(L) - 1; /* #args */
1064     int status;
1065     typval_T v, rettv;
1066 
1067     f->args.vval.v_list = list_alloc();
1068     rettv.v_type = VAR_UNKNOWN; /* as in clear_tv */
1069     for (i = 0; i < n; i++) {
1070 	luaV_checktypval(L, i + 2, &v, "calling funcref");
1071 	list_append_tv(f->args.vval.v_list, &v);
1072     }
1073     status = func_call(f->tv.vval.v_string, &f->args, NULL, f->self, &rettv);
1074     if (status == OK)
1075 	luaV_pushtypval(L, &rettv);
1076     clear_tv(&f->args);
1077     clear_tv(&rettv);
1078     if (status != OK)
1079 	luaL_error(L, "cannot call funcref");
1080     return 1;
1081 }
1082 
1083 static const luaL_Reg luaV_Funcref_mt[] = {
1084     {"__tostring", luaV_funcref_tostring},
1085     {"__gc", luaV_funcref_gc},
1086     {"__len", luaV_funcref_len},
1087     {"__call", luaV_funcref_call},
1088     {NULL, NULL}
1089 };
1090 
1091 
1092 /* =======   Buffer type   ======= */
1093 
1094 luaV_newtype(buf_T, buffer, luaV_Buffer, LUAVIM_BUFFER)
1095 luaV_pushtype(buf_T, buffer, luaV_Buffer)
1096 luaV_type_tostring(buffer, LUAVIM_BUFFER)
1097 
1098     static int
1099 luaV_buffer_len(lua_State *L)
1100 {
1101     buf_T *b = (buf_T *) luaV_checkvalid(L, luaV_Buffer, 1);
1102     lua_pushinteger(L, b->b_ml.ml_line_count);
1103     return 1;
1104 }
1105 
1106     static int
1107 luaV_buffer_call(lua_State *L)
1108 {
1109     buf_T *b = (buf_T *) luaV_checkvalid(L, luaV_Buffer, 1);
1110     lua_settop(L, 1);
1111     set_curbuf(b, DOBUF_SPLIT);
1112     return 1;
1113 }
1114 
1115     static int
1116 luaV_buffer_index(lua_State *L)
1117 {
1118     buf_T *b = (buf_T *) luaV_checkvalid(L, luaV_Buffer, 1);
1119     linenr_T n = (linenr_T) lua_tointeger(L, 2);
1120     if (n > 0 && n <= b->b_ml.ml_line_count)
1121 	luaV_pushline(L, b, n);
1122     else if (lua_isstring(L, 2))
1123     {
1124 	const char *s = lua_tostring(L, 2);
1125 	if (strncmp(s, "name", 4) == 0)
1126 	    lua_pushstring(L, (char *) b->b_sfname);
1127 	else if (strncmp(s, "fname", 5) == 0)
1128 	    lua_pushstring(L, (char *) b->b_ffname);
1129 	else if (strncmp(s, "number", 6) == 0)
1130 	    lua_pushinteger(L, b->b_fnum);
1131 	/* methods */
1132 	else if (strncmp(s,   "insert", 6) == 0
1133 		|| strncmp(s, "next", 4) == 0
1134 		|| strncmp(s, "previous", 8) == 0
1135 		|| strncmp(s, "isvalid", 7) == 0)
1136 	{
1137 	    lua_getmetatable(L, 1);
1138 	    lua_getfield(L, -1, s);
1139 	}
1140 	else
1141 	    lua_pushnil(L);
1142     }
1143     else
1144 	lua_pushnil(L);
1145     return 1;
1146 }
1147 
1148     static int
1149 luaV_buffer_newindex(lua_State *L)
1150 {
1151     buf_T *b = (buf_T *) luaV_checkvalid(L, luaV_Buffer, 1);
1152     linenr_T n = (linenr_T) luaL_checkinteger(L, 2);
1153 #ifdef HAVE_SANDBOX
1154     luaV_checksandbox(L);
1155 #endif
1156     if (n < 1 || n > b->b_ml.ml_line_count)
1157 	luaL_error(L, "invalid line number");
1158     if (lua_isnil(L, 3)) /* delete line */
1159     {
1160 	buf_T *buf = curbuf;
1161 	curbuf = b;
1162 	if (u_savedel(n, 1L) == FAIL)
1163 	{
1164 	    curbuf = buf;
1165 	    luaL_error(L, "cannot save undo information");
1166 	}
1167 	else if (ml_delete(n, FALSE) == FAIL)
1168 	{
1169 	    curbuf = buf;
1170 	    luaL_error(L, "cannot delete line");
1171 	}
1172 	else
1173 	{
1174 	    deleted_lines_mark(n, 1L);
1175 	    if (b == curwin->w_buffer) /* fix cursor in current window? */
1176 	    {
1177 		if (curwin->w_cursor.lnum >= n)
1178 		{
1179 		    if (curwin->w_cursor.lnum > n)
1180 		    {
1181 			curwin->w_cursor.lnum -= 1;
1182 			check_cursor_col();
1183 		    }
1184 		    else check_cursor();
1185 		    changed_cline_bef_curs();
1186 		}
1187 		invalidate_botline();
1188 	    }
1189 	}
1190 	curbuf = buf;
1191     }
1192     else if (lua_isstring(L, 3)) /* update line */
1193     {
1194 	buf_T *buf = curbuf;
1195 	curbuf = b;
1196 	if (u_savesub(n) == FAIL)
1197 	{
1198 	    curbuf = buf;
1199 	    luaL_error(L, "cannot save undo information");
1200 	}
1201 	else if (ml_replace(n, luaV_toline(L, 3), TRUE) == FAIL)
1202 	{
1203 	    curbuf = buf;
1204 	    luaL_error(L, "cannot replace line");
1205 	}
1206 	else changed_bytes(n, 0);
1207 	curbuf = buf;
1208 	if (b == curwin->w_buffer)
1209 	    check_cursor_col();
1210     }
1211     else
1212 	luaL_error(L, "wrong argument to change line");
1213     return 0;
1214 }
1215 
1216     static int
1217 luaV_buffer_insert(lua_State *L)
1218 {
1219     luaV_Buffer *lb = luaV_checkudata(L, 1, LUAVIM_BUFFER);
1220     buf_T *b = (buf_T *) luaV_checkcache(L, (void *) *lb);
1221     linenr_T last = b->b_ml.ml_line_count;
1222     linenr_T n = (linenr_T) luaL_optinteger(L, 3, last);
1223     buf_T *buf;
1224     luaL_checktype(L, 2, LUA_TSTRING);
1225 #ifdef HAVE_SANDBOX
1226     luaV_checksandbox(L);
1227 #endif
1228     /* fix insertion line */
1229     if (n < 0) n = 0;
1230     if (n > last) n = last;
1231     /* insert */
1232     buf = curbuf;
1233     curbuf = b;
1234     if (u_save(n, n + 1) == FAIL)
1235     {
1236 	curbuf = buf;
1237 	luaL_error(L, "cannot save undo information");
1238     }
1239     else if (ml_append(n, luaV_toline(L, 2), 0, FALSE) == FAIL)
1240     {
1241 	curbuf = buf;
1242 	luaL_error(L, "cannot insert line");
1243     }
1244     else
1245 	appended_lines_mark(n, 1L);
1246     curbuf = buf;
1247     update_screen(VALID);
1248     return 0;
1249 }
1250 
1251     static int
1252 luaV_buffer_next(lua_State *L)
1253 {
1254     luaV_Buffer *b = luaV_checkudata(L, 1, LUAVIM_BUFFER);
1255     buf_T *buf = (buf_T *) luaV_checkcache(L, (void *) *b);
1256     luaV_pushbuffer(L, buf->b_next);
1257     return 1;
1258 }
1259 
1260     static int
1261 luaV_buffer_previous(lua_State *L)
1262 {
1263     luaV_Buffer *b = luaV_checkudata(L, 1, LUAVIM_BUFFER);
1264     buf_T *buf = (buf_T *) luaV_checkcache(L, (void *) *b);
1265     luaV_pushbuffer(L, buf->b_prev);
1266     return 1;
1267 }
1268 
1269     static int
1270 luaV_buffer_isvalid(lua_State *L)
1271 {
1272     luaV_Buffer *b = luaV_checkudata(L, 1, LUAVIM_BUFFER);
1273     luaV_getudata(L, *b);
1274     lua_pushboolean(L, !lua_isnil(L, -1));
1275     return 1;
1276 }
1277 
1278 static const luaL_Reg luaV_Buffer_mt[] = {
1279     {"__tostring", luaV_buffer_tostring},
1280     {"__len", luaV_buffer_len},
1281     {"__call", luaV_buffer_call},
1282     {"__index", luaV_buffer_index},
1283     {"__newindex", luaV_buffer_newindex},
1284     {"insert", luaV_buffer_insert},
1285     {"next", luaV_buffer_next},
1286     {"previous", luaV_buffer_previous},
1287     {"isvalid", luaV_buffer_isvalid},
1288     {NULL, NULL}
1289 };
1290 
1291 
1292 /* =======   Window type   ======= */
1293 
1294 luaV_newtype(win_T, window, luaV_Window, LUAVIM_WINDOW)
1295 luaV_pushtype(win_T, window, luaV_Window)
1296 luaV_type_tostring(window, LUAVIM_WINDOW)
1297 
1298     static int
1299 luaV_window_call(lua_State *L)
1300 {
1301     win_T *w = (win_T *) luaV_checkvalid(L, luaV_Window, 1);
1302     lua_settop(L, 1);
1303     win_goto(w);
1304     return 1;
1305 }
1306 
1307     static int
1308 luaV_window_index(lua_State *L)
1309 {
1310     win_T *w = (win_T *) luaV_checkvalid(L, luaV_Window, 1);
1311     const char *s = luaL_checkstring(L, 2);
1312     if (strncmp(s, "buffer", 6) == 0)
1313 	luaV_pushbuffer(L, w->w_buffer);
1314     else if (strncmp(s, "line", 4) == 0)
1315 	lua_pushinteger(L, w->w_cursor.lnum);
1316     else if (strncmp(s, "col", 3) == 0)
1317 	lua_pushinteger(L, w->w_cursor.col + 1);
1318     else if (strncmp(s, "width", 5) == 0)
1319 	lua_pushinteger(L, w->w_width);
1320     else if (strncmp(s, "height", 6) == 0)
1321 	lua_pushinteger(L, w->w_height);
1322     /* methods */
1323     else if (strncmp(s,   "next", 4) == 0
1324 	    || strncmp(s, "previous", 8) == 0
1325 	    || strncmp(s, "isvalid", 7) == 0)
1326     {
1327 	lua_getmetatable(L, 1);
1328 	lua_getfield(L, -1, s);
1329     }
1330     else
1331 	lua_pushnil(L);
1332     return 1;
1333 }
1334 
1335     static int
1336 luaV_window_newindex (lua_State *L)
1337 {
1338     win_T *w = (win_T *) luaV_checkvalid(L, luaV_Window, 1);
1339     const char *s = luaL_checkstring(L, 2);
1340     int v = luaL_checkinteger(L, 3);
1341     if (strncmp(s, "line", 4) == 0)
1342     {
1343 #ifdef HAVE_SANDBOX
1344 	luaV_checksandbox(L);
1345 #endif
1346 	if (v < 1 || v > w->w_buffer->b_ml.ml_line_count)
1347 	    luaL_error(L, "line out of range");
1348 	w->w_cursor.lnum = v;
1349 	update_screen(VALID);
1350     }
1351     else if (strncmp(s, "col", 3) == 0)
1352     {
1353 #ifdef HAVE_SANDBOX
1354 	luaV_checksandbox(L);
1355 #endif
1356 	w->w_cursor.col = v - 1;
1357 	update_screen(VALID);
1358     }
1359     else if (strncmp(s, "width", 5) == 0)
1360     {
1361 	win_T *win = curwin;
1362 #ifdef FEAT_GUI
1363 	need_mouse_correct = TRUE;
1364 #endif
1365 	curwin = w;
1366 	win_setwidth(v);
1367 	curwin = win;
1368     }
1369     else if (strncmp(s, "height", 6) == 0)
1370     {
1371 	win_T *win = curwin;
1372 #ifdef FEAT_GUI
1373 	need_mouse_correct = TRUE;
1374 #endif
1375 	curwin = w;
1376 	win_setheight(v);
1377 	curwin = win;
1378     }
1379     else
1380 	luaL_error(L, "invalid window property: `%s'", s);
1381     return 0;
1382 }
1383 
1384     static int
1385 luaV_window_next(lua_State *L)
1386 {
1387     luaV_Window *w = luaV_checkudata(L, 1, LUAVIM_WINDOW);
1388     win_T *win = (win_T *) luaV_checkcache(L, (void *) *w);
1389     luaV_pushwindow(L, win->w_next);
1390     return 1;
1391 }
1392 
1393     static int
1394 luaV_window_previous(lua_State *L)
1395 {
1396     luaV_Window *w = luaV_checkudata(L, 1, LUAVIM_WINDOW);
1397     win_T *win = (win_T *) luaV_checkcache(L, (void *) *w);
1398     luaV_pushwindow(L, win->w_prev);
1399     return 1;
1400 }
1401 
1402     static int
1403 luaV_window_isvalid(lua_State *L)
1404 {
1405     luaV_Window *w = luaV_checkudata(L, 1, LUAVIM_WINDOW);
1406     luaV_getudata(L, *w);
1407     lua_pushboolean(L, !lua_isnil(L, -1));
1408     return 1;
1409 }
1410 
1411 static const luaL_Reg luaV_Window_mt[] = {
1412     {"__tostring", luaV_window_tostring},
1413     {"__call", luaV_window_call},
1414     {"__index", luaV_window_index},
1415     {"__newindex", luaV_window_newindex},
1416     {"next", luaV_window_next},
1417     {"previous", luaV_window_previous},
1418     {"isvalid", luaV_window_isvalid},
1419     {NULL, NULL}
1420 };
1421 
1422 
1423 /* =======   Vim module   ======= */
1424 
1425     static int
1426 luaV_print(lua_State *L)
1427 {
1428     int i, n = lua_gettop(L); /* nargs */
1429     const char *s;
1430     size_t l;
1431     luaL_Buffer b;
1432     luaL_buffinit(L, &b);
1433     lua_getglobal(L, "tostring");
1434     for (i = 1; i <= n; i++)
1435     {
1436 	lua_pushvalue(L, -1); /* tostring */
1437 	lua_pushvalue(L, i); /* arg */
1438 	lua_call(L, 1, 1);
1439 	s = lua_tolstring(L, -1, &l);
1440 	if (s == NULL)
1441 	    return luaL_error(L, "cannot convert to string");
1442 	if (i > 1) luaL_addchar(&b, ' '); /* use space instead of tab */
1443 	luaV_addlstring(&b, s, l, 0);
1444 	lua_pop(L, 1);
1445     }
1446     luaL_pushresult(&b);
1447     luaV_msg(L);
1448     return 0;
1449 }
1450 
1451     static int
1452 luaV_debug(lua_State *L)
1453 {
1454     lua_settop(L, 0);
1455     lua_getglobal(L, "vim");
1456     lua_getfield(L, -1, "eval");
1457     lua_remove(L, -2); /* vim.eval at position 1 */
1458     for (;;)
1459     {
1460 	const char *input;
1461 	size_t l;
1462 	lua_pushvalue(L, 1); /* vim.eval */
1463 	lua_pushliteral(L, "input('lua_debug> ')");
1464 	lua_call(L, 1, 1); /* return string */
1465 	input = lua_tolstring(L, -1, &l);
1466 	if (l == 0 || strcmp(input, "cont") == 0)
1467 	    return 0;
1468 	msg_putchar('\n'); /* avoid outputting on input line */
1469 	if (luaL_loadbuffer(L, input, l, "=(debug command)")
1470 		|| lua_pcall(L, 0, 0, 0))
1471 	    luaV_emsg(L);
1472 	lua_settop(L, 1); /* remove eventual returns, but keep vim.eval */
1473     }
1474 }
1475 
1476     static int
1477 luaV_command(lua_State *L)
1478 {
1479     do_cmdline_cmd((char_u *) luaL_checkstring(L, 1));
1480     update_screen(VALID);
1481     return 0;
1482 }
1483 
1484     static int
1485 luaV_eval(lua_State *L)
1486 {
1487     typval_T *tv = eval_expr((char_u *) luaL_checkstring(L, 1), NULL);
1488     if (tv == NULL) luaL_error(L, "invalid expression");
1489     luaV_pushtypval(L, tv);
1490     free_tv(tv);
1491     return 1;
1492 }
1493 
1494     static int
1495 luaV_beep(lua_State *L UNUSED)
1496 {
1497     vim_beep(BO_LANG);
1498     return 0;
1499 }
1500 
1501     static int
1502 luaV_line(lua_State *L)
1503 {
1504     luaV_pushline(L, curbuf, curwin->w_cursor.lnum);
1505     return 1;
1506 }
1507 
1508     static int
1509 luaV_list(lua_State *L)
1510 {
1511     list_T *l;
1512     int initarg = !lua_isnoneornil(L, 1);
1513 
1514     if (initarg && lua_type(L, 1) != LUA_TTABLE)
1515 	luaL_error(L, "table expected, got %s", luaL_typename(L, 1));
1516     l = list_alloc();
1517     if (l == NULL)
1518 	lua_pushnil(L);
1519     else
1520     {
1521 	luaV_newlist(L, l);
1522 	if (initarg) { /* traverse table to init dict */
1523 	    int notnil, i = 0;
1524 	    typval_T v;
1525 	    do {
1526 		lua_rawgeti(L, 1, ++i);
1527 		notnil = !lua_isnil(L, -1);
1528 		if (notnil) {
1529 		    luaV_checktypval(L, -1, &v, "vim.list");
1530 		    list_append_tv(l, &v);
1531 		}
1532 		lua_pop(L, 1); /* value */
1533 	    } while (notnil);
1534 	}
1535     }
1536     return 1;
1537 }
1538 
1539     static int
1540 luaV_dict(lua_State *L)
1541 {
1542     dict_T *d;
1543     int initarg = !lua_isnoneornil(L, 1);
1544 
1545     if (initarg && lua_type(L, 1) != LUA_TTABLE)
1546 	luaL_error(L, "table expected, got %s", luaL_typename(L, 1));
1547     d = dict_alloc();
1548     if (d == NULL)
1549 	lua_pushnil(L);
1550     else
1551     {
1552 	luaV_newdict(L, d);
1553 	if (initarg) /* traverse table to init dict */
1554 	{
1555 	    lua_pushnil(L);
1556 	    while (lua_next(L, 1))
1557 	    {
1558 		char_u *key;
1559 		dictitem_T *di;
1560 		typval_T v;
1561 		lua_pushvalue(L, -2); /* dup key in case it's a number */
1562 		key = (char_u *) lua_tostring(L, -1);
1563 		if (key != NULL && *key == NUL)
1564 		    luaL_error(L, "table has empty key");
1565 		luaV_checktypval(L, -2, &v, "vim.dict"); /* value */
1566 		di = dictitem_alloc(key);
1567 		if (di == NULL || dict_add(d, di) == FAIL) {
1568 		    vim_free(di);
1569 		    lua_pushnil(L);
1570 		    return 1;
1571 		}
1572 		copy_tv(&v, &di->di_tv);
1573 		clear_tv(&v);
1574 		lua_pop(L, 2); /* key copy and value */
1575 	    }
1576 	}
1577     }
1578     return 1;
1579 }
1580 
1581     static int
1582 luaV_funcref(lua_State *L)
1583 {
1584     const char *name = luaL_checkstring(L, 1);
1585     /* note: not checking if function exists (needs function_exists) */
1586     if (name == NULL || *name == NUL || VIM_ISDIGIT(*name))
1587 	luaL_error(L, "invalid function name: %s", name);
1588     luaV_newfuncref(L, (char_u *) name);
1589     return 1;
1590 }
1591 
1592     static int
1593 luaV_buffer(lua_State *L)
1594 {
1595     buf_T *buf;
1596     if (lua_isstring(L, 1)) /* get by number or name? */
1597     {
1598 	if (lua_isnumber(L, 1)) /* by number? */
1599 	{
1600 	    int n = lua_tointeger(L, 1);
1601 	    FOR_ALL_BUFFERS(buf)
1602 		if (buf->b_fnum == n) break;
1603 	}
1604 	else // by name
1605 	{
1606 	    size_t l;
1607 	    const char *s = lua_tolstring(L, 1, &l);
1608 	    FOR_ALL_BUFFERS(buf)
1609 	    {
1610 		if (buf->b_ffname == NULL || buf->b_sfname == NULL)
1611 		{
1612 		    if (l == 0) break;
1613 		}
1614 		else if (strncmp(s, (char *)buf->b_ffname, l) == 0
1615 			|| strncmp(s, (char *)buf->b_sfname, l) == 0)
1616 		    break;
1617 	    }
1618 	}
1619     }
1620     else
1621 	buf = (lua_toboolean(L, 1)) ? firstbuf : curbuf; /* first buffer? */
1622     luaV_pushbuffer(L, buf);
1623     return 1;
1624 }
1625 
1626     static int
1627 luaV_window(lua_State *L)
1628 {
1629     win_T *win;
1630     if (lua_isnumber(L, 1)) /* get by number? */
1631     {
1632 	int n = lua_tointeger(L, 1);
1633 	for (win = firstwin; win != NULL; win = win->w_next, n--)
1634 	    if (n == 1) break;
1635     }
1636     else
1637 	win = (lua_toboolean(L, 1)) ? firstwin : curwin; /* first window? */
1638     luaV_pushwindow(L, win);
1639     return 1;
1640 }
1641 
1642     static int
1643 luaV_open(lua_State *L)
1644 {
1645     char_u *s = NULL;
1646 #ifdef HAVE_SANDBOX
1647     luaV_checksandbox(L);
1648 #endif
1649     if (lua_isstring(L, 1)) s = (char_u *) lua_tostring(L, 1);
1650     luaV_pushbuffer(L, buflist_new(s, NULL, 1L, BLN_LISTED));
1651     return 1;
1652 }
1653 
1654     static int
1655 luaV_type(lua_State *L)
1656 {
1657     luaL_checkany(L, 1);
1658     if (lua_type(L, 1) == LUA_TUSERDATA) /* check vim udata? */
1659     {
1660 	lua_settop(L, 1);
1661 	if (lua_getmetatable(L, 1))
1662 	{
1663 	    luaV_getfield(L, LUAVIM_LIST);
1664 	    if (lua_rawequal(L, -1, 2))
1665 	    {
1666 		lua_pushstring(L, "list");
1667 		return 1;
1668 	    }
1669 	    luaV_getfield(L, LUAVIM_DICT);
1670 	    if (lua_rawequal(L, -1, 2))
1671 	    {
1672 		lua_pushstring(L, "dict");
1673 		return 1;
1674 	    }
1675 	    luaV_getfield(L, LUAVIM_FUNCREF);
1676 	    if (lua_rawequal(L, -1, 2))
1677 	    {
1678 		lua_pushstring(L, "funcref");
1679 		return 1;
1680 	    }
1681 	    luaV_getfield(L, LUAVIM_BUFFER);
1682 	    if (lua_rawequal(L, -1, 2))
1683 	    {
1684 		lua_pushstring(L, "buffer");
1685 		return 1;
1686 	    }
1687 	    luaV_getfield(L, LUAVIM_WINDOW);
1688 	    if (lua_rawequal(L, -1, 2))
1689 	    {
1690 		lua_pushstring(L, "window");
1691 		return 1;
1692 	    }
1693 	}
1694     }
1695     lua_pushstring(L, luaL_typename(L, 1)); /* fallback */
1696     return 1;
1697 }
1698 
1699 static const luaL_Reg luaV_module[] = {
1700     {"command", luaV_command},
1701     {"eval", luaV_eval},
1702     {"beep", luaV_beep},
1703     {"line", luaV_line},
1704     {"list", luaV_list},
1705     {"dict", luaV_dict},
1706     {"funcref", luaV_funcref},
1707     {"buffer", luaV_buffer},
1708     {"window", luaV_window},
1709     {"open", luaV_open},
1710     {"type", luaV_type},
1711     {NULL, NULL}
1712 };
1713 
1714 /* for freeing list, dict, buffer and window objects; lightuserdata as arg */
1715     static int
1716 luaV_free(lua_State *L)
1717 {
1718     lua_pushnil(L);
1719     luaV_setudata(L, lua_touserdata(L, 1));
1720     return 0;
1721 }
1722 
1723     static int
1724 luaV_luaeval (lua_State *L)
1725 {
1726     luaL_Buffer b;
1727     size_t l;
1728     const char *str = lua_tolstring(L, 1, &l);
1729     typval_T *arg = (typval_T *) lua_touserdata(L, 2);
1730     typval_T *rettv = (typval_T *) lua_touserdata(L, 3);
1731     luaL_buffinit(L, &b);
1732     luaL_addlstring(&b, LUAVIM_EVALHEADER, sizeof(LUAVIM_EVALHEADER) - 1);
1733     luaL_addlstring(&b, str, l);
1734     luaL_pushresult(&b);
1735     str = lua_tolstring(L, -1, &l);
1736     if (luaL_loadbuffer(L, str, l, LUAVIM_EVALNAME)) /* compile error? */
1737     {
1738 	luaV_emsg(L);
1739 	return 0;
1740     }
1741     luaV_pushtypval(L, arg);
1742     if (lua_pcall(L, 1, 1, 0)) /* running error? */
1743     {
1744 	luaV_emsg(L);
1745 	return 0;
1746     }
1747     if (luaV_totypval(L, -1, rettv) == FAIL)
1748 	EMSG("luaeval: cannot convert value");
1749     return 0;
1750 }
1751 
1752     static int
1753 luaV_setref (lua_State *L)
1754 {
1755     int		copyID = lua_tointeger(L, 1);
1756     int		abort = FALSE;
1757     typval_T	tv;
1758 
1759     luaV_getfield(L, LUAVIM_LIST);
1760     luaV_getfield(L, LUAVIM_DICT);
1761     lua_pushnil(L);
1762     /* traverse cache table */
1763     while (!abort && lua_next(L, lua_upvalueindex(1)) != 0)
1764     {
1765 	lua_getmetatable(L, -1);
1766 	if (lua_rawequal(L, -1, 2)) /* list? */
1767 	{
1768 	    tv.v_type = VAR_LIST;
1769 	    tv.vval.v_list = (list_T *) lua_touserdata(L, 4); /* key */
1770 	    abort = set_ref_in_item(&tv, copyID, NULL, NULL);
1771 	}
1772 	else if (lua_rawequal(L, -1, 3)) /* dict? */
1773 	{
1774 	    tv.v_type = VAR_DICT;
1775 	    tv.vval.v_dict = (dict_T *) lua_touserdata(L, 4); /* key */
1776 	    abort = set_ref_in_item(&tv, copyID, NULL, NULL);
1777 	}
1778 	lua_pop(L, 2); /* metatable and value */
1779     }
1780     lua_pushinteger(L, abort);
1781     return 1;
1782 }
1783 
1784     static int
1785 luaopen_vim(lua_State *L)
1786 {
1787     /* set cache table */
1788     lua_newtable(L);
1789     lua_newtable(L);
1790     lua_pushstring(L, "v");
1791     lua_setfield(L, -2, "__mode");
1792     lua_setmetatable(L, -2); /* cache is weak-valued */
1793     /* print */
1794     lua_pushcfunction(L, luaV_print);
1795     lua_setglobal(L, "print");
1796     /* debug.debug */
1797     lua_getglobal(L, "debug");
1798     lua_pushcfunction(L, luaV_debug);
1799     lua_setfield(L, -2, "debug");
1800     lua_pop(L, 1);
1801     /* free */
1802     lua_pushlightuserdata(L, (void *) LUAVIM_FREE);
1803     lua_pushvalue(L, 1); /* cache table */
1804     lua_pushcclosure(L, luaV_free, 1);
1805     lua_rawset(L, LUA_REGISTRYINDEX);
1806     /* luaeval */
1807     lua_pushlightuserdata(L, (void *) LUAVIM_LUAEVAL);
1808     lua_pushvalue(L, 1); /* cache table */
1809     lua_pushcclosure(L, luaV_luaeval, 1);
1810     lua_rawset(L, LUA_REGISTRYINDEX);
1811     /* setref */
1812     lua_pushlightuserdata(L, (void *) LUAVIM_SETREF);
1813     lua_pushvalue(L, 1); /* cache table */
1814     lua_pushcclosure(L, luaV_setref, 1);
1815     lua_rawset(L, LUA_REGISTRYINDEX);
1816     /* register */
1817     luaV_newmetatable(L, LUAVIM_LIST);
1818     lua_pushvalue(L, 1);
1819     luaV_openlib(L, luaV_List_mt, 1);
1820     luaV_newmetatable(L, LUAVIM_DICT);
1821     lua_pushvalue(L, 1);
1822     luaV_openlib(L, luaV_Dict_mt, 1);
1823     luaV_newmetatable(L, LUAVIM_FUNCREF);
1824     lua_pushvalue(L, 1);
1825     luaV_openlib(L, luaV_Funcref_mt, 1);
1826     luaV_newmetatable(L, LUAVIM_BUFFER);
1827     lua_pushvalue(L, 1); /* cache table */
1828     luaV_openlib(L, luaV_Buffer_mt, 1);
1829     luaV_newmetatable(L, LUAVIM_WINDOW);
1830     lua_pushvalue(L, 1); /* cache table */
1831     luaV_openlib(L, luaV_Window_mt, 1);
1832     lua_newtable(L); /* vim table */
1833     lua_pushvalue(L, 1); /* cache table */
1834     luaV_openlib(L, luaV_module, 1);
1835     lua_setglobal(L, LUAVIM_NAME);
1836     return 0;
1837 }
1838 
1839     static lua_State *
1840 luaV_newstate(void)
1841 {
1842     lua_State *L = luaL_newstate();
1843     luaL_openlibs(L); /* core libs */
1844     lua_pushcfunction(L, luaopen_vim); /* vim */
1845     lua_call(L, 0, 0);
1846     return L;
1847 }
1848 
1849     static void
1850 luaV_setrange(lua_State *L, int line1, int line2)
1851 {
1852     lua_getglobal(L, LUAVIM_NAME);
1853     lua_pushinteger(L, line1);
1854     lua_setfield(L, -2, "firstline");
1855     lua_pushinteger(L, line2);
1856     lua_setfield(L, -2, "lastline");
1857     lua_pop(L, 1); /* vim table */
1858 }
1859 
1860 
1861 /* =======   Interface   ======= */
1862 
1863 static lua_State *L = NULL;
1864 
1865     static int
1866 lua_isopen(void)
1867 {
1868     return L != NULL;
1869 }
1870 
1871     static int
1872 lua_init(void)
1873 {
1874     if (!lua_isopen())
1875     {
1876 #ifdef DYNAMIC_LUA
1877 	if (!lua_enabled(TRUE))
1878 	{
1879 	    EMSG(_("Lua library cannot be loaded."));
1880 	    return FAIL;
1881 	}
1882 #endif
1883 	L = luaV_newstate();
1884     }
1885     return OK;
1886 }
1887 
1888     void
1889 lua_end(void)
1890 {
1891     if (lua_isopen())
1892     {
1893 	lua_close(L);
1894 	L = NULL;
1895 #ifdef DYNAMIC_LUA
1896 	end_dynamic_lua();
1897 #endif
1898     }
1899 }
1900 
1901 /* ex commands */
1902     void
1903 ex_lua(exarg_T *eap)
1904 {
1905     char *script;
1906     if (lua_init() == FAIL) return;
1907     script = (char *) script_get(eap, eap->arg);
1908     if (!eap->skip)
1909     {
1910 	char *s = (script) ? script :  (char *) eap->arg;
1911 	luaV_setrange(L, eap->line1, eap->line2);
1912 	if (luaL_loadbuffer(L, s, strlen(s), LUAVIM_CHUNKNAME)
1913 		|| lua_pcall(L, 0, 0, 0))
1914 	    luaV_emsg(L);
1915     }
1916     if (script != NULL) vim_free(script);
1917 }
1918 
1919     void
1920 ex_luado(exarg_T *eap)
1921 {
1922     linenr_T l;
1923     const char *s = (const char *) eap->arg;
1924     luaL_Buffer b;
1925     size_t len;
1926     buf_T *was_curbuf = curbuf;
1927 
1928     if (lua_init() == FAIL) return;
1929     if (u_save(eap->line1 - 1, eap->line2 + 1) == FAIL)
1930     {
1931 	EMSG(_("cannot save undo information"));
1932 	return;
1933     }
1934     luaV_setrange(L, eap->line1, eap->line2);
1935     luaL_buffinit(L, &b);
1936     luaL_addlstring(&b, "return function(line, linenr) ", 30); /* header */
1937     luaL_addlstring(&b, s, strlen(s));
1938     luaL_addlstring(&b, " end", 4); /* footer */
1939     luaL_pushresult(&b);
1940     s = lua_tolstring(L, -1, &len);
1941     if (luaL_loadbuffer(L, s, len, LUAVIM_CHUNKNAME))
1942     {
1943 	luaV_emsg(L);
1944 	lua_pop(L, 1); /* function body */
1945 	return;
1946     }
1947     lua_call(L, 0, 1);
1948     lua_replace(L, -2); /* function -> body */
1949     for (l = eap->line1; l <= eap->line2; l++)
1950     {
1951 	/* Check the line number, the command my have deleted lines. */
1952 	if (l > curbuf->b_ml.ml_line_count)
1953 	    break;
1954 
1955 	lua_pushvalue(L, -1); /* function */
1956 	luaV_pushline(L, curbuf, l); /* current line as arg */
1957 	lua_pushinteger(L, l); /* current line number as arg */
1958 	if (lua_pcall(L, 2, 1, 0))
1959 	{
1960 	    luaV_emsg(L);
1961 	    break;
1962 	}
1963 	/* Catch the command switching to another buffer. */
1964 	if (curbuf != was_curbuf)
1965 	    break;
1966 	if (lua_isstring(L, -1)) /* update line? */
1967 	{
1968 #ifdef HAVE_SANDBOX
1969 	    luaV_checksandbox(L);
1970 #endif
1971 	    ml_replace(l, luaV_toline(L, -1), TRUE);
1972 	    changed_bytes(l, 0);
1973 	    lua_pop(L, 1); /* result from luaV_toline */
1974 	}
1975 	lua_pop(L, 1); /* line */
1976     }
1977     lua_pop(L, 1); /* function */
1978     check_cursor();
1979     update_screen(NOT_VALID);
1980 }
1981 
1982     void
1983 ex_luafile(exarg_T *eap)
1984 {
1985     if (lua_init() == FAIL)
1986 	return;
1987     if (!eap->skip)
1988     {
1989 	luaV_setrange(L, eap->line1, eap->line2);
1990 	if (luaL_loadfile(L, (char *) eap->arg) || lua_pcall(L, 0, 0, 0))
1991 	    luaV_emsg(L);
1992     }
1993 }
1994 
1995 #define luaV_freetype(typ,tname) \
1996 	void \
1997     lua_##tname##_free(typ *o) \
1998     { \
1999 	if (!lua_isopen()) return; \
2000 	luaV_getfield(L, LUAVIM_FREE); \
2001 	lua_pushlightuserdata(L, (void *) o); \
2002 	lua_call(L, 1, 0); \
2003     }
2004 
2005 luaV_freetype(buf_T, buffer)
2006 luaV_freetype(win_T, window)
2007 
2008     void
2009 do_luaeval (char_u *str, typval_T *arg, typval_T *rettv)
2010 {
2011     lua_init();
2012     luaV_getfield(L, LUAVIM_LUAEVAL);
2013     lua_pushstring(L, (char *) str);
2014     lua_pushlightuserdata(L, (void *) arg);
2015     lua_pushlightuserdata(L, (void *) rettv);
2016     lua_call(L, 3, 0);
2017 }
2018 
2019     int
2020 set_ref_in_lua (int copyID)
2021 {
2022     int aborted = 0;
2023 
2024     if (lua_isopen())
2025     {
2026 	luaV_getfield(L, LUAVIM_SETREF);
2027 	/* call the function with 1 arg, getting 1 result back */
2028 	lua_pushinteger(L, copyID);
2029 	lua_call(L, 1, 1);
2030 	/* get the result */
2031 	aborted = lua_tointeger(L, -1);
2032 	/* pop result off the stack */
2033 	lua_pop(L, 1);
2034     }
2035     return aborted;
2036 }
2037 
2038 #endif
2039