Mercurial > vim
annotate src/if_lua.c @ 4143:f5ef9b9c18cd v7.3.824
updated for version 7.3.824
Problem: Can redefine builtin functions. (ZyX)
Solution: Disallow adding a function to g:.
author | Bram Moolenaar <bram@vim.org> |
---|---|
date | Wed, 20 Feb 2013 15:19:43 +0100 |
parents | 688da4d486a0 |
children | c5eab656ec51 |
rev | line source |
---|---|
3450 | 1 /* vi:set ts=8 sts=4 sw=4 noet: |
2320
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 |
2683 | 12 #include "vim.h" |
13 | |
2320
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 |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
18 /* 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
|
19 * depend". */ |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
20 #if defined(FEAT_LUA) || defined(PROTO) |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
21 |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
22 #define LUAVIM_CHUNKNAME "vim chunk" |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
23 #define LUAVIM_NAME "vim" |
3450 | 24 #define LUAVIM_EVALNAME "luaeval" |
25 #define LUAVIM_EVALHEADER "local _A=select(1,...) return " | |
2320
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
26 |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
27 typedef buf_T *luaV_Buffer; |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
28 typedef win_T *luaV_Window; |
3450 | 29 typedef dict_T *luaV_Dict; |
30 typedef list_T *luaV_List; | |
2320
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
31 typedef void (*msgfunc_T)(char_u *); |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
32 |
3450 | 33 static const char LUAVIM_DICT[] = "dict"; |
34 static const char LUAVIM_LIST[] = "list"; | |
2320
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
35 static const char LUAVIM_BUFFER[] = "buffer"; |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
36 static const char LUAVIM_WINDOW[] = "window"; |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
37 static const char LUAVIM_FREE[] = "luaV_free"; |
3450 | 38 static const char LUAVIM_LUAEVAL[] = "luaV_luaeval"; |
39 static const char LUAVIM_SETREF[] = "luaV_setref"; | |
2320
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
40 |
3450 | 41 /* most functions are closures with a cache table as first upvalue; |
42 * get/setudata manage references to vim userdata in cache table through | |
43 * object pointers (light userdata) */ | |
44 #define luaV_getudata(L, v) \ | |
45 lua_pushlightuserdata((L), (void *) (v)); \ | |
46 lua_rawget((L), lua_upvalueindex(1)) | |
47 #define luaV_setudata(L, v) \ | |
48 lua_pushlightuserdata((L), (void *) (v)); \ | |
49 lua_pushvalue((L), -2); \ | |
50 lua_rawset((L), lua_upvalueindex(1)) | |
2320
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
51 #define luaV_getfield(L, s) \ |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
52 lua_pushlightuserdata((L), (void *)(s)); \ |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
53 lua_rawget((L), LUA_REGISTRYINDEX) |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
54 #define luaV_checksandbox(L) \ |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
55 if (sandbox) luaL_error((L), "not allowed in sandbox") |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
56 #define luaV_msg(L) luaV_msgfunc((L), (msgfunc_T) msg) |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
57 #define luaV_emsg(L) luaV_msgfunc((L), (msgfunc_T) emsg) |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
58 |
3450 | 59 static luaV_List *luaV_pushlist (lua_State *L, list_T *lis); |
60 static luaV_Dict *luaV_pushdict (lua_State *L, dict_T *dic); | |
61 | |
62 #if LUA_VERSION_NUM <= 501 | |
63 #define luaV_openlib(L, l, n) luaL_openlib(L, NULL, l, n) | |
64 #define luaL_typeerror luaL_typerror | |
65 #else | |
66 #define luaV_openlib luaL_setfuncs | |
67 #endif | |
2320
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
68 |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
69 #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
|
70 |
f149bb1cf5be
Make it possible to load Lua dynamically on Unix. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
2331
diff
changeset
|
71 #ifndef WIN3264 |
f149bb1cf5be
Make it possible to load Lua dynamically on Unix. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
2331
diff
changeset
|
72 # include <dlfcn.h> |
f149bb1cf5be
Make it possible to load Lua dynamically on Unix. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
2331
diff
changeset
|
73 # define HANDLE void* |
f149bb1cf5be
Make it possible to load Lua dynamically on Unix. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
2331
diff
changeset
|
74 # 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
|
75 # 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
|
76 # 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
|
77 #else |
2612 | 78 # define load_dll vimLoadLib |
2373
f149bb1cf5be
Make it possible to load Lua dynamically on Unix. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
2331
diff
changeset
|
79 # 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
|
80 # 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
|
81 #endif |
f149bb1cf5be
Make it possible to load Lua dynamically on Unix. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
2331
diff
changeset
|
82 |
2320
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
83 /* lauxlib */ |
3450 | 84 #if LUA_VERSION_NUM <= 501 |
2320
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
85 #define luaL_register dll_luaL_register |
3450 | 86 #define luaL_prepbuffer dll_luaL_prepbuffer |
87 #define luaL_openlib dll_luaL_openlib | |
2320
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
88 #define luaL_typerror dll_luaL_typerror |
3450 | 89 #define luaL_loadfile dll_luaL_loadfile |
90 #define luaL_loadbuffer dll_luaL_loadbuffer | |
91 #else | |
92 #define luaL_prepbuffsize dll_luaL_prepbuffsize | |
93 #define luaL_setfuncs dll_luaL_setfuncs | |
94 #define luaL_loadfilex dll_luaL_loadfilex | |
95 #define luaL_loadbufferx dll_luaL_loadbufferx | |
96 #define luaL_argerror dll_luaL_argerror | |
97 #endif | |
3459 | 98 #define luaL_checkany dll_luaL_checkany |
2320
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
99 #define luaL_checklstring dll_luaL_checklstring |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
100 #define luaL_checkinteger dll_luaL_checkinteger |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
101 #define luaL_optinteger dll_luaL_optinteger |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
102 #define luaL_checktype dll_luaL_checktype |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
103 #define luaL_error dll_luaL_error |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
104 #define luaL_newstate dll_luaL_newstate |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
105 #define luaL_buffinit dll_luaL_buffinit |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
106 #define luaL_addlstring dll_luaL_addlstring |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
107 #define luaL_pushresult dll_luaL_pushresult |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
108 /* lua */ |
3450 | 109 #if LUA_VERSION_NUM <= 501 |
110 #define lua_tonumber dll_lua_tonumber | |
111 #define lua_tointeger dll_lua_tointeger | |
112 #define lua_call dll_lua_call | |
113 #define lua_pcall dll_lua_pcall | |
114 #else | |
115 #define lua_tonumberx dll_lua_tonumberx | |
116 #define lua_tointegerx dll_lua_tointegerx | |
117 #define lua_callk dll_lua_callk | |
118 #define lua_pcallk dll_lua_pcallk | |
119 #define lua_getglobal dll_lua_getglobal | |
120 #define lua_setglobal dll_lua_setglobal | |
3459 | 121 #endif |
3450 | 122 #define lua_typename dll_lua_typename |
2320
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
123 #define lua_close dll_lua_close |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
124 #define lua_gettop dll_lua_gettop |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
125 #define lua_settop dll_lua_settop |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
126 #define lua_pushvalue dll_lua_pushvalue |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
127 #define lua_replace dll_lua_replace |
3450 | 128 #define lua_remove dll_lua_remove |
2320
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
129 #define lua_isnumber dll_lua_isnumber |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
130 #define lua_isstring dll_lua_isstring |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
131 #define lua_type dll_lua_type |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
132 #define lua_rawequal dll_lua_rawequal |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
133 #define lua_toboolean dll_lua_toboolean |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
134 #define lua_tolstring dll_lua_tolstring |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
135 #define lua_touserdata dll_lua_touserdata |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
136 #define lua_pushnil dll_lua_pushnil |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
137 #define lua_pushnumber dll_lua_pushnumber |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
138 #define lua_pushinteger dll_lua_pushinteger |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
139 #define lua_pushlstring dll_lua_pushlstring |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
140 #define lua_pushstring dll_lua_pushstring |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
141 #define lua_pushfstring dll_lua_pushfstring |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
142 #define lua_pushcclosure dll_lua_pushcclosure |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
143 #define lua_pushboolean dll_lua_pushboolean |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
144 #define lua_pushlightuserdata dll_lua_pushlightuserdata |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
145 #define lua_getfield dll_lua_getfield |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
146 #define lua_rawget dll_lua_rawget |
3450 | 147 #define lua_rawgeti dll_lua_rawgeti |
2320
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
148 #define lua_createtable dll_lua_createtable |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
149 #define lua_newuserdata dll_lua_newuserdata |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
150 #define lua_getmetatable dll_lua_getmetatable |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
151 #define lua_setfield dll_lua_setfield |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
152 #define lua_rawset dll_lua_rawset |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
153 #define lua_rawseti dll_lua_rawseti |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
154 #define lua_setmetatable dll_lua_setmetatable |
3459 | 155 #define lua_next dll_lua_next |
2320
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
156 /* libs */ |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
157 #define luaopen_base dll_luaopen_base |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
158 #define luaopen_table dll_luaopen_table |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
159 #define luaopen_string dll_luaopen_string |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
160 #define luaopen_math dll_luaopen_math |
2431
7b764999f9b9
Update for Lua interface. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
2373
diff
changeset
|
161 #define luaopen_io dll_luaopen_io |
2320
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
162 #define luaopen_os dll_luaopen_os |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
163 #define luaopen_package dll_luaopen_package |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
164 #define luaopen_debug dll_luaopen_debug |
2431
7b764999f9b9
Update for Lua interface. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
2373
diff
changeset
|
165 #define luaL_openlibs dll_luaL_openlibs |
2320
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
166 |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
167 /* lauxlib */ |
3450 | 168 #if LUA_VERSION_NUM <= 501 |
2320
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
169 void (*dll_luaL_register) (lua_State *L, const char *libname, const luaL_Reg *l); |
3450 | 170 char *(*dll_luaL_prepbuffer) (luaL_Buffer *B); |
171 void (*dll_luaL_openlib) (lua_State *L, const char *libname, const luaL_Reg *l, int nup); | |
2320
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
172 int (*dll_luaL_typerror) (lua_State *L, int narg, const char *tname); |
3450 | 173 int (*dll_luaL_loadfile) (lua_State *L, const char *filename); |
174 int (*dll_luaL_loadbuffer) (lua_State *L, const char *buff, size_t sz, const char *name); | |
175 #else | |
176 char *(*dll_luaL_prepbuffsize) (luaL_Buffer *B, size_t sz); | |
177 void (*dll_luaL_setfuncs) (lua_State *L, const luaL_Reg *l, int nup); | |
178 int (*dll_luaL_loadfilex) (lua_State *L, const char *filename, const char *mode); | |
179 int (*dll_luaL_loadbufferx) (lua_State *L, const char *buff, size_t sz, const char *name, const char *mode); | |
180 int (*dll_luaL_argerror) (lua_State *L, int numarg, const char *extramsg); | |
181 #endif | |
3459 | 182 void (*dll_luaL_checkany) (lua_State *L, int narg); |
2320
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
183 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
|
184 lua_Integer (*dll_luaL_checkinteger) (lua_State *L, int numArg); |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
185 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
|
186 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
|
187 int (*dll_luaL_error) (lua_State *L, const char *fmt, ...); |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
188 lua_State *(*dll_luaL_newstate) (void); |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
189 void (*dll_luaL_buffinit) (lua_State *L, luaL_Buffer *B); |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
190 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
|
191 void (*dll_luaL_pushresult) (luaL_Buffer *B); |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
192 /* lua */ |
3450 | 193 #if LUA_VERSION_NUM <= 501 |
194 lua_Number (*dll_lua_tonumber) (lua_State *L, int idx); | |
195 lua_Integer (*dll_lua_tointeger) (lua_State *L, int idx); | |
196 void (*dll_lua_call) (lua_State *L, int nargs, int nresults); | |
197 int (*dll_lua_pcall) (lua_State *L, int nargs, int nresults, int errfunc); | |
198 #else | |
199 lua_Number (*dll_lua_tonumberx) (lua_State *L, int idx, int *isnum); | |
200 lua_Integer (*dll_lua_tointegerx) (lua_State *L, int idx, int *isnum); | |
201 void (*dll_lua_callk) (lua_State *L, int nargs, int nresults, int ctx, | |
3618 | 202 lua_CFunction k); |
3450 | 203 int (*dll_lua_pcallk) (lua_State *L, int nargs, int nresults, int errfunc, |
3618 | 204 int ctx, lua_CFunction k); |
3450 | 205 void (*dll_lua_getglobal) (lua_State *L, const char *var); |
206 void (*dll_lua_setglobal) (lua_State *L, const char *var); | |
3459 | 207 #endif |
3450 | 208 const char *(*dll_lua_typename) (lua_State *L, int tp); |
2320
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
209 void (*dll_lua_close) (lua_State *L); |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
210 int (*dll_lua_gettop) (lua_State *L); |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
211 void (*dll_lua_settop) (lua_State *L, int idx); |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
212 void (*dll_lua_pushvalue) (lua_State *L, int idx); |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
213 void (*dll_lua_replace) (lua_State *L, int idx); |
3450 | 214 void (*dll_lua_remove) (lua_State *L, int idx); |
2320
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
215 int (*dll_lua_isnumber) (lua_State *L, int idx); |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
216 int (*dll_lua_isstring) (lua_State *L, int idx); |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
217 int (*dll_lua_type) (lua_State *L, int idx); |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
218 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
|
219 int (*dll_lua_toboolean) (lua_State *L, int idx); |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
220 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
|
221 void *(*dll_lua_touserdata) (lua_State *L, int idx); |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
222 void (*dll_lua_pushnil) (lua_State *L); |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
223 void (*dll_lua_pushnumber) (lua_State *L, lua_Number n); |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
224 void (*dll_lua_pushinteger) (lua_State *L, lua_Integer n); |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
225 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
|
226 void (*dll_lua_pushstring) (lua_State *L, const char *s); |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
227 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
|
228 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
|
229 void (*dll_lua_pushboolean) (lua_State *L, int b); |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
230 void (*dll_lua_pushlightuserdata) (lua_State *L, void *p); |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
231 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
|
232 void (*dll_lua_rawget) (lua_State *L, int idx); |
3450 | 233 void (*dll_lua_rawgeti) (lua_State *L, int idx, int n); |
2320
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
234 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
|
235 void *(*dll_lua_newuserdata) (lua_State *L, size_t sz); |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
236 int (*dll_lua_getmetatable) (lua_State *L, int objindex); |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
237 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
|
238 void (*dll_lua_rawset) (lua_State *L, int idx); |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
239 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
|
240 int (*dll_lua_setmetatable) (lua_State *L, int objindex); |
3459 | 241 int (*dll_lua_next) (lua_State *L, int idx); |
2320
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
242 /* libs */ |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
243 int (*dll_luaopen_base) (lua_State *L); |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
244 int (*dll_luaopen_table) (lua_State *L); |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
245 int (*dll_luaopen_string) (lua_State *L); |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
246 int (*dll_luaopen_math) (lua_State *L); |
2431
7b764999f9b9
Update for Lua interface. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
2373
diff
changeset
|
247 int (*dll_luaopen_io) (lua_State *L); |
2320
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
248 int (*dll_luaopen_os) (lua_State *L); |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
249 int (*dll_luaopen_package) (lua_State *L); |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
250 int (*dll_luaopen_debug) (lua_State *L); |
2431
7b764999f9b9
Update for Lua interface. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
2373
diff
changeset
|
251 void (*dll_luaL_openlibs) (lua_State *L); |
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 typedef void **luaV_function; |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
254 typedef struct { |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
255 const char *name; |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
256 luaV_function func; |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
257 } luaV_Reg; |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
258 |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
259 static const luaV_Reg luaV_dll[] = { |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
260 /* lauxlib */ |
3450 | 261 #if LUA_VERSION_NUM <= 501 |
2320
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
262 {"luaL_register", (luaV_function) &dll_luaL_register}, |
3450 | 263 {"luaL_prepbuffer", (luaV_function) &dll_luaL_prepbuffer}, |
264 {"luaL_openlib", (luaV_function) &dll_luaL_openlib}, | |
2320
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
265 {"luaL_typerror", (luaV_function) &dll_luaL_typerror}, |
3450 | 266 {"luaL_loadfile", (luaV_function) &dll_luaL_loadfile}, |
267 {"luaL_loadbuffer", (luaV_function) &dll_luaL_loadbuffer}, | |
268 #else | |
269 {"luaL_prepbuffsize", (luaV_function) &dll_luaL_prepbuffsize}, | |
270 {"luaL_setfuncs", (luaV_function) &dll_luaL_setfuncs}, | |
271 {"luaL_loadfilex", (luaV_function) &dll_luaL_loadfilex}, | |
272 {"luaL_loadbufferx", (luaV_function) &dll_luaL_loadbufferx}, | |
273 {"luaL_argerror", (luaV_function) &dll_luaL_argerror}, | |
274 #endif | |
3459 | 275 {"luaL_checkany", (luaV_function) &dll_luaL_checkany}, |
2320
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
276 {"luaL_checklstring", (luaV_function) &dll_luaL_checklstring}, |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
277 {"luaL_checkinteger", (luaV_function) &dll_luaL_checkinteger}, |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
278 {"luaL_optinteger", (luaV_function) &dll_luaL_optinteger}, |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
279 {"luaL_checktype", (luaV_function) &dll_luaL_checktype}, |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
280 {"luaL_error", (luaV_function) &dll_luaL_error}, |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
281 {"luaL_newstate", (luaV_function) &dll_luaL_newstate}, |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
282 {"luaL_buffinit", (luaV_function) &dll_luaL_buffinit}, |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
283 {"luaL_addlstring", (luaV_function) &dll_luaL_addlstring}, |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
284 {"luaL_pushresult", (luaV_function) &dll_luaL_pushresult}, |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
285 /* lua */ |
3450 | 286 #if LUA_VERSION_NUM <= 501 |
287 {"lua_tonumber", (luaV_function) &dll_lua_tonumber}, | |
288 {"lua_tointeger", (luaV_function) &dll_lua_tointeger}, | |
289 {"lua_call", (luaV_function) &dll_lua_call}, | |
290 {"lua_pcall", (luaV_function) &dll_lua_pcall}, | |
291 #else | |
292 {"lua_tonumberx", (luaV_function) &dll_lua_tonumberx}, | |
293 {"lua_tointegerx", (luaV_function) &dll_lua_tointegerx}, | |
294 {"lua_callk", (luaV_function) &dll_lua_callk}, | |
295 {"lua_pcallk", (luaV_function) &dll_lua_pcallk}, | |
296 {"lua_getglobal", (luaV_function) &dll_lua_getglobal}, | |
297 {"lua_setglobal", (luaV_function) &dll_lua_setglobal}, | |
3459 | 298 #endif |
3450 | 299 {"lua_typename", (luaV_function) &dll_lua_typename}, |
2320
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
300 {"lua_close", (luaV_function) &dll_lua_close}, |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
301 {"lua_gettop", (luaV_function) &dll_lua_gettop}, |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
302 {"lua_settop", (luaV_function) &dll_lua_settop}, |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
303 {"lua_pushvalue", (luaV_function) &dll_lua_pushvalue}, |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
304 {"lua_replace", (luaV_function) &dll_lua_replace}, |
3450 | 305 {"lua_remove", (luaV_function) &dll_lua_remove}, |
2320
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
306 {"lua_isnumber", (luaV_function) &dll_lua_isnumber}, |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
307 {"lua_isstring", (luaV_function) &dll_lua_isstring}, |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
308 {"lua_type", (luaV_function) &dll_lua_type}, |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
309 {"lua_rawequal", (luaV_function) &dll_lua_rawequal}, |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
310 {"lua_toboolean", (luaV_function) &dll_lua_toboolean}, |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
311 {"lua_tolstring", (luaV_function) &dll_lua_tolstring}, |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
312 {"lua_touserdata", (luaV_function) &dll_lua_touserdata}, |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
313 {"lua_pushnil", (luaV_function) &dll_lua_pushnil}, |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
314 {"lua_pushnumber", (luaV_function) &dll_lua_pushnumber}, |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
315 {"lua_pushinteger", (luaV_function) &dll_lua_pushinteger}, |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
316 {"lua_pushlstring", (luaV_function) &dll_lua_pushlstring}, |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
317 {"lua_pushstring", (luaV_function) &dll_lua_pushstring}, |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
318 {"lua_pushfstring", (luaV_function) &dll_lua_pushfstring}, |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
319 {"lua_pushcclosure", (luaV_function) &dll_lua_pushcclosure}, |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
320 {"lua_pushboolean", (luaV_function) &dll_lua_pushboolean}, |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
321 {"lua_pushlightuserdata", (luaV_function) &dll_lua_pushlightuserdata}, |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
322 {"lua_getfield", (luaV_function) &dll_lua_getfield}, |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
323 {"lua_rawget", (luaV_function) &dll_lua_rawget}, |
3450 | 324 {"lua_rawgeti", (luaV_function) &dll_lua_rawgeti}, |
2320
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
325 {"lua_createtable", (luaV_function) &dll_lua_createtable}, |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
326 {"lua_newuserdata", (luaV_function) &dll_lua_newuserdata}, |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
327 {"lua_getmetatable", (luaV_function) &dll_lua_getmetatable}, |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
328 {"lua_setfield", (luaV_function) &dll_lua_setfield}, |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
329 {"lua_rawset", (luaV_function) &dll_lua_rawset}, |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
330 {"lua_rawseti", (luaV_function) &dll_lua_rawseti}, |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
331 {"lua_setmetatable", (luaV_function) &dll_lua_setmetatable}, |
3459 | 332 {"lua_next", (luaV_function) &dll_lua_next}, |
2320
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
333 /* libs */ |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
334 {"luaopen_base", (luaV_function) &dll_luaopen_base}, |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
335 {"luaopen_table", (luaV_function) &dll_luaopen_table}, |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
336 {"luaopen_string", (luaV_function) &dll_luaopen_string}, |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
337 {"luaopen_math", (luaV_function) &dll_luaopen_math}, |
2431
7b764999f9b9
Update for Lua interface. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
2373
diff
changeset
|
338 {"luaopen_io", (luaV_function) &dll_luaopen_io}, |
2320
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
339 {"luaopen_os", (luaV_function) &dll_luaopen_os}, |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
340 {"luaopen_package", (luaV_function) &dll_luaopen_package}, |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
341 {"luaopen_debug", (luaV_function) &dll_luaopen_debug}, |
2431
7b764999f9b9
Update for Lua interface. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
2373
diff
changeset
|
342 {"luaL_openlibs", (luaV_function) &dll_luaL_openlibs}, |
2320
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
343 {NULL, NULL} |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
344 }; |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
345 |
2373
f149bb1cf5be
Make it possible to load Lua dynamically on Unix. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
2331
diff
changeset
|
346 static HANDLE hinstLua = NULL; |
2320
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
347 |
2330 | 348 static void |
349 end_dynamic_lua(void) | |
350 { | |
351 if (hinstLua) | |
352 { | |
2373
f149bb1cf5be
Make it possible to load Lua dynamically on Unix. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
2331
diff
changeset
|
353 close_dll(hinstLua); |
2330 | 354 hinstLua = 0; |
2320
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
355 } |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
356 } |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
357 |
2330 | 358 static int |
359 lua_link_init(char *libname, int verbose) | |
360 { | |
2320
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
361 const luaV_Reg *reg; |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
362 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
|
363 hinstLua = load_dll(libname); |
2330 | 364 if (!hinstLua) |
365 { | |
366 if (verbose) | |
367 EMSG2(_(e_loadlib), libname); | |
368 return FAIL; | |
2320
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
369 } |
2330 | 370 for (reg = luaV_dll; reg->func; reg++) |
371 { | |
2373
f149bb1cf5be
Make it possible to load Lua dynamically on Unix. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
2331
diff
changeset
|
372 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
|
373 { |
f149bb1cf5be
Make it possible to load Lua dynamically on Unix. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
2331
diff
changeset
|
374 close_dll(hinstLua); |
2330 | 375 hinstLua = 0; |
376 if (verbose) | |
377 EMSG2(_(e_loadfunc), reg->name); | |
378 return FAIL; | |
379 } | |
2320
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
380 } |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
381 return OK; |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
382 } |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
383 |
2330 | 384 int |
385 lua_enabled(int verbose) | |
386 { | |
2320
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
387 return lua_link_init(DYNAMIC_LUA_DLL, verbose) == OK; |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
388 } |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
389 |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
390 #endif /* DYNAMIC_LUA */ |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
391 |
3450 | 392 #if LUA_VERSION_NUM > 501 |
393 static int | |
394 luaL_typeerror (lua_State *L, int narg, const char *tname) | |
395 { | |
396 const char *msg = lua_pushfstring(L, "%s expected, got %s", | |
3618 | 397 tname, luaL_typename(L, narg)); |
3450 | 398 return luaL_argerror(L, narg, msg); |
399 } | |
400 #endif | |
401 | |
2320
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
402 |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
403 /* ======= Internal ======= */ |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
404 |
2330 | 405 static void |
406 luaV_newmetatable(lua_State *L, const char *tname) | |
407 { | |
2320
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
408 lua_newtable(L); |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
409 lua_pushlightuserdata(L, (void *) tname); |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
410 lua_pushvalue(L, -2); |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
411 lua_rawset(L, LUA_REGISTRYINDEX); |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
412 } |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
413 |
2330 | 414 static void * |
415 luaV_toudata(lua_State *L, int ud, const char *tname) | |
416 { | |
2320
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
417 void *p = lua_touserdata(L, ud); |
2330 | 418 |
419 if (p != NULL) /* value is userdata? */ | |
420 { | |
421 if (lua_getmetatable(L, ud)) /* does it have a metatable? */ | |
422 { | |
423 luaV_getfield(L, tname); /* get metatable */ | |
424 if (lua_rawequal(L, -1, -2)) /* MTs match? */ | |
425 { | |
426 lua_pop(L, 2); /* MTs */ | |
427 return p; | |
428 } | |
429 } | |
2320
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
430 } |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
431 return NULL; |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
432 } |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
433 |
2330 | 434 static void * |
3450 | 435 luaV_checkcache(lua_State *L, void *p) |
436 { | |
437 luaV_getudata(L, p); | |
438 if (lua_isnil(L, -1)) luaL_error(L, "invalid object"); | |
439 lua_pop(L, 1); | |
440 return p; | |
441 } | |
442 | |
443 #define luaV_unbox(L,luatyp,ud) (*((luatyp *) lua_touserdata((L),(ud)))) | |
444 | |
445 #define luaV_checkvalid(L,luatyp,ud) \ | |
446 luaV_checkcache((L), (void *) luaV_unbox((L),luatyp,(ud))) | |
447 | |
448 static void * | |
2330 | 449 luaV_checkudata(lua_State *L, int ud, const char *tname) |
450 { | |
2320
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
451 void *p = luaV_toudata(L, ud, tname); |
3450 | 452 if (p == NULL) luaL_typeerror(L, ud, tname); |
2320
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
453 return p; |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
454 } |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
455 |
2330 | 456 static void |
457 luaV_pushtypval(lua_State *L, typval_T *tv) | |
458 { | |
3450 | 459 if (tv == NULL) |
460 { | |
461 lua_pushnil(L); | |
462 return; | |
463 } | |
2330 | 464 switch (tv->v_type) |
465 { | |
466 case VAR_STRING: | |
3865 | 467 lua_pushstring(L, tv->vval.v_string == NULL |
468 ? "" : (char *)tv->vval.v_string); | |
2330 | 469 break; |
470 case VAR_NUMBER: | |
471 lua_pushinteger(L, (int) tv->vval.v_number); | |
472 break; | |
2320
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
473 #ifdef FEAT_FLOAT |
2330 | 474 case VAR_FLOAT: |
475 lua_pushnumber(L, (lua_Number) tv->vval.v_float); | |
476 break; | |
2320
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
477 #endif |
3450 | 478 case VAR_LIST: |
479 luaV_pushlist(L, tv->vval.v_list); | |
480 break; | |
481 case VAR_DICT: | |
482 luaV_pushdict(L, tv->vval.v_dict); | |
483 break; | |
484 default: | |
485 lua_pushnil(L); | |
486 } | |
487 } | |
2330 | 488 |
3450 | 489 /* converts lua value at 'pos' to typval 'tv' */ |
490 static void | |
491 luaV_totypval (lua_State *L, int pos, typval_T *tv) | |
492 { | |
493 switch(lua_type(L, pos)) { | |
494 case LUA_TBOOLEAN: | |
495 tv->v_type = VAR_NUMBER; | |
496 tv->vval.v_number = (varnumber_T) lua_toboolean(L, pos); | |
497 break; | |
498 case LUA_TSTRING: | |
499 tv->v_type = VAR_STRING; | |
500 tv->vval.v_string = vim_strsave((char_u *) lua_tostring(L, pos)); | |
2330 | 501 break; |
3450 | 502 case LUA_TNUMBER: |
503 #ifdef FEAT_FLOAT | |
504 tv->v_type = VAR_FLOAT; | |
505 tv->vval.v_float = (float_T) lua_tonumber(L, pos); | |
506 #else | |
507 tv->v_type = VAR_NUMBER; | |
508 tv->vval.v_number = (varnumber_T) lua_tointeger(L, pos); | |
509 #endif | |
510 break; | |
511 case LUA_TUSERDATA: { | |
512 void *p = lua_touserdata(L, pos); | |
513 if (lua_getmetatable(L, pos)) /* has metatable? */ | |
2330 | 514 { |
3450 | 515 /* check list */ |
516 luaV_getfield(L, LUAVIM_LIST); | |
517 if (lua_rawequal(L, -1, -2)) | |
2373
f149bb1cf5be
Make it possible to load Lua dynamically on Unix. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
2331
diff
changeset
|
518 { |
3450 | 519 tv->v_type = VAR_LIST; |
520 tv->vval.v_list = *((luaV_List *) p); | |
521 ++tv->vval.v_list->lv_refcount; | |
522 lua_pop(L, 2); /* MTs */ | |
523 return; | |
2330 | 524 } |
3450 | 525 /* check dict */ |
526 luaV_getfield(L, LUAVIM_DICT); | |
527 if (lua_rawequal(L, -1, -3)) | |
528 { | |
529 tv->v_type = VAR_DICT; | |
530 tv->vval.v_dict = *((luaV_Dict *) p); | |
531 ++tv->vval.v_dict->dv_refcount; | |
532 lua_pop(L, 3); /* MTs */ | |
533 return; | |
534 } | |
535 lua_pop(L, 3); /* MTs */ | |
2330 | 536 } |
537 break; | |
538 } | |
539 default: | |
3450 | 540 tv->v_type = VAR_NUMBER; |
541 tv->vval.v_number = 0; | |
2320
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
542 } |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
543 } |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
544 |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
545 /* 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
|
546 * \n with \0 otherwise */ |
2330 | 547 static void |
548 luaV_addlstring(luaL_Buffer *b, const char *s, size_t l, int toline) | |
549 { | |
550 while (l--) | |
551 { | |
552 if (*s == '\0' && toline) | |
553 luaL_addchar(b, '\n'); | |
554 else if (*s == '\n' && !toline) | |
555 luaL_addchar(b, '\0'); | |
556 else | |
557 luaL_addchar(b, *s); | |
558 s++; | |
2320
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
559 } |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
560 } |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
561 |
2330 | 562 static void |
563 luaV_pushline(lua_State *L, buf_T *buf, linenr_T n) | |
564 { | |
2320
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
565 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
|
566 luaL_Buffer b; |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
567 luaL_buffinit(L, &b); |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
568 luaV_addlstring(&b, s, strlen(s), 0); |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
569 luaL_pushresult(&b); |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
570 } |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
571 |
2330 | 572 static char_u * |
573 luaV_toline(lua_State *L, int pos) | |
574 { | |
2320
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
575 size_t l; |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
576 const char *s = lua_tolstring(L, pos, &l); |
2330 | 577 |
2320
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
578 luaL_Buffer b; |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
579 luaL_buffinit(L, &b); |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
580 luaV_addlstring(&b, s, l, 1); |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
581 luaL_pushresult(&b); |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
582 return (char_u *) lua_tostring(L, -1); |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
583 } |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
584 |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
585 /* 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
|
586 * s separated by newlines */ |
2330 | 587 static void |
588 luaV_msgfunc(lua_State *L, msgfunc_T mf) | |
589 { | |
2320
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
590 luaL_Buffer b; |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
591 size_t l; |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
592 const char *p, *s = lua_tolstring(L, -1, &l); |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
593 luaL_buffinit(L, &b); |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
594 luaV_addlstring(&b, s, l, 0); |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
595 luaL_pushresult(&b); |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
596 /* break string */ |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
597 p = s = lua_tolstring(L, -1, &l); |
2330 | 598 while (l--) |
599 { | |
600 if (*p++ == '\0') /* break? */ | |
601 { | |
602 mf((char_u *) s); | |
603 s = p; | |
604 } | |
2320
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
605 } |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
606 mf((char_u *) s); |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
607 lua_pop(L, 2); /* original and modified strings */ |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
608 } |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
609 |
3450 | 610 #define luaV_newtype(typ,tname,luatyp,luatname) \ |
611 static luatyp * \ | |
612 luaV_new##tname (lua_State *L, typ *obj) \ | |
613 { \ | |
614 luatyp *o = (luatyp *) lua_newuserdata(L, sizeof(luatyp)); \ | |
615 *o = obj; \ | |
616 luaV_setudata(L, obj); /* cache[obj] = udata */ \ | |
617 luaV_getfield(L, luatname); \ | |
618 lua_setmetatable(L, -2); \ | |
619 return o; \ | |
620 } | |
2320
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
621 |
3450 | 622 #define luaV_pushtype(typ,tname,luatyp) \ |
623 static luatyp * \ | |
624 luaV_push##tname (lua_State *L, typ *obj) \ | |
625 { \ | |
626 luatyp *o = NULL; \ | |
627 if (obj == NULL) \ | |
628 lua_pushnil(L); \ | |
629 else { \ | |
630 luaV_getudata(L, obj); \ | |
631 if (lua_isnil(L, -1)) /* not interned? */ \ | |
632 { \ | |
633 lua_pop(L, 1); \ | |
634 o = luaV_new##tname(L, obj); \ | |
635 } \ | |
636 else \ | |
637 o = (luatyp *) lua_touserdata(L, -1); \ | |
638 } \ | |
639 return o; \ | |
640 } | |
641 | |
642 #define luaV_type_tostring(tname,luatname) \ | |
643 static int \ | |
644 luaV_##tname##_tostring (lua_State *L) \ | |
645 { \ | |
646 lua_pushfstring(L, "%s: %p", luatname, lua_touserdata(L, 1)); \ | |
647 return 1; \ | |
648 } | |
649 | |
650 /* ======= List type ======= */ | |
651 | |
652 static luaV_List * | |
653 luaV_newlist (lua_State *L, list_T *lis) | |
654 { | |
655 luaV_List *l = (luaV_List *) lua_newuserdata(L, sizeof(luaV_List)); | |
656 *l = lis; | |
657 lis->lv_refcount++; /* reference in Lua */ | |
658 luaV_setudata(L, lis); /* cache[lis] = udata */ | |
659 luaV_getfield(L, LUAVIM_LIST); | |
660 lua_setmetatable(L, -2); | |
661 return l; | |
662 } | |
663 | |
664 luaV_pushtype(list_T, list, luaV_List) | |
665 luaV_type_tostring(list, LUAVIM_LIST) | |
2320
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
666 |
2330 | 667 static int |
3450 | 668 luaV_list_gc (lua_State *L) |
669 { | |
670 list_unref(luaV_unbox(L, luaV_List, 1)); | |
671 return 0; | |
672 } | |
673 | |
674 static int | |
675 luaV_list_len (lua_State *L) | |
2330 | 676 { |
3450 | 677 list_T *l = luaV_unbox(L, luaV_List, 1); |
678 lua_pushinteger(L, (l == NULL) ? 0 : (int) l->lv_len); | |
679 return 1; | |
680 } | |
681 | |
682 static int | |
683 luaV_list_iter (lua_State *L) | |
684 { | |
685 listitem_T *li = (listitem_T *) lua_touserdata(L, lua_upvalueindex(2)); | |
686 if (li == NULL) return 0; | |
687 luaV_pushtypval(L, &li->li_tv); | |
688 lua_pushlightuserdata(L, (void *) li->li_next); | |
689 lua_replace(L, lua_upvalueindex(2)); | |
690 return 1; | |
691 } | |
692 | |
693 static int | |
694 luaV_list_call (lua_State *L) | |
695 { | |
696 list_T *l = luaV_unbox(L, luaV_List, 1); | |
697 lua_pushvalue(L, lua_upvalueindex(1)); /* pass cache table along */ | |
698 lua_pushlightuserdata(L, (void *) l->lv_first); | |
699 lua_pushcclosure(L, luaV_list_iter, 2); | |
2320
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 |
2330 | 703 static int |
3450 | 704 luaV_list_index (lua_State *L) |
705 { | |
706 list_T *l = luaV_unbox(L, luaV_List, 1); | |
707 if (lua_isnumber(L, 2)) /* list item? */ | |
708 { | |
709 listitem_T *li = list_find(l, (long) luaL_checkinteger(L, 2)); | |
710 if (li == NULL) | |
711 lua_pushnil(L); | |
712 else | |
713 luaV_pushtypval(L, &li->li_tv); | |
714 } | |
715 else if (lua_isstring(L, 2)) /* method? */ | |
716 { | |
717 const char *s = lua_tostring(L, 2); | |
718 if (strncmp(s, "add", 3) == 0 | |
719 || strncmp(s, "insert", 6) == 0 | |
720 || strncmp(s, "extend", 6) == 0) | |
721 { | |
722 lua_getmetatable(L, 1); | |
723 lua_getfield(L, -1, s); | |
724 } | |
725 else | |
726 lua_pushnil(L); | |
727 } | |
728 else | |
729 lua_pushnil(L); | |
730 return 1; | |
731 } | |
732 | |
733 static int | |
734 luaV_list_newindex (lua_State *L) | |
735 { | |
736 list_T *l = luaV_unbox(L, luaV_List, 1); | |
737 long n = (long) luaL_checkinteger(L, 2); | |
738 listitem_T *li; | |
739 if (l->lv_lock) | |
740 luaL_error(L, "list is locked"); | |
741 li = list_find(l, n); | |
742 if (li == NULL) return 0; | |
743 if (lua_isnil(L, 3)) /* remove? */ | |
744 { | |
3618 | 745 list_remove(l, li, li); |
3450 | 746 clear_tv(&li->li_tv); |
747 vim_free(li); | |
748 } | |
749 else | |
750 { | |
751 typval_T v; | |
752 luaV_totypval(L, 3, &v); | |
753 clear_tv(&li->li_tv); | |
754 copy_tv(&v, &li->li_tv); | |
755 } | |
756 return 0; | |
757 } | |
758 | |
759 static int | |
760 luaV_list_add (lua_State *L) | |
761 { | |
762 luaV_List *lis = luaV_checkudata(L, 1, LUAVIM_LIST); | |
763 list_T *l = (list_T *) luaV_checkcache(L, (void *) *lis); | |
764 listitem_T *li; | |
765 if (l->lv_lock) | |
766 luaL_error(L, "list is locked"); | |
767 li = listitem_alloc(); | |
768 if (li != NULL) | |
769 { | |
770 typval_T v; | |
771 lua_settop(L, 2); | |
772 luaV_totypval(L, 2, &v); | |
3618 | 773 list_append_tv(l, &v); |
3450 | 774 } |
775 lua_settop(L, 1); | |
776 return 1; | |
777 } | |
778 | |
779 static int | |
780 luaV_list_insert (lua_State *L) | |
781 { | |
782 luaV_List *lis = luaV_checkudata(L, 1, LUAVIM_LIST); | |
783 list_T *l = (list_T *) luaV_checkcache(L, (void *) *lis); | |
784 long pos = luaL_optlong(L, 3, 0); | |
785 listitem_T *li = NULL; | |
786 typval_T v; | |
787 if (l->lv_lock) | |
788 luaL_error(L, "list is locked"); | |
789 if (pos < l->lv_len) | |
790 { | |
791 li = list_find(l, pos); | |
792 if (li == NULL) | |
793 luaL_error(L, "invalid position"); | |
794 } | |
795 lua_settop(L, 2); | |
796 luaV_totypval(L, 2, &v); | |
797 list_insert_tv(l, &v, li); | |
798 lua_settop(L, 1); | |
799 return 1; | |
800 } | |
801 | |
802 static const luaL_Reg luaV_List_mt[] = { | |
803 {"__tostring", luaV_list_tostring}, | |
804 {"__gc", luaV_list_gc}, | |
805 {"__len", luaV_list_len}, | |
806 {"__call", luaV_list_call}, | |
807 {"__index", luaV_list_index}, | |
808 {"__newindex", luaV_list_newindex}, | |
809 {"add", luaV_list_add}, | |
810 {"insert", luaV_list_insert}, | |
811 {NULL, NULL} | |
812 }; | |
813 | |
814 | |
815 /* ======= Dict type ======= */ | |
816 | |
817 static luaV_Dict * | |
818 luaV_newdict (lua_State *L, dict_T *dic) | |
819 { | |
820 luaV_Dict *d = (luaV_Dict *) lua_newuserdata(L, sizeof(luaV_Dict)); | |
821 *d = dic; | |
822 dic->dv_refcount++; /* reference in Lua */ | |
823 luaV_setudata(L, dic); /* cache[dic] = udata */ | |
824 luaV_getfield(L, LUAVIM_DICT); | |
825 lua_setmetatable(L, -2); | |
826 return d; | |
827 } | |
828 | |
829 luaV_pushtype(dict_T, dict, luaV_Dict) | |
830 luaV_type_tostring(dict, LUAVIM_DICT) | |
831 | |
832 static int | |
833 luaV_dict_gc (lua_State *L) | |
834 { | |
835 dict_unref(luaV_unbox(L, luaV_Dict, 1)); | |
836 return 0; | |
837 } | |
838 | |
839 static int | |
840 luaV_dict_len (lua_State *L) | |
841 { | |
842 dict_T *d = luaV_unbox(L, luaV_Dict, 1); | |
843 lua_pushinteger(L, (d == NULL) ? 0 : (int) d->dv_hashtab.ht_used); | |
844 return 1; | |
845 } | |
846 | |
847 static int | |
4135 | 848 luaV_dict_iter (lua_State *L UNUSED) |
3450 | 849 { |
4135 | 850 #ifdef FEAT_EVAL |
3450 | 851 hashitem_T *hi = (hashitem_T *) lua_touserdata(L, lua_upvalueindex(2)); |
852 int n = lua_tointeger(L, lua_upvalueindex(3)); | |
853 dictitem_T *di; | |
854 if (n <= 0) return 0; | |
855 while (HASHITEM_EMPTY(hi)) hi++; | |
856 di = dict_lookup(hi); | |
857 lua_pushstring(L, (char *) hi->hi_key); | |
858 luaV_pushtypval(L, &di->di_tv); | |
859 lua_pushlightuserdata(L, (void *) (hi + 1)); | |
860 lua_replace(L, lua_upvalueindex(2)); | |
861 lua_pushinteger(L, n - 1); | |
862 lua_replace(L, lua_upvalueindex(3)); | |
863 return 2; | |
4135 | 864 #else |
865 return 0; | |
866 #endif | |
3450 | 867 } |
868 | |
869 static int | |
870 luaV_dict_call (lua_State *L) | |
871 { | |
872 dict_T *d = luaV_unbox(L, luaV_Dict, 1); | |
873 hashtab_T *ht = &d->dv_hashtab; | |
874 lua_pushvalue(L, lua_upvalueindex(1)); /* pass cache table along */ | |
875 lua_pushlightuserdata(L, (void *) ht->ht_array); | |
876 lua_pushinteger(L, ht->ht_used); /* # remaining items */ | |
877 lua_pushcclosure(L, luaV_dict_iter, 3); | |
878 return 1; | |
879 } | |
880 | |
881 static int | |
882 luaV_dict_index (lua_State *L) | |
883 { | |
884 dict_T *d = luaV_unbox(L, luaV_Dict, 1); | |
885 char_u *key = (char_u *) luaL_checkstring(L, 2); | |
886 dictitem_T *di = dict_find(d, key, -1); | |
887 if (di == NULL) | |
888 lua_pushnil(L); | |
889 else | |
890 luaV_pushtypval(L, &di->di_tv); | |
891 return 1; | |
892 } | |
893 | |
894 static int | |
895 luaV_dict_newindex (lua_State *L) | |
896 { | |
897 dict_T *d = luaV_unbox(L, luaV_Dict, 1); | |
898 char_u *key = (char_u *) luaL_checkstring(L, 2); | |
899 dictitem_T *di; | |
900 if (d->dv_lock) | |
901 luaL_error(L, "dict is locked"); | |
902 di = dict_find(d, key, -1); | |
903 if (di == NULL) /* non-existing key? */ | |
904 { | |
905 if (lua_isnil(L, 3)) return 0; | |
906 di = dictitem_alloc(key); | |
907 if (di == NULL) return 0; | |
908 if (dict_add(d, di) == FAIL) | |
909 { | |
910 vim_free(di); | |
911 return 0; | |
912 } | |
913 } | |
914 else | |
915 clear_tv(&di->di_tv); | |
916 if (lua_isnil(L, 3)) /* remove? */ | |
917 { | |
918 hashitem_T *hi = hash_find(&d->dv_hashtab, di->di_key); | |
919 hash_remove(&d->dv_hashtab, hi); | |
920 dictitem_free(di); | |
921 } | |
922 else { | |
923 typval_T v; | |
924 luaV_totypval(L, 3, &v); | |
925 copy_tv(&v, &di->di_tv); | |
926 } | |
927 return 0; | |
928 } | |
929 | |
930 static const luaL_Reg luaV_Dict_mt[] = { | |
931 {"__tostring", luaV_dict_tostring}, | |
932 {"__gc", luaV_dict_gc}, | |
933 {"__len", luaV_dict_len}, | |
934 {"__call", luaV_dict_call}, | |
935 {"__index", luaV_dict_index}, | |
936 {"__newindex", luaV_dict_newindex}, | |
937 {NULL, NULL} | |
938 }; | |
939 | |
940 | |
941 /* ======= Buffer type ======= */ | |
942 | |
943 luaV_newtype(buf_T, buffer, luaV_Buffer, LUAVIM_BUFFER) | |
944 luaV_pushtype(buf_T, buffer, luaV_Buffer) | |
945 luaV_type_tostring(buffer, LUAVIM_BUFFER) | |
946 | |
947 static int | |
2330 | 948 luaV_buffer_len(lua_State *L) |
949 { | |
3450 | 950 buf_T *b = (buf_T *) luaV_checkvalid(L, luaV_Buffer, 1); |
951 lua_pushinteger(L, b->b_ml.ml_line_count); | |
2320
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
952 return 1; |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
953 } |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
954 |
2330 | 955 static int |
956 luaV_buffer_call(lua_State *L) | |
957 { | |
3450 | 958 buf_T *b = (buf_T *) luaV_checkvalid(L, luaV_Buffer, 1); |
2320
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
959 lua_settop(L, 1); |
3450 | 960 set_curbuf(b, DOBUF_SPLIT); |
2320
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
961 return 1; |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
962 } |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
963 |
2330 | 964 static int |
965 luaV_buffer_index(lua_State *L) | |
966 { | |
3450 | 967 buf_T *b = (buf_T *) luaV_checkvalid(L, luaV_Buffer, 1); |
2320
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
968 linenr_T n = (linenr_T) lua_tointeger(L, 2); |
3450 | 969 if (n > 0 && n <= b->b_ml.ml_line_count) |
970 luaV_pushline(L, b, n); | |
2330 | 971 else if (lua_isstring(L, 2)) |
972 { | |
973 const char *s = lua_tostring(L, 2); | |
974 if (strncmp(s, "name", 4) == 0) | |
3450 | 975 lua_pushstring(L, (char *) b->b_sfname); |
2330 | 976 else if (strncmp(s, "fname", 5) == 0) |
3450 | 977 lua_pushstring(L, (char *) b->b_ffname); |
2330 | 978 else if (strncmp(s, "number", 6) == 0) |
3450 | 979 lua_pushinteger(L, b->b_fnum); |
2330 | 980 /* methods */ |
981 else if (strncmp(s, "insert", 6) == 0 | |
982 || strncmp(s, "next", 4) == 0 | |
983 || strncmp(s, "previous", 8) == 0 | |
984 || strncmp(s, "isvalid", 7) == 0) | |
985 { | |
986 lua_getmetatable(L, 1); | |
987 lua_getfield(L, -1, s); | |
988 } | |
989 else | |
990 lua_pushnil(L); | |
2320
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
991 } |
2330 | 992 else |
993 lua_pushnil(L); | |
2320
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
994 return 1; |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
995 } |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
996 |
2330 | 997 static int |
998 luaV_buffer_newindex(lua_State *L) | |
999 { | |
3450 | 1000 buf_T *b = (buf_T *) luaV_checkvalid(L, luaV_Buffer, 1); |
2320
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
1001 linenr_T n = (linenr_T) luaL_checkinteger(L, 2); |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
1002 #ifdef HAVE_SANDBOX |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
1003 luaV_checksandbox(L); |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
1004 #endif |
3450 | 1005 if (n < 1 || n > b->b_ml.ml_line_count) |
2330 | 1006 luaL_error(L, "invalid line number"); |
1007 if (lua_isnil(L, 3)) /* delete line */ | |
1008 { | |
1009 buf_T *buf = curbuf; | |
3450 | 1010 curbuf = b; |
2330 | 1011 if (u_savedel(n, 1L) == FAIL) |
1012 { | |
1013 curbuf = buf; | |
1014 luaL_error(L, "cannot save undo information"); | |
1015 } | |
1016 else if (ml_delete(n, FALSE) == FAIL) | |
1017 { | |
1018 curbuf = buf; | |
1019 luaL_error(L, "cannot delete line"); | |
1020 } | |
1021 else { | |
1022 deleted_lines_mark(n, 1L); | |
3450 | 1023 if (b == curwin->w_buffer) /* fix cursor in current window? */ |
2330 | 1024 { |
1025 if (curwin->w_cursor.lnum >= n) | |
1026 { | |
1027 if (curwin->w_cursor.lnum > n) | |
1028 { | |
1029 curwin->w_cursor.lnum -= 1; | |
1030 check_cursor_col(); | |
1031 } | |
1032 else check_cursor(); | |
1033 changed_cline_bef_curs(); | |
1034 } | |
1035 invalidate_botline(); | |
1036 } | |
1037 } | |
1038 curbuf = buf; | |
2320
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
1039 } |
2330 | 1040 else if (lua_isstring(L, 3)) /* update line */ |
1041 { | |
1042 buf_T *buf = curbuf; | |
3450 | 1043 curbuf = b; |
2330 | 1044 if (u_savesub(n) == FAIL) |
1045 { | |
1046 curbuf = buf; | |
1047 luaL_error(L, "cannot save undo information"); | |
1048 } | |
1049 else if (ml_replace(n, luaV_toline(L, 3), TRUE) == FAIL) | |
1050 { | |
1051 curbuf = buf; | |
1052 luaL_error(L, "cannot replace line"); | |
1053 } | |
1054 else changed_bytes(n, 0); | |
1055 curbuf = buf; | |
3450 | 1056 if (b == curwin->w_buffer) |
2330 | 1057 check_cursor_col(); |
2320
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
1058 } |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
1059 else |
2330 | 1060 luaL_error(L, "wrong argument to change line"); |
2320
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
1061 return 0; |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
1062 } |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
1063 |
2330 | 1064 static int |
1065 luaV_buffer_insert(lua_State *L) | |
1066 { | |
3450 | 1067 luaV_Buffer *lb = luaV_checkudata(L, 1, LUAVIM_BUFFER); |
1068 buf_T *b = (buf_T *) luaV_checkcache(L, (void *) *lb); | |
1069 linenr_T last = b->b_ml.ml_line_count; | |
2320
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
1070 linenr_T n = (linenr_T) luaL_optinteger(L, 3, last); |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
1071 buf_T *buf; |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
1072 luaL_checktype(L, 2, LUA_TSTRING); |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
1073 #ifdef HAVE_SANDBOX |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
1074 luaV_checksandbox(L); |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
1075 #endif |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
1076 /* fix insertion line */ |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
1077 if (n < 0) n = 0; |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
1078 if (n > last) n = last; |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
1079 /* insert */ |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
1080 buf = curbuf; |
3450 | 1081 curbuf = b; |
2330 | 1082 if (u_save(n, n + 1) == FAIL) |
1083 { | |
1084 curbuf = buf; | |
1085 luaL_error(L, "cannot save undo information"); | |
2320
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
1086 } |
2330 | 1087 else if (ml_append(n, luaV_toline(L, 2), 0, FALSE) == FAIL) |
1088 { | |
1089 curbuf = buf; | |
1090 luaL_error(L, "cannot insert line"); | |
2320
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 else |
2330 | 1093 appended_lines_mark(n, 1L); |
2320
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
1094 curbuf = buf; |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
1095 update_screen(VALID); |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
1096 return 0; |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
1097 } |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
1098 |
2330 | 1099 static int |
1100 luaV_buffer_next(lua_State *L) | |
1101 { | |
2320
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
1102 luaV_Buffer *b = luaV_checkudata(L, 1, LUAVIM_BUFFER); |
3450 | 1103 buf_T *buf = (buf_T *) luaV_checkcache(L, (void *) *b); |
1104 luaV_pushbuffer(L, buf->b_next); | |
2320
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
1105 return 1; |
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 |
2330 | 1108 static int |
1109 luaV_buffer_previous(lua_State *L) | |
1110 { | |
2320
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
1111 luaV_Buffer *b = luaV_checkudata(L, 1, LUAVIM_BUFFER); |
3450 | 1112 buf_T *buf = (buf_T *) luaV_checkcache(L, (void *) *b); |
1113 luaV_pushbuffer(L, buf->b_prev); | |
2320
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
1114 return 1; |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
1115 } |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
1116 |
2330 | 1117 static int |
1118 luaV_buffer_isvalid(lua_State *L) | |
1119 { | |
2320
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
1120 luaV_Buffer *b = luaV_checkudata(L, 1, LUAVIM_BUFFER); |
3450 | 1121 luaV_getudata(L, *b); |
2320
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
1122 lua_pushboolean(L, !lua_isnil(L, -1)); |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
1123 return 1; |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
1124 } |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
1125 |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
1126 static const luaL_Reg luaV_Buffer_mt[] = { |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
1127 {"__tostring", luaV_buffer_tostring}, |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
1128 {"__len", luaV_buffer_len}, |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
1129 {"__call", luaV_buffer_call}, |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
1130 {"__index", luaV_buffer_index}, |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
1131 {"__newindex", luaV_buffer_newindex}, |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
1132 {"insert", luaV_buffer_insert}, |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
1133 {"next", luaV_buffer_next}, |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
1134 {"previous", luaV_buffer_previous}, |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
1135 {"isvalid", luaV_buffer_isvalid}, |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
1136 {NULL, NULL} |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
1137 }; |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
1138 |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
1139 |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
1140 /* ======= Window type ======= */ |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
1141 |
3450 | 1142 luaV_newtype(win_T, window, luaV_Window, LUAVIM_WINDOW) |
1143 luaV_pushtype(win_T, window, luaV_Window) | |
1144 luaV_type_tostring(window, LUAVIM_WINDOW) | |
2320
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
1145 |
2330 | 1146 static int |
1147 luaV_window_call(lua_State *L) | |
1148 { | |
3450 | 1149 win_T *w = (win_T *) luaV_checkvalid(L, luaV_Window, 1); |
2320
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
1150 lua_settop(L, 1); |
3450 | 1151 win_goto(w); |
2320
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
1152 return 1; |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
1153 } |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
1154 |
2330 | 1155 static int |
1156 luaV_window_index(lua_State *L) | |
1157 { | |
3450 | 1158 win_T *w = (win_T *) luaV_checkvalid(L, luaV_Window, 1); |
2320
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
1159 const char *s = luaL_checkstring(L, 2); |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
1160 if (strncmp(s, "buffer", 6) == 0) |
3450 | 1161 luaV_pushbuffer(L, w->w_buffer); |
2320
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
1162 else if (strncmp(s, "line", 4) == 0) |
3450 | 1163 lua_pushinteger(L, w->w_cursor.lnum); |
2320
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
1164 else if (strncmp(s, "col", 3) == 0) |
3450 | 1165 lua_pushinteger(L, w->w_cursor.col + 1); |
2320
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
1166 #ifdef FEAT_VERTSPLIT |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
1167 else if (strncmp(s, "width", 5) == 0) |
3450 | 1168 lua_pushinteger(L, W_WIDTH(w)); |
2320
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
1169 #endif |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
1170 else if (strncmp(s, "height", 6) == 0) |
3450 | 1171 lua_pushinteger(L, w->w_height); |
2320
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
1172 /* methods */ |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
1173 else if (strncmp(s, "next", 4) == 0 |
2330 | 1174 || strncmp(s, "previous", 8) == 0 |
1175 || strncmp(s, "isvalid", 7) == 0) | |
1176 { | |
1177 lua_getmetatable(L, 1); | |
1178 lua_getfield(L, -1, s); | |
2320
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 else |
2330 | 1181 lua_pushnil(L); |
2320
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
1182 return 1; |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
1183 } |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
1184 |
2330 | 1185 static int |
1186 luaV_window_newindex (lua_State *L) | |
1187 { | |
3450 | 1188 win_T *w = (win_T *) luaV_checkvalid(L, luaV_Window, 1); |
2320
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
1189 const char *s = luaL_checkstring(L, 2); |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
1190 int v = luaL_checkinteger(L, 3); |
2330 | 1191 if (strncmp(s, "line", 4) == 0) |
1192 { | |
2320
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
1193 #ifdef HAVE_SANDBOX |
2330 | 1194 luaV_checksandbox(L); |
2320
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
1195 #endif |
3450 | 1196 if (v < 1 || v > w->w_buffer->b_ml.ml_line_count) |
2330 | 1197 luaL_error(L, "line out of range"); |
3450 | 1198 w->w_cursor.lnum = v; |
2330 | 1199 update_screen(VALID); |
2320
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
1200 } |
2330 | 1201 else if (strncmp(s, "col", 3) == 0) |
1202 { | |
2320
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
1203 #ifdef HAVE_SANDBOX |
2330 | 1204 luaV_checksandbox(L); |
2320
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
1205 #endif |
3450 | 1206 w->w_cursor.col = v - 1; |
2330 | 1207 update_screen(VALID); |
2320
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
1208 } |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
1209 #ifdef FEAT_VERTSPLIT |
2330 | 1210 else if (strncmp(s, "width", 5) == 0) |
1211 { | |
1212 win_T *win = curwin; | |
2320
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
1213 #ifdef FEAT_GUI |
2330 | 1214 need_mouse_correct = TRUE; |
2320
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
1215 #endif |
3450 | 1216 curwin = w; |
2330 | 1217 win_setwidth(v); |
1218 curwin = win; | |
2320
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
1219 } |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
1220 #endif |
2330 | 1221 else if (strncmp(s, "height", 6) == 0) |
1222 { | |
1223 win_T *win = curwin; | |
2320
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
1224 #ifdef FEAT_GUI |
2330 | 1225 need_mouse_correct = TRUE; |
2320
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
1226 #endif |
3450 | 1227 curwin = w; |
2330 | 1228 win_setheight(v); |
1229 curwin = win; | |
2320
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
1230 } |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
1231 else |
2330 | 1232 luaL_error(L, "invalid window property: `%s'", s); |
2320
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
1233 return 0; |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
1234 } |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
1235 |
2330 | 1236 static int |
1237 luaV_window_next(lua_State *L) | |
1238 { | |
2320
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
1239 luaV_Window *w = luaV_checkudata(L, 1, LUAVIM_WINDOW); |
3450 | 1240 win_T *win = (win_T *) luaV_checkcache(L, (void *) *w); |
1241 luaV_pushwindow(L, win->w_next); | |
2320
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
1242 return 1; |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
1243 } |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
1244 |
2330 | 1245 static int |
1246 luaV_window_previous(lua_State *L) | |
1247 { | |
2320
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
1248 luaV_Window *w = luaV_checkudata(L, 1, LUAVIM_WINDOW); |
3450 | 1249 win_T *win = (win_T *) luaV_checkcache(L, (void *) *w); |
1250 luaV_pushwindow(L, win->w_prev); | |
2320
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
1251 return 1; |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
1252 } |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
1253 |
2330 | 1254 static int |
1255 luaV_window_isvalid(lua_State *L) | |
1256 { | |
2320
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
1257 luaV_Window *w = luaV_checkudata(L, 1, LUAVIM_WINDOW); |
3450 | 1258 luaV_getudata(L, *w); |
2320
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
1259 lua_pushboolean(L, !lua_isnil(L, -1)); |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
1260 return 1; |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
1261 } |
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 static const luaL_Reg luaV_Window_mt[] = { |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
1264 {"__tostring", luaV_window_tostring}, |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
1265 {"__call", luaV_window_call}, |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
1266 {"__index", luaV_window_index}, |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
1267 {"__newindex", luaV_window_newindex}, |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
1268 {"next", luaV_window_next}, |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
1269 {"previous", luaV_window_previous}, |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
1270 {"isvalid", luaV_window_isvalid}, |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
1271 {NULL, NULL} |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
1272 }; |
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 /* ======= Vim module ======= */ |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
1276 |
2330 | 1277 static int |
1278 luaV_print(lua_State *L) | |
1279 { | |
2320
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
1280 int i, n = lua_gettop(L); /* nargs */ |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
1281 const char *s; |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
1282 size_t l; |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
1283 luaL_Buffer b; |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
1284 luaL_buffinit(L, &b); |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
1285 lua_getglobal(L, "tostring"); |
2330 | 1286 for (i = 1; i <= n; i++) |
1287 { | |
1288 lua_pushvalue(L, -1); /* tostring */ | |
1289 lua_pushvalue(L, i); /* arg */ | |
1290 lua_call(L, 1, 1); | |
1291 s = lua_tolstring(L, -1, &l); | |
1292 if (s == NULL) | |
1293 return luaL_error(L, "cannot convert to string"); | |
1294 if (i > 1) luaL_addchar(&b, ' '); /* use space instead of tab */ | |
1295 luaV_addlstring(&b, s, l, 0); | |
1296 lua_pop(L, 1); | |
2320
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
1297 } |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
1298 luaL_pushresult(&b); |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
1299 luaV_msg(L); |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
1300 return 0; |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
1301 } |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
1302 |
2330 | 1303 static int |
3091 | 1304 luaV_debug(lua_State *L) |
1305 { | |
1306 lua_settop(L, 0); | |
1307 lua_getglobal(L, "vim"); | |
1308 lua_getfield(L, -1, "eval"); | |
1309 lua_remove(L, -2); /* vim.eval at position 1 */ | |
1310 for (;;) | |
1311 { | |
1312 const char *input; | |
1313 size_t l; | |
1314 lua_pushvalue(L, 1); /* vim.eval */ | |
1315 lua_pushliteral(L, "input('lua_debug> ')"); | |
1316 lua_call(L, 1, 1); /* return string */ | |
1317 input = lua_tolstring(L, -1, &l); | |
1318 if (l == 0 || strcmp(input, "cont") == 0) | |
1319 return 0; | |
1320 msg_putchar('\n'); /* avoid outputting on input line */ | |
1321 if (luaL_loadbuffer(L, input, l, "=(debug command)") | |
1322 || lua_pcall(L, 0, 0, 0)) | |
1323 luaV_emsg(L); | |
1324 lua_settop(L, 1); /* remove eventual returns, but keep vim.eval */ | |
1325 } | |
1326 } | |
1327 | |
1328 static int | |
2330 | 1329 luaV_command(lua_State *L) |
1330 { | |
2320
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
1331 do_cmdline_cmd((char_u *) luaL_checkstring(L, 1)); |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
1332 update_screen(VALID); |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
1333 return 0; |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
1334 } |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
1335 |
2330 | 1336 static int |
1337 luaV_eval(lua_State *L) | |
1338 { | |
2320
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
1339 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
|
1340 if (tv == NULL) luaL_error(L, "invalid expression"); |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
1341 luaV_pushtypval(L, tv); |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
1342 return 1; |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
1343 } |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
1344 |
2330 | 1345 static int |
2331
3840b7508835
Make it easier to build with Lua. Remove compiler warnings.
Bram Moolenaar <bram@vim.org>
parents:
2330
diff
changeset
|
1346 luaV_beep(lua_State *L UNUSED) |
2330 | 1347 { |
2320
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
1348 vim_beep(); |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
1349 return 0; |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
1350 } |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
1351 |
2330 | 1352 static int |
1353 luaV_line(lua_State *L) | |
1354 { | |
2320
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
1355 luaV_pushline(L, curbuf, curwin->w_cursor.lnum); |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
1356 return 1; |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
1357 } |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
1358 |
2330 | 1359 static int |
3450 | 1360 luaV_list(lua_State *L) |
1361 { | |
1362 list_T *l = list_alloc(); | |
1363 if (l == NULL) | |
1364 lua_pushnil(L); | |
1365 else | |
1366 luaV_newlist(L, l); | |
1367 return 1; | |
1368 } | |
1369 | |
1370 static int | |
1371 luaV_dict(lua_State *L) | |
1372 { | |
1373 dict_T *d = dict_alloc(); | |
1374 if (d == NULL) | |
1375 lua_pushnil(L); | |
1376 else | |
1377 luaV_newdict(L, d); | |
1378 return 1; | |
1379 } | |
1380 | |
1381 static int | |
2330 | 1382 luaV_buffer(lua_State *L) |
1383 { | |
2320
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
1384 buf_T *buf; |
2330 | 1385 if (lua_isstring(L, 1)) /* get by number or name? */ |
1386 { | |
1387 if (lua_isnumber(L, 1)) /* by number? */ | |
1388 { | |
1389 int n = lua_tointeger(L, 1); | |
1390 for (buf = firstbuf; buf != NULL; buf = buf->b_next) | |
1391 if (buf->b_fnum == n) break; | |
1392 } | |
1393 else { /* by name */ | |
1394 size_t l; | |
1395 const char *s = lua_tolstring(L, 1, &l); | |
1396 for (buf = firstbuf; buf != NULL; buf = buf->b_next) | |
1397 { | |
1398 if (buf->b_ffname == NULL || buf->b_sfname == NULL) | |
1399 { | |
1400 if (l == 0) break; | |
1401 } | |
2331
3840b7508835
Make it easier to build with Lua. Remove compiler warnings.
Bram Moolenaar <bram@vim.org>
parents:
2330
diff
changeset
|
1402 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
|
1403 || strncmp(s, (char *)buf->b_sfname, l) == 0) |
2330 | 1404 break; |
1405 } | |
1406 } | |
2320
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
1407 } |
3450 | 1408 else |
2330 | 1409 buf = (lua_toboolean(L, 1)) ? firstbuf : curbuf; /* first buffer? */ |
3450 | 1410 luaV_pushbuffer(L, buf); |
2320
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
1411 return 1; |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
1412 } |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
1413 |
2330 | 1414 static int |
1415 luaV_window(lua_State *L) | |
1416 { | |
2320
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
1417 win_T *win; |
2330 | 1418 if (lua_isnumber(L, 1)) /* get by number? */ |
1419 { | |
1420 int n = lua_tointeger(L, 1); | |
1421 for (win = firstwin; win != NULL; win = win->w_next, n--) | |
1422 if (n == 1) break; | |
2320
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
1423 } |
3450 | 1424 else |
2330 | 1425 win = (lua_toboolean(L, 1)) ? firstwin : curwin; /* first window? */ |
3450 | 1426 luaV_pushwindow(L, win); |
2320
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
1427 return 1; |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
1428 } |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
1429 |
2330 | 1430 static int |
1431 luaV_open(lua_State *L) | |
1432 { | |
2320
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
1433 char_u *s = NULL; |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
1434 #ifdef HAVE_SANDBOX |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
1435 luaV_checksandbox(L); |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
1436 #endif |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
1437 if (lua_isstring(L, 1)) s = (char_u *) lua_tostring(L, 1); |
3200 | 1438 luaV_pushbuffer(L, buflist_new(s, NULL, 1L, BLN_LISTED)); |
2320
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
1439 return 1; |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
1440 } |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
1441 |
2330 | 1442 static int |
3450 | 1443 luaV_type(lua_State *L) |
2330 | 1444 { |
3450 | 1445 luaL_checkany(L, 1); |
1446 if (lua_type(L, 1) == LUA_TUSERDATA) /* check vim udata? */ | |
1447 { | |
1448 lua_settop(L, 1); | |
1449 if (lua_getmetatable(L, 1)) | |
1450 { | |
1451 luaV_getfield(L, LUAVIM_LIST); | |
1452 if (lua_rawequal(L, -1, 2)) | |
1453 { | |
1454 lua_pushstring(L, "list"); | |
1455 return 1; | |
1456 } | |
1457 luaV_getfield(L, LUAVIM_DICT); | |
1458 if (lua_rawequal(L, -1, 2)) | |
1459 { | |
1460 lua_pushstring(L, "dict"); | |
1461 return 1; | |
1462 } | |
1463 luaV_getfield(L, LUAVIM_BUFFER); | |
1464 if (lua_rawequal(L, -1, 2)) | |
1465 { | |
1466 lua_pushstring(L, "buffer"); | |
1467 return 1; | |
1468 } | |
1469 luaV_getfield(L, LUAVIM_WINDOW); | |
1470 if (lua_rawequal(L, -1, 2)) | |
1471 { | |
1472 lua_pushstring(L, "window"); | |
1473 return 1; | |
1474 } | |
1475 } | |
1476 } | |
1477 lua_pushstring(L, luaL_typename(L, 1)); /* fallback */ | |
2320
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
1478 return 1; |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
1479 } |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
1480 |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
1481 static const luaL_Reg luaV_module[] = { |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
1482 {"command", luaV_command}, |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
1483 {"eval", luaV_eval}, |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
1484 {"beep", luaV_beep}, |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
1485 {"line", luaV_line}, |
3450 | 1486 {"list", luaV_list}, |
1487 {"dict", luaV_dict}, | |
2320
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
1488 {"buffer", luaV_buffer}, |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
1489 {"window", luaV_window}, |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
1490 {"open", luaV_open}, |
3450 | 1491 {"type", luaV_type}, |
2320
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
1492 {NULL, NULL} |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
1493 }; |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
1494 |
3450 | 1495 /* for freeing list, dict, buffer and window objects; lightuserdata as arg */ |
1496 static int | |
1497 luaV_free(lua_State *L) | |
1498 { | |
1499 lua_pushnil(L); | |
1500 luaV_setudata(L, lua_touserdata(L, 1)); | |
1501 return 0; | |
1502 } | |
1503 | |
1504 static int | |
1505 luaV_luaeval (lua_State *L) | |
1506 { | |
1507 luaL_Buffer b; | |
1508 size_t l; | |
1509 const char *str = lua_tolstring(L, 1, &l); | |
1510 typval_T *arg = (typval_T *) lua_touserdata(L, 2); | |
1511 typval_T *rettv = (typval_T *) lua_touserdata(L, 3); | |
1512 luaL_buffinit(L, &b); | |
1513 luaL_addlstring(&b, LUAVIM_EVALHEADER, sizeof(LUAVIM_EVALHEADER) - 1); | |
1514 luaL_addlstring(&b, str, l); | |
1515 luaL_pushresult(&b); | |
1516 str = lua_tolstring(L, -1, &l); | |
1517 if (luaL_loadbuffer(L, str, l, LUAVIM_EVALNAME)) /* compile error? */ | |
1518 { | |
1519 luaV_emsg(L); | |
1520 return 0; | |
1521 } | |
1522 luaV_pushtypval(L, arg); | |
1523 if (lua_pcall(L, 1, 1, 0)) /* running error? */ | |
1524 { | |
1525 luaV_emsg(L); | |
1526 return 0; | |
1527 } | |
1528 luaV_totypval(L, -1, rettv); | |
1529 return 0; | |
1530 } | |
1531 | |
1532 static int | |
1533 luaV_setref (lua_State *L) | |
1534 { | |
1535 int copyID = lua_tointeger(L, 1); | |
1536 typval_T tv; | |
1537 luaV_getfield(L, LUAVIM_LIST); | |
1538 luaV_getfield(L, LUAVIM_DICT); | |
1539 lua_pushnil(L); | |
1540 while (lua_next(L, lua_upvalueindex(1)) != 0) /* traverse cache table */ | |
1541 { | |
1542 lua_getmetatable(L, -1); | |
1543 if (lua_rawequal(L, -1, 2)) /* list? */ | |
1544 { | |
1545 tv.v_type = VAR_LIST; | |
1546 tv.vval.v_list = (list_T *) lua_touserdata(L, 4); /* key */ | |
1547 } | |
1548 else if (lua_rawequal(L, -1, 3)) /* dict? */ | |
1549 { | |
1550 tv.v_type = VAR_DICT; | |
1551 tv.vval.v_dict = (dict_T *) lua_touserdata(L, 4); /* key */ | |
1552 } | |
1553 lua_pop(L, 2); /* metatable and value */ | |
3618 | 1554 set_ref_in_item(&tv, copyID); |
3450 | 1555 } |
1556 return 0; | |
1557 } | |
1558 | |
2330 | 1559 static int |
1560 luaopen_vim(lua_State *L) | |
1561 { | |
3450 | 1562 /* set cache table */ |
2320
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
1563 lua_newtable(L); |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
1564 lua_newtable(L); |
3450 | 1565 lua_pushstring(L, "v"); |
2320
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
1566 lua_setfield(L, -2, "__mode"); |
3450 | 1567 lua_setmetatable(L, -2); /* cache is weak-valued */ |
2320
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
1568 /* print */ |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
1569 lua_pushcfunction(L, luaV_print); |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
1570 lua_setglobal(L, "print"); |
3091 | 1571 /* debug.debug */ |
1572 lua_getglobal(L, "debug"); | |
1573 lua_pushcfunction(L, luaV_debug); | |
1574 lua_setfield(L, -2, "debug"); | |
1575 lua_pop(L, 1); | |
2320
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
1576 /* free */ |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
1577 lua_pushlightuserdata(L, (void *) LUAVIM_FREE); |
3450 | 1578 lua_pushvalue(L, 1); /* cache table */ |
1579 lua_pushcclosure(L, luaV_free, 1); | |
1580 lua_rawset(L, LUA_REGISTRYINDEX); | |
1581 /* luaeval */ | |
1582 lua_pushlightuserdata(L, (void *) LUAVIM_LUAEVAL); | |
1583 lua_pushvalue(L, 1); /* cache table */ | |
1584 lua_pushcclosure(L, luaV_luaeval, 1); | |
1585 lua_rawset(L, LUA_REGISTRYINDEX); | |
1586 /* setref */ | |
1587 lua_pushlightuserdata(L, (void *) LUAVIM_SETREF); | |
1588 lua_pushvalue(L, 1); /* cache table */ | |
1589 lua_pushcclosure(L, luaV_setref, 1); | |
2320
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
1590 lua_rawset(L, LUA_REGISTRYINDEX); |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
1591 /* register */ |
3450 | 1592 luaV_newmetatable(L, LUAVIM_LIST); |
1593 lua_pushvalue(L, 1); | |
1594 luaV_openlib(L, luaV_List_mt, 1); | |
1595 luaV_newmetatable(L, LUAVIM_DICT); | |
1596 lua_pushvalue(L, 1); | |
1597 luaV_openlib(L, luaV_Dict_mt, 1); | |
2320
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
1598 luaV_newmetatable(L, LUAVIM_BUFFER); |
3450 | 1599 lua_pushvalue(L, 1); /* cache table */ |
1600 luaV_openlib(L, luaV_Buffer_mt, 1); | |
2320
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
1601 luaV_newmetatable(L, LUAVIM_WINDOW); |
3450 | 1602 lua_pushvalue(L, 1); /* cache table */ |
1603 luaV_openlib(L, luaV_Window_mt, 1); | |
1604 lua_newtable(L); /* vim table */ | |
1605 lua_pushvalue(L, 1); /* cache table */ | |
1606 luaV_openlib(L, luaV_module, 1); | |
1607 lua_setglobal(L, LUAVIM_NAME); | |
2320
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
1608 return 0; |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
1609 } |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
1610 |
2330 | 1611 static lua_State * |
1612 luaV_newstate(void) | |
1613 { | |
2320
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
1614 lua_State *L = luaL_newstate(); |
2431
7b764999f9b9
Update for Lua interface. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
2373
diff
changeset
|
1615 luaL_openlibs(L); /* core libs */ |
7b764999f9b9
Update for Lua interface. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
2373
diff
changeset
|
1616 lua_pushcfunction(L, luaopen_vim); /* vim */ |
2320
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
1617 lua_call(L, 0, 0); |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
1618 return L; |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
1619 } |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
1620 |
2330 | 1621 static void |
1622 luaV_setrange(lua_State *L, int line1, int line2) | |
1623 { | |
2320
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
1624 lua_getglobal(L, LUAVIM_NAME); |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
1625 lua_pushinteger(L, line1); |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
1626 lua_setfield(L, -2, "firstline"); |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
1627 lua_pushinteger(L, line2); |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
1628 lua_setfield(L, -2, "lastline"); |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
1629 lua_pop(L, 1); /* vim table */ |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
1630 } |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
1631 |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
1632 |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
1633 /* ======= Interface ======= */ |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
1634 |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
1635 static lua_State *L = NULL; |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
1636 |
2330 | 1637 static int |
3450 | 1638 lua_isopen(void) |
2543
8c512e2c7cb5
Fix: Lua interface tried to load the library when closing a buffer or window.
Bram Moolenaar <bram@vim.org>
parents:
2431
diff
changeset
|
1639 { |
8c512e2c7cb5
Fix: Lua interface tried to load the library when closing a buffer or window.
Bram Moolenaar <bram@vim.org>
parents:
2431
diff
changeset
|
1640 return L != NULL; |
8c512e2c7cb5
Fix: Lua interface tried to load the library when closing a buffer or window.
Bram Moolenaar <bram@vim.org>
parents:
2431
diff
changeset
|
1641 } |
8c512e2c7cb5
Fix: Lua interface tried to load the library when closing a buffer or window.
Bram Moolenaar <bram@vim.org>
parents:
2431
diff
changeset
|
1642 |
8c512e2c7cb5
Fix: Lua interface tried to load the library when closing a buffer or window.
Bram Moolenaar <bram@vim.org>
parents:
2431
diff
changeset
|
1643 static int |
2330 | 1644 lua_init(void) |
1645 { | |
3450 | 1646 if (!lua_isopen()) |
2330 | 1647 { |
2320
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
1648 #ifdef DYNAMIC_LUA |
2330 | 1649 if (!lua_enabled(TRUE)) |
1650 { | |
1651 EMSG(_("Lua library cannot be loaded.")); | |
1652 return FAIL; | |
1653 } | |
2320
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
1654 #endif |
2330 | 1655 L = luaV_newstate(); |
2320
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
1656 } |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
1657 return OK; |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
1658 } |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
1659 |
2330 | 1660 void |
1661 lua_end(void) | |
1662 { | |
3450 | 1663 if (lua_isopen()) |
2330 | 1664 { |
1665 lua_close(L); | |
1666 L = NULL; | |
2320
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
1667 #ifdef DYNAMIC_LUA |
2330 | 1668 end_dynamic_lua(); |
2320
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
1669 #endif |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
1670 } |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
1671 } |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
1672 |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
1673 /* ex commands */ |
2330 | 1674 void |
1675 ex_lua(exarg_T *eap) | |
1676 { | |
2320
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
1677 char *script; |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
1678 if (lua_init() == FAIL) return; |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
1679 script = (char *) script_get(eap, eap->arg); |
2330 | 1680 if (!eap->skip) |
1681 { | |
1682 char *s = (script) ? script : (char *) eap->arg; | |
1683 luaV_setrange(L, eap->line1, eap->line2); | |
1684 if (luaL_loadbuffer(L, s, strlen(s), LUAVIM_CHUNKNAME) | |
1685 || lua_pcall(L, 0, 0, 0)) | |
1686 luaV_emsg(L); | |
2320
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
1687 } |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
1688 if (script != NULL) vim_free(script); |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
1689 } |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
1690 |
2330 | 1691 void |
1692 ex_luado(exarg_T *eap) | |
1693 { | |
2320
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
1694 linenr_T l; |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
1695 const char *s = (const char *) eap->arg; |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
1696 luaL_Buffer b; |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
1697 size_t len; |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
1698 if (lua_init() == FAIL) return; |
2330 | 1699 if (u_save(eap->line1 - 1, eap->line2 + 1) == FAIL) |
1700 { | |
1701 EMSG(_("cannot save undo information")); | |
1702 return; | |
2320
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
1703 } |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
1704 luaV_setrange(L, eap->line1, eap->line2); |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
1705 luaL_buffinit(L, &b); |
3459 | 1706 luaL_addlstring(&b, "return function(line, linenr) ", 30); /* header */ |
2320
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
1707 luaL_addlstring(&b, s, strlen(s)); |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
1708 luaL_addlstring(&b, " end", 4); /* footer */ |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
1709 luaL_pushresult(&b); |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
1710 s = lua_tolstring(L, -1, &len); |
2330 | 1711 if (luaL_loadbuffer(L, s, len, LUAVIM_CHUNKNAME)) |
1712 { | |
1713 luaV_emsg(L); | |
1714 lua_pop(L, 1); /* function body */ | |
1715 return; | |
2320
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
1716 } |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
1717 lua_call(L, 0, 1); |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
1718 lua_replace(L, -2); /* function -> body */ |
2330 | 1719 for (l = eap->line1; l <= eap->line2; l++) |
1720 { | |
1721 lua_pushvalue(L, -1); /* function */ | |
1722 luaV_pushline(L, curbuf, l); /* current line as arg */ | |
3459 | 1723 lua_pushinteger(L, l); /* current line number as arg */ |
1724 if (lua_pcall(L, 2, 1, 0)) | |
2330 | 1725 { |
1726 luaV_emsg(L); | |
1727 break; | |
1728 } | |
1729 if (lua_isstring(L, -1)) /* update line? */ | |
1730 { | |
2320
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
1731 #ifdef HAVE_SANDBOX |
2330 | 1732 luaV_checksandbox(L); |
2320
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
1733 #endif |
2330 | 1734 ml_replace(l, luaV_toline(L, -1), TRUE); |
1735 changed_bytes(l, 0); | |
1736 lua_pop(L, 1); /* result from luaV_toline */ | |
1737 } | |
1738 lua_pop(L, 1); /* line */ | |
2320
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
1739 } |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
1740 lua_pop(L, 1); /* function */ |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
1741 check_cursor(); |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
1742 update_screen(NOT_VALID); |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
1743 } |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
1744 |
2330 | 1745 void |
1746 ex_luafile(exarg_T *eap) | |
1747 { | |
1748 if (lua_init() == FAIL) | |
1749 return; | |
1750 if (!eap->skip) | |
1751 { | |
1752 luaV_setrange(L, eap->line1, eap->line2); | |
1753 if (luaL_loadfile(L, (char *) eap->arg) || lua_pcall(L, 0, 0, 0)) | |
1754 luaV_emsg(L); | |
2320
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
1755 } |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
1756 } |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
1757 |
3450 | 1758 #define luaV_freetype(typ,tname) \ |
1759 void \ | |
1760 lua_##tname##_free(typ *o) \ | |
1761 { \ | |
1762 if (!lua_isopen()) return; \ | |
1763 luaV_getfield(L, LUAVIM_FREE); \ | |
1764 lua_pushlightuserdata(L, (void *) o); \ | |
1765 lua_call(L, 1, 0); \ | |
1766 } | |
1767 | |
1768 luaV_freetype(buf_T, buffer) | |
1769 luaV_freetype(win_T, window) | |
1770 | |
2330 | 1771 void |
3450 | 1772 do_luaeval (char_u *str, typval_T *arg, typval_T *rettv) |
2330 | 1773 { |
3450 | 1774 lua_init(); |
1775 luaV_getfield(L, LUAVIM_LUAEVAL); | |
1776 lua_pushstring(L, (char *) str); | |
1777 lua_pushlightuserdata(L, (void *) arg); | |
1778 lua_pushlightuserdata(L, (void *) rettv); | |
1779 lua_call(L, 3, 0); | |
2320
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
1780 } |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
1781 |
2330 | 1782 void |
3450 | 1783 set_ref_in_lua (int copyID) |
2330 | 1784 { |
3450 | 1785 if (!lua_isopen()) return; |
1786 luaV_getfield(L, LUAVIM_SETREF); | |
1787 lua_pushinteger(L, copyID); | |
2320
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
1788 lua_call(L, 1, 0); |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
1789 } |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
1790 |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
1791 #endif |