blob: ef827ca589de64df35ee103697ffcceba913cfc6 [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
114#else
115#define lua_tonumberx dll_lua_tonumberx
116#define lua_tointegerx dll_lua_tointegerx
117#define lua_callk dll_lua_callk
118#define lua_pcallk dll_lua_pcallk
119#define lua_getglobal dll_lua_getglobal
120#define lua_setglobal dll_lua_setglobal
Bram Moolenaar1dced572012-04-05 16:54:08 +0200121#endif
Bram Moolenaar1f860d82015-06-27 18:36:16 +0200122#if LUA_VERSION_NUM <= 502
123#define lua_replace dll_lua_replace
124#define lua_remove dll_lua_remove
125#endif
126#if LUA_VERSION_NUM >= 503
127#define lua_rotate dll_lua_rotate
128#define lua_copy dll_lua_copy
129#endif
Bram Moolenaarbd2f3c32012-04-06 14:31:00 +0200130#define lua_typename dll_lua_typename
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200131#define lua_close dll_lua_close
132#define lua_gettop dll_lua_gettop
133#define lua_settop dll_lua_settop
134#define lua_pushvalue dll_lua_pushvalue
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200135#define lua_isnumber dll_lua_isnumber
136#define lua_isstring dll_lua_isstring
137#define lua_type dll_lua_type
138#define lua_rawequal dll_lua_rawequal
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200139#define lua_toboolean dll_lua_toboolean
140#define lua_tolstring dll_lua_tolstring
141#define lua_touserdata dll_lua_touserdata
142#define lua_pushnil dll_lua_pushnil
143#define lua_pushnumber dll_lua_pushnumber
144#define lua_pushinteger dll_lua_pushinteger
145#define lua_pushlstring dll_lua_pushlstring
146#define lua_pushstring dll_lua_pushstring
147#define lua_pushfstring dll_lua_pushfstring
148#define lua_pushcclosure dll_lua_pushcclosure
149#define lua_pushboolean dll_lua_pushboolean
150#define lua_pushlightuserdata dll_lua_pushlightuserdata
151#define lua_getfield dll_lua_getfield
152#define lua_rawget dll_lua_rawget
Bram Moolenaar1dced572012-04-05 16:54:08 +0200153#define lua_rawgeti dll_lua_rawgeti
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200154#define lua_createtable dll_lua_createtable
155#define lua_newuserdata dll_lua_newuserdata
156#define lua_getmetatable dll_lua_getmetatable
157#define lua_setfield dll_lua_setfield
158#define lua_rawset dll_lua_rawset
159#define lua_rawseti dll_lua_rawseti
160#define lua_setmetatable dll_lua_setmetatable
Bram Moolenaarbd2f3c32012-04-06 14:31:00 +0200161#define lua_next dll_lua_next
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200162/* libs */
163#define luaopen_base dll_luaopen_base
164#define luaopen_table dll_luaopen_table
165#define luaopen_string dll_luaopen_string
166#define luaopen_math dll_luaopen_math
Bram Moolenaar16c98f92010-07-28 22:46:08 +0200167#define luaopen_io dll_luaopen_io
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200168#define luaopen_os dll_luaopen_os
169#define luaopen_package dll_luaopen_package
170#define luaopen_debug dll_luaopen_debug
Bram Moolenaar16c98f92010-07-28 22:46:08 +0200171#define luaL_openlibs dll_luaL_openlibs
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200172
173/* lauxlib */
Bram Moolenaar1dced572012-04-05 16:54:08 +0200174#if LUA_VERSION_NUM <= 501
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200175void (*dll_luaL_register) (lua_State *L, const char *libname, const luaL_Reg *l);
Bram Moolenaar1dced572012-04-05 16:54:08 +0200176char *(*dll_luaL_prepbuffer) (luaL_Buffer *B);
177void (*dll_luaL_openlib) (lua_State *L, const char *libname, const luaL_Reg *l, int nup);
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200178int (*dll_luaL_typerror) (lua_State *L, int narg, const char *tname);
Bram Moolenaar1dced572012-04-05 16:54:08 +0200179int (*dll_luaL_loadfile) (lua_State *L, const char *filename);
180int (*dll_luaL_loadbuffer) (lua_State *L, const char *buff, size_t sz, const char *name);
181#else
182char *(*dll_luaL_prepbuffsize) (luaL_Buffer *B, size_t sz);
183void (*dll_luaL_setfuncs) (lua_State *L, const luaL_Reg *l, int nup);
184int (*dll_luaL_loadfilex) (lua_State *L, const char *filename, const char *mode);
185int (*dll_luaL_loadbufferx) (lua_State *L, const char *buff, size_t sz, const char *name, const char *mode);
186int (*dll_luaL_argerror) (lua_State *L, int numarg, const char *extramsg);
187#endif
Bram Moolenaarbd2f3c32012-04-06 14:31:00 +0200188void (*dll_luaL_checkany) (lua_State *L, int narg);
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200189const char *(*dll_luaL_checklstring) (lua_State *L, int numArg, size_t *l);
190lua_Integer (*dll_luaL_checkinteger) (lua_State *L, int numArg);
191lua_Integer (*dll_luaL_optinteger) (lua_State *L, int nArg, lua_Integer def);
192void (*dll_luaL_checktype) (lua_State *L, int narg, int t);
193int (*dll_luaL_error) (lua_State *L, const char *fmt, ...);
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200194lua_State *(*dll_luaL_newstate) (void);
195void (*dll_luaL_buffinit) (lua_State *L, luaL_Buffer *B);
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200196void (*dll_luaL_addlstring) (luaL_Buffer *B, const char *s, size_t l);
197void (*dll_luaL_pushresult) (luaL_Buffer *B);
198/* lua */
Bram Moolenaar1dced572012-04-05 16:54:08 +0200199#if LUA_VERSION_NUM <= 501
200lua_Number (*dll_lua_tonumber) (lua_State *L, int idx);
201lua_Integer (*dll_lua_tointeger) (lua_State *L, int idx);
202void (*dll_lua_call) (lua_State *L, int nargs, int nresults);
203int (*dll_lua_pcall) (lua_State *L, int nargs, int nresults, int errfunc);
204#else
205lua_Number (*dll_lua_tonumberx) (lua_State *L, int idx, int *isnum);
206lua_Integer (*dll_lua_tointegerx) (lua_State *L, int idx, int *isnum);
207void (*dll_lua_callk) (lua_State *L, int nargs, int nresults, int ctx,
Bram Moolenaardb913952012-06-29 12:54:53 +0200208 lua_CFunction k);
Bram Moolenaar1dced572012-04-05 16:54:08 +0200209int (*dll_lua_pcallk) (lua_State *L, int nargs, int nresults, int errfunc,
Bram Moolenaardb913952012-06-29 12:54:53 +0200210 int ctx, lua_CFunction k);
Bram Moolenaar1dced572012-04-05 16:54:08 +0200211void (*dll_lua_getglobal) (lua_State *L, const char *var);
212void (*dll_lua_setglobal) (lua_State *L, const char *var);
Bram Moolenaar1f860d82015-06-27 18:36:16 +0200213#endif
214#if LUA_VERSION_NUM <= 502
215void (*dll_lua_replace) (lua_State *L, int idx);
216void (*dll_lua_remove) (lua_State *L, int idx);
217#endif
218#if LUA_VERSION_NUM >= 503
219void (*dll_lua_rotate) (lua_State *L, int idx, int n);
Bram Moolenaar9514b1f2015-06-25 18:27:32 +0200220void (*dll_lua_copy) (lua_State *L, int fromidx, int toidx);
Bram Moolenaar1dced572012-04-05 16:54:08 +0200221#endif
Bram Moolenaarbd2f3c32012-04-06 14:31:00 +0200222const char *(*dll_lua_typename) (lua_State *L, int tp);
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200223void (*dll_lua_close) (lua_State *L);
224int (*dll_lua_gettop) (lua_State *L);
225void (*dll_lua_settop) (lua_State *L, int idx);
226void (*dll_lua_pushvalue) (lua_State *L, int idx);
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200227int (*dll_lua_isnumber) (lua_State *L, int idx);
228int (*dll_lua_isstring) (lua_State *L, int idx);
229int (*dll_lua_type) (lua_State *L, int idx);
230int (*dll_lua_rawequal) (lua_State *L, int idx1, int idx2);
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200231int (*dll_lua_toboolean) (lua_State *L, int idx);
232const char *(*dll_lua_tolstring) (lua_State *L, int idx, size_t *len);
233void *(*dll_lua_touserdata) (lua_State *L, int idx);
234void (*dll_lua_pushnil) (lua_State *L);
235void (*dll_lua_pushnumber) (lua_State *L, lua_Number n);
236void (*dll_lua_pushinteger) (lua_State *L, lua_Integer n);
237void (*dll_lua_pushlstring) (lua_State *L, const char *s, size_t l);
238void (*dll_lua_pushstring) (lua_State *L, const char *s);
239const char *(*dll_lua_pushfstring) (lua_State *L, const char *fmt, ...);
240void (*dll_lua_pushcclosure) (lua_State *L, lua_CFunction fn, int n);
241void (*dll_lua_pushboolean) (lua_State *L, int b);
242void (*dll_lua_pushlightuserdata) (lua_State *L, void *p);
243void (*dll_lua_getfield) (lua_State *L, int idx, const char *k);
244void (*dll_lua_rawget) (lua_State *L, int idx);
Bram Moolenaar1dced572012-04-05 16:54:08 +0200245void (*dll_lua_rawgeti) (lua_State *L, int idx, int n);
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200246void (*dll_lua_createtable) (lua_State *L, int narr, int nrec);
247void *(*dll_lua_newuserdata) (lua_State *L, size_t sz);
248int (*dll_lua_getmetatable) (lua_State *L, int objindex);
249void (*dll_lua_setfield) (lua_State *L, int idx, const char *k);
250void (*dll_lua_rawset) (lua_State *L, int idx);
251void (*dll_lua_rawseti) (lua_State *L, int idx, int n);
252int (*dll_lua_setmetatable) (lua_State *L, int objindex);
Bram Moolenaarbd2f3c32012-04-06 14:31:00 +0200253int (*dll_lua_next) (lua_State *L, int idx);
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200254/* libs */
255int (*dll_luaopen_base) (lua_State *L);
256int (*dll_luaopen_table) (lua_State *L);
257int (*dll_luaopen_string) (lua_State *L);
258int (*dll_luaopen_math) (lua_State *L);
Bram Moolenaar16c98f92010-07-28 22:46:08 +0200259int (*dll_luaopen_io) (lua_State *L);
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200260int (*dll_luaopen_os) (lua_State *L);
261int (*dll_luaopen_package) (lua_State *L);
262int (*dll_luaopen_debug) (lua_State *L);
Bram Moolenaar16c98f92010-07-28 22:46:08 +0200263void (*dll_luaL_openlibs) (lua_State *L);
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200264
265typedef void **luaV_function;
266typedef struct {
267 const char *name;
268 luaV_function func;
269} luaV_Reg;
270
271static const luaV_Reg luaV_dll[] = {
272 /* lauxlib */
Bram Moolenaar1dced572012-04-05 16:54:08 +0200273#if LUA_VERSION_NUM <= 501
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200274 {"luaL_register", (luaV_function) &dll_luaL_register},
Bram Moolenaar1dced572012-04-05 16:54:08 +0200275 {"luaL_prepbuffer", (luaV_function) &dll_luaL_prepbuffer},
276 {"luaL_openlib", (luaV_function) &dll_luaL_openlib},
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200277 {"luaL_typerror", (luaV_function) &dll_luaL_typerror},
Bram Moolenaar1dced572012-04-05 16:54:08 +0200278 {"luaL_loadfile", (luaV_function) &dll_luaL_loadfile},
279 {"luaL_loadbuffer", (luaV_function) &dll_luaL_loadbuffer},
280#else
281 {"luaL_prepbuffsize", (luaV_function) &dll_luaL_prepbuffsize},
282 {"luaL_setfuncs", (luaV_function) &dll_luaL_setfuncs},
283 {"luaL_loadfilex", (luaV_function) &dll_luaL_loadfilex},
284 {"luaL_loadbufferx", (luaV_function) &dll_luaL_loadbufferx},
285 {"luaL_argerror", (luaV_function) &dll_luaL_argerror},
286#endif
Bram Moolenaarbd2f3c32012-04-06 14:31:00 +0200287 {"luaL_checkany", (luaV_function) &dll_luaL_checkany},
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200288 {"luaL_checklstring", (luaV_function) &dll_luaL_checklstring},
289 {"luaL_checkinteger", (luaV_function) &dll_luaL_checkinteger},
290 {"luaL_optinteger", (luaV_function) &dll_luaL_optinteger},
291 {"luaL_checktype", (luaV_function) &dll_luaL_checktype},
292 {"luaL_error", (luaV_function) &dll_luaL_error},
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200293 {"luaL_newstate", (luaV_function) &dll_luaL_newstate},
294 {"luaL_buffinit", (luaV_function) &dll_luaL_buffinit},
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200295 {"luaL_addlstring", (luaV_function) &dll_luaL_addlstring},
296 {"luaL_pushresult", (luaV_function) &dll_luaL_pushresult},
297 /* lua */
Bram Moolenaar1dced572012-04-05 16:54:08 +0200298#if LUA_VERSION_NUM <= 501
299 {"lua_tonumber", (luaV_function) &dll_lua_tonumber},
300 {"lua_tointeger", (luaV_function) &dll_lua_tointeger},
301 {"lua_call", (luaV_function) &dll_lua_call},
302 {"lua_pcall", (luaV_function) &dll_lua_pcall},
303#else
304 {"lua_tonumberx", (luaV_function) &dll_lua_tonumberx},
305 {"lua_tointegerx", (luaV_function) &dll_lua_tointegerx},
306 {"lua_callk", (luaV_function) &dll_lua_callk},
307 {"lua_pcallk", (luaV_function) &dll_lua_pcallk},
308 {"lua_getglobal", (luaV_function) &dll_lua_getglobal},
309 {"lua_setglobal", (luaV_function) &dll_lua_setglobal},
Bram Moolenaar1dced572012-04-05 16:54:08 +0200310#endif
Bram Moolenaar1f860d82015-06-27 18:36:16 +0200311#if LUA_VERSION_NUM <= 502
312 {"lua_replace", (luaV_function) &dll_lua_replace},
313 {"lua_remove", (luaV_function) &dll_lua_remove},
314#endif
315#if LUA_VERSION_NUM >= 503
316 {"lua_rotate", (luaV_function) &dll_lua_rotate},
317 {"lua_copy", (luaV_function) &dll_lua_copy},
318#endif
Bram Moolenaarbd2f3c32012-04-06 14:31:00 +0200319 {"lua_typename", (luaV_function) &dll_lua_typename},
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200320 {"lua_close", (luaV_function) &dll_lua_close},
321 {"lua_gettop", (luaV_function) &dll_lua_gettop},
322 {"lua_settop", (luaV_function) &dll_lua_settop},
323 {"lua_pushvalue", (luaV_function) &dll_lua_pushvalue},
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200324 {"lua_isnumber", (luaV_function) &dll_lua_isnumber},
325 {"lua_isstring", (luaV_function) &dll_lua_isstring},
326 {"lua_type", (luaV_function) &dll_lua_type},
327 {"lua_rawequal", (luaV_function) &dll_lua_rawequal},
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200328 {"lua_toboolean", (luaV_function) &dll_lua_toboolean},
329 {"lua_tolstring", (luaV_function) &dll_lua_tolstring},
330 {"lua_touserdata", (luaV_function) &dll_lua_touserdata},
331 {"lua_pushnil", (luaV_function) &dll_lua_pushnil},
332 {"lua_pushnumber", (luaV_function) &dll_lua_pushnumber},
333 {"lua_pushinteger", (luaV_function) &dll_lua_pushinteger},
334 {"lua_pushlstring", (luaV_function) &dll_lua_pushlstring},
335 {"lua_pushstring", (luaV_function) &dll_lua_pushstring},
336 {"lua_pushfstring", (luaV_function) &dll_lua_pushfstring},
337 {"lua_pushcclosure", (luaV_function) &dll_lua_pushcclosure},
338 {"lua_pushboolean", (luaV_function) &dll_lua_pushboolean},
339 {"lua_pushlightuserdata", (luaV_function) &dll_lua_pushlightuserdata},
340 {"lua_getfield", (luaV_function) &dll_lua_getfield},
341 {"lua_rawget", (luaV_function) &dll_lua_rawget},
Bram Moolenaar1dced572012-04-05 16:54:08 +0200342 {"lua_rawgeti", (luaV_function) &dll_lua_rawgeti},
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200343 {"lua_createtable", (luaV_function) &dll_lua_createtable},
344 {"lua_newuserdata", (luaV_function) &dll_lua_newuserdata},
345 {"lua_getmetatable", (luaV_function) &dll_lua_getmetatable},
346 {"lua_setfield", (luaV_function) &dll_lua_setfield},
347 {"lua_rawset", (luaV_function) &dll_lua_rawset},
348 {"lua_rawseti", (luaV_function) &dll_lua_rawseti},
349 {"lua_setmetatable", (luaV_function) &dll_lua_setmetatable},
Bram Moolenaarbd2f3c32012-04-06 14:31:00 +0200350 {"lua_next", (luaV_function) &dll_lua_next},
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200351 /* libs */
352 {"luaopen_base", (luaV_function) &dll_luaopen_base},
353 {"luaopen_table", (luaV_function) &dll_luaopen_table},
354 {"luaopen_string", (luaV_function) &dll_luaopen_string},
355 {"luaopen_math", (luaV_function) &dll_luaopen_math},
Bram Moolenaar16c98f92010-07-28 22:46:08 +0200356 {"luaopen_io", (luaV_function) &dll_luaopen_io},
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200357 {"luaopen_os", (luaV_function) &dll_luaopen_os},
358 {"luaopen_package", (luaV_function) &dll_luaopen_package},
359 {"luaopen_debug", (luaV_function) &dll_luaopen_debug},
Bram Moolenaar16c98f92010-07-28 22:46:08 +0200360 {"luaL_openlibs", (luaV_function) &dll_luaL_openlibs},
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200361 {NULL, NULL}
362};
363
Bram Moolenaar2334b6d2010-07-22 21:32:16 +0200364static HANDLE hinstLua = NULL;
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200365
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200366 static void
367end_dynamic_lua(void)
368{
369 if (hinstLua)
370 {
Bram Moolenaar2334b6d2010-07-22 21:32:16 +0200371 close_dll(hinstLua);
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200372 hinstLua = 0;
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200373 }
374}
375
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200376 static int
377lua_link_init(char *libname, int verbose)
378{
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200379 const luaV_Reg *reg;
380 if (hinstLua) return OK;
Bram Moolenaar2334b6d2010-07-22 21:32:16 +0200381 hinstLua = load_dll(libname);
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200382 if (!hinstLua)
383 {
384 if (verbose)
385 EMSG2(_(e_loadlib), libname);
386 return FAIL;
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200387 }
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200388 for (reg = luaV_dll; reg->func; reg++)
389 {
Bram Moolenaar2334b6d2010-07-22 21:32:16 +0200390 if ((*reg->func = symbol_from_dll(hinstLua, reg->name)) == NULL)
391 {
392 close_dll(hinstLua);
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200393 hinstLua = 0;
394 if (verbose)
395 EMSG2(_(e_loadfunc), reg->name);
396 return FAIL;
397 }
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200398 }
399 return OK;
400}
Bram Moolenaard90b6c02016-08-28 18:10:45 +0200401#endif /* DYNAMIC_LUA */
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200402
Bram Moolenaard90b6c02016-08-28 18:10:45 +0200403#if defined(DYNAMIC_LUA) || defined(PROTO)
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200404 int
405lua_enabled(int verbose)
406{
Bram Moolenaar25e4fcd2016-01-09 14:57:47 +0100407 return lua_link_init((char *)p_luadll, verbose) == OK;
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200408}
Bram Moolenaard90b6c02016-08-28 18:10:45 +0200409#endif
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200410
Bram Moolenaar1dced572012-04-05 16:54:08 +0200411#if LUA_VERSION_NUM > 501
412 static int
413luaL_typeerror (lua_State *L, int narg, const char *tname)
414{
415 const char *msg = lua_pushfstring(L, "%s expected, got %s",
Bram Moolenaardb913952012-06-29 12:54:53 +0200416 tname, luaL_typename(L, narg));
Bram Moolenaar1dced572012-04-05 16:54:08 +0200417 return luaL_argerror(L, narg, msg);
418}
419#endif
420
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200421
422/* ======= Internal ======= */
423
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200424 static void
425luaV_newmetatable(lua_State *L, const char *tname)
426{
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200427 lua_newtable(L);
428 lua_pushlightuserdata(L, (void *) tname);
429 lua_pushvalue(L, -2);
430 lua_rawset(L, LUA_REGISTRYINDEX);
431}
432
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200433 static void *
434luaV_toudata(lua_State *L, int ud, const char *tname)
435{
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200436 void *p = lua_touserdata(L, ud);
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200437
438 if (p != NULL) /* value is userdata? */
439 {
440 if (lua_getmetatable(L, ud)) /* does it have a metatable? */
441 {
442 luaV_getfield(L, tname); /* get metatable */
443 if (lua_rawequal(L, -1, -2)) /* MTs match? */
444 {
445 lua_pop(L, 2); /* MTs */
446 return p;
447 }
448 }
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200449 }
450 return NULL;
451}
452
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200453 static void *
Bram Moolenaar1dced572012-04-05 16:54:08 +0200454luaV_checkcache(lua_State *L, void *p)
455{
456 luaV_getudata(L, p);
457 if (lua_isnil(L, -1)) luaL_error(L, "invalid object");
458 lua_pop(L, 1);
459 return p;
460}
461
462#define luaV_unbox(L,luatyp,ud) (*((luatyp *) lua_touserdata((L),(ud))))
463
464#define luaV_checkvalid(L,luatyp,ud) \
465 luaV_checkcache((L), (void *) luaV_unbox((L),luatyp,(ud)))
466
467 static void *
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200468luaV_checkudata(lua_State *L, int ud, const char *tname)
469{
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200470 void *p = luaV_toudata(L, ud, tname);
Bram Moolenaar1dced572012-04-05 16:54:08 +0200471 if (p == NULL) luaL_typeerror(L, ud, tname);
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200472 return p;
473}
474
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200475 static void
476luaV_pushtypval(lua_State *L, typval_T *tv)
477{
Bram Moolenaar1dced572012-04-05 16:54:08 +0200478 if (tv == NULL)
479 {
480 lua_pushnil(L);
481 return;
482 }
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200483 switch (tv->v_type)
484 {
485 case VAR_STRING:
Bram Moolenaard04da7c2012-10-14 03:41:59 +0200486 lua_pushstring(L, tv->vval.v_string == NULL
487 ? "" : (char *)tv->vval.v_string);
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200488 break;
489 case VAR_NUMBER:
490 lua_pushinteger(L, (int) tv->vval.v_number);
491 break;
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200492#ifdef FEAT_FLOAT
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200493 case VAR_FLOAT:
494 lua_pushnumber(L, (lua_Number) tv->vval.v_float);
495 break;
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200496#endif
Bram Moolenaar1dced572012-04-05 16:54:08 +0200497 case VAR_LIST:
498 luaV_pushlist(L, tv->vval.v_list);
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200499 break;
Bram Moolenaar1dced572012-04-05 16:54:08 +0200500 case VAR_DICT:
501 luaV_pushdict(L, tv->vval.v_dict);
502 break;
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100503 case VAR_SPECIAL:
504 if (tv->vval.v_number <= VVAL_TRUE)
505 lua_pushinteger(L, (int) tv->vval.v_number);
506 else
507 lua_pushnil(L);
508 break;
Bram Moolenaar1dced572012-04-05 16:54:08 +0200509 default:
510 lua_pushnil(L);
511 }
512}
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200513
Bram Moolenaar1dced572012-04-05 16:54:08 +0200514/* converts lua value at 'pos' to typval 'tv' */
515 static void
516luaV_totypval (lua_State *L, int pos, typval_T *tv)
517{
518 switch(lua_type(L, pos)) {
519 case LUA_TBOOLEAN:
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100520 tv->v_type = VAR_SPECIAL;
Bram Moolenaar1dced572012-04-05 16:54:08 +0200521 tv->vval.v_number = (varnumber_T) lua_toboolean(L, pos);
522 break;
523 case LUA_TSTRING:
524 tv->v_type = VAR_STRING;
525 tv->vval.v_string = vim_strsave((char_u *) lua_tostring(L, pos));
526 break;
527 case LUA_TNUMBER:
528#ifdef FEAT_FLOAT
529 tv->v_type = VAR_FLOAT;
530 tv->vval.v_float = (float_T) lua_tonumber(L, pos);
531#else
532 tv->v_type = VAR_NUMBER;
533 tv->vval.v_number = (varnumber_T) lua_tointeger(L, pos);
534#endif
535 break;
536 case LUA_TUSERDATA: {
537 void *p = lua_touserdata(L, pos);
538 if (lua_getmetatable(L, pos)) /* has metatable? */
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200539 {
Bram Moolenaar1dced572012-04-05 16:54:08 +0200540 /* check list */
541 luaV_getfield(L, LUAVIM_LIST);
542 if (lua_rawequal(L, -1, -2))
Bram Moolenaar2334b6d2010-07-22 21:32:16 +0200543 {
Bram Moolenaar1dced572012-04-05 16:54:08 +0200544 tv->v_type = VAR_LIST;
545 tv->vval.v_list = *((luaV_List *) p);
546 ++tv->vval.v_list->lv_refcount;
547 lua_pop(L, 2); /* MTs */
548 return;
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200549 }
Bram Moolenaar1dced572012-04-05 16:54:08 +0200550 /* check dict */
551 luaV_getfield(L, LUAVIM_DICT);
552 if (lua_rawequal(L, -1, -3))
553 {
554 tv->v_type = VAR_DICT;
555 tv->vval.v_dict = *((luaV_Dict *) p);
556 ++tv->vval.v_dict->dv_refcount;
557 lua_pop(L, 3); /* MTs */
558 return;
559 }
560 lua_pop(L, 3); /* MTs */
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200561 }
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200562 break;
563 }
564 default:
Bram Moolenaar1dced572012-04-05 16:54:08 +0200565 tv->v_type = VAR_NUMBER;
566 tv->vval.v_number = 0;
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200567 }
568}
569
570/* similar to luaL_addlstring, but replaces \0 with \n if toline and
571 * \n with \0 otherwise */
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200572 static void
573luaV_addlstring(luaL_Buffer *b, const char *s, size_t l, int toline)
574{
575 while (l--)
576 {
577 if (*s == '\0' && toline)
578 luaL_addchar(b, '\n');
579 else if (*s == '\n' && !toline)
580 luaL_addchar(b, '\0');
581 else
582 luaL_addchar(b, *s);
583 s++;
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200584 }
585}
586
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200587 static void
588luaV_pushline(lua_State *L, buf_T *buf, linenr_T n)
589{
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200590 const char *s = (const char *) ml_get_buf(buf, n, FALSE);
591 luaL_Buffer b;
592 luaL_buffinit(L, &b);
593 luaV_addlstring(&b, s, strlen(s), 0);
594 luaL_pushresult(&b);
595}
596
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200597 static char_u *
598luaV_toline(lua_State *L, int pos)
599{
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200600 size_t l;
601 const char *s = lua_tolstring(L, pos, &l);
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200602
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200603 luaL_Buffer b;
604 luaL_buffinit(L, &b);
605 luaV_addlstring(&b, s, l, 1);
606 luaL_pushresult(&b);
607 return (char_u *) lua_tostring(L, -1);
608}
609
610/* pops a string s from the top of the stack and calls mf(t) for pieces t of
611 * s separated by newlines */
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200612 static void
613luaV_msgfunc(lua_State *L, msgfunc_T mf)
614{
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200615 luaL_Buffer b;
616 size_t l;
617 const char *p, *s = lua_tolstring(L, -1, &l);
618 luaL_buffinit(L, &b);
619 luaV_addlstring(&b, s, l, 0);
620 luaL_pushresult(&b);
621 /* break string */
622 p = s = lua_tolstring(L, -1, &l);
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200623 while (l--)
624 {
625 if (*p++ == '\0') /* break? */
626 {
627 mf((char_u *) s);
628 s = p;
629 }
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200630 }
631 mf((char_u *) s);
632 lua_pop(L, 2); /* original and modified strings */
633}
634
Bram Moolenaar1dced572012-04-05 16:54:08 +0200635#define luaV_newtype(typ,tname,luatyp,luatname) \
636 static luatyp * \
637 luaV_new##tname (lua_State *L, typ *obj) \
638 { \
639 luatyp *o = (luatyp *) lua_newuserdata(L, sizeof(luatyp)); \
640 *o = obj; \
641 luaV_setudata(L, obj); /* cache[obj] = udata */ \
642 luaV_getfield(L, luatname); \
643 lua_setmetatable(L, -2); \
644 return o; \
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200645 }
Bram Moolenaar1dced572012-04-05 16:54:08 +0200646
647#define luaV_pushtype(typ,tname,luatyp) \
648 static luatyp * \
649 luaV_push##tname (lua_State *L, typ *obj) \
650 { \
651 luatyp *o = NULL; \
652 if (obj == NULL) \
653 lua_pushnil(L); \
654 else { \
655 luaV_getudata(L, obj); \
656 if (lua_isnil(L, -1)) /* not interned? */ \
657 { \
658 lua_pop(L, 1); \
659 o = luaV_new##tname(L, obj); \
660 } \
661 else \
662 o = (luatyp *) lua_touserdata(L, -1); \
663 } \
664 return o; \
665 }
666
667#define luaV_type_tostring(tname,luatname) \
668 static int \
669 luaV_##tname##_tostring (lua_State *L) \
670 { \
671 lua_pushfstring(L, "%s: %p", luatname, lua_touserdata(L, 1)); \
672 return 1; \
673 }
674
Bram Moolenaar1dced572012-04-05 16:54:08 +0200675/* ======= List type ======= */
676
677 static luaV_List *
678luaV_newlist (lua_State *L, list_T *lis)
679{
680 luaV_List *l = (luaV_List *) lua_newuserdata(L, sizeof(luaV_List));
681 *l = lis;
682 lis->lv_refcount++; /* reference in Lua */
683 luaV_setudata(L, lis); /* cache[lis] = udata */
684 luaV_getfield(L, LUAVIM_LIST);
685 lua_setmetatable(L, -2);
686 return l;
687}
688
689luaV_pushtype(list_T, list, luaV_List)
690luaV_type_tostring(list, LUAVIM_LIST)
691
692 static int
Bram Moolenaar1dced572012-04-05 16:54:08 +0200693luaV_list_len (lua_State *L)
694{
695 list_T *l = luaV_unbox(L, luaV_List, 1);
696 lua_pushinteger(L, (l == NULL) ? 0 : (int) l->lv_len);
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200697 return 1;
698}
699
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200700 static int
Bram Moolenaar1dced572012-04-05 16:54:08 +0200701luaV_list_iter (lua_State *L)
702{
703 listitem_T *li = (listitem_T *) lua_touserdata(L, lua_upvalueindex(2));
704 if (li == NULL) return 0;
705 luaV_pushtypval(L, &li->li_tv);
706 lua_pushlightuserdata(L, (void *) li->li_next);
707 lua_replace(L, lua_upvalueindex(2));
708 return 1;
709}
710
711 static int
712luaV_list_call (lua_State *L)
713{
714 list_T *l = luaV_unbox(L, luaV_List, 1);
715 lua_pushvalue(L, lua_upvalueindex(1)); /* pass cache table along */
716 lua_pushlightuserdata(L, (void *) l->lv_first);
717 lua_pushcclosure(L, luaV_list_iter, 2);
718 return 1;
719}
720
721 static int
722luaV_list_index (lua_State *L)
723{
724 list_T *l = luaV_unbox(L, luaV_List, 1);
725 if (lua_isnumber(L, 2)) /* list item? */
726 {
727 listitem_T *li = list_find(l, (long) luaL_checkinteger(L, 2));
728 if (li == NULL)
729 lua_pushnil(L);
730 else
731 luaV_pushtypval(L, &li->li_tv);
732 }
733 else if (lua_isstring(L, 2)) /* method? */
734 {
735 const char *s = lua_tostring(L, 2);
736 if (strncmp(s, "add", 3) == 0
Bram Moolenaarb3766472013-04-15 13:49:21 +0200737 || strncmp(s, "insert", 6) == 0)
Bram Moolenaar1dced572012-04-05 16:54:08 +0200738 {
739 lua_getmetatable(L, 1);
740 lua_getfield(L, -1, s);
741 }
742 else
743 lua_pushnil(L);
744 }
745 else
746 lua_pushnil(L);
747 return 1;
748}
749
750 static int
751luaV_list_newindex (lua_State *L)
752{
753 list_T *l = luaV_unbox(L, luaV_List, 1);
754 long n = (long) luaL_checkinteger(L, 2);
755 listitem_T *li;
756 if (l->lv_lock)
757 luaL_error(L, "list is locked");
758 li = list_find(l, n);
759 if (li == NULL) return 0;
760 if (lua_isnil(L, 3)) /* remove? */
761 {
Bram Moolenaar3ec7f4e2014-05-07 17:31:37 +0200762 vimlist_remove(l, li, li);
Bram Moolenaar1dced572012-04-05 16:54:08 +0200763 clear_tv(&li->li_tv);
764 vim_free(li);
765 }
766 else
767 {
768 typval_T v;
769 luaV_totypval(L, 3, &v);
770 clear_tv(&li->li_tv);
771 copy_tv(&v, &li->li_tv);
Bram Moolenaarb3766472013-04-15 13:49:21 +0200772 clear_tv(&v);
Bram Moolenaar1dced572012-04-05 16:54:08 +0200773 }
774 return 0;
775}
776
777 static int
778luaV_list_add (lua_State *L)
779{
780 luaV_List *lis = luaV_checkudata(L, 1, LUAVIM_LIST);
781 list_T *l = (list_T *) luaV_checkcache(L, (void *) *lis);
Bram Moolenaarb3766472013-04-15 13:49:21 +0200782 typval_T v;
Bram Moolenaar1dced572012-04-05 16:54:08 +0200783 if (l->lv_lock)
784 luaL_error(L, "list is locked");
Bram Moolenaarb3766472013-04-15 13:49:21 +0200785 lua_settop(L, 2);
786 luaV_totypval(L, 2, &v);
787 if (list_append_tv(l, &v) == FAIL)
Bram Moolenaar1dced572012-04-05 16:54:08 +0200788 {
Bram Moolenaarb3766472013-04-15 13:49:21 +0200789 clear_tv(&v);
790 luaL_error(L, "Failed to add item to list");
Bram Moolenaar1dced572012-04-05 16:54:08 +0200791 }
Bram Moolenaarb3766472013-04-15 13:49:21 +0200792 clear_tv(&v);
Bram Moolenaar1dced572012-04-05 16:54:08 +0200793 lua_settop(L, 1);
794 return 1;
795}
796
797 static int
798luaV_list_insert (lua_State *L)
799{
800 luaV_List *lis = luaV_checkudata(L, 1, LUAVIM_LIST);
801 list_T *l = (list_T *) luaV_checkcache(L, (void *) *lis);
Bram Moolenaar46538ee2015-02-17 16:28:55 +0100802 long pos = (long) luaL_optinteger(L, 3, 0);
Bram Moolenaar1dced572012-04-05 16:54:08 +0200803 listitem_T *li = NULL;
804 typval_T v;
805 if (l->lv_lock)
806 luaL_error(L, "list is locked");
807 if (pos < l->lv_len)
808 {
809 li = list_find(l, pos);
810 if (li == NULL)
811 luaL_error(L, "invalid position");
812 }
813 lua_settop(L, 2);
814 luaV_totypval(L, 2, &v);
Bram Moolenaarb3766472013-04-15 13:49:21 +0200815 if (list_insert_tv(l, &v, li) == FAIL)
816 {
817 clear_tv(&v);
818 luaL_error(L, "Failed to add item to list");
819 }
820 clear_tv(&v);
Bram Moolenaar1dced572012-04-05 16:54:08 +0200821 lua_settop(L, 1);
822 return 1;
823}
824
825static const luaL_Reg luaV_List_mt[] = {
826 {"__tostring", luaV_list_tostring},
Bram Moolenaar1dced572012-04-05 16:54:08 +0200827 {"__len", luaV_list_len},
828 {"__call", luaV_list_call},
829 {"__index", luaV_list_index},
830 {"__newindex", luaV_list_newindex},
831 {"add", luaV_list_add},
832 {"insert", luaV_list_insert},
833 {NULL, NULL}
834};
835
836
837/* ======= Dict type ======= */
838
839 static luaV_Dict *
840luaV_newdict (lua_State *L, dict_T *dic)
841{
842 luaV_Dict *d = (luaV_Dict *) lua_newuserdata(L, sizeof(luaV_Dict));
843 *d = dic;
844 dic->dv_refcount++; /* reference in Lua */
845 luaV_setudata(L, dic); /* cache[dic] = udata */
846 luaV_getfield(L, LUAVIM_DICT);
847 lua_setmetatable(L, -2);
848 return d;
849}
850
851luaV_pushtype(dict_T, dict, luaV_Dict)
852luaV_type_tostring(dict, LUAVIM_DICT)
853
854 static int
Bram Moolenaar1dced572012-04-05 16:54:08 +0200855luaV_dict_len (lua_State *L)
856{
857 dict_T *d = luaV_unbox(L, luaV_Dict, 1);
858 lua_pushinteger(L, (d == NULL) ? 0 : (int) d->dv_hashtab.ht_used);
859 return 1;
860}
861
862 static int
Bram Moolenaarfeeaa682013-02-14 22:19:51 +0100863luaV_dict_iter (lua_State *L UNUSED)
Bram Moolenaar1dced572012-04-05 16:54:08 +0200864{
Bram Moolenaarfeeaa682013-02-14 22:19:51 +0100865#ifdef FEAT_EVAL
Bram Moolenaar1dced572012-04-05 16:54:08 +0200866 hashitem_T *hi = (hashitem_T *) lua_touserdata(L, lua_upvalueindex(2));
867 int n = lua_tointeger(L, lua_upvalueindex(3));
868 dictitem_T *di;
869 if (n <= 0) return 0;
870 while (HASHITEM_EMPTY(hi)) hi++;
871 di = dict_lookup(hi);
872 lua_pushstring(L, (char *) hi->hi_key);
873 luaV_pushtypval(L, &di->di_tv);
874 lua_pushlightuserdata(L, (void *) (hi + 1));
875 lua_replace(L, lua_upvalueindex(2));
876 lua_pushinteger(L, n - 1);
877 lua_replace(L, lua_upvalueindex(3));
878 return 2;
Bram Moolenaarfeeaa682013-02-14 22:19:51 +0100879#else
880 return 0;
881#endif
Bram Moolenaar1dced572012-04-05 16:54:08 +0200882}
883
884 static int
885luaV_dict_call (lua_State *L)
886{
887 dict_T *d = luaV_unbox(L, luaV_Dict, 1);
888 hashtab_T *ht = &d->dv_hashtab;
889 lua_pushvalue(L, lua_upvalueindex(1)); /* pass cache table along */
890 lua_pushlightuserdata(L, (void *) ht->ht_array);
891 lua_pushinteger(L, ht->ht_used); /* # remaining items */
892 lua_pushcclosure(L, luaV_dict_iter, 3);
893 return 1;
894}
895
896 static int
897luaV_dict_index (lua_State *L)
898{
899 dict_T *d = luaV_unbox(L, luaV_Dict, 1);
900 char_u *key = (char_u *) luaL_checkstring(L, 2);
901 dictitem_T *di = dict_find(d, key, -1);
902 if (di == NULL)
903 lua_pushnil(L);
904 else
905 luaV_pushtypval(L, &di->di_tv);
906 return 1;
907}
908
909 static int
910luaV_dict_newindex (lua_State *L)
911{
912 dict_T *d = luaV_unbox(L, luaV_Dict, 1);
913 char_u *key = (char_u *) luaL_checkstring(L, 2);
914 dictitem_T *di;
915 if (d->dv_lock)
916 luaL_error(L, "dict is locked");
917 di = dict_find(d, key, -1);
918 if (di == NULL) /* non-existing key? */
919 {
920 if (lua_isnil(L, 3)) return 0;
921 di = dictitem_alloc(key);
922 if (di == NULL) return 0;
923 if (dict_add(d, di) == FAIL)
924 {
925 vim_free(di);
926 return 0;
927 }
928 }
929 else
930 clear_tv(&di->di_tv);
931 if (lua_isnil(L, 3)) /* remove? */
932 {
933 hashitem_T *hi = hash_find(&d->dv_hashtab, di->di_key);
934 hash_remove(&d->dv_hashtab, hi);
935 dictitem_free(di);
936 }
937 else {
938 typval_T v;
939 luaV_totypval(L, 3, &v);
940 copy_tv(&v, &di->di_tv);
Bram Moolenaarb3766472013-04-15 13:49:21 +0200941 clear_tv(&v);
Bram Moolenaar1dced572012-04-05 16:54:08 +0200942 }
943 return 0;
944}
945
946static const luaL_Reg luaV_Dict_mt[] = {
947 {"__tostring", luaV_dict_tostring},
Bram Moolenaar1dced572012-04-05 16:54:08 +0200948 {"__len", luaV_dict_len},
949 {"__call", luaV_dict_call},
950 {"__index", luaV_dict_index},
951 {"__newindex", luaV_dict_newindex},
952 {NULL, NULL}
953};
954
955
956/* ======= Buffer type ======= */
957
958luaV_newtype(buf_T, buffer, luaV_Buffer, LUAVIM_BUFFER)
959luaV_pushtype(buf_T, buffer, luaV_Buffer)
960luaV_type_tostring(buffer, LUAVIM_BUFFER)
961
962 static int
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200963luaV_buffer_len(lua_State *L)
964{
Bram Moolenaar1dced572012-04-05 16:54:08 +0200965 buf_T *b = (buf_T *) luaV_checkvalid(L, luaV_Buffer, 1);
966 lua_pushinteger(L, b->b_ml.ml_line_count);
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200967 return 1;
968}
969
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200970 static int
971luaV_buffer_call(lua_State *L)
972{
Bram Moolenaar1dced572012-04-05 16:54:08 +0200973 buf_T *b = (buf_T *) luaV_checkvalid(L, luaV_Buffer, 1);
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200974 lua_settop(L, 1);
Bram Moolenaar1dced572012-04-05 16:54:08 +0200975 set_curbuf(b, DOBUF_SPLIT);
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200976 return 1;
977}
978
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200979 static int
980luaV_buffer_index(lua_State *L)
981{
Bram Moolenaar1dced572012-04-05 16:54:08 +0200982 buf_T *b = (buf_T *) luaV_checkvalid(L, luaV_Buffer, 1);
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200983 linenr_T n = (linenr_T) lua_tointeger(L, 2);
Bram Moolenaar1dced572012-04-05 16:54:08 +0200984 if (n > 0 && n <= b->b_ml.ml_line_count)
985 luaV_pushline(L, b, n);
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200986 else if (lua_isstring(L, 2))
987 {
988 const char *s = lua_tostring(L, 2);
989 if (strncmp(s, "name", 4) == 0)
Bram Moolenaar1dced572012-04-05 16:54:08 +0200990 lua_pushstring(L, (char *) b->b_sfname);
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200991 else if (strncmp(s, "fname", 5) == 0)
Bram Moolenaar1dced572012-04-05 16:54:08 +0200992 lua_pushstring(L, (char *) b->b_ffname);
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200993 else if (strncmp(s, "number", 6) == 0)
Bram Moolenaar1dced572012-04-05 16:54:08 +0200994 lua_pushinteger(L, b->b_fnum);
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200995 /* methods */
996 else if (strncmp(s, "insert", 6) == 0
997 || strncmp(s, "next", 4) == 0
998 || strncmp(s, "previous", 8) == 0
999 || strncmp(s, "isvalid", 7) == 0)
1000 {
1001 lua_getmetatable(L, 1);
1002 lua_getfield(L, -1, s);
1003 }
1004 else
1005 lua_pushnil(L);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001006 }
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001007 else
1008 lua_pushnil(L);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001009 return 1;
1010}
1011
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001012 static int
1013luaV_buffer_newindex(lua_State *L)
1014{
Bram Moolenaar1dced572012-04-05 16:54:08 +02001015 buf_T *b = (buf_T *) luaV_checkvalid(L, luaV_Buffer, 1);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001016 linenr_T n = (linenr_T) luaL_checkinteger(L, 2);
1017#ifdef HAVE_SANDBOX
1018 luaV_checksandbox(L);
1019#endif
Bram Moolenaar1dced572012-04-05 16:54:08 +02001020 if (n < 1 || n > b->b_ml.ml_line_count)
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001021 luaL_error(L, "invalid line number");
1022 if (lua_isnil(L, 3)) /* delete line */
1023 {
1024 buf_T *buf = curbuf;
Bram Moolenaar1dced572012-04-05 16:54:08 +02001025 curbuf = b;
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001026 if (u_savedel(n, 1L) == FAIL)
1027 {
1028 curbuf = buf;
1029 luaL_error(L, "cannot save undo information");
1030 }
1031 else if (ml_delete(n, FALSE) == FAIL)
1032 {
1033 curbuf = buf;
1034 luaL_error(L, "cannot delete line");
1035 }
1036 else {
1037 deleted_lines_mark(n, 1L);
Bram Moolenaar1dced572012-04-05 16:54:08 +02001038 if (b == curwin->w_buffer) /* fix cursor in current window? */
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001039 {
1040 if (curwin->w_cursor.lnum >= n)
1041 {
1042 if (curwin->w_cursor.lnum > n)
1043 {
1044 curwin->w_cursor.lnum -= 1;
1045 check_cursor_col();
1046 }
1047 else check_cursor();
1048 changed_cline_bef_curs();
1049 }
1050 invalidate_botline();
1051 }
1052 }
1053 curbuf = buf;
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001054 }
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001055 else if (lua_isstring(L, 3)) /* update line */
1056 {
1057 buf_T *buf = curbuf;
Bram Moolenaar1dced572012-04-05 16:54:08 +02001058 curbuf = b;
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001059 if (u_savesub(n) == FAIL)
1060 {
1061 curbuf = buf;
1062 luaL_error(L, "cannot save undo information");
1063 }
1064 else if (ml_replace(n, luaV_toline(L, 3), TRUE) == FAIL)
1065 {
1066 curbuf = buf;
1067 luaL_error(L, "cannot replace line");
1068 }
1069 else changed_bytes(n, 0);
1070 curbuf = buf;
Bram Moolenaar1dced572012-04-05 16:54:08 +02001071 if (b == curwin->w_buffer)
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001072 check_cursor_col();
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001073 }
1074 else
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001075 luaL_error(L, "wrong argument to change line");
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001076 return 0;
1077}
1078
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001079 static int
1080luaV_buffer_insert(lua_State *L)
1081{
Bram Moolenaar1dced572012-04-05 16:54:08 +02001082 luaV_Buffer *lb = luaV_checkudata(L, 1, LUAVIM_BUFFER);
1083 buf_T *b = (buf_T *) luaV_checkcache(L, (void *) *lb);
1084 linenr_T last = b->b_ml.ml_line_count;
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001085 linenr_T n = (linenr_T) luaL_optinteger(L, 3, last);
1086 buf_T *buf;
1087 luaL_checktype(L, 2, LUA_TSTRING);
1088#ifdef HAVE_SANDBOX
1089 luaV_checksandbox(L);
1090#endif
1091 /* fix insertion line */
1092 if (n < 0) n = 0;
1093 if (n > last) n = last;
1094 /* insert */
1095 buf = curbuf;
Bram Moolenaar1dced572012-04-05 16:54:08 +02001096 curbuf = b;
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001097 if (u_save(n, n + 1) == FAIL)
1098 {
1099 curbuf = buf;
1100 luaL_error(L, "cannot save undo information");
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001101 }
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001102 else if (ml_append(n, luaV_toline(L, 2), 0, FALSE) == FAIL)
1103 {
1104 curbuf = buf;
1105 luaL_error(L, "cannot insert line");
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001106 }
1107 else
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001108 appended_lines_mark(n, 1L);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001109 curbuf = buf;
1110 update_screen(VALID);
1111 return 0;
1112}
1113
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001114 static int
1115luaV_buffer_next(lua_State *L)
1116{
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001117 luaV_Buffer *b = luaV_checkudata(L, 1, LUAVIM_BUFFER);
Bram Moolenaar1dced572012-04-05 16:54:08 +02001118 buf_T *buf = (buf_T *) luaV_checkcache(L, (void *) *b);
1119 luaV_pushbuffer(L, buf->b_next);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001120 return 1;
1121}
1122
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001123 static int
1124luaV_buffer_previous(lua_State *L)
1125{
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001126 luaV_Buffer *b = luaV_checkudata(L, 1, LUAVIM_BUFFER);
Bram Moolenaar1dced572012-04-05 16:54:08 +02001127 buf_T *buf = (buf_T *) luaV_checkcache(L, (void *) *b);
1128 luaV_pushbuffer(L, buf->b_prev);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001129 return 1;
1130}
1131
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001132 static int
1133luaV_buffer_isvalid(lua_State *L)
1134{
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001135 luaV_Buffer *b = luaV_checkudata(L, 1, LUAVIM_BUFFER);
Bram Moolenaar1dced572012-04-05 16:54:08 +02001136 luaV_getudata(L, *b);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001137 lua_pushboolean(L, !lua_isnil(L, -1));
1138 return 1;
1139}
1140
1141static const luaL_Reg luaV_Buffer_mt[] = {
1142 {"__tostring", luaV_buffer_tostring},
1143 {"__len", luaV_buffer_len},
1144 {"__call", luaV_buffer_call},
1145 {"__index", luaV_buffer_index},
1146 {"__newindex", luaV_buffer_newindex},
1147 {"insert", luaV_buffer_insert},
1148 {"next", luaV_buffer_next},
1149 {"previous", luaV_buffer_previous},
1150 {"isvalid", luaV_buffer_isvalid},
1151 {NULL, NULL}
1152};
1153
1154
1155/* ======= Window type ======= */
1156
Bram Moolenaar1dced572012-04-05 16:54:08 +02001157luaV_newtype(win_T, window, luaV_Window, LUAVIM_WINDOW)
1158luaV_pushtype(win_T, window, luaV_Window)
1159luaV_type_tostring(window, LUAVIM_WINDOW)
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001160
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001161 static int
1162luaV_window_call(lua_State *L)
1163{
Bram Moolenaar1dced572012-04-05 16:54:08 +02001164 win_T *w = (win_T *) luaV_checkvalid(L, luaV_Window, 1);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001165 lua_settop(L, 1);
Bram Moolenaar1dced572012-04-05 16:54:08 +02001166 win_goto(w);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001167 return 1;
1168}
1169
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001170 static int
1171luaV_window_index(lua_State *L)
1172{
Bram Moolenaar1dced572012-04-05 16:54:08 +02001173 win_T *w = (win_T *) luaV_checkvalid(L, luaV_Window, 1);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001174 const char *s = luaL_checkstring(L, 2);
1175 if (strncmp(s, "buffer", 6) == 0)
Bram Moolenaar1dced572012-04-05 16:54:08 +02001176 luaV_pushbuffer(L, w->w_buffer);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001177 else if (strncmp(s, "line", 4) == 0)
Bram Moolenaar1dced572012-04-05 16:54:08 +02001178 lua_pushinteger(L, w->w_cursor.lnum);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001179 else if (strncmp(s, "col", 3) == 0)
Bram Moolenaar1dced572012-04-05 16:54:08 +02001180 lua_pushinteger(L, w->w_cursor.col + 1);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001181 else if (strncmp(s, "width", 5) == 0)
Bram Moolenaar1dced572012-04-05 16:54:08 +02001182 lua_pushinteger(L, W_WIDTH(w));
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001183 else if (strncmp(s, "height", 6) == 0)
Bram Moolenaar1dced572012-04-05 16:54:08 +02001184 lua_pushinteger(L, w->w_height);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001185 /* methods */
1186 else if (strncmp(s, "next", 4) == 0
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001187 || strncmp(s, "previous", 8) == 0
1188 || strncmp(s, "isvalid", 7) == 0)
1189 {
1190 lua_getmetatable(L, 1);
1191 lua_getfield(L, -1, s);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001192 }
1193 else
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001194 lua_pushnil(L);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001195 return 1;
1196}
1197
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001198 static int
1199luaV_window_newindex (lua_State *L)
1200{
Bram Moolenaar1dced572012-04-05 16:54:08 +02001201 win_T *w = (win_T *) luaV_checkvalid(L, luaV_Window, 1);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001202 const char *s = luaL_checkstring(L, 2);
1203 int v = luaL_checkinteger(L, 3);
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001204 if (strncmp(s, "line", 4) == 0)
1205 {
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001206#ifdef HAVE_SANDBOX
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001207 luaV_checksandbox(L);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001208#endif
Bram Moolenaar1dced572012-04-05 16:54:08 +02001209 if (v < 1 || v > w->w_buffer->b_ml.ml_line_count)
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001210 luaL_error(L, "line out of range");
Bram Moolenaar1dced572012-04-05 16:54:08 +02001211 w->w_cursor.lnum = v;
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001212 update_screen(VALID);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001213 }
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001214 else if (strncmp(s, "col", 3) == 0)
1215 {
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001216#ifdef HAVE_SANDBOX
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001217 luaV_checksandbox(L);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001218#endif
Bram Moolenaar1dced572012-04-05 16:54:08 +02001219 w->w_cursor.col = v - 1;
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001220 update_screen(VALID);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001221 }
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001222 else if (strncmp(s, "width", 5) == 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_setwidth(v);
1230 curwin = win;
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001231 }
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001232 else if (strncmp(s, "height", 6) == 0)
1233 {
1234 win_T *win = curwin;
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001235#ifdef FEAT_GUI
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001236 need_mouse_correct = TRUE;
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001237#endif
Bram Moolenaar1dced572012-04-05 16:54:08 +02001238 curwin = w;
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001239 win_setheight(v);
1240 curwin = win;
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001241 }
1242 else
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001243 luaL_error(L, "invalid window property: `%s'", s);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001244 return 0;
1245}
1246
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001247 static int
1248luaV_window_next(lua_State *L)
1249{
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001250 luaV_Window *w = luaV_checkudata(L, 1, LUAVIM_WINDOW);
Bram Moolenaar1dced572012-04-05 16:54:08 +02001251 win_T *win = (win_T *) luaV_checkcache(L, (void *) *w);
1252 luaV_pushwindow(L, win->w_next);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001253 return 1;
1254}
1255
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001256 static int
1257luaV_window_previous(lua_State *L)
1258{
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001259 luaV_Window *w = luaV_checkudata(L, 1, LUAVIM_WINDOW);
Bram Moolenaar1dced572012-04-05 16:54:08 +02001260 win_T *win = (win_T *) luaV_checkcache(L, (void *) *w);
1261 luaV_pushwindow(L, win->w_prev);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001262 return 1;
1263}
1264
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001265 static int
1266luaV_window_isvalid(lua_State *L)
1267{
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001268 luaV_Window *w = luaV_checkudata(L, 1, LUAVIM_WINDOW);
Bram Moolenaar1dced572012-04-05 16:54:08 +02001269 luaV_getudata(L, *w);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001270 lua_pushboolean(L, !lua_isnil(L, -1));
1271 return 1;
1272}
1273
1274static const luaL_Reg luaV_Window_mt[] = {
1275 {"__tostring", luaV_window_tostring},
1276 {"__call", luaV_window_call},
1277 {"__index", luaV_window_index},
1278 {"__newindex", luaV_window_newindex},
1279 {"next", luaV_window_next},
1280 {"previous", luaV_window_previous},
1281 {"isvalid", luaV_window_isvalid},
1282 {NULL, NULL}
1283};
1284
1285
1286/* ======= Vim module ======= */
1287
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001288 static int
1289luaV_print(lua_State *L)
1290{
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001291 int i, n = lua_gettop(L); /* nargs */
1292 const char *s;
1293 size_t l;
1294 luaL_Buffer b;
1295 luaL_buffinit(L, &b);
1296 lua_getglobal(L, "tostring");
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001297 for (i = 1; i <= n; i++)
1298 {
1299 lua_pushvalue(L, -1); /* tostring */
1300 lua_pushvalue(L, i); /* arg */
1301 lua_call(L, 1, 1);
1302 s = lua_tolstring(L, -1, &l);
1303 if (s == NULL)
1304 return luaL_error(L, "cannot convert to string");
1305 if (i > 1) luaL_addchar(&b, ' '); /* use space instead of tab */
1306 luaV_addlstring(&b, s, l, 0);
1307 lua_pop(L, 1);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001308 }
1309 luaL_pushresult(&b);
1310 luaV_msg(L);
1311 return 0;
1312}
1313
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001314 static int
Bram Moolenaar38e2b062011-09-21 17:15:39 +02001315luaV_debug(lua_State *L)
1316{
1317 lua_settop(L, 0);
1318 lua_getglobal(L, "vim");
1319 lua_getfield(L, -1, "eval");
1320 lua_remove(L, -2); /* vim.eval at position 1 */
1321 for (;;)
1322 {
1323 const char *input;
1324 size_t l;
1325 lua_pushvalue(L, 1); /* vim.eval */
1326 lua_pushliteral(L, "input('lua_debug> ')");
1327 lua_call(L, 1, 1); /* return string */
1328 input = lua_tolstring(L, -1, &l);
1329 if (l == 0 || strcmp(input, "cont") == 0)
1330 return 0;
1331 msg_putchar('\n'); /* avoid outputting on input line */
1332 if (luaL_loadbuffer(L, input, l, "=(debug command)")
1333 || lua_pcall(L, 0, 0, 0))
1334 luaV_emsg(L);
1335 lua_settop(L, 1); /* remove eventual returns, but keep vim.eval */
1336 }
1337}
1338
1339 static int
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001340luaV_command(lua_State *L)
1341{
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001342 do_cmdline_cmd((char_u *) luaL_checkstring(L, 1));
1343 update_screen(VALID);
1344 return 0;
1345}
1346
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001347 static int
1348luaV_eval(lua_State *L)
1349{
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001350 typval_T *tv = eval_expr((char_u *) luaL_checkstring(L, 1), NULL);
1351 if (tv == NULL) luaL_error(L, "invalid expression");
1352 luaV_pushtypval(L, tv);
Bram Moolenaarb3766472013-04-15 13:49:21 +02001353 free_tv(tv);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001354 return 1;
1355}
1356
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001357 static int
Bram Moolenaar0d2e4fc2010-07-18 12:35:47 +02001358luaV_beep(lua_State *L UNUSED)
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001359{
Bram Moolenaar165bc692015-07-21 17:53:25 +02001360 vim_beep(BO_LANG);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001361 return 0;
1362}
1363
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001364 static int
1365luaV_line(lua_State *L)
1366{
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001367 luaV_pushline(L, curbuf, curwin->w_cursor.lnum);
1368 return 1;
1369}
1370
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001371 static int
Bram Moolenaar1dced572012-04-05 16:54:08 +02001372luaV_list(lua_State *L)
1373{
1374 list_T *l = list_alloc();
1375 if (l == NULL)
1376 lua_pushnil(L);
1377 else
1378 luaV_newlist(L, l);
1379 return 1;
1380}
1381
1382 static int
1383luaV_dict(lua_State *L)
1384{
1385 dict_T *d = dict_alloc();
1386 if (d == NULL)
1387 lua_pushnil(L);
1388 else
1389 luaV_newdict(L, d);
1390 return 1;
1391}
1392
1393 static int
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001394luaV_buffer(lua_State *L)
1395{
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001396 buf_T *buf;
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001397 if (lua_isstring(L, 1)) /* get by number or name? */
1398 {
1399 if (lua_isnumber(L, 1)) /* by number? */
1400 {
1401 int n = lua_tointeger(L, 1);
Bram Moolenaar29323592016-07-24 22:04:11 +02001402 FOR_ALL_BUFFERS(buf)
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001403 if (buf->b_fnum == n) break;
1404 }
1405 else { /* by name */
1406 size_t l;
1407 const char *s = lua_tolstring(L, 1, &l);
Bram Moolenaar29323592016-07-24 22:04:11 +02001408 FOR_ALL_BUFFERS(buf)
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001409 {
1410 if (buf->b_ffname == NULL || buf->b_sfname == NULL)
1411 {
1412 if (l == 0) break;
1413 }
Bram Moolenaar0d2e4fc2010-07-18 12:35:47 +02001414 else if (strncmp(s, (char *)buf->b_ffname, l) == 0
1415 || strncmp(s, (char *)buf->b_sfname, l) == 0)
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001416 break;
1417 }
1418 }
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001419 }
Bram Moolenaar1dced572012-04-05 16:54:08 +02001420 else
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001421 buf = (lua_toboolean(L, 1)) ? firstbuf : curbuf; /* first buffer? */
Bram Moolenaar1dced572012-04-05 16:54:08 +02001422 luaV_pushbuffer(L, buf);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001423 return 1;
1424}
1425
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001426 static int
1427luaV_window(lua_State *L)
1428{
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001429 win_T *win;
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001430 if (lua_isnumber(L, 1)) /* get by number? */
1431 {
1432 int n = lua_tointeger(L, 1);
1433 for (win = firstwin; win != NULL; win = win->w_next, n--)
1434 if (n == 1) break;
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001435 }
Bram Moolenaar1dced572012-04-05 16:54:08 +02001436 else
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001437 win = (lua_toboolean(L, 1)) ? firstwin : curwin; /* first window? */
Bram Moolenaar1dced572012-04-05 16:54:08 +02001438 luaV_pushwindow(L, win);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001439 return 1;
1440}
1441
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001442 static int
1443luaV_open(lua_State *L)
1444{
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001445 char_u *s = NULL;
1446#ifdef HAVE_SANDBOX
1447 luaV_checksandbox(L);
1448#endif
1449 if (lua_isstring(L, 1)) s = (char_u *) lua_tostring(L, 1);
Bram Moolenaarfa263a52011-12-08 16:00:16 +01001450 luaV_pushbuffer(L, buflist_new(s, NULL, 1L, BLN_LISTED));
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001451 return 1;
1452}
1453
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001454 static int
Bram Moolenaar1dced572012-04-05 16:54:08 +02001455luaV_type(lua_State *L)
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001456{
Bram Moolenaar1dced572012-04-05 16:54:08 +02001457 luaL_checkany(L, 1);
1458 if (lua_type(L, 1) == LUA_TUSERDATA) /* check vim udata? */
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001459 {
Bram Moolenaar1dced572012-04-05 16:54:08 +02001460 lua_settop(L, 1);
1461 if (lua_getmetatable(L, 1))
1462 {
1463 luaV_getfield(L, LUAVIM_LIST);
1464 if (lua_rawequal(L, -1, 2))
1465 {
1466 lua_pushstring(L, "list");
1467 return 1;
1468 }
1469 luaV_getfield(L, LUAVIM_DICT);
1470 if (lua_rawequal(L, -1, 2))
1471 {
1472 lua_pushstring(L, "dict");
1473 return 1;
1474 }
1475 luaV_getfield(L, LUAVIM_BUFFER);
1476 if (lua_rawequal(L, -1, 2))
1477 {
1478 lua_pushstring(L, "buffer");
1479 return 1;
1480 }
1481 luaV_getfield(L, LUAVIM_WINDOW);
1482 if (lua_rawequal(L, -1, 2))
1483 {
1484 lua_pushstring(L, "window");
1485 return 1;
1486 }
1487 }
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001488 }
Bram Moolenaar1dced572012-04-05 16:54:08 +02001489 lua_pushstring(L, luaL_typename(L, 1)); /* fallback */
1490 return 1;
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001491}
1492
1493static const luaL_Reg luaV_module[] = {
1494 {"command", luaV_command},
1495 {"eval", luaV_eval},
1496 {"beep", luaV_beep},
1497 {"line", luaV_line},
Bram Moolenaar1dced572012-04-05 16:54:08 +02001498 {"list", luaV_list},
1499 {"dict", luaV_dict},
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001500 {"buffer", luaV_buffer},
1501 {"window", luaV_window},
1502 {"open", luaV_open},
Bram Moolenaar1dced572012-04-05 16:54:08 +02001503 {"type", luaV_type},
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001504 {NULL, NULL}
1505};
1506
Bram Moolenaar1dced572012-04-05 16:54:08 +02001507/* for freeing list, dict, buffer and window objects; lightuserdata as arg */
1508 static int
1509luaV_free(lua_State *L)
1510{
1511 lua_pushnil(L);
1512 luaV_setudata(L, lua_touserdata(L, 1));
1513 return 0;
1514}
1515
1516 static int
1517luaV_luaeval (lua_State *L)
1518{
1519 luaL_Buffer b;
1520 size_t l;
1521 const char *str = lua_tolstring(L, 1, &l);
1522 typval_T *arg = (typval_T *) lua_touserdata(L, 2);
1523 typval_T *rettv = (typval_T *) lua_touserdata(L, 3);
1524 luaL_buffinit(L, &b);
1525 luaL_addlstring(&b, LUAVIM_EVALHEADER, sizeof(LUAVIM_EVALHEADER) - 1);
1526 luaL_addlstring(&b, str, l);
1527 luaL_pushresult(&b);
1528 str = lua_tolstring(L, -1, &l);
1529 if (luaL_loadbuffer(L, str, l, LUAVIM_EVALNAME)) /* compile error? */
1530 {
1531 luaV_emsg(L);
1532 return 0;
1533 }
1534 luaV_pushtypval(L, arg);
1535 if (lua_pcall(L, 1, 1, 0)) /* running error? */
1536 {
1537 luaV_emsg(L);
1538 return 0;
1539 }
1540 luaV_totypval(L, -1, rettv);
Bram Moolenaarf554a322015-02-04 23:08:01 +01001541 return 0;
Bram Moolenaar1dced572012-04-05 16:54:08 +02001542}
1543
1544 static int
1545luaV_setref (lua_State *L)
1546{
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01001547 int copyID = lua_tointeger(L, 1);
1548 int abort = FALSE;
1549 typval_T tv;
1550
Bram Moolenaar1dced572012-04-05 16:54:08 +02001551 luaV_getfield(L, LUAVIM_LIST);
1552 luaV_getfield(L, LUAVIM_DICT);
1553 lua_pushnil(L);
Bram Moolenaarb84634d2015-02-04 22:02:37 +01001554 /* traverse cache table */
1555 while (!abort && lua_next(L, lua_upvalueindex(1)) != 0)
Bram Moolenaar1dced572012-04-05 16:54:08 +02001556 {
1557 lua_getmetatable(L, -1);
1558 if (lua_rawequal(L, -1, 2)) /* list? */
1559 {
1560 tv.v_type = VAR_LIST;
1561 tv.vval.v_list = (list_T *) lua_touserdata(L, 4); /* key */
Bram Moolenaarf609dcf2015-12-03 17:43:17 +01001562 abort = set_ref_in_item(&tv, copyID, NULL, NULL);
Bram Moolenaar1dced572012-04-05 16:54:08 +02001563 }
1564 else if (lua_rawequal(L, -1, 3)) /* dict? */
1565 {
1566 tv.v_type = VAR_DICT;
1567 tv.vval.v_dict = (dict_T *) lua_touserdata(L, 4); /* key */
Bram Moolenaarf609dcf2015-12-03 17:43:17 +01001568 abort = set_ref_in_item(&tv, copyID, NULL, NULL);
Bram Moolenaar1dced572012-04-05 16:54:08 +02001569 }
1570 lua_pop(L, 2); /* metatable and value */
Bram Moolenaar1dced572012-04-05 16:54:08 +02001571 }
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01001572 lua_pushinteger(L, abort);
Bram Moolenaarf554a322015-02-04 23:08:01 +01001573 return 1;
Bram Moolenaar1dced572012-04-05 16:54:08 +02001574}
1575
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001576 static int
1577luaopen_vim(lua_State *L)
1578{
Bram Moolenaar1dced572012-04-05 16:54:08 +02001579 /* set cache table */
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001580 lua_newtable(L);
1581 lua_newtable(L);
Bram Moolenaar1dced572012-04-05 16:54:08 +02001582 lua_pushstring(L, "v");
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001583 lua_setfield(L, -2, "__mode");
Bram Moolenaar1dced572012-04-05 16:54:08 +02001584 lua_setmetatable(L, -2); /* cache is weak-valued */
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001585 /* print */
1586 lua_pushcfunction(L, luaV_print);
1587 lua_setglobal(L, "print");
Bram Moolenaar38e2b062011-09-21 17:15:39 +02001588 /* debug.debug */
1589 lua_getglobal(L, "debug");
1590 lua_pushcfunction(L, luaV_debug);
1591 lua_setfield(L, -2, "debug");
1592 lua_pop(L, 1);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001593 /* free */
1594 lua_pushlightuserdata(L, (void *) LUAVIM_FREE);
Bram Moolenaar1dced572012-04-05 16:54:08 +02001595 lua_pushvalue(L, 1); /* cache table */
1596 lua_pushcclosure(L, luaV_free, 1);
1597 lua_rawset(L, LUA_REGISTRYINDEX);
1598 /* luaeval */
1599 lua_pushlightuserdata(L, (void *) LUAVIM_LUAEVAL);
1600 lua_pushvalue(L, 1); /* cache table */
1601 lua_pushcclosure(L, luaV_luaeval, 1);
1602 lua_rawset(L, LUA_REGISTRYINDEX);
1603 /* setref */
1604 lua_pushlightuserdata(L, (void *) LUAVIM_SETREF);
1605 lua_pushvalue(L, 1); /* cache table */
1606 lua_pushcclosure(L, luaV_setref, 1);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001607 lua_rawset(L, LUA_REGISTRYINDEX);
1608 /* register */
Bram Moolenaar1dced572012-04-05 16:54:08 +02001609 luaV_newmetatable(L, LUAVIM_LIST);
1610 lua_pushvalue(L, 1);
1611 luaV_openlib(L, luaV_List_mt, 1);
1612 luaV_newmetatable(L, LUAVIM_DICT);
1613 lua_pushvalue(L, 1);
1614 luaV_openlib(L, luaV_Dict_mt, 1);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001615 luaV_newmetatable(L, LUAVIM_BUFFER);
Bram Moolenaar1dced572012-04-05 16:54:08 +02001616 lua_pushvalue(L, 1); /* cache table */
1617 luaV_openlib(L, luaV_Buffer_mt, 1);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001618 luaV_newmetatable(L, LUAVIM_WINDOW);
Bram Moolenaar1dced572012-04-05 16:54:08 +02001619 lua_pushvalue(L, 1); /* cache table */
1620 luaV_openlib(L, luaV_Window_mt, 1);
1621 lua_newtable(L); /* vim table */
1622 lua_pushvalue(L, 1); /* cache table */
1623 luaV_openlib(L, luaV_module, 1);
1624 lua_setglobal(L, LUAVIM_NAME);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001625 return 0;
1626}
1627
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001628 static lua_State *
1629luaV_newstate(void)
1630{
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001631 lua_State *L = luaL_newstate();
Bram Moolenaar16c98f92010-07-28 22:46:08 +02001632 luaL_openlibs(L); /* core libs */
1633 lua_pushcfunction(L, luaopen_vim); /* vim */
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001634 lua_call(L, 0, 0);
1635 return L;
1636}
1637
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001638 static void
1639luaV_setrange(lua_State *L, int line1, int line2)
1640{
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001641 lua_getglobal(L, LUAVIM_NAME);
1642 lua_pushinteger(L, line1);
1643 lua_setfield(L, -2, "firstline");
1644 lua_pushinteger(L, line2);
1645 lua_setfield(L, -2, "lastline");
1646 lua_pop(L, 1); /* vim table */
1647}
1648
1649
1650/* ======= Interface ======= */
1651
1652static lua_State *L = NULL;
1653
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001654 static int
Bram Moolenaar1dced572012-04-05 16:54:08 +02001655lua_isopen(void)
Bram Moolenaar2bd6a1b2010-08-12 22:14:01 +02001656{
1657 return L != NULL;
1658}
1659
1660 static int
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001661lua_init(void)
1662{
Bram Moolenaar1dced572012-04-05 16:54:08 +02001663 if (!lua_isopen())
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001664 {
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001665#ifdef DYNAMIC_LUA
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001666 if (!lua_enabled(TRUE))
1667 {
1668 EMSG(_("Lua library cannot be loaded."));
1669 return FAIL;
1670 }
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001671#endif
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001672 L = luaV_newstate();
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001673 }
1674 return OK;
1675}
1676
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001677 void
1678lua_end(void)
1679{
Bram Moolenaar1dced572012-04-05 16:54:08 +02001680 if (lua_isopen())
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001681 {
1682 lua_close(L);
1683 L = NULL;
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001684#ifdef DYNAMIC_LUA
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001685 end_dynamic_lua();
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001686#endif
1687 }
1688}
1689
1690/* ex commands */
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001691 void
1692ex_lua(exarg_T *eap)
1693{
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001694 char *script;
1695 if (lua_init() == FAIL) return;
1696 script = (char *) script_get(eap, eap->arg);
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001697 if (!eap->skip)
1698 {
1699 char *s = (script) ? script : (char *) eap->arg;
1700 luaV_setrange(L, eap->line1, eap->line2);
1701 if (luaL_loadbuffer(L, s, strlen(s), LUAVIM_CHUNKNAME)
1702 || lua_pcall(L, 0, 0, 0))
1703 luaV_emsg(L);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001704 }
1705 if (script != NULL) vim_free(script);
1706}
1707
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001708 void
1709ex_luado(exarg_T *eap)
1710{
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001711 linenr_T l;
1712 const char *s = (const char *) eap->arg;
1713 luaL_Buffer b;
1714 size_t len;
Bram Moolenaard58f03b2017-01-29 22:48:45 +01001715 buf_T *was_curbuf = curbuf;
1716
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001717 if (lua_init() == FAIL) return;
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001718 if (u_save(eap->line1 - 1, eap->line2 + 1) == FAIL)
1719 {
1720 EMSG(_("cannot save undo information"));
1721 return;
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001722 }
1723 luaV_setrange(L, eap->line1, eap->line2);
1724 luaL_buffinit(L, &b);
Bram Moolenaarbd2f3c32012-04-06 14:31:00 +02001725 luaL_addlstring(&b, "return function(line, linenr) ", 30); /* header */
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001726 luaL_addlstring(&b, s, strlen(s));
1727 luaL_addlstring(&b, " end", 4); /* footer */
1728 luaL_pushresult(&b);
1729 s = lua_tolstring(L, -1, &len);
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001730 if (luaL_loadbuffer(L, s, len, LUAVIM_CHUNKNAME))
1731 {
1732 luaV_emsg(L);
1733 lua_pop(L, 1); /* function body */
1734 return;
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001735 }
1736 lua_call(L, 0, 1);
1737 lua_replace(L, -2); /* function -> body */
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001738 for (l = eap->line1; l <= eap->line2; l++)
1739 {
Bram Moolenaard58f03b2017-01-29 22:48:45 +01001740 /* Check the line number, the command my have deleted lines. */
1741 if (l > curbuf->b_ml.ml_line_count)
1742 break;
1743
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001744 lua_pushvalue(L, -1); /* function */
1745 luaV_pushline(L, curbuf, l); /* current line as arg */
Bram Moolenaarbd2f3c32012-04-06 14:31:00 +02001746 lua_pushinteger(L, l); /* current line number as arg */
1747 if (lua_pcall(L, 2, 1, 0))
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001748 {
1749 luaV_emsg(L);
1750 break;
1751 }
Bram Moolenaard58f03b2017-01-29 22:48:45 +01001752 /* Catch the command switching to another buffer. */
1753 if (curbuf != was_curbuf)
1754 break;
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001755 if (lua_isstring(L, -1)) /* update line? */
1756 {
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001757#ifdef HAVE_SANDBOX
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001758 luaV_checksandbox(L);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001759#endif
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001760 ml_replace(l, luaV_toline(L, -1), TRUE);
1761 changed_bytes(l, 0);
1762 lua_pop(L, 1); /* result from luaV_toline */
1763 }
1764 lua_pop(L, 1); /* line */
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001765 }
1766 lua_pop(L, 1); /* function */
1767 check_cursor();
1768 update_screen(NOT_VALID);
1769}
1770
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001771 void
1772ex_luafile(exarg_T *eap)
1773{
1774 if (lua_init() == FAIL)
1775 return;
1776 if (!eap->skip)
1777 {
1778 luaV_setrange(L, eap->line1, eap->line2);
1779 if (luaL_loadfile(L, (char *) eap->arg) || lua_pcall(L, 0, 0, 0))
1780 luaV_emsg(L);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001781 }
1782}
1783
Bram Moolenaar1dced572012-04-05 16:54:08 +02001784#define luaV_freetype(typ,tname) \
1785 void \
1786 lua_##tname##_free(typ *o) \
1787 { \
1788 if (!lua_isopen()) return; \
1789 luaV_getfield(L, LUAVIM_FREE); \
1790 lua_pushlightuserdata(L, (void *) o); \
1791 lua_call(L, 1, 0); \
1792 }
1793
1794luaV_freetype(buf_T, buffer)
1795luaV_freetype(win_T, window)
1796
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001797 void
Bram Moolenaar1dced572012-04-05 16:54:08 +02001798do_luaeval (char_u *str, typval_T *arg, typval_T *rettv)
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001799{
Bram Moolenaar1dced572012-04-05 16:54:08 +02001800 lua_init();
1801 luaV_getfield(L, LUAVIM_LUAEVAL);
1802 lua_pushstring(L, (char *) str);
1803 lua_pushlightuserdata(L, (void *) arg);
1804 lua_pushlightuserdata(L, (void *) rettv);
1805 lua_call(L, 3, 0);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001806}
1807
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01001808 int
Bram Moolenaar1dced572012-04-05 16:54:08 +02001809set_ref_in_lua (int copyID)
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001810{
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01001811 int aborted = 0;
1812
1813 if (lua_isopen())
1814 {
1815 luaV_getfield(L, LUAVIM_SETREF);
1816 /* call the function with 1 arg, getting 1 result back */
1817 lua_pushinteger(L, copyID);
1818 lua_call(L, 1, 1);
1819 /* get the result */
1820 aborted = lua_tointeger(L, -1);
1821 /* pop result off the stack */
1822 lua_pop(L, 1);
1823 }
1824 return aborted;
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001825}
1826
1827#endif