blob: 45f233fdecb0b5d49dc503f804ad18f7e74b4b1a [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 Moolenaarebfec1c2023-01-22 21:14:53 +0000660 if (lua_getmetatable(L, pos))
661 {
Bram Moolenaar801ab062020-06-25 19:27:56 +0200662 lua_getfield(L, -1, LUA___CALL);
Bram Moolenaarebfec1c2023-01-22 21:14:53 +0000663 if (lua_isfunction(L, -1))
664 {
Bram Moolenaar801ab062020-06-25 19:27:56 +0200665 char_u *name;
666 int lua_funcref = luaL_ref(L, LUA_REGISTRYINDEX);
667 luaV_CFuncState *state = ALLOC_CLEAR_ONE(luaV_CFuncState);
Bram Moolenaar066e7da2020-07-18 12:50:35 +0200668
Bram Moolenaar801ab062020-06-25 19:27:56 +0200669 state->lua_funcref = lua_funcref;
670 state->L = L;
671 state->lua_tableref = lua_tableref;
672 name = register_cfunc(&luaV_call_lua_func,
673 &luaV_call_lua_func_free, state);
674 tv->v_type = VAR_FUNC;
675 tv->vval.v_string = vim_strsave(name);
676 break;
677 }
678 }
679 tv->v_type = VAR_NUMBER;
680 tv->vval.v_number = 0;
681 status = FAIL;
682 break;
683 }
Bram Moolenaarca06da92018-07-01 15:12:05 +0200684 case LUA_TUSERDATA:
685 {
Bram Moolenaar1dced572012-04-05 16:54:08 +0200686 void *p = lua_touserdata(L, pos);
Bram Moolenaarca06da92018-07-01 15:12:05 +0200687
Bram Moolenaarb7828692019-03-23 13:57:02 +0100688 if (lua_getmetatable(L, pos)) // has metatable?
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200689 {
Bram Moolenaarb7828692019-03-23 13:57:02 +0100690 // check list
Bram Moolenaar1dced572012-04-05 16:54:08 +0200691 luaV_getfield(L, LUAVIM_LIST);
692 if (lua_rawequal(L, -1, -2))
Bram Moolenaar2334b6d2010-07-22 21:32:16 +0200693 {
Bram Moolenaar1dced572012-04-05 16:54:08 +0200694 tv->v_type = VAR_LIST;
695 tv->vval.v_list = *((luaV_List *) p);
696 ++tv->vval.v_list->lv_refcount;
Bram Moolenaarb7828692019-03-23 13:57:02 +0100697 lua_pop(L, 2); // MTs
Bram Moolenaarca06da92018-07-01 15:12:05 +0200698 break;
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200699 }
Bram Moolenaarb7828692019-03-23 13:57:02 +0100700 // check dict
Bram Moolenaar1dced572012-04-05 16:54:08 +0200701 luaV_getfield(L, LUAVIM_DICT);
702 if (lua_rawequal(L, -1, -3))
703 {
704 tv->v_type = VAR_DICT;
705 tv->vval.v_dict = *((luaV_Dict *) p);
706 ++tv->vval.v_dict->dv_refcount;
Bram Moolenaarb7828692019-03-23 13:57:02 +0100707 lua_pop(L, 3); // MTs
Bram Moolenaarca06da92018-07-01 15:12:05 +0200708 break;
Bram Moolenaar1dced572012-04-05 16:54:08 +0200709 }
Bram Moolenaarb7828692019-03-23 13:57:02 +0100710 // check blob
711 luaV_getfield(L, LUAVIM_BLOB);
Bram Moolenaarca06da92018-07-01 15:12:05 +0200712 if (lua_rawequal(L, -1, -4))
713 {
Bram Moolenaarb7828692019-03-23 13:57:02 +0100714 tv->v_type = VAR_BLOB;
715 tv->vval.v_blob = *((luaV_Blob *) p);
716 ++tv->vval.v_blob->bv_refcount;
717 lua_pop(L, 4); // MTs
718 break;
719 }
720 // check funcref
721 luaV_getfield(L, LUAVIM_FUNCREF);
722 if (lua_rawequal(L, -1, -5))
723 {
Bram Moolenaarca06da92018-07-01 15:12:05 +0200724 luaV_Funcref *f = (luaV_Funcref *) p;
Bram Moolenaar066e7da2020-07-18 12:50:35 +0200725
Bram Moolenaar4eefe472019-03-19 21:59:19 +0100726 func_ref(f->name);
727 tv->v_type = VAR_FUNC;
728 tv->vval.v_string = vim_strsave(f->name);
Bram Moolenaarb7828692019-03-23 13:57:02 +0100729 lua_pop(L, 5); // MTs
Bram Moolenaarca06da92018-07-01 15:12:05 +0200730 break;
731 }
Bram Moolenaarb7828692019-03-23 13:57:02 +0100732 lua_pop(L, 4); // MTs
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200733 }
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200734 }
Bram Moolenaarb7828692019-03-23 13:57:02 +0100735 // FALLTHROUGH
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200736 default:
Bram Moolenaar1dced572012-04-05 16:54:08 +0200737 tv->v_type = VAR_NUMBER;
738 tv->vval.v_number = 0;
Bram Moolenaarca06da92018-07-01 15:12:05 +0200739 status = FAIL;
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200740 }
Bram Moolenaarca06da92018-07-01 15:12:05 +0200741 return status;
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200742}
743
Bram Moolenaar2ab2e862019-12-04 21:24:53 +0100744/*
745 * similar to luaL_addlstring, but replaces \0 with \n if toline and
746 * \n with \0 otherwise
747 */
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200748 static void
749luaV_addlstring(luaL_Buffer *b, const char *s, size_t l, int toline)
750{
751 while (l--)
752 {
753 if (*s == '\0' && toline)
754 luaL_addchar(b, '\n');
755 else if (*s == '\n' && !toline)
756 luaL_addchar(b, '\0');
757 else
758 luaL_addchar(b, *s);
759 s++;
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200760 }
761}
762
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200763 static void
764luaV_pushline(lua_State *L, buf_T *buf, linenr_T n)
765{
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200766 const char *s = (const char *) ml_get_buf(buf, n, FALSE);
767 luaL_Buffer b;
768 luaL_buffinit(L, &b);
769 luaV_addlstring(&b, s, strlen(s), 0);
770 luaL_pushresult(&b);
771}
772
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200773 static char_u *
774luaV_toline(lua_State *L, int pos)
775{
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200776 size_t l;
777 const char *s = lua_tolstring(L, pos, &l);
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200778
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200779 luaL_Buffer b;
780 luaL_buffinit(L, &b);
781 luaV_addlstring(&b, s, l, 1);
782 luaL_pushresult(&b);
783 return (char_u *) lua_tostring(L, -1);
784}
785
Bram Moolenaar2ab2e862019-12-04 21:24:53 +0100786/*
787 * pops a string s from the top of the stack and calls mf(t) for pieces t of
788 * s separated by newlines
789 */
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200790 static void
791luaV_msgfunc(lua_State *L, msgfunc_T mf)
792{
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200793 luaL_Buffer b;
794 size_t l;
795 const char *p, *s = lua_tolstring(L, -1, &l);
796 luaL_buffinit(L, &b);
797 luaV_addlstring(&b, s, l, 0);
798 luaL_pushresult(&b);
Bram Moolenaar2ab2e862019-12-04 21:24:53 +0100799 // break string
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200800 p = s = lua_tolstring(L, -1, &l);
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200801 while (l--)
802 {
Bram Moolenaar2ab2e862019-12-04 21:24:53 +0100803 if (*p++ == '\0') // break?
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200804 {
Bram Moolenaarc8970b92020-10-26 20:18:08 +0100805 mf((char *)s);
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200806 s = p;
807 }
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200808 }
Bram Moolenaarc8970b92020-10-26 20:18:08 +0100809 mf((char *)s);
Bram Moolenaar2ab2e862019-12-04 21:24:53 +0100810 lua_pop(L, 2); // original and modified strings
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200811}
812
Bram Moolenaar1dced572012-04-05 16:54:08 +0200813#define luaV_newtype(typ,tname,luatyp,luatname) \
814 static luatyp * \
Bram Moolenaar4eefe472019-03-19 21:59:19 +0100815 luaV_new##tname(lua_State *L, typ *obj) \
Bram Moolenaar1dced572012-04-05 16:54:08 +0200816 { \
817 luatyp *o = (luatyp *) lua_newuserdata(L, sizeof(luatyp)); \
818 *o = obj; \
819 luaV_setudata(L, obj); /* cache[obj] = udata */ \
820 luaV_getfield(L, luatname); \
821 lua_setmetatable(L, -2); \
822 return o; \
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200823 }
Bram Moolenaar1dced572012-04-05 16:54:08 +0200824
825#define luaV_pushtype(typ,tname,luatyp) \
826 static luatyp * \
Bram Moolenaarca06da92018-07-01 15:12:05 +0200827 luaV_push##tname(lua_State *L, typ *obj) \
Bram Moolenaar1dced572012-04-05 16:54:08 +0200828 { \
829 luatyp *o = NULL; \
830 if (obj == NULL) \
831 lua_pushnil(L); \
Bram Moolenaarebfec1c2023-01-22 21:14:53 +0000832 else \
833 { \
Bram Moolenaar1dced572012-04-05 16:54:08 +0200834 luaV_getudata(L, obj); \
835 if (lua_isnil(L, -1)) /* not interned? */ \
836 { \
837 lua_pop(L, 1); \
838 o = luaV_new##tname(L, obj); \
839 } \
840 else \
841 o = (luatyp *) lua_touserdata(L, -1); \
842 } \
843 return o; \
844 }
845
846#define luaV_type_tostring(tname,luatname) \
847 static int \
Bram Moolenaar4eefe472019-03-19 21:59:19 +0100848 luaV_##tname##_tostring(lua_State *L) \
Bram Moolenaar1dced572012-04-05 16:54:08 +0200849 { \
850 lua_pushfstring(L, "%s: %p", luatname, lua_touserdata(L, 1)); \
851 return 1; \
852 }
853
Bram Moolenaar2ab2e862019-12-04 21:24:53 +0100854// ======= List type =======
Bram Moolenaar1dced572012-04-05 16:54:08 +0200855
856 static luaV_List *
Bram Moolenaar4eefe472019-03-19 21:59:19 +0100857luaV_newlist(lua_State *L, list_T *lis)
Bram Moolenaar1dced572012-04-05 16:54:08 +0200858{
859 luaV_List *l = (luaV_List *) lua_newuserdata(L, sizeof(luaV_List));
860 *l = lis;
Bram Moolenaar2ab2e862019-12-04 21:24:53 +0100861 lis->lv_refcount++; // reference in Lua
862 luaV_setudata(L, lis); // cache[lis] = udata
Bram Moolenaar1dced572012-04-05 16:54:08 +0200863 luaV_getfield(L, LUAVIM_LIST);
864 lua_setmetatable(L, -2);
865 return l;
866}
867
868luaV_pushtype(list_T, list, luaV_List)
869luaV_type_tostring(list, LUAVIM_LIST)
870
871 static int
Bram Moolenaar4eefe472019-03-19 21:59:19 +0100872luaV_list_len(lua_State *L)
Bram Moolenaar1dced572012-04-05 16:54:08 +0200873{
874 list_T *l = luaV_unbox(L, luaV_List, 1);
Bram Moolenaarb7828692019-03-23 13:57:02 +0100875 lua_pushinteger(L, (int) list_len(l));
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200876 return 1;
877}
878
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200879 static int
Bram Moolenaar4eefe472019-03-19 21:59:19 +0100880luaV_list_iter(lua_State *L)
Bram Moolenaar1dced572012-04-05 16:54:08 +0200881{
882 listitem_T *li = (listitem_T *) lua_touserdata(L, lua_upvalueindex(2));
883 if (li == NULL) return 0;
884 luaV_pushtypval(L, &li->li_tv);
885 lua_pushlightuserdata(L, (void *) li->li_next);
886 lua_replace(L, lua_upvalueindex(2));
887 return 1;
888}
889
890 static int
Bram Moolenaar4eefe472019-03-19 21:59:19 +0100891luaV_list_call(lua_State *L)
Bram Moolenaar1dced572012-04-05 16:54:08 +0200892{
893 list_T *l = luaV_unbox(L, luaV_List, 1);
Bram Moolenaar2ab2e862019-12-04 21:24:53 +0100894 lua_pushvalue(L, lua_upvalueindex(1)); // pass cache table along
Bram Moolenaar1dced572012-04-05 16:54:08 +0200895 lua_pushlightuserdata(L, (void *) l->lv_first);
896 lua_pushcclosure(L, luaV_list_iter, 2);
897 return 1;
898}
899
900 static int
Bram Moolenaar4eefe472019-03-19 21:59:19 +0100901luaV_list_index(lua_State *L)
Bram Moolenaar1dced572012-04-05 16:54:08 +0200902{
903 list_T *l = luaV_unbox(L, luaV_List, 1);
Bram Moolenaar2ab2e862019-12-04 21:24:53 +0100904 if (lua_isnumber(L, 2)) // list item?
Bram Moolenaar1dced572012-04-05 16:54:08 +0200905 {
Bram Moolenaarbd846172020-06-27 12:32:57 +0200906 long n = (long) luaL_checkinteger(L, 2);
907 listitem_T *li;
908
909 // Lua array index starts with 1 while Vim uses 0, subtract 1 to
910 // normalize.
911 n -= 1;
912 li = list_find(l, n);
Bram Moolenaar1dced572012-04-05 16:54:08 +0200913 if (li == NULL)
914 lua_pushnil(L);
915 else
916 luaV_pushtypval(L, &li->li_tv);
917 }
Bram Moolenaar2ab2e862019-12-04 21:24:53 +0100918 else if (lua_isstring(L, 2)) // method?
Bram Moolenaar1dced572012-04-05 16:54:08 +0200919 {
920 const char *s = lua_tostring(L, 2);
921 if (strncmp(s, "add", 3) == 0
Bram Moolenaarb3766472013-04-15 13:49:21 +0200922 || strncmp(s, "insert", 6) == 0)
Bram Moolenaar1dced572012-04-05 16:54:08 +0200923 {
924 lua_getmetatable(L, 1);
925 lua_getfield(L, -1, s);
926 }
927 else
928 lua_pushnil(L);
929 }
930 else
931 lua_pushnil(L);
932 return 1;
933}
934
935 static int
Bram Moolenaar4eefe472019-03-19 21:59:19 +0100936luaV_list_newindex(lua_State *L)
Bram Moolenaar1dced572012-04-05 16:54:08 +0200937{
938 list_T *l = luaV_unbox(L, luaV_List, 1);
939 long n = (long) luaL_checkinteger(L, 2);
940 listitem_T *li;
Bram Moolenaarbd846172020-06-27 12:32:57 +0200941
942 // Lua array index starts with 1 while Vim uses 0, subtract 1 to normalize.
943 n -= 1;
944
Bram Moolenaar1dced572012-04-05 16:54:08 +0200945 if (l->lv_lock)
946 luaL_error(L, "list is locked");
947 li = list_find(l, n);
Bram Moolenaara1f9f862020-06-28 22:41:26 +0200948 if (li == NULL)
Bram Moolenaar1dced572012-04-05 16:54:08 +0200949 {
Bram Moolenaar6ed545e2022-05-09 20:09:23 +0100950 if (!lua_isnil(L, 3))
951 {
952 typval_T v;
953 luaV_checktypval(L, 3, &v, "inserting list item");
954 if (list_insert_tv(l, &v, li) == FAIL)
955 luaL_error(L, "failed to add item to list");
956 clear_tv(&v);
957 }
Bram Moolenaar1dced572012-04-05 16:54:08 +0200958 }
959 else
960 {
Bram Moolenaar6ed545e2022-05-09 20:09:23 +0100961 if (lua_isnil(L, 3)) // remove?
962 {
Bram Moolenaara1f9f862020-06-28 22:41:26 +0200963 vimlist_remove(l, li, li);
964 listitem_free(l, li);
Bram Moolenaar6ed545e2022-05-09 20:09:23 +0100965 }
966 else
967 {
Bram Moolenaara1f9f862020-06-28 22:41:26 +0200968 typval_T v;
969 luaV_checktypval(L, 3, &v, "setting list item");
970 clear_tv(&li->li_tv);
Bram Moolenaare49b8e82020-07-01 13:52:55 +0200971 li->li_tv = v;
Bram Moolenaar6ed545e2022-05-09 20:09:23 +0100972 }
Bram Moolenaar1dced572012-04-05 16:54:08 +0200973 }
974 return 0;
975}
976
977 static int
Bram Moolenaar4eefe472019-03-19 21:59:19 +0100978luaV_list_add(lua_State *L)
Bram Moolenaar1dced572012-04-05 16:54:08 +0200979{
980 luaV_List *lis = luaV_checkudata(L, 1, LUAVIM_LIST);
981 list_T *l = (list_T *) luaV_checkcache(L, (void *) *lis);
Bram Moolenaarb3766472013-04-15 13:49:21 +0200982 typval_T v;
Bram Moolenaar1dced572012-04-05 16:54:08 +0200983 if (l->lv_lock)
984 luaL_error(L, "list is locked");
Bram Moolenaarb3766472013-04-15 13:49:21 +0200985 lua_settop(L, 2);
Bram Moolenaarca06da92018-07-01 15:12:05 +0200986 luaV_checktypval(L, 2, &v, "adding list item");
Bram Moolenaarb3766472013-04-15 13:49:21 +0200987 if (list_append_tv(l, &v) == FAIL)
Bram Moolenaarca06da92018-07-01 15:12:05 +0200988 luaL_error(L, "failed to add item to list");
Bram Moolenaarb3766472013-04-15 13:49:21 +0200989 clear_tv(&v);
Bram Moolenaar1dced572012-04-05 16:54:08 +0200990 lua_settop(L, 1);
991 return 1;
992}
993
994 static int
Bram Moolenaar4eefe472019-03-19 21:59:19 +0100995luaV_list_insert(lua_State *L)
Bram Moolenaar1dced572012-04-05 16:54:08 +0200996{
997 luaV_List *lis = luaV_checkudata(L, 1, LUAVIM_LIST);
998 list_T *l = (list_T *) luaV_checkcache(L, (void *) *lis);
Bram Moolenaar46538ee2015-02-17 16:28:55 +0100999 long pos = (long) luaL_optinteger(L, 3, 0);
Bram Moolenaar1dced572012-04-05 16:54:08 +02001000 listitem_T *li = NULL;
1001 typval_T v;
1002 if (l->lv_lock)
1003 luaL_error(L, "list is locked");
1004 if (pos < l->lv_len)
1005 {
1006 li = list_find(l, pos);
1007 if (li == NULL)
1008 luaL_error(L, "invalid position");
1009 }
1010 lua_settop(L, 2);
Bram Moolenaarca06da92018-07-01 15:12:05 +02001011 luaV_checktypval(L, 2, &v, "inserting list item");
Bram Moolenaarb3766472013-04-15 13:49:21 +02001012 if (list_insert_tv(l, &v, li) == FAIL)
Bram Moolenaarca06da92018-07-01 15:12:05 +02001013 luaL_error(L, "failed to add item to list");
Bram Moolenaarb3766472013-04-15 13:49:21 +02001014 clear_tv(&v);
Bram Moolenaar1dced572012-04-05 16:54:08 +02001015 lua_settop(L, 1);
1016 return 1;
1017}
1018
1019static const luaL_Reg luaV_List_mt[] = {
1020 {"__tostring", luaV_list_tostring},
Bram Moolenaar1dced572012-04-05 16:54:08 +02001021 {"__len", luaV_list_len},
1022 {"__call", luaV_list_call},
1023 {"__index", luaV_list_index},
1024 {"__newindex", luaV_list_newindex},
1025 {"add", luaV_list_add},
1026 {"insert", luaV_list_insert},
1027 {NULL, NULL}
1028};
1029
1030
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001031// ======= Dict type =======
Bram Moolenaar1dced572012-04-05 16:54:08 +02001032
1033 static luaV_Dict *
Bram Moolenaar4eefe472019-03-19 21:59:19 +01001034luaV_newdict(lua_State *L, dict_T *dic)
Bram Moolenaar1dced572012-04-05 16:54:08 +02001035{
1036 luaV_Dict *d = (luaV_Dict *) lua_newuserdata(L, sizeof(luaV_Dict));
1037 *d = dic;
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001038 dic->dv_refcount++; // reference in Lua
1039 luaV_setudata(L, dic); // cache[dic] = udata
Bram Moolenaar1dced572012-04-05 16:54:08 +02001040 luaV_getfield(L, LUAVIM_DICT);
1041 lua_setmetatable(L, -2);
1042 return d;
1043}
1044
1045luaV_pushtype(dict_T, dict, luaV_Dict)
1046luaV_type_tostring(dict, LUAVIM_DICT)
1047
1048 static int
Bram Moolenaar4eefe472019-03-19 21:59:19 +01001049luaV_dict_len(lua_State *L)
Bram Moolenaar1dced572012-04-05 16:54:08 +02001050{
1051 dict_T *d = luaV_unbox(L, luaV_Dict, 1);
Bram Moolenaarb7828692019-03-23 13:57:02 +01001052 lua_pushinteger(L, (int) dict_len(d));
Bram Moolenaar1dced572012-04-05 16:54:08 +02001053 return 1;
1054}
1055
1056 static int
Bram Moolenaar4eefe472019-03-19 21:59:19 +01001057luaV_dict_iter(lua_State *L UNUSED)
Bram Moolenaar1dced572012-04-05 16:54:08 +02001058{
Bram Moolenaarfeeaa682013-02-14 22:19:51 +01001059#ifdef FEAT_EVAL
Bram Moolenaar1dced572012-04-05 16:54:08 +02001060 hashitem_T *hi = (hashitem_T *) lua_touserdata(L, lua_upvalueindex(2));
1061 int n = lua_tointeger(L, lua_upvalueindex(3));
1062 dictitem_T *di;
1063 if (n <= 0) return 0;
1064 while (HASHITEM_EMPTY(hi)) hi++;
1065 di = dict_lookup(hi);
1066 lua_pushstring(L, (char *) hi->hi_key);
1067 luaV_pushtypval(L, &di->di_tv);
1068 lua_pushlightuserdata(L, (void *) (hi + 1));
1069 lua_replace(L, lua_upvalueindex(2));
1070 lua_pushinteger(L, n - 1);
1071 lua_replace(L, lua_upvalueindex(3));
1072 return 2;
Bram Moolenaarfeeaa682013-02-14 22:19:51 +01001073#else
1074 return 0;
1075#endif
Bram Moolenaar1dced572012-04-05 16:54:08 +02001076}
1077
1078 static int
Bram Moolenaar4eefe472019-03-19 21:59:19 +01001079luaV_dict_call(lua_State *L)
Bram Moolenaar1dced572012-04-05 16:54:08 +02001080{
1081 dict_T *d = luaV_unbox(L, luaV_Dict, 1);
1082 hashtab_T *ht = &d->dv_hashtab;
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001083 lua_pushvalue(L, lua_upvalueindex(1)); // pass cache table along
Bram Moolenaar1dced572012-04-05 16:54:08 +02001084 lua_pushlightuserdata(L, (void *) ht->ht_array);
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001085 lua_pushinteger(L, ht->ht_used); // # remaining items
Bram Moolenaar1dced572012-04-05 16:54:08 +02001086 lua_pushcclosure(L, luaV_dict_iter, 3);
1087 return 1;
1088}
1089
1090 static int
Bram Moolenaarca06da92018-07-01 15:12:05 +02001091luaV_dict_index(lua_State *L)
Bram Moolenaar1dced572012-04-05 16:54:08 +02001092{
1093 dict_T *d = luaV_unbox(L, luaV_Dict, 1);
1094 char_u *key = (char_u *) luaL_checkstring(L, 2);
1095 dictitem_T *di = dict_find(d, key, -1);
Bram Moolenaarca06da92018-07-01 15:12:05 +02001096
Bram Moolenaar1dced572012-04-05 16:54:08 +02001097 if (di == NULL)
Bram Moolenaarca06da92018-07-01 15:12:05 +02001098 {
Yegappan Lakshmanan0233bdf2023-01-12 12:33:30 +00001099 lua_pushnil(L);
1100 return 1;
Bram Moolenaarca06da92018-07-01 15:12:05 +02001101 }
Yegappan Lakshmanan0233bdf2023-01-12 12:33:30 +00001102
1103 luaV_pushtypval(L, &di->di_tv);
1104 if (di->di_tv.v_type == VAR_FUNC) // funcref?
1105 {
1106 luaV_Funcref *f = (luaV_Funcref *) lua_touserdata(L, -1);
1107 f->self = d; // keep "self" reference
1108 d->dv_refcount++;
1109 }
1110
Bram Moolenaar1dced572012-04-05 16:54:08 +02001111 return 1;
1112}
1113
1114 static int
Bram Moolenaarca06da92018-07-01 15:12:05 +02001115luaV_dict_newindex(lua_State *L)
Bram Moolenaar1dced572012-04-05 16:54:08 +02001116{
1117 dict_T *d = luaV_unbox(L, luaV_Dict, 1);
1118 char_u *key = (char_u *) luaL_checkstring(L, 2);
1119 dictitem_T *di;
Bram Moolenaare49b8e82020-07-01 13:52:55 +02001120 typval_T tv;
Bram Moolenaar713bf9e92019-03-16 16:38:41 +01001121
Bram Moolenaar1dced572012-04-05 16:54:08 +02001122 if (d->dv_lock)
1123 luaL_error(L, "dict is locked");
Bram Moolenaard6ef5f92018-07-13 22:08:23 +02001124 if (key == NULL)
1125 return 0;
1126 if (*key == NUL)
Bram Moolenaarca06da92018-07-01 15:12:05 +02001127 luaL_error(L, "empty key");
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001128 if (!lua_isnil(L, 3)) // read value?
Bram Moolenaar17413672018-07-14 20:49:42 +02001129 {
Bram Moolenaare49b8e82020-07-01 13:52:55 +02001130 luaV_checktypval(L, 3, &tv, "setting dict item");
1131 if (d->dv_scope == VAR_DEF_SCOPE && tv.v_type == VAR_FUNC)
1132 {
1133 clear_tv(&tv);
Bram Moolenaarca06da92018-07-01 15:12:05 +02001134 luaL_error(L, "cannot assign funcref to builtin scope");
Bram Moolenaare49b8e82020-07-01 13:52:55 +02001135 }
Bram Moolenaarca06da92018-07-01 15:12:05 +02001136 }
Bram Moolenaar1dced572012-04-05 16:54:08 +02001137 di = dict_find(d, key, -1);
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001138 if (di == NULL) // non-existing key?
Bram Moolenaar1dced572012-04-05 16:54:08 +02001139 {
Bram Moolenaard6ef5f92018-07-13 22:08:23 +02001140 if (lua_isnil(L, 3))
1141 return 0;
Bram Moolenaar1dced572012-04-05 16:54:08 +02001142 di = dictitem_alloc(key);
Bram Moolenaard6ef5f92018-07-13 22:08:23 +02001143 if (di == NULL)
Bram Moolenaare49b8e82020-07-01 13:52:55 +02001144 {
1145 clear_tv(&tv);
Bram Moolenaard6ef5f92018-07-13 22:08:23 +02001146 return 0;
Bram Moolenaare49b8e82020-07-01 13:52:55 +02001147 }
Bram Moolenaar1dced572012-04-05 16:54:08 +02001148 if (dict_add(d, di) == FAIL)
1149 {
Bram Moolenaard6ef5f92018-07-13 22:08:23 +02001150 vim_free(di);
Bram Moolenaare49b8e82020-07-01 13:52:55 +02001151 clear_tv(&tv);
Bram Moolenaard6ef5f92018-07-13 22:08:23 +02001152 return 0;
Bram Moolenaar1dced572012-04-05 16:54:08 +02001153 }
1154 }
1155 else
1156 clear_tv(&di->di_tv);
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001157 if (lua_isnil(L, 3)) // remove?
Bram Moolenaar1dced572012-04-05 16:54:08 +02001158 {
1159 hashitem_T *hi = hash_find(&d->dv_hashtab, di->di_key);
Bram Moolenaaref2c3252022-11-25 16:31:51 +00001160 hash_remove(&d->dv_hashtab, hi, "Lua new index");
Bram Moolenaar1dced572012-04-05 16:54:08 +02001161 dictitem_free(di);
1162 }
Bram Moolenaarca06da92018-07-01 15:12:05 +02001163 else
Bram Moolenaare49b8e82020-07-01 13:52:55 +02001164 di->di_tv = tv;
Bram Moolenaar1dced572012-04-05 16:54:08 +02001165 return 0;
1166}
1167
1168static const luaL_Reg luaV_Dict_mt[] = {
1169 {"__tostring", luaV_dict_tostring},
Bram Moolenaar1dced572012-04-05 16:54:08 +02001170 {"__len", luaV_dict_len},
1171 {"__call", luaV_dict_call},
1172 {"__index", luaV_dict_index},
1173 {"__newindex", luaV_dict_newindex},
1174 {NULL, NULL}
1175};
1176
1177
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001178// ======= Blob type =======
Bram Moolenaarb7828692019-03-23 13:57:02 +01001179
1180 static luaV_Blob *
1181luaV_newblob(lua_State *L, blob_T *blo)
1182{
1183 luaV_Blob *b = (luaV_Blob *) lua_newuserdata(L, sizeof(luaV_Blob));
1184 *b = blo;
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001185 blo->bv_refcount++; // reference in Lua
1186 luaV_setudata(L, blo); // cache[blo] = udata
Bram Moolenaarb7828692019-03-23 13:57:02 +01001187 luaV_getfield(L, LUAVIM_BLOB);
1188 lua_setmetatable(L, -2);
1189 return b;
1190}
1191
1192luaV_pushtype(blob_T, blob, luaV_Blob)
1193luaV_type_tostring(blob, LUAVIM_BLOB)
1194
1195 static int
1196luaV_blob_gc(lua_State *L)
1197{
1198 blob_T *b = luaV_unbox(L, luaV_Blob, 1);
1199 blob_unref(b);
1200 return 0;
1201}
1202
1203 static int
1204luaV_blob_len(lua_State *L)
1205{
1206 blob_T *b = luaV_unbox(L, luaV_Blob, 1);
1207 lua_pushinteger(L, (int) blob_len(b));
1208 return 1;
1209}
1210
1211 static int
1212luaV_blob_index(lua_State *L)
1213{
1214 blob_T *b = luaV_unbox(L, luaV_Blob, 1);
1215 if (lua_isnumber(L, 2))
1216 {
1217 int idx = luaL_checkinteger(L, 2);
1218 if (idx < blob_len(b))
1219 lua_pushnumber(L, (lua_Number) blob_get(b, idx));
1220 else
1221 lua_pushnil(L);
1222 }
1223 else if (lua_isstring(L, 2))
1224 {
1225 const char *s = lua_tostring(L, 2);
1226 if (strncmp(s, "add", 3) == 0)
1227 {
1228 lua_getmetatable(L, 1);
1229 lua_getfield(L, -1, s);
1230 }
1231 else
1232 lua_pushnil(L);
1233 }
1234 else
1235 lua_pushnil(L);
1236 return 1;
1237}
1238
1239 static int
1240luaV_blob_newindex(lua_State *L)
1241{
1242 blob_T *b = luaV_unbox(L, luaV_Blob, 1);
1243 if (b->bv_lock)
1244 luaL_error(L, "blob is locked");
Yegappan Lakshmanan0233bdf2023-01-12 12:33:30 +00001245
1246 if (!lua_isnumber(L, 2))
1247 return 0;
1248
1249 long len = blob_len(b);
1250 int idx = luaL_checkinteger(L, 2);
1251 int val = luaL_checkinteger(L, 3);
1252 if (idx < len || (idx == len && ga_grow(&b->bv_ga, 1) == OK))
Bram Moolenaarb7828692019-03-23 13:57:02 +01001253 {
Yegappan Lakshmanan0233bdf2023-01-12 12:33:30 +00001254 blob_set(b, idx, (char_u) val);
1255 if (idx == len)
1256 ++b->bv_ga.ga_len;
Bram Moolenaarb7828692019-03-23 13:57:02 +01001257 }
Yegappan Lakshmanan0233bdf2023-01-12 12:33:30 +00001258 else
1259 luaL_error(L, "index out of range");
1260
Bram Moolenaarb7828692019-03-23 13:57:02 +01001261 return 0;
1262}
1263
1264 static int
1265luaV_blob_add(lua_State *L)
1266{
1267 luaV_Blob *blo = luaV_checkudata(L, 1, LUAVIM_BLOB);
1268 blob_T *b = (blob_T *) luaV_checkcache(L, (void *) *blo);
1269 if (b->bv_lock)
1270 luaL_error(L, "blob is locked");
1271 lua_settop(L, 2);
1272 if (!lua_isstring(L, 2))
1273 luaL_error(L, "string expected, got %s", luaL_typename(L, 2));
1274 else
1275 {
1276 size_t i, l = 0;
1277 const char *s = lua_tolstring(L, 2, &l);
1278
Bram Moolenaar5f1d3ae2020-02-11 22:37:35 +01001279 if (ga_grow(&b->bv_ga, (int)l) == OK)
Bram Moolenaar6fb5c972019-03-26 21:44:20 +01001280 for (i = 0; i < l; ++i)
1281 ga_append(&b->bv_ga, s[i]);
Bram Moolenaarb7828692019-03-23 13:57:02 +01001282 }
1283 lua_settop(L, 1);
1284 return 1;
1285}
1286
1287static const luaL_Reg luaV_Blob_mt[] = {
1288 {"__tostring", luaV_blob_tostring},
1289 {"__gc", luaV_blob_gc},
1290 {"__len", luaV_blob_len},
1291 {"__index", luaV_blob_index},
1292 {"__newindex", luaV_blob_newindex},
1293 {"add", luaV_blob_add},
1294 {NULL, NULL}
1295};
1296
1297
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001298// ======= Funcref type =======
Bram Moolenaarca06da92018-07-01 15:12:05 +02001299
1300 static luaV_Funcref *
1301luaV_newfuncref(lua_State *L, char_u *name)
1302{
1303 luaV_Funcref *f = (luaV_Funcref *)lua_newuserdata(L, sizeof(luaV_Funcref));
1304
1305 if (name != NULL)
1306 {
Bram Moolenaar4eefe472019-03-19 21:59:19 +01001307 func_ref(name);
1308 f->name = vim_strsave(name);
Bram Moolenaarca06da92018-07-01 15:12:05 +02001309 }
Bram Moolenaarca06da92018-07-01 15:12:05 +02001310 f->self = NULL;
1311 luaV_getfield(L, LUAVIM_FUNCREF);
1312 lua_setmetatable(L, -2);
1313 return f;
1314}
1315
1316 static luaV_Funcref *
Bram Moolenaar4eefe472019-03-19 21:59:19 +01001317luaV_pushfuncref(lua_State *L, char_u *name)
Bram Moolenaarca06da92018-07-01 15:12:05 +02001318{
Bram Moolenaar4eefe472019-03-19 21:59:19 +01001319 return luaV_newfuncref(L, name);
Bram Moolenaarca06da92018-07-01 15:12:05 +02001320}
1321
1322
1323luaV_type_tostring(funcref, LUAVIM_FUNCREF)
1324
1325 static int
1326luaV_funcref_gc(lua_State *L)
1327{
1328 luaV_Funcref *f = (luaV_Funcref *) lua_touserdata(L, 1);
1329
Bram Moolenaar4eefe472019-03-19 21:59:19 +01001330 func_unref(f->name);
1331 vim_free(f->name);
1332 // NOTE: Don't call "dict_unref(f->self)", because the dict of "f->self"
1333 // will be (or has been already) freed by Vim's garbage collection.
Bram Moolenaarca06da92018-07-01 15:12:05 +02001334 return 0;
1335}
1336
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001337// equivalent to string(funcref)
Bram Moolenaarca06da92018-07-01 15:12:05 +02001338 static int
1339luaV_funcref_len(lua_State *L)
1340{
1341 luaV_Funcref *f = (luaV_Funcref *) lua_touserdata(L, 1);
1342
Bram Moolenaar4eefe472019-03-19 21:59:19 +01001343 lua_pushstring(L, (const char *) f->name);
Bram Moolenaarca06da92018-07-01 15:12:05 +02001344 return 1;
1345}
1346
1347 static int
1348luaV_funcref_call(lua_State *L)
1349{
1350 luaV_Funcref *f = (luaV_Funcref *) lua_touserdata(L, 1);
Bram Moolenaar4eefe472019-03-19 21:59:19 +01001351 int i, n = lua_gettop(L) - 1; // #args
1352 int status = FAIL;
1353 typval_T args;
1354 typval_T rettv;
Bram Moolenaarca06da92018-07-01 15:12:05 +02001355
Bram Moolenaar4eefe472019-03-19 21:59:19 +01001356 args.v_type = VAR_LIST;
1357 args.vval.v_list = list_alloc();
1358 rettv.v_type = VAR_UNKNOWN; // as in clear_tv
1359 if (args.vval.v_list != NULL)
Bram Moolenaard6ef5f92018-07-13 22:08:23 +02001360 {
Bram Moolenaar4eefe472019-03-19 21:59:19 +01001361 typval_T v;
1362
Bram Moolenaar17413672018-07-14 20:49:42 +02001363 for (i = 0; i < n; i++)
1364 {
Bram Moolenaard6ef5f92018-07-13 22:08:23 +02001365 luaV_checktypval(L, i + 2, &v, "calling funcref");
Bram Moolenaar4eefe472019-03-19 21:59:19 +01001366 list_append_tv(args.vval.v_list, &v);
Bram Moolenaar713bf9e92019-03-16 16:38:41 +01001367 clear_tv(&v);
Bram Moolenaard6ef5f92018-07-13 22:08:23 +02001368 }
Bram Moolenaar4eefe472019-03-19 21:59:19 +01001369 status = func_call(f->name, &args, NULL, f->self, &rettv);
Bram Moolenaard6ef5f92018-07-13 22:08:23 +02001370 if (status == OK)
1371 luaV_pushtypval(L, &rettv);
Bram Moolenaar4eefe472019-03-19 21:59:19 +01001372 clear_tv(&args);
Bram Moolenaard6ef5f92018-07-13 22:08:23 +02001373 clear_tv(&rettv);
Bram Moolenaarca06da92018-07-01 15:12:05 +02001374 }
Bram Moolenaarca06da92018-07-01 15:12:05 +02001375 if (status != OK)
1376 luaL_error(L, "cannot call funcref");
1377 return 1;
1378}
1379
1380static const luaL_Reg luaV_Funcref_mt[] = {
1381 {"__tostring", luaV_funcref_tostring},
1382 {"__gc", luaV_funcref_gc},
1383 {"__len", luaV_funcref_len},
1384 {"__call", luaV_funcref_call},
1385 {NULL, NULL}
1386};
1387
1388
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001389// ======= Buffer type =======
Bram Moolenaar1dced572012-04-05 16:54:08 +02001390
1391luaV_newtype(buf_T, buffer, luaV_Buffer, LUAVIM_BUFFER)
1392luaV_pushtype(buf_T, buffer, luaV_Buffer)
1393luaV_type_tostring(buffer, LUAVIM_BUFFER)
1394
1395 static int
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001396luaV_buffer_len(lua_State *L)
1397{
Bram Moolenaar1dced572012-04-05 16:54:08 +02001398 buf_T *b = (buf_T *) luaV_checkvalid(L, luaV_Buffer, 1);
1399 lua_pushinteger(L, b->b_ml.ml_line_count);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001400 return 1;
1401}
1402
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001403 static int
1404luaV_buffer_call(lua_State *L)
1405{
Bram Moolenaar1dced572012-04-05 16:54:08 +02001406 buf_T *b = (buf_T *) luaV_checkvalid(L, luaV_Buffer, 1);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001407 lua_settop(L, 1);
Bram Moolenaar1dced572012-04-05 16:54:08 +02001408 set_curbuf(b, DOBUF_SPLIT);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001409 return 1;
1410}
1411
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001412 static int
1413luaV_buffer_index(lua_State *L)
1414{
Bram Moolenaar1dced572012-04-05 16:54:08 +02001415 buf_T *b = (buf_T *) luaV_checkvalid(L, luaV_Buffer, 1);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001416 linenr_T n = (linenr_T) lua_tointeger(L, 2);
Bram Moolenaar1dced572012-04-05 16:54:08 +02001417 if (n > 0 && n <= b->b_ml.ml_line_count)
1418 luaV_pushline(L, b, n);
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001419 else if (lua_isstring(L, 2))
1420 {
1421 const char *s = lua_tostring(L, 2);
1422 if (strncmp(s, "name", 4) == 0)
Bram Moolenaarfe08df42018-07-07 23:07:41 +02001423 lua_pushstring(L, (b->b_sfname == NULL)
1424 ? "" : (char *) b->b_sfname);
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001425 else if (strncmp(s, "fname", 5) == 0)
Bram Moolenaarfe08df42018-07-07 23:07:41 +02001426 lua_pushstring(L, (b->b_ffname == NULL)
1427 ? "" : (char *) b->b_ffname);
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001428 else if (strncmp(s, "number", 6) == 0)
Bram Moolenaar1dced572012-04-05 16:54:08 +02001429 lua_pushinteger(L, b->b_fnum);
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001430 // methods
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001431 else if (strncmp(s, "insert", 6) == 0
1432 || strncmp(s, "next", 4) == 0
1433 || strncmp(s, "previous", 8) == 0
1434 || strncmp(s, "isvalid", 7) == 0)
1435 {
1436 lua_getmetatable(L, 1);
1437 lua_getfield(L, -1, s);
1438 }
1439 else
1440 lua_pushnil(L);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001441 }
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001442 else
1443 lua_pushnil(L);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001444 return 1;
1445}
1446
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001447 static int
1448luaV_buffer_newindex(lua_State *L)
1449{
Bram Moolenaar1dced572012-04-05 16:54:08 +02001450 buf_T *b = (buf_T *) luaV_checkvalid(L, luaV_Buffer, 1);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001451 linenr_T n = (linenr_T) luaL_checkinteger(L, 2);
1452#ifdef HAVE_SANDBOX
1453 luaV_checksandbox(L);
1454#endif
Bram Moolenaar1dced572012-04-05 16:54:08 +02001455 if (n < 1 || n > b->b_ml.ml_line_count)
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001456 luaL_error(L, "invalid line number");
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001457 if (lua_isnil(L, 3)) // delete line
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001458 {
1459 buf_T *buf = curbuf;
Bram Moolenaar1dced572012-04-05 16:54:08 +02001460 curbuf = b;
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001461 if (u_savedel(n, 1L) == FAIL)
1462 {
1463 curbuf = buf;
1464 luaL_error(L, "cannot save undo information");
1465 }
Bram Moolenaarca70c072020-05-30 20:30:46 +02001466 else if (ml_delete(n) == FAIL)
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001467 {
1468 curbuf = buf;
1469 luaL_error(L, "cannot delete line");
1470 }
Bram Moolenaarca06da92018-07-01 15:12:05 +02001471 else
1472 {
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001473 deleted_lines_mark(n, 1L);
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001474 if (b == curwin->w_buffer) // fix cursor in current window?
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001475 {
1476 if (curwin->w_cursor.lnum >= n)
1477 {
1478 if (curwin->w_cursor.lnum > n)
1479 {
1480 curwin->w_cursor.lnum -= 1;
1481 check_cursor_col();
1482 }
Bram Moolenaare49b8e82020-07-01 13:52:55 +02001483 else
1484 check_cursor();
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001485 changed_cline_bef_curs();
1486 }
1487 invalidate_botline();
1488 }
1489 }
1490 curbuf = buf;
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001491 }
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001492 else if (lua_isstring(L, 3)) // update line
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001493 {
1494 buf_T *buf = curbuf;
Bram Moolenaar1dced572012-04-05 16:54:08 +02001495 curbuf = b;
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001496 if (u_savesub(n) == FAIL)
1497 {
1498 curbuf = buf;
1499 luaL_error(L, "cannot save undo information");
1500 }
1501 else if (ml_replace(n, luaV_toline(L, 3), TRUE) == FAIL)
1502 {
1503 curbuf = buf;
1504 luaL_error(L, "cannot replace line");
1505 }
Bram Moolenaar113d9de2022-08-08 15:49:18 +01001506 else
1507 changed_bytes(n, 0);
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001508 curbuf = buf;
Bram Moolenaar1dced572012-04-05 16:54:08 +02001509 if (b == curwin->w_buffer)
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001510 check_cursor_col();
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001511 }
1512 else
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001513 luaL_error(L, "wrong argument to change line");
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001514 return 0;
1515}
1516
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001517 static int
1518luaV_buffer_insert(lua_State *L)
1519{
Bram Moolenaar1dced572012-04-05 16:54:08 +02001520 luaV_Buffer *lb = luaV_checkudata(L, 1, LUAVIM_BUFFER);
1521 buf_T *b = (buf_T *) luaV_checkcache(L, (void *) *lb);
1522 linenr_T last = b->b_ml.ml_line_count;
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001523 linenr_T n = (linenr_T) luaL_optinteger(L, 3, last);
1524 buf_T *buf;
1525 luaL_checktype(L, 2, LUA_TSTRING);
1526#ifdef HAVE_SANDBOX
1527 luaV_checksandbox(L);
1528#endif
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001529 // fix insertion line
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001530 if (n < 0) n = 0;
1531 if (n > last) n = last;
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001532 // insert
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001533 buf = curbuf;
Bram Moolenaar1dced572012-04-05 16:54:08 +02001534 curbuf = b;
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001535 if (u_save(n, n + 1) == FAIL)
1536 {
1537 curbuf = buf;
1538 luaL_error(L, "cannot save undo information");
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001539 }
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001540 else if (ml_append(n, luaV_toline(L, 2), 0, FALSE) == FAIL)
1541 {
1542 curbuf = buf;
1543 luaL_error(L, "cannot insert line");
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001544 }
1545 else
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001546 appended_lines_mark(n, 1L);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001547 curbuf = buf;
Bram Moolenaara4d158b2022-08-14 14:17:45 +01001548 update_screen(UPD_VALID);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001549 return 0;
1550}
1551
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001552 static int
1553luaV_buffer_next(lua_State *L)
1554{
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001555 luaV_Buffer *b = luaV_checkudata(L, 1, LUAVIM_BUFFER);
Bram Moolenaar1dced572012-04-05 16:54:08 +02001556 buf_T *buf = (buf_T *) luaV_checkcache(L, (void *) *b);
1557 luaV_pushbuffer(L, buf->b_next);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001558 return 1;
1559}
1560
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001561 static int
1562luaV_buffer_previous(lua_State *L)
1563{
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001564 luaV_Buffer *b = luaV_checkudata(L, 1, LUAVIM_BUFFER);
Bram Moolenaar1dced572012-04-05 16:54:08 +02001565 buf_T *buf = (buf_T *) luaV_checkcache(L, (void *) *b);
1566 luaV_pushbuffer(L, buf->b_prev);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001567 return 1;
1568}
1569
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001570 static int
1571luaV_buffer_isvalid(lua_State *L)
1572{
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001573 luaV_Buffer *b = luaV_checkudata(L, 1, LUAVIM_BUFFER);
Bram Moolenaar1dced572012-04-05 16:54:08 +02001574 luaV_getudata(L, *b);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001575 lua_pushboolean(L, !lua_isnil(L, -1));
1576 return 1;
1577}
1578
1579static const luaL_Reg luaV_Buffer_mt[] = {
1580 {"__tostring", luaV_buffer_tostring},
1581 {"__len", luaV_buffer_len},
1582 {"__call", luaV_buffer_call},
1583 {"__index", luaV_buffer_index},
1584 {"__newindex", luaV_buffer_newindex},
1585 {"insert", luaV_buffer_insert},
1586 {"next", luaV_buffer_next},
1587 {"previous", luaV_buffer_previous},
1588 {"isvalid", luaV_buffer_isvalid},
1589 {NULL, NULL}
1590};
1591
1592
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001593// ======= Window type =======
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001594
Bram Moolenaar1dced572012-04-05 16:54:08 +02001595luaV_newtype(win_T, window, luaV_Window, LUAVIM_WINDOW)
1596luaV_pushtype(win_T, window, luaV_Window)
1597luaV_type_tostring(window, LUAVIM_WINDOW)
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001598
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001599 static int
1600luaV_window_call(lua_State *L)
1601{
Bram Moolenaar1dced572012-04-05 16:54:08 +02001602 win_T *w = (win_T *) luaV_checkvalid(L, luaV_Window, 1);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001603 lua_settop(L, 1);
Bram Moolenaar1dced572012-04-05 16:54:08 +02001604 win_goto(w);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001605 return 1;
1606}
1607
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001608 static int
1609luaV_window_index(lua_State *L)
1610{
Bram Moolenaar1dced572012-04-05 16:54:08 +02001611 win_T *w = (win_T *) luaV_checkvalid(L, luaV_Window, 1);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001612 const char *s = luaL_checkstring(L, 2);
1613 if (strncmp(s, "buffer", 6) == 0)
Bram Moolenaar1dced572012-04-05 16:54:08 +02001614 luaV_pushbuffer(L, w->w_buffer);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001615 else if (strncmp(s, "line", 4) == 0)
Bram Moolenaar1dced572012-04-05 16:54:08 +02001616 lua_pushinteger(L, w->w_cursor.lnum);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001617 else if (strncmp(s, "col", 3) == 0)
Bram Moolenaar1dced572012-04-05 16:54:08 +02001618 lua_pushinteger(L, w->w_cursor.col + 1);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001619 else if (strncmp(s, "width", 5) == 0)
Bram Moolenaar02631462017-09-22 15:20:32 +02001620 lua_pushinteger(L, w->w_width);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001621 else if (strncmp(s, "height", 6) == 0)
Bram Moolenaar1dced572012-04-05 16:54:08 +02001622 lua_pushinteger(L, w->w_height);
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001623 // methods
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001624 else if (strncmp(s, "next", 4) == 0
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001625 || strncmp(s, "previous", 8) == 0
1626 || strncmp(s, "isvalid", 7) == 0)
1627 {
1628 lua_getmetatable(L, 1);
1629 lua_getfield(L, -1, s);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001630 }
1631 else
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001632 lua_pushnil(L);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001633 return 1;
1634}
1635
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001636 static int
Bram Moolenaar4eefe472019-03-19 21:59:19 +01001637luaV_window_newindex(lua_State *L)
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001638{
Bram Moolenaar1dced572012-04-05 16:54:08 +02001639 win_T *w = (win_T *) luaV_checkvalid(L, luaV_Window, 1);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001640 const char *s = luaL_checkstring(L, 2);
1641 int v = luaL_checkinteger(L, 3);
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001642 if (strncmp(s, "line", 4) == 0)
1643 {
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001644#ifdef HAVE_SANDBOX
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001645 luaV_checksandbox(L);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001646#endif
Bram Moolenaar1dced572012-04-05 16:54:08 +02001647 if (v < 1 || v > w->w_buffer->b_ml.ml_line_count)
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001648 luaL_error(L, "line out of range");
Bram Moolenaar1dced572012-04-05 16:54:08 +02001649 w->w_cursor.lnum = v;
Bram Moolenaara4d158b2022-08-14 14:17:45 +01001650 update_screen(UPD_VALID);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001651 }
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001652 else if (strncmp(s, "col", 3) == 0)
1653 {
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001654#ifdef HAVE_SANDBOX
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001655 luaV_checksandbox(L);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001656#endif
Bram Moolenaar1dced572012-04-05 16:54:08 +02001657 w->w_cursor.col = v - 1;
Bram Moolenaar53901442018-07-25 22:02:36 +02001658 w->w_set_curswant = TRUE;
Bram Moolenaara4d158b2022-08-14 14:17:45 +01001659 update_screen(UPD_VALID);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001660 }
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001661 else if (strncmp(s, "width", 5) == 0)
1662 {
1663 win_T *win = curwin;
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001664#ifdef FEAT_GUI
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001665 need_mouse_correct = TRUE;
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001666#endif
Bram Moolenaar1dced572012-04-05 16:54:08 +02001667 curwin = w;
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001668 win_setwidth(v);
1669 curwin = win;
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001670 }
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001671 else if (strncmp(s, "height", 6) == 0)
1672 {
1673 win_T *win = curwin;
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001674#ifdef FEAT_GUI
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001675 need_mouse_correct = TRUE;
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001676#endif
Bram Moolenaar1dced572012-04-05 16:54:08 +02001677 curwin = w;
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001678 win_setheight(v);
1679 curwin = win;
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001680 }
1681 else
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001682 luaL_error(L, "invalid window property: `%s'", s);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001683 return 0;
1684}
1685
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001686 static int
1687luaV_window_next(lua_State *L)
1688{
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001689 luaV_Window *w = luaV_checkudata(L, 1, LUAVIM_WINDOW);
Bram Moolenaar1dced572012-04-05 16:54:08 +02001690 win_T *win = (win_T *) luaV_checkcache(L, (void *) *w);
1691 luaV_pushwindow(L, win->w_next);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001692 return 1;
1693}
1694
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001695 static int
1696luaV_window_previous(lua_State *L)
1697{
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001698 luaV_Window *w = luaV_checkudata(L, 1, LUAVIM_WINDOW);
Bram Moolenaar1dced572012-04-05 16:54:08 +02001699 win_T *win = (win_T *) luaV_checkcache(L, (void *) *w);
1700 luaV_pushwindow(L, win->w_prev);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001701 return 1;
1702}
1703
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001704 static int
1705luaV_window_isvalid(lua_State *L)
1706{
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001707 luaV_Window *w = luaV_checkudata(L, 1, LUAVIM_WINDOW);
Bram Moolenaar1dced572012-04-05 16:54:08 +02001708 luaV_getudata(L, *w);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001709 lua_pushboolean(L, !lua_isnil(L, -1));
1710 return 1;
1711}
1712
1713static const luaL_Reg luaV_Window_mt[] = {
1714 {"__tostring", luaV_window_tostring},
1715 {"__call", luaV_window_call},
1716 {"__index", luaV_window_index},
1717 {"__newindex", luaV_window_newindex},
1718 {"next", luaV_window_next},
1719 {"previous", luaV_window_previous},
1720 {"isvalid", luaV_window_isvalid},
1721 {NULL, NULL}
1722};
1723
1724
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001725// ======= Vim module =======
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001726
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001727 static int
1728luaV_print(lua_State *L)
1729{
Yegappan Lakshmanan41114a22021-07-29 20:22:14 +02001730 int i, n = lua_gettop(L); // nargs
1731 const char *s;
1732 size_t l;
1733 garray_T msg_ga;
1734
1735 ga_init2(&msg_ga, 1, 128);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001736 lua_getglobal(L, "tostring");
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001737 for (i = 1; i <= n; i++)
1738 {
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001739 lua_pushvalue(L, -1); // tostring
1740 lua_pushvalue(L, i); // arg
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001741 lua_call(L, 1, 1);
1742 s = lua_tolstring(L, -1, &l);
1743 if (s == NULL)
1744 return luaL_error(L, "cannot convert to string");
Bram Moolenaar78e006b2021-07-28 15:07:01 +02001745 if (i > 1)
Yegappan Lakshmanan41114a22021-07-29 20:22:14 +02001746 ga_append(&msg_ga, ' '); // use space instead of tab
1747 ga_concat_len(&msg_ga, (char_u *)s, l);
Bram Moolenaar2a4bd002021-07-28 21:48:59 +02001748 lua_pop(L, 1);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001749 }
Yegappan Lakshmanan41114a22021-07-29 20:22:14 +02001750 // Replace any "\n" with "\0"
1751 for (i = 0; i < msg_ga.ga_len; i++)
1752 if (((char *)msg_ga.ga_data)[i] == '\n')
1753 ((char *)msg_ga.ga_data)[i] = '\0';
1754 lua_pushlstring(L, msg_ga.ga_data, msg_ga.ga_len);
Bram Moolenaarb98678a2019-10-19 15:18:44 +02001755 if (!got_int)
1756 luaV_msg(L);
Yegappan Lakshmanan41114a22021-07-29 20:22:14 +02001757
1758 ga_clear(&msg_ga);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001759 return 0;
1760}
1761
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001762 static int
Bram Moolenaar38e2b062011-09-21 17:15:39 +02001763luaV_debug(lua_State *L)
1764{
1765 lua_settop(L, 0);
1766 lua_getglobal(L, "vim");
1767 lua_getfield(L, -1, "eval");
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001768 lua_remove(L, -2); // vim.eval at position 1
Bram Moolenaar38e2b062011-09-21 17:15:39 +02001769 for (;;)
1770 {
1771 const char *input;
1772 size_t l;
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001773 lua_pushvalue(L, 1); // vim.eval
Bram Moolenaar38e2b062011-09-21 17:15:39 +02001774 lua_pushliteral(L, "input('lua_debug> ')");
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001775 lua_call(L, 1, 1); // return string
Bram Moolenaar38e2b062011-09-21 17:15:39 +02001776 input = lua_tolstring(L, -1, &l);
1777 if (l == 0 || strcmp(input, "cont") == 0)
1778 return 0;
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001779 msg_putchar('\n'); // avoid outputting on input line
Bram Moolenaar38e2b062011-09-21 17:15:39 +02001780 if (luaL_loadbuffer(L, input, l, "=(debug command)")
1781 || lua_pcall(L, 0, 0, 0))
1782 luaV_emsg(L);
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001783 lua_settop(L, 1); // remove eventual returns, but keep vim.eval
Bram Moolenaar38e2b062011-09-21 17:15:39 +02001784 }
1785}
1786
Yegappan Lakshmanan9dc4bef2021-08-04 21:12:52 +02001787 static dict_T *
1788luaV_get_var_scope(lua_State *L)
1789{
1790 const char *scope = luaL_checkstring(L, 1);
1791 dict_T *dict = NULL;
1792
1793 if (STRICMP((char *)scope, "g") == 0)
1794 dict = get_globvar_dict();
1795 else if (STRICMP((char *)scope, "v") == 0)
1796 dict = get_vimvar_dict();
1797 else if (STRICMP((char *)scope, "b") == 0)
1798 dict = curbuf->b_vars;
1799 else if (STRICMP((char *)scope, "w") == 0)
1800 dict = curwin->w_vars;
1801 else if (STRICMP((char *)scope, "t") == 0)
1802 dict = curtab->tp_vars;
1803 else
1804 {
1805 luaL_error(L, "invalid scope %s", scope);
1806 return NULL;
1807 }
1808
1809 return dict;
1810}
1811
1812 static int
1813luaV_setvar(lua_State *L)
1814{
1815 dict_T *dict;
1816 dictitem_T *di;
1817 size_t len;
1818 char *name;
1819 int del;
1820 char *error = NULL;
1821
1822 name = (char *)luaL_checklstring(L, 3, &len);
1823 del = (lua_gettop(L) < 4) || lua_isnil(L, 4);
1824
1825 dict = luaV_get_var_scope(L);
1826 if (dict == NULL)
1827 return 0;
1828
Yegappan Lakshmanan11328bc2021-08-06 21:34:38 +02001829 di = dict_find(dict, (char_u *)name, (int)len);
Yegappan Lakshmanan9dc4bef2021-08-04 21:12:52 +02001830 if (di != NULL)
1831 {
1832 if (di->di_flags & DI_FLAGS_RO)
1833 error = "variable is read-only";
1834 else if (di->di_flags & DI_FLAGS_LOCK)
1835 error = "variable is locked";
1836 else if (del && di->di_flags & DI_FLAGS_FIX)
1837 error = "variable is fixed";
1838 if (error != NULL)
1839 return luaL_error(L, error);
1840 }
1841 else if (dict->dv_lock)
1842 return luaL_error(L, "Dictionary is locked");
1843
1844 if (del)
1845 {
1846 // Delete the key
1847 if (di == NULL)
1848 // Doesn't exist, nothing to do
1849 return 0;
Bram Moolenaaref2c3252022-11-25 16:31:51 +00001850 // Delete the entry
1851 dictitem_remove(dict, di, "Lua delete variable");
Yegappan Lakshmanan9dc4bef2021-08-04 21:12:52 +02001852 }
1853 else
1854 {
1855 // Update the key
1856 typval_T tv;
1857
h-east965d2ed2021-10-04 21:51:57 +01001858 // Convert the lua value to a Vim script type in the temporary variable
Yegappan Lakshmanan9dc4bef2021-08-04 21:12:52 +02001859 lua_pushvalue(L, 4);
1860 if (luaV_totypval(L, -1, &tv) == FAIL)
1861 return luaL_error(L, "Couldn't convert lua value");
1862
1863 if (di == NULL)
1864 {
1865 // Need to create an entry
1866 di = dictitem_alloc((char_u *)name);
1867 if (di == NULL)
Bram Moolenaar1b6acf02021-08-05 16:47:08 +02001868 {
1869 clear_tv(&tv);
Yegappan Lakshmanan9dc4bef2021-08-04 21:12:52 +02001870 return 0;
Bram Moolenaar1b6acf02021-08-05 16:47:08 +02001871 }
Yegappan Lakshmanan9dc4bef2021-08-04 21:12:52 +02001872 // Update the value
1873 copy_tv(&tv, &di->di_tv);
Bram Moolenaar4a011592021-08-05 15:11:08 +02001874 if (dict_add(dict, di) == FAIL)
Bram Moolenaar1b6acf02021-08-05 16:47:08 +02001875 {
1876 dictitem_free(di);
1877 clear_tv(&tv);
Bram Moolenaar4a011592021-08-05 15:11:08 +02001878 return luaL_error(L, "Couldn't add to dictionary");
Bram Moolenaar1b6acf02021-08-05 16:47:08 +02001879 }
1880 }
1881 else
Yegappan Lakshmanan9dc4bef2021-08-04 21:12:52 +02001882 {
1883 // Clear the old value
1884 clear_tv(&di->di_tv);
1885 // Update the value
1886 copy_tv(&tv, &di->di_tv);
1887 }
1888
1889 // Clear the temporary variable
1890 clear_tv(&tv);
1891 }
1892
1893 return 0;
1894}
1895
1896 static int
1897luaV_getvar(lua_State *L)
1898{
1899 dict_T *dict = luaV_get_var_scope(L);
1900 size_t len;
1901 const char *name = luaL_checklstring(L, 3, &len);
Yegappan Lakshmanan11328bc2021-08-06 21:34:38 +02001902 dictitem_T *di = dict_find(dict, (char_u *)name, (int)len);
Yegappan Lakshmanan9dc4bef2021-08-04 21:12:52 +02001903
Yegappan Lakshmanan9dc4bef2021-08-04 21:12:52 +02001904 if (di == NULL)
1905 return 0; // nil
1906
1907 luaV_pushtypval(L, &di->di_tv);
1908 return 1;
1909}
1910
Bram Moolenaar38e2b062011-09-21 17:15:39 +02001911 static int
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001912luaV_command(lua_State *L)
1913{
Yegappan Lakshmanan11328bc2021-08-06 21:34:38 +02001914 char_u *s = vim_strsave((char_u *)luaL_checkstring(L, 1));
1915
1916 execute_cmds_from_string(s);
1917 vim_free(s);
Bram Moolenaara4d158b2022-08-14 14:17:45 +01001918 update_screen(UPD_VALID);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001919 return 0;
1920}
1921
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001922 static int
1923luaV_eval(lua_State *L)
1924{
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001925 typval_T *tv = eval_expr((char_u *) luaL_checkstring(L, 1), NULL);
Yegappan Lakshmanan11328bc2021-08-06 21:34:38 +02001926
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001927 if (tv == NULL) luaL_error(L, "invalid expression");
1928 luaV_pushtypval(L, tv);
Bram Moolenaarb3766472013-04-15 13:49:21 +02001929 free_tv(tv);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001930 return 1;
1931}
1932
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001933 static int
Bram Moolenaar0d2e4fc2010-07-18 12:35:47 +02001934luaV_beep(lua_State *L UNUSED)
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001935{
Bram Moolenaar165bc692015-07-21 17:53:25 +02001936 vim_beep(BO_LANG);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001937 return 0;
1938}
1939
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001940 static int
1941luaV_line(lua_State *L)
1942{
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001943 luaV_pushline(L, curbuf, curwin->w_cursor.lnum);
1944 return 1;
1945}
1946
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001947 static int
Bram Moolenaar1dced572012-04-05 16:54:08 +02001948luaV_list(lua_State *L)
1949{
Bram Moolenaarca06da92018-07-01 15:12:05 +02001950 list_T *l;
1951 int initarg = !lua_isnoneornil(L, 1);
1952
1953 if (initarg && lua_type(L, 1) != LUA_TTABLE)
1954 luaL_error(L, "table expected, got %s", luaL_typename(L, 1));
Yegappan Lakshmanan0233bdf2023-01-12 12:33:30 +00001955
Bram Moolenaarca06da92018-07-01 15:12:05 +02001956 l = list_alloc();
Bram Moolenaar1dced572012-04-05 16:54:08 +02001957 if (l == NULL)
Bram Moolenaarca06da92018-07-01 15:12:05 +02001958 {
Yegappan Lakshmanan0233bdf2023-01-12 12:33:30 +00001959 lua_pushnil(L);
1960 return 1;
Bram Moolenaarca06da92018-07-01 15:12:05 +02001961 }
Yegappan Lakshmanan0233bdf2023-01-12 12:33:30 +00001962
1963 luaV_newlist(L, l);
1964 if (!initarg)
1965 return 1;
1966
1967 // traverse table to init list
1968 int notnil, i = 0;
1969 typval_T v;
1970 do
1971 {
1972 lua_rawgeti(L, 1, ++i);
1973 notnil = !lua_isnil(L, -1);
1974 if (notnil)
1975 {
1976 luaV_checktypval(L, -1, &v, "vim.list");
1977 list_append_tv(l, &v);
1978 clear_tv(&v);
1979 }
1980 lua_pop(L, 1); // value
1981 } while (notnil);
1982
Bram Moolenaar1dced572012-04-05 16:54:08 +02001983 return 1;
1984}
1985
1986 static int
1987luaV_dict(lua_State *L)
1988{
Bram Moolenaarca06da92018-07-01 15:12:05 +02001989 dict_T *d;
1990 int initarg = !lua_isnoneornil(L, 1);
1991
1992 if (initarg && lua_type(L, 1) != LUA_TTABLE)
1993 luaL_error(L, "table expected, got %s", luaL_typename(L, 1));
Yegappan Lakshmanan0233bdf2023-01-12 12:33:30 +00001994
Bram Moolenaarca06da92018-07-01 15:12:05 +02001995 d = dict_alloc();
Bram Moolenaar1dced572012-04-05 16:54:08 +02001996 if (d == NULL)
Bram Moolenaarca06da92018-07-01 15:12:05 +02001997 {
Yegappan Lakshmanan0233bdf2023-01-12 12:33:30 +00001998 lua_pushnil(L);
1999 return 1;
2000 }
2001
2002 luaV_newdict(L, d);
2003 if (!initarg)
2004 return 1;
2005
2006 // traverse table to init dict
2007 lua_pushnil(L);
2008 while (lua_next(L, 1))
2009 {
2010 char_u *key;
2011 dictitem_T *di;
2012 typval_T v;
2013
2014 lua_pushvalue(L, -2); // dup key in case it's a number
2015 key = (char_u *) lua_tostring(L, -1);
2016 if (key == NULL)
Bram Moolenaarca06da92018-07-01 15:12:05 +02002017 {
2018 lua_pushnil(L);
Yegappan Lakshmanan0233bdf2023-01-12 12:33:30 +00002019 return 1;
Bram Moolenaarca06da92018-07-01 15:12:05 +02002020 }
Yegappan Lakshmanan0233bdf2023-01-12 12:33:30 +00002021 if (*key == NUL)
2022 luaL_error(L, "table has empty key");
2023 luaV_checktypval(L, -2, &v, "vim.dict"); // value
2024 di = dictitem_alloc(key);
2025 if (di == NULL || dict_add(d, di) == FAIL)
2026 {
2027 vim_free(di);
2028 lua_pushnil(L);
2029 return 1;
2030 }
2031 di->di_tv = v;
2032 lua_pop(L, 2); // key copy and value
Bram Moolenaarca06da92018-07-01 15:12:05 +02002033 }
Yegappan Lakshmanan0233bdf2023-01-12 12:33:30 +00002034
Bram Moolenaarca06da92018-07-01 15:12:05 +02002035 return 1;
2036}
2037
2038 static int
Bram Moolenaarb7828692019-03-23 13:57:02 +01002039luaV_blob(lua_State *L)
2040{
2041 blob_T *b;
2042 int initarg = !lua_isnoneornil(L, 1);
2043
2044 if (initarg && !lua_isstring(L, 1))
2045 luaL_error(L, "string expected, got %s", luaL_typename(L, 1));
Yegappan Lakshmanan0233bdf2023-01-12 12:33:30 +00002046
Bram Moolenaarb7828692019-03-23 13:57:02 +01002047 b = blob_alloc();
2048 if (b == NULL)
Bram Moolenaarb7828692019-03-23 13:57:02 +01002049 {
Yegappan Lakshmanan0233bdf2023-01-12 12:33:30 +00002050 lua_pushnil(L);
2051 return 1;
Bram Moolenaarb7828692019-03-23 13:57:02 +01002052 }
Yegappan Lakshmanan0233bdf2023-01-12 12:33:30 +00002053
2054 luaV_newblob(L, b);
2055 if (!initarg)
2056 return 1;
2057
2058 // traverse table to init blob
2059 size_t i, l = 0;
2060 const char *s = lua_tolstring(L, 1, &l);
2061
2062 if (ga_grow(&b->bv_ga, (int)l) == OK)
2063 for (i = 0; i < l; ++i)
2064 ga_append(&b->bv_ga, s[i]);
2065
Bram Moolenaarb7828692019-03-23 13:57:02 +01002066 return 1;
2067}
2068
2069 static int
Bram Moolenaarca06da92018-07-01 15:12:05 +02002070luaV_funcref(lua_State *L)
2071{
2072 const char *name = luaL_checkstring(L, 1);
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002073 // note: not checking if function exists (needs function_exists)
Bram Moolenaarca06da92018-07-01 15:12:05 +02002074 if (name == NULL || *name == NUL || VIM_ISDIGIT(*name))
2075 luaL_error(L, "invalid function name: %s", name);
2076 luaV_newfuncref(L, (char_u *) name);
Bram Moolenaar1dced572012-04-05 16:54:08 +02002077 return 1;
2078}
2079
2080 static int
Bram Moolenaar55d5c032010-07-17 23:52:29 +02002081luaV_buffer(lua_State *L)
2082{
Bram Moolenaar0ba04292010-07-14 23:23:17 +02002083 buf_T *buf;
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002084 if (lua_isstring(L, 1)) // get by number or name?
Bram Moolenaar55d5c032010-07-17 23:52:29 +02002085 {
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002086 if (lua_isnumber(L, 1)) // by number?
Bram Moolenaar55d5c032010-07-17 23:52:29 +02002087 {
2088 int n = lua_tointeger(L, 1);
Bram Moolenaar29323592016-07-24 22:04:11 +02002089 FOR_ALL_BUFFERS(buf)
Bram Moolenaar55d5c032010-07-17 23:52:29 +02002090 if (buf->b_fnum == n) break;
2091 }
Bram Moolenaarca06da92018-07-01 15:12:05 +02002092 else // by name
2093 {
Bram Moolenaar55d5c032010-07-17 23:52:29 +02002094 size_t l;
2095 const char *s = lua_tolstring(L, 1, &l);
Bram Moolenaar29323592016-07-24 22:04:11 +02002096 FOR_ALL_BUFFERS(buf)
Bram Moolenaar55d5c032010-07-17 23:52:29 +02002097 {
2098 if (buf->b_ffname == NULL || buf->b_sfname == NULL)
2099 {
2100 if (l == 0) break;
2101 }
Bram Moolenaar0d2e4fc2010-07-18 12:35:47 +02002102 else if (strncmp(s, (char *)buf->b_ffname, l) == 0
2103 || strncmp(s, (char *)buf->b_sfname, l) == 0)
Bram Moolenaar55d5c032010-07-17 23:52:29 +02002104 break;
2105 }
2106 }
Bram Moolenaar0ba04292010-07-14 23:23:17 +02002107 }
Bram Moolenaar1dced572012-04-05 16:54:08 +02002108 else
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002109 buf = (lua_toboolean(L, 1)) ? firstbuf : curbuf; // first buffer?
Bram Moolenaar1dced572012-04-05 16:54:08 +02002110 luaV_pushbuffer(L, buf);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02002111 return 1;
2112}
2113
Bram Moolenaar55d5c032010-07-17 23:52:29 +02002114 static int
2115luaV_window(lua_State *L)
2116{
Bram Moolenaar0ba04292010-07-14 23:23:17 +02002117 win_T *win;
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002118 if (lua_isnumber(L, 1)) // get by number?
Bram Moolenaar55d5c032010-07-17 23:52:29 +02002119 {
2120 int n = lua_tointeger(L, 1);
2121 for (win = firstwin; win != NULL; win = win->w_next, n--)
2122 if (n == 1) break;
Bram Moolenaar0ba04292010-07-14 23:23:17 +02002123 }
Bram Moolenaar1dced572012-04-05 16:54:08 +02002124 else
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002125 win = (lua_toboolean(L, 1)) ? firstwin : curwin; // first window?
Bram Moolenaar1dced572012-04-05 16:54:08 +02002126 luaV_pushwindow(L, win);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02002127 return 1;
2128}
2129
Bram Moolenaar55d5c032010-07-17 23:52:29 +02002130 static int
2131luaV_open(lua_State *L)
2132{
Bram Moolenaar0ba04292010-07-14 23:23:17 +02002133 char_u *s = NULL;
2134#ifdef HAVE_SANDBOX
2135 luaV_checksandbox(L);
2136#endif
2137 if (lua_isstring(L, 1)) s = (char_u *) lua_tostring(L, 1);
Bram Moolenaarfa263a52011-12-08 16:00:16 +01002138 luaV_pushbuffer(L, buflist_new(s, NULL, 1L, BLN_LISTED));
Bram Moolenaar0ba04292010-07-14 23:23:17 +02002139 return 1;
2140}
2141
Bram Moolenaar55d5c032010-07-17 23:52:29 +02002142 static int
Bram Moolenaar1dced572012-04-05 16:54:08 +02002143luaV_type(lua_State *L)
Bram Moolenaar55d5c032010-07-17 23:52:29 +02002144{
Bram Moolenaar1dced572012-04-05 16:54:08 +02002145 luaL_checkany(L, 1);
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002146 if (lua_type(L, 1) == LUA_TUSERDATA) // check vim udata?
Bram Moolenaar55d5c032010-07-17 23:52:29 +02002147 {
Bram Moolenaar1dced572012-04-05 16:54:08 +02002148 lua_settop(L, 1);
2149 if (lua_getmetatable(L, 1))
2150 {
2151 luaV_getfield(L, LUAVIM_LIST);
2152 if (lua_rawequal(L, -1, 2))
2153 {
2154 lua_pushstring(L, "list");
2155 return 1;
2156 }
2157 luaV_getfield(L, LUAVIM_DICT);
2158 if (lua_rawequal(L, -1, 2))
2159 {
2160 lua_pushstring(L, "dict");
2161 return 1;
2162 }
Bram Moolenaarb7828692019-03-23 13:57:02 +01002163 luaV_getfield(L, LUAVIM_BLOB);
2164 if (lua_rawequal(L, -1, 2))
2165 {
2166 lua_pushstring(L, "blob");
2167 return 1;
2168 }
Bram Moolenaarca06da92018-07-01 15:12:05 +02002169 luaV_getfield(L, LUAVIM_FUNCREF);
2170 if (lua_rawequal(L, -1, 2))
2171 {
2172 lua_pushstring(L, "funcref");
2173 return 1;
2174 }
Bram Moolenaar1dced572012-04-05 16:54:08 +02002175 luaV_getfield(L, LUAVIM_BUFFER);
2176 if (lua_rawequal(L, -1, 2))
2177 {
2178 lua_pushstring(L, "buffer");
2179 return 1;
2180 }
2181 luaV_getfield(L, LUAVIM_WINDOW);
2182 if (lua_rawequal(L, -1, 2))
2183 {
2184 lua_pushstring(L, "window");
2185 return 1;
2186 }
2187 }
Bram Moolenaar0ba04292010-07-14 23:23:17 +02002188 }
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002189 lua_pushstring(L, luaL_typename(L, 1)); // fallback
Bram Moolenaar1dced572012-04-05 16:54:08 +02002190 return 1;
Bram Moolenaar0ba04292010-07-14 23:23:17 +02002191}
2192
Bram Moolenaareb04f082020-05-17 14:32:35 +02002193 static int
2194luaV_call(lua_State *L)
2195{
2196 int argc = lua_gettop(L) - 1;
2197 size_t funcname_len;
2198 char_u *funcname;
2199 char *error = NULL;
2200 typval_T rettv;
2201 typval_T argv[MAX_FUNC_ARGS + 1];
2202 int i = 0;
2203
2204 if (argc > MAX_FUNC_ARGS)
2205 return luaL_error(L, "Function called with too many arguments");
2206
2207 funcname = (char_u *)luaL_checklstring(L, 1, &funcname_len);
2208
2209 for (; i < argc; i++)
2210 {
2211 if (luaV_totypval(L, i + 2, &argv[i]) == FAIL)
2212 {
2213 error = "lua: cannot convert value";
2214 goto free_vim_args;
2215 }
2216 }
2217
2218 argv[argc].v_type = VAR_UNKNOWN;
2219
2220 if (call_vim_function(funcname, argc, argv, &rettv) == FAIL)
2221 {
2222 error = "lua: call_vim_function failed";
2223 goto free_vim_args;
2224 }
2225
2226 luaV_pushtypval(L, &rettv);
2227 clear_tv(&rettv);
2228
2229free_vim_args:
2230 while (i > 0)
2231 clear_tv(&argv[--i]);
2232
2233 if (error == NULL)
2234 return 1;
2235 else
2236 return luaL_error(L, error);
2237}
2238
Yegappan Lakshmanan11328bc2021-08-06 21:34:38 +02002239/*
2240 * Return the Vim version as a Lua table
2241 */
2242 static int
2243luaV_version(lua_State *L)
2244{
2245 lua_newtable(L);
2246 lua_pushstring(L, "major");
2247 lua_pushinteger(L, VIM_VERSION_MAJOR);
2248 lua_settable(L, -3);
2249 lua_pushstring(L, "minor");
2250 lua_pushinteger(L, VIM_VERSION_MINOR);
2251 lua_settable(L, -3);
2252 lua_pushstring(L, "patch");
2253 lua_pushinteger(L, highest_patch());
2254 lua_settable(L, -3);
2255 return 1;
2256}
2257
Bram Moolenaar0ba04292010-07-14 23:23:17 +02002258static const luaL_Reg luaV_module[] = {
2259 {"command", luaV_command},
2260 {"eval", luaV_eval},
2261 {"beep", luaV_beep},
2262 {"line", luaV_line},
Bram Moolenaar1dced572012-04-05 16:54:08 +02002263 {"list", luaV_list},
2264 {"dict", luaV_dict},
Bram Moolenaarb7828692019-03-23 13:57:02 +01002265 {"blob", luaV_blob},
Bram Moolenaarca06da92018-07-01 15:12:05 +02002266 {"funcref", luaV_funcref},
Bram Moolenaar0ba04292010-07-14 23:23:17 +02002267 {"buffer", luaV_buffer},
2268 {"window", luaV_window},
2269 {"open", luaV_open},
Bram Moolenaar1dced572012-04-05 16:54:08 +02002270 {"type", luaV_type},
Bram Moolenaareb04f082020-05-17 14:32:35 +02002271 {"call", luaV_call},
Yegappan Lakshmanan9dc4bef2021-08-04 21:12:52 +02002272 {"_getvar", luaV_getvar},
2273 {"_setvar", luaV_setvar},
Yegappan Lakshmanan11328bc2021-08-06 21:34:38 +02002274 {"version", luaV_version},
Bram Moolenaar125ed272021-04-07 20:11:12 +02002275 {"lua_version", NULL},
Bram Moolenaar0ba04292010-07-14 23:23:17 +02002276 {NULL, NULL}
2277};
2278
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002279/*
2280 * for freeing list, dict, buffer and window objects; lightuserdata as arg
2281 */
Bram Moolenaar1dced572012-04-05 16:54:08 +02002282 static int
2283luaV_free(lua_State *L)
2284{
2285 lua_pushnil(L);
2286 luaV_setudata(L, lua_touserdata(L, 1));
2287 return 0;
2288}
2289
2290 static int
Bram Moolenaar4eefe472019-03-19 21:59:19 +01002291luaV_luaeval(lua_State *L)
Bram Moolenaar1dced572012-04-05 16:54:08 +02002292{
2293 luaL_Buffer b;
2294 size_t l;
2295 const char *str = lua_tolstring(L, 1, &l);
2296 typval_T *arg = (typval_T *) lua_touserdata(L, 2);
2297 typval_T *rettv = (typval_T *) lua_touserdata(L, 3);
2298 luaL_buffinit(L, &b);
2299 luaL_addlstring(&b, LUAVIM_EVALHEADER, sizeof(LUAVIM_EVALHEADER) - 1);
2300 luaL_addlstring(&b, str, l);
2301 luaL_pushresult(&b);
2302 str = lua_tolstring(L, -1, &l);
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002303 if (luaL_loadbuffer(L, str, l, LUAVIM_EVALNAME)) // compile error?
Bram Moolenaar1dced572012-04-05 16:54:08 +02002304 {
2305 luaV_emsg(L);
2306 return 0;
2307 }
2308 luaV_pushtypval(L, arg);
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002309 if (lua_pcall(L, 1, 1, 0)) // running error?
Bram Moolenaar1dced572012-04-05 16:54:08 +02002310 {
2311 luaV_emsg(L);
2312 return 0;
2313 }
Bram Moolenaarca06da92018-07-01 15:12:05 +02002314 if (luaV_totypval(L, -1, rettv) == FAIL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002315 emsg("luaeval: cannot convert value");
Bram Moolenaarf554a322015-02-04 23:08:01 +01002316 return 0;
Bram Moolenaar1dced572012-04-05 16:54:08 +02002317}
2318
2319 static int
Bram Moolenaar4eefe472019-03-19 21:59:19 +01002320luaV_setref(lua_State *L)
Bram Moolenaar1dced572012-04-05 16:54:08 +02002321{
Bram Moolenaar4eefe472019-03-19 21:59:19 +01002322 int copyID = lua_tointeger(L, 1);
2323 int abort = FALSE;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01002324
Bram Moolenaar1dced572012-04-05 16:54:08 +02002325 luaV_getfield(L, LUAVIM_LIST);
2326 luaV_getfield(L, LUAVIM_DICT);
Bram Moolenaar4eefe472019-03-19 21:59:19 +01002327 luaV_getfield(L, LUAVIM_FUNCREF);
Bram Moolenaar1dced572012-04-05 16:54:08 +02002328 lua_pushnil(L);
Bram Moolenaar4eefe472019-03-19 21:59:19 +01002329 // traverse cache table
Bram Moolenaarb84634d2015-02-04 22:02:37 +01002330 while (!abort && lua_next(L, lua_upvalueindex(1)) != 0)
Bram Moolenaar1dced572012-04-05 16:54:08 +02002331 {
2332 lua_getmetatable(L, -1);
Bram Moolenaar4eefe472019-03-19 21:59:19 +01002333 if (lua_rawequal(L, -1, 2)) // list?
Bram Moolenaar1dced572012-04-05 16:54:08 +02002334 {
Bram Moolenaar4eefe472019-03-19 21:59:19 +01002335 list_T *l = (list_T *)lua_touserdata(L, 5); // key
2336
Bram Moolenaar7be3ab22019-06-23 01:46:15 +02002337 abort = set_ref_in_list(l, copyID);
Bram Moolenaar1dced572012-04-05 16:54:08 +02002338 }
Bram Moolenaar4eefe472019-03-19 21:59:19 +01002339 else if (lua_rawequal(L, -1, 3)) // dict?
Bram Moolenaar1dced572012-04-05 16:54:08 +02002340 {
Bram Moolenaar4eefe472019-03-19 21:59:19 +01002341 dict_T *d = (dict_T *)lua_touserdata(L, 5); // key
2342
Bram Moolenaar7be3ab22019-06-23 01:46:15 +02002343 abort = set_ref_in_dict(d, copyID);
Bram Moolenaar1dced572012-04-05 16:54:08 +02002344 }
Bram Moolenaar4eefe472019-03-19 21:59:19 +01002345 else if (lua_rawequal(L, -1, 4)) // funcref?
2346 {
2347 luaV_Funcref *f = (luaV_Funcref *)lua_touserdata(L, 5); // key
2348
Bram Moolenaar7be3ab22019-06-23 01:46:15 +02002349 abort = set_ref_in_dict(f->self, copyID);
Bram Moolenaar4eefe472019-03-19 21:59:19 +01002350 }
2351 lua_pop(L, 2); // metatable and value
Bram Moolenaar1dced572012-04-05 16:54:08 +02002352 }
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01002353 lua_pushinteger(L, abort);
Bram Moolenaarf554a322015-02-04 23:08:01 +01002354 return 1;
Bram Moolenaar1dced572012-04-05 16:54:08 +02002355}
2356
Bram Moolenaar125ed272021-04-07 20:11:12 +02002357 static int
2358luaV_pushversion(lua_State *L)
2359{
2360 int major = 0;
2361 int minor = 0;
2362 int patch = 0;
2363 char s[16];
2364
2365 sscanf(LUAVIM_VERSION, "Lua %d.%d.%d", &major, &minor, &patch);
2366 vim_snprintf(s, sizeof(s), "%d.%d.%d", major, minor, patch);
2367 lua_pushstring(L, s);
2368 return 0;
2369}
2370
Bram Moolenaareb04f082020-05-17 14:32:35 +02002371#define LUA_VIM_FN_CODE \
Bram Moolenaar788fbb42020-05-31 14:08:12 +02002372 "vim.fn = setmetatable({}, {\n"\
2373 " __index = function (t, key)\n"\
2374 " local function _fn(...)\n"\
2375 " return vim.call(key, ...)\n"\
2376 " end\n"\
2377 " t[key] = _fn\n"\
2378 " return _fn\n"\
2379 " end\n"\
2380 " })"
2381
2382#define LUA_VIM_UPDATE_PACKAGE_PATHS \
2383 "local last_vim_paths = {}\n"\
2384 "vim._update_package_paths = function ()\n"\
2385 " local cur_vim_paths = {}\n"\
2386 " local function split(s, delimiter)\n"\
2387 " result = {}\n"\
2388 " for match in (s..delimiter):gmatch(\"(.-)\"..delimiter) do\n"\
2389 " table.insert(result, match)\n"\
2390 " end\n"\
2391 " return result\n"\
2392 " end\n"\
2393 " local rtps = split(vim.eval('&runtimepath'), ',')\n"\
2394 " local sep = package.config:sub(1, 1)\n"\
2395 " for _, key in ipairs({'path', 'cpath'}) do\n"\
2396 " local orig_str = package[key] .. ';'\n"\
2397 " local pathtrails_ordered = {}\n"\
2398 " -- Note: ignores trailing item without trailing `;`. Not using something\n"\
2399 " -- simpler in order to preserve empty items (stand for default path).\n"\
2400 " local orig = {}\n"\
2401 " for s in orig_str:gmatch('[^;]*;') do\n"\
2402 " s = s:sub(1, -2) -- Strip trailing semicolon\n"\
2403 " orig[#orig + 1] = s\n"\
2404 " end\n"\
2405 " if key == 'path' then\n"\
2406 " -- /?.lua and /?/init.lua\n"\
2407 " pathtrails_ordered = {sep .. '?.lua', sep .. '?' .. sep .. 'init.lua'}\n"\
2408 " else\n"\
2409 " local pathtrails = {}\n"\
2410 " for _, s in ipairs(orig) do\n"\
2411 " -- Find out path patterns. pathtrail should contain something like\n"\
2412 " -- /?.so, \?.dll. This allows not to bother determining what correct\n"\
2413 " -- suffixes are.\n"\
2414 " local pathtrail = s:match('[/\\\\][^/\\\\]*%?.*$')\n"\
2415 " if pathtrail and not pathtrails[pathtrail] then\n"\
2416 " pathtrails[pathtrail] = true\n"\
2417 " pathtrails_ordered[#pathtrails_ordered + 1] = pathtrail\n"\
2418 " end\n"\
2419 " end\n"\
2420 " end\n"\
2421 " local new = {}\n"\
2422 " for _, rtp in ipairs(rtps) do\n"\
2423 " if not rtp:match(';') then\n"\
2424 " for _, pathtrail in pairs(pathtrails_ordered) do\n"\
2425 " local new_path = rtp .. sep .. 'lua' .. pathtrail\n"\
2426 " -- Always keep paths from &runtimepath at the start:\n"\
2427 " -- append them here disregarding orig possibly containing one of them.\n"\
2428 " new[#new + 1] = new_path\n"\
2429 " cur_vim_paths[new_path] = true\n"\
2430 " end\n"\
2431 " end\n"\
2432 " end\n"\
2433 " for _, orig_path in ipairs(orig) do\n"\
2434 " -- Handle removing obsolete paths originating from &runtimepath: such\n"\
2435 " -- paths either belong to cur_nvim_paths and were already added above or\n"\
2436 " -- to last_nvim_paths and should not be added at all if corresponding\n"\
2437 " -- entry was removed from &runtimepath list.\n"\
2438 " if not (cur_vim_paths[orig_path] or last_vim_paths[orig_path]) then\n"\
2439 " new[#new + 1] = orig_path\n"\
2440 " end\n"\
2441 " end\n"\
2442 " package[key] = table.concat(new, ';')\n"\
2443 " end\n"\
2444 " last_vim_paths = cur_vim_paths\n"\
2445 "end"
Bram Moolenaareb04f082020-05-17 14:32:35 +02002446
Yegappan Lakshmanan9dc4bef2021-08-04 21:12:52 +02002447#define LUA_VIM_SETUP_VARIABLE_DICTS \
2448 "do\n"\
2449 " local function make_dict_accessor(scope)\n"\
2450 " local mt = {}\n"\
2451 " function mt:__newindex(k, v)\n"\
2452 " return vim._setvar(scope, 0, k, v)\n"\
2453 " end\n"\
2454 " function mt:__index(k)\n"\
2455 " return vim._getvar(scope, 0, k)\n"\
2456 " end\n"\
2457 " return setmetatable({}, mt)\n"\
2458 " end\n"\
2459 " vim.g = make_dict_accessor('g')\n"\
2460 " vim.v = make_dict_accessor('v')\n"\
2461 " vim.b = make_dict_accessor('b')\n"\
2462 " vim.w = make_dict_accessor('w')\n"\
2463 " vim.t = make_dict_accessor('t')\n"\
2464 "end"
2465
Bram Moolenaar55d5c032010-07-17 23:52:29 +02002466 static int
2467luaopen_vim(lua_State *L)
2468{
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002469 // set cache table
Bram Moolenaar0ba04292010-07-14 23:23:17 +02002470 lua_newtable(L);
2471 lua_newtable(L);
Bram Moolenaar1dced572012-04-05 16:54:08 +02002472 lua_pushstring(L, "v");
Bram Moolenaar0ba04292010-07-14 23:23:17 +02002473 lua_setfield(L, -2, "__mode");
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002474 lua_setmetatable(L, -2); // cache is weak-valued
2475 // print
Bram Moolenaar0ba04292010-07-14 23:23:17 +02002476 lua_pushcfunction(L, luaV_print);
2477 lua_setglobal(L, "print");
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002478 // debug.debug
Bram Moolenaar38e2b062011-09-21 17:15:39 +02002479 lua_getglobal(L, "debug");
2480 lua_pushcfunction(L, luaV_debug);
2481 lua_setfield(L, -2, "debug");
2482 lua_pop(L, 1);
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002483 // free
Bram Moolenaar0ba04292010-07-14 23:23:17 +02002484 lua_pushlightuserdata(L, (void *) LUAVIM_FREE);
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002485 lua_pushvalue(L, 1); // cache table
Bram Moolenaar1dced572012-04-05 16:54:08 +02002486 lua_pushcclosure(L, luaV_free, 1);
2487 lua_rawset(L, LUA_REGISTRYINDEX);
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002488 // luaeval
Bram Moolenaar1dced572012-04-05 16:54:08 +02002489 lua_pushlightuserdata(L, (void *) LUAVIM_LUAEVAL);
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002490 lua_pushvalue(L, 1); // cache table
Bram Moolenaar1dced572012-04-05 16:54:08 +02002491 lua_pushcclosure(L, luaV_luaeval, 1);
2492 lua_rawset(L, LUA_REGISTRYINDEX);
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002493 // setref
Bram Moolenaar1dced572012-04-05 16:54:08 +02002494 lua_pushlightuserdata(L, (void *) LUAVIM_SETREF);
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002495 lua_pushvalue(L, 1); // cache table
Bram Moolenaar1dced572012-04-05 16:54:08 +02002496 lua_pushcclosure(L, luaV_setref, 1);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02002497 lua_rawset(L, LUA_REGISTRYINDEX);
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002498 // register
Bram Moolenaar1dced572012-04-05 16:54:08 +02002499 luaV_newmetatable(L, LUAVIM_LIST);
2500 lua_pushvalue(L, 1);
2501 luaV_openlib(L, luaV_List_mt, 1);
2502 luaV_newmetatable(L, LUAVIM_DICT);
2503 lua_pushvalue(L, 1);
2504 luaV_openlib(L, luaV_Dict_mt, 1);
Bram Moolenaarb7828692019-03-23 13:57:02 +01002505 luaV_newmetatable(L, LUAVIM_BLOB);
2506 lua_pushvalue(L, 1);
2507 luaV_openlib(L, luaV_Blob_mt, 1);
Bram Moolenaarca06da92018-07-01 15:12:05 +02002508 luaV_newmetatable(L, LUAVIM_FUNCREF);
2509 lua_pushvalue(L, 1);
2510 luaV_openlib(L, luaV_Funcref_mt, 1);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02002511 luaV_newmetatable(L, LUAVIM_BUFFER);
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_Buffer_mt, 1);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02002514 luaV_newmetatable(L, LUAVIM_WINDOW);
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002515 lua_pushvalue(L, 1); // cache table
Bram Moolenaar1dced572012-04-05 16:54:08 +02002516 luaV_openlib(L, luaV_Window_mt, 1);
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002517 lua_newtable(L); // vim table
2518 lua_pushvalue(L, 1); // cache table
Bram Moolenaar1dced572012-04-05 16:54:08 +02002519 luaV_openlib(L, luaV_module, 1);
Bram Moolenaar125ed272021-04-07 20:11:12 +02002520 luaV_pushversion(L);
2521 lua_setfield(L, -2, "lua_version");
Bram Moolenaar1dced572012-04-05 16:54:08 +02002522 lua_setglobal(L, LUAVIM_NAME);
Bram Moolenaareb04f082020-05-17 14:32:35 +02002523 // custom code
Bram Moolenaar9309eb22020-05-17 16:53:56 +02002524 (void)luaL_dostring(L, LUA_VIM_FN_CODE);
Bram Moolenaar788fbb42020-05-31 14:08:12 +02002525 (void)luaL_dostring(L, LUA_VIM_UPDATE_PACKAGE_PATHS);
Yegappan Lakshmanan9dc4bef2021-08-04 21:12:52 +02002526 (void)luaL_dostring(L, LUA_VIM_SETUP_VARIABLE_DICTS);
Bram Moolenaar788fbb42020-05-31 14:08:12 +02002527
2528 lua_getglobal(L, "vim");
2529 lua_getfield(L, -1, "_update_package_paths");
2530
2531 if (lua_pcall(L, 0, 0, 0))
2532 luaV_emsg(L);
2533
Bram Moolenaar0ba04292010-07-14 23:23:17 +02002534 return 0;
2535}
2536
Bram Moolenaar55d5c032010-07-17 23:52:29 +02002537 static lua_State *
2538luaV_newstate(void)
2539{
Bram Moolenaar0ba04292010-07-14 23:23:17 +02002540 lua_State *L = luaL_newstate();
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002541 luaL_openlibs(L); // core libs
2542 lua_pushcfunction(L, luaopen_vim); // vim
Bram Moolenaar0ba04292010-07-14 23:23:17 +02002543 lua_call(L, 0, 0);
2544 return L;
2545}
2546
Bram Moolenaar55d5c032010-07-17 23:52:29 +02002547 static void
2548luaV_setrange(lua_State *L, int line1, int line2)
2549{
Bram Moolenaar0ba04292010-07-14 23:23:17 +02002550 lua_getglobal(L, LUAVIM_NAME);
2551 lua_pushinteger(L, line1);
2552 lua_setfield(L, -2, "firstline");
2553 lua_pushinteger(L, line2);
2554 lua_setfield(L, -2, "lastline");
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002555 lua_pop(L, 1); // vim table
Bram Moolenaar0ba04292010-07-14 23:23:17 +02002556}
2557
2558
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002559// ======= Interface =======
Bram Moolenaar0ba04292010-07-14 23:23:17 +02002560
2561static lua_State *L = NULL;
2562
Bram Moolenaar55d5c032010-07-17 23:52:29 +02002563 static int
Bram Moolenaar1dced572012-04-05 16:54:08 +02002564lua_isopen(void)
Bram Moolenaar2bd6a1b2010-08-12 22:14:01 +02002565{
2566 return L != NULL;
2567}
2568
2569 static int
Bram Moolenaar55d5c032010-07-17 23:52:29 +02002570lua_init(void)
2571{
Yegappan Lakshmanan0233bdf2023-01-12 12:33:30 +00002572 if (lua_isopen())
2573 return OK;
2574
Bram Moolenaar0ba04292010-07-14 23:23:17 +02002575#ifdef DYNAMIC_LUA
Yegappan Lakshmanan0233bdf2023-01-12 12:33:30 +00002576 if (!lua_enabled(TRUE))
2577 {
2578 emsg(_("Lua library cannot be loaded."));
2579 return FAIL;
Bram Moolenaar0ba04292010-07-14 23:23:17 +02002580 }
Yegappan Lakshmanan0233bdf2023-01-12 12:33:30 +00002581#endif
2582 L = luaV_newstate();
2583
Bram Moolenaar0ba04292010-07-14 23:23:17 +02002584 return OK;
2585}
2586
Bram Moolenaar55d5c032010-07-17 23:52:29 +02002587 void
2588lua_end(void)
2589{
Yegappan Lakshmanan0233bdf2023-01-12 12:33:30 +00002590 if (!lua_isopen())
2591 return;
2592
2593 lua_close(L);
2594 L = NULL;
Bram Moolenaar0ba04292010-07-14 23:23:17 +02002595}
2596
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002597/*
2598 * ex commands
2599 */
Bram Moolenaar55d5c032010-07-17 23:52:29 +02002600 void
2601ex_lua(exarg_T *eap)
2602{
Bram Moolenaarc8970b92020-10-26 20:18:08 +01002603 char *script = (char *)script_get(eap, eap->arg);
2604
2605 if (!eap->skip && lua_init() == OK)
Bram Moolenaar55d5c032010-07-17 23:52:29 +02002606 {
Bram Moolenaarc8970b92020-10-26 20:18:08 +01002607 char *s = script != NULL ? script : (char *)eap->arg;
2608
Bram Moolenaar55d5c032010-07-17 23:52:29 +02002609 luaV_setrange(L, eap->line1, eap->line2);
2610 if (luaL_loadbuffer(L, s, strlen(s), LUAVIM_CHUNKNAME)
2611 || lua_pcall(L, 0, 0, 0))
2612 luaV_emsg(L);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02002613 }
Bram Moolenaarc8970b92020-10-26 20:18:08 +01002614 if (script != NULL)
2615 vim_free(script);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02002616}
2617
Bram Moolenaar55d5c032010-07-17 23:52:29 +02002618 void
2619ex_luado(exarg_T *eap)
2620{
Bram Moolenaar0ba04292010-07-14 23:23:17 +02002621 linenr_T l;
2622 const char *s = (const char *) eap->arg;
2623 luaL_Buffer b;
2624 size_t len;
Bram Moolenaard58f03b2017-01-29 22:48:45 +01002625 buf_T *was_curbuf = curbuf;
2626
Bram Moolenaar0ba04292010-07-14 23:23:17 +02002627 if (lua_init() == FAIL) return;
Bram Moolenaar55d5c032010-07-17 23:52:29 +02002628 if (u_save(eap->line1 - 1, eap->line2 + 1) == FAIL)
2629 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002630 emsg(_("cannot save undo information"));
Bram Moolenaar55d5c032010-07-17 23:52:29 +02002631 return;
Bram Moolenaar0ba04292010-07-14 23:23:17 +02002632 }
2633 luaV_setrange(L, eap->line1, eap->line2);
2634 luaL_buffinit(L, &b);
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002635 luaL_addlstring(&b, "return function(line, linenr) ", 30); // header
Bram Moolenaar0ba04292010-07-14 23:23:17 +02002636 luaL_addlstring(&b, s, strlen(s));
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002637 luaL_addlstring(&b, " end", 4); // footer
Bram Moolenaar0ba04292010-07-14 23:23:17 +02002638 luaL_pushresult(&b);
2639 s = lua_tolstring(L, -1, &len);
Bram Moolenaar55d5c032010-07-17 23:52:29 +02002640 if (luaL_loadbuffer(L, s, len, LUAVIM_CHUNKNAME))
2641 {
2642 luaV_emsg(L);
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002643 lua_pop(L, 1); // function body
Bram Moolenaar55d5c032010-07-17 23:52:29 +02002644 return;
Bram Moolenaar0ba04292010-07-14 23:23:17 +02002645 }
2646 lua_call(L, 0, 1);
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002647 lua_replace(L, -2); // function -> body
Bram Moolenaar55d5c032010-07-17 23:52:29 +02002648 for (l = eap->line1; l <= eap->line2; l++)
2649 {
Bram Moolenaare49b8e82020-07-01 13:52:55 +02002650 // Check the line number, the command may have deleted lines.
Bram Moolenaard58f03b2017-01-29 22:48:45 +01002651 if (l > curbuf->b_ml.ml_line_count)
2652 break;
2653
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002654 lua_pushvalue(L, -1); // function
2655 luaV_pushline(L, curbuf, l); // current line as arg
2656 lua_pushinteger(L, l); // current line number as arg
Bram Moolenaarbd2f3c32012-04-06 14:31:00 +02002657 if (lua_pcall(L, 2, 1, 0))
Bram Moolenaar55d5c032010-07-17 23:52:29 +02002658 {
2659 luaV_emsg(L);
2660 break;
2661 }
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002662 // Catch the command switching to another buffer.
Bram Moolenaard58f03b2017-01-29 22:48:45 +01002663 if (curbuf != was_curbuf)
2664 break;
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002665 if (lua_isstring(L, -1)) // update line?
Bram Moolenaar55d5c032010-07-17 23:52:29 +02002666 {
Bram Moolenaar0ba04292010-07-14 23:23:17 +02002667#ifdef HAVE_SANDBOX
Bram Moolenaar55d5c032010-07-17 23:52:29 +02002668 luaV_checksandbox(L);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02002669#endif
Bram Moolenaar55d5c032010-07-17 23:52:29 +02002670 ml_replace(l, luaV_toline(L, -1), TRUE);
2671 changed_bytes(l, 0);
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002672 lua_pop(L, 1); // result from luaV_toline
Bram Moolenaar55d5c032010-07-17 23:52:29 +02002673 }
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002674 lua_pop(L, 1); // line
Bram Moolenaar0ba04292010-07-14 23:23:17 +02002675 }
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002676 lua_pop(L, 1); // function
Bram Moolenaar0ba04292010-07-14 23:23:17 +02002677 check_cursor();
Bram Moolenaara4d158b2022-08-14 14:17:45 +01002678 update_screen(UPD_NOT_VALID);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02002679}
2680
Bram Moolenaar55d5c032010-07-17 23:52:29 +02002681 void
2682ex_luafile(exarg_T *eap)
2683{
2684 if (lua_init() == FAIL)
2685 return;
2686 if (!eap->skip)
2687 {
2688 luaV_setrange(L, eap->line1, eap->line2);
2689 if (luaL_loadfile(L, (char *) eap->arg) || lua_pcall(L, 0, 0, 0))
2690 luaV_emsg(L);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02002691 }
2692}
2693
Bram Moolenaar1dced572012-04-05 16:54:08 +02002694#define luaV_freetype(typ,tname) \
2695 void \
2696 lua_##tname##_free(typ *o) \
2697 { \
2698 if (!lua_isopen()) return; \
2699 luaV_getfield(L, LUAVIM_FREE); \
2700 lua_pushlightuserdata(L, (void *) o); \
2701 lua_call(L, 1, 0); \
2702 }
2703
2704luaV_freetype(buf_T, buffer)
2705luaV_freetype(win_T, window)
2706
Bram Moolenaar55d5c032010-07-17 23:52:29 +02002707 void
Bram Moolenaar4eefe472019-03-19 21:59:19 +01002708do_luaeval(char_u *str, typval_T *arg, typval_T *rettv)
Bram Moolenaar55d5c032010-07-17 23:52:29 +02002709{
Bram Moolenaar1dced572012-04-05 16:54:08 +02002710 lua_init();
2711 luaV_getfield(L, LUAVIM_LUAEVAL);
2712 lua_pushstring(L, (char *) str);
2713 lua_pushlightuserdata(L, (void *) arg);
2714 lua_pushlightuserdata(L, (void *) rettv);
2715 lua_call(L, 3, 0);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02002716}
2717
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01002718 int
Bram Moolenaar4eefe472019-03-19 21:59:19 +01002719set_ref_in_lua(int copyID)
Bram Moolenaar55d5c032010-07-17 23:52:29 +02002720{
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01002721 int aborted = 0;
2722
Yegappan Lakshmanan0233bdf2023-01-12 12:33:30 +00002723 if (!lua_isopen())
2724 return 0;
2725
2726 luaV_getfield(L, LUAVIM_SETREF);
2727 // call the function with 1 arg, getting 1 result back
2728 lua_pushinteger(L, copyID);
2729 lua_call(L, 1, 1);
2730 // get the result
2731 aborted = lua_tointeger(L, -1);
2732 // pop result off the stack
2733 lua_pop(L, 1);
2734
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01002735 return aborted;
Bram Moolenaar0ba04292010-07-14 23:23:17 +02002736}
2737
Bram Moolenaar788fbb42020-05-31 14:08:12 +02002738 void
2739update_package_paths_in_lua()
2740{
Yegappan Lakshmanan0233bdf2023-01-12 12:33:30 +00002741 if (!lua_isopen())
2742 return;
Bram Moolenaar788fbb42020-05-31 14:08:12 +02002743
Yegappan Lakshmanan0233bdf2023-01-12 12:33:30 +00002744 lua_getglobal(L, "vim");
2745 lua_getfield(L, -1, "_update_package_paths");
2746
2747 if (lua_pcall(L, 0, 0, 0))
2748 luaV_emsg(L);
Bram Moolenaar788fbb42020-05-31 14:08:12 +02002749}
2750
Bram Moolenaar801ab062020-06-25 19:27:56 +02002751/*
2752 * Native C function callback
2753 */
2754 static int
2755luaV_call_lua_func(
2756 int argcount,
2757 typval_T *argvars,
2758 typval_T *rettv,
2759 void *state)
2760{
2761 int i;
2762 int luaargcount = argcount;
2763 luaV_CFuncState *funcstate = (luaV_CFuncState*)state;
2764 lua_rawgeti(funcstate->L, LUA_REGISTRYINDEX, funcstate->lua_funcref);
2765
2766 if (funcstate->lua_tableref != LUA_NOREF)
2767 {
2768 // First arg for metatable __call method is a table
2769 luaargcount += 1;
2770 lua_rawgeti(funcstate->L, LUA_REGISTRYINDEX, funcstate->lua_tableref);
2771 }
2772
2773 for (i = 0; i < argcount; ++i)
2774 luaV_pushtypval(funcstate->L, &argvars[i]);
2775
2776 if (lua_pcall(funcstate->L, luaargcount, 1, 0))
2777 {
2778 luaV_emsg(funcstate->L);
2779 return FCERR_OTHER;
2780 }
2781
2782 luaV_checktypval(funcstate->L, -1, rettv, "get return value");
2783 return FCERR_NONE;
2784}
2785
2786/*
2787 * Free up any lua references held by the func state.
2788 */
2789 static void
2790luaV_call_lua_func_free(void *state)
2791{
2792 luaV_CFuncState *funcstate = (luaV_CFuncState*)state;
2793 luaL_unref(L, LUA_REGISTRYINDEX, funcstate->lua_funcref);
2794 funcstate->L = NULL;
2795 if (funcstate->lua_tableref != LUA_NOREF)
2796 luaL_unref(L, LUA_REGISTRYINDEX, funcstate->lua_tableref);
2797 VIM_CLEAR(funcstate);
2798}
2799
Bram Moolenaar0ba04292010-07-14 23:23:17 +02002800#endif