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