blob: 9400e3a35f1ba8725fe4886e23ce1636f6575567 [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
Bram Moolenaar830e3582018-08-21 14:23:35 +0200167#if LUA_VERSION_NUM >= 504
168 #define lua_newuserdatauv dll_lua_newuserdatauv
169#else
170 #define lua_newuserdata dll_lua_newuserdata
171#endif
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200172#define lua_getmetatable dll_lua_getmetatable
173#define lua_setfield dll_lua_setfield
174#define lua_rawset dll_lua_rawset
175#define lua_rawseti dll_lua_rawseti
176#define lua_setmetatable dll_lua_setmetatable
Bram Moolenaarbd2f3c32012-04-06 14:31:00 +0200177#define lua_next dll_lua_next
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200178/* libs */
179#define luaopen_base dll_luaopen_base
180#define luaopen_table dll_luaopen_table
181#define luaopen_string dll_luaopen_string
182#define luaopen_math dll_luaopen_math
Bram Moolenaar16c98f92010-07-28 22:46:08 +0200183#define luaopen_io dll_luaopen_io
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200184#define luaopen_os dll_luaopen_os
185#define luaopen_package dll_luaopen_package
186#define luaopen_debug dll_luaopen_debug
Bram Moolenaar16c98f92010-07-28 22:46:08 +0200187#define luaL_openlibs dll_luaL_openlibs
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200188
189/* lauxlib */
Bram Moolenaar1dced572012-04-05 16:54:08 +0200190#if LUA_VERSION_NUM <= 501
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200191void (*dll_luaL_register) (lua_State *L, const char *libname, const luaL_Reg *l);
Bram Moolenaar1dced572012-04-05 16:54:08 +0200192char *(*dll_luaL_prepbuffer) (luaL_Buffer *B);
193void (*dll_luaL_openlib) (lua_State *L, const char *libname, const luaL_Reg *l, int nup);
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200194int (*dll_luaL_typerror) (lua_State *L, int narg, const char *tname);
Bram Moolenaar1dced572012-04-05 16:54:08 +0200195int (*dll_luaL_loadfile) (lua_State *L, const char *filename);
196int (*dll_luaL_loadbuffer) (lua_State *L, const char *buff, size_t sz, const char *name);
197#else
198char *(*dll_luaL_prepbuffsize) (luaL_Buffer *B, size_t sz);
199void (*dll_luaL_setfuncs) (lua_State *L, const luaL_Reg *l, int nup);
200int (*dll_luaL_loadfilex) (lua_State *L, const char *filename, const char *mode);
201int (*dll_luaL_loadbufferx) (lua_State *L, const char *buff, size_t sz, const char *name, const char *mode);
202int (*dll_luaL_argerror) (lua_State *L, int numarg, const char *extramsg);
203#endif
Bram Moolenaarbd2f3c32012-04-06 14:31:00 +0200204void (*dll_luaL_checkany) (lua_State *L, int narg);
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200205const char *(*dll_luaL_checklstring) (lua_State *L, int numArg, size_t *l);
206lua_Integer (*dll_luaL_checkinteger) (lua_State *L, int numArg);
207lua_Integer (*dll_luaL_optinteger) (lua_State *L, int nArg, lua_Integer def);
208void (*dll_luaL_checktype) (lua_State *L, int narg, int t);
209int (*dll_luaL_error) (lua_State *L, const char *fmt, ...);
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200210lua_State *(*dll_luaL_newstate) (void);
211void (*dll_luaL_buffinit) (lua_State *L, luaL_Buffer *B);
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200212void (*dll_luaL_addlstring) (luaL_Buffer *B, const char *s, size_t l);
213void (*dll_luaL_pushresult) (luaL_Buffer *B);
214/* lua */
Bram Moolenaar1dced572012-04-05 16:54:08 +0200215#if LUA_VERSION_NUM <= 501
216lua_Number (*dll_lua_tonumber) (lua_State *L, int idx);
217lua_Integer (*dll_lua_tointeger) (lua_State *L, int idx);
218void (*dll_lua_call) (lua_State *L, int nargs, int nresults);
219int (*dll_lua_pcall) (lua_State *L, int nargs, int nresults, int errfunc);
220#else
221lua_Number (*dll_lua_tonumberx) (lua_State *L, int idx, int *isnum);
222lua_Integer (*dll_lua_tointegerx) (lua_State *L, int idx, int *isnum);
223void (*dll_lua_callk) (lua_State *L, int nargs, int nresults, int ctx,
Bram Moolenaardb913952012-06-29 12:54:53 +0200224 lua_CFunction k);
Bram Moolenaar1dced572012-04-05 16:54:08 +0200225int (*dll_lua_pcallk) (lua_State *L, int nargs, int nresults, int errfunc,
Bram Moolenaardb913952012-06-29 12:54:53 +0200226 int ctx, lua_CFunction k);
Bram Moolenaar1dced572012-04-05 16:54:08 +0200227void (*dll_lua_getglobal) (lua_State *L, const char *var);
228void (*dll_lua_setglobal) (lua_State *L, const char *var);
Bram Moolenaar1f860d82015-06-27 18:36:16 +0200229#endif
230#if LUA_VERSION_NUM <= 502
231void (*dll_lua_replace) (lua_State *L, int idx);
232void (*dll_lua_remove) (lua_State *L, int idx);
233#endif
234#if LUA_VERSION_NUM >= 503
235void (*dll_lua_rotate) (lua_State *L, int idx, int n);
Bram Moolenaar9514b1f2015-06-25 18:27:32 +0200236void (*dll_lua_copy) (lua_State *L, int fromidx, int toidx);
Bram Moolenaar1dced572012-04-05 16:54:08 +0200237#endif
Bram Moolenaarbd2f3c32012-04-06 14:31:00 +0200238const char *(*dll_lua_typename) (lua_State *L, int tp);
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200239void (*dll_lua_close) (lua_State *L);
240int (*dll_lua_gettop) (lua_State *L);
241void (*dll_lua_settop) (lua_State *L, int idx);
242void (*dll_lua_pushvalue) (lua_State *L, int idx);
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200243int (*dll_lua_isnumber) (lua_State *L, int idx);
244int (*dll_lua_isstring) (lua_State *L, int idx);
245int (*dll_lua_type) (lua_State *L, int idx);
246int (*dll_lua_rawequal) (lua_State *L, int idx1, int idx2);
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200247int (*dll_lua_toboolean) (lua_State *L, int idx);
248const char *(*dll_lua_tolstring) (lua_State *L, int idx, size_t *len);
249void *(*dll_lua_touserdata) (lua_State *L, int idx);
250void (*dll_lua_pushnil) (lua_State *L);
251void (*dll_lua_pushnumber) (lua_State *L, lua_Number n);
252void (*dll_lua_pushinteger) (lua_State *L, lua_Integer n);
253void (*dll_lua_pushlstring) (lua_State *L, const char *s, size_t l);
254void (*dll_lua_pushstring) (lua_State *L, const char *s);
255const char *(*dll_lua_pushfstring) (lua_State *L, const char *fmt, ...);
256void (*dll_lua_pushcclosure) (lua_State *L, lua_CFunction fn, int n);
257void (*dll_lua_pushboolean) (lua_State *L, int b);
258void (*dll_lua_pushlightuserdata) (lua_State *L, void *p);
259void (*dll_lua_getfield) (lua_State *L, int idx, const char *k);
Bram Moolenaar17413672018-07-14 20:49:42 +0200260#if LUA_VERSION_NUM <= 502
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200261void (*dll_lua_rawget) (lua_State *L, int idx);
Bram Moolenaar1dced572012-04-05 16:54:08 +0200262void (*dll_lua_rawgeti) (lua_State *L, int idx, int n);
Bram Moolenaar17413672018-07-14 20:49:42 +0200263#else
264int (*dll_lua_rawget) (lua_State *L, int idx);
265int (*dll_lua_rawgeti) (lua_State *L, int idx, lua_Integer n);
266#endif
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200267void (*dll_lua_createtable) (lua_State *L, int narr, int nrec);
Bram Moolenaar830e3582018-08-21 14:23:35 +0200268#if LUA_VERSION_NUM >= 504
269void *(*dll_lua_newuserdatauv) (lua_State *L, size_t sz, int nuvalue);
270#else
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200271void *(*dll_lua_newuserdata) (lua_State *L, size_t sz);
Bram Moolenaar830e3582018-08-21 14:23:35 +0200272#endif
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200273int (*dll_lua_getmetatable) (lua_State *L, int objindex);
274void (*dll_lua_setfield) (lua_State *L, int idx, const char *k);
275void (*dll_lua_rawset) (lua_State *L, int idx);
Bram Moolenaar17413672018-07-14 20:49:42 +0200276#if LUA_VERSION_NUM <= 502
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200277void (*dll_lua_rawseti) (lua_State *L, int idx, int n);
Bram Moolenaar17413672018-07-14 20:49:42 +0200278#else
279void (*dll_lua_rawseti) (lua_State *L, int idx, lua_Integer n);
280#endif
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200281int (*dll_lua_setmetatable) (lua_State *L, int objindex);
Bram Moolenaarbd2f3c32012-04-06 14:31:00 +0200282int (*dll_lua_next) (lua_State *L, int idx);
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200283/* libs */
284int (*dll_luaopen_base) (lua_State *L);
285int (*dll_luaopen_table) (lua_State *L);
286int (*dll_luaopen_string) (lua_State *L);
287int (*dll_luaopen_math) (lua_State *L);
Bram Moolenaar16c98f92010-07-28 22:46:08 +0200288int (*dll_luaopen_io) (lua_State *L);
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200289int (*dll_luaopen_os) (lua_State *L);
290int (*dll_luaopen_package) (lua_State *L);
291int (*dll_luaopen_debug) (lua_State *L);
Bram Moolenaar16c98f92010-07-28 22:46:08 +0200292void (*dll_luaL_openlibs) (lua_State *L);
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200293
294typedef void **luaV_function;
295typedef struct {
296 const char *name;
297 luaV_function func;
298} luaV_Reg;
299
300static const luaV_Reg luaV_dll[] = {
301 /* lauxlib */
Bram Moolenaar1dced572012-04-05 16:54:08 +0200302#if LUA_VERSION_NUM <= 501
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200303 {"luaL_register", (luaV_function) &dll_luaL_register},
Bram Moolenaar1dced572012-04-05 16:54:08 +0200304 {"luaL_prepbuffer", (luaV_function) &dll_luaL_prepbuffer},
305 {"luaL_openlib", (luaV_function) &dll_luaL_openlib},
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200306 {"luaL_typerror", (luaV_function) &dll_luaL_typerror},
Bram Moolenaar1dced572012-04-05 16:54:08 +0200307 {"luaL_loadfile", (luaV_function) &dll_luaL_loadfile},
308 {"luaL_loadbuffer", (luaV_function) &dll_luaL_loadbuffer},
309#else
310 {"luaL_prepbuffsize", (luaV_function) &dll_luaL_prepbuffsize},
311 {"luaL_setfuncs", (luaV_function) &dll_luaL_setfuncs},
312 {"luaL_loadfilex", (luaV_function) &dll_luaL_loadfilex},
313 {"luaL_loadbufferx", (luaV_function) &dll_luaL_loadbufferx},
314 {"luaL_argerror", (luaV_function) &dll_luaL_argerror},
315#endif
Bram Moolenaarbd2f3c32012-04-06 14:31:00 +0200316 {"luaL_checkany", (luaV_function) &dll_luaL_checkany},
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200317 {"luaL_checklstring", (luaV_function) &dll_luaL_checklstring},
318 {"luaL_checkinteger", (luaV_function) &dll_luaL_checkinteger},
319 {"luaL_optinteger", (luaV_function) &dll_luaL_optinteger},
320 {"luaL_checktype", (luaV_function) &dll_luaL_checktype},
321 {"luaL_error", (luaV_function) &dll_luaL_error},
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200322 {"luaL_newstate", (luaV_function) &dll_luaL_newstate},
323 {"luaL_buffinit", (luaV_function) &dll_luaL_buffinit},
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200324 {"luaL_addlstring", (luaV_function) &dll_luaL_addlstring},
325 {"luaL_pushresult", (luaV_function) &dll_luaL_pushresult},
326 /* lua */
Bram Moolenaar1dced572012-04-05 16:54:08 +0200327#if LUA_VERSION_NUM <= 501
328 {"lua_tonumber", (luaV_function) &dll_lua_tonumber},
329 {"lua_tointeger", (luaV_function) &dll_lua_tointeger},
330 {"lua_call", (luaV_function) &dll_lua_call},
331 {"lua_pcall", (luaV_function) &dll_lua_pcall},
332#else
333 {"lua_tonumberx", (luaV_function) &dll_lua_tonumberx},
334 {"lua_tointegerx", (luaV_function) &dll_lua_tointegerx},
335 {"lua_callk", (luaV_function) &dll_lua_callk},
336 {"lua_pcallk", (luaV_function) &dll_lua_pcallk},
337 {"lua_getglobal", (luaV_function) &dll_lua_getglobal},
338 {"lua_setglobal", (luaV_function) &dll_lua_setglobal},
Bram Moolenaar1dced572012-04-05 16:54:08 +0200339#endif
Bram Moolenaar1f860d82015-06-27 18:36:16 +0200340#if LUA_VERSION_NUM <= 502
341 {"lua_replace", (luaV_function) &dll_lua_replace},
342 {"lua_remove", (luaV_function) &dll_lua_remove},
343#endif
344#if LUA_VERSION_NUM >= 503
345 {"lua_rotate", (luaV_function) &dll_lua_rotate},
346 {"lua_copy", (luaV_function) &dll_lua_copy},
347#endif
Bram Moolenaarbd2f3c32012-04-06 14:31:00 +0200348 {"lua_typename", (luaV_function) &dll_lua_typename},
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200349 {"lua_close", (luaV_function) &dll_lua_close},
350 {"lua_gettop", (luaV_function) &dll_lua_gettop},
351 {"lua_settop", (luaV_function) &dll_lua_settop},
352 {"lua_pushvalue", (luaV_function) &dll_lua_pushvalue},
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200353 {"lua_isnumber", (luaV_function) &dll_lua_isnumber},
354 {"lua_isstring", (luaV_function) &dll_lua_isstring},
355 {"lua_type", (luaV_function) &dll_lua_type},
356 {"lua_rawequal", (luaV_function) &dll_lua_rawequal},
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200357 {"lua_toboolean", (luaV_function) &dll_lua_toboolean},
358 {"lua_tolstring", (luaV_function) &dll_lua_tolstring},
359 {"lua_touserdata", (luaV_function) &dll_lua_touserdata},
360 {"lua_pushnil", (luaV_function) &dll_lua_pushnil},
361 {"lua_pushnumber", (luaV_function) &dll_lua_pushnumber},
362 {"lua_pushinteger", (luaV_function) &dll_lua_pushinteger},
363 {"lua_pushlstring", (luaV_function) &dll_lua_pushlstring},
364 {"lua_pushstring", (luaV_function) &dll_lua_pushstring},
365 {"lua_pushfstring", (luaV_function) &dll_lua_pushfstring},
366 {"lua_pushcclosure", (luaV_function) &dll_lua_pushcclosure},
367 {"lua_pushboolean", (luaV_function) &dll_lua_pushboolean},
368 {"lua_pushlightuserdata", (luaV_function) &dll_lua_pushlightuserdata},
369 {"lua_getfield", (luaV_function) &dll_lua_getfield},
370 {"lua_rawget", (luaV_function) &dll_lua_rawget},
Bram Moolenaar1dced572012-04-05 16:54:08 +0200371 {"lua_rawgeti", (luaV_function) &dll_lua_rawgeti},
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200372 {"lua_createtable", (luaV_function) &dll_lua_createtable},
Bram Moolenaar830e3582018-08-21 14:23:35 +0200373#if LUA_VERSION_NUM >= 504
374 {"lua_newuserdatauv", (luaV_function) &dll_lua_newuserdatauv},
375#else
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200376 {"lua_newuserdata", (luaV_function) &dll_lua_newuserdata},
Bram Moolenaar830e3582018-08-21 14:23:35 +0200377#endif
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200378 {"lua_getmetatable", (luaV_function) &dll_lua_getmetatable},
379 {"lua_setfield", (luaV_function) &dll_lua_setfield},
380 {"lua_rawset", (luaV_function) &dll_lua_rawset},
381 {"lua_rawseti", (luaV_function) &dll_lua_rawseti},
382 {"lua_setmetatable", (luaV_function) &dll_lua_setmetatable},
Bram Moolenaarbd2f3c32012-04-06 14:31:00 +0200383 {"lua_next", (luaV_function) &dll_lua_next},
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200384 /* libs */
385 {"luaopen_base", (luaV_function) &dll_luaopen_base},
386 {"luaopen_table", (luaV_function) &dll_luaopen_table},
387 {"luaopen_string", (luaV_function) &dll_luaopen_string},
388 {"luaopen_math", (luaV_function) &dll_luaopen_math},
Bram Moolenaar16c98f92010-07-28 22:46:08 +0200389 {"luaopen_io", (luaV_function) &dll_luaopen_io},
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200390 {"luaopen_os", (luaV_function) &dll_luaopen_os},
391 {"luaopen_package", (luaV_function) &dll_luaopen_package},
392 {"luaopen_debug", (luaV_function) &dll_luaopen_debug},
Bram Moolenaar16c98f92010-07-28 22:46:08 +0200393 {"luaL_openlibs", (luaV_function) &dll_luaL_openlibs},
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200394 {NULL, NULL}
395};
396
Bram Moolenaar2334b6d2010-07-22 21:32:16 +0200397static HANDLE hinstLua = NULL;
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200398
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200399 static void
400end_dynamic_lua(void)
401{
402 if (hinstLua)
403 {
Bram Moolenaar2334b6d2010-07-22 21:32:16 +0200404 close_dll(hinstLua);
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200405 hinstLua = 0;
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200406 }
407}
408
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200409 static int
410lua_link_init(char *libname, int verbose)
411{
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200412 const luaV_Reg *reg;
413 if (hinstLua) return OK;
Bram Moolenaar2334b6d2010-07-22 21:32:16 +0200414 hinstLua = load_dll(libname);
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200415 if (!hinstLua)
416 {
417 if (verbose)
418 EMSG2(_(e_loadlib), libname);
419 return FAIL;
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200420 }
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200421 for (reg = luaV_dll; reg->func; reg++)
422 {
Bram Moolenaar2334b6d2010-07-22 21:32:16 +0200423 if ((*reg->func = symbol_from_dll(hinstLua, reg->name)) == NULL)
424 {
425 close_dll(hinstLua);
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200426 hinstLua = 0;
427 if (verbose)
428 EMSG2(_(e_loadfunc), reg->name);
429 return FAIL;
430 }
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200431 }
432 return OK;
433}
Bram Moolenaard90b6c02016-08-28 18:10:45 +0200434#endif /* DYNAMIC_LUA */
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200435
Bram Moolenaard90b6c02016-08-28 18:10:45 +0200436#if defined(DYNAMIC_LUA) || defined(PROTO)
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200437 int
438lua_enabled(int verbose)
439{
Bram Moolenaar25e4fcd2016-01-09 14:57:47 +0100440 return lua_link_init((char *)p_luadll, verbose) == OK;
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200441}
Bram Moolenaard90b6c02016-08-28 18:10:45 +0200442#endif
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200443
Bram Moolenaar1dced572012-04-05 16:54:08 +0200444#if LUA_VERSION_NUM > 501
445 static int
446luaL_typeerror (lua_State *L, int narg, const char *tname)
447{
448 const char *msg = lua_pushfstring(L, "%s expected, got %s",
Bram Moolenaardb913952012-06-29 12:54:53 +0200449 tname, luaL_typename(L, narg));
Bram Moolenaar1dced572012-04-05 16:54:08 +0200450 return luaL_argerror(L, narg, msg);
451}
452#endif
453
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200454
455/* ======= Internal ======= */
456
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200457 static void
458luaV_newmetatable(lua_State *L, const char *tname)
459{
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200460 lua_newtable(L);
461 lua_pushlightuserdata(L, (void *) tname);
462 lua_pushvalue(L, -2);
463 lua_rawset(L, LUA_REGISTRYINDEX);
464}
465
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200466 static void *
467luaV_toudata(lua_State *L, int ud, const char *tname)
468{
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200469 void *p = lua_touserdata(L, ud);
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200470
471 if (p != NULL) /* value is userdata? */
472 {
473 if (lua_getmetatable(L, ud)) /* does it have a metatable? */
474 {
475 luaV_getfield(L, tname); /* get metatable */
476 if (lua_rawequal(L, -1, -2)) /* MTs match? */
477 {
478 lua_pop(L, 2); /* MTs */
479 return p;
480 }
481 }
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200482 }
483 return NULL;
484}
485
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200486 static void *
Bram Moolenaar1dced572012-04-05 16:54:08 +0200487luaV_checkcache(lua_State *L, void *p)
488{
489 luaV_getudata(L, p);
490 if (lua_isnil(L, -1)) luaL_error(L, "invalid object");
491 lua_pop(L, 1);
492 return p;
493}
494
495#define luaV_unbox(L,luatyp,ud) (*((luatyp *) lua_touserdata((L),(ud))))
496
497#define luaV_checkvalid(L,luatyp,ud) \
498 luaV_checkcache((L), (void *) luaV_unbox((L),luatyp,(ud)))
499
500 static void *
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200501luaV_checkudata(lua_State *L, int ud, const char *tname)
502{
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200503 void *p = luaV_toudata(L, ud, tname);
Bram Moolenaar1dced572012-04-05 16:54:08 +0200504 if (p == NULL) luaL_typeerror(L, ud, tname);
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200505 return p;
506}
507
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200508 static void
509luaV_pushtypval(lua_State *L, typval_T *tv)
510{
Bram Moolenaar1dced572012-04-05 16:54:08 +0200511 if (tv == NULL)
512 {
513 lua_pushnil(L);
514 return;
515 }
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200516 switch (tv->v_type)
517 {
518 case VAR_STRING:
Bram Moolenaard04da7c2012-10-14 03:41:59 +0200519 lua_pushstring(L, tv->vval.v_string == NULL
520 ? "" : (char *)tv->vval.v_string);
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200521 break;
522 case VAR_NUMBER:
523 lua_pushinteger(L, (int) tv->vval.v_number);
524 break;
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200525#ifdef FEAT_FLOAT
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200526 case VAR_FLOAT:
527 lua_pushnumber(L, (lua_Number) tv->vval.v_float);
528 break;
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200529#endif
Bram Moolenaar1dced572012-04-05 16:54:08 +0200530 case VAR_LIST:
531 luaV_pushlist(L, tv->vval.v_list);
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200532 break;
Bram Moolenaar1dced572012-04-05 16:54:08 +0200533 case VAR_DICT:
534 luaV_pushdict(L, tv->vval.v_dict);
535 break;
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100536 case VAR_SPECIAL:
537 if (tv->vval.v_number <= VVAL_TRUE)
538 lua_pushinteger(L, (int) tv->vval.v_number);
539 else
540 lua_pushnil(L);
541 break;
Bram Moolenaarca06da92018-07-01 15:12:05 +0200542 case VAR_FUNC:
543 luaV_pushfuncref(L, tv);
544 break;
Bram Moolenaar1dced572012-04-05 16:54:08 +0200545 default:
546 lua_pushnil(L);
547 }
548}
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200549
Bram Moolenaarca06da92018-07-01 15:12:05 +0200550/*
551 * Converts lua value at 'pos' to typval 'tv'.
552 * Returns OK or FAIL.
553 */
554 static int
555luaV_totypval(lua_State *L, int pos, typval_T *tv)
Bram Moolenaar1dced572012-04-05 16:54:08 +0200556{
Bram Moolenaarca06da92018-07-01 15:12:05 +0200557 int status = OK;
558
559 switch (lua_type(L, pos))
560 {
Bram Moolenaar1dced572012-04-05 16:54:08 +0200561 case LUA_TBOOLEAN:
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100562 tv->v_type = VAR_SPECIAL;
Bram Moolenaar1dced572012-04-05 16:54:08 +0200563 tv->vval.v_number = (varnumber_T) lua_toboolean(L, pos);
564 break;
Bram Moolenaar9067cd62019-01-01 00:41:54 +0100565 case LUA_TNIL:
566 tv->v_type = VAR_SPECIAL;
567 tv->vval.v_number = VVAL_NULL;
568 break;
Bram Moolenaar1dced572012-04-05 16:54:08 +0200569 case LUA_TSTRING:
570 tv->v_type = VAR_STRING;
571 tv->vval.v_string = vim_strsave((char_u *) lua_tostring(L, pos));
572 break;
573 case LUA_TNUMBER:
574#ifdef FEAT_FLOAT
575 tv->v_type = VAR_FLOAT;
576 tv->vval.v_float = (float_T) lua_tonumber(L, pos);
577#else
578 tv->v_type = VAR_NUMBER;
579 tv->vval.v_number = (varnumber_T) lua_tointeger(L, pos);
580#endif
581 break;
Bram Moolenaarca06da92018-07-01 15:12:05 +0200582 case LUA_TUSERDATA:
583 {
Bram Moolenaar1dced572012-04-05 16:54:08 +0200584 void *p = lua_touserdata(L, pos);
Bram Moolenaarca06da92018-07-01 15:12:05 +0200585
Bram Moolenaar1dced572012-04-05 16:54:08 +0200586 if (lua_getmetatable(L, pos)) /* has metatable? */
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200587 {
Bram Moolenaar1dced572012-04-05 16:54:08 +0200588 /* check list */
589 luaV_getfield(L, LUAVIM_LIST);
590 if (lua_rawequal(L, -1, -2))
Bram Moolenaar2334b6d2010-07-22 21:32:16 +0200591 {
Bram Moolenaar1dced572012-04-05 16:54:08 +0200592 tv->v_type = VAR_LIST;
593 tv->vval.v_list = *((luaV_List *) p);
594 ++tv->vval.v_list->lv_refcount;
595 lua_pop(L, 2); /* MTs */
Bram Moolenaarca06da92018-07-01 15:12:05 +0200596 break;
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200597 }
Bram Moolenaar1dced572012-04-05 16:54:08 +0200598 /* check dict */
599 luaV_getfield(L, LUAVIM_DICT);
600 if (lua_rawequal(L, -1, -3))
601 {
602 tv->v_type = VAR_DICT;
603 tv->vval.v_dict = *((luaV_Dict *) p);
604 ++tv->vval.v_dict->dv_refcount;
605 lua_pop(L, 3); /* MTs */
Bram Moolenaarca06da92018-07-01 15:12:05 +0200606 break;
Bram Moolenaar1dced572012-04-05 16:54:08 +0200607 }
Bram Moolenaarca06da92018-07-01 15:12:05 +0200608 /* check funcref */
609 luaV_getfield(L, LUAVIM_FUNCREF);
610 if (lua_rawequal(L, -1, -4))
611 {
612 luaV_Funcref *f = (luaV_Funcref *) p;
613 copy_tv(&f->tv, tv);
614 lua_pop(L, 4); /* MTs */
615 break;
616 }
617 lua_pop(L, 4); /* MTs */
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200618 }
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200619 }
Bram Moolenaarca06da92018-07-01 15:12:05 +0200620 /* FALLTHROUGH */
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200621 default:
Bram Moolenaar1dced572012-04-05 16:54:08 +0200622 tv->v_type = VAR_NUMBER;
623 tv->vval.v_number = 0;
Bram Moolenaarca06da92018-07-01 15:12:05 +0200624 status = FAIL;
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200625 }
Bram Moolenaarca06da92018-07-01 15:12:05 +0200626 return status;
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200627}
628
629/* similar to luaL_addlstring, but replaces \0 with \n if toline and
630 * \n with \0 otherwise */
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200631 static void
632luaV_addlstring(luaL_Buffer *b, const char *s, size_t l, int toline)
633{
634 while (l--)
635 {
636 if (*s == '\0' && toline)
637 luaL_addchar(b, '\n');
638 else if (*s == '\n' && !toline)
639 luaL_addchar(b, '\0');
640 else
641 luaL_addchar(b, *s);
642 s++;
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200643 }
644}
645
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200646 static void
647luaV_pushline(lua_State *L, buf_T *buf, linenr_T n)
648{
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200649 const char *s = (const char *) ml_get_buf(buf, n, FALSE);
650 luaL_Buffer b;
651 luaL_buffinit(L, &b);
652 luaV_addlstring(&b, s, strlen(s), 0);
653 luaL_pushresult(&b);
654}
655
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200656 static char_u *
657luaV_toline(lua_State *L, int pos)
658{
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200659 size_t l;
660 const char *s = lua_tolstring(L, pos, &l);
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200661
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200662 luaL_Buffer b;
663 luaL_buffinit(L, &b);
664 luaV_addlstring(&b, s, l, 1);
665 luaL_pushresult(&b);
666 return (char_u *) lua_tostring(L, -1);
667}
668
669/* pops a string s from the top of the stack and calls mf(t) for pieces t of
670 * s separated by newlines */
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200671 static void
672luaV_msgfunc(lua_State *L, msgfunc_T mf)
673{
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200674 luaL_Buffer b;
675 size_t l;
676 const char *p, *s = lua_tolstring(L, -1, &l);
677 luaL_buffinit(L, &b);
678 luaV_addlstring(&b, s, l, 0);
679 luaL_pushresult(&b);
680 /* break string */
681 p = s = lua_tolstring(L, -1, &l);
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200682 while (l--)
683 {
684 if (*p++ == '\0') /* break? */
685 {
686 mf((char_u *) s);
687 s = p;
688 }
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200689 }
690 mf((char_u *) s);
691 lua_pop(L, 2); /* original and modified strings */
692}
693
Bram Moolenaar1dced572012-04-05 16:54:08 +0200694#define luaV_newtype(typ,tname,luatyp,luatname) \
695 static luatyp * \
696 luaV_new##tname (lua_State *L, typ *obj) \
697 { \
698 luatyp *o = (luatyp *) lua_newuserdata(L, sizeof(luatyp)); \
699 *o = obj; \
700 luaV_setudata(L, obj); /* cache[obj] = udata */ \
701 luaV_getfield(L, luatname); \
702 lua_setmetatable(L, -2); \
703 return o; \
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200704 }
Bram Moolenaar1dced572012-04-05 16:54:08 +0200705
706#define luaV_pushtype(typ,tname,luatyp) \
707 static luatyp * \
Bram Moolenaarca06da92018-07-01 15:12:05 +0200708 luaV_push##tname(lua_State *L, typ *obj) \
Bram Moolenaar1dced572012-04-05 16:54:08 +0200709 { \
710 luatyp *o = NULL; \
711 if (obj == NULL) \
712 lua_pushnil(L); \
713 else { \
714 luaV_getudata(L, obj); \
715 if (lua_isnil(L, -1)) /* not interned? */ \
716 { \
717 lua_pop(L, 1); \
718 o = luaV_new##tname(L, obj); \
719 } \
720 else \
721 o = (luatyp *) lua_touserdata(L, -1); \
722 } \
723 return o; \
724 }
725
726#define luaV_type_tostring(tname,luatname) \
727 static int \
728 luaV_##tname##_tostring (lua_State *L) \
729 { \
730 lua_pushfstring(L, "%s: %p", luatname, lua_touserdata(L, 1)); \
731 return 1; \
732 }
733
Bram Moolenaar1dced572012-04-05 16:54:08 +0200734/* ======= List type ======= */
735
736 static luaV_List *
737luaV_newlist (lua_State *L, list_T *lis)
738{
739 luaV_List *l = (luaV_List *) lua_newuserdata(L, sizeof(luaV_List));
740 *l = lis;
741 lis->lv_refcount++; /* reference in Lua */
742 luaV_setudata(L, lis); /* cache[lis] = udata */
743 luaV_getfield(L, LUAVIM_LIST);
744 lua_setmetatable(L, -2);
745 return l;
746}
747
748luaV_pushtype(list_T, list, luaV_List)
749luaV_type_tostring(list, LUAVIM_LIST)
750
751 static int
Bram Moolenaar1dced572012-04-05 16:54:08 +0200752luaV_list_len (lua_State *L)
753{
754 list_T *l = luaV_unbox(L, luaV_List, 1);
755 lua_pushinteger(L, (l == NULL) ? 0 : (int) l->lv_len);
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200756 return 1;
757}
758
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200759 static int
Bram Moolenaar1dced572012-04-05 16:54:08 +0200760luaV_list_iter (lua_State *L)
761{
762 listitem_T *li = (listitem_T *) lua_touserdata(L, lua_upvalueindex(2));
763 if (li == NULL) return 0;
764 luaV_pushtypval(L, &li->li_tv);
765 lua_pushlightuserdata(L, (void *) li->li_next);
766 lua_replace(L, lua_upvalueindex(2));
767 return 1;
768}
769
770 static int
771luaV_list_call (lua_State *L)
772{
773 list_T *l = luaV_unbox(L, luaV_List, 1);
774 lua_pushvalue(L, lua_upvalueindex(1)); /* pass cache table along */
775 lua_pushlightuserdata(L, (void *) l->lv_first);
776 lua_pushcclosure(L, luaV_list_iter, 2);
777 return 1;
778}
779
780 static int
781luaV_list_index (lua_State *L)
782{
783 list_T *l = luaV_unbox(L, luaV_List, 1);
784 if (lua_isnumber(L, 2)) /* list item? */
785 {
786 listitem_T *li = list_find(l, (long) luaL_checkinteger(L, 2));
787 if (li == NULL)
788 lua_pushnil(L);
789 else
790 luaV_pushtypval(L, &li->li_tv);
791 }
792 else if (lua_isstring(L, 2)) /* method? */
793 {
794 const char *s = lua_tostring(L, 2);
795 if (strncmp(s, "add", 3) == 0
Bram Moolenaarb3766472013-04-15 13:49:21 +0200796 || strncmp(s, "insert", 6) == 0)
Bram Moolenaar1dced572012-04-05 16:54:08 +0200797 {
798 lua_getmetatable(L, 1);
799 lua_getfield(L, -1, s);
800 }
801 else
802 lua_pushnil(L);
803 }
804 else
805 lua_pushnil(L);
806 return 1;
807}
808
809 static int
810luaV_list_newindex (lua_State *L)
811{
812 list_T *l = luaV_unbox(L, luaV_List, 1);
813 long n = (long) luaL_checkinteger(L, 2);
814 listitem_T *li;
815 if (l->lv_lock)
816 luaL_error(L, "list is locked");
817 li = list_find(l, n);
818 if (li == NULL) return 0;
819 if (lua_isnil(L, 3)) /* remove? */
820 {
Bram Moolenaar3ec7f4e2014-05-07 17:31:37 +0200821 vimlist_remove(l, li, li);
Bram Moolenaar1dced572012-04-05 16:54:08 +0200822 clear_tv(&li->li_tv);
823 vim_free(li);
824 }
825 else
826 {
827 typval_T v;
Bram Moolenaarca06da92018-07-01 15:12:05 +0200828 luaV_checktypval(L, 3, &v, "setting list item");
Bram Moolenaar1dced572012-04-05 16:54:08 +0200829 clear_tv(&li->li_tv);
830 copy_tv(&v, &li->li_tv);
Bram Moolenaarb3766472013-04-15 13:49:21 +0200831 clear_tv(&v);
Bram Moolenaar1dced572012-04-05 16:54:08 +0200832 }
833 return 0;
834}
835
836 static int
837luaV_list_add (lua_State *L)
838{
839 luaV_List *lis = luaV_checkudata(L, 1, LUAVIM_LIST);
840 list_T *l = (list_T *) luaV_checkcache(L, (void *) *lis);
Bram Moolenaarb3766472013-04-15 13:49:21 +0200841 typval_T v;
Bram Moolenaar1dced572012-04-05 16:54:08 +0200842 if (l->lv_lock)
843 luaL_error(L, "list is locked");
Bram Moolenaarb3766472013-04-15 13:49:21 +0200844 lua_settop(L, 2);
Bram Moolenaarca06da92018-07-01 15:12:05 +0200845 luaV_checktypval(L, 2, &v, "adding list item");
Bram Moolenaarb3766472013-04-15 13:49:21 +0200846 if (list_append_tv(l, &v) == FAIL)
Bram Moolenaar1dced572012-04-05 16:54:08 +0200847 {
Bram Moolenaarb3766472013-04-15 13:49:21 +0200848 clear_tv(&v);
Bram Moolenaarca06da92018-07-01 15:12:05 +0200849 luaL_error(L, "failed to add item to list");
Bram Moolenaar1dced572012-04-05 16:54:08 +0200850 }
Bram Moolenaarb3766472013-04-15 13:49:21 +0200851 clear_tv(&v);
Bram Moolenaar1dced572012-04-05 16:54:08 +0200852 lua_settop(L, 1);
853 return 1;
854}
855
856 static int
857luaV_list_insert (lua_State *L)
858{
859 luaV_List *lis = luaV_checkudata(L, 1, LUAVIM_LIST);
860 list_T *l = (list_T *) luaV_checkcache(L, (void *) *lis);
Bram Moolenaar46538ee2015-02-17 16:28:55 +0100861 long pos = (long) luaL_optinteger(L, 3, 0);
Bram Moolenaar1dced572012-04-05 16:54:08 +0200862 listitem_T *li = NULL;
863 typval_T v;
864 if (l->lv_lock)
865 luaL_error(L, "list is locked");
866 if (pos < l->lv_len)
867 {
868 li = list_find(l, pos);
869 if (li == NULL)
870 luaL_error(L, "invalid position");
871 }
872 lua_settop(L, 2);
Bram Moolenaarca06da92018-07-01 15:12:05 +0200873 luaV_checktypval(L, 2, &v, "inserting list item");
Bram Moolenaarb3766472013-04-15 13:49:21 +0200874 if (list_insert_tv(l, &v, li) == FAIL)
875 {
876 clear_tv(&v);
Bram Moolenaarca06da92018-07-01 15:12:05 +0200877 luaL_error(L, "failed to add item to list");
Bram Moolenaarb3766472013-04-15 13:49:21 +0200878 }
879 clear_tv(&v);
Bram Moolenaar1dced572012-04-05 16:54:08 +0200880 lua_settop(L, 1);
881 return 1;
882}
883
884static const luaL_Reg luaV_List_mt[] = {
885 {"__tostring", luaV_list_tostring},
Bram Moolenaar1dced572012-04-05 16:54:08 +0200886 {"__len", luaV_list_len},
887 {"__call", luaV_list_call},
888 {"__index", luaV_list_index},
889 {"__newindex", luaV_list_newindex},
890 {"add", luaV_list_add},
891 {"insert", luaV_list_insert},
892 {NULL, NULL}
893};
894
895
896/* ======= Dict type ======= */
897
898 static luaV_Dict *
899luaV_newdict (lua_State *L, dict_T *dic)
900{
901 luaV_Dict *d = (luaV_Dict *) lua_newuserdata(L, sizeof(luaV_Dict));
902 *d = dic;
903 dic->dv_refcount++; /* reference in Lua */
904 luaV_setudata(L, dic); /* cache[dic] = udata */
905 luaV_getfield(L, LUAVIM_DICT);
906 lua_setmetatable(L, -2);
907 return d;
908}
909
910luaV_pushtype(dict_T, dict, luaV_Dict)
911luaV_type_tostring(dict, LUAVIM_DICT)
912
913 static int
Bram Moolenaar1dced572012-04-05 16:54:08 +0200914luaV_dict_len (lua_State *L)
915{
916 dict_T *d = luaV_unbox(L, luaV_Dict, 1);
917 lua_pushinteger(L, (d == NULL) ? 0 : (int) d->dv_hashtab.ht_used);
918 return 1;
919}
920
921 static int
Bram Moolenaarfeeaa682013-02-14 22:19:51 +0100922luaV_dict_iter (lua_State *L UNUSED)
Bram Moolenaar1dced572012-04-05 16:54:08 +0200923{
Bram Moolenaarfeeaa682013-02-14 22:19:51 +0100924#ifdef FEAT_EVAL
Bram Moolenaar1dced572012-04-05 16:54:08 +0200925 hashitem_T *hi = (hashitem_T *) lua_touserdata(L, lua_upvalueindex(2));
926 int n = lua_tointeger(L, lua_upvalueindex(3));
927 dictitem_T *di;
928 if (n <= 0) return 0;
929 while (HASHITEM_EMPTY(hi)) hi++;
930 di = dict_lookup(hi);
931 lua_pushstring(L, (char *) hi->hi_key);
932 luaV_pushtypval(L, &di->di_tv);
933 lua_pushlightuserdata(L, (void *) (hi + 1));
934 lua_replace(L, lua_upvalueindex(2));
935 lua_pushinteger(L, n - 1);
936 lua_replace(L, lua_upvalueindex(3));
937 return 2;
Bram Moolenaarfeeaa682013-02-14 22:19:51 +0100938#else
939 return 0;
940#endif
Bram Moolenaar1dced572012-04-05 16:54:08 +0200941}
942
943 static int
944luaV_dict_call (lua_State *L)
945{
946 dict_T *d = luaV_unbox(L, luaV_Dict, 1);
947 hashtab_T *ht = &d->dv_hashtab;
948 lua_pushvalue(L, lua_upvalueindex(1)); /* pass cache table along */
949 lua_pushlightuserdata(L, (void *) ht->ht_array);
950 lua_pushinteger(L, ht->ht_used); /* # remaining items */
951 lua_pushcclosure(L, luaV_dict_iter, 3);
952 return 1;
953}
954
955 static int
Bram Moolenaarca06da92018-07-01 15:12:05 +0200956luaV_dict_index(lua_State *L)
Bram Moolenaar1dced572012-04-05 16:54:08 +0200957{
958 dict_T *d = luaV_unbox(L, luaV_Dict, 1);
959 char_u *key = (char_u *) luaL_checkstring(L, 2);
960 dictitem_T *di = dict_find(d, key, -1);
Bram Moolenaarca06da92018-07-01 15:12:05 +0200961
Bram Moolenaar1dced572012-04-05 16:54:08 +0200962 if (di == NULL)
963 lua_pushnil(L);
964 else
Bram Moolenaarca06da92018-07-01 15:12:05 +0200965 {
Bram Moolenaar1dced572012-04-05 16:54:08 +0200966 luaV_pushtypval(L, &di->di_tv);
Bram Moolenaarca06da92018-07-01 15:12:05 +0200967 if (di->di_tv.v_type == VAR_FUNC) /* funcref? */
968 {
969 luaV_Funcref *f = (luaV_Funcref *) lua_touserdata(L, -1);
970 f->self = d; /* keep "self" reference */
971 d->dv_refcount++;
972 }
973 }
Bram Moolenaar1dced572012-04-05 16:54:08 +0200974 return 1;
975}
976
977 static int
Bram Moolenaarca06da92018-07-01 15:12:05 +0200978luaV_dict_newindex(lua_State *L)
Bram Moolenaar1dced572012-04-05 16:54:08 +0200979{
980 dict_T *d = luaV_unbox(L, luaV_Dict, 1);
981 char_u *key = (char_u *) luaL_checkstring(L, 2);
982 dictitem_T *di;
Bram Moolenaarca06da92018-07-01 15:12:05 +0200983 typval_T v;
Bram Moolenaar1dced572012-04-05 16:54:08 +0200984 if (d->dv_lock)
985 luaL_error(L, "dict is locked");
Bram Moolenaard6ef5f92018-07-13 22:08:23 +0200986 if (key == NULL)
987 return 0;
988 if (*key == NUL)
Bram Moolenaarca06da92018-07-01 15:12:05 +0200989 luaL_error(L, "empty key");
Bram Moolenaar17413672018-07-14 20:49:42 +0200990 if (!lua_isnil(L, 3)) /* read value? */
991 {
Bram Moolenaarca06da92018-07-01 15:12:05 +0200992 luaV_checktypval(L, 3, &v, "setting dict item");
993 if (d->dv_scope == VAR_DEF_SCOPE && v.v_type == VAR_FUNC)
994 luaL_error(L, "cannot assign funcref to builtin scope");
995 }
Bram Moolenaar1dced572012-04-05 16:54:08 +0200996 di = dict_find(d, key, -1);
997 if (di == NULL) /* non-existing key? */
998 {
Bram Moolenaard6ef5f92018-07-13 22:08:23 +0200999 if (lua_isnil(L, 3))
1000 return 0;
Bram Moolenaar1dced572012-04-05 16:54:08 +02001001 di = dictitem_alloc(key);
Bram Moolenaard6ef5f92018-07-13 22:08:23 +02001002 if (di == NULL)
1003 return 0;
Bram Moolenaar1dced572012-04-05 16:54:08 +02001004 if (dict_add(d, di) == FAIL)
1005 {
Bram Moolenaard6ef5f92018-07-13 22:08:23 +02001006 vim_free(di);
1007 return 0;
Bram Moolenaar1dced572012-04-05 16:54:08 +02001008 }
1009 }
1010 else
1011 clear_tv(&di->di_tv);
1012 if (lua_isnil(L, 3)) /* remove? */
1013 {
1014 hashitem_T *hi = hash_find(&d->dv_hashtab, di->di_key);
1015 hash_remove(&d->dv_hashtab, hi);
1016 dictitem_free(di);
1017 }
Bram Moolenaarca06da92018-07-01 15:12:05 +02001018 else
1019 {
Bram Moolenaar1dced572012-04-05 16:54:08 +02001020 copy_tv(&v, &di->di_tv);
Bram Moolenaarb3766472013-04-15 13:49:21 +02001021 clear_tv(&v);
Bram Moolenaar1dced572012-04-05 16:54:08 +02001022 }
1023 return 0;
1024}
1025
1026static const luaL_Reg luaV_Dict_mt[] = {
1027 {"__tostring", luaV_dict_tostring},
Bram Moolenaar1dced572012-04-05 16:54:08 +02001028 {"__len", luaV_dict_len},
1029 {"__call", luaV_dict_call},
1030 {"__index", luaV_dict_index},
1031 {"__newindex", luaV_dict_newindex},
1032 {NULL, NULL}
1033};
1034
1035
Bram Moolenaarca06da92018-07-01 15:12:05 +02001036/* ======= Funcref type ======= */
1037
1038 static luaV_Funcref *
1039luaV_newfuncref(lua_State *L, char_u *name)
1040{
1041 luaV_Funcref *f = (luaV_Funcref *)lua_newuserdata(L, sizeof(luaV_Funcref));
1042
1043 if (name != NULL)
1044 {
1045 func_ref(name); /* as in copy_tv */
1046 f->tv.vval.v_string = vim_strsave(name);
1047 }
1048 f->tv.v_type = VAR_FUNC;
1049 f->args.v_type = VAR_LIST;
1050 f->self = NULL;
1051 luaV_getfield(L, LUAVIM_FUNCREF);
1052 lua_setmetatable(L, -2);
1053 return f;
1054}
1055
1056 static luaV_Funcref *
1057luaV_pushfuncref(lua_State *L, typval_T *tv)
1058{
1059 luaV_Funcref *f = luaV_newfuncref(L, NULL);
1060 copy_tv(tv, &f->tv);
1061 clear_tv(tv);
1062 return f;
1063}
1064
1065
1066luaV_type_tostring(funcref, LUAVIM_FUNCREF)
1067
1068 static int
1069luaV_funcref_gc(lua_State *L)
1070{
1071 luaV_Funcref *f = (luaV_Funcref *) lua_touserdata(L, 1);
1072
1073 func_unref(f->tv.vval.v_string);
1074 vim_free(f->tv.vval.v_string);
1075 dict_unref(f->self);
1076 return 0;
1077}
1078
1079/* equivalent to string(funcref) */
1080 static int
1081luaV_funcref_len(lua_State *L)
1082{
1083 luaV_Funcref *f = (luaV_Funcref *) lua_touserdata(L, 1);
1084
1085 lua_pushstring(L, (const char *) f->tv.vval.v_string);
1086 return 1;
1087}
1088
1089 static int
1090luaV_funcref_call(lua_State *L)
1091{
1092 luaV_Funcref *f = (luaV_Funcref *) lua_touserdata(L, 1);
1093 int i, n = lua_gettop(L) - 1; /* #args */
1094 int status;
1095 typval_T v, rettv;
1096
1097 f->args.vval.v_list = list_alloc();
1098 rettv.v_type = VAR_UNKNOWN; /* as in clear_tv */
Bram Moolenaard6ef5f92018-07-13 22:08:23 +02001099 if (f->args.vval.v_list == NULL)
1100 status = FAIL;
1101 else
1102 {
Bram Moolenaar17413672018-07-14 20:49:42 +02001103 for (i = 0; i < n; i++)
1104 {
Bram Moolenaard6ef5f92018-07-13 22:08:23 +02001105 luaV_checktypval(L, i + 2, &v, "calling funcref");
1106 list_append_tv(f->args.vval.v_list, &v);
1107 }
1108 status = func_call(f->tv.vval.v_string, &f->args,
1109 NULL, f->self, &rettv);
1110 if (status == OK)
1111 luaV_pushtypval(L, &rettv);
1112 clear_tv(&f->args);
1113 clear_tv(&rettv);
Bram Moolenaarca06da92018-07-01 15:12:05 +02001114 }
Bram Moolenaarca06da92018-07-01 15:12:05 +02001115 if (status != OK)
1116 luaL_error(L, "cannot call funcref");
1117 return 1;
1118}
1119
1120static const luaL_Reg luaV_Funcref_mt[] = {
1121 {"__tostring", luaV_funcref_tostring},
1122 {"__gc", luaV_funcref_gc},
1123 {"__len", luaV_funcref_len},
1124 {"__call", luaV_funcref_call},
1125 {NULL, NULL}
1126};
1127
1128
Bram Moolenaar1dced572012-04-05 16:54:08 +02001129/* ======= Buffer type ======= */
1130
1131luaV_newtype(buf_T, buffer, luaV_Buffer, LUAVIM_BUFFER)
1132luaV_pushtype(buf_T, buffer, luaV_Buffer)
1133luaV_type_tostring(buffer, LUAVIM_BUFFER)
1134
1135 static int
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001136luaV_buffer_len(lua_State *L)
1137{
Bram Moolenaar1dced572012-04-05 16:54:08 +02001138 buf_T *b = (buf_T *) luaV_checkvalid(L, luaV_Buffer, 1);
1139 lua_pushinteger(L, b->b_ml.ml_line_count);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001140 return 1;
1141}
1142
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001143 static int
1144luaV_buffer_call(lua_State *L)
1145{
Bram Moolenaar1dced572012-04-05 16:54:08 +02001146 buf_T *b = (buf_T *) luaV_checkvalid(L, luaV_Buffer, 1);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001147 lua_settop(L, 1);
Bram Moolenaar1dced572012-04-05 16:54:08 +02001148 set_curbuf(b, DOBUF_SPLIT);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001149 return 1;
1150}
1151
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001152 static int
1153luaV_buffer_index(lua_State *L)
1154{
Bram Moolenaar1dced572012-04-05 16:54:08 +02001155 buf_T *b = (buf_T *) luaV_checkvalid(L, luaV_Buffer, 1);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001156 linenr_T n = (linenr_T) lua_tointeger(L, 2);
Bram Moolenaar1dced572012-04-05 16:54:08 +02001157 if (n > 0 && n <= b->b_ml.ml_line_count)
1158 luaV_pushline(L, b, n);
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001159 else if (lua_isstring(L, 2))
1160 {
1161 const char *s = lua_tostring(L, 2);
1162 if (strncmp(s, "name", 4) == 0)
Bram Moolenaarfe08df42018-07-07 23:07:41 +02001163 lua_pushstring(L, (b->b_sfname == NULL)
1164 ? "" : (char *) b->b_sfname);
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001165 else if (strncmp(s, "fname", 5) == 0)
Bram Moolenaarfe08df42018-07-07 23:07:41 +02001166 lua_pushstring(L, (b->b_ffname == NULL)
1167 ? "" : (char *) b->b_ffname);
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001168 else if (strncmp(s, "number", 6) == 0)
Bram Moolenaar1dced572012-04-05 16:54:08 +02001169 lua_pushinteger(L, b->b_fnum);
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001170 /* methods */
1171 else if (strncmp(s, "insert", 6) == 0
1172 || strncmp(s, "next", 4) == 0
1173 || strncmp(s, "previous", 8) == 0
1174 || strncmp(s, "isvalid", 7) == 0)
1175 {
1176 lua_getmetatable(L, 1);
1177 lua_getfield(L, -1, s);
1178 }
1179 else
1180 lua_pushnil(L);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001181 }
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001182 else
1183 lua_pushnil(L);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001184 return 1;
1185}
1186
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001187 static int
1188luaV_buffer_newindex(lua_State *L)
1189{
Bram Moolenaar1dced572012-04-05 16:54:08 +02001190 buf_T *b = (buf_T *) luaV_checkvalid(L, luaV_Buffer, 1);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001191 linenr_T n = (linenr_T) luaL_checkinteger(L, 2);
1192#ifdef HAVE_SANDBOX
1193 luaV_checksandbox(L);
1194#endif
Bram Moolenaar1dced572012-04-05 16:54:08 +02001195 if (n < 1 || n > b->b_ml.ml_line_count)
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001196 luaL_error(L, "invalid line number");
1197 if (lua_isnil(L, 3)) /* delete line */
1198 {
1199 buf_T *buf = curbuf;
Bram Moolenaar1dced572012-04-05 16:54:08 +02001200 curbuf = b;
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001201 if (u_savedel(n, 1L) == FAIL)
1202 {
1203 curbuf = buf;
1204 luaL_error(L, "cannot save undo information");
1205 }
1206 else if (ml_delete(n, FALSE) == FAIL)
1207 {
1208 curbuf = buf;
1209 luaL_error(L, "cannot delete line");
1210 }
Bram Moolenaarca06da92018-07-01 15:12:05 +02001211 else
1212 {
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001213 deleted_lines_mark(n, 1L);
Bram Moolenaar1dced572012-04-05 16:54:08 +02001214 if (b == curwin->w_buffer) /* fix cursor in current window? */
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001215 {
1216 if (curwin->w_cursor.lnum >= n)
1217 {
1218 if (curwin->w_cursor.lnum > n)
1219 {
1220 curwin->w_cursor.lnum -= 1;
1221 check_cursor_col();
1222 }
1223 else check_cursor();
1224 changed_cline_bef_curs();
1225 }
1226 invalidate_botline();
1227 }
1228 }
1229 curbuf = buf;
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001230 }
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001231 else if (lua_isstring(L, 3)) /* update line */
1232 {
1233 buf_T *buf = curbuf;
Bram Moolenaar1dced572012-04-05 16:54:08 +02001234 curbuf = b;
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001235 if (u_savesub(n) == FAIL)
1236 {
1237 curbuf = buf;
1238 luaL_error(L, "cannot save undo information");
1239 }
1240 else if (ml_replace(n, luaV_toline(L, 3), TRUE) == FAIL)
1241 {
1242 curbuf = buf;
1243 luaL_error(L, "cannot replace line");
1244 }
1245 else changed_bytes(n, 0);
1246 curbuf = buf;
Bram Moolenaar1dced572012-04-05 16:54:08 +02001247 if (b == curwin->w_buffer)
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001248 check_cursor_col();
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001249 }
1250 else
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001251 luaL_error(L, "wrong argument to change line");
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001252 return 0;
1253}
1254
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001255 static int
1256luaV_buffer_insert(lua_State *L)
1257{
Bram Moolenaar1dced572012-04-05 16:54:08 +02001258 luaV_Buffer *lb = luaV_checkudata(L, 1, LUAVIM_BUFFER);
1259 buf_T *b = (buf_T *) luaV_checkcache(L, (void *) *lb);
1260 linenr_T last = b->b_ml.ml_line_count;
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001261 linenr_T n = (linenr_T) luaL_optinteger(L, 3, last);
1262 buf_T *buf;
1263 luaL_checktype(L, 2, LUA_TSTRING);
1264#ifdef HAVE_SANDBOX
1265 luaV_checksandbox(L);
1266#endif
1267 /* fix insertion line */
1268 if (n < 0) n = 0;
1269 if (n > last) n = last;
1270 /* insert */
1271 buf = curbuf;
Bram Moolenaar1dced572012-04-05 16:54:08 +02001272 curbuf = b;
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001273 if (u_save(n, n + 1) == FAIL)
1274 {
1275 curbuf = buf;
1276 luaL_error(L, "cannot save undo information");
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001277 }
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001278 else if (ml_append(n, luaV_toline(L, 2), 0, FALSE) == FAIL)
1279 {
1280 curbuf = buf;
1281 luaL_error(L, "cannot insert line");
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001282 }
1283 else
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001284 appended_lines_mark(n, 1L);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001285 curbuf = buf;
1286 update_screen(VALID);
1287 return 0;
1288}
1289
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001290 static int
1291luaV_buffer_next(lua_State *L)
1292{
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001293 luaV_Buffer *b = luaV_checkudata(L, 1, LUAVIM_BUFFER);
Bram Moolenaar1dced572012-04-05 16:54:08 +02001294 buf_T *buf = (buf_T *) luaV_checkcache(L, (void *) *b);
1295 luaV_pushbuffer(L, buf->b_next);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001296 return 1;
1297}
1298
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001299 static int
1300luaV_buffer_previous(lua_State *L)
1301{
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001302 luaV_Buffer *b = luaV_checkudata(L, 1, LUAVIM_BUFFER);
Bram Moolenaar1dced572012-04-05 16:54:08 +02001303 buf_T *buf = (buf_T *) luaV_checkcache(L, (void *) *b);
1304 luaV_pushbuffer(L, buf->b_prev);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001305 return 1;
1306}
1307
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001308 static int
1309luaV_buffer_isvalid(lua_State *L)
1310{
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001311 luaV_Buffer *b = luaV_checkudata(L, 1, LUAVIM_BUFFER);
Bram Moolenaar1dced572012-04-05 16:54:08 +02001312 luaV_getudata(L, *b);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001313 lua_pushboolean(L, !lua_isnil(L, -1));
1314 return 1;
1315}
1316
1317static const luaL_Reg luaV_Buffer_mt[] = {
1318 {"__tostring", luaV_buffer_tostring},
1319 {"__len", luaV_buffer_len},
1320 {"__call", luaV_buffer_call},
1321 {"__index", luaV_buffer_index},
1322 {"__newindex", luaV_buffer_newindex},
1323 {"insert", luaV_buffer_insert},
1324 {"next", luaV_buffer_next},
1325 {"previous", luaV_buffer_previous},
1326 {"isvalid", luaV_buffer_isvalid},
1327 {NULL, NULL}
1328};
1329
1330
1331/* ======= Window type ======= */
1332
Bram Moolenaar1dced572012-04-05 16:54:08 +02001333luaV_newtype(win_T, window, luaV_Window, LUAVIM_WINDOW)
1334luaV_pushtype(win_T, window, luaV_Window)
1335luaV_type_tostring(window, LUAVIM_WINDOW)
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001336
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001337 static int
1338luaV_window_call(lua_State *L)
1339{
Bram Moolenaar1dced572012-04-05 16:54:08 +02001340 win_T *w = (win_T *) luaV_checkvalid(L, luaV_Window, 1);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001341 lua_settop(L, 1);
Bram Moolenaar1dced572012-04-05 16:54:08 +02001342 win_goto(w);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001343 return 1;
1344}
1345
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001346 static int
1347luaV_window_index(lua_State *L)
1348{
Bram Moolenaar1dced572012-04-05 16:54:08 +02001349 win_T *w = (win_T *) luaV_checkvalid(L, luaV_Window, 1);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001350 const char *s = luaL_checkstring(L, 2);
1351 if (strncmp(s, "buffer", 6) == 0)
Bram Moolenaar1dced572012-04-05 16:54:08 +02001352 luaV_pushbuffer(L, w->w_buffer);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001353 else if (strncmp(s, "line", 4) == 0)
Bram Moolenaar1dced572012-04-05 16:54:08 +02001354 lua_pushinteger(L, w->w_cursor.lnum);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001355 else if (strncmp(s, "col", 3) == 0)
Bram Moolenaar1dced572012-04-05 16:54:08 +02001356 lua_pushinteger(L, w->w_cursor.col + 1);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001357 else if (strncmp(s, "width", 5) == 0)
Bram Moolenaar02631462017-09-22 15:20:32 +02001358 lua_pushinteger(L, w->w_width);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001359 else if (strncmp(s, "height", 6) == 0)
Bram Moolenaar1dced572012-04-05 16:54:08 +02001360 lua_pushinteger(L, w->w_height);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001361 /* methods */
1362 else if (strncmp(s, "next", 4) == 0
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001363 || strncmp(s, "previous", 8) == 0
1364 || strncmp(s, "isvalid", 7) == 0)
1365 {
1366 lua_getmetatable(L, 1);
1367 lua_getfield(L, -1, s);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001368 }
1369 else
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001370 lua_pushnil(L);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001371 return 1;
1372}
1373
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001374 static int
1375luaV_window_newindex (lua_State *L)
1376{
Bram Moolenaar1dced572012-04-05 16:54:08 +02001377 win_T *w = (win_T *) luaV_checkvalid(L, luaV_Window, 1);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001378 const char *s = luaL_checkstring(L, 2);
1379 int v = luaL_checkinteger(L, 3);
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001380 if (strncmp(s, "line", 4) == 0)
1381 {
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001382#ifdef HAVE_SANDBOX
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001383 luaV_checksandbox(L);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001384#endif
Bram Moolenaar1dced572012-04-05 16:54:08 +02001385 if (v < 1 || v > w->w_buffer->b_ml.ml_line_count)
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001386 luaL_error(L, "line out of range");
Bram Moolenaar1dced572012-04-05 16:54:08 +02001387 w->w_cursor.lnum = v;
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001388 update_screen(VALID);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001389 }
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001390 else if (strncmp(s, "col", 3) == 0)
1391 {
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001392#ifdef HAVE_SANDBOX
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001393 luaV_checksandbox(L);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001394#endif
Bram Moolenaar1dced572012-04-05 16:54:08 +02001395 w->w_cursor.col = v - 1;
Bram Moolenaar53901442018-07-25 22:02:36 +02001396 w->w_set_curswant = TRUE;
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001397 update_screen(VALID);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001398 }
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001399 else if (strncmp(s, "width", 5) == 0)
1400 {
1401 win_T *win = curwin;
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001402#ifdef FEAT_GUI
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001403 need_mouse_correct = TRUE;
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001404#endif
Bram Moolenaar1dced572012-04-05 16:54:08 +02001405 curwin = w;
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001406 win_setwidth(v);
1407 curwin = win;
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001408 }
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001409 else if (strncmp(s, "height", 6) == 0)
1410 {
1411 win_T *win = curwin;
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001412#ifdef FEAT_GUI
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001413 need_mouse_correct = TRUE;
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001414#endif
Bram Moolenaar1dced572012-04-05 16:54:08 +02001415 curwin = w;
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001416 win_setheight(v);
1417 curwin = win;
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001418 }
1419 else
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001420 luaL_error(L, "invalid window property: `%s'", s);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001421 return 0;
1422}
1423
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001424 static int
1425luaV_window_next(lua_State *L)
1426{
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001427 luaV_Window *w = luaV_checkudata(L, 1, LUAVIM_WINDOW);
Bram Moolenaar1dced572012-04-05 16:54:08 +02001428 win_T *win = (win_T *) luaV_checkcache(L, (void *) *w);
1429 luaV_pushwindow(L, win->w_next);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001430 return 1;
1431}
1432
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001433 static int
1434luaV_window_previous(lua_State *L)
1435{
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001436 luaV_Window *w = luaV_checkudata(L, 1, LUAVIM_WINDOW);
Bram Moolenaar1dced572012-04-05 16:54:08 +02001437 win_T *win = (win_T *) luaV_checkcache(L, (void *) *w);
1438 luaV_pushwindow(L, win->w_prev);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001439 return 1;
1440}
1441
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001442 static int
1443luaV_window_isvalid(lua_State *L)
1444{
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001445 luaV_Window *w = luaV_checkudata(L, 1, LUAVIM_WINDOW);
Bram Moolenaar1dced572012-04-05 16:54:08 +02001446 luaV_getudata(L, *w);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001447 lua_pushboolean(L, !lua_isnil(L, -1));
1448 return 1;
1449}
1450
1451static const luaL_Reg luaV_Window_mt[] = {
1452 {"__tostring", luaV_window_tostring},
1453 {"__call", luaV_window_call},
1454 {"__index", luaV_window_index},
1455 {"__newindex", luaV_window_newindex},
1456 {"next", luaV_window_next},
1457 {"previous", luaV_window_previous},
1458 {"isvalid", luaV_window_isvalid},
1459 {NULL, NULL}
1460};
1461
1462
1463/* ======= Vim module ======= */
1464
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001465 static int
1466luaV_print(lua_State *L)
1467{
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001468 int i, n = lua_gettop(L); /* nargs */
1469 const char *s;
1470 size_t l;
1471 luaL_Buffer b;
1472 luaL_buffinit(L, &b);
1473 lua_getglobal(L, "tostring");
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001474 for (i = 1; i <= n; i++)
1475 {
1476 lua_pushvalue(L, -1); /* tostring */
1477 lua_pushvalue(L, i); /* arg */
1478 lua_call(L, 1, 1);
1479 s = lua_tolstring(L, -1, &l);
1480 if (s == NULL)
1481 return luaL_error(L, "cannot convert to string");
1482 if (i > 1) luaL_addchar(&b, ' '); /* use space instead of tab */
1483 luaV_addlstring(&b, s, l, 0);
1484 lua_pop(L, 1);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001485 }
1486 luaL_pushresult(&b);
1487 luaV_msg(L);
1488 return 0;
1489}
1490
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001491 static int
Bram Moolenaar38e2b062011-09-21 17:15:39 +02001492luaV_debug(lua_State *L)
1493{
1494 lua_settop(L, 0);
1495 lua_getglobal(L, "vim");
1496 lua_getfield(L, -1, "eval");
1497 lua_remove(L, -2); /* vim.eval at position 1 */
1498 for (;;)
1499 {
1500 const char *input;
1501 size_t l;
1502 lua_pushvalue(L, 1); /* vim.eval */
1503 lua_pushliteral(L, "input('lua_debug> ')");
1504 lua_call(L, 1, 1); /* return string */
1505 input = lua_tolstring(L, -1, &l);
1506 if (l == 0 || strcmp(input, "cont") == 0)
1507 return 0;
1508 msg_putchar('\n'); /* avoid outputting on input line */
1509 if (luaL_loadbuffer(L, input, l, "=(debug command)")
1510 || lua_pcall(L, 0, 0, 0))
1511 luaV_emsg(L);
1512 lua_settop(L, 1); /* remove eventual returns, but keep vim.eval */
1513 }
1514}
1515
1516 static int
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001517luaV_command(lua_State *L)
1518{
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001519 do_cmdline_cmd((char_u *) luaL_checkstring(L, 1));
1520 update_screen(VALID);
1521 return 0;
1522}
1523
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001524 static int
1525luaV_eval(lua_State *L)
1526{
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001527 typval_T *tv = eval_expr((char_u *) luaL_checkstring(L, 1), NULL);
1528 if (tv == NULL) luaL_error(L, "invalid expression");
1529 luaV_pushtypval(L, tv);
Bram Moolenaarb3766472013-04-15 13:49:21 +02001530 free_tv(tv);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001531 return 1;
1532}
1533
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001534 static int
Bram Moolenaar0d2e4fc2010-07-18 12:35:47 +02001535luaV_beep(lua_State *L UNUSED)
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001536{
Bram Moolenaar165bc692015-07-21 17:53:25 +02001537 vim_beep(BO_LANG);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001538 return 0;
1539}
1540
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001541 static int
1542luaV_line(lua_State *L)
1543{
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001544 luaV_pushline(L, curbuf, curwin->w_cursor.lnum);
1545 return 1;
1546}
1547
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001548 static int
Bram Moolenaar1dced572012-04-05 16:54:08 +02001549luaV_list(lua_State *L)
1550{
Bram Moolenaarca06da92018-07-01 15:12:05 +02001551 list_T *l;
1552 int initarg = !lua_isnoneornil(L, 1);
1553
1554 if (initarg && lua_type(L, 1) != LUA_TTABLE)
1555 luaL_error(L, "table expected, got %s", luaL_typename(L, 1));
1556 l = list_alloc();
Bram Moolenaar1dced572012-04-05 16:54:08 +02001557 if (l == NULL)
1558 lua_pushnil(L);
1559 else
Bram Moolenaarca06da92018-07-01 15:12:05 +02001560 {
Bram Moolenaar1dced572012-04-05 16:54:08 +02001561 luaV_newlist(L, l);
Bram Moolenaar17413672018-07-14 20:49:42 +02001562 if (initarg) /* traverse table to init list */
1563 {
Bram Moolenaarca06da92018-07-01 15:12:05 +02001564 int notnil, i = 0;
1565 typval_T v;
Bram Moolenaar17413672018-07-14 20:49:42 +02001566 do
1567 {
Bram Moolenaarca06da92018-07-01 15:12:05 +02001568 lua_rawgeti(L, 1, ++i);
1569 notnil = !lua_isnil(L, -1);
Bram Moolenaar17413672018-07-14 20:49:42 +02001570 if (notnil)
1571 {
Bram Moolenaarca06da92018-07-01 15:12:05 +02001572 luaV_checktypval(L, -1, &v, "vim.list");
1573 list_append_tv(l, &v);
1574 }
1575 lua_pop(L, 1); /* value */
1576 } while (notnil);
1577 }
1578 }
Bram Moolenaar1dced572012-04-05 16:54:08 +02001579 return 1;
1580}
1581
1582 static int
1583luaV_dict(lua_State *L)
1584{
Bram Moolenaarca06da92018-07-01 15:12:05 +02001585 dict_T *d;
1586 int initarg = !lua_isnoneornil(L, 1);
1587
1588 if (initarg && lua_type(L, 1) != LUA_TTABLE)
1589 luaL_error(L, "table expected, got %s", luaL_typename(L, 1));
1590 d = dict_alloc();
Bram Moolenaar1dced572012-04-05 16:54:08 +02001591 if (d == NULL)
1592 lua_pushnil(L);
1593 else
Bram Moolenaarca06da92018-07-01 15:12:05 +02001594 {
Bram Moolenaar1dced572012-04-05 16:54:08 +02001595 luaV_newdict(L, d);
Bram Moolenaarca06da92018-07-01 15:12:05 +02001596 if (initarg) /* traverse table to init dict */
1597 {
1598 lua_pushnil(L);
1599 while (lua_next(L, 1))
1600 {
1601 char_u *key;
1602 dictitem_T *di;
1603 typval_T v;
Bram Moolenaard6ef5f92018-07-13 22:08:23 +02001604
Bram Moolenaarca06da92018-07-01 15:12:05 +02001605 lua_pushvalue(L, -2); /* dup key in case it's a number */
1606 key = (char_u *) lua_tostring(L, -1);
Bram Moolenaard6ef5f92018-07-13 22:08:23 +02001607 if (key == NULL)
1608 {
1609 lua_pushnil(L);
1610 return 1;
1611 }
1612 if (*key == NUL)
Bram Moolenaarca06da92018-07-01 15:12:05 +02001613 luaL_error(L, "table has empty key");
1614 luaV_checktypval(L, -2, &v, "vim.dict"); /* value */
1615 di = dictitem_alloc(key);
Bram Moolenaard6ef5f92018-07-13 22:08:23 +02001616 if (di == NULL || dict_add(d, di) == FAIL)
1617 {
Bram Moolenaarca06da92018-07-01 15:12:05 +02001618 vim_free(di);
1619 lua_pushnil(L);
1620 return 1;
1621 }
1622 copy_tv(&v, &di->di_tv);
1623 clear_tv(&v);
1624 lua_pop(L, 2); /* key copy and value */
1625 }
1626 }
1627 }
1628 return 1;
1629}
1630
1631 static int
1632luaV_funcref(lua_State *L)
1633{
1634 const char *name = luaL_checkstring(L, 1);
1635 /* note: not checking if function exists (needs function_exists) */
1636 if (name == NULL || *name == NUL || VIM_ISDIGIT(*name))
1637 luaL_error(L, "invalid function name: %s", name);
1638 luaV_newfuncref(L, (char_u *) name);
Bram Moolenaar1dced572012-04-05 16:54:08 +02001639 return 1;
1640}
1641
1642 static int
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001643luaV_buffer(lua_State *L)
1644{
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001645 buf_T *buf;
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001646 if (lua_isstring(L, 1)) /* get by number or name? */
1647 {
1648 if (lua_isnumber(L, 1)) /* by number? */
1649 {
1650 int n = lua_tointeger(L, 1);
Bram Moolenaar29323592016-07-24 22:04:11 +02001651 FOR_ALL_BUFFERS(buf)
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001652 if (buf->b_fnum == n) break;
1653 }
Bram Moolenaarca06da92018-07-01 15:12:05 +02001654 else // by name
1655 {
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001656 size_t l;
1657 const char *s = lua_tolstring(L, 1, &l);
Bram Moolenaar29323592016-07-24 22:04:11 +02001658 FOR_ALL_BUFFERS(buf)
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001659 {
1660 if (buf->b_ffname == NULL || buf->b_sfname == NULL)
1661 {
1662 if (l == 0) break;
1663 }
Bram Moolenaar0d2e4fc2010-07-18 12:35:47 +02001664 else if (strncmp(s, (char *)buf->b_ffname, l) == 0
1665 || strncmp(s, (char *)buf->b_sfname, l) == 0)
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001666 break;
1667 }
1668 }
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 buf = (lua_toboolean(L, 1)) ? firstbuf : curbuf; /* first buffer? */
Bram Moolenaar1dced572012-04-05 16:54:08 +02001672 luaV_pushbuffer(L, buf);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001673 return 1;
1674}
1675
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001676 static int
1677luaV_window(lua_State *L)
1678{
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001679 win_T *win;
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001680 if (lua_isnumber(L, 1)) /* get by number? */
1681 {
1682 int n = lua_tointeger(L, 1);
1683 for (win = firstwin; win != NULL; win = win->w_next, n--)
1684 if (n == 1) break;
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001685 }
Bram Moolenaar1dced572012-04-05 16:54:08 +02001686 else
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001687 win = (lua_toboolean(L, 1)) ? firstwin : curwin; /* first window? */
Bram Moolenaar1dced572012-04-05 16:54:08 +02001688 luaV_pushwindow(L, win);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001689 return 1;
1690}
1691
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001692 static int
1693luaV_open(lua_State *L)
1694{
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001695 char_u *s = NULL;
1696#ifdef HAVE_SANDBOX
1697 luaV_checksandbox(L);
1698#endif
1699 if (lua_isstring(L, 1)) s = (char_u *) lua_tostring(L, 1);
Bram Moolenaarfa263a52011-12-08 16:00:16 +01001700 luaV_pushbuffer(L, buflist_new(s, NULL, 1L, BLN_LISTED));
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001701 return 1;
1702}
1703
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001704 static int
Bram Moolenaar1dced572012-04-05 16:54:08 +02001705luaV_type(lua_State *L)
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001706{
Bram Moolenaar1dced572012-04-05 16:54:08 +02001707 luaL_checkany(L, 1);
1708 if (lua_type(L, 1) == LUA_TUSERDATA) /* check vim udata? */
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001709 {
Bram Moolenaar1dced572012-04-05 16:54:08 +02001710 lua_settop(L, 1);
1711 if (lua_getmetatable(L, 1))
1712 {
1713 luaV_getfield(L, LUAVIM_LIST);
1714 if (lua_rawequal(L, -1, 2))
1715 {
1716 lua_pushstring(L, "list");
1717 return 1;
1718 }
1719 luaV_getfield(L, LUAVIM_DICT);
1720 if (lua_rawequal(L, -1, 2))
1721 {
1722 lua_pushstring(L, "dict");
1723 return 1;
1724 }
Bram Moolenaarca06da92018-07-01 15:12:05 +02001725 luaV_getfield(L, LUAVIM_FUNCREF);
1726 if (lua_rawequal(L, -1, 2))
1727 {
1728 lua_pushstring(L, "funcref");
1729 return 1;
1730 }
Bram Moolenaar1dced572012-04-05 16:54:08 +02001731 luaV_getfield(L, LUAVIM_BUFFER);
1732 if (lua_rawequal(L, -1, 2))
1733 {
1734 lua_pushstring(L, "buffer");
1735 return 1;
1736 }
1737 luaV_getfield(L, LUAVIM_WINDOW);
1738 if (lua_rawequal(L, -1, 2))
1739 {
1740 lua_pushstring(L, "window");
1741 return 1;
1742 }
1743 }
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001744 }
Bram Moolenaar1dced572012-04-05 16:54:08 +02001745 lua_pushstring(L, luaL_typename(L, 1)); /* fallback */
1746 return 1;
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001747}
1748
1749static const luaL_Reg luaV_module[] = {
1750 {"command", luaV_command},
1751 {"eval", luaV_eval},
1752 {"beep", luaV_beep},
1753 {"line", luaV_line},
Bram Moolenaar1dced572012-04-05 16:54:08 +02001754 {"list", luaV_list},
1755 {"dict", luaV_dict},
Bram Moolenaarca06da92018-07-01 15:12:05 +02001756 {"funcref", luaV_funcref},
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001757 {"buffer", luaV_buffer},
1758 {"window", luaV_window},
1759 {"open", luaV_open},
Bram Moolenaar1dced572012-04-05 16:54:08 +02001760 {"type", luaV_type},
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001761 {NULL, NULL}
1762};
1763
Bram Moolenaar1dced572012-04-05 16:54:08 +02001764/* for freeing list, dict, buffer and window objects; lightuserdata as arg */
1765 static int
1766luaV_free(lua_State *L)
1767{
1768 lua_pushnil(L);
1769 luaV_setudata(L, lua_touserdata(L, 1));
1770 return 0;
1771}
1772
1773 static int
1774luaV_luaeval (lua_State *L)
1775{
1776 luaL_Buffer b;
1777 size_t l;
1778 const char *str = lua_tolstring(L, 1, &l);
1779 typval_T *arg = (typval_T *) lua_touserdata(L, 2);
1780 typval_T *rettv = (typval_T *) lua_touserdata(L, 3);
1781 luaL_buffinit(L, &b);
1782 luaL_addlstring(&b, LUAVIM_EVALHEADER, sizeof(LUAVIM_EVALHEADER) - 1);
1783 luaL_addlstring(&b, str, l);
1784 luaL_pushresult(&b);
1785 str = lua_tolstring(L, -1, &l);
1786 if (luaL_loadbuffer(L, str, l, LUAVIM_EVALNAME)) /* compile error? */
1787 {
1788 luaV_emsg(L);
1789 return 0;
1790 }
1791 luaV_pushtypval(L, arg);
1792 if (lua_pcall(L, 1, 1, 0)) /* running error? */
1793 {
1794 luaV_emsg(L);
1795 return 0;
1796 }
Bram Moolenaarca06da92018-07-01 15:12:05 +02001797 if (luaV_totypval(L, -1, rettv) == FAIL)
1798 EMSG("luaeval: cannot convert value");
Bram Moolenaarf554a322015-02-04 23:08:01 +01001799 return 0;
Bram Moolenaar1dced572012-04-05 16:54:08 +02001800}
1801
1802 static int
1803luaV_setref (lua_State *L)
1804{
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01001805 int copyID = lua_tointeger(L, 1);
1806 int abort = FALSE;
1807 typval_T tv;
1808
Bram Moolenaar1dced572012-04-05 16:54:08 +02001809 luaV_getfield(L, LUAVIM_LIST);
1810 luaV_getfield(L, LUAVIM_DICT);
1811 lua_pushnil(L);
Bram Moolenaarb84634d2015-02-04 22:02:37 +01001812 /* traverse cache table */
1813 while (!abort && lua_next(L, lua_upvalueindex(1)) != 0)
Bram Moolenaar1dced572012-04-05 16:54:08 +02001814 {
1815 lua_getmetatable(L, -1);
1816 if (lua_rawequal(L, -1, 2)) /* list? */
1817 {
1818 tv.v_type = VAR_LIST;
1819 tv.vval.v_list = (list_T *) lua_touserdata(L, 4); /* key */
Bram Moolenaarf609dcf2015-12-03 17:43:17 +01001820 abort = set_ref_in_item(&tv, copyID, NULL, NULL);
Bram Moolenaar1dced572012-04-05 16:54:08 +02001821 }
1822 else if (lua_rawequal(L, -1, 3)) /* dict? */
1823 {
1824 tv.v_type = VAR_DICT;
1825 tv.vval.v_dict = (dict_T *) lua_touserdata(L, 4); /* key */
Bram Moolenaarf609dcf2015-12-03 17:43:17 +01001826 abort = set_ref_in_item(&tv, copyID, NULL, NULL);
Bram Moolenaar1dced572012-04-05 16:54:08 +02001827 }
1828 lua_pop(L, 2); /* metatable and value */
Bram Moolenaar1dced572012-04-05 16:54:08 +02001829 }
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01001830 lua_pushinteger(L, abort);
Bram Moolenaarf554a322015-02-04 23:08:01 +01001831 return 1;
Bram Moolenaar1dced572012-04-05 16:54:08 +02001832}
1833
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001834 static int
1835luaopen_vim(lua_State *L)
1836{
Bram Moolenaar1dced572012-04-05 16:54:08 +02001837 /* set cache table */
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001838 lua_newtable(L);
1839 lua_newtable(L);
Bram Moolenaar1dced572012-04-05 16:54:08 +02001840 lua_pushstring(L, "v");
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001841 lua_setfield(L, -2, "__mode");
Bram Moolenaar1dced572012-04-05 16:54:08 +02001842 lua_setmetatable(L, -2); /* cache is weak-valued */
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001843 /* print */
1844 lua_pushcfunction(L, luaV_print);
1845 lua_setglobal(L, "print");
Bram Moolenaar38e2b062011-09-21 17:15:39 +02001846 /* debug.debug */
1847 lua_getglobal(L, "debug");
1848 lua_pushcfunction(L, luaV_debug);
1849 lua_setfield(L, -2, "debug");
1850 lua_pop(L, 1);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001851 /* free */
1852 lua_pushlightuserdata(L, (void *) LUAVIM_FREE);
Bram Moolenaar1dced572012-04-05 16:54:08 +02001853 lua_pushvalue(L, 1); /* cache table */
1854 lua_pushcclosure(L, luaV_free, 1);
1855 lua_rawset(L, LUA_REGISTRYINDEX);
1856 /* luaeval */
1857 lua_pushlightuserdata(L, (void *) LUAVIM_LUAEVAL);
1858 lua_pushvalue(L, 1); /* cache table */
1859 lua_pushcclosure(L, luaV_luaeval, 1);
1860 lua_rawset(L, LUA_REGISTRYINDEX);
1861 /* setref */
1862 lua_pushlightuserdata(L, (void *) LUAVIM_SETREF);
1863 lua_pushvalue(L, 1); /* cache table */
1864 lua_pushcclosure(L, luaV_setref, 1);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001865 lua_rawset(L, LUA_REGISTRYINDEX);
1866 /* register */
Bram Moolenaar1dced572012-04-05 16:54:08 +02001867 luaV_newmetatable(L, LUAVIM_LIST);
1868 lua_pushvalue(L, 1);
1869 luaV_openlib(L, luaV_List_mt, 1);
1870 luaV_newmetatable(L, LUAVIM_DICT);
1871 lua_pushvalue(L, 1);
1872 luaV_openlib(L, luaV_Dict_mt, 1);
Bram Moolenaarca06da92018-07-01 15:12:05 +02001873 luaV_newmetatable(L, LUAVIM_FUNCREF);
1874 lua_pushvalue(L, 1);
1875 luaV_openlib(L, luaV_Funcref_mt, 1);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001876 luaV_newmetatable(L, LUAVIM_BUFFER);
Bram Moolenaar1dced572012-04-05 16:54:08 +02001877 lua_pushvalue(L, 1); /* cache table */
1878 luaV_openlib(L, luaV_Buffer_mt, 1);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001879 luaV_newmetatable(L, LUAVIM_WINDOW);
Bram Moolenaar1dced572012-04-05 16:54:08 +02001880 lua_pushvalue(L, 1); /* cache table */
1881 luaV_openlib(L, luaV_Window_mt, 1);
1882 lua_newtable(L); /* vim table */
1883 lua_pushvalue(L, 1); /* cache table */
1884 luaV_openlib(L, luaV_module, 1);
1885 lua_setglobal(L, LUAVIM_NAME);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001886 return 0;
1887}
1888
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001889 static lua_State *
1890luaV_newstate(void)
1891{
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001892 lua_State *L = luaL_newstate();
Bram Moolenaar16c98f92010-07-28 22:46:08 +02001893 luaL_openlibs(L); /* core libs */
1894 lua_pushcfunction(L, luaopen_vim); /* vim */
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001895 lua_call(L, 0, 0);
1896 return L;
1897}
1898
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001899 static void
1900luaV_setrange(lua_State *L, int line1, int line2)
1901{
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001902 lua_getglobal(L, LUAVIM_NAME);
1903 lua_pushinteger(L, line1);
1904 lua_setfield(L, -2, "firstline");
1905 lua_pushinteger(L, line2);
1906 lua_setfield(L, -2, "lastline");
1907 lua_pop(L, 1); /* vim table */
1908}
1909
1910
1911/* ======= Interface ======= */
1912
1913static lua_State *L = NULL;
1914
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001915 static int
Bram Moolenaar1dced572012-04-05 16:54:08 +02001916lua_isopen(void)
Bram Moolenaar2bd6a1b2010-08-12 22:14:01 +02001917{
1918 return L != NULL;
1919}
1920
1921 static int
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001922lua_init(void)
1923{
Bram Moolenaar1dced572012-04-05 16:54:08 +02001924 if (!lua_isopen())
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001925 {
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001926#ifdef DYNAMIC_LUA
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001927 if (!lua_enabled(TRUE))
1928 {
1929 EMSG(_("Lua library cannot be loaded."));
1930 return FAIL;
1931 }
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001932#endif
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001933 L = luaV_newstate();
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001934 }
1935 return OK;
1936}
1937
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001938 void
1939lua_end(void)
1940{
Bram Moolenaar1dced572012-04-05 16:54:08 +02001941 if (lua_isopen())
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001942 {
1943 lua_close(L);
1944 L = NULL;
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001945#ifdef DYNAMIC_LUA
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001946 end_dynamic_lua();
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001947#endif
1948 }
1949}
1950
1951/* ex commands */
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001952 void
1953ex_lua(exarg_T *eap)
1954{
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001955 char *script;
1956 if (lua_init() == FAIL) return;
1957 script = (char *) script_get(eap, eap->arg);
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001958 if (!eap->skip)
1959 {
1960 char *s = (script) ? script : (char *) eap->arg;
1961 luaV_setrange(L, eap->line1, eap->line2);
1962 if (luaL_loadbuffer(L, s, strlen(s), LUAVIM_CHUNKNAME)
1963 || lua_pcall(L, 0, 0, 0))
1964 luaV_emsg(L);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001965 }
1966 if (script != NULL) vim_free(script);
1967}
1968
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001969 void
1970ex_luado(exarg_T *eap)
1971{
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001972 linenr_T l;
1973 const char *s = (const char *) eap->arg;
1974 luaL_Buffer b;
1975 size_t len;
Bram Moolenaard58f03b2017-01-29 22:48:45 +01001976 buf_T *was_curbuf = curbuf;
1977
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001978 if (lua_init() == FAIL) return;
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001979 if (u_save(eap->line1 - 1, eap->line2 + 1) == FAIL)
1980 {
1981 EMSG(_("cannot save undo information"));
1982 return;
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001983 }
1984 luaV_setrange(L, eap->line1, eap->line2);
1985 luaL_buffinit(L, &b);
Bram Moolenaarbd2f3c32012-04-06 14:31:00 +02001986 luaL_addlstring(&b, "return function(line, linenr) ", 30); /* header */
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001987 luaL_addlstring(&b, s, strlen(s));
1988 luaL_addlstring(&b, " end", 4); /* footer */
1989 luaL_pushresult(&b);
1990 s = lua_tolstring(L, -1, &len);
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001991 if (luaL_loadbuffer(L, s, len, LUAVIM_CHUNKNAME))
1992 {
1993 luaV_emsg(L);
1994 lua_pop(L, 1); /* function body */
1995 return;
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001996 }
1997 lua_call(L, 0, 1);
1998 lua_replace(L, -2); /* function -> body */
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001999 for (l = eap->line1; l <= eap->line2; l++)
2000 {
Bram Moolenaard58f03b2017-01-29 22:48:45 +01002001 /* Check the line number, the command my have deleted lines. */
2002 if (l > curbuf->b_ml.ml_line_count)
2003 break;
2004
Bram Moolenaar55d5c032010-07-17 23:52:29 +02002005 lua_pushvalue(L, -1); /* function */
2006 luaV_pushline(L, curbuf, l); /* current line as arg */
Bram Moolenaarbd2f3c32012-04-06 14:31:00 +02002007 lua_pushinteger(L, l); /* current line number as arg */
2008 if (lua_pcall(L, 2, 1, 0))
Bram Moolenaar55d5c032010-07-17 23:52:29 +02002009 {
2010 luaV_emsg(L);
2011 break;
2012 }
Bram Moolenaard58f03b2017-01-29 22:48:45 +01002013 /* Catch the command switching to another buffer. */
2014 if (curbuf != was_curbuf)
2015 break;
Bram Moolenaar55d5c032010-07-17 23:52:29 +02002016 if (lua_isstring(L, -1)) /* update line? */
2017 {
Bram Moolenaar0ba04292010-07-14 23:23:17 +02002018#ifdef HAVE_SANDBOX
Bram Moolenaar55d5c032010-07-17 23:52:29 +02002019 luaV_checksandbox(L);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02002020#endif
Bram Moolenaar55d5c032010-07-17 23:52:29 +02002021 ml_replace(l, luaV_toline(L, -1), TRUE);
2022 changed_bytes(l, 0);
2023 lua_pop(L, 1); /* result from luaV_toline */
2024 }
2025 lua_pop(L, 1); /* line */
Bram Moolenaar0ba04292010-07-14 23:23:17 +02002026 }
2027 lua_pop(L, 1); /* function */
2028 check_cursor();
2029 update_screen(NOT_VALID);
2030}
2031
Bram Moolenaar55d5c032010-07-17 23:52:29 +02002032 void
2033ex_luafile(exarg_T *eap)
2034{
2035 if (lua_init() == FAIL)
2036 return;
2037 if (!eap->skip)
2038 {
2039 luaV_setrange(L, eap->line1, eap->line2);
2040 if (luaL_loadfile(L, (char *) eap->arg) || lua_pcall(L, 0, 0, 0))
2041 luaV_emsg(L);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02002042 }
2043}
2044
Bram Moolenaar1dced572012-04-05 16:54:08 +02002045#define luaV_freetype(typ,tname) \
2046 void \
2047 lua_##tname##_free(typ *o) \
2048 { \
2049 if (!lua_isopen()) return; \
2050 luaV_getfield(L, LUAVIM_FREE); \
2051 lua_pushlightuserdata(L, (void *) o); \
2052 lua_call(L, 1, 0); \
2053 }
2054
2055luaV_freetype(buf_T, buffer)
2056luaV_freetype(win_T, window)
2057
Bram Moolenaar55d5c032010-07-17 23:52:29 +02002058 void
Bram Moolenaar1dced572012-04-05 16:54:08 +02002059do_luaeval (char_u *str, typval_T *arg, typval_T *rettv)
Bram Moolenaar55d5c032010-07-17 23:52:29 +02002060{
Bram Moolenaar1dced572012-04-05 16:54:08 +02002061 lua_init();
2062 luaV_getfield(L, LUAVIM_LUAEVAL);
2063 lua_pushstring(L, (char *) str);
2064 lua_pushlightuserdata(L, (void *) arg);
2065 lua_pushlightuserdata(L, (void *) rettv);
2066 lua_call(L, 3, 0);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02002067}
2068
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01002069 int
Bram Moolenaar1dced572012-04-05 16:54:08 +02002070set_ref_in_lua (int copyID)
Bram Moolenaar55d5c032010-07-17 23:52:29 +02002071{
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01002072 int aborted = 0;
2073
2074 if (lua_isopen())
2075 {
2076 luaV_getfield(L, LUAVIM_SETREF);
2077 /* call the function with 1 arg, getting 1 result back */
2078 lua_pushinteger(L, copyID);
2079 lua_call(L, 1, 1);
2080 /* get the result */
2081 aborted = lua_tointeger(L, -1);
2082 /* pop result off the stack */
2083 lua_pop(L, 1);
2084 }
2085 return aborted;
Bram Moolenaar0ba04292010-07-14 23:23:17 +02002086}
2087
2088#endif