blob: 19842912fbbaff4a445c951532301223c3627b24 [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
571 tv->v_type = VAR_FLOAT;
572 tv->vval.v_float = (float_T) lua_tonumber(L, pos);
573#else
574 tv->v_type = VAR_NUMBER;
575 tv->vval.v_number = (varnumber_T) lua_tointeger(L, pos);
576#endif
577 break;
Bram Moolenaarca06da92018-07-01 15:12:05 +0200578 case LUA_TUSERDATA:
579 {
Bram Moolenaar1dced572012-04-05 16:54:08 +0200580 void *p = lua_touserdata(L, pos);
Bram Moolenaarca06da92018-07-01 15:12:05 +0200581
Bram Moolenaarb7828692019-03-23 13:57:02 +0100582 if (lua_getmetatable(L, pos)) // has metatable?
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200583 {
Bram Moolenaarb7828692019-03-23 13:57:02 +0100584 // check list
Bram Moolenaar1dced572012-04-05 16:54:08 +0200585 luaV_getfield(L, LUAVIM_LIST);
586 if (lua_rawequal(L, -1, -2))
Bram Moolenaar2334b6d2010-07-22 21:32:16 +0200587 {
Bram Moolenaar1dced572012-04-05 16:54:08 +0200588 tv->v_type = VAR_LIST;
589 tv->vval.v_list = *((luaV_List *) p);
590 ++tv->vval.v_list->lv_refcount;
Bram Moolenaarb7828692019-03-23 13:57:02 +0100591 lua_pop(L, 2); // MTs
Bram Moolenaarca06da92018-07-01 15:12:05 +0200592 break;
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200593 }
Bram Moolenaarb7828692019-03-23 13:57:02 +0100594 // check dict
Bram Moolenaar1dced572012-04-05 16:54:08 +0200595 luaV_getfield(L, LUAVIM_DICT);
596 if (lua_rawequal(L, -1, -3))
597 {
598 tv->v_type = VAR_DICT;
599 tv->vval.v_dict = *((luaV_Dict *) p);
600 ++tv->vval.v_dict->dv_refcount;
Bram Moolenaarb7828692019-03-23 13:57:02 +0100601 lua_pop(L, 3); // MTs
Bram Moolenaarca06da92018-07-01 15:12:05 +0200602 break;
Bram Moolenaar1dced572012-04-05 16:54:08 +0200603 }
Bram Moolenaarb7828692019-03-23 13:57:02 +0100604 // check blob
605 luaV_getfield(L, LUAVIM_BLOB);
Bram Moolenaarca06da92018-07-01 15:12:05 +0200606 if (lua_rawequal(L, -1, -4))
607 {
Bram Moolenaarb7828692019-03-23 13:57:02 +0100608 tv->v_type = VAR_BLOB;
609 tv->vval.v_blob = *((luaV_Blob *) p);
610 ++tv->vval.v_blob->bv_refcount;
611 lua_pop(L, 4); // MTs
612 break;
613 }
614 // check funcref
615 luaV_getfield(L, LUAVIM_FUNCREF);
616 if (lua_rawequal(L, -1, -5))
617 {
Bram Moolenaarca06da92018-07-01 15:12:05 +0200618 luaV_Funcref *f = (luaV_Funcref *) p;
Bram Moolenaar4eefe472019-03-19 21:59:19 +0100619 func_ref(f->name);
620 tv->v_type = VAR_FUNC;
621 tv->vval.v_string = vim_strsave(f->name);
Bram Moolenaarb7828692019-03-23 13:57:02 +0100622 lua_pop(L, 5); // MTs
Bram Moolenaarca06da92018-07-01 15:12:05 +0200623 break;
624 }
Bram Moolenaarb7828692019-03-23 13:57:02 +0100625 lua_pop(L, 4); // MTs
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200626 }
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200627 }
Bram Moolenaarb7828692019-03-23 13:57:02 +0100628 // FALLTHROUGH
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200629 default:
Bram Moolenaar1dced572012-04-05 16:54:08 +0200630 tv->v_type = VAR_NUMBER;
631 tv->vval.v_number = 0;
Bram Moolenaarca06da92018-07-01 15:12:05 +0200632 status = FAIL;
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200633 }
Bram Moolenaarca06da92018-07-01 15:12:05 +0200634 return status;
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200635}
636
Bram Moolenaar2ab2e862019-12-04 21:24:53 +0100637/*
638 * similar to luaL_addlstring, but replaces \0 with \n if toline and
639 * \n with \0 otherwise
640 */
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200641 static void
642luaV_addlstring(luaL_Buffer *b, const char *s, size_t l, int toline)
643{
644 while (l--)
645 {
646 if (*s == '\0' && toline)
647 luaL_addchar(b, '\n');
648 else if (*s == '\n' && !toline)
649 luaL_addchar(b, '\0');
650 else
651 luaL_addchar(b, *s);
652 s++;
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200653 }
654}
655
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200656 static void
657luaV_pushline(lua_State *L, buf_T *buf, linenr_T n)
658{
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200659 const char *s = (const char *) ml_get_buf(buf, n, FALSE);
660 luaL_Buffer b;
661 luaL_buffinit(L, &b);
662 luaV_addlstring(&b, s, strlen(s), 0);
663 luaL_pushresult(&b);
664}
665
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200666 static char_u *
667luaV_toline(lua_State *L, int pos)
668{
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200669 size_t l;
670 const char *s = lua_tolstring(L, pos, &l);
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200671
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200672 luaL_Buffer b;
673 luaL_buffinit(L, &b);
674 luaV_addlstring(&b, s, l, 1);
675 luaL_pushresult(&b);
676 return (char_u *) lua_tostring(L, -1);
677}
678
Bram Moolenaar2ab2e862019-12-04 21:24:53 +0100679/*
680 * pops a string s from the top of the stack and calls mf(t) for pieces t of
681 * s separated by newlines
682 */
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200683 static void
684luaV_msgfunc(lua_State *L, msgfunc_T mf)
685{
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200686 luaL_Buffer b;
687 size_t l;
688 const char *p, *s = lua_tolstring(L, -1, &l);
689 luaL_buffinit(L, &b);
690 luaV_addlstring(&b, s, l, 0);
691 luaL_pushresult(&b);
Bram Moolenaar2ab2e862019-12-04 21:24:53 +0100692 // break string
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200693 p = s = lua_tolstring(L, -1, &l);
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200694 while (l--)
695 {
Bram Moolenaar2ab2e862019-12-04 21:24:53 +0100696 if (*p++ == '\0') // break?
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200697 {
698 mf((char_u *) s);
699 s = p;
700 }
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200701 }
702 mf((char_u *) s);
Bram Moolenaar2ab2e862019-12-04 21:24:53 +0100703 lua_pop(L, 2); // original and modified strings
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200704}
705
Bram Moolenaar1dced572012-04-05 16:54:08 +0200706#define luaV_newtype(typ,tname,luatyp,luatname) \
707 static luatyp * \
Bram Moolenaar4eefe472019-03-19 21:59:19 +0100708 luaV_new##tname(lua_State *L, typ *obj) \
Bram Moolenaar1dced572012-04-05 16:54:08 +0200709 { \
710 luatyp *o = (luatyp *) lua_newuserdata(L, sizeof(luatyp)); \
711 *o = obj; \
712 luaV_setudata(L, obj); /* cache[obj] = udata */ \
713 luaV_getfield(L, luatname); \
714 lua_setmetatable(L, -2); \
715 return o; \
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200716 }
Bram Moolenaar1dced572012-04-05 16:54:08 +0200717
718#define luaV_pushtype(typ,tname,luatyp) \
719 static luatyp * \
Bram Moolenaarca06da92018-07-01 15:12:05 +0200720 luaV_push##tname(lua_State *L, typ *obj) \
Bram Moolenaar1dced572012-04-05 16:54:08 +0200721 { \
722 luatyp *o = NULL; \
723 if (obj == NULL) \
724 lua_pushnil(L); \
725 else { \
726 luaV_getudata(L, obj); \
727 if (lua_isnil(L, -1)) /* not interned? */ \
728 { \
729 lua_pop(L, 1); \
730 o = luaV_new##tname(L, obj); \
731 } \
732 else \
733 o = (luatyp *) lua_touserdata(L, -1); \
734 } \
735 return o; \
736 }
737
738#define luaV_type_tostring(tname,luatname) \
739 static int \
Bram Moolenaar4eefe472019-03-19 21:59:19 +0100740 luaV_##tname##_tostring(lua_State *L) \
Bram Moolenaar1dced572012-04-05 16:54:08 +0200741 { \
742 lua_pushfstring(L, "%s: %p", luatname, lua_touserdata(L, 1)); \
743 return 1; \
744 }
745
Bram Moolenaar2ab2e862019-12-04 21:24:53 +0100746// ======= List type =======
Bram Moolenaar1dced572012-04-05 16:54:08 +0200747
748 static luaV_List *
Bram Moolenaar4eefe472019-03-19 21:59:19 +0100749luaV_newlist(lua_State *L, list_T *lis)
Bram Moolenaar1dced572012-04-05 16:54:08 +0200750{
751 luaV_List *l = (luaV_List *) lua_newuserdata(L, sizeof(luaV_List));
752 *l = lis;
Bram Moolenaar2ab2e862019-12-04 21:24:53 +0100753 lis->lv_refcount++; // reference in Lua
754 luaV_setudata(L, lis); // cache[lis] = udata
Bram Moolenaar1dced572012-04-05 16:54:08 +0200755 luaV_getfield(L, LUAVIM_LIST);
756 lua_setmetatable(L, -2);
757 return l;
758}
759
760luaV_pushtype(list_T, list, luaV_List)
761luaV_type_tostring(list, LUAVIM_LIST)
762
763 static int
Bram Moolenaar4eefe472019-03-19 21:59:19 +0100764luaV_list_len(lua_State *L)
Bram Moolenaar1dced572012-04-05 16:54:08 +0200765{
766 list_T *l = luaV_unbox(L, luaV_List, 1);
Bram Moolenaarb7828692019-03-23 13:57:02 +0100767 lua_pushinteger(L, (int) list_len(l));
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200768 return 1;
769}
770
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200771 static int
Bram Moolenaar4eefe472019-03-19 21:59:19 +0100772luaV_list_iter(lua_State *L)
Bram Moolenaar1dced572012-04-05 16:54:08 +0200773{
774 listitem_T *li = (listitem_T *) lua_touserdata(L, lua_upvalueindex(2));
775 if (li == NULL) return 0;
776 luaV_pushtypval(L, &li->li_tv);
777 lua_pushlightuserdata(L, (void *) li->li_next);
778 lua_replace(L, lua_upvalueindex(2));
779 return 1;
780}
781
782 static int
Bram Moolenaar4eefe472019-03-19 21:59:19 +0100783luaV_list_call(lua_State *L)
Bram Moolenaar1dced572012-04-05 16:54:08 +0200784{
785 list_T *l = luaV_unbox(L, luaV_List, 1);
Bram Moolenaar2ab2e862019-12-04 21:24:53 +0100786 lua_pushvalue(L, lua_upvalueindex(1)); // pass cache table along
Bram Moolenaar1dced572012-04-05 16:54:08 +0200787 lua_pushlightuserdata(L, (void *) l->lv_first);
788 lua_pushcclosure(L, luaV_list_iter, 2);
789 return 1;
790}
791
792 static int
Bram Moolenaar4eefe472019-03-19 21:59:19 +0100793luaV_list_index(lua_State *L)
Bram Moolenaar1dced572012-04-05 16:54:08 +0200794{
795 list_T *l = luaV_unbox(L, luaV_List, 1);
Bram Moolenaar2ab2e862019-12-04 21:24:53 +0100796 if (lua_isnumber(L, 2)) // list item?
Bram Moolenaar1dced572012-04-05 16:54:08 +0200797 {
798 listitem_T *li = list_find(l, (long) luaL_checkinteger(L, 2));
799 if (li == NULL)
800 lua_pushnil(L);
801 else
802 luaV_pushtypval(L, &li->li_tv);
803 }
Bram Moolenaar2ab2e862019-12-04 21:24:53 +0100804 else if (lua_isstring(L, 2)) // method?
Bram Moolenaar1dced572012-04-05 16:54:08 +0200805 {
806 const char *s = lua_tostring(L, 2);
807 if (strncmp(s, "add", 3) == 0
Bram Moolenaarb3766472013-04-15 13:49:21 +0200808 || strncmp(s, "insert", 6) == 0)
Bram Moolenaar1dced572012-04-05 16:54:08 +0200809 {
810 lua_getmetatable(L, 1);
811 lua_getfield(L, -1, s);
812 }
813 else
814 lua_pushnil(L);
815 }
816 else
817 lua_pushnil(L);
818 return 1;
819}
820
821 static int
Bram Moolenaar4eefe472019-03-19 21:59:19 +0100822luaV_list_newindex(lua_State *L)
Bram Moolenaar1dced572012-04-05 16:54:08 +0200823{
824 list_T *l = luaV_unbox(L, luaV_List, 1);
825 long n = (long) luaL_checkinteger(L, 2);
826 listitem_T *li;
827 if (l->lv_lock)
828 luaL_error(L, "list is locked");
829 li = list_find(l, n);
830 if (li == NULL) return 0;
Bram Moolenaar2ab2e862019-12-04 21:24:53 +0100831 if (lua_isnil(L, 3)) // remove?
Bram Moolenaar1dced572012-04-05 16:54:08 +0200832 {
Bram Moolenaar3ec7f4e2014-05-07 17:31:37 +0200833 vimlist_remove(l, li, li);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100834 listitem_free(l, li);
Bram Moolenaar1dced572012-04-05 16:54:08 +0200835 }
836 else
837 {
838 typval_T v;
Bram Moolenaarca06da92018-07-01 15:12:05 +0200839 luaV_checktypval(L, 3, &v, "setting list item");
Bram Moolenaar1dced572012-04-05 16:54:08 +0200840 clear_tv(&li->li_tv);
841 copy_tv(&v, &li->li_tv);
Bram Moolenaarb3766472013-04-15 13:49:21 +0200842 clear_tv(&v);
Bram Moolenaar1dced572012-04-05 16:54:08 +0200843 }
844 return 0;
845}
846
847 static int
Bram Moolenaar4eefe472019-03-19 21:59:19 +0100848luaV_list_add(lua_State *L)
Bram Moolenaar1dced572012-04-05 16:54:08 +0200849{
850 luaV_List *lis = luaV_checkudata(L, 1, LUAVIM_LIST);
851 list_T *l = (list_T *) luaV_checkcache(L, (void *) *lis);
Bram Moolenaarb3766472013-04-15 13:49:21 +0200852 typval_T v;
Bram Moolenaar1dced572012-04-05 16:54:08 +0200853 if (l->lv_lock)
854 luaL_error(L, "list is locked");
Bram Moolenaarb3766472013-04-15 13:49:21 +0200855 lua_settop(L, 2);
Bram Moolenaarca06da92018-07-01 15:12:05 +0200856 luaV_checktypval(L, 2, &v, "adding list item");
Bram Moolenaarb3766472013-04-15 13:49:21 +0200857 if (list_append_tv(l, &v) == FAIL)
Bram Moolenaarca06da92018-07-01 15:12:05 +0200858 luaL_error(L, "failed to add item to list");
Bram Moolenaarb3766472013-04-15 13:49:21 +0200859 clear_tv(&v);
Bram Moolenaar1dced572012-04-05 16:54:08 +0200860 lua_settop(L, 1);
861 return 1;
862}
863
864 static int
Bram Moolenaar4eefe472019-03-19 21:59:19 +0100865luaV_list_insert(lua_State *L)
Bram Moolenaar1dced572012-04-05 16:54:08 +0200866{
867 luaV_List *lis = luaV_checkudata(L, 1, LUAVIM_LIST);
868 list_T *l = (list_T *) luaV_checkcache(L, (void *) *lis);
Bram Moolenaar46538ee2015-02-17 16:28:55 +0100869 long pos = (long) luaL_optinteger(L, 3, 0);
Bram Moolenaar1dced572012-04-05 16:54:08 +0200870 listitem_T *li = NULL;
871 typval_T v;
872 if (l->lv_lock)
873 luaL_error(L, "list is locked");
874 if (pos < l->lv_len)
875 {
876 li = list_find(l, pos);
877 if (li == NULL)
878 luaL_error(L, "invalid position");
879 }
880 lua_settop(L, 2);
Bram Moolenaarca06da92018-07-01 15:12:05 +0200881 luaV_checktypval(L, 2, &v, "inserting list item");
Bram Moolenaarb3766472013-04-15 13:49:21 +0200882 if (list_insert_tv(l, &v, li) == FAIL)
Bram Moolenaarca06da92018-07-01 15:12:05 +0200883 luaL_error(L, "failed to add item to list");
Bram Moolenaarb3766472013-04-15 13:49:21 +0200884 clear_tv(&v);
Bram Moolenaar1dced572012-04-05 16:54:08 +0200885 lua_settop(L, 1);
886 return 1;
887}
888
889static const luaL_Reg luaV_List_mt[] = {
890 {"__tostring", luaV_list_tostring},
Bram Moolenaar1dced572012-04-05 16:54:08 +0200891 {"__len", luaV_list_len},
892 {"__call", luaV_list_call},
893 {"__index", luaV_list_index},
894 {"__newindex", luaV_list_newindex},
895 {"add", luaV_list_add},
896 {"insert", luaV_list_insert},
897 {NULL, NULL}
898};
899
900
Bram Moolenaar2ab2e862019-12-04 21:24:53 +0100901// ======= Dict type =======
Bram Moolenaar1dced572012-04-05 16:54:08 +0200902
903 static luaV_Dict *
Bram Moolenaar4eefe472019-03-19 21:59:19 +0100904luaV_newdict(lua_State *L, dict_T *dic)
Bram Moolenaar1dced572012-04-05 16:54:08 +0200905{
906 luaV_Dict *d = (luaV_Dict *) lua_newuserdata(L, sizeof(luaV_Dict));
907 *d = dic;
Bram Moolenaar2ab2e862019-12-04 21:24:53 +0100908 dic->dv_refcount++; // reference in Lua
909 luaV_setudata(L, dic); // cache[dic] = udata
Bram Moolenaar1dced572012-04-05 16:54:08 +0200910 luaV_getfield(L, LUAVIM_DICT);
911 lua_setmetatable(L, -2);
912 return d;
913}
914
915luaV_pushtype(dict_T, dict, luaV_Dict)
916luaV_type_tostring(dict, LUAVIM_DICT)
917
918 static int
Bram Moolenaar4eefe472019-03-19 21:59:19 +0100919luaV_dict_len(lua_State *L)
Bram Moolenaar1dced572012-04-05 16:54:08 +0200920{
921 dict_T *d = luaV_unbox(L, luaV_Dict, 1);
Bram Moolenaarb7828692019-03-23 13:57:02 +0100922 lua_pushinteger(L, (int) dict_len(d));
Bram Moolenaar1dced572012-04-05 16:54:08 +0200923 return 1;
924}
925
926 static int
Bram Moolenaar4eefe472019-03-19 21:59:19 +0100927luaV_dict_iter(lua_State *L UNUSED)
Bram Moolenaar1dced572012-04-05 16:54:08 +0200928{
Bram Moolenaarfeeaa682013-02-14 22:19:51 +0100929#ifdef FEAT_EVAL
Bram Moolenaar1dced572012-04-05 16:54:08 +0200930 hashitem_T *hi = (hashitem_T *) lua_touserdata(L, lua_upvalueindex(2));
931 int n = lua_tointeger(L, lua_upvalueindex(3));
932 dictitem_T *di;
933 if (n <= 0) return 0;
934 while (HASHITEM_EMPTY(hi)) hi++;
935 di = dict_lookup(hi);
936 lua_pushstring(L, (char *) hi->hi_key);
937 luaV_pushtypval(L, &di->di_tv);
938 lua_pushlightuserdata(L, (void *) (hi + 1));
939 lua_replace(L, lua_upvalueindex(2));
940 lua_pushinteger(L, n - 1);
941 lua_replace(L, lua_upvalueindex(3));
942 return 2;
Bram Moolenaarfeeaa682013-02-14 22:19:51 +0100943#else
944 return 0;
945#endif
Bram Moolenaar1dced572012-04-05 16:54:08 +0200946}
947
948 static int
Bram Moolenaar4eefe472019-03-19 21:59:19 +0100949luaV_dict_call(lua_State *L)
Bram Moolenaar1dced572012-04-05 16:54:08 +0200950{
951 dict_T *d = luaV_unbox(L, luaV_Dict, 1);
952 hashtab_T *ht = &d->dv_hashtab;
Bram Moolenaar2ab2e862019-12-04 21:24:53 +0100953 lua_pushvalue(L, lua_upvalueindex(1)); // pass cache table along
Bram Moolenaar1dced572012-04-05 16:54:08 +0200954 lua_pushlightuserdata(L, (void *) ht->ht_array);
Bram Moolenaar2ab2e862019-12-04 21:24:53 +0100955 lua_pushinteger(L, ht->ht_used); // # remaining items
Bram Moolenaar1dced572012-04-05 16:54:08 +0200956 lua_pushcclosure(L, luaV_dict_iter, 3);
957 return 1;
958}
959
960 static int
Bram Moolenaarca06da92018-07-01 15:12:05 +0200961luaV_dict_index(lua_State *L)
Bram Moolenaar1dced572012-04-05 16:54:08 +0200962{
963 dict_T *d = luaV_unbox(L, luaV_Dict, 1);
964 char_u *key = (char_u *) luaL_checkstring(L, 2);
965 dictitem_T *di = dict_find(d, key, -1);
Bram Moolenaarca06da92018-07-01 15:12:05 +0200966
Bram Moolenaar1dced572012-04-05 16:54:08 +0200967 if (di == NULL)
968 lua_pushnil(L);
969 else
Bram Moolenaarca06da92018-07-01 15:12:05 +0200970 {
Bram Moolenaar1dced572012-04-05 16:54:08 +0200971 luaV_pushtypval(L, &di->di_tv);
Bram Moolenaar2ab2e862019-12-04 21:24:53 +0100972 if (di->di_tv.v_type == VAR_FUNC) // funcref?
Bram Moolenaarca06da92018-07-01 15:12:05 +0200973 {
974 luaV_Funcref *f = (luaV_Funcref *) lua_touserdata(L, -1);
Bram Moolenaar2ab2e862019-12-04 21:24:53 +0100975 f->self = d; // keep "self" reference
Bram Moolenaarca06da92018-07-01 15:12:05 +0200976 d->dv_refcount++;
977 }
978 }
Bram Moolenaar1dced572012-04-05 16:54:08 +0200979 return 1;
980}
981
982 static int
Bram Moolenaarca06da92018-07-01 15:12:05 +0200983luaV_dict_newindex(lua_State *L)
Bram Moolenaar1dced572012-04-05 16:54:08 +0200984{
985 dict_T *d = luaV_unbox(L, luaV_Dict, 1);
986 char_u *key = (char_u *) luaL_checkstring(L, 2);
987 dictitem_T *di;
Bram Moolenaarca06da92018-07-01 15:12:05 +0200988 typval_T v;
Bram Moolenaar713bf9e92019-03-16 16:38:41 +0100989
Bram Moolenaar1dced572012-04-05 16:54:08 +0200990 if (d->dv_lock)
991 luaL_error(L, "dict is locked");
Bram Moolenaard6ef5f92018-07-13 22:08:23 +0200992 if (key == NULL)
993 return 0;
994 if (*key == NUL)
Bram Moolenaarca06da92018-07-01 15:12:05 +0200995 luaL_error(L, "empty key");
Bram Moolenaar2ab2e862019-12-04 21:24:53 +0100996 if (!lua_isnil(L, 3)) // read value?
Bram Moolenaar17413672018-07-14 20:49:42 +0200997 {
Bram Moolenaarca06da92018-07-01 15:12:05 +0200998 luaV_checktypval(L, 3, &v, "setting dict item");
999 if (d->dv_scope == VAR_DEF_SCOPE && v.v_type == VAR_FUNC)
1000 luaL_error(L, "cannot assign funcref to builtin scope");
1001 }
Bram Moolenaar1dced572012-04-05 16:54:08 +02001002 di = dict_find(d, key, -1);
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001003 if (di == NULL) // non-existing key?
Bram Moolenaar1dced572012-04-05 16:54:08 +02001004 {
Bram Moolenaard6ef5f92018-07-13 22:08:23 +02001005 if (lua_isnil(L, 3))
1006 return 0;
Bram Moolenaar1dced572012-04-05 16:54:08 +02001007 di = dictitem_alloc(key);
Bram Moolenaard6ef5f92018-07-13 22:08:23 +02001008 if (di == NULL)
1009 return 0;
Bram Moolenaar1dced572012-04-05 16:54:08 +02001010 if (dict_add(d, di) == FAIL)
1011 {
Bram Moolenaard6ef5f92018-07-13 22:08:23 +02001012 vim_free(di);
1013 return 0;
Bram Moolenaar1dced572012-04-05 16:54:08 +02001014 }
1015 }
1016 else
1017 clear_tv(&di->di_tv);
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001018 if (lua_isnil(L, 3)) // remove?
Bram Moolenaar1dced572012-04-05 16:54:08 +02001019 {
1020 hashitem_T *hi = hash_find(&d->dv_hashtab, di->di_key);
1021 hash_remove(&d->dv_hashtab, hi);
1022 dictitem_free(di);
1023 }
Bram Moolenaarca06da92018-07-01 15:12:05 +02001024 else
1025 {
Bram Moolenaar1dced572012-04-05 16:54:08 +02001026 copy_tv(&v, &di->di_tv);
Bram Moolenaarb3766472013-04-15 13:49:21 +02001027 clear_tv(&v);
Bram Moolenaar1dced572012-04-05 16:54:08 +02001028 }
1029 return 0;
1030}
1031
1032static const luaL_Reg luaV_Dict_mt[] = {
1033 {"__tostring", luaV_dict_tostring},
Bram Moolenaar1dced572012-04-05 16:54:08 +02001034 {"__len", luaV_dict_len},
1035 {"__call", luaV_dict_call},
1036 {"__index", luaV_dict_index},
1037 {"__newindex", luaV_dict_newindex},
1038 {NULL, NULL}
1039};
1040
1041
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001042// ======= Blob type =======
Bram Moolenaarb7828692019-03-23 13:57:02 +01001043
1044 static luaV_Blob *
1045luaV_newblob(lua_State *L, blob_T *blo)
1046{
1047 luaV_Blob *b = (luaV_Blob *) lua_newuserdata(L, sizeof(luaV_Blob));
1048 *b = blo;
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001049 blo->bv_refcount++; // reference in Lua
1050 luaV_setudata(L, blo); // cache[blo] = udata
Bram Moolenaarb7828692019-03-23 13:57:02 +01001051 luaV_getfield(L, LUAVIM_BLOB);
1052 lua_setmetatable(L, -2);
1053 return b;
1054}
1055
1056luaV_pushtype(blob_T, blob, luaV_Blob)
1057luaV_type_tostring(blob, LUAVIM_BLOB)
1058
1059 static int
1060luaV_blob_gc(lua_State *L)
1061{
1062 blob_T *b = luaV_unbox(L, luaV_Blob, 1);
1063 blob_unref(b);
1064 return 0;
1065}
1066
1067 static int
1068luaV_blob_len(lua_State *L)
1069{
1070 blob_T *b = luaV_unbox(L, luaV_Blob, 1);
1071 lua_pushinteger(L, (int) blob_len(b));
1072 return 1;
1073}
1074
1075 static int
1076luaV_blob_index(lua_State *L)
1077{
1078 blob_T *b = luaV_unbox(L, luaV_Blob, 1);
1079 if (lua_isnumber(L, 2))
1080 {
1081 int idx = luaL_checkinteger(L, 2);
1082 if (idx < blob_len(b))
1083 lua_pushnumber(L, (lua_Number) blob_get(b, idx));
1084 else
1085 lua_pushnil(L);
1086 }
1087 else if (lua_isstring(L, 2))
1088 {
1089 const char *s = lua_tostring(L, 2);
1090 if (strncmp(s, "add", 3) == 0)
1091 {
1092 lua_getmetatable(L, 1);
1093 lua_getfield(L, -1, s);
1094 }
1095 else
1096 lua_pushnil(L);
1097 }
1098 else
1099 lua_pushnil(L);
1100 return 1;
1101}
1102
1103 static int
1104luaV_blob_newindex(lua_State *L)
1105{
1106 blob_T *b = luaV_unbox(L, luaV_Blob, 1);
1107 if (b->bv_lock)
1108 luaL_error(L, "blob is locked");
1109 if (lua_isnumber(L, 2))
1110 {
1111 long len = blob_len(b);
1112 int idx = luaL_checkinteger(L, 2);
1113 int val = luaL_checkinteger(L, 3);
1114 if (idx < len || (idx == len && ga_grow(&b->bv_ga, 1) == OK))
1115 {
1116 blob_set(b, idx, (char_u) val);
1117 if (idx == len)
1118 ++b->bv_ga.ga_len;
1119 }
1120 else
1121 luaL_error(L, "index out of range");
1122 }
1123 return 0;
1124}
1125
1126 static int
1127luaV_blob_add(lua_State *L)
1128{
1129 luaV_Blob *blo = luaV_checkudata(L, 1, LUAVIM_BLOB);
1130 blob_T *b = (blob_T *) luaV_checkcache(L, (void *) *blo);
1131 if (b->bv_lock)
1132 luaL_error(L, "blob is locked");
1133 lua_settop(L, 2);
1134 if (!lua_isstring(L, 2))
1135 luaL_error(L, "string expected, got %s", luaL_typename(L, 2));
1136 else
1137 {
1138 size_t i, l = 0;
1139 const char *s = lua_tolstring(L, 2, &l);
1140
Bram Moolenaar5f1d3ae2020-02-11 22:37:35 +01001141 if (ga_grow(&b->bv_ga, (int)l) == OK)
Bram Moolenaar6fb5c972019-03-26 21:44:20 +01001142 for (i = 0; i < l; ++i)
1143 ga_append(&b->bv_ga, s[i]);
Bram Moolenaarb7828692019-03-23 13:57:02 +01001144 }
1145 lua_settop(L, 1);
1146 return 1;
1147}
1148
1149static const luaL_Reg luaV_Blob_mt[] = {
1150 {"__tostring", luaV_blob_tostring},
1151 {"__gc", luaV_blob_gc},
1152 {"__len", luaV_blob_len},
1153 {"__index", luaV_blob_index},
1154 {"__newindex", luaV_blob_newindex},
1155 {"add", luaV_blob_add},
1156 {NULL, NULL}
1157};
1158
1159
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001160// ======= Funcref type =======
Bram Moolenaarca06da92018-07-01 15:12:05 +02001161
1162 static luaV_Funcref *
1163luaV_newfuncref(lua_State *L, char_u *name)
1164{
1165 luaV_Funcref *f = (luaV_Funcref *)lua_newuserdata(L, sizeof(luaV_Funcref));
1166
1167 if (name != NULL)
1168 {
Bram Moolenaar4eefe472019-03-19 21:59:19 +01001169 func_ref(name);
1170 f->name = vim_strsave(name);
Bram Moolenaarca06da92018-07-01 15:12:05 +02001171 }
Bram Moolenaarca06da92018-07-01 15:12:05 +02001172 f->self = NULL;
1173 luaV_getfield(L, LUAVIM_FUNCREF);
1174 lua_setmetatable(L, -2);
1175 return f;
1176}
1177
1178 static luaV_Funcref *
Bram Moolenaar4eefe472019-03-19 21:59:19 +01001179luaV_pushfuncref(lua_State *L, char_u *name)
Bram Moolenaarca06da92018-07-01 15:12:05 +02001180{
Bram Moolenaar4eefe472019-03-19 21:59:19 +01001181 return luaV_newfuncref(L, name);
Bram Moolenaarca06da92018-07-01 15:12:05 +02001182}
1183
1184
1185luaV_type_tostring(funcref, LUAVIM_FUNCREF)
1186
1187 static int
1188luaV_funcref_gc(lua_State *L)
1189{
1190 luaV_Funcref *f = (luaV_Funcref *) lua_touserdata(L, 1);
1191
Bram Moolenaar4eefe472019-03-19 21:59:19 +01001192 func_unref(f->name);
1193 vim_free(f->name);
1194 // NOTE: Don't call "dict_unref(f->self)", because the dict of "f->self"
1195 // will be (or has been already) freed by Vim's garbage collection.
Bram Moolenaarca06da92018-07-01 15:12:05 +02001196 return 0;
1197}
1198
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001199// equivalent to string(funcref)
Bram Moolenaarca06da92018-07-01 15:12:05 +02001200 static int
1201luaV_funcref_len(lua_State *L)
1202{
1203 luaV_Funcref *f = (luaV_Funcref *) lua_touserdata(L, 1);
1204
Bram Moolenaar4eefe472019-03-19 21:59:19 +01001205 lua_pushstring(L, (const char *) f->name);
Bram Moolenaarca06da92018-07-01 15:12:05 +02001206 return 1;
1207}
1208
1209 static int
1210luaV_funcref_call(lua_State *L)
1211{
1212 luaV_Funcref *f = (luaV_Funcref *) lua_touserdata(L, 1);
Bram Moolenaar4eefe472019-03-19 21:59:19 +01001213 int i, n = lua_gettop(L) - 1; // #args
1214 int status = FAIL;
1215 typval_T args;
1216 typval_T rettv;
Bram Moolenaarca06da92018-07-01 15:12:05 +02001217
Bram Moolenaar4eefe472019-03-19 21:59:19 +01001218 args.v_type = VAR_LIST;
1219 args.vval.v_list = list_alloc();
1220 rettv.v_type = VAR_UNKNOWN; // as in clear_tv
1221 if (args.vval.v_list != NULL)
Bram Moolenaard6ef5f92018-07-13 22:08:23 +02001222 {
Bram Moolenaar4eefe472019-03-19 21:59:19 +01001223 typval_T v;
1224
Bram Moolenaar17413672018-07-14 20:49:42 +02001225 for (i = 0; i < n; i++)
1226 {
Bram Moolenaard6ef5f92018-07-13 22:08:23 +02001227 luaV_checktypval(L, i + 2, &v, "calling funcref");
Bram Moolenaar4eefe472019-03-19 21:59:19 +01001228 list_append_tv(args.vval.v_list, &v);
Bram Moolenaar713bf9e92019-03-16 16:38:41 +01001229 clear_tv(&v);
Bram Moolenaard6ef5f92018-07-13 22:08:23 +02001230 }
Bram Moolenaar4eefe472019-03-19 21:59:19 +01001231 status = func_call(f->name, &args, NULL, f->self, &rettv);
Bram Moolenaard6ef5f92018-07-13 22:08:23 +02001232 if (status == OK)
1233 luaV_pushtypval(L, &rettv);
Bram Moolenaar4eefe472019-03-19 21:59:19 +01001234 clear_tv(&args);
Bram Moolenaard6ef5f92018-07-13 22:08:23 +02001235 clear_tv(&rettv);
Bram Moolenaarca06da92018-07-01 15:12:05 +02001236 }
Bram Moolenaarca06da92018-07-01 15:12:05 +02001237 if (status != OK)
1238 luaL_error(L, "cannot call funcref");
1239 return 1;
1240}
1241
1242static const luaL_Reg luaV_Funcref_mt[] = {
1243 {"__tostring", luaV_funcref_tostring},
1244 {"__gc", luaV_funcref_gc},
1245 {"__len", luaV_funcref_len},
1246 {"__call", luaV_funcref_call},
1247 {NULL, NULL}
1248};
1249
1250
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001251// ======= Buffer type =======
Bram Moolenaar1dced572012-04-05 16:54:08 +02001252
1253luaV_newtype(buf_T, buffer, luaV_Buffer, LUAVIM_BUFFER)
1254luaV_pushtype(buf_T, buffer, luaV_Buffer)
1255luaV_type_tostring(buffer, LUAVIM_BUFFER)
1256
1257 static int
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001258luaV_buffer_len(lua_State *L)
1259{
Bram Moolenaar1dced572012-04-05 16:54:08 +02001260 buf_T *b = (buf_T *) luaV_checkvalid(L, luaV_Buffer, 1);
1261 lua_pushinteger(L, b->b_ml.ml_line_count);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001262 return 1;
1263}
1264
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001265 static int
1266luaV_buffer_call(lua_State *L)
1267{
Bram Moolenaar1dced572012-04-05 16:54:08 +02001268 buf_T *b = (buf_T *) luaV_checkvalid(L, luaV_Buffer, 1);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001269 lua_settop(L, 1);
Bram Moolenaar1dced572012-04-05 16:54:08 +02001270 set_curbuf(b, DOBUF_SPLIT);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001271 return 1;
1272}
1273
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001274 static int
1275luaV_buffer_index(lua_State *L)
1276{
Bram Moolenaar1dced572012-04-05 16:54:08 +02001277 buf_T *b = (buf_T *) luaV_checkvalid(L, luaV_Buffer, 1);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001278 linenr_T n = (linenr_T) lua_tointeger(L, 2);
Bram Moolenaar1dced572012-04-05 16:54:08 +02001279 if (n > 0 && n <= b->b_ml.ml_line_count)
1280 luaV_pushline(L, b, n);
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001281 else if (lua_isstring(L, 2))
1282 {
1283 const char *s = lua_tostring(L, 2);
1284 if (strncmp(s, "name", 4) == 0)
Bram Moolenaarfe08df42018-07-07 23:07:41 +02001285 lua_pushstring(L, (b->b_sfname == NULL)
1286 ? "" : (char *) b->b_sfname);
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001287 else if (strncmp(s, "fname", 5) == 0)
Bram Moolenaarfe08df42018-07-07 23:07:41 +02001288 lua_pushstring(L, (b->b_ffname == NULL)
1289 ? "" : (char *) b->b_ffname);
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001290 else if (strncmp(s, "number", 6) == 0)
Bram Moolenaar1dced572012-04-05 16:54:08 +02001291 lua_pushinteger(L, b->b_fnum);
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001292 // methods
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001293 else if (strncmp(s, "insert", 6) == 0
1294 || strncmp(s, "next", 4) == 0
1295 || strncmp(s, "previous", 8) == 0
1296 || strncmp(s, "isvalid", 7) == 0)
1297 {
1298 lua_getmetatable(L, 1);
1299 lua_getfield(L, -1, s);
1300 }
1301 else
1302 lua_pushnil(L);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001303 }
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001304 else
1305 lua_pushnil(L);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001306 return 1;
1307}
1308
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001309 static int
1310luaV_buffer_newindex(lua_State *L)
1311{
Bram Moolenaar1dced572012-04-05 16:54:08 +02001312 buf_T *b = (buf_T *) luaV_checkvalid(L, luaV_Buffer, 1);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001313 linenr_T n = (linenr_T) luaL_checkinteger(L, 2);
1314#ifdef HAVE_SANDBOX
1315 luaV_checksandbox(L);
1316#endif
Bram Moolenaar1dced572012-04-05 16:54:08 +02001317 if (n < 1 || n > b->b_ml.ml_line_count)
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001318 luaL_error(L, "invalid line number");
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001319 if (lua_isnil(L, 3)) // delete line
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001320 {
1321 buf_T *buf = curbuf;
Bram Moolenaar1dced572012-04-05 16:54:08 +02001322 curbuf = b;
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001323 if (u_savedel(n, 1L) == FAIL)
1324 {
1325 curbuf = buf;
1326 luaL_error(L, "cannot save undo information");
1327 }
1328 else if (ml_delete(n, FALSE) == FAIL)
1329 {
1330 curbuf = buf;
1331 luaL_error(L, "cannot delete line");
1332 }
Bram Moolenaarca06da92018-07-01 15:12:05 +02001333 else
1334 {
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001335 deleted_lines_mark(n, 1L);
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001336 if (b == curwin->w_buffer) // fix cursor in current window?
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001337 {
1338 if (curwin->w_cursor.lnum >= n)
1339 {
1340 if (curwin->w_cursor.lnum > n)
1341 {
1342 curwin->w_cursor.lnum -= 1;
1343 check_cursor_col();
1344 }
1345 else check_cursor();
1346 changed_cline_bef_curs();
1347 }
1348 invalidate_botline();
1349 }
1350 }
1351 curbuf = buf;
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001352 }
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001353 else if (lua_isstring(L, 3)) // update line
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001354 {
1355 buf_T *buf = curbuf;
Bram Moolenaar1dced572012-04-05 16:54:08 +02001356 curbuf = b;
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001357 if (u_savesub(n) == FAIL)
1358 {
1359 curbuf = buf;
1360 luaL_error(L, "cannot save undo information");
1361 }
1362 else if (ml_replace(n, luaV_toline(L, 3), TRUE) == FAIL)
1363 {
1364 curbuf = buf;
1365 luaL_error(L, "cannot replace line");
1366 }
1367 else changed_bytes(n, 0);
1368 curbuf = buf;
Bram Moolenaar1dced572012-04-05 16:54:08 +02001369 if (b == curwin->w_buffer)
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001370 check_cursor_col();
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001371 }
1372 else
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001373 luaL_error(L, "wrong argument to change line");
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001374 return 0;
1375}
1376
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001377 static int
1378luaV_buffer_insert(lua_State *L)
1379{
Bram Moolenaar1dced572012-04-05 16:54:08 +02001380 luaV_Buffer *lb = luaV_checkudata(L, 1, LUAVIM_BUFFER);
1381 buf_T *b = (buf_T *) luaV_checkcache(L, (void *) *lb);
1382 linenr_T last = b->b_ml.ml_line_count;
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001383 linenr_T n = (linenr_T) luaL_optinteger(L, 3, last);
1384 buf_T *buf;
1385 luaL_checktype(L, 2, LUA_TSTRING);
1386#ifdef HAVE_SANDBOX
1387 luaV_checksandbox(L);
1388#endif
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001389 // fix insertion line
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001390 if (n < 0) n = 0;
1391 if (n > last) n = last;
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001392 // insert
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001393 buf = curbuf;
Bram Moolenaar1dced572012-04-05 16:54:08 +02001394 curbuf = b;
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001395 if (u_save(n, n + 1) == FAIL)
1396 {
1397 curbuf = buf;
1398 luaL_error(L, "cannot save undo information");
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001399 }
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001400 else if (ml_append(n, luaV_toline(L, 2), 0, FALSE) == FAIL)
1401 {
1402 curbuf = buf;
1403 luaL_error(L, "cannot insert line");
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001404 }
1405 else
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001406 appended_lines_mark(n, 1L);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001407 curbuf = buf;
1408 update_screen(VALID);
1409 return 0;
1410}
1411
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001412 static int
1413luaV_buffer_next(lua_State *L)
1414{
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001415 luaV_Buffer *b = luaV_checkudata(L, 1, LUAVIM_BUFFER);
Bram Moolenaar1dced572012-04-05 16:54:08 +02001416 buf_T *buf = (buf_T *) luaV_checkcache(L, (void *) *b);
1417 luaV_pushbuffer(L, buf->b_next);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001418 return 1;
1419}
1420
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001421 static int
1422luaV_buffer_previous(lua_State *L)
1423{
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001424 luaV_Buffer *b = luaV_checkudata(L, 1, LUAVIM_BUFFER);
Bram Moolenaar1dced572012-04-05 16:54:08 +02001425 buf_T *buf = (buf_T *) luaV_checkcache(L, (void *) *b);
1426 luaV_pushbuffer(L, buf->b_prev);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001427 return 1;
1428}
1429
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001430 static int
1431luaV_buffer_isvalid(lua_State *L)
1432{
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001433 luaV_Buffer *b = luaV_checkudata(L, 1, LUAVIM_BUFFER);
Bram Moolenaar1dced572012-04-05 16:54:08 +02001434 luaV_getudata(L, *b);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001435 lua_pushboolean(L, !lua_isnil(L, -1));
1436 return 1;
1437}
1438
1439static const luaL_Reg luaV_Buffer_mt[] = {
1440 {"__tostring", luaV_buffer_tostring},
1441 {"__len", luaV_buffer_len},
1442 {"__call", luaV_buffer_call},
1443 {"__index", luaV_buffer_index},
1444 {"__newindex", luaV_buffer_newindex},
1445 {"insert", luaV_buffer_insert},
1446 {"next", luaV_buffer_next},
1447 {"previous", luaV_buffer_previous},
1448 {"isvalid", luaV_buffer_isvalid},
1449 {NULL, NULL}
1450};
1451
1452
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001453// ======= Window type =======
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001454
Bram Moolenaar1dced572012-04-05 16:54:08 +02001455luaV_newtype(win_T, window, luaV_Window, LUAVIM_WINDOW)
1456luaV_pushtype(win_T, window, luaV_Window)
1457luaV_type_tostring(window, LUAVIM_WINDOW)
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001458
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001459 static int
1460luaV_window_call(lua_State *L)
1461{
Bram Moolenaar1dced572012-04-05 16:54:08 +02001462 win_T *w = (win_T *) luaV_checkvalid(L, luaV_Window, 1);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001463 lua_settop(L, 1);
Bram Moolenaar1dced572012-04-05 16:54:08 +02001464 win_goto(w);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001465 return 1;
1466}
1467
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001468 static int
1469luaV_window_index(lua_State *L)
1470{
Bram Moolenaar1dced572012-04-05 16:54:08 +02001471 win_T *w = (win_T *) luaV_checkvalid(L, luaV_Window, 1);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001472 const char *s = luaL_checkstring(L, 2);
1473 if (strncmp(s, "buffer", 6) == 0)
Bram Moolenaar1dced572012-04-05 16:54:08 +02001474 luaV_pushbuffer(L, w->w_buffer);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001475 else if (strncmp(s, "line", 4) == 0)
Bram Moolenaar1dced572012-04-05 16:54:08 +02001476 lua_pushinteger(L, w->w_cursor.lnum);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001477 else if (strncmp(s, "col", 3) == 0)
Bram Moolenaar1dced572012-04-05 16:54:08 +02001478 lua_pushinteger(L, w->w_cursor.col + 1);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001479 else if (strncmp(s, "width", 5) == 0)
Bram Moolenaar02631462017-09-22 15:20:32 +02001480 lua_pushinteger(L, w->w_width);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001481 else if (strncmp(s, "height", 6) == 0)
Bram Moolenaar1dced572012-04-05 16:54:08 +02001482 lua_pushinteger(L, w->w_height);
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001483 // methods
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001484 else if (strncmp(s, "next", 4) == 0
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001485 || strncmp(s, "previous", 8) == 0
1486 || strncmp(s, "isvalid", 7) == 0)
1487 {
1488 lua_getmetatable(L, 1);
1489 lua_getfield(L, -1, s);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001490 }
1491 else
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001492 lua_pushnil(L);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001493 return 1;
1494}
1495
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001496 static int
Bram Moolenaar4eefe472019-03-19 21:59:19 +01001497luaV_window_newindex(lua_State *L)
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001498{
Bram Moolenaar1dced572012-04-05 16:54:08 +02001499 win_T *w = (win_T *) luaV_checkvalid(L, luaV_Window, 1);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001500 const char *s = luaL_checkstring(L, 2);
1501 int v = luaL_checkinteger(L, 3);
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001502 if (strncmp(s, "line", 4) == 0)
1503 {
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001504#ifdef HAVE_SANDBOX
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001505 luaV_checksandbox(L);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001506#endif
Bram Moolenaar1dced572012-04-05 16:54:08 +02001507 if (v < 1 || v > w->w_buffer->b_ml.ml_line_count)
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001508 luaL_error(L, "line out of range");
Bram Moolenaar1dced572012-04-05 16:54:08 +02001509 w->w_cursor.lnum = v;
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001510 update_screen(VALID);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001511 }
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001512 else if (strncmp(s, "col", 3) == 0)
1513 {
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001514#ifdef HAVE_SANDBOX
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001515 luaV_checksandbox(L);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001516#endif
Bram Moolenaar1dced572012-04-05 16:54:08 +02001517 w->w_cursor.col = v - 1;
Bram Moolenaar53901442018-07-25 22:02:36 +02001518 w->w_set_curswant = TRUE;
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001519 update_screen(VALID);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001520 }
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001521 else if (strncmp(s, "width", 5) == 0)
1522 {
1523 win_T *win = curwin;
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001524#ifdef FEAT_GUI
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001525 need_mouse_correct = TRUE;
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001526#endif
Bram Moolenaar1dced572012-04-05 16:54:08 +02001527 curwin = w;
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001528 win_setwidth(v);
1529 curwin = win;
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001530 }
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001531 else if (strncmp(s, "height", 6) == 0)
1532 {
1533 win_T *win = curwin;
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001534#ifdef FEAT_GUI
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001535 need_mouse_correct = TRUE;
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001536#endif
Bram Moolenaar1dced572012-04-05 16:54:08 +02001537 curwin = w;
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001538 win_setheight(v);
1539 curwin = win;
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001540 }
1541 else
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001542 luaL_error(L, "invalid window property: `%s'", s);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001543 return 0;
1544}
1545
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001546 static int
1547luaV_window_next(lua_State *L)
1548{
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001549 luaV_Window *w = luaV_checkudata(L, 1, LUAVIM_WINDOW);
Bram Moolenaar1dced572012-04-05 16:54:08 +02001550 win_T *win = (win_T *) luaV_checkcache(L, (void *) *w);
1551 luaV_pushwindow(L, win->w_next);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001552 return 1;
1553}
1554
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001555 static int
1556luaV_window_previous(lua_State *L)
1557{
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001558 luaV_Window *w = luaV_checkudata(L, 1, LUAVIM_WINDOW);
Bram Moolenaar1dced572012-04-05 16:54:08 +02001559 win_T *win = (win_T *) luaV_checkcache(L, (void *) *w);
1560 luaV_pushwindow(L, win->w_prev);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001561 return 1;
1562}
1563
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001564 static int
1565luaV_window_isvalid(lua_State *L)
1566{
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001567 luaV_Window *w = luaV_checkudata(L, 1, LUAVIM_WINDOW);
Bram Moolenaar1dced572012-04-05 16:54:08 +02001568 luaV_getudata(L, *w);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001569 lua_pushboolean(L, !lua_isnil(L, -1));
1570 return 1;
1571}
1572
1573static const luaL_Reg luaV_Window_mt[] = {
1574 {"__tostring", luaV_window_tostring},
1575 {"__call", luaV_window_call},
1576 {"__index", luaV_window_index},
1577 {"__newindex", luaV_window_newindex},
1578 {"next", luaV_window_next},
1579 {"previous", luaV_window_previous},
1580 {"isvalid", luaV_window_isvalid},
1581 {NULL, NULL}
1582};
1583
1584
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001585// ======= Vim module =======
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001586
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001587 static int
1588luaV_print(lua_State *L)
1589{
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001590 int i, n = lua_gettop(L); // nargs
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001591 const char *s;
1592 size_t l;
1593 luaL_Buffer b;
1594 luaL_buffinit(L, &b);
1595 lua_getglobal(L, "tostring");
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001596 for (i = 1; i <= n; i++)
1597 {
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001598 lua_pushvalue(L, -1); // tostring
1599 lua_pushvalue(L, i); // arg
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001600 lua_call(L, 1, 1);
1601 s = lua_tolstring(L, -1, &l);
1602 if (s == NULL)
1603 return luaL_error(L, "cannot convert to string");
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001604 if (i > 1) luaL_addchar(&b, ' '); // use space instead of tab
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001605 luaV_addlstring(&b, s, l, 0);
1606 lua_pop(L, 1);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001607 }
1608 luaL_pushresult(&b);
Bram Moolenaarb98678a2019-10-19 15:18:44 +02001609 if (!got_int)
1610 luaV_msg(L);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001611 return 0;
1612}
1613
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001614 static int
Bram Moolenaar38e2b062011-09-21 17:15:39 +02001615luaV_debug(lua_State *L)
1616{
1617 lua_settop(L, 0);
1618 lua_getglobal(L, "vim");
1619 lua_getfield(L, -1, "eval");
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001620 lua_remove(L, -2); // vim.eval at position 1
Bram Moolenaar38e2b062011-09-21 17:15:39 +02001621 for (;;)
1622 {
1623 const char *input;
1624 size_t l;
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001625 lua_pushvalue(L, 1); // vim.eval
Bram Moolenaar38e2b062011-09-21 17:15:39 +02001626 lua_pushliteral(L, "input('lua_debug> ')");
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001627 lua_call(L, 1, 1); // return string
Bram Moolenaar38e2b062011-09-21 17:15:39 +02001628 input = lua_tolstring(L, -1, &l);
1629 if (l == 0 || strcmp(input, "cont") == 0)
1630 return 0;
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001631 msg_putchar('\n'); // avoid outputting on input line
Bram Moolenaar38e2b062011-09-21 17:15:39 +02001632 if (luaL_loadbuffer(L, input, l, "=(debug command)")
1633 || lua_pcall(L, 0, 0, 0))
1634 luaV_emsg(L);
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001635 lua_settop(L, 1); // remove eventual returns, but keep vim.eval
Bram Moolenaar38e2b062011-09-21 17:15:39 +02001636 }
1637}
1638
1639 static int
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001640luaV_command(lua_State *L)
1641{
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001642 do_cmdline_cmd((char_u *) luaL_checkstring(L, 1));
1643 update_screen(VALID);
1644 return 0;
1645}
1646
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001647 static int
1648luaV_eval(lua_State *L)
1649{
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001650 typval_T *tv = eval_expr((char_u *) luaL_checkstring(L, 1), NULL);
1651 if (tv == NULL) luaL_error(L, "invalid expression");
1652 luaV_pushtypval(L, tv);
Bram Moolenaarb3766472013-04-15 13:49:21 +02001653 free_tv(tv);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001654 return 1;
1655}
1656
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001657 static int
Bram Moolenaar0d2e4fc2010-07-18 12:35:47 +02001658luaV_beep(lua_State *L UNUSED)
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001659{
Bram Moolenaar165bc692015-07-21 17:53:25 +02001660 vim_beep(BO_LANG);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001661 return 0;
1662}
1663
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001664 static int
1665luaV_line(lua_State *L)
1666{
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001667 luaV_pushline(L, curbuf, curwin->w_cursor.lnum);
1668 return 1;
1669}
1670
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001671 static int
Bram Moolenaar1dced572012-04-05 16:54:08 +02001672luaV_list(lua_State *L)
1673{
Bram Moolenaarca06da92018-07-01 15:12:05 +02001674 list_T *l;
1675 int initarg = !lua_isnoneornil(L, 1);
1676
1677 if (initarg && lua_type(L, 1) != LUA_TTABLE)
1678 luaL_error(L, "table expected, got %s", luaL_typename(L, 1));
1679 l = list_alloc();
Bram Moolenaar1dced572012-04-05 16:54:08 +02001680 if (l == NULL)
1681 lua_pushnil(L);
1682 else
Bram Moolenaarca06da92018-07-01 15:12:05 +02001683 {
Bram Moolenaar1dced572012-04-05 16:54:08 +02001684 luaV_newlist(L, l);
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001685 if (initarg) // traverse table to init list
Bram Moolenaar17413672018-07-14 20:49:42 +02001686 {
Bram Moolenaarca06da92018-07-01 15:12:05 +02001687 int notnil, i = 0;
1688 typval_T v;
Bram Moolenaar17413672018-07-14 20:49:42 +02001689 do
1690 {
Bram Moolenaarca06da92018-07-01 15:12:05 +02001691 lua_rawgeti(L, 1, ++i);
1692 notnil = !lua_isnil(L, -1);
Bram Moolenaar17413672018-07-14 20:49:42 +02001693 if (notnil)
1694 {
Bram Moolenaarca06da92018-07-01 15:12:05 +02001695 luaV_checktypval(L, -1, &v, "vim.list");
1696 list_append_tv(l, &v);
Bram Moolenaar713bf9e92019-03-16 16:38:41 +01001697 clear_tv(&v);
Bram Moolenaarca06da92018-07-01 15:12:05 +02001698 }
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001699 lua_pop(L, 1); // value
Bram Moolenaarca06da92018-07-01 15:12:05 +02001700 } while (notnil);
1701 }
1702 }
Bram Moolenaar1dced572012-04-05 16:54:08 +02001703 return 1;
1704}
1705
1706 static int
1707luaV_dict(lua_State *L)
1708{
Bram Moolenaarca06da92018-07-01 15:12:05 +02001709 dict_T *d;
1710 int initarg = !lua_isnoneornil(L, 1);
1711
1712 if (initarg && lua_type(L, 1) != LUA_TTABLE)
1713 luaL_error(L, "table expected, got %s", luaL_typename(L, 1));
1714 d = dict_alloc();
Bram Moolenaar1dced572012-04-05 16:54:08 +02001715 if (d == NULL)
1716 lua_pushnil(L);
1717 else
Bram Moolenaarca06da92018-07-01 15:12:05 +02001718 {
Bram Moolenaar1dced572012-04-05 16:54:08 +02001719 luaV_newdict(L, d);
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001720 if (initarg) // traverse table to init dict
Bram Moolenaarca06da92018-07-01 15:12:05 +02001721 {
1722 lua_pushnil(L);
1723 while (lua_next(L, 1))
1724 {
1725 char_u *key;
1726 dictitem_T *di;
1727 typval_T v;
Bram Moolenaard6ef5f92018-07-13 22:08:23 +02001728
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001729 lua_pushvalue(L, -2); // dup key in case it's a number
Bram Moolenaarca06da92018-07-01 15:12:05 +02001730 key = (char_u *) lua_tostring(L, -1);
Bram Moolenaard6ef5f92018-07-13 22:08:23 +02001731 if (key == NULL)
1732 {
1733 lua_pushnil(L);
1734 return 1;
1735 }
1736 if (*key == NUL)
Bram Moolenaarca06da92018-07-01 15:12:05 +02001737 luaL_error(L, "table has empty key");
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001738 luaV_checktypval(L, -2, &v, "vim.dict"); // value
Bram Moolenaarca06da92018-07-01 15:12:05 +02001739 di = dictitem_alloc(key);
Bram Moolenaard6ef5f92018-07-13 22:08:23 +02001740 if (di == NULL || dict_add(d, di) == FAIL)
1741 {
Bram Moolenaarca06da92018-07-01 15:12:05 +02001742 vim_free(di);
1743 lua_pushnil(L);
1744 return 1;
1745 }
1746 copy_tv(&v, &di->di_tv);
1747 clear_tv(&v);
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001748 lua_pop(L, 2); // key copy and value
Bram Moolenaarca06da92018-07-01 15:12:05 +02001749 }
1750 }
1751 }
1752 return 1;
1753}
1754
1755 static int
Bram Moolenaarb7828692019-03-23 13:57:02 +01001756luaV_blob(lua_State *L)
1757{
1758 blob_T *b;
1759 int initarg = !lua_isnoneornil(L, 1);
1760
1761 if (initarg && !lua_isstring(L, 1))
1762 luaL_error(L, "string expected, got %s", luaL_typename(L, 1));
1763 b = blob_alloc();
1764 if (b == NULL)
1765 lua_pushnil(L);
1766 else
1767 {
1768 luaV_newblob(L, b);
1769 if (initarg)
1770 {
1771 size_t i, l = 0;
1772 const char *s = lua_tolstring(L, 1, &l);
1773
Bram Moolenaar5f1d3ae2020-02-11 22:37:35 +01001774 if (ga_grow(&b->bv_ga, (int)l) == OK)
Bram Moolenaar6fb5c972019-03-26 21:44:20 +01001775 for (i = 0; i < l; ++i)
1776 ga_append(&b->bv_ga, s[i]);
Bram Moolenaarb7828692019-03-23 13:57:02 +01001777 }
1778 }
1779 return 1;
1780}
1781
1782 static int
Bram Moolenaarca06da92018-07-01 15:12:05 +02001783luaV_funcref(lua_State *L)
1784{
1785 const char *name = luaL_checkstring(L, 1);
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001786 // note: not checking if function exists (needs function_exists)
Bram Moolenaarca06da92018-07-01 15:12:05 +02001787 if (name == NULL || *name == NUL || VIM_ISDIGIT(*name))
1788 luaL_error(L, "invalid function name: %s", name);
1789 luaV_newfuncref(L, (char_u *) name);
Bram Moolenaar1dced572012-04-05 16:54:08 +02001790 return 1;
1791}
1792
1793 static int
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001794luaV_buffer(lua_State *L)
1795{
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001796 buf_T *buf;
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001797 if (lua_isstring(L, 1)) // get by number or name?
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001798 {
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001799 if (lua_isnumber(L, 1)) // by number?
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001800 {
1801 int n = lua_tointeger(L, 1);
Bram Moolenaar29323592016-07-24 22:04:11 +02001802 FOR_ALL_BUFFERS(buf)
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001803 if (buf->b_fnum == n) break;
1804 }
Bram Moolenaarca06da92018-07-01 15:12:05 +02001805 else // by name
1806 {
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001807 size_t l;
1808 const char *s = lua_tolstring(L, 1, &l);
Bram Moolenaar29323592016-07-24 22:04:11 +02001809 FOR_ALL_BUFFERS(buf)
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001810 {
1811 if (buf->b_ffname == NULL || buf->b_sfname == NULL)
1812 {
1813 if (l == 0) break;
1814 }
Bram Moolenaar0d2e4fc2010-07-18 12:35:47 +02001815 else if (strncmp(s, (char *)buf->b_ffname, l) == 0
1816 || strncmp(s, (char *)buf->b_sfname, l) == 0)
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001817 break;
1818 }
1819 }
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001820 }
Bram Moolenaar1dced572012-04-05 16:54:08 +02001821 else
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001822 buf = (lua_toboolean(L, 1)) ? firstbuf : curbuf; // first buffer?
Bram Moolenaar1dced572012-04-05 16:54:08 +02001823 luaV_pushbuffer(L, buf);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001824 return 1;
1825}
1826
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001827 static int
1828luaV_window(lua_State *L)
1829{
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001830 win_T *win;
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001831 if (lua_isnumber(L, 1)) // get by number?
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001832 {
1833 int n = lua_tointeger(L, 1);
1834 for (win = firstwin; win != NULL; win = win->w_next, n--)
1835 if (n == 1) break;
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001836 }
Bram Moolenaar1dced572012-04-05 16:54:08 +02001837 else
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001838 win = (lua_toboolean(L, 1)) ? firstwin : curwin; // first window?
Bram Moolenaar1dced572012-04-05 16:54:08 +02001839 luaV_pushwindow(L, win);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001840 return 1;
1841}
1842
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001843 static int
1844luaV_open(lua_State *L)
1845{
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001846 char_u *s = NULL;
1847#ifdef HAVE_SANDBOX
1848 luaV_checksandbox(L);
1849#endif
1850 if (lua_isstring(L, 1)) s = (char_u *) lua_tostring(L, 1);
Bram Moolenaarfa263a52011-12-08 16:00:16 +01001851 luaV_pushbuffer(L, buflist_new(s, NULL, 1L, BLN_LISTED));
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001852 return 1;
1853}
1854
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001855 static int
Bram Moolenaar1dced572012-04-05 16:54:08 +02001856luaV_type(lua_State *L)
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001857{
Bram Moolenaar1dced572012-04-05 16:54:08 +02001858 luaL_checkany(L, 1);
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001859 if (lua_type(L, 1) == LUA_TUSERDATA) // check vim udata?
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001860 {
Bram Moolenaar1dced572012-04-05 16:54:08 +02001861 lua_settop(L, 1);
1862 if (lua_getmetatable(L, 1))
1863 {
1864 luaV_getfield(L, LUAVIM_LIST);
1865 if (lua_rawequal(L, -1, 2))
1866 {
1867 lua_pushstring(L, "list");
1868 return 1;
1869 }
1870 luaV_getfield(L, LUAVIM_DICT);
1871 if (lua_rawequal(L, -1, 2))
1872 {
1873 lua_pushstring(L, "dict");
1874 return 1;
1875 }
Bram Moolenaarb7828692019-03-23 13:57:02 +01001876 luaV_getfield(L, LUAVIM_BLOB);
1877 if (lua_rawequal(L, -1, 2))
1878 {
1879 lua_pushstring(L, "blob");
1880 return 1;
1881 }
Bram Moolenaarca06da92018-07-01 15:12:05 +02001882 luaV_getfield(L, LUAVIM_FUNCREF);
1883 if (lua_rawequal(L, -1, 2))
1884 {
1885 lua_pushstring(L, "funcref");
1886 return 1;
1887 }
Bram Moolenaar1dced572012-04-05 16:54:08 +02001888 luaV_getfield(L, LUAVIM_BUFFER);
1889 if (lua_rawequal(L, -1, 2))
1890 {
1891 lua_pushstring(L, "buffer");
1892 return 1;
1893 }
1894 luaV_getfield(L, LUAVIM_WINDOW);
1895 if (lua_rawequal(L, -1, 2))
1896 {
1897 lua_pushstring(L, "window");
1898 return 1;
1899 }
1900 }
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001901 }
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001902 lua_pushstring(L, luaL_typename(L, 1)); // fallback
Bram Moolenaar1dced572012-04-05 16:54:08 +02001903 return 1;
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001904}
1905
1906static const luaL_Reg luaV_module[] = {
1907 {"command", luaV_command},
1908 {"eval", luaV_eval},
1909 {"beep", luaV_beep},
1910 {"line", luaV_line},
Bram Moolenaar1dced572012-04-05 16:54:08 +02001911 {"list", luaV_list},
1912 {"dict", luaV_dict},
Bram Moolenaarb7828692019-03-23 13:57:02 +01001913 {"blob", luaV_blob},
Bram Moolenaarca06da92018-07-01 15:12:05 +02001914 {"funcref", luaV_funcref},
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001915 {"buffer", luaV_buffer},
1916 {"window", luaV_window},
1917 {"open", luaV_open},
Bram Moolenaar1dced572012-04-05 16:54:08 +02001918 {"type", luaV_type},
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001919 {NULL, NULL}
1920};
1921
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001922/*
1923 * for freeing list, dict, buffer and window objects; lightuserdata as arg
1924 */
Bram Moolenaar1dced572012-04-05 16:54:08 +02001925 static int
1926luaV_free(lua_State *L)
1927{
1928 lua_pushnil(L);
1929 luaV_setudata(L, lua_touserdata(L, 1));
1930 return 0;
1931}
1932
1933 static int
Bram Moolenaar4eefe472019-03-19 21:59:19 +01001934luaV_luaeval(lua_State *L)
Bram Moolenaar1dced572012-04-05 16:54:08 +02001935{
1936 luaL_Buffer b;
1937 size_t l;
1938 const char *str = lua_tolstring(L, 1, &l);
1939 typval_T *arg = (typval_T *) lua_touserdata(L, 2);
1940 typval_T *rettv = (typval_T *) lua_touserdata(L, 3);
1941 luaL_buffinit(L, &b);
1942 luaL_addlstring(&b, LUAVIM_EVALHEADER, sizeof(LUAVIM_EVALHEADER) - 1);
1943 luaL_addlstring(&b, str, l);
1944 luaL_pushresult(&b);
1945 str = lua_tolstring(L, -1, &l);
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001946 if (luaL_loadbuffer(L, str, l, LUAVIM_EVALNAME)) // compile error?
Bram Moolenaar1dced572012-04-05 16:54:08 +02001947 {
1948 luaV_emsg(L);
1949 return 0;
1950 }
1951 luaV_pushtypval(L, arg);
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001952 if (lua_pcall(L, 1, 1, 0)) // running error?
Bram Moolenaar1dced572012-04-05 16:54:08 +02001953 {
1954 luaV_emsg(L);
1955 return 0;
1956 }
Bram Moolenaarca06da92018-07-01 15:12:05 +02001957 if (luaV_totypval(L, -1, rettv) == FAIL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001958 emsg("luaeval: cannot convert value");
Bram Moolenaarf554a322015-02-04 23:08:01 +01001959 return 0;
Bram Moolenaar1dced572012-04-05 16:54:08 +02001960}
1961
1962 static int
Bram Moolenaar4eefe472019-03-19 21:59:19 +01001963luaV_setref(lua_State *L)
Bram Moolenaar1dced572012-04-05 16:54:08 +02001964{
Bram Moolenaar4eefe472019-03-19 21:59:19 +01001965 int copyID = lua_tointeger(L, 1);
1966 int abort = FALSE;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01001967
Bram Moolenaar1dced572012-04-05 16:54:08 +02001968 luaV_getfield(L, LUAVIM_LIST);
1969 luaV_getfield(L, LUAVIM_DICT);
Bram Moolenaar4eefe472019-03-19 21:59:19 +01001970 luaV_getfield(L, LUAVIM_FUNCREF);
Bram Moolenaar1dced572012-04-05 16:54:08 +02001971 lua_pushnil(L);
Bram Moolenaar4eefe472019-03-19 21:59:19 +01001972 // traverse cache table
Bram Moolenaarb84634d2015-02-04 22:02:37 +01001973 while (!abort && lua_next(L, lua_upvalueindex(1)) != 0)
Bram Moolenaar1dced572012-04-05 16:54:08 +02001974 {
1975 lua_getmetatable(L, -1);
Bram Moolenaar4eefe472019-03-19 21:59:19 +01001976 if (lua_rawequal(L, -1, 2)) // list?
Bram Moolenaar1dced572012-04-05 16:54:08 +02001977 {
Bram Moolenaar4eefe472019-03-19 21:59:19 +01001978 list_T *l = (list_T *)lua_touserdata(L, 5); // key
1979
Bram Moolenaar7be3ab22019-06-23 01:46:15 +02001980 abort = set_ref_in_list(l, copyID);
Bram Moolenaar1dced572012-04-05 16:54:08 +02001981 }
Bram Moolenaar4eefe472019-03-19 21:59:19 +01001982 else if (lua_rawequal(L, -1, 3)) // dict?
Bram Moolenaar1dced572012-04-05 16:54:08 +02001983 {
Bram Moolenaar4eefe472019-03-19 21:59:19 +01001984 dict_T *d = (dict_T *)lua_touserdata(L, 5); // key
1985
Bram Moolenaar7be3ab22019-06-23 01:46:15 +02001986 abort = set_ref_in_dict(d, copyID);
Bram Moolenaar1dced572012-04-05 16:54:08 +02001987 }
Bram Moolenaar4eefe472019-03-19 21:59:19 +01001988 else if (lua_rawequal(L, -1, 4)) // funcref?
1989 {
1990 luaV_Funcref *f = (luaV_Funcref *)lua_touserdata(L, 5); // key
1991
Bram Moolenaar7be3ab22019-06-23 01:46:15 +02001992 abort = set_ref_in_dict(f->self, copyID);
Bram Moolenaar4eefe472019-03-19 21:59:19 +01001993 }
1994 lua_pop(L, 2); // metatable and value
Bram Moolenaar1dced572012-04-05 16:54:08 +02001995 }
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01001996 lua_pushinteger(L, abort);
Bram Moolenaarf554a322015-02-04 23:08:01 +01001997 return 1;
Bram Moolenaar1dced572012-04-05 16:54:08 +02001998}
1999
Bram Moolenaar55d5c032010-07-17 23:52:29 +02002000 static int
2001luaopen_vim(lua_State *L)
2002{
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002003 // set cache table
Bram Moolenaar0ba04292010-07-14 23:23:17 +02002004 lua_newtable(L);
2005 lua_newtable(L);
Bram Moolenaar1dced572012-04-05 16:54:08 +02002006 lua_pushstring(L, "v");
Bram Moolenaar0ba04292010-07-14 23:23:17 +02002007 lua_setfield(L, -2, "__mode");
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002008 lua_setmetatable(L, -2); // cache is weak-valued
2009 // print
Bram Moolenaar0ba04292010-07-14 23:23:17 +02002010 lua_pushcfunction(L, luaV_print);
2011 lua_setglobal(L, "print");
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002012 // debug.debug
Bram Moolenaar38e2b062011-09-21 17:15:39 +02002013 lua_getglobal(L, "debug");
2014 lua_pushcfunction(L, luaV_debug);
2015 lua_setfield(L, -2, "debug");
2016 lua_pop(L, 1);
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002017 // free
Bram Moolenaar0ba04292010-07-14 23:23:17 +02002018 lua_pushlightuserdata(L, (void *) LUAVIM_FREE);
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002019 lua_pushvalue(L, 1); // cache table
Bram Moolenaar1dced572012-04-05 16:54:08 +02002020 lua_pushcclosure(L, luaV_free, 1);
2021 lua_rawset(L, LUA_REGISTRYINDEX);
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002022 // luaeval
Bram Moolenaar1dced572012-04-05 16:54:08 +02002023 lua_pushlightuserdata(L, (void *) LUAVIM_LUAEVAL);
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002024 lua_pushvalue(L, 1); // cache table
Bram Moolenaar1dced572012-04-05 16:54:08 +02002025 lua_pushcclosure(L, luaV_luaeval, 1);
2026 lua_rawset(L, LUA_REGISTRYINDEX);
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002027 // setref
Bram Moolenaar1dced572012-04-05 16:54:08 +02002028 lua_pushlightuserdata(L, (void *) LUAVIM_SETREF);
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002029 lua_pushvalue(L, 1); // cache table
Bram Moolenaar1dced572012-04-05 16:54:08 +02002030 lua_pushcclosure(L, luaV_setref, 1);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02002031 lua_rawset(L, LUA_REGISTRYINDEX);
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002032 // register
Bram Moolenaar1dced572012-04-05 16:54:08 +02002033 luaV_newmetatable(L, LUAVIM_LIST);
2034 lua_pushvalue(L, 1);
2035 luaV_openlib(L, luaV_List_mt, 1);
2036 luaV_newmetatable(L, LUAVIM_DICT);
2037 lua_pushvalue(L, 1);
2038 luaV_openlib(L, luaV_Dict_mt, 1);
Bram Moolenaarb7828692019-03-23 13:57:02 +01002039 luaV_newmetatable(L, LUAVIM_BLOB);
2040 lua_pushvalue(L, 1);
2041 luaV_openlib(L, luaV_Blob_mt, 1);
Bram Moolenaarca06da92018-07-01 15:12:05 +02002042 luaV_newmetatable(L, LUAVIM_FUNCREF);
2043 lua_pushvalue(L, 1);
2044 luaV_openlib(L, luaV_Funcref_mt, 1);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02002045 luaV_newmetatable(L, LUAVIM_BUFFER);
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002046 lua_pushvalue(L, 1); // cache table
Bram Moolenaar1dced572012-04-05 16:54:08 +02002047 luaV_openlib(L, luaV_Buffer_mt, 1);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02002048 luaV_newmetatable(L, LUAVIM_WINDOW);
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002049 lua_pushvalue(L, 1); // cache table
Bram Moolenaar1dced572012-04-05 16:54:08 +02002050 luaV_openlib(L, luaV_Window_mt, 1);
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002051 lua_newtable(L); // vim table
2052 lua_pushvalue(L, 1); // cache table
Bram Moolenaar1dced572012-04-05 16:54:08 +02002053 luaV_openlib(L, luaV_module, 1);
2054 lua_setglobal(L, LUAVIM_NAME);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02002055 return 0;
2056}
2057
Bram Moolenaar55d5c032010-07-17 23:52:29 +02002058 static lua_State *
2059luaV_newstate(void)
2060{
Bram Moolenaar0ba04292010-07-14 23:23:17 +02002061 lua_State *L = luaL_newstate();
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002062 luaL_openlibs(L); // core libs
2063 lua_pushcfunction(L, luaopen_vim); // vim
Bram Moolenaar0ba04292010-07-14 23:23:17 +02002064 lua_call(L, 0, 0);
2065 return L;
2066}
2067
Bram Moolenaar55d5c032010-07-17 23:52:29 +02002068 static void
2069luaV_setrange(lua_State *L, int line1, int line2)
2070{
Bram Moolenaar0ba04292010-07-14 23:23:17 +02002071 lua_getglobal(L, LUAVIM_NAME);
2072 lua_pushinteger(L, line1);
2073 lua_setfield(L, -2, "firstline");
2074 lua_pushinteger(L, line2);
2075 lua_setfield(L, -2, "lastline");
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002076 lua_pop(L, 1); // vim table
Bram Moolenaar0ba04292010-07-14 23:23:17 +02002077}
2078
2079
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002080// ======= Interface =======
Bram Moolenaar0ba04292010-07-14 23:23:17 +02002081
2082static lua_State *L = NULL;
2083
Bram Moolenaar55d5c032010-07-17 23:52:29 +02002084 static int
Bram Moolenaar1dced572012-04-05 16:54:08 +02002085lua_isopen(void)
Bram Moolenaar2bd6a1b2010-08-12 22:14:01 +02002086{
2087 return L != NULL;
2088}
2089
2090 static int
Bram Moolenaar55d5c032010-07-17 23:52:29 +02002091lua_init(void)
2092{
Bram Moolenaar1dced572012-04-05 16:54:08 +02002093 if (!lua_isopen())
Bram Moolenaar55d5c032010-07-17 23:52:29 +02002094 {
Bram Moolenaar0ba04292010-07-14 23:23:17 +02002095#ifdef DYNAMIC_LUA
Bram Moolenaar55d5c032010-07-17 23:52:29 +02002096 if (!lua_enabled(TRUE))
2097 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002098 emsg(_("Lua library cannot be loaded."));
Bram Moolenaar55d5c032010-07-17 23:52:29 +02002099 return FAIL;
2100 }
Bram Moolenaar0ba04292010-07-14 23:23:17 +02002101#endif
Bram Moolenaar55d5c032010-07-17 23:52:29 +02002102 L = luaV_newstate();
Bram Moolenaar0ba04292010-07-14 23:23:17 +02002103 }
2104 return OK;
2105}
2106
Bram Moolenaar55d5c032010-07-17 23:52:29 +02002107 void
2108lua_end(void)
2109{
Bram Moolenaar1dced572012-04-05 16:54:08 +02002110 if (lua_isopen())
Bram Moolenaar55d5c032010-07-17 23:52:29 +02002111 {
2112 lua_close(L);
2113 L = NULL;
Bram Moolenaar0ba04292010-07-14 23:23:17 +02002114 }
2115}
2116
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002117/*
2118 * ex commands
2119 */
Bram Moolenaar55d5c032010-07-17 23:52:29 +02002120 void
2121ex_lua(exarg_T *eap)
2122{
Bram Moolenaar0ba04292010-07-14 23:23:17 +02002123 char *script;
2124 if (lua_init() == FAIL) return;
2125 script = (char *) script_get(eap, eap->arg);
Bram Moolenaar55d5c032010-07-17 23:52:29 +02002126 if (!eap->skip)
2127 {
2128 char *s = (script) ? script : (char *) eap->arg;
2129 luaV_setrange(L, eap->line1, eap->line2);
2130 if (luaL_loadbuffer(L, s, strlen(s), LUAVIM_CHUNKNAME)
2131 || lua_pcall(L, 0, 0, 0))
2132 luaV_emsg(L);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02002133 }
2134 if (script != NULL) vim_free(script);
2135}
2136
Bram Moolenaar55d5c032010-07-17 23:52:29 +02002137 void
2138ex_luado(exarg_T *eap)
2139{
Bram Moolenaar0ba04292010-07-14 23:23:17 +02002140 linenr_T l;
2141 const char *s = (const char *) eap->arg;
2142 luaL_Buffer b;
2143 size_t len;
Bram Moolenaard58f03b2017-01-29 22:48:45 +01002144 buf_T *was_curbuf = curbuf;
2145
Bram Moolenaar0ba04292010-07-14 23:23:17 +02002146 if (lua_init() == FAIL) return;
Bram Moolenaar55d5c032010-07-17 23:52:29 +02002147 if (u_save(eap->line1 - 1, eap->line2 + 1) == FAIL)
2148 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002149 emsg(_("cannot save undo information"));
Bram Moolenaar55d5c032010-07-17 23:52:29 +02002150 return;
Bram Moolenaar0ba04292010-07-14 23:23:17 +02002151 }
2152 luaV_setrange(L, eap->line1, eap->line2);
2153 luaL_buffinit(L, &b);
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002154 luaL_addlstring(&b, "return function(line, linenr) ", 30); // header
Bram Moolenaar0ba04292010-07-14 23:23:17 +02002155 luaL_addlstring(&b, s, strlen(s));
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002156 luaL_addlstring(&b, " end", 4); // footer
Bram Moolenaar0ba04292010-07-14 23:23:17 +02002157 luaL_pushresult(&b);
2158 s = lua_tolstring(L, -1, &len);
Bram Moolenaar55d5c032010-07-17 23:52:29 +02002159 if (luaL_loadbuffer(L, s, len, LUAVIM_CHUNKNAME))
2160 {
2161 luaV_emsg(L);
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002162 lua_pop(L, 1); // function body
Bram Moolenaar55d5c032010-07-17 23:52:29 +02002163 return;
Bram Moolenaar0ba04292010-07-14 23:23:17 +02002164 }
2165 lua_call(L, 0, 1);
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002166 lua_replace(L, -2); // function -> body
Bram Moolenaar55d5c032010-07-17 23:52:29 +02002167 for (l = eap->line1; l <= eap->line2; l++)
2168 {
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002169 // Check the line number, the command my have deleted lines.
Bram Moolenaard58f03b2017-01-29 22:48:45 +01002170 if (l > curbuf->b_ml.ml_line_count)
2171 break;
2172
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002173 lua_pushvalue(L, -1); // function
2174 luaV_pushline(L, curbuf, l); // current line as arg
2175 lua_pushinteger(L, l); // current line number as arg
Bram Moolenaarbd2f3c32012-04-06 14:31:00 +02002176 if (lua_pcall(L, 2, 1, 0))
Bram Moolenaar55d5c032010-07-17 23:52:29 +02002177 {
2178 luaV_emsg(L);
2179 break;
2180 }
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002181 // Catch the command switching to another buffer.
Bram Moolenaard58f03b2017-01-29 22:48:45 +01002182 if (curbuf != was_curbuf)
2183 break;
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002184 if (lua_isstring(L, -1)) // update line?
Bram Moolenaar55d5c032010-07-17 23:52:29 +02002185 {
Bram Moolenaar0ba04292010-07-14 23:23:17 +02002186#ifdef HAVE_SANDBOX
Bram Moolenaar55d5c032010-07-17 23:52:29 +02002187 luaV_checksandbox(L);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02002188#endif
Bram Moolenaar55d5c032010-07-17 23:52:29 +02002189 ml_replace(l, luaV_toline(L, -1), TRUE);
2190 changed_bytes(l, 0);
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002191 lua_pop(L, 1); // result from luaV_toline
Bram Moolenaar55d5c032010-07-17 23:52:29 +02002192 }
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002193 lua_pop(L, 1); // line
Bram Moolenaar0ba04292010-07-14 23:23:17 +02002194 }
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002195 lua_pop(L, 1); // function
Bram Moolenaar0ba04292010-07-14 23:23:17 +02002196 check_cursor();
2197 update_screen(NOT_VALID);
2198}
2199
Bram Moolenaar55d5c032010-07-17 23:52:29 +02002200 void
2201ex_luafile(exarg_T *eap)
2202{
2203 if (lua_init() == FAIL)
2204 return;
2205 if (!eap->skip)
2206 {
2207 luaV_setrange(L, eap->line1, eap->line2);
2208 if (luaL_loadfile(L, (char *) eap->arg) || lua_pcall(L, 0, 0, 0))
2209 luaV_emsg(L);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02002210 }
2211}
2212
Bram Moolenaar1dced572012-04-05 16:54:08 +02002213#define luaV_freetype(typ,tname) \
2214 void \
2215 lua_##tname##_free(typ *o) \
2216 { \
2217 if (!lua_isopen()) return; \
2218 luaV_getfield(L, LUAVIM_FREE); \
2219 lua_pushlightuserdata(L, (void *) o); \
2220 lua_call(L, 1, 0); \
2221 }
2222
2223luaV_freetype(buf_T, buffer)
2224luaV_freetype(win_T, window)
2225
Bram Moolenaar55d5c032010-07-17 23:52:29 +02002226 void
Bram Moolenaar4eefe472019-03-19 21:59:19 +01002227do_luaeval(char_u *str, typval_T *arg, typval_T *rettv)
Bram Moolenaar55d5c032010-07-17 23:52:29 +02002228{
Bram Moolenaar1dced572012-04-05 16:54:08 +02002229 lua_init();
2230 luaV_getfield(L, LUAVIM_LUAEVAL);
2231 lua_pushstring(L, (char *) str);
2232 lua_pushlightuserdata(L, (void *) arg);
2233 lua_pushlightuserdata(L, (void *) rettv);
2234 lua_call(L, 3, 0);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02002235}
2236
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01002237 int
Bram Moolenaar4eefe472019-03-19 21:59:19 +01002238set_ref_in_lua(int copyID)
Bram Moolenaar55d5c032010-07-17 23:52:29 +02002239{
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01002240 int aborted = 0;
2241
2242 if (lua_isopen())
2243 {
2244 luaV_getfield(L, LUAVIM_SETREF);
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002245 // call the function with 1 arg, getting 1 result back
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01002246 lua_pushinteger(L, copyID);
2247 lua_call(L, 1, 1);
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002248 // get the result
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01002249 aborted = lua_tointeger(L, -1);
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002250 // pop result off the stack
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01002251 lua_pop(L, 1);
2252 }
2253 return aborted;
Bram Moolenaar0ba04292010-07-14 23:23:17 +02002254}
2255
2256#endif