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