blob: c19244fae57958b326d5d970e35ab937c13fe49c [file] [log] [blame]
Bram Moolenaar1dced572012-04-05 16:54:08 +02001/* vi:set ts=8 sts=4 sw=4 noet:
Bram Moolenaar0ba04292010-07-14 23:23:17 +02002 *
3 * VIM - Vi IMproved by Bram Moolenaar
4 *
5 * Lua interface by Luis Carvalho
6 *
7 * Do ":help uganda" in Vim to read copying and usage conditions.
8 * Do ":help credits" in Vim to see a list of people who contributed.
9 * See README.txt for an overview of the Vim source code.
10 */
11
Bram Moolenaare2793352011-01-17 19:53:27 +010012#include "vim.h"
13
Bram Moolenaar0ba04292010-07-14 23:23:17 +020014#include <lua.h>
15#include <lualib.h>
16#include <lauxlib.h>
Bram Moolenaar0ba04292010-07-14 23:23:17 +020017
Bram Moolenaar2ab2e862019-12-04 21:24:53 +010018// Only do the following when the feature is enabled. Needed for "make
19// depend".
Bram Moolenaar0ba04292010-07-14 23:23:17 +020020#if defined(FEAT_LUA) || defined(PROTO)
21
22#define LUAVIM_CHUNKNAME "vim chunk"
23#define LUAVIM_NAME "vim"
Bram Moolenaar1dced572012-04-05 16:54:08 +020024#define LUAVIM_EVALNAME "luaeval"
25#define LUAVIM_EVALHEADER "local _A=select(1,...) return "
Bram Moolenaar0ba04292010-07-14 23:23:17 +020026
Bram Moolenaar125ed272021-04-07 20:11:12 +020027#ifdef LUA_RELEASE
28# define LUAVIM_VERSION LUA_RELEASE
29#else
30# define LUAVIM_VERSION LUA_VERSION
31#endif
32
Bram Moolenaar0ba04292010-07-14 23:23:17 +020033typedef buf_T *luaV_Buffer;
34typedef win_T *luaV_Window;
Bram Moolenaar1dced572012-04-05 16:54:08 +020035typedef dict_T *luaV_Dict;
36typedef list_T *luaV_List;
Bram Moolenaarb7828692019-03-23 13:57:02 +010037typedef blob_T *luaV_Blob;
Bram Moolenaarca06da92018-07-01 15:12:05 +020038typedef struct {
Bram Moolenaar4eefe472019-03-19 21:59:19 +010039 char_u *name; // funcref
Bram Moolenaarca06da92018-07-01 15:12:05 +020040 dict_T *self; // selfdict
41} luaV_Funcref;
Bram Moolenaarc8970b92020-10-26 20:18:08 +010042typedef int (*msgfunc_T)(char *);
Bram Moolenaar0ba04292010-07-14 23:23:17 +020043
Bram Moolenaar801ab062020-06-25 19:27:56 +020044typedef struct {
45 int lua_funcref; // ref to a lua func
46 int lua_tableref; // ref to a lua table if metatable else LUA_NOREF. used
47 // for __call
48 lua_State *L;
49} luaV_CFuncState;
50
Bram Moolenaar1dced572012-04-05 16:54:08 +020051static const char LUAVIM_DICT[] = "dict";
52static const char LUAVIM_LIST[] = "list";
Bram Moolenaarb7828692019-03-23 13:57:02 +010053static const char LUAVIM_BLOB[] = "blob";
Bram Moolenaarca06da92018-07-01 15:12:05 +020054static const char LUAVIM_FUNCREF[] = "funcref";
Bram Moolenaar0ba04292010-07-14 23:23:17 +020055static const char LUAVIM_BUFFER[] = "buffer";
56static const char LUAVIM_WINDOW[] = "window";
57static const char LUAVIM_FREE[] = "luaV_free";
Bram Moolenaar1dced572012-04-05 16:54:08 +020058static const char LUAVIM_LUAEVAL[] = "luaV_luaeval";
59static const char LUAVIM_SETREF[] = "luaV_setref";
Bram Moolenaar0ba04292010-07-14 23:23:17 +020060
Bram Moolenaar801ab062020-06-25 19:27:56 +020061static const char LUA___CALL[] = "__call";
62
Bram Moolenaar2ab2e862019-12-04 21:24:53 +010063// most functions are closures with a cache table as first upvalue;
64// get/setudata manage references to vim userdata in cache table through
65// object pointers (light userdata)
Bram Moolenaar1dced572012-04-05 16:54:08 +020066#define luaV_getudata(L, v) \
67 lua_pushlightuserdata((L), (void *) (v)); \
68 lua_rawget((L), lua_upvalueindex(1))
69#define luaV_setudata(L, v) \
70 lua_pushlightuserdata((L), (void *) (v)); \
71 lua_pushvalue((L), -2); \
72 lua_rawset((L), lua_upvalueindex(1))
Bram Moolenaar0ba04292010-07-14 23:23:17 +020073#define luaV_getfield(L, s) \
74 lua_pushlightuserdata((L), (void *)(s)); \
75 lua_rawget((L), LUA_REGISTRYINDEX)
76#define luaV_checksandbox(L) \
77 if (sandbox) luaL_error((L), "not allowed in sandbox")
78#define luaV_msg(L) luaV_msgfunc((L), (msgfunc_T) msg)
79#define luaV_emsg(L) luaV_msgfunc((L), (msgfunc_T) emsg)
Bram Moolenaarca06da92018-07-01 15:12:05 +020080#define luaV_checktypval(L, a, v, msg) \
81 do { \
Bram Moolenaar801ab062020-06-25 19:27:56 +020082 if (luaV_totypval(L, a, v) == FAIL) \
Bram Moolenaarca06da92018-07-01 15:12:05 +020083 luaL_error(L, msg ": cannot convert value"); \
84 } while (0)
Bram Moolenaar0ba04292010-07-14 23:23:17 +020085
Bram Moolenaarca06da92018-07-01 15:12:05 +020086static luaV_List *luaV_pushlist(lua_State *L, list_T *lis);
87static luaV_Dict *luaV_pushdict(lua_State *L, dict_T *dic);
Bram Moolenaarb7828692019-03-23 13:57:02 +010088static luaV_Blob *luaV_pushblob(lua_State *L, blob_T *blo);
Bram Moolenaar4eefe472019-03-19 21:59:19 +010089static luaV_Funcref *luaV_pushfuncref(lua_State *L, char_u *name);
Bram Moolenaar801ab062020-06-25 19:27:56 +020090static int luaV_call_lua_func(int argcount, typval_T *argvars, typval_T *rettv, void *state);
91static void luaV_call_lua_func_free(void *state);
Bram Moolenaar1dced572012-04-05 16:54:08 +020092
93#if LUA_VERSION_NUM <= 501
94#define luaV_openlib(L, l, n) luaL_openlib(L, NULL, l, n)
95#define luaL_typeerror luaL_typerror
96#else
97#define luaV_openlib luaL_setfuncs
98#endif
Bram Moolenaar0ba04292010-07-14 23:23:17 +020099
100#ifdef DYNAMIC_LUA
Bram Moolenaar2334b6d2010-07-22 21:32:16 +0200101
Bram Moolenaar4f974752019-02-17 17:44:42 +0100102#ifndef MSWIN
Bram Moolenaar2334b6d2010-07-22 21:32:16 +0200103# include <dlfcn.h>
104# define HANDLE void*
105# define load_dll(n) dlopen((n), RTLD_LAZY|RTLD_GLOBAL)
106# define symbol_from_dll dlsym
107# define close_dll dlclose
108#else
Bram Moolenaarebbcb822010-10-23 14:02:54 +0200109# define load_dll vimLoadLib
Bram Moolenaar2334b6d2010-07-22 21:32:16 +0200110# define symbol_from_dll GetProcAddress
111# define close_dll FreeLibrary
112#endif
113
Bram Moolenaar2ab2e862019-12-04 21:24:53 +0100114// lauxlib
Bram Moolenaar1dced572012-04-05 16:54:08 +0200115#if LUA_VERSION_NUM <= 501
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200116#define luaL_register dll_luaL_register
Bram Moolenaar1dced572012-04-05 16:54:08 +0200117#define luaL_prepbuffer dll_luaL_prepbuffer
118#define luaL_openlib dll_luaL_openlib
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200119#define luaL_typerror dll_luaL_typerror
Bram Moolenaar1dced572012-04-05 16:54:08 +0200120#define luaL_loadfile dll_luaL_loadfile
121#define luaL_loadbuffer dll_luaL_loadbuffer
122#else
123#define luaL_prepbuffsize dll_luaL_prepbuffsize
124#define luaL_setfuncs dll_luaL_setfuncs
125#define luaL_loadfilex dll_luaL_loadfilex
126#define luaL_loadbufferx dll_luaL_loadbufferx
127#define luaL_argerror dll_luaL_argerror
128#endif
Bram Moolenaar5551b132020-07-14 21:54:28 +0200129#if LUA_VERSION_NUM >= 504
130#define luaL_typeerror dll_luaL_typeerror
131#endif
Bram Moolenaarbd2f3c32012-04-06 14:31:00 +0200132#define luaL_checkany dll_luaL_checkany
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200133#define luaL_checklstring dll_luaL_checklstring
134#define luaL_checkinteger dll_luaL_checkinteger
135#define luaL_optinteger dll_luaL_optinteger
136#define luaL_checktype dll_luaL_checktype
137#define luaL_error dll_luaL_error
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200138#define luaL_newstate dll_luaL_newstate
139#define luaL_buffinit dll_luaL_buffinit
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200140#define luaL_addlstring dll_luaL_addlstring
141#define luaL_pushresult dll_luaL_pushresult
Bram Moolenaardf1643a2020-05-17 18:53:27 +0200142#define luaL_loadstring dll_luaL_loadstring
Bram Moolenaar1e4c7d02020-06-25 20:56:42 +0200143#define luaL_ref dll_luaL_ref
144#define luaL_unref dll_luaL_unref
Bram Moolenaar2ab2e862019-12-04 21:24:53 +0100145// lua
Bram Moolenaar1dced572012-04-05 16:54:08 +0200146#if LUA_VERSION_NUM <= 501
147#define lua_tonumber dll_lua_tonumber
148#define lua_tointeger dll_lua_tointeger
149#define lua_call dll_lua_call
150#define lua_pcall dll_lua_pcall
151#else
152#define lua_tonumberx dll_lua_tonumberx
153#define lua_tointegerx dll_lua_tointegerx
154#define lua_callk dll_lua_callk
155#define lua_pcallk dll_lua_pcallk
156#define lua_getglobal dll_lua_getglobal
157#define lua_setglobal dll_lua_setglobal
Bram Moolenaar1dced572012-04-05 16:54:08 +0200158#endif
Bram Moolenaar1f860d82015-06-27 18:36:16 +0200159#if LUA_VERSION_NUM <= 502
160#define lua_replace dll_lua_replace
161#define lua_remove dll_lua_remove
162#endif
163#if LUA_VERSION_NUM >= 503
164#define lua_rotate dll_lua_rotate
165#define lua_copy dll_lua_copy
166#endif
Bram Moolenaarbd2f3c32012-04-06 14:31:00 +0200167#define lua_typename dll_lua_typename
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200168#define lua_close dll_lua_close
169#define lua_gettop dll_lua_gettop
170#define lua_settop dll_lua_settop
171#define lua_pushvalue dll_lua_pushvalue
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200172#define lua_isnumber dll_lua_isnumber
173#define lua_isstring dll_lua_isstring
174#define lua_type dll_lua_type
175#define lua_rawequal dll_lua_rawequal
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200176#define lua_toboolean dll_lua_toboolean
177#define lua_tolstring dll_lua_tolstring
178#define lua_touserdata dll_lua_touserdata
179#define lua_pushnil dll_lua_pushnil
180#define lua_pushnumber dll_lua_pushnumber
181#define lua_pushinteger dll_lua_pushinteger
182#define lua_pushlstring dll_lua_pushlstring
183#define lua_pushstring dll_lua_pushstring
184#define lua_pushfstring dll_lua_pushfstring
185#define lua_pushcclosure dll_lua_pushcclosure
186#define lua_pushboolean dll_lua_pushboolean
187#define lua_pushlightuserdata dll_lua_pushlightuserdata
188#define lua_getfield dll_lua_getfield
189#define lua_rawget dll_lua_rawget
Bram Moolenaar1dced572012-04-05 16:54:08 +0200190#define lua_rawgeti dll_lua_rawgeti
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200191#define lua_createtable dll_lua_createtable
Bram Moolenaar830e3582018-08-21 14:23:35 +0200192#if LUA_VERSION_NUM >= 504
193 #define lua_newuserdatauv dll_lua_newuserdatauv
194#else
195 #define lua_newuserdata dll_lua_newuserdata
196#endif
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200197#define lua_getmetatable dll_lua_getmetatable
198#define lua_setfield dll_lua_setfield
199#define lua_rawset dll_lua_rawset
200#define lua_rawseti dll_lua_rawseti
201#define lua_setmetatable dll_lua_setmetatable
Bram Moolenaarbd2f3c32012-04-06 14:31:00 +0200202#define lua_next dll_lua_next
Bram Moolenaar2ab2e862019-12-04 21:24:53 +0100203// libs
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200204#define luaopen_base dll_luaopen_base
205#define luaopen_table dll_luaopen_table
206#define luaopen_string dll_luaopen_string
207#define luaopen_math dll_luaopen_math
Bram Moolenaar16c98f92010-07-28 22:46:08 +0200208#define luaopen_io dll_luaopen_io
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200209#define luaopen_os dll_luaopen_os
210#define luaopen_package dll_luaopen_package
211#define luaopen_debug dll_luaopen_debug
Bram Moolenaar16c98f92010-07-28 22:46:08 +0200212#define luaL_openlibs dll_luaL_openlibs
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200213
Bram Moolenaar2ab2e862019-12-04 21:24:53 +0100214// lauxlib
Bram Moolenaar1dced572012-04-05 16:54:08 +0200215#if LUA_VERSION_NUM <= 501
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200216void (*dll_luaL_register) (lua_State *L, const char *libname, const luaL_Reg *l);
Bram Moolenaar1dced572012-04-05 16:54:08 +0200217char *(*dll_luaL_prepbuffer) (luaL_Buffer *B);
218void (*dll_luaL_openlib) (lua_State *L, const char *libname, const luaL_Reg *l, int nup);
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200219int (*dll_luaL_typerror) (lua_State *L, int narg, const char *tname);
Bram Moolenaar1dced572012-04-05 16:54:08 +0200220int (*dll_luaL_loadfile) (lua_State *L, const char *filename);
221int (*dll_luaL_loadbuffer) (lua_State *L, const char *buff, size_t sz, const char *name);
222#else
223char *(*dll_luaL_prepbuffsize) (luaL_Buffer *B, size_t sz);
224void (*dll_luaL_setfuncs) (lua_State *L, const luaL_Reg *l, int nup);
225int (*dll_luaL_loadfilex) (lua_State *L, const char *filename, const char *mode);
226int (*dll_luaL_loadbufferx) (lua_State *L, const char *buff, size_t sz, const char *name, const char *mode);
227int (*dll_luaL_argerror) (lua_State *L, int numarg, const char *extramsg);
228#endif
Bram Moolenaar5551b132020-07-14 21:54:28 +0200229#if LUA_VERSION_NUM >= 504
230int (*dll_luaL_typeerror) (lua_State *L, int narg, const char *tname);
231#endif
Bram Moolenaarbd2f3c32012-04-06 14:31:00 +0200232void (*dll_luaL_checkany) (lua_State *L, int narg);
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200233const char *(*dll_luaL_checklstring) (lua_State *L, int numArg, size_t *l);
234lua_Integer (*dll_luaL_checkinteger) (lua_State *L, int numArg);
235lua_Integer (*dll_luaL_optinteger) (lua_State *L, int nArg, lua_Integer def);
236void (*dll_luaL_checktype) (lua_State *L, int narg, int t);
237int (*dll_luaL_error) (lua_State *L, const char *fmt, ...);
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200238lua_State *(*dll_luaL_newstate) (void);
239void (*dll_luaL_buffinit) (lua_State *L, luaL_Buffer *B);
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200240void (*dll_luaL_addlstring) (luaL_Buffer *B, const char *s, size_t l);
241void (*dll_luaL_pushresult) (luaL_Buffer *B);
Bram Moolenaardf1643a2020-05-17 18:53:27 +0200242int (*dll_luaL_loadstring) (lua_State *L, const char *s);
Bram Moolenaar1e4c7d02020-06-25 20:56:42 +0200243int (*dll_luaL_ref) (lua_State *L, int idx);
244#if LUA_VERSION_NUM <= 502
245void (*dll_luaL_unref) (lua_State *L, int idx, int n);
246#else
247void (*dll_luaL_unref) (lua_State *L, int idx, lua_Integer n);
248#endif
Bram Moolenaar2ab2e862019-12-04 21:24:53 +0100249// lua
Bram Moolenaar1dced572012-04-05 16:54:08 +0200250#if LUA_VERSION_NUM <= 501
251lua_Number (*dll_lua_tonumber) (lua_State *L, int idx);
252lua_Integer (*dll_lua_tointeger) (lua_State *L, int idx);
253void (*dll_lua_call) (lua_State *L, int nargs, int nresults);
254int (*dll_lua_pcall) (lua_State *L, int nargs, int nresults, int errfunc);
255#else
256lua_Number (*dll_lua_tonumberx) (lua_State *L, int idx, int *isnum);
257lua_Integer (*dll_lua_tointegerx) (lua_State *L, int idx, int *isnum);
258void (*dll_lua_callk) (lua_State *L, int nargs, int nresults, int ctx,
Bram Moolenaardb913952012-06-29 12:54:53 +0200259 lua_CFunction k);
Bram Moolenaar1dced572012-04-05 16:54:08 +0200260int (*dll_lua_pcallk) (lua_State *L, int nargs, int nresults, int errfunc,
Bram Moolenaardb913952012-06-29 12:54:53 +0200261 int ctx, lua_CFunction k);
Bram Moolenaar1dced572012-04-05 16:54:08 +0200262void (*dll_lua_getglobal) (lua_State *L, const char *var);
263void (*dll_lua_setglobal) (lua_State *L, const char *var);
Bram Moolenaar1f860d82015-06-27 18:36:16 +0200264#endif
265#if LUA_VERSION_NUM <= 502
266void (*dll_lua_replace) (lua_State *L, int idx);
267void (*dll_lua_remove) (lua_State *L, int idx);
268#endif
269#if LUA_VERSION_NUM >= 503
270void (*dll_lua_rotate) (lua_State *L, int idx, int n);
Bram Moolenaar9514b1f2015-06-25 18:27:32 +0200271void (*dll_lua_copy) (lua_State *L, int fromidx, int toidx);
Bram Moolenaar1dced572012-04-05 16:54:08 +0200272#endif
Bram Moolenaarbd2f3c32012-04-06 14:31:00 +0200273const char *(*dll_lua_typename) (lua_State *L, int tp);
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200274void (*dll_lua_close) (lua_State *L);
275int (*dll_lua_gettop) (lua_State *L);
276void (*dll_lua_settop) (lua_State *L, int idx);
277void (*dll_lua_pushvalue) (lua_State *L, int idx);
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200278int (*dll_lua_isnumber) (lua_State *L, int idx);
279int (*dll_lua_isstring) (lua_State *L, int idx);
280int (*dll_lua_type) (lua_State *L, int idx);
281int (*dll_lua_rawequal) (lua_State *L, int idx1, int idx2);
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200282int (*dll_lua_toboolean) (lua_State *L, int idx);
283const char *(*dll_lua_tolstring) (lua_State *L, int idx, size_t *len);
284void *(*dll_lua_touserdata) (lua_State *L, int idx);
285void (*dll_lua_pushnil) (lua_State *L);
286void (*dll_lua_pushnumber) (lua_State *L, lua_Number n);
287void (*dll_lua_pushinteger) (lua_State *L, lua_Integer n);
288void (*dll_lua_pushlstring) (lua_State *L, const char *s, size_t l);
289void (*dll_lua_pushstring) (lua_State *L, const char *s);
290const char *(*dll_lua_pushfstring) (lua_State *L, const char *fmt, ...);
291void (*dll_lua_pushcclosure) (lua_State *L, lua_CFunction fn, int n);
292void (*dll_lua_pushboolean) (lua_State *L, int b);
293void (*dll_lua_pushlightuserdata) (lua_State *L, void *p);
294void (*dll_lua_getfield) (lua_State *L, int idx, const char *k);
Bram Moolenaar17413672018-07-14 20:49:42 +0200295#if LUA_VERSION_NUM <= 502
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200296void (*dll_lua_rawget) (lua_State *L, int idx);
Bram Moolenaar1dced572012-04-05 16:54:08 +0200297void (*dll_lua_rawgeti) (lua_State *L, int idx, int n);
Bram Moolenaar17413672018-07-14 20:49:42 +0200298#else
299int (*dll_lua_rawget) (lua_State *L, int idx);
300int (*dll_lua_rawgeti) (lua_State *L, int idx, lua_Integer n);
301#endif
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200302void (*dll_lua_createtable) (lua_State *L, int narr, int nrec);
Bram Moolenaar830e3582018-08-21 14:23:35 +0200303#if LUA_VERSION_NUM >= 504
304void *(*dll_lua_newuserdatauv) (lua_State *L, size_t sz, int nuvalue);
305#else
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200306void *(*dll_lua_newuserdata) (lua_State *L, size_t sz);
Bram Moolenaar830e3582018-08-21 14:23:35 +0200307#endif
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200308int (*dll_lua_getmetatable) (lua_State *L, int objindex);
309void (*dll_lua_setfield) (lua_State *L, int idx, const char *k);
310void (*dll_lua_rawset) (lua_State *L, int idx);
Bram Moolenaar17413672018-07-14 20:49:42 +0200311#if LUA_VERSION_NUM <= 502
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200312void (*dll_lua_rawseti) (lua_State *L, int idx, int n);
Bram Moolenaar17413672018-07-14 20:49:42 +0200313#else
314void (*dll_lua_rawseti) (lua_State *L, int idx, lua_Integer n);
315#endif
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200316int (*dll_lua_setmetatable) (lua_State *L, int objindex);
Bram Moolenaarbd2f3c32012-04-06 14:31:00 +0200317int (*dll_lua_next) (lua_State *L, int idx);
Bram Moolenaar2ab2e862019-12-04 21:24:53 +0100318// libs
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200319int (*dll_luaopen_base) (lua_State *L);
320int (*dll_luaopen_table) (lua_State *L);
321int (*dll_luaopen_string) (lua_State *L);
322int (*dll_luaopen_math) (lua_State *L);
Bram Moolenaar16c98f92010-07-28 22:46:08 +0200323int (*dll_luaopen_io) (lua_State *L);
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200324int (*dll_luaopen_os) (lua_State *L);
325int (*dll_luaopen_package) (lua_State *L);
326int (*dll_luaopen_debug) (lua_State *L);
Bram Moolenaar16c98f92010-07-28 22:46:08 +0200327void (*dll_luaL_openlibs) (lua_State *L);
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200328
329typedef void **luaV_function;
330typedef struct {
331 const char *name;
332 luaV_function func;
333} luaV_Reg;
334
335static const luaV_Reg luaV_dll[] = {
Bram Moolenaar2ab2e862019-12-04 21:24:53 +0100336 // lauxlib
Bram Moolenaar1dced572012-04-05 16:54:08 +0200337#if LUA_VERSION_NUM <= 501
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200338 {"luaL_register", (luaV_function) &dll_luaL_register},
Bram Moolenaar1dced572012-04-05 16:54:08 +0200339 {"luaL_prepbuffer", (luaV_function) &dll_luaL_prepbuffer},
340 {"luaL_openlib", (luaV_function) &dll_luaL_openlib},
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200341 {"luaL_typerror", (luaV_function) &dll_luaL_typerror},
Bram Moolenaar1dced572012-04-05 16:54:08 +0200342 {"luaL_loadfile", (luaV_function) &dll_luaL_loadfile},
343 {"luaL_loadbuffer", (luaV_function) &dll_luaL_loadbuffer},
344#else
345 {"luaL_prepbuffsize", (luaV_function) &dll_luaL_prepbuffsize},
346 {"luaL_setfuncs", (luaV_function) &dll_luaL_setfuncs},
347 {"luaL_loadfilex", (luaV_function) &dll_luaL_loadfilex},
348 {"luaL_loadbufferx", (luaV_function) &dll_luaL_loadbufferx},
349 {"luaL_argerror", (luaV_function) &dll_luaL_argerror},
350#endif
Bram Moolenaar5551b132020-07-14 21:54:28 +0200351#if LUA_VERSION_NUM >= 504
352 {"luaL_typeerror", (luaV_function) &dll_luaL_typeerror},
353#endif
Bram Moolenaarbd2f3c32012-04-06 14:31:00 +0200354 {"luaL_checkany", (luaV_function) &dll_luaL_checkany},
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200355 {"luaL_checklstring", (luaV_function) &dll_luaL_checklstring},
356 {"luaL_checkinteger", (luaV_function) &dll_luaL_checkinteger},
357 {"luaL_optinteger", (luaV_function) &dll_luaL_optinteger},
358 {"luaL_checktype", (luaV_function) &dll_luaL_checktype},
359 {"luaL_error", (luaV_function) &dll_luaL_error},
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200360 {"luaL_newstate", (luaV_function) &dll_luaL_newstate},
361 {"luaL_buffinit", (luaV_function) &dll_luaL_buffinit},
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200362 {"luaL_addlstring", (luaV_function) &dll_luaL_addlstring},
363 {"luaL_pushresult", (luaV_function) &dll_luaL_pushresult},
Bram Moolenaardf1643a2020-05-17 18:53:27 +0200364 {"luaL_loadstring", (luaV_function) &dll_luaL_loadstring},
Bram Moolenaar1e4c7d02020-06-25 20:56:42 +0200365 {"luaL_ref", (luaV_function) &dll_luaL_ref},
366 {"luaL_unref", (luaV_function) &dll_luaL_unref},
Bram Moolenaar2ab2e862019-12-04 21:24:53 +0100367 // lua
Bram Moolenaar1dced572012-04-05 16:54:08 +0200368#if LUA_VERSION_NUM <= 501
369 {"lua_tonumber", (luaV_function) &dll_lua_tonumber},
370 {"lua_tointeger", (luaV_function) &dll_lua_tointeger},
371 {"lua_call", (luaV_function) &dll_lua_call},
372 {"lua_pcall", (luaV_function) &dll_lua_pcall},
373#else
374 {"lua_tonumberx", (luaV_function) &dll_lua_tonumberx},
375 {"lua_tointegerx", (luaV_function) &dll_lua_tointegerx},
376 {"lua_callk", (luaV_function) &dll_lua_callk},
377 {"lua_pcallk", (luaV_function) &dll_lua_pcallk},
378 {"lua_getglobal", (luaV_function) &dll_lua_getglobal},
379 {"lua_setglobal", (luaV_function) &dll_lua_setglobal},
Bram Moolenaar1dced572012-04-05 16:54:08 +0200380#endif
Bram Moolenaar1f860d82015-06-27 18:36:16 +0200381#if LUA_VERSION_NUM <= 502
382 {"lua_replace", (luaV_function) &dll_lua_replace},
383 {"lua_remove", (luaV_function) &dll_lua_remove},
384#endif
385#if LUA_VERSION_NUM >= 503
386 {"lua_rotate", (luaV_function) &dll_lua_rotate},
387 {"lua_copy", (luaV_function) &dll_lua_copy},
388#endif
Bram Moolenaarbd2f3c32012-04-06 14:31:00 +0200389 {"lua_typename", (luaV_function) &dll_lua_typename},
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200390 {"lua_close", (luaV_function) &dll_lua_close},
391 {"lua_gettop", (luaV_function) &dll_lua_gettop},
392 {"lua_settop", (luaV_function) &dll_lua_settop},
393 {"lua_pushvalue", (luaV_function) &dll_lua_pushvalue},
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200394 {"lua_isnumber", (luaV_function) &dll_lua_isnumber},
395 {"lua_isstring", (luaV_function) &dll_lua_isstring},
396 {"lua_type", (luaV_function) &dll_lua_type},
397 {"lua_rawequal", (luaV_function) &dll_lua_rawequal},
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200398 {"lua_toboolean", (luaV_function) &dll_lua_toboolean},
399 {"lua_tolstring", (luaV_function) &dll_lua_tolstring},
400 {"lua_touserdata", (luaV_function) &dll_lua_touserdata},
401 {"lua_pushnil", (luaV_function) &dll_lua_pushnil},
402 {"lua_pushnumber", (luaV_function) &dll_lua_pushnumber},
403 {"lua_pushinteger", (luaV_function) &dll_lua_pushinteger},
404 {"lua_pushlstring", (luaV_function) &dll_lua_pushlstring},
405 {"lua_pushstring", (luaV_function) &dll_lua_pushstring},
406 {"lua_pushfstring", (luaV_function) &dll_lua_pushfstring},
407 {"lua_pushcclosure", (luaV_function) &dll_lua_pushcclosure},
408 {"lua_pushboolean", (luaV_function) &dll_lua_pushboolean},
409 {"lua_pushlightuserdata", (luaV_function) &dll_lua_pushlightuserdata},
410 {"lua_getfield", (luaV_function) &dll_lua_getfield},
411 {"lua_rawget", (luaV_function) &dll_lua_rawget},
Bram Moolenaar1dced572012-04-05 16:54:08 +0200412 {"lua_rawgeti", (luaV_function) &dll_lua_rawgeti},
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200413 {"lua_createtable", (luaV_function) &dll_lua_createtable},
Bram Moolenaar830e3582018-08-21 14:23:35 +0200414#if LUA_VERSION_NUM >= 504
415 {"lua_newuserdatauv", (luaV_function) &dll_lua_newuserdatauv},
416#else
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200417 {"lua_newuserdata", (luaV_function) &dll_lua_newuserdata},
Bram Moolenaar830e3582018-08-21 14:23:35 +0200418#endif
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200419 {"lua_getmetatable", (luaV_function) &dll_lua_getmetatable},
420 {"lua_setfield", (luaV_function) &dll_lua_setfield},
421 {"lua_rawset", (luaV_function) &dll_lua_rawset},
422 {"lua_rawseti", (luaV_function) &dll_lua_rawseti},
423 {"lua_setmetatable", (luaV_function) &dll_lua_setmetatable},
Bram Moolenaarbd2f3c32012-04-06 14:31:00 +0200424 {"lua_next", (luaV_function) &dll_lua_next},
Bram Moolenaar2ab2e862019-12-04 21:24:53 +0100425 // libs
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200426 {"luaopen_base", (luaV_function) &dll_luaopen_base},
427 {"luaopen_table", (luaV_function) &dll_luaopen_table},
428 {"luaopen_string", (luaV_function) &dll_luaopen_string},
429 {"luaopen_math", (luaV_function) &dll_luaopen_math},
Bram Moolenaar16c98f92010-07-28 22:46:08 +0200430 {"luaopen_io", (luaV_function) &dll_luaopen_io},
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200431 {"luaopen_os", (luaV_function) &dll_luaopen_os},
432 {"luaopen_package", (luaV_function) &dll_luaopen_package},
433 {"luaopen_debug", (luaV_function) &dll_luaopen_debug},
Bram Moolenaar16c98f92010-07-28 22:46:08 +0200434 {"luaL_openlibs", (luaV_function) &dll_luaL_openlibs},
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200435 {NULL, NULL}
436};
437
Bram Moolenaar2334b6d2010-07-22 21:32:16 +0200438static HANDLE hinstLua = NULL;
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200439
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200440 static int
441lua_link_init(char *libname, int verbose)
442{
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200443 const luaV_Reg *reg;
444 if (hinstLua) return OK;
Bram Moolenaar2334b6d2010-07-22 21:32:16 +0200445 hinstLua = load_dll(libname);
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200446 if (!hinstLua)
447 {
448 if (verbose)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100449 semsg(_(e_loadlib), libname);
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200450 return FAIL;
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200451 }
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200452 for (reg = luaV_dll; reg->func; reg++)
453 {
Bram Moolenaar2334b6d2010-07-22 21:32:16 +0200454 if ((*reg->func = symbol_from_dll(hinstLua, reg->name)) == NULL)
455 {
456 close_dll(hinstLua);
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200457 hinstLua = 0;
458 if (verbose)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100459 semsg(_(e_loadfunc), reg->name);
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200460 return FAIL;
461 }
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200462 }
463 return OK;
464}
Bram Moolenaar2ab2e862019-12-04 21:24:53 +0100465#endif // DYNAMIC_LUA
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200466
Bram Moolenaard90b6c02016-08-28 18:10:45 +0200467#if defined(DYNAMIC_LUA) || defined(PROTO)
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200468 int
469lua_enabled(int verbose)
470{
Bram Moolenaar25e4fcd2016-01-09 14:57:47 +0100471 return lua_link_init((char *)p_luadll, verbose) == OK;
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200472}
Bram Moolenaard90b6c02016-08-28 18:10:45 +0200473#endif
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200474
Bram Moolenaar5551b132020-07-14 21:54:28 +0200475#if LUA_VERSION_NUM > 501 && LUA_VERSION_NUM < 504
Bram Moolenaar1dced572012-04-05 16:54:08 +0200476 static int
Bram Moolenaar4eefe472019-03-19 21:59:19 +0100477luaL_typeerror(lua_State *L, int narg, const char *tname)
Bram Moolenaar1dced572012-04-05 16:54:08 +0200478{
479 const char *msg = lua_pushfstring(L, "%s expected, got %s",
Bram Moolenaardb913952012-06-29 12:54:53 +0200480 tname, luaL_typename(L, narg));
Bram Moolenaar1dced572012-04-05 16:54:08 +0200481 return luaL_argerror(L, narg, msg);
482}
483#endif
484
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200485
Bram Moolenaar2ab2e862019-12-04 21:24:53 +0100486// ======= Internal =======
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200487
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200488 static void
489luaV_newmetatable(lua_State *L, const char *tname)
490{
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200491 lua_newtable(L);
492 lua_pushlightuserdata(L, (void *) tname);
493 lua_pushvalue(L, -2);
494 lua_rawset(L, LUA_REGISTRYINDEX);
495}
496
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200497 static void *
498luaV_toudata(lua_State *L, int ud, const char *tname)
499{
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200500 void *p = lua_touserdata(L, ud);
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200501
Bram Moolenaar2ab2e862019-12-04 21:24:53 +0100502 if (p != NULL) // value is userdata?
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200503 {
Bram Moolenaar2ab2e862019-12-04 21:24:53 +0100504 if (lua_getmetatable(L, ud)) // does it have a metatable?
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200505 {
Bram Moolenaar2ab2e862019-12-04 21:24:53 +0100506 luaV_getfield(L, tname); // get metatable
507 if (lua_rawequal(L, -1, -2)) // MTs match?
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200508 {
Bram Moolenaar2ab2e862019-12-04 21:24:53 +0100509 lua_pop(L, 2); // MTs
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200510 return p;
511 }
512 }
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200513 }
514 return NULL;
515}
516
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200517 static void *
Bram Moolenaar1dced572012-04-05 16:54:08 +0200518luaV_checkcache(lua_State *L, void *p)
519{
520 luaV_getudata(L, p);
521 if (lua_isnil(L, -1)) luaL_error(L, "invalid object");
522 lua_pop(L, 1);
523 return p;
524}
525
526#define luaV_unbox(L,luatyp,ud) (*((luatyp *) lua_touserdata((L),(ud))))
527
528#define luaV_checkvalid(L,luatyp,ud) \
529 luaV_checkcache((L), (void *) luaV_unbox((L),luatyp,(ud)))
530
531 static void *
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200532luaV_checkudata(lua_State *L, int ud, const char *tname)
533{
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200534 void *p = luaV_toudata(L, ud, tname);
Bram Moolenaar1dced572012-04-05 16:54:08 +0200535 if (p == NULL) luaL_typeerror(L, ud, tname);
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200536 return p;
537}
538
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200539 static void
540luaV_pushtypval(lua_State *L, typval_T *tv)
541{
Bram Moolenaar1dced572012-04-05 16:54:08 +0200542 if (tv == NULL)
543 {
544 lua_pushnil(L);
545 return;
546 }
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200547 switch (tv->v_type)
548 {
549 case VAR_STRING:
Bram Moolenaard04da7c2012-10-14 03:41:59 +0200550 lua_pushstring(L, tv->vval.v_string == NULL
551 ? "" : (char *)tv->vval.v_string);
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200552 break;
553 case VAR_NUMBER:
554 lua_pushinteger(L, (int) tv->vval.v_number);
555 break;
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200556#ifdef FEAT_FLOAT
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200557 case VAR_FLOAT:
558 lua_pushnumber(L, (lua_Number) tv->vval.v_float);
559 break;
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200560#endif
Bram Moolenaar1dced572012-04-05 16:54:08 +0200561 case VAR_LIST:
562 luaV_pushlist(L, tv->vval.v_list);
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200563 break;
Bram Moolenaar1dced572012-04-05 16:54:08 +0200564 case VAR_DICT:
565 luaV_pushdict(L, tv->vval.v_dict);
566 break;
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +0100567 case VAR_BOOL:
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100568 case VAR_SPECIAL:
569 if (tv->vval.v_number <= VVAL_TRUE)
570 lua_pushinteger(L, (int) tv->vval.v_number);
571 else
572 lua_pushnil(L);
573 break;
Bram Moolenaarca06da92018-07-01 15:12:05 +0200574 case VAR_FUNC:
Bram Moolenaar4eefe472019-03-19 21:59:19 +0100575 luaV_pushfuncref(L, tv->vval.v_string);
Bram Moolenaarca06da92018-07-01 15:12:05 +0200576 break;
Bram Moolenaar86c3a212021-03-08 19:50:24 +0100577 case VAR_PARTIAL:
578 // TODO: handle partial arguments
579 luaV_pushfuncref(L, partial_name(tv->vval.v_partial));
580 break;
581
Bram Moolenaarb7828692019-03-23 13:57:02 +0100582 case VAR_BLOB:
583 luaV_pushblob(L, tv->vval.v_blob);
584 break;
Bram Moolenaar1dced572012-04-05 16:54:08 +0200585 default:
586 lua_pushnil(L);
587 }
588}
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200589
Bram Moolenaarca06da92018-07-01 15:12:05 +0200590/*
591 * Converts lua value at 'pos' to typval 'tv'.
592 * Returns OK or FAIL.
593 */
594 static int
595luaV_totypval(lua_State *L, int pos, typval_T *tv)
Bram Moolenaar1dced572012-04-05 16:54:08 +0200596{
Bram Moolenaarca06da92018-07-01 15:12:05 +0200597 int status = OK;
598
Bram Moolenaara9a8e5f2020-07-02 21:17:57 +0200599 tv->v_lock = 0;
600
Bram Moolenaarca06da92018-07-01 15:12:05 +0200601 switch (lua_type(L, pos))
602 {
Bram Moolenaar1dced572012-04-05 16:54:08 +0200603 case LUA_TBOOLEAN:
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +0100604 tv->v_type = VAR_BOOL;
Bram Moolenaar1dced572012-04-05 16:54:08 +0200605 tv->vval.v_number = (varnumber_T) lua_toboolean(L, pos);
606 break;
Bram Moolenaar9067cd62019-01-01 00:41:54 +0100607 case LUA_TNIL:
608 tv->v_type = VAR_SPECIAL;
609 tv->vval.v_number = VVAL_NULL;
610 break;
Bram Moolenaar1dced572012-04-05 16:54:08 +0200611 case LUA_TSTRING:
612 tv->v_type = VAR_STRING;
613 tv->vval.v_string = vim_strsave((char_u *) lua_tostring(L, pos));
614 break;
615 case LUA_TNUMBER:
616#ifdef FEAT_FLOAT
Bram Moolenaareb04f082020-05-17 14:32:35 +0200617 {
618 const lua_Number n = lua_tonumber(L, pos);
619
620 if (n > (lua_Number)INT64_MAX || n < (lua_Number)INT64_MIN
621 || ((lua_Number)((varnumber_T)n)) != n)
622 {
623 tv->v_type = VAR_FLOAT;
624 tv->vval.v_float = (float_T)n;
625 }
626 else
627 {
628 tv->v_type = VAR_NUMBER;
629 tv->vval.v_number = (varnumber_T)n;
630 }
631 }
Bram Moolenaar1dced572012-04-05 16:54:08 +0200632#else
633 tv->v_type = VAR_NUMBER;
634 tv->vval.v_number = (varnumber_T) lua_tointeger(L, pos);
635#endif
636 break;
Bram Moolenaar801ab062020-06-25 19:27:56 +0200637 case LUA_TFUNCTION:
638 {
639 char_u *name;
Bram Moolenaar066e7da2020-07-18 12:50:35 +0200640 luaV_CFuncState *state;
641
Bram Moolenaar801ab062020-06-25 19:27:56 +0200642 lua_pushvalue(L, pos);
Bram Moolenaar066e7da2020-07-18 12:50:35 +0200643 state = ALLOC_CLEAR_ONE(luaV_CFuncState);
Bram Moolenaar801ab062020-06-25 19:27:56 +0200644 state->lua_funcref = luaL_ref(L, LUA_REGISTRYINDEX);
645 state->L = L;
646 state->lua_tableref = LUA_NOREF;
647 name = register_cfunc(&luaV_call_lua_func,
648 &luaV_call_lua_func_free, state);
649 tv->v_type = VAR_FUNC;
650 tv->vval.v_string = vim_strsave(name);
651 break;
652 }
653 case LUA_TTABLE:
654 {
Bram Moolenaar066e7da2020-07-18 12:50:35 +0200655 int lua_tableref;
656
Bram Moolenaar801ab062020-06-25 19:27:56 +0200657 lua_pushvalue(L, pos);
Bram Moolenaar066e7da2020-07-18 12:50:35 +0200658 lua_tableref = luaL_ref(L, LUA_REGISTRYINDEX);
Bram Moolenaar801ab062020-06-25 19:27:56 +0200659 if (lua_getmetatable(L, pos)) {
660 lua_getfield(L, -1, LUA___CALL);
661 if (lua_isfunction(L, -1)) {
662 char_u *name;
663 int lua_funcref = luaL_ref(L, LUA_REGISTRYINDEX);
664 luaV_CFuncState *state = ALLOC_CLEAR_ONE(luaV_CFuncState);
Bram Moolenaar066e7da2020-07-18 12:50:35 +0200665
Bram Moolenaar801ab062020-06-25 19:27:56 +0200666 state->lua_funcref = lua_funcref;
667 state->L = L;
668 state->lua_tableref = lua_tableref;
669 name = register_cfunc(&luaV_call_lua_func,
670 &luaV_call_lua_func_free, state);
671 tv->v_type = VAR_FUNC;
672 tv->vval.v_string = vim_strsave(name);
673 break;
674 }
675 }
676 tv->v_type = VAR_NUMBER;
677 tv->vval.v_number = 0;
678 status = FAIL;
679 break;
680 }
Bram Moolenaarca06da92018-07-01 15:12:05 +0200681 case LUA_TUSERDATA:
682 {
Bram Moolenaar1dced572012-04-05 16:54:08 +0200683 void *p = lua_touserdata(L, pos);
Bram Moolenaarca06da92018-07-01 15:12:05 +0200684
Bram Moolenaarb7828692019-03-23 13:57:02 +0100685 if (lua_getmetatable(L, pos)) // has metatable?
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200686 {
Bram Moolenaarb7828692019-03-23 13:57:02 +0100687 // check list
Bram Moolenaar1dced572012-04-05 16:54:08 +0200688 luaV_getfield(L, LUAVIM_LIST);
689 if (lua_rawequal(L, -1, -2))
Bram Moolenaar2334b6d2010-07-22 21:32:16 +0200690 {
Bram Moolenaar1dced572012-04-05 16:54:08 +0200691 tv->v_type = VAR_LIST;
692 tv->vval.v_list = *((luaV_List *) p);
693 ++tv->vval.v_list->lv_refcount;
Bram Moolenaarb7828692019-03-23 13:57:02 +0100694 lua_pop(L, 2); // MTs
Bram Moolenaarca06da92018-07-01 15:12:05 +0200695 break;
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200696 }
Bram Moolenaarb7828692019-03-23 13:57:02 +0100697 // check dict
Bram Moolenaar1dced572012-04-05 16:54:08 +0200698 luaV_getfield(L, LUAVIM_DICT);
699 if (lua_rawequal(L, -1, -3))
700 {
701 tv->v_type = VAR_DICT;
702 tv->vval.v_dict = *((luaV_Dict *) p);
703 ++tv->vval.v_dict->dv_refcount;
Bram Moolenaarb7828692019-03-23 13:57:02 +0100704 lua_pop(L, 3); // MTs
Bram Moolenaarca06da92018-07-01 15:12:05 +0200705 break;
Bram Moolenaar1dced572012-04-05 16:54:08 +0200706 }
Bram Moolenaarb7828692019-03-23 13:57:02 +0100707 // check blob
708 luaV_getfield(L, LUAVIM_BLOB);
Bram Moolenaarca06da92018-07-01 15:12:05 +0200709 if (lua_rawequal(L, -1, -4))
710 {
Bram Moolenaarb7828692019-03-23 13:57:02 +0100711 tv->v_type = VAR_BLOB;
712 tv->vval.v_blob = *((luaV_Blob *) p);
713 ++tv->vval.v_blob->bv_refcount;
714 lua_pop(L, 4); // MTs
715 break;
716 }
717 // check funcref
718 luaV_getfield(L, LUAVIM_FUNCREF);
719 if (lua_rawequal(L, -1, -5))
720 {
Bram Moolenaarca06da92018-07-01 15:12:05 +0200721 luaV_Funcref *f = (luaV_Funcref *) p;
Bram Moolenaar066e7da2020-07-18 12:50:35 +0200722
Bram Moolenaar4eefe472019-03-19 21:59:19 +0100723 func_ref(f->name);
724 tv->v_type = VAR_FUNC;
725 tv->vval.v_string = vim_strsave(f->name);
Bram Moolenaarb7828692019-03-23 13:57:02 +0100726 lua_pop(L, 5); // MTs
Bram Moolenaarca06da92018-07-01 15:12:05 +0200727 break;
728 }
Bram Moolenaarb7828692019-03-23 13:57:02 +0100729 lua_pop(L, 4); // MTs
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200730 }
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200731 }
Bram Moolenaarb7828692019-03-23 13:57:02 +0100732 // FALLTHROUGH
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200733 default:
Bram Moolenaar1dced572012-04-05 16:54:08 +0200734 tv->v_type = VAR_NUMBER;
735 tv->vval.v_number = 0;
Bram Moolenaarca06da92018-07-01 15:12:05 +0200736 status = FAIL;
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200737 }
Bram Moolenaarca06da92018-07-01 15:12:05 +0200738 return status;
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200739}
740
Bram Moolenaar2ab2e862019-12-04 21:24:53 +0100741/*
742 * similar to luaL_addlstring, but replaces \0 with \n if toline and
743 * \n with \0 otherwise
744 */
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200745 static void
746luaV_addlstring(luaL_Buffer *b, const char *s, size_t l, int toline)
747{
748 while (l--)
749 {
750 if (*s == '\0' && toline)
751 luaL_addchar(b, '\n');
752 else if (*s == '\n' && !toline)
753 luaL_addchar(b, '\0');
754 else
755 luaL_addchar(b, *s);
756 s++;
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200757 }
758}
759
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200760 static void
761luaV_pushline(lua_State *L, buf_T *buf, linenr_T n)
762{
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200763 const char *s = (const char *) ml_get_buf(buf, n, FALSE);
764 luaL_Buffer b;
765 luaL_buffinit(L, &b);
766 luaV_addlstring(&b, s, strlen(s), 0);
767 luaL_pushresult(&b);
768}
769
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200770 static char_u *
771luaV_toline(lua_State *L, int pos)
772{
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200773 size_t l;
774 const char *s = lua_tolstring(L, pos, &l);
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200775
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200776 luaL_Buffer b;
777 luaL_buffinit(L, &b);
778 luaV_addlstring(&b, s, l, 1);
779 luaL_pushresult(&b);
780 return (char_u *) lua_tostring(L, -1);
781}
782
Bram Moolenaar2ab2e862019-12-04 21:24:53 +0100783/*
784 * pops a string s from the top of the stack and calls mf(t) for pieces t of
785 * s separated by newlines
786 */
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200787 static void
788luaV_msgfunc(lua_State *L, msgfunc_T mf)
789{
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200790 luaL_Buffer b;
791 size_t l;
792 const char *p, *s = lua_tolstring(L, -1, &l);
793 luaL_buffinit(L, &b);
794 luaV_addlstring(&b, s, l, 0);
795 luaL_pushresult(&b);
Bram Moolenaar2ab2e862019-12-04 21:24:53 +0100796 // break string
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200797 p = s = lua_tolstring(L, -1, &l);
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200798 while (l--)
799 {
Bram Moolenaar2ab2e862019-12-04 21:24:53 +0100800 if (*p++ == '\0') // break?
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200801 {
Bram Moolenaarc8970b92020-10-26 20:18:08 +0100802 mf((char *)s);
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200803 s = p;
804 }
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200805 }
Bram Moolenaarc8970b92020-10-26 20:18:08 +0100806 mf((char *)s);
Bram Moolenaar2ab2e862019-12-04 21:24:53 +0100807 lua_pop(L, 2); // original and modified strings
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200808}
809
Bram Moolenaar1dced572012-04-05 16:54:08 +0200810#define luaV_newtype(typ,tname,luatyp,luatname) \
811 static luatyp * \
Bram Moolenaar4eefe472019-03-19 21:59:19 +0100812 luaV_new##tname(lua_State *L, typ *obj) \
Bram Moolenaar1dced572012-04-05 16:54:08 +0200813 { \
814 luatyp *o = (luatyp *) lua_newuserdata(L, sizeof(luatyp)); \
815 *o = obj; \
816 luaV_setudata(L, obj); /* cache[obj] = udata */ \
817 luaV_getfield(L, luatname); \
818 lua_setmetatable(L, -2); \
819 return o; \
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200820 }
Bram Moolenaar1dced572012-04-05 16:54:08 +0200821
822#define luaV_pushtype(typ,tname,luatyp) \
823 static luatyp * \
Bram Moolenaarca06da92018-07-01 15:12:05 +0200824 luaV_push##tname(lua_State *L, typ *obj) \
Bram Moolenaar1dced572012-04-05 16:54:08 +0200825 { \
826 luatyp *o = NULL; \
827 if (obj == NULL) \
828 lua_pushnil(L); \
829 else { \
830 luaV_getudata(L, obj); \
831 if (lua_isnil(L, -1)) /* not interned? */ \
832 { \
833 lua_pop(L, 1); \
834 o = luaV_new##tname(L, obj); \
835 } \
836 else \
837 o = (luatyp *) lua_touserdata(L, -1); \
838 } \
839 return o; \
840 }
841
842#define luaV_type_tostring(tname,luatname) \
843 static int \
Bram Moolenaar4eefe472019-03-19 21:59:19 +0100844 luaV_##tname##_tostring(lua_State *L) \
Bram Moolenaar1dced572012-04-05 16:54:08 +0200845 { \
846 lua_pushfstring(L, "%s: %p", luatname, lua_touserdata(L, 1)); \
847 return 1; \
848 }
849
Bram Moolenaar2ab2e862019-12-04 21:24:53 +0100850// ======= List type =======
Bram Moolenaar1dced572012-04-05 16:54:08 +0200851
852 static luaV_List *
Bram Moolenaar4eefe472019-03-19 21:59:19 +0100853luaV_newlist(lua_State *L, list_T *lis)
Bram Moolenaar1dced572012-04-05 16:54:08 +0200854{
855 luaV_List *l = (luaV_List *) lua_newuserdata(L, sizeof(luaV_List));
856 *l = lis;
Bram Moolenaar2ab2e862019-12-04 21:24:53 +0100857 lis->lv_refcount++; // reference in Lua
858 luaV_setudata(L, lis); // cache[lis] = udata
Bram Moolenaar1dced572012-04-05 16:54:08 +0200859 luaV_getfield(L, LUAVIM_LIST);
860 lua_setmetatable(L, -2);
861 return l;
862}
863
864luaV_pushtype(list_T, list, luaV_List)
865luaV_type_tostring(list, LUAVIM_LIST)
866
867 static int
Bram Moolenaar4eefe472019-03-19 21:59:19 +0100868luaV_list_len(lua_State *L)
Bram Moolenaar1dced572012-04-05 16:54:08 +0200869{
870 list_T *l = luaV_unbox(L, luaV_List, 1);
Bram Moolenaarb7828692019-03-23 13:57:02 +0100871 lua_pushinteger(L, (int) list_len(l));
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200872 return 1;
873}
874
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200875 static int
Bram Moolenaar4eefe472019-03-19 21:59:19 +0100876luaV_list_iter(lua_State *L)
Bram Moolenaar1dced572012-04-05 16:54:08 +0200877{
878 listitem_T *li = (listitem_T *) lua_touserdata(L, lua_upvalueindex(2));
879 if (li == NULL) return 0;
880 luaV_pushtypval(L, &li->li_tv);
881 lua_pushlightuserdata(L, (void *) li->li_next);
882 lua_replace(L, lua_upvalueindex(2));
883 return 1;
884}
885
886 static int
Bram Moolenaar4eefe472019-03-19 21:59:19 +0100887luaV_list_call(lua_State *L)
Bram Moolenaar1dced572012-04-05 16:54:08 +0200888{
889 list_T *l = luaV_unbox(L, luaV_List, 1);
Bram Moolenaar2ab2e862019-12-04 21:24:53 +0100890 lua_pushvalue(L, lua_upvalueindex(1)); // pass cache table along
Bram Moolenaar1dced572012-04-05 16:54:08 +0200891 lua_pushlightuserdata(L, (void *) l->lv_first);
892 lua_pushcclosure(L, luaV_list_iter, 2);
893 return 1;
894}
895
896 static int
Bram Moolenaar4eefe472019-03-19 21:59:19 +0100897luaV_list_index(lua_State *L)
Bram Moolenaar1dced572012-04-05 16:54:08 +0200898{
899 list_T *l = luaV_unbox(L, luaV_List, 1);
Bram Moolenaar2ab2e862019-12-04 21:24:53 +0100900 if (lua_isnumber(L, 2)) // list item?
Bram Moolenaar1dced572012-04-05 16:54:08 +0200901 {
Bram Moolenaarbd846172020-06-27 12:32:57 +0200902 long n = (long) luaL_checkinteger(L, 2);
903 listitem_T *li;
904
905 // Lua array index starts with 1 while Vim uses 0, subtract 1 to
906 // normalize.
907 n -= 1;
908 li = list_find(l, n);
Bram Moolenaar1dced572012-04-05 16:54:08 +0200909 if (li == NULL)
910 lua_pushnil(L);
911 else
912 luaV_pushtypval(L, &li->li_tv);
913 }
Bram Moolenaar2ab2e862019-12-04 21:24:53 +0100914 else if (lua_isstring(L, 2)) // method?
Bram Moolenaar1dced572012-04-05 16:54:08 +0200915 {
916 const char *s = lua_tostring(L, 2);
917 if (strncmp(s, "add", 3) == 0
Bram Moolenaarb3766472013-04-15 13:49:21 +0200918 || strncmp(s, "insert", 6) == 0)
Bram Moolenaar1dced572012-04-05 16:54:08 +0200919 {
920 lua_getmetatable(L, 1);
921 lua_getfield(L, -1, s);
922 }
923 else
924 lua_pushnil(L);
925 }
926 else
927 lua_pushnil(L);
928 return 1;
929}
930
931 static int
Bram Moolenaar4eefe472019-03-19 21:59:19 +0100932luaV_list_newindex(lua_State *L)
Bram Moolenaar1dced572012-04-05 16:54:08 +0200933{
934 list_T *l = luaV_unbox(L, luaV_List, 1);
935 long n = (long) luaL_checkinteger(L, 2);
936 listitem_T *li;
Bram Moolenaarbd846172020-06-27 12:32:57 +0200937
938 // Lua array index starts with 1 while Vim uses 0, subtract 1 to normalize.
939 n -= 1;
940
Bram Moolenaar1dced572012-04-05 16:54:08 +0200941 if (l->lv_lock)
942 luaL_error(L, "list is locked");
943 li = list_find(l, n);
Bram Moolenaara1f9f862020-06-28 22:41:26 +0200944 if (li == NULL)
Bram Moolenaar1dced572012-04-05 16:54:08 +0200945 {
Bram Moolenaara1f9f862020-06-28 22:41:26 +0200946 if (!lua_isnil(L, 3))
947 {
948 typval_T v;
949 luaV_checktypval(L, 3, &v, "inserting list item");
950 if (list_insert_tv(l, &v, li) == FAIL)
951 luaL_error(L, "failed to add item to list");
952 clear_tv(&v);
953 }
Bram Moolenaar1dced572012-04-05 16:54:08 +0200954 }
955 else
956 {
Bram Moolenaara1f9f862020-06-28 22:41:26 +0200957 if (lua_isnil(L, 3)) // remove?
958 {
959 vimlist_remove(l, li, li);
960 listitem_free(l, li);
961 }
962 else
963 {
964 typval_T v;
965 luaV_checktypval(L, 3, &v, "setting list item");
966 clear_tv(&li->li_tv);
Bram Moolenaare49b8e82020-07-01 13:52:55 +0200967 li->li_tv = v;
Bram Moolenaara1f9f862020-06-28 22:41:26 +0200968 }
Bram Moolenaar1dced572012-04-05 16:54:08 +0200969 }
970 return 0;
971}
972
973 static int
Bram Moolenaar4eefe472019-03-19 21:59:19 +0100974luaV_list_add(lua_State *L)
Bram Moolenaar1dced572012-04-05 16:54:08 +0200975{
976 luaV_List *lis = luaV_checkudata(L, 1, LUAVIM_LIST);
977 list_T *l = (list_T *) luaV_checkcache(L, (void *) *lis);
Bram Moolenaarb3766472013-04-15 13:49:21 +0200978 typval_T v;
Bram Moolenaar1dced572012-04-05 16:54:08 +0200979 if (l->lv_lock)
980 luaL_error(L, "list is locked");
Bram Moolenaarb3766472013-04-15 13:49:21 +0200981 lua_settop(L, 2);
Bram Moolenaarca06da92018-07-01 15:12:05 +0200982 luaV_checktypval(L, 2, &v, "adding list item");
Bram Moolenaarb3766472013-04-15 13:49:21 +0200983 if (list_append_tv(l, &v) == FAIL)
Bram Moolenaarca06da92018-07-01 15:12:05 +0200984 luaL_error(L, "failed to add item to list");
Bram Moolenaarb3766472013-04-15 13:49:21 +0200985 clear_tv(&v);
Bram Moolenaar1dced572012-04-05 16:54:08 +0200986 lua_settop(L, 1);
987 return 1;
988}
989
990 static int
Bram Moolenaar4eefe472019-03-19 21:59:19 +0100991luaV_list_insert(lua_State *L)
Bram Moolenaar1dced572012-04-05 16:54:08 +0200992{
993 luaV_List *lis = luaV_checkudata(L, 1, LUAVIM_LIST);
994 list_T *l = (list_T *) luaV_checkcache(L, (void *) *lis);
Bram Moolenaar46538ee2015-02-17 16:28:55 +0100995 long pos = (long) luaL_optinteger(L, 3, 0);
Bram Moolenaar1dced572012-04-05 16:54:08 +0200996 listitem_T *li = NULL;
997 typval_T v;
998 if (l->lv_lock)
999 luaL_error(L, "list is locked");
1000 if (pos < l->lv_len)
1001 {
1002 li = list_find(l, pos);
1003 if (li == NULL)
1004 luaL_error(L, "invalid position");
1005 }
1006 lua_settop(L, 2);
Bram Moolenaarca06da92018-07-01 15:12:05 +02001007 luaV_checktypval(L, 2, &v, "inserting list item");
Bram Moolenaarb3766472013-04-15 13:49:21 +02001008 if (list_insert_tv(l, &v, li) == FAIL)
Bram Moolenaarca06da92018-07-01 15:12:05 +02001009 luaL_error(L, "failed to add item to list");
Bram Moolenaarb3766472013-04-15 13:49:21 +02001010 clear_tv(&v);
Bram Moolenaar1dced572012-04-05 16:54:08 +02001011 lua_settop(L, 1);
1012 return 1;
1013}
1014
1015static const luaL_Reg luaV_List_mt[] = {
1016 {"__tostring", luaV_list_tostring},
Bram Moolenaar1dced572012-04-05 16:54:08 +02001017 {"__len", luaV_list_len},
1018 {"__call", luaV_list_call},
1019 {"__index", luaV_list_index},
1020 {"__newindex", luaV_list_newindex},
1021 {"add", luaV_list_add},
1022 {"insert", luaV_list_insert},
1023 {NULL, NULL}
1024};
1025
1026
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001027// ======= Dict type =======
Bram Moolenaar1dced572012-04-05 16:54:08 +02001028
1029 static luaV_Dict *
Bram Moolenaar4eefe472019-03-19 21:59:19 +01001030luaV_newdict(lua_State *L, dict_T *dic)
Bram Moolenaar1dced572012-04-05 16:54:08 +02001031{
1032 luaV_Dict *d = (luaV_Dict *) lua_newuserdata(L, sizeof(luaV_Dict));
1033 *d = dic;
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001034 dic->dv_refcount++; // reference in Lua
1035 luaV_setudata(L, dic); // cache[dic] = udata
Bram Moolenaar1dced572012-04-05 16:54:08 +02001036 luaV_getfield(L, LUAVIM_DICT);
1037 lua_setmetatable(L, -2);
1038 return d;
1039}
1040
1041luaV_pushtype(dict_T, dict, luaV_Dict)
1042luaV_type_tostring(dict, LUAVIM_DICT)
1043
1044 static int
Bram Moolenaar4eefe472019-03-19 21:59:19 +01001045luaV_dict_len(lua_State *L)
Bram Moolenaar1dced572012-04-05 16:54:08 +02001046{
1047 dict_T *d = luaV_unbox(L, luaV_Dict, 1);
Bram Moolenaarb7828692019-03-23 13:57:02 +01001048 lua_pushinteger(L, (int) dict_len(d));
Bram Moolenaar1dced572012-04-05 16:54:08 +02001049 return 1;
1050}
1051
1052 static int
Bram Moolenaar4eefe472019-03-19 21:59:19 +01001053luaV_dict_iter(lua_State *L UNUSED)
Bram Moolenaar1dced572012-04-05 16:54:08 +02001054{
Bram Moolenaarfeeaa682013-02-14 22:19:51 +01001055#ifdef FEAT_EVAL
Bram Moolenaar1dced572012-04-05 16:54:08 +02001056 hashitem_T *hi = (hashitem_T *) lua_touserdata(L, lua_upvalueindex(2));
1057 int n = lua_tointeger(L, lua_upvalueindex(3));
1058 dictitem_T *di;
1059 if (n <= 0) return 0;
1060 while (HASHITEM_EMPTY(hi)) hi++;
1061 di = dict_lookup(hi);
1062 lua_pushstring(L, (char *) hi->hi_key);
1063 luaV_pushtypval(L, &di->di_tv);
1064 lua_pushlightuserdata(L, (void *) (hi + 1));
1065 lua_replace(L, lua_upvalueindex(2));
1066 lua_pushinteger(L, n - 1);
1067 lua_replace(L, lua_upvalueindex(3));
1068 return 2;
Bram Moolenaarfeeaa682013-02-14 22:19:51 +01001069#else
1070 return 0;
1071#endif
Bram Moolenaar1dced572012-04-05 16:54:08 +02001072}
1073
1074 static int
Bram Moolenaar4eefe472019-03-19 21:59:19 +01001075luaV_dict_call(lua_State *L)
Bram Moolenaar1dced572012-04-05 16:54:08 +02001076{
1077 dict_T *d = luaV_unbox(L, luaV_Dict, 1);
1078 hashtab_T *ht = &d->dv_hashtab;
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001079 lua_pushvalue(L, lua_upvalueindex(1)); // pass cache table along
Bram Moolenaar1dced572012-04-05 16:54:08 +02001080 lua_pushlightuserdata(L, (void *) ht->ht_array);
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001081 lua_pushinteger(L, ht->ht_used); // # remaining items
Bram Moolenaar1dced572012-04-05 16:54:08 +02001082 lua_pushcclosure(L, luaV_dict_iter, 3);
1083 return 1;
1084}
1085
1086 static int
Bram Moolenaarca06da92018-07-01 15:12:05 +02001087luaV_dict_index(lua_State *L)
Bram Moolenaar1dced572012-04-05 16:54:08 +02001088{
1089 dict_T *d = luaV_unbox(L, luaV_Dict, 1);
1090 char_u *key = (char_u *) luaL_checkstring(L, 2);
1091 dictitem_T *di = dict_find(d, key, -1);
Bram Moolenaarca06da92018-07-01 15:12:05 +02001092
Bram Moolenaar1dced572012-04-05 16:54:08 +02001093 if (di == NULL)
1094 lua_pushnil(L);
1095 else
Bram Moolenaarca06da92018-07-01 15:12:05 +02001096 {
Bram Moolenaar1dced572012-04-05 16:54:08 +02001097 luaV_pushtypval(L, &di->di_tv);
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001098 if (di->di_tv.v_type == VAR_FUNC) // funcref?
Bram Moolenaarca06da92018-07-01 15:12:05 +02001099 {
1100 luaV_Funcref *f = (luaV_Funcref *) lua_touserdata(L, -1);
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001101 f->self = d; // keep "self" reference
Bram Moolenaarca06da92018-07-01 15:12:05 +02001102 d->dv_refcount++;
1103 }
1104 }
Bram Moolenaar1dced572012-04-05 16:54:08 +02001105 return 1;
1106}
1107
1108 static int
Bram Moolenaarca06da92018-07-01 15:12:05 +02001109luaV_dict_newindex(lua_State *L)
Bram Moolenaar1dced572012-04-05 16:54:08 +02001110{
1111 dict_T *d = luaV_unbox(L, luaV_Dict, 1);
1112 char_u *key = (char_u *) luaL_checkstring(L, 2);
1113 dictitem_T *di;
Bram Moolenaare49b8e82020-07-01 13:52:55 +02001114 typval_T tv;
Bram Moolenaar713bf9e92019-03-16 16:38:41 +01001115
Bram Moolenaar1dced572012-04-05 16:54:08 +02001116 if (d->dv_lock)
1117 luaL_error(L, "dict is locked");
Bram Moolenaard6ef5f92018-07-13 22:08:23 +02001118 if (key == NULL)
1119 return 0;
1120 if (*key == NUL)
Bram Moolenaarca06da92018-07-01 15:12:05 +02001121 luaL_error(L, "empty key");
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001122 if (!lua_isnil(L, 3)) // read value?
Bram Moolenaar17413672018-07-14 20:49:42 +02001123 {
Bram Moolenaare49b8e82020-07-01 13:52:55 +02001124 luaV_checktypval(L, 3, &tv, "setting dict item");
1125 if (d->dv_scope == VAR_DEF_SCOPE && tv.v_type == VAR_FUNC)
1126 {
1127 clear_tv(&tv);
Bram Moolenaarca06da92018-07-01 15:12:05 +02001128 luaL_error(L, "cannot assign funcref to builtin scope");
Bram Moolenaare49b8e82020-07-01 13:52:55 +02001129 }
Bram Moolenaarca06da92018-07-01 15:12:05 +02001130 }
Bram Moolenaar1dced572012-04-05 16:54:08 +02001131 di = dict_find(d, key, -1);
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001132 if (di == NULL) // non-existing key?
Bram Moolenaar1dced572012-04-05 16:54:08 +02001133 {
Bram Moolenaard6ef5f92018-07-13 22:08:23 +02001134 if (lua_isnil(L, 3))
1135 return 0;
Bram Moolenaar1dced572012-04-05 16:54:08 +02001136 di = dictitem_alloc(key);
Bram Moolenaard6ef5f92018-07-13 22:08:23 +02001137 if (di == NULL)
Bram Moolenaare49b8e82020-07-01 13:52:55 +02001138 {
1139 clear_tv(&tv);
Bram Moolenaard6ef5f92018-07-13 22:08:23 +02001140 return 0;
Bram Moolenaare49b8e82020-07-01 13:52:55 +02001141 }
Bram Moolenaar1dced572012-04-05 16:54:08 +02001142 if (dict_add(d, di) == FAIL)
1143 {
Bram Moolenaard6ef5f92018-07-13 22:08:23 +02001144 vim_free(di);
Bram Moolenaare49b8e82020-07-01 13:52:55 +02001145 clear_tv(&tv);
Bram Moolenaard6ef5f92018-07-13 22:08:23 +02001146 return 0;
Bram Moolenaar1dced572012-04-05 16:54:08 +02001147 }
1148 }
1149 else
1150 clear_tv(&di->di_tv);
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001151 if (lua_isnil(L, 3)) // remove?
Bram Moolenaar1dced572012-04-05 16:54:08 +02001152 {
1153 hashitem_T *hi = hash_find(&d->dv_hashtab, di->di_key);
1154 hash_remove(&d->dv_hashtab, hi);
1155 dictitem_free(di);
1156 }
Bram Moolenaarca06da92018-07-01 15:12:05 +02001157 else
Bram Moolenaare49b8e82020-07-01 13:52:55 +02001158 di->di_tv = tv;
Bram Moolenaar1dced572012-04-05 16:54:08 +02001159 return 0;
1160}
1161
1162static const luaL_Reg luaV_Dict_mt[] = {
1163 {"__tostring", luaV_dict_tostring},
Bram Moolenaar1dced572012-04-05 16:54:08 +02001164 {"__len", luaV_dict_len},
1165 {"__call", luaV_dict_call},
1166 {"__index", luaV_dict_index},
1167 {"__newindex", luaV_dict_newindex},
1168 {NULL, NULL}
1169};
1170
1171
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001172// ======= Blob type =======
Bram Moolenaarb7828692019-03-23 13:57:02 +01001173
1174 static luaV_Blob *
1175luaV_newblob(lua_State *L, blob_T *blo)
1176{
1177 luaV_Blob *b = (luaV_Blob *) lua_newuserdata(L, sizeof(luaV_Blob));
1178 *b = blo;
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001179 blo->bv_refcount++; // reference in Lua
1180 luaV_setudata(L, blo); // cache[blo] = udata
Bram Moolenaarb7828692019-03-23 13:57:02 +01001181 luaV_getfield(L, LUAVIM_BLOB);
1182 lua_setmetatable(L, -2);
1183 return b;
1184}
1185
1186luaV_pushtype(blob_T, blob, luaV_Blob)
1187luaV_type_tostring(blob, LUAVIM_BLOB)
1188
1189 static int
1190luaV_blob_gc(lua_State *L)
1191{
1192 blob_T *b = luaV_unbox(L, luaV_Blob, 1);
1193 blob_unref(b);
1194 return 0;
1195}
1196
1197 static int
1198luaV_blob_len(lua_State *L)
1199{
1200 blob_T *b = luaV_unbox(L, luaV_Blob, 1);
1201 lua_pushinteger(L, (int) blob_len(b));
1202 return 1;
1203}
1204
1205 static int
1206luaV_blob_index(lua_State *L)
1207{
1208 blob_T *b = luaV_unbox(L, luaV_Blob, 1);
1209 if (lua_isnumber(L, 2))
1210 {
1211 int idx = luaL_checkinteger(L, 2);
1212 if (idx < blob_len(b))
1213 lua_pushnumber(L, (lua_Number) blob_get(b, idx));
1214 else
1215 lua_pushnil(L);
1216 }
1217 else if (lua_isstring(L, 2))
1218 {
1219 const char *s = lua_tostring(L, 2);
1220 if (strncmp(s, "add", 3) == 0)
1221 {
1222 lua_getmetatable(L, 1);
1223 lua_getfield(L, -1, s);
1224 }
1225 else
1226 lua_pushnil(L);
1227 }
1228 else
1229 lua_pushnil(L);
1230 return 1;
1231}
1232
1233 static int
1234luaV_blob_newindex(lua_State *L)
1235{
1236 blob_T *b = luaV_unbox(L, luaV_Blob, 1);
1237 if (b->bv_lock)
1238 luaL_error(L, "blob is locked");
1239 if (lua_isnumber(L, 2))
1240 {
1241 long len = blob_len(b);
1242 int idx = luaL_checkinteger(L, 2);
1243 int val = luaL_checkinteger(L, 3);
1244 if (idx < len || (idx == len && ga_grow(&b->bv_ga, 1) == OK))
1245 {
1246 blob_set(b, idx, (char_u) val);
1247 if (idx == len)
1248 ++b->bv_ga.ga_len;
1249 }
1250 else
1251 luaL_error(L, "index out of range");
1252 }
1253 return 0;
1254}
1255
1256 static int
1257luaV_blob_add(lua_State *L)
1258{
1259 luaV_Blob *blo = luaV_checkudata(L, 1, LUAVIM_BLOB);
1260 blob_T *b = (blob_T *) luaV_checkcache(L, (void *) *blo);
1261 if (b->bv_lock)
1262 luaL_error(L, "blob is locked");
1263 lua_settop(L, 2);
1264 if (!lua_isstring(L, 2))
1265 luaL_error(L, "string expected, got %s", luaL_typename(L, 2));
1266 else
1267 {
1268 size_t i, l = 0;
1269 const char *s = lua_tolstring(L, 2, &l);
1270
Bram Moolenaar5f1d3ae2020-02-11 22:37:35 +01001271 if (ga_grow(&b->bv_ga, (int)l) == OK)
Bram Moolenaar6fb5c972019-03-26 21:44:20 +01001272 for (i = 0; i < l; ++i)
1273 ga_append(&b->bv_ga, s[i]);
Bram Moolenaarb7828692019-03-23 13:57:02 +01001274 }
1275 lua_settop(L, 1);
1276 return 1;
1277}
1278
1279static const luaL_Reg luaV_Blob_mt[] = {
1280 {"__tostring", luaV_blob_tostring},
1281 {"__gc", luaV_blob_gc},
1282 {"__len", luaV_blob_len},
1283 {"__index", luaV_blob_index},
1284 {"__newindex", luaV_blob_newindex},
1285 {"add", luaV_blob_add},
1286 {NULL, NULL}
1287};
1288
1289
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001290// ======= Funcref type =======
Bram Moolenaarca06da92018-07-01 15:12:05 +02001291
1292 static luaV_Funcref *
1293luaV_newfuncref(lua_State *L, char_u *name)
1294{
1295 luaV_Funcref *f = (luaV_Funcref *)lua_newuserdata(L, sizeof(luaV_Funcref));
1296
1297 if (name != NULL)
1298 {
Bram Moolenaar4eefe472019-03-19 21:59:19 +01001299 func_ref(name);
1300 f->name = vim_strsave(name);
Bram Moolenaarca06da92018-07-01 15:12:05 +02001301 }
Bram Moolenaarca06da92018-07-01 15:12:05 +02001302 f->self = NULL;
1303 luaV_getfield(L, LUAVIM_FUNCREF);
1304 lua_setmetatable(L, -2);
1305 return f;
1306}
1307
1308 static luaV_Funcref *
Bram Moolenaar4eefe472019-03-19 21:59:19 +01001309luaV_pushfuncref(lua_State *L, char_u *name)
Bram Moolenaarca06da92018-07-01 15:12:05 +02001310{
Bram Moolenaar4eefe472019-03-19 21:59:19 +01001311 return luaV_newfuncref(L, name);
Bram Moolenaarca06da92018-07-01 15:12:05 +02001312}
1313
1314
1315luaV_type_tostring(funcref, LUAVIM_FUNCREF)
1316
1317 static int
1318luaV_funcref_gc(lua_State *L)
1319{
1320 luaV_Funcref *f = (luaV_Funcref *) lua_touserdata(L, 1);
1321
Bram Moolenaar4eefe472019-03-19 21:59:19 +01001322 func_unref(f->name);
1323 vim_free(f->name);
1324 // NOTE: Don't call "dict_unref(f->self)", because the dict of "f->self"
1325 // will be (or has been already) freed by Vim's garbage collection.
Bram Moolenaarca06da92018-07-01 15:12:05 +02001326 return 0;
1327}
1328
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001329// equivalent to string(funcref)
Bram Moolenaarca06da92018-07-01 15:12:05 +02001330 static int
1331luaV_funcref_len(lua_State *L)
1332{
1333 luaV_Funcref *f = (luaV_Funcref *) lua_touserdata(L, 1);
1334
Bram Moolenaar4eefe472019-03-19 21:59:19 +01001335 lua_pushstring(L, (const char *) f->name);
Bram Moolenaarca06da92018-07-01 15:12:05 +02001336 return 1;
1337}
1338
1339 static int
1340luaV_funcref_call(lua_State *L)
1341{
1342 luaV_Funcref *f = (luaV_Funcref *) lua_touserdata(L, 1);
Bram Moolenaar4eefe472019-03-19 21:59:19 +01001343 int i, n = lua_gettop(L) - 1; // #args
1344 int status = FAIL;
1345 typval_T args;
1346 typval_T rettv;
Bram Moolenaarca06da92018-07-01 15:12:05 +02001347
Bram Moolenaar4eefe472019-03-19 21:59:19 +01001348 args.v_type = VAR_LIST;
1349 args.vval.v_list = list_alloc();
1350 rettv.v_type = VAR_UNKNOWN; // as in clear_tv
1351 if (args.vval.v_list != NULL)
Bram Moolenaard6ef5f92018-07-13 22:08:23 +02001352 {
Bram Moolenaar4eefe472019-03-19 21:59:19 +01001353 typval_T v;
1354
Bram Moolenaar17413672018-07-14 20:49:42 +02001355 for (i = 0; i < n; i++)
1356 {
Bram Moolenaard6ef5f92018-07-13 22:08:23 +02001357 luaV_checktypval(L, i + 2, &v, "calling funcref");
Bram Moolenaar4eefe472019-03-19 21:59:19 +01001358 list_append_tv(args.vval.v_list, &v);
Bram Moolenaar713bf9e92019-03-16 16:38:41 +01001359 clear_tv(&v);
Bram Moolenaard6ef5f92018-07-13 22:08:23 +02001360 }
Bram Moolenaar4eefe472019-03-19 21:59:19 +01001361 status = func_call(f->name, &args, NULL, f->self, &rettv);
Bram Moolenaard6ef5f92018-07-13 22:08:23 +02001362 if (status == OK)
1363 luaV_pushtypval(L, &rettv);
Bram Moolenaar4eefe472019-03-19 21:59:19 +01001364 clear_tv(&args);
Bram Moolenaard6ef5f92018-07-13 22:08:23 +02001365 clear_tv(&rettv);
Bram Moolenaarca06da92018-07-01 15:12:05 +02001366 }
Bram Moolenaarca06da92018-07-01 15:12:05 +02001367 if (status != OK)
1368 luaL_error(L, "cannot call funcref");
1369 return 1;
1370}
1371
1372static const luaL_Reg luaV_Funcref_mt[] = {
1373 {"__tostring", luaV_funcref_tostring},
1374 {"__gc", luaV_funcref_gc},
1375 {"__len", luaV_funcref_len},
1376 {"__call", luaV_funcref_call},
1377 {NULL, NULL}
1378};
1379
1380
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001381// ======= Buffer type =======
Bram Moolenaar1dced572012-04-05 16:54:08 +02001382
1383luaV_newtype(buf_T, buffer, luaV_Buffer, LUAVIM_BUFFER)
1384luaV_pushtype(buf_T, buffer, luaV_Buffer)
1385luaV_type_tostring(buffer, LUAVIM_BUFFER)
1386
1387 static int
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001388luaV_buffer_len(lua_State *L)
1389{
Bram Moolenaar1dced572012-04-05 16:54:08 +02001390 buf_T *b = (buf_T *) luaV_checkvalid(L, luaV_Buffer, 1);
1391 lua_pushinteger(L, b->b_ml.ml_line_count);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001392 return 1;
1393}
1394
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001395 static int
1396luaV_buffer_call(lua_State *L)
1397{
Bram Moolenaar1dced572012-04-05 16:54:08 +02001398 buf_T *b = (buf_T *) luaV_checkvalid(L, luaV_Buffer, 1);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001399 lua_settop(L, 1);
Bram Moolenaar1dced572012-04-05 16:54:08 +02001400 set_curbuf(b, DOBUF_SPLIT);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001401 return 1;
1402}
1403
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001404 static int
1405luaV_buffer_index(lua_State *L)
1406{
Bram Moolenaar1dced572012-04-05 16:54:08 +02001407 buf_T *b = (buf_T *) luaV_checkvalid(L, luaV_Buffer, 1);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001408 linenr_T n = (linenr_T) lua_tointeger(L, 2);
Bram Moolenaar1dced572012-04-05 16:54:08 +02001409 if (n > 0 && n <= b->b_ml.ml_line_count)
1410 luaV_pushline(L, b, n);
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001411 else if (lua_isstring(L, 2))
1412 {
1413 const char *s = lua_tostring(L, 2);
1414 if (strncmp(s, "name", 4) == 0)
Bram Moolenaarfe08df42018-07-07 23:07:41 +02001415 lua_pushstring(L, (b->b_sfname == NULL)
1416 ? "" : (char *) b->b_sfname);
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001417 else if (strncmp(s, "fname", 5) == 0)
Bram Moolenaarfe08df42018-07-07 23:07:41 +02001418 lua_pushstring(L, (b->b_ffname == NULL)
1419 ? "" : (char *) b->b_ffname);
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001420 else if (strncmp(s, "number", 6) == 0)
Bram Moolenaar1dced572012-04-05 16:54:08 +02001421 lua_pushinteger(L, b->b_fnum);
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001422 // methods
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001423 else if (strncmp(s, "insert", 6) == 0
1424 || strncmp(s, "next", 4) == 0
1425 || strncmp(s, "previous", 8) == 0
1426 || strncmp(s, "isvalid", 7) == 0)
1427 {
1428 lua_getmetatable(L, 1);
1429 lua_getfield(L, -1, s);
1430 }
1431 else
1432 lua_pushnil(L);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001433 }
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001434 else
1435 lua_pushnil(L);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001436 return 1;
1437}
1438
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001439 static int
1440luaV_buffer_newindex(lua_State *L)
1441{
Bram Moolenaar1dced572012-04-05 16:54:08 +02001442 buf_T *b = (buf_T *) luaV_checkvalid(L, luaV_Buffer, 1);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001443 linenr_T n = (linenr_T) luaL_checkinteger(L, 2);
1444#ifdef HAVE_SANDBOX
1445 luaV_checksandbox(L);
1446#endif
Bram Moolenaar1dced572012-04-05 16:54:08 +02001447 if (n < 1 || n > b->b_ml.ml_line_count)
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001448 luaL_error(L, "invalid line number");
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001449 if (lua_isnil(L, 3)) // delete line
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001450 {
1451 buf_T *buf = curbuf;
Bram Moolenaar1dced572012-04-05 16:54:08 +02001452 curbuf = b;
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001453 if (u_savedel(n, 1L) == FAIL)
1454 {
1455 curbuf = buf;
1456 luaL_error(L, "cannot save undo information");
1457 }
Bram Moolenaarca70c072020-05-30 20:30:46 +02001458 else if (ml_delete(n) == FAIL)
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001459 {
1460 curbuf = buf;
1461 luaL_error(L, "cannot delete line");
1462 }
Bram Moolenaarca06da92018-07-01 15:12:05 +02001463 else
1464 {
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001465 deleted_lines_mark(n, 1L);
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001466 if (b == curwin->w_buffer) // fix cursor in current window?
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001467 {
1468 if (curwin->w_cursor.lnum >= n)
1469 {
1470 if (curwin->w_cursor.lnum > n)
1471 {
1472 curwin->w_cursor.lnum -= 1;
1473 check_cursor_col();
1474 }
Bram Moolenaare49b8e82020-07-01 13:52:55 +02001475 else
1476 check_cursor();
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001477 changed_cline_bef_curs();
1478 }
1479 invalidate_botline();
1480 }
1481 }
1482 curbuf = buf;
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001483 }
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001484 else if (lua_isstring(L, 3)) // update line
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001485 {
1486 buf_T *buf = curbuf;
Bram Moolenaar1dced572012-04-05 16:54:08 +02001487 curbuf = b;
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001488 if (u_savesub(n) == FAIL)
1489 {
1490 curbuf = buf;
1491 luaL_error(L, "cannot save undo information");
1492 }
1493 else if (ml_replace(n, luaV_toline(L, 3), TRUE) == FAIL)
1494 {
1495 curbuf = buf;
1496 luaL_error(L, "cannot replace line");
1497 }
1498 else changed_bytes(n, 0);
1499 curbuf = buf;
Bram Moolenaar1dced572012-04-05 16:54:08 +02001500 if (b == curwin->w_buffer)
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001501 check_cursor_col();
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001502 }
1503 else
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001504 luaL_error(L, "wrong argument to change line");
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001505 return 0;
1506}
1507
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001508 static int
1509luaV_buffer_insert(lua_State *L)
1510{
Bram Moolenaar1dced572012-04-05 16:54:08 +02001511 luaV_Buffer *lb = luaV_checkudata(L, 1, LUAVIM_BUFFER);
1512 buf_T *b = (buf_T *) luaV_checkcache(L, (void *) *lb);
1513 linenr_T last = b->b_ml.ml_line_count;
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001514 linenr_T n = (linenr_T) luaL_optinteger(L, 3, last);
1515 buf_T *buf;
1516 luaL_checktype(L, 2, LUA_TSTRING);
1517#ifdef HAVE_SANDBOX
1518 luaV_checksandbox(L);
1519#endif
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001520 // fix insertion line
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001521 if (n < 0) n = 0;
1522 if (n > last) n = last;
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001523 // insert
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001524 buf = curbuf;
Bram Moolenaar1dced572012-04-05 16:54:08 +02001525 curbuf = b;
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001526 if (u_save(n, n + 1) == FAIL)
1527 {
1528 curbuf = buf;
1529 luaL_error(L, "cannot save undo information");
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001530 }
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001531 else if (ml_append(n, luaV_toline(L, 2), 0, FALSE) == FAIL)
1532 {
1533 curbuf = buf;
1534 luaL_error(L, "cannot insert line");
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001535 }
1536 else
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001537 appended_lines_mark(n, 1L);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001538 curbuf = buf;
1539 update_screen(VALID);
1540 return 0;
1541}
1542
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001543 static int
1544luaV_buffer_next(lua_State *L)
1545{
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001546 luaV_Buffer *b = luaV_checkudata(L, 1, LUAVIM_BUFFER);
Bram Moolenaar1dced572012-04-05 16:54:08 +02001547 buf_T *buf = (buf_T *) luaV_checkcache(L, (void *) *b);
1548 luaV_pushbuffer(L, buf->b_next);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001549 return 1;
1550}
1551
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001552 static int
1553luaV_buffer_previous(lua_State *L)
1554{
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001555 luaV_Buffer *b = luaV_checkudata(L, 1, LUAVIM_BUFFER);
Bram Moolenaar1dced572012-04-05 16:54:08 +02001556 buf_T *buf = (buf_T *) luaV_checkcache(L, (void *) *b);
1557 luaV_pushbuffer(L, buf->b_prev);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001558 return 1;
1559}
1560
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001561 static int
1562luaV_buffer_isvalid(lua_State *L)
1563{
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001564 luaV_Buffer *b = luaV_checkudata(L, 1, LUAVIM_BUFFER);
Bram Moolenaar1dced572012-04-05 16:54:08 +02001565 luaV_getudata(L, *b);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001566 lua_pushboolean(L, !lua_isnil(L, -1));
1567 return 1;
1568}
1569
1570static const luaL_Reg luaV_Buffer_mt[] = {
1571 {"__tostring", luaV_buffer_tostring},
1572 {"__len", luaV_buffer_len},
1573 {"__call", luaV_buffer_call},
1574 {"__index", luaV_buffer_index},
1575 {"__newindex", luaV_buffer_newindex},
1576 {"insert", luaV_buffer_insert},
1577 {"next", luaV_buffer_next},
1578 {"previous", luaV_buffer_previous},
1579 {"isvalid", luaV_buffer_isvalid},
1580 {NULL, NULL}
1581};
1582
1583
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001584// ======= Window type =======
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001585
Bram Moolenaar1dced572012-04-05 16:54:08 +02001586luaV_newtype(win_T, window, luaV_Window, LUAVIM_WINDOW)
1587luaV_pushtype(win_T, window, luaV_Window)
1588luaV_type_tostring(window, LUAVIM_WINDOW)
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001589
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001590 static int
1591luaV_window_call(lua_State *L)
1592{
Bram Moolenaar1dced572012-04-05 16:54:08 +02001593 win_T *w = (win_T *) luaV_checkvalid(L, luaV_Window, 1);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001594 lua_settop(L, 1);
Bram Moolenaar1dced572012-04-05 16:54:08 +02001595 win_goto(w);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001596 return 1;
1597}
1598
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001599 static int
1600luaV_window_index(lua_State *L)
1601{
Bram Moolenaar1dced572012-04-05 16:54:08 +02001602 win_T *w = (win_T *) luaV_checkvalid(L, luaV_Window, 1);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001603 const char *s = luaL_checkstring(L, 2);
1604 if (strncmp(s, "buffer", 6) == 0)
Bram Moolenaar1dced572012-04-05 16:54:08 +02001605 luaV_pushbuffer(L, w->w_buffer);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001606 else if (strncmp(s, "line", 4) == 0)
Bram Moolenaar1dced572012-04-05 16:54:08 +02001607 lua_pushinteger(L, w->w_cursor.lnum);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001608 else if (strncmp(s, "col", 3) == 0)
Bram Moolenaar1dced572012-04-05 16:54:08 +02001609 lua_pushinteger(L, w->w_cursor.col + 1);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001610 else if (strncmp(s, "width", 5) == 0)
Bram Moolenaar02631462017-09-22 15:20:32 +02001611 lua_pushinteger(L, w->w_width);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001612 else if (strncmp(s, "height", 6) == 0)
Bram Moolenaar1dced572012-04-05 16:54:08 +02001613 lua_pushinteger(L, w->w_height);
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001614 // methods
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001615 else if (strncmp(s, "next", 4) == 0
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001616 || strncmp(s, "previous", 8) == 0
1617 || strncmp(s, "isvalid", 7) == 0)
1618 {
1619 lua_getmetatable(L, 1);
1620 lua_getfield(L, -1, s);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001621 }
1622 else
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001623 lua_pushnil(L);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001624 return 1;
1625}
1626
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001627 static int
Bram Moolenaar4eefe472019-03-19 21:59:19 +01001628luaV_window_newindex(lua_State *L)
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001629{
Bram Moolenaar1dced572012-04-05 16:54:08 +02001630 win_T *w = (win_T *) luaV_checkvalid(L, luaV_Window, 1);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001631 const char *s = luaL_checkstring(L, 2);
1632 int v = luaL_checkinteger(L, 3);
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001633 if (strncmp(s, "line", 4) == 0)
1634 {
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001635#ifdef HAVE_SANDBOX
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001636 luaV_checksandbox(L);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001637#endif
Bram Moolenaar1dced572012-04-05 16:54:08 +02001638 if (v < 1 || v > w->w_buffer->b_ml.ml_line_count)
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001639 luaL_error(L, "line out of range");
Bram Moolenaar1dced572012-04-05 16:54:08 +02001640 w->w_cursor.lnum = v;
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001641 update_screen(VALID);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001642 }
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001643 else if (strncmp(s, "col", 3) == 0)
1644 {
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001645#ifdef HAVE_SANDBOX
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001646 luaV_checksandbox(L);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001647#endif
Bram Moolenaar1dced572012-04-05 16:54:08 +02001648 w->w_cursor.col = v - 1;
Bram Moolenaar53901442018-07-25 22:02:36 +02001649 w->w_set_curswant = TRUE;
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001650 update_screen(VALID);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001651 }
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001652 else if (strncmp(s, "width", 5) == 0)
1653 {
1654 win_T *win = curwin;
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001655#ifdef FEAT_GUI
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001656 need_mouse_correct = TRUE;
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001657#endif
Bram Moolenaar1dced572012-04-05 16:54:08 +02001658 curwin = w;
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001659 win_setwidth(v);
1660 curwin = win;
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001661 }
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001662 else if (strncmp(s, "height", 6) == 0)
1663 {
1664 win_T *win = curwin;
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001665#ifdef FEAT_GUI
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001666 need_mouse_correct = TRUE;
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001667#endif
Bram Moolenaar1dced572012-04-05 16:54:08 +02001668 curwin = w;
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001669 win_setheight(v);
1670 curwin = win;
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001671 }
1672 else
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001673 luaL_error(L, "invalid window property: `%s'", s);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001674 return 0;
1675}
1676
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001677 static int
1678luaV_window_next(lua_State *L)
1679{
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001680 luaV_Window *w = luaV_checkudata(L, 1, LUAVIM_WINDOW);
Bram Moolenaar1dced572012-04-05 16:54:08 +02001681 win_T *win = (win_T *) luaV_checkcache(L, (void *) *w);
1682 luaV_pushwindow(L, win->w_next);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001683 return 1;
1684}
1685
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001686 static int
1687luaV_window_previous(lua_State *L)
1688{
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001689 luaV_Window *w = luaV_checkudata(L, 1, LUAVIM_WINDOW);
Bram Moolenaar1dced572012-04-05 16:54:08 +02001690 win_T *win = (win_T *) luaV_checkcache(L, (void *) *w);
1691 luaV_pushwindow(L, win->w_prev);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001692 return 1;
1693}
1694
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001695 static int
1696luaV_window_isvalid(lua_State *L)
1697{
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001698 luaV_Window *w = luaV_checkudata(L, 1, LUAVIM_WINDOW);
Bram Moolenaar1dced572012-04-05 16:54:08 +02001699 luaV_getudata(L, *w);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001700 lua_pushboolean(L, !lua_isnil(L, -1));
1701 return 1;
1702}
1703
1704static const luaL_Reg luaV_Window_mt[] = {
1705 {"__tostring", luaV_window_tostring},
1706 {"__call", luaV_window_call},
1707 {"__index", luaV_window_index},
1708 {"__newindex", luaV_window_newindex},
1709 {"next", luaV_window_next},
1710 {"previous", luaV_window_previous},
1711 {"isvalid", luaV_window_isvalid},
1712 {NULL, NULL}
1713};
1714
1715
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001716// ======= Vim module =======
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001717
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001718 static int
1719luaV_print(lua_State *L)
1720{
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001721 int i, n = lua_gettop(L); // nargs
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001722 const char *s;
1723 size_t l;
1724 luaL_Buffer b;
1725 luaL_buffinit(L, &b);
1726 lua_getglobal(L, "tostring");
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001727 for (i = 1; i <= n; i++)
1728 {
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001729 lua_pushvalue(L, -1); // tostring
1730 lua_pushvalue(L, i); // arg
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001731 lua_call(L, 1, 1);
1732 s = lua_tolstring(L, -1, &l);
1733 if (s == NULL)
1734 return luaL_error(L, "cannot convert to string");
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001735 if (i > 1) luaL_addchar(&b, ' '); // use space instead of tab
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001736 luaV_addlstring(&b, s, l, 0);
1737 lua_pop(L, 1);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001738 }
1739 luaL_pushresult(&b);
Bram Moolenaarb98678a2019-10-19 15:18:44 +02001740 if (!got_int)
1741 luaV_msg(L);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001742 return 0;
1743}
1744
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001745 static int
Bram Moolenaar38e2b062011-09-21 17:15:39 +02001746luaV_debug(lua_State *L)
1747{
1748 lua_settop(L, 0);
1749 lua_getglobal(L, "vim");
1750 lua_getfield(L, -1, "eval");
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001751 lua_remove(L, -2); // vim.eval at position 1
Bram Moolenaar38e2b062011-09-21 17:15:39 +02001752 for (;;)
1753 {
1754 const char *input;
1755 size_t l;
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001756 lua_pushvalue(L, 1); // vim.eval
Bram Moolenaar38e2b062011-09-21 17:15:39 +02001757 lua_pushliteral(L, "input('lua_debug> ')");
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001758 lua_call(L, 1, 1); // return string
Bram Moolenaar38e2b062011-09-21 17:15:39 +02001759 input = lua_tolstring(L, -1, &l);
1760 if (l == 0 || strcmp(input, "cont") == 0)
1761 return 0;
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001762 msg_putchar('\n'); // avoid outputting on input line
Bram Moolenaar38e2b062011-09-21 17:15:39 +02001763 if (luaL_loadbuffer(L, input, l, "=(debug command)")
1764 || lua_pcall(L, 0, 0, 0))
1765 luaV_emsg(L);
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001766 lua_settop(L, 1); // remove eventual returns, but keep vim.eval
Bram Moolenaar38e2b062011-09-21 17:15:39 +02001767 }
1768}
1769
1770 static int
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001771luaV_command(lua_State *L)
1772{
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001773 do_cmdline_cmd((char_u *) luaL_checkstring(L, 1));
1774 update_screen(VALID);
1775 return 0;
1776}
1777
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001778 static int
1779luaV_eval(lua_State *L)
1780{
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001781 typval_T *tv = eval_expr((char_u *) luaL_checkstring(L, 1), NULL);
1782 if (tv == NULL) luaL_error(L, "invalid expression");
1783 luaV_pushtypval(L, tv);
Bram Moolenaarb3766472013-04-15 13:49:21 +02001784 free_tv(tv);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001785 return 1;
1786}
1787
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001788 static int
Bram Moolenaar0d2e4fc2010-07-18 12:35:47 +02001789luaV_beep(lua_State *L UNUSED)
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001790{
Bram Moolenaar165bc692015-07-21 17:53:25 +02001791 vim_beep(BO_LANG);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001792 return 0;
1793}
1794
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001795 static int
1796luaV_line(lua_State *L)
1797{
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001798 luaV_pushline(L, curbuf, curwin->w_cursor.lnum);
1799 return 1;
1800}
1801
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001802 static int
Bram Moolenaar1dced572012-04-05 16:54:08 +02001803luaV_list(lua_State *L)
1804{
Bram Moolenaarca06da92018-07-01 15:12:05 +02001805 list_T *l;
1806 int initarg = !lua_isnoneornil(L, 1);
1807
1808 if (initarg && lua_type(L, 1) != LUA_TTABLE)
1809 luaL_error(L, "table expected, got %s", luaL_typename(L, 1));
1810 l = list_alloc();
Bram Moolenaar1dced572012-04-05 16:54:08 +02001811 if (l == NULL)
1812 lua_pushnil(L);
1813 else
Bram Moolenaarca06da92018-07-01 15:12:05 +02001814 {
Bram Moolenaar1dced572012-04-05 16:54:08 +02001815 luaV_newlist(L, l);
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001816 if (initarg) // traverse table to init list
Bram Moolenaar17413672018-07-14 20:49:42 +02001817 {
Bram Moolenaarca06da92018-07-01 15:12:05 +02001818 int notnil, i = 0;
1819 typval_T v;
Bram Moolenaar17413672018-07-14 20:49:42 +02001820 do
1821 {
Bram Moolenaarca06da92018-07-01 15:12:05 +02001822 lua_rawgeti(L, 1, ++i);
1823 notnil = !lua_isnil(L, -1);
Bram Moolenaar17413672018-07-14 20:49:42 +02001824 if (notnil)
1825 {
Bram Moolenaarca06da92018-07-01 15:12:05 +02001826 luaV_checktypval(L, -1, &v, "vim.list");
1827 list_append_tv(l, &v);
Bram Moolenaar713bf9e92019-03-16 16:38:41 +01001828 clear_tv(&v);
Bram Moolenaarca06da92018-07-01 15:12:05 +02001829 }
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001830 lua_pop(L, 1); // value
Bram Moolenaarca06da92018-07-01 15:12:05 +02001831 } while (notnil);
1832 }
1833 }
Bram Moolenaar1dced572012-04-05 16:54:08 +02001834 return 1;
1835}
1836
1837 static int
1838luaV_dict(lua_State *L)
1839{
Bram Moolenaarca06da92018-07-01 15:12:05 +02001840 dict_T *d;
1841 int initarg = !lua_isnoneornil(L, 1);
1842
1843 if (initarg && lua_type(L, 1) != LUA_TTABLE)
1844 luaL_error(L, "table expected, got %s", luaL_typename(L, 1));
1845 d = dict_alloc();
Bram Moolenaar1dced572012-04-05 16:54:08 +02001846 if (d == NULL)
1847 lua_pushnil(L);
1848 else
Bram Moolenaarca06da92018-07-01 15:12:05 +02001849 {
Bram Moolenaar1dced572012-04-05 16:54:08 +02001850 luaV_newdict(L, d);
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001851 if (initarg) // traverse table to init dict
Bram Moolenaarca06da92018-07-01 15:12:05 +02001852 {
1853 lua_pushnil(L);
1854 while (lua_next(L, 1))
1855 {
1856 char_u *key;
1857 dictitem_T *di;
1858 typval_T v;
Bram Moolenaard6ef5f92018-07-13 22:08:23 +02001859
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001860 lua_pushvalue(L, -2); // dup key in case it's a number
Bram Moolenaarca06da92018-07-01 15:12:05 +02001861 key = (char_u *) lua_tostring(L, -1);
Bram Moolenaard6ef5f92018-07-13 22:08:23 +02001862 if (key == NULL)
1863 {
1864 lua_pushnil(L);
1865 return 1;
1866 }
1867 if (*key == NUL)
Bram Moolenaarca06da92018-07-01 15:12:05 +02001868 luaL_error(L, "table has empty key");
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001869 luaV_checktypval(L, -2, &v, "vim.dict"); // value
Bram Moolenaarca06da92018-07-01 15:12:05 +02001870 di = dictitem_alloc(key);
Bram Moolenaard6ef5f92018-07-13 22:08:23 +02001871 if (di == NULL || dict_add(d, di) == FAIL)
1872 {
Bram Moolenaarca06da92018-07-01 15:12:05 +02001873 vim_free(di);
1874 lua_pushnil(L);
1875 return 1;
1876 }
Bram Moolenaare49b8e82020-07-01 13:52:55 +02001877 di->di_tv = v;
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001878 lua_pop(L, 2); // key copy and value
Bram Moolenaarca06da92018-07-01 15:12:05 +02001879 }
1880 }
1881 }
1882 return 1;
1883}
1884
1885 static int
Bram Moolenaarb7828692019-03-23 13:57:02 +01001886luaV_blob(lua_State *L)
1887{
1888 blob_T *b;
1889 int initarg = !lua_isnoneornil(L, 1);
1890
1891 if (initarg && !lua_isstring(L, 1))
1892 luaL_error(L, "string expected, got %s", luaL_typename(L, 1));
1893 b = blob_alloc();
1894 if (b == NULL)
1895 lua_pushnil(L);
1896 else
1897 {
1898 luaV_newblob(L, b);
1899 if (initarg)
1900 {
1901 size_t i, l = 0;
1902 const char *s = lua_tolstring(L, 1, &l);
1903
Bram Moolenaar5f1d3ae2020-02-11 22:37:35 +01001904 if (ga_grow(&b->bv_ga, (int)l) == OK)
Bram Moolenaar6fb5c972019-03-26 21:44:20 +01001905 for (i = 0; i < l; ++i)
1906 ga_append(&b->bv_ga, s[i]);
Bram Moolenaarb7828692019-03-23 13:57:02 +01001907 }
1908 }
1909 return 1;
1910}
1911
1912 static int
Bram Moolenaarca06da92018-07-01 15:12:05 +02001913luaV_funcref(lua_State *L)
1914{
1915 const char *name = luaL_checkstring(L, 1);
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001916 // note: not checking if function exists (needs function_exists)
Bram Moolenaarca06da92018-07-01 15:12:05 +02001917 if (name == NULL || *name == NUL || VIM_ISDIGIT(*name))
1918 luaL_error(L, "invalid function name: %s", name);
1919 luaV_newfuncref(L, (char_u *) name);
Bram Moolenaar1dced572012-04-05 16:54:08 +02001920 return 1;
1921}
1922
1923 static int
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001924luaV_buffer(lua_State *L)
1925{
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001926 buf_T *buf;
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001927 if (lua_isstring(L, 1)) // get by number or name?
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001928 {
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001929 if (lua_isnumber(L, 1)) // by number?
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001930 {
1931 int n = lua_tointeger(L, 1);
Bram Moolenaar29323592016-07-24 22:04:11 +02001932 FOR_ALL_BUFFERS(buf)
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001933 if (buf->b_fnum == n) break;
1934 }
Bram Moolenaarca06da92018-07-01 15:12:05 +02001935 else // by name
1936 {
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001937 size_t l;
1938 const char *s = lua_tolstring(L, 1, &l);
Bram Moolenaar29323592016-07-24 22:04:11 +02001939 FOR_ALL_BUFFERS(buf)
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001940 {
1941 if (buf->b_ffname == NULL || buf->b_sfname == NULL)
1942 {
1943 if (l == 0) break;
1944 }
Bram Moolenaar0d2e4fc2010-07-18 12:35:47 +02001945 else if (strncmp(s, (char *)buf->b_ffname, l) == 0
1946 || strncmp(s, (char *)buf->b_sfname, l) == 0)
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001947 break;
1948 }
1949 }
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001950 }
Bram Moolenaar1dced572012-04-05 16:54:08 +02001951 else
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001952 buf = (lua_toboolean(L, 1)) ? firstbuf : curbuf; // first buffer?
Bram Moolenaar1dced572012-04-05 16:54:08 +02001953 luaV_pushbuffer(L, buf);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001954 return 1;
1955}
1956
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001957 static int
1958luaV_window(lua_State *L)
1959{
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001960 win_T *win;
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001961 if (lua_isnumber(L, 1)) // get by number?
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001962 {
1963 int n = lua_tointeger(L, 1);
1964 for (win = firstwin; win != NULL; win = win->w_next, n--)
1965 if (n == 1) break;
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001966 }
Bram Moolenaar1dced572012-04-05 16:54:08 +02001967 else
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001968 win = (lua_toboolean(L, 1)) ? firstwin : curwin; // first window?
Bram Moolenaar1dced572012-04-05 16:54:08 +02001969 luaV_pushwindow(L, win);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001970 return 1;
1971}
1972
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001973 static int
1974luaV_open(lua_State *L)
1975{
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001976 char_u *s = NULL;
1977#ifdef HAVE_SANDBOX
1978 luaV_checksandbox(L);
1979#endif
1980 if (lua_isstring(L, 1)) s = (char_u *) lua_tostring(L, 1);
Bram Moolenaarfa263a52011-12-08 16:00:16 +01001981 luaV_pushbuffer(L, buflist_new(s, NULL, 1L, BLN_LISTED));
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001982 return 1;
1983}
1984
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001985 static int
Bram Moolenaar1dced572012-04-05 16:54:08 +02001986luaV_type(lua_State *L)
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001987{
Bram Moolenaar1dced572012-04-05 16:54:08 +02001988 luaL_checkany(L, 1);
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001989 if (lua_type(L, 1) == LUA_TUSERDATA) // check vim udata?
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001990 {
Bram Moolenaar1dced572012-04-05 16:54:08 +02001991 lua_settop(L, 1);
1992 if (lua_getmetatable(L, 1))
1993 {
1994 luaV_getfield(L, LUAVIM_LIST);
1995 if (lua_rawequal(L, -1, 2))
1996 {
1997 lua_pushstring(L, "list");
1998 return 1;
1999 }
2000 luaV_getfield(L, LUAVIM_DICT);
2001 if (lua_rawequal(L, -1, 2))
2002 {
2003 lua_pushstring(L, "dict");
2004 return 1;
2005 }
Bram Moolenaarb7828692019-03-23 13:57:02 +01002006 luaV_getfield(L, LUAVIM_BLOB);
2007 if (lua_rawequal(L, -1, 2))
2008 {
2009 lua_pushstring(L, "blob");
2010 return 1;
2011 }
Bram Moolenaarca06da92018-07-01 15:12:05 +02002012 luaV_getfield(L, LUAVIM_FUNCREF);
2013 if (lua_rawequal(L, -1, 2))
2014 {
2015 lua_pushstring(L, "funcref");
2016 return 1;
2017 }
Bram Moolenaar1dced572012-04-05 16:54:08 +02002018 luaV_getfield(L, LUAVIM_BUFFER);
2019 if (lua_rawequal(L, -1, 2))
2020 {
2021 lua_pushstring(L, "buffer");
2022 return 1;
2023 }
2024 luaV_getfield(L, LUAVIM_WINDOW);
2025 if (lua_rawequal(L, -1, 2))
2026 {
2027 lua_pushstring(L, "window");
2028 return 1;
2029 }
2030 }
Bram Moolenaar0ba04292010-07-14 23:23:17 +02002031 }
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002032 lua_pushstring(L, luaL_typename(L, 1)); // fallback
Bram Moolenaar1dced572012-04-05 16:54:08 +02002033 return 1;
Bram Moolenaar0ba04292010-07-14 23:23:17 +02002034}
2035
Bram Moolenaareb04f082020-05-17 14:32:35 +02002036 static int
2037luaV_call(lua_State *L)
2038{
2039 int argc = lua_gettop(L) - 1;
2040 size_t funcname_len;
2041 char_u *funcname;
2042 char *error = NULL;
2043 typval_T rettv;
2044 typval_T argv[MAX_FUNC_ARGS + 1];
2045 int i = 0;
2046
2047 if (argc > MAX_FUNC_ARGS)
2048 return luaL_error(L, "Function called with too many arguments");
2049
2050 funcname = (char_u *)luaL_checklstring(L, 1, &funcname_len);
2051
2052 for (; i < argc; i++)
2053 {
2054 if (luaV_totypval(L, i + 2, &argv[i]) == FAIL)
2055 {
2056 error = "lua: cannot convert value";
2057 goto free_vim_args;
2058 }
2059 }
2060
2061 argv[argc].v_type = VAR_UNKNOWN;
2062
2063 if (call_vim_function(funcname, argc, argv, &rettv) == FAIL)
2064 {
2065 error = "lua: call_vim_function failed";
2066 goto free_vim_args;
2067 }
2068
2069 luaV_pushtypval(L, &rettv);
2070 clear_tv(&rettv);
2071
2072free_vim_args:
2073 while (i > 0)
2074 clear_tv(&argv[--i]);
2075
2076 if (error == NULL)
2077 return 1;
2078 else
2079 return luaL_error(L, error);
2080}
2081
Bram Moolenaar0ba04292010-07-14 23:23:17 +02002082static const luaL_Reg luaV_module[] = {
2083 {"command", luaV_command},
2084 {"eval", luaV_eval},
2085 {"beep", luaV_beep},
2086 {"line", luaV_line},
Bram Moolenaar1dced572012-04-05 16:54:08 +02002087 {"list", luaV_list},
2088 {"dict", luaV_dict},
Bram Moolenaarb7828692019-03-23 13:57:02 +01002089 {"blob", luaV_blob},
Bram Moolenaarca06da92018-07-01 15:12:05 +02002090 {"funcref", luaV_funcref},
Bram Moolenaar0ba04292010-07-14 23:23:17 +02002091 {"buffer", luaV_buffer},
2092 {"window", luaV_window},
2093 {"open", luaV_open},
Bram Moolenaar1dced572012-04-05 16:54:08 +02002094 {"type", luaV_type},
Bram Moolenaareb04f082020-05-17 14:32:35 +02002095 {"call", luaV_call},
Bram Moolenaar125ed272021-04-07 20:11:12 +02002096 {"lua_version", NULL},
Bram Moolenaar0ba04292010-07-14 23:23:17 +02002097 {NULL, NULL}
2098};
2099
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002100/*
2101 * for freeing list, dict, buffer and window objects; lightuserdata as arg
2102 */
Bram Moolenaar1dced572012-04-05 16:54:08 +02002103 static int
2104luaV_free(lua_State *L)
2105{
2106 lua_pushnil(L);
2107 luaV_setudata(L, lua_touserdata(L, 1));
2108 return 0;
2109}
2110
2111 static int
Bram Moolenaar4eefe472019-03-19 21:59:19 +01002112luaV_luaeval(lua_State *L)
Bram Moolenaar1dced572012-04-05 16:54:08 +02002113{
2114 luaL_Buffer b;
2115 size_t l;
2116 const char *str = lua_tolstring(L, 1, &l);
2117 typval_T *arg = (typval_T *) lua_touserdata(L, 2);
2118 typval_T *rettv = (typval_T *) lua_touserdata(L, 3);
2119 luaL_buffinit(L, &b);
2120 luaL_addlstring(&b, LUAVIM_EVALHEADER, sizeof(LUAVIM_EVALHEADER) - 1);
2121 luaL_addlstring(&b, str, l);
2122 luaL_pushresult(&b);
2123 str = lua_tolstring(L, -1, &l);
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002124 if (luaL_loadbuffer(L, str, l, LUAVIM_EVALNAME)) // compile error?
Bram Moolenaar1dced572012-04-05 16:54:08 +02002125 {
2126 luaV_emsg(L);
2127 return 0;
2128 }
2129 luaV_pushtypval(L, arg);
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002130 if (lua_pcall(L, 1, 1, 0)) // running error?
Bram Moolenaar1dced572012-04-05 16:54:08 +02002131 {
2132 luaV_emsg(L);
2133 return 0;
2134 }
Bram Moolenaarca06da92018-07-01 15:12:05 +02002135 if (luaV_totypval(L, -1, rettv) == FAIL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002136 emsg("luaeval: cannot convert value");
Bram Moolenaarf554a322015-02-04 23:08:01 +01002137 return 0;
Bram Moolenaar1dced572012-04-05 16:54:08 +02002138}
2139
2140 static int
Bram Moolenaar4eefe472019-03-19 21:59:19 +01002141luaV_setref(lua_State *L)
Bram Moolenaar1dced572012-04-05 16:54:08 +02002142{
Bram Moolenaar4eefe472019-03-19 21:59:19 +01002143 int copyID = lua_tointeger(L, 1);
2144 int abort = FALSE;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01002145
Bram Moolenaar1dced572012-04-05 16:54:08 +02002146 luaV_getfield(L, LUAVIM_LIST);
2147 luaV_getfield(L, LUAVIM_DICT);
Bram Moolenaar4eefe472019-03-19 21:59:19 +01002148 luaV_getfield(L, LUAVIM_FUNCREF);
Bram Moolenaar1dced572012-04-05 16:54:08 +02002149 lua_pushnil(L);
Bram Moolenaar4eefe472019-03-19 21:59:19 +01002150 // traverse cache table
Bram Moolenaarb84634d2015-02-04 22:02:37 +01002151 while (!abort && lua_next(L, lua_upvalueindex(1)) != 0)
Bram Moolenaar1dced572012-04-05 16:54:08 +02002152 {
2153 lua_getmetatable(L, -1);
Bram Moolenaar4eefe472019-03-19 21:59:19 +01002154 if (lua_rawequal(L, -1, 2)) // list?
Bram Moolenaar1dced572012-04-05 16:54:08 +02002155 {
Bram Moolenaar4eefe472019-03-19 21:59:19 +01002156 list_T *l = (list_T *)lua_touserdata(L, 5); // key
2157
Bram Moolenaar7be3ab22019-06-23 01:46:15 +02002158 abort = set_ref_in_list(l, copyID);
Bram Moolenaar1dced572012-04-05 16:54:08 +02002159 }
Bram Moolenaar4eefe472019-03-19 21:59:19 +01002160 else if (lua_rawequal(L, -1, 3)) // dict?
Bram Moolenaar1dced572012-04-05 16:54:08 +02002161 {
Bram Moolenaar4eefe472019-03-19 21:59:19 +01002162 dict_T *d = (dict_T *)lua_touserdata(L, 5); // key
2163
Bram Moolenaar7be3ab22019-06-23 01:46:15 +02002164 abort = set_ref_in_dict(d, copyID);
Bram Moolenaar1dced572012-04-05 16:54:08 +02002165 }
Bram Moolenaar4eefe472019-03-19 21:59:19 +01002166 else if (lua_rawequal(L, -1, 4)) // funcref?
2167 {
2168 luaV_Funcref *f = (luaV_Funcref *)lua_touserdata(L, 5); // key
2169
Bram Moolenaar7be3ab22019-06-23 01:46:15 +02002170 abort = set_ref_in_dict(f->self, copyID);
Bram Moolenaar4eefe472019-03-19 21:59:19 +01002171 }
2172 lua_pop(L, 2); // metatable and value
Bram Moolenaar1dced572012-04-05 16:54:08 +02002173 }
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01002174 lua_pushinteger(L, abort);
Bram Moolenaarf554a322015-02-04 23:08:01 +01002175 return 1;
Bram Moolenaar1dced572012-04-05 16:54:08 +02002176}
2177
Bram Moolenaar125ed272021-04-07 20:11:12 +02002178 static int
2179luaV_pushversion(lua_State *L)
2180{
2181 int major = 0;
2182 int minor = 0;
2183 int patch = 0;
2184 char s[16];
2185
2186 sscanf(LUAVIM_VERSION, "Lua %d.%d.%d", &major, &minor, &patch);
2187 vim_snprintf(s, sizeof(s), "%d.%d.%d", major, minor, patch);
2188 lua_pushstring(L, s);
2189 return 0;
2190}
2191
Bram Moolenaareb04f082020-05-17 14:32:35 +02002192#define LUA_VIM_FN_CODE \
Bram Moolenaar788fbb42020-05-31 14:08:12 +02002193 "vim.fn = setmetatable({}, {\n"\
2194 " __index = function (t, key)\n"\
2195 " local function _fn(...)\n"\
2196 " return vim.call(key, ...)\n"\
2197 " end\n"\
2198 " t[key] = _fn\n"\
2199 " return _fn\n"\
2200 " end\n"\
2201 " })"
2202
2203#define LUA_VIM_UPDATE_PACKAGE_PATHS \
2204 "local last_vim_paths = {}\n"\
2205 "vim._update_package_paths = function ()\n"\
2206 " local cur_vim_paths = {}\n"\
2207 " local function split(s, delimiter)\n"\
2208 " result = {}\n"\
2209 " for match in (s..delimiter):gmatch(\"(.-)\"..delimiter) do\n"\
2210 " table.insert(result, match)\n"\
2211 " end\n"\
2212 " return result\n"\
2213 " end\n"\
2214 " local rtps = split(vim.eval('&runtimepath'), ',')\n"\
2215 " local sep = package.config:sub(1, 1)\n"\
2216 " for _, key in ipairs({'path', 'cpath'}) do\n"\
2217 " local orig_str = package[key] .. ';'\n"\
2218 " local pathtrails_ordered = {}\n"\
2219 " -- Note: ignores trailing item without trailing `;`. Not using something\n"\
2220 " -- simpler in order to preserve empty items (stand for default path).\n"\
2221 " local orig = {}\n"\
2222 " for s in orig_str:gmatch('[^;]*;') do\n"\
2223 " s = s:sub(1, -2) -- Strip trailing semicolon\n"\
2224 " orig[#orig + 1] = s\n"\
2225 " end\n"\
2226 " if key == 'path' then\n"\
2227 " -- /?.lua and /?/init.lua\n"\
2228 " pathtrails_ordered = {sep .. '?.lua', sep .. '?' .. sep .. 'init.lua'}\n"\
2229 " else\n"\
2230 " local pathtrails = {}\n"\
2231 " for _, s in ipairs(orig) do\n"\
2232 " -- Find out path patterns. pathtrail should contain something like\n"\
2233 " -- /?.so, \?.dll. This allows not to bother determining what correct\n"\
2234 " -- suffixes are.\n"\
2235 " local pathtrail = s:match('[/\\\\][^/\\\\]*%?.*$')\n"\
2236 " if pathtrail and not pathtrails[pathtrail] then\n"\
2237 " pathtrails[pathtrail] = true\n"\
2238 " pathtrails_ordered[#pathtrails_ordered + 1] = pathtrail\n"\
2239 " end\n"\
2240 " end\n"\
2241 " end\n"\
2242 " local new = {}\n"\
2243 " for _, rtp in ipairs(rtps) do\n"\
2244 " if not rtp:match(';') then\n"\
2245 " for _, pathtrail in pairs(pathtrails_ordered) do\n"\
2246 " local new_path = rtp .. sep .. 'lua' .. pathtrail\n"\
2247 " -- Always keep paths from &runtimepath at the start:\n"\
2248 " -- append them here disregarding orig possibly containing one of them.\n"\
2249 " new[#new + 1] = new_path\n"\
2250 " cur_vim_paths[new_path] = true\n"\
2251 " end\n"\
2252 " end\n"\
2253 " end\n"\
2254 " for _, orig_path in ipairs(orig) do\n"\
2255 " -- Handle removing obsolete paths originating from &runtimepath: such\n"\
2256 " -- paths either belong to cur_nvim_paths and were already added above or\n"\
2257 " -- to last_nvim_paths and should not be added at all if corresponding\n"\
2258 " -- entry was removed from &runtimepath list.\n"\
2259 " if not (cur_vim_paths[orig_path] or last_vim_paths[orig_path]) then\n"\
2260 " new[#new + 1] = orig_path\n"\
2261 " end\n"\
2262 " end\n"\
2263 " package[key] = table.concat(new, ';')\n"\
2264 " end\n"\
2265 " last_vim_paths = cur_vim_paths\n"\
2266 "end"
Bram Moolenaareb04f082020-05-17 14:32:35 +02002267
Bram Moolenaar55d5c032010-07-17 23:52:29 +02002268 static int
2269luaopen_vim(lua_State *L)
2270{
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002271 // set cache table
Bram Moolenaar0ba04292010-07-14 23:23:17 +02002272 lua_newtable(L);
2273 lua_newtable(L);
Bram Moolenaar1dced572012-04-05 16:54:08 +02002274 lua_pushstring(L, "v");
Bram Moolenaar0ba04292010-07-14 23:23:17 +02002275 lua_setfield(L, -2, "__mode");
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002276 lua_setmetatable(L, -2); // cache is weak-valued
2277 // print
Bram Moolenaar0ba04292010-07-14 23:23:17 +02002278 lua_pushcfunction(L, luaV_print);
2279 lua_setglobal(L, "print");
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002280 // debug.debug
Bram Moolenaar38e2b062011-09-21 17:15:39 +02002281 lua_getglobal(L, "debug");
2282 lua_pushcfunction(L, luaV_debug);
2283 lua_setfield(L, -2, "debug");
2284 lua_pop(L, 1);
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002285 // free
Bram Moolenaar0ba04292010-07-14 23:23:17 +02002286 lua_pushlightuserdata(L, (void *) LUAVIM_FREE);
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002287 lua_pushvalue(L, 1); // cache table
Bram Moolenaar1dced572012-04-05 16:54:08 +02002288 lua_pushcclosure(L, luaV_free, 1);
2289 lua_rawset(L, LUA_REGISTRYINDEX);
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002290 // luaeval
Bram Moolenaar1dced572012-04-05 16:54:08 +02002291 lua_pushlightuserdata(L, (void *) LUAVIM_LUAEVAL);
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002292 lua_pushvalue(L, 1); // cache table
Bram Moolenaar1dced572012-04-05 16:54:08 +02002293 lua_pushcclosure(L, luaV_luaeval, 1);
2294 lua_rawset(L, LUA_REGISTRYINDEX);
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002295 // setref
Bram Moolenaar1dced572012-04-05 16:54:08 +02002296 lua_pushlightuserdata(L, (void *) LUAVIM_SETREF);
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002297 lua_pushvalue(L, 1); // cache table
Bram Moolenaar1dced572012-04-05 16:54:08 +02002298 lua_pushcclosure(L, luaV_setref, 1);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02002299 lua_rawset(L, LUA_REGISTRYINDEX);
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002300 // register
Bram Moolenaar1dced572012-04-05 16:54:08 +02002301 luaV_newmetatable(L, LUAVIM_LIST);
2302 lua_pushvalue(L, 1);
2303 luaV_openlib(L, luaV_List_mt, 1);
2304 luaV_newmetatable(L, LUAVIM_DICT);
2305 lua_pushvalue(L, 1);
2306 luaV_openlib(L, luaV_Dict_mt, 1);
Bram Moolenaarb7828692019-03-23 13:57:02 +01002307 luaV_newmetatable(L, LUAVIM_BLOB);
2308 lua_pushvalue(L, 1);
2309 luaV_openlib(L, luaV_Blob_mt, 1);
Bram Moolenaarca06da92018-07-01 15:12:05 +02002310 luaV_newmetatable(L, LUAVIM_FUNCREF);
2311 lua_pushvalue(L, 1);
2312 luaV_openlib(L, luaV_Funcref_mt, 1);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02002313 luaV_newmetatable(L, LUAVIM_BUFFER);
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002314 lua_pushvalue(L, 1); // cache table
Bram Moolenaar1dced572012-04-05 16:54:08 +02002315 luaV_openlib(L, luaV_Buffer_mt, 1);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02002316 luaV_newmetatable(L, LUAVIM_WINDOW);
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002317 lua_pushvalue(L, 1); // cache table
Bram Moolenaar1dced572012-04-05 16:54:08 +02002318 luaV_openlib(L, luaV_Window_mt, 1);
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002319 lua_newtable(L); // vim table
2320 lua_pushvalue(L, 1); // cache table
Bram Moolenaar1dced572012-04-05 16:54:08 +02002321 luaV_openlib(L, luaV_module, 1);
Bram Moolenaar125ed272021-04-07 20:11:12 +02002322 luaV_pushversion(L);
2323 lua_setfield(L, -2, "lua_version");
Bram Moolenaar1dced572012-04-05 16:54:08 +02002324 lua_setglobal(L, LUAVIM_NAME);
Bram Moolenaareb04f082020-05-17 14:32:35 +02002325 // custom code
Bram Moolenaar9309eb22020-05-17 16:53:56 +02002326 (void)luaL_dostring(L, LUA_VIM_FN_CODE);
Bram Moolenaar788fbb42020-05-31 14:08:12 +02002327 (void)luaL_dostring(L, LUA_VIM_UPDATE_PACKAGE_PATHS);
2328
2329 lua_getglobal(L, "vim");
2330 lua_getfield(L, -1, "_update_package_paths");
2331
2332 if (lua_pcall(L, 0, 0, 0))
2333 luaV_emsg(L);
2334
Bram Moolenaar0ba04292010-07-14 23:23:17 +02002335 return 0;
2336}
2337
Bram Moolenaar55d5c032010-07-17 23:52:29 +02002338 static lua_State *
2339luaV_newstate(void)
2340{
Bram Moolenaar0ba04292010-07-14 23:23:17 +02002341 lua_State *L = luaL_newstate();
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002342 luaL_openlibs(L); // core libs
2343 lua_pushcfunction(L, luaopen_vim); // vim
Bram Moolenaar0ba04292010-07-14 23:23:17 +02002344 lua_call(L, 0, 0);
2345 return L;
2346}
2347
Bram Moolenaar55d5c032010-07-17 23:52:29 +02002348 static void
2349luaV_setrange(lua_State *L, int line1, int line2)
2350{
Bram Moolenaar0ba04292010-07-14 23:23:17 +02002351 lua_getglobal(L, LUAVIM_NAME);
2352 lua_pushinteger(L, line1);
2353 lua_setfield(L, -2, "firstline");
2354 lua_pushinteger(L, line2);
2355 lua_setfield(L, -2, "lastline");
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002356 lua_pop(L, 1); // vim table
Bram Moolenaar0ba04292010-07-14 23:23:17 +02002357}
2358
2359
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002360// ======= Interface =======
Bram Moolenaar0ba04292010-07-14 23:23:17 +02002361
2362static lua_State *L = NULL;
2363
Bram Moolenaar55d5c032010-07-17 23:52:29 +02002364 static int
Bram Moolenaar1dced572012-04-05 16:54:08 +02002365lua_isopen(void)
Bram Moolenaar2bd6a1b2010-08-12 22:14:01 +02002366{
2367 return L != NULL;
2368}
2369
2370 static int
Bram Moolenaar55d5c032010-07-17 23:52:29 +02002371lua_init(void)
2372{
Bram Moolenaar1dced572012-04-05 16:54:08 +02002373 if (!lua_isopen())
Bram Moolenaar55d5c032010-07-17 23:52:29 +02002374 {
Bram Moolenaar0ba04292010-07-14 23:23:17 +02002375#ifdef DYNAMIC_LUA
Bram Moolenaar55d5c032010-07-17 23:52:29 +02002376 if (!lua_enabled(TRUE))
2377 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002378 emsg(_("Lua library cannot be loaded."));
Bram Moolenaar55d5c032010-07-17 23:52:29 +02002379 return FAIL;
2380 }
Bram Moolenaar0ba04292010-07-14 23:23:17 +02002381#endif
Bram Moolenaar55d5c032010-07-17 23:52:29 +02002382 L = luaV_newstate();
Bram Moolenaar0ba04292010-07-14 23:23:17 +02002383 }
2384 return OK;
2385}
2386
Bram Moolenaar55d5c032010-07-17 23:52:29 +02002387 void
2388lua_end(void)
2389{
Bram Moolenaar1dced572012-04-05 16:54:08 +02002390 if (lua_isopen())
Bram Moolenaar55d5c032010-07-17 23:52:29 +02002391 {
2392 lua_close(L);
2393 L = NULL;
Bram Moolenaar0ba04292010-07-14 23:23:17 +02002394 }
2395}
2396
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002397/*
2398 * ex commands
2399 */
Bram Moolenaar55d5c032010-07-17 23:52:29 +02002400 void
2401ex_lua(exarg_T *eap)
2402{
Bram Moolenaarc8970b92020-10-26 20:18:08 +01002403 char *script = (char *)script_get(eap, eap->arg);
2404
2405 if (!eap->skip && lua_init() == OK)
Bram Moolenaar55d5c032010-07-17 23:52:29 +02002406 {
Bram Moolenaarc8970b92020-10-26 20:18:08 +01002407 char *s = script != NULL ? script : (char *)eap->arg;
2408
Bram Moolenaar55d5c032010-07-17 23:52:29 +02002409 luaV_setrange(L, eap->line1, eap->line2);
2410 if (luaL_loadbuffer(L, s, strlen(s), LUAVIM_CHUNKNAME)
2411 || lua_pcall(L, 0, 0, 0))
2412 luaV_emsg(L);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02002413 }
Bram Moolenaarc8970b92020-10-26 20:18:08 +01002414 if (script != NULL)
2415 vim_free(script);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02002416}
2417
Bram Moolenaar55d5c032010-07-17 23:52:29 +02002418 void
2419ex_luado(exarg_T *eap)
2420{
Bram Moolenaar0ba04292010-07-14 23:23:17 +02002421 linenr_T l;
2422 const char *s = (const char *) eap->arg;
2423 luaL_Buffer b;
2424 size_t len;
Bram Moolenaard58f03b2017-01-29 22:48:45 +01002425 buf_T *was_curbuf = curbuf;
2426
Bram Moolenaar0ba04292010-07-14 23:23:17 +02002427 if (lua_init() == FAIL) return;
Bram Moolenaar55d5c032010-07-17 23:52:29 +02002428 if (u_save(eap->line1 - 1, eap->line2 + 1) == FAIL)
2429 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002430 emsg(_("cannot save undo information"));
Bram Moolenaar55d5c032010-07-17 23:52:29 +02002431 return;
Bram Moolenaar0ba04292010-07-14 23:23:17 +02002432 }
2433 luaV_setrange(L, eap->line1, eap->line2);
2434 luaL_buffinit(L, &b);
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002435 luaL_addlstring(&b, "return function(line, linenr) ", 30); // header
Bram Moolenaar0ba04292010-07-14 23:23:17 +02002436 luaL_addlstring(&b, s, strlen(s));
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002437 luaL_addlstring(&b, " end", 4); // footer
Bram Moolenaar0ba04292010-07-14 23:23:17 +02002438 luaL_pushresult(&b);
2439 s = lua_tolstring(L, -1, &len);
Bram Moolenaar55d5c032010-07-17 23:52:29 +02002440 if (luaL_loadbuffer(L, s, len, LUAVIM_CHUNKNAME))
2441 {
2442 luaV_emsg(L);
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002443 lua_pop(L, 1); // function body
Bram Moolenaar55d5c032010-07-17 23:52:29 +02002444 return;
Bram Moolenaar0ba04292010-07-14 23:23:17 +02002445 }
2446 lua_call(L, 0, 1);
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002447 lua_replace(L, -2); // function -> body
Bram Moolenaar55d5c032010-07-17 23:52:29 +02002448 for (l = eap->line1; l <= eap->line2; l++)
2449 {
Bram Moolenaare49b8e82020-07-01 13:52:55 +02002450 // Check the line number, the command may have deleted lines.
Bram Moolenaard58f03b2017-01-29 22:48:45 +01002451 if (l > curbuf->b_ml.ml_line_count)
2452 break;
2453
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002454 lua_pushvalue(L, -1); // function
2455 luaV_pushline(L, curbuf, l); // current line as arg
2456 lua_pushinteger(L, l); // current line number as arg
Bram Moolenaarbd2f3c32012-04-06 14:31:00 +02002457 if (lua_pcall(L, 2, 1, 0))
Bram Moolenaar55d5c032010-07-17 23:52:29 +02002458 {
2459 luaV_emsg(L);
2460 break;
2461 }
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002462 // Catch the command switching to another buffer.
Bram Moolenaard58f03b2017-01-29 22:48:45 +01002463 if (curbuf != was_curbuf)
2464 break;
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002465 if (lua_isstring(L, -1)) // update line?
Bram Moolenaar55d5c032010-07-17 23:52:29 +02002466 {
Bram Moolenaar0ba04292010-07-14 23:23:17 +02002467#ifdef HAVE_SANDBOX
Bram Moolenaar55d5c032010-07-17 23:52:29 +02002468 luaV_checksandbox(L);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02002469#endif
Bram Moolenaar55d5c032010-07-17 23:52:29 +02002470 ml_replace(l, luaV_toline(L, -1), TRUE);
2471 changed_bytes(l, 0);
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002472 lua_pop(L, 1); // result from luaV_toline
Bram Moolenaar55d5c032010-07-17 23:52:29 +02002473 }
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002474 lua_pop(L, 1); // line
Bram Moolenaar0ba04292010-07-14 23:23:17 +02002475 }
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002476 lua_pop(L, 1); // function
Bram Moolenaar0ba04292010-07-14 23:23:17 +02002477 check_cursor();
2478 update_screen(NOT_VALID);
2479}
2480
Bram Moolenaar55d5c032010-07-17 23:52:29 +02002481 void
2482ex_luafile(exarg_T *eap)
2483{
2484 if (lua_init() == FAIL)
2485 return;
2486 if (!eap->skip)
2487 {
2488 luaV_setrange(L, eap->line1, eap->line2);
2489 if (luaL_loadfile(L, (char *) eap->arg) || lua_pcall(L, 0, 0, 0))
2490 luaV_emsg(L);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02002491 }
2492}
2493
Bram Moolenaar1dced572012-04-05 16:54:08 +02002494#define luaV_freetype(typ,tname) \
2495 void \
2496 lua_##tname##_free(typ *o) \
2497 { \
2498 if (!lua_isopen()) return; \
2499 luaV_getfield(L, LUAVIM_FREE); \
2500 lua_pushlightuserdata(L, (void *) o); \
2501 lua_call(L, 1, 0); \
2502 }
2503
2504luaV_freetype(buf_T, buffer)
2505luaV_freetype(win_T, window)
2506
Bram Moolenaar55d5c032010-07-17 23:52:29 +02002507 void
Bram Moolenaar4eefe472019-03-19 21:59:19 +01002508do_luaeval(char_u *str, typval_T *arg, typval_T *rettv)
Bram Moolenaar55d5c032010-07-17 23:52:29 +02002509{
Bram Moolenaar1dced572012-04-05 16:54:08 +02002510 lua_init();
2511 luaV_getfield(L, LUAVIM_LUAEVAL);
2512 lua_pushstring(L, (char *) str);
2513 lua_pushlightuserdata(L, (void *) arg);
2514 lua_pushlightuserdata(L, (void *) rettv);
2515 lua_call(L, 3, 0);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02002516}
2517
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01002518 int
Bram Moolenaar4eefe472019-03-19 21:59:19 +01002519set_ref_in_lua(int copyID)
Bram Moolenaar55d5c032010-07-17 23:52:29 +02002520{
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01002521 int aborted = 0;
2522
2523 if (lua_isopen())
2524 {
2525 luaV_getfield(L, LUAVIM_SETREF);
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002526 // call the function with 1 arg, getting 1 result back
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01002527 lua_pushinteger(L, copyID);
2528 lua_call(L, 1, 1);
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002529 // get the result
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01002530 aborted = lua_tointeger(L, -1);
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002531 // pop result off the stack
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01002532 lua_pop(L, 1);
2533 }
2534 return aborted;
Bram Moolenaar0ba04292010-07-14 23:23:17 +02002535}
2536
Bram Moolenaar788fbb42020-05-31 14:08:12 +02002537 void
2538update_package_paths_in_lua()
2539{
2540 if (lua_isopen())
2541 {
2542 lua_getglobal(L, "vim");
2543 lua_getfield(L, -1, "_update_package_paths");
2544
2545 if (lua_pcall(L, 0, 0, 0))
2546 luaV_emsg(L);
2547 }
2548}
2549
Bram Moolenaar801ab062020-06-25 19:27:56 +02002550/*
2551 * Native C function callback
2552 */
2553 static int
2554luaV_call_lua_func(
2555 int argcount,
2556 typval_T *argvars,
2557 typval_T *rettv,
2558 void *state)
2559{
2560 int i;
2561 int luaargcount = argcount;
2562 luaV_CFuncState *funcstate = (luaV_CFuncState*)state;
2563 lua_rawgeti(funcstate->L, LUA_REGISTRYINDEX, funcstate->lua_funcref);
2564
2565 if (funcstate->lua_tableref != LUA_NOREF)
2566 {
2567 // First arg for metatable __call method is a table
2568 luaargcount += 1;
2569 lua_rawgeti(funcstate->L, LUA_REGISTRYINDEX, funcstate->lua_tableref);
2570 }
2571
2572 for (i = 0; i < argcount; ++i)
2573 luaV_pushtypval(funcstate->L, &argvars[i]);
2574
2575 if (lua_pcall(funcstate->L, luaargcount, 1, 0))
2576 {
2577 luaV_emsg(funcstate->L);
2578 return FCERR_OTHER;
2579 }
2580
2581 luaV_checktypval(funcstate->L, -1, rettv, "get return value");
2582 return FCERR_NONE;
2583}
2584
2585/*
2586 * Free up any lua references held by the func state.
2587 */
2588 static void
2589luaV_call_lua_func_free(void *state)
2590{
2591 luaV_CFuncState *funcstate = (luaV_CFuncState*)state;
2592 luaL_unref(L, LUA_REGISTRYINDEX, funcstate->lua_funcref);
2593 funcstate->L = NULL;
2594 if (funcstate->lua_tableref != LUA_NOREF)
2595 luaL_unref(L, LUA_REGISTRYINDEX, funcstate->lua_tableref);
2596 VIM_CLEAR(funcstate);
2597}
2598
Bram Moolenaar0ba04292010-07-14 23:23:17 +02002599#endif