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