blob: f0d5a4d04d7cbd8107af2339e37b8c3ec873570e [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 Moolenaarca06da92018-07-01 15:12:05 +020031typedef struct {
32 typval_T tv; // funcref
33 typval_T args;
34 dict_T *self; // selfdict
35} luaV_Funcref;
Bram Moolenaar0ba04292010-07-14 23:23:17 +020036typedef void (*msgfunc_T)(char_u *);
37
Bram Moolenaar1dced572012-04-05 16:54:08 +020038static const char LUAVIM_DICT[] = "dict";
39static const char LUAVIM_LIST[] = "list";
Bram Moolenaarca06da92018-07-01 15:12:05 +020040static const char LUAVIM_FUNCREF[] = "funcref";
Bram Moolenaar0ba04292010-07-14 23:23:17 +020041static const char LUAVIM_BUFFER[] = "buffer";
42static const char LUAVIM_WINDOW[] = "window";
43static const char LUAVIM_FREE[] = "luaV_free";
Bram Moolenaar1dced572012-04-05 16:54:08 +020044static const char LUAVIM_LUAEVAL[] = "luaV_luaeval";
45static const char LUAVIM_SETREF[] = "luaV_setref";
Bram Moolenaar0ba04292010-07-14 23:23:17 +020046
Bram Moolenaar1dced572012-04-05 16:54:08 +020047/* 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))
Bram Moolenaar0ba04292010-07-14 23:23:17 +020057#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)
Bram Moolenaarca06da92018-07-01 15:12:05 +020064#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)
Bram Moolenaar0ba04292010-07-14 23:23:17 +020069
Bram Moolenaarca06da92018-07-01 15:12:05 +020070static luaV_List *luaV_pushlist(lua_State *L, list_T *lis);
71static luaV_Dict *luaV_pushdict(lua_State *L, dict_T *dic);
72static luaV_Funcref *luaV_pushfuncref(lua_State *L, typval_T *tv);
Bram Moolenaar1dced572012-04-05 16:54:08 +020073
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
Bram Moolenaar0ba04292010-07-14 23:23:17 +020080
81#ifdef DYNAMIC_LUA
Bram Moolenaar2334b6d2010-07-22 21:32:16 +020082
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
Bram Moolenaarebbcb822010-10-23 14:02:54 +020090# define load_dll vimLoadLib
Bram Moolenaar2334b6d2010-07-22 21:32:16 +020091# define symbol_from_dll GetProcAddress
92# define close_dll FreeLibrary
93#endif
94
Bram Moolenaar0ba04292010-07-14 23:23:17 +020095/* lauxlib */
Bram Moolenaar1dced572012-04-05 16:54:08 +020096#if LUA_VERSION_NUM <= 501
Bram Moolenaar0ba04292010-07-14 23:23:17 +020097#define luaL_register dll_luaL_register
Bram Moolenaar1dced572012-04-05 16:54:08 +020098#define luaL_prepbuffer dll_luaL_prepbuffer
99#define luaL_openlib dll_luaL_openlib
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200100#define luaL_typerror dll_luaL_typerror
Bram Moolenaar1dced572012-04-05 16:54:08 +0200101#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
Bram Moolenaarbd2f3c32012-04-06 14:31:00 +0200110#define luaL_checkany dll_luaL_checkany
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200111#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
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200116#define luaL_newstate dll_luaL_newstate
117#define luaL_buffinit dll_luaL_buffinit
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200118#define luaL_addlstring dll_luaL_addlstring
119#define luaL_pushresult dll_luaL_pushresult
120/* lua */
Bram Moolenaar1dced572012-04-05 16:54:08 +0200121#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
Bram Moolenaar1dced572012-04-05 16:54:08 +0200133#endif
Bram Moolenaar1f860d82015-06-27 18:36:16 +0200134#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
Bram Moolenaarbd2f3c32012-04-06 14:31:00 +0200142#define lua_typename dll_lua_typename
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200143#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
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200147#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
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200151#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
Bram Moolenaar1dced572012-04-05 16:54:08 +0200165#define lua_rawgeti dll_lua_rawgeti
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200166#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
Bram Moolenaarbd2f3c32012-04-06 14:31:00 +0200173#define lua_next dll_lua_next
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200174/* 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
Bram Moolenaar16c98f92010-07-28 22:46:08 +0200179#define luaopen_io dll_luaopen_io
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200180#define luaopen_os dll_luaopen_os
181#define luaopen_package dll_luaopen_package
182#define luaopen_debug dll_luaopen_debug
Bram Moolenaar16c98f92010-07-28 22:46:08 +0200183#define luaL_openlibs dll_luaL_openlibs
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200184
185/* lauxlib */
Bram Moolenaar1dced572012-04-05 16:54:08 +0200186#if LUA_VERSION_NUM <= 501
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200187void (*dll_luaL_register) (lua_State *L, const char *libname, const luaL_Reg *l);
Bram Moolenaar1dced572012-04-05 16:54:08 +0200188char *(*dll_luaL_prepbuffer) (luaL_Buffer *B);
189void (*dll_luaL_openlib) (lua_State *L, const char *libname, const luaL_Reg *l, int nup);
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200190int (*dll_luaL_typerror) (lua_State *L, int narg, const char *tname);
Bram Moolenaar1dced572012-04-05 16:54:08 +0200191int (*dll_luaL_loadfile) (lua_State *L, const char *filename);
192int (*dll_luaL_loadbuffer) (lua_State *L, const char *buff, size_t sz, const char *name);
193#else
194char *(*dll_luaL_prepbuffsize) (luaL_Buffer *B, size_t sz);
195void (*dll_luaL_setfuncs) (lua_State *L, const luaL_Reg *l, int nup);
196int (*dll_luaL_loadfilex) (lua_State *L, const char *filename, const char *mode);
197int (*dll_luaL_loadbufferx) (lua_State *L, const char *buff, size_t sz, const char *name, const char *mode);
198int (*dll_luaL_argerror) (lua_State *L, int numarg, const char *extramsg);
199#endif
Bram Moolenaarbd2f3c32012-04-06 14:31:00 +0200200void (*dll_luaL_checkany) (lua_State *L, int narg);
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200201const char *(*dll_luaL_checklstring) (lua_State *L, int numArg, size_t *l);
202lua_Integer (*dll_luaL_checkinteger) (lua_State *L, int numArg);
203lua_Integer (*dll_luaL_optinteger) (lua_State *L, int nArg, lua_Integer def);
204void (*dll_luaL_checktype) (lua_State *L, int narg, int t);
205int (*dll_luaL_error) (lua_State *L, const char *fmt, ...);
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200206lua_State *(*dll_luaL_newstate) (void);
207void (*dll_luaL_buffinit) (lua_State *L, luaL_Buffer *B);
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200208void (*dll_luaL_addlstring) (luaL_Buffer *B, const char *s, size_t l);
209void (*dll_luaL_pushresult) (luaL_Buffer *B);
210/* lua */
Bram Moolenaar1dced572012-04-05 16:54:08 +0200211#if LUA_VERSION_NUM <= 501
212lua_Number (*dll_lua_tonumber) (lua_State *L, int idx);
213lua_Integer (*dll_lua_tointeger) (lua_State *L, int idx);
214void (*dll_lua_call) (lua_State *L, int nargs, int nresults);
215int (*dll_lua_pcall) (lua_State *L, int nargs, int nresults, int errfunc);
216#else
217lua_Number (*dll_lua_tonumberx) (lua_State *L, int idx, int *isnum);
218lua_Integer (*dll_lua_tointegerx) (lua_State *L, int idx, int *isnum);
219void (*dll_lua_callk) (lua_State *L, int nargs, int nresults, int ctx,
Bram Moolenaardb913952012-06-29 12:54:53 +0200220 lua_CFunction k);
Bram Moolenaar1dced572012-04-05 16:54:08 +0200221int (*dll_lua_pcallk) (lua_State *L, int nargs, int nresults, int errfunc,
Bram Moolenaardb913952012-06-29 12:54:53 +0200222 int ctx, lua_CFunction k);
Bram Moolenaar1dced572012-04-05 16:54:08 +0200223void (*dll_lua_getglobal) (lua_State *L, const char *var);
224void (*dll_lua_setglobal) (lua_State *L, const char *var);
Bram Moolenaar1f860d82015-06-27 18:36:16 +0200225#endif
226#if LUA_VERSION_NUM <= 502
227void (*dll_lua_replace) (lua_State *L, int idx);
228void (*dll_lua_remove) (lua_State *L, int idx);
229#endif
230#if LUA_VERSION_NUM >= 503
231void (*dll_lua_rotate) (lua_State *L, int idx, int n);
Bram Moolenaar9514b1f2015-06-25 18:27:32 +0200232void (*dll_lua_copy) (lua_State *L, int fromidx, int toidx);
Bram Moolenaar1dced572012-04-05 16:54:08 +0200233#endif
Bram Moolenaarbd2f3c32012-04-06 14:31:00 +0200234const char *(*dll_lua_typename) (lua_State *L, int tp);
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200235void (*dll_lua_close) (lua_State *L);
236int (*dll_lua_gettop) (lua_State *L);
237void (*dll_lua_settop) (lua_State *L, int idx);
238void (*dll_lua_pushvalue) (lua_State *L, int idx);
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200239int (*dll_lua_isnumber) (lua_State *L, int idx);
240int (*dll_lua_isstring) (lua_State *L, int idx);
241int (*dll_lua_type) (lua_State *L, int idx);
242int (*dll_lua_rawequal) (lua_State *L, int idx1, int idx2);
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200243int (*dll_lua_toboolean) (lua_State *L, int idx);
244const char *(*dll_lua_tolstring) (lua_State *L, int idx, size_t *len);
245void *(*dll_lua_touserdata) (lua_State *L, int idx);
246void (*dll_lua_pushnil) (lua_State *L);
247void (*dll_lua_pushnumber) (lua_State *L, lua_Number n);
248void (*dll_lua_pushinteger) (lua_State *L, lua_Integer n);
249void (*dll_lua_pushlstring) (lua_State *L, const char *s, size_t l);
250void (*dll_lua_pushstring) (lua_State *L, const char *s);
251const char *(*dll_lua_pushfstring) (lua_State *L, const char *fmt, ...);
252void (*dll_lua_pushcclosure) (lua_State *L, lua_CFunction fn, int n);
253void (*dll_lua_pushboolean) (lua_State *L, int b);
254void (*dll_lua_pushlightuserdata) (lua_State *L, void *p);
255void (*dll_lua_getfield) (lua_State *L, int idx, const char *k);
256void (*dll_lua_rawget) (lua_State *L, int idx);
Bram Moolenaar1dced572012-04-05 16:54:08 +0200257void (*dll_lua_rawgeti) (lua_State *L, int idx, int n);
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200258void (*dll_lua_createtable) (lua_State *L, int narr, int nrec);
259void *(*dll_lua_newuserdata) (lua_State *L, size_t sz);
260int (*dll_lua_getmetatable) (lua_State *L, int objindex);
261void (*dll_lua_setfield) (lua_State *L, int idx, const char *k);
262void (*dll_lua_rawset) (lua_State *L, int idx);
263void (*dll_lua_rawseti) (lua_State *L, int idx, int n);
264int (*dll_lua_setmetatable) (lua_State *L, int objindex);
Bram Moolenaarbd2f3c32012-04-06 14:31:00 +0200265int (*dll_lua_next) (lua_State *L, int idx);
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200266/* libs */
267int (*dll_luaopen_base) (lua_State *L);
268int (*dll_luaopen_table) (lua_State *L);
269int (*dll_luaopen_string) (lua_State *L);
270int (*dll_luaopen_math) (lua_State *L);
Bram Moolenaar16c98f92010-07-28 22:46:08 +0200271int (*dll_luaopen_io) (lua_State *L);
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200272int (*dll_luaopen_os) (lua_State *L);
273int (*dll_luaopen_package) (lua_State *L);
274int (*dll_luaopen_debug) (lua_State *L);
Bram Moolenaar16c98f92010-07-28 22:46:08 +0200275void (*dll_luaL_openlibs) (lua_State *L);
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200276
277typedef void **luaV_function;
278typedef struct {
279 const char *name;
280 luaV_function func;
281} luaV_Reg;
282
283static const luaV_Reg luaV_dll[] = {
284 /* lauxlib */
Bram Moolenaar1dced572012-04-05 16:54:08 +0200285#if LUA_VERSION_NUM <= 501
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200286 {"luaL_register", (luaV_function) &dll_luaL_register},
Bram Moolenaar1dced572012-04-05 16:54:08 +0200287 {"luaL_prepbuffer", (luaV_function) &dll_luaL_prepbuffer},
288 {"luaL_openlib", (luaV_function) &dll_luaL_openlib},
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200289 {"luaL_typerror", (luaV_function) &dll_luaL_typerror},
Bram Moolenaar1dced572012-04-05 16:54:08 +0200290 {"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
Bram Moolenaarbd2f3c32012-04-06 14:31:00 +0200299 {"luaL_checkany", (luaV_function) &dll_luaL_checkany},
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200300 {"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},
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200305 {"luaL_newstate", (luaV_function) &dll_luaL_newstate},
306 {"luaL_buffinit", (luaV_function) &dll_luaL_buffinit},
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200307 {"luaL_addlstring", (luaV_function) &dll_luaL_addlstring},
308 {"luaL_pushresult", (luaV_function) &dll_luaL_pushresult},
309 /* lua */
Bram Moolenaar1dced572012-04-05 16:54:08 +0200310#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},
Bram Moolenaar1dced572012-04-05 16:54:08 +0200322#endif
Bram Moolenaar1f860d82015-06-27 18:36:16 +0200323#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
Bram Moolenaarbd2f3c32012-04-06 14:31:00 +0200331 {"lua_typename", (luaV_function) &dll_lua_typename},
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200332 {"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},
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200336 {"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},
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200340 {"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},
Bram Moolenaar1dced572012-04-05 16:54:08 +0200354 {"lua_rawgeti", (luaV_function) &dll_lua_rawgeti},
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200355 {"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},
Bram Moolenaarbd2f3c32012-04-06 14:31:00 +0200362 {"lua_next", (luaV_function) &dll_lua_next},
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200363 /* 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},
Bram Moolenaar16c98f92010-07-28 22:46:08 +0200368 {"luaopen_io", (luaV_function) &dll_luaopen_io},
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200369 {"luaopen_os", (luaV_function) &dll_luaopen_os},
370 {"luaopen_package", (luaV_function) &dll_luaopen_package},
371 {"luaopen_debug", (luaV_function) &dll_luaopen_debug},
Bram Moolenaar16c98f92010-07-28 22:46:08 +0200372 {"luaL_openlibs", (luaV_function) &dll_luaL_openlibs},
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200373 {NULL, NULL}
374};
375
Bram Moolenaar2334b6d2010-07-22 21:32:16 +0200376static HANDLE hinstLua = NULL;
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200377
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200378 static void
379end_dynamic_lua(void)
380{
381 if (hinstLua)
382 {
Bram Moolenaar2334b6d2010-07-22 21:32:16 +0200383 close_dll(hinstLua);
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200384 hinstLua = 0;
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200385 }
386}
387
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200388 static int
389lua_link_init(char *libname, int verbose)
390{
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200391 const luaV_Reg *reg;
392 if (hinstLua) return OK;
Bram Moolenaar2334b6d2010-07-22 21:32:16 +0200393 hinstLua = load_dll(libname);
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200394 if (!hinstLua)
395 {
396 if (verbose)
397 EMSG2(_(e_loadlib), libname);
398 return FAIL;
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200399 }
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200400 for (reg = luaV_dll; reg->func; reg++)
401 {
Bram Moolenaar2334b6d2010-07-22 21:32:16 +0200402 if ((*reg->func = symbol_from_dll(hinstLua, reg->name)) == NULL)
403 {
404 close_dll(hinstLua);
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200405 hinstLua = 0;
406 if (verbose)
407 EMSG2(_(e_loadfunc), reg->name);
408 return FAIL;
409 }
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200410 }
411 return OK;
412}
Bram Moolenaard90b6c02016-08-28 18:10:45 +0200413#endif /* DYNAMIC_LUA */
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200414
Bram Moolenaard90b6c02016-08-28 18:10:45 +0200415#if defined(DYNAMIC_LUA) || defined(PROTO)
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200416 int
417lua_enabled(int verbose)
418{
Bram Moolenaar25e4fcd2016-01-09 14:57:47 +0100419 return lua_link_init((char *)p_luadll, verbose) == OK;
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200420}
Bram Moolenaard90b6c02016-08-28 18:10:45 +0200421#endif
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200422
Bram Moolenaar1dced572012-04-05 16:54:08 +0200423#if LUA_VERSION_NUM > 501
424 static int
425luaL_typeerror (lua_State *L, int narg, const char *tname)
426{
427 const char *msg = lua_pushfstring(L, "%s expected, got %s",
Bram Moolenaardb913952012-06-29 12:54:53 +0200428 tname, luaL_typename(L, narg));
Bram Moolenaar1dced572012-04-05 16:54:08 +0200429 return luaL_argerror(L, narg, msg);
430}
431#endif
432
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200433
434/* ======= Internal ======= */
435
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200436 static void
437luaV_newmetatable(lua_State *L, const char *tname)
438{
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200439 lua_newtable(L);
440 lua_pushlightuserdata(L, (void *) tname);
441 lua_pushvalue(L, -2);
442 lua_rawset(L, LUA_REGISTRYINDEX);
443}
444
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200445 static void *
446luaV_toudata(lua_State *L, int ud, const char *tname)
447{
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200448 void *p = lua_touserdata(L, ud);
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200449
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 }
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200461 }
462 return NULL;
463}
464
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200465 static void *
Bram Moolenaar1dced572012-04-05 16:54:08 +0200466luaV_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 *
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200480luaV_checkudata(lua_State *L, int ud, const char *tname)
481{
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200482 void *p = luaV_toudata(L, ud, tname);
Bram Moolenaar1dced572012-04-05 16:54:08 +0200483 if (p == NULL) luaL_typeerror(L, ud, tname);
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200484 return p;
485}
486
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200487 static void
488luaV_pushtypval(lua_State *L, typval_T *tv)
489{
Bram Moolenaar1dced572012-04-05 16:54:08 +0200490 if (tv == NULL)
491 {
492 lua_pushnil(L);
493 return;
494 }
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200495 switch (tv->v_type)
496 {
497 case VAR_STRING:
Bram Moolenaard04da7c2012-10-14 03:41:59 +0200498 lua_pushstring(L, tv->vval.v_string == NULL
499 ? "" : (char *)tv->vval.v_string);
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200500 break;
501 case VAR_NUMBER:
502 lua_pushinteger(L, (int) tv->vval.v_number);
503 break;
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200504#ifdef FEAT_FLOAT
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200505 case VAR_FLOAT:
506 lua_pushnumber(L, (lua_Number) tv->vval.v_float);
507 break;
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200508#endif
Bram Moolenaar1dced572012-04-05 16:54:08 +0200509 case VAR_LIST:
510 luaV_pushlist(L, tv->vval.v_list);
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200511 break;
Bram Moolenaar1dced572012-04-05 16:54:08 +0200512 case VAR_DICT:
513 luaV_pushdict(L, tv->vval.v_dict);
514 break;
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100515 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;
Bram Moolenaarca06da92018-07-01 15:12:05 +0200521 case VAR_FUNC:
522 luaV_pushfuncref(L, tv);
523 break;
Bram Moolenaar1dced572012-04-05 16:54:08 +0200524 default:
525 lua_pushnil(L);
526 }
527}
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200528
Bram Moolenaarca06da92018-07-01 15:12:05 +0200529/*
530 * Converts lua value at 'pos' to typval 'tv'.
531 * Returns OK or FAIL.
532 */
533 static int
534luaV_totypval(lua_State *L, int pos, typval_T *tv)
Bram Moolenaar1dced572012-04-05 16:54:08 +0200535{
Bram Moolenaarca06da92018-07-01 15:12:05 +0200536 int status = OK;
537
538 switch (lua_type(L, pos))
539 {
Bram Moolenaar1dced572012-04-05 16:54:08 +0200540 case LUA_TBOOLEAN:
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100541 tv->v_type = VAR_SPECIAL;
Bram Moolenaar1dced572012-04-05 16:54:08 +0200542 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;
Bram Moolenaarca06da92018-07-01 15:12:05 +0200557 case LUA_TUSERDATA:
558 {
Bram Moolenaar1dced572012-04-05 16:54:08 +0200559 void *p = lua_touserdata(L, pos);
Bram Moolenaarca06da92018-07-01 15:12:05 +0200560
Bram Moolenaar1dced572012-04-05 16:54:08 +0200561 if (lua_getmetatable(L, pos)) /* has metatable? */
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200562 {
Bram Moolenaar1dced572012-04-05 16:54:08 +0200563 /* check list */
564 luaV_getfield(L, LUAVIM_LIST);
565 if (lua_rawequal(L, -1, -2))
Bram Moolenaar2334b6d2010-07-22 21:32:16 +0200566 {
Bram Moolenaar1dced572012-04-05 16:54:08 +0200567 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 */
Bram Moolenaarca06da92018-07-01 15:12:05 +0200571 break;
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200572 }
Bram Moolenaar1dced572012-04-05 16:54:08 +0200573 /* 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 */
Bram Moolenaarca06da92018-07-01 15:12:05 +0200581 break;
Bram Moolenaar1dced572012-04-05 16:54:08 +0200582 }
Bram Moolenaarca06da92018-07-01 15:12:05 +0200583 /* 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 */
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200593 }
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200594 }
Bram Moolenaarca06da92018-07-01 15:12:05 +0200595 /* FALLTHROUGH */
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200596 default:
Bram Moolenaar1dced572012-04-05 16:54:08 +0200597 tv->v_type = VAR_NUMBER;
598 tv->vval.v_number = 0;
Bram Moolenaarca06da92018-07-01 15:12:05 +0200599 status = FAIL;
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200600 }
Bram Moolenaarca06da92018-07-01 15:12:05 +0200601 return status;
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200602}
603
604/* similar to luaL_addlstring, but replaces \0 with \n if toline and
605 * \n with \0 otherwise */
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200606 static void
607luaV_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++;
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200618 }
619}
620
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200621 static void
622luaV_pushline(lua_State *L, buf_T *buf, linenr_T n)
623{
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200624 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
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200631 static char_u *
632luaV_toline(lua_State *L, int pos)
633{
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200634 size_t l;
635 const char *s = lua_tolstring(L, pos, &l);
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200636
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200637 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 */
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200646 static void
647luaV_msgfunc(lua_State *L, msgfunc_T mf)
648{
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200649 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);
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200657 while (l--)
658 {
659 if (*p++ == '\0') /* break? */
660 {
661 mf((char_u *) s);
662 s = p;
663 }
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200664 }
665 mf((char_u *) s);
666 lua_pop(L, 2); /* original and modified strings */
667}
668
Bram Moolenaar1dced572012-04-05 16:54:08 +0200669#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; \
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200679 }
Bram Moolenaar1dced572012-04-05 16:54:08 +0200680
681#define luaV_pushtype(typ,tname,luatyp) \
682 static luatyp * \
Bram Moolenaarca06da92018-07-01 15:12:05 +0200683 luaV_push##tname(lua_State *L, typ *obj) \
Bram Moolenaar1dced572012-04-05 16:54:08 +0200684 { \
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
Bram Moolenaar1dced572012-04-05 16:54:08 +0200709/* ======= List type ======= */
710
711 static luaV_List *
712luaV_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
723luaV_pushtype(list_T, list, luaV_List)
724luaV_type_tostring(list, LUAVIM_LIST)
725
726 static int
Bram Moolenaar1dced572012-04-05 16:54:08 +0200727luaV_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);
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200731 return 1;
732}
733
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200734 static int
Bram Moolenaar1dced572012-04-05 16:54:08 +0200735luaV_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
746luaV_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
756luaV_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
Bram Moolenaarb3766472013-04-15 13:49:21 +0200771 || strncmp(s, "insert", 6) == 0)
Bram Moolenaar1dced572012-04-05 16:54:08 +0200772 {
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
785luaV_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 {
Bram Moolenaar3ec7f4e2014-05-07 17:31:37 +0200796 vimlist_remove(l, li, li);
Bram Moolenaar1dced572012-04-05 16:54:08 +0200797 clear_tv(&li->li_tv);
798 vim_free(li);
799 }
800 else
801 {
802 typval_T v;
Bram Moolenaarca06da92018-07-01 15:12:05 +0200803 luaV_checktypval(L, 3, &v, "setting list item");
Bram Moolenaar1dced572012-04-05 16:54:08 +0200804 clear_tv(&li->li_tv);
805 copy_tv(&v, &li->li_tv);
Bram Moolenaarb3766472013-04-15 13:49:21 +0200806 clear_tv(&v);
Bram Moolenaar1dced572012-04-05 16:54:08 +0200807 }
808 return 0;
809}
810
811 static int
812luaV_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);
Bram Moolenaarb3766472013-04-15 13:49:21 +0200816 typval_T v;
Bram Moolenaar1dced572012-04-05 16:54:08 +0200817 if (l->lv_lock)
818 luaL_error(L, "list is locked");
Bram Moolenaarb3766472013-04-15 13:49:21 +0200819 lua_settop(L, 2);
Bram Moolenaarca06da92018-07-01 15:12:05 +0200820 luaV_checktypval(L, 2, &v, "adding list item");
Bram Moolenaarb3766472013-04-15 13:49:21 +0200821 if (list_append_tv(l, &v) == FAIL)
Bram Moolenaar1dced572012-04-05 16:54:08 +0200822 {
Bram Moolenaarb3766472013-04-15 13:49:21 +0200823 clear_tv(&v);
Bram Moolenaarca06da92018-07-01 15:12:05 +0200824 luaL_error(L, "failed to add item to list");
Bram Moolenaar1dced572012-04-05 16:54:08 +0200825 }
Bram Moolenaarb3766472013-04-15 13:49:21 +0200826 clear_tv(&v);
Bram Moolenaar1dced572012-04-05 16:54:08 +0200827 lua_settop(L, 1);
828 return 1;
829}
830
831 static int
832luaV_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);
Bram Moolenaar46538ee2015-02-17 16:28:55 +0100836 long pos = (long) luaL_optinteger(L, 3, 0);
Bram Moolenaar1dced572012-04-05 16:54:08 +0200837 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);
Bram Moolenaarca06da92018-07-01 15:12:05 +0200848 luaV_checktypval(L, 2, &v, "inserting list item");
Bram Moolenaarb3766472013-04-15 13:49:21 +0200849 if (list_insert_tv(l, &v, li) == FAIL)
850 {
851 clear_tv(&v);
Bram Moolenaarca06da92018-07-01 15:12:05 +0200852 luaL_error(L, "failed to add item to list");
Bram Moolenaarb3766472013-04-15 13:49:21 +0200853 }
854 clear_tv(&v);
Bram Moolenaar1dced572012-04-05 16:54:08 +0200855 lua_settop(L, 1);
856 return 1;
857}
858
859static const luaL_Reg luaV_List_mt[] = {
860 {"__tostring", luaV_list_tostring},
Bram Moolenaar1dced572012-04-05 16:54:08 +0200861 {"__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 *
874luaV_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
885luaV_pushtype(dict_T, dict, luaV_Dict)
886luaV_type_tostring(dict, LUAVIM_DICT)
887
888 static int
Bram Moolenaar1dced572012-04-05 16:54:08 +0200889luaV_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
Bram Moolenaarfeeaa682013-02-14 22:19:51 +0100897luaV_dict_iter (lua_State *L UNUSED)
Bram Moolenaar1dced572012-04-05 16:54:08 +0200898{
Bram Moolenaarfeeaa682013-02-14 22:19:51 +0100899#ifdef FEAT_EVAL
Bram Moolenaar1dced572012-04-05 16:54:08 +0200900 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;
Bram Moolenaarfeeaa682013-02-14 22:19:51 +0100913#else
914 return 0;
915#endif
Bram Moolenaar1dced572012-04-05 16:54:08 +0200916}
917
918 static int
919luaV_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
Bram Moolenaarca06da92018-07-01 15:12:05 +0200931luaV_dict_index(lua_State *L)
Bram Moolenaar1dced572012-04-05 16:54:08 +0200932{
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);
Bram Moolenaarca06da92018-07-01 15:12:05 +0200936
Bram Moolenaar1dced572012-04-05 16:54:08 +0200937 if (di == NULL)
938 lua_pushnil(L);
939 else
Bram Moolenaarca06da92018-07-01 15:12:05 +0200940 {
Bram Moolenaar1dced572012-04-05 16:54:08 +0200941 luaV_pushtypval(L, &di->di_tv);
Bram Moolenaarca06da92018-07-01 15:12:05 +0200942 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 }
Bram Moolenaar1dced572012-04-05 16:54:08 +0200949 return 1;
950}
951
952 static int
Bram Moolenaarca06da92018-07-01 15:12:05 +0200953luaV_dict_newindex(lua_State *L)
Bram Moolenaar1dced572012-04-05 16:54:08 +0200954{
955 dict_T *d = luaV_unbox(L, luaV_Dict, 1);
956 char_u *key = (char_u *) luaL_checkstring(L, 2);
957 dictitem_T *di;
Bram Moolenaarca06da92018-07-01 15:12:05 +0200958 typval_T v;
Bram Moolenaar1dced572012-04-05 16:54:08 +0200959 if (d->dv_lock)
960 luaL_error(L, "dict is locked");
Bram Moolenaarca06da92018-07-01 15:12:05 +0200961 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 }
Bram Moolenaar1dced572012-04-05 16:54:08 +0200968 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 }
Bram Moolenaarca06da92018-07-01 15:12:05 +0200988 else
989 {
Bram Moolenaar1dced572012-04-05 16:54:08 +0200990 copy_tv(&v, &di->di_tv);
Bram Moolenaarb3766472013-04-15 13:49:21 +0200991 clear_tv(&v);
Bram Moolenaar1dced572012-04-05 16:54:08 +0200992 }
993 return 0;
994}
995
996static const luaL_Reg luaV_Dict_mt[] = {
997 {"__tostring", luaV_dict_tostring},
Bram Moolenaar1dced572012-04-05 16:54:08 +0200998 {"__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
Bram Moolenaarca06da92018-07-01 15:12:05 +02001006/* ======= Funcref type ======= */
1007
1008 static luaV_Funcref *
1009luaV_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 *
1027luaV_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
1036luaV_type_tostring(funcref, LUAVIM_FUNCREF)
1037
1038 static int
1039luaV_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
1051luaV_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
1060luaV_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
1083static 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
Bram Moolenaar1dced572012-04-05 16:54:08 +02001092/* ======= Buffer type ======= */
1093
1094luaV_newtype(buf_T, buffer, luaV_Buffer, LUAVIM_BUFFER)
1095luaV_pushtype(buf_T, buffer, luaV_Buffer)
1096luaV_type_tostring(buffer, LUAVIM_BUFFER)
1097
1098 static int
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001099luaV_buffer_len(lua_State *L)
1100{
Bram Moolenaar1dced572012-04-05 16:54:08 +02001101 buf_T *b = (buf_T *) luaV_checkvalid(L, luaV_Buffer, 1);
1102 lua_pushinteger(L, b->b_ml.ml_line_count);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001103 return 1;
1104}
1105
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001106 static int
1107luaV_buffer_call(lua_State *L)
1108{
Bram Moolenaar1dced572012-04-05 16:54:08 +02001109 buf_T *b = (buf_T *) luaV_checkvalid(L, luaV_Buffer, 1);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001110 lua_settop(L, 1);
Bram Moolenaar1dced572012-04-05 16:54:08 +02001111 set_curbuf(b, DOBUF_SPLIT);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001112 return 1;
1113}
1114
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001115 static int
1116luaV_buffer_index(lua_State *L)
1117{
Bram Moolenaar1dced572012-04-05 16:54:08 +02001118 buf_T *b = (buf_T *) luaV_checkvalid(L, luaV_Buffer, 1);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001119 linenr_T n = (linenr_T) lua_tointeger(L, 2);
Bram Moolenaar1dced572012-04-05 16:54:08 +02001120 if (n > 0 && n <= b->b_ml.ml_line_count)
1121 luaV_pushline(L, b, n);
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001122 else if (lua_isstring(L, 2))
1123 {
1124 const char *s = lua_tostring(L, 2);
1125 if (strncmp(s, "name", 4) == 0)
Bram Moolenaarfe08df42018-07-07 23:07:41 +02001126 lua_pushstring(L, (b->b_sfname == NULL)
1127 ? "" : (char *) b->b_sfname);
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001128 else if (strncmp(s, "fname", 5) == 0)
Bram Moolenaarfe08df42018-07-07 23:07:41 +02001129 lua_pushstring(L, (b->b_ffname == NULL)
1130 ? "" : (char *) b->b_ffname);
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001131 else if (strncmp(s, "number", 6) == 0)
Bram Moolenaar1dced572012-04-05 16:54:08 +02001132 lua_pushinteger(L, b->b_fnum);
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001133 /* methods */
1134 else if (strncmp(s, "insert", 6) == 0
1135 || strncmp(s, "next", 4) == 0
1136 || strncmp(s, "previous", 8) == 0
1137 || strncmp(s, "isvalid", 7) == 0)
1138 {
1139 lua_getmetatable(L, 1);
1140 lua_getfield(L, -1, s);
1141 }
1142 else
1143 lua_pushnil(L);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001144 }
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001145 else
1146 lua_pushnil(L);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001147 return 1;
1148}
1149
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001150 static int
1151luaV_buffer_newindex(lua_State *L)
1152{
Bram Moolenaar1dced572012-04-05 16:54:08 +02001153 buf_T *b = (buf_T *) luaV_checkvalid(L, luaV_Buffer, 1);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001154 linenr_T n = (linenr_T) luaL_checkinteger(L, 2);
1155#ifdef HAVE_SANDBOX
1156 luaV_checksandbox(L);
1157#endif
Bram Moolenaar1dced572012-04-05 16:54:08 +02001158 if (n < 1 || n > b->b_ml.ml_line_count)
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001159 luaL_error(L, "invalid line number");
1160 if (lua_isnil(L, 3)) /* delete line */
1161 {
1162 buf_T *buf = curbuf;
Bram Moolenaar1dced572012-04-05 16:54:08 +02001163 curbuf = b;
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001164 if (u_savedel(n, 1L) == FAIL)
1165 {
1166 curbuf = buf;
1167 luaL_error(L, "cannot save undo information");
1168 }
1169 else if (ml_delete(n, FALSE) == FAIL)
1170 {
1171 curbuf = buf;
1172 luaL_error(L, "cannot delete line");
1173 }
Bram Moolenaarca06da92018-07-01 15:12:05 +02001174 else
1175 {
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001176 deleted_lines_mark(n, 1L);
Bram Moolenaar1dced572012-04-05 16:54:08 +02001177 if (b == curwin->w_buffer) /* fix cursor in current window? */
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001178 {
1179 if (curwin->w_cursor.lnum >= n)
1180 {
1181 if (curwin->w_cursor.lnum > n)
1182 {
1183 curwin->w_cursor.lnum -= 1;
1184 check_cursor_col();
1185 }
1186 else check_cursor();
1187 changed_cline_bef_curs();
1188 }
1189 invalidate_botline();
1190 }
1191 }
1192 curbuf = buf;
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001193 }
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001194 else if (lua_isstring(L, 3)) /* update line */
1195 {
1196 buf_T *buf = curbuf;
Bram Moolenaar1dced572012-04-05 16:54:08 +02001197 curbuf = b;
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001198 if (u_savesub(n) == FAIL)
1199 {
1200 curbuf = buf;
1201 luaL_error(L, "cannot save undo information");
1202 }
1203 else if (ml_replace(n, luaV_toline(L, 3), TRUE) == FAIL)
1204 {
1205 curbuf = buf;
1206 luaL_error(L, "cannot replace line");
1207 }
1208 else changed_bytes(n, 0);
1209 curbuf = buf;
Bram Moolenaar1dced572012-04-05 16:54:08 +02001210 if (b == curwin->w_buffer)
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001211 check_cursor_col();
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001212 }
1213 else
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001214 luaL_error(L, "wrong argument to change line");
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001215 return 0;
1216}
1217
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001218 static int
1219luaV_buffer_insert(lua_State *L)
1220{
Bram Moolenaar1dced572012-04-05 16:54:08 +02001221 luaV_Buffer *lb = luaV_checkudata(L, 1, LUAVIM_BUFFER);
1222 buf_T *b = (buf_T *) luaV_checkcache(L, (void *) *lb);
1223 linenr_T last = b->b_ml.ml_line_count;
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001224 linenr_T n = (linenr_T) luaL_optinteger(L, 3, last);
1225 buf_T *buf;
1226 luaL_checktype(L, 2, LUA_TSTRING);
1227#ifdef HAVE_SANDBOX
1228 luaV_checksandbox(L);
1229#endif
1230 /* fix insertion line */
1231 if (n < 0) n = 0;
1232 if (n > last) n = last;
1233 /* insert */
1234 buf = curbuf;
Bram Moolenaar1dced572012-04-05 16:54:08 +02001235 curbuf = b;
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001236 if (u_save(n, n + 1) == FAIL)
1237 {
1238 curbuf = buf;
1239 luaL_error(L, "cannot save undo information");
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001240 }
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001241 else if (ml_append(n, luaV_toline(L, 2), 0, FALSE) == FAIL)
1242 {
1243 curbuf = buf;
1244 luaL_error(L, "cannot insert line");
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001245 }
1246 else
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001247 appended_lines_mark(n, 1L);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001248 curbuf = buf;
1249 update_screen(VALID);
1250 return 0;
1251}
1252
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001253 static int
1254luaV_buffer_next(lua_State *L)
1255{
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001256 luaV_Buffer *b = luaV_checkudata(L, 1, LUAVIM_BUFFER);
Bram Moolenaar1dced572012-04-05 16:54:08 +02001257 buf_T *buf = (buf_T *) luaV_checkcache(L, (void *) *b);
1258 luaV_pushbuffer(L, buf->b_next);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001259 return 1;
1260}
1261
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001262 static int
1263luaV_buffer_previous(lua_State *L)
1264{
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001265 luaV_Buffer *b = luaV_checkudata(L, 1, LUAVIM_BUFFER);
Bram Moolenaar1dced572012-04-05 16:54:08 +02001266 buf_T *buf = (buf_T *) luaV_checkcache(L, (void *) *b);
1267 luaV_pushbuffer(L, buf->b_prev);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001268 return 1;
1269}
1270
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001271 static int
1272luaV_buffer_isvalid(lua_State *L)
1273{
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001274 luaV_Buffer *b = luaV_checkudata(L, 1, LUAVIM_BUFFER);
Bram Moolenaar1dced572012-04-05 16:54:08 +02001275 luaV_getudata(L, *b);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001276 lua_pushboolean(L, !lua_isnil(L, -1));
1277 return 1;
1278}
1279
1280static const luaL_Reg luaV_Buffer_mt[] = {
1281 {"__tostring", luaV_buffer_tostring},
1282 {"__len", luaV_buffer_len},
1283 {"__call", luaV_buffer_call},
1284 {"__index", luaV_buffer_index},
1285 {"__newindex", luaV_buffer_newindex},
1286 {"insert", luaV_buffer_insert},
1287 {"next", luaV_buffer_next},
1288 {"previous", luaV_buffer_previous},
1289 {"isvalid", luaV_buffer_isvalid},
1290 {NULL, NULL}
1291};
1292
1293
1294/* ======= Window type ======= */
1295
Bram Moolenaar1dced572012-04-05 16:54:08 +02001296luaV_newtype(win_T, window, luaV_Window, LUAVIM_WINDOW)
1297luaV_pushtype(win_T, window, luaV_Window)
1298luaV_type_tostring(window, LUAVIM_WINDOW)
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001299
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001300 static int
1301luaV_window_call(lua_State *L)
1302{
Bram Moolenaar1dced572012-04-05 16:54:08 +02001303 win_T *w = (win_T *) luaV_checkvalid(L, luaV_Window, 1);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001304 lua_settop(L, 1);
Bram Moolenaar1dced572012-04-05 16:54:08 +02001305 win_goto(w);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001306 return 1;
1307}
1308
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001309 static int
1310luaV_window_index(lua_State *L)
1311{
Bram Moolenaar1dced572012-04-05 16:54:08 +02001312 win_T *w = (win_T *) luaV_checkvalid(L, luaV_Window, 1);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001313 const char *s = luaL_checkstring(L, 2);
1314 if (strncmp(s, "buffer", 6) == 0)
Bram Moolenaar1dced572012-04-05 16:54:08 +02001315 luaV_pushbuffer(L, w->w_buffer);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001316 else if (strncmp(s, "line", 4) == 0)
Bram Moolenaar1dced572012-04-05 16:54:08 +02001317 lua_pushinteger(L, w->w_cursor.lnum);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001318 else if (strncmp(s, "col", 3) == 0)
Bram Moolenaar1dced572012-04-05 16:54:08 +02001319 lua_pushinteger(L, w->w_cursor.col + 1);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001320 else if (strncmp(s, "width", 5) == 0)
Bram Moolenaar02631462017-09-22 15:20:32 +02001321 lua_pushinteger(L, w->w_width);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001322 else if (strncmp(s, "height", 6) == 0)
Bram Moolenaar1dced572012-04-05 16:54:08 +02001323 lua_pushinteger(L, w->w_height);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001324 /* methods */
1325 else if (strncmp(s, "next", 4) == 0
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001326 || strncmp(s, "previous", 8) == 0
1327 || strncmp(s, "isvalid", 7) == 0)
1328 {
1329 lua_getmetatable(L, 1);
1330 lua_getfield(L, -1, s);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001331 }
1332 else
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001333 lua_pushnil(L);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001334 return 1;
1335}
1336
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001337 static int
1338luaV_window_newindex (lua_State *L)
1339{
Bram Moolenaar1dced572012-04-05 16:54:08 +02001340 win_T *w = (win_T *) luaV_checkvalid(L, luaV_Window, 1);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001341 const char *s = luaL_checkstring(L, 2);
1342 int v = luaL_checkinteger(L, 3);
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001343 if (strncmp(s, "line", 4) == 0)
1344 {
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001345#ifdef HAVE_SANDBOX
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001346 luaV_checksandbox(L);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001347#endif
Bram Moolenaar1dced572012-04-05 16:54:08 +02001348 if (v < 1 || v > w->w_buffer->b_ml.ml_line_count)
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001349 luaL_error(L, "line out of range");
Bram Moolenaar1dced572012-04-05 16:54:08 +02001350 w->w_cursor.lnum = v;
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001351 update_screen(VALID);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001352 }
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001353 else if (strncmp(s, "col", 3) == 0)
1354 {
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001355#ifdef HAVE_SANDBOX
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001356 luaV_checksandbox(L);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001357#endif
Bram Moolenaar1dced572012-04-05 16:54:08 +02001358 w->w_cursor.col = v - 1;
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001359 update_screen(VALID);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001360 }
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001361 else if (strncmp(s, "width", 5) == 0)
1362 {
1363 win_T *win = curwin;
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001364#ifdef FEAT_GUI
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001365 need_mouse_correct = TRUE;
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001366#endif
Bram Moolenaar1dced572012-04-05 16:54:08 +02001367 curwin = w;
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001368 win_setwidth(v);
1369 curwin = win;
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001370 }
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001371 else if (strncmp(s, "height", 6) == 0)
1372 {
1373 win_T *win = curwin;
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001374#ifdef FEAT_GUI
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001375 need_mouse_correct = TRUE;
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001376#endif
Bram Moolenaar1dced572012-04-05 16:54:08 +02001377 curwin = w;
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001378 win_setheight(v);
1379 curwin = win;
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001380 }
1381 else
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001382 luaL_error(L, "invalid window property: `%s'", s);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001383 return 0;
1384}
1385
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001386 static int
1387luaV_window_next(lua_State *L)
1388{
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001389 luaV_Window *w = luaV_checkudata(L, 1, LUAVIM_WINDOW);
Bram Moolenaar1dced572012-04-05 16:54:08 +02001390 win_T *win = (win_T *) luaV_checkcache(L, (void *) *w);
1391 luaV_pushwindow(L, win->w_next);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001392 return 1;
1393}
1394
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001395 static int
1396luaV_window_previous(lua_State *L)
1397{
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001398 luaV_Window *w = luaV_checkudata(L, 1, LUAVIM_WINDOW);
Bram Moolenaar1dced572012-04-05 16:54:08 +02001399 win_T *win = (win_T *) luaV_checkcache(L, (void *) *w);
1400 luaV_pushwindow(L, win->w_prev);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001401 return 1;
1402}
1403
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001404 static int
1405luaV_window_isvalid(lua_State *L)
1406{
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001407 luaV_Window *w = luaV_checkudata(L, 1, LUAVIM_WINDOW);
Bram Moolenaar1dced572012-04-05 16:54:08 +02001408 luaV_getudata(L, *w);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001409 lua_pushboolean(L, !lua_isnil(L, -1));
1410 return 1;
1411}
1412
1413static const luaL_Reg luaV_Window_mt[] = {
1414 {"__tostring", luaV_window_tostring},
1415 {"__call", luaV_window_call},
1416 {"__index", luaV_window_index},
1417 {"__newindex", luaV_window_newindex},
1418 {"next", luaV_window_next},
1419 {"previous", luaV_window_previous},
1420 {"isvalid", luaV_window_isvalid},
1421 {NULL, NULL}
1422};
1423
1424
1425/* ======= Vim module ======= */
1426
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001427 static int
1428luaV_print(lua_State *L)
1429{
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001430 int i, n = lua_gettop(L); /* nargs */
1431 const char *s;
1432 size_t l;
1433 luaL_Buffer b;
1434 luaL_buffinit(L, &b);
1435 lua_getglobal(L, "tostring");
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001436 for (i = 1; i <= n; i++)
1437 {
1438 lua_pushvalue(L, -1); /* tostring */
1439 lua_pushvalue(L, i); /* arg */
1440 lua_call(L, 1, 1);
1441 s = lua_tolstring(L, -1, &l);
1442 if (s == NULL)
1443 return luaL_error(L, "cannot convert to string");
1444 if (i > 1) luaL_addchar(&b, ' '); /* use space instead of tab */
1445 luaV_addlstring(&b, s, l, 0);
1446 lua_pop(L, 1);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001447 }
1448 luaL_pushresult(&b);
1449 luaV_msg(L);
1450 return 0;
1451}
1452
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001453 static int
Bram Moolenaar38e2b062011-09-21 17:15:39 +02001454luaV_debug(lua_State *L)
1455{
1456 lua_settop(L, 0);
1457 lua_getglobal(L, "vim");
1458 lua_getfield(L, -1, "eval");
1459 lua_remove(L, -2); /* vim.eval at position 1 */
1460 for (;;)
1461 {
1462 const char *input;
1463 size_t l;
1464 lua_pushvalue(L, 1); /* vim.eval */
1465 lua_pushliteral(L, "input('lua_debug> ')");
1466 lua_call(L, 1, 1); /* return string */
1467 input = lua_tolstring(L, -1, &l);
1468 if (l == 0 || strcmp(input, "cont") == 0)
1469 return 0;
1470 msg_putchar('\n'); /* avoid outputting on input line */
1471 if (luaL_loadbuffer(L, input, l, "=(debug command)")
1472 || lua_pcall(L, 0, 0, 0))
1473 luaV_emsg(L);
1474 lua_settop(L, 1); /* remove eventual returns, but keep vim.eval */
1475 }
1476}
1477
1478 static int
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001479luaV_command(lua_State *L)
1480{
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001481 do_cmdline_cmd((char_u *) luaL_checkstring(L, 1));
1482 update_screen(VALID);
1483 return 0;
1484}
1485
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001486 static int
1487luaV_eval(lua_State *L)
1488{
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001489 typval_T *tv = eval_expr((char_u *) luaL_checkstring(L, 1), NULL);
1490 if (tv == NULL) luaL_error(L, "invalid expression");
1491 luaV_pushtypval(L, tv);
Bram Moolenaarb3766472013-04-15 13:49:21 +02001492 free_tv(tv);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001493 return 1;
1494}
1495
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001496 static int
Bram Moolenaar0d2e4fc2010-07-18 12:35:47 +02001497luaV_beep(lua_State *L UNUSED)
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001498{
Bram Moolenaar165bc692015-07-21 17:53:25 +02001499 vim_beep(BO_LANG);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001500 return 0;
1501}
1502
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001503 static int
1504luaV_line(lua_State *L)
1505{
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001506 luaV_pushline(L, curbuf, curwin->w_cursor.lnum);
1507 return 1;
1508}
1509
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001510 static int
Bram Moolenaar1dced572012-04-05 16:54:08 +02001511luaV_list(lua_State *L)
1512{
Bram Moolenaarca06da92018-07-01 15:12:05 +02001513 list_T *l;
1514 int initarg = !lua_isnoneornil(L, 1);
1515
1516 if (initarg && lua_type(L, 1) != LUA_TTABLE)
1517 luaL_error(L, "table expected, got %s", luaL_typename(L, 1));
1518 l = list_alloc();
Bram Moolenaar1dced572012-04-05 16:54:08 +02001519 if (l == NULL)
1520 lua_pushnil(L);
1521 else
Bram Moolenaarca06da92018-07-01 15:12:05 +02001522 {
Bram Moolenaar1dced572012-04-05 16:54:08 +02001523 luaV_newlist(L, l);
Bram Moolenaarca06da92018-07-01 15:12:05 +02001524 if (initarg) { /* traverse table to init dict */
1525 int notnil, i = 0;
1526 typval_T v;
1527 do {
1528 lua_rawgeti(L, 1, ++i);
1529 notnil = !lua_isnil(L, -1);
1530 if (notnil) {
1531 luaV_checktypval(L, -1, &v, "vim.list");
1532 list_append_tv(l, &v);
1533 }
1534 lua_pop(L, 1); /* value */
1535 } while (notnil);
1536 }
1537 }
Bram Moolenaar1dced572012-04-05 16:54:08 +02001538 return 1;
1539}
1540
1541 static int
1542luaV_dict(lua_State *L)
1543{
Bram Moolenaarca06da92018-07-01 15:12:05 +02001544 dict_T *d;
1545 int initarg = !lua_isnoneornil(L, 1);
1546
1547 if (initarg && lua_type(L, 1) != LUA_TTABLE)
1548 luaL_error(L, "table expected, got %s", luaL_typename(L, 1));
1549 d = dict_alloc();
Bram Moolenaar1dced572012-04-05 16:54:08 +02001550 if (d == NULL)
1551 lua_pushnil(L);
1552 else
Bram Moolenaarca06da92018-07-01 15:12:05 +02001553 {
Bram Moolenaar1dced572012-04-05 16:54:08 +02001554 luaV_newdict(L, d);
Bram Moolenaarca06da92018-07-01 15:12:05 +02001555 if (initarg) /* traverse table to init dict */
1556 {
1557 lua_pushnil(L);
1558 while (lua_next(L, 1))
1559 {
1560 char_u *key;
1561 dictitem_T *di;
1562 typval_T v;
1563 lua_pushvalue(L, -2); /* dup key in case it's a number */
1564 key = (char_u *) lua_tostring(L, -1);
1565 if (key != NULL && *key == NUL)
1566 luaL_error(L, "table has empty key");
1567 luaV_checktypval(L, -2, &v, "vim.dict"); /* value */
1568 di = dictitem_alloc(key);
1569 if (di == NULL || dict_add(d, di) == FAIL) {
1570 vim_free(di);
1571 lua_pushnil(L);
1572 return 1;
1573 }
1574 copy_tv(&v, &di->di_tv);
1575 clear_tv(&v);
1576 lua_pop(L, 2); /* key copy and value */
1577 }
1578 }
1579 }
1580 return 1;
1581}
1582
1583 static int
1584luaV_funcref(lua_State *L)
1585{
1586 const char *name = luaL_checkstring(L, 1);
1587 /* note: not checking if function exists (needs function_exists) */
1588 if (name == NULL || *name == NUL || VIM_ISDIGIT(*name))
1589 luaL_error(L, "invalid function name: %s", name);
1590 luaV_newfuncref(L, (char_u *) name);
Bram Moolenaar1dced572012-04-05 16:54:08 +02001591 return 1;
1592}
1593
1594 static int
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001595luaV_buffer(lua_State *L)
1596{
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001597 buf_T *buf;
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001598 if (lua_isstring(L, 1)) /* get by number or name? */
1599 {
1600 if (lua_isnumber(L, 1)) /* by number? */
1601 {
1602 int n = lua_tointeger(L, 1);
Bram Moolenaar29323592016-07-24 22:04:11 +02001603 FOR_ALL_BUFFERS(buf)
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001604 if (buf->b_fnum == n) break;
1605 }
Bram Moolenaarca06da92018-07-01 15:12:05 +02001606 else // by name
1607 {
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001608 size_t l;
1609 const char *s = lua_tolstring(L, 1, &l);
Bram Moolenaar29323592016-07-24 22:04:11 +02001610 FOR_ALL_BUFFERS(buf)
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001611 {
1612 if (buf->b_ffname == NULL || buf->b_sfname == NULL)
1613 {
1614 if (l == 0) break;
1615 }
Bram Moolenaar0d2e4fc2010-07-18 12:35:47 +02001616 else if (strncmp(s, (char *)buf->b_ffname, l) == 0
1617 || strncmp(s, (char *)buf->b_sfname, l) == 0)
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001618 break;
1619 }
1620 }
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001621 }
Bram Moolenaar1dced572012-04-05 16:54:08 +02001622 else
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001623 buf = (lua_toboolean(L, 1)) ? firstbuf : curbuf; /* first buffer? */
Bram Moolenaar1dced572012-04-05 16:54:08 +02001624 luaV_pushbuffer(L, buf);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001625 return 1;
1626}
1627
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001628 static int
1629luaV_window(lua_State *L)
1630{
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001631 win_T *win;
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001632 if (lua_isnumber(L, 1)) /* get by number? */
1633 {
1634 int n = lua_tointeger(L, 1);
1635 for (win = firstwin; win != NULL; win = win->w_next, n--)
1636 if (n == 1) break;
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001637 }
Bram Moolenaar1dced572012-04-05 16:54:08 +02001638 else
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001639 win = (lua_toboolean(L, 1)) ? firstwin : curwin; /* first window? */
Bram Moolenaar1dced572012-04-05 16:54:08 +02001640 luaV_pushwindow(L, win);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001641 return 1;
1642}
1643
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001644 static int
1645luaV_open(lua_State *L)
1646{
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001647 char_u *s = NULL;
1648#ifdef HAVE_SANDBOX
1649 luaV_checksandbox(L);
1650#endif
1651 if (lua_isstring(L, 1)) s = (char_u *) lua_tostring(L, 1);
Bram Moolenaarfa263a52011-12-08 16:00:16 +01001652 luaV_pushbuffer(L, buflist_new(s, NULL, 1L, BLN_LISTED));
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001653 return 1;
1654}
1655
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001656 static int
Bram Moolenaar1dced572012-04-05 16:54:08 +02001657luaV_type(lua_State *L)
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001658{
Bram Moolenaar1dced572012-04-05 16:54:08 +02001659 luaL_checkany(L, 1);
1660 if (lua_type(L, 1) == LUA_TUSERDATA) /* check vim udata? */
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001661 {
Bram Moolenaar1dced572012-04-05 16:54:08 +02001662 lua_settop(L, 1);
1663 if (lua_getmetatable(L, 1))
1664 {
1665 luaV_getfield(L, LUAVIM_LIST);
1666 if (lua_rawequal(L, -1, 2))
1667 {
1668 lua_pushstring(L, "list");
1669 return 1;
1670 }
1671 luaV_getfield(L, LUAVIM_DICT);
1672 if (lua_rawequal(L, -1, 2))
1673 {
1674 lua_pushstring(L, "dict");
1675 return 1;
1676 }
Bram Moolenaarca06da92018-07-01 15:12:05 +02001677 luaV_getfield(L, LUAVIM_FUNCREF);
1678 if (lua_rawequal(L, -1, 2))
1679 {
1680 lua_pushstring(L, "funcref");
1681 return 1;
1682 }
Bram Moolenaar1dced572012-04-05 16:54:08 +02001683 luaV_getfield(L, LUAVIM_BUFFER);
1684 if (lua_rawequal(L, -1, 2))
1685 {
1686 lua_pushstring(L, "buffer");
1687 return 1;
1688 }
1689 luaV_getfield(L, LUAVIM_WINDOW);
1690 if (lua_rawequal(L, -1, 2))
1691 {
1692 lua_pushstring(L, "window");
1693 return 1;
1694 }
1695 }
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001696 }
Bram Moolenaar1dced572012-04-05 16:54:08 +02001697 lua_pushstring(L, luaL_typename(L, 1)); /* fallback */
1698 return 1;
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001699}
1700
1701static const luaL_Reg luaV_module[] = {
1702 {"command", luaV_command},
1703 {"eval", luaV_eval},
1704 {"beep", luaV_beep},
1705 {"line", luaV_line},
Bram Moolenaar1dced572012-04-05 16:54:08 +02001706 {"list", luaV_list},
1707 {"dict", luaV_dict},
Bram Moolenaarca06da92018-07-01 15:12:05 +02001708 {"funcref", luaV_funcref},
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001709 {"buffer", luaV_buffer},
1710 {"window", luaV_window},
1711 {"open", luaV_open},
Bram Moolenaar1dced572012-04-05 16:54:08 +02001712 {"type", luaV_type},
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001713 {NULL, NULL}
1714};
1715
Bram Moolenaar1dced572012-04-05 16:54:08 +02001716/* for freeing list, dict, buffer and window objects; lightuserdata as arg */
1717 static int
1718luaV_free(lua_State *L)
1719{
1720 lua_pushnil(L);
1721 luaV_setudata(L, lua_touserdata(L, 1));
1722 return 0;
1723}
1724
1725 static int
1726luaV_luaeval (lua_State *L)
1727{
1728 luaL_Buffer b;
1729 size_t l;
1730 const char *str = lua_tolstring(L, 1, &l);
1731 typval_T *arg = (typval_T *) lua_touserdata(L, 2);
1732 typval_T *rettv = (typval_T *) lua_touserdata(L, 3);
1733 luaL_buffinit(L, &b);
1734 luaL_addlstring(&b, LUAVIM_EVALHEADER, sizeof(LUAVIM_EVALHEADER) - 1);
1735 luaL_addlstring(&b, str, l);
1736 luaL_pushresult(&b);
1737 str = lua_tolstring(L, -1, &l);
1738 if (luaL_loadbuffer(L, str, l, LUAVIM_EVALNAME)) /* compile error? */
1739 {
1740 luaV_emsg(L);
1741 return 0;
1742 }
1743 luaV_pushtypval(L, arg);
1744 if (lua_pcall(L, 1, 1, 0)) /* running error? */
1745 {
1746 luaV_emsg(L);
1747 return 0;
1748 }
Bram Moolenaarca06da92018-07-01 15:12:05 +02001749 if (luaV_totypval(L, -1, rettv) == FAIL)
1750 EMSG("luaeval: cannot convert value");
Bram Moolenaarf554a322015-02-04 23:08:01 +01001751 return 0;
Bram Moolenaar1dced572012-04-05 16:54:08 +02001752}
1753
1754 static int
1755luaV_setref (lua_State *L)
1756{
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01001757 int copyID = lua_tointeger(L, 1);
1758 int abort = FALSE;
1759 typval_T tv;
1760
Bram Moolenaar1dced572012-04-05 16:54:08 +02001761 luaV_getfield(L, LUAVIM_LIST);
1762 luaV_getfield(L, LUAVIM_DICT);
1763 lua_pushnil(L);
Bram Moolenaarb84634d2015-02-04 22:02:37 +01001764 /* traverse cache table */
1765 while (!abort && lua_next(L, lua_upvalueindex(1)) != 0)
Bram Moolenaar1dced572012-04-05 16:54:08 +02001766 {
1767 lua_getmetatable(L, -1);
1768 if (lua_rawequal(L, -1, 2)) /* list? */
1769 {
1770 tv.v_type = VAR_LIST;
1771 tv.vval.v_list = (list_T *) lua_touserdata(L, 4); /* key */
Bram Moolenaarf609dcf2015-12-03 17:43:17 +01001772 abort = set_ref_in_item(&tv, copyID, NULL, NULL);
Bram Moolenaar1dced572012-04-05 16:54:08 +02001773 }
1774 else if (lua_rawequal(L, -1, 3)) /* dict? */
1775 {
1776 tv.v_type = VAR_DICT;
1777 tv.vval.v_dict = (dict_T *) lua_touserdata(L, 4); /* key */
Bram Moolenaarf609dcf2015-12-03 17:43:17 +01001778 abort = set_ref_in_item(&tv, copyID, NULL, NULL);
Bram Moolenaar1dced572012-04-05 16:54:08 +02001779 }
1780 lua_pop(L, 2); /* metatable and value */
Bram Moolenaar1dced572012-04-05 16:54:08 +02001781 }
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01001782 lua_pushinteger(L, abort);
Bram Moolenaarf554a322015-02-04 23:08:01 +01001783 return 1;
Bram Moolenaar1dced572012-04-05 16:54:08 +02001784}
1785
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001786 static int
1787luaopen_vim(lua_State *L)
1788{
Bram Moolenaar1dced572012-04-05 16:54:08 +02001789 /* set cache table */
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001790 lua_newtable(L);
1791 lua_newtable(L);
Bram Moolenaar1dced572012-04-05 16:54:08 +02001792 lua_pushstring(L, "v");
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001793 lua_setfield(L, -2, "__mode");
Bram Moolenaar1dced572012-04-05 16:54:08 +02001794 lua_setmetatable(L, -2); /* cache is weak-valued */
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001795 /* print */
1796 lua_pushcfunction(L, luaV_print);
1797 lua_setglobal(L, "print");
Bram Moolenaar38e2b062011-09-21 17:15:39 +02001798 /* debug.debug */
1799 lua_getglobal(L, "debug");
1800 lua_pushcfunction(L, luaV_debug);
1801 lua_setfield(L, -2, "debug");
1802 lua_pop(L, 1);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001803 /* free */
1804 lua_pushlightuserdata(L, (void *) LUAVIM_FREE);
Bram Moolenaar1dced572012-04-05 16:54:08 +02001805 lua_pushvalue(L, 1); /* cache table */
1806 lua_pushcclosure(L, luaV_free, 1);
1807 lua_rawset(L, LUA_REGISTRYINDEX);
1808 /* luaeval */
1809 lua_pushlightuserdata(L, (void *) LUAVIM_LUAEVAL);
1810 lua_pushvalue(L, 1); /* cache table */
1811 lua_pushcclosure(L, luaV_luaeval, 1);
1812 lua_rawset(L, LUA_REGISTRYINDEX);
1813 /* setref */
1814 lua_pushlightuserdata(L, (void *) LUAVIM_SETREF);
1815 lua_pushvalue(L, 1); /* cache table */
1816 lua_pushcclosure(L, luaV_setref, 1);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001817 lua_rawset(L, LUA_REGISTRYINDEX);
1818 /* register */
Bram Moolenaar1dced572012-04-05 16:54:08 +02001819 luaV_newmetatable(L, LUAVIM_LIST);
1820 lua_pushvalue(L, 1);
1821 luaV_openlib(L, luaV_List_mt, 1);
1822 luaV_newmetatable(L, LUAVIM_DICT);
1823 lua_pushvalue(L, 1);
1824 luaV_openlib(L, luaV_Dict_mt, 1);
Bram Moolenaarca06da92018-07-01 15:12:05 +02001825 luaV_newmetatable(L, LUAVIM_FUNCREF);
1826 lua_pushvalue(L, 1);
1827 luaV_openlib(L, luaV_Funcref_mt, 1);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001828 luaV_newmetatable(L, LUAVIM_BUFFER);
Bram Moolenaar1dced572012-04-05 16:54:08 +02001829 lua_pushvalue(L, 1); /* cache table */
1830 luaV_openlib(L, luaV_Buffer_mt, 1);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001831 luaV_newmetatable(L, LUAVIM_WINDOW);
Bram Moolenaar1dced572012-04-05 16:54:08 +02001832 lua_pushvalue(L, 1); /* cache table */
1833 luaV_openlib(L, luaV_Window_mt, 1);
1834 lua_newtable(L); /* vim table */
1835 lua_pushvalue(L, 1); /* cache table */
1836 luaV_openlib(L, luaV_module, 1);
1837 lua_setglobal(L, LUAVIM_NAME);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001838 return 0;
1839}
1840
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001841 static lua_State *
1842luaV_newstate(void)
1843{
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001844 lua_State *L = luaL_newstate();
Bram Moolenaar16c98f92010-07-28 22:46:08 +02001845 luaL_openlibs(L); /* core libs */
1846 lua_pushcfunction(L, luaopen_vim); /* vim */
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001847 lua_call(L, 0, 0);
1848 return L;
1849}
1850
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001851 static void
1852luaV_setrange(lua_State *L, int line1, int line2)
1853{
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001854 lua_getglobal(L, LUAVIM_NAME);
1855 lua_pushinteger(L, line1);
1856 lua_setfield(L, -2, "firstline");
1857 lua_pushinteger(L, line2);
1858 lua_setfield(L, -2, "lastline");
1859 lua_pop(L, 1); /* vim table */
1860}
1861
1862
1863/* ======= Interface ======= */
1864
1865static lua_State *L = NULL;
1866
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001867 static int
Bram Moolenaar1dced572012-04-05 16:54:08 +02001868lua_isopen(void)
Bram Moolenaar2bd6a1b2010-08-12 22:14:01 +02001869{
1870 return L != NULL;
1871}
1872
1873 static int
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001874lua_init(void)
1875{
Bram Moolenaar1dced572012-04-05 16:54:08 +02001876 if (!lua_isopen())
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001877 {
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001878#ifdef DYNAMIC_LUA
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001879 if (!lua_enabled(TRUE))
1880 {
1881 EMSG(_("Lua library cannot be loaded."));
1882 return FAIL;
1883 }
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001884#endif
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001885 L = luaV_newstate();
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001886 }
1887 return OK;
1888}
1889
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001890 void
1891lua_end(void)
1892{
Bram Moolenaar1dced572012-04-05 16:54:08 +02001893 if (lua_isopen())
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001894 {
1895 lua_close(L);
1896 L = NULL;
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001897#ifdef DYNAMIC_LUA
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001898 end_dynamic_lua();
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001899#endif
1900 }
1901}
1902
1903/* ex commands */
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001904 void
1905ex_lua(exarg_T *eap)
1906{
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001907 char *script;
1908 if (lua_init() == FAIL) return;
1909 script = (char *) script_get(eap, eap->arg);
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001910 if (!eap->skip)
1911 {
1912 char *s = (script) ? script : (char *) eap->arg;
1913 luaV_setrange(L, eap->line1, eap->line2);
1914 if (luaL_loadbuffer(L, s, strlen(s), LUAVIM_CHUNKNAME)
1915 || lua_pcall(L, 0, 0, 0))
1916 luaV_emsg(L);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001917 }
1918 if (script != NULL) vim_free(script);
1919}
1920
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001921 void
1922ex_luado(exarg_T *eap)
1923{
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001924 linenr_T l;
1925 const char *s = (const char *) eap->arg;
1926 luaL_Buffer b;
1927 size_t len;
Bram Moolenaard58f03b2017-01-29 22:48:45 +01001928 buf_T *was_curbuf = curbuf;
1929
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001930 if (lua_init() == FAIL) return;
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001931 if (u_save(eap->line1 - 1, eap->line2 + 1) == FAIL)
1932 {
1933 EMSG(_("cannot save undo information"));
1934 return;
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001935 }
1936 luaV_setrange(L, eap->line1, eap->line2);
1937 luaL_buffinit(L, &b);
Bram Moolenaarbd2f3c32012-04-06 14:31:00 +02001938 luaL_addlstring(&b, "return function(line, linenr) ", 30); /* header */
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001939 luaL_addlstring(&b, s, strlen(s));
1940 luaL_addlstring(&b, " end", 4); /* footer */
1941 luaL_pushresult(&b);
1942 s = lua_tolstring(L, -1, &len);
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001943 if (luaL_loadbuffer(L, s, len, LUAVIM_CHUNKNAME))
1944 {
1945 luaV_emsg(L);
1946 lua_pop(L, 1); /* function body */
1947 return;
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001948 }
1949 lua_call(L, 0, 1);
1950 lua_replace(L, -2); /* function -> body */
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001951 for (l = eap->line1; l <= eap->line2; l++)
1952 {
Bram Moolenaard58f03b2017-01-29 22:48:45 +01001953 /* Check the line number, the command my have deleted lines. */
1954 if (l > curbuf->b_ml.ml_line_count)
1955 break;
1956
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001957 lua_pushvalue(L, -1); /* function */
1958 luaV_pushline(L, curbuf, l); /* current line as arg */
Bram Moolenaarbd2f3c32012-04-06 14:31:00 +02001959 lua_pushinteger(L, l); /* current line number as arg */
1960 if (lua_pcall(L, 2, 1, 0))
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001961 {
1962 luaV_emsg(L);
1963 break;
1964 }
Bram Moolenaard58f03b2017-01-29 22:48:45 +01001965 /* Catch the command switching to another buffer. */
1966 if (curbuf != was_curbuf)
1967 break;
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001968 if (lua_isstring(L, -1)) /* update line? */
1969 {
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001970#ifdef HAVE_SANDBOX
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001971 luaV_checksandbox(L);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001972#endif
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001973 ml_replace(l, luaV_toline(L, -1), TRUE);
1974 changed_bytes(l, 0);
1975 lua_pop(L, 1); /* result from luaV_toline */
1976 }
1977 lua_pop(L, 1); /* line */
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001978 }
1979 lua_pop(L, 1); /* function */
1980 check_cursor();
1981 update_screen(NOT_VALID);
1982}
1983
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001984 void
1985ex_luafile(exarg_T *eap)
1986{
1987 if (lua_init() == FAIL)
1988 return;
1989 if (!eap->skip)
1990 {
1991 luaV_setrange(L, eap->line1, eap->line2);
1992 if (luaL_loadfile(L, (char *) eap->arg) || lua_pcall(L, 0, 0, 0))
1993 luaV_emsg(L);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001994 }
1995}
1996
Bram Moolenaar1dced572012-04-05 16:54:08 +02001997#define luaV_freetype(typ,tname) \
1998 void \
1999 lua_##tname##_free(typ *o) \
2000 { \
2001 if (!lua_isopen()) return; \
2002 luaV_getfield(L, LUAVIM_FREE); \
2003 lua_pushlightuserdata(L, (void *) o); \
2004 lua_call(L, 1, 0); \
2005 }
2006
2007luaV_freetype(buf_T, buffer)
2008luaV_freetype(win_T, window)
2009
Bram Moolenaar55d5c032010-07-17 23:52:29 +02002010 void
Bram Moolenaar1dced572012-04-05 16:54:08 +02002011do_luaeval (char_u *str, typval_T *arg, typval_T *rettv)
Bram Moolenaar55d5c032010-07-17 23:52:29 +02002012{
Bram Moolenaar1dced572012-04-05 16:54:08 +02002013 lua_init();
2014 luaV_getfield(L, LUAVIM_LUAEVAL);
2015 lua_pushstring(L, (char *) str);
2016 lua_pushlightuserdata(L, (void *) arg);
2017 lua_pushlightuserdata(L, (void *) rettv);
2018 lua_call(L, 3, 0);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02002019}
2020
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01002021 int
Bram Moolenaar1dced572012-04-05 16:54:08 +02002022set_ref_in_lua (int copyID)
Bram Moolenaar55d5c032010-07-17 23:52:29 +02002023{
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01002024 int aborted = 0;
2025
2026 if (lua_isopen())
2027 {
2028 luaV_getfield(L, LUAVIM_SETREF);
2029 /* call the function with 1 arg, getting 1 result back */
2030 lua_pushinteger(L, copyID);
2031 lua_call(L, 1, 1);
2032 /* get the result */
2033 aborted = lua_tointeger(L, -1);
2034 /* pop result off the stack */
2035 lua_pop(L, 1);
2036 }
2037 return aborted;
Bram Moolenaar0ba04292010-07-14 23:23:17 +02002038}
2039
2040#endif