blob: ce0901a20e67909264659d4a63033d67f967bce4 [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
27typedef buf_T *luaV_Buffer;
28typedef win_T *luaV_Window;
Bram Moolenaar1dced572012-04-05 16:54:08 +020029typedef dict_T *luaV_Dict;
30typedef list_T *luaV_List;
Bram Moolenaarb7828692019-03-23 13:57:02 +010031typedef blob_T *luaV_Blob;
Bram Moolenaarca06da92018-07-01 15:12:05 +020032typedef struct {
Bram Moolenaar4eefe472019-03-19 21:59:19 +010033 char_u *name; // funcref
Bram Moolenaarca06da92018-07-01 15:12:05 +020034 dict_T *self; // selfdict
35} luaV_Funcref;
Bram Moolenaar0ba04292010-07-14 23:23:17 +020036typedef void (*msgfunc_T)(char_u *);
37
Bram Moolenaar801ab062020-06-25 19:27:56 +020038typedef struct {
39 int lua_funcref; // ref to a lua func
40 int lua_tableref; // ref to a lua table if metatable else LUA_NOREF. used
41 // for __call
42 lua_State *L;
43} luaV_CFuncState;
44
Bram Moolenaar1dced572012-04-05 16:54:08 +020045static const char LUAVIM_DICT[] = "dict";
46static const char LUAVIM_LIST[] = "list";
Bram Moolenaarb7828692019-03-23 13:57:02 +010047static const char LUAVIM_BLOB[] = "blob";
Bram Moolenaarca06da92018-07-01 15:12:05 +020048static const char LUAVIM_FUNCREF[] = "funcref";
Bram Moolenaar0ba04292010-07-14 23:23:17 +020049static const char LUAVIM_BUFFER[] = "buffer";
50static const char LUAVIM_WINDOW[] = "window";
51static const char LUAVIM_FREE[] = "luaV_free";
Bram Moolenaar1dced572012-04-05 16:54:08 +020052static const char LUAVIM_LUAEVAL[] = "luaV_luaeval";
53static const char LUAVIM_SETREF[] = "luaV_setref";
Bram Moolenaar0ba04292010-07-14 23:23:17 +020054
Bram Moolenaar801ab062020-06-25 19:27:56 +020055static const char LUA___CALL[] = "__call";
56
Bram Moolenaar2ab2e862019-12-04 21:24:53 +010057// most functions are closures with a cache table as first upvalue;
58// get/setudata manage references to vim userdata in cache table through
59// object pointers (light userdata)
Bram Moolenaar1dced572012-04-05 16:54:08 +020060#define luaV_getudata(L, v) \
61 lua_pushlightuserdata((L), (void *) (v)); \
62 lua_rawget((L), lua_upvalueindex(1))
63#define luaV_setudata(L, v) \
64 lua_pushlightuserdata((L), (void *) (v)); \
65 lua_pushvalue((L), -2); \
66 lua_rawset((L), lua_upvalueindex(1))
Bram Moolenaar0ba04292010-07-14 23:23:17 +020067#define luaV_getfield(L, s) \
68 lua_pushlightuserdata((L), (void *)(s)); \
69 lua_rawget((L), LUA_REGISTRYINDEX)
70#define luaV_checksandbox(L) \
71 if (sandbox) luaL_error((L), "not allowed in sandbox")
72#define luaV_msg(L) luaV_msgfunc((L), (msgfunc_T) msg)
73#define luaV_emsg(L) luaV_msgfunc((L), (msgfunc_T) emsg)
Bram Moolenaarca06da92018-07-01 15:12:05 +020074#define luaV_checktypval(L, a, v, msg) \
75 do { \
Bram Moolenaar801ab062020-06-25 19:27:56 +020076 if (luaV_totypval(L, a, v) == FAIL) \
Bram Moolenaarca06da92018-07-01 15:12:05 +020077 luaL_error(L, msg ": cannot convert value"); \
78 } while (0)
Bram Moolenaar0ba04292010-07-14 23:23:17 +020079
Bram Moolenaarca06da92018-07-01 15:12:05 +020080static luaV_List *luaV_pushlist(lua_State *L, list_T *lis);
81static luaV_Dict *luaV_pushdict(lua_State *L, dict_T *dic);
Bram Moolenaarb7828692019-03-23 13:57:02 +010082static luaV_Blob *luaV_pushblob(lua_State *L, blob_T *blo);
Bram Moolenaar4eefe472019-03-19 21:59:19 +010083static luaV_Funcref *luaV_pushfuncref(lua_State *L, char_u *name);
Bram Moolenaar801ab062020-06-25 19:27:56 +020084static int luaV_call_lua_func(int argcount, typval_T *argvars, typval_T *rettv, void *state);
85static void luaV_call_lua_func_free(void *state);
Bram Moolenaar1dced572012-04-05 16:54:08 +020086
87#if LUA_VERSION_NUM <= 501
88#define luaV_openlib(L, l, n) luaL_openlib(L, NULL, l, n)
89#define luaL_typeerror luaL_typerror
90#else
91#define luaV_openlib luaL_setfuncs
92#endif
Bram Moolenaar0ba04292010-07-14 23:23:17 +020093
94#ifdef DYNAMIC_LUA
Bram Moolenaar2334b6d2010-07-22 21:32:16 +020095
Bram Moolenaar4f974752019-02-17 17:44:42 +010096#ifndef MSWIN
Bram Moolenaar2334b6d2010-07-22 21:32:16 +020097# include <dlfcn.h>
98# define HANDLE void*
99# define load_dll(n) dlopen((n), RTLD_LAZY|RTLD_GLOBAL)
100# define symbol_from_dll dlsym
101# define close_dll dlclose
102#else
Bram Moolenaarebbcb822010-10-23 14:02:54 +0200103# define load_dll vimLoadLib
Bram Moolenaar2334b6d2010-07-22 21:32:16 +0200104# define symbol_from_dll GetProcAddress
105# define close_dll FreeLibrary
106#endif
107
Bram Moolenaar2ab2e862019-12-04 21:24:53 +0100108// lauxlib
Bram Moolenaar1dced572012-04-05 16:54:08 +0200109#if LUA_VERSION_NUM <= 501
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200110#define luaL_register dll_luaL_register
Bram Moolenaar1dced572012-04-05 16:54:08 +0200111#define luaL_prepbuffer dll_luaL_prepbuffer
112#define luaL_openlib dll_luaL_openlib
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200113#define luaL_typerror dll_luaL_typerror
Bram Moolenaar1dced572012-04-05 16:54:08 +0200114#define luaL_loadfile dll_luaL_loadfile
115#define luaL_loadbuffer dll_luaL_loadbuffer
116#else
117#define luaL_prepbuffsize dll_luaL_prepbuffsize
118#define luaL_setfuncs dll_luaL_setfuncs
119#define luaL_loadfilex dll_luaL_loadfilex
120#define luaL_loadbufferx dll_luaL_loadbufferx
121#define luaL_argerror dll_luaL_argerror
122#endif
Bram Moolenaarbd2f3c32012-04-06 14:31:00 +0200123#define luaL_checkany dll_luaL_checkany
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200124#define luaL_checklstring dll_luaL_checklstring
125#define luaL_checkinteger dll_luaL_checkinteger
126#define luaL_optinteger dll_luaL_optinteger
127#define luaL_checktype dll_luaL_checktype
128#define luaL_error dll_luaL_error
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200129#define luaL_newstate dll_luaL_newstate
130#define luaL_buffinit dll_luaL_buffinit
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200131#define luaL_addlstring dll_luaL_addlstring
132#define luaL_pushresult dll_luaL_pushresult
Bram Moolenaardf1643a2020-05-17 18:53:27 +0200133#define luaL_loadstring dll_luaL_loadstring
Bram Moolenaar2ab2e862019-12-04 21:24:53 +0100134// lua
Bram Moolenaar1dced572012-04-05 16:54:08 +0200135#if LUA_VERSION_NUM <= 501
136#define lua_tonumber dll_lua_tonumber
137#define lua_tointeger dll_lua_tointeger
138#define lua_call dll_lua_call
139#define lua_pcall dll_lua_pcall
140#else
141#define lua_tonumberx dll_lua_tonumberx
142#define lua_tointegerx dll_lua_tointegerx
143#define lua_callk dll_lua_callk
144#define lua_pcallk dll_lua_pcallk
145#define lua_getglobal dll_lua_getglobal
146#define lua_setglobal dll_lua_setglobal
Bram Moolenaar1dced572012-04-05 16:54:08 +0200147#endif
Bram Moolenaar1f860d82015-06-27 18:36:16 +0200148#if LUA_VERSION_NUM <= 502
149#define lua_replace dll_lua_replace
150#define lua_remove dll_lua_remove
151#endif
152#if LUA_VERSION_NUM >= 503
153#define lua_rotate dll_lua_rotate
154#define lua_copy dll_lua_copy
155#endif
Bram Moolenaarbd2f3c32012-04-06 14:31:00 +0200156#define lua_typename dll_lua_typename
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200157#define lua_close dll_lua_close
158#define lua_gettop dll_lua_gettop
159#define lua_settop dll_lua_settop
160#define lua_pushvalue dll_lua_pushvalue
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200161#define lua_isnumber dll_lua_isnumber
162#define lua_isstring dll_lua_isstring
163#define lua_type dll_lua_type
164#define lua_rawequal dll_lua_rawequal
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200165#define lua_toboolean dll_lua_toboolean
166#define lua_tolstring dll_lua_tolstring
167#define lua_touserdata dll_lua_touserdata
168#define lua_pushnil dll_lua_pushnil
169#define lua_pushnumber dll_lua_pushnumber
170#define lua_pushinteger dll_lua_pushinteger
171#define lua_pushlstring dll_lua_pushlstring
172#define lua_pushstring dll_lua_pushstring
173#define lua_pushfstring dll_lua_pushfstring
174#define lua_pushcclosure dll_lua_pushcclosure
175#define lua_pushboolean dll_lua_pushboolean
176#define lua_pushlightuserdata dll_lua_pushlightuserdata
177#define lua_getfield dll_lua_getfield
178#define lua_rawget dll_lua_rawget
Bram Moolenaar1dced572012-04-05 16:54:08 +0200179#define lua_rawgeti dll_lua_rawgeti
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200180#define lua_createtable dll_lua_createtable
Bram Moolenaar830e3582018-08-21 14:23:35 +0200181#if LUA_VERSION_NUM >= 504
182 #define lua_newuserdatauv dll_lua_newuserdatauv
183#else
184 #define lua_newuserdata dll_lua_newuserdata
185#endif
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200186#define lua_getmetatable dll_lua_getmetatable
187#define lua_setfield dll_lua_setfield
188#define lua_rawset dll_lua_rawset
189#define lua_rawseti dll_lua_rawseti
190#define lua_setmetatable dll_lua_setmetatable
Bram Moolenaarbd2f3c32012-04-06 14:31:00 +0200191#define lua_next dll_lua_next
Bram Moolenaar2ab2e862019-12-04 21:24:53 +0100192// libs
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200193#define luaopen_base dll_luaopen_base
194#define luaopen_table dll_luaopen_table
195#define luaopen_string dll_luaopen_string
196#define luaopen_math dll_luaopen_math
Bram Moolenaar16c98f92010-07-28 22:46:08 +0200197#define luaopen_io dll_luaopen_io
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200198#define luaopen_os dll_luaopen_os
199#define luaopen_package dll_luaopen_package
200#define luaopen_debug dll_luaopen_debug
Bram Moolenaar16c98f92010-07-28 22:46:08 +0200201#define luaL_openlibs dll_luaL_openlibs
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200202
Bram Moolenaar2ab2e862019-12-04 21:24:53 +0100203// lauxlib
Bram Moolenaar1dced572012-04-05 16:54:08 +0200204#if LUA_VERSION_NUM <= 501
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200205void (*dll_luaL_register) (lua_State *L, const char *libname, const luaL_Reg *l);
Bram Moolenaar1dced572012-04-05 16:54:08 +0200206char *(*dll_luaL_prepbuffer) (luaL_Buffer *B);
207void (*dll_luaL_openlib) (lua_State *L, const char *libname, const luaL_Reg *l, int nup);
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200208int (*dll_luaL_typerror) (lua_State *L, int narg, const char *tname);
Bram Moolenaar1dced572012-04-05 16:54:08 +0200209int (*dll_luaL_loadfile) (lua_State *L, const char *filename);
210int (*dll_luaL_loadbuffer) (lua_State *L, const char *buff, size_t sz, const char *name);
211#else
212char *(*dll_luaL_prepbuffsize) (luaL_Buffer *B, size_t sz);
213void (*dll_luaL_setfuncs) (lua_State *L, const luaL_Reg *l, int nup);
214int (*dll_luaL_loadfilex) (lua_State *L, const char *filename, const char *mode);
215int (*dll_luaL_loadbufferx) (lua_State *L, const char *buff, size_t sz, const char *name, const char *mode);
216int (*dll_luaL_argerror) (lua_State *L, int numarg, const char *extramsg);
217#endif
Bram Moolenaarbd2f3c32012-04-06 14:31:00 +0200218void (*dll_luaL_checkany) (lua_State *L, int narg);
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200219const char *(*dll_luaL_checklstring) (lua_State *L, int numArg, size_t *l);
220lua_Integer (*dll_luaL_checkinteger) (lua_State *L, int numArg);
221lua_Integer (*dll_luaL_optinteger) (lua_State *L, int nArg, lua_Integer def);
222void (*dll_luaL_checktype) (lua_State *L, int narg, int t);
223int (*dll_luaL_error) (lua_State *L, const char *fmt, ...);
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200224lua_State *(*dll_luaL_newstate) (void);
225void (*dll_luaL_buffinit) (lua_State *L, luaL_Buffer *B);
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200226void (*dll_luaL_addlstring) (luaL_Buffer *B, const char *s, size_t l);
227void (*dll_luaL_pushresult) (luaL_Buffer *B);
Bram Moolenaardf1643a2020-05-17 18:53:27 +0200228int (*dll_luaL_loadstring) (lua_State *L, const char *s);
Bram Moolenaar2ab2e862019-12-04 21:24:53 +0100229// lua
Bram Moolenaar1dced572012-04-05 16:54:08 +0200230#if LUA_VERSION_NUM <= 501
231lua_Number (*dll_lua_tonumber) (lua_State *L, int idx);
232lua_Integer (*dll_lua_tointeger) (lua_State *L, int idx);
233void (*dll_lua_call) (lua_State *L, int nargs, int nresults);
234int (*dll_lua_pcall) (lua_State *L, int nargs, int nresults, int errfunc);
235#else
236lua_Number (*dll_lua_tonumberx) (lua_State *L, int idx, int *isnum);
237lua_Integer (*dll_lua_tointegerx) (lua_State *L, int idx, int *isnum);
238void (*dll_lua_callk) (lua_State *L, int nargs, int nresults, int ctx,
Bram Moolenaardb913952012-06-29 12:54:53 +0200239 lua_CFunction k);
Bram Moolenaar1dced572012-04-05 16:54:08 +0200240int (*dll_lua_pcallk) (lua_State *L, int nargs, int nresults, int errfunc,
Bram Moolenaardb913952012-06-29 12:54:53 +0200241 int ctx, lua_CFunction k);
Bram Moolenaar1dced572012-04-05 16:54:08 +0200242void (*dll_lua_getglobal) (lua_State *L, const char *var);
243void (*dll_lua_setglobal) (lua_State *L, const char *var);
Bram Moolenaar1f860d82015-06-27 18:36:16 +0200244#endif
245#if LUA_VERSION_NUM <= 502
246void (*dll_lua_replace) (lua_State *L, int idx);
247void (*dll_lua_remove) (lua_State *L, int idx);
248#endif
249#if LUA_VERSION_NUM >= 503
250void (*dll_lua_rotate) (lua_State *L, int idx, int n);
Bram Moolenaar9514b1f2015-06-25 18:27:32 +0200251void (*dll_lua_copy) (lua_State *L, int fromidx, int toidx);
Bram Moolenaar1dced572012-04-05 16:54:08 +0200252#endif
Bram Moolenaarbd2f3c32012-04-06 14:31:00 +0200253const char *(*dll_lua_typename) (lua_State *L, int tp);
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200254void (*dll_lua_close) (lua_State *L);
255int (*dll_lua_gettop) (lua_State *L);
256void (*dll_lua_settop) (lua_State *L, int idx);
257void (*dll_lua_pushvalue) (lua_State *L, int idx);
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200258int (*dll_lua_isnumber) (lua_State *L, int idx);
259int (*dll_lua_isstring) (lua_State *L, int idx);
260int (*dll_lua_type) (lua_State *L, int idx);
261int (*dll_lua_rawequal) (lua_State *L, int idx1, int idx2);
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200262int (*dll_lua_toboolean) (lua_State *L, int idx);
263const char *(*dll_lua_tolstring) (lua_State *L, int idx, size_t *len);
264void *(*dll_lua_touserdata) (lua_State *L, int idx);
265void (*dll_lua_pushnil) (lua_State *L);
266void (*dll_lua_pushnumber) (lua_State *L, lua_Number n);
267void (*dll_lua_pushinteger) (lua_State *L, lua_Integer n);
268void (*dll_lua_pushlstring) (lua_State *L, const char *s, size_t l);
269void (*dll_lua_pushstring) (lua_State *L, const char *s);
270const char *(*dll_lua_pushfstring) (lua_State *L, const char *fmt, ...);
271void (*dll_lua_pushcclosure) (lua_State *L, lua_CFunction fn, int n);
272void (*dll_lua_pushboolean) (lua_State *L, int b);
273void (*dll_lua_pushlightuserdata) (lua_State *L, void *p);
274void (*dll_lua_getfield) (lua_State *L, int idx, const char *k);
Bram Moolenaar17413672018-07-14 20:49:42 +0200275#if LUA_VERSION_NUM <= 502
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200276void (*dll_lua_rawget) (lua_State *L, int idx);
Bram Moolenaar1dced572012-04-05 16:54:08 +0200277void (*dll_lua_rawgeti) (lua_State *L, int idx, int n);
Bram Moolenaar17413672018-07-14 20:49:42 +0200278#else
279int (*dll_lua_rawget) (lua_State *L, int idx);
280int (*dll_lua_rawgeti) (lua_State *L, int idx, lua_Integer n);
281#endif
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200282void (*dll_lua_createtable) (lua_State *L, int narr, int nrec);
Bram Moolenaar830e3582018-08-21 14:23:35 +0200283#if LUA_VERSION_NUM >= 504
284void *(*dll_lua_newuserdatauv) (lua_State *L, size_t sz, int nuvalue);
285#else
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200286void *(*dll_lua_newuserdata) (lua_State *L, size_t sz);
Bram Moolenaar830e3582018-08-21 14:23:35 +0200287#endif
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200288int (*dll_lua_getmetatable) (lua_State *L, int objindex);
289void (*dll_lua_setfield) (lua_State *L, int idx, const char *k);
290void (*dll_lua_rawset) (lua_State *L, int idx);
Bram Moolenaar17413672018-07-14 20:49:42 +0200291#if LUA_VERSION_NUM <= 502
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200292void (*dll_lua_rawseti) (lua_State *L, int idx, int n);
Bram Moolenaar17413672018-07-14 20:49:42 +0200293#else
294void (*dll_lua_rawseti) (lua_State *L, int idx, lua_Integer n);
295#endif
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200296int (*dll_lua_setmetatable) (lua_State *L, int objindex);
Bram Moolenaarbd2f3c32012-04-06 14:31:00 +0200297int (*dll_lua_next) (lua_State *L, int idx);
Bram Moolenaar2ab2e862019-12-04 21:24:53 +0100298// libs
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200299int (*dll_luaopen_base) (lua_State *L);
300int (*dll_luaopen_table) (lua_State *L);
301int (*dll_luaopen_string) (lua_State *L);
302int (*dll_luaopen_math) (lua_State *L);
Bram Moolenaar16c98f92010-07-28 22:46:08 +0200303int (*dll_luaopen_io) (lua_State *L);
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200304int (*dll_luaopen_os) (lua_State *L);
305int (*dll_luaopen_package) (lua_State *L);
306int (*dll_luaopen_debug) (lua_State *L);
Bram Moolenaar16c98f92010-07-28 22:46:08 +0200307void (*dll_luaL_openlibs) (lua_State *L);
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200308
309typedef void **luaV_function;
310typedef struct {
311 const char *name;
312 luaV_function func;
313} luaV_Reg;
314
315static const luaV_Reg luaV_dll[] = {
Bram Moolenaar2ab2e862019-12-04 21:24:53 +0100316 // lauxlib
Bram Moolenaar1dced572012-04-05 16:54:08 +0200317#if LUA_VERSION_NUM <= 501
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200318 {"luaL_register", (luaV_function) &dll_luaL_register},
Bram Moolenaar1dced572012-04-05 16:54:08 +0200319 {"luaL_prepbuffer", (luaV_function) &dll_luaL_prepbuffer},
320 {"luaL_openlib", (luaV_function) &dll_luaL_openlib},
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200321 {"luaL_typerror", (luaV_function) &dll_luaL_typerror},
Bram Moolenaar1dced572012-04-05 16:54:08 +0200322 {"luaL_loadfile", (luaV_function) &dll_luaL_loadfile},
323 {"luaL_loadbuffer", (luaV_function) &dll_luaL_loadbuffer},
324#else
325 {"luaL_prepbuffsize", (luaV_function) &dll_luaL_prepbuffsize},
326 {"luaL_setfuncs", (luaV_function) &dll_luaL_setfuncs},
327 {"luaL_loadfilex", (luaV_function) &dll_luaL_loadfilex},
328 {"luaL_loadbufferx", (luaV_function) &dll_luaL_loadbufferx},
329 {"luaL_argerror", (luaV_function) &dll_luaL_argerror},
330#endif
Bram Moolenaarbd2f3c32012-04-06 14:31:00 +0200331 {"luaL_checkany", (luaV_function) &dll_luaL_checkany},
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200332 {"luaL_checklstring", (luaV_function) &dll_luaL_checklstring},
333 {"luaL_checkinteger", (luaV_function) &dll_luaL_checkinteger},
334 {"luaL_optinteger", (luaV_function) &dll_luaL_optinteger},
335 {"luaL_checktype", (luaV_function) &dll_luaL_checktype},
336 {"luaL_error", (luaV_function) &dll_luaL_error},
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200337 {"luaL_newstate", (luaV_function) &dll_luaL_newstate},
338 {"luaL_buffinit", (luaV_function) &dll_luaL_buffinit},
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200339 {"luaL_addlstring", (luaV_function) &dll_luaL_addlstring},
340 {"luaL_pushresult", (luaV_function) &dll_luaL_pushresult},
Bram Moolenaardf1643a2020-05-17 18:53:27 +0200341 {"luaL_loadstring", (luaV_function) &dll_luaL_loadstring},
Bram Moolenaar2ab2e862019-12-04 21:24:53 +0100342 // lua
Bram Moolenaar1dced572012-04-05 16:54:08 +0200343#if LUA_VERSION_NUM <= 501
344 {"lua_tonumber", (luaV_function) &dll_lua_tonumber},
345 {"lua_tointeger", (luaV_function) &dll_lua_tointeger},
346 {"lua_call", (luaV_function) &dll_lua_call},
347 {"lua_pcall", (luaV_function) &dll_lua_pcall},
348#else
349 {"lua_tonumberx", (luaV_function) &dll_lua_tonumberx},
350 {"lua_tointegerx", (luaV_function) &dll_lua_tointegerx},
351 {"lua_callk", (luaV_function) &dll_lua_callk},
352 {"lua_pcallk", (luaV_function) &dll_lua_pcallk},
353 {"lua_getglobal", (luaV_function) &dll_lua_getglobal},
354 {"lua_setglobal", (luaV_function) &dll_lua_setglobal},
Bram Moolenaar1dced572012-04-05 16:54:08 +0200355#endif
Bram Moolenaar1f860d82015-06-27 18:36:16 +0200356#if LUA_VERSION_NUM <= 502
357 {"lua_replace", (luaV_function) &dll_lua_replace},
358 {"lua_remove", (luaV_function) &dll_lua_remove},
359#endif
360#if LUA_VERSION_NUM >= 503
361 {"lua_rotate", (luaV_function) &dll_lua_rotate},
362 {"lua_copy", (luaV_function) &dll_lua_copy},
363#endif
Bram Moolenaarbd2f3c32012-04-06 14:31:00 +0200364 {"lua_typename", (luaV_function) &dll_lua_typename},
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200365 {"lua_close", (luaV_function) &dll_lua_close},
366 {"lua_gettop", (luaV_function) &dll_lua_gettop},
367 {"lua_settop", (luaV_function) &dll_lua_settop},
368 {"lua_pushvalue", (luaV_function) &dll_lua_pushvalue},
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200369 {"lua_isnumber", (luaV_function) &dll_lua_isnumber},
370 {"lua_isstring", (luaV_function) &dll_lua_isstring},
371 {"lua_type", (luaV_function) &dll_lua_type},
372 {"lua_rawequal", (luaV_function) &dll_lua_rawequal},
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200373 {"lua_toboolean", (luaV_function) &dll_lua_toboolean},
374 {"lua_tolstring", (luaV_function) &dll_lua_tolstring},
375 {"lua_touserdata", (luaV_function) &dll_lua_touserdata},
376 {"lua_pushnil", (luaV_function) &dll_lua_pushnil},
377 {"lua_pushnumber", (luaV_function) &dll_lua_pushnumber},
378 {"lua_pushinteger", (luaV_function) &dll_lua_pushinteger},
379 {"lua_pushlstring", (luaV_function) &dll_lua_pushlstring},
380 {"lua_pushstring", (luaV_function) &dll_lua_pushstring},
381 {"lua_pushfstring", (luaV_function) &dll_lua_pushfstring},
382 {"lua_pushcclosure", (luaV_function) &dll_lua_pushcclosure},
383 {"lua_pushboolean", (luaV_function) &dll_lua_pushboolean},
384 {"lua_pushlightuserdata", (luaV_function) &dll_lua_pushlightuserdata},
385 {"lua_getfield", (luaV_function) &dll_lua_getfield},
386 {"lua_rawget", (luaV_function) &dll_lua_rawget},
Bram Moolenaar1dced572012-04-05 16:54:08 +0200387 {"lua_rawgeti", (luaV_function) &dll_lua_rawgeti},
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200388 {"lua_createtable", (luaV_function) &dll_lua_createtable},
Bram Moolenaar830e3582018-08-21 14:23:35 +0200389#if LUA_VERSION_NUM >= 504
390 {"lua_newuserdatauv", (luaV_function) &dll_lua_newuserdatauv},
391#else
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200392 {"lua_newuserdata", (luaV_function) &dll_lua_newuserdata},
Bram Moolenaar830e3582018-08-21 14:23:35 +0200393#endif
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200394 {"lua_getmetatable", (luaV_function) &dll_lua_getmetatable},
395 {"lua_setfield", (luaV_function) &dll_lua_setfield},
396 {"lua_rawset", (luaV_function) &dll_lua_rawset},
397 {"lua_rawseti", (luaV_function) &dll_lua_rawseti},
398 {"lua_setmetatable", (luaV_function) &dll_lua_setmetatable},
Bram Moolenaarbd2f3c32012-04-06 14:31:00 +0200399 {"lua_next", (luaV_function) &dll_lua_next},
Bram Moolenaar2ab2e862019-12-04 21:24:53 +0100400 // libs
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200401 {"luaopen_base", (luaV_function) &dll_luaopen_base},
402 {"luaopen_table", (luaV_function) &dll_luaopen_table},
403 {"luaopen_string", (luaV_function) &dll_luaopen_string},
404 {"luaopen_math", (luaV_function) &dll_luaopen_math},
Bram Moolenaar16c98f92010-07-28 22:46:08 +0200405 {"luaopen_io", (luaV_function) &dll_luaopen_io},
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200406 {"luaopen_os", (luaV_function) &dll_luaopen_os},
407 {"luaopen_package", (luaV_function) &dll_luaopen_package},
408 {"luaopen_debug", (luaV_function) &dll_luaopen_debug},
Bram Moolenaar16c98f92010-07-28 22:46:08 +0200409 {"luaL_openlibs", (luaV_function) &dll_luaL_openlibs},
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200410 {NULL, NULL}
411};
412
Bram Moolenaar2334b6d2010-07-22 21:32:16 +0200413static HANDLE hinstLua = NULL;
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200414
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200415 static int
416lua_link_init(char *libname, int verbose)
417{
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200418 const luaV_Reg *reg;
419 if (hinstLua) return OK;
Bram Moolenaar2334b6d2010-07-22 21:32:16 +0200420 hinstLua = load_dll(libname);
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200421 if (!hinstLua)
422 {
423 if (verbose)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100424 semsg(_(e_loadlib), libname);
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200425 return FAIL;
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200426 }
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200427 for (reg = luaV_dll; reg->func; reg++)
428 {
Bram Moolenaar2334b6d2010-07-22 21:32:16 +0200429 if ((*reg->func = symbol_from_dll(hinstLua, reg->name)) == NULL)
430 {
431 close_dll(hinstLua);
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200432 hinstLua = 0;
433 if (verbose)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100434 semsg(_(e_loadfunc), reg->name);
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200435 return FAIL;
436 }
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200437 }
438 return OK;
439}
Bram Moolenaar2ab2e862019-12-04 21:24:53 +0100440#endif // DYNAMIC_LUA
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200441
Bram Moolenaard90b6c02016-08-28 18:10:45 +0200442#if defined(DYNAMIC_LUA) || defined(PROTO)
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200443 int
444lua_enabled(int verbose)
445{
Bram Moolenaar25e4fcd2016-01-09 14:57:47 +0100446 return lua_link_init((char *)p_luadll, verbose) == OK;
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200447}
Bram Moolenaard90b6c02016-08-28 18:10:45 +0200448#endif
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200449
Bram Moolenaar1dced572012-04-05 16:54:08 +0200450#if LUA_VERSION_NUM > 501
451 static int
Bram Moolenaar4eefe472019-03-19 21:59:19 +0100452luaL_typeerror(lua_State *L, int narg, const char *tname)
Bram Moolenaar1dced572012-04-05 16:54:08 +0200453{
454 const char *msg = lua_pushfstring(L, "%s expected, got %s",
Bram Moolenaardb913952012-06-29 12:54:53 +0200455 tname, luaL_typename(L, narg));
Bram Moolenaar1dced572012-04-05 16:54:08 +0200456 return luaL_argerror(L, narg, msg);
457}
458#endif
459
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200460
Bram Moolenaar2ab2e862019-12-04 21:24:53 +0100461// ======= Internal =======
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200462
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200463 static void
464luaV_newmetatable(lua_State *L, const char *tname)
465{
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200466 lua_newtable(L);
467 lua_pushlightuserdata(L, (void *) tname);
468 lua_pushvalue(L, -2);
469 lua_rawset(L, LUA_REGISTRYINDEX);
470}
471
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200472 static void *
473luaV_toudata(lua_State *L, int ud, const char *tname)
474{
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200475 void *p = lua_touserdata(L, ud);
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200476
Bram Moolenaar2ab2e862019-12-04 21:24:53 +0100477 if (p != NULL) // value is userdata?
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200478 {
Bram Moolenaar2ab2e862019-12-04 21:24:53 +0100479 if (lua_getmetatable(L, ud)) // does it have a metatable?
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200480 {
Bram Moolenaar2ab2e862019-12-04 21:24:53 +0100481 luaV_getfield(L, tname); // get metatable
482 if (lua_rawequal(L, -1, -2)) // MTs match?
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200483 {
Bram Moolenaar2ab2e862019-12-04 21:24:53 +0100484 lua_pop(L, 2); // MTs
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200485 return p;
486 }
487 }
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200488 }
489 return NULL;
490}
491
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200492 static void *
Bram Moolenaar1dced572012-04-05 16:54:08 +0200493luaV_checkcache(lua_State *L, void *p)
494{
495 luaV_getudata(L, p);
496 if (lua_isnil(L, -1)) luaL_error(L, "invalid object");
497 lua_pop(L, 1);
498 return p;
499}
500
501#define luaV_unbox(L,luatyp,ud) (*((luatyp *) lua_touserdata((L),(ud))))
502
503#define luaV_checkvalid(L,luatyp,ud) \
504 luaV_checkcache((L), (void *) luaV_unbox((L),luatyp,(ud)))
505
506 static void *
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200507luaV_checkudata(lua_State *L, int ud, const char *tname)
508{
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200509 void *p = luaV_toudata(L, ud, tname);
Bram Moolenaar1dced572012-04-05 16:54:08 +0200510 if (p == NULL) luaL_typeerror(L, ud, tname);
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200511 return p;
512}
513
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200514 static void
515luaV_pushtypval(lua_State *L, typval_T *tv)
516{
Bram Moolenaar1dced572012-04-05 16:54:08 +0200517 if (tv == NULL)
518 {
519 lua_pushnil(L);
520 return;
521 }
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200522 switch (tv->v_type)
523 {
524 case VAR_STRING:
Bram Moolenaard04da7c2012-10-14 03:41:59 +0200525 lua_pushstring(L, tv->vval.v_string == NULL
526 ? "" : (char *)tv->vval.v_string);
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200527 break;
528 case VAR_NUMBER:
529 lua_pushinteger(L, (int) tv->vval.v_number);
530 break;
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200531#ifdef FEAT_FLOAT
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200532 case VAR_FLOAT:
533 lua_pushnumber(L, (lua_Number) tv->vval.v_float);
534 break;
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200535#endif
Bram Moolenaar1dced572012-04-05 16:54:08 +0200536 case VAR_LIST:
537 luaV_pushlist(L, tv->vval.v_list);
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200538 break;
Bram Moolenaar1dced572012-04-05 16:54:08 +0200539 case VAR_DICT:
540 luaV_pushdict(L, tv->vval.v_dict);
541 break;
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +0100542 case VAR_BOOL:
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100543 case VAR_SPECIAL:
544 if (tv->vval.v_number <= VVAL_TRUE)
545 lua_pushinteger(L, (int) tv->vval.v_number);
546 else
547 lua_pushnil(L);
548 break;
Bram Moolenaarca06da92018-07-01 15:12:05 +0200549 case VAR_FUNC:
Bram Moolenaar4eefe472019-03-19 21:59:19 +0100550 luaV_pushfuncref(L, tv->vval.v_string);
Bram Moolenaarca06da92018-07-01 15:12:05 +0200551 break;
Bram Moolenaarb7828692019-03-23 13:57:02 +0100552 case VAR_BLOB:
553 luaV_pushblob(L, tv->vval.v_blob);
554 break;
Bram Moolenaar1dced572012-04-05 16:54:08 +0200555 default:
556 lua_pushnil(L);
557 }
558}
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200559
Bram Moolenaarca06da92018-07-01 15:12:05 +0200560/*
561 * Converts lua value at 'pos' to typval 'tv'.
562 * Returns OK or FAIL.
563 */
564 static int
565luaV_totypval(lua_State *L, int pos, typval_T *tv)
Bram Moolenaar1dced572012-04-05 16:54:08 +0200566{
Bram Moolenaarca06da92018-07-01 15:12:05 +0200567 int status = OK;
568
569 switch (lua_type(L, pos))
570 {
Bram Moolenaar1dced572012-04-05 16:54:08 +0200571 case LUA_TBOOLEAN:
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +0100572 tv->v_type = VAR_BOOL;
Bram Moolenaar1dced572012-04-05 16:54:08 +0200573 tv->vval.v_number = (varnumber_T) lua_toboolean(L, pos);
574 break;
Bram Moolenaar9067cd62019-01-01 00:41:54 +0100575 case LUA_TNIL:
576 tv->v_type = VAR_SPECIAL;
577 tv->vval.v_number = VVAL_NULL;
578 break;
Bram Moolenaar1dced572012-04-05 16:54:08 +0200579 case LUA_TSTRING:
580 tv->v_type = VAR_STRING;
581 tv->vval.v_string = vim_strsave((char_u *) lua_tostring(L, pos));
582 break;
583 case LUA_TNUMBER:
584#ifdef FEAT_FLOAT
Bram Moolenaareb04f082020-05-17 14:32:35 +0200585 {
586 const lua_Number n = lua_tonumber(L, pos);
587
588 if (n > (lua_Number)INT64_MAX || n < (lua_Number)INT64_MIN
589 || ((lua_Number)((varnumber_T)n)) != n)
590 {
591 tv->v_type = VAR_FLOAT;
592 tv->vval.v_float = (float_T)n;
593 }
594 else
595 {
596 tv->v_type = VAR_NUMBER;
597 tv->vval.v_number = (varnumber_T)n;
598 }
599 }
Bram Moolenaar1dced572012-04-05 16:54:08 +0200600#else
601 tv->v_type = VAR_NUMBER;
602 tv->vval.v_number = (varnumber_T) lua_tointeger(L, pos);
603#endif
604 break;
Bram Moolenaar801ab062020-06-25 19:27:56 +0200605 case LUA_TFUNCTION:
606 {
607 char_u *name;
608 lua_pushvalue(L, pos);
609 luaV_CFuncState *state = ALLOC_CLEAR_ONE(luaV_CFuncState);
610 state->lua_funcref = luaL_ref(L, LUA_REGISTRYINDEX);
611 state->L = L;
612 state->lua_tableref = LUA_NOREF;
613 name = register_cfunc(&luaV_call_lua_func,
614 &luaV_call_lua_func_free, state);
615 tv->v_type = VAR_FUNC;
616 tv->vval.v_string = vim_strsave(name);
617 break;
618 }
619 case LUA_TTABLE:
620 {
621 lua_pushvalue(L, pos);
622 int lua_tableref = luaL_ref(L, LUA_REGISTRYINDEX);
623 if (lua_getmetatable(L, pos)) {
624 lua_getfield(L, -1, LUA___CALL);
625 if (lua_isfunction(L, -1)) {
626 char_u *name;
627 int lua_funcref = luaL_ref(L, LUA_REGISTRYINDEX);
628 luaV_CFuncState *state = ALLOC_CLEAR_ONE(luaV_CFuncState);
629 state->lua_funcref = lua_funcref;
630 state->L = L;
631 state->lua_tableref = lua_tableref;
632 name = register_cfunc(&luaV_call_lua_func,
633 &luaV_call_lua_func_free, state);
634 tv->v_type = VAR_FUNC;
635 tv->vval.v_string = vim_strsave(name);
636 break;
637 }
638 }
639 tv->v_type = VAR_NUMBER;
640 tv->vval.v_number = 0;
641 status = FAIL;
642 break;
643 }
Bram Moolenaarca06da92018-07-01 15:12:05 +0200644 case LUA_TUSERDATA:
645 {
Bram Moolenaar1dced572012-04-05 16:54:08 +0200646 void *p = lua_touserdata(L, pos);
Bram Moolenaarca06da92018-07-01 15:12:05 +0200647
Bram Moolenaarb7828692019-03-23 13:57:02 +0100648 if (lua_getmetatable(L, pos)) // has metatable?
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200649 {
Bram Moolenaarb7828692019-03-23 13:57:02 +0100650 // check list
Bram Moolenaar1dced572012-04-05 16:54:08 +0200651 luaV_getfield(L, LUAVIM_LIST);
652 if (lua_rawequal(L, -1, -2))
Bram Moolenaar2334b6d2010-07-22 21:32:16 +0200653 {
Bram Moolenaar1dced572012-04-05 16:54:08 +0200654 tv->v_type = VAR_LIST;
655 tv->vval.v_list = *((luaV_List *) p);
656 ++tv->vval.v_list->lv_refcount;
Bram Moolenaarb7828692019-03-23 13:57:02 +0100657 lua_pop(L, 2); // MTs
Bram Moolenaarca06da92018-07-01 15:12:05 +0200658 break;
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200659 }
Bram Moolenaarb7828692019-03-23 13:57:02 +0100660 // check dict
Bram Moolenaar1dced572012-04-05 16:54:08 +0200661 luaV_getfield(L, LUAVIM_DICT);
662 if (lua_rawequal(L, -1, -3))
663 {
664 tv->v_type = VAR_DICT;
665 tv->vval.v_dict = *((luaV_Dict *) p);
666 ++tv->vval.v_dict->dv_refcount;
Bram Moolenaarb7828692019-03-23 13:57:02 +0100667 lua_pop(L, 3); // MTs
Bram Moolenaarca06da92018-07-01 15:12:05 +0200668 break;
Bram Moolenaar1dced572012-04-05 16:54:08 +0200669 }
Bram Moolenaarb7828692019-03-23 13:57:02 +0100670 // check blob
671 luaV_getfield(L, LUAVIM_BLOB);
Bram Moolenaarca06da92018-07-01 15:12:05 +0200672 if (lua_rawequal(L, -1, -4))
673 {
Bram Moolenaarb7828692019-03-23 13:57:02 +0100674 tv->v_type = VAR_BLOB;
675 tv->vval.v_blob = *((luaV_Blob *) p);
676 ++tv->vval.v_blob->bv_refcount;
677 lua_pop(L, 4); // MTs
678 break;
679 }
680 // check funcref
681 luaV_getfield(L, LUAVIM_FUNCREF);
682 if (lua_rawequal(L, -1, -5))
683 {
Bram Moolenaarca06da92018-07-01 15:12:05 +0200684 luaV_Funcref *f = (luaV_Funcref *) p;
Bram Moolenaar4eefe472019-03-19 21:59:19 +0100685 func_ref(f->name);
686 tv->v_type = VAR_FUNC;
687 tv->vval.v_string = vim_strsave(f->name);
Bram Moolenaarb7828692019-03-23 13:57:02 +0100688 lua_pop(L, 5); // MTs
Bram Moolenaarca06da92018-07-01 15:12:05 +0200689 break;
690 }
Bram Moolenaarb7828692019-03-23 13:57:02 +0100691 lua_pop(L, 4); // MTs
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200692 }
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200693 }
Bram Moolenaarb7828692019-03-23 13:57:02 +0100694 // FALLTHROUGH
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200695 default:
Bram Moolenaar1dced572012-04-05 16:54:08 +0200696 tv->v_type = VAR_NUMBER;
697 tv->vval.v_number = 0;
Bram Moolenaarca06da92018-07-01 15:12:05 +0200698 status = FAIL;
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200699 }
Bram Moolenaarca06da92018-07-01 15:12:05 +0200700 return status;
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200701}
702
Bram Moolenaar2ab2e862019-12-04 21:24:53 +0100703/*
704 * similar to luaL_addlstring, but replaces \0 with \n if toline and
705 * \n with \0 otherwise
706 */
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200707 static void
708luaV_addlstring(luaL_Buffer *b, const char *s, size_t l, int toline)
709{
710 while (l--)
711 {
712 if (*s == '\0' && toline)
713 luaL_addchar(b, '\n');
714 else if (*s == '\n' && !toline)
715 luaL_addchar(b, '\0');
716 else
717 luaL_addchar(b, *s);
718 s++;
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200719 }
720}
721
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200722 static void
723luaV_pushline(lua_State *L, buf_T *buf, linenr_T n)
724{
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200725 const char *s = (const char *) ml_get_buf(buf, n, FALSE);
726 luaL_Buffer b;
727 luaL_buffinit(L, &b);
728 luaV_addlstring(&b, s, strlen(s), 0);
729 luaL_pushresult(&b);
730}
731
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200732 static char_u *
733luaV_toline(lua_State *L, int pos)
734{
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200735 size_t l;
736 const char *s = lua_tolstring(L, pos, &l);
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200737
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200738 luaL_Buffer b;
739 luaL_buffinit(L, &b);
740 luaV_addlstring(&b, s, l, 1);
741 luaL_pushresult(&b);
742 return (char_u *) lua_tostring(L, -1);
743}
744
Bram Moolenaar2ab2e862019-12-04 21:24:53 +0100745/*
746 * pops a string s from the top of the stack and calls mf(t) for pieces t of
747 * s separated by newlines
748 */
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200749 static void
750luaV_msgfunc(lua_State *L, msgfunc_T mf)
751{
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200752 luaL_Buffer b;
753 size_t l;
754 const char *p, *s = lua_tolstring(L, -1, &l);
755 luaL_buffinit(L, &b);
756 luaV_addlstring(&b, s, l, 0);
757 luaL_pushresult(&b);
Bram Moolenaar2ab2e862019-12-04 21:24:53 +0100758 // break string
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200759 p = s = lua_tolstring(L, -1, &l);
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200760 while (l--)
761 {
Bram Moolenaar2ab2e862019-12-04 21:24:53 +0100762 if (*p++ == '\0') // break?
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200763 {
764 mf((char_u *) s);
765 s = p;
766 }
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200767 }
768 mf((char_u *) s);
Bram Moolenaar2ab2e862019-12-04 21:24:53 +0100769 lua_pop(L, 2); // original and modified strings
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200770}
771
Bram Moolenaar1dced572012-04-05 16:54:08 +0200772#define luaV_newtype(typ,tname,luatyp,luatname) \
773 static luatyp * \
Bram Moolenaar4eefe472019-03-19 21:59:19 +0100774 luaV_new##tname(lua_State *L, typ *obj) \
Bram Moolenaar1dced572012-04-05 16:54:08 +0200775 { \
776 luatyp *o = (luatyp *) lua_newuserdata(L, sizeof(luatyp)); \
777 *o = obj; \
778 luaV_setudata(L, obj); /* cache[obj] = udata */ \
779 luaV_getfield(L, luatname); \
780 lua_setmetatable(L, -2); \
781 return o; \
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200782 }
Bram Moolenaar1dced572012-04-05 16:54:08 +0200783
784#define luaV_pushtype(typ,tname,luatyp) \
785 static luatyp * \
Bram Moolenaarca06da92018-07-01 15:12:05 +0200786 luaV_push##tname(lua_State *L, typ *obj) \
Bram Moolenaar1dced572012-04-05 16:54:08 +0200787 { \
788 luatyp *o = NULL; \
789 if (obj == NULL) \
790 lua_pushnil(L); \
791 else { \
792 luaV_getudata(L, obj); \
793 if (lua_isnil(L, -1)) /* not interned? */ \
794 { \
795 lua_pop(L, 1); \
796 o = luaV_new##tname(L, obj); \
797 } \
798 else \
799 o = (luatyp *) lua_touserdata(L, -1); \
800 } \
801 return o; \
802 }
803
804#define luaV_type_tostring(tname,luatname) \
805 static int \
Bram Moolenaar4eefe472019-03-19 21:59:19 +0100806 luaV_##tname##_tostring(lua_State *L) \
Bram Moolenaar1dced572012-04-05 16:54:08 +0200807 { \
808 lua_pushfstring(L, "%s: %p", luatname, lua_touserdata(L, 1)); \
809 return 1; \
810 }
811
Bram Moolenaar2ab2e862019-12-04 21:24:53 +0100812// ======= List type =======
Bram Moolenaar1dced572012-04-05 16:54:08 +0200813
814 static luaV_List *
Bram Moolenaar4eefe472019-03-19 21:59:19 +0100815luaV_newlist(lua_State *L, list_T *lis)
Bram Moolenaar1dced572012-04-05 16:54:08 +0200816{
817 luaV_List *l = (luaV_List *) lua_newuserdata(L, sizeof(luaV_List));
818 *l = lis;
Bram Moolenaar2ab2e862019-12-04 21:24:53 +0100819 lis->lv_refcount++; // reference in Lua
820 luaV_setudata(L, lis); // cache[lis] = udata
Bram Moolenaar1dced572012-04-05 16:54:08 +0200821 luaV_getfield(L, LUAVIM_LIST);
822 lua_setmetatable(L, -2);
823 return l;
824}
825
826luaV_pushtype(list_T, list, luaV_List)
827luaV_type_tostring(list, LUAVIM_LIST)
828
829 static int
Bram Moolenaar4eefe472019-03-19 21:59:19 +0100830luaV_list_len(lua_State *L)
Bram Moolenaar1dced572012-04-05 16:54:08 +0200831{
832 list_T *l = luaV_unbox(L, luaV_List, 1);
Bram Moolenaarb7828692019-03-23 13:57:02 +0100833 lua_pushinteger(L, (int) list_len(l));
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200834 return 1;
835}
836
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200837 static int
Bram Moolenaar4eefe472019-03-19 21:59:19 +0100838luaV_list_iter(lua_State *L)
Bram Moolenaar1dced572012-04-05 16:54:08 +0200839{
840 listitem_T *li = (listitem_T *) lua_touserdata(L, lua_upvalueindex(2));
841 if (li == NULL) return 0;
842 luaV_pushtypval(L, &li->li_tv);
843 lua_pushlightuserdata(L, (void *) li->li_next);
844 lua_replace(L, lua_upvalueindex(2));
845 return 1;
846}
847
848 static int
Bram Moolenaar4eefe472019-03-19 21:59:19 +0100849luaV_list_call(lua_State *L)
Bram Moolenaar1dced572012-04-05 16:54:08 +0200850{
851 list_T *l = luaV_unbox(L, luaV_List, 1);
Bram Moolenaar2ab2e862019-12-04 21:24:53 +0100852 lua_pushvalue(L, lua_upvalueindex(1)); // pass cache table along
Bram Moolenaar1dced572012-04-05 16:54:08 +0200853 lua_pushlightuserdata(L, (void *) l->lv_first);
854 lua_pushcclosure(L, luaV_list_iter, 2);
855 return 1;
856}
857
858 static int
Bram Moolenaar4eefe472019-03-19 21:59:19 +0100859luaV_list_index(lua_State *L)
Bram Moolenaar1dced572012-04-05 16:54:08 +0200860{
861 list_T *l = luaV_unbox(L, luaV_List, 1);
Bram Moolenaar2ab2e862019-12-04 21:24:53 +0100862 if (lua_isnumber(L, 2)) // list item?
Bram Moolenaar1dced572012-04-05 16:54:08 +0200863 {
864 listitem_T *li = list_find(l, (long) luaL_checkinteger(L, 2));
865 if (li == NULL)
866 lua_pushnil(L);
867 else
868 luaV_pushtypval(L, &li->li_tv);
869 }
Bram Moolenaar2ab2e862019-12-04 21:24:53 +0100870 else if (lua_isstring(L, 2)) // method?
Bram Moolenaar1dced572012-04-05 16:54:08 +0200871 {
872 const char *s = lua_tostring(L, 2);
873 if (strncmp(s, "add", 3) == 0
Bram Moolenaarb3766472013-04-15 13:49:21 +0200874 || strncmp(s, "insert", 6) == 0)
Bram Moolenaar1dced572012-04-05 16:54:08 +0200875 {
876 lua_getmetatable(L, 1);
877 lua_getfield(L, -1, s);
878 }
879 else
880 lua_pushnil(L);
881 }
882 else
883 lua_pushnil(L);
884 return 1;
885}
886
887 static int
Bram Moolenaar4eefe472019-03-19 21:59:19 +0100888luaV_list_newindex(lua_State *L)
Bram Moolenaar1dced572012-04-05 16:54:08 +0200889{
890 list_T *l = luaV_unbox(L, luaV_List, 1);
891 long n = (long) luaL_checkinteger(L, 2);
892 listitem_T *li;
893 if (l->lv_lock)
894 luaL_error(L, "list is locked");
895 li = list_find(l, n);
896 if (li == NULL) return 0;
Bram Moolenaar2ab2e862019-12-04 21:24:53 +0100897 if (lua_isnil(L, 3)) // remove?
Bram Moolenaar1dced572012-04-05 16:54:08 +0200898 {
Bram Moolenaar3ec7f4e2014-05-07 17:31:37 +0200899 vimlist_remove(l, li, li);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100900 listitem_free(l, li);
Bram Moolenaar1dced572012-04-05 16:54:08 +0200901 }
902 else
903 {
904 typval_T v;
Bram Moolenaarca06da92018-07-01 15:12:05 +0200905 luaV_checktypval(L, 3, &v, "setting list item");
Bram Moolenaar1dced572012-04-05 16:54:08 +0200906 clear_tv(&li->li_tv);
907 copy_tv(&v, &li->li_tv);
Bram Moolenaarb3766472013-04-15 13:49:21 +0200908 clear_tv(&v);
Bram Moolenaar1dced572012-04-05 16:54:08 +0200909 }
910 return 0;
911}
912
913 static int
Bram Moolenaar4eefe472019-03-19 21:59:19 +0100914luaV_list_add(lua_State *L)
Bram Moolenaar1dced572012-04-05 16:54:08 +0200915{
916 luaV_List *lis = luaV_checkudata(L, 1, LUAVIM_LIST);
917 list_T *l = (list_T *) luaV_checkcache(L, (void *) *lis);
Bram Moolenaarb3766472013-04-15 13:49:21 +0200918 typval_T v;
Bram Moolenaar1dced572012-04-05 16:54:08 +0200919 if (l->lv_lock)
920 luaL_error(L, "list is locked");
Bram Moolenaarb3766472013-04-15 13:49:21 +0200921 lua_settop(L, 2);
Bram Moolenaarca06da92018-07-01 15:12:05 +0200922 luaV_checktypval(L, 2, &v, "adding list item");
Bram Moolenaarb3766472013-04-15 13:49:21 +0200923 if (list_append_tv(l, &v) == FAIL)
Bram Moolenaarca06da92018-07-01 15:12:05 +0200924 luaL_error(L, "failed to add item to list");
Bram Moolenaarb3766472013-04-15 13:49:21 +0200925 clear_tv(&v);
Bram Moolenaar1dced572012-04-05 16:54:08 +0200926 lua_settop(L, 1);
927 return 1;
928}
929
930 static int
Bram Moolenaar4eefe472019-03-19 21:59:19 +0100931luaV_list_insert(lua_State *L)
Bram Moolenaar1dced572012-04-05 16:54:08 +0200932{
933 luaV_List *lis = luaV_checkudata(L, 1, LUAVIM_LIST);
934 list_T *l = (list_T *) luaV_checkcache(L, (void *) *lis);
Bram Moolenaar46538ee2015-02-17 16:28:55 +0100935 long pos = (long) luaL_optinteger(L, 3, 0);
Bram Moolenaar1dced572012-04-05 16:54:08 +0200936 listitem_T *li = NULL;
937 typval_T v;
938 if (l->lv_lock)
939 luaL_error(L, "list is locked");
940 if (pos < l->lv_len)
941 {
942 li = list_find(l, pos);
943 if (li == NULL)
944 luaL_error(L, "invalid position");
945 }
946 lua_settop(L, 2);
Bram Moolenaarca06da92018-07-01 15:12:05 +0200947 luaV_checktypval(L, 2, &v, "inserting list item");
Bram Moolenaarb3766472013-04-15 13:49:21 +0200948 if (list_insert_tv(l, &v, li) == FAIL)
Bram Moolenaarca06da92018-07-01 15:12:05 +0200949 luaL_error(L, "failed to add item to list");
Bram Moolenaarb3766472013-04-15 13:49:21 +0200950 clear_tv(&v);
Bram Moolenaar1dced572012-04-05 16:54:08 +0200951 lua_settop(L, 1);
952 return 1;
953}
954
955static const luaL_Reg luaV_List_mt[] = {
956 {"__tostring", luaV_list_tostring},
Bram Moolenaar1dced572012-04-05 16:54:08 +0200957 {"__len", luaV_list_len},
958 {"__call", luaV_list_call},
959 {"__index", luaV_list_index},
960 {"__newindex", luaV_list_newindex},
961 {"add", luaV_list_add},
962 {"insert", luaV_list_insert},
963 {NULL, NULL}
964};
965
966
Bram Moolenaar2ab2e862019-12-04 21:24:53 +0100967// ======= Dict type =======
Bram Moolenaar1dced572012-04-05 16:54:08 +0200968
969 static luaV_Dict *
Bram Moolenaar4eefe472019-03-19 21:59:19 +0100970luaV_newdict(lua_State *L, dict_T *dic)
Bram Moolenaar1dced572012-04-05 16:54:08 +0200971{
972 luaV_Dict *d = (luaV_Dict *) lua_newuserdata(L, sizeof(luaV_Dict));
973 *d = dic;
Bram Moolenaar2ab2e862019-12-04 21:24:53 +0100974 dic->dv_refcount++; // reference in Lua
975 luaV_setudata(L, dic); // cache[dic] = udata
Bram Moolenaar1dced572012-04-05 16:54:08 +0200976 luaV_getfield(L, LUAVIM_DICT);
977 lua_setmetatable(L, -2);
978 return d;
979}
980
981luaV_pushtype(dict_T, dict, luaV_Dict)
982luaV_type_tostring(dict, LUAVIM_DICT)
983
984 static int
Bram Moolenaar4eefe472019-03-19 21:59:19 +0100985luaV_dict_len(lua_State *L)
Bram Moolenaar1dced572012-04-05 16:54:08 +0200986{
987 dict_T *d = luaV_unbox(L, luaV_Dict, 1);
Bram Moolenaarb7828692019-03-23 13:57:02 +0100988 lua_pushinteger(L, (int) dict_len(d));
Bram Moolenaar1dced572012-04-05 16:54:08 +0200989 return 1;
990}
991
992 static int
Bram Moolenaar4eefe472019-03-19 21:59:19 +0100993luaV_dict_iter(lua_State *L UNUSED)
Bram Moolenaar1dced572012-04-05 16:54:08 +0200994{
Bram Moolenaarfeeaa682013-02-14 22:19:51 +0100995#ifdef FEAT_EVAL
Bram Moolenaar1dced572012-04-05 16:54:08 +0200996 hashitem_T *hi = (hashitem_T *) lua_touserdata(L, lua_upvalueindex(2));
997 int n = lua_tointeger(L, lua_upvalueindex(3));
998 dictitem_T *di;
999 if (n <= 0) return 0;
1000 while (HASHITEM_EMPTY(hi)) hi++;
1001 di = dict_lookup(hi);
1002 lua_pushstring(L, (char *) hi->hi_key);
1003 luaV_pushtypval(L, &di->di_tv);
1004 lua_pushlightuserdata(L, (void *) (hi + 1));
1005 lua_replace(L, lua_upvalueindex(2));
1006 lua_pushinteger(L, n - 1);
1007 lua_replace(L, lua_upvalueindex(3));
1008 return 2;
Bram Moolenaarfeeaa682013-02-14 22:19:51 +01001009#else
1010 return 0;
1011#endif
Bram Moolenaar1dced572012-04-05 16:54:08 +02001012}
1013
1014 static int
Bram Moolenaar4eefe472019-03-19 21:59:19 +01001015luaV_dict_call(lua_State *L)
Bram Moolenaar1dced572012-04-05 16:54:08 +02001016{
1017 dict_T *d = luaV_unbox(L, luaV_Dict, 1);
1018 hashtab_T *ht = &d->dv_hashtab;
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001019 lua_pushvalue(L, lua_upvalueindex(1)); // pass cache table along
Bram Moolenaar1dced572012-04-05 16:54:08 +02001020 lua_pushlightuserdata(L, (void *) ht->ht_array);
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001021 lua_pushinteger(L, ht->ht_used); // # remaining items
Bram Moolenaar1dced572012-04-05 16:54:08 +02001022 lua_pushcclosure(L, luaV_dict_iter, 3);
1023 return 1;
1024}
1025
1026 static int
Bram Moolenaarca06da92018-07-01 15:12:05 +02001027luaV_dict_index(lua_State *L)
Bram Moolenaar1dced572012-04-05 16:54:08 +02001028{
1029 dict_T *d = luaV_unbox(L, luaV_Dict, 1);
1030 char_u *key = (char_u *) luaL_checkstring(L, 2);
1031 dictitem_T *di = dict_find(d, key, -1);
Bram Moolenaarca06da92018-07-01 15:12:05 +02001032
Bram Moolenaar1dced572012-04-05 16:54:08 +02001033 if (di == NULL)
1034 lua_pushnil(L);
1035 else
Bram Moolenaarca06da92018-07-01 15:12:05 +02001036 {
Bram Moolenaar1dced572012-04-05 16:54:08 +02001037 luaV_pushtypval(L, &di->di_tv);
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001038 if (di->di_tv.v_type == VAR_FUNC) // funcref?
Bram Moolenaarca06da92018-07-01 15:12:05 +02001039 {
1040 luaV_Funcref *f = (luaV_Funcref *) lua_touserdata(L, -1);
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001041 f->self = d; // keep "self" reference
Bram Moolenaarca06da92018-07-01 15:12:05 +02001042 d->dv_refcount++;
1043 }
1044 }
Bram Moolenaar1dced572012-04-05 16:54:08 +02001045 return 1;
1046}
1047
1048 static int
Bram Moolenaarca06da92018-07-01 15:12:05 +02001049luaV_dict_newindex(lua_State *L)
Bram Moolenaar1dced572012-04-05 16:54:08 +02001050{
1051 dict_T *d = luaV_unbox(L, luaV_Dict, 1);
1052 char_u *key = (char_u *) luaL_checkstring(L, 2);
1053 dictitem_T *di;
Bram Moolenaarca06da92018-07-01 15:12:05 +02001054 typval_T v;
Bram Moolenaar713bf9e92019-03-16 16:38:41 +01001055
Bram Moolenaar1dced572012-04-05 16:54:08 +02001056 if (d->dv_lock)
1057 luaL_error(L, "dict is locked");
Bram Moolenaard6ef5f92018-07-13 22:08:23 +02001058 if (key == NULL)
1059 return 0;
1060 if (*key == NUL)
Bram Moolenaarca06da92018-07-01 15:12:05 +02001061 luaL_error(L, "empty key");
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001062 if (!lua_isnil(L, 3)) // read value?
Bram Moolenaar17413672018-07-14 20:49:42 +02001063 {
Bram Moolenaarca06da92018-07-01 15:12:05 +02001064 luaV_checktypval(L, 3, &v, "setting dict item");
1065 if (d->dv_scope == VAR_DEF_SCOPE && v.v_type == VAR_FUNC)
1066 luaL_error(L, "cannot assign funcref to builtin scope");
1067 }
Bram Moolenaar1dced572012-04-05 16:54:08 +02001068 di = dict_find(d, key, -1);
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001069 if (di == NULL) // non-existing key?
Bram Moolenaar1dced572012-04-05 16:54:08 +02001070 {
Bram Moolenaard6ef5f92018-07-13 22:08:23 +02001071 if (lua_isnil(L, 3))
1072 return 0;
Bram Moolenaar1dced572012-04-05 16:54:08 +02001073 di = dictitem_alloc(key);
Bram Moolenaard6ef5f92018-07-13 22:08:23 +02001074 if (di == NULL)
1075 return 0;
Bram Moolenaar1dced572012-04-05 16:54:08 +02001076 if (dict_add(d, di) == FAIL)
1077 {
Bram Moolenaard6ef5f92018-07-13 22:08:23 +02001078 vim_free(di);
1079 return 0;
Bram Moolenaar1dced572012-04-05 16:54:08 +02001080 }
1081 }
1082 else
1083 clear_tv(&di->di_tv);
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001084 if (lua_isnil(L, 3)) // remove?
Bram Moolenaar1dced572012-04-05 16:54:08 +02001085 {
1086 hashitem_T *hi = hash_find(&d->dv_hashtab, di->di_key);
1087 hash_remove(&d->dv_hashtab, hi);
1088 dictitem_free(di);
1089 }
Bram Moolenaarca06da92018-07-01 15:12:05 +02001090 else
1091 {
Bram Moolenaar1dced572012-04-05 16:54:08 +02001092 copy_tv(&v, &di->di_tv);
Bram Moolenaarb3766472013-04-15 13:49:21 +02001093 clear_tv(&v);
Bram Moolenaar1dced572012-04-05 16:54:08 +02001094 }
1095 return 0;
1096}
1097
1098static const luaL_Reg luaV_Dict_mt[] = {
1099 {"__tostring", luaV_dict_tostring},
Bram Moolenaar1dced572012-04-05 16:54:08 +02001100 {"__len", luaV_dict_len},
1101 {"__call", luaV_dict_call},
1102 {"__index", luaV_dict_index},
1103 {"__newindex", luaV_dict_newindex},
1104 {NULL, NULL}
1105};
1106
1107
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001108// ======= Blob type =======
Bram Moolenaarb7828692019-03-23 13:57:02 +01001109
1110 static luaV_Blob *
1111luaV_newblob(lua_State *L, blob_T *blo)
1112{
1113 luaV_Blob *b = (luaV_Blob *) lua_newuserdata(L, sizeof(luaV_Blob));
1114 *b = blo;
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001115 blo->bv_refcount++; // reference in Lua
1116 luaV_setudata(L, blo); // cache[blo] = udata
Bram Moolenaarb7828692019-03-23 13:57:02 +01001117 luaV_getfield(L, LUAVIM_BLOB);
1118 lua_setmetatable(L, -2);
1119 return b;
1120}
1121
1122luaV_pushtype(blob_T, blob, luaV_Blob)
1123luaV_type_tostring(blob, LUAVIM_BLOB)
1124
1125 static int
1126luaV_blob_gc(lua_State *L)
1127{
1128 blob_T *b = luaV_unbox(L, luaV_Blob, 1);
1129 blob_unref(b);
1130 return 0;
1131}
1132
1133 static int
1134luaV_blob_len(lua_State *L)
1135{
1136 blob_T *b = luaV_unbox(L, luaV_Blob, 1);
1137 lua_pushinteger(L, (int) blob_len(b));
1138 return 1;
1139}
1140
1141 static int
1142luaV_blob_index(lua_State *L)
1143{
1144 blob_T *b = luaV_unbox(L, luaV_Blob, 1);
1145 if (lua_isnumber(L, 2))
1146 {
1147 int idx = luaL_checkinteger(L, 2);
1148 if (idx < blob_len(b))
1149 lua_pushnumber(L, (lua_Number) blob_get(b, idx));
1150 else
1151 lua_pushnil(L);
1152 }
1153 else if (lua_isstring(L, 2))
1154 {
1155 const char *s = lua_tostring(L, 2);
1156 if (strncmp(s, "add", 3) == 0)
1157 {
1158 lua_getmetatable(L, 1);
1159 lua_getfield(L, -1, s);
1160 }
1161 else
1162 lua_pushnil(L);
1163 }
1164 else
1165 lua_pushnil(L);
1166 return 1;
1167}
1168
1169 static int
1170luaV_blob_newindex(lua_State *L)
1171{
1172 blob_T *b = luaV_unbox(L, luaV_Blob, 1);
1173 if (b->bv_lock)
1174 luaL_error(L, "blob is locked");
1175 if (lua_isnumber(L, 2))
1176 {
1177 long len = blob_len(b);
1178 int idx = luaL_checkinteger(L, 2);
1179 int val = luaL_checkinteger(L, 3);
1180 if (idx < len || (idx == len && ga_grow(&b->bv_ga, 1) == OK))
1181 {
1182 blob_set(b, idx, (char_u) val);
1183 if (idx == len)
1184 ++b->bv_ga.ga_len;
1185 }
1186 else
1187 luaL_error(L, "index out of range");
1188 }
1189 return 0;
1190}
1191
1192 static int
1193luaV_blob_add(lua_State *L)
1194{
1195 luaV_Blob *blo = luaV_checkudata(L, 1, LUAVIM_BLOB);
1196 blob_T *b = (blob_T *) luaV_checkcache(L, (void *) *blo);
1197 if (b->bv_lock)
1198 luaL_error(L, "blob is locked");
1199 lua_settop(L, 2);
1200 if (!lua_isstring(L, 2))
1201 luaL_error(L, "string expected, got %s", luaL_typename(L, 2));
1202 else
1203 {
1204 size_t i, l = 0;
1205 const char *s = lua_tolstring(L, 2, &l);
1206
Bram Moolenaar5f1d3ae2020-02-11 22:37:35 +01001207 if (ga_grow(&b->bv_ga, (int)l) == OK)
Bram Moolenaar6fb5c972019-03-26 21:44:20 +01001208 for (i = 0; i < l; ++i)
1209 ga_append(&b->bv_ga, s[i]);
Bram Moolenaarb7828692019-03-23 13:57:02 +01001210 }
1211 lua_settop(L, 1);
1212 return 1;
1213}
1214
1215static const luaL_Reg luaV_Blob_mt[] = {
1216 {"__tostring", luaV_blob_tostring},
1217 {"__gc", luaV_blob_gc},
1218 {"__len", luaV_blob_len},
1219 {"__index", luaV_blob_index},
1220 {"__newindex", luaV_blob_newindex},
1221 {"add", luaV_blob_add},
1222 {NULL, NULL}
1223};
1224
1225
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001226// ======= Funcref type =======
Bram Moolenaarca06da92018-07-01 15:12:05 +02001227
1228 static luaV_Funcref *
1229luaV_newfuncref(lua_State *L, char_u *name)
1230{
1231 luaV_Funcref *f = (luaV_Funcref *)lua_newuserdata(L, sizeof(luaV_Funcref));
1232
1233 if (name != NULL)
1234 {
Bram Moolenaar4eefe472019-03-19 21:59:19 +01001235 func_ref(name);
1236 f->name = vim_strsave(name);
Bram Moolenaarca06da92018-07-01 15:12:05 +02001237 }
Bram Moolenaarca06da92018-07-01 15:12:05 +02001238 f->self = NULL;
1239 luaV_getfield(L, LUAVIM_FUNCREF);
1240 lua_setmetatable(L, -2);
1241 return f;
1242}
1243
1244 static luaV_Funcref *
Bram Moolenaar4eefe472019-03-19 21:59:19 +01001245luaV_pushfuncref(lua_State *L, char_u *name)
Bram Moolenaarca06da92018-07-01 15:12:05 +02001246{
Bram Moolenaar4eefe472019-03-19 21:59:19 +01001247 return luaV_newfuncref(L, name);
Bram Moolenaarca06da92018-07-01 15:12:05 +02001248}
1249
1250
1251luaV_type_tostring(funcref, LUAVIM_FUNCREF)
1252
1253 static int
1254luaV_funcref_gc(lua_State *L)
1255{
1256 luaV_Funcref *f = (luaV_Funcref *) lua_touserdata(L, 1);
1257
Bram Moolenaar4eefe472019-03-19 21:59:19 +01001258 func_unref(f->name);
1259 vim_free(f->name);
1260 // NOTE: Don't call "dict_unref(f->self)", because the dict of "f->self"
1261 // will be (or has been already) freed by Vim's garbage collection.
Bram Moolenaarca06da92018-07-01 15:12:05 +02001262 return 0;
1263}
1264
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001265// equivalent to string(funcref)
Bram Moolenaarca06da92018-07-01 15:12:05 +02001266 static int
1267luaV_funcref_len(lua_State *L)
1268{
1269 luaV_Funcref *f = (luaV_Funcref *) lua_touserdata(L, 1);
1270
Bram Moolenaar4eefe472019-03-19 21:59:19 +01001271 lua_pushstring(L, (const char *) f->name);
Bram Moolenaarca06da92018-07-01 15:12:05 +02001272 return 1;
1273}
1274
1275 static int
1276luaV_funcref_call(lua_State *L)
1277{
1278 luaV_Funcref *f = (luaV_Funcref *) lua_touserdata(L, 1);
Bram Moolenaar4eefe472019-03-19 21:59:19 +01001279 int i, n = lua_gettop(L) - 1; // #args
1280 int status = FAIL;
1281 typval_T args;
1282 typval_T rettv;
Bram Moolenaarca06da92018-07-01 15:12:05 +02001283
Bram Moolenaar4eefe472019-03-19 21:59:19 +01001284 args.v_type = VAR_LIST;
1285 args.vval.v_list = list_alloc();
1286 rettv.v_type = VAR_UNKNOWN; // as in clear_tv
1287 if (args.vval.v_list != NULL)
Bram Moolenaard6ef5f92018-07-13 22:08:23 +02001288 {
Bram Moolenaar4eefe472019-03-19 21:59:19 +01001289 typval_T v;
1290
Bram Moolenaar17413672018-07-14 20:49:42 +02001291 for (i = 0; i < n; i++)
1292 {
Bram Moolenaard6ef5f92018-07-13 22:08:23 +02001293 luaV_checktypval(L, i + 2, &v, "calling funcref");
Bram Moolenaar4eefe472019-03-19 21:59:19 +01001294 list_append_tv(args.vval.v_list, &v);
Bram Moolenaar713bf9e92019-03-16 16:38:41 +01001295 clear_tv(&v);
Bram Moolenaard6ef5f92018-07-13 22:08:23 +02001296 }
Bram Moolenaar4eefe472019-03-19 21:59:19 +01001297 status = func_call(f->name, &args, NULL, f->self, &rettv);
Bram Moolenaard6ef5f92018-07-13 22:08:23 +02001298 if (status == OK)
1299 luaV_pushtypval(L, &rettv);
Bram Moolenaar4eefe472019-03-19 21:59:19 +01001300 clear_tv(&args);
Bram Moolenaard6ef5f92018-07-13 22:08:23 +02001301 clear_tv(&rettv);
Bram Moolenaarca06da92018-07-01 15:12:05 +02001302 }
Bram Moolenaarca06da92018-07-01 15:12:05 +02001303 if (status != OK)
1304 luaL_error(L, "cannot call funcref");
1305 return 1;
1306}
1307
1308static const luaL_Reg luaV_Funcref_mt[] = {
1309 {"__tostring", luaV_funcref_tostring},
1310 {"__gc", luaV_funcref_gc},
1311 {"__len", luaV_funcref_len},
1312 {"__call", luaV_funcref_call},
1313 {NULL, NULL}
1314};
1315
1316
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001317// ======= Buffer type =======
Bram Moolenaar1dced572012-04-05 16:54:08 +02001318
1319luaV_newtype(buf_T, buffer, luaV_Buffer, LUAVIM_BUFFER)
1320luaV_pushtype(buf_T, buffer, luaV_Buffer)
1321luaV_type_tostring(buffer, LUAVIM_BUFFER)
1322
1323 static int
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001324luaV_buffer_len(lua_State *L)
1325{
Bram Moolenaar1dced572012-04-05 16:54:08 +02001326 buf_T *b = (buf_T *) luaV_checkvalid(L, luaV_Buffer, 1);
1327 lua_pushinteger(L, b->b_ml.ml_line_count);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001328 return 1;
1329}
1330
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001331 static int
1332luaV_buffer_call(lua_State *L)
1333{
Bram Moolenaar1dced572012-04-05 16:54:08 +02001334 buf_T *b = (buf_T *) luaV_checkvalid(L, luaV_Buffer, 1);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001335 lua_settop(L, 1);
Bram Moolenaar1dced572012-04-05 16:54:08 +02001336 set_curbuf(b, DOBUF_SPLIT);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001337 return 1;
1338}
1339
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001340 static int
1341luaV_buffer_index(lua_State *L)
1342{
Bram Moolenaar1dced572012-04-05 16:54:08 +02001343 buf_T *b = (buf_T *) luaV_checkvalid(L, luaV_Buffer, 1);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001344 linenr_T n = (linenr_T) lua_tointeger(L, 2);
Bram Moolenaar1dced572012-04-05 16:54:08 +02001345 if (n > 0 && n <= b->b_ml.ml_line_count)
1346 luaV_pushline(L, b, n);
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001347 else if (lua_isstring(L, 2))
1348 {
1349 const char *s = lua_tostring(L, 2);
1350 if (strncmp(s, "name", 4) == 0)
Bram Moolenaarfe08df42018-07-07 23:07:41 +02001351 lua_pushstring(L, (b->b_sfname == NULL)
1352 ? "" : (char *) b->b_sfname);
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001353 else if (strncmp(s, "fname", 5) == 0)
Bram Moolenaarfe08df42018-07-07 23:07:41 +02001354 lua_pushstring(L, (b->b_ffname == NULL)
1355 ? "" : (char *) b->b_ffname);
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001356 else if (strncmp(s, "number", 6) == 0)
Bram Moolenaar1dced572012-04-05 16:54:08 +02001357 lua_pushinteger(L, b->b_fnum);
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001358 // methods
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001359 else if (strncmp(s, "insert", 6) == 0
1360 || strncmp(s, "next", 4) == 0
1361 || strncmp(s, "previous", 8) == 0
1362 || strncmp(s, "isvalid", 7) == 0)
1363 {
1364 lua_getmetatable(L, 1);
1365 lua_getfield(L, -1, s);
1366 }
1367 else
1368 lua_pushnil(L);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001369 }
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001370 else
1371 lua_pushnil(L);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001372 return 1;
1373}
1374
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001375 static int
1376luaV_buffer_newindex(lua_State *L)
1377{
Bram Moolenaar1dced572012-04-05 16:54:08 +02001378 buf_T *b = (buf_T *) luaV_checkvalid(L, luaV_Buffer, 1);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001379 linenr_T n = (linenr_T) luaL_checkinteger(L, 2);
1380#ifdef HAVE_SANDBOX
1381 luaV_checksandbox(L);
1382#endif
Bram Moolenaar1dced572012-04-05 16:54:08 +02001383 if (n < 1 || n > b->b_ml.ml_line_count)
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001384 luaL_error(L, "invalid line number");
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001385 if (lua_isnil(L, 3)) // delete line
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001386 {
1387 buf_T *buf = curbuf;
Bram Moolenaar1dced572012-04-05 16:54:08 +02001388 curbuf = b;
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001389 if (u_savedel(n, 1L) == FAIL)
1390 {
1391 curbuf = buf;
1392 luaL_error(L, "cannot save undo information");
1393 }
Bram Moolenaarca70c072020-05-30 20:30:46 +02001394 else if (ml_delete(n) == FAIL)
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001395 {
1396 curbuf = buf;
1397 luaL_error(L, "cannot delete line");
1398 }
Bram Moolenaarca06da92018-07-01 15:12:05 +02001399 else
1400 {
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001401 deleted_lines_mark(n, 1L);
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001402 if (b == curwin->w_buffer) // fix cursor in current window?
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001403 {
1404 if (curwin->w_cursor.lnum >= n)
1405 {
1406 if (curwin->w_cursor.lnum > n)
1407 {
1408 curwin->w_cursor.lnum -= 1;
1409 check_cursor_col();
1410 }
1411 else check_cursor();
1412 changed_cline_bef_curs();
1413 }
1414 invalidate_botline();
1415 }
1416 }
1417 curbuf = buf;
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001418 }
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001419 else if (lua_isstring(L, 3)) // update line
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001420 {
1421 buf_T *buf = curbuf;
Bram Moolenaar1dced572012-04-05 16:54:08 +02001422 curbuf = b;
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001423 if (u_savesub(n) == FAIL)
1424 {
1425 curbuf = buf;
1426 luaL_error(L, "cannot save undo information");
1427 }
1428 else if (ml_replace(n, luaV_toline(L, 3), TRUE) == FAIL)
1429 {
1430 curbuf = buf;
1431 luaL_error(L, "cannot replace line");
1432 }
1433 else changed_bytes(n, 0);
1434 curbuf = buf;
Bram Moolenaar1dced572012-04-05 16:54:08 +02001435 if (b == curwin->w_buffer)
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001436 check_cursor_col();
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001437 }
1438 else
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001439 luaL_error(L, "wrong argument to change line");
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001440 return 0;
1441}
1442
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001443 static int
1444luaV_buffer_insert(lua_State *L)
1445{
Bram Moolenaar1dced572012-04-05 16:54:08 +02001446 luaV_Buffer *lb = luaV_checkudata(L, 1, LUAVIM_BUFFER);
1447 buf_T *b = (buf_T *) luaV_checkcache(L, (void *) *lb);
1448 linenr_T last = b->b_ml.ml_line_count;
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001449 linenr_T n = (linenr_T) luaL_optinteger(L, 3, last);
1450 buf_T *buf;
1451 luaL_checktype(L, 2, LUA_TSTRING);
1452#ifdef HAVE_SANDBOX
1453 luaV_checksandbox(L);
1454#endif
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001455 // fix insertion line
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001456 if (n < 0) n = 0;
1457 if (n > last) n = last;
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001458 // insert
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001459 buf = curbuf;
Bram Moolenaar1dced572012-04-05 16:54:08 +02001460 curbuf = b;
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001461 if (u_save(n, n + 1) == FAIL)
1462 {
1463 curbuf = buf;
1464 luaL_error(L, "cannot save undo information");
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001465 }
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001466 else if (ml_append(n, luaV_toline(L, 2), 0, FALSE) == FAIL)
1467 {
1468 curbuf = buf;
1469 luaL_error(L, "cannot insert line");
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001470 }
1471 else
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001472 appended_lines_mark(n, 1L);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001473 curbuf = buf;
1474 update_screen(VALID);
1475 return 0;
1476}
1477
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001478 static int
1479luaV_buffer_next(lua_State *L)
1480{
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001481 luaV_Buffer *b = luaV_checkudata(L, 1, LUAVIM_BUFFER);
Bram Moolenaar1dced572012-04-05 16:54:08 +02001482 buf_T *buf = (buf_T *) luaV_checkcache(L, (void *) *b);
1483 luaV_pushbuffer(L, buf->b_next);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001484 return 1;
1485}
1486
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001487 static int
1488luaV_buffer_previous(lua_State *L)
1489{
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001490 luaV_Buffer *b = luaV_checkudata(L, 1, LUAVIM_BUFFER);
Bram Moolenaar1dced572012-04-05 16:54:08 +02001491 buf_T *buf = (buf_T *) luaV_checkcache(L, (void *) *b);
1492 luaV_pushbuffer(L, buf->b_prev);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001493 return 1;
1494}
1495
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001496 static int
1497luaV_buffer_isvalid(lua_State *L)
1498{
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001499 luaV_Buffer *b = luaV_checkudata(L, 1, LUAVIM_BUFFER);
Bram Moolenaar1dced572012-04-05 16:54:08 +02001500 luaV_getudata(L, *b);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001501 lua_pushboolean(L, !lua_isnil(L, -1));
1502 return 1;
1503}
1504
1505static const luaL_Reg luaV_Buffer_mt[] = {
1506 {"__tostring", luaV_buffer_tostring},
1507 {"__len", luaV_buffer_len},
1508 {"__call", luaV_buffer_call},
1509 {"__index", luaV_buffer_index},
1510 {"__newindex", luaV_buffer_newindex},
1511 {"insert", luaV_buffer_insert},
1512 {"next", luaV_buffer_next},
1513 {"previous", luaV_buffer_previous},
1514 {"isvalid", luaV_buffer_isvalid},
1515 {NULL, NULL}
1516};
1517
1518
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001519// ======= Window type =======
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001520
Bram Moolenaar1dced572012-04-05 16:54:08 +02001521luaV_newtype(win_T, window, luaV_Window, LUAVIM_WINDOW)
1522luaV_pushtype(win_T, window, luaV_Window)
1523luaV_type_tostring(window, LUAVIM_WINDOW)
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001524
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001525 static int
1526luaV_window_call(lua_State *L)
1527{
Bram Moolenaar1dced572012-04-05 16:54:08 +02001528 win_T *w = (win_T *) luaV_checkvalid(L, luaV_Window, 1);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001529 lua_settop(L, 1);
Bram Moolenaar1dced572012-04-05 16:54:08 +02001530 win_goto(w);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001531 return 1;
1532}
1533
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001534 static int
1535luaV_window_index(lua_State *L)
1536{
Bram Moolenaar1dced572012-04-05 16:54:08 +02001537 win_T *w = (win_T *) luaV_checkvalid(L, luaV_Window, 1);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001538 const char *s = luaL_checkstring(L, 2);
1539 if (strncmp(s, "buffer", 6) == 0)
Bram Moolenaar1dced572012-04-05 16:54:08 +02001540 luaV_pushbuffer(L, w->w_buffer);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001541 else if (strncmp(s, "line", 4) == 0)
Bram Moolenaar1dced572012-04-05 16:54:08 +02001542 lua_pushinteger(L, w->w_cursor.lnum);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001543 else if (strncmp(s, "col", 3) == 0)
Bram Moolenaar1dced572012-04-05 16:54:08 +02001544 lua_pushinteger(L, w->w_cursor.col + 1);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001545 else if (strncmp(s, "width", 5) == 0)
Bram Moolenaar02631462017-09-22 15:20:32 +02001546 lua_pushinteger(L, w->w_width);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001547 else if (strncmp(s, "height", 6) == 0)
Bram Moolenaar1dced572012-04-05 16:54:08 +02001548 lua_pushinteger(L, w->w_height);
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001549 // methods
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001550 else if (strncmp(s, "next", 4) == 0
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001551 || strncmp(s, "previous", 8) == 0
1552 || strncmp(s, "isvalid", 7) == 0)
1553 {
1554 lua_getmetatable(L, 1);
1555 lua_getfield(L, -1, s);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001556 }
1557 else
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001558 lua_pushnil(L);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001559 return 1;
1560}
1561
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001562 static int
Bram Moolenaar4eefe472019-03-19 21:59:19 +01001563luaV_window_newindex(lua_State *L)
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001564{
Bram Moolenaar1dced572012-04-05 16:54:08 +02001565 win_T *w = (win_T *) luaV_checkvalid(L, luaV_Window, 1);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001566 const char *s = luaL_checkstring(L, 2);
1567 int v = luaL_checkinteger(L, 3);
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001568 if (strncmp(s, "line", 4) == 0)
1569 {
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001570#ifdef HAVE_SANDBOX
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001571 luaV_checksandbox(L);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001572#endif
Bram Moolenaar1dced572012-04-05 16:54:08 +02001573 if (v < 1 || v > w->w_buffer->b_ml.ml_line_count)
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001574 luaL_error(L, "line out of range");
Bram Moolenaar1dced572012-04-05 16:54:08 +02001575 w->w_cursor.lnum = v;
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001576 update_screen(VALID);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001577 }
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001578 else if (strncmp(s, "col", 3) == 0)
1579 {
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001580#ifdef HAVE_SANDBOX
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001581 luaV_checksandbox(L);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001582#endif
Bram Moolenaar1dced572012-04-05 16:54:08 +02001583 w->w_cursor.col = v - 1;
Bram Moolenaar53901442018-07-25 22:02:36 +02001584 w->w_set_curswant = TRUE;
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001585 update_screen(VALID);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001586 }
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001587 else if (strncmp(s, "width", 5) == 0)
1588 {
1589 win_T *win = curwin;
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001590#ifdef FEAT_GUI
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001591 need_mouse_correct = TRUE;
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001592#endif
Bram Moolenaar1dced572012-04-05 16:54:08 +02001593 curwin = w;
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001594 win_setwidth(v);
1595 curwin = win;
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001596 }
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001597 else if (strncmp(s, "height", 6) == 0)
1598 {
1599 win_T *win = curwin;
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001600#ifdef FEAT_GUI
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001601 need_mouse_correct = TRUE;
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001602#endif
Bram Moolenaar1dced572012-04-05 16:54:08 +02001603 curwin = w;
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001604 win_setheight(v);
1605 curwin = win;
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001606 }
1607 else
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001608 luaL_error(L, "invalid window property: `%s'", s);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001609 return 0;
1610}
1611
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001612 static int
1613luaV_window_next(lua_State *L)
1614{
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001615 luaV_Window *w = luaV_checkudata(L, 1, LUAVIM_WINDOW);
Bram Moolenaar1dced572012-04-05 16:54:08 +02001616 win_T *win = (win_T *) luaV_checkcache(L, (void *) *w);
1617 luaV_pushwindow(L, win->w_next);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001618 return 1;
1619}
1620
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001621 static int
1622luaV_window_previous(lua_State *L)
1623{
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001624 luaV_Window *w = luaV_checkudata(L, 1, LUAVIM_WINDOW);
Bram Moolenaar1dced572012-04-05 16:54:08 +02001625 win_T *win = (win_T *) luaV_checkcache(L, (void *) *w);
1626 luaV_pushwindow(L, win->w_prev);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001627 return 1;
1628}
1629
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001630 static int
1631luaV_window_isvalid(lua_State *L)
1632{
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001633 luaV_Window *w = luaV_checkudata(L, 1, LUAVIM_WINDOW);
Bram Moolenaar1dced572012-04-05 16:54:08 +02001634 luaV_getudata(L, *w);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001635 lua_pushboolean(L, !lua_isnil(L, -1));
1636 return 1;
1637}
1638
1639static const luaL_Reg luaV_Window_mt[] = {
1640 {"__tostring", luaV_window_tostring},
1641 {"__call", luaV_window_call},
1642 {"__index", luaV_window_index},
1643 {"__newindex", luaV_window_newindex},
1644 {"next", luaV_window_next},
1645 {"previous", luaV_window_previous},
1646 {"isvalid", luaV_window_isvalid},
1647 {NULL, NULL}
1648};
1649
1650
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001651// ======= Vim module =======
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001652
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001653 static int
1654luaV_print(lua_State *L)
1655{
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001656 int i, n = lua_gettop(L); // nargs
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001657 const char *s;
1658 size_t l;
1659 luaL_Buffer b;
1660 luaL_buffinit(L, &b);
1661 lua_getglobal(L, "tostring");
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001662 for (i = 1; i <= n; i++)
1663 {
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001664 lua_pushvalue(L, -1); // tostring
1665 lua_pushvalue(L, i); // arg
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001666 lua_call(L, 1, 1);
1667 s = lua_tolstring(L, -1, &l);
1668 if (s == NULL)
1669 return luaL_error(L, "cannot convert to string");
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001670 if (i > 1) luaL_addchar(&b, ' '); // use space instead of tab
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001671 luaV_addlstring(&b, s, l, 0);
1672 lua_pop(L, 1);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001673 }
1674 luaL_pushresult(&b);
Bram Moolenaarb98678a2019-10-19 15:18:44 +02001675 if (!got_int)
1676 luaV_msg(L);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001677 return 0;
1678}
1679
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001680 static int
Bram Moolenaar38e2b062011-09-21 17:15:39 +02001681luaV_debug(lua_State *L)
1682{
1683 lua_settop(L, 0);
1684 lua_getglobal(L, "vim");
1685 lua_getfield(L, -1, "eval");
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001686 lua_remove(L, -2); // vim.eval at position 1
Bram Moolenaar38e2b062011-09-21 17:15:39 +02001687 for (;;)
1688 {
1689 const char *input;
1690 size_t l;
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001691 lua_pushvalue(L, 1); // vim.eval
Bram Moolenaar38e2b062011-09-21 17:15:39 +02001692 lua_pushliteral(L, "input('lua_debug> ')");
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001693 lua_call(L, 1, 1); // return string
Bram Moolenaar38e2b062011-09-21 17:15:39 +02001694 input = lua_tolstring(L, -1, &l);
1695 if (l == 0 || strcmp(input, "cont") == 0)
1696 return 0;
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001697 msg_putchar('\n'); // avoid outputting on input line
Bram Moolenaar38e2b062011-09-21 17:15:39 +02001698 if (luaL_loadbuffer(L, input, l, "=(debug command)")
1699 || lua_pcall(L, 0, 0, 0))
1700 luaV_emsg(L);
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001701 lua_settop(L, 1); // remove eventual returns, but keep vim.eval
Bram Moolenaar38e2b062011-09-21 17:15:39 +02001702 }
1703}
1704
1705 static int
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001706luaV_command(lua_State *L)
1707{
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001708 do_cmdline_cmd((char_u *) luaL_checkstring(L, 1));
1709 update_screen(VALID);
1710 return 0;
1711}
1712
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001713 static int
1714luaV_eval(lua_State *L)
1715{
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001716 typval_T *tv = eval_expr((char_u *) luaL_checkstring(L, 1), NULL);
1717 if (tv == NULL) luaL_error(L, "invalid expression");
1718 luaV_pushtypval(L, tv);
Bram Moolenaarb3766472013-04-15 13:49:21 +02001719 free_tv(tv);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001720 return 1;
1721}
1722
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001723 static int
Bram Moolenaar0d2e4fc2010-07-18 12:35:47 +02001724luaV_beep(lua_State *L UNUSED)
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001725{
Bram Moolenaar165bc692015-07-21 17:53:25 +02001726 vim_beep(BO_LANG);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001727 return 0;
1728}
1729
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001730 static int
1731luaV_line(lua_State *L)
1732{
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001733 luaV_pushline(L, curbuf, curwin->w_cursor.lnum);
1734 return 1;
1735}
1736
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001737 static int
Bram Moolenaar1dced572012-04-05 16:54:08 +02001738luaV_list(lua_State *L)
1739{
Bram Moolenaarca06da92018-07-01 15:12:05 +02001740 list_T *l;
1741 int initarg = !lua_isnoneornil(L, 1);
1742
1743 if (initarg && lua_type(L, 1) != LUA_TTABLE)
1744 luaL_error(L, "table expected, got %s", luaL_typename(L, 1));
1745 l = list_alloc();
Bram Moolenaar1dced572012-04-05 16:54:08 +02001746 if (l == NULL)
1747 lua_pushnil(L);
1748 else
Bram Moolenaarca06da92018-07-01 15:12:05 +02001749 {
Bram Moolenaar1dced572012-04-05 16:54:08 +02001750 luaV_newlist(L, l);
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001751 if (initarg) // traverse table to init list
Bram Moolenaar17413672018-07-14 20:49:42 +02001752 {
Bram Moolenaarca06da92018-07-01 15:12:05 +02001753 int notnil, i = 0;
1754 typval_T v;
Bram Moolenaar17413672018-07-14 20:49:42 +02001755 do
1756 {
Bram Moolenaarca06da92018-07-01 15:12:05 +02001757 lua_rawgeti(L, 1, ++i);
1758 notnil = !lua_isnil(L, -1);
Bram Moolenaar17413672018-07-14 20:49:42 +02001759 if (notnil)
1760 {
Bram Moolenaarca06da92018-07-01 15:12:05 +02001761 luaV_checktypval(L, -1, &v, "vim.list");
1762 list_append_tv(l, &v);
Bram Moolenaar713bf9e92019-03-16 16:38:41 +01001763 clear_tv(&v);
Bram Moolenaarca06da92018-07-01 15:12:05 +02001764 }
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001765 lua_pop(L, 1); // value
Bram Moolenaarca06da92018-07-01 15:12:05 +02001766 } while (notnil);
1767 }
1768 }
Bram Moolenaar1dced572012-04-05 16:54:08 +02001769 return 1;
1770}
1771
1772 static int
1773luaV_dict(lua_State *L)
1774{
Bram Moolenaarca06da92018-07-01 15:12:05 +02001775 dict_T *d;
1776 int initarg = !lua_isnoneornil(L, 1);
1777
1778 if (initarg && lua_type(L, 1) != LUA_TTABLE)
1779 luaL_error(L, "table expected, got %s", luaL_typename(L, 1));
1780 d = dict_alloc();
Bram Moolenaar1dced572012-04-05 16:54:08 +02001781 if (d == NULL)
1782 lua_pushnil(L);
1783 else
Bram Moolenaarca06da92018-07-01 15:12:05 +02001784 {
Bram Moolenaar1dced572012-04-05 16:54:08 +02001785 luaV_newdict(L, d);
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001786 if (initarg) // traverse table to init dict
Bram Moolenaarca06da92018-07-01 15:12:05 +02001787 {
1788 lua_pushnil(L);
1789 while (lua_next(L, 1))
1790 {
1791 char_u *key;
1792 dictitem_T *di;
1793 typval_T v;
Bram Moolenaard6ef5f92018-07-13 22:08:23 +02001794
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001795 lua_pushvalue(L, -2); // dup key in case it's a number
Bram Moolenaarca06da92018-07-01 15:12:05 +02001796 key = (char_u *) lua_tostring(L, -1);
Bram Moolenaard6ef5f92018-07-13 22:08:23 +02001797 if (key == NULL)
1798 {
1799 lua_pushnil(L);
1800 return 1;
1801 }
1802 if (*key == NUL)
Bram Moolenaarca06da92018-07-01 15:12:05 +02001803 luaL_error(L, "table has empty key");
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001804 luaV_checktypval(L, -2, &v, "vim.dict"); // value
Bram Moolenaarca06da92018-07-01 15:12:05 +02001805 di = dictitem_alloc(key);
Bram Moolenaard6ef5f92018-07-13 22:08:23 +02001806 if (di == NULL || dict_add(d, di) == FAIL)
1807 {
Bram Moolenaarca06da92018-07-01 15:12:05 +02001808 vim_free(di);
1809 lua_pushnil(L);
1810 return 1;
1811 }
1812 copy_tv(&v, &di->di_tv);
1813 clear_tv(&v);
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001814 lua_pop(L, 2); // key copy and value
Bram Moolenaarca06da92018-07-01 15:12:05 +02001815 }
1816 }
1817 }
1818 return 1;
1819}
1820
1821 static int
Bram Moolenaarb7828692019-03-23 13:57:02 +01001822luaV_blob(lua_State *L)
1823{
1824 blob_T *b;
1825 int initarg = !lua_isnoneornil(L, 1);
1826
1827 if (initarg && !lua_isstring(L, 1))
1828 luaL_error(L, "string expected, got %s", luaL_typename(L, 1));
1829 b = blob_alloc();
1830 if (b == NULL)
1831 lua_pushnil(L);
1832 else
1833 {
1834 luaV_newblob(L, b);
1835 if (initarg)
1836 {
1837 size_t i, l = 0;
1838 const char *s = lua_tolstring(L, 1, &l);
1839
Bram Moolenaar5f1d3ae2020-02-11 22:37:35 +01001840 if (ga_grow(&b->bv_ga, (int)l) == OK)
Bram Moolenaar6fb5c972019-03-26 21:44:20 +01001841 for (i = 0; i < l; ++i)
1842 ga_append(&b->bv_ga, s[i]);
Bram Moolenaarb7828692019-03-23 13:57:02 +01001843 }
1844 }
1845 return 1;
1846}
1847
1848 static int
Bram Moolenaarca06da92018-07-01 15:12:05 +02001849luaV_funcref(lua_State *L)
1850{
1851 const char *name = luaL_checkstring(L, 1);
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001852 // note: not checking if function exists (needs function_exists)
Bram Moolenaarca06da92018-07-01 15:12:05 +02001853 if (name == NULL || *name == NUL || VIM_ISDIGIT(*name))
1854 luaL_error(L, "invalid function name: %s", name);
1855 luaV_newfuncref(L, (char_u *) name);
Bram Moolenaar1dced572012-04-05 16:54:08 +02001856 return 1;
1857}
1858
1859 static int
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001860luaV_buffer(lua_State *L)
1861{
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001862 buf_T *buf;
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001863 if (lua_isstring(L, 1)) // get by number or name?
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001864 {
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001865 if (lua_isnumber(L, 1)) // by number?
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001866 {
1867 int n = lua_tointeger(L, 1);
Bram Moolenaar29323592016-07-24 22:04:11 +02001868 FOR_ALL_BUFFERS(buf)
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001869 if (buf->b_fnum == n) break;
1870 }
Bram Moolenaarca06da92018-07-01 15:12:05 +02001871 else // by name
1872 {
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001873 size_t l;
1874 const char *s = lua_tolstring(L, 1, &l);
Bram Moolenaar29323592016-07-24 22:04:11 +02001875 FOR_ALL_BUFFERS(buf)
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001876 {
1877 if (buf->b_ffname == NULL || buf->b_sfname == NULL)
1878 {
1879 if (l == 0) break;
1880 }
Bram Moolenaar0d2e4fc2010-07-18 12:35:47 +02001881 else if (strncmp(s, (char *)buf->b_ffname, l) == 0
1882 || strncmp(s, (char *)buf->b_sfname, l) == 0)
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001883 break;
1884 }
1885 }
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001886 }
Bram Moolenaar1dced572012-04-05 16:54:08 +02001887 else
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001888 buf = (lua_toboolean(L, 1)) ? firstbuf : curbuf; // first buffer?
Bram Moolenaar1dced572012-04-05 16:54:08 +02001889 luaV_pushbuffer(L, buf);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001890 return 1;
1891}
1892
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001893 static int
1894luaV_window(lua_State *L)
1895{
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001896 win_T *win;
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001897 if (lua_isnumber(L, 1)) // get by number?
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001898 {
1899 int n = lua_tointeger(L, 1);
1900 for (win = firstwin; win != NULL; win = win->w_next, n--)
1901 if (n == 1) break;
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001902 }
Bram Moolenaar1dced572012-04-05 16:54:08 +02001903 else
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001904 win = (lua_toboolean(L, 1)) ? firstwin : curwin; // first window?
Bram Moolenaar1dced572012-04-05 16:54:08 +02001905 luaV_pushwindow(L, win);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001906 return 1;
1907}
1908
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001909 static int
1910luaV_open(lua_State *L)
1911{
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001912 char_u *s = NULL;
1913#ifdef HAVE_SANDBOX
1914 luaV_checksandbox(L);
1915#endif
1916 if (lua_isstring(L, 1)) s = (char_u *) lua_tostring(L, 1);
Bram Moolenaarfa263a52011-12-08 16:00:16 +01001917 luaV_pushbuffer(L, buflist_new(s, NULL, 1L, BLN_LISTED));
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001918 return 1;
1919}
1920
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001921 static int
Bram Moolenaar1dced572012-04-05 16:54:08 +02001922luaV_type(lua_State *L)
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001923{
Bram Moolenaar1dced572012-04-05 16:54:08 +02001924 luaL_checkany(L, 1);
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001925 if (lua_type(L, 1) == LUA_TUSERDATA) // check vim udata?
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001926 {
Bram Moolenaar1dced572012-04-05 16:54:08 +02001927 lua_settop(L, 1);
1928 if (lua_getmetatable(L, 1))
1929 {
1930 luaV_getfield(L, LUAVIM_LIST);
1931 if (lua_rawequal(L, -1, 2))
1932 {
1933 lua_pushstring(L, "list");
1934 return 1;
1935 }
1936 luaV_getfield(L, LUAVIM_DICT);
1937 if (lua_rawequal(L, -1, 2))
1938 {
1939 lua_pushstring(L, "dict");
1940 return 1;
1941 }
Bram Moolenaarb7828692019-03-23 13:57:02 +01001942 luaV_getfield(L, LUAVIM_BLOB);
1943 if (lua_rawequal(L, -1, 2))
1944 {
1945 lua_pushstring(L, "blob");
1946 return 1;
1947 }
Bram Moolenaarca06da92018-07-01 15:12:05 +02001948 luaV_getfield(L, LUAVIM_FUNCREF);
1949 if (lua_rawequal(L, -1, 2))
1950 {
1951 lua_pushstring(L, "funcref");
1952 return 1;
1953 }
Bram Moolenaar1dced572012-04-05 16:54:08 +02001954 luaV_getfield(L, LUAVIM_BUFFER);
1955 if (lua_rawequal(L, -1, 2))
1956 {
1957 lua_pushstring(L, "buffer");
1958 return 1;
1959 }
1960 luaV_getfield(L, LUAVIM_WINDOW);
1961 if (lua_rawequal(L, -1, 2))
1962 {
1963 lua_pushstring(L, "window");
1964 return 1;
1965 }
1966 }
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001967 }
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001968 lua_pushstring(L, luaL_typename(L, 1)); // fallback
Bram Moolenaar1dced572012-04-05 16:54:08 +02001969 return 1;
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001970}
1971
Bram Moolenaareb04f082020-05-17 14:32:35 +02001972 static int
1973luaV_call(lua_State *L)
1974{
1975 int argc = lua_gettop(L) - 1;
1976 size_t funcname_len;
1977 char_u *funcname;
1978 char *error = NULL;
1979 typval_T rettv;
1980 typval_T argv[MAX_FUNC_ARGS + 1];
1981 int i = 0;
1982
1983 if (argc > MAX_FUNC_ARGS)
1984 return luaL_error(L, "Function called with too many arguments");
1985
1986 funcname = (char_u *)luaL_checklstring(L, 1, &funcname_len);
1987
1988 for (; i < argc; i++)
1989 {
1990 if (luaV_totypval(L, i + 2, &argv[i]) == FAIL)
1991 {
1992 error = "lua: cannot convert value";
1993 goto free_vim_args;
1994 }
1995 }
1996
1997 argv[argc].v_type = VAR_UNKNOWN;
1998
1999 if (call_vim_function(funcname, argc, argv, &rettv) == FAIL)
2000 {
2001 error = "lua: call_vim_function failed";
2002 goto free_vim_args;
2003 }
2004
2005 luaV_pushtypval(L, &rettv);
2006 clear_tv(&rettv);
2007
2008free_vim_args:
2009 while (i > 0)
2010 clear_tv(&argv[--i]);
2011
2012 if (error == NULL)
2013 return 1;
2014 else
2015 return luaL_error(L, error);
2016}
2017
Bram Moolenaar0ba04292010-07-14 23:23:17 +02002018static const luaL_Reg luaV_module[] = {
2019 {"command", luaV_command},
2020 {"eval", luaV_eval},
2021 {"beep", luaV_beep},
2022 {"line", luaV_line},
Bram Moolenaar1dced572012-04-05 16:54:08 +02002023 {"list", luaV_list},
2024 {"dict", luaV_dict},
Bram Moolenaarb7828692019-03-23 13:57:02 +01002025 {"blob", luaV_blob},
Bram Moolenaarca06da92018-07-01 15:12:05 +02002026 {"funcref", luaV_funcref},
Bram Moolenaar0ba04292010-07-14 23:23:17 +02002027 {"buffer", luaV_buffer},
2028 {"window", luaV_window},
2029 {"open", luaV_open},
Bram Moolenaar1dced572012-04-05 16:54:08 +02002030 {"type", luaV_type},
Bram Moolenaareb04f082020-05-17 14:32:35 +02002031 {"call", luaV_call},
Bram Moolenaar0ba04292010-07-14 23:23:17 +02002032 {NULL, NULL}
2033};
2034
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002035/*
2036 * for freeing list, dict, buffer and window objects; lightuserdata as arg
2037 */
Bram Moolenaar1dced572012-04-05 16:54:08 +02002038 static int
2039luaV_free(lua_State *L)
2040{
2041 lua_pushnil(L);
2042 luaV_setudata(L, lua_touserdata(L, 1));
2043 return 0;
2044}
2045
2046 static int
Bram Moolenaar4eefe472019-03-19 21:59:19 +01002047luaV_luaeval(lua_State *L)
Bram Moolenaar1dced572012-04-05 16:54:08 +02002048{
2049 luaL_Buffer b;
2050 size_t l;
2051 const char *str = lua_tolstring(L, 1, &l);
2052 typval_T *arg = (typval_T *) lua_touserdata(L, 2);
2053 typval_T *rettv = (typval_T *) lua_touserdata(L, 3);
2054 luaL_buffinit(L, &b);
2055 luaL_addlstring(&b, LUAVIM_EVALHEADER, sizeof(LUAVIM_EVALHEADER) - 1);
2056 luaL_addlstring(&b, str, l);
2057 luaL_pushresult(&b);
2058 str = lua_tolstring(L, -1, &l);
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002059 if (luaL_loadbuffer(L, str, l, LUAVIM_EVALNAME)) // compile error?
Bram Moolenaar1dced572012-04-05 16:54:08 +02002060 {
2061 luaV_emsg(L);
2062 return 0;
2063 }
2064 luaV_pushtypval(L, arg);
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002065 if (lua_pcall(L, 1, 1, 0)) // running error?
Bram Moolenaar1dced572012-04-05 16:54:08 +02002066 {
2067 luaV_emsg(L);
2068 return 0;
2069 }
Bram Moolenaarca06da92018-07-01 15:12:05 +02002070 if (luaV_totypval(L, -1, rettv) == FAIL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002071 emsg("luaeval: cannot convert value");
Bram Moolenaarf554a322015-02-04 23:08:01 +01002072 return 0;
Bram Moolenaar1dced572012-04-05 16:54:08 +02002073}
2074
2075 static int
Bram Moolenaar4eefe472019-03-19 21:59:19 +01002076luaV_setref(lua_State *L)
Bram Moolenaar1dced572012-04-05 16:54:08 +02002077{
Bram Moolenaar4eefe472019-03-19 21:59:19 +01002078 int copyID = lua_tointeger(L, 1);
2079 int abort = FALSE;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01002080
Bram Moolenaar1dced572012-04-05 16:54:08 +02002081 luaV_getfield(L, LUAVIM_LIST);
2082 luaV_getfield(L, LUAVIM_DICT);
Bram Moolenaar4eefe472019-03-19 21:59:19 +01002083 luaV_getfield(L, LUAVIM_FUNCREF);
Bram Moolenaar1dced572012-04-05 16:54:08 +02002084 lua_pushnil(L);
Bram Moolenaar4eefe472019-03-19 21:59:19 +01002085 // traverse cache table
Bram Moolenaarb84634d2015-02-04 22:02:37 +01002086 while (!abort && lua_next(L, lua_upvalueindex(1)) != 0)
Bram Moolenaar1dced572012-04-05 16:54:08 +02002087 {
2088 lua_getmetatable(L, -1);
Bram Moolenaar4eefe472019-03-19 21:59:19 +01002089 if (lua_rawequal(L, -1, 2)) // list?
Bram Moolenaar1dced572012-04-05 16:54:08 +02002090 {
Bram Moolenaar4eefe472019-03-19 21:59:19 +01002091 list_T *l = (list_T *)lua_touserdata(L, 5); // key
2092
Bram Moolenaar7be3ab22019-06-23 01:46:15 +02002093 abort = set_ref_in_list(l, copyID);
Bram Moolenaar1dced572012-04-05 16:54:08 +02002094 }
Bram Moolenaar4eefe472019-03-19 21:59:19 +01002095 else if (lua_rawequal(L, -1, 3)) // dict?
Bram Moolenaar1dced572012-04-05 16:54:08 +02002096 {
Bram Moolenaar4eefe472019-03-19 21:59:19 +01002097 dict_T *d = (dict_T *)lua_touserdata(L, 5); // key
2098
Bram Moolenaar7be3ab22019-06-23 01:46:15 +02002099 abort = set_ref_in_dict(d, copyID);
Bram Moolenaar1dced572012-04-05 16:54:08 +02002100 }
Bram Moolenaar4eefe472019-03-19 21:59:19 +01002101 else if (lua_rawequal(L, -1, 4)) // funcref?
2102 {
2103 luaV_Funcref *f = (luaV_Funcref *)lua_touserdata(L, 5); // key
2104
Bram Moolenaar7be3ab22019-06-23 01:46:15 +02002105 abort = set_ref_in_dict(f->self, copyID);
Bram Moolenaar4eefe472019-03-19 21:59:19 +01002106 }
2107 lua_pop(L, 2); // metatable and value
Bram Moolenaar1dced572012-04-05 16:54:08 +02002108 }
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01002109 lua_pushinteger(L, abort);
Bram Moolenaarf554a322015-02-04 23:08:01 +01002110 return 1;
Bram Moolenaar1dced572012-04-05 16:54:08 +02002111}
2112
Bram Moolenaareb04f082020-05-17 14:32:35 +02002113#define LUA_VIM_FN_CODE \
Bram Moolenaar788fbb42020-05-31 14:08:12 +02002114 "vim.fn = setmetatable({}, {\n"\
2115 " __index = function (t, key)\n"\
2116 " local function _fn(...)\n"\
2117 " return vim.call(key, ...)\n"\
2118 " end\n"\
2119 " t[key] = _fn\n"\
2120 " return _fn\n"\
2121 " end\n"\
2122 " })"
2123
2124#define LUA_VIM_UPDATE_PACKAGE_PATHS \
2125 "local last_vim_paths = {}\n"\
2126 "vim._update_package_paths = function ()\n"\
2127 " local cur_vim_paths = {}\n"\
2128 " local function split(s, delimiter)\n"\
2129 " result = {}\n"\
2130 " for match in (s..delimiter):gmatch(\"(.-)\"..delimiter) do\n"\
2131 " table.insert(result, match)\n"\
2132 " end\n"\
2133 " return result\n"\
2134 " end\n"\
2135 " local rtps = split(vim.eval('&runtimepath'), ',')\n"\
2136 " local sep = package.config:sub(1, 1)\n"\
2137 " for _, key in ipairs({'path', 'cpath'}) do\n"\
2138 " local orig_str = package[key] .. ';'\n"\
2139 " local pathtrails_ordered = {}\n"\
2140 " -- Note: ignores trailing item without trailing `;`. Not using something\n"\
2141 " -- simpler in order to preserve empty items (stand for default path).\n"\
2142 " local orig = {}\n"\
2143 " for s in orig_str:gmatch('[^;]*;') do\n"\
2144 " s = s:sub(1, -2) -- Strip trailing semicolon\n"\
2145 " orig[#orig + 1] = s\n"\
2146 " end\n"\
2147 " if key == 'path' then\n"\
2148 " -- /?.lua and /?/init.lua\n"\
2149 " pathtrails_ordered = {sep .. '?.lua', sep .. '?' .. sep .. 'init.lua'}\n"\
2150 " else\n"\
2151 " local pathtrails = {}\n"\
2152 " for _, s in ipairs(orig) do\n"\
2153 " -- Find out path patterns. pathtrail should contain something like\n"\
2154 " -- /?.so, \?.dll. This allows not to bother determining what correct\n"\
2155 " -- suffixes are.\n"\
2156 " local pathtrail = s:match('[/\\\\][^/\\\\]*%?.*$')\n"\
2157 " if pathtrail and not pathtrails[pathtrail] then\n"\
2158 " pathtrails[pathtrail] = true\n"\
2159 " pathtrails_ordered[#pathtrails_ordered + 1] = pathtrail\n"\
2160 " end\n"\
2161 " end\n"\
2162 " end\n"\
2163 " local new = {}\n"\
2164 " for _, rtp in ipairs(rtps) do\n"\
2165 " if not rtp:match(';') then\n"\
2166 " for _, pathtrail in pairs(pathtrails_ordered) do\n"\
2167 " local new_path = rtp .. sep .. 'lua' .. pathtrail\n"\
2168 " -- Always keep paths from &runtimepath at the start:\n"\
2169 " -- append them here disregarding orig possibly containing one of them.\n"\
2170 " new[#new + 1] = new_path\n"\
2171 " cur_vim_paths[new_path] = true\n"\
2172 " end\n"\
2173 " end\n"\
2174 " end\n"\
2175 " for _, orig_path in ipairs(orig) do\n"\
2176 " -- Handle removing obsolete paths originating from &runtimepath: such\n"\
2177 " -- paths either belong to cur_nvim_paths and were already added above or\n"\
2178 " -- to last_nvim_paths and should not be added at all if corresponding\n"\
2179 " -- entry was removed from &runtimepath list.\n"\
2180 " if not (cur_vim_paths[orig_path] or last_vim_paths[orig_path]) then\n"\
2181 " new[#new + 1] = orig_path\n"\
2182 " end\n"\
2183 " end\n"\
2184 " package[key] = table.concat(new, ';')\n"\
2185 " end\n"\
2186 " last_vim_paths = cur_vim_paths\n"\
2187 "end"
Bram Moolenaareb04f082020-05-17 14:32:35 +02002188
Bram Moolenaar55d5c032010-07-17 23:52:29 +02002189 static int
2190luaopen_vim(lua_State *L)
2191{
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002192 // set cache table
Bram Moolenaar0ba04292010-07-14 23:23:17 +02002193 lua_newtable(L);
2194 lua_newtable(L);
Bram Moolenaar1dced572012-04-05 16:54:08 +02002195 lua_pushstring(L, "v");
Bram Moolenaar0ba04292010-07-14 23:23:17 +02002196 lua_setfield(L, -2, "__mode");
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002197 lua_setmetatable(L, -2); // cache is weak-valued
2198 // print
Bram Moolenaar0ba04292010-07-14 23:23:17 +02002199 lua_pushcfunction(L, luaV_print);
2200 lua_setglobal(L, "print");
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002201 // debug.debug
Bram Moolenaar38e2b062011-09-21 17:15:39 +02002202 lua_getglobal(L, "debug");
2203 lua_pushcfunction(L, luaV_debug);
2204 lua_setfield(L, -2, "debug");
2205 lua_pop(L, 1);
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002206 // free
Bram Moolenaar0ba04292010-07-14 23:23:17 +02002207 lua_pushlightuserdata(L, (void *) LUAVIM_FREE);
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002208 lua_pushvalue(L, 1); // cache table
Bram Moolenaar1dced572012-04-05 16:54:08 +02002209 lua_pushcclosure(L, luaV_free, 1);
2210 lua_rawset(L, LUA_REGISTRYINDEX);
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002211 // luaeval
Bram Moolenaar1dced572012-04-05 16:54:08 +02002212 lua_pushlightuserdata(L, (void *) LUAVIM_LUAEVAL);
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002213 lua_pushvalue(L, 1); // cache table
Bram Moolenaar1dced572012-04-05 16:54:08 +02002214 lua_pushcclosure(L, luaV_luaeval, 1);
2215 lua_rawset(L, LUA_REGISTRYINDEX);
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002216 // setref
Bram Moolenaar1dced572012-04-05 16:54:08 +02002217 lua_pushlightuserdata(L, (void *) LUAVIM_SETREF);
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002218 lua_pushvalue(L, 1); // cache table
Bram Moolenaar1dced572012-04-05 16:54:08 +02002219 lua_pushcclosure(L, luaV_setref, 1);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02002220 lua_rawset(L, LUA_REGISTRYINDEX);
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002221 // register
Bram Moolenaar1dced572012-04-05 16:54:08 +02002222 luaV_newmetatable(L, LUAVIM_LIST);
2223 lua_pushvalue(L, 1);
2224 luaV_openlib(L, luaV_List_mt, 1);
2225 luaV_newmetatable(L, LUAVIM_DICT);
2226 lua_pushvalue(L, 1);
2227 luaV_openlib(L, luaV_Dict_mt, 1);
Bram Moolenaarb7828692019-03-23 13:57:02 +01002228 luaV_newmetatable(L, LUAVIM_BLOB);
2229 lua_pushvalue(L, 1);
2230 luaV_openlib(L, luaV_Blob_mt, 1);
Bram Moolenaarca06da92018-07-01 15:12:05 +02002231 luaV_newmetatable(L, LUAVIM_FUNCREF);
2232 lua_pushvalue(L, 1);
2233 luaV_openlib(L, luaV_Funcref_mt, 1);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02002234 luaV_newmetatable(L, LUAVIM_BUFFER);
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002235 lua_pushvalue(L, 1); // cache table
Bram Moolenaar1dced572012-04-05 16:54:08 +02002236 luaV_openlib(L, luaV_Buffer_mt, 1);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02002237 luaV_newmetatable(L, LUAVIM_WINDOW);
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002238 lua_pushvalue(L, 1); // cache table
Bram Moolenaar1dced572012-04-05 16:54:08 +02002239 luaV_openlib(L, luaV_Window_mt, 1);
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002240 lua_newtable(L); // vim table
2241 lua_pushvalue(L, 1); // cache table
Bram Moolenaar1dced572012-04-05 16:54:08 +02002242 luaV_openlib(L, luaV_module, 1);
2243 lua_setglobal(L, LUAVIM_NAME);
Bram Moolenaareb04f082020-05-17 14:32:35 +02002244 // custom code
Bram Moolenaar9309eb22020-05-17 16:53:56 +02002245 (void)luaL_dostring(L, LUA_VIM_FN_CODE);
Bram Moolenaar788fbb42020-05-31 14:08:12 +02002246 (void)luaL_dostring(L, LUA_VIM_UPDATE_PACKAGE_PATHS);
2247
2248 lua_getglobal(L, "vim");
2249 lua_getfield(L, -1, "_update_package_paths");
2250
2251 if (lua_pcall(L, 0, 0, 0))
2252 luaV_emsg(L);
2253
Bram Moolenaar0ba04292010-07-14 23:23:17 +02002254 return 0;
2255}
2256
Bram Moolenaar55d5c032010-07-17 23:52:29 +02002257 static lua_State *
2258luaV_newstate(void)
2259{
Bram Moolenaar0ba04292010-07-14 23:23:17 +02002260 lua_State *L = luaL_newstate();
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002261 luaL_openlibs(L); // core libs
2262 lua_pushcfunction(L, luaopen_vim); // vim
Bram Moolenaar0ba04292010-07-14 23:23:17 +02002263 lua_call(L, 0, 0);
2264 return L;
2265}
2266
Bram Moolenaar55d5c032010-07-17 23:52:29 +02002267 static void
2268luaV_setrange(lua_State *L, int line1, int line2)
2269{
Bram Moolenaar0ba04292010-07-14 23:23:17 +02002270 lua_getglobal(L, LUAVIM_NAME);
2271 lua_pushinteger(L, line1);
2272 lua_setfield(L, -2, "firstline");
2273 lua_pushinteger(L, line2);
2274 lua_setfield(L, -2, "lastline");
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002275 lua_pop(L, 1); // vim table
Bram Moolenaar0ba04292010-07-14 23:23:17 +02002276}
2277
2278
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002279// ======= Interface =======
Bram Moolenaar0ba04292010-07-14 23:23:17 +02002280
2281static lua_State *L = NULL;
2282
Bram Moolenaar55d5c032010-07-17 23:52:29 +02002283 static int
Bram Moolenaar1dced572012-04-05 16:54:08 +02002284lua_isopen(void)
Bram Moolenaar2bd6a1b2010-08-12 22:14:01 +02002285{
2286 return L != NULL;
2287}
2288
2289 static int
Bram Moolenaar55d5c032010-07-17 23:52:29 +02002290lua_init(void)
2291{
Bram Moolenaar1dced572012-04-05 16:54:08 +02002292 if (!lua_isopen())
Bram Moolenaar55d5c032010-07-17 23:52:29 +02002293 {
Bram Moolenaar0ba04292010-07-14 23:23:17 +02002294#ifdef DYNAMIC_LUA
Bram Moolenaar55d5c032010-07-17 23:52:29 +02002295 if (!lua_enabled(TRUE))
2296 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002297 emsg(_("Lua library cannot be loaded."));
Bram Moolenaar55d5c032010-07-17 23:52:29 +02002298 return FAIL;
2299 }
Bram Moolenaar0ba04292010-07-14 23:23:17 +02002300#endif
Bram Moolenaar55d5c032010-07-17 23:52:29 +02002301 L = luaV_newstate();
Bram Moolenaar0ba04292010-07-14 23:23:17 +02002302 }
2303 return OK;
2304}
2305
Bram Moolenaar55d5c032010-07-17 23:52:29 +02002306 void
2307lua_end(void)
2308{
Bram Moolenaar1dced572012-04-05 16:54:08 +02002309 if (lua_isopen())
Bram Moolenaar55d5c032010-07-17 23:52:29 +02002310 {
2311 lua_close(L);
2312 L = NULL;
Bram Moolenaar0ba04292010-07-14 23:23:17 +02002313 }
2314}
2315
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002316/*
2317 * ex commands
2318 */
Bram Moolenaar55d5c032010-07-17 23:52:29 +02002319 void
2320ex_lua(exarg_T *eap)
2321{
Bram Moolenaar0ba04292010-07-14 23:23:17 +02002322 char *script;
2323 if (lua_init() == FAIL) return;
2324 script = (char *) script_get(eap, eap->arg);
Bram Moolenaar55d5c032010-07-17 23:52:29 +02002325 if (!eap->skip)
2326 {
2327 char *s = (script) ? script : (char *) eap->arg;
2328 luaV_setrange(L, eap->line1, eap->line2);
2329 if (luaL_loadbuffer(L, s, strlen(s), LUAVIM_CHUNKNAME)
2330 || lua_pcall(L, 0, 0, 0))
2331 luaV_emsg(L);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02002332 }
2333 if (script != NULL) vim_free(script);
2334}
2335
Bram Moolenaar55d5c032010-07-17 23:52:29 +02002336 void
2337ex_luado(exarg_T *eap)
2338{
Bram Moolenaar0ba04292010-07-14 23:23:17 +02002339 linenr_T l;
2340 const char *s = (const char *) eap->arg;
2341 luaL_Buffer b;
2342 size_t len;
Bram Moolenaard58f03b2017-01-29 22:48:45 +01002343 buf_T *was_curbuf = curbuf;
2344
Bram Moolenaar0ba04292010-07-14 23:23:17 +02002345 if (lua_init() == FAIL) return;
Bram Moolenaar55d5c032010-07-17 23:52:29 +02002346 if (u_save(eap->line1 - 1, eap->line2 + 1) == FAIL)
2347 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002348 emsg(_("cannot save undo information"));
Bram Moolenaar55d5c032010-07-17 23:52:29 +02002349 return;
Bram Moolenaar0ba04292010-07-14 23:23:17 +02002350 }
2351 luaV_setrange(L, eap->line1, eap->line2);
2352 luaL_buffinit(L, &b);
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002353 luaL_addlstring(&b, "return function(line, linenr) ", 30); // header
Bram Moolenaar0ba04292010-07-14 23:23:17 +02002354 luaL_addlstring(&b, s, strlen(s));
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002355 luaL_addlstring(&b, " end", 4); // footer
Bram Moolenaar0ba04292010-07-14 23:23:17 +02002356 luaL_pushresult(&b);
2357 s = lua_tolstring(L, -1, &len);
Bram Moolenaar55d5c032010-07-17 23:52:29 +02002358 if (luaL_loadbuffer(L, s, len, LUAVIM_CHUNKNAME))
2359 {
2360 luaV_emsg(L);
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002361 lua_pop(L, 1); // function body
Bram Moolenaar55d5c032010-07-17 23:52:29 +02002362 return;
Bram Moolenaar0ba04292010-07-14 23:23:17 +02002363 }
2364 lua_call(L, 0, 1);
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002365 lua_replace(L, -2); // function -> body
Bram Moolenaar55d5c032010-07-17 23:52:29 +02002366 for (l = eap->line1; l <= eap->line2; l++)
2367 {
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002368 // Check the line number, the command my have deleted lines.
Bram Moolenaard58f03b2017-01-29 22:48:45 +01002369 if (l > curbuf->b_ml.ml_line_count)
2370 break;
2371
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002372 lua_pushvalue(L, -1); // function
2373 luaV_pushline(L, curbuf, l); // current line as arg
2374 lua_pushinteger(L, l); // current line number as arg
Bram Moolenaarbd2f3c32012-04-06 14:31:00 +02002375 if (lua_pcall(L, 2, 1, 0))
Bram Moolenaar55d5c032010-07-17 23:52:29 +02002376 {
2377 luaV_emsg(L);
2378 break;
2379 }
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002380 // Catch the command switching to another buffer.
Bram Moolenaard58f03b2017-01-29 22:48:45 +01002381 if (curbuf != was_curbuf)
2382 break;
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002383 if (lua_isstring(L, -1)) // update line?
Bram Moolenaar55d5c032010-07-17 23:52:29 +02002384 {
Bram Moolenaar0ba04292010-07-14 23:23:17 +02002385#ifdef HAVE_SANDBOX
Bram Moolenaar55d5c032010-07-17 23:52:29 +02002386 luaV_checksandbox(L);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02002387#endif
Bram Moolenaar55d5c032010-07-17 23:52:29 +02002388 ml_replace(l, luaV_toline(L, -1), TRUE);
2389 changed_bytes(l, 0);
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002390 lua_pop(L, 1); // result from luaV_toline
Bram Moolenaar55d5c032010-07-17 23:52:29 +02002391 }
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002392 lua_pop(L, 1); // line
Bram Moolenaar0ba04292010-07-14 23:23:17 +02002393 }
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002394 lua_pop(L, 1); // function
Bram Moolenaar0ba04292010-07-14 23:23:17 +02002395 check_cursor();
2396 update_screen(NOT_VALID);
2397}
2398
Bram Moolenaar55d5c032010-07-17 23:52:29 +02002399 void
2400ex_luafile(exarg_T *eap)
2401{
2402 if (lua_init() == FAIL)
2403 return;
2404 if (!eap->skip)
2405 {
2406 luaV_setrange(L, eap->line1, eap->line2);
2407 if (luaL_loadfile(L, (char *) eap->arg) || lua_pcall(L, 0, 0, 0))
2408 luaV_emsg(L);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02002409 }
2410}
2411
Bram Moolenaar1dced572012-04-05 16:54:08 +02002412#define luaV_freetype(typ,tname) \
2413 void \
2414 lua_##tname##_free(typ *o) \
2415 { \
2416 if (!lua_isopen()) return; \
2417 luaV_getfield(L, LUAVIM_FREE); \
2418 lua_pushlightuserdata(L, (void *) o); \
2419 lua_call(L, 1, 0); \
2420 }
2421
2422luaV_freetype(buf_T, buffer)
2423luaV_freetype(win_T, window)
2424
Bram Moolenaar55d5c032010-07-17 23:52:29 +02002425 void
Bram Moolenaar4eefe472019-03-19 21:59:19 +01002426do_luaeval(char_u *str, typval_T *arg, typval_T *rettv)
Bram Moolenaar55d5c032010-07-17 23:52:29 +02002427{
Bram Moolenaar1dced572012-04-05 16:54:08 +02002428 lua_init();
2429 luaV_getfield(L, LUAVIM_LUAEVAL);
2430 lua_pushstring(L, (char *) str);
2431 lua_pushlightuserdata(L, (void *) arg);
2432 lua_pushlightuserdata(L, (void *) rettv);
2433 lua_call(L, 3, 0);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02002434}
2435
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01002436 int
Bram Moolenaar4eefe472019-03-19 21:59:19 +01002437set_ref_in_lua(int copyID)
Bram Moolenaar55d5c032010-07-17 23:52:29 +02002438{
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01002439 int aborted = 0;
2440
2441 if (lua_isopen())
2442 {
2443 luaV_getfield(L, LUAVIM_SETREF);
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002444 // call the function with 1 arg, getting 1 result back
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01002445 lua_pushinteger(L, copyID);
2446 lua_call(L, 1, 1);
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002447 // get the result
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01002448 aborted = lua_tointeger(L, -1);
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002449 // pop result off the stack
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01002450 lua_pop(L, 1);
2451 }
2452 return aborted;
Bram Moolenaar0ba04292010-07-14 23:23:17 +02002453}
2454
Bram Moolenaar788fbb42020-05-31 14:08:12 +02002455 void
2456update_package_paths_in_lua()
2457{
2458 if (lua_isopen())
2459 {
2460 lua_getglobal(L, "vim");
2461 lua_getfield(L, -1, "_update_package_paths");
2462
2463 if (lua_pcall(L, 0, 0, 0))
2464 luaV_emsg(L);
2465 }
2466}
2467
Bram Moolenaar801ab062020-06-25 19:27:56 +02002468/*
2469 * Native C function callback
2470 */
2471 static int
2472luaV_call_lua_func(
2473 int argcount,
2474 typval_T *argvars,
2475 typval_T *rettv,
2476 void *state)
2477{
2478 int i;
2479 int luaargcount = argcount;
2480 luaV_CFuncState *funcstate = (luaV_CFuncState*)state;
2481 lua_rawgeti(funcstate->L, LUA_REGISTRYINDEX, funcstate->lua_funcref);
2482
2483 if (funcstate->lua_tableref != LUA_NOREF)
2484 {
2485 // First arg for metatable __call method is a table
2486 luaargcount += 1;
2487 lua_rawgeti(funcstate->L, LUA_REGISTRYINDEX, funcstate->lua_tableref);
2488 }
2489
2490 for (i = 0; i < argcount; ++i)
2491 luaV_pushtypval(funcstate->L, &argvars[i]);
2492
2493 if (lua_pcall(funcstate->L, luaargcount, 1, 0))
2494 {
2495 luaV_emsg(funcstate->L);
2496 return FCERR_OTHER;
2497 }
2498
2499 luaV_checktypval(funcstate->L, -1, rettv, "get return value");
2500 return FCERR_NONE;
2501}
2502
2503/*
2504 * Free up any lua references held by the func state.
2505 */
2506 static void
2507luaV_call_lua_func_free(void *state)
2508{
2509 luaV_CFuncState *funcstate = (luaV_CFuncState*)state;
2510 luaL_unref(L, LUA_REGISTRYINDEX, funcstate->lua_funcref);
2511 funcstate->L = NULL;
2512 if (funcstate->lua_tableref != LUA_NOREF)
2513 luaL_unref(L, LUA_REGISTRYINDEX, funcstate->lua_tableref);
2514 VIM_CLEAR(funcstate);
2515}
2516
Bram Moolenaar0ba04292010-07-14 23:23:17 +02002517#endif