blob: d4cc21f3d0f5c3f803a345c81ecbdf04a37dee99 [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 Moolenaar2ab2e862019-12-04 21:24:53 +0100122// lua
Bram Moolenaar1dced572012-04-05 16:54:08 +0200123#if LUA_VERSION_NUM <= 501
124#define lua_tonumber dll_lua_tonumber
125#define lua_tointeger dll_lua_tointeger
126#define lua_call dll_lua_call
127#define lua_pcall dll_lua_pcall
128#else
129#define lua_tonumberx dll_lua_tonumberx
130#define lua_tointegerx dll_lua_tointegerx
131#define lua_callk dll_lua_callk
132#define lua_pcallk dll_lua_pcallk
133#define lua_getglobal dll_lua_getglobal
134#define lua_setglobal dll_lua_setglobal
Bram Moolenaar1dced572012-04-05 16:54:08 +0200135#endif
Bram Moolenaar1f860d82015-06-27 18:36:16 +0200136#if LUA_VERSION_NUM <= 502
137#define lua_replace dll_lua_replace
138#define lua_remove dll_lua_remove
139#endif
140#if LUA_VERSION_NUM >= 503
141#define lua_rotate dll_lua_rotate
142#define lua_copy dll_lua_copy
143#endif
Bram Moolenaarbd2f3c32012-04-06 14:31:00 +0200144#define lua_typename dll_lua_typename
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200145#define lua_close dll_lua_close
146#define lua_gettop dll_lua_gettop
147#define lua_settop dll_lua_settop
148#define lua_pushvalue dll_lua_pushvalue
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200149#define lua_isnumber dll_lua_isnumber
150#define lua_isstring dll_lua_isstring
151#define lua_type dll_lua_type
152#define lua_rawequal dll_lua_rawequal
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200153#define lua_toboolean dll_lua_toboolean
154#define lua_tolstring dll_lua_tolstring
155#define lua_touserdata dll_lua_touserdata
156#define lua_pushnil dll_lua_pushnil
157#define lua_pushnumber dll_lua_pushnumber
158#define lua_pushinteger dll_lua_pushinteger
159#define lua_pushlstring dll_lua_pushlstring
160#define lua_pushstring dll_lua_pushstring
161#define lua_pushfstring dll_lua_pushfstring
162#define lua_pushcclosure dll_lua_pushcclosure
163#define lua_pushboolean dll_lua_pushboolean
164#define lua_pushlightuserdata dll_lua_pushlightuserdata
165#define lua_getfield dll_lua_getfield
166#define lua_rawget dll_lua_rawget
Bram Moolenaar1dced572012-04-05 16:54:08 +0200167#define lua_rawgeti dll_lua_rawgeti
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200168#define lua_createtable dll_lua_createtable
Bram Moolenaar830e3582018-08-21 14:23:35 +0200169#if LUA_VERSION_NUM >= 504
170 #define lua_newuserdatauv dll_lua_newuserdatauv
171#else
172 #define lua_newuserdata dll_lua_newuserdata
173#endif
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200174#define lua_getmetatable dll_lua_getmetatable
175#define lua_setfield dll_lua_setfield
176#define lua_rawset dll_lua_rawset
177#define lua_rawseti dll_lua_rawseti
178#define lua_setmetatable dll_lua_setmetatable
Bram Moolenaarbd2f3c32012-04-06 14:31:00 +0200179#define lua_next dll_lua_next
Bram Moolenaar2ab2e862019-12-04 21:24:53 +0100180// libs
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200181#define luaopen_base dll_luaopen_base
182#define luaopen_table dll_luaopen_table
183#define luaopen_string dll_luaopen_string
184#define luaopen_math dll_luaopen_math
Bram Moolenaar16c98f92010-07-28 22:46:08 +0200185#define luaopen_io dll_luaopen_io
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200186#define luaopen_os dll_luaopen_os
187#define luaopen_package dll_luaopen_package
188#define luaopen_debug dll_luaopen_debug
Bram Moolenaar16c98f92010-07-28 22:46:08 +0200189#define luaL_openlibs dll_luaL_openlibs
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200190
Bram Moolenaar2ab2e862019-12-04 21:24:53 +0100191// lauxlib
Bram Moolenaar1dced572012-04-05 16:54:08 +0200192#if LUA_VERSION_NUM <= 501
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200193void (*dll_luaL_register) (lua_State *L, const char *libname, const luaL_Reg *l);
Bram Moolenaar1dced572012-04-05 16:54:08 +0200194char *(*dll_luaL_prepbuffer) (luaL_Buffer *B);
195void (*dll_luaL_openlib) (lua_State *L, const char *libname, const luaL_Reg *l, int nup);
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200196int (*dll_luaL_typerror) (lua_State *L, int narg, const char *tname);
Bram Moolenaar1dced572012-04-05 16:54:08 +0200197int (*dll_luaL_loadfile) (lua_State *L, const char *filename);
198int (*dll_luaL_loadbuffer) (lua_State *L, const char *buff, size_t sz, const char *name);
199#else
200char *(*dll_luaL_prepbuffsize) (luaL_Buffer *B, size_t sz);
201void (*dll_luaL_setfuncs) (lua_State *L, const luaL_Reg *l, int nup);
202int (*dll_luaL_loadfilex) (lua_State *L, const char *filename, const char *mode);
203int (*dll_luaL_loadbufferx) (lua_State *L, const char *buff, size_t sz, const char *name, const char *mode);
204int (*dll_luaL_argerror) (lua_State *L, int numarg, const char *extramsg);
205#endif
Bram Moolenaarbd2f3c32012-04-06 14:31:00 +0200206void (*dll_luaL_checkany) (lua_State *L, int narg);
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200207const char *(*dll_luaL_checklstring) (lua_State *L, int numArg, size_t *l);
208lua_Integer (*dll_luaL_checkinteger) (lua_State *L, int numArg);
209lua_Integer (*dll_luaL_optinteger) (lua_State *L, int nArg, lua_Integer def);
210void (*dll_luaL_checktype) (lua_State *L, int narg, int t);
211int (*dll_luaL_error) (lua_State *L, const char *fmt, ...);
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200212lua_State *(*dll_luaL_newstate) (void);
213void (*dll_luaL_buffinit) (lua_State *L, luaL_Buffer *B);
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200214void (*dll_luaL_addlstring) (luaL_Buffer *B, const char *s, size_t l);
215void (*dll_luaL_pushresult) (luaL_Buffer *B);
Bram Moolenaar2ab2e862019-12-04 21:24:53 +0100216// lua
Bram Moolenaar1dced572012-04-05 16:54:08 +0200217#if LUA_VERSION_NUM <= 501
218lua_Number (*dll_lua_tonumber) (lua_State *L, int idx);
219lua_Integer (*dll_lua_tointeger) (lua_State *L, int idx);
220void (*dll_lua_call) (lua_State *L, int nargs, int nresults);
221int (*dll_lua_pcall) (lua_State *L, int nargs, int nresults, int errfunc);
222#else
223lua_Number (*dll_lua_tonumberx) (lua_State *L, int idx, int *isnum);
224lua_Integer (*dll_lua_tointegerx) (lua_State *L, int idx, int *isnum);
225void (*dll_lua_callk) (lua_State *L, int nargs, int nresults, int ctx,
Bram Moolenaardb913952012-06-29 12:54:53 +0200226 lua_CFunction k);
Bram Moolenaar1dced572012-04-05 16:54:08 +0200227int (*dll_lua_pcallk) (lua_State *L, int nargs, int nresults, int errfunc,
Bram Moolenaardb913952012-06-29 12:54:53 +0200228 int ctx, lua_CFunction k);
Bram Moolenaar1dced572012-04-05 16:54:08 +0200229void (*dll_lua_getglobal) (lua_State *L, const char *var);
230void (*dll_lua_setglobal) (lua_State *L, const char *var);
Bram Moolenaar1f860d82015-06-27 18:36:16 +0200231#endif
232#if LUA_VERSION_NUM <= 502
233void (*dll_lua_replace) (lua_State *L, int idx);
234void (*dll_lua_remove) (lua_State *L, int idx);
235#endif
236#if LUA_VERSION_NUM >= 503
237void (*dll_lua_rotate) (lua_State *L, int idx, int n);
Bram Moolenaar9514b1f2015-06-25 18:27:32 +0200238void (*dll_lua_copy) (lua_State *L, int fromidx, int toidx);
Bram Moolenaar1dced572012-04-05 16:54:08 +0200239#endif
Bram Moolenaarbd2f3c32012-04-06 14:31:00 +0200240const char *(*dll_lua_typename) (lua_State *L, int tp);
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200241void (*dll_lua_close) (lua_State *L);
242int (*dll_lua_gettop) (lua_State *L);
243void (*dll_lua_settop) (lua_State *L, int idx);
244void (*dll_lua_pushvalue) (lua_State *L, int idx);
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200245int (*dll_lua_isnumber) (lua_State *L, int idx);
246int (*dll_lua_isstring) (lua_State *L, int idx);
247int (*dll_lua_type) (lua_State *L, int idx);
248int (*dll_lua_rawequal) (lua_State *L, int idx1, int idx2);
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200249int (*dll_lua_toboolean) (lua_State *L, int idx);
250const char *(*dll_lua_tolstring) (lua_State *L, int idx, size_t *len);
251void *(*dll_lua_touserdata) (lua_State *L, int idx);
252void (*dll_lua_pushnil) (lua_State *L);
253void (*dll_lua_pushnumber) (lua_State *L, lua_Number n);
254void (*dll_lua_pushinteger) (lua_State *L, lua_Integer n);
255void (*dll_lua_pushlstring) (lua_State *L, const char *s, size_t l);
256void (*dll_lua_pushstring) (lua_State *L, const char *s);
257const char *(*dll_lua_pushfstring) (lua_State *L, const char *fmt, ...);
258void (*dll_lua_pushcclosure) (lua_State *L, lua_CFunction fn, int n);
259void (*dll_lua_pushboolean) (lua_State *L, int b);
260void (*dll_lua_pushlightuserdata) (lua_State *L, void *p);
261void (*dll_lua_getfield) (lua_State *L, int idx, const char *k);
Bram Moolenaar17413672018-07-14 20:49:42 +0200262#if LUA_VERSION_NUM <= 502
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200263void (*dll_lua_rawget) (lua_State *L, int idx);
Bram Moolenaar1dced572012-04-05 16:54:08 +0200264void (*dll_lua_rawgeti) (lua_State *L, int idx, int n);
Bram Moolenaar17413672018-07-14 20:49:42 +0200265#else
266int (*dll_lua_rawget) (lua_State *L, int idx);
267int (*dll_lua_rawgeti) (lua_State *L, int idx, lua_Integer n);
268#endif
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200269void (*dll_lua_createtable) (lua_State *L, int narr, int nrec);
Bram Moolenaar830e3582018-08-21 14:23:35 +0200270#if LUA_VERSION_NUM >= 504
271void *(*dll_lua_newuserdatauv) (lua_State *L, size_t sz, int nuvalue);
272#else
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200273void *(*dll_lua_newuserdata) (lua_State *L, size_t sz);
Bram Moolenaar830e3582018-08-21 14:23:35 +0200274#endif
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200275int (*dll_lua_getmetatable) (lua_State *L, int objindex);
276void (*dll_lua_setfield) (lua_State *L, int idx, const char *k);
277void (*dll_lua_rawset) (lua_State *L, int idx);
Bram Moolenaar17413672018-07-14 20:49:42 +0200278#if LUA_VERSION_NUM <= 502
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200279void (*dll_lua_rawseti) (lua_State *L, int idx, int n);
Bram Moolenaar17413672018-07-14 20:49:42 +0200280#else
281void (*dll_lua_rawseti) (lua_State *L, int idx, lua_Integer n);
282#endif
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200283int (*dll_lua_setmetatable) (lua_State *L, int objindex);
Bram Moolenaarbd2f3c32012-04-06 14:31:00 +0200284int (*dll_lua_next) (lua_State *L, int idx);
Bram Moolenaar2ab2e862019-12-04 21:24:53 +0100285// libs
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200286int (*dll_luaopen_base) (lua_State *L);
287int (*dll_luaopen_table) (lua_State *L);
288int (*dll_luaopen_string) (lua_State *L);
289int (*dll_luaopen_math) (lua_State *L);
Bram Moolenaar16c98f92010-07-28 22:46:08 +0200290int (*dll_luaopen_io) (lua_State *L);
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200291int (*dll_luaopen_os) (lua_State *L);
292int (*dll_luaopen_package) (lua_State *L);
293int (*dll_luaopen_debug) (lua_State *L);
Bram Moolenaar16c98f92010-07-28 22:46:08 +0200294void (*dll_luaL_openlibs) (lua_State *L);
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200295
296typedef void **luaV_function;
297typedef struct {
298 const char *name;
299 luaV_function func;
300} luaV_Reg;
301
302static const luaV_Reg luaV_dll[] = {
Bram Moolenaar2ab2e862019-12-04 21:24:53 +0100303 // lauxlib
Bram Moolenaar1dced572012-04-05 16:54:08 +0200304#if LUA_VERSION_NUM <= 501
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200305 {"luaL_register", (luaV_function) &dll_luaL_register},
Bram Moolenaar1dced572012-04-05 16:54:08 +0200306 {"luaL_prepbuffer", (luaV_function) &dll_luaL_prepbuffer},
307 {"luaL_openlib", (luaV_function) &dll_luaL_openlib},
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200308 {"luaL_typerror", (luaV_function) &dll_luaL_typerror},
Bram Moolenaar1dced572012-04-05 16:54:08 +0200309 {"luaL_loadfile", (luaV_function) &dll_luaL_loadfile},
310 {"luaL_loadbuffer", (luaV_function) &dll_luaL_loadbuffer},
311#else
312 {"luaL_prepbuffsize", (luaV_function) &dll_luaL_prepbuffsize},
313 {"luaL_setfuncs", (luaV_function) &dll_luaL_setfuncs},
314 {"luaL_loadfilex", (luaV_function) &dll_luaL_loadfilex},
315 {"luaL_loadbufferx", (luaV_function) &dll_luaL_loadbufferx},
316 {"luaL_argerror", (luaV_function) &dll_luaL_argerror},
317#endif
Bram Moolenaarbd2f3c32012-04-06 14:31:00 +0200318 {"luaL_checkany", (luaV_function) &dll_luaL_checkany},
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200319 {"luaL_checklstring", (luaV_function) &dll_luaL_checklstring},
320 {"luaL_checkinteger", (luaV_function) &dll_luaL_checkinteger},
321 {"luaL_optinteger", (luaV_function) &dll_luaL_optinteger},
322 {"luaL_checktype", (luaV_function) &dll_luaL_checktype},
323 {"luaL_error", (luaV_function) &dll_luaL_error},
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200324 {"luaL_newstate", (luaV_function) &dll_luaL_newstate},
325 {"luaL_buffinit", (luaV_function) &dll_luaL_buffinit},
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200326 {"luaL_addlstring", (luaV_function) &dll_luaL_addlstring},
327 {"luaL_pushresult", (luaV_function) &dll_luaL_pushresult},
Bram Moolenaar2ab2e862019-12-04 21:24:53 +0100328 // lua
Bram Moolenaar1dced572012-04-05 16:54:08 +0200329#if LUA_VERSION_NUM <= 501
330 {"lua_tonumber", (luaV_function) &dll_lua_tonumber},
331 {"lua_tointeger", (luaV_function) &dll_lua_tointeger},
332 {"lua_call", (luaV_function) &dll_lua_call},
333 {"lua_pcall", (luaV_function) &dll_lua_pcall},
334#else
335 {"lua_tonumberx", (luaV_function) &dll_lua_tonumberx},
336 {"lua_tointegerx", (luaV_function) &dll_lua_tointegerx},
337 {"lua_callk", (luaV_function) &dll_lua_callk},
338 {"lua_pcallk", (luaV_function) &dll_lua_pcallk},
339 {"lua_getglobal", (luaV_function) &dll_lua_getglobal},
340 {"lua_setglobal", (luaV_function) &dll_lua_setglobal},
Bram Moolenaar1dced572012-04-05 16:54:08 +0200341#endif
Bram Moolenaar1f860d82015-06-27 18:36:16 +0200342#if LUA_VERSION_NUM <= 502
343 {"lua_replace", (luaV_function) &dll_lua_replace},
344 {"lua_remove", (luaV_function) &dll_lua_remove},
345#endif
346#if LUA_VERSION_NUM >= 503
347 {"lua_rotate", (luaV_function) &dll_lua_rotate},
348 {"lua_copy", (luaV_function) &dll_lua_copy},
349#endif
Bram Moolenaarbd2f3c32012-04-06 14:31:00 +0200350 {"lua_typename", (luaV_function) &dll_lua_typename},
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200351 {"lua_close", (luaV_function) &dll_lua_close},
352 {"lua_gettop", (luaV_function) &dll_lua_gettop},
353 {"lua_settop", (luaV_function) &dll_lua_settop},
354 {"lua_pushvalue", (luaV_function) &dll_lua_pushvalue},
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200355 {"lua_isnumber", (luaV_function) &dll_lua_isnumber},
356 {"lua_isstring", (luaV_function) &dll_lua_isstring},
357 {"lua_type", (luaV_function) &dll_lua_type},
358 {"lua_rawequal", (luaV_function) &dll_lua_rawequal},
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200359 {"lua_toboolean", (luaV_function) &dll_lua_toboolean},
360 {"lua_tolstring", (luaV_function) &dll_lua_tolstring},
361 {"lua_touserdata", (luaV_function) &dll_lua_touserdata},
362 {"lua_pushnil", (luaV_function) &dll_lua_pushnil},
363 {"lua_pushnumber", (luaV_function) &dll_lua_pushnumber},
364 {"lua_pushinteger", (luaV_function) &dll_lua_pushinteger},
365 {"lua_pushlstring", (luaV_function) &dll_lua_pushlstring},
366 {"lua_pushstring", (luaV_function) &dll_lua_pushstring},
367 {"lua_pushfstring", (luaV_function) &dll_lua_pushfstring},
368 {"lua_pushcclosure", (luaV_function) &dll_lua_pushcclosure},
369 {"lua_pushboolean", (luaV_function) &dll_lua_pushboolean},
370 {"lua_pushlightuserdata", (luaV_function) &dll_lua_pushlightuserdata},
371 {"lua_getfield", (luaV_function) &dll_lua_getfield},
372 {"lua_rawget", (luaV_function) &dll_lua_rawget},
Bram Moolenaar1dced572012-04-05 16:54:08 +0200373 {"lua_rawgeti", (luaV_function) &dll_lua_rawgeti},
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200374 {"lua_createtable", (luaV_function) &dll_lua_createtable},
Bram Moolenaar830e3582018-08-21 14:23:35 +0200375#if LUA_VERSION_NUM >= 504
376 {"lua_newuserdatauv", (luaV_function) &dll_lua_newuserdatauv},
377#else
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200378 {"lua_newuserdata", (luaV_function) &dll_lua_newuserdata},
Bram Moolenaar830e3582018-08-21 14:23:35 +0200379#endif
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200380 {"lua_getmetatable", (luaV_function) &dll_lua_getmetatable},
381 {"lua_setfield", (luaV_function) &dll_lua_setfield},
382 {"lua_rawset", (luaV_function) &dll_lua_rawset},
383 {"lua_rawseti", (luaV_function) &dll_lua_rawseti},
384 {"lua_setmetatable", (luaV_function) &dll_lua_setmetatable},
Bram Moolenaarbd2f3c32012-04-06 14:31:00 +0200385 {"lua_next", (luaV_function) &dll_lua_next},
Bram Moolenaar2ab2e862019-12-04 21:24:53 +0100386 // libs
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200387 {"luaopen_base", (luaV_function) &dll_luaopen_base},
388 {"luaopen_table", (luaV_function) &dll_luaopen_table},
389 {"luaopen_string", (luaV_function) &dll_luaopen_string},
390 {"luaopen_math", (luaV_function) &dll_luaopen_math},
Bram Moolenaar16c98f92010-07-28 22:46:08 +0200391 {"luaopen_io", (luaV_function) &dll_luaopen_io},
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200392 {"luaopen_os", (luaV_function) &dll_luaopen_os},
393 {"luaopen_package", (luaV_function) &dll_luaopen_package},
394 {"luaopen_debug", (luaV_function) &dll_luaopen_debug},
Bram Moolenaar16c98f92010-07-28 22:46:08 +0200395 {"luaL_openlibs", (luaV_function) &dll_luaL_openlibs},
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200396 {NULL, NULL}
397};
398
Bram Moolenaar2334b6d2010-07-22 21:32:16 +0200399static HANDLE hinstLua = NULL;
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200400
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200401 static int
402lua_link_init(char *libname, int verbose)
403{
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200404 const luaV_Reg *reg;
405 if (hinstLua) return OK;
Bram Moolenaar2334b6d2010-07-22 21:32:16 +0200406 hinstLua = load_dll(libname);
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200407 if (!hinstLua)
408 {
409 if (verbose)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100410 semsg(_(e_loadlib), libname);
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200411 return FAIL;
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200412 }
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200413 for (reg = luaV_dll; reg->func; reg++)
414 {
Bram Moolenaar2334b6d2010-07-22 21:32:16 +0200415 if ((*reg->func = symbol_from_dll(hinstLua, reg->name)) == NULL)
416 {
417 close_dll(hinstLua);
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200418 hinstLua = 0;
419 if (verbose)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100420 semsg(_(e_loadfunc), reg->name);
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200421 return FAIL;
422 }
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200423 }
424 return OK;
425}
Bram Moolenaar2ab2e862019-12-04 21:24:53 +0100426#endif // DYNAMIC_LUA
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200427
Bram Moolenaard90b6c02016-08-28 18:10:45 +0200428#if defined(DYNAMIC_LUA) || defined(PROTO)
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200429 int
430lua_enabled(int verbose)
431{
Bram Moolenaar25e4fcd2016-01-09 14:57:47 +0100432 return lua_link_init((char *)p_luadll, verbose) == OK;
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200433}
Bram Moolenaard90b6c02016-08-28 18:10:45 +0200434#endif
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200435
Bram Moolenaar1dced572012-04-05 16:54:08 +0200436#if LUA_VERSION_NUM > 501
437 static int
Bram Moolenaar4eefe472019-03-19 21:59:19 +0100438luaL_typeerror(lua_State *L, int narg, const char *tname)
Bram Moolenaar1dced572012-04-05 16:54:08 +0200439{
440 const char *msg = lua_pushfstring(L, "%s expected, got %s",
Bram Moolenaardb913952012-06-29 12:54:53 +0200441 tname, luaL_typename(L, narg));
Bram Moolenaar1dced572012-04-05 16:54:08 +0200442 return luaL_argerror(L, narg, msg);
443}
444#endif
445
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200446
Bram Moolenaar2ab2e862019-12-04 21:24:53 +0100447// ======= Internal =======
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200448
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200449 static void
450luaV_newmetatable(lua_State *L, const char *tname)
451{
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200452 lua_newtable(L);
453 lua_pushlightuserdata(L, (void *) tname);
454 lua_pushvalue(L, -2);
455 lua_rawset(L, LUA_REGISTRYINDEX);
456}
457
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200458 static void *
459luaV_toudata(lua_State *L, int ud, const char *tname)
460{
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200461 void *p = lua_touserdata(L, ud);
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200462
Bram Moolenaar2ab2e862019-12-04 21:24:53 +0100463 if (p != NULL) // value is userdata?
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200464 {
Bram Moolenaar2ab2e862019-12-04 21:24:53 +0100465 if (lua_getmetatable(L, ud)) // does it have a metatable?
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200466 {
Bram Moolenaar2ab2e862019-12-04 21:24:53 +0100467 luaV_getfield(L, tname); // get metatable
468 if (lua_rawequal(L, -1, -2)) // MTs match?
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200469 {
Bram Moolenaar2ab2e862019-12-04 21:24:53 +0100470 lua_pop(L, 2); // MTs
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200471 return p;
472 }
473 }
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200474 }
475 return NULL;
476}
477
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200478 static void *
Bram Moolenaar1dced572012-04-05 16:54:08 +0200479luaV_checkcache(lua_State *L, void *p)
480{
481 luaV_getudata(L, p);
482 if (lua_isnil(L, -1)) luaL_error(L, "invalid object");
483 lua_pop(L, 1);
484 return p;
485}
486
487#define luaV_unbox(L,luatyp,ud) (*((luatyp *) lua_touserdata((L),(ud))))
488
489#define luaV_checkvalid(L,luatyp,ud) \
490 luaV_checkcache((L), (void *) luaV_unbox((L),luatyp,(ud)))
491
492 static void *
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200493luaV_checkudata(lua_State *L, int ud, const char *tname)
494{
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200495 void *p = luaV_toudata(L, ud, tname);
Bram Moolenaar1dced572012-04-05 16:54:08 +0200496 if (p == NULL) luaL_typeerror(L, ud, tname);
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200497 return p;
498}
499
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200500 static void
501luaV_pushtypval(lua_State *L, typval_T *tv)
502{
Bram Moolenaar1dced572012-04-05 16:54:08 +0200503 if (tv == NULL)
504 {
505 lua_pushnil(L);
506 return;
507 }
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200508 switch (tv->v_type)
509 {
510 case VAR_STRING:
Bram Moolenaard04da7c2012-10-14 03:41:59 +0200511 lua_pushstring(L, tv->vval.v_string == NULL
512 ? "" : (char *)tv->vval.v_string);
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200513 break;
514 case VAR_NUMBER:
515 lua_pushinteger(L, (int) tv->vval.v_number);
516 break;
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200517#ifdef FEAT_FLOAT
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200518 case VAR_FLOAT:
519 lua_pushnumber(L, (lua_Number) tv->vval.v_float);
520 break;
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200521#endif
Bram Moolenaar1dced572012-04-05 16:54:08 +0200522 case VAR_LIST:
523 luaV_pushlist(L, tv->vval.v_list);
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200524 break;
Bram Moolenaar1dced572012-04-05 16:54:08 +0200525 case VAR_DICT:
526 luaV_pushdict(L, tv->vval.v_dict);
527 break;
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +0100528 case VAR_BOOL:
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100529 case VAR_SPECIAL:
530 if (tv->vval.v_number <= VVAL_TRUE)
531 lua_pushinteger(L, (int) tv->vval.v_number);
532 else
533 lua_pushnil(L);
534 break;
Bram Moolenaarca06da92018-07-01 15:12:05 +0200535 case VAR_FUNC:
Bram Moolenaar4eefe472019-03-19 21:59:19 +0100536 luaV_pushfuncref(L, tv->vval.v_string);
Bram Moolenaarca06da92018-07-01 15:12:05 +0200537 break;
Bram Moolenaarb7828692019-03-23 13:57:02 +0100538 case VAR_BLOB:
539 luaV_pushblob(L, tv->vval.v_blob);
540 break;
Bram Moolenaar1dced572012-04-05 16:54:08 +0200541 default:
542 lua_pushnil(L);
543 }
544}
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200545
Bram Moolenaarca06da92018-07-01 15:12:05 +0200546/*
547 * Converts lua value at 'pos' to typval 'tv'.
548 * Returns OK or FAIL.
549 */
550 static int
551luaV_totypval(lua_State *L, int pos, typval_T *tv)
Bram Moolenaar1dced572012-04-05 16:54:08 +0200552{
Bram Moolenaarca06da92018-07-01 15:12:05 +0200553 int status = OK;
554
555 switch (lua_type(L, pos))
556 {
Bram Moolenaar1dced572012-04-05 16:54:08 +0200557 case LUA_TBOOLEAN:
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +0100558 tv->v_type = VAR_BOOL;
Bram Moolenaar1dced572012-04-05 16:54:08 +0200559 tv->vval.v_number = (varnumber_T) lua_toboolean(L, pos);
560 break;
Bram Moolenaar9067cd62019-01-01 00:41:54 +0100561 case LUA_TNIL:
562 tv->v_type = VAR_SPECIAL;
563 tv->vval.v_number = VVAL_NULL;
564 break;
Bram Moolenaar1dced572012-04-05 16:54:08 +0200565 case LUA_TSTRING:
566 tv->v_type = VAR_STRING;
567 tv->vval.v_string = vim_strsave((char_u *) lua_tostring(L, pos));
568 break;
569 case LUA_TNUMBER:
570#ifdef FEAT_FLOAT
Bram Moolenaareb04f082020-05-17 14:32:35 +0200571 {
572 const lua_Number n = lua_tonumber(L, pos);
573
574 if (n > (lua_Number)INT64_MAX || n < (lua_Number)INT64_MIN
575 || ((lua_Number)((varnumber_T)n)) != n)
576 {
577 tv->v_type = VAR_FLOAT;
578 tv->vval.v_float = (float_T)n;
579 }
580 else
581 {
582 tv->v_type = VAR_NUMBER;
583 tv->vval.v_number = (varnumber_T)n;
584 }
585 }
Bram Moolenaar1dced572012-04-05 16:54:08 +0200586#else
587 tv->v_type = VAR_NUMBER;
588 tv->vval.v_number = (varnumber_T) lua_tointeger(L, pos);
589#endif
590 break;
Bram Moolenaarca06da92018-07-01 15:12:05 +0200591 case LUA_TUSERDATA:
592 {
Bram Moolenaar1dced572012-04-05 16:54:08 +0200593 void *p = lua_touserdata(L, pos);
Bram Moolenaarca06da92018-07-01 15:12:05 +0200594
Bram Moolenaarb7828692019-03-23 13:57:02 +0100595 if (lua_getmetatable(L, pos)) // has metatable?
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200596 {
Bram Moolenaarb7828692019-03-23 13:57:02 +0100597 // check list
Bram Moolenaar1dced572012-04-05 16:54:08 +0200598 luaV_getfield(L, LUAVIM_LIST);
599 if (lua_rawequal(L, -1, -2))
Bram Moolenaar2334b6d2010-07-22 21:32:16 +0200600 {
Bram Moolenaar1dced572012-04-05 16:54:08 +0200601 tv->v_type = VAR_LIST;
602 tv->vval.v_list = *((luaV_List *) p);
603 ++tv->vval.v_list->lv_refcount;
Bram Moolenaarb7828692019-03-23 13:57:02 +0100604 lua_pop(L, 2); // MTs
Bram Moolenaarca06da92018-07-01 15:12:05 +0200605 break;
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200606 }
Bram Moolenaarb7828692019-03-23 13:57:02 +0100607 // check dict
Bram Moolenaar1dced572012-04-05 16:54:08 +0200608 luaV_getfield(L, LUAVIM_DICT);
609 if (lua_rawequal(L, -1, -3))
610 {
611 tv->v_type = VAR_DICT;
612 tv->vval.v_dict = *((luaV_Dict *) p);
613 ++tv->vval.v_dict->dv_refcount;
Bram Moolenaarb7828692019-03-23 13:57:02 +0100614 lua_pop(L, 3); // MTs
Bram Moolenaarca06da92018-07-01 15:12:05 +0200615 break;
Bram Moolenaar1dced572012-04-05 16:54:08 +0200616 }
Bram Moolenaarb7828692019-03-23 13:57:02 +0100617 // check blob
618 luaV_getfield(L, LUAVIM_BLOB);
Bram Moolenaarca06da92018-07-01 15:12:05 +0200619 if (lua_rawequal(L, -1, -4))
620 {
Bram Moolenaarb7828692019-03-23 13:57:02 +0100621 tv->v_type = VAR_BLOB;
622 tv->vval.v_blob = *((luaV_Blob *) p);
623 ++tv->vval.v_blob->bv_refcount;
624 lua_pop(L, 4); // MTs
625 break;
626 }
627 // check funcref
628 luaV_getfield(L, LUAVIM_FUNCREF);
629 if (lua_rawequal(L, -1, -5))
630 {
Bram Moolenaarca06da92018-07-01 15:12:05 +0200631 luaV_Funcref *f = (luaV_Funcref *) p;
Bram Moolenaar4eefe472019-03-19 21:59:19 +0100632 func_ref(f->name);
633 tv->v_type = VAR_FUNC;
634 tv->vval.v_string = vim_strsave(f->name);
Bram Moolenaarb7828692019-03-23 13:57:02 +0100635 lua_pop(L, 5); // MTs
Bram Moolenaarca06da92018-07-01 15:12:05 +0200636 break;
637 }
Bram Moolenaarb7828692019-03-23 13:57:02 +0100638 lua_pop(L, 4); // MTs
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200639 }
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200640 }
Bram Moolenaarb7828692019-03-23 13:57:02 +0100641 // FALLTHROUGH
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200642 default:
Bram Moolenaar1dced572012-04-05 16:54:08 +0200643 tv->v_type = VAR_NUMBER;
644 tv->vval.v_number = 0;
Bram Moolenaarca06da92018-07-01 15:12:05 +0200645 status = FAIL;
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200646 }
Bram Moolenaarca06da92018-07-01 15:12:05 +0200647 return status;
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200648}
649
Bram Moolenaar2ab2e862019-12-04 21:24:53 +0100650/*
651 * similar to luaL_addlstring, but replaces \0 with \n if toline and
652 * \n with \0 otherwise
653 */
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200654 static void
655luaV_addlstring(luaL_Buffer *b, const char *s, size_t l, int toline)
656{
657 while (l--)
658 {
659 if (*s == '\0' && toline)
660 luaL_addchar(b, '\n');
661 else if (*s == '\n' && !toline)
662 luaL_addchar(b, '\0');
663 else
664 luaL_addchar(b, *s);
665 s++;
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200666 }
667}
668
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200669 static void
670luaV_pushline(lua_State *L, buf_T *buf, linenr_T n)
671{
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200672 const char *s = (const char *) ml_get_buf(buf, n, FALSE);
673 luaL_Buffer b;
674 luaL_buffinit(L, &b);
675 luaV_addlstring(&b, s, strlen(s), 0);
676 luaL_pushresult(&b);
677}
678
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200679 static char_u *
680luaV_toline(lua_State *L, int pos)
681{
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200682 size_t l;
683 const char *s = lua_tolstring(L, pos, &l);
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200684
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200685 luaL_Buffer b;
686 luaL_buffinit(L, &b);
687 luaV_addlstring(&b, s, l, 1);
688 luaL_pushresult(&b);
689 return (char_u *) lua_tostring(L, -1);
690}
691
Bram Moolenaar2ab2e862019-12-04 21:24:53 +0100692/*
693 * pops a string s from the top of the stack and calls mf(t) for pieces t of
694 * s separated by newlines
695 */
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200696 static void
697luaV_msgfunc(lua_State *L, msgfunc_T mf)
698{
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200699 luaL_Buffer b;
700 size_t l;
701 const char *p, *s = lua_tolstring(L, -1, &l);
702 luaL_buffinit(L, &b);
703 luaV_addlstring(&b, s, l, 0);
704 luaL_pushresult(&b);
Bram Moolenaar2ab2e862019-12-04 21:24:53 +0100705 // break string
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200706 p = s = lua_tolstring(L, -1, &l);
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200707 while (l--)
708 {
Bram Moolenaar2ab2e862019-12-04 21:24:53 +0100709 if (*p++ == '\0') // break?
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200710 {
711 mf((char_u *) s);
712 s = p;
713 }
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200714 }
715 mf((char_u *) s);
Bram Moolenaar2ab2e862019-12-04 21:24:53 +0100716 lua_pop(L, 2); // original and modified strings
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200717}
718
Bram Moolenaar1dced572012-04-05 16:54:08 +0200719#define luaV_newtype(typ,tname,luatyp,luatname) \
720 static luatyp * \
Bram Moolenaar4eefe472019-03-19 21:59:19 +0100721 luaV_new##tname(lua_State *L, typ *obj) \
Bram Moolenaar1dced572012-04-05 16:54:08 +0200722 { \
723 luatyp *o = (luatyp *) lua_newuserdata(L, sizeof(luatyp)); \
724 *o = obj; \
725 luaV_setudata(L, obj); /* cache[obj] = udata */ \
726 luaV_getfield(L, luatname); \
727 lua_setmetatable(L, -2); \
728 return o; \
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200729 }
Bram Moolenaar1dced572012-04-05 16:54:08 +0200730
731#define luaV_pushtype(typ,tname,luatyp) \
732 static luatyp * \
Bram Moolenaarca06da92018-07-01 15:12:05 +0200733 luaV_push##tname(lua_State *L, typ *obj) \
Bram Moolenaar1dced572012-04-05 16:54:08 +0200734 { \
735 luatyp *o = NULL; \
736 if (obj == NULL) \
737 lua_pushnil(L); \
738 else { \
739 luaV_getudata(L, obj); \
740 if (lua_isnil(L, -1)) /* not interned? */ \
741 { \
742 lua_pop(L, 1); \
743 o = luaV_new##tname(L, obj); \
744 } \
745 else \
746 o = (luatyp *) lua_touserdata(L, -1); \
747 } \
748 return o; \
749 }
750
751#define luaV_type_tostring(tname,luatname) \
752 static int \
Bram Moolenaar4eefe472019-03-19 21:59:19 +0100753 luaV_##tname##_tostring(lua_State *L) \
Bram Moolenaar1dced572012-04-05 16:54:08 +0200754 { \
755 lua_pushfstring(L, "%s: %p", luatname, lua_touserdata(L, 1)); \
756 return 1; \
757 }
758
Bram Moolenaar2ab2e862019-12-04 21:24:53 +0100759// ======= List type =======
Bram Moolenaar1dced572012-04-05 16:54:08 +0200760
761 static luaV_List *
Bram Moolenaar4eefe472019-03-19 21:59:19 +0100762luaV_newlist(lua_State *L, list_T *lis)
Bram Moolenaar1dced572012-04-05 16:54:08 +0200763{
764 luaV_List *l = (luaV_List *) lua_newuserdata(L, sizeof(luaV_List));
765 *l = lis;
Bram Moolenaar2ab2e862019-12-04 21:24:53 +0100766 lis->lv_refcount++; // reference in Lua
767 luaV_setudata(L, lis); // cache[lis] = udata
Bram Moolenaar1dced572012-04-05 16:54:08 +0200768 luaV_getfield(L, LUAVIM_LIST);
769 lua_setmetatable(L, -2);
770 return l;
771}
772
773luaV_pushtype(list_T, list, luaV_List)
774luaV_type_tostring(list, LUAVIM_LIST)
775
776 static int
Bram Moolenaar4eefe472019-03-19 21:59:19 +0100777luaV_list_len(lua_State *L)
Bram Moolenaar1dced572012-04-05 16:54:08 +0200778{
779 list_T *l = luaV_unbox(L, luaV_List, 1);
Bram Moolenaarb7828692019-03-23 13:57:02 +0100780 lua_pushinteger(L, (int) list_len(l));
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200781 return 1;
782}
783
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200784 static int
Bram Moolenaar4eefe472019-03-19 21:59:19 +0100785luaV_list_iter(lua_State *L)
Bram Moolenaar1dced572012-04-05 16:54:08 +0200786{
787 listitem_T *li = (listitem_T *) lua_touserdata(L, lua_upvalueindex(2));
788 if (li == NULL) return 0;
789 luaV_pushtypval(L, &li->li_tv);
790 lua_pushlightuserdata(L, (void *) li->li_next);
791 lua_replace(L, lua_upvalueindex(2));
792 return 1;
793}
794
795 static int
Bram Moolenaar4eefe472019-03-19 21:59:19 +0100796luaV_list_call(lua_State *L)
Bram Moolenaar1dced572012-04-05 16:54:08 +0200797{
798 list_T *l = luaV_unbox(L, luaV_List, 1);
Bram Moolenaar2ab2e862019-12-04 21:24:53 +0100799 lua_pushvalue(L, lua_upvalueindex(1)); // pass cache table along
Bram Moolenaar1dced572012-04-05 16:54:08 +0200800 lua_pushlightuserdata(L, (void *) l->lv_first);
801 lua_pushcclosure(L, luaV_list_iter, 2);
802 return 1;
803}
804
805 static int
Bram Moolenaar4eefe472019-03-19 21:59:19 +0100806luaV_list_index(lua_State *L)
Bram Moolenaar1dced572012-04-05 16:54:08 +0200807{
808 list_T *l = luaV_unbox(L, luaV_List, 1);
Bram Moolenaar2ab2e862019-12-04 21:24:53 +0100809 if (lua_isnumber(L, 2)) // list item?
Bram Moolenaar1dced572012-04-05 16:54:08 +0200810 {
811 listitem_T *li = list_find(l, (long) luaL_checkinteger(L, 2));
812 if (li == NULL)
813 lua_pushnil(L);
814 else
815 luaV_pushtypval(L, &li->li_tv);
816 }
Bram Moolenaar2ab2e862019-12-04 21:24:53 +0100817 else if (lua_isstring(L, 2)) // method?
Bram Moolenaar1dced572012-04-05 16:54:08 +0200818 {
819 const char *s = lua_tostring(L, 2);
820 if (strncmp(s, "add", 3) == 0
Bram Moolenaarb3766472013-04-15 13:49:21 +0200821 || strncmp(s, "insert", 6) == 0)
Bram Moolenaar1dced572012-04-05 16:54:08 +0200822 {
823 lua_getmetatable(L, 1);
824 lua_getfield(L, -1, s);
825 }
826 else
827 lua_pushnil(L);
828 }
829 else
830 lua_pushnil(L);
831 return 1;
832}
833
834 static int
Bram Moolenaar4eefe472019-03-19 21:59:19 +0100835luaV_list_newindex(lua_State *L)
Bram Moolenaar1dced572012-04-05 16:54:08 +0200836{
837 list_T *l = luaV_unbox(L, luaV_List, 1);
838 long n = (long) luaL_checkinteger(L, 2);
839 listitem_T *li;
840 if (l->lv_lock)
841 luaL_error(L, "list is locked");
842 li = list_find(l, n);
843 if (li == NULL) return 0;
Bram Moolenaar2ab2e862019-12-04 21:24:53 +0100844 if (lua_isnil(L, 3)) // remove?
Bram Moolenaar1dced572012-04-05 16:54:08 +0200845 {
Bram Moolenaar3ec7f4e2014-05-07 17:31:37 +0200846 vimlist_remove(l, li, li);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100847 listitem_free(l, li);
Bram Moolenaar1dced572012-04-05 16:54:08 +0200848 }
849 else
850 {
851 typval_T v;
Bram Moolenaarca06da92018-07-01 15:12:05 +0200852 luaV_checktypval(L, 3, &v, "setting list item");
Bram Moolenaar1dced572012-04-05 16:54:08 +0200853 clear_tv(&li->li_tv);
854 copy_tv(&v, &li->li_tv);
Bram Moolenaarb3766472013-04-15 13:49:21 +0200855 clear_tv(&v);
Bram Moolenaar1dced572012-04-05 16:54:08 +0200856 }
857 return 0;
858}
859
860 static int
Bram Moolenaar4eefe472019-03-19 21:59:19 +0100861luaV_list_add(lua_State *L)
Bram Moolenaar1dced572012-04-05 16:54:08 +0200862{
863 luaV_List *lis = luaV_checkudata(L, 1, LUAVIM_LIST);
864 list_T *l = (list_T *) luaV_checkcache(L, (void *) *lis);
Bram Moolenaarb3766472013-04-15 13:49:21 +0200865 typval_T v;
Bram Moolenaar1dced572012-04-05 16:54:08 +0200866 if (l->lv_lock)
867 luaL_error(L, "list is locked");
Bram Moolenaarb3766472013-04-15 13:49:21 +0200868 lua_settop(L, 2);
Bram Moolenaarca06da92018-07-01 15:12:05 +0200869 luaV_checktypval(L, 2, &v, "adding list item");
Bram Moolenaarb3766472013-04-15 13:49:21 +0200870 if (list_append_tv(l, &v) == FAIL)
Bram Moolenaarca06da92018-07-01 15:12:05 +0200871 luaL_error(L, "failed to add item to list");
Bram Moolenaarb3766472013-04-15 13:49:21 +0200872 clear_tv(&v);
Bram Moolenaar1dced572012-04-05 16:54:08 +0200873 lua_settop(L, 1);
874 return 1;
875}
876
877 static int
Bram Moolenaar4eefe472019-03-19 21:59:19 +0100878luaV_list_insert(lua_State *L)
Bram Moolenaar1dced572012-04-05 16:54:08 +0200879{
880 luaV_List *lis = luaV_checkudata(L, 1, LUAVIM_LIST);
881 list_T *l = (list_T *) luaV_checkcache(L, (void *) *lis);
Bram Moolenaar46538ee2015-02-17 16:28:55 +0100882 long pos = (long) luaL_optinteger(L, 3, 0);
Bram Moolenaar1dced572012-04-05 16:54:08 +0200883 listitem_T *li = NULL;
884 typval_T v;
885 if (l->lv_lock)
886 luaL_error(L, "list is locked");
887 if (pos < l->lv_len)
888 {
889 li = list_find(l, pos);
890 if (li == NULL)
891 luaL_error(L, "invalid position");
892 }
893 lua_settop(L, 2);
Bram Moolenaarca06da92018-07-01 15:12:05 +0200894 luaV_checktypval(L, 2, &v, "inserting list item");
Bram Moolenaarb3766472013-04-15 13:49:21 +0200895 if (list_insert_tv(l, &v, li) == FAIL)
Bram Moolenaarca06da92018-07-01 15:12:05 +0200896 luaL_error(L, "failed to add item to list");
Bram Moolenaarb3766472013-04-15 13:49:21 +0200897 clear_tv(&v);
Bram Moolenaar1dced572012-04-05 16:54:08 +0200898 lua_settop(L, 1);
899 return 1;
900}
901
902static const luaL_Reg luaV_List_mt[] = {
903 {"__tostring", luaV_list_tostring},
Bram Moolenaar1dced572012-04-05 16:54:08 +0200904 {"__len", luaV_list_len},
905 {"__call", luaV_list_call},
906 {"__index", luaV_list_index},
907 {"__newindex", luaV_list_newindex},
908 {"add", luaV_list_add},
909 {"insert", luaV_list_insert},
910 {NULL, NULL}
911};
912
913
Bram Moolenaar2ab2e862019-12-04 21:24:53 +0100914// ======= Dict type =======
Bram Moolenaar1dced572012-04-05 16:54:08 +0200915
916 static luaV_Dict *
Bram Moolenaar4eefe472019-03-19 21:59:19 +0100917luaV_newdict(lua_State *L, dict_T *dic)
Bram Moolenaar1dced572012-04-05 16:54:08 +0200918{
919 luaV_Dict *d = (luaV_Dict *) lua_newuserdata(L, sizeof(luaV_Dict));
920 *d = dic;
Bram Moolenaar2ab2e862019-12-04 21:24:53 +0100921 dic->dv_refcount++; // reference in Lua
922 luaV_setudata(L, dic); // cache[dic] = udata
Bram Moolenaar1dced572012-04-05 16:54:08 +0200923 luaV_getfield(L, LUAVIM_DICT);
924 lua_setmetatable(L, -2);
925 return d;
926}
927
928luaV_pushtype(dict_T, dict, luaV_Dict)
929luaV_type_tostring(dict, LUAVIM_DICT)
930
931 static int
Bram Moolenaar4eefe472019-03-19 21:59:19 +0100932luaV_dict_len(lua_State *L)
Bram Moolenaar1dced572012-04-05 16:54:08 +0200933{
934 dict_T *d = luaV_unbox(L, luaV_Dict, 1);
Bram Moolenaarb7828692019-03-23 13:57:02 +0100935 lua_pushinteger(L, (int) dict_len(d));
Bram Moolenaar1dced572012-04-05 16:54:08 +0200936 return 1;
937}
938
939 static int
Bram Moolenaar4eefe472019-03-19 21:59:19 +0100940luaV_dict_iter(lua_State *L UNUSED)
Bram Moolenaar1dced572012-04-05 16:54:08 +0200941{
Bram Moolenaarfeeaa682013-02-14 22:19:51 +0100942#ifdef FEAT_EVAL
Bram Moolenaar1dced572012-04-05 16:54:08 +0200943 hashitem_T *hi = (hashitem_T *) lua_touserdata(L, lua_upvalueindex(2));
944 int n = lua_tointeger(L, lua_upvalueindex(3));
945 dictitem_T *di;
946 if (n <= 0) return 0;
947 while (HASHITEM_EMPTY(hi)) hi++;
948 di = dict_lookup(hi);
949 lua_pushstring(L, (char *) hi->hi_key);
950 luaV_pushtypval(L, &di->di_tv);
951 lua_pushlightuserdata(L, (void *) (hi + 1));
952 lua_replace(L, lua_upvalueindex(2));
953 lua_pushinteger(L, n - 1);
954 lua_replace(L, lua_upvalueindex(3));
955 return 2;
Bram Moolenaarfeeaa682013-02-14 22:19:51 +0100956#else
957 return 0;
958#endif
Bram Moolenaar1dced572012-04-05 16:54:08 +0200959}
960
961 static int
Bram Moolenaar4eefe472019-03-19 21:59:19 +0100962luaV_dict_call(lua_State *L)
Bram Moolenaar1dced572012-04-05 16:54:08 +0200963{
964 dict_T *d = luaV_unbox(L, luaV_Dict, 1);
965 hashtab_T *ht = &d->dv_hashtab;
Bram Moolenaar2ab2e862019-12-04 21:24:53 +0100966 lua_pushvalue(L, lua_upvalueindex(1)); // pass cache table along
Bram Moolenaar1dced572012-04-05 16:54:08 +0200967 lua_pushlightuserdata(L, (void *) ht->ht_array);
Bram Moolenaar2ab2e862019-12-04 21:24:53 +0100968 lua_pushinteger(L, ht->ht_used); // # remaining items
Bram Moolenaar1dced572012-04-05 16:54:08 +0200969 lua_pushcclosure(L, luaV_dict_iter, 3);
970 return 1;
971}
972
973 static int
Bram Moolenaarca06da92018-07-01 15:12:05 +0200974luaV_dict_index(lua_State *L)
Bram Moolenaar1dced572012-04-05 16:54:08 +0200975{
976 dict_T *d = luaV_unbox(L, luaV_Dict, 1);
977 char_u *key = (char_u *) luaL_checkstring(L, 2);
978 dictitem_T *di = dict_find(d, key, -1);
Bram Moolenaarca06da92018-07-01 15:12:05 +0200979
Bram Moolenaar1dced572012-04-05 16:54:08 +0200980 if (di == NULL)
981 lua_pushnil(L);
982 else
Bram Moolenaarca06da92018-07-01 15:12:05 +0200983 {
Bram Moolenaar1dced572012-04-05 16:54:08 +0200984 luaV_pushtypval(L, &di->di_tv);
Bram Moolenaar2ab2e862019-12-04 21:24:53 +0100985 if (di->di_tv.v_type == VAR_FUNC) // funcref?
Bram Moolenaarca06da92018-07-01 15:12:05 +0200986 {
987 luaV_Funcref *f = (luaV_Funcref *) lua_touserdata(L, -1);
Bram Moolenaar2ab2e862019-12-04 21:24:53 +0100988 f->self = d; // keep "self" reference
Bram Moolenaarca06da92018-07-01 15:12:05 +0200989 d->dv_refcount++;
990 }
991 }
Bram Moolenaar1dced572012-04-05 16:54:08 +0200992 return 1;
993}
994
995 static int
Bram Moolenaarca06da92018-07-01 15:12:05 +0200996luaV_dict_newindex(lua_State *L)
Bram Moolenaar1dced572012-04-05 16:54:08 +0200997{
998 dict_T *d = luaV_unbox(L, luaV_Dict, 1);
999 char_u *key = (char_u *) luaL_checkstring(L, 2);
1000 dictitem_T *di;
Bram Moolenaarca06da92018-07-01 15:12:05 +02001001 typval_T v;
Bram Moolenaar713bf9e92019-03-16 16:38:41 +01001002
Bram Moolenaar1dced572012-04-05 16:54:08 +02001003 if (d->dv_lock)
1004 luaL_error(L, "dict is locked");
Bram Moolenaard6ef5f92018-07-13 22:08:23 +02001005 if (key == NULL)
1006 return 0;
1007 if (*key == NUL)
Bram Moolenaarca06da92018-07-01 15:12:05 +02001008 luaL_error(L, "empty key");
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001009 if (!lua_isnil(L, 3)) // read value?
Bram Moolenaar17413672018-07-14 20:49:42 +02001010 {
Bram Moolenaarca06da92018-07-01 15:12:05 +02001011 luaV_checktypval(L, 3, &v, "setting dict item");
1012 if (d->dv_scope == VAR_DEF_SCOPE && v.v_type == VAR_FUNC)
1013 luaL_error(L, "cannot assign funcref to builtin scope");
1014 }
Bram Moolenaar1dced572012-04-05 16:54:08 +02001015 di = dict_find(d, key, -1);
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001016 if (di == NULL) // non-existing key?
Bram Moolenaar1dced572012-04-05 16:54:08 +02001017 {
Bram Moolenaard6ef5f92018-07-13 22:08:23 +02001018 if (lua_isnil(L, 3))
1019 return 0;
Bram Moolenaar1dced572012-04-05 16:54:08 +02001020 di = dictitem_alloc(key);
Bram Moolenaard6ef5f92018-07-13 22:08:23 +02001021 if (di == NULL)
1022 return 0;
Bram Moolenaar1dced572012-04-05 16:54:08 +02001023 if (dict_add(d, di) == FAIL)
1024 {
Bram Moolenaard6ef5f92018-07-13 22:08:23 +02001025 vim_free(di);
1026 return 0;
Bram Moolenaar1dced572012-04-05 16:54:08 +02001027 }
1028 }
1029 else
1030 clear_tv(&di->di_tv);
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001031 if (lua_isnil(L, 3)) // remove?
Bram Moolenaar1dced572012-04-05 16:54:08 +02001032 {
1033 hashitem_T *hi = hash_find(&d->dv_hashtab, di->di_key);
1034 hash_remove(&d->dv_hashtab, hi);
1035 dictitem_free(di);
1036 }
Bram Moolenaarca06da92018-07-01 15:12:05 +02001037 else
1038 {
Bram Moolenaar1dced572012-04-05 16:54:08 +02001039 copy_tv(&v, &di->di_tv);
Bram Moolenaarb3766472013-04-15 13:49:21 +02001040 clear_tv(&v);
Bram Moolenaar1dced572012-04-05 16:54:08 +02001041 }
1042 return 0;
1043}
1044
1045static const luaL_Reg luaV_Dict_mt[] = {
1046 {"__tostring", luaV_dict_tostring},
Bram Moolenaar1dced572012-04-05 16:54:08 +02001047 {"__len", luaV_dict_len},
1048 {"__call", luaV_dict_call},
1049 {"__index", luaV_dict_index},
1050 {"__newindex", luaV_dict_newindex},
1051 {NULL, NULL}
1052};
1053
1054
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001055// ======= Blob type =======
Bram Moolenaarb7828692019-03-23 13:57:02 +01001056
1057 static luaV_Blob *
1058luaV_newblob(lua_State *L, blob_T *blo)
1059{
1060 luaV_Blob *b = (luaV_Blob *) lua_newuserdata(L, sizeof(luaV_Blob));
1061 *b = blo;
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001062 blo->bv_refcount++; // reference in Lua
1063 luaV_setudata(L, blo); // cache[blo] = udata
Bram Moolenaarb7828692019-03-23 13:57:02 +01001064 luaV_getfield(L, LUAVIM_BLOB);
1065 lua_setmetatable(L, -2);
1066 return b;
1067}
1068
1069luaV_pushtype(blob_T, blob, luaV_Blob)
1070luaV_type_tostring(blob, LUAVIM_BLOB)
1071
1072 static int
1073luaV_blob_gc(lua_State *L)
1074{
1075 blob_T *b = luaV_unbox(L, luaV_Blob, 1);
1076 blob_unref(b);
1077 return 0;
1078}
1079
1080 static int
1081luaV_blob_len(lua_State *L)
1082{
1083 blob_T *b = luaV_unbox(L, luaV_Blob, 1);
1084 lua_pushinteger(L, (int) blob_len(b));
1085 return 1;
1086}
1087
1088 static int
1089luaV_blob_index(lua_State *L)
1090{
1091 blob_T *b = luaV_unbox(L, luaV_Blob, 1);
1092 if (lua_isnumber(L, 2))
1093 {
1094 int idx = luaL_checkinteger(L, 2);
1095 if (idx < blob_len(b))
1096 lua_pushnumber(L, (lua_Number) blob_get(b, idx));
1097 else
1098 lua_pushnil(L);
1099 }
1100 else if (lua_isstring(L, 2))
1101 {
1102 const char *s = lua_tostring(L, 2);
1103 if (strncmp(s, "add", 3) == 0)
1104 {
1105 lua_getmetatable(L, 1);
1106 lua_getfield(L, -1, s);
1107 }
1108 else
1109 lua_pushnil(L);
1110 }
1111 else
1112 lua_pushnil(L);
1113 return 1;
1114}
1115
1116 static int
1117luaV_blob_newindex(lua_State *L)
1118{
1119 blob_T *b = luaV_unbox(L, luaV_Blob, 1);
1120 if (b->bv_lock)
1121 luaL_error(L, "blob is locked");
1122 if (lua_isnumber(L, 2))
1123 {
1124 long len = blob_len(b);
1125 int idx = luaL_checkinteger(L, 2);
1126 int val = luaL_checkinteger(L, 3);
1127 if (idx < len || (idx == len && ga_grow(&b->bv_ga, 1) == OK))
1128 {
1129 blob_set(b, idx, (char_u) val);
1130 if (idx == len)
1131 ++b->bv_ga.ga_len;
1132 }
1133 else
1134 luaL_error(L, "index out of range");
1135 }
1136 return 0;
1137}
1138
1139 static int
1140luaV_blob_add(lua_State *L)
1141{
1142 luaV_Blob *blo = luaV_checkudata(L, 1, LUAVIM_BLOB);
1143 blob_T *b = (blob_T *) luaV_checkcache(L, (void *) *blo);
1144 if (b->bv_lock)
1145 luaL_error(L, "blob is locked");
1146 lua_settop(L, 2);
1147 if (!lua_isstring(L, 2))
1148 luaL_error(L, "string expected, got %s", luaL_typename(L, 2));
1149 else
1150 {
1151 size_t i, l = 0;
1152 const char *s = lua_tolstring(L, 2, &l);
1153
Bram Moolenaar5f1d3ae2020-02-11 22:37:35 +01001154 if (ga_grow(&b->bv_ga, (int)l) == OK)
Bram Moolenaar6fb5c972019-03-26 21:44:20 +01001155 for (i = 0; i < l; ++i)
1156 ga_append(&b->bv_ga, s[i]);
Bram Moolenaarb7828692019-03-23 13:57:02 +01001157 }
1158 lua_settop(L, 1);
1159 return 1;
1160}
1161
1162static const luaL_Reg luaV_Blob_mt[] = {
1163 {"__tostring", luaV_blob_tostring},
1164 {"__gc", luaV_blob_gc},
1165 {"__len", luaV_blob_len},
1166 {"__index", luaV_blob_index},
1167 {"__newindex", luaV_blob_newindex},
1168 {"add", luaV_blob_add},
1169 {NULL, NULL}
1170};
1171
1172
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001173// ======= Funcref type =======
Bram Moolenaarca06da92018-07-01 15:12:05 +02001174
1175 static luaV_Funcref *
1176luaV_newfuncref(lua_State *L, char_u *name)
1177{
1178 luaV_Funcref *f = (luaV_Funcref *)lua_newuserdata(L, sizeof(luaV_Funcref));
1179
1180 if (name != NULL)
1181 {
Bram Moolenaar4eefe472019-03-19 21:59:19 +01001182 func_ref(name);
1183 f->name = vim_strsave(name);
Bram Moolenaarca06da92018-07-01 15:12:05 +02001184 }
Bram Moolenaarca06da92018-07-01 15:12:05 +02001185 f->self = NULL;
1186 luaV_getfield(L, LUAVIM_FUNCREF);
1187 lua_setmetatable(L, -2);
1188 return f;
1189}
1190
1191 static luaV_Funcref *
Bram Moolenaar4eefe472019-03-19 21:59:19 +01001192luaV_pushfuncref(lua_State *L, char_u *name)
Bram Moolenaarca06da92018-07-01 15:12:05 +02001193{
Bram Moolenaar4eefe472019-03-19 21:59:19 +01001194 return luaV_newfuncref(L, name);
Bram Moolenaarca06da92018-07-01 15:12:05 +02001195}
1196
1197
1198luaV_type_tostring(funcref, LUAVIM_FUNCREF)
1199
1200 static int
1201luaV_funcref_gc(lua_State *L)
1202{
1203 luaV_Funcref *f = (luaV_Funcref *) lua_touserdata(L, 1);
1204
Bram Moolenaar4eefe472019-03-19 21:59:19 +01001205 func_unref(f->name);
1206 vim_free(f->name);
1207 // NOTE: Don't call "dict_unref(f->self)", because the dict of "f->self"
1208 // will be (or has been already) freed by Vim's garbage collection.
Bram Moolenaarca06da92018-07-01 15:12:05 +02001209 return 0;
1210}
1211
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001212// equivalent to string(funcref)
Bram Moolenaarca06da92018-07-01 15:12:05 +02001213 static int
1214luaV_funcref_len(lua_State *L)
1215{
1216 luaV_Funcref *f = (luaV_Funcref *) lua_touserdata(L, 1);
1217
Bram Moolenaar4eefe472019-03-19 21:59:19 +01001218 lua_pushstring(L, (const char *) f->name);
Bram Moolenaarca06da92018-07-01 15:12:05 +02001219 return 1;
1220}
1221
1222 static int
1223luaV_funcref_call(lua_State *L)
1224{
1225 luaV_Funcref *f = (luaV_Funcref *) lua_touserdata(L, 1);
Bram Moolenaar4eefe472019-03-19 21:59:19 +01001226 int i, n = lua_gettop(L) - 1; // #args
1227 int status = FAIL;
1228 typval_T args;
1229 typval_T rettv;
Bram Moolenaarca06da92018-07-01 15:12:05 +02001230
Bram Moolenaar4eefe472019-03-19 21:59:19 +01001231 args.v_type = VAR_LIST;
1232 args.vval.v_list = list_alloc();
1233 rettv.v_type = VAR_UNKNOWN; // as in clear_tv
1234 if (args.vval.v_list != NULL)
Bram Moolenaard6ef5f92018-07-13 22:08:23 +02001235 {
Bram Moolenaar4eefe472019-03-19 21:59:19 +01001236 typval_T v;
1237
Bram Moolenaar17413672018-07-14 20:49:42 +02001238 for (i = 0; i < n; i++)
1239 {
Bram Moolenaard6ef5f92018-07-13 22:08:23 +02001240 luaV_checktypval(L, i + 2, &v, "calling funcref");
Bram Moolenaar4eefe472019-03-19 21:59:19 +01001241 list_append_tv(args.vval.v_list, &v);
Bram Moolenaar713bf9e92019-03-16 16:38:41 +01001242 clear_tv(&v);
Bram Moolenaard6ef5f92018-07-13 22:08:23 +02001243 }
Bram Moolenaar4eefe472019-03-19 21:59:19 +01001244 status = func_call(f->name, &args, NULL, f->self, &rettv);
Bram Moolenaard6ef5f92018-07-13 22:08:23 +02001245 if (status == OK)
1246 luaV_pushtypval(L, &rettv);
Bram Moolenaar4eefe472019-03-19 21:59:19 +01001247 clear_tv(&args);
Bram Moolenaard6ef5f92018-07-13 22:08:23 +02001248 clear_tv(&rettv);
Bram Moolenaarca06da92018-07-01 15:12:05 +02001249 }
Bram Moolenaarca06da92018-07-01 15:12:05 +02001250 if (status != OK)
1251 luaL_error(L, "cannot call funcref");
1252 return 1;
1253}
1254
1255static const luaL_Reg luaV_Funcref_mt[] = {
1256 {"__tostring", luaV_funcref_tostring},
1257 {"__gc", luaV_funcref_gc},
1258 {"__len", luaV_funcref_len},
1259 {"__call", luaV_funcref_call},
1260 {NULL, NULL}
1261};
1262
1263
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001264// ======= Buffer type =======
Bram Moolenaar1dced572012-04-05 16:54:08 +02001265
1266luaV_newtype(buf_T, buffer, luaV_Buffer, LUAVIM_BUFFER)
1267luaV_pushtype(buf_T, buffer, luaV_Buffer)
1268luaV_type_tostring(buffer, LUAVIM_BUFFER)
1269
1270 static int
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001271luaV_buffer_len(lua_State *L)
1272{
Bram Moolenaar1dced572012-04-05 16:54:08 +02001273 buf_T *b = (buf_T *) luaV_checkvalid(L, luaV_Buffer, 1);
1274 lua_pushinteger(L, b->b_ml.ml_line_count);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001275 return 1;
1276}
1277
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001278 static int
1279luaV_buffer_call(lua_State *L)
1280{
Bram Moolenaar1dced572012-04-05 16:54:08 +02001281 buf_T *b = (buf_T *) luaV_checkvalid(L, luaV_Buffer, 1);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001282 lua_settop(L, 1);
Bram Moolenaar1dced572012-04-05 16:54:08 +02001283 set_curbuf(b, DOBUF_SPLIT);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001284 return 1;
1285}
1286
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001287 static int
1288luaV_buffer_index(lua_State *L)
1289{
Bram Moolenaar1dced572012-04-05 16:54:08 +02001290 buf_T *b = (buf_T *) luaV_checkvalid(L, luaV_Buffer, 1);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001291 linenr_T n = (linenr_T) lua_tointeger(L, 2);
Bram Moolenaar1dced572012-04-05 16:54:08 +02001292 if (n > 0 && n <= b->b_ml.ml_line_count)
1293 luaV_pushline(L, b, n);
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001294 else if (lua_isstring(L, 2))
1295 {
1296 const char *s = lua_tostring(L, 2);
1297 if (strncmp(s, "name", 4) == 0)
Bram Moolenaarfe08df42018-07-07 23:07:41 +02001298 lua_pushstring(L, (b->b_sfname == NULL)
1299 ? "" : (char *) b->b_sfname);
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001300 else if (strncmp(s, "fname", 5) == 0)
Bram Moolenaarfe08df42018-07-07 23:07:41 +02001301 lua_pushstring(L, (b->b_ffname == NULL)
1302 ? "" : (char *) b->b_ffname);
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001303 else if (strncmp(s, "number", 6) == 0)
Bram Moolenaar1dced572012-04-05 16:54:08 +02001304 lua_pushinteger(L, b->b_fnum);
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001305 // methods
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001306 else if (strncmp(s, "insert", 6) == 0
1307 || strncmp(s, "next", 4) == 0
1308 || strncmp(s, "previous", 8) == 0
1309 || strncmp(s, "isvalid", 7) == 0)
1310 {
1311 lua_getmetatable(L, 1);
1312 lua_getfield(L, -1, s);
1313 }
1314 else
1315 lua_pushnil(L);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001316 }
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001317 else
1318 lua_pushnil(L);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001319 return 1;
1320}
1321
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001322 static int
1323luaV_buffer_newindex(lua_State *L)
1324{
Bram Moolenaar1dced572012-04-05 16:54:08 +02001325 buf_T *b = (buf_T *) luaV_checkvalid(L, luaV_Buffer, 1);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001326 linenr_T n = (linenr_T) luaL_checkinteger(L, 2);
1327#ifdef HAVE_SANDBOX
1328 luaV_checksandbox(L);
1329#endif
Bram Moolenaar1dced572012-04-05 16:54:08 +02001330 if (n < 1 || n > b->b_ml.ml_line_count)
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001331 luaL_error(L, "invalid line number");
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001332 if (lua_isnil(L, 3)) // delete line
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001333 {
1334 buf_T *buf = curbuf;
Bram Moolenaar1dced572012-04-05 16:54:08 +02001335 curbuf = b;
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001336 if (u_savedel(n, 1L) == FAIL)
1337 {
1338 curbuf = buf;
1339 luaL_error(L, "cannot save undo information");
1340 }
1341 else if (ml_delete(n, FALSE) == FAIL)
1342 {
1343 curbuf = buf;
1344 luaL_error(L, "cannot delete line");
1345 }
Bram Moolenaarca06da92018-07-01 15:12:05 +02001346 else
1347 {
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001348 deleted_lines_mark(n, 1L);
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001349 if (b == curwin->w_buffer) // fix cursor in current window?
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001350 {
1351 if (curwin->w_cursor.lnum >= n)
1352 {
1353 if (curwin->w_cursor.lnum > n)
1354 {
1355 curwin->w_cursor.lnum -= 1;
1356 check_cursor_col();
1357 }
1358 else check_cursor();
1359 changed_cline_bef_curs();
1360 }
1361 invalidate_botline();
1362 }
1363 }
1364 curbuf = buf;
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001365 }
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001366 else if (lua_isstring(L, 3)) // update line
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001367 {
1368 buf_T *buf = curbuf;
Bram Moolenaar1dced572012-04-05 16:54:08 +02001369 curbuf = b;
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001370 if (u_savesub(n) == FAIL)
1371 {
1372 curbuf = buf;
1373 luaL_error(L, "cannot save undo information");
1374 }
1375 else if (ml_replace(n, luaV_toline(L, 3), TRUE) == FAIL)
1376 {
1377 curbuf = buf;
1378 luaL_error(L, "cannot replace line");
1379 }
1380 else changed_bytes(n, 0);
1381 curbuf = buf;
Bram Moolenaar1dced572012-04-05 16:54:08 +02001382 if (b == curwin->w_buffer)
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001383 check_cursor_col();
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001384 }
1385 else
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001386 luaL_error(L, "wrong argument to change line");
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001387 return 0;
1388}
1389
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001390 static int
1391luaV_buffer_insert(lua_State *L)
1392{
Bram Moolenaar1dced572012-04-05 16:54:08 +02001393 luaV_Buffer *lb = luaV_checkudata(L, 1, LUAVIM_BUFFER);
1394 buf_T *b = (buf_T *) luaV_checkcache(L, (void *) *lb);
1395 linenr_T last = b->b_ml.ml_line_count;
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001396 linenr_T n = (linenr_T) luaL_optinteger(L, 3, last);
1397 buf_T *buf;
1398 luaL_checktype(L, 2, LUA_TSTRING);
1399#ifdef HAVE_SANDBOX
1400 luaV_checksandbox(L);
1401#endif
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001402 // fix insertion line
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001403 if (n < 0) n = 0;
1404 if (n > last) n = last;
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001405 // insert
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001406 buf = curbuf;
Bram Moolenaar1dced572012-04-05 16:54:08 +02001407 curbuf = b;
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001408 if (u_save(n, n + 1) == FAIL)
1409 {
1410 curbuf = buf;
1411 luaL_error(L, "cannot save undo information");
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001412 }
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001413 else if (ml_append(n, luaV_toline(L, 2), 0, FALSE) == FAIL)
1414 {
1415 curbuf = buf;
1416 luaL_error(L, "cannot insert line");
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001417 }
1418 else
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001419 appended_lines_mark(n, 1L);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001420 curbuf = buf;
1421 update_screen(VALID);
1422 return 0;
1423}
1424
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001425 static int
1426luaV_buffer_next(lua_State *L)
1427{
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001428 luaV_Buffer *b = luaV_checkudata(L, 1, LUAVIM_BUFFER);
Bram Moolenaar1dced572012-04-05 16:54:08 +02001429 buf_T *buf = (buf_T *) luaV_checkcache(L, (void *) *b);
1430 luaV_pushbuffer(L, buf->b_next);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001431 return 1;
1432}
1433
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001434 static int
1435luaV_buffer_previous(lua_State *L)
1436{
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001437 luaV_Buffer *b = luaV_checkudata(L, 1, LUAVIM_BUFFER);
Bram Moolenaar1dced572012-04-05 16:54:08 +02001438 buf_T *buf = (buf_T *) luaV_checkcache(L, (void *) *b);
1439 luaV_pushbuffer(L, buf->b_prev);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001440 return 1;
1441}
1442
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001443 static int
1444luaV_buffer_isvalid(lua_State *L)
1445{
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001446 luaV_Buffer *b = luaV_checkudata(L, 1, LUAVIM_BUFFER);
Bram Moolenaar1dced572012-04-05 16:54:08 +02001447 luaV_getudata(L, *b);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001448 lua_pushboolean(L, !lua_isnil(L, -1));
1449 return 1;
1450}
1451
1452static const luaL_Reg luaV_Buffer_mt[] = {
1453 {"__tostring", luaV_buffer_tostring},
1454 {"__len", luaV_buffer_len},
1455 {"__call", luaV_buffer_call},
1456 {"__index", luaV_buffer_index},
1457 {"__newindex", luaV_buffer_newindex},
1458 {"insert", luaV_buffer_insert},
1459 {"next", luaV_buffer_next},
1460 {"previous", luaV_buffer_previous},
1461 {"isvalid", luaV_buffer_isvalid},
1462 {NULL, NULL}
1463};
1464
1465
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001466// ======= Window type =======
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001467
Bram Moolenaar1dced572012-04-05 16:54:08 +02001468luaV_newtype(win_T, window, luaV_Window, LUAVIM_WINDOW)
1469luaV_pushtype(win_T, window, luaV_Window)
1470luaV_type_tostring(window, LUAVIM_WINDOW)
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001471
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001472 static int
1473luaV_window_call(lua_State *L)
1474{
Bram Moolenaar1dced572012-04-05 16:54:08 +02001475 win_T *w = (win_T *) luaV_checkvalid(L, luaV_Window, 1);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001476 lua_settop(L, 1);
Bram Moolenaar1dced572012-04-05 16:54:08 +02001477 win_goto(w);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001478 return 1;
1479}
1480
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001481 static int
1482luaV_window_index(lua_State *L)
1483{
Bram Moolenaar1dced572012-04-05 16:54:08 +02001484 win_T *w = (win_T *) luaV_checkvalid(L, luaV_Window, 1);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001485 const char *s = luaL_checkstring(L, 2);
1486 if (strncmp(s, "buffer", 6) == 0)
Bram Moolenaar1dced572012-04-05 16:54:08 +02001487 luaV_pushbuffer(L, w->w_buffer);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001488 else if (strncmp(s, "line", 4) == 0)
Bram Moolenaar1dced572012-04-05 16:54:08 +02001489 lua_pushinteger(L, w->w_cursor.lnum);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001490 else if (strncmp(s, "col", 3) == 0)
Bram Moolenaar1dced572012-04-05 16:54:08 +02001491 lua_pushinteger(L, w->w_cursor.col + 1);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001492 else if (strncmp(s, "width", 5) == 0)
Bram Moolenaar02631462017-09-22 15:20:32 +02001493 lua_pushinteger(L, w->w_width);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001494 else if (strncmp(s, "height", 6) == 0)
Bram Moolenaar1dced572012-04-05 16:54:08 +02001495 lua_pushinteger(L, w->w_height);
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001496 // methods
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001497 else if (strncmp(s, "next", 4) == 0
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001498 || strncmp(s, "previous", 8) == 0
1499 || strncmp(s, "isvalid", 7) == 0)
1500 {
1501 lua_getmetatable(L, 1);
1502 lua_getfield(L, -1, s);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001503 }
1504 else
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001505 lua_pushnil(L);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001506 return 1;
1507}
1508
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001509 static int
Bram Moolenaar4eefe472019-03-19 21:59:19 +01001510luaV_window_newindex(lua_State *L)
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001511{
Bram Moolenaar1dced572012-04-05 16:54:08 +02001512 win_T *w = (win_T *) luaV_checkvalid(L, luaV_Window, 1);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001513 const char *s = luaL_checkstring(L, 2);
1514 int v = luaL_checkinteger(L, 3);
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001515 if (strncmp(s, "line", 4) == 0)
1516 {
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001517#ifdef HAVE_SANDBOX
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001518 luaV_checksandbox(L);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001519#endif
Bram Moolenaar1dced572012-04-05 16:54:08 +02001520 if (v < 1 || v > w->w_buffer->b_ml.ml_line_count)
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001521 luaL_error(L, "line out of range");
Bram Moolenaar1dced572012-04-05 16:54:08 +02001522 w->w_cursor.lnum = v;
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001523 update_screen(VALID);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001524 }
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001525 else if (strncmp(s, "col", 3) == 0)
1526 {
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001527#ifdef HAVE_SANDBOX
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001528 luaV_checksandbox(L);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001529#endif
Bram Moolenaar1dced572012-04-05 16:54:08 +02001530 w->w_cursor.col = v - 1;
Bram Moolenaar53901442018-07-25 22:02:36 +02001531 w->w_set_curswant = TRUE;
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001532 update_screen(VALID);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001533 }
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001534 else if (strncmp(s, "width", 5) == 0)
1535 {
1536 win_T *win = curwin;
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001537#ifdef FEAT_GUI
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001538 need_mouse_correct = TRUE;
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001539#endif
Bram Moolenaar1dced572012-04-05 16:54:08 +02001540 curwin = w;
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001541 win_setwidth(v);
1542 curwin = win;
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001543 }
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001544 else if (strncmp(s, "height", 6) == 0)
1545 {
1546 win_T *win = curwin;
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001547#ifdef FEAT_GUI
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001548 need_mouse_correct = TRUE;
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001549#endif
Bram Moolenaar1dced572012-04-05 16:54:08 +02001550 curwin = w;
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001551 win_setheight(v);
1552 curwin = win;
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001553 }
1554 else
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001555 luaL_error(L, "invalid window property: `%s'", s);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001556 return 0;
1557}
1558
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001559 static int
1560luaV_window_next(lua_State *L)
1561{
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001562 luaV_Window *w = luaV_checkudata(L, 1, LUAVIM_WINDOW);
Bram Moolenaar1dced572012-04-05 16:54:08 +02001563 win_T *win = (win_T *) luaV_checkcache(L, (void *) *w);
1564 luaV_pushwindow(L, win->w_next);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001565 return 1;
1566}
1567
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001568 static int
1569luaV_window_previous(lua_State *L)
1570{
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001571 luaV_Window *w = luaV_checkudata(L, 1, LUAVIM_WINDOW);
Bram Moolenaar1dced572012-04-05 16:54:08 +02001572 win_T *win = (win_T *) luaV_checkcache(L, (void *) *w);
1573 luaV_pushwindow(L, win->w_prev);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001574 return 1;
1575}
1576
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001577 static int
1578luaV_window_isvalid(lua_State *L)
1579{
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001580 luaV_Window *w = luaV_checkudata(L, 1, LUAVIM_WINDOW);
Bram Moolenaar1dced572012-04-05 16:54:08 +02001581 luaV_getudata(L, *w);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001582 lua_pushboolean(L, !lua_isnil(L, -1));
1583 return 1;
1584}
1585
1586static const luaL_Reg luaV_Window_mt[] = {
1587 {"__tostring", luaV_window_tostring},
1588 {"__call", luaV_window_call},
1589 {"__index", luaV_window_index},
1590 {"__newindex", luaV_window_newindex},
1591 {"next", luaV_window_next},
1592 {"previous", luaV_window_previous},
1593 {"isvalid", luaV_window_isvalid},
1594 {NULL, NULL}
1595};
1596
1597
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001598// ======= Vim module =======
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001599
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001600 static int
1601luaV_print(lua_State *L)
1602{
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001603 int i, n = lua_gettop(L); // nargs
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001604 const char *s;
1605 size_t l;
1606 luaL_Buffer b;
1607 luaL_buffinit(L, &b);
1608 lua_getglobal(L, "tostring");
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001609 for (i = 1; i <= n; i++)
1610 {
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001611 lua_pushvalue(L, -1); // tostring
1612 lua_pushvalue(L, i); // arg
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001613 lua_call(L, 1, 1);
1614 s = lua_tolstring(L, -1, &l);
1615 if (s == NULL)
1616 return luaL_error(L, "cannot convert to string");
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001617 if (i > 1) luaL_addchar(&b, ' '); // use space instead of tab
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001618 luaV_addlstring(&b, s, l, 0);
1619 lua_pop(L, 1);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001620 }
1621 luaL_pushresult(&b);
Bram Moolenaarb98678a2019-10-19 15:18:44 +02001622 if (!got_int)
1623 luaV_msg(L);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001624 return 0;
1625}
1626
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001627 static int
Bram Moolenaar38e2b062011-09-21 17:15:39 +02001628luaV_debug(lua_State *L)
1629{
1630 lua_settop(L, 0);
1631 lua_getglobal(L, "vim");
1632 lua_getfield(L, -1, "eval");
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001633 lua_remove(L, -2); // vim.eval at position 1
Bram Moolenaar38e2b062011-09-21 17:15:39 +02001634 for (;;)
1635 {
1636 const char *input;
1637 size_t l;
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001638 lua_pushvalue(L, 1); // vim.eval
Bram Moolenaar38e2b062011-09-21 17:15:39 +02001639 lua_pushliteral(L, "input('lua_debug> ')");
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001640 lua_call(L, 1, 1); // return string
Bram Moolenaar38e2b062011-09-21 17:15:39 +02001641 input = lua_tolstring(L, -1, &l);
1642 if (l == 0 || strcmp(input, "cont") == 0)
1643 return 0;
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001644 msg_putchar('\n'); // avoid outputting on input line
Bram Moolenaar38e2b062011-09-21 17:15:39 +02001645 if (luaL_loadbuffer(L, input, l, "=(debug command)")
1646 || lua_pcall(L, 0, 0, 0))
1647 luaV_emsg(L);
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001648 lua_settop(L, 1); // remove eventual returns, but keep vim.eval
Bram Moolenaar38e2b062011-09-21 17:15:39 +02001649 }
1650}
1651
1652 static int
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001653luaV_command(lua_State *L)
1654{
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001655 do_cmdline_cmd((char_u *) luaL_checkstring(L, 1));
1656 update_screen(VALID);
1657 return 0;
1658}
1659
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001660 static int
1661luaV_eval(lua_State *L)
1662{
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001663 typval_T *tv = eval_expr((char_u *) luaL_checkstring(L, 1), NULL);
1664 if (tv == NULL) luaL_error(L, "invalid expression");
1665 luaV_pushtypval(L, tv);
Bram Moolenaarb3766472013-04-15 13:49:21 +02001666 free_tv(tv);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001667 return 1;
1668}
1669
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001670 static int
Bram Moolenaar0d2e4fc2010-07-18 12:35:47 +02001671luaV_beep(lua_State *L UNUSED)
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001672{
Bram Moolenaar165bc692015-07-21 17:53:25 +02001673 vim_beep(BO_LANG);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001674 return 0;
1675}
1676
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001677 static int
1678luaV_line(lua_State *L)
1679{
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001680 luaV_pushline(L, curbuf, curwin->w_cursor.lnum);
1681 return 1;
1682}
1683
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001684 static int
Bram Moolenaar1dced572012-04-05 16:54:08 +02001685luaV_list(lua_State *L)
1686{
Bram Moolenaarca06da92018-07-01 15:12:05 +02001687 list_T *l;
1688 int initarg = !lua_isnoneornil(L, 1);
1689
1690 if (initarg && lua_type(L, 1) != LUA_TTABLE)
1691 luaL_error(L, "table expected, got %s", luaL_typename(L, 1));
1692 l = list_alloc();
Bram Moolenaar1dced572012-04-05 16:54:08 +02001693 if (l == NULL)
1694 lua_pushnil(L);
1695 else
Bram Moolenaarca06da92018-07-01 15:12:05 +02001696 {
Bram Moolenaar1dced572012-04-05 16:54:08 +02001697 luaV_newlist(L, l);
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001698 if (initarg) // traverse table to init list
Bram Moolenaar17413672018-07-14 20:49:42 +02001699 {
Bram Moolenaarca06da92018-07-01 15:12:05 +02001700 int notnil, i = 0;
1701 typval_T v;
Bram Moolenaar17413672018-07-14 20:49:42 +02001702 do
1703 {
Bram Moolenaarca06da92018-07-01 15:12:05 +02001704 lua_rawgeti(L, 1, ++i);
1705 notnil = !lua_isnil(L, -1);
Bram Moolenaar17413672018-07-14 20:49:42 +02001706 if (notnil)
1707 {
Bram Moolenaarca06da92018-07-01 15:12:05 +02001708 luaV_checktypval(L, -1, &v, "vim.list");
1709 list_append_tv(l, &v);
Bram Moolenaar713bf9e92019-03-16 16:38:41 +01001710 clear_tv(&v);
Bram Moolenaarca06da92018-07-01 15:12:05 +02001711 }
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001712 lua_pop(L, 1); // value
Bram Moolenaarca06da92018-07-01 15:12:05 +02001713 } while (notnil);
1714 }
1715 }
Bram Moolenaar1dced572012-04-05 16:54:08 +02001716 return 1;
1717}
1718
1719 static int
1720luaV_dict(lua_State *L)
1721{
Bram Moolenaarca06da92018-07-01 15:12:05 +02001722 dict_T *d;
1723 int initarg = !lua_isnoneornil(L, 1);
1724
1725 if (initarg && lua_type(L, 1) != LUA_TTABLE)
1726 luaL_error(L, "table expected, got %s", luaL_typename(L, 1));
1727 d = dict_alloc();
Bram Moolenaar1dced572012-04-05 16:54:08 +02001728 if (d == NULL)
1729 lua_pushnil(L);
1730 else
Bram Moolenaarca06da92018-07-01 15:12:05 +02001731 {
Bram Moolenaar1dced572012-04-05 16:54:08 +02001732 luaV_newdict(L, d);
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001733 if (initarg) // traverse table to init dict
Bram Moolenaarca06da92018-07-01 15:12:05 +02001734 {
1735 lua_pushnil(L);
1736 while (lua_next(L, 1))
1737 {
1738 char_u *key;
1739 dictitem_T *di;
1740 typval_T v;
Bram Moolenaard6ef5f92018-07-13 22:08:23 +02001741
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001742 lua_pushvalue(L, -2); // dup key in case it's a number
Bram Moolenaarca06da92018-07-01 15:12:05 +02001743 key = (char_u *) lua_tostring(L, -1);
Bram Moolenaard6ef5f92018-07-13 22:08:23 +02001744 if (key == NULL)
1745 {
1746 lua_pushnil(L);
1747 return 1;
1748 }
1749 if (*key == NUL)
Bram Moolenaarca06da92018-07-01 15:12:05 +02001750 luaL_error(L, "table has empty key");
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001751 luaV_checktypval(L, -2, &v, "vim.dict"); // value
Bram Moolenaarca06da92018-07-01 15:12:05 +02001752 di = dictitem_alloc(key);
Bram Moolenaard6ef5f92018-07-13 22:08:23 +02001753 if (di == NULL || dict_add(d, di) == FAIL)
1754 {
Bram Moolenaarca06da92018-07-01 15:12:05 +02001755 vim_free(di);
1756 lua_pushnil(L);
1757 return 1;
1758 }
1759 copy_tv(&v, &di->di_tv);
1760 clear_tv(&v);
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001761 lua_pop(L, 2); // key copy and value
Bram Moolenaarca06da92018-07-01 15:12:05 +02001762 }
1763 }
1764 }
1765 return 1;
1766}
1767
1768 static int
Bram Moolenaarb7828692019-03-23 13:57:02 +01001769luaV_blob(lua_State *L)
1770{
1771 blob_T *b;
1772 int initarg = !lua_isnoneornil(L, 1);
1773
1774 if (initarg && !lua_isstring(L, 1))
1775 luaL_error(L, "string expected, got %s", luaL_typename(L, 1));
1776 b = blob_alloc();
1777 if (b == NULL)
1778 lua_pushnil(L);
1779 else
1780 {
1781 luaV_newblob(L, b);
1782 if (initarg)
1783 {
1784 size_t i, l = 0;
1785 const char *s = lua_tolstring(L, 1, &l);
1786
Bram Moolenaar5f1d3ae2020-02-11 22:37:35 +01001787 if (ga_grow(&b->bv_ga, (int)l) == OK)
Bram Moolenaar6fb5c972019-03-26 21:44:20 +01001788 for (i = 0; i < l; ++i)
1789 ga_append(&b->bv_ga, s[i]);
Bram Moolenaarb7828692019-03-23 13:57:02 +01001790 }
1791 }
1792 return 1;
1793}
1794
1795 static int
Bram Moolenaarca06da92018-07-01 15:12:05 +02001796luaV_funcref(lua_State *L)
1797{
1798 const char *name = luaL_checkstring(L, 1);
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001799 // note: not checking if function exists (needs function_exists)
Bram Moolenaarca06da92018-07-01 15:12:05 +02001800 if (name == NULL || *name == NUL || VIM_ISDIGIT(*name))
1801 luaL_error(L, "invalid function name: %s", name);
1802 luaV_newfuncref(L, (char_u *) name);
Bram Moolenaar1dced572012-04-05 16:54:08 +02001803 return 1;
1804}
1805
1806 static int
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001807luaV_buffer(lua_State *L)
1808{
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001809 buf_T *buf;
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001810 if (lua_isstring(L, 1)) // get by number or name?
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001811 {
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001812 if (lua_isnumber(L, 1)) // by number?
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001813 {
1814 int n = lua_tointeger(L, 1);
Bram Moolenaar29323592016-07-24 22:04:11 +02001815 FOR_ALL_BUFFERS(buf)
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001816 if (buf->b_fnum == n) break;
1817 }
Bram Moolenaarca06da92018-07-01 15:12:05 +02001818 else // by name
1819 {
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001820 size_t l;
1821 const char *s = lua_tolstring(L, 1, &l);
Bram Moolenaar29323592016-07-24 22:04:11 +02001822 FOR_ALL_BUFFERS(buf)
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001823 {
1824 if (buf->b_ffname == NULL || buf->b_sfname == NULL)
1825 {
1826 if (l == 0) break;
1827 }
Bram Moolenaar0d2e4fc2010-07-18 12:35:47 +02001828 else if (strncmp(s, (char *)buf->b_ffname, l) == 0
1829 || strncmp(s, (char *)buf->b_sfname, l) == 0)
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001830 break;
1831 }
1832 }
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001833 }
Bram Moolenaar1dced572012-04-05 16:54:08 +02001834 else
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001835 buf = (lua_toboolean(L, 1)) ? firstbuf : curbuf; // first buffer?
Bram Moolenaar1dced572012-04-05 16:54:08 +02001836 luaV_pushbuffer(L, buf);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001837 return 1;
1838}
1839
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001840 static int
1841luaV_window(lua_State *L)
1842{
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001843 win_T *win;
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001844 if (lua_isnumber(L, 1)) // get by number?
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001845 {
1846 int n = lua_tointeger(L, 1);
1847 for (win = firstwin; win != NULL; win = win->w_next, n--)
1848 if (n == 1) break;
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001849 }
Bram Moolenaar1dced572012-04-05 16:54:08 +02001850 else
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001851 win = (lua_toboolean(L, 1)) ? firstwin : curwin; // first window?
Bram Moolenaar1dced572012-04-05 16:54:08 +02001852 luaV_pushwindow(L, win);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001853 return 1;
1854}
1855
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001856 static int
1857luaV_open(lua_State *L)
1858{
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001859 char_u *s = NULL;
1860#ifdef HAVE_SANDBOX
1861 luaV_checksandbox(L);
1862#endif
1863 if (lua_isstring(L, 1)) s = (char_u *) lua_tostring(L, 1);
Bram Moolenaarfa263a52011-12-08 16:00:16 +01001864 luaV_pushbuffer(L, buflist_new(s, NULL, 1L, BLN_LISTED));
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001865 return 1;
1866}
1867
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001868 static int
Bram Moolenaar1dced572012-04-05 16:54:08 +02001869luaV_type(lua_State *L)
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001870{
Bram Moolenaar1dced572012-04-05 16:54:08 +02001871 luaL_checkany(L, 1);
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001872 if (lua_type(L, 1) == LUA_TUSERDATA) // check vim udata?
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001873 {
Bram Moolenaar1dced572012-04-05 16:54:08 +02001874 lua_settop(L, 1);
1875 if (lua_getmetatable(L, 1))
1876 {
1877 luaV_getfield(L, LUAVIM_LIST);
1878 if (lua_rawequal(L, -1, 2))
1879 {
1880 lua_pushstring(L, "list");
1881 return 1;
1882 }
1883 luaV_getfield(L, LUAVIM_DICT);
1884 if (lua_rawequal(L, -1, 2))
1885 {
1886 lua_pushstring(L, "dict");
1887 return 1;
1888 }
Bram Moolenaarb7828692019-03-23 13:57:02 +01001889 luaV_getfield(L, LUAVIM_BLOB);
1890 if (lua_rawequal(L, -1, 2))
1891 {
1892 lua_pushstring(L, "blob");
1893 return 1;
1894 }
Bram Moolenaarca06da92018-07-01 15:12:05 +02001895 luaV_getfield(L, LUAVIM_FUNCREF);
1896 if (lua_rawequal(L, -1, 2))
1897 {
1898 lua_pushstring(L, "funcref");
1899 return 1;
1900 }
Bram Moolenaar1dced572012-04-05 16:54:08 +02001901 luaV_getfield(L, LUAVIM_BUFFER);
1902 if (lua_rawequal(L, -1, 2))
1903 {
1904 lua_pushstring(L, "buffer");
1905 return 1;
1906 }
1907 luaV_getfield(L, LUAVIM_WINDOW);
1908 if (lua_rawequal(L, -1, 2))
1909 {
1910 lua_pushstring(L, "window");
1911 return 1;
1912 }
1913 }
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001914 }
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001915 lua_pushstring(L, luaL_typename(L, 1)); // fallback
Bram Moolenaar1dced572012-04-05 16:54:08 +02001916 return 1;
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001917}
1918
Bram Moolenaareb04f082020-05-17 14:32:35 +02001919 static int
1920luaV_call(lua_State *L)
1921{
1922 int argc = lua_gettop(L) - 1;
1923 size_t funcname_len;
1924 char_u *funcname;
1925 char *error = NULL;
1926 typval_T rettv;
1927 typval_T argv[MAX_FUNC_ARGS + 1];
1928 int i = 0;
1929
1930 if (argc > MAX_FUNC_ARGS)
1931 return luaL_error(L, "Function called with too many arguments");
1932
1933 funcname = (char_u *)luaL_checklstring(L, 1, &funcname_len);
1934
1935 for (; i < argc; i++)
1936 {
1937 if (luaV_totypval(L, i + 2, &argv[i]) == FAIL)
1938 {
1939 error = "lua: cannot convert value";
1940 goto free_vim_args;
1941 }
1942 }
1943
1944 argv[argc].v_type = VAR_UNKNOWN;
1945
1946 if (call_vim_function(funcname, argc, argv, &rettv) == FAIL)
1947 {
1948 error = "lua: call_vim_function failed";
1949 goto free_vim_args;
1950 }
1951
1952 luaV_pushtypval(L, &rettv);
1953 clear_tv(&rettv);
1954
1955free_vim_args:
1956 while (i > 0)
1957 clear_tv(&argv[--i]);
1958
1959 if (error == NULL)
1960 return 1;
1961 else
1962 return luaL_error(L, error);
1963}
1964
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001965static const luaL_Reg luaV_module[] = {
1966 {"command", luaV_command},
1967 {"eval", luaV_eval},
1968 {"beep", luaV_beep},
1969 {"line", luaV_line},
Bram Moolenaar1dced572012-04-05 16:54:08 +02001970 {"list", luaV_list},
1971 {"dict", luaV_dict},
Bram Moolenaarb7828692019-03-23 13:57:02 +01001972 {"blob", luaV_blob},
Bram Moolenaarca06da92018-07-01 15:12:05 +02001973 {"funcref", luaV_funcref},
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001974 {"buffer", luaV_buffer},
1975 {"window", luaV_window},
1976 {"open", luaV_open},
Bram Moolenaar1dced572012-04-05 16:54:08 +02001977 {"type", luaV_type},
Bram Moolenaareb04f082020-05-17 14:32:35 +02001978 {"call", luaV_call},
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001979 {NULL, NULL}
1980};
1981
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001982/*
1983 * for freeing list, dict, buffer and window objects; lightuserdata as arg
1984 */
Bram Moolenaar1dced572012-04-05 16:54:08 +02001985 static int
1986luaV_free(lua_State *L)
1987{
1988 lua_pushnil(L);
1989 luaV_setudata(L, lua_touserdata(L, 1));
1990 return 0;
1991}
1992
1993 static int
Bram Moolenaar4eefe472019-03-19 21:59:19 +01001994luaV_luaeval(lua_State *L)
Bram Moolenaar1dced572012-04-05 16:54:08 +02001995{
1996 luaL_Buffer b;
1997 size_t l;
1998 const char *str = lua_tolstring(L, 1, &l);
1999 typval_T *arg = (typval_T *) lua_touserdata(L, 2);
2000 typval_T *rettv = (typval_T *) lua_touserdata(L, 3);
2001 luaL_buffinit(L, &b);
2002 luaL_addlstring(&b, LUAVIM_EVALHEADER, sizeof(LUAVIM_EVALHEADER) - 1);
2003 luaL_addlstring(&b, str, l);
2004 luaL_pushresult(&b);
2005 str = lua_tolstring(L, -1, &l);
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002006 if (luaL_loadbuffer(L, str, l, LUAVIM_EVALNAME)) // compile error?
Bram Moolenaar1dced572012-04-05 16:54:08 +02002007 {
2008 luaV_emsg(L);
2009 return 0;
2010 }
2011 luaV_pushtypval(L, arg);
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002012 if (lua_pcall(L, 1, 1, 0)) // running error?
Bram Moolenaar1dced572012-04-05 16:54:08 +02002013 {
2014 luaV_emsg(L);
2015 return 0;
2016 }
Bram Moolenaarca06da92018-07-01 15:12:05 +02002017 if (luaV_totypval(L, -1, rettv) == FAIL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002018 emsg("luaeval: cannot convert value");
Bram Moolenaarf554a322015-02-04 23:08:01 +01002019 return 0;
Bram Moolenaar1dced572012-04-05 16:54:08 +02002020}
2021
2022 static int
Bram Moolenaar4eefe472019-03-19 21:59:19 +01002023luaV_setref(lua_State *L)
Bram Moolenaar1dced572012-04-05 16:54:08 +02002024{
Bram Moolenaar4eefe472019-03-19 21:59:19 +01002025 int copyID = lua_tointeger(L, 1);
2026 int abort = FALSE;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01002027
Bram Moolenaar1dced572012-04-05 16:54:08 +02002028 luaV_getfield(L, LUAVIM_LIST);
2029 luaV_getfield(L, LUAVIM_DICT);
Bram Moolenaar4eefe472019-03-19 21:59:19 +01002030 luaV_getfield(L, LUAVIM_FUNCREF);
Bram Moolenaar1dced572012-04-05 16:54:08 +02002031 lua_pushnil(L);
Bram Moolenaar4eefe472019-03-19 21:59:19 +01002032 // traverse cache table
Bram Moolenaarb84634d2015-02-04 22:02:37 +01002033 while (!abort && lua_next(L, lua_upvalueindex(1)) != 0)
Bram Moolenaar1dced572012-04-05 16:54:08 +02002034 {
2035 lua_getmetatable(L, -1);
Bram Moolenaar4eefe472019-03-19 21:59:19 +01002036 if (lua_rawequal(L, -1, 2)) // list?
Bram Moolenaar1dced572012-04-05 16:54:08 +02002037 {
Bram Moolenaar4eefe472019-03-19 21:59:19 +01002038 list_T *l = (list_T *)lua_touserdata(L, 5); // key
2039
Bram Moolenaar7be3ab22019-06-23 01:46:15 +02002040 abort = set_ref_in_list(l, copyID);
Bram Moolenaar1dced572012-04-05 16:54:08 +02002041 }
Bram Moolenaar4eefe472019-03-19 21:59:19 +01002042 else if (lua_rawequal(L, -1, 3)) // dict?
Bram Moolenaar1dced572012-04-05 16:54:08 +02002043 {
Bram Moolenaar4eefe472019-03-19 21:59:19 +01002044 dict_T *d = (dict_T *)lua_touserdata(L, 5); // key
2045
Bram Moolenaar7be3ab22019-06-23 01:46:15 +02002046 abort = set_ref_in_dict(d, copyID);
Bram Moolenaar1dced572012-04-05 16:54:08 +02002047 }
Bram Moolenaar4eefe472019-03-19 21:59:19 +01002048 else if (lua_rawequal(L, -1, 4)) // funcref?
2049 {
2050 luaV_Funcref *f = (luaV_Funcref *)lua_touserdata(L, 5); // key
2051
Bram Moolenaar7be3ab22019-06-23 01:46:15 +02002052 abort = set_ref_in_dict(f->self, copyID);
Bram Moolenaar4eefe472019-03-19 21:59:19 +01002053 }
2054 lua_pop(L, 2); // metatable and value
Bram Moolenaar1dced572012-04-05 16:54:08 +02002055 }
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01002056 lua_pushinteger(L, abort);
Bram Moolenaarf554a322015-02-04 23:08:01 +01002057 return 1;
Bram Moolenaar1dced572012-04-05 16:54:08 +02002058}
2059
Bram Moolenaareb04f082020-05-17 14:32:35 +02002060#define LUA_VIM_FN_CODE \
2061 "vim.fn = setmetatable({}, {"\
2062 " __index = function (t, key)"\
2063 " local function _fn(...)"\
2064 " return vim.call(key, ...)"\
2065 " end"\
2066 " t[key] = _fn"\
2067 " return _fn"\
2068 " end"\
2069 "})"
2070
Bram Moolenaar55d5c032010-07-17 23:52:29 +02002071 static int
2072luaopen_vim(lua_State *L)
2073{
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002074 // set cache table
Bram Moolenaar0ba04292010-07-14 23:23:17 +02002075 lua_newtable(L);
2076 lua_newtable(L);
Bram Moolenaar1dced572012-04-05 16:54:08 +02002077 lua_pushstring(L, "v");
Bram Moolenaar0ba04292010-07-14 23:23:17 +02002078 lua_setfield(L, -2, "__mode");
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002079 lua_setmetatable(L, -2); // cache is weak-valued
2080 // print
Bram Moolenaar0ba04292010-07-14 23:23:17 +02002081 lua_pushcfunction(L, luaV_print);
2082 lua_setglobal(L, "print");
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002083 // debug.debug
Bram Moolenaar38e2b062011-09-21 17:15:39 +02002084 lua_getglobal(L, "debug");
2085 lua_pushcfunction(L, luaV_debug);
2086 lua_setfield(L, -2, "debug");
2087 lua_pop(L, 1);
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002088 // free
Bram Moolenaar0ba04292010-07-14 23:23:17 +02002089 lua_pushlightuserdata(L, (void *) LUAVIM_FREE);
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002090 lua_pushvalue(L, 1); // cache table
Bram Moolenaar1dced572012-04-05 16:54:08 +02002091 lua_pushcclosure(L, luaV_free, 1);
2092 lua_rawset(L, LUA_REGISTRYINDEX);
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002093 // luaeval
Bram Moolenaar1dced572012-04-05 16:54:08 +02002094 lua_pushlightuserdata(L, (void *) LUAVIM_LUAEVAL);
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002095 lua_pushvalue(L, 1); // cache table
Bram Moolenaar1dced572012-04-05 16:54:08 +02002096 lua_pushcclosure(L, luaV_luaeval, 1);
2097 lua_rawset(L, LUA_REGISTRYINDEX);
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002098 // setref
Bram Moolenaar1dced572012-04-05 16:54:08 +02002099 lua_pushlightuserdata(L, (void *) LUAVIM_SETREF);
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002100 lua_pushvalue(L, 1); // cache table
Bram Moolenaar1dced572012-04-05 16:54:08 +02002101 lua_pushcclosure(L, luaV_setref, 1);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02002102 lua_rawset(L, LUA_REGISTRYINDEX);
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002103 // register
Bram Moolenaar1dced572012-04-05 16:54:08 +02002104 luaV_newmetatable(L, LUAVIM_LIST);
2105 lua_pushvalue(L, 1);
2106 luaV_openlib(L, luaV_List_mt, 1);
2107 luaV_newmetatable(L, LUAVIM_DICT);
2108 lua_pushvalue(L, 1);
2109 luaV_openlib(L, luaV_Dict_mt, 1);
Bram Moolenaarb7828692019-03-23 13:57:02 +01002110 luaV_newmetatable(L, LUAVIM_BLOB);
2111 lua_pushvalue(L, 1);
2112 luaV_openlib(L, luaV_Blob_mt, 1);
Bram Moolenaarca06da92018-07-01 15:12:05 +02002113 luaV_newmetatable(L, LUAVIM_FUNCREF);
2114 lua_pushvalue(L, 1);
2115 luaV_openlib(L, luaV_Funcref_mt, 1);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02002116 luaV_newmetatable(L, LUAVIM_BUFFER);
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002117 lua_pushvalue(L, 1); // cache table
Bram Moolenaar1dced572012-04-05 16:54:08 +02002118 luaV_openlib(L, luaV_Buffer_mt, 1);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02002119 luaV_newmetatable(L, LUAVIM_WINDOW);
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002120 lua_pushvalue(L, 1); // cache table
Bram Moolenaar1dced572012-04-05 16:54:08 +02002121 luaV_openlib(L, luaV_Window_mt, 1);
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002122 lua_newtable(L); // vim table
2123 lua_pushvalue(L, 1); // cache table
Bram Moolenaar1dced572012-04-05 16:54:08 +02002124 luaV_openlib(L, luaV_module, 1);
2125 lua_setglobal(L, LUAVIM_NAME);
Bram Moolenaareb04f082020-05-17 14:32:35 +02002126 // custom code
2127 luaL_dostring(L, LUA_VIM_FN_CODE);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02002128 return 0;
2129}
2130
Bram Moolenaar55d5c032010-07-17 23:52:29 +02002131 static lua_State *
2132luaV_newstate(void)
2133{
Bram Moolenaar0ba04292010-07-14 23:23:17 +02002134 lua_State *L = luaL_newstate();
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002135 luaL_openlibs(L); // core libs
2136 lua_pushcfunction(L, luaopen_vim); // vim
Bram Moolenaar0ba04292010-07-14 23:23:17 +02002137 lua_call(L, 0, 0);
2138 return L;
2139}
2140
Bram Moolenaar55d5c032010-07-17 23:52:29 +02002141 static void
2142luaV_setrange(lua_State *L, int line1, int line2)
2143{
Bram Moolenaar0ba04292010-07-14 23:23:17 +02002144 lua_getglobal(L, LUAVIM_NAME);
2145 lua_pushinteger(L, line1);
2146 lua_setfield(L, -2, "firstline");
2147 lua_pushinteger(L, line2);
2148 lua_setfield(L, -2, "lastline");
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002149 lua_pop(L, 1); // vim table
Bram Moolenaar0ba04292010-07-14 23:23:17 +02002150}
2151
2152
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002153// ======= Interface =======
Bram Moolenaar0ba04292010-07-14 23:23:17 +02002154
2155static lua_State *L = NULL;
2156
Bram Moolenaar55d5c032010-07-17 23:52:29 +02002157 static int
Bram Moolenaar1dced572012-04-05 16:54:08 +02002158lua_isopen(void)
Bram Moolenaar2bd6a1b2010-08-12 22:14:01 +02002159{
2160 return L != NULL;
2161}
2162
2163 static int
Bram Moolenaar55d5c032010-07-17 23:52:29 +02002164lua_init(void)
2165{
Bram Moolenaar1dced572012-04-05 16:54:08 +02002166 if (!lua_isopen())
Bram Moolenaar55d5c032010-07-17 23:52:29 +02002167 {
Bram Moolenaar0ba04292010-07-14 23:23:17 +02002168#ifdef DYNAMIC_LUA
Bram Moolenaar55d5c032010-07-17 23:52:29 +02002169 if (!lua_enabled(TRUE))
2170 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002171 emsg(_("Lua library cannot be loaded."));
Bram Moolenaar55d5c032010-07-17 23:52:29 +02002172 return FAIL;
2173 }
Bram Moolenaar0ba04292010-07-14 23:23:17 +02002174#endif
Bram Moolenaar55d5c032010-07-17 23:52:29 +02002175 L = luaV_newstate();
Bram Moolenaar0ba04292010-07-14 23:23:17 +02002176 }
2177 return OK;
2178}
2179
Bram Moolenaar55d5c032010-07-17 23:52:29 +02002180 void
2181lua_end(void)
2182{
Bram Moolenaar1dced572012-04-05 16:54:08 +02002183 if (lua_isopen())
Bram Moolenaar55d5c032010-07-17 23:52:29 +02002184 {
2185 lua_close(L);
2186 L = NULL;
Bram Moolenaar0ba04292010-07-14 23:23:17 +02002187 }
2188}
2189
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002190/*
2191 * ex commands
2192 */
Bram Moolenaar55d5c032010-07-17 23:52:29 +02002193 void
2194ex_lua(exarg_T *eap)
2195{
Bram Moolenaar0ba04292010-07-14 23:23:17 +02002196 char *script;
2197 if (lua_init() == FAIL) return;
2198 script = (char *) script_get(eap, eap->arg);
Bram Moolenaar55d5c032010-07-17 23:52:29 +02002199 if (!eap->skip)
2200 {
2201 char *s = (script) ? script : (char *) eap->arg;
2202 luaV_setrange(L, eap->line1, eap->line2);
2203 if (luaL_loadbuffer(L, s, strlen(s), LUAVIM_CHUNKNAME)
2204 || lua_pcall(L, 0, 0, 0))
2205 luaV_emsg(L);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02002206 }
2207 if (script != NULL) vim_free(script);
2208}
2209
Bram Moolenaar55d5c032010-07-17 23:52:29 +02002210 void
2211ex_luado(exarg_T *eap)
2212{
Bram Moolenaar0ba04292010-07-14 23:23:17 +02002213 linenr_T l;
2214 const char *s = (const char *) eap->arg;
2215 luaL_Buffer b;
2216 size_t len;
Bram Moolenaard58f03b2017-01-29 22:48:45 +01002217 buf_T *was_curbuf = curbuf;
2218
Bram Moolenaar0ba04292010-07-14 23:23:17 +02002219 if (lua_init() == FAIL) return;
Bram Moolenaar55d5c032010-07-17 23:52:29 +02002220 if (u_save(eap->line1 - 1, eap->line2 + 1) == FAIL)
2221 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002222 emsg(_("cannot save undo information"));
Bram Moolenaar55d5c032010-07-17 23:52:29 +02002223 return;
Bram Moolenaar0ba04292010-07-14 23:23:17 +02002224 }
2225 luaV_setrange(L, eap->line1, eap->line2);
2226 luaL_buffinit(L, &b);
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002227 luaL_addlstring(&b, "return function(line, linenr) ", 30); // header
Bram Moolenaar0ba04292010-07-14 23:23:17 +02002228 luaL_addlstring(&b, s, strlen(s));
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002229 luaL_addlstring(&b, " end", 4); // footer
Bram Moolenaar0ba04292010-07-14 23:23:17 +02002230 luaL_pushresult(&b);
2231 s = lua_tolstring(L, -1, &len);
Bram Moolenaar55d5c032010-07-17 23:52:29 +02002232 if (luaL_loadbuffer(L, s, len, LUAVIM_CHUNKNAME))
2233 {
2234 luaV_emsg(L);
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002235 lua_pop(L, 1); // function body
Bram Moolenaar55d5c032010-07-17 23:52:29 +02002236 return;
Bram Moolenaar0ba04292010-07-14 23:23:17 +02002237 }
2238 lua_call(L, 0, 1);
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002239 lua_replace(L, -2); // function -> body
Bram Moolenaar55d5c032010-07-17 23:52:29 +02002240 for (l = eap->line1; l <= eap->line2; l++)
2241 {
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002242 // Check the line number, the command my have deleted lines.
Bram Moolenaard58f03b2017-01-29 22:48:45 +01002243 if (l > curbuf->b_ml.ml_line_count)
2244 break;
2245
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002246 lua_pushvalue(L, -1); // function
2247 luaV_pushline(L, curbuf, l); // current line as arg
2248 lua_pushinteger(L, l); // current line number as arg
Bram Moolenaarbd2f3c32012-04-06 14:31:00 +02002249 if (lua_pcall(L, 2, 1, 0))
Bram Moolenaar55d5c032010-07-17 23:52:29 +02002250 {
2251 luaV_emsg(L);
2252 break;
2253 }
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002254 // Catch the command switching to another buffer.
Bram Moolenaard58f03b2017-01-29 22:48:45 +01002255 if (curbuf != was_curbuf)
2256 break;
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002257 if (lua_isstring(L, -1)) // update line?
Bram Moolenaar55d5c032010-07-17 23:52:29 +02002258 {
Bram Moolenaar0ba04292010-07-14 23:23:17 +02002259#ifdef HAVE_SANDBOX
Bram Moolenaar55d5c032010-07-17 23:52:29 +02002260 luaV_checksandbox(L);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02002261#endif
Bram Moolenaar55d5c032010-07-17 23:52:29 +02002262 ml_replace(l, luaV_toline(L, -1), TRUE);
2263 changed_bytes(l, 0);
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002264 lua_pop(L, 1); // result from luaV_toline
Bram Moolenaar55d5c032010-07-17 23:52:29 +02002265 }
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002266 lua_pop(L, 1); // line
Bram Moolenaar0ba04292010-07-14 23:23:17 +02002267 }
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002268 lua_pop(L, 1); // function
Bram Moolenaar0ba04292010-07-14 23:23:17 +02002269 check_cursor();
2270 update_screen(NOT_VALID);
2271}
2272
Bram Moolenaar55d5c032010-07-17 23:52:29 +02002273 void
2274ex_luafile(exarg_T *eap)
2275{
2276 if (lua_init() == FAIL)
2277 return;
2278 if (!eap->skip)
2279 {
2280 luaV_setrange(L, eap->line1, eap->line2);
2281 if (luaL_loadfile(L, (char *) eap->arg) || lua_pcall(L, 0, 0, 0))
2282 luaV_emsg(L);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02002283 }
2284}
2285
Bram Moolenaar1dced572012-04-05 16:54:08 +02002286#define luaV_freetype(typ,tname) \
2287 void \
2288 lua_##tname##_free(typ *o) \
2289 { \
2290 if (!lua_isopen()) return; \
2291 luaV_getfield(L, LUAVIM_FREE); \
2292 lua_pushlightuserdata(L, (void *) o); \
2293 lua_call(L, 1, 0); \
2294 }
2295
2296luaV_freetype(buf_T, buffer)
2297luaV_freetype(win_T, window)
2298
Bram Moolenaar55d5c032010-07-17 23:52:29 +02002299 void
Bram Moolenaar4eefe472019-03-19 21:59:19 +01002300do_luaeval(char_u *str, typval_T *arg, typval_T *rettv)
Bram Moolenaar55d5c032010-07-17 23:52:29 +02002301{
Bram Moolenaar1dced572012-04-05 16:54:08 +02002302 lua_init();
2303 luaV_getfield(L, LUAVIM_LUAEVAL);
2304 lua_pushstring(L, (char *) str);
2305 lua_pushlightuserdata(L, (void *) arg);
2306 lua_pushlightuserdata(L, (void *) rettv);
2307 lua_call(L, 3, 0);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02002308}
2309
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01002310 int
Bram Moolenaar4eefe472019-03-19 21:59:19 +01002311set_ref_in_lua(int copyID)
Bram Moolenaar55d5c032010-07-17 23:52:29 +02002312{
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01002313 int aborted = 0;
2314
2315 if (lua_isopen())
2316 {
2317 luaV_getfield(L, LUAVIM_SETREF);
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002318 // call the function with 1 arg, getting 1 result back
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01002319 lua_pushinteger(L, copyID);
2320 lua_call(L, 1, 1);
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002321 // get the result
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01002322 aborted = lua_tointeger(L, -1);
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002323 // pop result off the stack
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01002324 lua_pop(L, 1);
2325 }
2326 return aborted;
Bram Moolenaar0ba04292010-07-14 23:23:17 +02002327}
2328
2329#endif