blob: 6941789b91d38383f50a80c5290f10db0f3d0abb [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
Yegappan Lakshmanan0233bdf2023-01-12 12:33:30 +0000508 if (p == NULL)
509 return NULL;
510
511 // value is userdata
512 if (lua_getmetatable(L, ud)) // does it have a metatable?
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200513 {
Yegappan Lakshmanan0233bdf2023-01-12 12:33:30 +0000514 luaV_getfield(L, tname); // get metatable
515 if (lua_rawequal(L, -1, -2)) // MTs match?
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200516 {
Yegappan Lakshmanan0233bdf2023-01-12 12:33:30 +0000517 lua_pop(L, 2); // MTs
518 return p;
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200519 }
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200520 }
Yegappan Lakshmanan0233bdf2023-01-12 12:33:30 +0000521
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200522 return NULL;
523}
524
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200525 static void *
Bram Moolenaar1dced572012-04-05 16:54:08 +0200526luaV_checkcache(lua_State *L, void *p)
527{
528 luaV_getudata(L, p);
529 if (lua_isnil(L, -1)) luaL_error(L, "invalid object");
530 lua_pop(L, 1);
531 return p;
532}
533
534#define luaV_unbox(L,luatyp,ud) (*((luatyp *) lua_touserdata((L),(ud))))
535
536#define luaV_checkvalid(L,luatyp,ud) \
537 luaV_checkcache((L), (void *) luaV_unbox((L),luatyp,(ud)))
538
539 static void *
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200540luaV_checkudata(lua_State *L, int ud, const char *tname)
541{
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200542 void *p = luaV_toudata(L, ud, tname);
Bram Moolenaar1dced572012-04-05 16:54:08 +0200543 if (p == NULL) luaL_typeerror(L, ud, tname);
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200544 return p;
545}
546
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200547 static void
548luaV_pushtypval(lua_State *L, typval_T *tv)
549{
Bram Moolenaar1dced572012-04-05 16:54:08 +0200550 if (tv == NULL)
551 {
552 lua_pushnil(L);
553 return;
554 }
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200555 switch (tv->v_type)
556 {
557 case VAR_STRING:
Bram Moolenaard04da7c2012-10-14 03:41:59 +0200558 lua_pushstring(L, tv->vval.v_string == NULL
559 ? "" : (char *)tv->vval.v_string);
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200560 break;
561 case VAR_NUMBER:
562 lua_pushinteger(L, (int) tv->vval.v_number);
563 break;
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200564 case VAR_FLOAT:
565 lua_pushnumber(L, (lua_Number) tv->vval.v_float);
566 break;
Bram Moolenaar1dced572012-04-05 16:54:08 +0200567 case VAR_LIST:
568 luaV_pushlist(L, tv->vval.v_list);
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200569 break;
Bram Moolenaar1dced572012-04-05 16:54:08 +0200570 case VAR_DICT:
571 luaV_pushdict(L, tv->vval.v_dict);
572 break;
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +0100573 case VAR_BOOL:
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100574 case VAR_SPECIAL:
575 if (tv->vval.v_number <= VVAL_TRUE)
576 lua_pushinteger(L, (int) tv->vval.v_number);
577 else
578 lua_pushnil(L);
579 break;
Bram Moolenaarca06da92018-07-01 15:12:05 +0200580 case VAR_FUNC:
Bram Moolenaar4eefe472019-03-19 21:59:19 +0100581 luaV_pushfuncref(L, tv->vval.v_string);
Bram Moolenaarca06da92018-07-01 15:12:05 +0200582 break;
Bram Moolenaar86c3a212021-03-08 19:50:24 +0100583 case VAR_PARTIAL:
584 // TODO: handle partial arguments
585 luaV_pushfuncref(L, partial_name(tv->vval.v_partial));
586 break;
587
Bram Moolenaarb7828692019-03-23 13:57:02 +0100588 case VAR_BLOB:
589 luaV_pushblob(L, tv->vval.v_blob);
590 break;
Bram Moolenaar1dced572012-04-05 16:54:08 +0200591 default:
592 lua_pushnil(L);
593 }
594}
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200595
Bram Moolenaarca06da92018-07-01 15:12:05 +0200596/*
597 * Converts lua value at 'pos' to typval 'tv'.
598 * Returns OK or FAIL.
599 */
600 static int
601luaV_totypval(lua_State *L, int pos, typval_T *tv)
Bram Moolenaar1dced572012-04-05 16:54:08 +0200602{
Bram Moolenaarca06da92018-07-01 15:12:05 +0200603 int status = OK;
604
Bram Moolenaara9a8e5f2020-07-02 21:17:57 +0200605 tv->v_lock = 0;
606
Bram Moolenaarca06da92018-07-01 15:12:05 +0200607 switch (lua_type(L, pos))
608 {
Bram Moolenaar1dced572012-04-05 16:54:08 +0200609 case LUA_TBOOLEAN:
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +0100610 tv->v_type = VAR_BOOL;
Bram Moolenaar1dced572012-04-05 16:54:08 +0200611 tv->vval.v_number = (varnumber_T) lua_toboolean(L, pos);
612 break;
Bram Moolenaar9067cd62019-01-01 00:41:54 +0100613 case LUA_TNIL:
614 tv->v_type = VAR_SPECIAL;
615 tv->vval.v_number = VVAL_NULL;
616 break;
Bram Moolenaar1dced572012-04-05 16:54:08 +0200617 case LUA_TSTRING:
618 tv->v_type = VAR_STRING;
619 tv->vval.v_string = vim_strsave((char_u *) lua_tostring(L, pos));
620 break;
621 case LUA_TNUMBER:
Bram Moolenaareb04f082020-05-17 14:32:35 +0200622 {
623 const lua_Number n = lua_tonumber(L, pos);
624
625 if (n > (lua_Number)INT64_MAX || n < (lua_Number)INT64_MIN
626 || ((lua_Number)((varnumber_T)n)) != n)
627 {
628 tv->v_type = VAR_FLOAT;
629 tv->vval.v_float = (float_T)n;
630 }
631 else
632 {
633 tv->v_type = VAR_NUMBER;
634 tv->vval.v_number = (varnumber_T)n;
635 }
636 }
Bram Moolenaar1dced572012-04-05 16:54:08 +0200637 break;
Bram Moolenaar801ab062020-06-25 19:27:56 +0200638 case LUA_TFUNCTION:
639 {
640 char_u *name;
Bram Moolenaar066e7da2020-07-18 12:50:35 +0200641 luaV_CFuncState *state;
642
Bram Moolenaar801ab062020-06-25 19:27:56 +0200643 lua_pushvalue(L, pos);
Bram Moolenaar066e7da2020-07-18 12:50:35 +0200644 state = ALLOC_CLEAR_ONE(luaV_CFuncState);
Bram Moolenaar801ab062020-06-25 19:27:56 +0200645 state->lua_funcref = luaL_ref(L, LUA_REGISTRYINDEX);
646 state->L = L;
647 state->lua_tableref = LUA_NOREF;
648 name = register_cfunc(&luaV_call_lua_func,
649 &luaV_call_lua_func_free, state);
650 tv->v_type = VAR_FUNC;
651 tv->vval.v_string = vim_strsave(name);
652 break;
653 }
654 case LUA_TTABLE:
655 {
Bram Moolenaar066e7da2020-07-18 12:50:35 +0200656 int lua_tableref;
657
Bram Moolenaar801ab062020-06-25 19:27:56 +0200658 lua_pushvalue(L, pos);
Bram Moolenaar066e7da2020-07-18 12:50:35 +0200659 lua_tableref = luaL_ref(L, LUA_REGISTRYINDEX);
Bram Moolenaar801ab062020-06-25 19:27:56 +0200660 if (lua_getmetatable(L, pos)) {
661 lua_getfield(L, -1, LUA___CALL);
662 if (lua_isfunction(L, -1)) {
663 char_u *name;
664 int lua_funcref = luaL_ref(L, LUA_REGISTRYINDEX);
665 luaV_CFuncState *state = ALLOC_CLEAR_ONE(luaV_CFuncState);
Bram Moolenaar066e7da2020-07-18 12:50:35 +0200666
Bram Moolenaar801ab062020-06-25 19:27:56 +0200667 state->lua_funcref = lua_funcref;
668 state->L = L;
669 state->lua_tableref = lua_tableref;
670 name = register_cfunc(&luaV_call_lua_func,
671 &luaV_call_lua_func_free, state);
672 tv->v_type = VAR_FUNC;
673 tv->vval.v_string = vim_strsave(name);
674 break;
675 }
676 }
677 tv->v_type = VAR_NUMBER;
678 tv->vval.v_number = 0;
679 status = FAIL;
680 break;
681 }
Bram Moolenaarca06da92018-07-01 15:12:05 +0200682 case LUA_TUSERDATA:
683 {
Bram Moolenaar1dced572012-04-05 16:54:08 +0200684 void *p = lua_touserdata(L, pos);
Bram Moolenaarca06da92018-07-01 15:12:05 +0200685
Bram Moolenaarb7828692019-03-23 13:57:02 +0100686 if (lua_getmetatable(L, pos)) // has metatable?
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200687 {
Bram Moolenaarb7828692019-03-23 13:57:02 +0100688 // check list
Bram Moolenaar1dced572012-04-05 16:54:08 +0200689 luaV_getfield(L, LUAVIM_LIST);
690 if (lua_rawequal(L, -1, -2))
Bram Moolenaar2334b6d2010-07-22 21:32:16 +0200691 {
Bram Moolenaar1dced572012-04-05 16:54:08 +0200692 tv->v_type = VAR_LIST;
693 tv->vval.v_list = *((luaV_List *) p);
694 ++tv->vval.v_list->lv_refcount;
Bram Moolenaarb7828692019-03-23 13:57:02 +0100695 lua_pop(L, 2); // MTs
Bram Moolenaarca06da92018-07-01 15:12:05 +0200696 break;
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200697 }
Bram Moolenaarb7828692019-03-23 13:57:02 +0100698 // check dict
Bram Moolenaar1dced572012-04-05 16:54:08 +0200699 luaV_getfield(L, LUAVIM_DICT);
700 if (lua_rawequal(L, -1, -3))
701 {
702 tv->v_type = VAR_DICT;
703 tv->vval.v_dict = *((luaV_Dict *) p);
704 ++tv->vval.v_dict->dv_refcount;
Bram Moolenaarb7828692019-03-23 13:57:02 +0100705 lua_pop(L, 3); // MTs
Bram Moolenaarca06da92018-07-01 15:12:05 +0200706 break;
Bram Moolenaar1dced572012-04-05 16:54:08 +0200707 }
Bram Moolenaarb7828692019-03-23 13:57:02 +0100708 // check blob
709 luaV_getfield(L, LUAVIM_BLOB);
Bram Moolenaarca06da92018-07-01 15:12:05 +0200710 if (lua_rawequal(L, -1, -4))
711 {
Bram Moolenaarb7828692019-03-23 13:57:02 +0100712 tv->v_type = VAR_BLOB;
713 tv->vval.v_blob = *((luaV_Blob *) p);
714 ++tv->vval.v_blob->bv_refcount;
715 lua_pop(L, 4); // MTs
716 break;
717 }
718 // check funcref
719 luaV_getfield(L, LUAVIM_FUNCREF);
720 if (lua_rawequal(L, -1, -5))
721 {
Bram Moolenaarca06da92018-07-01 15:12:05 +0200722 luaV_Funcref *f = (luaV_Funcref *) p;
Bram Moolenaar066e7da2020-07-18 12:50:35 +0200723
Bram Moolenaar4eefe472019-03-19 21:59:19 +0100724 func_ref(f->name);
725 tv->v_type = VAR_FUNC;
726 tv->vval.v_string = vim_strsave(f->name);
Bram Moolenaarb7828692019-03-23 13:57:02 +0100727 lua_pop(L, 5); // MTs
Bram Moolenaarca06da92018-07-01 15:12:05 +0200728 break;
729 }
Bram Moolenaarb7828692019-03-23 13:57:02 +0100730 lua_pop(L, 4); // MTs
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200731 }
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200732 }
Bram Moolenaarb7828692019-03-23 13:57:02 +0100733 // FALLTHROUGH
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200734 default:
Bram Moolenaar1dced572012-04-05 16:54:08 +0200735 tv->v_type = VAR_NUMBER;
736 tv->vval.v_number = 0;
Bram Moolenaarca06da92018-07-01 15:12:05 +0200737 status = FAIL;
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200738 }
Bram Moolenaarca06da92018-07-01 15:12:05 +0200739 return status;
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200740}
741
Bram Moolenaar2ab2e862019-12-04 21:24:53 +0100742/*
743 * similar to luaL_addlstring, but replaces \0 with \n if toline and
744 * \n with \0 otherwise
745 */
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200746 static void
747luaV_addlstring(luaL_Buffer *b, const char *s, size_t l, int toline)
748{
749 while (l--)
750 {
751 if (*s == '\0' && toline)
752 luaL_addchar(b, '\n');
753 else if (*s == '\n' && !toline)
754 luaL_addchar(b, '\0');
755 else
756 luaL_addchar(b, *s);
757 s++;
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200758 }
759}
760
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200761 static void
762luaV_pushline(lua_State *L, buf_T *buf, linenr_T n)
763{
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200764 const char *s = (const char *) ml_get_buf(buf, n, FALSE);
765 luaL_Buffer b;
766 luaL_buffinit(L, &b);
767 luaV_addlstring(&b, s, strlen(s), 0);
768 luaL_pushresult(&b);
769}
770
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200771 static char_u *
772luaV_toline(lua_State *L, int pos)
773{
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200774 size_t l;
775 const char *s = lua_tolstring(L, pos, &l);
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200776
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200777 luaL_Buffer b;
778 luaL_buffinit(L, &b);
779 luaV_addlstring(&b, s, l, 1);
780 luaL_pushresult(&b);
781 return (char_u *) lua_tostring(L, -1);
782}
783
Bram Moolenaar2ab2e862019-12-04 21:24:53 +0100784/*
785 * pops a string s from the top of the stack and calls mf(t) for pieces t of
786 * s separated by newlines
787 */
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200788 static void
789luaV_msgfunc(lua_State *L, msgfunc_T mf)
790{
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200791 luaL_Buffer b;
792 size_t l;
793 const char *p, *s = lua_tolstring(L, -1, &l);
794 luaL_buffinit(L, &b);
795 luaV_addlstring(&b, s, l, 0);
796 luaL_pushresult(&b);
Bram Moolenaar2ab2e862019-12-04 21:24:53 +0100797 // break string
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200798 p = s = lua_tolstring(L, -1, &l);
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200799 while (l--)
800 {
Bram Moolenaar2ab2e862019-12-04 21:24:53 +0100801 if (*p++ == '\0') // break?
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200802 {
Bram Moolenaarc8970b92020-10-26 20:18:08 +0100803 mf((char *)s);
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200804 s = p;
805 }
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200806 }
Bram Moolenaarc8970b92020-10-26 20:18:08 +0100807 mf((char *)s);
Bram Moolenaar2ab2e862019-12-04 21:24:53 +0100808 lua_pop(L, 2); // original and modified strings
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200809}
810
Bram Moolenaar1dced572012-04-05 16:54:08 +0200811#define luaV_newtype(typ,tname,luatyp,luatname) \
812 static luatyp * \
Bram Moolenaar4eefe472019-03-19 21:59:19 +0100813 luaV_new##tname(lua_State *L, typ *obj) \
Bram Moolenaar1dced572012-04-05 16:54:08 +0200814 { \
815 luatyp *o = (luatyp *) lua_newuserdata(L, sizeof(luatyp)); \
816 *o = obj; \
817 luaV_setudata(L, obj); /* cache[obj] = udata */ \
818 luaV_getfield(L, luatname); \
819 lua_setmetatable(L, -2); \
820 return o; \
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200821 }
Bram Moolenaar1dced572012-04-05 16:54:08 +0200822
823#define luaV_pushtype(typ,tname,luatyp) \
824 static luatyp * \
Bram Moolenaarca06da92018-07-01 15:12:05 +0200825 luaV_push##tname(lua_State *L, typ *obj) \
Bram Moolenaar1dced572012-04-05 16:54:08 +0200826 { \
827 luatyp *o = NULL; \
828 if (obj == NULL) \
829 lua_pushnil(L); \
830 else { \
831 luaV_getudata(L, obj); \
832 if (lua_isnil(L, -1)) /* not interned? */ \
833 { \
834 lua_pop(L, 1); \
835 o = luaV_new##tname(L, obj); \
836 } \
837 else \
838 o = (luatyp *) lua_touserdata(L, -1); \
839 } \
840 return o; \
841 }
842
843#define luaV_type_tostring(tname,luatname) \
844 static int \
Bram Moolenaar4eefe472019-03-19 21:59:19 +0100845 luaV_##tname##_tostring(lua_State *L) \
Bram Moolenaar1dced572012-04-05 16:54:08 +0200846 { \
847 lua_pushfstring(L, "%s: %p", luatname, lua_touserdata(L, 1)); \
848 return 1; \
849 }
850
Bram Moolenaar2ab2e862019-12-04 21:24:53 +0100851// ======= List type =======
Bram Moolenaar1dced572012-04-05 16:54:08 +0200852
853 static luaV_List *
Bram Moolenaar4eefe472019-03-19 21:59:19 +0100854luaV_newlist(lua_State *L, list_T *lis)
Bram Moolenaar1dced572012-04-05 16:54:08 +0200855{
856 luaV_List *l = (luaV_List *) lua_newuserdata(L, sizeof(luaV_List));
857 *l = lis;
Bram Moolenaar2ab2e862019-12-04 21:24:53 +0100858 lis->lv_refcount++; // reference in Lua
859 luaV_setudata(L, lis); // cache[lis] = udata
Bram Moolenaar1dced572012-04-05 16:54:08 +0200860 luaV_getfield(L, LUAVIM_LIST);
861 lua_setmetatable(L, -2);
862 return l;
863}
864
865luaV_pushtype(list_T, list, luaV_List)
866luaV_type_tostring(list, LUAVIM_LIST)
867
868 static int
Bram Moolenaar4eefe472019-03-19 21:59:19 +0100869luaV_list_len(lua_State *L)
Bram Moolenaar1dced572012-04-05 16:54:08 +0200870{
871 list_T *l = luaV_unbox(L, luaV_List, 1);
Bram Moolenaarb7828692019-03-23 13:57:02 +0100872 lua_pushinteger(L, (int) list_len(l));
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200873 return 1;
874}
875
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200876 static int
Bram Moolenaar4eefe472019-03-19 21:59:19 +0100877luaV_list_iter(lua_State *L)
Bram Moolenaar1dced572012-04-05 16:54:08 +0200878{
879 listitem_T *li = (listitem_T *) lua_touserdata(L, lua_upvalueindex(2));
880 if (li == NULL) return 0;
881 luaV_pushtypval(L, &li->li_tv);
882 lua_pushlightuserdata(L, (void *) li->li_next);
883 lua_replace(L, lua_upvalueindex(2));
884 return 1;
885}
886
887 static int
Bram Moolenaar4eefe472019-03-19 21:59:19 +0100888luaV_list_call(lua_State *L)
Bram Moolenaar1dced572012-04-05 16:54:08 +0200889{
890 list_T *l = luaV_unbox(L, luaV_List, 1);
Bram Moolenaar2ab2e862019-12-04 21:24:53 +0100891 lua_pushvalue(L, lua_upvalueindex(1)); // pass cache table along
Bram Moolenaar1dced572012-04-05 16:54:08 +0200892 lua_pushlightuserdata(L, (void *) l->lv_first);
893 lua_pushcclosure(L, luaV_list_iter, 2);
894 return 1;
895}
896
897 static int
Bram Moolenaar4eefe472019-03-19 21:59:19 +0100898luaV_list_index(lua_State *L)
Bram Moolenaar1dced572012-04-05 16:54:08 +0200899{
900 list_T *l = luaV_unbox(L, luaV_List, 1);
Bram Moolenaar2ab2e862019-12-04 21:24:53 +0100901 if (lua_isnumber(L, 2)) // list item?
Bram Moolenaar1dced572012-04-05 16:54:08 +0200902 {
Bram Moolenaarbd846172020-06-27 12:32:57 +0200903 long n = (long) luaL_checkinteger(L, 2);
904 listitem_T *li;
905
906 // Lua array index starts with 1 while Vim uses 0, subtract 1 to
907 // normalize.
908 n -= 1;
909 li = list_find(l, n);
Bram Moolenaar1dced572012-04-05 16:54:08 +0200910 if (li == NULL)
911 lua_pushnil(L);
912 else
913 luaV_pushtypval(L, &li->li_tv);
914 }
Bram Moolenaar2ab2e862019-12-04 21:24:53 +0100915 else if (lua_isstring(L, 2)) // method?
Bram Moolenaar1dced572012-04-05 16:54:08 +0200916 {
917 const char *s = lua_tostring(L, 2);
918 if (strncmp(s, "add", 3) == 0
Bram Moolenaarb3766472013-04-15 13:49:21 +0200919 || strncmp(s, "insert", 6) == 0)
Bram Moolenaar1dced572012-04-05 16:54:08 +0200920 {
921 lua_getmetatable(L, 1);
922 lua_getfield(L, -1, s);
923 }
924 else
925 lua_pushnil(L);
926 }
927 else
928 lua_pushnil(L);
929 return 1;
930}
931
932 static int
Bram Moolenaar4eefe472019-03-19 21:59:19 +0100933luaV_list_newindex(lua_State *L)
Bram Moolenaar1dced572012-04-05 16:54:08 +0200934{
935 list_T *l = luaV_unbox(L, luaV_List, 1);
936 long n = (long) luaL_checkinteger(L, 2);
937 listitem_T *li;
Bram Moolenaarbd846172020-06-27 12:32:57 +0200938
939 // Lua array index starts with 1 while Vim uses 0, subtract 1 to normalize.
940 n -= 1;
941
Bram Moolenaar1dced572012-04-05 16:54:08 +0200942 if (l->lv_lock)
943 luaL_error(L, "list is locked");
944 li = list_find(l, n);
Bram Moolenaara1f9f862020-06-28 22:41:26 +0200945 if (li == NULL)
Bram Moolenaar1dced572012-04-05 16:54:08 +0200946 {
Bram Moolenaar6ed545e2022-05-09 20:09:23 +0100947 if (!lua_isnil(L, 3))
948 {
949 typval_T v;
950 luaV_checktypval(L, 3, &v, "inserting list item");
951 if (list_insert_tv(l, &v, li) == FAIL)
952 luaL_error(L, "failed to add item to list");
953 clear_tv(&v);
954 }
Bram Moolenaar1dced572012-04-05 16:54:08 +0200955 }
956 else
957 {
Bram Moolenaar6ed545e2022-05-09 20:09:23 +0100958 if (lua_isnil(L, 3)) // remove?
959 {
Bram Moolenaara1f9f862020-06-28 22:41:26 +0200960 vimlist_remove(l, li, li);
961 listitem_free(l, li);
Bram Moolenaar6ed545e2022-05-09 20:09:23 +0100962 }
963 else
964 {
Bram Moolenaara1f9f862020-06-28 22:41:26 +0200965 typval_T v;
966 luaV_checktypval(L, 3, &v, "setting list item");
967 clear_tv(&li->li_tv);
Bram Moolenaare49b8e82020-07-01 13:52:55 +0200968 li->li_tv = v;
Bram Moolenaar6ed545e2022-05-09 20:09:23 +0100969 }
Bram Moolenaar1dced572012-04-05 16:54:08 +0200970 }
971 return 0;
972}
973
974 static int
Bram Moolenaar4eefe472019-03-19 21:59:19 +0100975luaV_list_add(lua_State *L)
Bram Moolenaar1dced572012-04-05 16:54:08 +0200976{
977 luaV_List *lis = luaV_checkudata(L, 1, LUAVIM_LIST);
978 list_T *l = (list_T *) luaV_checkcache(L, (void *) *lis);
Bram Moolenaarb3766472013-04-15 13:49:21 +0200979 typval_T v;
Bram Moolenaar1dced572012-04-05 16:54:08 +0200980 if (l->lv_lock)
981 luaL_error(L, "list is locked");
Bram Moolenaarb3766472013-04-15 13:49:21 +0200982 lua_settop(L, 2);
Bram Moolenaarca06da92018-07-01 15:12:05 +0200983 luaV_checktypval(L, 2, &v, "adding list item");
Bram Moolenaarb3766472013-04-15 13:49:21 +0200984 if (list_append_tv(l, &v) == FAIL)
Bram Moolenaarca06da92018-07-01 15:12:05 +0200985 luaL_error(L, "failed to add item to list");
Bram Moolenaarb3766472013-04-15 13:49:21 +0200986 clear_tv(&v);
Bram Moolenaar1dced572012-04-05 16:54:08 +0200987 lua_settop(L, 1);
988 return 1;
989}
990
991 static int
Bram Moolenaar4eefe472019-03-19 21:59:19 +0100992luaV_list_insert(lua_State *L)
Bram Moolenaar1dced572012-04-05 16:54:08 +0200993{
994 luaV_List *lis = luaV_checkudata(L, 1, LUAVIM_LIST);
995 list_T *l = (list_T *) luaV_checkcache(L, (void *) *lis);
Bram Moolenaar46538ee2015-02-17 16:28:55 +0100996 long pos = (long) luaL_optinteger(L, 3, 0);
Bram Moolenaar1dced572012-04-05 16:54:08 +0200997 listitem_T *li = NULL;
998 typval_T v;
999 if (l->lv_lock)
1000 luaL_error(L, "list is locked");
1001 if (pos < l->lv_len)
1002 {
1003 li = list_find(l, pos);
1004 if (li == NULL)
1005 luaL_error(L, "invalid position");
1006 }
1007 lua_settop(L, 2);
Bram Moolenaarca06da92018-07-01 15:12:05 +02001008 luaV_checktypval(L, 2, &v, "inserting list item");
Bram Moolenaarb3766472013-04-15 13:49:21 +02001009 if (list_insert_tv(l, &v, li) == FAIL)
Bram Moolenaarca06da92018-07-01 15:12:05 +02001010 luaL_error(L, "failed to add item to list");
Bram Moolenaarb3766472013-04-15 13:49:21 +02001011 clear_tv(&v);
Bram Moolenaar1dced572012-04-05 16:54:08 +02001012 lua_settop(L, 1);
1013 return 1;
1014}
1015
1016static const luaL_Reg luaV_List_mt[] = {
1017 {"__tostring", luaV_list_tostring},
Bram Moolenaar1dced572012-04-05 16:54:08 +02001018 {"__len", luaV_list_len},
1019 {"__call", luaV_list_call},
1020 {"__index", luaV_list_index},
1021 {"__newindex", luaV_list_newindex},
1022 {"add", luaV_list_add},
1023 {"insert", luaV_list_insert},
1024 {NULL, NULL}
1025};
1026
1027
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001028// ======= Dict type =======
Bram Moolenaar1dced572012-04-05 16:54:08 +02001029
1030 static luaV_Dict *
Bram Moolenaar4eefe472019-03-19 21:59:19 +01001031luaV_newdict(lua_State *L, dict_T *dic)
Bram Moolenaar1dced572012-04-05 16:54:08 +02001032{
1033 luaV_Dict *d = (luaV_Dict *) lua_newuserdata(L, sizeof(luaV_Dict));
1034 *d = dic;
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001035 dic->dv_refcount++; // reference in Lua
1036 luaV_setudata(L, dic); // cache[dic] = udata
Bram Moolenaar1dced572012-04-05 16:54:08 +02001037 luaV_getfield(L, LUAVIM_DICT);
1038 lua_setmetatable(L, -2);
1039 return d;
1040}
1041
1042luaV_pushtype(dict_T, dict, luaV_Dict)
1043luaV_type_tostring(dict, LUAVIM_DICT)
1044
1045 static int
Bram Moolenaar4eefe472019-03-19 21:59:19 +01001046luaV_dict_len(lua_State *L)
Bram Moolenaar1dced572012-04-05 16:54:08 +02001047{
1048 dict_T *d = luaV_unbox(L, luaV_Dict, 1);
Bram Moolenaarb7828692019-03-23 13:57:02 +01001049 lua_pushinteger(L, (int) dict_len(d));
Bram Moolenaar1dced572012-04-05 16:54:08 +02001050 return 1;
1051}
1052
1053 static int
Bram Moolenaar4eefe472019-03-19 21:59:19 +01001054luaV_dict_iter(lua_State *L UNUSED)
Bram Moolenaar1dced572012-04-05 16:54:08 +02001055{
Bram Moolenaarfeeaa682013-02-14 22:19:51 +01001056#ifdef FEAT_EVAL
Bram Moolenaar1dced572012-04-05 16:54:08 +02001057 hashitem_T *hi = (hashitem_T *) lua_touserdata(L, lua_upvalueindex(2));
1058 int n = lua_tointeger(L, lua_upvalueindex(3));
1059 dictitem_T *di;
1060 if (n <= 0) return 0;
1061 while (HASHITEM_EMPTY(hi)) hi++;
1062 di = dict_lookup(hi);
1063 lua_pushstring(L, (char *) hi->hi_key);
1064 luaV_pushtypval(L, &di->di_tv);
1065 lua_pushlightuserdata(L, (void *) (hi + 1));
1066 lua_replace(L, lua_upvalueindex(2));
1067 lua_pushinteger(L, n - 1);
1068 lua_replace(L, lua_upvalueindex(3));
1069 return 2;
Bram Moolenaarfeeaa682013-02-14 22:19:51 +01001070#else
1071 return 0;
1072#endif
Bram Moolenaar1dced572012-04-05 16:54:08 +02001073}
1074
1075 static int
Bram Moolenaar4eefe472019-03-19 21:59:19 +01001076luaV_dict_call(lua_State *L)
Bram Moolenaar1dced572012-04-05 16:54:08 +02001077{
1078 dict_T *d = luaV_unbox(L, luaV_Dict, 1);
1079 hashtab_T *ht = &d->dv_hashtab;
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001080 lua_pushvalue(L, lua_upvalueindex(1)); // pass cache table along
Bram Moolenaar1dced572012-04-05 16:54:08 +02001081 lua_pushlightuserdata(L, (void *) ht->ht_array);
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001082 lua_pushinteger(L, ht->ht_used); // # remaining items
Bram Moolenaar1dced572012-04-05 16:54:08 +02001083 lua_pushcclosure(L, luaV_dict_iter, 3);
1084 return 1;
1085}
1086
1087 static int
Bram Moolenaarca06da92018-07-01 15:12:05 +02001088luaV_dict_index(lua_State *L)
Bram Moolenaar1dced572012-04-05 16:54:08 +02001089{
1090 dict_T *d = luaV_unbox(L, luaV_Dict, 1);
1091 char_u *key = (char_u *) luaL_checkstring(L, 2);
1092 dictitem_T *di = dict_find(d, key, -1);
Bram Moolenaarca06da92018-07-01 15:12:05 +02001093
Bram Moolenaar1dced572012-04-05 16:54:08 +02001094 if (di == NULL)
Bram Moolenaarca06da92018-07-01 15:12:05 +02001095 {
Yegappan Lakshmanan0233bdf2023-01-12 12:33:30 +00001096 lua_pushnil(L);
1097 return 1;
Bram Moolenaarca06da92018-07-01 15:12:05 +02001098 }
Yegappan Lakshmanan0233bdf2023-01-12 12:33:30 +00001099
1100 luaV_pushtypval(L, &di->di_tv);
1101 if (di->di_tv.v_type == VAR_FUNC) // funcref?
1102 {
1103 luaV_Funcref *f = (luaV_Funcref *) lua_touserdata(L, -1);
1104 f->self = d; // keep "self" reference
1105 d->dv_refcount++;
1106 }
1107
Bram Moolenaar1dced572012-04-05 16:54:08 +02001108 return 1;
1109}
1110
1111 static int
Bram Moolenaarca06da92018-07-01 15:12:05 +02001112luaV_dict_newindex(lua_State *L)
Bram Moolenaar1dced572012-04-05 16:54:08 +02001113{
1114 dict_T *d = luaV_unbox(L, luaV_Dict, 1);
1115 char_u *key = (char_u *) luaL_checkstring(L, 2);
1116 dictitem_T *di;
Bram Moolenaare49b8e82020-07-01 13:52:55 +02001117 typval_T tv;
Bram Moolenaar713bf9e92019-03-16 16:38:41 +01001118
Bram Moolenaar1dced572012-04-05 16:54:08 +02001119 if (d->dv_lock)
1120 luaL_error(L, "dict is locked");
Bram Moolenaard6ef5f92018-07-13 22:08:23 +02001121 if (key == NULL)
1122 return 0;
1123 if (*key == NUL)
Bram Moolenaarca06da92018-07-01 15:12:05 +02001124 luaL_error(L, "empty key");
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001125 if (!lua_isnil(L, 3)) // read value?
Bram Moolenaar17413672018-07-14 20:49:42 +02001126 {
Bram Moolenaare49b8e82020-07-01 13:52:55 +02001127 luaV_checktypval(L, 3, &tv, "setting dict item");
1128 if (d->dv_scope == VAR_DEF_SCOPE && tv.v_type == VAR_FUNC)
1129 {
1130 clear_tv(&tv);
Bram Moolenaarca06da92018-07-01 15:12:05 +02001131 luaL_error(L, "cannot assign funcref to builtin scope");
Bram Moolenaare49b8e82020-07-01 13:52:55 +02001132 }
Bram Moolenaarca06da92018-07-01 15:12:05 +02001133 }
Bram Moolenaar1dced572012-04-05 16:54:08 +02001134 di = dict_find(d, key, -1);
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001135 if (di == NULL) // non-existing key?
Bram Moolenaar1dced572012-04-05 16:54:08 +02001136 {
Bram Moolenaard6ef5f92018-07-13 22:08:23 +02001137 if (lua_isnil(L, 3))
1138 return 0;
Bram Moolenaar1dced572012-04-05 16:54:08 +02001139 di = dictitem_alloc(key);
Bram Moolenaard6ef5f92018-07-13 22:08:23 +02001140 if (di == NULL)
Bram Moolenaare49b8e82020-07-01 13:52:55 +02001141 {
1142 clear_tv(&tv);
Bram Moolenaard6ef5f92018-07-13 22:08:23 +02001143 return 0;
Bram Moolenaare49b8e82020-07-01 13:52:55 +02001144 }
Bram Moolenaar1dced572012-04-05 16:54:08 +02001145 if (dict_add(d, di) == FAIL)
1146 {
Bram Moolenaard6ef5f92018-07-13 22:08:23 +02001147 vim_free(di);
Bram Moolenaare49b8e82020-07-01 13:52:55 +02001148 clear_tv(&tv);
Bram Moolenaard6ef5f92018-07-13 22:08:23 +02001149 return 0;
Bram Moolenaar1dced572012-04-05 16:54:08 +02001150 }
1151 }
1152 else
1153 clear_tv(&di->di_tv);
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001154 if (lua_isnil(L, 3)) // remove?
Bram Moolenaar1dced572012-04-05 16:54:08 +02001155 {
1156 hashitem_T *hi = hash_find(&d->dv_hashtab, di->di_key);
Bram Moolenaaref2c3252022-11-25 16:31:51 +00001157 hash_remove(&d->dv_hashtab, hi, "Lua new index");
Bram Moolenaar1dced572012-04-05 16:54:08 +02001158 dictitem_free(di);
1159 }
Bram Moolenaarca06da92018-07-01 15:12:05 +02001160 else
Bram Moolenaare49b8e82020-07-01 13:52:55 +02001161 di->di_tv = tv;
Bram Moolenaar1dced572012-04-05 16:54:08 +02001162 return 0;
1163}
1164
1165static const luaL_Reg luaV_Dict_mt[] = {
1166 {"__tostring", luaV_dict_tostring},
Bram Moolenaar1dced572012-04-05 16:54:08 +02001167 {"__len", luaV_dict_len},
1168 {"__call", luaV_dict_call},
1169 {"__index", luaV_dict_index},
1170 {"__newindex", luaV_dict_newindex},
1171 {NULL, NULL}
1172};
1173
1174
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001175// ======= Blob type =======
Bram Moolenaarb7828692019-03-23 13:57:02 +01001176
1177 static luaV_Blob *
1178luaV_newblob(lua_State *L, blob_T *blo)
1179{
1180 luaV_Blob *b = (luaV_Blob *) lua_newuserdata(L, sizeof(luaV_Blob));
1181 *b = blo;
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001182 blo->bv_refcount++; // reference in Lua
1183 luaV_setudata(L, blo); // cache[blo] = udata
Bram Moolenaarb7828692019-03-23 13:57:02 +01001184 luaV_getfield(L, LUAVIM_BLOB);
1185 lua_setmetatable(L, -2);
1186 return b;
1187}
1188
1189luaV_pushtype(blob_T, blob, luaV_Blob)
1190luaV_type_tostring(blob, LUAVIM_BLOB)
1191
1192 static int
1193luaV_blob_gc(lua_State *L)
1194{
1195 blob_T *b = luaV_unbox(L, luaV_Blob, 1);
1196 blob_unref(b);
1197 return 0;
1198}
1199
1200 static int
1201luaV_blob_len(lua_State *L)
1202{
1203 blob_T *b = luaV_unbox(L, luaV_Blob, 1);
1204 lua_pushinteger(L, (int) blob_len(b));
1205 return 1;
1206}
1207
1208 static int
1209luaV_blob_index(lua_State *L)
1210{
1211 blob_T *b = luaV_unbox(L, luaV_Blob, 1);
1212 if (lua_isnumber(L, 2))
1213 {
1214 int idx = luaL_checkinteger(L, 2);
1215 if (idx < blob_len(b))
1216 lua_pushnumber(L, (lua_Number) blob_get(b, idx));
1217 else
1218 lua_pushnil(L);
1219 }
1220 else if (lua_isstring(L, 2))
1221 {
1222 const char *s = lua_tostring(L, 2);
1223 if (strncmp(s, "add", 3) == 0)
1224 {
1225 lua_getmetatable(L, 1);
1226 lua_getfield(L, -1, s);
1227 }
1228 else
1229 lua_pushnil(L);
1230 }
1231 else
1232 lua_pushnil(L);
1233 return 1;
1234}
1235
1236 static int
1237luaV_blob_newindex(lua_State *L)
1238{
1239 blob_T *b = luaV_unbox(L, luaV_Blob, 1);
1240 if (b->bv_lock)
1241 luaL_error(L, "blob is locked");
Yegappan Lakshmanan0233bdf2023-01-12 12:33:30 +00001242
1243 if (!lua_isnumber(L, 2))
1244 return 0;
1245
1246 long len = blob_len(b);
1247 int idx = luaL_checkinteger(L, 2);
1248 int val = luaL_checkinteger(L, 3);
1249 if (idx < len || (idx == len && ga_grow(&b->bv_ga, 1) == OK))
Bram Moolenaarb7828692019-03-23 13:57:02 +01001250 {
Yegappan Lakshmanan0233bdf2023-01-12 12:33:30 +00001251 blob_set(b, idx, (char_u) val);
1252 if (idx == len)
1253 ++b->bv_ga.ga_len;
Bram Moolenaarb7828692019-03-23 13:57:02 +01001254 }
Yegappan Lakshmanan0233bdf2023-01-12 12:33:30 +00001255 else
1256 luaL_error(L, "index out of range");
1257
Bram Moolenaarb7828692019-03-23 13:57:02 +01001258 return 0;
1259}
1260
1261 static int
1262luaV_blob_add(lua_State *L)
1263{
1264 luaV_Blob *blo = luaV_checkudata(L, 1, LUAVIM_BLOB);
1265 blob_T *b = (blob_T *) luaV_checkcache(L, (void *) *blo);
1266 if (b->bv_lock)
1267 luaL_error(L, "blob is locked");
1268 lua_settop(L, 2);
1269 if (!lua_isstring(L, 2))
1270 luaL_error(L, "string expected, got %s", luaL_typename(L, 2));
1271 else
1272 {
1273 size_t i, l = 0;
1274 const char *s = lua_tolstring(L, 2, &l);
1275
Bram Moolenaar5f1d3ae2020-02-11 22:37:35 +01001276 if (ga_grow(&b->bv_ga, (int)l) == OK)
Bram Moolenaar6fb5c972019-03-26 21:44:20 +01001277 for (i = 0; i < l; ++i)
1278 ga_append(&b->bv_ga, s[i]);
Bram Moolenaarb7828692019-03-23 13:57:02 +01001279 }
1280 lua_settop(L, 1);
1281 return 1;
1282}
1283
1284static const luaL_Reg luaV_Blob_mt[] = {
1285 {"__tostring", luaV_blob_tostring},
1286 {"__gc", luaV_blob_gc},
1287 {"__len", luaV_blob_len},
1288 {"__index", luaV_blob_index},
1289 {"__newindex", luaV_blob_newindex},
1290 {"add", luaV_blob_add},
1291 {NULL, NULL}
1292};
1293
1294
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001295// ======= Funcref type =======
Bram Moolenaarca06da92018-07-01 15:12:05 +02001296
1297 static luaV_Funcref *
1298luaV_newfuncref(lua_State *L, char_u *name)
1299{
1300 luaV_Funcref *f = (luaV_Funcref *)lua_newuserdata(L, sizeof(luaV_Funcref));
1301
1302 if (name != NULL)
1303 {
Bram Moolenaar4eefe472019-03-19 21:59:19 +01001304 func_ref(name);
1305 f->name = vim_strsave(name);
Bram Moolenaarca06da92018-07-01 15:12:05 +02001306 }
Bram Moolenaarca06da92018-07-01 15:12:05 +02001307 f->self = NULL;
1308 luaV_getfield(L, LUAVIM_FUNCREF);
1309 lua_setmetatable(L, -2);
1310 return f;
1311}
1312
1313 static luaV_Funcref *
Bram Moolenaar4eefe472019-03-19 21:59:19 +01001314luaV_pushfuncref(lua_State *L, char_u *name)
Bram Moolenaarca06da92018-07-01 15:12:05 +02001315{
Bram Moolenaar4eefe472019-03-19 21:59:19 +01001316 return luaV_newfuncref(L, name);
Bram Moolenaarca06da92018-07-01 15:12:05 +02001317}
1318
1319
1320luaV_type_tostring(funcref, LUAVIM_FUNCREF)
1321
1322 static int
1323luaV_funcref_gc(lua_State *L)
1324{
1325 luaV_Funcref *f = (luaV_Funcref *) lua_touserdata(L, 1);
1326
Bram Moolenaar4eefe472019-03-19 21:59:19 +01001327 func_unref(f->name);
1328 vim_free(f->name);
1329 // NOTE: Don't call "dict_unref(f->self)", because the dict of "f->self"
1330 // will be (or has been already) freed by Vim's garbage collection.
Bram Moolenaarca06da92018-07-01 15:12:05 +02001331 return 0;
1332}
1333
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001334// equivalent to string(funcref)
Bram Moolenaarca06da92018-07-01 15:12:05 +02001335 static int
1336luaV_funcref_len(lua_State *L)
1337{
1338 luaV_Funcref *f = (luaV_Funcref *) lua_touserdata(L, 1);
1339
Bram Moolenaar4eefe472019-03-19 21:59:19 +01001340 lua_pushstring(L, (const char *) f->name);
Bram Moolenaarca06da92018-07-01 15:12:05 +02001341 return 1;
1342}
1343
1344 static int
1345luaV_funcref_call(lua_State *L)
1346{
1347 luaV_Funcref *f = (luaV_Funcref *) lua_touserdata(L, 1);
Bram Moolenaar4eefe472019-03-19 21:59:19 +01001348 int i, n = lua_gettop(L) - 1; // #args
1349 int status = FAIL;
1350 typval_T args;
1351 typval_T rettv;
Bram Moolenaarca06da92018-07-01 15:12:05 +02001352
Bram Moolenaar4eefe472019-03-19 21:59:19 +01001353 args.v_type = VAR_LIST;
1354 args.vval.v_list = list_alloc();
1355 rettv.v_type = VAR_UNKNOWN; // as in clear_tv
1356 if (args.vval.v_list != NULL)
Bram Moolenaard6ef5f92018-07-13 22:08:23 +02001357 {
Bram Moolenaar4eefe472019-03-19 21:59:19 +01001358 typval_T v;
1359
Bram Moolenaar17413672018-07-14 20:49:42 +02001360 for (i = 0; i < n; i++)
1361 {
Bram Moolenaard6ef5f92018-07-13 22:08:23 +02001362 luaV_checktypval(L, i + 2, &v, "calling funcref");
Bram Moolenaar4eefe472019-03-19 21:59:19 +01001363 list_append_tv(args.vval.v_list, &v);
Bram Moolenaar713bf9e92019-03-16 16:38:41 +01001364 clear_tv(&v);
Bram Moolenaard6ef5f92018-07-13 22:08:23 +02001365 }
Bram Moolenaar4eefe472019-03-19 21:59:19 +01001366 status = func_call(f->name, &args, NULL, f->self, &rettv);
Bram Moolenaard6ef5f92018-07-13 22:08:23 +02001367 if (status == OK)
1368 luaV_pushtypval(L, &rettv);
Bram Moolenaar4eefe472019-03-19 21:59:19 +01001369 clear_tv(&args);
Bram Moolenaard6ef5f92018-07-13 22:08:23 +02001370 clear_tv(&rettv);
Bram Moolenaarca06da92018-07-01 15:12:05 +02001371 }
Bram Moolenaarca06da92018-07-01 15:12:05 +02001372 if (status != OK)
1373 luaL_error(L, "cannot call funcref");
1374 return 1;
1375}
1376
1377static const luaL_Reg luaV_Funcref_mt[] = {
1378 {"__tostring", luaV_funcref_tostring},
1379 {"__gc", luaV_funcref_gc},
1380 {"__len", luaV_funcref_len},
1381 {"__call", luaV_funcref_call},
1382 {NULL, NULL}
1383};
1384
1385
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001386// ======= Buffer type =======
Bram Moolenaar1dced572012-04-05 16:54:08 +02001387
1388luaV_newtype(buf_T, buffer, luaV_Buffer, LUAVIM_BUFFER)
1389luaV_pushtype(buf_T, buffer, luaV_Buffer)
1390luaV_type_tostring(buffer, LUAVIM_BUFFER)
1391
1392 static int
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001393luaV_buffer_len(lua_State *L)
1394{
Bram Moolenaar1dced572012-04-05 16:54:08 +02001395 buf_T *b = (buf_T *) luaV_checkvalid(L, luaV_Buffer, 1);
1396 lua_pushinteger(L, b->b_ml.ml_line_count);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001397 return 1;
1398}
1399
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001400 static int
1401luaV_buffer_call(lua_State *L)
1402{
Bram Moolenaar1dced572012-04-05 16:54:08 +02001403 buf_T *b = (buf_T *) luaV_checkvalid(L, luaV_Buffer, 1);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001404 lua_settop(L, 1);
Bram Moolenaar1dced572012-04-05 16:54:08 +02001405 set_curbuf(b, DOBUF_SPLIT);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001406 return 1;
1407}
1408
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001409 static int
1410luaV_buffer_index(lua_State *L)
1411{
Bram Moolenaar1dced572012-04-05 16:54:08 +02001412 buf_T *b = (buf_T *) luaV_checkvalid(L, luaV_Buffer, 1);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001413 linenr_T n = (linenr_T) lua_tointeger(L, 2);
Bram Moolenaar1dced572012-04-05 16:54:08 +02001414 if (n > 0 && n <= b->b_ml.ml_line_count)
1415 luaV_pushline(L, b, n);
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001416 else if (lua_isstring(L, 2))
1417 {
1418 const char *s = lua_tostring(L, 2);
1419 if (strncmp(s, "name", 4) == 0)
Bram Moolenaarfe08df42018-07-07 23:07:41 +02001420 lua_pushstring(L, (b->b_sfname == NULL)
1421 ? "" : (char *) b->b_sfname);
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001422 else if (strncmp(s, "fname", 5) == 0)
Bram Moolenaarfe08df42018-07-07 23:07:41 +02001423 lua_pushstring(L, (b->b_ffname == NULL)
1424 ? "" : (char *) b->b_ffname);
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001425 else if (strncmp(s, "number", 6) == 0)
Bram Moolenaar1dced572012-04-05 16:54:08 +02001426 lua_pushinteger(L, b->b_fnum);
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001427 // methods
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001428 else if (strncmp(s, "insert", 6) == 0
1429 || strncmp(s, "next", 4) == 0
1430 || strncmp(s, "previous", 8) == 0
1431 || strncmp(s, "isvalid", 7) == 0)
1432 {
1433 lua_getmetatable(L, 1);
1434 lua_getfield(L, -1, s);
1435 }
1436 else
1437 lua_pushnil(L);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001438 }
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001439 else
1440 lua_pushnil(L);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001441 return 1;
1442}
1443
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001444 static int
1445luaV_buffer_newindex(lua_State *L)
1446{
Bram Moolenaar1dced572012-04-05 16:54:08 +02001447 buf_T *b = (buf_T *) luaV_checkvalid(L, luaV_Buffer, 1);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001448 linenr_T n = (linenr_T) luaL_checkinteger(L, 2);
1449#ifdef HAVE_SANDBOX
1450 luaV_checksandbox(L);
1451#endif
Bram Moolenaar1dced572012-04-05 16:54:08 +02001452 if (n < 1 || n > b->b_ml.ml_line_count)
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001453 luaL_error(L, "invalid line number");
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001454 if (lua_isnil(L, 3)) // delete line
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001455 {
1456 buf_T *buf = curbuf;
Bram Moolenaar1dced572012-04-05 16:54:08 +02001457 curbuf = b;
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001458 if (u_savedel(n, 1L) == FAIL)
1459 {
1460 curbuf = buf;
1461 luaL_error(L, "cannot save undo information");
1462 }
Bram Moolenaarca70c072020-05-30 20:30:46 +02001463 else if (ml_delete(n) == FAIL)
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001464 {
1465 curbuf = buf;
1466 luaL_error(L, "cannot delete line");
1467 }
Bram Moolenaarca06da92018-07-01 15:12:05 +02001468 else
1469 {
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001470 deleted_lines_mark(n, 1L);
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001471 if (b == curwin->w_buffer) // fix cursor in current window?
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001472 {
1473 if (curwin->w_cursor.lnum >= n)
1474 {
1475 if (curwin->w_cursor.lnum > n)
1476 {
1477 curwin->w_cursor.lnum -= 1;
1478 check_cursor_col();
1479 }
Bram Moolenaare49b8e82020-07-01 13:52:55 +02001480 else
1481 check_cursor();
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001482 changed_cline_bef_curs();
1483 }
1484 invalidate_botline();
1485 }
1486 }
1487 curbuf = buf;
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001488 }
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001489 else if (lua_isstring(L, 3)) // update line
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001490 {
1491 buf_T *buf = curbuf;
Bram Moolenaar1dced572012-04-05 16:54:08 +02001492 curbuf = b;
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001493 if (u_savesub(n) == FAIL)
1494 {
1495 curbuf = buf;
1496 luaL_error(L, "cannot save undo information");
1497 }
1498 else if (ml_replace(n, luaV_toline(L, 3), TRUE) == FAIL)
1499 {
1500 curbuf = buf;
1501 luaL_error(L, "cannot replace line");
1502 }
Bram Moolenaar113d9de2022-08-08 15:49:18 +01001503 else
1504 changed_bytes(n, 0);
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001505 curbuf = buf;
Bram Moolenaar1dced572012-04-05 16:54:08 +02001506 if (b == curwin->w_buffer)
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001507 check_cursor_col();
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001508 }
1509 else
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001510 luaL_error(L, "wrong argument to change line");
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001511 return 0;
1512}
1513
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001514 static int
1515luaV_buffer_insert(lua_State *L)
1516{
Bram Moolenaar1dced572012-04-05 16:54:08 +02001517 luaV_Buffer *lb = luaV_checkudata(L, 1, LUAVIM_BUFFER);
1518 buf_T *b = (buf_T *) luaV_checkcache(L, (void *) *lb);
1519 linenr_T last = b->b_ml.ml_line_count;
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001520 linenr_T n = (linenr_T) luaL_optinteger(L, 3, last);
1521 buf_T *buf;
1522 luaL_checktype(L, 2, LUA_TSTRING);
1523#ifdef HAVE_SANDBOX
1524 luaV_checksandbox(L);
1525#endif
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001526 // fix insertion line
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001527 if (n < 0) n = 0;
1528 if (n > last) n = last;
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001529 // insert
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001530 buf = curbuf;
Bram Moolenaar1dced572012-04-05 16:54:08 +02001531 curbuf = b;
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001532 if (u_save(n, n + 1) == FAIL)
1533 {
1534 curbuf = buf;
1535 luaL_error(L, "cannot save undo information");
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001536 }
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001537 else if (ml_append(n, luaV_toline(L, 2), 0, FALSE) == FAIL)
1538 {
1539 curbuf = buf;
1540 luaL_error(L, "cannot insert line");
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001541 }
1542 else
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001543 appended_lines_mark(n, 1L);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001544 curbuf = buf;
Bram Moolenaara4d158b2022-08-14 14:17:45 +01001545 update_screen(UPD_VALID);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001546 return 0;
1547}
1548
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001549 static int
1550luaV_buffer_next(lua_State *L)
1551{
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001552 luaV_Buffer *b = luaV_checkudata(L, 1, LUAVIM_BUFFER);
Bram Moolenaar1dced572012-04-05 16:54:08 +02001553 buf_T *buf = (buf_T *) luaV_checkcache(L, (void *) *b);
1554 luaV_pushbuffer(L, buf->b_next);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001555 return 1;
1556}
1557
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001558 static int
1559luaV_buffer_previous(lua_State *L)
1560{
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001561 luaV_Buffer *b = luaV_checkudata(L, 1, LUAVIM_BUFFER);
Bram Moolenaar1dced572012-04-05 16:54:08 +02001562 buf_T *buf = (buf_T *) luaV_checkcache(L, (void *) *b);
1563 luaV_pushbuffer(L, buf->b_prev);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001564 return 1;
1565}
1566
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001567 static int
1568luaV_buffer_isvalid(lua_State *L)
1569{
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001570 luaV_Buffer *b = luaV_checkudata(L, 1, LUAVIM_BUFFER);
Bram Moolenaar1dced572012-04-05 16:54:08 +02001571 luaV_getudata(L, *b);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001572 lua_pushboolean(L, !lua_isnil(L, -1));
1573 return 1;
1574}
1575
1576static const luaL_Reg luaV_Buffer_mt[] = {
1577 {"__tostring", luaV_buffer_tostring},
1578 {"__len", luaV_buffer_len},
1579 {"__call", luaV_buffer_call},
1580 {"__index", luaV_buffer_index},
1581 {"__newindex", luaV_buffer_newindex},
1582 {"insert", luaV_buffer_insert},
1583 {"next", luaV_buffer_next},
1584 {"previous", luaV_buffer_previous},
1585 {"isvalid", luaV_buffer_isvalid},
1586 {NULL, NULL}
1587};
1588
1589
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001590// ======= Window type =======
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001591
Bram Moolenaar1dced572012-04-05 16:54:08 +02001592luaV_newtype(win_T, window, luaV_Window, LUAVIM_WINDOW)
1593luaV_pushtype(win_T, window, luaV_Window)
1594luaV_type_tostring(window, LUAVIM_WINDOW)
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001595
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001596 static int
1597luaV_window_call(lua_State *L)
1598{
Bram Moolenaar1dced572012-04-05 16:54:08 +02001599 win_T *w = (win_T *) luaV_checkvalid(L, luaV_Window, 1);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001600 lua_settop(L, 1);
Bram Moolenaar1dced572012-04-05 16:54:08 +02001601 win_goto(w);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001602 return 1;
1603}
1604
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001605 static int
1606luaV_window_index(lua_State *L)
1607{
Bram Moolenaar1dced572012-04-05 16:54:08 +02001608 win_T *w = (win_T *) luaV_checkvalid(L, luaV_Window, 1);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001609 const char *s = luaL_checkstring(L, 2);
1610 if (strncmp(s, "buffer", 6) == 0)
Bram Moolenaar1dced572012-04-05 16:54:08 +02001611 luaV_pushbuffer(L, w->w_buffer);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001612 else if (strncmp(s, "line", 4) == 0)
Bram Moolenaar1dced572012-04-05 16:54:08 +02001613 lua_pushinteger(L, w->w_cursor.lnum);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001614 else if (strncmp(s, "col", 3) == 0)
Bram Moolenaar1dced572012-04-05 16:54:08 +02001615 lua_pushinteger(L, w->w_cursor.col + 1);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001616 else if (strncmp(s, "width", 5) == 0)
Bram Moolenaar02631462017-09-22 15:20:32 +02001617 lua_pushinteger(L, w->w_width);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001618 else if (strncmp(s, "height", 6) == 0)
Bram Moolenaar1dced572012-04-05 16:54:08 +02001619 lua_pushinteger(L, w->w_height);
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001620 // methods
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001621 else if (strncmp(s, "next", 4) == 0
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001622 || strncmp(s, "previous", 8) == 0
1623 || strncmp(s, "isvalid", 7) == 0)
1624 {
1625 lua_getmetatable(L, 1);
1626 lua_getfield(L, -1, s);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001627 }
1628 else
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001629 lua_pushnil(L);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001630 return 1;
1631}
1632
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001633 static int
Bram Moolenaar4eefe472019-03-19 21:59:19 +01001634luaV_window_newindex(lua_State *L)
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001635{
Bram Moolenaar1dced572012-04-05 16:54:08 +02001636 win_T *w = (win_T *) luaV_checkvalid(L, luaV_Window, 1);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001637 const char *s = luaL_checkstring(L, 2);
1638 int v = luaL_checkinteger(L, 3);
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001639 if (strncmp(s, "line", 4) == 0)
1640 {
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001641#ifdef HAVE_SANDBOX
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001642 luaV_checksandbox(L);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001643#endif
Bram Moolenaar1dced572012-04-05 16:54:08 +02001644 if (v < 1 || v > w->w_buffer->b_ml.ml_line_count)
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001645 luaL_error(L, "line out of range");
Bram Moolenaar1dced572012-04-05 16:54:08 +02001646 w->w_cursor.lnum = v;
Bram Moolenaara4d158b2022-08-14 14:17:45 +01001647 update_screen(UPD_VALID);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001648 }
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001649 else if (strncmp(s, "col", 3) == 0)
1650 {
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001651#ifdef HAVE_SANDBOX
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001652 luaV_checksandbox(L);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001653#endif
Bram Moolenaar1dced572012-04-05 16:54:08 +02001654 w->w_cursor.col = v - 1;
Bram Moolenaar53901442018-07-25 22:02:36 +02001655 w->w_set_curswant = TRUE;
Bram Moolenaara4d158b2022-08-14 14:17:45 +01001656 update_screen(UPD_VALID);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001657 }
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001658 else if (strncmp(s, "width", 5) == 0)
1659 {
1660 win_T *win = curwin;
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001661#ifdef FEAT_GUI
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001662 need_mouse_correct = TRUE;
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001663#endif
Bram Moolenaar1dced572012-04-05 16:54:08 +02001664 curwin = w;
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001665 win_setwidth(v);
1666 curwin = win;
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001667 }
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001668 else if (strncmp(s, "height", 6) == 0)
1669 {
1670 win_T *win = curwin;
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001671#ifdef FEAT_GUI
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001672 need_mouse_correct = TRUE;
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001673#endif
Bram Moolenaar1dced572012-04-05 16:54:08 +02001674 curwin = w;
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001675 win_setheight(v);
1676 curwin = win;
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001677 }
1678 else
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001679 luaL_error(L, "invalid window property: `%s'", s);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001680 return 0;
1681}
1682
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001683 static int
1684luaV_window_next(lua_State *L)
1685{
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001686 luaV_Window *w = luaV_checkudata(L, 1, LUAVIM_WINDOW);
Bram Moolenaar1dced572012-04-05 16:54:08 +02001687 win_T *win = (win_T *) luaV_checkcache(L, (void *) *w);
1688 luaV_pushwindow(L, win->w_next);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001689 return 1;
1690}
1691
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001692 static int
1693luaV_window_previous(lua_State *L)
1694{
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001695 luaV_Window *w = luaV_checkudata(L, 1, LUAVIM_WINDOW);
Bram Moolenaar1dced572012-04-05 16:54:08 +02001696 win_T *win = (win_T *) luaV_checkcache(L, (void *) *w);
1697 luaV_pushwindow(L, win->w_prev);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001698 return 1;
1699}
1700
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001701 static int
1702luaV_window_isvalid(lua_State *L)
1703{
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001704 luaV_Window *w = luaV_checkudata(L, 1, LUAVIM_WINDOW);
Bram Moolenaar1dced572012-04-05 16:54:08 +02001705 luaV_getudata(L, *w);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001706 lua_pushboolean(L, !lua_isnil(L, -1));
1707 return 1;
1708}
1709
1710static const luaL_Reg luaV_Window_mt[] = {
1711 {"__tostring", luaV_window_tostring},
1712 {"__call", luaV_window_call},
1713 {"__index", luaV_window_index},
1714 {"__newindex", luaV_window_newindex},
1715 {"next", luaV_window_next},
1716 {"previous", luaV_window_previous},
1717 {"isvalid", luaV_window_isvalid},
1718 {NULL, NULL}
1719};
1720
1721
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001722// ======= Vim module =======
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001723
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001724 static int
1725luaV_print(lua_State *L)
1726{
Yegappan Lakshmanan41114a22021-07-29 20:22:14 +02001727 int i, n = lua_gettop(L); // nargs
1728 const char *s;
1729 size_t l;
1730 garray_T msg_ga;
1731
1732 ga_init2(&msg_ga, 1, 128);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001733 lua_getglobal(L, "tostring");
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001734 for (i = 1; i <= n; i++)
1735 {
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001736 lua_pushvalue(L, -1); // tostring
1737 lua_pushvalue(L, i); // arg
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001738 lua_call(L, 1, 1);
1739 s = lua_tolstring(L, -1, &l);
1740 if (s == NULL)
1741 return luaL_error(L, "cannot convert to string");
Bram Moolenaar78e006b2021-07-28 15:07:01 +02001742 if (i > 1)
Yegappan Lakshmanan41114a22021-07-29 20:22:14 +02001743 ga_append(&msg_ga, ' '); // use space instead of tab
1744 ga_concat_len(&msg_ga, (char_u *)s, l);
Bram Moolenaar2a4bd002021-07-28 21:48:59 +02001745 lua_pop(L, 1);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001746 }
Yegappan Lakshmanan41114a22021-07-29 20:22:14 +02001747 // Replace any "\n" with "\0"
1748 for (i = 0; i < msg_ga.ga_len; i++)
1749 if (((char *)msg_ga.ga_data)[i] == '\n')
1750 ((char *)msg_ga.ga_data)[i] = '\0';
1751 lua_pushlstring(L, msg_ga.ga_data, msg_ga.ga_len);
Bram Moolenaarb98678a2019-10-19 15:18:44 +02001752 if (!got_int)
1753 luaV_msg(L);
Yegappan Lakshmanan41114a22021-07-29 20:22:14 +02001754
1755 ga_clear(&msg_ga);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001756 return 0;
1757}
1758
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001759 static int
Bram Moolenaar38e2b062011-09-21 17:15:39 +02001760luaV_debug(lua_State *L)
1761{
1762 lua_settop(L, 0);
1763 lua_getglobal(L, "vim");
1764 lua_getfield(L, -1, "eval");
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001765 lua_remove(L, -2); // vim.eval at position 1
Bram Moolenaar38e2b062011-09-21 17:15:39 +02001766 for (;;)
1767 {
1768 const char *input;
1769 size_t l;
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001770 lua_pushvalue(L, 1); // vim.eval
Bram Moolenaar38e2b062011-09-21 17:15:39 +02001771 lua_pushliteral(L, "input('lua_debug> ')");
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001772 lua_call(L, 1, 1); // return string
Bram Moolenaar38e2b062011-09-21 17:15:39 +02001773 input = lua_tolstring(L, -1, &l);
1774 if (l == 0 || strcmp(input, "cont") == 0)
1775 return 0;
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001776 msg_putchar('\n'); // avoid outputting on input line
Bram Moolenaar38e2b062011-09-21 17:15:39 +02001777 if (luaL_loadbuffer(L, input, l, "=(debug command)")
1778 || lua_pcall(L, 0, 0, 0))
1779 luaV_emsg(L);
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001780 lua_settop(L, 1); // remove eventual returns, but keep vim.eval
Bram Moolenaar38e2b062011-09-21 17:15:39 +02001781 }
1782}
1783
Yegappan Lakshmanan9dc4bef2021-08-04 21:12:52 +02001784 static dict_T *
1785luaV_get_var_scope(lua_State *L)
1786{
1787 const char *scope = luaL_checkstring(L, 1);
1788 dict_T *dict = NULL;
1789
1790 if (STRICMP((char *)scope, "g") == 0)
1791 dict = get_globvar_dict();
1792 else if (STRICMP((char *)scope, "v") == 0)
1793 dict = get_vimvar_dict();
1794 else if (STRICMP((char *)scope, "b") == 0)
1795 dict = curbuf->b_vars;
1796 else if (STRICMP((char *)scope, "w") == 0)
1797 dict = curwin->w_vars;
1798 else if (STRICMP((char *)scope, "t") == 0)
1799 dict = curtab->tp_vars;
1800 else
1801 {
1802 luaL_error(L, "invalid scope %s", scope);
1803 return NULL;
1804 }
1805
1806 return dict;
1807}
1808
1809 static int
1810luaV_setvar(lua_State *L)
1811{
1812 dict_T *dict;
1813 dictitem_T *di;
1814 size_t len;
1815 char *name;
1816 int del;
1817 char *error = NULL;
1818
1819 name = (char *)luaL_checklstring(L, 3, &len);
1820 del = (lua_gettop(L) < 4) || lua_isnil(L, 4);
1821
1822 dict = luaV_get_var_scope(L);
1823 if (dict == NULL)
1824 return 0;
1825
Yegappan Lakshmanan11328bc2021-08-06 21:34:38 +02001826 di = dict_find(dict, (char_u *)name, (int)len);
Yegappan Lakshmanan9dc4bef2021-08-04 21:12:52 +02001827 if (di != NULL)
1828 {
1829 if (di->di_flags & DI_FLAGS_RO)
1830 error = "variable is read-only";
1831 else if (di->di_flags & DI_FLAGS_LOCK)
1832 error = "variable is locked";
1833 else if (del && di->di_flags & DI_FLAGS_FIX)
1834 error = "variable is fixed";
1835 if (error != NULL)
1836 return luaL_error(L, error);
1837 }
1838 else if (dict->dv_lock)
1839 return luaL_error(L, "Dictionary is locked");
1840
1841 if (del)
1842 {
1843 // Delete the key
1844 if (di == NULL)
1845 // Doesn't exist, nothing to do
1846 return 0;
Bram Moolenaaref2c3252022-11-25 16:31:51 +00001847 // Delete the entry
1848 dictitem_remove(dict, di, "Lua delete variable");
Yegappan Lakshmanan9dc4bef2021-08-04 21:12:52 +02001849 }
1850 else
1851 {
1852 // Update the key
1853 typval_T tv;
1854
h-east965d2ed2021-10-04 21:51:57 +01001855 // Convert the lua value to a Vim script type in the temporary variable
Yegappan Lakshmanan9dc4bef2021-08-04 21:12:52 +02001856 lua_pushvalue(L, 4);
1857 if (luaV_totypval(L, -1, &tv) == FAIL)
1858 return luaL_error(L, "Couldn't convert lua value");
1859
1860 if (di == NULL)
1861 {
1862 // Need to create an entry
1863 di = dictitem_alloc((char_u *)name);
1864 if (di == NULL)
Bram Moolenaar1b6acf02021-08-05 16:47:08 +02001865 {
1866 clear_tv(&tv);
Yegappan Lakshmanan9dc4bef2021-08-04 21:12:52 +02001867 return 0;
Bram Moolenaar1b6acf02021-08-05 16:47:08 +02001868 }
Yegappan Lakshmanan9dc4bef2021-08-04 21:12:52 +02001869 // Update the value
1870 copy_tv(&tv, &di->di_tv);
Bram Moolenaar4a011592021-08-05 15:11:08 +02001871 if (dict_add(dict, di) == FAIL)
Bram Moolenaar1b6acf02021-08-05 16:47:08 +02001872 {
1873 dictitem_free(di);
1874 clear_tv(&tv);
Bram Moolenaar4a011592021-08-05 15:11:08 +02001875 return luaL_error(L, "Couldn't add to dictionary");
Bram Moolenaar1b6acf02021-08-05 16:47:08 +02001876 }
1877 }
1878 else
Yegappan Lakshmanan9dc4bef2021-08-04 21:12:52 +02001879 {
1880 // Clear the old value
1881 clear_tv(&di->di_tv);
1882 // Update the value
1883 copy_tv(&tv, &di->di_tv);
1884 }
1885
1886 // Clear the temporary variable
1887 clear_tv(&tv);
1888 }
1889
1890 return 0;
1891}
1892
1893 static int
1894luaV_getvar(lua_State *L)
1895{
1896 dict_T *dict = luaV_get_var_scope(L);
1897 size_t len;
1898 const char *name = luaL_checklstring(L, 3, &len);
Yegappan Lakshmanan11328bc2021-08-06 21:34:38 +02001899 dictitem_T *di = dict_find(dict, (char_u *)name, (int)len);
Yegappan Lakshmanan9dc4bef2021-08-04 21:12:52 +02001900
Yegappan Lakshmanan9dc4bef2021-08-04 21:12:52 +02001901 if (di == NULL)
1902 return 0; // nil
1903
1904 luaV_pushtypval(L, &di->di_tv);
1905 return 1;
1906}
1907
Bram Moolenaar38e2b062011-09-21 17:15:39 +02001908 static int
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001909luaV_command(lua_State *L)
1910{
Yegappan Lakshmanan11328bc2021-08-06 21:34:38 +02001911 char_u *s = vim_strsave((char_u *)luaL_checkstring(L, 1));
1912
1913 execute_cmds_from_string(s);
1914 vim_free(s);
Bram Moolenaara4d158b2022-08-14 14:17:45 +01001915 update_screen(UPD_VALID);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001916 return 0;
1917}
1918
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001919 static int
1920luaV_eval(lua_State *L)
1921{
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001922 typval_T *tv = eval_expr((char_u *) luaL_checkstring(L, 1), NULL);
Yegappan Lakshmanan11328bc2021-08-06 21:34:38 +02001923
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001924 if (tv == NULL) luaL_error(L, "invalid expression");
1925 luaV_pushtypval(L, tv);
Bram Moolenaarb3766472013-04-15 13:49:21 +02001926 free_tv(tv);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001927 return 1;
1928}
1929
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001930 static int
Bram Moolenaar0d2e4fc2010-07-18 12:35:47 +02001931luaV_beep(lua_State *L UNUSED)
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001932{
Bram Moolenaar165bc692015-07-21 17:53:25 +02001933 vim_beep(BO_LANG);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001934 return 0;
1935}
1936
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001937 static int
1938luaV_line(lua_State *L)
1939{
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001940 luaV_pushline(L, curbuf, curwin->w_cursor.lnum);
1941 return 1;
1942}
1943
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001944 static int
Bram Moolenaar1dced572012-04-05 16:54:08 +02001945luaV_list(lua_State *L)
1946{
Bram Moolenaarca06da92018-07-01 15:12:05 +02001947 list_T *l;
1948 int initarg = !lua_isnoneornil(L, 1);
1949
1950 if (initarg && lua_type(L, 1) != LUA_TTABLE)
1951 luaL_error(L, "table expected, got %s", luaL_typename(L, 1));
Yegappan Lakshmanan0233bdf2023-01-12 12:33:30 +00001952
Bram Moolenaarca06da92018-07-01 15:12:05 +02001953 l = list_alloc();
Bram Moolenaar1dced572012-04-05 16:54:08 +02001954 if (l == NULL)
Bram Moolenaarca06da92018-07-01 15:12:05 +02001955 {
Yegappan Lakshmanan0233bdf2023-01-12 12:33:30 +00001956 lua_pushnil(L);
1957 return 1;
Bram Moolenaarca06da92018-07-01 15:12:05 +02001958 }
Yegappan Lakshmanan0233bdf2023-01-12 12:33:30 +00001959
1960 luaV_newlist(L, l);
1961 if (!initarg)
1962 return 1;
1963
1964 // traverse table to init list
1965 int notnil, i = 0;
1966 typval_T v;
1967 do
1968 {
1969 lua_rawgeti(L, 1, ++i);
1970 notnil = !lua_isnil(L, -1);
1971 if (notnil)
1972 {
1973 luaV_checktypval(L, -1, &v, "vim.list");
1974 list_append_tv(l, &v);
1975 clear_tv(&v);
1976 }
1977 lua_pop(L, 1); // value
1978 } while (notnil);
1979
Bram Moolenaar1dced572012-04-05 16:54:08 +02001980 return 1;
1981}
1982
1983 static int
1984luaV_dict(lua_State *L)
1985{
Bram Moolenaarca06da92018-07-01 15:12:05 +02001986 dict_T *d;
1987 int initarg = !lua_isnoneornil(L, 1);
1988
1989 if (initarg && lua_type(L, 1) != LUA_TTABLE)
1990 luaL_error(L, "table expected, got %s", luaL_typename(L, 1));
Yegappan Lakshmanan0233bdf2023-01-12 12:33:30 +00001991
Bram Moolenaarca06da92018-07-01 15:12:05 +02001992 d = dict_alloc();
Bram Moolenaar1dced572012-04-05 16:54:08 +02001993 if (d == NULL)
Bram Moolenaarca06da92018-07-01 15:12:05 +02001994 {
Yegappan Lakshmanan0233bdf2023-01-12 12:33:30 +00001995 lua_pushnil(L);
1996 return 1;
1997 }
1998
1999 luaV_newdict(L, d);
2000 if (!initarg)
2001 return 1;
2002
2003 // traverse table to init dict
2004 lua_pushnil(L);
2005 while (lua_next(L, 1))
2006 {
2007 char_u *key;
2008 dictitem_T *di;
2009 typval_T v;
2010
2011 lua_pushvalue(L, -2); // dup key in case it's a number
2012 key = (char_u *) lua_tostring(L, -1);
2013 if (key == NULL)
Bram Moolenaarca06da92018-07-01 15:12:05 +02002014 {
2015 lua_pushnil(L);
Yegappan Lakshmanan0233bdf2023-01-12 12:33:30 +00002016 return 1;
Bram Moolenaarca06da92018-07-01 15:12:05 +02002017 }
Yegappan Lakshmanan0233bdf2023-01-12 12:33:30 +00002018 if (*key == NUL)
2019 luaL_error(L, "table has empty key");
2020 luaV_checktypval(L, -2, &v, "vim.dict"); // value
2021 di = dictitem_alloc(key);
2022 if (di == NULL || dict_add(d, di) == FAIL)
2023 {
2024 vim_free(di);
2025 lua_pushnil(L);
2026 return 1;
2027 }
2028 di->di_tv = v;
2029 lua_pop(L, 2); // key copy and value
Bram Moolenaarca06da92018-07-01 15:12:05 +02002030 }
Yegappan Lakshmanan0233bdf2023-01-12 12:33:30 +00002031
Bram Moolenaarca06da92018-07-01 15:12:05 +02002032 return 1;
2033}
2034
2035 static int
Bram Moolenaarb7828692019-03-23 13:57:02 +01002036luaV_blob(lua_State *L)
2037{
2038 blob_T *b;
2039 int initarg = !lua_isnoneornil(L, 1);
2040
2041 if (initarg && !lua_isstring(L, 1))
2042 luaL_error(L, "string expected, got %s", luaL_typename(L, 1));
Yegappan Lakshmanan0233bdf2023-01-12 12:33:30 +00002043
Bram Moolenaarb7828692019-03-23 13:57:02 +01002044 b = blob_alloc();
2045 if (b == NULL)
Bram Moolenaarb7828692019-03-23 13:57:02 +01002046 {
Yegappan Lakshmanan0233bdf2023-01-12 12:33:30 +00002047 lua_pushnil(L);
2048 return 1;
Bram Moolenaarb7828692019-03-23 13:57:02 +01002049 }
Yegappan Lakshmanan0233bdf2023-01-12 12:33:30 +00002050
2051 luaV_newblob(L, b);
2052 if (!initarg)
2053 return 1;
2054
2055 // traverse table to init blob
2056 size_t i, l = 0;
2057 const char *s = lua_tolstring(L, 1, &l);
2058
2059 if (ga_grow(&b->bv_ga, (int)l) == OK)
2060 for (i = 0; i < l; ++i)
2061 ga_append(&b->bv_ga, s[i]);
2062
Bram Moolenaarb7828692019-03-23 13:57:02 +01002063 return 1;
2064}
2065
2066 static int
Bram Moolenaarca06da92018-07-01 15:12:05 +02002067luaV_funcref(lua_State *L)
2068{
2069 const char *name = luaL_checkstring(L, 1);
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002070 // note: not checking if function exists (needs function_exists)
Bram Moolenaarca06da92018-07-01 15:12:05 +02002071 if (name == NULL || *name == NUL || VIM_ISDIGIT(*name))
2072 luaL_error(L, "invalid function name: %s", name);
2073 luaV_newfuncref(L, (char_u *) name);
Bram Moolenaar1dced572012-04-05 16:54:08 +02002074 return 1;
2075}
2076
2077 static int
Bram Moolenaar55d5c032010-07-17 23:52:29 +02002078luaV_buffer(lua_State *L)
2079{
Bram Moolenaar0ba04292010-07-14 23:23:17 +02002080 buf_T *buf;
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002081 if (lua_isstring(L, 1)) // get by number or name?
Bram Moolenaar55d5c032010-07-17 23:52:29 +02002082 {
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002083 if (lua_isnumber(L, 1)) // by number?
Bram Moolenaar55d5c032010-07-17 23:52:29 +02002084 {
2085 int n = lua_tointeger(L, 1);
Bram Moolenaar29323592016-07-24 22:04:11 +02002086 FOR_ALL_BUFFERS(buf)
Bram Moolenaar55d5c032010-07-17 23:52:29 +02002087 if (buf->b_fnum == n) break;
2088 }
Bram Moolenaarca06da92018-07-01 15:12:05 +02002089 else // by name
2090 {
Bram Moolenaar55d5c032010-07-17 23:52:29 +02002091 size_t l;
2092 const char *s = lua_tolstring(L, 1, &l);
Bram Moolenaar29323592016-07-24 22:04:11 +02002093 FOR_ALL_BUFFERS(buf)
Bram Moolenaar55d5c032010-07-17 23:52:29 +02002094 {
2095 if (buf->b_ffname == NULL || buf->b_sfname == NULL)
2096 {
2097 if (l == 0) break;
2098 }
Bram Moolenaar0d2e4fc2010-07-18 12:35:47 +02002099 else if (strncmp(s, (char *)buf->b_ffname, l) == 0
2100 || strncmp(s, (char *)buf->b_sfname, l) == 0)
Bram Moolenaar55d5c032010-07-17 23:52:29 +02002101 break;
2102 }
2103 }
Bram Moolenaar0ba04292010-07-14 23:23:17 +02002104 }
Bram Moolenaar1dced572012-04-05 16:54:08 +02002105 else
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002106 buf = (lua_toboolean(L, 1)) ? firstbuf : curbuf; // first buffer?
Bram Moolenaar1dced572012-04-05 16:54:08 +02002107 luaV_pushbuffer(L, buf);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02002108 return 1;
2109}
2110
Bram Moolenaar55d5c032010-07-17 23:52:29 +02002111 static int
2112luaV_window(lua_State *L)
2113{
Bram Moolenaar0ba04292010-07-14 23:23:17 +02002114 win_T *win;
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002115 if (lua_isnumber(L, 1)) // get by number?
Bram Moolenaar55d5c032010-07-17 23:52:29 +02002116 {
2117 int n = lua_tointeger(L, 1);
2118 for (win = firstwin; win != NULL; win = win->w_next, n--)
2119 if (n == 1) break;
Bram Moolenaar0ba04292010-07-14 23:23:17 +02002120 }
Bram Moolenaar1dced572012-04-05 16:54:08 +02002121 else
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002122 win = (lua_toboolean(L, 1)) ? firstwin : curwin; // first window?
Bram Moolenaar1dced572012-04-05 16:54:08 +02002123 luaV_pushwindow(L, win);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02002124 return 1;
2125}
2126
Bram Moolenaar55d5c032010-07-17 23:52:29 +02002127 static int
2128luaV_open(lua_State *L)
2129{
Bram Moolenaar0ba04292010-07-14 23:23:17 +02002130 char_u *s = NULL;
2131#ifdef HAVE_SANDBOX
2132 luaV_checksandbox(L);
2133#endif
2134 if (lua_isstring(L, 1)) s = (char_u *) lua_tostring(L, 1);
Bram Moolenaarfa263a52011-12-08 16:00:16 +01002135 luaV_pushbuffer(L, buflist_new(s, NULL, 1L, BLN_LISTED));
Bram Moolenaar0ba04292010-07-14 23:23:17 +02002136 return 1;
2137}
2138
Bram Moolenaar55d5c032010-07-17 23:52:29 +02002139 static int
Bram Moolenaar1dced572012-04-05 16:54:08 +02002140luaV_type(lua_State *L)
Bram Moolenaar55d5c032010-07-17 23:52:29 +02002141{
Bram Moolenaar1dced572012-04-05 16:54:08 +02002142 luaL_checkany(L, 1);
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002143 if (lua_type(L, 1) == LUA_TUSERDATA) // check vim udata?
Bram Moolenaar55d5c032010-07-17 23:52:29 +02002144 {
Bram Moolenaar1dced572012-04-05 16:54:08 +02002145 lua_settop(L, 1);
2146 if (lua_getmetatable(L, 1))
2147 {
2148 luaV_getfield(L, LUAVIM_LIST);
2149 if (lua_rawequal(L, -1, 2))
2150 {
2151 lua_pushstring(L, "list");
2152 return 1;
2153 }
2154 luaV_getfield(L, LUAVIM_DICT);
2155 if (lua_rawequal(L, -1, 2))
2156 {
2157 lua_pushstring(L, "dict");
2158 return 1;
2159 }
Bram Moolenaarb7828692019-03-23 13:57:02 +01002160 luaV_getfield(L, LUAVIM_BLOB);
2161 if (lua_rawequal(L, -1, 2))
2162 {
2163 lua_pushstring(L, "blob");
2164 return 1;
2165 }
Bram Moolenaarca06da92018-07-01 15:12:05 +02002166 luaV_getfield(L, LUAVIM_FUNCREF);
2167 if (lua_rawequal(L, -1, 2))
2168 {
2169 lua_pushstring(L, "funcref");
2170 return 1;
2171 }
Bram Moolenaar1dced572012-04-05 16:54:08 +02002172 luaV_getfield(L, LUAVIM_BUFFER);
2173 if (lua_rawequal(L, -1, 2))
2174 {
2175 lua_pushstring(L, "buffer");
2176 return 1;
2177 }
2178 luaV_getfield(L, LUAVIM_WINDOW);
2179 if (lua_rawequal(L, -1, 2))
2180 {
2181 lua_pushstring(L, "window");
2182 return 1;
2183 }
2184 }
Bram Moolenaar0ba04292010-07-14 23:23:17 +02002185 }
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002186 lua_pushstring(L, luaL_typename(L, 1)); // fallback
Bram Moolenaar1dced572012-04-05 16:54:08 +02002187 return 1;
Bram Moolenaar0ba04292010-07-14 23:23:17 +02002188}
2189
Bram Moolenaareb04f082020-05-17 14:32:35 +02002190 static int
2191luaV_call(lua_State *L)
2192{
2193 int argc = lua_gettop(L) - 1;
2194 size_t funcname_len;
2195 char_u *funcname;
2196 char *error = NULL;
2197 typval_T rettv;
2198 typval_T argv[MAX_FUNC_ARGS + 1];
2199 int i = 0;
2200
2201 if (argc > MAX_FUNC_ARGS)
2202 return luaL_error(L, "Function called with too many arguments");
2203
2204 funcname = (char_u *)luaL_checklstring(L, 1, &funcname_len);
2205
2206 for (; i < argc; i++)
2207 {
2208 if (luaV_totypval(L, i + 2, &argv[i]) == FAIL)
2209 {
2210 error = "lua: cannot convert value";
2211 goto free_vim_args;
2212 }
2213 }
2214
2215 argv[argc].v_type = VAR_UNKNOWN;
2216
2217 if (call_vim_function(funcname, argc, argv, &rettv) == FAIL)
2218 {
2219 error = "lua: call_vim_function failed";
2220 goto free_vim_args;
2221 }
2222
2223 luaV_pushtypval(L, &rettv);
2224 clear_tv(&rettv);
2225
2226free_vim_args:
2227 while (i > 0)
2228 clear_tv(&argv[--i]);
2229
2230 if (error == NULL)
2231 return 1;
2232 else
2233 return luaL_error(L, error);
2234}
2235
Yegappan Lakshmanan11328bc2021-08-06 21:34:38 +02002236/*
2237 * Return the Vim version as a Lua table
2238 */
2239 static int
2240luaV_version(lua_State *L)
2241{
2242 lua_newtable(L);
2243 lua_pushstring(L, "major");
2244 lua_pushinteger(L, VIM_VERSION_MAJOR);
2245 lua_settable(L, -3);
2246 lua_pushstring(L, "minor");
2247 lua_pushinteger(L, VIM_VERSION_MINOR);
2248 lua_settable(L, -3);
2249 lua_pushstring(L, "patch");
2250 lua_pushinteger(L, highest_patch());
2251 lua_settable(L, -3);
2252 return 1;
2253}
2254
Bram Moolenaar0ba04292010-07-14 23:23:17 +02002255static const luaL_Reg luaV_module[] = {
2256 {"command", luaV_command},
2257 {"eval", luaV_eval},
2258 {"beep", luaV_beep},
2259 {"line", luaV_line},
Bram Moolenaar1dced572012-04-05 16:54:08 +02002260 {"list", luaV_list},
2261 {"dict", luaV_dict},
Bram Moolenaarb7828692019-03-23 13:57:02 +01002262 {"blob", luaV_blob},
Bram Moolenaarca06da92018-07-01 15:12:05 +02002263 {"funcref", luaV_funcref},
Bram Moolenaar0ba04292010-07-14 23:23:17 +02002264 {"buffer", luaV_buffer},
2265 {"window", luaV_window},
2266 {"open", luaV_open},
Bram Moolenaar1dced572012-04-05 16:54:08 +02002267 {"type", luaV_type},
Bram Moolenaareb04f082020-05-17 14:32:35 +02002268 {"call", luaV_call},
Yegappan Lakshmanan9dc4bef2021-08-04 21:12:52 +02002269 {"_getvar", luaV_getvar},
2270 {"_setvar", luaV_setvar},
Yegappan Lakshmanan11328bc2021-08-06 21:34:38 +02002271 {"version", luaV_version},
Bram Moolenaar125ed272021-04-07 20:11:12 +02002272 {"lua_version", NULL},
Bram Moolenaar0ba04292010-07-14 23:23:17 +02002273 {NULL, NULL}
2274};
2275
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002276/*
2277 * for freeing list, dict, buffer and window objects; lightuserdata as arg
2278 */
Bram Moolenaar1dced572012-04-05 16:54:08 +02002279 static int
2280luaV_free(lua_State *L)
2281{
2282 lua_pushnil(L);
2283 luaV_setudata(L, lua_touserdata(L, 1));
2284 return 0;
2285}
2286
2287 static int
Bram Moolenaar4eefe472019-03-19 21:59:19 +01002288luaV_luaeval(lua_State *L)
Bram Moolenaar1dced572012-04-05 16:54:08 +02002289{
2290 luaL_Buffer b;
2291 size_t l;
2292 const char *str = lua_tolstring(L, 1, &l);
2293 typval_T *arg = (typval_T *) lua_touserdata(L, 2);
2294 typval_T *rettv = (typval_T *) lua_touserdata(L, 3);
2295 luaL_buffinit(L, &b);
2296 luaL_addlstring(&b, LUAVIM_EVALHEADER, sizeof(LUAVIM_EVALHEADER) - 1);
2297 luaL_addlstring(&b, str, l);
2298 luaL_pushresult(&b);
2299 str = lua_tolstring(L, -1, &l);
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002300 if (luaL_loadbuffer(L, str, l, LUAVIM_EVALNAME)) // compile error?
Bram Moolenaar1dced572012-04-05 16:54:08 +02002301 {
2302 luaV_emsg(L);
2303 return 0;
2304 }
2305 luaV_pushtypval(L, arg);
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002306 if (lua_pcall(L, 1, 1, 0)) // running error?
Bram Moolenaar1dced572012-04-05 16:54:08 +02002307 {
2308 luaV_emsg(L);
2309 return 0;
2310 }
Bram Moolenaarca06da92018-07-01 15:12:05 +02002311 if (luaV_totypval(L, -1, rettv) == FAIL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002312 emsg("luaeval: cannot convert value");
Bram Moolenaarf554a322015-02-04 23:08:01 +01002313 return 0;
Bram Moolenaar1dced572012-04-05 16:54:08 +02002314}
2315
2316 static int
Bram Moolenaar4eefe472019-03-19 21:59:19 +01002317luaV_setref(lua_State *L)
Bram Moolenaar1dced572012-04-05 16:54:08 +02002318{
Bram Moolenaar4eefe472019-03-19 21:59:19 +01002319 int copyID = lua_tointeger(L, 1);
2320 int abort = FALSE;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01002321
Bram Moolenaar1dced572012-04-05 16:54:08 +02002322 luaV_getfield(L, LUAVIM_LIST);
2323 luaV_getfield(L, LUAVIM_DICT);
Bram Moolenaar4eefe472019-03-19 21:59:19 +01002324 luaV_getfield(L, LUAVIM_FUNCREF);
Bram Moolenaar1dced572012-04-05 16:54:08 +02002325 lua_pushnil(L);
Bram Moolenaar4eefe472019-03-19 21:59:19 +01002326 // traverse cache table
Bram Moolenaarb84634d2015-02-04 22:02:37 +01002327 while (!abort && lua_next(L, lua_upvalueindex(1)) != 0)
Bram Moolenaar1dced572012-04-05 16:54:08 +02002328 {
2329 lua_getmetatable(L, -1);
Bram Moolenaar4eefe472019-03-19 21:59:19 +01002330 if (lua_rawequal(L, -1, 2)) // list?
Bram Moolenaar1dced572012-04-05 16:54:08 +02002331 {
Bram Moolenaar4eefe472019-03-19 21:59:19 +01002332 list_T *l = (list_T *)lua_touserdata(L, 5); // key
2333
Bram Moolenaar7be3ab22019-06-23 01:46:15 +02002334 abort = set_ref_in_list(l, copyID);
Bram Moolenaar1dced572012-04-05 16:54:08 +02002335 }
Bram Moolenaar4eefe472019-03-19 21:59:19 +01002336 else if (lua_rawequal(L, -1, 3)) // dict?
Bram Moolenaar1dced572012-04-05 16:54:08 +02002337 {
Bram Moolenaar4eefe472019-03-19 21:59:19 +01002338 dict_T *d = (dict_T *)lua_touserdata(L, 5); // key
2339
Bram Moolenaar7be3ab22019-06-23 01:46:15 +02002340 abort = set_ref_in_dict(d, copyID);
Bram Moolenaar1dced572012-04-05 16:54:08 +02002341 }
Bram Moolenaar4eefe472019-03-19 21:59:19 +01002342 else if (lua_rawequal(L, -1, 4)) // funcref?
2343 {
2344 luaV_Funcref *f = (luaV_Funcref *)lua_touserdata(L, 5); // key
2345
Bram Moolenaar7be3ab22019-06-23 01:46:15 +02002346 abort = set_ref_in_dict(f->self, copyID);
Bram Moolenaar4eefe472019-03-19 21:59:19 +01002347 }
2348 lua_pop(L, 2); // metatable and value
Bram Moolenaar1dced572012-04-05 16:54:08 +02002349 }
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01002350 lua_pushinteger(L, abort);
Bram Moolenaarf554a322015-02-04 23:08:01 +01002351 return 1;
Bram Moolenaar1dced572012-04-05 16:54:08 +02002352}
2353
Bram Moolenaar125ed272021-04-07 20:11:12 +02002354 static int
2355luaV_pushversion(lua_State *L)
2356{
2357 int major = 0;
2358 int minor = 0;
2359 int patch = 0;
2360 char s[16];
2361
2362 sscanf(LUAVIM_VERSION, "Lua %d.%d.%d", &major, &minor, &patch);
2363 vim_snprintf(s, sizeof(s), "%d.%d.%d", major, minor, patch);
2364 lua_pushstring(L, s);
2365 return 0;
2366}
2367
Bram Moolenaareb04f082020-05-17 14:32:35 +02002368#define LUA_VIM_FN_CODE \
Bram Moolenaar788fbb42020-05-31 14:08:12 +02002369 "vim.fn = setmetatable({}, {\n"\
2370 " __index = function (t, key)\n"\
2371 " local function _fn(...)\n"\
2372 " return vim.call(key, ...)\n"\
2373 " end\n"\
2374 " t[key] = _fn\n"\
2375 " return _fn\n"\
2376 " end\n"\
2377 " })"
2378
2379#define LUA_VIM_UPDATE_PACKAGE_PATHS \
2380 "local last_vim_paths = {}\n"\
2381 "vim._update_package_paths = function ()\n"\
2382 " local cur_vim_paths = {}\n"\
2383 " local function split(s, delimiter)\n"\
2384 " result = {}\n"\
2385 " for match in (s..delimiter):gmatch(\"(.-)\"..delimiter) do\n"\
2386 " table.insert(result, match)\n"\
2387 " end\n"\
2388 " return result\n"\
2389 " end\n"\
2390 " local rtps = split(vim.eval('&runtimepath'), ',')\n"\
2391 " local sep = package.config:sub(1, 1)\n"\
2392 " for _, key in ipairs({'path', 'cpath'}) do\n"\
2393 " local orig_str = package[key] .. ';'\n"\
2394 " local pathtrails_ordered = {}\n"\
2395 " -- Note: ignores trailing item without trailing `;`. Not using something\n"\
2396 " -- simpler in order to preserve empty items (stand for default path).\n"\
2397 " local orig = {}\n"\
2398 " for s in orig_str:gmatch('[^;]*;') do\n"\
2399 " s = s:sub(1, -2) -- Strip trailing semicolon\n"\
2400 " orig[#orig + 1] = s\n"\
2401 " end\n"\
2402 " if key == 'path' then\n"\
2403 " -- /?.lua and /?/init.lua\n"\
2404 " pathtrails_ordered = {sep .. '?.lua', sep .. '?' .. sep .. 'init.lua'}\n"\
2405 " else\n"\
2406 " local pathtrails = {}\n"\
2407 " for _, s in ipairs(orig) do\n"\
2408 " -- Find out path patterns. pathtrail should contain something like\n"\
2409 " -- /?.so, \?.dll. This allows not to bother determining what correct\n"\
2410 " -- suffixes are.\n"\
2411 " local pathtrail = s:match('[/\\\\][^/\\\\]*%?.*$')\n"\
2412 " if pathtrail and not pathtrails[pathtrail] then\n"\
2413 " pathtrails[pathtrail] = true\n"\
2414 " pathtrails_ordered[#pathtrails_ordered + 1] = pathtrail\n"\
2415 " end\n"\
2416 " end\n"\
2417 " end\n"\
2418 " local new = {}\n"\
2419 " for _, rtp in ipairs(rtps) do\n"\
2420 " if not rtp:match(';') then\n"\
2421 " for _, pathtrail in pairs(pathtrails_ordered) do\n"\
2422 " local new_path = rtp .. sep .. 'lua' .. pathtrail\n"\
2423 " -- Always keep paths from &runtimepath at the start:\n"\
2424 " -- append them here disregarding orig possibly containing one of them.\n"\
2425 " new[#new + 1] = new_path\n"\
2426 " cur_vim_paths[new_path] = true\n"\
2427 " end\n"\
2428 " end\n"\
2429 " end\n"\
2430 " for _, orig_path in ipairs(orig) do\n"\
2431 " -- Handle removing obsolete paths originating from &runtimepath: such\n"\
2432 " -- paths either belong to cur_nvim_paths and were already added above or\n"\
2433 " -- to last_nvim_paths and should not be added at all if corresponding\n"\
2434 " -- entry was removed from &runtimepath list.\n"\
2435 " if not (cur_vim_paths[orig_path] or last_vim_paths[orig_path]) then\n"\
2436 " new[#new + 1] = orig_path\n"\
2437 " end\n"\
2438 " end\n"\
2439 " package[key] = table.concat(new, ';')\n"\
2440 " end\n"\
2441 " last_vim_paths = cur_vim_paths\n"\
2442 "end"
Bram Moolenaareb04f082020-05-17 14:32:35 +02002443
Yegappan Lakshmanan9dc4bef2021-08-04 21:12:52 +02002444#define LUA_VIM_SETUP_VARIABLE_DICTS \
2445 "do\n"\
2446 " local function make_dict_accessor(scope)\n"\
2447 " local mt = {}\n"\
2448 " function mt:__newindex(k, v)\n"\
2449 " return vim._setvar(scope, 0, k, v)\n"\
2450 " end\n"\
2451 " function mt:__index(k)\n"\
2452 " return vim._getvar(scope, 0, k)\n"\
2453 " end\n"\
2454 " return setmetatable({}, mt)\n"\
2455 " end\n"\
2456 " vim.g = make_dict_accessor('g')\n"\
2457 " vim.v = make_dict_accessor('v')\n"\
2458 " vim.b = make_dict_accessor('b')\n"\
2459 " vim.w = make_dict_accessor('w')\n"\
2460 " vim.t = make_dict_accessor('t')\n"\
2461 "end"
2462
Bram Moolenaar55d5c032010-07-17 23:52:29 +02002463 static int
2464luaopen_vim(lua_State *L)
2465{
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002466 // set cache table
Bram Moolenaar0ba04292010-07-14 23:23:17 +02002467 lua_newtable(L);
2468 lua_newtable(L);
Bram Moolenaar1dced572012-04-05 16:54:08 +02002469 lua_pushstring(L, "v");
Bram Moolenaar0ba04292010-07-14 23:23:17 +02002470 lua_setfield(L, -2, "__mode");
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002471 lua_setmetatable(L, -2); // cache is weak-valued
2472 // print
Bram Moolenaar0ba04292010-07-14 23:23:17 +02002473 lua_pushcfunction(L, luaV_print);
2474 lua_setglobal(L, "print");
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002475 // debug.debug
Bram Moolenaar38e2b062011-09-21 17:15:39 +02002476 lua_getglobal(L, "debug");
2477 lua_pushcfunction(L, luaV_debug);
2478 lua_setfield(L, -2, "debug");
2479 lua_pop(L, 1);
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002480 // free
Bram Moolenaar0ba04292010-07-14 23:23:17 +02002481 lua_pushlightuserdata(L, (void *) LUAVIM_FREE);
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002482 lua_pushvalue(L, 1); // cache table
Bram Moolenaar1dced572012-04-05 16:54:08 +02002483 lua_pushcclosure(L, luaV_free, 1);
2484 lua_rawset(L, LUA_REGISTRYINDEX);
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002485 // luaeval
Bram Moolenaar1dced572012-04-05 16:54:08 +02002486 lua_pushlightuserdata(L, (void *) LUAVIM_LUAEVAL);
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002487 lua_pushvalue(L, 1); // cache table
Bram Moolenaar1dced572012-04-05 16:54:08 +02002488 lua_pushcclosure(L, luaV_luaeval, 1);
2489 lua_rawset(L, LUA_REGISTRYINDEX);
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002490 // setref
Bram Moolenaar1dced572012-04-05 16:54:08 +02002491 lua_pushlightuserdata(L, (void *) LUAVIM_SETREF);
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002492 lua_pushvalue(L, 1); // cache table
Bram Moolenaar1dced572012-04-05 16:54:08 +02002493 lua_pushcclosure(L, luaV_setref, 1);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02002494 lua_rawset(L, LUA_REGISTRYINDEX);
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002495 // register
Bram Moolenaar1dced572012-04-05 16:54:08 +02002496 luaV_newmetatable(L, LUAVIM_LIST);
2497 lua_pushvalue(L, 1);
2498 luaV_openlib(L, luaV_List_mt, 1);
2499 luaV_newmetatable(L, LUAVIM_DICT);
2500 lua_pushvalue(L, 1);
2501 luaV_openlib(L, luaV_Dict_mt, 1);
Bram Moolenaarb7828692019-03-23 13:57:02 +01002502 luaV_newmetatable(L, LUAVIM_BLOB);
2503 lua_pushvalue(L, 1);
2504 luaV_openlib(L, luaV_Blob_mt, 1);
Bram Moolenaarca06da92018-07-01 15:12:05 +02002505 luaV_newmetatable(L, LUAVIM_FUNCREF);
2506 lua_pushvalue(L, 1);
2507 luaV_openlib(L, luaV_Funcref_mt, 1);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02002508 luaV_newmetatable(L, LUAVIM_BUFFER);
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002509 lua_pushvalue(L, 1); // cache table
Bram Moolenaar1dced572012-04-05 16:54:08 +02002510 luaV_openlib(L, luaV_Buffer_mt, 1);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02002511 luaV_newmetatable(L, LUAVIM_WINDOW);
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002512 lua_pushvalue(L, 1); // cache table
Bram Moolenaar1dced572012-04-05 16:54:08 +02002513 luaV_openlib(L, luaV_Window_mt, 1);
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002514 lua_newtable(L); // vim table
2515 lua_pushvalue(L, 1); // cache table
Bram Moolenaar1dced572012-04-05 16:54:08 +02002516 luaV_openlib(L, luaV_module, 1);
Bram Moolenaar125ed272021-04-07 20:11:12 +02002517 luaV_pushversion(L);
2518 lua_setfield(L, -2, "lua_version");
Bram Moolenaar1dced572012-04-05 16:54:08 +02002519 lua_setglobal(L, LUAVIM_NAME);
Bram Moolenaareb04f082020-05-17 14:32:35 +02002520 // custom code
Bram Moolenaar9309eb22020-05-17 16:53:56 +02002521 (void)luaL_dostring(L, LUA_VIM_FN_CODE);
Bram Moolenaar788fbb42020-05-31 14:08:12 +02002522 (void)luaL_dostring(L, LUA_VIM_UPDATE_PACKAGE_PATHS);
Yegappan Lakshmanan9dc4bef2021-08-04 21:12:52 +02002523 (void)luaL_dostring(L, LUA_VIM_SETUP_VARIABLE_DICTS);
Bram Moolenaar788fbb42020-05-31 14:08:12 +02002524
2525 lua_getglobal(L, "vim");
2526 lua_getfield(L, -1, "_update_package_paths");
2527
2528 if (lua_pcall(L, 0, 0, 0))
2529 luaV_emsg(L);
2530
Bram Moolenaar0ba04292010-07-14 23:23:17 +02002531 return 0;
2532}
2533
Bram Moolenaar55d5c032010-07-17 23:52:29 +02002534 static lua_State *
2535luaV_newstate(void)
2536{
Bram Moolenaar0ba04292010-07-14 23:23:17 +02002537 lua_State *L = luaL_newstate();
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002538 luaL_openlibs(L); // core libs
2539 lua_pushcfunction(L, luaopen_vim); // vim
Bram Moolenaar0ba04292010-07-14 23:23:17 +02002540 lua_call(L, 0, 0);
2541 return L;
2542}
2543
Bram Moolenaar55d5c032010-07-17 23:52:29 +02002544 static void
2545luaV_setrange(lua_State *L, int line1, int line2)
2546{
Bram Moolenaar0ba04292010-07-14 23:23:17 +02002547 lua_getglobal(L, LUAVIM_NAME);
2548 lua_pushinteger(L, line1);
2549 lua_setfield(L, -2, "firstline");
2550 lua_pushinteger(L, line2);
2551 lua_setfield(L, -2, "lastline");
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002552 lua_pop(L, 1); // vim table
Bram Moolenaar0ba04292010-07-14 23:23:17 +02002553}
2554
2555
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002556// ======= Interface =======
Bram Moolenaar0ba04292010-07-14 23:23:17 +02002557
2558static lua_State *L = NULL;
2559
Bram Moolenaar55d5c032010-07-17 23:52:29 +02002560 static int
Bram Moolenaar1dced572012-04-05 16:54:08 +02002561lua_isopen(void)
Bram Moolenaar2bd6a1b2010-08-12 22:14:01 +02002562{
2563 return L != NULL;
2564}
2565
2566 static int
Bram Moolenaar55d5c032010-07-17 23:52:29 +02002567lua_init(void)
2568{
Yegappan Lakshmanan0233bdf2023-01-12 12:33:30 +00002569 if (lua_isopen())
2570 return OK;
2571
Bram Moolenaar0ba04292010-07-14 23:23:17 +02002572#ifdef DYNAMIC_LUA
Yegappan Lakshmanan0233bdf2023-01-12 12:33:30 +00002573 if (!lua_enabled(TRUE))
2574 {
2575 emsg(_("Lua library cannot be loaded."));
2576 return FAIL;
Bram Moolenaar0ba04292010-07-14 23:23:17 +02002577 }
Yegappan Lakshmanan0233bdf2023-01-12 12:33:30 +00002578#endif
2579 L = luaV_newstate();
2580
Bram Moolenaar0ba04292010-07-14 23:23:17 +02002581 return OK;
2582}
2583
Bram Moolenaar55d5c032010-07-17 23:52:29 +02002584 void
2585lua_end(void)
2586{
Yegappan Lakshmanan0233bdf2023-01-12 12:33:30 +00002587 if (!lua_isopen())
2588 return;
2589
2590 lua_close(L);
2591 L = NULL;
Bram Moolenaar0ba04292010-07-14 23:23:17 +02002592}
2593
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002594/*
2595 * ex commands
2596 */
Bram Moolenaar55d5c032010-07-17 23:52:29 +02002597 void
2598ex_lua(exarg_T *eap)
2599{
Bram Moolenaarc8970b92020-10-26 20:18:08 +01002600 char *script = (char *)script_get(eap, eap->arg);
2601
2602 if (!eap->skip && lua_init() == OK)
Bram Moolenaar55d5c032010-07-17 23:52:29 +02002603 {
Bram Moolenaarc8970b92020-10-26 20:18:08 +01002604 char *s = script != NULL ? script : (char *)eap->arg;
2605
Bram Moolenaar55d5c032010-07-17 23:52:29 +02002606 luaV_setrange(L, eap->line1, eap->line2);
2607 if (luaL_loadbuffer(L, s, strlen(s), LUAVIM_CHUNKNAME)
2608 || lua_pcall(L, 0, 0, 0))
2609 luaV_emsg(L);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02002610 }
Bram Moolenaarc8970b92020-10-26 20:18:08 +01002611 if (script != NULL)
2612 vim_free(script);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02002613}
2614
Bram Moolenaar55d5c032010-07-17 23:52:29 +02002615 void
2616ex_luado(exarg_T *eap)
2617{
Bram Moolenaar0ba04292010-07-14 23:23:17 +02002618 linenr_T l;
2619 const char *s = (const char *) eap->arg;
2620 luaL_Buffer b;
2621 size_t len;
Bram Moolenaard58f03b2017-01-29 22:48:45 +01002622 buf_T *was_curbuf = curbuf;
2623
Bram Moolenaar0ba04292010-07-14 23:23:17 +02002624 if (lua_init() == FAIL) return;
Bram Moolenaar55d5c032010-07-17 23:52:29 +02002625 if (u_save(eap->line1 - 1, eap->line2 + 1) == FAIL)
2626 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002627 emsg(_("cannot save undo information"));
Bram Moolenaar55d5c032010-07-17 23:52:29 +02002628 return;
Bram Moolenaar0ba04292010-07-14 23:23:17 +02002629 }
2630 luaV_setrange(L, eap->line1, eap->line2);
2631 luaL_buffinit(L, &b);
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002632 luaL_addlstring(&b, "return function(line, linenr) ", 30); // header
Bram Moolenaar0ba04292010-07-14 23:23:17 +02002633 luaL_addlstring(&b, s, strlen(s));
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002634 luaL_addlstring(&b, " end", 4); // footer
Bram Moolenaar0ba04292010-07-14 23:23:17 +02002635 luaL_pushresult(&b);
2636 s = lua_tolstring(L, -1, &len);
Bram Moolenaar55d5c032010-07-17 23:52:29 +02002637 if (luaL_loadbuffer(L, s, len, LUAVIM_CHUNKNAME))
2638 {
2639 luaV_emsg(L);
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002640 lua_pop(L, 1); // function body
Bram Moolenaar55d5c032010-07-17 23:52:29 +02002641 return;
Bram Moolenaar0ba04292010-07-14 23:23:17 +02002642 }
2643 lua_call(L, 0, 1);
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002644 lua_replace(L, -2); // function -> body
Bram Moolenaar55d5c032010-07-17 23:52:29 +02002645 for (l = eap->line1; l <= eap->line2; l++)
2646 {
Bram Moolenaare49b8e82020-07-01 13:52:55 +02002647 // Check the line number, the command may have deleted lines.
Bram Moolenaard58f03b2017-01-29 22:48:45 +01002648 if (l > curbuf->b_ml.ml_line_count)
2649 break;
2650
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002651 lua_pushvalue(L, -1); // function
2652 luaV_pushline(L, curbuf, l); // current line as arg
2653 lua_pushinteger(L, l); // current line number as arg
Bram Moolenaarbd2f3c32012-04-06 14:31:00 +02002654 if (lua_pcall(L, 2, 1, 0))
Bram Moolenaar55d5c032010-07-17 23:52:29 +02002655 {
2656 luaV_emsg(L);
2657 break;
2658 }
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002659 // Catch the command switching to another buffer.
Bram Moolenaard58f03b2017-01-29 22:48:45 +01002660 if (curbuf != was_curbuf)
2661 break;
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002662 if (lua_isstring(L, -1)) // update line?
Bram Moolenaar55d5c032010-07-17 23:52:29 +02002663 {
Bram Moolenaar0ba04292010-07-14 23:23:17 +02002664#ifdef HAVE_SANDBOX
Bram Moolenaar55d5c032010-07-17 23:52:29 +02002665 luaV_checksandbox(L);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02002666#endif
Bram Moolenaar55d5c032010-07-17 23:52:29 +02002667 ml_replace(l, luaV_toline(L, -1), TRUE);
2668 changed_bytes(l, 0);
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002669 lua_pop(L, 1); // result from luaV_toline
Bram Moolenaar55d5c032010-07-17 23:52:29 +02002670 }
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002671 lua_pop(L, 1); // line
Bram Moolenaar0ba04292010-07-14 23:23:17 +02002672 }
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002673 lua_pop(L, 1); // function
Bram Moolenaar0ba04292010-07-14 23:23:17 +02002674 check_cursor();
Bram Moolenaara4d158b2022-08-14 14:17:45 +01002675 update_screen(UPD_NOT_VALID);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02002676}
2677
Bram Moolenaar55d5c032010-07-17 23:52:29 +02002678 void
2679ex_luafile(exarg_T *eap)
2680{
2681 if (lua_init() == FAIL)
2682 return;
2683 if (!eap->skip)
2684 {
2685 luaV_setrange(L, eap->line1, eap->line2);
2686 if (luaL_loadfile(L, (char *) eap->arg) || lua_pcall(L, 0, 0, 0))
2687 luaV_emsg(L);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02002688 }
2689}
2690
Bram Moolenaar1dced572012-04-05 16:54:08 +02002691#define luaV_freetype(typ,tname) \
2692 void \
2693 lua_##tname##_free(typ *o) \
2694 { \
2695 if (!lua_isopen()) return; \
2696 luaV_getfield(L, LUAVIM_FREE); \
2697 lua_pushlightuserdata(L, (void *) o); \
2698 lua_call(L, 1, 0); \
2699 }
2700
2701luaV_freetype(buf_T, buffer)
2702luaV_freetype(win_T, window)
2703
Bram Moolenaar55d5c032010-07-17 23:52:29 +02002704 void
Bram Moolenaar4eefe472019-03-19 21:59:19 +01002705do_luaeval(char_u *str, typval_T *arg, typval_T *rettv)
Bram Moolenaar55d5c032010-07-17 23:52:29 +02002706{
Bram Moolenaar1dced572012-04-05 16:54:08 +02002707 lua_init();
2708 luaV_getfield(L, LUAVIM_LUAEVAL);
2709 lua_pushstring(L, (char *) str);
2710 lua_pushlightuserdata(L, (void *) arg);
2711 lua_pushlightuserdata(L, (void *) rettv);
2712 lua_call(L, 3, 0);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02002713}
2714
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01002715 int
Bram Moolenaar4eefe472019-03-19 21:59:19 +01002716set_ref_in_lua(int copyID)
Bram Moolenaar55d5c032010-07-17 23:52:29 +02002717{
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01002718 int aborted = 0;
2719
Yegappan Lakshmanan0233bdf2023-01-12 12:33:30 +00002720 if (!lua_isopen())
2721 return 0;
2722
2723 luaV_getfield(L, LUAVIM_SETREF);
2724 // call the function with 1 arg, getting 1 result back
2725 lua_pushinteger(L, copyID);
2726 lua_call(L, 1, 1);
2727 // get the result
2728 aborted = lua_tointeger(L, -1);
2729 // pop result off the stack
2730 lua_pop(L, 1);
2731
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01002732 return aborted;
Bram Moolenaar0ba04292010-07-14 23:23:17 +02002733}
2734
Bram Moolenaar788fbb42020-05-31 14:08:12 +02002735 void
2736update_package_paths_in_lua()
2737{
Yegappan Lakshmanan0233bdf2023-01-12 12:33:30 +00002738 if (!lua_isopen())
2739 return;
Bram Moolenaar788fbb42020-05-31 14:08:12 +02002740
Yegappan Lakshmanan0233bdf2023-01-12 12:33:30 +00002741 lua_getglobal(L, "vim");
2742 lua_getfield(L, -1, "_update_package_paths");
2743
2744 if (lua_pcall(L, 0, 0, 0))
2745 luaV_emsg(L);
Bram Moolenaar788fbb42020-05-31 14:08:12 +02002746}
2747
Bram Moolenaar801ab062020-06-25 19:27:56 +02002748/*
2749 * Native C function callback
2750 */
2751 static int
2752luaV_call_lua_func(
2753 int argcount,
2754 typval_T *argvars,
2755 typval_T *rettv,
2756 void *state)
2757{
2758 int i;
2759 int luaargcount = argcount;
2760 luaV_CFuncState *funcstate = (luaV_CFuncState*)state;
2761 lua_rawgeti(funcstate->L, LUA_REGISTRYINDEX, funcstate->lua_funcref);
2762
2763 if (funcstate->lua_tableref != LUA_NOREF)
2764 {
2765 // First arg for metatable __call method is a table
2766 luaargcount += 1;
2767 lua_rawgeti(funcstate->L, LUA_REGISTRYINDEX, funcstate->lua_tableref);
2768 }
2769
2770 for (i = 0; i < argcount; ++i)
2771 luaV_pushtypval(funcstate->L, &argvars[i]);
2772
2773 if (lua_pcall(funcstate->L, luaargcount, 1, 0))
2774 {
2775 luaV_emsg(funcstate->L);
2776 return FCERR_OTHER;
2777 }
2778
2779 luaV_checktypval(funcstate->L, -1, rettv, "get return value");
2780 return FCERR_NONE;
2781}
2782
2783/*
2784 * Free up any lua references held by the func state.
2785 */
2786 static void
2787luaV_call_lua_func_free(void *state)
2788{
2789 luaV_CFuncState *funcstate = (luaV_CFuncState*)state;
2790 luaL_unref(L, LUA_REGISTRYINDEX, funcstate->lua_funcref);
2791 funcstate->L = NULL;
2792 if (funcstate->lua_tableref != LUA_NOREF)
2793 luaL_unref(L, LUA_REGISTRYINDEX, funcstate->lua_tableref);
2794 VIM_CLEAR(funcstate);
2795}
2796
Bram Moolenaar0ba04292010-07-14 23:23:17 +02002797#endif