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