blob: 2d02f7c58d493066fda4946c2409b0cb074288e5 [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
Bram Moolenaar2ab2e862019-12-04 21:24:53 +010018// Only do the following when the feature is enabled. Needed for "make
19// depend".
Bram Moolenaar0ba04292010-07-14 23:23:17 +020020#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 Moolenaarb7828692019-03-23 13:57:02 +010031typedef blob_T *luaV_Blob;
Bram Moolenaarca06da92018-07-01 15:12:05 +020032typedef struct {
Bram Moolenaar4eefe472019-03-19 21:59:19 +010033 char_u *name; // funcref
Bram Moolenaarca06da92018-07-01 15:12:05 +020034 dict_T *self; // selfdict
35} luaV_Funcref;
Bram Moolenaar0ba04292010-07-14 23:23:17 +020036typedef void (*msgfunc_T)(char_u *);
37
Bram Moolenaar801ab062020-06-25 19:27:56 +020038typedef struct {
39 int lua_funcref; // ref to a lua func
40 int lua_tableref; // ref to a lua table if metatable else LUA_NOREF. used
41 // for __call
42 lua_State *L;
43} luaV_CFuncState;
44
Bram Moolenaar1dced572012-04-05 16:54:08 +020045static const char LUAVIM_DICT[] = "dict";
46static const char LUAVIM_LIST[] = "list";
Bram Moolenaarb7828692019-03-23 13:57:02 +010047static const char LUAVIM_BLOB[] = "blob";
Bram Moolenaarca06da92018-07-01 15:12:05 +020048static const char LUAVIM_FUNCREF[] = "funcref";
Bram Moolenaar0ba04292010-07-14 23:23:17 +020049static const char LUAVIM_BUFFER[] = "buffer";
50static const char LUAVIM_WINDOW[] = "window";
51static const char LUAVIM_FREE[] = "luaV_free";
Bram Moolenaar1dced572012-04-05 16:54:08 +020052static const char LUAVIM_LUAEVAL[] = "luaV_luaeval";
53static const char LUAVIM_SETREF[] = "luaV_setref";
Bram Moolenaar0ba04292010-07-14 23:23:17 +020054
Bram Moolenaar801ab062020-06-25 19:27:56 +020055static const char LUA___CALL[] = "__call";
56
Bram Moolenaar2ab2e862019-12-04 21:24:53 +010057// most functions are closures with a cache table as first upvalue;
58// get/setudata manage references to vim userdata in cache table through
59// object pointers (light userdata)
Bram Moolenaar1dced572012-04-05 16:54:08 +020060#define luaV_getudata(L, v) \
61 lua_pushlightuserdata((L), (void *) (v)); \
62 lua_rawget((L), lua_upvalueindex(1))
63#define luaV_setudata(L, v) \
64 lua_pushlightuserdata((L), (void *) (v)); \
65 lua_pushvalue((L), -2); \
66 lua_rawset((L), lua_upvalueindex(1))
Bram Moolenaar0ba04292010-07-14 23:23:17 +020067#define luaV_getfield(L, s) \
68 lua_pushlightuserdata((L), (void *)(s)); \
69 lua_rawget((L), LUA_REGISTRYINDEX)
70#define luaV_checksandbox(L) \
71 if (sandbox) luaL_error((L), "not allowed in sandbox")
72#define luaV_msg(L) luaV_msgfunc((L), (msgfunc_T) msg)
73#define luaV_emsg(L) luaV_msgfunc((L), (msgfunc_T) emsg)
Bram Moolenaarca06da92018-07-01 15:12:05 +020074#define luaV_checktypval(L, a, v, msg) \
75 do { \
Bram Moolenaar801ab062020-06-25 19:27:56 +020076 if (luaV_totypval(L, a, v) == FAIL) \
Bram Moolenaarca06da92018-07-01 15:12:05 +020077 luaL_error(L, msg ": cannot convert value"); \
78 } while (0)
Bram Moolenaar0ba04292010-07-14 23:23:17 +020079
Bram Moolenaarca06da92018-07-01 15:12:05 +020080static luaV_List *luaV_pushlist(lua_State *L, list_T *lis);
81static luaV_Dict *luaV_pushdict(lua_State *L, dict_T *dic);
Bram Moolenaarb7828692019-03-23 13:57:02 +010082static luaV_Blob *luaV_pushblob(lua_State *L, blob_T *blo);
Bram Moolenaar4eefe472019-03-19 21:59:19 +010083static luaV_Funcref *luaV_pushfuncref(lua_State *L, char_u *name);
Bram Moolenaar801ab062020-06-25 19:27:56 +020084static int luaV_call_lua_func(int argcount, typval_T *argvars, typval_T *rettv, void *state);
85static void luaV_call_lua_func_free(void *state);
Bram Moolenaar1dced572012-04-05 16:54:08 +020086
87#if LUA_VERSION_NUM <= 501
88#define luaV_openlib(L, l, n) luaL_openlib(L, NULL, l, n)
89#define luaL_typeerror luaL_typerror
90#else
91#define luaV_openlib luaL_setfuncs
92#endif
Bram Moolenaar0ba04292010-07-14 23:23:17 +020093
94#ifdef DYNAMIC_LUA
Bram Moolenaar2334b6d2010-07-22 21:32:16 +020095
Bram Moolenaar4f974752019-02-17 17:44:42 +010096#ifndef MSWIN
Bram Moolenaar2334b6d2010-07-22 21:32:16 +020097# include <dlfcn.h>
98# define HANDLE void*
99# define load_dll(n) dlopen((n), RTLD_LAZY|RTLD_GLOBAL)
100# define symbol_from_dll dlsym
101# define close_dll dlclose
102#else
Bram Moolenaarebbcb822010-10-23 14:02:54 +0200103# define load_dll vimLoadLib
Bram Moolenaar2334b6d2010-07-22 21:32:16 +0200104# define symbol_from_dll GetProcAddress
105# define close_dll FreeLibrary
106#endif
107
Bram Moolenaar2ab2e862019-12-04 21:24:53 +0100108// lauxlib
Bram Moolenaar1dced572012-04-05 16:54:08 +0200109#if LUA_VERSION_NUM <= 501
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200110#define luaL_register dll_luaL_register
Bram Moolenaar1dced572012-04-05 16:54:08 +0200111#define luaL_prepbuffer dll_luaL_prepbuffer
112#define luaL_openlib dll_luaL_openlib
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200113#define luaL_typerror dll_luaL_typerror
Bram Moolenaar1dced572012-04-05 16:54:08 +0200114#define luaL_loadfile dll_luaL_loadfile
115#define luaL_loadbuffer dll_luaL_loadbuffer
116#else
117#define luaL_prepbuffsize dll_luaL_prepbuffsize
118#define luaL_setfuncs dll_luaL_setfuncs
119#define luaL_loadfilex dll_luaL_loadfilex
120#define luaL_loadbufferx dll_luaL_loadbufferx
121#define luaL_argerror dll_luaL_argerror
122#endif
Bram Moolenaarbd2f3c32012-04-06 14:31:00 +0200123#define luaL_checkany dll_luaL_checkany
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200124#define luaL_checklstring dll_luaL_checklstring
125#define luaL_checkinteger dll_luaL_checkinteger
126#define luaL_optinteger dll_luaL_optinteger
127#define luaL_checktype dll_luaL_checktype
128#define luaL_error dll_luaL_error
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200129#define luaL_newstate dll_luaL_newstate
130#define luaL_buffinit dll_luaL_buffinit
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200131#define luaL_addlstring dll_luaL_addlstring
132#define luaL_pushresult dll_luaL_pushresult
Bram Moolenaardf1643a2020-05-17 18:53:27 +0200133#define luaL_loadstring dll_luaL_loadstring
Bram Moolenaar1e4c7d02020-06-25 20:56:42 +0200134#define luaL_ref dll_luaL_ref
135#define luaL_unref dll_luaL_unref
Bram Moolenaar2ab2e862019-12-04 21:24:53 +0100136// lua
Bram Moolenaar1dced572012-04-05 16:54:08 +0200137#if LUA_VERSION_NUM <= 501
138#define lua_tonumber dll_lua_tonumber
139#define lua_tointeger dll_lua_tointeger
140#define lua_call dll_lua_call
141#define lua_pcall dll_lua_pcall
142#else
143#define lua_tonumberx dll_lua_tonumberx
144#define lua_tointegerx dll_lua_tointegerx
145#define lua_callk dll_lua_callk
146#define lua_pcallk dll_lua_pcallk
147#define lua_getglobal dll_lua_getglobal
148#define lua_setglobal dll_lua_setglobal
Bram Moolenaar1dced572012-04-05 16:54:08 +0200149#endif
Bram Moolenaar1f860d82015-06-27 18:36:16 +0200150#if LUA_VERSION_NUM <= 502
151#define lua_replace dll_lua_replace
152#define lua_remove dll_lua_remove
153#endif
154#if LUA_VERSION_NUM >= 503
155#define lua_rotate dll_lua_rotate
156#define lua_copy dll_lua_copy
157#endif
Bram Moolenaarbd2f3c32012-04-06 14:31:00 +0200158#define lua_typename dll_lua_typename
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200159#define lua_close dll_lua_close
160#define lua_gettop dll_lua_gettop
161#define lua_settop dll_lua_settop
162#define lua_pushvalue dll_lua_pushvalue
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200163#define lua_isnumber dll_lua_isnumber
164#define lua_isstring dll_lua_isstring
165#define lua_type dll_lua_type
166#define lua_rawequal dll_lua_rawequal
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200167#define lua_toboolean dll_lua_toboolean
168#define lua_tolstring dll_lua_tolstring
169#define lua_touserdata dll_lua_touserdata
170#define lua_pushnil dll_lua_pushnil
171#define lua_pushnumber dll_lua_pushnumber
172#define lua_pushinteger dll_lua_pushinteger
173#define lua_pushlstring dll_lua_pushlstring
174#define lua_pushstring dll_lua_pushstring
175#define lua_pushfstring dll_lua_pushfstring
176#define lua_pushcclosure dll_lua_pushcclosure
177#define lua_pushboolean dll_lua_pushboolean
178#define lua_pushlightuserdata dll_lua_pushlightuserdata
179#define lua_getfield dll_lua_getfield
180#define lua_rawget dll_lua_rawget
Bram Moolenaar1dced572012-04-05 16:54:08 +0200181#define lua_rawgeti dll_lua_rawgeti
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200182#define lua_createtable dll_lua_createtable
Bram Moolenaar830e3582018-08-21 14:23:35 +0200183#if LUA_VERSION_NUM >= 504
184 #define lua_newuserdatauv dll_lua_newuserdatauv
185#else
186 #define lua_newuserdata dll_lua_newuserdata
187#endif
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200188#define lua_getmetatable dll_lua_getmetatable
189#define lua_setfield dll_lua_setfield
190#define lua_rawset dll_lua_rawset
191#define lua_rawseti dll_lua_rawseti
192#define lua_setmetatable dll_lua_setmetatable
Bram Moolenaarbd2f3c32012-04-06 14:31:00 +0200193#define lua_next dll_lua_next
Bram Moolenaar2ab2e862019-12-04 21:24:53 +0100194// libs
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200195#define luaopen_base dll_luaopen_base
196#define luaopen_table dll_luaopen_table
197#define luaopen_string dll_luaopen_string
198#define luaopen_math dll_luaopen_math
Bram Moolenaar16c98f92010-07-28 22:46:08 +0200199#define luaopen_io dll_luaopen_io
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200200#define luaopen_os dll_luaopen_os
201#define luaopen_package dll_luaopen_package
202#define luaopen_debug dll_luaopen_debug
Bram Moolenaar16c98f92010-07-28 22:46:08 +0200203#define luaL_openlibs dll_luaL_openlibs
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200204
Bram Moolenaar2ab2e862019-12-04 21:24:53 +0100205// lauxlib
Bram Moolenaar1dced572012-04-05 16:54:08 +0200206#if LUA_VERSION_NUM <= 501
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200207void (*dll_luaL_register) (lua_State *L, const char *libname, const luaL_Reg *l);
Bram Moolenaar1dced572012-04-05 16:54:08 +0200208char *(*dll_luaL_prepbuffer) (luaL_Buffer *B);
209void (*dll_luaL_openlib) (lua_State *L, const char *libname, const luaL_Reg *l, int nup);
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200210int (*dll_luaL_typerror) (lua_State *L, int narg, const char *tname);
Bram Moolenaar1dced572012-04-05 16:54:08 +0200211int (*dll_luaL_loadfile) (lua_State *L, const char *filename);
212int (*dll_luaL_loadbuffer) (lua_State *L, const char *buff, size_t sz, const char *name);
213#else
214char *(*dll_luaL_prepbuffsize) (luaL_Buffer *B, size_t sz);
215void (*dll_luaL_setfuncs) (lua_State *L, const luaL_Reg *l, int nup);
216int (*dll_luaL_loadfilex) (lua_State *L, const char *filename, const char *mode);
217int (*dll_luaL_loadbufferx) (lua_State *L, const char *buff, size_t sz, const char *name, const char *mode);
218int (*dll_luaL_argerror) (lua_State *L, int numarg, const char *extramsg);
219#endif
Bram Moolenaarbd2f3c32012-04-06 14:31:00 +0200220void (*dll_luaL_checkany) (lua_State *L, int narg);
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200221const char *(*dll_luaL_checklstring) (lua_State *L, int numArg, size_t *l);
222lua_Integer (*dll_luaL_checkinteger) (lua_State *L, int numArg);
223lua_Integer (*dll_luaL_optinteger) (lua_State *L, int nArg, lua_Integer def);
224void (*dll_luaL_checktype) (lua_State *L, int narg, int t);
225int (*dll_luaL_error) (lua_State *L, const char *fmt, ...);
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200226lua_State *(*dll_luaL_newstate) (void);
227void (*dll_luaL_buffinit) (lua_State *L, luaL_Buffer *B);
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200228void (*dll_luaL_addlstring) (luaL_Buffer *B, const char *s, size_t l);
229void (*dll_luaL_pushresult) (luaL_Buffer *B);
Bram Moolenaardf1643a2020-05-17 18:53:27 +0200230int (*dll_luaL_loadstring) (lua_State *L, const char *s);
Bram Moolenaar1e4c7d02020-06-25 20:56:42 +0200231int (*dll_luaL_ref) (lua_State *L, int idx);
232#if LUA_VERSION_NUM <= 502
233void (*dll_luaL_unref) (lua_State *L, int idx, int n);
234#else
235void (*dll_luaL_unref) (lua_State *L, int idx, lua_Integer n);
236#endif
Bram Moolenaar2ab2e862019-12-04 21:24:53 +0100237// lua
Bram Moolenaar1dced572012-04-05 16:54:08 +0200238#if LUA_VERSION_NUM <= 501
239lua_Number (*dll_lua_tonumber) (lua_State *L, int idx);
240lua_Integer (*dll_lua_tointeger) (lua_State *L, int idx);
241void (*dll_lua_call) (lua_State *L, int nargs, int nresults);
242int (*dll_lua_pcall) (lua_State *L, int nargs, int nresults, int errfunc);
243#else
244lua_Number (*dll_lua_tonumberx) (lua_State *L, int idx, int *isnum);
245lua_Integer (*dll_lua_tointegerx) (lua_State *L, int idx, int *isnum);
246void (*dll_lua_callk) (lua_State *L, int nargs, int nresults, int ctx,
Bram Moolenaardb913952012-06-29 12:54:53 +0200247 lua_CFunction k);
Bram Moolenaar1dced572012-04-05 16:54:08 +0200248int (*dll_lua_pcallk) (lua_State *L, int nargs, int nresults, int errfunc,
Bram Moolenaardb913952012-06-29 12:54:53 +0200249 int ctx, lua_CFunction k);
Bram Moolenaar1dced572012-04-05 16:54:08 +0200250void (*dll_lua_getglobal) (lua_State *L, const char *var);
251void (*dll_lua_setglobal) (lua_State *L, const char *var);
Bram Moolenaar1f860d82015-06-27 18:36:16 +0200252#endif
253#if LUA_VERSION_NUM <= 502
254void (*dll_lua_replace) (lua_State *L, int idx);
255void (*dll_lua_remove) (lua_State *L, int idx);
256#endif
257#if LUA_VERSION_NUM >= 503
258void (*dll_lua_rotate) (lua_State *L, int idx, int n);
Bram Moolenaar9514b1f2015-06-25 18:27:32 +0200259void (*dll_lua_copy) (lua_State *L, int fromidx, int toidx);
Bram Moolenaar1dced572012-04-05 16:54:08 +0200260#endif
Bram Moolenaarbd2f3c32012-04-06 14:31:00 +0200261const char *(*dll_lua_typename) (lua_State *L, int tp);
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200262void (*dll_lua_close) (lua_State *L);
263int (*dll_lua_gettop) (lua_State *L);
264void (*dll_lua_settop) (lua_State *L, int idx);
265void (*dll_lua_pushvalue) (lua_State *L, int idx);
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200266int (*dll_lua_isnumber) (lua_State *L, int idx);
267int (*dll_lua_isstring) (lua_State *L, int idx);
268int (*dll_lua_type) (lua_State *L, int idx);
269int (*dll_lua_rawequal) (lua_State *L, int idx1, int idx2);
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200270int (*dll_lua_toboolean) (lua_State *L, int idx);
271const char *(*dll_lua_tolstring) (lua_State *L, int idx, size_t *len);
272void *(*dll_lua_touserdata) (lua_State *L, int idx);
273void (*dll_lua_pushnil) (lua_State *L);
274void (*dll_lua_pushnumber) (lua_State *L, lua_Number n);
275void (*dll_lua_pushinteger) (lua_State *L, lua_Integer n);
276void (*dll_lua_pushlstring) (lua_State *L, const char *s, size_t l);
277void (*dll_lua_pushstring) (lua_State *L, const char *s);
278const char *(*dll_lua_pushfstring) (lua_State *L, const char *fmt, ...);
279void (*dll_lua_pushcclosure) (lua_State *L, lua_CFunction fn, int n);
280void (*dll_lua_pushboolean) (lua_State *L, int b);
281void (*dll_lua_pushlightuserdata) (lua_State *L, void *p);
282void (*dll_lua_getfield) (lua_State *L, int idx, const char *k);
Bram Moolenaar17413672018-07-14 20:49:42 +0200283#if LUA_VERSION_NUM <= 502
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200284void (*dll_lua_rawget) (lua_State *L, int idx);
Bram Moolenaar1dced572012-04-05 16:54:08 +0200285void (*dll_lua_rawgeti) (lua_State *L, int idx, int n);
Bram Moolenaar17413672018-07-14 20:49:42 +0200286#else
287int (*dll_lua_rawget) (lua_State *L, int idx);
288int (*dll_lua_rawgeti) (lua_State *L, int idx, lua_Integer n);
289#endif
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200290void (*dll_lua_createtable) (lua_State *L, int narr, int nrec);
Bram Moolenaar830e3582018-08-21 14:23:35 +0200291#if LUA_VERSION_NUM >= 504
292void *(*dll_lua_newuserdatauv) (lua_State *L, size_t sz, int nuvalue);
293#else
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200294void *(*dll_lua_newuserdata) (lua_State *L, size_t sz);
Bram Moolenaar830e3582018-08-21 14:23:35 +0200295#endif
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200296int (*dll_lua_getmetatable) (lua_State *L, int objindex);
297void (*dll_lua_setfield) (lua_State *L, int idx, const char *k);
298void (*dll_lua_rawset) (lua_State *L, int idx);
Bram Moolenaar17413672018-07-14 20:49:42 +0200299#if LUA_VERSION_NUM <= 502
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200300void (*dll_lua_rawseti) (lua_State *L, int idx, int n);
Bram Moolenaar17413672018-07-14 20:49:42 +0200301#else
302void (*dll_lua_rawseti) (lua_State *L, int idx, lua_Integer n);
303#endif
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200304int (*dll_lua_setmetatable) (lua_State *L, int objindex);
Bram Moolenaarbd2f3c32012-04-06 14:31:00 +0200305int (*dll_lua_next) (lua_State *L, int idx);
Bram Moolenaar2ab2e862019-12-04 21:24:53 +0100306// libs
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200307int (*dll_luaopen_base) (lua_State *L);
308int (*dll_luaopen_table) (lua_State *L);
309int (*dll_luaopen_string) (lua_State *L);
310int (*dll_luaopen_math) (lua_State *L);
Bram Moolenaar16c98f92010-07-28 22:46:08 +0200311int (*dll_luaopen_io) (lua_State *L);
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200312int (*dll_luaopen_os) (lua_State *L);
313int (*dll_luaopen_package) (lua_State *L);
314int (*dll_luaopen_debug) (lua_State *L);
Bram Moolenaar16c98f92010-07-28 22:46:08 +0200315void (*dll_luaL_openlibs) (lua_State *L);
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200316
317typedef void **luaV_function;
318typedef struct {
319 const char *name;
320 luaV_function func;
321} luaV_Reg;
322
323static const luaV_Reg luaV_dll[] = {
Bram Moolenaar2ab2e862019-12-04 21:24:53 +0100324 // lauxlib
Bram Moolenaar1dced572012-04-05 16:54:08 +0200325#if LUA_VERSION_NUM <= 501
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200326 {"luaL_register", (luaV_function) &dll_luaL_register},
Bram Moolenaar1dced572012-04-05 16:54:08 +0200327 {"luaL_prepbuffer", (luaV_function) &dll_luaL_prepbuffer},
328 {"luaL_openlib", (luaV_function) &dll_luaL_openlib},
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200329 {"luaL_typerror", (luaV_function) &dll_luaL_typerror},
Bram Moolenaar1dced572012-04-05 16:54:08 +0200330 {"luaL_loadfile", (luaV_function) &dll_luaL_loadfile},
331 {"luaL_loadbuffer", (luaV_function) &dll_luaL_loadbuffer},
332#else
333 {"luaL_prepbuffsize", (luaV_function) &dll_luaL_prepbuffsize},
334 {"luaL_setfuncs", (luaV_function) &dll_luaL_setfuncs},
335 {"luaL_loadfilex", (luaV_function) &dll_luaL_loadfilex},
336 {"luaL_loadbufferx", (luaV_function) &dll_luaL_loadbufferx},
337 {"luaL_argerror", (luaV_function) &dll_luaL_argerror},
338#endif
Bram Moolenaarbd2f3c32012-04-06 14:31:00 +0200339 {"luaL_checkany", (luaV_function) &dll_luaL_checkany},
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200340 {"luaL_checklstring", (luaV_function) &dll_luaL_checklstring},
341 {"luaL_checkinteger", (luaV_function) &dll_luaL_checkinteger},
342 {"luaL_optinteger", (luaV_function) &dll_luaL_optinteger},
343 {"luaL_checktype", (luaV_function) &dll_luaL_checktype},
344 {"luaL_error", (luaV_function) &dll_luaL_error},
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200345 {"luaL_newstate", (luaV_function) &dll_luaL_newstate},
346 {"luaL_buffinit", (luaV_function) &dll_luaL_buffinit},
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200347 {"luaL_addlstring", (luaV_function) &dll_luaL_addlstring},
348 {"luaL_pushresult", (luaV_function) &dll_luaL_pushresult},
Bram Moolenaardf1643a2020-05-17 18:53:27 +0200349 {"luaL_loadstring", (luaV_function) &dll_luaL_loadstring},
Bram Moolenaar1e4c7d02020-06-25 20:56:42 +0200350 {"luaL_ref", (luaV_function) &dll_luaL_ref},
351 {"luaL_unref", (luaV_function) &dll_luaL_unref},
Bram Moolenaar2ab2e862019-12-04 21:24:53 +0100352 // lua
Bram Moolenaar1dced572012-04-05 16:54:08 +0200353#if LUA_VERSION_NUM <= 501
354 {"lua_tonumber", (luaV_function) &dll_lua_tonumber},
355 {"lua_tointeger", (luaV_function) &dll_lua_tointeger},
356 {"lua_call", (luaV_function) &dll_lua_call},
357 {"lua_pcall", (luaV_function) &dll_lua_pcall},
358#else
359 {"lua_tonumberx", (luaV_function) &dll_lua_tonumberx},
360 {"lua_tointegerx", (luaV_function) &dll_lua_tointegerx},
361 {"lua_callk", (luaV_function) &dll_lua_callk},
362 {"lua_pcallk", (luaV_function) &dll_lua_pcallk},
363 {"lua_getglobal", (luaV_function) &dll_lua_getglobal},
364 {"lua_setglobal", (luaV_function) &dll_lua_setglobal},
Bram Moolenaar1dced572012-04-05 16:54:08 +0200365#endif
Bram Moolenaar1f860d82015-06-27 18:36:16 +0200366#if LUA_VERSION_NUM <= 502
367 {"lua_replace", (luaV_function) &dll_lua_replace},
368 {"lua_remove", (luaV_function) &dll_lua_remove},
369#endif
370#if LUA_VERSION_NUM >= 503
371 {"lua_rotate", (luaV_function) &dll_lua_rotate},
372 {"lua_copy", (luaV_function) &dll_lua_copy},
373#endif
Bram Moolenaarbd2f3c32012-04-06 14:31:00 +0200374 {"lua_typename", (luaV_function) &dll_lua_typename},
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200375 {"lua_close", (luaV_function) &dll_lua_close},
376 {"lua_gettop", (luaV_function) &dll_lua_gettop},
377 {"lua_settop", (luaV_function) &dll_lua_settop},
378 {"lua_pushvalue", (luaV_function) &dll_lua_pushvalue},
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200379 {"lua_isnumber", (luaV_function) &dll_lua_isnumber},
380 {"lua_isstring", (luaV_function) &dll_lua_isstring},
381 {"lua_type", (luaV_function) &dll_lua_type},
382 {"lua_rawequal", (luaV_function) &dll_lua_rawequal},
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200383 {"lua_toboolean", (luaV_function) &dll_lua_toboolean},
384 {"lua_tolstring", (luaV_function) &dll_lua_tolstring},
385 {"lua_touserdata", (luaV_function) &dll_lua_touserdata},
386 {"lua_pushnil", (luaV_function) &dll_lua_pushnil},
387 {"lua_pushnumber", (luaV_function) &dll_lua_pushnumber},
388 {"lua_pushinteger", (luaV_function) &dll_lua_pushinteger},
389 {"lua_pushlstring", (luaV_function) &dll_lua_pushlstring},
390 {"lua_pushstring", (luaV_function) &dll_lua_pushstring},
391 {"lua_pushfstring", (luaV_function) &dll_lua_pushfstring},
392 {"lua_pushcclosure", (luaV_function) &dll_lua_pushcclosure},
393 {"lua_pushboolean", (luaV_function) &dll_lua_pushboolean},
394 {"lua_pushlightuserdata", (luaV_function) &dll_lua_pushlightuserdata},
395 {"lua_getfield", (luaV_function) &dll_lua_getfield},
396 {"lua_rawget", (luaV_function) &dll_lua_rawget},
Bram Moolenaar1dced572012-04-05 16:54:08 +0200397 {"lua_rawgeti", (luaV_function) &dll_lua_rawgeti},
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200398 {"lua_createtable", (luaV_function) &dll_lua_createtable},
Bram Moolenaar830e3582018-08-21 14:23:35 +0200399#if LUA_VERSION_NUM >= 504
400 {"lua_newuserdatauv", (luaV_function) &dll_lua_newuserdatauv},
401#else
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200402 {"lua_newuserdata", (luaV_function) &dll_lua_newuserdata},
Bram Moolenaar830e3582018-08-21 14:23:35 +0200403#endif
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200404 {"lua_getmetatable", (luaV_function) &dll_lua_getmetatable},
405 {"lua_setfield", (luaV_function) &dll_lua_setfield},
406 {"lua_rawset", (luaV_function) &dll_lua_rawset},
407 {"lua_rawseti", (luaV_function) &dll_lua_rawseti},
408 {"lua_setmetatable", (luaV_function) &dll_lua_setmetatable},
Bram Moolenaarbd2f3c32012-04-06 14:31:00 +0200409 {"lua_next", (luaV_function) &dll_lua_next},
Bram Moolenaar2ab2e862019-12-04 21:24:53 +0100410 // libs
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200411 {"luaopen_base", (luaV_function) &dll_luaopen_base},
412 {"luaopen_table", (luaV_function) &dll_luaopen_table},
413 {"luaopen_string", (luaV_function) &dll_luaopen_string},
414 {"luaopen_math", (luaV_function) &dll_luaopen_math},
Bram Moolenaar16c98f92010-07-28 22:46:08 +0200415 {"luaopen_io", (luaV_function) &dll_luaopen_io},
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200416 {"luaopen_os", (luaV_function) &dll_luaopen_os},
417 {"luaopen_package", (luaV_function) &dll_luaopen_package},
418 {"luaopen_debug", (luaV_function) &dll_luaopen_debug},
Bram Moolenaar16c98f92010-07-28 22:46:08 +0200419 {"luaL_openlibs", (luaV_function) &dll_luaL_openlibs},
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200420 {NULL, NULL}
421};
422
Bram Moolenaar2334b6d2010-07-22 21:32:16 +0200423static HANDLE hinstLua = NULL;
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200424
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200425 static int
426lua_link_init(char *libname, int verbose)
427{
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200428 const luaV_Reg *reg;
429 if (hinstLua) return OK;
Bram Moolenaar2334b6d2010-07-22 21:32:16 +0200430 hinstLua = load_dll(libname);
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200431 if (!hinstLua)
432 {
433 if (verbose)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100434 semsg(_(e_loadlib), libname);
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200435 return FAIL;
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200436 }
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200437 for (reg = luaV_dll; reg->func; reg++)
438 {
Bram Moolenaar2334b6d2010-07-22 21:32:16 +0200439 if ((*reg->func = symbol_from_dll(hinstLua, reg->name)) == NULL)
440 {
441 close_dll(hinstLua);
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200442 hinstLua = 0;
443 if (verbose)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100444 semsg(_(e_loadfunc), reg->name);
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200445 return FAIL;
446 }
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200447 }
448 return OK;
449}
Bram Moolenaar2ab2e862019-12-04 21:24:53 +0100450#endif // DYNAMIC_LUA
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200451
Bram Moolenaard90b6c02016-08-28 18:10:45 +0200452#if defined(DYNAMIC_LUA) || defined(PROTO)
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200453 int
454lua_enabled(int verbose)
455{
Bram Moolenaar25e4fcd2016-01-09 14:57:47 +0100456 return lua_link_init((char *)p_luadll, verbose) == OK;
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200457}
Bram Moolenaard90b6c02016-08-28 18:10:45 +0200458#endif
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200459
Bram Moolenaar1dced572012-04-05 16:54:08 +0200460#if LUA_VERSION_NUM > 501
461 static int
Bram Moolenaar4eefe472019-03-19 21:59:19 +0100462luaL_typeerror(lua_State *L, int narg, const char *tname)
Bram Moolenaar1dced572012-04-05 16:54:08 +0200463{
464 const char *msg = lua_pushfstring(L, "%s expected, got %s",
Bram Moolenaardb913952012-06-29 12:54:53 +0200465 tname, luaL_typename(L, narg));
Bram Moolenaar1dced572012-04-05 16:54:08 +0200466 return luaL_argerror(L, narg, msg);
467}
468#endif
469
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200470
Bram Moolenaar2ab2e862019-12-04 21:24:53 +0100471// ======= Internal =======
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200472
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200473 static void
474luaV_newmetatable(lua_State *L, const char *tname)
475{
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200476 lua_newtable(L);
477 lua_pushlightuserdata(L, (void *) tname);
478 lua_pushvalue(L, -2);
479 lua_rawset(L, LUA_REGISTRYINDEX);
480}
481
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200482 static void *
483luaV_toudata(lua_State *L, int ud, const char *tname)
484{
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200485 void *p = lua_touserdata(L, ud);
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200486
Bram Moolenaar2ab2e862019-12-04 21:24:53 +0100487 if (p != NULL) // value is userdata?
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200488 {
Bram Moolenaar2ab2e862019-12-04 21:24:53 +0100489 if (lua_getmetatable(L, ud)) // does it have a metatable?
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200490 {
Bram Moolenaar2ab2e862019-12-04 21:24:53 +0100491 luaV_getfield(L, tname); // get metatable
492 if (lua_rawequal(L, -1, -2)) // MTs match?
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200493 {
Bram Moolenaar2ab2e862019-12-04 21:24:53 +0100494 lua_pop(L, 2); // MTs
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200495 return p;
496 }
497 }
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200498 }
499 return NULL;
500}
501
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200502 static void *
Bram Moolenaar1dced572012-04-05 16:54:08 +0200503luaV_checkcache(lua_State *L, void *p)
504{
505 luaV_getudata(L, p);
506 if (lua_isnil(L, -1)) luaL_error(L, "invalid object");
507 lua_pop(L, 1);
508 return p;
509}
510
511#define luaV_unbox(L,luatyp,ud) (*((luatyp *) lua_touserdata((L),(ud))))
512
513#define luaV_checkvalid(L,luatyp,ud) \
514 luaV_checkcache((L), (void *) luaV_unbox((L),luatyp,(ud)))
515
516 static void *
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200517luaV_checkudata(lua_State *L, int ud, const char *tname)
518{
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200519 void *p = luaV_toudata(L, ud, tname);
Bram Moolenaar1dced572012-04-05 16:54:08 +0200520 if (p == NULL) luaL_typeerror(L, ud, tname);
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200521 return p;
522}
523
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200524 static void
525luaV_pushtypval(lua_State *L, typval_T *tv)
526{
Bram Moolenaar1dced572012-04-05 16:54:08 +0200527 if (tv == NULL)
528 {
529 lua_pushnil(L);
530 return;
531 }
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200532 switch (tv->v_type)
533 {
534 case VAR_STRING:
Bram Moolenaard04da7c2012-10-14 03:41:59 +0200535 lua_pushstring(L, tv->vval.v_string == NULL
536 ? "" : (char *)tv->vval.v_string);
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200537 break;
538 case VAR_NUMBER:
539 lua_pushinteger(L, (int) tv->vval.v_number);
540 break;
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200541#ifdef FEAT_FLOAT
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200542 case VAR_FLOAT:
543 lua_pushnumber(L, (lua_Number) tv->vval.v_float);
544 break;
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200545#endif
Bram Moolenaar1dced572012-04-05 16:54:08 +0200546 case VAR_LIST:
547 luaV_pushlist(L, tv->vval.v_list);
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200548 break;
Bram Moolenaar1dced572012-04-05 16:54:08 +0200549 case VAR_DICT:
550 luaV_pushdict(L, tv->vval.v_dict);
551 break;
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +0100552 case VAR_BOOL:
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100553 case VAR_SPECIAL:
554 if (tv->vval.v_number <= VVAL_TRUE)
555 lua_pushinteger(L, (int) tv->vval.v_number);
556 else
557 lua_pushnil(L);
558 break;
Bram Moolenaarca06da92018-07-01 15:12:05 +0200559 case VAR_FUNC:
Bram Moolenaar4eefe472019-03-19 21:59:19 +0100560 luaV_pushfuncref(L, tv->vval.v_string);
Bram Moolenaarca06da92018-07-01 15:12:05 +0200561 break;
Bram Moolenaarb7828692019-03-23 13:57:02 +0100562 case VAR_BLOB:
563 luaV_pushblob(L, tv->vval.v_blob);
564 break;
Bram Moolenaar1dced572012-04-05 16:54:08 +0200565 default:
566 lua_pushnil(L);
567 }
568}
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200569
Bram Moolenaarca06da92018-07-01 15:12:05 +0200570/*
571 * Converts lua value at 'pos' to typval 'tv'.
572 * Returns OK or FAIL.
573 */
574 static int
575luaV_totypval(lua_State *L, int pos, typval_T *tv)
Bram Moolenaar1dced572012-04-05 16:54:08 +0200576{
Bram Moolenaarca06da92018-07-01 15:12:05 +0200577 int status = OK;
578
579 switch (lua_type(L, pos))
580 {
Bram Moolenaar1dced572012-04-05 16:54:08 +0200581 case LUA_TBOOLEAN:
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +0100582 tv->v_type = VAR_BOOL;
Bram Moolenaar1dced572012-04-05 16:54:08 +0200583 tv->vval.v_number = (varnumber_T) lua_toboolean(L, pos);
584 break;
Bram Moolenaar9067cd62019-01-01 00:41:54 +0100585 case LUA_TNIL:
586 tv->v_type = VAR_SPECIAL;
587 tv->vval.v_number = VVAL_NULL;
588 break;
Bram Moolenaar1dced572012-04-05 16:54:08 +0200589 case LUA_TSTRING:
590 tv->v_type = VAR_STRING;
591 tv->vval.v_string = vim_strsave((char_u *) lua_tostring(L, pos));
592 break;
593 case LUA_TNUMBER:
594#ifdef FEAT_FLOAT
Bram Moolenaareb04f082020-05-17 14:32:35 +0200595 {
596 const lua_Number n = lua_tonumber(L, pos);
597
598 if (n > (lua_Number)INT64_MAX || n < (lua_Number)INT64_MIN
599 || ((lua_Number)((varnumber_T)n)) != n)
600 {
601 tv->v_type = VAR_FLOAT;
602 tv->vval.v_float = (float_T)n;
603 }
604 else
605 {
606 tv->v_type = VAR_NUMBER;
607 tv->vval.v_number = (varnumber_T)n;
608 }
609 }
Bram Moolenaar1dced572012-04-05 16:54:08 +0200610#else
611 tv->v_type = VAR_NUMBER;
612 tv->vval.v_number = (varnumber_T) lua_tointeger(L, pos);
613#endif
614 break;
Bram Moolenaar801ab062020-06-25 19:27:56 +0200615 case LUA_TFUNCTION:
616 {
617 char_u *name;
618 lua_pushvalue(L, pos);
619 luaV_CFuncState *state = ALLOC_CLEAR_ONE(luaV_CFuncState);
620 state->lua_funcref = luaL_ref(L, LUA_REGISTRYINDEX);
621 state->L = L;
622 state->lua_tableref = LUA_NOREF;
623 name = register_cfunc(&luaV_call_lua_func,
624 &luaV_call_lua_func_free, state);
625 tv->v_type = VAR_FUNC;
626 tv->vval.v_string = vim_strsave(name);
627 break;
628 }
629 case LUA_TTABLE:
630 {
631 lua_pushvalue(L, pos);
632 int lua_tableref = luaL_ref(L, LUA_REGISTRYINDEX);
633 if (lua_getmetatable(L, pos)) {
634 lua_getfield(L, -1, LUA___CALL);
635 if (lua_isfunction(L, -1)) {
636 char_u *name;
637 int lua_funcref = luaL_ref(L, LUA_REGISTRYINDEX);
638 luaV_CFuncState *state = ALLOC_CLEAR_ONE(luaV_CFuncState);
639 state->lua_funcref = lua_funcref;
640 state->L = L;
641 state->lua_tableref = lua_tableref;
642 name = register_cfunc(&luaV_call_lua_func,
643 &luaV_call_lua_func_free, state);
644 tv->v_type = VAR_FUNC;
645 tv->vval.v_string = vim_strsave(name);
646 break;
647 }
648 }
649 tv->v_type = VAR_NUMBER;
650 tv->vval.v_number = 0;
651 status = FAIL;
652 break;
653 }
Bram Moolenaarca06da92018-07-01 15:12:05 +0200654 case LUA_TUSERDATA:
655 {
Bram Moolenaar1dced572012-04-05 16:54:08 +0200656 void *p = lua_touserdata(L, pos);
Bram Moolenaarca06da92018-07-01 15:12:05 +0200657
Bram Moolenaarb7828692019-03-23 13:57:02 +0100658 if (lua_getmetatable(L, pos)) // has metatable?
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200659 {
Bram Moolenaarb7828692019-03-23 13:57:02 +0100660 // check list
Bram Moolenaar1dced572012-04-05 16:54:08 +0200661 luaV_getfield(L, LUAVIM_LIST);
662 if (lua_rawequal(L, -1, -2))
Bram Moolenaar2334b6d2010-07-22 21:32:16 +0200663 {
Bram Moolenaar1dced572012-04-05 16:54:08 +0200664 tv->v_type = VAR_LIST;
665 tv->vval.v_list = *((luaV_List *) p);
666 ++tv->vval.v_list->lv_refcount;
Bram Moolenaarb7828692019-03-23 13:57:02 +0100667 lua_pop(L, 2); // MTs
Bram Moolenaarca06da92018-07-01 15:12:05 +0200668 break;
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200669 }
Bram Moolenaarb7828692019-03-23 13:57:02 +0100670 // check dict
Bram Moolenaar1dced572012-04-05 16:54:08 +0200671 luaV_getfield(L, LUAVIM_DICT);
672 if (lua_rawequal(L, -1, -3))
673 {
674 tv->v_type = VAR_DICT;
675 tv->vval.v_dict = *((luaV_Dict *) p);
676 ++tv->vval.v_dict->dv_refcount;
Bram Moolenaarb7828692019-03-23 13:57:02 +0100677 lua_pop(L, 3); // MTs
Bram Moolenaarca06da92018-07-01 15:12:05 +0200678 break;
Bram Moolenaar1dced572012-04-05 16:54:08 +0200679 }
Bram Moolenaarb7828692019-03-23 13:57:02 +0100680 // check blob
681 luaV_getfield(L, LUAVIM_BLOB);
Bram Moolenaarca06da92018-07-01 15:12:05 +0200682 if (lua_rawequal(L, -1, -4))
683 {
Bram Moolenaarb7828692019-03-23 13:57:02 +0100684 tv->v_type = VAR_BLOB;
685 tv->vval.v_blob = *((luaV_Blob *) p);
686 ++tv->vval.v_blob->bv_refcount;
687 lua_pop(L, 4); // MTs
688 break;
689 }
690 // check funcref
691 luaV_getfield(L, LUAVIM_FUNCREF);
692 if (lua_rawequal(L, -1, -5))
693 {
Bram Moolenaarca06da92018-07-01 15:12:05 +0200694 luaV_Funcref *f = (luaV_Funcref *) p;
Bram Moolenaar4eefe472019-03-19 21:59:19 +0100695 func_ref(f->name);
696 tv->v_type = VAR_FUNC;
697 tv->vval.v_string = vim_strsave(f->name);
Bram Moolenaarb7828692019-03-23 13:57:02 +0100698 lua_pop(L, 5); // MTs
Bram Moolenaarca06da92018-07-01 15:12:05 +0200699 break;
700 }
Bram Moolenaarb7828692019-03-23 13:57:02 +0100701 lua_pop(L, 4); // MTs
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200702 }
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200703 }
Bram Moolenaarb7828692019-03-23 13:57:02 +0100704 // FALLTHROUGH
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200705 default:
Bram Moolenaar1dced572012-04-05 16:54:08 +0200706 tv->v_type = VAR_NUMBER;
707 tv->vval.v_number = 0;
Bram Moolenaarca06da92018-07-01 15:12:05 +0200708 status = FAIL;
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200709 }
Bram Moolenaarca06da92018-07-01 15:12:05 +0200710 return status;
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200711}
712
Bram Moolenaar2ab2e862019-12-04 21:24:53 +0100713/*
714 * similar to luaL_addlstring, but replaces \0 with \n if toline and
715 * \n with \0 otherwise
716 */
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200717 static void
718luaV_addlstring(luaL_Buffer *b, const char *s, size_t l, int toline)
719{
720 while (l--)
721 {
722 if (*s == '\0' && toline)
723 luaL_addchar(b, '\n');
724 else if (*s == '\n' && !toline)
725 luaL_addchar(b, '\0');
726 else
727 luaL_addchar(b, *s);
728 s++;
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200729 }
730}
731
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200732 static void
733luaV_pushline(lua_State *L, buf_T *buf, linenr_T n)
734{
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200735 const char *s = (const char *) ml_get_buf(buf, n, FALSE);
736 luaL_Buffer b;
737 luaL_buffinit(L, &b);
738 luaV_addlstring(&b, s, strlen(s), 0);
739 luaL_pushresult(&b);
740}
741
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200742 static char_u *
743luaV_toline(lua_State *L, int pos)
744{
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200745 size_t l;
746 const char *s = lua_tolstring(L, pos, &l);
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200747
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200748 luaL_Buffer b;
749 luaL_buffinit(L, &b);
750 luaV_addlstring(&b, s, l, 1);
751 luaL_pushresult(&b);
752 return (char_u *) lua_tostring(L, -1);
753}
754
Bram Moolenaar2ab2e862019-12-04 21:24:53 +0100755/*
756 * pops a string s from the top of the stack and calls mf(t) for pieces t of
757 * s separated by newlines
758 */
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200759 static void
760luaV_msgfunc(lua_State *L, msgfunc_T mf)
761{
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200762 luaL_Buffer b;
763 size_t l;
764 const char *p, *s = lua_tolstring(L, -1, &l);
765 luaL_buffinit(L, &b);
766 luaV_addlstring(&b, s, l, 0);
767 luaL_pushresult(&b);
Bram Moolenaar2ab2e862019-12-04 21:24:53 +0100768 // break string
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200769 p = s = lua_tolstring(L, -1, &l);
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200770 while (l--)
771 {
Bram Moolenaar2ab2e862019-12-04 21:24:53 +0100772 if (*p++ == '\0') // break?
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200773 {
774 mf((char_u *) s);
775 s = p;
776 }
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200777 }
778 mf((char_u *) s);
Bram Moolenaar2ab2e862019-12-04 21:24:53 +0100779 lua_pop(L, 2); // original and modified strings
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200780}
781
Bram Moolenaar1dced572012-04-05 16:54:08 +0200782#define luaV_newtype(typ,tname,luatyp,luatname) \
783 static luatyp * \
Bram Moolenaar4eefe472019-03-19 21:59:19 +0100784 luaV_new##tname(lua_State *L, typ *obj) \
Bram Moolenaar1dced572012-04-05 16:54:08 +0200785 { \
786 luatyp *o = (luatyp *) lua_newuserdata(L, sizeof(luatyp)); \
787 *o = obj; \
788 luaV_setudata(L, obj); /* cache[obj] = udata */ \
789 luaV_getfield(L, luatname); \
790 lua_setmetatable(L, -2); \
791 return o; \
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200792 }
Bram Moolenaar1dced572012-04-05 16:54:08 +0200793
794#define luaV_pushtype(typ,tname,luatyp) \
795 static luatyp * \
Bram Moolenaarca06da92018-07-01 15:12:05 +0200796 luaV_push##tname(lua_State *L, typ *obj) \
Bram Moolenaar1dced572012-04-05 16:54:08 +0200797 { \
798 luatyp *o = NULL; \
799 if (obj == NULL) \
800 lua_pushnil(L); \
801 else { \
802 luaV_getudata(L, obj); \
803 if (lua_isnil(L, -1)) /* not interned? */ \
804 { \
805 lua_pop(L, 1); \
806 o = luaV_new##tname(L, obj); \
807 } \
808 else \
809 o = (luatyp *) lua_touserdata(L, -1); \
810 } \
811 return o; \
812 }
813
814#define luaV_type_tostring(tname,luatname) \
815 static int \
Bram Moolenaar4eefe472019-03-19 21:59:19 +0100816 luaV_##tname##_tostring(lua_State *L) \
Bram Moolenaar1dced572012-04-05 16:54:08 +0200817 { \
818 lua_pushfstring(L, "%s: %p", luatname, lua_touserdata(L, 1)); \
819 return 1; \
820 }
821
Bram Moolenaar2ab2e862019-12-04 21:24:53 +0100822// ======= List type =======
Bram Moolenaar1dced572012-04-05 16:54:08 +0200823
824 static luaV_List *
Bram Moolenaar4eefe472019-03-19 21:59:19 +0100825luaV_newlist(lua_State *L, list_T *lis)
Bram Moolenaar1dced572012-04-05 16:54:08 +0200826{
827 luaV_List *l = (luaV_List *) lua_newuserdata(L, sizeof(luaV_List));
828 *l = lis;
Bram Moolenaar2ab2e862019-12-04 21:24:53 +0100829 lis->lv_refcount++; // reference in Lua
830 luaV_setudata(L, lis); // cache[lis] = udata
Bram Moolenaar1dced572012-04-05 16:54:08 +0200831 luaV_getfield(L, LUAVIM_LIST);
832 lua_setmetatable(L, -2);
833 return l;
834}
835
836luaV_pushtype(list_T, list, luaV_List)
837luaV_type_tostring(list, LUAVIM_LIST)
838
839 static int
Bram Moolenaar4eefe472019-03-19 21:59:19 +0100840luaV_list_len(lua_State *L)
Bram Moolenaar1dced572012-04-05 16:54:08 +0200841{
842 list_T *l = luaV_unbox(L, luaV_List, 1);
Bram Moolenaarb7828692019-03-23 13:57:02 +0100843 lua_pushinteger(L, (int) list_len(l));
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200844 return 1;
845}
846
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200847 static int
Bram Moolenaar4eefe472019-03-19 21:59:19 +0100848luaV_list_iter(lua_State *L)
Bram Moolenaar1dced572012-04-05 16:54:08 +0200849{
850 listitem_T *li = (listitem_T *) lua_touserdata(L, lua_upvalueindex(2));
851 if (li == NULL) return 0;
852 luaV_pushtypval(L, &li->li_tv);
853 lua_pushlightuserdata(L, (void *) li->li_next);
854 lua_replace(L, lua_upvalueindex(2));
855 return 1;
856}
857
858 static int
Bram Moolenaar4eefe472019-03-19 21:59:19 +0100859luaV_list_call(lua_State *L)
Bram Moolenaar1dced572012-04-05 16:54:08 +0200860{
861 list_T *l = luaV_unbox(L, luaV_List, 1);
Bram Moolenaar2ab2e862019-12-04 21:24:53 +0100862 lua_pushvalue(L, lua_upvalueindex(1)); // pass cache table along
Bram Moolenaar1dced572012-04-05 16:54:08 +0200863 lua_pushlightuserdata(L, (void *) l->lv_first);
864 lua_pushcclosure(L, luaV_list_iter, 2);
865 return 1;
866}
867
868 static int
Bram Moolenaar4eefe472019-03-19 21:59:19 +0100869luaV_list_index(lua_State *L)
Bram Moolenaar1dced572012-04-05 16:54:08 +0200870{
871 list_T *l = luaV_unbox(L, luaV_List, 1);
Bram Moolenaar2ab2e862019-12-04 21:24:53 +0100872 if (lua_isnumber(L, 2)) // list item?
Bram Moolenaar1dced572012-04-05 16:54:08 +0200873 {
Bram Moolenaarbd846172020-06-27 12:32:57 +0200874 long n = (long) luaL_checkinteger(L, 2);
875 listitem_T *li;
876
877 // Lua array index starts with 1 while Vim uses 0, subtract 1 to
878 // normalize.
879 n -= 1;
880 li = list_find(l, n);
Bram Moolenaar1dced572012-04-05 16:54:08 +0200881 if (li == NULL)
882 lua_pushnil(L);
883 else
884 luaV_pushtypval(L, &li->li_tv);
885 }
Bram Moolenaar2ab2e862019-12-04 21:24:53 +0100886 else if (lua_isstring(L, 2)) // method?
Bram Moolenaar1dced572012-04-05 16:54:08 +0200887 {
888 const char *s = lua_tostring(L, 2);
889 if (strncmp(s, "add", 3) == 0
Bram Moolenaarb3766472013-04-15 13:49:21 +0200890 || strncmp(s, "insert", 6) == 0)
Bram Moolenaar1dced572012-04-05 16:54:08 +0200891 {
892 lua_getmetatable(L, 1);
893 lua_getfield(L, -1, s);
894 }
895 else
896 lua_pushnil(L);
897 }
898 else
899 lua_pushnil(L);
900 return 1;
901}
902
903 static int
Bram Moolenaar4eefe472019-03-19 21:59:19 +0100904luaV_list_newindex(lua_State *L)
Bram Moolenaar1dced572012-04-05 16:54:08 +0200905{
906 list_T *l = luaV_unbox(L, luaV_List, 1);
907 long n = (long) luaL_checkinteger(L, 2);
908 listitem_T *li;
Bram Moolenaarbd846172020-06-27 12:32:57 +0200909
910 // Lua array index starts with 1 while Vim uses 0, subtract 1 to normalize.
911 n -= 1;
912
Bram Moolenaar1dced572012-04-05 16:54:08 +0200913 if (l->lv_lock)
914 luaL_error(L, "list is locked");
915 li = list_find(l, n);
916 if (li == NULL) return 0;
Bram Moolenaar2ab2e862019-12-04 21:24:53 +0100917 if (lua_isnil(L, 3)) // remove?
Bram Moolenaar1dced572012-04-05 16:54:08 +0200918 {
Bram Moolenaar3ec7f4e2014-05-07 17:31:37 +0200919 vimlist_remove(l, li, li);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100920 listitem_free(l, li);
Bram Moolenaar1dced572012-04-05 16:54:08 +0200921 }
922 else
923 {
924 typval_T v;
Bram Moolenaarca06da92018-07-01 15:12:05 +0200925 luaV_checktypval(L, 3, &v, "setting list item");
Bram Moolenaar1dced572012-04-05 16:54:08 +0200926 clear_tv(&li->li_tv);
927 copy_tv(&v, &li->li_tv);
Bram Moolenaarb3766472013-04-15 13:49:21 +0200928 clear_tv(&v);
Bram Moolenaar1dced572012-04-05 16:54:08 +0200929 }
930 return 0;
931}
932
933 static int
Bram Moolenaar4eefe472019-03-19 21:59:19 +0100934luaV_list_add(lua_State *L)
Bram Moolenaar1dced572012-04-05 16:54:08 +0200935{
936 luaV_List *lis = luaV_checkudata(L, 1, LUAVIM_LIST);
937 list_T *l = (list_T *) luaV_checkcache(L, (void *) *lis);
Bram Moolenaarb3766472013-04-15 13:49:21 +0200938 typval_T v;
Bram Moolenaar1dced572012-04-05 16:54:08 +0200939 if (l->lv_lock)
940 luaL_error(L, "list is locked");
Bram Moolenaarb3766472013-04-15 13:49:21 +0200941 lua_settop(L, 2);
Bram Moolenaarca06da92018-07-01 15:12:05 +0200942 luaV_checktypval(L, 2, &v, "adding list item");
Bram Moolenaarb3766472013-04-15 13:49:21 +0200943 if (list_append_tv(l, &v) == FAIL)
Bram Moolenaarca06da92018-07-01 15:12:05 +0200944 luaL_error(L, "failed to add item to list");
Bram Moolenaarb3766472013-04-15 13:49:21 +0200945 clear_tv(&v);
Bram Moolenaar1dced572012-04-05 16:54:08 +0200946 lua_settop(L, 1);
947 return 1;
948}
949
950 static int
Bram Moolenaar4eefe472019-03-19 21:59:19 +0100951luaV_list_insert(lua_State *L)
Bram Moolenaar1dced572012-04-05 16:54:08 +0200952{
953 luaV_List *lis = luaV_checkudata(L, 1, LUAVIM_LIST);
954 list_T *l = (list_T *) luaV_checkcache(L, (void *) *lis);
Bram Moolenaar46538ee2015-02-17 16:28:55 +0100955 long pos = (long) luaL_optinteger(L, 3, 0);
Bram Moolenaar1dced572012-04-05 16:54:08 +0200956 listitem_T *li = NULL;
957 typval_T v;
958 if (l->lv_lock)
959 luaL_error(L, "list is locked");
960 if (pos < l->lv_len)
961 {
962 li = list_find(l, pos);
963 if (li == NULL)
964 luaL_error(L, "invalid position");
965 }
966 lua_settop(L, 2);
Bram Moolenaarca06da92018-07-01 15:12:05 +0200967 luaV_checktypval(L, 2, &v, "inserting list item");
Bram Moolenaarb3766472013-04-15 13:49:21 +0200968 if (list_insert_tv(l, &v, li) == FAIL)
Bram Moolenaarca06da92018-07-01 15:12:05 +0200969 luaL_error(L, "failed to add item to list");
Bram Moolenaarb3766472013-04-15 13:49:21 +0200970 clear_tv(&v);
Bram Moolenaar1dced572012-04-05 16:54:08 +0200971 lua_settop(L, 1);
972 return 1;
973}
974
975static const luaL_Reg luaV_List_mt[] = {
976 {"__tostring", luaV_list_tostring},
Bram Moolenaar1dced572012-04-05 16:54:08 +0200977 {"__len", luaV_list_len},
978 {"__call", luaV_list_call},
979 {"__index", luaV_list_index},
980 {"__newindex", luaV_list_newindex},
981 {"add", luaV_list_add},
982 {"insert", luaV_list_insert},
983 {NULL, NULL}
984};
985
986
Bram Moolenaar2ab2e862019-12-04 21:24:53 +0100987// ======= Dict type =======
Bram Moolenaar1dced572012-04-05 16:54:08 +0200988
989 static luaV_Dict *
Bram Moolenaar4eefe472019-03-19 21:59:19 +0100990luaV_newdict(lua_State *L, dict_T *dic)
Bram Moolenaar1dced572012-04-05 16:54:08 +0200991{
992 luaV_Dict *d = (luaV_Dict *) lua_newuserdata(L, sizeof(luaV_Dict));
993 *d = dic;
Bram Moolenaar2ab2e862019-12-04 21:24:53 +0100994 dic->dv_refcount++; // reference in Lua
995 luaV_setudata(L, dic); // cache[dic] = udata
Bram Moolenaar1dced572012-04-05 16:54:08 +0200996 luaV_getfield(L, LUAVIM_DICT);
997 lua_setmetatable(L, -2);
998 return d;
999}
1000
1001luaV_pushtype(dict_T, dict, luaV_Dict)
1002luaV_type_tostring(dict, LUAVIM_DICT)
1003
1004 static int
Bram Moolenaar4eefe472019-03-19 21:59:19 +01001005luaV_dict_len(lua_State *L)
Bram Moolenaar1dced572012-04-05 16:54:08 +02001006{
1007 dict_T *d = luaV_unbox(L, luaV_Dict, 1);
Bram Moolenaarb7828692019-03-23 13:57:02 +01001008 lua_pushinteger(L, (int) dict_len(d));
Bram Moolenaar1dced572012-04-05 16:54:08 +02001009 return 1;
1010}
1011
1012 static int
Bram Moolenaar4eefe472019-03-19 21:59:19 +01001013luaV_dict_iter(lua_State *L UNUSED)
Bram Moolenaar1dced572012-04-05 16:54:08 +02001014{
Bram Moolenaarfeeaa682013-02-14 22:19:51 +01001015#ifdef FEAT_EVAL
Bram Moolenaar1dced572012-04-05 16:54:08 +02001016 hashitem_T *hi = (hashitem_T *) lua_touserdata(L, lua_upvalueindex(2));
1017 int n = lua_tointeger(L, lua_upvalueindex(3));
1018 dictitem_T *di;
1019 if (n <= 0) return 0;
1020 while (HASHITEM_EMPTY(hi)) hi++;
1021 di = dict_lookup(hi);
1022 lua_pushstring(L, (char *) hi->hi_key);
1023 luaV_pushtypval(L, &di->di_tv);
1024 lua_pushlightuserdata(L, (void *) (hi + 1));
1025 lua_replace(L, lua_upvalueindex(2));
1026 lua_pushinteger(L, n - 1);
1027 lua_replace(L, lua_upvalueindex(3));
1028 return 2;
Bram Moolenaarfeeaa682013-02-14 22:19:51 +01001029#else
1030 return 0;
1031#endif
Bram Moolenaar1dced572012-04-05 16:54:08 +02001032}
1033
1034 static int
Bram Moolenaar4eefe472019-03-19 21:59:19 +01001035luaV_dict_call(lua_State *L)
Bram Moolenaar1dced572012-04-05 16:54:08 +02001036{
1037 dict_T *d = luaV_unbox(L, luaV_Dict, 1);
1038 hashtab_T *ht = &d->dv_hashtab;
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001039 lua_pushvalue(L, lua_upvalueindex(1)); // pass cache table along
Bram Moolenaar1dced572012-04-05 16:54:08 +02001040 lua_pushlightuserdata(L, (void *) ht->ht_array);
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001041 lua_pushinteger(L, ht->ht_used); // # remaining items
Bram Moolenaar1dced572012-04-05 16:54:08 +02001042 lua_pushcclosure(L, luaV_dict_iter, 3);
1043 return 1;
1044}
1045
1046 static int
Bram Moolenaarca06da92018-07-01 15:12:05 +02001047luaV_dict_index(lua_State *L)
Bram Moolenaar1dced572012-04-05 16:54:08 +02001048{
1049 dict_T *d = luaV_unbox(L, luaV_Dict, 1);
1050 char_u *key = (char_u *) luaL_checkstring(L, 2);
1051 dictitem_T *di = dict_find(d, key, -1);
Bram Moolenaarca06da92018-07-01 15:12:05 +02001052
Bram Moolenaar1dced572012-04-05 16:54:08 +02001053 if (di == NULL)
1054 lua_pushnil(L);
1055 else
Bram Moolenaarca06da92018-07-01 15:12:05 +02001056 {
Bram Moolenaar1dced572012-04-05 16:54:08 +02001057 luaV_pushtypval(L, &di->di_tv);
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001058 if (di->di_tv.v_type == VAR_FUNC) // funcref?
Bram Moolenaarca06da92018-07-01 15:12:05 +02001059 {
1060 luaV_Funcref *f = (luaV_Funcref *) lua_touserdata(L, -1);
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001061 f->self = d; // keep "self" reference
Bram Moolenaarca06da92018-07-01 15:12:05 +02001062 d->dv_refcount++;
1063 }
1064 }
Bram Moolenaar1dced572012-04-05 16:54:08 +02001065 return 1;
1066}
1067
1068 static int
Bram Moolenaarca06da92018-07-01 15:12:05 +02001069luaV_dict_newindex(lua_State *L)
Bram Moolenaar1dced572012-04-05 16:54:08 +02001070{
1071 dict_T *d = luaV_unbox(L, luaV_Dict, 1);
1072 char_u *key = (char_u *) luaL_checkstring(L, 2);
1073 dictitem_T *di;
Bram Moolenaarca06da92018-07-01 15:12:05 +02001074 typval_T v;
Bram Moolenaar713bf9e92019-03-16 16:38:41 +01001075
Bram Moolenaar1dced572012-04-05 16:54:08 +02001076 if (d->dv_lock)
1077 luaL_error(L, "dict is locked");
Bram Moolenaard6ef5f92018-07-13 22:08:23 +02001078 if (key == NULL)
1079 return 0;
1080 if (*key == NUL)
Bram Moolenaarca06da92018-07-01 15:12:05 +02001081 luaL_error(L, "empty key");
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001082 if (!lua_isnil(L, 3)) // read value?
Bram Moolenaar17413672018-07-14 20:49:42 +02001083 {
Bram Moolenaarca06da92018-07-01 15:12:05 +02001084 luaV_checktypval(L, 3, &v, "setting dict item");
1085 if (d->dv_scope == VAR_DEF_SCOPE && v.v_type == VAR_FUNC)
1086 luaL_error(L, "cannot assign funcref to builtin scope");
1087 }
Bram Moolenaar1dced572012-04-05 16:54:08 +02001088 di = dict_find(d, key, -1);
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001089 if (di == NULL) // non-existing key?
Bram Moolenaar1dced572012-04-05 16:54:08 +02001090 {
Bram Moolenaard6ef5f92018-07-13 22:08:23 +02001091 if (lua_isnil(L, 3))
1092 return 0;
Bram Moolenaar1dced572012-04-05 16:54:08 +02001093 di = dictitem_alloc(key);
Bram Moolenaard6ef5f92018-07-13 22:08:23 +02001094 if (di == NULL)
1095 return 0;
Bram Moolenaar1dced572012-04-05 16:54:08 +02001096 if (dict_add(d, di) == FAIL)
1097 {
Bram Moolenaard6ef5f92018-07-13 22:08:23 +02001098 vim_free(di);
1099 return 0;
Bram Moolenaar1dced572012-04-05 16:54:08 +02001100 }
1101 }
1102 else
1103 clear_tv(&di->di_tv);
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001104 if (lua_isnil(L, 3)) // remove?
Bram Moolenaar1dced572012-04-05 16:54:08 +02001105 {
1106 hashitem_T *hi = hash_find(&d->dv_hashtab, di->di_key);
1107 hash_remove(&d->dv_hashtab, hi);
1108 dictitem_free(di);
1109 }
Bram Moolenaarca06da92018-07-01 15:12:05 +02001110 else
1111 {
Bram Moolenaar1dced572012-04-05 16:54:08 +02001112 copy_tv(&v, &di->di_tv);
Bram Moolenaarb3766472013-04-15 13:49:21 +02001113 clear_tv(&v);
Bram Moolenaar1dced572012-04-05 16:54:08 +02001114 }
1115 return 0;
1116}
1117
1118static const luaL_Reg luaV_Dict_mt[] = {
1119 {"__tostring", luaV_dict_tostring},
Bram Moolenaar1dced572012-04-05 16:54:08 +02001120 {"__len", luaV_dict_len},
1121 {"__call", luaV_dict_call},
1122 {"__index", luaV_dict_index},
1123 {"__newindex", luaV_dict_newindex},
1124 {NULL, NULL}
1125};
1126
1127
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001128// ======= Blob type =======
Bram Moolenaarb7828692019-03-23 13:57:02 +01001129
1130 static luaV_Blob *
1131luaV_newblob(lua_State *L, blob_T *blo)
1132{
1133 luaV_Blob *b = (luaV_Blob *) lua_newuserdata(L, sizeof(luaV_Blob));
1134 *b = blo;
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001135 blo->bv_refcount++; // reference in Lua
1136 luaV_setudata(L, blo); // cache[blo] = udata
Bram Moolenaarb7828692019-03-23 13:57:02 +01001137 luaV_getfield(L, LUAVIM_BLOB);
1138 lua_setmetatable(L, -2);
1139 return b;
1140}
1141
1142luaV_pushtype(blob_T, blob, luaV_Blob)
1143luaV_type_tostring(blob, LUAVIM_BLOB)
1144
1145 static int
1146luaV_blob_gc(lua_State *L)
1147{
1148 blob_T *b = luaV_unbox(L, luaV_Blob, 1);
1149 blob_unref(b);
1150 return 0;
1151}
1152
1153 static int
1154luaV_blob_len(lua_State *L)
1155{
1156 blob_T *b = luaV_unbox(L, luaV_Blob, 1);
1157 lua_pushinteger(L, (int) blob_len(b));
1158 return 1;
1159}
1160
1161 static int
1162luaV_blob_index(lua_State *L)
1163{
1164 blob_T *b = luaV_unbox(L, luaV_Blob, 1);
1165 if (lua_isnumber(L, 2))
1166 {
1167 int idx = luaL_checkinteger(L, 2);
1168 if (idx < blob_len(b))
1169 lua_pushnumber(L, (lua_Number) blob_get(b, idx));
1170 else
1171 lua_pushnil(L);
1172 }
1173 else if (lua_isstring(L, 2))
1174 {
1175 const char *s = lua_tostring(L, 2);
1176 if (strncmp(s, "add", 3) == 0)
1177 {
1178 lua_getmetatable(L, 1);
1179 lua_getfield(L, -1, s);
1180 }
1181 else
1182 lua_pushnil(L);
1183 }
1184 else
1185 lua_pushnil(L);
1186 return 1;
1187}
1188
1189 static int
1190luaV_blob_newindex(lua_State *L)
1191{
1192 blob_T *b = luaV_unbox(L, luaV_Blob, 1);
1193 if (b->bv_lock)
1194 luaL_error(L, "blob is locked");
1195 if (lua_isnumber(L, 2))
1196 {
1197 long len = blob_len(b);
1198 int idx = luaL_checkinteger(L, 2);
1199 int val = luaL_checkinteger(L, 3);
1200 if (idx < len || (idx == len && ga_grow(&b->bv_ga, 1) == OK))
1201 {
1202 blob_set(b, idx, (char_u) val);
1203 if (idx == len)
1204 ++b->bv_ga.ga_len;
1205 }
1206 else
1207 luaL_error(L, "index out of range");
1208 }
1209 return 0;
1210}
1211
1212 static int
1213luaV_blob_add(lua_State *L)
1214{
1215 luaV_Blob *blo = luaV_checkudata(L, 1, LUAVIM_BLOB);
1216 blob_T *b = (blob_T *) luaV_checkcache(L, (void *) *blo);
1217 if (b->bv_lock)
1218 luaL_error(L, "blob is locked");
1219 lua_settop(L, 2);
1220 if (!lua_isstring(L, 2))
1221 luaL_error(L, "string expected, got %s", luaL_typename(L, 2));
1222 else
1223 {
1224 size_t i, l = 0;
1225 const char *s = lua_tolstring(L, 2, &l);
1226
Bram Moolenaar5f1d3ae2020-02-11 22:37:35 +01001227 if (ga_grow(&b->bv_ga, (int)l) == OK)
Bram Moolenaar6fb5c972019-03-26 21:44:20 +01001228 for (i = 0; i < l; ++i)
1229 ga_append(&b->bv_ga, s[i]);
Bram Moolenaarb7828692019-03-23 13:57:02 +01001230 }
1231 lua_settop(L, 1);
1232 return 1;
1233}
1234
1235static const luaL_Reg luaV_Blob_mt[] = {
1236 {"__tostring", luaV_blob_tostring},
1237 {"__gc", luaV_blob_gc},
1238 {"__len", luaV_blob_len},
1239 {"__index", luaV_blob_index},
1240 {"__newindex", luaV_blob_newindex},
1241 {"add", luaV_blob_add},
1242 {NULL, NULL}
1243};
1244
1245
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001246// ======= Funcref type =======
Bram Moolenaarca06da92018-07-01 15:12:05 +02001247
1248 static luaV_Funcref *
1249luaV_newfuncref(lua_State *L, char_u *name)
1250{
1251 luaV_Funcref *f = (luaV_Funcref *)lua_newuserdata(L, sizeof(luaV_Funcref));
1252
1253 if (name != NULL)
1254 {
Bram Moolenaar4eefe472019-03-19 21:59:19 +01001255 func_ref(name);
1256 f->name = vim_strsave(name);
Bram Moolenaarca06da92018-07-01 15:12:05 +02001257 }
Bram Moolenaarca06da92018-07-01 15:12:05 +02001258 f->self = NULL;
1259 luaV_getfield(L, LUAVIM_FUNCREF);
1260 lua_setmetatable(L, -2);
1261 return f;
1262}
1263
1264 static luaV_Funcref *
Bram Moolenaar4eefe472019-03-19 21:59:19 +01001265luaV_pushfuncref(lua_State *L, char_u *name)
Bram Moolenaarca06da92018-07-01 15:12:05 +02001266{
Bram Moolenaar4eefe472019-03-19 21:59:19 +01001267 return luaV_newfuncref(L, name);
Bram Moolenaarca06da92018-07-01 15:12:05 +02001268}
1269
1270
1271luaV_type_tostring(funcref, LUAVIM_FUNCREF)
1272
1273 static int
1274luaV_funcref_gc(lua_State *L)
1275{
1276 luaV_Funcref *f = (luaV_Funcref *) lua_touserdata(L, 1);
1277
Bram Moolenaar4eefe472019-03-19 21:59:19 +01001278 func_unref(f->name);
1279 vim_free(f->name);
1280 // NOTE: Don't call "dict_unref(f->self)", because the dict of "f->self"
1281 // will be (or has been already) freed by Vim's garbage collection.
Bram Moolenaarca06da92018-07-01 15:12:05 +02001282 return 0;
1283}
1284
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001285// equivalent to string(funcref)
Bram Moolenaarca06da92018-07-01 15:12:05 +02001286 static int
1287luaV_funcref_len(lua_State *L)
1288{
1289 luaV_Funcref *f = (luaV_Funcref *) lua_touserdata(L, 1);
1290
Bram Moolenaar4eefe472019-03-19 21:59:19 +01001291 lua_pushstring(L, (const char *) f->name);
Bram Moolenaarca06da92018-07-01 15:12:05 +02001292 return 1;
1293}
1294
1295 static int
1296luaV_funcref_call(lua_State *L)
1297{
1298 luaV_Funcref *f = (luaV_Funcref *) lua_touserdata(L, 1);
Bram Moolenaar4eefe472019-03-19 21:59:19 +01001299 int i, n = lua_gettop(L) - 1; // #args
1300 int status = FAIL;
1301 typval_T args;
1302 typval_T rettv;
Bram Moolenaarca06da92018-07-01 15:12:05 +02001303
Bram Moolenaar4eefe472019-03-19 21:59:19 +01001304 args.v_type = VAR_LIST;
1305 args.vval.v_list = list_alloc();
1306 rettv.v_type = VAR_UNKNOWN; // as in clear_tv
1307 if (args.vval.v_list != NULL)
Bram Moolenaard6ef5f92018-07-13 22:08:23 +02001308 {
Bram Moolenaar4eefe472019-03-19 21:59:19 +01001309 typval_T v;
1310
Bram Moolenaar17413672018-07-14 20:49:42 +02001311 for (i = 0; i < n; i++)
1312 {
Bram Moolenaard6ef5f92018-07-13 22:08:23 +02001313 luaV_checktypval(L, i + 2, &v, "calling funcref");
Bram Moolenaar4eefe472019-03-19 21:59:19 +01001314 list_append_tv(args.vval.v_list, &v);
Bram Moolenaar713bf9e92019-03-16 16:38:41 +01001315 clear_tv(&v);
Bram Moolenaard6ef5f92018-07-13 22:08:23 +02001316 }
Bram Moolenaar4eefe472019-03-19 21:59:19 +01001317 status = func_call(f->name, &args, NULL, f->self, &rettv);
Bram Moolenaard6ef5f92018-07-13 22:08:23 +02001318 if (status == OK)
1319 luaV_pushtypval(L, &rettv);
Bram Moolenaar4eefe472019-03-19 21:59:19 +01001320 clear_tv(&args);
Bram Moolenaard6ef5f92018-07-13 22:08:23 +02001321 clear_tv(&rettv);
Bram Moolenaarca06da92018-07-01 15:12:05 +02001322 }
Bram Moolenaarca06da92018-07-01 15:12:05 +02001323 if (status != OK)
1324 luaL_error(L, "cannot call funcref");
1325 return 1;
1326}
1327
1328static const luaL_Reg luaV_Funcref_mt[] = {
1329 {"__tostring", luaV_funcref_tostring},
1330 {"__gc", luaV_funcref_gc},
1331 {"__len", luaV_funcref_len},
1332 {"__call", luaV_funcref_call},
1333 {NULL, NULL}
1334};
1335
1336
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001337// ======= Buffer type =======
Bram Moolenaar1dced572012-04-05 16:54:08 +02001338
1339luaV_newtype(buf_T, buffer, luaV_Buffer, LUAVIM_BUFFER)
1340luaV_pushtype(buf_T, buffer, luaV_Buffer)
1341luaV_type_tostring(buffer, LUAVIM_BUFFER)
1342
1343 static int
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001344luaV_buffer_len(lua_State *L)
1345{
Bram Moolenaar1dced572012-04-05 16:54:08 +02001346 buf_T *b = (buf_T *) luaV_checkvalid(L, luaV_Buffer, 1);
1347 lua_pushinteger(L, b->b_ml.ml_line_count);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001348 return 1;
1349}
1350
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001351 static int
1352luaV_buffer_call(lua_State *L)
1353{
Bram Moolenaar1dced572012-04-05 16:54:08 +02001354 buf_T *b = (buf_T *) luaV_checkvalid(L, luaV_Buffer, 1);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001355 lua_settop(L, 1);
Bram Moolenaar1dced572012-04-05 16:54:08 +02001356 set_curbuf(b, DOBUF_SPLIT);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001357 return 1;
1358}
1359
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001360 static int
1361luaV_buffer_index(lua_State *L)
1362{
Bram Moolenaar1dced572012-04-05 16:54:08 +02001363 buf_T *b = (buf_T *) luaV_checkvalid(L, luaV_Buffer, 1);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001364 linenr_T n = (linenr_T) lua_tointeger(L, 2);
Bram Moolenaar1dced572012-04-05 16:54:08 +02001365 if (n > 0 && n <= b->b_ml.ml_line_count)
1366 luaV_pushline(L, b, n);
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001367 else if (lua_isstring(L, 2))
1368 {
1369 const char *s = lua_tostring(L, 2);
1370 if (strncmp(s, "name", 4) == 0)
Bram Moolenaarfe08df42018-07-07 23:07:41 +02001371 lua_pushstring(L, (b->b_sfname == NULL)
1372 ? "" : (char *) b->b_sfname);
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001373 else if (strncmp(s, "fname", 5) == 0)
Bram Moolenaarfe08df42018-07-07 23:07:41 +02001374 lua_pushstring(L, (b->b_ffname == NULL)
1375 ? "" : (char *) b->b_ffname);
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001376 else if (strncmp(s, "number", 6) == 0)
Bram Moolenaar1dced572012-04-05 16:54:08 +02001377 lua_pushinteger(L, b->b_fnum);
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001378 // methods
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001379 else if (strncmp(s, "insert", 6) == 0
1380 || strncmp(s, "next", 4) == 0
1381 || strncmp(s, "previous", 8) == 0
1382 || strncmp(s, "isvalid", 7) == 0)
1383 {
1384 lua_getmetatable(L, 1);
1385 lua_getfield(L, -1, s);
1386 }
1387 else
1388 lua_pushnil(L);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001389 }
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001390 else
1391 lua_pushnil(L);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001392 return 1;
1393}
1394
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001395 static int
1396luaV_buffer_newindex(lua_State *L)
1397{
Bram Moolenaar1dced572012-04-05 16:54:08 +02001398 buf_T *b = (buf_T *) luaV_checkvalid(L, luaV_Buffer, 1);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001399 linenr_T n = (linenr_T) luaL_checkinteger(L, 2);
1400#ifdef HAVE_SANDBOX
1401 luaV_checksandbox(L);
1402#endif
Bram Moolenaar1dced572012-04-05 16:54:08 +02001403 if (n < 1 || n > b->b_ml.ml_line_count)
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001404 luaL_error(L, "invalid line number");
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001405 if (lua_isnil(L, 3)) // delete line
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001406 {
1407 buf_T *buf = curbuf;
Bram Moolenaar1dced572012-04-05 16:54:08 +02001408 curbuf = b;
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001409 if (u_savedel(n, 1L) == FAIL)
1410 {
1411 curbuf = buf;
1412 luaL_error(L, "cannot save undo information");
1413 }
Bram Moolenaarca70c072020-05-30 20:30:46 +02001414 else if (ml_delete(n) == FAIL)
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001415 {
1416 curbuf = buf;
1417 luaL_error(L, "cannot delete line");
1418 }
Bram Moolenaarca06da92018-07-01 15:12:05 +02001419 else
1420 {
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001421 deleted_lines_mark(n, 1L);
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001422 if (b == curwin->w_buffer) // fix cursor in current window?
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001423 {
1424 if (curwin->w_cursor.lnum >= n)
1425 {
1426 if (curwin->w_cursor.lnum > n)
1427 {
1428 curwin->w_cursor.lnum -= 1;
1429 check_cursor_col();
1430 }
1431 else check_cursor();
1432 changed_cline_bef_curs();
1433 }
1434 invalidate_botline();
1435 }
1436 }
1437 curbuf = buf;
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001438 }
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001439 else if (lua_isstring(L, 3)) // update line
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001440 {
1441 buf_T *buf = curbuf;
Bram Moolenaar1dced572012-04-05 16:54:08 +02001442 curbuf = b;
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001443 if (u_savesub(n) == FAIL)
1444 {
1445 curbuf = buf;
1446 luaL_error(L, "cannot save undo information");
1447 }
1448 else if (ml_replace(n, luaV_toline(L, 3), TRUE) == FAIL)
1449 {
1450 curbuf = buf;
1451 luaL_error(L, "cannot replace line");
1452 }
1453 else changed_bytes(n, 0);
1454 curbuf = buf;
Bram Moolenaar1dced572012-04-05 16:54:08 +02001455 if (b == curwin->w_buffer)
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001456 check_cursor_col();
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001457 }
1458 else
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001459 luaL_error(L, "wrong argument to change line");
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001460 return 0;
1461}
1462
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001463 static int
1464luaV_buffer_insert(lua_State *L)
1465{
Bram Moolenaar1dced572012-04-05 16:54:08 +02001466 luaV_Buffer *lb = luaV_checkudata(L, 1, LUAVIM_BUFFER);
1467 buf_T *b = (buf_T *) luaV_checkcache(L, (void *) *lb);
1468 linenr_T last = b->b_ml.ml_line_count;
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001469 linenr_T n = (linenr_T) luaL_optinteger(L, 3, last);
1470 buf_T *buf;
1471 luaL_checktype(L, 2, LUA_TSTRING);
1472#ifdef HAVE_SANDBOX
1473 luaV_checksandbox(L);
1474#endif
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001475 // fix insertion line
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001476 if (n < 0) n = 0;
1477 if (n > last) n = last;
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001478 // insert
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001479 buf = curbuf;
Bram Moolenaar1dced572012-04-05 16:54:08 +02001480 curbuf = b;
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001481 if (u_save(n, n + 1) == FAIL)
1482 {
1483 curbuf = buf;
1484 luaL_error(L, "cannot save undo information");
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001485 }
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001486 else if (ml_append(n, luaV_toline(L, 2), 0, FALSE) == FAIL)
1487 {
1488 curbuf = buf;
1489 luaL_error(L, "cannot insert line");
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001490 }
1491 else
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001492 appended_lines_mark(n, 1L);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001493 curbuf = buf;
1494 update_screen(VALID);
1495 return 0;
1496}
1497
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001498 static int
1499luaV_buffer_next(lua_State *L)
1500{
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001501 luaV_Buffer *b = luaV_checkudata(L, 1, LUAVIM_BUFFER);
Bram Moolenaar1dced572012-04-05 16:54:08 +02001502 buf_T *buf = (buf_T *) luaV_checkcache(L, (void *) *b);
1503 luaV_pushbuffer(L, buf->b_next);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001504 return 1;
1505}
1506
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001507 static int
1508luaV_buffer_previous(lua_State *L)
1509{
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001510 luaV_Buffer *b = luaV_checkudata(L, 1, LUAVIM_BUFFER);
Bram Moolenaar1dced572012-04-05 16:54:08 +02001511 buf_T *buf = (buf_T *) luaV_checkcache(L, (void *) *b);
1512 luaV_pushbuffer(L, buf->b_prev);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001513 return 1;
1514}
1515
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001516 static int
1517luaV_buffer_isvalid(lua_State *L)
1518{
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001519 luaV_Buffer *b = luaV_checkudata(L, 1, LUAVIM_BUFFER);
Bram Moolenaar1dced572012-04-05 16:54:08 +02001520 luaV_getudata(L, *b);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001521 lua_pushboolean(L, !lua_isnil(L, -1));
1522 return 1;
1523}
1524
1525static const luaL_Reg luaV_Buffer_mt[] = {
1526 {"__tostring", luaV_buffer_tostring},
1527 {"__len", luaV_buffer_len},
1528 {"__call", luaV_buffer_call},
1529 {"__index", luaV_buffer_index},
1530 {"__newindex", luaV_buffer_newindex},
1531 {"insert", luaV_buffer_insert},
1532 {"next", luaV_buffer_next},
1533 {"previous", luaV_buffer_previous},
1534 {"isvalid", luaV_buffer_isvalid},
1535 {NULL, NULL}
1536};
1537
1538
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001539// ======= Window type =======
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001540
Bram Moolenaar1dced572012-04-05 16:54:08 +02001541luaV_newtype(win_T, window, luaV_Window, LUAVIM_WINDOW)
1542luaV_pushtype(win_T, window, luaV_Window)
1543luaV_type_tostring(window, LUAVIM_WINDOW)
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001544
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001545 static int
1546luaV_window_call(lua_State *L)
1547{
Bram Moolenaar1dced572012-04-05 16:54:08 +02001548 win_T *w = (win_T *) luaV_checkvalid(L, luaV_Window, 1);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001549 lua_settop(L, 1);
Bram Moolenaar1dced572012-04-05 16:54:08 +02001550 win_goto(w);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001551 return 1;
1552}
1553
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001554 static int
1555luaV_window_index(lua_State *L)
1556{
Bram Moolenaar1dced572012-04-05 16:54:08 +02001557 win_T *w = (win_T *) luaV_checkvalid(L, luaV_Window, 1);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001558 const char *s = luaL_checkstring(L, 2);
1559 if (strncmp(s, "buffer", 6) == 0)
Bram Moolenaar1dced572012-04-05 16:54:08 +02001560 luaV_pushbuffer(L, w->w_buffer);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001561 else if (strncmp(s, "line", 4) == 0)
Bram Moolenaar1dced572012-04-05 16:54:08 +02001562 lua_pushinteger(L, w->w_cursor.lnum);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001563 else if (strncmp(s, "col", 3) == 0)
Bram Moolenaar1dced572012-04-05 16:54:08 +02001564 lua_pushinteger(L, w->w_cursor.col + 1);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001565 else if (strncmp(s, "width", 5) == 0)
Bram Moolenaar02631462017-09-22 15:20:32 +02001566 lua_pushinteger(L, w->w_width);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001567 else if (strncmp(s, "height", 6) == 0)
Bram Moolenaar1dced572012-04-05 16:54:08 +02001568 lua_pushinteger(L, w->w_height);
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001569 // methods
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001570 else if (strncmp(s, "next", 4) == 0
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001571 || strncmp(s, "previous", 8) == 0
1572 || strncmp(s, "isvalid", 7) == 0)
1573 {
1574 lua_getmetatable(L, 1);
1575 lua_getfield(L, -1, s);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001576 }
1577 else
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001578 lua_pushnil(L);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001579 return 1;
1580}
1581
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001582 static int
Bram Moolenaar4eefe472019-03-19 21:59:19 +01001583luaV_window_newindex(lua_State *L)
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001584{
Bram Moolenaar1dced572012-04-05 16:54:08 +02001585 win_T *w = (win_T *) luaV_checkvalid(L, luaV_Window, 1);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001586 const char *s = luaL_checkstring(L, 2);
1587 int v = luaL_checkinteger(L, 3);
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001588 if (strncmp(s, "line", 4) == 0)
1589 {
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001590#ifdef HAVE_SANDBOX
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001591 luaV_checksandbox(L);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001592#endif
Bram Moolenaar1dced572012-04-05 16:54:08 +02001593 if (v < 1 || v > w->w_buffer->b_ml.ml_line_count)
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001594 luaL_error(L, "line out of range");
Bram Moolenaar1dced572012-04-05 16:54:08 +02001595 w->w_cursor.lnum = v;
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001596 update_screen(VALID);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001597 }
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001598 else if (strncmp(s, "col", 3) == 0)
1599 {
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001600#ifdef HAVE_SANDBOX
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001601 luaV_checksandbox(L);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001602#endif
Bram Moolenaar1dced572012-04-05 16:54:08 +02001603 w->w_cursor.col = v - 1;
Bram Moolenaar53901442018-07-25 22:02:36 +02001604 w->w_set_curswant = TRUE;
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001605 update_screen(VALID);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001606 }
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001607 else if (strncmp(s, "width", 5) == 0)
1608 {
1609 win_T *win = curwin;
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001610#ifdef FEAT_GUI
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001611 need_mouse_correct = TRUE;
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001612#endif
Bram Moolenaar1dced572012-04-05 16:54:08 +02001613 curwin = w;
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001614 win_setwidth(v);
1615 curwin = win;
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001616 }
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001617 else if (strncmp(s, "height", 6) == 0)
1618 {
1619 win_T *win = curwin;
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001620#ifdef FEAT_GUI
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001621 need_mouse_correct = TRUE;
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001622#endif
Bram Moolenaar1dced572012-04-05 16:54:08 +02001623 curwin = w;
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001624 win_setheight(v);
1625 curwin = win;
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001626 }
1627 else
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001628 luaL_error(L, "invalid window property: `%s'", s);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001629 return 0;
1630}
1631
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001632 static int
1633luaV_window_next(lua_State *L)
1634{
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001635 luaV_Window *w = luaV_checkudata(L, 1, LUAVIM_WINDOW);
Bram Moolenaar1dced572012-04-05 16:54:08 +02001636 win_T *win = (win_T *) luaV_checkcache(L, (void *) *w);
1637 luaV_pushwindow(L, win->w_next);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001638 return 1;
1639}
1640
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001641 static int
1642luaV_window_previous(lua_State *L)
1643{
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001644 luaV_Window *w = luaV_checkudata(L, 1, LUAVIM_WINDOW);
Bram Moolenaar1dced572012-04-05 16:54:08 +02001645 win_T *win = (win_T *) luaV_checkcache(L, (void *) *w);
1646 luaV_pushwindow(L, win->w_prev);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001647 return 1;
1648}
1649
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001650 static int
1651luaV_window_isvalid(lua_State *L)
1652{
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001653 luaV_Window *w = luaV_checkudata(L, 1, LUAVIM_WINDOW);
Bram Moolenaar1dced572012-04-05 16:54:08 +02001654 luaV_getudata(L, *w);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001655 lua_pushboolean(L, !lua_isnil(L, -1));
1656 return 1;
1657}
1658
1659static const luaL_Reg luaV_Window_mt[] = {
1660 {"__tostring", luaV_window_tostring},
1661 {"__call", luaV_window_call},
1662 {"__index", luaV_window_index},
1663 {"__newindex", luaV_window_newindex},
1664 {"next", luaV_window_next},
1665 {"previous", luaV_window_previous},
1666 {"isvalid", luaV_window_isvalid},
1667 {NULL, NULL}
1668};
1669
1670
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001671// ======= Vim module =======
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001672
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001673 static int
1674luaV_print(lua_State *L)
1675{
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001676 int i, n = lua_gettop(L); // nargs
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001677 const char *s;
1678 size_t l;
1679 luaL_Buffer b;
1680 luaL_buffinit(L, &b);
1681 lua_getglobal(L, "tostring");
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001682 for (i = 1; i <= n; i++)
1683 {
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001684 lua_pushvalue(L, -1); // tostring
1685 lua_pushvalue(L, i); // arg
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001686 lua_call(L, 1, 1);
1687 s = lua_tolstring(L, -1, &l);
1688 if (s == NULL)
1689 return luaL_error(L, "cannot convert to string");
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001690 if (i > 1) luaL_addchar(&b, ' '); // use space instead of tab
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001691 luaV_addlstring(&b, s, l, 0);
1692 lua_pop(L, 1);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001693 }
1694 luaL_pushresult(&b);
Bram Moolenaarb98678a2019-10-19 15:18:44 +02001695 if (!got_int)
1696 luaV_msg(L);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001697 return 0;
1698}
1699
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001700 static int
Bram Moolenaar38e2b062011-09-21 17:15:39 +02001701luaV_debug(lua_State *L)
1702{
1703 lua_settop(L, 0);
1704 lua_getglobal(L, "vim");
1705 lua_getfield(L, -1, "eval");
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001706 lua_remove(L, -2); // vim.eval at position 1
Bram Moolenaar38e2b062011-09-21 17:15:39 +02001707 for (;;)
1708 {
1709 const char *input;
1710 size_t l;
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001711 lua_pushvalue(L, 1); // vim.eval
Bram Moolenaar38e2b062011-09-21 17:15:39 +02001712 lua_pushliteral(L, "input('lua_debug> ')");
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001713 lua_call(L, 1, 1); // return string
Bram Moolenaar38e2b062011-09-21 17:15:39 +02001714 input = lua_tolstring(L, -1, &l);
1715 if (l == 0 || strcmp(input, "cont") == 0)
1716 return 0;
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001717 msg_putchar('\n'); // avoid outputting on input line
Bram Moolenaar38e2b062011-09-21 17:15:39 +02001718 if (luaL_loadbuffer(L, input, l, "=(debug command)")
1719 || lua_pcall(L, 0, 0, 0))
1720 luaV_emsg(L);
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001721 lua_settop(L, 1); // remove eventual returns, but keep vim.eval
Bram Moolenaar38e2b062011-09-21 17:15:39 +02001722 }
1723}
1724
1725 static int
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001726luaV_command(lua_State *L)
1727{
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001728 do_cmdline_cmd((char_u *) luaL_checkstring(L, 1));
1729 update_screen(VALID);
1730 return 0;
1731}
1732
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001733 static int
1734luaV_eval(lua_State *L)
1735{
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001736 typval_T *tv = eval_expr((char_u *) luaL_checkstring(L, 1), NULL);
1737 if (tv == NULL) luaL_error(L, "invalid expression");
1738 luaV_pushtypval(L, tv);
Bram Moolenaarb3766472013-04-15 13:49:21 +02001739 free_tv(tv);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001740 return 1;
1741}
1742
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001743 static int
Bram Moolenaar0d2e4fc2010-07-18 12:35:47 +02001744luaV_beep(lua_State *L UNUSED)
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001745{
Bram Moolenaar165bc692015-07-21 17:53:25 +02001746 vim_beep(BO_LANG);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001747 return 0;
1748}
1749
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001750 static int
1751luaV_line(lua_State *L)
1752{
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001753 luaV_pushline(L, curbuf, curwin->w_cursor.lnum);
1754 return 1;
1755}
1756
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001757 static int
Bram Moolenaar1dced572012-04-05 16:54:08 +02001758luaV_list(lua_State *L)
1759{
Bram Moolenaarca06da92018-07-01 15:12:05 +02001760 list_T *l;
1761 int initarg = !lua_isnoneornil(L, 1);
1762
1763 if (initarg && lua_type(L, 1) != LUA_TTABLE)
1764 luaL_error(L, "table expected, got %s", luaL_typename(L, 1));
1765 l = list_alloc();
Bram Moolenaar1dced572012-04-05 16:54:08 +02001766 if (l == NULL)
1767 lua_pushnil(L);
1768 else
Bram Moolenaarca06da92018-07-01 15:12:05 +02001769 {
Bram Moolenaar1dced572012-04-05 16:54:08 +02001770 luaV_newlist(L, l);
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001771 if (initarg) // traverse table to init list
Bram Moolenaar17413672018-07-14 20:49:42 +02001772 {
Bram Moolenaarca06da92018-07-01 15:12:05 +02001773 int notnil, i = 0;
1774 typval_T v;
Bram Moolenaar17413672018-07-14 20:49:42 +02001775 do
1776 {
Bram Moolenaarca06da92018-07-01 15:12:05 +02001777 lua_rawgeti(L, 1, ++i);
1778 notnil = !lua_isnil(L, -1);
Bram Moolenaar17413672018-07-14 20:49:42 +02001779 if (notnil)
1780 {
Bram Moolenaarca06da92018-07-01 15:12:05 +02001781 luaV_checktypval(L, -1, &v, "vim.list");
1782 list_append_tv(l, &v);
Bram Moolenaar713bf9e92019-03-16 16:38:41 +01001783 clear_tv(&v);
Bram Moolenaarca06da92018-07-01 15:12:05 +02001784 }
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001785 lua_pop(L, 1); // value
Bram Moolenaarca06da92018-07-01 15:12:05 +02001786 } while (notnil);
1787 }
1788 }
Bram Moolenaar1dced572012-04-05 16:54:08 +02001789 return 1;
1790}
1791
1792 static int
1793luaV_dict(lua_State *L)
1794{
Bram Moolenaarca06da92018-07-01 15:12:05 +02001795 dict_T *d;
1796 int initarg = !lua_isnoneornil(L, 1);
1797
1798 if (initarg && lua_type(L, 1) != LUA_TTABLE)
1799 luaL_error(L, "table expected, got %s", luaL_typename(L, 1));
1800 d = dict_alloc();
Bram Moolenaar1dced572012-04-05 16:54:08 +02001801 if (d == NULL)
1802 lua_pushnil(L);
1803 else
Bram Moolenaarca06da92018-07-01 15:12:05 +02001804 {
Bram Moolenaar1dced572012-04-05 16:54:08 +02001805 luaV_newdict(L, d);
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001806 if (initarg) // traverse table to init dict
Bram Moolenaarca06da92018-07-01 15:12:05 +02001807 {
1808 lua_pushnil(L);
1809 while (lua_next(L, 1))
1810 {
1811 char_u *key;
1812 dictitem_T *di;
1813 typval_T v;
Bram Moolenaard6ef5f92018-07-13 22:08:23 +02001814
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001815 lua_pushvalue(L, -2); // dup key in case it's a number
Bram Moolenaarca06da92018-07-01 15:12:05 +02001816 key = (char_u *) lua_tostring(L, -1);
Bram Moolenaard6ef5f92018-07-13 22:08:23 +02001817 if (key == NULL)
1818 {
1819 lua_pushnil(L);
1820 return 1;
1821 }
1822 if (*key == NUL)
Bram Moolenaarca06da92018-07-01 15:12:05 +02001823 luaL_error(L, "table has empty key");
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001824 luaV_checktypval(L, -2, &v, "vim.dict"); // value
Bram Moolenaarca06da92018-07-01 15:12:05 +02001825 di = dictitem_alloc(key);
Bram Moolenaard6ef5f92018-07-13 22:08:23 +02001826 if (di == NULL || dict_add(d, di) == FAIL)
1827 {
Bram Moolenaarca06da92018-07-01 15:12:05 +02001828 vim_free(di);
1829 lua_pushnil(L);
1830 return 1;
1831 }
1832 copy_tv(&v, &di->di_tv);
1833 clear_tv(&v);
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001834 lua_pop(L, 2); // key copy and value
Bram Moolenaarca06da92018-07-01 15:12:05 +02001835 }
1836 }
1837 }
1838 return 1;
1839}
1840
1841 static int
Bram Moolenaarb7828692019-03-23 13:57:02 +01001842luaV_blob(lua_State *L)
1843{
1844 blob_T *b;
1845 int initarg = !lua_isnoneornil(L, 1);
1846
1847 if (initarg && !lua_isstring(L, 1))
1848 luaL_error(L, "string expected, got %s", luaL_typename(L, 1));
1849 b = blob_alloc();
1850 if (b == NULL)
1851 lua_pushnil(L);
1852 else
1853 {
1854 luaV_newblob(L, b);
1855 if (initarg)
1856 {
1857 size_t i, l = 0;
1858 const char *s = lua_tolstring(L, 1, &l);
1859
Bram Moolenaar5f1d3ae2020-02-11 22:37:35 +01001860 if (ga_grow(&b->bv_ga, (int)l) == OK)
Bram Moolenaar6fb5c972019-03-26 21:44:20 +01001861 for (i = 0; i < l; ++i)
1862 ga_append(&b->bv_ga, s[i]);
Bram Moolenaarb7828692019-03-23 13:57:02 +01001863 }
1864 }
1865 return 1;
1866}
1867
1868 static int
Bram Moolenaarca06da92018-07-01 15:12:05 +02001869luaV_funcref(lua_State *L)
1870{
1871 const char *name = luaL_checkstring(L, 1);
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001872 // note: not checking if function exists (needs function_exists)
Bram Moolenaarca06da92018-07-01 15:12:05 +02001873 if (name == NULL || *name == NUL || VIM_ISDIGIT(*name))
1874 luaL_error(L, "invalid function name: %s", name);
1875 luaV_newfuncref(L, (char_u *) name);
Bram Moolenaar1dced572012-04-05 16:54:08 +02001876 return 1;
1877}
1878
1879 static int
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001880luaV_buffer(lua_State *L)
1881{
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001882 buf_T *buf;
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001883 if (lua_isstring(L, 1)) // get by number or name?
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001884 {
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001885 if (lua_isnumber(L, 1)) // by number?
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001886 {
1887 int n = lua_tointeger(L, 1);
Bram Moolenaar29323592016-07-24 22:04:11 +02001888 FOR_ALL_BUFFERS(buf)
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001889 if (buf->b_fnum == n) break;
1890 }
Bram Moolenaarca06da92018-07-01 15:12:05 +02001891 else // by name
1892 {
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001893 size_t l;
1894 const char *s = lua_tolstring(L, 1, &l);
Bram Moolenaar29323592016-07-24 22:04:11 +02001895 FOR_ALL_BUFFERS(buf)
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001896 {
1897 if (buf->b_ffname == NULL || buf->b_sfname == NULL)
1898 {
1899 if (l == 0) break;
1900 }
Bram Moolenaar0d2e4fc2010-07-18 12:35:47 +02001901 else if (strncmp(s, (char *)buf->b_ffname, l) == 0
1902 || strncmp(s, (char *)buf->b_sfname, l) == 0)
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001903 break;
1904 }
1905 }
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001906 }
Bram Moolenaar1dced572012-04-05 16:54:08 +02001907 else
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001908 buf = (lua_toboolean(L, 1)) ? firstbuf : curbuf; // first buffer?
Bram Moolenaar1dced572012-04-05 16:54:08 +02001909 luaV_pushbuffer(L, buf);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001910 return 1;
1911}
1912
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001913 static int
1914luaV_window(lua_State *L)
1915{
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001916 win_T *win;
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001917 if (lua_isnumber(L, 1)) // get by number?
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001918 {
1919 int n = lua_tointeger(L, 1);
1920 for (win = firstwin; win != NULL; win = win->w_next, n--)
1921 if (n == 1) break;
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001922 }
Bram Moolenaar1dced572012-04-05 16:54:08 +02001923 else
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001924 win = (lua_toboolean(L, 1)) ? firstwin : curwin; // first window?
Bram Moolenaar1dced572012-04-05 16:54:08 +02001925 luaV_pushwindow(L, win);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001926 return 1;
1927}
1928
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001929 static int
1930luaV_open(lua_State *L)
1931{
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001932 char_u *s = NULL;
1933#ifdef HAVE_SANDBOX
1934 luaV_checksandbox(L);
1935#endif
1936 if (lua_isstring(L, 1)) s = (char_u *) lua_tostring(L, 1);
Bram Moolenaarfa263a52011-12-08 16:00:16 +01001937 luaV_pushbuffer(L, buflist_new(s, NULL, 1L, BLN_LISTED));
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001938 return 1;
1939}
1940
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001941 static int
Bram Moolenaar1dced572012-04-05 16:54:08 +02001942luaV_type(lua_State *L)
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001943{
Bram Moolenaar1dced572012-04-05 16:54:08 +02001944 luaL_checkany(L, 1);
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001945 if (lua_type(L, 1) == LUA_TUSERDATA) // check vim udata?
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001946 {
Bram Moolenaar1dced572012-04-05 16:54:08 +02001947 lua_settop(L, 1);
1948 if (lua_getmetatable(L, 1))
1949 {
1950 luaV_getfield(L, LUAVIM_LIST);
1951 if (lua_rawequal(L, -1, 2))
1952 {
1953 lua_pushstring(L, "list");
1954 return 1;
1955 }
1956 luaV_getfield(L, LUAVIM_DICT);
1957 if (lua_rawequal(L, -1, 2))
1958 {
1959 lua_pushstring(L, "dict");
1960 return 1;
1961 }
Bram Moolenaarb7828692019-03-23 13:57:02 +01001962 luaV_getfield(L, LUAVIM_BLOB);
1963 if (lua_rawequal(L, -1, 2))
1964 {
1965 lua_pushstring(L, "blob");
1966 return 1;
1967 }
Bram Moolenaarca06da92018-07-01 15:12:05 +02001968 luaV_getfield(L, LUAVIM_FUNCREF);
1969 if (lua_rawequal(L, -1, 2))
1970 {
1971 lua_pushstring(L, "funcref");
1972 return 1;
1973 }
Bram Moolenaar1dced572012-04-05 16:54:08 +02001974 luaV_getfield(L, LUAVIM_BUFFER);
1975 if (lua_rawequal(L, -1, 2))
1976 {
1977 lua_pushstring(L, "buffer");
1978 return 1;
1979 }
1980 luaV_getfield(L, LUAVIM_WINDOW);
1981 if (lua_rawequal(L, -1, 2))
1982 {
1983 lua_pushstring(L, "window");
1984 return 1;
1985 }
1986 }
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001987 }
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001988 lua_pushstring(L, luaL_typename(L, 1)); // fallback
Bram Moolenaar1dced572012-04-05 16:54:08 +02001989 return 1;
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001990}
1991
Bram Moolenaareb04f082020-05-17 14:32:35 +02001992 static int
1993luaV_call(lua_State *L)
1994{
1995 int argc = lua_gettop(L) - 1;
1996 size_t funcname_len;
1997 char_u *funcname;
1998 char *error = NULL;
1999 typval_T rettv;
2000 typval_T argv[MAX_FUNC_ARGS + 1];
2001 int i = 0;
2002
2003 if (argc > MAX_FUNC_ARGS)
2004 return luaL_error(L, "Function called with too many arguments");
2005
2006 funcname = (char_u *)luaL_checklstring(L, 1, &funcname_len);
2007
2008 for (; i < argc; i++)
2009 {
2010 if (luaV_totypval(L, i + 2, &argv[i]) == FAIL)
2011 {
2012 error = "lua: cannot convert value";
2013 goto free_vim_args;
2014 }
2015 }
2016
2017 argv[argc].v_type = VAR_UNKNOWN;
2018
2019 if (call_vim_function(funcname, argc, argv, &rettv) == FAIL)
2020 {
2021 error = "lua: call_vim_function failed";
2022 goto free_vim_args;
2023 }
2024
2025 luaV_pushtypval(L, &rettv);
2026 clear_tv(&rettv);
2027
2028free_vim_args:
2029 while (i > 0)
2030 clear_tv(&argv[--i]);
2031
2032 if (error == NULL)
2033 return 1;
2034 else
2035 return luaL_error(L, error);
2036}
2037
Bram Moolenaar0ba04292010-07-14 23:23:17 +02002038static const luaL_Reg luaV_module[] = {
2039 {"command", luaV_command},
2040 {"eval", luaV_eval},
2041 {"beep", luaV_beep},
2042 {"line", luaV_line},
Bram Moolenaar1dced572012-04-05 16:54:08 +02002043 {"list", luaV_list},
2044 {"dict", luaV_dict},
Bram Moolenaarb7828692019-03-23 13:57:02 +01002045 {"blob", luaV_blob},
Bram Moolenaarca06da92018-07-01 15:12:05 +02002046 {"funcref", luaV_funcref},
Bram Moolenaar0ba04292010-07-14 23:23:17 +02002047 {"buffer", luaV_buffer},
2048 {"window", luaV_window},
2049 {"open", luaV_open},
Bram Moolenaar1dced572012-04-05 16:54:08 +02002050 {"type", luaV_type},
Bram Moolenaareb04f082020-05-17 14:32:35 +02002051 {"call", luaV_call},
Bram Moolenaar0ba04292010-07-14 23:23:17 +02002052 {NULL, NULL}
2053};
2054
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002055/*
2056 * for freeing list, dict, buffer and window objects; lightuserdata as arg
2057 */
Bram Moolenaar1dced572012-04-05 16:54:08 +02002058 static int
2059luaV_free(lua_State *L)
2060{
2061 lua_pushnil(L);
2062 luaV_setudata(L, lua_touserdata(L, 1));
2063 return 0;
2064}
2065
2066 static int
Bram Moolenaar4eefe472019-03-19 21:59:19 +01002067luaV_luaeval(lua_State *L)
Bram Moolenaar1dced572012-04-05 16:54:08 +02002068{
2069 luaL_Buffer b;
2070 size_t l;
2071 const char *str = lua_tolstring(L, 1, &l);
2072 typval_T *arg = (typval_T *) lua_touserdata(L, 2);
2073 typval_T *rettv = (typval_T *) lua_touserdata(L, 3);
2074 luaL_buffinit(L, &b);
2075 luaL_addlstring(&b, LUAVIM_EVALHEADER, sizeof(LUAVIM_EVALHEADER) - 1);
2076 luaL_addlstring(&b, str, l);
2077 luaL_pushresult(&b);
2078 str = lua_tolstring(L, -1, &l);
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002079 if (luaL_loadbuffer(L, str, l, LUAVIM_EVALNAME)) // compile error?
Bram Moolenaar1dced572012-04-05 16:54:08 +02002080 {
2081 luaV_emsg(L);
2082 return 0;
2083 }
2084 luaV_pushtypval(L, arg);
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002085 if (lua_pcall(L, 1, 1, 0)) // running error?
Bram Moolenaar1dced572012-04-05 16:54:08 +02002086 {
2087 luaV_emsg(L);
2088 return 0;
2089 }
Bram Moolenaarca06da92018-07-01 15:12:05 +02002090 if (luaV_totypval(L, -1, rettv) == FAIL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002091 emsg("luaeval: cannot convert value");
Bram Moolenaarf554a322015-02-04 23:08:01 +01002092 return 0;
Bram Moolenaar1dced572012-04-05 16:54:08 +02002093}
2094
2095 static int
Bram Moolenaar4eefe472019-03-19 21:59:19 +01002096luaV_setref(lua_State *L)
Bram Moolenaar1dced572012-04-05 16:54:08 +02002097{
Bram Moolenaar4eefe472019-03-19 21:59:19 +01002098 int copyID = lua_tointeger(L, 1);
2099 int abort = FALSE;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01002100
Bram Moolenaar1dced572012-04-05 16:54:08 +02002101 luaV_getfield(L, LUAVIM_LIST);
2102 luaV_getfield(L, LUAVIM_DICT);
Bram Moolenaar4eefe472019-03-19 21:59:19 +01002103 luaV_getfield(L, LUAVIM_FUNCREF);
Bram Moolenaar1dced572012-04-05 16:54:08 +02002104 lua_pushnil(L);
Bram Moolenaar4eefe472019-03-19 21:59:19 +01002105 // traverse cache table
Bram Moolenaarb84634d2015-02-04 22:02:37 +01002106 while (!abort && lua_next(L, lua_upvalueindex(1)) != 0)
Bram Moolenaar1dced572012-04-05 16:54:08 +02002107 {
2108 lua_getmetatable(L, -1);
Bram Moolenaar4eefe472019-03-19 21:59:19 +01002109 if (lua_rawequal(L, -1, 2)) // list?
Bram Moolenaar1dced572012-04-05 16:54:08 +02002110 {
Bram Moolenaar4eefe472019-03-19 21:59:19 +01002111 list_T *l = (list_T *)lua_touserdata(L, 5); // key
2112
Bram Moolenaar7be3ab22019-06-23 01:46:15 +02002113 abort = set_ref_in_list(l, copyID);
Bram Moolenaar1dced572012-04-05 16:54:08 +02002114 }
Bram Moolenaar4eefe472019-03-19 21:59:19 +01002115 else if (lua_rawequal(L, -1, 3)) // dict?
Bram Moolenaar1dced572012-04-05 16:54:08 +02002116 {
Bram Moolenaar4eefe472019-03-19 21:59:19 +01002117 dict_T *d = (dict_T *)lua_touserdata(L, 5); // key
2118
Bram Moolenaar7be3ab22019-06-23 01:46:15 +02002119 abort = set_ref_in_dict(d, copyID);
Bram Moolenaar1dced572012-04-05 16:54:08 +02002120 }
Bram Moolenaar4eefe472019-03-19 21:59:19 +01002121 else if (lua_rawequal(L, -1, 4)) // funcref?
2122 {
2123 luaV_Funcref *f = (luaV_Funcref *)lua_touserdata(L, 5); // key
2124
Bram Moolenaar7be3ab22019-06-23 01:46:15 +02002125 abort = set_ref_in_dict(f->self, copyID);
Bram Moolenaar4eefe472019-03-19 21:59:19 +01002126 }
2127 lua_pop(L, 2); // metatable and value
Bram Moolenaar1dced572012-04-05 16:54:08 +02002128 }
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01002129 lua_pushinteger(L, abort);
Bram Moolenaarf554a322015-02-04 23:08:01 +01002130 return 1;
Bram Moolenaar1dced572012-04-05 16:54:08 +02002131}
2132
Bram Moolenaareb04f082020-05-17 14:32:35 +02002133#define LUA_VIM_FN_CODE \
Bram Moolenaar788fbb42020-05-31 14:08:12 +02002134 "vim.fn = setmetatable({}, {\n"\
2135 " __index = function (t, key)\n"\
2136 " local function _fn(...)\n"\
2137 " return vim.call(key, ...)\n"\
2138 " end\n"\
2139 " t[key] = _fn\n"\
2140 " return _fn\n"\
2141 " end\n"\
2142 " })"
2143
2144#define LUA_VIM_UPDATE_PACKAGE_PATHS \
2145 "local last_vim_paths = {}\n"\
2146 "vim._update_package_paths = function ()\n"\
2147 " local cur_vim_paths = {}\n"\
2148 " local function split(s, delimiter)\n"\
2149 " result = {}\n"\
2150 " for match in (s..delimiter):gmatch(\"(.-)\"..delimiter) do\n"\
2151 " table.insert(result, match)\n"\
2152 " end\n"\
2153 " return result\n"\
2154 " end\n"\
2155 " local rtps = split(vim.eval('&runtimepath'), ',')\n"\
2156 " local sep = package.config:sub(1, 1)\n"\
2157 " for _, key in ipairs({'path', 'cpath'}) do\n"\
2158 " local orig_str = package[key] .. ';'\n"\
2159 " local pathtrails_ordered = {}\n"\
2160 " -- Note: ignores trailing item without trailing `;`. Not using something\n"\
2161 " -- simpler in order to preserve empty items (stand for default path).\n"\
2162 " local orig = {}\n"\
2163 " for s in orig_str:gmatch('[^;]*;') do\n"\
2164 " s = s:sub(1, -2) -- Strip trailing semicolon\n"\
2165 " orig[#orig + 1] = s\n"\
2166 " end\n"\
2167 " if key == 'path' then\n"\
2168 " -- /?.lua and /?/init.lua\n"\
2169 " pathtrails_ordered = {sep .. '?.lua', sep .. '?' .. sep .. 'init.lua'}\n"\
2170 " else\n"\
2171 " local pathtrails = {}\n"\
2172 " for _, s in ipairs(orig) do\n"\
2173 " -- Find out path patterns. pathtrail should contain something like\n"\
2174 " -- /?.so, \?.dll. This allows not to bother determining what correct\n"\
2175 " -- suffixes are.\n"\
2176 " local pathtrail = s:match('[/\\\\][^/\\\\]*%?.*$')\n"\
2177 " if pathtrail and not pathtrails[pathtrail] then\n"\
2178 " pathtrails[pathtrail] = true\n"\
2179 " pathtrails_ordered[#pathtrails_ordered + 1] = pathtrail\n"\
2180 " end\n"\
2181 " end\n"\
2182 " end\n"\
2183 " local new = {}\n"\
2184 " for _, rtp in ipairs(rtps) do\n"\
2185 " if not rtp:match(';') then\n"\
2186 " for _, pathtrail in pairs(pathtrails_ordered) do\n"\
2187 " local new_path = rtp .. sep .. 'lua' .. pathtrail\n"\
2188 " -- Always keep paths from &runtimepath at the start:\n"\
2189 " -- append them here disregarding orig possibly containing one of them.\n"\
2190 " new[#new + 1] = new_path\n"\
2191 " cur_vim_paths[new_path] = true\n"\
2192 " end\n"\
2193 " end\n"\
2194 " end\n"\
2195 " for _, orig_path in ipairs(orig) do\n"\
2196 " -- Handle removing obsolete paths originating from &runtimepath: such\n"\
2197 " -- paths either belong to cur_nvim_paths and were already added above or\n"\
2198 " -- to last_nvim_paths and should not be added at all if corresponding\n"\
2199 " -- entry was removed from &runtimepath list.\n"\
2200 " if not (cur_vim_paths[orig_path] or last_vim_paths[orig_path]) then\n"\
2201 " new[#new + 1] = orig_path\n"\
2202 " end\n"\
2203 " end\n"\
2204 " package[key] = table.concat(new, ';')\n"\
2205 " end\n"\
2206 " last_vim_paths = cur_vim_paths\n"\
2207 "end"
Bram Moolenaareb04f082020-05-17 14:32:35 +02002208
Bram Moolenaar55d5c032010-07-17 23:52:29 +02002209 static int
2210luaopen_vim(lua_State *L)
2211{
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002212 // set cache table
Bram Moolenaar0ba04292010-07-14 23:23:17 +02002213 lua_newtable(L);
2214 lua_newtable(L);
Bram Moolenaar1dced572012-04-05 16:54:08 +02002215 lua_pushstring(L, "v");
Bram Moolenaar0ba04292010-07-14 23:23:17 +02002216 lua_setfield(L, -2, "__mode");
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002217 lua_setmetatable(L, -2); // cache is weak-valued
2218 // print
Bram Moolenaar0ba04292010-07-14 23:23:17 +02002219 lua_pushcfunction(L, luaV_print);
2220 lua_setglobal(L, "print");
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002221 // debug.debug
Bram Moolenaar38e2b062011-09-21 17:15:39 +02002222 lua_getglobal(L, "debug");
2223 lua_pushcfunction(L, luaV_debug);
2224 lua_setfield(L, -2, "debug");
2225 lua_pop(L, 1);
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002226 // free
Bram Moolenaar0ba04292010-07-14 23:23:17 +02002227 lua_pushlightuserdata(L, (void *) LUAVIM_FREE);
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002228 lua_pushvalue(L, 1); // cache table
Bram Moolenaar1dced572012-04-05 16:54:08 +02002229 lua_pushcclosure(L, luaV_free, 1);
2230 lua_rawset(L, LUA_REGISTRYINDEX);
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002231 // luaeval
Bram Moolenaar1dced572012-04-05 16:54:08 +02002232 lua_pushlightuserdata(L, (void *) LUAVIM_LUAEVAL);
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002233 lua_pushvalue(L, 1); // cache table
Bram Moolenaar1dced572012-04-05 16:54:08 +02002234 lua_pushcclosure(L, luaV_luaeval, 1);
2235 lua_rawset(L, LUA_REGISTRYINDEX);
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002236 // setref
Bram Moolenaar1dced572012-04-05 16:54:08 +02002237 lua_pushlightuserdata(L, (void *) LUAVIM_SETREF);
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002238 lua_pushvalue(L, 1); // cache table
Bram Moolenaar1dced572012-04-05 16:54:08 +02002239 lua_pushcclosure(L, luaV_setref, 1);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02002240 lua_rawset(L, LUA_REGISTRYINDEX);
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002241 // register
Bram Moolenaar1dced572012-04-05 16:54:08 +02002242 luaV_newmetatable(L, LUAVIM_LIST);
2243 lua_pushvalue(L, 1);
2244 luaV_openlib(L, luaV_List_mt, 1);
2245 luaV_newmetatable(L, LUAVIM_DICT);
2246 lua_pushvalue(L, 1);
2247 luaV_openlib(L, luaV_Dict_mt, 1);
Bram Moolenaarb7828692019-03-23 13:57:02 +01002248 luaV_newmetatable(L, LUAVIM_BLOB);
2249 lua_pushvalue(L, 1);
2250 luaV_openlib(L, luaV_Blob_mt, 1);
Bram Moolenaarca06da92018-07-01 15:12:05 +02002251 luaV_newmetatable(L, LUAVIM_FUNCREF);
2252 lua_pushvalue(L, 1);
2253 luaV_openlib(L, luaV_Funcref_mt, 1);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02002254 luaV_newmetatable(L, LUAVIM_BUFFER);
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002255 lua_pushvalue(L, 1); // cache table
Bram Moolenaar1dced572012-04-05 16:54:08 +02002256 luaV_openlib(L, luaV_Buffer_mt, 1);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02002257 luaV_newmetatable(L, LUAVIM_WINDOW);
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002258 lua_pushvalue(L, 1); // cache table
Bram Moolenaar1dced572012-04-05 16:54:08 +02002259 luaV_openlib(L, luaV_Window_mt, 1);
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002260 lua_newtable(L); // vim table
2261 lua_pushvalue(L, 1); // cache table
Bram Moolenaar1dced572012-04-05 16:54:08 +02002262 luaV_openlib(L, luaV_module, 1);
2263 lua_setglobal(L, LUAVIM_NAME);
Bram Moolenaareb04f082020-05-17 14:32:35 +02002264 // custom code
Bram Moolenaar9309eb22020-05-17 16:53:56 +02002265 (void)luaL_dostring(L, LUA_VIM_FN_CODE);
Bram Moolenaar788fbb42020-05-31 14:08:12 +02002266 (void)luaL_dostring(L, LUA_VIM_UPDATE_PACKAGE_PATHS);
2267
2268 lua_getglobal(L, "vim");
2269 lua_getfield(L, -1, "_update_package_paths");
2270
2271 if (lua_pcall(L, 0, 0, 0))
2272 luaV_emsg(L);
2273
Bram Moolenaar0ba04292010-07-14 23:23:17 +02002274 return 0;
2275}
2276
Bram Moolenaar55d5c032010-07-17 23:52:29 +02002277 static lua_State *
2278luaV_newstate(void)
2279{
Bram Moolenaar0ba04292010-07-14 23:23:17 +02002280 lua_State *L = luaL_newstate();
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002281 luaL_openlibs(L); // core libs
2282 lua_pushcfunction(L, luaopen_vim); // vim
Bram Moolenaar0ba04292010-07-14 23:23:17 +02002283 lua_call(L, 0, 0);
2284 return L;
2285}
2286
Bram Moolenaar55d5c032010-07-17 23:52:29 +02002287 static void
2288luaV_setrange(lua_State *L, int line1, int line2)
2289{
Bram Moolenaar0ba04292010-07-14 23:23:17 +02002290 lua_getglobal(L, LUAVIM_NAME);
2291 lua_pushinteger(L, line1);
2292 lua_setfield(L, -2, "firstline");
2293 lua_pushinteger(L, line2);
2294 lua_setfield(L, -2, "lastline");
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002295 lua_pop(L, 1); // vim table
Bram Moolenaar0ba04292010-07-14 23:23:17 +02002296}
2297
2298
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002299// ======= Interface =======
Bram Moolenaar0ba04292010-07-14 23:23:17 +02002300
2301static lua_State *L = NULL;
2302
Bram Moolenaar55d5c032010-07-17 23:52:29 +02002303 static int
Bram Moolenaar1dced572012-04-05 16:54:08 +02002304lua_isopen(void)
Bram Moolenaar2bd6a1b2010-08-12 22:14:01 +02002305{
2306 return L != NULL;
2307}
2308
2309 static int
Bram Moolenaar55d5c032010-07-17 23:52:29 +02002310lua_init(void)
2311{
Bram Moolenaar1dced572012-04-05 16:54:08 +02002312 if (!lua_isopen())
Bram Moolenaar55d5c032010-07-17 23:52:29 +02002313 {
Bram Moolenaar0ba04292010-07-14 23:23:17 +02002314#ifdef DYNAMIC_LUA
Bram Moolenaar55d5c032010-07-17 23:52:29 +02002315 if (!lua_enabled(TRUE))
2316 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002317 emsg(_("Lua library cannot be loaded."));
Bram Moolenaar55d5c032010-07-17 23:52:29 +02002318 return FAIL;
2319 }
Bram Moolenaar0ba04292010-07-14 23:23:17 +02002320#endif
Bram Moolenaar55d5c032010-07-17 23:52:29 +02002321 L = luaV_newstate();
Bram Moolenaar0ba04292010-07-14 23:23:17 +02002322 }
2323 return OK;
2324}
2325
Bram Moolenaar55d5c032010-07-17 23:52:29 +02002326 void
2327lua_end(void)
2328{
Bram Moolenaar1dced572012-04-05 16:54:08 +02002329 if (lua_isopen())
Bram Moolenaar55d5c032010-07-17 23:52:29 +02002330 {
2331 lua_close(L);
2332 L = NULL;
Bram Moolenaar0ba04292010-07-14 23:23:17 +02002333 }
2334}
2335
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002336/*
2337 * ex commands
2338 */
Bram Moolenaar55d5c032010-07-17 23:52:29 +02002339 void
2340ex_lua(exarg_T *eap)
2341{
Bram Moolenaar0ba04292010-07-14 23:23:17 +02002342 char *script;
2343 if (lua_init() == FAIL) return;
2344 script = (char *) script_get(eap, eap->arg);
Bram Moolenaar55d5c032010-07-17 23:52:29 +02002345 if (!eap->skip)
2346 {
2347 char *s = (script) ? script : (char *) eap->arg;
2348 luaV_setrange(L, eap->line1, eap->line2);
2349 if (luaL_loadbuffer(L, s, strlen(s), LUAVIM_CHUNKNAME)
2350 || lua_pcall(L, 0, 0, 0))
2351 luaV_emsg(L);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02002352 }
2353 if (script != NULL) vim_free(script);
2354}
2355
Bram Moolenaar55d5c032010-07-17 23:52:29 +02002356 void
2357ex_luado(exarg_T *eap)
2358{
Bram Moolenaar0ba04292010-07-14 23:23:17 +02002359 linenr_T l;
2360 const char *s = (const char *) eap->arg;
2361 luaL_Buffer b;
2362 size_t len;
Bram Moolenaard58f03b2017-01-29 22:48:45 +01002363 buf_T *was_curbuf = curbuf;
2364
Bram Moolenaar0ba04292010-07-14 23:23:17 +02002365 if (lua_init() == FAIL) return;
Bram Moolenaar55d5c032010-07-17 23:52:29 +02002366 if (u_save(eap->line1 - 1, eap->line2 + 1) == FAIL)
2367 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002368 emsg(_("cannot save undo information"));
Bram Moolenaar55d5c032010-07-17 23:52:29 +02002369 return;
Bram Moolenaar0ba04292010-07-14 23:23:17 +02002370 }
2371 luaV_setrange(L, eap->line1, eap->line2);
2372 luaL_buffinit(L, &b);
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002373 luaL_addlstring(&b, "return function(line, linenr) ", 30); // header
Bram Moolenaar0ba04292010-07-14 23:23:17 +02002374 luaL_addlstring(&b, s, strlen(s));
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002375 luaL_addlstring(&b, " end", 4); // footer
Bram Moolenaar0ba04292010-07-14 23:23:17 +02002376 luaL_pushresult(&b);
2377 s = lua_tolstring(L, -1, &len);
Bram Moolenaar55d5c032010-07-17 23:52:29 +02002378 if (luaL_loadbuffer(L, s, len, LUAVIM_CHUNKNAME))
2379 {
2380 luaV_emsg(L);
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002381 lua_pop(L, 1); // function body
Bram Moolenaar55d5c032010-07-17 23:52:29 +02002382 return;
Bram Moolenaar0ba04292010-07-14 23:23:17 +02002383 }
2384 lua_call(L, 0, 1);
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002385 lua_replace(L, -2); // function -> body
Bram Moolenaar55d5c032010-07-17 23:52:29 +02002386 for (l = eap->line1; l <= eap->line2; l++)
2387 {
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002388 // Check the line number, the command my have deleted lines.
Bram Moolenaard58f03b2017-01-29 22:48:45 +01002389 if (l > curbuf->b_ml.ml_line_count)
2390 break;
2391
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002392 lua_pushvalue(L, -1); // function
2393 luaV_pushline(L, curbuf, l); // current line as arg
2394 lua_pushinteger(L, l); // current line number as arg
Bram Moolenaarbd2f3c32012-04-06 14:31:00 +02002395 if (lua_pcall(L, 2, 1, 0))
Bram Moolenaar55d5c032010-07-17 23:52:29 +02002396 {
2397 luaV_emsg(L);
2398 break;
2399 }
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002400 // Catch the command switching to another buffer.
Bram Moolenaard58f03b2017-01-29 22:48:45 +01002401 if (curbuf != was_curbuf)
2402 break;
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002403 if (lua_isstring(L, -1)) // update line?
Bram Moolenaar55d5c032010-07-17 23:52:29 +02002404 {
Bram Moolenaar0ba04292010-07-14 23:23:17 +02002405#ifdef HAVE_SANDBOX
Bram Moolenaar55d5c032010-07-17 23:52:29 +02002406 luaV_checksandbox(L);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02002407#endif
Bram Moolenaar55d5c032010-07-17 23:52:29 +02002408 ml_replace(l, luaV_toline(L, -1), TRUE);
2409 changed_bytes(l, 0);
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002410 lua_pop(L, 1); // result from luaV_toline
Bram Moolenaar55d5c032010-07-17 23:52:29 +02002411 }
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002412 lua_pop(L, 1); // line
Bram Moolenaar0ba04292010-07-14 23:23:17 +02002413 }
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002414 lua_pop(L, 1); // function
Bram Moolenaar0ba04292010-07-14 23:23:17 +02002415 check_cursor();
2416 update_screen(NOT_VALID);
2417}
2418
Bram Moolenaar55d5c032010-07-17 23:52:29 +02002419 void
2420ex_luafile(exarg_T *eap)
2421{
2422 if (lua_init() == FAIL)
2423 return;
2424 if (!eap->skip)
2425 {
2426 luaV_setrange(L, eap->line1, eap->line2);
2427 if (luaL_loadfile(L, (char *) eap->arg) || lua_pcall(L, 0, 0, 0))
2428 luaV_emsg(L);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02002429 }
2430}
2431
Bram Moolenaar1dced572012-04-05 16:54:08 +02002432#define luaV_freetype(typ,tname) \
2433 void \
2434 lua_##tname##_free(typ *o) \
2435 { \
2436 if (!lua_isopen()) return; \
2437 luaV_getfield(L, LUAVIM_FREE); \
2438 lua_pushlightuserdata(L, (void *) o); \
2439 lua_call(L, 1, 0); \
2440 }
2441
2442luaV_freetype(buf_T, buffer)
2443luaV_freetype(win_T, window)
2444
Bram Moolenaar55d5c032010-07-17 23:52:29 +02002445 void
Bram Moolenaar4eefe472019-03-19 21:59:19 +01002446do_luaeval(char_u *str, typval_T *arg, typval_T *rettv)
Bram Moolenaar55d5c032010-07-17 23:52:29 +02002447{
Bram Moolenaar1dced572012-04-05 16:54:08 +02002448 lua_init();
2449 luaV_getfield(L, LUAVIM_LUAEVAL);
2450 lua_pushstring(L, (char *) str);
2451 lua_pushlightuserdata(L, (void *) arg);
2452 lua_pushlightuserdata(L, (void *) rettv);
2453 lua_call(L, 3, 0);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02002454}
2455
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01002456 int
Bram Moolenaar4eefe472019-03-19 21:59:19 +01002457set_ref_in_lua(int copyID)
Bram Moolenaar55d5c032010-07-17 23:52:29 +02002458{
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01002459 int aborted = 0;
2460
2461 if (lua_isopen())
2462 {
2463 luaV_getfield(L, LUAVIM_SETREF);
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002464 // call the function with 1 arg, getting 1 result back
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01002465 lua_pushinteger(L, copyID);
2466 lua_call(L, 1, 1);
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002467 // get the result
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01002468 aborted = lua_tointeger(L, -1);
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002469 // pop result off the stack
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01002470 lua_pop(L, 1);
2471 }
2472 return aborted;
Bram Moolenaar0ba04292010-07-14 23:23:17 +02002473}
2474
Bram Moolenaar788fbb42020-05-31 14:08:12 +02002475 void
2476update_package_paths_in_lua()
2477{
2478 if (lua_isopen())
2479 {
2480 lua_getglobal(L, "vim");
2481 lua_getfield(L, -1, "_update_package_paths");
2482
2483 if (lua_pcall(L, 0, 0, 0))
2484 luaV_emsg(L);
2485 }
2486}
2487
Bram Moolenaar801ab062020-06-25 19:27:56 +02002488/*
2489 * Native C function callback
2490 */
2491 static int
2492luaV_call_lua_func(
2493 int argcount,
2494 typval_T *argvars,
2495 typval_T *rettv,
2496 void *state)
2497{
2498 int i;
2499 int luaargcount = argcount;
2500 luaV_CFuncState *funcstate = (luaV_CFuncState*)state;
2501 lua_rawgeti(funcstate->L, LUA_REGISTRYINDEX, funcstate->lua_funcref);
2502
2503 if (funcstate->lua_tableref != LUA_NOREF)
2504 {
2505 // First arg for metatable __call method is a table
2506 luaargcount += 1;
2507 lua_rawgeti(funcstate->L, LUA_REGISTRYINDEX, funcstate->lua_tableref);
2508 }
2509
2510 for (i = 0; i < argcount; ++i)
2511 luaV_pushtypval(funcstate->L, &argvars[i]);
2512
2513 if (lua_pcall(funcstate->L, luaargcount, 1, 0))
2514 {
2515 luaV_emsg(funcstate->L);
2516 return FCERR_OTHER;
2517 }
2518
2519 luaV_checktypval(funcstate->L, -1, rettv, "get return value");
2520 return FCERR_NONE;
2521}
2522
2523/*
2524 * Free up any lua references held by the func state.
2525 */
2526 static void
2527luaV_call_lua_func_free(void *state)
2528{
2529 luaV_CFuncState *funcstate = (luaV_CFuncState*)state;
2530 luaL_unref(L, LUA_REGISTRYINDEX, funcstate->lua_funcref);
2531 funcstate->L = NULL;
2532 if (funcstate->lua_tableref != LUA_NOREF)
2533 luaL_unref(L, LUA_REGISTRYINDEX, funcstate->lua_tableref);
2534 VIM_CLEAR(funcstate);
2535}
2536
Bram Moolenaar0ba04292010-07-14 23:23:17 +02002537#endif