blob: 3886401be516b36830acf715789262175857932a [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 Moolenaard6ef5f92018-07-13 22:08:23 +0200961 if (key == NULL)
962 return 0;
963 if (*key == NUL)
Bram Moolenaarca06da92018-07-01 15:12:05 +0200964 luaL_error(L, "empty key");
965 if (!lua_isnil(L, 3)) { /* read value? */
966 luaV_checktypval(L, 3, &v, "setting dict item");
967 if (d->dv_scope == VAR_DEF_SCOPE && v.v_type == VAR_FUNC)
968 luaL_error(L, "cannot assign funcref to builtin scope");
969 }
Bram Moolenaar1dced572012-04-05 16:54:08 +0200970 di = dict_find(d, key, -1);
971 if (di == NULL) /* non-existing key? */
972 {
Bram Moolenaard6ef5f92018-07-13 22:08:23 +0200973 if (lua_isnil(L, 3))
974 return 0;
Bram Moolenaar1dced572012-04-05 16:54:08 +0200975 di = dictitem_alloc(key);
Bram Moolenaard6ef5f92018-07-13 22:08:23 +0200976 if (di == NULL)
977 return 0;
Bram Moolenaar1dced572012-04-05 16:54:08 +0200978 if (dict_add(d, di) == FAIL)
979 {
Bram Moolenaard6ef5f92018-07-13 22:08:23 +0200980 vim_free(di);
981 return 0;
Bram Moolenaar1dced572012-04-05 16:54:08 +0200982 }
983 }
984 else
985 clear_tv(&di->di_tv);
986 if (lua_isnil(L, 3)) /* remove? */
987 {
988 hashitem_T *hi = hash_find(&d->dv_hashtab, di->di_key);
989 hash_remove(&d->dv_hashtab, hi);
990 dictitem_free(di);
991 }
Bram Moolenaarca06da92018-07-01 15:12:05 +0200992 else
993 {
Bram Moolenaar1dced572012-04-05 16:54:08 +0200994 copy_tv(&v, &di->di_tv);
Bram Moolenaarb3766472013-04-15 13:49:21 +0200995 clear_tv(&v);
Bram Moolenaar1dced572012-04-05 16:54:08 +0200996 }
997 return 0;
998}
999
1000static const luaL_Reg luaV_Dict_mt[] = {
1001 {"__tostring", luaV_dict_tostring},
Bram Moolenaar1dced572012-04-05 16:54:08 +02001002 {"__len", luaV_dict_len},
1003 {"__call", luaV_dict_call},
1004 {"__index", luaV_dict_index},
1005 {"__newindex", luaV_dict_newindex},
1006 {NULL, NULL}
1007};
1008
1009
Bram Moolenaarca06da92018-07-01 15:12:05 +02001010/* ======= Funcref type ======= */
1011
1012 static luaV_Funcref *
1013luaV_newfuncref(lua_State *L, char_u *name)
1014{
1015 luaV_Funcref *f = (luaV_Funcref *)lua_newuserdata(L, sizeof(luaV_Funcref));
1016
1017 if (name != NULL)
1018 {
1019 func_ref(name); /* as in copy_tv */
1020 f->tv.vval.v_string = vim_strsave(name);
1021 }
1022 f->tv.v_type = VAR_FUNC;
1023 f->args.v_type = VAR_LIST;
1024 f->self = NULL;
1025 luaV_getfield(L, LUAVIM_FUNCREF);
1026 lua_setmetatable(L, -2);
1027 return f;
1028}
1029
1030 static luaV_Funcref *
1031luaV_pushfuncref(lua_State *L, typval_T *tv)
1032{
1033 luaV_Funcref *f = luaV_newfuncref(L, NULL);
1034 copy_tv(tv, &f->tv);
1035 clear_tv(tv);
1036 return f;
1037}
1038
1039
1040luaV_type_tostring(funcref, LUAVIM_FUNCREF)
1041
1042 static int
1043luaV_funcref_gc(lua_State *L)
1044{
1045 luaV_Funcref *f = (luaV_Funcref *) lua_touserdata(L, 1);
1046
1047 func_unref(f->tv.vval.v_string);
1048 vim_free(f->tv.vval.v_string);
1049 dict_unref(f->self);
1050 return 0;
1051}
1052
1053/* equivalent to string(funcref) */
1054 static int
1055luaV_funcref_len(lua_State *L)
1056{
1057 luaV_Funcref *f = (luaV_Funcref *) lua_touserdata(L, 1);
1058
1059 lua_pushstring(L, (const char *) f->tv.vval.v_string);
1060 return 1;
1061}
1062
1063 static int
1064luaV_funcref_call(lua_State *L)
1065{
1066 luaV_Funcref *f = (luaV_Funcref *) lua_touserdata(L, 1);
1067 int i, n = lua_gettop(L) - 1; /* #args */
1068 int status;
1069 typval_T v, rettv;
1070
1071 f->args.vval.v_list = list_alloc();
1072 rettv.v_type = VAR_UNKNOWN; /* as in clear_tv */
Bram Moolenaard6ef5f92018-07-13 22:08:23 +02001073 if (f->args.vval.v_list == NULL)
1074 status = FAIL;
1075 else
1076 {
1077 for (i = 0; i < n; i++) {
1078 luaV_checktypval(L, i + 2, &v, "calling funcref");
1079 list_append_tv(f->args.vval.v_list, &v);
1080 }
1081 status = func_call(f->tv.vval.v_string, &f->args,
1082 NULL, f->self, &rettv);
1083 if (status == OK)
1084 luaV_pushtypval(L, &rettv);
1085 clear_tv(&f->args);
1086 clear_tv(&rettv);
Bram Moolenaarca06da92018-07-01 15:12:05 +02001087 }
Bram Moolenaarca06da92018-07-01 15:12:05 +02001088 if (status != OK)
1089 luaL_error(L, "cannot call funcref");
1090 return 1;
1091}
1092
1093static const luaL_Reg luaV_Funcref_mt[] = {
1094 {"__tostring", luaV_funcref_tostring},
1095 {"__gc", luaV_funcref_gc},
1096 {"__len", luaV_funcref_len},
1097 {"__call", luaV_funcref_call},
1098 {NULL, NULL}
1099};
1100
1101
Bram Moolenaar1dced572012-04-05 16:54:08 +02001102/* ======= Buffer type ======= */
1103
1104luaV_newtype(buf_T, buffer, luaV_Buffer, LUAVIM_BUFFER)
1105luaV_pushtype(buf_T, buffer, luaV_Buffer)
1106luaV_type_tostring(buffer, LUAVIM_BUFFER)
1107
1108 static int
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001109luaV_buffer_len(lua_State *L)
1110{
Bram Moolenaar1dced572012-04-05 16:54:08 +02001111 buf_T *b = (buf_T *) luaV_checkvalid(L, luaV_Buffer, 1);
1112 lua_pushinteger(L, b->b_ml.ml_line_count);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001113 return 1;
1114}
1115
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001116 static int
1117luaV_buffer_call(lua_State *L)
1118{
Bram Moolenaar1dced572012-04-05 16:54:08 +02001119 buf_T *b = (buf_T *) luaV_checkvalid(L, luaV_Buffer, 1);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001120 lua_settop(L, 1);
Bram Moolenaar1dced572012-04-05 16:54:08 +02001121 set_curbuf(b, DOBUF_SPLIT);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001122 return 1;
1123}
1124
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001125 static int
1126luaV_buffer_index(lua_State *L)
1127{
Bram Moolenaar1dced572012-04-05 16:54:08 +02001128 buf_T *b = (buf_T *) luaV_checkvalid(L, luaV_Buffer, 1);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001129 linenr_T n = (linenr_T) lua_tointeger(L, 2);
Bram Moolenaar1dced572012-04-05 16:54:08 +02001130 if (n > 0 && n <= b->b_ml.ml_line_count)
1131 luaV_pushline(L, b, n);
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001132 else if (lua_isstring(L, 2))
1133 {
1134 const char *s = lua_tostring(L, 2);
1135 if (strncmp(s, "name", 4) == 0)
Bram Moolenaarfe08df42018-07-07 23:07:41 +02001136 lua_pushstring(L, (b->b_sfname == NULL)
1137 ? "" : (char *) b->b_sfname);
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001138 else if (strncmp(s, "fname", 5) == 0)
Bram Moolenaarfe08df42018-07-07 23:07:41 +02001139 lua_pushstring(L, (b->b_ffname == NULL)
1140 ? "" : (char *) b->b_ffname);
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001141 else if (strncmp(s, "number", 6) == 0)
Bram Moolenaar1dced572012-04-05 16:54:08 +02001142 lua_pushinteger(L, b->b_fnum);
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001143 /* methods */
1144 else if (strncmp(s, "insert", 6) == 0
1145 || strncmp(s, "next", 4) == 0
1146 || strncmp(s, "previous", 8) == 0
1147 || strncmp(s, "isvalid", 7) == 0)
1148 {
1149 lua_getmetatable(L, 1);
1150 lua_getfield(L, -1, s);
1151 }
1152 else
1153 lua_pushnil(L);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001154 }
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001155 else
1156 lua_pushnil(L);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001157 return 1;
1158}
1159
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001160 static int
1161luaV_buffer_newindex(lua_State *L)
1162{
Bram Moolenaar1dced572012-04-05 16:54:08 +02001163 buf_T *b = (buf_T *) luaV_checkvalid(L, luaV_Buffer, 1);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001164 linenr_T n = (linenr_T) luaL_checkinteger(L, 2);
1165#ifdef HAVE_SANDBOX
1166 luaV_checksandbox(L);
1167#endif
Bram Moolenaar1dced572012-04-05 16:54:08 +02001168 if (n < 1 || n > b->b_ml.ml_line_count)
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001169 luaL_error(L, "invalid line number");
1170 if (lua_isnil(L, 3)) /* delete line */
1171 {
1172 buf_T *buf = curbuf;
Bram Moolenaar1dced572012-04-05 16:54:08 +02001173 curbuf = b;
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001174 if (u_savedel(n, 1L) == FAIL)
1175 {
1176 curbuf = buf;
1177 luaL_error(L, "cannot save undo information");
1178 }
1179 else if (ml_delete(n, FALSE) == FAIL)
1180 {
1181 curbuf = buf;
1182 luaL_error(L, "cannot delete line");
1183 }
Bram Moolenaarca06da92018-07-01 15:12:05 +02001184 else
1185 {
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001186 deleted_lines_mark(n, 1L);
Bram Moolenaar1dced572012-04-05 16:54:08 +02001187 if (b == curwin->w_buffer) /* fix cursor in current window? */
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001188 {
1189 if (curwin->w_cursor.lnum >= n)
1190 {
1191 if (curwin->w_cursor.lnum > n)
1192 {
1193 curwin->w_cursor.lnum -= 1;
1194 check_cursor_col();
1195 }
1196 else check_cursor();
1197 changed_cline_bef_curs();
1198 }
1199 invalidate_botline();
1200 }
1201 }
1202 curbuf = buf;
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001203 }
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001204 else if (lua_isstring(L, 3)) /* update line */
1205 {
1206 buf_T *buf = curbuf;
Bram Moolenaar1dced572012-04-05 16:54:08 +02001207 curbuf = b;
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001208 if (u_savesub(n) == FAIL)
1209 {
1210 curbuf = buf;
1211 luaL_error(L, "cannot save undo information");
1212 }
1213 else if (ml_replace(n, luaV_toline(L, 3), TRUE) == FAIL)
1214 {
1215 curbuf = buf;
1216 luaL_error(L, "cannot replace line");
1217 }
1218 else changed_bytes(n, 0);
1219 curbuf = buf;
Bram Moolenaar1dced572012-04-05 16:54:08 +02001220 if (b == curwin->w_buffer)
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001221 check_cursor_col();
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001222 }
1223 else
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001224 luaL_error(L, "wrong argument to change line");
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001225 return 0;
1226}
1227
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001228 static int
1229luaV_buffer_insert(lua_State *L)
1230{
Bram Moolenaar1dced572012-04-05 16:54:08 +02001231 luaV_Buffer *lb = luaV_checkudata(L, 1, LUAVIM_BUFFER);
1232 buf_T *b = (buf_T *) luaV_checkcache(L, (void *) *lb);
1233 linenr_T last = b->b_ml.ml_line_count;
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001234 linenr_T n = (linenr_T) luaL_optinteger(L, 3, last);
1235 buf_T *buf;
1236 luaL_checktype(L, 2, LUA_TSTRING);
1237#ifdef HAVE_SANDBOX
1238 luaV_checksandbox(L);
1239#endif
1240 /* fix insertion line */
1241 if (n < 0) n = 0;
1242 if (n > last) n = last;
1243 /* insert */
1244 buf = curbuf;
Bram Moolenaar1dced572012-04-05 16:54:08 +02001245 curbuf = b;
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001246 if (u_save(n, n + 1) == FAIL)
1247 {
1248 curbuf = buf;
1249 luaL_error(L, "cannot save undo information");
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001250 }
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001251 else if (ml_append(n, luaV_toline(L, 2), 0, FALSE) == FAIL)
1252 {
1253 curbuf = buf;
1254 luaL_error(L, "cannot insert line");
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001255 }
1256 else
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001257 appended_lines_mark(n, 1L);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001258 curbuf = buf;
1259 update_screen(VALID);
1260 return 0;
1261}
1262
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001263 static int
1264luaV_buffer_next(lua_State *L)
1265{
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001266 luaV_Buffer *b = luaV_checkudata(L, 1, LUAVIM_BUFFER);
Bram Moolenaar1dced572012-04-05 16:54:08 +02001267 buf_T *buf = (buf_T *) luaV_checkcache(L, (void *) *b);
1268 luaV_pushbuffer(L, buf->b_next);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001269 return 1;
1270}
1271
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001272 static int
1273luaV_buffer_previous(lua_State *L)
1274{
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001275 luaV_Buffer *b = luaV_checkudata(L, 1, LUAVIM_BUFFER);
Bram Moolenaar1dced572012-04-05 16:54:08 +02001276 buf_T *buf = (buf_T *) luaV_checkcache(L, (void *) *b);
1277 luaV_pushbuffer(L, buf->b_prev);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001278 return 1;
1279}
1280
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001281 static int
1282luaV_buffer_isvalid(lua_State *L)
1283{
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001284 luaV_Buffer *b = luaV_checkudata(L, 1, LUAVIM_BUFFER);
Bram Moolenaar1dced572012-04-05 16:54:08 +02001285 luaV_getudata(L, *b);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001286 lua_pushboolean(L, !lua_isnil(L, -1));
1287 return 1;
1288}
1289
1290static const luaL_Reg luaV_Buffer_mt[] = {
1291 {"__tostring", luaV_buffer_tostring},
1292 {"__len", luaV_buffer_len},
1293 {"__call", luaV_buffer_call},
1294 {"__index", luaV_buffer_index},
1295 {"__newindex", luaV_buffer_newindex},
1296 {"insert", luaV_buffer_insert},
1297 {"next", luaV_buffer_next},
1298 {"previous", luaV_buffer_previous},
1299 {"isvalid", luaV_buffer_isvalid},
1300 {NULL, NULL}
1301};
1302
1303
1304/* ======= Window type ======= */
1305
Bram Moolenaar1dced572012-04-05 16:54:08 +02001306luaV_newtype(win_T, window, luaV_Window, LUAVIM_WINDOW)
1307luaV_pushtype(win_T, window, luaV_Window)
1308luaV_type_tostring(window, LUAVIM_WINDOW)
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001309
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001310 static int
1311luaV_window_call(lua_State *L)
1312{
Bram Moolenaar1dced572012-04-05 16:54:08 +02001313 win_T *w = (win_T *) luaV_checkvalid(L, luaV_Window, 1);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001314 lua_settop(L, 1);
Bram Moolenaar1dced572012-04-05 16:54:08 +02001315 win_goto(w);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001316 return 1;
1317}
1318
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001319 static int
1320luaV_window_index(lua_State *L)
1321{
Bram Moolenaar1dced572012-04-05 16:54:08 +02001322 win_T *w = (win_T *) luaV_checkvalid(L, luaV_Window, 1);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001323 const char *s = luaL_checkstring(L, 2);
1324 if (strncmp(s, "buffer", 6) == 0)
Bram Moolenaar1dced572012-04-05 16:54:08 +02001325 luaV_pushbuffer(L, w->w_buffer);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001326 else if (strncmp(s, "line", 4) == 0)
Bram Moolenaar1dced572012-04-05 16:54:08 +02001327 lua_pushinteger(L, w->w_cursor.lnum);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001328 else if (strncmp(s, "col", 3) == 0)
Bram Moolenaar1dced572012-04-05 16:54:08 +02001329 lua_pushinteger(L, w->w_cursor.col + 1);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001330 else if (strncmp(s, "width", 5) == 0)
Bram Moolenaar02631462017-09-22 15:20:32 +02001331 lua_pushinteger(L, w->w_width);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001332 else if (strncmp(s, "height", 6) == 0)
Bram Moolenaar1dced572012-04-05 16:54:08 +02001333 lua_pushinteger(L, w->w_height);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001334 /* methods */
1335 else if (strncmp(s, "next", 4) == 0
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001336 || strncmp(s, "previous", 8) == 0
1337 || strncmp(s, "isvalid", 7) == 0)
1338 {
1339 lua_getmetatable(L, 1);
1340 lua_getfield(L, -1, s);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001341 }
1342 else
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001343 lua_pushnil(L);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001344 return 1;
1345}
1346
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001347 static int
1348luaV_window_newindex (lua_State *L)
1349{
Bram Moolenaar1dced572012-04-05 16:54:08 +02001350 win_T *w = (win_T *) luaV_checkvalid(L, luaV_Window, 1);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001351 const char *s = luaL_checkstring(L, 2);
1352 int v = luaL_checkinteger(L, 3);
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001353 if (strncmp(s, "line", 4) == 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 if (v < 1 || v > w->w_buffer->b_ml.ml_line_count)
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001359 luaL_error(L, "line out of range");
Bram Moolenaar1dced572012-04-05 16:54:08 +02001360 w->w_cursor.lnum = v;
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001361 update_screen(VALID);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001362 }
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001363 else if (strncmp(s, "col", 3) == 0)
1364 {
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001365#ifdef HAVE_SANDBOX
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001366 luaV_checksandbox(L);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001367#endif
Bram Moolenaar1dced572012-04-05 16:54:08 +02001368 w->w_cursor.col = v - 1;
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001369 update_screen(VALID);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001370 }
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001371 else if (strncmp(s, "width", 5) == 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_setwidth(v);
1379 curwin = win;
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001380 }
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001381 else if (strncmp(s, "height", 6) == 0)
1382 {
1383 win_T *win = curwin;
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001384#ifdef FEAT_GUI
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001385 need_mouse_correct = TRUE;
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001386#endif
Bram Moolenaar1dced572012-04-05 16:54:08 +02001387 curwin = w;
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001388 win_setheight(v);
1389 curwin = win;
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001390 }
1391 else
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001392 luaL_error(L, "invalid window property: `%s'", s);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001393 return 0;
1394}
1395
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001396 static int
1397luaV_window_next(lua_State *L)
1398{
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001399 luaV_Window *w = luaV_checkudata(L, 1, LUAVIM_WINDOW);
Bram Moolenaar1dced572012-04-05 16:54:08 +02001400 win_T *win = (win_T *) luaV_checkcache(L, (void *) *w);
1401 luaV_pushwindow(L, win->w_next);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001402 return 1;
1403}
1404
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001405 static int
1406luaV_window_previous(lua_State *L)
1407{
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001408 luaV_Window *w = luaV_checkudata(L, 1, LUAVIM_WINDOW);
Bram Moolenaar1dced572012-04-05 16:54:08 +02001409 win_T *win = (win_T *) luaV_checkcache(L, (void *) *w);
1410 luaV_pushwindow(L, win->w_prev);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001411 return 1;
1412}
1413
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001414 static int
1415luaV_window_isvalid(lua_State *L)
1416{
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001417 luaV_Window *w = luaV_checkudata(L, 1, LUAVIM_WINDOW);
Bram Moolenaar1dced572012-04-05 16:54:08 +02001418 luaV_getudata(L, *w);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001419 lua_pushboolean(L, !lua_isnil(L, -1));
1420 return 1;
1421}
1422
1423static const luaL_Reg luaV_Window_mt[] = {
1424 {"__tostring", luaV_window_tostring},
1425 {"__call", luaV_window_call},
1426 {"__index", luaV_window_index},
1427 {"__newindex", luaV_window_newindex},
1428 {"next", luaV_window_next},
1429 {"previous", luaV_window_previous},
1430 {"isvalid", luaV_window_isvalid},
1431 {NULL, NULL}
1432};
1433
1434
1435/* ======= Vim module ======= */
1436
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001437 static int
1438luaV_print(lua_State *L)
1439{
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001440 int i, n = lua_gettop(L); /* nargs */
1441 const char *s;
1442 size_t l;
1443 luaL_Buffer b;
1444 luaL_buffinit(L, &b);
1445 lua_getglobal(L, "tostring");
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001446 for (i = 1; i <= n; i++)
1447 {
1448 lua_pushvalue(L, -1); /* tostring */
1449 lua_pushvalue(L, i); /* arg */
1450 lua_call(L, 1, 1);
1451 s = lua_tolstring(L, -1, &l);
1452 if (s == NULL)
1453 return luaL_error(L, "cannot convert to string");
1454 if (i > 1) luaL_addchar(&b, ' '); /* use space instead of tab */
1455 luaV_addlstring(&b, s, l, 0);
1456 lua_pop(L, 1);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001457 }
1458 luaL_pushresult(&b);
1459 luaV_msg(L);
1460 return 0;
1461}
1462
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001463 static int
Bram Moolenaar38e2b062011-09-21 17:15:39 +02001464luaV_debug(lua_State *L)
1465{
1466 lua_settop(L, 0);
1467 lua_getglobal(L, "vim");
1468 lua_getfield(L, -1, "eval");
1469 lua_remove(L, -2); /* vim.eval at position 1 */
1470 for (;;)
1471 {
1472 const char *input;
1473 size_t l;
1474 lua_pushvalue(L, 1); /* vim.eval */
1475 lua_pushliteral(L, "input('lua_debug> ')");
1476 lua_call(L, 1, 1); /* return string */
1477 input = lua_tolstring(L, -1, &l);
1478 if (l == 0 || strcmp(input, "cont") == 0)
1479 return 0;
1480 msg_putchar('\n'); /* avoid outputting on input line */
1481 if (luaL_loadbuffer(L, input, l, "=(debug command)")
1482 || lua_pcall(L, 0, 0, 0))
1483 luaV_emsg(L);
1484 lua_settop(L, 1); /* remove eventual returns, but keep vim.eval */
1485 }
1486}
1487
1488 static int
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001489luaV_command(lua_State *L)
1490{
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001491 do_cmdline_cmd((char_u *) luaL_checkstring(L, 1));
1492 update_screen(VALID);
1493 return 0;
1494}
1495
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001496 static int
1497luaV_eval(lua_State *L)
1498{
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001499 typval_T *tv = eval_expr((char_u *) luaL_checkstring(L, 1), NULL);
1500 if (tv == NULL) luaL_error(L, "invalid expression");
1501 luaV_pushtypval(L, tv);
Bram Moolenaarb3766472013-04-15 13:49:21 +02001502 free_tv(tv);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001503 return 1;
1504}
1505
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001506 static int
Bram Moolenaar0d2e4fc2010-07-18 12:35:47 +02001507luaV_beep(lua_State *L UNUSED)
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001508{
Bram Moolenaar165bc692015-07-21 17:53:25 +02001509 vim_beep(BO_LANG);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001510 return 0;
1511}
1512
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001513 static int
1514luaV_line(lua_State *L)
1515{
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001516 luaV_pushline(L, curbuf, curwin->w_cursor.lnum);
1517 return 1;
1518}
1519
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001520 static int
Bram Moolenaar1dced572012-04-05 16:54:08 +02001521luaV_list(lua_State *L)
1522{
Bram Moolenaarca06da92018-07-01 15:12:05 +02001523 list_T *l;
1524 int initarg = !lua_isnoneornil(L, 1);
1525
1526 if (initarg && lua_type(L, 1) != LUA_TTABLE)
1527 luaL_error(L, "table expected, got %s", luaL_typename(L, 1));
1528 l = list_alloc();
Bram Moolenaar1dced572012-04-05 16:54:08 +02001529 if (l == NULL)
1530 lua_pushnil(L);
1531 else
Bram Moolenaarca06da92018-07-01 15:12:05 +02001532 {
Bram Moolenaar1dced572012-04-05 16:54:08 +02001533 luaV_newlist(L, l);
Bram Moolenaarca06da92018-07-01 15:12:05 +02001534 if (initarg) { /* traverse table to init dict */
1535 int notnil, i = 0;
1536 typval_T v;
1537 do {
1538 lua_rawgeti(L, 1, ++i);
1539 notnil = !lua_isnil(L, -1);
1540 if (notnil) {
1541 luaV_checktypval(L, -1, &v, "vim.list");
1542 list_append_tv(l, &v);
1543 }
1544 lua_pop(L, 1); /* value */
1545 } while (notnil);
1546 }
1547 }
Bram Moolenaar1dced572012-04-05 16:54:08 +02001548 return 1;
1549}
1550
1551 static int
1552luaV_dict(lua_State *L)
1553{
Bram Moolenaarca06da92018-07-01 15:12:05 +02001554 dict_T *d;
1555 int initarg = !lua_isnoneornil(L, 1);
1556
1557 if (initarg && lua_type(L, 1) != LUA_TTABLE)
1558 luaL_error(L, "table expected, got %s", luaL_typename(L, 1));
1559 d = dict_alloc();
Bram Moolenaar1dced572012-04-05 16:54:08 +02001560 if (d == NULL)
1561 lua_pushnil(L);
1562 else
Bram Moolenaarca06da92018-07-01 15:12:05 +02001563 {
Bram Moolenaar1dced572012-04-05 16:54:08 +02001564 luaV_newdict(L, d);
Bram Moolenaarca06da92018-07-01 15:12:05 +02001565 if (initarg) /* traverse table to init dict */
1566 {
1567 lua_pushnil(L);
1568 while (lua_next(L, 1))
1569 {
1570 char_u *key;
1571 dictitem_T *di;
1572 typval_T v;
Bram Moolenaard6ef5f92018-07-13 22:08:23 +02001573
Bram Moolenaarca06da92018-07-01 15:12:05 +02001574 lua_pushvalue(L, -2); /* dup key in case it's a number */
1575 key = (char_u *) lua_tostring(L, -1);
Bram Moolenaard6ef5f92018-07-13 22:08:23 +02001576 if (key == NULL)
1577 {
1578 lua_pushnil(L);
1579 return 1;
1580 }
1581 if (*key == NUL)
Bram Moolenaarca06da92018-07-01 15:12:05 +02001582 luaL_error(L, "table has empty key");
1583 luaV_checktypval(L, -2, &v, "vim.dict"); /* value */
1584 di = dictitem_alloc(key);
Bram Moolenaard6ef5f92018-07-13 22:08:23 +02001585 if (di == NULL || dict_add(d, di) == FAIL)
1586 {
Bram Moolenaarca06da92018-07-01 15:12:05 +02001587 vim_free(di);
1588 lua_pushnil(L);
1589 return 1;
1590 }
1591 copy_tv(&v, &di->di_tv);
1592 clear_tv(&v);
1593 lua_pop(L, 2); /* key copy and value */
1594 }
1595 }
1596 }
1597 return 1;
1598}
1599
1600 static int
1601luaV_funcref(lua_State *L)
1602{
1603 const char *name = luaL_checkstring(L, 1);
1604 /* note: not checking if function exists (needs function_exists) */
1605 if (name == NULL || *name == NUL || VIM_ISDIGIT(*name))
1606 luaL_error(L, "invalid function name: %s", name);
1607 luaV_newfuncref(L, (char_u *) name);
Bram Moolenaar1dced572012-04-05 16:54:08 +02001608 return 1;
1609}
1610
1611 static int
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001612luaV_buffer(lua_State *L)
1613{
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001614 buf_T *buf;
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001615 if (lua_isstring(L, 1)) /* get by number or name? */
1616 {
1617 if (lua_isnumber(L, 1)) /* by number? */
1618 {
1619 int n = lua_tointeger(L, 1);
Bram Moolenaar29323592016-07-24 22:04:11 +02001620 FOR_ALL_BUFFERS(buf)
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001621 if (buf->b_fnum == n) break;
1622 }
Bram Moolenaarca06da92018-07-01 15:12:05 +02001623 else // by name
1624 {
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001625 size_t l;
1626 const char *s = lua_tolstring(L, 1, &l);
Bram Moolenaar29323592016-07-24 22:04:11 +02001627 FOR_ALL_BUFFERS(buf)
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001628 {
1629 if (buf->b_ffname == NULL || buf->b_sfname == NULL)
1630 {
1631 if (l == 0) break;
1632 }
Bram Moolenaar0d2e4fc2010-07-18 12:35:47 +02001633 else if (strncmp(s, (char *)buf->b_ffname, l) == 0
1634 || strncmp(s, (char *)buf->b_sfname, l) == 0)
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001635 break;
1636 }
1637 }
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001638 }
Bram Moolenaar1dced572012-04-05 16:54:08 +02001639 else
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001640 buf = (lua_toboolean(L, 1)) ? firstbuf : curbuf; /* first buffer? */
Bram Moolenaar1dced572012-04-05 16:54:08 +02001641 luaV_pushbuffer(L, buf);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001642 return 1;
1643}
1644
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001645 static int
1646luaV_window(lua_State *L)
1647{
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001648 win_T *win;
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001649 if (lua_isnumber(L, 1)) /* get by number? */
1650 {
1651 int n = lua_tointeger(L, 1);
1652 for (win = firstwin; win != NULL; win = win->w_next, n--)
1653 if (n == 1) break;
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001654 }
Bram Moolenaar1dced572012-04-05 16:54:08 +02001655 else
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001656 win = (lua_toboolean(L, 1)) ? firstwin : curwin; /* first window? */
Bram Moolenaar1dced572012-04-05 16:54:08 +02001657 luaV_pushwindow(L, win);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001658 return 1;
1659}
1660
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001661 static int
1662luaV_open(lua_State *L)
1663{
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001664 char_u *s = NULL;
1665#ifdef HAVE_SANDBOX
1666 luaV_checksandbox(L);
1667#endif
1668 if (lua_isstring(L, 1)) s = (char_u *) lua_tostring(L, 1);
Bram Moolenaarfa263a52011-12-08 16:00:16 +01001669 luaV_pushbuffer(L, buflist_new(s, NULL, 1L, BLN_LISTED));
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001670 return 1;
1671}
1672
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001673 static int
Bram Moolenaar1dced572012-04-05 16:54:08 +02001674luaV_type(lua_State *L)
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001675{
Bram Moolenaar1dced572012-04-05 16:54:08 +02001676 luaL_checkany(L, 1);
1677 if (lua_type(L, 1) == LUA_TUSERDATA) /* check vim udata? */
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001678 {
Bram Moolenaar1dced572012-04-05 16:54:08 +02001679 lua_settop(L, 1);
1680 if (lua_getmetatable(L, 1))
1681 {
1682 luaV_getfield(L, LUAVIM_LIST);
1683 if (lua_rawequal(L, -1, 2))
1684 {
1685 lua_pushstring(L, "list");
1686 return 1;
1687 }
1688 luaV_getfield(L, LUAVIM_DICT);
1689 if (lua_rawequal(L, -1, 2))
1690 {
1691 lua_pushstring(L, "dict");
1692 return 1;
1693 }
Bram Moolenaarca06da92018-07-01 15:12:05 +02001694 luaV_getfield(L, LUAVIM_FUNCREF);
1695 if (lua_rawequal(L, -1, 2))
1696 {
1697 lua_pushstring(L, "funcref");
1698 return 1;
1699 }
Bram Moolenaar1dced572012-04-05 16:54:08 +02001700 luaV_getfield(L, LUAVIM_BUFFER);
1701 if (lua_rawequal(L, -1, 2))
1702 {
1703 lua_pushstring(L, "buffer");
1704 return 1;
1705 }
1706 luaV_getfield(L, LUAVIM_WINDOW);
1707 if (lua_rawequal(L, -1, 2))
1708 {
1709 lua_pushstring(L, "window");
1710 return 1;
1711 }
1712 }
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001713 }
Bram Moolenaar1dced572012-04-05 16:54:08 +02001714 lua_pushstring(L, luaL_typename(L, 1)); /* fallback */
1715 return 1;
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001716}
1717
1718static const luaL_Reg luaV_module[] = {
1719 {"command", luaV_command},
1720 {"eval", luaV_eval},
1721 {"beep", luaV_beep},
1722 {"line", luaV_line},
Bram Moolenaar1dced572012-04-05 16:54:08 +02001723 {"list", luaV_list},
1724 {"dict", luaV_dict},
Bram Moolenaarca06da92018-07-01 15:12:05 +02001725 {"funcref", luaV_funcref},
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001726 {"buffer", luaV_buffer},
1727 {"window", luaV_window},
1728 {"open", luaV_open},
Bram Moolenaar1dced572012-04-05 16:54:08 +02001729 {"type", luaV_type},
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001730 {NULL, NULL}
1731};
1732
Bram Moolenaar1dced572012-04-05 16:54:08 +02001733/* for freeing list, dict, buffer and window objects; lightuserdata as arg */
1734 static int
1735luaV_free(lua_State *L)
1736{
1737 lua_pushnil(L);
1738 luaV_setudata(L, lua_touserdata(L, 1));
1739 return 0;
1740}
1741
1742 static int
1743luaV_luaeval (lua_State *L)
1744{
1745 luaL_Buffer b;
1746 size_t l;
1747 const char *str = lua_tolstring(L, 1, &l);
1748 typval_T *arg = (typval_T *) lua_touserdata(L, 2);
1749 typval_T *rettv = (typval_T *) lua_touserdata(L, 3);
1750 luaL_buffinit(L, &b);
1751 luaL_addlstring(&b, LUAVIM_EVALHEADER, sizeof(LUAVIM_EVALHEADER) - 1);
1752 luaL_addlstring(&b, str, l);
1753 luaL_pushresult(&b);
1754 str = lua_tolstring(L, -1, &l);
1755 if (luaL_loadbuffer(L, str, l, LUAVIM_EVALNAME)) /* compile error? */
1756 {
1757 luaV_emsg(L);
1758 return 0;
1759 }
1760 luaV_pushtypval(L, arg);
1761 if (lua_pcall(L, 1, 1, 0)) /* running error? */
1762 {
1763 luaV_emsg(L);
1764 return 0;
1765 }
Bram Moolenaarca06da92018-07-01 15:12:05 +02001766 if (luaV_totypval(L, -1, rettv) == FAIL)
1767 EMSG("luaeval: cannot convert value");
Bram Moolenaarf554a322015-02-04 23:08:01 +01001768 return 0;
Bram Moolenaar1dced572012-04-05 16:54:08 +02001769}
1770
1771 static int
1772luaV_setref (lua_State *L)
1773{
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01001774 int copyID = lua_tointeger(L, 1);
1775 int abort = FALSE;
1776 typval_T tv;
1777
Bram Moolenaar1dced572012-04-05 16:54:08 +02001778 luaV_getfield(L, LUAVIM_LIST);
1779 luaV_getfield(L, LUAVIM_DICT);
1780 lua_pushnil(L);
Bram Moolenaarb84634d2015-02-04 22:02:37 +01001781 /* traverse cache table */
1782 while (!abort && lua_next(L, lua_upvalueindex(1)) != 0)
Bram Moolenaar1dced572012-04-05 16:54:08 +02001783 {
1784 lua_getmetatable(L, -1);
1785 if (lua_rawequal(L, -1, 2)) /* list? */
1786 {
1787 tv.v_type = VAR_LIST;
1788 tv.vval.v_list = (list_T *) lua_touserdata(L, 4); /* key */
Bram Moolenaarf609dcf2015-12-03 17:43:17 +01001789 abort = set_ref_in_item(&tv, copyID, NULL, NULL);
Bram Moolenaar1dced572012-04-05 16:54:08 +02001790 }
1791 else if (lua_rawequal(L, -1, 3)) /* dict? */
1792 {
1793 tv.v_type = VAR_DICT;
1794 tv.vval.v_dict = (dict_T *) lua_touserdata(L, 4); /* key */
Bram Moolenaarf609dcf2015-12-03 17:43:17 +01001795 abort = set_ref_in_item(&tv, copyID, NULL, NULL);
Bram Moolenaar1dced572012-04-05 16:54:08 +02001796 }
1797 lua_pop(L, 2); /* metatable and value */
Bram Moolenaar1dced572012-04-05 16:54:08 +02001798 }
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01001799 lua_pushinteger(L, abort);
Bram Moolenaarf554a322015-02-04 23:08:01 +01001800 return 1;
Bram Moolenaar1dced572012-04-05 16:54:08 +02001801}
1802
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001803 static int
1804luaopen_vim(lua_State *L)
1805{
Bram Moolenaar1dced572012-04-05 16:54:08 +02001806 /* set cache table */
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001807 lua_newtable(L);
1808 lua_newtable(L);
Bram Moolenaar1dced572012-04-05 16:54:08 +02001809 lua_pushstring(L, "v");
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001810 lua_setfield(L, -2, "__mode");
Bram Moolenaar1dced572012-04-05 16:54:08 +02001811 lua_setmetatable(L, -2); /* cache is weak-valued */
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001812 /* print */
1813 lua_pushcfunction(L, luaV_print);
1814 lua_setglobal(L, "print");
Bram Moolenaar38e2b062011-09-21 17:15:39 +02001815 /* debug.debug */
1816 lua_getglobal(L, "debug");
1817 lua_pushcfunction(L, luaV_debug);
1818 lua_setfield(L, -2, "debug");
1819 lua_pop(L, 1);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001820 /* free */
1821 lua_pushlightuserdata(L, (void *) LUAVIM_FREE);
Bram Moolenaar1dced572012-04-05 16:54:08 +02001822 lua_pushvalue(L, 1); /* cache table */
1823 lua_pushcclosure(L, luaV_free, 1);
1824 lua_rawset(L, LUA_REGISTRYINDEX);
1825 /* luaeval */
1826 lua_pushlightuserdata(L, (void *) LUAVIM_LUAEVAL);
1827 lua_pushvalue(L, 1); /* cache table */
1828 lua_pushcclosure(L, luaV_luaeval, 1);
1829 lua_rawset(L, LUA_REGISTRYINDEX);
1830 /* setref */
1831 lua_pushlightuserdata(L, (void *) LUAVIM_SETREF);
1832 lua_pushvalue(L, 1); /* cache table */
1833 lua_pushcclosure(L, luaV_setref, 1);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001834 lua_rawset(L, LUA_REGISTRYINDEX);
1835 /* register */
Bram Moolenaar1dced572012-04-05 16:54:08 +02001836 luaV_newmetatable(L, LUAVIM_LIST);
1837 lua_pushvalue(L, 1);
1838 luaV_openlib(L, luaV_List_mt, 1);
1839 luaV_newmetatable(L, LUAVIM_DICT);
1840 lua_pushvalue(L, 1);
1841 luaV_openlib(L, luaV_Dict_mt, 1);
Bram Moolenaarca06da92018-07-01 15:12:05 +02001842 luaV_newmetatable(L, LUAVIM_FUNCREF);
1843 lua_pushvalue(L, 1);
1844 luaV_openlib(L, luaV_Funcref_mt, 1);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001845 luaV_newmetatable(L, LUAVIM_BUFFER);
Bram Moolenaar1dced572012-04-05 16:54:08 +02001846 lua_pushvalue(L, 1); /* cache table */
1847 luaV_openlib(L, luaV_Buffer_mt, 1);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001848 luaV_newmetatable(L, LUAVIM_WINDOW);
Bram Moolenaar1dced572012-04-05 16:54:08 +02001849 lua_pushvalue(L, 1); /* cache table */
1850 luaV_openlib(L, luaV_Window_mt, 1);
1851 lua_newtable(L); /* vim table */
1852 lua_pushvalue(L, 1); /* cache table */
1853 luaV_openlib(L, luaV_module, 1);
1854 lua_setglobal(L, LUAVIM_NAME);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001855 return 0;
1856}
1857
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001858 static lua_State *
1859luaV_newstate(void)
1860{
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001861 lua_State *L = luaL_newstate();
Bram Moolenaar16c98f92010-07-28 22:46:08 +02001862 luaL_openlibs(L); /* core libs */
1863 lua_pushcfunction(L, luaopen_vim); /* vim */
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001864 lua_call(L, 0, 0);
1865 return L;
1866}
1867
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001868 static void
1869luaV_setrange(lua_State *L, int line1, int line2)
1870{
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001871 lua_getglobal(L, LUAVIM_NAME);
1872 lua_pushinteger(L, line1);
1873 lua_setfield(L, -2, "firstline");
1874 lua_pushinteger(L, line2);
1875 lua_setfield(L, -2, "lastline");
1876 lua_pop(L, 1); /* vim table */
1877}
1878
1879
1880/* ======= Interface ======= */
1881
1882static lua_State *L = NULL;
1883
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001884 static int
Bram Moolenaar1dced572012-04-05 16:54:08 +02001885lua_isopen(void)
Bram Moolenaar2bd6a1b2010-08-12 22:14:01 +02001886{
1887 return L != NULL;
1888}
1889
1890 static int
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001891lua_init(void)
1892{
Bram Moolenaar1dced572012-04-05 16:54:08 +02001893 if (!lua_isopen())
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001894 {
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001895#ifdef DYNAMIC_LUA
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001896 if (!lua_enabled(TRUE))
1897 {
1898 EMSG(_("Lua library cannot be loaded."));
1899 return FAIL;
1900 }
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001901#endif
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001902 L = luaV_newstate();
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001903 }
1904 return OK;
1905}
1906
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001907 void
1908lua_end(void)
1909{
Bram Moolenaar1dced572012-04-05 16:54:08 +02001910 if (lua_isopen())
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001911 {
1912 lua_close(L);
1913 L = NULL;
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001914#ifdef DYNAMIC_LUA
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001915 end_dynamic_lua();
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001916#endif
1917 }
1918}
1919
1920/* ex commands */
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001921 void
1922ex_lua(exarg_T *eap)
1923{
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001924 char *script;
1925 if (lua_init() == FAIL) return;
1926 script = (char *) script_get(eap, eap->arg);
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001927 if (!eap->skip)
1928 {
1929 char *s = (script) ? script : (char *) eap->arg;
1930 luaV_setrange(L, eap->line1, eap->line2);
1931 if (luaL_loadbuffer(L, s, strlen(s), LUAVIM_CHUNKNAME)
1932 || lua_pcall(L, 0, 0, 0))
1933 luaV_emsg(L);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001934 }
1935 if (script != NULL) vim_free(script);
1936}
1937
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001938 void
1939ex_luado(exarg_T *eap)
1940{
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001941 linenr_T l;
1942 const char *s = (const char *) eap->arg;
1943 luaL_Buffer b;
1944 size_t len;
Bram Moolenaard58f03b2017-01-29 22:48:45 +01001945 buf_T *was_curbuf = curbuf;
1946
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001947 if (lua_init() == FAIL) return;
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001948 if (u_save(eap->line1 - 1, eap->line2 + 1) == FAIL)
1949 {
1950 EMSG(_("cannot save undo information"));
1951 return;
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001952 }
1953 luaV_setrange(L, eap->line1, eap->line2);
1954 luaL_buffinit(L, &b);
Bram Moolenaarbd2f3c32012-04-06 14:31:00 +02001955 luaL_addlstring(&b, "return function(line, linenr) ", 30); /* header */
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001956 luaL_addlstring(&b, s, strlen(s));
1957 luaL_addlstring(&b, " end", 4); /* footer */
1958 luaL_pushresult(&b);
1959 s = lua_tolstring(L, -1, &len);
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001960 if (luaL_loadbuffer(L, s, len, LUAVIM_CHUNKNAME))
1961 {
1962 luaV_emsg(L);
1963 lua_pop(L, 1); /* function body */
1964 return;
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001965 }
1966 lua_call(L, 0, 1);
1967 lua_replace(L, -2); /* function -> body */
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001968 for (l = eap->line1; l <= eap->line2; l++)
1969 {
Bram Moolenaard58f03b2017-01-29 22:48:45 +01001970 /* Check the line number, the command my have deleted lines. */
1971 if (l > curbuf->b_ml.ml_line_count)
1972 break;
1973
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001974 lua_pushvalue(L, -1); /* function */
1975 luaV_pushline(L, curbuf, l); /* current line as arg */
Bram Moolenaarbd2f3c32012-04-06 14:31:00 +02001976 lua_pushinteger(L, l); /* current line number as arg */
1977 if (lua_pcall(L, 2, 1, 0))
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001978 {
1979 luaV_emsg(L);
1980 break;
1981 }
Bram Moolenaard58f03b2017-01-29 22:48:45 +01001982 /* Catch the command switching to another buffer. */
1983 if (curbuf != was_curbuf)
1984 break;
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001985 if (lua_isstring(L, -1)) /* update line? */
1986 {
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001987#ifdef HAVE_SANDBOX
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001988 luaV_checksandbox(L);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001989#endif
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001990 ml_replace(l, luaV_toline(L, -1), TRUE);
1991 changed_bytes(l, 0);
1992 lua_pop(L, 1); /* result from luaV_toline */
1993 }
1994 lua_pop(L, 1); /* line */
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001995 }
1996 lua_pop(L, 1); /* function */
1997 check_cursor();
1998 update_screen(NOT_VALID);
1999}
2000
Bram Moolenaar55d5c032010-07-17 23:52:29 +02002001 void
2002ex_luafile(exarg_T *eap)
2003{
2004 if (lua_init() == FAIL)
2005 return;
2006 if (!eap->skip)
2007 {
2008 luaV_setrange(L, eap->line1, eap->line2);
2009 if (luaL_loadfile(L, (char *) eap->arg) || lua_pcall(L, 0, 0, 0))
2010 luaV_emsg(L);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02002011 }
2012}
2013
Bram Moolenaar1dced572012-04-05 16:54:08 +02002014#define luaV_freetype(typ,tname) \
2015 void \
2016 lua_##tname##_free(typ *o) \
2017 { \
2018 if (!lua_isopen()) return; \
2019 luaV_getfield(L, LUAVIM_FREE); \
2020 lua_pushlightuserdata(L, (void *) o); \
2021 lua_call(L, 1, 0); \
2022 }
2023
2024luaV_freetype(buf_T, buffer)
2025luaV_freetype(win_T, window)
2026
Bram Moolenaar55d5c032010-07-17 23:52:29 +02002027 void
Bram Moolenaar1dced572012-04-05 16:54:08 +02002028do_luaeval (char_u *str, typval_T *arg, typval_T *rettv)
Bram Moolenaar55d5c032010-07-17 23:52:29 +02002029{
Bram Moolenaar1dced572012-04-05 16:54:08 +02002030 lua_init();
2031 luaV_getfield(L, LUAVIM_LUAEVAL);
2032 lua_pushstring(L, (char *) str);
2033 lua_pushlightuserdata(L, (void *) arg);
2034 lua_pushlightuserdata(L, (void *) rettv);
2035 lua_call(L, 3, 0);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02002036}
2037
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01002038 int
Bram Moolenaar1dced572012-04-05 16:54:08 +02002039set_ref_in_lua (int copyID)
Bram Moolenaar55d5c032010-07-17 23:52:29 +02002040{
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01002041 int aborted = 0;
2042
2043 if (lua_isopen())
2044 {
2045 luaV_getfield(L, LUAVIM_SETREF);
2046 /* call the function with 1 arg, getting 1 result back */
2047 lua_pushinteger(L, copyID);
2048 lua_call(L, 1, 1);
2049 /* get the result */
2050 aborted = lua_tointeger(L, -1);
2051 /* pop result off the stack */
2052 lua_pop(L, 1);
2053 }
2054 return aborted;
Bram Moolenaar0ba04292010-07-14 23:23:17 +02002055}
2056
2057#endif