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 | |
| 18 | /* Only do the following when the feature is enabled. Needed for "make |
| 19 | * depend". */ |
| 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 | |
| 27 | typedef buf_T *luaV_Buffer; |
| 28 | typedef win_T *luaV_Window; |
Bram Moolenaar | 1dced57 | 2012-04-05 16:54:08 +0200 | [diff] [blame] | 29 | typedef dict_T *luaV_Dict; |
| 30 | typedef list_T *luaV_List; |
Bram Moolenaar | 0ba0429 | 2010-07-14 23:23:17 +0200 | [diff] [blame] | 31 | typedef void (*msgfunc_T)(char_u *); |
| 32 | |
Bram Moolenaar | 1dced57 | 2012-04-05 16:54:08 +0200 | [diff] [blame] | 33 | static const char LUAVIM_DICT[] = "dict"; |
| 34 | static const char LUAVIM_LIST[] = "list"; |
Bram Moolenaar | 0ba0429 | 2010-07-14 23:23:17 +0200 | [diff] [blame] | 35 | static const char LUAVIM_BUFFER[] = "buffer"; |
| 36 | static const char LUAVIM_WINDOW[] = "window"; |
| 37 | static const char LUAVIM_FREE[] = "luaV_free"; |
Bram Moolenaar | 1dced57 | 2012-04-05 16:54:08 +0200 | [diff] [blame] | 38 | static const char LUAVIM_LUAEVAL[] = "luaV_luaeval"; |
| 39 | static const char LUAVIM_SETREF[] = "luaV_setref"; |
Bram Moolenaar | 0ba0429 | 2010-07-14 23:23:17 +0200 | [diff] [blame] | 40 | |
Bram Moolenaar | 1dced57 | 2012-04-05 16:54:08 +0200 | [diff] [blame] | 41 | /* most functions are closures with a cache table as first upvalue; |
| 42 | * get/setudata manage references to vim userdata in cache table through |
| 43 | * object pointers (light userdata) */ |
| 44 | #define luaV_getudata(L, v) \ |
| 45 | lua_pushlightuserdata((L), (void *) (v)); \ |
| 46 | lua_rawget((L), lua_upvalueindex(1)) |
| 47 | #define luaV_setudata(L, v) \ |
| 48 | lua_pushlightuserdata((L), (void *) (v)); \ |
| 49 | lua_pushvalue((L), -2); \ |
| 50 | lua_rawset((L), lua_upvalueindex(1)) |
Bram Moolenaar | 0ba0429 | 2010-07-14 23:23:17 +0200 | [diff] [blame] | 51 | #define luaV_getfield(L, s) \ |
| 52 | lua_pushlightuserdata((L), (void *)(s)); \ |
| 53 | lua_rawget((L), LUA_REGISTRYINDEX) |
| 54 | #define luaV_checksandbox(L) \ |
| 55 | if (sandbox) luaL_error((L), "not allowed in sandbox") |
| 56 | #define luaV_msg(L) luaV_msgfunc((L), (msgfunc_T) msg) |
| 57 | #define luaV_emsg(L) luaV_msgfunc((L), (msgfunc_T) emsg) |
| 58 | |
Bram Moolenaar | 1dced57 | 2012-04-05 16:54:08 +0200 | [diff] [blame] | 59 | static luaV_List *luaV_pushlist (lua_State *L, list_T *lis); |
| 60 | static luaV_Dict *luaV_pushdict (lua_State *L, dict_T *dic); |
| 61 | |
| 62 | #if LUA_VERSION_NUM <= 501 |
| 63 | #define luaV_openlib(L, l, n) luaL_openlib(L, NULL, l, n) |
| 64 | #define luaL_typeerror luaL_typerror |
| 65 | #else |
| 66 | #define luaV_openlib luaL_setfuncs |
| 67 | #endif |
Bram Moolenaar | 0ba0429 | 2010-07-14 23:23:17 +0200 | [diff] [blame] | 68 | |
| 69 | #ifdef DYNAMIC_LUA |
Bram Moolenaar | 2334b6d | 2010-07-22 21:32:16 +0200 | [diff] [blame] | 70 | |
| 71 | #ifndef WIN3264 |
| 72 | # include <dlfcn.h> |
| 73 | # define HANDLE void* |
| 74 | # define load_dll(n) dlopen((n), RTLD_LAZY|RTLD_GLOBAL) |
| 75 | # define symbol_from_dll dlsym |
| 76 | # define close_dll dlclose |
| 77 | #else |
Bram Moolenaar | ebbcb82 | 2010-10-23 14:02:54 +0200 | [diff] [blame] | 78 | # define load_dll vimLoadLib |
Bram Moolenaar | 2334b6d | 2010-07-22 21:32:16 +0200 | [diff] [blame] | 79 | # define symbol_from_dll GetProcAddress |
| 80 | # define close_dll FreeLibrary |
| 81 | #endif |
| 82 | |
Bram Moolenaar | 0ba0429 | 2010-07-14 23:23:17 +0200 | [diff] [blame] | 83 | /* lauxlib */ |
Bram Moolenaar | 1dced57 | 2012-04-05 16:54:08 +0200 | [diff] [blame] | 84 | #if LUA_VERSION_NUM <= 501 |
Bram Moolenaar | 0ba0429 | 2010-07-14 23:23:17 +0200 | [diff] [blame] | 85 | #define luaL_register dll_luaL_register |
Bram Moolenaar | 1dced57 | 2012-04-05 16:54:08 +0200 | [diff] [blame] | 86 | #define luaL_prepbuffer dll_luaL_prepbuffer |
| 87 | #define luaL_openlib dll_luaL_openlib |
Bram Moolenaar | 0ba0429 | 2010-07-14 23:23:17 +0200 | [diff] [blame] | 88 | #define luaL_typerror dll_luaL_typerror |
Bram Moolenaar | 1dced57 | 2012-04-05 16:54:08 +0200 | [diff] [blame] | 89 | #define luaL_loadfile dll_luaL_loadfile |
| 90 | #define luaL_loadbuffer dll_luaL_loadbuffer |
| 91 | #else |
| 92 | #define luaL_prepbuffsize dll_luaL_prepbuffsize |
| 93 | #define luaL_setfuncs dll_luaL_setfuncs |
| 94 | #define luaL_loadfilex dll_luaL_loadfilex |
| 95 | #define luaL_loadbufferx dll_luaL_loadbufferx |
| 96 | #define luaL_argerror dll_luaL_argerror |
| 97 | #endif |
Bram Moolenaar | bd2f3c3 | 2012-04-06 14:31:00 +0200 | [diff] [blame] | 98 | #define luaL_checkany dll_luaL_checkany |
Bram Moolenaar | 0ba0429 | 2010-07-14 23:23:17 +0200 | [diff] [blame] | 99 | #define luaL_checklstring dll_luaL_checklstring |
| 100 | #define luaL_checkinteger dll_luaL_checkinteger |
| 101 | #define luaL_optinteger dll_luaL_optinteger |
| 102 | #define luaL_checktype dll_luaL_checktype |
| 103 | #define luaL_error dll_luaL_error |
Bram Moolenaar | 0ba0429 | 2010-07-14 23:23:17 +0200 | [diff] [blame] | 104 | #define luaL_newstate dll_luaL_newstate |
| 105 | #define luaL_buffinit dll_luaL_buffinit |
Bram Moolenaar | 0ba0429 | 2010-07-14 23:23:17 +0200 | [diff] [blame] | 106 | #define luaL_addlstring dll_luaL_addlstring |
| 107 | #define luaL_pushresult dll_luaL_pushresult |
| 108 | /* lua */ |
Bram Moolenaar | 1dced57 | 2012-04-05 16:54:08 +0200 | [diff] [blame] | 109 | #if LUA_VERSION_NUM <= 501 |
| 110 | #define lua_tonumber dll_lua_tonumber |
| 111 | #define lua_tointeger dll_lua_tointeger |
| 112 | #define lua_call dll_lua_call |
| 113 | #define lua_pcall dll_lua_pcall |
| 114 | #else |
| 115 | #define lua_tonumberx dll_lua_tonumberx |
| 116 | #define lua_tointegerx dll_lua_tointegerx |
| 117 | #define lua_callk dll_lua_callk |
| 118 | #define lua_pcallk dll_lua_pcallk |
| 119 | #define lua_getglobal dll_lua_getglobal |
| 120 | #define lua_setglobal dll_lua_setglobal |
Bram Moolenaar | 1dced57 | 2012-04-05 16:54:08 +0200 | [diff] [blame] | 121 | #endif |
Bram Moolenaar | bd2f3c3 | 2012-04-06 14:31:00 +0200 | [diff] [blame] | 122 | #define lua_typename dll_lua_typename |
Bram Moolenaar | 0ba0429 | 2010-07-14 23:23:17 +0200 | [diff] [blame] | 123 | #define lua_close dll_lua_close |
| 124 | #define lua_gettop dll_lua_gettop |
| 125 | #define lua_settop dll_lua_settop |
| 126 | #define lua_pushvalue dll_lua_pushvalue |
| 127 | #define lua_replace dll_lua_replace |
Bram Moolenaar | 1dced57 | 2012-04-05 16:54:08 +0200 | [diff] [blame] | 128 | #define lua_remove dll_lua_remove |
Bram Moolenaar | 0ba0429 | 2010-07-14 23:23:17 +0200 | [diff] [blame] | 129 | #define lua_isnumber dll_lua_isnumber |
| 130 | #define lua_isstring dll_lua_isstring |
| 131 | #define lua_type dll_lua_type |
| 132 | #define lua_rawequal dll_lua_rawequal |
Bram Moolenaar | 0ba0429 | 2010-07-14 23:23:17 +0200 | [diff] [blame] | 133 | #define lua_toboolean dll_lua_toboolean |
| 134 | #define lua_tolstring dll_lua_tolstring |
| 135 | #define lua_touserdata dll_lua_touserdata |
| 136 | #define lua_pushnil dll_lua_pushnil |
| 137 | #define lua_pushnumber dll_lua_pushnumber |
| 138 | #define lua_pushinteger dll_lua_pushinteger |
| 139 | #define lua_pushlstring dll_lua_pushlstring |
| 140 | #define lua_pushstring dll_lua_pushstring |
| 141 | #define lua_pushfstring dll_lua_pushfstring |
| 142 | #define lua_pushcclosure dll_lua_pushcclosure |
| 143 | #define lua_pushboolean dll_lua_pushboolean |
| 144 | #define lua_pushlightuserdata dll_lua_pushlightuserdata |
| 145 | #define lua_getfield dll_lua_getfield |
| 146 | #define lua_rawget dll_lua_rawget |
Bram Moolenaar | 1dced57 | 2012-04-05 16:54:08 +0200 | [diff] [blame] | 147 | #define lua_rawgeti dll_lua_rawgeti |
Bram Moolenaar | 0ba0429 | 2010-07-14 23:23:17 +0200 | [diff] [blame] | 148 | #define lua_createtable dll_lua_createtable |
| 149 | #define lua_newuserdata dll_lua_newuserdata |
| 150 | #define lua_getmetatable dll_lua_getmetatable |
| 151 | #define lua_setfield dll_lua_setfield |
| 152 | #define lua_rawset dll_lua_rawset |
| 153 | #define lua_rawseti dll_lua_rawseti |
| 154 | #define lua_setmetatable dll_lua_setmetatable |
Bram Moolenaar | bd2f3c3 | 2012-04-06 14:31:00 +0200 | [diff] [blame] | 155 | #define lua_next dll_lua_next |
Bram Moolenaar | 0ba0429 | 2010-07-14 23:23:17 +0200 | [diff] [blame] | 156 | /* libs */ |
| 157 | #define luaopen_base dll_luaopen_base |
| 158 | #define luaopen_table dll_luaopen_table |
| 159 | #define luaopen_string dll_luaopen_string |
| 160 | #define luaopen_math dll_luaopen_math |
Bram Moolenaar | 16c98f9 | 2010-07-28 22:46:08 +0200 | [diff] [blame] | 161 | #define luaopen_io dll_luaopen_io |
Bram Moolenaar | 0ba0429 | 2010-07-14 23:23:17 +0200 | [diff] [blame] | 162 | #define luaopen_os dll_luaopen_os |
| 163 | #define luaopen_package dll_luaopen_package |
| 164 | #define luaopen_debug dll_luaopen_debug |
Bram Moolenaar | 16c98f9 | 2010-07-28 22:46:08 +0200 | [diff] [blame] | 165 | #define luaL_openlibs dll_luaL_openlibs |
Bram Moolenaar | 0ba0429 | 2010-07-14 23:23:17 +0200 | [diff] [blame] | 166 | |
| 167 | /* lauxlib */ |
Bram Moolenaar | 1dced57 | 2012-04-05 16:54:08 +0200 | [diff] [blame] | 168 | #if LUA_VERSION_NUM <= 501 |
Bram Moolenaar | 0ba0429 | 2010-07-14 23:23:17 +0200 | [diff] [blame] | 169 | 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] | 170 | char *(*dll_luaL_prepbuffer) (luaL_Buffer *B); |
| 171 | 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] | 172 | int (*dll_luaL_typerror) (lua_State *L, int narg, const char *tname); |
Bram Moolenaar | 1dced57 | 2012-04-05 16:54:08 +0200 | [diff] [blame] | 173 | int (*dll_luaL_loadfile) (lua_State *L, const char *filename); |
| 174 | int (*dll_luaL_loadbuffer) (lua_State *L, const char *buff, size_t sz, const char *name); |
| 175 | #else |
| 176 | char *(*dll_luaL_prepbuffsize) (luaL_Buffer *B, size_t sz); |
| 177 | void (*dll_luaL_setfuncs) (lua_State *L, const luaL_Reg *l, int nup); |
| 178 | int (*dll_luaL_loadfilex) (lua_State *L, const char *filename, const char *mode); |
| 179 | int (*dll_luaL_loadbufferx) (lua_State *L, const char *buff, size_t sz, const char *name, const char *mode); |
| 180 | int (*dll_luaL_argerror) (lua_State *L, int numarg, const char *extramsg); |
| 181 | #endif |
Bram Moolenaar | bd2f3c3 | 2012-04-06 14:31:00 +0200 | [diff] [blame] | 182 | void (*dll_luaL_checkany) (lua_State *L, int narg); |
Bram Moolenaar | 0ba0429 | 2010-07-14 23:23:17 +0200 | [diff] [blame] | 183 | const char *(*dll_luaL_checklstring) (lua_State *L, int numArg, size_t *l); |
| 184 | lua_Integer (*dll_luaL_checkinteger) (lua_State *L, int numArg); |
| 185 | lua_Integer (*dll_luaL_optinteger) (lua_State *L, int nArg, lua_Integer def); |
| 186 | void (*dll_luaL_checktype) (lua_State *L, int narg, int t); |
| 187 | int (*dll_luaL_error) (lua_State *L, const char *fmt, ...); |
Bram Moolenaar | 0ba0429 | 2010-07-14 23:23:17 +0200 | [diff] [blame] | 188 | lua_State *(*dll_luaL_newstate) (void); |
| 189 | void (*dll_luaL_buffinit) (lua_State *L, luaL_Buffer *B); |
Bram Moolenaar | 0ba0429 | 2010-07-14 23:23:17 +0200 | [diff] [blame] | 190 | void (*dll_luaL_addlstring) (luaL_Buffer *B, const char *s, size_t l); |
| 191 | void (*dll_luaL_pushresult) (luaL_Buffer *B); |
| 192 | /* lua */ |
Bram Moolenaar | 1dced57 | 2012-04-05 16:54:08 +0200 | [diff] [blame] | 193 | #if LUA_VERSION_NUM <= 501 |
| 194 | lua_Number (*dll_lua_tonumber) (lua_State *L, int idx); |
| 195 | lua_Integer (*dll_lua_tointeger) (lua_State *L, int idx); |
| 196 | void (*dll_lua_call) (lua_State *L, int nargs, int nresults); |
| 197 | int (*dll_lua_pcall) (lua_State *L, int nargs, int nresults, int errfunc); |
| 198 | #else |
| 199 | lua_Number (*dll_lua_tonumberx) (lua_State *L, int idx, int *isnum); |
| 200 | lua_Integer (*dll_lua_tointegerx) (lua_State *L, int idx, int *isnum); |
| 201 | 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] | 202 | lua_CFunction k); |
Bram Moolenaar | 1dced57 | 2012-04-05 16:54:08 +0200 | [diff] [blame] | 203 | 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] | 204 | int ctx, lua_CFunction k); |
Bram Moolenaar | 1dced57 | 2012-04-05 16:54:08 +0200 | [diff] [blame] | 205 | void (*dll_lua_getglobal) (lua_State *L, const char *var); |
| 206 | void (*dll_lua_setglobal) (lua_State *L, const char *var); |
Bram Moolenaar | 1dced57 | 2012-04-05 16:54:08 +0200 | [diff] [blame] | 207 | #endif |
Bram Moolenaar | bd2f3c3 | 2012-04-06 14:31:00 +0200 | [diff] [blame] | 208 | const char *(*dll_lua_typename) (lua_State *L, int tp); |
Bram Moolenaar | 0ba0429 | 2010-07-14 23:23:17 +0200 | [diff] [blame] | 209 | void (*dll_lua_close) (lua_State *L); |
| 210 | int (*dll_lua_gettop) (lua_State *L); |
| 211 | void (*dll_lua_settop) (lua_State *L, int idx); |
| 212 | void (*dll_lua_pushvalue) (lua_State *L, int idx); |
| 213 | void (*dll_lua_replace) (lua_State *L, int idx); |
Bram Moolenaar | 1dced57 | 2012-04-05 16:54:08 +0200 | [diff] [blame] | 214 | void (*dll_lua_remove) (lua_State *L, int idx); |
Bram Moolenaar | 0ba0429 | 2010-07-14 23:23:17 +0200 | [diff] [blame] | 215 | int (*dll_lua_isnumber) (lua_State *L, int idx); |
| 216 | int (*dll_lua_isstring) (lua_State *L, int idx); |
| 217 | int (*dll_lua_type) (lua_State *L, int idx); |
| 218 | int (*dll_lua_rawequal) (lua_State *L, int idx1, int idx2); |
Bram Moolenaar | 0ba0429 | 2010-07-14 23:23:17 +0200 | [diff] [blame] | 219 | int (*dll_lua_toboolean) (lua_State *L, int idx); |
| 220 | const char *(*dll_lua_tolstring) (lua_State *L, int idx, size_t *len); |
| 221 | void *(*dll_lua_touserdata) (lua_State *L, int idx); |
| 222 | void (*dll_lua_pushnil) (lua_State *L); |
| 223 | void (*dll_lua_pushnumber) (lua_State *L, lua_Number n); |
| 224 | void (*dll_lua_pushinteger) (lua_State *L, lua_Integer n); |
| 225 | void (*dll_lua_pushlstring) (lua_State *L, const char *s, size_t l); |
| 226 | void (*dll_lua_pushstring) (lua_State *L, const char *s); |
| 227 | const char *(*dll_lua_pushfstring) (lua_State *L, const char *fmt, ...); |
| 228 | void (*dll_lua_pushcclosure) (lua_State *L, lua_CFunction fn, int n); |
| 229 | void (*dll_lua_pushboolean) (lua_State *L, int b); |
| 230 | void (*dll_lua_pushlightuserdata) (lua_State *L, void *p); |
| 231 | void (*dll_lua_getfield) (lua_State *L, int idx, const char *k); |
| 232 | void (*dll_lua_rawget) (lua_State *L, int idx); |
Bram Moolenaar | 1dced57 | 2012-04-05 16:54:08 +0200 | [diff] [blame] | 233 | void (*dll_lua_rawgeti) (lua_State *L, int idx, int n); |
Bram Moolenaar | 0ba0429 | 2010-07-14 23:23:17 +0200 | [diff] [blame] | 234 | void (*dll_lua_createtable) (lua_State *L, int narr, int nrec); |
| 235 | void *(*dll_lua_newuserdata) (lua_State *L, size_t sz); |
| 236 | int (*dll_lua_getmetatable) (lua_State *L, int objindex); |
| 237 | void (*dll_lua_setfield) (lua_State *L, int idx, const char *k); |
| 238 | void (*dll_lua_rawset) (lua_State *L, int idx); |
| 239 | void (*dll_lua_rawseti) (lua_State *L, int idx, int n); |
| 240 | int (*dll_lua_setmetatable) (lua_State *L, int objindex); |
Bram Moolenaar | bd2f3c3 | 2012-04-06 14:31:00 +0200 | [diff] [blame] | 241 | int (*dll_lua_next) (lua_State *L, int idx); |
Bram Moolenaar | 0ba0429 | 2010-07-14 23:23:17 +0200 | [diff] [blame] | 242 | /* libs */ |
| 243 | int (*dll_luaopen_base) (lua_State *L); |
| 244 | int (*dll_luaopen_table) (lua_State *L); |
| 245 | int (*dll_luaopen_string) (lua_State *L); |
| 246 | int (*dll_luaopen_math) (lua_State *L); |
Bram Moolenaar | 16c98f9 | 2010-07-28 22:46:08 +0200 | [diff] [blame] | 247 | int (*dll_luaopen_io) (lua_State *L); |
Bram Moolenaar | 0ba0429 | 2010-07-14 23:23:17 +0200 | [diff] [blame] | 248 | int (*dll_luaopen_os) (lua_State *L); |
| 249 | int (*dll_luaopen_package) (lua_State *L); |
| 250 | int (*dll_luaopen_debug) (lua_State *L); |
Bram Moolenaar | 16c98f9 | 2010-07-28 22:46:08 +0200 | [diff] [blame] | 251 | void (*dll_luaL_openlibs) (lua_State *L); |
Bram Moolenaar | 0ba0429 | 2010-07-14 23:23:17 +0200 | [diff] [blame] | 252 | |
| 253 | typedef void **luaV_function; |
| 254 | typedef struct { |
| 255 | const char *name; |
| 256 | luaV_function func; |
| 257 | } luaV_Reg; |
| 258 | |
| 259 | static const luaV_Reg luaV_dll[] = { |
| 260 | /* lauxlib */ |
Bram Moolenaar | 1dced57 | 2012-04-05 16:54:08 +0200 | [diff] [blame] | 261 | #if LUA_VERSION_NUM <= 501 |
Bram Moolenaar | 0ba0429 | 2010-07-14 23:23:17 +0200 | [diff] [blame] | 262 | {"luaL_register", (luaV_function) &dll_luaL_register}, |
Bram Moolenaar | 1dced57 | 2012-04-05 16:54:08 +0200 | [diff] [blame] | 263 | {"luaL_prepbuffer", (luaV_function) &dll_luaL_prepbuffer}, |
| 264 | {"luaL_openlib", (luaV_function) &dll_luaL_openlib}, |
Bram Moolenaar | 0ba0429 | 2010-07-14 23:23:17 +0200 | [diff] [blame] | 265 | {"luaL_typerror", (luaV_function) &dll_luaL_typerror}, |
Bram Moolenaar | 1dced57 | 2012-04-05 16:54:08 +0200 | [diff] [blame] | 266 | {"luaL_loadfile", (luaV_function) &dll_luaL_loadfile}, |
| 267 | {"luaL_loadbuffer", (luaV_function) &dll_luaL_loadbuffer}, |
| 268 | #else |
| 269 | {"luaL_prepbuffsize", (luaV_function) &dll_luaL_prepbuffsize}, |
| 270 | {"luaL_setfuncs", (luaV_function) &dll_luaL_setfuncs}, |
| 271 | {"luaL_loadfilex", (luaV_function) &dll_luaL_loadfilex}, |
| 272 | {"luaL_loadbufferx", (luaV_function) &dll_luaL_loadbufferx}, |
| 273 | {"luaL_argerror", (luaV_function) &dll_luaL_argerror}, |
| 274 | #endif |
Bram Moolenaar | bd2f3c3 | 2012-04-06 14:31:00 +0200 | [diff] [blame] | 275 | {"luaL_checkany", (luaV_function) &dll_luaL_checkany}, |
Bram Moolenaar | 0ba0429 | 2010-07-14 23:23:17 +0200 | [diff] [blame] | 276 | {"luaL_checklstring", (luaV_function) &dll_luaL_checklstring}, |
| 277 | {"luaL_checkinteger", (luaV_function) &dll_luaL_checkinteger}, |
| 278 | {"luaL_optinteger", (luaV_function) &dll_luaL_optinteger}, |
| 279 | {"luaL_checktype", (luaV_function) &dll_luaL_checktype}, |
| 280 | {"luaL_error", (luaV_function) &dll_luaL_error}, |
Bram Moolenaar | 0ba0429 | 2010-07-14 23:23:17 +0200 | [diff] [blame] | 281 | {"luaL_newstate", (luaV_function) &dll_luaL_newstate}, |
| 282 | {"luaL_buffinit", (luaV_function) &dll_luaL_buffinit}, |
Bram Moolenaar | 0ba0429 | 2010-07-14 23:23:17 +0200 | [diff] [blame] | 283 | {"luaL_addlstring", (luaV_function) &dll_luaL_addlstring}, |
| 284 | {"luaL_pushresult", (luaV_function) &dll_luaL_pushresult}, |
| 285 | /* lua */ |
Bram Moolenaar | 1dced57 | 2012-04-05 16:54:08 +0200 | [diff] [blame] | 286 | #if LUA_VERSION_NUM <= 501 |
| 287 | {"lua_tonumber", (luaV_function) &dll_lua_tonumber}, |
| 288 | {"lua_tointeger", (luaV_function) &dll_lua_tointeger}, |
| 289 | {"lua_call", (luaV_function) &dll_lua_call}, |
| 290 | {"lua_pcall", (luaV_function) &dll_lua_pcall}, |
| 291 | #else |
| 292 | {"lua_tonumberx", (luaV_function) &dll_lua_tonumberx}, |
| 293 | {"lua_tointegerx", (luaV_function) &dll_lua_tointegerx}, |
| 294 | {"lua_callk", (luaV_function) &dll_lua_callk}, |
| 295 | {"lua_pcallk", (luaV_function) &dll_lua_pcallk}, |
| 296 | {"lua_getglobal", (luaV_function) &dll_lua_getglobal}, |
| 297 | {"lua_setglobal", (luaV_function) &dll_lua_setglobal}, |
Bram Moolenaar | 1dced57 | 2012-04-05 16:54:08 +0200 | [diff] [blame] | 298 | #endif |
Bram Moolenaar | bd2f3c3 | 2012-04-06 14:31:00 +0200 | [diff] [blame] | 299 | {"lua_typename", (luaV_function) &dll_lua_typename}, |
Bram Moolenaar | 0ba0429 | 2010-07-14 23:23:17 +0200 | [diff] [blame] | 300 | {"lua_close", (luaV_function) &dll_lua_close}, |
| 301 | {"lua_gettop", (luaV_function) &dll_lua_gettop}, |
| 302 | {"lua_settop", (luaV_function) &dll_lua_settop}, |
| 303 | {"lua_pushvalue", (luaV_function) &dll_lua_pushvalue}, |
| 304 | {"lua_replace", (luaV_function) &dll_lua_replace}, |
Bram Moolenaar | 1dced57 | 2012-04-05 16:54:08 +0200 | [diff] [blame] | 305 | {"lua_remove", (luaV_function) &dll_lua_remove}, |
Bram Moolenaar | 0ba0429 | 2010-07-14 23:23:17 +0200 | [diff] [blame] | 306 | {"lua_isnumber", (luaV_function) &dll_lua_isnumber}, |
| 307 | {"lua_isstring", (luaV_function) &dll_lua_isstring}, |
| 308 | {"lua_type", (luaV_function) &dll_lua_type}, |
| 309 | {"lua_rawequal", (luaV_function) &dll_lua_rawequal}, |
Bram Moolenaar | 0ba0429 | 2010-07-14 23:23:17 +0200 | [diff] [blame] | 310 | {"lua_toboolean", (luaV_function) &dll_lua_toboolean}, |
| 311 | {"lua_tolstring", (luaV_function) &dll_lua_tolstring}, |
| 312 | {"lua_touserdata", (luaV_function) &dll_lua_touserdata}, |
| 313 | {"lua_pushnil", (luaV_function) &dll_lua_pushnil}, |
| 314 | {"lua_pushnumber", (luaV_function) &dll_lua_pushnumber}, |
| 315 | {"lua_pushinteger", (luaV_function) &dll_lua_pushinteger}, |
| 316 | {"lua_pushlstring", (luaV_function) &dll_lua_pushlstring}, |
| 317 | {"lua_pushstring", (luaV_function) &dll_lua_pushstring}, |
| 318 | {"lua_pushfstring", (luaV_function) &dll_lua_pushfstring}, |
| 319 | {"lua_pushcclosure", (luaV_function) &dll_lua_pushcclosure}, |
| 320 | {"lua_pushboolean", (luaV_function) &dll_lua_pushboolean}, |
| 321 | {"lua_pushlightuserdata", (luaV_function) &dll_lua_pushlightuserdata}, |
| 322 | {"lua_getfield", (luaV_function) &dll_lua_getfield}, |
| 323 | {"lua_rawget", (luaV_function) &dll_lua_rawget}, |
Bram Moolenaar | 1dced57 | 2012-04-05 16:54:08 +0200 | [diff] [blame] | 324 | {"lua_rawgeti", (luaV_function) &dll_lua_rawgeti}, |
Bram Moolenaar | 0ba0429 | 2010-07-14 23:23:17 +0200 | [diff] [blame] | 325 | {"lua_createtable", (luaV_function) &dll_lua_createtable}, |
| 326 | {"lua_newuserdata", (luaV_function) &dll_lua_newuserdata}, |
| 327 | {"lua_getmetatable", (luaV_function) &dll_lua_getmetatable}, |
| 328 | {"lua_setfield", (luaV_function) &dll_lua_setfield}, |
| 329 | {"lua_rawset", (luaV_function) &dll_lua_rawset}, |
| 330 | {"lua_rawseti", (luaV_function) &dll_lua_rawseti}, |
| 331 | {"lua_setmetatable", (luaV_function) &dll_lua_setmetatable}, |
Bram Moolenaar | bd2f3c3 | 2012-04-06 14:31:00 +0200 | [diff] [blame] | 332 | {"lua_next", (luaV_function) &dll_lua_next}, |
Bram Moolenaar | 0ba0429 | 2010-07-14 23:23:17 +0200 | [diff] [blame] | 333 | /* libs */ |
| 334 | {"luaopen_base", (luaV_function) &dll_luaopen_base}, |
| 335 | {"luaopen_table", (luaV_function) &dll_luaopen_table}, |
| 336 | {"luaopen_string", (luaV_function) &dll_luaopen_string}, |
| 337 | {"luaopen_math", (luaV_function) &dll_luaopen_math}, |
Bram Moolenaar | 16c98f9 | 2010-07-28 22:46:08 +0200 | [diff] [blame] | 338 | {"luaopen_io", (luaV_function) &dll_luaopen_io}, |
Bram Moolenaar | 0ba0429 | 2010-07-14 23:23:17 +0200 | [diff] [blame] | 339 | {"luaopen_os", (luaV_function) &dll_luaopen_os}, |
| 340 | {"luaopen_package", (luaV_function) &dll_luaopen_package}, |
| 341 | {"luaopen_debug", (luaV_function) &dll_luaopen_debug}, |
Bram Moolenaar | 16c98f9 | 2010-07-28 22:46:08 +0200 | [diff] [blame] | 342 | {"luaL_openlibs", (luaV_function) &dll_luaL_openlibs}, |
Bram Moolenaar | 0ba0429 | 2010-07-14 23:23:17 +0200 | [diff] [blame] | 343 | {NULL, NULL} |
| 344 | }; |
| 345 | |
Bram Moolenaar | 2334b6d | 2010-07-22 21:32:16 +0200 | [diff] [blame] | 346 | static HANDLE hinstLua = NULL; |
Bram Moolenaar | 0ba0429 | 2010-07-14 23:23:17 +0200 | [diff] [blame] | 347 | |
Bram Moolenaar | 55d5c03 | 2010-07-17 23:52:29 +0200 | [diff] [blame] | 348 | static void |
| 349 | end_dynamic_lua(void) |
| 350 | { |
| 351 | if (hinstLua) |
| 352 | { |
Bram Moolenaar | 2334b6d | 2010-07-22 21:32:16 +0200 | [diff] [blame] | 353 | close_dll(hinstLua); |
Bram Moolenaar | 55d5c03 | 2010-07-17 23:52:29 +0200 | [diff] [blame] | 354 | hinstLua = 0; |
Bram Moolenaar | 0ba0429 | 2010-07-14 23:23:17 +0200 | [diff] [blame] | 355 | } |
| 356 | } |
| 357 | |
Bram Moolenaar | 55d5c03 | 2010-07-17 23:52:29 +0200 | [diff] [blame] | 358 | static int |
| 359 | lua_link_init(char *libname, int verbose) |
| 360 | { |
Bram Moolenaar | 0ba0429 | 2010-07-14 23:23:17 +0200 | [diff] [blame] | 361 | const luaV_Reg *reg; |
| 362 | if (hinstLua) return OK; |
Bram Moolenaar | 2334b6d | 2010-07-22 21:32:16 +0200 | [diff] [blame] | 363 | hinstLua = load_dll(libname); |
Bram Moolenaar | 55d5c03 | 2010-07-17 23:52:29 +0200 | [diff] [blame] | 364 | if (!hinstLua) |
| 365 | { |
| 366 | if (verbose) |
| 367 | EMSG2(_(e_loadlib), libname); |
| 368 | return FAIL; |
Bram Moolenaar | 0ba0429 | 2010-07-14 23:23:17 +0200 | [diff] [blame] | 369 | } |
Bram Moolenaar | 55d5c03 | 2010-07-17 23:52:29 +0200 | [diff] [blame] | 370 | for (reg = luaV_dll; reg->func; reg++) |
| 371 | { |
Bram Moolenaar | 2334b6d | 2010-07-22 21:32:16 +0200 | [diff] [blame] | 372 | if ((*reg->func = symbol_from_dll(hinstLua, reg->name)) == NULL) |
| 373 | { |
| 374 | close_dll(hinstLua); |
Bram Moolenaar | 55d5c03 | 2010-07-17 23:52:29 +0200 | [diff] [blame] | 375 | hinstLua = 0; |
| 376 | if (verbose) |
| 377 | EMSG2(_(e_loadfunc), reg->name); |
| 378 | return FAIL; |
| 379 | } |
Bram Moolenaar | 0ba0429 | 2010-07-14 23:23:17 +0200 | [diff] [blame] | 380 | } |
| 381 | return OK; |
| 382 | } |
| 383 | |
Bram Moolenaar | 55d5c03 | 2010-07-17 23:52:29 +0200 | [diff] [blame] | 384 | int |
| 385 | lua_enabled(int verbose) |
| 386 | { |
Bram Moolenaar | 0ba0429 | 2010-07-14 23:23:17 +0200 | [diff] [blame] | 387 | return lua_link_init(DYNAMIC_LUA_DLL, verbose) == OK; |
| 388 | } |
| 389 | |
| 390 | #endif /* DYNAMIC_LUA */ |
| 391 | |
Bram Moolenaar | 1dced57 | 2012-04-05 16:54:08 +0200 | [diff] [blame] | 392 | #if LUA_VERSION_NUM > 501 |
| 393 | static int |
| 394 | luaL_typeerror (lua_State *L, int narg, const char *tname) |
| 395 | { |
| 396 | const char *msg = lua_pushfstring(L, "%s expected, got %s", |
Bram Moolenaar | db91395 | 2012-06-29 12:54:53 +0200 | [diff] [blame] | 397 | tname, luaL_typename(L, narg)); |
Bram Moolenaar | 1dced57 | 2012-04-05 16:54:08 +0200 | [diff] [blame] | 398 | return luaL_argerror(L, narg, msg); |
| 399 | } |
| 400 | #endif |
| 401 | |
Bram Moolenaar | 0ba0429 | 2010-07-14 23:23:17 +0200 | [diff] [blame] | 402 | |
| 403 | /* ======= Internal ======= */ |
| 404 | |
Bram Moolenaar | 55d5c03 | 2010-07-17 23:52:29 +0200 | [diff] [blame] | 405 | static void |
| 406 | luaV_newmetatable(lua_State *L, const char *tname) |
| 407 | { |
Bram Moolenaar | 0ba0429 | 2010-07-14 23:23:17 +0200 | [diff] [blame] | 408 | lua_newtable(L); |
| 409 | lua_pushlightuserdata(L, (void *) tname); |
| 410 | lua_pushvalue(L, -2); |
| 411 | lua_rawset(L, LUA_REGISTRYINDEX); |
| 412 | } |
| 413 | |
Bram Moolenaar | 55d5c03 | 2010-07-17 23:52:29 +0200 | [diff] [blame] | 414 | static void * |
| 415 | luaV_toudata(lua_State *L, int ud, const char *tname) |
| 416 | { |
Bram Moolenaar | 0ba0429 | 2010-07-14 23:23:17 +0200 | [diff] [blame] | 417 | void *p = lua_touserdata(L, ud); |
Bram Moolenaar | 55d5c03 | 2010-07-17 23:52:29 +0200 | [diff] [blame] | 418 | |
| 419 | if (p != NULL) /* value is userdata? */ |
| 420 | { |
| 421 | if (lua_getmetatable(L, ud)) /* does it have a metatable? */ |
| 422 | { |
| 423 | luaV_getfield(L, tname); /* get metatable */ |
| 424 | if (lua_rawequal(L, -1, -2)) /* MTs match? */ |
| 425 | { |
| 426 | lua_pop(L, 2); /* MTs */ |
| 427 | return p; |
| 428 | } |
| 429 | } |
Bram Moolenaar | 0ba0429 | 2010-07-14 23:23:17 +0200 | [diff] [blame] | 430 | } |
| 431 | return NULL; |
| 432 | } |
| 433 | |
Bram Moolenaar | 55d5c03 | 2010-07-17 23:52:29 +0200 | [diff] [blame] | 434 | static void * |
Bram Moolenaar | 1dced57 | 2012-04-05 16:54:08 +0200 | [diff] [blame] | 435 | luaV_checkcache(lua_State *L, void *p) |
| 436 | { |
| 437 | luaV_getudata(L, p); |
| 438 | if (lua_isnil(L, -1)) luaL_error(L, "invalid object"); |
| 439 | lua_pop(L, 1); |
| 440 | return p; |
| 441 | } |
| 442 | |
| 443 | #define luaV_unbox(L,luatyp,ud) (*((luatyp *) lua_touserdata((L),(ud)))) |
| 444 | |
| 445 | #define luaV_checkvalid(L,luatyp,ud) \ |
| 446 | luaV_checkcache((L), (void *) luaV_unbox((L),luatyp,(ud))) |
| 447 | |
| 448 | static void * |
Bram Moolenaar | 55d5c03 | 2010-07-17 23:52:29 +0200 | [diff] [blame] | 449 | luaV_checkudata(lua_State *L, int ud, const char *tname) |
| 450 | { |
Bram Moolenaar | 0ba0429 | 2010-07-14 23:23:17 +0200 | [diff] [blame] | 451 | void *p = luaV_toudata(L, ud, tname); |
Bram Moolenaar | 1dced57 | 2012-04-05 16:54:08 +0200 | [diff] [blame] | 452 | if (p == NULL) luaL_typeerror(L, ud, tname); |
Bram Moolenaar | 0ba0429 | 2010-07-14 23:23:17 +0200 | [diff] [blame] | 453 | return p; |
| 454 | } |
| 455 | |
Bram Moolenaar | 55d5c03 | 2010-07-17 23:52:29 +0200 | [diff] [blame] | 456 | static void |
| 457 | luaV_pushtypval(lua_State *L, typval_T *tv) |
| 458 | { |
Bram Moolenaar | 1dced57 | 2012-04-05 16:54:08 +0200 | [diff] [blame] | 459 | if (tv == NULL) |
| 460 | { |
| 461 | lua_pushnil(L); |
| 462 | return; |
| 463 | } |
Bram Moolenaar | 55d5c03 | 2010-07-17 23:52:29 +0200 | [diff] [blame] | 464 | switch (tv->v_type) |
| 465 | { |
| 466 | case VAR_STRING: |
Bram Moolenaar | d04da7c | 2012-10-14 03:41:59 +0200 | [diff] [blame] | 467 | lua_pushstring(L, tv->vval.v_string == NULL |
| 468 | ? "" : (char *)tv->vval.v_string); |
Bram Moolenaar | 55d5c03 | 2010-07-17 23:52:29 +0200 | [diff] [blame] | 469 | break; |
| 470 | case VAR_NUMBER: |
| 471 | lua_pushinteger(L, (int) tv->vval.v_number); |
| 472 | break; |
Bram Moolenaar | 0ba0429 | 2010-07-14 23:23:17 +0200 | [diff] [blame] | 473 | #ifdef FEAT_FLOAT |
Bram Moolenaar | 55d5c03 | 2010-07-17 23:52:29 +0200 | [diff] [blame] | 474 | case VAR_FLOAT: |
| 475 | lua_pushnumber(L, (lua_Number) tv->vval.v_float); |
| 476 | break; |
Bram Moolenaar | 0ba0429 | 2010-07-14 23:23:17 +0200 | [diff] [blame] | 477 | #endif |
Bram Moolenaar | 1dced57 | 2012-04-05 16:54:08 +0200 | [diff] [blame] | 478 | case VAR_LIST: |
| 479 | luaV_pushlist(L, tv->vval.v_list); |
Bram Moolenaar | 55d5c03 | 2010-07-17 23:52:29 +0200 | [diff] [blame] | 480 | break; |
Bram Moolenaar | 1dced57 | 2012-04-05 16:54:08 +0200 | [diff] [blame] | 481 | case VAR_DICT: |
| 482 | luaV_pushdict(L, tv->vval.v_dict); |
| 483 | break; |
| 484 | default: |
| 485 | lua_pushnil(L); |
| 486 | } |
| 487 | } |
Bram Moolenaar | 55d5c03 | 2010-07-17 23:52:29 +0200 | [diff] [blame] | 488 | |
Bram Moolenaar | 1dced57 | 2012-04-05 16:54:08 +0200 | [diff] [blame] | 489 | /* converts lua value at 'pos' to typval 'tv' */ |
| 490 | static void |
| 491 | luaV_totypval (lua_State *L, int pos, typval_T *tv) |
| 492 | { |
| 493 | switch(lua_type(L, pos)) { |
| 494 | case LUA_TBOOLEAN: |
| 495 | tv->v_type = VAR_NUMBER; |
| 496 | tv->vval.v_number = (varnumber_T) lua_toboolean(L, pos); |
| 497 | break; |
| 498 | case LUA_TSTRING: |
| 499 | tv->v_type = VAR_STRING; |
| 500 | tv->vval.v_string = vim_strsave((char_u *) lua_tostring(L, pos)); |
| 501 | break; |
| 502 | case LUA_TNUMBER: |
| 503 | #ifdef FEAT_FLOAT |
| 504 | tv->v_type = VAR_FLOAT; |
| 505 | tv->vval.v_float = (float_T) lua_tonumber(L, pos); |
| 506 | #else |
| 507 | tv->v_type = VAR_NUMBER; |
| 508 | tv->vval.v_number = (varnumber_T) lua_tointeger(L, pos); |
| 509 | #endif |
| 510 | break; |
| 511 | case LUA_TUSERDATA: { |
| 512 | void *p = lua_touserdata(L, pos); |
| 513 | if (lua_getmetatable(L, pos)) /* has metatable? */ |
Bram Moolenaar | 55d5c03 | 2010-07-17 23:52:29 +0200 | [diff] [blame] | 514 | { |
Bram Moolenaar | 1dced57 | 2012-04-05 16:54:08 +0200 | [diff] [blame] | 515 | /* check list */ |
| 516 | luaV_getfield(L, LUAVIM_LIST); |
| 517 | if (lua_rawequal(L, -1, -2)) |
Bram Moolenaar | 2334b6d | 2010-07-22 21:32:16 +0200 | [diff] [blame] | 518 | { |
Bram Moolenaar | 1dced57 | 2012-04-05 16:54:08 +0200 | [diff] [blame] | 519 | tv->v_type = VAR_LIST; |
| 520 | tv->vval.v_list = *((luaV_List *) p); |
| 521 | ++tv->vval.v_list->lv_refcount; |
| 522 | lua_pop(L, 2); /* MTs */ |
| 523 | return; |
Bram Moolenaar | 55d5c03 | 2010-07-17 23:52:29 +0200 | [diff] [blame] | 524 | } |
Bram Moolenaar | 1dced57 | 2012-04-05 16:54:08 +0200 | [diff] [blame] | 525 | /* check dict */ |
| 526 | luaV_getfield(L, LUAVIM_DICT); |
| 527 | if (lua_rawequal(L, -1, -3)) |
| 528 | { |
| 529 | tv->v_type = VAR_DICT; |
| 530 | tv->vval.v_dict = *((luaV_Dict *) p); |
| 531 | ++tv->vval.v_dict->dv_refcount; |
| 532 | lua_pop(L, 3); /* MTs */ |
| 533 | return; |
| 534 | } |
| 535 | lua_pop(L, 3); /* MTs */ |
Bram Moolenaar | 55d5c03 | 2010-07-17 23:52:29 +0200 | [diff] [blame] | 536 | } |
Bram Moolenaar | 55d5c03 | 2010-07-17 23:52:29 +0200 | [diff] [blame] | 537 | break; |
| 538 | } |
| 539 | default: |
Bram Moolenaar | 1dced57 | 2012-04-05 16:54:08 +0200 | [diff] [blame] | 540 | tv->v_type = VAR_NUMBER; |
| 541 | tv->vval.v_number = 0; |
Bram Moolenaar | 0ba0429 | 2010-07-14 23:23:17 +0200 | [diff] [blame] | 542 | } |
| 543 | } |
| 544 | |
| 545 | /* similar to luaL_addlstring, but replaces \0 with \n if toline and |
| 546 | * \n with \0 otherwise */ |
Bram Moolenaar | 55d5c03 | 2010-07-17 23:52:29 +0200 | [diff] [blame] | 547 | static void |
| 548 | luaV_addlstring(luaL_Buffer *b, const char *s, size_t l, int toline) |
| 549 | { |
| 550 | while (l--) |
| 551 | { |
| 552 | if (*s == '\0' && toline) |
| 553 | luaL_addchar(b, '\n'); |
| 554 | else if (*s == '\n' && !toline) |
| 555 | luaL_addchar(b, '\0'); |
| 556 | else |
| 557 | luaL_addchar(b, *s); |
| 558 | s++; |
Bram Moolenaar | 0ba0429 | 2010-07-14 23:23:17 +0200 | [diff] [blame] | 559 | } |
| 560 | } |
| 561 | |
Bram Moolenaar | 55d5c03 | 2010-07-17 23:52:29 +0200 | [diff] [blame] | 562 | static void |
| 563 | luaV_pushline(lua_State *L, buf_T *buf, linenr_T n) |
| 564 | { |
Bram Moolenaar | 0ba0429 | 2010-07-14 23:23:17 +0200 | [diff] [blame] | 565 | const char *s = (const char *) ml_get_buf(buf, n, FALSE); |
| 566 | luaL_Buffer b; |
| 567 | luaL_buffinit(L, &b); |
| 568 | luaV_addlstring(&b, s, strlen(s), 0); |
| 569 | luaL_pushresult(&b); |
| 570 | } |
| 571 | |
Bram Moolenaar | 55d5c03 | 2010-07-17 23:52:29 +0200 | [diff] [blame] | 572 | static char_u * |
| 573 | luaV_toline(lua_State *L, int pos) |
| 574 | { |
Bram Moolenaar | 0ba0429 | 2010-07-14 23:23:17 +0200 | [diff] [blame] | 575 | size_t l; |
| 576 | const char *s = lua_tolstring(L, pos, &l); |
Bram Moolenaar | 55d5c03 | 2010-07-17 23:52:29 +0200 | [diff] [blame] | 577 | |
Bram Moolenaar | 0ba0429 | 2010-07-14 23:23:17 +0200 | [diff] [blame] | 578 | luaL_Buffer b; |
| 579 | luaL_buffinit(L, &b); |
| 580 | luaV_addlstring(&b, s, l, 1); |
| 581 | luaL_pushresult(&b); |
| 582 | return (char_u *) lua_tostring(L, -1); |
| 583 | } |
| 584 | |
| 585 | /* pops a string s from the top of the stack and calls mf(t) for pieces t of |
| 586 | * s separated by newlines */ |
Bram Moolenaar | 55d5c03 | 2010-07-17 23:52:29 +0200 | [diff] [blame] | 587 | static void |
| 588 | luaV_msgfunc(lua_State *L, msgfunc_T mf) |
| 589 | { |
Bram Moolenaar | 0ba0429 | 2010-07-14 23:23:17 +0200 | [diff] [blame] | 590 | luaL_Buffer b; |
| 591 | size_t l; |
| 592 | const char *p, *s = lua_tolstring(L, -1, &l); |
| 593 | luaL_buffinit(L, &b); |
| 594 | luaV_addlstring(&b, s, l, 0); |
| 595 | luaL_pushresult(&b); |
| 596 | /* break string */ |
| 597 | p = s = lua_tolstring(L, -1, &l); |
Bram Moolenaar | 55d5c03 | 2010-07-17 23:52:29 +0200 | [diff] [blame] | 598 | while (l--) |
| 599 | { |
| 600 | if (*p++ == '\0') /* break? */ |
| 601 | { |
| 602 | mf((char_u *) s); |
| 603 | s = p; |
| 604 | } |
Bram Moolenaar | 0ba0429 | 2010-07-14 23:23:17 +0200 | [diff] [blame] | 605 | } |
| 606 | mf((char_u *) s); |
| 607 | lua_pop(L, 2); /* original and modified strings */ |
| 608 | } |
| 609 | |
Bram Moolenaar | 1dced57 | 2012-04-05 16:54:08 +0200 | [diff] [blame] | 610 | #define luaV_newtype(typ,tname,luatyp,luatname) \ |
| 611 | static luatyp * \ |
| 612 | luaV_new##tname (lua_State *L, typ *obj) \ |
| 613 | { \ |
| 614 | luatyp *o = (luatyp *) lua_newuserdata(L, sizeof(luatyp)); \ |
| 615 | *o = obj; \ |
| 616 | luaV_setudata(L, obj); /* cache[obj] = udata */ \ |
| 617 | luaV_getfield(L, luatname); \ |
| 618 | lua_setmetatable(L, -2); \ |
| 619 | return o; \ |
Bram Moolenaar | 0ba0429 | 2010-07-14 23:23:17 +0200 | [diff] [blame] | 620 | } |
Bram Moolenaar | 1dced57 | 2012-04-05 16:54:08 +0200 | [diff] [blame] | 621 | |
| 622 | #define luaV_pushtype(typ,tname,luatyp) \ |
| 623 | static luatyp * \ |
| 624 | luaV_push##tname (lua_State *L, typ *obj) \ |
| 625 | { \ |
| 626 | luatyp *o = NULL; \ |
| 627 | if (obj == NULL) \ |
| 628 | lua_pushnil(L); \ |
| 629 | else { \ |
| 630 | luaV_getudata(L, obj); \ |
| 631 | if (lua_isnil(L, -1)) /* not interned? */ \ |
| 632 | { \ |
| 633 | lua_pop(L, 1); \ |
| 634 | o = luaV_new##tname(L, obj); \ |
| 635 | } \ |
| 636 | else \ |
| 637 | o = (luatyp *) lua_touserdata(L, -1); \ |
| 638 | } \ |
| 639 | return o; \ |
| 640 | } |
| 641 | |
| 642 | #define luaV_type_tostring(tname,luatname) \ |
| 643 | static int \ |
| 644 | luaV_##tname##_tostring (lua_State *L) \ |
| 645 | { \ |
| 646 | lua_pushfstring(L, "%s: %p", luatname, lua_touserdata(L, 1)); \ |
| 647 | return 1; \ |
| 648 | } |
| 649 | |
Bram Moolenaar | 1dced57 | 2012-04-05 16:54:08 +0200 | [diff] [blame] | 650 | /* ======= List type ======= */ |
| 651 | |
| 652 | static luaV_List * |
| 653 | luaV_newlist (lua_State *L, list_T *lis) |
| 654 | { |
| 655 | luaV_List *l = (luaV_List *) lua_newuserdata(L, sizeof(luaV_List)); |
| 656 | *l = lis; |
| 657 | lis->lv_refcount++; /* reference in Lua */ |
| 658 | luaV_setudata(L, lis); /* cache[lis] = udata */ |
| 659 | luaV_getfield(L, LUAVIM_LIST); |
| 660 | lua_setmetatable(L, -2); |
| 661 | return l; |
| 662 | } |
| 663 | |
| 664 | luaV_pushtype(list_T, list, luaV_List) |
| 665 | luaV_type_tostring(list, LUAVIM_LIST) |
| 666 | |
| 667 | static int |
Bram Moolenaar | 1dced57 | 2012-04-05 16:54:08 +0200 | [diff] [blame] | 668 | luaV_list_len (lua_State *L) |
| 669 | { |
| 670 | list_T *l = luaV_unbox(L, luaV_List, 1); |
| 671 | lua_pushinteger(L, (l == NULL) ? 0 : (int) l->lv_len); |
Bram Moolenaar | 0ba0429 | 2010-07-14 23:23:17 +0200 | [diff] [blame] | 672 | return 1; |
| 673 | } |
| 674 | |
Bram Moolenaar | 55d5c03 | 2010-07-17 23:52:29 +0200 | [diff] [blame] | 675 | static int |
Bram Moolenaar | 1dced57 | 2012-04-05 16:54:08 +0200 | [diff] [blame] | 676 | luaV_list_iter (lua_State *L) |
| 677 | { |
| 678 | listitem_T *li = (listitem_T *) lua_touserdata(L, lua_upvalueindex(2)); |
| 679 | if (li == NULL) return 0; |
| 680 | luaV_pushtypval(L, &li->li_tv); |
| 681 | lua_pushlightuserdata(L, (void *) li->li_next); |
| 682 | lua_replace(L, lua_upvalueindex(2)); |
| 683 | return 1; |
| 684 | } |
| 685 | |
| 686 | static int |
| 687 | luaV_list_call (lua_State *L) |
| 688 | { |
| 689 | list_T *l = luaV_unbox(L, luaV_List, 1); |
| 690 | lua_pushvalue(L, lua_upvalueindex(1)); /* pass cache table along */ |
| 691 | lua_pushlightuserdata(L, (void *) l->lv_first); |
| 692 | lua_pushcclosure(L, luaV_list_iter, 2); |
| 693 | return 1; |
| 694 | } |
| 695 | |
| 696 | static int |
| 697 | luaV_list_index (lua_State *L) |
| 698 | { |
| 699 | list_T *l = luaV_unbox(L, luaV_List, 1); |
| 700 | if (lua_isnumber(L, 2)) /* list item? */ |
| 701 | { |
| 702 | listitem_T *li = list_find(l, (long) luaL_checkinteger(L, 2)); |
| 703 | if (li == NULL) |
| 704 | lua_pushnil(L); |
| 705 | else |
| 706 | luaV_pushtypval(L, &li->li_tv); |
| 707 | } |
| 708 | else if (lua_isstring(L, 2)) /* method? */ |
| 709 | { |
| 710 | const char *s = lua_tostring(L, 2); |
| 711 | if (strncmp(s, "add", 3) == 0 |
Bram Moolenaar | b376647 | 2013-04-15 13:49:21 +0200 | [diff] [blame] | 712 | || strncmp(s, "insert", 6) == 0) |
Bram Moolenaar | 1dced57 | 2012-04-05 16:54:08 +0200 | [diff] [blame] | 713 | { |
| 714 | lua_getmetatable(L, 1); |
| 715 | lua_getfield(L, -1, s); |
| 716 | } |
| 717 | else |
| 718 | lua_pushnil(L); |
| 719 | } |
| 720 | else |
| 721 | lua_pushnil(L); |
| 722 | return 1; |
| 723 | } |
| 724 | |
| 725 | static int |
| 726 | luaV_list_newindex (lua_State *L) |
| 727 | { |
| 728 | list_T *l = luaV_unbox(L, luaV_List, 1); |
| 729 | long n = (long) luaL_checkinteger(L, 2); |
| 730 | listitem_T *li; |
| 731 | if (l->lv_lock) |
| 732 | luaL_error(L, "list is locked"); |
| 733 | li = list_find(l, n); |
| 734 | if (li == NULL) return 0; |
| 735 | if (lua_isnil(L, 3)) /* remove? */ |
| 736 | { |
Bram Moolenaar | 3ec7f4e | 2014-05-07 17:31:37 +0200 | [diff] [blame] | 737 | vimlist_remove(l, li, li); |
Bram Moolenaar | 1dced57 | 2012-04-05 16:54:08 +0200 | [diff] [blame] | 738 | clear_tv(&li->li_tv); |
| 739 | vim_free(li); |
| 740 | } |
| 741 | else |
| 742 | { |
| 743 | typval_T v; |
| 744 | luaV_totypval(L, 3, &v); |
| 745 | clear_tv(&li->li_tv); |
| 746 | copy_tv(&v, &li->li_tv); |
Bram Moolenaar | b376647 | 2013-04-15 13:49:21 +0200 | [diff] [blame] | 747 | clear_tv(&v); |
Bram Moolenaar | 1dced57 | 2012-04-05 16:54:08 +0200 | [diff] [blame] | 748 | } |
| 749 | return 0; |
| 750 | } |
| 751 | |
| 752 | static int |
| 753 | luaV_list_add (lua_State *L) |
| 754 | { |
| 755 | luaV_List *lis = luaV_checkudata(L, 1, LUAVIM_LIST); |
| 756 | list_T *l = (list_T *) luaV_checkcache(L, (void *) *lis); |
Bram Moolenaar | b376647 | 2013-04-15 13:49:21 +0200 | [diff] [blame] | 757 | typval_T v; |
Bram Moolenaar | 1dced57 | 2012-04-05 16:54:08 +0200 | [diff] [blame] | 758 | if (l->lv_lock) |
| 759 | luaL_error(L, "list is locked"); |
Bram Moolenaar | b376647 | 2013-04-15 13:49:21 +0200 | [diff] [blame] | 760 | lua_settop(L, 2); |
| 761 | luaV_totypval(L, 2, &v); |
| 762 | if (list_append_tv(l, &v) == FAIL) |
Bram Moolenaar | 1dced57 | 2012-04-05 16:54:08 +0200 | [diff] [blame] | 763 | { |
Bram Moolenaar | b376647 | 2013-04-15 13:49:21 +0200 | [diff] [blame] | 764 | clear_tv(&v); |
| 765 | luaL_error(L, "Failed to add item to list"); |
Bram Moolenaar | 1dced57 | 2012-04-05 16:54:08 +0200 | [diff] [blame] | 766 | } |
Bram Moolenaar | b376647 | 2013-04-15 13:49:21 +0200 | [diff] [blame] | 767 | clear_tv(&v); |
Bram Moolenaar | 1dced57 | 2012-04-05 16:54:08 +0200 | [diff] [blame] | 768 | lua_settop(L, 1); |
| 769 | return 1; |
| 770 | } |
| 771 | |
| 772 | static int |
| 773 | luaV_list_insert (lua_State *L) |
| 774 | { |
| 775 | luaV_List *lis = luaV_checkudata(L, 1, LUAVIM_LIST); |
| 776 | list_T *l = (list_T *) luaV_checkcache(L, (void *) *lis); |
| 777 | long pos = luaL_optlong(L, 3, 0); |
| 778 | listitem_T *li = NULL; |
| 779 | typval_T v; |
| 780 | if (l->lv_lock) |
| 781 | luaL_error(L, "list is locked"); |
| 782 | if (pos < l->lv_len) |
| 783 | { |
| 784 | li = list_find(l, pos); |
| 785 | if (li == NULL) |
| 786 | luaL_error(L, "invalid position"); |
| 787 | } |
| 788 | lua_settop(L, 2); |
| 789 | luaV_totypval(L, 2, &v); |
Bram Moolenaar | b376647 | 2013-04-15 13:49:21 +0200 | [diff] [blame] | 790 | if (list_insert_tv(l, &v, li) == FAIL) |
| 791 | { |
| 792 | clear_tv(&v); |
| 793 | luaL_error(L, "Failed to add item to list"); |
| 794 | } |
| 795 | clear_tv(&v); |
Bram Moolenaar | 1dced57 | 2012-04-05 16:54:08 +0200 | [diff] [blame] | 796 | lua_settop(L, 1); |
| 797 | return 1; |
| 798 | } |
| 799 | |
| 800 | static const luaL_Reg luaV_List_mt[] = { |
| 801 | {"__tostring", luaV_list_tostring}, |
Bram Moolenaar | 1dced57 | 2012-04-05 16:54:08 +0200 | [diff] [blame] | 802 | {"__len", luaV_list_len}, |
| 803 | {"__call", luaV_list_call}, |
| 804 | {"__index", luaV_list_index}, |
| 805 | {"__newindex", luaV_list_newindex}, |
| 806 | {"add", luaV_list_add}, |
| 807 | {"insert", luaV_list_insert}, |
| 808 | {NULL, NULL} |
| 809 | }; |
| 810 | |
| 811 | |
| 812 | /* ======= Dict type ======= */ |
| 813 | |
| 814 | static luaV_Dict * |
| 815 | luaV_newdict (lua_State *L, dict_T *dic) |
| 816 | { |
| 817 | luaV_Dict *d = (luaV_Dict *) lua_newuserdata(L, sizeof(luaV_Dict)); |
| 818 | *d = dic; |
| 819 | dic->dv_refcount++; /* reference in Lua */ |
| 820 | luaV_setudata(L, dic); /* cache[dic] = udata */ |
| 821 | luaV_getfield(L, LUAVIM_DICT); |
| 822 | lua_setmetatable(L, -2); |
| 823 | return d; |
| 824 | } |
| 825 | |
| 826 | luaV_pushtype(dict_T, dict, luaV_Dict) |
| 827 | luaV_type_tostring(dict, LUAVIM_DICT) |
| 828 | |
| 829 | static int |
Bram Moolenaar | 1dced57 | 2012-04-05 16:54:08 +0200 | [diff] [blame] | 830 | luaV_dict_len (lua_State *L) |
| 831 | { |
| 832 | dict_T *d = luaV_unbox(L, luaV_Dict, 1); |
| 833 | lua_pushinteger(L, (d == NULL) ? 0 : (int) d->dv_hashtab.ht_used); |
| 834 | return 1; |
| 835 | } |
| 836 | |
| 837 | static int |
Bram Moolenaar | feeaa68 | 2013-02-14 22:19:51 +0100 | [diff] [blame] | 838 | luaV_dict_iter (lua_State *L UNUSED) |
Bram Moolenaar | 1dced57 | 2012-04-05 16:54:08 +0200 | [diff] [blame] | 839 | { |
Bram Moolenaar | feeaa68 | 2013-02-14 22:19:51 +0100 | [diff] [blame] | 840 | #ifdef FEAT_EVAL |
Bram Moolenaar | 1dced57 | 2012-04-05 16:54:08 +0200 | [diff] [blame] | 841 | hashitem_T *hi = (hashitem_T *) lua_touserdata(L, lua_upvalueindex(2)); |
| 842 | int n = lua_tointeger(L, lua_upvalueindex(3)); |
| 843 | dictitem_T *di; |
| 844 | if (n <= 0) return 0; |
| 845 | while (HASHITEM_EMPTY(hi)) hi++; |
| 846 | di = dict_lookup(hi); |
| 847 | lua_pushstring(L, (char *) hi->hi_key); |
| 848 | luaV_pushtypval(L, &di->di_tv); |
| 849 | lua_pushlightuserdata(L, (void *) (hi + 1)); |
| 850 | lua_replace(L, lua_upvalueindex(2)); |
| 851 | lua_pushinteger(L, n - 1); |
| 852 | lua_replace(L, lua_upvalueindex(3)); |
| 853 | return 2; |
Bram Moolenaar | feeaa68 | 2013-02-14 22:19:51 +0100 | [diff] [blame] | 854 | #else |
| 855 | return 0; |
| 856 | #endif |
Bram Moolenaar | 1dced57 | 2012-04-05 16:54:08 +0200 | [diff] [blame] | 857 | } |
| 858 | |
| 859 | static int |
| 860 | luaV_dict_call (lua_State *L) |
| 861 | { |
| 862 | dict_T *d = luaV_unbox(L, luaV_Dict, 1); |
| 863 | hashtab_T *ht = &d->dv_hashtab; |
| 864 | lua_pushvalue(L, lua_upvalueindex(1)); /* pass cache table along */ |
| 865 | lua_pushlightuserdata(L, (void *) ht->ht_array); |
| 866 | lua_pushinteger(L, ht->ht_used); /* # remaining items */ |
| 867 | lua_pushcclosure(L, luaV_dict_iter, 3); |
| 868 | return 1; |
| 869 | } |
| 870 | |
| 871 | static int |
| 872 | luaV_dict_index (lua_State *L) |
| 873 | { |
| 874 | dict_T *d = luaV_unbox(L, luaV_Dict, 1); |
| 875 | char_u *key = (char_u *) luaL_checkstring(L, 2); |
| 876 | dictitem_T *di = dict_find(d, key, -1); |
| 877 | if (di == NULL) |
| 878 | lua_pushnil(L); |
| 879 | else |
| 880 | luaV_pushtypval(L, &di->di_tv); |
| 881 | return 1; |
| 882 | } |
| 883 | |
| 884 | static int |
| 885 | luaV_dict_newindex (lua_State *L) |
| 886 | { |
| 887 | dict_T *d = luaV_unbox(L, luaV_Dict, 1); |
| 888 | char_u *key = (char_u *) luaL_checkstring(L, 2); |
| 889 | dictitem_T *di; |
| 890 | if (d->dv_lock) |
| 891 | luaL_error(L, "dict is locked"); |
| 892 | di = dict_find(d, key, -1); |
| 893 | if (di == NULL) /* non-existing key? */ |
| 894 | { |
| 895 | if (lua_isnil(L, 3)) return 0; |
| 896 | di = dictitem_alloc(key); |
| 897 | if (di == NULL) return 0; |
| 898 | if (dict_add(d, di) == FAIL) |
| 899 | { |
| 900 | vim_free(di); |
| 901 | return 0; |
| 902 | } |
| 903 | } |
| 904 | else |
| 905 | clear_tv(&di->di_tv); |
| 906 | if (lua_isnil(L, 3)) /* remove? */ |
| 907 | { |
| 908 | hashitem_T *hi = hash_find(&d->dv_hashtab, di->di_key); |
| 909 | hash_remove(&d->dv_hashtab, hi); |
| 910 | dictitem_free(di); |
| 911 | } |
| 912 | else { |
| 913 | typval_T v; |
| 914 | luaV_totypval(L, 3, &v); |
| 915 | copy_tv(&v, &di->di_tv); |
Bram Moolenaar | b376647 | 2013-04-15 13:49:21 +0200 | [diff] [blame] | 916 | clear_tv(&v); |
Bram Moolenaar | 1dced57 | 2012-04-05 16:54:08 +0200 | [diff] [blame] | 917 | } |
| 918 | return 0; |
| 919 | } |
| 920 | |
| 921 | static const luaL_Reg luaV_Dict_mt[] = { |
| 922 | {"__tostring", luaV_dict_tostring}, |
Bram Moolenaar | 1dced57 | 2012-04-05 16:54:08 +0200 | [diff] [blame] | 923 | {"__len", luaV_dict_len}, |
| 924 | {"__call", luaV_dict_call}, |
| 925 | {"__index", luaV_dict_index}, |
| 926 | {"__newindex", luaV_dict_newindex}, |
| 927 | {NULL, NULL} |
| 928 | }; |
| 929 | |
| 930 | |
| 931 | /* ======= Buffer type ======= */ |
| 932 | |
| 933 | luaV_newtype(buf_T, buffer, luaV_Buffer, LUAVIM_BUFFER) |
| 934 | luaV_pushtype(buf_T, buffer, luaV_Buffer) |
| 935 | luaV_type_tostring(buffer, LUAVIM_BUFFER) |
| 936 | |
| 937 | static int |
Bram Moolenaar | 55d5c03 | 2010-07-17 23:52:29 +0200 | [diff] [blame] | 938 | luaV_buffer_len(lua_State *L) |
| 939 | { |
Bram Moolenaar | 1dced57 | 2012-04-05 16:54:08 +0200 | [diff] [blame] | 940 | buf_T *b = (buf_T *) luaV_checkvalid(L, luaV_Buffer, 1); |
| 941 | lua_pushinteger(L, b->b_ml.ml_line_count); |
Bram Moolenaar | 0ba0429 | 2010-07-14 23:23:17 +0200 | [diff] [blame] | 942 | return 1; |
| 943 | } |
| 944 | |
Bram Moolenaar | 55d5c03 | 2010-07-17 23:52:29 +0200 | [diff] [blame] | 945 | static int |
| 946 | luaV_buffer_call(lua_State *L) |
| 947 | { |
Bram Moolenaar | 1dced57 | 2012-04-05 16:54:08 +0200 | [diff] [blame] | 948 | buf_T *b = (buf_T *) luaV_checkvalid(L, luaV_Buffer, 1); |
Bram Moolenaar | 0ba0429 | 2010-07-14 23:23:17 +0200 | [diff] [blame] | 949 | lua_settop(L, 1); |
Bram Moolenaar | 1dced57 | 2012-04-05 16:54:08 +0200 | [diff] [blame] | 950 | set_curbuf(b, DOBUF_SPLIT); |
Bram Moolenaar | 0ba0429 | 2010-07-14 23:23:17 +0200 | [diff] [blame] | 951 | return 1; |
| 952 | } |
| 953 | |
Bram Moolenaar | 55d5c03 | 2010-07-17 23:52:29 +0200 | [diff] [blame] | 954 | static int |
| 955 | luaV_buffer_index(lua_State *L) |
| 956 | { |
Bram Moolenaar | 1dced57 | 2012-04-05 16:54:08 +0200 | [diff] [blame] | 957 | buf_T *b = (buf_T *) luaV_checkvalid(L, luaV_Buffer, 1); |
Bram Moolenaar | 0ba0429 | 2010-07-14 23:23:17 +0200 | [diff] [blame] | 958 | linenr_T n = (linenr_T) lua_tointeger(L, 2); |
Bram Moolenaar | 1dced57 | 2012-04-05 16:54:08 +0200 | [diff] [blame] | 959 | if (n > 0 && n <= b->b_ml.ml_line_count) |
| 960 | luaV_pushline(L, b, n); |
Bram Moolenaar | 55d5c03 | 2010-07-17 23:52:29 +0200 | [diff] [blame] | 961 | else if (lua_isstring(L, 2)) |
| 962 | { |
| 963 | const char *s = lua_tostring(L, 2); |
| 964 | if (strncmp(s, "name", 4) == 0) |
Bram Moolenaar | 1dced57 | 2012-04-05 16:54:08 +0200 | [diff] [blame] | 965 | lua_pushstring(L, (char *) b->b_sfname); |
Bram Moolenaar | 55d5c03 | 2010-07-17 23:52:29 +0200 | [diff] [blame] | 966 | else if (strncmp(s, "fname", 5) == 0) |
Bram Moolenaar | 1dced57 | 2012-04-05 16:54:08 +0200 | [diff] [blame] | 967 | lua_pushstring(L, (char *) b->b_ffname); |
Bram Moolenaar | 55d5c03 | 2010-07-17 23:52:29 +0200 | [diff] [blame] | 968 | else if (strncmp(s, "number", 6) == 0) |
Bram Moolenaar | 1dced57 | 2012-04-05 16:54:08 +0200 | [diff] [blame] | 969 | lua_pushinteger(L, b->b_fnum); |
Bram Moolenaar | 55d5c03 | 2010-07-17 23:52:29 +0200 | [diff] [blame] | 970 | /* methods */ |
| 971 | else if (strncmp(s, "insert", 6) == 0 |
| 972 | || strncmp(s, "next", 4) == 0 |
| 973 | || strncmp(s, "previous", 8) == 0 |
| 974 | || strncmp(s, "isvalid", 7) == 0) |
| 975 | { |
| 976 | lua_getmetatable(L, 1); |
| 977 | lua_getfield(L, -1, s); |
| 978 | } |
| 979 | else |
| 980 | lua_pushnil(L); |
Bram Moolenaar | 0ba0429 | 2010-07-14 23:23:17 +0200 | [diff] [blame] | 981 | } |
Bram Moolenaar | 55d5c03 | 2010-07-17 23:52:29 +0200 | [diff] [blame] | 982 | else |
| 983 | lua_pushnil(L); |
Bram Moolenaar | 0ba0429 | 2010-07-14 23:23:17 +0200 | [diff] [blame] | 984 | return 1; |
| 985 | } |
| 986 | |
Bram Moolenaar | 55d5c03 | 2010-07-17 23:52:29 +0200 | [diff] [blame] | 987 | static int |
| 988 | luaV_buffer_newindex(lua_State *L) |
| 989 | { |
Bram Moolenaar | 1dced57 | 2012-04-05 16:54:08 +0200 | [diff] [blame] | 990 | buf_T *b = (buf_T *) luaV_checkvalid(L, luaV_Buffer, 1); |
Bram Moolenaar | 0ba0429 | 2010-07-14 23:23:17 +0200 | [diff] [blame] | 991 | linenr_T n = (linenr_T) luaL_checkinteger(L, 2); |
| 992 | #ifdef HAVE_SANDBOX |
| 993 | luaV_checksandbox(L); |
| 994 | #endif |
Bram Moolenaar | 1dced57 | 2012-04-05 16:54:08 +0200 | [diff] [blame] | 995 | if (n < 1 || n > b->b_ml.ml_line_count) |
Bram Moolenaar | 55d5c03 | 2010-07-17 23:52:29 +0200 | [diff] [blame] | 996 | luaL_error(L, "invalid line number"); |
| 997 | if (lua_isnil(L, 3)) /* delete line */ |
| 998 | { |
| 999 | buf_T *buf = curbuf; |
Bram Moolenaar | 1dced57 | 2012-04-05 16:54:08 +0200 | [diff] [blame] | 1000 | curbuf = b; |
Bram Moolenaar | 55d5c03 | 2010-07-17 23:52:29 +0200 | [diff] [blame] | 1001 | if (u_savedel(n, 1L) == FAIL) |
| 1002 | { |
| 1003 | curbuf = buf; |
| 1004 | luaL_error(L, "cannot save undo information"); |
| 1005 | } |
| 1006 | else if (ml_delete(n, FALSE) == FAIL) |
| 1007 | { |
| 1008 | curbuf = buf; |
| 1009 | luaL_error(L, "cannot delete line"); |
| 1010 | } |
| 1011 | else { |
| 1012 | deleted_lines_mark(n, 1L); |
Bram Moolenaar | 1dced57 | 2012-04-05 16:54:08 +0200 | [diff] [blame] | 1013 | if (b == curwin->w_buffer) /* fix cursor in current window? */ |
Bram Moolenaar | 55d5c03 | 2010-07-17 23:52:29 +0200 | [diff] [blame] | 1014 | { |
| 1015 | if (curwin->w_cursor.lnum >= n) |
| 1016 | { |
| 1017 | if (curwin->w_cursor.lnum > n) |
| 1018 | { |
| 1019 | curwin->w_cursor.lnum -= 1; |
| 1020 | check_cursor_col(); |
| 1021 | } |
| 1022 | else check_cursor(); |
| 1023 | changed_cline_bef_curs(); |
| 1024 | } |
| 1025 | invalidate_botline(); |
| 1026 | } |
| 1027 | } |
| 1028 | curbuf = buf; |
Bram Moolenaar | 0ba0429 | 2010-07-14 23:23:17 +0200 | [diff] [blame] | 1029 | } |
Bram Moolenaar | 55d5c03 | 2010-07-17 23:52:29 +0200 | [diff] [blame] | 1030 | else if (lua_isstring(L, 3)) /* update line */ |
| 1031 | { |
| 1032 | buf_T *buf = curbuf; |
Bram Moolenaar | 1dced57 | 2012-04-05 16:54:08 +0200 | [diff] [blame] | 1033 | curbuf = b; |
Bram Moolenaar | 55d5c03 | 2010-07-17 23:52:29 +0200 | [diff] [blame] | 1034 | if (u_savesub(n) == FAIL) |
| 1035 | { |
| 1036 | curbuf = buf; |
| 1037 | luaL_error(L, "cannot save undo information"); |
| 1038 | } |
| 1039 | else if (ml_replace(n, luaV_toline(L, 3), TRUE) == FAIL) |
| 1040 | { |
| 1041 | curbuf = buf; |
| 1042 | luaL_error(L, "cannot replace line"); |
| 1043 | } |
| 1044 | else changed_bytes(n, 0); |
| 1045 | curbuf = buf; |
Bram Moolenaar | 1dced57 | 2012-04-05 16:54:08 +0200 | [diff] [blame] | 1046 | if (b == curwin->w_buffer) |
Bram Moolenaar | 55d5c03 | 2010-07-17 23:52:29 +0200 | [diff] [blame] | 1047 | check_cursor_col(); |
Bram Moolenaar | 0ba0429 | 2010-07-14 23:23:17 +0200 | [diff] [blame] | 1048 | } |
| 1049 | else |
Bram Moolenaar | 55d5c03 | 2010-07-17 23:52:29 +0200 | [diff] [blame] | 1050 | luaL_error(L, "wrong argument to change line"); |
Bram Moolenaar | 0ba0429 | 2010-07-14 23:23:17 +0200 | [diff] [blame] | 1051 | return 0; |
| 1052 | } |
| 1053 | |
Bram Moolenaar | 55d5c03 | 2010-07-17 23:52:29 +0200 | [diff] [blame] | 1054 | static int |
| 1055 | luaV_buffer_insert(lua_State *L) |
| 1056 | { |
Bram Moolenaar | 1dced57 | 2012-04-05 16:54:08 +0200 | [diff] [blame] | 1057 | luaV_Buffer *lb = luaV_checkudata(L, 1, LUAVIM_BUFFER); |
| 1058 | buf_T *b = (buf_T *) luaV_checkcache(L, (void *) *lb); |
| 1059 | linenr_T last = b->b_ml.ml_line_count; |
Bram Moolenaar | 0ba0429 | 2010-07-14 23:23:17 +0200 | [diff] [blame] | 1060 | linenr_T n = (linenr_T) luaL_optinteger(L, 3, last); |
| 1061 | buf_T *buf; |
| 1062 | luaL_checktype(L, 2, LUA_TSTRING); |
| 1063 | #ifdef HAVE_SANDBOX |
| 1064 | luaV_checksandbox(L); |
| 1065 | #endif |
| 1066 | /* fix insertion line */ |
| 1067 | if (n < 0) n = 0; |
| 1068 | if (n > last) n = last; |
| 1069 | /* insert */ |
| 1070 | buf = curbuf; |
Bram Moolenaar | 1dced57 | 2012-04-05 16:54:08 +0200 | [diff] [blame] | 1071 | curbuf = b; |
Bram Moolenaar | 55d5c03 | 2010-07-17 23:52:29 +0200 | [diff] [blame] | 1072 | if (u_save(n, n + 1) == FAIL) |
| 1073 | { |
| 1074 | curbuf = buf; |
| 1075 | luaL_error(L, "cannot save undo information"); |
Bram Moolenaar | 0ba0429 | 2010-07-14 23:23:17 +0200 | [diff] [blame] | 1076 | } |
Bram Moolenaar | 55d5c03 | 2010-07-17 23:52:29 +0200 | [diff] [blame] | 1077 | else if (ml_append(n, luaV_toline(L, 2), 0, FALSE) == FAIL) |
| 1078 | { |
| 1079 | curbuf = buf; |
| 1080 | luaL_error(L, "cannot insert line"); |
Bram Moolenaar | 0ba0429 | 2010-07-14 23:23:17 +0200 | [diff] [blame] | 1081 | } |
| 1082 | else |
Bram Moolenaar | 55d5c03 | 2010-07-17 23:52:29 +0200 | [diff] [blame] | 1083 | appended_lines_mark(n, 1L); |
Bram Moolenaar | 0ba0429 | 2010-07-14 23:23:17 +0200 | [diff] [blame] | 1084 | curbuf = buf; |
| 1085 | update_screen(VALID); |
| 1086 | return 0; |
| 1087 | } |
| 1088 | |
Bram Moolenaar | 55d5c03 | 2010-07-17 23:52:29 +0200 | [diff] [blame] | 1089 | static int |
| 1090 | luaV_buffer_next(lua_State *L) |
| 1091 | { |
Bram Moolenaar | 0ba0429 | 2010-07-14 23:23:17 +0200 | [diff] [blame] | 1092 | luaV_Buffer *b = luaV_checkudata(L, 1, LUAVIM_BUFFER); |
Bram Moolenaar | 1dced57 | 2012-04-05 16:54:08 +0200 | [diff] [blame] | 1093 | buf_T *buf = (buf_T *) luaV_checkcache(L, (void *) *b); |
| 1094 | luaV_pushbuffer(L, buf->b_next); |
Bram Moolenaar | 0ba0429 | 2010-07-14 23:23:17 +0200 | [diff] [blame] | 1095 | return 1; |
| 1096 | } |
| 1097 | |
Bram Moolenaar | 55d5c03 | 2010-07-17 23:52:29 +0200 | [diff] [blame] | 1098 | static int |
| 1099 | luaV_buffer_previous(lua_State *L) |
| 1100 | { |
Bram Moolenaar | 0ba0429 | 2010-07-14 23:23:17 +0200 | [diff] [blame] | 1101 | luaV_Buffer *b = luaV_checkudata(L, 1, LUAVIM_BUFFER); |
Bram Moolenaar | 1dced57 | 2012-04-05 16:54:08 +0200 | [diff] [blame] | 1102 | buf_T *buf = (buf_T *) luaV_checkcache(L, (void *) *b); |
| 1103 | luaV_pushbuffer(L, buf->b_prev); |
Bram Moolenaar | 0ba0429 | 2010-07-14 23:23:17 +0200 | [diff] [blame] | 1104 | return 1; |
| 1105 | } |
| 1106 | |
Bram Moolenaar | 55d5c03 | 2010-07-17 23:52:29 +0200 | [diff] [blame] | 1107 | static int |
| 1108 | luaV_buffer_isvalid(lua_State *L) |
| 1109 | { |
Bram Moolenaar | 0ba0429 | 2010-07-14 23:23:17 +0200 | [diff] [blame] | 1110 | luaV_Buffer *b = luaV_checkudata(L, 1, LUAVIM_BUFFER); |
Bram Moolenaar | 1dced57 | 2012-04-05 16:54:08 +0200 | [diff] [blame] | 1111 | luaV_getudata(L, *b); |
Bram Moolenaar | 0ba0429 | 2010-07-14 23:23:17 +0200 | [diff] [blame] | 1112 | lua_pushboolean(L, !lua_isnil(L, -1)); |
| 1113 | return 1; |
| 1114 | } |
| 1115 | |
| 1116 | static const luaL_Reg luaV_Buffer_mt[] = { |
| 1117 | {"__tostring", luaV_buffer_tostring}, |
| 1118 | {"__len", luaV_buffer_len}, |
| 1119 | {"__call", luaV_buffer_call}, |
| 1120 | {"__index", luaV_buffer_index}, |
| 1121 | {"__newindex", luaV_buffer_newindex}, |
| 1122 | {"insert", luaV_buffer_insert}, |
| 1123 | {"next", luaV_buffer_next}, |
| 1124 | {"previous", luaV_buffer_previous}, |
| 1125 | {"isvalid", luaV_buffer_isvalid}, |
| 1126 | {NULL, NULL} |
| 1127 | }; |
| 1128 | |
| 1129 | |
| 1130 | /* ======= Window type ======= */ |
| 1131 | |
Bram Moolenaar | 1dced57 | 2012-04-05 16:54:08 +0200 | [diff] [blame] | 1132 | luaV_newtype(win_T, window, luaV_Window, LUAVIM_WINDOW) |
| 1133 | luaV_pushtype(win_T, window, luaV_Window) |
| 1134 | luaV_type_tostring(window, LUAVIM_WINDOW) |
Bram Moolenaar | 0ba0429 | 2010-07-14 23:23:17 +0200 | [diff] [blame] | 1135 | |
Bram Moolenaar | 55d5c03 | 2010-07-17 23:52:29 +0200 | [diff] [blame] | 1136 | static int |
| 1137 | luaV_window_call(lua_State *L) |
| 1138 | { |
Bram Moolenaar | 1dced57 | 2012-04-05 16:54:08 +0200 | [diff] [blame] | 1139 | win_T *w = (win_T *) luaV_checkvalid(L, luaV_Window, 1); |
Bram Moolenaar | 0ba0429 | 2010-07-14 23:23:17 +0200 | [diff] [blame] | 1140 | lua_settop(L, 1); |
Bram Moolenaar | 1dced57 | 2012-04-05 16:54:08 +0200 | [diff] [blame] | 1141 | win_goto(w); |
Bram Moolenaar | 0ba0429 | 2010-07-14 23:23:17 +0200 | [diff] [blame] | 1142 | return 1; |
| 1143 | } |
| 1144 | |
Bram Moolenaar | 55d5c03 | 2010-07-17 23:52:29 +0200 | [diff] [blame] | 1145 | static int |
| 1146 | luaV_window_index(lua_State *L) |
| 1147 | { |
Bram Moolenaar | 1dced57 | 2012-04-05 16:54:08 +0200 | [diff] [blame] | 1148 | win_T *w = (win_T *) luaV_checkvalid(L, luaV_Window, 1); |
Bram Moolenaar | 0ba0429 | 2010-07-14 23:23:17 +0200 | [diff] [blame] | 1149 | const char *s = luaL_checkstring(L, 2); |
| 1150 | if (strncmp(s, "buffer", 6) == 0) |
Bram Moolenaar | 1dced57 | 2012-04-05 16:54:08 +0200 | [diff] [blame] | 1151 | luaV_pushbuffer(L, w->w_buffer); |
Bram Moolenaar | 0ba0429 | 2010-07-14 23:23:17 +0200 | [diff] [blame] | 1152 | else if (strncmp(s, "line", 4) == 0) |
Bram Moolenaar | 1dced57 | 2012-04-05 16:54:08 +0200 | [diff] [blame] | 1153 | lua_pushinteger(L, w->w_cursor.lnum); |
Bram Moolenaar | 0ba0429 | 2010-07-14 23:23:17 +0200 | [diff] [blame] | 1154 | else if (strncmp(s, "col", 3) == 0) |
Bram Moolenaar | 1dced57 | 2012-04-05 16:54:08 +0200 | [diff] [blame] | 1155 | lua_pushinteger(L, w->w_cursor.col + 1); |
Bram Moolenaar | 0ba0429 | 2010-07-14 23:23:17 +0200 | [diff] [blame] | 1156 | #ifdef FEAT_VERTSPLIT |
| 1157 | else if (strncmp(s, "width", 5) == 0) |
Bram Moolenaar | 1dced57 | 2012-04-05 16:54:08 +0200 | [diff] [blame] | 1158 | lua_pushinteger(L, W_WIDTH(w)); |
Bram Moolenaar | 0ba0429 | 2010-07-14 23:23:17 +0200 | [diff] [blame] | 1159 | #endif |
| 1160 | else if (strncmp(s, "height", 6) == 0) |
Bram Moolenaar | 1dced57 | 2012-04-05 16:54:08 +0200 | [diff] [blame] | 1161 | lua_pushinteger(L, w->w_height); |
Bram Moolenaar | 0ba0429 | 2010-07-14 23:23:17 +0200 | [diff] [blame] | 1162 | /* methods */ |
| 1163 | else if (strncmp(s, "next", 4) == 0 |
Bram Moolenaar | 55d5c03 | 2010-07-17 23:52:29 +0200 | [diff] [blame] | 1164 | || strncmp(s, "previous", 8) == 0 |
| 1165 | || strncmp(s, "isvalid", 7) == 0) |
| 1166 | { |
| 1167 | lua_getmetatable(L, 1); |
| 1168 | lua_getfield(L, -1, s); |
Bram Moolenaar | 0ba0429 | 2010-07-14 23:23:17 +0200 | [diff] [blame] | 1169 | } |
| 1170 | else |
Bram Moolenaar | 55d5c03 | 2010-07-17 23:52:29 +0200 | [diff] [blame] | 1171 | lua_pushnil(L); |
Bram Moolenaar | 0ba0429 | 2010-07-14 23:23:17 +0200 | [diff] [blame] | 1172 | return 1; |
| 1173 | } |
| 1174 | |
Bram Moolenaar | 55d5c03 | 2010-07-17 23:52:29 +0200 | [diff] [blame] | 1175 | static int |
| 1176 | luaV_window_newindex (lua_State *L) |
| 1177 | { |
Bram Moolenaar | 1dced57 | 2012-04-05 16:54:08 +0200 | [diff] [blame] | 1178 | win_T *w = (win_T *) luaV_checkvalid(L, luaV_Window, 1); |
Bram Moolenaar | 0ba0429 | 2010-07-14 23:23:17 +0200 | [diff] [blame] | 1179 | const char *s = luaL_checkstring(L, 2); |
| 1180 | int v = luaL_checkinteger(L, 3); |
Bram Moolenaar | 55d5c03 | 2010-07-17 23:52:29 +0200 | [diff] [blame] | 1181 | if (strncmp(s, "line", 4) == 0) |
| 1182 | { |
Bram Moolenaar | 0ba0429 | 2010-07-14 23:23:17 +0200 | [diff] [blame] | 1183 | #ifdef HAVE_SANDBOX |
Bram Moolenaar | 55d5c03 | 2010-07-17 23:52:29 +0200 | [diff] [blame] | 1184 | luaV_checksandbox(L); |
Bram Moolenaar | 0ba0429 | 2010-07-14 23:23:17 +0200 | [diff] [blame] | 1185 | #endif |
Bram Moolenaar | 1dced57 | 2012-04-05 16:54:08 +0200 | [diff] [blame] | 1186 | if (v < 1 || v > w->w_buffer->b_ml.ml_line_count) |
Bram Moolenaar | 55d5c03 | 2010-07-17 23:52:29 +0200 | [diff] [blame] | 1187 | luaL_error(L, "line out of range"); |
Bram Moolenaar | 1dced57 | 2012-04-05 16:54:08 +0200 | [diff] [blame] | 1188 | w->w_cursor.lnum = v; |
Bram Moolenaar | 55d5c03 | 2010-07-17 23:52:29 +0200 | [diff] [blame] | 1189 | update_screen(VALID); |
Bram Moolenaar | 0ba0429 | 2010-07-14 23:23:17 +0200 | [diff] [blame] | 1190 | } |
Bram Moolenaar | 55d5c03 | 2010-07-17 23:52:29 +0200 | [diff] [blame] | 1191 | else if (strncmp(s, "col", 3) == 0) |
| 1192 | { |
Bram Moolenaar | 0ba0429 | 2010-07-14 23:23:17 +0200 | [diff] [blame] | 1193 | #ifdef HAVE_SANDBOX |
Bram Moolenaar | 55d5c03 | 2010-07-17 23:52:29 +0200 | [diff] [blame] | 1194 | luaV_checksandbox(L); |
Bram Moolenaar | 0ba0429 | 2010-07-14 23:23:17 +0200 | [diff] [blame] | 1195 | #endif |
Bram Moolenaar | 1dced57 | 2012-04-05 16:54:08 +0200 | [diff] [blame] | 1196 | w->w_cursor.col = v - 1; |
Bram Moolenaar | 55d5c03 | 2010-07-17 23:52:29 +0200 | [diff] [blame] | 1197 | update_screen(VALID); |
Bram Moolenaar | 0ba0429 | 2010-07-14 23:23:17 +0200 | [diff] [blame] | 1198 | } |
| 1199 | #ifdef FEAT_VERTSPLIT |
Bram Moolenaar | 55d5c03 | 2010-07-17 23:52:29 +0200 | [diff] [blame] | 1200 | else if (strncmp(s, "width", 5) == 0) |
| 1201 | { |
| 1202 | win_T *win = curwin; |
Bram Moolenaar | 0ba0429 | 2010-07-14 23:23:17 +0200 | [diff] [blame] | 1203 | #ifdef FEAT_GUI |
Bram Moolenaar | 55d5c03 | 2010-07-17 23:52:29 +0200 | [diff] [blame] | 1204 | need_mouse_correct = TRUE; |
Bram Moolenaar | 0ba0429 | 2010-07-14 23:23:17 +0200 | [diff] [blame] | 1205 | #endif |
Bram Moolenaar | 1dced57 | 2012-04-05 16:54:08 +0200 | [diff] [blame] | 1206 | curwin = w; |
Bram Moolenaar | 55d5c03 | 2010-07-17 23:52:29 +0200 | [diff] [blame] | 1207 | win_setwidth(v); |
| 1208 | curwin = win; |
Bram Moolenaar | 0ba0429 | 2010-07-14 23:23:17 +0200 | [diff] [blame] | 1209 | } |
| 1210 | #endif |
Bram Moolenaar | 55d5c03 | 2010-07-17 23:52:29 +0200 | [diff] [blame] | 1211 | else if (strncmp(s, "height", 6) == 0) |
| 1212 | { |
| 1213 | win_T *win = curwin; |
Bram Moolenaar | 0ba0429 | 2010-07-14 23:23:17 +0200 | [diff] [blame] | 1214 | #ifdef FEAT_GUI |
Bram Moolenaar | 55d5c03 | 2010-07-17 23:52:29 +0200 | [diff] [blame] | 1215 | need_mouse_correct = TRUE; |
Bram Moolenaar | 0ba0429 | 2010-07-14 23:23:17 +0200 | [diff] [blame] | 1216 | #endif |
Bram Moolenaar | 1dced57 | 2012-04-05 16:54:08 +0200 | [diff] [blame] | 1217 | curwin = w; |
Bram Moolenaar | 55d5c03 | 2010-07-17 23:52:29 +0200 | [diff] [blame] | 1218 | win_setheight(v); |
| 1219 | curwin = win; |
Bram Moolenaar | 0ba0429 | 2010-07-14 23:23:17 +0200 | [diff] [blame] | 1220 | } |
| 1221 | else |
Bram Moolenaar | 55d5c03 | 2010-07-17 23:52:29 +0200 | [diff] [blame] | 1222 | luaL_error(L, "invalid window property: `%s'", s); |
Bram Moolenaar | 0ba0429 | 2010-07-14 23:23:17 +0200 | [diff] [blame] | 1223 | return 0; |
| 1224 | } |
| 1225 | |
Bram Moolenaar | 55d5c03 | 2010-07-17 23:52:29 +0200 | [diff] [blame] | 1226 | static int |
| 1227 | luaV_window_next(lua_State *L) |
| 1228 | { |
Bram Moolenaar | 0ba0429 | 2010-07-14 23:23:17 +0200 | [diff] [blame] | 1229 | luaV_Window *w = luaV_checkudata(L, 1, LUAVIM_WINDOW); |
Bram Moolenaar | 1dced57 | 2012-04-05 16:54:08 +0200 | [diff] [blame] | 1230 | win_T *win = (win_T *) luaV_checkcache(L, (void *) *w); |
| 1231 | luaV_pushwindow(L, win->w_next); |
Bram Moolenaar | 0ba0429 | 2010-07-14 23:23:17 +0200 | [diff] [blame] | 1232 | return 1; |
| 1233 | } |
| 1234 | |
Bram Moolenaar | 55d5c03 | 2010-07-17 23:52:29 +0200 | [diff] [blame] | 1235 | static int |
| 1236 | luaV_window_previous(lua_State *L) |
| 1237 | { |
Bram Moolenaar | 0ba0429 | 2010-07-14 23:23:17 +0200 | [diff] [blame] | 1238 | luaV_Window *w = luaV_checkudata(L, 1, LUAVIM_WINDOW); |
Bram Moolenaar | 1dced57 | 2012-04-05 16:54:08 +0200 | [diff] [blame] | 1239 | win_T *win = (win_T *) luaV_checkcache(L, (void *) *w); |
| 1240 | luaV_pushwindow(L, win->w_prev); |
Bram Moolenaar | 0ba0429 | 2010-07-14 23:23:17 +0200 | [diff] [blame] | 1241 | return 1; |
| 1242 | } |
| 1243 | |
Bram Moolenaar | 55d5c03 | 2010-07-17 23:52:29 +0200 | [diff] [blame] | 1244 | static int |
| 1245 | luaV_window_isvalid(lua_State *L) |
| 1246 | { |
Bram Moolenaar | 0ba0429 | 2010-07-14 23:23:17 +0200 | [diff] [blame] | 1247 | luaV_Window *w = luaV_checkudata(L, 1, LUAVIM_WINDOW); |
Bram Moolenaar | 1dced57 | 2012-04-05 16:54:08 +0200 | [diff] [blame] | 1248 | luaV_getudata(L, *w); |
Bram Moolenaar | 0ba0429 | 2010-07-14 23:23:17 +0200 | [diff] [blame] | 1249 | lua_pushboolean(L, !lua_isnil(L, -1)); |
| 1250 | return 1; |
| 1251 | } |
| 1252 | |
| 1253 | static const luaL_Reg luaV_Window_mt[] = { |
| 1254 | {"__tostring", luaV_window_tostring}, |
| 1255 | {"__call", luaV_window_call}, |
| 1256 | {"__index", luaV_window_index}, |
| 1257 | {"__newindex", luaV_window_newindex}, |
| 1258 | {"next", luaV_window_next}, |
| 1259 | {"previous", luaV_window_previous}, |
| 1260 | {"isvalid", luaV_window_isvalid}, |
| 1261 | {NULL, NULL} |
| 1262 | }; |
| 1263 | |
| 1264 | |
| 1265 | /* ======= Vim module ======= */ |
| 1266 | |
Bram Moolenaar | 55d5c03 | 2010-07-17 23:52:29 +0200 | [diff] [blame] | 1267 | static int |
| 1268 | luaV_print(lua_State *L) |
| 1269 | { |
Bram Moolenaar | 0ba0429 | 2010-07-14 23:23:17 +0200 | [diff] [blame] | 1270 | int i, n = lua_gettop(L); /* nargs */ |
| 1271 | const char *s; |
| 1272 | size_t l; |
| 1273 | luaL_Buffer b; |
| 1274 | luaL_buffinit(L, &b); |
| 1275 | lua_getglobal(L, "tostring"); |
Bram Moolenaar | 55d5c03 | 2010-07-17 23:52:29 +0200 | [diff] [blame] | 1276 | for (i = 1; i <= n; i++) |
| 1277 | { |
| 1278 | lua_pushvalue(L, -1); /* tostring */ |
| 1279 | lua_pushvalue(L, i); /* arg */ |
| 1280 | lua_call(L, 1, 1); |
| 1281 | s = lua_tolstring(L, -1, &l); |
| 1282 | if (s == NULL) |
| 1283 | return luaL_error(L, "cannot convert to string"); |
| 1284 | if (i > 1) luaL_addchar(&b, ' '); /* use space instead of tab */ |
| 1285 | luaV_addlstring(&b, s, l, 0); |
| 1286 | lua_pop(L, 1); |
Bram Moolenaar | 0ba0429 | 2010-07-14 23:23:17 +0200 | [diff] [blame] | 1287 | } |
| 1288 | luaL_pushresult(&b); |
| 1289 | luaV_msg(L); |
| 1290 | return 0; |
| 1291 | } |
| 1292 | |
Bram Moolenaar | 55d5c03 | 2010-07-17 23:52:29 +0200 | [diff] [blame] | 1293 | static int |
Bram Moolenaar | 38e2b06 | 2011-09-21 17:15:39 +0200 | [diff] [blame] | 1294 | luaV_debug(lua_State *L) |
| 1295 | { |
| 1296 | lua_settop(L, 0); |
| 1297 | lua_getglobal(L, "vim"); |
| 1298 | lua_getfield(L, -1, "eval"); |
| 1299 | lua_remove(L, -2); /* vim.eval at position 1 */ |
| 1300 | for (;;) |
| 1301 | { |
| 1302 | const char *input; |
| 1303 | size_t l; |
| 1304 | lua_pushvalue(L, 1); /* vim.eval */ |
| 1305 | lua_pushliteral(L, "input('lua_debug> ')"); |
| 1306 | lua_call(L, 1, 1); /* return string */ |
| 1307 | input = lua_tolstring(L, -1, &l); |
| 1308 | if (l == 0 || strcmp(input, "cont") == 0) |
| 1309 | return 0; |
| 1310 | msg_putchar('\n'); /* avoid outputting on input line */ |
| 1311 | if (luaL_loadbuffer(L, input, l, "=(debug command)") |
| 1312 | || lua_pcall(L, 0, 0, 0)) |
| 1313 | luaV_emsg(L); |
| 1314 | lua_settop(L, 1); /* remove eventual returns, but keep vim.eval */ |
| 1315 | } |
| 1316 | } |
| 1317 | |
| 1318 | static int |
Bram Moolenaar | 55d5c03 | 2010-07-17 23:52:29 +0200 | [diff] [blame] | 1319 | luaV_command(lua_State *L) |
| 1320 | { |
Bram Moolenaar | 0ba0429 | 2010-07-14 23:23:17 +0200 | [diff] [blame] | 1321 | do_cmdline_cmd((char_u *) luaL_checkstring(L, 1)); |
| 1322 | update_screen(VALID); |
| 1323 | return 0; |
| 1324 | } |
| 1325 | |
Bram Moolenaar | 55d5c03 | 2010-07-17 23:52:29 +0200 | [diff] [blame] | 1326 | static int |
| 1327 | luaV_eval(lua_State *L) |
| 1328 | { |
Bram Moolenaar | 0ba0429 | 2010-07-14 23:23:17 +0200 | [diff] [blame] | 1329 | typval_T *tv = eval_expr((char_u *) luaL_checkstring(L, 1), NULL); |
| 1330 | if (tv == NULL) luaL_error(L, "invalid expression"); |
| 1331 | luaV_pushtypval(L, tv); |
Bram Moolenaar | b376647 | 2013-04-15 13:49:21 +0200 | [diff] [blame] | 1332 | free_tv(tv); |
Bram Moolenaar | 0ba0429 | 2010-07-14 23:23:17 +0200 | [diff] [blame] | 1333 | return 1; |
| 1334 | } |
| 1335 | |
Bram Moolenaar | 55d5c03 | 2010-07-17 23:52:29 +0200 | [diff] [blame] | 1336 | static int |
Bram Moolenaar | 0d2e4fc | 2010-07-18 12:35:47 +0200 | [diff] [blame] | 1337 | luaV_beep(lua_State *L UNUSED) |
Bram Moolenaar | 55d5c03 | 2010-07-17 23:52:29 +0200 | [diff] [blame] | 1338 | { |
Bram Moolenaar | 0ba0429 | 2010-07-14 23:23:17 +0200 | [diff] [blame] | 1339 | vim_beep(); |
| 1340 | return 0; |
| 1341 | } |
| 1342 | |
Bram Moolenaar | 55d5c03 | 2010-07-17 23:52:29 +0200 | [diff] [blame] | 1343 | static int |
| 1344 | luaV_line(lua_State *L) |
| 1345 | { |
Bram Moolenaar | 0ba0429 | 2010-07-14 23:23:17 +0200 | [diff] [blame] | 1346 | luaV_pushline(L, curbuf, curwin->w_cursor.lnum); |
| 1347 | return 1; |
| 1348 | } |
| 1349 | |
Bram Moolenaar | 55d5c03 | 2010-07-17 23:52:29 +0200 | [diff] [blame] | 1350 | static int |
Bram Moolenaar | 1dced57 | 2012-04-05 16:54:08 +0200 | [diff] [blame] | 1351 | luaV_list(lua_State *L) |
| 1352 | { |
| 1353 | list_T *l = list_alloc(); |
| 1354 | if (l == NULL) |
| 1355 | lua_pushnil(L); |
| 1356 | else |
| 1357 | luaV_newlist(L, l); |
| 1358 | return 1; |
| 1359 | } |
| 1360 | |
| 1361 | static int |
| 1362 | luaV_dict(lua_State *L) |
| 1363 | { |
| 1364 | dict_T *d = dict_alloc(); |
| 1365 | if (d == NULL) |
| 1366 | lua_pushnil(L); |
| 1367 | else |
| 1368 | luaV_newdict(L, d); |
| 1369 | return 1; |
| 1370 | } |
| 1371 | |
| 1372 | static int |
Bram Moolenaar | 55d5c03 | 2010-07-17 23:52:29 +0200 | [diff] [blame] | 1373 | luaV_buffer(lua_State *L) |
| 1374 | { |
Bram Moolenaar | 0ba0429 | 2010-07-14 23:23:17 +0200 | [diff] [blame] | 1375 | buf_T *buf; |
Bram Moolenaar | 55d5c03 | 2010-07-17 23:52:29 +0200 | [diff] [blame] | 1376 | if (lua_isstring(L, 1)) /* get by number or name? */ |
| 1377 | { |
| 1378 | if (lua_isnumber(L, 1)) /* by number? */ |
| 1379 | { |
| 1380 | int n = lua_tointeger(L, 1); |
| 1381 | for (buf = firstbuf; buf != NULL; buf = buf->b_next) |
| 1382 | if (buf->b_fnum == n) break; |
| 1383 | } |
| 1384 | else { /* by name */ |
| 1385 | size_t l; |
| 1386 | const char *s = lua_tolstring(L, 1, &l); |
| 1387 | for (buf = firstbuf; buf != NULL; buf = buf->b_next) |
| 1388 | { |
| 1389 | if (buf->b_ffname == NULL || buf->b_sfname == NULL) |
| 1390 | { |
| 1391 | if (l == 0) break; |
| 1392 | } |
Bram Moolenaar | 0d2e4fc | 2010-07-18 12:35:47 +0200 | [diff] [blame] | 1393 | else if (strncmp(s, (char *)buf->b_ffname, l) == 0 |
| 1394 | || strncmp(s, (char *)buf->b_sfname, l) == 0) |
Bram Moolenaar | 55d5c03 | 2010-07-17 23:52:29 +0200 | [diff] [blame] | 1395 | break; |
| 1396 | } |
| 1397 | } |
Bram Moolenaar | 0ba0429 | 2010-07-14 23:23:17 +0200 | [diff] [blame] | 1398 | } |
Bram Moolenaar | 1dced57 | 2012-04-05 16:54:08 +0200 | [diff] [blame] | 1399 | else |
Bram Moolenaar | 55d5c03 | 2010-07-17 23:52:29 +0200 | [diff] [blame] | 1400 | buf = (lua_toboolean(L, 1)) ? firstbuf : curbuf; /* first buffer? */ |
Bram Moolenaar | 1dced57 | 2012-04-05 16:54:08 +0200 | [diff] [blame] | 1401 | luaV_pushbuffer(L, buf); |
Bram Moolenaar | 0ba0429 | 2010-07-14 23:23:17 +0200 | [diff] [blame] | 1402 | return 1; |
| 1403 | } |
| 1404 | |
Bram Moolenaar | 55d5c03 | 2010-07-17 23:52:29 +0200 | [diff] [blame] | 1405 | static int |
| 1406 | luaV_window(lua_State *L) |
| 1407 | { |
Bram Moolenaar | 0ba0429 | 2010-07-14 23:23:17 +0200 | [diff] [blame] | 1408 | win_T *win; |
Bram Moolenaar | 55d5c03 | 2010-07-17 23:52:29 +0200 | [diff] [blame] | 1409 | if (lua_isnumber(L, 1)) /* get by number? */ |
| 1410 | { |
| 1411 | int n = lua_tointeger(L, 1); |
| 1412 | for (win = firstwin; win != NULL; win = win->w_next, n--) |
| 1413 | if (n == 1) break; |
Bram Moolenaar | 0ba0429 | 2010-07-14 23:23:17 +0200 | [diff] [blame] | 1414 | } |
Bram Moolenaar | 1dced57 | 2012-04-05 16:54:08 +0200 | [diff] [blame] | 1415 | else |
Bram Moolenaar | 55d5c03 | 2010-07-17 23:52:29 +0200 | [diff] [blame] | 1416 | win = (lua_toboolean(L, 1)) ? firstwin : curwin; /* first window? */ |
Bram Moolenaar | 1dced57 | 2012-04-05 16:54:08 +0200 | [diff] [blame] | 1417 | luaV_pushwindow(L, win); |
Bram Moolenaar | 0ba0429 | 2010-07-14 23:23:17 +0200 | [diff] [blame] | 1418 | return 1; |
| 1419 | } |
| 1420 | |
Bram Moolenaar | 55d5c03 | 2010-07-17 23:52:29 +0200 | [diff] [blame] | 1421 | static int |
| 1422 | luaV_open(lua_State *L) |
| 1423 | { |
Bram Moolenaar | 0ba0429 | 2010-07-14 23:23:17 +0200 | [diff] [blame] | 1424 | char_u *s = NULL; |
| 1425 | #ifdef HAVE_SANDBOX |
| 1426 | luaV_checksandbox(L); |
| 1427 | #endif |
| 1428 | if (lua_isstring(L, 1)) s = (char_u *) lua_tostring(L, 1); |
Bram Moolenaar | fa263a5 | 2011-12-08 16:00:16 +0100 | [diff] [blame] | 1429 | luaV_pushbuffer(L, buflist_new(s, NULL, 1L, BLN_LISTED)); |
Bram Moolenaar | 0ba0429 | 2010-07-14 23:23:17 +0200 | [diff] [blame] | 1430 | return 1; |
| 1431 | } |
| 1432 | |
Bram Moolenaar | 55d5c03 | 2010-07-17 23:52:29 +0200 | [diff] [blame] | 1433 | static int |
Bram Moolenaar | 1dced57 | 2012-04-05 16:54:08 +0200 | [diff] [blame] | 1434 | luaV_type(lua_State *L) |
Bram Moolenaar | 55d5c03 | 2010-07-17 23:52:29 +0200 | [diff] [blame] | 1435 | { |
Bram Moolenaar | 1dced57 | 2012-04-05 16:54:08 +0200 | [diff] [blame] | 1436 | luaL_checkany(L, 1); |
| 1437 | if (lua_type(L, 1) == LUA_TUSERDATA) /* check vim udata? */ |
Bram Moolenaar | 55d5c03 | 2010-07-17 23:52:29 +0200 | [diff] [blame] | 1438 | { |
Bram Moolenaar | 1dced57 | 2012-04-05 16:54:08 +0200 | [diff] [blame] | 1439 | lua_settop(L, 1); |
| 1440 | if (lua_getmetatable(L, 1)) |
| 1441 | { |
| 1442 | luaV_getfield(L, LUAVIM_LIST); |
| 1443 | if (lua_rawequal(L, -1, 2)) |
| 1444 | { |
| 1445 | lua_pushstring(L, "list"); |
| 1446 | return 1; |
| 1447 | } |
| 1448 | luaV_getfield(L, LUAVIM_DICT); |
| 1449 | if (lua_rawequal(L, -1, 2)) |
| 1450 | { |
| 1451 | lua_pushstring(L, "dict"); |
| 1452 | return 1; |
| 1453 | } |
| 1454 | luaV_getfield(L, LUAVIM_BUFFER); |
| 1455 | if (lua_rawequal(L, -1, 2)) |
| 1456 | { |
| 1457 | lua_pushstring(L, "buffer"); |
| 1458 | return 1; |
| 1459 | } |
| 1460 | luaV_getfield(L, LUAVIM_WINDOW); |
| 1461 | if (lua_rawequal(L, -1, 2)) |
| 1462 | { |
| 1463 | lua_pushstring(L, "window"); |
| 1464 | return 1; |
| 1465 | } |
| 1466 | } |
Bram Moolenaar | 0ba0429 | 2010-07-14 23:23:17 +0200 | [diff] [blame] | 1467 | } |
Bram Moolenaar | 1dced57 | 2012-04-05 16:54:08 +0200 | [diff] [blame] | 1468 | lua_pushstring(L, luaL_typename(L, 1)); /* fallback */ |
| 1469 | return 1; |
Bram Moolenaar | 0ba0429 | 2010-07-14 23:23:17 +0200 | [diff] [blame] | 1470 | } |
| 1471 | |
| 1472 | static const luaL_Reg luaV_module[] = { |
| 1473 | {"command", luaV_command}, |
| 1474 | {"eval", luaV_eval}, |
| 1475 | {"beep", luaV_beep}, |
| 1476 | {"line", luaV_line}, |
Bram Moolenaar | 1dced57 | 2012-04-05 16:54:08 +0200 | [diff] [blame] | 1477 | {"list", luaV_list}, |
| 1478 | {"dict", luaV_dict}, |
Bram Moolenaar | 0ba0429 | 2010-07-14 23:23:17 +0200 | [diff] [blame] | 1479 | {"buffer", luaV_buffer}, |
| 1480 | {"window", luaV_window}, |
| 1481 | {"open", luaV_open}, |
Bram Moolenaar | 1dced57 | 2012-04-05 16:54:08 +0200 | [diff] [blame] | 1482 | {"type", luaV_type}, |
Bram Moolenaar | 0ba0429 | 2010-07-14 23:23:17 +0200 | [diff] [blame] | 1483 | {NULL, NULL} |
| 1484 | }; |
| 1485 | |
Bram Moolenaar | 1dced57 | 2012-04-05 16:54:08 +0200 | [diff] [blame] | 1486 | /* for freeing list, dict, buffer and window objects; lightuserdata as arg */ |
| 1487 | static int |
| 1488 | luaV_free(lua_State *L) |
| 1489 | { |
| 1490 | lua_pushnil(L); |
| 1491 | luaV_setudata(L, lua_touserdata(L, 1)); |
| 1492 | return 0; |
| 1493 | } |
| 1494 | |
| 1495 | static int |
| 1496 | luaV_luaeval (lua_State *L) |
| 1497 | { |
| 1498 | luaL_Buffer b; |
| 1499 | size_t l; |
| 1500 | const char *str = lua_tolstring(L, 1, &l); |
| 1501 | typval_T *arg = (typval_T *) lua_touserdata(L, 2); |
| 1502 | typval_T *rettv = (typval_T *) lua_touserdata(L, 3); |
| 1503 | luaL_buffinit(L, &b); |
| 1504 | luaL_addlstring(&b, LUAVIM_EVALHEADER, sizeof(LUAVIM_EVALHEADER) - 1); |
| 1505 | luaL_addlstring(&b, str, l); |
| 1506 | luaL_pushresult(&b); |
| 1507 | str = lua_tolstring(L, -1, &l); |
| 1508 | if (luaL_loadbuffer(L, str, l, LUAVIM_EVALNAME)) /* compile error? */ |
| 1509 | { |
| 1510 | luaV_emsg(L); |
| 1511 | return 0; |
| 1512 | } |
| 1513 | luaV_pushtypval(L, arg); |
| 1514 | if (lua_pcall(L, 1, 1, 0)) /* running error? */ |
| 1515 | { |
| 1516 | luaV_emsg(L); |
| 1517 | return 0; |
| 1518 | } |
| 1519 | luaV_totypval(L, -1, rettv); |
| 1520 | return 0; |
| 1521 | } |
| 1522 | |
| 1523 | static int |
| 1524 | luaV_setref (lua_State *L) |
| 1525 | { |
Bram Moolenaar | 2459a5e | 2015-02-03 12:55:18 +0100 | [diff] [blame] | 1526 | int copyID = lua_tointeger(L, 1); |
| 1527 | int abort = FALSE; |
| 1528 | typval_T tv; |
| 1529 | |
Bram Moolenaar | 1dced57 | 2012-04-05 16:54:08 +0200 | [diff] [blame] | 1530 | luaV_getfield(L, LUAVIM_LIST); |
| 1531 | luaV_getfield(L, LUAVIM_DICT); |
| 1532 | lua_pushnil(L); |
Bram Moolenaar | 2459a5e | 2015-02-03 12:55:18 +0100 | [diff] [blame] | 1533 | while (!abort && lua_next(L, lua_upvalueindex(1)) != 0) /* traverse cache table */ |
Bram Moolenaar | 1dced57 | 2012-04-05 16:54:08 +0200 | [diff] [blame] | 1534 | { |
| 1535 | lua_getmetatable(L, -1); |
| 1536 | if (lua_rawequal(L, -1, 2)) /* list? */ |
| 1537 | { |
| 1538 | tv.v_type = VAR_LIST; |
| 1539 | tv.vval.v_list = (list_T *) lua_touserdata(L, 4); /* key */ |
| 1540 | } |
| 1541 | else if (lua_rawequal(L, -1, 3)) /* dict? */ |
| 1542 | { |
| 1543 | tv.v_type = VAR_DICT; |
| 1544 | tv.vval.v_dict = (dict_T *) lua_touserdata(L, 4); /* key */ |
| 1545 | } |
| 1546 | lua_pop(L, 2); /* metatable and value */ |
Bram Moolenaar | 2459a5e | 2015-02-03 12:55:18 +0100 | [diff] [blame] | 1547 | abort = set_ref_in_item(&tv, copyID, NULL, NULL); |
Bram Moolenaar | 1dced57 | 2012-04-05 16:54:08 +0200 | [diff] [blame] | 1548 | } |
Bram Moolenaar | 2459a5e | 2015-02-03 12:55:18 +0100 | [diff] [blame] | 1549 | lua_pushinteger(L, abort); |
Bram Moolenaar | 9d49da8 | 2015-02-03 23:10:46 +0100 | [diff] [blame^] | 1550 | return 0; |
Bram Moolenaar | 1dced57 | 2012-04-05 16:54:08 +0200 | [diff] [blame] | 1551 | } |
| 1552 | |
Bram Moolenaar | 55d5c03 | 2010-07-17 23:52:29 +0200 | [diff] [blame] | 1553 | static int |
| 1554 | luaopen_vim(lua_State *L) |
| 1555 | { |
Bram Moolenaar | 1dced57 | 2012-04-05 16:54:08 +0200 | [diff] [blame] | 1556 | /* set cache table */ |
Bram Moolenaar | 0ba0429 | 2010-07-14 23:23:17 +0200 | [diff] [blame] | 1557 | lua_newtable(L); |
| 1558 | lua_newtable(L); |
Bram Moolenaar | 1dced57 | 2012-04-05 16:54:08 +0200 | [diff] [blame] | 1559 | lua_pushstring(L, "v"); |
Bram Moolenaar | 0ba0429 | 2010-07-14 23:23:17 +0200 | [diff] [blame] | 1560 | lua_setfield(L, -2, "__mode"); |
Bram Moolenaar | 1dced57 | 2012-04-05 16:54:08 +0200 | [diff] [blame] | 1561 | lua_setmetatable(L, -2); /* cache is weak-valued */ |
Bram Moolenaar | 0ba0429 | 2010-07-14 23:23:17 +0200 | [diff] [blame] | 1562 | /* print */ |
| 1563 | lua_pushcfunction(L, luaV_print); |
| 1564 | lua_setglobal(L, "print"); |
Bram Moolenaar | 38e2b06 | 2011-09-21 17:15:39 +0200 | [diff] [blame] | 1565 | /* debug.debug */ |
| 1566 | lua_getglobal(L, "debug"); |
| 1567 | lua_pushcfunction(L, luaV_debug); |
| 1568 | lua_setfield(L, -2, "debug"); |
| 1569 | lua_pop(L, 1); |
Bram Moolenaar | 0ba0429 | 2010-07-14 23:23:17 +0200 | [diff] [blame] | 1570 | /* free */ |
| 1571 | lua_pushlightuserdata(L, (void *) LUAVIM_FREE); |
Bram Moolenaar | 1dced57 | 2012-04-05 16:54:08 +0200 | [diff] [blame] | 1572 | lua_pushvalue(L, 1); /* cache table */ |
| 1573 | lua_pushcclosure(L, luaV_free, 1); |
| 1574 | lua_rawset(L, LUA_REGISTRYINDEX); |
| 1575 | /* luaeval */ |
| 1576 | lua_pushlightuserdata(L, (void *) LUAVIM_LUAEVAL); |
| 1577 | lua_pushvalue(L, 1); /* cache table */ |
| 1578 | lua_pushcclosure(L, luaV_luaeval, 1); |
| 1579 | lua_rawset(L, LUA_REGISTRYINDEX); |
| 1580 | /* setref */ |
| 1581 | lua_pushlightuserdata(L, (void *) LUAVIM_SETREF); |
| 1582 | lua_pushvalue(L, 1); /* cache table */ |
| 1583 | lua_pushcclosure(L, luaV_setref, 1); |
Bram Moolenaar | 0ba0429 | 2010-07-14 23:23:17 +0200 | [diff] [blame] | 1584 | lua_rawset(L, LUA_REGISTRYINDEX); |
| 1585 | /* register */ |
Bram Moolenaar | 1dced57 | 2012-04-05 16:54:08 +0200 | [diff] [blame] | 1586 | luaV_newmetatable(L, LUAVIM_LIST); |
| 1587 | lua_pushvalue(L, 1); |
| 1588 | luaV_openlib(L, luaV_List_mt, 1); |
| 1589 | luaV_newmetatable(L, LUAVIM_DICT); |
| 1590 | lua_pushvalue(L, 1); |
| 1591 | luaV_openlib(L, luaV_Dict_mt, 1); |
Bram Moolenaar | 0ba0429 | 2010-07-14 23:23:17 +0200 | [diff] [blame] | 1592 | luaV_newmetatable(L, LUAVIM_BUFFER); |
Bram Moolenaar | 1dced57 | 2012-04-05 16:54:08 +0200 | [diff] [blame] | 1593 | lua_pushvalue(L, 1); /* cache table */ |
| 1594 | luaV_openlib(L, luaV_Buffer_mt, 1); |
Bram Moolenaar | 0ba0429 | 2010-07-14 23:23:17 +0200 | [diff] [blame] | 1595 | luaV_newmetatable(L, LUAVIM_WINDOW); |
Bram Moolenaar | 1dced57 | 2012-04-05 16:54:08 +0200 | [diff] [blame] | 1596 | lua_pushvalue(L, 1); /* cache table */ |
| 1597 | luaV_openlib(L, luaV_Window_mt, 1); |
| 1598 | lua_newtable(L); /* vim table */ |
| 1599 | lua_pushvalue(L, 1); /* cache table */ |
| 1600 | luaV_openlib(L, luaV_module, 1); |
| 1601 | lua_setglobal(L, LUAVIM_NAME); |
Bram Moolenaar | 0ba0429 | 2010-07-14 23:23:17 +0200 | [diff] [blame] | 1602 | return 0; |
| 1603 | } |
| 1604 | |
Bram Moolenaar | 55d5c03 | 2010-07-17 23:52:29 +0200 | [diff] [blame] | 1605 | static lua_State * |
| 1606 | luaV_newstate(void) |
| 1607 | { |
Bram Moolenaar | 0ba0429 | 2010-07-14 23:23:17 +0200 | [diff] [blame] | 1608 | lua_State *L = luaL_newstate(); |
Bram Moolenaar | 16c98f9 | 2010-07-28 22:46:08 +0200 | [diff] [blame] | 1609 | luaL_openlibs(L); /* core libs */ |
| 1610 | lua_pushcfunction(L, luaopen_vim); /* vim */ |
Bram Moolenaar | 0ba0429 | 2010-07-14 23:23:17 +0200 | [diff] [blame] | 1611 | lua_call(L, 0, 0); |
| 1612 | return L; |
| 1613 | } |
| 1614 | |
Bram Moolenaar | 55d5c03 | 2010-07-17 23:52:29 +0200 | [diff] [blame] | 1615 | static void |
| 1616 | luaV_setrange(lua_State *L, int line1, int line2) |
| 1617 | { |
Bram Moolenaar | 0ba0429 | 2010-07-14 23:23:17 +0200 | [diff] [blame] | 1618 | lua_getglobal(L, LUAVIM_NAME); |
| 1619 | lua_pushinteger(L, line1); |
| 1620 | lua_setfield(L, -2, "firstline"); |
| 1621 | lua_pushinteger(L, line2); |
| 1622 | lua_setfield(L, -2, "lastline"); |
| 1623 | lua_pop(L, 1); /* vim table */ |
| 1624 | } |
| 1625 | |
| 1626 | |
| 1627 | /* ======= Interface ======= */ |
| 1628 | |
| 1629 | static lua_State *L = NULL; |
| 1630 | |
Bram Moolenaar | 55d5c03 | 2010-07-17 23:52:29 +0200 | [diff] [blame] | 1631 | static int |
Bram Moolenaar | 1dced57 | 2012-04-05 16:54:08 +0200 | [diff] [blame] | 1632 | lua_isopen(void) |
Bram Moolenaar | 2bd6a1b | 2010-08-12 22:14:01 +0200 | [diff] [blame] | 1633 | { |
| 1634 | return L != NULL; |
| 1635 | } |
| 1636 | |
| 1637 | static int |
Bram Moolenaar | 55d5c03 | 2010-07-17 23:52:29 +0200 | [diff] [blame] | 1638 | lua_init(void) |
| 1639 | { |
Bram Moolenaar | 1dced57 | 2012-04-05 16:54:08 +0200 | [diff] [blame] | 1640 | if (!lua_isopen()) |
Bram Moolenaar | 55d5c03 | 2010-07-17 23:52:29 +0200 | [diff] [blame] | 1641 | { |
Bram Moolenaar | 0ba0429 | 2010-07-14 23:23:17 +0200 | [diff] [blame] | 1642 | #ifdef DYNAMIC_LUA |
Bram Moolenaar | 55d5c03 | 2010-07-17 23:52:29 +0200 | [diff] [blame] | 1643 | if (!lua_enabled(TRUE)) |
| 1644 | { |
| 1645 | EMSG(_("Lua library cannot be loaded.")); |
| 1646 | return FAIL; |
| 1647 | } |
Bram Moolenaar | 0ba0429 | 2010-07-14 23:23:17 +0200 | [diff] [blame] | 1648 | #endif |
Bram Moolenaar | 55d5c03 | 2010-07-17 23:52:29 +0200 | [diff] [blame] | 1649 | L = luaV_newstate(); |
Bram Moolenaar | 0ba0429 | 2010-07-14 23:23:17 +0200 | [diff] [blame] | 1650 | } |
| 1651 | return OK; |
| 1652 | } |
| 1653 | |
Bram Moolenaar | 55d5c03 | 2010-07-17 23:52:29 +0200 | [diff] [blame] | 1654 | void |
| 1655 | lua_end(void) |
| 1656 | { |
Bram Moolenaar | 1dced57 | 2012-04-05 16:54:08 +0200 | [diff] [blame] | 1657 | if (lua_isopen()) |
Bram Moolenaar | 55d5c03 | 2010-07-17 23:52:29 +0200 | [diff] [blame] | 1658 | { |
| 1659 | lua_close(L); |
| 1660 | L = NULL; |
Bram Moolenaar | 0ba0429 | 2010-07-14 23:23:17 +0200 | [diff] [blame] | 1661 | #ifdef DYNAMIC_LUA |
Bram Moolenaar | 55d5c03 | 2010-07-17 23:52:29 +0200 | [diff] [blame] | 1662 | end_dynamic_lua(); |
Bram Moolenaar | 0ba0429 | 2010-07-14 23:23:17 +0200 | [diff] [blame] | 1663 | #endif |
| 1664 | } |
| 1665 | } |
| 1666 | |
| 1667 | /* ex commands */ |
Bram Moolenaar | 55d5c03 | 2010-07-17 23:52:29 +0200 | [diff] [blame] | 1668 | void |
| 1669 | ex_lua(exarg_T *eap) |
| 1670 | { |
Bram Moolenaar | 0ba0429 | 2010-07-14 23:23:17 +0200 | [diff] [blame] | 1671 | char *script; |
| 1672 | if (lua_init() == FAIL) return; |
| 1673 | script = (char *) script_get(eap, eap->arg); |
Bram Moolenaar | 55d5c03 | 2010-07-17 23:52:29 +0200 | [diff] [blame] | 1674 | if (!eap->skip) |
| 1675 | { |
| 1676 | char *s = (script) ? script : (char *) eap->arg; |
| 1677 | luaV_setrange(L, eap->line1, eap->line2); |
| 1678 | if (luaL_loadbuffer(L, s, strlen(s), LUAVIM_CHUNKNAME) |
| 1679 | || lua_pcall(L, 0, 0, 0)) |
| 1680 | luaV_emsg(L); |
Bram Moolenaar | 0ba0429 | 2010-07-14 23:23:17 +0200 | [diff] [blame] | 1681 | } |
| 1682 | if (script != NULL) vim_free(script); |
| 1683 | } |
| 1684 | |
Bram Moolenaar | 55d5c03 | 2010-07-17 23:52:29 +0200 | [diff] [blame] | 1685 | void |
| 1686 | ex_luado(exarg_T *eap) |
| 1687 | { |
Bram Moolenaar | 0ba0429 | 2010-07-14 23:23:17 +0200 | [diff] [blame] | 1688 | linenr_T l; |
| 1689 | const char *s = (const char *) eap->arg; |
| 1690 | luaL_Buffer b; |
| 1691 | size_t len; |
| 1692 | if (lua_init() == FAIL) return; |
Bram Moolenaar | 55d5c03 | 2010-07-17 23:52:29 +0200 | [diff] [blame] | 1693 | if (u_save(eap->line1 - 1, eap->line2 + 1) == FAIL) |
| 1694 | { |
| 1695 | EMSG(_("cannot save undo information")); |
| 1696 | return; |
Bram Moolenaar | 0ba0429 | 2010-07-14 23:23:17 +0200 | [diff] [blame] | 1697 | } |
| 1698 | luaV_setrange(L, eap->line1, eap->line2); |
| 1699 | luaL_buffinit(L, &b); |
Bram Moolenaar | bd2f3c3 | 2012-04-06 14:31:00 +0200 | [diff] [blame] | 1700 | luaL_addlstring(&b, "return function(line, linenr) ", 30); /* header */ |
Bram Moolenaar | 0ba0429 | 2010-07-14 23:23:17 +0200 | [diff] [blame] | 1701 | luaL_addlstring(&b, s, strlen(s)); |
| 1702 | luaL_addlstring(&b, " end", 4); /* footer */ |
| 1703 | luaL_pushresult(&b); |
| 1704 | s = lua_tolstring(L, -1, &len); |
Bram Moolenaar | 55d5c03 | 2010-07-17 23:52:29 +0200 | [diff] [blame] | 1705 | if (luaL_loadbuffer(L, s, len, LUAVIM_CHUNKNAME)) |
| 1706 | { |
| 1707 | luaV_emsg(L); |
| 1708 | lua_pop(L, 1); /* function body */ |
| 1709 | return; |
Bram Moolenaar | 0ba0429 | 2010-07-14 23:23:17 +0200 | [diff] [blame] | 1710 | } |
| 1711 | lua_call(L, 0, 1); |
| 1712 | lua_replace(L, -2); /* function -> body */ |
Bram Moolenaar | 55d5c03 | 2010-07-17 23:52:29 +0200 | [diff] [blame] | 1713 | for (l = eap->line1; l <= eap->line2; l++) |
| 1714 | { |
| 1715 | lua_pushvalue(L, -1); /* function */ |
| 1716 | luaV_pushline(L, curbuf, l); /* current line as arg */ |
Bram Moolenaar | bd2f3c3 | 2012-04-06 14:31:00 +0200 | [diff] [blame] | 1717 | lua_pushinteger(L, l); /* current line number as arg */ |
| 1718 | if (lua_pcall(L, 2, 1, 0)) |
Bram Moolenaar | 55d5c03 | 2010-07-17 23:52:29 +0200 | [diff] [blame] | 1719 | { |
| 1720 | luaV_emsg(L); |
| 1721 | break; |
| 1722 | } |
| 1723 | if (lua_isstring(L, -1)) /* update line? */ |
| 1724 | { |
Bram Moolenaar | 0ba0429 | 2010-07-14 23:23:17 +0200 | [diff] [blame] | 1725 | #ifdef HAVE_SANDBOX |
Bram Moolenaar | 55d5c03 | 2010-07-17 23:52:29 +0200 | [diff] [blame] | 1726 | luaV_checksandbox(L); |
Bram Moolenaar | 0ba0429 | 2010-07-14 23:23:17 +0200 | [diff] [blame] | 1727 | #endif |
Bram Moolenaar | 55d5c03 | 2010-07-17 23:52:29 +0200 | [diff] [blame] | 1728 | ml_replace(l, luaV_toline(L, -1), TRUE); |
| 1729 | changed_bytes(l, 0); |
| 1730 | lua_pop(L, 1); /* result from luaV_toline */ |
| 1731 | } |
| 1732 | lua_pop(L, 1); /* line */ |
Bram Moolenaar | 0ba0429 | 2010-07-14 23:23:17 +0200 | [diff] [blame] | 1733 | } |
| 1734 | lua_pop(L, 1); /* function */ |
| 1735 | check_cursor(); |
| 1736 | update_screen(NOT_VALID); |
| 1737 | } |
| 1738 | |
Bram Moolenaar | 55d5c03 | 2010-07-17 23:52:29 +0200 | [diff] [blame] | 1739 | void |
| 1740 | ex_luafile(exarg_T *eap) |
| 1741 | { |
| 1742 | if (lua_init() == FAIL) |
| 1743 | return; |
| 1744 | if (!eap->skip) |
| 1745 | { |
| 1746 | luaV_setrange(L, eap->line1, eap->line2); |
| 1747 | if (luaL_loadfile(L, (char *) eap->arg) || lua_pcall(L, 0, 0, 0)) |
| 1748 | luaV_emsg(L); |
Bram Moolenaar | 0ba0429 | 2010-07-14 23:23:17 +0200 | [diff] [blame] | 1749 | } |
| 1750 | } |
| 1751 | |
Bram Moolenaar | 1dced57 | 2012-04-05 16:54:08 +0200 | [diff] [blame] | 1752 | #define luaV_freetype(typ,tname) \ |
| 1753 | void \ |
| 1754 | lua_##tname##_free(typ *o) \ |
| 1755 | { \ |
| 1756 | if (!lua_isopen()) return; \ |
| 1757 | luaV_getfield(L, LUAVIM_FREE); \ |
| 1758 | lua_pushlightuserdata(L, (void *) o); \ |
| 1759 | lua_call(L, 1, 0); \ |
| 1760 | } |
| 1761 | |
| 1762 | luaV_freetype(buf_T, buffer) |
| 1763 | luaV_freetype(win_T, window) |
| 1764 | |
Bram Moolenaar | 55d5c03 | 2010-07-17 23:52:29 +0200 | [diff] [blame] | 1765 | void |
Bram Moolenaar | 1dced57 | 2012-04-05 16:54:08 +0200 | [diff] [blame] | 1766 | do_luaeval (char_u *str, typval_T *arg, typval_T *rettv) |
Bram Moolenaar | 55d5c03 | 2010-07-17 23:52:29 +0200 | [diff] [blame] | 1767 | { |
Bram Moolenaar | 1dced57 | 2012-04-05 16:54:08 +0200 | [diff] [blame] | 1768 | lua_init(); |
| 1769 | luaV_getfield(L, LUAVIM_LUAEVAL); |
| 1770 | lua_pushstring(L, (char *) str); |
| 1771 | lua_pushlightuserdata(L, (void *) arg); |
| 1772 | lua_pushlightuserdata(L, (void *) rettv); |
| 1773 | lua_call(L, 3, 0); |
Bram Moolenaar | 0ba0429 | 2010-07-14 23:23:17 +0200 | [diff] [blame] | 1774 | } |
| 1775 | |
Bram Moolenaar | 2459a5e | 2015-02-03 12:55:18 +0100 | [diff] [blame] | 1776 | int |
Bram Moolenaar | 1dced57 | 2012-04-05 16:54:08 +0200 | [diff] [blame] | 1777 | set_ref_in_lua (int copyID) |
Bram Moolenaar | 55d5c03 | 2010-07-17 23:52:29 +0200 | [diff] [blame] | 1778 | { |
Bram Moolenaar | 2459a5e | 2015-02-03 12:55:18 +0100 | [diff] [blame] | 1779 | int aborted = 0; |
| 1780 | |
| 1781 | if (lua_isopen()) |
| 1782 | { |
| 1783 | luaV_getfield(L, LUAVIM_SETREF); |
| 1784 | /* call the function with 1 arg, getting 1 result back */ |
| 1785 | lua_pushinteger(L, copyID); |
| 1786 | lua_call(L, 1, 1); |
| 1787 | /* get the result */ |
| 1788 | aborted = lua_tointeger(L, -1); |
| 1789 | /* pop result off the stack */ |
| 1790 | lua_pop(L, 1); |
| 1791 | } |
| 1792 | return aborted; |
Bram Moolenaar | 0ba0429 | 2010-07-14 23:23:17 +0200 | [diff] [blame] | 1793 | } |
| 1794 | |
| 1795 | #endif |