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 | 801ab06 | 2020-06-25 19:27:56 +0200 | [diff] [blame] | 660 | if (lua_getmetatable(L, pos)) { |
| 661 | lua_getfield(L, -1, LUA___CALL); |
| 662 | if (lua_isfunction(L, -1)) { |
| 663 | char_u *name; |
| 664 | int lua_funcref = luaL_ref(L, LUA_REGISTRYINDEX); |
| 665 | luaV_CFuncState *state = ALLOC_CLEAR_ONE(luaV_CFuncState); |
Bram Moolenaar | 066e7da | 2020-07-18 12:50:35 +0200 | [diff] [blame] | 666 | |
Bram Moolenaar | 801ab06 | 2020-06-25 19:27:56 +0200 | [diff] [blame] | 667 | state->lua_funcref = lua_funcref; |
| 668 | state->L = L; |
| 669 | state->lua_tableref = lua_tableref; |
| 670 | name = register_cfunc(&luaV_call_lua_func, |
| 671 | &luaV_call_lua_func_free, state); |
| 672 | tv->v_type = VAR_FUNC; |
| 673 | tv->vval.v_string = vim_strsave(name); |
| 674 | break; |
| 675 | } |
| 676 | } |
| 677 | tv->v_type = VAR_NUMBER; |
| 678 | tv->vval.v_number = 0; |
| 679 | status = FAIL; |
| 680 | break; |
| 681 | } |
Bram Moolenaar | ca06da9 | 2018-07-01 15:12:05 +0200 | [diff] [blame] | 682 | case LUA_TUSERDATA: |
| 683 | { |
Bram Moolenaar | 1dced57 | 2012-04-05 16:54:08 +0200 | [diff] [blame] | 684 | void *p = lua_touserdata(L, pos); |
Bram Moolenaar | ca06da9 | 2018-07-01 15:12:05 +0200 | [diff] [blame] | 685 | |
Bram Moolenaar | b782869 | 2019-03-23 13:57:02 +0100 | [diff] [blame] | 686 | if (lua_getmetatable(L, pos)) // has metatable? |
Bram Moolenaar | 55d5c03 | 2010-07-17 23:52:29 +0200 | [diff] [blame] | 687 | { |
Bram Moolenaar | b782869 | 2019-03-23 13:57:02 +0100 | [diff] [blame] | 688 | // check list |
Bram Moolenaar | 1dced57 | 2012-04-05 16:54:08 +0200 | [diff] [blame] | 689 | luaV_getfield(L, LUAVIM_LIST); |
| 690 | if (lua_rawequal(L, -1, -2)) |
Bram Moolenaar | 2334b6d | 2010-07-22 21:32:16 +0200 | [diff] [blame] | 691 | { |
Bram Moolenaar | 1dced57 | 2012-04-05 16:54:08 +0200 | [diff] [blame] | 692 | tv->v_type = VAR_LIST; |
| 693 | tv->vval.v_list = *((luaV_List *) p); |
| 694 | ++tv->vval.v_list->lv_refcount; |
Bram Moolenaar | b782869 | 2019-03-23 13:57:02 +0100 | [diff] [blame] | 695 | lua_pop(L, 2); // MTs |
Bram Moolenaar | ca06da9 | 2018-07-01 15:12:05 +0200 | [diff] [blame] | 696 | break; |
Bram Moolenaar | 55d5c03 | 2010-07-17 23:52:29 +0200 | [diff] [blame] | 697 | } |
Bram Moolenaar | b782869 | 2019-03-23 13:57:02 +0100 | [diff] [blame] | 698 | // check dict |
Bram Moolenaar | 1dced57 | 2012-04-05 16:54:08 +0200 | [diff] [blame] | 699 | luaV_getfield(L, LUAVIM_DICT); |
| 700 | if (lua_rawequal(L, -1, -3)) |
| 701 | { |
| 702 | tv->v_type = VAR_DICT; |
| 703 | tv->vval.v_dict = *((luaV_Dict *) p); |
| 704 | ++tv->vval.v_dict->dv_refcount; |
Bram Moolenaar | b782869 | 2019-03-23 13:57:02 +0100 | [diff] [blame] | 705 | lua_pop(L, 3); // MTs |
Bram Moolenaar | ca06da9 | 2018-07-01 15:12:05 +0200 | [diff] [blame] | 706 | break; |
Bram Moolenaar | 1dced57 | 2012-04-05 16:54:08 +0200 | [diff] [blame] | 707 | } |
Bram Moolenaar | b782869 | 2019-03-23 13:57:02 +0100 | [diff] [blame] | 708 | // check blob |
| 709 | luaV_getfield(L, LUAVIM_BLOB); |
Bram Moolenaar | ca06da9 | 2018-07-01 15:12:05 +0200 | [diff] [blame] | 710 | if (lua_rawequal(L, -1, -4)) |
| 711 | { |
Bram Moolenaar | b782869 | 2019-03-23 13:57:02 +0100 | [diff] [blame] | 712 | tv->v_type = VAR_BLOB; |
| 713 | tv->vval.v_blob = *((luaV_Blob *) p); |
| 714 | ++tv->vval.v_blob->bv_refcount; |
| 715 | lua_pop(L, 4); // MTs |
| 716 | break; |
| 717 | } |
| 718 | // check funcref |
| 719 | luaV_getfield(L, LUAVIM_FUNCREF); |
| 720 | if (lua_rawequal(L, -1, -5)) |
| 721 | { |
Bram Moolenaar | ca06da9 | 2018-07-01 15:12:05 +0200 | [diff] [blame] | 722 | luaV_Funcref *f = (luaV_Funcref *) p; |
Bram Moolenaar | 066e7da | 2020-07-18 12:50:35 +0200 | [diff] [blame] | 723 | |
Bram Moolenaar | 4eefe47 | 2019-03-19 21:59:19 +0100 | [diff] [blame] | 724 | func_ref(f->name); |
| 725 | tv->v_type = VAR_FUNC; |
| 726 | tv->vval.v_string = vim_strsave(f->name); |
Bram Moolenaar | b782869 | 2019-03-23 13:57:02 +0100 | [diff] [blame] | 727 | lua_pop(L, 5); // MTs |
Bram Moolenaar | ca06da9 | 2018-07-01 15:12:05 +0200 | [diff] [blame] | 728 | break; |
| 729 | } |
Bram Moolenaar | b782869 | 2019-03-23 13:57:02 +0100 | [diff] [blame] | 730 | lua_pop(L, 4); // MTs |
Bram Moolenaar | 55d5c03 | 2010-07-17 23:52:29 +0200 | [diff] [blame] | 731 | } |
Bram Moolenaar | 55d5c03 | 2010-07-17 23:52:29 +0200 | [diff] [blame] | 732 | } |
Bram Moolenaar | b782869 | 2019-03-23 13:57:02 +0100 | [diff] [blame] | 733 | // FALLTHROUGH |
Bram Moolenaar | 55d5c03 | 2010-07-17 23:52:29 +0200 | [diff] [blame] | 734 | default: |
Bram Moolenaar | 1dced57 | 2012-04-05 16:54:08 +0200 | [diff] [blame] | 735 | tv->v_type = VAR_NUMBER; |
| 736 | tv->vval.v_number = 0; |
Bram Moolenaar | ca06da9 | 2018-07-01 15:12:05 +0200 | [diff] [blame] | 737 | status = FAIL; |
Bram Moolenaar | 0ba0429 | 2010-07-14 23:23:17 +0200 | [diff] [blame] | 738 | } |
Bram Moolenaar | ca06da9 | 2018-07-01 15:12:05 +0200 | [diff] [blame] | 739 | return status; |
Bram Moolenaar | 0ba0429 | 2010-07-14 23:23:17 +0200 | [diff] [blame] | 740 | } |
| 741 | |
Bram Moolenaar | 2ab2e86 | 2019-12-04 21:24:53 +0100 | [diff] [blame] | 742 | /* |
| 743 | * similar to luaL_addlstring, but replaces \0 with \n if toline and |
| 744 | * \n with \0 otherwise |
| 745 | */ |
Bram Moolenaar | 55d5c03 | 2010-07-17 23:52:29 +0200 | [diff] [blame] | 746 | static void |
| 747 | luaV_addlstring(luaL_Buffer *b, const char *s, size_t l, int toline) |
| 748 | { |
| 749 | while (l--) |
| 750 | { |
| 751 | if (*s == '\0' && toline) |
| 752 | luaL_addchar(b, '\n'); |
| 753 | else if (*s == '\n' && !toline) |
| 754 | luaL_addchar(b, '\0'); |
| 755 | else |
| 756 | luaL_addchar(b, *s); |
| 757 | s++; |
Bram Moolenaar | 0ba0429 | 2010-07-14 23:23:17 +0200 | [diff] [blame] | 758 | } |
| 759 | } |
| 760 | |
Bram Moolenaar | 55d5c03 | 2010-07-17 23:52:29 +0200 | [diff] [blame] | 761 | static void |
| 762 | luaV_pushline(lua_State *L, buf_T *buf, linenr_T n) |
| 763 | { |
Bram Moolenaar | 0ba0429 | 2010-07-14 23:23:17 +0200 | [diff] [blame] | 764 | const char *s = (const char *) ml_get_buf(buf, n, FALSE); |
| 765 | luaL_Buffer b; |
| 766 | luaL_buffinit(L, &b); |
| 767 | luaV_addlstring(&b, s, strlen(s), 0); |
| 768 | luaL_pushresult(&b); |
| 769 | } |
| 770 | |
Bram Moolenaar | 55d5c03 | 2010-07-17 23:52:29 +0200 | [diff] [blame] | 771 | static char_u * |
| 772 | luaV_toline(lua_State *L, int pos) |
| 773 | { |
Bram Moolenaar | 0ba0429 | 2010-07-14 23:23:17 +0200 | [diff] [blame] | 774 | size_t l; |
| 775 | const char *s = lua_tolstring(L, pos, &l); |
Bram Moolenaar | 55d5c03 | 2010-07-17 23:52:29 +0200 | [diff] [blame] | 776 | |
Bram Moolenaar | 0ba0429 | 2010-07-14 23:23:17 +0200 | [diff] [blame] | 777 | luaL_Buffer b; |
| 778 | luaL_buffinit(L, &b); |
| 779 | luaV_addlstring(&b, s, l, 1); |
| 780 | luaL_pushresult(&b); |
| 781 | return (char_u *) lua_tostring(L, -1); |
| 782 | } |
| 783 | |
Bram Moolenaar | 2ab2e86 | 2019-12-04 21:24:53 +0100 | [diff] [blame] | 784 | /* |
| 785 | * pops a string s from the top of the stack and calls mf(t) for pieces t of |
| 786 | * s separated by newlines |
| 787 | */ |
Bram Moolenaar | 55d5c03 | 2010-07-17 23:52:29 +0200 | [diff] [blame] | 788 | static void |
| 789 | luaV_msgfunc(lua_State *L, msgfunc_T mf) |
| 790 | { |
Bram Moolenaar | 0ba0429 | 2010-07-14 23:23:17 +0200 | [diff] [blame] | 791 | luaL_Buffer b; |
| 792 | size_t l; |
| 793 | const char *p, *s = lua_tolstring(L, -1, &l); |
| 794 | luaL_buffinit(L, &b); |
| 795 | luaV_addlstring(&b, s, l, 0); |
| 796 | luaL_pushresult(&b); |
Bram Moolenaar | 2ab2e86 | 2019-12-04 21:24:53 +0100 | [diff] [blame] | 797 | // break string |
Bram Moolenaar | 0ba0429 | 2010-07-14 23:23:17 +0200 | [diff] [blame] | 798 | p = s = lua_tolstring(L, -1, &l); |
Bram Moolenaar | 55d5c03 | 2010-07-17 23:52:29 +0200 | [diff] [blame] | 799 | while (l--) |
| 800 | { |
Bram Moolenaar | 2ab2e86 | 2019-12-04 21:24:53 +0100 | [diff] [blame] | 801 | if (*p++ == '\0') // break? |
Bram Moolenaar | 55d5c03 | 2010-07-17 23:52:29 +0200 | [diff] [blame] | 802 | { |
Bram Moolenaar | c8970b9 | 2020-10-26 20:18:08 +0100 | [diff] [blame] | 803 | mf((char *)s); |
Bram Moolenaar | 55d5c03 | 2010-07-17 23:52:29 +0200 | [diff] [blame] | 804 | s = p; |
| 805 | } |
Bram Moolenaar | 0ba0429 | 2010-07-14 23:23:17 +0200 | [diff] [blame] | 806 | } |
Bram Moolenaar | c8970b9 | 2020-10-26 20:18:08 +0100 | [diff] [blame] | 807 | mf((char *)s); |
Bram Moolenaar | 2ab2e86 | 2019-12-04 21:24:53 +0100 | [diff] [blame] | 808 | lua_pop(L, 2); // original and modified strings |
Bram Moolenaar | 0ba0429 | 2010-07-14 23:23:17 +0200 | [diff] [blame] | 809 | } |
| 810 | |
Bram Moolenaar | 1dced57 | 2012-04-05 16:54:08 +0200 | [diff] [blame] | 811 | #define luaV_newtype(typ,tname,luatyp,luatname) \ |
| 812 | static luatyp * \ |
Bram Moolenaar | 4eefe47 | 2019-03-19 21:59:19 +0100 | [diff] [blame] | 813 | luaV_new##tname(lua_State *L, typ *obj) \ |
Bram Moolenaar | 1dced57 | 2012-04-05 16:54:08 +0200 | [diff] [blame] | 814 | { \ |
| 815 | luatyp *o = (luatyp *) lua_newuserdata(L, sizeof(luatyp)); \ |
| 816 | *o = obj; \ |
| 817 | luaV_setudata(L, obj); /* cache[obj] = udata */ \ |
| 818 | luaV_getfield(L, luatname); \ |
| 819 | lua_setmetatable(L, -2); \ |
| 820 | return o; \ |
Bram Moolenaar | 0ba0429 | 2010-07-14 23:23:17 +0200 | [diff] [blame] | 821 | } |
Bram Moolenaar | 1dced57 | 2012-04-05 16:54:08 +0200 | [diff] [blame] | 822 | |
| 823 | #define luaV_pushtype(typ,tname,luatyp) \ |
| 824 | static luatyp * \ |
Bram Moolenaar | ca06da9 | 2018-07-01 15:12:05 +0200 | [diff] [blame] | 825 | luaV_push##tname(lua_State *L, typ *obj) \ |
Bram Moolenaar | 1dced57 | 2012-04-05 16:54:08 +0200 | [diff] [blame] | 826 | { \ |
| 827 | luatyp *o = NULL; \ |
| 828 | if (obj == NULL) \ |
| 829 | lua_pushnil(L); \ |
| 830 | else { \ |
| 831 | luaV_getudata(L, obj); \ |
| 832 | if (lua_isnil(L, -1)) /* not interned? */ \ |
| 833 | { \ |
| 834 | lua_pop(L, 1); \ |
| 835 | o = luaV_new##tname(L, obj); \ |
| 836 | } \ |
| 837 | else \ |
| 838 | o = (luatyp *) lua_touserdata(L, -1); \ |
| 839 | } \ |
| 840 | return o; \ |
| 841 | } |
| 842 | |
| 843 | #define luaV_type_tostring(tname,luatname) \ |
| 844 | static int \ |
Bram Moolenaar | 4eefe47 | 2019-03-19 21:59:19 +0100 | [diff] [blame] | 845 | luaV_##tname##_tostring(lua_State *L) \ |
Bram Moolenaar | 1dced57 | 2012-04-05 16:54:08 +0200 | [diff] [blame] | 846 | { \ |
| 847 | lua_pushfstring(L, "%s: %p", luatname, lua_touserdata(L, 1)); \ |
| 848 | return 1; \ |
| 849 | } |
| 850 | |
Bram Moolenaar | 2ab2e86 | 2019-12-04 21:24:53 +0100 | [diff] [blame] | 851 | // ======= List type ======= |
Bram Moolenaar | 1dced57 | 2012-04-05 16:54:08 +0200 | [diff] [blame] | 852 | |
| 853 | static luaV_List * |
Bram Moolenaar | 4eefe47 | 2019-03-19 21:59:19 +0100 | [diff] [blame] | 854 | luaV_newlist(lua_State *L, list_T *lis) |
Bram Moolenaar | 1dced57 | 2012-04-05 16:54:08 +0200 | [diff] [blame] | 855 | { |
| 856 | luaV_List *l = (luaV_List *) lua_newuserdata(L, sizeof(luaV_List)); |
| 857 | *l = lis; |
Bram Moolenaar | 2ab2e86 | 2019-12-04 21:24:53 +0100 | [diff] [blame] | 858 | lis->lv_refcount++; // reference in Lua |
| 859 | luaV_setudata(L, lis); // cache[lis] = udata |
Bram Moolenaar | 1dced57 | 2012-04-05 16:54:08 +0200 | [diff] [blame] | 860 | luaV_getfield(L, LUAVIM_LIST); |
| 861 | lua_setmetatable(L, -2); |
| 862 | return l; |
| 863 | } |
| 864 | |
| 865 | luaV_pushtype(list_T, list, luaV_List) |
| 866 | luaV_type_tostring(list, LUAVIM_LIST) |
| 867 | |
| 868 | static int |
Bram Moolenaar | 4eefe47 | 2019-03-19 21:59:19 +0100 | [diff] [blame] | 869 | luaV_list_len(lua_State *L) |
Bram Moolenaar | 1dced57 | 2012-04-05 16:54:08 +0200 | [diff] [blame] | 870 | { |
| 871 | list_T *l = luaV_unbox(L, luaV_List, 1); |
Bram Moolenaar | b782869 | 2019-03-23 13:57:02 +0100 | [diff] [blame] | 872 | lua_pushinteger(L, (int) list_len(l)); |
Bram Moolenaar | 0ba0429 | 2010-07-14 23:23:17 +0200 | [diff] [blame] | 873 | return 1; |
| 874 | } |
| 875 | |
Bram Moolenaar | 55d5c03 | 2010-07-17 23:52:29 +0200 | [diff] [blame] | 876 | static int |
Bram Moolenaar | 4eefe47 | 2019-03-19 21:59:19 +0100 | [diff] [blame] | 877 | luaV_list_iter(lua_State *L) |
Bram Moolenaar | 1dced57 | 2012-04-05 16:54:08 +0200 | [diff] [blame] | 878 | { |
| 879 | listitem_T *li = (listitem_T *) lua_touserdata(L, lua_upvalueindex(2)); |
| 880 | if (li == NULL) return 0; |
| 881 | luaV_pushtypval(L, &li->li_tv); |
| 882 | lua_pushlightuserdata(L, (void *) li->li_next); |
| 883 | lua_replace(L, lua_upvalueindex(2)); |
| 884 | return 1; |
| 885 | } |
| 886 | |
| 887 | static int |
Bram Moolenaar | 4eefe47 | 2019-03-19 21:59:19 +0100 | [diff] [blame] | 888 | luaV_list_call(lua_State *L) |
Bram Moolenaar | 1dced57 | 2012-04-05 16:54:08 +0200 | [diff] [blame] | 889 | { |
| 890 | list_T *l = luaV_unbox(L, luaV_List, 1); |
Bram Moolenaar | 2ab2e86 | 2019-12-04 21:24:53 +0100 | [diff] [blame] | 891 | lua_pushvalue(L, lua_upvalueindex(1)); // pass cache table along |
Bram Moolenaar | 1dced57 | 2012-04-05 16:54:08 +0200 | [diff] [blame] | 892 | lua_pushlightuserdata(L, (void *) l->lv_first); |
| 893 | lua_pushcclosure(L, luaV_list_iter, 2); |
| 894 | return 1; |
| 895 | } |
| 896 | |
| 897 | static int |
Bram Moolenaar | 4eefe47 | 2019-03-19 21:59:19 +0100 | [diff] [blame] | 898 | luaV_list_index(lua_State *L) |
Bram Moolenaar | 1dced57 | 2012-04-05 16:54:08 +0200 | [diff] [blame] | 899 | { |
| 900 | list_T *l = luaV_unbox(L, luaV_List, 1); |
Bram Moolenaar | 2ab2e86 | 2019-12-04 21:24:53 +0100 | [diff] [blame] | 901 | if (lua_isnumber(L, 2)) // list item? |
Bram Moolenaar | 1dced57 | 2012-04-05 16:54:08 +0200 | [diff] [blame] | 902 | { |
Bram Moolenaar | bd84617 | 2020-06-27 12:32:57 +0200 | [diff] [blame] | 903 | long n = (long) luaL_checkinteger(L, 2); |
| 904 | listitem_T *li; |
| 905 | |
| 906 | // Lua array index starts with 1 while Vim uses 0, subtract 1 to |
| 907 | // normalize. |
| 908 | n -= 1; |
| 909 | li = list_find(l, n); |
Bram Moolenaar | 1dced57 | 2012-04-05 16:54:08 +0200 | [diff] [blame] | 910 | if (li == NULL) |
| 911 | lua_pushnil(L); |
| 912 | else |
| 913 | luaV_pushtypval(L, &li->li_tv); |
| 914 | } |
Bram Moolenaar | 2ab2e86 | 2019-12-04 21:24:53 +0100 | [diff] [blame] | 915 | else if (lua_isstring(L, 2)) // method? |
Bram Moolenaar | 1dced57 | 2012-04-05 16:54:08 +0200 | [diff] [blame] | 916 | { |
| 917 | const char *s = lua_tostring(L, 2); |
| 918 | if (strncmp(s, "add", 3) == 0 |
Bram Moolenaar | b376647 | 2013-04-15 13:49:21 +0200 | [diff] [blame] | 919 | || strncmp(s, "insert", 6) == 0) |
Bram Moolenaar | 1dced57 | 2012-04-05 16:54:08 +0200 | [diff] [blame] | 920 | { |
| 921 | lua_getmetatable(L, 1); |
| 922 | lua_getfield(L, -1, s); |
| 923 | } |
| 924 | else |
| 925 | lua_pushnil(L); |
| 926 | } |
| 927 | else |
| 928 | lua_pushnil(L); |
| 929 | return 1; |
| 930 | } |
| 931 | |
| 932 | static int |
Bram Moolenaar | 4eefe47 | 2019-03-19 21:59:19 +0100 | [diff] [blame] | 933 | luaV_list_newindex(lua_State *L) |
Bram Moolenaar | 1dced57 | 2012-04-05 16:54:08 +0200 | [diff] [blame] | 934 | { |
| 935 | list_T *l = luaV_unbox(L, luaV_List, 1); |
| 936 | long n = (long) luaL_checkinteger(L, 2); |
| 937 | listitem_T *li; |
Bram Moolenaar | bd84617 | 2020-06-27 12:32:57 +0200 | [diff] [blame] | 938 | |
| 939 | // Lua array index starts with 1 while Vim uses 0, subtract 1 to normalize. |
| 940 | n -= 1; |
| 941 | |
Bram Moolenaar | 1dced57 | 2012-04-05 16:54:08 +0200 | [diff] [blame] | 942 | if (l->lv_lock) |
| 943 | luaL_error(L, "list is locked"); |
| 944 | li = list_find(l, n); |
Bram Moolenaar | a1f9f86 | 2020-06-28 22:41:26 +0200 | [diff] [blame] | 945 | if (li == NULL) |
Bram Moolenaar | 1dced57 | 2012-04-05 16:54:08 +0200 | [diff] [blame] | 946 | { |
Bram Moolenaar | 6ed545e | 2022-05-09 20:09:23 +0100 | [diff] [blame] | 947 | if (!lua_isnil(L, 3)) |
| 948 | { |
| 949 | typval_T v; |
| 950 | luaV_checktypval(L, 3, &v, "inserting list item"); |
| 951 | if (list_insert_tv(l, &v, li) == FAIL) |
| 952 | luaL_error(L, "failed to add item to list"); |
| 953 | clear_tv(&v); |
| 954 | } |
Bram Moolenaar | 1dced57 | 2012-04-05 16:54:08 +0200 | [diff] [blame] | 955 | } |
| 956 | else |
| 957 | { |
Bram Moolenaar | 6ed545e | 2022-05-09 20:09:23 +0100 | [diff] [blame] | 958 | if (lua_isnil(L, 3)) // remove? |
| 959 | { |
Bram Moolenaar | a1f9f86 | 2020-06-28 22:41:26 +0200 | [diff] [blame] | 960 | vimlist_remove(l, li, li); |
| 961 | listitem_free(l, li); |
Bram Moolenaar | 6ed545e | 2022-05-09 20:09:23 +0100 | [diff] [blame] | 962 | } |
| 963 | else |
| 964 | { |
Bram Moolenaar | a1f9f86 | 2020-06-28 22:41:26 +0200 | [diff] [blame] | 965 | typval_T v; |
| 966 | luaV_checktypval(L, 3, &v, "setting list item"); |
| 967 | clear_tv(&li->li_tv); |
Bram Moolenaar | e49b8e8 | 2020-07-01 13:52:55 +0200 | [diff] [blame] | 968 | li->li_tv = v; |
Bram Moolenaar | 6ed545e | 2022-05-09 20:09:23 +0100 | [diff] [blame] | 969 | } |
Bram Moolenaar | 1dced57 | 2012-04-05 16:54:08 +0200 | [diff] [blame] | 970 | } |
| 971 | return 0; |
| 972 | } |
| 973 | |
| 974 | static int |
Bram Moolenaar | 4eefe47 | 2019-03-19 21:59:19 +0100 | [diff] [blame] | 975 | luaV_list_add(lua_State *L) |
Bram Moolenaar | 1dced57 | 2012-04-05 16:54:08 +0200 | [diff] [blame] | 976 | { |
| 977 | luaV_List *lis = luaV_checkudata(L, 1, LUAVIM_LIST); |
| 978 | list_T *l = (list_T *) luaV_checkcache(L, (void *) *lis); |
Bram Moolenaar | b376647 | 2013-04-15 13:49:21 +0200 | [diff] [blame] | 979 | typval_T v; |
Bram Moolenaar | 1dced57 | 2012-04-05 16:54:08 +0200 | [diff] [blame] | 980 | if (l->lv_lock) |
| 981 | luaL_error(L, "list is locked"); |
Bram Moolenaar | b376647 | 2013-04-15 13:49:21 +0200 | [diff] [blame] | 982 | lua_settop(L, 2); |
Bram Moolenaar | ca06da9 | 2018-07-01 15:12:05 +0200 | [diff] [blame] | 983 | luaV_checktypval(L, 2, &v, "adding list item"); |
Bram Moolenaar | b376647 | 2013-04-15 13:49:21 +0200 | [diff] [blame] | 984 | if (list_append_tv(l, &v) == FAIL) |
Bram Moolenaar | ca06da9 | 2018-07-01 15:12:05 +0200 | [diff] [blame] | 985 | luaL_error(L, "failed to add item to list"); |
Bram Moolenaar | b376647 | 2013-04-15 13:49:21 +0200 | [diff] [blame] | 986 | clear_tv(&v); |
Bram Moolenaar | 1dced57 | 2012-04-05 16:54:08 +0200 | [diff] [blame] | 987 | lua_settop(L, 1); |
| 988 | return 1; |
| 989 | } |
| 990 | |
| 991 | static int |
Bram Moolenaar | 4eefe47 | 2019-03-19 21:59:19 +0100 | [diff] [blame] | 992 | luaV_list_insert(lua_State *L) |
Bram Moolenaar | 1dced57 | 2012-04-05 16:54:08 +0200 | [diff] [blame] | 993 | { |
| 994 | luaV_List *lis = luaV_checkudata(L, 1, LUAVIM_LIST); |
| 995 | list_T *l = (list_T *) luaV_checkcache(L, (void *) *lis); |
Bram Moolenaar | 46538ee | 2015-02-17 16:28:55 +0100 | [diff] [blame] | 996 | long pos = (long) luaL_optinteger(L, 3, 0); |
Bram Moolenaar | 1dced57 | 2012-04-05 16:54:08 +0200 | [diff] [blame] | 997 | listitem_T *li = NULL; |
| 998 | typval_T v; |
| 999 | if (l->lv_lock) |
| 1000 | luaL_error(L, "list is locked"); |
| 1001 | if (pos < l->lv_len) |
| 1002 | { |
| 1003 | li = list_find(l, pos); |
| 1004 | if (li == NULL) |
| 1005 | luaL_error(L, "invalid position"); |
| 1006 | } |
| 1007 | lua_settop(L, 2); |
Bram Moolenaar | ca06da9 | 2018-07-01 15:12:05 +0200 | [diff] [blame] | 1008 | luaV_checktypval(L, 2, &v, "inserting list item"); |
Bram Moolenaar | b376647 | 2013-04-15 13:49:21 +0200 | [diff] [blame] | 1009 | if (list_insert_tv(l, &v, li) == FAIL) |
Bram Moolenaar | ca06da9 | 2018-07-01 15:12:05 +0200 | [diff] [blame] | 1010 | luaL_error(L, "failed to add item to list"); |
Bram Moolenaar | b376647 | 2013-04-15 13:49:21 +0200 | [diff] [blame] | 1011 | clear_tv(&v); |
Bram Moolenaar | 1dced57 | 2012-04-05 16:54:08 +0200 | [diff] [blame] | 1012 | lua_settop(L, 1); |
| 1013 | return 1; |
| 1014 | } |
| 1015 | |
| 1016 | static const luaL_Reg luaV_List_mt[] = { |
| 1017 | {"__tostring", luaV_list_tostring}, |
Bram Moolenaar | 1dced57 | 2012-04-05 16:54:08 +0200 | [diff] [blame] | 1018 | {"__len", luaV_list_len}, |
| 1019 | {"__call", luaV_list_call}, |
| 1020 | {"__index", luaV_list_index}, |
| 1021 | {"__newindex", luaV_list_newindex}, |
| 1022 | {"add", luaV_list_add}, |
| 1023 | {"insert", luaV_list_insert}, |
| 1024 | {NULL, NULL} |
| 1025 | }; |
| 1026 | |
| 1027 | |
Bram Moolenaar | 2ab2e86 | 2019-12-04 21:24:53 +0100 | [diff] [blame] | 1028 | // ======= Dict type ======= |
Bram Moolenaar | 1dced57 | 2012-04-05 16:54:08 +0200 | [diff] [blame] | 1029 | |
| 1030 | static luaV_Dict * |
Bram Moolenaar | 4eefe47 | 2019-03-19 21:59:19 +0100 | [diff] [blame] | 1031 | luaV_newdict(lua_State *L, dict_T *dic) |
Bram Moolenaar | 1dced57 | 2012-04-05 16:54:08 +0200 | [diff] [blame] | 1032 | { |
| 1033 | luaV_Dict *d = (luaV_Dict *) lua_newuserdata(L, sizeof(luaV_Dict)); |
| 1034 | *d = dic; |
Bram Moolenaar | 2ab2e86 | 2019-12-04 21:24:53 +0100 | [diff] [blame] | 1035 | dic->dv_refcount++; // reference in Lua |
| 1036 | luaV_setudata(L, dic); // cache[dic] = udata |
Bram Moolenaar | 1dced57 | 2012-04-05 16:54:08 +0200 | [diff] [blame] | 1037 | luaV_getfield(L, LUAVIM_DICT); |
| 1038 | lua_setmetatable(L, -2); |
| 1039 | return d; |
| 1040 | } |
| 1041 | |
| 1042 | luaV_pushtype(dict_T, dict, luaV_Dict) |
| 1043 | luaV_type_tostring(dict, LUAVIM_DICT) |
| 1044 | |
| 1045 | static int |
Bram Moolenaar | 4eefe47 | 2019-03-19 21:59:19 +0100 | [diff] [blame] | 1046 | luaV_dict_len(lua_State *L) |
Bram Moolenaar | 1dced57 | 2012-04-05 16:54:08 +0200 | [diff] [blame] | 1047 | { |
| 1048 | dict_T *d = luaV_unbox(L, luaV_Dict, 1); |
Bram Moolenaar | b782869 | 2019-03-23 13:57:02 +0100 | [diff] [blame] | 1049 | lua_pushinteger(L, (int) dict_len(d)); |
Bram Moolenaar | 1dced57 | 2012-04-05 16:54:08 +0200 | [diff] [blame] | 1050 | return 1; |
| 1051 | } |
| 1052 | |
| 1053 | static int |
Bram Moolenaar | 4eefe47 | 2019-03-19 21:59:19 +0100 | [diff] [blame] | 1054 | luaV_dict_iter(lua_State *L UNUSED) |
Bram Moolenaar | 1dced57 | 2012-04-05 16:54:08 +0200 | [diff] [blame] | 1055 | { |
Bram Moolenaar | feeaa68 | 2013-02-14 22:19:51 +0100 | [diff] [blame] | 1056 | #ifdef FEAT_EVAL |
Bram Moolenaar | 1dced57 | 2012-04-05 16:54:08 +0200 | [diff] [blame] | 1057 | hashitem_T *hi = (hashitem_T *) lua_touserdata(L, lua_upvalueindex(2)); |
| 1058 | int n = lua_tointeger(L, lua_upvalueindex(3)); |
| 1059 | dictitem_T *di; |
| 1060 | if (n <= 0) return 0; |
| 1061 | while (HASHITEM_EMPTY(hi)) hi++; |
| 1062 | di = dict_lookup(hi); |
| 1063 | lua_pushstring(L, (char *) hi->hi_key); |
| 1064 | luaV_pushtypval(L, &di->di_tv); |
| 1065 | lua_pushlightuserdata(L, (void *) (hi + 1)); |
| 1066 | lua_replace(L, lua_upvalueindex(2)); |
| 1067 | lua_pushinteger(L, n - 1); |
| 1068 | lua_replace(L, lua_upvalueindex(3)); |
| 1069 | return 2; |
Bram Moolenaar | feeaa68 | 2013-02-14 22:19:51 +0100 | [diff] [blame] | 1070 | #else |
| 1071 | return 0; |
| 1072 | #endif |
Bram Moolenaar | 1dced57 | 2012-04-05 16:54:08 +0200 | [diff] [blame] | 1073 | } |
| 1074 | |
| 1075 | static int |
Bram Moolenaar | 4eefe47 | 2019-03-19 21:59:19 +0100 | [diff] [blame] | 1076 | luaV_dict_call(lua_State *L) |
Bram Moolenaar | 1dced57 | 2012-04-05 16:54:08 +0200 | [diff] [blame] | 1077 | { |
| 1078 | dict_T *d = luaV_unbox(L, luaV_Dict, 1); |
| 1079 | hashtab_T *ht = &d->dv_hashtab; |
Bram Moolenaar | 2ab2e86 | 2019-12-04 21:24:53 +0100 | [diff] [blame] | 1080 | lua_pushvalue(L, lua_upvalueindex(1)); // pass cache table along |
Bram Moolenaar | 1dced57 | 2012-04-05 16:54:08 +0200 | [diff] [blame] | 1081 | lua_pushlightuserdata(L, (void *) ht->ht_array); |
Bram Moolenaar | 2ab2e86 | 2019-12-04 21:24:53 +0100 | [diff] [blame] | 1082 | lua_pushinteger(L, ht->ht_used); // # remaining items |
Bram Moolenaar | 1dced57 | 2012-04-05 16:54:08 +0200 | [diff] [blame] | 1083 | lua_pushcclosure(L, luaV_dict_iter, 3); |
| 1084 | return 1; |
| 1085 | } |
| 1086 | |
| 1087 | static int |
Bram Moolenaar | ca06da9 | 2018-07-01 15:12:05 +0200 | [diff] [blame] | 1088 | luaV_dict_index(lua_State *L) |
Bram Moolenaar | 1dced57 | 2012-04-05 16:54:08 +0200 | [diff] [blame] | 1089 | { |
| 1090 | dict_T *d = luaV_unbox(L, luaV_Dict, 1); |
| 1091 | char_u *key = (char_u *) luaL_checkstring(L, 2); |
| 1092 | dictitem_T *di = dict_find(d, key, -1); |
Bram Moolenaar | ca06da9 | 2018-07-01 15:12:05 +0200 | [diff] [blame] | 1093 | |
Bram Moolenaar | 1dced57 | 2012-04-05 16:54:08 +0200 | [diff] [blame] | 1094 | if (di == NULL) |
Bram Moolenaar | ca06da9 | 2018-07-01 15:12:05 +0200 | [diff] [blame] | 1095 | { |
Yegappan Lakshmanan | 0233bdf | 2023-01-12 12:33:30 +0000 | [diff] [blame] | 1096 | lua_pushnil(L); |
| 1097 | return 1; |
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 | |
| 1100 | luaV_pushtypval(L, &di->di_tv); |
| 1101 | if (di->di_tv.v_type == VAR_FUNC) // funcref? |
| 1102 | { |
| 1103 | luaV_Funcref *f = (luaV_Funcref *) lua_touserdata(L, -1); |
| 1104 | f->self = d; // keep "self" reference |
| 1105 | d->dv_refcount++; |
| 1106 | } |
| 1107 | |
Bram Moolenaar | 1dced57 | 2012-04-05 16:54:08 +0200 | [diff] [blame] | 1108 | return 1; |
| 1109 | } |
| 1110 | |
| 1111 | static int |
Bram Moolenaar | ca06da9 | 2018-07-01 15:12:05 +0200 | [diff] [blame] | 1112 | luaV_dict_newindex(lua_State *L) |
Bram Moolenaar | 1dced57 | 2012-04-05 16:54:08 +0200 | [diff] [blame] | 1113 | { |
| 1114 | dict_T *d = luaV_unbox(L, luaV_Dict, 1); |
| 1115 | char_u *key = (char_u *) luaL_checkstring(L, 2); |
| 1116 | dictitem_T *di; |
Bram Moolenaar | e49b8e8 | 2020-07-01 13:52:55 +0200 | [diff] [blame] | 1117 | typval_T tv; |
Bram Moolenaar | 713bf9e9 | 2019-03-16 16:38:41 +0100 | [diff] [blame] | 1118 | |
Bram Moolenaar | 1dced57 | 2012-04-05 16:54:08 +0200 | [diff] [blame] | 1119 | if (d->dv_lock) |
| 1120 | luaL_error(L, "dict is locked"); |
Bram Moolenaar | d6ef5f9 | 2018-07-13 22:08:23 +0200 | [diff] [blame] | 1121 | if (key == NULL) |
| 1122 | return 0; |
| 1123 | if (*key == NUL) |
Bram Moolenaar | ca06da9 | 2018-07-01 15:12:05 +0200 | [diff] [blame] | 1124 | luaL_error(L, "empty key"); |
Bram Moolenaar | 2ab2e86 | 2019-12-04 21:24:53 +0100 | [diff] [blame] | 1125 | if (!lua_isnil(L, 3)) // read value? |
Bram Moolenaar | 1741367 | 2018-07-14 20:49:42 +0200 | [diff] [blame] | 1126 | { |
Bram Moolenaar | e49b8e8 | 2020-07-01 13:52:55 +0200 | [diff] [blame] | 1127 | luaV_checktypval(L, 3, &tv, "setting dict item"); |
| 1128 | if (d->dv_scope == VAR_DEF_SCOPE && tv.v_type == VAR_FUNC) |
| 1129 | { |
| 1130 | clear_tv(&tv); |
Bram Moolenaar | ca06da9 | 2018-07-01 15:12:05 +0200 | [diff] [blame] | 1131 | luaL_error(L, "cannot assign funcref to builtin scope"); |
Bram Moolenaar | e49b8e8 | 2020-07-01 13:52:55 +0200 | [diff] [blame] | 1132 | } |
Bram Moolenaar | ca06da9 | 2018-07-01 15:12:05 +0200 | [diff] [blame] | 1133 | } |
Bram Moolenaar | 1dced57 | 2012-04-05 16:54:08 +0200 | [diff] [blame] | 1134 | di = dict_find(d, key, -1); |
Bram Moolenaar | 2ab2e86 | 2019-12-04 21:24:53 +0100 | [diff] [blame] | 1135 | if (di == NULL) // non-existing key? |
Bram Moolenaar | 1dced57 | 2012-04-05 16:54:08 +0200 | [diff] [blame] | 1136 | { |
Bram Moolenaar | d6ef5f9 | 2018-07-13 22:08:23 +0200 | [diff] [blame] | 1137 | if (lua_isnil(L, 3)) |
| 1138 | return 0; |
Bram Moolenaar | 1dced57 | 2012-04-05 16:54:08 +0200 | [diff] [blame] | 1139 | di = dictitem_alloc(key); |
Bram Moolenaar | d6ef5f9 | 2018-07-13 22:08:23 +0200 | [diff] [blame] | 1140 | if (di == NULL) |
Bram Moolenaar | e49b8e8 | 2020-07-01 13:52:55 +0200 | [diff] [blame] | 1141 | { |
| 1142 | clear_tv(&tv); |
Bram Moolenaar | d6ef5f9 | 2018-07-13 22:08:23 +0200 | [diff] [blame] | 1143 | return 0; |
Bram Moolenaar | e49b8e8 | 2020-07-01 13:52:55 +0200 | [diff] [blame] | 1144 | } |
Bram Moolenaar | 1dced57 | 2012-04-05 16:54:08 +0200 | [diff] [blame] | 1145 | if (dict_add(d, di) == FAIL) |
| 1146 | { |
Bram Moolenaar | d6ef5f9 | 2018-07-13 22:08:23 +0200 | [diff] [blame] | 1147 | vim_free(di); |
Bram Moolenaar | e49b8e8 | 2020-07-01 13:52:55 +0200 | [diff] [blame] | 1148 | clear_tv(&tv); |
Bram Moolenaar | d6ef5f9 | 2018-07-13 22:08:23 +0200 | [diff] [blame] | 1149 | return 0; |
Bram Moolenaar | 1dced57 | 2012-04-05 16:54:08 +0200 | [diff] [blame] | 1150 | } |
| 1151 | } |
| 1152 | else |
| 1153 | clear_tv(&di->di_tv); |
Bram Moolenaar | 2ab2e86 | 2019-12-04 21:24:53 +0100 | [diff] [blame] | 1154 | if (lua_isnil(L, 3)) // remove? |
Bram Moolenaar | 1dced57 | 2012-04-05 16:54:08 +0200 | [diff] [blame] | 1155 | { |
| 1156 | hashitem_T *hi = hash_find(&d->dv_hashtab, di->di_key); |
Bram Moolenaar | ef2c325 | 2022-11-25 16:31:51 +0000 | [diff] [blame] | 1157 | hash_remove(&d->dv_hashtab, hi, "Lua new index"); |
Bram Moolenaar | 1dced57 | 2012-04-05 16:54:08 +0200 | [diff] [blame] | 1158 | dictitem_free(di); |
| 1159 | } |
Bram Moolenaar | ca06da9 | 2018-07-01 15:12:05 +0200 | [diff] [blame] | 1160 | else |
Bram Moolenaar | e49b8e8 | 2020-07-01 13:52:55 +0200 | [diff] [blame] | 1161 | di->di_tv = tv; |
Bram Moolenaar | 1dced57 | 2012-04-05 16:54:08 +0200 | [diff] [blame] | 1162 | return 0; |
| 1163 | } |
| 1164 | |
| 1165 | static const luaL_Reg luaV_Dict_mt[] = { |
| 1166 | {"__tostring", luaV_dict_tostring}, |
Bram Moolenaar | 1dced57 | 2012-04-05 16:54:08 +0200 | [diff] [blame] | 1167 | {"__len", luaV_dict_len}, |
| 1168 | {"__call", luaV_dict_call}, |
| 1169 | {"__index", luaV_dict_index}, |
| 1170 | {"__newindex", luaV_dict_newindex}, |
| 1171 | {NULL, NULL} |
| 1172 | }; |
| 1173 | |
| 1174 | |
Bram Moolenaar | 2ab2e86 | 2019-12-04 21:24:53 +0100 | [diff] [blame] | 1175 | // ======= Blob type ======= |
Bram Moolenaar | b782869 | 2019-03-23 13:57:02 +0100 | [diff] [blame] | 1176 | |
| 1177 | static luaV_Blob * |
| 1178 | luaV_newblob(lua_State *L, blob_T *blo) |
| 1179 | { |
| 1180 | luaV_Blob *b = (luaV_Blob *) lua_newuserdata(L, sizeof(luaV_Blob)); |
| 1181 | *b = blo; |
Bram Moolenaar | 2ab2e86 | 2019-12-04 21:24:53 +0100 | [diff] [blame] | 1182 | blo->bv_refcount++; // reference in Lua |
| 1183 | luaV_setudata(L, blo); // cache[blo] = udata |
Bram Moolenaar | b782869 | 2019-03-23 13:57:02 +0100 | [diff] [blame] | 1184 | luaV_getfield(L, LUAVIM_BLOB); |
| 1185 | lua_setmetatable(L, -2); |
| 1186 | return b; |
| 1187 | } |
| 1188 | |
| 1189 | luaV_pushtype(blob_T, blob, luaV_Blob) |
| 1190 | luaV_type_tostring(blob, LUAVIM_BLOB) |
| 1191 | |
| 1192 | static int |
| 1193 | luaV_blob_gc(lua_State *L) |
| 1194 | { |
| 1195 | blob_T *b = luaV_unbox(L, luaV_Blob, 1); |
| 1196 | blob_unref(b); |
| 1197 | return 0; |
| 1198 | } |
| 1199 | |
| 1200 | static int |
| 1201 | luaV_blob_len(lua_State *L) |
| 1202 | { |
| 1203 | blob_T *b = luaV_unbox(L, luaV_Blob, 1); |
| 1204 | lua_pushinteger(L, (int) blob_len(b)); |
| 1205 | return 1; |
| 1206 | } |
| 1207 | |
| 1208 | static int |
| 1209 | luaV_blob_index(lua_State *L) |
| 1210 | { |
| 1211 | blob_T *b = luaV_unbox(L, luaV_Blob, 1); |
| 1212 | if (lua_isnumber(L, 2)) |
| 1213 | { |
| 1214 | int idx = luaL_checkinteger(L, 2); |
| 1215 | if (idx < blob_len(b)) |
| 1216 | lua_pushnumber(L, (lua_Number) blob_get(b, idx)); |
| 1217 | else |
| 1218 | lua_pushnil(L); |
| 1219 | } |
| 1220 | else if (lua_isstring(L, 2)) |
| 1221 | { |
| 1222 | const char *s = lua_tostring(L, 2); |
| 1223 | if (strncmp(s, "add", 3) == 0) |
| 1224 | { |
| 1225 | lua_getmetatable(L, 1); |
| 1226 | lua_getfield(L, -1, s); |
| 1227 | } |
| 1228 | else |
| 1229 | lua_pushnil(L); |
| 1230 | } |
| 1231 | else |
| 1232 | lua_pushnil(L); |
| 1233 | return 1; |
| 1234 | } |
| 1235 | |
| 1236 | static int |
| 1237 | luaV_blob_newindex(lua_State *L) |
| 1238 | { |
| 1239 | blob_T *b = luaV_unbox(L, luaV_Blob, 1); |
| 1240 | if (b->bv_lock) |
| 1241 | luaL_error(L, "blob is locked"); |
Yegappan Lakshmanan | 0233bdf | 2023-01-12 12:33:30 +0000 | [diff] [blame] | 1242 | |
| 1243 | if (!lua_isnumber(L, 2)) |
| 1244 | return 0; |
| 1245 | |
| 1246 | long len = blob_len(b); |
| 1247 | int idx = luaL_checkinteger(L, 2); |
| 1248 | int val = luaL_checkinteger(L, 3); |
| 1249 | if (idx < len || (idx == len && ga_grow(&b->bv_ga, 1) == OK)) |
Bram Moolenaar | b782869 | 2019-03-23 13:57:02 +0100 | [diff] [blame] | 1250 | { |
Yegappan Lakshmanan | 0233bdf | 2023-01-12 12:33:30 +0000 | [diff] [blame] | 1251 | blob_set(b, idx, (char_u) val); |
| 1252 | if (idx == len) |
| 1253 | ++b->bv_ga.ga_len; |
Bram Moolenaar | b782869 | 2019-03-23 13:57:02 +0100 | [diff] [blame] | 1254 | } |
Yegappan Lakshmanan | 0233bdf | 2023-01-12 12:33:30 +0000 | [diff] [blame] | 1255 | else |
| 1256 | luaL_error(L, "index out of range"); |
| 1257 | |
Bram Moolenaar | b782869 | 2019-03-23 13:57:02 +0100 | [diff] [blame] | 1258 | return 0; |
| 1259 | } |
| 1260 | |
| 1261 | static int |
| 1262 | luaV_blob_add(lua_State *L) |
| 1263 | { |
| 1264 | luaV_Blob *blo = luaV_checkudata(L, 1, LUAVIM_BLOB); |
| 1265 | blob_T *b = (blob_T *) luaV_checkcache(L, (void *) *blo); |
| 1266 | if (b->bv_lock) |
| 1267 | luaL_error(L, "blob is locked"); |
| 1268 | lua_settop(L, 2); |
| 1269 | if (!lua_isstring(L, 2)) |
| 1270 | luaL_error(L, "string expected, got %s", luaL_typename(L, 2)); |
| 1271 | else |
| 1272 | { |
| 1273 | size_t i, l = 0; |
| 1274 | const char *s = lua_tolstring(L, 2, &l); |
| 1275 | |
Bram Moolenaar | 5f1d3ae | 2020-02-11 22:37:35 +0100 | [diff] [blame] | 1276 | if (ga_grow(&b->bv_ga, (int)l) == OK) |
Bram Moolenaar | 6fb5c97 | 2019-03-26 21:44:20 +0100 | [diff] [blame] | 1277 | for (i = 0; i < l; ++i) |
| 1278 | ga_append(&b->bv_ga, s[i]); |
Bram Moolenaar | b782869 | 2019-03-23 13:57:02 +0100 | [diff] [blame] | 1279 | } |
| 1280 | lua_settop(L, 1); |
| 1281 | return 1; |
| 1282 | } |
| 1283 | |
| 1284 | static const luaL_Reg luaV_Blob_mt[] = { |
| 1285 | {"__tostring", luaV_blob_tostring}, |
| 1286 | {"__gc", luaV_blob_gc}, |
| 1287 | {"__len", luaV_blob_len}, |
| 1288 | {"__index", luaV_blob_index}, |
| 1289 | {"__newindex", luaV_blob_newindex}, |
| 1290 | {"add", luaV_blob_add}, |
| 1291 | {NULL, NULL} |
| 1292 | }; |
| 1293 | |
| 1294 | |
Bram Moolenaar | 2ab2e86 | 2019-12-04 21:24:53 +0100 | [diff] [blame] | 1295 | // ======= Funcref type ======= |
Bram Moolenaar | ca06da9 | 2018-07-01 15:12:05 +0200 | [diff] [blame] | 1296 | |
| 1297 | static luaV_Funcref * |
| 1298 | luaV_newfuncref(lua_State *L, char_u *name) |
| 1299 | { |
| 1300 | luaV_Funcref *f = (luaV_Funcref *)lua_newuserdata(L, sizeof(luaV_Funcref)); |
| 1301 | |
| 1302 | if (name != NULL) |
| 1303 | { |
Bram Moolenaar | 4eefe47 | 2019-03-19 21:59:19 +0100 | [diff] [blame] | 1304 | func_ref(name); |
| 1305 | f->name = vim_strsave(name); |
Bram Moolenaar | ca06da9 | 2018-07-01 15:12:05 +0200 | [diff] [blame] | 1306 | } |
Bram Moolenaar | ca06da9 | 2018-07-01 15:12:05 +0200 | [diff] [blame] | 1307 | f->self = NULL; |
| 1308 | luaV_getfield(L, LUAVIM_FUNCREF); |
| 1309 | lua_setmetatable(L, -2); |
| 1310 | return f; |
| 1311 | } |
| 1312 | |
| 1313 | static luaV_Funcref * |
Bram Moolenaar | 4eefe47 | 2019-03-19 21:59:19 +0100 | [diff] [blame] | 1314 | luaV_pushfuncref(lua_State *L, char_u *name) |
Bram Moolenaar | ca06da9 | 2018-07-01 15:12:05 +0200 | [diff] [blame] | 1315 | { |
Bram Moolenaar | 4eefe47 | 2019-03-19 21:59:19 +0100 | [diff] [blame] | 1316 | return luaV_newfuncref(L, name); |
Bram Moolenaar | ca06da9 | 2018-07-01 15:12:05 +0200 | [diff] [blame] | 1317 | } |
| 1318 | |
| 1319 | |
| 1320 | luaV_type_tostring(funcref, LUAVIM_FUNCREF) |
| 1321 | |
| 1322 | static int |
| 1323 | luaV_funcref_gc(lua_State *L) |
| 1324 | { |
| 1325 | luaV_Funcref *f = (luaV_Funcref *) lua_touserdata(L, 1); |
| 1326 | |
Bram Moolenaar | 4eefe47 | 2019-03-19 21:59:19 +0100 | [diff] [blame] | 1327 | func_unref(f->name); |
| 1328 | vim_free(f->name); |
| 1329 | // NOTE: Don't call "dict_unref(f->self)", because the dict of "f->self" |
| 1330 | // will be (or has been already) freed by Vim's garbage collection. |
Bram Moolenaar | ca06da9 | 2018-07-01 15:12:05 +0200 | [diff] [blame] | 1331 | return 0; |
| 1332 | } |
| 1333 | |
Bram Moolenaar | 2ab2e86 | 2019-12-04 21:24:53 +0100 | [diff] [blame] | 1334 | // equivalent to string(funcref) |
Bram Moolenaar | ca06da9 | 2018-07-01 15:12:05 +0200 | [diff] [blame] | 1335 | static int |
| 1336 | luaV_funcref_len(lua_State *L) |
| 1337 | { |
| 1338 | luaV_Funcref *f = (luaV_Funcref *) lua_touserdata(L, 1); |
| 1339 | |
Bram Moolenaar | 4eefe47 | 2019-03-19 21:59:19 +0100 | [diff] [blame] | 1340 | lua_pushstring(L, (const char *) f->name); |
Bram Moolenaar | ca06da9 | 2018-07-01 15:12:05 +0200 | [diff] [blame] | 1341 | return 1; |
| 1342 | } |
| 1343 | |
| 1344 | static int |
| 1345 | luaV_funcref_call(lua_State *L) |
| 1346 | { |
| 1347 | luaV_Funcref *f = (luaV_Funcref *) lua_touserdata(L, 1); |
Bram Moolenaar | 4eefe47 | 2019-03-19 21:59:19 +0100 | [diff] [blame] | 1348 | int i, n = lua_gettop(L) - 1; // #args |
| 1349 | int status = FAIL; |
| 1350 | typval_T args; |
| 1351 | typval_T rettv; |
Bram Moolenaar | ca06da9 | 2018-07-01 15:12:05 +0200 | [diff] [blame] | 1352 | |
Bram Moolenaar | 4eefe47 | 2019-03-19 21:59:19 +0100 | [diff] [blame] | 1353 | args.v_type = VAR_LIST; |
| 1354 | args.vval.v_list = list_alloc(); |
| 1355 | rettv.v_type = VAR_UNKNOWN; // as in clear_tv |
| 1356 | if (args.vval.v_list != NULL) |
Bram Moolenaar | d6ef5f9 | 2018-07-13 22:08:23 +0200 | [diff] [blame] | 1357 | { |
Bram Moolenaar | 4eefe47 | 2019-03-19 21:59:19 +0100 | [diff] [blame] | 1358 | typval_T v; |
| 1359 | |
Bram Moolenaar | 1741367 | 2018-07-14 20:49:42 +0200 | [diff] [blame] | 1360 | for (i = 0; i < n; i++) |
| 1361 | { |
Bram Moolenaar | d6ef5f9 | 2018-07-13 22:08:23 +0200 | [diff] [blame] | 1362 | luaV_checktypval(L, i + 2, &v, "calling funcref"); |
Bram Moolenaar | 4eefe47 | 2019-03-19 21:59:19 +0100 | [diff] [blame] | 1363 | list_append_tv(args.vval.v_list, &v); |
Bram Moolenaar | 713bf9e9 | 2019-03-16 16:38:41 +0100 | [diff] [blame] | 1364 | clear_tv(&v); |
Bram Moolenaar | d6ef5f9 | 2018-07-13 22:08:23 +0200 | [diff] [blame] | 1365 | } |
Bram Moolenaar | 4eefe47 | 2019-03-19 21:59:19 +0100 | [diff] [blame] | 1366 | status = func_call(f->name, &args, NULL, f->self, &rettv); |
Bram Moolenaar | d6ef5f9 | 2018-07-13 22:08:23 +0200 | [diff] [blame] | 1367 | if (status == OK) |
| 1368 | luaV_pushtypval(L, &rettv); |
Bram Moolenaar | 4eefe47 | 2019-03-19 21:59:19 +0100 | [diff] [blame] | 1369 | clear_tv(&args); |
Bram Moolenaar | d6ef5f9 | 2018-07-13 22:08:23 +0200 | [diff] [blame] | 1370 | clear_tv(&rettv); |
Bram Moolenaar | ca06da9 | 2018-07-01 15:12:05 +0200 | [diff] [blame] | 1371 | } |
Bram Moolenaar | ca06da9 | 2018-07-01 15:12:05 +0200 | [diff] [blame] | 1372 | if (status != OK) |
| 1373 | luaL_error(L, "cannot call funcref"); |
| 1374 | return 1; |
| 1375 | } |
| 1376 | |
| 1377 | static const luaL_Reg luaV_Funcref_mt[] = { |
| 1378 | {"__tostring", luaV_funcref_tostring}, |
| 1379 | {"__gc", luaV_funcref_gc}, |
| 1380 | {"__len", luaV_funcref_len}, |
| 1381 | {"__call", luaV_funcref_call}, |
| 1382 | {NULL, NULL} |
| 1383 | }; |
| 1384 | |
| 1385 | |
Bram Moolenaar | 2ab2e86 | 2019-12-04 21:24:53 +0100 | [diff] [blame] | 1386 | // ======= Buffer type ======= |
Bram Moolenaar | 1dced57 | 2012-04-05 16:54:08 +0200 | [diff] [blame] | 1387 | |
| 1388 | luaV_newtype(buf_T, buffer, luaV_Buffer, LUAVIM_BUFFER) |
| 1389 | luaV_pushtype(buf_T, buffer, luaV_Buffer) |
| 1390 | luaV_type_tostring(buffer, LUAVIM_BUFFER) |
| 1391 | |
| 1392 | static int |
Bram Moolenaar | 55d5c03 | 2010-07-17 23:52:29 +0200 | [diff] [blame] | 1393 | luaV_buffer_len(lua_State *L) |
| 1394 | { |
Bram Moolenaar | 1dced57 | 2012-04-05 16:54:08 +0200 | [diff] [blame] | 1395 | buf_T *b = (buf_T *) luaV_checkvalid(L, luaV_Buffer, 1); |
| 1396 | lua_pushinteger(L, b->b_ml.ml_line_count); |
Bram Moolenaar | 0ba0429 | 2010-07-14 23:23:17 +0200 | [diff] [blame] | 1397 | return 1; |
| 1398 | } |
| 1399 | |
Bram Moolenaar | 55d5c03 | 2010-07-17 23:52:29 +0200 | [diff] [blame] | 1400 | static int |
| 1401 | luaV_buffer_call(lua_State *L) |
| 1402 | { |
Bram Moolenaar | 1dced57 | 2012-04-05 16:54:08 +0200 | [diff] [blame] | 1403 | buf_T *b = (buf_T *) luaV_checkvalid(L, luaV_Buffer, 1); |
Bram Moolenaar | 0ba0429 | 2010-07-14 23:23:17 +0200 | [diff] [blame] | 1404 | lua_settop(L, 1); |
Bram Moolenaar | 1dced57 | 2012-04-05 16:54:08 +0200 | [diff] [blame] | 1405 | set_curbuf(b, DOBUF_SPLIT); |
Bram Moolenaar | 0ba0429 | 2010-07-14 23:23:17 +0200 | [diff] [blame] | 1406 | return 1; |
| 1407 | } |
| 1408 | |
Bram Moolenaar | 55d5c03 | 2010-07-17 23:52:29 +0200 | [diff] [blame] | 1409 | static int |
| 1410 | luaV_buffer_index(lua_State *L) |
| 1411 | { |
Bram Moolenaar | 1dced57 | 2012-04-05 16:54:08 +0200 | [diff] [blame] | 1412 | buf_T *b = (buf_T *) luaV_checkvalid(L, luaV_Buffer, 1); |
Bram Moolenaar | 0ba0429 | 2010-07-14 23:23:17 +0200 | [diff] [blame] | 1413 | linenr_T n = (linenr_T) lua_tointeger(L, 2); |
Bram Moolenaar | 1dced57 | 2012-04-05 16:54:08 +0200 | [diff] [blame] | 1414 | if (n > 0 && n <= b->b_ml.ml_line_count) |
| 1415 | luaV_pushline(L, b, n); |
Bram Moolenaar | 55d5c03 | 2010-07-17 23:52:29 +0200 | [diff] [blame] | 1416 | else if (lua_isstring(L, 2)) |
| 1417 | { |
| 1418 | const char *s = lua_tostring(L, 2); |
| 1419 | if (strncmp(s, "name", 4) == 0) |
Bram Moolenaar | fe08df4 | 2018-07-07 23:07:41 +0200 | [diff] [blame] | 1420 | lua_pushstring(L, (b->b_sfname == NULL) |
| 1421 | ? "" : (char *) b->b_sfname); |
Bram Moolenaar | 55d5c03 | 2010-07-17 23:52:29 +0200 | [diff] [blame] | 1422 | else if (strncmp(s, "fname", 5) == 0) |
Bram Moolenaar | fe08df4 | 2018-07-07 23:07:41 +0200 | [diff] [blame] | 1423 | lua_pushstring(L, (b->b_ffname == NULL) |
| 1424 | ? "" : (char *) b->b_ffname); |
Bram Moolenaar | 55d5c03 | 2010-07-17 23:52:29 +0200 | [diff] [blame] | 1425 | else if (strncmp(s, "number", 6) == 0) |
Bram Moolenaar | 1dced57 | 2012-04-05 16:54:08 +0200 | [diff] [blame] | 1426 | lua_pushinteger(L, b->b_fnum); |
Bram Moolenaar | 2ab2e86 | 2019-12-04 21:24:53 +0100 | [diff] [blame] | 1427 | // methods |
Bram Moolenaar | 55d5c03 | 2010-07-17 23:52:29 +0200 | [diff] [blame] | 1428 | else if (strncmp(s, "insert", 6) == 0 |
| 1429 | || strncmp(s, "next", 4) == 0 |
| 1430 | || strncmp(s, "previous", 8) == 0 |
| 1431 | || strncmp(s, "isvalid", 7) == 0) |
| 1432 | { |
| 1433 | lua_getmetatable(L, 1); |
| 1434 | lua_getfield(L, -1, s); |
| 1435 | } |
| 1436 | else |
| 1437 | lua_pushnil(L); |
Bram Moolenaar | 0ba0429 | 2010-07-14 23:23:17 +0200 | [diff] [blame] | 1438 | } |
Bram Moolenaar | 55d5c03 | 2010-07-17 23:52:29 +0200 | [diff] [blame] | 1439 | else |
| 1440 | lua_pushnil(L); |
Bram Moolenaar | 0ba0429 | 2010-07-14 23:23:17 +0200 | [diff] [blame] | 1441 | return 1; |
| 1442 | } |
| 1443 | |
Bram Moolenaar | 55d5c03 | 2010-07-17 23:52:29 +0200 | [diff] [blame] | 1444 | static int |
| 1445 | luaV_buffer_newindex(lua_State *L) |
| 1446 | { |
Bram Moolenaar | 1dced57 | 2012-04-05 16:54:08 +0200 | [diff] [blame] | 1447 | buf_T *b = (buf_T *) luaV_checkvalid(L, luaV_Buffer, 1); |
Bram Moolenaar | 0ba0429 | 2010-07-14 23:23:17 +0200 | [diff] [blame] | 1448 | linenr_T n = (linenr_T) luaL_checkinteger(L, 2); |
| 1449 | #ifdef HAVE_SANDBOX |
| 1450 | luaV_checksandbox(L); |
| 1451 | #endif |
Bram Moolenaar | 1dced57 | 2012-04-05 16:54:08 +0200 | [diff] [blame] | 1452 | if (n < 1 || n > b->b_ml.ml_line_count) |
Bram Moolenaar | 55d5c03 | 2010-07-17 23:52:29 +0200 | [diff] [blame] | 1453 | luaL_error(L, "invalid line number"); |
Bram Moolenaar | 2ab2e86 | 2019-12-04 21:24:53 +0100 | [diff] [blame] | 1454 | if (lua_isnil(L, 3)) // delete line |
Bram Moolenaar | 55d5c03 | 2010-07-17 23:52:29 +0200 | [diff] [blame] | 1455 | { |
| 1456 | buf_T *buf = curbuf; |
Bram Moolenaar | 1dced57 | 2012-04-05 16:54:08 +0200 | [diff] [blame] | 1457 | curbuf = b; |
Bram Moolenaar | 55d5c03 | 2010-07-17 23:52:29 +0200 | [diff] [blame] | 1458 | if (u_savedel(n, 1L) == FAIL) |
| 1459 | { |
| 1460 | curbuf = buf; |
| 1461 | luaL_error(L, "cannot save undo information"); |
| 1462 | } |
Bram Moolenaar | ca70c07 | 2020-05-30 20:30:46 +0200 | [diff] [blame] | 1463 | else if (ml_delete(n) == FAIL) |
Bram Moolenaar | 55d5c03 | 2010-07-17 23:52:29 +0200 | [diff] [blame] | 1464 | { |
| 1465 | curbuf = buf; |
| 1466 | luaL_error(L, "cannot delete line"); |
| 1467 | } |
Bram Moolenaar | ca06da9 | 2018-07-01 15:12:05 +0200 | [diff] [blame] | 1468 | else |
| 1469 | { |
Bram Moolenaar | 55d5c03 | 2010-07-17 23:52:29 +0200 | [diff] [blame] | 1470 | deleted_lines_mark(n, 1L); |
Bram Moolenaar | 2ab2e86 | 2019-12-04 21:24:53 +0100 | [diff] [blame] | 1471 | if (b == curwin->w_buffer) // fix cursor in current window? |
Bram Moolenaar | 55d5c03 | 2010-07-17 23:52:29 +0200 | [diff] [blame] | 1472 | { |
| 1473 | if (curwin->w_cursor.lnum >= n) |
| 1474 | { |
| 1475 | if (curwin->w_cursor.lnum > n) |
| 1476 | { |
| 1477 | curwin->w_cursor.lnum -= 1; |
| 1478 | check_cursor_col(); |
| 1479 | } |
Bram Moolenaar | e49b8e8 | 2020-07-01 13:52:55 +0200 | [diff] [blame] | 1480 | else |
| 1481 | check_cursor(); |
Bram Moolenaar | 55d5c03 | 2010-07-17 23:52:29 +0200 | [diff] [blame] | 1482 | changed_cline_bef_curs(); |
| 1483 | } |
| 1484 | invalidate_botline(); |
| 1485 | } |
| 1486 | } |
| 1487 | curbuf = buf; |
Bram Moolenaar | 0ba0429 | 2010-07-14 23:23:17 +0200 | [diff] [blame] | 1488 | } |
Bram Moolenaar | 2ab2e86 | 2019-12-04 21:24:53 +0100 | [diff] [blame] | 1489 | else if (lua_isstring(L, 3)) // update line |
Bram Moolenaar | 55d5c03 | 2010-07-17 23:52:29 +0200 | [diff] [blame] | 1490 | { |
| 1491 | buf_T *buf = curbuf; |
Bram Moolenaar | 1dced57 | 2012-04-05 16:54:08 +0200 | [diff] [blame] | 1492 | curbuf = b; |
Bram Moolenaar | 55d5c03 | 2010-07-17 23:52:29 +0200 | [diff] [blame] | 1493 | if (u_savesub(n) == FAIL) |
| 1494 | { |
| 1495 | curbuf = buf; |
| 1496 | luaL_error(L, "cannot save undo information"); |
| 1497 | } |
| 1498 | else if (ml_replace(n, luaV_toline(L, 3), TRUE) == FAIL) |
| 1499 | { |
| 1500 | curbuf = buf; |
| 1501 | luaL_error(L, "cannot replace line"); |
| 1502 | } |
Bram Moolenaar | 113d9de | 2022-08-08 15:49:18 +0100 | [diff] [blame] | 1503 | else |
| 1504 | changed_bytes(n, 0); |
Bram Moolenaar | 55d5c03 | 2010-07-17 23:52:29 +0200 | [diff] [blame] | 1505 | curbuf = buf; |
Bram Moolenaar | 1dced57 | 2012-04-05 16:54:08 +0200 | [diff] [blame] | 1506 | if (b == curwin->w_buffer) |
Bram Moolenaar | 55d5c03 | 2010-07-17 23:52:29 +0200 | [diff] [blame] | 1507 | check_cursor_col(); |
Bram Moolenaar | 0ba0429 | 2010-07-14 23:23:17 +0200 | [diff] [blame] | 1508 | } |
| 1509 | else |
Bram Moolenaar | 55d5c03 | 2010-07-17 23:52:29 +0200 | [diff] [blame] | 1510 | luaL_error(L, "wrong argument to change line"); |
Bram Moolenaar | 0ba0429 | 2010-07-14 23:23:17 +0200 | [diff] [blame] | 1511 | return 0; |
| 1512 | } |
| 1513 | |
Bram Moolenaar | 55d5c03 | 2010-07-17 23:52:29 +0200 | [diff] [blame] | 1514 | static int |
| 1515 | luaV_buffer_insert(lua_State *L) |
| 1516 | { |
Bram Moolenaar | 1dced57 | 2012-04-05 16:54:08 +0200 | [diff] [blame] | 1517 | luaV_Buffer *lb = luaV_checkudata(L, 1, LUAVIM_BUFFER); |
| 1518 | buf_T *b = (buf_T *) luaV_checkcache(L, (void *) *lb); |
| 1519 | linenr_T last = b->b_ml.ml_line_count; |
Bram Moolenaar | 0ba0429 | 2010-07-14 23:23:17 +0200 | [diff] [blame] | 1520 | linenr_T n = (linenr_T) luaL_optinteger(L, 3, last); |
| 1521 | buf_T *buf; |
| 1522 | luaL_checktype(L, 2, LUA_TSTRING); |
| 1523 | #ifdef HAVE_SANDBOX |
| 1524 | luaV_checksandbox(L); |
| 1525 | #endif |
Bram Moolenaar | 2ab2e86 | 2019-12-04 21:24:53 +0100 | [diff] [blame] | 1526 | // fix insertion line |
Bram Moolenaar | 0ba0429 | 2010-07-14 23:23:17 +0200 | [diff] [blame] | 1527 | if (n < 0) n = 0; |
| 1528 | if (n > last) n = last; |
Bram Moolenaar | 2ab2e86 | 2019-12-04 21:24:53 +0100 | [diff] [blame] | 1529 | // insert |
Bram Moolenaar | 0ba0429 | 2010-07-14 23:23:17 +0200 | [diff] [blame] | 1530 | buf = curbuf; |
Bram Moolenaar | 1dced57 | 2012-04-05 16:54:08 +0200 | [diff] [blame] | 1531 | curbuf = b; |
Bram Moolenaar | 55d5c03 | 2010-07-17 23:52:29 +0200 | [diff] [blame] | 1532 | if (u_save(n, n + 1) == FAIL) |
| 1533 | { |
| 1534 | curbuf = buf; |
| 1535 | luaL_error(L, "cannot save undo information"); |
Bram Moolenaar | 0ba0429 | 2010-07-14 23:23:17 +0200 | [diff] [blame] | 1536 | } |
Bram Moolenaar | 55d5c03 | 2010-07-17 23:52:29 +0200 | [diff] [blame] | 1537 | else if (ml_append(n, luaV_toline(L, 2), 0, FALSE) == FAIL) |
| 1538 | { |
| 1539 | curbuf = buf; |
| 1540 | luaL_error(L, "cannot insert line"); |
Bram Moolenaar | 0ba0429 | 2010-07-14 23:23:17 +0200 | [diff] [blame] | 1541 | } |
| 1542 | else |
Bram Moolenaar | 55d5c03 | 2010-07-17 23:52:29 +0200 | [diff] [blame] | 1543 | appended_lines_mark(n, 1L); |
Bram Moolenaar | 0ba0429 | 2010-07-14 23:23:17 +0200 | [diff] [blame] | 1544 | curbuf = buf; |
Bram Moolenaar | a4d158b | 2022-08-14 14:17:45 +0100 | [diff] [blame] | 1545 | update_screen(UPD_VALID); |
Bram Moolenaar | 0ba0429 | 2010-07-14 23:23:17 +0200 | [diff] [blame] | 1546 | return 0; |
| 1547 | } |
| 1548 | |
Bram Moolenaar | 55d5c03 | 2010-07-17 23:52:29 +0200 | [diff] [blame] | 1549 | static int |
| 1550 | luaV_buffer_next(lua_State *L) |
| 1551 | { |
Bram Moolenaar | 0ba0429 | 2010-07-14 23:23:17 +0200 | [diff] [blame] | 1552 | luaV_Buffer *b = luaV_checkudata(L, 1, LUAVIM_BUFFER); |
Bram Moolenaar | 1dced57 | 2012-04-05 16:54:08 +0200 | [diff] [blame] | 1553 | buf_T *buf = (buf_T *) luaV_checkcache(L, (void *) *b); |
| 1554 | luaV_pushbuffer(L, buf->b_next); |
Bram Moolenaar | 0ba0429 | 2010-07-14 23:23:17 +0200 | [diff] [blame] | 1555 | return 1; |
| 1556 | } |
| 1557 | |
Bram Moolenaar | 55d5c03 | 2010-07-17 23:52:29 +0200 | [diff] [blame] | 1558 | static int |
| 1559 | luaV_buffer_previous(lua_State *L) |
| 1560 | { |
Bram Moolenaar | 0ba0429 | 2010-07-14 23:23:17 +0200 | [diff] [blame] | 1561 | luaV_Buffer *b = luaV_checkudata(L, 1, LUAVIM_BUFFER); |
Bram Moolenaar | 1dced57 | 2012-04-05 16:54:08 +0200 | [diff] [blame] | 1562 | buf_T *buf = (buf_T *) luaV_checkcache(L, (void *) *b); |
| 1563 | luaV_pushbuffer(L, buf->b_prev); |
Bram Moolenaar | 0ba0429 | 2010-07-14 23:23:17 +0200 | [diff] [blame] | 1564 | return 1; |
| 1565 | } |
| 1566 | |
Bram Moolenaar | 55d5c03 | 2010-07-17 23:52:29 +0200 | [diff] [blame] | 1567 | static int |
| 1568 | luaV_buffer_isvalid(lua_State *L) |
| 1569 | { |
Bram Moolenaar | 0ba0429 | 2010-07-14 23:23:17 +0200 | [diff] [blame] | 1570 | luaV_Buffer *b = luaV_checkudata(L, 1, LUAVIM_BUFFER); |
Bram Moolenaar | 1dced57 | 2012-04-05 16:54:08 +0200 | [diff] [blame] | 1571 | luaV_getudata(L, *b); |
Bram Moolenaar | 0ba0429 | 2010-07-14 23:23:17 +0200 | [diff] [blame] | 1572 | lua_pushboolean(L, !lua_isnil(L, -1)); |
| 1573 | return 1; |
| 1574 | } |
| 1575 | |
| 1576 | static const luaL_Reg luaV_Buffer_mt[] = { |
| 1577 | {"__tostring", luaV_buffer_tostring}, |
| 1578 | {"__len", luaV_buffer_len}, |
| 1579 | {"__call", luaV_buffer_call}, |
| 1580 | {"__index", luaV_buffer_index}, |
| 1581 | {"__newindex", luaV_buffer_newindex}, |
| 1582 | {"insert", luaV_buffer_insert}, |
| 1583 | {"next", luaV_buffer_next}, |
| 1584 | {"previous", luaV_buffer_previous}, |
| 1585 | {"isvalid", luaV_buffer_isvalid}, |
| 1586 | {NULL, NULL} |
| 1587 | }; |
| 1588 | |
| 1589 | |
Bram Moolenaar | 2ab2e86 | 2019-12-04 21:24:53 +0100 | [diff] [blame] | 1590 | // ======= Window type ======= |
Bram Moolenaar | 0ba0429 | 2010-07-14 23:23:17 +0200 | [diff] [blame] | 1591 | |
Bram Moolenaar | 1dced57 | 2012-04-05 16:54:08 +0200 | [diff] [blame] | 1592 | luaV_newtype(win_T, window, luaV_Window, LUAVIM_WINDOW) |
| 1593 | luaV_pushtype(win_T, window, luaV_Window) |
| 1594 | luaV_type_tostring(window, LUAVIM_WINDOW) |
Bram Moolenaar | 0ba0429 | 2010-07-14 23:23:17 +0200 | [diff] [blame] | 1595 | |
Bram Moolenaar | 55d5c03 | 2010-07-17 23:52:29 +0200 | [diff] [blame] | 1596 | static int |
| 1597 | luaV_window_call(lua_State *L) |
| 1598 | { |
Bram Moolenaar | 1dced57 | 2012-04-05 16:54:08 +0200 | [diff] [blame] | 1599 | win_T *w = (win_T *) luaV_checkvalid(L, luaV_Window, 1); |
Bram Moolenaar | 0ba0429 | 2010-07-14 23:23:17 +0200 | [diff] [blame] | 1600 | lua_settop(L, 1); |
Bram Moolenaar | 1dced57 | 2012-04-05 16:54:08 +0200 | [diff] [blame] | 1601 | win_goto(w); |
Bram Moolenaar | 0ba0429 | 2010-07-14 23:23:17 +0200 | [diff] [blame] | 1602 | return 1; |
| 1603 | } |
| 1604 | |
Bram Moolenaar | 55d5c03 | 2010-07-17 23:52:29 +0200 | [diff] [blame] | 1605 | static int |
| 1606 | luaV_window_index(lua_State *L) |
| 1607 | { |
Bram Moolenaar | 1dced57 | 2012-04-05 16:54:08 +0200 | [diff] [blame] | 1608 | win_T *w = (win_T *) luaV_checkvalid(L, luaV_Window, 1); |
Bram Moolenaar | 0ba0429 | 2010-07-14 23:23:17 +0200 | [diff] [blame] | 1609 | const char *s = luaL_checkstring(L, 2); |
| 1610 | if (strncmp(s, "buffer", 6) == 0) |
Bram Moolenaar | 1dced57 | 2012-04-05 16:54:08 +0200 | [diff] [blame] | 1611 | luaV_pushbuffer(L, w->w_buffer); |
Bram Moolenaar | 0ba0429 | 2010-07-14 23:23:17 +0200 | [diff] [blame] | 1612 | else if (strncmp(s, "line", 4) == 0) |
Bram Moolenaar | 1dced57 | 2012-04-05 16:54:08 +0200 | [diff] [blame] | 1613 | lua_pushinteger(L, w->w_cursor.lnum); |
Bram Moolenaar | 0ba0429 | 2010-07-14 23:23:17 +0200 | [diff] [blame] | 1614 | else if (strncmp(s, "col", 3) == 0) |
Bram Moolenaar | 1dced57 | 2012-04-05 16:54:08 +0200 | [diff] [blame] | 1615 | lua_pushinteger(L, w->w_cursor.col + 1); |
Bram Moolenaar | 0ba0429 | 2010-07-14 23:23:17 +0200 | [diff] [blame] | 1616 | else if (strncmp(s, "width", 5) == 0) |
Bram Moolenaar | 0263146 | 2017-09-22 15:20:32 +0200 | [diff] [blame] | 1617 | lua_pushinteger(L, w->w_width); |
Bram Moolenaar | 0ba0429 | 2010-07-14 23:23:17 +0200 | [diff] [blame] | 1618 | else if (strncmp(s, "height", 6) == 0) |
Bram Moolenaar | 1dced57 | 2012-04-05 16:54:08 +0200 | [diff] [blame] | 1619 | lua_pushinteger(L, w->w_height); |
Bram Moolenaar | 2ab2e86 | 2019-12-04 21:24:53 +0100 | [diff] [blame] | 1620 | // methods |
Bram Moolenaar | 0ba0429 | 2010-07-14 23:23:17 +0200 | [diff] [blame] | 1621 | else if (strncmp(s, "next", 4) == 0 |
Bram Moolenaar | 55d5c03 | 2010-07-17 23:52:29 +0200 | [diff] [blame] | 1622 | || strncmp(s, "previous", 8) == 0 |
| 1623 | || strncmp(s, "isvalid", 7) == 0) |
| 1624 | { |
| 1625 | lua_getmetatable(L, 1); |
| 1626 | lua_getfield(L, -1, s); |
Bram Moolenaar | 0ba0429 | 2010-07-14 23:23:17 +0200 | [diff] [blame] | 1627 | } |
| 1628 | else |
Bram Moolenaar | 55d5c03 | 2010-07-17 23:52:29 +0200 | [diff] [blame] | 1629 | lua_pushnil(L); |
Bram Moolenaar | 0ba0429 | 2010-07-14 23:23:17 +0200 | [diff] [blame] | 1630 | return 1; |
| 1631 | } |
| 1632 | |
Bram Moolenaar | 55d5c03 | 2010-07-17 23:52:29 +0200 | [diff] [blame] | 1633 | static int |
Bram Moolenaar | 4eefe47 | 2019-03-19 21:59:19 +0100 | [diff] [blame] | 1634 | luaV_window_newindex(lua_State *L) |
Bram Moolenaar | 55d5c03 | 2010-07-17 23:52:29 +0200 | [diff] [blame] | 1635 | { |
Bram Moolenaar | 1dced57 | 2012-04-05 16:54:08 +0200 | [diff] [blame] | 1636 | win_T *w = (win_T *) luaV_checkvalid(L, luaV_Window, 1); |
Bram Moolenaar | 0ba0429 | 2010-07-14 23:23:17 +0200 | [diff] [blame] | 1637 | const char *s = luaL_checkstring(L, 2); |
| 1638 | int v = luaL_checkinteger(L, 3); |
Bram Moolenaar | 55d5c03 | 2010-07-17 23:52:29 +0200 | [diff] [blame] | 1639 | if (strncmp(s, "line", 4) == 0) |
| 1640 | { |
Bram Moolenaar | 0ba0429 | 2010-07-14 23:23:17 +0200 | [diff] [blame] | 1641 | #ifdef HAVE_SANDBOX |
Bram Moolenaar | 55d5c03 | 2010-07-17 23:52:29 +0200 | [diff] [blame] | 1642 | luaV_checksandbox(L); |
Bram Moolenaar | 0ba0429 | 2010-07-14 23:23:17 +0200 | [diff] [blame] | 1643 | #endif |
Bram Moolenaar | 1dced57 | 2012-04-05 16:54:08 +0200 | [diff] [blame] | 1644 | if (v < 1 || v > w->w_buffer->b_ml.ml_line_count) |
Bram Moolenaar | 55d5c03 | 2010-07-17 23:52:29 +0200 | [diff] [blame] | 1645 | luaL_error(L, "line out of range"); |
Bram Moolenaar | 1dced57 | 2012-04-05 16:54:08 +0200 | [diff] [blame] | 1646 | w->w_cursor.lnum = v; |
Bram Moolenaar | a4d158b | 2022-08-14 14:17:45 +0100 | [diff] [blame] | 1647 | update_screen(UPD_VALID); |
Bram Moolenaar | 0ba0429 | 2010-07-14 23:23:17 +0200 | [diff] [blame] | 1648 | } |
Bram Moolenaar | 55d5c03 | 2010-07-17 23:52:29 +0200 | [diff] [blame] | 1649 | else if (strncmp(s, "col", 3) == 0) |
| 1650 | { |
Bram Moolenaar | 0ba0429 | 2010-07-14 23:23:17 +0200 | [diff] [blame] | 1651 | #ifdef HAVE_SANDBOX |
Bram Moolenaar | 55d5c03 | 2010-07-17 23:52:29 +0200 | [diff] [blame] | 1652 | luaV_checksandbox(L); |
Bram Moolenaar | 0ba0429 | 2010-07-14 23:23:17 +0200 | [diff] [blame] | 1653 | #endif |
Bram Moolenaar | 1dced57 | 2012-04-05 16:54:08 +0200 | [diff] [blame] | 1654 | w->w_cursor.col = v - 1; |
Bram Moolenaar | 5390144 | 2018-07-25 22:02:36 +0200 | [diff] [blame] | 1655 | w->w_set_curswant = TRUE; |
Bram Moolenaar | a4d158b | 2022-08-14 14:17:45 +0100 | [diff] [blame] | 1656 | update_screen(UPD_VALID); |
Bram Moolenaar | 0ba0429 | 2010-07-14 23:23:17 +0200 | [diff] [blame] | 1657 | } |
Bram Moolenaar | 55d5c03 | 2010-07-17 23:52:29 +0200 | [diff] [blame] | 1658 | else if (strncmp(s, "width", 5) == 0) |
| 1659 | { |
| 1660 | win_T *win = curwin; |
Bram Moolenaar | 0ba0429 | 2010-07-14 23:23:17 +0200 | [diff] [blame] | 1661 | #ifdef FEAT_GUI |
Bram Moolenaar | 55d5c03 | 2010-07-17 23:52:29 +0200 | [diff] [blame] | 1662 | need_mouse_correct = TRUE; |
Bram Moolenaar | 0ba0429 | 2010-07-14 23:23:17 +0200 | [diff] [blame] | 1663 | #endif |
Bram Moolenaar | 1dced57 | 2012-04-05 16:54:08 +0200 | [diff] [blame] | 1664 | curwin = w; |
Bram Moolenaar | 55d5c03 | 2010-07-17 23:52:29 +0200 | [diff] [blame] | 1665 | win_setwidth(v); |
| 1666 | curwin = win; |
Bram Moolenaar | 0ba0429 | 2010-07-14 23:23:17 +0200 | [diff] [blame] | 1667 | } |
Bram Moolenaar | 55d5c03 | 2010-07-17 23:52:29 +0200 | [diff] [blame] | 1668 | else if (strncmp(s, "height", 6) == 0) |
| 1669 | { |
| 1670 | win_T *win = curwin; |
Bram Moolenaar | 0ba0429 | 2010-07-14 23:23:17 +0200 | [diff] [blame] | 1671 | #ifdef FEAT_GUI |
Bram Moolenaar | 55d5c03 | 2010-07-17 23:52:29 +0200 | [diff] [blame] | 1672 | need_mouse_correct = TRUE; |
Bram Moolenaar | 0ba0429 | 2010-07-14 23:23:17 +0200 | [diff] [blame] | 1673 | #endif |
Bram Moolenaar | 1dced57 | 2012-04-05 16:54:08 +0200 | [diff] [blame] | 1674 | curwin = w; |
Bram Moolenaar | 55d5c03 | 2010-07-17 23:52:29 +0200 | [diff] [blame] | 1675 | win_setheight(v); |
| 1676 | curwin = win; |
Bram Moolenaar | 0ba0429 | 2010-07-14 23:23:17 +0200 | [diff] [blame] | 1677 | } |
| 1678 | else |
Bram Moolenaar | 55d5c03 | 2010-07-17 23:52:29 +0200 | [diff] [blame] | 1679 | luaL_error(L, "invalid window property: `%s'", s); |
Bram Moolenaar | 0ba0429 | 2010-07-14 23:23:17 +0200 | [diff] [blame] | 1680 | return 0; |
| 1681 | } |
| 1682 | |
Bram Moolenaar | 55d5c03 | 2010-07-17 23:52:29 +0200 | [diff] [blame] | 1683 | static int |
| 1684 | luaV_window_next(lua_State *L) |
| 1685 | { |
Bram Moolenaar | 0ba0429 | 2010-07-14 23:23:17 +0200 | [diff] [blame] | 1686 | luaV_Window *w = luaV_checkudata(L, 1, LUAVIM_WINDOW); |
Bram Moolenaar | 1dced57 | 2012-04-05 16:54:08 +0200 | [diff] [blame] | 1687 | win_T *win = (win_T *) luaV_checkcache(L, (void *) *w); |
| 1688 | luaV_pushwindow(L, win->w_next); |
Bram Moolenaar | 0ba0429 | 2010-07-14 23:23:17 +0200 | [diff] [blame] | 1689 | return 1; |
| 1690 | } |
| 1691 | |
Bram Moolenaar | 55d5c03 | 2010-07-17 23:52:29 +0200 | [diff] [blame] | 1692 | static int |
| 1693 | luaV_window_previous(lua_State *L) |
| 1694 | { |
Bram Moolenaar | 0ba0429 | 2010-07-14 23:23:17 +0200 | [diff] [blame] | 1695 | luaV_Window *w = luaV_checkudata(L, 1, LUAVIM_WINDOW); |
Bram Moolenaar | 1dced57 | 2012-04-05 16:54:08 +0200 | [diff] [blame] | 1696 | win_T *win = (win_T *) luaV_checkcache(L, (void *) *w); |
| 1697 | luaV_pushwindow(L, win->w_prev); |
Bram Moolenaar | 0ba0429 | 2010-07-14 23:23:17 +0200 | [diff] [blame] | 1698 | return 1; |
| 1699 | } |
| 1700 | |
Bram Moolenaar | 55d5c03 | 2010-07-17 23:52:29 +0200 | [diff] [blame] | 1701 | static int |
| 1702 | luaV_window_isvalid(lua_State *L) |
| 1703 | { |
Bram Moolenaar | 0ba0429 | 2010-07-14 23:23:17 +0200 | [diff] [blame] | 1704 | luaV_Window *w = luaV_checkudata(L, 1, LUAVIM_WINDOW); |
Bram Moolenaar | 1dced57 | 2012-04-05 16:54:08 +0200 | [diff] [blame] | 1705 | luaV_getudata(L, *w); |
Bram Moolenaar | 0ba0429 | 2010-07-14 23:23:17 +0200 | [diff] [blame] | 1706 | lua_pushboolean(L, !lua_isnil(L, -1)); |
| 1707 | return 1; |
| 1708 | } |
| 1709 | |
| 1710 | static const luaL_Reg luaV_Window_mt[] = { |
| 1711 | {"__tostring", luaV_window_tostring}, |
| 1712 | {"__call", luaV_window_call}, |
| 1713 | {"__index", luaV_window_index}, |
| 1714 | {"__newindex", luaV_window_newindex}, |
| 1715 | {"next", luaV_window_next}, |
| 1716 | {"previous", luaV_window_previous}, |
| 1717 | {"isvalid", luaV_window_isvalid}, |
| 1718 | {NULL, NULL} |
| 1719 | }; |
| 1720 | |
| 1721 | |
Bram Moolenaar | 2ab2e86 | 2019-12-04 21:24:53 +0100 | [diff] [blame] | 1722 | // ======= Vim module ======= |
Bram Moolenaar | 0ba0429 | 2010-07-14 23:23:17 +0200 | [diff] [blame] | 1723 | |
Bram Moolenaar | 55d5c03 | 2010-07-17 23:52:29 +0200 | [diff] [blame] | 1724 | static int |
| 1725 | luaV_print(lua_State *L) |
| 1726 | { |
Yegappan Lakshmanan | 41114a2 | 2021-07-29 20:22:14 +0200 | [diff] [blame] | 1727 | int i, n = lua_gettop(L); // nargs |
| 1728 | const char *s; |
| 1729 | size_t l; |
| 1730 | garray_T msg_ga; |
| 1731 | |
| 1732 | ga_init2(&msg_ga, 1, 128); |
Bram Moolenaar | 0ba0429 | 2010-07-14 23:23:17 +0200 | [diff] [blame] | 1733 | lua_getglobal(L, "tostring"); |
Bram Moolenaar | 55d5c03 | 2010-07-17 23:52:29 +0200 | [diff] [blame] | 1734 | for (i = 1; i <= n; i++) |
| 1735 | { |
Bram Moolenaar | 2ab2e86 | 2019-12-04 21:24:53 +0100 | [diff] [blame] | 1736 | lua_pushvalue(L, -1); // tostring |
| 1737 | lua_pushvalue(L, i); // arg |
Bram Moolenaar | 55d5c03 | 2010-07-17 23:52:29 +0200 | [diff] [blame] | 1738 | lua_call(L, 1, 1); |
| 1739 | s = lua_tolstring(L, -1, &l); |
| 1740 | if (s == NULL) |
| 1741 | return luaL_error(L, "cannot convert to string"); |
Bram Moolenaar | 78e006b | 2021-07-28 15:07:01 +0200 | [diff] [blame] | 1742 | if (i > 1) |
Yegappan Lakshmanan | 41114a2 | 2021-07-29 20:22:14 +0200 | [diff] [blame] | 1743 | ga_append(&msg_ga, ' '); // use space instead of tab |
| 1744 | ga_concat_len(&msg_ga, (char_u *)s, l); |
Bram Moolenaar | 2a4bd00 | 2021-07-28 21:48:59 +0200 | [diff] [blame] | 1745 | lua_pop(L, 1); |
Bram Moolenaar | 0ba0429 | 2010-07-14 23:23:17 +0200 | [diff] [blame] | 1746 | } |
Yegappan Lakshmanan | 41114a2 | 2021-07-29 20:22:14 +0200 | [diff] [blame] | 1747 | // Replace any "\n" with "\0" |
| 1748 | for (i = 0; i < msg_ga.ga_len; i++) |
| 1749 | if (((char *)msg_ga.ga_data)[i] == '\n') |
| 1750 | ((char *)msg_ga.ga_data)[i] = '\0'; |
| 1751 | lua_pushlstring(L, msg_ga.ga_data, msg_ga.ga_len); |
Bram Moolenaar | b98678a | 2019-10-19 15:18:44 +0200 | [diff] [blame] | 1752 | if (!got_int) |
| 1753 | luaV_msg(L); |
Yegappan Lakshmanan | 41114a2 | 2021-07-29 20:22:14 +0200 | [diff] [blame] | 1754 | |
| 1755 | ga_clear(&msg_ga); |
Bram Moolenaar | 0ba0429 | 2010-07-14 23:23:17 +0200 | [diff] [blame] | 1756 | return 0; |
| 1757 | } |
| 1758 | |
Bram Moolenaar | 55d5c03 | 2010-07-17 23:52:29 +0200 | [diff] [blame] | 1759 | static int |
Bram Moolenaar | 38e2b06 | 2011-09-21 17:15:39 +0200 | [diff] [blame] | 1760 | luaV_debug(lua_State *L) |
| 1761 | { |
| 1762 | lua_settop(L, 0); |
| 1763 | lua_getglobal(L, "vim"); |
| 1764 | lua_getfield(L, -1, "eval"); |
Bram Moolenaar | 2ab2e86 | 2019-12-04 21:24:53 +0100 | [diff] [blame] | 1765 | lua_remove(L, -2); // vim.eval at position 1 |
Bram Moolenaar | 38e2b06 | 2011-09-21 17:15:39 +0200 | [diff] [blame] | 1766 | for (;;) |
| 1767 | { |
| 1768 | const char *input; |
| 1769 | size_t l; |
Bram Moolenaar | 2ab2e86 | 2019-12-04 21:24:53 +0100 | [diff] [blame] | 1770 | lua_pushvalue(L, 1); // vim.eval |
Bram Moolenaar | 38e2b06 | 2011-09-21 17:15:39 +0200 | [diff] [blame] | 1771 | lua_pushliteral(L, "input('lua_debug> ')"); |
Bram Moolenaar | 2ab2e86 | 2019-12-04 21:24:53 +0100 | [diff] [blame] | 1772 | lua_call(L, 1, 1); // return string |
Bram Moolenaar | 38e2b06 | 2011-09-21 17:15:39 +0200 | [diff] [blame] | 1773 | input = lua_tolstring(L, -1, &l); |
| 1774 | if (l == 0 || strcmp(input, "cont") == 0) |
| 1775 | return 0; |
Bram Moolenaar | 2ab2e86 | 2019-12-04 21:24:53 +0100 | [diff] [blame] | 1776 | msg_putchar('\n'); // avoid outputting on input line |
Bram Moolenaar | 38e2b06 | 2011-09-21 17:15:39 +0200 | [diff] [blame] | 1777 | if (luaL_loadbuffer(L, input, l, "=(debug command)") |
| 1778 | || lua_pcall(L, 0, 0, 0)) |
| 1779 | luaV_emsg(L); |
Bram Moolenaar | 2ab2e86 | 2019-12-04 21:24:53 +0100 | [diff] [blame] | 1780 | lua_settop(L, 1); // remove eventual returns, but keep vim.eval |
Bram Moolenaar | 38e2b06 | 2011-09-21 17:15:39 +0200 | [diff] [blame] | 1781 | } |
| 1782 | } |
| 1783 | |
Yegappan Lakshmanan | 9dc4bef | 2021-08-04 21:12:52 +0200 | [diff] [blame] | 1784 | static dict_T * |
| 1785 | luaV_get_var_scope(lua_State *L) |
| 1786 | { |
| 1787 | const char *scope = luaL_checkstring(L, 1); |
| 1788 | dict_T *dict = NULL; |
| 1789 | |
| 1790 | if (STRICMP((char *)scope, "g") == 0) |
| 1791 | dict = get_globvar_dict(); |
| 1792 | else if (STRICMP((char *)scope, "v") == 0) |
| 1793 | dict = get_vimvar_dict(); |
| 1794 | else if (STRICMP((char *)scope, "b") == 0) |
| 1795 | dict = curbuf->b_vars; |
| 1796 | else if (STRICMP((char *)scope, "w") == 0) |
| 1797 | dict = curwin->w_vars; |
| 1798 | else if (STRICMP((char *)scope, "t") == 0) |
| 1799 | dict = curtab->tp_vars; |
| 1800 | else |
| 1801 | { |
| 1802 | luaL_error(L, "invalid scope %s", scope); |
| 1803 | return NULL; |
| 1804 | } |
| 1805 | |
| 1806 | return dict; |
| 1807 | } |
| 1808 | |
| 1809 | static int |
| 1810 | luaV_setvar(lua_State *L) |
| 1811 | { |
| 1812 | dict_T *dict; |
| 1813 | dictitem_T *di; |
| 1814 | size_t len; |
| 1815 | char *name; |
| 1816 | int del; |
| 1817 | char *error = NULL; |
| 1818 | |
| 1819 | name = (char *)luaL_checklstring(L, 3, &len); |
| 1820 | del = (lua_gettop(L) < 4) || lua_isnil(L, 4); |
| 1821 | |
| 1822 | dict = luaV_get_var_scope(L); |
| 1823 | if (dict == NULL) |
| 1824 | return 0; |
| 1825 | |
Yegappan Lakshmanan | 11328bc | 2021-08-06 21:34:38 +0200 | [diff] [blame] | 1826 | di = dict_find(dict, (char_u *)name, (int)len); |
Yegappan Lakshmanan | 9dc4bef | 2021-08-04 21:12:52 +0200 | [diff] [blame] | 1827 | if (di != NULL) |
| 1828 | { |
| 1829 | if (di->di_flags & DI_FLAGS_RO) |
| 1830 | error = "variable is read-only"; |
| 1831 | else if (di->di_flags & DI_FLAGS_LOCK) |
| 1832 | error = "variable is locked"; |
| 1833 | else if (del && di->di_flags & DI_FLAGS_FIX) |
| 1834 | error = "variable is fixed"; |
| 1835 | if (error != NULL) |
| 1836 | return luaL_error(L, error); |
| 1837 | } |
| 1838 | else if (dict->dv_lock) |
| 1839 | return luaL_error(L, "Dictionary is locked"); |
| 1840 | |
| 1841 | if (del) |
| 1842 | { |
| 1843 | // Delete the key |
| 1844 | if (di == NULL) |
| 1845 | // Doesn't exist, nothing to do |
| 1846 | return 0; |
Bram Moolenaar | ef2c325 | 2022-11-25 16:31:51 +0000 | [diff] [blame] | 1847 | // Delete the entry |
| 1848 | dictitem_remove(dict, di, "Lua delete variable"); |
Yegappan Lakshmanan | 9dc4bef | 2021-08-04 21:12:52 +0200 | [diff] [blame] | 1849 | } |
| 1850 | else |
| 1851 | { |
| 1852 | // Update the key |
| 1853 | typval_T tv; |
| 1854 | |
h-east | 965d2ed | 2021-10-04 21:51:57 +0100 | [diff] [blame] | 1855 | // 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] | 1856 | lua_pushvalue(L, 4); |
| 1857 | if (luaV_totypval(L, -1, &tv) == FAIL) |
| 1858 | return luaL_error(L, "Couldn't convert lua value"); |
| 1859 | |
| 1860 | if (di == NULL) |
| 1861 | { |
| 1862 | // Need to create an entry |
| 1863 | di = dictitem_alloc((char_u *)name); |
| 1864 | if (di == NULL) |
Bram Moolenaar | 1b6acf0 | 2021-08-05 16:47:08 +0200 | [diff] [blame] | 1865 | { |
| 1866 | clear_tv(&tv); |
Yegappan Lakshmanan | 9dc4bef | 2021-08-04 21:12:52 +0200 | [diff] [blame] | 1867 | return 0; |
Bram Moolenaar | 1b6acf0 | 2021-08-05 16:47:08 +0200 | [diff] [blame] | 1868 | } |
Yegappan Lakshmanan | 9dc4bef | 2021-08-04 21:12:52 +0200 | [diff] [blame] | 1869 | // Update the value |
| 1870 | copy_tv(&tv, &di->di_tv); |
Bram Moolenaar | 4a01159 | 2021-08-05 15:11:08 +0200 | [diff] [blame] | 1871 | if (dict_add(dict, di) == FAIL) |
Bram Moolenaar | 1b6acf0 | 2021-08-05 16:47:08 +0200 | [diff] [blame] | 1872 | { |
| 1873 | dictitem_free(di); |
| 1874 | clear_tv(&tv); |
Bram Moolenaar | 4a01159 | 2021-08-05 15:11:08 +0200 | [diff] [blame] | 1875 | return luaL_error(L, "Couldn't add to dictionary"); |
Bram Moolenaar | 1b6acf0 | 2021-08-05 16:47:08 +0200 | [diff] [blame] | 1876 | } |
| 1877 | } |
| 1878 | else |
Yegappan Lakshmanan | 9dc4bef | 2021-08-04 21:12:52 +0200 | [diff] [blame] | 1879 | { |
| 1880 | // Clear the old value |
| 1881 | clear_tv(&di->di_tv); |
| 1882 | // Update the value |
| 1883 | copy_tv(&tv, &di->di_tv); |
| 1884 | } |
| 1885 | |
| 1886 | // Clear the temporary variable |
| 1887 | clear_tv(&tv); |
| 1888 | } |
| 1889 | |
| 1890 | return 0; |
| 1891 | } |
| 1892 | |
| 1893 | static int |
| 1894 | luaV_getvar(lua_State *L) |
| 1895 | { |
| 1896 | dict_T *dict = luaV_get_var_scope(L); |
| 1897 | size_t len; |
| 1898 | const char *name = luaL_checklstring(L, 3, &len); |
Yegappan Lakshmanan | 11328bc | 2021-08-06 21:34:38 +0200 | [diff] [blame] | 1899 | dictitem_T *di = dict_find(dict, (char_u *)name, (int)len); |
Yegappan Lakshmanan | 9dc4bef | 2021-08-04 21:12:52 +0200 | [diff] [blame] | 1900 | |
Yegappan Lakshmanan | 9dc4bef | 2021-08-04 21:12:52 +0200 | [diff] [blame] | 1901 | if (di == NULL) |
| 1902 | return 0; // nil |
| 1903 | |
| 1904 | luaV_pushtypval(L, &di->di_tv); |
| 1905 | return 1; |
| 1906 | } |
| 1907 | |
Bram Moolenaar | 38e2b06 | 2011-09-21 17:15:39 +0200 | [diff] [blame] | 1908 | static int |
Bram Moolenaar | 55d5c03 | 2010-07-17 23:52:29 +0200 | [diff] [blame] | 1909 | luaV_command(lua_State *L) |
| 1910 | { |
Yegappan Lakshmanan | 11328bc | 2021-08-06 21:34:38 +0200 | [diff] [blame] | 1911 | char_u *s = vim_strsave((char_u *)luaL_checkstring(L, 1)); |
| 1912 | |
| 1913 | execute_cmds_from_string(s); |
| 1914 | vim_free(s); |
Bram Moolenaar | a4d158b | 2022-08-14 14:17:45 +0100 | [diff] [blame] | 1915 | update_screen(UPD_VALID); |
Bram Moolenaar | 0ba0429 | 2010-07-14 23:23:17 +0200 | [diff] [blame] | 1916 | return 0; |
| 1917 | } |
| 1918 | |
Bram Moolenaar | 55d5c03 | 2010-07-17 23:52:29 +0200 | [diff] [blame] | 1919 | static int |
| 1920 | luaV_eval(lua_State *L) |
| 1921 | { |
Bram Moolenaar | 0ba0429 | 2010-07-14 23:23:17 +0200 | [diff] [blame] | 1922 | typval_T *tv = eval_expr((char_u *) luaL_checkstring(L, 1), NULL); |
Yegappan Lakshmanan | 11328bc | 2021-08-06 21:34:38 +0200 | [diff] [blame] | 1923 | |
Bram Moolenaar | 0ba0429 | 2010-07-14 23:23:17 +0200 | [diff] [blame] | 1924 | if (tv == NULL) luaL_error(L, "invalid expression"); |
| 1925 | luaV_pushtypval(L, tv); |
Bram Moolenaar | b376647 | 2013-04-15 13:49:21 +0200 | [diff] [blame] | 1926 | free_tv(tv); |
Bram Moolenaar | 0ba0429 | 2010-07-14 23:23:17 +0200 | [diff] [blame] | 1927 | return 1; |
| 1928 | } |
| 1929 | |
Bram Moolenaar | 55d5c03 | 2010-07-17 23:52:29 +0200 | [diff] [blame] | 1930 | static int |
Bram Moolenaar | 0d2e4fc | 2010-07-18 12:35:47 +0200 | [diff] [blame] | 1931 | luaV_beep(lua_State *L UNUSED) |
Bram Moolenaar | 55d5c03 | 2010-07-17 23:52:29 +0200 | [diff] [blame] | 1932 | { |
Bram Moolenaar | 165bc69 | 2015-07-21 17:53:25 +0200 | [diff] [blame] | 1933 | vim_beep(BO_LANG); |
Bram Moolenaar | 0ba0429 | 2010-07-14 23:23:17 +0200 | [diff] [blame] | 1934 | return 0; |
| 1935 | } |
| 1936 | |
Bram Moolenaar | 55d5c03 | 2010-07-17 23:52:29 +0200 | [diff] [blame] | 1937 | static int |
| 1938 | luaV_line(lua_State *L) |
| 1939 | { |
Bram Moolenaar | 0ba0429 | 2010-07-14 23:23:17 +0200 | [diff] [blame] | 1940 | luaV_pushline(L, curbuf, curwin->w_cursor.lnum); |
| 1941 | return 1; |
| 1942 | } |
| 1943 | |
Bram Moolenaar | 55d5c03 | 2010-07-17 23:52:29 +0200 | [diff] [blame] | 1944 | static int |
Bram Moolenaar | 1dced57 | 2012-04-05 16:54:08 +0200 | [diff] [blame] | 1945 | luaV_list(lua_State *L) |
| 1946 | { |
Bram Moolenaar | ca06da9 | 2018-07-01 15:12:05 +0200 | [diff] [blame] | 1947 | list_T *l; |
| 1948 | int initarg = !lua_isnoneornil(L, 1); |
| 1949 | |
| 1950 | if (initarg && lua_type(L, 1) != LUA_TTABLE) |
| 1951 | luaL_error(L, "table expected, got %s", luaL_typename(L, 1)); |
Yegappan Lakshmanan | 0233bdf | 2023-01-12 12:33:30 +0000 | [diff] [blame] | 1952 | |
Bram Moolenaar | ca06da9 | 2018-07-01 15:12:05 +0200 | [diff] [blame] | 1953 | l = list_alloc(); |
Bram Moolenaar | 1dced57 | 2012-04-05 16:54:08 +0200 | [diff] [blame] | 1954 | if (l == NULL) |
Bram Moolenaar | ca06da9 | 2018-07-01 15:12:05 +0200 | [diff] [blame] | 1955 | { |
Yegappan Lakshmanan | 0233bdf | 2023-01-12 12:33:30 +0000 | [diff] [blame] | 1956 | lua_pushnil(L); |
| 1957 | return 1; |
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 | |
| 1960 | luaV_newlist(L, l); |
| 1961 | if (!initarg) |
| 1962 | return 1; |
| 1963 | |
| 1964 | // traverse table to init list |
| 1965 | int notnil, i = 0; |
| 1966 | typval_T v; |
| 1967 | do |
| 1968 | { |
| 1969 | lua_rawgeti(L, 1, ++i); |
| 1970 | notnil = !lua_isnil(L, -1); |
| 1971 | if (notnil) |
| 1972 | { |
| 1973 | luaV_checktypval(L, -1, &v, "vim.list"); |
| 1974 | list_append_tv(l, &v); |
| 1975 | clear_tv(&v); |
| 1976 | } |
| 1977 | lua_pop(L, 1); // value |
| 1978 | } while (notnil); |
| 1979 | |
Bram Moolenaar | 1dced57 | 2012-04-05 16:54:08 +0200 | [diff] [blame] | 1980 | return 1; |
| 1981 | } |
| 1982 | |
| 1983 | static int |
| 1984 | luaV_dict(lua_State *L) |
| 1985 | { |
Bram Moolenaar | ca06da9 | 2018-07-01 15:12:05 +0200 | [diff] [blame] | 1986 | dict_T *d; |
| 1987 | int initarg = !lua_isnoneornil(L, 1); |
| 1988 | |
| 1989 | if (initarg && lua_type(L, 1) != LUA_TTABLE) |
| 1990 | luaL_error(L, "table expected, got %s", luaL_typename(L, 1)); |
Yegappan Lakshmanan | 0233bdf | 2023-01-12 12:33:30 +0000 | [diff] [blame] | 1991 | |
Bram Moolenaar | ca06da9 | 2018-07-01 15:12:05 +0200 | [diff] [blame] | 1992 | d = dict_alloc(); |
Bram Moolenaar | 1dced57 | 2012-04-05 16:54:08 +0200 | [diff] [blame] | 1993 | if (d == NULL) |
Bram Moolenaar | ca06da9 | 2018-07-01 15:12:05 +0200 | [diff] [blame] | 1994 | { |
Yegappan Lakshmanan | 0233bdf | 2023-01-12 12:33:30 +0000 | [diff] [blame] | 1995 | lua_pushnil(L); |
| 1996 | return 1; |
| 1997 | } |
| 1998 | |
| 1999 | luaV_newdict(L, d); |
| 2000 | if (!initarg) |
| 2001 | return 1; |
| 2002 | |
| 2003 | // traverse table to init dict |
| 2004 | lua_pushnil(L); |
| 2005 | while (lua_next(L, 1)) |
| 2006 | { |
| 2007 | char_u *key; |
| 2008 | dictitem_T *di; |
| 2009 | typval_T v; |
| 2010 | |
| 2011 | lua_pushvalue(L, -2); // dup key in case it's a number |
| 2012 | key = (char_u *) lua_tostring(L, -1); |
| 2013 | if (key == NULL) |
Bram Moolenaar | ca06da9 | 2018-07-01 15:12:05 +0200 | [diff] [blame] | 2014 | { |
| 2015 | lua_pushnil(L); |
Yegappan Lakshmanan | 0233bdf | 2023-01-12 12:33:30 +0000 | [diff] [blame] | 2016 | return 1; |
Bram Moolenaar | ca06da9 | 2018-07-01 15:12:05 +0200 | [diff] [blame] | 2017 | } |
Yegappan Lakshmanan | 0233bdf | 2023-01-12 12:33:30 +0000 | [diff] [blame] | 2018 | if (*key == NUL) |
| 2019 | luaL_error(L, "table has empty key"); |
| 2020 | luaV_checktypval(L, -2, &v, "vim.dict"); // value |
| 2021 | di = dictitem_alloc(key); |
| 2022 | if (di == NULL || dict_add(d, di) == FAIL) |
| 2023 | { |
| 2024 | vim_free(di); |
| 2025 | lua_pushnil(L); |
| 2026 | return 1; |
| 2027 | } |
| 2028 | di->di_tv = v; |
| 2029 | lua_pop(L, 2); // key copy and value |
Bram Moolenaar | ca06da9 | 2018-07-01 15:12:05 +0200 | [diff] [blame] | 2030 | } |
Yegappan Lakshmanan | 0233bdf | 2023-01-12 12:33:30 +0000 | [diff] [blame] | 2031 | |
Bram Moolenaar | ca06da9 | 2018-07-01 15:12:05 +0200 | [diff] [blame] | 2032 | return 1; |
| 2033 | } |
| 2034 | |
| 2035 | static int |
Bram Moolenaar | b782869 | 2019-03-23 13:57:02 +0100 | [diff] [blame] | 2036 | luaV_blob(lua_State *L) |
| 2037 | { |
| 2038 | blob_T *b; |
| 2039 | int initarg = !lua_isnoneornil(L, 1); |
| 2040 | |
| 2041 | if (initarg && !lua_isstring(L, 1)) |
| 2042 | luaL_error(L, "string expected, got %s", luaL_typename(L, 1)); |
Yegappan Lakshmanan | 0233bdf | 2023-01-12 12:33:30 +0000 | [diff] [blame] | 2043 | |
Bram Moolenaar | b782869 | 2019-03-23 13:57:02 +0100 | [diff] [blame] | 2044 | b = blob_alloc(); |
| 2045 | if (b == NULL) |
Bram Moolenaar | b782869 | 2019-03-23 13:57:02 +0100 | [diff] [blame] | 2046 | { |
Yegappan Lakshmanan | 0233bdf | 2023-01-12 12:33:30 +0000 | [diff] [blame] | 2047 | lua_pushnil(L); |
| 2048 | return 1; |
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 | |
| 2051 | luaV_newblob(L, b); |
| 2052 | if (!initarg) |
| 2053 | return 1; |
| 2054 | |
| 2055 | // traverse table to init blob |
| 2056 | size_t i, l = 0; |
| 2057 | const char *s = lua_tolstring(L, 1, &l); |
| 2058 | |
| 2059 | if (ga_grow(&b->bv_ga, (int)l) == OK) |
| 2060 | for (i = 0; i < l; ++i) |
| 2061 | ga_append(&b->bv_ga, s[i]); |
| 2062 | |
Bram Moolenaar | b782869 | 2019-03-23 13:57:02 +0100 | [diff] [blame] | 2063 | return 1; |
| 2064 | } |
| 2065 | |
| 2066 | static int |
Bram Moolenaar | ca06da9 | 2018-07-01 15:12:05 +0200 | [diff] [blame] | 2067 | luaV_funcref(lua_State *L) |
| 2068 | { |
| 2069 | const char *name = luaL_checkstring(L, 1); |
Bram Moolenaar | 2ab2e86 | 2019-12-04 21:24:53 +0100 | [diff] [blame] | 2070 | // note: not checking if function exists (needs function_exists) |
Bram Moolenaar | ca06da9 | 2018-07-01 15:12:05 +0200 | [diff] [blame] | 2071 | if (name == NULL || *name == NUL || VIM_ISDIGIT(*name)) |
| 2072 | luaL_error(L, "invalid function name: %s", name); |
| 2073 | luaV_newfuncref(L, (char_u *) name); |
Bram Moolenaar | 1dced57 | 2012-04-05 16:54:08 +0200 | [diff] [blame] | 2074 | return 1; |
| 2075 | } |
| 2076 | |
| 2077 | static int |
Bram Moolenaar | 55d5c03 | 2010-07-17 23:52:29 +0200 | [diff] [blame] | 2078 | luaV_buffer(lua_State *L) |
| 2079 | { |
Bram Moolenaar | 0ba0429 | 2010-07-14 23:23:17 +0200 | [diff] [blame] | 2080 | buf_T *buf; |
Bram Moolenaar | 2ab2e86 | 2019-12-04 21:24:53 +0100 | [diff] [blame] | 2081 | if (lua_isstring(L, 1)) // get by number or name? |
Bram Moolenaar | 55d5c03 | 2010-07-17 23:52:29 +0200 | [diff] [blame] | 2082 | { |
Bram Moolenaar | 2ab2e86 | 2019-12-04 21:24:53 +0100 | [diff] [blame] | 2083 | if (lua_isnumber(L, 1)) // by number? |
Bram Moolenaar | 55d5c03 | 2010-07-17 23:52:29 +0200 | [diff] [blame] | 2084 | { |
| 2085 | int n = lua_tointeger(L, 1); |
Bram Moolenaar | 2932359 | 2016-07-24 22:04:11 +0200 | [diff] [blame] | 2086 | FOR_ALL_BUFFERS(buf) |
Bram Moolenaar | 55d5c03 | 2010-07-17 23:52:29 +0200 | [diff] [blame] | 2087 | if (buf->b_fnum == n) break; |
| 2088 | } |
Bram Moolenaar | ca06da9 | 2018-07-01 15:12:05 +0200 | [diff] [blame] | 2089 | else // by name |
| 2090 | { |
Bram Moolenaar | 55d5c03 | 2010-07-17 23:52:29 +0200 | [diff] [blame] | 2091 | size_t l; |
| 2092 | const char *s = lua_tolstring(L, 1, &l); |
Bram Moolenaar | 2932359 | 2016-07-24 22:04:11 +0200 | [diff] [blame] | 2093 | FOR_ALL_BUFFERS(buf) |
Bram Moolenaar | 55d5c03 | 2010-07-17 23:52:29 +0200 | [diff] [blame] | 2094 | { |
| 2095 | if (buf->b_ffname == NULL || buf->b_sfname == NULL) |
| 2096 | { |
| 2097 | if (l == 0) break; |
| 2098 | } |
Bram Moolenaar | 0d2e4fc | 2010-07-18 12:35:47 +0200 | [diff] [blame] | 2099 | else if (strncmp(s, (char *)buf->b_ffname, l) == 0 |
| 2100 | || strncmp(s, (char *)buf->b_sfname, l) == 0) |
Bram Moolenaar | 55d5c03 | 2010-07-17 23:52:29 +0200 | [diff] [blame] | 2101 | break; |
| 2102 | } |
| 2103 | } |
Bram Moolenaar | 0ba0429 | 2010-07-14 23:23:17 +0200 | [diff] [blame] | 2104 | } |
Bram Moolenaar | 1dced57 | 2012-04-05 16:54:08 +0200 | [diff] [blame] | 2105 | else |
Bram Moolenaar | 2ab2e86 | 2019-12-04 21:24:53 +0100 | [diff] [blame] | 2106 | buf = (lua_toboolean(L, 1)) ? firstbuf : curbuf; // first buffer? |
Bram Moolenaar | 1dced57 | 2012-04-05 16:54:08 +0200 | [diff] [blame] | 2107 | luaV_pushbuffer(L, buf); |
Bram Moolenaar | 0ba0429 | 2010-07-14 23:23:17 +0200 | [diff] [blame] | 2108 | return 1; |
| 2109 | } |
| 2110 | |
Bram Moolenaar | 55d5c03 | 2010-07-17 23:52:29 +0200 | [diff] [blame] | 2111 | static int |
| 2112 | luaV_window(lua_State *L) |
| 2113 | { |
Bram Moolenaar | 0ba0429 | 2010-07-14 23:23:17 +0200 | [diff] [blame] | 2114 | win_T *win; |
Bram Moolenaar | 2ab2e86 | 2019-12-04 21:24:53 +0100 | [diff] [blame] | 2115 | if (lua_isnumber(L, 1)) // get by number? |
Bram Moolenaar | 55d5c03 | 2010-07-17 23:52:29 +0200 | [diff] [blame] | 2116 | { |
| 2117 | int n = lua_tointeger(L, 1); |
| 2118 | for (win = firstwin; win != NULL; win = win->w_next, n--) |
| 2119 | if (n == 1) break; |
Bram Moolenaar | 0ba0429 | 2010-07-14 23:23:17 +0200 | [diff] [blame] | 2120 | } |
Bram Moolenaar | 1dced57 | 2012-04-05 16:54:08 +0200 | [diff] [blame] | 2121 | else |
Bram Moolenaar | 2ab2e86 | 2019-12-04 21:24:53 +0100 | [diff] [blame] | 2122 | win = (lua_toboolean(L, 1)) ? firstwin : curwin; // first window? |
Bram Moolenaar | 1dced57 | 2012-04-05 16:54:08 +0200 | [diff] [blame] | 2123 | luaV_pushwindow(L, win); |
Bram Moolenaar | 0ba0429 | 2010-07-14 23:23:17 +0200 | [diff] [blame] | 2124 | return 1; |
| 2125 | } |
| 2126 | |
Bram Moolenaar | 55d5c03 | 2010-07-17 23:52:29 +0200 | [diff] [blame] | 2127 | static int |
| 2128 | luaV_open(lua_State *L) |
| 2129 | { |
Bram Moolenaar | 0ba0429 | 2010-07-14 23:23:17 +0200 | [diff] [blame] | 2130 | char_u *s = NULL; |
| 2131 | #ifdef HAVE_SANDBOX |
| 2132 | luaV_checksandbox(L); |
| 2133 | #endif |
| 2134 | if (lua_isstring(L, 1)) s = (char_u *) lua_tostring(L, 1); |
Bram Moolenaar | fa263a5 | 2011-12-08 16:00:16 +0100 | [diff] [blame] | 2135 | luaV_pushbuffer(L, buflist_new(s, NULL, 1L, BLN_LISTED)); |
Bram Moolenaar | 0ba0429 | 2010-07-14 23:23:17 +0200 | [diff] [blame] | 2136 | return 1; |
| 2137 | } |
| 2138 | |
Bram Moolenaar | 55d5c03 | 2010-07-17 23:52:29 +0200 | [diff] [blame] | 2139 | static int |
Bram Moolenaar | 1dced57 | 2012-04-05 16:54:08 +0200 | [diff] [blame] | 2140 | luaV_type(lua_State *L) |
Bram Moolenaar | 55d5c03 | 2010-07-17 23:52:29 +0200 | [diff] [blame] | 2141 | { |
Bram Moolenaar | 1dced57 | 2012-04-05 16:54:08 +0200 | [diff] [blame] | 2142 | luaL_checkany(L, 1); |
Bram Moolenaar | 2ab2e86 | 2019-12-04 21:24:53 +0100 | [diff] [blame] | 2143 | if (lua_type(L, 1) == LUA_TUSERDATA) // check vim udata? |
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 | lua_settop(L, 1); |
| 2146 | if (lua_getmetatable(L, 1)) |
| 2147 | { |
| 2148 | luaV_getfield(L, LUAVIM_LIST); |
| 2149 | if (lua_rawequal(L, -1, 2)) |
| 2150 | { |
| 2151 | lua_pushstring(L, "list"); |
| 2152 | return 1; |
| 2153 | } |
| 2154 | luaV_getfield(L, LUAVIM_DICT); |
| 2155 | if (lua_rawequal(L, -1, 2)) |
| 2156 | { |
| 2157 | lua_pushstring(L, "dict"); |
| 2158 | return 1; |
| 2159 | } |
Bram Moolenaar | b782869 | 2019-03-23 13:57:02 +0100 | [diff] [blame] | 2160 | luaV_getfield(L, LUAVIM_BLOB); |
| 2161 | if (lua_rawequal(L, -1, 2)) |
| 2162 | { |
| 2163 | lua_pushstring(L, "blob"); |
| 2164 | return 1; |
| 2165 | } |
Bram Moolenaar | ca06da9 | 2018-07-01 15:12:05 +0200 | [diff] [blame] | 2166 | luaV_getfield(L, LUAVIM_FUNCREF); |
| 2167 | if (lua_rawequal(L, -1, 2)) |
| 2168 | { |
| 2169 | lua_pushstring(L, "funcref"); |
| 2170 | return 1; |
| 2171 | } |
Bram Moolenaar | 1dced57 | 2012-04-05 16:54:08 +0200 | [diff] [blame] | 2172 | luaV_getfield(L, LUAVIM_BUFFER); |
| 2173 | if (lua_rawequal(L, -1, 2)) |
| 2174 | { |
| 2175 | lua_pushstring(L, "buffer"); |
| 2176 | return 1; |
| 2177 | } |
| 2178 | luaV_getfield(L, LUAVIM_WINDOW); |
| 2179 | if (lua_rawequal(L, -1, 2)) |
| 2180 | { |
| 2181 | lua_pushstring(L, "window"); |
| 2182 | return 1; |
| 2183 | } |
| 2184 | } |
Bram Moolenaar | 0ba0429 | 2010-07-14 23:23:17 +0200 | [diff] [blame] | 2185 | } |
Bram Moolenaar | 2ab2e86 | 2019-12-04 21:24:53 +0100 | [diff] [blame] | 2186 | lua_pushstring(L, luaL_typename(L, 1)); // fallback |
Bram Moolenaar | 1dced57 | 2012-04-05 16:54:08 +0200 | [diff] [blame] | 2187 | return 1; |
Bram Moolenaar | 0ba0429 | 2010-07-14 23:23:17 +0200 | [diff] [blame] | 2188 | } |
| 2189 | |
Bram Moolenaar | eb04f08 | 2020-05-17 14:32:35 +0200 | [diff] [blame] | 2190 | static int |
| 2191 | luaV_call(lua_State *L) |
| 2192 | { |
| 2193 | int argc = lua_gettop(L) - 1; |
| 2194 | size_t funcname_len; |
| 2195 | char_u *funcname; |
| 2196 | char *error = NULL; |
| 2197 | typval_T rettv; |
| 2198 | typval_T argv[MAX_FUNC_ARGS + 1]; |
| 2199 | int i = 0; |
| 2200 | |
| 2201 | if (argc > MAX_FUNC_ARGS) |
| 2202 | return luaL_error(L, "Function called with too many arguments"); |
| 2203 | |
| 2204 | funcname = (char_u *)luaL_checklstring(L, 1, &funcname_len); |
| 2205 | |
| 2206 | for (; i < argc; i++) |
| 2207 | { |
| 2208 | if (luaV_totypval(L, i + 2, &argv[i]) == FAIL) |
| 2209 | { |
| 2210 | error = "lua: cannot convert value"; |
| 2211 | goto free_vim_args; |
| 2212 | } |
| 2213 | } |
| 2214 | |
| 2215 | argv[argc].v_type = VAR_UNKNOWN; |
| 2216 | |
| 2217 | if (call_vim_function(funcname, argc, argv, &rettv) == FAIL) |
| 2218 | { |
| 2219 | error = "lua: call_vim_function failed"; |
| 2220 | goto free_vim_args; |
| 2221 | } |
| 2222 | |
| 2223 | luaV_pushtypval(L, &rettv); |
| 2224 | clear_tv(&rettv); |
| 2225 | |
| 2226 | free_vim_args: |
| 2227 | while (i > 0) |
| 2228 | clear_tv(&argv[--i]); |
| 2229 | |
| 2230 | if (error == NULL) |
| 2231 | return 1; |
| 2232 | else |
| 2233 | return luaL_error(L, error); |
| 2234 | } |
| 2235 | |
Yegappan Lakshmanan | 11328bc | 2021-08-06 21:34:38 +0200 | [diff] [blame] | 2236 | /* |
| 2237 | * Return the Vim version as a Lua table |
| 2238 | */ |
| 2239 | static int |
| 2240 | luaV_version(lua_State *L) |
| 2241 | { |
| 2242 | lua_newtable(L); |
| 2243 | lua_pushstring(L, "major"); |
| 2244 | lua_pushinteger(L, VIM_VERSION_MAJOR); |
| 2245 | lua_settable(L, -3); |
| 2246 | lua_pushstring(L, "minor"); |
| 2247 | lua_pushinteger(L, VIM_VERSION_MINOR); |
| 2248 | lua_settable(L, -3); |
| 2249 | lua_pushstring(L, "patch"); |
| 2250 | lua_pushinteger(L, highest_patch()); |
| 2251 | lua_settable(L, -3); |
| 2252 | return 1; |
| 2253 | } |
| 2254 | |
Bram Moolenaar | 0ba0429 | 2010-07-14 23:23:17 +0200 | [diff] [blame] | 2255 | static const luaL_Reg luaV_module[] = { |
| 2256 | {"command", luaV_command}, |
| 2257 | {"eval", luaV_eval}, |
| 2258 | {"beep", luaV_beep}, |
| 2259 | {"line", luaV_line}, |
Bram Moolenaar | 1dced57 | 2012-04-05 16:54:08 +0200 | [diff] [blame] | 2260 | {"list", luaV_list}, |
| 2261 | {"dict", luaV_dict}, |
Bram Moolenaar | b782869 | 2019-03-23 13:57:02 +0100 | [diff] [blame] | 2262 | {"blob", luaV_blob}, |
Bram Moolenaar | ca06da9 | 2018-07-01 15:12:05 +0200 | [diff] [blame] | 2263 | {"funcref", luaV_funcref}, |
Bram Moolenaar | 0ba0429 | 2010-07-14 23:23:17 +0200 | [diff] [blame] | 2264 | {"buffer", luaV_buffer}, |
| 2265 | {"window", luaV_window}, |
| 2266 | {"open", luaV_open}, |
Bram Moolenaar | 1dced57 | 2012-04-05 16:54:08 +0200 | [diff] [blame] | 2267 | {"type", luaV_type}, |
Bram Moolenaar | eb04f08 | 2020-05-17 14:32:35 +0200 | [diff] [blame] | 2268 | {"call", luaV_call}, |
Yegappan Lakshmanan | 9dc4bef | 2021-08-04 21:12:52 +0200 | [diff] [blame] | 2269 | {"_getvar", luaV_getvar}, |
| 2270 | {"_setvar", luaV_setvar}, |
Yegappan Lakshmanan | 11328bc | 2021-08-06 21:34:38 +0200 | [diff] [blame] | 2271 | {"version", luaV_version}, |
Bram Moolenaar | 125ed27 | 2021-04-07 20:11:12 +0200 | [diff] [blame] | 2272 | {"lua_version", NULL}, |
Bram Moolenaar | 0ba0429 | 2010-07-14 23:23:17 +0200 | [diff] [blame] | 2273 | {NULL, NULL} |
| 2274 | }; |
| 2275 | |
Bram Moolenaar | 2ab2e86 | 2019-12-04 21:24:53 +0100 | [diff] [blame] | 2276 | /* |
| 2277 | * for freeing list, dict, buffer and window objects; lightuserdata as arg |
| 2278 | */ |
Bram Moolenaar | 1dced57 | 2012-04-05 16:54:08 +0200 | [diff] [blame] | 2279 | static int |
| 2280 | luaV_free(lua_State *L) |
| 2281 | { |
| 2282 | lua_pushnil(L); |
| 2283 | luaV_setudata(L, lua_touserdata(L, 1)); |
| 2284 | return 0; |
| 2285 | } |
| 2286 | |
| 2287 | static int |
Bram Moolenaar | 4eefe47 | 2019-03-19 21:59:19 +0100 | [diff] [blame] | 2288 | luaV_luaeval(lua_State *L) |
Bram Moolenaar | 1dced57 | 2012-04-05 16:54:08 +0200 | [diff] [blame] | 2289 | { |
| 2290 | luaL_Buffer b; |
| 2291 | size_t l; |
| 2292 | const char *str = lua_tolstring(L, 1, &l); |
| 2293 | typval_T *arg = (typval_T *) lua_touserdata(L, 2); |
| 2294 | typval_T *rettv = (typval_T *) lua_touserdata(L, 3); |
| 2295 | luaL_buffinit(L, &b); |
| 2296 | luaL_addlstring(&b, LUAVIM_EVALHEADER, sizeof(LUAVIM_EVALHEADER) - 1); |
| 2297 | luaL_addlstring(&b, str, l); |
| 2298 | luaL_pushresult(&b); |
| 2299 | str = lua_tolstring(L, -1, &l); |
Bram Moolenaar | 2ab2e86 | 2019-12-04 21:24:53 +0100 | [diff] [blame] | 2300 | if (luaL_loadbuffer(L, str, l, LUAVIM_EVALNAME)) // compile error? |
Bram Moolenaar | 1dced57 | 2012-04-05 16:54:08 +0200 | [diff] [blame] | 2301 | { |
| 2302 | luaV_emsg(L); |
| 2303 | return 0; |
| 2304 | } |
| 2305 | luaV_pushtypval(L, arg); |
Bram Moolenaar | 2ab2e86 | 2019-12-04 21:24:53 +0100 | [diff] [blame] | 2306 | if (lua_pcall(L, 1, 1, 0)) // running error? |
Bram Moolenaar | 1dced57 | 2012-04-05 16:54:08 +0200 | [diff] [blame] | 2307 | { |
| 2308 | luaV_emsg(L); |
| 2309 | return 0; |
| 2310 | } |
Bram Moolenaar | ca06da9 | 2018-07-01 15:12:05 +0200 | [diff] [blame] | 2311 | if (luaV_totypval(L, -1, rettv) == FAIL) |
Bram Moolenaar | f9e3e09 | 2019-01-13 23:38:42 +0100 | [diff] [blame] | 2312 | emsg("luaeval: cannot convert value"); |
Bram Moolenaar | f554a32 | 2015-02-04 23:08:01 +0100 | [diff] [blame] | 2313 | return 0; |
Bram Moolenaar | 1dced57 | 2012-04-05 16:54:08 +0200 | [diff] [blame] | 2314 | } |
| 2315 | |
| 2316 | static int |
Bram Moolenaar | 4eefe47 | 2019-03-19 21:59:19 +0100 | [diff] [blame] | 2317 | luaV_setref(lua_State *L) |
Bram Moolenaar | 1dced57 | 2012-04-05 16:54:08 +0200 | [diff] [blame] | 2318 | { |
Bram Moolenaar | 4eefe47 | 2019-03-19 21:59:19 +0100 | [diff] [blame] | 2319 | int copyID = lua_tointeger(L, 1); |
| 2320 | int abort = FALSE; |
Bram Moolenaar | 2459a5e | 2015-02-03 12:55:18 +0100 | [diff] [blame] | 2321 | |
Bram Moolenaar | 1dced57 | 2012-04-05 16:54:08 +0200 | [diff] [blame] | 2322 | luaV_getfield(L, LUAVIM_LIST); |
| 2323 | luaV_getfield(L, LUAVIM_DICT); |
Bram Moolenaar | 4eefe47 | 2019-03-19 21:59:19 +0100 | [diff] [blame] | 2324 | luaV_getfield(L, LUAVIM_FUNCREF); |
Bram Moolenaar | 1dced57 | 2012-04-05 16:54:08 +0200 | [diff] [blame] | 2325 | lua_pushnil(L); |
Bram Moolenaar | 4eefe47 | 2019-03-19 21:59:19 +0100 | [diff] [blame] | 2326 | // traverse cache table |
Bram Moolenaar | b84634d | 2015-02-04 22:02:37 +0100 | [diff] [blame] | 2327 | while (!abort && lua_next(L, lua_upvalueindex(1)) != 0) |
Bram Moolenaar | 1dced57 | 2012-04-05 16:54:08 +0200 | [diff] [blame] | 2328 | { |
| 2329 | lua_getmetatable(L, -1); |
Bram Moolenaar | 4eefe47 | 2019-03-19 21:59:19 +0100 | [diff] [blame] | 2330 | if (lua_rawequal(L, -1, 2)) // list? |
Bram Moolenaar | 1dced57 | 2012-04-05 16:54:08 +0200 | [diff] [blame] | 2331 | { |
Bram Moolenaar | 4eefe47 | 2019-03-19 21:59:19 +0100 | [diff] [blame] | 2332 | list_T *l = (list_T *)lua_touserdata(L, 5); // key |
| 2333 | |
Bram Moolenaar | 7be3ab2 | 2019-06-23 01:46:15 +0200 | [diff] [blame] | 2334 | abort = set_ref_in_list(l, copyID); |
Bram Moolenaar | 1dced57 | 2012-04-05 16:54:08 +0200 | [diff] [blame] | 2335 | } |
Bram Moolenaar | 4eefe47 | 2019-03-19 21:59:19 +0100 | [diff] [blame] | 2336 | else if (lua_rawequal(L, -1, 3)) // dict? |
Bram Moolenaar | 1dced57 | 2012-04-05 16:54:08 +0200 | [diff] [blame] | 2337 | { |
Bram Moolenaar | 4eefe47 | 2019-03-19 21:59:19 +0100 | [diff] [blame] | 2338 | dict_T *d = (dict_T *)lua_touserdata(L, 5); // key |
| 2339 | |
Bram Moolenaar | 7be3ab2 | 2019-06-23 01:46:15 +0200 | [diff] [blame] | 2340 | abort = set_ref_in_dict(d, copyID); |
Bram Moolenaar | 1dced57 | 2012-04-05 16:54:08 +0200 | [diff] [blame] | 2341 | } |
Bram Moolenaar | 4eefe47 | 2019-03-19 21:59:19 +0100 | [diff] [blame] | 2342 | else if (lua_rawequal(L, -1, 4)) // funcref? |
| 2343 | { |
| 2344 | luaV_Funcref *f = (luaV_Funcref *)lua_touserdata(L, 5); // key |
| 2345 | |
Bram Moolenaar | 7be3ab2 | 2019-06-23 01:46:15 +0200 | [diff] [blame] | 2346 | abort = set_ref_in_dict(f->self, copyID); |
Bram Moolenaar | 4eefe47 | 2019-03-19 21:59:19 +0100 | [diff] [blame] | 2347 | } |
| 2348 | lua_pop(L, 2); // metatable and value |
Bram Moolenaar | 1dced57 | 2012-04-05 16:54:08 +0200 | [diff] [blame] | 2349 | } |
Bram Moolenaar | 2459a5e | 2015-02-03 12:55:18 +0100 | [diff] [blame] | 2350 | lua_pushinteger(L, abort); |
Bram Moolenaar | f554a32 | 2015-02-04 23:08:01 +0100 | [diff] [blame] | 2351 | return 1; |
Bram Moolenaar | 1dced57 | 2012-04-05 16:54:08 +0200 | [diff] [blame] | 2352 | } |
| 2353 | |
Bram Moolenaar | 125ed27 | 2021-04-07 20:11:12 +0200 | [diff] [blame] | 2354 | static int |
| 2355 | luaV_pushversion(lua_State *L) |
| 2356 | { |
| 2357 | int major = 0; |
| 2358 | int minor = 0; |
| 2359 | int patch = 0; |
| 2360 | char s[16]; |
| 2361 | |
| 2362 | sscanf(LUAVIM_VERSION, "Lua %d.%d.%d", &major, &minor, &patch); |
| 2363 | vim_snprintf(s, sizeof(s), "%d.%d.%d", major, minor, patch); |
| 2364 | lua_pushstring(L, s); |
| 2365 | return 0; |
| 2366 | } |
| 2367 | |
Bram Moolenaar | eb04f08 | 2020-05-17 14:32:35 +0200 | [diff] [blame] | 2368 | #define LUA_VIM_FN_CODE \ |
Bram Moolenaar | 788fbb4 | 2020-05-31 14:08:12 +0200 | [diff] [blame] | 2369 | "vim.fn = setmetatable({}, {\n"\ |
| 2370 | " __index = function (t, key)\n"\ |
| 2371 | " local function _fn(...)\n"\ |
| 2372 | " return vim.call(key, ...)\n"\ |
| 2373 | " end\n"\ |
| 2374 | " t[key] = _fn\n"\ |
| 2375 | " return _fn\n"\ |
| 2376 | " end\n"\ |
| 2377 | " })" |
| 2378 | |
| 2379 | #define LUA_VIM_UPDATE_PACKAGE_PATHS \ |
| 2380 | "local last_vim_paths = {}\n"\ |
| 2381 | "vim._update_package_paths = function ()\n"\ |
| 2382 | " local cur_vim_paths = {}\n"\ |
| 2383 | " local function split(s, delimiter)\n"\ |
| 2384 | " result = {}\n"\ |
| 2385 | " for match in (s..delimiter):gmatch(\"(.-)\"..delimiter) do\n"\ |
| 2386 | " table.insert(result, match)\n"\ |
| 2387 | " end\n"\ |
| 2388 | " return result\n"\ |
| 2389 | " end\n"\ |
| 2390 | " local rtps = split(vim.eval('&runtimepath'), ',')\n"\ |
| 2391 | " local sep = package.config:sub(1, 1)\n"\ |
| 2392 | " for _, key in ipairs({'path', 'cpath'}) do\n"\ |
| 2393 | " local orig_str = package[key] .. ';'\n"\ |
| 2394 | " local pathtrails_ordered = {}\n"\ |
| 2395 | " -- Note: ignores trailing item without trailing `;`. Not using something\n"\ |
| 2396 | " -- simpler in order to preserve empty items (stand for default path).\n"\ |
| 2397 | " local orig = {}\n"\ |
| 2398 | " for s in orig_str:gmatch('[^;]*;') do\n"\ |
| 2399 | " s = s:sub(1, -2) -- Strip trailing semicolon\n"\ |
| 2400 | " orig[#orig + 1] = s\n"\ |
| 2401 | " end\n"\ |
| 2402 | " if key == 'path' then\n"\ |
| 2403 | " -- /?.lua and /?/init.lua\n"\ |
| 2404 | " pathtrails_ordered = {sep .. '?.lua', sep .. '?' .. sep .. 'init.lua'}\n"\ |
| 2405 | " else\n"\ |
| 2406 | " local pathtrails = {}\n"\ |
| 2407 | " for _, s in ipairs(orig) do\n"\ |
| 2408 | " -- Find out path patterns. pathtrail should contain something like\n"\ |
| 2409 | " -- /?.so, \?.dll. This allows not to bother determining what correct\n"\ |
| 2410 | " -- suffixes are.\n"\ |
| 2411 | " local pathtrail = s:match('[/\\\\][^/\\\\]*%?.*$')\n"\ |
| 2412 | " if pathtrail and not pathtrails[pathtrail] then\n"\ |
| 2413 | " pathtrails[pathtrail] = true\n"\ |
| 2414 | " pathtrails_ordered[#pathtrails_ordered + 1] = pathtrail\n"\ |
| 2415 | " end\n"\ |
| 2416 | " end\n"\ |
| 2417 | " end\n"\ |
| 2418 | " local new = {}\n"\ |
| 2419 | " for _, rtp in ipairs(rtps) do\n"\ |
| 2420 | " if not rtp:match(';') then\n"\ |
| 2421 | " for _, pathtrail in pairs(pathtrails_ordered) do\n"\ |
| 2422 | " local new_path = rtp .. sep .. 'lua' .. pathtrail\n"\ |
| 2423 | " -- Always keep paths from &runtimepath at the start:\n"\ |
| 2424 | " -- append them here disregarding orig possibly containing one of them.\n"\ |
| 2425 | " new[#new + 1] = new_path\n"\ |
| 2426 | " cur_vim_paths[new_path] = true\n"\ |
| 2427 | " end\n"\ |
| 2428 | " end\n"\ |
| 2429 | " end\n"\ |
| 2430 | " for _, orig_path in ipairs(orig) do\n"\ |
| 2431 | " -- Handle removing obsolete paths originating from &runtimepath: such\n"\ |
| 2432 | " -- paths either belong to cur_nvim_paths and were already added above or\n"\ |
| 2433 | " -- to last_nvim_paths and should not be added at all if corresponding\n"\ |
| 2434 | " -- entry was removed from &runtimepath list.\n"\ |
| 2435 | " if not (cur_vim_paths[orig_path] or last_vim_paths[orig_path]) then\n"\ |
| 2436 | " new[#new + 1] = orig_path\n"\ |
| 2437 | " end\n"\ |
| 2438 | " end\n"\ |
| 2439 | " package[key] = table.concat(new, ';')\n"\ |
| 2440 | " end\n"\ |
| 2441 | " last_vim_paths = cur_vim_paths\n"\ |
| 2442 | "end" |
Bram Moolenaar | eb04f08 | 2020-05-17 14:32:35 +0200 | [diff] [blame] | 2443 | |
Yegappan Lakshmanan | 9dc4bef | 2021-08-04 21:12:52 +0200 | [diff] [blame] | 2444 | #define LUA_VIM_SETUP_VARIABLE_DICTS \ |
| 2445 | "do\n"\ |
| 2446 | " local function make_dict_accessor(scope)\n"\ |
| 2447 | " local mt = {}\n"\ |
| 2448 | " function mt:__newindex(k, v)\n"\ |
| 2449 | " return vim._setvar(scope, 0, k, v)\n"\ |
| 2450 | " end\n"\ |
| 2451 | " function mt:__index(k)\n"\ |
| 2452 | " return vim._getvar(scope, 0, k)\n"\ |
| 2453 | " end\n"\ |
| 2454 | " return setmetatable({}, mt)\n"\ |
| 2455 | " end\n"\ |
| 2456 | " vim.g = make_dict_accessor('g')\n"\ |
| 2457 | " vim.v = make_dict_accessor('v')\n"\ |
| 2458 | " vim.b = make_dict_accessor('b')\n"\ |
| 2459 | " vim.w = make_dict_accessor('w')\n"\ |
| 2460 | " vim.t = make_dict_accessor('t')\n"\ |
| 2461 | "end" |
| 2462 | |
Bram Moolenaar | 55d5c03 | 2010-07-17 23:52:29 +0200 | [diff] [blame] | 2463 | static int |
| 2464 | luaopen_vim(lua_State *L) |
| 2465 | { |
Bram Moolenaar | 2ab2e86 | 2019-12-04 21:24:53 +0100 | [diff] [blame] | 2466 | // set cache table |
Bram Moolenaar | 0ba0429 | 2010-07-14 23:23:17 +0200 | [diff] [blame] | 2467 | lua_newtable(L); |
| 2468 | lua_newtable(L); |
Bram Moolenaar | 1dced57 | 2012-04-05 16:54:08 +0200 | [diff] [blame] | 2469 | lua_pushstring(L, "v"); |
Bram Moolenaar | 0ba0429 | 2010-07-14 23:23:17 +0200 | [diff] [blame] | 2470 | lua_setfield(L, -2, "__mode"); |
Bram Moolenaar | 2ab2e86 | 2019-12-04 21:24:53 +0100 | [diff] [blame] | 2471 | lua_setmetatable(L, -2); // cache is weak-valued |
| 2472 | // print |
Bram Moolenaar | 0ba0429 | 2010-07-14 23:23:17 +0200 | [diff] [blame] | 2473 | lua_pushcfunction(L, luaV_print); |
| 2474 | lua_setglobal(L, "print"); |
Bram Moolenaar | 2ab2e86 | 2019-12-04 21:24:53 +0100 | [diff] [blame] | 2475 | // debug.debug |
Bram Moolenaar | 38e2b06 | 2011-09-21 17:15:39 +0200 | [diff] [blame] | 2476 | lua_getglobal(L, "debug"); |
| 2477 | lua_pushcfunction(L, luaV_debug); |
| 2478 | lua_setfield(L, -2, "debug"); |
| 2479 | lua_pop(L, 1); |
Bram Moolenaar | 2ab2e86 | 2019-12-04 21:24:53 +0100 | [diff] [blame] | 2480 | // free |
Bram Moolenaar | 0ba0429 | 2010-07-14 23:23:17 +0200 | [diff] [blame] | 2481 | lua_pushlightuserdata(L, (void *) LUAVIM_FREE); |
Bram Moolenaar | 2ab2e86 | 2019-12-04 21:24:53 +0100 | [diff] [blame] | 2482 | lua_pushvalue(L, 1); // cache table |
Bram Moolenaar | 1dced57 | 2012-04-05 16:54:08 +0200 | [diff] [blame] | 2483 | lua_pushcclosure(L, luaV_free, 1); |
| 2484 | lua_rawset(L, LUA_REGISTRYINDEX); |
Bram Moolenaar | 2ab2e86 | 2019-12-04 21:24:53 +0100 | [diff] [blame] | 2485 | // luaeval |
Bram Moolenaar | 1dced57 | 2012-04-05 16:54:08 +0200 | [diff] [blame] | 2486 | lua_pushlightuserdata(L, (void *) LUAVIM_LUAEVAL); |
Bram Moolenaar | 2ab2e86 | 2019-12-04 21:24:53 +0100 | [diff] [blame] | 2487 | lua_pushvalue(L, 1); // cache table |
Bram Moolenaar | 1dced57 | 2012-04-05 16:54:08 +0200 | [diff] [blame] | 2488 | lua_pushcclosure(L, luaV_luaeval, 1); |
| 2489 | lua_rawset(L, LUA_REGISTRYINDEX); |
Bram Moolenaar | 2ab2e86 | 2019-12-04 21:24:53 +0100 | [diff] [blame] | 2490 | // setref |
Bram Moolenaar | 1dced57 | 2012-04-05 16:54:08 +0200 | [diff] [blame] | 2491 | lua_pushlightuserdata(L, (void *) LUAVIM_SETREF); |
Bram Moolenaar | 2ab2e86 | 2019-12-04 21:24:53 +0100 | [diff] [blame] | 2492 | lua_pushvalue(L, 1); // cache table |
Bram Moolenaar | 1dced57 | 2012-04-05 16:54:08 +0200 | [diff] [blame] | 2493 | lua_pushcclosure(L, luaV_setref, 1); |
Bram Moolenaar | 0ba0429 | 2010-07-14 23:23:17 +0200 | [diff] [blame] | 2494 | lua_rawset(L, LUA_REGISTRYINDEX); |
Bram Moolenaar | 2ab2e86 | 2019-12-04 21:24:53 +0100 | [diff] [blame] | 2495 | // register |
Bram Moolenaar | 1dced57 | 2012-04-05 16:54:08 +0200 | [diff] [blame] | 2496 | luaV_newmetatable(L, LUAVIM_LIST); |
| 2497 | lua_pushvalue(L, 1); |
| 2498 | luaV_openlib(L, luaV_List_mt, 1); |
| 2499 | luaV_newmetatable(L, LUAVIM_DICT); |
| 2500 | lua_pushvalue(L, 1); |
| 2501 | luaV_openlib(L, luaV_Dict_mt, 1); |
Bram Moolenaar | b782869 | 2019-03-23 13:57:02 +0100 | [diff] [blame] | 2502 | luaV_newmetatable(L, LUAVIM_BLOB); |
| 2503 | lua_pushvalue(L, 1); |
| 2504 | luaV_openlib(L, luaV_Blob_mt, 1); |
Bram Moolenaar | ca06da9 | 2018-07-01 15:12:05 +0200 | [diff] [blame] | 2505 | luaV_newmetatable(L, LUAVIM_FUNCREF); |
| 2506 | lua_pushvalue(L, 1); |
| 2507 | luaV_openlib(L, luaV_Funcref_mt, 1); |
Bram Moolenaar | 0ba0429 | 2010-07-14 23:23:17 +0200 | [diff] [blame] | 2508 | luaV_newmetatable(L, LUAVIM_BUFFER); |
Bram Moolenaar | 2ab2e86 | 2019-12-04 21:24:53 +0100 | [diff] [blame] | 2509 | lua_pushvalue(L, 1); // cache table |
Bram Moolenaar | 1dced57 | 2012-04-05 16:54:08 +0200 | [diff] [blame] | 2510 | luaV_openlib(L, luaV_Buffer_mt, 1); |
Bram Moolenaar | 0ba0429 | 2010-07-14 23:23:17 +0200 | [diff] [blame] | 2511 | luaV_newmetatable(L, LUAVIM_WINDOW); |
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_Window_mt, 1); |
Bram Moolenaar | 2ab2e86 | 2019-12-04 21:24:53 +0100 | [diff] [blame] | 2514 | lua_newtable(L); // vim table |
| 2515 | lua_pushvalue(L, 1); // cache table |
Bram Moolenaar | 1dced57 | 2012-04-05 16:54:08 +0200 | [diff] [blame] | 2516 | luaV_openlib(L, luaV_module, 1); |
Bram Moolenaar | 125ed27 | 2021-04-07 20:11:12 +0200 | [diff] [blame] | 2517 | luaV_pushversion(L); |
| 2518 | lua_setfield(L, -2, "lua_version"); |
Bram Moolenaar | 1dced57 | 2012-04-05 16:54:08 +0200 | [diff] [blame] | 2519 | lua_setglobal(L, LUAVIM_NAME); |
Bram Moolenaar | eb04f08 | 2020-05-17 14:32:35 +0200 | [diff] [blame] | 2520 | // custom code |
Bram Moolenaar | 9309eb2 | 2020-05-17 16:53:56 +0200 | [diff] [blame] | 2521 | (void)luaL_dostring(L, LUA_VIM_FN_CODE); |
Bram Moolenaar | 788fbb4 | 2020-05-31 14:08:12 +0200 | [diff] [blame] | 2522 | (void)luaL_dostring(L, LUA_VIM_UPDATE_PACKAGE_PATHS); |
Yegappan Lakshmanan | 9dc4bef | 2021-08-04 21:12:52 +0200 | [diff] [blame] | 2523 | (void)luaL_dostring(L, LUA_VIM_SETUP_VARIABLE_DICTS); |
Bram Moolenaar | 788fbb4 | 2020-05-31 14:08:12 +0200 | [diff] [blame] | 2524 | |
| 2525 | lua_getglobal(L, "vim"); |
| 2526 | lua_getfield(L, -1, "_update_package_paths"); |
| 2527 | |
| 2528 | if (lua_pcall(L, 0, 0, 0)) |
| 2529 | luaV_emsg(L); |
| 2530 | |
Bram Moolenaar | 0ba0429 | 2010-07-14 23:23:17 +0200 | [diff] [blame] | 2531 | return 0; |
| 2532 | } |
| 2533 | |
Bram Moolenaar | 55d5c03 | 2010-07-17 23:52:29 +0200 | [diff] [blame] | 2534 | static lua_State * |
| 2535 | luaV_newstate(void) |
| 2536 | { |
Bram Moolenaar | 0ba0429 | 2010-07-14 23:23:17 +0200 | [diff] [blame] | 2537 | lua_State *L = luaL_newstate(); |
Bram Moolenaar | 2ab2e86 | 2019-12-04 21:24:53 +0100 | [diff] [blame] | 2538 | luaL_openlibs(L); // core libs |
| 2539 | lua_pushcfunction(L, luaopen_vim); // vim |
Bram Moolenaar | 0ba0429 | 2010-07-14 23:23:17 +0200 | [diff] [blame] | 2540 | lua_call(L, 0, 0); |
| 2541 | return L; |
| 2542 | } |
| 2543 | |
Bram Moolenaar | 55d5c03 | 2010-07-17 23:52:29 +0200 | [diff] [blame] | 2544 | static void |
| 2545 | luaV_setrange(lua_State *L, int line1, int line2) |
| 2546 | { |
Bram Moolenaar | 0ba0429 | 2010-07-14 23:23:17 +0200 | [diff] [blame] | 2547 | lua_getglobal(L, LUAVIM_NAME); |
| 2548 | lua_pushinteger(L, line1); |
| 2549 | lua_setfield(L, -2, "firstline"); |
| 2550 | lua_pushinteger(L, line2); |
| 2551 | lua_setfield(L, -2, "lastline"); |
Bram Moolenaar | 2ab2e86 | 2019-12-04 21:24:53 +0100 | [diff] [blame] | 2552 | lua_pop(L, 1); // vim table |
Bram Moolenaar | 0ba0429 | 2010-07-14 23:23:17 +0200 | [diff] [blame] | 2553 | } |
| 2554 | |
| 2555 | |
Bram Moolenaar | 2ab2e86 | 2019-12-04 21:24:53 +0100 | [diff] [blame] | 2556 | // ======= Interface ======= |
Bram Moolenaar | 0ba0429 | 2010-07-14 23:23:17 +0200 | [diff] [blame] | 2557 | |
| 2558 | static lua_State *L = NULL; |
| 2559 | |
Bram Moolenaar | 55d5c03 | 2010-07-17 23:52:29 +0200 | [diff] [blame] | 2560 | static int |
Bram Moolenaar | 1dced57 | 2012-04-05 16:54:08 +0200 | [diff] [blame] | 2561 | lua_isopen(void) |
Bram Moolenaar | 2bd6a1b | 2010-08-12 22:14:01 +0200 | [diff] [blame] | 2562 | { |
| 2563 | return L != NULL; |
| 2564 | } |
| 2565 | |
| 2566 | static int |
Bram Moolenaar | 55d5c03 | 2010-07-17 23:52:29 +0200 | [diff] [blame] | 2567 | lua_init(void) |
| 2568 | { |
Yegappan Lakshmanan | 0233bdf | 2023-01-12 12:33:30 +0000 | [diff] [blame] | 2569 | if (lua_isopen()) |
| 2570 | return OK; |
| 2571 | |
Bram Moolenaar | 0ba0429 | 2010-07-14 23:23:17 +0200 | [diff] [blame] | 2572 | #ifdef DYNAMIC_LUA |
Yegappan Lakshmanan | 0233bdf | 2023-01-12 12:33:30 +0000 | [diff] [blame] | 2573 | if (!lua_enabled(TRUE)) |
| 2574 | { |
| 2575 | emsg(_("Lua library cannot be loaded.")); |
| 2576 | return FAIL; |
Bram Moolenaar | 0ba0429 | 2010-07-14 23:23:17 +0200 | [diff] [blame] | 2577 | } |
Yegappan Lakshmanan | 0233bdf | 2023-01-12 12:33:30 +0000 | [diff] [blame] | 2578 | #endif |
| 2579 | L = luaV_newstate(); |
| 2580 | |
Bram Moolenaar | 0ba0429 | 2010-07-14 23:23:17 +0200 | [diff] [blame] | 2581 | return OK; |
| 2582 | } |
| 2583 | |
Bram Moolenaar | 55d5c03 | 2010-07-17 23:52:29 +0200 | [diff] [blame] | 2584 | void |
| 2585 | lua_end(void) |
| 2586 | { |
Yegappan Lakshmanan | 0233bdf | 2023-01-12 12:33:30 +0000 | [diff] [blame] | 2587 | if (!lua_isopen()) |
| 2588 | return; |
| 2589 | |
| 2590 | lua_close(L); |
| 2591 | L = NULL; |
Bram Moolenaar | 0ba0429 | 2010-07-14 23:23:17 +0200 | [diff] [blame] | 2592 | } |
| 2593 | |
Bram Moolenaar | 2ab2e86 | 2019-12-04 21:24:53 +0100 | [diff] [blame] | 2594 | /* |
| 2595 | * ex commands |
| 2596 | */ |
Bram Moolenaar | 55d5c03 | 2010-07-17 23:52:29 +0200 | [diff] [blame] | 2597 | void |
| 2598 | ex_lua(exarg_T *eap) |
| 2599 | { |
Bram Moolenaar | c8970b9 | 2020-10-26 20:18:08 +0100 | [diff] [blame] | 2600 | char *script = (char *)script_get(eap, eap->arg); |
| 2601 | |
| 2602 | if (!eap->skip && lua_init() == OK) |
Bram Moolenaar | 55d5c03 | 2010-07-17 23:52:29 +0200 | [diff] [blame] | 2603 | { |
Bram Moolenaar | c8970b9 | 2020-10-26 20:18:08 +0100 | [diff] [blame] | 2604 | char *s = script != NULL ? script : (char *)eap->arg; |
| 2605 | |
Bram Moolenaar | 55d5c03 | 2010-07-17 23:52:29 +0200 | [diff] [blame] | 2606 | luaV_setrange(L, eap->line1, eap->line2); |
| 2607 | if (luaL_loadbuffer(L, s, strlen(s), LUAVIM_CHUNKNAME) |
| 2608 | || lua_pcall(L, 0, 0, 0)) |
| 2609 | luaV_emsg(L); |
Bram Moolenaar | 0ba0429 | 2010-07-14 23:23:17 +0200 | [diff] [blame] | 2610 | } |
Bram Moolenaar | c8970b9 | 2020-10-26 20:18:08 +0100 | [diff] [blame] | 2611 | if (script != NULL) |
| 2612 | vim_free(script); |
Bram Moolenaar | 0ba0429 | 2010-07-14 23:23:17 +0200 | [diff] [blame] | 2613 | } |
| 2614 | |
Bram Moolenaar | 55d5c03 | 2010-07-17 23:52:29 +0200 | [diff] [blame] | 2615 | void |
| 2616 | ex_luado(exarg_T *eap) |
| 2617 | { |
Bram Moolenaar | 0ba0429 | 2010-07-14 23:23:17 +0200 | [diff] [blame] | 2618 | linenr_T l; |
| 2619 | const char *s = (const char *) eap->arg; |
| 2620 | luaL_Buffer b; |
| 2621 | size_t len; |
Bram Moolenaar | d58f03b | 2017-01-29 22:48:45 +0100 | [diff] [blame] | 2622 | buf_T *was_curbuf = curbuf; |
| 2623 | |
Bram Moolenaar | 0ba0429 | 2010-07-14 23:23:17 +0200 | [diff] [blame] | 2624 | if (lua_init() == FAIL) return; |
Bram Moolenaar | 55d5c03 | 2010-07-17 23:52:29 +0200 | [diff] [blame] | 2625 | if (u_save(eap->line1 - 1, eap->line2 + 1) == FAIL) |
| 2626 | { |
Bram Moolenaar | f9e3e09 | 2019-01-13 23:38:42 +0100 | [diff] [blame] | 2627 | emsg(_("cannot save undo information")); |
Bram Moolenaar | 55d5c03 | 2010-07-17 23:52:29 +0200 | [diff] [blame] | 2628 | return; |
Bram Moolenaar | 0ba0429 | 2010-07-14 23:23:17 +0200 | [diff] [blame] | 2629 | } |
| 2630 | luaV_setrange(L, eap->line1, eap->line2); |
| 2631 | luaL_buffinit(L, &b); |
Bram Moolenaar | 2ab2e86 | 2019-12-04 21:24:53 +0100 | [diff] [blame] | 2632 | luaL_addlstring(&b, "return function(line, linenr) ", 30); // header |
Bram Moolenaar | 0ba0429 | 2010-07-14 23:23:17 +0200 | [diff] [blame] | 2633 | luaL_addlstring(&b, s, strlen(s)); |
Bram Moolenaar | 2ab2e86 | 2019-12-04 21:24:53 +0100 | [diff] [blame] | 2634 | luaL_addlstring(&b, " end", 4); // footer |
Bram Moolenaar | 0ba0429 | 2010-07-14 23:23:17 +0200 | [diff] [blame] | 2635 | luaL_pushresult(&b); |
| 2636 | s = lua_tolstring(L, -1, &len); |
Bram Moolenaar | 55d5c03 | 2010-07-17 23:52:29 +0200 | [diff] [blame] | 2637 | if (luaL_loadbuffer(L, s, len, LUAVIM_CHUNKNAME)) |
| 2638 | { |
| 2639 | luaV_emsg(L); |
Bram Moolenaar | 2ab2e86 | 2019-12-04 21:24:53 +0100 | [diff] [blame] | 2640 | lua_pop(L, 1); // function body |
Bram Moolenaar | 55d5c03 | 2010-07-17 23:52:29 +0200 | [diff] [blame] | 2641 | return; |
Bram Moolenaar | 0ba0429 | 2010-07-14 23:23:17 +0200 | [diff] [blame] | 2642 | } |
| 2643 | lua_call(L, 0, 1); |
Bram Moolenaar | 2ab2e86 | 2019-12-04 21:24:53 +0100 | [diff] [blame] | 2644 | lua_replace(L, -2); // function -> body |
Bram Moolenaar | 55d5c03 | 2010-07-17 23:52:29 +0200 | [diff] [blame] | 2645 | for (l = eap->line1; l <= eap->line2; l++) |
| 2646 | { |
Bram Moolenaar | e49b8e8 | 2020-07-01 13:52:55 +0200 | [diff] [blame] | 2647 | // Check the line number, the command may have deleted lines. |
Bram Moolenaar | d58f03b | 2017-01-29 22:48:45 +0100 | [diff] [blame] | 2648 | if (l > curbuf->b_ml.ml_line_count) |
| 2649 | break; |
| 2650 | |
Bram Moolenaar | 2ab2e86 | 2019-12-04 21:24:53 +0100 | [diff] [blame] | 2651 | lua_pushvalue(L, -1); // function |
| 2652 | luaV_pushline(L, curbuf, l); // current line as arg |
| 2653 | lua_pushinteger(L, l); // current line number as arg |
Bram Moolenaar | bd2f3c3 | 2012-04-06 14:31:00 +0200 | [diff] [blame] | 2654 | if (lua_pcall(L, 2, 1, 0)) |
Bram Moolenaar | 55d5c03 | 2010-07-17 23:52:29 +0200 | [diff] [blame] | 2655 | { |
| 2656 | luaV_emsg(L); |
| 2657 | break; |
| 2658 | } |
Bram Moolenaar | 2ab2e86 | 2019-12-04 21:24:53 +0100 | [diff] [blame] | 2659 | // Catch the command switching to another buffer. |
Bram Moolenaar | d58f03b | 2017-01-29 22:48:45 +0100 | [diff] [blame] | 2660 | if (curbuf != was_curbuf) |
| 2661 | break; |
Bram Moolenaar | 2ab2e86 | 2019-12-04 21:24:53 +0100 | [diff] [blame] | 2662 | if (lua_isstring(L, -1)) // update line? |
Bram Moolenaar | 55d5c03 | 2010-07-17 23:52:29 +0200 | [diff] [blame] | 2663 | { |
Bram Moolenaar | 0ba0429 | 2010-07-14 23:23:17 +0200 | [diff] [blame] | 2664 | #ifdef HAVE_SANDBOX |
Bram Moolenaar | 55d5c03 | 2010-07-17 23:52:29 +0200 | [diff] [blame] | 2665 | luaV_checksandbox(L); |
Bram Moolenaar | 0ba0429 | 2010-07-14 23:23:17 +0200 | [diff] [blame] | 2666 | #endif |
Bram Moolenaar | 55d5c03 | 2010-07-17 23:52:29 +0200 | [diff] [blame] | 2667 | ml_replace(l, luaV_toline(L, -1), TRUE); |
| 2668 | changed_bytes(l, 0); |
Bram Moolenaar | 2ab2e86 | 2019-12-04 21:24:53 +0100 | [diff] [blame] | 2669 | lua_pop(L, 1); // result from luaV_toline |
Bram Moolenaar | 55d5c03 | 2010-07-17 23:52:29 +0200 | [diff] [blame] | 2670 | } |
Bram Moolenaar | 2ab2e86 | 2019-12-04 21:24:53 +0100 | [diff] [blame] | 2671 | lua_pop(L, 1); // line |
Bram Moolenaar | 0ba0429 | 2010-07-14 23:23:17 +0200 | [diff] [blame] | 2672 | } |
Bram Moolenaar | 2ab2e86 | 2019-12-04 21:24:53 +0100 | [diff] [blame] | 2673 | lua_pop(L, 1); // function |
Bram Moolenaar | 0ba0429 | 2010-07-14 23:23:17 +0200 | [diff] [blame] | 2674 | check_cursor(); |
Bram Moolenaar | a4d158b | 2022-08-14 14:17:45 +0100 | [diff] [blame] | 2675 | update_screen(UPD_NOT_VALID); |
Bram Moolenaar | 0ba0429 | 2010-07-14 23:23:17 +0200 | [diff] [blame] | 2676 | } |
| 2677 | |
Bram Moolenaar | 55d5c03 | 2010-07-17 23:52:29 +0200 | [diff] [blame] | 2678 | void |
| 2679 | ex_luafile(exarg_T *eap) |
| 2680 | { |
| 2681 | if (lua_init() == FAIL) |
| 2682 | return; |
| 2683 | if (!eap->skip) |
| 2684 | { |
| 2685 | luaV_setrange(L, eap->line1, eap->line2); |
| 2686 | if (luaL_loadfile(L, (char *) eap->arg) || lua_pcall(L, 0, 0, 0)) |
| 2687 | luaV_emsg(L); |
Bram Moolenaar | 0ba0429 | 2010-07-14 23:23:17 +0200 | [diff] [blame] | 2688 | } |
| 2689 | } |
| 2690 | |
Bram Moolenaar | 1dced57 | 2012-04-05 16:54:08 +0200 | [diff] [blame] | 2691 | #define luaV_freetype(typ,tname) \ |
| 2692 | void \ |
| 2693 | lua_##tname##_free(typ *o) \ |
| 2694 | { \ |
| 2695 | if (!lua_isopen()) return; \ |
| 2696 | luaV_getfield(L, LUAVIM_FREE); \ |
| 2697 | lua_pushlightuserdata(L, (void *) o); \ |
| 2698 | lua_call(L, 1, 0); \ |
| 2699 | } |
| 2700 | |
| 2701 | luaV_freetype(buf_T, buffer) |
| 2702 | luaV_freetype(win_T, window) |
| 2703 | |
Bram Moolenaar | 55d5c03 | 2010-07-17 23:52:29 +0200 | [diff] [blame] | 2704 | void |
Bram Moolenaar | 4eefe47 | 2019-03-19 21:59:19 +0100 | [diff] [blame] | 2705 | do_luaeval(char_u *str, typval_T *arg, typval_T *rettv) |
Bram Moolenaar | 55d5c03 | 2010-07-17 23:52:29 +0200 | [diff] [blame] | 2706 | { |
Bram Moolenaar | 1dced57 | 2012-04-05 16:54:08 +0200 | [diff] [blame] | 2707 | lua_init(); |
| 2708 | luaV_getfield(L, LUAVIM_LUAEVAL); |
| 2709 | lua_pushstring(L, (char *) str); |
| 2710 | lua_pushlightuserdata(L, (void *) arg); |
| 2711 | lua_pushlightuserdata(L, (void *) rettv); |
| 2712 | lua_call(L, 3, 0); |
Bram Moolenaar | 0ba0429 | 2010-07-14 23:23:17 +0200 | [diff] [blame] | 2713 | } |
| 2714 | |
Bram Moolenaar | 2459a5e | 2015-02-03 12:55:18 +0100 | [diff] [blame] | 2715 | int |
Bram Moolenaar | 4eefe47 | 2019-03-19 21:59:19 +0100 | [diff] [blame] | 2716 | set_ref_in_lua(int copyID) |
Bram Moolenaar | 55d5c03 | 2010-07-17 23:52:29 +0200 | [diff] [blame] | 2717 | { |
Bram Moolenaar | 2459a5e | 2015-02-03 12:55:18 +0100 | [diff] [blame] | 2718 | int aborted = 0; |
| 2719 | |
Yegappan Lakshmanan | 0233bdf | 2023-01-12 12:33:30 +0000 | [diff] [blame] | 2720 | if (!lua_isopen()) |
| 2721 | return 0; |
| 2722 | |
| 2723 | luaV_getfield(L, LUAVIM_SETREF); |
| 2724 | // call the function with 1 arg, getting 1 result back |
| 2725 | lua_pushinteger(L, copyID); |
| 2726 | lua_call(L, 1, 1); |
| 2727 | // get the result |
| 2728 | aborted = lua_tointeger(L, -1); |
| 2729 | // pop result off the stack |
| 2730 | lua_pop(L, 1); |
| 2731 | |
Bram Moolenaar | 2459a5e | 2015-02-03 12:55:18 +0100 | [diff] [blame] | 2732 | return aborted; |
Bram Moolenaar | 0ba0429 | 2010-07-14 23:23:17 +0200 | [diff] [blame] | 2733 | } |
| 2734 | |
Bram Moolenaar | 788fbb4 | 2020-05-31 14:08:12 +0200 | [diff] [blame] | 2735 | void |
| 2736 | update_package_paths_in_lua() |
| 2737 | { |
Yegappan Lakshmanan | 0233bdf | 2023-01-12 12:33:30 +0000 | [diff] [blame] | 2738 | if (!lua_isopen()) |
| 2739 | return; |
Bram Moolenaar | 788fbb4 | 2020-05-31 14:08:12 +0200 | [diff] [blame] | 2740 | |
Yegappan Lakshmanan | 0233bdf | 2023-01-12 12:33:30 +0000 | [diff] [blame] | 2741 | lua_getglobal(L, "vim"); |
| 2742 | lua_getfield(L, -1, "_update_package_paths"); |
| 2743 | |
| 2744 | if (lua_pcall(L, 0, 0, 0)) |
| 2745 | luaV_emsg(L); |
Bram Moolenaar | 788fbb4 | 2020-05-31 14:08:12 +0200 | [diff] [blame] | 2746 | } |
| 2747 | |
Bram Moolenaar | 801ab06 | 2020-06-25 19:27:56 +0200 | [diff] [blame] | 2748 | /* |
| 2749 | * Native C function callback |
| 2750 | */ |
| 2751 | static int |
| 2752 | luaV_call_lua_func( |
| 2753 | int argcount, |
| 2754 | typval_T *argvars, |
| 2755 | typval_T *rettv, |
| 2756 | void *state) |
| 2757 | { |
| 2758 | int i; |
| 2759 | int luaargcount = argcount; |
| 2760 | luaV_CFuncState *funcstate = (luaV_CFuncState*)state; |
| 2761 | lua_rawgeti(funcstate->L, LUA_REGISTRYINDEX, funcstate->lua_funcref); |
| 2762 | |
| 2763 | if (funcstate->lua_tableref != LUA_NOREF) |
| 2764 | { |
| 2765 | // First arg for metatable __call method is a table |
| 2766 | luaargcount += 1; |
| 2767 | lua_rawgeti(funcstate->L, LUA_REGISTRYINDEX, funcstate->lua_tableref); |
| 2768 | } |
| 2769 | |
| 2770 | for (i = 0; i < argcount; ++i) |
| 2771 | luaV_pushtypval(funcstate->L, &argvars[i]); |
| 2772 | |
| 2773 | if (lua_pcall(funcstate->L, luaargcount, 1, 0)) |
| 2774 | { |
| 2775 | luaV_emsg(funcstate->L); |
| 2776 | return FCERR_OTHER; |
| 2777 | } |
| 2778 | |
| 2779 | luaV_checktypval(funcstate->L, -1, rettv, "get return value"); |
| 2780 | return FCERR_NONE; |
| 2781 | } |
| 2782 | |
| 2783 | /* |
| 2784 | * Free up any lua references held by the func state. |
| 2785 | */ |
| 2786 | static void |
| 2787 | luaV_call_lua_func_free(void *state) |
| 2788 | { |
| 2789 | luaV_CFuncState *funcstate = (luaV_CFuncState*)state; |
| 2790 | luaL_unref(L, LUA_REGISTRYINDEX, funcstate->lua_funcref); |
| 2791 | funcstate->L = NULL; |
| 2792 | if (funcstate->lua_tableref != LUA_NOREF) |
| 2793 | luaL_unref(L, LUA_REGISTRYINDEX, funcstate->lua_tableref); |
| 2794 | VIM_CLEAR(funcstate); |
| 2795 | } |
| 2796 | |
Bram Moolenaar | 0ba0429 | 2010-07-14 23:23:17 +0200 | [diff] [blame] | 2797 | #endif |