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