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