blob: 75231b4c272361d2f6525f41452a0ee55e65b73b [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 Moolenaar1dced572012-04-05 16:54:08 +020038static const char LUAVIM_DICT[] = "dict";
39static const char LUAVIM_LIST[] = "list";
Bram Moolenaarb7828692019-03-23 13:57:02 +010040static const char LUAVIM_BLOB[] = "blob";
Bram Moolenaarca06da92018-07-01 15:12:05 +020041static const char LUAVIM_FUNCREF[] = "funcref";
Bram Moolenaar0ba04292010-07-14 23:23:17 +020042static const char LUAVIM_BUFFER[] = "buffer";
43static const char LUAVIM_WINDOW[] = "window";
44static const char LUAVIM_FREE[] = "luaV_free";
Bram Moolenaar1dced572012-04-05 16:54:08 +020045static const char LUAVIM_LUAEVAL[] = "luaV_luaeval";
46static const char LUAVIM_SETREF[] = "luaV_setref";
Bram Moolenaar0ba04292010-07-14 23:23:17 +020047
Bram Moolenaar2ab2e862019-12-04 21:24:53 +010048// most functions are closures with a cache table as first upvalue;
49// get/setudata manage references to vim userdata in cache table through
50// object pointers (light userdata)
Bram Moolenaar1dced572012-04-05 16:54:08 +020051#define luaV_getudata(L, v) \
52 lua_pushlightuserdata((L), (void *) (v)); \
53 lua_rawget((L), lua_upvalueindex(1))
54#define luaV_setudata(L, v) \
55 lua_pushlightuserdata((L), (void *) (v)); \
56 lua_pushvalue((L), -2); \
57 lua_rawset((L), lua_upvalueindex(1))
Bram Moolenaar0ba04292010-07-14 23:23:17 +020058#define luaV_getfield(L, s) \
59 lua_pushlightuserdata((L), (void *)(s)); \
60 lua_rawget((L), LUA_REGISTRYINDEX)
61#define luaV_checksandbox(L) \
62 if (sandbox) luaL_error((L), "not allowed in sandbox")
63#define luaV_msg(L) luaV_msgfunc((L), (msgfunc_T) msg)
64#define luaV_emsg(L) luaV_msgfunc((L), (msgfunc_T) emsg)
Bram Moolenaarca06da92018-07-01 15:12:05 +020065#define luaV_checktypval(L, a, v, msg) \
66 do { \
67 if (luaV_totypval(L, a, v) == FAIL) \
68 luaL_error(L, msg ": cannot convert value"); \
69 } while (0)
Bram Moolenaar0ba04292010-07-14 23:23:17 +020070
Bram Moolenaarca06da92018-07-01 15:12:05 +020071static luaV_List *luaV_pushlist(lua_State *L, list_T *lis);
72static luaV_Dict *luaV_pushdict(lua_State *L, dict_T *dic);
Bram Moolenaarb7828692019-03-23 13:57:02 +010073static luaV_Blob *luaV_pushblob(lua_State *L, blob_T *blo);
Bram Moolenaar4eefe472019-03-19 21:59:19 +010074static luaV_Funcref *luaV_pushfuncref(lua_State *L, char_u *name);
Bram Moolenaar1dced572012-04-05 16:54:08 +020075
76#if LUA_VERSION_NUM <= 501
77#define luaV_openlib(L, l, n) luaL_openlib(L, NULL, l, n)
78#define luaL_typeerror luaL_typerror
79#else
80#define luaV_openlib luaL_setfuncs
81#endif
Bram Moolenaar0ba04292010-07-14 23:23:17 +020082
83#ifdef DYNAMIC_LUA
Bram Moolenaar2334b6d2010-07-22 21:32:16 +020084
Bram Moolenaar4f974752019-02-17 17:44:42 +010085#ifndef MSWIN
Bram Moolenaar2334b6d2010-07-22 21:32:16 +020086# include <dlfcn.h>
87# define HANDLE void*
88# define load_dll(n) dlopen((n), RTLD_LAZY|RTLD_GLOBAL)
89# define symbol_from_dll dlsym
90# define close_dll dlclose
91#else
Bram Moolenaarebbcb822010-10-23 14:02:54 +020092# define load_dll vimLoadLib
Bram Moolenaar2334b6d2010-07-22 21:32:16 +020093# define symbol_from_dll GetProcAddress
94# define close_dll FreeLibrary
95#endif
96
Bram Moolenaar2ab2e862019-12-04 21:24:53 +010097// lauxlib
Bram Moolenaar1dced572012-04-05 16:54:08 +020098#if LUA_VERSION_NUM <= 501
Bram Moolenaar0ba04292010-07-14 23:23:17 +020099#define luaL_register dll_luaL_register
Bram Moolenaar1dced572012-04-05 16:54:08 +0200100#define luaL_prepbuffer dll_luaL_prepbuffer
101#define luaL_openlib dll_luaL_openlib
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200102#define luaL_typerror dll_luaL_typerror
Bram Moolenaar1dced572012-04-05 16:54:08 +0200103#define luaL_loadfile dll_luaL_loadfile
104#define luaL_loadbuffer dll_luaL_loadbuffer
105#else
106#define luaL_prepbuffsize dll_luaL_prepbuffsize
107#define luaL_setfuncs dll_luaL_setfuncs
108#define luaL_loadfilex dll_luaL_loadfilex
109#define luaL_loadbufferx dll_luaL_loadbufferx
110#define luaL_argerror dll_luaL_argerror
111#endif
Bram Moolenaarbd2f3c32012-04-06 14:31:00 +0200112#define luaL_checkany dll_luaL_checkany
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200113#define luaL_checklstring dll_luaL_checklstring
114#define luaL_checkinteger dll_luaL_checkinteger
115#define luaL_optinteger dll_luaL_optinteger
116#define luaL_checktype dll_luaL_checktype
117#define luaL_error dll_luaL_error
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200118#define luaL_newstate dll_luaL_newstate
119#define luaL_buffinit dll_luaL_buffinit
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200120#define luaL_addlstring dll_luaL_addlstring
121#define luaL_pushresult dll_luaL_pushresult
Bram Moolenaardf1643a2020-05-17 18:53:27 +0200122#define luaL_loadstring dll_luaL_loadstring
Bram Moolenaar2ab2e862019-12-04 21:24:53 +0100123// lua
Bram Moolenaar1dced572012-04-05 16:54:08 +0200124#if LUA_VERSION_NUM <= 501
125#define lua_tonumber dll_lua_tonumber
126#define lua_tointeger dll_lua_tointeger
127#define lua_call dll_lua_call
128#define lua_pcall dll_lua_pcall
129#else
130#define lua_tonumberx dll_lua_tonumberx
131#define lua_tointegerx dll_lua_tointegerx
132#define lua_callk dll_lua_callk
133#define lua_pcallk dll_lua_pcallk
134#define lua_getglobal dll_lua_getglobal
135#define lua_setglobal dll_lua_setglobal
Bram Moolenaar1dced572012-04-05 16:54:08 +0200136#endif
Bram Moolenaar1f860d82015-06-27 18:36:16 +0200137#if LUA_VERSION_NUM <= 502
138#define lua_replace dll_lua_replace
139#define lua_remove dll_lua_remove
140#endif
141#if LUA_VERSION_NUM >= 503
142#define lua_rotate dll_lua_rotate
143#define lua_copy dll_lua_copy
144#endif
Bram Moolenaarbd2f3c32012-04-06 14:31:00 +0200145#define lua_typename dll_lua_typename
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200146#define lua_close dll_lua_close
147#define lua_gettop dll_lua_gettop
148#define lua_settop dll_lua_settop
149#define lua_pushvalue dll_lua_pushvalue
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200150#define lua_isnumber dll_lua_isnumber
151#define lua_isstring dll_lua_isstring
152#define lua_type dll_lua_type
153#define lua_rawequal dll_lua_rawequal
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200154#define lua_toboolean dll_lua_toboolean
155#define lua_tolstring dll_lua_tolstring
156#define lua_touserdata dll_lua_touserdata
157#define lua_pushnil dll_lua_pushnil
158#define lua_pushnumber dll_lua_pushnumber
159#define lua_pushinteger dll_lua_pushinteger
160#define lua_pushlstring dll_lua_pushlstring
161#define lua_pushstring dll_lua_pushstring
162#define lua_pushfstring dll_lua_pushfstring
163#define lua_pushcclosure dll_lua_pushcclosure
164#define lua_pushboolean dll_lua_pushboolean
165#define lua_pushlightuserdata dll_lua_pushlightuserdata
166#define lua_getfield dll_lua_getfield
167#define lua_rawget dll_lua_rawget
Bram Moolenaar1dced572012-04-05 16:54:08 +0200168#define lua_rawgeti dll_lua_rawgeti
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200169#define lua_createtable dll_lua_createtable
Bram Moolenaar830e3582018-08-21 14:23:35 +0200170#if LUA_VERSION_NUM >= 504
171 #define lua_newuserdatauv dll_lua_newuserdatauv
172#else
173 #define lua_newuserdata dll_lua_newuserdata
174#endif
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200175#define lua_getmetatable dll_lua_getmetatable
176#define lua_setfield dll_lua_setfield
177#define lua_rawset dll_lua_rawset
178#define lua_rawseti dll_lua_rawseti
179#define lua_setmetatable dll_lua_setmetatable
Bram Moolenaarbd2f3c32012-04-06 14:31:00 +0200180#define lua_next dll_lua_next
Bram Moolenaar2ab2e862019-12-04 21:24:53 +0100181// libs
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200182#define luaopen_base dll_luaopen_base
183#define luaopen_table dll_luaopen_table
184#define luaopen_string dll_luaopen_string
185#define luaopen_math dll_luaopen_math
Bram Moolenaar16c98f92010-07-28 22:46:08 +0200186#define luaopen_io dll_luaopen_io
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200187#define luaopen_os dll_luaopen_os
188#define luaopen_package dll_luaopen_package
189#define luaopen_debug dll_luaopen_debug
Bram Moolenaar16c98f92010-07-28 22:46:08 +0200190#define luaL_openlibs dll_luaL_openlibs
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200191
Bram Moolenaar2ab2e862019-12-04 21:24:53 +0100192// lauxlib
Bram Moolenaar1dced572012-04-05 16:54:08 +0200193#if LUA_VERSION_NUM <= 501
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200194void (*dll_luaL_register) (lua_State *L, const char *libname, const luaL_Reg *l);
Bram Moolenaar1dced572012-04-05 16:54:08 +0200195char *(*dll_luaL_prepbuffer) (luaL_Buffer *B);
196void (*dll_luaL_openlib) (lua_State *L, const char *libname, const luaL_Reg *l, int nup);
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200197int (*dll_luaL_typerror) (lua_State *L, int narg, const char *tname);
Bram Moolenaar1dced572012-04-05 16:54:08 +0200198int (*dll_luaL_loadfile) (lua_State *L, const char *filename);
199int (*dll_luaL_loadbuffer) (lua_State *L, const char *buff, size_t sz, const char *name);
200#else
201char *(*dll_luaL_prepbuffsize) (luaL_Buffer *B, size_t sz);
202void (*dll_luaL_setfuncs) (lua_State *L, const luaL_Reg *l, int nup);
203int (*dll_luaL_loadfilex) (lua_State *L, const char *filename, const char *mode);
204int (*dll_luaL_loadbufferx) (lua_State *L, const char *buff, size_t sz, const char *name, const char *mode);
205int (*dll_luaL_argerror) (lua_State *L, int numarg, const char *extramsg);
206#endif
Bram Moolenaarbd2f3c32012-04-06 14:31:00 +0200207void (*dll_luaL_checkany) (lua_State *L, int narg);
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200208const char *(*dll_luaL_checklstring) (lua_State *L, int numArg, size_t *l);
209lua_Integer (*dll_luaL_checkinteger) (lua_State *L, int numArg);
210lua_Integer (*dll_luaL_optinteger) (lua_State *L, int nArg, lua_Integer def);
211void (*dll_luaL_checktype) (lua_State *L, int narg, int t);
212int (*dll_luaL_error) (lua_State *L, const char *fmt, ...);
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200213lua_State *(*dll_luaL_newstate) (void);
214void (*dll_luaL_buffinit) (lua_State *L, luaL_Buffer *B);
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200215void (*dll_luaL_addlstring) (luaL_Buffer *B, const char *s, size_t l);
216void (*dll_luaL_pushresult) (luaL_Buffer *B);
Bram Moolenaardf1643a2020-05-17 18:53:27 +0200217int (*dll_luaL_loadstring) (lua_State *L, const char *s);
Bram Moolenaar2ab2e862019-12-04 21:24:53 +0100218// lua
Bram Moolenaar1dced572012-04-05 16:54:08 +0200219#if LUA_VERSION_NUM <= 501
220lua_Number (*dll_lua_tonumber) (lua_State *L, int idx);
221lua_Integer (*dll_lua_tointeger) (lua_State *L, int idx);
222void (*dll_lua_call) (lua_State *L, int nargs, int nresults);
223int (*dll_lua_pcall) (lua_State *L, int nargs, int nresults, int errfunc);
224#else
225lua_Number (*dll_lua_tonumberx) (lua_State *L, int idx, int *isnum);
226lua_Integer (*dll_lua_tointegerx) (lua_State *L, int idx, int *isnum);
227void (*dll_lua_callk) (lua_State *L, int nargs, int nresults, int ctx,
Bram Moolenaardb913952012-06-29 12:54:53 +0200228 lua_CFunction k);
Bram Moolenaar1dced572012-04-05 16:54:08 +0200229int (*dll_lua_pcallk) (lua_State *L, int nargs, int nresults, int errfunc,
Bram Moolenaardb913952012-06-29 12:54:53 +0200230 int ctx, lua_CFunction k);
Bram Moolenaar1dced572012-04-05 16:54:08 +0200231void (*dll_lua_getglobal) (lua_State *L, const char *var);
232void (*dll_lua_setglobal) (lua_State *L, const char *var);
Bram Moolenaar1f860d82015-06-27 18:36:16 +0200233#endif
234#if LUA_VERSION_NUM <= 502
235void (*dll_lua_replace) (lua_State *L, int idx);
236void (*dll_lua_remove) (lua_State *L, int idx);
237#endif
238#if LUA_VERSION_NUM >= 503
239void (*dll_lua_rotate) (lua_State *L, int idx, int n);
Bram Moolenaar9514b1f2015-06-25 18:27:32 +0200240void (*dll_lua_copy) (lua_State *L, int fromidx, int toidx);
Bram Moolenaar1dced572012-04-05 16:54:08 +0200241#endif
Bram Moolenaarbd2f3c32012-04-06 14:31:00 +0200242const char *(*dll_lua_typename) (lua_State *L, int tp);
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200243void (*dll_lua_close) (lua_State *L);
244int (*dll_lua_gettop) (lua_State *L);
245void (*dll_lua_settop) (lua_State *L, int idx);
246void (*dll_lua_pushvalue) (lua_State *L, int idx);
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200247int (*dll_lua_isnumber) (lua_State *L, int idx);
248int (*dll_lua_isstring) (lua_State *L, int idx);
249int (*dll_lua_type) (lua_State *L, int idx);
250int (*dll_lua_rawequal) (lua_State *L, int idx1, int idx2);
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200251int (*dll_lua_toboolean) (lua_State *L, int idx);
252const char *(*dll_lua_tolstring) (lua_State *L, int idx, size_t *len);
253void *(*dll_lua_touserdata) (lua_State *L, int idx);
254void (*dll_lua_pushnil) (lua_State *L);
255void (*dll_lua_pushnumber) (lua_State *L, lua_Number n);
256void (*dll_lua_pushinteger) (lua_State *L, lua_Integer n);
257void (*dll_lua_pushlstring) (lua_State *L, const char *s, size_t l);
258void (*dll_lua_pushstring) (lua_State *L, const char *s);
259const char *(*dll_lua_pushfstring) (lua_State *L, const char *fmt, ...);
260void (*dll_lua_pushcclosure) (lua_State *L, lua_CFunction fn, int n);
261void (*dll_lua_pushboolean) (lua_State *L, int b);
262void (*dll_lua_pushlightuserdata) (lua_State *L, void *p);
263void (*dll_lua_getfield) (lua_State *L, int idx, const char *k);
Bram Moolenaar17413672018-07-14 20:49:42 +0200264#if LUA_VERSION_NUM <= 502
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200265void (*dll_lua_rawget) (lua_State *L, int idx);
Bram Moolenaar1dced572012-04-05 16:54:08 +0200266void (*dll_lua_rawgeti) (lua_State *L, int idx, int n);
Bram Moolenaar17413672018-07-14 20:49:42 +0200267#else
268int (*dll_lua_rawget) (lua_State *L, int idx);
269int (*dll_lua_rawgeti) (lua_State *L, int idx, lua_Integer n);
270#endif
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200271void (*dll_lua_createtable) (lua_State *L, int narr, int nrec);
Bram Moolenaar830e3582018-08-21 14:23:35 +0200272#if LUA_VERSION_NUM >= 504
273void *(*dll_lua_newuserdatauv) (lua_State *L, size_t sz, int nuvalue);
274#else
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200275void *(*dll_lua_newuserdata) (lua_State *L, size_t sz);
Bram Moolenaar830e3582018-08-21 14:23:35 +0200276#endif
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200277int (*dll_lua_getmetatable) (lua_State *L, int objindex);
278void (*dll_lua_setfield) (lua_State *L, int idx, const char *k);
279void (*dll_lua_rawset) (lua_State *L, int idx);
Bram Moolenaar17413672018-07-14 20:49:42 +0200280#if LUA_VERSION_NUM <= 502
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200281void (*dll_lua_rawseti) (lua_State *L, int idx, int n);
Bram Moolenaar17413672018-07-14 20:49:42 +0200282#else
283void (*dll_lua_rawseti) (lua_State *L, int idx, lua_Integer n);
284#endif
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200285int (*dll_lua_setmetatable) (lua_State *L, int objindex);
Bram Moolenaarbd2f3c32012-04-06 14:31:00 +0200286int (*dll_lua_next) (lua_State *L, int idx);
Bram Moolenaar2ab2e862019-12-04 21:24:53 +0100287// libs
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200288int (*dll_luaopen_base) (lua_State *L);
289int (*dll_luaopen_table) (lua_State *L);
290int (*dll_luaopen_string) (lua_State *L);
291int (*dll_luaopen_math) (lua_State *L);
Bram Moolenaar16c98f92010-07-28 22:46:08 +0200292int (*dll_luaopen_io) (lua_State *L);
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200293int (*dll_luaopen_os) (lua_State *L);
294int (*dll_luaopen_package) (lua_State *L);
295int (*dll_luaopen_debug) (lua_State *L);
Bram Moolenaar16c98f92010-07-28 22:46:08 +0200296void (*dll_luaL_openlibs) (lua_State *L);
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200297
298typedef void **luaV_function;
299typedef struct {
300 const char *name;
301 luaV_function func;
302} luaV_Reg;
303
304static const luaV_Reg luaV_dll[] = {
Bram Moolenaar2ab2e862019-12-04 21:24:53 +0100305 // lauxlib
Bram Moolenaar1dced572012-04-05 16:54:08 +0200306#if LUA_VERSION_NUM <= 501
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200307 {"luaL_register", (luaV_function) &dll_luaL_register},
Bram Moolenaar1dced572012-04-05 16:54:08 +0200308 {"luaL_prepbuffer", (luaV_function) &dll_luaL_prepbuffer},
309 {"luaL_openlib", (luaV_function) &dll_luaL_openlib},
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200310 {"luaL_typerror", (luaV_function) &dll_luaL_typerror},
Bram Moolenaar1dced572012-04-05 16:54:08 +0200311 {"luaL_loadfile", (luaV_function) &dll_luaL_loadfile},
312 {"luaL_loadbuffer", (luaV_function) &dll_luaL_loadbuffer},
313#else
314 {"luaL_prepbuffsize", (luaV_function) &dll_luaL_prepbuffsize},
315 {"luaL_setfuncs", (luaV_function) &dll_luaL_setfuncs},
316 {"luaL_loadfilex", (luaV_function) &dll_luaL_loadfilex},
317 {"luaL_loadbufferx", (luaV_function) &dll_luaL_loadbufferx},
318 {"luaL_argerror", (luaV_function) &dll_luaL_argerror},
319#endif
Bram Moolenaarbd2f3c32012-04-06 14:31:00 +0200320 {"luaL_checkany", (luaV_function) &dll_luaL_checkany},
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200321 {"luaL_checklstring", (luaV_function) &dll_luaL_checklstring},
322 {"luaL_checkinteger", (luaV_function) &dll_luaL_checkinteger},
323 {"luaL_optinteger", (luaV_function) &dll_luaL_optinteger},
324 {"luaL_checktype", (luaV_function) &dll_luaL_checktype},
325 {"luaL_error", (luaV_function) &dll_luaL_error},
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200326 {"luaL_newstate", (luaV_function) &dll_luaL_newstate},
327 {"luaL_buffinit", (luaV_function) &dll_luaL_buffinit},
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200328 {"luaL_addlstring", (luaV_function) &dll_luaL_addlstring},
329 {"luaL_pushresult", (luaV_function) &dll_luaL_pushresult},
Bram Moolenaardf1643a2020-05-17 18:53:27 +0200330 {"luaL_loadstring", (luaV_function) &dll_luaL_loadstring},
Bram Moolenaar2ab2e862019-12-04 21:24:53 +0100331 // lua
Bram Moolenaar1dced572012-04-05 16:54:08 +0200332#if LUA_VERSION_NUM <= 501
333 {"lua_tonumber", (luaV_function) &dll_lua_tonumber},
334 {"lua_tointeger", (luaV_function) &dll_lua_tointeger},
335 {"lua_call", (luaV_function) &dll_lua_call},
336 {"lua_pcall", (luaV_function) &dll_lua_pcall},
337#else
338 {"lua_tonumberx", (luaV_function) &dll_lua_tonumberx},
339 {"lua_tointegerx", (luaV_function) &dll_lua_tointegerx},
340 {"lua_callk", (luaV_function) &dll_lua_callk},
341 {"lua_pcallk", (luaV_function) &dll_lua_pcallk},
342 {"lua_getglobal", (luaV_function) &dll_lua_getglobal},
343 {"lua_setglobal", (luaV_function) &dll_lua_setglobal},
Bram Moolenaar1dced572012-04-05 16:54:08 +0200344#endif
Bram Moolenaar1f860d82015-06-27 18:36:16 +0200345#if LUA_VERSION_NUM <= 502
346 {"lua_replace", (luaV_function) &dll_lua_replace},
347 {"lua_remove", (luaV_function) &dll_lua_remove},
348#endif
349#if LUA_VERSION_NUM >= 503
350 {"lua_rotate", (luaV_function) &dll_lua_rotate},
351 {"lua_copy", (luaV_function) &dll_lua_copy},
352#endif
Bram Moolenaarbd2f3c32012-04-06 14:31:00 +0200353 {"lua_typename", (luaV_function) &dll_lua_typename},
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200354 {"lua_close", (luaV_function) &dll_lua_close},
355 {"lua_gettop", (luaV_function) &dll_lua_gettop},
356 {"lua_settop", (luaV_function) &dll_lua_settop},
357 {"lua_pushvalue", (luaV_function) &dll_lua_pushvalue},
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200358 {"lua_isnumber", (luaV_function) &dll_lua_isnumber},
359 {"lua_isstring", (luaV_function) &dll_lua_isstring},
360 {"lua_type", (luaV_function) &dll_lua_type},
361 {"lua_rawequal", (luaV_function) &dll_lua_rawequal},
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200362 {"lua_toboolean", (luaV_function) &dll_lua_toboolean},
363 {"lua_tolstring", (luaV_function) &dll_lua_tolstring},
364 {"lua_touserdata", (luaV_function) &dll_lua_touserdata},
365 {"lua_pushnil", (luaV_function) &dll_lua_pushnil},
366 {"lua_pushnumber", (luaV_function) &dll_lua_pushnumber},
367 {"lua_pushinteger", (luaV_function) &dll_lua_pushinteger},
368 {"lua_pushlstring", (luaV_function) &dll_lua_pushlstring},
369 {"lua_pushstring", (luaV_function) &dll_lua_pushstring},
370 {"lua_pushfstring", (luaV_function) &dll_lua_pushfstring},
371 {"lua_pushcclosure", (luaV_function) &dll_lua_pushcclosure},
372 {"lua_pushboolean", (luaV_function) &dll_lua_pushboolean},
373 {"lua_pushlightuserdata", (luaV_function) &dll_lua_pushlightuserdata},
374 {"lua_getfield", (luaV_function) &dll_lua_getfield},
375 {"lua_rawget", (luaV_function) &dll_lua_rawget},
Bram Moolenaar1dced572012-04-05 16:54:08 +0200376 {"lua_rawgeti", (luaV_function) &dll_lua_rawgeti},
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200377 {"lua_createtable", (luaV_function) &dll_lua_createtable},
Bram Moolenaar830e3582018-08-21 14:23:35 +0200378#if LUA_VERSION_NUM >= 504
379 {"lua_newuserdatauv", (luaV_function) &dll_lua_newuserdatauv},
380#else
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200381 {"lua_newuserdata", (luaV_function) &dll_lua_newuserdata},
Bram Moolenaar830e3582018-08-21 14:23:35 +0200382#endif
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200383 {"lua_getmetatable", (luaV_function) &dll_lua_getmetatable},
384 {"lua_setfield", (luaV_function) &dll_lua_setfield},
385 {"lua_rawset", (luaV_function) &dll_lua_rawset},
386 {"lua_rawseti", (luaV_function) &dll_lua_rawseti},
387 {"lua_setmetatable", (luaV_function) &dll_lua_setmetatable},
Bram Moolenaarbd2f3c32012-04-06 14:31:00 +0200388 {"lua_next", (luaV_function) &dll_lua_next},
Bram Moolenaar2ab2e862019-12-04 21:24:53 +0100389 // libs
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200390 {"luaopen_base", (luaV_function) &dll_luaopen_base},
391 {"luaopen_table", (luaV_function) &dll_luaopen_table},
392 {"luaopen_string", (luaV_function) &dll_luaopen_string},
393 {"luaopen_math", (luaV_function) &dll_luaopen_math},
Bram Moolenaar16c98f92010-07-28 22:46:08 +0200394 {"luaopen_io", (luaV_function) &dll_luaopen_io},
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200395 {"luaopen_os", (luaV_function) &dll_luaopen_os},
396 {"luaopen_package", (luaV_function) &dll_luaopen_package},
397 {"luaopen_debug", (luaV_function) &dll_luaopen_debug},
Bram Moolenaar16c98f92010-07-28 22:46:08 +0200398 {"luaL_openlibs", (luaV_function) &dll_luaL_openlibs},
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200399 {NULL, NULL}
400};
401
Bram Moolenaar2334b6d2010-07-22 21:32:16 +0200402static HANDLE hinstLua = NULL;
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200403
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200404 static int
405lua_link_init(char *libname, int verbose)
406{
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200407 const luaV_Reg *reg;
408 if (hinstLua) return OK;
Bram Moolenaar2334b6d2010-07-22 21:32:16 +0200409 hinstLua = load_dll(libname);
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200410 if (!hinstLua)
411 {
412 if (verbose)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100413 semsg(_(e_loadlib), libname);
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200414 return FAIL;
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200415 }
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200416 for (reg = luaV_dll; reg->func; reg++)
417 {
Bram Moolenaar2334b6d2010-07-22 21:32:16 +0200418 if ((*reg->func = symbol_from_dll(hinstLua, reg->name)) == NULL)
419 {
420 close_dll(hinstLua);
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200421 hinstLua = 0;
422 if (verbose)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100423 semsg(_(e_loadfunc), reg->name);
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200424 return FAIL;
425 }
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200426 }
427 return OK;
428}
Bram Moolenaar2ab2e862019-12-04 21:24:53 +0100429#endif // DYNAMIC_LUA
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200430
Bram Moolenaard90b6c02016-08-28 18:10:45 +0200431#if defined(DYNAMIC_LUA) || defined(PROTO)
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200432 int
433lua_enabled(int verbose)
434{
Bram Moolenaar25e4fcd2016-01-09 14:57:47 +0100435 return lua_link_init((char *)p_luadll, verbose) == OK;
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200436}
Bram Moolenaard90b6c02016-08-28 18:10:45 +0200437#endif
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200438
Bram Moolenaar1dced572012-04-05 16:54:08 +0200439#if LUA_VERSION_NUM > 501
440 static int
Bram Moolenaar4eefe472019-03-19 21:59:19 +0100441luaL_typeerror(lua_State *L, int narg, const char *tname)
Bram Moolenaar1dced572012-04-05 16:54:08 +0200442{
443 const char *msg = lua_pushfstring(L, "%s expected, got %s",
Bram Moolenaardb913952012-06-29 12:54:53 +0200444 tname, luaL_typename(L, narg));
Bram Moolenaar1dced572012-04-05 16:54:08 +0200445 return luaL_argerror(L, narg, msg);
446}
447#endif
448
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200449
Bram Moolenaar2ab2e862019-12-04 21:24:53 +0100450// ======= Internal =======
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200451
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200452 static void
453luaV_newmetatable(lua_State *L, const char *tname)
454{
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200455 lua_newtable(L);
456 lua_pushlightuserdata(L, (void *) tname);
457 lua_pushvalue(L, -2);
458 lua_rawset(L, LUA_REGISTRYINDEX);
459}
460
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200461 static void *
462luaV_toudata(lua_State *L, int ud, const char *tname)
463{
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200464 void *p = lua_touserdata(L, ud);
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200465
Bram Moolenaar2ab2e862019-12-04 21:24:53 +0100466 if (p != NULL) // value is userdata?
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200467 {
Bram Moolenaar2ab2e862019-12-04 21:24:53 +0100468 if (lua_getmetatable(L, ud)) // does it have a metatable?
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200469 {
Bram Moolenaar2ab2e862019-12-04 21:24:53 +0100470 luaV_getfield(L, tname); // get metatable
471 if (lua_rawequal(L, -1, -2)) // MTs match?
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200472 {
Bram Moolenaar2ab2e862019-12-04 21:24:53 +0100473 lua_pop(L, 2); // MTs
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200474 return p;
475 }
476 }
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200477 }
478 return NULL;
479}
480
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200481 static void *
Bram Moolenaar1dced572012-04-05 16:54:08 +0200482luaV_checkcache(lua_State *L, void *p)
483{
484 luaV_getudata(L, p);
485 if (lua_isnil(L, -1)) luaL_error(L, "invalid object");
486 lua_pop(L, 1);
487 return p;
488}
489
490#define luaV_unbox(L,luatyp,ud) (*((luatyp *) lua_touserdata((L),(ud))))
491
492#define luaV_checkvalid(L,luatyp,ud) \
493 luaV_checkcache((L), (void *) luaV_unbox((L),luatyp,(ud)))
494
495 static void *
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200496luaV_checkudata(lua_State *L, int ud, const char *tname)
497{
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200498 void *p = luaV_toudata(L, ud, tname);
Bram Moolenaar1dced572012-04-05 16:54:08 +0200499 if (p == NULL) luaL_typeerror(L, ud, tname);
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200500 return p;
501}
502
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200503 static void
504luaV_pushtypval(lua_State *L, typval_T *tv)
505{
Bram Moolenaar1dced572012-04-05 16:54:08 +0200506 if (tv == NULL)
507 {
508 lua_pushnil(L);
509 return;
510 }
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200511 switch (tv->v_type)
512 {
513 case VAR_STRING:
Bram Moolenaard04da7c2012-10-14 03:41:59 +0200514 lua_pushstring(L, tv->vval.v_string == NULL
515 ? "" : (char *)tv->vval.v_string);
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200516 break;
517 case VAR_NUMBER:
518 lua_pushinteger(L, (int) tv->vval.v_number);
519 break;
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200520#ifdef FEAT_FLOAT
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200521 case VAR_FLOAT:
522 lua_pushnumber(L, (lua_Number) tv->vval.v_float);
523 break;
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200524#endif
Bram Moolenaar1dced572012-04-05 16:54:08 +0200525 case VAR_LIST:
526 luaV_pushlist(L, tv->vval.v_list);
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200527 break;
Bram Moolenaar1dced572012-04-05 16:54:08 +0200528 case VAR_DICT:
529 luaV_pushdict(L, tv->vval.v_dict);
530 break;
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +0100531 case VAR_BOOL:
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100532 case VAR_SPECIAL:
533 if (tv->vval.v_number <= VVAL_TRUE)
534 lua_pushinteger(L, (int) tv->vval.v_number);
535 else
536 lua_pushnil(L);
537 break;
Bram Moolenaarca06da92018-07-01 15:12:05 +0200538 case VAR_FUNC:
Bram Moolenaar4eefe472019-03-19 21:59:19 +0100539 luaV_pushfuncref(L, tv->vval.v_string);
Bram Moolenaarca06da92018-07-01 15:12:05 +0200540 break;
Bram Moolenaarb7828692019-03-23 13:57:02 +0100541 case VAR_BLOB:
542 luaV_pushblob(L, tv->vval.v_blob);
543 break;
Bram Moolenaar1dced572012-04-05 16:54:08 +0200544 default:
545 lua_pushnil(L);
546 }
547}
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200548
Bram Moolenaarca06da92018-07-01 15:12:05 +0200549/*
550 * Converts lua value at 'pos' to typval 'tv'.
551 * Returns OK or FAIL.
552 */
553 static int
554luaV_totypval(lua_State *L, int pos, typval_T *tv)
Bram Moolenaar1dced572012-04-05 16:54:08 +0200555{
Bram Moolenaarca06da92018-07-01 15:12:05 +0200556 int status = OK;
557
558 switch (lua_type(L, pos))
559 {
Bram Moolenaar1dced572012-04-05 16:54:08 +0200560 case LUA_TBOOLEAN:
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +0100561 tv->v_type = VAR_BOOL;
Bram Moolenaar1dced572012-04-05 16:54:08 +0200562 tv->vval.v_number = (varnumber_T) lua_toboolean(L, pos);
563 break;
Bram Moolenaar9067cd62019-01-01 00:41:54 +0100564 case LUA_TNIL:
565 tv->v_type = VAR_SPECIAL;
566 tv->vval.v_number = VVAL_NULL;
567 break;
Bram Moolenaar1dced572012-04-05 16:54:08 +0200568 case LUA_TSTRING:
569 tv->v_type = VAR_STRING;
570 tv->vval.v_string = vim_strsave((char_u *) lua_tostring(L, pos));
571 break;
572 case LUA_TNUMBER:
573#ifdef FEAT_FLOAT
Bram Moolenaareb04f082020-05-17 14:32:35 +0200574 {
575 const lua_Number n = lua_tonumber(L, pos);
576
577 if (n > (lua_Number)INT64_MAX || n < (lua_Number)INT64_MIN
578 || ((lua_Number)((varnumber_T)n)) != n)
579 {
580 tv->v_type = VAR_FLOAT;
581 tv->vval.v_float = (float_T)n;
582 }
583 else
584 {
585 tv->v_type = VAR_NUMBER;
586 tv->vval.v_number = (varnumber_T)n;
587 }
588 }
Bram Moolenaar1dced572012-04-05 16:54:08 +0200589#else
590 tv->v_type = VAR_NUMBER;
591 tv->vval.v_number = (varnumber_T) lua_tointeger(L, pos);
592#endif
593 break;
Bram Moolenaarca06da92018-07-01 15:12:05 +0200594 case LUA_TUSERDATA:
595 {
Bram Moolenaar1dced572012-04-05 16:54:08 +0200596 void *p = lua_touserdata(L, pos);
Bram Moolenaarca06da92018-07-01 15:12:05 +0200597
Bram Moolenaarb7828692019-03-23 13:57:02 +0100598 if (lua_getmetatable(L, pos)) // has metatable?
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200599 {
Bram Moolenaarb7828692019-03-23 13:57:02 +0100600 // check list
Bram Moolenaar1dced572012-04-05 16:54:08 +0200601 luaV_getfield(L, LUAVIM_LIST);
602 if (lua_rawequal(L, -1, -2))
Bram Moolenaar2334b6d2010-07-22 21:32:16 +0200603 {
Bram Moolenaar1dced572012-04-05 16:54:08 +0200604 tv->v_type = VAR_LIST;
605 tv->vval.v_list = *((luaV_List *) p);
606 ++tv->vval.v_list->lv_refcount;
Bram Moolenaarb7828692019-03-23 13:57:02 +0100607 lua_pop(L, 2); // MTs
Bram Moolenaarca06da92018-07-01 15:12:05 +0200608 break;
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200609 }
Bram Moolenaarb7828692019-03-23 13:57:02 +0100610 // check dict
Bram Moolenaar1dced572012-04-05 16:54:08 +0200611 luaV_getfield(L, LUAVIM_DICT);
612 if (lua_rawequal(L, -1, -3))
613 {
614 tv->v_type = VAR_DICT;
615 tv->vval.v_dict = *((luaV_Dict *) p);
616 ++tv->vval.v_dict->dv_refcount;
Bram Moolenaarb7828692019-03-23 13:57:02 +0100617 lua_pop(L, 3); // MTs
Bram Moolenaarca06da92018-07-01 15:12:05 +0200618 break;
Bram Moolenaar1dced572012-04-05 16:54:08 +0200619 }
Bram Moolenaarb7828692019-03-23 13:57:02 +0100620 // check blob
621 luaV_getfield(L, LUAVIM_BLOB);
Bram Moolenaarca06da92018-07-01 15:12:05 +0200622 if (lua_rawequal(L, -1, -4))
623 {
Bram Moolenaarb7828692019-03-23 13:57:02 +0100624 tv->v_type = VAR_BLOB;
625 tv->vval.v_blob = *((luaV_Blob *) p);
626 ++tv->vval.v_blob->bv_refcount;
627 lua_pop(L, 4); // MTs
628 break;
629 }
630 // check funcref
631 luaV_getfield(L, LUAVIM_FUNCREF);
632 if (lua_rawequal(L, -1, -5))
633 {
Bram Moolenaarca06da92018-07-01 15:12:05 +0200634 luaV_Funcref *f = (luaV_Funcref *) p;
Bram Moolenaar4eefe472019-03-19 21:59:19 +0100635 func_ref(f->name);
636 tv->v_type = VAR_FUNC;
637 tv->vval.v_string = vim_strsave(f->name);
Bram Moolenaarb7828692019-03-23 13:57:02 +0100638 lua_pop(L, 5); // MTs
Bram Moolenaarca06da92018-07-01 15:12:05 +0200639 break;
640 }
Bram Moolenaarb7828692019-03-23 13:57:02 +0100641 lua_pop(L, 4); // MTs
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200642 }
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200643 }
Bram Moolenaarb7828692019-03-23 13:57:02 +0100644 // FALLTHROUGH
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200645 default:
Bram Moolenaar1dced572012-04-05 16:54:08 +0200646 tv->v_type = VAR_NUMBER;
647 tv->vval.v_number = 0;
Bram Moolenaarca06da92018-07-01 15:12:05 +0200648 status = FAIL;
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200649 }
Bram Moolenaarca06da92018-07-01 15:12:05 +0200650 return status;
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200651}
652
Bram Moolenaar2ab2e862019-12-04 21:24:53 +0100653/*
654 * similar to luaL_addlstring, but replaces \0 with \n if toline and
655 * \n with \0 otherwise
656 */
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200657 static void
658luaV_addlstring(luaL_Buffer *b, const char *s, size_t l, int toline)
659{
660 while (l--)
661 {
662 if (*s == '\0' && toline)
663 luaL_addchar(b, '\n');
664 else if (*s == '\n' && !toline)
665 luaL_addchar(b, '\0');
666 else
667 luaL_addchar(b, *s);
668 s++;
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200669 }
670}
671
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200672 static void
673luaV_pushline(lua_State *L, buf_T *buf, linenr_T n)
674{
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200675 const char *s = (const char *) ml_get_buf(buf, n, FALSE);
676 luaL_Buffer b;
677 luaL_buffinit(L, &b);
678 luaV_addlstring(&b, s, strlen(s), 0);
679 luaL_pushresult(&b);
680}
681
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200682 static char_u *
683luaV_toline(lua_State *L, int pos)
684{
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200685 size_t l;
686 const char *s = lua_tolstring(L, pos, &l);
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200687
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200688 luaL_Buffer b;
689 luaL_buffinit(L, &b);
690 luaV_addlstring(&b, s, l, 1);
691 luaL_pushresult(&b);
692 return (char_u *) lua_tostring(L, -1);
693}
694
Bram Moolenaar2ab2e862019-12-04 21:24:53 +0100695/*
696 * pops a string s from the top of the stack and calls mf(t) for pieces t of
697 * s separated by newlines
698 */
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200699 static void
700luaV_msgfunc(lua_State *L, msgfunc_T mf)
701{
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200702 luaL_Buffer b;
703 size_t l;
704 const char *p, *s = lua_tolstring(L, -1, &l);
705 luaL_buffinit(L, &b);
706 luaV_addlstring(&b, s, l, 0);
707 luaL_pushresult(&b);
Bram Moolenaar2ab2e862019-12-04 21:24:53 +0100708 // break string
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200709 p = s = lua_tolstring(L, -1, &l);
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200710 while (l--)
711 {
Bram Moolenaar2ab2e862019-12-04 21:24:53 +0100712 if (*p++ == '\0') // break?
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200713 {
714 mf((char_u *) s);
715 s = p;
716 }
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200717 }
718 mf((char_u *) s);
Bram Moolenaar2ab2e862019-12-04 21:24:53 +0100719 lua_pop(L, 2); // original and modified strings
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200720}
721
Bram Moolenaar1dced572012-04-05 16:54:08 +0200722#define luaV_newtype(typ,tname,luatyp,luatname) \
723 static luatyp * \
Bram Moolenaar4eefe472019-03-19 21:59:19 +0100724 luaV_new##tname(lua_State *L, typ *obj) \
Bram Moolenaar1dced572012-04-05 16:54:08 +0200725 { \
726 luatyp *o = (luatyp *) lua_newuserdata(L, sizeof(luatyp)); \
727 *o = obj; \
728 luaV_setudata(L, obj); /* cache[obj] = udata */ \
729 luaV_getfield(L, luatname); \
730 lua_setmetatable(L, -2); \
731 return o; \
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200732 }
Bram Moolenaar1dced572012-04-05 16:54:08 +0200733
734#define luaV_pushtype(typ,tname,luatyp) \
735 static luatyp * \
Bram Moolenaarca06da92018-07-01 15:12:05 +0200736 luaV_push##tname(lua_State *L, typ *obj) \
Bram Moolenaar1dced572012-04-05 16:54:08 +0200737 { \
738 luatyp *o = NULL; \
739 if (obj == NULL) \
740 lua_pushnil(L); \
741 else { \
742 luaV_getudata(L, obj); \
743 if (lua_isnil(L, -1)) /* not interned? */ \
744 { \
745 lua_pop(L, 1); \
746 o = luaV_new##tname(L, obj); \
747 } \
748 else \
749 o = (luatyp *) lua_touserdata(L, -1); \
750 } \
751 return o; \
752 }
753
754#define luaV_type_tostring(tname,luatname) \
755 static int \
Bram Moolenaar4eefe472019-03-19 21:59:19 +0100756 luaV_##tname##_tostring(lua_State *L) \
Bram Moolenaar1dced572012-04-05 16:54:08 +0200757 { \
758 lua_pushfstring(L, "%s: %p", luatname, lua_touserdata(L, 1)); \
759 return 1; \
760 }
761
Bram Moolenaar2ab2e862019-12-04 21:24:53 +0100762// ======= List type =======
Bram Moolenaar1dced572012-04-05 16:54:08 +0200763
764 static luaV_List *
Bram Moolenaar4eefe472019-03-19 21:59:19 +0100765luaV_newlist(lua_State *L, list_T *lis)
Bram Moolenaar1dced572012-04-05 16:54:08 +0200766{
767 luaV_List *l = (luaV_List *) lua_newuserdata(L, sizeof(luaV_List));
768 *l = lis;
Bram Moolenaar2ab2e862019-12-04 21:24:53 +0100769 lis->lv_refcount++; // reference in Lua
770 luaV_setudata(L, lis); // cache[lis] = udata
Bram Moolenaar1dced572012-04-05 16:54:08 +0200771 luaV_getfield(L, LUAVIM_LIST);
772 lua_setmetatable(L, -2);
773 return l;
774}
775
776luaV_pushtype(list_T, list, luaV_List)
777luaV_type_tostring(list, LUAVIM_LIST)
778
779 static int
Bram Moolenaar4eefe472019-03-19 21:59:19 +0100780luaV_list_len(lua_State *L)
Bram Moolenaar1dced572012-04-05 16:54:08 +0200781{
782 list_T *l = luaV_unbox(L, luaV_List, 1);
Bram Moolenaarb7828692019-03-23 13:57:02 +0100783 lua_pushinteger(L, (int) list_len(l));
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200784 return 1;
785}
786
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200787 static int
Bram Moolenaar4eefe472019-03-19 21:59:19 +0100788luaV_list_iter(lua_State *L)
Bram Moolenaar1dced572012-04-05 16:54:08 +0200789{
790 listitem_T *li = (listitem_T *) lua_touserdata(L, lua_upvalueindex(2));
791 if (li == NULL) return 0;
792 luaV_pushtypval(L, &li->li_tv);
793 lua_pushlightuserdata(L, (void *) li->li_next);
794 lua_replace(L, lua_upvalueindex(2));
795 return 1;
796}
797
798 static int
Bram Moolenaar4eefe472019-03-19 21:59:19 +0100799luaV_list_call(lua_State *L)
Bram Moolenaar1dced572012-04-05 16:54:08 +0200800{
801 list_T *l = luaV_unbox(L, luaV_List, 1);
Bram Moolenaar2ab2e862019-12-04 21:24:53 +0100802 lua_pushvalue(L, lua_upvalueindex(1)); // pass cache table along
Bram Moolenaar1dced572012-04-05 16:54:08 +0200803 lua_pushlightuserdata(L, (void *) l->lv_first);
804 lua_pushcclosure(L, luaV_list_iter, 2);
805 return 1;
806}
807
808 static int
Bram Moolenaar4eefe472019-03-19 21:59:19 +0100809luaV_list_index(lua_State *L)
Bram Moolenaar1dced572012-04-05 16:54:08 +0200810{
811 list_T *l = luaV_unbox(L, luaV_List, 1);
Bram Moolenaar2ab2e862019-12-04 21:24:53 +0100812 if (lua_isnumber(L, 2)) // list item?
Bram Moolenaar1dced572012-04-05 16:54:08 +0200813 {
814 listitem_T *li = list_find(l, (long) luaL_checkinteger(L, 2));
815 if (li == NULL)
816 lua_pushnil(L);
817 else
818 luaV_pushtypval(L, &li->li_tv);
819 }
Bram Moolenaar2ab2e862019-12-04 21:24:53 +0100820 else if (lua_isstring(L, 2)) // method?
Bram Moolenaar1dced572012-04-05 16:54:08 +0200821 {
822 const char *s = lua_tostring(L, 2);
823 if (strncmp(s, "add", 3) == 0
Bram Moolenaarb3766472013-04-15 13:49:21 +0200824 || strncmp(s, "insert", 6) == 0)
Bram Moolenaar1dced572012-04-05 16:54:08 +0200825 {
826 lua_getmetatable(L, 1);
827 lua_getfield(L, -1, s);
828 }
829 else
830 lua_pushnil(L);
831 }
832 else
833 lua_pushnil(L);
834 return 1;
835}
836
837 static int
Bram Moolenaar4eefe472019-03-19 21:59:19 +0100838luaV_list_newindex(lua_State *L)
Bram Moolenaar1dced572012-04-05 16:54:08 +0200839{
840 list_T *l = luaV_unbox(L, luaV_List, 1);
841 long n = (long) luaL_checkinteger(L, 2);
842 listitem_T *li;
843 if (l->lv_lock)
844 luaL_error(L, "list is locked");
845 li = list_find(l, n);
846 if (li == NULL) return 0;
Bram Moolenaar2ab2e862019-12-04 21:24:53 +0100847 if (lua_isnil(L, 3)) // remove?
Bram Moolenaar1dced572012-04-05 16:54:08 +0200848 {
Bram Moolenaar3ec7f4e2014-05-07 17:31:37 +0200849 vimlist_remove(l, li, li);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100850 listitem_free(l, li);
Bram Moolenaar1dced572012-04-05 16:54:08 +0200851 }
852 else
853 {
854 typval_T v;
Bram Moolenaarca06da92018-07-01 15:12:05 +0200855 luaV_checktypval(L, 3, &v, "setting list item");
Bram Moolenaar1dced572012-04-05 16:54:08 +0200856 clear_tv(&li->li_tv);
857 copy_tv(&v, &li->li_tv);
Bram Moolenaarb3766472013-04-15 13:49:21 +0200858 clear_tv(&v);
Bram Moolenaar1dced572012-04-05 16:54:08 +0200859 }
860 return 0;
861}
862
863 static int
Bram Moolenaar4eefe472019-03-19 21:59:19 +0100864luaV_list_add(lua_State *L)
Bram Moolenaar1dced572012-04-05 16:54:08 +0200865{
866 luaV_List *lis = luaV_checkudata(L, 1, LUAVIM_LIST);
867 list_T *l = (list_T *) luaV_checkcache(L, (void *) *lis);
Bram Moolenaarb3766472013-04-15 13:49:21 +0200868 typval_T v;
Bram Moolenaar1dced572012-04-05 16:54:08 +0200869 if (l->lv_lock)
870 luaL_error(L, "list is locked");
Bram Moolenaarb3766472013-04-15 13:49:21 +0200871 lua_settop(L, 2);
Bram Moolenaarca06da92018-07-01 15:12:05 +0200872 luaV_checktypval(L, 2, &v, "adding list item");
Bram Moolenaarb3766472013-04-15 13:49:21 +0200873 if (list_append_tv(l, &v) == FAIL)
Bram Moolenaarca06da92018-07-01 15:12:05 +0200874 luaL_error(L, "failed to add item to list");
Bram Moolenaarb3766472013-04-15 13:49:21 +0200875 clear_tv(&v);
Bram Moolenaar1dced572012-04-05 16:54:08 +0200876 lua_settop(L, 1);
877 return 1;
878}
879
880 static int
Bram Moolenaar4eefe472019-03-19 21:59:19 +0100881luaV_list_insert(lua_State *L)
Bram Moolenaar1dced572012-04-05 16:54:08 +0200882{
883 luaV_List *lis = luaV_checkudata(L, 1, LUAVIM_LIST);
884 list_T *l = (list_T *) luaV_checkcache(L, (void *) *lis);
Bram Moolenaar46538ee2015-02-17 16:28:55 +0100885 long pos = (long) luaL_optinteger(L, 3, 0);
Bram Moolenaar1dced572012-04-05 16:54:08 +0200886 listitem_T *li = NULL;
887 typval_T v;
888 if (l->lv_lock)
889 luaL_error(L, "list is locked");
890 if (pos < l->lv_len)
891 {
892 li = list_find(l, pos);
893 if (li == NULL)
894 luaL_error(L, "invalid position");
895 }
896 lua_settop(L, 2);
Bram Moolenaarca06da92018-07-01 15:12:05 +0200897 luaV_checktypval(L, 2, &v, "inserting list item");
Bram Moolenaarb3766472013-04-15 13:49:21 +0200898 if (list_insert_tv(l, &v, li) == FAIL)
Bram Moolenaarca06da92018-07-01 15:12:05 +0200899 luaL_error(L, "failed to add item to list");
Bram Moolenaarb3766472013-04-15 13:49:21 +0200900 clear_tv(&v);
Bram Moolenaar1dced572012-04-05 16:54:08 +0200901 lua_settop(L, 1);
902 return 1;
903}
904
905static const luaL_Reg luaV_List_mt[] = {
906 {"__tostring", luaV_list_tostring},
Bram Moolenaar1dced572012-04-05 16:54:08 +0200907 {"__len", luaV_list_len},
908 {"__call", luaV_list_call},
909 {"__index", luaV_list_index},
910 {"__newindex", luaV_list_newindex},
911 {"add", luaV_list_add},
912 {"insert", luaV_list_insert},
913 {NULL, NULL}
914};
915
916
Bram Moolenaar2ab2e862019-12-04 21:24:53 +0100917// ======= Dict type =======
Bram Moolenaar1dced572012-04-05 16:54:08 +0200918
919 static luaV_Dict *
Bram Moolenaar4eefe472019-03-19 21:59:19 +0100920luaV_newdict(lua_State *L, dict_T *dic)
Bram Moolenaar1dced572012-04-05 16:54:08 +0200921{
922 luaV_Dict *d = (luaV_Dict *) lua_newuserdata(L, sizeof(luaV_Dict));
923 *d = dic;
Bram Moolenaar2ab2e862019-12-04 21:24:53 +0100924 dic->dv_refcount++; // reference in Lua
925 luaV_setudata(L, dic); // cache[dic] = udata
Bram Moolenaar1dced572012-04-05 16:54:08 +0200926 luaV_getfield(L, LUAVIM_DICT);
927 lua_setmetatable(L, -2);
928 return d;
929}
930
931luaV_pushtype(dict_T, dict, luaV_Dict)
932luaV_type_tostring(dict, LUAVIM_DICT)
933
934 static int
Bram Moolenaar4eefe472019-03-19 21:59:19 +0100935luaV_dict_len(lua_State *L)
Bram Moolenaar1dced572012-04-05 16:54:08 +0200936{
937 dict_T *d = luaV_unbox(L, luaV_Dict, 1);
Bram Moolenaarb7828692019-03-23 13:57:02 +0100938 lua_pushinteger(L, (int) dict_len(d));
Bram Moolenaar1dced572012-04-05 16:54:08 +0200939 return 1;
940}
941
942 static int
Bram Moolenaar4eefe472019-03-19 21:59:19 +0100943luaV_dict_iter(lua_State *L UNUSED)
Bram Moolenaar1dced572012-04-05 16:54:08 +0200944{
Bram Moolenaarfeeaa682013-02-14 22:19:51 +0100945#ifdef FEAT_EVAL
Bram Moolenaar1dced572012-04-05 16:54:08 +0200946 hashitem_T *hi = (hashitem_T *) lua_touserdata(L, lua_upvalueindex(2));
947 int n = lua_tointeger(L, lua_upvalueindex(3));
948 dictitem_T *di;
949 if (n <= 0) return 0;
950 while (HASHITEM_EMPTY(hi)) hi++;
951 di = dict_lookup(hi);
952 lua_pushstring(L, (char *) hi->hi_key);
953 luaV_pushtypval(L, &di->di_tv);
954 lua_pushlightuserdata(L, (void *) (hi + 1));
955 lua_replace(L, lua_upvalueindex(2));
956 lua_pushinteger(L, n - 1);
957 lua_replace(L, lua_upvalueindex(3));
958 return 2;
Bram Moolenaarfeeaa682013-02-14 22:19:51 +0100959#else
960 return 0;
961#endif
Bram Moolenaar1dced572012-04-05 16:54:08 +0200962}
963
964 static int
Bram Moolenaar4eefe472019-03-19 21:59:19 +0100965luaV_dict_call(lua_State *L)
Bram Moolenaar1dced572012-04-05 16:54:08 +0200966{
967 dict_T *d = luaV_unbox(L, luaV_Dict, 1);
968 hashtab_T *ht = &d->dv_hashtab;
Bram Moolenaar2ab2e862019-12-04 21:24:53 +0100969 lua_pushvalue(L, lua_upvalueindex(1)); // pass cache table along
Bram Moolenaar1dced572012-04-05 16:54:08 +0200970 lua_pushlightuserdata(L, (void *) ht->ht_array);
Bram Moolenaar2ab2e862019-12-04 21:24:53 +0100971 lua_pushinteger(L, ht->ht_used); // # remaining items
Bram Moolenaar1dced572012-04-05 16:54:08 +0200972 lua_pushcclosure(L, luaV_dict_iter, 3);
973 return 1;
974}
975
976 static int
Bram Moolenaarca06da92018-07-01 15:12:05 +0200977luaV_dict_index(lua_State *L)
Bram Moolenaar1dced572012-04-05 16:54:08 +0200978{
979 dict_T *d = luaV_unbox(L, luaV_Dict, 1);
980 char_u *key = (char_u *) luaL_checkstring(L, 2);
981 dictitem_T *di = dict_find(d, key, -1);
Bram Moolenaarca06da92018-07-01 15:12:05 +0200982
Bram Moolenaar1dced572012-04-05 16:54:08 +0200983 if (di == NULL)
984 lua_pushnil(L);
985 else
Bram Moolenaarca06da92018-07-01 15:12:05 +0200986 {
Bram Moolenaar1dced572012-04-05 16:54:08 +0200987 luaV_pushtypval(L, &di->di_tv);
Bram Moolenaar2ab2e862019-12-04 21:24:53 +0100988 if (di->di_tv.v_type == VAR_FUNC) // funcref?
Bram Moolenaarca06da92018-07-01 15:12:05 +0200989 {
990 luaV_Funcref *f = (luaV_Funcref *) lua_touserdata(L, -1);
Bram Moolenaar2ab2e862019-12-04 21:24:53 +0100991 f->self = d; // keep "self" reference
Bram Moolenaarca06da92018-07-01 15:12:05 +0200992 d->dv_refcount++;
993 }
994 }
Bram Moolenaar1dced572012-04-05 16:54:08 +0200995 return 1;
996}
997
998 static int
Bram Moolenaarca06da92018-07-01 15:12:05 +0200999luaV_dict_newindex(lua_State *L)
Bram Moolenaar1dced572012-04-05 16:54:08 +02001000{
1001 dict_T *d = luaV_unbox(L, luaV_Dict, 1);
1002 char_u *key = (char_u *) luaL_checkstring(L, 2);
1003 dictitem_T *di;
Bram Moolenaarca06da92018-07-01 15:12:05 +02001004 typval_T v;
Bram Moolenaar713bf9e92019-03-16 16:38:41 +01001005
Bram Moolenaar1dced572012-04-05 16:54:08 +02001006 if (d->dv_lock)
1007 luaL_error(L, "dict is locked");
Bram Moolenaard6ef5f92018-07-13 22:08:23 +02001008 if (key == NULL)
1009 return 0;
1010 if (*key == NUL)
Bram Moolenaarca06da92018-07-01 15:12:05 +02001011 luaL_error(L, "empty key");
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001012 if (!lua_isnil(L, 3)) // read value?
Bram Moolenaar17413672018-07-14 20:49:42 +02001013 {
Bram Moolenaarca06da92018-07-01 15:12:05 +02001014 luaV_checktypval(L, 3, &v, "setting dict item");
1015 if (d->dv_scope == VAR_DEF_SCOPE && v.v_type == VAR_FUNC)
1016 luaL_error(L, "cannot assign funcref to builtin scope");
1017 }
Bram Moolenaar1dced572012-04-05 16:54:08 +02001018 di = dict_find(d, key, -1);
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001019 if (di == NULL) // non-existing key?
Bram Moolenaar1dced572012-04-05 16:54:08 +02001020 {
Bram Moolenaard6ef5f92018-07-13 22:08:23 +02001021 if (lua_isnil(L, 3))
1022 return 0;
Bram Moolenaar1dced572012-04-05 16:54:08 +02001023 di = dictitem_alloc(key);
Bram Moolenaard6ef5f92018-07-13 22:08:23 +02001024 if (di == NULL)
1025 return 0;
Bram Moolenaar1dced572012-04-05 16:54:08 +02001026 if (dict_add(d, di) == FAIL)
1027 {
Bram Moolenaard6ef5f92018-07-13 22:08:23 +02001028 vim_free(di);
1029 return 0;
Bram Moolenaar1dced572012-04-05 16:54:08 +02001030 }
1031 }
1032 else
1033 clear_tv(&di->di_tv);
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001034 if (lua_isnil(L, 3)) // remove?
Bram Moolenaar1dced572012-04-05 16:54:08 +02001035 {
1036 hashitem_T *hi = hash_find(&d->dv_hashtab, di->di_key);
1037 hash_remove(&d->dv_hashtab, hi);
1038 dictitem_free(di);
1039 }
Bram Moolenaarca06da92018-07-01 15:12:05 +02001040 else
1041 {
Bram Moolenaar1dced572012-04-05 16:54:08 +02001042 copy_tv(&v, &di->di_tv);
Bram Moolenaarb3766472013-04-15 13:49:21 +02001043 clear_tv(&v);
Bram Moolenaar1dced572012-04-05 16:54:08 +02001044 }
1045 return 0;
1046}
1047
1048static const luaL_Reg luaV_Dict_mt[] = {
1049 {"__tostring", luaV_dict_tostring},
Bram Moolenaar1dced572012-04-05 16:54:08 +02001050 {"__len", luaV_dict_len},
1051 {"__call", luaV_dict_call},
1052 {"__index", luaV_dict_index},
1053 {"__newindex", luaV_dict_newindex},
1054 {NULL, NULL}
1055};
1056
1057
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001058// ======= Blob type =======
Bram Moolenaarb7828692019-03-23 13:57:02 +01001059
1060 static luaV_Blob *
1061luaV_newblob(lua_State *L, blob_T *blo)
1062{
1063 luaV_Blob *b = (luaV_Blob *) lua_newuserdata(L, sizeof(luaV_Blob));
1064 *b = blo;
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001065 blo->bv_refcount++; // reference in Lua
1066 luaV_setudata(L, blo); // cache[blo] = udata
Bram Moolenaarb7828692019-03-23 13:57:02 +01001067 luaV_getfield(L, LUAVIM_BLOB);
1068 lua_setmetatable(L, -2);
1069 return b;
1070}
1071
1072luaV_pushtype(blob_T, blob, luaV_Blob)
1073luaV_type_tostring(blob, LUAVIM_BLOB)
1074
1075 static int
1076luaV_blob_gc(lua_State *L)
1077{
1078 blob_T *b = luaV_unbox(L, luaV_Blob, 1);
1079 blob_unref(b);
1080 return 0;
1081}
1082
1083 static int
1084luaV_blob_len(lua_State *L)
1085{
1086 blob_T *b = luaV_unbox(L, luaV_Blob, 1);
1087 lua_pushinteger(L, (int) blob_len(b));
1088 return 1;
1089}
1090
1091 static int
1092luaV_blob_index(lua_State *L)
1093{
1094 blob_T *b = luaV_unbox(L, luaV_Blob, 1);
1095 if (lua_isnumber(L, 2))
1096 {
1097 int idx = luaL_checkinteger(L, 2);
1098 if (idx < blob_len(b))
1099 lua_pushnumber(L, (lua_Number) blob_get(b, idx));
1100 else
1101 lua_pushnil(L);
1102 }
1103 else if (lua_isstring(L, 2))
1104 {
1105 const char *s = lua_tostring(L, 2);
1106 if (strncmp(s, "add", 3) == 0)
1107 {
1108 lua_getmetatable(L, 1);
1109 lua_getfield(L, -1, s);
1110 }
1111 else
1112 lua_pushnil(L);
1113 }
1114 else
1115 lua_pushnil(L);
1116 return 1;
1117}
1118
1119 static int
1120luaV_blob_newindex(lua_State *L)
1121{
1122 blob_T *b = luaV_unbox(L, luaV_Blob, 1);
1123 if (b->bv_lock)
1124 luaL_error(L, "blob is locked");
1125 if (lua_isnumber(L, 2))
1126 {
1127 long len = blob_len(b);
1128 int idx = luaL_checkinteger(L, 2);
1129 int val = luaL_checkinteger(L, 3);
1130 if (idx < len || (idx == len && ga_grow(&b->bv_ga, 1) == OK))
1131 {
1132 blob_set(b, idx, (char_u) val);
1133 if (idx == len)
1134 ++b->bv_ga.ga_len;
1135 }
1136 else
1137 luaL_error(L, "index out of range");
1138 }
1139 return 0;
1140}
1141
1142 static int
1143luaV_blob_add(lua_State *L)
1144{
1145 luaV_Blob *blo = luaV_checkudata(L, 1, LUAVIM_BLOB);
1146 blob_T *b = (blob_T *) luaV_checkcache(L, (void *) *blo);
1147 if (b->bv_lock)
1148 luaL_error(L, "blob is locked");
1149 lua_settop(L, 2);
1150 if (!lua_isstring(L, 2))
1151 luaL_error(L, "string expected, got %s", luaL_typename(L, 2));
1152 else
1153 {
1154 size_t i, l = 0;
1155 const char *s = lua_tolstring(L, 2, &l);
1156
Bram Moolenaar5f1d3ae2020-02-11 22:37:35 +01001157 if (ga_grow(&b->bv_ga, (int)l) == OK)
Bram Moolenaar6fb5c972019-03-26 21:44:20 +01001158 for (i = 0; i < l; ++i)
1159 ga_append(&b->bv_ga, s[i]);
Bram Moolenaarb7828692019-03-23 13:57:02 +01001160 }
1161 lua_settop(L, 1);
1162 return 1;
1163}
1164
1165static const luaL_Reg luaV_Blob_mt[] = {
1166 {"__tostring", luaV_blob_tostring},
1167 {"__gc", luaV_blob_gc},
1168 {"__len", luaV_blob_len},
1169 {"__index", luaV_blob_index},
1170 {"__newindex", luaV_blob_newindex},
1171 {"add", luaV_blob_add},
1172 {NULL, NULL}
1173};
1174
1175
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001176// ======= Funcref type =======
Bram Moolenaarca06da92018-07-01 15:12:05 +02001177
1178 static luaV_Funcref *
1179luaV_newfuncref(lua_State *L, char_u *name)
1180{
1181 luaV_Funcref *f = (luaV_Funcref *)lua_newuserdata(L, sizeof(luaV_Funcref));
1182
1183 if (name != NULL)
1184 {
Bram Moolenaar4eefe472019-03-19 21:59:19 +01001185 func_ref(name);
1186 f->name = vim_strsave(name);
Bram Moolenaarca06da92018-07-01 15:12:05 +02001187 }
Bram Moolenaarca06da92018-07-01 15:12:05 +02001188 f->self = NULL;
1189 luaV_getfield(L, LUAVIM_FUNCREF);
1190 lua_setmetatable(L, -2);
1191 return f;
1192}
1193
1194 static luaV_Funcref *
Bram Moolenaar4eefe472019-03-19 21:59:19 +01001195luaV_pushfuncref(lua_State *L, char_u *name)
Bram Moolenaarca06da92018-07-01 15:12:05 +02001196{
Bram Moolenaar4eefe472019-03-19 21:59:19 +01001197 return luaV_newfuncref(L, name);
Bram Moolenaarca06da92018-07-01 15:12:05 +02001198}
1199
1200
1201luaV_type_tostring(funcref, LUAVIM_FUNCREF)
1202
1203 static int
1204luaV_funcref_gc(lua_State *L)
1205{
1206 luaV_Funcref *f = (luaV_Funcref *) lua_touserdata(L, 1);
1207
Bram Moolenaar4eefe472019-03-19 21:59:19 +01001208 func_unref(f->name);
1209 vim_free(f->name);
1210 // NOTE: Don't call "dict_unref(f->self)", because the dict of "f->self"
1211 // will be (or has been already) freed by Vim's garbage collection.
Bram Moolenaarca06da92018-07-01 15:12:05 +02001212 return 0;
1213}
1214
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001215// equivalent to string(funcref)
Bram Moolenaarca06da92018-07-01 15:12:05 +02001216 static int
1217luaV_funcref_len(lua_State *L)
1218{
1219 luaV_Funcref *f = (luaV_Funcref *) lua_touserdata(L, 1);
1220
Bram Moolenaar4eefe472019-03-19 21:59:19 +01001221 lua_pushstring(L, (const char *) f->name);
Bram Moolenaarca06da92018-07-01 15:12:05 +02001222 return 1;
1223}
1224
1225 static int
1226luaV_funcref_call(lua_State *L)
1227{
1228 luaV_Funcref *f = (luaV_Funcref *) lua_touserdata(L, 1);
Bram Moolenaar4eefe472019-03-19 21:59:19 +01001229 int i, n = lua_gettop(L) - 1; // #args
1230 int status = FAIL;
1231 typval_T args;
1232 typval_T rettv;
Bram Moolenaarca06da92018-07-01 15:12:05 +02001233
Bram Moolenaar4eefe472019-03-19 21:59:19 +01001234 args.v_type = VAR_LIST;
1235 args.vval.v_list = list_alloc();
1236 rettv.v_type = VAR_UNKNOWN; // as in clear_tv
1237 if (args.vval.v_list != NULL)
Bram Moolenaard6ef5f92018-07-13 22:08:23 +02001238 {
Bram Moolenaar4eefe472019-03-19 21:59:19 +01001239 typval_T v;
1240
Bram Moolenaar17413672018-07-14 20:49:42 +02001241 for (i = 0; i < n; i++)
1242 {
Bram Moolenaard6ef5f92018-07-13 22:08:23 +02001243 luaV_checktypval(L, i + 2, &v, "calling funcref");
Bram Moolenaar4eefe472019-03-19 21:59:19 +01001244 list_append_tv(args.vval.v_list, &v);
Bram Moolenaar713bf9e92019-03-16 16:38:41 +01001245 clear_tv(&v);
Bram Moolenaard6ef5f92018-07-13 22:08:23 +02001246 }
Bram Moolenaar4eefe472019-03-19 21:59:19 +01001247 status = func_call(f->name, &args, NULL, f->self, &rettv);
Bram Moolenaard6ef5f92018-07-13 22:08:23 +02001248 if (status == OK)
1249 luaV_pushtypval(L, &rettv);
Bram Moolenaar4eefe472019-03-19 21:59:19 +01001250 clear_tv(&args);
Bram Moolenaard6ef5f92018-07-13 22:08:23 +02001251 clear_tv(&rettv);
Bram Moolenaarca06da92018-07-01 15:12:05 +02001252 }
Bram Moolenaarca06da92018-07-01 15:12:05 +02001253 if (status != OK)
1254 luaL_error(L, "cannot call funcref");
1255 return 1;
1256}
1257
1258static const luaL_Reg luaV_Funcref_mt[] = {
1259 {"__tostring", luaV_funcref_tostring},
1260 {"__gc", luaV_funcref_gc},
1261 {"__len", luaV_funcref_len},
1262 {"__call", luaV_funcref_call},
1263 {NULL, NULL}
1264};
1265
1266
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001267// ======= Buffer type =======
Bram Moolenaar1dced572012-04-05 16:54:08 +02001268
1269luaV_newtype(buf_T, buffer, luaV_Buffer, LUAVIM_BUFFER)
1270luaV_pushtype(buf_T, buffer, luaV_Buffer)
1271luaV_type_tostring(buffer, LUAVIM_BUFFER)
1272
1273 static int
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001274luaV_buffer_len(lua_State *L)
1275{
Bram Moolenaar1dced572012-04-05 16:54:08 +02001276 buf_T *b = (buf_T *) luaV_checkvalid(L, luaV_Buffer, 1);
1277 lua_pushinteger(L, b->b_ml.ml_line_count);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001278 return 1;
1279}
1280
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001281 static int
1282luaV_buffer_call(lua_State *L)
1283{
Bram Moolenaar1dced572012-04-05 16:54:08 +02001284 buf_T *b = (buf_T *) luaV_checkvalid(L, luaV_Buffer, 1);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001285 lua_settop(L, 1);
Bram Moolenaar1dced572012-04-05 16:54:08 +02001286 set_curbuf(b, DOBUF_SPLIT);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001287 return 1;
1288}
1289
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001290 static int
1291luaV_buffer_index(lua_State *L)
1292{
Bram Moolenaar1dced572012-04-05 16:54:08 +02001293 buf_T *b = (buf_T *) luaV_checkvalid(L, luaV_Buffer, 1);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001294 linenr_T n = (linenr_T) lua_tointeger(L, 2);
Bram Moolenaar1dced572012-04-05 16:54:08 +02001295 if (n > 0 && n <= b->b_ml.ml_line_count)
1296 luaV_pushline(L, b, n);
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001297 else if (lua_isstring(L, 2))
1298 {
1299 const char *s = lua_tostring(L, 2);
1300 if (strncmp(s, "name", 4) == 0)
Bram Moolenaarfe08df42018-07-07 23:07:41 +02001301 lua_pushstring(L, (b->b_sfname == NULL)
1302 ? "" : (char *) b->b_sfname);
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001303 else if (strncmp(s, "fname", 5) == 0)
Bram Moolenaarfe08df42018-07-07 23:07:41 +02001304 lua_pushstring(L, (b->b_ffname == NULL)
1305 ? "" : (char *) b->b_ffname);
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001306 else if (strncmp(s, "number", 6) == 0)
Bram Moolenaar1dced572012-04-05 16:54:08 +02001307 lua_pushinteger(L, b->b_fnum);
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001308 // methods
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001309 else if (strncmp(s, "insert", 6) == 0
1310 || strncmp(s, "next", 4) == 0
1311 || strncmp(s, "previous", 8) == 0
1312 || strncmp(s, "isvalid", 7) == 0)
1313 {
1314 lua_getmetatable(L, 1);
1315 lua_getfield(L, -1, s);
1316 }
1317 else
1318 lua_pushnil(L);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001319 }
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001320 else
1321 lua_pushnil(L);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001322 return 1;
1323}
1324
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001325 static int
1326luaV_buffer_newindex(lua_State *L)
1327{
Bram Moolenaar1dced572012-04-05 16:54:08 +02001328 buf_T *b = (buf_T *) luaV_checkvalid(L, luaV_Buffer, 1);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001329 linenr_T n = (linenr_T) luaL_checkinteger(L, 2);
1330#ifdef HAVE_SANDBOX
1331 luaV_checksandbox(L);
1332#endif
Bram Moolenaar1dced572012-04-05 16:54:08 +02001333 if (n < 1 || n > b->b_ml.ml_line_count)
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001334 luaL_error(L, "invalid line number");
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001335 if (lua_isnil(L, 3)) // delete line
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001336 {
1337 buf_T *buf = curbuf;
Bram Moolenaar1dced572012-04-05 16:54:08 +02001338 curbuf = b;
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001339 if (u_savedel(n, 1L) == FAIL)
1340 {
1341 curbuf = buf;
1342 luaL_error(L, "cannot save undo information");
1343 }
Bram Moolenaarca70c072020-05-30 20:30:46 +02001344 else if (ml_delete(n) == FAIL)
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001345 {
1346 curbuf = buf;
1347 luaL_error(L, "cannot delete line");
1348 }
Bram Moolenaarca06da92018-07-01 15:12:05 +02001349 else
1350 {
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001351 deleted_lines_mark(n, 1L);
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001352 if (b == curwin->w_buffer) // fix cursor in current window?
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001353 {
1354 if (curwin->w_cursor.lnum >= n)
1355 {
1356 if (curwin->w_cursor.lnum > n)
1357 {
1358 curwin->w_cursor.lnum -= 1;
1359 check_cursor_col();
1360 }
1361 else check_cursor();
1362 changed_cline_bef_curs();
1363 }
1364 invalidate_botline();
1365 }
1366 }
1367 curbuf = buf;
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001368 }
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001369 else if (lua_isstring(L, 3)) // update line
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001370 {
1371 buf_T *buf = curbuf;
Bram Moolenaar1dced572012-04-05 16:54:08 +02001372 curbuf = b;
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001373 if (u_savesub(n) == FAIL)
1374 {
1375 curbuf = buf;
1376 luaL_error(L, "cannot save undo information");
1377 }
1378 else if (ml_replace(n, luaV_toline(L, 3), TRUE) == FAIL)
1379 {
1380 curbuf = buf;
1381 luaL_error(L, "cannot replace line");
1382 }
1383 else changed_bytes(n, 0);
1384 curbuf = buf;
Bram Moolenaar1dced572012-04-05 16:54:08 +02001385 if (b == curwin->w_buffer)
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001386 check_cursor_col();
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001387 }
1388 else
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001389 luaL_error(L, "wrong argument to change line");
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001390 return 0;
1391}
1392
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001393 static int
1394luaV_buffer_insert(lua_State *L)
1395{
Bram Moolenaar1dced572012-04-05 16:54:08 +02001396 luaV_Buffer *lb = luaV_checkudata(L, 1, LUAVIM_BUFFER);
1397 buf_T *b = (buf_T *) luaV_checkcache(L, (void *) *lb);
1398 linenr_T last = b->b_ml.ml_line_count;
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001399 linenr_T n = (linenr_T) luaL_optinteger(L, 3, last);
1400 buf_T *buf;
1401 luaL_checktype(L, 2, LUA_TSTRING);
1402#ifdef HAVE_SANDBOX
1403 luaV_checksandbox(L);
1404#endif
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001405 // fix insertion line
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001406 if (n < 0) n = 0;
1407 if (n > last) n = last;
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001408 // insert
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001409 buf = curbuf;
Bram Moolenaar1dced572012-04-05 16:54:08 +02001410 curbuf = b;
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001411 if (u_save(n, n + 1) == FAIL)
1412 {
1413 curbuf = buf;
1414 luaL_error(L, "cannot save undo information");
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001415 }
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001416 else if (ml_append(n, luaV_toline(L, 2), 0, FALSE) == FAIL)
1417 {
1418 curbuf = buf;
1419 luaL_error(L, "cannot insert line");
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001420 }
1421 else
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001422 appended_lines_mark(n, 1L);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001423 curbuf = buf;
1424 update_screen(VALID);
1425 return 0;
1426}
1427
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001428 static int
1429luaV_buffer_next(lua_State *L)
1430{
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001431 luaV_Buffer *b = luaV_checkudata(L, 1, LUAVIM_BUFFER);
Bram Moolenaar1dced572012-04-05 16:54:08 +02001432 buf_T *buf = (buf_T *) luaV_checkcache(L, (void *) *b);
1433 luaV_pushbuffer(L, buf->b_next);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001434 return 1;
1435}
1436
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001437 static int
1438luaV_buffer_previous(lua_State *L)
1439{
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001440 luaV_Buffer *b = luaV_checkudata(L, 1, LUAVIM_BUFFER);
Bram Moolenaar1dced572012-04-05 16:54:08 +02001441 buf_T *buf = (buf_T *) luaV_checkcache(L, (void *) *b);
1442 luaV_pushbuffer(L, buf->b_prev);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001443 return 1;
1444}
1445
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001446 static int
1447luaV_buffer_isvalid(lua_State *L)
1448{
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001449 luaV_Buffer *b = luaV_checkudata(L, 1, LUAVIM_BUFFER);
Bram Moolenaar1dced572012-04-05 16:54:08 +02001450 luaV_getudata(L, *b);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001451 lua_pushboolean(L, !lua_isnil(L, -1));
1452 return 1;
1453}
1454
1455static const luaL_Reg luaV_Buffer_mt[] = {
1456 {"__tostring", luaV_buffer_tostring},
1457 {"__len", luaV_buffer_len},
1458 {"__call", luaV_buffer_call},
1459 {"__index", luaV_buffer_index},
1460 {"__newindex", luaV_buffer_newindex},
1461 {"insert", luaV_buffer_insert},
1462 {"next", luaV_buffer_next},
1463 {"previous", luaV_buffer_previous},
1464 {"isvalid", luaV_buffer_isvalid},
1465 {NULL, NULL}
1466};
1467
1468
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001469// ======= Window type =======
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001470
Bram Moolenaar1dced572012-04-05 16:54:08 +02001471luaV_newtype(win_T, window, luaV_Window, LUAVIM_WINDOW)
1472luaV_pushtype(win_T, window, luaV_Window)
1473luaV_type_tostring(window, LUAVIM_WINDOW)
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001474
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001475 static int
1476luaV_window_call(lua_State *L)
1477{
Bram Moolenaar1dced572012-04-05 16:54:08 +02001478 win_T *w = (win_T *) luaV_checkvalid(L, luaV_Window, 1);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001479 lua_settop(L, 1);
Bram Moolenaar1dced572012-04-05 16:54:08 +02001480 win_goto(w);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001481 return 1;
1482}
1483
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001484 static int
1485luaV_window_index(lua_State *L)
1486{
Bram Moolenaar1dced572012-04-05 16:54:08 +02001487 win_T *w = (win_T *) luaV_checkvalid(L, luaV_Window, 1);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001488 const char *s = luaL_checkstring(L, 2);
1489 if (strncmp(s, "buffer", 6) == 0)
Bram Moolenaar1dced572012-04-05 16:54:08 +02001490 luaV_pushbuffer(L, w->w_buffer);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001491 else if (strncmp(s, "line", 4) == 0)
Bram Moolenaar1dced572012-04-05 16:54:08 +02001492 lua_pushinteger(L, w->w_cursor.lnum);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001493 else if (strncmp(s, "col", 3) == 0)
Bram Moolenaar1dced572012-04-05 16:54:08 +02001494 lua_pushinteger(L, w->w_cursor.col + 1);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001495 else if (strncmp(s, "width", 5) == 0)
Bram Moolenaar02631462017-09-22 15:20:32 +02001496 lua_pushinteger(L, w->w_width);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001497 else if (strncmp(s, "height", 6) == 0)
Bram Moolenaar1dced572012-04-05 16:54:08 +02001498 lua_pushinteger(L, w->w_height);
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001499 // methods
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001500 else if (strncmp(s, "next", 4) == 0
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001501 || strncmp(s, "previous", 8) == 0
1502 || strncmp(s, "isvalid", 7) == 0)
1503 {
1504 lua_getmetatable(L, 1);
1505 lua_getfield(L, -1, s);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001506 }
1507 else
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001508 lua_pushnil(L);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001509 return 1;
1510}
1511
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001512 static int
Bram Moolenaar4eefe472019-03-19 21:59:19 +01001513luaV_window_newindex(lua_State *L)
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001514{
Bram Moolenaar1dced572012-04-05 16:54:08 +02001515 win_T *w = (win_T *) luaV_checkvalid(L, luaV_Window, 1);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001516 const char *s = luaL_checkstring(L, 2);
1517 int v = luaL_checkinteger(L, 3);
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001518 if (strncmp(s, "line", 4) == 0)
1519 {
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001520#ifdef HAVE_SANDBOX
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001521 luaV_checksandbox(L);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001522#endif
Bram Moolenaar1dced572012-04-05 16:54:08 +02001523 if (v < 1 || v > w->w_buffer->b_ml.ml_line_count)
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001524 luaL_error(L, "line out of range");
Bram Moolenaar1dced572012-04-05 16:54:08 +02001525 w->w_cursor.lnum = v;
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001526 update_screen(VALID);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001527 }
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001528 else if (strncmp(s, "col", 3) == 0)
1529 {
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001530#ifdef HAVE_SANDBOX
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001531 luaV_checksandbox(L);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001532#endif
Bram Moolenaar1dced572012-04-05 16:54:08 +02001533 w->w_cursor.col = v - 1;
Bram Moolenaar53901442018-07-25 22:02:36 +02001534 w->w_set_curswant = TRUE;
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001535 update_screen(VALID);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001536 }
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001537 else if (strncmp(s, "width", 5) == 0)
1538 {
1539 win_T *win = curwin;
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001540#ifdef FEAT_GUI
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001541 need_mouse_correct = TRUE;
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001542#endif
Bram Moolenaar1dced572012-04-05 16:54:08 +02001543 curwin = w;
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001544 win_setwidth(v);
1545 curwin = win;
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001546 }
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001547 else if (strncmp(s, "height", 6) == 0)
1548 {
1549 win_T *win = curwin;
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001550#ifdef FEAT_GUI
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001551 need_mouse_correct = TRUE;
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001552#endif
Bram Moolenaar1dced572012-04-05 16:54:08 +02001553 curwin = w;
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001554 win_setheight(v);
1555 curwin = win;
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001556 }
1557 else
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001558 luaL_error(L, "invalid window property: `%s'", s);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001559 return 0;
1560}
1561
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001562 static int
1563luaV_window_next(lua_State *L)
1564{
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001565 luaV_Window *w = luaV_checkudata(L, 1, LUAVIM_WINDOW);
Bram Moolenaar1dced572012-04-05 16:54:08 +02001566 win_T *win = (win_T *) luaV_checkcache(L, (void *) *w);
1567 luaV_pushwindow(L, win->w_next);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001568 return 1;
1569}
1570
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001571 static int
1572luaV_window_previous(lua_State *L)
1573{
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001574 luaV_Window *w = luaV_checkudata(L, 1, LUAVIM_WINDOW);
Bram Moolenaar1dced572012-04-05 16:54:08 +02001575 win_T *win = (win_T *) luaV_checkcache(L, (void *) *w);
1576 luaV_pushwindow(L, win->w_prev);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001577 return 1;
1578}
1579
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001580 static int
1581luaV_window_isvalid(lua_State *L)
1582{
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001583 luaV_Window *w = luaV_checkudata(L, 1, LUAVIM_WINDOW);
Bram Moolenaar1dced572012-04-05 16:54:08 +02001584 luaV_getudata(L, *w);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001585 lua_pushboolean(L, !lua_isnil(L, -1));
1586 return 1;
1587}
1588
1589static const luaL_Reg luaV_Window_mt[] = {
1590 {"__tostring", luaV_window_tostring},
1591 {"__call", luaV_window_call},
1592 {"__index", luaV_window_index},
1593 {"__newindex", luaV_window_newindex},
1594 {"next", luaV_window_next},
1595 {"previous", luaV_window_previous},
1596 {"isvalid", luaV_window_isvalid},
1597 {NULL, NULL}
1598};
1599
1600
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001601// ======= Vim module =======
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001602
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001603 static int
1604luaV_print(lua_State *L)
1605{
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001606 int i, n = lua_gettop(L); // nargs
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001607 const char *s;
1608 size_t l;
1609 luaL_Buffer b;
1610 luaL_buffinit(L, &b);
1611 lua_getglobal(L, "tostring");
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001612 for (i = 1; i <= n; i++)
1613 {
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001614 lua_pushvalue(L, -1); // tostring
1615 lua_pushvalue(L, i); // arg
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001616 lua_call(L, 1, 1);
1617 s = lua_tolstring(L, -1, &l);
1618 if (s == NULL)
1619 return luaL_error(L, "cannot convert to string");
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001620 if (i > 1) luaL_addchar(&b, ' '); // use space instead of tab
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001621 luaV_addlstring(&b, s, l, 0);
1622 lua_pop(L, 1);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001623 }
1624 luaL_pushresult(&b);
Bram Moolenaarb98678a2019-10-19 15:18:44 +02001625 if (!got_int)
1626 luaV_msg(L);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001627 return 0;
1628}
1629
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001630 static int
Bram Moolenaar38e2b062011-09-21 17:15:39 +02001631luaV_debug(lua_State *L)
1632{
1633 lua_settop(L, 0);
1634 lua_getglobal(L, "vim");
1635 lua_getfield(L, -1, "eval");
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001636 lua_remove(L, -2); // vim.eval at position 1
Bram Moolenaar38e2b062011-09-21 17:15:39 +02001637 for (;;)
1638 {
1639 const char *input;
1640 size_t l;
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001641 lua_pushvalue(L, 1); // vim.eval
Bram Moolenaar38e2b062011-09-21 17:15:39 +02001642 lua_pushliteral(L, "input('lua_debug> ')");
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001643 lua_call(L, 1, 1); // return string
Bram Moolenaar38e2b062011-09-21 17:15:39 +02001644 input = lua_tolstring(L, -1, &l);
1645 if (l == 0 || strcmp(input, "cont") == 0)
1646 return 0;
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001647 msg_putchar('\n'); // avoid outputting on input line
Bram Moolenaar38e2b062011-09-21 17:15:39 +02001648 if (luaL_loadbuffer(L, input, l, "=(debug command)")
1649 || lua_pcall(L, 0, 0, 0))
1650 luaV_emsg(L);
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001651 lua_settop(L, 1); // remove eventual returns, but keep vim.eval
Bram Moolenaar38e2b062011-09-21 17:15:39 +02001652 }
1653}
1654
1655 static int
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001656luaV_command(lua_State *L)
1657{
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001658 do_cmdline_cmd((char_u *) luaL_checkstring(L, 1));
1659 update_screen(VALID);
1660 return 0;
1661}
1662
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001663 static int
1664luaV_eval(lua_State *L)
1665{
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001666 typval_T *tv = eval_expr((char_u *) luaL_checkstring(L, 1), NULL);
1667 if (tv == NULL) luaL_error(L, "invalid expression");
1668 luaV_pushtypval(L, tv);
Bram Moolenaarb3766472013-04-15 13:49:21 +02001669 free_tv(tv);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001670 return 1;
1671}
1672
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001673 static int
Bram Moolenaar0d2e4fc2010-07-18 12:35:47 +02001674luaV_beep(lua_State *L UNUSED)
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001675{
Bram Moolenaar165bc692015-07-21 17:53:25 +02001676 vim_beep(BO_LANG);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001677 return 0;
1678}
1679
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001680 static int
1681luaV_line(lua_State *L)
1682{
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001683 luaV_pushline(L, curbuf, curwin->w_cursor.lnum);
1684 return 1;
1685}
1686
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001687 static int
Bram Moolenaar1dced572012-04-05 16:54:08 +02001688luaV_list(lua_State *L)
1689{
Bram Moolenaarca06da92018-07-01 15:12:05 +02001690 list_T *l;
1691 int initarg = !lua_isnoneornil(L, 1);
1692
1693 if (initarg && lua_type(L, 1) != LUA_TTABLE)
1694 luaL_error(L, "table expected, got %s", luaL_typename(L, 1));
1695 l = list_alloc();
Bram Moolenaar1dced572012-04-05 16:54:08 +02001696 if (l == NULL)
1697 lua_pushnil(L);
1698 else
Bram Moolenaarca06da92018-07-01 15:12:05 +02001699 {
Bram Moolenaar1dced572012-04-05 16:54:08 +02001700 luaV_newlist(L, l);
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001701 if (initarg) // traverse table to init list
Bram Moolenaar17413672018-07-14 20:49:42 +02001702 {
Bram Moolenaarca06da92018-07-01 15:12:05 +02001703 int notnil, i = 0;
1704 typval_T v;
Bram Moolenaar17413672018-07-14 20:49:42 +02001705 do
1706 {
Bram Moolenaarca06da92018-07-01 15:12:05 +02001707 lua_rawgeti(L, 1, ++i);
1708 notnil = !lua_isnil(L, -1);
Bram Moolenaar17413672018-07-14 20:49:42 +02001709 if (notnil)
1710 {
Bram Moolenaarca06da92018-07-01 15:12:05 +02001711 luaV_checktypval(L, -1, &v, "vim.list");
1712 list_append_tv(l, &v);
Bram Moolenaar713bf9e92019-03-16 16:38:41 +01001713 clear_tv(&v);
Bram Moolenaarca06da92018-07-01 15:12:05 +02001714 }
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001715 lua_pop(L, 1); // value
Bram Moolenaarca06da92018-07-01 15:12:05 +02001716 } while (notnil);
1717 }
1718 }
Bram Moolenaar1dced572012-04-05 16:54:08 +02001719 return 1;
1720}
1721
1722 static int
1723luaV_dict(lua_State *L)
1724{
Bram Moolenaarca06da92018-07-01 15:12:05 +02001725 dict_T *d;
1726 int initarg = !lua_isnoneornil(L, 1);
1727
1728 if (initarg && lua_type(L, 1) != LUA_TTABLE)
1729 luaL_error(L, "table expected, got %s", luaL_typename(L, 1));
1730 d = dict_alloc();
Bram Moolenaar1dced572012-04-05 16:54:08 +02001731 if (d == NULL)
1732 lua_pushnil(L);
1733 else
Bram Moolenaarca06da92018-07-01 15:12:05 +02001734 {
Bram Moolenaar1dced572012-04-05 16:54:08 +02001735 luaV_newdict(L, d);
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001736 if (initarg) // traverse table to init dict
Bram Moolenaarca06da92018-07-01 15:12:05 +02001737 {
1738 lua_pushnil(L);
1739 while (lua_next(L, 1))
1740 {
1741 char_u *key;
1742 dictitem_T *di;
1743 typval_T v;
Bram Moolenaard6ef5f92018-07-13 22:08:23 +02001744
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001745 lua_pushvalue(L, -2); // dup key in case it's a number
Bram Moolenaarca06da92018-07-01 15:12:05 +02001746 key = (char_u *) lua_tostring(L, -1);
Bram Moolenaard6ef5f92018-07-13 22:08:23 +02001747 if (key == NULL)
1748 {
1749 lua_pushnil(L);
1750 return 1;
1751 }
1752 if (*key == NUL)
Bram Moolenaarca06da92018-07-01 15:12:05 +02001753 luaL_error(L, "table has empty key");
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001754 luaV_checktypval(L, -2, &v, "vim.dict"); // value
Bram Moolenaarca06da92018-07-01 15:12:05 +02001755 di = dictitem_alloc(key);
Bram Moolenaard6ef5f92018-07-13 22:08:23 +02001756 if (di == NULL || dict_add(d, di) == FAIL)
1757 {
Bram Moolenaarca06da92018-07-01 15:12:05 +02001758 vim_free(di);
1759 lua_pushnil(L);
1760 return 1;
1761 }
1762 copy_tv(&v, &di->di_tv);
1763 clear_tv(&v);
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001764 lua_pop(L, 2); // key copy and value
Bram Moolenaarca06da92018-07-01 15:12:05 +02001765 }
1766 }
1767 }
1768 return 1;
1769}
1770
1771 static int
Bram Moolenaarb7828692019-03-23 13:57:02 +01001772luaV_blob(lua_State *L)
1773{
1774 blob_T *b;
1775 int initarg = !lua_isnoneornil(L, 1);
1776
1777 if (initarg && !lua_isstring(L, 1))
1778 luaL_error(L, "string expected, got %s", luaL_typename(L, 1));
1779 b = blob_alloc();
1780 if (b == NULL)
1781 lua_pushnil(L);
1782 else
1783 {
1784 luaV_newblob(L, b);
1785 if (initarg)
1786 {
1787 size_t i, l = 0;
1788 const char *s = lua_tolstring(L, 1, &l);
1789
Bram Moolenaar5f1d3ae2020-02-11 22:37:35 +01001790 if (ga_grow(&b->bv_ga, (int)l) == OK)
Bram Moolenaar6fb5c972019-03-26 21:44:20 +01001791 for (i = 0; i < l; ++i)
1792 ga_append(&b->bv_ga, s[i]);
Bram Moolenaarb7828692019-03-23 13:57:02 +01001793 }
1794 }
1795 return 1;
1796}
1797
1798 static int
Bram Moolenaarca06da92018-07-01 15:12:05 +02001799luaV_funcref(lua_State *L)
1800{
1801 const char *name = luaL_checkstring(L, 1);
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001802 // note: not checking if function exists (needs function_exists)
Bram Moolenaarca06da92018-07-01 15:12:05 +02001803 if (name == NULL || *name == NUL || VIM_ISDIGIT(*name))
1804 luaL_error(L, "invalid function name: %s", name);
1805 luaV_newfuncref(L, (char_u *) name);
Bram Moolenaar1dced572012-04-05 16:54:08 +02001806 return 1;
1807}
1808
1809 static int
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001810luaV_buffer(lua_State *L)
1811{
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001812 buf_T *buf;
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001813 if (lua_isstring(L, 1)) // get by number or name?
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001814 {
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001815 if (lua_isnumber(L, 1)) // by number?
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001816 {
1817 int n = lua_tointeger(L, 1);
Bram Moolenaar29323592016-07-24 22:04:11 +02001818 FOR_ALL_BUFFERS(buf)
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001819 if (buf->b_fnum == n) break;
1820 }
Bram Moolenaarca06da92018-07-01 15:12:05 +02001821 else // by name
1822 {
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001823 size_t l;
1824 const char *s = lua_tolstring(L, 1, &l);
Bram Moolenaar29323592016-07-24 22:04:11 +02001825 FOR_ALL_BUFFERS(buf)
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001826 {
1827 if (buf->b_ffname == NULL || buf->b_sfname == NULL)
1828 {
1829 if (l == 0) break;
1830 }
Bram Moolenaar0d2e4fc2010-07-18 12:35:47 +02001831 else if (strncmp(s, (char *)buf->b_ffname, l) == 0
1832 || strncmp(s, (char *)buf->b_sfname, l) == 0)
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001833 break;
1834 }
1835 }
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001836 }
Bram Moolenaar1dced572012-04-05 16:54:08 +02001837 else
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001838 buf = (lua_toboolean(L, 1)) ? firstbuf : curbuf; // first buffer?
Bram Moolenaar1dced572012-04-05 16:54:08 +02001839 luaV_pushbuffer(L, buf);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001840 return 1;
1841}
1842
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001843 static int
1844luaV_window(lua_State *L)
1845{
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001846 win_T *win;
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001847 if (lua_isnumber(L, 1)) // get by number?
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001848 {
1849 int n = lua_tointeger(L, 1);
1850 for (win = firstwin; win != NULL; win = win->w_next, n--)
1851 if (n == 1) break;
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001852 }
Bram Moolenaar1dced572012-04-05 16:54:08 +02001853 else
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001854 win = (lua_toboolean(L, 1)) ? firstwin : curwin; // first window?
Bram Moolenaar1dced572012-04-05 16:54:08 +02001855 luaV_pushwindow(L, win);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001856 return 1;
1857}
1858
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001859 static int
1860luaV_open(lua_State *L)
1861{
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001862 char_u *s = NULL;
1863#ifdef HAVE_SANDBOX
1864 luaV_checksandbox(L);
1865#endif
1866 if (lua_isstring(L, 1)) s = (char_u *) lua_tostring(L, 1);
Bram Moolenaarfa263a52011-12-08 16:00:16 +01001867 luaV_pushbuffer(L, buflist_new(s, NULL, 1L, BLN_LISTED));
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001868 return 1;
1869}
1870
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001871 static int
Bram Moolenaar1dced572012-04-05 16:54:08 +02001872luaV_type(lua_State *L)
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001873{
Bram Moolenaar1dced572012-04-05 16:54:08 +02001874 luaL_checkany(L, 1);
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001875 if (lua_type(L, 1) == LUA_TUSERDATA) // check vim udata?
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001876 {
Bram Moolenaar1dced572012-04-05 16:54:08 +02001877 lua_settop(L, 1);
1878 if (lua_getmetatable(L, 1))
1879 {
1880 luaV_getfield(L, LUAVIM_LIST);
1881 if (lua_rawequal(L, -1, 2))
1882 {
1883 lua_pushstring(L, "list");
1884 return 1;
1885 }
1886 luaV_getfield(L, LUAVIM_DICT);
1887 if (lua_rawequal(L, -1, 2))
1888 {
1889 lua_pushstring(L, "dict");
1890 return 1;
1891 }
Bram Moolenaarb7828692019-03-23 13:57:02 +01001892 luaV_getfield(L, LUAVIM_BLOB);
1893 if (lua_rawequal(L, -1, 2))
1894 {
1895 lua_pushstring(L, "blob");
1896 return 1;
1897 }
Bram Moolenaarca06da92018-07-01 15:12:05 +02001898 luaV_getfield(L, LUAVIM_FUNCREF);
1899 if (lua_rawequal(L, -1, 2))
1900 {
1901 lua_pushstring(L, "funcref");
1902 return 1;
1903 }
Bram Moolenaar1dced572012-04-05 16:54:08 +02001904 luaV_getfield(L, LUAVIM_BUFFER);
1905 if (lua_rawequal(L, -1, 2))
1906 {
1907 lua_pushstring(L, "buffer");
1908 return 1;
1909 }
1910 luaV_getfield(L, LUAVIM_WINDOW);
1911 if (lua_rawequal(L, -1, 2))
1912 {
1913 lua_pushstring(L, "window");
1914 return 1;
1915 }
1916 }
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001917 }
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001918 lua_pushstring(L, luaL_typename(L, 1)); // fallback
Bram Moolenaar1dced572012-04-05 16:54:08 +02001919 return 1;
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001920}
1921
Bram Moolenaareb04f082020-05-17 14:32:35 +02001922 static int
1923luaV_call(lua_State *L)
1924{
1925 int argc = lua_gettop(L) - 1;
1926 size_t funcname_len;
1927 char_u *funcname;
1928 char *error = NULL;
1929 typval_T rettv;
1930 typval_T argv[MAX_FUNC_ARGS + 1];
1931 int i = 0;
1932
1933 if (argc > MAX_FUNC_ARGS)
1934 return luaL_error(L, "Function called with too many arguments");
1935
1936 funcname = (char_u *)luaL_checklstring(L, 1, &funcname_len);
1937
1938 for (; i < argc; i++)
1939 {
1940 if (luaV_totypval(L, i + 2, &argv[i]) == FAIL)
1941 {
1942 error = "lua: cannot convert value";
1943 goto free_vim_args;
1944 }
1945 }
1946
1947 argv[argc].v_type = VAR_UNKNOWN;
1948
1949 if (call_vim_function(funcname, argc, argv, &rettv) == FAIL)
1950 {
1951 error = "lua: call_vim_function failed";
1952 goto free_vim_args;
1953 }
1954
1955 luaV_pushtypval(L, &rettv);
1956 clear_tv(&rettv);
1957
1958free_vim_args:
1959 while (i > 0)
1960 clear_tv(&argv[--i]);
1961
1962 if (error == NULL)
1963 return 1;
1964 else
1965 return luaL_error(L, error);
1966}
1967
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001968static const luaL_Reg luaV_module[] = {
1969 {"command", luaV_command},
1970 {"eval", luaV_eval},
1971 {"beep", luaV_beep},
1972 {"line", luaV_line},
Bram Moolenaar1dced572012-04-05 16:54:08 +02001973 {"list", luaV_list},
1974 {"dict", luaV_dict},
Bram Moolenaarb7828692019-03-23 13:57:02 +01001975 {"blob", luaV_blob},
Bram Moolenaarca06da92018-07-01 15:12:05 +02001976 {"funcref", luaV_funcref},
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001977 {"buffer", luaV_buffer},
1978 {"window", luaV_window},
1979 {"open", luaV_open},
Bram Moolenaar1dced572012-04-05 16:54:08 +02001980 {"type", luaV_type},
Bram Moolenaareb04f082020-05-17 14:32:35 +02001981 {"call", luaV_call},
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001982 {NULL, NULL}
1983};
1984
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001985/*
1986 * for freeing list, dict, buffer and window objects; lightuserdata as arg
1987 */
Bram Moolenaar1dced572012-04-05 16:54:08 +02001988 static int
1989luaV_free(lua_State *L)
1990{
1991 lua_pushnil(L);
1992 luaV_setudata(L, lua_touserdata(L, 1));
1993 return 0;
1994}
1995
1996 static int
Bram Moolenaar4eefe472019-03-19 21:59:19 +01001997luaV_luaeval(lua_State *L)
Bram Moolenaar1dced572012-04-05 16:54:08 +02001998{
1999 luaL_Buffer b;
2000 size_t l;
2001 const char *str = lua_tolstring(L, 1, &l);
2002 typval_T *arg = (typval_T *) lua_touserdata(L, 2);
2003 typval_T *rettv = (typval_T *) lua_touserdata(L, 3);
2004 luaL_buffinit(L, &b);
2005 luaL_addlstring(&b, LUAVIM_EVALHEADER, sizeof(LUAVIM_EVALHEADER) - 1);
2006 luaL_addlstring(&b, str, l);
2007 luaL_pushresult(&b);
2008 str = lua_tolstring(L, -1, &l);
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002009 if (luaL_loadbuffer(L, str, l, LUAVIM_EVALNAME)) // compile error?
Bram Moolenaar1dced572012-04-05 16:54:08 +02002010 {
2011 luaV_emsg(L);
2012 return 0;
2013 }
2014 luaV_pushtypval(L, arg);
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002015 if (lua_pcall(L, 1, 1, 0)) // running error?
Bram Moolenaar1dced572012-04-05 16:54:08 +02002016 {
2017 luaV_emsg(L);
2018 return 0;
2019 }
Bram Moolenaarca06da92018-07-01 15:12:05 +02002020 if (luaV_totypval(L, -1, rettv) == FAIL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002021 emsg("luaeval: cannot convert value");
Bram Moolenaarf554a322015-02-04 23:08:01 +01002022 return 0;
Bram Moolenaar1dced572012-04-05 16:54:08 +02002023}
2024
2025 static int
Bram Moolenaar4eefe472019-03-19 21:59:19 +01002026luaV_setref(lua_State *L)
Bram Moolenaar1dced572012-04-05 16:54:08 +02002027{
Bram Moolenaar4eefe472019-03-19 21:59:19 +01002028 int copyID = lua_tointeger(L, 1);
2029 int abort = FALSE;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01002030
Bram Moolenaar1dced572012-04-05 16:54:08 +02002031 luaV_getfield(L, LUAVIM_LIST);
2032 luaV_getfield(L, LUAVIM_DICT);
Bram Moolenaar4eefe472019-03-19 21:59:19 +01002033 luaV_getfield(L, LUAVIM_FUNCREF);
Bram Moolenaar1dced572012-04-05 16:54:08 +02002034 lua_pushnil(L);
Bram Moolenaar4eefe472019-03-19 21:59:19 +01002035 // traverse cache table
Bram Moolenaarb84634d2015-02-04 22:02:37 +01002036 while (!abort && lua_next(L, lua_upvalueindex(1)) != 0)
Bram Moolenaar1dced572012-04-05 16:54:08 +02002037 {
2038 lua_getmetatable(L, -1);
Bram Moolenaar4eefe472019-03-19 21:59:19 +01002039 if (lua_rawequal(L, -1, 2)) // list?
Bram Moolenaar1dced572012-04-05 16:54:08 +02002040 {
Bram Moolenaar4eefe472019-03-19 21:59:19 +01002041 list_T *l = (list_T *)lua_touserdata(L, 5); // key
2042
Bram Moolenaar7be3ab22019-06-23 01:46:15 +02002043 abort = set_ref_in_list(l, copyID);
Bram Moolenaar1dced572012-04-05 16:54:08 +02002044 }
Bram Moolenaar4eefe472019-03-19 21:59:19 +01002045 else if (lua_rawequal(L, -1, 3)) // dict?
Bram Moolenaar1dced572012-04-05 16:54:08 +02002046 {
Bram Moolenaar4eefe472019-03-19 21:59:19 +01002047 dict_T *d = (dict_T *)lua_touserdata(L, 5); // key
2048
Bram Moolenaar7be3ab22019-06-23 01:46:15 +02002049 abort = set_ref_in_dict(d, copyID);
Bram Moolenaar1dced572012-04-05 16:54:08 +02002050 }
Bram Moolenaar4eefe472019-03-19 21:59:19 +01002051 else if (lua_rawequal(L, -1, 4)) // funcref?
2052 {
2053 luaV_Funcref *f = (luaV_Funcref *)lua_touserdata(L, 5); // key
2054
Bram Moolenaar7be3ab22019-06-23 01:46:15 +02002055 abort = set_ref_in_dict(f->self, copyID);
Bram Moolenaar4eefe472019-03-19 21:59:19 +01002056 }
2057 lua_pop(L, 2); // metatable and value
Bram Moolenaar1dced572012-04-05 16:54:08 +02002058 }
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01002059 lua_pushinteger(L, abort);
Bram Moolenaarf554a322015-02-04 23:08:01 +01002060 return 1;
Bram Moolenaar1dced572012-04-05 16:54:08 +02002061}
2062
Bram Moolenaareb04f082020-05-17 14:32:35 +02002063#define LUA_VIM_FN_CODE \
Bram Moolenaar788fbb42020-05-31 14:08:12 +02002064 "vim.fn = setmetatable({}, {\n"\
2065 " __index = function (t, key)\n"\
2066 " local function _fn(...)\n"\
2067 " return vim.call(key, ...)\n"\
2068 " end\n"\
2069 " t[key] = _fn\n"\
2070 " return _fn\n"\
2071 " end\n"\
2072 " })"
2073
2074#define LUA_VIM_UPDATE_PACKAGE_PATHS \
2075 "local last_vim_paths = {}\n"\
2076 "vim._update_package_paths = function ()\n"\
2077 " local cur_vim_paths = {}\n"\
2078 " local function split(s, delimiter)\n"\
2079 " result = {}\n"\
2080 " for match in (s..delimiter):gmatch(\"(.-)\"..delimiter) do\n"\
2081 " table.insert(result, match)\n"\
2082 " end\n"\
2083 " return result\n"\
2084 " end\n"\
2085 " local rtps = split(vim.eval('&runtimepath'), ',')\n"\
2086 " local sep = package.config:sub(1, 1)\n"\
2087 " for _, key in ipairs({'path', 'cpath'}) do\n"\
2088 " local orig_str = package[key] .. ';'\n"\
2089 " local pathtrails_ordered = {}\n"\
2090 " -- Note: ignores trailing item without trailing `;`. Not using something\n"\
2091 " -- simpler in order to preserve empty items (stand for default path).\n"\
2092 " local orig = {}\n"\
2093 " for s in orig_str:gmatch('[^;]*;') do\n"\
2094 " s = s:sub(1, -2) -- Strip trailing semicolon\n"\
2095 " orig[#orig + 1] = s\n"\
2096 " end\n"\
2097 " if key == 'path' then\n"\
2098 " -- /?.lua and /?/init.lua\n"\
2099 " pathtrails_ordered = {sep .. '?.lua', sep .. '?' .. sep .. 'init.lua'}\n"\
2100 " else\n"\
2101 " local pathtrails = {}\n"\
2102 " for _, s in ipairs(orig) do\n"\
2103 " -- Find out path patterns. pathtrail should contain something like\n"\
2104 " -- /?.so, \?.dll. This allows not to bother determining what correct\n"\
2105 " -- suffixes are.\n"\
2106 " local pathtrail = s:match('[/\\\\][^/\\\\]*%?.*$')\n"\
2107 " if pathtrail and not pathtrails[pathtrail] then\n"\
2108 " pathtrails[pathtrail] = true\n"\
2109 " pathtrails_ordered[#pathtrails_ordered + 1] = pathtrail\n"\
2110 " end\n"\
2111 " end\n"\
2112 " end\n"\
2113 " local new = {}\n"\
2114 " for _, rtp in ipairs(rtps) do\n"\
2115 " if not rtp:match(';') then\n"\
2116 " for _, pathtrail in pairs(pathtrails_ordered) do\n"\
2117 " local new_path = rtp .. sep .. 'lua' .. pathtrail\n"\
2118 " -- Always keep paths from &runtimepath at the start:\n"\
2119 " -- append them here disregarding orig possibly containing one of them.\n"\
2120 " new[#new + 1] = new_path\n"\
2121 " cur_vim_paths[new_path] = true\n"\
2122 " end\n"\
2123 " end\n"\
2124 " end\n"\
2125 " for _, orig_path in ipairs(orig) do\n"\
2126 " -- Handle removing obsolete paths originating from &runtimepath: such\n"\
2127 " -- paths either belong to cur_nvim_paths and were already added above or\n"\
2128 " -- to last_nvim_paths and should not be added at all if corresponding\n"\
2129 " -- entry was removed from &runtimepath list.\n"\
2130 " if not (cur_vim_paths[orig_path] or last_vim_paths[orig_path]) then\n"\
2131 " new[#new + 1] = orig_path\n"\
2132 " end\n"\
2133 " end\n"\
2134 " package[key] = table.concat(new, ';')\n"\
2135 " end\n"\
2136 " last_vim_paths = cur_vim_paths\n"\
2137 "end"
Bram Moolenaareb04f082020-05-17 14:32:35 +02002138
Bram Moolenaar55d5c032010-07-17 23:52:29 +02002139 static int
2140luaopen_vim(lua_State *L)
2141{
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002142 // set cache table
Bram Moolenaar0ba04292010-07-14 23:23:17 +02002143 lua_newtable(L);
2144 lua_newtable(L);
Bram Moolenaar1dced572012-04-05 16:54:08 +02002145 lua_pushstring(L, "v");
Bram Moolenaar0ba04292010-07-14 23:23:17 +02002146 lua_setfield(L, -2, "__mode");
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002147 lua_setmetatable(L, -2); // cache is weak-valued
2148 // print
Bram Moolenaar0ba04292010-07-14 23:23:17 +02002149 lua_pushcfunction(L, luaV_print);
2150 lua_setglobal(L, "print");
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002151 // debug.debug
Bram Moolenaar38e2b062011-09-21 17:15:39 +02002152 lua_getglobal(L, "debug");
2153 lua_pushcfunction(L, luaV_debug);
2154 lua_setfield(L, -2, "debug");
2155 lua_pop(L, 1);
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002156 // free
Bram Moolenaar0ba04292010-07-14 23:23:17 +02002157 lua_pushlightuserdata(L, (void *) LUAVIM_FREE);
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002158 lua_pushvalue(L, 1); // cache table
Bram Moolenaar1dced572012-04-05 16:54:08 +02002159 lua_pushcclosure(L, luaV_free, 1);
2160 lua_rawset(L, LUA_REGISTRYINDEX);
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002161 // luaeval
Bram Moolenaar1dced572012-04-05 16:54:08 +02002162 lua_pushlightuserdata(L, (void *) LUAVIM_LUAEVAL);
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002163 lua_pushvalue(L, 1); // cache table
Bram Moolenaar1dced572012-04-05 16:54:08 +02002164 lua_pushcclosure(L, luaV_luaeval, 1);
2165 lua_rawset(L, LUA_REGISTRYINDEX);
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002166 // setref
Bram Moolenaar1dced572012-04-05 16:54:08 +02002167 lua_pushlightuserdata(L, (void *) LUAVIM_SETREF);
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002168 lua_pushvalue(L, 1); // cache table
Bram Moolenaar1dced572012-04-05 16:54:08 +02002169 lua_pushcclosure(L, luaV_setref, 1);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02002170 lua_rawset(L, LUA_REGISTRYINDEX);
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002171 // register
Bram Moolenaar1dced572012-04-05 16:54:08 +02002172 luaV_newmetatable(L, LUAVIM_LIST);
2173 lua_pushvalue(L, 1);
2174 luaV_openlib(L, luaV_List_mt, 1);
2175 luaV_newmetatable(L, LUAVIM_DICT);
2176 lua_pushvalue(L, 1);
2177 luaV_openlib(L, luaV_Dict_mt, 1);
Bram Moolenaarb7828692019-03-23 13:57:02 +01002178 luaV_newmetatable(L, LUAVIM_BLOB);
2179 lua_pushvalue(L, 1);
2180 luaV_openlib(L, luaV_Blob_mt, 1);
Bram Moolenaarca06da92018-07-01 15:12:05 +02002181 luaV_newmetatable(L, LUAVIM_FUNCREF);
2182 lua_pushvalue(L, 1);
2183 luaV_openlib(L, luaV_Funcref_mt, 1);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02002184 luaV_newmetatable(L, LUAVIM_BUFFER);
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002185 lua_pushvalue(L, 1); // cache table
Bram Moolenaar1dced572012-04-05 16:54:08 +02002186 luaV_openlib(L, luaV_Buffer_mt, 1);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02002187 luaV_newmetatable(L, LUAVIM_WINDOW);
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002188 lua_pushvalue(L, 1); // cache table
Bram Moolenaar1dced572012-04-05 16:54:08 +02002189 luaV_openlib(L, luaV_Window_mt, 1);
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002190 lua_newtable(L); // vim table
2191 lua_pushvalue(L, 1); // cache table
Bram Moolenaar1dced572012-04-05 16:54:08 +02002192 luaV_openlib(L, luaV_module, 1);
2193 lua_setglobal(L, LUAVIM_NAME);
Bram Moolenaareb04f082020-05-17 14:32:35 +02002194 // custom code
Bram Moolenaar9309eb22020-05-17 16:53:56 +02002195 (void)luaL_dostring(L, LUA_VIM_FN_CODE);
Bram Moolenaar788fbb42020-05-31 14:08:12 +02002196 (void)luaL_dostring(L, LUA_VIM_UPDATE_PACKAGE_PATHS);
2197
2198 lua_getglobal(L, "vim");
2199 lua_getfield(L, -1, "_update_package_paths");
2200
2201 if (lua_pcall(L, 0, 0, 0))
2202 luaV_emsg(L);
2203
Bram Moolenaar0ba04292010-07-14 23:23:17 +02002204 return 0;
2205}
2206
Bram Moolenaar55d5c032010-07-17 23:52:29 +02002207 static lua_State *
2208luaV_newstate(void)
2209{
Bram Moolenaar0ba04292010-07-14 23:23:17 +02002210 lua_State *L = luaL_newstate();
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002211 luaL_openlibs(L); // core libs
2212 lua_pushcfunction(L, luaopen_vim); // vim
Bram Moolenaar0ba04292010-07-14 23:23:17 +02002213 lua_call(L, 0, 0);
2214 return L;
2215}
2216
Bram Moolenaar55d5c032010-07-17 23:52:29 +02002217 static void
2218luaV_setrange(lua_State *L, int line1, int line2)
2219{
Bram Moolenaar0ba04292010-07-14 23:23:17 +02002220 lua_getglobal(L, LUAVIM_NAME);
2221 lua_pushinteger(L, line1);
2222 lua_setfield(L, -2, "firstline");
2223 lua_pushinteger(L, line2);
2224 lua_setfield(L, -2, "lastline");
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002225 lua_pop(L, 1); // vim table
Bram Moolenaar0ba04292010-07-14 23:23:17 +02002226}
2227
2228
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002229// ======= Interface =======
Bram Moolenaar0ba04292010-07-14 23:23:17 +02002230
2231static lua_State *L = NULL;
2232
Bram Moolenaar55d5c032010-07-17 23:52:29 +02002233 static int
Bram Moolenaar1dced572012-04-05 16:54:08 +02002234lua_isopen(void)
Bram Moolenaar2bd6a1b2010-08-12 22:14:01 +02002235{
2236 return L != NULL;
2237}
2238
2239 static int
Bram Moolenaar55d5c032010-07-17 23:52:29 +02002240lua_init(void)
2241{
Bram Moolenaar1dced572012-04-05 16:54:08 +02002242 if (!lua_isopen())
Bram Moolenaar55d5c032010-07-17 23:52:29 +02002243 {
Bram Moolenaar0ba04292010-07-14 23:23:17 +02002244#ifdef DYNAMIC_LUA
Bram Moolenaar55d5c032010-07-17 23:52:29 +02002245 if (!lua_enabled(TRUE))
2246 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002247 emsg(_("Lua library cannot be loaded."));
Bram Moolenaar55d5c032010-07-17 23:52:29 +02002248 return FAIL;
2249 }
Bram Moolenaar0ba04292010-07-14 23:23:17 +02002250#endif
Bram Moolenaar55d5c032010-07-17 23:52:29 +02002251 L = luaV_newstate();
Bram Moolenaar0ba04292010-07-14 23:23:17 +02002252 }
2253 return OK;
2254}
2255
Bram Moolenaar55d5c032010-07-17 23:52:29 +02002256 void
2257lua_end(void)
2258{
Bram Moolenaar1dced572012-04-05 16:54:08 +02002259 if (lua_isopen())
Bram Moolenaar55d5c032010-07-17 23:52:29 +02002260 {
2261 lua_close(L);
2262 L = NULL;
Bram Moolenaar0ba04292010-07-14 23:23:17 +02002263 }
2264}
2265
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002266/*
2267 * ex commands
2268 */
Bram Moolenaar55d5c032010-07-17 23:52:29 +02002269 void
2270ex_lua(exarg_T *eap)
2271{
Bram Moolenaar0ba04292010-07-14 23:23:17 +02002272 char *script;
2273 if (lua_init() == FAIL) return;
2274 script = (char *) script_get(eap, eap->arg);
Bram Moolenaar55d5c032010-07-17 23:52:29 +02002275 if (!eap->skip)
2276 {
2277 char *s = (script) ? script : (char *) eap->arg;
2278 luaV_setrange(L, eap->line1, eap->line2);
2279 if (luaL_loadbuffer(L, s, strlen(s), LUAVIM_CHUNKNAME)
2280 || lua_pcall(L, 0, 0, 0))
2281 luaV_emsg(L);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02002282 }
2283 if (script != NULL) vim_free(script);
2284}
2285
Bram Moolenaar55d5c032010-07-17 23:52:29 +02002286 void
2287ex_luado(exarg_T *eap)
2288{
Bram Moolenaar0ba04292010-07-14 23:23:17 +02002289 linenr_T l;
2290 const char *s = (const char *) eap->arg;
2291 luaL_Buffer b;
2292 size_t len;
Bram Moolenaard58f03b2017-01-29 22:48:45 +01002293 buf_T *was_curbuf = curbuf;
2294
Bram Moolenaar0ba04292010-07-14 23:23:17 +02002295 if (lua_init() == FAIL) return;
Bram Moolenaar55d5c032010-07-17 23:52:29 +02002296 if (u_save(eap->line1 - 1, eap->line2 + 1) == FAIL)
2297 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002298 emsg(_("cannot save undo information"));
Bram Moolenaar55d5c032010-07-17 23:52:29 +02002299 return;
Bram Moolenaar0ba04292010-07-14 23:23:17 +02002300 }
2301 luaV_setrange(L, eap->line1, eap->line2);
2302 luaL_buffinit(L, &b);
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002303 luaL_addlstring(&b, "return function(line, linenr) ", 30); // header
Bram Moolenaar0ba04292010-07-14 23:23:17 +02002304 luaL_addlstring(&b, s, strlen(s));
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002305 luaL_addlstring(&b, " end", 4); // footer
Bram Moolenaar0ba04292010-07-14 23:23:17 +02002306 luaL_pushresult(&b);
2307 s = lua_tolstring(L, -1, &len);
Bram Moolenaar55d5c032010-07-17 23:52:29 +02002308 if (luaL_loadbuffer(L, s, len, LUAVIM_CHUNKNAME))
2309 {
2310 luaV_emsg(L);
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002311 lua_pop(L, 1); // function body
Bram Moolenaar55d5c032010-07-17 23:52:29 +02002312 return;
Bram Moolenaar0ba04292010-07-14 23:23:17 +02002313 }
2314 lua_call(L, 0, 1);
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002315 lua_replace(L, -2); // function -> body
Bram Moolenaar55d5c032010-07-17 23:52:29 +02002316 for (l = eap->line1; l <= eap->line2; l++)
2317 {
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002318 // Check the line number, the command my have deleted lines.
Bram Moolenaard58f03b2017-01-29 22:48:45 +01002319 if (l > curbuf->b_ml.ml_line_count)
2320 break;
2321
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002322 lua_pushvalue(L, -1); // function
2323 luaV_pushline(L, curbuf, l); // current line as arg
2324 lua_pushinteger(L, l); // current line number as arg
Bram Moolenaarbd2f3c32012-04-06 14:31:00 +02002325 if (lua_pcall(L, 2, 1, 0))
Bram Moolenaar55d5c032010-07-17 23:52:29 +02002326 {
2327 luaV_emsg(L);
2328 break;
2329 }
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002330 // Catch the command switching to another buffer.
Bram Moolenaard58f03b2017-01-29 22:48:45 +01002331 if (curbuf != was_curbuf)
2332 break;
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002333 if (lua_isstring(L, -1)) // update line?
Bram Moolenaar55d5c032010-07-17 23:52:29 +02002334 {
Bram Moolenaar0ba04292010-07-14 23:23:17 +02002335#ifdef HAVE_SANDBOX
Bram Moolenaar55d5c032010-07-17 23:52:29 +02002336 luaV_checksandbox(L);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02002337#endif
Bram Moolenaar55d5c032010-07-17 23:52:29 +02002338 ml_replace(l, luaV_toline(L, -1), TRUE);
2339 changed_bytes(l, 0);
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002340 lua_pop(L, 1); // result from luaV_toline
Bram Moolenaar55d5c032010-07-17 23:52:29 +02002341 }
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002342 lua_pop(L, 1); // line
Bram Moolenaar0ba04292010-07-14 23:23:17 +02002343 }
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002344 lua_pop(L, 1); // function
Bram Moolenaar0ba04292010-07-14 23:23:17 +02002345 check_cursor();
2346 update_screen(NOT_VALID);
2347}
2348
Bram Moolenaar55d5c032010-07-17 23:52:29 +02002349 void
2350ex_luafile(exarg_T *eap)
2351{
2352 if (lua_init() == FAIL)
2353 return;
2354 if (!eap->skip)
2355 {
2356 luaV_setrange(L, eap->line1, eap->line2);
2357 if (luaL_loadfile(L, (char *) eap->arg) || lua_pcall(L, 0, 0, 0))
2358 luaV_emsg(L);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02002359 }
2360}
2361
Bram Moolenaar1dced572012-04-05 16:54:08 +02002362#define luaV_freetype(typ,tname) \
2363 void \
2364 lua_##tname##_free(typ *o) \
2365 { \
2366 if (!lua_isopen()) return; \
2367 luaV_getfield(L, LUAVIM_FREE); \
2368 lua_pushlightuserdata(L, (void *) o); \
2369 lua_call(L, 1, 0); \
2370 }
2371
2372luaV_freetype(buf_T, buffer)
2373luaV_freetype(win_T, window)
2374
Bram Moolenaar55d5c032010-07-17 23:52:29 +02002375 void
Bram Moolenaar4eefe472019-03-19 21:59:19 +01002376do_luaeval(char_u *str, typval_T *arg, typval_T *rettv)
Bram Moolenaar55d5c032010-07-17 23:52:29 +02002377{
Bram Moolenaar1dced572012-04-05 16:54:08 +02002378 lua_init();
2379 luaV_getfield(L, LUAVIM_LUAEVAL);
2380 lua_pushstring(L, (char *) str);
2381 lua_pushlightuserdata(L, (void *) arg);
2382 lua_pushlightuserdata(L, (void *) rettv);
2383 lua_call(L, 3, 0);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02002384}
2385
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01002386 int
Bram Moolenaar4eefe472019-03-19 21:59:19 +01002387set_ref_in_lua(int copyID)
Bram Moolenaar55d5c032010-07-17 23:52:29 +02002388{
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01002389 int aborted = 0;
2390
2391 if (lua_isopen())
2392 {
2393 luaV_getfield(L, LUAVIM_SETREF);
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002394 // call the function with 1 arg, getting 1 result back
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01002395 lua_pushinteger(L, copyID);
2396 lua_call(L, 1, 1);
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002397 // get the result
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01002398 aborted = lua_tointeger(L, -1);
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002399 // pop result off the stack
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01002400 lua_pop(L, 1);
2401 }
2402 return aborted;
Bram Moolenaar0ba04292010-07-14 23:23:17 +02002403}
2404
Bram Moolenaar788fbb42020-05-31 14:08:12 +02002405 void
2406update_package_paths_in_lua()
2407{
2408 if (lua_isopen())
2409 {
2410 lua_getglobal(L, "vim");
2411 lua_getfield(L, -1, "_update_package_paths");
2412
2413 if (lua_pcall(L, 0, 0, 0))
2414 luaV_emsg(L);
2415 }
2416}
2417
Bram Moolenaar0ba04292010-07-14 23:23:17 +02002418#endif