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