blob: 7602c80de48a62a514c8ef5213bf4750ead24a39 [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);
Bram Moolenaar17413672018-07-14 20:49:42 +0200256#if LUA_VERSION_NUM <= 502
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200257void (*dll_lua_rawget) (lua_State *L, int idx);
Bram Moolenaar1dced572012-04-05 16:54:08 +0200258void (*dll_lua_rawgeti) (lua_State *L, int idx, int n);
Bram Moolenaar17413672018-07-14 20:49:42 +0200259#else
260int (*dll_lua_rawget) (lua_State *L, int idx);
261int (*dll_lua_rawgeti) (lua_State *L, int idx, lua_Integer n);
262#endif
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200263void (*dll_lua_createtable) (lua_State *L, int narr, int nrec);
264void *(*dll_lua_newuserdata) (lua_State *L, size_t sz);
265int (*dll_lua_getmetatable) (lua_State *L, int objindex);
266void (*dll_lua_setfield) (lua_State *L, int idx, const char *k);
267void (*dll_lua_rawset) (lua_State *L, int idx);
Bram Moolenaar17413672018-07-14 20:49:42 +0200268#if LUA_VERSION_NUM <= 502
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200269void (*dll_lua_rawseti) (lua_State *L, int idx, int n);
Bram Moolenaar17413672018-07-14 20:49:42 +0200270#else
271void (*dll_lua_rawseti) (lua_State *L, int idx, lua_Integer n);
272#endif
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200273int (*dll_lua_setmetatable) (lua_State *L, int objindex);
Bram Moolenaarbd2f3c32012-04-06 14:31:00 +0200274int (*dll_lua_next) (lua_State *L, int idx);
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200275/* libs */
276int (*dll_luaopen_base) (lua_State *L);
277int (*dll_luaopen_table) (lua_State *L);
278int (*dll_luaopen_string) (lua_State *L);
279int (*dll_luaopen_math) (lua_State *L);
Bram Moolenaar16c98f92010-07-28 22:46:08 +0200280int (*dll_luaopen_io) (lua_State *L);
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200281int (*dll_luaopen_os) (lua_State *L);
282int (*dll_luaopen_package) (lua_State *L);
283int (*dll_luaopen_debug) (lua_State *L);
Bram Moolenaar16c98f92010-07-28 22:46:08 +0200284void (*dll_luaL_openlibs) (lua_State *L);
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200285
286typedef void **luaV_function;
287typedef struct {
288 const char *name;
289 luaV_function func;
290} luaV_Reg;
291
292static const luaV_Reg luaV_dll[] = {
293 /* lauxlib */
Bram Moolenaar1dced572012-04-05 16:54:08 +0200294#if LUA_VERSION_NUM <= 501
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200295 {"luaL_register", (luaV_function) &dll_luaL_register},
Bram Moolenaar1dced572012-04-05 16:54:08 +0200296 {"luaL_prepbuffer", (luaV_function) &dll_luaL_prepbuffer},
297 {"luaL_openlib", (luaV_function) &dll_luaL_openlib},
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200298 {"luaL_typerror", (luaV_function) &dll_luaL_typerror},
Bram Moolenaar1dced572012-04-05 16:54:08 +0200299 {"luaL_loadfile", (luaV_function) &dll_luaL_loadfile},
300 {"luaL_loadbuffer", (luaV_function) &dll_luaL_loadbuffer},
301#else
302 {"luaL_prepbuffsize", (luaV_function) &dll_luaL_prepbuffsize},
303 {"luaL_setfuncs", (luaV_function) &dll_luaL_setfuncs},
304 {"luaL_loadfilex", (luaV_function) &dll_luaL_loadfilex},
305 {"luaL_loadbufferx", (luaV_function) &dll_luaL_loadbufferx},
306 {"luaL_argerror", (luaV_function) &dll_luaL_argerror},
307#endif
Bram Moolenaarbd2f3c32012-04-06 14:31:00 +0200308 {"luaL_checkany", (luaV_function) &dll_luaL_checkany},
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200309 {"luaL_checklstring", (luaV_function) &dll_luaL_checklstring},
310 {"luaL_checkinteger", (luaV_function) &dll_luaL_checkinteger},
311 {"luaL_optinteger", (luaV_function) &dll_luaL_optinteger},
312 {"luaL_checktype", (luaV_function) &dll_luaL_checktype},
313 {"luaL_error", (luaV_function) &dll_luaL_error},
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200314 {"luaL_newstate", (luaV_function) &dll_luaL_newstate},
315 {"luaL_buffinit", (luaV_function) &dll_luaL_buffinit},
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200316 {"luaL_addlstring", (luaV_function) &dll_luaL_addlstring},
317 {"luaL_pushresult", (luaV_function) &dll_luaL_pushresult},
318 /* lua */
Bram Moolenaar1dced572012-04-05 16:54:08 +0200319#if LUA_VERSION_NUM <= 501
320 {"lua_tonumber", (luaV_function) &dll_lua_tonumber},
321 {"lua_tointeger", (luaV_function) &dll_lua_tointeger},
322 {"lua_call", (luaV_function) &dll_lua_call},
323 {"lua_pcall", (luaV_function) &dll_lua_pcall},
324#else
325 {"lua_tonumberx", (luaV_function) &dll_lua_tonumberx},
326 {"lua_tointegerx", (luaV_function) &dll_lua_tointegerx},
327 {"lua_callk", (luaV_function) &dll_lua_callk},
328 {"lua_pcallk", (luaV_function) &dll_lua_pcallk},
329 {"lua_getglobal", (luaV_function) &dll_lua_getglobal},
330 {"lua_setglobal", (luaV_function) &dll_lua_setglobal},
Bram Moolenaar1dced572012-04-05 16:54:08 +0200331#endif
Bram Moolenaar1f860d82015-06-27 18:36:16 +0200332#if LUA_VERSION_NUM <= 502
333 {"lua_replace", (luaV_function) &dll_lua_replace},
334 {"lua_remove", (luaV_function) &dll_lua_remove},
335#endif
336#if LUA_VERSION_NUM >= 503
337 {"lua_rotate", (luaV_function) &dll_lua_rotate},
338 {"lua_copy", (luaV_function) &dll_lua_copy},
339#endif
Bram Moolenaarbd2f3c32012-04-06 14:31:00 +0200340 {"lua_typename", (luaV_function) &dll_lua_typename},
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200341 {"lua_close", (luaV_function) &dll_lua_close},
342 {"lua_gettop", (luaV_function) &dll_lua_gettop},
343 {"lua_settop", (luaV_function) &dll_lua_settop},
344 {"lua_pushvalue", (luaV_function) &dll_lua_pushvalue},
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200345 {"lua_isnumber", (luaV_function) &dll_lua_isnumber},
346 {"lua_isstring", (luaV_function) &dll_lua_isstring},
347 {"lua_type", (luaV_function) &dll_lua_type},
348 {"lua_rawequal", (luaV_function) &dll_lua_rawequal},
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200349 {"lua_toboolean", (luaV_function) &dll_lua_toboolean},
350 {"lua_tolstring", (luaV_function) &dll_lua_tolstring},
351 {"lua_touserdata", (luaV_function) &dll_lua_touserdata},
352 {"lua_pushnil", (luaV_function) &dll_lua_pushnil},
353 {"lua_pushnumber", (luaV_function) &dll_lua_pushnumber},
354 {"lua_pushinteger", (luaV_function) &dll_lua_pushinteger},
355 {"lua_pushlstring", (luaV_function) &dll_lua_pushlstring},
356 {"lua_pushstring", (luaV_function) &dll_lua_pushstring},
357 {"lua_pushfstring", (luaV_function) &dll_lua_pushfstring},
358 {"lua_pushcclosure", (luaV_function) &dll_lua_pushcclosure},
359 {"lua_pushboolean", (luaV_function) &dll_lua_pushboolean},
360 {"lua_pushlightuserdata", (luaV_function) &dll_lua_pushlightuserdata},
361 {"lua_getfield", (luaV_function) &dll_lua_getfield},
362 {"lua_rawget", (luaV_function) &dll_lua_rawget},
Bram Moolenaar1dced572012-04-05 16:54:08 +0200363 {"lua_rawgeti", (luaV_function) &dll_lua_rawgeti},
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200364 {"lua_createtable", (luaV_function) &dll_lua_createtable},
365 {"lua_newuserdata", (luaV_function) &dll_lua_newuserdata},
366 {"lua_getmetatable", (luaV_function) &dll_lua_getmetatable},
367 {"lua_setfield", (luaV_function) &dll_lua_setfield},
368 {"lua_rawset", (luaV_function) &dll_lua_rawset},
369 {"lua_rawseti", (luaV_function) &dll_lua_rawseti},
370 {"lua_setmetatable", (luaV_function) &dll_lua_setmetatable},
Bram Moolenaarbd2f3c32012-04-06 14:31:00 +0200371 {"lua_next", (luaV_function) &dll_lua_next},
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200372 /* libs */
373 {"luaopen_base", (luaV_function) &dll_luaopen_base},
374 {"luaopen_table", (luaV_function) &dll_luaopen_table},
375 {"luaopen_string", (luaV_function) &dll_luaopen_string},
376 {"luaopen_math", (luaV_function) &dll_luaopen_math},
Bram Moolenaar16c98f92010-07-28 22:46:08 +0200377 {"luaopen_io", (luaV_function) &dll_luaopen_io},
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200378 {"luaopen_os", (luaV_function) &dll_luaopen_os},
379 {"luaopen_package", (luaV_function) &dll_luaopen_package},
380 {"luaopen_debug", (luaV_function) &dll_luaopen_debug},
Bram Moolenaar16c98f92010-07-28 22:46:08 +0200381 {"luaL_openlibs", (luaV_function) &dll_luaL_openlibs},
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200382 {NULL, NULL}
383};
384
Bram Moolenaar2334b6d2010-07-22 21:32:16 +0200385static HANDLE hinstLua = NULL;
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200386
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200387 static void
388end_dynamic_lua(void)
389{
390 if (hinstLua)
391 {
Bram Moolenaar2334b6d2010-07-22 21:32:16 +0200392 close_dll(hinstLua);
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200393 hinstLua = 0;
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200394 }
395}
396
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200397 static int
398lua_link_init(char *libname, int verbose)
399{
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200400 const luaV_Reg *reg;
401 if (hinstLua) return OK;
Bram Moolenaar2334b6d2010-07-22 21:32:16 +0200402 hinstLua = load_dll(libname);
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200403 if (!hinstLua)
404 {
405 if (verbose)
406 EMSG2(_(e_loadlib), libname);
407 return FAIL;
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200408 }
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200409 for (reg = luaV_dll; reg->func; reg++)
410 {
Bram Moolenaar2334b6d2010-07-22 21:32:16 +0200411 if ((*reg->func = symbol_from_dll(hinstLua, reg->name)) == NULL)
412 {
413 close_dll(hinstLua);
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200414 hinstLua = 0;
415 if (verbose)
416 EMSG2(_(e_loadfunc), reg->name);
417 return FAIL;
418 }
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200419 }
420 return OK;
421}
Bram Moolenaard90b6c02016-08-28 18:10:45 +0200422#endif /* DYNAMIC_LUA */
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200423
Bram Moolenaard90b6c02016-08-28 18:10:45 +0200424#if defined(DYNAMIC_LUA) || defined(PROTO)
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200425 int
426lua_enabled(int verbose)
427{
Bram Moolenaar25e4fcd2016-01-09 14:57:47 +0100428 return lua_link_init((char *)p_luadll, verbose) == OK;
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200429}
Bram Moolenaard90b6c02016-08-28 18:10:45 +0200430#endif
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200431
Bram Moolenaar1dced572012-04-05 16:54:08 +0200432#if LUA_VERSION_NUM > 501
433 static int
434luaL_typeerror (lua_State *L, int narg, const char *tname)
435{
436 const char *msg = lua_pushfstring(L, "%s expected, got %s",
Bram Moolenaardb913952012-06-29 12:54:53 +0200437 tname, luaL_typename(L, narg));
Bram Moolenaar1dced572012-04-05 16:54:08 +0200438 return luaL_argerror(L, narg, msg);
439}
440#endif
441
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200442
443/* ======= Internal ======= */
444
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200445 static void
446luaV_newmetatable(lua_State *L, const char *tname)
447{
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200448 lua_newtable(L);
449 lua_pushlightuserdata(L, (void *) tname);
450 lua_pushvalue(L, -2);
451 lua_rawset(L, LUA_REGISTRYINDEX);
452}
453
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200454 static void *
455luaV_toudata(lua_State *L, int ud, const char *tname)
456{
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200457 void *p = lua_touserdata(L, ud);
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200458
459 if (p != NULL) /* value is userdata? */
460 {
461 if (lua_getmetatable(L, ud)) /* does it have a metatable? */
462 {
463 luaV_getfield(L, tname); /* get metatable */
464 if (lua_rawequal(L, -1, -2)) /* MTs match? */
465 {
466 lua_pop(L, 2); /* MTs */
467 return p;
468 }
469 }
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200470 }
471 return NULL;
472}
473
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200474 static void *
Bram Moolenaar1dced572012-04-05 16:54:08 +0200475luaV_checkcache(lua_State *L, void *p)
476{
477 luaV_getudata(L, p);
478 if (lua_isnil(L, -1)) luaL_error(L, "invalid object");
479 lua_pop(L, 1);
480 return p;
481}
482
483#define luaV_unbox(L,luatyp,ud) (*((luatyp *) lua_touserdata((L),(ud))))
484
485#define luaV_checkvalid(L,luatyp,ud) \
486 luaV_checkcache((L), (void *) luaV_unbox((L),luatyp,(ud)))
487
488 static void *
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200489luaV_checkudata(lua_State *L, int ud, const char *tname)
490{
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200491 void *p = luaV_toudata(L, ud, tname);
Bram Moolenaar1dced572012-04-05 16:54:08 +0200492 if (p == NULL) luaL_typeerror(L, ud, tname);
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200493 return p;
494}
495
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200496 static void
497luaV_pushtypval(lua_State *L, typval_T *tv)
498{
Bram Moolenaar1dced572012-04-05 16:54:08 +0200499 if (tv == NULL)
500 {
501 lua_pushnil(L);
502 return;
503 }
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200504 switch (tv->v_type)
505 {
506 case VAR_STRING:
Bram Moolenaard04da7c2012-10-14 03:41:59 +0200507 lua_pushstring(L, tv->vval.v_string == NULL
508 ? "" : (char *)tv->vval.v_string);
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200509 break;
510 case VAR_NUMBER:
511 lua_pushinteger(L, (int) tv->vval.v_number);
512 break;
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200513#ifdef FEAT_FLOAT
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200514 case VAR_FLOAT:
515 lua_pushnumber(L, (lua_Number) tv->vval.v_float);
516 break;
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200517#endif
Bram Moolenaar1dced572012-04-05 16:54:08 +0200518 case VAR_LIST:
519 luaV_pushlist(L, tv->vval.v_list);
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200520 break;
Bram Moolenaar1dced572012-04-05 16:54:08 +0200521 case VAR_DICT:
522 luaV_pushdict(L, tv->vval.v_dict);
523 break;
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100524 case VAR_SPECIAL:
525 if (tv->vval.v_number <= VVAL_TRUE)
526 lua_pushinteger(L, (int) tv->vval.v_number);
527 else
528 lua_pushnil(L);
529 break;
Bram Moolenaarca06da92018-07-01 15:12:05 +0200530 case VAR_FUNC:
531 luaV_pushfuncref(L, tv);
532 break;
Bram Moolenaar1dced572012-04-05 16:54:08 +0200533 default:
534 lua_pushnil(L);
535 }
536}
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200537
Bram Moolenaarca06da92018-07-01 15:12:05 +0200538/*
539 * Converts lua value at 'pos' to typval 'tv'.
540 * Returns OK or FAIL.
541 */
542 static int
543luaV_totypval(lua_State *L, int pos, typval_T *tv)
Bram Moolenaar1dced572012-04-05 16:54:08 +0200544{
Bram Moolenaarca06da92018-07-01 15:12:05 +0200545 int status = OK;
546
547 switch (lua_type(L, pos))
548 {
Bram Moolenaar1dced572012-04-05 16:54:08 +0200549 case LUA_TBOOLEAN:
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100550 tv->v_type = VAR_SPECIAL;
Bram Moolenaar1dced572012-04-05 16:54:08 +0200551 tv->vval.v_number = (varnumber_T) lua_toboolean(L, pos);
552 break;
553 case LUA_TSTRING:
554 tv->v_type = VAR_STRING;
555 tv->vval.v_string = vim_strsave((char_u *) lua_tostring(L, pos));
556 break;
557 case LUA_TNUMBER:
558#ifdef FEAT_FLOAT
559 tv->v_type = VAR_FLOAT;
560 tv->vval.v_float = (float_T) lua_tonumber(L, pos);
561#else
562 tv->v_type = VAR_NUMBER;
563 tv->vval.v_number = (varnumber_T) lua_tointeger(L, pos);
564#endif
565 break;
Bram Moolenaarca06da92018-07-01 15:12:05 +0200566 case LUA_TUSERDATA:
567 {
Bram Moolenaar1dced572012-04-05 16:54:08 +0200568 void *p = lua_touserdata(L, pos);
Bram Moolenaarca06da92018-07-01 15:12:05 +0200569
Bram Moolenaar1dced572012-04-05 16:54:08 +0200570 if (lua_getmetatable(L, pos)) /* has metatable? */
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200571 {
Bram Moolenaar1dced572012-04-05 16:54:08 +0200572 /* check list */
573 luaV_getfield(L, LUAVIM_LIST);
574 if (lua_rawequal(L, -1, -2))
Bram Moolenaar2334b6d2010-07-22 21:32:16 +0200575 {
Bram Moolenaar1dced572012-04-05 16:54:08 +0200576 tv->v_type = VAR_LIST;
577 tv->vval.v_list = *((luaV_List *) p);
578 ++tv->vval.v_list->lv_refcount;
579 lua_pop(L, 2); /* MTs */
Bram Moolenaarca06da92018-07-01 15:12:05 +0200580 break;
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200581 }
Bram Moolenaar1dced572012-04-05 16:54:08 +0200582 /* check dict */
583 luaV_getfield(L, LUAVIM_DICT);
584 if (lua_rawequal(L, -1, -3))
585 {
586 tv->v_type = VAR_DICT;
587 tv->vval.v_dict = *((luaV_Dict *) p);
588 ++tv->vval.v_dict->dv_refcount;
589 lua_pop(L, 3); /* MTs */
Bram Moolenaarca06da92018-07-01 15:12:05 +0200590 break;
Bram Moolenaar1dced572012-04-05 16:54:08 +0200591 }
Bram Moolenaarca06da92018-07-01 15:12:05 +0200592 /* check funcref */
593 luaV_getfield(L, LUAVIM_FUNCREF);
594 if (lua_rawequal(L, -1, -4))
595 {
596 luaV_Funcref *f = (luaV_Funcref *) p;
597 copy_tv(&f->tv, tv);
598 lua_pop(L, 4); /* MTs */
599 break;
600 }
601 lua_pop(L, 4); /* MTs */
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200602 }
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200603 }
Bram Moolenaarca06da92018-07-01 15:12:05 +0200604 /* FALLTHROUGH */
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200605 default:
Bram Moolenaar1dced572012-04-05 16:54:08 +0200606 tv->v_type = VAR_NUMBER;
607 tv->vval.v_number = 0;
Bram Moolenaarca06da92018-07-01 15:12:05 +0200608 status = FAIL;
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200609 }
Bram Moolenaarca06da92018-07-01 15:12:05 +0200610 return status;
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200611}
612
613/* similar to luaL_addlstring, but replaces \0 with \n if toline and
614 * \n with \0 otherwise */
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200615 static void
616luaV_addlstring(luaL_Buffer *b, const char *s, size_t l, int toline)
617{
618 while (l--)
619 {
620 if (*s == '\0' && toline)
621 luaL_addchar(b, '\n');
622 else if (*s == '\n' && !toline)
623 luaL_addchar(b, '\0');
624 else
625 luaL_addchar(b, *s);
626 s++;
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200627 }
628}
629
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200630 static void
631luaV_pushline(lua_State *L, buf_T *buf, linenr_T n)
632{
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200633 const char *s = (const char *) ml_get_buf(buf, n, FALSE);
634 luaL_Buffer b;
635 luaL_buffinit(L, &b);
636 luaV_addlstring(&b, s, strlen(s), 0);
637 luaL_pushresult(&b);
638}
639
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200640 static char_u *
641luaV_toline(lua_State *L, int pos)
642{
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200643 size_t l;
644 const char *s = lua_tolstring(L, pos, &l);
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200645
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200646 luaL_Buffer b;
647 luaL_buffinit(L, &b);
648 luaV_addlstring(&b, s, l, 1);
649 luaL_pushresult(&b);
650 return (char_u *) lua_tostring(L, -1);
651}
652
653/* pops a string s from the top of the stack and calls mf(t) for pieces t of
654 * s separated by newlines */
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200655 static void
656luaV_msgfunc(lua_State *L, msgfunc_T mf)
657{
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200658 luaL_Buffer b;
659 size_t l;
660 const char *p, *s = lua_tolstring(L, -1, &l);
661 luaL_buffinit(L, &b);
662 luaV_addlstring(&b, s, l, 0);
663 luaL_pushresult(&b);
664 /* break string */
665 p = s = lua_tolstring(L, -1, &l);
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200666 while (l--)
667 {
668 if (*p++ == '\0') /* break? */
669 {
670 mf((char_u *) s);
671 s = p;
672 }
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200673 }
674 mf((char_u *) s);
675 lua_pop(L, 2); /* original and modified strings */
676}
677
Bram Moolenaar1dced572012-04-05 16:54:08 +0200678#define luaV_newtype(typ,tname,luatyp,luatname) \
679 static luatyp * \
680 luaV_new##tname (lua_State *L, typ *obj) \
681 { \
682 luatyp *o = (luatyp *) lua_newuserdata(L, sizeof(luatyp)); \
683 *o = obj; \
684 luaV_setudata(L, obj); /* cache[obj] = udata */ \
685 luaV_getfield(L, luatname); \
686 lua_setmetatable(L, -2); \
687 return o; \
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200688 }
Bram Moolenaar1dced572012-04-05 16:54:08 +0200689
690#define luaV_pushtype(typ,tname,luatyp) \
691 static luatyp * \
Bram Moolenaarca06da92018-07-01 15:12:05 +0200692 luaV_push##tname(lua_State *L, typ *obj) \
Bram Moolenaar1dced572012-04-05 16:54:08 +0200693 { \
694 luatyp *o = NULL; \
695 if (obj == NULL) \
696 lua_pushnil(L); \
697 else { \
698 luaV_getudata(L, obj); \
699 if (lua_isnil(L, -1)) /* not interned? */ \
700 { \
701 lua_pop(L, 1); \
702 o = luaV_new##tname(L, obj); \
703 } \
704 else \
705 o = (luatyp *) lua_touserdata(L, -1); \
706 } \
707 return o; \
708 }
709
710#define luaV_type_tostring(tname,luatname) \
711 static int \
712 luaV_##tname##_tostring (lua_State *L) \
713 { \
714 lua_pushfstring(L, "%s: %p", luatname, lua_touserdata(L, 1)); \
715 return 1; \
716 }
717
Bram Moolenaar1dced572012-04-05 16:54:08 +0200718/* ======= List type ======= */
719
720 static luaV_List *
721luaV_newlist (lua_State *L, list_T *lis)
722{
723 luaV_List *l = (luaV_List *) lua_newuserdata(L, sizeof(luaV_List));
724 *l = lis;
725 lis->lv_refcount++; /* reference in Lua */
726 luaV_setudata(L, lis); /* cache[lis] = udata */
727 luaV_getfield(L, LUAVIM_LIST);
728 lua_setmetatable(L, -2);
729 return l;
730}
731
732luaV_pushtype(list_T, list, luaV_List)
733luaV_type_tostring(list, LUAVIM_LIST)
734
735 static int
Bram Moolenaar1dced572012-04-05 16:54:08 +0200736luaV_list_len (lua_State *L)
737{
738 list_T *l = luaV_unbox(L, luaV_List, 1);
739 lua_pushinteger(L, (l == NULL) ? 0 : (int) l->lv_len);
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200740 return 1;
741}
742
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200743 static int
Bram Moolenaar1dced572012-04-05 16:54:08 +0200744luaV_list_iter (lua_State *L)
745{
746 listitem_T *li = (listitem_T *) lua_touserdata(L, lua_upvalueindex(2));
747 if (li == NULL) return 0;
748 luaV_pushtypval(L, &li->li_tv);
749 lua_pushlightuserdata(L, (void *) li->li_next);
750 lua_replace(L, lua_upvalueindex(2));
751 return 1;
752}
753
754 static int
755luaV_list_call (lua_State *L)
756{
757 list_T *l = luaV_unbox(L, luaV_List, 1);
758 lua_pushvalue(L, lua_upvalueindex(1)); /* pass cache table along */
759 lua_pushlightuserdata(L, (void *) l->lv_first);
760 lua_pushcclosure(L, luaV_list_iter, 2);
761 return 1;
762}
763
764 static int
765luaV_list_index (lua_State *L)
766{
767 list_T *l = luaV_unbox(L, luaV_List, 1);
768 if (lua_isnumber(L, 2)) /* list item? */
769 {
770 listitem_T *li = list_find(l, (long) luaL_checkinteger(L, 2));
771 if (li == NULL)
772 lua_pushnil(L);
773 else
774 luaV_pushtypval(L, &li->li_tv);
775 }
776 else if (lua_isstring(L, 2)) /* method? */
777 {
778 const char *s = lua_tostring(L, 2);
779 if (strncmp(s, "add", 3) == 0
Bram Moolenaarb3766472013-04-15 13:49:21 +0200780 || strncmp(s, "insert", 6) == 0)
Bram Moolenaar1dced572012-04-05 16:54:08 +0200781 {
782 lua_getmetatable(L, 1);
783 lua_getfield(L, -1, s);
784 }
785 else
786 lua_pushnil(L);
787 }
788 else
789 lua_pushnil(L);
790 return 1;
791}
792
793 static int
794luaV_list_newindex (lua_State *L)
795{
796 list_T *l = luaV_unbox(L, luaV_List, 1);
797 long n = (long) luaL_checkinteger(L, 2);
798 listitem_T *li;
799 if (l->lv_lock)
800 luaL_error(L, "list is locked");
801 li = list_find(l, n);
802 if (li == NULL) return 0;
803 if (lua_isnil(L, 3)) /* remove? */
804 {
Bram Moolenaar3ec7f4e2014-05-07 17:31:37 +0200805 vimlist_remove(l, li, li);
Bram Moolenaar1dced572012-04-05 16:54:08 +0200806 clear_tv(&li->li_tv);
807 vim_free(li);
808 }
809 else
810 {
811 typval_T v;
Bram Moolenaarca06da92018-07-01 15:12:05 +0200812 luaV_checktypval(L, 3, &v, "setting list item");
Bram Moolenaar1dced572012-04-05 16:54:08 +0200813 clear_tv(&li->li_tv);
814 copy_tv(&v, &li->li_tv);
Bram Moolenaarb3766472013-04-15 13:49:21 +0200815 clear_tv(&v);
Bram Moolenaar1dced572012-04-05 16:54:08 +0200816 }
817 return 0;
818}
819
820 static int
821luaV_list_add (lua_State *L)
822{
823 luaV_List *lis = luaV_checkudata(L, 1, LUAVIM_LIST);
824 list_T *l = (list_T *) luaV_checkcache(L, (void *) *lis);
Bram Moolenaarb3766472013-04-15 13:49:21 +0200825 typval_T v;
Bram Moolenaar1dced572012-04-05 16:54:08 +0200826 if (l->lv_lock)
827 luaL_error(L, "list is locked");
Bram Moolenaarb3766472013-04-15 13:49:21 +0200828 lua_settop(L, 2);
Bram Moolenaarca06da92018-07-01 15:12:05 +0200829 luaV_checktypval(L, 2, &v, "adding list item");
Bram Moolenaarb3766472013-04-15 13:49:21 +0200830 if (list_append_tv(l, &v) == FAIL)
Bram Moolenaar1dced572012-04-05 16:54:08 +0200831 {
Bram Moolenaarb3766472013-04-15 13:49:21 +0200832 clear_tv(&v);
Bram Moolenaarca06da92018-07-01 15:12:05 +0200833 luaL_error(L, "failed to add item to list");
Bram Moolenaar1dced572012-04-05 16:54:08 +0200834 }
Bram Moolenaarb3766472013-04-15 13:49:21 +0200835 clear_tv(&v);
Bram Moolenaar1dced572012-04-05 16:54:08 +0200836 lua_settop(L, 1);
837 return 1;
838}
839
840 static int
841luaV_list_insert (lua_State *L)
842{
843 luaV_List *lis = luaV_checkudata(L, 1, LUAVIM_LIST);
844 list_T *l = (list_T *) luaV_checkcache(L, (void *) *lis);
Bram Moolenaar46538ee2015-02-17 16:28:55 +0100845 long pos = (long) luaL_optinteger(L, 3, 0);
Bram Moolenaar1dced572012-04-05 16:54:08 +0200846 listitem_T *li = NULL;
847 typval_T v;
848 if (l->lv_lock)
849 luaL_error(L, "list is locked");
850 if (pos < l->lv_len)
851 {
852 li = list_find(l, pos);
853 if (li == NULL)
854 luaL_error(L, "invalid position");
855 }
856 lua_settop(L, 2);
Bram Moolenaarca06da92018-07-01 15:12:05 +0200857 luaV_checktypval(L, 2, &v, "inserting list item");
Bram Moolenaarb3766472013-04-15 13:49:21 +0200858 if (list_insert_tv(l, &v, li) == FAIL)
859 {
860 clear_tv(&v);
Bram Moolenaarca06da92018-07-01 15:12:05 +0200861 luaL_error(L, "failed to add item to list");
Bram Moolenaarb3766472013-04-15 13:49:21 +0200862 }
863 clear_tv(&v);
Bram Moolenaar1dced572012-04-05 16:54:08 +0200864 lua_settop(L, 1);
865 return 1;
866}
867
868static const luaL_Reg luaV_List_mt[] = {
869 {"__tostring", luaV_list_tostring},
Bram Moolenaar1dced572012-04-05 16:54:08 +0200870 {"__len", luaV_list_len},
871 {"__call", luaV_list_call},
872 {"__index", luaV_list_index},
873 {"__newindex", luaV_list_newindex},
874 {"add", luaV_list_add},
875 {"insert", luaV_list_insert},
876 {NULL, NULL}
877};
878
879
880/* ======= Dict type ======= */
881
882 static luaV_Dict *
883luaV_newdict (lua_State *L, dict_T *dic)
884{
885 luaV_Dict *d = (luaV_Dict *) lua_newuserdata(L, sizeof(luaV_Dict));
886 *d = dic;
887 dic->dv_refcount++; /* reference in Lua */
888 luaV_setudata(L, dic); /* cache[dic] = udata */
889 luaV_getfield(L, LUAVIM_DICT);
890 lua_setmetatable(L, -2);
891 return d;
892}
893
894luaV_pushtype(dict_T, dict, luaV_Dict)
895luaV_type_tostring(dict, LUAVIM_DICT)
896
897 static int
Bram Moolenaar1dced572012-04-05 16:54:08 +0200898luaV_dict_len (lua_State *L)
899{
900 dict_T *d = luaV_unbox(L, luaV_Dict, 1);
901 lua_pushinteger(L, (d == NULL) ? 0 : (int) d->dv_hashtab.ht_used);
902 return 1;
903}
904
905 static int
Bram Moolenaarfeeaa682013-02-14 22:19:51 +0100906luaV_dict_iter (lua_State *L UNUSED)
Bram Moolenaar1dced572012-04-05 16:54:08 +0200907{
Bram Moolenaarfeeaa682013-02-14 22:19:51 +0100908#ifdef FEAT_EVAL
Bram Moolenaar1dced572012-04-05 16:54:08 +0200909 hashitem_T *hi = (hashitem_T *) lua_touserdata(L, lua_upvalueindex(2));
910 int n = lua_tointeger(L, lua_upvalueindex(3));
911 dictitem_T *di;
912 if (n <= 0) return 0;
913 while (HASHITEM_EMPTY(hi)) hi++;
914 di = dict_lookup(hi);
915 lua_pushstring(L, (char *) hi->hi_key);
916 luaV_pushtypval(L, &di->di_tv);
917 lua_pushlightuserdata(L, (void *) (hi + 1));
918 lua_replace(L, lua_upvalueindex(2));
919 lua_pushinteger(L, n - 1);
920 lua_replace(L, lua_upvalueindex(3));
921 return 2;
Bram Moolenaarfeeaa682013-02-14 22:19:51 +0100922#else
923 return 0;
924#endif
Bram Moolenaar1dced572012-04-05 16:54:08 +0200925}
926
927 static int
928luaV_dict_call (lua_State *L)
929{
930 dict_T *d = luaV_unbox(L, luaV_Dict, 1);
931 hashtab_T *ht = &d->dv_hashtab;
932 lua_pushvalue(L, lua_upvalueindex(1)); /* pass cache table along */
933 lua_pushlightuserdata(L, (void *) ht->ht_array);
934 lua_pushinteger(L, ht->ht_used); /* # remaining items */
935 lua_pushcclosure(L, luaV_dict_iter, 3);
936 return 1;
937}
938
939 static int
Bram Moolenaarca06da92018-07-01 15:12:05 +0200940luaV_dict_index(lua_State *L)
Bram Moolenaar1dced572012-04-05 16:54:08 +0200941{
942 dict_T *d = luaV_unbox(L, luaV_Dict, 1);
943 char_u *key = (char_u *) luaL_checkstring(L, 2);
944 dictitem_T *di = dict_find(d, key, -1);
Bram Moolenaarca06da92018-07-01 15:12:05 +0200945
Bram Moolenaar1dced572012-04-05 16:54:08 +0200946 if (di == NULL)
947 lua_pushnil(L);
948 else
Bram Moolenaarca06da92018-07-01 15:12:05 +0200949 {
Bram Moolenaar1dced572012-04-05 16:54:08 +0200950 luaV_pushtypval(L, &di->di_tv);
Bram Moolenaarca06da92018-07-01 15:12:05 +0200951 if (di->di_tv.v_type == VAR_FUNC) /* funcref? */
952 {
953 luaV_Funcref *f = (luaV_Funcref *) lua_touserdata(L, -1);
954 f->self = d; /* keep "self" reference */
955 d->dv_refcount++;
956 }
957 }
Bram Moolenaar1dced572012-04-05 16:54:08 +0200958 return 1;
959}
960
961 static int
Bram Moolenaarca06da92018-07-01 15:12:05 +0200962luaV_dict_newindex(lua_State *L)
Bram Moolenaar1dced572012-04-05 16:54:08 +0200963{
964 dict_T *d = luaV_unbox(L, luaV_Dict, 1);
965 char_u *key = (char_u *) luaL_checkstring(L, 2);
966 dictitem_T *di;
Bram Moolenaarca06da92018-07-01 15:12:05 +0200967 typval_T v;
Bram Moolenaar1dced572012-04-05 16:54:08 +0200968 if (d->dv_lock)
969 luaL_error(L, "dict is locked");
Bram Moolenaard6ef5f92018-07-13 22:08:23 +0200970 if (key == NULL)
971 return 0;
972 if (*key == NUL)
Bram Moolenaarca06da92018-07-01 15:12:05 +0200973 luaL_error(L, "empty key");
Bram Moolenaar17413672018-07-14 20:49:42 +0200974 if (!lua_isnil(L, 3)) /* read value? */
975 {
Bram Moolenaarca06da92018-07-01 15:12:05 +0200976 luaV_checktypval(L, 3, &v, "setting dict item");
977 if (d->dv_scope == VAR_DEF_SCOPE && v.v_type == VAR_FUNC)
978 luaL_error(L, "cannot assign funcref to builtin scope");
979 }
Bram Moolenaar1dced572012-04-05 16:54:08 +0200980 di = dict_find(d, key, -1);
981 if (di == NULL) /* non-existing key? */
982 {
Bram Moolenaard6ef5f92018-07-13 22:08:23 +0200983 if (lua_isnil(L, 3))
984 return 0;
Bram Moolenaar1dced572012-04-05 16:54:08 +0200985 di = dictitem_alloc(key);
Bram Moolenaard6ef5f92018-07-13 22:08:23 +0200986 if (di == NULL)
987 return 0;
Bram Moolenaar1dced572012-04-05 16:54:08 +0200988 if (dict_add(d, di) == FAIL)
989 {
Bram Moolenaard6ef5f92018-07-13 22:08:23 +0200990 vim_free(di);
991 return 0;
Bram Moolenaar1dced572012-04-05 16:54:08 +0200992 }
993 }
994 else
995 clear_tv(&di->di_tv);
996 if (lua_isnil(L, 3)) /* remove? */
997 {
998 hashitem_T *hi = hash_find(&d->dv_hashtab, di->di_key);
999 hash_remove(&d->dv_hashtab, hi);
1000 dictitem_free(di);
1001 }
Bram Moolenaarca06da92018-07-01 15:12:05 +02001002 else
1003 {
Bram Moolenaar1dced572012-04-05 16:54:08 +02001004 copy_tv(&v, &di->di_tv);
Bram Moolenaarb3766472013-04-15 13:49:21 +02001005 clear_tv(&v);
Bram Moolenaar1dced572012-04-05 16:54:08 +02001006 }
1007 return 0;
1008}
1009
1010static const luaL_Reg luaV_Dict_mt[] = {
1011 {"__tostring", luaV_dict_tostring},
Bram Moolenaar1dced572012-04-05 16:54:08 +02001012 {"__len", luaV_dict_len},
1013 {"__call", luaV_dict_call},
1014 {"__index", luaV_dict_index},
1015 {"__newindex", luaV_dict_newindex},
1016 {NULL, NULL}
1017};
1018
1019
Bram Moolenaarca06da92018-07-01 15:12:05 +02001020/* ======= Funcref type ======= */
1021
1022 static luaV_Funcref *
1023luaV_newfuncref(lua_State *L, char_u *name)
1024{
1025 luaV_Funcref *f = (luaV_Funcref *)lua_newuserdata(L, sizeof(luaV_Funcref));
1026
1027 if (name != NULL)
1028 {
1029 func_ref(name); /* as in copy_tv */
1030 f->tv.vval.v_string = vim_strsave(name);
1031 }
1032 f->tv.v_type = VAR_FUNC;
1033 f->args.v_type = VAR_LIST;
1034 f->self = NULL;
1035 luaV_getfield(L, LUAVIM_FUNCREF);
1036 lua_setmetatable(L, -2);
1037 return f;
1038}
1039
1040 static luaV_Funcref *
1041luaV_pushfuncref(lua_State *L, typval_T *tv)
1042{
1043 luaV_Funcref *f = luaV_newfuncref(L, NULL);
1044 copy_tv(tv, &f->tv);
1045 clear_tv(tv);
1046 return f;
1047}
1048
1049
1050luaV_type_tostring(funcref, LUAVIM_FUNCREF)
1051
1052 static int
1053luaV_funcref_gc(lua_State *L)
1054{
1055 luaV_Funcref *f = (luaV_Funcref *) lua_touserdata(L, 1);
1056
1057 func_unref(f->tv.vval.v_string);
1058 vim_free(f->tv.vval.v_string);
1059 dict_unref(f->self);
1060 return 0;
1061}
1062
1063/* equivalent to string(funcref) */
1064 static int
1065luaV_funcref_len(lua_State *L)
1066{
1067 luaV_Funcref *f = (luaV_Funcref *) lua_touserdata(L, 1);
1068
1069 lua_pushstring(L, (const char *) f->tv.vval.v_string);
1070 return 1;
1071}
1072
1073 static int
1074luaV_funcref_call(lua_State *L)
1075{
1076 luaV_Funcref *f = (luaV_Funcref *) lua_touserdata(L, 1);
1077 int i, n = lua_gettop(L) - 1; /* #args */
1078 int status;
1079 typval_T v, rettv;
1080
1081 f->args.vval.v_list = list_alloc();
1082 rettv.v_type = VAR_UNKNOWN; /* as in clear_tv */
Bram Moolenaard6ef5f92018-07-13 22:08:23 +02001083 if (f->args.vval.v_list == NULL)
1084 status = FAIL;
1085 else
1086 {
Bram Moolenaar17413672018-07-14 20:49:42 +02001087 for (i = 0; i < n; i++)
1088 {
Bram Moolenaard6ef5f92018-07-13 22:08:23 +02001089 luaV_checktypval(L, i + 2, &v, "calling funcref");
1090 list_append_tv(f->args.vval.v_list, &v);
1091 }
1092 status = func_call(f->tv.vval.v_string, &f->args,
1093 NULL, f->self, &rettv);
1094 if (status == OK)
1095 luaV_pushtypval(L, &rettv);
1096 clear_tv(&f->args);
1097 clear_tv(&rettv);
Bram Moolenaarca06da92018-07-01 15:12:05 +02001098 }
Bram Moolenaarca06da92018-07-01 15:12:05 +02001099 if (status != OK)
1100 luaL_error(L, "cannot call funcref");
1101 return 1;
1102}
1103
1104static const luaL_Reg luaV_Funcref_mt[] = {
1105 {"__tostring", luaV_funcref_tostring},
1106 {"__gc", luaV_funcref_gc},
1107 {"__len", luaV_funcref_len},
1108 {"__call", luaV_funcref_call},
1109 {NULL, NULL}
1110};
1111
1112
Bram Moolenaar1dced572012-04-05 16:54:08 +02001113/* ======= Buffer type ======= */
1114
1115luaV_newtype(buf_T, buffer, luaV_Buffer, LUAVIM_BUFFER)
1116luaV_pushtype(buf_T, buffer, luaV_Buffer)
1117luaV_type_tostring(buffer, LUAVIM_BUFFER)
1118
1119 static int
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001120luaV_buffer_len(lua_State *L)
1121{
Bram Moolenaar1dced572012-04-05 16:54:08 +02001122 buf_T *b = (buf_T *) luaV_checkvalid(L, luaV_Buffer, 1);
1123 lua_pushinteger(L, b->b_ml.ml_line_count);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001124 return 1;
1125}
1126
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001127 static int
1128luaV_buffer_call(lua_State *L)
1129{
Bram Moolenaar1dced572012-04-05 16:54:08 +02001130 buf_T *b = (buf_T *) luaV_checkvalid(L, luaV_Buffer, 1);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001131 lua_settop(L, 1);
Bram Moolenaar1dced572012-04-05 16:54:08 +02001132 set_curbuf(b, DOBUF_SPLIT);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001133 return 1;
1134}
1135
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001136 static int
1137luaV_buffer_index(lua_State *L)
1138{
Bram Moolenaar1dced572012-04-05 16:54:08 +02001139 buf_T *b = (buf_T *) luaV_checkvalid(L, luaV_Buffer, 1);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001140 linenr_T n = (linenr_T) lua_tointeger(L, 2);
Bram Moolenaar1dced572012-04-05 16:54:08 +02001141 if (n > 0 && n <= b->b_ml.ml_line_count)
1142 luaV_pushline(L, b, n);
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001143 else if (lua_isstring(L, 2))
1144 {
1145 const char *s = lua_tostring(L, 2);
1146 if (strncmp(s, "name", 4) == 0)
Bram Moolenaarfe08df42018-07-07 23:07:41 +02001147 lua_pushstring(L, (b->b_sfname == NULL)
1148 ? "" : (char *) b->b_sfname);
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001149 else if (strncmp(s, "fname", 5) == 0)
Bram Moolenaarfe08df42018-07-07 23:07:41 +02001150 lua_pushstring(L, (b->b_ffname == NULL)
1151 ? "" : (char *) b->b_ffname);
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001152 else if (strncmp(s, "number", 6) == 0)
Bram Moolenaar1dced572012-04-05 16:54:08 +02001153 lua_pushinteger(L, b->b_fnum);
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001154 /* methods */
1155 else if (strncmp(s, "insert", 6) == 0
1156 || strncmp(s, "next", 4) == 0
1157 || strncmp(s, "previous", 8) == 0
1158 || strncmp(s, "isvalid", 7) == 0)
1159 {
1160 lua_getmetatable(L, 1);
1161 lua_getfield(L, -1, s);
1162 }
1163 else
1164 lua_pushnil(L);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001165 }
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001166 else
1167 lua_pushnil(L);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001168 return 1;
1169}
1170
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001171 static int
1172luaV_buffer_newindex(lua_State *L)
1173{
Bram Moolenaar1dced572012-04-05 16:54:08 +02001174 buf_T *b = (buf_T *) luaV_checkvalid(L, luaV_Buffer, 1);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001175 linenr_T n = (linenr_T) luaL_checkinteger(L, 2);
1176#ifdef HAVE_SANDBOX
1177 luaV_checksandbox(L);
1178#endif
Bram Moolenaar1dced572012-04-05 16:54:08 +02001179 if (n < 1 || n > b->b_ml.ml_line_count)
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001180 luaL_error(L, "invalid line number");
1181 if (lua_isnil(L, 3)) /* delete line */
1182 {
1183 buf_T *buf = curbuf;
Bram Moolenaar1dced572012-04-05 16:54:08 +02001184 curbuf = b;
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001185 if (u_savedel(n, 1L) == FAIL)
1186 {
1187 curbuf = buf;
1188 luaL_error(L, "cannot save undo information");
1189 }
1190 else if (ml_delete(n, FALSE) == FAIL)
1191 {
1192 curbuf = buf;
1193 luaL_error(L, "cannot delete line");
1194 }
Bram Moolenaarca06da92018-07-01 15:12:05 +02001195 else
1196 {
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001197 deleted_lines_mark(n, 1L);
Bram Moolenaar1dced572012-04-05 16:54:08 +02001198 if (b == curwin->w_buffer) /* fix cursor in current window? */
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001199 {
1200 if (curwin->w_cursor.lnum >= n)
1201 {
1202 if (curwin->w_cursor.lnum > n)
1203 {
1204 curwin->w_cursor.lnum -= 1;
1205 check_cursor_col();
1206 }
1207 else check_cursor();
1208 changed_cline_bef_curs();
1209 }
1210 invalidate_botline();
1211 }
1212 }
1213 curbuf = buf;
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001214 }
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001215 else if (lua_isstring(L, 3)) /* update line */
1216 {
1217 buf_T *buf = curbuf;
Bram Moolenaar1dced572012-04-05 16:54:08 +02001218 curbuf = b;
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001219 if (u_savesub(n) == FAIL)
1220 {
1221 curbuf = buf;
1222 luaL_error(L, "cannot save undo information");
1223 }
1224 else if (ml_replace(n, luaV_toline(L, 3), TRUE) == FAIL)
1225 {
1226 curbuf = buf;
1227 luaL_error(L, "cannot replace line");
1228 }
1229 else changed_bytes(n, 0);
1230 curbuf = buf;
Bram Moolenaar1dced572012-04-05 16:54:08 +02001231 if (b == curwin->w_buffer)
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001232 check_cursor_col();
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001233 }
1234 else
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001235 luaL_error(L, "wrong argument to change line");
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001236 return 0;
1237}
1238
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001239 static int
1240luaV_buffer_insert(lua_State *L)
1241{
Bram Moolenaar1dced572012-04-05 16:54:08 +02001242 luaV_Buffer *lb = luaV_checkudata(L, 1, LUAVIM_BUFFER);
1243 buf_T *b = (buf_T *) luaV_checkcache(L, (void *) *lb);
1244 linenr_T last = b->b_ml.ml_line_count;
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001245 linenr_T n = (linenr_T) luaL_optinteger(L, 3, last);
1246 buf_T *buf;
1247 luaL_checktype(L, 2, LUA_TSTRING);
1248#ifdef HAVE_SANDBOX
1249 luaV_checksandbox(L);
1250#endif
1251 /* fix insertion line */
1252 if (n < 0) n = 0;
1253 if (n > last) n = last;
1254 /* insert */
1255 buf = curbuf;
Bram Moolenaar1dced572012-04-05 16:54:08 +02001256 curbuf = b;
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001257 if (u_save(n, n + 1) == FAIL)
1258 {
1259 curbuf = buf;
1260 luaL_error(L, "cannot save undo information");
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001261 }
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001262 else if (ml_append(n, luaV_toline(L, 2), 0, FALSE) == FAIL)
1263 {
1264 curbuf = buf;
1265 luaL_error(L, "cannot insert line");
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001266 }
1267 else
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001268 appended_lines_mark(n, 1L);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001269 curbuf = buf;
1270 update_screen(VALID);
1271 return 0;
1272}
1273
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001274 static int
1275luaV_buffer_next(lua_State *L)
1276{
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001277 luaV_Buffer *b = luaV_checkudata(L, 1, LUAVIM_BUFFER);
Bram Moolenaar1dced572012-04-05 16:54:08 +02001278 buf_T *buf = (buf_T *) luaV_checkcache(L, (void *) *b);
1279 luaV_pushbuffer(L, buf->b_next);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001280 return 1;
1281}
1282
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001283 static int
1284luaV_buffer_previous(lua_State *L)
1285{
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001286 luaV_Buffer *b = luaV_checkudata(L, 1, LUAVIM_BUFFER);
Bram Moolenaar1dced572012-04-05 16:54:08 +02001287 buf_T *buf = (buf_T *) luaV_checkcache(L, (void *) *b);
1288 luaV_pushbuffer(L, buf->b_prev);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001289 return 1;
1290}
1291
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001292 static int
1293luaV_buffer_isvalid(lua_State *L)
1294{
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001295 luaV_Buffer *b = luaV_checkudata(L, 1, LUAVIM_BUFFER);
Bram Moolenaar1dced572012-04-05 16:54:08 +02001296 luaV_getudata(L, *b);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001297 lua_pushboolean(L, !lua_isnil(L, -1));
1298 return 1;
1299}
1300
1301static const luaL_Reg luaV_Buffer_mt[] = {
1302 {"__tostring", luaV_buffer_tostring},
1303 {"__len", luaV_buffer_len},
1304 {"__call", luaV_buffer_call},
1305 {"__index", luaV_buffer_index},
1306 {"__newindex", luaV_buffer_newindex},
1307 {"insert", luaV_buffer_insert},
1308 {"next", luaV_buffer_next},
1309 {"previous", luaV_buffer_previous},
1310 {"isvalid", luaV_buffer_isvalid},
1311 {NULL, NULL}
1312};
1313
1314
1315/* ======= Window type ======= */
1316
Bram Moolenaar1dced572012-04-05 16:54:08 +02001317luaV_newtype(win_T, window, luaV_Window, LUAVIM_WINDOW)
1318luaV_pushtype(win_T, window, luaV_Window)
1319luaV_type_tostring(window, LUAVIM_WINDOW)
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001320
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001321 static int
1322luaV_window_call(lua_State *L)
1323{
Bram Moolenaar1dced572012-04-05 16:54:08 +02001324 win_T *w = (win_T *) luaV_checkvalid(L, luaV_Window, 1);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001325 lua_settop(L, 1);
Bram Moolenaar1dced572012-04-05 16:54:08 +02001326 win_goto(w);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001327 return 1;
1328}
1329
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001330 static int
1331luaV_window_index(lua_State *L)
1332{
Bram Moolenaar1dced572012-04-05 16:54:08 +02001333 win_T *w = (win_T *) luaV_checkvalid(L, luaV_Window, 1);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001334 const char *s = luaL_checkstring(L, 2);
1335 if (strncmp(s, "buffer", 6) == 0)
Bram Moolenaar1dced572012-04-05 16:54:08 +02001336 luaV_pushbuffer(L, w->w_buffer);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001337 else if (strncmp(s, "line", 4) == 0)
Bram Moolenaar1dced572012-04-05 16:54:08 +02001338 lua_pushinteger(L, w->w_cursor.lnum);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001339 else if (strncmp(s, "col", 3) == 0)
Bram Moolenaar1dced572012-04-05 16:54:08 +02001340 lua_pushinteger(L, w->w_cursor.col + 1);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001341 else if (strncmp(s, "width", 5) == 0)
Bram Moolenaar02631462017-09-22 15:20:32 +02001342 lua_pushinteger(L, w->w_width);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001343 else if (strncmp(s, "height", 6) == 0)
Bram Moolenaar1dced572012-04-05 16:54:08 +02001344 lua_pushinteger(L, w->w_height);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001345 /* methods */
1346 else if (strncmp(s, "next", 4) == 0
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001347 || strncmp(s, "previous", 8) == 0
1348 || strncmp(s, "isvalid", 7) == 0)
1349 {
1350 lua_getmetatable(L, 1);
1351 lua_getfield(L, -1, s);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001352 }
1353 else
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001354 lua_pushnil(L);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001355 return 1;
1356}
1357
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001358 static int
1359luaV_window_newindex (lua_State *L)
1360{
Bram Moolenaar1dced572012-04-05 16:54:08 +02001361 win_T *w = (win_T *) luaV_checkvalid(L, luaV_Window, 1);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001362 const char *s = luaL_checkstring(L, 2);
1363 int v = luaL_checkinteger(L, 3);
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001364 if (strncmp(s, "line", 4) == 0)
1365 {
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001366#ifdef HAVE_SANDBOX
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001367 luaV_checksandbox(L);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001368#endif
Bram Moolenaar1dced572012-04-05 16:54:08 +02001369 if (v < 1 || v > w->w_buffer->b_ml.ml_line_count)
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001370 luaL_error(L, "line out of range");
Bram Moolenaar1dced572012-04-05 16:54:08 +02001371 w->w_cursor.lnum = v;
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001372 update_screen(VALID);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001373 }
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001374 else if (strncmp(s, "col", 3) == 0)
1375 {
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001376#ifdef HAVE_SANDBOX
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001377 luaV_checksandbox(L);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001378#endif
Bram Moolenaar1dced572012-04-05 16:54:08 +02001379 w->w_cursor.col = v - 1;
Bram Moolenaar53901442018-07-25 22:02:36 +02001380 w->w_set_curswant = TRUE;
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001381 update_screen(VALID);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001382 }
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001383 else if (strncmp(s, "width", 5) == 0)
1384 {
1385 win_T *win = curwin;
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001386#ifdef FEAT_GUI
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001387 need_mouse_correct = TRUE;
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001388#endif
Bram Moolenaar1dced572012-04-05 16:54:08 +02001389 curwin = w;
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001390 win_setwidth(v);
1391 curwin = win;
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001392 }
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001393 else if (strncmp(s, "height", 6) == 0)
1394 {
1395 win_T *win = curwin;
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001396#ifdef FEAT_GUI
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001397 need_mouse_correct = TRUE;
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001398#endif
Bram Moolenaar1dced572012-04-05 16:54:08 +02001399 curwin = w;
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001400 win_setheight(v);
1401 curwin = win;
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001402 }
1403 else
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001404 luaL_error(L, "invalid window property: `%s'", s);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001405 return 0;
1406}
1407
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001408 static int
1409luaV_window_next(lua_State *L)
1410{
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001411 luaV_Window *w = luaV_checkudata(L, 1, LUAVIM_WINDOW);
Bram Moolenaar1dced572012-04-05 16:54:08 +02001412 win_T *win = (win_T *) luaV_checkcache(L, (void *) *w);
1413 luaV_pushwindow(L, win->w_next);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001414 return 1;
1415}
1416
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001417 static int
1418luaV_window_previous(lua_State *L)
1419{
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001420 luaV_Window *w = luaV_checkudata(L, 1, LUAVIM_WINDOW);
Bram Moolenaar1dced572012-04-05 16:54:08 +02001421 win_T *win = (win_T *) luaV_checkcache(L, (void *) *w);
1422 luaV_pushwindow(L, win->w_prev);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001423 return 1;
1424}
1425
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001426 static int
1427luaV_window_isvalid(lua_State *L)
1428{
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001429 luaV_Window *w = luaV_checkudata(L, 1, LUAVIM_WINDOW);
Bram Moolenaar1dced572012-04-05 16:54:08 +02001430 luaV_getudata(L, *w);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001431 lua_pushboolean(L, !lua_isnil(L, -1));
1432 return 1;
1433}
1434
1435static const luaL_Reg luaV_Window_mt[] = {
1436 {"__tostring", luaV_window_tostring},
1437 {"__call", luaV_window_call},
1438 {"__index", luaV_window_index},
1439 {"__newindex", luaV_window_newindex},
1440 {"next", luaV_window_next},
1441 {"previous", luaV_window_previous},
1442 {"isvalid", luaV_window_isvalid},
1443 {NULL, NULL}
1444};
1445
1446
1447/* ======= Vim module ======= */
1448
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001449 static int
1450luaV_print(lua_State *L)
1451{
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001452 int i, n = lua_gettop(L); /* nargs */
1453 const char *s;
1454 size_t l;
1455 luaL_Buffer b;
1456 luaL_buffinit(L, &b);
1457 lua_getglobal(L, "tostring");
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001458 for (i = 1; i <= n; i++)
1459 {
1460 lua_pushvalue(L, -1); /* tostring */
1461 lua_pushvalue(L, i); /* arg */
1462 lua_call(L, 1, 1);
1463 s = lua_tolstring(L, -1, &l);
1464 if (s == NULL)
1465 return luaL_error(L, "cannot convert to string");
1466 if (i > 1) luaL_addchar(&b, ' '); /* use space instead of tab */
1467 luaV_addlstring(&b, s, l, 0);
1468 lua_pop(L, 1);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001469 }
1470 luaL_pushresult(&b);
1471 luaV_msg(L);
1472 return 0;
1473}
1474
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001475 static int
Bram Moolenaar38e2b062011-09-21 17:15:39 +02001476luaV_debug(lua_State *L)
1477{
1478 lua_settop(L, 0);
1479 lua_getglobal(L, "vim");
1480 lua_getfield(L, -1, "eval");
1481 lua_remove(L, -2); /* vim.eval at position 1 */
1482 for (;;)
1483 {
1484 const char *input;
1485 size_t l;
1486 lua_pushvalue(L, 1); /* vim.eval */
1487 lua_pushliteral(L, "input('lua_debug> ')");
1488 lua_call(L, 1, 1); /* return string */
1489 input = lua_tolstring(L, -1, &l);
1490 if (l == 0 || strcmp(input, "cont") == 0)
1491 return 0;
1492 msg_putchar('\n'); /* avoid outputting on input line */
1493 if (luaL_loadbuffer(L, input, l, "=(debug command)")
1494 || lua_pcall(L, 0, 0, 0))
1495 luaV_emsg(L);
1496 lua_settop(L, 1); /* remove eventual returns, but keep vim.eval */
1497 }
1498}
1499
1500 static int
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001501luaV_command(lua_State *L)
1502{
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001503 do_cmdline_cmd((char_u *) luaL_checkstring(L, 1));
1504 update_screen(VALID);
1505 return 0;
1506}
1507
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001508 static int
1509luaV_eval(lua_State *L)
1510{
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001511 typval_T *tv = eval_expr((char_u *) luaL_checkstring(L, 1), NULL);
1512 if (tv == NULL) luaL_error(L, "invalid expression");
1513 luaV_pushtypval(L, tv);
Bram Moolenaarb3766472013-04-15 13:49:21 +02001514 free_tv(tv);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001515 return 1;
1516}
1517
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001518 static int
Bram Moolenaar0d2e4fc2010-07-18 12:35:47 +02001519luaV_beep(lua_State *L UNUSED)
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001520{
Bram Moolenaar165bc692015-07-21 17:53:25 +02001521 vim_beep(BO_LANG);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001522 return 0;
1523}
1524
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001525 static int
1526luaV_line(lua_State *L)
1527{
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001528 luaV_pushline(L, curbuf, curwin->w_cursor.lnum);
1529 return 1;
1530}
1531
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001532 static int
Bram Moolenaar1dced572012-04-05 16:54:08 +02001533luaV_list(lua_State *L)
1534{
Bram Moolenaarca06da92018-07-01 15:12:05 +02001535 list_T *l;
1536 int initarg = !lua_isnoneornil(L, 1);
1537
1538 if (initarg && lua_type(L, 1) != LUA_TTABLE)
1539 luaL_error(L, "table expected, got %s", luaL_typename(L, 1));
1540 l = list_alloc();
Bram Moolenaar1dced572012-04-05 16:54:08 +02001541 if (l == NULL)
1542 lua_pushnil(L);
1543 else
Bram Moolenaarca06da92018-07-01 15:12:05 +02001544 {
Bram Moolenaar1dced572012-04-05 16:54:08 +02001545 luaV_newlist(L, l);
Bram Moolenaar17413672018-07-14 20:49:42 +02001546 if (initarg) /* traverse table to init list */
1547 {
Bram Moolenaarca06da92018-07-01 15:12:05 +02001548 int notnil, i = 0;
1549 typval_T v;
Bram Moolenaar17413672018-07-14 20:49:42 +02001550 do
1551 {
Bram Moolenaarca06da92018-07-01 15:12:05 +02001552 lua_rawgeti(L, 1, ++i);
1553 notnil = !lua_isnil(L, -1);
Bram Moolenaar17413672018-07-14 20:49:42 +02001554 if (notnil)
1555 {
Bram Moolenaarca06da92018-07-01 15:12:05 +02001556 luaV_checktypval(L, -1, &v, "vim.list");
1557 list_append_tv(l, &v);
1558 }
1559 lua_pop(L, 1); /* value */
1560 } while (notnil);
1561 }
1562 }
Bram Moolenaar1dced572012-04-05 16:54:08 +02001563 return 1;
1564}
1565
1566 static int
1567luaV_dict(lua_State *L)
1568{
Bram Moolenaarca06da92018-07-01 15:12:05 +02001569 dict_T *d;
1570 int initarg = !lua_isnoneornil(L, 1);
1571
1572 if (initarg && lua_type(L, 1) != LUA_TTABLE)
1573 luaL_error(L, "table expected, got %s", luaL_typename(L, 1));
1574 d = dict_alloc();
Bram Moolenaar1dced572012-04-05 16:54:08 +02001575 if (d == NULL)
1576 lua_pushnil(L);
1577 else
Bram Moolenaarca06da92018-07-01 15:12:05 +02001578 {
Bram Moolenaar1dced572012-04-05 16:54:08 +02001579 luaV_newdict(L, d);
Bram Moolenaarca06da92018-07-01 15:12:05 +02001580 if (initarg) /* traverse table to init dict */
1581 {
1582 lua_pushnil(L);
1583 while (lua_next(L, 1))
1584 {
1585 char_u *key;
1586 dictitem_T *di;
1587 typval_T v;
Bram Moolenaard6ef5f92018-07-13 22:08:23 +02001588
Bram Moolenaarca06da92018-07-01 15:12:05 +02001589 lua_pushvalue(L, -2); /* dup key in case it's a number */
1590 key = (char_u *) lua_tostring(L, -1);
Bram Moolenaard6ef5f92018-07-13 22:08:23 +02001591 if (key == NULL)
1592 {
1593 lua_pushnil(L);
1594 return 1;
1595 }
1596 if (*key == NUL)
Bram Moolenaarca06da92018-07-01 15:12:05 +02001597 luaL_error(L, "table has empty key");
1598 luaV_checktypval(L, -2, &v, "vim.dict"); /* value */
1599 di = dictitem_alloc(key);
Bram Moolenaard6ef5f92018-07-13 22:08:23 +02001600 if (di == NULL || dict_add(d, di) == FAIL)
1601 {
Bram Moolenaarca06da92018-07-01 15:12:05 +02001602 vim_free(di);
1603 lua_pushnil(L);
1604 return 1;
1605 }
1606 copy_tv(&v, &di->di_tv);
1607 clear_tv(&v);
1608 lua_pop(L, 2); /* key copy and value */
1609 }
1610 }
1611 }
1612 return 1;
1613}
1614
1615 static int
1616luaV_funcref(lua_State *L)
1617{
1618 const char *name = luaL_checkstring(L, 1);
1619 /* note: not checking if function exists (needs function_exists) */
1620 if (name == NULL || *name == NUL || VIM_ISDIGIT(*name))
1621 luaL_error(L, "invalid function name: %s", name);
1622 luaV_newfuncref(L, (char_u *) name);
Bram Moolenaar1dced572012-04-05 16:54:08 +02001623 return 1;
1624}
1625
1626 static int
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001627luaV_buffer(lua_State *L)
1628{
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001629 buf_T *buf;
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001630 if (lua_isstring(L, 1)) /* get by number or name? */
1631 {
1632 if (lua_isnumber(L, 1)) /* by number? */
1633 {
1634 int n = lua_tointeger(L, 1);
Bram Moolenaar29323592016-07-24 22:04:11 +02001635 FOR_ALL_BUFFERS(buf)
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001636 if (buf->b_fnum == n) break;
1637 }
Bram Moolenaarca06da92018-07-01 15:12:05 +02001638 else // by name
1639 {
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001640 size_t l;
1641 const char *s = lua_tolstring(L, 1, &l);
Bram Moolenaar29323592016-07-24 22:04:11 +02001642 FOR_ALL_BUFFERS(buf)
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001643 {
1644 if (buf->b_ffname == NULL || buf->b_sfname == NULL)
1645 {
1646 if (l == 0) break;
1647 }
Bram Moolenaar0d2e4fc2010-07-18 12:35:47 +02001648 else if (strncmp(s, (char *)buf->b_ffname, l) == 0
1649 || strncmp(s, (char *)buf->b_sfname, l) == 0)
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001650 break;
1651 }
1652 }
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001653 }
Bram Moolenaar1dced572012-04-05 16:54:08 +02001654 else
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001655 buf = (lua_toboolean(L, 1)) ? firstbuf : curbuf; /* first buffer? */
Bram Moolenaar1dced572012-04-05 16:54:08 +02001656 luaV_pushbuffer(L, buf);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001657 return 1;
1658}
1659
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001660 static int
1661luaV_window(lua_State *L)
1662{
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001663 win_T *win;
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001664 if (lua_isnumber(L, 1)) /* get by number? */
1665 {
1666 int n = lua_tointeger(L, 1);
1667 for (win = firstwin; win != NULL; win = win->w_next, n--)
1668 if (n == 1) break;
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001669 }
Bram Moolenaar1dced572012-04-05 16:54:08 +02001670 else
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001671 win = (lua_toboolean(L, 1)) ? firstwin : curwin; /* first window? */
Bram Moolenaar1dced572012-04-05 16:54:08 +02001672 luaV_pushwindow(L, win);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001673 return 1;
1674}
1675
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001676 static int
1677luaV_open(lua_State *L)
1678{
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001679 char_u *s = NULL;
1680#ifdef HAVE_SANDBOX
1681 luaV_checksandbox(L);
1682#endif
1683 if (lua_isstring(L, 1)) s = (char_u *) lua_tostring(L, 1);
Bram Moolenaarfa263a52011-12-08 16:00:16 +01001684 luaV_pushbuffer(L, buflist_new(s, NULL, 1L, BLN_LISTED));
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001685 return 1;
1686}
1687
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001688 static int
Bram Moolenaar1dced572012-04-05 16:54:08 +02001689luaV_type(lua_State *L)
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001690{
Bram Moolenaar1dced572012-04-05 16:54:08 +02001691 luaL_checkany(L, 1);
1692 if (lua_type(L, 1) == LUA_TUSERDATA) /* check vim udata? */
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001693 {
Bram Moolenaar1dced572012-04-05 16:54:08 +02001694 lua_settop(L, 1);
1695 if (lua_getmetatable(L, 1))
1696 {
1697 luaV_getfield(L, LUAVIM_LIST);
1698 if (lua_rawequal(L, -1, 2))
1699 {
1700 lua_pushstring(L, "list");
1701 return 1;
1702 }
1703 luaV_getfield(L, LUAVIM_DICT);
1704 if (lua_rawequal(L, -1, 2))
1705 {
1706 lua_pushstring(L, "dict");
1707 return 1;
1708 }
Bram Moolenaarca06da92018-07-01 15:12:05 +02001709 luaV_getfield(L, LUAVIM_FUNCREF);
1710 if (lua_rawequal(L, -1, 2))
1711 {
1712 lua_pushstring(L, "funcref");
1713 return 1;
1714 }
Bram Moolenaar1dced572012-04-05 16:54:08 +02001715 luaV_getfield(L, LUAVIM_BUFFER);
1716 if (lua_rawequal(L, -1, 2))
1717 {
1718 lua_pushstring(L, "buffer");
1719 return 1;
1720 }
1721 luaV_getfield(L, LUAVIM_WINDOW);
1722 if (lua_rawequal(L, -1, 2))
1723 {
1724 lua_pushstring(L, "window");
1725 return 1;
1726 }
1727 }
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001728 }
Bram Moolenaar1dced572012-04-05 16:54:08 +02001729 lua_pushstring(L, luaL_typename(L, 1)); /* fallback */
1730 return 1;
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001731}
1732
1733static const luaL_Reg luaV_module[] = {
1734 {"command", luaV_command},
1735 {"eval", luaV_eval},
1736 {"beep", luaV_beep},
1737 {"line", luaV_line},
Bram Moolenaar1dced572012-04-05 16:54:08 +02001738 {"list", luaV_list},
1739 {"dict", luaV_dict},
Bram Moolenaarca06da92018-07-01 15:12:05 +02001740 {"funcref", luaV_funcref},
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001741 {"buffer", luaV_buffer},
1742 {"window", luaV_window},
1743 {"open", luaV_open},
Bram Moolenaar1dced572012-04-05 16:54:08 +02001744 {"type", luaV_type},
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001745 {NULL, NULL}
1746};
1747
Bram Moolenaar1dced572012-04-05 16:54:08 +02001748/* for freeing list, dict, buffer and window objects; lightuserdata as arg */
1749 static int
1750luaV_free(lua_State *L)
1751{
1752 lua_pushnil(L);
1753 luaV_setudata(L, lua_touserdata(L, 1));
1754 return 0;
1755}
1756
1757 static int
1758luaV_luaeval (lua_State *L)
1759{
1760 luaL_Buffer b;
1761 size_t l;
1762 const char *str = lua_tolstring(L, 1, &l);
1763 typval_T *arg = (typval_T *) lua_touserdata(L, 2);
1764 typval_T *rettv = (typval_T *) lua_touserdata(L, 3);
1765 luaL_buffinit(L, &b);
1766 luaL_addlstring(&b, LUAVIM_EVALHEADER, sizeof(LUAVIM_EVALHEADER) - 1);
1767 luaL_addlstring(&b, str, l);
1768 luaL_pushresult(&b);
1769 str = lua_tolstring(L, -1, &l);
1770 if (luaL_loadbuffer(L, str, l, LUAVIM_EVALNAME)) /* compile error? */
1771 {
1772 luaV_emsg(L);
1773 return 0;
1774 }
1775 luaV_pushtypval(L, arg);
1776 if (lua_pcall(L, 1, 1, 0)) /* running error? */
1777 {
1778 luaV_emsg(L);
1779 return 0;
1780 }
Bram Moolenaarca06da92018-07-01 15:12:05 +02001781 if (luaV_totypval(L, -1, rettv) == FAIL)
1782 EMSG("luaeval: cannot convert value");
Bram Moolenaarf554a322015-02-04 23:08:01 +01001783 return 0;
Bram Moolenaar1dced572012-04-05 16:54:08 +02001784}
1785
1786 static int
1787luaV_setref (lua_State *L)
1788{
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01001789 int copyID = lua_tointeger(L, 1);
1790 int abort = FALSE;
1791 typval_T tv;
1792
Bram Moolenaar1dced572012-04-05 16:54:08 +02001793 luaV_getfield(L, LUAVIM_LIST);
1794 luaV_getfield(L, LUAVIM_DICT);
1795 lua_pushnil(L);
Bram Moolenaarb84634d2015-02-04 22:02:37 +01001796 /* traverse cache table */
1797 while (!abort && lua_next(L, lua_upvalueindex(1)) != 0)
Bram Moolenaar1dced572012-04-05 16:54:08 +02001798 {
1799 lua_getmetatable(L, -1);
1800 if (lua_rawequal(L, -1, 2)) /* list? */
1801 {
1802 tv.v_type = VAR_LIST;
1803 tv.vval.v_list = (list_T *) lua_touserdata(L, 4); /* key */
Bram Moolenaarf609dcf2015-12-03 17:43:17 +01001804 abort = set_ref_in_item(&tv, copyID, NULL, NULL);
Bram Moolenaar1dced572012-04-05 16:54:08 +02001805 }
1806 else if (lua_rawequal(L, -1, 3)) /* dict? */
1807 {
1808 tv.v_type = VAR_DICT;
1809 tv.vval.v_dict = (dict_T *) lua_touserdata(L, 4); /* key */
Bram Moolenaarf609dcf2015-12-03 17:43:17 +01001810 abort = set_ref_in_item(&tv, copyID, NULL, NULL);
Bram Moolenaar1dced572012-04-05 16:54:08 +02001811 }
1812 lua_pop(L, 2); /* metatable and value */
Bram Moolenaar1dced572012-04-05 16:54:08 +02001813 }
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01001814 lua_pushinteger(L, abort);
Bram Moolenaarf554a322015-02-04 23:08:01 +01001815 return 1;
Bram Moolenaar1dced572012-04-05 16:54:08 +02001816}
1817
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001818 static int
1819luaopen_vim(lua_State *L)
1820{
Bram Moolenaar1dced572012-04-05 16:54:08 +02001821 /* set cache table */
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001822 lua_newtable(L);
1823 lua_newtable(L);
Bram Moolenaar1dced572012-04-05 16:54:08 +02001824 lua_pushstring(L, "v");
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001825 lua_setfield(L, -2, "__mode");
Bram Moolenaar1dced572012-04-05 16:54:08 +02001826 lua_setmetatable(L, -2); /* cache is weak-valued */
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001827 /* print */
1828 lua_pushcfunction(L, luaV_print);
1829 lua_setglobal(L, "print");
Bram Moolenaar38e2b062011-09-21 17:15:39 +02001830 /* debug.debug */
1831 lua_getglobal(L, "debug");
1832 lua_pushcfunction(L, luaV_debug);
1833 lua_setfield(L, -2, "debug");
1834 lua_pop(L, 1);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001835 /* free */
1836 lua_pushlightuserdata(L, (void *) LUAVIM_FREE);
Bram Moolenaar1dced572012-04-05 16:54:08 +02001837 lua_pushvalue(L, 1); /* cache table */
1838 lua_pushcclosure(L, luaV_free, 1);
1839 lua_rawset(L, LUA_REGISTRYINDEX);
1840 /* luaeval */
1841 lua_pushlightuserdata(L, (void *) LUAVIM_LUAEVAL);
1842 lua_pushvalue(L, 1); /* cache table */
1843 lua_pushcclosure(L, luaV_luaeval, 1);
1844 lua_rawset(L, LUA_REGISTRYINDEX);
1845 /* setref */
1846 lua_pushlightuserdata(L, (void *) LUAVIM_SETREF);
1847 lua_pushvalue(L, 1); /* cache table */
1848 lua_pushcclosure(L, luaV_setref, 1);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001849 lua_rawset(L, LUA_REGISTRYINDEX);
1850 /* register */
Bram Moolenaar1dced572012-04-05 16:54:08 +02001851 luaV_newmetatable(L, LUAVIM_LIST);
1852 lua_pushvalue(L, 1);
1853 luaV_openlib(L, luaV_List_mt, 1);
1854 luaV_newmetatable(L, LUAVIM_DICT);
1855 lua_pushvalue(L, 1);
1856 luaV_openlib(L, luaV_Dict_mt, 1);
Bram Moolenaarca06da92018-07-01 15:12:05 +02001857 luaV_newmetatable(L, LUAVIM_FUNCREF);
1858 lua_pushvalue(L, 1);
1859 luaV_openlib(L, luaV_Funcref_mt, 1);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001860 luaV_newmetatable(L, LUAVIM_BUFFER);
Bram Moolenaar1dced572012-04-05 16:54:08 +02001861 lua_pushvalue(L, 1); /* cache table */
1862 luaV_openlib(L, luaV_Buffer_mt, 1);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001863 luaV_newmetatable(L, LUAVIM_WINDOW);
Bram Moolenaar1dced572012-04-05 16:54:08 +02001864 lua_pushvalue(L, 1); /* cache table */
1865 luaV_openlib(L, luaV_Window_mt, 1);
1866 lua_newtable(L); /* vim table */
1867 lua_pushvalue(L, 1); /* cache table */
1868 luaV_openlib(L, luaV_module, 1);
1869 lua_setglobal(L, LUAVIM_NAME);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001870 return 0;
1871}
1872
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001873 static lua_State *
1874luaV_newstate(void)
1875{
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001876 lua_State *L = luaL_newstate();
Bram Moolenaar16c98f92010-07-28 22:46:08 +02001877 luaL_openlibs(L); /* core libs */
1878 lua_pushcfunction(L, luaopen_vim); /* vim */
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001879 lua_call(L, 0, 0);
1880 return L;
1881}
1882
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001883 static void
1884luaV_setrange(lua_State *L, int line1, int line2)
1885{
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001886 lua_getglobal(L, LUAVIM_NAME);
1887 lua_pushinteger(L, line1);
1888 lua_setfield(L, -2, "firstline");
1889 lua_pushinteger(L, line2);
1890 lua_setfield(L, -2, "lastline");
1891 lua_pop(L, 1); /* vim table */
1892}
1893
1894
1895/* ======= Interface ======= */
1896
1897static lua_State *L = NULL;
1898
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001899 static int
Bram Moolenaar1dced572012-04-05 16:54:08 +02001900lua_isopen(void)
Bram Moolenaar2bd6a1b2010-08-12 22:14:01 +02001901{
1902 return L != NULL;
1903}
1904
1905 static int
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001906lua_init(void)
1907{
Bram Moolenaar1dced572012-04-05 16:54:08 +02001908 if (!lua_isopen())
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001909 {
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001910#ifdef DYNAMIC_LUA
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001911 if (!lua_enabled(TRUE))
1912 {
1913 EMSG(_("Lua library cannot be loaded."));
1914 return FAIL;
1915 }
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001916#endif
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001917 L = luaV_newstate();
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001918 }
1919 return OK;
1920}
1921
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001922 void
1923lua_end(void)
1924{
Bram Moolenaar1dced572012-04-05 16:54:08 +02001925 if (lua_isopen())
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001926 {
1927 lua_close(L);
1928 L = NULL;
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001929#ifdef DYNAMIC_LUA
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001930 end_dynamic_lua();
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001931#endif
1932 }
1933}
1934
1935/* ex commands */
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001936 void
1937ex_lua(exarg_T *eap)
1938{
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001939 char *script;
1940 if (lua_init() == FAIL) return;
1941 script = (char *) script_get(eap, eap->arg);
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001942 if (!eap->skip)
1943 {
1944 char *s = (script) ? script : (char *) eap->arg;
1945 luaV_setrange(L, eap->line1, eap->line2);
1946 if (luaL_loadbuffer(L, s, strlen(s), LUAVIM_CHUNKNAME)
1947 || lua_pcall(L, 0, 0, 0))
1948 luaV_emsg(L);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001949 }
1950 if (script != NULL) vim_free(script);
1951}
1952
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001953 void
1954ex_luado(exarg_T *eap)
1955{
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001956 linenr_T l;
1957 const char *s = (const char *) eap->arg;
1958 luaL_Buffer b;
1959 size_t len;
Bram Moolenaard58f03b2017-01-29 22:48:45 +01001960 buf_T *was_curbuf = curbuf;
1961
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001962 if (lua_init() == FAIL) return;
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001963 if (u_save(eap->line1 - 1, eap->line2 + 1) == FAIL)
1964 {
1965 EMSG(_("cannot save undo information"));
1966 return;
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001967 }
1968 luaV_setrange(L, eap->line1, eap->line2);
1969 luaL_buffinit(L, &b);
Bram Moolenaarbd2f3c32012-04-06 14:31:00 +02001970 luaL_addlstring(&b, "return function(line, linenr) ", 30); /* header */
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001971 luaL_addlstring(&b, s, strlen(s));
1972 luaL_addlstring(&b, " end", 4); /* footer */
1973 luaL_pushresult(&b);
1974 s = lua_tolstring(L, -1, &len);
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001975 if (luaL_loadbuffer(L, s, len, LUAVIM_CHUNKNAME))
1976 {
1977 luaV_emsg(L);
1978 lua_pop(L, 1); /* function body */
1979 return;
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001980 }
1981 lua_call(L, 0, 1);
1982 lua_replace(L, -2); /* function -> body */
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001983 for (l = eap->line1; l <= eap->line2; l++)
1984 {
Bram Moolenaard58f03b2017-01-29 22:48:45 +01001985 /* Check the line number, the command my have deleted lines. */
1986 if (l > curbuf->b_ml.ml_line_count)
1987 break;
1988
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001989 lua_pushvalue(L, -1); /* function */
1990 luaV_pushline(L, curbuf, l); /* current line as arg */
Bram Moolenaarbd2f3c32012-04-06 14:31:00 +02001991 lua_pushinteger(L, l); /* current line number as arg */
1992 if (lua_pcall(L, 2, 1, 0))
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001993 {
1994 luaV_emsg(L);
1995 break;
1996 }
Bram Moolenaard58f03b2017-01-29 22:48:45 +01001997 /* Catch the command switching to another buffer. */
1998 if (curbuf != was_curbuf)
1999 break;
Bram Moolenaar55d5c032010-07-17 23:52:29 +02002000 if (lua_isstring(L, -1)) /* update line? */
2001 {
Bram Moolenaar0ba04292010-07-14 23:23:17 +02002002#ifdef HAVE_SANDBOX
Bram Moolenaar55d5c032010-07-17 23:52:29 +02002003 luaV_checksandbox(L);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02002004#endif
Bram Moolenaar55d5c032010-07-17 23:52:29 +02002005 ml_replace(l, luaV_toline(L, -1), TRUE);
2006 changed_bytes(l, 0);
2007 lua_pop(L, 1); /* result from luaV_toline */
2008 }
2009 lua_pop(L, 1); /* line */
Bram Moolenaar0ba04292010-07-14 23:23:17 +02002010 }
2011 lua_pop(L, 1); /* function */
2012 check_cursor();
2013 update_screen(NOT_VALID);
2014}
2015
Bram Moolenaar55d5c032010-07-17 23:52:29 +02002016 void
2017ex_luafile(exarg_T *eap)
2018{
2019 if (lua_init() == FAIL)
2020 return;
2021 if (!eap->skip)
2022 {
2023 luaV_setrange(L, eap->line1, eap->line2);
2024 if (luaL_loadfile(L, (char *) eap->arg) || lua_pcall(L, 0, 0, 0))
2025 luaV_emsg(L);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02002026 }
2027}
2028
Bram Moolenaar1dced572012-04-05 16:54:08 +02002029#define luaV_freetype(typ,tname) \
2030 void \
2031 lua_##tname##_free(typ *o) \
2032 { \
2033 if (!lua_isopen()) return; \
2034 luaV_getfield(L, LUAVIM_FREE); \
2035 lua_pushlightuserdata(L, (void *) o); \
2036 lua_call(L, 1, 0); \
2037 }
2038
2039luaV_freetype(buf_T, buffer)
2040luaV_freetype(win_T, window)
2041
Bram Moolenaar55d5c032010-07-17 23:52:29 +02002042 void
Bram Moolenaar1dced572012-04-05 16:54:08 +02002043do_luaeval (char_u *str, typval_T *arg, typval_T *rettv)
Bram Moolenaar55d5c032010-07-17 23:52:29 +02002044{
Bram Moolenaar1dced572012-04-05 16:54:08 +02002045 lua_init();
2046 luaV_getfield(L, LUAVIM_LUAEVAL);
2047 lua_pushstring(L, (char *) str);
2048 lua_pushlightuserdata(L, (void *) arg);
2049 lua_pushlightuserdata(L, (void *) rettv);
2050 lua_call(L, 3, 0);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02002051}
2052
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01002053 int
Bram Moolenaar1dced572012-04-05 16:54:08 +02002054set_ref_in_lua (int copyID)
Bram Moolenaar55d5c032010-07-17 23:52:29 +02002055{
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01002056 int aborted = 0;
2057
2058 if (lua_isopen())
2059 {
2060 luaV_getfield(L, LUAVIM_SETREF);
2061 /* call the function with 1 arg, getting 1 result back */
2062 lua_pushinteger(L, copyID);
2063 lua_call(L, 1, 1);
2064 /* get the result */
2065 aborted = lua_tointeger(L, -1);
2066 /* pop result off the stack */
2067 lua_pop(L, 1);
2068 }
2069 return aborted;
Bram Moolenaar0ba04292010-07-14 23:23:17 +02002070}
2071
2072#endif