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