Mercurial > vim
annotate src/if_lua.c @ 2429:7ce8b24450dc vim73
Improvements for ":find" completion. (Nazri Ramliy)
author | Bram Moolenaar <bram@vim.org> |
---|---|
date | Wed, 28 Jul 2010 22:29:10 +0200 |
parents | f149bb1cf5be |
children | 7b764999f9b9 |
rev | line source |
---|---|
2320
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
1 /* vi:set ts=8 sts=4 sw=4: |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
2 * |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
3 * VIM - Vi IMproved by Bram Moolenaar |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
4 * |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
5 * Lua interface by Luis Carvalho |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
6 * |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
7 * Do ":help uganda" in Vim to read copying and usage conditions. |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
8 * Do ":help credits" in Vim to see a list of people who contributed. |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
9 * See README.txt for an overview of the Vim source code. |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
10 */ |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
11 |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
12 #include <stdio.h> |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
13 #include <string.h> |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
14 #include <lua.h> |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
15 #include <lualib.h> |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
16 #include <lauxlib.h> |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
17 #include "vim.h" |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
18 |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
19 /* Only do the following when the feature is enabled. Needed for "make |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
20 * depend". */ |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
21 #if defined(FEAT_LUA) || defined(PROTO) |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
22 |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
23 #define LUAVIM_CHUNKNAME "vim chunk" |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
24 #define LUAVIM_NAME "vim" |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
25 |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
26 typedef buf_T *luaV_Buffer; |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
27 typedef win_T *luaV_Window; |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
28 typedef void (*msgfunc_T)(char_u *); |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
29 |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
30 static const char LUAVIM_BUFFER[] = "buffer"; |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
31 static const char LUAVIM_WINDOW[] = "window"; |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
32 static const char LUAVIM_FREE[] = "luaV_free"; |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
33 |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
34 #define luaV_getfield(L, s) \ |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
35 lua_pushlightuserdata((L), (void *)(s)); \ |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
36 lua_rawget((L), LUA_REGISTRYINDEX) |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
37 #define luaV_checksandbox(L) \ |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
38 if (sandbox) luaL_error((L), "not allowed in sandbox") |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
39 #define luaV_msg(L) luaV_msgfunc((L), (msgfunc_T) msg) |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
40 #define luaV_emsg(L) luaV_msgfunc((L), (msgfunc_T) emsg) |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
41 |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
42 |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
43 #ifdef DYNAMIC_LUA |
2373
f149bb1cf5be
Make it possible to load Lua dynamically on Unix. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
2331
diff
changeset
|
44 |
f149bb1cf5be
Make it possible to load Lua dynamically on Unix. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
2331
diff
changeset
|
45 #ifndef WIN3264 |
f149bb1cf5be
Make it possible to load Lua dynamically on Unix. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
2331
diff
changeset
|
46 # include <dlfcn.h> |
f149bb1cf5be
Make it possible to load Lua dynamically on Unix. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
2331
diff
changeset
|
47 # define HANDLE void* |
f149bb1cf5be
Make it possible to load Lua dynamically on Unix. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
2331
diff
changeset
|
48 # define load_dll(n) dlopen((n), RTLD_LAZY|RTLD_GLOBAL) |
f149bb1cf5be
Make it possible to load Lua dynamically on Unix. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
2331
diff
changeset
|
49 # define symbol_from_dll dlsym |
f149bb1cf5be
Make it possible to load Lua dynamically on Unix. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
2331
diff
changeset
|
50 # define close_dll dlclose |
f149bb1cf5be
Make it possible to load Lua dynamically on Unix. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
2331
diff
changeset
|
51 #else |
f149bb1cf5be
Make it possible to load Lua dynamically on Unix. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
2331
diff
changeset
|
52 # define load_dll LoadLibrary |
f149bb1cf5be
Make it possible to load Lua dynamically on Unix. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
2331
diff
changeset
|
53 # define symbol_from_dll GetProcAddress |
f149bb1cf5be
Make it possible to load Lua dynamically on Unix. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
2331
diff
changeset
|
54 # define close_dll FreeLibrary |
f149bb1cf5be
Make it possible to load Lua dynamically on Unix. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
2331
diff
changeset
|
55 #endif |
f149bb1cf5be
Make it possible to load Lua dynamically on Unix. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
2331
diff
changeset
|
56 |
2320
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
57 /* lauxlib */ |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
58 #define luaL_register dll_luaL_register |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
59 #define luaL_typerror dll_luaL_typerror |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
60 #define luaL_checklstring dll_luaL_checklstring |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
61 #define luaL_checkinteger dll_luaL_checkinteger |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
62 #define luaL_optinteger dll_luaL_optinteger |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
63 #define luaL_checktype dll_luaL_checktype |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
64 #define luaL_error dll_luaL_error |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
65 #define luaL_loadfile dll_luaL_loadfile |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
66 #define luaL_loadbuffer dll_luaL_loadbuffer |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
67 #define luaL_newstate dll_luaL_newstate |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
68 #define luaL_buffinit dll_luaL_buffinit |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
69 #define luaL_prepbuffer dll_luaL_prepbuffer |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
70 #define luaL_addlstring dll_luaL_addlstring |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
71 #define luaL_pushresult dll_luaL_pushresult |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
72 /* lua */ |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
73 #define lua_close dll_lua_close |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
74 #define lua_gettop dll_lua_gettop |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
75 #define lua_settop dll_lua_settop |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
76 #define lua_pushvalue dll_lua_pushvalue |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
77 #define lua_replace dll_lua_replace |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
78 #define lua_isnumber dll_lua_isnumber |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
79 #define lua_isstring dll_lua_isstring |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
80 #define lua_type dll_lua_type |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
81 #define lua_rawequal dll_lua_rawequal |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
82 #define lua_tonumber dll_lua_tonumber |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
83 #define lua_tointeger dll_lua_tointeger |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
84 #define lua_toboolean dll_lua_toboolean |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
85 #define lua_tolstring dll_lua_tolstring |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
86 #define lua_touserdata dll_lua_touserdata |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
87 #define lua_pushnil dll_lua_pushnil |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
88 #define lua_pushnumber dll_lua_pushnumber |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
89 #define lua_pushinteger dll_lua_pushinteger |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
90 #define lua_pushlstring dll_lua_pushlstring |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
91 #define lua_pushstring dll_lua_pushstring |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
92 #define lua_pushfstring dll_lua_pushfstring |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
93 #define lua_pushcclosure dll_lua_pushcclosure |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
94 #define lua_pushboolean dll_lua_pushboolean |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
95 #define lua_pushlightuserdata dll_lua_pushlightuserdata |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
96 #define lua_getfield dll_lua_getfield |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
97 #define lua_rawget dll_lua_rawget |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
98 #define lua_createtable dll_lua_createtable |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
99 #define lua_newuserdata dll_lua_newuserdata |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
100 #define lua_getmetatable dll_lua_getmetatable |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
101 #define lua_setfield dll_lua_setfield |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
102 #define lua_rawset dll_lua_rawset |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
103 #define lua_rawseti dll_lua_rawseti |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
104 #define lua_setmetatable dll_lua_setmetatable |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
105 #define lua_call dll_lua_call |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
106 #define lua_pcall dll_lua_pcall |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
107 /* libs */ |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
108 #define luaopen_base dll_luaopen_base |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
109 #define luaopen_table dll_luaopen_table |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
110 #define luaopen_string dll_luaopen_string |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
111 #define luaopen_math dll_luaopen_math |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
112 #define luaopen_os dll_luaopen_os |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
113 #define luaopen_package dll_luaopen_package |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
114 #define luaopen_debug dll_luaopen_debug |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
115 |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
116 /* lauxlib */ |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
117 void (*dll_luaL_register) (lua_State *L, const char *libname, const luaL_Reg *l); |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
118 int (*dll_luaL_typerror) (lua_State *L, int narg, const char *tname); |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
119 const char *(*dll_luaL_checklstring) (lua_State *L, int numArg, size_t *l); |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
120 lua_Integer (*dll_luaL_checkinteger) (lua_State *L, int numArg); |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
121 lua_Integer (*dll_luaL_optinteger) (lua_State *L, int nArg, lua_Integer def); |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
122 void (*dll_luaL_checktype) (lua_State *L, int narg, int t); |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
123 int (*dll_luaL_error) (lua_State *L, const char *fmt, ...); |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
124 int (*dll_luaL_loadfile) (lua_State *L, const char *filename); |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
125 int (*dll_luaL_loadbuffer) (lua_State *L, const char *buff, size_t sz, const char *name); |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
126 lua_State *(*dll_luaL_newstate) (void); |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
127 void (*dll_luaL_buffinit) (lua_State *L, luaL_Buffer *B); |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
128 char *(*dll_luaL_prepbuffer) (luaL_Buffer *B); |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
129 void (*dll_luaL_addlstring) (luaL_Buffer *B, const char *s, size_t l); |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
130 void (*dll_luaL_pushresult) (luaL_Buffer *B); |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
131 /* lua */ |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
132 void (*dll_lua_close) (lua_State *L); |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
133 int (*dll_lua_gettop) (lua_State *L); |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
134 void (*dll_lua_settop) (lua_State *L, int idx); |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
135 void (*dll_lua_pushvalue) (lua_State *L, int idx); |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
136 void (*dll_lua_replace) (lua_State *L, int idx); |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
137 int (*dll_lua_isnumber) (lua_State *L, int idx); |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
138 int (*dll_lua_isstring) (lua_State *L, int idx); |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
139 int (*dll_lua_type) (lua_State *L, int idx); |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
140 int (*dll_lua_rawequal) (lua_State *L, int idx1, int idx2); |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
141 lua_Number (*dll_lua_tonumber) (lua_State *L, int idx); |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
142 lua_Integer (*dll_lua_tointeger) (lua_State *L, int idx); |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
143 int (*dll_lua_toboolean) (lua_State *L, int idx); |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
144 const char *(*dll_lua_tolstring) (lua_State *L, int idx, size_t *len); |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
145 void *(*dll_lua_touserdata) (lua_State *L, int idx); |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
146 void (*dll_lua_pushnil) (lua_State *L); |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
147 void (*dll_lua_pushnumber) (lua_State *L, lua_Number n); |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
148 void (*dll_lua_pushinteger) (lua_State *L, lua_Integer n); |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
149 void (*dll_lua_pushlstring) (lua_State *L, const char *s, size_t l); |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
150 void (*dll_lua_pushstring) (lua_State *L, const char *s); |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
151 const char *(*dll_lua_pushfstring) (lua_State *L, const char *fmt, ...); |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
152 void (*dll_lua_pushcclosure) (lua_State *L, lua_CFunction fn, int n); |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
153 void (*dll_lua_pushboolean) (lua_State *L, int b); |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
154 void (*dll_lua_pushlightuserdata) (lua_State *L, void *p); |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
155 void (*dll_lua_getfield) (lua_State *L, int idx, const char *k); |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
156 void (*dll_lua_rawget) (lua_State *L, int idx); |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
157 void (*dll_lua_createtable) (lua_State *L, int narr, int nrec); |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
158 void *(*dll_lua_newuserdata) (lua_State *L, size_t sz); |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
159 int (*dll_lua_getmetatable) (lua_State *L, int objindex); |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
160 void (*dll_lua_setfield) (lua_State *L, int idx, const char *k); |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
161 void (*dll_lua_rawset) (lua_State *L, int idx); |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
162 void (*dll_lua_rawseti) (lua_State *L, int idx, int n); |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
163 int (*dll_lua_setmetatable) (lua_State *L, int objindex); |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
164 void (*dll_lua_call) (lua_State *L, int nargs, int nresults); |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
165 int (*dll_lua_pcall) (lua_State *L, int nargs, int nresults, int errfunc); |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
166 /* libs */ |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
167 int (*dll_luaopen_base) (lua_State *L); |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
168 int (*dll_luaopen_table) (lua_State *L); |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
169 int (*dll_luaopen_string) (lua_State *L); |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
170 int (*dll_luaopen_math) (lua_State *L); |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
171 int (*dll_luaopen_os) (lua_State *L); |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
172 int (*dll_luaopen_package) (lua_State *L); |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
173 int (*dll_luaopen_debug) (lua_State *L); |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
174 |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
175 typedef void **luaV_function; |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
176 typedef struct { |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
177 const char *name; |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
178 luaV_function func; |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
179 } luaV_Reg; |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
180 |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
181 static const luaV_Reg luaV_dll[] = { |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
182 /* lauxlib */ |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
183 {"luaL_register", (luaV_function) &dll_luaL_register}, |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
184 {"luaL_typerror", (luaV_function) &dll_luaL_typerror}, |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
185 {"luaL_checklstring", (luaV_function) &dll_luaL_checklstring}, |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
186 {"luaL_checkinteger", (luaV_function) &dll_luaL_checkinteger}, |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
187 {"luaL_optinteger", (luaV_function) &dll_luaL_optinteger}, |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
188 {"luaL_checktype", (luaV_function) &dll_luaL_checktype}, |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
189 {"luaL_error", (luaV_function) &dll_luaL_error}, |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
190 {"luaL_loadfile", (luaV_function) &dll_luaL_loadfile}, |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
191 {"luaL_loadbuffer", (luaV_function) &dll_luaL_loadbuffer}, |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
192 {"luaL_newstate", (luaV_function) &dll_luaL_newstate}, |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
193 {"luaL_buffinit", (luaV_function) &dll_luaL_buffinit}, |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
194 {"luaL_prepbuffer", (luaV_function) &dll_luaL_prepbuffer}, |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
195 {"luaL_addlstring", (luaV_function) &dll_luaL_addlstring}, |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
196 {"luaL_pushresult", (luaV_function) &dll_luaL_pushresult}, |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
197 /* lua */ |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
198 {"lua_close", (luaV_function) &dll_lua_close}, |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
199 {"lua_gettop", (luaV_function) &dll_lua_gettop}, |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
200 {"lua_settop", (luaV_function) &dll_lua_settop}, |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
201 {"lua_pushvalue", (luaV_function) &dll_lua_pushvalue}, |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
202 {"lua_replace", (luaV_function) &dll_lua_replace}, |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
203 {"lua_isnumber", (luaV_function) &dll_lua_isnumber}, |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
204 {"lua_isstring", (luaV_function) &dll_lua_isstring}, |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
205 {"lua_type", (luaV_function) &dll_lua_type}, |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
206 {"lua_rawequal", (luaV_function) &dll_lua_rawequal}, |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
207 {"lua_tonumber", (luaV_function) &dll_lua_tonumber}, |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
208 {"lua_tointeger", (luaV_function) &dll_lua_tointeger}, |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
209 {"lua_toboolean", (luaV_function) &dll_lua_toboolean}, |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
210 {"lua_tolstring", (luaV_function) &dll_lua_tolstring}, |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
211 {"lua_touserdata", (luaV_function) &dll_lua_touserdata}, |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
212 {"lua_pushnil", (luaV_function) &dll_lua_pushnil}, |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
213 {"lua_pushnumber", (luaV_function) &dll_lua_pushnumber}, |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
214 {"lua_pushinteger", (luaV_function) &dll_lua_pushinteger}, |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
215 {"lua_pushlstring", (luaV_function) &dll_lua_pushlstring}, |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
216 {"lua_pushstring", (luaV_function) &dll_lua_pushstring}, |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
217 {"lua_pushfstring", (luaV_function) &dll_lua_pushfstring}, |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
218 {"lua_pushcclosure", (luaV_function) &dll_lua_pushcclosure}, |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
219 {"lua_pushboolean", (luaV_function) &dll_lua_pushboolean}, |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
220 {"lua_pushlightuserdata", (luaV_function) &dll_lua_pushlightuserdata}, |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
221 {"lua_getfield", (luaV_function) &dll_lua_getfield}, |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
222 {"lua_rawget", (luaV_function) &dll_lua_rawget}, |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
223 {"lua_createtable", (luaV_function) &dll_lua_createtable}, |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
224 {"lua_newuserdata", (luaV_function) &dll_lua_newuserdata}, |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
225 {"lua_getmetatable", (luaV_function) &dll_lua_getmetatable}, |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
226 {"lua_setfield", (luaV_function) &dll_lua_setfield}, |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
227 {"lua_rawset", (luaV_function) &dll_lua_rawset}, |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
228 {"lua_rawseti", (luaV_function) &dll_lua_rawseti}, |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
229 {"lua_setmetatable", (luaV_function) &dll_lua_setmetatable}, |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
230 {"lua_call", (luaV_function) &dll_lua_call}, |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
231 {"lua_pcall", (luaV_function) &dll_lua_pcall}, |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
232 /* libs */ |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
233 {"luaopen_base", (luaV_function) &dll_luaopen_base}, |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
234 {"luaopen_table", (luaV_function) &dll_luaopen_table}, |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
235 {"luaopen_string", (luaV_function) &dll_luaopen_string}, |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
236 {"luaopen_math", (luaV_function) &dll_luaopen_math}, |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
237 {"luaopen_os", (luaV_function) &dll_luaopen_os}, |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
238 {"luaopen_package", (luaV_function) &dll_luaopen_package}, |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
239 {"luaopen_debug", (luaV_function) &dll_luaopen_debug}, |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
240 {NULL, NULL} |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
241 }; |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
242 |
2373
f149bb1cf5be
Make it possible to load Lua dynamically on Unix. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
2331
diff
changeset
|
243 static HANDLE hinstLua = NULL; |
2320
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
244 |
2330 | 245 static void |
246 end_dynamic_lua(void) | |
247 { | |
248 if (hinstLua) | |
249 { | |
2373
f149bb1cf5be
Make it possible to load Lua dynamically on Unix. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
2331
diff
changeset
|
250 close_dll(hinstLua); |
2330 | 251 hinstLua = 0; |
2320
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
252 } |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
253 } |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
254 |
2330 | 255 static int |
256 lua_link_init(char *libname, int verbose) | |
257 { | |
2320
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
258 const luaV_Reg *reg; |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
259 if (hinstLua) return OK; |
2373
f149bb1cf5be
Make it possible to load Lua dynamically on Unix. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
2331
diff
changeset
|
260 hinstLua = load_dll(libname); |
2330 | 261 if (!hinstLua) |
262 { | |
263 if (verbose) | |
264 EMSG2(_(e_loadlib), libname); | |
265 return FAIL; | |
2320
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
266 } |
2330 | 267 for (reg = luaV_dll; reg->func; reg++) |
268 { | |
2373
f149bb1cf5be
Make it possible to load Lua dynamically on Unix. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
2331
diff
changeset
|
269 if ((*reg->func = symbol_from_dll(hinstLua, reg->name)) == NULL) |
f149bb1cf5be
Make it possible to load Lua dynamically on Unix. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
2331
diff
changeset
|
270 { |
f149bb1cf5be
Make it possible to load Lua dynamically on Unix. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
2331
diff
changeset
|
271 close_dll(hinstLua); |
2330 | 272 hinstLua = 0; |
273 if (verbose) | |
274 EMSG2(_(e_loadfunc), reg->name); | |
275 return FAIL; | |
276 } | |
2320
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
277 } |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
278 return OK; |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
279 } |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
280 |
2330 | 281 int |
282 lua_enabled(int verbose) | |
283 { | |
2320
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
284 return lua_link_init(DYNAMIC_LUA_DLL, verbose) == OK; |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
285 } |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
286 |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
287 #endif /* DYNAMIC_LUA */ |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
288 |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
289 |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
290 /* ======= Internal ======= */ |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
291 |
2330 | 292 static void |
293 luaV_newmetatable(lua_State *L, const char *tname) | |
294 { | |
2320
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
295 lua_newtable(L); |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
296 lua_pushlightuserdata(L, (void *) tname); |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
297 lua_pushvalue(L, -2); |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
298 lua_rawset(L, LUA_REGISTRYINDEX); |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
299 } |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
300 |
2330 | 301 static void * |
302 luaV_toudata(lua_State *L, int ud, const char *tname) | |
303 { | |
2320
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
304 void *p = lua_touserdata(L, ud); |
2330 | 305 |
306 if (p != NULL) /* value is userdata? */ | |
307 { | |
308 if (lua_getmetatable(L, ud)) /* does it have a metatable? */ | |
309 { | |
310 luaV_getfield(L, tname); /* get metatable */ | |
311 if (lua_rawequal(L, -1, -2)) /* MTs match? */ | |
312 { | |
313 lua_pop(L, 2); /* MTs */ | |
314 return p; | |
315 } | |
316 } | |
2320
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
317 } |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
318 return NULL; |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
319 } |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
320 |
2330 | 321 static void * |
322 luaV_checkudata(lua_State *L, int ud, const char *tname) | |
323 { | |
2320
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
324 void *p = luaV_toudata(L, ud, tname); |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
325 if (p == NULL) luaL_typerror(L, ud, tname); |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
326 return p; |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
327 } |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
328 |
2330 | 329 static void |
330 luaV_pushtypval(lua_State *L, typval_T *tv) | |
331 { | |
2320
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
332 if (tv == NULL) luaL_error(L, "null type"); |
2330 | 333 switch (tv->v_type) |
334 { | |
335 case VAR_STRING: | |
336 lua_pushstring(L, (char *) tv->vval.v_string); | |
337 break; | |
338 case VAR_NUMBER: | |
339 lua_pushinteger(L, (int) tv->vval.v_number); | |
340 break; | |
2320
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
341 #ifdef FEAT_FLOAT |
2330 | 342 case VAR_FLOAT: |
343 lua_pushnumber(L, (lua_Number) tv->vval.v_float); | |
344 break; | |
2320
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
345 #endif |
2330 | 346 case VAR_LIST: { |
347 list_T *l = tv->vval.v_list; | |
348 | |
349 if (l != NULL) | |
350 { | |
351 /* check cache */ | |
352 lua_pushlightuserdata(L, (void *) l); | |
353 lua_rawget(L, LUA_ENVIRONINDEX); | |
354 if (lua_isnil(L, -1)) /* not interned? */ | |
355 { | |
356 listitem_T *li; | |
357 int n = 0; | |
358 lua_pop(L, 1); /* nil */ | |
359 lua_newtable(L); | |
360 lua_pushlightuserdata(L, (void *) l); | |
361 lua_pushvalue(L, -2); | |
362 lua_rawset(L, LUA_ENVIRONINDEX); | |
363 for (li = l->lv_first; li != NULL; li = li->li_next) | |
364 { | |
365 luaV_pushtypval(L, &li->li_tv); | |
366 lua_rawseti(L, -2, ++n); | |
367 } | |
368 } | |
369 } | |
370 else lua_pushnil(L); | |
371 break; | |
372 } | |
373 case VAR_DICT: { | |
374 dict_T *d = tv->vval.v_dict; | |
375 | |
376 if (d != NULL) | |
377 { | |
378 /* check cache */ | |
379 lua_pushlightuserdata(L, (void *) d); | |
380 lua_rawget(L, LUA_ENVIRONINDEX); | |
2373
f149bb1cf5be
Make it possible to load Lua dynamically on Unix. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
2331
diff
changeset
|
381 if (lua_isnil(L, -1)) /* not interned? */ |
f149bb1cf5be
Make it possible to load Lua dynamically on Unix. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
2331
diff
changeset
|
382 { |
2330 | 383 hashtab_T *ht = &d->dv_hashtab; |
384 hashitem_T *hi; | |
385 int n = ht->ht_used; /* remaining items */ | |
386 lua_pop(L, 1); /* nil */ | |
387 lua_newtable(L); | |
388 lua_pushlightuserdata(L, (void *) d); | |
389 lua_pushvalue(L, -2); | |
390 lua_rawset(L, LUA_ENVIRONINDEX); | |
391 for (hi = ht->ht_array; n > 0; hi++) | |
392 { | |
393 if (!HASHITEM_EMPTY(hi)) | |
394 { | |
395 dictitem_T *di = dict_lookup(hi); | |
396 luaV_pushtypval(L, &di->di_tv); | |
397 lua_setfield(L, -2, (char *) hi->hi_key); | |
398 n--; | |
399 } | |
400 } | |
401 } | |
402 } | |
403 else lua_pushnil(L); | |
404 break; | |
405 } | |
406 default: | |
407 luaL_error(L, "invalid type"); | |
2320
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
408 } |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
409 } |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
410 |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
411 /* similar to luaL_addlstring, but replaces \0 with \n if toline and |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
412 * \n with \0 otherwise */ |
2330 | 413 static void |
414 luaV_addlstring(luaL_Buffer *b, const char *s, size_t l, int toline) | |
415 { | |
416 while (l--) | |
417 { | |
418 if (*s == '\0' && toline) | |
419 luaL_addchar(b, '\n'); | |
420 else if (*s == '\n' && !toline) | |
421 luaL_addchar(b, '\0'); | |
422 else | |
423 luaL_addchar(b, *s); | |
424 s++; | |
2320
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
425 } |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
426 } |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
427 |
2330 | 428 static void |
429 luaV_pushline(lua_State *L, buf_T *buf, linenr_T n) | |
430 { | |
2320
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
431 const char *s = (const char *) ml_get_buf(buf, n, FALSE); |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
432 luaL_Buffer b; |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
433 luaL_buffinit(L, &b); |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
434 luaV_addlstring(&b, s, strlen(s), 0); |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
435 luaL_pushresult(&b); |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
436 } |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
437 |
2330 | 438 static char_u * |
439 luaV_toline(lua_State *L, int pos) | |
440 { | |
2320
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
441 size_t l; |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
442 const char *s = lua_tolstring(L, pos, &l); |
2330 | 443 |
2320
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
444 luaL_Buffer b; |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
445 luaL_buffinit(L, &b); |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
446 luaV_addlstring(&b, s, l, 1); |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
447 luaL_pushresult(&b); |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
448 return (char_u *) lua_tostring(L, -1); |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
449 } |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
450 |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
451 /* pops a string s from the top of the stack and calls mf(t) for pieces t of |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
452 * s separated by newlines */ |
2330 | 453 static void |
454 luaV_msgfunc(lua_State *L, msgfunc_T mf) | |
455 { | |
2320
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
456 luaL_Buffer b; |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
457 size_t l; |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
458 const char *p, *s = lua_tolstring(L, -1, &l); |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
459 luaL_buffinit(L, &b); |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
460 luaV_addlstring(&b, s, l, 0); |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
461 luaL_pushresult(&b); |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
462 /* break string */ |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
463 p = s = lua_tolstring(L, -1, &l); |
2330 | 464 while (l--) |
465 { | |
466 if (*p++ == '\0') /* break? */ | |
467 { | |
468 mf((char_u *) s); | |
469 s = p; | |
470 } | |
2320
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
471 } |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
472 mf((char_u *) s); |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
473 lua_pop(L, 2); /* original and modified strings */ |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
474 } |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
475 |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
476 |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
477 /* ======= Buffer type ======= */ |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
478 |
2330 | 479 static luaV_Buffer * |
480 luaV_newbuffer(lua_State *L, buf_T *buf) | |
481 { | |
2320
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
482 luaV_Buffer *b = (luaV_Buffer *) lua_newuserdata(L, sizeof(luaV_Buffer)); |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
483 *b = buf; |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
484 lua_pushlightuserdata(L, (void *) buf); |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
485 lua_pushvalue(L, -2); |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
486 lua_rawset(L, LUA_ENVIRONINDEX); /* env[buf] = udata */ |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
487 /* to avoid GC, store as key in env */ |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
488 lua_pushvalue(L, -1); |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
489 lua_pushboolean(L, 1); |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
490 lua_rawset(L, LUA_ENVIRONINDEX); /* env[udata] = true */ |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
491 /* set metatable */ |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
492 luaV_getfield(L, LUAVIM_BUFFER); |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
493 lua_setmetatable(L, -2); |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
494 return b; |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
495 } |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
496 |
2330 | 497 static luaV_Buffer * |
498 luaV_pushbuffer (lua_State *L, buf_T *buf) | |
499 { | |
2320
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
500 luaV_Buffer *b = NULL; |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
501 if (buf == NULL) |
2330 | 502 lua_pushnil(L); |
2320
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
503 else { |
2330 | 504 lua_pushlightuserdata(L, (void *) buf); |
505 lua_rawget(L, LUA_ENVIRONINDEX); | |
506 if (lua_isnil(L, -1)) /* not interned? */ | |
507 { | |
508 lua_pop(L, 1); | |
509 b = luaV_newbuffer(L, buf); | |
510 } | |
511 else | |
512 b = (luaV_Buffer *) lua_touserdata(L, -1); | |
2320
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
513 } |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
514 return b; |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
515 } |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
516 |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
517 /* Buffer metamethods */ |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
518 |
2330 | 519 static int |
520 luaV_buffer_tostring(lua_State *L) | |
521 { | |
2320
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
522 lua_pushfstring(L, "%s: %p", LUAVIM_BUFFER, lua_touserdata(L, 1)); |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
523 return 1; |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
524 } |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
525 |
2330 | 526 static int |
527 luaV_buffer_len(lua_State *L) | |
528 { | |
2320
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
529 luaV_Buffer *b = lua_touserdata(L, 1); |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
530 lua_pushinteger(L, (*b)->b_ml.ml_line_count); |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
531 return 1; |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
532 } |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
533 |
2330 | 534 static int |
535 luaV_buffer_call(lua_State *L) | |
536 { | |
2320
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
537 luaV_Buffer *b = (luaV_Buffer *) lua_touserdata(L, 1); |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
538 lua_settop(L, 1); |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
539 set_curbuf(*b, DOBUF_SPLIT); |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
540 return 1; |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
541 } |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
542 |
2330 | 543 static int |
544 luaV_buffer_index(lua_State *L) | |
545 { | |
2320
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
546 luaV_Buffer *b = (luaV_Buffer *) lua_touserdata(L, 1); |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
547 linenr_T n = (linenr_T) lua_tointeger(L, 2); |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
548 if (n > 0 && n <= (*b)->b_ml.ml_line_count) |
2330 | 549 luaV_pushline(L, *b, n); |
550 else if (lua_isstring(L, 2)) | |
551 { | |
552 const char *s = lua_tostring(L, 2); | |
553 if (strncmp(s, "name", 4) == 0) | |
554 lua_pushstring(L, (char *) (*b)->b_sfname); | |
555 else if (strncmp(s, "fname", 5) == 0) | |
556 lua_pushstring(L, (char *) (*b)->b_ffname); | |
557 else if (strncmp(s, "number", 6) == 0) | |
558 lua_pushinteger(L, (*b)->b_fnum); | |
559 /* methods */ | |
560 else if (strncmp(s, "insert", 6) == 0 | |
561 || strncmp(s, "next", 4) == 0 | |
562 || strncmp(s, "previous", 8) == 0 | |
563 || strncmp(s, "isvalid", 7) == 0) | |
564 { | |
565 lua_getmetatable(L, 1); | |
566 lua_getfield(L, -1, s); | |
567 } | |
568 else | |
569 lua_pushnil(L); | |
2320
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
570 } |
2330 | 571 else |
572 lua_pushnil(L); | |
2320
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
573 return 1; |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
574 } |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
575 |
2330 | 576 static int |
577 luaV_buffer_newindex(lua_State *L) | |
578 { | |
2320
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
579 luaV_Buffer *b = (luaV_Buffer *) lua_touserdata(L, 1); |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
580 linenr_T n = (linenr_T) luaL_checkinteger(L, 2); |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
581 #ifdef HAVE_SANDBOX |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
582 luaV_checksandbox(L); |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
583 #endif |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
584 if (n < 1 || n > (*b)->b_ml.ml_line_count) |
2330 | 585 luaL_error(L, "invalid line number"); |
586 if (lua_isnil(L, 3)) /* delete line */ | |
587 { | |
588 buf_T *buf = curbuf; | |
589 curbuf = *b; | |
590 if (u_savedel(n, 1L) == FAIL) | |
591 { | |
592 curbuf = buf; | |
593 luaL_error(L, "cannot save undo information"); | |
594 } | |
595 else if (ml_delete(n, FALSE) == FAIL) | |
596 { | |
597 curbuf = buf; | |
598 luaL_error(L, "cannot delete line"); | |
599 } | |
600 else { | |
601 deleted_lines_mark(n, 1L); | |
602 if (*b == curwin->w_buffer) /* fix cursor in current window? */ | |
603 { | |
604 if (curwin->w_cursor.lnum >= n) | |
605 { | |
606 if (curwin->w_cursor.lnum > n) | |
607 { | |
608 curwin->w_cursor.lnum -= 1; | |
609 check_cursor_col(); | |
610 } | |
611 else check_cursor(); | |
612 changed_cline_bef_curs(); | |
613 } | |
614 invalidate_botline(); | |
615 } | |
616 } | |
617 curbuf = buf; | |
2320
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
618 } |
2330 | 619 else if (lua_isstring(L, 3)) /* update line */ |
620 { | |
621 buf_T *buf = curbuf; | |
622 curbuf = *b; | |
623 if (u_savesub(n) == FAIL) | |
624 { | |
625 curbuf = buf; | |
626 luaL_error(L, "cannot save undo information"); | |
627 } | |
628 else if (ml_replace(n, luaV_toline(L, 3), TRUE) == FAIL) | |
629 { | |
630 curbuf = buf; | |
631 luaL_error(L, "cannot replace line"); | |
632 } | |
633 else changed_bytes(n, 0); | |
634 curbuf = buf; | |
635 if (*b == curwin->w_buffer) | |
636 check_cursor_col(); | |
2320
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
637 } |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
638 else |
2330 | 639 luaL_error(L, "wrong argument to change line"); |
2320
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
640 return 0; |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
641 } |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
642 |
2330 | 643 static int |
644 luaV_buffer_insert(lua_State *L) | |
645 { | |
2320
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
646 luaV_Buffer *b = luaV_checkudata(L, 1, LUAVIM_BUFFER); |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
647 linenr_T last = (*b)->b_ml.ml_line_count; |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
648 linenr_T n = (linenr_T) luaL_optinteger(L, 3, last); |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
649 buf_T *buf; |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
650 luaL_checktype(L, 2, LUA_TSTRING); |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
651 #ifdef HAVE_SANDBOX |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
652 luaV_checksandbox(L); |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
653 #endif |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
654 /* fix insertion line */ |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
655 if (n < 0) n = 0; |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
656 if (n > last) n = last; |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
657 /* insert */ |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
658 buf = curbuf; |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
659 curbuf = *b; |
2330 | 660 if (u_save(n, n + 1) == FAIL) |
661 { | |
662 curbuf = buf; | |
663 luaL_error(L, "cannot save undo information"); | |
2320
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
664 } |
2330 | 665 else if (ml_append(n, luaV_toline(L, 2), 0, FALSE) == FAIL) |
666 { | |
667 curbuf = buf; | |
668 luaL_error(L, "cannot insert line"); | |
2320
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
669 } |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
670 else |
2330 | 671 appended_lines_mark(n, 1L); |
2320
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
672 curbuf = buf; |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
673 update_screen(VALID); |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
674 return 0; |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
675 } |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
676 |
2330 | 677 static int |
678 luaV_buffer_next(lua_State *L) | |
679 { | |
2320
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
680 luaV_Buffer *b = luaV_checkudata(L, 1, LUAVIM_BUFFER); |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
681 luaV_pushbuffer(L, (*b)->b_next); |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
682 return 1; |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
683 } |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
684 |
2330 | 685 static int |
686 luaV_buffer_previous(lua_State *L) | |
687 { | |
2320
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
688 luaV_Buffer *b = luaV_checkudata(L, 1, LUAVIM_BUFFER); |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
689 luaV_pushbuffer(L, (*b)->b_prev); |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
690 return 1; |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
691 } |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
692 |
2330 | 693 static int |
694 luaV_buffer_isvalid(lua_State *L) | |
695 { | |
2320
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
696 luaV_Buffer *b = luaV_checkudata(L, 1, LUAVIM_BUFFER); |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
697 lua_pushlightuserdata(L, (void *) (*b)); |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
698 lua_rawget(L, LUA_ENVIRONINDEX); |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
699 lua_pushboolean(L, !lua_isnil(L, -1)); |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
700 return 1; |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
701 } |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
702 |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
703 static const luaL_Reg luaV_Buffer_mt[] = { |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
704 {"__tostring", luaV_buffer_tostring}, |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
705 {"__len", luaV_buffer_len}, |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
706 {"__call", luaV_buffer_call}, |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
707 {"__index", luaV_buffer_index}, |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
708 {"__newindex", luaV_buffer_newindex}, |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
709 {"insert", luaV_buffer_insert}, |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
710 {"next", luaV_buffer_next}, |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
711 {"previous", luaV_buffer_previous}, |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
712 {"isvalid", luaV_buffer_isvalid}, |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
713 {NULL, NULL} |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
714 }; |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
715 |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
716 |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
717 /* ======= Window type ======= */ |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
718 |
2330 | 719 static luaV_Window * |
720 luaV_newwindow(lua_State *L, win_T *win) | |
721 { | |
2320
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
722 luaV_Window *w = (luaV_Window *) lua_newuserdata(L, sizeof(luaV_Window)); |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
723 *w = win; |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
724 lua_pushlightuserdata(L, (void *) win); |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
725 lua_pushvalue(L, -2); |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
726 lua_rawset(L, LUA_ENVIRONINDEX); /* env[win] = udata */ |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
727 /* to avoid GC, store as key in env */ |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
728 lua_pushvalue(L, -1); |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
729 lua_pushboolean(L, 1); |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
730 lua_rawset(L, LUA_ENVIRONINDEX); /* env[udata] = true */ |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
731 /* set metatable */ |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
732 luaV_getfield(L, LUAVIM_WINDOW); |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
733 lua_setmetatable(L, -2); |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
734 return w; |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
735 } |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
736 |
2330 | 737 static luaV_Window * |
738 luaV_pushwindow(lua_State *L, win_T *win) | |
739 { | |
2320
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
740 luaV_Window *w = NULL; |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
741 if (win == NULL) |
2330 | 742 lua_pushnil(L); |
2320
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
743 else { |
2330 | 744 lua_pushlightuserdata(L, (void *) win); |
745 lua_rawget(L, LUA_ENVIRONINDEX); | |
746 if (lua_isnil(L, -1)) /* not interned? */ | |
747 { | |
748 lua_pop(L, 1); | |
749 w = luaV_newwindow(L, win); | |
750 } | |
751 else w = (luaV_Window *) lua_touserdata(L, -1); | |
2320
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
752 } |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
753 return w; |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
754 } |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
755 |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
756 /* Window metamethods */ |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
757 |
2330 | 758 static int |
759 luaV_window_tostring(lua_State *L) | |
760 { | |
2320
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
761 lua_pushfstring(L, "%s: %p", LUAVIM_WINDOW, lua_touserdata(L, 1)); |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
762 return 1; |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
763 } |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
764 |
2330 | 765 static int |
766 luaV_window_call(lua_State *L) | |
767 { | |
2320
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
768 luaV_Window *w = (luaV_Window *) lua_touserdata(L, 1); |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
769 lua_settop(L, 1); |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
770 win_goto(*w); |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
771 return 1; |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
772 } |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
773 |
2330 | 774 static int |
775 luaV_window_index(lua_State *L) | |
776 { | |
2320
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
777 luaV_Window *w = (luaV_Window *) lua_touserdata(L, 1); |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
778 const char *s = luaL_checkstring(L, 2); |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
779 if (strncmp(s, "buffer", 6) == 0) |
2330 | 780 luaV_pushbuffer(L, (*w)->w_buffer); |
2320
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
781 else if (strncmp(s, "line", 4) == 0) |
2330 | 782 lua_pushinteger(L, (*w)->w_cursor.lnum); |
2320
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
783 else if (strncmp(s, "col", 3) == 0) |
2330 | 784 lua_pushinteger(L, (*w)->w_cursor.col + 1); |
2320
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
785 #ifdef FEAT_VERTSPLIT |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
786 else if (strncmp(s, "width", 5) == 0) |
2330 | 787 lua_pushinteger(L, W_WIDTH((*w))); |
2320
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
788 #endif |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
789 else if (strncmp(s, "height", 6) == 0) |
2330 | 790 lua_pushinteger(L, (*w)->w_height); |
2320
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
791 /* methods */ |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
792 else if (strncmp(s, "next", 4) == 0 |
2330 | 793 || strncmp(s, "previous", 8) == 0 |
794 || strncmp(s, "isvalid", 7) == 0) | |
795 { | |
796 lua_getmetatable(L, 1); | |
797 lua_getfield(L, -1, s); | |
2320
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
798 } |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
799 else |
2330 | 800 lua_pushnil(L); |
2320
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
801 return 1; |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
802 } |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
803 |
2330 | 804 static int |
805 luaV_window_newindex (lua_State *L) | |
806 { | |
2320
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
807 luaV_Window *w = (luaV_Window *) lua_touserdata(L, 1); |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
808 const char *s = luaL_checkstring(L, 2); |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
809 int v = luaL_checkinteger(L, 3); |
2330 | 810 if (strncmp(s, "line", 4) == 0) |
811 { | |
2320
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
812 #ifdef HAVE_SANDBOX |
2330 | 813 luaV_checksandbox(L); |
2320
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
814 #endif |
2330 | 815 if (v < 1 || v > (*w)->w_buffer->b_ml.ml_line_count) |
816 luaL_error(L, "line out of range"); | |
817 (*w)->w_cursor.lnum = v; | |
818 update_screen(VALID); | |
2320
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
819 } |
2330 | 820 else if (strncmp(s, "col", 3) == 0) |
821 { | |
2320
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
822 #ifdef HAVE_SANDBOX |
2330 | 823 luaV_checksandbox(L); |
2320
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
824 #endif |
2330 | 825 (*w)->w_cursor.col = v - 1; |
826 update_screen(VALID); | |
2320
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
827 } |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
828 #ifdef FEAT_VERTSPLIT |
2330 | 829 else if (strncmp(s, "width", 5) == 0) |
830 { | |
831 win_T *win = curwin; | |
2320
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
832 #ifdef FEAT_GUI |
2330 | 833 need_mouse_correct = TRUE; |
2320
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
834 #endif |
2330 | 835 curwin = *w; |
836 win_setwidth(v); | |
837 curwin = win; | |
2320
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
838 } |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
839 #endif |
2330 | 840 else if (strncmp(s, "height", 6) == 0) |
841 { | |
842 win_T *win = curwin; | |
2320
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
843 #ifdef FEAT_GUI |
2330 | 844 need_mouse_correct = TRUE; |
2320
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
845 #endif |
2330 | 846 curwin = *w; |
847 win_setheight(v); | |
848 curwin = win; | |
2320
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
849 } |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
850 else |
2330 | 851 luaL_error(L, "invalid window property: `%s'", s); |
2320
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
852 return 0; |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
853 } |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
854 |
2330 | 855 static int |
856 luaV_window_next(lua_State *L) | |
857 { | |
2320
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
858 luaV_Window *w = luaV_checkudata(L, 1, LUAVIM_WINDOW); |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
859 luaV_pushwindow(L, (*w)->w_next); |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
860 return 1; |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
861 } |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
862 |
2330 | 863 static int |
864 luaV_window_previous(lua_State *L) | |
865 { | |
2320
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
866 luaV_Window *w = luaV_checkudata(L, 1, LUAVIM_WINDOW); |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
867 luaV_pushwindow(L, (*w)->w_prev); |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
868 return 1; |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
869 } |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
870 |
2330 | 871 static int |
872 luaV_window_isvalid(lua_State *L) | |
873 { | |
2320
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
874 luaV_Window *w = luaV_checkudata(L, 1, LUAVIM_WINDOW); |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
875 lua_pushlightuserdata(L, (void *) (*w)); |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
876 lua_rawget(L, LUA_ENVIRONINDEX); |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
877 lua_pushboolean(L, !lua_isnil(L, -1)); |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
878 return 1; |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
879 } |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
880 |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
881 static const luaL_Reg luaV_Window_mt[] = { |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
882 {"__tostring", luaV_window_tostring}, |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
883 {"__call", luaV_window_call}, |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
884 {"__index", luaV_window_index}, |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
885 {"__newindex", luaV_window_newindex}, |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
886 {"next", luaV_window_next}, |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
887 {"previous", luaV_window_previous}, |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
888 {"isvalid", luaV_window_isvalid}, |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
889 {NULL, NULL} |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
890 }; |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
891 |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
892 |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
893 /* ======= Vim module ======= */ |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
894 |
2330 | 895 static int |
896 luaV_print(lua_State *L) | |
897 { | |
2320
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
898 int i, n = lua_gettop(L); /* nargs */ |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
899 const char *s; |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
900 size_t l; |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
901 luaL_Buffer b; |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
902 luaL_buffinit(L, &b); |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
903 lua_getglobal(L, "tostring"); |
2330 | 904 for (i = 1; i <= n; i++) |
905 { | |
906 lua_pushvalue(L, -1); /* tostring */ | |
907 lua_pushvalue(L, i); /* arg */ | |
908 lua_call(L, 1, 1); | |
909 s = lua_tolstring(L, -1, &l); | |
910 if (s == NULL) | |
911 return luaL_error(L, "cannot convert to string"); | |
912 if (i > 1) luaL_addchar(&b, ' '); /* use space instead of tab */ | |
913 luaV_addlstring(&b, s, l, 0); | |
914 lua_pop(L, 1); | |
2320
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
915 } |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
916 luaL_pushresult(&b); |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
917 luaV_msg(L); |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
918 return 0; |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
919 } |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
920 |
2330 | 921 static int |
922 luaV_command(lua_State *L) | |
923 { | |
2320
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
924 do_cmdline_cmd((char_u *) luaL_checkstring(L, 1)); |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
925 update_screen(VALID); |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
926 return 0; |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
927 } |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
928 |
2330 | 929 static int |
930 luaV_eval(lua_State *L) | |
931 { | |
2320
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
932 typval_T *tv = eval_expr((char_u *) luaL_checkstring(L, 1), NULL); |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
933 if (tv == NULL) luaL_error(L, "invalid expression"); |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
934 luaV_pushtypval(L, tv); |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
935 return 1; |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
936 } |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
937 |
2330 | 938 static int |
2331
3840b7508835
Make it easier to build with Lua. Remove compiler warnings.
Bram Moolenaar <bram@vim.org>
parents:
2330
diff
changeset
|
939 luaV_beep(lua_State *L UNUSED) |
2330 | 940 { |
2320
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
941 vim_beep(); |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
942 return 0; |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
943 } |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
944 |
2330 | 945 static int |
946 luaV_line(lua_State *L) | |
947 { | |
2320
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
948 luaV_pushline(L, curbuf, curwin->w_cursor.lnum); |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
949 return 1; |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
950 } |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
951 |
2330 | 952 static int |
953 luaV_buffer(lua_State *L) | |
954 { | |
2320
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
955 buf_T *buf; |
2330 | 956 if (lua_isstring(L, 1)) /* get by number or name? */ |
957 { | |
958 if (lua_isnumber(L, 1)) /* by number? */ | |
959 { | |
960 int n = lua_tointeger(L, 1); | |
961 for (buf = firstbuf; buf != NULL; buf = buf->b_next) | |
962 if (buf->b_fnum == n) break; | |
963 } | |
964 else { /* by name */ | |
965 size_t l; | |
966 const char *s = lua_tolstring(L, 1, &l); | |
967 for (buf = firstbuf; buf != NULL; buf = buf->b_next) | |
968 { | |
969 if (buf->b_ffname == NULL || buf->b_sfname == NULL) | |
970 { | |
971 if (l == 0) break; | |
972 } | |
2331
3840b7508835
Make it easier to build with Lua. Remove compiler warnings.
Bram Moolenaar <bram@vim.org>
parents:
2330
diff
changeset
|
973 else if (strncmp(s, (char *)buf->b_ffname, l) == 0 |
3840b7508835
Make it easier to build with Lua. Remove compiler warnings.
Bram Moolenaar <bram@vim.org>
parents:
2330
diff
changeset
|
974 || strncmp(s, (char *)buf->b_sfname, l) == 0) |
2330 | 975 break; |
976 } | |
977 } | |
978 if (buf == NULL) /* not found? */ | |
979 lua_pushnil(L); | |
980 else | |
981 luaV_pushbuffer(L, buf); | |
2320
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
982 } |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
983 else { |
2330 | 984 buf = (lua_toboolean(L, 1)) ? firstbuf : curbuf; /* first buffer? */ |
985 luaV_pushbuffer(L, buf); | |
2320
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
986 } |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
987 return 1; |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
988 } |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
989 |
2330 | 990 static int |
991 luaV_window(lua_State *L) | |
992 { | |
2320
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
993 win_T *win; |
2330 | 994 if (lua_isnumber(L, 1)) /* get by number? */ |
995 { | |
996 int n = lua_tointeger(L, 1); | |
997 for (win = firstwin; win != NULL; win = win->w_next, n--) | |
998 if (n == 1) break; | |
999 if (win == NULL) /* not found? */ | |
1000 lua_pushnil(L); | |
1001 else | |
1002 luaV_pushwindow(L, win); | |
2320
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
1003 } |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
1004 else { |
2330 | 1005 win = (lua_toboolean(L, 1)) ? firstwin : curwin; /* first window? */ |
1006 luaV_pushwindow(L, win); | |
2320
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
1007 } |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
1008 return 1; |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
1009 } |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
1010 |
2330 | 1011 static int |
1012 luaV_open(lua_State *L) | |
1013 { | |
2320
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
1014 luaV_Buffer *b; |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
1015 char_u *s = NULL; |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
1016 #ifdef HAVE_SANDBOX |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
1017 luaV_checksandbox(L); |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
1018 #endif |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
1019 if (lua_isstring(L, 1)) s = (char_u *) lua_tostring(L, 1); |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
1020 b = luaV_pushbuffer(L, buflist_new(s, NULL, 1L, BLN_LISTED)); |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
1021 return 1; |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
1022 } |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
1023 |
2330 | 1024 static int |
1025 luaV_isbuffer(lua_State *L) | |
1026 { | |
2320
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
1027 lua_pushboolean(L, luaV_toudata(L, 1, LUAVIM_BUFFER) != NULL); |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
1028 return 1; |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
1029 } |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
1030 |
2330 | 1031 static int |
1032 luaV_iswindow(lua_State *L) | |
1033 { | |
2320
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
1034 lua_pushboolean(L, luaV_toudata(L, 1, LUAVIM_WINDOW) != NULL); |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
1035 return 1; |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
1036 } |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
1037 |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
1038 /* for freeing buffer and window objects; lightuserdata as arg */ |
2330 | 1039 static int |
1040 luaV_free(lua_State *L) | |
1041 { | |
2320
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
1042 lua_pushvalue(L, 1); /* lightudata */ |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
1043 lua_rawget(L, LUA_ENVIRONINDEX); |
2330 | 1044 if (!lua_isnil(L, -1)) |
1045 { | |
1046 lua_pushnil(L); | |
1047 lua_rawset(L, LUA_ENVIRONINDEX); /* env[udata] = nil */ | |
1048 lua_pushnil(L); | |
1049 lua_rawset(L, LUA_ENVIRONINDEX); /* env[lightudata] = nil */ | |
2320
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
1050 } |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
1051 return 0; |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
1052 } |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
1053 |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
1054 static const luaL_Reg luaV_module[] = { |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
1055 {"command", luaV_command}, |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
1056 {"eval", luaV_eval}, |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
1057 {"beep", luaV_beep}, |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
1058 {"line", luaV_line}, |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
1059 {"buffer", luaV_buffer}, |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
1060 {"window", luaV_window}, |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
1061 {"open", luaV_open}, |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
1062 {"isbuffer", luaV_isbuffer}, |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
1063 {"iswindow", luaV_iswindow}, |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
1064 {NULL, NULL} |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
1065 }; |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
1066 |
2330 | 1067 static int |
1068 luaopen_vim(lua_State *L) | |
1069 { | |
2320
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
1070 /* set environment */ |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
1071 lua_newtable(L); |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
1072 lua_newtable(L); |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
1073 lua_pushliteral(L, "v"); |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
1074 lua_setfield(L, -2, "__mode"); |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
1075 lua_setmetatable(L, -2); |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
1076 lua_replace(L, LUA_ENVIRONINDEX); |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
1077 /* print */ |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
1078 lua_pushcfunction(L, luaV_print); |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
1079 lua_setglobal(L, "print"); |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
1080 /* free */ |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
1081 lua_pushlightuserdata(L, (void *) LUAVIM_FREE); |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
1082 lua_pushcfunction(L, luaV_free); |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
1083 lua_rawset(L, LUA_REGISTRYINDEX); |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
1084 /* register */ |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
1085 luaV_newmetatable(L, LUAVIM_BUFFER); |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
1086 luaL_register(L, NULL, luaV_Buffer_mt); |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
1087 luaV_newmetatable(L, LUAVIM_WINDOW); |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
1088 luaL_register(L, NULL, luaV_Window_mt); |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
1089 luaL_register(L, LUAVIM_NAME, luaV_module); |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
1090 return 0; |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
1091 } |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
1092 |
2330 | 1093 static lua_State * |
1094 luaV_newstate(void) | |
1095 { | |
2320
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
1096 lua_State *L = luaL_newstate(); |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
1097 const luaL_Reg luaV_core_libs[] = { |
2330 | 1098 {"", luaopen_base}, |
1099 {LUA_TABLIBNAME, luaopen_table}, | |
1100 {LUA_STRLIBNAME, luaopen_string}, | |
1101 {LUA_MATHLIBNAME, luaopen_math}, | |
1102 {LUA_OSLIBNAME, luaopen_os}, /* restricted */ | |
1103 {LUA_LOADLIBNAME, luaopen_package}, | |
1104 {LUA_DBLIBNAME, luaopen_debug}, | |
1105 {NULL, NULL} | |
2320
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
1106 }; |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
1107 const char *os_funcs[] = { |
2330 | 1108 "date", "clock", "time", "difftime", "getenv", NULL |
2320
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
1109 }; |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
1110 const luaL_Reg *reg = luaV_core_libs; |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
1111 const char **s = os_funcs; |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
1112 /* core libs */ |
2330 | 1113 for ( ; reg->func; reg++) |
1114 { | |
1115 lua_pushcfunction(L, reg->func); | |
1116 lua_pushstring(L, reg->name); | |
1117 lua_call(L, 1, 0); | |
2320
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
1118 } |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
1119 /* restricted os lib */ |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
1120 lua_getglobal(L, LUA_OSLIBNAME); |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
1121 lua_newtable(L); |
2330 | 1122 for ( ; *s; s++) |
1123 { | |
1124 lua_getfield(L, -2, *s); | |
1125 lua_setfield(L, -2, *s); | |
2320
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
1126 } |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
1127 lua_setglobal(L, LUA_OSLIBNAME); |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
1128 lua_pop(L, 1); /* os table */ |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
1129 /* vim */ |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
1130 lua_pushcfunction(L, luaopen_vim); |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
1131 lua_call(L, 0, 0); |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
1132 return L; |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
1133 } |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
1134 |
2330 | 1135 static void |
1136 luaV_setrange(lua_State *L, int line1, int line2) | |
1137 { | |
2320
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
1138 lua_getglobal(L, LUAVIM_NAME); |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
1139 lua_pushinteger(L, line1); |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
1140 lua_setfield(L, -2, "firstline"); |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
1141 lua_pushinteger(L, line2); |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
1142 lua_setfield(L, -2, "lastline"); |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
1143 lua_pop(L, 1); /* vim table */ |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
1144 } |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
1145 |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
1146 |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
1147 /* ======= Interface ======= */ |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
1148 |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
1149 static lua_State *L = NULL; |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
1150 |
2330 | 1151 static int |
1152 lua_init(void) | |
1153 { | |
1154 if (L == NULL) | |
1155 { | |
2320
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
1156 #ifdef DYNAMIC_LUA |
2330 | 1157 if (!lua_enabled(TRUE)) |
1158 { | |
1159 EMSG(_("Lua library cannot be loaded.")); | |
1160 return FAIL; | |
1161 } | |
2320
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
1162 #endif |
2330 | 1163 L = luaV_newstate(); |
2320
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
1164 } |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
1165 return OK; |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
1166 } |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
1167 |
2330 | 1168 void |
1169 lua_end(void) | |
1170 { | |
1171 if (L != NULL) | |
1172 { | |
1173 lua_close(L); | |
1174 L = NULL; | |
2320
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
1175 #ifdef DYNAMIC_LUA |
2330 | 1176 end_dynamic_lua(); |
2320
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
1177 #endif |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
1178 } |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
1179 } |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
1180 |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
1181 /* ex commands */ |
2330 | 1182 void |
1183 ex_lua(exarg_T *eap) | |
1184 { | |
2320
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
1185 char *script; |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
1186 if (lua_init() == FAIL) return; |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
1187 script = (char *) script_get(eap, eap->arg); |
2330 | 1188 if (!eap->skip) |
1189 { | |
1190 char *s = (script) ? script : (char *) eap->arg; | |
1191 luaV_setrange(L, eap->line1, eap->line2); | |
1192 if (luaL_loadbuffer(L, s, strlen(s), LUAVIM_CHUNKNAME) | |
1193 || lua_pcall(L, 0, 0, 0)) | |
1194 luaV_emsg(L); | |
2320
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
1195 } |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
1196 if (script != NULL) vim_free(script); |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
1197 } |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
1198 |
2330 | 1199 void |
1200 ex_luado(exarg_T *eap) | |
1201 { | |
2320
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
1202 linenr_T l; |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
1203 const char *s = (const char *) eap->arg; |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
1204 luaL_Buffer b; |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
1205 size_t len; |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
1206 if (lua_init() == FAIL) return; |
2330 | 1207 if (u_save(eap->line1 - 1, eap->line2 + 1) == FAIL) |
1208 { | |
1209 EMSG(_("cannot save undo information")); | |
1210 return; | |
2320
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
1211 } |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
1212 luaV_setrange(L, eap->line1, eap->line2); |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
1213 luaL_buffinit(L, &b); |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
1214 luaL_addlstring(&b, "return function(line) ", 22); /* header */ |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
1215 luaL_addlstring(&b, s, strlen(s)); |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
1216 luaL_addlstring(&b, " end", 4); /* footer */ |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
1217 luaL_pushresult(&b); |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
1218 s = lua_tolstring(L, -1, &len); |
2330 | 1219 if (luaL_loadbuffer(L, s, len, LUAVIM_CHUNKNAME)) |
1220 { | |
1221 luaV_emsg(L); | |
1222 lua_pop(L, 1); /* function body */ | |
1223 return; | |
2320
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
1224 } |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
1225 lua_call(L, 0, 1); |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
1226 lua_replace(L, -2); /* function -> body */ |
2330 | 1227 for (l = eap->line1; l <= eap->line2; l++) |
1228 { | |
1229 lua_pushvalue(L, -1); /* function */ | |
1230 luaV_pushline(L, curbuf, l); /* current line as arg */ | |
1231 if (lua_pcall(L, 1, 1, 0)) | |
1232 { | |
1233 luaV_emsg(L); | |
1234 break; | |
1235 } | |
1236 if (lua_isstring(L, -1)) /* update line? */ | |
1237 { | |
2320
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
1238 #ifdef HAVE_SANDBOX |
2330 | 1239 luaV_checksandbox(L); |
2320
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
1240 #endif |
2330 | 1241 ml_replace(l, luaV_toline(L, -1), TRUE); |
1242 changed_bytes(l, 0); | |
1243 lua_pop(L, 1); /* result from luaV_toline */ | |
1244 } | |
1245 lua_pop(L, 1); /* line */ | |
2320
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
1246 } |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
1247 lua_pop(L, 1); /* function */ |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
1248 check_cursor(); |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
1249 update_screen(NOT_VALID); |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
1250 } |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
1251 |
2330 | 1252 void |
1253 ex_luafile(exarg_T *eap) | |
1254 { | |
1255 if (lua_init() == FAIL) | |
1256 return; | |
1257 if (!eap->skip) | |
1258 { | |
1259 luaV_setrange(L, eap->line1, eap->line2); | |
1260 if (luaL_loadfile(L, (char *) eap->arg) || lua_pcall(L, 0, 0, 0)) | |
1261 luaV_emsg(L); | |
2320
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
1262 } |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
1263 } |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
1264 |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
1265 /* buffer */ |
2330 | 1266 void |
1267 lua_buffer_free(buf_T *buf) | |
1268 { | |
2320
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
1269 if (lua_init() == FAIL) return; |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
1270 luaV_getfield(L, LUAVIM_FREE); |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
1271 lua_pushlightuserdata(L, (void *) buf); |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
1272 lua_call(L, 1, 0); |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
1273 } |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
1274 |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
1275 /* window */ |
2330 | 1276 void |
1277 lua_window_free(win_T *win) | |
1278 { | |
2320
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
1279 if (lua_init() == FAIL) return; |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
1280 luaV_getfield(L, LUAVIM_FREE); |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
1281 lua_pushlightuserdata(L, (void *) win); |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
1282 lua_call(L, 1, 0); |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
1283 } |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
1284 |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
1285 #endif |