blob: a3bb0b96bdb830ce59207c92eee20e7e21bb77bd [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
Martin Tournoij1a3e5742021-07-24 13:57:29 +0200108# define load_dll_error dlerror
Bram Moolenaar2334b6d2010-07-22 21:32:16 +0200109#else
Bram Moolenaarebbcb822010-10-23 14:02:54 +0200110# define load_dll vimLoadLib
Bram Moolenaar2334b6d2010-07-22 21:32:16 +0200111# define symbol_from_dll GetProcAddress
112# define close_dll FreeLibrary
Martin Tournoij1a3e5742021-07-24 13:57:29 +0200113# define load_dll_error GetWin32Error
Bram Moolenaar2334b6d2010-07-22 21:32:16 +0200114#endif
115
Bram Moolenaar2ab2e862019-12-04 21:24:53 +0100116// lauxlib
Bram Moolenaar1dced572012-04-05 16:54:08 +0200117#if LUA_VERSION_NUM <= 501
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200118#define luaL_register dll_luaL_register
Bram Moolenaar1dced572012-04-05 16:54:08 +0200119#define luaL_prepbuffer dll_luaL_prepbuffer
120#define luaL_openlib dll_luaL_openlib
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200121#define luaL_typerror dll_luaL_typerror
Bram Moolenaar1dced572012-04-05 16:54:08 +0200122#define luaL_loadfile dll_luaL_loadfile
123#define luaL_loadbuffer dll_luaL_loadbuffer
124#else
125#define luaL_prepbuffsize dll_luaL_prepbuffsize
126#define luaL_setfuncs dll_luaL_setfuncs
127#define luaL_loadfilex dll_luaL_loadfilex
128#define luaL_loadbufferx dll_luaL_loadbufferx
129#define luaL_argerror dll_luaL_argerror
130#endif
Bram Moolenaar5551b132020-07-14 21:54:28 +0200131#if LUA_VERSION_NUM >= 504
132#define luaL_typeerror dll_luaL_typeerror
133#endif
Bram Moolenaarbd2f3c32012-04-06 14:31:00 +0200134#define luaL_checkany dll_luaL_checkany
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200135#define luaL_checklstring dll_luaL_checklstring
136#define luaL_checkinteger dll_luaL_checkinteger
137#define luaL_optinteger dll_luaL_optinteger
138#define luaL_checktype dll_luaL_checktype
139#define luaL_error dll_luaL_error
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200140#define luaL_newstate dll_luaL_newstate
141#define luaL_buffinit dll_luaL_buffinit
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200142#define luaL_addlstring dll_luaL_addlstring
143#define luaL_pushresult dll_luaL_pushresult
Bram Moolenaardf1643a2020-05-17 18:53:27 +0200144#define luaL_loadstring dll_luaL_loadstring
Bram Moolenaar1e4c7d02020-06-25 20:56:42 +0200145#define luaL_ref dll_luaL_ref
146#define luaL_unref dll_luaL_unref
Bram Moolenaar2ab2e862019-12-04 21:24:53 +0100147// lua
Bram Moolenaar1dced572012-04-05 16:54:08 +0200148#if LUA_VERSION_NUM <= 501
149#define lua_tonumber dll_lua_tonumber
150#define lua_tointeger dll_lua_tointeger
151#define lua_call dll_lua_call
152#define lua_pcall dll_lua_pcall
153#else
154#define lua_tonumberx dll_lua_tonumberx
155#define lua_tointegerx dll_lua_tointegerx
156#define lua_callk dll_lua_callk
157#define lua_pcallk dll_lua_pcallk
158#define lua_getglobal dll_lua_getglobal
159#define lua_setglobal dll_lua_setglobal
Bram Moolenaar1dced572012-04-05 16:54:08 +0200160#endif
Bram Moolenaar1f860d82015-06-27 18:36:16 +0200161#if LUA_VERSION_NUM <= 502
162#define lua_replace dll_lua_replace
163#define lua_remove dll_lua_remove
164#endif
165#if LUA_VERSION_NUM >= 503
166#define lua_rotate dll_lua_rotate
167#define lua_copy dll_lua_copy
168#endif
Bram Moolenaarbd2f3c32012-04-06 14:31:00 +0200169#define lua_typename dll_lua_typename
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200170#define lua_close dll_lua_close
171#define lua_gettop dll_lua_gettop
172#define lua_settop dll_lua_settop
173#define lua_pushvalue dll_lua_pushvalue
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200174#define lua_isnumber dll_lua_isnumber
175#define lua_isstring dll_lua_isstring
176#define lua_type dll_lua_type
177#define lua_rawequal dll_lua_rawequal
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200178#define lua_toboolean dll_lua_toboolean
179#define lua_tolstring dll_lua_tolstring
180#define lua_touserdata dll_lua_touserdata
181#define lua_pushnil dll_lua_pushnil
182#define lua_pushnumber dll_lua_pushnumber
183#define lua_pushinteger dll_lua_pushinteger
184#define lua_pushlstring dll_lua_pushlstring
185#define lua_pushstring dll_lua_pushstring
186#define lua_pushfstring dll_lua_pushfstring
187#define lua_pushcclosure dll_lua_pushcclosure
188#define lua_pushboolean dll_lua_pushboolean
189#define lua_pushlightuserdata dll_lua_pushlightuserdata
190#define lua_getfield dll_lua_getfield
191#define lua_rawget dll_lua_rawget
Bram Moolenaar1dced572012-04-05 16:54:08 +0200192#define lua_rawgeti dll_lua_rawgeti
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200193#define lua_createtable dll_lua_createtable
Bram Moolenaar830e3582018-08-21 14:23:35 +0200194#if LUA_VERSION_NUM >= 504
195 #define lua_newuserdatauv dll_lua_newuserdatauv
196#else
197 #define lua_newuserdata dll_lua_newuserdata
198#endif
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200199#define lua_getmetatable dll_lua_getmetatable
200#define lua_setfield dll_lua_setfield
201#define lua_rawset dll_lua_rawset
202#define lua_rawseti dll_lua_rawseti
203#define lua_setmetatable dll_lua_setmetatable
Bram Moolenaarbd2f3c32012-04-06 14:31:00 +0200204#define lua_next dll_lua_next
Bram Moolenaar2ab2e862019-12-04 21:24:53 +0100205// libs
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200206#define luaopen_base dll_luaopen_base
207#define luaopen_table dll_luaopen_table
208#define luaopen_string dll_luaopen_string
209#define luaopen_math dll_luaopen_math
Bram Moolenaar16c98f92010-07-28 22:46:08 +0200210#define luaopen_io dll_luaopen_io
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200211#define luaopen_os dll_luaopen_os
212#define luaopen_package dll_luaopen_package
213#define luaopen_debug dll_luaopen_debug
Bram Moolenaar16c98f92010-07-28 22:46:08 +0200214#define luaL_openlibs dll_luaL_openlibs
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200215
Bram Moolenaar2ab2e862019-12-04 21:24:53 +0100216// lauxlib
Bram Moolenaar1dced572012-04-05 16:54:08 +0200217#if LUA_VERSION_NUM <= 501
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200218void (*dll_luaL_register) (lua_State *L, const char *libname, const luaL_Reg *l);
Bram Moolenaar1dced572012-04-05 16:54:08 +0200219char *(*dll_luaL_prepbuffer) (luaL_Buffer *B);
220void (*dll_luaL_openlib) (lua_State *L, const char *libname, const luaL_Reg *l, int nup);
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200221int (*dll_luaL_typerror) (lua_State *L, int narg, const char *tname);
Bram Moolenaar1dced572012-04-05 16:54:08 +0200222int (*dll_luaL_loadfile) (lua_State *L, const char *filename);
223int (*dll_luaL_loadbuffer) (lua_State *L, const char *buff, size_t sz, const char *name);
224#else
225char *(*dll_luaL_prepbuffsize) (luaL_Buffer *B, size_t sz);
226void (*dll_luaL_setfuncs) (lua_State *L, const luaL_Reg *l, int nup);
227int (*dll_luaL_loadfilex) (lua_State *L, const char *filename, const char *mode);
228int (*dll_luaL_loadbufferx) (lua_State *L, const char *buff, size_t sz, const char *name, const char *mode);
229int (*dll_luaL_argerror) (lua_State *L, int numarg, const char *extramsg);
230#endif
Bram Moolenaar5551b132020-07-14 21:54:28 +0200231#if LUA_VERSION_NUM >= 504
232int (*dll_luaL_typeerror) (lua_State *L, int narg, const char *tname);
233#endif
Bram Moolenaarbd2f3c32012-04-06 14:31:00 +0200234void (*dll_luaL_checkany) (lua_State *L, int narg);
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200235const char *(*dll_luaL_checklstring) (lua_State *L, int numArg, size_t *l);
236lua_Integer (*dll_luaL_checkinteger) (lua_State *L, int numArg);
237lua_Integer (*dll_luaL_optinteger) (lua_State *L, int nArg, lua_Integer def);
238void (*dll_luaL_checktype) (lua_State *L, int narg, int t);
239int (*dll_luaL_error) (lua_State *L, const char *fmt, ...);
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200240lua_State *(*dll_luaL_newstate) (void);
241void (*dll_luaL_buffinit) (lua_State *L, luaL_Buffer *B);
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200242void (*dll_luaL_addlstring) (luaL_Buffer *B, const char *s, size_t l);
243void (*dll_luaL_pushresult) (luaL_Buffer *B);
Bram Moolenaardf1643a2020-05-17 18:53:27 +0200244int (*dll_luaL_loadstring) (lua_State *L, const char *s);
Bram Moolenaar1e4c7d02020-06-25 20:56:42 +0200245int (*dll_luaL_ref) (lua_State *L, int idx);
246#if LUA_VERSION_NUM <= 502
247void (*dll_luaL_unref) (lua_State *L, int idx, int n);
248#else
249void (*dll_luaL_unref) (lua_State *L, int idx, lua_Integer n);
250#endif
Bram Moolenaar2ab2e862019-12-04 21:24:53 +0100251// lua
Bram Moolenaar1dced572012-04-05 16:54:08 +0200252#if LUA_VERSION_NUM <= 501
253lua_Number (*dll_lua_tonumber) (lua_State *L, int idx);
254lua_Integer (*dll_lua_tointeger) (lua_State *L, int idx);
255void (*dll_lua_call) (lua_State *L, int nargs, int nresults);
256int (*dll_lua_pcall) (lua_State *L, int nargs, int nresults, int errfunc);
257#else
258lua_Number (*dll_lua_tonumberx) (lua_State *L, int idx, int *isnum);
259lua_Integer (*dll_lua_tointegerx) (lua_State *L, int idx, int *isnum);
260void (*dll_lua_callk) (lua_State *L, int nargs, int nresults, int ctx,
Bram Moolenaardb913952012-06-29 12:54:53 +0200261 lua_CFunction k);
Bram Moolenaar1dced572012-04-05 16:54:08 +0200262int (*dll_lua_pcallk) (lua_State *L, int nargs, int nresults, int errfunc,
Bram Moolenaardb913952012-06-29 12:54:53 +0200263 int ctx, lua_CFunction k);
Bram Moolenaar1dced572012-04-05 16:54:08 +0200264void (*dll_lua_getglobal) (lua_State *L, const char *var);
265void (*dll_lua_setglobal) (lua_State *L, const char *var);
Bram Moolenaar1f860d82015-06-27 18:36:16 +0200266#endif
267#if LUA_VERSION_NUM <= 502
268void (*dll_lua_replace) (lua_State *L, int idx);
269void (*dll_lua_remove) (lua_State *L, int idx);
270#endif
271#if LUA_VERSION_NUM >= 503
272void (*dll_lua_rotate) (lua_State *L, int idx, int n);
Bram Moolenaar9514b1f2015-06-25 18:27:32 +0200273void (*dll_lua_copy) (lua_State *L, int fromidx, int toidx);
Bram Moolenaar1dced572012-04-05 16:54:08 +0200274#endif
Bram Moolenaarbd2f3c32012-04-06 14:31:00 +0200275const char *(*dll_lua_typename) (lua_State *L, int tp);
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200276void (*dll_lua_close) (lua_State *L);
277int (*dll_lua_gettop) (lua_State *L);
278void (*dll_lua_settop) (lua_State *L, int idx);
279void (*dll_lua_pushvalue) (lua_State *L, int idx);
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200280int (*dll_lua_isnumber) (lua_State *L, int idx);
281int (*dll_lua_isstring) (lua_State *L, int idx);
282int (*dll_lua_type) (lua_State *L, int idx);
283int (*dll_lua_rawequal) (lua_State *L, int idx1, int idx2);
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200284int (*dll_lua_toboolean) (lua_State *L, int idx);
285const char *(*dll_lua_tolstring) (lua_State *L, int idx, size_t *len);
286void *(*dll_lua_touserdata) (lua_State *L, int idx);
287void (*dll_lua_pushnil) (lua_State *L);
288void (*dll_lua_pushnumber) (lua_State *L, lua_Number n);
289void (*dll_lua_pushinteger) (lua_State *L, lua_Integer n);
290void (*dll_lua_pushlstring) (lua_State *L, const char *s, size_t l);
291void (*dll_lua_pushstring) (lua_State *L, const char *s);
292const char *(*dll_lua_pushfstring) (lua_State *L, const char *fmt, ...);
293void (*dll_lua_pushcclosure) (lua_State *L, lua_CFunction fn, int n);
294void (*dll_lua_pushboolean) (lua_State *L, int b);
295void (*dll_lua_pushlightuserdata) (lua_State *L, void *p);
296void (*dll_lua_getfield) (lua_State *L, int idx, const char *k);
Bram Moolenaar17413672018-07-14 20:49:42 +0200297#if LUA_VERSION_NUM <= 502
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200298void (*dll_lua_rawget) (lua_State *L, int idx);
Bram Moolenaar1dced572012-04-05 16:54:08 +0200299void (*dll_lua_rawgeti) (lua_State *L, int idx, int n);
Bram Moolenaar17413672018-07-14 20:49:42 +0200300#else
301int (*dll_lua_rawget) (lua_State *L, int idx);
302int (*dll_lua_rawgeti) (lua_State *L, int idx, lua_Integer n);
303#endif
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200304void (*dll_lua_createtable) (lua_State *L, int narr, int nrec);
Bram Moolenaar830e3582018-08-21 14:23:35 +0200305#if LUA_VERSION_NUM >= 504
306void *(*dll_lua_newuserdatauv) (lua_State *L, size_t sz, int nuvalue);
307#else
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200308void *(*dll_lua_newuserdata) (lua_State *L, size_t sz);
Bram Moolenaar830e3582018-08-21 14:23:35 +0200309#endif
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200310int (*dll_lua_getmetatable) (lua_State *L, int objindex);
311void (*dll_lua_setfield) (lua_State *L, int idx, const char *k);
312void (*dll_lua_rawset) (lua_State *L, int idx);
Bram Moolenaar17413672018-07-14 20:49:42 +0200313#if LUA_VERSION_NUM <= 502
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200314void (*dll_lua_rawseti) (lua_State *L, int idx, int n);
Bram Moolenaar17413672018-07-14 20:49:42 +0200315#else
316void (*dll_lua_rawseti) (lua_State *L, int idx, lua_Integer n);
317#endif
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200318int (*dll_lua_setmetatable) (lua_State *L, int objindex);
Bram Moolenaarbd2f3c32012-04-06 14:31:00 +0200319int (*dll_lua_next) (lua_State *L, int idx);
Bram Moolenaar2ab2e862019-12-04 21:24:53 +0100320// libs
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200321int (*dll_luaopen_base) (lua_State *L);
322int (*dll_luaopen_table) (lua_State *L);
323int (*dll_luaopen_string) (lua_State *L);
324int (*dll_luaopen_math) (lua_State *L);
Bram Moolenaar16c98f92010-07-28 22:46:08 +0200325int (*dll_luaopen_io) (lua_State *L);
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200326int (*dll_luaopen_os) (lua_State *L);
327int (*dll_luaopen_package) (lua_State *L);
328int (*dll_luaopen_debug) (lua_State *L);
Bram Moolenaar16c98f92010-07-28 22:46:08 +0200329void (*dll_luaL_openlibs) (lua_State *L);
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200330
331typedef void **luaV_function;
332typedef struct {
333 const char *name;
334 luaV_function func;
335} luaV_Reg;
336
337static const luaV_Reg luaV_dll[] = {
Bram Moolenaar2ab2e862019-12-04 21:24:53 +0100338 // lauxlib
Bram Moolenaar1dced572012-04-05 16:54:08 +0200339#if LUA_VERSION_NUM <= 501
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200340 {"luaL_register", (luaV_function) &dll_luaL_register},
Bram Moolenaar1dced572012-04-05 16:54:08 +0200341 {"luaL_prepbuffer", (luaV_function) &dll_luaL_prepbuffer},
342 {"luaL_openlib", (luaV_function) &dll_luaL_openlib},
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200343 {"luaL_typerror", (luaV_function) &dll_luaL_typerror},
Bram Moolenaar1dced572012-04-05 16:54:08 +0200344 {"luaL_loadfile", (luaV_function) &dll_luaL_loadfile},
345 {"luaL_loadbuffer", (luaV_function) &dll_luaL_loadbuffer},
346#else
347 {"luaL_prepbuffsize", (luaV_function) &dll_luaL_prepbuffsize},
348 {"luaL_setfuncs", (luaV_function) &dll_luaL_setfuncs},
349 {"luaL_loadfilex", (luaV_function) &dll_luaL_loadfilex},
350 {"luaL_loadbufferx", (luaV_function) &dll_luaL_loadbufferx},
351 {"luaL_argerror", (luaV_function) &dll_luaL_argerror},
352#endif
Bram Moolenaar5551b132020-07-14 21:54:28 +0200353#if LUA_VERSION_NUM >= 504
354 {"luaL_typeerror", (luaV_function) &dll_luaL_typeerror},
355#endif
Bram Moolenaarbd2f3c32012-04-06 14:31:00 +0200356 {"luaL_checkany", (luaV_function) &dll_luaL_checkany},
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200357 {"luaL_checklstring", (luaV_function) &dll_luaL_checklstring},
358 {"luaL_checkinteger", (luaV_function) &dll_luaL_checkinteger},
359 {"luaL_optinteger", (luaV_function) &dll_luaL_optinteger},
360 {"luaL_checktype", (luaV_function) &dll_luaL_checktype},
361 {"luaL_error", (luaV_function) &dll_luaL_error},
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200362 {"luaL_newstate", (luaV_function) &dll_luaL_newstate},
363 {"luaL_buffinit", (luaV_function) &dll_luaL_buffinit},
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200364 {"luaL_addlstring", (luaV_function) &dll_luaL_addlstring},
365 {"luaL_pushresult", (luaV_function) &dll_luaL_pushresult},
Bram Moolenaardf1643a2020-05-17 18:53:27 +0200366 {"luaL_loadstring", (luaV_function) &dll_luaL_loadstring},
Bram Moolenaar1e4c7d02020-06-25 20:56:42 +0200367 {"luaL_ref", (luaV_function) &dll_luaL_ref},
368 {"luaL_unref", (luaV_function) &dll_luaL_unref},
Bram Moolenaar2ab2e862019-12-04 21:24:53 +0100369 // lua
Bram Moolenaar1dced572012-04-05 16:54:08 +0200370#if LUA_VERSION_NUM <= 501
371 {"lua_tonumber", (luaV_function) &dll_lua_tonumber},
372 {"lua_tointeger", (luaV_function) &dll_lua_tointeger},
373 {"lua_call", (luaV_function) &dll_lua_call},
374 {"lua_pcall", (luaV_function) &dll_lua_pcall},
375#else
376 {"lua_tonumberx", (luaV_function) &dll_lua_tonumberx},
377 {"lua_tointegerx", (luaV_function) &dll_lua_tointegerx},
378 {"lua_callk", (luaV_function) &dll_lua_callk},
379 {"lua_pcallk", (luaV_function) &dll_lua_pcallk},
380 {"lua_getglobal", (luaV_function) &dll_lua_getglobal},
381 {"lua_setglobal", (luaV_function) &dll_lua_setglobal},
Bram Moolenaar1dced572012-04-05 16:54:08 +0200382#endif
Bram Moolenaar1f860d82015-06-27 18:36:16 +0200383#if LUA_VERSION_NUM <= 502
384 {"lua_replace", (luaV_function) &dll_lua_replace},
385 {"lua_remove", (luaV_function) &dll_lua_remove},
386#endif
387#if LUA_VERSION_NUM >= 503
388 {"lua_rotate", (luaV_function) &dll_lua_rotate},
389 {"lua_copy", (luaV_function) &dll_lua_copy},
390#endif
Bram Moolenaarbd2f3c32012-04-06 14:31:00 +0200391 {"lua_typename", (luaV_function) &dll_lua_typename},
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200392 {"lua_close", (luaV_function) &dll_lua_close},
393 {"lua_gettop", (luaV_function) &dll_lua_gettop},
394 {"lua_settop", (luaV_function) &dll_lua_settop},
395 {"lua_pushvalue", (luaV_function) &dll_lua_pushvalue},
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200396 {"lua_isnumber", (luaV_function) &dll_lua_isnumber},
397 {"lua_isstring", (luaV_function) &dll_lua_isstring},
398 {"lua_type", (luaV_function) &dll_lua_type},
399 {"lua_rawequal", (luaV_function) &dll_lua_rawequal},
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200400 {"lua_toboolean", (luaV_function) &dll_lua_toboolean},
401 {"lua_tolstring", (luaV_function) &dll_lua_tolstring},
402 {"lua_touserdata", (luaV_function) &dll_lua_touserdata},
403 {"lua_pushnil", (luaV_function) &dll_lua_pushnil},
404 {"lua_pushnumber", (luaV_function) &dll_lua_pushnumber},
405 {"lua_pushinteger", (luaV_function) &dll_lua_pushinteger},
406 {"lua_pushlstring", (luaV_function) &dll_lua_pushlstring},
407 {"lua_pushstring", (luaV_function) &dll_lua_pushstring},
408 {"lua_pushfstring", (luaV_function) &dll_lua_pushfstring},
409 {"lua_pushcclosure", (luaV_function) &dll_lua_pushcclosure},
410 {"lua_pushboolean", (luaV_function) &dll_lua_pushboolean},
411 {"lua_pushlightuserdata", (luaV_function) &dll_lua_pushlightuserdata},
412 {"lua_getfield", (luaV_function) &dll_lua_getfield},
413 {"lua_rawget", (luaV_function) &dll_lua_rawget},
Bram Moolenaar1dced572012-04-05 16:54:08 +0200414 {"lua_rawgeti", (luaV_function) &dll_lua_rawgeti},
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200415 {"lua_createtable", (luaV_function) &dll_lua_createtable},
Bram Moolenaar830e3582018-08-21 14:23:35 +0200416#if LUA_VERSION_NUM >= 504
417 {"lua_newuserdatauv", (luaV_function) &dll_lua_newuserdatauv},
418#else
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200419 {"lua_newuserdata", (luaV_function) &dll_lua_newuserdata},
Bram Moolenaar830e3582018-08-21 14:23:35 +0200420#endif
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200421 {"lua_getmetatable", (luaV_function) &dll_lua_getmetatable},
422 {"lua_setfield", (luaV_function) &dll_lua_setfield},
423 {"lua_rawset", (luaV_function) &dll_lua_rawset},
424 {"lua_rawseti", (luaV_function) &dll_lua_rawseti},
425 {"lua_setmetatable", (luaV_function) &dll_lua_setmetatable},
Bram Moolenaarbd2f3c32012-04-06 14:31:00 +0200426 {"lua_next", (luaV_function) &dll_lua_next},
Bram Moolenaar2ab2e862019-12-04 21:24:53 +0100427 // libs
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200428 {"luaopen_base", (luaV_function) &dll_luaopen_base},
429 {"luaopen_table", (luaV_function) &dll_luaopen_table},
430 {"luaopen_string", (luaV_function) &dll_luaopen_string},
431 {"luaopen_math", (luaV_function) &dll_luaopen_math},
Bram Moolenaar16c98f92010-07-28 22:46:08 +0200432 {"luaopen_io", (luaV_function) &dll_luaopen_io},
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200433 {"luaopen_os", (luaV_function) &dll_luaopen_os},
434 {"luaopen_package", (luaV_function) &dll_luaopen_package},
435 {"luaopen_debug", (luaV_function) &dll_luaopen_debug},
Bram Moolenaar16c98f92010-07-28 22:46:08 +0200436 {"luaL_openlibs", (luaV_function) &dll_luaL_openlibs},
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200437 {NULL, NULL}
438};
439
Bram Moolenaar2334b6d2010-07-22 21:32:16 +0200440static HANDLE hinstLua = NULL;
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200441
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200442 static int
443lua_link_init(char *libname, int verbose)
444{
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200445 const luaV_Reg *reg;
446 if (hinstLua) return OK;
Bram Moolenaar2334b6d2010-07-22 21:32:16 +0200447 hinstLua = load_dll(libname);
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200448 if (!hinstLua)
449 {
450 if (verbose)
Martin Tournoij1a3e5742021-07-24 13:57:29 +0200451 semsg(_(e_loadlib), libname, load_dll_error());
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200452 return FAIL;
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200453 }
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200454 for (reg = luaV_dll; reg->func; reg++)
455 {
Bram Moolenaar2334b6d2010-07-22 21:32:16 +0200456 if ((*reg->func = symbol_from_dll(hinstLua, reg->name)) == NULL)
457 {
458 close_dll(hinstLua);
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200459 hinstLua = 0;
460 if (verbose)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100461 semsg(_(e_loadfunc), reg->name);
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200462 return FAIL;
463 }
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200464 }
465 return OK;
466}
Bram Moolenaar2ab2e862019-12-04 21:24:53 +0100467#endif // DYNAMIC_LUA
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200468
Bram Moolenaard90b6c02016-08-28 18:10:45 +0200469#if defined(DYNAMIC_LUA) || defined(PROTO)
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200470 int
471lua_enabled(int verbose)
472{
Bram Moolenaar25e4fcd2016-01-09 14:57:47 +0100473 return lua_link_init((char *)p_luadll, verbose) == OK;
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200474}
Bram Moolenaard90b6c02016-08-28 18:10:45 +0200475#endif
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200476
Bram Moolenaar5551b132020-07-14 21:54:28 +0200477#if LUA_VERSION_NUM > 501 && LUA_VERSION_NUM < 504
Bram Moolenaar1dced572012-04-05 16:54:08 +0200478 static int
Bram Moolenaar4eefe472019-03-19 21:59:19 +0100479luaL_typeerror(lua_State *L, int narg, const char *tname)
Bram Moolenaar1dced572012-04-05 16:54:08 +0200480{
481 const char *msg = lua_pushfstring(L, "%s expected, got %s",
Bram Moolenaardb913952012-06-29 12:54:53 +0200482 tname, luaL_typename(L, narg));
Bram Moolenaar1dced572012-04-05 16:54:08 +0200483 return luaL_argerror(L, narg, msg);
484}
485#endif
486
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200487
Bram Moolenaar2ab2e862019-12-04 21:24:53 +0100488// ======= Internal =======
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200489
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200490 static void
491luaV_newmetatable(lua_State *L, const char *tname)
492{
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200493 lua_newtable(L);
494 lua_pushlightuserdata(L, (void *) tname);
495 lua_pushvalue(L, -2);
496 lua_rawset(L, LUA_REGISTRYINDEX);
497}
498
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200499 static void *
500luaV_toudata(lua_State *L, int ud, const char *tname)
501{
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200502 void *p = lua_touserdata(L, ud);
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200503
Bram Moolenaar2ab2e862019-12-04 21:24:53 +0100504 if (p != NULL) // value is userdata?
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200505 {
Bram Moolenaar2ab2e862019-12-04 21:24:53 +0100506 if (lua_getmetatable(L, ud)) // does it have a metatable?
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200507 {
Bram Moolenaar2ab2e862019-12-04 21:24:53 +0100508 luaV_getfield(L, tname); // get metatable
509 if (lua_rawequal(L, -1, -2)) // MTs match?
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200510 {
Bram Moolenaar2ab2e862019-12-04 21:24:53 +0100511 lua_pop(L, 2); // MTs
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200512 return p;
513 }
514 }
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200515 }
516 return NULL;
517}
518
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200519 static void *
Bram Moolenaar1dced572012-04-05 16:54:08 +0200520luaV_checkcache(lua_State *L, void *p)
521{
522 luaV_getudata(L, p);
523 if (lua_isnil(L, -1)) luaL_error(L, "invalid object");
524 lua_pop(L, 1);
525 return p;
526}
527
528#define luaV_unbox(L,luatyp,ud) (*((luatyp *) lua_touserdata((L),(ud))))
529
530#define luaV_checkvalid(L,luatyp,ud) \
531 luaV_checkcache((L), (void *) luaV_unbox((L),luatyp,(ud)))
532
533 static void *
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200534luaV_checkudata(lua_State *L, int ud, const char *tname)
535{
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200536 void *p = luaV_toudata(L, ud, tname);
Bram Moolenaar1dced572012-04-05 16:54:08 +0200537 if (p == NULL) luaL_typeerror(L, ud, tname);
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200538 return p;
539}
540
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200541 static void
542luaV_pushtypval(lua_State *L, typval_T *tv)
543{
Bram Moolenaar1dced572012-04-05 16:54:08 +0200544 if (tv == NULL)
545 {
546 lua_pushnil(L);
547 return;
548 }
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200549 switch (tv->v_type)
550 {
551 case VAR_STRING:
Bram Moolenaard04da7c2012-10-14 03:41:59 +0200552 lua_pushstring(L, tv->vval.v_string == NULL
553 ? "" : (char *)tv->vval.v_string);
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200554 break;
555 case VAR_NUMBER:
556 lua_pushinteger(L, (int) tv->vval.v_number);
557 break;
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200558#ifdef FEAT_FLOAT
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200559 case VAR_FLOAT:
560 lua_pushnumber(L, (lua_Number) tv->vval.v_float);
561 break;
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200562#endif
Bram Moolenaar1dced572012-04-05 16:54:08 +0200563 case VAR_LIST:
564 luaV_pushlist(L, tv->vval.v_list);
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200565 break;
Bram Moolenaar1dced572012-04-05 16:54:08 +0200566 case VAR_DICT:
567 luaV_pushdict(L, tv->vval.v_dict);
568 break;
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +0100569 case VAR_BOOL:
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100570 case VAR_SPECIAL:
571 if (tv->vval.v_number <= VVAL_TRUE)
572 lua_pushinteger(L, (int) tv->vval.v_number);
573 else
574 lua_pushnil(L);
575 break;
Bram Moolenaarca06da92018-07-01 15:12:05 +0200576 case VAR_FUNC:
Bram Moolenaar4eefe472019-03-19 21:59:19 +0100577 luaV_pushfuncref(L, tv->vval.v_string);
Bram Moolenaarca06da92018-07-01 15:12:05 +0200578 break;
Bram Moolenaar86c3a212021-03-08 19:50:24 +0100579 case VAR_PARTIAL:
580 // TODO: handle partial arguments
581 luaV_pushfuncref(L, partial_name(tv->vval.v_partial));
582 break;
583
Bram Moolenaarb7828692019-03-23 13:57:02 +0100584 case VAR_BLOB:
585 luaV_pushblob(L, tv->vval.v_blob);
586 break;
Bram Moolenaar1dced572012-04-05 16:54:08 +0200587 default:
588 lua_pushnil(L);
589 }
590}
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200591
Bram Moolenaarca06da92018-07-01 15:12:05 +0200592/*
593 * Converts lua value at 'pos' to typval 'tv'.
594 * Returns OK or FAIL.
595 */
596 static int
597luaV_totypval(lua_State *L, int pos, typval_T *tv)
Bram Moolenaar1dced572012-04-05 16:54:08 +0200598{
Bram Moolenaarca06da92018-07-01 15:12:05 +0200599 int status = OK;
600
Bram Moolenaara9a8e5f2020-07-02 21:17:57 +0200601 tv->v_lock = 0;
602
Bram Moolenaarca06da92018-07-01 15:12:05 +0200603 switch (lua_type(L, pos))
604 {
Bram Moolenaar1dced572012-04-05 16:54:08 +0200605 case LUA_TBOOLEAN:
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +0100606 tv->v_type = VAR_BOOL;
Bram Moolenaar1dced572012-04-05 16:54:08 +0200607 tv->vval.v_number = (varnumber_T) lua_toboolean(L, pos);
608 break;
Bram Moolenaar9067cd62019-01-01 00:41:54 +0100609 case LUA_TNIL:
610 tv->v_type = VAR_SPECIAL;
611 tv->vval.v_number = VVAL_NULL;
612 break;
Bram Moolenaar1dced572012-04-05 16:54:08 +0200613 case LUA_TSTRING:
614 tv->v_type = VAR_STRING;
615 tv->vval.v_string = vim_strsave((char_u *) lua_tostring(L, pos));
616 break;
617 case LUA_TNUMBER:
618#ifdef FEAT_FLOAT
Bram Moolenaareb04f082020-05-17 14:32:35 +0200619 {
620 const lua_Number n = lua_tonumber(L, pos);
621
622 if (n > (lua_Number)INT64_MAX || n < (lua_Number)INT64_MIN
623 || ((lua_Number)((varnumber_T)n)) != n)
624 {
625 tv->v_type = VAR_FLOAT;
626 tv->vval.v_float = (float_T)n;
627 }
628 else
629 {
630 tv->v_type = VAR_NUMBER;
631 tv->vval.v_number = (varnumber_T)n;
632 }
633 }
Bram Moolenaar1dced572012-04-05 16:54:08 +0200634#else
635 tv->v_type = VAR_NUMBER;
636 tv->vval.v_number = (varnumber_T) lua_tointeger(L, pos);
637#endif
638 break;
Bram Moolenaar801ab062020-06-25 19:27:56 +0200639 case LUA_TFUNCTION:
640 {
641 char_u *name;
Bram Moolenaar066e7da2020-07-18 12:50:35 +0200642 luaV_CFuncState *state;
643
Bram Moolenaar801ab062020-06-25 19:27:56 +0200644 lua_pushvalue(L, pos);
Bram Moolenaar066e7da2020-07-18 12:50:35 +0200645 state = ALLOC_CLEAR_ONE(luaV_CFuncState);
Bram Moolenaar801ab062020-06-25 19:27:56 +0200646 state->lua_funcref = luaL_ref(L, LUA_REGISTRYINDEX);
647 state->L = L;
648 state->lua_tableref = LUA_NOREF;
649 name = register_cfunc(&luaV_call_lua_func,
650 &luaV_call_lua_func_free, state);
651 tv->v_type = VAR_FUNC;
652 tv->vval.v_string = vim_strsave(name);
653 break;
654 }
655 case LUA_TTABLE:
656 {
Bram Moolenaar066e7da2020-07-18 12:50:35 +0200657 int lua_tableref;
658
Bram Moolenaar801ab062020-06-25 19:27:56 +0200659 lua_pushvalue(L, pos);
Bram Moolenaar066e7da2020-07-18 12:50:35 +0200660 lua_tableref = luaL_ref(L, LUA_REGISTRYINDEX);
Bram Moolenaar801ab062020-06-25 19:27:56 +0200661 if (lua_getmetatable(L, pos)) {
662 lua_getfield(L, -1, LUA___CALL);
663 if (lua_isfunction(L, -1)) {
664 char_u *name;
665 int lua_funcref = luaL_ref(L, LUA_REGISTRYINDEX);
666 luaV_CFuncState *state = ALLOC_CLEAR_ONE(luaV_CFuncState);
Bram Moolenaar066e7da2020-07-18 12:50:35 +0200667
Bram Moolenaar801ab062020-06-25 19:27:56 +0200668 state->lua_funcref = lua_funcref;
669 state->L = L;
670 state->lua_tableref = lua_tableref;
671 name = register_cfunc(&luaV_call_lua_func,
672 &luaV_call_lua_func_free, state);
673 tv->v_type = VAR_FUNC;
674 tv->vval.v_string = vim_strsave(name);
675 break;
676 }
677 }
678 tv->v_type = VAR_NUMBER;
679 tv->vval.v_number = 0;
680 status = FAIL;
681 break;
682 }
Bram Moolenaarca06da92018-07-01 15:12:05 +0200683 case LUA_TUSERDATA:
684 {
Bram Moolenaar1dced572012-04-05 16:54:08 +0200685 void *p = lua_touserdata(L, pos);
Bram Moolenaarca06da92018-07-01 15:12:05 +0200686
Bram Moolenaarb7828692019-03-23 13:57:02 +0100687 if (lua_getmetatable(L, pos)) // has metatable?
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200688 {
Bram Moolenaarb7828692019-03-23 13:57:02 +0100689 // check list
Bram Moolenaar1dced572012-04-05 16:54:08 +0200690 luaV_getfield(L, LUAVIM_LIST);
691 if (lua_rawequal(L, -1, -2))
Bram Moolenaar2334b6d2010-07-22 21:32:16 +0200692 {
Bram Moolenaar1dced572012-04-05 16:54:08 +0200693 tv->v_type = VAR_LIST;
694 tv->vval.v_list = *((luaV_List *) p);
695 ++tv->vval.v_list->lv_refcount;
Bram Moolenaarb7828692019-03-23 13:57:02 +0100696 lua_pop(L, 2); // MTs
Bram Moolenaarca06da92018-07-01 15:12:05 +0200697 break;
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200698 }
Bram Moolenaarb7828692019-03-23 13:57:02 +0100699 // check dict
Bram Moolenaar1dced572012-04-05 16:54:08 +0200700 luaV_getfield(L, LUAVIM_DICT);
701 if (lua_rawequal(L, -1, -3))
702 {
703 tv->v_type = VAR_DICT;
704 tv->vval.v_dict = *((luaV_Dict *) p);
705 ++tv->vval.v_dict->dv_refcount;
Bram Moolenaarb7828692019-03-23 13:57:02 +0100706 lua_pop(L, 3); // MTs
Bram Moolenaarca06da92018-07-01 15:12:05 +0200707 break;
Bram Moolenaar1dced572012-04-05 16:54:08 +0200708 }
Bram Moolenaarb7828692019-03-23 13:57:02 +0100709 // check blob
710 luaV_getfield(L, LUAVIM_BLOB);
Bram Moolenaarca06da92018-07-01 15:12:05 +0200711 if (lua_rawequal(L, -1, -4))
712 {
Bram Moolenaarb7828692019-03-23 13:57:02 +0100713 tv->v_type = VAR_BLOB;
714 tv->vval.v_blob = *((luaV_Blob *) p);
715 ++tv->vval.v_blob->bv_refcount;
716 lua_pop(L, 4); // MTs
717 break;
718 }
719 // check funcref
720 luaV_getfield(L, LUAVIM_FUNCREF);
721 if (lua_rawequal(L, -1, -5))
722 {
Bram Moolenaarca06da92018-07-01 15:12:05 +0200723 luaV_Funcref *f = (luaV_Funcref *) p;
Bram Moolenaar066e7da2020-07-18 12:50:35 +0200724
Bram Moolenaar4eefe472019-03-19 21:59:19 +0100725 func_ref(f->name);
726 tv->v_type = VAR_FUNC;
727 tv->vval.v_string = vim_strsave(f->name);
Bram Moolenaarb7828692019-03-23 13:57:02 +0100728 lua_pop(L, 5); // MTs
Bram Moolenaarca06da92018-07-01 15:12:05 +0200729 break;
730 }
Bram Moolenaarb7828692019-03-23 13:57:02 +0100731 lua_pop(L, 4); // MTs
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200732 }
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200733 }
Bram Moolenaarb7828692019-03-23 13:57:02 +0100734 // FALLTHROUGH
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200735 default:
Bram Moolenaar1dced572012-04-05 16:54:08 +0200736 tv->v_type = VAR_NUMBER;
737 tv->vval.v_number = 0;
Bram Moolenaarca06da92018-07-01 15:12:05 +0200738 status = FAIL;
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200739 }
Bram Moolenaarca06da92018-07-01 15:12:05 +0200740 return status;
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200741}
742
Bram Moolenaar2ab2e862019-12-04 21:24:53 +0100743/*
744 * similar to luaL_addlstring, but replaces \0 with \n if toline and
745 * \n with \0 otherwise
746 */
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200747 static void
748luaV_addlstring(luaL_Buffer *b, const char *s, size_t l, int toline)
749{
750 while (l--)
751 {
752 if (*s == '\0' && toline)
753 luaL_addchar(b, '\n');
754 else if (*s == '\n' && !toline)
755 luaL_addchar(b, '\0');
756 else
757 luaL_addchar(b, *s);
758 s++;
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200759 }
760}
761
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200762 static void
763luaV_pushline(lua_State *L, buf_T *buf, linenr_T n)
764{
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200765 const char *s = (const char *) ml_get_buf(buf, n, FALSE);
766 luaL_Buffer b;
767 luaL_buffinit(L, &b);
768 luaV_addlstring(&b, s, strlen(s), 0);
769 luaL_pushresult(&b);
770}
771
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200772 static char_u *
773luaV_toline(lua_State *L, int pos)
774{
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200775 size_t l;
776 const char *s = lua_tolstring(L, pos, &l);
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200777
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200778 luaL_Buffer b;
779 luaL_buffinit(L, &b);
780 luaV_addlstring(&b, s, l, 1);
781 luaL_pushresult(&b);
782 return (char_u *) lua_tostring(L, -1);
783}
784
Bram Moolenaar2ab2e862019-12-04 21:24:53 +0100785/*
786 * pops a string s from the top of the stack and calls mf(t) for pieces t of
787 * s separated by newlines
788 */
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200789 static void
790luaV_msgfunc(lua_State *L, msgfunc_T mf)
791{
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200792 luaL_Buffer b;
793 size_t l;
794 const char *p, *s = lua_tolstring(L, -1, &l);
795 luaL_buffinit(L, &b);
796 luaV_addlstring(&b, s, l, 0);
797 luaL_pushresult(&b);
Bram Moolenaar2ab2e862019-12-04 21:24:53 +0100798 // break string
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200799 p = s = lua_tolstring(L, -1, &l);
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200800 while (l--)
801 {
Bram Moolenaar2ab2e862019-12-04 21:24:53 +0100802 if (*p++ == '\0') // break?
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200803 {
Bram Moolenaarc8970b92020-10-26 20:18:08 +0100804 mf((char *)s);
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200805 s = p;
806 }
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200807 }
Bram Moolenaarc8970b92020-10-26 20:18:08 +0100808 mf((char *)s);
Bram Moolenaar2ab2e862019-12-04 21:24:53 +0100809 lua_pop(L, 2); // original and modified strings
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200810}
811
Bram Moolenaar1dced572012-04-05 16:54:08 +0200812#define luaV_newtype(typ,tname,luatyp,luatname) \
813 static luatyp * \
Bram Moolenaar4eefe472019-03-19 21:59:19 +0100814 luaV_new##tname(lua_State *L, typ *obj) \
Bram Moolenaar1dced572012-04-05 16:54:08 +0200815 { \
816 luatyp *o = (luatyp *) lua_newuserdata(L, sizeof(luatyp)); \
817 *o = obj; \
818 luaV_setudata(L, obj); /* cache[obj] = udata */ \
819 luaV_getfield(L, luatname); \
820 lua_setmetatable(L, -2); \
821 return o; \
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200822 }
Bram Moolenaar1dced572012-04-05 16:54:08 +0200823
824#define luaV_pushtype(typ,tname,luatyp) \
825 static luatyp * \
Bram Moolenaarca06da92018-07-01 15:12:05 +0200826 luaV_push##tname(lua_State *L, typ *obj) \
Bram Moolenaar1dced572012-04-05 16:54:08 +0200827 { \
828 luatyp *o = NULL; \
829 if (obj == NULL) \
830 lua_pushnil(L); \
831 else { \
832 luaV_getudata(L, obj); \
833 if (lua_isnil(L, -1)) /* not interned? */ \
834 { \
835 lua_pop(L, 1); \
836 o = luaV_new##tname(L, obj); \
837 } \
838 else \
839 o = (luatyp *) lua_touserdata(L, -1); \
840 } \
841 return o; \
842 }
843
844#define luaV_type_tostring(tname,luatname) \
845 static int \
Bram Moolenaar4eefe472019-03-19 21:59:19 +0100846 luaV_##tname##_tostring(lua_State *L) \
Bram Moolenaar1dced572012-04-05 16:54:08 +0200847 { \
848 lua_pushfstring(L, "%s: %p", luatname, lua_touserdata(L, 1)); \
849 return 1; \
850 }
851
Bram Moolenaar2ab2e862019-12-04 21:24:53 +0100852// ======= List type =======
Bram Moolenaar1dced572012-04-05 16:54:08 +0200853
854 static luaV_List *
Bram Moolenaar4eefe472019-03-19 21:59:19 +0100855luaV_newlist(lua_State *L, list_T *lis)
Bram Moolenaar1dced572012-04-05 16:54:08 +0200856{
857 luaV_List *l = (luaV_List *) lua_newuserdata(L, sizeof(luaV_List));
858 *l = lis;
Bram Moolenaar2ab2e862019-12-04 21:24:53 +0100859 lis->lv_refcount++; // reference in Lua
860 luaV_setudata(L, lis); // cache[lis] = udata
Bram Moolenaar1dced572012-04-05 16:54:08 +0200861 luaV_getfield(L, LUAVIM_LIST);
862 lua_setmetatable(L, -2);
863 return l;
864}
865
866luaV_pushtype(list_T, list, luaV_List)
867luaV_type_tostring(list, LUAVIM_LIST)
868
869 static int
Bram Moolenaar4eefe472019-03-19 21:59:19 +0100870luaV_list_len(lua_State *L)
Bram Moolenaar1dced572012-04-05 16:54:08 +0200871{
872 list_T *l = luaV_unbox(L, luaV_List, 1);
Bram Moolenaarb7828692019-03-23 13:57:02 +0100873 lua_pushinteger(L, (int) list_len(l));
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200874 return 1;
875}
876
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200877 static int
Bram Moolenaar4eefe472019-03-19 21:59:19 +0100878luaV_list_iter(lua_State *L)
Bram Moolenaar1dced572012-04-05 16:54:08 +0200879{
880 listitem_T *li = (listitem_T *) lua_touserdata(L, lua_upvalueindex(2));
881 if (li == NULL) return 0;
882 luaV_pushtypval(L, &li->li_tv);
883 lua_pushlightuserdata(L, (void *) li->li_next);
884 lua_replace(L, lua_upvalueindex(2));
885 return 1;
886}
887
888 static int
Bram Moolenaar4eefe472019-03-19 21:59:19 +0100889luaV_list_call(lua_State *L)
Bram Moolenaar1dced572012-04-05 16:54:08 +0200890{
891 list_T *l = luaV_unbox(L, luaV_List, 1);
Bram Moolenaar2ab2e862019-12-04 21:24:53 +0100892 lua_pushvalue(L, lua_upvalueindex(1)); // pass cache table along
Bram Moolenaar1dced572012-04-05 16:54:08 +0200893 lua_pushlightuserdata(L, (void *) l->lv_first);
894 lua_pushcclosure(L, luaV_list_iter, 2);
895 return 1;
896}
897
898 static int
Bram Moolenaar4eefe472019-03-19 21:59:19 +0100899luaV_list_index(lua_State *L)
Bram Moolenaar1dced572012-04-05 16:54:08 +0200900{
901 list_T *l = luaV_unbox(L, luaV_List, 1);
Bram Moolenaar2ab2e862019-12-04 21:24:53 +0100902 if (lua_isnumber(L, 2)) // list item?
Bram Moolenaar1dced572012-04-05 16:54:08 +0200903 {
Bram Moolenaarbd846172020-06-27 12:32:57 +0200904 long n = (long) luaL_checkinteger(L, 2);
905 listitem_T *li;
906
907 // Lua array index starts with 1 while Vim uses 0, subtract 1 to
908 // normalize.
909 n -= 1;
910 li = list_find(l, n);
Bram Moolenaar1dced572012-04-05 16:54:08 +0200911 if (li == NULL)
912 lua_pushnil(L);
913 else
914 luaV_pushtypval(L, &li->li_tv);
915 }
Bram Moolenaar2ab2e862019-12-04 21:24:53 +0100916 else if (lua_isstring(L, 2)) // method?
Bram Moolenaar1dced572012-04-05 16:54:08 +0200917 {
918 const char *s = lua_tostring(L, 2);
919 if (strncmp(s, "add", 3) == 0
Bram Moolenaarb3766472013-04-15 13:49:21 +0200920 || strncmp(s, "insert", 6) == 0)
Bram Moolenaar1dced572012-04-05 16:54:08 +0200921 {
922 lua_getmetatable(L, 1);
923 lua_getfield(L, -1, s);
924 }
925 else
926 lua_pushnil(L);
927 }
928 else
929 lua_pushnil(L);
930 return 1;
931}
932
933 static int
Bram Moolenaar4eefe472019-03-19 21:59:19 +0100934luaV_list_newindex(lua_State *L)
Bram Moolenaar1dced572012-04-05 16:54:08 +0200935{
936 list_T *l = luaV_unbox(L, luaV_List, 1);
937 long n = (long) luaL_checkinteger(L, 2);
938 listitem_T *li;
Bram Moolenaarbd846172020-06-27 12:32:57 +0200939
940 // Lua array index starts with 1 while Vim uses 0, subtract 1 to normalize.
941 n -= 1;
942
Bram Moolenaar1dced572012-04-05 16:54:08 +0200943 if (l->lv_lock)
944 luaL_error(L, "list is locked");
945 li = list_find(l, n);
Bram Moolenaara1f9f862020-06-28 22:41:26 +0200946 if (li == NULL)
Bram Moolenaar1dced572012-04-05 16:54:08 +0200947 {
Bram Moolenaara1f9f862020-06-28 22:41:26 +0200948 if (!lua_isnil(L, 3))
949 {
950 typval_T v;
951 luaV_checktypval(L, 3, &v, "inserting list item");
952 if (list_insert_tv(l, &v, li) == FAIL)
953 luaL_error(L, "failed to add item to list");
954 clear_tv(&v);
955 }
Bram Moolenaar1dced572012-04-05 16:54:08 +0200956 }
957 else
958 {
Bram Moolenaara1f9f862020-06-28 22:41:26 +0200959 if (lua_isnil(L, 3)) // remove?
960 {
961 vimlist_remove(l, li, li);
962 listitem_free(l, li);
963 }
964 else
965 {
966 typval_T v;
967 luaV_checktypval(L, 3, &v, "setting list item");
968 clear_tv(&li->li_tv);
Bram Moolenaare49b8e82020-07-01 13:52:55 +0200969 li->li_tv = v;
Bram Moolenaara1f9f862020-06-28 22:41:26 +0200970 }
Bram Moolenaar1dced572012-04-05 16:54:08 +0200971 }
972 return 0;
973}
974
975 static int
Bram Moolenaar4eefe472019-03-19 21:59:19 +0100976luaV_list_add(lua_State *L)
Bram Moolenaar1dced572012-04-05 16:54:08 +0200977{
978 luaV_List *lis = luaV_checkudata(L, 1, LUAVIM_LIST);
979 list_T *l = (list_T *) luaV_checkcache(L, (void *) *lis);
Bram Moolenaarb3766472013-04-15 13:49:21 +0200980 typval_T v;
Bram Moolenaar1dced572012-04-05 16:54:08 +0200981 if (l->lv_lock)
982 luaL_error(L, "list is locked");
Bram Moolenaarb3766472013-04-15 13:49:21 +0200983 lua_settop(L, 2);
Bram Moolenaarca06da92018-07-01 15:12:05 +0200984 luaV_checktypval(L, 2, &v, "adding list item");
Bram Moolenaarb3766472013-04-15 13:49:21 +0200985 if (list_append_tv(l, &v) == FAIL)
Bram Moolenaarca06da92018-07-01 15:12:05 +0200986 luaL_error(L, "failed to add item to list");
Bram Moolenaarb3766472013-04-15 13:49:21 +0200987 clear_tv(&v);
Bram Moolenaar1dced572012-04-05 16:54:08 +0200988 lua_settop(L, 1);
989 return 1;
990}
991
992 static int
Bram Moolenaar4eefe472019-03-19 21:59:19 +0100993luaV_list_insert(lua_State *L)
Bram Moolenaar1dced572012-04-05 16:54:08 +0200994{
995 luaV_List *lis = luaV_checkudata(L, 1, LUAVIM_LIST);
996 list_T *l = (list_T *) luaV_checkcache(L, (void *) *lis);
Bram Moolenaar46538ee2015-02-17 16:28:55 +0100997 long pos = (long) luaL_optinteger(L, 3, 0);
Bram Moolenaar1dced572012-04-05 16:54:08 +0200998 listitem_T *li = NULL;
999 typval_T v;
1000 if (l->lv_lock)
1001 luaL_error(L, "list is locked");
1002 if (pos < l->lv_len)
1003 {
1004 li = list_find(l, pos);
1005 if (li == NULL)
1006 luaL_error(L, "invalid position");
1007 }
1008 lua_settop(L, 2);
Bram Moolenaarca06da92018-07-01 15:12:05 +02001009 luaV_checktypval(L, 2, &v, "inserting list item");
Bram Moolenaarb3766472013-04-15 13:49:21 +02001010 if (list_insert_tv(l, &v, li) == FAIL)
Bram Moolenaarca06da92018-07-01 15:12:05 +02001011 luaL_error(L, "failed to add item to list");
Bram Moolenaarb3766472013-04-15 13:49:21 +02001012 clear_tv(&v);
Bram Moolenaar1dced572012-04-05 16:54:08 +02001013 lua_settop(L, 1);
1014 return 1;
1015}
1016
1017static const luaL_Reg luaV_List_mt[] = {
1018 {"__tostring", luaV_list_tostring},
Bram Moolenaar1dced572012-04-05 16:54:08 +02001019 {"__len", luaV_list_len},
1020 {"__call", luaV_list_call},
1021 {"__index", luaV_list_index},
1022 {"__newindex", luaV_list_newindex},
1023 {"add", luaV_list_add},
1024 {"insert", luaV_list_insert},
1025 {NULL, NULL}
1026};
1027
1028
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001029// ======= Dict type =======
Bram Moolenaar1dced572012-04-05 16:54:08 +02001030
1031 static luaV_Dict *
Bram Moolenaar4eefe472019-03-19 21:59:19 +01001032luaV_newdict(lua_State *L, dict_T *dic)
Bram Moolenaar1dced572012-04-05 16:54:08 +02001033{
1034 luaV_Dict *d = (luaV_Dict *) lua_newuserdata(L, sizeof(luaV_Dict));
1035 *d = dic;
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001036 dic->dv_refcount++; // reference in Lua
1037 luaV_setudata(L, dic); // cache[dic] = udata
Bram Moolenaar1dced572012-04-05 16:54:08 +02001038 luaV_getfield(L, LUAVIM_DICT);
1039 lua_setmetatable(L, -2);
1040 return d;
1041}
1042
1043luaV_pushtype(dict_T, dict, luaV_Dict)
1044luaV_type_tostring(dict, LUAVIM_DICT)
1045
1046 static int
Bram Moolenaar4eefe472019-03-19 21:59:19 +01001047luaV_dict_len(lua_State *L)
Bram Moolenaar1dced572012-04-05 16:54:08 +02001048{
1049 dict_T *d = luaV_unbox(L, luaV_Dict, 1);
Bram Moolenaarb7828692019-03-23 13:57:02 +01001050 lua_pushinteger(L, (int) dict_len(d));
Bram Moolenaar1dced572012-04-05 16:54:08 +02001051 return 1;
1052}
1053
1054 static int
Bram Moolenaar4eefe472019-03-19 21:59:19 +01001055luaV_dict_iter(lua_State *L UNUSED)
Bram Moolenaar1dced572012-04-05 16:54:08 +02001056{
Bram Moolenaarfeeaa682013-02-14 22:19:51 +01001057#ifdef FEAT_EVAL
Bram Moolenaar1dced572012-04-05 16:54:08 +02001058 hashitem_T *hi = (hashitem_T *) lua_touserdata(L, lua_upvalueindex(2));
1059 int n = lua_tointeger(L, lua_upvalueindex(3));
1060 dictitem_T *di;
1061 if (n <= 0) return 0;
1062 while (HASHITEM_EMPTY(hi)) hi++;
1063 di = dict_lookup(hi);
1064 lua_pushstring(L, (char *) hi->hi_key);
1065 luaV_pushtypval(L, &di->di_tv);
1066 lua_pushlightuserdata(L, (void *) (hi + 1));
1067 lua_replace(L, lua_upvalueindex(2));
1068 lua_pushinteger(L, n - 1);
1069 lua_replace(L, lua_upvalueindex(3));
1070 return 2;
Bram Moolenaarfeeaa682013-02-14 22:19:51 +01001071#else
1072 return 0;
1073#endif
Bram Moolenaar1dced572012-04-05 16:54:08 +02001074}
1075
1076 static int
Bram Moolenaar4eefe472019-03-19 21:59:19 +01001077luaV_dict_call(lua_State *L)
Bram Moolenaar1dced572012-04-05 16:54:08 +02001078{
1079 dict_T *d = luaV_unbox(L, luaV_Dict, 1);
1080 hashtab_T *ht = &d->dv_hashtab;
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001081 lua_pushvalue(L, lua_upvalueindex(1)); // pass cache table along
Bram Moolenaar1dced572012-04-05 16:54:08 +02001082 lua_pushlightuserdata(L, (void *) ht->ht_array);
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001083 lua_pushinteger(L, ht->ht_used); // # remaining items
Bram Moolenaar1dced572012-04-05 16:54:08 +02001084 lua_pushcclosure(L, luaV_dict_iter, 3);
1085 return 1;
1086}
1087
1088 static int
Bram Moolenaarca06da92018-07-01 15:12:05 +02001089luaV_dict_index(lua_State *L)
Bram Moolenaar1dced572012-04-05 16:54:08 +02001090{
1091 dict_T *d = luaV_unbox(L, luaV_Dict, 1);
1092 char_u *key = (char_u *) luaL_checkstring(L, 2);
1093 dictitem_T *di = dict_find(d, key, -1);
Bram Moolenaarca06da92018-07-01 15:12:05 +02001094
Bram Moolenaar1dced572012-04-05 16:54:08 +02001095 if (di == NULL)
1096 lua_pushnil(L);
1097 else
Bram Moolenaarca06da92018-07-01 15:12:05 +02001098 {
Bram Moolenaar1dced572012-04-05 16:54:08 +02001099 luaV_pushtypval(L, &di->di_tv);
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001100 if (di->di_tv.v_type == VAR_FUNC) // funcref?
Bram Moolenaarca06da92018-07-01 15:12:05 +02001101 {
1102 luaV_Funcref *f = (luaV_Funcref *) lua_touserdata(L, -1);
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001103 f->self = d; // keep "self" reference
Bram Moolenaarca06da92018-07-01 15:12:05 +02001104 d->dv_refcount++;
1105 }
1106 }
Bram Moolenaar1dced572012-04-05 16:54:08 +02001107 return 1;
1108}
1109
1110 static int
Bram Moolenaarca06da92018-07-01 15:12:05 +02001111luaV_dict_newindex(lua_State *L)
Bram Moolenaar1dced572012-04-05 16:54:08 +02001112{
1113 dict_T *d = luaV_unbox(L, luaV_Dict, 1);
1114 char_u *key = (char_u *) luaL_checkstring(L, 2);
1115 dictitem_T *di;
Bram Moolenaare49b8e82020-07-01 13:52:55 +02001116 typval_T tv;
Bram Moolenaar713bf9e92019-03-16 16:38:41 +01001117
Bram Moolenaar1dced572012-04-05 16:54:08 +02001118 if (d->dv_lock)
1119 luaL_error(L, "dict is locked");
Bram Moolenaard6ef5f92018-07-13 22:08:23 +02001120 if (key == NULL)
1121 return 0;
1122 if (*key == NUL)
Bram Moolenaarca06da92018-07-01 15:12:05 +02001123 luaL_error(L, "empty key");
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001124 if (!lua_isnil(L, 3)) // read value?
Bram Moolenaar17413672018-07-14 20:49:42 +02001125 {
Bram Moolenaare49b8e82020-07-01 13:52:55 +02001126 luaV_checktypval(L, 3, &tv, "setting dict item");
1127 if (d->dv_scope == VAR_DEF_SCOPE && tv.v_type == VAR_FUNC)
1128 {
1129 clear_tv(&tv);
Bram Moolenaarca06da92018-07-01 15:12:05 +02001130 luaL_error(L, "cannot assign funcref to builtin scope");
Bram Moolenaare49b8e82020-07-01 13:52:55 +02001131 }
Bram Moolenaarca06da92018-07-01 15:12:05 +02001132 }
Bram Moolenaar1dced572012-04-05 16:54:08 +02001133 di = dict_find(d, key, -1);
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001134 if (di == NULL) // non-existing key?
Bram Moolenaar1dced572012-04-05 16:54:08 +02001135 {
Bram Moolenaard6ef5f92018-07-13 22:08:23 +02001136 if (lua_isnil(L, 3))
1137 return 0;
Bram Moolenaar1dced572012-04-05 16:54:08 +02001138 di = dictitem_alloc(key);
Bram Moolenaard6ef5f92018-07-13 22:08:23 +02001139 if (di == NULL)
Bram Moolenaare49b8e82020-07-01 13:52:55 +02001140 {
1141 clear_tv(&tv);
Bram Moolenaard6ef5f92018-07-13 22:08:23 +02001142 return 0;
Bram Moolenaare49b8e82020-07-01 13:52:55 +02001143 }
Bram Moolenaar1dced572012-04-05 16:54:08 +02001144 if (dict_add(d, di) == FAIL)
1145 {
Bram Moolenaard6ef5f92018-07-13 22:08:23 +02001146 vim_free(di);
Bram Moolenaare49b8e82020-07-01 13:52:55 +02001147 clear_tv(&tv);
Bram Moolenaard6ef5f92018-07-13 22:08:23 +02001148 return 0;
Bram Moolenaar1dced572012-04-05 16:54:08 +02001149 }
1150 }
1151 else
1152 clear_tv(&di->di_tv);
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001153 if (lua_isnil(L, 3)) // remove?
Bram Moolenaar1dced572012-04-05 16:54:08 +02001154 {
1155 hashitem_T *hi = hash_find(&d->dv_hashtab, di->di_key);
1156 hash_remove(&d->dv_hashtab, hi);
1157 dictitem_free(di);
1158 }
Bram Moolenaarca06da92018-07-01 15:12:05 +02001159 else
Bram Moolenaare49b8e82020-07-01 13:52:55 +02001160 di->di_tv = tv;
Bram Moolenaar1dced572012-04-05 16:54:08 +02001161 return 0;
1162}
1163
1164static const luaL_Reg luaV_Dict_mt[] = {
1165 {"__tostring", luaV_dict_tostring},
Bram Moolenaar1dced572012-04-05 16:54:08 +02001166 {"__len", luaV_dict_len},
1167 {"__call", luaV_dict_call},
1168 {"__index", luaV_dict_index},
1169 {"__newindex", luaV_dict_newindex},
1170 {NULL, NULL}
1171};
1172
1173
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001174// ======= Blob type =======
Bram Moolenaarb7828692019-03-23 13:57:02 +01001175
1176 static luaV_Blob *
1177luaV_newblob(lua_State *L, blob_T *blo)
1178{
1179 luaV_Blob *b = (luaV_Blob *) lua_newuserdata(L, sizeof(luaV_Blob));
1180 *b = blo;
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001181 blo->bv_refcount++; // reference in Lua
1182 luaV_setudata(L, blo); // cache[blo] = udata
Bram Moolenaarb7828692019-03-23 13:57:02 +01001183 luaV_getfield(L, LUAVIM_BLOB);
1184 lua_setmetatable(L, -2);
1185 return b;
1186}
1187
1188luaV_pushtype(blob_T, blob, luaV_Blob)
1189luaV_type_tostring(blob, LUAVIM_BLOB)
1190
1191 static int
1192luaV_blob_gc(lua_State *L)
1193{
1194 blob_T *b = luaV_unbox(L, luaV_Blob, 1);
1195 blob_unref(b);
1196 return 0;
1197}
1198
1199 static int
1200luaV_blob_len(lua_State *L)
1201{
1202 blob_T *b = luaV_unbox(L, luaV_Blob, 1);
1203 lua_pushinteger(L, (int) blob_len(b));
1204 return 1;
1205}
1206
1207 static int
1208luaV_blob_index(lua_State *L)
1209{
1210 blob_T *b = luaV_unbox(L, luaV_Blob, 1);
1211 if (lua_isnumber(L, 2))
1212 {
1213 int idx = luaL_checkinteger(L, 2);
1214 if (idx < blob_len(b))
1215 lua_pushnumber(L, (lua_Number) blob_get(b, idx));
1216 else
1217 lua_pushnil(L);
1218 }
1219 else if (lua_isstring(L, 2))
1220 {
1221 const char *s = lua_tostring(L, 2);
1222 if (strncmp(s, "add", 3) == 0)
1223 {
1224 lua_getmetatable(L, 1);
1225 lua_getfield(L, -1, s);
1226 }
1227 else
1228 lua_pushnil(L);
1229 }
1230 else
1231 lua_pushnil(L);
1232 return 1;
1233}
1234
1235 static int
1236luaV_blob_newindex(lua_State *L)
1237{
1238 blob_T *b = luaV_unbox(L, luaV_Blob, 1);
1239 if (b->bv_lock)
1240 luaL_error(L, "blob is locked");
1241 if (lua_isnumber(L, 2))
1242 {
1243 long len = blob_len(b);
1244 int idx = luaL_checkinteger(L, 2);
1245 int val = luaL_checkinteger(L, 3);
1246 if (idx < len || (idx == len && ga_grow(&b->bv_ga, 1) == OK))
1247 {
1248 blob_set(b, idx, (char_u) val);
1249 if (idx == len)
1250 ++b->bv_ga.ga_len;
1251 }
1252 else
1253 luaL_error(L, "index out of range");
1254 }
1255 return 0;
1256}
1257
1258 static int
1259luaV_blob_add(lua_State *L)
1260{
1261 luaV_Blob *blo = luaV_checkudata(L, 1, LUAVIM_BLOB);
1262 blob_T *b = (blob_T *) luaV_checkcache(L, (void *) *blo);
1263 if (b->bv_lock)
1264 luaL_error(L, "blob is locked");
1265 lua_settop(L, 2);
1266 if (!lua_isstring(L, 2))
1267 luaL_error(L, "string expected, got %s", luaL_typename(L, 2));
1268 else
1269 {
1270 size_t i, l = 0;
1271 const char *s = lua_tolstring(L, 2, &l);
1272
Bram Moolenaar5f1d3ae2020-02-11 22:37:35 +01001273 if (ga_grow(&b->bv_ga, (int)l) == OK)
Bram Moolenaar6fb5c972019-03-26 21:44:20 +01001274 for (i = 0; i < l; ++i)
1275 ga_append(&b->bv_ga, s[i]);
Bram Moolenaarb7828692019-03-23 13:57:02 +01001276 }
1277 lua_settop(L, 1);
1278 return 1;
1279}
1280
1281static const luaL_Reg luaV_Blob_mt[] = {
1282 {"__tostring", luaV_blob_tostring},
1283 {"__gc", luaV_blob_gc},
1284 {"__len", luaV_blob_len},
1285 {"__index", luaV_blob_index},
1286 {"__newindex", luaV_blob_newindex},
1287 {"add", luaV_blob_add},
1288 {NULL, NULL}
1289};
1290
1291
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001292// ======= Funcref type =======
Bram Moolenaarca06da92018-07-01 15:12:05 +02001293
1294 static luaV_Funcref *
1295luaV_newfuncref(lua_State *L, char_u *name)
1296{
1297 luaV_Funcref *f = (luaV_Funcref *)lua_newuserdata(L, sizeof(luaV_Funcref));
1298
1299 if (name != NULL)
1300 {
Bram Moolenaar4eefe472019-03-19 21:59:19 +01001301 func_ref(name);
1302 f->name = vim_strsave(name);
Bram Moolenaarca06da92018-07-01 15:12:05 +02001303 }
Bram Moolenaarca06da92018-07-01 15:12:05 +02001304 f->self = NULL;
1305 luaV_getfield(L, LUAVIM_FUNCREF);
1306 lua_setmetatable(L, -2);
1307 return f;
1308}
1309
1310 static luaV_Funcref *
Bram Moolenaar4eefe472019-03-19 21:59:19 +01001311luaV_pushfuncref(lua_State *L, char_u *name)
Bram Moolenaarca06da92018-07-01 15:12:05 +02001312{
Bram Moolenaar4eefe472019-03-19 21:59:19 +01001313 return luaV_newfuncref(L, name);
Bram Moolenaarca06da92018-07-01 15:12:05 +02001314}
1315
1316
1317luaV_type_tostring(funcref, LUAVIM_FUNCREF)
1318
1319 static int
1320luaV_funcref_gc(lua_State *L)
1321{
1322 luaV_Funcref *f = (luaV_Funcref *) lua_touserdata(L, 1);
1323
Bram Moolenaar4eefe472019-03-19 21:59:19 +01001324 func_unref(f->name);
1325 vim_free(f->name);
1326 // NOTE: Don't call "dict_unref(f->self)", because the dict of "f->self"
1327 // will be (or has been already) freed by Vim's garbage collection.
Bram Moolenaarca06da92018-07-01 15:12:05 +02001328 return 0;
1329}
1330
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001331// equivalent to string(funcref)
Bram Moolenaarca06da92018-07-01 15:12:05 +02001332 static int
1333luaV_funcref_len(lua_State *L)
1334{
1335 luaV_Funcref *f = (luaV_Funcref *) lua_touserdata(L, 1);
1336
Bram Moolenaar4eefe472019-03-19 21:59:19 +01001337 lua_pushstring(L, (const char *) f->name);
Bram Moolenaarca06da92018-07-01 15:12:05 +02001338 return 1;
1339}
1340
1341 static int
1342luaV_funcref_call(lua_State *L)
1343{
1344 luaV_Funcref *f = (luaV_Funcref *) lua_touserdata(L, 1);
Bram Moolenaar4eefe472019-03-19 21:59:19 +01001345 int i, n = lua_gettop(L) - 1; // #args
1346 int status = FAIL;
1347 typval_T args;
1348 typval_T rettv;
Bram Moolenaarca06da92018-07-01 15:12:05 +02001349
Bram Moolenaar4eefe472019-03-19 21:59:19 +01001350 args.v_type = VAR_LIST;
1351 args.vval.v_list = list_alloc();
1352 rettv.v_type = VAR_UNKNOWN; // as in clear_tv
1353 if (args.vval.v_list != NULL)
Bram Moolenaard6ef5f92018-07-13 22:08:23 +02001354 {
Bram Moolenaar4eefe472019-03-19 21:59:19 +01001355 typval_T v;
1356
Bram Moolenaar17413672018-07-14 20:49:42 +02001357 for (i = 0; i < n; i++)
1358 {
Bram Moolenaard6ef5f92018-07-13 22:08:23 +02001359 luaV_checktypval(L, i + 2, &v, "calling funcref");
Bram Moolenaar4eefe472019-03-19 21:59:19 +01001360 list_append_tv(args.vval.v_list, &v);
Bram Moolenaar713bf9e92019-03-16 16:38:41 +01001361 clear_tv(&v);
Bram Moolenaard6ef5f92018-07-13 22:08:23 +02001362 }
Bram Moolenaar4eefe472019-03-19 21:59:19 +01001363 status = func_call(f->name, &args, NULL, f->self, &rettv);
Bram Moolenaard6ef5f92018-07-13 22:08:23 +02001364 if (status == OK)
1365 luaV_pushtypval(L, &rettv);
Bram Moolenaar4eefe472019-03-19 21:59:19 +01001366 clear_tv(&args);
Bram Moolenaard6ef5f92018-07-13 22:08:23 +02001367 clear_tv(&rettv);
Bram Moolenaarca06da92018-07-01 15:12:05 +02001368 }
Bram Moolenaarca06da92018-07-01 15:12:05 +02001369 if (status != OK)
1370 luaL_error(L, "cannot call funcref");
1371 return 1;
1372}
1373
1374static const luaL_Reg luaV_Funcref_mt[] = {
1375 {"__tostring", luaV_funcref_tostring},
1376 {"__gc", luaV_funcref_gc},
1377 {"__len", luaV_funcref_len},
1378 {"__call", luaV_funcref_call},
1379 {NULL, NULL}
1380};
1381
1382
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001383// ======= Buffer type =======
Bram Moolenaar1dced572012-04-05 16:54:08 +02001384
1385luaV_newtype(buf_T, buffer, luaV_Buffer, LUAVIM_BUFFER)
1386luaV_pushtype(buf_T, buffer, luaV_Buffer)
1387luaV_type_tostring(buffer, LUAVIM_BUFFER)
1388
1389 static int
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001390luaV_buffer_len(lua_State *L)
1391{
Bram Moolenaar1dced572012-04-05 16:54:08 +02001392 buf_T *b = (buf_T *) luaV_checkvalid(L, luaV_Buffer, 1);
1393 lua_pushinteger(L, b->b_ml.ml_line_count);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001394 return 1;
1395}
1396
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001397 static int
1398luaV_buffer_call(lua_State *L)
1399{
Bram Moolenaar1dced572012-04-05 16:54:08 +02001400 buf_T *b = (buf_T *) luaV_checkvalid(L, luaV_Buffer, 1);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001401 lua_settop(L, 1);
Bram Moolenaar1dced572012-04-05 16:54:08 +02001402 set_curbuf(b, DOBUF_SPLIT);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001403 return 1;
1404}
1405
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001406 static int
1407luaV_buffer_index(lua_State *L)
1408{
Bram Moolenaar1dced572012-04-05 16:54:08 +02001409 buf_T *b = (buf_T *) luaV_checkvalid(L, luaV_Buffer, 1);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001410 linenr_T n = (linenr_T) lua_tointeger(L, 2);
Bram Moolenaar1dced572012-04-05 16:54:08 +02001411 if (n > 0 && n <= b->b_ml.ml_line_count)
1412 luaV_pushline(L, b, n);
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001413 else if (lua_isstring(L, 2))
1414 {
1415 const char *s = lua_tostring(L, 2);
1416 if (strncmp(s, "name", 4) == 0)
Bram Moolenaarfe08df42018-07-07 23:07:41 +02001417 lua_pushstring(L, (b->b_sfname == NULL)
1418 ? "" : (char *) b->b_sfname);
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001419 else if (strncmp(s, "fname", 5) == 0)
Bram Moolenaarfe08df42018-07-07 23:07:41 +02001420 lua_pushstring(L, (b->b_ffname == NULL)
1421 ? "" : (char *) b->b_ffname);
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001422 else if (strncmp(s, "number", 6) == 0)
Bram Moolenaar1dced572012-04-05 16:54:08 +02001423 lua_pushinteger(L, b->b_fnum);
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001424 // methods
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001425 else if (strncmp(s, "insert", 6) == 0
1426 || strncmp(s, "next", 4) == 0
1427 || strncmp(s, "previous", 8) == 0
1428 || strncmp(s, "isvalid", 7) == 0)
1429 {
1430 lua_getmetatable(L, 1);
1431 lua_getfield(L, -1, s);
1432 }
1433 else
1434 lua_pushnil(L);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001435 }
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001436 else
1437 lua_pushnil(L);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001438 return 1;
1439}
1440
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001441 static int
1442luaV_buffer_newindex(lua_State *L)
1443{
Bram Moolenaar1dced572012-04-05 16:54:08 +02001444 buf_T *b = (buf_T *) luaV_checkvalid(L, luaV_Buffer, 1);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001445 linenr_T n = (linenr_T) luaL_checkinteger(L, 2);
1446#ifdef HAVE_SANDBOX
1447 luaV_checksandbox(L);
1448#endif
Bram Moolenaar1dced572012-04-05 16:54:08 +02001449 if (n < 1 || n > b->b_ml.ml_line_count)
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001450 luaL_error(L, "invalid line number");
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001451 if (lua_isnil(L, 3)) // delete line
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001452 {
1453 buf_T *buf = curbuf;
Bram Moolenaar1dced572012-04-05 16:54:08 +02001454 curbuf = b;
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001455 if (u_savedel(n, 1L) == FAIL)
1456 {
1457 curbuf = buf;
1458 luaL_error(L, "cannot save undo information");
1459 }
Bram Moolenaarca70c072020-05-30 20:30:46 +02001460 else if (ml_delete(n) == FAIL)
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001461 {
1462 curbuf = buf;
1463 luaL_error(L, "cannot delete line");
1464 }
Bram Moolenaarca06da92018-07-01 15:12:05 +02001465 else
1466 {
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001467 deleted_lines_mark(n, 1L);
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001468 if (b == curwin->w_buffer) // fix cursor in current window?
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001469 {
1470 if (curwin->w_cursor.lnum >= n)
1471 {
1472 if (curwin->w_cursor.lnum > n)
1473 {
1474 curwin->w_cursor.lnum -= 1;
1475 check_cursor_col();
1476 }
Bram Moolenaare49b8e82020-07-01 13:52:55 +02001477 else
1478 check_cursor();
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001479 changed_cline_bef_curs();
1480 }
1481 invalidate_botline();
1482 }
1483 }
1484 curbuf = buf;
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001485 }
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001486 else if (lua_isstring(L, 3)) // update line
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001487 {
1488 buf_T *buf = curbuf;
Bram Moolenaar1dced572012-04-05 16:54:08 +02001489 curbuf = b;
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001490 if (u_savesub(n) == FAIL)
1491 {
1492 curbuf = buf;
1493 luaL_error(L, "cannot save undo information");
1494 }
1495 else if (ml_replace(n, luaV_toline(L, 3), TRUE) == FAIL)
1496 {
1497 curbuf = buf;
1498 luaL_error(L, "cannot replace line");
1499 }
1500 else changed_bytes(n, 0);
1501 curbuf = buf;
Bram Moolenaar1dced572012-04-05 16:54:08 +02001502 if (b == curwin->w_buffer)
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001503 check_cursor_col();
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001504 }
1505 else
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001506 luaL_error(L, "wrong argument to change line");
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001507 return 0;
1508}
1509
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001510 static int
1511luaV_buffer_insert(lua_State *L)
1512{
Bram Moolenaar1dced572012-04-05 16:54:08 +02001513 luaV_Buffer *lb = luaV_checkudata(L, 1, LUAVIM_BUFFER);
1514 buf_T *b = (buf_T *) luaV_checkcache(L, (void *) *lb);
1515 linenr_T last = b->b_ml.ml_line_count;
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001516 linenr_T n = (linenr_T) luaL_optinteger(L, 3, last);
1517 buf_T *buf;
1518 luaL_checktype(L, 2, LUA_TSTRING);
1519#ifdef HAVE_SANDBOX
1520 luaV_checksandbox(L);
1521#endif
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001522 // fix insertion line
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001523 if (n < 0) n = 0;
1524 if (n > last) n = last;
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001525 // insert
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001526 buf = curbuf;
Bram Moolenaar1dced572012-04-05 16:54:08 +02001527 curbuf = b;
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001528 if (u_save(n, n + 1) == FAIL)
1529 {
1530 curbuf = buf;
1531 luaL_error(L, "cannot save undo information");
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001532 }
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001533 else if (ml_append(n, luaV_toline(L, 2), 0, FALSE) == FAIL)
1534 {
1535 curbuf = buf;
1536 luaL_error(L, "cannot insert line");
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001537 }
1538 else
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001539 appended_lines_mark(n, 1L);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001540 curbuf = buf;
1541 update_screen(VALID);
1542 return 0;
1543}
1544
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001545 static int
1546luaV_buffer_next(lua_State *L)
1547{
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001548 luaV_Buffer *b = luaV_checkudata(L, 1, LUAVIM_BUFFER);
Bram Moolenaar1dced572012-04-05 16:54:08 +02001549 buf_T *buf = (buf_T *) luaV_checkcache(L, (void *) *b);
1550 luaV_pushbuffer(L, buf->b_next);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001551 return 1;
1552}
1553
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001554 static int
1555luaV_buffer_previous(lua_State *L)
1556{
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001557 luaV_Buffer *b = luaV_checkudata(L, 1, LUAVIM_BUFFER);
Bram Moolenaar1dced572012-04-05 16:54:08 +02001558 buf_T *buf = (buf_T *) luaV_checkcache(L, (void *) *b);
1559 luaV_pushbuffer(L, buf->b_prev);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001560 return 1;
1561}
1562
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001563 static int
1564luaV_buffer_isvalid(lua_State *L)
1565{
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001566 luaV_Buffer *b = luaV_checkudata(L, 1, LUAVIM_BUFFER);
Bram Moolenaar1dced572012-04-05 16:54:08 +02001567 luaV_getudata(L, *b);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001568 lua_pushboolean(L, !lua_isnil(L, -1));
1569 return 1;
1570}
1571
1572static const luaL_Reg luaV_Buffer_mt[] = {
1573 {"__tostring", luaV_buffer_tostring},
1574 {"__len", luaV_buffer_len},
1575 {"__call", luaV_buffer_call},
1576 {"__index", luaV_buffer_index},
1577 {"__newindex", luaV_buffer_newindex},
1578 {"insert", luaV_buffer_insert},
1579 {"next", luaV_buffer_next},
1580 {"previous", luaV_buffer_previous},
1581 {"isvalid", luaV_buffer_isvalid},
1582 {NULL, NULL}
1583};
1584
1585
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001586// ======= Window type =======
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001587
Bram Moolenaar1dced572012-04-05 16:54:08 +02001588luaV_newtype(win_T, window, luaV_Window, LUAVIM_WINDOW)
1589luaV_pushtype(win_T, window, luaV_Window)
1590luaV_type_tostring(window, LUAVIM_WINDOW)
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001591
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001592 static int
1593luaV_window_call(lua_State *L)
1594{
Bram Moolenaar1dced572012-04-05 16:54:08 +02001595 win_T *w = (win_T *) luaV_checkvalid(L, luaV_Window, 1);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001596 lua_settop(L, 1);
Bram Moolenaar1dced572012-04-05 16:54:08 +02001597 win_goto(w);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001598 return 1;
1599}
1600
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001601 static int
1602luaV_window_index(lua_State *L)
1603{
Bram Moolenaar1dced572012-04-05 16:54:08 +02001604 win_T *w = (win_T *) luaV_checkvalid(L, luaV_Window, 1);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001605 const char *s = luaL_checkstring(L, 2);
1606 if (strncmp(s, "buffer", 6) == 0)
Bram Moolenaar1dced572012-04-05 16:54:08 +02001607 luaV_pushbuffer(L, w->w_buffer);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001608 else if (strncmp(s, "line", 4) == 0)
Bram Moolenaar1dced572012-04-05 16:54:08 +02001609 lua_pushinteger(L, w->w_cursor.lnum);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001610 else if (strncmp(s, "col", 3) == 0)
Bram Moolenaar1dced572012-04-05 16:54:08 +02001611 lua_pushinteger(L, w->w_cursor.col + 1);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001612 else if (strncmp(s, "width", 5) == 0)
Bram Moolenaar02631462017-09-22 15:20:32 +02001613 lua_pushinteger(L, w->w_width);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001614 else if (strncmp(s, "height", 6) == 0)
Bram Moolenaar1dced572012-04-05 16:54:08 +02001615 lua_pushinteger(L, w->w_height);
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001616 // methods
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001617 else if (strncmp(s, "next", 4) == 0
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001618 || strncmp(s, "previous", 8) == 0
1619 || strncmp(s, "isvalid", 7) == 0)
1620 {
1621 lua_getmetatable(L, 1);
1622 lua_getfield(L, -1, s);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001623 }
1624 else
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001625 lua_pushnil(L);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001626 return 1;
1627}
1628
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001629 static int
Bram Moolenaar4eefe472019-03-19 21:59:19 +01001630luaV_window_newindex(lua_State *L)
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001631{
Bram Moolenaar1dced572012-04-05 16:54:08 +02001632 win_T *w = (win_T *) luaV_checkvalid(L, luaV_Window, 1);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001633 const char *s = luaL_checkstring(L, 2);
1634 int v = luaL_checkinteger(L, 3);
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001635 if (strncmp(s, "line", 4) == 0)
1636 {
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001637#ifdef HAVE_SANDBOX
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001638 luaV_checksandbox(L);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001639#endif
Bram Moolenaar1dced572012-04-05 16:54:08 +02001640 if (v < 1 || v > w->w_buffer->b_ml.ml_line_count)
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001641 luaL_error(L, "line out of range");
Bram Moolenaar1dced572012-04-05 16:54:08 +02001642 w->w_cursor.lnum = v;
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001643 update_screen(VALID);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001644 }
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001645 else if (strncmp(s, "col", 3) == 0)
1646 {
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001647#ifdef HAVE_SANDBOX
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001648 luaV_checksandbox(L);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001649#endif
Bram Moolenaar1dced572012-04-05 16:54:08 +02001650 w->w_cursor.col = v - 1;
Bram Moolenaar53901442018-07-25 22:02:36 +02001651 w->w_set_curswant = TRUE;
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001652 update_screen(VALID);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001653 }
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001654 else if (strncmp(s, "width", 5) == 0)
1655 {
1656 win_T *win = curwin;
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001657#ifdef FEAT_GUI
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001658 need_mouse_correct = TRUE;
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001659#endif
Bram Moolenaar1dced572012-04-05 16:54:08 +02001660 curwin = w;
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001661 win_setwidth(v);
1662 curwin = win;
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001663 }
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001664 else if (strncmp(s, "height", 6) == 0)
1665 {
1666 win_T *win = curwin;
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001667#ifdef FEAT_GUI
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001668 need_mouse_correct = TRUE;
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001669#endif
Bram Moolenaar1dced572012-04-05 16:54:08 +02001670 curwin = w;
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001671 win_setheight(v);
1672 curwin = win;
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001673 }
1674 else
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001675 luaL_error(L, "invalid window property: `%s'", s);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001676 return 0;
1677}
1678
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001679 static int
1680luaV_window_next(lua_State *L)
1681{
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001682 luaV_Window *w = luaV_checkudata(L, 1, LUAVIM_WINDOW);
Bram Moolenaar1dced572012-04-05 16:54:08 +02001683 win_T *win = (win_T *) luaV_checkcache(L, (void *) *w);
1684 luaV_pushwindow(L, win->w_next);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001685 return 1;
1686}
1687
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001688 static int
1689luaV_window_previous(lua_State *L)
1690{
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001691 luaV_Window *w = luaV_checkudata(L, 1, LUAVIM_WINDOW);
Bram Moolenaar1dced572012-04-05 16:54:08 +02001692 win_T *win = (win_T *) luaV_checkcache(L, (void *) *w);
1693 luaV_pushwindow(L, win->w_prev);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001694 return 1;
1695}
1696
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001697 static int
1698luaV_window_isvalid(lua_State *L)
1699{
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001700 luaV_Window *w = luaV_checkudata(L, 1, LUAVIM_WINDOW);
Bram Moolenaar1dced572012-04-05 16:54:08 +02001701 luaV_getudata(L, *w);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001702 lua_pushboolean(L, !lua_isnil(L, -1));
1703 return 1;
1704}
1705
1706static const luaL_Reg luaV_Window_mt[] = {
1707 {"__tostring", luaV_window_tostring},
1708 {"__call", luaV_window_call},
1709 {"__index", luaV_window_index},
1710 {"__newindex", luaV_window_newindex},
1711 {"next", luaV_window_next},
1712 {"previous", luaV_window_previous},
1713 {"isvalid", luaV_window_isvalid},
1714 {NULL, NULL}
1715};
1716
1717
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001718// ======= Vim module =======
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001719
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001720 static int
1721luaV_print(lua_State *L)
1722{
Yegappan Lakshmanan41114a22021-07-29 20:22:14 +02001723 int i, n = lua_gettop(L); // nargs
1724 const char *s;
1725 size_t l;
1726 garray_T msg_ga;
1727
1728 ga_init2(&msg_ga, 1, 128);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001729 lua_getglobal(L, "tostring");
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001730 for (i = 1; i <= n; i++)
1731 {
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001732 lua_pushvalue(L, -1); // tostring
1733 lua_pushvalue(L, i); // arg
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001734 lua_call(L, 1, 1);
1735 s = lua_tolstring(L, -1, &l);
1736 if (s == NULL)
1737 return luaL_error(L, "cannot convert to string");
Bram Moolenaar78e006b2021-07-28 15:07:01 +02001738 if (i > 1)
Yegappan Lakshmanan41114a22021-07-29 20:22:14 +02001739 ga_append(&msg_ga, ' '); // use space instead of tab
1740 ga_concat_len(&msg_ga, (char_u *)s, l);
Bram Moolenaar2a4bd002021-07-28 21:48:59 +02001741 lua_pop(L, 1);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001742 }
Yegappan Lakshmanan41114a22021-07-29 20:22:14 +02001743 // Replace any "\n" with "\0"
1744 for (i = 0; i < msg_ga.ga_len; i++)
1745 if (((char *)msg_ga.ga_data)[i] == '\n')
1746 ((char *)msg_ga.ga_data)[i] = '\0';
1747 lua_pushlstring(L, msg_ga.ga_data, msg_ga.ga_len);
Bram Moolenaarb98678a2019-10-19 15:18:44 +02001748 if (!got_int)
1749 luaV_msg(L);
Yegappan Lakshmanan41114a22021-07-29 20:22:14 +02001750
1751 ga_clear(&msg_ga);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001752 return 0;
1753}
1754
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001755 static int
Bram Moolenaar38e2b062011-09-21 17:15:39 +02001756luaV_debug(lua_State *L)
1757{
1758 lua_settop(L, 0);
1759 lua_getglobal(L, "vim");
1760 lua_getfield(L, -1, "eval");
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001761 lua_remove(L, -2); // vim.eval at position 1
Bram Moolenaar38e2b062011-09-21 17:15:39 +02001762 for (;;)
1763 {
1764 const char *input;
1765 size_t l;
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001766 lua_pushvalue(L, 1); // vim.eval
Bram Moolenaar38e2b062011-09-21 17:15:39 +02001767 lua_pushliteral(L, "input('lua_debug> ')");
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001768 lua_call(L, 1, 1); // return string
Bram Moolenaar38e2b062011-09-21 17:15:39 +02001769 input = lua_tolstring(L, -1, &l);
1770 if (l == 0 || strcmp(input, "cont") == 0)
1771 return 0;
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001772 msg_putchar('\n'); // avoid outputting on input line
Bram Moolenaar38e2b062011-09-21 17:15:39 +02001773 if (luaL_loadbuffer(L, input, l, "=(debug command)")
1774 || lua_pcall(L, 0, 0, 0))
1775 luaV_emsg(L);
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001776 lua_settop(L, 1); // remove eventual returns, but keep vim.eval
Bram Moolenaar38e2b062011-09-21 17:15:39 +02001777 }
1778}
1779
1780 static int
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001781luaV_command(lua_State *L)
1782{
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001783 do_cmdline_cmd((char_u *) luaL_checkstring(L, 1));
1784 update_screen(VALID);
1785 return 0;
1786}
1787
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001788 static int
1789luaV_eval(lua_State *L)
1790{
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001791 typval_T *tv = eval_expr((char_u *) luaL_checkstring(L, 1), NULL);
1792 if (tv == NULL) luaL_error(L, "invalid expression");
1793 luaV_pushtypval(L, tv);
Bram Moolenaarb3766472013-04-15 13:49:21 +02001794 free_tv(tv);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001795 return 1;
1796}
1797
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001798 static int
Bram Moolenaar0d2e4fc2010-07-18 12:35:47 +02001799luaV_beep(lua_State *L UNUSED)
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001800{
Bram Moolenaar165bc692015-07-21 17:53:25 +02001801 vim_beep(BO_LANG);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001802 return 0;
1803}
1804
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001805 static int
1806luaV_line(lua_State *L)
1807{
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001808 luaV_pushline(L, curbuf, curwin->w_cursor.lnum);
1809 return 1;
1810}
1811
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001812 static int
Bram Moolenaar1dced572012-04-05 16:54:08 +02001813luaV_list(lua_State *L)
1814{
Bram Moolenaarca06da92018-07-01 15:12:05 +02001815 list_T *l;
1816 int initarg = !lua_isnoneornil(L, 1);
1817
1818 if (initarg && lua_type(L, 1) != LUA_TTABLE)
1819 luaL_error(L, "table expected, got %s", luaL_typename(L, 1));
1820 l = list_alloc();
Bram Moolenaar1dced572012-04-05 16:54:08 +02001821 if (l == NULL)
1822 lua_pushnil(L);
1823 else
Bram Moolenaarca06da92018-07-01 15:12:05 +02001824 {
Bram Moolenaar1dced572012-04-05 16:54:08 +02001825 luaV_newlist(L, l);
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001826 if (initarg) // traverse table to init list
Bram Moolenaar17413672018-07-14 20:49:42 +02001827 {
Bram Moolenaarca06da92018-07-01 15:12:05 +02001828 int notnil, i = 0;
1829 typval_T v;
Bram Moolenaar17413672018-07-14 20:49:42 +02001830 do
1831 {
Bram Moolenaarca06da92018-07-01 15:12:05 +02001832 lua_rawgeti(L, 1, ++i);
1833 notnil = !lua_isnil(L, -1);
Bram Moolenaar17413672018-07-14 20:49:42 +02001834 if (notnil)
1835 {
Bram Moolenaarca06da92018-07-01 15:12:05 +02001836 luaV_checktypval(L, -1, &v, "vim.list");
1837 list_append_tv(l, &v);
Bram Moolenaar713bf9e92019-03-16 16:38:41 +01001838 clear_tv(&v);
Bram Moolenaarca06da92018-07-01 15:12:05 +02001839 }
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001840 lua_pop(L, 1); // value
Bram Moolenaarca06da92018-07-01 15:12:05 +02001841 } while (notnil);
1842 }
1843 }
Bram Moolenaar1dced572012-04-05 16:54:08 +02001844 return 1;
1845}
1846
1847 static int
1848luaV_dict(lua_State *L)
1849{
Bram Moolenaarca06da92018-07-01 15:12:05 +02001850 dict_T *d;
1851 int initarg = !lua_isnoneornil(L, 1);
1852
1853 if (initarg && lua_type(L, 1) != LUA_TTABLE)
1854 luaL_error(L, "table expected, got %s", luaL_typename(L, 1));
1855 d = dict_alloc();
Bram Moolenaar1dced572012-04-05 16:54:08 +02001856 if (d == NULL)
1857 lua_pushnil(L);
1858 else
Bram Moolenaarca06da92018-07-01 15:12:05 +02001859 {
Bram Moolenaar1dced572012-04-05 16:54:08 +02001860 luaV_newdict(L, d);
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001861 if (initarg) // traverse table to init dict
Bram Moolenaarca06da92018-07-01 15:12:05 +02001862 {
1863 lua_pushnil(L);
1864 while (lua_next(L, 1))
1865 {
1866 char_u *key;
1867 dictitem_T *di;
1868 typval_T v;
Bram Moolenaard6ef5f92018-07-13 22:08:23 +02001869
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001870 lua_pushvalue(L, -2); // dup key in case it's a number
Bram Moolenaarca06da92018-07-01 15:12:05 +02001871 key = (char_u *) lua_tostring(L, -1);
Bram Moolenaard6ef5f92018-07-13 22:08:23 +02001872 if (key == NULL)
1873 {
1874 lua_pushnil(L);
1875 return 1;
1876 }
1877 if (*key == NUL)
Bram Moolenaarca06da92018-07-01 15:12:05 +02001878 luaL_error(L, "table has empty key");
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001879 luaV_checktypval(L, -2, &v, "vim.dict"); // value
Bram Moolenaarca06da92018-07-01 15:12:05 +02001880 di = dictitem_alloc(key);
Bram Moolenaard6ef5f92018-07-13 22:08:23 +02001881 if (di == NULL || dict_add(d, di) == FAIL)
1882 {
Bram Moolenaarca06da92018-07-01 15:12:05 +02001883 vim_free(di);
1884 lua_pushnil(L);
1885 return 1;
1886 }
Bram Moolenaare49b8e82020-07-01 13:52:55 +02001887 di->di_tv = v;
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001888 lua_pop(L, 2); // key copy and value
Bram Moolenaarca06da92018-07-01 15:12:05 +02001889 }
1890 }
1891 }
1892 return 1;
1893}
1894
1895 static int
Bram Moolenaarb7828692019-03-23 13:57:02 +01001896luaV_blob(lua_State *L)
1897{
1898 blob_T *b;
1899 int initarg = !lua_isnoneornil(L, 1);
1900
1901 if (initarg && !lua_isstring(L, 1))
1902 luaL_error(L, "string expected, got %s", luaL_typename(L, 1));
1903 b = blob_alloc();
1904 if (b == NULL)
1905 lua_pushnil(L);
1906 else
1907 {
1908 luaV_newblob(L, b);
1909 if (initarg)
1910 {
1911 size_t i, l = 0;
1912 const char *s = lua_tolstring(L, 1, &l);
1913
Bram Moolenaar5f1d3ae2020-02-11 22:37:35 +01001914 if (ga_grow(&b->bv_ga, (int)l) == OK)
Bram Moolenaar6fb5c972019-03-26 21:44:20 +01001915 for (i = 0; i < l; ++i)
1916 ga_append(&b->bv_ga, s[i]);
Bram Moolenaarb7828692019-03-23 13:57:02 +01001917 }
1918 }
1919 return 1;
1920}
1921
1922 static int
Bram Moolenaarca06da92018-07-01 15:12:05 +02001923luaV_funcref(lua_State *L)
1924{
1925 const char *name = luaL_checkstring(L, 1);
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001926 // note: not checking if function exists (needs function_exists)
Bram Moolenaarca06da92018-07-01 15:12:05 +02001927 if (name == NULL || *name == NUL || VIM_ISDIGIT(*name))
1928 luaL_error(L, "invalid function name: %s", name);
1929 luaV_newfuncref(L, (char_u *) name);
Bram Moolenaar1dced572012-04-05 16:54:08 +02001930 return 1;
1931}
1932
1933 static int
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001934luaV_buffer(lua_State *L)
1935{
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001936 buf_T *buf;
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001937 if (lua_isstring(L, 1)) // get by number or name?
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001938 {
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001939 if (lua_isnumber(L, 1)) // by number?
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001940 {
1941 int n = lua_tointeger(L, 1);
Bram Moolenaar29323592016-07-24 22:04:11 +02001942 FOR_ALL_BUFFERS(buf)
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001943 if (buf->b_fnum == n) break;
1944 }
Bram Moolenaarca06da92018-07-01 15:12:05 +02001945 else // by name
1946 {
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001947 size_t l;
1948 const char *s = lua_tolstring(L, 1, &l);
Bram Moolenaar29323592016-07-24 22:04:11 +02001949 FOR_ALL_BUFFERS(buf)
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001950 {
1951 if (buf->b_ffname == NULL || buf->b_sfname == NULL)
1952 {
1953 if (l == 0) break;
1954 }
Bram Moolenaar0d2e4fc2010-07-18 12:35:47 +02001955 else if (strncmp(s, (char *)buf->b_ffname, l) == 0
1956 || strncmp(s, (char *)buf->b_sfname, l) == 0)
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001957 break;
1958 }
1959 }
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001960 }
Bram Moolenaar1dced572012-04-05 16:54:08 +02001961 else
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001962 buf = (lua_toboolean(L, 1)) ? firstbuf : curbuf; // first buffer?
Bram Moolenaar1dced572012-04-05 16:54:08 +02001963 luaV_pushbuffer(L, buf);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001964 return 1;
1965}
1966
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001967 static int
1968luaV_window(lua_State *L)
1969{
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001970 win_T *win;
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001971 if (lua_isnumber(L, 1)) // get by number?
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001972 {
1973 int n = lua_tointeger(L, 1);
1974 for (win = firstwin; win != NULL; win = win->w_next, n--)
1975 if (n == 1) break;
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001976 }
Bram Moolenaar1dced572012-04-05 16:54:08 +02001977 else
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001978 win = (lua_toboolean(L, 1)) ? firstwin : curwin; // first window?
Bram Moolenaar1dced572012-04-05 16:54:08 +02001979 luaV_pushwindow(L, win);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001980 return 1;
1981}
1982
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001983 static int
1984luaV_open(lua_State *L)
1985{
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001986 char_u *s = NULL;
1987#ifdef HAVE_SANDBOX
1988 luaV_checksandbox(L);
1989#endif
1990 if (lua_isstring(L, 1)) s = (char_u *) lua_tostring(L, 1);
Bram Moolenaarfa263a52011-12-08 16:00:16 +01001991 luaV_pushbuffer(L, buflist_new(s, NULL, 1L, BLN_LISTED));
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001992 return 1;
1993}
1994
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001995 static int
Bram Moolenaar1dced572012-04-05 16:54:08 +02001996luaV_type(lua_State *L)
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001997{
Bram Moolenaar1dced572012-04-05 16:54:08 +02001998 luaL_checkany(L, 1);
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001999 if (lua_type(L, 1) == LUA_TUSERDATA) // check vim udata?
Bram Moolenaar55d5c032010-07-17 23:52:29 +02002000 {
Bram Moolenaar1dced572012-04-05 16:54:08 +02002001 lua_settop(L, 1);
2002 if (lua_getmetatable(L, 1))
2003 {
2004 luaV_getfield(L, LUAVIM_LIST);
2005 if (lua_rawequal(L, -1, 2))
2006 {
2007 lua_pushstring(L, "list");
2008 return 1;
2009 }
2010 luaV_getfield(L, LUAVIM_DICT);
2011 if (lua_rawequal(L, -1, 2))
2012 {
2013 lua_pushstring(L, "dict");
2014 return 1;
2015 }
Bram Moolenaarb7828692019-03-23 13:57:02 +01002016 luaV_getfield(L, LUAVIM_BLOB);
2017 if (lua_rawequal(L, -1, 2))
2018 {
2019 lua_pushstring(L, "blob");
2020 return 1;
2021 }
Bram Moolenaarca06da92018-07-01 15:12:05 +02002022 luaV_getfield(L, LUAVIM_FUNCREF);
2023 if (lua_rawequal(L, -1, 2))
2024 {
2025 lua_pushstring(L, "funcref");
2026 return 1;
2027 }
Bram Moolenaar1dced572012-04-05 16:54:08 +02002028 luaV_getfield(L, LUAVIM_BUFFER);
2029 if (lua_rawequal(L, -1, 2))
2030 {
2031 lua_pushstring(L, "buffer");
2032 return 1;
2033 }
2034 luaV_getfield(L, LUAVIM_WINDOW);
2035 if (lua_rawequal(L, -1, 2))
2036 {
2037 lua_pushstring(L, "window");
2038 return 1;
2039 }
2040 }
Bram Moolenaar0ba04292010-07-14 23:23:17 +02002041 }
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002042 lua_pushstring(L, luaL_typename(L, 1)); // fallback
Bram Moolenaar1dced572012-04-05 16:54:08 +02002043 return 1;
Bram Moolenaar0ba04292010-07-14 23:23:17 +02002044}
2045
Bram Moolenaareb04f082020-05-17 14:32:35 +02002046 static int
2047luaV_call(lua_State *L)
2048{
2049 int argc = lua_gettop(L) - 1;
2050 size_t funcname_len;
2051 char_u *funcname;
2052 char *error = NULL;
2053 typval_T rettv;
2054 typval_T argv[MAX_FUNC_ARGS + 1];
2055 int i = 0;
2056
2057 if (argc > MAX_FUNC_ARGS)
2058 return luaL_error(L, "Function called with too many arguments");
2059
2060 funcname = (char_u *)luaL_checklstring(L, 1, &funcname_len);
2061
2062 for (; i < argc; i++)
2063 {
2064 if (luaV_totypval(L, i + 2, &argv[i]) == FAIL)
2065 {
2066 error = "lua: cannot convert value";
2067 goto free_vim_args;
2068 }
2069 }
2070
2071 argv[argc].v_type = VAR_UNKNOWN;
2072
2073 if (call_vim_function(funcname, argc, argv, &rettv) == FAIL)
2074 {
2075 error = "lua: call_vim_function failed";
2076 goto free_vim_args;
2077 }
2078
2079 luaV_pushtypval(L, &rettv);
2080 clear_tv(&rettv);
2081
2082free_vim_args:
2083 while (i > 0)
2084 clear_tv(&argv[--i]);
2085
2086 if (error == NULL)
2087 return 1;
2088 else
2089 return luaL_error(L, error);
2090}
2091
Bram Moolenaar0ba04292010-07-14 23:23:17 +02002092static const luaL_Reg luaV_module[] = {
2093 {"command", luaV_command},
2094 {"eval", luaV_eval},
2095 {"beep", luaV_beep},
2096 {"line", luaV_line},
Bram Moolenaar1dced572012-04-05 16:54:08 +02002097 {"list", luaV_list},
2098 {"dict", luaV_dict},
Bram Moolenaarb7828692019-03-23 13:57:02 +01002099 {"blob", luaV_blob},
Bram Moolenaarca06da92018-07-01 15:12:05 +02002100 {"funcref", luaV_funcref},
Bram Moolenaar0ba04292010-07-14 23:23:17 +02002101 {"buffer", luaV_buffer},
2102 {"window", luaV_window},
2103 {"open", luaV_open},
Bram Moolenaar1dced572012-04-05 16:54:08 +02002104 {"type", luaV_type},
Bram Moolenaareb04f082020-05-17 14:32:35 +02002105 {"call", luaV_call},
Bram Moolenaar125ed272021-04-07 20:11:12 +02002106 {"lua_version", NULL},
Bram Moolenaar0ba04292010-07-14 23:23:17 +02002107 {NULL, NULL}
2108};
2109
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002110/*
2111 * for freeing list, dict, buffer and window objects; lightuserdata as arg
2112 */
Bram Moolenaar1dced572012-04-05 16:54:08 +02002113 static int
2114luaV_free(lua_State *L)
2115{
2116 lua_pushnil(L);
2117 luaV_setudata(L, lua_touserdata(L, 1));
2118 return 0;
2119}
2120
2121 static int
Bram Moolenaar4eefe472019-03-19 21:59:19 +01002122luaV_luaeval(lua_State *L)
Bram Moolenaar1dced572012-04-05 16:54:08 +02002123{
2124 luaL_Buffer b;
2125 size_t l;
2126 const char *str = lua_tolstring(L, 1, &l);
2127 typval_T *arg = (typval_T *) lua_touserdata(L, 2);
2128 typval_T *rettv = (typval_T *) lua_touserdata(L, 3);
2129 luaL_buffinit(L, &b);
2130 luaL_addlstring(&b, LUAVIM_EVALHEADER, sizeof(LUAVIM_EVALHEADER) - 1);
2131 luaL_addlstring(&b, str, l);
2132 luaL_pushresult(&b);
2133 str = lua_tolstring(L, -1, &l);
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002134 if (luaL_loadbuffer(L, str, l, LUAVIM_EVALNAME)) // compile error?
Bram Moolenaar1dced572012-04-05 16:54:08 +02002135 {
2136 luaV_emsg(L);
2137 return 0;
2138 }
2139 luaV_pushtypval(L, arg);
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002140 if (lua_pcall(L, 1, 1, 0)) // running error?
Bram Moolenaar1dced572012-04-05 16:54:08 +02002141 {
2142 luaV_emsg(L);
2143 return 0;
2144 }
Bram Moolenaarca06da92018-07-01 15:12:05 +02002145 if (luaV_totypval(L, -1, rettv) == FAIL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002146 emsg("luaeval: cannot convert value");
Bram Moolenaarf554a322015-02-04 23:08:01 +01002147 return 0;
Bram Moolenaar1dced572012-04-05 16:54:08 +02002148}
2149
2150 static int
Bram Moolenaar4eefe472019-03-19 21:59:19 +01002151luaV_setref(lua_State *L)
Bram Moolenaar1dced572012-04-05 16:54:08 +02002152{
Bram Moolenaar4eefe472019-03-19 21:59:19 +01002153 int copyID = lua_tointeger(L, 1);
2154 int abort = FALSE;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01002155
Bram Moolenaar1dced572012-04-05 16:54:08 +02002156 luaV_getfield(L, LUAVIM_LIST);
2157 luaV_getfield(L, LUAVIM_DICT);
Bram Moolenaar4eefe472019-03-19 21:59:19 +01002158 luaV_getfield(L, LUAVIM_FUNCREF);
Bram Moolenaar1dced572012-04-05 16:54:08 +02002159 lua_pushnil(L);
Bram Moolenaar4eefe472019-03-19 21:59:19 +01002160 // traverse cache table
Bram Moolenaarb84634d2015-02-04 22:02:37 +01002161 while (!abort && lua_next(L, lua_upvalueindex(1)) != 0)
Bram Moolenaar1dced572012-04-05 16:54:08 +02002162 {
2163 lua_getmetatable(L, -1);
Bram Moolenaar4eefe472019-03-19 21:59:19 +01002164 if (lua_rawequal(L, -1, 2)) // list?
Bram Moolenaar1dced572012-04-05 16:54:08 +02002165 {
Bram Moolenaar4eefe472019-03-19 21:59:19 +01002166 list_T *l = (list_T *)lua_touserdata(L, 5); // key
2167
Bram Moolenaar7be3ab22019-06-23 01:46:15 +02002168 abort = set_ref_in_list(l, copyID);
Bram Moolenaar1dced572012-04-05 16:54:08 +02002169 }
Bram Moolenaar4eefe472019-03-19 21:59:19 +01002170 else if (lua_rawequal(L, -1, 3)) // dict?
Bram Moolenaar1dced572012-04-05 16:54:08 +02002171 {
Bram Moolenaar4eefe472019-03-19 21:59:19 +01002172 dict_T *d = (dict_T *)lua_touserdata(L, 5); // key
2173
Bram Moolenaar7be3ab22019-06-23 01:46:15 +02002174 abort = set_ref_in_dict(d, copyID);
Bram Moolenaar1dced572012-04-05 16:54:08 +02002175 }
Bram Moolenaar4eefe472019-03-19 21:59:19 +01002176 else if (lua_rawequal(L, -1, 4)) // funcref?
2177 {
2178 luaV_Funcref *f = (luaV_Funcref *)lua_touserdata(L, 5); // key
2179
Bram Moolenaar7be3ab22019-06-23 01:46:15 +02002180 abort = set_ref_in_dict(f->self, copyID);
Bram Moolenaar4eefe472019-03-19 21:59:19 +01002181 }
2182 lua_pop(L, 2); // metatable and value
Bram Moolenaar1dced572012-04-05 16:54:08 +02002183 }
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01002184 lua_pushinteger(L, abort);
Bram Moolenaarf554a322015-02-04 23:08:01 +01002185 return 1;
Bram Moolenaar1dced572012-04-05 16:54:08 +02002186}
2187
Bram Moolenaar125ed272021-04-07 20:11:12 +02002188 static int
2189luaV_pushversion(lua_State *L)
2190{
2191 int major = 0;
2192 int minor = 0;
2193 int patch = 0;
2194 char s[16];
2195
2196 sscanf(LUAVIM_VERSION, "Lua %d.%d.%d", &major, &minor, &patch);
2197 vim_snprintf(s, sizeof(s), "%d.%d.%d", major, minor, patch);
2198 lua_pushstring(L, s);
2199 return 0;
2200}
2201
Bram Moolenaareb04f082020-05-17 14:32:35 +02002202#define LUA_VIM_FN_CODE \
Bram Moolenaar788fbb42020-05-31 14:08:12 +02002203 "vim.fn = setmetatable({}, {\n"\
2204 " __index = function (t, key)\n"\
2205 " local function _fn(...)\n"\
2206 " return vim.call(key, ...)\n"\
2207 " end\n"\
2208 " t[key] = _fn\n"\
2209 " return _fn\n"\
2210 " end\n"\
2211 " })"
2212
2213#define LUA_VIM_UPDATE_PACKAGE_PATHS \
2214 "local last_vim_paths = {}\n"\
2215 "vim._update_package_paths = function ()\n"\
2216 " local cur_vim_paths = {}\n"\
2217 " local function split(s, delimiter)\n"\
2218 " result = {}\n"\
2219 " for match in (s..delimiter):gmatch(\"(.-)\"..delimiter) do\n"\
2220 " table.insert(result, match)\n"\
2221 " end\n"\
2222 " return result\n"\
2223 " end\n"\
2224 " local rtps = split(vim.eval('&runtimepath'), ',')\n"\
2225 " local sep = package.config:sub(1, 1)\n"\
2226 " for _, key in ipairs({'path', 'cpath'}) do\n"\
2227 " local orig_str = package[key] .. ';'\n"\
2228 " local pathtrails_ordered = {}\n"\
2229 " -- Note: ignores trailing item without trailing `;`. Not using something\n"\
2230 " -- simpler in order to preserve empty items (stand for default path).\n"\
2231 " local orig = {}\n"\
2232 " for s in orig_str:gmatch('[^;]*;') do\n"\
2233 " s = s:sub(1, -2) -- Strip trailing semicolon\n"\
2234 " orig[#orig + 1] = s\n"\
2235 " end\n"\
2236 " if key == 'path' then\n"\
2237 " -- /?.lua and /?/init.lua\n"\
2238 " pathtrails_ordered = {sep .. '?.lua', sep .. '?' .. sep .. 'init.lua'}\n"\
2239 " else\n"\
2240 " local pathtrails = {}\n"\
2241 " for _, s in ipairs(orig) do\n"\
2242 " -- Find out path patterns. pathtrail should contain something like\n"\
2243 " -- /?.so, \?.dll. This allows not to bother determining what correct\n"\
2244 " -- suffixes are.\n"\
2245 " local pathtrail = s:match('[/\\\\][^/\\\\]*%?.*$')\n"\
2246 " if pathtrail and not pathtrails[pathtrail] then\n"\
2247 " pathtrails[pathtrail] = true\n"\
2248 " pathtrails_ordered[#pathtrails_ordered + 1] = pathtrail\n"\
2249 " end\n"\
2250 " end\n"\
2251 " end\n"\
2252 " local new = {}\n"\
2253 " for _, rtp in ipairs(rtps) do\n"\
2254 " if not rtp:match(';') then\n"\
2255 " for _, pathtrail in pairs(pathtrails_ordered) do\n"\
2256 " local new_path = rtp .. sep .. 'lua' .. pathtrail\n"\
2257 " -- Always keep paths from &runtimepath at the start:\n"\
2258 " -- append them here disregarding orig possibly containing one of them.\n"\
2259 " new[#new + 1] = new_path\n"\
2260 " cur_vim_paths[new_path] = true\n"\
2261 " end\n"\
2262 " end\n"\
2263 " end\n"\
2264 " for _, orig_path in ipairs(orig) do\n"\
2265 " -- Handle removing obsolete paths originating from &runtimepath: such\n"\
2266 " -- paths either belong to cur_nvim_paths and were already added above or\n"\
2267 " -- to last_nvim_paths and should not be added at all if corresponding\n"\
2268 " -- entry was removed from &runtimepath list.\n"\
2269 " if not (cur_vim_paths[orig_path] or last_vim_paths[orig_path]) then\n"\
2270 " new[#new + 1] = orig_path\n"\
2271 " end\n"\
2272 " end\n"\
2273 " package[key] = table.concat(new, ';')\n"\
2274 " end\n"\
2275 " last_vim_paths = cur_vim_paths\n"\
2276 "end"
Bram Moolenaareb04f082020-05-17 14:32:35 +02002277
Bram Moolenaar55d5c032010-07-17 23:52:29 +02002278 static int
2279luaopen_vim(lua_State *L)
2280{
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002281 // set cache table
Bram Moolenaar0ba04292010-07-14 23:23:17 +02002282 lua_newtable(L);
2283 lua_newtable(L);
Bram Moolenaar1dced572012-04-05 16:54:08 +02002284 lua_pushstring(L, "v");
Bram Moolenaar0ba04292010-07-14 23:23:17 +02002285 lua_setfield(L, -2, "__mode");
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002286 lua_setmetatable(L, -2); // cache is weak-valued
2287 // print
Bram Moolenaar0ba04292010-07-14 23:23:17 +02002288 lua_pushcfunction(L, luaV_print);
2289 lua_setglobal(L, "print");
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002290 // debug.debug
Bram Moolenaar38e2b062011-09-21 17:15:39 +02002291 lua_getglobal(L, "debug");
2292 lua_pushcfunction(L, luaV_debug);
2293 lua_setfield(L, -2, "debug");
2294 lua_pop(L, 1);
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002295 // free
Bram Moolenaar0ba04292010-07-14 23:23:17 +02002296 lua_pushlightuserdata(L, (void *) LUAVIM_FREE);
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_free, 1);
2299 lua_rawset(L, LUA_REGISTRYINDEX);
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002300 // luaeval
Bram Moolenaar1dced572012-04-05 16:54:08 +02002301 lua_pushlightuserdata(L, (void *) LUAVIM_LUAEVAL);
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002302 lua_pushvalue(L, 1); // cache table
Bram Moolenaar1dced572012-04-05 16:54:08 +02002303 lua_pushcclosure(L, luaV_luaeval, 1);
2304 lua_rawset(L, LUA_REGISTRYINDEX);
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002305 // setref
Bram Moolenaar1dced572012-04-05 16:54:08 +02002306 lua_pushlightuserdata(L, (void *) LUAVIM_SETREF);
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002307 lua_pushvalue(L, 1); // cache table
Bram Moolenaar1dced572012-04-05 16:54:08 +02002308 lua_pushcclosure(L, luaV_setref, 1);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02002309 lua_rawset(L, LUA_REGISTRYINDEX);
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002310 // register
Bram Moolenaar1dced572012-04-05 16:54:08 +02002311 luaV_newmetatable(L, LUAVIM_LIST);
2312 lua_pushvalue(L, 1);
2313 luaV_openlib(L, luaV_List_mt, 1);
2314 luaV_newmetatable(L, LUAVIM_DICT);
2315 lua_pushvalue(L, 1);
2316 luaV_openlib(L, luaV_Dict_mt, 1);
Bram Moolenaarb7828692019-03-23 13:57:02 +01002317 luaV_newmetatable(L, LUAVIM_BLOB);
2318 lua_pushvalue(L, 1);
2319 luaV_openlib(L, luaV_Blob_mt, 1);
Bram Moolenaarca06da92018-07-01 15:12:05 +02002320 luaV_newmetatable(L, LUAVIM_FUNCREF);
2321 lua_pushvalue(L, 1);
2322 luaV_openlib(L, luaV_Funcref_mt, 1);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02002323 luaV_newmetatable(L, LUAVIM_BUFFER);
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002324 lua_pushvalue(L, 1); // cache table
Bram Moolenaar1dced572012-04-05 16:54:08 +02002325 luaV_openlib(L, luaV_Buffer_mt, 1);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02002326 luaV_newmetatable(L, LUAVIM_WINDOW);
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002327 lua_pushvalue(L, 1); // cache table
Bram Moolenaar1dced572012-04-05 16:54:08 +02002328 luaV_openlib(L, luaV_Window_mt, 1);
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002329 lua_newtable(L); // vim table
2330 lua_pushvalue(L, 1); // cache table
Bram Moolenaar1dced572012-04-05 16:54:08 +02002331 luaV_openlib(L, luaV_module, 1);
Bram Moolenaar125ed272021-04-07 20:11:12 +02002332 luaV_pushversion(L);
2333 lua_setfield(L, -2, "lua_version");
Bram Moolenaar1dced572012-04-05 16:54:08 +02002334 lua_setglobal(L, LUAVIM_NAME);
Bram Moolenaareb04f082020-05-17 14:32:35 +02002335 // custom code
Bram Moolenaar9309eb22020-05-17 16:53:56 +02002336 (void)luaL_dostring(L, LUA_VIM_FN_CODE);
Bram Moolenaar788fbb42020-05-31 14:08:12 +02002337 (void)luaL_dostring(L, LUA_VIM_UPDATE_PACKAGE_PATHS);
2338
2339 lua_getglobal(L, "vim");
2340 lua_getfield(L, -1, "_update_package_paths");
2341
2342 if (lua_pcall(L, 0, 0, 0))
2343 luaV_emsg(L);
2344
Bram Moolenaar0ba04292010-07-14 23:23:17 +02002345 return 0;
2346}
2347
Bram Moolenaar55d5c032010-07-17 23:52:29 +02002348 static lua_State *
2349luaV_newstate(void)
2350{
Bram Moolenaar0ba04292010-07-14 23:23:17 +02002351 lua_State *L = luaL_newstate();
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002352 luaL_openlibs(L); // core libs
2353 lua_pushcfunction(L, luaopen_vim); // vim
Bram Moolenaar0ba04292010-07-14 23:23:17 +02002354 lua_call(L, 0, 0);
2355 return L;
2356}
2357
Bram Moolenaar55d5c032010-07-17 23:52:29 +02002358 static void
2359luaV_setrange(lua_State *L, int line1, int line2)
2360{
Bram Moolenaar0ba04292010-07-14 23:23:17 +02002361 lua_getglobal(L, LUAVIM_NAME);
2362 lua_pushinteger(L, line1);
2363 lua_setfield(L, -2, "firstline");
2364 lua_pushinteger(L, line2);
2365 lua_setfield(L, -2, "lastline");
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002366 lua_pop(L, 1); // vim table
Bram Moolenaar0ba04292010-07-14 23:23:17 +02002367}
2368
2369
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002370// ======= Interface =======
Bram Moolenaar0ba04292010-07-14 23:23:17 +02002371
2372static lua_State *L = NULL;
2373
Bram Moolenaar55d5c032010-07-17 23:52:29 +02002374 static int
Bram Moolenaar1dced572012-04-05 16:54:08 +02002375lua_isopen(void)
Bram Moolenaar2bd6a1b2010-08-12 22:14:01 +02002376{
2377 return L != NULL;
2378}
2379
2380 static int
Bram Moolenaar55d5c032010-07-17 23:52:29 +02002381lua_init(void)
2382{
Bram Moolenaar1dced572012-04-05 16:54:08 +02002383 if (!lua_isopen())
Bram Moolenaar55d5c032010-07-17 23:52:29 +02002384 {
Bram Moolenaar0ba04292010-07-14 23:23:17 +02002385#ifdef DYNAMIC_LUA
Bram Moolenaar55d5c032010-07-17 23:52:29 +02002386 if (!lua_enabled(TRUE))
2387 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002388 emsg(_("Lua library cannot be loaded."));
Bram Moolenaar55d5c032010-07-17 23:52:29 +02002389 return FAIL;
2390 }
Bram Moolenaar0ba04292010-07-14 23:23:17 +02002391#endif
Bram Moolenaar55d5c032010-07-17 23:52:29 +02002392 L = luaV_newstate();
Bram Moolenaar0ba04292010-07-14 23:23:17 +02002393 }
2394 return OK;
2395}
2396
Bram Moolenaar55d5c032010-07-17 23:52:29 +02002397 void
2398lua_end(void)
2399{
Bram Moolenaar1dced572012-04-05 16:54:08 +02002400 if (lua_isopen())
Bram Moolenaar55d5c032010-07-17 23:52:29 +02002401 {
2402 lua_close(L);
2403 L = NULL;
Bram Moolenaar0ba04292010-07-14 23:23:17 +02002404 }
2405}
2406
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002407/*
2408 * ex commands
2409 */
Bram Moolenaar55d5c032010-07-17 23:52:29 +02002410 void
2411ex_lua(exarg_T *eap)
2412{
Bram Moolenaarc8970b92020-10-26 20:18:08 +01002413 char *script = (char *)script_get(eap, eap->arg);
2414
2415 if (!eap->skip && lua_init() == OK)
Bram Moolenaar55d5c032010-07-17 23:52:29 +02002416 {
Bram Moolenaarc8970b92020-10-26 20:18:08 +01002417 char *s = script != NULL ? script : (char *)eap->arg;
2418
Bram Moolenaar55d5c032010-07-17 23:52:29 +02002419 luaV_setrange(L, eap->line1, eap->line2);
2420 if (luaL_loadbuffer(L, s, strlen(s), LUAVIM_CHUNKNAME)
2421 || lua_pcall(L, 0, 0, 0))
2422 luaV_emsg(L);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02002423 }
Bram Moolenaarc8970b92020-10-26 20:18:08 +01002424 if (script != NULL)
2425 vim_free(script);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02002426}
2427
Bram Moolenaar55d5c032010-07-17 23:52:29 +02002428 void
2429ex_luado(exarg_T *eap)
2430{
Bram Moolenaar0ba04292010-07-14 23:23:17 +02002431 linenr_T l;
2432 const char *s = (const char *) eap->arg;
2433 luaL_Buffer b;
2434 size_t len;
Bram Moolenaard58f03b2017-01-29 22:48:45 +01002435 buf_T *was_curbuf = curbuf;
2436
Bram Moolenaar0ba04292010-07-14 23:23:17 +02002437 if (lua_init() == FAIL) return;
Bram Moolenaar55d5c032010-07-17 23:52:29 +02002438 if (u_save(eap->line1 - 1, eap->line2 + 1) == FAIL)
2439 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002440 emsg(_("cannot save undo information"));
Bram Moolenaar55d5c032010-07-17 23:52:29 +02002441 return;
Bram Moolenaar0ba04292010-07-14 23:23:17 +02002442 }
2443 luaV_setrange(L, eap->line1, eap->line2);
2444 luaL_buffinit(L, &b);
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002445 luaL_addlstring(&b, "return function(line, linenr) ", 30); // header
Bram Moolenaar0ba04292010-07-14 23:23:17 +02002446 luaL_addlstring(&b, s, strlen(s));
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002447 luaL_addlstring(&b, " end", 4); // footer
Bram Moolenaar0ba04292010-07-14 23:23:17 +02002448 luaL_pushresult(&b);
2449 s = lua_tolstring(L, -1, &len);
Bram Moolenaar55d5c032010-07-17 23:52:29 +02002450 if (luaL_loadbuffer(L, s, len, LUAVIM_CHUNKNAME))
2451 {
2452 luaV_emsg(L);
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002453 lua_pop(L, 1); // function body
Bram Moolenaar55d5c032010-07-17 23:52:29 +02002454 return;
Bram Moolenaar0ba04292010-07-14 23:23:17 +02002455 }
2456 lua_call(L, 0, 1);
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002457 lua_replace(L, -2); // function -> body
Bram Moolenaar55d5c032010-07-17 23:52:29 +02002458 for (l = eap->line1; l <= eap->line2; l++)
2459 {
Bram Moolenaare49b8e82020-07-01 13:52:55 +02002460 // Check the line number, the command may have deleted lines.
Bram Moolenaard58f03b2017-01-29 22:48:45 +01002461 if (l > curbuf->b_ml.ml_line_count)
2462 break;
2463
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002464 lua_pushvalue(L, -1); // function
2465 luaV_pushline(L, curbuf, l); // current line as arg
2466 lua_pushinteger(L, l); // current line number as arg
Bram Moolenaarbd2f3c32012-04-06 14:31:00 +02002467 if (lua_pcall(L, 2, 1, 0))
Bram Moolenaar55d5c032010-07-17 23:52:29 +02002468 {
2469 luaV_emsg(L);
2470 break;
2471 }
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002472 // Catch the command switching to another buffer.
Bram Moolenaard58f03b2017-01-29 22:48:45 +01002473 if (curbuf != was_curbuf)
2474 break;
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002475 if (lua_isstring(L, -1)) // update line?
Bram Moolenaar55d5c032010-07-17 23:52:29 +02002476 {
Bram Moolenaar0ba04292010-07-14 23:23:17 +02002477#ifdef HAVE_SANDBOX
Bram Moolenaar55d5c032010-07-17 23:52:29 +02002478 luaV_checksandbox(L);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02002479#endif
Bram Moolenaar55d5c032010-07-17 23:52:29 +02002480 ml_replace(l, luaV_toline(L, -1), TRUE);
2481 changed_bytes(l, 0);
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002482 lua_pop(L, 1); // result from luaV_toline
Bram Moolenaar55d5c032010-07-17 23:52:29 +02002483 }
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002484 lua_pop(L, 1); // line
Bram Moolenaar0ba04292010-07-14 23:23:17 +02002485 }
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002486 lua_pop(L, 1); // function
Bram Moolenaar0ba04292010-07-14 23:23:17 +02002487 check_cursor();
2488 update_screen(NOT_VALID);
2489}
2490
Bram Moolenaar55d5c032010-07-17 23:52:29 +02002491 void
2492ex_luafile(exarg_T *eap)
2493{
2494 if (lua_init() == FAIL)
2495 return;
2496 if (!eap->skip)
2497 {
2498 luaV_setrange(L, eap->line1, eap->line2);
2499 if (luaL_loadfile(L, (char *) eap->arg) || lua_pcall(L, 0, 0, 0))
2500 luaV_emsg(L);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02002501 }
2502}
2503
Bram Moolenaar1dced572012-04-05 16:54:08 +02002504#define luaV_freetype(typ,tname) \
2505 void \
2506 lua_##tname##_free(typ *o) \
2507 { \
2508 if (!lua_isopen()) return; \
2509 luaV_getfield(L, LUAVIM_FREE); \
2510 lua_pushlightuserdata(L, (void *) o); \
2511 lua_call(L, 1, 0); \
2512 }
2513
2514luaV_freetype(buf_T, buffer)
2515luaV_freetype(win_T, window)
2516
Bram Moolenaar55d5c032010-07-17 23:52:29 +02002517 void
Bram Moolenaar4eefe472019-03-19 21:59:19 +01002518do_luaeval(char_u *str, typval_T *arg, typval_T *rettv)
Bram Moolenaar55d5c032010-07-17 23:52:29 +02002519{
Bram Moolenaar1dced572012-04-05 16:54:08 +02002520 lua_init();
2521 luaV_getfield(L, LUAVIM_LUAEVAL);
2522 lua_pushstring(L, (char *) str);
2523 lua_pushlightuserdata(L, (void *) arg);
2524 lua_pushlightuserdata(L, (void *) rettv);
2525 lua_call(L, 3, 0);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02002526}
2527
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01002528 int
Bram Moolenaar4eefe472019-03-19 21:59:19 +01002529set_ref_in_lua(int copyID)
Bram Moolenaar55d5c032010-07-17 23:52:29 +02002530{
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01002531 int aborted = 0;
2532
2533 if (lua_isopen())
2534 {
2535 luaV_getfield(L, LUAVIM_SETREF);
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002536 // call the function with 1 arg, getting 1 result back
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01002537 lua_pushinteger(L, copyID);
2538 lua_call(L, 1, 1);
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002539 // get the result
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01002540 aborted = lua_tointeger(L, -1);
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002541 // pop result off the stack
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01002542 lua_pop(L, 1);
2543 }
2544 return aborted;
Bram Moolenaar0ba04292010-07-14 23:23:17 +02002545}
2546
Bram Moolenaar788fbb42020-05-31 14:08:12 +02002547 void
2548update_package_paths_in_lua()
2549{
2550 if (lua_isopen())
2551 {
2552 lua_getglobal(L, "vim");
2553 lua_getfield(L, -1, "_update_package_paths");
2554
2555 if (lua_pcall(L, 0, 0, 0))
2556 luaV_emsg(L);
2557 }
2558}
2559
Bram Moolenaar801ab062020-06-25 19:27:56 +02002560/*
2561 * Native C function callback
2562 */
2563 static int
2564luaV_call_lua_func(
2565 int argcount,
2566 typval_T *argvars,
2567 typval_T *rettv,
2568 void *state)
2569{
2570 int i;
2571 int luaargcount = argcount;
2572 luaV_CFuncState *funcstate = (luaV_CFuncState*)state;
2573 lua_rawgeti(funcstate->L, LUA_REGISTRYINDEX, funcstate->lua_funcref);
2574
2575 if (funcstate->lua_tableref != LUA_NOREF)
2576 {
2577 // First arg for metatable __call method is a table
2578 luaargcount += 1;
2579 lua_rawgeti(funcstate->L, LUA_REGISTRYINDEX, funcstate->lua_tableref);
2580 }
2581
2582 for (i = 0; i < argcount; ++i)
2583 luaV_pushtypval(funcstate->L, &argvars[i]);
2584
2585 if (lua_pcall(funcstate->L, luaargcount, 1, 0))
2586 {
2587 luaV_emsg(funcstate->L);
2588 return FCERR_OTHER;
2589 }
2590
2591 luaV_checktypval(funcstate->L, -1, rettv, "get return value");
2592 return FCERR_NONE;
2593}
2594
2595/*
2596 * Free up any lua references held by the func state.
2597 */
2598 static void
2599luaV_call_lua_func_free(void *state)
2600{
2601 luaV_CFuncState *funcstate = (luaV_CFuncState*)state;
2602 luaL_unref(L, LUA_REGISTRYINDEX, funcstate->lua_funcref);
2603 funcstate->L = NULL;
2604 if (funcstate->lua_tableref != LUA_NOREF)
2605 luaL_unref(L, LUA_REGISTRYINDEX, funcstate->lua_tableref);
2606 VIM_CLEAR(funcstate);
2607}
2608
Bram Moolenaar0ba04292010-07-14 23:23:17 +02002609#endif