blob: df0ef345459ca1fdf8f05b0fb7d70403d436bb3c [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 Moolenaar1dced572012-04-05 16:54:08 +02001126 lua_pushstring(L, (char *) b->b_sfname);
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001127 else if (strncmp(s, "fname", 5) == 0)
Bram Moolenaar1dced572012-04-05 16:54:08 +02001128 lua_pushstring(L, (char *) b->b_ffname);
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001129 else if (strncmp(s, "number", 6) == 0)
Bram Moolenaar1dced572012-04-05 16:54:08 +02001130 lua_pushinteger(L, b->b_fnum);
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001131 /* methods */
1132 else if (strncmp(s, "insert", 6) == 0
1133 || strncmp(s, "next", 4) == 0
1134 || strncmp(s, "previous", 8) == 0
1135 || strncmp(s, "isvalid", 7) == 0)
1136 {
1137 lua_getmetatable(L, 1);
1138 lua_getfield(L, -1, s);
1139 }
1140 else
1141 lua_pushnil(L);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001142 }
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001143 else
1144 lua_pushnil(L);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001145 return 1;
1146}
1147
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001148 static int
1149luaV_buffer_newindex(lua_State *L)
1150{
Bram Moolenaar1dced572012-04-05 16:54:08 +02001151 buf_T *b = (buf_T *) luaV_checkvalid(L, luaV_Buffer, 1);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001152 linenr_T n = (linenr_T) luaL_checkinteger(L, 2);
1153#ifdef HAVE_SANDBOX
1154 luaV_checksandbox(L);
1155#endif
Bram Moolenaar1dced572012-04-05 16:54:08 +02001156 if (n < 1 || n > b->b_ml.ml_line_count)
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001157 luaL_error(L, "invalid line number");
1158 if (lua_isnil(L, 3)) /* delete line */
1159 {
1160 buf_T *buf = curbuf;
Bram Moolenaar1dced572012-04-05 16:54:08 +02001161 curbuf = b;
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001162 if (u_savedel(n, 1L) == FAIL)
1163 {
1164 curbuf = buf;
1165 luaL_error(L, "cannot save undo information");
1166 }
1167 else if (ml_delete(n, FALSE) == FAIL)
1168 {
1169 curbuf = buf;
1170 luaL_error(L, "cannot delete line");
1171 }
Bram Moolenaarca06da92018-07-01 15:12:05 +02001172 else
1173 {
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001174 deleted_lines_mark(n, 1L);
Bram Moolenaar1dced572012-04-05 16:54:08 +02001175 if (b == curwin->w_buffer) /* fix cursor in current window? */
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001176 {
1177 if (curwin->w_cursor.lnum >= n)
1178 {
1179 if (curwin->w_cursor.lnum > n)
1180 {
1181 curwin->w_cursor.lnum -= 1;
1182 check_cursor_col();
1183 }
1184 else check_cursor();
1185 changed_cline_bef_curs();
1186 }
1187 invalidate_botline();
1188 }
1189 }
1190 curbuf = buf;
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001191 }
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001192 else if (lua_isstring(L, 3)) /* update line */
1193 {
1194 buf_T *buf = curbuf;
Bram Moolenaar1dced572012-04-05 16:54:08 +02001195 curbuf = b;
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001196 if (u_savesub(n) == FAIL)
1197 {
1198 curbuf = buf;
1199 luaL_error(L, "cannot save undo information");
1200 }
1201 else if (ml_replace(n, luaV_toline(L, 3), TRUE) == FAIL)
1202 {
1203 curbuf = buf;
1204 luaL_error(L, "cannot replace line");
1205 }
1206 else changed_bytes(n, 0);
1207 curbuf = buf;
Bram Moolenaar1dced572012-04-05 16:54:08 +02001208 if (b == curwin->w_buffer)
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001209 check_cursor_col();
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001210 }
1211 else
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001212 luaL_error(L, "wrong argument to change line");
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001213 return 0;
1214}
1215
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001216 static int
1217luaV_buffer_insert(lua_State *L)
1218{
Bram Moolenaar1dced572012-04-05 16:54:08 +02001219 luaV_Buffer *lb = luaV_checkudata(L, 1, LUAVIM_BUFFER);
1220 buf_T *b = (buf_T *) luaV_checkcache(L, (void *) *lb);
1221 linenr_T last = b->b_ml.ml_line_count;
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001222 linenr_T n = (linenr_T) luaL_optinteger(L, 3, last);
1223 buf_T *buf;
1224 luaL_checktype(L, 2, LUA_TSTRING);
1225#ifdef HAVE_SANDBOX
1226 luaV_checksandbox(L);
1227#endif
1228 /* fix insertion line */
1229 if (n < 0) n = 0;
1230 if (n > last) n = last;
1231 /* insert */
1232 buf = curbuf;
Bram Moolenaar1dced572012-04-05 16:54:08 +02001233 curbuf = b;
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001234 if (u_save(n, n + 1) == FAIL)
1235 {
1236 curbuf = buf;
1237 luaL_error(L, "cannot save undo information");
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001238 }
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001239 else if (ml_append(n, luaV_toline(L, 2), 0, FALSE) == FAIL)
1240 {
1241 curbuf = buf;
1242 luaL_error(L, "cannot insert line");
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001243 }
1244 else
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001245 appended_lines_mark(n, 1L);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001246 curbuf = buf;
1247 update_screen(VALID);
1248 return 0;
1249}
1250
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001251 static int
1252luaV_buffer_next(lua_State *L)
1253{
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001254 luaV_Buffer *b = luaV_checkudata(L, 1, LUAVIM_BUFFER);
Bram Moolenaar1dced572012-04-05 16:54:08 +02001255 buf_T *buf = (buf_T *) luaV_checkcache(L, (void *) *b);
1256 luaV_pushbuffer(L, buf->b_next);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001257 return 1;
1258}
1259
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001260 static int
1261luaV_buffer_previous(lua_State *L)
1262{
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001263 luaV_Buffer *b = luaV_checkudata(L, 1, LUAVIM_BUFFER);
Bram Moolenaar1dced572012-04-05 16:54:08 +02001264 buf_T *buf = (buf_T *) luaV_checkcache(L, (void *) *b);
1265 luaV_pushbuffer(L, buf->b_prev);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001266 return 1;
1267}
1268
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001269 static int
1270luaV_buffer_isvalid(lua_State *L)
1271{
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001272 luaV_Buffer *b = luaV_checkudata(L, 1, LUAVIM_BUFFER);
Bram Moolenaar1dced572012-04-05 16:54:08 +02001273 luaV_getudata(L, *b);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001274 lua_pushboolean(L, !lua_isnil(L, -1));
1275 return 1;
1276}
1277
1278static const luaL_Reg luaV_Buffer_mt[] = {
1279 {"__tostring", luaV_buffer_tostring},
1280 {"__len", luaV_buffer_len},
1281 {"__call", luaV_buffer_call},
1282 {"__index", luaV_buffer_index},
1283 {"__newindex", luaV_buffer_newindex},
1284 {"insert", luaV_buffer_insert},
1285 {"next", luaV_buffer_next},
1286 {"previous", luaV_buffer_previous},
1287 {"isvalid", luaV_buffer_isvalid},
1288 {NULL, NULL}
1289};
1290
1291
1292/* ======= Window type ======= */
1293
Bram Moolenaar1dced572012-04-05 16:54:08 +02001294luaV_newtype(win_T, window, luaV_Window, LUAVIM_WINDOW)
1295luaV_pushtype(win_T, window, luaV_Window)
1296luaV_type_tostring(window, LUAVIM_WINDOW)
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001297
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001298 static int
1299luaV_window_call(lua_State *L)
1300{
Bram Moolenaar1dced572012-04-05 16:54:08 +02001301 win_T *w = (win_T *) luaV_checkvalid(L, luaV_Window, 1);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001302 lua_settop(L, 1);
Bram Moolenaar1dced572012-04-05 16:54:08 +02001303 win_goto(w);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001304 return 1;
1305}
1306
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001307 static int
1308luaV_window_index(lua_State *L)
1309{
Bram Moolenaar1dced572012-04-05 16:54:08 +02001310 win_T *w = (win_T *) luaV_checkvalid(L, luaV_Window, 1);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001311 const char *s = luaL_checkstring(L, 2);
1312 if (strncmp(s, "buffer", 6) == 0)
Bram Moolenaar1dced572012-04-05 16:54:08 +02001313 luaV_pushbuffer(L, w->w_buffer);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001314 else if (strncmp(s, "line", 4) == 0)
Bram Moolenaar1dced572012-04-05 16:54:08 +02001315 lua_pushinteger(L, w->w_cursor.lnum);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001316 else if (strncmp(s, "col", 3) == 0)
Bram Moolenaar1dced572012-04-05 16:54:08 +02001317 lua_pushinteger(L, w->w_cursor.col + 1);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001318 else if (strncmp(s, "width", 5) == 0)
Bram Moolenaar02631462017-09-22 15:20:32 +02001319 lua_pushinteger(L, w->w_width);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001320 else if (strncmp(s, "height", 6) == 0)
Bram Moolenaar1dced572012-04-05 16:54:08 +02001321 lua_pushinteger(L, w->w_height);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001322 /* methods */
1323 else if (strncmp(s, "next", 4) == 0
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001324 || strncmp(s, "previous", 8) == 0
1325 || strncmp(s, "isvalid", 7) == 0)
1326 {
1327 lua_getmetatable(L, 1);
1328 lua_getfield(L, -1, s);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001329 }
1330 else
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001331 lua_pushnil(L);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001332 return 1;
1333}
1334
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001335 static int
1336luaV_window_newindex (lua_State *L)
1337{
Bram Moolenaar1dced572012-04-05 16:54:08 +02001338 win_T *w = (win_T *) luaV_checkvalid(L, luaV_Window, 1);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001339 const char *s = luaL_checkstring(L, 2);
1340 int v = luaL_checkinteger(L, 3);
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001341 if (strncmp(s, "line", 4) == 0)
1342 {
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001343#ifdef HAVE_SANDBOX
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001344 luaV_checksandbox(L);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001345#endif
Bram Moolenaar1dced572012-04-05 16:54:08 +02001346 if (v < 1 || v > w->w_buffer->b_ml.ml_line_count)
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001347 luaL_error(L, "line out of range");
Bram Moolenaar1dced572012-04-05 16:54:08 +02001348 w->w_cursor.lnum = v;
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001349 update_screen(VALID);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001350 }
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001351 else if (strncmp(s, "col", 3) == 0)
1352 {
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001353#ifdef HAVE_SANDBOX
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001354 luaV_checksandbox(L);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001355#endif
Bram Moolenaar1dced572012-04-05 16:54:08 +02001356 w->w_cursor.col = v - 1;
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001357 update_screen(VALID);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001358 }
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001359 else if (strncmp(s, "width", 5) == 0)
1360 {
1361 win_T *win = curwin;
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001362#ifdef FEAT_GUI
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001363 need_mouse_correct = TRUE;
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001364#endif
Bram Moolenaar1dced572012-04-05 16:54:08 +02001365 curwin = w;
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001366 win_setwidth(v);
1367 curwin = win;
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001368 }
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001369 else if (strncmp(s, "height", 6) == 0)
1370 {
1371 win_T *win = curwin;
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001372#ifdef FEAT_GUI
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001373 need_mouse_correct = TRUE;
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001374#endif
Bram Moolenaar1dced572012-04-05 16:54:08 +02001375 curwin = w;
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001376 win_setheight(v);
1377 curwin = win;
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001378 }
1379 else
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001380 luaL_error(L, "invalid window property: `%s'", s);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001381 return 0;
1382}
1383
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001384 static int
1385luaV_window_next(lua_State *L)
1386{
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001387 luaV_Window *w = luaV_checkudata(L, 1, LUAVIM_WINDOW);
Bram Moolenaar1dced572012-04-05 16:54:08 +02001388 win_T *win = (win_T *) luaV_checkcache(L, (void *) *w);
1389 luaV_pushwindow(L, win->w_next);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001390 return 1;
1391}
1392
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001393 static int
1394luaV_window_previous(lua_State *L)
1395{
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001396 luaV_Window *w = luaV_checkudata(L, 1, LUAVIM_WINDOW);
Bram Moolenaar1dced572012-04-05 16:54:08 +02001397 win_T *win = (win_T *) luaV_checkcache(L, (void *) *w);
1398 luaV_pushwindow(L, win->w_prev);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001399 return 1;
1400}
1401
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001402 static int
1403luaV_window_isvalid(lua_State *L)
1404{
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001405 luaV_Window *w = luaV_checkudata(L, 1, LUAVIM_WINDOW);
Bram Moolenaar1dced572012-04-05 16:54:08 +02001406 luaV_getudata(L, *w);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001407 lua_pushboolean(L, !lua_isnil(L, -1));
1408 return 1;
1409}
1410
1411static const luaL_Reg luaV_Window_mt[] = {
1412 {"__tostring", luaV_window_tostring},
1413 {"__call", luaV_window_call},
1414 {"__index", luaV_window_index},
1415 {"__newindex", luaV_window_newindex},
1416 {"next", luaV_window_next},
1417 {"previous", luaV_window_previous},
1418 {"isvalid", luaV_window_isvalid},
1419 {NULL, NULL}
1420};
1421
1422
1423/* ======= Vim module ======= */
1424
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001425 static int
1426luaV_print(lua_State *L)
1427{
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001428 int i, n = lua_gettop(L); /* nargs */
1429 const char *s;
1430 size_t l;
1431 luaL_Buffer b;
1432 luaL_buffinit(L, &b);
1433 lua_getglobal(L, "tostring");
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001434 for (i = 1; i <= n; i++)
1435 {
1436 lua_pushvalue(L, -1); /* tostring */
1437 lua_pushvalue(L, i); /* arg */
1438 lua_call(L, 1, 1);
1439 s = lua_tolstring(L, -1, &l);
1440 if (s == NULL)
1441 return luaL_error(L, "cannot convert to string");
1442 if (i > 1) luaL_addchar(&b, ' '); /* use space instead of tab */
1443 luaV_addlstring(&b, s, l, 0);
1444 lua_pop(L, 1);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001445 }
1446 luaL_pushresult(&b);
1447 luaV_msg(L);
1448 return 0;
1449}
1450
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001451 static int
Bram Moolenaar38e2b062011-09-21 17:15:39 +02001452luaV_debug(lua_State *L)
1453{
1454 lua_settop(L, 0);
1455 lua_getglobal(L, "vim");
1456 lua_getfield(L, -1, "eval");
1457 lua_remove(L, -2); /* vim.eval at position 1 */
1458 for (;;)
1459 {
1460 const char *input;
1461 size_t l;
1462 lua_pushvalue(L, 1); /* vim.eval */
1463 lua_pushliteral(L, "input('lua_debug> ')");
1464 lua_call(L, 1, 1); /* return string */
1465 input = lua_tolstring(L, -1, &l);
1466 if (l == 0 || strcmp(input, "cont") == 0)
1467 return 0;
1468 msg_putchar('\n'); /* avoid outputting on input line */
1469 if (luaL_loadbuffer(L, input, l, "=(debug command)")
1470 || lua_pcall(L, 0, 0, 0))
1471 luaV_emsg(L);
1472 lua_settop(L, 1); /* remove eventual returns, but keep vim.eval */
1473 }
1474}
1475
1476 static int
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001477luaV_command(lua_State *L)
1478{
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001479 do_cmdline_cmd((char_u *) luaL_checkstring(L, 1));
1480 update_screen(VALID);
1481 return 0;
1482}
1483
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001484 static int
1485luaV_eval(lua_State *L)
1486{
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001487 typval_T *tv = eval_expr((char_u *) luaL_checkstring(L, 1), NULL);
1488 if (tv == NULL) luaL_error(L, "invalid expression");
1489 luaV_pushtypval(L, tv);
Bram Moolenaarb3766472013-04-15 13:49:21 +02001490 free_tv(tv);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001491 return 1;
1492}
1493
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001494 static int
Bram Moolenaar0d2e4fc2010-07-18 12:35:47 +02001495luaV_beep(lua_State *L UNUSED)
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001496{
Bram Moolenaar165bc692015-07-21 17:53:25 +02001497 vim_beep(BO_LANG);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001498 return 0;
1499}
1500
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001501 static int
1502luaV_line(lua_State *L)
1503{
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001504 luaV_pushline(L, curbuf, curwin->w_cursor.lnum);
1505 return 1;
1506}
1507
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001508 static int
Bram Moolenaar1dced572012-04-05 16:54:08 +02001509luaV_list(lua_State *L)
1510{
Bram Moolenaarca06da92018-07-01 15:12:05 +02001511 list_T *l;
1512 int initarg = !lua_isnoneornil(L, 1);
1513
1514 if (initarg && lua_type(L, 1) != LUA_TTABLE)
1515 luaL_error(L, "table expected, got %s", luaL_typename(L, 1));
1516 l = list_alloc();
Bram Moolenaar1dced572012-04-05 16:54:08 +02001517 if (l == NULL)
1518 lua_pushnil(L);
1519 else
Bram Moolenaarca06da92018-07-01 15:12:05 +02001520 {
Bram Moolenaar1dced572012-04-05 16:54:08 +02001521 luaV_newlist(L, l);
Bram Moolenaarca06da92018-07-01 15:12:05 +02001522 if (initarg) { /* traverse table to init dict */
1523 int notnil, i = 0;
1524 typval_T v;
1525 do {
1526 lua_rawgeti(L, 1, ++i);
1527 notnil = !lua_isnil(L, -1);
1528 if (notnil) {
1529 luaV_checktypval(L, -1, &v, "vim.list");
1530 list_append_tv(l, &v);
1531 }
1532 lua_pop(L, 1); /* value */
1533 } while (notnil);
1534 }
1535 }
Bram Moolenaar1dced572012-04-05 16:54:08 +02001536 return 1;
1537}
1538
1539 static int
1540luaV_dict(lua_State *L)
1541{
Bram Moolenaarca06da92018-07-01 15:12:05 +02001542 dict_T *d;
1543 int initarg = !lua_isnoneornil(L, 1);
1544
1545 if (initarg && lua_type(L, 1) != LUA_TTABLE)
1546 luaL_error(L, "table expected, got %s", luaL_typename(L, 1));
1547 d = dict_alloc();
Bram Moolenaar1dced572012-04-05 16:54:08 +02001548 if (d == NULL)
1549 lua_pushnil(L);
1550 else
Bram Moolenaarca06da92018-07-01 15:12:05 +02001551 {
Bram Moolenaar1dced572012-04-05 16:54:08 +02001552 luaV_newdict(L, d);
Bram Moolenaarca06da92018-07-01 15:12:05 +02001553 if (initarg) /* traverse table to init dict */
1554 {
1555 lua_pushnil(L);
1556 while (lua_next(L, 1))
1557 {
1558 char_u *key;
1559 dictitem_T *di;
1560 typval_T v;
1561 lua_pushvalue(L, -2); /* dup key in case it's a number */
1562 key = (char_u *) lua_tostring(L, -1);
1563 if (key != NULL && *key == NUL)
1564 luaL_error(L, "table has empty key");
1565 luaV_checktypval(L, -2, &v, "vim.dict"); /* value */
1566 di = dictitem_alloc(key);
1567 if (di == NULL || dict_add(d, di) == FAIL) {
1568 vim_free(di);
1569 lua_pushnil(L);
1570 return 1;
1571 }
1572 copy_tv(&v, &di->di_tv);
1573 clear_tv(&v);
1574 lua_pop(L, 2); /* key copy and value */
1575 }
1576 }
1577 }
1578 return 1;
1579}
1580
1581 static int
1582luaV_funcref(lua_State *L)
1583{
1584 const char *name = luaL_checkstring(L, 1);
1585 /* note: not checking if function exists (needs function_exists) */
1586 if (name == NULL || *name == NUL || VIM_ISDIGIT(*name))
1587 luaL_error(L, "invalid function name: %s", name);
1588 luaV_newfuncref(L, (char_u *) name);
Bram Moolenaar1dced572012-04-05 16:54:08 +02001589 return 1;
1590}
1591
1592 static int
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001593luaV_buffer(lua_State *L)
1594{
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001595 buf_T *buf;
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001596 if (lua_isstring(L, 1)) /* get by number or name? */
1597 {
1598 if (lua_isnumber(L, 1)) /* by number? */
1599 {
1600 int n = lua_tointeger(L, 1);
Bram Moolenaar29323592016-07-24 22:04:11 +02001601 FOR_ALL_BUFFERS(buf)
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001602 if (buf->b_fnum == n) break;
1603 }
Bram Moolenaarca06da92018-07-01 15:12:05 +02001604 else // by name
1605 {
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001606 size_t l;
1607 const char *s = lua_tolstring(L, 1, &l);
Bram Moolenaar29323592016-07-24 22:04:11 +02001608 FOR_ALL_BUFFERS(buf)
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001609 {
1610 if (buf->b_ffname == NULL || buf->b_sfname == NULL)
1611 {
1612 if (l == 0) break;
1613 }
Bram Moolenaar0d2e4fc2010-07-18 12:35:47 +02001614 else if (strncmp(s, (char *)buf->b_ffname, l) == 0
1615 || strncmp(s, (char *)buf->b_sfname, l) == 0)
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001616 break;
1617 }
1618 }
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001619 }
Bram Moolenaar1dced572012-04-05 16:54:08 +02001620 else
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001621 buf = (lua_toboolean(L, 1)) ? firstbuf : curbuf; /* first buffer? */
Bram Moolenaar1dced572012-04-05 16:54:08 +02001622 luaV_pushbuffer(L, buf);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001623 return 1;
1624}
1625
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001626 static int
1627luaV_window(lua_State *L)
1628{
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001629 win_T *win;
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001630 if (lua_isnumber(L, 1)) /* get by number? */
1631 {
1632 int n = lua_tointeger(L, 1);
1633 for (win = firstwin; win != NULL; win = win->w_next, n--)
1634 if (n == 1) break;
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001635 }
Bram Moolenaar1dced572012-04-05 16:54:08 +02001636 else
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001637 win = (lua_toboolean(L, 1)) ? firstwin : curwin; /* first window? */
Bram Moolenaar1dced572012-04-05 16:54:08 +02001638 luaV_pushwindow(L, win);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001639 return 1;
1640}
1641
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001642 static int
1643luaV_open(lua_State *L)
1644{
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001645 char_u *s = NULL;
1646#ifdef HAVE_SANDBOX
1647 luaV_checksandbox(L);
1648#endif
1649 if (lua_isstring(L, 1)) s = (char_u *) lua_tostring(L, 1);
Bram Moolenaarfa263a52011-12-08 16:00:16 +01001650 luaV_pushbuffer(L, buflist_new(s, NULL, 1L, BLN_LISTED));
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001651 return 1;
1652}
1653
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001654 static int
Bram Moolenaar1dced572012-04-05 16:54:08 +02001655luaV_type(lua_State *L)
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001656{
Bram Moolenaar1dced572012-04-05 16:54:08 +02001657 luaL_checkany(L, 1);
1658 if (lua_type(L, 1) == LUA_TUSERDATA) /* check vim udata? */
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001659 {
Bram Moolenaar1dced572012-04-05 16:54:08 +02001660 lua_settop(L, 1);
1661 if (lua_getmetatable(L, 1))
1662 {
1663 luaV_getfield(L, LUAVIM_LIST);
1664 if (lua_rawequal(L, -1, 2))
1665 {
1666 lua_pushstring(L, "list");
1667 return 1;
1668 }
1669 luaV_getfield(L, LUAVIM_DICT);
1670 if (lua_rawequal(L, -1, 2))
1671 {
1672 lua_pushstring(L, "dict");
1673 return 1;
1674 }
Bram Moolenaarca06da92018-07-01 15:12:05 +02001675 luaV_getfield(L, LUAVIM_FUNCREF);
1676 if (lua_rawequal(L, -1, 2))
1677 {
1678 lua_pushstring(L, "funcref");
1679 return 1;
1680 }
Bram Moolenaar1dced572012-04-05 16:54:08 +02001681 luaV_getfield(L, LUAVIM_BUFFER);
1682 if (lua_rawequal(L, -1, 2))
1683 {
1684 lua_pushstring(L, "buffer");
1685 return 1;
1686 }
1687 luaV_getfield(L, LUAVIM_WINDOW);
1688 if (lua_rawequal(L, -1, 2))
1689 {
1690 lua_pushstring(L, "window");
1691 return 1;
1692 }
1693 }
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001694 }
Bram Moolenaar1dced572012-04-05 16:54:08 +02001695 lua_pushstring(L, luaL_typename(L, 1)); /* fallback */
1696 return 1;
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001697}
1698
1699static const luaL_Reg luaV_module[] = {
1700 {"command", luaV_command},
1701 {"eval", luaV_eval},
1702 {"beep", luaV_beep},
1703 {"line", luaV_line},
Bram Moolenaar1dced572012-04-05 16:54:08 +02001704 {"list", luaV_list},
1705 {"dict", luaV_dict},
Bram Moolenaarca06da92018-07-01 15:12:05 +02001706 {"funcref", luaV_funcref},
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001707 {"buffer", luaV_buffer},
1708 {"window", luaV_window},
1709 {"open", luaV_open},
Bram Moolenaar1dced572012-04-05 16:54:08 +02001710 {"type", luaV_type},
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001711 {NULL, NULL}
1712};
1713
Bram Moolenaar1dced572012-04-05 16:54:08 +02001714/* for freeing list, dict, buffer and window objects; lightuserdata as arg */
1715 static int
1716luaV_free(lua_State *L)
1717{
1718 lua_pushnil(L);
1719 luaV_setudata(L, lua_touserdata(L, 1));
1720 return 0;
1721}
1722
1723 static int
1724luaV_luaeval (lua_State *L)
1725{
1726 luaL_Buffer b;
1727 size_t l;
1728 const char *str = lua_tolstring(L, 1, &l);
1729 typval_T *arg = (typval_T *) lua_touserdata(L, 2);
1730 typval_T *rettv = (typval_T *) lua_touserdata(L, 3);
1731 luaL_buffinit(L, &b);
1732 luaL_addlstring(&b, LUAVIM_EVALHEADER, sizeof(LUAVIM_EVALHEADER) - 1);
1733 luaL_addlstring(&b, str, l);
1734 luaL_pushresult(&b);
1735 str = lua_tolstring(L, -1, &l);
1736 if (luaL_loadbuffer(L, str, l, LUAVIM_EVALNAME)) /* compile error? */
1737 {
1738 luaV_emsg(L);
1739 return 0;
1740 }
1741 luaV_pushtypval(L, arg);
1742 if (lua_pcall(L, 1, 1, 0)) /* running error? */
1743 {
1744 luaV_emsg(L);
1745 return 0;
1746 }
Bram Moolenaarca06da92018-07-01 15:12:05 +02001747 if (luaV_totypval(L, -1, rettv) == FAIL)
1748 EMSG("luaeval: cannot convert value");
Bram Moolenaarf554a322015-02-04 23:08:01 +01001749 return 0;
Bram Moolenaar1dced572012-04-05 16:54:08 +02001750}
1751
1752 static int
1753luaV_setref (lua_State *L)
1754{
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01001755 int copyID = lua_tointeger(L, 1);
1756 int abort = FALSE;
1757 typval_T tv;
1758
Bram Moolenaar1dced572012-04-05 16:54:08 +02001759 luaV_getfield(L, LUAVIM_LIST);
1760 luaV_getfield(L, LUAVIM_DICT);
1761 lua_pushnil(L);
Bram Moolenaarb84634d2015-02-04 22:02:37 +01001762 /* traverse cache table */
1763 while (!abort && lua_next(L, lua_upvalueindex(1)) != 0)
Bram Moolenaar1dced572012-04-05 16:54:08 +02001764 {
1765 lua_getmetatable(L, -1);
1766 if (lua_rawequal(L, -1, 2)) /* list? */
1767 {
1768 tv.v_type = VAR_LIST;
1769 tv.vval.v_list = (list_T *) lua_touserdata(L, 4); /* key */
Bram Moolenaarf609dcf2015-12-03 17:43:17 +01001770 abort = set_ref_in_item(&tv, copyID, NULL, NULL);
Bram Moolenaar1dced572012-04-05 16:54:08 +02001771 }
1772 else if (lua_rawequal(L, -1, 3)) /* dict? */
1773 {
1774 tv.v_type = VAR_DICT;
1775 tv.vval.v_dict = (dict_T *) lua_touserdata(L, 4); /* key */
Bram Moolenaarf609dcf2015-12-03 17:43:17 +01001776 abort = set_ref_in_item(&tv, copyID, NULL, NULL);
Bram Moolenaar1dced572012-04-05 16:54:08 +02001777 }
1778 lua_pop(L, 2); /* metatable and value */
Bram Moolenaar1dced572012-04-05 16:54:08 +02001779 }
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01001780 lua_pushinteger(L, abort);
Bram Moolenaarf554a322015-02-04 23:08:01 +01001781 return 1;
Bram Moolenaar1dced572012-04-05 16:54:08 +02001782}
1783
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001784 static int
1785luaopen_vim(lua_State *L)
1786{
Bram Moolenaar1dced572012-04-05 16:54:08 +02001787 /* set cache table */
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001788 lua_newtable(L);
1789 lua_newtable(L);
Bram Moolenaar1dced572012-04-05 16:54:08 +02001790 lua_pushstring(L, "v");
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001791 lua_setfield(L, -2, "__mode");
Bram Moolenaar1dced572012-04-05 16:54:08 +02001792 lua_setmetatable(L, -2); /* cache is weak-valued */
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001793 /* print */
1794 lua_pushcfunction(L, luaV_print);
1795 lua_setglobal(L, "print");
Bram Moolenaar38e2b062011-09-21 17:15:39 +02001796 /* debug.debug */
1797 lua_getglobal(L, "debug");
1798 lua_pushcfunction(L, luaV_debug);
1799 lua_setfield(L, -2, "debug");
1800 lua_pop(L, 1);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001801 /* free */
1802 lua_pushlightuserdata(L, (void *) LUAVIM_FREE);
Bram Moolenaar1dced572012-04-05 16:54:08 +02001803 lua_pushvalue(L, 1); /* cache table */
1804 lua_pushcclosure(L, luaV_free, 1);
1805 lua_rawset(L, LUA_REGISTRYINDEX);
1806 /* luaeval */
1807 lua_pushlightuserdata(L, (void *) LUAVIM_LUAEVAL);
1808 lua_pushvalue(L, 1); /* cache table */
1809 lua_pushcclosure(L, luaV_luaeval, 1);
1810 lua_rawset(L, LUA_REGISTRYINDEX);
1811 /* setref */
1812 lua_pushlightuserdata(L, (void *) LUAVIM_SETREF);
1813 lua_pushvalue(L, 1); /* cache table */
1814 lua_pushcclosure(L, luaV_setref, 1);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001815 lua_rawset(L, LUA_REGISTRYINDEX);
1816 /* register */
Bram Moolenaar1dced572012-04-05 16:54:08 +02001817 luaV_newmetatable(L, LUAVIM_LIST);
1818 lua_pushvalue(L, 1);
1819 luaV_openlib(L, luaV_List_mt, 1);
1820 luaV_newmetatable(L, LUAVIM_DICT);
1821 lua_pushvalue(L, 1);
1822 luaV_openlib(L, luaV_Dict_mt, 1);
Bram Moolenaarca06da92018-07-01 15:12:05 +02001823 luaV_newmetatable(L, LUAVIM_FUNCREF);
1824 lua_pushvalue(L, 1);
1825 luaV_openlib(L, luaV_Funcref_mt, 1);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001826 luaV_newmetatable(L, LUAVIM_BUFFER);
Bram Moolenaar1dced572012-04-05 16:54:08 +02001827 lua_pushvalue(L, 1); /* cache table */
1828 luaV_openlib(L, luaV_Buffer_mt, 1);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001829 luaV_newmetatable(L, LUAVIM_WINDOW);
Bram Moolenaar1dced572012-04-05 16:54:08 +02001830 lua_pushvalue(L, 1); /* cache table */
1831 luaV_openlib(L, luaV_Window_mt, 1);
1832 lua_newtable(L); /* vim table */
1833 lua_pushvalue(L, 1); /* cache table */
1834 luaV_openlib(L, luaV_module, 1);
1835 lua_setglobal(L, LUAVIM_NAME);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001836 return 0;
1837}
1838
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001839 static lua_State *
1840luaV_newstate(void)
1841{
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001842 lua_State *L = luaL_newstate();
Bram Moolenaar16c98f92010-07-28 22:46:08 +02001843 luaL_openlibs(L); /* core libs */
1844 lua_pushcfunction(L, luaopen_vim); /* vim */
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001845 lua_call(L, 0, 0);
1846 return L;
1847}
1848
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001849 static void
1850luaV_setrange(lua_State *L, int line1, int line2)
1851{
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001852 lua_getglobal(L, LUAVIM_NAME);
1853 lua_pushinteger(L, line1);
1854 lua_setfield(L, -2, "firstline");
1855 lua_pushinteger(L, line2);
1856 lua_setfield(L, -2, "lastline");
1857 lua_pop(L, 1); /* vim table */
1858}
1859
1860
1861/* ======= Interface ======= */
1862
1863static lua_State *L = NULL;
1864
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001865 static int
Bram Moolenaar1dced572012-04-05 16:54:08 +02001866lua_isopen(void)
Bram Moolenaar2bd6a1b2010-08-12 22:14:01 +02001867{
1868 return L != NULL;
1869}
1870
1871 static int
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001872lua_init(void)
1873{
Bram Moolenaar1dced572012-04-05 16:54:08 +02001874 if (!lua_isopen())
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001875 {
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001876#ifdef DYNAMIC_LUA
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001877 if (!lua_enabled(TRUE))
1878 {
1879 EMSG(_("Lua library cannot be loaded."));
1880 return FAIL;
1881 }
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001882#endif
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001883 L = luaV_newstate();
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001884 }
1885 return OK;
1886}
1887
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001888 void
1889lua_end(void)
1890{
Bram Moolenaar1dced572012-04-05 16:54:08 +02001891 if (lua_isopen())
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001892 {
1893 lua_close(L);
1894 L = NULL;
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001895#ifdef DYNAMIC_LUA
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001896 end_dynamic_lua();
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001897#endif
1898 }
1899}
1900
1901/* ex commands */
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001902 void
1903ex_lua(exarg_T *eap)
1904{
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001905 char *script;
1906 if (lua_init() == FAIL) return;
1907 script = (char *) script_get(eap, eap->arg);
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001908 if (!eap->skip)
1909 {
1910 char *s = (script) ? script : (char *) eap->arg;
1911 luaV_setrange(L, eap->line1, eap->line2);
1912 if (luaL_loadbuffer(L, s, strlen(s), LUAVIM_CHUNKNAME)
1913 || lua_pcall(L, 0, 0, 0))
1914 luaV_emsg(L);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001915 }
1916 if (script != NULL) vim_free(script);
1917}
1918
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001919 void
1920ex_luado(exarg_T *eap)
1921{
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001922 linenr_T l;
1923 const char *s = (const char *) eap->arg;
1924 luaL_Buffer b;
1925 size_t len;
Bram Moolenaard58f03b2017-01-29 22:48:45 +01001926 buf_T *was_curbuf = curbuf;
1927
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001928 if (lua_init() == FAIL) return;
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001929 if (u_save(eap->line1 - 1, eap->line2 + 1) == FAIL)
1930 {
1931 EMSG(_("cannot save undo information"));
1932 return;
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001933 }
1934 luaV_setrange(L, eap->line1, eap->line2);
1935 luaL_buffinit(L, &b);
Bram Moolenaarbd2f3c32012-04-06 14:31:00 +02001936 luaL_addlstring(&b, "return function(line, linenr) ", 30); /* header */
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001937 luaL_addlstring(&b, s, strlen(s));
1938 luaL_addlstring(&b, " end", 4); /* footer */
1939 luaL_pushresult(&b);
1940 s = lua_tolstring(L, -1, &len);
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001941 if (luaL_loadbuffer(L, s, len, LUAVIM_CHUNKNAME))
1942 {
1943 luaV_emsg(L);
1944 lua_pop(L, 1); /* function body */
1945 return;
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001946 }
1947 lua_call(L, 0, 1);
1948 lua_replace(L, -2); /* function -> body */
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001949 for (l = eap->line1; l <= eap->line2; l++)
1950 {
Bram Moolenaard58f03b2017-01-29 22:48:45 +01001951 /* Check the line number, the command my have deleted lines. */
1952 if (l > curbuf->b_ml.ml_line_count)
1953 break;
1954
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001955 lua_pushvalue(L, -1); /* function */
1956 luaV_pushline(L, curbuf, l); /* current line as arg */
Bram Moolenaarbd2f3c32012-04-06 14:31:00 +02001957 lua_pushinteger(L, l); /* current line number as arg */
1958 if (lua_pcall(L, 2, 1, 0))
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001959 {
1960 luaV_emsg(L);
1961 break;
1962 }
Bram Moolenaard58f03b2017-01-29 22:48:45 +01001963 /* Catch the command switching to another buffer. */
1964 if (curbuf != was_curbuf)
1965 break;
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001966 if (lua_isstring(L, -1)) /* update line? */
1967 {
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001968#ifdef HAVE_SANDBOX
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001969 luaV_checksandbox(L);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001970#endif
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001971 ml_replace(l, luaV_toline(L, -1), TRUE);
1972 changed_bytes(l, 0);
1973 lua_pop(L, 1); /* result from luaV_toline */
1974 }
1975 lua_pop(L, 1); /* line */
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001976 }
1977 lua_pop(L, 1); /* function */
1978 check_cursor();
1979 update_screen(NOT_VALID);
1980}
1981
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001982 void
1983ex_luafile(exarg_T *eap)
1984{
1985 if (lua_init() == FAIL)
1986 return;
1987 if (!eap->skip)
1988 {
1989 luaV_setrange(L, eap->line1, eap->line2);
1990 if (luaL_loadfile(L, (char *) eap->arg) || lua_pcall(L, 0, 0, 0))
1991 luaV_emsg(L);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001992 }
1993}
1994
Bram Moolenaar1dced572012-04-05 16:54:08 +02001995#define luaV_freetype(typ,tname) \
1996 void \
1997 lua_##tname##_free(typ *o) \
1998 { \
1999 if (!lua_isopen()) return; \
2000 luaV_getfield(L, LUAVIM_FREE); \
2001 lua_pushlightuserdata(L, (void *) o); \
2002 lua_call(L, 1, 0); \
2003 }
2004
2005luaV_freetype(buf_T, buffer)
2006luaV_freetype(win_T, window)
2007
Bram Moolenaar55d5c032010-07-17 23:52:29 +02002008 void
Bram Moolenaar1dced572012-04-05 16:54:08 +02002009do_luaeval (char_u *str, typval_T *arg, typval_T *rettv)
Bram Moolenaar55d5c032010-07-17 23:52:29 +02002010{
Bram Moolenaar1dced572012-04-05 16:54:08 +02002011 lua_init();
2012 luaV_getfield(L, LUAVIM_LUAEVAL);
2013 lua_pushstring(L, (char *) str);
2014 lua_pushlightuserdata(L, (void *) arg);
2015 lua_pushlightuserdata(L, (void *) rettv);
2016 lua_call(L, 3, 0);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02002017}
2018
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01002019 int
Bram Moolenaar1dced572012-04-05 16:54:08 +02002020set_ref_in_lua (int copyID)
Bram Moolenaar55d5c032010-07-17 23:52:29 +02002021{
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01002022 int aborted = 0;
2023
2024 if (lua_isopen())
2025 {
2026 luaV_getfield(L, LUAVIM_SETREF);
2027 /* call the function with 1 arg, getting 1 result back */
2028 lua_pushinteger(L, copyID);
2029 lua_call(L, 1, 1);
2030 /* get the result */
2031 aborted = lua_tointeger(L, -1);
2032 /* pop result off the stack */
2033 lua_pop(L, 1);
2034 }
2035 return aborted;
Bram Moolenaar0ba04292010-07-14 23:23:17 +02002036}
2037
2038#endif