blob: 0d1d08e2b73c345c4d905852840ccfe599acbd30 [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 Moolenaarbd2f3c32012-04-06 14:31:00 +0200122#define lua_typename dll_lua_typename
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200123#define lua_close dll_lua_close
124#define lua_gettop dll_lua_gettop
125#define lua_settop dll_lua_settop
126#define lua_pushvalue dll_lua_pushvalue
127#define lua_replace dll_lua_replace
Bram Moolenaar1dced572012-04-05 16:54:08 +0200128#define lua_remove dll_lua_remove
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200129#define lua_isnumber dll_lua_isnumber
130#define lua_isstring dll_lua_isstring
131#define lua_type dll_lua_type
132#define lua_rawequal dll_lua_rawequal
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200133#define lua_toboolean dll_lua_toboolean
134#define lua_tolstring dll_lua_tolstring
135#define lua_touserdata dll_lua_touserdata
136#define lua_pushnil dll_lua_pushnil
137#define lua_pushnumber dll_lua_pushnumber
138#define lua_pushinteger dll_lua_pushinteger
139#define lua_pushlstring dll_lua_pushlstring
140#define lua_pushstring dll_lua_pushstring
141#define lua_pushfstring dll_lua_pushfstring
142#define lua_pushcclosure dll_lua_pushcclosure
143#define lua_pushboolean dll_lua_pushboolean
144#define lua_pushlightuserdata dll_lua_pushlightuserdata
145#define lua_getfield dll_lua_getfield
146#define lua_rawget dll_lua_rawget
Bram Moolenaar1dced572012-04-05 16:54:08 +0200147#define lua_rawgeti dll_lua_rawgeti
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200148#define lua_createtable dll_lua_createtable
149#define lua_newuserdata dll_lua_newuserdata
150#define lua_getmetatable dll_lua_getmetatable
151#define lua_setfield dll_lua_setfield
152#define lua_rawset dll_lua_rawset
153#define lua_rawseti dll_lua_rawseti
154#define lua_setmetatable dll_lua_setmetatable
Bram Moolenaarbd2f3c32012-04-06 14:31:00 +0200155#define lua_next dll_lua_next
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200156/* libs */
157#define luaopen_base dll_luaopen_base
158#define luaopen_table dll_luaopen_table
159#define luaopen_string dll_luaopen_string
160#define luaopen_math dll_luaopen_math
Bram Moolenaar16c98f92010-07-28 22:46:08 +0200161#define luaopen_io dll_luaopen_io
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200162#define luaopen_os dll_luaopen_os
163#define luaopen_package dll_luaopen_package
164#define luaopen_debug dll_luaopen_debug
Bram Moolenaar16c98f92010-07-28 22:46:08 +0200165#define luaL_openlibs dll_luaL_openlibs
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200166
167/* lauxlib */
Bram Moolenaar1dced572012-04-05 16:54:08 +0200168#if LUA_VERSION_NUM <= 501
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200169void (*dll_luaL_register) (lua_State *L, const char *libname, const luaL_Reg *l);
Bram Moolenaar1dced572012-04-05 16:54:08 +0200170char *(*dll_luaL_prepbuffer) (luaL_Buffer *B);
171void (*dll_luaL_openlib) (lua_State *L, const char *libname, const luaL_Reg *l, int nup);
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200172int (*dll_luaL_typerror) (lua_State *L, int narg, const char *tname);
Bram Moolenaar1dced572012-04-05 16:54:08 +0200173int (*dll_luaL_loadfile) (lua_State *L, const char *filename);
174int (*dll_luaL_loadbuffer) (lua_State *L, const char *buff, size_t sz, const char *name);
175#else
176char *(*dll_luaL_prepbuffsize) (luaL_Buffer *B, size_t sz);
177void (*dll_luaL_setfuncs) (lua_State *L, const luaL_Reg *l, int nup);
178int (*dll_luaL_loadfilex) (lua_State *L, const char *filename, const char *mode);
179int (*dll_luaL_loadbufferx) (lua_State *L, const char *buff, size_t sz, const char *name, const char *mode);
180int (*dll_luaL_argerror) (lua_State *L, int numarg, const char *extramsg);
181#endif
Bram Moolenaarbd2f3c32012-04-06 14:31:00 +0200182void (*dll_luaL_checkany) (lua_State *L, int narg);
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200183const char *(*dll_luaL_checklstring) (lua_State *L, int numArg, size_t *l);
184lua_Integer (*dll_luaL_checkinteger) (lua_State *L, int numArg);
185lua_Integer (*dll_luaL_optinteger) (lua_State *L, int nArg, lua_Integer def);
186void (*dll_luaL_checktype) (lua_State *L, int narg, int t);
187int (*dll_luaL_error) (lua_State *L, const char *fmt, ...);
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200188lua_State *(*dll_luaL_newstate) (void);
189void (*dll_luaL_buffinit) (lua_State *L, luaL_Buffer *B);
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200190void (*dll_luaL_addlstring) (luaL_Buffer *B, const char *s, size_t l);
191void (*dll_luaL_pushresult) (luaL_Buffer *B);
192/* lua */
Bram Moolenaar1dced572012-04-05 16:54:08 +0200193#if LUA_VERSION_NUM <= 501
194lua_Number (*dll_lua_tonumber) (lua_State *L, int idx);
195lua_Integer (*dll_lua_tointeger) (lua_State *L, int idx);
196void (*dll_lua_call) (lua_State *L, int nargs, int nresults);
197int (*dll_lua_pcall) (lua_State *L, int nargs, int nresults, int errfunc);
198#else
199lua_Number (*dll_lua_tonumberx) (lua_State *L, int idx, int *isnum);
200lua_Integer (*dll_lua_tointegerx) (lua_State *L, int idx, int *isnum);
201void (*dll_lua_callk) (lua_State *L, int nargs, int nresults, int ctx,
Bram Moolenaardb913952012-06-29 12:54:53 +0200202 lua_CFunction k);
Bram Moolenaar1dced572012-04-05 16:54:08 +0200203int (*dll_lua_pcallk) (lua_State *L, int nargs, int nresults, int errfunc,
Bram Moolenaardb913952012-06-29 12:54:53 +0200204 int ctx, lua_CFunction k);
Bram Moolenaar1dced572012-04-05 16:54:08 +0200205void (*dll_lua_getglobal) (lua_State *L, const char *var);
206void (*dll_lua_setglobal) (lua_State *L, const char *var);
Bram Moolenaar1dced572012-04-05 16:54:08 +0200207#endif
Bram Moolenaarbd2f3c32012-04-06 14:31:00 +0200208const char *(*dll_lua_typename) (lua_State *L, int tp);
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200209void (*dll_lua_close) (lua_State *L);
210int (*dll_lua_gettop) (lua_State *L);
211void (*dll_lua_settop) (lua_State *L, int idx);
212void (*dll_lua_pushvalue) (lua_State *L, int idx);
213void (*dll_lua_replace) (lua_State *L, int idx);
Bram Moolenaar1dced572012-04-05 16:54:08 +0200214void (*dll_lua_remove) (lua_State *L, int idx);
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200215int (*dll_lua_isnumber) (lua_State *L, int idx);
216int (*dll_lua_isstring) (lua_State *L, int idx);
217int (*dll_lua_type) (lua_State *L, int idx);
218int (*dll_lua_rawequal) (lua_State *L, int idx1, int idx2);
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200219int (*dll_lua_toboolean) (lua_State *L, int idx);
220const char *(*dll_lua_tolstring) (lua_State *L, int idx, size_t *len);
221void *(*dll_lua_touserdata) (lua_State *L, int idx);
222void (*dll_lua_pushnil) (lua_State *L);
223void (*dll_lua_pushnumber) (lua_State *L, lua_Number n);
224void (*dll_lua_pushinteger) (lua_State *L, lua_Integer n);
225void (*dll_lua_pushlstring) (lua_State *L, const char *s, size_t l);
226void (*dll_lua_pushstring) (lua_State *L, const char *s);
227const char *(*dll_lua_pushfstring) (lua_State *L, const char *fmt, ...);
228void (*dll_lua_pushcclosure) (lua_State *L, lua_CFunction fn, int n);
229void (*dll_lua_pushboolean) (lua_State *L, int b);
230void (*dll_lua_pushlightuserdata) (lua_State *L, void *p);
231void (*dll_lua_getfield) (lua_State *L, int idx, const char *k);
232void (*dll_lua_rawget) (lua_State *L, int idx);
Bram Moolenaar1dced572012-04-05 16:54:08 +0200233void (*dll_lua_rawgeti) (lua_State *L, int idx, int n);
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200234void (*dll_lua_createtable) (lua_State *L, int narr, int nrec);
235void *(*dll_lua_newuserdata) (lua_State *L, size_t sz);
236int (*dll_lua_getmetatable) (lua_State *L, int objindex);
237void (*dll_lua_setfield) (lua_State *L, int idx, const char *k);
238void (*dll_lua_rawset) (lua_State *L, int idx);
239void (*dll_lua_rawseti) (lua_State *L, int idx, int n);
240int (*dll_lua_setmetatable) (lua_State *L, int objindex);
Bram Moolenaarbd2f3c32012-04-06 14:31:00 +0200241int (*dll_lua_next) (lua_State *L, int idx);
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200242/* libs */
243int (*dll_luaopen_base) (lua_State *L);
244int (*dll_luaopen_table) (lua_State *L);
245int (*dll_luaopen_string) (lua_State *L);
246int (*dll_luaopen_math) (lua_State *L);
Bram Moolenaar16c98f92010-07-28 22:46:08 +0200247int (*dll_luaopen_io) (lua_State *L);
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200248int (*dll_luaopen_os) (lua_State *L);
249int (*dll_luaopen_package) (lua_State *L);
250int (*dll_luaopen_debug) (lua_State *L);
Bram Moolenaar16c98f92010-07-28 22:46:08 +0200251void (*dll_luaL_openlibs) (lua_State *L);
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200252
253typedef void **luaV_function;
254typedef struct {
255 const char *name;
256 luaV_function func;
257} luaV_Reg;
258
259static const luaV_Reg luaV_dll[] = {
260 /* lauxlib */
Bram Moolenaar1dced572012-04-05 16:54:08 +0200261#if LUA_VERSION_NUM <= 501
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200262 {"luaL_register", (luaV_function) &dll_luaL_register},
Bram Moolenaar1dced572012-04-05 16:54:08 +0200263 {"luaL_prepbuffer", (luaV_function) &dll_luaL_prepbuffer},
264 {"luaL_openlib", (luaV_function) &dll_luaL_openlib},
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200265 {"luaL_typerror", (luaV_function) &dll_luaL_typerror},
Bram Moolenaar1dced572012-04-05 16:54:08 +0200266 {"luaL_loadfile", (luaV_function) &dll_luaL_loadfile},
267 {"luaL_loadbuffer", (luaV_function) &dll_luaL_loadbuffer},
268#else
269 {"luaL_prepbuffsize", (luaV_function) &dll_luaL_prepbuffsize},
270 {"luaL_setfuncs", (luaV_function) &dll_luaL_setfuncs},
271 {"luaL_loadfilex", (luaV_function) &dll_luaL_loadfilex},
272 {"luaL_loadbufferx", (luaV_function) &dll_luaL_loadbufferx},
273 {"luaL_argerror", (luaV_function) &dll_luaL_argerror},
274#endif
Bram Moolenaarbd2f3c32012-04-06 14:31:00 +0200275 {"luaL_checkany", (luaV_function) &dll_luaL_checkany},
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200276 {"luaL_checklstring", (luaV_function) &dll_luaL_checklstring},
277 {"luaL_checkinteger", (luaV_function) &dll_luaL_checkinteger},
278 {"luaL_optinteger", (luaV_function) &dll_luaL_optinteger},
279 {"luaL_checktype", (luaV_function) &dll_luaL_checktype},
280 {"luaL_error", (luaV_function) &dll_luaL_error},
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200281 {"luaL_newstate", (luaV_function) &dll_luaL_newstate},
282 {"luaL_buffinit", (luaV_function) &dll_luaL_buffinit},
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200283 {"luaL_addlstring", (luaV_function) &dll_luaL_addlstring},
284 {"luaL_pushresult", (luaV_function) &dll_luaL_pushresult},
285 /* lua */
Bram Moolenaar1dced572012-04-05 16:54:08 +0200286#if LUA_VERSION_NUM <= 501
287 {"lua_tonumber", (luaV_function) &dll_lua_tonumber},
288 {"lua_tointeger", (luaV_function) &dll_lua_tointeger},
289 {"lua_call", (luaV_function) &dll_lua_call},
290 {"lua_pcall", (luaV_function) &dll_lua_pcall},
291#else
292 {"lua_tonumberx", (luaV_function) &dll_lua_tonumberx},
293 {"lua_tointegerx", (luaV_function) &dll_lua_tointegerx},
294 {"lua_callk", (luaV_function) &dll_lua_callk},
295 {"lua_pcallk", (luaV_function) &dll_lua_pcallk},
296 {"lua_getglobal", (luaV_function) &dll_lua_getglobal},
297 {"lua_setglobal", (luaV_function) &dll_lua_setglobal},
Bram Moolenaar1dced572012-04-05 16:54:08 +0200298#endif
Bram Moolenaarbd2f3c32012-04-06 14:31:00 +0200299 {"lua_typename", (luaV_function) &dll_lua_typename},
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200300 {"lua_close", (luaV_function) &dll_lua_close},
301 {"lua_gettop", (luaV_function) &dll_lua_gettop},
302 {"lua_settop", (luaV_function) &dll_lua_settop},
303 {"lua_pushvalue", (luaV_function) &dll_lua_pushvalue},
304 {"lua_replace", (luaV_function) &dll_lua_replace},
Bram Moolenaar1dced572012-04-05 16:54:08 +0200305 {"lua_remove", (luaV_function) &dll_lua_remove},
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200306 {"lua_isnumber", (luaV_function) &dll_lua_isnumber},
307 {"lua_isstring", (luaV_function) &dll_lua_isstring},
308 {"lua_type", (luaV_function) &dll_lua_type},
309 {"lua_rawequal", (luaV_function) &dll_lua_rawequal},
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200310 {"lua_toboolean", (luaV_function) &dll_lua_toboolean},
311 {"lua_tolstring", (luaV_function) &dll_lua_tolstring},
312 {"lua_touserdata", (luaV_function) &dll_lua_touserdata},
313 {"lua_pushnil", (luaV_function) &dll_lua_pushnil},
314 {"lua_pushnumber", (luaV_function) &dll_lua_pushnumber},
315 {"lua_pushinteger", (luaV_function) &dll_lua_pushinteger},
316 {"lua_pushlstring", (luaV_function) &dll_lua_pushlstring},
317 {"lua_pushstring", (luaV_function) &dll_lua_pushstring},
318 {"lua_pushfstring", (luaV_function) &dll_lua_pushfstring},
319 {"lua_pushcclosure", (luaV_function) &dll_lua_pushcclosure},
320 {"lua_pushboolean", (luaV_function) &dll_lua_pushboolean},
321 {"lua_pushlightuserdata", (luaV_function) &dll_lua_pushlightuserdata},
322 {"lua_getfield", (luaV_function) &dll_lua_getfield},
323 {"lua_rawget", (luaV_function) &dll_lua_rawget},
Bram Moolenaar1dced572012-04-05 16:54:08 +0200324 {"lua_rawgeti", (luaV_function) &dll_lua_rawgeti},
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200325 {"lua_createtable", (luaV_function) &dll_lua_createtable},
326 {"lua_newuserdata", (luaV_function) &dll_lua_newuserdata},
327 {"lua_getmetatable", (luaV_function) &dll_lua_getmetatable},
328 {"lua_setfield", (luaV_function) &dll_lua_setfield},
329 {"lua_rawset", (luaV_function) &dll_lua_rawset},
330 {"lua_rawseti", (luaV_function) &dll_lua_rawseti},
331 {"lua_setmetatable", (luaV_function) &dll_lua_setmetatable},
Bram Moolenaarbd2f3c32012-04-06 14:31:00 +0200332 {"lua_next", (luaV_function) &dll_lua_next},
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200333 /* libs */
334 {"luaopen_base", (luaV_function) &dll_luaopen_base},
335 {"luaopen_table", (luaV_function) &dll_luaopen_table},
336 {"luaopen_string", (luaV_function) &dll_luaopen_string},
337 {"luaopen_math", (luaV_function) &dll_luaopen_math},
Bram Moolenaar16c98f92010-07-28 22:46:08 +0200338 {"luaopen_io", (luaV_function) &dll_luaopen_io},
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200339 {"luaopen_os", (luaV_function) &dll_luaopen_os},
340 {"luaopen_package", (luaV_function) &dll_luaopen_package},
341 {"luaopen_debug", (luaV_function) &dll_luaopen_debug},
Bram Moolenaar16c98f92010-07-28 22:46:08 +0200342 {"luaL_openlibs", (luaV_function) &dll_luaL_openlibs},
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200343 {NULL, NULL}
344};
345
Bram Moolenaar2334b6d2010-07-22 21:32:16 +0200346static HANDLE hinstLua = NULL;
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200347
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200348 static void
349end_dynamic_lua(void)
350{
351 if (hinstLua)
352 {
Bram Moolenaar2334b6d2010-07-22 21:32:16 +0200353 close_dll(hinstLua);
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200354 hinstLua = 0;
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200355 }
356}
357
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200358 static int
359lua_link_init(char *libname, int verbose)
360{
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200361 const luaV_Reg *reg;
362 if (hinstLua) return OK;
Bram Moolenaar2334b6d2010-07-22 21:32:16 +0200363 hinstLua = load_dll(libname);
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200364 if (!hinstLua)
365 {
366 if (verbose)
367 EMSG2(_(e_loadlib), libname);
368 return FAIL;
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200369 }
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200370 for (reg = luaV_dll; reg->func; reg++)
371 {
Bram Moolenaar2334b6d2010-07-22 21:32:16 +0200372 if ((*reg->func = symbol_from_dll(hinstLua, reg->name)) == NULL)
373 {
374 close_dll(hinstLua);
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200375 hinstLua = 0;
376 if (verbose)
377 EMSG2(_(e_loadfunc), reg->name);
378 return FAIL;
379 }
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200380 }
381 return OK;
382}
383
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200384 int
385lua_enabled(int verbose)
386{
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200387 return lua_link_init(DYNAMIC_LUA_DLL, verbose) == OK;
388}
389
390#endif /* DYNAMIC_LUA */
391
Bram Moolenaar1dced572012-04-05 16:54:08 +0200392#if LUA_VERSION_NUM > 501
393 static int
394luaL_typeerror (lua_State *L, int narg, const char *tname)
395{
396 const char *msg = lua_pushfstring(L, "%s expected, got %s",
Bram Moolenaardb913952012-06-29 12:54:53 +0200397 tname, luaL_typename(L, narg));
Bram Moolenaar1dced572012-04-05 16:54:08 +0200398 return luaL_argerror(L, narg, msg);
399}
400#endif
401
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200402
403/* ======= Internal ======= */
404
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200405 static void
406luaV_newmetatable(lua_State *L, const char *tname)
407{
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200408 lua_newtable(L);
409 lua_pushlightuserdata(L, (void *) tname);
410 lua_pushvalue(L, -2);
411 lua_rawset(L, LUA_REGISTRYINDEX);
412}
413
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200414 static void *
415luaV_toudata(lua_State *L, int ud, const char *tname)
416{
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200417 void *p = lua_touserdata(L, ud);
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200418
419 if (p != NULL) /* value is userdata? */
420 {
421 if (lua_getmetatable(L, ud)) /* does it have a metatable? */
422 {
423 luaV_getfield(L, tname); /* get metatable */
424 if (lua_rawequal(L, -1, -2)) /* MTs match? */
425 {
426 lua_pop(L, 2); /* MTs */
427 return p;
428 }
429 }
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200430 }
431 return NULL;
432}
433
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200434 static void *
Bram Moolenaar1dced572012-04-05 16:54:08 +0200435luaV_checkcache(lua_State *L, void *p)
436{
437 luaV_getudata(L, p);
438 if (lua_isnil(L, -1)) luaL_error(L, "invalid object");
439 lua_pop(L, 1);
440 return p;
441}
442
443#define luaV_unbox(L,luatyp,ud) (*((luatyp *) lua_touserdata((L),(ud))))
444
445#define luaV_checkvalid(L,luatyp,ud) \
446 luaV_checkcache((L), (void *) luaV_unbox((L),luatyp,(ud)))
447
448 static void *
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200449luaV_checkudata(lua_State *L, int ud, const char *tname)
450{
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200451 void *p = luaV_toudata(L, ud, tname);
Bram Moolenaar1dced572012-04-05 16:54:08 +0200452 if (p == NULL) luaL_typeerror(L, ud, tname);
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200453 return p;
454}
455
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200456 static void
457luaV_pushtypval(lua_State *L, typval_T *tv)
458{
Bram Moolenaar1dced572012-04-05 16:54:08 +0200459 if (tv == NULL)
460 {
461 lua_pushnil(L);
462 return;
463 }
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200464 switch (tv->v_type)
465 {
466 case VAR_STRING:
Bram Moolenaard04da7c2012-10-14 03:41:59 +0200467 lua_pushstring(L, tv->vval.v_string == NULL
468 ? "" : (char *)tv->vval.v_string);
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200469 break;
470 case VAR_NUMBER:
471 lua_pushinteger(L, (int) tv->vval.v_number);
472 break;
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200473#ifdef FEAT_FLOAT
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200474 case VAR_FLOAT:
475 lua_pushnumber(L, (lua_Number) tv->vval.v_float);
476 break;
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200477#endif
Bram Moolenaar1dced572012-04-05 16:54:08 +0200478 case VAR_LIST:
479 luaV_pushlist(L, tv->vval.v_list);
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200480 break;
Bram Moolenaar1dced572012-04-05 16:54:08 +0200481 case VAR_DICT:
482 luaV_pushdict(L, tv->vval.v_dict);
483 break;
484 default:
485 lua_pushnil(L);
486 }
487}
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200488
Bram Moolenaar1dced572012-04-05 16:54:08 +0200489/* converts lua value at 'pos' to typval 'tv' */
490 static void
491luaV_totypval (lua_State *L, int pos, typval_T *tv)
492{
493 switch(lua_type(L, pos)) {
494 case LUA_TBOOLEAN:
495 tv->v_type = VAR_NUMBER;
496 tv->vval.v_number = (varnumber_T) lua_toboolean(L, pos);
497 break;
498 case LUA_TSTRING:
499 tv->v_type = VAR_STRING;
500 tv->vval.v_string = vim_strsave((char_u *) lua_tostring(L, pos));
501 break;
502 case LUA_TNUMBER:
503#ifdef FEAT_FLOAT
504 tv->v_type = VAR_FLOAT;
505 tv->vval.v_float = (float_T) lua_tonumber(L, pos);
506#else
507 tv->v_type = VAR_NUMBER;
508 tv->vval.v_number = (varnumber_T) lua_tointeger(L, pos);
509#endif
510 break;
511 case LUA_TUSERDATA: {
512 void *p = lua_touserdata(L, pos);
513 if (lua_getmetatable(L, pos)) /* has metatable? */
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200514 {
Bram Moolenaar1dced572012-04-05 16:54:08 +0200515 /* check list */
516 luaV_getfield(L, LUAVIM_LIST);
517 if (lua_rawequal(L, -1, -2))
Bram Moolenaar2334b6d2010-07-22 21:32:16 +0200518 {
Bram Moolenaar1dced572012-04-05 16:54:08 +0200519 tv->v_type = VAR_LIST;
520 tv->vval.v_list = *((luaV_List *) p);
521 ++tv->vval.v_list->lv_refcount;
522 lua_pop(L, 2); /* MTs */
523 return;
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200524 }
Bram Moolenaar1dced572012-04-05 16:54:08 +0200525 /* check dict */
526 luaV_getfield(L, LUAVIM_DICT);
527 if (lua_rawequal(L, -1, -3))
528 {
529 tv->v_type = VAR_DICT;
530 tv->vval.v_dict = *((luaV_Dict *) p);
531 ++tv->vval.v_dict->dv_refcount;
532 lua_pop(L, 3); /* MTs */
533 return;
534 }
535 lua_pop(L, 3); /* MTs */
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200536 }
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200537 break;
538 }
539 default:
Bram Moolenaar1dced572012-04-05 16:54:08 +0200540 tv->v_type = VAR_NUMBER;
541 tv->vval.v_number = 0;
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200542 }
543}
544
545/* similar to luaL_addlstring, but replaces \0 with \n if toline and
546 * \n with \0 otherwise */
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200547 static void
548luaV_addlstring(luaL_Buffer *b, const char *s, size_t l, int toline)
549{
550 while (l--)
551 {
552 if (*s == '\0' && toline)
553 luaL_addchar(b, '\n');
554 else if (*s == '\n' && !toline)
555 luaL_addchar(b, '\0');
556 else
557 luaL_addchar(b, *s);
558 s++;
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200559 }
560}
561
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200562 static void
563luaV_pushline(lua_State *L, buf_T *buf, linenr_T n)
564{
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200565 const char *s = (const char *) ml_get_buf(buf, n, FALSE);
566 luaL_Buffer b;
567 luaL_buffinit(L, &b);
568 luaV_addlstring(&b, s, strlen(s), 0);
569 luaL_pushresult(&b);
570}
571
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200572 static char_u *
573luaV_toline(lua_State *L, int pos)
574{
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200575 size_t l;
576 const char *s = lua_tolstring(L, pos, &l);
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200577
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200578 luaL_Buffer b;
579 luaL_buffinit(L, &b);
580 luaV_addlstring(&b, s, l, 1);
581 luaL_pushresult(&b);
582 return (char_u *) lua_tostring(L, -1);
583}
584
585/* pops a string s from the top of the stack and calls mf(t) for pieces t of
586 * s separated by newlines */
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200587 static void
588luaV_msgfunc(lua_State *L, msgfunc_T mf)
589{
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200590 luaL_Buffer b;
591 size_t l;
592 const char *p, *s = lua_tolstring(L, -1, &l);
593 luaL_buffinit(L, &b);
594 luaV_addlstring(&b, s, l, 0);
595 luaL_pushresult(&b);
596 /* break string */
597 p = s = lua_tolstring(L, -1, &l);
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200598 while (l--)
599 {
600 if (*p++ == '\0') /* break? */
601 {
602 mf((char_u *) s);
603 s = p;
604 }
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200605 }
606 mf((char_u *) s);
607 lua_pop(L, 2); /* original and modified strings */
608}
609
Bram Moolenaar1dced572012-04-05 16:54:08 +0200610#define luaV_newtype(typ,tname,luatyp,luatname) \
611 static luatyp * \
612 luaV_new##tname (lua_State *L, typ *obj) \
613 { \
614 luatyp *o = (luatyp *) lua_newuserdata(L, sizeof(luatyp)); \
615 *o = obj; \
616 luaV_setudata(L, obj); /* cache[obj] = udata */ \
617 luaV_getfield(L, luatname); \
618 lua_setmetatable(L, -2); \
619 return o; \
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200620 }
Bram Moolenaar1dced572012-04-05 16:54:08 +0200621
622#define luaV_pushtype(typ,tname,luatyp) \
623 static luatyp * \
624 luaV_push##tname (lua_State *L, typ *obj) \
625 { \
626 luatyp *o = NULL; \
627 if (obj == NULL) \
628 lua_pushnil(L); \
629 else { \
630 luaV_getudata(L, obj); \
631 if (lua_isnil(L, -1)) /* not interned? */ \
632 { \
633 lua_pop(L, 1); \
634 o = luaV_new##tname(L, obj); \
635 } \
636 else \
637 o = (luatyp *) lua_touserdata(L, -1); \
638 } \
639 return o; \
640 }
641
642#define luaV_type_tostring(tname,luatname) \
643 static int \
644 luaV_##tname##_tostring (lua_State *L) \
645 { \
646 lua_pushfstring(L, "%s: %p", luatname, lua_touserdata(L, 1)); \
647 return 1; \
648 }
649
Bram Moolenaar1dced572012-04-05 16:54:08 +0200650/* ======= List type ======= */
651
652 static luaV_List *
653luaV_newlist (lua_State *L, list_T *lis)
654{
655 luaV_List *l = (luaV_List *) lua_newuserdata(L, sizeof(luaV_List));
656 *l = lis;
657 lis->lv_refcount++; /* reference in Lua */
658 luaV_setudata(L, lis); /* cache[lis] = udata */
659 luaV_getfield(L, LUAVIM_LIST);
660 lua_setmetatable(L, -2);
661 return l;
662}
663
664luaV_pushtype(list_T, list, luaV_List)
665luaV_type_tostring(list, LUAVIM_LIST)
666
667 static int
Bram Moolenaar1dced572012-04-05 16:54:08 +0200668luaV_list_len (lua_State *L)
669{
670 list_T *l = luaV_unbox(L, luaV_List, 1);
671 lua_pushinteger(L, (l == NULL) ? 0 : (int) l->lv_len);
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200672 return 1;
673}
674
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200675 static int
Bram Moolenaar1dced572012-04-05 16:54:08 +0200676luaV_list_iter (lua_State *L)
677{
678 listitem_T *li = (listitem_T *) lua_touserdata(L, lua_upvalueindex(2));
679 if (li == NULL) return 0;
680 luaV_pushtypval(L, &li->li_tv);
681 lua_pushlightuserdata(L, (void *) li->li_next);
682 lua_replace(L, lua_upvalueindex(2));
683 return 1;
684}
685
686 static int
687luaV_list_call (lua_State *L)
688{
689 list_T *l = luaV_unbox(L, luaV_List, 1);
690 lua_pushvalue(L, lua_upvalueindex(1)); /* pass cache table along */
691 lua_pushlightuserdata(L, (void *) l->lv_first);
692 lua_pushcclosure(L, luaV_list_iter, 2);
693 return 1;
694}
695
696 static int
697luaV_list_index (lua_State *L)
698{
699 list_T *l = luaV_unbox(L, luaV_List, 1);
700 if (lua_isnumber(L, 2)) /* list item? */
701 {
702 listitem_T *li = list_find(l, (long) luaL_checkinteger(L, 2));
703 if (li == NULL)
704 lua_pushnil(L);
705 else
706 luaV_pushtypval(L, &li->li_tv);
707 }
708 else if (lua_isstring(L, 2)) /* method? */
709 {
710 const char *s = lua_tostring(L, 2);
711 if (strncmp(s, "add", 3) == 0
712 || strncmp(s, "insert", 6) == 0
713 || strncmp(s, "extend", 6) == 0)
714 {
715 lua_getmetatable(L, 1);
716 lua_getfield(L, -1, s);
717 }
718 else
719 lua_pushnil(L);
720 }
721 else
722 lua_pushnil(L);
723 return 1;
724}
725
726 static int
727luaV_list_newindex (lua_State *L)
728{
729 list_T *l = luaV_unbox(L, luaV_List, 1);
730 long n = (long) luaL_checkinteger(L, 2);
731 listitem_T *li;
732 if (l->lv_lock)
733 luaL_error(L, "list is locked");
734 li = list_find(l, n);
735 if (li == NULL) return 0;
736 if (lua_isnil(L, 3)) /* remove? */
737 {
Bram Moolenaardb913952012-06-29 12:54:53 +0200738 list_remove(l, li, li);
Bram Moolenaar1dced572012-04-05 16:54:08 +0200739 clear_tv(&li->li_tv);
740 vim_free(li);
741 }
742 else
743 {
744 typval_T v;
745 luaV_totypval(L, 3, &v);
746 clear_tv(&li->li_tv);
747 copy_tv(&v, &li->li_tv);
748 }
749 return 0;
750}
751
752 static int
753luaV_list_add (lua_State *L)
754{
755 luaV_List *lis = luaV_checkudata(L, 1, LUAVIM_LIST);
756 list_T *l = (list_T *) luaV_checkcache(L, (void *) *lis);
757 listitem_T *li;
758 if (l->lv_lock)
759 luaL_error(L, "list is locked");
760 li = listitem_alloc();
761 if (li != NULL)
762 {
763 typval_T v;
764 lua_settop(L, 2);
765 luaV_totypval(L, 2, &v);
Bram Moolenaardb913952012-06-29 12:54:53 +0200766 list_append_tv(l, &v);
Bram Moolenaar1dced572012-04-05 16:54:08 +0200767 }
768 lua_settop(L, 1);
769 return 1;
770}
771
772 static int
773luaV_list_insert (lua_State *L)
774{
775 luaV_List *lis = luaV_checkudata(L, 1, LUAVIM_LIST);
776 list_T *l = (list_T *) luaV_checkcache(L, (void *) *lis);
777 long pos = luaL_optlong(L, 3, 0);
778 listitem_T *li = NULL;
779 typval_T v;
780 if (l->lv_lock)
781 luaL_error(L, "list is locked");
782 if (pos < l->lv_len)
783 {
784 li = list_find(l, pos);
785 if (li == NULL)
786 luaL_error(L, "invalid position");
787 }
788 lua_settop(L, 2);
789 luaV_totypval(L, 2, &v);
790 list_insert_tv(l, &v, li);
791 lua_settop(L, 1);
792 return 1;
793}
794
795static const luaL_Reg luaV_List_mt[] = {
796 {"__tostring", luaV_list_tostring},
Bram Moolenaar1dced572012-04-05 16:54:08 +0200797 {"__len", luaV_list_len},
798 {"__call", luaV_list_call},
799 {"__index", luaV_list_index},
800 {"__newindex", luaV_list_newindex},
801 {"add", luaV_list_add},
802 {"insert", luaV_list_insert},
803 {NULL, NULL}
804};
805
806
807/* ======= Dict type ======= */
808
809 static luaV_Dict *
810luaV_newdict (lua_State *L, dict_T *dic)
811{
812 luaV_Dict *d = (luaV_Dict *) lua_newuserdata(L, sizeof(luaV_Dict));
813 *d = dic;
814 dic->dv_refcount++; /* reference in Lua */
815 luaV_setudata(L, dic); /* cache[dic] = udata */
816 luaV_getfield(L, LUAVIM_DICT);
817 lua_setmetatable(L, -2);
818 return d;
819}
820
821luaV_pushtype(dict_T, dict, luaV_Dict)
822luaV_type_tostring(dict, LUAVIM_DICT)
823
824 static int
Bram Moolenaar1dced572012-04-05 16:54:08 +0200825luaV_dict_len (lua_State *L)
826{
827 dict_T *d = luaV_unbox(L, luaV_Dict, 1);
828 lua_pushinteger(L, (d == NULL) ? 0 : (int) d->dv_hashtab.ht_used);
829 return 1;
830}
831
832 static int
Bram Moolenaarfeeaa682013-02-14 22:19:51 +0100833luaV_dict_iter (lua_State *L UNUSED)
Bram Moolenaar1dced572012-04-05 16:54:08 +0200834{
Bram Moolenaarfeeaa682013-02-14 22:19:51 +0100835#ifdef FEAT_EVAL
Bram Moolenaar1dced572012-04-05 16:54:08 +0200836 hashitem_T *hi = (hashitem_T *) lua_touserdata(L, lua_upvalueindex(2));
837 int n = lua_tointeger(L, lua_upvalueindex(3));
838 dictitem_T *di;
839 if (n <= 0) return 0;
840 while (HASHITEM_EMPTY(hi)) hi++;
841 di = dict_lookup(hi);
842 lua_pushstring(L, (char *) hi->hi_key);
843 luaV_pushtypval(L, &di->di_tv);
844 lua_pushlightuserdata(L, (void *) (hi + 1));
845 lua_replace(L, lua_upvalueindex(2));
846 lua_pushinteger(L, n - 1);
847 lua_replace(L, lua_upvalueindex(3));
848 return 2;
Bram Moolenaarfeeaa682013-02-14 22:19:51 +0100849#else
850 return 0;
851#endif
Bram Moolenaar1dced572012-04-05 16:54:08 +0200852}
853
854 static int
855luaV_dict_call (lua_State *L)
856{
857 dict_T *d = luaV_unbox(L, luaV_Dict, 1);
858 hashtab_T *ht = &d->dv_hashtab;
859 lua_pushvalue(L, lua_upvalueindex(1)); /* pass cache table along */
860 lua_pushlightuserdata(L, (void *) ht->ht_array);
861 lua_pushinteger(L, ht->ht_used); /* # remaining items */
862 lua_pushcclosure(L, luaV_dict_iter, 3);
863 return 1;
864}
865
866 static int
867luaV_dict_index (lua_State *L)
868{
869 dict_T *d = luaV_unbox(L, luaV_Dict, 1);
870 char_u *key = (char_u *) luaL_checkstring(L, 2);
871 dictitem_T *di = dict_find(d, key, -1);
872 if (di == NULL)
873 lua_pushnil(L);
874 else
875 luaV_pushtypval(L, &di->di_tv);
876 return 1;
877}
878
879 static int
880luaV_dict_newindex (lua_State *L)
881{
882 dict_T *d = luaV_unbox(L, luaV_Dict, 1);
883 char_u *key = (char_u *) luaL_checkstring(L, 2);
884 dictitem_T *di;
885 if (d->dv_lock)
886 luaL_error(L, "dict is locked");
887 di = dict_find(d, key, -1);
888 if (di == NULL) /* non-existing key? */
889 {
890 if (lua_isnil(L, 3)) return 0;
891 di = dictitem_alloc(key);
892 if (di == NULL) return 0;
893 if (dict_add(d, di) == FAIL)
894 {
895 vim_free(di);
896 return 0;
897 }
898 }
899 else
900 clear_tv(&di->di_tv);
901 if (lua_isnil(L, 3)) /* remove? */
902 {
903 hashitem_T *hi = hash_find(&d->dv_hashtab, di->di_key);
904 hash_remove(&d->dv_hashtab, hi);
905 dictitem_free(di);
906 }
907 else {
908 typval_T v;
909 luaV_totypval(L, 3, &v);
910 copy_tv(&v, &di->di_tv);
911 }
912 return 0;
913}
914
915static const luaL_Reg luaV_Dict_mt[] = {
916 {"__tostring", luaV_dict_tostring},
Bram Moolenaar1dced572012-04-05 16:54:08 +0200917 {"__len", luaV_dict_len},
918 {"__call", luaV_dict_call},
919 {"__index", luaV_dict_index},
920 {"__newindex", luaV_dict_newindex},
921 {NULL, NULL}
922};
923
924
925/* ======= Buffer type ======= */
926
927luaV_newtype(buf_T, buffer, luaV_Buffer, LUAVIM_BUFFER)
928luaV_pushtype(buf_T, buffer, luaV_Buffer)
929luaV_type_tostring(buffer, LUAVIM_BUFFER)
930
931 static int
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200932luaV_buffer_len(lua_State *L)
933{
Bram Moolenaar1dced572012-04-05 16:54:08 +0200934 buf_T *b = (buf_T *) luaV_checkvalid(L, luaV_Buffer, 1);
935 lua_pushinteger(L, b->b_ml.ml_line_count);
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200936 return 1;
937}
938
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200939 static int
940luaV_buffer_call(lua_State *L)
941{
Bram Moolenaar1dced572012-04-05 16:54:08 +0200942 buf_T *b = (buf_T *) luaV_checkvalid(L, luaV_Buffer, 1);
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200943 lua_settop(L, 1);
Bram Moolenaar1dced572012-04-05 16:54:08 +0200944 set_curbuf(b, DOBUF_SPLIT);
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200945 return 1;
946}
947
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200948 static int
949luaV_buffer_index(lua_State *L)
950{
Bram Moolenaar1dced572012-04-05 16:54:08 +0200951 buf_T *b = (buf_T *) luaV_checkvalid(L, luaV_Buffer, 1);
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200952 linenr_T n = (linenr_T) lua_tointeger(L, 2);
Bram Moolenaar1dced572012-04-05 16:54:08 +0200953 if (n > 0 && n <= b->b_ml.ml_line_count)
954 luaV_pushline(L, b, n);
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200955 else if (lua_isstring(L, 2))
956 {
957 const char *s = lua_tostring(L, 2);
958 if (strncmp(s, "name", 4) == 0)
Bram Moolenaar1dced572012-04-05 16:54:08 +0200959 lua_pushstring(L, (char *) b->b_sfname);
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200960 else if (strncmp(s, "fname", 5) == 0)
Bram Moolenaar1dced572012-04-05 16:54:08 +0200961 lua_pushstring(L, (char *) b->b_ffname);
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200962 else if (strncmp(s, "number", 6) == 0)
Bram Moolenaar1dced572012-04-05 16:54:08 +0200963 lua_pushinteger(L, b->b_fnum);
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200964 /* methods */
965 else if (strncmp(s, "insert", 6) == 0
966 || strncmp(s, "next", 4) == 0
967 || strncmp(s, "previous", 8) == 0
968 || strncmp(s, "isvalid", 7) == 0)
969 {
970 lua_getmetatable(L, 1);
971 lua_getfield(L, -1, s);
972 }
973 else
974 lua_pushnil(L);
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200975 }
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200976 else
977 lua_pushnil(L);
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200978 return 1;
979}
980
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200981 static int
982luaV_buffer_newindex(lua_State *L)
983{
Bram Moolenaar1dced572012-04-05 16:54:08 +0200984 buf_T *b = (buf_T *) luaV_checkvalid(L, luaV_Buffer, 1);
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200985 linenr_T n = (linenr_T) luaL_checkinteger(L, 2);
986#ifdef HAVE_SANDBOX
987 luaV_checksandbox(L);
988#endif
Bram Moolenaar1dced572012-04-05 16:54:08 +0200989 if (n < 1 || n > b->b_ml.ml_line_count)
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200990 luaL_error(L, "invalid line number");
991 if (lua_isnil(L, 3)) /* delete line */
992 {
993 buf_T *buf = curbuf;
Bram Moolenaar1dced572012-04-05 16:54:08 +0200994 curbuf = b;
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200995 if (u_savedel(n, 1L) == FAIL)
996 {
997 curbuf = buf;
998 luaL_error(L, "cannot save undo information");
999 }
1000 else if (ml_delete(n, FALSE) == FAIL)
1001 {
1002 curbuf = buf;
1003 luaL_error(L, "cannot delete line");
1004 }
1005 else {
1006 deleted_lines_mark(n, 1L);
Bram Moolenaar1dced572012-04-05 16:54:08 +02001007 if (b == curwin->w_buffer) /* fix cursor in current window? */
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001008 {
1009 if (curwin->w_cursor.lnum >= n)
1010 {
1011 if (curwin->w_cursor.lnum > n)
1012 {
1013 curwin->w_cursor.lnum -= 1;
1014 check_cursor_col();
1015 }
1016 else check_cursor();
1017 changed_cline_bef_curs();
1018 }
1019 invalidate_botline();
1020 }
1021 }
1022 curbuf = buf;
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001023 }
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001024 else if (lua_isstring(L, 3)) /* update line */
1025 {
1026 buf_T *buf = curbuf;
Bram Moolenaar1dced572012-04-05 16:54:08 +02001027 curbuf = b;
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001028 if (u_savesub(n) == FAIL)
1029 {
1030 curbuf = buf;
1031 luaL_error(L, "cannot save undo information");
1032 }
1033 else if (ml_replace(n, luaV_toline(L, 3), TRUE) == FAIL)
1034 {
1035 curbuf = buf;
1036 luaL_error(L, "cannot replace line");
1037 }
1038 else changed_bytes(n, 0);
1039 curbuf = buf;
Bram Moolenaar1dced572012-04-05 16:54:08 +02001040 if (b == curwin->w_buffer)
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001041 check_cursor_col();
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001042 }
1043 else
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001044 luaL_error(L, "wrong argument to change line");
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001045 return 0;
1046}
1047
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001048 static int
1049luaV_buffer_insert(lua_State *L)
1050{
Bram Moolenaar1dced572012-04-05 16:54:08 +02001051 luaV_Buffer *lb = luaV_checkudata(L, 1, LUAVIM_BUFFER);
1052 buf_T *b = (buf_T *) luaV_checkcache(L, (void *) *lb);
1053 linenr_T last = b->b_ml.ml_line_count;
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001054 linenr_T n = (linenr_T) luaL_optinteger(L, 3, last);
1055 buf_T *buf;
1056 luaL_checktype(L, 2, LUA_TSTRING);
1057#ifdef HAVE_SANDBOX
1058 luaV_checksandbox(L);
1059#endif
1060 /* fix insertion line */
1061 if (n < 0) n = 0;
1062 if (n > last) n = last;
1063 /* insert */
1064 buf = curbuf;
Bram Moolenaar1dced572012-04-05 16:54:08 +02001065 curbuf = b;
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001066 if (u_save(n, n + 1) == FAIL)
1067 {
1068 curbuf = buf;
1069 luaL_error(L, "cannot save undo information");
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001070 }
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001071 else if (ml_append(n, luaV_toline(L, 2), 0, FALSE) == FAIL)
1072 {
1073 curbuf = buf;
1074 luaL_error(L, "cannot insert line");
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001075 }
1076 else
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001077 appended_lines_mark(n, 1L);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001078 curbuf = buf;
1079 update_screen(VALID);
1080 return 0;
1081}
1082
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001083 static int
1084luaV_buffer_next(lua_State *L)
1085{
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001086 luaV_Buffer *b = luaV_checkudata(L, 1, LUAVIM_BUFFER);
Bram Moolenaar1dced572012-04-05 16:54:08 +02001087 buf_T *buf = (buf_T *) luaV_checkcache(L, (void *) *b);
1088 luaV_pushbuffer(L, buf->b_next);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001089 return 1;
1090}
1091
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001092 static int
1093luaV_buffer_previous(lua_State *L)
1094{
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001095 luaV_Buffer *b = luaV_checkudata(L, 1, LUAVIM_BUFFER);
Bram Moolenaar1dced572012-04-05 16:54:08 +02001096 buf_T *buf = (buf_T *) luaV_checkcache(L, (void *) *b);
1097 luaV_pushbuffer(L, buf->b_prev);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001098 return 1;
1099}
1100
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001101 static int
1102luaV_buffer_isvalid(lua_State *L)
1103{
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001104 luaV_Buffer *b = luaV_checkudata(L, 1, LUAVIM_BUFFER);
Bram Moolenaar1dced572012-04-05 16:54:08 +02001105 luaV_getudata(L, *b);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001106 lua_pushboolean(L, !lua_isnil(L, -1));
1107 return 1;
1108}
1109
1110static const luaL_Reg luaV_Buffer_mt[] = {
1111 {"__tostring", luaV_buffer_tostring},
1112 {"__len", luaV_buffer_len},
1113 {"__call", luaV_buffer_call},
1114 {"__index", luaV_buffer_index},
1115 {"__newindex", luaV_buffer_newindex},
1116 {"insert", luaV_buffer_insert},
1117 {"next", luaV_buffer_next},
1118 {"previous", luaV_buffer_previous},
1119 {"isvalid", luaV_buffer_isvalid},
1120 {NULL, NULL}
1121};
1122
1123
1124/* ======= Window type ======= */
1125
Bram Moolenaar1dced572012-04-05 16:54:08 +02001126luaV_newtype(win_T, window, luaV_Window, LUAVIM_WINDOW)
1127luaV_pushtype(win_T, window, luaV_Window)
1128luaV_type_tostring(window, LUAVIM_WINDOW)
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001129
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001130 static int
1131luaV_window_call(lua_State *L)
1132{
Bram Moolenaar1dced572012-04-05 16:54:08 +02001133 win_T *w = (win_T *) luaV_checkvalid(L, luaV_Window, 1);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001134 lua_settop(L, 1);
Bram Moolenaar1dced572012-04-05 16:54:08 +02001135 win_goto(w);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001136 return 1;
1137}
1138
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001139 static int
1140luaV_window_index(lua_State *L)
1141{
Bram Moolenaar1dced572012-04-05 16:54:08 +02001142 win_T *w = (win_T *) luaV_checkvalid(L, luaV_Window, 1);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001143 const char *s = luaL_checkstring(L, 2);
1144 if (strncmp(s, "buffer", 6) == 0)
Bram Moolenaar1dced572012-04-05 16:54:08 +02001145 luaV_pushbuffer(L, w->w_buffer);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001146 else if (strncmp(s, "line", 4) == 0)
Bram Moolenaar1dced572012-04-05 16:54:08 +02001147 lua_pushinteger(L, w->w_cursor.lnum);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001148 else if (strncmp(s, "col", 3) == 0)
Bram Moolenaar1dced572012-04-05 16:54:08 +02001149 lua_pushinteger(L, w->w_cursor.col + 1);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001150#ifdef FEAT_VERTSPLIT
1151 else if (strncmp(s, "width", 5) == 0)
Bram Moolenaar1dced572012-04-05 16:54:08 +02001152 lua_pushinteger(L, W_WIDTH(w));
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001153#endif
1154 else if (strncmp(s, "height", 6) == 0)
Bram Moolenaar1dced572012-04-05 16:54:08 +02001155 lua_pushinteger(L, w->w_height);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001156 /* methods */
1157 else if (strncmp(s, "next", 4) == 0
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001158 || strncmp(s, "previous", 8) == 0
1159 || strncmp(s, "isvalid", 7) == 0)
1160 {
1161 lua_getmetatable(L, 1);
1162 lua_getfield(L, -1, s);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001163 }
1164 else
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001165 lua_pushnil(L);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001166 return 1;
1167}
1168
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001169 static int
1170luaV_window_newindex (lua_State *L)
1171{
Bram Moolenaar1dced572012-04-05 16:54:08 +02001172 win_T *w = (win_T *) luaV_checkvalid(L, luaV_Window, 1);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001173 const char *s = luaL_checkstring(L, 2);
1174 int v = luaL_checkinteger(L, 3);
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001175 if (strncmp(s, "line", 4) == 0)
1176 {
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001177#ifdef HAVE_SANDBOX
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001178 luaV_checksandbox(L);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001179#endif
Bram Moolenaar1dced572012-04-05 16:54:08 +02001180 if (v < 1 || v > w->w_buffer->b_ml.ml_line_count)
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001181 luaL_error(L, "line out of range");
Bram Moolenaar1dced572012-04-05 16:54:08 +02001182 w->w_cursor.lnum = v;
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001183 update_screen(VALID);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001184 }
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001185 else if (strncmp(s, "col", 3) == 0)
1186 {
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001187#ifdef HAVE_SANDBOX
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001188 luaV_checksandbox(L);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001189#endif
Bram Moolenaar1dced572012-04-05 16:54:08 +02001190 w->w_cursor.col = v - 1;
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001191 update_screen(VALID);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001192 }
1193#ifdef FEAT_VERTSPLIT
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001194 else if (strncmp(s, "width", 5) == 0)
1195 {
1196 win_T *win = curwin;
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001197#ifdef FEAT_GUI
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001198 need_mouse_correct = TRUE;
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001199#endif
Bram Moolenaar1dced572012-04-05 16:54:08 +02001200 curwin = w;
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001201 win_setwidth(v);
1202 curwin = win;
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001203 }
1204#endif
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001205 else if (strncmp(s, "height", 6) == 0)
1206 {
1207 win_T *win = curwin;
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001208#ifdef FEAT_GUI
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001209 need_mouse_correct = TRUE;
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001210#endif
Bram Moolenaar1dced572012-04-05 16:54:08 +02001211 curwin = w;
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001212 win_setheight(v);
1213 curwin = win;
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001214 }
1215 else
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001216 luaL_error(L, "invalid window property: `%s'", s);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001217 return 0;
1218}
1219
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001220 static int
1221luaV_window_next(lua_State *L)
1222{
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001223 luaV_Window *w = luaV_checkudata(L, 1, LUAVIM_WINDOW);
Bram Moolenaar1dced572012-04-05 16:54:08 +02001224 win_T *win = (win_T *) luaV_checkcache(L, (void *) *w);
1225 luaV_pushwindow(L, win->w_next);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001226 return 1;
1227}
1228
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001229 static int
1230luaV_window_previous(lua_State *L)
1231{
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001232 luaV_Window *w = luaV_checkudata(L, 1, LUAVIM_WINDOW);
Bram Moolenaar1dced572012-04-05 16:54:08 +02001233 win_T *win = (win_T *) luaV_checkcache(L, (void *) *w);
1234 luaV_pushwindow(L, win->w_prev);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001235 return 1;
1236}
1237
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001238 static int
1239luaV_window_isvalid(lua_State *L)
1240{
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001241 luaV_Window *w = luaV_checkudata(L, 1, LUAVIM_WINDOW);
Bram Moolenaar1dced572012-04-05 16:54:08 +02001242 luaV_getudata(L, *w);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001243 lua_pushboolean(L, !lua_isnil(L, -1));
1244 return 1;
1245}
1246
1247static const luaL_Reg luaV_Window_mt[] = {
1248 {"__tostring", luaV_window_tostring},
1249 {"__call", luaV_window_call},
1250 {"__index", luaV_window_index},
1251 {"__newindex", luaV_window_newindex},
1252 {"next", luaV_window_next},
1253 {"previous", luaV_window_previous},
1254 {"isvalid", luaV_window_isvalid},
1255 {NULL, NULL}
1256};
1257
1258
1259/* ======= Vim module ======= */
1260
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001261 static int
1262luaV_print(lua_State *L)
1263{
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001264 int i, n = lua_gettop(L); /* nargs */
1265 const char *s;
1266 size_t l;
1267 luaL_Buffer b;
1268 luaL_buffinit(L, &b);
1269 lua_getglobal(L, "tostring");
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001270 for (i = 1; i <= n; i++)
1271 {
1272 lua_pushvalue(L, -1); /* tostring */
1273 lua_pushvalue(L, i); /* arg */
1274 lua_call(L, 1, 1);
1275 s = lua_tolstring(L, -1, &l);
1276 if (s == NULL)
1277 return luaL_error(L, "cannot convert to string");
1278 if (i > 1) luaL_addchar(&b, ' '); /* use space instead of tab */
1279 luaV_addlstring(&b, s, l, 0);
1280 lua_pop(L, 1);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001281 }
1282 luaL_pushresult(&b);
1283 luaV_msg(L);
1284 return 0;
1285}
1286
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001287 static int
Bram Moolenaar38e2b062011-09-21 17:15:39 +02001288luaV_debug(lua_State *L)
1289{
1290 lua_settop(L, 0);
1291 lua_getglobal(L, "vim");
1292 lua_getfield(L, -1, "eval");
1293 lua_remove(L, -2); /* vim.eval at position 1 */
1294 for (;;)
1295 {
1296 const char *input;
1297 size_t l;
1298 lua_pushvalue(L, 1); /* vim.eval */
1299 lua_pushliteral(L, "input('lua_debug> ')");
1300 lua_call(L, 1, 1); /* return string */
1301 input = lua_tolstring(L, -1, &l);
1302 if (l == 0 || strcmp(input, "cont") == 0)
1303 return 0;
1304 msg_putchar('\n'); /* avoid outputting on input line */
1305 if (luaL_loadbuffer(L, input, l, "=(debug command)")
1306 || lua_pcall(L, 0, 0, 0))
1307 luaV_emsg(L);
1308 lua_settop(L, 1); /* remove eventual returns, but keep vim.eval */
1309 }
1310}
1311
1312 static int
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001313luaV_command(lua_State *L)
1314{
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001315 do_cmdline_cmd((char_u *) luaL_checkstring(L, 1));
1316 update_screen(VALID);
1317 return 0;
1318}
1319
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001320 static int
1321luaV_eval(lua_State *L)
1322{
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001323 typval_T *tv = eval_expr((char_u *) luaL_checkstring(L, 1), NULL);
1324 if (tv == NULL) luaL_error(L, "invalid expression");
1325 luaV_pushtypval(L, tv);
1326 return 1;
1327}
1328
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001329 static int
Bram Moolenaar0d2e4fc2010-07-18 12:35:47 +02001330luaV_beep(lua_State *L UNUSED)
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001331{
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001332 vim_beep();
1333 return 0;
1334}
1335
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001336 static int
1337luaV_line(lua_State *L)
1338{
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001339 luaV_pushline(L, curbuf, curwin->w_cursor.lnum);
1340 return 1;
1341}
1342
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001343 static int
Bram Moolenaar1dced572012-04-05 16:54:08 +02001344luaV_list(lua_State *L)
1345{
1346 list_T *l = list_alloc();
1347 if (l == NULL)
1348 lua_pushnil(L);
1349 else
1350 luaV_newlist(L, l);
1351 return 1;
1352}
1353
1354 static int
1355luaV_dict(lua_State *L)
1356{
1357 dict_T *d = dict_alloc();
1358 if (d == NULL)
1359 lua_pushnil(L);
1360 else
1361 luaV_newdict(L, d);
1362 return 1;
1363}
1364
1365 static int
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001366luaV_buffer(lua_State *L)
1367{
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001368 buf_T *buf;
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001369 if (lua_isstring(L, 1)) /* get by number or name? */
1370 {
1371 if (lua_isnumber(L, 1)) /* by number? */
1372 {
1373 int n = lua_tointeger(L, 1);
1374 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
1375 if (buf->b_fnum == n) break;
1376 }
1377 else { /* by name */
1378 size_t l;
1379 const char *s = lua_tolstring(L, 1, &l);
1380 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
1381 {
1382 if (buf->b_ffname == NULL || buf->b_sfname == NULL)
1383 {
1384 if (l == 0) break;
1385 }
Bram Moolenaar0d2e4fc2010-07-18 12:35:47 +02001386 else if (strncmp(s, (char *)buf->b_ffname, l) == 0
1387 || strncmp(s, (char *)buf->b_sfname, l) == 0)
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001388 break;
1389 }
1390 }
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001391 }
Bram Moolenaar1dced572012-04-05 16:54:08 +02001392 else
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001393 buf = (lua_toboolean(L, 1)) ? firstbuf : curbuf; /* first buffer? */
Bram Moolenaar1dced572012-04-05 16:54:08 +02001394 luaV_pushbuffer(L, buf);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001395 return 1;
1396}
1397
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001398 static int
1399luaV_window(lua_State *L)
1400{
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001401 win_T *win;
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001402 if (lua_isnumber(L, 1)) /* get by number? */
1403 {
1404 int n = lua_tointeger(L, 1);
1405 for (win = firstwin; win != NULL; win = win->w_next, n--)
1406 if (n == 1) break;
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001407 }
Bram Moolenaar1dced572012-04-05 16:54:08 +02001408 else
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001409 win = (lua_toboolean(L, 1)) ? firstwin : curwin; /* first window? */
Bram Moolenaar1dced572012-04-05 16:54:08 +02001410 luaV_pushwindow(L, win);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001411 return 1;
1412}
1413
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001414 static int
1415luaV_open(lua_State *L)
1416{
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001417 char_u *s = NULL;
1418#ifdef HAVE_SANDBOX
1419 luaV_checksandbox(L);
1420#endif
1421 if (lua_isstring(L, 1)) s = (char_u *) lua_tostring(L, 1);
Bram Moolenaarfa263a52011-12-08 16:00:16 +01001422 luaV_pushbuffer(L, buflist_new(s, NULL, 1L, BLN_LISTED));
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001423 return 1;
1424}
1425
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001426 static int
Bram Moolenaar1dced572012-04-05 16:54:08 +02001427luaV_type(lua_State *L)
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001428{
Bram Moolenaar1dced572012-04-05 16:54:08 +02001429 luaL_checkany(L, 1);
1430 if (lua_type(L, 1) == LUA_TUSERDATA) /* check vim udata? */
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001431 {
Bram Moolenaar1dced572012-04-05 16:54:08 +02001432 lua_settop(L, 1);
1433 if (lua_getmetatable(L, 1))
1434 {
1435 luaV_getfield(L, LUAVIM_LIST);
1436 if (lua_rawequal(L, -1, 2))
1437 {
1438 lua_pushstring(L, "list");
1439 return 1;
1440 }
1441 luaV_getfield(L, LUAVIM_DICT);
1442 if (lua_rawequal(L, -1, 2))
1443 {
1444 lua_pushstring(L, "dict");
1445 return 1;
1446 }
1447 luaV_getfield(L, LUAVIM_BUFFER);
1448 if (lua_rawequal(L, -1, 2))
1449 {
1450 lua_pushstring(L, "buffer");
1451 return 1;
1452 }
1453 luaV_getfield(L, LUAVIM_WINDOW);
1454 if (lua_rawequal(L, -1, 2))
1455 {
1456 lua_pushstring(L, "window");
1457 return 1;
1458 }
1459 }
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001460 }
Bram Moolenaar1dced572012-04-05 16:54:08 +02001461 lua_pushstring(L, luaL_typename(L, 1)); /* fallback */
1462 return 1;
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001463}
1464
1465static const luaL_Reg luaV_module[] = {
1466 {"command", luaV_command},
1467 {"eval", luaV_eval},
1468 {"beep", luaV_beep},
1469 {"line", luaV_line},
Bram Moolenaar1dced572012-04-05 16:54:08 +02001470 {"list", luaV_list},
1471 {"dict", luaV_dict},
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001472 {"buffer", luaV_buffer},
1473 {"window", luaV_window},
1474 {"open", luaV_open},
Bram Moolenaar1dced572012-04-05 16:54:08 +02001475 {"type", luaV_type},
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001476 {NULL, NULL}
1477};
1478
Bram Moolenaar1dced572012-04-05 16:54:08 +02001479/* for freeing list, dict, buffer and window objects; lightuserdata as arg */
1480 static int
1481luaV_free(lua_State *L)
1482{
1483 lua_pushnil(L);
1484 luaV_setudata(L, lua_touserdata(L, 1));
1485 return 0;
1486}
1487
1488 static int
1489luaV_luaeval (lua_State *L)
1490{
1491 luaL_Buffer b;
1492 size_t l;
1493 const char *str = lua_tolstring(L, 1, &l);
1494 typval_T *arg = (typval_T *) lua_touserdata(L, 2);
1495 typval_T *rettv = (typval_T *) lua_touserdata(L, 3);
1496 luaL_buffinit(L, &b);
1497 luaL_addlstring(&b, LUAVIM_EVALHEADER, sizeof(LUAVIM_EVALHEADER) - 1);
1498 luaL_addlstring(&b, str, l);
1499 luaL_pushresult(&b);
1500 str = lua_tolstring(L, -1, &l);
1501 if (luaL_loadbuffer(L, str, l, LUAVIM_EVALNAME)) /* compile error? */
1502 {
1503 luaV_emsg(L);
1504 return 0;
1505 }
1506 luaV_pushtypval(L, arg);
1507 if (lua_pcall(L, 1, 1, 0)) /* running error? */
1508 {
1509 luaV_emsg(L);
1510 return 0;
1511 }
1512 luaV_totypval(L, -1, rettv);
1513 return 0;
1514}
1515
1516 static int
1517luaV_setref (lua_State *L)
1518{
1519 int copyID = lua_tointeger(L, 1);
1520 typval_T tv;
1521 luaV_getfield(L, LUAVIM_LIST);
1522 luaV_getfield(L, LUAVIM_DICT);
1523 lua_pushnil(L);
1524 while (lua_next(L, lua_upvalueindex(1)) != 0) /* traverse cache table */
1525 {
1526 lua_getmetatable(L, -1);
1527 if (lua_rawequal(L, -1, 2)) /* list? */
1528 {
1529 tv.v_type = VAR_LIST;
1530 tv.vval.v_list = (list_T *) lua_touserdata(L, 4); /* key */
1531 }
1532 else if (lua_rawequal(L, -1, 3)) /* dict? */
1533 {
1534 tv.v_type = VAR_DICT;
1535 tv.vval.v_dict = (dict_T *) lua_touserdata(L, 4); /* key */
1536 }
1537 lua_pop(L, 2); /* metatable and value */
Bram Moolenaardb913952012-06-29 12:54:53 +02001538 set_ref_in_item(&tv, copyID);
Bram Moolenaar1dced572012-04-05 16:54:08 +02001539 }
1540 return 0;
1541}
1542
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001543 static int
1544luaopen_vim(lua_State *L)
1545{
Bram Moolenaar1dced572012-04-05 16:54:08 +02001546 /* set cache table */
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001547 lua_newtable(L);
1548 lua_newtable(L);
Bram Moolenaar1dced572012-04-05 16:54:08 +02001549 lua_pushstring(L, "v");
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001550 lua_setfield(L, -2, "__mode");
Bram Moolenaar1dced572012-04-05 16:54:08 +02001551 lua_setmetatable(L, -2); /* cache is weak-valued */
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001552 /* print */
1553 lua_pushcfunction(L, luaV_print);
1554 lua_setglobal(L, "print");
Bram Moolenaar38e2b062011-09-21 17:15:39 +02001555 /* debug.debug */
1556 lua_getglobal(L, "debug");
1557 lua_pushcfunction(L, luaV_debug);
1558 lua_setfield(L, -2, "debug");
1559 lua_pop(L, 1);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001560 /* free */
1561 lua_pushlightuserdata(L, (void *) LUAVIM_FREE);
Bram Moolenaar1dced572012-04-05 16:54:08 +02001562 lua_pushvalue(L, 1); /* cache table */
1563 lua_pushcclosure(L, luaV_free, 1);
1564 lua_rawset(L, LUA_REGISTRYINDEX);
1565 /* luaeval */
1566 lua_pushlightuserdata(L, (void *) LUAVIM_LUAEVAL);
1567 lua_pushvalue(L, 1); /* cache table */
1568 lua_pushcclosure(L, luaV_luaeval, 1);
1569 lua_rawset(L, LUA_REGISTRYINDEX);
1570 /* setref */
1571 lua_pushlightuserdata(L, (void *) LUAVIM_SETREF);
1572 lua_pushvalue(L, 1); /* cache table */
1573 lua_pushcclosure(L, luaV_setref, 1);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001574 lua_rawset(L, LUA_REGISTRYINDEX);
1575 /* register */
Bram Moolenaar1dced572012-04-05 16:54:08 +02001576 luaV_newmetatable(L, LUAVIM_LIST);
1577 lua_pushvalue(L, 1);
1578 luaV_openlib(L, luaV_List_mt, 1);
1579 luaV_newmetatable(L, LUAVIM_DICT);
1580 lua_pushvalue(L, 1);
1581 luaV_openlib(L, luaV_Dict_mt, 1);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001582 luaV_newmetatable(L, LUAVIM_BUFFER);
Bram Moolenaar1dced572012-04-05 16:54:08 +02001583 lua_pushvalue(L, 1); /* cache table */
1584 luaV_openlib(L, luaV_Buffer_mt, 1);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001585 luaV_newmetatable(L, LUAVIM_WINDOW);
Bram Moolenaar1dced572012-04-05 16:54:08 +02001586 lua_pushvalue(L, 1); /* cache table */
1587 luaV_openlib(L, luaV_Window_mt, 1);
1588 lua_newtable(L); /* vim table */
1589 lua_pushvalue(L, 1); /* cache table */
1590 luaV_openlib(L, luaV_module, 1);
1591 lua_setglobal(L, LUAVIM_NAME);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001592 return 0;
1593}
1594
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001595 static lua_State *
1596luaV_newstate(void)
1597{
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001598 lua_State *L = luaL_newstate();
Bram Moolenaar16c98f92010-07-28 22:46:08 +02001599 luaL_openlibs(L); /* core libs */
1600 lua_pushcfunction(L, luaopen_vim); /* vim */
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001601 lua_call(L, 0, 0);
1602 return L;
1603}
1604
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001605 static void
1606luaV_setrange(lua_State *L, int line1, int line2)
1607{
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001608 lua_getglobal(L, LUAVIM_NAME);
1609 lua_pushinteger(L, line1);
1610 lua_setfield(L, -2, "firstline");
1611 lua_pushinteger(L, line2);
1612 lua_setfield(L, -2, "lastline");
1613 lua_pop(L, 1); /* vim table */
1614}
1615
1616
1617/* ======= Interface ======= */
1618
1619static lua_State *L = NULL;
1620
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001621 static int
Bram Moolenaar1dced572012-04-05 16:54:08 +02001622lua_isopen(void)
Bram Moolenaar2bd6a1b2010-08-12 22:14:01 +02001623{
1624 return L != NULL;
1625}
1626
1627 static int
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001628lua_init(void)
1629{
Bram Moolenaar1dced572012-04-05 16:54:08 +02001630 if (!lua_isopen())
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001631 {
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001632#ifdef DYNAMIC_LUA
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001633 if (!lua_enabled(TRUE))
1634 {
1635 EMSG(_("Lua library cannot be loaded."));
1636 return FAIL;
1637 }
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001638#endif
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001639 L = luaV_newstate();
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001640 }
1641 return OK;
1642}
1643
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001644 void
1645lua_end(void)
1646{
Bram Moolenaar1dced572012-04-05 16:54:08 +02001647 if (lua_isopen())
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001648 {
1649 lua_close(L);
1650 L = NULL;
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001651#ifdef DYNAMIC_LUA
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001652 end_dynamic_lua();
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001653#endif
1654 }
1655}
1656
1657/* ex commands */
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001658 void
1659ex_lua(exarg_T *eap)
1660{
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001661 char *script;
1662 if (lua_init() == FAIL) return;
1663 script = (char *) script_get(eap, eap->arg);
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001664 if (!eap->skip)
1665 {
1666 char *s = (script) ? script : (char *) eap->arg;
1667 luaV_setrange(L, eap->line1, eap->line2);
1668 if (luaL_loadbuffer(L, s, strlen(s), LUAVIM_CHUNKNAME)
1669 || lua_pcall(L, 0, 0, 0))
1670 luaV_emsg(L);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001671 }
1672 if (script != NULL) vim_free(script);
1673}
1674
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001675 void
1676ex_luado(exarg_T *eap)
1677{
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001678 linenr_T l;
1679 const char *s = (const char *) eap->arg;
1680 luaL_Buffer b;
1681 size_t len;
1682 if (lua_init() == FAIL) return;
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001683 if (u_save(eap->line1 - 1, eap->line2 + 1) == FAIL)
1684 {
1685 EMSG(_("cannot save undo information"));
1686 return;
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001687 }
1688 luaV_setrange(L, eap->line1, eap->line2);
1689 luaL_buffinit(L, &b);
Bram Moolenaarbd2f3c32012-04-06 14:31:00 +02001690 luaL_addlstring(&b, "return function(line, linenr) ", 30); /* header */
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001691 luaL_addlstring(&b, s, strlen(s));
1692 luaL_addlstring(&b, " end", 4); /* footer */
1693 luaL_pushresult(&b);
1694 s = lua_tolstring(L, -1, &len);
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001695 if (luaL_loadbuffer(L, s, len, LUAVIM_CHUNKNAME))
1696 {
1697 luaV_emsg(L);
1698 lua_pop(L, 1); /* function body */
1699 return;
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001700 }
1701 lua_call(L, 0, 1);
1702 lua_replace(L, -2); /* function -> body */
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001703 for (l = eap->line1; l <= eap->line2; l++)
1704 {
1705 lua_pushvalue(L, -1); /* function */
1706 luaV_pushline(L, curbuf, l); /* current line as arg */
Bram Moolenaarbd2f3c32012-04-06 14:31:00 +02001707 lua_pushinteger(L, l); /* current line number as arg */
1708 if (lua_pcall(L, 2, 1, 0))
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001709 {
1710 luaV_emsg(L);
1711 break;
1712 }
1713 if (lua_isstring(L, -1)) /* update line? */
1714 {
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001715#ifdef HAVE_SANDBOX
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001716 luaV_checksandbox(L);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001717#endif
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001718 ml_replace(l, luaV_toline(L, -1), TRUE);
1719 changed_bytes(l, 0);
1720 lua_pop(L, 1); /* result from luaV_toline */
1721 }
1722 lua_pop(L, 1); /* line */
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001723 }
1724 lua_pop(L, 1); /* function */
1725 check_cursor();
1726 update_screen(NOT_VALID);
1727}
1728
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001729 void
1730ex_luafile(exarg_T *eap)
1731{
1732 if (lua_init() == FAIL)
1733 return;
1734 if (!eap->skip)
1735 {
1736 luaV_setrange(L, eap->line1, eap->line2);
1737 if (luaL_loadfile(L, (char *) eap->arg) || lua_pcall(L, 0, 0, 0))
1738 luaV_emsg(L);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001739 }
1740}
1741
Bram Moolenaar1dced572012-04-05 16:54:08 +02001742#define luaV_freetype(typ,tname) \
1743 void \
1744 lua_##tname##_free(typ *o) \
1745 { \
1746 if (!lua_isopen()) return; \
1747 luaV_getfield(L, LUAVIM_FREE); \
1748 lua_pushlightuserdata(L, (void *) o); \
1749 lua_call(L, 1, 0); \
1750 }
1751
1752luaV_freetype(buf_T, buffer)
1753luaV_freetype(win_T, window)
1754
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001755 void
Bram Moolenaar1dced572012-04-05 16:54:08 +02001756do_luaeval (char_u *str, typval_T *arg, typval_T *rettv)
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001757{
Bram Moolenaar1dced572012-04-05 16:54:08 +02001758 lua_init();
1759 luaV_getfield(L, LUAVIM_LUAEVAL);
1760 lua_pushstring(L, (char *) str);
1761 lua_pushlightuserdata(L, (void *) arg);
1762 lua_pushlightuserdata(L, (void *) rettv);
1763 lua_call(L, 3, 0);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001764}
1765
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001766 void
Bram Moolenaar1dced572012-04-05 16:54:08 +02001767set_ref_in_lua (int copyID)
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001768{
Bram Moolenaar1dced572012-04-05 16:54:08 +02001769 if (!lua_isopen()) return;
1770 luaV_getfield(L, LUAVIM_SETREF);
1771 lua_pushinteger(L, copyID);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001772 lua_call(L, 1, 0);
1773}
1774
1775#endif