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