blob: 9b03c97aa859e3c6dc14be88ab06405af99744ea [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 Moolenaar520e1e42016-01-23 19:46:28 +0100538 case VAR_SPECIAL:
539 if (tv->vval.v_number <= VVAL_TRUE)
540 lua_pushinteger(L, (int) tv->vval.v_number);
541 else
542 lua_pushnil(L);
543 break;
Bram Moolenaarca06da92018-07-01 15:12:05 +0200544 case VAR_FUNC:
Bram Moolenaar4eefe472019-03-19 21:59:19 +0100545 luaV_pushfuncref(L, tv->vval.v_string);
Bram Moolenaarca06da92018-07-01 15:12:05 +0200546 break;
Bram Moolenaarb7828692019-03-23 13:57:02 +0100547 case VAR_BLOB:
548 luaV_pushblob(L, tv->vval.v_blob);
549 break;
Bram Moolenaar1dced572012-04-05 16:54:08 +0200550 default:
551 lua_pushnil(L);
552 }
553}
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200554
Bram Moolenaarca06da92018-07-01 15:12:05 +0200555/*
556 * Converts lua value at 'pos' to typval 'tv'.
557 * Returns OK or FAIL.
558 */
559 static int
560luaV_totypval(lua_State *L, int pos, typval_T *tv)
Bram Moolenaar1dced572012-04-05 16:54:08 +0200561{
Bram Moolenaarca06da92018-07-01 15:12:05 +0200562 int status = OK;
563
564 switch (lua_type(L, pos))
565 {
Bram Moolenaar1dced572012-04-05 16:54:08 +0200566 case LUA_TBOOLEAN:
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100567 tv->v_type = VAR_SPECIAL;
Bram Moolenaar1dced572012-04-05 16:54:08 +0200568 tv->vval.v_number = (varnumber_T) lua_toboolean(L, pos);
569 break;
Bram Moolenaar9067cd62019-01-01 00:41:54 +0100570 case LUA_TNIL:
571 tv->v_type = VAR_SPECIAL;
572 tv->vval.v_number = VVAL_NULL;
573 break;
Bram Moolenaar1dced572012-04-05 16:54:08 +0200574 case LUA_TSTRING:
575 tv->v_type = VAR_STRING;
576 tv->vval.v_string = vim_strsave((char_u *) lua_tostring(L, pos));
577 break;
578 case LUA_TNUMBER:
579#ifdef FEAT_FLOAT
580 tv->v_type = VAR_FLOAT;
581 tv->vval.v_float = (float_T) lua_tonumber(L, pos);
582#else
583 tv->v_type = VAR_NUMBER;
584 tv->vval.v_number = (varnumber_T) lua_tointeger(L, pos);
585#endif
586 break;
Bram Moolenaarca06da92018-07-01 15:12:05 +0200587 case LUA_TUSERDATA:
588 {
Bram Moolenaar1dced572012-04-05 16:54:08 +0200589 void *p = lua_touserdata(L, pos);
Bram Moolenaarca06da92018-07-01 15:12:05 +0200590
Bram Moolenaarb7828692019-03-23 13:57:02 +0100591 if (lua_getmetatable(L, pos)) // has metatable?
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200592 {
Bram Moolenaarb7828692019-03-23 13:57:02 +0100593 // check list
Bram Moolenaar1dced572012-04-05 16:54:08 +0200594 luaV_getfield(L, LUAVIM_LIST);
595 if (lua_rawequal(L, -1, -2))
Bram Moolenaar2334b6d2010-07-22 21:32:16 +0200596 {
Bram Moolenaar1dced572012-04-05 16:54:08 +0200597 tv->v_type = VAR_LIST;
598 tv->vval.v_list = *((luaV_List *) p);
599 ++tv->vval.v_list->lv_refcount;
Bram Moolenaarb7828692019-03-23 13:57:02 +0100600 lua_pop(L, 2); // MTs
Bram Moolenaarca06da92018-07-01 15:12:05 +0200601 break;
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200602 }
Bram Moolenaarb7828692019-03-23 13:57:02 +0100603 // check dict
Bram Moolenaar1dced572012-04-05 16:54:08 +0200604 luaV_getfield(L, LUAVIM_DICT);
605 if (lua_rawequal(L, -1, -3))
606 {
607 tv->v_type = VAR_DICT;
608 tv->vval.v_dict = *((luaV_Dict *) p);
609 ++tv->vval.v_dict->dv_refcount;
Bram Moolenaarb7828692019-03-23 13:57:02 +0100610 lua_pop(L, 3); // MTs
Bram Moolenaarca06da92018-07-01 15:12:05 +0200611 break;
Bram Moolenaar1dced572012-04-05 16:54:08 +0200612 }
Bram Moolenaarb7828692019-03-23 13:57:02 +0100613 // check blob
614 luaV_getfield(L, LUAVIM_BLOB);
Bram Moolenaarca06da92018-07-01 15:12:05 +0200615 if (lua_rawequal(L, -1, -4))
616 {
Bram Moolenaarb7828692019-03-23 13:57:02 +0100617 tv->v_type = VAR_BLOB;
618 tv->vval.v_blob = *((luaV_Blob *) p);
619 ++tv->vval.v_blob->bv_refcount;
620 lua_pop(L, 4); // MTs
621 break;
622 }
623 // check funcref
624 luaV_getfield(L, LUAVIM_FUNCREF);
625 if (lua_rawequal(L, -1, -5))
626 {
Bram Moolenaarca06da92018-07-01 15:12:05 +0200627 luaV_Funcref *f = (luaV_Funcref *) p;
Bram Moolenaar4eefe472019-03-19 21:59:19 +0100628 func_ref(f->name);
629 tv->v_type = VAR_FUNC;
630 tv->vval.v_string = vim_strsave(f->name);
Bram Moolenaarb7828692019-03-23 13:57:02 +0100631 lua_pop(L, 5); // MTs
Bram Moolenaarca06da92018-07-01 15:12:05 +0200632 break;
633 }
Bram Moolenaarb7828692019-03-23 13:57:02 +0100634 lua_pop(L, 4); // MTs
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200635 }
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200636 }
Bram Moolenaarb7828692019-03-23 13:57:02 +0100637 // FALLTHROUGH
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200638 default:
Bram Moolenaar1dced572012-04-05 16:54:08 +0200639 tv->v_type = VAR_NUMBER;
640 tv->vval.v_number = 0;
Bram Moolenaarca06da92018-07-01 15:12:05 +0200641 status = FAIL;
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200642 }
Bram Moolenaarca06da92018-07-01 15:12:05 +0200643 return status;
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200644}
645
Bram Moolenaar2ab2e862019-12-04 21:24:53 +0100646/*
647 * similar to luaL_addlstring, but replaces \0 with \n if toline and
648 * \n with \0 otherwise
649 */
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200650 static void
651luaV_addlstring(luaL_Buffer *b, const char *s, size_t l, int toline)
652{
653 while (l--)
654 {
655 if (*s == '\0' && toline)
656 luaL_addchar(b, '\n');
657 else if (*s == '\n' && !toline)
658 luaL_addchar(b, '\0');
659 else
660 luaL_addchar(b, *s);
661 s++;
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200662 }
663}
664
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200665 static void
666luaV_pushline(lua_State *L, buf_T *buf, linenr_T n)
667{
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200668 const char *s = (const char *) ml_get_buf(buf, n, FALSE);
669 luaL_Buffer b;
670 luaL_buffinit(L, &b);
671 luaV_addlstring(&b, s, strlen(s), 0);
672 luaL_pushresult(&b);
673}
674
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200675 static char_u *
676luaV_toline(lua_State *L, int pos)
677{
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200678 size_t l;
679 const char *s = lua_tolstring(L, pos, &l);
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200680
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200681 luaL_Buffer b;
682 luaL_buffinit(L, &b);
683 luaV_addlstring(&b, s, l, 1);
684 luaL_pushresult(&b);
685 return (char_u *) lua_tostring(L, -1);
686}
687
Bram Moolenaar2ab2e862019-12-04 21:24:53 +0100688/*
689 * pops a string s from the top of the stack and calls mf(t) for pieces t of
690 * s separated by newlines
691 */
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200692 static void
693luaV_msgfunc(lua_State *L, msgfunc_T mf)
694{
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200695 luaL_Buffer b;
696 size_t l;
697 const char *p, *s = lua_tolstring(L, -1, &l);
698 luaL_buffinit(L, &b);
699 luaV_addlstring(&b, s, l, 0);
700 luaL_pushresult(&b);
Bram Moolenaar2ab2e862019-12-04 21:24:53 +0100701 // break string
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200702 p = s = lua_tolstring(L, -1, &l);
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200703 while (l--)
704 {
Bram Moolenaar2ab2e862019-12-04 21:24:53 +0100705 if (*p++ == '\0') // break?
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200706 {
707 mf((char_u *) s);
708 s = p;
709 }
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200710 }
711 mf((char_u *) s);
Bram Moolenaar2ab2e862019-12-04 21:24:53 +0100712 lua_pop(L, 2); // original and modified strings
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200713}
714
Bram Moolenaar1dced572012-04-05 16:54:08 +0200715#define luaV_newtype(typ,tname,luatyp,luatname) \
716 static luatyp * \
Bram Moolenaar4eefe472019-03-19 21:59:19 +0100717 luaV_new##tname(lua_State *L, typ *obj) \
Bram Moolenaar1dced572012-04-05 16:54:08 +0200718 { \
719 luatyp *o = (luatyp *) lua_newuserdata(L, sizeof(luatyp)); \
720 *o = obj; \
721 luaV_setudata(L, obj); /* cache[obj] = udata */ \
722 luaV_getfield(L, luatname); \
723 lua_setmetatable(L, -2); \
724 return o; \
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200725 }
Bram Moolenaar1dced572012-04-05 16:54:08 +0200726
727#define luaV_pushtype(typ,tname,luatyp) \
728 static luatyp * \
Bram Moolenaarca06da92018-07-01 15:12:05 +0200729 luaV_push##tname(lua_State *L, typ *obj) \
Bram Moolenaar1dced572012-04-05 16:54:08 +0200730 { \
731 luatyp *o = NULL; \
732 if (obj == NULL) \
733 lua_pushnil(L); \
734 else { \
735 luaV_getudata(L, obj); \
736 if (lua_isnil(L, -1)) /* not interned? */ \
737 { \
738 lua_pop(L, 1); \
739 o = luaV_new##tname(L, obj); \
740 } \
741 else \
742 o = (luatyp *) lua_touserdata(L, -1); \
743 } \
744 return o; \
745 }
746
747#define luaV_type_tostring(tname,luatname) \
748 static int \
Bram Moolenaar4eefe472019-03-19 21:59:19 +0100749 luaV_##tname##_tostring(lua_State *L) \
Bram Moolenaar1dced572012-04-05 16:54:08 +0200750 { \
751 lua_pushfstring(L, "%s: %p", luatname, lua_touserdata(L, 1)); \
752 return 1; \
753 }
754
Bram Moolenaar2ab2e862019-12-04 21:24:53 +0100755// ======= List type =======
Bram Moolenaar1dced572012-04-05 16:54:08 +0200756
757 static luaV_List *
Bram Moolenaar4eefe472019-03-19 21:59:19 +0100758luaV_newlist(lua_State *L, list_T *lis)
Bram Moolenaar1dced572012-04-05 16:54:08 +0200759{
760 luaV_List *l = (luaV_List *) lua_newuserdata(L, sizeof(luaV_List));
761 *l = lis;
Bram Moolenaar2ab2e862019-12-04 21:24:53 +0100762 lis->lv_refcount++; // reference in Lua
763 luaV_setudata(L, lis); // cache[lis] = udata
Bram Moolenaar1dced572012-04-05 16:54:08 +0200764 luaV_getfield(L, LUAVIM_LIST);
765 lua_setmetatable(L, -2);
766 return l;
767}
768
769luaV_pushtype(list_T, list, luaV_List)
770luaV_type_tostring(list, LUAVIM_LIST)
771
772 static int
Bram Moolenaar4eefe472019-03-19 21:59:19 +0100773luaV_list_len(lua_State *L)
Bram Moolenaar1dced572012-04-05 16:54:08 +0200774{
775 list_T *l = luaV_unbox(L, luaV_List, 1);
Bram Moolenaarb7828692019-03-23 13:57:02 +0100776 lua_pushinteger(L, (int) list_len(l));
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200777 return 1;
778}
779
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200780 static int
Bram Moolenaar4eefe472019-03-19 21:59:19 +0100781luaV_list_iter(lua_State *L)
Bram Moolenaar1dced572012-04-05 16:54:08 +0200782{
783 listitem_T *li = (listitem_T *) lua_touserdata(L, lua_upvalueindex(2));
784 if (li == NULL) return 0;
785 luaV_pushtypval(L, &li->li_tv);
786 lua_pushlightuserdata(L, (void *) li->li_next);
787 lua_replace(L, lua_upvalueindex(2));
788 return 1;
789}
790
791 static int
Bram Moolenaar4eefe472019-03-19 21:59:19 +0100792luaV_list_call(lua_State *L)
Bram Moolenaar1dced572012-04-05 16:54:08 +0200793{
794 list_T *l = luaV_unbox(L, luaV_List, 1);
Bram Moolenaar2ab2e862019-12-04 21:24:53 +0100795 lua_pushvalue(L, lua_upvalueindex(1)); // pass cache table along
Bram Moolenaar1dced572012-04-05 16:54:08 +0200796 lua_pushlightuserdata(L, (void *) l->lv_first);
797 lua_pushcclosure(L, luaV_list_iter, 2);
798 return 1;
799}
800
801 static int
Bram Moolenaar4eefe472019-03-19 21:59:19 +0100802luaV_list_index(lua_State *L)
Bram Moolenaar1dced572012-04-05 16:54:08 +0200803{
804 list_T *l = luaV_unbox(L, luaV_List, 1);
Bram Moolenaar2ab2e862019-12-04 21:24:53 +0100805 if (lua_isnumber(L, 2)) // list item?
Bram Moolenaar1dced572012-04-05 16:54:08 +0200806 {
807 listitem_T *li = list_find(l, (long) luaL_checkinteger(L, 2));
808 if (li == NULL)
809 lua_pushnil(L);
810 else
811 luaV_pushtypval(L, &li->li_tv);
812 }
Bram Moolenaar2ab2e862019-12-04 21:24:53 +0100813 else if (lua_isstring(L, 2)) // method?
Bram Moolenaar1dced572012-04-05 16:54:08 +0200814 {
815 const char *s = lua_tostring(L, 2);
816 if (strncmp(s, "add", 3) == 0
Bram Moolenaarb3766472013-04-15 13:49:21 +0200817 || strncmp(s, "insert", 6) == 0)
Bram Moolenaar1dced572012-04-05 16:54:08 +0200818 {
819 lua_getmetatable(L, 1);
820 lua_getfield(L, -1, s);
821 }
822 else
823 lua_pushnil(L);
824 }
825 else
826 lua_pushnil(L);
827 return 1;
828}
829
830 static int
Bram Moolenaar4eefe472019-03-19 21:59:19 +0100831luaV_list_newindex(lua_State *L)
Bram Moolenaar1dced572012-04-05 16:54:08 +0200832{
833 list_T *l = luaV_unbox(L, luaV_List, 1);
834 long n = (long) luaL_checkinteger(L, 2);
835 listitem_T *li;
836 if (l->lv_lock)
837 luaL_error(L, "list is locked");
838 li = list_find(l, n);
839 if (li == NULL) return 0;
Bram Moolenaar2ab2e862019-12-04 21:24:53 +0100840 if (lua_isnil(L, 3)) // remove?
Bram Moolenaar1dced572012-04-05 16:54:08 +0200841 {
Bram Moolenaar3ec7f4e2014-05-07 17:31:37 +0200842 vimlist_remove(l, li, li);
Bram Moolenaar1dced572012-04-05 16:54:08 +0200843 clear_tv(&li->li_tv);
844 vim_free(li);
845 }
846 else
847 {
848 typval_T v;
Bram Moolenaarca06da92018-07-01 15:12:05 +0200849 luaV_checktypval(L, 3, &v, "setting list item");
Bram Moolenaar1dced572012-04-05 16:54:08 +0200850 clear_tv(&li->li_tv);
851 copy_tv(&v, &li->li_tv);
Bram Moolenaarb3766472013-04-15 13:49:21 +0200852 clear_tv(&v);
Bram Moolenaar1dced572012-04-05 16:54:08 +0200853 }
854 return 0;
855}
856
857 static int
Bram Moolenaar4eefe472019-03-19 21:59:19 +0100858luaV_list_add(lua_State *L)
Bram Moolenaar1dced572012-04-05 16:54:08 +0200859{
860 luaV_List *lis = luaV_checkudata(L, 1, LUAVIM_LIST);
861 list_T *l = (list_T *) luaV_checkcache(L, (void *) *lis);
Bram Moolenaarb3766472013-04-15 13:49:21 +0200862 typval_T v;
Bram Moolenaar1dced572012-04-05 16:54:08 +0200863 if (l->lv_lock)
864 luaL_error(L, "list is locked");
Bram Moolenaarb3766472013-04-15 13:49:21 +0200865 lua_settop(L, 2);
Bram Moolenaarca06da92018-07-01 15:12:05 +0200866 luaV_checktypval(L, 2, &v, "adding list item");
Bram Moolenaarb3766472013-04-15 13:49:21 +0200867 if (list_append_tv(l, &v) == FAIL)
Bram Moolenaarca06da92018-07-01 15:12:05 +0200868 luaL_error(L, "failed to add item to list");
Bram Moolenaarb3766472013-04-15 13:49:21 +0200869 clear_tv(&v);
Bram Moolenaar1dced572012-04-05 16:54:08 +0200870 lua_settop(L, 1);
871 return 1;
872}
873
874 static int
Bram Moolenaar4eefe472019-03-19 21:59:19 +0100875luaV_list_insert(lua_State *L)
Bram Moolenaar1dced572012-04-05 16:54:08 +0200876{
877 luaV_List *lis = luaV_checkudata(L, 1, LUAVIM_LIST);
878 list_T *l = (list_T *) luaV_checkcache(L, (void *) *lis);
Bram Moolenaar46538ee2015-02-17 16:28:55 +0100879 long pos = (long) luaL_optinteger(L, 3, 0);
Bram Moolenaar1dced572012-04-05 16:54:08 +0200880 listitem_T *li = NULL;
881 typval_T v;
882 if (l->lv_lock)
883 luaL_error(L, "list is locked");
884 if (pos < l->lv_len)
885 {
886 li = list_find(l, pos);
887 if (li == NULL)
888 luaL_error(L, "invalid position");
889 }
890 lua_settop(L, 2);
Bram Moolenaarca06da92018-07-01 15:12:05 +0200891 luaV_checktypval(L, 2, &v, "inserting list item");
Bram Moolenaarb3766472013-04-15 13:49:21 +0200892 if (list_insert_tv(l, &v, li) == FAIL)
Bram Moolenaarca06da92018-07-01 15:12:05 +0200893 luaL_error(L, "failed to add item to list");
Bram Moolenaarb3766472013-04-15 13:49:21 +0200894 clear_tv(&v);
Bram Moolenaar1dced572012-04-05 16:54:08 +0200895 lua_settop(L, 1);
896 return 1;
897}
898
899static const luaL_Reg luaV_List_mt[] = {
900 {"__tostring", luaV_list_tostring},
Bram Moolenaar1dced572012-04-05 16:54:08 +0200901 {"__len", luaV_list_len},
902 {"__call", luaV_list_call},
903 {"__index", luaV_list_index},
904 {"__newindex", luaV_list_newindex},
905 {"add", luaV_list_add},
906 {"insert", luaV_list_insert},
907 {NULL, NULL}
908};
909
910
Bram Moolenaar2ab2e862019-12-04 21:24:53 +0100911// ======= Dict type =======
Bram Moolenaar1dced572012-04-05 16:54:08 +0200912
913 static luaV_Dict *
Bram Moolenaar4eefe472019-03-19 21:59:19 +0100914luaV_newdict(lua_State *L, dict_T *dic)
Bram Moolenaar1dced572012-04-05 16:54:08 +0200915{
916 luaV_Dict *d = (luaV_Dict *) lua_newuserdata(L, sizeof(luaV_Dict));
917 *d = dic;
Bram Moolenaar2ab2e862019-12-04 21:24:53 +0100918 dic->dv_refcount++; // reference in Lua
919 luaV_setudata(L, dic); // cache[dic] = udata
Bram Moolenaar1dced572012-04-05 16:54:08 +0200920 luaV_getfield(L, LUAVIM_DICT);
921 lua_setmetatable(L, -2);
922 return d;
923}
924
925luaV_pushtype(dict_T, dict, luaV_Dict)
926luaV_type_tostring(dict, LUAVIM_DICT)
927
928 static int
Bram Moolenaar4eefe472019-03-19 21:59:19 +0100929luaV_dict_len(lua_State *L)
Bram Moolenaar1dced572012-04-05 16:54:08 +0200930{
931 dict_T *d = luaV_unbox(L, luaV_Dict, 1);
Bram Moolenaarb7828692019-03-23 13:57:02 +0100932 lua_pushinteger(L, (int) dict_len(d));
Bram Moolenaar1dced572012-04-05 16:54:08 +0200933 return 1;
934}
935
936 static int
Bram Moolenaar4eefe472019-03-19 21:59:19 +0100937luaV_dict_iter(lua_State *L UNUSED)
Bram Moolenaar1dced572012-04-05 16:54:08 +0200938{
Bram Moolenaarfeeaa682013-02-14 22:19:51 +0100939#ifdef FEAT_EVAL
Bram Moolenaar1dced572012-04-05 16:54:08 +0200940 hashitem_T *hi = (hashitem_T *) lua_touserdata(L, lua_upvalueindex(2));
941 int n = lua_tointeger(L, lua_upvalueindex(3));
942 dictitem_T *di;
943 if (n <= 0) return 0;
944 while (HASHITEM_EMPTY(hi)) hi++;
945 di = dict_lookup(hi);
946 lua_pushstring(L, (char *) hi->hi_key);
947 luaV_pushtypval(L, &di->di_tv);
948 lua_pushlightuserdata(L, (void *) (hi + 1));
949 lua_replace(L, lua_upvalueindex(2));
950 lua_pushinteger(L, n - 1);
951 lua_replace(L, lua_upvalueindex(3));
952 return 2;
Bram Moolenaarfeeaa682013-02-14 22:19:51 +0100953#else
954 return 0;
955#endif
Bram Moolenaar1dced572012-04-05 16:54:08 +0200956}
957
958 static int
Bram Moolenaar4eefe472019-03-19 21:59:19 +0100959luaV_dict_call(lua_State *L)
Bram Moolenaar1dced572012-04-05 16:54:08 +0200960{
961 dict_T *d = luaV_unbox(L, luaV_Dict, 1);
962 hashtab_T *ht = &d->dv_hashtab;
Bram Moolenaar2ab2e862019-12-04 21:24:53 +0100963 lua_pushvalue(L, lua_upvalueindex(1)); // pass cache table along
Bram Moolenaar1dced572012-04-05 16:54:08 +0200964 lua_pushlightuserdata(L, (void *) ht->ht_array);
Bram Moolenaar2ab2e862019-12-04 21:24:53 +0100965 lua_pushinteger(L, ht->ht_used); // # remaining items
Bram Moolenaar1dced572012-04-05 16:54:08 +0200966 lua_pushcclosure(L, luaV_dict_iter, 3);
967 return 1;
968}
969
970 static int
Bram Moolenaarca06da92018-07-01 15:12:05 +0200971luaV_dict_index(lua_State *L)
Bram Moolenaar1dced572012-04-05 16:54:08 +0200972{
973 dict_T *d = luaV_unbox(L, luaV_Dict, 1);
974 char_u *key = (char_u *) luaL_checkstring(L, 2);
975 dictitem_T *di = dict_find(d, key, -1);
Bram Moolenaarca06da92018-07-01 15:12:05 +0200976
Bram Moolenaar1dced572012-04-05 16:54:08 +0200977 if (di == NULL)
978 lua_pushnil(L);
979 else
Bram Moolenaarca06da92018-07-01 15:12:05 +0200980 {
Bram Moolenaar1dced572012-04-05 16:54:08 +0200981 luaV_pushtypval(L, &di->di_tv);
Bram Moolenaar2ab2e862019-12-04 21:24:53 +0100982 if (di->di_tv.v_type == VAR_FUNC) // funcref?
Bram Moolenaarca06da92018-07-01 15:12:05 +0200983 {
984 luaV_Funcref *f = (luaV_Funcref *) lua_touserdata(L, -1);
Bram Moolenaar2ab2e862019-12-04 21:24:53 +0100985 f->self = d; // keep "self" reference
Bram Moolenaarca06da92018-07-01 15:12:05 +0200986 d->dv_refcount++;
987 }
988 }
Bram Moolenaar1dced572012-04-05 16:54:08 +0200989 return 1;
990}
991
992 static int
Bram Moolenaarca06da92018-07-01 15:12:05 +0200993luaV_dict_newindex(lua_State *L)
Bram Moolenaar1dced572012-04-05 16:54:08 +0200994{
995 dict_T *d = luaV_unbox(L, luaV_Dict, 1);
996 char_u *key = (char_u *) luaL_checkstring(L, 2);
997 dictitem_T *di;
Bram Moolenaarca06da92018-07-01 15:12:05 +0200998 typval_T v;
Bram Moolenaar713bf9e92019-03-16 16:38:41 +0100999
Bram Moolenaar1dced572012-04-05 16:54:08 +02001000 if (d->dv_lock)
1001 luaL_error(L, "dict is locked");
Bram Moolenaard6ef5f92018-07-13 22:08:23 +02001002 if (key == NULL)
1003 return 0;
1004 if (*key == NUL)
Bram Moolenaarca06da92018-07-01 15:12:05 +02001005 luaL_error(L, "empty key");
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001006 if (!lua_isnil(L, 3)) // read value?
Bram Moolenaar17413672018-07-14 20:49:42 +02001007 {
Bram Moolenaarca06da92018-07-01 15:12:05 +02001008 luaV_checktypval(L, 3, &v, "setting dict item");
1009 if (d->dv_scope == VAR_DEF_SCOPE && v.v_type == VAR_FUNC)
1010 luaL_error(L, "cannot assign funcref to builtin scope");
1011 }
Bram Moolenaar1dced572012-04-05 16:54:08 +02001012 di = dict_find(d, key, -1);
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001013 if (di == NULL) // non-existing key?
Bram Moolenaar1dced572012-04-05 16:54:08 +02001014 {
Bram Moolenaard6ef5f92018-07-13 22:08:23 +02001015 if (lua_isnil(L, 3))
1016 return 0;
Bram Moolenaar1dced572012-04-05 16:54:08 +02001017 di = dictitem_alloc(key);
Bram Moolenaard6ef5f92018-07-13 22:08:23 +02001018 if (di == NULL)
1019 return 0;
Bram Moolenaar1dced572012-04-05 16:54:08 +02001020 if (dict_add(d, di) == FAIL)
1021 {
Bram Moolenaard6ef5f92018-07-13 22:08:23 +02001022 vim_free(di);
1023 return 0;
Bram Moolenaar1dced572012-04-05 16:54:08 +02001024 }
1025 }
1026 else
1027 clear_tv(&di->di_tv);
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001028 if (lua_isnil(L, 3)) // remove?
Bram Moolenaar1dced572012-04-05 16:54:08 +02001029 {
1030 hashitem_T *hi = hash_find(&d->dv_hashtab, di->di_key);
1031 hash_remove(&d->dv_hashtab, hi);
1032 dictitem_free(di);
1033 }
Bram Moolenaarca06da92018-07-01 15:12:05 +02001034 else
1035 {
Bram Moolenaar1dced572012-04-05 16:54:08 +02001036 copy_tv(&v, &di->di_tv);
Bram Moolenaarb3766472013-04-15 13:49:21 +02001037 clear_tv(&v);
Bram Moolenaar1dced572012-04-05 16:54:08 +02001038 }
1039 return 0;
1040}
1041
1042static const luaL_Reg luaV_Dict_mt[] = {
1043 {"__tostring", luaV_dict_tostring},
Bram Moolenaar1dced572012-04-05 16:54:08 +02001044 {"__len", luaV_dict_len},
1045 {"__call", luaV_dict_call},
1046 {"__index", luaV_dict_index},
1047 {"__newindex", luaV_dict_newindex},
1048 {NULL, NULL}
1049};
1050
1051
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001052// ======= Blob type =======
Bram Moolenaarb7828692019-03-23 13:57:02 +01001053
1054 static luaV_Blob *
1055luaV_newblob(lua_State *L, blob_T *blo)
1056{
1057 luaV_Blob *b = (luaV_Blob *) lua_newuserdata(L, sizeof(luaV_Blob));
1058 *b = blo;
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001059 blo->bv_refcount++; // reference in Lua
1060 luaV_setudata(L, blo); // cache[blo] = udata
Bram Moolenaarb7828692019-03-23 13:57:02 +01001061 luaV_getfield(L, LUAVIM_BLOB);
1062 lua_setmetatable(L, -2);
1063 return b;
1064}
1065
1066luaV_pushtype(blob_T, blob, luaV_Blob)
1067luaV_type_tostring(blob, LUAVIM_BLOB)
1068
1069 static int
1070luaV_blob_gc(lua_State *L)
1071{
1072 blob_T *b = luaV_unbox(L, luaV_Blob, 1);
1073 blob_unref(b);
1074 return 0;
1075}
1076
1077 static int
1078luaV_blob_len(lua_State *L)
1079{
1080 blob_T *b = luaV_unbox(L, luaV_Blob, 1);
1081 lua_pushinteger(L, (int) blob_len(b));
1082 return 1;
1083}
1084
1085 static int
1086luaV_blob_index(lua_State *L)
1087{
1088 blob_T *b = luaV_unbox(L, luaV_Blob, 1);
1089 if (lua_isnumber(L, 2))
1090 {
1091 int idx = luaL_checkinteger(L, 2);
1092 if (idx < blob_len(b))
1093 lua_pushnumber(L, (lua_Number) blob_get(b, idx));
1094 else
1095 lua_pushnil(L);
1096 }
1097 else if (lua_isstring(L, 2))
1098 {
1099 const char *s = lua_tostring(L, 2);
1100 if (strncmp(s, "add", 3) == 0)
1101 {
1102 lua_getmetatable(L, 1);
1103 lua_getfield(L, -1, s);
1104 }
1105 else
1106 lua_pushnil(L);
1107 }
1108 else
1109 lua_pushnil(L);
1110 return 1;
1111}
1112
1113 static int
1114luaV_blob_newindex(lua_State *L)
1115{
1116 blob_T *b = luaV_unbox(L, luaV_Blob, 1);
1117 if (b->bv_lock)
1118 luaL_error(L, "blob is locked");
1119 if (lua_isnumber(L, 2))
1120 {
1121 long len = blob_len(b);
1122 int idx = luaL_checkinteger(L, 2);
1123 int val = luaL_checkinteger(L, 3);
1124 if (idx < len || (idx == len && ga_grow(&b->bv_ga, 1) == OK))
1125 {
1126 blob_set(b, idx, (char_u) val);
1127 if (idx == len)
1128 ++b->bv_ga.ga_len;
1129 }
1130 else
1131 luaL_error(L, "index out of range");
1132 }
1133 return 0;
1134}
1135
1136 static int
1137luaV_blob_add(lua_State *L)
1138{
1139 luaV_Blob *blo = luaV_checkudata(L, 1, LUAVIM_BLOB);
1140 blob_T *b = (blob_T *) luaV_checkcache(L, (void *) *blo);
1141 if (b->bv_lock)
1142 luaL_error(L, "blob is locked");
1143 lua_settop(L, 2);
1144 if (!lua_isstring(L, 2))
1145 luaL_error(L, "string expected, got %s", luaL_typename(L, 2));
1146 else
1147 {
1148 size_t i, l = 0;
1149 const char *s = lua_tolstring(L, 2, &l);
1150
Bram Moolenaar6fb5c972019-03-26 21:44:20 +01001151 if (ga_grow(&b->bv_ga, l) == OK)
1152 for (i = 0; i < l; ++i)
1153 ga_append(&b->bv_ga, s[i]);
Bram Moolenaarb7828692019-03-23 13:57:02 +01001154 }
1155 lua_settop(L, 1);
1156 return 1;
1157}
1158
1159static const luaL_Reg luaV_Blob_mt[] = {
1160 {"__tostring", luaV_blob_tostring},
1161 {"__gc", luaV_blob_gc},
1162 {"__len", luaV_blob_len},
1163 {"__index", luaV_blob_index},
1164 {"__newindex", luaV_blob_newindex},
1165 {"add", luaV_blob_add},
1166 {NULL, NULL}
1167};
1168
1169
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001170// ======= Funcref type =======
Bram Moolenaarca06da92018-07-01 15:12:05 +02001171
1172 static luaV_Funcref *
1173luaV_newfuncref(lua_State *L, char_u *name)
1174{
1175 luaV_Funcref *f = (luaV_Funcref *)lua_newuserdata(L, sizeof(luaV_Funcref));
1176
1177 if (name != NULL)
1178 {
Bram Moolenaar4eefe472019-03-19 21:59:19 +01001179 func_ref(name);
1180 f->name = vim_strsave(name);
Bram Moolenaarca06da92018-07-01 15:12:05 +02001181 }
Bram Moolenaarca06da92018-07-01 15:12:05 +02001182 f->self = NULL;
1183 luaV_getfield(L, LUAVIM_FUNCREF);
1184 lua_setmetatable(L, -2);
1185 return f;
1186}
1187
1188 static luaV_Funcref *
Bram Moolenaar4eefe472019-03-19 21:59:19 +01001189luaV_pushfuncref(lua_State *L, char_u *name)
Bram Moolenaarca06da92018-07-01 15:12:05 +02001190{
Bram Moolenaar4eefe472019-03-19 21:59:19 +01001191 return luaV_newfuncref(L, name);
Bram Moolenaarca06da92018-07-01 15:12:05 +02001192}
1193
1194
1195luaV_type_tostring(funcref, LUAVIM_FUNCREF)
1196
1197 static int
1198luaV_funcref_gc(lua_State *L)
1199{
1200 luaV_Funcref *f = (luaV_Funcref *) lua_touserdata(L, 1);
1201
Bram Moolenaar4eefe472019-03-19 21:59:19 +01001202 func_unref(f->name);
1203 vim_free(f->name);
1204 // NOTE: Don't call "dict_unref(f->self)", because the dict of "f->self"
1205 // will be (or has been already) freed by Vim's garbage collection.
Bram Moolenaarca06da92018-07-01 15:12:05 +02001206 return 0;
1207}
1208
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001209// equivalent to string(funcref)
Bram Moolenaarca06da92018-07-01 15:12:05 +02001210 static int
1211luaV_funcref_len(lua_State *L)
1212{
1213 luaV_Funcref *f = (luaV_Funcref *) lua_touserdata(L, 1);
1214
Bram Moolenaar4eefe472019-03-19 21:59:19 +01001215 lua_pushstring(L, (const char *) f->name);
Bram Moolenaarca06da92018-07-01 15:12:05 +02001216 return 1;
1217}
1218
1219 static int
1220luaV_funcref_call(lua_State *L)
1221{
1222 luaV_Funcref *f = (luaV_Funcref *) lua_touserdata(L, 1);
Bram Moolenaar4eefe472019-03-19 21:59:19 +01001223 int i, n = lua_gettop(L) - 1; // #args
1224 int status = FAIL;
1225 typval_T args;
1226 typval_T rettv;
Bram Moolenaarca06da92018-07-01 15:12:05 +02001227
Bram Moolenaar4eefe472019-03-19 21:59:19 +01001228 args.v_type = VAR_LIST;
1229 args.vval.v_list = list_alloc();
1230 rettv.v_type = VAR_UNKNOWN; // as in clear_tv
1231 if (args.vval.v_list != NULL)
Bram Moolenaard6ef5f92018-07-13 22:08:23 +02001232 {
Bram Moolenaar4eefe472019-03-19 21:59:19 +01001233 typval_T v;
1234
Bram Moolenaar17413672018-07-14 20:49:42 +02001235 for (i = 0; i < n; i++)
1236 {
Bram Moolenaard6ef5f92018-07-13 22:08:23 +02001237 luaV_checktypval(L, i + 2, &v, "calling funcref");
Bram Moolenaar4eefe472019-03-19 21:59:19 +01001238 list_append_tv(args.vval.v_list, &v);
Bram Moolenaar713bf9e92019-03-16 16:38:41 +01001239 clear_tv(&v);
Bram Moolenaard6ef5f92018-07-13 22:08:23 +02001240 }
Bram Moolenaar4eefe472019-03-19 21:59:19 +01001241 status = func_call(f->name, &args, NULL, f->self, &rettv);
Bram Moolenaard6ef5f92018-07-13 22:08:23 +02001242 if (status == OK)
1243 luaV_pushtypval(L, &rettv);
Bram Moolenaar4eefe472019-03-19 21:59:19 +01001244 clear_tv(&args);
Bram Moolenaard6ef5f92018-07-13 22:08:23 +02001245 clear_tv(&rettv);
Bram Moolenaarca06da92018-07-01 15:12:05 +02001246 }
Bram Moolenaarca06da92018-07-01 15:12:05 +02001247 if (status != OK)
1248 luaL_error(L, "cannot call funcref");
1249 return 1;
1250}
1251
1252static const luaL_Reg luaV_Funcref_mt[] = {
1253 {"__tostring", luaV_funcref_tostring},
1254 {"__gc", luaV_funcref_gc},
1255 {"__len", luaV_funcref_len},
1256 {"__call", luaV_funcref_call},
1257 {NULL, NULL}
1258};
1259
1260
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001261// ======= Buffer type =======
Bram Moolenaar1dced572012-04-05 16:54:08 +02001262
1263luaV_newtype(buf_T, buffer, luaV_Buffer, LUAVIM_BUFFER)
1264luaV_pushtype(buf_T, buffer, luaV_Buffer)
1265luaV_type_tostring(buffer, LUAVIM_BUFFER)
1266
1267 static int
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001268luaV_buffer_len(lua_State *L)
1269{
Bram Moolenaar1dced572012-04-05 16:54:08 +02001270 buf_T *b = (buf_T *) luaV_checkvalid(L, luaV_Buffer, 1);
1271 lua_pushinteger(L, b->b_ml.ml_line_count);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001272 return 1;
1273}
1274
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001275 static int
1276luaV_buffer_call(lua_State *L)
1277{
Bram Moolenaar1dced572012-04-05 16:54:08 +02001278 buf_T *b = (buf_T *) luaV_checkvalid(L, luaV_Buffer, 1);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001279 lua_settop(L, 1);
Bram Moolenaar1dced572012-04-05 16:54:08 +02001280 set_curbuf(b, DOBUF_SPLIT);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001281 return 1;
1282}
1283
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001284 static int
1285luaV_buffer_index(lua_State *L)
1286{
Bram Moolenaar1dced572012-04-05 16:54:08 +02001287 buf_T *b = (buf_T *) luaV_checkvalid(L, luaV_Buffer, 1);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001288 linenr_T n = (linenr_T) lua_tointeger(L, 2);
Bram Moolenaar1dced572012-04-05 16:54:08 +02001289 if (n > 0 && n <= b->b_ml.ml_line_count)
1290 luaV_pushline(L, b, n);
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001291 else if (lua_isstring(L, 2))
1292 {
1293 const char *s = lua_tostring(L, 2);
1294 if (strncmp(s, "name", 4) == 0)
Bram Moolenaarfe08df42018-07-07 23:07:41 +02001295 lua_pushstring(L, (b->b_sfname == NULL)
1296 ? "" : (char *) b->b_sfname);
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001297 else if (strncmp(s, "fname", 5) == 0)
Bram Moolenaarfe08df42018-07-07 23:07:41 +02001298 lua_pushstring(L, (b->b_ffname == NULL)
1299 ? "" : (char *) b->b_ffname);
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001300 else if (strncmp(s, "number", 6) == 0)
Bram Moolenaar1dced572012-04-05 16:54:08 +02001301 lua_pushinteger(L, b->b_fnum);
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001302 // methods
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001303 else if (strncmp(s, "insert", 6) == 0
1304 || strncmp(s, "next", 4) == 0
1305 || strncmp(s, "previous", 8) == 0
1306 || strncmp(s, "isvalid", 7) == 0)
1307 {
1308 lua_getmetatable(L, 1);
1309 lua_getfield(L, -1, s);
1310 }
1311 else
1312 lua_pushnil(L);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001313 }
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001314 else
1315 lua_pushnil(L);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001316 return 1;
1317}
1318
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001319 static int
1320luaV_buffer_newindex(lua_State *L)
1321{
Bram Moolenaar1dced572012-04-05 16:54:08 +02001322 buf_T *b = (buf_T *) luaV_checkvalid(L, luaV_Buffer, 1);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001323 linenr_T n = (linenr_T) luaL_checkinteger(L, 2);
1324#ifdef HAVE_SANDBOX
1325 luaV_checksandbox(L);
1326#endif
Bram Moolenaar1dced572012-04-05 16:54:08 +02001327 if (n < 1 || n > b->b_ml.ml_line_count)
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001328 luaL_error(L, "invalid line number");
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001329 if (lua_isnil(L, 3)) // delete line
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001330 {
1331 buf_T *buf = curbuf;
Bram Moolenaar1dced572012-04-05 16:54:08 +02001332 curbuf = b;
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001333 if (u_savedel(n, 1L) == FAIL)
1334 {
1335 curbuf = buf;
1336 luaL_error(L, "cannot save undo information");
1337 }
1338 else if (ml_delete(n, FALSE) == FAIL)
1339 {
1340 curbuf = buf;
1341 luaL_error(L, "cannot delete line");
1342 }
Bram Moolenaarca06da92018-07-01 15:12:05 +02001343 else
1344 {
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001345 deleted_lines_mark(n, 1L);
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001346 if (b == curwin->w_buffer) // fix cursor in current window?
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001347 {
1348 if (curwin->w_cursor.lnum >= n)
1349 {
1350 if (curwin->w_cursor.lnum > n)
1351 {
1352 curwin->w_cursor.lnum -= 1;
1353 check_cursor_col();
1354 }
1355 else check_cursor();
1356 changed_cline_bef_curs();
1357 }
1358 invalidate_botline();
1359 }
1360 }
1361 curbuf = buf;
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001362 }
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001363 else if (lua_isstring(L, 3)) // update line
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001364 {
1365 buf_T *buf = curbuf;
Bram Moolenaar1dced572012-04-05 16:54:08 +02001366 curbuf = b;
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001367 if (u_savesub(n) == FAIL)
1368 {
1369 curbuf = buf;
1370 luaL_error(L, "cannot save undo information");
1371 }
1372 else if (ml_replace(n, luaV_toline(L, 3), TRUE) == FAIL)
1373 {
1374 curbuf = buf;
1375 luaL_error(L, "cannot replace line");
1376 }
1377 else changed_bytes(n, 0);
1378 curbuf = buf;
Bram Moolenaar1dced572012-04-05 16:54:08 +02001379 if (b == curwin->w_buffer)
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001380 check_cursor_col();
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001381 }
1382 else
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001383 luaL_error(L, "wrong argument to change line");
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001384 return 0;
1385}
1386
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001387 static int
1388luaV_buffer_insert(lua_State *L)
1389{
Bram Moolenaar1dced572012-04-05 16:54:08 +02001390 luaV_Buffer *lb = luaV_checkudata(L, 1, LUAVIM_BUFFER);
1391 buf_T *b = (buf_T *) luaV_checkcache(L, (void *) *lb);
1392 linenr_T last = b->b_ml.ml_line_count;
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001393 linenr_T n = (linenr_T) luaL_optinteger(L, 3, last);
1394 buf_T *buf;
1395 luaL_checktype(L, 2, LUA_TSTRING);
1396#ifdef HAVE_SANDBOX
1397 luaV_checksandbox(L);
1398#endif
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001399 // fix insertion line
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001400 if (n < 0) n = 0;
1401 if (n > last) n = last;
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001402 // insert
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001403 buf = curbuf;
Bram Moolenaar1dced572012-04-05 16:54:08 +02001404 curbuf = b;
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001405 if (u_save(n, n + 1) == FAIL)
1406 {
1407 curbuf = buf;
1408 luaL_error(L, "cannot save undo information");
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001409 }
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001410 else if (ml_append(n, luaV_toline(L, 2), 0, FALSE) == FAIL)
1411 {
1412 curbuf = buf;
1413 luaL_error(L, "cannot insert line");
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001414 }
1415 else
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001416 appended_lines_mark(n, 1L);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001417 curbuf = buf;
1418 update_screen(VALID);
1419 return 0;
1420}
1421
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001422 static int
1423luaV_buffer_next(lua_State *L)
1424{
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001425 luaV_Buffer *b = luaV_checkudata(L, 1, LUAVIM_BUFFER);
Bram Moolenaar1dced572012-04-05 16:54:08 +02001426 buf_T *buf = (buf_T *) luaV_checkcache(L, (void *) *b);
1427 luaV_pushbuffer(L, buf->b_next);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001428 return 1;
1429}
1430
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001431 static int
1432luaV_buffer_previous(lua_State *L)
1433{
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001434 luaV_Buffer *b = luaV_checkudata(L, 1, LUAVIM_BUFFER);
Bram Moolenaar1dced572012-04-05 16:54:08 +02001435 buf_T *buf = (buf_T *) luaV_checkcache(L, (void *) *b);
1436 luaV_pushbuffer(L, buf->b_prev);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001437 return 1;
1438}
1439
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001440 static int
1441luaV_buffer_isvalid(lua_State *L)
1442{
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001443 luaV_Buffer *b = luaV_checkudata(L, 1, LUAVIM_BUFFER);
Bram Moolenaar1dced572012-04-05 16:54:08 +02001444 luaV_getudata(L, *b);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001445 lua_pushboolean(L, !lua_isnil(L, -1));
1446 return 1;
1447}
1448
1449static const luaL_Reg luaV_Buffer_mt[] = {
1450 {"__tostring", luaV_buffer_tostring},
1451 {"__len", luaV_buffer_len},
1452 {"__call", luaV_buffer_call},
1453 {"__index", luaV_buffer_index},
1454 {"__newindex", luaV_buffer_newindex},
1455 {"insert", luaV_buffer_insert},
1456 {"next", luaV_buffer_next},
1457 {"previous", luaV_buffer_previous},
1458 {"isvalid", luaV_buffer_isvalid},
1459 {NULL, NULL}
1460};
1461
1462
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001463// ======= Window type =======
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001464
Bram Moolenaar1dced572012-04-05 16:54:08 +02001465luaV_newtype(win_T, window, luaV_Window, LUAVIM_WINDOW)
1466luaV_pushtype(win_T, window, luaV_Window)
1467luaV_type_tostring(window, LUAVIM_WINDOW)
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001468
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001469 static int
1470luaV_window_call(lua_State *L)
1471{
Bram Moolenaar1dced572012-04-05 16:54:08 +02001472 win_T *w = (win_T *) luaV_checkvalid(L, luaV_Window, 1);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001473 lua_settop(L, 1);
Bram Moolenaar1dced572012-04-05 16:54:08 +02001474 win_goto(w);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001475 return 1;
1476}
1477
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001478 static int
1479luaV_window_index(lua_State *L)
1480{
Bram Moolenaar1dced572012-04-05 16:54:08 +02001481 win_T *w = (win_T *) luaV_checkvalid(L, luaV_Window, 1);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001482 const char *s = luaL_checkstring(L, 2);
1483 if (strncmp(s, "buffer", 6) == 0)
Bram Moolenaar1dced572012-04-05 16:54:08 +02001484 luaV_pushbuffer(L, w->w_buffer);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001485 else if (strncmp(s, "line", 4) == 0)
Bram Moolenaar1dced572012-04-05 16:54:08 +02001486 lua_pushinteger(L, w->w_cursor.lnum);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001487 else if (strncmp(s, "col", 3) == 0)
Bram Moolenaar1dced572012-04-05 16:54:08 +02001488 lua_pushinteger(L, w->w_cursor.col + 1);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001489 else if (strncmp(s, "width", 5) == 0)
Bram Moolenaar02631462017-09-22 15:20:32 +02001490 lua_pushinteger(L, w->w_width);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001491 else if (strncmp(s, "height", 6) == 0)
Bram Moolenaar1dced572012-04-05 16:54:08 +02001492 lua_pushinteger(L, w->w_height);
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001493 // methods
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001494 else if (strncmp(s, "next", 4) == 0
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001495 || strncmp(s, "previous", 8) == 0
1496 || strncmp(s, "isvalid", 7) == 0)
1497 {
1498 lua_getmetatable(L, 1);
1499 lua_getfield(L, -1, s);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001500 }
1501 else
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001502 lua_pushnil(L);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001503 return 1;
1504}
1505
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001506 static int
Bram Moolenaar4eefe472019-03-19 21:59:19 +01001507luaV_window_newindex(lua_State *L)
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001508{
Bram Moolenaar1dced572012-04-05 16:54:08 +02001509 win_T *w = (win_T *) luaV_checkvalid(L, luaV_Window, 1);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001510 const char *s = luaL_checkstring(L, 2);
1511 int v = luaL_checkinteger(L, 3);
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001512 if (strncmp(s, "line", 4) == 0)
1513 {
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001514#ifdef HAVE_SANDBOX
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001515 luaV_checksandbox(L);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001516#endif
Bram Moolenaar1dced572012-04-05 16:54:08 +02001517 if (v < 1 || v > w->w_buffer->b_ml.ml_line_count)
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001518 luaL_error(L, "line out of range");
Bram Moolenaar1dced572012-04-05 16:54:08 +02001519 w->w_cursor.lnum = v;
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001520 update_screen(VALID);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001521 }
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001522 else if (strncmp(s, "col", 3) == 0)
1523 {
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001524#ifdef HAVE_SANDBOX
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001525 luaV_checksandbox(L);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001526#endif
Bram Moolenaar1dced572012-04-05 16:54:08 +02001527 w->w_cursor.col = v - 1;
Bram Moolenaar53901442018-07-25 22:02:36 +02001528 w->w_set_curswant = TRUE;
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001529 update_screen(VALID);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001530 }
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001531 else if (strncmp(s, "width", 5) == 0)
1532 {
1533 win_T *win = curwin;
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001534#ifdef FEAT_GUI
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001535 need_mouse_correct = TRUE;
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001536#endif
Bram Moolenaar1dced572012-04-05 16:54:08 +02001537 curwin = w;
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001538 win_setwidth(v);
1539 curwin = win;
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001540 }
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001541 else if (strncmp(s, "height", 6) == 0)
1542 {
1543 win_T *win = curwin;
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001544#ifdef FEAT_GUI
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001545 need_mouse_correct = TRUE;
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001546#endif
Bram Moolenaar1dced572012-04-05 16:54:08 +02001547 curwin = w;
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001548 win_setheight(v);
1549 curwin = win;
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001550 }
1551 else
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001552 luaL_error(L, "invalid window property: `%s'", s);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001553 return 0;
1554}
1555
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001556 static int
1557luaV_window_next(lua_State *L)
1558{
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001559 luaV_Window *w = luaV_checkudata(L, 1, LUAVIM_WINDOW);
Bram Moolenaar1dced572012-04-05 16:54:08 +02001560 win_T *win = (win_T *) luaV_checkcache(L, (void *) *w);
1561 luaV_pushwindow(L, win->w_next);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001562 return 1;
1563}
1564
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001565 static int
1566luaV_window_previous(lua_State *L)
1567{
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001568 luaV_Window *w = luaV_checkudata(L, 1, LUAVIM_WINDOW);
Bram Moolenaar1dced572012-04-05 16:54:08 +02001569 win_T *win = (win_T *) luaV_checkcache(L, (void *) *w);
1570 luaV_pushwindow(L, win->w_prev);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001571 return 1;
1572}
1573
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001574 static int
1575luaV_window_isvalid(lua_State *L)
1576{
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001577 luaV_Window *w = luaV_checkudata(L, 1, LUAVIM_WINDOW);
Bram Moolenaar1dced572012-04-05 16:54:08 +02001578 luaV_getudata(L, *w);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001579 lua_pushboolean(L, !lua_isnil(L, -1));
1580 return 1;
1581}
1582
1583static const luaL_Reg luaV_Window_mt[] = {
1584 {"__tostring", luaV_window_tostring},
1585 {"__call", luaV_window_call},
1586 {"__index", luaV_window_index},
1587 {"__newindex", luaV_window_newindex},
1588 {"next", luaV_window_next},
1589 {"previous", luaV_window_previous},
1590 {"isvalid", luaV_window_isvalid},
1591 {NULL, NULL}
1592};
1593
1594
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001595// ======= Vim module =======
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001596
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001597 static int
1598luaV_print(lua_State *L)
1599{
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001600 int i, n = lua_gettop(L); // nargs
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001601 const char *s;
1602 size_t l;
1603 luaL_Buffer b;
1604 luaL_buffinit(L, &b);
1605 lua_getglobal(L, "tostring");
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001606 for (i = 1; i <= n; i++)
1607 {
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001608 lua_pushvalue(L, -1); // tostring
1609 lua_pushvalue(L, i); // arg
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001610 lua_call(L, 1, 1);
1611 s = lua_tolstring(L, -1, &l);
1612 if (s == NULL)
1613 return luaL_error(L, "cannot convert to string");
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001614 if (i > 1) luaL_addchar(&b, ' '); // use space instead of tab
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001615 luaV_addlstring(&b, s, l, 0);
1616 lua_pop(L, 1);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001617 }
1618 luaL_pushresult(&b);
Bram Moolenaarb98678a2019-10-19 15:18:44 +02001619 if (!got_int)
1620 luaV_msg(L);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001621 return 0;
1622}
1623
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001624 static int
Bram Moolenaar38e2b062011-09-21 17:15:39 +02001625luaV_debug(lua_State *L)
1626{
1627 lua_settop(L, 0);
1628 lua_getglobal(L, "vim");
1629 lua_getfield(L, -1, "eval");
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001630 lua_remove(L, -2); // vim.eval at position 1
Bram Moolenaar38e2b062011-09-21 17:15:39 +02001631 for (;;)
1632 {
1633 const char *input;
1634 size_t l;
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001635 lua_pushvalue(L, 1); // vim.eval
Bram Moolenaar38e2b062011-09-21 17:15:39 +02001636 lua_pushliteral(L, "input('lua_debug> ')");
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001637 lua_call(L, 1, 1); // return string
Bram Moolenaar38e2b062011-09-21 17:15:39 +02001638 input = lua_tolstring(L, -1, &l);
1639 if (l == 0 || strcmp(input, "cont") == 0)
1640 return 0;
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001641 msg_putchar('\n'); // avoid outputting on input line
Bram Moolenaar38e2b062011-09-21 17:15:39 +02001642 if (luaL_loadbuffer(L, input, l, "=(debug command)")
1643 || lua_pcall(L, 0, 0, 0))
1644 luaV_emsg(L);
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001645 lua_settop(L, 1); // remove eventual returns, but keep vim.eval
Bram Moolenaar38e2b062011-09-21 17:15:39 +02001646 }
1647}
1648
1649 static int
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001650luaV_command(lua_State *L)
1651{
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001652 do_cmdline_cmd((char_u *) luaL_checkstring(L, 1));
1653 update_screen(VALID);
1654 return 0;
1655}
1656
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001657 static int
1658luaV_eval(lua_State *L)
1659{
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001660 typval_T *tv = eval_expr((char_u *) luaL_checkstring(L, 1), NULL);
1661 if (tv == NULL) luaL_error(L, "invalid expression");
1662 luaV_pushtypval(L, tv);
Bram Moolenaarb3766472013-04-15 13:49:21 +02001663 free_tv(tv);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001664 return 1;
1665}
1666
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001667 static int
Bram Moolenaar0d2e4fc2010-07-18 12:35:47 +02001668luaV_beep(lua_State *L UNUSED)
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001669{
Bram Moolenaar165bc692015-07-21 17:53:25 +02001670 vim_beep(BO_LANG);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001671 return 0;
1672}
1673
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001674 static int
1675luaV_line(lua_State *L)
1676{
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001677 luaV_pushline(L, curbuf, curwin->w_cursor.lnum);
1678 return 1;
1679}
1680
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001681 static int
Bram Moolenaar1dced572012-04-05 16:54:08 +02001682luaV_list(lua_State *L)
1683{
Bram Moolenaarca06da92018-07-01 15:12:05 +02001684 list_T *l;
1685 int initarg = !lua_isnoneornil(L, 1);
1686
1687 if (initarg && lua_type(L, 1) != LUA_TTABLE)
1688 luaL_error(L, "table expected, got %s", luaL_typename(L, 1));
1689 l = list_alloc();
Bram Moolenaar1dced572012-04-05 16:54:08 +02001690 if (l == NULL)
1691 lua_pushnil(L);
1692 else
Bram Moolenaarca06da92018-07-01 15:12:05 +02001693 {
Bram Moolenaar1dced572012-04-05 16:54:08 +02001694 luaV_newlist(L, l);
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001695 if (initarg) // traverse table to init list
Bram Moolenaar17413672018-07-14 20:49:42 +02001696 {
Bram Moolenaarca06da92018-07-01 15:12:05 +02001697 int notnil, i = 0;
1698 typval_T v;
Bram Moolenaar17413672018-07-14 20:49:42 +02001699 do
1700 {
Bram Moolenaarca06da92018-07-01 15:12:05 +02001701 lua_rawgeti(L, 1, ++i);
1702 notnil = !lua_isnil(L, -1);
Bram Moolenaar17413672018-07-14 20:49:42 +02001703 if (notnil)
1704 {
Bram Moolenaarca06da92018-07-01 15:12:05 +02001705 luaV_checktypval(L, -1, &v, "vim.list");
1706 list_append_tv(l, &v);
Bram Moolenaar713bf9e92019-03-16 16:38:41 +01001707 clear_tv(&v);
Bram Moolenaarca06da92018-07-01 15:12:05 +02001708 }
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001709 lua_pop(L, 1); // value
Bram Moolenaarca06da92018-07-01 15:12:05 +02001710 } while (notnil);
1711 }
1712 }
Bram Moolenaar1dced572012-04-05 16:54:08 +02001713 return 1;
1714}
1715
1716 static int
1717luaV_dict(lua_State *L)
1718{
Bram Moolenaarca06da92018-07-01 15:12:05 +02001719 dict_T *d;
1720 int initarg = !lua_isnoneornil(L, 1);
1721
1722 if (initarg && lua_type(L, 1) != LUA_TTABLE)
1723 luaL_error(L, "table expected, got %s", luaL_typename(L, 1));
1724 d = dict_alloc();
Bram Moolenaar1dced572012-04-05 16:54:08 +02001725 if (d == NULL)
1726 lua_pushnil(L);
1727 else
Bram Moolenaarca06da92018-07-01 15:12:05 +02001728 {
Bram Moolenaar1dced572012-04-05 16:54:08 +02001729 luaV_newdict(L, d);
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001730 if (initarg) // traverse table to init dict
Bram Moolenaarca06da92018-07-01 15:12:05 +02001731 {
1732 lua_pushnil(L);
1733 while (lua_next(L, 1))
1734 {
1735 char_u *key;
1736 dictitem_T *di;
1737 typval_T v;
Bram Moolenaard6ef5f92018-07-13 22:08:23 +02001738
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001739 lua_pushvalue(L, -2); // dup key in case it's a number
Bram Moolenaarca06da92018-07-01 15:12:05 +02001740 key = (char_u *) lua_tostring(L, -1);
Bram Moolenaard6ef5f92018-07-13 22:08:23 +02001741 if (key == NULL)
1742 {
1743 lua_pushnil(L);
1744 return 1;
1745 }
1746 if (*key == NUL)
Bram Moolenaarca06da92018-07-01 15:12:05 +02001747 luaL_error(L, "table has empty key");
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001748 luaV_checktypval(L, -2, &v, "vim.dict"); // value
Bram Moolenaarca06da92018-07-01 15:12:05 +02001749 di = dictitem_alloc(key);
Bram Moolenaard6ef5f92018-07-13 22:08:23 +02001750 if (di == NULL || dict_add(d, di) == FAIL)
1751 {
Bram Moolenaarca06da92018-07-01 15:12:05 +02001752 vim_free(di);
1753 lua_pushnil(L);
1754 return 1;
1755 }
1756 copy_tv(&v, &di->di_tv);
1757 clear_tv(&v);
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001758 lua_pop(L, 2); // key copy and value
Bram Moolenaarca06da92018-07-01 15:12:05 +02001759 }
1760 }
1761 }
1762 return 1;
1763}
1764
1765 static int
Bram Moolenaarb7828692019-03-23 13:57:02 +01001766luaV_blob(lua_State *L)
1767{
1768 blob_T *b;
1769 int initarg = !lua_isnoneornil(L, 1);
1770
1771 if (initarg && !lua_isstring(L, 1))
1772 luaL_error(L, "string expected, got %s", luaL_typename(L, 1));
1773 b = blob_alloc();
1774 if (b == NULL)
1775 lua_pushnil(L);
1776 else
1777 {
1778 luaV_newblob(L, b);
1779 if (initarg)
1780 {
1781 size_t i, l = 0;
1782 const char *s = lua_tolstring(L, 1, &l);
1783
Bram Moolenaar6fb5c972019-03-26 21:44:20 +01001784 if (ga_grow(&b->bv_ga, l) == OK)
1785 for (i = 0; i < l; ++i)
1786 ga_append(&b->bv_ga, s[i]);
Bram Moolenaarb7828692019-03-23 13:57:02 +01001787 }
1788 }
1789 return 1;
1790}
1791
1792 static int
Bram Moolenaarca06da92018-07-01 15:12:05 +02001793luaV_funcref(lua_State *L)
1794{
1795 const char *name = luaL_checkstring(L, 1);
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001796 // note: not checking if function exists (needs function_exists)
Bram Moolenaarca06da92018-07-01 15:12:05 +02001797 if (name == NULL || *name == NUL || VIM_ISDIGIT(*name))
1798 luaL_error(L, "invalid function name: %s", name);
1799 luaV_newfuncref(L, (char_u *) name);
Bram Moolenaar1dced572012-04-05 16:54:08 +02001800 return 1;
1801}
1802
1803 static int
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001804luaV_buffer(lua_State *L)
1805{
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001806 buf_T *buf;
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001807 if (lua_isstring(L, 1)) // get by number or name?
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001808 {
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001809 if (lua_isnumber(L, 1)) // by number?
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001810 {
1811 int n = lua_tointeger(L, 1);
Bram Moolenaar29323592016-07-24 22:04:11 +02001812 FOR_ALL_BUFFERS(buf)
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001813 if (buf->b_fnum == n) break;
1814 }
Bram Moolenaarca06da92018-07-01 15:12:05 +02001815 else // by name
1816 {
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001817 size_t l;
1818 const char *s = lua_tolstring(L, 1, &l);
Bram Moolenaar29323592016-07-24 22:04:11 +02001819 FOR_ALL_BUFFERS(buf)
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001820 {
1821 if (buf->b_ffname == NULL || buf->b_sfname == NULL)
1822 {
1823 if (l == 0) break;
1824 }
Bram Moolenaar0d2e4fc2010-07-18 12:35:47 +02001825 else if (strncmp(s, (char *)buf->b_ffname, l) == 0
1826 || strncmp(s, (char *)buf->b_sfname, l) == 0)
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001827 break;
1828 }
1829 }
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001830 }
Bram Moolenaar1dced572012-04-05 16:54:08 +02001831 else
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001832 buf = (lua_toboolean(L, 1)) ? firstbuf : curbuf; // first buffer?
Bram Moolenaar1dced572012-04-05 16:54:08 +02001833 luaV_pushbuffer(L, buf);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001834 return 1;
1835}
1836
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001837 static int
1838luaV_window(lua_State *L)
1839{
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001840 win_T *win;
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001841 if (lua_isnumber(L, 1)) // get by number?
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001842 {
1843 int n = lua_tointeger(L, 1);
1844 for (win = firstwin; win != NULL; win = win->w_next, n--)
1845 if (n == 1) break;
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001846 }
Bram Moolenaar1dced572012-04-05 16:54:08 +02001847 else
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001848 win = (lua_toboolean(L, 1)) ? firstwin : curwin; // first window?
Bram Moolenaar1dced572012-04-05 16:54:08 +02001849 luaV_pushwindow(L, win);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001850 return 1;
1851}
1852
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001853 static int
1854luaV_open(lua_State *L)
1855{
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001856 char_u *s = NULL;
1857#ifdef HAVE_SANDBOX
1858 luaV_checksandbox(L);
1859#endif
1860 if (lua_isstring(L, 1)) s = (char_u *) lua_tostring(L, 1);
Bram Moolenaarfa263a52011-12-08 16:00:16 +01001861 luaV_pushbuffer(L, buflist_new(s, NULL, 1L, BLN_LISTED));
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001862 return 1;
1863}
1864
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001865 static int
Bram Moolenaar1dced572012-04-05 16:54:08 +02001866luaV_type(lua_State *L)
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001867{
Bram Moolenaar1dced572012-04-05 16:54:08 +02001868 luaL_checkany(L, 1);
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001869 if (lua_type(L, 1) == LUA_TUSERDATA) // check vim udata?
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001870 {
Bram Moolenaar1dced572012-04-05 16:54:08 +02001871 lua_settop(L, 1);
1872 if (lua_getmetatable(L, 1))
1873 {
1874 luaV_getfield(L, LUAVIM_LIST);
1875 if (lua_rawequal(L, -1, 2))
1876 {
1877 lua_pushstring(L, "list");
1878 return 1;
1879 }
1880 luaV_getfield(L, LUAVIM_DICT);
1881 if (lua_rawequal(L, -1, 2))
1882 {
1883 lua_pushstring(L, "dict");
1884 return 1;
1885 }
Bram Moolenaarb7828692019-03-23 13:57:02 +01001886 luaV_getfield(L, LUAVIM_BLOB);
1887 if (lua_rawequal(L, -1, 2))
1888 {
1889 lua_pushstring(L, "blob");
1890 return 1;
1891 }
Bram Moolenaarca06da92018-07-01 15:12:05 +02001892 luaV_getfield(L, LUAVIM_FUNCREF);
1893 if (lua_rawequal(L, -1, 2))
1894 {
1895 lua_pushstring(L, "funcref");
1896 return 1;
1897 }
Bram Moolenaar1dced572012-04-05 16:54:08 +02001898 luaV_getfield(L, LUAVIM_BUFFER);
1899 if (lua_rawequal(L, -1, 2))
1900 {
1901 lua_pushstring(L, "buffer");
1902 return 1;
1903 }
1904 luaV_getfield(L, LUAVIM_WINDOW);
1905 if (lua_rawequal(L, -1, 2))
1906 {
1907 lua_pushstring(L, "window");
1908 return 1;
1909 }
1910 }
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001911 }
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001912 lua_pushstring(L, luaL_typename(L, 1)); // fallback
Bram Moolenaar1dced572012-04-05 16:54:08 +02001913 return 1;
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001914}
1915
1916static const luaL_Reg luaV_module[] = {
1917 {"command", luaV_command},
1918 {"eval", luaV_eval},
1919 {"beep", luaV_beep},
1920 {"line", luaV_line},
Bram Moolenaar1dced572012-04-05 16:54:08 +02001921 {"list", luaV_list},
1922 {"dict", luaV_dict},
Bram Moolenaarb7828692019-03-23 13:57:02 +01001923 {"blob", luaV_blob},
Bram Moolenaarca06da92018-07-01 15:12:05 +02001924 {"funcref", luaV_funcref},
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001925 {"buffer", luaV_buffer},
1926 {"window", luaV_window},
1927 {"open", luaV_open},
Bram Moolenaar1dced572012-04-05 16:54:08 +02001928 {"type", luaV_type},
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001929 {NULL, NULL}
1930};
1931
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001932/*
1933 * for freeing list, dict, buffer and window objects; lightuserdata as arg
1934 */
Bram Moolenaar1dced572012-04-05 16:54:08 +02001935 static int
1936luaV_free(lua_State *L)
1937{
1938 lua_pushnil(L);
1939 luaV_setudata(L, lua_touserdata(L, 1));
1940 return 0;
1941}
1942
1943 static int
Bram Moolenaar4eefe472019-03-19 21:59:19 +01001944luaV_luaeval(lua_State *L)
Bram Moolenaar1dced572012-04-05 16:54:08 +02001945{
1946 luaL_Buffer b;
1947 size_t l;
1948 const char *str = lua_tolstring(L, 1, &l);
1949 typval_T *arg = (typval_T *) lua_touserdata(L, 2);
1950 typval_T *rettv = (typval_T *) lua_touserdata(L, 3);
1951 luaL_buffinit(L, &b);
1952 luaL_addlstring(&b, LUAVIM_EVALHEADER, sizeof(LUAVIM_EVALHEADER) - 1);
1953 luaL_addlstring(&b, str, l);
1954 luaL_pushresult(&b);
1955 str = lua_tolstring(L, -1, &l);
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001956 if (luaL_loadbuffer(L, str, l, LUAVIM_EVALNAME)) // compile error?
Bram Moolenaar1dced572012-04-05 16:54:08 +02001957 {
1958 luaV_emsg(L);
1959 return 0;
1960 }
1961 luaV_pushtypval(L, arg);
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01001962 if (lua_pcall(L, 1, 1, 0)) // running error?
Bram Moolenaar1dced572012-04-05 16:54:08 +02001963 {
1964 luaV_emsg(L);
1965 return 0;
1966 }
Bram Moolenaarca06da92018-07-01 15:12:05 +02001967 if (luaV_totypval(L, -1, rettv) == FAIL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001968 emsg("luaeval: cannot convert value");
Bram Moolenaarf554a322015-02-04 23:08:01 +01001969 return 0;
Bram Moolenaar1dced572012-04-05 16:54:08 +02001970}
1971
1972 static int
Bram Moolenaar4eefe472019-03-19 21:59:19 +01001973luaV_setref(lua_State *L)
Bram Moolenaar1dced572012-04-05 16:54:08 +02001974{
Bram Moolenaar4eefe472019-03-19 21:59:19 +01001975 int copyID = lua_tointeger(L, 1);
1976 int abort = FALSE;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01001977
Bram Moolenaar1dced572012-04-05 16:54:08 +02001978 luaV_getfield(L, LUAVIM_LIST);
1979 luaV_getfield(L, LUAVIM_DICT);
Bram Moolenaar4eefe472019-03-19 21:59:19 +01001980 luaV_getfield(L, LUAVIM_FUNCREF);
Bram Moolenaar1dced572012-04-05 16:54:08 +02001981 lua_pushnil(L);
Bram Moolenaar4eefe472019-03-19 21:59:19 +01001982 // traverse cache table
Bram Moolenaarb84634d2015-02-04 22:02:37 +01001983 while (!abort && lua_next(L, lua_upvalueindex(1)) != 0)
Bram Moolenaar1dced572012-04-05 16:54:08 +02001984 {
1985 lua_getmetatable(L, -1);
Bram Moolenaar4eefe472019-03-19 21:59:19 +01001986 if (lua_rawequal(L, -1, 2)) // list?
Bram Moolenaar1dced572012-04-05 16:54:08 +02001987 {
Bram Moolenaar4eefe472019-03-19 21:59:19 +01001988 list_T *l = (list_T *)lua_touserdata(L, 5); // key
1989
Bram Moolenaar7be3ab22019-06-23 01:46:15 +02001990 abort = set_ref_in_list(l, copyID);
Bram Moolenaar1dced572012-04-05 16:54:08 +02001991 }
Bram Moolenaar4eefe472019-03-19 21:59:19 +01001992 else if (lua_rawequal(L, -1, 3)) // dict?
Bram Moolenaar1dced572012-04-05 16:54:08 +02001993 {
Bram Moolenaar4eefe472019-03-19 21:59:19 +01001994 dict_T *d = (dict_T *)lua_touserdata(L, 5); // key
1995
Bram Moolenaar7be3ab22019-06-23 01:46:15 +02001996 abort = set_ref_in_dict(d, copyID);
Bram Moolenaar1dced572012-04-05 16:54:08 +02001997 }
Bram Moolenaar4eefe472019-03-19 21:59:19 +01001998 else if (lua_rawequal(L, -1, 4)) // funcref?
1999 {
2000 luaV_Funcref *f = (luaV_Funcref *)lua_touserdata(L, 5); // key
2001
Bram Moolenaar7be3ab22019-06-23 01:46:15 +02002002 abort = set_ref_in_dict(f->self, copyID);
Bram Moolenaar4eefe472019-03-19 21:59:19 +01002003 }
2004 lua_pop(L, 2); // metatable and value
Bram Moolenaar1dced572012-04-05 16:54:08 +02002005 }
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01002006 lua_pushinteger(L, abort);
Bram Moolenaarf554a322015-02-04 23:08:01 +01002007 return 1;
Bram Moolenaar1dced572012-04-05 16:54:08 +02002008}
2009
Bram Moolenaar55d5c032010-07-17 23:52:29 +02002010 static int
2011luaopen_vim(lua_State *L)
2012{
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002013 // set cache table
Bram Moolenaar0ba04292010-07-14 23:23:17 +02002014 lua_newtable(L);
2015 lua_newtable(L);
Bram Moolenaar1dced572012-04-05 16:54:08 +02002016 lua_pushstring(L, "v");
Bram Moolenaar0ba04292010-07-14 23:23:17 +02002017 lua_setfield(L, -2, "__mode");
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002018 lua_setmetatable(L, -2); // cache is weak-valued
2019 // print
Bram Moolenaar0ba04292010-07-14 23:23:17 +02002020 lua_pushcfunction(L, luaV_print);
2021 lua_setglobal(L, "print");
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002022 // debug.debug
Bram Moolenaar38e2b062011-09-21 17:15:39 +02002023 lua_getglobal(L, "debug");
2024 lua_pushcfunction(L, luaV_debug);
2025 lua_setfield(L, -2, "debug");
2026 lua_pop(L, 1);
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002027 // free
Bram Moolenaar0ba04292010-07-14 23:23:17 +02002028 lua_pushlightuserdata(L, (void *) LUAVIM_FREE);
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002029 lua_pushvalue(L, 1); // cache table
Bram Moolenaar1dced572012-04-05 16:54:08 +02002030 lua_pushcclosure(L, luaV_free, 1);
2031 lua_rawset(L, LUA_REGISTRYINDEX);
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002032 // luaeval
Bram Moolenaar1dced572012-04-05 16:54:08 +02002033 lua_pushlightuserdata(L, (void *) LUAVIM_LUAEVAL);
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002034 lua_pushvalue(L, 1); // cache table
Bram Moolenaar1dced572012-04-05 16:54:08 +02002035 lua_pushcclosure(L, luaV_luaeval, 1);
2036 lua_rawset(L, LUA_REGISTRYINDEX);
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002037 // setref
Bram Moolenaar1dced572012-04-05 16:54:08 +02002038 lua_pushlightuserdata(L, (void *) LUAVIM_SETREF);
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002039 lua_pushvalue(L, 1); // cache table
Bram Moolenaar1dced572012-04-05 16:54:08 +02002040 lua_pushcclosure(L, luaV_setref, 1);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02002041 lua_rawset(L, LUA_REGISTRYINDEX);
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002042 // register
Bram Moolenaar1dced572012-04-05 16:54:08 +02002043 luaV_newmetatable(L, LUAVIM_LIST);
2044 lua_pushvalue(L, 1);
2045 luaV_openlib(L, luaV_List_mt, 1);
2046 luaV_newmetatable(L, LUAVIM_DICT);
2047 lua_pushvalue(L, 1);
2048 luaV_openlib(L, luaV_Dict_mt, 1);
Bram Moolenaarb7828692019-03-23 13:57:02 +01002049 luaV_newmetatable(L, LUAVIM_BLOB);
2050 lua_pushvalue(L, 1);
2051 luaV_openlib(L, luaV_Blob_mt, 1);
Bram Moolenaarca06da92018-07-01 15:12:05 +02002052 luaV_newmetatable(L, LUAVIM_FUNCREF);
2053 lua_pushvalue(L, 1);
2054 luaV_openlib(L, luaV_Funcref_mt, 1);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02002055 luaV_newmetatable(L, LUAVIM_BUFFER);
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002056 lua_pushvalue(L, 1); // cache table
Bram Moolenaar1dced572012-04-05 16:54:08 +02002057 luaV_openlib(L, luaV_Buffer_mt, 1);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02002058 luaV_newmetatable(L, LUAVIM_WINDOW);
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002059 lua_pushvalue(L, 1); // cache table
Bram Moolenaar1dced572012-04-05 16:54:08 +02002060 luaV_openlib(L, luaV_Window_mt, 1);
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002061 lua_newtable(L); // vim table
2062 lua_pushvalue(L, 1); // cache table
Bram Moolenaar1dced572012-04-05 16:54:08 +02002063 luaV_openlib(L, luaV_module, 1);
2064 lua_setglobal(L, LUAVIM_NAME);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02002065 return 0;
2066}
2067
Bram Moolenaar55d5c032010-07-17 23:52:29 +02002068 static lua_State *
2069luaV_newstate(void)
2070{
Bram Moolenaar0ba04292010-07-14 23:23:17 +02002071 lua_State *L = luaL_newstate();
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002072 luaL_openlibs(L); // core libs
2073 lua_pushcfunction(L, luaopen_vim); // vim
Bram Moolenaar0ba04292010-07-14 23:23:17 +02002074 lua_call(L, 0, 0);
2075 return L;
2076}
2077
Bram Moolenaar55d5c032010-07-17 23:52:29 +02002078 static void
2079luaV_setrange(lua_State *L, int line1, int line2)
2080{
Bram Moolenaar0ba04292010-07-14 23:23:17 +02002081 lua_getglobal(L, LUAVIM_NAME);
2082 lua_pushinteger(L, line1);
2083 lua_setfield(L, -2, "firstline");
2084 lua_pushinteger(L, line2);
2085 lua_setfield(L, -2, "lastline");
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002086 lua_pop(L, 1); // vim table
Bram Moolenaar0ba04292010-07-14 23:23:17 +02002087}
2088
2089
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002090// ======= Interface =======
Bram Moolenaar0ba04292010-07-14 23:23:17 +02002091
2092static lua_State *L = NULL;
2093
Bram Moolenaar55d5c032010-07-17 23:52:29 +02002094 static int
Bram Moolenaar1dced572012-04-05 16:54:08 +02002095lua_isopen(void)
Bram Moolenaar2bd6a1b2010-08-12 22:14:01 +02002096{
2097 return L != NULL;
2098}
2099
2100 static int
Bram Moolenaar55d5c032010-07-17 23:52:29 +02002101lua_init(void)
2102{
Bram Moolenaar1dced572012-04-05 16:54:08 +02002103 if (!lua_isopen())
Bram Moolenaar55d5c032010-07-17 23:52:29 +02002104 {
Bram Moolenaar0ba04292010-07-14 23:23:17 +02002105#ifdef DYNAMIC_LUA
Bram Moolenaar55d5c032010-07-17 23:52:29 +02002106 if (!lua_enabled(TRUE))
2107 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002108 emsg(_("Lua library cannot be loaded."));
Bram Moolenaar55d5c032010-07-17 23:52:29 +02002109 return FAIL;
2110 }
Bram Moolenaar0ba04292010-07-14 23:23:17 +02002111#endif
Bram Moolenaar55d5c032010-07-17 23:52:29 +02002112 L = luaV_newstate();
Bram Moolenaar0ba04292010-07-14 23:23:17 +02002113 }
2114 return OK;
2115}
2116
Bram Moolenaar55d5c032010-07-17 23:52:29 +02002117 void
2118lua_end(void)
2119{
Bram Moolenaar1dced572012-04-05 16:54:08 +02002120 if (lua_isopen())
Bram Moolenaar55d5c032010-07-17 23:52:29 +02002121 {
2122 lua_close(L);
2123 L = NULL;
Bram Moolenaar0ba04292010-07-14 23:23:17 +02002124#ifdef DYNAMIC_LUA
Bram Moolenaar55d5c032010-07-17 23:52:29 +02002125 end_dynamic_lua();
Bram Moolenaar0ba04292010-07-14 23:23:17 +02002126#endif
2127 }
2128}
2129
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002130/*
2131 * ex commands
2132 */
Bram Moolenaar55d5c032010-07-17 23:52:29 +02002133 void
2134ex_lua(exarg_T *eap)
2135{
Bram Moolenaar0ba04292010-07-14 23:23:17 +02002136 char *script;
2137 if (lua_init() == FAIL) return;
2138 script = (char *) script_get(eap, eap->arg);
Bram Moolenaar55d5c032010-07-17 23:52:29 +02002139 if (!eap->skip)
2140 {
2141 char *s = (script) ? script : (char *) eap->arg;
2142 luaV_setrange(L, eap->line1, eap->line2);
2143 if (luaL_loadbuffer(L, s, strlen(s), LUAVIM_CHUNKNAME)
2144 || lua_pcall(L, 0, 0, 0))
2145 luaV_emsg(L);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02002146 }
2147 if (script != NULL) vim_free(script);
2148}
2149
Bram Moolenaar55d5c032010-07-17 23:52:29 +02002150 void
2151ex_luado(exarg_T *eap)
2152{
Bram Moolenaar0ba04292010-07-14 23:23:17 +02002153 linenr_T l;
2154 const char *s = (const char *) eap->arg;
2155 luaL_Buffer b;
2156 size_t len;
Bram Moolenaard58f03b2017-01-29 22:48:45 +01002157 buf_T *was_curbuf = curbuf;
2158
Bram Moolenaar0ba04292010-07-14 23:23:17 +02002159 if (lua_init() == FAIL) return;
Bram Moolenaar55d5c032010-07-17 23:52:29 +02002160 if (u_save(eap->line1 - 1, eap->line2 + 1) == FAIL)
2161 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002162 emsg(_("cannot save undo information"));
Bram Moolenaar55d5c032010-07-17 23:52:29 +02002163 return;
Bram Moolenaar0ba04292010-07-14 23:23:17 +02002164 }
2165 luaV_setrange(L, eap->line1, eap->line2);
2166 luaL_buffinit(L, &b);
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002167 luaL_addlstring(&b, "return function(line, linenr) ", 30); // header
Bram Moolenaar0ba04292010-07-14 23:23:17 +02002168 luaL_addlstring(&b, s, strlen(s));
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002169 luaL_addlstring(&b, " end", 4); // footer
Bram Moolenaar0ba04292010-07-14 23:23:17 +02002170 luaL_pushresult(&b);
2171 s = lua_tolstring(L, -1, &len);
Bram Moolenaar55d5c032010-07-17 23:52:29 +02002172 if (luaL_loadbuffer(L, s, len, LUAVIM_CHUNKNAME))
2173 {
2174 luaV_emsg(L);
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002175 lua_pop(L, 1); // function body
Bram Moolenaar55d5c032010-07-17 23:52:29 +02002176 return;
Bram Moolenaar0ba04292010-07-14 23:23:17 +02002177 }
2178 lua_call(L, 0, 1);
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002179 lua_replace(L, -2); // function -> body
Bram Moolenaar55d5c032010-07-17 23:52:29 +02002180 for (l = eap->line1; l <= eap->line2; l++)
2181 {
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002182 // Check the line number, the command my have deleted lines.
Bram Moolenaard58f03b2017-01-29 22:48:45 +01002183 if (l > curbuf->b_ml.ml_line_count)
2184 break;
2185
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002186 lua_pushvalue(L, -1); // function
2187 luaV_pushline(L, curbuf, l); // current line as arg
2188 lua_pushinteger(L, l); // current line number as arg
Bram Moolenaarbd2f3c32012-04-06 14:31:00 +02002189 if (lua_pcall(L, 2, 1, 0))
Bram Moolenaar55d5c032010-07-17 23:52:29 +02002190 {
2191 luaV_emsg(L);
2192 break;
2193 }
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002194 // Catch the command switching to another buffer.
Bram Moolenaard58f03b2017-01-29 22:48:45 +01002195 if (curbuf != was_curbuf)
2196 break;
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002197 if (lua_isstring(L, -1)) // update line?
Bram Moolenaar55d5c032010-07-17 23:52:29 +02002198 {
Bram Moolenaar0ba04292010-07-14 23:23:17 +02002199#ifdef HAVE_SANDBOX
Bram Moolenaar55d5c032010-07-17 23:52:29 +02002200 luaV_checksandbox(L);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02002201#endif
Bram Moolenaar55d5c032010-07-17 23:52:29 +02002202 ml_replace(l, luaV_toline(L, -1), TRUE);
2203 changed_bytes(l, 0);
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002204 lua_pop(L, 1); // result from luaV_toline
Bram Moolenaar55d5c032010-07-17 23:52:29 +02002205 }
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002206 lua_pop(L, 1); // line
Bram Moolenaar0ba04292010-07-14 23:23:17 +02002207 }
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002208 lua_pop(L, 1); // function
Bram Moolenaar0ba04292010-07-14 23:23:17 +02002209 check_cursor();
2210 update_screen(NOT_VALID);
2211}
2212
Bram Moolenaar55d5c032010-07-17 23:52:29 +02002213 void
2214ex_luafile(exarg_T *eap)
2215{
2216 if (lua_init() == FAIL)
2217 return;
2218 if (!eap->skip)
2219 {
2220 luaV_setrange(L, eap->line1, eap->line2);
2221 if (luaL_loadfile(L, (char *) eap->arg) || lua_pcall(L, 0, 0, 0))
2222 luaV_emsg(L);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02002223 }
2224}
2225
Bram Moolenaar1dced572012-04-05 16:54:08 +02002226#define luaV_freetype(typ,tname) \
2227 void \
2228 lua_##tname##_free(typ *o) \
2229 { \
2230 if (!lua_isopen()) return; \
2231 luaV_getfield(L, LUAVIM_FREE); \
2232 lua_pushlightuserdata(L, (void *) o); \
2233 lua_call(L, 1, 0); \
2234 }
2235
2236luaV_freetype(buf_T, buffer)
2237luaV_freetype(win_T, window)
2238
Bram Moolenaar55d5c032010-07-17 23:52:29 +02002239 void
Bram Moolenaar4eefe472019-03-19 21:59:19 +01002240do_luaeval(char_u *str, typval_T *arg, typval_T *rettv)
Bram Moolenaar55d5c032010-07-17 23:52:29 +02002241{
Bram Moolenaar1dced572012-04-05 16:54:08 +02002242 lua_init();
2243 luaV_getfield(L, LUAVIM_LUAEVAL);
2244 lua_pushstring(L, (char *) str);
2245 lua_pushlightuserdata(L, (void *) arg);
2246 lua_pushlightuserdata(L, (void *) rettv);
2247 lua_call(L, 3, 0);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02002248}
2249
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01002250 int
Bram Moolenaar4eefe472019-03-19 21:59:19 +01002251set_ref_in_lua(int copyID)
Bram Moolenaar55d5c032010-07-17 23:52:29 +02002252{
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01002253 int aborted = 0;
2254
2255 if (lua_isopen())
2256 {
2257 luaV_getfield(L, LUAVIM_SETREF);
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002258 // call the function with 1 arg, getting 1 result back
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01002259 lua_pushinteger(L, copyID);
2260 lua_call(L, 1, 1);
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002261 // get the result
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01002262 aborted = lua_tointeger(L, -1);
Bram Moolenaar2ab2e862019-12-04 21:24:53 +01002263 // pop result off the stack
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01002264 lua_pop(L, 1);
2265 }
2266 return aborted;
Bram Moolenaar0ba04292010-07-14 23:23:17 +02002267}
2268
2269#endif