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