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