blob: ab0bc5dc59def40c0681b16657aa4db98daa787d [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
18/* Only do the following when the feature is enabled. Needed for "make
19 * depend". */
20#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 Moolenaar0ba04292010-07-14 23:23:17 +020031typedef void (*msgfunc_T)(char_u *);
32
Bram Moolenaar1dced572012-04-05 16:54:08 +020033static const char LUAVIM_DICT[] = "dict";
34static const char LUAVIM_LIST[] = "list";
Bram Moolenaar0ba04292010-07-14 23:23:17 +020035static const char LUAVIM_BUFFER[] = "buffer";
36static const char LUAVIM_WINDOW[] = "window";
37static const char LUAVIM_FREE[] = "luaV_free";
Bram Moolenaar1dced572012-04-05 16:54:08 +020038static const char LUAVIM_LUAEVAL[] = "luaV_luaeval";
39static const char LUAVIM_SETREF[] = "luaV_setref";
Bram Moolenaar0ba04292010-07-14 23:23:17 +020040
Bram Moolenaar1dced572012-04-05 16:54:08 +020041/* most functions are closures with a cache table as first upvalue;
42 * get/setudata manage references to vim userdata in cache table through
43 * object pointers (light userdata) */
44#define luaV_getudata(L, v) \
45 lua_pushlightuserdata((L), (void *) (v)); \
46 lua_rawget((L), lua_upvalueindex(1))
47#define luaV_setudata(L, v) \
48 lua_pushlightuserdata((L), (void *) (v)); \
49 lua_pushvalue((L), -2); \
50 lua_rawset((L), lua_upvalueindex(1))
Bram Moolenaar0ba04292010-07-14 23:23:17 +020051#define luaV_getfield(L, s) \
52 lua_pushlightuserdata((L), (void *)(s)); \
53 lua_rawget((L), LUA_REGISTRYINDEX)
54#define luaV_checksandbox(L) \
55 if (sandbox) luaL_error((L), "not allowed in sandbox")
56#define luaV_msg(L) luaV_msgfunc((L), (msgfunc_T) msg)
57#define luaV_emsg(L) luaV_msgfunc((L), (msgfunc_T) emsg)
58
Bram Moolenaar1dced572012-04-05 16:54:08 +020059static luaV_List *luaV_pushlist (lua_State *L, list_T *lis);
60static luaV_Dict *luaV_pushdict (lua_State *L, dict_T *dic);
61
62#if LUA_VERSION_NUM <= 501
63#define luaV_openlib(L, l, n) luaL_openlib(L, NULL, l, n)
64#define luaL_typeerror luaL_typerror
65#else
66#define luaV_openlib luaL_setfuncs
67#endif
Bram Moolenaar0ba04292010-07-14 23:23:17 +020068
69#ifdef DYNAMIC_LUA
Bram Moolenaar2334b6d2010-07-22 21:32:16 +020070
71#ifndef WIN3264
72# include <dlfcn.h>
73# define HANDLE void*
74# define load_dll(n) dlopen((n), RTLD_LAZY|RTLD_GLOBAL)
75# define symbol_from_dll dlsym
76# define close_dll dlclose
77#else
Bram Moolenaarebbcb822010-10-23 14:02:54 +020078# define load_dll vimLoadLib
Bram Moolenaar2334b6d2010-07-22 21:32:16 +020079# define symbol_from_dll GetProcAddress
80# define close_dll FreeLibrary
81#endif
82
Bram Moolenaar0ba04292010-07-14 23:23:17 +020083/* lauxlib */
Bram Moolenaar1dced572012-04-05 16:54:08 +020084#if LUA_VERSION_NUM <= 501
Bram Moolenaar0ba04292010-07-14 23:23:17 +020085#define luaL_register dll_luaL_register
Bram Moolenaar1dced572012-04-05 16:54:08 +020086#define luaL_prepbuffer dll_luaL_prepbuffer
87#define luaL_openlib dll_luaL_openlib
Bram Moolenaar0ba04292010-07-14 23:23:17 +020088#define luaL_typerror dll_luaL_typerror
Bram Moolenaar1dced572012-04-05 16:54:08 +020089#define luaL_loadfile dll_luaL_loadfile
90#define luaL_loadbuffer dll_luaL_loadbuffer
91#else
92#define luaL_prepbuffsize dll_luaL_prepbuffsize
93#define luaL_setfuncs dll_luaL_setfuncs
94#define luaL_loadfilex dll_luaL_loadfilex
95#define luaL_loadbufferx dll_luaL_loadbufferx
96#define luaL_argerror dll_luaL_argerror
97#endif
Bram Moolenaarbd2f3c32012-04-06 14:31:00 +020098#define luaL_checkany dll_luaL_checkany
Bram Moolenaar0ba04292010-07-14 23:23:17 +020099#define luaL_checklstring dll_luaL_checklstring
100#define luaL_checkinteger dll_luaL_checkinteger
101#define luaL_optinteger dll_luaL_optinteger
102#define luaL_checktype dll_luaL_checktype
103#define luaL_error dll_luaL_error
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200104#define luaL_newstate dll_luaL_newstate
105#define luaL_buffinit dll_luaL_buffinit
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200106#define luaL_addlstring dll_luaL_addlstring
107#define luaL_pushresult dll_luaL_pushresult
108/* lua */
Bram Moolenaar1dced572012-04-05 16:54:08 +0200109#if LUA_VERSION_NUM <= 501
110#define lua_tonumber dll_lua_tonumber
111#define lua_tointeger dll_lua_tointeger
112#define lua_call dll_lua_call
113#define lua_pcall dll_lua_pcall
Bram Moolenaar9514b1f2015-06-25 18:27:32 +0200114
115#elif LUA_VERSION_NUM <= 502
116#define lua_replace dll_lua_replace
117#define lua_remove dll_lua_remove
Bram Moolenaar1dced572012-04-05 16:54:08 +0200118#else
Bram Moolenaar9514b1f2015-06-25 18:27:32 +0200119#define lua_rotate dll_lua_rotate
Bram Moolenaar1dced572012-04-05 16:54:08 +0200120#define lua_tonumberx dll_lua_tonumberx
121#define lua_tointegerx dll_lua_tointegerx
122#define lua_callk dll_lua_callk
123#define lua_pcallk dll_lua_pcallk
124#define lua_getglobal dll_lua_getglobal
125#define lua_setglobal dll_lua_setglobal
Bram Moolenaar1dced572012-04-05 16:54:08 +0200126#endif
Bram Moolenaarbd2f3c32012-04-06 14:31:00 +0200127#define lua_typename dll_lua_typename
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200128#define lua_close dll_lua_close
129#define lua_gettop dll_lua_gettop
130#define lua_settop dll_lua_settop
131#define lua_pushvalue dll_lua_pushvalue
Bram Moolenaar9514b1f2015-06-25 18:27:32 +0200132#define lua_copy dll_lua_copy
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200133#define lua_isnumber dll_lua_isnumber
134#define lua_isstring dll_lua_isstring
135#define lua_type dll_lua_type
136#define lua_rawequal dll_lua_rawequal
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200137#define lua_toboolean dll_lua_toboolean
138#define lua_tolstring dll_lua_tolstring
139#define lua_touserdata dll_lua_touserdata
140#define lua_pushnil dll_lua_pushnil
141#define lua_pushnumber dll_lua_pushnumber
142#define lua_pushinteger dll_lua_pushinteger
143#define lua_pushlstring dll_lua_pushlstring
144#define lua_pushstring dll_lua_pushstring
145#define lua_pushfstring dll_lua_pushfstring
146#define lua_pushcclosure dll_lua_pushcclosure
147#define lua_pushboolean dll_lua_pushboolean
148#define lua_pushlightuserdata dll_lua_pushlightuserdata
149#define lua_getfield dll_lua_getfield
150#define lua_rawget dll_lua_rawget
Bram Moolenaar1dced572012-04-05 16:54:08 +0200151#define lua_rawgeti dll_lua_rawgeti
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200152#define lua_createtable dll_lua_createtable
153#define lua_newuserdata dll_lua_newuserdata
154#define lua_getmetatable dll_lua_getmetatable
155#define lua_setfield dll_lua_setfield
156#define lua_rawset dll_lua_rawset
157#define lua_rawseti dll_lua_rawseti
158#define lua_setmetatable dll_lua_setmetatable
Bram Moolenaarbd2f3c32012-04-06 14:31:00 +0200159#define lua_next dll_lua_next
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200160/* libs */
161#define luaopen_base dll_luaopen_base
162#define luaopen_table dll_luaopen_table
163#define luaopen_string dll_luaopen_string
164#define luaopen_math dll_luaopen_math
Bram Moolenaar16c98f92010-07-28 22:46:08 +0200165#define luaopen_io dll_luaopen_io
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200166#define luaopen_os dll_luaopen_os
167#define luaopen_package dll_luaopen_package
168#define luaopen_debug dll_luaopen_debug
Bram Moolenaar16c98f92010-07-28 22:46:08 +0200169#define luaL_openlibs dll_luaL_openlibs
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200170
171/* lauxlib */
Bram Moolenaar1dced572012-04-05 16:54:08 +0200172#if LUA_VERSION_NUM <= 501
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200173void (*dll_luaL_register) (lua_State *L, const char *libname, const luaL_Reg *l);
Bram Moolenaar1dced572012-04-05 16:54:08 +0200174char *(*dll_luaL_prepbuffer) (luaL_Buffer *B);
175void (*dll_luaL_openlib) (lua_State *L, const char *libname, const luaL_Reg *l, int nup);
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200176int (*dll_luaL_typerror) (lua_State *L, int narg, const char *tname);
Bram Moolenaar1dced572012-04-05 16:54:08 +0200177int (*dll_luaL_loadfile) (lua_State *L, const char *filename);
178int (*dll_luaL_loadbuffer) (lua_State *L, const char *buff, size_t sz, const char *name);
179#else
180char *(*dll_luaL_prepbuffsize) (luaL_Buffer *B, size_t sz);
181void (*dll_luaL_setfuncs) (lua_State *L, const luaL_Reg *l, int nup);
182int (*dll_luaL_loadfilex) (lua_State *L, const char *filename, const char *mode);
183int (*dll_luaL_loadbufferx) (lua_State *L, const char *buff, size_t sz, const char *name, const char *mode);
184int (*dll_luaL_argerror) (lua_State *L, int numarg, const char *extramsg);
185#endif
Bram Moolenaarbd2f3c32012-04-06 14:31:00 +0200186void (*dll_luaL_checkany) (lua_State *L, int narg);
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200187const char *(*dll_luaL_checklstring) (lua_State *L, int numArg, size_t *l);
188lua_Integer (*dll_luaL_checkinteger) (lua_State *L, int numArg);
189lua_Integer (*dll_luaL_optinteger) (lua_State *L, int nArg, lua_Integer def);
190void (*dll_luaL_checktype) (lua_State *L, int narg, int t);
191int (*dll_luaL_error) (lua_State *L, const char *fmt, ...);
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200192lua_State *(*dll_luaL_newstate) (void);
193void (*dll_luaL_buffinit) (lua_State *L, luaL_Buffer *B);
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200194void (*dll_luaL_addlstring) (luaL_Buffer *B, const char *s, size_t l);
195void (*dll_luaL_pushresult) (luaL_Buffer *B);
196/* lua */
Bram Moolenaar1dced572012-04-05 16:54:08 +0200197#if LUA_VERSION_NUM <= 501
198lua_Number (*dll_lua_tonumber) (lua_State *L, int idx);
199lua_Integer (*dll_lua_tointeger) (lua_State *L, int idx);
200void (*dll_lua_call) (lua_State *L, int nargs, int nresults);
201int (*dll_lua_pcall) (lua_State *L, int nargs, int nresults, int errfunc);
Bram Moolenaar9514b1f2015-06-25 18:27:32 +0200202#elif LUA_VERSION_NUM <= 502
203void (*dll_lua_replace) (lua_State *L, int idx);
204void (*dll_lua_remove) (lua_State *L, int idx);
Bram Moolenaar1dced572012-04-05 16:54:08 +0200205#else
Bram Moolenaar9514b1f2015-06-25 18:27:32 +0200206
207void (*dll_lua_rotate) (lua_State *L, int idx, int n);
Bram Moolenaar1dced572012-04-05 16:54:08 +0200208lua_Number (*dll_lua_tonumberx) (lua_State *L, int idx, int *isnum);
209lua_Integer (*dll_lua_tointegerx) (lua_State *L, int idx, int *isnum);
210void (*dll_lua_callk) (lua_State *L, int nargs, int nresults, int ctx,
Bram Moolenaardb913952012-06-29 12:54:53 +0200211 lua_CFunction k);
Bram Moolenaar1dced572012-04-05 16:54:08 +0200212int (*dll_lua_pcallk) (lua_State *L, int nargs, int nresults, int errfunc,
Bram Moolenaardb913952012-06-29 12:54:53 +0200213 int ctx, lua_CFunction k);
Bram Moolenaar1dced572012-04-05 16:54:08 +0200214void (*dll_lua_getglobal) (lua_State *L, const char *var);
215void (*dll_lua_setglobal) (lua_State *L, const char *var);
Bram Moolenaar9514b1f2015-06-25 18:27:32 +0200216void (*dll_lua_copy) (lua_State *L, int fromidx, int toidx);
Bram Moolenaar1dced572012-04-05 16:54:08 +0200217#endif
Bram Moolenaarbd2f3c32012-04-06 14:31:00 +0200218const char *(*dll_lua_typename) (lua_State *L, int tp);
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200219void (*dll_lua_close) (lua_State *L);
220int (*dll_lua_gettop) (lua_State *L);
221void (*dll_lua_settop) (lua_State *L, int idx);
222void (*dll_lua_pushvalue) (lua_State *L, int idx);
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200223int (*dll_lua_isnumber) (lua_State *L, int idx);
224int (*dll_lua_isstring) (lua_State *L, int idx);
225int (*dll_lua_type) (lua_State *L, int idx);
226int (*dll_lua_rawequal) (lua_State *L, int idx1, int idx2);
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200227int (*dll_lua_toboolean) (lua_State *L, int idx);
228const char *(*dll_lua_tolstring) (lua_State *L, int idx, size_t *len);
229void *(*dll_lua_touserdata) (lua_State *L, int idx);
230void (*dll_lua_pushnil) (lua_State *L);
231void (*dll_lua_pushnumber) (lua_State *L, lua_Number n);
232void (*dll_lua_pushinteger) (lua_State *L, lua_Integer n);
233void (*dll_lua_pushlstring) (lua_State *L, const char *s, size_t l);
234void (*dll_lua_pushstring) (lua_State *L, const char *s);
235const char *(*dll_lua_pushfstring) (lua_State *L, const char *fmt, ...);
236void (*dll_lua_pushcclosure) (lua_State *L, lua_CFunction fn, int n);
237void (*dll_lua_pushboolean) (lua_State *L, int b);
238void (*dll_lua_pushlightuserdata) (lua_State *L, void *p);
239void (*dll_lua_getfield) (lua_State *L, int idx, const char *k);
240void (*dll_lua_rawget) (lua_State *L, int idx);
Bram Moolenaar1dced572012-04-05 16:54:08 +0200241void (*dll_lua_rawgeti) (lua_State *L, int idx, int n);
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200242void (*dll_lua_createtable) (lua_State *L, int narr, int nrec);
243void *(*dll_lua_newuserdata) (lua_State *L, size_t sz);
244int (*dll_lua_getmetatable) (lua_State *L, int objindex);
245void (*dll_lua_setfield) (lua_State *L, int idx, const char *k);
246void (*dll_lua_rawset) (lua_State *L, int idx);
247void (*dll_lua_rawseti) (lua_State *L, int idx, int n);
248int (*dll_lua_setmetatable) (lua_State *L, int objindex);
Bram Moolenaarbd2f3c32012-04-06 14:31:00 +0200249int (*dll_lua_next) (lua_State *L, int idx);
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200250/* libs */
251int (*dll_luaopen_base) (lua_State *L);
252int (*dll_luaopen_table) (lua_State *L);
253int (*dll_luaopen_string) (lua_State *L);
254int (*dll_luaopen_math) (lua_State *L);
Bram Moolenaar16c98f92010-07-28 22:46:08 +0200255int (*dll_luaopen_io) (lua_State *L);
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200256int (*dll_luaopen_os) (lua_State *L);
257int (*dll_luaopen_package) (lua_State *L);
258int (*dll_luaopen_debug) (lua_State *L);
Bram Moolenaar16c98f92010-07-28 22:46:08 +0200259void (*dll_luaL_openlibs) (lua_State *L);
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200260
261typedef void **luaV_function;
262typedef struct {
263 const char *name;
264 luaV_function func;
265} luaV_Reg;
266
267static const luaV_Reg luaV_dll[] = {
268 /* lauxlib */
Bram Moolenaar1dced572012-04-05 16:54:08 +0200269#if LUA_VERSION_NUM <= 501
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200270 {"luaL_register", (luaV_function) &dll_luaL_register},
Bram Moolenaar1dced572012-04-05 16:54:08 +0200271 {"luaL_prepbuffer", (luaV_function) &dll_luaL_prepbuffer},
272 {"luaL_openlib", (luaV_function) &dll_luaL_openlib},
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200273 {"luaL_typerror", (luaV_function) &dll_luaL_typerror},
Bram Moolenaar1dced572012-04-05 16:54:08 +0200274 {"luaL_loadfile", (luaV_function) &dll_luaL_loadfile},
275 {"luaL_loadbuffer", (luaV_function) &dll_luaL_loadbuffer},
276#else
277 {"luaL_prepbuffsize", (luaV_function) &dll_luaL_prepbuffsize},
278 {"luaL_setfuncs", (luaV_function) &dll_luaL_setfuncs},
279 {"luaL_loadfilex", (luaV_function) &dll_luaL_loadfilex},
280 {"luaL_loadbufferx", (luaV_function) &dll_luaL_loadbufferx},
281 {"luaL_argerror", (luaV_function) &dll_luaL_argerror},
282#endif
Bram Moolenaarbd2f3c32012-04-06 14:31:00 +0200283 {"luaL_checkany", (luaV_function) &dll_luaL_checkany},
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200284 {"luaL_checklstring", (luaV_function) &dll_luaL_checklstring},
285 {"luaL_checkinteger", (luaV_function) &dll_luaL_checkinteger},
286 {"luaL_optinteger", (luaV_function) &dll_luaL_optinteger},
287 {"luaL_checktype", (luaV_function) &dll_luaL_checktype},
288 {"luaL_error", (luaV_function) &dll_luaL_error},
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200289 {"luaL_newstate", (luaV_function) &dll_luaL_newstate},
290 {"luaL_buffinit", (luaV_function) &dll_luaL_buffinit},
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200291 {"luaL_addlstring", (luaV_function) &dll_luaL_addlstring},
292 {"luaL_pushresult", (luaV_function) &dll_luaL_pushresult},
293 /* lua */
Bram Moolenaar1dced572012-04-05 16:54:08 +0200294#if LUA_VERSION_NUM <= 501
295 {"lua_tonumber", (luaV_function) &dll_lua_tonumber},
296 {"lua_tointeger", (luaV_function) &dll_lua_tointeger},
297 {"lua_call", (luaV_function) &dll_lua_call},
298 {"lua_pcall", (luaV_function) &dll_lua_pcall},
Bram Moolenaar9514b1f2015-06-25 18:27:32 +0200299#elif LUA_VERSION_NUM <= 502
300 {"lua_replace", (luaV_function) &dll_lua_replace},
301 {"lua_remove", (luaV_function) &dll_lua_remove},
Bram Moolenaar1dced572012-04-05 16:54:08 +0200302#else
Bram Moolenaar9514b1f2015-06-25 18:27:32 +0200303 {"lua_rotate", (luaV_function) &dll_lua_rotate},
304 {"lua_copy", (luaV_function) &dll_lua_copy},
Bram Moolenaar1dced572012-04-05 16:54:08 +0200305 {"lua_tonumberx", (luaV_function) &dll_lua_tonumberx},
306 {"lua_tointegerx", (luaV_function) &dll_lua_tointegerx},
307 {"lua_callk", (luaV_function) &dll_lua_callk},
308 {"lua_pcallk", (luaV_function) &dll_lua_pcallk},
309 {"lua_getglobal", (luaV_function) &dll_lua_getglobal},
310 {"lua_setglobal", (luaV_function) &dll_lua_setglobal},
Bram Moolenaar1dced572012-04-05 16:54:08 +0200311#endif
Bram Moolenaarbd2f3c32012-04-06 14:31:00 +0200312 {"lua_typename", (luaV_function) &dll_lua_typename},
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200313 {"lua_close", (luaV_function) &dll_lua_close},
314 {"lua_gettop", (luaV_function) &dll_lua_gettop},
315 {"lua_settop", (luaV_function) &dll_lua_settop},
316 {"lua_pushvalue", (luaV_function) &dll_lua_pushvalue},
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200317 {"lua_isnumber", (luaV_function) &dll_lua_isnumber},
318 {"lua_isstring", (luaV_function) &dll_lua_isstring},
319 {"lua_type", (luaV_function) &dll_lua_type},
320 {"lua_rawequal", (luaV_function) &dll_lua_rawequal},
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200321 {"lua_toboolean", (luaV_function) &dll_lua_toboolean},
322 {"lua_tolstring", (luaV_function) &dll_lua_tolstring},
323 {"lua_touserdata", (luaV_function) &dll_lua_touserdata},
324 {"lua_pushnil", (luaV_function) &dll_lua_pushnil},
325 {"lua_pushnumber", (luaV_function) &dll_lua_pushnumber},
326 {"lua_pushinteger", (luaV_function) &dll_lua_pushinteger},
327 {"lua_pushlstring", (luaV_function) &dll_lua_pushlstring},
328 {"lua_pushstring", (luaV_function) &dll_lua_pushstring},
329 {"lua_pushfstring", (luaV_function) &dll_lua_pushfstring},
330 {"lua_pushcclosure", (luaV_function) &dll_lua_pushcclosure},
331 {"lua_pushboolean", (luaV_function) &dll_lua_pushboolean},
332 {"lua_pushlightuserdata", (luaV_function) &dll_lua_pushlightuserdata},
333 {"lua_getfield", (luaV_function) &dll_lua_getfield},
334 {"lua_rawget", (luaV_function) &dll_lua_rawget},
Bram Moolenaar1dced572012-04-05 16:54:08 +0200335 {"lua_rawgeti", (luaV_function) &dll_lua_rawgeti},
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200336 {"lua_createtable", (luaV_function) &dll_lua_createtable},
337 {"lua_newuserdata", (luaV_function) &dll_lua_newuserdata},
338 {"lua_getmetatable", (luaV_function) &dll_lua_getmetatable},
339 {"lua_setfield", (luaV_function) &dll_lua_setfield},
340 {"lua_rawset", (luaV_function) &dll_lua_rawset},
341 {"lua_rawseti", (luaV_function) &dll_lua_rawseti},
342 {"lua_setmetatable", (luaV_function) &dll_lua_setmetatable},
Bram Moolenaarbd2f3c32012-04-06 14:31:00 +0200343 {"lua_next", (luaV_function) &dll_lua_next},
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200344 /* libs */
345 {"luaopen_base", (luaV_function) &dll_luaopen_base},
346 {"luaopen_table", (luaV_function) &dll_luaopen_table},
347 {"luaopen_string", (luaV_function) &dll_luaopen_string},
348 {"luaopen_math", (luaV_function) &dll_luaopen_math},
Bram Moolenaar16c98f92010-07-28 22:46:08 +0200349 {"luaopen_io", (luaV_function) &dll_luaopen_io},
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200350 {"luaopen_os", (luaV_function) &dll_luaopen_os},
351 {"luaopen_package", (luaV_function) &dll_luaopen_package},
352 {"luaopen_debug", (luaV_function) &dll_luaopen_debug},
Bram Moolenaar16c98f92010-07-28 22:46:08 +0200353 {"luaL_openlibs", (luaV_function) &dll_luaL_openlibs},
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200354 {NULL, NULL}
355};
356
Bram Moolenaar2334b6d2010-07-22 21:32:16 +0200357static HANDLE hinstLua = NULL;
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200358
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200359 static void
360end_dynamic_lua(void)
361{
362 if (hinstLua)
363 {
Bram Moolenaar2334b6d2010-07-22 21:32:16 +0200364 close_dll(hinstLua);
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200365 hinstLua = 0;
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200366 }
367}
368
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200369 static int
370lua_link_init(char *libname, int verbose)
371{
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200372 const luaV_Reg *reg;
373 if (hinstLua) return OK;
Bram Moolenaar2334b6d2010-07-22 21:32:16 +0200374 hinstLua = load_dll(libname);
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200375 if (!hinstLua)
376 {
377 if (verbose)
378 EMSG2(_(e_loadlib), libname);
379 return FAIL;
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200380 }
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200381 for (reg = luaV_dll; reg->func; reg++)
382 {
Bram Moolenaar2334b6d2010-07-22 21:32:16 +0200383 if ((*reg->func = symbol_from_dll(hinstLua, reg->name)) == NULL)
384 {
385 close_dll(hinstLua);
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200386 hinstLua = 0;
387 if (verbose)
388 EMSG2(_(e_loadfunc), reg->name);
389 return FAIL;
390 }
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200391 }
392 return OK;
393}
394
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200395 int
396lua_enabled(int verbose)
397{
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200398 return lua_link_init(DYNAMIC_LUA_DLL, verbose) == OK;
399}
400
401#endif /* DYNAMIC_LUA */
402
Bram Moolenaar1dced572012-04-05 16:54:08 +0200403#if LUA_VERSION_NUM > 501
404 static int
405luaL_typeerror (lua_State *L, int narg, const char *tname)
406{
407 const char *msg = lua_pushfstring(L, "%s expected, got %s",
Bram Moolenaardb913952012-06-29 12:54:53 +0200408 tname, luaL_typename(L, narg));
Bram Moolenaar1dced572012-04-05 16:54:08 +0200409 return luaL_argerror(L, narg, msg);
410}
411#endif
412
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200413
414/* ======= Internal ======= */
415
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200416 static void
417luaV_newmetatable(lua_State *L, const char *tname)
418{
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200419 lua_newtable(L);
420 lua_pushlightuserdata(L, (void *) tname);
421 lua_pushvalue(L, -2);
422 lua_rawset(L, LUA_REGISTRYINDEX);
423}
424
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200425 static void *
426luaV_toudata(lua_State *L, int ud, const char *tname)
427{
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200428 void *p = lua_touserdata(L, ud);
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200429
430 if (p != NULL) /* value is userdata? */
431 {
432 if (lua_getmetatable(L, ud)) /* does it have a metatable? */
433 {
434 luaV_getfield(L, tname); /* get metatable */
435 if (lua_rawequal(L, -1, -2)) /* MTs match? */
436 {
437 lua_pop(L, 2); /* MTs */
438 return p;
439 }
440 }
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200441 }
442 return NULL;
443}
444
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200445 static void *
Bram Moolenaar1dced572012-04-05 16:54:08 +0200446luaV_checkcache(lua_State *L, void *p)
447{
448 luaV_getudata(L, p);
449 if (lua_isnil(L, -1)) luaL_error(L, "invalid object");
450 lua_pop(L, 1);
451 return p;
452}
453
454#define luaV_unbox(L,luatyp,ud) (*((luatyp *) lua_touserdata((L),(ud))))
455
456#define luaV_checkvalid(L,luatyp,ud) \
457 luaV_checkcache((L), (void *) luaV_unbox((L),luatyp,(ud)))
458
459 static void *
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200460luaV_checkudata(lua_State *L, int ud, const char *tname)
461{
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200462 void *p = luaV_toudata(L, ud, tname);
Bram Moolenaar1dced572012-04-05 16:54:08 +0200463 if (p == NULL) luaL_typeerror(L, ud, tname);
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200464 return p;
465}
466
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200467 static void
468luaV_pushtypval(lua_State *L, typval_T *tv)
469{
Bram Moolenaar1dced572012-04-05 16:54:08 +0200470 if (tv == NULL)
471 {
472 lua_pushnil(L);
473 return;
474 }
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200475 switch (tv->v_type)
476 {
477 case VAR_STRING:
Bram Moolenaard04da7c2012-10-14 03:41:59 +0200478 lua_pushstring(L, tv->vval.v_string == NULL
479 ? "" : (char *)tv->vval.v_string);
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200480 break;
481 case VAR_NUMBER:
482 lua_pushinteger(L, (int) tv->vval.v_number);
483 break;
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200484#ifdef FEAT_FLOAT
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200485 case VAR_FLOAT:
486 lua_pushnumber(L, (lua_Number) tv->vval.v_float);
487 break;
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200488#endif
Bram Moolenaar1dced572012-04-05 16:54:08 +0200489 case VAR_LIST:
490 luaV_pushlist(L, tv->vval.v_list);
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200491 break;
Bram Moolenaar1dced572012-04-05 16:54:08 +0200492 case VAR_DICT:
493 luaV_pushdict(L, tv->vval.v_dict);
494 break;
495 default:
496 lua_pushnil(L);
497 }
498}
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200499
Bram Moolenaar1dced572012-04-05 16:54:08 +0200500/* converts lua value at 'pos' to typval 'tv' */
501 static void
502luaV_totypval (lua_State *L, int pos, typval_T *tv)
503{
504 switch(lua_type(L, pos)) {
505 case LUA_TBOOLEAN:
506 tv->v_type = VAR_NUMBER;
507 tv->vval.v_number = (varnumber_T) lua_toboolean(L, pos);
508 break;
509 case LUA_TSTRING:
510 tv->v_type = VAR_STRING;
511 tv->vval.v_string = vim_strsave((char_u *) lua_tostring(L, pos));
512 break;
513 case LUA_TNUMBER:
514#ifdef FEAT_FLOAT
515 tv->v_type = VAR_FLOAT;
516 tv->vval.v_float = (float_T) lua_tonumber(L, pos);
517#else
518 tv->v_type = VAR_NUMBER;
519 tv->vval.v_number = (varnumber_T) lua_tointeger(L, pos);
520#endif
521 break;
522 case LUA_TUSERDATA: {
523 void *p = lua_touserdata(L, pos);
524 if (lua_getmetatable(L, pos)) /* has metatable? */
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200525 {
Bram Moolenaar1dced572012-04-05 16:54:08 +0200526 /* check list */
527 luaV_getfield(L, LUAVIM_LIST);
528 if (lua_rawequal(L, -1, -2))
Bram Moolenaar2334b6d2010-07-22 21:32:16 +0200529 {
Bram Moolenaar1dced572012-04-05 16:54:08 +0200530 tv->v_type = VAR_LIST;
531 tv->vval.v_list = *((luaV_List *) p);
532 ++tv->vval.v_list->lv_refcount;
533 lua_pop(L, 2); /* MTs */
534 return;
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200535 }
Bram Moolenaar1dced572012-04-05 16:54:08 +0200536 /* check dict */
537 luaV_getfield(L, LUAVIM_DICT);
538 if (lua_rawequal(L, -1, -3))
539 {
540 tv->v_type = VAR_DICT;
541 tv->vval.v_dict = *((luaV_Dict *) p);
542 ++tv->vval.v_dict->dv_refcount;
543 lua_pop(L, 3); /* MTs */
544 return;
545 }
546 lua_pop(L, 3); /* MTs */
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200547 }
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200548 break;
549 }
550 default:
Bram Moolenaar1dced572012-04-05 16:54:08 +0200551 tv->v_type = VAR_NUMBER;
552 tv->vval.v_number = 0;
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200553 }
554}
555
556/* similar to luaL_addlstring, but replaces \0 with \n if toline and
557 * \n with \0 otherwise */
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200558 static void
559luaV_addlstring(luaL_Buffer *b, const char *s, size_t l, int toline)
560{
561 while (l--)
562 {
563 if (*s == '\0' && toline)
564 luaL_addchar(b, '\n');
565 else if (*s == '\n' && !toline)
566 luaL_addchar(b, '\0');
567 else
568 luaL_addchar(b, *s);
569 s++;
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200570 }
571}
572
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200573 static void
574luaV_pushline(lua_State *L, buf_T *buf, linenr_T n)
575{
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200576 const char *s = (const char *) ml_get_buf(buf, n, FALSE);
577 luaL_Buffer b;
578 luaL_buffinit(L, &b);
579 luaV_addlstring(&b, s, strlen(s), 0);
580 luaL_pushresult(&b);
581}
582
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200583 static char_u *
584luaV_toline(lua_State *L, int pos)
585{
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200586 size_t l;
587 const char *s = lua_tolstring(L, pos, &l);
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200588
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200589 luaL_Buffer b;
590 luaL_buffinit(L, &b);
591 luaV_addlstring(&b, s, l, 1);
592 luaL_pushresult(&b);
593 return (char_u *) lua_tostring(L, -1);
594}
595
596/* pops a string s from the top of the stack and calls mf(t) for pieces t of
597 * s separated by newlines */
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200598 static void
599luaV_msgfunc(lua_State *L, msgfunc_T mf)
600{
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200601 luaL_Buffer b;
602 size_t l;
603 const char *p, *s = lua_tolstring(L, -1, &l);
604 luaL_buffinit(L, &b);
605 luaV_addlstring(&b, s, l, 0);
606 luaL_pushresult(&b);
607 /* break string */
608 p = s = lua_tolstring(L, -1, &l);
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200609 while (l--)
610 {
611 if (*p++ == '\0') /* break? */
612 {
613 mf((char_u *) s);
614 s = p;
615 }
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200616 }
617 mf((char_u *) s);
618 lua_pop(L, 2); /* original and modified strings */
619}
620
Bram Moolenaar1dced572012-04-05 16:54:08 +0200621#define luaV_newtype(typ,tname,luatyp,luatname) \
622 static luatyp * \
623 luaV_new##tname (lua_State *L, typ *obj) \
624 { \
625 luatyp *o = (luatyp *) lua_newuserdata(L, sizeof(luatyp)); \
626 *o = obj; \
627 luaV_setudata(L, obj); /* cache[obj] = udata */ \
628 luaV_getfield(L, luatname); \
629 lua_setmetatable(L, -2); \
630 return o; \
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200631 }
Bram Moolenaar1dced572012-04-05 16:54:08 +0200632
633#define luaV_pushtype(typ,tname,luatyp) \
634 static luatyp * \
635 luaV_push##tname (lua_State *L, typ *obj) \
636 { \
637 luatyp *o = NULL; \
638 if (obj == NULL) \
639 lua_pushnil(L); \
640 else { \
641 luaV_getudata(L, obj); \
642 if (lua_isnil(L, -1)) /* not interned? */ \
643 { \
644 lua_pop(L, 1); \
645 o = luaV_new##tname(L, obj); \
646 } \
647 else \
648 o = (luatyp *) lua_touserdata(L, -1); \
649 } \
650 return o; \
651 }
652
653#define luaV_type_tostring(tname,luatname) \
654 static int \
655 luaV_##tname##_tostring (lua_State *L) \
656 { \
657 lua_pushfstring(L, "%s: %p", luatname, lua_touserdata(L, 1)); \
658 return 1; \
659 }
660
Bram Moolenaar1dced572012-04-05 16:54:08 +0200661/* ======= List type ======= */
662
663 static luaV_List *
664luaV_newlist (lua_State *L, list_T *lis)
665{
666 luaV_List *l = (luaV_List *) lua_newuserdata(L, sizeof(luaV_List));
667 *l = lis;
668 lis->lv_refcount++; /* reference in Lua */
669 luaV_setudata(L, lis); /* cache[lis] = udata */
670 luaV_getfield(L, LUAVIM_LIST);
671 lua_setmetatable(L, -2);
672 return l;
673}
674
675luaV_pushtype(list_T, list, luaV_List)
676luaV_type_tostring(list, LUAVIM_LIST)
677
678 static int
Bram Moolenaar1dced572012-04-05 16:54:08 +0200679luaV_list_len (lua_State *L)
680{
681 list_T *l = luaV_unbox(L, luaV_List, 1);
682 lua_pushinteger(L, (l == NULL) ? 0 : (int) l->lv_len);
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200683 return 1;
684}
685
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200686 static int
Bram Moolenaar1dced572012-04-05 16:54:08 +0200687luaV_list_iter (lua_State *L)
688{
689 listitem_T *li = (listitem_T *) lua_touserdata(L, lua_upvalueindex(2));
690 if (li == NULL) return 0;
691 luaV_pushtypval(L, &li->li_tv);
692 lua_pushlightuserdata(L, (void *) li->li_next);
693 lua_replace(L, lua_upvalueindex(2));
694 return 1;
695}
696
697 static int
698luaV_list_call (lua_State *L)
699{
700 list_T *l = luaV_unbox(L, luaV_List, 1);
701 lua_pushvalue(L, lua_upvalueindex(1)); /* pass cache table along */
702 lua_pushlightuserdata(L, (void *) l->lv_first);
703 lua_pushcclosure(L, luaV_list_iter, 2);
704 return 1;
705}
706
707 static int
708luaV_list_index (lua_State *L)
709{
710 list_T *l = luaV_unbox(L, luaV_List, 1);
711 if (lua_isnumber(L, 2)) /* list item? */
712 {
713 listitem_T *li = list_find(l, (long) luaL_checkinteger(L, 2));
714 if (li == NULL)
715 lua_pushnil(L);
716 else
717 luaV_pushtypval(L, &li->li_tv);
718 }
719 else if (lua_isstring(L, 2)) /* method? */
720 {
721 const char *s = lua_tostring(L, 2);
722 if (strncmp(s, "add", 3) == 0
Bram Moolenaarb3766472013-04-15 13:49:21 +0200723 || strncmp(s, "insert", 6) == 0)
Bram Moolenaar1dced572012-04-05 16:54:08 +0200724 {
725 lua_getmetatable(L, 1);
726 lua_getfield(L, -1, s);
727 }
728 else
729 lua_pushnil(L);
730 }
731 else
732 lua_pushnil(L);
733 return 1;
734}
735
736 static int
737luaV_list_newindex (lua_State *L)
738{
739 list_T *l = luaV_unbox(L, luaV_List, 1);
740 long n = (long) luaL_checkinteger(L, 2);
741 listitem_T *li;
742 if (l->lv_lock)
743 luaL_error(L, "list is locked");
744 li = list_find(l, n);
745 if (li == NULL) return 0;
746 if (lua_isnil(L, 3)) /* remove? */
747 {
Bram Moolenaar3ec7f4e2014-05-07 17:31:37 +0200748 vimlist_remove(l, li, li);
Bram Moolenaar1dced572012-04-05 16:54:08 +0200749 clear_tv(&li->li_tv);
750 vim_free(li);
751 }
752 else
753 {
754 typval_T v;
755 luaV_totypval(L, 3, &v);
756 clear_tv(&li->li_tv);
757 copy_tv(&v, &li->li_tv);
Bram Moolenaarb3766472013-04-15 13:49:21 +0200758 clear_tv(&v);
Bram Moolenaar1dced572012-04-05 16:54:08 +0200759 }
760 return 0;
761}
762
763 static int
764luaV_list_add (lua_State *L)
765{
766 luaV_List *lis = luaV_checkudata(L, 1, LUAVIM_LIST);
767 list_T *l = (list_T *) luaV_checkcache(L, (void *) *lis);
Bram Moolenaarb3766472013-04-15 13:49:21 +0200768 typval_T v;
Bram Moolenaar1dced572012-04-05 16:54:08 +0200769 if (l->lv_lock)
770 luaL_error(L, "list is locked");
Bram Moolenaarb3766472013-04-15 13:49:21 +0200771 lua_settop(L, 2);
772 luaV_totypval(L, 2, &v);
773 if (list_append_tv(l, &v) == FAIL)
Bram Moolenaar1dced572012-04-05 16:54:08 +0200774 {
Bram Moolenaarb3766472013-04-15 13:49:21 +0200775 clear_tv(&v);
776 luaL_error(L, "Failed to add item to list");
Bram Moolenaar1dced572012-04-05 16:54:08 +0200777 }
Bram Moolenaarb3766472013-04-15 13:49:21 +0200778 clear_tv(&v);
Bram Moolenaar1dced572012-04-05 16:54:08 +0200779 lua_settop(L, 1);
780 return 1;
781}
782
783 static int
784luaV_list_insert (lua_State *L)
785{
786 luaV_List *lis = luaV_checkudata(L, 1, LUAVIM_LIST);
787 list_T *l = (list_T *) luaV_checkcache(L, (void *) *lis);
Bram Moolenaar46538ee2015-02-17 16:28:55 +0100788 long pos = (long) luaL_optinteger(L, 3, 0);
Bram Moolenaar1dced572012-04-05 16:54:08 +0200789 listitem_T *li = NULL;
790 typval_T v;
791 if (l->lv_lock)
792 luaL_error(L, "list is locked");
793 if (pos < l->lv_len)
794 {
795 li = list_find(l, pos);
796 if (li == NULL)
797 luaL_error(L, "invalid position");
798 }
799 lua_settop(L, 2);
800 luaV_totypval(L, 2, &v);
Bram Moolenaarb3766472013-04-15 13:49:21 +0200801 if (list_insert_tv(l, &v, li) == FAIL)
802 {
803 clear_tv(&v);
804 luaL_error(L, "Failed to add item to list");
805 }
806 clear_tv(&v);
Bram Moolenaar1dced572012-04-05 16:54:08 +0200807 lua_settop(L, 1);
808 return 1;
809}
810
811static const luaL_Reg luaV_List_mt[] = {
812 {"__tostring", luaV_list_tostring},
Bram Moolenaar1dced572012-04-05 16:54:08 +0200813 {"__len", luaV_list_len},
814 {"__call", luaV_list_call},
815 {"__index", luaV_list_index},
816 {"__newindex", luaV_list_newindex},
817 {"add", luaV_list_add},
818 {"insert", luaV_list_insert},
819 {NULL, NULL}
820};
821
822
823/* ======= Dict type ======= */
824
825 static luaV_Dict *
826luaV_newdict (lua_State *L, dict_T *dic)
827{
828 luaV_Dict *d = (luaV_Dict *) lua_newuserdata(L, sizeof(luaV_Dict));
829 *d = dic;
830 dic->dv_refcount++; /* reference in Lua */
831 luaV_setudata(L, dic); /* cache[dic] = udata */
832 luaV_getfield(L, LUAVIM_DICT);
833 lua_setmetatable(L, -2);
834 return d;
835}
836
837luaV_pushtype(dict_T, dict, luaV_Dict)
838luaV_type_tostring(dict, LUAVIM_DICT)
839
840 static int
Bram Moolenaar1dced572012-04-05 16:54:08 +0200841luaV_dict_len (lua_State *L)
842{
843 dict_T *d = luaV_unbox(L, luaV_Dict, 1);
844 lua_pushinteger(L, (d == NULL) ? 0 : (int) d->dv_hashtab.ht_used);
845 return 1;
846}
847
848 static int
Bram Moolenaarfeeaa682013-02-14 22:19:51 +0100849luaV_dict_iter (lua_State *L UNUSED)
Bram Moolenaar1dced572012-04-05 16:54:08 +0200850{
Bram Moolenaarfeeaa682013-02-14 22:19:51 +0100851#ifdef FEAT_EVAL
Bram Moolenaar1dced572012-04-05 16:54:08 +0200852 hashitem_T *hi = (hashitem_T *) lua_touserdata(L, lua_upvalueindex(2));
853 int n = lua_tointeger(L, lua_upvalueindex(3));
854 dictitem_T *di;
855 if (n <= 0) return 0;
856 while (HASHITEM_EMPTY(hi)) hi++;
857 di = dict_lookup(hi);
858 lua_pushstring(L, (char *) hi->hi_key);
859 luaV_pushtypval(L, &di->di_tv);
860 lua_pushlightuserdata(L, (void *) (hi + 1));
861 lua_replace(L, lua_upvalueindex(2));
862 lua_pushinteger(L, n - 1);
863 lua_replace(L, lua_upvalueindex(3));
864 return 2;
Bram Moolenaarfeeaa682013-02-14 22:19:51 +0100865#else
866 return 0;
867#endif
Bram Moolenaar1dced572012-04-05 16:54:08 +0200868}
869
870 static int
871luaV_dict_call (lua_State *L)
872{
873 dict_T *d = luaV_unbox(L, luaV_Dict, 1);
874 hashtab_T *ht = &d->dv_hashtab;
875 lua_pushvalue(L, lua_upvalueindex(1)); /* pass cache table along */
876 lua_pushlightuserdata(L, (void *) ht->ht_array);
877 lua_pushinteger(L, ht->ht_used); /* # remaining items */
878 lua_pushcclosure(L, luaV_dict_iter, 3);
879 return 1;
880}
881
882 static int
883luaV_dict_index (lua_State *L)
884{
885 dict_T *d = luaV_unbox(L, luaV_Dict, 1);
886 char_u *key = (char_u *) luaL_checkstring(L, 2);
887 dictitem_T *di = dict_find(d, key, -1);
888 if (di == NULL)
889 lua_pushnil(L);
890 else
891 luaV_pushtypval(L, &di->di_tv);
892 return 1;
893}
894
895 static int
896luaV_dict_newindex (lua_State *L)
897{
898 dict_T *d = luaV_unbox(L, luaV_Dict, 1);
899 char_u *key = (char_u *) luaL_checkstring(L, 2);
900 dictitem_T *di;
901 if (d->dv_lock)
902 luaL_error(L, "dict is locked");
903 di = dict_find(d, key, -1);
904 if (di == NULL) /* non-existing key? */
905 {
906 if (lua_isnil(L, 3)) return 0;
907 di = dictitem_alloc(key);
908 if (di == NULL) return 0;
909 if (dict_add(d, di) == FAIL)
910 {
911 vim_free(di);
912 return 0;
913 }
914 }
915 else
916 clear_tv(&di->di_tv);
917 if (lua_isnil(L, 3)) /* remove? */
918 {
919 hashitem_T *hi = hash_find(&d->dv_hashtab, di->di_key);
920 hash_remove(&d->dv_hashtab, hi);
921 dictitem_free(di);
922 }
923 else {
924 typval_T v;
925 luaV_totypval(L, 3, &v);
926 copy_tv(&v, &di->di_tv);
Bram Moolenaarb3766472013-04-15 13:49:21 +0200927 clear_tv(&v);
Bram Moolenaar1dced572012-04-05 16:54:08 +0200928 }
929 return 0;
930}
931
932static const luaL_Reg luaV_Dict_mt[] = {
933 {"__tostring", luaV_dict_tostring},
Bram Moolenaar1dced572012-04-05 16:54:08 +0200934 {"__len", luaV_dict_len},
935 {"__call", luaV_dict_call},
936 {"__index", luaV_dict_index},
937 {"__newindex", luaV_dict_newindex},
938 {NULL, NULL}
939};
940
941
942/* ======= Buffer type ======= */
943
944luaV_newtype(buf_T, buffer, luaV_Buffer, LUAVIM_BUFFER)
945luaV_pushtype(buf_T, buffer, luaV_Buffer)
946luaV_type_tostring(buffer, LUAVIM_BUFFER)
947
948 static int
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200949luaV_buffer_len(lua_State *L)
950{
Bram Moolenaar1dced572012-04-05 16:54:08 +0200951 buf_T *b = (buf_T *) luaV_checkvalid(L, luaV_Buffer, 1);
952 lua_pushinteger(L, b->b_ml.ml_line_count);
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200953 return 1;
954}
955
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200956 static int
957luaV_buffer_call(lua_State *L)
958{
Bram Moolenaar1dced572012-04-05 16:54:08 +0200959 buf_T *b = (buf_T *) luaV_checkvalid(L, luaV_Buffer, 1);
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200960 lua_settop(L, 1);
Bram Moolenaar1dced572012-04-05 16:54:08 +0200961 set_curbuf(b, DOBUF_SPLIT);
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200962 return 1;
963}
964
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200965 static int
966luaV_buffer_index(lua_State *L)
967{
Bram Moolenaar1dced572012-04-05 16:54:08 +0200968 buf_T *b = (buf_T *) luaV_checkvalid(L, luaV_Buffer, 1);
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200969 linenr_T n = (linenr_T) lua_tointeger(L, 2);
Bram Moolenaar1dced572012-04-05 16:54:08 +0200970 if (n > 0 && n <= b->b_ml.ml_line_count)
971 luaV_pushline(L, b, n);
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200972 else if (lua_isstring(L, 2))
973 {
974 const char *s = lua_tostring(L, 2);
975 if (strncmp(s, "name", 4) == 0)
Bram Moolenaar1dced572012-04-05 16:54:08 +0200976 lua_pushstring(L, (char *) b->b_sfname);
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200977 else if (strncmp(s, "fname", 5) == 0)
Bram Moolenaar1dced572012-04-05 16:54:08 +0200978 lua_pushstring(L, (char *) b->b_ffname);
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200979 else if (strncmp(s, "number", 6) == 0)
Bram Moolenaar1dced572012-04-05 16:54:08 +0200980 lua_pushinteger(L, b->b_fnum);
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200981 /* methods */
982 else if (strncmp(s, "insert", 6) == 0
983 || strncmp(s, "next", 4) == 0
984 || strncmp(s, "previous", 8) == 0
985 || strncmp(s, "isvalid", 7) == 0)
986 {
987 lua_getmetatable(L, 1);
988 lua_getfield(L, -1, s);
989 }
990 else
991 lua_pushnil(L);
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200992 }
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200993 else
994 lua_pushnil(L);
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200995 return 1;
996}
997
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200998 static int
999luaV_buffer_newindex(lua_State *L)
1000{
Bram Moolenaar1dced572012-04-05 16:54:08 +02001001 buf_T *b = (buf_T *) luaV_checkvalid(L, luaV_Buffer, 1);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001002 linenr_T n = (linenr_T) luaL_checkinteger(L, 2);
1003#ifdef HAVE_SANDBOX
1004 luaV_checksandbox(L);
1005#endif
Bram Moolenaar1dced572012-04-05 16:54:08 +02001006 if (n < 1 || n > b->b_ml.ml_line_count)
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001007 luaL_error(L, "invalid line number");
1008 if (lua_isnil(L, 3)) /* delete line */
1009 {
1010 buf_T *buf = curbuf;
Bram Moolenaar1dced572012-04-05 16:54:08 +02001011 curbuf = b;
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001012 if (u_savedel(n, 1L) == FAIL)
1013 {
1014 curbuf = buf;
1015 luaL_error(L, "cannot save undo information");
1016 }
1017 else if (ml_delete(n, FALSE) == FAIL)
1018 {
1019 curbuf = buf;
1020 luaL_error(L, "cannot delete line");
1021 }
1022 else {
1023 deleted_lines_mark(n, 1L);
Bram Moolenaar1dced572012-04-05 16:54:08 +02001024 if (b == curwin->w_buffer) /* fix cursor in current window? */
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001025 {
1026 if (curwin->w_cursor.lnum >= n)
1027 {
1028 if (curwin->w_cursor.lnum > n)
1029 {
1030 curwin->w_cursor.lnum -= 1;
1031 check_cursor_col();
1032 }
1033 else check_cursor();
1034 changed_cline_bef_curs();
1035 }
1036 invalidate_botline();
1037 }
1038 }
1039 curbuf = buf;
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001040 }
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001041 else if (lua_isstring(L, 3)) /* update line */
1042 {
1043 buf_T *buf = curbuf;
Bram Moolenaar1dced572012-04-05 16:54:08 +02001044 curbuf = b;
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001045 if (u_savesub(n) == FAIL)
1046 {
1047 curbuf = buf;
1048 luaL_error(L, "cannot save undo information");
1049 }
1050 else if (ml_replace(n, luaV_toline(L, 3), TRUE) == FAIL)
1051 {
1052 curbuf = buf;
1053 luaL_error(L, "cannot replace line");
1054 }
1055 else changed_bytes(n, 0);
1056 curbuf = buf;
Bram Moolenaar1dced572012-04-05 16:54:08 +02001057 if (b == curwin->w_buffer)
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001058 check_cursor_col();
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001059 }
1060 else
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001061 luaL_error(L, "wrong argument to change line");
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001062 return 0;
1063}
1064
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001065 static int
1066luaV_buffer_insert(lua_State *L)
1067{
Bram Moolenaar1dced572012-04-05 16:54:08 +02001068 luaV_Buffer *lb = luaV_checkudata(L, 1, LUAVIM_BUFFER);
1069 buf_T *b = (buf_T *) luaV_checkcache(L, (void *) *lb);
1070 linenr_T last = b->b_ml.ml_line_count;
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001071 linenr_T n = (linenr_T) luaL_optinteger(L, 3, last);
1072 buf_T *buf;
1073 luaL_checktype(L, 2, LUA_TSTRING);
1074#ifdef HAVE_SANDBOX
1075 luaV_checksandbox(L);
1076#endif
1077 /* fix insertion line */
1078 if (n < 0) n = 0;
1079 if (n > last) n = last;
1080 /* insert */
1081 buf = curbuf;
Bram Moolenaar1dced572012-04-05 16:54:08 +02001082 curbuf = b;
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001083 if (u_save(n, n + 1) == FAIL)
1084 {
1085 curbuf = buf;
1086 luaL_error(L, "cannot save undo information");
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001087 }
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001088 else if (ml_append(n, luaV_toline(L, 2), 0, FALSE) == FAIL)
1089 {
1090 curbuf = buf;
1091 luaL_error(L, "cannot insert line");
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001092 }
1093 else
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001094 appended_lines_mark(n, 1L);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001095 curbuf = buf;
1096 update_screen(VALID);
1097 return 0;
1098}
1099
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001100 static int
1101luaV_buffer_next(lua_State *L)
1102{
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001103 luaV_Buffer *b = luaV_checkudata(L, 1, LUAVIM_BUFFER);
Bram Moolenaar1dced572012-04-05 16:54:08 +02001104 buf_T *buf = (buf_T *) luaV_checkcache(L, (void *) *b);
1105 luaV_pushbuffer(L, buf->b_next);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001106 return 1;
1107}
1108
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001109 static int
1110luaV_buffer_previous(lua_State *L)
1111{
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001112 luaV_Buffer *b = luaV_checkudata(L, 1, LUAVIM_BUFFER);
Bram Moolenaar1dced572012-04-05 16:54:08 +02001113 buf_T *buf = (buf_T *) luaV_checkcache(L, (void *) *b);
1114 luaV_pushbuffer(L, buf->b_prev);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001115 return 1;
1116}
1117
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001118 static int
1119luaV_buffer_isvalid(lua_State *L)
1120{
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001121 luaV_Buffer *b = luaV_checkudata(L, 1, LUAVIM_BUFFER);
Bram Moolenaar1dced572012-04-05 16:54:08 +02001122 luaV_getudata(L, *b);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001123 lua_pushboolean(L, !lua_isnil(L, -1));
1124 return 1;
1125}
1126
1127static const luaL_Reg luaV_Buffer_mt[] = {
1128 {"__tostring", luaV_buffer_tostring},
1129 {"__len", luaV_buffer_len},
1130 {"__call", luaV_buffer_call},
1131 {"__index", luaV_buffer_index},
1132 {"__newindex", luaV_buffer_newindex},
1133 {"insert", luaV_buffer_insert},
1134 {"next", luaV_buffer_next},
1135 {"previous", luaV_buffer_previous},
1136 {"isvalid", luaV_buffer_isvalid},
1137 {NULL, NULL}
1138};
1139
1140
1141/* ======= Window type ======= */
1142
Bram Moolenaar1dced572012-04-05 16:54:08 +02001143luaV_newtype(win_T, window, luaV_Window, LUAVIM_WINDOW)
1144luaV_pushtype(win_T, window, luaV_Window)
1145luaV_type_tostring(window, LUAVIM_WINDOW)
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001146
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001147 static int
1148luaV_window_call(lua_State *L)
1149{
Bram Moolenaar1dced572012-04-05 16:54:08 +02001150 win_T *w = (win_T *) luaV_checkvalid(L, luaV_Window, 1);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001151 lua_settop(L, 1);
Bram Moolenaar1dced572012-04-05 16:54:08 +02001152 win_goto(w);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001153 return 1;
1154}
1155
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001156 static int
1157luaV_window_index(lua_State *L)
1158{
Bram Moolenaar1dced572012-04-05 16:54:08 +02001159 win_T *w = (win_T *) luaV_checkvalid(L, luaV_Window, 1);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001160 const char *s = luaL_checkstring(L, 2);
1161 if (strncmp(s, "buffer", 6) == 0)
Bram Moolenaar1dced572012-04-05 16:54:08 +02001162 luaV_pushbuffer(L, w->w_buffer);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001163 else if (strncmp(s, "line", 4) == 0)
Bram Moolenaar1dced572012-04-05 16:54:08 +02001164 lua_pushinteger(L, w->w_cursor.lnum);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001165 else if (strncmp(s, "col", 3) == 0)
Bram Moolenaar1dced572012-04-05 16:54:08 +02001166 lua_pushinteger(L, w->w_cursor.col + 1);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001167#ifdef FEAT_VERTSPLIT
1168 else if (strncmp(s, "width", 5) == 0)
Bram Moolenaar1dced572012-04-05 16:54:08 +02001169 lua_pushinteger(L, W_WIDTH(w));
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001170#endif
1171 else if (strncmp(s, "height", 6) == 0)
Bram Moolenaar1dced572012-04-05 16:54:08 +02001172 lua_pushinteger(L, w->w_height);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001173 /* methods */
1174 else if (strncmp(s, "next", 4) == 0
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001175 || strncmp(s, "previous", 8) == 0
1176 || strncmp(s, "isvalid", 7) == 0)
1177 {
1178 lua_getmetatable(L, 1);
1179 lua_getfield(L, -1, s);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001180 }
1181 else
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001182 lua_pushnil(L);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001183 return 1;
1184}
1185
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001186 static int
1187luaV_window_newindex (lua_State *L)
1188{
Bram Moolenaar1dced572012-04-05 16:54:08 +02001189 win_T *w = (win_T *) luaV_checkvalid(L, luaV_Window, 1);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001190 const char *s = luaL_checkstring(L, 2);
1191 int v = luaL_checkinteger(L, 3);
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001192 if (strncmp(s, "line", 4) == 0)
1193 {
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001194#ifdef HAVE_SANDBOX
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001195 luaV_checksandbox(L);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001196#endif
Bram Moolenaar1dced572012-04-05 16:54:08 +02001197 if (v < 1 || v > w->w_buffer->b_ml.ml_line_count)
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001198 luaL_error(L, "line out of range");
Bram Moolenaar1dced572012-04-05 16:54:08 +02001199 w->w_cursor.lnum = v;
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001200 update_screen(VALID);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001201 }
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001202 else if (strncmp(s, "col", 3) == 0)
1203 {
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001204#ifdef HAVE_SANDBOX
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001205 luaV_checksandbox(L);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001206#endif
Bram Moolenaar1dced572012-04-05 16:54:08 +02001207 w->w_cursor.col = v - 1;
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001208 update_screen(VALID);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001209 }
1210#ifdef FEAT_VERTSPLIT
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001211 else if (strncmp(s, "width", 5) == 0)
1212 {
1213 win_T *win = curwin;
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001214#ifdef FEAT_GUI
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001215 need_mouse_correct = TRUE;
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001216#endif
Bram Moolenaar1dced572012-04-05 16:54:08 +02001217 curwin = w;
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001218 win_setwidth(v);
1219 curwin = win;
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001220 }
1221#endif
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001222 else if (strncmp(s, "height", 6) == 0)
1223 {
1224 win_T *win = curwin;
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001225#ifdef FEAT_GUI
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001226 need_mouse_correct = TRUE;
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001227#endif
Bram Moolenaar1dced572012-04-05 16:54:08 +02001228 curwin = w;
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001229 win_setheight(v);
1230 curwin = win;
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001231 }
1232 else
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001233 luaL_error(L, "invalid window property: `%s'", s);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001234 return 0;
1235}
1236
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001237 static int
1238luaV_window_next(lua_State *L)
1239{
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001240 luaV_Window *w = luaV_checkudata(L, 1, LUAVIM_WINDOW);
Bram Moolenaar1dced572012-04-05 16:54:08 +02001241 win_T *win = (win_T *) luaV_checkcache(L, (void *) *w);
1242 luaV_pushwindow(L, win->w_next);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001243 return 1;
1244}
1245
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001246 static int
1247luaV_window_previous(lua_State *L)
1248{
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001249 luaV_Window *w = luaV_checkudata(L, 1, LUAVIM_WINDOW);
Bram Moolenaar1dced572012-04-05 16:54:08 +02001250 win_T *win = (win_T *) luaV_checkcache(L, (void *) *w);
1251 luaV_pushwindow(L, win->w_prev);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001252 return 1;
1253}
1254
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001255 static int
1256luaV_window_isvalid(lua_State *L)
1257{
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001258 luaV_Window *w = luaV_checkudata(L, 1, LUAVIM_WINDOW);
Bram Moolenaar1dced572012-04-05 16:54:08 +02001259 luaV_getudata(L, *w);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001260 lua_pushboolean(L, !lua_isnil(L, -1));
1261 return 1;
1262}
1263
1264static const luaL_Reg luaV_Window_mt[] = {
1265 {"__tostring", luaV_window_tostring},
1266 {"__call", luaV_window_call},
1267 {"__index", luaV_window_index},
1268 {"__newindex", luaV_window_newindex},
1269 {"next", luaV_window_next},
1270 {"previous", luaV_window_previous},
1271 {"isvalid", luaV_window_isvalid},
1272 {NULL, NULL}
1273};
1274
1275
1276/* ======= Vim module ======= */
1277
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001278 static int
1279luaV_print(lua_State *L)
1280{
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001281 int i, n = lua_gettop(L); /* nargs */
1282 const char *s;
1283 size_t l;
1284 luaL_Buffer b;
1285 luaL_buffinit(L, &b);
1286 lua_getglobal(L, "tostring");
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001287 for (i = 1; i <= n; i++)
1288 {
1289 lua_pushvalue(L, -1); /* tostring */
1290 lua_pushvalue(L, i); /* arg */
1291 lua_call(L, 1, 1);
1292 s = lua_tolstring(L, -1, &l);
1293 if (s == NULL)
1294 return luaL_error(L, "cannot convert to string");
1295 if (i > 1) luaL_addchar(&b, ' '); /* use space instead of tab */
1296 luaV_addlstring(&b, s, l, 0);
1297 lua_pop(L, 1);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001298 }
1299 luaL_pushresult(&b);
1300 luaV_msg(L);
1301 return 0;
1302}
1303
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001304 static int
Bram Moolenaar38e2b062011-09-21 17:15:39 +02001305luaV_debug(lua_State *L)
1306{
1307 lua_settop(L, 0);
1308 lua_getglobal(L, "vim");
1309 lua_getfield(L, -1, "eval");
1310 lua_remove(L, -2); /* vim.eval at position 1 */
1311 for (;;)
1312 {
1313 const char *input;
1314 size_t l;
1315 lua_pushvalue(L, 1); /* vim.eval */
1316 lua_pushliteral(L, "input('lua_debug> ')");
1317 lua_call(L, 1, 1); /* return string */
1318 input = lua_tolstring(L, -1, &l);
1319 if (l == 0 || strcmp(input, "cont") == 0)
1320 return 0;
1321 msg_putchar('\n'); /* avoid outputting on input line */
1322 if (luaL_loadbuffer(L, input, l, "=(debug command)")
1323 || lua_pcall(L, 0, 0, 0))
1324 luaV_emsg(L);
1325 lua_settop(L, 1); /* remove eventual returns, but keep vim.eval */
1326 }
1327}
1328
1329 static int
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001330luaV_command(lua_State *L)
1331{
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001332 do_cmdline_cmd((char_u *) luaL_checkstring(L, 1));
1333 update_screen(VALID);
1334 return 0;
1335}
1336
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001337 static int
1338luaV_eval(lua_State *L)
1339{
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001340 typval_T *tv = eval_expr((char_u *) luaL_checkstring(L, 1), NULL);
1341 if (tv == NULL) luaL_error(L, "invalid expression");
1342 luaV_pushtypval(L, tv);
Bram Moolenaarb3766472013-04-15 13:49:21 +02001343 free_tv(tv);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001344 return 1;
1345}
1346
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001347 static int
Bram Moolenaar0d2e4fc2010-07-18 12:35:47 +02001348luaV_beep(lua_State *L UNUSED)
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001349{
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001350 vim_beep();
1351 return 0;
1352}
1353
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001354 static int
1355luaV_line(lua_State *L)
1356{
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001357 luaV_pushline(L, curbuf, curwin->w_cursor.lnum);
1358 return 1;
1359}
1360
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001361 static int
Bram Moolenaar1dced572012-04-05 16:54:08 +02001362luaV_list(lua_State *L)
1363{
1364 list_T *l = list_alloc();
1365 if (l == NULL)
1366 lua_pushnil(L);
1367 else
1368 luaV_newlist(L, l);
1369 return 1;
1370}
1371
1372 static int
1373luaV_dict(lua_State *L)
1374{
1375 dict_T *d = dict_alloc();
1376 if (d == NULL)
1377 lua_pushnil(L);
1378 else
1379 luaV_newdict(L, d);
1380 return 1;
1381}
1382
1383 static int
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001384luaV_buffer(lua_State *L)
1385{
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001386 buf_T *buf;
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001387 if (lua_isstring(L, 1)) /* get by number or name? */
1388 {
1389 if (lua_isnumber(L, 1)) /* by number? */
1390 {
1391 int n = lua_tointeger(L, 1);
1392 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
1393 if (buf->b_fnum == n) break;
1394 }
1395 else { /* by name */
1396 size_t l;
1397 const char *s = lua_tolstring(L, 1, &l);
1398 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
1399 {
1400 if (buf->b_ffname == NULL || buf->b_sfname == NULL)
1401 {
1402 if (l == 0) break;
1403 }
Bram Moolenaar0d2e4fc2010-07-18 12:35:47 +02001404 else if (strncmp(s, (char *)buf->b_ffname, l) == 0
1405 || strncmp(s, (char *)buf->b_sfname, l) == 0)
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001406 break;
1407 }
1408 }
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001409 }
Bram Moolenaar1dced572012-04-05 16:54:08 +02001410 else
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001411 buf = (lua_toboolean(L, 1)) ? firstbuf : curbuf; /* first buffer? */
Bram Moolenaar1dced572012-04-05 16:54:08 +02001412 luaV_pushbuffer(L, buf);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001413 return 1;
1414}
1415
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001416 static int
1417luaV_window(lua_State *L)
1418{
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001419 win_T *win;
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001420 if (lua_isnumber(L, 1)) /* get by number? */
1421 {
1422 int n = lua_tointeger(L, 1);
1423 for (win = firstwin; win != NULL; win = win->w_next, n--)
1424 if (n == 1) break;
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001425 }
Bram Moolenaar1dced572012-04-05 16:54:08 +02001426 else
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001427 win = (lua_toboolean(L, 1)) ? firstwin : curwin; /* first window? */
Bram Moolenaar1dced572012-04-05 16:54:08 +02001428 luaV_pushwindow(L, win);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001429 return 1;
1430}
1431
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001432 static int
1433luaV_open(lua_State *L)
1434{
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001435 char_u *s = NULL;
1436#ifdef HAVE_SANDBOX
1437 luaV_checksandbox(L);
1438#endif
1439 if (lua_isstring(L, 1)) s = (char_u *) lua_tostring(L, 1);
Bram Moolenaarfa263a52011-12-08 16:00:16 +01001440 luaV_pushbuffer(L, buflist_new(s, NULL, 1L, BLN_LISTED));
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001441 return 1;
1442}
1443
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001444 static int
Bram Moolenaar1dced572012-04-05 16:54:08 +02001445luaV_type(lua_State *L)
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001446{
Bram Moolenaar1dced572012-04-05 16:54:08 +02001447 luaL_checkany(L, 1);
1448 if (lua_type(L, 1) == LUA_TUSERDATA) /* check vim udata? */
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001449 {
Bram Moolenaar1dced572012-04-05 16:54:08 +02001450 lua_settop(L, 1);
1451 if (lua_getmetatable(L, 1))
1452 {
1453 luaV_getfield(L, LUAVIM_LIST);
1454 if (lua_rawequal(L, -1, 2))
1455 {
1456 lua_pushstring(L, "list");
1457 return 1;
1458 }
1459 luaV_getfield(L, LUAVIM_DICT);
1460 if (lua_rawequal(L, -1, 2))
1461 {
1462 lua_pushstring(L, "dict");
1463 return 1;
1464 }
1465 luaV_getfield(L, LUAVIM_BUFFER);
1466 if (lua_rawequal(L, -1, 2))
1467 {
1468 lua_pushstring(L, "buffer");
1469 return 1;
1470 }
1471 luaV_getfield(L, LUAVIM_WINDOW);
1472 if (lua_rawequal(L, -1, 2))
1473 {
1474 lua_pushstring(L, "window");
1475 return 1;
1476 }
1477 }
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001478 }
Bram Moolenaar1dced572012-04-05 16:54:08 +02001479 lua_pushstring(L, luaL_typename(L, 1)); /* fallback */
1480 return 1;
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001481}
1482
1483static const luaL_Reg luaV_module[] = {
1484 {"command", luaV_command},
1485 {"eval", luaV_eval},
1486 {"beep", luaV_beep},
1487 {"line", luaV_line},
Bram Moolenaar1dced572012-04-05 16:54:08 +02001488 {"list", luaV_list},
1489 {"dict", luaV_dict},
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001490 {"buffer", luaV_buffer},
1491 {"window", luaV_window},
1492 {"open", luaV_open},
Bram Moolenaar1dced572012-04-05 16:54:08 +02001493 {"type", luaV_type},
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001494 {NULL, NULL}
1495};
1496
Bram Moolenaar1dced572012-04-05 16:54:08 +02001497/* for freeing list, dict, buffer and window objects; lightuserdata as arg */
1498 static int
1499luaV_free(lua_State *L)
1500{
1501 lua_pushnil(L);
1502 luaV_setudata(L, lua_touserdata(L, 1));
1503 return 0;
1504}
1505
1506 static int
1507luaV_luaeval (lua_State *L)
1508{
1509 luaL_Buffer b;
1510 size_t l;
1511 const char *str = lua_tolstring(L, 1, &l);
1512 typval_T *arg = (typval_T *) lua_touserdata(L, 2);
1513 typval_T *rettv = (typval_T *) lua_touserdata(L, 3);
1514 luaL_buffinit(L, &b);
1515 luaL_addlstring(&b, LUAVIM_EVALHEADER, sizeof(LUAVIM_EVALHEADER) - 1);
1516 luaL_addlstring(&b, str, l);
1517 luaL_pushresult(&b);
1518 str = lua_tolstring(L, -1, &l);
1519 if (luaL_loadbuffer(L, str, l, LUAVIM_EVALNAME)) /* compile error? */
1520 {
1521 luaV_emsg(L);
1522 return 0;
1523 }
1524 luaV_pushtypval(L, arg);
1525 if (lua_pcall(L, 1, 1, 0)) /* running error? */
1526 {
1527 luaV_emsg(L);
1528 return 0;
1529 }
1530 luaV_totypval(L, -1, rettv);
Bram Moolenaarf554a322015-02-04 23:08:01 +01001531 return 0;
Bram Moolenaar1dced572012-04-05 16:54:08 +02001532}
1533
1534 static int
1535luaV_setref (lua_State *L)
1536{
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01001537 int copyID = lua_tointeger(L, 1);
1538 int abort = FALSE;
1539 typval_T tv;
1540
Bram Moolenaar1dced572012-04-05 16:54:08 +02001541 luaV_getfield(L, LUAVIM_LIST);
1542 luaV_getfield(L, LUAVIM_DICT);
1543 lua_pushnil(L);
Bram Moolenaarb84634d2015-02-04 22:02:37 +01001544 /* traverse cache table */
1545 while (!abort && lua_next(L, lua_upvalueindex(1)) != 0)
Bram Moolenaar1dced572012-04-05 16:54:08 +02001546 {
1547 lua_getmetatable(L, -1);
1548 if (lua_rawequal(L, -1, 2)) /* list? */
1549 {
1550 tv.v_type = VAR_LIST;
1551 tv.vval.v_list = (list_T *) lua_touserdata(L, 4); /* key */
1552 }
1553 else if (lua_rawequal(L, -1, 3)) /* dict? */
1554 {
1555 tv.v_type = VAR_DICT;
1556 tv.vval.v_dict = (dict_T *) lua_touserdata(L, 4); /* key */
1557 }
1558 lua_pop(L, 2); /* metatable and value */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01001559 abort = set_ref_in_item(&tv, copyID, NULL, NULL);
Bram Moolenaar1dced572012-04-05 16:54:08 +02001560 }
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01001561 lua_pushinteger(L, abort);
Bram Moolenaarf554a322015-02-04 23:08:01 +01001562 return 1;
Bram Moolenaar1dced572012-04-05 16:54:08 +02001563}
1564
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001565 static int
1566luaopen_vim(lua_State *L)
1567{
Bram Moolenaar1dced572012-04-05 16:54:08 +02001568 /* set cache table */
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001569 lua_newtable(L);
1570 lua_newtable(L);
Bram Moolenaar1dced572012-04-05 16:54:08 +02001571 lua_pushstring(L, "v");
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001572 lua_setfield(L, -2, "__mode");
Bram Moolenaar1dced572012-04-05 16:54:08 +02001573 lua_setmetatable(L, -2); /* cache is weak-valued */
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001574 /* print */
1575 lua_pushcfunction(L, luaV_print);
1576 lua_setglobal(L, "print");
Bram Moolenaar38e2b062011-09-21 17:15:39 +02001577 /* debug.debug */
1578 lua_getglobal(L, "debug");
1579 lua_pushcfunction(L, luaV_debug);
1580 lua_setfield(L, -2, "debug");
1581 lua_pop(L, 1);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001582 /* free */
1583 lua_pushlightuserdata(L, (void *) LUAVIM_FREE);
Bram Moolenaar1dced572012-04-05 16:54:08 +02001584 lua_pushvalue(L, 1); /* cache table */
1585 lua_pushcclosure(L, luaV_free, 1);
1586 lua_rawset(L, LUA_REGISTRYINDEX);
1587 /* luaeval */
1588 lua_pushlightuserdata(L, (void *) LUAVIM_LUAEVAL);
1589 lua_pushvalue(L, 1); /* cache table */
1590 lua_pushcclosure(L, luaV_luaeval, 1);
1591 lua_rawset(L, LUA_REGISTRYINDEX);
1592 /* setref */
1593 lua_pushlightuserdata(L, (void *) LUAVIM_SETREF);
1594 lua_pushvalue(L, 1); /* cache table */
1595 lua_pushcclosure(L, luaV_setref, 1);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001596 lua_rawset(L, LUA_REGISTRYINDEX);
1597 /* register */
Bram Moolenaar1dced572012-04-05 16:54:08 +02001598 luaV_newmetatable(L, LUAVIM_LIST);
1599 lua_pushvalue(L, 1);
1600 luaV_openlib(L, luaV_List_mt, 1);
1601 luaV_newmetatable(L, LUAVIM_DICT);
1602 lua_pushvalue(L, 1);
1603 luaV_openlib(L, luaV_Dict_mt, 1);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001604 luaV_newmetatable(L, LUAVIM_BUFFER);
Bram Moolenaar1dced572012-04-05 16:54:08 +02001605 lua_pushvalue(L, 1); /* cache table */
1606 luaV_openlib(L, luaV_Buffer_mt, 1);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001607 luaV_newmetatable(L, LUAVIM_WINDOW);
Bram Moolenaar1dced572012-04-05 16:54:08 +02001608 lua_pushvalue(L, 1); /* cache table */
1609 luaV_openlib(L, luaV_Window_mt, 1);
1610 lua_newtable(L); /* vim table */
1611 lua_pushvalue(L, 1); /* cache table */
1612 luaV_openlib(L, luaV_module, 1);
1613 lua_setglobal(L, LUAVIM_NAME);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001614 return 0;
1615}
1616
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001617 static lua_State *
1618luaV_newstate(void)
1619{
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001620 lua_State *L = luaL_newstate();
Bram Moolenaar16c98f92010-07-28 22:46:08 +02001621 luaL_openlibs(L); /* core libs */
1622 lua_pushcfunction(L, luaopen_vim); /* vim */
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001623 lua_call(L, 0, 0);
1624 return L;
1625}
1626
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001627 static void
1628luaV_setrange(lua_State *L, int line1, int line2)
1629{
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001630 lua_getglobal(L, LUAVIM_NAME);
1631 lua_pushinteger(L, line1);
1632 lua_setfield(L, -2, "firstline");
1633 lua_pushinteger(L, line2);
1634 lua_setfield(L, -2, "lastline");
1635 lua_pop(L, 1); /* vim table */
1636}
1637
1638
1639/* ======= Interface ======= */
1640
1641static lua_State *L = NULL;
1642
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001643 static int
Bram Moolenaar1dced572012-04-05 16:54:08 +02001644lua_isopen(void)
Bram Moolenaar2bd6a1b2010-08-12 22:14:01 +02001645{
1646 return L != NULL;
1647}
1648
1649 static int
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001650lua_init(void)
1651{
Bram Moolenaar1dced572012-04-05 16:54:08 +02001652 if (!lua_isopen())
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001653 {
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001654#ifdef DYNAMIC_LUA
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001655 if (!lua_enabled(TRUE))
1656 {
1657 EMSG(_("Lua library cannot be loaded."));
1658 return FAIL;
1659 }
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001660#endif
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001661 L = luaV_newstate();
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001662 }
1663 return OK;
1664}
1665
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001666 void
1667lua_end(void)
1668{
Bram Moolenaar1dced572012-04-05 16:54:08 +02001669 if (lua_isopen())
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001670 {
1671 lua_close(L);
1672 L = NULL;
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001673#ifdef DYNAMIC_LUA
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001674 end_dynamic_lua();
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001675#endif
1676 }
1677}
1678
1679/* ex commands */
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001680 void
1681ex_lua(exarg_T *eap)
1682{
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001683 char *script;
1684 if (lua_init() == FAIL) return;
1685 script = (char *) script_get(eap, eap->arg);
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001686 if (!eap->skip)
1687 {
1688 char *s = (script) ? script : (char *) eap->arg;
1689 luaV_setrange(L, eap->line1, eap->line2);
1690 if (luaL_loadbuffer(L, s, strlen(s), LUAVIM_CHUNKNAME)
1691 || lua_pcall(L, 0, 0, 0))
1692 luaV_emsg(L);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001693 }
1694 if (script != NULL) vim_free(script);
1695}
1696
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001697 void
1698ex_luado(exarg_T *eap)
1699{
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001700 linenr_T l;
1701 const char *s = (const char *) eap->arg;
1702 luaL_Buffer b;
1703 size_t len;
1704 if (lua_init() == FAIL) return;
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001705 if (u_save(eap->line1 - 1, eap->line2 + 1) == FAIL)
1706 {
1707 EMSG(_("cannot save undo information"));
1708 return;
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001709 }
1710 luaV_setrange(L, eap->line1, eap->line2);
1711 luaL_buffinit(L, &b);
Bram Moolenaarbd2f3c32012-04-06 14:31:00 +02001712 luaL_addlstring(&b, "return function(line, linenr) ", 30); /* header */
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001713 luaL_addlstring(&b, s, strlen(s));
1714 luaL_addlstring(&b, " end", 4); /* footer */
1715 luaL_pushresult(&b);
1716 s = lua_tolstring(L, -1, &len);
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001717 if (luaL_loadbuffer(L, s, len, LUAVIM_CHUNKNAME))
1718 {
1719 luaV_emsg(L);
1720 lua_pop(L, 1); /* function body */
1721 return;
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001722 }
1723 lua_call(L, 0, 1);
1724 lua_replace(L, -2); /* function -> body */
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001725 for (l = eap->line1; l <= eap->line2; l++)
1726 {
1727 lua_pushvalue(L, -1); /* function */
1728 luaV_pushline(L, curbuf, l); /* current line as arg */
Bram Moolenaarbd2f3c32012-04-06 14:31:00 +02001729 lua_pushinteger(L, l); /* current line number as arg */
1730 if (lua_pcall(L, 2, 1, 0))
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001731 {
1732 luaV_emsg(L);
1733 break;
1734 }
1735 if (lua_isstring(L, -1)) /* update line? */
1736 {
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001737#ifdef HAVE_SANDBOX
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001738 luaV_checksandbox(L);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001739#endif
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001740 ml_replace(l, luaV_toline(L, -1), TRUE);
1741 changed_bytes(l, 0);
1742 lua_pop(L, 1); /* result from luaV_toline */
1743 }
1744 lua_pop(L, 1); /* line */
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001745 }
1746 lua_pop(L, 1); /* function */
1747 check_cursor();
1748 update_screen(NOT_VALID);
1749}
1750
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001751 void
1752ex_luafile(exarg_T *eap)
1753{
1754 if (lua_init() == FAIL)
1755 return;
1756 if (!eap->skip)
1757 {
1758 luaV_setrange(L, eap->line1, eap->line2);
1759 if (luaL_loadfile(L, (char *) eap->arg) || lua_pcall(L, 0, 0, 0))
1760 luaV_emsg(L);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001761 }
1762}
1763
Bram Moolenaar1dced572012-04-05 16:54:08 +02001764#define luaV_freetype(typ,tname) \
1765 void \
1766 lua_##tname##_free(typ *o) \
1767 { \
1768 if (!lua_isopen()) return; \
1769 luaV_getfield(L, LUAVIM_FREE); \
1770 lua_pushlightuserdata(L, (void *) o); \
1771 lua_call(L, 1, 0); \
1772 }
1773
1774luaV_freetype(buf_T, buffer)
1775luaV_freetype(win_T, window)
1776
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001777 void
Bram Moolenaar1dced572012-04-05 16:54:08 +02001778do_luaeval (char_u *str, typval_T *arg, typval_T *rettv)
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001779{
Bram Moolenaar1dced572012-04-05 16:54:08 +02001780 lua_init();
1781 luaV_getfield(L, LUAVIM_LUAEVAL);
1782 lua_pushstring(L, (char *) str);
1783 lua_pushlightuserdata(L, (void *) arg);
1784 lua_pushlightuserdata(L, (void *) rettv);
1785 lua_call(L, 3, 0);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001786}
1787
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01001788 int
Bram Moolenaar1dced572012-04-05 16:54:08 +02001789set_ref_in_lua (int copyID)
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001790{
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01001791 int aborted = 0;
1792
1793 if (lua_isopen())
1794 {
1795 luaV_getfield(L, LUAVIM_SETREF);
1796 /* call the function with 1 arg, getting 1 result back */
1797 lua_pushinteger(L, copyID);
1798 lua_call(L, 1, 1);
1799 /* get the result */
1800 aborted = lua_tointeger(L, -1);
1801 /* pop result off the stack */
1802 lua_pop(L, 1);
1803 }
1804 return aborted;
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001805}
1806
1807#endif