blob: 78bc1909503e81db46dd84d104566f6220661906 [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"
Yegappan Lakshmanan11328bc2021-08-06 21:34:38 +020013#include "version.h"
Bram Moolenaare2793352011-01-17 19:53:27 +010014
Bram Moolenaar0ba04292010-07-14 23:23:17 +020015#include <lua.h>
16#include <lualib.h>
17#include <lauxlib.h>
Bram Moolenaar0ba04292010-07-14 23:23:17 +020018
Bram Moolenaar2ab2e862019-12-04 21:24:53 +010019// Only do the following when the feature is enabled. Needed for "make
20// depend".
Bram Moolenaar0ba04292010-07-14 23:23:17 +020021#if defined(FEAT_LUA) || defined(PROTO)
22
23#define LUAVIM_CHUNKNAME "vim chunk"
24#define LUAVIM_NAME "vim"
Bram Moolenaar1dced572012-04-05 16:54:08 +020025#define LUAVIM_EVALNAME "luaeval"
26#define LUAVIM_EVALHEADER "local _A=select(1,...) return "
Bram Moolenaar0ba04292010-07-14 23:23:17 +020027
Bram Moolenaar125ed272021-04-07 20:11:12 +020028#ifdef LUA_RELEASE
29# define LUAVIM_VERSION LUA_RELEASE
30#else
31# define LUAVIM_VERSION LUA_VERSION
32#endif
33
Bram Moolenaar0ba04292010-07-14 23:23:17 +020034typedef buf_T *luaV_Buffer;
35typedef win_T *luaV_Window;
Bram Moolenaar1dced572012-04-05 16:54:08 +020036typedef dict_T *luaV_Dict;
37typedef list_T *luaV_List;
Bram Moolenaarb7828692019-03-23 13:57:02 +010038typedef blob_T *luaV_Blob;
Bram Moolenaarca06da92018-07-01 15:12:05 +020039typedef struct {
Bram Moolenaar4eefe472019-03-19 21:59:19 +010040 char_u *name; // funcref
Bram Moolenaarca06da92018-07-01 15:12:05 +020041 dict_T *self; // selfdict
42} luaV_Funcref;
Bram Moolenaarc8970b92020-10-26 20:18:08 +010043typedef int (*msgfunc_T)(char *);
Bram Moolenaar0ba04292010-07-14 23:23:17 +020044
Bram Moolenaar801ab062020-06-25 19:27:56 +020045typedef struct {
46 int lua_funcref; // ref to a lua func
47 int lua_tableref; // ref to a lua table if metatable else LUA_NOREF. used
48 // for __call
49 lua_State *L;
50} luaV_CFuncState;
51
Bram Moolenaar1dced572012-04-05 16:54:08 +020052static const char LUAVIM_DICT[] = "dict";
53static const char LUAVIM_LIST[] = "list";
Bram Moolenaarb7828692019-03-23 13:57:02 +010054static const char LUAVIM_BLOB[] = "blob";
Bram Moolenaarca06da92018-07-01 15:12:05 +020055static const char LUAVIM_FUNCREF[] = "funcref";
Bram Moolenaar0ba04292010-07-14 23:23:17 +020056static const char LUAVIM_BUFFER[] = "buffer";
57static const char LUAVIM_WINDOW[] = "window";
58static const char LUAVIM_FREE[] = "luaV_free";
Bram Moolenaar1dced572012-04-05 16:54:08 +020059static const char LUAVIM_LUAEVAL[] = "luaV_luaeval";
60static const char LUAVIM_SETREF[] = "luaV_setref";
Bram Moolenaar0ba04292010-07-14 23:23:17 +020061
Bram Moolenaar801ab062020-06-25 19:27:56 +020062static const char LUA___CALL[] = "__call";
63
Bram Moolenaar2ab2e862019-12-04 21:24:53 +010064// most functions are closures with a cache table as first upvalue;
65// get/setudata manage references to vim userdata in cache table through
66// object pointers (light userdata)
Bram Moolenaar1dced572012-04-05 16:54:08 +020067#define luaV_getudata(L, v) \
68 lua_pushlightuserdata((L), (void *) (v)); \
69 lua_rawget((L), lua_upvalueindex(1))
70#define luaV_setudata(L, v) \
71 lua_pushlightuserdata((L), (void *) (v)); \
72 lua_pushvalue((L), -2); \
73 lua_rawset((L), lua_upvalueindex(1))
Bram Moolenaar0ba04292010-07-14 23:23:17 +020074#define luaV_getfield(L, s) \
75 lua_pushlightuserdata((L), (void *)(s)); \
76 lua_rawget((L), LUA_REGISTRYINDEX)
77#define luaV_checksandbox(L) \
78 if (sandbox) luaL_error((L), "not allowed in sandbox")
79#define luaV_msg(L) luaV_msgfunc((L), (msgfunc_T) msg)
80#define luaV_emsg(L) luaV_msgfunc((L), (msgfunc_T) emsg)
Bram Moolenaarca06da92018-07-01 15:12:05 +020081#define luaV_checktypval(L, a, v, msg) \
82 do { \
Bram Moolenaar801ab062020-06-25 19:27:56 +020083 if (luaV_totypval(L, a, v) == FAIL) \
Bram Moolenaarca06da92018-07-01 15:12:05 +020084 luaL_error(L, msg ": cannot convert value"); \
85 } while (0)
Bram Moolenaar0ba04292010-07-14 23:23:17 +020086
Bram Moolenaarca06da92018-07-01 15:12:05 +020087static luaV_List *luaV_pushlist(lua_State *L, list_T *lis);
88static luaV_Dict *luaV_pushdict(lua_State *L, dict_T *dic);
Bram Moolenaarb7828692019-03-23 13:57:02 +010089static luaV_Blob *luaV_pushblob(lua_State *L, blob_T *blo);
Bram Moolenaar4eefe472019-03-19 21:59:19 +010090static luaV_Funcref *luaV_pushfuncref(lua_State *L, char_u *name);
Bram Moolenaar801ab062020-06-25 19:27:56 +020091static int luaV_call_lua_func(int argcount, typval_T *argvars, typval_T *rettv, void *state);
92static void luaV_call_lua_func_free(void *state);
Bram Moolenaar1dced572012-04-05 16:54:08 +020093
94#if LUA_VERSION_NUM <= 501
95#define luaV_openlib(L, l, n) luaL_openlib(L, NULL, l, n)
96#define luaL_typeerror luaL_typerror
97#else
98#define luaV_openlib luaL_setfuncs
99#endif
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200100
101#ifdef DYNAMIC_LUA
Bram Moolenaar2334b6d2010-07-22 21:32:16 +0200102
Bram Moolenaar4f974752019-02-17 17:44:42 +0100103#ifndef MSWIN
Bram Moolenaar2334b6d2010-07-22 21:32:16 +0200104# include <dlfcn.h>
105# define HANDLE void*
106# define load_dll(n) dlopen((n), RTLD_LAZY|RTLD_GLOBAL)
107# define symbol_from_dll dlsym
108# define close_dll dlclose
Martin Tournoij1a3e5742021-07-24 13:57:29 +0200109# define load_dll_error dlerror
Bram Moolenaar2334b6d2010-07-22 21:32:16 +0200110#else
Bram Moolenaarebbcb822010-10-23 14:02:54 +0200111# define load_dll vimLoadLib
Bram Moolenaar2334b6d2010-07-22 21:32:16 +0200112# define symbol_from_dll GetProcAddress
113# define close_dll FreeLibrary
Martin Tournoij1a3e5742021-07-24 13:57:29 +0200114# define load_dll_error GetWin32Error
Bram Moolenaar2334b6d2010-07-22 21:32:16 +0200115#endif
116
Bram Moolenaar2ab2e862019-12-04 21:24:53 +0100117// lauxlib
Bram Moolenaar1dced572012-04-05 16:54:08 +0200118#if LUA_VERSION_NUM <= 501
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200119#define luaL_register dll_luaL_register
Bram Moolenaar1dced572012-04-05 16:54:08 +0200120#define luaL_prepbuffer dll_luaL_prepbuffer
121#define luaL_openlib dll_luaL_openlib
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200122#define luaL_typerror dll_luaL_typerror
Bram Moolenaar1dced572012-04-05 16:54:08 +0200123#define luaL_loadfile dll_luaL_loadfile
124#define luaL_loadbuffer dll_luaL_loadbuffer
125#else
126#define luaL_prepbuffsize dll_luaL_prepbuffsize
127#define luaL_setfuncs dll_luaL_setfuncs
128#define luaL_loadfilex dll_luaL_loadfilex
129#define luaL_loadbufferx dll_luaL_loadbufferx
130#define luaL_argerror dll_luaL_argerror
131#endif
Bram Moolenaar5551b132020-07-14 21:54:28 +0200132#if LUA_VERSION_NUM >= 504
133#define luaL_typeerror dll_luaL_typeerror
134#endif
Bram Moolenaarbd2f3c32012-04-06 14:31:00 +0200135#define luaL_checkany dll_luaL_checkany
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200136#define luaL_checklstring dll_luaL_checklstring
137#define luaL_checkinteger dll_luaL_checkinteger
138#define luaL_optinteger dll_luaL_optinteger
139#define luaL_checktype dll_luaL_checktype
140#define luaL_error dll_luaL_error
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200141#define luaL_newstate dll_luaL_newstate
142#define luaL_buffinit dll_luaL_buffinit
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200143#define luaL_addlstring dll_luaL_addlstring
144#define luaL_pushresult dll_luaL_pushresult
Bram Moolenaardf1643a2020-05-17 18:53:27 +0200145#define luaL_loadstring dll_luaL_loadstring
Bram Moolenaar1e4c7d02020-06-25 20:56:42 +0200146#define luaL_ref dll_luaL_ref
147#define luaL_unref dll_luaL_unref
Bram Moolenaar2ab2e862019-12-04 21:24:53 +0100148// lua
Bram Moolenaar1dced572012-04-05 16:54:08 +0200149#if LUA_VERSION_NUM <= 501
150#define lua_tonumber dll_lua_tonumber
151#define lua_tointeger dll_lua_tointeger
152#define lua_call dll_lua_call
153#define lua_pcall dll_lua_pcall
154#else
155#define lua_tonumberx dll_lua_tonumberx
156#define lua_tointegerx dll_lua_tointegerx
157#define lua_callk dll_lua_callk
158#define lua_pcallk dll_lua_pcallk
159#define lua_getglobal dll_lua_getglobal
160#define lua_setglobal dll_lua_setglobal
Bram Moolenaar1dced572012-04-05 16:54:08 +0200161#endif
Bram Moolenaar1f860d82015-06-27 18:36:16 +0200162#if LUA_VERSION_NUM <= 502
163#define lua_replace dll_lua_replace
164#define lua_remove dll_lua_remove
165#endif
166#if LUA_VERSION_NUM >= 503
167#define lua_rotate dll_lua_rotate
168#define lua_copy dll_lua_copy
169#endif
Bram Moolenaarbd2f3c32012-04-06 14:31:00 +0200170#define lua_typename dll_lua_typename
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200171#define lua_close dll_lua_close
172#define lua_gettop dll_lua_gettop
173#define lua_settop dll_lua_settop
174#define lua_pushvalue dll_lua_pushvalue
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200175#define lua_isnumber dll_lua_isnumber
176#define lua_isstring dll_lua_isstring
177#define lua_type dll_lua_type
178#define lua_rawequal dll_lua_rawequal
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200179#define lua_toboolean dll_lua_toboolean
180#define lua_tolstring dll_lua_tolstring
181#define lua_touserdata dll_lua_touserdata
182#define lua_pushnil dll_lua_pushnil
183#define lua_pushnumber dll_lua_pushnumber
184#define lua_pushinteger dll_lua_pushinteger
185#define lua_pushlstring dll_lua_pushlstring
186#define lua_pushstring dll_lua_pushstring
187#define lua_pushfstring dll_lua_pushfstring
188#define lua_pushcclosure dll_lua_pushcclosure
189#define lua_pushboolean dll_lua_pushboolean
190#define lua_pushlightuserdata dll_lua_pushlightuserdata
191#define lua_getfield dll_lua_getfield
192#define lua_rawget dll_lua_rawget
Bram Moolenaar1dced572012-04-05 16:54:08 +0200193#define lua_rawgeti dll_lua_rawgeti
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200194#define lua_createtable dll_lua_createtable
Yegappan Lakshmanan11328bc2021-08-06 21:34:38 +0200195#define lua_settable dll_lua_settable
Bram Moolenaar830e3582018-08-21 14:23:35 +0200196#if LUA_VERSION_NUM >= 504
197 #define lua_newuserdatauv dll_lua_newuserdatauv
198#else
199 #define lua_newuserdata dll_lua_newuserdata
200#endif
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200201#define lua_getmetatable dll_lua_getmetatable
202#define lua_setfield dll_lua_setfield
203#define lua_rawset dll_lua_rawset
204#define lua_rawseti dll_lua_rawseti
205#define lua_setmetatable dll_lua_setmetatable
Bram Moolenaarbd2f3c32012-04-06 14:31:00 +0200206#define lua_next dll_lua_next
Bram Moolenaar2ab2e862019-12-04 21:24:53 +0100207// libs
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200208#define luaopen_base dll_luaopen_base
209#define luaopen_table dll_luaopen_table
210#define luaopen_string dll_luaopen_string
211#define luaopen_math dll_luaopen_math
Bram Moolenaar16c98f92010-07-28 22:46:08 +0200212#define luaopen_io dll_luaopen_io
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200213#define luaopen_os dll_luaopen_os
214#define luaopen_package dll_luaopen_package
215#define luaopen_debug dll_luaopen_debug
Bram Moolenaar16c98f92010-07-28 22:46:08 +0200216#define luaL_openlibs dll_luaL_openlibs
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200217
Bram Moolenaar2ab2e862019-12-04 21:24:53 +0100218// lauxlib
Bram Moolenaar1dced572012-04-05 16:54:08 +0200219#if LUA_VERSION_NUM <= 501
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200220void (*dll_luaL_register) (lua_State *L, const char *libname, const luaL_Reg *l);
Bram Moolenaar1dced572012-04-05 16:54:08 +0200221char *(*dll_luaL_prepbuffer) (luaL_Buffer *B);
222void (*dll_luaL_openlib) (lua_State *L, const char *libname, const luaL_Reg *l, int nup);
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200223int (*dll_luaL_typerror) (lua_State *L, int narg, const char *tname);
Bram Moolenaar1dced572012-04-05 16:54:08 +0200224int (*dll_luaL_loadfile) (lua_State *L, const char *filename);
225int (*dll_luaL_loadbuffer) (lua_State *L, const char *buff, size_t sz, const char *name);
226#else
227char *(*dll_luaL_prepbuffsize) (luaL_Buffer *B, size_t sz);
228void (*dll_luaL_setfuncs) (lua_State *L, const luaL_Reg *l, int nup);
229int (*dll_luaL_loadfilex) (lua_State *L, const char *filename, const char *mode);
230int (*dll_luaL_loadbufferx) (lua_State *L, const char *buff, size_t sz, const char *name, const char *mode);
231int (*dll_luaL_argerror) (lua_State *L, int numarg, const char *extramsg);
232#endif
Bram Moolenaar5551b132020-07-14 21:54:28 +0200233#if LUA_VERSION_NUM >= 504
234int (*dll_luaL_typeerror) (lua_State *L, int narg, const char *tname);
235#endif
Bram Moolenaarbd2f3c32012-04-06 14:31:00 +0200236void (*dll_luaL_checkany) (lua_State *L, int narg);
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200237const char *(*dll_luaL_checklstring) (lua_State *L, int numArg, size_t *l);
238lua_Integer (*dll_luaL_checkinteger) (lua_State *L, int numArg);
239lua_Integer (*dll_luaL_optinteger) (lua_State *L, int nArg, lua_Integer def);
240void (*dll_luaL_checktype) (lua_State *L, int narg, int t);
241int (*dll_luaL_error) (lua_State *L, const char *fmt, ...);
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200242lua_State *(*dll_luaL_newstate) (void);
243void (*dll_luaL_buffinit) (lua_State *L, luaL_Buffer *B);
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200244void (*dll_luaL_addlstring) (luaL_Buffer *B, const char *s, size_t l);
245void (*dll_luaL_pushresult) (luaL_Buffer *B);
Bram Moolenaardf1643a2020-05-17 18:53:27 +0200246int (*dll_luaL_loadstring) (lua_State *L, const char *s);
Bram Moolenaar1e4c7d02020-06-25 20:56:42 +0200247int (*dll_luaL_ref) (lua_State *L, int idx);
248#if LUA_VERSION_NUM <= 502
249void (*dll_luaL_unref) (lua_State *L, int idx, int n);
250#else
251void (*dll_luaL_unref) (lua_State *L, int idx, lua_Integer n);
252#endif
Bram Moolenaar2ab2e862019-12-04 21:24:53 +0100253// lua
Bram Moolenaar1dced572012-04-05 16:54:08 +0200254#if LUA_VERSION_NUM <= 501
255lua_Number (*dll_lua_tonumber) (lua_State *L, int idx);
256lua_Integer (*dll_lua_tointeger) (lua_State *L, int idx);
257void (*dll_lua_call) (lua_State *L, int nargs, int nresults);
258int (*dll_lua_pcall) (lua_State *L, int nargs, int nresults, int errfunc);
259#else
260lua_Number (*dll_lua_tonumberx) (lua_State *L, int idx, int *isnum);
261lua_Integer (*dll_lua_tointegerx) (lua_State *L, int idx, int *isnum);
262void (*dll_lua_callk) (lua_State *L, int nargs, int nresults, int ctx,
Bram Moolenaardb913952012-06-29 12:54:53 +0200263 lua_CFunction k);
Bram Moolenaar1dced572012-04-05 16:54:08 +0200264int (*dll_lua_pcallk) (lua_State *L, int nargs, int nresults, int errfunc,
Bram Moolenaardb913952012-06-29 12:54:53 +0200265 int ctx, lua_CFunction k);
Bram Moolenaar1dced572012-04-05 16:54:08 +0200266void (*dll_lua_getglobal) (lua_State *L, const char *var);
267void (*dll_lua_setglobal) (lua_State *L, const char *var);
Bram Moolenaar1f860d82015-06-27 18:36:16 +0200268#endif
269#if LUA_VERSION_NUM <= 502
270void (*dll_lua_replace) (lua_State *L, int idx);
271void (*dll_lua_remove) (lua_State *L, int idx);
272#endif
273#if LUA_VERSION_NUM >= 503
274void (*dll_lua_rotate) (lua_State *L, int idx, int n);
Bram Moolenaar9514b1f2015-06-25 18:27:32 +0200275void (*dll_lua_copy) (lua_State *L, int fromidx, int toidx);
Bram Moolenaar1dced572012-04-05 16:54:08 +0200276#endif
Bram Moolenaarbd2f3c32012-04-06 14:31:00 +0200277const char *(*dll_lua_typename) (lua_State *L, int tp);
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200278void (*dll_lua_close) (lua_State *L);
279int (*dll_lua_gettop) (lua_State *L);
280void (*dll_lua_settop) (lua_State *L, int idx);
281void (*dll_lua_pushvalue) (lua_State *L, int idx);
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200282int (*dll_lua_isnumber) (lua_State *L, int idx);
283int (*dll_lua_isstring) (lua_State *L, int idx);
284int (*dll_lua_type) (lua_State *L, int idx);
285int (*dll_lua_rawequal) (lua_State *L, int idx1, int idx2);
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200286int (*dll_lua_toboolean) (lua_State *L, int idx);
287const char *(*dll_lua_tolstring) (lua_State *L, int idx, size_t *len);
288void *(*dll_lua_touserdata) (lua_State *L, int idx);
289void (*dll_lua_pushnil) (lua_State *L);
290void (*dll_lua_pushnumber) (lua_State *L, lua_Number n);
291void (*dll_lua_pushinteger) (lua_State *L, lua_Integer n);
292void (*dll_lua_pushlstring) (lua_State *L, const char *s, size_t l);
293void (*dll_lua_pushstring) (lua_State *L, const char *s);
294const char *(*dll_lua_pushfstring) (lua_State *L, const char *fmt, ...);
295void (*dll_lua_pushcclosure) (lua_State *L, lua_CFunction fn, int n);
296void (*dll_lua_pushboolean) (lua_State *L, int b);
297void (*dll_lua_pushlightuserdata) (lua_State *L, void *p);
298void (*dll_lua_getfield) (lua_State *L, int idx, const char *k);
Bram Moolenaar17413672018-07-14 20:49:42 +0200299#if LUA_VERSION_NUM <= 502
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200300void (*dll_lua_rawget) (lua_State *L, int idx);
Bram Moolenaar1dced572012-04-05 16:54:08 +0200301void (*dll_lua_rawgeti) (lua_State *L, int idx, int n);
Bram Moolenaar17413672018-07-14 20:49:42 +0200302#else
303int (*dll_lua_rawget) (lua_State *L, int idx);
304int (*dll_lua_rawgeti) (lua_State *L, int idx, lua_Integer n);
305#endif
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200306void (*dll_lua_createtable) (lua_State *L, int narr, int nrec);
Yegappan Lakshmanan11328bc2021-08-06 21:34:38 +0200307void (*dll_lua_settable) (lua_State *L, int idx);
Bram Moolenaar830e3582018-08-21 14:23:35 +0200308#if LUA_VERSION_NUM >= 504
309void *(*dll_lua_newuserdatauv) (lua_State *L, size_t sz, int nuvalue);
310#else
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200311void *(*dll_lua_newuserdata) (lua_State *L, size_t sz);
Bram Moolenaar830e3582018-08-21 14:23:35 +0200312#endif
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200313int (*dll_lua_getmetatable) (lua_State *L, int objindex);
314void (*dll_lua_setfield) (lua_State *L, int idx, const char *k);
315void (*dll_lua_rawset) (lua_State *L, int idx);
Bram Moolenaar17413672018-07-14 20:49:42 +0200316#if LUA_VERSION_NUM <= 502
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200317void (*dll_lua_rawseti) (lua_State *L, int idx, int n);
Bram Moolenaar17413672018-07-14 20:49:42 +0200318#else
319void (*dll_lua_rawseti) (lua_State *L, int idx, lua_Integer n);
320#endif
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200321int (*dll_lua_setmetatable) (lua_State *L, int objindex);
Bram Moolenaarbd2f3c32012-04-06 14:31:00 +0200322int (*dll_lua_next) (lua_State *L, int idx);
Bram Moolenaar2ab2e862019-12-04 21:24:53 +0100323// libs
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200324int (*dll_luaopen_base) (lua_State *L);
325int (*dll_luaopen_table) (lua_State *L);
326int (*dll_luaopen_string) (lua_State *L);
327int (*dll_luaopen_math) (lua_State *L);
Bram Moolenaar16c98f92010-07-28 22:46:08 +0200328int (*dll_luaopen_io) (lua_State *L);
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200329int (*dll_luaopen_os) (lua_State *L);
330int (*dll_luaopen_package) (lua_State *L);
331int (*dll_luaopen_debug) (lua_State *L);
Bram Moolenaar16c98f92010-07-28 22:46:08 +0200332void (*dll_luaL_openlibs) (lua_State *L);
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200333
334typedef void **luaV_function;
335typedef struct {
336 const char *name;
337 luaV_function func;
338} luaV_Reg;
339
340static const luaV_Reg luaV_dll[] = {
Bram Moolenaar2ab2e862019-12-04 21:24:53 +0100341 // lauxlib
Bram Moolenaar1dced572012-04-05 16:54:08 +0200342#if LUA_VERSION_NUM <= 501
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200343 {"luaL_register", (luaV_function) &dll_luaL_register},
Bram Moolenaar1dced572012-04-05 16:54:08 +0200344 {"luaL_prepbuffer", (luaV_function) &dll_luaL_prepbuffer},
345 {"luaL_openlib", (luaV_function) &dll_luaL_openlib},
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200346 {"luaL_typerror", (luaV_function) &dll_luaL_typerror},
Bram Moolenaar1dced572012-04-05 16:54:08 +0200347 {"luaL_loadfile", (luaV_function) &dll_luaL_loadfile},
348 {"luaL_loadbuffer", (luaV_function) &dll_luaL_loadbuffer},
349#else
350 {"luaL_prepbuffsize", (luaV_function) &dll_luaL_prepbuffsize},
351 {"luaL_setfuncs", (luaV_function) &dll_luaL_setfuncs},
352 {"luaL_loadfilex", (luaV_function) &dll_luaL_loadfilex},
353 {"luaL_loadbufferx", (luaV_function) &dll_luaL_loadbufferx},
354 {"luaL_argerror", (luaV_function) &dll_luaL_argerror},
355#endif
Bram Moolenaar5551b132020-07-14 21:54:28 +0200356#if LUA_VERSION_NUM >= 504
357 {"luaL_typeerror", (luaV_function) &dll_luaL_typeerror},
358#endif
Bram Moolenaarbd2f3c32012-04-06 14:31:00 +0200359 {"luaL_checkany", (luaV_function) &dll_luaL_checkany},
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200360 {"luaL_checklstring", (luaV_function) &dll_luaL_checklstring},
361 {"luaL_checkinteger", (luaV_function) &dll_luaL_checkinteger},
362 {"luaL_optinteger", (luaV_function) &dll_luaL_optinteger},
363 {"luaL_checktype", (luaV_function) &dll_luaL_checktype},
364 {"luaL_error", (luaV_function) &dll_luaL_error},
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200365 {"luaL_newstate", (luaV_function) &dll_luaL_newstate},
366 {"luaL_buffinit", (luaV_function) &dll_luaL_buffinit},
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200367 {"luaL_addlstring", (luaV_function) &dll_luaL_addlstring},
368 {"luaL_pushresult", (luaV_function) &dll_luaL_pushresult},
Bram Moolenaardf1643a2020-05-17 18:53:27 +0200369 {"luaL_loadstring", (luaV_function) &dll_luaL_loadstring},
Bram Moolenaar1e4c7d02020-06-25 20:56:42 +0200370 {"luaL_ref", (luaV_function) &dll_luaL_ref},
371 {"luaL_unref", (luaV_function) &dll_luaL_unref},
Bram Moolenaar2ab2e862019-12-04 21:24:53 +0100372 // lua
Bram Moolenaar1dced572012-04-05 16:54:08 +0200373#if LUA_VERSION_NUM <= 501
374 {"lua_tonumber", (luaV_function) &dll_lua_tonumber},
375 {"lua_tointeger", (luaV_function) &dll_lua_tointeger},
376 {"lua_call", (luaV_function) &dll_lua_call},
377 {"lua_pcall", (luaV_function) &dll_lua_pcall},
378#else
379 {"lua_tonumberx", (luaV_function) &dll_lua_tonumberx},
380 {"lua_tointegerx", (luaV_function) &dll_lua_tointegerx},
381 {"lua_callk", (luaV_function) &dll_lua_callk},
382 {"lua_pcallk", (luaV_function) &dll_lua_pcallk},
383 {"lua_getglobal", (luaV_function) &dll_lua_getglobal},
384 {"lua_setglobal", (luaV_function) &dll_lua_setglobal},
Bram Moolenaar1dced572012-04-05 16:54:08 +0200385#endif
Bram Moolenaar1f860d82015-06-27 18:36:16 +0200386#if LUA_VERSION_NUM <= 502
387 {"lua_replace", (luaV_function) &dll_lua_replace},
388 {"lua_remove", (luaV_function) &dll_lua_remove},
389#endif
390#if LUA_VERSION_NUM >= 503
391 {"lua_rotate", (luaV_function) &dll_lua_rotate},
392 {"lua_copy", (luaV_function) &dll_lua_copy},
393#endif
Bram Moolenaarbd2f3c32012-04-06 14:31:00 +0200394 {"lua_typename", (luaV_function) &dll_lua_typename},
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200395 {"lua_close", (luaV_function) &dll_lua_close},
396 {"lua_gettop", (luaV_function) &dll_lua_gettop},
397 {"lua_settop", (luaV_function) &dll_lua_settop},
398 {"lua_pushvalue", (luaV_function) &dll_lua_pushvalue},
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200399 {"lua_isnumber", (luaV_function) &dll_lua_isnumber},
400 {"lua_isstring", (luaV_function) &dll_lua_isstring},
401 {"lua_type", (luaV_function) &dll_lua_type},
402 {"lua_rawequal", (luaV_function) &dll_lua_rawequal},
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200403 {"lua_toboolean", (luaV_function) &dll_lua_toboolean},
404 {"lua_tolstring", (luaV_function) &dll_lua_tolstring},
405 {"lua_touserdata", (luaV_function) &dll_lua_touserdata},
406 {"lua_pushnil", (luaV_function) &dll_lua_pushnil},
407 {"lua_pushnumber", (luaV_function) &dll_lua_pushnumber},
408 {"lua_pushinteger", (luaV_function) &dll_lua_pushinteger},
409 {"lua_pushlstring", (luaV_function) &dll_lua_pushlstring},
410 {"lua_pushstring", (luaV_function) &dll_lua_pushstring},
411 {"lua_pushfstring", (luaV_function) &dll_lua_pushfstring},
412 {"lua_pushcclosure", (luaV_function) &dll_lua_pushcclosure},
413 {"lua_pushboolean", (luaV_function) &dll_lua_pushboolean},
414 {"lua_pushlightuserdata", (luaV_function) &dll_lua_pushlightuserdata},
415 {"lua_getfield", (luaV_function) &dll_lua_getfield},
416 {"lua_rawget", (luaV_function) &dll_lua_rawget},
Bram Moolenaar1dced572012-04-05 16:54:08 +0200417 {"lua_rawgeti", (luaV_function) &dll_lua_rawgeti},
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200418 {"lua_createtable", (luaV_function) &dll_lua_createtable},
Yegappan Lakshmanan11328bc2021-08-06 21:34:38 +0200419 {"lua_settable", (luaV_function) &dll_lua_settable},
Bram Moolenaar830e3582018-08-21 14:23:35 +0200420#if LUA_VERSION_NUM >= 504
421 {"lua_newuserdatauv", (luaV_function) &dll_lua_newuserdatauv},
422#else
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200423 {"lua_newuserdata", (luaV_function) &dll_lua_newuserdata},
Bram Moolenaar830e3582018-08-21 14:23:35 +0200424#endif
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200425 {"lua_getmetatable", (luaV_function) &dll_lua_getmetatable},
426 {"lua_setfield", (luaV_function) &dll_lua_setfield},
427 {"lua_rawset", (luaV_function) &dll_lua_rawset},
428 {"lua_rawseti", (luaV_function) &dll_lua_rawseti},
429 {"lua_setmetatable", (luaV_function) &dll_lua_setmetatable},
Bram Moolenaarbd2f3c32012-04-06 14:31:00 +0200430 {"lua_next", (luaV_function) &dll_lua_next},
Bram Moolenaar2ab2e862019-12-04 21:24:53 +0100431 // libs
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200432 {"luaopen_base", (luaV_function) &dll_luaopen_base},
433 {"luaopen_table", (luaV_function) &dll_luaopen_table},
434 {"luaopen_string", (luaV_function) &dll_luaopen_string},
435 {"luaopen_math", (luaV_function) &dll_luaopen_math},
Bram Moolenaar16c98f92010-07-28 22:46:08 +0200436 {"luaopen_io", (luaV_function) &dll_luaopen_io},
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200437 {"luaopen_os", (luaV_function) &dll_luaopen_os},
438 {"luaopen_package", (luaV_function) &dll_luaopen_package},
439 {"luaopen_debug", (luaV_function) &dll_luaopen_debug},
Bram Moolenaar16c98f92010-07-28 22:46:08 +0200440 {"luaL_openlibs", (luaV_function) &dll_luaL_openlibs},
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200441 {NULL, NULL}
442};
443
Bram Moolenaar2334b6d2010-07-22 21:32:16 +0200444static HANDLE hinstLua = NULL;
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200445
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200446 static int
447lua_link_init(char *libname, int verbose)
448{
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200449 const luaV_Reg *reg;
450 if (hinstLua) return OK;
Bram Moolenaar2334b6d2010-07-22 21:32:16 +0200451 hinstLua = load_dll(libname);
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200452 if (!hinstLua)
453 {
454 if (verbose)
Bram Moolenaar460ae5d2022-01-01 14:19:49 +0000455 semsg(_(e_could_not_load_library_str_str), libname, load_dll_error());
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200456 return FAIL;
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200457 }
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200458 for (reg = luaV_dll; reg->func; reg++)
459 {
Bram Moolenaar2334b6d2010-07-22 21:32:16 +0200460 if ((*reg->func = symbol_from_dll(hinstLua, reg->name)) == NULL)
461 {
462 close_dll(hinstLua);
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200463 hinstLua = 0;
464 if (verbose)
Bram Moolenaar460ae5d2022-01-01 14:19:49 +0000465 semsg(_(e_could_not_load_library_function_str), reg->name);
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200466 return FAIL;
467 }
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200468 }
469 return OK;
470}
Bram Moolenaar2ab2e862019-12-04 21:24:53 +0100471#endif // DYNAMIC_LUA
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200472
Bram Moolenaard90b6c02016-08-28 18:10:45 +0200473#if defined(DYNAMIC_LUA) || defined(PROTO)
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200474 int
475lua_enabled(int verbose)
476{
Bram Moolenaar25e4fcd2016-01-09 14:57:47 +0100477 return lua_link_init((char *)p_luadll, verbose) == OK;
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200478}
Bram Moolenaard90b6c02016-08-28 18:10:45 +0200479#endif
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200480
Bram Moolenaar5551b132020-07-14 21:54:28 +0200481#if LUA_VERSION_NUM > 501 && LUA_VERSION_NUM < 504
Bram Moolenaar1dced572012-04-05 16:54:08 +0200482 static int
Bram Moolenaar4eefe472019-03-19 21:59:19 +0100483luaL_typeerror(lua_State *L, int narg, const char *tname)
Bram Moolenaar1dced572012-04-05 16:54:08 +0200484{
485 const char *msg = lua_pushfstring(L, "%s expected, got %s",
Bram Moolenaardb913952012-06-29 12:54:53 +0200486 tname, luaL_typename(L, narg));
Bram Moolenaar1dced572012-04-05 16:54:08 +0200487 return luaL_argerror(L, narg, msg);
488}
489#endif
490
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200491
Bram Moolenaar2ab2e862019-12-04 21:24:53 +0100492// ======= Internal =======
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200493
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200494 static void
495luaV_newmetatable(lua_State *L, const char *tname)
496{
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200497 lua_newtable(L);
498 lua_pushlightuserdata(L, (void *) tname);
499 lua_pushvalue(L, -2);
500 lua_rawset(L, LUA_REGISTRYINDEX);
501}
502
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200503 static void *
504luaV_toudata(lua_State *L, int ud, const char *tname)
505{
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200506 void *p = lua_touserdata(L, ud);
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200507
Bram Moolenaar2ab2e862019-12-04 21:24:53 +0100508 if (p != NULL) // value is userdata?
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200509 {
Bram Moolenaar2ab2e862019-12-04 21:24:53 +0100510 if (lua_getmetatable(L, ud)) // does it have a metatable?
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200511 {
Bram Moolenaar2ab2e862019-12-04 21:24:53 +0100512 luaV_getfield(L, tname); // get metatable
513 if (lua_rawequal(L, -1, -2)) // MTs match?
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200514 {
Bram Moolenaar2ab2e862019-12-04 21:24:53 +0100515 lua_pop(L, 2); // MTs
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200516 return p;
517 }
518 }
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200519 }
520 return NULL;
521}
522
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200523 static void *
Bram Moolenaar1dced572012-04-05 16:54:08 +0200524luaV_checkcache(lua_State *L, void *p)
525{
526 luaV_getudata(L, p);
527 if (lua_isnil(L, -1)) luaL_error(L, "invalid object");
528 lua_pop(L, 1);
529 return p;
530}
531
532#define luaV_unbox(L,luatyp,ud) (*((luatyp *) lua_touserdata((L),(ud))))
533
534#define luaV_checkvalid(L,luatyp,ud) \
535 luaV_checkcache((L), (void *) luaV_unbox((L),luatyp,(ud)))
536
537 static void *
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200538luaV_checkudata(lua_State *L, int ud, const char *tname)
539{
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200540 void *p = luaV_toudata(L, ud, tname);
Bram Moolenaar1dced572012-04-05 16:54:08 +0200541 if (p == NULL) luaL_typeerror(L, ud, tname);
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200542 return p;
543}
544
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200545 static void
546luaV_pushtypval(lua_State *L, typval_T *tv)
547{
Bram Moolenaar1dced572012-04-05 16:54:08 +0200548 if (tv == NULL)
549 {
550 lua_pushnil(L);
551 return;
552 }
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200553 switch (tv->v_type)
554 {
555 case VAR_STRING:
Bram Moolenaard04da7c2012-10-14 03:41:59 +0200556 lua_pushstring(L, tv->vval.v_string == NULL
557 ? "" : (char *)tv->vval.v_string);
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200558 break;
559 case VAR_NUMBER:
560 lua_pushinteger(L, (int) tv->vval.v_number);
561 break;
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200562 case VAR_FLOAT:
563 lua_pushnumber(L, (lua_Number) tv->vval.v_float);
564 break;
Bram Moolenaar1dced572012-04-05 16:54:08 +0200565 case VAR_LIST:
566 luaV_pushlist(L, tv->vval.v_list);
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200567 break;
Bram Moolenaar1dced572012-04-05 16:54:08 +0200568 case VAR_DICT:
569 luaV_pushdict(L, tv->vval.v_dict);
570 break;
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +0100571 case VAR_BOOL:
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100572 case VAR_SPECIAL:
573 if (tv->vval.v_number <= VVAL_TRUE)
574 lua_pushinteger(L, (int) tv->vval.v_number);
575 else
576 lua_pushnil(L);
577 break;
Bram Moolenaarca06da92018-07-01 15:12:05 +0200578 case VAR_FUNC:
Bram Moolenaar4eefe472019-03-19 21:59:19 +0100579 luaV_pushfuncref(L, tv->vval.v_string);
Bram Moolenaarca06da92018-07-01 15:12:05 +0200580 break;
Bram Moolenaar86c3a212021-03-08 19:50:24 +0100581 case VAR_PARTIAL:
582 // TODO: handle partial arguments
583 luaV_pushfuncref(L, partial_name(tv->vval.v_partial));
584 break;
585
Bram Moolenaarb7828692019-03-23 13:57:02 +0100586 case VAR_BLOB:
587 luaV_pushblob(L, tv->vval.v_blob);
588 break;
Bram Moolenaar1dced572012-04-05 16:54:08 +0200589 default:
590 lua_pushnil(L);
591 }
592}
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200593
Bram Moolenaarca06da92018-07-01 15:12:05 +0200594/*
595 * Converts lua value at 'pos' to typval 'tv'.
596 * Returns OK or FAIL.
597 */
598 static int
599luaV_totypval(lua_State *L, int pos, typval_T *tv)
Bram Moolenaar1dced572012-04-05 16:54:08 +0200600{
Bram Moolenaarca06da92018-07-01 15:12:05 +0200601 int status = OK;
602
Bram Moolenaara9a8e5f2020-07-02 21:17:57 +0200603 tv->v_lock = 0;
604
Bram Moolenaarca06da92018-07-01 15:12:05 +0200605 switch (lua_type(L, pos))
606 {
Bram Moolenaar1dced572012-04-05 16:54:08 +0200607 case LUA_TBOOLEAN:
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +0100608 tv->v_type = VAR_BOOL;
Bram Moolenaar1dced572012-04-05 16:54:08 +0200609 tv->vval.v_number = (varnumber_T) lua_toboolean(L, pos);
610 break;
Bram Moolenaar9067cd62019-01-01 00:41:54 +0100611 case LUA_TNIL:
612 tv->v_type = VAR_SPECIAL;
613 tv->vval.v_number = VVAL_NULL;
614 break;
Bram Moolenaar1dced572012-04-05 16:54:08 +0200615 case LUA_TSTRING:
616 tv->v_type = VAR_STRING;
617 tv->vval.v_string = vim_strsave((char_u *) lua_tostring(L, pos));
618 break;
619 case LUA_TNUMBER:
Bram Moolenaareb04f082020-05-17 14:32:35 +0200620 {
621 const lua_Number n = lua_tonumber(L, pos);
622
623 if (n > (lua_Number)INT64_MAX || n < (lua_Number)INT64_MIN
624 || ((lua_Number)((varnumber_T)n)) != n)
625 {
626 tv->v_type = VAR_FLOAT;
627 tv->vval.v_float = (float_T)n;
628 }
629 else
630 {
631 tv->v_type = VAR_NUMBER;
632 tv->vval.v_number = (varnumber_T)n;
633 }
634 }
Bram Moolenaar1dced572012-04-05 16:54:08 +0200635 break;
Bram Moolenaar801ab062020-06-25 19:27:56 +0200636 case LUA_TFUNCTION:
637 {
638 char_u *name;
Bram Moolenaar066e7da2020-07-18 12:50:35 +0200639 luaV_CFuncState *state;
640
Bram Moolenaar801ab062020-06-25 19:27:56 +0200641 lua_pushvalue(L, pos);
Bram Moolenaar066e7da2020-07-18 12:50:35 +0200642 state = ALLOC_CLEAR_ONE(luaV_CFuncState);
Bram Moolenaar801ab062020-06-25 19:27:56 +0200643 state->lua_funcref = luaL_ref(L, LUA_REGISTRYINDEX);
644 state->L = L;
645 state->lua_tableref = LUA_NOREF;
646 name = register_cfunc(&luaV_call_lua_func,
647 &luaV_call_lua_func_free, state);
648 tv->v_type = VAR_FUNC;
649 tv->vval.v_string = vim_strsave(name);
650 break;
651 }
652 case LUA_TTABLE:
653 {
Bram Moolenaar066e7da2020-07-18 12:50:35 +0200654 int lua_tableref;
655
Bram Moolenaar801ab062020-06-25 19:27:56 +0200656 lua_pushvalue(L, pos);
Bram Moolenaar066e7da2020-07-18 12:50:35 +0200657 lua_tableref = luaL_ref(L, LUA_REGISTRYINDEX);
Bram Moolenaar801ab062020-06-25 19:27:56 +0200658 if (lua_getmetatable(L, pos)) {
659 lua_getfield(L, -1, LUA___CALL);
660 if (lua_isfunction(L, -1)) {
661 char_u *name;
662 int lua_funcref = luaL_ref(L, LUA_REGISTRYINDEX);
663 luaV_CFuncState *state = ALLOC_CLEAR_ONE(luaV_CFuncState);
Bram Moolenaar066e7da2020-07-18 12:50:35 +0200664
Bram Moolenaar801ab062020-06-25 19:27:56 +0200665 state->lua_funcref = lua_funcref;
666 state->L = L;
667 state->lua_tableref = lua_tableref;
668 name = register_cfunc(&luaV_call_lua_func,
669 &luaV_call_lua_func_free, state);
670 tv->v_type = VAR_FUNC;
671 tv->vval.v_string = vim_strsave(name);
672 break;
673 }
674 }
675 tv->v_type = VAR_NUMBER;
676 tv->vval.v_number = 0;
677 status = FAIL;
678 break;
679 }
Bram Moolenaarca06da92018-07-01 15:12:05 +0200680 case LUA_TUSERDATA:
681 {
Bram Moolenaar1dced572012-04-05 16:54:08 +0200682 void *p = lua_touserdata(L, pos);
Bram Moolenaarca06da92018-07-01 15:12:05 +0200683
Bram Moolenaarb7828692019-03-23 13:57:02 +0100684 if (lua_getmetatable(L, pos)) // has metatable?
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200685 {
Bram Moolenaarb7828692019-03-23 13:57:02 +0100686 // check list
Bram Moolenaar1dced572012-04-05 16:54:08 +0200687 luaV_getfield(L, LUAVIM_LIST);
688 if (lua_rawequal(L, -1, -2))
Bram Moolenaar2334b6d2010-07-22 21:32:16 +0200689 {
Bram Moolenaar1dced572012-04-05 16:54:08 +0200690 tv->v_type = VAR_LIST;
691 tv->vval.v_list = *((luaV_List *) p);
692 ++tv->vval.v_list->lv_refcount;
Bram Moolenaarb7828692019-03-23 13:57:02 +0100693 lua_pop(L, 2); // MTs
Bram Moolenaarca06da92018-07-01 15:12:05 +0200694 break;
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200695 }
Bram Moolenaarb7828692019-03-23 13:57:02 +0100696 // check dict
Bram Moolenaar1dced572012-04-05 16:54:08 +0200697 luaV_getfield(L, LUAVIM_DICT);
698 if (lua_rawequal(L, -1, -3))
699 {
700 tv->v_type = VAR_DICT;
701 tv->vval.v_dict = *((luaV_Dict *) p);
702 ++tv->vval.v_dict->dv_refcount;
Bram Moolenaarb7828692019-03-23 13:57:02 +0100703 lua_pop(L, 3); // MTs
Bram Moolenaarca06da92018-07-01 15:12:05 +0200704 break;
Bram Moolenaar1dced572012-04-05 16:54:08 +0200705 }
Bram Moolenaarb7828692019-03-23 13:57:02 +0100706 // check blob
707 luaV_getfield(L, LUAVIM_BLOB);
Bram Moolenaarca06da92018-07-01 15:12:05 +0200708 if (lua_rawequal(L, -1, -4))
709 {
Bram Moolenaarb7828692019-03-23 13:57:02 +0100710 tv->v_type = VAR_BLOB;
711 tv->vval.v_blob = *((luaV_Blob *) p);
712 ++tv->vval.v_blob->bv_refcount;
713 lua_pop(L, 4); // MTs
714 break;
715 }
716 // check funcref
717 luaV_getfield(L, LUAVIM_FUNCREF);
718 if (lua_rawequal(L, -1, -5))
719 {
Bram Moolenaarca06da92018-07-01 15:12:05 +0200720 luaV_Funcref *f = (luaV_Funcref *) p;
Bram Moolenaar066e7da2020-07-18 12:50:35 +0200721
Bram Moolenaar4eefe472019-03-19 21:59:19 +0100722 func_ref(f->name);
723 tv->v_type = VAR_FUNC;
724 tv->vval.v_string = vim_strsave(f->name);
Bram Moolenaarb7828692019-03-23 13:57:02 +0100725 lua_pop(L, 5); // MTs
Bram Moolenaarca06da92018-07-01 15:12:05 +0200726 break;
727 }
Bram Moolenaarb7828692019-03-23 13:57:02 +0100728 lua_pop(L, 4); // MTs
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200729 }
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200730 }
Bram Moolenaarb7828692019-03-23 13:57:02 +0100731 // FALLTHROUGH
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200732 default:
Bram Moolenaar1dced572012-04-05 16:54:08 +0200733 tv->v_type = VAR_NUMBER;
734 tv->vval.v_number = 0;
Bram Moolenaarca06da92018-07-01 15:12:05 +0200735 status = FAIL;
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200736 }
Bram Moolenaarca06da92018-07-01 15:12:05 +0200737 return status;
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200738}
739
Bram Moolenaar2ab2e862019-12-04 21:24:53 +0100740/*
741 * similar to luaL_addlstring, but replaces \0 with \n if toline and
742 * \n with \0 otherwise
743 */
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200744 static void
745luaV_addlstring(luaL_Buffer *b, const char *s, size_t l, int toline)
746{
747 while (l--)
748 {
749 if (*s == '\0' && toline)
750 luaL_addchar(b, '\n');
751 else if (*s == '\n' && !toline)
752 luaL_addchar(b, '\0');
753 else
754 luaL_addchar(b, *s);
755 s++;
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200756 }
757}
758
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200759 static void
760luaV_pushline(lua_State *L, buf_T *buf, linenr_T n)
761{
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200762 const char *s = (const char *) ml_get_buf(buf, n, FALSE);
763 luaL_Buffer b;
764 luaL_buffinit(L, &b);
765 luaV_addlstring(&b, s, strlen(s), 0);
766 luaL_pushresult(&b);
767}
768
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200769 static char_u *
770luaV_toline(lua_State *L, int pos)
771{
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200772 size_t l;
773 const char *s = lua_tolstring(L, pos, &l);
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200774
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200775 luaL_Buffer b;
776 luaL_buffinit(L, &b);
777 luaV_addlstring(&b, s, l, 1);
778 luaL_pushresult(&b);
779 return (char_u *) lua_tostring(L, -1);
780}
781
Bram Moolenaar2ab2e862019-12-04 21:24:53 +0100782/*
783 * pops a string s from the top of the stack and calls mf(t) for pieces t of
784 * s separated by newlines
785 */
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200786 static void
787luaV_msgfunc(lua_State *L, msgfunc_T mf)
788{
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200789 luaL_Buffer b;
790 size_t l;
791 const char *p, *s = lua_tolstring(L, -1, &l);
792 luaL_buffinit(L, &b);
793 luaV_addlstring(&b, s, l, 0);
794 luaL_pushresult(&b);
Bram Moolenaar2ab2e862019-12-04 21:24:53 +0100795 // break string
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200796 p = s = lua_tolstring(L, -1, &l);
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200797 while (l--)
798 {
Bram Moolenaar2ab2e862019-12-04 21:24:53 +0100799 if (*p++ == '\0') // break?
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200800 {
Bram Moolenaarc8970b92020-10-26 20:18:08 +0100801 mf((char *)s);
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200802 s = p;
803 }
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200804 }
Bram Moolenaarc8970b92020-10-26 20:18:08 +0100805 mf((char *)s);
Bram Moolenaar2ab2e862019-12-04 21:24:53 +0100806 lua_pop(L, 2); // original and modified strings
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200807}
808
Bram Moolenaar1dced572012-04-05 16:54:08 +0200809#define luaV_newtype(typ,tname,luatyp,luatname) \
810 static luatyp * \
Bram Moolenaar4eefe472019-03-19 21:59:19 +0100811 luaV_new##tname(lua_State *L, typ *obj) \
Bram Moolenaar1dced572012-04-05 16:54:08 +0200812 { \
813 luatyp *o = (luatyp *) lua_newuserdata(L, sizeof(luatyp)); \
814 *o = obj; \
815 luaV_setudata(L, obj); /* cache[obj] = udata */ \
816 luaV_getfield(L, luatname); \
817 lua_setmetatable(L, -2); \
818 return o; \
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200819 }
Bram Moolenaar1dced572012-04-05 16:54:08 +0200820
821#define luaV_pushtype(typ,tname,luatyp) \
822 static luatyp * \
Bram Moolenaarca06da92018-07-01 15:12:05 +0200823 luaV_push##tname(lua_State *L, typ *obj) \
Bram Moolenaar1dced572012-04-05 16:54:08 +0200824 { \
825 luatyp *o = NULL; \
826 if (obj == NULL) \
827 lua_pushnil(L); \
828 else { \
829 luaV_getudata(L, obj); \
830 if (lua_isnil(L, -1)) /* not interned? */ \
831 { \
832 lua_pop(L, 1); \
833 o = luaV_new##tname(L, obj); \
834 } \
835 else \
836 o = (luatyp *) lua_touserdata(L, -1); \
837 } \
838 return o; \
839 }
840
841#define luaV_type_tostring(tname,luatname) \
842 static int \
Bram Moolenaar4eefe472019-03-19 21:59:19 +0100843 luaV_##tname##_tostring(lua_State *L) \
Bram Moolenaar1dced572012-04-05 16:54:08 +0200844 { \
845 lua_pushfstring(L, "%s: %p", luatname, lua_touserdata(L, 1)); \
846 return 1; \
847 }
848
Bram Moolenaar2ab2e862019-12-04 21:24:53 +0100849// ======= List type =======
Bram Moolenaar1dced572012-04-05 16:54:08 +0200850
851 static luaV_List *
Bram Moolenaar4eefe472019-03-19 21:59:19 +0100852luaV_newlist(lua_State *L, list_T *lis)
Bram Moolenaar1dced572012-04-05 16:54:08 +0200853{
854 luaV_List *l = (luaV_List *) lua_newuserdata(L, sizeof(luaV_List));
855 *l = lis;
Bram Moolenaar2ab2e862019-12-04 21:24:53 +0100856 lis->lv_refcount++; // reference in Lua
857 luaV_setudata(L, lis); // cache[lis] = udata
Bram Moolenaar1dced572012-04-05 16:54:08 +0200858 luaV_getfield(L, LUAVIM_LIST);
859 lua_setmetatable(L, -2);
860 return l;
861}
862
863luaV_pushtype(list_T, list, luaV_List)
864luaV_type_tostring(list, LUAVIM_LIST)
865
866 static int
Bram Moolenaar4eefe472019-03-19 21:59:19 +0100867luaV_list_len(lua_State *L)
Bram Moolenaar1dced572012-04-05 16:54:08 +0200868{
869 list_T *l = luaV_unbox(L, luaV_List, 1);
Bram Moolenaarb7828692019-03-23 13:57:02 +0100870 lua_pushinteger(L, (int) list_len(l));
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200871 return 1;
872}
873
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200874 static int
Bram Moolenaar4eefe472019-03-19 21:59:19 +0100875luaV_list_iter(lua_State *L)
Bram Moolenaar1dced572012-04-05 16:54:08 +0200876{
877 listitem_T *li = (listitem_T *) lua_touserdata(L, lua_upvalueindex(2));
878 if (li == NULL) return 0;
879 luaV_pushtypval(L, &li->li_tv);
880 lua_pushlightuserdata(L, (void *) li->li_next);
881 lua_replace(L, lua_upvalueindex(2));
882 return 1;
883}
884
885 static int
Bram Moolenaar4eefe472019-03-19 21:59:19 +0100886luaV_list_call(lua_State *L)
Bram Moolenaar1dced572012-04-05 16:54:08 +0200887{
888 list_T *l = luaV_unbox(L, luaV_List, 1);
Bram Moolenaar2ab2e862019-12-04 21:24:53 +0100889 lua_pushvalue(L, lua_upvalueindex(1)); // pass cache table along
Bram Moolenaar1dced572012-04-05 16:54:08 +0200890 lua_pushlightuserdata(L, (void *) l->lv_first);
891 lua_pushcclosure(L, luaV_list_iter, 2);
892 return 1;
893}
894
895 static int
Bram Moolenaar4eefe472019-03-19 21:59:19 +0100896luaV_list_index(lua_State *L)
Bram Moolenaar1dced572012-04-05 16:54:08 +0200897{
898 list_T *l = luaV_unbox(L, luaV_List, 1);
Bram Moolenaar2ab2e862019-12-04 21:24:53 +0100899 if (lua_isnumber(L, 2)) // list item?
Bram Moolenaar1dced572012-04-05 16:54:08 +0200900 {
Bram Moolenaarbd846172020-06-27 12:32:57 +0200901 long n = (long) luaL_checkinteger(L, 2);
902 listitem_T *li;
903
904 // Lua array index starts with 1 while Vim uses 0, subtract 1 to
905 // normalize.
906 n -= 1;
907 li = list_find(l, n);
Bram Moolenaar1dced572012-04-05 16:54:08 +0200908 if (li == NULL)
909 lua_pushnil(L);
910 else
911 luaV_pushtypval(L, &li->li_tv);
912 }
Bram Moolenaar2ab2e862019-12-04 21:24:53 +0100913 else if (lua_isstring(L, 2)) // method?
Bram Moolenaar1dced572012-04-05 16:54:08 +0200914 {
915 const char *s = lua_tostring(L, 2);
916 if (strncmp(s, "add", 3) == 0
Bram Moolenaarb3766472013-04-15 13:49:21 +0200917 || strncmp(s, "insert", 6) == 0)
Bram Moolenaar1dced572012-04-05 16:54:08 +0200918 {
919 lua_getmetatable(L, 1);
920 lua_getfield(L, -1, s);
921 }
922 else
923 lua_pushnil(L);
924 }
925 else
926 lua_pushnil(L);
927 return 1;
928}
929
930 static int
Bram Moolenaar4eefe472019-03-19 21:59:19 +0100931luaV_list_newindex(lua_State *L)
Bram Moolenaar1dced572012-04-05 16:54:08 +0200932{
933 list_T *l = luaV_unbox(L, luaV_List, 1);
934 long n = (long) luaL_checkinteger(L, 2);
935 listitem_T *li;
Bram Moolenaarbd846172020-06-27 12:32:57 +0200936
937 // Lua array index starts with 1 while Vim uses 0, subtract 1 to normalize.
938 n -= 1;
939
Bram Moolenaar1dced572012-04-05 16:54:08 +0200940 if (l->lv_lock)
941 luaL_error(L, "list is locked");
942 li = list_find(l, n);
Bram Moolenaara1f9f862020-06-28 22:41:26 +0200943 if (li == NULL)
Bram Moolenaar1dced572012-04-05 16:54:08 +0200944 {
Bram Moolenaar6ed545e2022-05-09 20:09:23 +0100945 if (!lua_isnil(L, 3))
946 {
947 typval_T v;
948 luaV_checktypval(L, 3, &v, "inserting list item");
949 if (list_insert_tv(l, &v, li) == FAIL)
950 luaL_error(L, "failed to add item to list");
951 clear_tv(&v);
952 }
Bram Moolenaar1dced572012-04-05 16:54:08 +0200953 }
954 else
955 {
Bram Moolenaar6ed545e2022-05-09 20:09:23 +0100956 if (lua_isnil(L, 3)) // remove?
957 {
Bram Moolenaara1f9f862020-06-28 22:41:26 +0200958 vimlist_remove(l, li, li);
959 listitem_free(l, li);
Bram Moolenaar6ed545e2022-05-09 20:09:23 +0100960 }
961 else
962 {
Bram Moolenaara1f9f862020-06-28 22:41:26 +0200963 typval_T v;
964 luaV_checktypval(L, 3, &v, "setting list item");
965 clear_tv(&li->li_tv);
Bram Moolenaare49b8e82020-07-01 13:52:55 +0200966 li->li_tv = v;
Bram Moolenaar6ed545e2022-05-09 20:09:23 +0100967 }
Bram Moolenaar1dced572012-04-05 16:54:08 +0200968 }
969 return 0;
970}
971
972 static int
Bram Moolenaar4eefe472019-03-19 21:59:19 +0100973luaV_list_add(lua_State *L)
Bram Moolenaar1dced572012-04-05 16:54:08 +0200974{
975 luaV_List *lis = luaV_checkudata(L, 1, LUAVIM_LIST);
976 list_T *l = (list_T *) luaV_checkcache(L, (void *) *lis);
Bram Moolenaarb3766472013-04-15 13:49:21 +0200977 typval_T v;
Bram Moolenaar1dced572012-04-05 16:54:08 +0200978 if (l->lv_lock)
979 luaL_error(L, "list is locked");
Bram Moolenaarb3766472013-04-15 13:49:21 +0200980 lua_settop(L, 2);
Bram Moolenaarca06da92018-07-01 15:12:05 +0200981 luaV_checktypval(L, 2, &v, "adding list item");
Bram Moolenaarb3766472013-04-15 13:49:21 +0200982 if (list_append_tv(l, &v) == FAIL)
Bram Moolenaarca06da92018-07-01 15:12:05 +0200983 luaL_error(L, "failed to add item to list");
Bram Moolenaarb3766472013-04-15 13:49:21 +0200984 clear_tv(&v);
Bram Moolenaar1dced572012-04-05 16:54:08 +0200985 lua_settop(L, 1);
986 return 1;
987}
988
989 static int
Bram Moolenaar4eefe472019-03-19 21:59:19 +0100990luaV_list_insert(lua_State *L)
Bram Moolenaar1dced572012-04-05 16:54:08 +0200991{
992 luaV_List *lis = luaV_checkudata(L, 1, LUAVIM_LIST);
993 list_T *l = (list_T *) luaV_checkcache(L, (void *) *lis);
Bram Moolenaar46538ee2015-02-17 16:28:55 +0100994 long pos = (long) luaL_optinteger(L, 3, 0);
Bram Moolenaar1dced572012-04-05 16:54:08 +0200995 listitem_T *li = NULL;
996 typval_T v;
997 if (l->lv_lock)
998 luaL_error(L, "list is locked");
999 if (pos < l->lv_len)
1000 {
1001 li = list_find(l, pos);
1002 if (li == NULL)
1003 luaL_error(L, "invalid position");
1004 }
1005 lua_settop(L, 2);
Bram Moolenaarca06da92018-07-01 15:12:05 +02001006 luaV_checktypval(L, 2, &v, "inserting list item");
Bram Moolenaarb3766472013-04-15 13:49:21 +02001007 if (list_insert_tv(l, &v, li) == FAIL)
Bram Moolenaarca06da92018-07-01 15:12:05 +02001008 luaL_error(L, "failed to add item to list");
Bram Moolenaarb3766472013-04-15 13:49:21 +02001009 clear_tv(&v);
Bram Moolenaar1dced572012-04-05 16:54:08 +02001010 lua_settop(L, 1);
1011 return 1;
1012}
1013
1014static const luaL_Reg luaV_List_mt[] = {
1015 {"__tostring", luaV_list_tostring},
Bram Moolenaar1dced572012-04-05 16:54:08 +02001016 {"__len", luaV_list_len},
1017 {"__call", luaV_list_call},
1018 {"__index", luaV_list_index},
1019 {"__newindex", luaV_list_newindex},
1020 {"add", luaV_list_add},
1021 {"insert", luaV_list_insert},
1022 {NULL, NULL}
1023};
1024
1025
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001026// ======= Dict type =======
Bram Moolenaar1dced572012-04-05 16:54:08 +02001027
1028 static luaV_Dict *
Bram Moolenaar4eefe472019-03-19 21:59:19 +01001029luaV_newdict(lua_State *L, dict_T *dic)
Bram Moolenaar1dced572012-04-05 16:54:08 +02001030{
1031 luaV_Dict *d = (luaV_Dict *) lua_newuserdata(L, sizeof(luaV_Dict));
1032 *d = dic;
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001033 dic->dv_refcount++; // reference in Lua
1034 luaV_setudata(L, dic); // cache[dic] = udata
Bram Moolenaar1dced572012-04-05 16:54:08 +02001035 luaV_getfield(L, LUAVIM_DICT);
1036 lua_setmetatable(L, -2);
1037 return d;
1038}
1039
1040luaV_pushtype(dict_T, dict, luaV_Dict)
1041luaV_type_tostring(dict, LUAVIM_DICT)
1042
1043 static int
Bram Moolenaar4eefe472019-03-19 21:59:19 +01001044luaV_dict_len(lua_State *L)
Bram Moolenaar1dced572012-04-05 16:54:08 +02001045{
1046 dict_T *d = luaV_unbox(L, luaV_Dict, 1);
Bram Moolenaarb7828692019-03-23 13:57:02 +01001047 lua_pushinteger(L, (int) dict_len(d));
Bram Moolenaar1dced572012-04-05 16:54:08 +02001048 return 1;
1049}
1050
1051 static int
Bram Moolenaar4eefe472019-03-19 21:59:19 +01001052luaV_dict_iter(lua_State *L UNUSED)
Bram Moolenaar1dced572012-04-05 16:54:08 +02001053{
Bram Moolenaarfeeaa682013-02-14 22:19:51 +01001054#ifdef FEAT_EVAL
Bram Moolenaar1dced572012-04-05 16:54:08 +02001055 hashitem_T *hi = (hashitem_T *) lua_touserdata(L, lua_upvalueindex(2));
1056 int n = lua_tointeger(L, lua_upvalueindex(3));
1057 dictitem_T *di;
1058 if (n <= 0) return 0;
1059 while (HASHITEM_EMPTY(hi)) hi++;
1060 di = dict_lookup(hi);
1061 lua_pushstring(L, (char *) hi->hi_key);
1062 luaV_pushtypval(L, &di->di_tv);
1063 lua_pushlightuserdata(L, (void *) (hi + 1));
1064 lua_replace(L, lua_upvalueindex(2));
1065 lua_pushinteger(L, n - 1);
1066 lua_replace(L, lua_upvalueindex(3));
1067 return 2;
Bram Moolenaarfeeaa682013-02-14 22:19:51 +01001068#else
1069 return 0;
1070#endif
Bram Moolenaar1dced572012-04-05 16:54:08 +02001071}
1072
1073 static int
Bram Moolenaar4eefe472019-03-19 21:59:19 +01001074luaV_dict_call(lua_State *L)
Bram Moolenaar1dced572012-04-05 16:54:08 +02001075{
1076 dict_T *d = luaV_unbox(L, luaV_Dict, 1);
1077 hashtab_T *ht = &d->dv_hashtab;
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001078 lua_pushvalue(L, lua_upvalueindex(1)); // pass cache table along
Bram Moolenaar1dced572012-04-05 16:54:08 +02001079 lua_pushlightuserdata(L, (void *) ht->ht_array);
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001080 lua_pushinteger(L, ht->ht_used); // # remaining items
Bram Moolenaar1dced572012-04-05 16:54:08 +02001081 lua_pushcclosure(L, luaV_dict_iter, 3);
1082 return 1;
1083}
1084
1085 static int
Bram Moolenaarca06da92018-07-01 15:12:05 +02001086luaV_dict_index(lua_State *L)
Bram Moolenaar1dced572012-04-05 16:54:08 +02001087{
1088 dict_T *d = luaV_unbox(L, luaV_Dict, 1);
1089 char_u *key = (char_u *) luaL_checkstring(L, 2);
1090 dictitem_T *di = dict_find(d, key, -1);
Bram Moolenaarca06da92018-07-01 15:12:05 +02001091
Bram Moolenaar1dced572012-04-05 16:54:08 +02001092 if (di == NULL)
1093 lua_pushnil(L);
1094 else
Bram Moolenaarca06da92018-07-01 15:12:05 +02001095 {
Bram Moolenaar1dced572012-04-05 16:54:08 +02001096 luaV_pushtypval(L, &di->di_tv);
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001097 if (di->di_tv.v_type == VAR_FUNC) // funcref?
Bram Moolenaarca06da92018-07-01 15:12:05 +02001098 {
1099 luaV_Funcref *f = (luaV_Funcref *) lua_touserdata(L, -1);
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001100 f->self = d; // keep "self" reference
Bram Moolenaarca06da92018-07-01 15:12:05 +02001101 d->dv_refcount++;
1102 }
1103 }
Bram Moolenaar1dced572012-04-05 16:54:08 +02001104 return 1;
1105}
1106
1107 static int
Bram Moolenaarca06da92018-07-01 15:12:05 +02001108luaV_dict_newindex(lua_State *L)
Bram Moolenaar1dced572012-04-05 16:54:08 +02001109{
1110 dict_T *d = luaV_unbox(L, luaV_Dict, 1);
1111 char_u *key = (char_u *) luaL_checkstring(L, 2);
1112 dictitem_T *di;
Bram Moolenaare49b8e82020-07-01 13:52:55 +02001113 typval_T tv;
Bram Moolenaar713bf9e92019-03-16 16:38:41 +01001114
Bram Moolenaar1dced572012-04-05 16:54:08 +02001115 if (d->dv_lock)
1116 luaL_error(L, "dict is locked");
Bram Moolenaard6ef5f92018-07-13 22:08:23 +02001117 if (key == NULL)
1118 return 0;
1119 if (*key == NUL)
Bram Moolenaarca06da92018-07-01 15:12:05 +02001120 luaL_error(L, "empty key");
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001121 if (!lua_isnil(L, 3)) // read value?
Bram Moolenaar17413672018-07-14 20:49:42 +02001122 {
Bram Moolenaare49b8e82020-07-01 13:52:55 +02001123 luaV_checktypval(L, 3, &tv, "setting dict item");
1124 if (d->dv_scope == VAR_DEF_SCOPE && tv.v_type == VAR_FUNC)
1125 {
1126 clear_tv(&tv);
Bram Moolenaarca06da92018-07-01 15:12:05 +02001127 luaL_error(L, "cannot assign funcref to builtin scope");
Bram Moolenaare49b8e82020-07-01 13:52:55 +02001128 }
Bram Moolenaarca06da92018-07-01 15:12:05 +02001129 }
Bram Moolenaar1dced572012-04-05 16:54:08 +02001130 di = dict_find(d, key, -1);
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001131 if (di == NULL) // non-existing key?
Bram Moolenaar1dced572012-04-05 16:54:08 +02001132 {
Bram Moolenaard6ef5f92018-07-13 22:08:23 +02001133 if (lua_isnil(L, 3))
1134 return 0;
Bram Moolenaar1dced572012-04-05 16:54:08 +02001135 di = dictitem_alloc(key);
Bram Moolenaard6ef5f92018-07-13 22:08:23 +02001136 if (di == NULL)
Bram Moolenaare49b8e82020-07-01 13:52:55 +02001137 {
1138 clear_tv(&tv);
Bram Moolenaard6ef5f92018-07-13 22:08:23 +02001139 return 0;
Bram Moolenaare49b8e82020-07-01 13:52:55 +02001140 }
Bram Moolenaar1dced572012-04-05 16:54:08 +02001141 if (dict_add(d, di) == FAIL)
1142 {
Bram Moolenaard6ef5f92018-07-13 22:08:23 +02001143 vim_free(di);
Bram Moolenaare49b8e82020-07-01 13:52:55 +02001144 clear_tv(&tv);
Bram Moolenaard6ef5f92018-07-13 22:08:23 +02001145 return 0;
Bram Moolenaar1dced572012-04-05 16:54:08 +02001146 }
1147 }
1148 else
1149 clear_tv(&di->di_tv);
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001150 if (lua_isnil(L, 3)) // remove?
Bram Moolenaar1dced572012-04-05 16:54:08 +02001151 {
1152 hashitem_T *hi = hash_find(&d->dv_hashtab, di->di_key);
Bram Moolenaaref2c3252022-11-25 16:31:51 +00001153 hash_remove(&d->dv_hashtab, hi, "Lua new index");
Bram Moolenaar1dced572012-04-05 16:54:08 +02001154 dictitem_free(di);
1155 }
Bram Moolenaarca06da92018-07-01 15:12:05 +02001156 else
Bram Moolenaare49b8e82020-07-01 13:52:55 +02001157 di->di_tv = tv;
Bram Moolenaar1dced572012-04-05 16:54:08 +02001158 return 0;
1159}
1160
1161static const luaL_Reg luaV_Dict_mt[] = {
1162 {"__tostring", luaV_dict_tostring},
Bram Moolenaar1dced572012-04-05 16:54:08 +02001163 {"__len", luaV_dict_len},
1164 {"__call", luaV_dict_call},
1165 {"__index", luaV_dict_index},
1166 {"__newindex", luaV_dict_newindex},
1167 {NULL, NULL}
1168};
1169
1170
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001171// ======= Blob type =======
Bram Moolenaarb7828692019-03-23 13:57:02 +01001172
1173 static luaV_Blob *
1174luaV_newblob(lua_State *L, blob_T *blo)
1175{
1176 luaV_Blob *b = (luaV_Blob *) lua_newuserdata(L, sizeof(luaV_Blob));
1177 *b = blo;
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001178 blo->bv_refcount++; // reference in Lua
1179 luaV_setudata(L, blo); // cache[blo] = udata
Bram Moolenaarb7828692019-03-23 13:57:02 +01001180 luaV_getfield(L, LUAVIM_BLOB);
1181 lua_setmetatable(L, -2);
1182 return b;
1183}
1184
1185luaV_pushtype(blob_T, blob, luaV_Blob)
1186luaV_type_tostring(blob, LUAVIM_BLOB)
1187
1188 static int
1189luaV_blob_gc(lua_State *L)
1190{
1191 blob_T *b = luaV_unbox(L, luaV_Blob, 1);
1192 blob_unref(b);
1193 return 0;
1194}
1195
1196 static int
1197luaV_blob_len(lua_State *L)
1198{
1199 blob_T *b = luaV_unbox(L, luaV_Blob, 1);
1200 lua_pushinteger(L, (int) blob_len(b));
1201 return 1;
1202}
1203
1204 static int
1205luaV_blob_index(lua_State *L)
1206{
1207 blob_T *b = luaV_unbox(L, luaV_Blob, 1);
1208 if (lua_isnumber(L, 2))
1209 {
1210 int idx = luaL_checkinteger(L, 2);
1211 if (idx < blob_len(b))
1212 lua_pushnumber(L, (lua_Number) blob_get(b, idx));
1213 else
1214 lua_pushnil(L);
1215 }
1216 else if (lua_isstring(L, 2))
1217 {
1218 const char *s = lua_tostring(L, 2);
1219 if (strncmp(s, "add", 3) == 0)
1220 {
1221 lua_getmetatable(L, 1);
1222 lua_getfield(L, -1, s);
1223 }
1224 else
1225 lua_pushnil(L);
1226 }
1227 else
1228 lua_pushnil(L);
1229 return 1;
1230}
1231
1232 static int
1233luaV_blob_newindex(lua_State *L)
1234{
1235 blob_T *b = luaV_unbox(L, luaV_Blob, 1);
1236 if (b->bv_lock)
1237 luaL_error(L, "blob is locked");
1238 if (lua_isnumber(L, 2))
1239 {
1240 long len = blob_len(b);
1241 int idx = luaL_checkinteger(L, 2);
1242 int val = luaL_checkinteger(L, 3);
1243 if (idx < len || (idx == len && ga_grow(&b->bv_ga, 1) == OK))
1244 {
1245 blob_set(b, idx, (char_u) val);
1246 if (idx == len)
1247 ++b->bv_ga.ga_len;
1248 }
1249 else
1250 luaL_error(L, "index out of range");
1251 }
1252 return 0;
1253}
1254
1255 static int
1256luaV_blob_add(lua_State *L)
1257{
1258 luaV_Blob *blo = luaV_checkudata(L, 1, LUAVIM_BLOB);
1259 blob_T *b = (blob_T *) luaV_checkcache(L, (void *) *blo);
1260 if (b->bv_lock)
1261 luaL_error(L, "blob is locked");
1262 lua_settop(L, 2);
1263 if (!lua_isstring(L, 2))
1264 luaL_error(L, "string expected, got %s", luaL_typename(L, 2));
1265 else
1266 {
1267 size_t i, l = 0;
1268 const char *s = lua_tolstring(L, 2, &l);
1269
Bram Moolenaar5f1d3ae2020-02-11 22:37:35 +01001270 if (ga_grow(&b->bv_ga, (int)l) == OK)
Bram Moolenaar6fb5c972019-03-26 21:44:20 +01001271 for (i = 0; i < l; ++i)
1272 ga_append(&b->bv_ga, s[i]);
Bram Moolenaarb7828692019-03-23 13:57:02 +01001273 }
1274 lua_settop(L, 1);
1275 return 1;
1276}
1277
1278static const luaL_Reg luaV_Blob_mt[] = {
1279 {"__tostring", luaV_blob_tostring},
1280 {"__gc", luaV_blob_gc},
1281 {"__len", luaV_blob_len},
1282 {"__index", luaV_blob_index},
1283 {"__newindex", luaV_blob_newindex},
1284 {"add", luaV_blob_add},
1285 {NULL, NULL}
1286};
1287
1288
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001289// ======= Funcref type =======
Bram Moolenaarca06da92018-07-01 15:12:05 +02001290
1291 static luaV_Funcref *
1292luaV_newfuncref(lua_State *L, char_u *name)
1293{
1294 luaV_Funcref *f = (luaV_Funcref *)lua_newuserdata(L, sizeof(luaV_Funcref));
1295
1296 if (name != NULL)
1297 {
Bram Moolenaar4eefe472019-03-19 21:59:19 +01001298 func_ref(name);
1299 f->name = vim_strsave(name);
Bram Moolenaarca06da92018-07-01 15:12:05 +02001300 }
Bram Moolenaarca06da92018-07-01 15:12:05 +02001301 f->self = NULL;
1302 luaV_getfield(L, LUAVIM_FUNCREF);
1303 lua_setmetatable(L, -2);
1304 return f;
1305}
1306
1307 static luaV_Funcref *
Bram Moolenaar4eefe472019-03-19 21:59:19 +01001308luaV_pushfuncref(lua_State *L, char_u *name)
Bram Moolenaarca06da92018-07-01 15:12:05 +02001309{
Bram Moolenaar4eefe472019-03-19 21:59:19 +01001310 return luaV_newfuncref(L, name);
Bram Moolenaarca06da92018-07-01 15:12:05 +02001311}
1312
1313
1314luaV_type_tostring(funcref, LUAVIM_FUNCREF)
1315
1316 static int
1317luaV_funcref_gc(lua_State *L)
1318{
1319 luaV_Funcref *f = (luaV_Funcref *) lua_touserdata(L, 1);
1320
Bram Moolenaar4eefe472019-03-19 21:59:19 +01001321 func_unref(f->name);
1322 vim_free(f->name);
1323 // NOTE: Don't call "dict_unref(f->self)", because the dict of "f->self"
1324 // will be (or has been already) freed by Vim's garbage collection.
Bram Moolenaarca06da92018-07-01 15:12:05 +02001325 return 0;
1326}
1327
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001328// equivalent to string(funcref)
Bram Moolenaarca06da92018-07-01 15:12:05 +02001329 static int
1330luaV_funcref_len(lua_State *L)
1331{
1332 luaV_Funcref *f = (luaV_Funcref *) lua_touserdata(L, 1);
1333
Bram Moolenaar4eefe472019-03-19 21:59:19 +01001334 lua_pushstring(L, (const char *) f->name);
Bram Moolenaarca06da92018-07-01 15:12:05 +02001335 return 1;
1336}
1337
1338 static int
1339luaV_funcref_call(lua_State *L)
1340{
1341 luaV_Funcref *f = (luaV_Funcref *) lua_touserdata(L, 1);
Bram Moolenaar4eefe472019-03-19 21:59:19 +01001342 int i, n = lua_gettop(L) - 1; // #args
1343 int status = FAIL;
1344 typval_T args;
1345 typval_T rettv;
Bram Moolenaarca06da92018-07-01 15:12:05 +02001346
Bram Moolenaar4eefe472019-03-19 21:59:19 +01001347 args.v_type = VAR_LIST;
1348 args.vval.v_list = list_alloc();
1349 rettv.v_type = VAR_UNKNOWN; // as in clear_tv
1350 if (args.vval.v_list != NULL)
Bram Moolenaard6ef5f92018-07-13 22:08:23 +02001351 {
Bram Moolenaar4eefe472019-03-19 21:59:19 +01001352 typval_T v;
1353
Bram Moolenaar17413672018-07-14 20:49:42 +02001354 for (i = 0; i < n; i++)
1355 {
Bram Moolenaard6ef5f92018-07-13 22:08:23 +02001356 luaV_checktypval(L, i + 2, &v, "calling funcref");
Bram Moolenaar4eefe472019-03-19 21:59:19 +01001357 list_append_tv(args.vval.v_list, &v);
Bram Moolenaar713bf9e92019-03-16 16:38:41 +01001358 clear_tv(&v);
Bram Moolenaard6ef5f92018-07-13 22:08:23 +02001359 }
Bram Moolenaar4eefe472019-03-19 21:59:19 +01001360 status = func_call(f->name, &args, NULL, f->self, &rettv);
Bram Moolenaard6ef5f92018-07-13 22:08:23 +02001361 if (status == OK)
1362 luaV_pushtypval(L, &rettv);
Bram Moolenaar4eefe472019-03-19 21:59:19 +01001363 clear_tv(&args);
Bram Moolenaard6ef5f92018-07-13 22:08:23 +02001364 clear_tv(&rettv);
Bram Moolenaarca06da92018-07-01 15:12:05 +02001365 }
Bram Moolenaarca06da92018-07-01 15:12:05 +02001366 if (status != OK)
1367 luaL_error(L, "cannot call funcref");
1368 return 1;
1369}
1370
1371static const luaL_Reg luaV_Funcref_mt[] = {
1372 {"__tostring", luaV_funcref_tostring},
1373 {"__gc", luaV_funcref_gc},
1374 {"__len", luaV_funcref_len},
1375 {"__call", luaV_funcref_call},
1376 {NULL, NULL}
1377};
1378
1379
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001380// ======= Buffer type =======
Bram Moolenaar1dced572012-04-05 16:54:08 +02001381
1382luaV_newtype(buf_T, buffer, luaV_Buffer, LUAVIM_BUFFER)
1383luaV_pushtype(buf_T, buffer, luaV_Buffer)
1384luaV_type_tostring(buffer, LUAVIM_BUFFER)
1385
1386 static int
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001387luaV_buffer_len(lua_State *L)
1388{
Bram Moolenaar1dced572012-04-05 16:54:08 +02001389 buf_T *b = (buf_T *) luaV_checkvalid(L, luaV_Buffer, 1);
1390 lua_pushinteger(L, b->b_ml.ml_line_count);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001391 return 1;
1392}
1393
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001394 static int
1395luaV_buffer_call(lua_State *L)
1396{
Bram Moolenaar1dced572012-04-05 16:54:08 +02001397 buf_T *b = (buf_T *) luaV_checkvalid(L, luaV_Buffer, 1);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001398 lua_settop(L, 1);
Bram Moolenaar1dced572012-04-05 16:54:08 +02001399 set_curbuf(b, DOBUF_SPLIT);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001400 return 1;
1401}
1402
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001403 static int
1404luaV_buffer_index(lua_State *L)
1405{
Bram Moolenaar1dced572012-04-05 16:54:08 +02001406 buf_T *b = (buf_T *) luaV_checkvalid(L, luaV_Buffer, 1);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001407 linenr_T n = (linenr_T) lua_tointeger(L, 2);
Bram Moolenaar1dced572012-04-05 16:54:08 +02001408 if (n > 0 && n <= b->b_ml.ml_line_count)
1409 luaV_pushline(L, b, n);
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001410 else if (lua_isstring(L, 2))
1411 {
1412 const char *s = lua_tostring(L, 2);
1413 if (strncmp(s, "name", 4) == 0)
Bram Moolenaarfe08df42018-07-07 23:07:41 +02001414 lua_pushstring(L, (b->b_sfname == NULL)
1415 ? "" : (char *) b->b_sfname);
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001416 else if (strncmp(s, "fname", 5) == 0)
Bram Moolenaarfe08df42018-07-07 23:07:41 +02001417 lua_pushstring(L, (b->b_ffname == NULL)
1418 ? "" : (char *) b->b_ffname);
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001419 else if (strncmp(s, "number", 6) == 0)
Bram Moolenaar1dced572012-04-05 16:54:08 +02001420 lua_pushinteger(L, b->b_fnum);
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001421 // methods
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001422 else if (strncmp(s, "insert", 6) == 0
1423 || strncmp(s, "next", 4) == 0
1424 || strncmp(s, "previous", 8) == 0
1425 || strncmp(s, "isvalid", 7) == 0)
1426 {
1427 lua_getmetatable(L, 1);
1428 lua_getfield(L, -1, s);
1429 }
1430 else
1431 lua_pushnil(L);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001432 }
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001433 else
1434 lua_pushnil(L);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001435 return 1;
1436}
1437
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001438 static int
1439luaV_buffer_newindex(lua_State *L)
1440{
Bram Moolenaar1dced572012-04-05 16:54:08 +02001441 buf_T *b = (buf_T *) luaV_checkvalid(L, luaV_Buffer, 1);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001442 linenr_T n = (linenr_T) luaL_checkinteger(L, 2);
1443#ifdef HAVE_SANDBOX
1444 luaV_checksandbox(L);
1445#endif
Bram Moolenaar1dced572012-04-05 16:54:08 +02001446 if (n < 1 || n > b->b_ml.ml_line_count)
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001447 luaL_error(L, "invalid line number");
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001448 if (lua_isnil(L, 3)) // delete line
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001449 {
1450 buf_T *buf = curbuf;
Bram Moolenaar1dced572012-04-05 16:54:08 +02001451 curbuf = b;
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001452 if (u_savedel(n, 1L) == FAIL)
1453 {
1454 curbuf = buf;
1455 luaL_error(L, "cannot save undo information");
1456 }
Bram Moolenaarca70c072020-05-30 20:30:46 +02001457 else if (ml_delete(n) == FAIL)
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001458 {
1459 curbuf = buf;
1460 luaL_error(L, "cannot delete line");
1461 }
Bram Moolenaarca06da92018-07-01 15:12:05 +02001462 else
1463 {
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001464 deleted_lines_mark(n, 1L);
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001465 if (b == curwin->w_buffer) // fix cursor in current window?
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001466 {
1467 if (curwin->w_cursor.lnum >= n)
1468 {
1469 if (curwin->w_cursor.lnum > n)
1470 {
1471 curwin->w_cursor.lnum -= 1;
1472 check_cursor_col();
1473 }
Bram Moolenaare49b8e82020-07-01 13:52:55 +02001474 else
1475 check_cursor();
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001476 changed_cline_bef_curs();
1477 }
1478 invalidate_botline();
1479 }
1480 }
1481 curbuf = buf;
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001482 }
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001483 else if (lua_isstring(L, 3)) // update line
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001484 {
1485 buf_T *buf = curbuf;
Bram Moolenaar1dced572012-04-05 16:54:08 +02001486 curbuf = b;
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001487 if (u_savesub(n) == FAIL)
1488 {
1489 curbuf = buf;
1490 luaL_error(L, "cannot save undo information");
1491 }
1492 else if (ml_replace(n, luaV_toline(L, 3), TRUE) == FAIL)
1493 {
1494 curbuf = buf;
1495 luaL_error(L, "cannot replace line");
1496 }
Bram Moolenaar113d9de2022-08-08 15:49:18 +01001497 else
1498 changed_bytes(n, 0);
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001499 curbuf = buf;
Bram Moolenaar1dced572012-04-05 16:54:08 +02001500 if (b == curwin->w_buffer)
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001501 check_cursor_col();
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001502 }
1503 else
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001504 luaL_error(L, "wrong argument to change line");
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001505 return 0;
1506}
1507
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001508 static int
1509luaV_buffer_insert(lua_State *L)
1510{
Bram Moolenaar1dced572012-04-05 16:54:08 +02001511 luaV_Buffer *lb = luaV_checkudata(L, 1, LUAVIM_BUFFER);
1512 buf_T *b = (buf_T *) luaV_checkcache(L, (void *) *lb);
1513 linenr_T last = b->b_ml.ml_line_count;
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001514 linenr_T n = (linenr_T) luaL_optinteger(L, 3, last);
1515 buf_T *buf;
1516 luaL_checktype(L, 2, LUA_TSTRING);
1517#ifdef HAVE_SANDBOX
1518 luaV_checksandbox(L);
1519#endif
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001520 // fix insertion line
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001521 if (n < 0) n = 0;
1522 if (n > last) n = last;
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001523 // insert
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001524 buf = curbuf;
Bram Moolenaar1dced572012-04-05 16:54:08 +02001525 curbuf = b;
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001526 if (u_save(n, n + 1) == FAIL)
1527 {
1528 curbuf = buf;
1529 luaL_error(L, "cannot save undo information");
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001530 }
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001531 else if (ml_append(n, luaV_toline(L, 2), 0, FALSE) == FAIL)
1532 {
1533 curbuf = buf;
1534 luaL_error(L, "cannot insert line");
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001535 }
1536 else
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001537 appended_lines_mark(n, 1L);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001538 curbuf = buf;
Bram Moolenaara4d158b2022-08-14 14:17:45 +01001539 update_screen(UPD_VALID);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001540 return 0;
1541}
1542
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001543 static int
1544luaV_buffer_next(lua_State *L)
1545{
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001546 luaV_Buffer *b = luaV_checkudata(L, 1, LUAVIM_BUFFER);
Bram Moolenaar1dced572012-04-05 16:54:08 +02001547 buf_T *buf = (buf_T *) luaV_checkcache(L, (void *) *b);
1548 luaV_pushbuffer(L, buf->b_next);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001549 return 1;
1550}
1551
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001552 static int
1553luaV_buffer_previous(lua_State *L)
1554{
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001555 luaV_Buffer *b = luaV_checkudata(L, 1, LUAVIM_BUFFER);
Bram Moolenaar1dced572012-04-05 16:54:08 +02001556 buf_T *buf = (buf_T *) luaV_checkcache(L, (void *) *b);
1557 luaV_pushbuffer(L, buf->b_prev);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001558 return 1;
1559}
1560
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001561 static int
1562luaV_buffer_isvalid(lua_State *L)
1563{
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001564 luaV_Buffer *b = luaV_checkudata(L, 1, LUAVIM_BUFFER);
Bram Moolenaar1dced572012-04-05 16:54:08 +02001565 luaV_getudata(L, *b);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001566 lua_pushboolean(L, !lua_isnil(L, -1));
1567 return 1;
1568}
1569
1570static const luaL_Reg luaV_Buffer_mt[] = {
1571 {"__tostring", luaV_buffer_tostring},
1572 {"__len", luaV_buffer_len},
1573 {"__call", luaV_buffer_call},
1574 {"__index", luaV_buffer_index},
1575 {"__newindex", luaV_buffer_newindex},
1576 {"insert", luaV_buffer_insert},
1577 {"next", luaV_buffer_next},
1578 {"previous", luaV_buffer_previous},
1579 {"isvalid", luaV_buffer_isvalid},
1580 {NULL, NULL}
1581};
1582
1583
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001584// ======= Window type =======
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001585
Bram Moolenaar1dced572012-04-05 16:54:08 +02001586luaV_newtype(win_T, window, luaV_Window, LUAVIM_WINDOW)
1587luaV_pushtype(win_T, window, luaV_Window)
1588luaV_type_tostring(window, LUAVIM_WINDOW)
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001589
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001590 static int
1591luaV_window_call(lua_State *L)
1592{
Bram Moolenaar1dced572012-04-05 16:54:08 +02001593 win_T *w = (win_T *) luaV_checkvalid(L, luaV_Window, 1);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001594 lua_settop(L, 1);
Bram Moolenaar1dced572012-04-05 16:54:08 +02001595 win_goto(w);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001596 return 1;
1597}
1598
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001599 static int
1600luaV_window_index(lua_State *L)
1601{
Bram Moolenaar1dced572012-04-05 16:54:08 +02001602 win_T *w = (win_T *) luaV_checkvalid(L, luaV_Window, 1);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001603 const char *s = luaL_checkstring(L, 2);
1604 if (strncmp(s, "buffer", 6) == 0)
Bram Moolenaar1dced572012-04-05 16:54:08 +02001605 luaV_pushbuffer(L, w->w_buffer);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001606 else if (strncmp(s, "line", 4) == 0)
Bram Moolenaar1dced572012-04-05 16:54:08 +02001607 lua_pushinteger(L, w->w_cursor.lnum);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001608 else if (strncmp(s, "col", 3) == 0)
Bram Moolenaar1dced572012-04-05 16:54:08 +02001609 lua_pushinteger(L, w->w_cursor.col + 1);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001610 else if (strncmp(s, "width", 5) == 0)
Bram Moolenaar02631462017-09-22 15:20:32 +02001611 lua_pushinteger(L, w->w_width);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001612 else if (strncmp(s, "height", 6) == 0)
Bram Moolenaar1dced572012-04-05 16:54:08 +02001613 lua_pushinteger(L, w->w_height);
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001614 // methods
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001615 else if (strncmp(s, "next", 4) == 0
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001616 || strncmp(s, "previous", 8) == 0
1617 || strncmp(s, "isvalid", 7) == 0)
1618 {
1619 lua_getmetatable(L, 1);
1620 lua_getfield(L, -1, s);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001621 }
1622 else
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001623 lua_pushnil(L);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001624 return 1;
1625}
1626
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001627 static int
Bram Moolenaar4eefe472019-03-19 21:59:19 +01001628luaV_window_newindex(lua_State *L)
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001629{
Bram Moolenaar1dced572012-04-05 16:54:08 +02001630 win_T *w = (win_T *) luaV_checkvalid(L, luaV_Window, 1);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001631 const char *s = luaL_checkstring(L, 2);
1632 int v = luaL_checkinteger(L, 3);
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001633 if (strncmp(s, "line", 4) == 0)
1634 {
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001635#ifdef HAVE_SANDBOX
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001636 luaV_checksandbox(L);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001637#endif
Bram Moolenaar1dced572012-04-05 16:54:08 +02001638 if (v < 1 || v > w->w_buffer->b_ml.ml_line_count)
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001639 luaL_error(L, "line out of range");
Bram Moolenaar1dced572012-04-05 16:54:08 +02001640 w->w_cursor.lnum = v;
Bram Moolenaara4d158b2022-08-14 14:17:45 +01001641 update_screen(UPD_VALID);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001642 }
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001643 else if (strncmp(s, "col", 3) == 0)
1644 {
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001645#ifdef HAVE_SANDBOX
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001646 luaV_checksandbox(L);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001647#endif
Bram Moolenaar1dced572012-04-05 16:54:08 +02001648 w->w_cursor.col = v - 1;
Bram Moolenaar53901442018-07-25 22:02:36 +02001649 w->w_set_curswant = TRUE;
Bram Moolenaara4d158b2022-08-14 14:17:45 +01001650 update_screen(UPD_VALID);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001651 }
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001652 else if (strncmp(s, "width", 5) == 0)
1653 {
1654 win_T *win = curwin;
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001655#ifdef FEAT_GUI
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001656 need_mouse_correct = TRUE;
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001657#endif
Bram Moolenaar1dced572012-04-05 16:54:08 +02001658 curwin = w;
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001659 win_setwidth(v);
1660 curwin = win;
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001661 }
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001662 else if (strncmp(s, "height", 6) == 0)
1663 {
1664 win_T *win = curwin;
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001665#ifdef FEAT_GUI
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001666 need_mouse_correct = TRUE;
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001667#endif
Bram Moolenaar1dced572012-04-05 16:54:08 +02001668 curwin = w;
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001669 win_setheight(v);
1670 curwin = win;
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001671 }
1672 else
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001673 luaL_error(L, "invalid window property: `%s'", s);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001674 return 0;
1675}
1676
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001677 static int
1678luaV_window_next(lua_State *L)
1679{
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001680 luaV_Window *w = luaV_checkudata(L, 1, LUAVIM_WINDOW);
Bram Moolenaar1dced572012-04-05 16:54:08 +02001681 win_T *win = (win_T *) luaV_checkcache(L, (void *) *w);
1682 luaV_pushwindow(L, win->w_next);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001683 return 1;
1684}
1685
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001686 static int
1687luaV_window_previous(lua_State *L)
1688{
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001689 luaV_Window *w = luaV_checkudata(L, 1, LUAVIM_WINDOW);
Bram Moolenaar1dced572012-04-05 16:54:08 +02001690 win_T *win = (win_T *) luaV_checkcache(L, (void *) *w);
1691 luaV_pushwindow(L, win->w_prev);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001692 return 1;
1693}
1694
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001695 static int
1696luaV_window_isvalid(lua_State *L)
1697{
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001698 luaV_Window *w = luaV_checkudata(L, 1, LUAVIM_WINDOW);
Bram Moolenaar1dced572012-04-05 16:54:08 +02001699 luaV_getudata(L, *w);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001700 lua_pushboolean(L, !lua_isnil(L, -1));
1701 return 1;
1702}
1703
1704static const luaL_Reg luaV_Window_mt[] = {
1705 {"__tostring", luaV_window_tostring},
1706 {"__call", luaV_window_call},
1707 {"__index", luaV_window_index},
1708 {"__newindex", luaV_window_newindex},
1709 {"next", luaV_window_next},
1710 {"previous", luaV_window_previous},
1711 {"isvalid", luaV_window_isvalid},
1712 {NULL, NULL}
1713};
1714
1715
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001716// ======= Vim module =======
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001717
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001718 static int
1719luaV_print(lua_State *L)
1720{
Yegappan Lakshmanan41114a22021-07-29 20:22:14 +02001721 int i, n = lua_gettop(L); // nargs
1722 const char *s;
1723 size_t l;
1724 garray_T msg_ga;
1725
1726 ga_init2(&msg_ga, 1, 128);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001727 lua_getglobal(L, "tostring");
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001728 for (i = 1; i <= n; i++)
1729 {
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001730 lua_pushvalue(L, -1); // tostring
1731 lua_pushvalue(L, i); // arg
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001732 lua_call(L, 1, 1);
1733 s = lua_tolstring(L, -1, &l);
1734 if (s == NULL)
1735 return luaL_error(L, "cannot convert to string");
Bram Moolenaar78e006b2021-07-28 15:07:01 +02001736 if (i > 1)
Yegappan Lakshmanan41114a22021-07-29 20:22:14 +02001737 ga_append(&msg_ga, ' '); // use space instead of tab
1738 ga_concat_len(&msg_ga, (char_u *)s, l);
Bram Moolenaar2a4bd002021-07-28 21:48:59 +02001739 lua_pop(L, 1);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001740 }
Yegappan Lakshmanan41114a22021-07-29 20:22:14 +02001741 // Replace any "\n" with "\0"
1742 for (i = 0; i < msg_ga.ga_len; i++)
1743 if (((char *)msg_ga.ga_data)[i] == '\n')
1744 ((char *)msg_ga.ga_data)[i] = '\0';
1745 lua_pushlstring(L, msg_ga.ga_data, msg_ga.ga_len);
Bram Moolenaarb98678a2019-10-19 15:18:44 +02001746 if (!got_int)
1747 luaV_msg(L);
Yegappan Lakshmanan41114a22021-07-29 20:22:14 +02001748
1749 ga_clear(&msg_ga);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001750 return 0;
1751}
1752
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001753 static int
Bram Moolenaar38e2b062011-09-21 17:15:39 +02001754luaV_debug(lua_State *L)
1755{
1756 lua_settop(L, 0);
1757 lua_getglobal(L, "vim");
1758 lua_getfield(L, -1, "eval");
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001759 lua_remove(L, -2); // vim.eval at position 1
Bram Moolenaar38e2b062011-09-21 17:15:39 +02001760 for (;;)
1761 {
1762 const char *input;
1763 size_t l;
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001764 lua_pushvalue(L, 1); // vim.eval
Bram Moolenaar38e2b062011-09-21 17:15:39 +02001765 lua_pushliteral(L, "input('lua_debug> ')");
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001766 lua_call(L, 1, 1); // return string
Bram Moolenaar38e2b062011-09-21 17:15:39 +02001767 input = lua_tolstring(L, -1, &l);
1768 if (l == 0 || strcmp(input, "cont") == 0)
1769 return 0;
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001770 msg_putchar('\n'); // avoid outputting on input line
Bram Moolenaar38e2b062011-09-21 17:15:39 +02001771 if (luaL_loadbuffer(L, input, l, "=(debug command)")
1772 || lua_pcall(L, 0, 0, 0))
1773 luaV_emsg(L);
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001774 lua_settop(L, 1); // remove eventual returns, but keep vim.eval
Bram Moolenaar38e2b062011-09-21 17:15:39 +02001775 }
1776}
1777
Yegappan Lakshmanan9dc4bef2021-08-04 21:12:52 +02001778 static dict_T *
1779luaV_get_var_scope(lua_State *L)
1780{
1781 const char *scope = luaL_checkstring(L, 1);
1782 dict_T *dict = NULL;
1783
1784 if (STRICMP((char *)scope, "g") == 0)
1785 dict = get_globvar_dict();
1786 else if (STRICMP((char *)scope, "v") == 0)
1787 dict = get_vimvar_dict();
1788 else if (STRICMP((char *)scope, "b") == 0)
1789 dict = curbuf->b_vars;
1790 else if (STRICMP((char *)scope, "w") == 0)
1791 dict = curwin->w_vars;
1792 else if (STRICMP((char *)scope, "t") == 0)
1793 dict = curtab->tp_vars;
1794 else
1795 {
1796 luaL_error(L, "invalid scope %s", scope);
1797 return NULL;
1798 }
1799
1800 return dict;
1801}
1802
1803 static int
1804luaV_setvar(lua_State *L)
1805{
1806 dict_T *dict;
1807 dictitem_T *di;
1808 size_t len;
1809 char *name;
1810 int del;
1811 char *error = NULL;
1812
1813 name = (char *)luaL_checklstring(L, 3, &len);
1814 del = (lua_gettop(L) < 4) || lua_isnil(L, 4);
1815
1816 dict = luaV_get_var_scope(L);
1817 if (dict == NULL)
1818 return 0;
1819
Yegappan Lakshmanan11328bc2021-08-06 21:34:38 +02001820 di = dict_find(dict, (char_u *)name, (int)len);
Yegappan Lakshmanan9dc4bef2021-08-04 21:12:52 +02001821 if (di != NULL)
1822 {
1823 if (di->di_flags & DI_FLAGS_RO)
1824 error = "variable is read-only";
1825 else if (di->di_flags & DI_FLAGS_LOCK)
1826 error = "variable is locked";
1827 else if (del && di->di_flags & DI_FLAGS_FIX)
1828 error = "variable is fixed";
1829 if (error != NULL)
1830 return luaL_error(L, error);
1831 }
1832 else if (dict->dv_lock)
1833 return luaL_error(L, "Dictionary is locked");
1834
1835 if (del)
1836 {
1837 // Delete the key
1838 if (di == NULL)
1839 // Doesn't exist, nothing to do
1840 return 0;
Bram Moolenaaref2c3252022-11-25 16:31:51 +00001841 // Delete the entry
1842 dictitem_remove(dict, di, "Lua delete variable");
Yegappan Lakshmanan9dc4bef2021-08-04 21:12:52 +02001843 }
1844 else
1845 {
1846 // Update the key
1847 typval_T tv;
1848
h-east965d2ed2021-10-04 21:51:57 +01001849 // Convert the lua value to a Vim script type in the temporary variable
Yegappan Lakshmanan9dc4bef2021-08-04 21:12:52 +02001850 lua_pushvalue(L, 4);
1851 if (luaV_totypval(L, -1, &tv) == FAIL)
1852 return luaL_error(L, "Couldn't convert lua value");
1853
1854 if (di == NULL)
1855 {
1856 // Need to create an entry
1857 di = dictitem_alloc((char_u *)name);
1858 if (di == NULL)
Bram Moolenaar1b6acf02021-08-05 16:47:08 +02001859 {
1860 clear_tv(&tv);
Yegappan Lakshmanan9dc4bef2021-08-04 21:12:52 +02001861 return 0;
Bram Moolenaar1b6acf02021-08-05 16:47:08 +02001862 }
Yegappan Lakshmanan9dc4bef2021-08-04 21:12:52 +02001863 // Update the value
1864 copy_tv(&tv, &di->di_tv);
Bram Moolenaar4a011592021-08-05 15:11:08 +02001865 if (dict_add(dict, di) == FAIL)
Bram Moolenaar1b6acf02021-08-05 16:47:08 +02001866 {
1867 dictitem_free(di);
1868 clear_tv(&tv);
Bram Moolenaar4a011592021-08-05 15:11:08 +02001869 return luaL_error(L, "Couldn't add to dictionary");
Bram Moolenaar1b6acf02021-08-05 16:47:08 +02001870 }
1871 }
1872 else
Yegappan Lakshmanan9dc4bef2021-08-04 21:12:52 +02001873 {
1874 // Clear the old value
1875 clear_tv(&di->di_tv);
1876 // Update the value
1877 copy_tv(&tv, &di->di_tv);
1878 }
1879
1880 // Clear the temporary variable
1881 clear_tv(&tv);
1882 }
1883
1884 return 0;
1885}
1886
1887 static int
1888luaV_getvar(lua_State *L)
1889{
1890 dict_T *dict = luaV_get_var_scope(L);
1891 size_t len;
1892 const char *name = luaL_checklstring(L, 3, &len);
Yegappan Lakshmanan11328bc2021-08-06 21:34:38 +02001893 dictitem_T *di = dict_find(dict, (char_u *)name, (int)len);
Yegappan Lakshmanan9dc4bef2021-08-04 21:12:52 +02001894
Yegappan Lakshmanan9dc4bef2021-08-04 21:12:52 +02001895 if (di == NULL)
1896 return 0; // nil
1897
1898 luaV_pushtypval(L, &di->di_tv);
1899 return 1;
1900}
1901
Bram Moolenaar38e2b062011-09-21 17:15:39 +02001902 static int
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001903luaV_command(lua_State *L)
1904{
Yegappan Lakshmanan11328bc2021-08-06 21:34:38 +02001905 char_u *s = vim_strsave((char_u *)luaL_checkstring(L, 1));
1906
1907 execute_cmds_from_string(s);
1908 vim_free(s);
Bram Moolenaara4d158b2022-08-14 14:17:45 +01001909 update_screen(UPD_VALID);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001910 return 0;
1911}
1912
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001913 static int
1914luaV_eval(lua_State *L)
1915{
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001916 typval_T *tv = eval_expr((char_u *) luaL_checkstring(L, 1), NULL);
Yegappan Lakshmanan11328bc2021-08-06 21:34:38 +02001917
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001918 if (tv == NULL) luaL_error(L, "invalid expression");
1919 luaV_pushtypval(L, tv);
Bram Moolenaarb3766472013-04-15 13:49:21 +02001920 free_tv(tv);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001921 return 1;
1922}
1923
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001924 static int
Bram Moolenaar0d2e4fc2010-07-18 12:35:47 +02001925luaV_beep(lua_State *L UNUSED)
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001926{
Bram Moolenaar165bc692015-07-21 17:53:25 +02001927 vim_beep(BO_LANG);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001928 return 0;
1929}
1930
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001931 static int
1932luaV_line(lua_State *L)
1933{
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001934 luaV_pushline(L, curbuf, curwin->w_cursor.lnum);
1935 return 1;
1936}
1937
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001938 static int
Bram Moolenaar1dced572012-04-05 16:54:08 +02001939luaV_list(lua_State *L)
1940{
Bram Moolenaarca06da92018-07-01 15:12:05 +02001941 list_T *l;
1942 int initarg = !lua_isnoneornil(L, 1);
1943
1944 if (initarg && lua_type(L, 1) != LUA_TTABLE)
1945 luaL_error(L, "table expected, got %s", luaL_typename(L, 1));
1946 l = list_alloc();
Bram Moolenaar1dced572012-04-05 16:54:08 +02001947 if (l == NULL)
1948 lua_pushnil(L);
1949 else
Bram Moolenaarca06da92018-07-01 15:12:05 +02001950 {
Bram Moolenaar1dced572012-04-05 16:54:08 +02001951 luaV_newlist(L, l);
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001952 if (initarg) // traverse table to init list
Bram Moolenaar17413672018-07-14 20:49:42 +02001953 {
Bram Moolenaarca06da92018-07-01 15:12:05 +02001954 int notnil, i = 0;
1955 typval_T v;
Bram Moolenaar17413672018-07-14 20:49:42 +02001956 do
1957 {
Bram Moolenaarca06da92018-07-01 15:12:05 +02001958 lua_rawgeti(L, 1, ++i);
1959 notnil = !lua_isnil(L, -1);
Bram Moolenaar17413672018-07-14 20:49:42 +02001960 if (notnil)
1961 {
Bram Moolenaarca06da92018-07-01 15:12:05 +02001962 luaV_checktypval(L, -1, &v, "vim.list");
1963 list_append_tv(l, &v);
Bram Moolenaar713bf9e92019-03-16 16:38:41 +01001964 clear_tv(&v);
Bram Moolenaarca06da92018-07-01 15:12:05 +02001965 }
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001966 lua_pop(L, 1); // value
Bram Moolenaarca06da92018-07-01 15:12:05 +02001967 } while (notnil);
1968 }
1969 }
Bram Moolenaar1dced572012-04-05 16:54:08 +02001970 return 1;
1971}
1972
1973 static int
1974luaV_dict(lua_State *L)
1975{
Bram Moolenaarca06da92018-07-01 15:12:05 +02001976 dict_T *d;
1977 int initarg = !lua_isnoneornil(L, 1);
1978
1979 if (initarg && lua_type(L, 1) != LUA_TTABLE)
1980 luaL_error(L, "table expected, got %s", luaL_typename(L, 1));
1981 d = dict_alloc();
Bram Moolenaar1dced572012-04-05 16:54:08 +02001982 if (d == NULL)
1983 lua_pushnil(L);
1984 else
Bram Moolenaarca06da92018-07-01 15:12:05 +02001985 {
Bram Moolenaar1dced572012-04-05 16:54:08 +02001986 luaV_newdict(L, d);
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001987 if (initarg) // traverse table to init dict
Bram Moolenaarca06da92018-07-01 15:12:05 +02001988 {
1989 lua_pushnil(L);
1990 while (lua_next(L, 1))
1991 {
1992 char_u *key;
1993 dictitem_T *di;
1994 typval_T v;
Bram Moolenaard6ef5f92018-07-13 22:08:23 +02001995
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001996 lua_pushvalue(L, -2); // dup key in case it's a number
Bram Moolenaarca06da92018-07-01 15:12:05 +02001997 key = (char_u *) lua_tostring(L, -1);
Bram Moolenaard6ef5f92018-07-13 22:08:23 +02001998 if (key == NULL)
1999 {
2000 lua_pushnil(L);
2001 return 1;
2002 }
2003 if (*key == NUL)
Bram Moolenaarca06da92018-07-01 15:12:05 +02002004 luaL_error(L, "table has empty key");
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002005 luaV_checktypval(L, -2, &v, "vim.dict"); // value
Bram Moolenaarca06da92018-07-01 15:12:05 +02002006 di = dictitem_alloc(key);
Bram Moolenaard6ef5f92018-07-13 22:08:23 +02002007 if (di == NULL || dict_add(d, di) == FAIL)
2008 {
Bram Moolenaarca06da92018-07-01 15:12:05 +02002009 vim_free(di);
2010 lua_pushnil(L);
2011 return 1;
2012 }
Bram Moolenaare49b8e82020-07-01 13:52:55 +02002013 di->di_tv = v;
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002014 lua_pop(L, 2); // key copy and value
Bram Moolenaarca06da92018-07-01 15:12:05 +02002015 }
2016 }
2017 }
2018 return 1;
2019}
2020
2021 static int
Bram Moolenaarb7828692019-03-23 13:57:02 +01002022luaV_blob(lua_State *L)
2023{
2024 blob_T *b;
2025 int initarg = !lua_isnoneornil(L, 1);
2026
2027 if (initarg && !lua_isstring(L, 1))
2028 luaL_error(L, "string expected, got %s", luaL_typename(L, 1));
2029 b = blob_alloc();
2030 if (b == NULL)
2031 lua_pushnil(L);
2032 else
2033 {
2034 luaV_newblob(L, b);
2035 if (initarg)
2036 {
2037 size_t i, l = 0;
2038 const char *s = lua_tolstring(L, 1, &l);
2039
Bram Moolenaar5f1d3ae2020-02-11 22:37:35 +01002040 if (ga_grow(&b->bv_ga, (int)l) == OK)
Bram Moolenaar6fb5c972019-03-26 21:44:20 +01002041 for (i = 0; i < l; ++i)
2042 ga_append(&b->bv_ga, s[i]);
Bram Moolenaarb7828692019-03-23 13:57:02 +01002043 }
2044 }
2045 return 1;
2046}
2047
2048 static int
Bram Moolenaarca06da92018-07-01 15:12:05 +02002049luaV_funcref(lua_State *L)
2050{
2051 const char *name = luaL_checkstring(L, 1);
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002052 // note: not checking if function exists (needs function_exists)
Bram Moolenaarca06da92018-07-01 15:12:05 +02002053 if (name == NULL || *name == NUL || VIM_ISDIGIT(*name))
2054 luaL_error(L, "invalid function name: %s", name);
2055 luaV_newfuncref(L, (char_u *) name);
Bram Moolenaar1dced572012-04-05 16:54:08 +02002056 return 1;
2057}
2058
2059 static int
Bram Moolenaar55d5c032010-07-17 23:52:29 +02002060luaV_buffer(lua_State *L)
2061{
Bram Moolenaar0ba04292010-07-14 23:23:17 +02002062 buf_T *buf;
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002063 if (lua_isstring(L, 1)) // get by number or name?
Bram Moolenaar55d5c032010-07-17 23:52:29 +02002064 {
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002065 if (lua_isnumber(L, 1)) // by number?
Bram Moolenaar55d5c032010-07-17 23:52:29 +02002066 {
2067 int n = lua_tointeger(L, 1);
Bram Moolenaar29323592016-07-24 22:04:11 +02002068 FOR_ALL_BUFFERS(buf)
Bram Moolenaar55d5c032010-07-17 23:52:29 +02002069 if (buf->b_fnum == n) break;
2070 }
Bram Moolenaarca06da92018-07-01 15:12:05 +02002071 else // by name
2072 {
Bram Moolenaar55d5c032010-07-17 23:52:29 +02002073 size_t l;
2074 const char *s = lua_tolstring(L, 1, &l);
Bram Moolenaar29323592016-07-24 22:04:11 +02002075 FOR_ALL_BUFFERS(buf)
Bram Moolenaar55d5c032010-07-17 23:52:29 +02002076 {
2077 if (buf->b_ffname == NULL || buf->b_sfname == NULL)
2078 {
2079 if (l == 0) break;
2080 }
Bram Moolenaar0d2e4fc2010-07-18 12:35:47 +02002081 else if (strncmp(s, (char *)buf->b_ffname, l) == 0
2082 || strncmp(s, (char *)buf->b_sfname, l) == 0)
Bram Moolenaar55d5c032010-07-17 23:52:29 +02002083 break;
2084 }
2085 }
Bram Moolenaar0ba04292010-07-14 23:23:17 +02002086 }
Bram Moolenaar1dced572012-04-05 16:54:08 +02002087 else
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002088 buf = (lua_toboolean(L, 1)) ? firstbuf : curbuf; // first buffer?
Bram Moolenaar1dced572012-04-05 16:54:08 +02002089 luaV_pushbuffer(L, buf);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02002090 return 1;
2091}
2092
Bram Moolenaar55d5c032010-07-17 23:52:29 +02002093 static int
2094luaV_window(lua_State *L)
2095{
Bram Moolenaar0ba04292010-07-14 23:23:17 +02002096 win_T *win;
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002097 if (lua_isnumber(L, 1)) // get by number?
Bram Moolenaar55d5c032010-07-17 23:52:29 +02002098 {
2099 int n = lua_tointeger(L, 1);
2100 for (win = firstwin; win != NULL; win = win->w_next, n--)
2101 if (n == 1) break;
Bram Moolenaar0ba04292010-07-14 23:23:17 +02002102 }
Bram Moolenaar1dced572012-04-05 16:54:08 +02002103 else
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002104 win = (lua_toboolean(L, 1)) ? firstwin : curwin; // first window?
Bram Moolenaar1dced572012-04-05 16:54:08 +02002105 luaV_pushwindow(L, win);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02002106 return 1;
2107}
2108
Bram Moolenaar55d5c032010-07-17 23:52:29 +02002109 static int
2110luaV_open(lua_State *L)
2111{
Bram Moolenaar0ba04292010-07-14 23:23:17 +02002112 char_u *s = NULL;
2113#ifdef HAVE_SANDBOX
2114 luaV_checksandbox(L);
2115#endif
2116 if (lua_isstring(L, 1)) s = (char_u *) lua_tostring(L, 1);
Bram Moolenaarfa263a52011-12-08 16:00:16 +01002117 luaV_pushbuffer(L, buflist_new(s, NULL, 1L, BLN_LISTED));
Bram Moolenaar0ba04292010-07-14 23:23:17 +02002118 return 1;
2119}
2120
Bram Moolenaar55d5c032010-07-17 23:52:29 +02002121 static int
Bram Moolenaar1dced572012-04-05 16:54:08 +02002122luaV_type(lua_State *L)
Bram Moolenaar55d5c032010-07-17 23:52:29 +02002123{
Bram Moolenaar1dced572012-04-05 16:54:08 +02002124 luaL_checkany(L, 1);
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002125 if (lua_type(L, 1) == LUA_TUSERDATA) // check vim udata?
Bram Moolenaar55d5c032010-07-17 23:52:29 +02002126 {
Bram Moolenaar1dced572012-04-05 16:54:08 +02002127 lua_settop(L, 1);
2128 if (lua_getmetatable(L, 1))
2129 {
2130 luaV_getfield(L, LUAVIM_LIST);
2131 if (lua_rawequal(L, -1, 2))
2132 {
2133 lua_pushstring(L, "list");
2134 return 1;
2135 }
2136 luaV_getfield(L, LUAVIM_DICT);
2137 if (lua_rawequal(L, -1, 2))
2138 {
2139 lua_pushstring(L, "dict");
2140 return 1;
2141 }
Bram Moolenaarb7828692019-03-23 13:57:02 +01002142 luaV_getfield(L, LUAVIM_BLOB);
2143 if (lua_rawequal(L, -1, 2))
2144 {
2145 lua_pushstring(L, "blob");
2146 return 1;
2147 }
Bram Moolenaarca06da92018-07-01 15:12:05 +02002148 luaV_getfield(L, LUAVIM_FUNCREF);
2149 if (lua_rawequal(L, -1, 2))
2150 {
2151 lua_pushstring(L, "funcref");
2152 return 1;
2153 }
Bram Moolenaar1dced572012-04-05 16:54:08 +02002154 luaV_getfield(L, LUAVIM_BUFFER);
2155 if (lua_rawequal(L, -1, 2))
2156 {
2157 lua_pushstring(L, "buffer");
2158 return 1;
2159 }
2160 luaV_getfield(L, LUAVIM_WINDOW);
2161 if (lua_rawequal(L, -1, 2))
2162 {
2163 lua_pushstring(L, "window");
2164 return 1;
2165 }
2166 }
Bram Moolenaar0ba04292010-07-14 23:23:17 +02002167 }
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002168 lua_pushstring(L, luaL_typename(L, 1)); // fallback
Bram Moolenaar1dced572012-04-05 16:54:08 +02002169 return 1;
Bram Moolenaar0ba04292010-07-14 23:23:17 +02002170}
2171
Bram Moolenaareb04f082020-05-17 14:32:35 +02002172 static int
2173luaV_call(lua_State *L)
2174{
2175 int argc = lua_gettop(L) - 1;
2176 size_t funcname_len;
2177 char_u *funcname;
2178 char *error = NULL;
2179 typval_T rettv;
2180 typval_T argv[MAX_FUNC_ARGS + 1];
2181 int i = 0;
2182
2183 if (argc > MAX_FUNC_ARGS)
2184 return luaL_error(L, "Function called with too many arguments");
2185
2186 funcname = (char_u *)luaL_checklstring(L, 1, &funcname_len);
2187
2188 for (; i < argc; i++)
2189 {
2190 if (luaV_totypval(L, i + 2, &argv[i]) == FAIL)
2191 {
2192 error = "lua: cannot convert value";
2193 goto free_vim_args;
2194 }
2195 }
2196
2197 argv[argc].v_type = VAR_UNKNOWN;
2198
2199 if (call_vim_function(funcname, argc, argv, &rettv) == FAIL)
2200 {
2201 error = "lua: call_vim_function failed";
2202 goto free_vim_args;
2203 }
2204
2205 luaV_pushtypval(L, &rettv);
2206 clear_tv(&rettv);
2207
2208free_vim_args:
2209 while (i > 0)
2210 clear_tv(&argv[--i]);
2211
2212 if (error == NULL)
2213 return 1;
2214 else
2215 return luaL_error(L, error);
2216}
2217
Yegappan Lakshmanan11328bc2021-08-06 21:34:38 +02002218/*
2219 * Return the Vim version as a Lua table
2220 */
2221 static int
2222luaV_version(lua_State *L)
2223{
2224 lua_newtable(L);
2225 lua_pushstring(L, "major");
2226 lua_pushinteger(L, VIM_VERSION_MAJOR);
2227 lua_settable(L, -3);
2228 lua_pushstring(L, "minor");
2229 lua_pushinteger(L, VIM_VERSION_MINOR);
2230 lua_settable(L, -3);
2231 lua_pushstring(L, "patch");
2232 lua_pushinteger(L, highest_patch());
2233 lua_settable(L, -3);
2234 return 1;
2235}
2236
Bram Moolenaar0ba04292010-07-14 23:23:17 +02002237static const luaL_Reg luaV_module[] = {
2238 {"command", luaV_command},
2239 {"eval", luaV_eval},
2240 {"beep", luaV_beep},
2241 {"line", luaV_line},
Bram Moolenaar1dced572012-04-05 16:54:08 +02002242 {"list", luaV_list},
2243 {"dict", luaV_dict},
Bram Moolenaarb7828692019-03-23 13:57:02 +01002244 {"blob", luaV_blob},
Bram Moolenaarca06da92018-07-01 15:12:05 +02002245 {"funcref", luaV_funcref},
Bram Moolenaar0ba04292010-07-14 23:23:17 +02002246 {"buffer", luaV_buffer},
2247 {"window", luaV_window},
2248 {"open", luaV_open},
Bram Moolenaar1dced572012-04-05 16:54:08 +02002249 {"type", luaV_type},
Bram Moolenaareb04f082020-05-17 14:32:35 +02002250 {"call", luaV_call},
Yegappan Lakshmanan9dc4bef2021-08-04 21:12:52 +02002251 {"_getvar", luaV_getvar},
2252 {"_setvar", luaV_setvar},
Yegappan Lakshmanan11328bc2021-08-06 21:34:38 +02002253 {"version", luaV_version},
Bram Moolenaar125ed272021-04-07 20:11:12 +02002254 {"lua_version", NULL},
Bram Moolenaar0ba04292010-07-14 23:23:17 +02002255 {NULL, NULL}
2256};
2257
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002258/*
2259 * for freeing list, dict, buffer and window objects; lightuserdata as arg
2260 */
Bram Moolenaar1dced572012-04-05 16:54:08 +02002261 static int
2262luaV_free(lua_State *L)
2263{
2264 lua_pushnil(L);
2265 luaV_setudata(L, lua_touserdata(L, 1));
2266 return 0;
2267}
2268
2269 static int
Bram Moolenaar4eefe472019-03-19 21:59:19 +01002270luaV_luaeval(lua_State *L)
Bram Moolenaar1dced572012-04-05 16:54:08 +02002271{
2272 luaL_Buffer b;
2273 size_t l;
2274 const char *str = lua_tolstring(L, 1, &l);
2275 typval_T *arg = (typval_T *) lua_touserdata(L, 2);
2276 typval_T *rettv = (typval_T *) lua_touserdata(L, 3);
2277 luaL_buffinit(L, &b);
2278 luaL_addlstring(&b, LUAVIM_EVALHEADER, sizeof(LUAVIM_EVALHEADER) - 1);
2279 luaL_addlstring(&b, str, l);
2280 luaL_pushresult(&b);
2281 str = lua_tolstring(L, -1, &l);
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002282 if (luaL_loadbuffer(L, str, l, LUAVIM_EVALNAME)) // compile error?
Bram Moolenaar1dced572012-04-05 16:54:08 +02002283 {
2284 luaV_emsg(L);
2285 return 0;
2286 }
2287 luaV_pushtypval(L, arg);
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002288 if (lua_pcall(L, 1, 1, 0)) // running error?
Bram Moolenaar1dced572012-04-05 16:54:08 +02002289 {
2290 luaV_emsg(L);
2291 return 0;
2292 }
Bram Moolenaarca06da92018-07-01 15:12:05 +02002293 if (luaV_totypval(L, -1, rettv) == FAIL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002294 emsg("luaeval: cannot convert value");
Bram Moolenaarf554a322015-02-04 23:08:01 +01002295 return 0;
Bram Moolenaar1dced572012-04-05 16:54:08 +02002296}
2297
2298 static int
Bram Moolenaar4eefe472019-03-19 21:59:19 +01002299luaV_setref(lua_State *L)
Bram Moolenaar1dced572012-04-05 16:54:08 +02002300{
Bram Moolenaar4eefe472019-03-19 21:59:19 +01002301 int copyID = lua_tointeger(L, 1);
2302 int abort = FALSE;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01002303
Bram Moolenaar1dced572012-04-05 16:54:08 +02002304 luaV_getfield(L, LUAVIM_LIST);
2305 luaV_getfield(L, LUAVIM_DICT);
Bram Moolenaar4eefe472019-03-19 21:59:19 +01002306 luaV_getfield(L, LUAVIM_FUNCREF);
Bram Moolenaar1dced572012-04-05 16:54:08 +02002307 lua_pushnil(L);
Bram Moolenaar4eefe472019-03-19 21:59:19 +01002308 // traverse cache table
Bram Moolenaarb84634d2015-02-04 22:02:37 +01002309 while (!abort && lua_next(L, lua_upvalueindex(1)) != 0)
Bram Moolenaar1dced572012-04-05 16:54:08 +02002310 {
2311 lua_getmetatable(L, -1);
Bram Moolenaar4eefe472019-03-19 21:59:19 +01002312 if (lua_rawequal(L, -1, 2)) // list?
Bram Moolenaar1dced572012-04-05 16:54:08 +02002313 {
Bram Moolenaar4eefe472019-03-19 21:59:19 +01002314 list_T *l = (list_T *)lua_touserdata(L, 5); // key
2315
Bram Moolenaar7be3ab22019-06-23 01:46:15 +02002316 abort = set_ref_in_list(l, copyID);
Bram Moolenaar1dced572012-04-05 16:54:08 +02002317 }
Bram Moolenaar4eefe472019-03-19 21:59:19 +01002318 else if (lua_rawequal(L, -1, 3)) // dict?
Bram Moolenaar1dced572012-04-05 16:54:08 +02002319 {
Bram Moolenaar4eefe472019-03-19 21:59:19 +01002320 dict_T *d = (dict_T *)lua_touserdata(L, 5); // key
2321
Bram Moolenaar7be3ab22019-06-23 01:46:15 +02002322 abort = set_ref_in_dict(d, copyID);
Bram Moolenaar1dced572012-04-05 16:54:08 +02002323 }
Bram Moolenaar4eefe472019-03-19 21:59:19 +01002324 else if (lua_rawequal(L, -1, 4)) // funcref?
2325 {
2326 luaV_Funcref *f = (luaV_Funcref *)lua_touserdata(L, 5); // key
2327
Bram Moolenaar7be3ab22019-06-23 01:46:15 +02002328 abort = set_ref_in_dict(f->self, copyID);
Bram Moolenaar4eefe472019-03-19 21:59:19 +01002329 }
2330 lua_pop(L, 2); // metatable and value
Bram Moolenaar1dced572012-04-05 16:54:08 +02002331 }
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01002332 lua_pushinteger(L, abort);
Bram Moolenaarf554a322015-02-04 23:08:01 +01002333 return 1;
Bram Moolenaar1dced572012-04-05 16:54:08 +02002334}
2335
Bram Moolenaar125ed272021-04-07 20:11:12 +02002336 static int
2337luaV_pushversion(lua_State *L)
2338{
2339 int major = 0;
2340 int minor = 0;
2341 int patch = 0;
2342 char s[16];
2343
2344 sscanf(LUAVIM_VERSION, "Lua %d.%d.%d", &major, &minor, &patch);
2345 vim_snprintf(s, sizeof(s), "%d.%d.%d", major, minor, patch);
2346 lua_pushstring(L, s);
2347 return 0;
2348}
2349
Bram Moolenaareb04f082020-05-17 14:32:35 +02002350#define LUA_VIM_FN_CODE \
Bram Moolenaar788fbb42020-05-31 14:08:12 +02002351 "vim.fn = setmetatable({}, {\n"\
2352 " __index = function (t, key)\n"\
2353 " local function _fn(...)\n"\
2354 " return vim.call(key, ...)\n"\
2355 " end\n"\
2356 " t[key] = _fn\n"\
2357 " return _fn\n"\
2358 " end\n"\
2359 " })"
2360
2361#define LUA_VIM_UPDATE_PACKAGE_PATHS \
2362 "local last_vim_paths = {}\n"\
2363 "vim._update_package_paths = function ()\n"\
2364 " local cur_vim_paths = {}\n"\
2365 " local function split(s, delimiter)\n"\
2366 " result = {}\n"\
2367 " for match in (s..delimiter):gmatch(\"(.-)\"..delimiter) do\n"\
2368 " table.insert(result, match)\n"\
2369 " end\n"\
2370 " return result\n"\
2371 " end\n"\
2372 " local rtps = split(vim.eval('&runtimepath'), ',')\n"\
2373 " local sep = package.config:sub(1, 1)\n"\
2374 " for _, key in ipairs({'path', 'cpath'}) do\n"\
2375 " local orig_str = package[key] .. ';'\n"\
2376 " local pathtrails_ordered = {}\n"\
2377 " -- Note: ignores trailing item without trailing `;`. Not using something\n"\
2378 " -- simpler in order to preserve empty items (stand for default path).\n"\
2379 " local orig = {}\n"\
2380 " for s in orig_str:gmatch('[^;]*;') do\n"\
2381 " s = s:sub(1, -2) -- Strip trailing semicolon\n"\
2382 " orig[#orig + 1] = s\n"\
2383 " end\n"\
2384 " if key == 'path' then\n"\
2385 " -- /?.lua and /?/init.lua\n"\
2386 " pathtrails_ordered = {sep .. '?.lua', sep .. '?' .. sep .. 'init.lua'}\n"\
2387 " else\n"\
2388 " local pathtrails = {}\n"\
2389 " for _, s in ipairs(orig) do\n"\
2390 " -- Find out path patterns. pathtrail should contain something like\n"\
2391 " -- /?.so, \?.dll. This allows not to bother determining what correct\n"\
2392 " -- suffixes are.\n"\
2393 " local pathtrail = s:match('[/\\\\][^/\\\\]*%?.*$')\n"\
2394 " if pathtrail and not pathtrails[pathtrail] then\n"\
2395 " pathtrails[pathtrail] = true\n"\
2396 " pathtrails_ordered[#pathtrails_ordered + 1] = pathtrail\n"\
2397 " end\n"\
2398 " end\n"\
2399 " end\n"\
2400 " local new = {}\n"\
2401 " for _, rtp in ipairs(rtps) do\n"\
2402 " if not rtp:match(';') then\n"\
2403 " for _, pathtrail in pairs(pathtrails_ordered) do\n"\
2404 " local new_path = rtp .. sep .. 'lua' .. pathtrail\n"\
2405 " -- Always keep paths from &runtimepath at the start:\n"\
2406 " -- append them here disregarding orig possibly containing one of them.\n"\
2407 " new[#new + 1] = new_path\n"\
2408 " cur_vim_paths[new_path] = true\n"\
2409 " end\n"\
2410 " end\n"\
2411 " end\n"\
2412 " for _, orig_path in ipairs(orig) do\n"\
2413 " -- Handle removing obsolete paths originating from &runtimepath: such\n"\
2414 " -- paths either belong to cur_nvim_paths and were already added above or\n"\
2415 " -- to last_nvim_paths and should not be added at all if corresponding\n"\
2416 " -- entry was removed from &runtimepath list.\n"\
2417 " if not (cur_vim_paths[orig_path] or last_vim_paths[orig_path]) then\n"\
2418 " new[#new + 1] = orig_path\n"\
2419 " end\n"\
2420 " end\n"\
2421 " package[key] = table.concat(new, ';')\n"\
2422 " end\n"\
2423 " last_vim_paths = cur_vim_paths\n"\
2424 "end"
Bram Moolenaareb04f082020-05-17 14:32:35 +02002425
Yegappan Lakshmanan9dc4bef2021-08-04 21:12:52 +02002426#define LUA_VIM_SETUP_VARIABLE_DICTS \
2427 "do\n"\
2428 " local function make_dict_accessor(scope)\n"\
2429 " local mt = {}\n"\
2430 " function mt:__newindex(k, v)\n"\
2431 " return vim._setvar(scope, 0, k, v)\n"\
2432 " end\n"\
2433 " function mt:__index(k)\n"\
2434 " return vim._getvar(scope, 0, k)\n"\
2435 " end\n"\
2436 " return setmetatable({}, mt)\n"\
2437 " end\n"\
2438 " vim.g = make_dict_accessor('g')\n"\
2439 " vim.v = make_dict_accessor('v')\n"\
2440 " vim.b = make_dict_accessor('b')\n"\
2441 " vim.w = make_dict_accessor('w')\n"\
2442 " vim.t = make_dict_accessor('t')\n"\
2443 "end"
2444
Bram Moolenaar55d5c032010-07-17 23:52:29 +02002445 static int
2446luaopen_vim(lua_State *L)
2447{
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002448 // set cache table
Bram Moolenaar0ba04292010-07-14 23:23:17 +02002449 lua_newtable(L);
2450 lua_newtable(L);
Bram Moolenaar1dced572012-04-05 16:54:08 +02002451 lua_pushstring(L, "v");
Bram Moolenaar0ba04292010-07-14 23:23:17 +02002452 lua_setfield(L, -2, "__mode");
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002453 lua_setmetatable(L, -2); // cache is weak-valued
2454 // print
Bram Moolenaar0ba04292010-07-14 23:23:17 +02002455 lua_pushcfunction(L, luaV_print);
2456 lua_setglobal(L, "print");
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002457 // debug.debug
Bram Moolenaar38e2b062011-09-21 17:15:39 +02002458 lua_getglobal(L, "debug");
2459 lua_pushcfunction(L, luaV_debug);
2460 lua_setfield(L, -2, "debug");
2461 lua_pop(L, 1);
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002462 // free
Bram Moolenaar0ba04292010-07-14 23:23:17 +02002463 lua_pushlightuserdata(L, (void *) LUAVIM_FREE);
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002464 lua_pushvalue(L, 1); // cache table
Bram Moolenaar1dced572012-04-05 16:54:08 +02002465 lua_pushcclosure(L, luaV_free, 1);
2466 lua_rawset(L, LUA_REGISTRYINDEX);
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002467 // luaeval
Bram Moolenaar1dced572012-04-05 16:54:08 +02002468 lua_pushlightuserdata(L, (void *) LUAVIM_LUAEVAL);
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002469 lua_pushvalue(L, 1); // cache table
Bram Moolenaar1dced572012-04-05 16:54:08 +02002470 lua_pushcclosure(L, luaV_luaeval, 1);
2471 lua_rawset(L, LUA_REGISTRYINDEX);
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002472 // setref
Bram Moolenaar1dced572012-04-05 16:54:08 +02002473 lua_pushlightuserdata(L, (void *) LUAVIM_SETREF);
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002474 lua_pushvalue(L, 1); // cache table
Bram Moolenaar1dced572012-04-05 16:54:08 +02002475 lua_pushcclosure(L, luaV_setref, 1);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02002476 lua_rawset(L, LUA_REGISTRYINDEX);
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002477 // register
Bram Moolenaar1dced572012-04-05 16:54:08 +02002478 luaV_newmetatable(L, LUAVIM_LIST);
2479 lua_pushvalue(L, 1);
2480 luaV_openlib(L, luaV_List_mt, 1);
2481 luaV_newmetatable(L, LUAVIM_DICT);
2482 lua_pushvalue(L, 1);
2483 luaV_openlib(L, luaV_Dict_mt, 1);
Bram Moolenaarb7828692019-03-23 13:57:02 +01002484 luaV_newmetatable(L, LUAVIM_BLOB);
2485 lua_pushvalue(L, 1);
2486 luaV_openlib(L, luaV_Blob_mt, 1);
Bram Moolenaarca06da92018-07-01 15:12:05 +02002487 luaV_newmetatable(L, LUAVIM_FUNCREF);
2488 lua_pushvalue(L, 1);
2489 luaV_openlib(L, luaV_Funcref_mt, 1);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02002490 luaV_newmetatable(L, LUAVIM_BUFFER);
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002491 lua_pushvalue(L, 1); // cache table
Bram Moolenaar1dced572012-04-05 16:54:08 +02002492 luaV_openlib(L, luaV_Buffer_mt, 1);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02002493 luaV_newmetatable(L, LUAVIM_WINDOW);
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002494 lua_pushvalue(L, 1); // cache table
Bram Moolenaar1dced572012-04-05 16:54:08 +02002495 luaV_openlib(L, luaV_Window_mt, 1);
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002496 lua_newtable(L); // vim table
2497 lua_pushvalue(L, 1); // cache table
Bram Moolenaar1dced572012-04-05 16:54:08 +02002498 luaV_openlib(L, luaV_module, 1);
Bram Moolenaar125ed272021-04-07 20:11:12 +02002499 luaV_pushversion(L);
2500 lua_setfield(L, -2, "lua_version");
Bram Moolenaar1dced572012-04-05 16:54:08 +02002501 lua_setglobal(L, LUAVIM_NAME);
Bram Moolenaareb04f082020-05-17 14:32:35 +02002502 // custom code
Bram Moolenaar9309eb22020-05-17 16:53:56 +02002503 (void)luaL_dostring(L, LUA_VIM_FN_CODE);
Bram Moolenaar788fbb42020-05-31 14:08:12 +02002504 (void)luaL_dostring(L, LUA_VIM_UPDATE_PACKAGE_PATHS);
Yegappan Lakshmanan9dc4bef2021-08-04 21:12:52 +02002505 (void)luaL_dostring(L, LUA_VIM_SETUP_VARIABLE_DICTS);
Bram Moolenaar788fbb42020-05-31 14:08:12 +02002506
2507 lua_getglobal(L, "vim");
2508 lua_getfield(L, -1, "_update_package_paths");
2509
2510 if (lua_pcall(L, 0, 0, 0))
2511 luaV_emsg(L);
2512
Bram Moolenaar0ba04292010-07-14 23:23:17 +02002513 return 0;
2514}
2515
Bram Moolenaar55d5c032010-07-17 23:52:29 +02002516 static lua_State *
2517luaV_newstate(void)
2518{
Bram Moolenaar0ba04292010-07-14 23:23:17 +02002519 lua_State *L = luaL_newstate();
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002520 luaL_openlibs(L); // core libs
2521 lua_pushcfunction(L, luaopen_vim); // vim
Bram Moolenaar0ba04292010-07-14 23:23:17 +02002522 lua_call(L, 0, 0);
2523 return L;
2524}
2525
Bram Moolenaar55d5c032010-07-17 23:52:29 +02002526 static void
2527luaV_setrange(lua_State *L, int line1, int line2)
2528{
Bram Moolenaar0ba04292010-07-14 23:23:17 +02002529 lua_getglobal(L, LUAVIM_NAME);
2530 lua_pushinteger(L, line1);
2531 lua_setfield(L, -2, "firstline");
2532 lua_pushinteger(L, line2);
2533 lua_setfield(L, -2, "lastline");
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002534 lua_pop(L, 1); // vim table
Bram Moolenaar0ba04292010-07-14 23:23:17 +02002535}
2536
2537
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002538// ======= Interface =======
Bram Moolenaar0ba04292010-07-14 23:23:17 +02002539
2540static lua_State *L = NULL;
2541
Bram Moolenaar55d5c032010-07-17 23:52:29 +02002542 static int
Bram Moolenaar1dced572012-04-05 16:54:08 +02002543lua_isopen(void)
Bram Moolenaar2bd6a1b2010-08-12 22:14:01 +02002544{
2545 return L != NULL;
2546}
2547
2548 static int
Bram Moolenaar55d5c032010-07-17 23:52:29 +02002549lua_init(void)
2550{
Bram Moolenaar1dced572012-04-05 16:54:08 +02002551 if (!lua_isopen())
Bram Moolenaar55d5c032010-07-17 23:52:29 +02002552 {
Bram Moolenaar0ba04292010-07-14 23:23:17 +02002553#ifdef DYNAMIC_LUA
Bram Moolenaar55d5c032010-07-17 23:52:29 +02002554 if (!lua_enabled(TRUE))
2555 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002556 emsg(_("Lua library cannot be loaded."));
Bram Moolenaar55d5c032010-07-17 23:52:29 +02002557 return FAIL;
2558 }
Bram Moolenaar0ba04292010-07-14 23:23:17 +02002559#endif
Bram Moolenaar55d5c032010-07-17 23:52:29 +02002560 L = luaV_newstate();
Bram Moolenaar0ba04292010-07-14 23:23:17 +02002561 }
2562 return OK;
2563}
2564
Bram Moolenaar55d5c032010-07-17 23:52:29 +02002565 void
2566lua_end(void)
2567{
Bram Moolenaar1dced572012-04-05 16:54:08 +02002568 if (lua_isopen())
Bram Moolenaar55d5c032010-07-17 23:52:29 +02002569 {
2570 lua_close(L);
2571 L = NULL;
Bram Moolenaar0ba04292010-07-14 23:23:17 +02002572 }
2573}
2574
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002575/*
2576 * ex commands
2577 */
Bram Moolenaar55d5c032010-07-17 23:52:29 +02002578 void
2579ex_lua(exarg_T *eap)
2580{
Bram Moolenaarc8970b92020-10-26 20:18:08 +01002581 char *script = (char *)script_get(eap, eap->arg);
2582
2583 if (!eap->skip && lua_init() == OK)
Bram Moolenaar55d5c032010-07-17 23:52:29 +02002584 {
Bram Moolenaarc8970b92020-10-26 20:18:08 +01002585 char *s = script != NULL ? script : (char *)eap->arg;
2586
Bram Moolenaar55d5c032010-07-17 23:52:29 +02002587 luaV_setrange(L, eap->line1, eap->line2);
2588 if (luaL_loadbuffer(L, s, strlen(s), LUAVIM_CHUNKNAME)
2589 || lua_pcall(L, 0, 0, 0))
2590 luaV_emsg(L);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02002591 }
Bram Moolenaarc8970b92020-10-26 20:18:08 +01002592 if (script != NULL)
2593 vim_free(script);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02002594}
2595
Bram Moolenaar55d5c032010-07-17 23:52:29 +02002596 void
2597ex_luado(exarg_T *eap)
2598{
Bram Moolenaar0ba04292010-07-14 23:23:17 +02002599 linenr_T l;
2600 const char *s = (const char *) eap->arg;
2601 luaL_Buffer b;
2602 size_t len;
Bram Moolenaard58f03b2017-01-29 22:48:45 +01002603 buf_T *was_curbuf = curbuf;
2604
Bram Moolenaar0ba04292010-07-14 23:23:17 +02002605 if (lua_init() == FAIL) return;
Bram Moolenaar55d5c032010-07-17 23:52:29 +02002606 if (u_save(eap->line1 - 1, eap->line2 + 1) == FAIL)
2607 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002608 emsg(_("cannot save undo information"));
Bram Moolenaar55d5c032010-07-17 23:52:29 +02002609 return;
Bram Moolenaar0ba04292010-07-14 23:23:17 +02002610 }
2611 luaV_setrange(L, eap->line1, eap->line2);
2612 luaL_buffinit(L, &b);
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002613 luaL_addlstring(&b, "return function(line, linenr) ", 30); // header
Bram Moolenaar0ba04292010-07-14 23:23:17 +02002614 luaL_addlstring(&b, s, strlen(s));
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002615 luaL_addlstring(&b, " end", 4); // footer
Bram Moolenaar0ba04292010-07-14 23:23:17 +02002616 luaL_pushresult(&b);
2617 s = lua_tolstring(L, -1, &len);
Bram Moolenaar55d5c032010-07-17 23:52:29 +02002618 if (luaL_loadbuffer(L, s, len, LUAVIM_CHUNKNAME))
2619 {
2620 luaV_emsg(L);
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002621 lua_pop(L, 1); // function body
Bram Moolenaar55d5c032010-07-17 23:52:29 +02002622 return;
Bram Moolenaar0ba04292010-07-14 23:23:17 +02002623 }
2624 lua_call(L, 0, 1);
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002625 lua_replace(L, -2); // function -> body
Bram Moolenaar55d5c032010-07-17 23:52:29 +02002626 for (l = eap->line1; l <= eap->line2; l++)
2627 {
Bram Moolenaare49b8e82020-07-01 13:52:55 +02002628 // Check the line number, the command may have deleted lines.
Bram Moolenaard58f03b2017-01-29 22:48:45 +01002629 if (l > curbuf->b_ml.ml_line_count)
2630 break;
2631
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002632 lua_pushvalue(L, -1); // function
2633 luaV_pushline(L, curbuf, l); // current line as arg
2634 lua_pushinteger(L, l); // current line number as arg
Bram Moolenaarbd2f3c32012-04-06 14:31:00 +02002635 if (lua_pcall(L, 2, 1, 0))
Bram Moolenaar55d5c032010-07-17 23:52:29 +02002636 {
2637 luaV_emsg(L);
2638 break;
2639 }
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002640 // Catch the command switching to another buffer.
Bram Moolenaard58f03b2017-01-29 22:48:45 +01002641 if (curbuf != was_curbuf)
2642 break;
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002643 if (lua_isstring(L, -1)) // update line?
Bram Moolenaar55d5c032010-07-17 23:52:29 +02002644 {
Bram Moolenaar0ba04292010-07-14 23:23:17 +02002645#ifdef HAVE_SANDBOX
Bram Moolenaar55d5c032010-07-17 23:52:29 +02002646 luaV_checksandbox(L);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02002647#endif
Bram Moolenaar55d5c032010-07-17 23:52:29 +02002648 ml_replace(l, luaV_toline(L, -1), TRUE);
2649 changed_bytes(l, 0);
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002650 lua_pop(L, 1); // result from luaV_toline
Bram Moolenaar55d5c032010-07-17 23:52:29 +02002651 }
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002652 lua_pop(L, 1); // line
Bram Moolenaar0ba04292010-07-14 23:23:17 +02002653 }
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002654 lua_pop(L, 1); // function
Bram Moolenaar0ba04292010-07-14 23:23:17 +02002655 check_cursor();
Bram Moolenaara4d158b2022-08-14 14:17:45 +01002656 update_screen(UPD_NOT_VALID);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02002657}
2658
Bram Moolenaar55d5c032010-07-17 23:52:29 +02002659 void
2660ex_luafile(exarg_T *eap)
2661{
2662 if (lua_init() == FAIL)
2663 return;
2664 if (!eap->skip)
2665 {
2666 luaV_setrange(L, eap->line1, eap->line2);
2667 if (luaL_loadfile(L, (char *) eap->arg) || lua_pcall(L, 0, 0, 0))
2668 luaV_emsg(L);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02002669 }
2670}
2671
Bram Moolenaar1dced572012-04-05 16:54:08 +02002672#define luaV_freetype(typ,tname) \
2673 void \
2674 lua_##tname##_free(typ *o) \
2675 { \
2676 if (!lua_isopen()) return; \
2677 luaV_getfield(L, LUAVIM_FREE); \
2678 lua_pushlightuserdata(L, (void *) o); \
2679 lua_call(L, 1, 0); \
2680 }
2681
2682luaV_freetype(buf_T, buffer)
2683luaV_freetype(win_T, window)
2684
Bram Moolenaar55d5c032010-07-17 23:52:29 +02002685 void
Bram Moolenaar4eefe472019-03-19 21:59:19 +01002686do_luaeval(char_u *str, typval_T *arg, typval_T *rettv)
Bram Moolenaar55d5c032010-07-17 23:52:29 +02002687{
Bram Moolenaar1dced572012-04-05 16:54:08 +02002688 lua_init();
2689 luaV_getfield(L, LUAVIM_LUAEVAL);
2690 lua_pushstring(L, (char *) str);
2691 lua_pushlightuserdata(L, (void *) arg);
2692 lua_pushlightuserdata(L, (void *) rettv);
2693 lua_call(L, 3, 0);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02002694}
2695
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01002696 int
Bram Moolenaar4eefe472019-03-19 21:59:19 +01002697set_ref_in_lua(int copyID)
Bram Moolenaar55d5c032010-07-17 23:52:29 +02002698{
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01002699 int aborted = 0;
2700
2701 if (lua_isopen())
2702 {
2703 luaV_getfield(L, LUAVIM_SETREF);
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002704 // call the function with 1 arg, getting 1 result back
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01002705 lua_pushinteger(L, copyID);
2706 lua_call(L, 1, 1);
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002707 // get the result
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01002708 aborted = lua_tointeger(L, -1);
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002709 // pop result off the stack
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01002710 lua_pop(L, 1);
2711 }
2712 return aborted;
Bram Moolenaar0ba04292010-07-14 23:23:17 +02002713}
2714
Bram Moolenaar788fbb42020-05-31 14:08:12 +02002715 void
2716update_package_paths_in_lua()
2717{
2718 if (lua_isopen())
2719 {
2720 lua_getglobal(L, "vim");
2721 lua_getfield(L, -1, "_update_package_paths");
2722
2723 if (lua_pcall(L, 0, 0, 0))
2724 luaV_emsg(L);
2725 }
2726}
2727
Bram Moolenaar801ab062020-06-25 19:27:56 +02002728/*
2729 * Native C function callback
2730 */
2731 static int
2732luaV_call_lua_func(
2733 int argcount,
2734 typval_T *argvars,
2735 typval_T *rettv,
2736 void *state)
2737{
2738 int i;
2739 int luaargcount = argcount;
2740 luaV_CFuncState *funcstate = (luaV_CFuncState*)state;
2741 lua_rawgeti(funcstate->L, LUA_REGISTRYINDEX, funcstate->lua_funcref);
2742
2743 if (funcstate->lua_tableref != LUA_NOREF)
2744 {
2745 // First arg for metatable __call method is a table
2746 luaargcount += 1;
2747 lua_rawgeti(funcstate->L, LUA_REGISTRYINDEX, funcstate->lua_tableref);
2748 }
2749
2750 for (i = 0; i < argcount; ++i)
2751 luaV_pushtypval(funcstate->L, &argvars[i]);
2752
2753 if (lua_pcall(funcstate->L, luaargcount, 1, 0))
2754 {
2755 luaV_emsg(funcstate->L);
2756 return FCERR_OTHER;
2757 }
2758
2759 luaV_checktypval(funcstate->L, -1, rettv, "get return value");
2760 return FCERR_NONE;
2761}
2762
2763/*
2764 * Free up any lua references held by the func state.
2765 */
2766 static void
2767luaV_call_lua_func_free(void *state)
2768{
2769 luaV_CFuncState *funcstate = (luaV_CFuncState*)state;
2770 luaL_unref(L, LUA_REGISTRYINDEX, funcstate->lua_funcref);
2771 funcstate->L = NULL;
2772 if (funcstate->lua_tableref != LUA_NOREF)
2773 luaL_unref(L, LUA_REGISTRYINDEX, funcstate->lua_tableref);
2774 VIM_CLEAR(funcstate);
2775}
2776
Bram Moolenaar0ba04292010-07-14 23:23:17 +02002777#endif