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