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