blob: b80b6c9e26c5c9d69c1094d3f30d33be4e43189f [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 void
402end_dynamic_lua(void)
403{
404 if (hinstLua)
405 {
Bram Moolenaar2334b6d2010-07-22 21:32:16 +0200406 close_dll(hinstLua);
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200407 hinstLua = 0;
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200408 }
409}
410
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200411 static int
412lua_link_init(char *libname, int verbose)
413{
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200414 const luaV_Reg *reg;
415 if (hinstLua) return OK;
Bram Moolenaar2334b6d2010-07-22 21:32:16 +0200416 hinstLua = load_dll(libname);
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200417 if (!hinstLua)
418 {
419 if (verbose)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100420 semsg(_(e_loadlib), libname);
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200421 return FAIL;
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200422 }
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200423 for (reg = luaV_dll; reg->func; reg++)
424 {
Bram Moolenaar2334b6d2010-07-22 21:32:16 +0200425 if ((*reg->func = symbol_from_dll(hinstLua, reg->name)) == NULL)
426 {
427 close_dll(hinstLua);
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200428 hinstLua = 0;
429 if (verbose)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100430 semsg(_(e_loadfunc), reg->name);
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200431 return FAIL;
432 }
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200433 }
434 return OK;
435}
Bram Moolenaar2ab2e862019-12-04 21:24:53 +0100436#endif // DYNAMIC_LUA
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200437
Bram Moolenaard90b6c02016-08-28 18:10:45 +0200438#if defined(DYNAMIC_LUA) || defined(PROTO)
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200439 int
440lua_enabled(int verbose)
441{
Bram Moolenaar25e4fcd2016-01-09 14:57:47 +0100442 return lua_link_init((char *)p_luadll, verbose) == OK;
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200443}
Bram Moolenaard90b6c02016-08-28 18:10:45 +0200444#endif
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200445
Bram Moolenaar1dced572012-04-05 16:54:08 +0200446#if LUA_VERSION_NUM > 501
447 static int
Bram Moolenaar4eefe472019-03-19 21:59:19 +0100448luaL_typeerror(lua_State *L, int narg, const char *tname)
Bram Moolenaar1dced572012-04-05 16:54:08 +0200449{
450 const char *msg = lua_pushfstring(L, "%s expected, got %s",
Bram Moolenaardb913952012-06-29 12:54:53 +0200451 tname, luaL_typename(L, narg));
Bram Moolenaar1dced572012-04-05 16:54:08 +0200452 return luaL_argerror(L, narg, msg);
453}
454#endif
455
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200456
Bram Moolenaar2ab2e862019-12-04 21:24:53 +0100457// ======= Internal =======
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200458
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200459 static void
460luaV_newmetatable(lua_State *L, const char *tname)
461{
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200462 lua_newtable(L);
463 lua_pushlightuserdata(L, (void *) tname);
464 lua_pushvalue(L, -2);
465 lua_rawset(L, LUA_REGISTRYINDEX);
466}
467
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200468 static void *
469luaV_toudata(lua_State *L, int ud, const char *tname)
470{
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200471 void *p = lua_touserdata(L, ud);
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200472
Bram Moolenaar2ab2e862019-12-04 21:24:53 +0100473 if (p != NULL) // value is userdata?
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200474 {
Bram Moolenaar2ab2e862019-12-04 21:24:53 +0100475 if (lua_getmetatable(L, ud)) // does it have a metatable?
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200476 {
Bram Moolenaar2ab2e862019-12-04 21:24:53 +0100477 luaV_getfield(L, tname); // get metatable
478 if (lua_rawequal(L, -1, -2)) // MTs match?
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200479 {
Bram Moolenaar2ab2e862019-12-04 21:24:53 +0100480 lua_pop(L, 2); // MTs
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200481 return p;
482 }
483 }
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200484 }
485 return NULL;
486}
487
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200488 static void *
Bram Moolenaar1dced572012-04-05 16:54:08 +0200489luaV_checkcache(lua_State *L, void *p)
490{
491 luaV_getudata(L, p);
492 if (lua_isnil(L, -1)) luaL_error(L, "invalid object");
493 lua_pop(L, 1);
494 return p;
495}
496
497#define luaV_unbox(L,luatyp,ud) (*((luatyp *) lua_touserdata((L),(ud))))
498
499#define luaV_checkvalid(L,luatyp,ud) \
500 luaV_checkcache((L), (void *) luaV_unbox((L),luatyp,(ud)))
501
502 static void *
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200503luaV_checkudata(lua_State *L, int ud, const char *tname)
504{
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200505 void *p = luaV_toudata(L, ud, tname);
Bram Moolenaar1dced572012-04-05 16:54:08 +0200506 if (p == NULL) luaL_typeerror(L, ud, tname);
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200507 return p;
508}
509
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200510 static void
511luaV_pushtypval(lua_State *L, typval_T *tv)
512{
Bram Moolenaar1dced572012-04-05 16:54:08 +0200513 if (tv == NULL)
514 {
515 lua_pushnil(L);
516 return;
517 }
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200518 switch (tv->v_type)
519 {
520 case VAR_STRING:
Bram Moolenaard04da7c2012-10-14 03:41:59 +0200521 lua_pushstring(L, tv->vval.v_string == NULL
522 ? "" : (char *)tv->vval.v_string);
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200523 break;
524 case VAR_NUMBER:
525 lua_pushinteger(L, (int) tv->vval.v_number);
526 break;
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200527#ifdef FEAT_FLOAT
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200528 case VAR_FLOAT:
529 lua_pushnumber(L, (lua_Number) tv->vval.v_float);
530 break;
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200531#endif
Bram Moolenaar1dced572012-04-05 16:54:08 +0200532 case VAR_LIST:
533 luaV_pushlist(L, tv->vval.v_list);
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200534 break;
Bram Moolenaar1dced572012-04-05 16:54:08 +0200535 case VAR_DICT:
536 luaV_pushdict(L, tv->vval.v_dict);
537 break;
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +0100538 case VAR_BOOL:
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100539 case VAR_SPECIAL:
540 if (tv->vval.v_number <= VVAL_TRUE)
541 lua_pushinteger(L, (int) tv->vval.v_number);
542 else
543 lua_pushnil(L);
544 break;
Bram Moolenaarca06da92018-07-01 15:12:05 +0200545 case VAR_FUNC:
Bram Moolenaar4eefe472019-03-19 21:59:19 +0100546 luaV_pushfuncref(L, tv->vval.v_string);
Bram Moolenaarca06da92018-07-01 15:12:05 +0200547 break;
Bram Moolenaarb7828692019-03-23 13:57:02 +0100548 case VAR_BLOB:
549 luaV_pushblob(L, tv->vval.v_blob);
550 break;
Bram Moolenaar1dced572012-04-05 16:54:08 +0200551 default:
552 lua_pushnil(L);
553 }
554}
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200555
Bram Moolenaarca06da92018-07-01 15:12:05 +0200556/*
557 * Converts lua value at 'pos' to typval 'tv'.
558 * Returns OK or FAIL.
559 */
560 static int
561luaV_totypval(lua_State *L, int pos, typval_T *tv)
Bram Moolenaar1dced572012-04-05 16:54:08 +0200562{
Bram Moolenaarca06da92018-07-01 15:12:05 +0200563 int status = OK;
564
565 switch (lua_type(L, pos))
566 {
Bram Moolenaar1dced572012-04-05 16:54:08 +0200567 case LUA_TBOOLEAN:
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +0100568 tv->v_type = VAR_BOOL;
Bram Moolenaar1dced572012-04-05 16:54:08 +0200569 tv->vval.v_number = (varnumber_T) lua_toboolean(L, pos);
570 break;
Bram Moolenaar9067cd62019-01-01 00:41:54 +0100571 case LUA_TNIL:
572 tv->v_type = VAR_SPECIAL;
573 tv->vval.v_number = VVAL_NULL;
574 break;
Bram Moolenaar1dced572012-04-05 16:54:08 +0200575 case LUA_TSTRING:
576 tv->v_type = VAR_STRING;
577 tv->vval.v_string = vim_strsave((char_u *) lua_tostring(L, pos));
578 break;
579 case LUA_TNUMBER:
580#ifdef FEAT_FLOAT
581 tv->v_type = VAR_FLOAT;
582 tv->vval.v_float = (float_T) lua_tonumber(L, pos);
583#else
584 tv->v_type = VAR_NUMBER;
585 tv->vval.v_number = (varnumber_T) lua_tointeger(L, pos);
586#endif
587 break;
Bram Moolenaarca06da92018-07-01 15:12:05 +0200588 case LUA_TUSERDATA:
589 {
Bram Moolenaar1dced572012-04-05 16:54:08 +0200590 void *p = lua_touserdata(L, pos);
Bram Moolenaarca06da92018-07-01 15:12:05 +0200591
Bram Moolenaarb7828692019-03-23 13:57:02 +0100592 if (lua_getmetatable(L, pos)) // has metatable?
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200593 {
Bram Moolenaarb7828692019-03-23 13:57:02 +0100594 // check list
Bram Moolenaar1dced572012-04-05 16:54:08 +0200595 luaV_getfield(L, LUAVIM_LIST);
596 if (lua_rawequal(L, -1, -2))
Bram Moolenaar2334b6d2010-07-22 21:32:16 +0200597 {
Bram Moolenaar1dced572012-04-05 16:54:08 +0200598 tv->v_type = VAR_LIST;
599 tv->vval.v_list = *((luaV_List *) p);
600 ++tv->vval.v_list->lv_refcount;
Bram Moolenaarb7828692019-03-23 13:57:02 +0100601 lua_pop(L, 2); // MTs
Bram Moolenaarca06da92018-07-01 15:12:05 +0200602 break;
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200603 }
Bram Moolenaarb7828692019-03-23 13:57:02 +0100604 // check dict
Bram Moolenaar1dced572012-04-05 16:54:08 +0200605 luaV_getfield(L, LUAVIM_DICT);
606 if (lua_rawequal(L, -1, -3))
607 {
608 tv->v_type = VAR_DICT;
609 tv->vval.v_dict = *((luaV_Dict *) p);
610 ++tv->vval.v_dict->dv_refcount;
Bram Moolenaarb7828692019-03-23 13:57:02 +0100611 lua_pop(L, 3); // MTs
Bram Moolenaarca06da92018-07-01 15:12:05 +0200612 break;
Bram Moolenaar1dced572012-04-05 16:54:08 +0200613 }
Bram Moolenaarb7828692019-03-23 13:57:02 +0100614 // check blob
615 luaV_getfield(L, LUAVIM_BLOB);
Bram Moolenaarca06da92018-07-01 15:12:05 +0200616 if (lua_rawequal(L, -1, -4))
617 {
Bram Moolenaarb7828692019-03-23 13:57:02 +0100618 tv->v_type = VAR_BLOB;
619 tv->vval.v_blob = *((luaV_Blob *) p);
620 ++tv->vval.v_blob->bv_refcount;
621 lua_pop(L, 4); // MTs
622 break;
623 }
624 // check funcref
625 luaV_getfield(L, LUAVIM_FUNCREF);
626 if (lua_rawequal(L, -1, -5))
627 {
Bram Moolenaarca06da92018-07-01 15:12:05 +0200628 luaV_Funcref *f = (luaV_Funcref *) p;
Bram Moolenaar4eefe472019-03-19 21:59:19 +0100629 func_ref(f->name);
630 tv->v_type = VAR_FUNC;
631 tv->vval.v_string = vim_strsave(f->name);
Bram Moolenaarb7828692019-03-23 13:57:02 +0100632 lua_pop(L, 5); // MTs
Bram Moolenaarca06da92018-07-01 15:12:05 +0200633 break;
634 }
Bram Moolenaarb7828692019-03-23 13:57:02 +0100635 lua_pop(L, 4); // MTs
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200636 }
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200637 }
Bram Moolenaarb7828692019-03-23 13:57:02 +0100638 // FALLTHROUGH
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200639 default:
Bram Moolenaar1dced572012-04-05 16:54:08 +0200640 tv->v_type = VAR_NUMBER;
641 tv->vval.v_number = 0;
Bram Moolenaarca06da92018-07-01 15:12:05 +0200642 status = FAIL;
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200643 }
Bram Moolenaarca06da92018-07-01 15:12:05 +0200644 return status;
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200645}
646
Bram Moolenaar2ab2e862019-12-04 21:24:53 +0100647/*
648 * similar to luaL_addlstring, but replaces \0 with \n if toline and
649 * \n with \0 otherwise
650 */
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200651 static void
652luaV_addlstring(luaL_Buffer *b, const char *s, size_t l, int toline)
653{
654 while (l--)
655 {
656 if (*s == '\0' && toline)
657 luaL_addchar(b, '\n');
658 else if (*s == '\n' && !toline)
659 luaL_addchar(b, '\0');
660 else
661 luaL_addchar(b, *s);
662 s++;
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200663 }
664}
665
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200666 static void
667luaV_pushline(lua_State *L, buf_T *buf, linenr_T n)
668{
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200669 const char *s = (const char *) ml_get_buf(buf, n, FALSE);
670 luaL_Buffer b;
671 luaL_buffinit(L, &b);
672 luaV_addlstring(&b, s, strlen(s), 0);
673 luaL_pushresult(&b);
674}
675
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200676 static char_u *
677luaV_toline(lua_State *L, int pos)
678{
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200679 size_t l;
680 const char *s = lua_tolstring(L, pos, &l);
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200681
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200682 luaL_Buffer b;
683 luaL_buffinit(L, &b);
684 luaV_addlstring(&b, s, l, 1);
685 luaL_pushresult(&b);
686 return (char_u *) lua_tostring(L, -1);
687}
688
Bram Moolenaar2ab2e862019-12-04 21:24:53 +0100689/*
690 * pops a string s from the top of the stack and calls mf(t) for pieces t of
691 * s separated by newlines
692 */
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200693 static void
694luaV_msgfunc(lua_State *L, msgfunc_T mf)
695{
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200696 luaL_Buffer b;
697 size_t l;
698 const char *p, *s = lua_tolstring(L, -1, &l);
699 luaL_buffinit(L, &b);
700 luaV_addlstring(&b, s, l, 0);
701 luaL_pushresult(&b);
Bram Moolenaar2ab2e862019-12-04 21:24:53 +0100702 // break string
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200703 p = s = lua_tolstring(L, -1, &l);
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200704 while (l--)
705 {
Bram Moolenaar2ab2e862019-12-04 21:24:53 +0100706 if (*p++ == '\0') // break?
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200707 {
708 mf((char_u *) s);
709 s = p;
710 }
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200711 }
712 mf((char_u *) s);
Bram Moolenaar2ab2e862019-12-04 21:24:53 +0100713 lua_pop(L, 2); // original and modified strings
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200714}
715
Bram Moolenaar1dced572012-04-05 16:54:08 +0200716#define luaV_newtype(typ,tname,luatyp,luatname) \
717 static luatyp * \
Bram Moolenaar4eefe472019-03-19 21:59:19 +0100718 luaV_new##tname(lua_State *L, typ *obj) \
Bram Moolenaar1dced572012-04-05 16:54:08 +0200719 { \
720 luatyp *o = (luatyp *) lua_newuserdata(L, sizeof(luatyp)); \
721 *o = obj; \
722 luaV_setudata(L, obj); /* cache[obj] = udata */ \
723 luaV_getfield(L, luatname); \
724 lua_setmetatable(L, -2); \
725 return o; \
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200726 }
Bram Moolenaar1dced572012-04-05 16:54:08 +0200727
728#define luaV_pushtype(typ,tname,luatyp) \
729 static luatyp * \
Bram Moolenaarca06da92018-07-01 15:12:05 +0200730 luaV_push##tname(lua_State *L, typ *obj) \
Bram Moolenaar1dced572012-04-05 16:54:08 +0200731 { \
732 luatyp *o = NULL; \
733 if (obj == NULL) \
734 lua_pushnil(L); \
735 else { \
736 luaV_getudata(L, obj); \
737 if (lua_isnil(L, -1)) /* not interned? */ \
738 { \
739 lua_pop(L, 1); \
740 o = luaV_new##tname(L, obj); \
741 } \
742 else \
743 o = (luatyp *) lua_touserdata(L, -1); \
744 } \
745 return o; \
746 }
747
748#define luaV_type_tostring(tname,luatname) \
749 static int \
Bram Moolenaar4eefe472019-03-19 21:59:19 +0100750 luaV_##tname##_tostring(lua_State *L) \
Bram Moolenaar1dced572012-04-05 16:54:08 +0200751 { \
752 lua_pushfstring(L, "%s: %p", luatname, lua_touserdata(L, 1)); \
753 return 1; \
754 }
755
Bram Moolenaar2ab2e862019-12-04 21:24:53 +0100756// ======= List type =======
Bram Moolenaar1dced572012-04-05 16:54:08 +0200757
758 static luaV_List *
Bram Moolenaar4eefe472019-03-19 21:59:19 +0100759luaV_newlist(lua_State *L, list_T *lis)
Bram Moolenaar1dced572012-04-05 16:54:08 +0200760{
761 luaV_List *l = (luaV_List *) lua_newuserdata(L, sizeof(luaV_List));
762 *l = lis;
Bram Moolenaar2ab2e862019-12-04 21:24:53 +0100763 lis->lv_refcount++; // reference in Lua
764 luaV_setudata(L, lis); // cache[lis] = udata
Bram Moolenaar1dced572012-04-05 16:54:08 +0200765 luaV_getfield(L, LUAVIM_LIST);
766 lua_setmetatable(L, -2);
767 return l;
768}
769
770luaV_pushtype(list_T, list, luaV_List)
771luaV_type_tostring(list, LUAVIM_LIST)
772
773 static int
Bram Moolenaar4eefe472019-03-19 21:59:19 +0100774luaV_list_len(lua_State *L)
Bram Moolenaar1dced572012-04-05 16:54:08 +0200775{
776 list_T *l = luaV_unbox(L, luaV_List, 1);
Bram Moolenaarb7828692019-03-23 13:57:02 +0100777 lua_pushinteger(L, (int) list_len(l));
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200778 return 1;
779}
780
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200781 static int
Bram Moolenaar4eefe472019-03-19 21:59:19 +0100782luaV_list_iter(lua_State *L)
Bram Moolenaar1dced572012-04-05 16:54:08 +0200783{
784 listitem_T *li = (listitem_T *) lua_touserdata(L, lua_upvalueindex(2));
785 if (li == NULL) return 0;
786 luaV_pushtypval(L, &li->li_tv);
787 lua_pushlightuserdata(L, (void *) li->li_next);
788 lua_replace(L, lua_upvalueindex(2));
789 return 1;
790}
791
792 static int
Bram Moolenaar4eefe472019-03-19 21:59:19 +0100793luaV_list_call(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 lua_pushvalue(L, lua_upvalueindex(1)); // pass cache table along
Bram Moolenaar1dced572012-04-05 16:54:08 +0200797 lua_pushlightuserdata(L, (void *) l->lv_first);
798 lua_pushcclosure(L, luaV_list_iter, 2);
799 return 1;
800}
801
802 static int
Bram Moolenaar4eefe472019-03-19 21:59:19 +0100803luaV_list_index(lua_State *L)
Bram Moolenaar1dced572012-04-05 16:54:08 +0200804{
805 list_T *l = luaV_unbox(L, luaV_List, 1);
Bram Moolenaar2ab2e862019-12-04 21:24:53 +0100806 if (lua_isnumber(L, 2)) // list item?
Bram Moolenaar1dced572012-04-05 16:54:08 +0200807 {
808 listitem_T *li = list_find(l, (long) luaL_checkinteger(L, 2));
809 if (li == NULL)
810 lua_pushnil(L);
811 else
812 luaV_pushtypval(L, &li->li_tv);
813 }
Bram Moolenaar2ab2e862019-12-04 21:24:53 +0100814 else if (lua_isstring(L, 2)) // method?
Bram Moolenaar1dced572012-04-05 16:54:08 +0200815 {
816 const char *s = lua_tostring(L, 2);
817 if (strncmp(s, "add", 3) == 0
Bram Moolenaarb3766472013-04-15 13:49:21 +0200818 || strncmp(s, "insert", 6) == 0)
Bram Moolenaar1dced572012-04-05 16:54:08 +0200819 {
820 lua_getmetatable(L, 1);
821 lua_getfield(L, -1, s);
822 }
823 else
824 lua_pushnil(L);
825 }
826 else
827 lua_pushnil(L);
828 return 1;
829}
830
831 static int
Bram Moolenaar4eefe472019-03-19 21:59:19 +0100832luaV_list_newindex(lua_State *L)
Bram Moolenaar1dced572012-04-05 16:54:08 +0200833{
834 list_T *l = luaV_unbox(L, luaV_List, 1);
835 long n = (long) luaL_checkinteger(L, 2);
836 listitem_T *li;
837 if (l->lv_lock)
838 luaL_error(L, "list is locked");
839 li = list_find(l, n);
840 if (li == NULL) return 0;
Bram Moolenaar2ab2e862019-12-04 21:24:53 +0100841 if (lua_isnil(L, 3)) // remove?
Bram Moolenaar1dced572012-04-05 16:54:08 +0200842 {
Bram Moolenaar3ec7f4e2014-05-07 17:31:37 +0200843 vimlist_remove(l, li, li);
Bram Moolenaar1dced572012-04-05 16:54:08 +0200844 clear_tv(&li->li_tv);
845 vim_free(li);
846 }
847 else
848 {
849 typval_T v;
Bram Moolenaarca06da92018-07-01 15:12:05 +0200850 luaV_checktypval(L, 3, &v, "setting list item");
Bram Moolenaar1dced572012-04-05 16:54:08 +0200851 clear_tv(&li->li_tv);
852 copy_tv(&v, &li->li_tv);
Bram Moolenaarb3766472013-04-15 13:49:21 +0200853 clear_tv(&v);
Bram Moolenaar1dced572012-04-05 16:54:08 +0200854 }
855 return 0;
856}
857
858 static int
Bram Moolenaar4eefe472019-03-19 21:59:19 +0100859luaV_list_add(lua_State *L)
Bram Moolenaar1dced572012-04-05 16:54:08 +0200860{
861 luaV_List *lis = luaV_checkudata(L, 1, LUAVIM_LIST);
862 list_T *l = (list_T *) luaV_checkcache(L, (void *) *lis);
Bram Moolenaarb3766472013-04-15 13:49:21 +0200863 typval_T v;
Bram Moolenaar1dced572012-04-05 16:54:08 +0200864 if (l->lv_lock)
865 luaL_error(L, "list is locked");
Bram Moolenaarb3766472013-04-15 13:49:21 +0200866 lua_settop(L, 2);
Bram Moolenaarca06da92018-07-01 15:12:05 +0200867 luaV_checktypval(L, 2, &v, "adding list item");
Bram Moolenaarb3766472013-04-15 13:49:21 +0200868 if (list_append_tv(l, &v) == FAIL)
Bram Moolenaarca06da92018-07-01 15:12:05 +0200869 luaL_error(L, "failed to add item to list");
Bram Moolenaarb3766472013-04-15 13:49:21 +0200870 clear_tv(&v);
Bram Moolenaar1dced572012-04-05 16:54:08 +0200871 lua_settop(L, 1);
872 return 1;
873}
874
875 static int
Bram Moolenaar4eefe472019-03-19 21:59:19 +0100876luaV_list_insert(lua_State *L)
Bram Moolenaar1dced572012-04-05 16:54:08 +0200877{
878 luaV_List *lis = luaV_checkudata(L, 1, LUAVIM_LIST);
879 list_T *l = (list_T *) luaV_checkcache(L, (void *) *lis);
Bram Moolenaar46538ee2015-02-17 16:28:55 +0100880 long pos = (long) luaL_optinteger(L, 3, 0);
Bram Moolenaar1dced572012-04-05 16:54:08 +0200881 listitem_T *li = NULL;
882 typval_T v;
883 if (l->lv_lock)
884 luaL_error(L, "list is locked");
885 if (pos < l->lv_len)
886 {
887 li = list_find(l, pos);
888 if (li == NULL)
889 luaL_error(L, "invalid position");
890 }
891 lua_settop(L, 2);
Bram Moolenaarca06da92018-07-01 15:12:05 +0200892 luaV_checktypval(L, 2, &v, "inserting list item");
Bram Moolenaarb3766472013-04-15 13:49:21 +0200893 if (list_insert_tv(l, &v, li) == FAIL)
Bram Moolenaarca06da92018-07-01 15:12:05 +0200894 luaL_error(L, "failed to add item to list");
Bram Moolenaarb3766472013-04-15 13:49:21 +0200895 clear_tv(&v);
Bram Moolenaar1dced572012-04-05 16:54:08 +0200896 lua_settop(L, 1);
897 return 1;
898}
899
900static const luaL_Reg luaV_List_mt[] = {
901 {"__tostring", luaV_list_tostring},
Bram Moolenaar1dced572012-04-05 16:54:08 +0200902 {"__len", luaV_list_len},
903 {"__call", luaV_list_call},
904 {"__index", luaV_list_index},
905 {"__newindex", luaV_list_newindex},
906 {"add", luaV_list_add},
907 {"insert", luaV_list_insert},
908 {NULL, NULL}
909};
910
911
Bram Moolenaar2ab2e862019-12-04 21:24:53 +0100912// ======= Dict type =======
Bram Moolenaar1dced572012-04-05 16:54:08 +0200913
914 static luaV_Dict *
Bram Moolenaar4eefe472019-03-19 21:59:19 +0100915luaV_newdict(lua_State *L, dict_T *dic)
Bram Moolenaar1dced572012-04-05 16:54:08 +0200916{
917 luaV_Dict *d = (luaV_Dict *) lua_newuserdata(L, sizeof(luaV_Dict));
918 *d = dic;
Bram Moolenaar2ab2e862019-12-04 21:24:53 +0100919 dic->dv_refcount++; // reference in Lua
920 luaV_setudata(L, dic); // cache[dic] = udata
Bram Moolenaar1dced572012-04-05 16:54:08 +0200921 luaV_getfield(L, LUAVIM_DICT);
922 lua_setmetatable(L, -2);
923 return d;
924}
925
926luaV_pushtype(dict_T, dict, luaV_Dict)
927luaV_type_tostring(dict, LUAVIM_DICT)
928
929 static int
Bram Moolenaar4eefe472019-03-19 21:59:19 +0100930luaV_dict_len(lua_State *L)
Bram Moolenaar1dced572012-04-05 16:54:08 +0200931{
932 dict_T *d = luaV_unbox(L, luaV_Dict, 1);
Bram Moolenaarb7828692019-03-23 13:57:02 +0100933 lua_pushinteger(L, (int) dict_len(d));
Bram Moolenaar1dced572012-04-05 16:54:08 +0200934 return 1;
935}
936
937 static int
Bram Moolenaar4eefe472019-03-19 21:59:19 +0100938luaV_dict_iter(lua_State *L UNUSED)
Bram Moolenaar1dced572012-04-05 16:54:08 +0200939{
Bram Moolenaarfeeaa682013-02-14 22:19:51 +0100940#ifdef FEAT_EVAL
Bram Moolenaar1dced572012-04-05 16:54:08 +0200941 hashitem_T *hi = (hashitem_T *) lua_touserdata(L, lua_upvalueindex(2));
942 int n = lua_tointeger(L, lua_upvalueindex(3));
943 dictitem_T *di;
944 if (n <= 0) return 0;
945 while (HASHITEM_EMPTY(hi)) hi++;
946 di = dict_lookup(hi);
947 lua_pushstring(L, (char *) hi->hi_key);
948 luaV_pushtypval(L, &di->di_tv);
949 lua_pushlightuserdata(L, (void *) (hi + 1));
950 lua_replace(L, lua_upvalueindex(2));
951 lua_pushinteger(L, n - 1);
952 lua_replace(L, lua_upvalueindex(3));
953 return 2;
Bram Moolenaarfeeaa682013-02-14 22:19:51 +0100954#else
955 return 0;
956#endif
Bram Moolenaar1dced572012-04-05 16:54:08 +0200957}
958
959 static int
Bram Moolenaar4eefe472019-03-19 21:59:19 +0100960luaV_dict_call(lua_State *L)
Bram Moolenaar1dced572012-04-05 16:54:08 +0200961{
962 dict_T *d = luaV_unbox(L, luaV_Dict, 1);
963 hashtab_T *ht = &d->dv_hashtab;
Bram Moolenaar2ab2e862019-12-04 21:24:53 +0100964 lua_pushvalue(L, lua_upvalueindex(1)); // pass cache table along
Bram Moolenaar1dced572012-04-05 16:54:08 +0200965 lua_pushlightuserdata(L, (void *) ht->ht_array);
Bram Moolenaar2ab2e862019-12-04 21:24:53 +0100966 lua_pushinteger(L, ht->ht_used); // # remaining items
Bram Moolenaar1dced572012-04-05 16:54:08 +0200967 lua_pushcclosure(L, luaV_dict_iter, 3);
968 return 1;
969}
970
971 static int
Bram Moolenaarca06da92018-07-01 15:12:05 +0200972luaV_dict_index(lua_State *L)
Bram Moolenaar1dced572012-04-05 16:54:08 +0200973{
974 dict_T *d = luaV_unbox(L, luaV_Dict, 1);
975 char_u *key = (char_u *) luaL_checkstring(L, 2);
976 dictitem_T *di = dict_find(d, key, -1);
Bram Moolenaarca06da92018-07-01 15:12:05 +0200977
Bram Moolenaar1dced572012-04-05 16:54:08 +0200978 if (di == NULL)
979 lua_pushnil(L);
980 else
Bram Moolenaarca06da92018-07-01 15:12:05 +0200981 {
Bram Moolenaar1dced572012-04-05 16:54:08 +0200982 luaV_pushtypval(L, &di->di_tv);
Bram Moolenaar2ab2e862019-12-04 21:24:53 +0100983 if (di->di_tv.v_type == VAR_FUNC) // funcref?
Bram Moolenaarca06da92018-07-01 15:12:05 +0200984 {
985 luaV_Funcref *f = (luaV_Funcref *) lua_touserdata(L, -1);
Bram Moolenaar2ab2e862019-12-04 21:24:53 +0100986 f->self = d; // keep "self" reference
Bram Moolenaarca06da92018-07-01 15:12:05 +0200987 d->dv_refcount++;
988 }
989 }
Bram Moolenaar1dced572012-04-05 16:54:08 +0200990 return 1;
991}
992
993 static int
Bram Moolenaarca06da92018-07-01 15:12:05 +0200994luaV_dict_newindex(lua_State *L)
Bram Moolenaar1dced572012-04-05 16:54:08 +0200995{
996 dict_T *d = luaV_unbox(L, luaV_Dict, 1);
997 char_u *key = (char_u *) luaL_checkstring(L, 2);
998 dictitem_T *di;
Bram Moolenaarca06da92018-07-01 15:12:05 +0200999 typval_T v;
Bram Moolenaar713bf9e92019-03-16 16:38:41 +01001000
Bram Moolenaar1dced572012-04-05 16:54:08 +02001001 if (d->dv_lock)
1002 luaL_error(L, "dict is locked");
Bram Moolenaard6ef5f92018-07-13 22:08:23 +02001003 if (key == NULL)
1004 return 0;
1005 if (*key == NUL)
Bram Moolenaarca06da92018-07-01 15:12:05 +02001006 luaL_error(L, "empty key");
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001007 if (!lua_isnil(L, 3)) // read value?
Bram Moolenaar17413672018-07-14 20:49:42 +02001008 {
Bram Moolenaarca06da92018-07-01 15:12:05 +02001009 luaV_checktypval(L, 3, &v, "setting dict item");
1010 if (d->dv_scope == VAR_DEF_SCOPE && v.v_type == VAR_FUNC)
1011 luaL_error(L, "cannot assign funcref to builtin scope");
1012 }
Bram Moolenaar1dced572012-04-05 16:54:08 +02001013 di = dict_find(d, key, -1);
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001014 if (di == NULL) // non-existing key?
Bram Moolenaar1dced572012-04-05 16:54:08 +02001015 {
Bram Moolenaard6ef5f92018-07-13 22:08:23 +02001016 if (lua_isnil(L, 3))
1017 return 0;
Bram Moolenaar1dced572012-04-05 16:54:08 +02001018 di = dictitem_alloc(key);
Bram Moolenaard6ef5f92018-07-13 22:08:23 +02001019 if (di == NULL)
1020 return 0;
Bram Moolenaar1dced572012-04-05 16:54:08 +02001021 if (dict_add(d, di) == FAIL)
1022 {
Bram Moolenaard6ef5f92018-07-13 22:08:23 +02001023 vim_free(di);
1024 return 0;
Bram Moolenaar1dced572012-04-05 16:54:08 +02001025 }
1026 }
1027 else
1028 clear_tv(&di->di_tv);
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001029 if (lua_isnil(L, 3)) // remove?
Bram Moolenaar1dced572012-04-05 16:54:08 +02001030 {
1031 hashitem_T *hi = hash_find(&d->dv_hashtab, di->di_key);
1032 hash_remove(&d->dv_hashtab, hi);
1033 dictitem_free(di);
1034 }
Bram Moolenaarca06da92018-07-01 15:12:05 +02001035 else
1036 {
Bram Moolenaar1dced572012-04-05 16:54:08 +02001037 copy_tv(&v, &di->di_tv);
Bram Moolenaarb3766472013-04-15 13:49:21 +02001038 clear_tv(&v);
Bram Moolenaar1dced572012-04-05 16:54:08 +02001039 }
1040 return 0;
1041}
1042
1043static const luaL_Reg luaV_Dict_mt[] = {
1044 {"__tostring", luaV_dict_tostring},
Bram Moolenaar1dced572012-04-05 16:54:08 +02001045 {"__len", luaV_dict_len},
1046 {"__call", luaV_dict_call},
1047 {"__index", luaV_dict_index},
1048 {"__newindex", luaV_dict_newindex},
1049 {NULL, NULL}
1050};
1051
1052
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001053// ======= Blob type =======
Bram Moolenaarb7828692019-03-23 13:57:02 +01001054
1055 static luaV_Blob *
1056luaV_newblob(lua_State *L, blob_T *blo)
1057{
1058 luaV_Blob *b = (luaV_Blob *) lua_newuserdata(L, sizeof(luaV_Blob));
1059 *b = blo;
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001060 blo->bv_refcount++; // reference in Lua
1061 luaV_setudata(L, blo); // cache[blo] = udata
Bram Moolenaarb7828692019-03-23 13:57:02 +01001062 luaV_getfield(L, LUAVIM_BLOB);
1063 lua_setmetatable(L, -2);
1064 return b;
1065}
1066
1067luaV_pushtype(blob_T, blob, luaV_Blob)
1068luaV_type_tostring(blob, LUAVIM_BLOB)
1069
1070 static int
1071luaV_blob_gc(lua_State *L)
1072{
1073 blob_T *b = luaV_unbox(L, luaV_Blob, 1);
1074 blob_unref(b);
1075 return 0;
1076}
1077
1078 static int
1079luaV_blob_len(lua_State *L)
1080{
1081 blob_T *b = luaV_unbox(L, luaV_Blob, 1);
1082 lua_pushinteger(L, (int) blob_len(b));
1083 return 1;
1084}
1085
1086 static int
1087luaV_blob_index(lua_State *L)
1088{
1089 blob_T *b = luaV_unbox(L, luaV_Blob, 1);
1090 if (lua_isnumber(L, 2))
1091 {
1092 int idx = luaL_checkinteger(L, 2);
1093 if (idx < blob_len(b))
1094 lua_pushnumber(L, (lua_Number) blob_get(b, idx));
1095 else
1096 lua_pushnil(L);
1097 }
1098 else if (lua_isstring(L, 2))
1099 {
1100 const char *s = lua_tostring(L, 2);
1101 if (strncmp(s, "add", 3) == 0)
1102 {
1103 lua_getmetatable(L, 1);
1104 lua_getfield(L, -1, s);
1105 }
1106 else
1107 lua_pushnil(L);
1108 }
1109 else
1110 lua_pushnil(L);
1111 return 1;
1112}
1113
1114 static int
1115luaV_blob_newindex(lua_State *L)
1116{
1117 blob_T *b = luaV_unbox(L, luaV_Blob, 1);
1118 if (b->bv_lock)
1119 luaL_error(L, "blob is locked");
1120 if (lua_isnumber(L, 2))
1121 {
1122 long len = blob_len(b);
1123 int idx = luaL_checkinteger(L, 2);
1124 int val = luaL_checkinteger(L, 3);
1125 if (idx < len || (idx == len && ga_grow(&b->bv_ga, 1) == OK))
1126 {
1127 blob_set(b, idx, (char_u) val);
1128 if (idx == len)
1129 ++b->bv_ga.ga_len;
1130 }
1131 else
1132 luaL_error(L, "index out of range");
1133 }
1134 return 0;
1135}
1136
1137 static int
1138luaV_blob_add(lua_State *L)
1139{
1140 luaV_Blob *blo = luaV_checkudata(L, 1, LUAVIM_BLOB);
1141 blob_T *b = (blob_T *) luaV_checkcache(L, (void *) *blo);
1142 if (b->bv_lock)
1143 luaL_error(L, "blob is locked");
1144 lua_settop(L, 2);
1145 if (!lua_isstring(L, 2))
1146 luaL_error(L, "string expected, got %s", luaL_typename(L, 2));
1147 else
1148 {
1149 size_t i, l = 0;
1150 const char *s = lua_tolstring(L, 2, &l);
1151
Bram Moolenaar6fb5c972019-03-26 21:44:20 +01001152 if (ga_grow(&b->bv_ga, l) == OK)
1153 for (i = 0; i < l; ++i)
1154 ga_append(&b->bv_ga, s[i]);
Bram Moolenaarb7828692019-03-23 13:57:02 +01001155 }
1156 lua_settop(L, 1);
1157 return 1;
1158}
1159
1160static const luaL_Reg luaV_Blob_mt[] = {
1161 {"__tostring", luaV_blob_tostring},
1162 {"__gc", luaV_blob_gc},
1163 {"__len", luaV_blob_len},
1164 {"__index", luaV_blob_index},
1165 {"__newindex", luaV_blob_newindex},
1166 {"add", luaV_blob_add},
1167 {NULL, NULL}
1168};
1169
1170
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001171// ======= Funcref type =======
Bram Moolenaarca06da92018-07-01 15:12:05 +02001172
1173 static luaV_Funcref *
1174luaV_newfuncref(lua_State *L, char_u *name)
1175{
1176 luaV_Funcref *f = (luaV_Funcref *)lua_newuserdata(L, sizeof(luaV_Funcref));
1177
1178 if (name != NULL)
1179 {
Bram Moolenaar4eefe472019-03-19 21:59:19 +01001180 func_ref(name);
1181 f->name = vim_strsave(name);
Bram Moolenaarca06da92018-07-01 15:12:05 +02001182 }
Bram Moolenaarca06da92018-07-01 15:12:05 +02001183 f->self = NULL;
1184 luaV_getfield(L, LUAVIM_FUNCREF);
1185 lua_setmetatable(L, -2);
1186 return f;
1187}
1188
1189 static luaV_Funcref *
Bram Moolenaar4eefe472019-03-19 21:59:19 +01001190luaV_pushfuncref(lua_State *L, char_u *name)
Bram Moolenaarca06da92018-07-01 15:12:05 +02001191{
Bram Moolenaar4eefe472019-03-19 21:59:19 +01001192 return luaV_newfuncref(L, name);
Bram Moolenaarca06da92018-07-01 15:12:05 +02001193}
1194
1195
1196luaV_type_tostring(funcref, LUAVIM_FUNCREF)
1197
1198 static int
1199luaV_funcref_gc(lua_State *L)
1200{
1201 luaV_Funcref *f = (luaV_Funcref *) lua_touserdata(L, 1);
1202
Bram Moolenaar4eefe472019-03-19 21:59:19 +01001203 func_unref(f->name);
1204 vim_free(f->name);
1205 // NOTE: Don't call "dict_unref(f->self)", because the dict of "f->self"
1206 // will be (or has been already) freed by Vim's garbage collection.
Bram Moolenaarca06da92018-07-01 15:12:05 +02001207 return 0;
1208}
1209
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001210// equivalent to string(funcref)
Bram Moolenaarca06da92018-07-01 15:12:05 +02001211 static int
1212luaV_funcref_len(lua_State *L)
1213{
1214 luaV_Funcref *f = (luaV_Funcref *) lua_touserdata(L, 1);
1215
Bram Moolenaar4eefe472019-03-19 21:59:19 +01001216 lua_pushstring(L, (const char *) f->name);
Bram Moolenaarca06da92018-07-01 15:12:05 +02001217 return 1;
1218}
1219
1220 static int
1221luaV_funcref_call(lua_State *L)
1222{
1223 luaV_Funcref *f = (luaV_Funcref *) lua_touserdata(L, 1);
Bram Moolenaar4eefe472019-03-19 21:59:19 +01001224 int i, n = lua_gettop(L) - 1; // #args
1225 int status = FAIL;
1226 typval_T args;
1227 typval_T rettv;
Bram Moolenaarca06da92018-07-01 15:12:05 +02001228
Bram Moolenaar4eefe472019-03-19 21:59:19 +01001229 args.v_type = VAR_LIST;
1230 args.vval.v_list = list_alloc();
1231 rettv.v_type = VAR_UNKNOWN; // as in clear_tv
1232 if (args.vval.v_list != NULL)
Bram Moolenaard6ef5f92018-07-13 22:08:23 +02001233 {
Bram Moolenaar4eefe472019-03-19 21:59:19 +01001234 typval_T v;
1235
Bram Moolenaar17413672018-07-14 20:49:42 +02001236 for (i = 0; i < n; i++)
1237 {
Bram Moolenaard6ef5f92018-07-13 22:08:23 +02001238 luaV_checktypval(L, i + 2, &v, "calling funcref");
Bram Moolenaar4eefe472019-03-19 21:59:19 +01001239 list_append_tv(args.vval.v_list, &v);
Bram Moolenaar713bf9e92019-03-16 16:38:41 +01001240 clear_tv(&v);
Bram Moolenaard6ef5f92018-07-13 22:08:23 +02001241 }
Bram Moolenaar4eefe472019-03-19 21:59:19 +01001242 status = func_call(f->name, &args, NULL, f->self, &rettv);
Bram Moolenaard6ef5f92018-07-13 22:08:23 +02001243 if (status == OK)
1244 luaV_pushtypval(L, &rettv);
Bram Moolenaar4eefe472019-03-19 21:59:19 +01001245 clear_tv(&args);
Bram Moolenaard6ef5f92018-07-13 22:08:23 +02001246 clear_tv(&rettv);
Bram Moolenaarca06da92018-07-01 15:12:05 +02001247 }
Bram Moolenaarca06da92018-07-01 15:12:05 +02001248 if (status != OK)
1249 luaL_error(L, "cannot call funcref");
1250 return 1;
1251}
1252
1253static const luaL_Reg luaV_Funcref_mt[] = {
1254 {"__tostring", luaV_funcref_tostring},
1255 {"__gc", luaV_funcref_gc},
1256 {"__len", luaV_funcref_len},
1257 {"__call", luaV_funcref_call},
1258 {NULL, NULL}
1259};
1260
1261
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001262// ======= Buffer type =======
Bram Moolenaar1dced572012-04-05 16:54:08 +02001263
1264luaV_newtype(buf_T, buffer, luaV_Buffer, LUAVIM_BUFFER)
1265luaV_pushtype(buf_T, buffer, luaV_Buffer)
1266luaV_type_tostring(buffer, LUAVIM_BUFFER)
1267
1268 static int
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001269luaV_buffer_len(lua_State *L)
1270{
Bram Moolenaar1dced572012-04-05 16:54:08 +02001271 buf_T *b = (buf_T *) luaV_checkvalid(L, luaV_Buffer, 1);
1272 lua_pushinteger(L, b->b_ml.ml_line_count);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001273 return 1;
1274}
1275
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001276 static int
1277luaV_buffer_call(lua_State *L)
1278{
Bram Moolenaar1dced572012-04-05 16:54:08 +02001279 buf_T *b = (buf_T *) luaV_checkvalid(L, luaV_Buffer, 1);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001280 lua_settop(L, 1);
Bram Moolenaar1dced572012-04-05 16:54:08 +02001281 set_curbuf(b, DOBUF_SPLIT);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001282 return 1;
1283}
1284
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001285 static int
1286luaV_buffer_index(lua_State *L)
1287{
Bram Moolenaar1dced572012-04-05 16:54:08 +02001288 buf_T *b = (buf_T *) luaV_checkvalid(L, luaV_Buffer, 1);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001289 linenr_T n = (linenr_T) lua_tointeger(L, 2);
Bram Moolenaar1dced572012-04-05 16:54:08 +02001290 if (n > 0 && n <= b->b_ml.ml_line_count)
1291 luaV_pushline(L, b, n);
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001292 else if (lua_isstring(L, 2))
1293 {
1294 const char *s = lua_tostring(L, 2);
1295 if (strncmp(s, "name", 4) == 0)
Bram Moolenaarfe08df42018-07-07 23:07:41 +02001296 lua_pushstring(L, (b->b_sfname == NULL)
1297 ? "" : (char *) b->b_sfname);
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001298 else if (strncmp(s, "fname", 5) == 0)
Bram Moolenaarfe08df42018-07-07 23:07:41 +02001299 lua_pushstring(L, (b->b_ffname == NULL)
1300 ? "" : (char *) b->b_ffname);
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001301 else if (strncmp(s, "number", 6) == 0)
Bram Moolenaar1dced572012-04-05 16:54:08 +02001302 lua_pushinteger(L, b->b_fnum);
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001303 // methods
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001304 else if (strncmp(s, "insert", 6) == 0
1305 || strncmp(s, "next", 4) == 0
1306 || strncmp(s, "previous", 8) == 0
1307 || strncmp(s, "isvalid", 7) == 0)
1308 {
1309 lua_getmetatable(L, 1);
1310 lua_getfield(L, -1, s);
1311 }
1312 else
1313 lua_pushnil(L);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001314 }
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001315 else
1316 lua_pushnil(L);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001317 return 1;
1318}
1319
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001320 static int
1321luaV_buffer_newindex(lua_State *L)
1322{
Bram Moolenaar1dced572012-04-05 16:54:08 +02001323 buf_T *b = (buf_T *) luaV_checkvalid(L, luaV_Buffer, 1);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001324 linenr_T n = (linenr_T) luaL_checkinteger(L, 2);
1325#ifdef HAVE_SANDBOX
1326 luaV_checksandbox(L);
1327#endif
Bram Moolenaar1dced572012-04-05 16:54:08 +02001328 if (n < 1 || n > b->b_ml.ml_line_count)
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001329 luaL_error(L, "invalid line number");
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001330 if (lua_isnil(L, 3)) // delete line
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001331 {
1332 buf_T *buf = curbuf;
Bram Moolenaar1dced572012-04-05 16:54:08 +02001333 curbuf = b;
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001334 if (u_savedel(n, 1L) == FAIL)
1335 {
1336 curbuf = buf;
1337 luaL_error(L, "cannot save undo information");
1338 }
1339 else if (ml_delete(n, FALSE) == FAIL)
1340 {
1341 curbuf = buf;
1342 luaL_error(L, "cannot delete line");
1343 }
Bram Moolenaarca06da92018-07-01 15:12:05 +02001344 else
1345 {
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001346 deleted_lines_mark(n, 1L);
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001347 if (b == curwin->w_buffer) // fix cursor in current window?
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001348 {
1349 if (curwin->w_cursor.lnum >= n)
1350 {
1351 if (curwin->w_cursor.lnum > n)
1352 {
1353 curwin->w_cursor.lnum -= 1;
1354 check_cursor_col();
1355 }
1356 else check_cursor();
1357 changed_cline_bef_curs();
1358 }
1359 invalidate_botline();
1360 }
1361 }
1362 curbuf = buf;
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001363 }
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001364 else if (lua_isstring(L, 3)) // update line
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001365 {
1366 buf_T *buf = curbuf;
Bram Moolenaar1dced572012-04-05 16:54:08 +02001367 curbuf = b;
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001368 if (u_savesub(n) == FAIL)
1369 {
1370 curbuf = buf;
1371 luaL_error(L, "cannot save undo information");
1372 }
1373 else if (ml_replace(n, luaV_toline(L, 3), TRUE) == FAIL)
1374 {
1375 curbuf = buf;
1376 luaL_error(L, "cannot replace line");
1377 }
1378 else changed_bytes(n, 0);
1379 curbuf = buf;
Bram Moolenaar1dced572012-04-05 16:54:08 +02001380 if (b == curwin->w_buffer)
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001381 check_cursor_col();
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001382 }
1383 else
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001384 luaL_error(L, "wrong argument to change line");
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001385 return 0;
1386}
1387
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001388 static int
1389luaV_buffer_insert(lua_State *L)
1390{
Bram Moolenaar1dced572012-04-05 16:54:08 +02001391 luaV_Buffer *lb = luaV_checkudata(L, 1, LUAVIM_BUFFER);
1392 buf_T *b = (buf_T *) luaV_checkcache(L, (void *) *lb);
1393 linenr_T last = b->b_ml.ml_line_count;
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001394 linenr_T n = (linenr_T) luaL_optinteger(L, 3, last);
1395 buf_T *buf;
1396 luaL_checktype(L, 2, LUA_TSTRING);
1397#ifdef HAVE_SANDBOX
1398 luaV_checksandbox(L);
1399#endif
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001400 // fix insertion line
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001401 if (n < 0) n = 0;
1402 if (n > last) n = last;
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001403 // insert
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001404 buf = curbuf;
Bram Moolenaar1dced572012-04-05 16:54:08 +02001405 curbuf = b;
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001406 if (u_save(n, n + 1) == FAIL)
1407 {
1408 curbuf = buf;
1409 luaL_error(L, "cannot save undo information");
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001410 }
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001411 else if (ml_append(n, luaV_toline(L, 2), 0, FALSE) == FAIL)
1412 {
1413 curbuf = buf;
1414 luaL_error(L, "cannot insert line");
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001415 }
1416 else
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001417 appended_lines_mark(n, 1L);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001418 curbuf = buf;
1419 update_screen(VALID);
1420 return 0;
1421}
1422
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001423 static int
1424luaV_buffer_next(lua_State *L)
1425{
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001426 luaV_Buffer *b = luaV_checkudata(L, 1, LUAVIM_BUFFER);
Bram Moolenaar1dced572012-04-05 16:54:08 +02001427 buf_T *buf = (buf_T *) luaV_checkcache(L, (void *) *b);
1428 luaV_pushbuffer(L, buf->b_next);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001429 return 1;
1430}
1431
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001432 static int
1433luaV_buffer_previous(lua_State *L)
1434{
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001435 luaV_Buffer *b = luaV_checkudata(L, 1, LUAVIM_BUFFER);
Bram Moolenaar1dced572012-04-05 16:54:08 +02001436 buf_T *buf = (buf_T *) luaV_checkcache(L, (void *) *b);
1437 luaV_pushbuffer(L, buf->b_prev);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001438 return 1;
1439}
1440
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001441 static int
1442luaV_buffer_isvalid(lua_State *L)
1443{
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001444 luaV_Buffer *b = luaV_checkudata(L, 1, LUAVIM_BUFFER);
Bram Moolenaar1dced572012-04-05 16:54:08 +02001445 luaV_getudata(L, *b);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001446 lua_pushboolean(L, !lua_isnil(L, -1));
1447 return 1;
1448}
1449
1450static const luaL_Reg luaV_Buffer_mt[] = {
1451 {"__tostring", luaV_buffer_tostring},
1452 {"__len", luaV_buffer_len},
1453 {"__call", luaV_buffer_call},
1454 {"__index", luaV_buffer_index},
1455 {"__newindex", luaV_buffer_newindex},
1456 {"insert", luaV_buffer_insert},
1457 {"next", luaV_buffer_next},
1458 {"previous", luaV_buffer_previous},
1459 {"isvalid", luaV_buffer_isvalid},
1460 {NULL, NULL}
1461};
1462
1463
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001464// ======= Window type =======
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001465
Bram Moolenaar1dced572012-04-05 16:54:08 +02001466luaV_newtype(win_T, window, luaV_Window, LUAVIM_WINDOW)
1467luaV_pushtype(win_T, window, luaV_Window)
1468luaV_type_tostring(window, LUAVIM_WINDOW)
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001469
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001470 static int
1471luaV_window_call(lua_State *L)
1472{
Bram Moolenaar1dced572012-04-05 16:54:08 +02001473 win_T *w = (win_T *) luaV_checkvalid(L, luaV_Window, 1);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001474 lua_settop(L, 1);
Bram Moolenaar1dced572012-04-05 16:54:08 +02001475 win_goto(w);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001476 return 1;
1477}
1478
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001479 static int
1480luaV_window_index(lua_State *L)
1481{
Bram Moolenaar1dced572012-04-05 16:54:08 +02001482 win_T *w = (win_T *) luaV_checkvalid(L, luaV_Window, 1);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001483 const char *s = luaL_checkstring(L, 2);
1484 if (strncmp(s, "buffer", 6) == 0)
Bram Moolenaar1dced572012-04-05 16:54:08 +02001485 luaV_pushbuffer(L, w->w_buffer);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001486 else if (strncmp(s, "line", 4) == 0)
Bram Moolenaar1dced572012-04-05 16:54:08 +02001487 lua_pushinteger(L, w->w_cursor.lnum);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001488 else if (strncmp(s, "col", 3) == 0)
Bram Moolenaar1dced572012-04-05 16:54:08 +02001489 lua_pushinteger(L, w->w_cursor.col + 1);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001490 else if (strncmp(s, "width", 5) == 0)
Bram Moolenaar02631462017-09-22 15:20:32 +02001491 lua_pushinteger(L, w->w_width);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001492 else if (strncmp(s, "height", 6) == 0)
Bram Moolenaar1dced572012-04-05 16:54:08 +02001493 lua_pushinteger(L, w->w_height);
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001494 // methods
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001495 else if (strncmp(s, "next", 4) == 0
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001496 || strncmp(s, "previous", 8) == 0
1497 || strncmp(s, "isvalid", 7) == 0)
1498 {
1499 lua_getmetatable(L, 1);
1500 lua_getfield(L, -1, s);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001501 }
1502 else
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001503 lua_pushnil(L);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001504 return 1;
1505}
1506
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001507 static int
Bram Moolenaar4eefe472019-03-19 21:59:19 +01001508luaV_window_newindex(lua_State *L)
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001509{
Bram Moolenaar1dced572012-04-05 16:54:08 +02001510 win_T *w = (win_T *) luaV_checkvalid(L, luaV_Window, 1);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001511 const char *s = luaL_checkstring(L, 2);
1512 int v = luaL_checkinteger(L, 3);
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001513 if (strncmp(s, "line", 4) == 0)
1514 {
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001515#ifdef HAVE_SANDBOX
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001516 luaV_checksandbox(L);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001517#endif
Bram Moolenaar1dced572012-04-05 16:54:08 +02001518 if (v < 1 || v > w->w_buffer->b_ml.ml_line_count)
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001519 luaL_error(L, "line out of range");
Bram Moolenaar1dced572012-04-05 16:54:08 +02001520 w->w_cursor.lnum = v;
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001521 update_screen(VALID);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001522 }
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001523 else if (strncmp(s, "col", 3) == 0)
1524 {
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001525#ifdef HAVE_SANDBOX
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001526 luaV_checksandbox(L);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001527#endif
Bram Moolenaar1dced572012-04-05 16:54:08 +02001528 w->w_cursor.col = v - 1;
Bram Moolenaar53901442018-07-25 22:02:36 +02001529 w->w_set_curswant = TRUE;
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001530 update_screen(VALID);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001531 }
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001532 else if (strncmp(s, "width", 5) == 0)
1533 {
1534 win_T *win = curwin;
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001535#ifdef FEAT_GUI
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001536 need_mouse_correct = TRUE;
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001537#endif
Bram Moolenaar1dced572012-04-05 16:54:08 +02001538 curwin = w;
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001539 win_setwidth(v);
1540 curwin = win;
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001541 }
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001542 else if (strncmp(s, "height", 6) == 0)
1543 {
1544 win_T *win = curwin;
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001545#ifdef FEAT_GUI
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001546 need_mouse_correct = TRUE;
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001547#endif
Bram Moolenaar1dced572012-04-05 16:54:08 +02001548 curwin = w;
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001549 win_setheight(v);
1550 curwin = win;
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001551 }
1552 else
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001553 luaL_error(L, "invalid window property: `%s'", s);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001554 return 0;
1555}
1556
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001557 static int
1558luaV_window_next(lua_State *L)
1559{
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001560 luaV_Window *w = luaV_checkudata(L, 1, LUAVIM_WINDOW);
Bram Moolenaar1dced572012-04-05 16:54:08 +02001561 win_T *win = (win_T *) luaV_checkcache(L, (void *) *w);
1562 luaV_pushwindow(L, win->w_next);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001563 return 1;
1564}
1565
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001566 static int
1567luaV_window_previous(lua_State *L)
1568{
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001569 luaV_Window *w = luaV_checkudata(L, 1, LUAVIM_WINDOW);
Bram Moolenaar1dced572012-04-05 16:54:08 +02001570 win_T *win = (win_T *) luaV_checkcache(L, (void *) *w);
1571 luaV_pushwindow(L, win->w_prev);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001572 return 1;
1573}
1574
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001575 static int
1576luaV_window_isvalid(lua_State *L)
1577{
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001578 luaV_Window *w = luaV_checkudata(L, 1, LUAVIM_WINDOW);
Bram Moolenaar1dced572012-04-05 16:54:08 +02001579 luaV_getudata(L, *w);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001580 lua_pushboolean(L, !lua_isnil(L, -1));
1581 return 1;
1582}
1583
1584static const luaL_Reg luaV_Window_mt[] = {
1585 {"__tostring", luaV_window_tostring},
1586 {"__call", luaV_window_call},
1587 {"__index", luaV_window_index},
1588 {"__newindex", luaV_window_newindex},
1589 {"next", luaV_window_next},
1590 {"previous", luaV_window_previous},
1591 {"isvalid", luaV_window_isvalid},
1592 {NULL, NULL}
1593};
1594
1595
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001596// ======= Vim module =======
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001597
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001598 static int
1599luaV_print(lua_State *L)
1600{
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001601 int i, n = lua_gettop(L); // nargs
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001602 const char *s;
1603 size_t l;
1604 luaL_Buffer b;
1605 luaL_buffinit(L, &b);
1606 lua_getglobal(L, "tostring");
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001607 for (i = 1; i <= n; i++)
1608 {
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001609 lua_pushvalue(L, -1); // tostring
1610 lua_pushvalue(L, i); // arg
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001611 lua_call(L, 1, 1);
1612 s = lua_tolstring(L, -1, &l);
1613 if (s == NULL)
1614 return luaL_error(L, "cannot convert to string");
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001615 if (i > 1) luaL_addchar(&b, ' '); // use space instead of tab
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001616 luaV_addlstring(&b, s, l, 0);
1617 lua_pop(L, 1);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001618 }
1619 luaL_pushresult(&b);
Bram Moolenaarb98678a2019-10-19 15:18:44 +02001620 if (!got_int)
1621 luaV_msg(L);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001622 return 0;
1623}
1624
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001625 static int
Bram Moolenaar38e2b062011-09-21 17:15:39 +02001626luaV_debug(lua_State *L)
1627{
1628 lua_settop(L, 0);
1629 lua_getglobal(L, "vim");
1630 lua_getfield(L, -1, "eval");
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001631 lua_remove(L, -2); // vim.eval at position 1
Bram Moolenaar38e2b062011-09-21 17:15:39 +02001632 for (;;)
1633 {
1634 const char *input;
1635 size_t l;
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001636 lua_pushvalue(L, 1); // vim.eval
Bram Moolenaar38e2b062011-09-21 17:15:39 +02001637 lua_pushliteral(L, "input('lua_debug> ')");
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001638 lua_call(L, 1, 1); // return string
Bram Moolenaar38e2b062011-09-21 17:15:39 +02001639 input = lua_tolstring(L, -1, &l);
1640 if (l == 0 || strcmp(input, "cont") == 0)
1641 return 0;
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001642 msg_putchar('\n'); // avoid outputting on input line
Bram Moolenaar38e2b062011-09-21 17:15:39 +02001643 if (luaL_loadbuffer(L, input, l, "=(debug command)")
1644 || lua_pcall(L, 0, 0, 0))
1645 luaV_emsg(L);
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001646 lua_settop(L, 1); // remove eventual returns, but keep vim.eval
Bram Moolenaar38e2b062011-09-21 17:15:39 +02001647 }
1648}
1649
1650 static int
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001651luaV_command(lua_State *L)
1652{
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001653 do_cmdline_cmd((char_u *) luaL_checkstring(L, 1));
1654 update_screen(VALID);
1655 return 0;
1656}
1657
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001658 static int
1659luaV_eval(lua_State *L)
1660{
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001661 typval_T *tv = eval_expr((char_u *) luaL_checkstring(L, 1), NULL);
1662 if (tv == NULL) luaL_error(L, "invalid expression");
1663 luaV_pushtypval(L, tv);
Bram Moolenaarb3766472013-04-15 13:49:21 +02001664 free_tv(tv);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001665 return 1;
1666}
1667
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001668 static int
Bram Moolenaar0d2e4fc2010-07-18 12:35:47 +02001669luaV_beep(lua_State *L UNUSED)
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001670{
Bram Moolenaar165bc692015-07-21 17:53:25 +02001671 vim_beep(BO_LANG);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001672 return 0;
1673}
1674
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001675 static int
1676luaV_line(lua_State *L)
1677{
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001678 luaV_pushline(L, curbuf, curwin->w_cursor.lnum);
1679 return 1;
1680}
1681
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001682 static int
Bram Moolenaar1dced572012-04-05 16:54:08 +02001683luaV_list(lua_State *L)
1684{
Bram Moolenaarca06da92018-07-01 15:12:05 +02001685 list_T *l;
1686 int initarg = !lua_isnoneornil(L, 1);
1687
1688 if (initarg && lua_type(L, 1) != LUA_TTABLE)
1689 luaL_error(L, "table expected, got %s", luaL_typename(L, 1));
1690 l = list_alloc();
Bram Moolenaar1dced572012-04-05 16:54:08 +02001691 if (l == NULL)
1692 lua_pushnil(L);
1693 else
Bram Moolenaarca06da92018-07-01 15:12:05 +02001694 {
Bram Moolenaar1dced572012-04-05 16:54:08 +02001695 luaV_newlist(L, l);
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001696 if (initarg) // traverse table to init list
Bram Moolenaar17413672018-07-14 20:49:42 +02001697 {
Bram Moolenaarca06da92018-07-01 15:12:05 +02001698 int notnil, i = 0;
1699 typval_T v;
Bram Moolenaar17413672018-07-14 20:49:42 +02001700 do
1701 {
Bram Moolenaarca06da92018-07-01 15:12:05 +02001702 lua_rawgeti(L, 1, ++i);
1703 notnil = !lua_isnil(L, -1);
Bram Moolenaar17413672018-07-14 20:49:42 +02001704 if (notnil)
1705 {
Bram Moolenaarca06da92018-07-01 15:12:05 +02001706 luaV_checktypval(L, -1, &v, "vim.list");
1707 list_append_tv(l, &v);
Bram Moolenaar713bf9e92019-03-16 16:38:41 +01001708 clear_tv(&v);
Bram Moolenaarca06da92018-07-01 15:12:05 +02001709 }
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001710 lua_pop(L, 1); // value
Bram Moolenaarca06da92018-07-01 15:12:05 +02001711 } while (notnil);
1712 }
1713 }
Bram Moolenaar1dced572012-04-05 16:54:08 +02001714 return 1;
1715}
1716
1717 static int
1718luaV_dict(lua_State *L)
1719{
Bram Moolenaarca06da92018-07-01 15:12:05 +02001720 dict_T *d;
1721 int initarg = !lua_isnoneornil(L, 1);
1722
1723 if (initarg && lua_type(L, 1) != LUA_TTABLE)
1724 luaL_error(L, "table expected, got %s", luaL_typename(L, 1));
1725 d = dict_alloc();
Bram Moolenaar1dced572012-04-05 16:54:08 +02001726 if (d == NULL)
1727 lua_pushnil(L);
1728 else
Bram Moolenaarca06da92018-07-01 15:12:05 +02001729 {
Bram Moolenaar1dced572012-04-05 16:54:08 +02001730 luaV_newdict(L, d);
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001731 if (initarg) // traverse table to init dict
Bram Moolenaarca06da92018-07-01 15:12:05 +02001732 {
1733 lua_pushnil(L);
1734 while (lua_next(L, 1))
1735 {
1736 char_u *key;
1737 dictitem_T *di;
1738 typval_T v;
Bram Moolenaard6ef5f92018-07-13 22:08:23 +02001739
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001740 lua_pushvalue(L, -2); // dup key in case it's a number
Bram Moolenaarca06da92018-07-01 15:12:05 +02001741 key = (char_u *) lua_tostring(L, -1);
Bram Moolenaard6ef5f92018-07-13 22:08:23 +02001742 if (key == NULL)
1743 {
1744 lua_pushnil(L);
1745 return 1;
1746 }
1747 if (*key == NUL)
Bram Moolenaarca06da92018-07-01 15:12:05 +02001748 luaL_error(L, "table has empty key");
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001749 luaV_checktypval(L, -2, &v, "vim.dict"); // value
Bram Moolenaarca06da92018-07-01 15:12:05 +02001750 di = dictitem_alloc(key);
Bram Moolenaard6ef5f92018-07-13 22:08:23 +02001751 if (di == NULL || dict_add(d, di) == FAIL)
1752 {
Bram Moolenaarca06da92018-07-01 15:12:05 +02001753 vim_free(di);
1754 lua_pushnil(L);
1755 return 1;
1756 }
1757 copy_tv(&v, &di->di_tv);
1758 clear_tv(&v);
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001759 lua_pop(L, 2); // key copy and value
Bram Moolenaarca06da92018-07-01 15:12:05 +02001760 }
1761 }
1762 }
1763 return 1;
1764}
1765
1766 static int
Bram Moolenaarb7828692019-03-23 13:57:02 +01001767luaV_blob(lua_State *L)
1768{
1769 blob_T *b;
1770 int initarg = !lua_isnoneornil(L, 1);
1771
1772 if (initarg && !lua_isstring(L, 1))
1773 luaL_error(L, "string expected, got %s", luaL_typename(L, 1));
1774 b = blob_alloc();
1775 if (b == NULL)
1776 lua_pushnil(L);
1777 else
1778 {
1779 luaV_newblob(L, b);
1780 if (initarg)
1781 {
1782 size_t i, l = 0;
1783 const char *s = lua_tolstring(L, 1, &l);
1784
Bram Moolenaar6fb5c972019-03-26 21:44:20 +01001785 if (ga_grow(&b->bv_ga, l) == OK)
1786 for (i = 0; i < l; ++i)
1787 ga_append(&b->bv_ga, s[i]);
Bram Moolenaarb7828692019-03-23 13:57:02 +01001788 }
1789 }
1790 return 1;
1791}
1792
1793 static int
Bram Moolenaarca06da92018-07-01 15:12:05 +02001794luaV_funcref(lua_State *L)
1795{
1796 const char *name = luaL_checkstring(L, 1);
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001797 // note: not checking if function exists (needs function_exists)
Bram Moolenaarca06da92018-07-01 15:12:05 +02001798 if (name == NULL || *name == NUL || VIM_ISDIGIT(*name))
1799 luaL_error(L, "invalid function name: %s", name);
1800 luaV_newfuncref(L, (char_u *) name);
Bram Moolenaar1dced572012-04-05 16:54:08 +02001801 return 1;
1802}
1803
1804 static int
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001805luaV_buffer(lua_State *L)
1806{
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001807 buf_T *buf;
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001808 if (lua_isstring(L, 1)) // get by number or name?
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001809 {
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001810 if (lua_isnumber(L, 1)) // by number?
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001811 {
1812 int n = lua_tointeger(L, 1);
Bram Moolenaar29323592016-07-24 22:04:11 +02001813 FOR_ALL_BUFFERS(buf)
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001814 if (buf->b_fnum == n) break;
1815 }
Bram Moolenaarca06da92018-07-01 15:12:05 +02001816 else // by name
1817 {
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001818 size_t l;
1819 const char *s = lua_tolstring(L, 1, &l);
Bram Moolenaar29323592016-07-24 22:04:11 +02001820 FOR_ALL_BUFFERS(buf)
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001821 {
1822 if (buf->b_ffname == NULL || buf->b_sfname == NULL)
1823 {
1824 if (l == 0) break;
1825 }
Bram Moolenaar0d2e4fc2010-07-18 12:35:47 +02001826 else if (strncmp(s, (char *)buf->b_ffname, l) == 0
1827 || strncmp(s, (char *)buf->b_sfname, l) == 0)
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001828 break;
1829 }
1830 }
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001831 }
Bram Moolenaar1dced572012-04-05 16:54:08 +02001832 else
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001833 buf = (lua_toboolean(L, 1)) ? firstbuf : curbuf; // first buffer?
Bram Moolenaar1dced572012-04-05 16:54:08 +02001834 luaV_pushbuffer(L, buf);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001835 return 1;
1836}
1837
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001838 static int
1839luaV_window(lua_State *L)
1840{
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001841 win_T *win;
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001842 if (lua_isnumber(L, 1)) // get by number?
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001843 {
1844 int n = lua_tointeger(L, 1);
1845 for (win = firstwin; win != NULL; win = win->w_next, n--)
1846 if (n == 1) break;
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001847 }
Bram Moolenaar1dced572012-04-05 16:54:08 +02001848 else
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001849 win = (lua_toboolean(L, 1)) ? firstwin : curwin; // first window?
Bram Moolenaar1dced572012-04-05 16:54:08 +02001850 luaV_pushwindow(L, win);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001851 return 1;
1852}
1853
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001854 static int
1855luaV_open(lua_State *L)
1856{
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001857 char_u *s = NULL;
1858#ifdef HAVE_SANDBOX
1859 luaV_checksandbox(L);
1860#endif
1861 if (lua_isstring(L, 1)) s = (char_u *) lua_tostring(L, 1);
Bram Moolenaarfa263a52011-12-08 16:00:16 +01001862 luaV_pushbuffer(L, buflist_new(s, NULL, 1L, BLN_LISTED));
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001863 return 1;
1864}
1865
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001866 static int
Bram Moolenaar1dced572012-04-05 16:54:08 +02001867luaV_type(lua_State *L)
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001868{
Bram Moolenaar1dced572012-04-05 16:54:08 +02001869 luaL_checkany(L, 1);
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001870 if (lua_type(L, 1) == LUA_TUSERDATA) // check vim udata?
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001871 {
Bram Moolenaar1dced572012-04-05 16:54:08 +02001872 lua_settop(L, 1);
1873 if (lua_getmetatable(L, 1))
1874 {
1875 luaV_getfield(L, LUAVIM_LIST);
1876 if (lua_rawequal(L, -1, 2))
1877 {
1878 lua_pushstring(L, "list");
1879 return 1;
1880 }
1881 luaV_getfield(L, LUAVIM_DICT);
1882 if (lua_rawequal(L, -1, 2))
1883 {
1884 lua_pushstring(L, "dict");
1885 return 1;
1886 }
Bram Moolenaarb7828692019-03-23 13:57:02 +01001887 luaV_getfield(L, LUAVIM_BLOB);
1888 if (lua_rawequal(L, -1, 2))
1889 {
1890 lua_pushstring(L, "blob");
1891 return 1;
1892 }
Bram Moolenaarca06da92018-07-01 15:12:05 +02001893 luaV_getfield(L, LUAVIM_FUNCREF);
1894 if (lua_rawequal(L, -1, 2))
1895 {
1896 lua_pushstring(L, "funcref");
1897 return 1;
1898 }
Bram Moolenaar1dced572012-04-05 16:54:08 +02001899 luaV_getfield(L, LUAVIM_BUFFER);
1900 if (lua_rawequal(L, -1, 2))
1901 {
1902 lua_pushstring(L, "buffer");
1903 return 1;
1904 }
1905 luaV_getfield(L, LUAVIM_WINDOW);
1906 if (lua_rawequal(L, -1, 2))
1907 {
1908 lua_pushstring(L, "window");
1909 return 1;
1910 }
1911 }
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001912 }
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001913 lua_pushstring(L, luaL_typename(L, 1)); // fallback
Bram Moolenaar1dced572012-04-05 16:54:08 +02001914 return 1;
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001915}
1916
1917static const luaL_Reg luaV_module[] = {
1918 {"command", luaV_command},
1919 {"eval", luaV_eval},
1920 {"beep", luaV_beep},
1921 {"line", luaV_line},
Bram Moolenaar1dced572012-04-05 16:54:08 +02001922 {"list", luaV_list},
1923 {"dict", luaV_dict},
Bram Moolenaarb7828692019-03-23 13:57:02 +01001924 {"blob", luaV_blob},
Bram Moolenaarca06da92018-07-01 15:12:05 +02001925 {"funcref", luaV_funcref},
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001926 {"buffer", luaV_buffer},
1927 {"window", luaV_window},
1928 {"open", luaV_open},
Bram Moolenaar1dced572012-04-05 16:54:08 +02001929 {"type", luaV_type},
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001930 {NULL, NULL}
1931};
1932
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001933/*
1934 * for freeing list, dict, buffer and window objects; lightuserdata as arg
1935 */
Bram Moolenaar1dced572012-04-05 16:54:08 +02001936 static int
1937luaV_free(lua_State *L)
1938{
1939 lua_pushnil(L);
1940 luaV_setudata(L, lua_touserdata(L, 1));
1941 return 0;
1942}
1943
1944 static int
Bram Moolenaar4eefe472019-03-19 21:59:19 +01001945luaV_luaeval(lua_State *L)
Bram Moolenaar1dced572012-04-05 16:54:08 +02001946{
1947 luaL_Buffer b;
1948 size_t l;
1949 const char *str = lua_tolstring(L, 1, &l);
1950 typval_T *arg = (typval_T *) lua_touserdata(L, 2);
1951 typval_T *rettv = (typval_T *) lua_touserdata(L, 3);
1952 luaL_buffinit(L, &b);
1953 luaL_addlstring(&b, LUAVIM_EVALHEADER, sizeof(LUAVIM_EVALHEADER) - 1);
1954 luaL_addlstring(&b, str, l);
1955 luaL_pushresult(&b);
1956 str = lua_tolstring(L, -1, &l);
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001957 if (luaL_loadbuffer(L, str, l, LUAVIM_EVALNAME)) // compile error?
Bram Moolenaar1dced572012-04-05 16:54:08 +02001958 {
1959 luaV_emsg(L);
1960 return 0;
1961 }
1962 luaV_pushtypval(L, arg);
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001963 if (lua_pcall(L, 1, 1, 0)) // running error?
Bram Moolenaar1dced572012-04-05 16:54:08 +02001964 {
1965 luaV_emsg(L);
1966 return 0;
1967 }
Bram Moolenaarca06da92018-07-01 15:12:05 +02001968 if (luaV_totypval(L, -1, rettv) == FAIL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001969 emsg("luaeval: cannot convert value");
Bram Moolenaarf554a322015-02-04 23:08:01 +01001970 return 0;
Bram Moolenaar1dced572012-04-05 16:54:08 +02001971}
1972
1973 static int
Bram Moolenaar4eefe472019-03-19 21:59:19 +01001974luaV_setref(lua_State *L)
Bram Moolenaar1dced572012-04-05 16:54:08 +02001975{
Bram Moolenaar4eefe472019-03-19 21:59:19 +01001976 int copyID = lua_tointeger(L, 1);
1977 int abort = FALSE;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01001978
Bram Moolenaar1dced572012-04-05 16:54:08 +02001979 luaV_getfield(L, LUAVIM_LIST);
1980 luaV_getfield(L, LUAVIM_DICT);
Bram Moolenaar4eefe472019-03-19 21:59:19 +01001981 luaV_getfield(L, LUAVIM_FUNCREF);
Bram Moolenaar1dced572012-04-05 16:54:08 +02001982 lua_pushnil(L);
Bram Moolenaar4eefe472019-03-19 21:59:19 +01001983 // traverse cache table
Bram Moolenaarb84634d2015-02-04 22:02:37 +01001984 while (!abort && lua_next(L, lua_upvalueindex(1)) != 0)
Bram Moolenaar1dced572012-04-05 16:54:08 +02001985 {
1986 lua_getmetatable(L, -1);
Bram Moolenaar4eefe472019-03-19 21:59:19 +01001987 if (lua_rawequal(L, -1, 2)) // list?
Bram Moolenaar1dced572012-04-05 16:54:08 +02001988 {
Bram Moolenaar4eefe472019-03-19 21:59:19 +01001989 list_T *l = (list_T *)lua_touserdata(L, 5); // key
1990
Bram Moolenaar7be3ab22019-06-23 01:46:15 +02001991 abort = set_ref_in_list(l, copyID);
Bram Moolenaar1dced572012-04-05 16:54:08 +02001992 }
Bram Moolenaar4eefe472019-03-19 21:59:19 +01001993 else if (lua_rawequal(L, -1, 3)) // dict?
Bram Moolenaar1dced572012-04-05 16:54:08 +02001994 {
Bram Moolenaar4eefe472019-03-19 21:59:19 +01001995 dict_T *d = (dict_T *)lua_touserdata(L, 5); // key
1996
Bram Moolenaar7be3ab22019-06-23 01:46:15 +02001997 abort = set_ref_in_dict(d, copyID);
Bram Moolenaar1dced572012-04-05 16:54:08 +02001998 }
Bram Moolenaar4eefe472019-03-19 21:59:19 +01001999 else if (lua_rawequal(L, -1, 4)) // funcref?
2000 {
2001 luaV_Funcref *f = (luaV_Funcref *)lua_touserdata(L, 5); // key
2002
Bram Moolenaar7be3ab22019-06-23 01:46:15 +02002003 abort = set_ref_in_dict(f->self, copyID);
Bram Moolenaar4eefe472019-03-19 21:59:19 +01002004 }
2005 lua_pop(L, 2); // metatable and value
Bram Moolenaar1dced572012-04-05 16:54:08 +02002006 }
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01002007 lua_pushinteger(L, abort);
Bram Moolenaarf554a322015-02-04 23:08:01 +01002008 return 1;
Bram Moolenaar1dced572012-04-05 16:54:08 +02002009}
2010
Bram Moolenaar55d5c032010-07-17 23:52:29 +02002011 static int
2012luaopen_vim(lua_State *L)
2013{
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002014 // set cache table
Bram Moolenaar0ba04292010-07-14 23:23:17 +02002015 lua_newtable(L);
2016 lua_newtable(L);
Bram Moolenaar1dced572012-04-05 16:54:08 +02002017 lua_pushstring(L, "v");
Bram Moolenaar0ba04292010-07-14 23:23:17 +02002018 lua_setfield(L, -2, "__mode");
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002019 lua_setmetatable(L, -2); // cache is weak-valued
2020 // print
Bram Moolenaar0ba04292010-07-14 23:23:17 +02002021 lua_pushcfunction(L, luaV_print);
2022 lua_setglobal(L, "print");
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002023 // debug.debug
Bram Moolenaar38e2b062011-09-21 17:15:39 +02002024 lua_getglobal(L, "debug");
2025 lua_pushcfunction(L, luaV_debug);
2026 lua_setfield(L, -2, "debug");
2027 lua_pop(L, 1);
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002028 // free
Bram Moolenaar0ba04292010-07-14 23:23:17 +02002029 lua_pushlightuserdata(L, (void *) LUAVIM_FREE);
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002030 lua_pushvalue(L, 1); // cache table
Bram Moolenaar1dced572012-04-05 16:54:08 +02002031 lua_pushcclosure(L, luaV_free, 1);
2032 lua_rawset(L, LUA_REGISTRYINDEX);
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002033 // luaeval
Bram Moolenaar1dced572012-04-05 16:54:08 +02002034 lua_pushlightuserdata(L, (void *) LUAVIM_LUAEVAL);
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002035 lua_pushvalue(L, 1); // cache table
Bram Moolenaar1dced572012-04-05 16:54:08 +02002036 lua_pushcclosure(L, luaV_luaeval, 1);
2037 lua_rawset(L, LUA_REGISTRYINDEX);
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002038 // setref
Bram Moolenaar1dced572012-04-05 16:54:08 +02002039 lua_pushlightuserdata(L, (void *) LUAVIM_SETREF);
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002040 lua_pushvalue(L, 1); // cache table
Bram Moolenaar1dced572012-04-05 16:54:08 +02002041 lua_pushcclosure(L, luaV_setref, 1);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02002042 lua_rawset(L, LUA_REGISTRYINDEX);
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002043 // register
Bram Moolenaar1dced572012-04-05 16:54:08 +02002044 luaV_newmetatable(L, LUAVIM_LIST);
2045 lua_pushvalue(L, 1);
2046 luaV_openlib(L, luaV_List_mt, 1);
2047 luaV_newmetatable(L, LUAVIM_DICT);
2048 lua_pushvalue(L, 1);
2049 luaV_openlib(L, luaV_Dict_mt, 1);
Bram Moolenaarb7828692019-03-23 13:57:02 +01002050 luaV_newmetatable(L, LUAVIM_BLOB);
2051 lua_pushvalue(L, 1);
2052 luaV_openlib(L, luaV_Blob_mt, 1);
Bram Moolenaarca06da92018-07-01 15:12:05 +02002053 luaV_newmetatable(L, LUAVIM_FUNCREF);
2054 lua_pushvalue(L, 1);
2055 luaV_openlib(L, luaV_Funcref_mt, 1);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02002056 luaV_newmetatable(L, LUAVIM_BUFFER);
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002057 lua_pushvalue(L, 1); // cache table
Bram Moolenaar1dced572012-04-05 16:54:08 +02002058 luaV_openlib(L, luaV_Buffer_mt, 1);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02002059 luaV_newmetatable(L, LUAVIM_WINDOW);
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002060 lua_pushvalue(L, 1); // cache table
Bram Moolenaar1dced572012-04-05 16:54:08 +02002061 luaV_openlib(L, luaV_Window_mt, 1);
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002062 lua_newtable(L); // vim table
2063 lua_pushvalue(L, 1); // cache table
Bram Moolenaar1dced572012-04-05 16:54:08 +02002064 luaV_openlib(L, luaV_module, 1);
2065 lua_setglobal(L, LUAVIM_NAME);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02002066 return 0;
2067}
2068
Bram Moolenaar55d5c032010-07-17 23:52:29 +02002069 static lua_State *
2070luaV_newstate(void)
2071{
Bram Moolenaar0ba04292010-07-14 23:23:17 +02002072 lua_State *L = luaL_newstate();
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002073 luaL_openlibs(L); // core libs
2074 lua_pushcfunction(L, luaopen_vim); // vim
Bram Moolenaar0ba04292010-07-14 23:23:17 +02002075 lua_call(L, 0, 0);
2076 return L;
2077}
2078
Bram Moolenaar55d5c032010-07-17 23:52:29 +02002079 static void
2080luaV_setrange(lua_State *L, int line1, int line2)
2081{
Bram Moolenaar0ba04292010-07-14 23:23:17 +02002082 lua_getglobal(L, LUAVIM_NAME);
2083 lua_pushinteger(L, line1);
2084 lua_setfield(L, -2, "firstline");
2085 lua_pushinteger(L, line2);
2086 lua_setfield(L, -2, "lastline");
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002087 lua_pop(L, 1); // vim table
Bram Moolenaar0ba04292010-07-14 23:23:17 +02002088}
2089
2090
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002091// ======= Interface =======
Bram Moolenaar0ba04292010-07-14 23:23:17 +02002092
2093static lua_State *L = NULL;
2094
Bram Moolenaar55d5c032010-07-17 23:52:29 +02002095 static int
Bram Moolenaar1dced572012-04-05 16:54:08 +02002096lua_isopen(void)
Bram Moolenaar2bd6a1b2010-08-12 22:14:01 +02002097{
2098 return L != NULL;
2099}
2100
2101 static int
Bram Moolenaar55d5c032010-07-17 23:52:29 +02002102lua_init(void)
2103{
Bram Moolenaar1dced572012-04-05 16:54:08 +02002104 if (!lua_isopen())
Bram Moolenaar55d5c032010-07-17 23:52:29 +02002105 {
Bram Moolenaar0ba04292010-07-14 23:23:17 +02002106#ifdef DYNAMIC_LUA
Bram Moolenaar55d5c032010-07-17 23:52:29 +02002107 if (!lua_enabled(TRUE))
2108 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002109 emsg(_("Lua library cannot be loaded."));
Bram Moolenaar55d5c032010-07-17 23:52:29 +02002110 return FAIL;
2111 }
Bram Moolenaar0ba04292010-07-14 23:23:17 +02002112#endif
Bram Moolenaar55d5c032010-07-17 23:52:29 +02002113 L = luaV_newstate();
Bram Moolenaar0ba04292010-07-14 23:23:17 +02002114 }
2115 return OK;
2116}
2117
Bram Moolenaar55d5c032010-07-17 23:52:29 +02002118 void
2119lua_end(void)
2120{
Bram Moolenaar1dced572012-04-05 16:54:08 +02002121 if (lua_isopen())
Bram Moolenaar55d5c032010-07-17 23:52:29 +02002122 {
2123 lua_close(L);
2124 L = NULL;
Bram Moolenaar0ba04292010-07-14 23:23:17 +02002125#ifdef DYNAMIC_LUA
Bram Moolenaar55d5c032010-07-17 23:52:29 +02002126 end_dynamic_lua();
Bram Moolenaar0ba04292010-07-14 23:23:17 +02002127#endif
2128 }
2129}
2130
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002131/*
2132 * ex commands
2133 */
Bram Moolenaar55d5c032010-07-17 23:52:29 +02002134 void
2135ex_lua(exarg_T *eap)
2136{
Bram Moolenaar0ba04292010-07-14 23:23:17 +02002137 char *script;
2138 if (lua_init() == FAIL) return;
2139 script = (char *) script_get(eap, eap->arg);
Bram Moolenaar55d5c032010-07-17 23:52:29 +02002140 if (!eap->skip)
2141 {
2142 char *s = (script) ? script : (char *) eap->arg;
2143 luaV_setrange(L, eap->line1, eap->line2);
2144 if (luaL_loadbuffer(L, s, strlen(s), LUAVIM_CHUNKNAME)
2145 || lua_pcall(L, 0, 0, 0))
2146 luaV_emsg(L);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02002147 }
2148 if (script != NULL) vim_free(script);
2149}
2150
Bram Moolenaar55d5c032010-07-17 23:52:29 +02002151 void
2152ex_luado(exarg_T *eap)
2153{
Bram Moolenaar0ba04292010-07-14 23:23:17 +02002154 linenr_T l;
2155 const char *s = (const char *) eap->arg;
2156 luaL_Buffer b;
2157 size_t len;
Bram Moolenaard58f03b2017-01-29 22:48:45 +01002158 buf_T *was_curbuf = curbuf;
2159
Bram Moolenaar0ba04292010-07-14 23:23:17 +02002160 if (lua_init() == FAIL) return;
Bram Moolenaar55d5c032010-07-17 23:52:29 +02002161 if (u_save(eap->line1 - 1, eap->line2 + 1) == FAIL)
2162 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002163 emsg(_("cannot save undo information"));
Bram Moolenaar55d5c032010-07-17 23:52:29 +02002164 return;
Bram Moolenaar0ba04292010-07-14 23:23:17 +02002165 }
2166 luaV_setrange(L, eap->line1, eap->line2);
2167 luaL_buffinit(L, &b);
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002168 luaL_addlstring(&b, "return function(line, linenr) ", 30); // header
Bram Moolenaar0ba04292010-07-14 23:23:17 +02002169 luaL_addlstring(&b, s, strlen(s));
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002170 luaL_addlstring(&b, " end", 4); // footer
Bram Moolenaar0ba04292010-07-14 23:23:17 +02002171 luaL_pushresult(&b);
2172 s = lua_tolstring(L, -1, &len);
Bram Moolenaar55d5c032010-07-17 23:52:29 +02002173 if (luaL_loadbuffer(L, s, len, LUAVIM_CHUNKNAME))
2174 {
2175 luaV_emsg(L);
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002176 lua_pop(L, 1); // function body
Bram Moolenaar55d5c032010-07-17 23:52:29 +02002177 return;
Bram Moolenaar0ba04292010-07-14 23:23:17 +02002178 }
2179 lua_call(L, 0, 1);
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002180 lua_replace(L, -2); // function -> body
Bram Moolenaar55d5c032010-07-17 23:52:29 +02002181 for (l = eap->line1; l <= eap->line2; l++)
2182 {
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002183 // Check the line number, the command my have deleted lines.
Bram Moolenaard58f03b2017-01-29 22:48:45 +01002184 if (l > curbuf->b_ml.ml_line_count)
2185 break;
2186
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002187 lua_pushvalue(L, -1); // function
2188 luaV_pushline(L, curbuf, l); // current line as arg
2189 lua_pushinteger(L, l); // current line number as arg
Bram Moolenaarbd2f3c32012-04-06 14:31:00 +02002190 if (lua_pcall(L, 2, 1, 0))
Bram Moolenaar55d5c032010-07-17 23:52:29 +02002191 {
2192 luaV_emsg(L);
2193 break;
2194 }
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002195 // Catch the command switching to another buffer.
Bram Moolenaard58f03b2017-01-29 22:48:45 +01002196 if (curbuf != was_curbuf)
2197 break;
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002198 if (lua_isstring(L, -1)) // update line?
Bram Moolenaar55d5c032010-07-17 23:52:29 +02002199 {
Bram Moolenaar0ba04292010-07-14 23:23:17 +02002200#ifdef HAVE_SANDBOX
Bram Moolenaar55d5c032010-07-17 23:52:29 +02002201 luaV_checksandbox(L);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02002202#endif
Bram Moolenaar55d5c032010-07-17 23:52:29 +02002203 ml_replace(l, luaV_toline(L, -1), TRUE);
2204 changed_bytes(l, 0);
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002205 lua_pop(L, 1); // result from luaV_toline
Bram Moolenaar55d5c032010-07-17 23:52:29 +02002206 }
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002207 lua_pop(L, 1); // line
Bram Moolenaar0ba04292010-07-14 23:23:17 +02002208 }
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002209 lua_pop(L, 1); // function
Bram Moolenaar0ba04292010-07-14 23:23:17 +02002210 check_cursor();
2211 update_screen(NOT_VALID);
2212}
2213
Bram Moolenaar55d5c032010-07-17 23:52:29 +02002214 void
2215ex_luafile(exarg_T *eap)
2216{
2217 if (lua_init() == FAIL)
2218 return;
2219 if (!eap->skip)
2220 {
2221 luaV_setrange(L, eap->line1, eap->line2);
2222 if (luaL_loadfile(L, (char *) eap->arg) || lua_pcall(L, 0, 0, 0))
2223 luaV_emsg(L);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02002224 }
2225}
2226
Bram Moolenaar1dced572012-04-05 16:54:08 +02002227#define luaV_freetype(typ,tname) \
2228 void \
2229 lua_##tname##_free(typ *o) \
2230 { \
2231 if (!lua_isopen()) return; \
2232 luaV_getfield(L, LUAVIM_FREE); \
2233 lua_pushlightuserdata(L, (void *) o); \
2234 lua_call(L, 1, 0); \
2235 }
2236
2237luaV_freetype(buf_T, buffer)
2238luaV_freetype(win_T, window)
2239
Bram Moolenaar55d5c032010-07-17 23:52:29 +02002240 void
Bram Moolenaar4eefe472019-03-19 21:59:19 +01002241do_luaeval(char_u *str, typval_T *arg, typval_T *rettv)
Bram Moolenaar55d5c032010-07-17 23:52:29 +02002242{
Bram Moolenaar1dced572012-04-05 16:54:08 +02002243 lua_init();
2244 luaV_getfield(L, LUAVIM_LUAEVAL);
2245 lua_pushstring(L, (char *) str);
2246 lua_pushlightuserdata(L, (void *) arg);
2247 lua_pushlightuserdata(L, (void *) rettv);
2248 lua_call(L, 3, 0);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02002249}
2250
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01002251 int
Bram Moolenaar4eefe472019-03-19 21:59:19 +01002252set_ref_in_lua(int copyID)
Bram Moolenaar55d5c032010-07-17 23:52:29 +02002253{
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01002254 int aborted = 0;
2255
2256 if (lua_isopen())
2257 {
2258 luaV_getfield(L, LUAVIM_SETREF);
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002259 // call the function with 1 arg, getting 1 result back
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01002260 lua_pushinteger(L, copyID);
2261 lua_call(L, 1, 1);
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002262 // get the result
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01002263 aborted = lua_tointeger(L, -1);
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002264 // pop result off the stack
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01002265 lua_pop(L, 1);
2266 }
2267 return aborted;
Bram Moolenaar0ba04292010-07-14 23:23:17 +02002268}
2269
2270#endif