blob: 5e669045c04715c80fa6ce51e0368b70afb36211 [file] [log] [blame]
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001/* vi:set ts=8 sts=4 sw=4:
2 *
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"
24
25typedef buf_T *luaV_Buffer;
26typedef win_T *luaV_Window;
27typedef void (*msgfunc_T)(char_u *);
28
29static const char LUAVIM_BUFFER[] = "buffer";
30static const char LUAVIM_WINDOW[] = "window";
31static const char LUAVIM_FREE[] = "luaV_free";
32
33#define luaV_getfield(L, s) \
34 lua_pushlightuserdata((L), (void *)(s)); \
35 lua_rawget((L), LUA_REGISTRYINDEX)
36#define luaV_checksandbox(L) \
37 if (sandbox) luaL_error((L), "not allowed in sandbox")
38#define luaV_msg(L) luaV_msgfunc((L), (msgfunc_T) msg)
39#define luaV_emsg(L) luaV_msgfunc((L), (msgfunc_T) emsg)
40
41
42#ifdef DYNAMIC_LUA
Bram Moolenaar2334b6d2010-07-22 21:32:16 +020043
44#ifndef WIN3264
45# include <dlfcn.h>
46# define HANDLE void*
47# define load_dll(n) dlopen((n), RTLD_LAZY|RTLD_GLOBAL)
48# define symbol_from_dll dlsym
49# define close_dll dlclose
50#else
Bram Moolenaarebbcb822010-10-23 14:02:54 +020051# define load_dll vimLoadLib
Bram Moolenaar2334b6d2010-07-22 21:32:16 +020052# define symbol_from_dll GetProcAddress
53# define close_dll FreeLibrary
54#endif
55
Bram Moolenaar0ba04292010-07-14 23:23:17 +020056/* lauxlib */
57#define luaL_register dll_luaL_register
58#define luaL_typerror dll_luaL_typerror
59#define luaL_checklstring dll_luaL_checklstring
60#define luaL_checkinteger dll_luaL_checkinteger
61#define luaL_optinteger dll_luaL_optinteger
62#define luaL_checktype dll_luaL_checktype
63#define luaL_error dll_luaL_error
64#define luaL_loadfile dll_luaL_loadfile
65#define luaL_loadbuffer dll_luaL_loadbuffer
66#define luaL_newstate dll_luaL_newstate
67#define luaL_buffinit dll_luaL_buffinit
68#define luaL_prepbuffer dll_luaL_prepbuffer
69#define luaL_addlstring dll_luaL_addlstring
70#define luaL_pushresult dll_luaL_pushresult
71/* lua */
72#define lua_close dll_lua_close
73#define lua_gettop dll_lua_gettop
74#define lua_settop dll_lua_settop
75#define lua_pushvalue dll_lua_pushvalue
76#define lua_replace dll_lua_replace
77#define lua_isnumber dll_lua_isnumber
78#define lua_isstring dll_lua_isstring
79#define lua_type dll_lua_type
80#define lua_rawequal dll_lua_rawequal
81#define lua_tonumber dll_lua_tonumber
82#define lua_tointeger dll_lua_tointeger
83#define lua_toboolean dll_lua_toboolean
84#define lua_tolstring dll_lua_tolstring
85#define lua_touserdata dll_lua_touserdata
86#define lua_pushnil dll_lua_pushnil
87#define lua_pushnumber dll_lua_pushnumber
88#define lua_pushinteger dll_lua_pushinteger
89#define lua_pushlstring dll_lua_pushlstring
90#define lua_pushstring dll_lua_pushstring
91#define lua_pushfstring dll_lua_pushfstring
92#define lua_pushcclosure dll_lua_pushcclosure
93#define lua_pushboolean dll_lua_pushboolean
94#define lua_pushlightuserdata dll_lua_pushlightuserdata
95#define lua_getfield dll_lua_getfield
96#define lua_rawget dll_lua_rawget
97#define lua_createtable dll_lua_createtable
98#define lua_newuserdata dll_lua_newuserdata
99#define lua_getmetatable dll_lua_getmetatable
100#define lua_setfield dll_lua_setfield
101#define lua_rawset dll_lua_rawset
102#define lua_rawseti dll_lua_rawseti
Bram Moolenaar38e2b062011-09-21 17:15:39 +0200103#define lua_remove dll_lua_remove
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200104#define lua_setmetatable dll_lua_setmetatable
105#define lua_call dll_lua_call
106#define lua_pcall dll_lua_pcall
107/* libs */
108#define luaopen_base dll_luaopen_base
109#define luaopen_table dll_luaopen_table
110#define luaopen_string dll_luaopen_string
111#define luaopen_math dll_luaopen_math
Bram Moolenaar16c98f92010-07-28 22:46:08 +0200112#define luaopen_io dll_luaopen_io
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200113#define luaopen_os dll_luaopen_os
114#define luaopen_package dll_luaopen_package
115#define luaopen_debug dll_luaopen_debug
Bram Moolenaar16c98f92010-07-28 22:46:08 +0200116#define luaL_openlibs dll_luaL_openlibs
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200117
118/* lauxlib */
119void (*dll_luaL_register) (lua_State *L, const char *libname, const luaL_Reg *l);
120int (*dll_luaL_typerror) (lua_State *L, int narg, const char *tname);
121const char *(*dll_luaL_checklstring) (lua_State *L, int numArg, size_t *l);
122lua_Integer (*dll_luaL_checkinteger) (lua_State *L, int numArg);
123lua_Integer (*dll_luaL_optinteger) (lua_State *L, int nArg, lua_Integer def);
124void (*dll_luaL_checktype) (lua_State *L, int narg, int t);
125int (*dll_luaL_error) (lua_State *L, const char *fmt, ...);
126int (*dll_luaL_loadfile) (lua_State *L, const char *filename);
127int (*dll_luaL_loadbuffer) (lua_State *L, const char *buff, size_t sz, const char *name);
128lua_State *(*dll_luaL_newstate) (void);
129void (*dll_luaL_buffinit) (lua_State *L, luaL_Buffer *B);
130char *(*dll_luaL_prepbuffer) (luaL_Buffer *B);
131void (*dll_luaL_addlstring) (luaL_Buffer *B, const char *s, size_t l);
132void (*dll_luaL_pushresult) (luaL_Buffer *B);
133/* lua */
134void (*dll_lua_close) (lua_State *L);
135int (*dll_lua_gettop) (lua_State *L);
136void (*dll_lua_settop) (lua_State *L, int idx);
137void (*dll_lua_pushvalue) (lua_State *L, int idx);
138void (*dll_lua_replace) (lua_State *L, int idx);
139int (*dll_lua_isnumber) (lua_State *L, int idx);
140int (*dll_lua_isstring) (lua_State *L, int idx);
141int (*dll_lua_type) (lua_State *L, int idx);
142int (*dll_lua_rawequal) (lua_State *L, int idx1, int idx2);
143lua_Number (*dll_lua_tonumber) (lua_State *L, int idx);
144lua_Integer (*dll_lua_tointeger) (lua_State *L, int idx);
145int (*dll_lua_toboolean) (lua_State *L, int idx);
146const char *(*dll_lua_tolstring) (lua_State *L, int idx, size_t *len);
147void *(*dll_lua_touserdata) (lua_State *L, int idx);
148void (*dll_lua_pushnil) (lua_State *L);
149void (*dll_lua_pushnumber) (lua_State *L, lua_Number n);
150void (*dll_lua_pushinteger) (lua_State *L, lua_Integer n);
151void (*dll_lua_pushlstring) (lua_State *L, const char *s, size_t l);
152void (*dll_lua_pushstring) (lua_State *L, const char *s);
153const char *(*dll_lua_pushfstring) (lua_State *L, const char *fmt, ...);
154void (*dll_lua_pushcclosure) (lua_State *L, lua_CFunction fn, int n);
155void (*dll_lua_pushboolean) (lua_State *L, int b);
156void (*dll_lua_pushlightuserdata) (lua_State *L, void *p);
157void (*dll_lua_getfield) (lua_State *L, int idx, const char *k);
158void (*dll_lua_rawget) (lua_State *L, int idx);
159void (*dll_lua_createtable) (lua_State *L, int narr, int nrec);
160void *(*dll_lua_newuserdata) (lua_State *L, size_t sz);
161int (*dll_lua_getmetatable) (lua_State *L, int objindex);
162void (*dll_lua_setfield) (lua_State *L, int idx, const char *k);
163void (*dll_lua_rawset) (lua_State *L, int idx);
164void (*dll_lua_rawseti) (lua_State *L, int idx, int n);
Bram Moolenaar38e2b062011-09-21 17:15:39 +0200165void (*dll_lua_remove) (lua_State *L, int idx);
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200166int (*dll_lua_setmetatable) (lua_State *L, int objindex);
167void (*dll_lua_call) (lua_State *L, int nargs, int nresults);
168int (*dll_lua_pcall) (lua_State *L, int nargs, int nresults, int errfunc);
169/* libs */
170int (*dll_luaopen_base) (lua_State *L);
171int (*dll_luaopen_table) (lua_State *L);
172int (*dll_luaopen_string) (lua_State *L);
173int (*dll_luaopen_math) (lua_State *L);
Bram Moolenaar16c98f92010-07-28 22:46:08 +0200174int (*dll_luaopen_io) (lua_State *L);
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200175int (*dll_luaopen_os) (lua_State *L);
176int (*dll_luaopen_package) (lua_State *L);
177int (*dll_luaopen_debug) (lua_State *L);
Bram Moolenaar16c98f92010-07-28 22:46:08 +0200178void (*dll_luaL_openlibs) (lua_State *L);
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200179
180typedef void **luaV_function;
181typedef struct {
182 const char *name;
183 luaV_function func;
184} luaV_Reg;
185
186static const luaV_Reg luaV_dll[] = {
187 /* lauxlib */
188 {"luaL_register", (luaV_function) &dll_luaL_register},
189 {"luaL_typerror", (luaV_function) &dll_luaL_typerror},
190 {"luaL_checklstring", (luaV_function) &dll_luaL_checklstring},
191 {"luaL_checkinteger", (luaV_function) &dll_luaL_checkinteger},
192 {"luaL_optinteger", (luaV_function) &dll_luaL_optinteger},
193 {"luaL_checktype", (luaV_function) &dll_luaL_checktype},
194 {"luaL_error", (luaV_function) &dll_luaL_error},
195 {"luaL_loadfile", (luaV_function) &dll_luaL_loadfile},
196 {"luaL_loadbuffer", (luaV_function) &dll_luaL_loadbuffer},
197 {"luaL_newstate", (luaV_function) &dll_luaL_newstate},
198 {"luaL_buffinit", (luaV_function) &dll_luaL_buffinit},
199 {"luaL_prepbuffer", (luaV_function) &dll_luaL_prepbuffer},
200 {"luaL_addlstring", (luaV_function) &dll_luaL_addlstring},
201 {"luaL_pushresult", (luaV_function) &dll_luaL_pushresult},
202 /* lua */
203 {"lua_close", (luaV_function) &dll_lua_close},
204 {"lua_gettop", (luaV_function) &dll_lua_gettop},
205 {"lua_settop", (luaV_function) &dll_lua_settop},
206 {"lua_pushvalue", (luaV_function) &dll_lua_pushvalue},
207 {"lua_replace", (luaV_function) &dll_lua_replace},
208 {"lua_isnumber", (luaV_function) &dll_lua_isnumber},
209 {"lua_isstring", (luaV_function) &dll_lua_isstring},
210 {"lua_type", (luaV_function) &dll_lua_type},
211 {"lua_rawequal", (luaV_function) &dll_lua_rawequal},
212 {"lua_tonumber", (luaV_function) &dll_lua_tonumber},
213 {"lua_tointeger", (luaV_function) &dll_lua_tointeger},
214 {"lua_toboolean", (luaV_function) &dll_lua_toboolean},
215 {"lua_tolstring", (luaV_function) &dll_lua_tolstring},
216 {"lua_touserdata", (luaV_function) &dll_lua_touserdata},
217 {"lua_pushnil", (luaV_function) &dll_lua_pushnil},
218 {"lua_pushnumber", (luaV_function) &dll_lua_pushnumber},
219 {"lua_pushinteger", (luaV_function) &dll_lua_pushinteger},
220 {"lua_pushlstring", (luaV_function) &dll_lua_pushlstring},
221 {"lua_pushstring", (luaV_function) &dll_lua_pushstring},
222 {"lua_pushfstring", (luaV_function) &dll_lua_pushfstring},
223 {"lua_pushcclosure", (luaV_function) &dll_lua_pushcclosure},
224 {"lua_pushboolean", (luaV_function) &dll_lua_pushboolean},
225 {"lua_pushlightuserdata", (luaV_function) &dll_lua_pushlightuserdata},
226 {"lua_getfield", (luaV_function) &dll_lua_getfield},
227 {"lua_rawget", (luaV_function) &dll_lua_rawget},
228 {"lua_createtable", (luaV_function) &dll_lua_createtable},
229 {"lua_newuserdata", (luaV_function) &dll_lua_newuserdata},
230 {"lua_getmetatable", (luaV_function) &dll_lua_getmetatable},
231 {"lua_setfield", (luaV_function) &dll_lua_setfield},
232 {"lua_rawset", (luaV_function) &dll_lua_rawset},
233 {"lua_rawseti", (luaV_function) &dll_lua_rawseti},
Bram Moolenaar38e2b062011-09-21 17:15:39 +0200234 {"lua_remove", (luaV_function) &dll_lua_remove},
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200235 {"lua_setmetatable", (luaV_function) &dll_lua_setmetatable},
236 {"lua_call", (luaV_function) &dll_lua_call},
237 {"lua_pcall", (luaV_function) &dll_lua_pcall},
238 /* libs */
239 {"luaopen_base", (luaV_function) &dll_luaopen_base},
240 {"luaopen_table", (luaV_function) &dll_luaopen_table},
241 {"luaopen_string", (luaV_function) &dll_luaopen_string},
242 {"luaopen_math", (luaV_function) &dll_luaopen_math},
Bram Moolenaar16c98f92010-07-28 22:46:08 +0200243 {"luaopen_io", (luaV_function) &dll_luaopen_io},
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200244 {"luaopen_os", (luaV_function) &dll_luaopen_os},
245 {"luaopen_package", (luaV_function) &dll_luaopen_package},
246 {"luaopen_debug", (luaV_function) &dll_luaopen_debug},
Bram Moolenaar16c98f92010-07-28 22:46:08 +0200247 {"luaL_openlibs", (luaV_function) &dll_luaL_openlibs},
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200248 {NULL, NULL}
249};
250
Bram Moolenaar2334b6d2010-07-22 21:32:16 +0200251static HANDLE hinstLua = NULL;
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200252
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200253 static void
254end_dynamic_lua(void)
255{
256 if (hinstLua)
257 {
Bram Moolenaar2334b6d2010-07-22 21:32:16 +0200258 close_dll(hinstLua);
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200259 hinstLua = 0;
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200260 }
261}
262
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200263 static int
264lua_link_init(char *libname, int verbose)
265{
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200266 const luaV_Reg *reg;
267 if (hinstLua) return OK;
Bram Moolenaar2334b6d2010-07-22 21:32:16 +0200268 hinstLua = load_dll(libname);
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200269 if (!hinstLua)
270 {
271 if (verbose)
272 EMSG2(_(e_loadlib), libname);
273 return FAIL;
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200274 }
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200275 for (reg = luaV_dll; reg->func; reg++)
276 {
Bram Moolenaar2334b6d2010-07-22 21:32:16 +0200277 if ((*reg->func = symbol_from_dll(hinstLua, reg->name)) == NULL)
278 {
279 close_dll(hinstLua);
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200280 hinstLua = 0;
281 if (verbose)
282 EMSG2(_(e_loadfunc), reg->name);
283 return FAIL;
284 }
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200285 }
286 return OK;
287}
288
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200289 int
290lua_enabled(int verbose)
291{
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200292 return lua_link_init(DYNAMIC_LUA_DLL, verbose) == OK;
293}
294
295#endif /* DYNAMIC_LUA */
296
297
298/* ======= Internal ======= */
299
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200300 static void
301luaV_newmetatable(lua_State *L, const char *tname)
302{
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200303 lua_newtable(L);
304 lua_pushlightuserdata(L, (void *) tname);
305 lua_pushvalue(L, -2);
306 lua_rawset(L, LUA_REGISTRYINDEX);
307}
308
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200309 static void *
310luaV_toudata(lua_State *L, int ud, const char *tname)
311{
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200312 void *p = lua_touserdata(L, ud);
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200313
314 if (p != NULL) /* value is userdata? */
315 {
316 if (lua_getmetatable(L, ud)) /* does it have a metatable? */
317 {
318 luaV_getfield(L, tname); /* get metatable */
319 if (lua_rawequal(L, -1, -2)) /* MTs match? */
320 {
321 lua_pop(L, 2); /* MTs */
322 return p;
323 }
324 }
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200325 }
326 return NULL;
327}
328
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200329 static void *
330luaV_checkudata(lua_State *L, int ud, const char *tname)
331{
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200332 void *p = luaV_toudata(L, ud, tname);
333 if (p == NULL) luaL_typerror(L, ud, tname);
334 return p;
335}
336
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200337 static void
338luaV_pushtypval(lua_State *L, typval_T *tv)
339{
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200340 if (tv == NULL) luaL_error(L, "null type");
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200341 switch (tv->v_type)
342 {
343 case VAR_STRING:
344 lua_pushstring(L, (char *) tv->vval.v_string);
345 break;
346 case VAR_NUMBER:
347 lua_pushinteger(L, (int) tv->vval.v_number);
348 break;
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200349#ifdef FEAT_FLOAT
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200350 case VAR_FLOAT:
351 lua_pushnumber(L, (lua_Number) tv->vval.v_float);
352 break;
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200353#endif
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200354 case VAR_LIST: {
355 list_T *l = tv->vval.v_list;
356
357 if (l != NULL)
358 {
359 /* check cache */
360 lua_pushlightuserdata(L, (void *) l);
361 lua_rawget(L, LUA_ENVIRONINDEX);
362 if (lua_isnil(L, -1)) /* not interned? */
363 {
364 listitem_T *li;
365 int n = 0;
366 lua_pop(L, 1); /* nil */
367 lua_newtable(L);
368 lua_pushlightuserdata(L, (void *) l);
369 lua_pushvalue(L, -2);
370 lua_rawset(L, LUA_ENVIRONINDEX);
371 for (li = l->lv_first; li != NULL; li = li->li_next)
372 {
373 luaV_pushtypval(L, &li->li_tv);
374 lua_rawseti(L, -2, ++n);
375 }
376 }
377 }
378 else lua_pushnil(L);
379 break;
380 }
381 case VAR_DICT: {
382 dict_T *d = tv->vval.v_dict;
383
384 if (d != NULL)
385 {
386 /* check cache */
387 lua_pushlightuserdata(L, (void *) d);
388 lua_rawget(L, LUA_ENVIRONINDEX);
Bram Moolenaar2334b6d2010-07-22 21:32:16 +0200389 if (lua_isnil(L, -1)) /* not interned? */
390 {
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200391 hashtab_T *ht = &d->dv_hashtab;
392 hashitem_T *hi;
393 int n = ht->ht_used; /* remaining items */
394 lua_pop(L, 1); /* nil */
395 lua_newtable(L);
396 lua_pushlightuserdata(L, (void *) d);
397 lua_pushvalue(L, -2);
398 lua_rawset(L, LUA_ENVIRONINDEX);
399 for (hi = ht->ht_array; n > 0; hi++)
400 {
401 if (!HASHITEM_EMPTY(hi))
402 {
403 dictitem_T *di = dict_lookup(hi);
404 luaV_pushtypval(L, &di->di_tv);
405 lua_setfield(L, -2, (char *) hi->hi_key);
406 n--;
407 }
408 }
409 }
410 }
411 else lua_pushnil(L);
412 break;
413 }
414 default:
415 luaL_error(L, "invalid type");
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200416 }
417}
418
419/* similar to luaL_addlstring, but replaces \0 with \n if toline and
420 * \n with \0 otherwise */
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200421 static void
422luaV_addlstring(luaL_Buffer *b, const char *s, size_t l, int toline)
423{
424 while (l--)
425 {
426 if (*s == '\0' && toline)
427 luaL_addchar(b, '\n');
428 else if (*s == '\n' && !toline)
429 luaL_addchar(b, '\0');
430 else
431 luaL_addchar(b, *s);
432 s++;
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200433 }
434}
435
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200436 static void
437luaV_pushline(lua_State *L, buf_T *buf, linenr_T n)
438{
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200439 const char *s = (const char *) ml_get_buf(buf, n, FALSE);
440 luaL_Buffer b;
441 luaL_buffinit(L, &b);
442 luaV_addlstring(&b, s, strlen(s), 0);
443 luaL_pushresult(&b);
444}
445
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200446 static char_u *
447luaV_toline(lua_State *L, int pos)
448{
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200449 size_t l;
450 const char *s = lua_tolstring(L, pos, &l);
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200451
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200452 luaL_Buffer b;
453 luaL_buffinit(L, &b);
454 luaV_addlstring(&b, s, l, 1);
455 luaL_pushresult(&b);
456 return (char_u *) lua_tostring(L, -1);
457}
458
459/* pops a string s from the top of the stack and calls mf(t) for pieces t of
460 * s separated by newlines */
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200461 static void
462luaV_msgfunc(lua_State *L, msgfunc_T mf)
463{
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200464 luaL_Buffer b;
465 size_t l;
466 const char *p, *s = lua_tolstring(L, -1, &l);
467 luaL_buffinit(L, &b);
468 luaV_addlstring(&b, s, l, 0);
469 luaL_pushresult(&b);
470 /* break string */
471 p = s = lua_tolstring(L, -1, &l);
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200472 while (l--)
473 {
474 if (*p++ == '\0') /* break? */
475 {
476 mf((char_u *) s);
477 s = p;
478 }
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200479 }
480 mf((char_u *) s);
481 lua_pop(L, 2); /* original and modified strings */
482}
483
484
485/* ======= Buffer type ======= */
486
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200487 static luaV_Buffer *
488luaV_newbuffer(lua_State *L, buf_T *buf)
489{
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200490 luaV_Buffer *b = (luaV_Buffer *) lua_newuserdata(L, sizeof(luaV_Buffer));
491 *b = buf;
492 lua_pushlightuserdata(L, (void *) buf);
493 lua_pushvalue(L, -2);
494 lua_rawset(L, LUA_ENVIRONINDEX); /* env[buf] = udata */
495 /* to avoid GC, store as key in env */
496 lua_pushvalue(L, -1);
497 lua_pushboolean(L, 1);
498 lua_rawset(L, LUA_ENVIRONINDEX); /* env[udata] = true */
499 /* set metatable */
500 luaV_getfield(L, LUAVIM_BUFFER);
501 lua_setmetatable(L, -2);
502 return b;
503}
504
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200505 static luaV_Buffer *
506luaV_pushbuffer (lua_State *L, buf_T *buf)
507{
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200508 luaV_Buffer *b = NULL;
509 if (buf == NULL)
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200510 lua_pushnil(L);
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200511 else {
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200512 lua_pushlightuserdata(L, (void *) buf);
513 lua_rawget(L, LUA_ENVIRONINDEX);
514 if (lua_isnil(L, -1)) /* not interned? */
515 {
516 lua_pop(L, 1);
517 b = luaV_newbuffer(L, buf);
518 }
519 else
520 b = (luaV_Buffer *) lua_touserdata(L, -1);
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200521 }
522 return b;
523}
524
525/* Buffer metamethods */
526
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200527 static int
528luaV_buffer_tostring(lua_State *L)
529{
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200530 lua_pushfstring(L, "%s: %p", LUAVIM_BUFFER, lua_touserdata(L, 1));
531 return 1;
532}
533
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200534 static int
535luaV_buffer_len(lua_State *L)
536{
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200537 luaV_Buffer *b = lua_touserdata(L, 1);
538 lua_pushinteger(L, (*b)->b_ml.ml_line_count);
539 return 1;
540}
541
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200542 static int
543luaV_buffer_call(lua_State *L)
544{
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200545 luaV_Buffer *b = (luaV_Buffer *) lua_touserdata(L, 1);
546 lua_settop(L, 1);
547 set_curbuf(*b, DOBUF_SPLIT);
548 return 1;
549}
550
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200551 static int
552luaV_buffer_index(lua_State *L)
553{
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200554 luaV_Buffer *b = (luaV_Buffer *) lua_touserdata(L, 1);
555 linenr_T n = (linenr_T) lua_tointeger(L, 2);
556 if (n > 0 && n <= (*b)->b_ml.ml_line_count)
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200557 luaV_pushline(L, *b, n);
558 else if (lua_isstring(L, 2))
559 {
560 const char *s = lua_tostring(L, 2);
561 if (strncmp(s, "name", 4) == 0)
562 lua_pushstring(L, (char *) (*b)->b_sfname);
563 else if (strncmp(s, "fname", 5) == 0)
564 lua_pushstring(L, (char *) (*b)->b_ffname);
565 else if (strncmp(s, "number", 6) == 0)
566 lua_pushinteger(L, (*b)->b_fnum);
567 /* methods */
568 else if (strncmp(s, "insert", 6) == 0
569 || strncmp(s, "next", 4) == 0
570 || strncmp(s, "previous", 8) == 0
571 || strncmp(s, "isvalid", 7) == 0)
572 {
573 lua_getmetatable(L, 1);
574 lua_getfield(L, -1, s);
575 }
576 else
577 lua_pushnil(L);
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200578 }
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200579 else
580 lua_pushnil(L);
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200581 return 1;
582}
583
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200584 static int
585luaV_buffer_newindex(lua_State *L)
586{
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200587 luaV_Buffer *b = (luaV_Buffer *) lua_touserdata(L, 1);
588 linenr_T n = (linenr_T) luaL_checkinteger(L, 2);
589#ifdef HAVE_SANDBOX
590 luaV_checksandbox(L);
591#endif
592 if (n < 1 || n > (*b)->b_ml.ml_line_count)
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200593 luaL_error(L, "invalid line number");
594 if (lua_isnil(L, 3)) /* delete line */
595 {
596 buf_T *buf = curbuf;
597 curbuf = *b;
598 if (u_savedel(n, 1L) == FAIL)
599 {
600 curbuf = buf;
601 luaL_error(L, "cannot save undo information");
602 }
603 else if (ml_delete(n, FALSE) == FAIL)
604 {
605 curbuf = buf;
606 luaL_error(L, "cannot delete line");
607 }
608 else {
609 deleted_lines_mark(n, 1L);
610 if (*b == curwin->w_buffer) /* fix cursor in current window? */
611 {
612 if (curwin->w_cursor.lnum >= n)
613 {
614 if (curwin->w_cursor.lnum > n)
615 {
616 curwin->w_cursor.lnum -= 1;
617 check_cursor_col();
618 }
619 else check_cursor();
620 changed_cline_bef_curs();
621 }
622 invalidate_botline();
623 }
624 }
625 curbuf = buf;
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200626 }
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200627 else if (lua_isstring(L, 3)) /* update line */
628 {
629 buf_T *buf = curbuf;
630 curbuf = *b;
631 if (u_savesub(n) == FAIL)
632 {
633 curbuf = buf;
634 luaL_error(L, "cannot save undo information");
635 }
636 else if (ml_replace(n, luaV_toline(L, 3), TRUE) == FAIL)
637 {
638 curbuf = buf;
639 luaL_error(L, "cannot replace line");
640 }
641 else changed_bytes(n, 0);
642 curbuf = buf;
643 if (*b == curwin->w_buffer)
644 check_cursor_col();
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200645 }
646 else
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200647 luaL_error(L, "wrong argument to change line");
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200648 return 0;
649}
650
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200651 static int
652luaV_buffer_insert(lua_State *L)
653{
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200654 luaV_Buffer *b = luaV_checkudata(L, 1, LUAVIM_BUFFER);
655 linenr_T last = (*b)->b_ml.ml_line_count;
656 linenr_T n = (linenr_T) luaL_optinteger(L, 3, last);
657 buf_T *buf;
658 luaL_checktype(L, 2, LUA_TSTRING);
659#ifdef HAVE_SANDBOX
660 luaV_checksandbox(L);
661#endif
662 /* fix insertion line */
663 if (n < 0) n = 0;
664 if (n > last) n = last;
665 /* insert */
666 buf = curbuf;
667 curbuf = *b;
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200668 if (u_save(n, n + 1) == FAIL)
669 {
670 curbuf = buf;
671 luaL_error(L, "cannot save undo information");
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200672 }
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200673 else if (ml_append(n, luaV_toline(L, 2), 0, FALSE) == FAIL)
674 {
675 curbuf = buf;
676 luaL_error(L, "cannot insert line");
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200677 }
678 else
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200679 appended_lines_mark(n, 1L);
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200680 curbuf = buf;
681 update_screen(VALID);
682 return 0;
683}
684
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200685 static int
686luaV_buffer_next(lua_State *L)
687{
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200688 luaV_Buffer *b = luaV_checkudata(L, 1, LUAVIM_BUFFER);
689 luaV_pushbuffer(L, (*b)->b_next);
690 return 1;
691}
692
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200693 static int
694luaV_buffer_previous(lua_State *L)
695{
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200696 luaV_Buffer *b = luaV_checkudata(L, 1, LUAVIM_BUFFER);
697 luaV_pushbuffer(L, (*b)->b_prev);
698 return 1;
699}
700
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200701 static int
702luaV_buffer_isvalid(lua_State *L)
703{
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200704 luaV_Buffer *b = luaV_checkudata(L, 1, LUAVIM_BUFFER);
705 lua_pushlightuserdata(L, (void *) (*b));
706 lua_rawget(L, LUA_ENVIRONINDEX);
707 lua_pushboolean(L, !lua_isnil(L, -1));
708 return 1;
709}
710
711static const luaL_Reg luaV_Buffer_mt[] = {
712 {"__tostring", luaV_buffer_tostring},
713 {"__len", luaV_buffer_len},
714 {"__call", luaV_buffer_call},
715 {"__index", luaV_buffer_index},
716 {"__newindex", luaV_buffer_newindex},
717 {"insert", luaV_buffer_insert},
718 {"next", luaV_buffer_next},
719 {"previous", luaV_buffer_previous},
720 {"isvalid", luaV_buffer_isvalid},
721 {NULL, NULL}
722};
723
724
725/* ======= Window type ======= */
726
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200727 static luaV_Window *
728luaV_newwindow(lua_State *L, win_T *win)
729{
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200730 luaV_Window *w = (luaV_Window *) lua_newuserdata(L, sizeof(luaV_Window));
731 *w = win;
732 lua_pushlightuserdata(L, (void *) win);
733 lua_pushvalue(L, -2);
734 lua_rawset(L, LUA_ENVIRONINDEX); /* env[win] = udata */
735 /* to avoid GC, store as key in env */
736 lua_pushvalue(L, -1);
737 lua_pushboolean(L, 1);
738 lua_rawset(L, LUA_ENVIRONINDEX); /* env[udata] = true */
739 /* set metatable */
740 luaV_getfield(L, LUAVIM_WINDOW);
741 lua_setmetatable(L, -2);
742 return w;
743}
744
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200745 static luaV_Window *
746luaV_pushwindow(lua_State *L, win_T *win)
747{
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200748 luaV_Window *w = NULL;
749 if (win == NULL)
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200750 lua_pushnil(L);
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200751 else {
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200752 lua_pushlightuserdata(L, (void *) win);
753 lua_rawget(L, LUA_ENVIRONINDEX);
754 if (lua_isnil(L, -1)) /* not interned? */
755 {
756 lua_pop(L, 1);
757 w = luaV_newwindow(L, win);
758 }
759 else w = (luaV_Window *) lua_touserdata(L, -1);
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200760 }
761 return w;
762}
763
764/* Window metamethods */
765
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200766 static int
767luaV_window_tostring(lua_State *L)
768{
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200769 lua_pushfstring(L, "%s: %p", LUAVIM_WINDOW, lua_touserdata(L, 1));
770 return 1;
771}
772
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200773 static int
774luaV_window_call(lua_State *L)
775{
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200776 luaV_Window *w = (luaV_Window *) lua_touserdata(L, 1);
777 lua_settop(L, 1);
778 win_goto(*w);
779 return 1;
780}
781
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200782 static int
783luaV_window_index(lua_State *L)
784{
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200785 luaV_Window *w = (luaV_Window *) lua_touserdata(L, 1);
786 const char *s = luaL_checkstring(L, 2);
787 if (strncmp(s, "buffer", 6) == 0)
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200788 luaV_pushbuffer(L, (*w)->w_buffer);
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200789 else if (strncmp(s, "line", 4) == 0)
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200790 lua_pushinteger(L, (*w)->w_cursor.lnum);
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200791 else if (strncmp(s, "col", 3) == 0)
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200792 lua_pushinteger(L, (*w)->w_cursor.col + 1);
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200793#ifdef FEAT_VERTSPLIT
794 else if (strncmp(s, "width", 5) == 0)
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200795 lua_pushinteger(L, W_WIDTH((*w)));
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200796#endif
797 else if (strncmp(s, "height", 6) == 0)
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200798 lua_pushinteger(L, (*w)->w_height);
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200799 /* methods */
800 else if (strncmp(s, "next", 4) == 0
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200801 || strncmp(s, "previous", 8) == 0
802 || strncmp(s, "isvalid", 7) == 0)
803 {
804 lua_getmetatable(L, 1);
805 lua_getfield(L, -1, s);
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200806 }
807 else
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200808 lua_pushnil(L);
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200809 return 1;
810}
811
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200812 static int
813luaV_window_newindex (lua_State *L)
814{
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200815 luaV_Window *w = (luaV_Window *) lua_touserdata(L, 1);
816 const char *s = luaL_checkstring(L, 2);
817 int v = luaL_checkinteger(L, 3);
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200818 if (strncmp(s, "line", 4) == 0)
819 {
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200820#ifdef HAVE_SANDBOX
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200821 luaV_checksandbox(L);
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200822#endif
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200823 if (v < 1 || v > (*w)->w_buffer->b_ml.ml_line_count)
824 luaL_error(L, "line out of range");
825 (*w)->w_cursor.lnum = v;
826 update_screen(VALID);
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200827 }
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200828 else if (strncmp(s, "col", 3) == 0)
829 {
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200830#ifdef HAVE_SANDBOX
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200831 luaV_checksandbox(L);
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200832#endif
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200833 (*w)->w_cursor.col = v - 1;
834 update_screen(VALID);
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200835 }
836#ifdef FEAT_VERTSPLIT
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200837 else if (strncmp(s, "width", 5) == 0)
838 {
839 win_T *win = curwin;
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200840#ifdef FEAT_GUI
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200841 need_mouse_correct = TRUE;
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200842#endif
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200843 curwin = *w;
844 win_setwidth(v);
845 curwin = win;
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200846 }
847#endif
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200848 else if (strncmp(s, "height", 6) == 0)
849 {
850 win_T *win = curwin;
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200851#ifdef FEAT_GUI
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200852 need_mouse_correct = TRUE;
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200853#endif
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200854 curwin = *w;
855 win_setheight(v);
856 curwin = win;
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200857 }
858 else
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200859 luaL_error(L, "invalid window property: `%s'", s);
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200860 return 0;
861}
862
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200863 static int
864luaV_window_next(lua_State *L)
865{
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200866 luaV_Window *w = luaV_checkudata(L, 1, LUAVIM_WINDOW);
867 luaV_pushwindow(L, (*w)->w_next);
868 return 1;
869}
870
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200871 static int
872luaV_window_previous(lua_State *L)
873{
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200874 luaV_Window *w = luaV_checkudata(L, 1, LUAVIM_WINDOW);
875 luaV_pushwindow(L, (*w)->w_prev);
876 return 1;
877}
878
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200879 static int
880luaV_window_isvalid(lua_State *L)
881{
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200882 luaV_Window *w = luaV_checkudata(L, 1, LUAVIM_WINDOW);
883 lua_pushlightuserdata(L, (void *) (*w));
884 lua_rawget(L, LUA_ENVIRONINDEX);
885 lua_pushboolean(L, !lua_isnil(L, -1));
886 return 1;
887}
888
889static const luaL_Reg luaV_Window_mt[] = {
890 {"__tostring", luaV_window_tostring},
891 {"__call", luaV_window_call},
892 {"__index", luaV_window_index},
893 {"__newindex", luaV_window_newindex},
894 {"next", luaV_window_next},
895 {"previous", luaV_window_previous},
896 {"isvalid", luaV_window_isvalid},
897 {NULL, NULL}
898};
899
900
901/* ======= Vim module ======= */
902
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200903 static int
904luaV_print(lua_State *L)
905{
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200906 int i, n = lua_gettop(L); /* nargs */
907 const char *s;
908 size_t l;
909 luaL_Buffer b;
910 luaL_buffinit(L, &b);
911 lua_getglobal(L, "tostring");
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200912 for (i = 1; i <= n; i++)
913 {
914 lua_pushvalue(L, -1); /* tostring */
915 lua_pushvalue(L, i); /* arg */
916 lua_call(L, 1, 1);
917 s = lua_tolstring(L, -1, &l);
918 if (s == NULL)
919 return luaL_error(L, "cannot convert to string");
920 if (i > 1) luaL_addchar(&b, ' '); /* use space instead of tab */
921 luaV_addlstring(&b, s, l, 0);
922 lua_pop(L, 1);
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200923 }
924 luaL_pushresult(&b);
925 luaV_msg(L);
926 return 0;
927}
928
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200929 static int
Bram Moolenaar38e2b062011-09-21 17:15:39 +0200930luaV_debug(lua_State *L)
931{
932 lua_settop(L, 0);
933 lua_getglobal(L, "vim");
934 lua_getfield(L, -1, "eval");
935 lua_remove(L, -2); /* vim.eval at position 1 */
936 for (;;)
937 {
938 const char *input;
939 size_t l;
940 lua_pushvalue(L, 1); /* vim.eval */
941 lua_pushliteral(L, "input('lua_debug> ')");
942 lua_call(L, 1, 1); /* return string */
943 input = lua_tolstring(L, -1, &l);
944 if (l == 0 || strcmp(input, "cont") == 0)
945 return 0;
946 msg_putchar('\n'); /* avoid outputting on input line */
947 if (luaL_loadbuffer(L, input, l, "=(debug command)")
948 || lua_pcall(L, 0, 0, 0))
949 luaV_emsg(L);
950 lua_settop(L, 1); /* remove eventual returns, but keep vim.eval */
951 }
952}
953
954 static int
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200955luaV_command(lua_State *L)
956{
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200957 do_cmdline_cmd((char_u *) luaL_checkstring(L, 1));
958 update_screen(VALID);
959 return 0;
960}
961
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200962 static int
963luaV_eval(lua_State *L)
964{
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200965 typval_T *tv = eval_expr((char_u *) luaL_checkstring(L, 1), NULL);
966 if (tv == NULL) luaL_error(L, "invalid expression");
967 luaV_pushtypval(L, tv);
968 return 1;
969}
970
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200971 static int
Bram Moolenaar0d2e4fc2010-07-18 12:35:47 +0200972luaV_beep(lua_State *L UNUSED)
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200973{
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200974 vim_beep();
975 return 0;
976}
977
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200978 static int
979luaV_line(lua_State *L)
980{
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200981 luaV_pushline(L, curbuf, curwin->w_cursor.lnum);
982 return 1;
983}
984
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200985 static int
986luaV_buffer(lua_State *L)
987{
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200988 buf_T *buf;
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200989 if (lua_isstring(L, 1)) /* get by number or name? */
990 {
991 if (lua_isnumber(L, 1)) /* by number? */
992 {
993 int n = lua_tointeger(L, 1);
994 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
995 if (buf->b_fnum == n) break;
996 }
997 else { /* by name */
998 size_t l;
999 const char *s = lua_tolstring(L, 1, &l);
1000 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
1001 {
1002 if (buf->b_ffname == NULL || buf->b_sfname == NULL)
1003 {
1004 if (l == 0) break;
1005 }
Bram Moolenaar0d2e4fc2010-07-18 12:35:47 +02001006 else if (strncmp(s, (char *)buf->b_ffname, l) == 0
1007 || strncmp(s, (char *)buf->b_sfname, l) == 0)
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001008 break;
1009 }
1010 }
1011 if (buf == NULL) /* not found? */
1012 lua_pushnil(L);
1013 else
1014 luaV_pushbuffer(L, buf);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001015 }
1016 else {
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001017 buf = (lua_toboolean(L, 1)) ? firstbuf : curbuf; /* first buffer? */
1018 luaV_pushbuffer(L, buf);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001019 }
1020 return 1;
1021}
1022
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001023 static int
1024luaV_window(lua_State *L)
1025{
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001026 win_T *win;
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001027 if (lua_isnumber(L, 1)) /* get by number? */
1028 {
1029 int n = lua_tointeger(L, 1);
1030 for (win = firstwin; win != NULL; win = win->w_next, n--)
1031 if (n == 1) break;
1032 if (win == NULL) /* not found? */
1033 lua_pushnil(L);
1034 else
1035 luaV_pushwindow(L, win);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001036 }
1037 else {
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001038 win = (lua_toboolean(L, 1)) ? firstwin : curwin; /* first window? */
1039 luaV_pushwindow(L, win);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001040 }
1041 return 1;
1042}
1043
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001044 static int
1045luaV_open(lua_State *L)
1046{
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001047 luaV_Buffer *b;
1048 char_u *s = NULL;
1049#ifdef HAVE_SANDBOX
1050 luaV_checksandbox(L);
1051#endif
1052 if (lua_isstring(L, 1)) s = (char_u *) lua_tostring(L, 1);
1053 b = luaV_pushbuffer(L, buflist_new(s, NULL, 1L, BLN_LISTED));
1054 return 1;
1055}
1056
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001057 static int
1058luaV_isbuffer(lua_State *L)
1059{
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001060 lua_pushboolean(L, luaV_toudata(L, 1, LUAVIM_BUFFER) != NULL);
1061 return 1;
1062}
1063
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001064 static int
1065luaV_iswindow(lua_State *L)
1066{
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001067 lua_pushboolean(L, luaV_toudata(L, 1, LUAVIM_WINDOW) != NULL);
1068 return 1;
1069}
1070
1071/* for freeing buffer and window objects; lightuserdata as arg */
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001072 static int
1073luaV_free(lua_State *L)
1074{
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001075 lua_pushvalue(L, 1); /* lightudata */
1076 lua_rawget(L, LUA_ENVIRONINDEX);
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001077 if (!lua_isnil(L, -1))
1078 {
1079 lua_pushnil(L);
1080 lua_rawset(L, LUA_ENVIRONINDEX); /* env[udata] = nil */
1081 lua_pushnil(L);
1082 lua_rawset(L, LUA_ENVIRONINDEX); /* env[lightudata] = nil */
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001083 }
1084 return 0;
1085}
1086
1087static const luaL_Reg luaV_module[] = {
1088 {"command", luaV_command},
1089 {"eval", luaV_eval},
1090 {"beep", luaV_beep},
1091 {"line", luaV_line},
1092 {"buffer", luaV_buffer},
1093 {"window", luaV_window},
1094 {"open", luaV_open},
1095 {"isbuffer", luaV_isbuffer},
1096 {"iswindow", luaV_iswindow},
1097 {NULL, NULL}
1098};
1099
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001100 static int
1101luaopen_vim(lua_State *L)
1102{
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001103 /* set environment */
1104 lua_newtable(L);
1105 lua_newtable(L);
1106 lua_pushliteral(L, "v");
1107 lua_setfield(L, -2, "__mode");
1108 lua_setmetatable(L, -2);
1109 lua_replace(L, LUA_ENVIRONINDEX);
1110 /* print */
1111 lua_pushcfunction(L, luaV_print);
1112 lua_setglobal(L, "print");
Bram Moolenaar38e2b062011-09-21 17:15:39 +02001113 /* debug.debug */
1114 lua_getglobal(L, "debug");
1115 lua_pushcfunction(L, luaV_debug);
1116 lua_setfield(L, -2, "debug");
1117 lua_pop(L, 1);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001118 /* free */
1119 lua_pushlightuserdata(L, (void *) LUAVIM_FREE);
1120 lua_pushcfunction(L, luaV_free);
1121 lua_rawset(L, LUA_REGISTRYINDEX);
1122 /* register */
1123 luaV_newmetatable(L, LUAVIM_BUFFER);
1124 luaL_register(L, NULL, luaV_Buffer_mt);
1125 luaV_newmetatable(L, LUAVIM_WINDOW);
1126 luaL_register(L, NULL, luaV_Window_mt);
1127 luaL_register(L, LUAVIM_NAME, luaV_module);
1128 return 0;
1129}
1130
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001131 static lua_State *
1132luaV_newstate(void)
1133{
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001134 lua_State *L = luaL_newstate();
Bram Moolenaar16c98f92010-07-28 22:46:08 +02001135 luaL_openlibs(L); /* core libs */
1136 lua_pushcfunction(L, luaopen_vim); /* vim */
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001137 lua_call(L, 0, 0);
1138 return L;
1139}
1140
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001141 static void
1142luaV_setrange(lua_State *L, int line1, int line2)
1143{
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001144 lua_getglobal(L, LUAVIM_NAME);
1145 lua_pushinteger(L, line1);
1146 lua_setfield(L, -2, "firstline");
1147 lua_pushinteger(L, line2);
1148 lua_setfield(L, -2, "lastline");
1149 lua_pop(L, 1); /* vim table */
1150}
1151
1152
1153/* ======= Interface ======= */
1154
1155static lua_State *L = NULL;
1156
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001157 static int
Bram Moolenaar2bd6a1b2010-08-12 22:14:01 +02001158lua_is_open(void)
1159{
1160 return L != NULL;
1161}
1162
1163 static int
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001164lua_init(void)
1165{
1166 if (L == NULL)
1167 {
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001168#ifdef DYNAMIC_LUA
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001169 if (!lua_enabled(TRUE))
1170 {
1171 EMSG(_("Lua library cannot be loaded."));
1172 return FAIL;
1173 }
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001174#endif
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001175 L = luaV_newstate();
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001176 }
1177 return OK;
1178}
1179
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001180 void
1181lua_end(void)
1182{
1183 if (L != NULL)
1184 {
1185 lua_close(L);
1186 L = NULL;
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001187#ifdef DYNAMIC_LUA
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001188 end_dynamic_lua();
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001189#endif
1190 }
1191}
1192
1193/* ex commands */
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001194 void
1195ex_lua(exarg_T *eap)
1196{
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001197 char *script;
1198 if (lua_init() == FAIL) return;
1199 script = (char *) script_get(eap, eap->arg);
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001200 if (!eap->skip)
1201 {
1202 char *s = (script) ? script : (char *) eap->arg;
1203 luaV_setrange(L, eap->line1, eap->line2);
1204 if (luaL_loadbuffer(L, s, strlen(s), LUAVIM_CHUNKNAME)
1205 || lua_pcall(L, 0, 0, 0))
1206 luaV_emsg(L);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001207 }
1208 if (script != NULL) vim_free(script);
1209}
1210
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001211 void
1212ex_luado(exarg_T *eap)
1213{
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001214 linenr_T l;
1215 const char *s = (const char *) eap->arg;
1216 luaL_Buffer b;
1217 size_t len;
1218 if (lua_init() == FAIL) return;
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001219 if (u_save(eap->line1 - 1, eap->line2 + 1) == FAIL)
1220 {
1221 EMSG(_("cannot save undo information"));
1222 return;
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001223 }
1224 luaV_setrange(L, eap->line1, eap->line2);
1225 luaL_buffinit(L, &b);
1226 luaL_addlstring(&b, "return function(line) ", 22); /* header */
1227 luaL_addlstring(&b, s, strlen(s));
1228 luaL_addlstring(&b, " end", 4); /* footer */
1229 luaL_pushresult(&b);
1230 s = lua_tolstring(L, -1, &len);
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001231 if (luaL_loadbuffer(L, s, len, LUAVIM_CHUNKNAME))
1232 {
1233 luaV_emsg(L);
1234 lua_pop(L, 1); /* function body */
1235 return;
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001236 }
1237 lua_call(L, 0, 1);
1238 lua_replace(L, -2); /* function -> body */
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001239 for (l = eap->line1; l <= eap->line2; l++)
1240 {
1241 lua_pushvalue(L, -1); /* function */
1242 luaV_pushline(L, curbuf, l); /* current line as arg */
1243 if (lua_pcall(L, 1, 1, 0))
1244 {
1245 luaV_emsg(L);
1246 break;
1247 }
1248 if (lua_isstring(L, -1)) /* update line? */
1249 {
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001250#ifdef HAVE_SANDBOX
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001251 luaV_checksandbox(L);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001252#endif
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001253 ml_replace(l, luaV_toline(L, -1), TRUE);
1254 changed_bytes(l, 0);
1255 lua_pop(L, 1); /* result from luaV_toline */
1256 }
1257 lua_pop(L, 1); /* line */
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001258 }
1259 lua_pop(L, 1); /* function */
1260 check_cursor();
1261 update_screen(NOT_VALID);
1262}
1263
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001264 void
1265ex_luafile(exarg_T *eap)
1266{
1267 if (lua_init() == FAIL)
1268 return;
1269 if (!eap->skip)
1270 {
1271 luaV_setrange(L, eap->line1, eap->line2);
1272 if (luaL_loadfile(L, (char *) eap->arg) || lua_pcall(L, 0, 0, 0))
1273 luaV_emsg(L);
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001274 }
1275}
1276
1277/* buffer */
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001278 void
1279lua_buffer_free(buf_T *buf)
1280{
Bram Moolenaar2bd6a1b2010-08-12 22:14:01 +02001281 if (!lua_is_open()) return;
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001282 luaV_getfield(L, LUAVIM_FREE);
1283 lua_pushlightuserdata(L, (void *) buf);
1284 lua_call(L, 1, 0);
1285}
1286
1287/* window */
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001288 void
1289lua_window_free(win_T *win)
1290{
Bram Moolenaar2bd6a1b2010-08-12 22:14:01 +02001291 if (!lua_is_open()) return;
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001292 luaV_getfield(L, LUAVIM_FREE);
1293 lua_pushlightuserdata(L, (void *) win);
1294 lua_call(L, 1, 0);
1295}
1296
1297#endif