blob: ea6d1bd504f4c21490c905bbd675fb350504165e [file] [log] [blame]
Bram Moolenaar1dced572012-04-05 16:54:08 +02001/* vi:set ts=8 sts=4 sw=4 noet:
Bram Moolenaar0ba04292010-07-14 23:23:17 +02002 *
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
Bram Moolenaare2793352011-01-17 19:53:27 +010012#include "vim.h"
13
Bram Moolenaar0ba04292010-07-14 23:23:17 +020014#include <lua.h>
15#include <lualib.h>
16#include <lauxlib.h>
Bram Moolenaar0ba04292010-07-14 23:23:17 +020017
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"
Bram Moolenaar1dced572012-04-05 16:54:08 +020024#define LUAVIM_EVALNAME "luaeval"
25#define LUAVIM_EVALHEADER "local _A=select(1,...) return "
Bram Moolenaar0ba04292010-07-14 23:23:17 +020026
27typedef buf_T *luaV_Buffer;
28typedef win_T *luaV_Window;
Bram Moolenaar1dced572012-04-05 16:54:08 +020029typedef dict_T *luaV_Dict;
30typedef list_T *luaV_List;
Bram Moolenaar0ba04292010-07-14 23:23:17 +020031typedef void (*msgfunc_T)(char_u *);
32
Bram Moolenaar1dced572012-04-05 16:54:08 +020033static const char LUAVIM_DICT[] = "dict";
34static const char LUAVIM_LIST[] = "list";
Bram Moolenaar0ba04292010-07-14 23:23:17 +020035static const char LUAVIM_BUFFER[] = "buffer";
36static const char LUAVIM_WINDOW[] = "window";
37static const char LUAVIM_FREE[] = "luaV_free";
Bram Moolenaar1dced572012-04-05 16:54:08 +020038static const char LUAVIM_LUAEVAL[] = "luaV_luaeval";
39static const char LUAVIM_SETREF[] = "luaV_setref";
Bram Moolenaar0ba04292010-07-14 23:23:17 +020040
Bram Moolenaar1dced572012-04-05 16:54:08 +020041/* most functions are closures with a cache table as first upvalue;
42 * get/setudata manage references to vim userdata in cache table through
43 * object pointers (light userdata) */
44#define luaV_getudata(L, v) \
45 lua_pushlightuserdata((L), (void *) (v)); \
46 lua_rawget((L), lua_upvalueindex(1))
47#define luaV_setudata(L, v) \
48 lua_pushlightuserdata((L), (void *) (v)); \
49 lua_pushvalue((L), -2); \
50 lua_rawset((L), lua_upvalueindex(1))
Bram Moolenaar0ba04292010-07-14 23:23:17 +020051#define luaV_getfield(L, s) \
52 lua_pushlightuserdata((L), (void *)(s)); \
53 lua_rawget((L), LUA_REGISTRYINDEX)
54#define luaV_checksandbox(L) \
55 if (sandbox) luaL_error((L), "not allowed in sandbox")
56#define luaV_msg(L) luaV_msgfunc((L), (msgfunc_T) msg)
57#define luaV_emsg(L) luaV_msgfunc((L), (msgfunc_T) emsg)
58
Bram Moolenaar1dced572012-04-05 16:54:08 +020059static luaV_List *luaV_pushlist (lua_State *L, list_T *lis);
60static luaV_Dict *luaV_pushdict (lua_State *L, dict_T *dic);
61
62#if LUA_VERSION_NUM <= 501
63#define luaV_openlib(L, l, n) luaL_openlib(L, NULL, l, n)
64#define luaL_typeerror luaL_typerror
65#else
66#define luaV_openlib luaL_setfuncs
67#endif
Bram Moolenaar0ba04292010-07-14 23:23:17 +020068
69#ifdef DYNAMIC_LUA
Bram Moolenaar2334b6d2010-07-22 21:32:16 +020070
71#ifndef WIN3264
72# include <dlfcn.h>
73# define HANDLE void*
74# define load_dll(n) dlopen((n), RTLD_LAZY|RTLD_GLOBAL)
75# define symbol_from_dll dlsym
76# define close_dll dlclose
77#else
Bram Moolenaarebbcb822010-10-23 14:02:54 +020078# define load_dll vimLoadLib
Bram Moolenaar2334b6d2010-07-22 21:32:16 +020079# define symbol_from_dll GetProcAddress
80# define close_dll FreeLibrary
81#endif
82
Bram Moolenaar0ba04292010-07-14 23:23:17 +020083/* lauxlib */
Bram Moolenaar1dced572012-04-05 16:54:08 +020084#if LUA_VERSION_NUM <= 501
Bram Moolenaar0ba04292010-07-14 23:23:17 +020085#define luaL_register dll_luaL_register
Bram Moolenaar1dced572012-04-05 16:54:08 +020086#define luaL_prepbuffer dll_luaL_prepbuffer
87#define luaL_openlib dll_luaL_openlib
Bram Moolenaar0ba04292010-07-14 23:23:17 +020088#define luaL_typerror dll_luaL_typerror
Bram Moolenaar1dced572012-04-05 16:54:08 +020089#define luaL_loadfile dll_luaL_loadfile
90#define luaL_loadbuffer dll_luaL_loadbuffer
91#else
92#define luaL_prepbuffsize dll_luaL_prepbuffsize
93#define luaL_setfuncs dll_luaL_setfuncs
94#define luaL_loadfilex dll_luaL_loadfilex
95#define luaL_loadbufferx dll_luaL_loadbufferx
96#define luaL_argerror dll_luaL_argerror
97#endif
Bram Moolenaarbd2f3c32012-04-06 14:31:00 +020098#define luaL_checkany dll_luaL_checkany
Bram Moolenaar0ba04292010-07-14 23:23:17 +020099#define luaL_checklstring dll_luaL_checklstring
100#define luaL_checkinteger dll_luaL_checkinteger
101#define luaL_optinteger dll_luaL_optinteger
102#define luaL_checktype dll_luaL_checktype
103#define luaL_error dll_luaL_error
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200104#define luaL_newstate dll_luaL_newstate
105#define luaL_buffinit dll_luaL_buffinit
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200106#define luaL_addlstring dll_luaL_addlstring
107#define luaL_pushresult dll_luaL_pushresult
108/* lua */
Bram Moolenaar1dced572012-04-05 16:54:08 +0200109#if LUA_VERSION_NUM <= 501
110#define lua_tonumber dll_lua_tonumber
111#define lua_tointeger dll_lua_tointeger
112#define lua_call dll_lua_call
113#define lua_pcall dll_lua_pcall
114#else
115#define lua_tonumberx dll_lua_tonumberx
116#define lua_tointegerx dll_lua_tointegerx
117#define lua_callk dll_lua_callk
118#define lua_pcallk dll_lua_pcallk
119#define lua_getglobal dll_lua_getglobal
120#define lua_setglobal dll_lua_setglobal
Bram Moolenaar1dced572012-04-05 16:54:08 +0200121#endif
Bram Moolenaarbd2f3c32012-04-06 14:31:00 +0200122#define lua_typename dll_lua_typename
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200123#define lua_close dll_lua_close
124#define lua_gettop dll_lua_gettop
125#define lua_settop dll_lua_settop
126#define lua_pushvalue dll_lua_pushvalue
127#define lua_replace dll_lua_replace
Bram Moolenaar1dced572012-04-05 16:54:08 +0200128#define lua_remove dll_lua_remove
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200129#define lua_isnumber dll_lua_isnumber
130#define lua_isstring dll_lua_isstring
131#define lua_type dll_lua_type
132#define lua_rawequal dll_lua_rawequal
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200133#define lua_toboolean dll_lua_toboolean
134#define lua_tolstring dll_lua_tolstring
135#define lua_touserdata dll_lua_touserdata
136#define lua_pushnil dll_lua_pushnil
137#define lua_pushnumber dll_lua_pushnumber
138#define lua_pushinteger dll_lua_pushinteger
139#define lua_pushlstring dll_lua_pushlstring
140#define lua_pushstring dll_lua_pushstring
141#define lua_pushfstring dll_lua_pushfstring
142#define lua_pushcclosure dll_lua_pushcclosure
143#define lua_pushboolean dll_lua_pushboolean
144#define lua_pushlightuserdata dll_lua_pushlightuserdata
145#define lua_getfield dll_lua_getfield
146#define lua_rawget dll_lua_rawget
Bram Moolenaar1dced572012-04-05 16:54:08 +0200147#define lua_rawgeti dll_lua_rawgeti
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200148#define lua_createtable dll_lua_createtable
149#define lua_newuserdata dll_lua_newuserdata
150#define lua_getmetatable dll_lua_getmetatable
151#define lua_setfield dll_lua_setfield
152#define lua_rawset dll_lua_rawset
153#define lua_rawseti dll_lua_rawseti
154#define lua_setmetatable dll_lua_setmetatable
Bram Moolenaarbd2f3c32012-04-06 14:31:00 +0200155#define lua_next dll_lua_next
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200156/* libs */
157#define luaopen_base dll_luaopen_base
158#define luaopen_table dll_luaopen_table
159#define luaopen_string dll_luaopen_string
160#define luaopen_math dll_luaopen_math
Bram Moolenaar16c98f92010-07-28 22:46:08 +0200161#define luaopen_io dll_luaopen_io
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200162#define luaopen_os dll_luaopen_os
163#define luaopen_package dll_luaopen_package
164#define luaopen_debug dll_luaopen_debug
Bram Moolenaar16c98f92010-07-28 22:46:08 +0200165#define luaL_openlibs dll_luaL_openlibs
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200166
167/* lauxlib */
Bram Moolenaar1dced572012-04-05 16:54:08 +0200168#if LUA_VERSION_NUM <= 501
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200169void (*dll_luaL_register) (lua_State *L, const char *libname, const luaL_Reg *l);
Bram Moolenaar1dced572012-04-05 16:54:08 +0200170char *(*dll_luaL_prepbuffer) (luaL_Buffer *B);
171void (*dll_luaL_openlib) (lua_State *L, const char *libname, const luaL_Reg *l, int nup);
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200172int (*dll_luaL_typerror) (lua_State *L, int narg, const char *tname);
Bram Moolenaar1dced572012-04-05 16:54:08 +0200173int (*dll_luaL_loadfile) (lua_State *L, const char *filename);
174int (*dll_luaL_loadbuffer) (lua_State *L, const char *buff, size_t sz, const char *name);
175#else
176char *(*dll_luaL_prepbuffsize) (luaL_Buffer *B, size_t sz);
177void (*dll_luaL_setfuncs) (lua_State *L, const luaL_Reg *l, int nup);
178int (*dll_luaL_loadfilex) (lua_State *L, const char *filename, const char *mode);
179int (*dll_luaL_loadbufferx) (lua_State *L, const char *buff, size_t sz, const char *name, const char *mode);
180int (*dll_luaL_argerror) (lua_State *L, int numarg, const char *extramsg);
181#endif
Bram Moolenaarbd2f3c32012-04-06 14:31:00 +0200182void (*dll_luaL_checkany) (lua_State *L, int narg);
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200183const char *(*dll_luaL_checklstring) (lua_State *L, int numArg, size_t *l);
184lua_Integer (*dll_luaL_checkinteger) (lua_State *L, int numArg);
185lua_Integer (*dll_luaL_optinteger) (lua_State *L, int nArg, lua_Integer def);
186void (*dll_luaL_checktype) (lua_State *L, int narg, int t);
187int (*dll_luaL_error) (lua_State *L, const char *fmt, ...);
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200188lua_State *(*dll_luaL_newstate) (void);
189void (*dll_luaL_buffinit) (lua_State *L, luaL_Buffer *B);
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200190void (*dll_luaL_addlstring) (luaL_Buffer *B, const char *s, size_t l);
191void (*dll_luaL_pushresult) (luaL_Buffer *B);
192/* lua */
Bram Moolenaar1dced572012-04-05 16:54:08 +0200193#if LUA_VERSION_NUM <= 501
194lua_Number (*dll_lua_tonumber) (lua_State *L, int idx);
195lua_Integer (*dll_lua_tointeger) (lua_State *L, int idx);
196void (*dll_lua_call) (lua_State *L, int nargs, int nresults);
197int (*dll_lua_pcall) (lua_State *L, int nargs, int nresults, int errfunc);
198#else
199lua_Number (*dll_lua_tonumberx) (lua_State *L, int idx, int *isnum);
200lua_Integer (*dll_lua_tointegerx) (lua_State *L, int idx, int *isnum);
201void (*dll_lua_callk) (lua_State *L, int nargs, int nresults, int ctx,
202 lua_CFunction k);
203int (*dll_lua_pcallk) (lua_State *L, int nargs, int nresults, int errfunc,
204 int ctx, lua_CFunction k);
205void (*dll_lua_getglobal) (lua_State *L, const char *var);
206void (*dll_lua_setglobal) (lua_State *L, const char *var);
Bram Moolenaar1dced572012-04-05 16:54:08 +0200207#endif
Bram Moolenaarbd2f3c32012-04-06 14:31:00 +0200208const char *(*dll_lua_typename) (lua_State *L, int tp);
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200209void (*dll_lua_close) (lua_State *L);
210int (*dll_lua_gettop) (lua_State *L);
211void (*dll_lua_settop) (lua_State *L, int idx);
212void (*dll_lua_pushvalue) (lua_State *L, int idx);
213void (*dll_lua_replace) (lua_State *L, int idx);
Bram Moolenaar1dced572012-04-05 16:54:08 +0200214void (*dll_lua_remove) (lua_State *L, int idx);
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200215int (*dll_lua_isnumber) (lua_State *L, int idx);
216int (*dll_lua_isstring) (lua_State *L, int idx);
217int (*dll_lua_type) (lua_State *L, int idx);
218int (*dll_lua_rawequal) (lua_State *L, int idx1, int idx2);
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200219int (*dll_lua_toboolean) (lua_State *L, int idx);
220const char *(*dll_lua_tolstring) (lua_State *L, int idx, size_t *len);
221void *(*dll_lua_touserdata) (lua_State *L, int idx);
222void (*dll_lua_pushnil) (lua_State *L);
223void (*dll_lua_pushnumber) (lua_State *L, lua_Number n);
224void (*dll_lua_pushinteger) (lua_State *L, lua_Integer n);
225void (*dll_lua_pushlstring) (lua_State *L, const char *s, size_t l);
226void (*dll_lua_pushstring) (lua_State *L, const char *s);
227const char *(*dll_lua_pushfstring) (lua_State *L, const char *fmt, ...);
228void (*dll_lua_pushcclosure) (lua_State *L, lua_CFunction fn, int n);
229void (*dll_lua_pushboolean) (lua_State *L, int b);
230void (*dll_lua_pushlightuserdata) (lua_State *L, void *p);
231void (*dll_lua_getfield) (lua_State *L, int idx, const char *k);
232void (*dll_lua_rawget) (lua_State *L, int idx);
Bram Moolenaar1dced572012-04-05 16:54:08 +0200233void (*dll_lua_rawgeti) (lua_State *L, int idx, int n);
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200234void (*dll_lua_createtable) (lua_State *L, int narr, int nrec);
235void *(*dll_lua_newuserdata) (lua_State *L, size_t sz);
236int (*dll_lua_getmetatable) (lua_State *L, int objindex);
237void (*dll_lua_setfield) (lua_State *L, int idx, const char *k);
238void (*dll_lua_rawset) (lua_State *L, int idx);
239void (*dll_lua_rawseti) (lua_State *L, int idx, int n);
240int (*dll_lua_setmetatable) (lua_State *L, int objindex);
Bram Moolenaarbd2f3c32012-04-06 14:31:00 +0200241int (*dll_lua_next) (lua_State *L, int idx);
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200242/* libs */
243int (*dll_luaopen_base) (lua_State *L);
244int (*dll_luaopen_table) (lua_State *L);
245int (*dll_luaopen_string) (lua_State *L);
246int (*dll_luaopen_math) (lua_State *L);
Bram Moolenaar16c98f92010-07-28 22:46:08 +0200247int (*dll_luaopen_io) (lua_State *L);
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200248int (*dll_luaopen_os) (lua_State *L);
249int (*dll_luaopen_package) (lua_State *L);
250int (*dll_luaopen_debug) (lua_State *L);
Bram Moolenaar16c98f92010-07-28 22:46:08 +0200251void (*dll_luaL_openlibs) (lua_State *L);
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200252
253typedef void **luaV_function;
254typedef struct {
255 const char *name;
256 luaV_function func;
257} luaV_Reg;
258
259static const luaV_Reg luaV_dll[] = {
260 /* lauxlib */
Bram Moolenaar1dced572012-04-05 16:54:08 +0200261#if LUA_VERSION_NUM <= 501
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200262 {"luaL_register", (luaV_function) &dll_luaL_register},
Bram Moolenaar1dced572012-04-05 16:54:08 +0200263 {"luaL_prepbuffer", (luaV_function) &dll_luaL_prepbuffer},
264 {"luaL_openlib", (luaV_function) &dll_luaL_openlib},
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200265 {"luaL_typerror", (luaV_function) &dll_luaL_typerror},
Bram Moolenaar1dced572012-04-05 16:54:08 +0200266 {"luaL_loadfile", (luaV_function) &dll_luaL_loadfile},
267 {"luaL_loadbuffer", (luaV_function) &dll_luaL_loadbuffer},
268#else
269 {"luaL_prepbuffsize", (luaV_function) &dll_luaL_prepbuffsize},
270 {"luaL_setfuncs", (luaV_function) &dll_luaL_setfuncs},
271 {"luaL_loadfilex", (luaV_function) &dll_luaL_loadfilex},
272 {"luaL_loadbufferx", (luaV_function) &dll_luaL_loadbufferx},
273 {"luaL_argerror", (luaV_function) &dll_luaL_argerror},
274#endif
Bram Moolenaarbd2f3c32012-04-06 14:31:00 +0200275 {"luaL_checkany", (luaV_function) &dll_luaL_checkany},
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200276 {"luaL_checklstring", (luaV_function) &dll_luaL_checklstring},
277 {"luaL_checkinteger", (luaV_function) &dll_luaL_checkinteger},
278 {"luaL_optinteger", (luaV_function) &dll_luaL_optinteger},
279 {"luaL_checktype", (luaV_function) &dll_luaL_checktype},
280 {"luaL_error", (luaV_function) &dll_luaL_error},
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200281 {"luaL_newstate", (luaV_function) &dll_luaL_newstate},
282 {"luaL_buffinit", (luaV_function) &dll_luaL_buffinit},
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200283 {"luaL_addlstring", (luaV_function) &dll_luaL_addlstring},
284 {"luaL_pushresult", (luaV_function) &dll_luaL_pushresult},
285 /* lua */
Bram Moolenaar1dced572012-04-05 16:54:08 +0200286#if LUA_VERSION_NUM <= 501
287 {"lua_tonumber", (luaV_function) &dll_lua_tonumber},
288 {"lua_tointeger", (luaV_function) &dll_lua_tointeger},
289 {"lua_call", (luaV_function) &dll_lua_call},
290 {"lua_pcall", (luaV_function) &dll_lua_pcall},
291#else
292 {"lua_tonumberx", (luaV_function) &dll_lua_tonumberx},
293 {"lua_tointegerx", (luaV_function) &dll_lua_tointegerx},
294 {"lua_callk", (luaV_function) &dll_lua_callk},
295 {"lua_pcallk", (luaV_function) &dll_lua_pcallk},
296 {"lua_getglobal", (luaV_function) &dll_lua_getglobal},
297 {"lua_setglobal", (luaV_function) &dll_lua_setglobal},
Bram Moolenaar1dced572012-04-05 16:54:08 +0200298#endif
Bram Moolenaarbd2f3c32012-04-06 14:31:00 +0200299 {"lua_typename", (luaV_function) &dll_lua_typename},
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200300 {"lua_close", (luaV_function) &dll_lua_close},
301 {"lua_gettop", (luaV_function) &dll_lua_gettop},
302 {"lua_settop", (luaV_function) &dll_lua_settop},
303 {"lua_pushvalue", (luaV_function) &dll_lua_pushvalue},
304 {"lua_replace", (luaV_function) &dll_lua_replace},
Bram Moolenaar1dced572012-04-05 16:54:08 +0200305 {"lua_remove", (luaV_function) &dll_lua_remove},
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200306 {"lua_isnumber", (luaV_function) &dll_lua_isnumber},
307 {"lua_isstring", (luaV_function) &dll_lua_isstring},
308 {"lua_type", (luaV_function) &dll_lua_type},
309 {"lua_rawequal", (luaV_function) &dll_lua_rawequal},
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200310 {"lua_toboolean", (luaV_function) &dll_lua_toboolean},
311 {"lua_tolstring", (luaV_function) &dll_lua_tolstring},
312 {"lua_touserdata", (luaV_function) &dll_lua_touserdata},
313 {"lua_pushnil", (luaV_function) &dll_lua_pushnil},
314 {"lua_pushnumber", (luaV_function) &dll_lua_pushnumber},
315 {"lua_pushinteger", (luaV_function) &dll_lua_pushinteger},
316 {"lua_pushlstring", (luaV_function) &dll_lua_pushlstring},
317 {"lua_pushstring", (luaV_function) &dll_lua_pushstring},
318 {"lua_pushfstring", (luaV_function) &dll_lua_pushfstring},
319 {"lua_pushcclosure", (luaV_function) &dll_lua_pushcclosure},
320 {"lua_pushboolean", (luaV_function) &dll_lua_pushboolean},
321 {"lua_pushlightuserdata", (luaV_function) &dll_lua_pushlightuserdata},
322 {"lua_getfield", (luaV_function) &dll_lua_getfield},
323 {"lua_rawget", (luaV_function) &dll_lua_rawget},
Bram Moolenaar1dced572012-04-05 16:54:08 +0200324 {"lua_rawgeti", (luaV_function) &dll_lua_rawgeti},
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200325 {"lua_createtable", (luaV_function) &dll_lua_createtable},
326 {"lua_newuserdata", (luaV_function) &dll_lua_newuserdata},
327 {"lua_getmetatable", (luaV_function) &dll_lua_getmetatable},
328 {"lua_setfield", (luaV_function) &dll_lua_setfield},
329 {"lua_rawset", (luaV_function) &dll_lua_rawset},
330 {"lua_rawseti", (luaV_function) &dll_lua_rawseti},
331 {"lua_setmetatable", (luaV_function) &dll_lua_setmetatable},
Bram Moolenaarbd2f3c32012-04-06 14:31:00 +0200332 {"lua_next", (luaV_function) &dll_lua_next},
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200333 /* libs */
334 {"luaopen_base", (luaV_function) &dll_luaopen_base},
335 {"luaopen_table", (luaV_function) &dll_luaopen_table},
336 {"luaopen_string", (luaV_function) &dll_luaopen_string},
337 {"luaopen_math", (luaV_function) &dll_luaopen_math},
Bram Moolenaar16c98f92010-07-28 22:46:08 +0200338 {"luaopen_io", (luaV_function) &dll_luaopen_io},
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200339 {"luaopen_os", (luaV_function) &dll_luaopen_os},
340 {"luaopen_package", (luaV_function) &dll_luaopen_package},
341 {"luaopen_debug", (luaV_function) &dll_luaopen_debug},
Bram Moolenaar16c98f92010-07-28 22:46:08 +0200342 {"luaL_openlibs", (luaV_function) &dll_luaL_openlibs},
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200343 {NULL, NULL}
344};
345
Bram Moolenaar2334b6d2010-07-22 21:32:16 +0200346static HANDLE hinstLua = NULL;
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200347
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200348 static void
349end_dynamic_lua(void)
350{
351 if (hinstLua)
352 {
Bram Moolenaar2334b6d2010-07-22 21:32:16 +0200353 close_dll(hinstLua);
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200354 hinstLua = 0;
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200355 }
356}
357
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200358 static int
359lua_link_init(char *libname, int verbose)
360{
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200361 const luaV_Reg *reg;
362 if (hinstLua) return OK;
Bram Moolenaar2334b6d2010-07-22 21:32:16 +0200363 hinstLua = load_dll(libname);
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200364 if (!hinstLua)
365 {
366 if (verbose)
367 EMSG2(_(e_loadlib), libname);
368 return FAIL;
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200369 }
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200370 for (reg = luaV_dll; reg->func; reg++)
371 {
Bram Moolenaar2334b6d2010-07-22 21:32:16 +0200372 if ((*reg->func = symbol_from_dll(hinstLua, reg->name)) == NULL)
373 {
374 close_dll(hinstLua);
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200375 hinstLua = 0;
376 if (verbose)
377 EMSG2(_(e_loadfunc), reg->name);
378 return FAIL;
379 }
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200380 }
381 return OK;
382}
383
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200384 int
385lua_enabled(int verbose)
386{
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200387 return lua_link_init(DYNAMIC_LUA_DLL, verbose) == OK;
388}
389
390#endif /* DYNAMIC_LUA */
391
Bram Moolenaar1dced572012-04-05 16:54:08 +0200392#if LUA_VERSION_NUM > 501
393 static int
394luaL_typeerror (lua_State *L, int narg, const char *tname)
395{
396 const char *msg = lua_pushfstring(L, "%s expected, got %s",
397 tname, luaL_typename(L, narg));
398 return luaL_argerror(L, narg, msg);
399}
400#endif
401
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200402
403/* ======= Internal ======= */
404
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200405 static void
406luaV_newmetatable(lua_State *L, const char *tname)
407{
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200408 lua_newtable(L);
409 lua_pushlightuserdata(L, (void *) tname);
410 lua_pushvalue(L, -2);
411 lua_rawset(L, LUA_REGISTRYINDEX);
412}
413
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200414 static void *
415luaV_toudata(lua_State *L, int ud, const char *tname)
416{
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200417 void *p = lua_touserdata(L, ud);
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200418
419 if (p != NULL) /* value is userdata? */
420 {
421 if (lua_getmetatable(L, ud)) /* does it have a metatable? */
422 {
423 luaV_getfield(L, tname); /* get metatable */
424 if (lua_rawequal(L, -1, -2)) /* MTs match? */
425 {
426 lua_pop(L, 2); /* MTs */
427 return p;
428 }
429 }
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200430 }
431 return NULL;
432}
433
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200434 static void *
Bram Moolenaar1dced572012-04-05 16:54:08 +0200435luaV_checkcache(lua_State *L, void *p)
436{
437 luaV_getudata(L, p);
438 if (lua_isnil(L, -1)) luaL_error(L, "invalid object");
439 lua_pop(L, 1);
440 return p;
441}
442
443#define luaV_unbox(L,luatyp,ud) (*((luatyp *) lua_touserdata((L),(ud))))
444
445#define luaV_checkvalid(L,luatyp,ud) \
446 luaV_checkcache((L), (void *) luaV_unbox((L),luatyp,(ud)))
447
448 static void *
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200449luaV_checkudata(lua_State *L, int ud, const char *tname)
450{
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200451 void *p = luaV_toudata(L, ud, tname);
Bram Moolenaar1dced572012-04-05 16:54:08 +0200452 if (p == NULL) luaL_typeerror(L, ud, tname);
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200453 return p;
454}
455
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200456 static void
457luaV_pushtypval(lua_State *L, typval_T *tv)
458{
Bram Moolenaar1dced572012-04-05 16:54:08 +0200459 if (tv == NULL)
460 {
461 lua_pushnil(L);
462 return;
463 }
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200464 switch (tv->v_type)
465 {
466 case VAR_STRING:
467 lua_pushstring(L, (char *) tv->vval.v_string);
468 break;
469 case VAR_NUMBER:
470 lua_pushinteger(L, (int) tv->vval.v_number);
471 break;
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200472#ifdef FEAT_FLOAT
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200473 case VAR_FLOAT:
474 lua_pushnumber(L, (lua_Number) tv->vval.v_float);
475 break;
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200476#endif
Bram Moolenaar1dced572012-04-05 16:54:08 +0200477 case VAR_LIST:
478 luaV_pushlist(L, tv->vval.v_list);
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200479 break;
Bram Moolenaar1dced572012-04-05 16:54:08 +0200480 case VAR_DICT:
481 luaV_pushdict(L, tv->vval.v_dict);
482 break;
483 default:
484 lua_pushnil(L);
485 }
486}
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200487
Bram Moolenaar1dced572012-04-05 16:54:08 +0200488/* converts lua value at 'pos' to typval 'tv' */
489 static void
490luaV_totypval (lua_State *L, int pos, typval_T *tv)
491{
492 switch(lua_type(L, pos)) {
493 case LUA_TBOOLEAN:
494 tv->v_type = VAR_NUMBER;
495 tv->vval.v_number = (varnumber_T) lua_toboolean(L, pos);
496 break;
497 case LUA_TSTRING:
498 tv->v_type = VAR_STRING;
499 tv->vval.v_string = vim_strsave((char_u *) lua_tostring(L, pos));
500 break;
501 case LUA_TNUMBER:
502#ifdef FEAT_FLOAT
503 tv->v_type = VAR_FLOAT;
504 tv->vval.v_float = (float_T) lua_tonumber(L, pos);
505#else
506 tv->v_type = VAR_NUMBER;
507 tv->vval.v_number = (varnumber_T) lua_tointeger(L, pos);
508#endif
509 break;
510 case LUA_TUSERDATA: {
511 void *p = lua_touserdata(L, pos);
512 if (lua_getmetatable(L, pos)) /* has metatable? */
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200513 {
Bram Moolenaar1dced572012-04-05 16:54:08 +0200514 /* check list */
515 luaV_getfield(L, LUAVIM_LIST);
516 if (lua_rawequal(L, -1, -2))
Bram Moolenaar2334b6d2010-07-22 21:32:16 +0200517 {
Bram Moolenaar1dced572012-04-05 16:54:08 +0200518 tv->v_type = VAR_LIST;
519 tv->vval.v_list = *((luaV_List *) p);
520 ++tv->vval.v_list->lv_refcount;
521 lua_pop(L, 2); /* MTs */
522 return;
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200523 }
Bram Moolenaar1dced572012-04-05 16:54:08 +0200524 /* check dict */
525 luaV_getfield(L, LUAVIM_DICT);
526 if (lua_rawequal(L, -1, -3))
527 {
528 tv->v_type = VAR_DICT;
529 tv->vval.v_dict = *((luaV_Dict *) p);
530 ++tv->vval.v_dict->dv_refcount;
531 lua_pop(L, 3); /* MTs */
532 return;
533 }
534 lua_pop(L, 3); /* MTs */
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200535 }
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200536 break;
537 }
538 default:
Bram Moolenaar1dced572012-04-05 16:54:08 +0200539 tv->v_type = VAR_NUMBER;
540 tv->vval.v_number = 0;
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200541 }
542}
543
544/* similar to luaL_addlstring, but replaces \0 with \n if toline and
545 * \n with \0 otherwise */
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200546 static void
547luaV_addlstring(luaL_Buffer *b, const char *s, size_t l, int toline)
548{
549 while (l--)
550 {
551 if (*s == '\0' && toline)
552 luaL_addchar(b, '\n');
553 else if (*s == '\n' && !toline)
554 luaL_addchar(b, '\0');
555 else
556 luaL_addchar(b, *s);
557 s++;
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200558 }
559}
560
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200561 static void
562luaV_pushline(lua_State *L, buf_T *buf, linenr_T n)
563{
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200564 const char *s = (const char *) ml_get_buf(buf, n, FALSE);
565 luaL_Buffer b;
566 luaL_buffinit(L, &b);
567 luaV_addlstring(&b, s, strlen(s), 0);
568 luaL_pushresult(&b);
569}
570
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200571 static char_u *
572luaV_toline(lua_State *L, int pos)
573{
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200574 size_t l;
575 const char *s = lua_tolstring(L, pos, &l);
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200576
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200577 luaL_Buffer b;
578 luaL_buffinit(L, &b);
579 luaV_addlstring(&b, s, l, 1);
580 luaL_pushresult(&b);
581 return (char_u *) lua_tostring(L, -1);
582}
583
584/* pops a string s from the top of the stack and calls mf(t) for pieces t of
585 * s separated by newlines */
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200586 static void
587luaV_msgfunc(lua_State *L, msgfunc_T mf)
588{
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200589 luaL_Buffer b;
590 size_t l;
591 const char *p, *s = lua_tolstring(L, -1, &l);
592 luaL_buffinit(L, &b);
593 luaV_addlstring(&b, s, l, 0);
594 luaL_pushresult(&b);
595 /* break string */
596 p = s = lua_tolstring(L, -1, &l);
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200597 while (l--)
598 {
599 if (*p++ == '\0') /* break? */
600 {
601 mf((char_u *) s);
602 s = p;
603 }
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200604 }
605 mf((char_u *) s);
606 lua_pop(L, 2); /* original and modified strings */
607}
608
Bram Moolenaar1dced572012-04-05 16:54:08 +0200609#define luaV_newtype(typ,tname,luatyp,luatname) \
610 static luatyp * \
611 luaV_new##tname (lua_State *L, typ *obj) \
612 { \
613 luatyp *o = (luatyp *) lua_newuserdata(L, sizeof(luatyp)); \
614 *o = obj; \
615 luaV_setudata(L, obj); /* cache[obj] = udata */ \
616 luaV_getfield(L, luatname); \
617 lua_setmetatable(L, -2); \
618 return o; \
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200619 }
Bram Moolenaar1dced572012-04-05 16:54:08 +0200620
621#define luaV_pushtype(typ,tname,luatyp) \
622 static luatyp * \
623 luaV_push##tname (lua_State *L, typ *obj) \
624 { \
625 luatyp *o = NULL; \
626 if (obj == NULL) \
627 lua_pushnil(L); \
628 else { \
629 luaV_getudata(L, obj); \
630 if (lua_isnil(L, -1)) /* not interned? */ \
631 { \
632 lua_pop(L, 1); \
633 o = luaV_new##tname(L, obj); \
634 } \
635 else \
636 o = (luatyp *) lua_touserdata(L, -1); \
637 } \
638 return o; \
639 }
640
641#define luaV_type_tostring(tname,luatname) \
642 static int \
643 luaV_##tname##_tostring (lua_State *L) \
644 { \
645 lua_pushfstring(L, "%s: %p", luatname, lua_touserdata(L, 1)); \
646 return 1; \
647 }
648
649
650/* adapted from eval.c */
651
652#define listitem_alloc() (listitem_T *)alloc(sizeof(listitem_T))
653
654 static listitem_T *
655list_find (list_T *l, long n)
656{
657 listitem_T *li;
658 if (l == NULL || n < -l->lv_len || n >= l->lv_len)
659 return NULL;
660 if (n < 0) /* search backward? */
661 for (li = l->lv_last; n < -1; li = li->li_prev)
662 n++;
663 else /* search forward */
664 for (li = l->lv_first; n > 0; li = li->li_next)
665 n--;
666 return li;
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200667}
668
Bram Moolenaar1dced572012-04-05 16:54:08 +0200669 static void
670list_remove (list_T *l, listitem_T *li)
671{
672 listwatch_T *lw;
673 --l->lv_len;
674 /* fix watchers */
675 for (lw = l->lv_watch; lw != NULL; lw = lw->lw_next)
676 if (lw->lw_item == li)
677 lw->lw_item = li->li_next;
678 /* fix list pointers */
679 if (li->li_next == NULL) /* last? */
680 l->lv_last = li->li_prev;
681 else
682 li->li_next->li_prev = li->li_prev;
683 if (li->li_prev == NULL) /* first? */
684 l->lv_first = li->li_next;
685 else
686 li->li_prev->li_next = li->li_next;
687 l->lv_idx_item = NULL;
688}
689
690 static void
691list_append(list_T *l, listitem_T *item)
692{
693 if (l->lv_last == NULL) /* empty list? */
694 l->lv_first = item;
695 else
696 l->lv_last->li_next = item;
697 item->li_prev = l->lv_last;
698 item->li_next = NULL;
699 l->lv_last = item;
700 ++l->lv_len;
701}
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200702
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200703 static int
Bram Moolenaar1dced572012-04-05 16:54:08 +0200704list_insert_tv(list_T *l, typval_T *tv, listitem_T *item)
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200705{
Bram Moolenaar1dced572012-04-05 16:54:08 +0200706 listitem_T *ni = listitem_alloc();
707
708 if (ni == NULL)
709 return FAIL;
710 copy_tv(tv, &ni->li_tv);
711 if (item == NULL)
712 list_append(l, ni);
713 else
714 {
715 ni->li_prev = item->li_prev;
716 ni->li_next = item;
717 if (item->li_prev == NULL)
718 {
719 l->lv_first = ni;
720 ++l->lv_idx;
721 }
722 else
723 {
724 item->li_prev->li_next = ni;
725 l->lv_idx_item = NULL;
726 }
727 item->li_prev = ni;
728 ++l->lv_len;
729 }
730 return OK;
731}
732
733/* set references */
734
735static void set_ref_in_tv (typval_T *tv, int copyID);
736
737 static void
738set_ref_in_dict(dict_T *d, int copyID)
739{
740 hashtab_T *ht = &d->dv_hashtab;
741 int n = ht->ht_used;
742 hashitem_T *hi;
743 for (hi = ht->ht_array; n > 0; ++hi)
744 if (!HASHITEM_EMPTY(hi))
745 {
746 dictitem_T *di = dict_lookup(hi);
747 set_ref_in_tv(&di->di_tv, copyID);
748 --n;
749 }
750}
751
752 static void
753set_ref_in_list(list_T *l, int copyID)
754{
755 listitem_T *li;
756 for (li = l->lv_first; li != NULL; li = li->li_next)
757 set_ref_in_tv(&li->li_tv, copyID);
758}
759
760 static void
761set_ref_in_tv(typval_T *tv, int copyID)
762{
763 if (tv->v_type == VAR_LIST)
764 {
765 list_T *l = tv->vval.v_list;
766 if (l != NULL && l->lv_copyID != copyID)
767 {
768 l->lv_copyID = copyID;
769 set_ref_in_list(l, copyID);
770 }
771 }
772 else if (tv->v_type == VAR_DICT)
773 {
774 dict_T *d = tv->vval.v_dict;
775 if (d != NULL && d->dv_copyID != copyID)
776 {
777 d->dv_copyID = copyID;
778 set_ref_in_dict(d, copyID);
779 }
780 }
781}
782
783
784/* ======= List type ======= */
785
786 static luaV_List *
787luaV_newlist (lua_State *L, list_T *lis)
788{
789 luaV_List *l = (luaV_List *) lua_newuserdata(L, sizeof(luaV_List));
790 *l = lis;
791 lis->lv_refcount++; /* reference in Lua */
792 luaV_setudata(L, lis); /* cache[lis] = udata */
793 luaV_getfield(L, LUAVIM_LIST);
794 lua_setmetatable(L, -2);
795 return l;
796}
797
798luaV_pushtype(list_T, list, luaV_List)
799luaV_type_tostring(list, LUAVIM_LIST)
800
801 static int
802luaV_list_gc (lua_State *L)
803{
804 list_unref(luaV_unbox(L, luaV_List, 1));
805 return 0;
806}
807
808 static int
809luaV_list_len (lua_State *L)
810{
811 list_T *l = luaV_unbox(L, luaV_List, 1);
812 lua_pushinteger(L, (l == NULL) ? 0 : (int) l->lv_len);
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200813 return 1;
814}
815
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200816 static int
Bram Moolenaar1dced572012-04-05 16:54:08 +0200817luaV_list_iter (lua_State *L)
818{
819 listitem_T *li = (listitem_T *) lua_touserdata(L, lua_upvalueindex(2));
820 if (li == NULL) return 0;
821 luaV_pushtypval(L, &li->li_tv);
822 lua_pushlightuserdata(L, (void *) li->li_next);
823 lua_replace(L, lua_upvalueindex(2));
824 return 1;
825}
826
827 static int
828luaV_list_call (lua_State *L)
829{
830 list_T *l = luaV_unbox(L, luaV_List, 1);
831 lua_pushvalue(L, lua_upvalueindex(1)); /* pass cache table along */
832 lua_pushlightuserdata(L, (void *) l->lv_first);
833 lua_pushcclosure(L, luaV_list_iter, 2);
834 return 1;
835}
836
837 static int
838luaV_list_index (lua_State *L)
839{
840 list_T *l = luaV_unbox(L, luaV_List, 1);
841 if (lua_isnumber(L, 2)) /* list item? */
842 {
843 listitem_T *li = list_find(l, (long) luaL_checkinteger(L, 2));
844 if (li == NULL)
845 lua_pushnil(L);
846 else
847 luaV_pushtypval(L, &li->li_tv);
848 }
849 else if (lua_isstring(L, 2)) /* method? */
850 {
851 const char *s = lua_tostring(L, 2);
852 if (strncmp(s, "add", 3) == 0
853 || strncmp(s, "insert", 6) == 0
854 || strncmp(s, "extend", 6) == 0)
855 {
856 lua_getmetatable(L, 1);
857 lua_getfield(L, -1, s);
858 }
859 else
860 lua_pushnil(L);
861 }
862 else
863 lua_pushnil(L);
864 return 1;
865}
866
867 static int
868luaV_list_newindex (lua_State *L)
869{
870 list_T *l = luaV_unbox(L, luaV_List, 1);
871 long n = (long) luaL_checkinteger(L, 2);
872 listitem_T *li;
873 if (l->lv_lock)
874 luaL_error(L, "list is locked");
875 li = list_find(l, n);
876 if (li == NULL) return 0;
877 if (lua_isnil(L, 3)) /* remove? */
878 {
879 list_remove(l, li);
880 clear_tv(&li->li_tv);
881 vim_free(li);
882 }
883 else
884 {
885 typval_T v;
886 luaV_totypval(L, 3, &v);
887 clear_tv(&li->li_tv);
888 copy_tv(&v, &li->li_tv);
889 }
890 return 0;
891}
892
893 static int
894luaV_list_add (lua_State *L)
895{
896 luaV_List *lis = luaV_checkudata(L, 1, LUAVIM_LIST);
897 list_T *l = (list_T *) luaV_checkcache(L, (void *) *lis);
898 listitem_T *li;
899 if (l->lv_lock)
900 luaL_error(L, "list is locked");
901 li = listitem_alloc();
902 if (li != NULL)
903 {
904 typval_T v;
905 lua_settop(L, 2);
906 luaV_totypval(L, 2, &v);
907 copy_tv(&v, &li->li_tv);
908 list_append(l, li);
909 }
910 lua_settop(L, 1);
911 return 1;
912}
913
914 static int
915luaV_list_insert (lua_State *L)
916{
917 luaV_List *lis = luaV_checkudata(L, 1, LUAVIM_LIST);
918 list_T *l = (list_T *) luaV_checkcache(L, (void *) *lis);
919 long pos = luaL_optlong(L, 3, 0);
920 listitem_T *li = NULL;
921 typval_T v;
922 if (l->lv_lock)
923 luaL_error(L, "list is locked");
924 if (pos < l->lv_len)
925 {
926 li = list_find(l, pos);
927 if (li == NULL)
928 luaL_error(L, "invalid position");
929 }
930 lua_settop(L, 2);
931 luaV_totypval(L, 2, &v);
932 list_insert_tv(l, &v, li);
933 lua_settop(L, 1);
934 return 1;
935}
936
937static const luaL_Reg luaV_List_mt[] = {
938 {"__tostring", luaV_list_tostring},
939 {"__gc", luaV_list_gc},
940 {"__len", luaV_list_len},
941 {"__call", luaV_list_call},
942 {"__index", luaV_list_index},
943 {"__newindex", luaV_list_newindex},
944 {"add", luaV_list_add},
945 {"insert", luaV_list_insert},
946 {NULL, NULL}
947};
948
949
950/* ======= Dict type ======= */
951
952 static luaV_Dict *
953luaV_newdict (lua_State *L, dict_T *dic)
954{
955 luaV_Dict *d = (luaV_Dict *) lua_newuserdata(L, sizeof(luaV_Dict));
956 *d = dic;
957 dic->dv_refcount++; /* reference in Lua */
958 luaV_setudata(L, dic); /* cache[dic] = udata */
959 luaV_getfield(L, LUAVIM_DICT);
960 lua_setmetatable(L, -2);
961 return d;
962}
963
964luaV_pushtype(dict_T, dict, luaV_Dict)
965luaV_type_tostring(dict, LUAVIM_DICT)
966
967 static int
968luaV_dict_gc (lua_State *L)
969{
970 dict_unref(luaV_unbox(L, luaV_Dict, 1));
971 return 0;
972}
973
974 static int
975luaV_dict_len (lua_State *L)
976{
977 dict_T *d = luaV_unbox(L, luaV_Dict, 1);
978 lua_pushinteger(L, (d == NULL) ? 0 : (int) d->dv_hashtab.ht_used);
979 return 1;
980}
981
982 static int
983luaV_dict_iter (lua_State *L)
984{
985 hashitem_T *hi = (hashitem_T *) lua_touserdata(L, lua_upvalueindex(2));
986 int n = lua_tointeger(L, lua_upvalueindex(3));
987 dictitem_T *di;
988 if (n <= 0) return 0;
989 while (HASHITEM_EMPTY(hi)) hi++;
990 di = dict_lookup(hi);
991 lua_pushstring(L, (char *) hi->hi_key);
992 luaV_pushtypval(L, &di->di_tv);
993 lua_pushlightuserdata(L, (void *) (hi + 1));
994 lua_replace(L, lua_upvalueindex(2));
995 lua_pushinteger(L, n - 1);
996 lua_replace(L, lua_upvalueindex(3));
997 return 2;
998}
999
1000 static int
1001luaV_dict_call (lua_State *L)
1002{
1003 dict_T *d = luaV_unbox(L, luaV_Dict, 1);
1004 hashtab_T *ht = &d->dv_hashtab;
1005 lua_pushvalue(L, lua_upvalueindex(1)); /* pass cache table along */
1006 lua_pushlightuserdata(L, (void *) ht->ht_array);
1007 lua_pushinteger(L, ht->ht_used); /* # remaining items */
1008 lua_pushcclosure(L, luaV_dict_iter, 3);
1009 return 1;
1010}
1011
1012 static int
1013luaV_dict_index (lua_State *L)
1014{
1015 dict_T *d = luaV_unbox(L, luaV_Dict, 1);
1016 char_u *key = (char_u *) luaL_checkstring(L, 2);
1017 dictitem_T *di = dict_find(d, key, -1);
1018 if (di == NULL)
1019 lua_pushnil(L);
1020 else
1021 luaV_pushtypval(L, &di->di_tv);
1022 return 1;
1023}
1024
1025 static int
1026luaV_dict_newindex (lua_State *L)
1027{
1028 dict_T *d = luaV_unbox(L, luaV_Dict, 1);
1029 char_u *key = (char_u *) luaL_checkstring(L, 2);
1030 dictitem_T *di;
1031 if (d->dv_lock)
1032 luaL_error(L, "dict is locked");
1033 di = dict_find(d, key, -1);
1034 if (di == NULL) /* non-existing key? */
1035 {
1036 if (lua_isnil(L, 3)) return 0;
1037 di = dictitem_alloc(key);
1038 if (di == NULL) return 0;
1039 if (dict_add(d, di) == FAIL)
1040 {
1041 vim_free(di);
1042 return 0;
1043 }
1044 }
1045 else
1046 clear_tv(&di->di_tv);
1047 if (lua_isnil(L, 3)) /* remove? */
1048 {
1049 hashitem_T *hi = hash_find(&d->dv_hashtab, di->di_key);
1050 hash_remove(&d->dv_hashtab, hi);
1051 dictitem_free(di);
1052 }
1053 else {
1054 typval_T v;
1055 luaV_totypval(L, 3, &v);
1056 copy_tv(&v, &di->di_tv);
1057 }
1058 return 0;
1059}
1060
1061static const luaL_Reg luaV_Dict_mt[] = {
1062 {"__tostring", luaV_dict_tostring},
1063 {"__gc", luaV_dict_gc},
1064 {"__len", luaV_dict_len},
1065 {"__call", luaV_dict_call},
1066 {"__index", luaV_dict_index},
1067 {"__newindex", luaV_dict_newindex},
1068 {NULL, NULL}
1069};
1070
1071
1072/* ======= Buffer type ======= */
1073
1074luaV_newtype(buf_T, buffer, luaV_Buffer, LUAVIM_BUFFER)
1075luaV_pushtype(buf_T, buffer, luaV_Buffer)
1076luaV_type_tostring(buffer, LUAVIM_BUFFER)
1077
1078 static int
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001079luaV_buffer_len(lua_State *L)
1080{
Bram Moolenaar1dced572012-04-05 16:54:08 +02001081 buf_T *b = (buf_T *) luaV_checkvalid(L, luaV_Buffer, 1);
1082 lua_pushinteger(L, b->b_ml.ml_line_count);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001083 return 1;
1084}
1085
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001086 static int
1087luaV_buffer_call(lua_State *L)
1088{
Bram Moolenaar1dced572012-04-05 16:54:08 +02001089 buf_T *b = (buf_T *) luaV_checkvalid(L, luaV_Buffer, 1);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001090 lua_settop(L, 1);
Bram Moolenaar1dced572012-04-05 16:54:08 +02001091 set_curbuf(b, DOBUF_SPLIT);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001092 return 1;
1093}
1094
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001095 static int
1096luaV_buffer_index(lua_State *L)
1097{
Bram Moolenaar1dced572012-04-05 16:54:08 +02001098 buf_T *b = (buf_T *) luaV_checkvalid(L, luaV_Buffer, 1);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001099 linenr_T n = (linenr_T) lua_tointeger(L, 2);
Bram Moolenaar1dced572012-04-05 16:54:08 +02001100 if (n > 0 && n <= b->b_ml.ml_line_count)
1101 luaV_pushline(L, b, n);
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001102 else if (lua_isstring(L, 2))
1103 {
1104 const char *s = lua_tostring(L, 2);
1105 if (strncmp(s, "name", 4) == 0)
Bram Moolenaar1dced572012-04-05 16:54:08 +02001106 lua_pushstring(L, (char *) b->b_sfname);
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001107 else if (strncmp(s, "fname", 5) == 0)
Bram Moolenaar1dced572012-04-05 16:54:08 +02001108 lua_pushstring(L, (char *) b->b_ffname);
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001109 else if (strncmp(s, "number", 6) == 0)
Bram Moolenaar1dced572012-04-05 16:54:08 +02001110 lua_pushinteger(L, b->b_fnum);
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001111 /* methods */
1112 else if (strncmp(s, "insert", 6) == 0
1113 || strncmp(s, "next", 4) == 0
1114 || strncmp(s, "previous", 8) == 0
1115 || strncmp(s, "isvalid", 7) == 0)
1116 {
1117 lua_getmetatable(L, 1);
1118 lua_getfield(L, -1, s);
1119 }
1120 else
1121 lua_pushnil(L);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001122 }
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001123 else
1124 lua_pushnil(L);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001125 return 1;
1126}
1127
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001128 static int
1129luaV_buffer_newindex(lua_State *L)
1130{
Bram Moolenaar1dced572012-04-05 16:54:08 +02001131 buf_T *b = (buf_T *) luaV_checkvalid(L, luaV_Buffer, 1);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001132 linenr_T n = (linenr_T) luaL_checkinteger(L, 2);
1133#ifdef HAVE_SANDBOX
1134 luaV_checksandbox(L);
1135#endif
Bram Moolenaar1dced572012-04-05 16:54:08 +02001136 if (n < 1 || n > b->b_ml.ml_line_count)
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001137 luaL_error(L, "invalid line number");
1138 if (lua_isnil(L, 3)) /* delete line */
1139 {
1140 buf_T *buf = curbuf;
Bram Moolenaar1dced572012-04-05 16:54:08 +02001141 curbuf = b;
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001142 if (u_savedel(n, 1L) == FAIL)
1143 {
1144 curbuf = buf;
1145 luaL_error(L, "cannot save undo information");
1146 }
1147 else if (ml_delete(n, FALSE) == FAIL)
1148 {
1149 curbuf = buf;
1150 luaL_error(L, "cannot delete line");
1151 }
1152 else {
1153 deleted_lines_mark(n, 1L);
Bram Moolenaar1dced572012-04-05 16:54:08 +02001154 if (b == curwin->w_buffer) /* fix cursor in current window? */
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001155 {
1156 if (curwin->w_cursor.lnum >= n)
1157 {
1158 if (curwin->w_cursor.lnum > n)
1159 {
1160 curwin->w_cursor.lnum -= 1;
1161 check_cursor_col();
1162 }
1163 else check_cursor();
1164 changed_cline_bef_curs();
1165 }
1166 invalidate_botline();
1167 }
1168 }
1169 curbuf = buf;
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001170 }
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001171 else if (lua_isstring(L, 3)) /* update line */
1172 {
1173 buf_T *buf = curbuf;
Bram Moolenaar1dced572012-04-05 16:54:08 +02001174 curbuf = b;
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001175 if (u_savesub(n) == FAIL)
1176 {
1177 curbuf = buf;
1178 luaL_error(L, "cannot save undo information");
1179 }
1180 else if (ml_replace(n, luaV_toline(L, 3), TRUE) == FAIL)
1181 {
1182 curbuf = buf;
1183 luaL_error(L, "cannot replace line");
1184 }
1185 else changed_bytes(n, 0);
1186 curbuf = buf;
Bram Moolenaar1dced572012-04-05 16:54:08 +02001187 if (b == curwin->w_buffer)
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001188 check_cursor_col();
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001189 }
1190 else
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001191 luaL_error(L, "wrong argument to change line");
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001192 return 0;
1193}
1194
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001195 static int
1196luaV_buffer_insert(lua_State *L)
1197{
Bram Moolenaar1dced572012-04-05 16:54:08 +02001198 luaV_Buffer *lb = luaV_checkudata(L, 1, LUAVIM_BUFFER);
1199 buf_T *b = (buf_T *) luaV_checkcache(L, (void *) *lb);
1200 linenr_T last = b->b_ml.ml_line_count;
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001201 linenr_T n = (linenr_T) luaL_optinteger(L, 3, last);
1202 buf_T *buf;
1203 luaL_checktype(L, 2, LUA_TSTRING);
1204#ifdef HAVE_SANDBOX
1205 luaV_checksandbox(L);
1206#endif
1207 /* fix insertion line */
1208 if (n < 0) n = 0;
1209 if (n > last) n = last;
1210 /* insert */
1211 buf = curbuf;
Bram Moolenaar1dced572012-04-05 16:54:08 +02001212 curbuf = b;
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001213 if (u_save(n, n + 1) == FAIL)
1214 {
1215 curbuf = buf;
1216 luaL_error(L, "cannot save undo information");
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001217 }
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001218 else if (ml_append(n, luaV_toline(L, 2), 0, FALSE) == FAIL)
1219 {
1220 curbuf = buf;
1221 luaL_error(L, "cannot insert line");
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001222 }
1223 else
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001224 appended_lines_mark(n, 1L);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001225 curbuf = buf;
1226 update_screen(VALID);
1227 return 0;
1228}
1229
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001230 static int
1231luaV_buffer_next(lua_State *L)
1232{
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001233 luaV_Buffer *b = luaV_checkudata(L, 1, LUAVIM_BUFFER);
Bram Moolenaar1dced572012-04-05 16:54:08 +02001234 buf_T *buf = (buf_T *) luaV_checkcache(L, (void *) *b);
1235 luaV_pushbuffer(L, buf->b_next);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001236 return 1;
1237}
1238
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001239 static int
1240luaV_buffer_previous(lua_State *L)
1241{
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001242 luaV_Buffer *b = luaV_checkudata(L, 1, LUAVIM_BUFFER);
Bram Moolenaar1dced572012-04-05 16:54:08 +02001243 buf_T *buf = (buf_T *) luaV_checkcache(L, (void *) *b);
1244 luaV_pushbuffer(L, buf->b_prev);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001245 return 1;
1246}
1247
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001248 static int
1249luaV_buffer_isvalid(lua_State *L)
1250{
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001251 luaV_Buffer *b = luaV_checkudata(L, 1, LUAVIM_BUFFER);
Bram Moolenaar1dced572012-04-05 16:54:08 +02001252 luaV_getudata(L, *b);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001253 lua_pushboolean(L, !lua_isnil(L, -1));
1254 return 1;
1255}
1256
1257static const luaL_Reg luaV_Buffer_mt[] = {
1258 {"__tostring", luaV_buffer_tostring},
1259 {"__len", luaV_buffer_len},
1260 {"__call", luaV_buffer_call},
1261 {"__index", luaV_buffer_index},
1262 {"__newindex", luaV_buffer_newindex},
1263 {"insert", luaV_buffer_insert},
1264 {"next", luaV_buffer_next},
1265 {"previous", luaV_buffer_previous},
1266 {"isvalid", luaV_buffer_isvalid},
1267 {NULL, NULL}
1268};
1269
1270
1271/* ======= Window type ======= */
1272
Bram Moolenaar1dced572012-04-05 16:54:08 +02001273luaV_newtype(win_T, window, luaV_Window, LUAVIM_WINDOW)
1274luaV_pushtype(win_T, window, luaV_Window)
1275luaV_type_tostring(window, LUAVIM_WINDOW)
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001276
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001277 static int
1278luaV_window_call(lua_State *L)
1279{
Bram Moolenaar1dced572012-04-05 16:54:08 +02001280 win_T *w = (win_T *) luaV_checkvalid(L, luaV_Window, 1);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001281 lua_settop(L, 1);
Bram Moolenaar1dced572012-04-05 16:54:08 +02001282 win_goto(w);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001283 return 1;
1284}
1285
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001286 static int
1287luaV_window_index(lua_State *L)
1288{
Bram Moolenaar1dced572012-04-05 16:54:08 +02001289 win_T *w = (win_T *) luaV_checkvalid(L, luaV_Window, 1);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001290 const char *s = luaL_checkstring(L, 2);
1291 if (strncmp(s, "buffer", 6) == 0)
Bram Moolenaar1dced572012-04-05 16:54:08 +02001292 luaV_pushbuffer(L, w->w_buffer);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001293 else if (strncmp(s, "line", 4) == 0)
Bram Moolenaar1dced572012-04-05 16:54:08 +02001294 lua_pushinteger(L, w->w_cursor.lnum);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001295 else if (strncmp(s, "col", 3) == 0)
Bram Moolenaar1dced572012-04-05 16:54:08 +02001296 lua_pushinteger(L, w->w_cursor.col + 1);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001297#ifdef FEAT_VERTSPLIT
1298 else if (strncmp(s, "width", 5) == 0)
Bram Moolenaar1dced572012-04-05 16:54:08 +02001299 lua_pushinteger(L, W_WIDTH(w));
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001300#endif
1301 else if (strncmp(s, "height", 6) == 0)
Bram Moolenaar1dced572012-04-05 16:54:08 +02001302 lua_pushinteger(L, w->w_height);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001303 /* methods */
1304 else if (strncmp(s, "next", 4) == 0
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001305 || strncmp(s, "previous", 8) == 0
1306 || strncmp(s, "isvalid", 7) == 0)
1307 {
1308 lua_getmetatable(L, 1);
1309 lua_getfield(L, -1, s);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001310 }
1311 else
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001312 lua_pushnil(L);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001313 return 1;
1314}
1315
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001316 static int
1317luaV_window_newindex (lua_State *L)
1318{
Bram Moolenaar1dced572012-04-05 16:54:08 +02001319 win_T *w = (win_T *) luaV_checkvalid(L, luaV_Window, 1);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001320 const char *s = luaL_checkstring(L, 2);
1321 int v = luaL_checkinteger(L, 3);
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001322 if (strncmp(s, "line", 4) == 0)
1323 {
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001324#ifdef HAVE_SANDBOX
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001325 luaV_checksandbox(L);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001326#endif
Bram Moolenaar1dced572012-04-05 16:54:08 +02001327 if (v < 1 || v > w->w_buffer->b_ml.ml_line_count)
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001328 luaL_error(L, "line out of range");
Bram Moolenaar1dced572012-04-05 16:54:08 +02001329 w->w_cursor.lnum = v;
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001330 update_screen(VALID);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001331 }
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001332 else if (strncmp(s, "col", 3) == 0)
1333 {
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001334#ifdef HAVE_SANDBOX
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001335 luaV_checksandbox(L);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001336#endif
Bram Moolenaar1dced572012-04-05 16:54:08 +02001337 w->w_cursor.col = v - 1;
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001338 update_screen(VALID);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001339 }
1340#ifdef FEAT_VERTSPLIT
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001341 else if (strncmp(s, "width", 5) == 0)
1342 {
1343 win_T *win = curwin;
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001344#ifdef FEAT_GUI
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001345 need_mouse_correct = TRUE;
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001346#endif
Bram Moolenaar1dced572012-04-05 16:54:08 +02001347 curwin = w;
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001348 win_setwidth(v);
1349 curwin = win;
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001350 }
1351#endif
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001352 else if (strncmp(s, "height", 6) == 0)
1353 {
1354 win_T *win = curwin;
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001355#ifdef FEAT_GUI
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001356 need_mouse_correct = TRUE;
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001357#endif
Bram Moolenaar1dced572012-04-05 16:54:08 +02001358 curwin = w;
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001359 win_setheight(v);
1360 curwin = win;
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001361 }
1362 else
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001363 luaL_error(L, "invalid window property: `%s'", s);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001364 return 0;
1365}
1366
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001367 static int
1368luaV_window_next(lua_State *L)
1369{
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001370 luaV_Window *w = luaV_checkudata(L, 1, LUAVIM_WINDOW);
Bram Moolenaar1dced572012-04-05 16:54:08 +02001371 win_T *win = (win_T *) luaV_checkcache(L, (void *) *w);
1372 luaV_pushwindow(L, win->w_next);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001373 return 1;
1374}
1375
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001376 static int
1377luaV_window_previous(lua_State *L)
1378{
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001379 luaV_Window *w = luaV_checkudata(L, 1, LUAVIM_WINDOW);
Bram Moolenaar1dced572012-04-05 16:54:08 +02001380 win_T *win = (win_T *) luaV_checkcache(L, (void *) *w);
1381 luaV_pushwindow(L, win->w_prev);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001382 return 1;
1383}
1384
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001385 static int
1386luaV_window_isvalid(lua_State *L)
1387{
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001388 luaV_Window *w = luaV_checkudata(L, 1, LUAVIM_WINDOW);
Bram Moolenaar1dced572012-04-05 16:54:08 +02001389 luaV_getudata(L, *w);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001390 lua_pushboolean(L, !lua_isnil(L, -1));
1391 return 1;
1392}
1393
1394static const luaL_Reg luaV_Window_mt[] = {
1395 {"__tostring", luaV_window_tostring},
1396 {"__call", luaV_window_call},
1397 {"__index", luaV_window_index},
1398 {"__newindex", luaV_window_newindex},
1399 {"next", luaV_window_next},
1400 {"previous", luaV_window_previous},
1401 {"isvalid", luaV_window_isvalid},
1402 {NULL, NULL}
1403};
1404
1405
1406/* ======= Vim module ======= */
1407
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001408 static int
1409luaV_print(lua_State *L)
1410{
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001411 int i, n = lua_gettop(L); /* nargs */
1412 const char *s;
1413 size_t l;
1414 luaL_Buffer b;
1415 luaL_buffinit(L, &b);
1416 lua_getglobal(L, "tostring");
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001417 for (i = 1; i <= n; i++)
1418 {
1419 lua_pushvalue(L, -1); /* tostring */
1420 lua_pushvalue(L, i); /* arg */
1421 lua_call(L, 1, 1);
1422 s = lua_tolstring(L, -1, &l);
1423 if (s == NULL)
1424 return luaL_error(L, "cannot convert to string");
1425 if (i > 1) luaL_addchar(&b, ' '); /* use space instead of tab */
1426 luaV_addlstring(&b, s, l, 0);
1427 lua_pop(L, 1);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001428 }
1429 luaL_pushresult(&b);
1430 luaV_msg(L);
1431 return 0;
1432}
1433
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001434 static int
Bram Moolenaar38e2b062011-09-21 17:15:39 +02001435luaV_debug(lua_State *L)
1436{
1437 lua_settop(L, 0);
1438 lua_getglobal(L, "vim");
1439 lua_getfield(L, -1, "eval");
1440 lua_remove(L, -2); /* vim.eval at position 1 */
1441 for (;;)
1442 {
1443 const char *input;
1444 size_t l;
1445 lua_pushvalue(L, 1); /* vim.eval */
1446 lua_pushliteral(L, "input('lua_debug> ')");
1447 lua_call(L, 1, 1); /* return string */
1448 input = lua_tolstring(L, -1, &l);
1449 if (l == 0 || strcmp(input, "cont") == 0)
1450 return 0;
1451 msg_putchar('\n'); /* avoid outputting on input line */
1452 if (luaL_loadbuffer(L, input, l, "=(debug command)")
1453 || lua_pcall(L, 0, 0, 0))
1454 luaV_emsg(L);
1455 lua_settop(L, 1); /* remove eventual returns, but keep vim.eval */
1456 }
1457}
1458
1459 static int
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001460luaV_command(lua_State *L)
1461{
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001462 do_cmdline_cmd((char_u *) luaL_checkstring(L, 1));
1463 update_screen(VALID);
1464 return 0;
1465}
1466
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001467 static int
1468luaV_eval(lua_State *L)
1469{
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001470 typval_T *tv = eval_expr((char_u *) luaL_checkstring(L, 1), NULL);
1471 if (tv == NULL) luaL_error(L, "invalid expression");
1472 luaV_pushtypval(L, tv);
1473 return 1;
1474}
1475
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001476 static int
Bram Moolenaar0d2e4fc2010-07-18 12:35:47 +02001477luaV_beep(lua_State *L UNUSED)
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001478{
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001479 vim_beep();
1480 return 0;
1481}
1482
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001483 static int
1484luaV_line(lua_State *L)
1485{
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001486 luaV_pushline(L, curbuf, curwin->w_cursor.lnum);
1487 return 1;
1488}
1489
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001490 static int
Bram Moolenaar1dced572012-04-05 16:54:08 +02001491luaV_list(lua_State *L)
1492{
1493 list_T *l = list_alloc();
1494 if (l == NULL)
1495 lua_pushnil(L);
1496 else
1497 luaV_newlist(L, l);
1498 return 1;
1499}
1500
1501 static int
1502luaV_dict(lua_State *L)
1503{
1504 dict_T *d = dict_alloc();
1505 if (d == NULL)
1506 lua_pushnil(L);
1507 else
1508 luaV_newdict(L, d);
1509 return 1;
1510}
1511
1512 static int
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001513luaV_buffer(lua_State *L)
1514{
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001515 buf_T *buf;
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001516 if (lua_isstring(L, 1)) /* get by number or name? */
1517 {
1518 if (lua_isnumber(L, 1)) /* by number? */
1519 {
1520 int n = lua_tointeger(L, 1);
1521 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
1522 if (buf->b_fnum == n) break;
1523 }
1524 else { /* by name */
1525 size_t l;
1526 const char *s = lua_tolstring(L, 1, &l);
1527 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
1528 {
1529 if (buf->b_ffname == NULL || buf->b_sfname == NULL)
1530 {
1531 if (l == 0) break;
1532 }
Bram Moolenaar0d2e4fc2010-07-18 12:35:47 +02001533 else if (strncmp(s, (char *)buf->b_ffname, l) == 0
1534 || strncmp(s, (char *)buf->b_sfname, l) == 0)
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001535 break;
1536 }
1537 }
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001538 }
Bram Moolenaar1dced572012-04-05 16:54:08 +02001539 else
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001540 buf = (lua_toboolean(L, 1)) ? firstbuf : curbuf; /* first buffer? */
Bram Moolenaar1dced572012-04-05 16:54:08 +02001541 luaV_pushbuffer(L, buf);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001542 return 1;
1543}
1544
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001545 static int
1546luaV_window(lua_State *L)
1547{
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001548 win_T *win;
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001549 if (lua_isnumber(L, 1)) /* get by number? */
1550 {
1551 int n = lua_tointeger(L, 1);
1552 for (win = firstwin; win != NULL; win = win->w_next, n--)
1553 if (n == 1) break;
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001554 }
Bram Moolenaar1dced572012-04-05 16:54:08 +02001555 else
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001556 win = (lua_toboolean(L, 1)) ? firstwin : curwin; /* first window? */
Bram Moolenaar1dced572012-04-05 16:54:08 +02001557 luaV_pushwindow(L, win);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001558 return 1;
1559}
1560
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001561 static int
1562luaV_open(lua_State *L)
1563{
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001564 char_u *s = NULL;
1565#ifdef HAVE_SANDBOX
1566 luaV_checksandbox(L);
1567#endif
1568 if (lua_isstring(L, 1)) s = (char_u *) lua_tostring(L, 1);
Bram Moolenaarfa263a52011-12-08 16:00:16 +01001569 luaV_pushbuffer(L, buflist_new(s, NULL, 1L, BLN_LISTED));
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001570 return 1;
1571}
1572
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001573 static int
Bram Moolenaar1dced572012-04-05 16:54:08 +02001574luaV_type(lua_State *L)
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001575{
Bram Moolenaar1dced572012-04-05 16:54:08 +02001576 luaL_checkany(L, 1);
1577 if (lua_type(L, 1) == LUA_TUSERDATA) /* check vim udata? */
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001578 {
Bram Moolenaar1dced572012-04-05 16:54:08 +02001579 lua_settop(L, 1);
1580 if (lua_getmetatable(L, 1))
1581 {
1582 luaV_getfield(L, LUAVIM_LIST);
1583 if (lua_rawequal(L, -1, 2))
1584 {
1585 lua_pushstring(L, "list");
1586 return 1;
1587 }
1588 luaV_getfield(L, LUAVIM_DICT);
1589 if (lua_rawequal(L, -1, 2))
1590 {
1591 lua_pushstring(L, "dict");
1592 return 1;
1593 }
1594 luaV_getfield(L, LUAVIM_BUFFER);
1595 if (lua_rawequal(L, -1, 2))
1596 {
1597 lua_pushstring(L, "buffer");
1598 return 1;
1599 }
1600 luaV_getfield(L, LUAVIM_WINDOW);
1601 if (lua_rawequal(L, -1, 2))
1602 {
1603 lua_pushstring(L, "window");
1604 return 1;
1605 }
1606 }
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001607 }
Bram Moolenaar1dced572012-04-05 16:54:08 +02001608 lua_pushstring(L, luaL_typename(L, 1)); /* fallback */
1609 return 1;
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001610}
1611
1612static const luaL_Reg luaV_module[] = {
1613 {"command", luaV_command},
1614 {"eval", luaV_eval},
1615 {"beep", luaV_beep},
1616 {"line", luaV_line},
Bram Moolenaar1dced572012-04-05 16:54:08 +02001617 {"list", luaV_list},
1618 {"dict", luaV_dict},
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001619 {"buffer", luaV_buffer},
1620 {"window", luaV_window},
1621 {"open", luaV_open},
Bram Moolenaar1dced572012-04-05 16:54:08 +02001622 {"type", luaV_type},
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001623 {NULL, NULL}
1624};
1625
Bram Moolenaar1dced572012-04-05 16:54:08 +02001626/* for freeing list, dict, buffer and window objects; lightuserdata as arg */
1627 static int
1628luaV_free(lua_State *L)
1629{
1630 lua_pushnil(L);
1631 luaV_setudata(L, lua_touserdata(L, 1));
1632 return 0;
1633}
1634
1635 static int
1636luaV_luaeval (lua_State *L)
1637{
1638 luaL_Buffer b;
1639 size_t l;
1640 const char *str = lua_tolstring(L, 1, &l);
1641 typval_T *arg = (typval_T *) lua_touserdata(L, 2);
1642 typval_T *rettv = (typval_T *) lua_touserdata(L, 3);
1643 luaL_buffinit(L, &b);
1644 luaL_addlstring(&b, LUAVIM_EVALHEADER, sizeof(LUAVIM_EVALHEADER) - 1);
1645 luaL_addlstring(&b, str, l);
1646 luaL_pushresult(&b);
1647 str = lua_tolstring(L, -1, &l);
1648 if (luaL_loadbuffer(L, str, l, LUAVIM_EVALNAME)) /* compile error? */
1649 {
1650 luaV_emsg(L);
1651 return 0;
1652 }
1653 luaV_pushtypval(L, arg);
1654 if (lua_pcall(L, 1, 1, 0)) /* running error? */
1655 {
1656 luaV_emsg(L);
1657 return 0;
1658 }
1659 luaV_totypval(L, -1, rettv);
1660 return 0;
1661}
1662
1663 static int
1664luaV_setref (lua_State *L)
1665{
1666 int copyID = lua_tointeger(L, 1);
1667 typval_T tv;
1668 luaV_getfield(L, LUAVIM_LIST);
1669 luaV_getfield(L, LUAVIM_DICT);
1670 lua_pushnil(L);
1671 while (lua_next(L, lua_upvalueindex(1)) != 0) /* traverse cache table */
1672 {
1673 lua_getmetatable(L, -1);
1674 if (lua_rawequal(L, -1, 2)) /* list? */
1675 {
1676 tv.v_type = VAR_LIST;
1677 tv.vval.v_list = (list_T *) lua_touserdata(L, 4); /* key */
1678 }
1679 else if (lua_rawequal(L, -1, 3)) /* dict? */
1680 {
1681 tv.v_type = VAR_DICT;
1682 tv.vval.v_dict = (dict_T *) lua_touserdata(L, 4); /* key */
1683 }
1684 lua_pop(L, 2); /* metatable and value */
1685 set_ref_in_tv(&tv, copyID);
1686 }
1687 return 0;
1688}
1689
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001690 static int
1691luaopen_vim(lua_State *L)
1692{
Bram Moolenaar1dced572012-04-05 16:54:08 +02001693 /* set cache table */
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001694 lua_newtable(L);
1695 lua_newtable(L);
Bram Moolenaar1dced572012-04-05 16:54:08 +02001696 lua_pushstring(L, "v");
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001697 lua_setfield(L, -2, "__mode");
Bram Moolenaar1dced572012-04-05 16:54:08 +02001698 lua_setmetatable(L, -2); /* cache is weak-valued */
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001699 /* print */
1700 lua_pushcfunction(L, luaV_print);
1701 lua_setglobal(L, "print");
Bram Moolenaar38e2b062011-09-21 17:15:39 +02001702 /* debug.debug */
1703 lua_getglobal(L, "debug");
1704 lua_pushcfunction(L, luaV_debug);
1705 lua_setfield(L, -2, "debug");
1706 lua_pop(L, 1);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001707 /* free */
1708 lua_pushlightuserdata(L, (void *) LUAVIM_FREE);
Bram Moolenaar1dced572012-04-05 16:54:08 +02001709 lua_pushvalue(L, 1); /* cache table */
1710 lua_pushcclosure(L, luaV_free, 1);
1711 lua_rawset(L, LUA_REGISTRYINDEX);
1712 /* luaeval */
1713 lua_pushlightuserdata(L, (void *) LUAVIM_LUAEVAL);
1714 lua_pushvalue(L, 1); /* cache table */
1715 lua_pushcclosure(L, luaV_luaeval, 1);
1716 lua_rawset(L, LUA_REGISTRYINDEX);
1717 /* setref */
1718 lua_pushlightuserdata(L, (void *) LUAVIM_SETREF);
1719 lua_pushvalue(L, 1); /* cache table */
1720 lua_pushcclosure(L, luaV_setref, 1);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001721 lua_rawset(L, LUA_REGISTRYINDEX);
1722 /* register */
Bram Moolenaar1dced572012-04-05 16:54:08 +02001723 luaV_newmetatable(L, LUAVIM_LIST);
1724 lua_pushvalue(L, 1);
1725 luaV_openlib(L, luaV_List_mt, 1);
1726 luaV_newmetatable(L, LUAVIM_DICT);
1727 lua_pushvalue(L, 1);
1728 luaV_openlib(L, luaV_Dict_mt, 1);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001729 luaV_newmetatable(L, LUAVIM_BUFFER);
Bram Moolenaar1dced572012-04-05 16:54:08 +02001730 lua_pushvalue(L, 1); /* cache table */
1731 luaV_openlib(L, luaV_Buffer_mt, 1);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001732 luaV_newmetatable(L, LUAVIM_WINDOW);
Bram Moolenaar1dced572012-04-05 16:54:08 +02001733 lua_pushvalue(L, 1); /* cache table */
1734 luaV_openlib(L, luaV_Window_mt, 1);
1735 lua_newtable(L); /* vim table */
1736 lua_pushvalue(L, 1); /* cache table */
1737 luaV_openlib(L, luaV_module, 1);
1738 lua_setglobal(L, LUAVIM_NAME);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001739 return 0;
1740}
1741
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001742 static lua_State *
1743luaV_newstate(void)
1744{
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001745 lua_State *L = luaL_newstate();
Bram Moolenaar16c98f92010-07-28 22:46:08 +02001746 luaL_openlibs(L); /* core libs */
1747 lua_pushcfunction(L, luaopen_vim); /* vim */
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001748 lua_call(L, 0, 0);
1749 return L;
1750}
1751
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001752 static void
1753luaV_setrange(lua_State *L, int line1, int line2)
1754{
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001755 lua_getglobal(L, LUAVIM_NAME);
1756 lua_pushinteger(L, line1);
1757 lua_setfield(L, -2, "firstline");
1758 lua_pushinteger(L, line2);
1759 lua_setfield(L, -2, "lastline");
1760 lua_pop(L, 1); /* vim table */
1761}
1762
1763
1764/* ======= Interface ======= */
1765
1766static lua_State *L = NULL;
1767
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001768 static int
Bram Moolenaar1dced572012-04-05 16:54:08 +02001769lua_isopen(void)
Bram Moolenaar2bd6a1b2010-08-12 22:14:01 +02001770{
1771 return L != NULL;
1772}
1773
1774 static int
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001775lua_init(void)
1776{
Bram Moolenaar1dced572012-04-05 16:54:08 +02001777 if (!lua_isopen())
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001778 {
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001779#ifdef DYNAMIC_LUA
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001780 if (!lua_enabled(TRUE))
1781 {
1782 EMSG(_("Lua library cannot be loaded."));
1783 return FAIL;
1784 }
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001785#endif
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001786 L = luaV_newstate();
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001787 }
1788 return OK;
1789}
1790
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001791 void
1792lua_end(void)
1793{
Bram Moolenaar1dced572012-04-05 16:54:08 +02001794 if (lua_isopen())
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001795 {
1796 lua_close(L);
1797 L = NULL;
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001798#ifdef DYNAMIC_LUA
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001799 end_dynamic_lua();
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001800#endif
1801 }
1802}
1803
1804/* ex commands */
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001805 void
1806ex_lua(exarg_T *eap)
1807{
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001808 char *script;
1809 if (lua_init() == FAIL) return;
1810 script = (char *) script_get(eap, eap->arg);
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001811 if (!eap->skip)
1812 {
1813 char *s = (script) ? script : (char *) eap->arg;
1814 luaV_setrange(L, eap->line1, eap->line2);
1815 if (luaL_loadbuffer(L, s, strlen(s), LUAVIM_CHUNKNAME)
1816 || lua_pcall(L, 0, 0, 0))
1817 luaV_emsg(L);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001818 }
1819 if (script != NULL) vim_free(script);
1820}
1821
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001822 void
1823ex_luado(exarg_T *eap)
1824{
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001825 linenr_T l;
1826 const char *s = (const char *) eap->arg;
1827 luaL_Buffer b;
1828 size_t len;
1829 if (lua_init() == FAIL) return;
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001830 if (u_save(eap->line1 - 1, eap->line2 + 1) == FAIL)
1831 {
1832 EMSG(_("cannot save undo information"));
1833 return;
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001834 }
1835 luaV_setrange(L, eap->line1, eap->line2);
1836 luaL_buffinit(L, &b);
Bram Moolenaarbd2f3c32012-04-06 14:31:00 +02001837 luaL_addlstring(&b, "return function(line, linenr) ", 30); /* header */
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001838 luaL_addlstring(&b, s, strlen(s));
1839 luaL_addlstring(&b, " end", 4); /* footer */
1840 luaL_pushresult(&b);
1841 s = lua_tolstring(L, -1, &len);
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001842 if (luaL_loadbuffer(L, s, len, LUAVIM_CHUNKNAME))
1843 {
1844 luaV_emsg(L);
1845 lua_pop(L, 1); /* function body */
1846 return;
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001847 }
1848 lua_call(L, 0, 1);
1849 lua_replace(L, -2); /* function -> body */
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001850 for (l = eap->line1; l <= eap->line2; l++)
1851 {
1852 lua_pushvalue(L, -1); /* function */
1853 luaV_pushline(L, curbuf, l); /* current line as arg */
Bram Moolenaarbd2f3c32012-04-06 14:31:00 +02001854 lua_pushinteger(L, l); /* current line number as arg */
1855 if (lua_pcall(L, 2, 1, 0))
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001856 {
1857 luaV_emsg(L);
1858 break;
1859 }
1860 if (lua_isstring(L, -1)) /* update line? */
1861 {
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001862#ifdef HAVE_SANDBOX
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001863 luaV_checksandbox(L);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001864#endif
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001865 ml_replace(l, luaV_toline(L, -1), TRUE);
1866 changed_bytes(l, 0);
1867 lua_pop(L, 1); /* result from luaV_toline */
1868 }
1869 lua_pop(L, 1); /* line */
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001870 }
1871 lua_pop(L, 1); /* function */
1872 check_cursor();
1873 update_screen(NOT_VALID);
1874}
1875
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001876 void
1877ex_luafile(exarg_T *eap)
1878{
1879 if (lua_init() == FAIL)
1880 return;
1881 if (!eap->skip)
1882 {
1883 luaV_setrange(L, eap->line1, eap->line2);
1884 if (luaL_loadfile(L, (char *) eap->arg) || lua_pcall(L, 0, 0, 0))
1885 luaV_emsg(L);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001886 }
1887}
1888
Bram Moolenaar1dced572012-04-05 16:54:08 +02001889#define luaV_freetype(typ,tname) \
1890 void \
1891 lua_##tname##_free(typ *o) \
1892 { \
1893 if (!lua_isopen()) return; \
1894 luaV_getfield(L, LUAVIM_FREE); \
1895 lua_pushlightuserdata(L, (void *) o); \
1896 lua_call(L, 1, 0); \
1897 }
1898
1899luaV_freetype(buf_T, buffer)
1900luaV_freetype(win_T, window)
1901
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001902 void
Bram Moolenaar1dced572012-04-05 16:54:08 +02001903do_luaeval (char_u *str, typval_T *arg, typval_T *rettv)
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001904{
Bram Moolenaar1dced572012-04-05 16:54:08 +02001905 lua_init();
1906 luaV_getfield(L, LUAVIM_LUAEVAL);
1907 lua_pushstring(L, (char *) str);
1908 lua_pushlightuserdata(L, (void *) arg);
1909 lua_pushlightuserdata(L, (void *) rettv);
1910 lua_call(L, 3, 0);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001911}
1912
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001913 void
Bram Moolenaar1dced572012-04-05 16:54:08 +02001914set_ref_in_lua (int copyID)
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001915{
Bram Moolenaar1dced572012-04-05 16:54:08 +02001916 if (!lua_isopen()) return;
1917 luaV_getfield(L, LUAVIM_SETREF);
1918 lua_pushinteger(L, copyID);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001919 lua_call(L, 1, 0);
1920}
1921
1922#endif