Mercurial > vim
annotate src/if_lua.c @ 19177:0d7b7b121ca6
Added tag v8.2.0147 for changeset be81baeb69f8abd67575ea4164866bf9ab8db96c
author | Bram Moolenaar <Bram@vim.org> |
---|---|
date | Thu, 23 Jan 2020 21:00:04 +0100 |
parents | ba9f50bfda83 |
children | 94eda51ba9ba |
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; | |
19102
ba9f50bfda83
patch 8.2.0111: VAR_SPECIAL is also used for booleans
Bram Moolenaar <Bram@vim.org>
parents:
18798
diff
changeset
|
538 case VAR_BOOL: |
7712
bce3b5ddb393
commit https://github.com/vim/vim/commit/520e1e41f35b063ede63b41738c82d6636e78c34
Christian Brabandt <cb@256bit.org>
parents:
7528
diff
changeset
|
539 case VAR_SPECIAL: |
bce3b5ddb393
commit https://github.com/vim/vim/commit/520e1e41f35b063ede63b41738c82d6636e78c34
Christian Brabandt <cb@256bit.org>
parents:
7528
diff
changeset
|
540 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
|
541 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
|
542 else |
bce3b5ddb393
commit https://github.com/vim/vim/commit/520e1e41f35b063ede63b41738c82d6636e78c34
Christian Brabandt <cb@256bit.org>
parents:
7528
diff
changeset
|
543 lua_pushnil(L); |
bce3b5ddb393
commit https://github.com/vim/vim/commit/520e1e41f35b063ede63b41738c82d6636e78c34
Christian Brabandt <cb@256bit.org>
parents:
7528
diff
changeset
|
544 break; |
14235
e3bc8cdc94dd
patch 8.1.0134: Lua interface does not support funcref
Christian Brabandt <cb@256bit.org>
parents:
12515
diff
changeset
|
545 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
|
546 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
|
547 break; |
16076
a2f0e93a5857
patch 8.1.1043: Lua interface does not support Blob
Bram Moolenaar <Bram@vim.org>
parents:
16028
diff
changeset
|
548 case VAR_BLOB: |
a2f0e93a5857
patch 8.1.1043: Lua interface does not support Blob
Bram Moolenaar <Bram@vim.org>
parents:
16028
diff
changeset
|
549 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
|
550 break; |
3450 | 551 default: |
552 lua_pushnil(L); | |
553 } | |
554 } | |
2330 | 555 |
14235
e3bc8cdc94dd
patch 8.1.0134: Lua interface does not support funcref
Christian Brabandt <cb@256bit.org>
parents:
12515
diff
changeset
|
556 /* |
e3bc8cdc94dd
patch 8.1.0134: Lua interface does not support funcref
Christian Brabandt <cb@256bit.org>
parents:
12515
diff
changeset
|
557 * 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
|
558 * Returns OK or FAIL. |
e3bc8cdc94dd
patch 8.1.0134: Lua interface does not support funcref
Christian Brabandt <cb@256bit.org>
parents:
12515
diff
changeset
|
559 */ |
e3bc8cdc94dd
patch 8.1.0134: Lua interface does not support funcref
Christian Brabandt <cb@256bit.org>
parents:
12515
diff
changeset
|
560 static int |
e3bc8cdc94dd
patch 8.1.0134: Lua interface does not support funcref
Christian Brabandt <cb@256bit.org>
parents:
12515
diff
changeset
|
561 luaV_totypval(lua_State *L, int pos, typval_T *tv) |
3450 | 562 { |
14235
e3bc8cdc94dd
patch 8.1.0134: Lua interface does not support funcref
Christian Brabandt <cb@256bit.org>
parents:
12515
diff
changeset
|
563 int status = OK; |
e3bc8cdc94dd
patch 8.1.0134: Lua interface does not support funcref
Christian Brabandt <cb@256bit.org>
parents:
12515
diff
changeset
|
564 |
e3bc8cdc94dd
patch 8.1.0134: Lua interface does not support funcref
Christian Brabandt <cb@256bit.org>
parents:
12515
diff
changeset
|
565 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
|
566 { |
3450 | 567 case LUA_TBOOLEAN: |
19102
ba9f50bfda83
patch 8.2.0111: VAR_SPECIAL is also used for booleans
Bram Moolenaar <Bram@vim.org>
parents:
18798
diff
changeset
|
568 tv->v_type = VAR_BOOL; |
3450 | 569 tv->vval.v_number = (varnumber_T) lua_toboolean(L, pos); |
570 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
|
571 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
|
572 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
|
573 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
|
574 break; |
3450 | 575 case LUA_TSTRING: |
576 tv->v_type = VAR_STRING; | |
577 tv->vval.v_string = vim_strsave((char_u *) lua_tostring(L, pos)); | |
2330 | 578 break; |
3450 | 579 case LUA_TNUMBER: |
580 #ifdef FEAT_FLOAT | |
581 tv->v_type = VAR_FLOAT; | |
582 tv->vval.v_float = (float_T) lua_tonumber(L, pos); | |
583 #else | |
584 tv->v_type = VAR_NUMBER; | |
585 tv->vval.v_number = (varnumber_T) lua_tointeger(L, pos); | |
586 #endif | |
587 break; | |
14235
e3bc8cdc94dd
patch 8.1.0134: Lua interface does not support funcref
Christian Brabandt <cb@256bit.org>
parents:
12515
diff
changeset
|
588 case LUA_TUSERDATA: |
e3bc8cdc94dd
patch 8.1.0134: Lua interface does not support funcref
Christian Brabandt <cb@256bit.org>
parents:
12515
diff
changeset
|
589 { |
3450 | 590 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
|
591 |
16076
a2f0e93a5857
patch 8.1.1043: Lua interface does not support Blob
Bram Moolenaar <Bram@vim.org>
parents:
16028
diff
changeset
|
592 if (lua_getmetatable(L, pos)) // has metatable? |
2330 | 593 { |
16076
a2f0e93a5857
patch 8.1.1043: Lua interface does not support Blob
Bram Moolenaar <Bram@vim.org>
parents:
16028
diff
changeset
|
594 // check list |
3450 | 595 luaV_getfield(L, LUAVIM_LIST); |
596 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
|
597 { |
3450 | 598 tv->v_type = VAR_LIST; |
599 tv->vval.v_list = *((luaV_List *) p); | |
600 ++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
|
601 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
|
602 break; |
2330 | 603 } |
16076
a2f0e93a5857
patch 8.1.1043: Lua interface does not support Blob
Bram Moolenaar <Bram@vim.org>
parents:
16028
diff
changeset
|
604 // check dict |
3450 | 605 luaV_getfield(L, LUAVIM_DICT); |
606 if (lua_rawequal(L, -1, -3)) | |
607 { | |
608 tv->v_type = VAR_DICT; | |
609 tv->vval.v_dict = *((luaV_Dict *) p); | |
610 ++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
|
611 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
|
612 break; |
3450 | 613 } |
16076
a2f0e93a5857
patch 8.1.1043: Lua interface does not support Blob
Bram Moolenaar <Bram@vim.org>
parents:
16028
diff
changeset
|
614 // check blob |
a2f0e93a5857
patch 8.1.1043: Lua interface does not support Blob
Bram Moolenaar <Bram@vim.org>
parents:
16028
diff
changeset
|
615 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
|
616 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
|
617 { |
a2f0e93a5857
patch 8.1.1043: Lua interface does not support Blob
Bram Moolenaar <Bram@vim.org>
parents:
16028
diff
changeset
|
618 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
|
619 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
|
620 ++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
|
621 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
|
622 break; |
a2f0e93a5857
patch 8.1.1043: Lua interface does not support Blob
Bram Moolenaar <Bram@vim.org>
parents:
16028
diff
changeset
|
623 } |
a2f0e93a5857
patch 8.1.1043: Lua interface does not support Blob
Bram Moolenaar <Bram@vim.org>
parents:
16028
diff
changeset
|
624 // check funcref |
14235
e3bc8cdc94dd
patch 8.1.0134: Lua interface does not support funcref
Christian Brabandt <cb@256bit.org>
parents:
12515
diff
changeset
|
625 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
|
626 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
|
627 { |
e3bc8cdc94dd
patch 8.1.0134: Lua interface does not support funcref
Christian Brabandt <cb@256bit.org>
parents:
12515
diff
changeset
|
628 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
|
629 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
|
630 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
|
631 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
|
632 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
|
633 break; |
e3bc8cdc94dd
patch 8.1.0134: Lua interface does not support funcref
Christian Brabandt <cb@256bit.org>
parents:
12515
diff
changeset
|
634 } |
16076
a2f0e93a5857
patch 8.1.1043: Lua interface does not support Blob
Bram Moolenaar <Bram@vim.org>
parents:
16028
diff
changeset
|
635 lua_pop(L, 4); // MTs |
2330 | 636 } |
637 } | |
16076
a2f0e93a5857
patch 8.1.1043: Lua interface does not support Blob
Bram Moolenaar <Bram@vim.org>
parents:
16028
diff
changeset
|
638 // FALLTHROUGH |
2330 | 639 default: |
3450 | 640 tv->v_type = VAR_NUMBER; |
641 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
|
642 status = FAIL; |
2320
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
643 } |
14235
e3bc8cdc94dd
patch 8.1.0134: Lua interface does not support funcref
Christian Brabandt <cb@256bit.org>
parents:
12515
diff
changeset
|
644 return status; |
2320
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
645 } |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
646 |
18798
f0f9692d4487
patch 8.1.2387: using old C style comments
Bram Moolenaar <Bram@vim.org>
parents:
18370
diff
changeset
|
647 /* |
f0f9692d4487
patch 8.1.2387: using old C style comments
Bram Moolenaar <Bram@vim.org>
parents:
18370
diff
changeset
|
648 * 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
|
649 * \n with \0 otherwise |
f0f9692d4487
patch 8.1.2387: using old C style comments
Bram Moolenaar <Bram@vim.org>
parents:
18370
diff
changeset
|
650 */ |
2330 | 651 static void |
652 luaV_addlstring(luaL_Buffer *b, const char *s, size_t l, int toline) | |
653 { | |
654 while (l--) | |
655 { | |
656 if (*s == '\0' && toline) | |
657 luaL_addchar(b, '\n'); | |
658 else if (*s == '\n' && !toline) | |
659 luaL_addchar(b, '\0'); | |
660 else | |
661 luaL_addchar(b, *s); | |
662 s++; | |
2320
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 } |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
665 |
2330 | 666 static void |
667 luaV_pushline(lua_State *L, buf_T *buf, linenr_T n) | |
668 { | |
2320
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
669 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
|
670 luaL_Buffer b; |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
671 luaL_buffinit(L, &b); |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
672 luaV_addlstring(&b, s, strlen(s), 0); |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
673 luaL_pushresult(&b); |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
674 } |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
675 |
2330 | 676 static char_u * |
677 luaV_toline(lua_State *L, int pos) | |
678 { | |
2320
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
679 size_t l; |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
680 const char *s = lua_tolstring(L, pos, &l); |
2330 | 681 |
2320
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
682 luaL_Buffer b; |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
683 luaL_buffinit(L, &b); |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
684 luaV_addlstring(&b, s, l, 1); |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
685 luaL_pushresult(&b); |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
686 return (char_u *) lua_tostring(L, -1); |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
687 } |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
688 |
18798
f0f9692d4487
patch 8.1.2387: using old C style comments
Bram Moolenaar <Bram@vim.org>
parents:
18370
diff
changeset
|
689 /* |
f0f9692d4487
patch 8.1.2387: using old C style comments
Bram Moolenaar <Bram@vim.org>
parents:
18370
diff
changeset
|
690 * 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
|
691 * s separated by newlines |
f0f9692d4487
patch 8.1.2387: using old C style comments
Bram Moolenaar <Bram@vim.org>
parents:
18370
diff
changeset
|
692 */ |
2330 | 693 static void |
694 luaV_msgfunc(lua_State *L, msgfunc_T mf) | |
695 { | |
2320
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
696 luaL_Buffer b; |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
697 size_t l; |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
698 const char *p, *s = lua_tolstring(L, -1, &l); |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
699 luaL_buffinit(L, &b); |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
700 luaV_addlstring(&b, s, l, 0); |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
701 luaL_pushresult(&b); |
18798
f0f9692d4487
patch 8.1.2387: using old C style comments
Bram Moolenaar <Bram@vim.org>
parents:
18370
diff
changeset
|
702 // break string |
2320
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
703 p = s = lua_tolstring(L, -1, &l); |
2330 | 704 while (l--) |
705 { | |
18798
f0f9692d4487
patch 8.1.2387: using old C style comments
Bram Moolenaar <Bram@vim.org>
parents:
18370
diff
changeset
|
706 if (*p++ == '\0') // break? |
2330 | 707 { |
708 mf((char_u *) s); | |
709 s = p; | |
710 } | |
2320
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
711 } |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
712 mf((char_u *) s); |
18798
f0f9692d4487
patch 8.1.2387: using old C style comments
Bram Moolenaar <Bram@vim.org>
parents:
18370
diff
changeset
|
713 lua_pop(L, 2); // original and modified strings |
2320
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
714 } |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
715 |
3450 | 716 #define luaV_newtype(typ,tname,luatyp,luatname) \ |
717 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
|
718 luaV_new##tname(lua_State *L, typ *obj) \ |
3450 | 719 { \ |
720 luatyp *o = (luatyp *) lua_newuserdata(L, sizeof(luatyp)); \ | |
721 *o = obj; \ | |
722 luaV_setudata(L, obj); /* cache[obj] = udata */ \ | |
723 luaV_getfield(L, luatname); \ | |
724 lua_setmetatable(L, -2); \ | |
725 return o; \ | |
726 } | |
2320
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
727 |
3450 | 728 #define luaV_pushtype(typ,tname,luatyp) \ |
729 static luatyp * \ | |
14235
e3bc8cdc94dd
patch 8.1.0134: Lua interface does not support funcref
Christian Brabandt <cb@256bit.org>
parents:
12515
diff
changeset
|
730 luaV_push##tname(lua_State *L, typ *obj) \ |
3450 | 731 { \ |
732 luatyp *o = NULL; \ | |
733 if (obj == NULL) \ | |
734 lua_pushnil(L); \ | |
735 else { \ | |
736 luaV_getudata(L, obj); \ | |
737 if (lua_isnil(L, -1)) /* not interned? */ \ | |
738 { \ | |
739 lua_pop(L, 1); \ | |
740 o = luaV_new##tname(L, obj); \ | |
741 } \ | |
742 else \ | |
743 o = (luatyp *) lua_touserdata(L, -1); \ | |
744 } \ | |
745 return o; \ | |
746 } | |
747 | |
748 #define luaV_type_tostring(tname,luatname) \ | |
749 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
|
750 luaV_##tname##_tostring(lua_State *L) \ |
3450 | 751 { \ |
752 lua_pushfstring(L, "%s: %p", luatname, lua_touserdata(L, 1)); \ | |
753 return 1; \ | |
754 } | |
755 | |
18798
f0f9692d4487
patch 8.1.2387: using old C style comments
Bram Moolenaar <Bram@vim.org>
parents:
18370
diff
changeset
|
756 // ======= List type ======= |
3450 | 757 |
758 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
|
759 luaV_newlist(lua_State *L, list_T *lis) |
3450 | 760 { |
761 luaV_List *l = (luaV_List *) lua_newuserdata(L, sizeof(luaV_List)); | |
762 *l = lis; | |
18798
f0f9692d4487
patch 8.1.2387: using old C style comments
Bram Moolenaar <Bram@vim.org>
parents:
18370
diff
changeset
|
763 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
|
764 luaV_setudata(L, lis); // cache[lis] = udata |
3450 | 765 luaV_getfield(L, LUAVIM_LIST); |
766 lua_setmetatable(L, -2); | |
767 return l; | |
768 } | |
769 | |
770 luaV_pushtype(list_T, list, luaV_List) | |
771 luaV_type_tostring(list, LUAVIM_LIST) | |
2320
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
772 |
2330 | 773 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
|
774 luaV_list_len(lua_State *L) |
2330 | 775 { |
3450 | 776 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
|
777 lua_pushinteger(L, (int) list_len(l)); |
3450 | 778 return 1; |
779 } | |
780 | |
781 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
|
782 luaV_list_iter(lua_State *L) |
3450 | 783 { |
784 listitem_T *li = (listitem_T *) lua_touserdata(L, lua_upvalueindex(2)); | |
785 if (li == NULL) return 0; | |
786 luaV_pushtypval(L, &li->li_tv); | |
787 lua_pushlightuserdata(L, (void *) li->li_next); | |
788 lua_replace(L, lua_upvalueindex(2)); | |
789 return 1; | |
790 } | |
791 | |
792 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
|
793 luaV_list_call(lua_State *L) |
3450 | 794 { |
795 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
|
796 lua_pushvalue(L, lua_upvalueindex(1)); // pass cache table along |
3450 | 797 lua_pushlightuserdata(L, (void *) l->lv_first); |
798 lua_pushcclosure(L, luaV_list_iter, 2); | |
2320
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
799 return 1; |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
800 } |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
801 |
2330 | 802 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
|
803 luaV_list_index(lua_State *L) |
3450 | 804 { |
805 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
|
806 if (lua_isnumber(L, 2)) // list item? |
3450 | 807 { |
808 listitem_T *li = list_find(l, (long) luaL_checkinteger(L, 2)); | |
809 if (li == NULL) | |
810 lua_pushnil(L); | |
811 else | |
812 luaV_pushtypval(L, &li->li_tv); | |
813 } | |
18798
f0f9692d4487
patch 8.1.2387: using old C style comments
Bram Moolenaar <Bram@vim.org>
parents:
18370
diff
changeset
|
814 else if (lua_isstring(L, 2)) // method? |
3450 | 815 { |
816 const char *s = lua_tostring(L, 2); | |
817 if (strncmp(s, "add", 3) == 0 | |
4293 | 818 || strncmp(s, "insert", 6) == 0) |
3450 | 819 { |
820 lua_getmetatable(L, 1); | |
821 lua_getfield(L, -1, s); | |
822 } | |
823 else | |
824 lua_pushnil(L); | |
825 } | |
826 else | |
827 lua_pushnil(L); | |
828 return 1; | |
829 } | |
830 | |
831 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
|
832 luaV_list_newindex(lua_State *L) |
3450 | 833 { |
834 list_T *l = luaV_unbox(L, luaV_List, 1); | |
835 long n = (long) luaL_checkinteger(L, 2); | |
836 listitem_T *li; | |
837 if (l->lv_lock) | |
838 luaL_error(L, "list is locked"); | |
839 li = list_find(l, n); | |
840 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
|
841 if (lua_isnil(L, 3)) // remove? |
3450 | 842 { |
5871 | 843 vimlist_remove(l, li, li); |
3450 | 844 clear_tv(&li->li_tv); |
845 vim_free(li); | |
846 } | |
847 else | |
848 { | |
849 typval_T v; | |
14235
e3bc8cdc94dd
patch 8.1.0134: Lua interface does not support funcref
Christian Brabandt <cb@256bit.org>
parents:
12515
diff
changeset
|
850 luaV_checktypval(L, 3, &v, "setting list item"); |
3450 | 851 clear_tv(&li->li_tv); |
852 copy_tv(&v, &li->li_tv); | |
4293 | 853 clear_tv(&v); |
3450 | 854 } |
855 return 0; | |
856 } | |
857 | |
858 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
|
859 luaV_list_add(lua_State *L) |
3450 | 860 { |
861 luaV_List *lis = luaV_checkudata(L, 1, LUAVIM_LIST); | |
862 list_T *l = (list_T *) luaV_checkcache(L, (void *) *lis); | |
4293 | 863 typval_T v; |
3450 | 864 if (l->lv_lock) |
865 luaL_error(L, "list is locked"); | |
4293 | 866 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
|
867 luaV_checktypval(L, 2, &v, "adding list item"); |
4293 | 868 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
|
869 luaL_error(L, "failed to add item to list"); |
4293 | 870 clear_tv(&v); |
3450 | 871 lua_settop(L, 1); |
872 return 1; | |
873 } | |
874 | |
875 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
|
876 luaV_list_insert(lua_State *L) |
3450 | 877 { |
878 luaV_List *lis = luaV_checkudata(L, 1, LUAVIM_LIST); | |
879 list_T *l = (list_T *) luaV_checkcache(L, (void *) *lis); | |
6625 | 880 long pos = (long) luaL_optinteger(L, 3, 0); |
3450 | 881 listitem_T *li = NULL; |
882 typval_T v; | |
883 if (l->lv_lock) | |
884 luaL_error(L, "list is locked"); | |
885 if (pos < l->lv_len) | |
886 { | |
887 li = list_find(l, pos); | |
888 if (li == NULL) | |
889 luaL_error(L, "invalid position"); | |
890 } | |
891 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
|
892 luaV_checktypval(L, 2, &v, "inserting list item"); |
4293 | 893 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
|
894 luaL_error(L, "failed to add item to list"); |
4293 | 895 clear_tv(&v); |
3450 | 896 lua_settop(L, 1); |
897 return 1; | |
898 } | |
899 | |
900 static const luaL_Reg luaV_List_mt[] = { | |
901 {"__tostring", luaV_list_tostring}, | |
902 {"__len", luaV_list_len}, | |
903 {"__call", luaV_list_call}, | |
904 {"__index", luaV_list_index}, | |
905 {"__newindex", luaV_list_newindex}, | |
906 {"add", luaV_list_add}, | |
907 {"insert", luaV_list_insert}, | |
908 {NULL, NULL} | |
909 }; | |
910 | |
911 | |
18798
f0f9692d4487
patch 8.1.2387: using old C style comments
Bram Moolenaar <Bram@vim.org>
parents:
18370
diff
changeset
|
912 // ======= Dict type ======= |
3450 | 913 |
914 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
|
915 luaV_newdict(lua_State *L, dict_T *dic) |
3450 | 916 { |
917 luaV_Dict *d = (luaV_Dict *) lua_newuserdata(L, sizeof(luaV_Dict)); | |
918 *d = dic; | |
18798
f0f9692d4487
patch 8.1.2387: using old C style comments
Bram Moolenaar <Bram@vim.org>
parents:
18370
diff
changeset
|
919 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
|
920 luaV_setudata(L, dic); // cache[dic] = udata |
3450 | 921 luaV_getfield(L, LUAVIM_DICT); |
922 lua_setmetatable(L, -2); | |
923 return d; | |
924 } | |
925 | |
926 luaV_pushtype(dict_T, dict, luaV_Dict) | |
927 luaV_type_tostring(dict, LUAVIM_DICT) | |
928 | |
929 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
|
930 luaV_dict_len(lua_State *L) |
3450 | 931 { |
932 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
|
933 lua_pushinteger(L, (int) dict_len(d)); |
3450 | 934 return 1; |
935 } | |
936 | |
937 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
|
938 luaV_dict_iter(lua_State *L UNUSED) |
3450 | 939 { |
4135 | 940 #ifdef FEAT_EVAL |
3450 | 941 hashitem_T *hi = (hashitem_T *) lua_touserdata(L, lua_upvalueindex(2)); |
942 int n = lua_tointeger(L, lua_upvalueindex(3)); | |
943 dictitem_T *di; | |
944 if (n <= 0) return 0; | |
945 while (HASHITEM_EMPTY(hi)) hi++; | |
946 di = dict_lookup(hi); | |
947 lua_pushstring(L, (char *) hi->hi_key); | |
948 luaV_pushtypval(L, &di->di_tv); | |
949 lua_pushlightuserdata(L, (void *) (hi + 1)); | |
950 lua_replace(L, lua_upvalueindex(2)); | |
951 lua_pushinteger(L, n - 1); | |
952 lua_replace(L, lua_upvalueindex(3)); | |
953 return 2; | |
4135 | 954 #else |
955 return 0; | |
956 #endif | |
3450 | 957 } |
958 | |
959 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
|
960 luaV_dict_call(lua_State *L) |
3450 | 961 { |
962 dict_T *d = luaV_unbox(L, luaV_Dict, 1); | |
963 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
|
964 lua_pushvalue(L, lua_upvalueindex(1)); // pass cache table along |
3450 | 965 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
|
966 lua_pushinteger(L, ht->ht_used); // # remaining items |
3450 | 967 lua_pushcclosure(L, luaV_dict_iter, 3); |
968 return 1; | |
969 } | |
970 | |
971 static int | |
14235
e3bc8cdc94dd
patch 8.1.0134: Lua interface does not support funcref
Christian Brabandt <cb@256bit.org>
parents:
12515
diff
changeset
|
972 luaV_dict_index(lua_State *L) |
3450 | 973 { |
974 dict_T *d = luaV_unbox(L, luaV_Dict, 1); | |
975 char_u *key = (char_u *) luaL_checkstring(L, 2); | |
976 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
|
977 |
3450 | 978 if (di == NULL) |
979 lua_pushnil(L); | |
980 else | |
14235
e3bc8cdc94dd
patch 8.1.0134: Lua interface does not support funcref
Christian Brabandt <cb@256bit.org>
parents:
12515
diff
changeset
|
981 { |
3450 | 982 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
|
983 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
|
984 { |
e3bc8cdc94dd
patch 8.1.0134: Lua interface does not support funcref
Christian Brabandt <cb@256bit.org>
parents:
12515
diff
changeset
|
985 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
|
986 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
|
987 d->dv_refcount++; |
e3bc8cdc94dd
patch 8.1.0134: Lua interface does not support funcref
Christian Brabandt <cb@256bit.org>
parents:
12515
diff
changeset
|
988 } |
e3bc8cdc94dd
patch 8.1.0134: Lua interface does not support funcref
Christian Brabandt <cb@256bit.org>
parents:
12515
diff
changeset
|
989 } |
3450 | 990 return 1; |
991 } | |
992 | |
993 static int | |
14235
e3bc8cdc94dd
patch 8.1.0134: Lua interface does not support funcref
Christian Brabandt <cb@256bit.org>
parents:
12515
diff
changeset
|
994 luaV_dict_newindex(lua_State *L) |
3450 | 995 { |
996 dict_T *d = luaV_unbox(L, luaV_Dict, 1); | |
997 char_u *key = (char_u *) luaL_checkstring(L, 2); | |
998 dictitem_T *di; | |
14235
e3bc8cdc94dd
patch 8.1.0134: Lua interface does not support funcref
Christian Brabandt <cb@256bit.org>
parents:
12515
diff
changeset
|
999 typval_T v; |
16009
3b524e8110ab
patch 8.1.1010: Lua interface leaks memory
Bram Moolenaar <Bram@vim.org>
parents:
15868
diff
changeset
|
1000 |
3450 | 1001 if (d->dv_lock) |
1002 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
|
1003 if (key == NULL) |
fe651f98e173
patch 8.1.0180: static analysis errors in Lua interface
Christian Brabandt <cb@256bit.org>
parents:
14296
diff
changeset
|
1004 return 0; |
fe651f98e173
patch 8.1.0180: static analysis errors in Lua interface
Christian Brabandt <cb@256bit.org>
parents:
14296
diff
changeset
|
1005 if (*key == NUL) |
14235
e3bc8cdc94dd
patch 8.1.0134: Lua interface does not support funcref
Christian Brabandt <cb@256bit.org>
parents:
12515
diff
changeset
|
1006 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
|
1007 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
|
1008 { |
14235
e3bc8cdc94dd
patch 8.1.0134: Lua interface does not support funcref
Christian Brabandt <cb@256bit.org>
parents:
12515
diff
changeset
|
1009 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
|
1010 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
|
1011 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
|
1012 } |
3450 | 1013 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
|
1014 if (di == NULL) // non-existing key? |
3450 | 1015 { |
14329
fe651f98e173
patch 8.1.0180: static analysis errors in Lua interface
Christian Brabandt <cb@256bit.org>
parents:
14296
diff
changeset
|
1016 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
|
1017 return 0; |
3450 | 1018 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
|
1019 if (di == NULL) |
fe651f98e173
patch 8.1.0180: static analysis errors in Lua interface
Christian Brabandt <cb@256bit.org>
parents:
14296
diff
changeset
|
1020 return 0; |
3450 | 1021 if (dict_add(d, di) == FAIL) |
1022 { | |
14329
fe651f98e173
patch 8.1.0180: static analysis errors in Lua interface
Christian Brabandt <cb@256bit.org>
parents:
14296
diff
changeset
|
1023 vim_free(di); |
fe651f98e173
patch 8.1.0180: static analysis errors in Lua interface
Christian Brabandt <cb@256bit.org>
parents:
14296
diff
changeset
|
1024 return 0; |
3450 | 1025 } |
1026 } | |
1027 else | |
1028 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
|
1029 if (lua_isnil(L, 3)) // remove? |
3450 | 1030 { |
1031 hashitem_T *hi = hash_find(&d->dv_hashtab, di->di_key); | |
1032 hash_remove(&d->dv_hashtab, hi); | |
1033 dictitem_free(di); | |
1034 } | |
14235
e3bc8cdc94dd
patch 8.1.0134: Lua interface does not support funcref
Christian Brabandt <cb@256bit.org>
parents:
12515
diff
changeset
|
1035 else |
e3bc8cdc94dd
patch 8.1.0134: Lua interface does not support funcref
Christian Brabandt <cb@256bit.org>
parents:
12515
diff
changeset
|
1036 { |
3450 | 1037 copy_tv(&v, &di->di_tv); |
4293 | 1038 clear_tv(&v); |
3450 | 1039 } |
1040 return 0; | |
1041 } | |
1042 | |
1043 static const luaL_Reg luaV_Dict_mt[] = { | |
1044 {"__tostring", luaV_dict_tostring}, | |
1045 {"__len", luaV_dict_len}, | |
1046 {"__call", luaV_dict_call}, | |
1047 {"__index", luaV_dict_index}, | |
1048 {"__newindex", luaV_dict_newindex}, | |
1049 {NULL, NULL} | |
1050 }; | |
1051 | |
1052 | |
18798
f0f9692d4487
patch 8.1.2387: using old C style comments
Bram Moolenaar <Bram@vim.org>
parents:
18370
diff
changeset
|
1053 // ======= Blob type ======= |
16076
a2f0e93a5857
patch 8.1.1043: Lua interface does not support Blob
Bram Moolenaar <Bram@vim.org>
parents:
16028
diff
changeset
|
1054 |
a2f0e93a5857
patch 8.1.1043: Lua interface does not support Blob
Bram Moolenaar <Bram@vim.org>
parents:
16028
diff
changeset
|
1055 static luaV_Blob * |
a2f0e93a5857
patch 8.1.1043: Lua interface does not support Blob
Bram Moolenaar <Bram@vim.org>
parents:
16028
diff
changeset
|
1056 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
|
1057 { |
a2f0e93a5857
patch 8.1.1043: Lua interface does not support Blob
Bram Moolenaar <Bram@vim.org>
parents:
16028
diff
changeset
|
1058 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
|
1059 *b = blo; |
18798
f0f9692d4487
patch 8.1.2387: using old C style comments
Bram Moolenaar <Bram@vim.org>
parents:
18370
diff
changeset
|
1060 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
|
1061 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
|
1062 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
|
1063 lua_setmetatable(L, -2); |
a2f0e93a5857
patch 8.1.1043: Lua interface does not support Blob
Bram Moolenaar <Bram@vim.org>
parents:
16028
diff
changeset
|
1064 return b; |
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 |
a2f0e93a5857
patch 8.1.1043: Lua interface does not support Blob
Bram Moolenaar <Bram@vim.org>
parents:
16028
diff
changeset
|
1067 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
|
1068 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
|
1069 |
a2f0e93a5857
patch 8.1.1043: Lua interface does not support Blob
Bram Moolenaar <Bram@vim.org>
parents:
16028
diff
changeset
|
1070 static int |
a2f0e93a5857
patch 8.1.1043: Lua interface does not support Blob
Bram Moolenaar <Bram@vim.org>
parents:
16028
diff
changeset
|
1071 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
|
1072 { |
a2f0e93a5857
patch 8.1.1043: Lua interface does not support Blob
Bram Moolenaar <Bram@vim.org>
parents:
16028
diff
changeset
|
1073 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
|
1074 blob_unref(b); |
a2f0e93a5857
patch 8.1.1043: Lua interface does not support Blob
Bram Moolenaar <Bram@vim.org>
parents:
16028
diff
changeset
|
1075 return 0; |
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 |
a2f0e93a5857
patch 8.1.1043: Lua interface does not support Blob
Bram Moolenaar <Bram@vim.org>
parents:
16028
diff
changeset
|
1078 static int |
a2f0e93a5857
patch 8.1.1043: Lua interface does not support Blob
Bram Moolenaar <Bram@vim.org>
parents:
16028
diff
changeset
|
1079 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
|
1080 { |
a2f0e93a5857
patch 8.1.1043: Lua interface does not support Blob
Bram Moolenaar <Bram@vim.org>
parents:
16028
diff
changeset
|
1081 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
|
1082 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
|
1083 return 1; |
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 |
a2f0e93a5857
patch 8.1.1043: Lua interface does not support Blob
Bram Moolenaar <Bram@vim.org>
parents:
16028
diff
changeset
|
1086 static int |
a2f0e93a5857
patch 8.1.1043: Lua interface does not support Blob
Bram Moolenaar <Bram@vim.org>
parents:
16028
diff
changeset
|
1087 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
|
1088 { |
a2f0e93a5857
patch 8.1.1043: Lua interface does not support Blob
Bram Moolenaar <Bram@vim.org>
parents:
16028
diff
changeset
|
1089 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
|
1090 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
|
1091 { |
a2f0e93a5857
patch 8.1.1043: Lua interface does not support Blob
Bram Moolenaar <Bram@vim.org>
parents:
16028
diff
changeset
|
1092 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
|
1093 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
|
1094 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
|
1095 else |
a2f0e93a5857
patch 8.1.1043: Lua interface does not support Blob
Bram Moolenaar <Bram@vim.org>
parents:
16028
diff
changeset
|
1096 lua_pushnil(L); |
a2f0e93a5857
patch 8.1.1043: Lua interface does not support Blob
Bram Moolenaar <Bram@vim.org>
parents:
16028
diff
changeset
|
1097 } |
a2f0e93a5857
patch 8.1.1043: Lua interface does not support Blob
Bram Moolenaar <Bram@vim.org>
parents:
16028
diff
changeset
|
1098 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
|
1099 { |
a2f0e93a5857
patch 8.1.1043: Lua interface does not support Blob
Bram Moolenaar <Bram@vim.org>
parents:
16028
diff
changeset
|
1100 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
|
1101 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
|
1102 { |
a2f0e93a5857
patch 8.1.1043: Lua interface does not support Blob
Bram Moolenaar <Bram@vim.org>
parents:
16028
diff
changeset
|
1103 lua_getmetatable(L, 1); |
a2f0e93a5857
patch 8.1.1043: Lua interface does not support Blob
Bram Moolenaar <Bram@vim.org>
parents:
16028
diff
changeset
|
1104 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
|
1105 } |
a2f0e93a5857
patch 8.1.1043: Lua interface does not support Blob
Bram Moolenaar <Bram@vim.org>
parents:
16028
diff
changeset
|
1106 else |
a2f0e93a5857
patch 8.1.1043: Lua interface does not support Blob
Bram Moolenaar <Bram@vim.org>
parents:
16028
diff
changeset
|
1107 lua_pushnil(L); |
a2f0e93a5857
patch 8.1.1043: Lua interface does not support Blob
Bram Moolenaar <Bram@vim.org>
parents:
16028
diff
changeset
|
1108 } |
a2f0e93a5857
patch 8.1.1043: Lua interface does not support Blob
Bram Moolenaar <Bram@vim.org>
parents:
16028
diff
changeset
|
1109 else |
a2f0e93a5857
patch 8.1.1043: Lua interface does not support Blob
Bram Moolenaar <Bram@vim.org>
parents:
16028
diff
changeset
|
1110 lua_pushnil(L); |
a2f0e93a5857
patch 8.1.1043: Lua interface does not support Blob
Bram Moolenaar <Bram@vim.org>
parents:
16028
diff
changeset
|
1111 return 1; |
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 |
a2f0e93a5857
patch 8.1.1043: Lua interface does not support Blob
Bram Moolenaar <Bram@vim.org>
parents:
16028
diff
changeset
|
1114 static int |
a2f0e93a5857
patch 8.1.1043: Lua interface does not support Blob
Bram Moolenaar <Bram@vim.org>
parents:
16028
diff
changeset
|
1115 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
|
1116 { |
a2f0e93a5857
patch 8.1.1043: Lua interface does not support Blob
Bram Moolenaar <Bram@vim.org>
parents:
16028
diff
changeset
|
1117 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
|
1118 if (b->bv_lock) |
a2f0e93a5857
patch 8.1.1043: Lua interface does not support Blob
Bram Moolenaar <Bram@vim.org>
parents:
16028
diff
changeset
|
1119 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
|
1120 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
|
1121 { |
a2f0e93a5857
patch 8.1.1043: Lua interface does not support Blob
Bram Moolenaar <Bram@vim.org>
parents:
16028
diff
changeset
|
1122 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
|
1123 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
|
1124 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
|
1125 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
|
1126 { |
a2f0e93a5857
patch 8.1.1043: Lua interface does not support Blob
Bram Moolenaar <Bram@vim.org>
parents:
16028
diff
changeset
|
1127 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
|
1128 if (idx == len) |
a2f0e93a5857
patch 8.1.1043: Lua interface does not support Blob
Bram Moolenaar <Bram@vim.org>
parents:
16028
diff
changeset
|
1129 ++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
|
1130 } |
a2f0e93a5857
patch 8.1.1043: Lua interface does not support Blob
Bram Moolenaar <Bram@vim.org>
parents:
16028
diff
changeset
|
1131 else |
a2f0e93a5857
patch 8.1.1043: Lua interface does not support Blob
Bram Moolenaar <Bram@vim.org>
parents:
16028
diff
changeset
|
1132 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
|
1133 } |
a2f0e93a5857
patch 8.1.1043: Lua interface does not support Blob
Bram Moolenaar <Bram@vim.org>
parents:
16028
diff
changeset
|
1134 return 0; |
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 |
a2f0e93a5857
patch 8.1.1043: Lua interface does not support Blob
Bram Moolenaar <Bram@vim.org>
parents:
16028
diff
changeset
|
1137 static int |
a2f0e93a5857
patch 8.1.1043: Lua interface does not support Blob
Bram Moolenaar <Bram@vim.org>
parents:
16028
diff
changeset
|
1138 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
|
1139 { |
a2f0e93a5857
patch 8.1.1043: Lua interface does not support Blob
Bram Moolenaar <Bram@vim.org>
parents:
16028
diff
changeset
|
1140 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
|
1141 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
|
1142 if (b->bv_lock) |
a2f0e93a5857
patch 8.1.1043: Lua interface does not support Blob
Bram Moolenaar <Bram@vim.org>
parents:
16028
diff
changeset
|
1143 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
|
1144 lua_settop(L, 2); |
a2f0e93a5857
patch 8.1.1043: Lua interface does not support Blob
Bram Moolenaar <Bram@vim.org>
parents:
16028
diff
changeset
|
1145 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
|
1146 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
|
1147 else |
a2f0e93a5857
patch 8.1.1043: Lua interface does not support Blob
Bram Moolenaar <Bram@vim.org>
parents:
16028
diff
changeset
|
1148 { |
a2f0e93a5857
patch 8.1.1043: Lua interface does not support Blob
Bram Moolenaar <Bram@vim.org>
parents:
16028
diff
changeset
|
1149 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
|
1150 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
|
1151 |
16099
643fe07aa0e2
patch 8.1.1054: not checking return value of ga_grow()
Bram Moolenaar <Bram@vim.org>
parents:
16076
diff
changeset
|
1152 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
|
1153 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
|
1154 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
|
1155 } |
a2f0e93a5857
patch 8.1.1043: Lua interface does not support Blob
Bram Moolenaar <Bram@vim.org>
parents:
16028
diff
changeset
|
1156 lua_settop(L, 1); |
a2f0e93a5857
patch 8.1.1043: Lua interface does not support Blob
Bram Moolenaar <Bram@vim.org>
parents:
16028
diff
changeset
|
1157 return 1; |
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 |
a2f0e93a5857
patch 8.1.1043: Lua interface does not support Blob
Bram Moolenaar <Bram@vim.org>
parents:
16028
diff
changeset
|
1160 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
|
1161 {"__tostring", luaV_blob_tostring}, |
a2f0e93a5857
patch 8.1.1043: Lua interface does not support Blob
Bram Moolenaar <Bram@vim.org>
parents:
16028
diff
changeset
|
1162 {"__gc", luaV_blob_gc}, |
a2f0e93a5857
patch 8.1.1043: Lua interface does not support Blob
Bram Moolenaar <Bram@vim.org>
parents:
16028
diff
changeset
|
1163 {"__len", luaV_blob_len}, |
a2f0e93a5857
patch 8.1.1043: Lua interface does not support Blob
Bram Moolenaar <Bram@vim.org>
parents:
16028
diff
changeset
|
1164 {"__index", luaV_blob_index}, |
a2f0e93a5857
patch 8.1.1043: Lua interface does not support Blob
Bram Moolenaar <Bram@vim.org>
parents:
16028
diff
changeset
|
1165 {"__newindex", luaV_blob_newindex}, |
a2f0e93a5857
patch 8.1.1043: Lua interface does not support Blob
Bram Moolenaar <Bram@vim.org>
parents:
16028
diff
changeset
|
1166 {"add", luaV_blob_add}, |
a2f0e93a5857
patch 8.1.1043: Lua interface does not support Blob
Bram Moolenaar <Bram@vim.org>
parents:
16028
diff
changeset
|
1167 {NULL, NULL} |
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 |
a2f0e93a5857
patch 8.1.1043: Lua interface does not support Blob
Bram Moolenaar <Bram@vim.org>
parents:
16028
diff
changeset
|
1170 |
18798
f0f9692d4487
patch 8.1.2387: using old C style comments
Bram Moolenaar <Bram@vim.org>
parents:
18370
diff
changeset
|
1171 // ======= Funcref type ======= |
14235
e3bc8cdc94dd
patch 8.1.0134: Lua interface does not support funcref
Christian Brabandt <cb@256bit.org>
parents:
12515
diff
changeset
|
1172 |
e3bc8cdc94dd
patch 8.1.0134: Lua interface does not support funcref
Christian Brabandt <cb@256bit.org>
parents:
12515
diff
changeset
|
1173 static luaV_Funcref * |
e3bc8cdc94dd
patch 8.1.0134: Lua interface does not support funcref
Christian Brabandt <cb@256bit.org>
parents:
12515
diff
changeset
|
1174 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
|
1175 { |
e3bc8cdc94dd
patch 8.1.0134: Lua interface does not support funcref
Christian Brabandt <cb@256bit.org>
parents:
12515
diff
changeset
|
1176 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
|
1177 |
e3bc8cdc94dd
patch 8.1.0134: Lua interface does not support funcref
Christian Brabandt <cb@256bit.org>
parents:
12515
diff
changeset
|
1178 if (name != NULL) |
e3bc8cdc94dd
patch 8.1.0134: Lua interface does not support funcref
Christian Brabandt <cb@256bit.org>
parents:
12515
diff
changeset
|
1179 { |
16028
8ad2cda3757d
patch 8.1.1019: Lua: may garbage collect function reference in use
Bram Moolenaar <Bram@vim.org>
parents:
16009
diff
changeset
|
1180 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
|
1181 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
|
1182 } |
e3bc8cdc94dd
patch 8.1.0134: Lua interface does not support funcref
Christian Brabandt <cb@256bit.org>
parents:
12515
diff
changeset
|
1183 f->self = NULL; |
e3bc8cdc94dd
patch 8.1.0134: Lua interface does not support funcref
Christian Brabandt <cb@256bit.org>
parents:
12515
diff
changeset
|
1184 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
|
1185 lua_setmetatable(L, -2); |
e3bc8cdc94dd
patch 8.1.0134: Lua interface does not support funcref
Christian Brabandt <cb@256bit.org>
parents:
12515
diff
changeset
|
1186 return f; |
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 |
e3bc8cdc94dd
patch 8.1.0134: Lua interface does not support funcref
Christian Brabandt <cb@256bit.org>
parents:
12515
diff
changeset
|
1189 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
|
1190 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
|
1191 { |
16028
8ad2cda3757d
patch 8.1.1019: Lua: may garbage collect function reference in use
Bram Moolenaar <Bram@vim.org>
parents:
16009
diff
changeset
|
1192 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
|
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 |
e3bc8cdc94dd
patch 8.1.0134: Lua interface does not support funcref
Christian Brabandt <cb@256bit.org>
parents:
12515
diff
changeset
|
1196 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
|
1197 |
e3bc8cdc94dd
patch 8.1.0134: Lua interface does not support funcref
Christian Brabandt <cb@256bit.org>
parents:
12515
diff
changeset
|
1198 static int |
e3bc8cdc94dd
patch 8.1.0134: Lua interface does not support funcref
Christian Brabandt <cb@256bit.org>
parents:
12515
diff
changeset
|
1199 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
|
1200 { |
e3bc8cdc94dd
patch 8.1.0134: Lua interface does not support funcref
Christian Brabandt <cb@256bit.org>
parents:
12515
diff
changeset
|
1201 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
|
1202 |
16028
8ad2cda3757d
patch 8.1.1019: Lua: may garbage collect function reference in use
Bram Moolenaar <Bram@vim.org>
parents:
16009
diff
changeset
|
1203 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
|
1204 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
|
1205 // 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
|
1206 // 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
|
1207 return 0; |
e3bc8cdc94dd
patch 8.1.0134: Lua interface does not support funcref
Christian Brabandt <cb@256bit.org>
parents:
12515
diff
changeset
|
1208 } |
e3bc8cdc94dd
patch 8.1.0134: Lua interface does not support funcref
Christian Brabandt <cb@256bit.org>
parents:
12515
diff
changeset
|
1209 |
18798
f0f9692d4487
patch 8.1.2387: using old C style comments
Bram Moolenaar <Bram@vim.org>
parents:
18370
diff
changeset
|
1210 // 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
|
1211 static int |
e3bc8cdc94dd
patch 8.1.0134: Lua interface does not support funcref
Christian Brabandt <cb@256bit.org>
parents:
12515
diff
changeset
|
1212 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
|
1213 { |
e3bc8cdc94dd
patch 8.1.0134: Lua interface does not support funcref
Christian Brabandt <cb@256bit.org>
parents:
12515
diff
changeset
|
1214 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
|
1215 |
16028
8ad2cda3757d
patch 8.1.1019: Lua: may garbage collect function reference in use
Bram Moolenaar <Bram@vim.org>
parents:
16009
diff
changeset
|
1216 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
|
1217 return 1; |
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 |
e3bc8cdc94dd
patch 8.1.0134: Lua interface does not support funcref
Christian Brabandt <cb@256bit.org>
parents:
12515
diff
changeset
|
1220 static int |
e3bc8cdc94dd
patch 8.1.0134: Lua interface does not support funcref
Christian Brabandt <cb@256bit.org>
parents:
12515
diff
changeset
|
1221 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
|
1222 { |
e3bc8cdc94dd
patch 8.1.0134: Lua interface does not support funcref
Christian Brabandt <cb@256bit.org>
parents:
12515
diff
changeset
|
1223 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
|
1224 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
|
1225 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
|
1226 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
|
1227 typval_T rettv; |
14235
e3bc8cdc94dd
patch 8.1.0134: Lua interface does not support funcref
Christian Brabandt <cb@256bit.org>
parents:
12515
diff
changeset
|
1228 |
16028
8ad2cda3757d
patch 8.1.1019: Lua: may garbage collect function reference in use
Bram Moolenaar <Bram@vim.org>
parents:
16009
diff
changeset
|
1229 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
|
1230 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
|
1231 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
|
1232 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
|
1233 { |
16028
8ad2cda3757d
patch 8.1.1019: Lua: may garbage collect function reference in use
Bram Moolenaar <Bram@vim.org>
parents:
16009
diff
changeset
|
1234 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
|
1235 |
14335
d59bf91128ea
patch 8.1.0183: Lua API changed, breaking the build
Christian Brabandt <cb@256bit.org>
parents:
14329
diff
changeset
|
1236 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
|
1237 { |
14329
fe651f98e173
patch 8.1.0180: static analysis errors in Lua interface
Christian Brabandt <cb@256bit.org>
parents:
14296
diff
changeset
|
1238 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
|
1239 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
|
1240 clear_tv(&v); |
14329
fe651f98e173
patch 8.1.0180: static analysis errors in Lua interface
Christian Brabandt <cb@256bit.org>
parents:
14296
diff
changeset
|
1241 } |
16028
8ad2cda3757d
patch 8.1.1019: Lua: may garbage collect function reference in use
Bram Moolenaar <Bram@vim.org>
parents:
16009
diff
changeset
|
1242 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
|
1243 if (status == OK) |
fe651f98e173
patch 8.1.0180: static analysis errors in Lua interface
Christian Brabandt <cb@256bit.org>
parents:
14296
diff
changeset
|
1244 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
|
1245 clear_tv(&args); |
14329
fe651f98e173
patch 8.1.0180: static analysis errors in Lua interface
Christian Brabandt <cb@256bit.org>
parents:
14296
diff
changeset
|
1246 clear_tv(&rettv); |
14235
e3bc8cdc94dd
patch 8.1.0134: Lua interface does not support funcref
Christian Brabandt <cb@256bit.org>
parents:
12515
diff
changeset
|
1247 } |
e3bc8cdc94dd
patch 8.1.0134: Lua interface does not support funcref
Christian Brabandt <cb@256bit.org>
parents:
12515
diff
changeset
|
1248 if (status != OK) |
e3bc8cdc94dd
patch 8.1.0134: Lua interface does not support funcref
Christian Brabandt <cb@256bit.org>
parents:
12515
diff
changeset
|
1249 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
|
1250 return 1; |
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 |
e3bc8cdc94dd
patch 8.1.0134: Lua interface does not support funcref
Christian Brabandt <cb@256bit.org>
parents:
12515
diff
changeset
|
1253 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
|
1254 {"__tostring", luaV_funcref_tostring}, |
e3bc8cdc94dd
patch 8.1.0134: Lua interface does not support funcref
Christian Brabandt <cb@256bit.org>
parents:
12515
diff
changeset
|
1255 {"__gc", luaV_funcref_gc}, |
e3bc8cdc94dd
patch 8.1.0134: Lua interface does not support funcref
Christian Brabandt <cb@256bit.org>
parents:
12515
diff
changeset
|
1256 {"__len", luaV_funcref_len}, |
e3bc8cdc94dd
patch 8.1.0134: Lua interface does not support funcref
Christian Brabandt <cb@256bit.org>
parents:
12515
diff
changeset
|
1257 {"__call", luaV_funcref_call}, |
e3bc8cdc94dd
patch 8.1.0134: Lua interface does not support funcref
Christian Brabandt <cb@256bit.org>
parents:
12515
diff
changeset
|
1258 {NULL, NULL} |
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 |
e3bc8cdc94dd
patch 8.1.0134: Lua interface does not support funcref
Christian Brabandt <cb@256bit.org>
parents:
12515
diff
changeset
|
1261 |
18798
f0f9692d4487
patch 8.1.2387: using old C style comments
Bram Moolenaar <Bram@vim.org>
parents:
18370
diff
changeset
|
1262 // ======= Buffer type ======= |
3450 | 1263 |
1264 luaV_newtype(buf_T, buffer, luaV_Buffer, LUAVIM_BUFFER) | |
1265 luaV_pushtype(buf_T, buffer, luaV_Buffer) | |
1266 luaV_type_tostring(buffer, LUAVIM_BUFFER) | |
1267 | |
1268 static int | |
2330 | 1269 luaV_buffer_len(lua_State *L) |
1270 { | |
3450 | 1271 buf_T *b = (buf_T *) luaV_checkvalid(L, luaV_Buffer, 1); |
1272 lua_pushinteger(L, b->b_ml.ml_line_count); | |
2320
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
1273 return 1; |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
1274 } |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
1275 |
2330 | 1276 static int |
1277 luaV_buffer_call(lua_State *L) | |
1278 { | |
3450 | 1279 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
|
1280 lua_settop(L, 1); |
3450 | 1281 set_curbuf(b, DOBUF_SPLIT); |
2320
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
1282 return 1; |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
1283 } |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
1284 |
2330 | 1285 static int |
1286 luaV_buffer_index(lua_State *L) | |
1287 { | |
3450 | 1288 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
|
1289 linenr_T n = (linenr_T) lua_tointeger(L, 2); |
3450 | 1290 if (n > 0 && n <= b->b_ml.ml_line_count) |
1291 luaV_pushline(L, b, n); | |
2330 | 1292 else if (lua_isstring(L, 2)) |
1293 { | |
1294 const char *s = lua_tostring(L, 2); | |
1295 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
|
1296 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
|
1297 ? "" : (char *) b->b_sfname); |
2330 | 1298 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
|
1299 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
|
1300 ? "" : (char *) b->b_ffname); |
2330 | 1301 else if (strncmp(s, "number", 6) == 0) |
3450 | 1302 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
|
1303 // methods |
2330 | 1304 else if (strncmp(s, "insert", 6) == 0 |
1305 || strncmp(s, "next", 4) == 0 | |
1306 || strncmp(s, "previous", 8) == 0 | |
1307 || strncmp(s, "isvalid", 7) == 0) | |
1308 { | |
1309 lua_getmetatable(L, 1); | |
1310 lua_getfield(L, -1, s); | |
1311 } | |
1312 else | |
1313 lua_pushnil(L); | |
2320
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
1314 } |
2330 | 1315 else |
1316 lua_pushnil(L); | |
2320
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
1317 return 1; |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
1318 } |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
1319 |
2330 | 1320 static int |
1321 luaV_buffer_newindex(lua_State *L) | |
1322 { | |
3450 | 1323 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
|
1324 linenr_T n = (linenr_T) luaL_checkinteger(L, 2); |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
1325 #ifdef HAVE_SANDBOX |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
1326 luaV_checksandbox(L); |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
1327 #endif |
3450 | 1328 if (n < 1 || n > b->b_ml.ml_line_count) |
2330 | 1329 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
|
1330 if (lua_isnil(L, 3)) // delete line |
2330 | 1331 { |
1332 buf_T *buf = curbuf; | |
3450 | 1333 curbuf = b; |
2330 | 1334 if (u_savedel(n, 1L) == FAIL) |
1335 { | |
1336 curbuf = buf; | |
1337 luaL_error(L, "cannot save undo information"); | |
1338 } | |
1339 else if (ml_delete(n, FALSE) == FAIL) | |
1340 { | |
1341 curbuf = buf; | |
1342 luaL_error(L, "cannot delete line"); | |
1343 } | |
14235
e3bc8cdc94dd
patch 8.1.0134: Lua interface does not support funcref
Christian Brabandt <cb@256bit.org>
parents:
12515
diff
changeset
|
1344 else |
e3bc8cdc94dd
patch 8.1.0134: Lua interface does not support funcref
Christian Brabandt <cb@256bit.org>
parents:
12515
diff
changeset
|
1345 { |
2330 | 1346 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
|
1347 if (b == curwin->w_buffer) // fix cursor in current window? |
2330 | 1348 { |
1349 if (curwin->w_cursor.lnum >= n) | |
1350 { | |
1351 if (curwin->w_cursor.lnum > n) | |
1352 { | |
1353 curwin->w_cursor.lnum -= 1; | |
1354 check_cursor_col(); | |
1355 } | |
1356 else check_cursor(); | |
1357 changed_cline_bef_curs(); | |
1358 } | |
1359 invalidate_botline(); | |
1360 } | |
1361 } | |
1362 curbuf = buf; | |
2320
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
1363 } |
18798
f0f9692d4487
patch 8.1.2387: using old C style comments
Bram Moolenaar <Bram@vim.org>
parents:
18370
diff
changeset
|
1364 else if (lua_isstring(L, 3)) // update line |
2330 | 1365 { |
1366 buf_T *buf = curbuf; | |
3450 | 1367 curbuf = b; |
2330 | 1368 if (u_savesub(n) == FAIL) |
1369 { | |
1370 curbuf = buf; | |
1371 luaL_error(L, "cannot save undo information"); | |
1372 } | |
1373 else if (ml_replace(n, luaV_toline(L, 3), TRUE) == FAIL) | |
1374 { | |
1375 curbuf = buf; | |
1376 luaL_error(L, "cannot replace line"); | |
1377 } | |
1378 else changed_bytes(n, 0); | |
1379 curbuf = buf; | |
3450 | 1380 if (b == curwin->w_buffer) |
2330 | 1381 check_cursor_col(); |
2320
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
1382 } |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
1383 else |
2330 | 1384 luaL_error(L, "wrong argument to change line"); |
2320
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
1385 return 0; |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
1386 } |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
1387 |
2330 | 1388 static int |
1389 luaV_buffer_insert(lua_State *L) | |
1390 { | |
3450 | 1391 luaV_Buffer *lb = luaV_checkudata(L, 1, LUAVIM_BUFFER); |
1392 buf_T *b = (buf_T *) luaV_checkcache(L, (void *) *lb); | |
1393 linenr_T last = b->b_ml.ml_line_count; | |
2320
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
1394 linenr_T n = (linenr_T) luaL_optinteger(L, 3, last); |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
1395 buf_T *buf; |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
1396 luaL_checktype(L, 2, LUA_TSTRING); |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
1397 #ifdef HAVE_SANDBOX |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
1398 luaV_checksandbox(L); |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
1399 #endif |
18798
f0f9692d4487
patch 8.1.2387: using old C style comments
Bram Moolenaar <Bram@vim.org>
parents:
18370
diff
changeset
|
1400 // fix insertion line |
2320
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
1401 if (n < 0) n = 0; |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
1402 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
|
1403 // insert |
2320
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
1404 buf = curbuf; |
3450 | 1405 curbuf = b; |
2330 | 1406 if (u_save(n, n + 1) == FAIL) |
1407 { | |
1408 curbuf = buf; | |
1409 luaL_error(L, "cannot save undo information"); | |
2320
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
1410 } |
2330 | 1411 else if (ml_append(n, luaV_toline(L, 2), 0, FALSE) == FAIL) |
1412 { | |
1413 curbuf = buf; | |
1414 luaL_error(L, "cannot insert line"); | |
2320
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
1415 } |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
1416 else |
2330 | 1417 appended_lines_mark(n, 1L); |
2320
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
1418 curbuf = buf; |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
1419 update_screen(VALID); |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
1420 return 0; |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
1421 } |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
1422 |
2330 | 1423 static int |
1424 luaV_buffer_next(lua_State *L) | |
1425 { | |
2320
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
1426 luaV_Buffer *b = luaV_checkudata(L, 1, LUAVIM_BUFFER); |
3450 | 1427 buf_T *buf = (buf_T *) luaV_checkcache(L, (void *) *b); |
1428 luaV_pushbuffer(L, buf->b_next); | |
2320
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
1429 return 1; |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
1430 } |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
1431 |
2330 | 1432 static int |
1433 luaV_buffer_previous(lua_State *L) | |
1434 { | |
2320
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
1435 luaV_Buffer *b = luaV_checkudata(L, 1, LUAVIM_BUFFER); |
3450 | 1436 buf_T *buf = (buf_T *) luaV_checkcache(L, (void *) *b); |
1437 luaV_pushbuffer(L, buf->b_prev); | |
2320
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
1438 return 1; |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
1439 } |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
1440 |
2330 | 1441 static int |
1442 luaV_buffer_isvalid(lua_State *L) | |
1443 { | |
2320
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
1444 luaV_Buffer *b = luaV_checkudata(L, 1, LUAVIM_BUFFER); |
3450 | 1445 luaV_getudata(L, *b); |
2320
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
1446 lua_pushboolean(L, !lua_isnil(L, -1)); |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
1447 return 1; |
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 |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
1450 static const luaL_Reg luaV_Buffer_mt[] = { |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
1451 {"__tostring", luaV_buffer_tostring}, |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
1452 {"__len", luaV_buffer_len}, |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
1453 {"__call", luaV_buffer_call}, |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
1454 {"__index", luaV_buffer_index}, |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
1455 {"__newindex", luaV_buffer_newindex}, |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
1456 {"insert", luaV_buffer_insert}, |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
1457 {"next", luaV_buffer_next}, |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
1458 {"previous", luaV_buffer_previous}, |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
1459 {"isvalid", luaV_buffer_isvalid}, |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
1460 {NULL, NULL} |
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 |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
1463 |
18798
f0f9692d4487
patch 8.1.2387: using old C style comments
Bram Moolenaar <Bram@vim.org>
parents:
18370
diff
changeset
|
1464 // ======= Window type ======= |
2320
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
1465 |
3450 | 1466 luaV_newtype(win_T, window, luaV_Window, LUAVIM_WINDOW) |
1467 luaV_pushtype(win_T, window, luaV_Window) | |
1468 luaV_type_tostring(window, LUAVIM_WINDOW) | |
2320
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
1469 |
2330 | 1470 static int |
1471 luaV_window_call(lua_State *L) | |
1472 { | |
3450 | 1473 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
|
1474 lua_settop(L, 1); |
3450 | 1475 win_goto(w); |
2320
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
1476 return 1; |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
1477 } |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
1478 |
2330 | 1479 static int |
1480 luaV_window_index(lua_State *L) | |
1481 { | |
3450 | 1482 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
|
1483 const char *s = luaL_checkstring(L, 2); |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
1484 if (strncmp(s, "buffer", 6) == 0) |
3450 | 1485 luaV_pushbuffer(L, w->w_buffer); |
2320
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
1486 else if (strncmp(s, "line", 4) == 0) |
3450 | 1487 lua_pushinteger(L, w->w_cursor.lnum); |
2320
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
1488 else if (strncmp(s, "col", 3) == 0) |
3450 | 1489 lua_pushinteger(L, w->w_cursor.col + 1); |
2320
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
1490 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
|
1491 lua_pushinteger(L, w->w_width); |
2320
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
1492 else if (strncmp(s, "height", 6) == 0) |
3450 | 1493 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
|
1494 // methods |
2320
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
1495 else if (strncmp(s, "next", 4) == 0 |
2330 | 1496 || strncmp(s, "previous", 8) == 0 |
1497 || strncmp(s, "isvalid", 7) == 0) | |
1498 { | |
1499 lua_getmetatable(L, 1); | |
1500 lua_getfield(L, -1, s); | |
2320
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
1501 } |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
1502 else |
2330 | 1503 lua_pushnil(L); |
2320
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
1504 return 1; |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
1505 } |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
1506 |
2330 | 1507 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
|
1508 luaV_window_newindex(lua_State *L) |
2330 | 1509 { |
3450 | 1510 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
|
1511 const char *s = luaL_checkstring(L, 2); |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
1512 int v = luaL_checkinteger(L, 3); |
2330 | 1513 if (strncmp(s, "line", 4) == 0) |
1514 { | |
2320
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
1515 #ifdef HAVE_SANDBOX |
2330 | 1516 luaV_checksandbox(L); |
2320
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
1517 #endif |
3450 | 1518 if (v < 1 || v > w->w_buffer->b_ml.ml_line_count) |
2330 | 1519 luaL_error(L, "line out of range"); |
3450 | 1520 w->w_cursor.lnum = v; |
2330 | 1521 update_screen(VALID); |
2320
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
1522 } |
2330 | 1523 else if (strncmp(s, "col", 3) == 0) |
1524 { | |
2320
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
1525 #ifdef HAVE_SANDBOX |
2330 | 1526 luaV_checksandbox(L); |
2320
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
1527 #endif |
3450 | 1528 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
|
1529 w->w_set_curswant = TRUE; |
2330 | 1530 update_screen(VALID); |
2320
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
1531 } |
2330 | 1532 else if (strncmp(s, "width", 5) == 0) |
1533 { | |
1534 win_T *win = curwin; | |
2320
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
1535 #ifdef FEAT_GUI |
2330 | 1536 need_mouse_correct = TRUE; |
2320
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
1537 #endif |
3450 | 1538 curwin = w; |
2330 | 1539 win_setwidth(v); |
1540 curwin = win; | |
2320
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
1541 } |
2330 | 1542 else if (strncmp(s, "height", 6) == 0) |
1543 { | |
1544 win_T *win = curwin; | |
2320
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
1545 #ifdef FEAT_GUI |
2330 | 1546 need_mouse_correct = TRUE; |
2320
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
1547 #endif |
3450 | 1548 curwin = w; |
2330 | 1549 win_setheight(v); |
1550 curwin = win; | |
2320
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
1551 } |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
1552 else |
2330 | 1553 luaL_error(L, "invalid window property: `%s'", s); |
2320
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
1554 return 0; |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
1555 } |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
1556 |
2330 | 1557 static int |
1558 luaV_window_next(lua_State *L) | |
1559 { | |
2320
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
1560 luaV_Window *w = luaV_checkudata(L, 1, LUAVIM_WINDOW); |
3450 | 1561 win_T *win = (win_T *) luaV_checkcache(L, (void *) *w); |
1562 luaV_pushwindow(L, win->w_next); | |
2320
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
1563 return 1; |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
1564 } |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
1565 |
2330 | 1566 static int |
1567 luaV_window_previous(lua_State *L) | |
1568 { | |
2320
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
1569 luaV_Window *w = luaV_checkudata(L, 1, LUAVIM_WINDOW); |
3450 | 1570 win_T *win = (win_T *) luaV_checkcache(L, (void *) *w); |
1571 luaV_pushwindow(L, win->w_prev); | |
2320
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
1572 return 1; |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
1573 } |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
1574 |
2330 | 1575 static int |
1576 luaV_window_isvalid(lua_State *L) | |
1577 { | |
2320
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
1578 luaV_Window *w = luaV_checkudata(L, 1, LUAVIM_WINDOW); |
3450 | 1579 luaV_getudata(L, *w); |
2320
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
1580 lua_pushboolean(L, !lua_isnil(L, -1)); |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
1581 return 1; |
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 |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
1584 static const luaL_Reg luaV_Window_mt[] = { |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
1585 {"__tostring", luaV_window_tostring}, |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
1586 {"__call", luaV_window_call}, |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
1587 {"__index", luaV_window_index}, |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
1588 {"__newindex", luaV_window_newindex}, |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
1589 {"next", luaV_window_next}, |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
1590 {"previous", luaV_window_previous}, |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
1591 {"isvalid", luaV_window_isvalid}, |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
1592 {NULL, NULL} |
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 |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
1595 |
18798
f0f9692d4487
patch 8.1.2387: using old C style comments
Bram Moolenaar <Bram@vim.org>
parents:
18370
diff
changeset
|
1596 // ======= Vim module ======= |
2320
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
1597 |
2330 | 1598 static int |
1599 luaV_print(lua_State *L) | |
1600 { | |
18798
f0f9692d4487
patch 8.1.2387: using old C style comments
Bram Moolenaar <Bram@vim.org>
parents:
18370
diff
changeset
|
1601 int i, n = lua_gettop(L); // nargs |
2320
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
1602 const char *s; |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
1603 size_t l; |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
1604 luaL_Buffer b; |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
1605 luaL_buffinit(L, &b); |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
1606 lua_getglobal(L, "tostring"); |
2330 | 1607 for (i = 1; i <= n; i++) |
1608 { | |
18798
f0f9692d4487
patch 8.1.2387: using old C style comments
Bram Moolenaar <Bram@vim.org>
parents:
18370
diff
changeset
|
1609 lua_pushvalue(L, -1); // tostring |
f0f9692d4487
patch 8.1.2387: using old C style comments
Bram Moolenaar <Bram@vim.org>
parents:
18370
diff
changeset
|
1610 lua_pushvalue(L, i); // arg |
2330 | 1611 lua_call(L, 1, 1); |
1612 s = lua_tolstring(L, -1, &l); | |
1613 if (s == NULL) | |
1614 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
|
1615 if (i > 1) luaL_addchar(&b, ' '); // use space instead of tab |
2330 | 1616 luaV_addlstring(&b, s, l, 0); |
1617 lua_pop(L, 1); | |
2320
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
1618 } |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
1619 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
|
1620 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
|
1621 luaV_msg(L); |
2320
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
1622 return 0; |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
1623 } |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
1624 |
2330 | 1625 static int |
3091 | 1626 luaV_debug(lua_State *L) |
1627 { | |
1628 lua_settop(L, 0); | |
1629 lua_getglobal(L, "vim"); | |
1630 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
|
1631 lua_remove(L, -2); // vim.eval at position 1 |
3091 | 1632 for (;;) |
1633 { | |
1634 const char *input; | |
1635 size_t l; | |
18798
f0f9692d4487
patch 8.1.2387: using old C style comments
Bram Moolenaar <Bram@vim.org>
parents:
18370
diff
changeset
|
1636 lua_pushvalue(L, 1); // vim.eval |
3091 | 1637 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
|
1638 lua_call(L, 1, 1); // return string |
3091 | 1639 input = lua_tolstring(L, -1, &l); |
1640 if (l == 0 || strcmp(input, "cont") == 0) | |
1641 return 0; | |
18798
f0f9692d4487
patch 8.1.2387: using old C style comments
Bram Moolenaar <Bram@vim.org>
parents:
18370
diff
changeset
|
1642 msg_putchar('\n'); // avoid outputting on input line |
3091 | 1643 if (luaL_loadbuffer(L, input, l, "=(debug command)") |
1644 || lua_pcall(L, 0, 0, 0)) | |
1645 luaV_emsg(L); | |
18798
f0f9692d4487
patch 8.1.2387: using old C style comments
Bram Moolenaar <Bram@vim.org>
parents:
18370
diff
changeset
|
1646 lua_settop(L, 1); // remove eventual returns, but keep vim.eval |
3091 | 1647 } |
1648 } | |
1649 | |
1650 static int | |
2330 | 1651 luaV_command(lua_State *L) |
1652 { | |
2320
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
1653 do_cmdline_cmd((char_u *) luaL_checkstring(L, 1)); |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
1654 update_screen(VALID); |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
1655 return 0; |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
1656 } |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
1657 |
2330 | 1658 static int |
1659 luaV_eval(lua_State *L) | |
1660 { | |
2320
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
1661 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
|
1662 if (tv == NULL) luaL_error(L, "invalid expression"); |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
1663 luaV_pushtypval(L, tv); |
4293 | 1664 free_tv(tv); |
2320
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
1665 return 1; |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
1666 } |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
1667 |
2330 | 1668 static int |
2331
3840b7508835
Make it easier to build with Lua. Remove compiler warnings.
Bram Moolenaar <bram@vim.org>
parents:
2330
diff
changeset
|
1669 luaV_beep(lua_State *L UNUSED) |
2330 | 1670 { |
6949 | 1671 vim_beep(BO_LANG); |
2320
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
1672 return 0; |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
1673 } |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
1674 |
2330 | 1675 static int |
1676 luaV_line(lua_State *L) | |
1677 { | |
2320
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
1678 luaV_pushline(L, curbuf, curwin->w_cursor.lnum); |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
1679 return 1; |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
1680 } |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
1681 |
2330 | 1682 static int |
3450 | 1683 luaV_list(lua_State *L) |
1684 { | |
14235
e3bc8cdc94dd
patch 8.1.0134: Lua interface does not support funcref
Christian Brabandt <cb@256bit.org>
parents:
12515
diff
changeset
|
1685 list_T *l; |
e3bc8cdc94dd
patch 8.1.0134: Lua interface does not support funcref
Christian Brabandt <cb@256bit.org>
parents:
12515
diff
changeset
|
1686 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
|
1687 |
e3bc8cdc94dd
patch 8.1.0134: Lua interface does not support funcref
Christian Brabandt <cb@256bit.org>
parents:
12515
diff
changeset
|
1688 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
|
1689 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
|
1690 l = list_alloc(); |
3450 | 1691 if (l == NULL) |
1692 lua_pushnil(L); | |
1693 else | |
14235
e3bc8cdc94dd
patch 8.1.0134: Lua interface does not support funcref
Christian Brabandt <cb@256bit.org>
parents:
12515
diff
changeset
|
1694 { |
3450 | 1695 luaV_newlist(L, l); |
18798
f0f9692d4487
patch 8.1.2387: using old C style comments
Bram Moolenaar <Bram@vim.org>
parents:
18370
diff
changeset
|
1696 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
|
1697 { |
14235
e3bc8cdc94dd
patch 8.1.0134: Lua interface does not support funcref
Christian Brabandt <cb@256bit.org>
parents:
12515
diff
changeset
|
1698 int notnil, i = 0; |
e3bc8cdc94dd
patch 8.1.0134: Lua interface does not support funcref
Christian Brabandt <cb@256bit.org>
parents:
12515
diff
changeset
|
1699 typval_T v; |
14335
d59bf91128ea
patch 8.1.0183: Lua API changed, breaking the build
Christian Brabandt <cb@256bit.org>
parents:
14329
diff
changeset
|
1700 do |
d59bf91128ea
patch 8.1.0183: Lua API changed, breaking the build
Christian Brabandt <cb@256bit.org>
parents:
14329
diff
changeset
|
1701 { |
14235
e3bc8cdc94dd
patch 8.1.0134: Lua interface does not support funcref
Christian Brabandt <cb@256bit.org>
parents:
12515
diff
changeset
|
1702 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
|
1703 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
|
1704 if (notnil) |
d59bf91128ea
patch 8.1.0183: Lua API changed, breaking the build
Christian Brabandt <cb@256bit.org>
parents:
14329
diff
changeset
|
1705 { |
14235
e3bc8cdc94dd
patch 8.1.0134: Lua interface does not support funcref
Christian Brabandt <cb@256bit.org>
parents:
12515
diff
changeset
|
1706 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
|
1707 list_append_tv(l, &v); |
16009
3b524e8110ab
patch 8.1.1010: Lua interface leaks memory
Bram Moolenaar <Bram@vim.org>
parents:
15868
diff
changeset
|
1708 clear_tv(&v); |
14235
e3bc8cdc94dd
patch 8.1.0134: Lua interface does not support funcref
Christian Brabandt <cb@256bit.org>
parents:
12515
diff
changeset
|
1709 } |
18798
f0f9692d4487
patch 8.1.2387: using old C style comments
Bram Moolenaar <Bram@vim.org>
parents:
18370
diff
changeset
|
1710 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
|
1711 } while (notnil); |
e3bc8cdc94dd
patch 8.1.0134: Lua interface does not support funcref
Christian Brabandt <cb@256bit.org>
parents:
12515
diff
changeset
|
1712 } |
e3bc8cdc94dd
patch 8.1.0134: Lua interface does not support funcref
Christian Brabandt <cb@256bit.org>
parents:
12515
diff
changeset
|
1713 } |
3450 | 1714 return 1; |
1715 } | |
1716 | |
1717 static int | |
1718 luaV_dict(lua_State *L) | |
1719 { | |
14235
e3bc8cdc94dd
patch 8.1.0134: Lua interface does not support funcref
Christian Brabandt <cb@256bit.org>
parents:
12515
diff
changeset
|
1720 dict_T *d; |
e3bc8cdc94dd
patch 8.1.0134: Lua interface does not support funcref
Christian Brabandt <cb@256bit.org>
parents:
12515
diff
changeset
|
1721 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
|
1722 |
e3bc8cdc94dd
patch 8.1.0134: Lua interface does not support funcref
Christian Brabandt <cb@256bit.org>
parents:
12515
diff
changeset
|
1723 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
|
1724 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
|
1725 d = dict_alloc(); |
3450 | 1726 if (d == NULL) |
1727 lua_pushnil(L); | |
1728 else | |
14235
e3bc8cdc94dd
patch 8.1.0134: Lua interface does not support funcref
Christian Brabandt <cb@256bit.org>
parents:
12515
diff
changeset
|
1729 { |
3450 | 1730 luaV_newdict(L, d); |
18798
f0f9692d4487
patch 8.1.2387: using old C style comments
Bram Moolenaar <Bram@vim.org>
parents:
18370
diff
changeset
|
1731 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
|
1732 { |
e3bc8cdc94dd
patch 8.1.0134: Lua interface does not support funcref
Christian Brabandt <cb@256bit.org>
parents:
12515
diff
changeset
|
1733 lua_pushnil(L); |
e3bc8cdc94dd
patch 8.1.0134: Lua interface does not support funcref
Christian Brabandt <cb@256bit.org>
parents:
12515
diff
changeset
|
1734 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
|
1735 { |
e3bc8cdc94dd
patch 8.1.0134: Lua interface does not support funcref
Christian Brabandt <cb@256bit.org>
parents:
12515
diff
changeset
|
1736 char_u *key; |
e3bc8cdc94dd
patch 8.1.0134: Lua interface does not support funcref
Christian Brabandt <cb@256bit.org>
parents:
12515
diff
changeset
|
1737 dictitem_T *di; |
e3bc8cdc94dd
patch 8.1.0134: Lua interface does not support funcref
Christian Brabandt <cb@256bit.org>
parents:
12515
diff
changeset
|
1738 typval_T v; |
14329
fe651f98e173
patch 8.1.0180: static analysis errors in Lua interface
Christian Brabandt <cb@256bit.org>
parents:
14296
diff
changeset
|
1739 |
18798
f0f9692d4487
patch 8.1.2387: using old C style comments
Bram Moolenaar <Bram@vim.org>
parents:
18370
diff
changeset
|
1740 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
|
1741 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
|
1742 if (key == NULL) |
fe651f98e173
patch 8.1.0180: static analysis errors in Lua interface
Christian Brabandt <cb@256bit.org>
parents:
14296
diff
changeset
|
1743 { |
fe651f98e173
patch 8.1.0180: static analysis errors in Lua interface
Christian Brabandt <cb@256bit.org>
parents:
14296
diff
changeset
|
1744 lua_pushnil(L); |
fe651f98e173
patch 8.1.0180: static analysis errors in Lua interface
Christian Brabandt <cb@256bit.org>
parents:
14296
diff
changeset
|
1745 return 1; |
fe651f98e173
patch 8.1.0180: static analysis errors in Lua interface
Christian Brabandt <cb@256bit.org>
parents:
14296
diff
changeset
|
1746 } |
fe651f98e173
patch 8.1.0180: static analysis errors in Lua interface
Christian Brabandt <cb@256bit.org>
parents:
14296
diff
changeset
|
1747 if (*key == NUL) |
14235
e3bc8cdc94dd
patch 8.1.0134: Lua interface does not support funcref
Christian Brabandt <cb@256bit.org>
parents:
12515
diff
changeset
|
1748 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
|
1749 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
|
1750 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
|
1751 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
|
1752 { |
14235
e3bc8cdc94dd
patch 8.1.0134: Lua interface does not support funcref
Christian Brabandt <cb@256bit.org>
parents:
12515
diff
changeset
|
1753 vim_free(di); |
e3bc8cdc94dd
patch 8.1.0134: Lua interface does not support funcref
Christian Brabandt <cb@256bit.org>
parents:
12515
diff
changeset
|
1754 lua_pushnil(L); |
e3bc8cdc94dd
patch 8.1.0134: Lua interface does not support funcref
Christian Brabandt <cb@256bit.org>
parents:
12515
diff
changeset
|
1755 return 1; |
e3bc8cdc94dd
patch 8.1.0134: Lua interface does not support funcref
Christian Brabandt <cb@256bit.org>
parents:
12515
diff
changeset
|
1756 } |
e3bc8cdc94dd
patch 8.1.0134: Lua interface does not support funcref
Christian Brabandt <cb@256bit.org>
parents:
12515
diff
changeset
|
1757 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
|
1758 clear_tv(&v); |
18798
f0f9692d4487
patch 8.1.2387: using old C style comments
Bram Moolenaar <Bram@vim.org>
parents:
18370
diff
changeset
|
1759 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
|
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 } |
e3bc8cdc94dd
patch 8.1.0134: Lua interface does not support funcref
Christian Brabandt <cb@256bit.org>
parents:
12515
diff
changeset
|
1763 return 1; |
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 |
e3bc8cdc94dd
patch 8.1.0134: Lua interface does not support funcref
Christian Brabandt <cb@256bit.org>
parents:
12515
diff
changeset
|
1766 static int |
16076
a2f0e93a5857
patch 8.1.1043: Lua interface does not support Blob
Bram Moolenaar <Bram@vim.org>
parents:
16028
diff
changeset
|
1767 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
|
1768 { |
a2f0e93a5857
patch 8.1.1043: Lua interface does not support Blob
Bram Moolenaar <Bram@vim.org>
parents:
16028
diff
changeset
|
1769 blob_T *b; |
a2f0e93a5857
patch 8.1.1043: Lua interface does not support Blob
Bram Moolenaar <Bram@vim.org>
parents:
16028
diff
changeset
|
1770 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
|
1771 |
a2f0e93a5857
patch 8.1.1043: Lua interface does not support Blob
Bram Moolenaar <Bram@vim.org>
parents:
16028
diff
changeset
|
1772 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
|
1773 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
|
1774 b = blob_alloc(); |
a2f0e93a5857
patch 8.1.1043: Lua interface does not support Blob
Bram Moolenaar <Bram@vim.org>
parents:
16028
diff
changeset
|
1775 if (b == NULL) |
a2f0e93a5857
patch 8.1.1043: Lua interface does not support Blob
Bram Moolenaar <Bram@vim.org>
parents:
16028
diff
changeset
|
1776 lua_pushnil(L); |
a2f0e93a5857
patch 8.1.1043: Lua interface does not support Blob
Bram Moolenaar <Bram@vim.org>
parents:
16028
diff
changeset
|
1777 else |
a2f0e93a5857
patch 8.1.1043: Lua interface does not support Blob
Bram Moolenaar <Bram@vim.org>
parents:
16028
diff
changeset
|
1778 { |
a2f0e93a5857
patch 8.1.1043: Lua interface does not support Blob
Bram Moolenaar <Bram@vim.org>
parents:
16028
diff
changeset
|
1779 luaV_newblob(L, b); |
a2f0e93a5857
patch 8.1.1043: Lua interface does not support Blob
Bram Moolenaar <Bram@vim.org>
parents:
16028
diff
changeset
|
1780 if (initarg) |
a2f0e93a5857
patch 8.1.1043: Lua interface does not support Blob
Bram Moolenaar <Bram@vim.org>
parents:
16028
diff
changeset
|
1781 { |
a2f0e93a5857
patch 8.1.1043: Lua interface does not support Blob
Bram Moolenaar <Bram@vim.org>
parents:
16028
diff
changeset
|
1782 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
|
1783 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
|
1784 |
16099
643fe07aa0e2
patch 8.1.1054: not checking return value of ga_grow()
Bram Moolenaar <Bram@vim.org>
parents:
16076
diff
changeset
|
1785 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
|
1786 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
|
1787 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
|
1788 } |
a2f0e93a5857
patch 8.1.1043: Lua interface does not support Blob
Bram Moolenaar <Bram@vim.org>
parents:
16028
diff
changeset
|
1789 } |
a2f0e93a5857
patch 8.1.1043: Lua interface does not support Blob
Bram Moolenaar <Bram@vim.org>
parents:
16028
diff
changeset
|
1790 return 1; |
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 |
a2f0e93a5857
patch 8.1.1043: Lua interface does not support Blob
Bram Moolenaar <Bram@vim.org>
parents:
16028
diff
changeset
|
1793 static int |
14235
e3bc8cdc94dd
patch 8.1.0134: Lua interface does not support funcref
Christian Brabandt <cb@256bit.org>
parents:
12515
diff
changeset
|
1794 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
|
1795 { |
e3bc8cdc94dd
patch 8.1.0134: Lua interface does not support funcref
Christian Brabandt <cb@256bit.org>
parents:
12515
diff
changeset
|
1796 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
|
1797 // 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
|
1798 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
|
1799 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
|
1800 luaV_newfuncref(L, (char_u *) name); |
3450 | 1801 return 1; |
1802 } | |
1803 | |
1804 static int | |
2330 | 1805 luaV_buffer(lua_State *L) |
1806 { | |
2320
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
1807 buf_T *buf; |
18798
f0f9692d4487
patch 8.1.2387: using old C style comments
Bram Moolenaar <Bram@vim.org>
parents:
18370
diff
changeset
|
1808 if (lua_isstring(L, 1)) // get by number or name? |
2330 | 1809 { |
18798
f0f9692d4487
patch 8.1.2387: using old C style comments
Bram Moolenaar <Bram@vim.org>
parents:
18370
diff
changeset
|
1810 if (lua_isnumber(L, 1)) // by number? |
2330 | 1811 { |
1812 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
|
1813 FOR_ALL_BUFFERS(buf) |
2330 | 1814 if (buf->b_fnum == n) break; |
1815 } | |
14235
e3bc8cdc94dd
patch 8.1.0134: Lua interface does not support funcref
Christian Brabandt <cb@256bit.org>
parents:
12515
diff
changeset
|
1816 else // by name |
e3bc8cdc94dd
patch 8.1.0134: Lua interface does not support funcref
Christian Brabandt <cb@256bit.org>
parents:
12515
diff
changeset
|
1817 { |
2330 | 1818 size_t l; |
1819 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
|
1820 FOR_ALL_BUFFERS(buf) |
2330 | 1821 { |
1822 if (buf->b_ffname == NULL || buf->b_sfname == NULL) | |
1823 { | |
1824 if (l == 0) break; | |
1825 } | |
2331
3840b7508835
Make it easier to build with Lua. Remove compiler warnings.
Bram Moolenaar <bram@vim.org>
parents:
2330
diff
changeset
|
1826 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
|
1827 || strncmp(s, (char *)buf->b_sfname, l) == 0) |
2330 | 1828 break; |
1829 } | |
1830 } | |
2320
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
1831 } |
3450 | 1832 else |
18798
f0f9692d4487
patch 8.1.2387: using old C style comments
Bram Moolenaar <Bram@vim.org>
parents:
18370
diff
changeset
|
1833 buf = (lua_toboolean(L, 1)) ? firstbuf : curbuf; // first buffer? |
3450 | 1834 luaV_pushbuffer(L, buf); |
2320
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
1835 return 1; |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
1836 } |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
1837 |
2330 | 1838 static int |
1839 luaV_window(lua_State *L) | |
1840 { | |
2320
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
1841 win_T *win; |
18798
f0f9692d4487
patch 8.1.2387: using old C style comments
Bram Moolenaar <Bram@vim.org>
parents:
18370
diff
changeset
|
1842 if (lua_isnumber(L, 1)) // get by number? |
2330 | 1843 { |
1844 int n = lua_tointeger(L, 1); | |
1845 for (win = firstwin; win != NULL; win = win->w_next, n--) | |
1846 if (n == 1) break; | |
2320
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
1847 } |
3450 | 1848 else |
18798
f0f9692d4487
patch 8.1.2387: using old C style comments
Bram Moolenaar <Bram@vim.org>
parents:
18370
diff
changeset
|
1849 win = (lua_toboolean(L, 1)) ? firstwin : curwin; // first window? |
3450 | 1850 luaV_pushwindow(L, win); |
2320
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
1851 return 1; |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
1852 } |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
1853 |
2330 | 1854 static int |
1855 luaV_open(lua_State *L) | |
1856 { | |
2320
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
1857 char_u *s = NULL; |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
1858 #ifdef HAVE_SANDBOX |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
1859 luaV_checksandbox(L); |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
1860 #endif |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
1861 if (lua_isstring(L, 1)) s = (char_u *) lua_tostring(L, 1); |
3200 | 1862 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
|
1863 return 1; |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
1864 } |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
1865 |
2330 | 1866 static int |
3450 | 1867 luaV_type(lua_State *L) |
2330 | 1868 { |
3450 | 1869 luaL_checkany(L, 1); |
18798
f0f9692d4487
patch 8.1.2387: using old C style comments
Bram Moolenaar <Bram@vim.org>
parents:
18370
diff
changeset
|
1870 if (lua_type(L, 1) == LUA_TUSERDATA) // check vim udata? |
3450 | 1871 { |
1872 lua_settop(L, 1); | |
1873 if (lua_getmetatable(L, 1)) | |
1874 { | |
1875 luaV_getfield(L, LUAVIM_LIST); | |
1876 if (lua_rawequal(L, -1, 2)) | |
1877 { | |
1878 lua_pushstring(L, "list"); | |
1879 return 1; | |
1880 } | |
1881 luaV_getfield(L, LUAVIM_DICT); | |
1882 if (lua_rawequal(L, -1, 2)) | |
1883 { | |
1884 lua_pushstring(L, "dict"); | |
1885 return 1; | |
1886 } | |
16076
a2f0e93a5857
patch 8.1.1043: Lua interface does not support Blob
Bram Moolenaar <Bram@vim.org>
parents:
16028
diff
changeset
|
1887 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
|
1888 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
|
1889 { |
a2f0e93a5857
patch 8.1.1043: Lua interface does not support Blob
Bram Moolenaar <Bram@vim.org>
parents:
16028
diff
changeset
|
1890 lua_pushstring(L, "blob"); |
a2f0e93a5857
patch 8.1.1043: Lua interface does not support Blob
Bram Moolenaar <Bram@vim.org>
parents:
16028
diff
changeset
|
1891 return 1; |
a2f0e93a5857
patch 8.1.1043: Lua interface does not support Blob
Bram Moolenaar <Bram@vim.org>
parents:
16028
diff
changeset
|
1892 } |
14235
e3bc8cdc94dd
patch 8.1.0134: Lua interface does not support funcref
Christian Brabandt <cb@256bit.org>
parents:
12515
diff
changeset
|
1893 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
|
1894 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
|
1895 { |
e3bc8cdc94dd
patch 8.1.0134: Lua interface does not support funcref
Christian Brabandt <cb@256bit.org>
parents:
12515
diff
changeset
|
1896 lua_pushstring(L, "funcref"); |
e3bc8cdc94dd
patch 8.1.0134: Lua interface does not support funcref
Christian Brabandt <cb@256bit.org>
parents:
12515
diff
changeset
|
1897 return 1; |
e3bc8cdc94dd
patch 8.1.0134: Lua interface does not support funcref
Christian Brabandt <cb@256bit.org>
parents:
12515
diff
changeset
|
1898 } |
3450 | 1899 luaV_getfield(L, LUAVIM_BUFFER); |
1900 if (lua_rawequal(L, -1, 2)) | |
1901 { | |
1902 lua_pushstring(L, "buffer"); | |
1903 return 1; | |
1904 } | |
1905 luaV_getfield(L, LUAVIM_WINDOW); | |
1906 if (lua_rawequal(L, -1, 2)) | |
1907 { | |
1908 lua_pushstring(L, "window"); | |
1909 return 1; | |
1910 } | |
1911 } | |
1912 } | |
18798
f0f9692d4487
patch 8.1.2387: using old C style comments
Bram Moolenaar <Bram@vim.org>
parents:
18370
diff
changeset
|
1913 lua_pushstring(L, luaL_typename(L, 1)); // fallback |
2320
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
1914 return 1; |
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 |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
1917 static const luaL_Reg luaV_module[] = { |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
1918 {"command", luaV_command}, |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
1919 {"eval", luaV_eval}, |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
1920 {"beep", luaV_beep}, |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
1921 {"line", luaV_line}, |
3450 | 1922 {"list", luaV_list}, |
1923 {"dict", luaV_dict}, | |
16076
a2f0e93a5857
patch 8.1.1043: Lua interface does not support Blob
Bram Moolenaar <Bram@vim.org>
parents:
16028
diff
changeset
|
1924 {"blob", luaV_blob}, |
14235
e3bc8cdc94dd
patch 8.1.0134: Lua interface does not support funcref
Christian Brabandt <cb@256bit.org>
parents:
12515
diff
changeset
|
1925 {"funcref", luaV_funcref}, |
2320
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
1926 {"buffer", luaV_buffer}, |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
1927 {"window", luaV_window}, |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
1928 {"open", luaV_open}, |
3450 | 1929 {"type", luaV_type}, |
2320
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
1930 {NULL, NULL} |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
1931 }; |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
1932 |
18798
f0f9692d4487
patch 8.1.2387: using old C style comments
Bram Moolenaar <Bram@vim.org>
parents:
18370
diff
changeset
|
1933 /* |
f0f9692d4487
patch 8.1.2387: using old C style comments
Bram Moolenaar <Bram@vim.org>
parents:
18370
diff
changeset
|
1934 * 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
|
1935 */ |
3450 | 1936 static int |
1937 luaV_free(lua_State *L) | |
1938 { | |
1939 lua_pushnil(L); | |
1940 luaV_setudata(L, lua_touserdata(L, 1)); | |
1941 return 0; | |
1942 } | |
1943 | |
1944 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
|
1945 luaV_luaeval(lua_State *L) |
3450 | 1946 { |
1947 luaL_Buffer b; | |
1948 size_t l; | |
1949 const char *str = lua_tolstring(L, 1, &l); | |
1950 typval_T *arg = (typval_T *) lua_touserdata(L, 2); | |
1951 typval_T *rettv = (typval_T *) lua_touserdata(L, 3); | |
1952 luaL_buffinit(L, &b); | |
1953 luaL_addlstring(&b, LUAVIM_EVALHEADER, sizeof(LUAVIM_EVALHEADER) - 1); | |
1954 luaL_addlstring(&b, str, l); | |
1955 luaL_pushresult(&b); | |
1956 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
|
1957 if (luaL_loadbuffer(L, str, l, LUAVIM_EVALNAME)) // compile error? |
3450 | 1958 { |
1959 luaV_emsg(L); | |
1960 return 0; | |
1961 } | |
1962 luaV_pushtypval(L, arg); | |
18798
f0f9692d4487
patch 8.1.2387: using old C style comments
Bram Moolenaar <Bram@vim.org>
parents:
18370
diff
changeset
|
1963 if (lua_pcall(L, 1, 1, 0)) // running error? |
3450 | 1964 { |
1965 luaV_emsg(L); | |
1966 return 0; | |
1967 } | |
14235
e3bc8cdc94dd
patch 8.1.0134: Lua interface does not support funcref
Christian Brabandt <cb@256bit.org>
parents:
12515
diff
changeset
|
1968 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
|
1969 emsg("luaeval: cannot convert value"); |
6590 | 1970 return 0; |
3450 | 1971 } |
1972 | |
1973 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
|
1974 luaV_setref(lua_State *L) |
3450 | 1975 { |
16028
8ad2cda3757d
patch 8.1.1019: Lua: may garbage collect function reference in use
Bram Moolenaar <Bram@vim.org>
parents:
16009
diff
changeset
|
1976 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
|
1977 int abort = FALSE; |
6565 | 1978 |
3450 | 1979 luaV_getfield(L, LUAVIM_LIST); |
1980 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
|
1981 luaV_getfield(L, LUAVIM_FUNCREF); |
3450 | 1982 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
|
1983 // traverse cache table |
6586 | 1984 while (!abort && lua_next(L, lua_upvalueindex(1)) != 0) |
3450 | 1985 { |
1986 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
|
1987 if (lua_rawequal(L, -1, 2)) // list? |
3450 | 1988 { |
16028
8ad2cda3757d
patch 8.1.1019: Lua: may garbage collect function reference in use
Bram Moolenaar <Bram@vim.org>
parents:
16009
diff
changeset
|
1989 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
|
1990 |
17168
1d30eb64a7a2
patch 8.1.1583: set_ref_in_list() only sets ref in items
Bram Moolenaar <Bram@vim.org>
parents:
16099
diff
changeset
|
1991 abort = set_ref_in_list(l, copyID); |
3450 | 1992 } |
16028
8ad2cda3757d
patch 8.1.1019: Lua: may garbage collect function reference in use
Bram Moolenaar <Bram@vim.org>
parents:
16009
diff
changeset
|
1993 else if (lua_rawequal(L, -1, 3)) // dict? |
3450 | 1994 { |
16028
8ad2cda3757d
patch 8.1.1019: Lua: may garbage collect function reference in use
Bram Moolenaar <Bram@vim.org>
parents:
16009
diff
changeset
|
1995 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
|
1996 |
17168
1d30eb64a7a2
patch 8.1.1583: set_ref_in_list() only sets ref in items
Bram Moolenaar <Bram@vim.org>
parents:
16099
diff
changeset
|
1997 abort = set_ref_in_dict(d, copyID); |
3450 | 1998 } |
16028
8ad2cda3757d
patch 8.1.1019: Lua: may garbage collect function reference in use
Bram Moolenaar <Bram@vim.org>
parents:
16009
diff
changeset
|
1999 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
|
2000 { |
8ad2cda3757d
patch 8.1.1019: Lua: may garbage collect function reference in use
Bram Moolenaar <Bram@vim.org>
parents:
16009
diff
changeset
|
2001 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
|
2002 |
17168
1d30eb64a7a2
patch 8.1.1583: set_ref_in_list() only sets ref in items
Bram Moolenaar <Bram@vim.org>
parents:
16099
diff
changeset
|
2003 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
|
2004 } |
8ad2cda3757d
patch 8.1.1019: Lua: may garbage collect function reference in use
Bram Moolenaar <Bram@vim.org>
parents:
16009
diff
changeset
|
2005 lua_pop(L, 2); // metatable and value |
3450 | 2006 } |
6565 | 2007 lua_pushinteger(L, abort); |
6590 | 2008 return 1; |
3450 | 2009 } |
2010 | |
2330 | 2011 static int |
2012 luaopen_vim(lua_State *L) | |
2013 { | |
18798
f0f9692d4487
patch 8.1.2387: using old C style comments
Bram Moolenaar <Bram@vim.org>
parents:
18370
diff
changeset
|
2014 // set cache table |
2320
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
2015 lua_newtable(L); |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
2016 lua_newtable(L); |
3450 | 2017 lua_pushstring(L, "v"); |
2320
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
2018 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
|
2019 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
|
2020 // print |
2320
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
2021 lua_pushcfunction(L, luaV_print); |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
2022 lua_setglobal(L, "print"); |
18798
f0f9692d4487
patch 8.1.2387: using old C style comments
Bram Moolenaar <Bram@vim.org>
parents:
18370
diff
changeset
|
2023 // debug.debug |
3091 | 2024 lua_getglobal(L, "debug"); |
2025 lua_pushcfunction(L, luaV_debug); | |
2026 lua_setfield(L, -2, "debug"); | |
2027 lua_pop(L, 1); | |
18798
f0f9692d4487
patch 8.1.2387: using old C style comments
Bram Moolenaar <Bram@vim.org>
parents:
18370
diff
changeset
|
2028 // free |
2320
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
2029 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
|
2030 lua_pushvalue(L, 1); // cache table |
3450 | 2031 lua_pushcclosure(L, luaV_free, 1); |
2032 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
|
2033 // luaeval |
3450 | 2034 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
|
2035 lua_pushvalue(L, 1); // cache table |
3450 | 2036 lua_pushcclosure(L, luaV_luaeval, 1); |
2037 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
|
2038 // setref |
3450 | 2039 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
|
2040 lua_pushvalue(L, 1); // cache table |
3450 | 2041 lua_pushcclosure(L, luaV_setref, 1); |
2320
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
2042 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
|
2043 // register |
3450 | 2044 luaV_newmetatable(L, LUAVIM_LIST); |
2045 lua_pushvalue(L, 1); | |
2046 luaV_openlib(L, luaV_List_mt, 1); | |
2047 luaV_newmetatable(L, LUAVIM_DICT); | |
2048 lua_pushvalue(L, 1); | |
2049 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
|
2050 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
|
2051 lua_pushvalue(L, 1); |
a2f0e93a5857
patch 8.1.1043: Lua interface does not support Blob
Bram Moolenaar <Bram@vim.org>
parents:
16028
diff
changeset
|
2052 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
|
2053 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
|
2054 lua_pushvalue(L, 1); |
e3bc8cdc94dd
patch 8.1.0134: Lua interface does not support funcref
Christian Brabandt <cb@256bit.org>
parents:
12515
diff
changeset
|
2055 luaV_openlib(L, luaV_Funcref_mt, 1); |
2320
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
2056 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
|
2057 lua_pushvalue(L, 1); // cache table |
3450 | 2058 luaV_openlib(L, luaV_Buffer_mt, 1); |
2320
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
2059 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
|
2060 lua_pushvalue(L, 1); // cache table |
3450 | 2061 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
|
2062 lua_newtable(L); // vim table |
f0f9692d4487
patch 8.1.2387: using old C style comments
Bram Moolenaar <Bram@vim.org>
parents:
18370
diff
changeset
|
2063 lua_pushvalue(L, 1); // cache table |
3450 | 2064 luaV_openlib(L, luaV_module, 1); |
2065 lua_setglobal(L, LUAVIM_NAME); | |
2320
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
2066 return 0; |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
2067 } |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
2068 |
2330 | 2069 static lua_State * |
2070 luaV_newstate(void) | |
2071 { | |
2320
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
2072 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
|
2073 luaL_openlibs(L); // core libs |
f0f9692d4487
patch 8.1.2387: using old C style comments
Bram Moolenaar <Bram@vim.org>
parents:
18370
diff
changeset
|
2074 lua_pushcfunction(L, luaopen_vim); // vim |
2320
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
2075 lua_call(L, 0, 0); |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
2076 return L; |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
2077 } |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
2078 |
2330 | 2079 static void |
2080 luaV_setrange(lua_State *L, int line1, int line2) | |
2081 { | |
2320
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
2082 lua_getglobal(L, LUAVIM_NAME); |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
2083 lua_pushinteger(L, line1); |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
2084 lua_setfield(L, -2, "firstline"); |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
2085 lua_pushinteger(L, line2); |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
2086 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
|
2087 lua_pop(L, 1); // vim table |
2320
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 |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
2090 |
18798
f0f9692d4487
patch 8.1.2387: using old C style comments
Bram Moolenaar <Bram@vim.org>
parents:
18370
diff
changeset
|
2091 // ======= Interface ======= |
2320
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
2092 |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
2093 static lua_State *L = NULL; |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
2094 |
2330 | 2095 static int |
3450 | 2096 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
|
2097 { |
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 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
|
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 |
8c512e2c7cb5
Fix: Lua interface tried to load the library when closing a buffer or window.
Bram Moolenaar <bram@vim.org>
parents:
2431
diff
changeset
|
2101 static int |
2330 | 2102 lua_init(void) |
2103 { | |
3450 | 2104 if (!lua_isopen()) |
2330 | 2105 { |
2320
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
2106 #ifdef DYNAMIC_LUA |
2330 | 2107 if (!lua_enabled(TRUE)) |
2108 { | |
15470
55ccc2d353bd
patch 8.1.0743: giving error messages is not flexible
Bram Moolenaar <Bram@vim.org>
parents:
15328
diff
changeset
|
2109 emsg(_("Lua library cannot be loaded.")); |
2330 | 2110 return FAIL; |
2111 } | |
2320
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
2112 #endif |
2330 | 2113 L = luaV_newstate(); |
2320
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
2114 } |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
2115 return OK; |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
2116 } |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
2117 |
2330 | 2118 void |
2119 lua_end(void) | |
2120 { | |
3450 | 2121 if (lua_isopen()) |
2330 | 2122 { |
2123 lua_close(L); | |
2124 L = NULL; | |
2320
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
2125 #ifdef DYNAMIC_LUA |
2330 | 2126 end_dynamic_lua(); |
2320
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
2127 #endif |
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 } |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
2130 |
18798
f0f9692d4487
patch 8.1.2387: using old C style comments
Bram Moolenaar <Bram@vim.org>
parents:
18370
diff
changeset
|
2131 /* |
f0f9692d4487
patch 8.1.2387: using old C style comments
Bram Moolenaar <Bram@vim.org>
parents:
18370
diff
changeset
|
2132 * ex commands |
f0f9692d4487
patch 8.1.2387: using old C style comments
Bram Moolenaar <Bram@vim.org>
parents:
18370
diff
changeset
|
2133 */ |
2330 | 2134 void |
2135 ex_lua(exarg_T *eap) | |
2136 { | |
2320
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
2137 char *script; |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
2138 if (lua_init() == FAIL) return; |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
2139 script = (char *) script_get(eap, eap->arg); |
2330 | 2140 if (!eap->skip) |
2141 { | |
2142 char *s = (script) ? script : (char *) eap->arg; | |
2143 luaV_setrange(L, eap->line1, eap->line2); | |
2144 if (luaL_loadbuffer(L, s, strlen(s), LUAVIM_CHUNKNAME) | |
2145 || lua_pcall(L, 0, 0, 0)) | |
2146 luaV_emsg(L); | |
2320
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
2147 } |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
2148 if (script != NULL) vim_free(script); |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
2149 } |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
2150 |
2330 | 2151 void |
2152 ex_luado(exarg_T *eap) | |
2153 { | |
2320
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
2154 linenr_T l; |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
2155 const char *s = (const char *) eap->arg; |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
2156 luaL_Buffer b; |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
2157 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
|
2158 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
|
2159 |
2320
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
2160 if (lua_init() == FAIL) return; |
2330 | 2161 if (u_save(eap->line1 - 1, eap->line2 + 1) == FAIL) |
2162 { | |
15470
55ccc2d353bd
patch 8.1.0743: giving error messages is not flexible
Bram Moolenaar <Bram@vim.org>
parents:
15328
diff
changeset
|
2163 emsg(_("cannot save undo information")); |
2330 | 2164 return; |
2320
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
2165 } |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
2166 luaV_setrange(L, eap->line1, eap->line2); |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
2167 luaL_buffinit(L, &b); |
18798
f0f9692d4487
patch 8.1.2387: using old C style comments
Bram Moolenaar <Bram@vim.org>
parents:
18370
diff
changeset
|
2168 luaL_addlstring(&b, "return function(line, linenr) ", 30); // header |
2320
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
2169 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
|
2170 luaL_addlstring(&b, " end", 4); // footer |
2320
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
2171 luaL_pushresult(&b); |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
2172 s = lua_tolstring(L, -1, &len); |
2330 | 2173 if (luaL_loadbuffer(L, s, len, LUAVIM_CHUNKNAME)) |
2174 { | |
2175 luaV_emsg(L); | |
18798
f0f9692d4487
patch 8.1.2387: using old C style comments
Bram Moolenaar <Bram@vim.org>
parents:
18370
diff
changeset
|
2176 lua_pop(L, 1); // function body |
2330 | 2177 return; |
2320
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
2178 } |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
2179 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
|
2180 lua_replace(L, -2); // function -> body |
2330 | 2181 for (l = eap->line1; l <= eap->line2; l++) |
2182 { | |
18798
f0f9692d4487
patch 8.1.2387: using old C style comments
Bram Moolenaar <Bram@vim.org>
parents:
18370
diff
changeset
|
2183 // 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
|
2184 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
|
2185 break; |
6c3d42d18366
patch 8.0.0268: may get ml_get error when :luado deletes lines
Christian Brabandt <cb@256bit.org>
parents:
10025
diff
changeset
|
2186 |
18798
f0f9692d4487
patch 8.1.2387: using old C style comments
Bram Moolenaar <Bram@vim.org>
parents:
18370
diff
changeset
|
2187 lua_pushvalue(L, -1); // function |
f0f9692d4487
patch 8.1.2387: using old C style comments
Bram Moolenaar <Bram@vim.org>
parents:
18370
diff
changeset
|
2188 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
|
2189 lua_pushinteger(L, l); // current line number as arg |
3459 | 2190 if (lua_pcall(L, 2, 1, 0)) |
2330 | 2191 { |
2192 luaV_emsg(L); | |
2193 break; | |
2194 } | |
18798
f0f9692d4487
patch 8.1.2387: using old C style comments
Bram Moolenaar <Bram@vim.org>
parents:
18370
diff
changeset
|
2195 // 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
|
2196 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
|
2197 break; |
18798
f0f9692d4487
patch 8.1.2387: using old C style comments
Bram Moolenaar <Bram@vim.org>
parents:
18370
diff
changeset
|
2198 if (lua_isstring(L, -1)) // update line? |
2330 | 2199 { |
2320
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
2200 #ifdef HAVE_SANDBOX |
2330 | 2201 luaV_checksandbox(L); |
2320
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
2202 #endif |
2330 | 2203 ml_replace(l, luaV_toline(L, -1), TRUE); |
2204 changed_bytes(l, 0); | |
18798
f0f9692d4487
patch 8.1.2387: using old C style comments
Bram Moolenaar <Bram@vim.org>
parents:
18370
diff
changeset
|
2205 lua_pop(L, 1); // result from luaV_toline |
2330 | 2206 } |
18798
f0f9692d4487
patch 8.1.2387: using old C style comments
Bram Moolenaar <Bram@vim.org>
parents:
18370
diff
changeset
|
2207 lua_pop(L, 1); // line |
2320
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
2208 } |
18798
f0f9692d4487
patch 8.1.2387: using old C style comments
Bram Moolenaar <Bram@vim.org>
parents:
18370
diff
changeset
|
2209 lua_pop(L, 1); // function |
2320
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
2210 check_cursor(); |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
2211 update_screen(NOT_VALID); |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
2212 } |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
2213 |
2330 | 2214 void |
2215 ex_luafile(exarg_T *eap) | |
2216 { | |
2217 if (lua_init() == FAIL) | |
2218 return; | |
2219 if (!eap->skip) | |
2220 { | |
2221 luaV_setrange(L, eap->line1, eap->line2); | |
2222 if (luaL_loadfile(L, (char *) eap->arg) || lua_pcall(L, 0, 0, 0)) | |
2223 luaV_emsg(L); | |
2320
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 } |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
2226 |
3450 | 2227 #define luaV_freetype(typ,tname) \ |
2228 void \ | |
2229 lua_##tname##_free(typ *o) \ | |
2230 { \ | |
2231 if (!lua_isopen()) return; \ | |
2232 luaV_getfield(L, LUAVIM_FREE); \ | |
2233 lua_pushlightuserdata(L, (void *) o); \ | |
2234 lua_call(L, 1, 0); \ | |
2235 } | |
2236 | |
2237 luaV_freetype(buf_T, buffer) | |
2238 luaV_freetype(win_T, window) | |
2239 | |
2330 | 2240 void |
16028
8ad2cda3757d
patch 8.1.1019: Lua: may garbage collect function reference in use
Bram Moolenaar <Bram@vim.org>
parents:
16009
diff
changeset
|
2241 do_luaeval(char_u *str, typval_T *arg, typval_T *rettv) |
2330 | 2242 { |
3450 | 2243 lua_init(); |
2244 luaV_getfield(L, LUAVIM_LUAEVAL); | |
2245 lua_pushstring(L, (char *) str); | |
2246 lua_pushlightuserdata(L, (void *) arg); | |
2247 lua_pushlightuserdata(L, (void *) rettv); | |
2248 lua_call(L, 3, 0); | |
2320
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
2249 } |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
2250 |
6565 | 2251 int |
16028
8ad2cda3757d
patch 8.1.1019: Lua: may garbage collect function reference in use
Bram Moolenaar <Bram@vim.org>
parents:
16009
diff
changeset
|
2252 set_ref_in_lua(int copyID) |
2330 | 2253 { |
6565 | 2254 int aborted = 0; |
2255 | |
2256 if (lua_isopen()) | |
2257 { | |
2258 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
|
2259 // call the function with 1 arg, getting 1 result back |
6565 | 2260 lua_pushinteger(L, copyID); |
2261 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
|
2262 // get the result |
6565 | 2263 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
|
2264 // pop result off the stack |
6565 | 2265 lua_pop(L, 1); |
2266 } | |
2267 return aborted; | |
2320
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 |
966a5609669e
Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff
changeset
|
2270 #endif |