annotate src/if_lua.c @ 2429:7ce8b24450dc vim73

Improvements for ":find" completion. (Nazri Ramliy)
author Bram Moolenaar <bram@vim.org>
date Wed, 28 Jul 2010 22:29:10 +0200
parents f149bb1cf5be
children 7b764999f9b9
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
2320
966a5609669e Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
1 /* vi:set ts=8 sts=4 sw=4:
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
966a5609669e Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
12 #include <stdio.h>
966a5609669e Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
13 #include <string.h>
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 #include "vim.h"
966a5609669e Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
18
966a5609669e Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
19 /* Only do the following when the feature is enabled. Needed for "make
966a5609669e Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
20 * depend". */
966a5609669e Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
21 #if defined(FEAT_LUA) || defined(PROTO)
966a5609669e Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
22
966a5609669e Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
23 #define LUAVIM_CHUNKNAME "vim chunk"
966a5609669e Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
24 #define LUAVIM_NAME "vim"
966a5609669e Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
25
966a5609669e Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
26 typedef buf_T *luaV_Buffer;
966a5609669e Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
27 typedef win_T *luaV_Window;
966a5609669e Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
28 typedef void (*msgfunc_T)(char_u *);
966a5609669e Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
29
966a5609669e Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
30 static const char LUAVIM_BUFFER[] = "buffer";
966a5609669e Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
31 static const char LUAVIM_WINDOW[] = "window";
966a5609669e Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
32 static const char LUAVIM_FREE[] = "luaV_free";
966a5609669e Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
33
966a5609669e Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
34 #define luaV_getfield(L, s) \
966a5609669e Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
35 lua_pushlightuserdata((L), (void *)(s)); \
966a5609669e Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
36 lua_rawget((L), LUA_REGISTRYINDEX)
966a5609669e Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
37 #define luaV_checksandbox(L) \
966a5609669e Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
38 if (sandbox) luaL_error((L), "not allowed in sandbox")
966a5609669e Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
39 #define luaV_msg(L) luaV_msgfunc((L), (msgfunc_T) msg)
966a5609669e Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
40 #define luaV_emsg(L) luaV_msgfunc((L), (msgfunc_T) emsg)
966a5609669e Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
41
966a5609669e Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
42
966a5609669e Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
43 #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
44
f149bb1cf5be Make it possible to load Lua dynamically on Unix. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents: 2331
diff changeset
45 #ifndef WIN3264
f149bb1cf5be Make it possible to load Lua dynamically on Unix. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents: 2331
diff changeset
46 # include <dlfcn.h>
f149bb1cf5be Make it possible to load Lua dynamically on Unix. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents: 2331
diff changeset
47 # define HANDLE void*
f149bb1cf5be Make it possible to load Lua dynamically on Unix. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents: 2331
diff changeset
48 # 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
49 # 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
50 # 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
51 #else
f149bb1cf5be Make it possible to load Lua dynamically on Unix. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents: 2331
diff changeset
52 # define load_dll LoadLibrary
f149bb1cf5be Make it possible to load Lua dynamically on Unix. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents: 2331
diff changeset
53 # 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
54 # 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
55 #endif
f149bb1cf5be Make it possible to load Lua dynamically on Unix. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents: 2331
diff changeset
56
2320
966a5609669e Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
57 /* lauxlib */
966a5609669e Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
58 #define luaL_register dll_luaL_register
966a5609669e Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
59 #define luaL_typerror dll_luaL_typerror
966a5609669e Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
60 #define luaL_checklstring dll_luaL_checklstring
966a5609669e Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
61 #define luaL_checkinteger dll_luaL_checkinteger
966a5609669e Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
62 #define luaL_optinteger dll_luaL_optinteger
966a5609669e Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
63 #define luaL_checktype dll_luaL_checktype
966a5609669e Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
64 #define luaL_error dll_luaL_error
966a5609669e Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
65 #define luaL_loadfile dll_luaL_loadfile
966a5609669e Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
66 #define luaL_loadbuffer dll_luaL_loadbuffer
966a5609669e Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
67 #define luaL_newstate dll_luaL_newstate
966a5609669e Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
68 #define luaL_buffinit dll_luaL_buffinit
966a5609669e Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
69 #define luaL_prepbuffer dll_luaL_prepbuffer
966a5609669e Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
70 #define luaL_addlstring dll_luaL_addlstring
966a5609669e Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
71 #define luaL_pushresult dll_luaL_pushresult
966a5609669e Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
72 /* lua */
966a5609669e Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
73 #define lua_close dll_lua_close
966a5609669e Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
74 #define lua_gettop dll_lua_gettop
966a5609669e Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
75 #define lua_settop dll_lua_settop
966a5609669e Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
76 #define lua_pushvalue dll_lua_pushvalue
966a5609669e Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
77 #define lua_replace dll_lua_replace
966a5609669e Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
78 #define lua_isnumber dll_lua_isnumber
966a5609669e Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
79 #define lua_isstring dll_lua_isstring
966a5609669e Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
80 #define lua_type dll_lua_type
966a5609669e Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
81 #define lua_rawequal dll_lua_rawequal
966a5609669e Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
82 #define lua_tonumber dll_lua_tonumber
966a5609669e Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
83 #define lua_tointeger dll_lua_tointeger
966a5609669e Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
84 #define lua_toboolean dll_lua_toboolean
966a5609669e Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
85 #define lua_tolstring dll_lua_tolstring
966a5609669e Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
86 #define lua_touserdata dll_lua_touserdata
966a5609669e Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
87 #define lua_pushnil dll_lua_pushnil
966a5609669e Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
88 #define lua_pushnumber dll_lua_pushnumber
966a5609669e Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
89 #define lua_pushinteger dll_lua_pushinteger
966a5609669e Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
90 #define lua_pushlstring dll_lua_pushlstring
966a5609669e Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
91 #define lua_pushstring dll_lua_pushstring
966a5609669e Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
92 #define lua_pushfstring dll_lua_pushfstring
966a5609669e Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
93 #define lua_pushcclosure dll_lua_pushcclosure
966a5609669e Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
94 #define lua_pushboolean dll_lua_pushboolean
966a5609669e Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
95 #define lua_pushlightuserdata dll_lua_pushlightuserdata
966a5609669e Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
96 #define lua_getfield dll_lua_getfield
966a5609669e Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
97 #define lua_rawget dll_lua_rawget
966a5609669e Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
98 #define lua_createtable dll_lua_createtable
966a5609669e Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
99 #define lua_newuserdata dll_lua_newuserdata
966a5609669e Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
100 #define lua_getmetatable dll_lua_getmetatable
966a5609669e Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
101 #define lua_setfield dll_lua_setfield
966a5609669e Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
102 #define lua_rawset dll_lua_rawset
966a5609669e Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
103 #define lua_rawseti dll_lua_rawseti
966a5609669e Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
104 #define lua_setmetatable dll_lua_setmetatable
966a5609669e Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
105 #define lua_call dll_lua_call
966a5609669e Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
106 #define lua_pcall dll_lua_pcall
966a5609669e Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
107 /* libs */
966a5609669e Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
108 #define luaopen_base dll_luaopen_base
966a5609669e Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
109 #define luaopen_table dll_luaopen_table
966a5609669e Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
110 #define luaopen_string dll_luaopen_string
966a5609669e Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
111 #define luaopen_math dll_luaopen_math
966a5609669e Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
112 #define luaopen_os dll_luaopen_os
966a5609669e Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
113 #define luaopen_package dll_luaopen_package
966a5609669e Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
114 #define luaopen_debug dll_luaopen_debug
966a5609669e Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
115
966a5609669e Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
116 /* lauxlib */
966a5609669e Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
117 void (*dll_luaL_register) (lua_State *L, const char *libname, const luaL_Reg *l);
966a5609669e Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
118 int (*dll_luaL_typerror) (lua_State *L, int narg, const char *tname);
966a5609669e Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
119 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
120 lua_Integer (*dll_luaL_checkinteger) (lua_State *L, int numArg);
966a5609669e Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
121 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
122 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
123 int (*dll_luaL_error) (lua_State *L, const char *fmt, ...);
966a5609669e Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
124 int (*dll_luaL_loadfile) (lua_State *L, const char *filename);
966a5609669e Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
125 int (*dll_luaL_loadbuffer) (lua_State *L, const char *buff, size_t sz, const char *name);
966a5609669e Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
126 lua_State *(*dll_luaL_newstate) (void);
966a5609669e Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
127 void (*dll_luaL_buffinit) (lua_State *L, luaL_Buffer *B);
966a5609669e Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
128 char *(*dll_luaL_prepbuffer) (luaL_Buffer *B);
966a5609669e Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
129 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
130 void (*dll_luaL_pushresult) (luaL_Buffer *B);
966a5609669e Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
131 /* lua */
966a5609669e Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
132 void (*dll_lua_close) (lua_State *L);
966a5609669e Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
133 int (*dll_lua_gettop) (lua_State *L);
966a5609669e Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
134 void (*dll_lua_settop) (lua_State *L, int idx);
966a5609669e Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
135 void (*dll_lua_pushvalue) (lua_State *L, int idx);
966a5609669e Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
136 void (*dll_lua_replace) (lua_State *L, int idx);
966a5609669e Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
137 int (*dll_lua_isnumber) (lua_State *L, int idx);
966a5609669e Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
138 int (*dll_lua_isstring) (lua_State *L, int idx);
966a5609669e Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
139 int (*dll_lua_type) (lua_State *L, int idx);
966a5609669e Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
140 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
141 lua_Number (*dll_lua_tonumber) (lua_State *L, int idx);
966a5609669e Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
142 lua_Integer (*dll_lua_tointeger) (lua_State *L, int idx);
966a5609669e Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
143 int (*dll_lua_toboolean) (lua_State *L, int idx);
966a5609669e Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
144 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
145 void *(*dll_lua_touserdata) (lua_State *L, int idx);
966a5609669e Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
146 void (*dll_lua_pushnil) (lua_State *L);
966a5609669e Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
147 void (*dll_lua_pushnumber) (lua_State *L, lua_Number n);
966a5609669e Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
148 void (*dll_lua_pushinteger) (lua_State *L, lua_Integer n);
966a5609669e Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
149 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
150 void (*dll_lua_pushstring) (lua_State *L, const char *s);
966a5609669e Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
151 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
152 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
153 void (*dll_lua_pushboolean) (lua_State *L, int b);
966a5609669e Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
154 void (*dll_lua_pushlightuserdata) (lua_State *L, void *p);
966a5609669e Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
155 void (*dll_lua_getfield) (lua_State *L, int idx, const char *k);
966a5609669e Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
156 void (*dll_lua_rawget) (lua_State *L, int idx);
966a5609669e Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
157 void (*dll_lua_createtable) (lua_State *L, int narr, int nrec);
966a5609669e Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
158 void *(*dll_lua_newuserdata) (lua_State *L, size_t sz);
966a5609669e Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
159 int (*dll_lua_getmetatable) (lua_State *L, int objindex);
966a5609669e Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
160 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
161 void (*dll_lua_rawset) (lua_State *L, int idx);
966a5609669e Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
162 void (*dll_lua_rawseti) (lua_State *L, int idx, int n);
966a5609669e Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
163 int (*dll_lua_setmetatable) (lua_State *L, int objindex);
966a5609669e Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
164 void (*dll_lua_call) (lua_State *L, int nargs, int nresults);
966a5609669e Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
165 int (*dll_lua_pcall) (lua_State *L, int nargs, int nresults, int errfunc);
966a5609669e Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
166 /* libs */
966a5609669e Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
167 int (*dll_luaopen_base) (lua_State *L);
966a5609669e Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
168 int (*dll_luaopen_table) (lua_State *L);
966a5609669e Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
169 int (*dll_luaopen_string) (lua_State *L);
966a5609669e Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
170 int (*dll_luaopen_math) (lua_State *L);
966a5609669e Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
171 int (*dll_luaopen_os) (lua_State *L);
966a5609669e Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
172 int (*dll_luaopen_package) (lua_State *L);
966a5609669e Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
173 int (*dll_luaopen_debug) (lua_State *L);
966a5609669e Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
174
966a5609669e Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
175 typedef void **luaV_function;
966a5609669e Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
176 typedef struct {
966a5609669e Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
177 const char *name;
966a5609669e Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
178 luaV_function func;
966a5609669e Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
179 } luaV_Reg;
966a5609669e Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
180
966a5609669e Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
181 static const luaV_Reg luaV_dll[] = {
966a5609669e Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
182 /* lauxlib */
966a5609669e Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
183 {"luaL_register", (luaV_function) &dll_luaL_register},
966a5609669e Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
184 {"luaL_typerror", (luaV_function) &dll_luaL_typerror},
966a5609669e Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
185 {"luaL_checklstring", (luaV_function) &dll_luaL_checklstring},
966a5609669e Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
186 {"luaL_checkinteger", (luaV_function) &dll_luaL_checkinteger},
966a5609669e Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
187 {"luaL_optinteger", (luaV_function) &dll_luaL_optinteger},
966a5609669e Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
188 {"luaL_checktype", (luaV_function) &dll_luaL_checktype},
966a5609669e Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
189 {"luaL_error", (luaV_function) &dll_luaL_error},
966a5609669e Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
190 {"luaL_loadfile", (luaV_function) &dll_luaL_loadfile},
966a5609669e Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
191 {"luaL_loadbuffer", (luaV_function) &dll_luaL_loadbuffer},
966a5609669e Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
192 {"luaL_newstate", (luaV_function) &dll_luaL_newstate},
966a5609669e Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
193 {"luaL_buffinit", (luaV_function) &dll_luaL_buffinit},
966a5609669e Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
194 {"luaL_prepbuffer", (luaV_function) &dll_luaL_prepbuffer},
966a5609669e Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
195 {"luaL_addlstring", (luaV_function) &dll_luaL_addlstring},
966a5609669e Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
196 {"luaL_pushresult", (luaV_function) &dll_luaL_pushresult},
966a5609669e Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
197 /* lua */
966a5609669e Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
198 {"lua_close", (luaV_function) &dll_lua_close},
966a5609669e Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
199 {"lua_gettop", (luaV_function) &dll_lua_gettop},
966a5609669e Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
200 {"lua_settop", (luaV_function) &dll_lua_settop},
966a5609669e Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
201 {"lua_pushvalue", (luaV_function) &dll_lua_pushvalue},
966a5609669e Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
202 {"lua_replace", (luaV_function) &dll_lua_replace},
966a5609669e Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
203 {"lua_isnumber", (luaV_function) &dll_lua_isnumber},
966a5609669e Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
204 {"lua_isstring", (luaV_function) &dll_lua_isstring},
966a5609669e Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
205 {"lua_type", (luaV_function) &dll_lua_type},
966a5609669e Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
206 {"lua_rawequal", (luaV_function) &dll_lua_rawequal},
966a5609669e Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
207 {"lua_tonumber", (luaV_function) &dll_lua_tonumber},
966a5609669e Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
208 {"lua_tointeger", (luaV_function) &dll_lua_tointeger},
966a5609669e Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
209 {"lua_toboolean", (luaV_function) &dll_lua_toboolean},
966a5609669e Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
210 {"lua_tolstring", (luaV_function) &dll_lua_tolstring},
966a5609669e Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
211 {"lua_touserdata", (luaV_function) &dll_lua_touserdata},
966a5609669e Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
212 {"lua_pushnil", (luaV_function) &dll_lua_pushnil},
966a5609669e Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
213 {"lua_pushnumber", (luaV_function) &dll_lua_pushnumber},
966a5609669e Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
214 {"lua_pushinteger", (luaV_function) &dll_lua_pushinteger},
966a5609669e Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
215 {"lua_pushlstring", (luaV_function) &dll_lua_pushlstring},
966a5609669e Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
216 {"lua_pushstring", (luaV_function) &dll_lua_pushstring},
966a5609669e Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
217 {"lua_pushfstring", (luaV_function) &dll_lua_pushfstring},
966a5609669e Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
218 {"lua_pushcclosure", (luaV_function) &dll_lua_pushcclosure},
966a5609669e Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
219 {"lua_pushboolean", (luaV_function) &dll_lua_pushboolean},
966a5609669e Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
220 {"lua_pushlightuserdata", (luaV_function) &dll_lua_pushlightuserdata},
966a5609669e Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
221 {"lua_getfield", (luaV_function) &dll_lua_getfield},
966a5609669e Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
222 {"lua_rawget", (luaV_function) &dll_lua_rawget},
966a5609669e Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
223 {"lua_createtable", (luaV_function) &dll_lua_createtable},
966a5609669e Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
224 {"lua_newuserdata", (luaV_function) &dll_lua_newuserdata},
966a5609669e Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
225 {"lua_getmetatable", (luaV_function) &dll_lua_getmetatable},
966a5609669e Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
226 {"lua_setfield", (luaV_function) &dll_lua_setfield},
966a5609669e Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
227 {"lua_rawset", (luaV_function) &dll_lua_rawset},
966a5609669e Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
228 {"lua_rawseti", (luaV_function) &dll_lua_rawseti},
966a5609669e Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
229 {"lua_setmetatable", (luaV_function) &dll_lua_setmetatable},
966a5609669e Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
230 {"lua_call", (luaV_function) &dll_lua_call},
966a5609669e Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
231 {"lua_pcall", (luaV_function) &dll_lua_pcall},
966a5609669e Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
232 /* libs */
966a5609669e Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
233 {"luaopen_base", (luaV_function) &dll_luaopen_base},
966a5609669e Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
234 {"luaopen_table", (luaV_function) &dll_luaopen_table},
966a5609669e Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
235 {"luaopen_string", (luaV_function) &dll_luaopen_string},
966a5609669e Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
236 {"luaopen_math", (luaV_function) &dll_luaopen_math},
966a5609669e Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
237 {"luaopen_os", (luaV_function) &dll_luaopen_os},
966a5609669e Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
238 {"luaopen_package", (luaV_function) &dll_luaopen_package},
966a5609669e Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
239 {"luaopen_debug", (luaV_function) &dll_luaopen_debug},
966a5609669e Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
240 {NULL, NULL}
966a5609669e Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
241 };
966a5609669e Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
242
2373
f149bb1cf5be Make it possible to load Lua dynamically on Unix. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents: 2331
diff changeset
243 static HANDLE hinstLua = NULL;
2320
966a5609669e Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
244
2330
ea4bf6df1a8a Whitespace cleanup.
Bram Moolenaar <bram@vim.org>
parents: 2320
diff changeset
245 static void
ea4bf6df1a8a Whitespace cleanup.
Bram Moolenaar <bram@vim.org>
parents: 2320
diff changeset
246 end_dynamic_lua(void)
ea4bf6df1a8a Whitespace cleanup.
Bram Moolenaar <bram@vim.org>
parents: 2320
diff changeset
247 {
ea4bf6df1a8a Whitespace cleanup.
Bram Moolenaar <bram@vim.org>
parents: 2320
diff changeset
248 if (hinstLua)
ea4bf6df1a8a Whitespace cleanup.
Bram Moolenaar <bram@vim.org>
parents: 2320
diff changeset
249 {
2373
f149bb1cf5be Make it possible to load Lua dynamically on Unix. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents: 2331
diff changeset
250 close_dll(hinstLua);
2330
ea4bf6df1a8a Whitespace cleanup.
Bram Moolenaar <bram@vim.org>
parents: 2320
diff changeset
251 hinstLua = 0;
2320
966a5609669e Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
252 }
966a5609669e Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
253 }
966a5609669e Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
254
2330
ea4bf6df1a8a Whitespace cleanup.
Bram Moolenaar <bram@vim.org>
parents: 2320
diff changeset
255 static int
ea4bf6df1a8a Whitespace cleanup.
Bram Moolenaar <bram@vim.org>
parents: 2320
diff changeset
256 lua_link_init(char *libname, int verbose)
ea4bf6df1a8a Whitespace cleanup.
Bram Moolenaar <bram@vim.org>
parents: 2320
diff changeset
257 {
2320
966a5609669e Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
258 const luaV_Reg *reg;
966a5609669e Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
259 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
260 hinstLua = load_dll(libname);
2330
ea4bf6df1a8a Whitespace cleanup.
Bram Moolenaar <bram@vim.org>
parents: 2320
diff changeset
261 if (!hinstLua)
ea4bf6df1a8a Whitespace cleanup.
Bram Moolenaar <bram@vim.org>
parents: 2320
diff changeset
262 {
ea4bf6df1a8a Whitespace cleanup.
Bram Moolenaar <bram@vim.org>
parents: 2320
diff changeset
263 if (verbose)
ea4bf6df1a8a Whitespace cleanup.
Bram Moolenaar <bram@vim.org>
parents: 2320
diff changeset
264 EMSG2(_(e_loadlib), libname);
ea4bf6df1a8a Whitespace cleanup.
Bram Moolenaar <bram@vim.org>
parents: 2320
diff changeset
265 return FAIL;
2320
966a5609669e Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
266 }
2330
ea4bf6df1a8a Whitespace cleanup.
Bram Moolenaar <bram@vim.org>
parents: 2320
diff changeset
267 for (reg = luaV_dll; reg->func; reg++)
ea4bf6df1a8a Whitespace cleanup.
Bram Moolenaar <bram@vim.org>
parents: 2320
diff changeset
268 {
2373
f149bb1cf5be Make it possible to load Lua dynamically on Unix. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents: 2331
diff changeset
269 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
270 {
f149bb1cf5be Make it possible to load Lua dynamically on Unix. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents: 2331
diff changeset
271 close_dll(hinstLua);
2330
ea4bf6df1a8a Whitespace cleanup.
Bram Moolenaar <bram@vim.org>
parents: 2320
diff changeset
272 hinstLua = 0;
ea4bf6df1a8a Whitespace cleanup.
Bram Moolenaar <bram@vim.org>
parents: 2320
diff changeset
273 if (verbose)
ea4bf6df1a8a Whitespace cleanup.
Bram Moolenaar <bram@vim.org>
parents: 2320
diff changeset
274 EMSG2(_(e_loadfunc), reg->name);
ea4bf6df1a8a Whitespace cleanup.
Bram Moolenaar <bram@vim.org>
parents: 2320
diff changeset
275 return FAIL;
ea4bf6df1a8a Whitespace cleanup.
Bram Moolenaar <bram@vim.org>
parents: 2320
diff changeset
276 }
2320
966a5609669e Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
277 }
966a5609669e Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
278 return OK;
966a5609669e Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
279 }
966a5609669e Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
280
2330
ea4bf6df1a8a Whitespace cleanup.
Bram Moolenaar <bram@vim.org>
parents: 2320
diff changeset
281 int
ea4bf6df1a8a Whitespace cleanup.
Bram Moolenaar <bram@vim.org>
parents: 2320
diff changeset
282 lua_enabled(int verbose)
ea4bf6df1a8a Whitespace cleanup.
Bram Moolenaar <bram@vim.org>
parents: 2320
diff changeset
283 {
2320
966a5609669e Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
284 return lua_link_init(DYNAMIC_LUA_DLL, verbose) == OK;
966a5609669e Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
285 }
966a5609669e Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
286
966a5609669e Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
287 #endif /* DYNAMIC_LUA */
966a5609669e Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
288
966a5609669e Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
289
966a5609669e Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
290 /* ======= Internal ======= */
966a5609669e Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
291
2330
ea4bf6df1a8a Whitespace cleanup.
Bram Moolenaar <bram@vim.org>
parents: 2320
diff changeset
292 static void
ea4bf6df1a8a Whitespace cleanup.
Bram Moolenaar <bram@vim.org>
parents: 2320
diff changeset
293 luaV_newmetatable(lua_State *L, const char *tname)
ea4bf6df1a8a Whitespace cleanup.
Bram Moolenaar <bram@vim.org>
parents: 2320
diff changeset
294 {
2320
966a5609669e Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
295 lua_newtable(L);
966a5609669e Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
296 lua_pushlightuserdata(L, (void *) tname);
966a5609669e Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
297 lua_pushvalue(L, -2);
966a5609669e Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
298 lua_rawset(L, LUA_REGISTRYINDEX);
966a5609669e Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
299 }
966a5609669e Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
300
2330
ea4bf6df1a8a Whitespace cleanup.
Bram Moolenaar <bram@vim.org>
parents: 2320
diff changeset
301 static void *
ea4bf6df1a8a Whitespace cleanup.
Bram Moolenaar <bram@vim.org>
parents: 2320
diff changeset
302 luaV_toudata(lua_State *L, int ud, const char *tname)
ea4bf6df1a8a Whitespace cleanup.
Bram Moolenaar <bram@vim.org>
parents: 2320
diff changeset
303 {
2320
966a5609669e Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
304 void *p = lua_touserdata(L, ud);
2330
ea4bf6df1a8a Whitespace cleanup.
Bram Moolenaar <bram@vim.org>
parents: 2320
diff changeset
305
ea4bf6df1a8a Whitespace cleanup.
Bram Moolenaar <bram@vim.org>
parents: 2320
diff changeset
306 if (p != NULL) /* value is userdata? */
ea4bf6df1a8a Whitespace cleanup.
Bram Moolenaar <bram@vim.org>
parents: 2320
diff changeset
307 {
ea4bf6df1a8a Whitespace cleanup.
Bram Moolenaar <bram@vim.org>
parents: 2320
diff changeset
308 if (lua_getmetatable(L, ud)) /* does it have a metatable? */
ea4bf6df1a8a Whitespace cleanup.
Bram Moolenaar <bram@vim.org>
parents: 2320
diff changeset
309 {
ea4bf6df1a8a Whitespace cleanup.
Bram Moolenaar <bram@vim.org>
parents: 2320
diff changeset
310 luaV_getfield(L, tname); /* get metatable */
ea4bf6df1a8a Whitespace cleanup.
Bram Moolenaar <bram@vim.org>
parents: 2320
diff changeset
311 if (lua_rawequal(L, -1, -2)) /* MTs match? */
ea4bf6df1a8a Whitespace cleanup.
Bram Moolenaar <bram@vim.org>
parents: 2320
diff changeset
312 {
ea4bf6df1a8a Whitespace cleanup.
Bram Moolenaar <bram@vim.org>
parents: 2320
diff changeset
313 lua_pop(L, 2); /* MTs */
ea4bf6df1a8a Whitespace cleanup.
Bram Moolenaar <bram@vim.org>
parents: 2320
diff changeset
314 return p;
ea4bf6df1a8a Whitespace cleanup.
Bram Moolenaar <bram@vim.org>
parents: 2320
diff changeset
315 }
ea4bf6df1a8a Whitespace cleanup.
Bram Moolenaar <bram@vim.org>
parents: 2320
diff changeset
316 }
2320
966a5609669e Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
317 }
966a5609669e Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
318 return NULL;
966a5609669e Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
319 }
966a5609669e Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
320
2330
ea4bf6df1a8a Whitespace cleanup.
Bram Moolenaar <bram@vim.org>
parents: 2320
diff changeset
321 static void *
ea4bf6df1a8a Whitespace cleanup.
Bram Moolenaar <bram@vim.org>
parents: 2320
diff changeset
322 luaV_checkudata(lua_State *L, int ud, const char *tname)
ea4bf6df1a8a Whitespace cleanup.
Bram Moolenaar <bram@vim.org>
parents: 2320
diff changeset
323 {
2320
966a5609669e Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
324 void *p = luaV_toudata(L, ud, tname);
966a5609669e Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
325 if (p == NULL) luaL_typerror(L, ud, tname);
966a5609669e Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
326 return p;
966a5609669e Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
327 }
966a5609669e Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
328
2330
ea4bf6df1a8a Whitespace cleanup.
Bram Moolenaar <bram@vim.org>
parents: 2320
diff changeset
329 static void
ea4bf6df1a8a Whitespace cleanup.
Bram Moolenaar <bram@vim.org>
parents: 2320
diff changeset
330 luaV_pushtypval(lua_State *L, typval_T *tv)
ea4bf6df1a8a Whitespace cleanup.
Bram Moolenaar <bram@vim.org>
parents: 2320
diff changeset
331 {
2320
966a5609669e Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
332 if (tv == NULL) luaL_error(L, "null type");
2330
ea4bf6df1a8a Whitespace cleanup.
Bram Moolenaar <bram@vim.org>
parents: 2320
diff changeset
333 switch (tv->v_type)
ea4bf6df1a8a Whitespace cleanup.
Bram Moolenaar <bram@vim.org>
parents: 2320
diff changeset
334 {
ea4bf6df1a8a Whitespace cleanup.
Bram Moolenaar <bram@vim.org>
parents: 2320
diff changeset
335 case VAR_STRING:
ea4bf6df1a8a Whitespace cleanup.
Bram Moolenaar <bram@vim.org>
parents: 2320
diff changeset
336 lua_pushstring(L, (char *) tv->vval.v_string);
ea4bf6df1a8a Whitespace cleanup.
Bram Moolenaar <bram@vim.org>
parents: 2320
diff changeset
337 break;
ea4bf6df1a8a Whitespace cleanup.
Bram Moolenaar <bram@vim.org>
parents: 2320
diff changeset
338 case VAR_NUMBER:
ea4bf6df1a8a Whitespace cleanup.
Bram Moolenaar <bram@vim.org>
parents: 2320
diff changeset
339 lua_pushinteger(L, (int) tv->vval.v_number);
ea4bf6df1a8a Whitespace cleanup.
Bram Moolenaar <bram@vim.org>
parents: 2320
diff changeset
340 break;
2320
966a5609669e Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
341 #ifdef FEAT_FLOAT
2330
ea4bf6df1a8a Whitespace cleanup.
Bram Moolenaar <bram@vim.org>
parents: 2320
diff changeset
342 case VAR_FLOAT:
ea4bf6df1a8a Whitespace cleanup.
Bram Moolenaar <bram@vim.org>
parents: 2320
diff changeset
343 lua_pushnumber(L, (lua_Number) tv->vval.v_float);
ea4bf6df1a8a Whitespace cleanup.
Bram Moolenaar <bram@vim.org>
parents: 2320
diff changeset
344 break;
2320
966a5609669e Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
345 #endif
2330
ea4bf6df1a8a Whitespace cleanup.
Bram Moolenaar <bram@vim.org>
parents: 2320
diff changeset
346 case VAR_LIST: {
ea4bf6df1a8a Whitespace cleanup.
Bram Moolenaar <bram@vim.org>
parents: 2320
diff changeset
347 list_T *l = tv->vval.v_list;
ea4bf6df1a8a Whitespace cleanup.
Bram Moolenaar <bram@vim.org>
parents: 2320
diff changeset
348
ea4bf6df1a8a Whitespace cleanup.
Bram Moolenaar <bram@vim.org>
parents: 2320
diff changeset
349 if (l != NULL)
ea4bf6df1a8a Whitespace cleanup.
Bram Moolenaar <bram@vim.org>
parents: 2320
diff changeset
350 {
ea4bf6df1a8a Whitespace cleanup.
Bram Moolenaar <bram@vim.org>
parents: 2320
diff changeset
351 /* check cache */
ea4bf6df1a8a Whitespace cleanup.
Bram Moolenaar <bram@vim.org>
parents: 2320
diff changeset
352 lua_pushlightuserdata(L, (void *) l);
ea4bf6df1a8a Whitespace cleanup.
Bram Moolenaar <bram@vim.org>
parents: 2320
diff changeset
353 lua_rawget(L, LUA_ENVIRONINDEX);
ea4bf6df1a8a Whitespace cleanup.
Bram Moolenaar <bram@vim.org>
parents: 2320
diff changeset
354 if (lua_isnil(L, -1)) /* not interned? */
ea4bf6df1a8a Whitespace cleanup.
Bram Moolenaar <bram@vim.org>
parents: 2320
diff changeset
355 {
ea4bf6df1a8a Whitespace cleanup.
Bram Moolenaar <bram@vim.org>
parents: 2320
diff changeset
356 listitem_T *li;
ea4bf6df1a8a Whitespace cleanup.
Bram Moolenaar <bram@vim.org>
parents: 2320
diff changeset
357 int n = 0;
ea4bf6df1a8a Whitespace cleanup.
Bram Moolenaar <bram@vim.org>
parents: 2320
diff changeset
358 lua_pop(L, 1); /* nil */
ea4bf6df1a8a Whitespace cleanup.
Bram Moolenaar <bram@vim.org>
parents: 2320
diff changeset
359 lua_newtable(L);
ea4bf6df1a8a Whitespace cleanup.
Bram Moolenaar <bram@vim.org>
parents: 2320
diff changeset
360 lua_pushlightuserdata(L, (void *) l);
ea4bf6df1a8a Whitespace cleanup.
Bram Moolenaar <bram@vim.org>
parents: 2320
diff changeset
361 lua_pushvalue(L, -2);
ea4bf6df1a8a Whitespace cleanup.
Bram Moolenaar <bram@vim.org>
parents: 2320
diff changeset
362 lua_rawset(L, LUA_ENVIRONINDEX);
ea4bf6df1a8a Whitespace cleanup.
Bram Moolenaar <bram@vim.org>
parents: 2320
diff changeset
363 for (li = l->lv_first; li != NULL; li = li->li_next)
ea4bf6df1a8a Whitespace cleanup.
Bram Moolenaar <bram@vim.org>
parents: 2320
diff changeset
364 {
ea4bf6df1a8a Whitespace cleanup.
Bram Moolenaar <bram@vim.org>
parents: 2320
diff changeset
365 luaV_pushtypval(L, &li->li_tv);
ea4bf6df1a8a Whitespace cleanup.
Bram Moolenaar <bram@vim.org>
parents: 2320
diff changeset
366 lua_rawseti(L, -2, ++n);
ea4bf6df1a8a Whitespace cleanup.
Bram Moolenaar <bram@vim.org>
parents: 2320
diff changeset
367 }
ea4bf6df1a8a Whitespace cleanup.
Bram Moolenaar <bram@vim.org>
parents: 2320
diff changeset
368 }
ea4bf6df1a8a Whitespace cleanup.
Bram Moolenaar <bram@vim.org>
parents: 2320
diff changeset
369 }
ea4bf6df1a8a Whitespace cleanup.
Bram Moolenaar <bram@vim.org>
parents: 2320
diff changeset
370 else lua_pushnil(L);
ea4bf6df1a8a Whitespace cleanup.
Bram Moolenaar <bram@vim.org>
parents: 2320
diff changeset
371 break;
ea4bf6df1a8a Whitespace cleanup.
Bram Moolenaar <bram@vim.org>
parents: 2320
diff changeset
372 }
ea4bf6df1a8a Whitespace cleanup.
Bram Moolenaar <bram@vim.org>
parents: 2320
diff changeset
373 case VAR_DICT: {
ea4bf6df1a8a Whitespace cleanup.
Bram Moolenaar <bram@vim.org>
parents: 2320
diff changeset
374 dict_T *d = tv->vval.v_dict;
ea4bf6df1a8a Whitespace cleanup.
Bram Moolenaar <bram@vim.org>
parents: 2320
diff changeset
375
ea4bf6df1a8a Whitespace cleanup.
Bram Moolenaar <bram@vim.org>
parents: 2320
diff changeset
376 if (d != NULL)
ea4bf6df1a8a Whitespace cleanup.
Bram Moolenaar <bram@vim.org>
parents: 2320
diff changeset
377 {
ea4bf6df1a8a Whitespace cleanup.
Bram Moolenaar <bram@vim.org>
parents: 2320
diff changeset
378 /* check cache */
ea4bf6df1a8a Whitespace cleanup.
Bram Moolenaar <bram@vim.org>
parents: 2320
diff changeset
379 lua_pushlightuserdata(L, (void *) d);
ea4bf6df1a8a Whitespace cleanup.
Bram Moolenaar <bram@vim.org>
parents: 2320
diff changeset
380 lua_rawget(L, LUA_ENVIRONINDEX);
2373
f149bb1cf5be Make it possible to load Lua dynamically on Unix. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents: 2331
diff changeset
381 if (lua_isnil(L, -1)) /* not interned? */
f149bb1cf5be Make it possible to load Lua dynamically on Unix. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents: 2331
diff changeset
382 {
2330
ea4bf6df1a8a Whitespace cleanup.
Bram Moolenaar <bram@vim.org>
parents: 2320
diff changeset
383 hashtab_T *ht = &d->dv_hashtab;
ea4bf6df1a8a Whitespace cleanup.
Bram Moolenaar <bram@vim.org>
parents: 2320
diff changeset
384 hashitem_T *hi;
ea4bf6df1a8a Whitespace cleanup.
Bram Moolenaar <bram@vim.org>
parents: 2320
diff changeset
385 int n = ht->ht_used; /* remaining items */
ea4bf6df1a8a Whitespace cleanup.
Bram Moolenaar <bram@vim.org>
parents: 2320
diff changeset
386 lua_pop(L, 1); /* nil */
ea4bf6df1a8a Whitespace cleanup.
Bram Moolenaar <bram@vim.org>
parents: 2320
diff changeset
387 lua_newtable(L);
ea4bf6df1a8a Whitespace cleanup.
Bram Moolenaar <bram@vim.org>
parents: 2320
diff changeset
388 lua_pushlightuserdata(L, (void *) d);
ea4bf6df1a8a Whitespace cleanup.
Bram Moolenaar <bram@vim.org>
parents: 2320
diff changeset
389 lua_pushvalue(L, -2);
ea4bf6df1a8a Whitespace cleanup.
Bram Moolenaar <bram@vim.org>
parents: 2320
diff changeset
390 lua_rawset(L, LUA_ENVIRONINDEX);
ea4bf6df1a8a Whitespace cleanup.
Bram Moolenaar <bram@vim.org>
parents: 2320
diff changeset
391 for (hi = ht->ht_array; n > 0; hi++)
ea4bf6df1a8a Whitespace cleanup.
Bram Moolenaar <bram@vim.org>
parents: 2320
diff changeset
392 {
ea4bf6df1a8a Whitespace cleanup.
Bram Moolenaar <bram@vim.org>
parents: 2320
diff changeset
393 if (!HASHITEM_EMPTY(hi))
ea4bf6df1a8a Whitespace cleanup.
Bram Moolenaar <bram@vim.org>
parents: 2320
diff changeset
394 {
ea4bf6df1a8a Whitespace cleanup.
Bram Moolenaar <bram@vim.org>
parents: 2320
diff changeset
395 dictitem_T *di = dict_lookup(hi);
ea4bf6df1a8a Whitespace cleanup.
Bram Moolenaar <bram@vim.org>
parents: 2320
diff changeset
396 luaV_pushtypval(L, &di->di_tv);
ea4bf6df1a8a Whitespace cleanup.
Bram Moolenaar <bram@vim.org>
parents: 2320
diff changeset
397 lua_setfield(L, -2, (char *) hi->hi_key);
ea4bf6df1a8a Whitespace cleanup.
Bram Moolenaar <bram@vim.org>
parents: 2320
diff changeset
398 n--;
ea4bf6df1a8a Whitespace cleanup.
Bram Moolenaar <bram@vim.org>
parents: 2320
diff changeset
399 }
ea4bf6df1a8a Whitespace cleanup.
Bram Moolenaar <bram@vim.org>
parents: 2320
diff changeset
400 }
ea4bf6df1a8a Whitespace cleanup.
Bram Moolenaar <bram@vim.org>
parents: 2320
diff changeset
401 }
ea4bf6df1a8a Whitespace cleanup.
Bram Moolenaar <bram@vim.org>
parents: 2320
diff changeset
402 }
ea4bf6df1a8a Whitespace cleanup.
Bram Moolenaar <bram@vim.org>
parents: 2320
diff changeset
403 else lua_pushnil(L);
ea4bf6df1a8a Whitespace cleanup.
Bram Moolenaar <bram@vim.org>
parents: 2320
diff changeset
404 break;
ea4bf6df1a8a Whitespace cleanup.
Bram Moolenaar <bram@vim.org>
parents: 2320
diff changeset
405 }
ea4bf6df1a8a Whitespace cleanup.
Bram Moolenaar <bram@vim.org>
parents: 2320
diff changeset
406 default:
ea4bf6df1a8a Whitespace cleanup.
Bram Moolenaar <bram@vim.org>
parents: 2320
diff changeset
407 luaL_error(L, "invalid type");
2320
966a5609669e Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
408 }
966a5609669e Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
409 }
966a5609669e Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
410
966a5609669e Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
411 /* similar to luaL_addlstring, but replaces \0 with \n if toline and
966a5609669e Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
412 * \n with \0 otherwise */
2330
ea4bf6df1a8a Whitespace cleanup.
Bram Moolenaar <bram@vim.org>
parents: 2320
diff changeset
413 static void
ea4bf6df1a8a Whitespace cleanup.
Bram Moolenaar <bram@vim.org>
parents: 2320
diff changeset
414 luaV_addlstring(luaL_Buffer *b, const char *s, size_t l, int toline)
ea4bf6df1a8a Whitespace cleanup.
Bram Moolenaar <bram@vim.org>
parents: 2320
diff changeset
415 {
ea4bf6df1a8a Whitespace cleanup.
Bram Moolenaar <bram@vim.org>
parents: 2320
diff changeset
416 while (l--)
ea4bf6df1a8a Whitespace cleanup.
Bram Moolenaar <bram@vim.org>
parents: 2320
diff changeset
417 {
ea4bf6df1a8a Whitespace cleanup.
Bram Moolenaar <bram@vim.org>
parents: 2320
diff changeset
418 if (*s == '\0' && toline)
ea4bf6df1a8a Whitespace cleanup.
Bram Moolenaar <bram@vim.org>
parents: 2320
diff changeset
419 luaL_addchar(b, '\n');
ea4bf6df1a8a Whitespace cleanup.
Bram Moolenaar <bram@vim.org>
parents: 2320
diff changeset
420 else if (*s == '\n' && !toline)
ea4bf6df1a8a Whitespace cleanup.
Bram Moolenaar <bram@vim.org>
parents: 2320
diff changeset
421 luaL_addchar(b, '\0');
ea4bf6df1a8a Whitespace cleanup.
Bram Moolenaar <bram@vim.org>
parents: 2320
diff changeset
422 else
ea4bf6df1a8a Whitespace cleanup.
Bram Moolenaar <bram@vim.org>
parents: 2320
diff changeset
423 luaL_addchar(b, *s);
ea4bf6df1a8a Whitespace cleanup.
Bram Moolenaar <bram@vim.org>
parents: 2320
diff changeset
424 s++;
2320
966a5609669e Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
425 }
966a5609669e Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
426 }
966a5609669e Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
427
2330
ea4bf6df1a8a Whitespace cleanup.
Bram Moolenaar <bram@vim.org>
parents: 2320
diff changeset
428 static void
ea4bf6df1a8a Whitespace cleanup.
Bram Moolenaar <bram@vim.org>
parents: 2320
diff changeset
429 luaV_pushline(lua_State *L, buf_T *buf, linenr_T n)
ea4bf6df1a8a Whitespace cleanup.
Bram Moolenaar <bram@vim.org>
parents: 2320
diff changeset
430 {
2320
966a5609669e Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
431 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
432 luaL_Buffer b;
966a5609669e Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
433 luaL_buffinit(L, &b);
966a5609669e Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
434 luaV_addlstring(&b, s, strlen(s), 0);
966a5609669e Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
435 luaL_pushresult(&b);
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
2330
ea4bf6df1a8a Whitespace cleanup.
Bram Moolenaar <bram@vim.org>
parents: 2320
diff changeset
438 static char_u *
ea4bf6df1a8a Whitespace cleanup.
Bram Moolenaar <bram@vim.org>
parents: 2320
diff changeset
439 luaV_toline(lua_State *L, int pos)
ea4bf6df1a8a Whitespace cleanup.
Bram Moolenaar <bram@vim.org>
parents: 2320
diff changeset
440 {
2320
966a5609669e Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
441 size_t l;
966a5609669e Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
442 const char *s = lua_tolstring(L, pos, &l);
2330
ea4bf6df1a8a Whitespace cleanup.
Bram Moolenaar <bram@vim.org>
parents: 2320
diff changeset
443
2320
966a5609669e Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
444 luaL_Buffer b;
966a5609669e Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
445 luaL_buffinit(L, &b);
966a5609669e Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
446 luaV_addlstring(&b, s, l, 1);
966a5609669e Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
447 luaL_pushresult(&b);
966a5609669e Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
448 return (char_u *) lua_tostring(L, -1);
966a5609669e Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
449 }
966a5609669e Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
450
966a5609669e Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
451 /* pops a string s from the top of the stack and calls mf(t) for pieces t of
966a5609669e Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
452 * s separated by newlines */
2330
ea4bf6df1a8a Whitespace cleanup.
Bram Moolenaar <bram@vim.org>
parents: 2320
diff changeset
453 static void
ea4bf6df1a8a Whitespace cleanup.
Bram Moolenaar <bram@vim.org>
parents: 2320
diff changeset
454 luaV_msgfunc(lua_State *L, msgfunc_T mf)
ea4bf6df1a8a Whitespace cleanup.
Bram Moolenaar <bram@vim.org>
parents: 2320
diff changeset
455 {
2320
966a5609669e Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
456 luaL_Buffer b;
966a5609669e Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
457 size_t l;
966a5609669e Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
458 const char *p, *s = lua_tolstring(L, -1, &l);
966a5609669e Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
459 luaL_buffinit(L, &b);
966a5609669e Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
460 luaV_addlstring(&b, s, l, 0);
966a5609669e Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
461 luaL_pushresult(&b);
966a5609669e Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
462 /* break string */
966a5609669e Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
463 p = s = lua_tolstring(L, -1, &l);
2330
ea4bf6df1a8a Whitespace cleanup.
Bram Moolenaar <bram@vim.org>
parents: 2320
diff changeset
464 while (l--)
ea4bf6df1a8a Whitespace cleanup.
Bram Moolenaar <bram@vim.org>
parents: 2320
diff changeset
465 {
ea4bf6df1a8a Whitespace cleanup.
Bram Moolenaar <bram@vim.org>
parents: 2320
diff changeset
466 if (*p++ == '\0') /* break? */
ea4bf6df1a8a Whitespace cleanup.
Bram Moolenaar <bram@vim.org>
parents: 2320
diff changeset
467 {
ea4bf6df1a8a Whitespace cleanup.
Bram Moolenaar <bram@vim.org>
parents: 2320
diff changeset
468 mf((char_u *) s);
ea4bf6df1a8a Whitespace cleanup.
Bram Moolenaar <bram@vim.org>
parents: 2320
diff changeset
469 s = p;
ea4bf6df1a8a Whitespace cleanup.
Bram Moolenaar <bram@vim.org>
parents: 2320
diff changeset
470 }
2320
966a5609669e Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
471 }
966a5609669e Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
472 mf((char_u *) s);
966a5609669e Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
473 lua_pop(L, 2); /* original and modified strings */
966a5609669e Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
474 }
966a5609669e Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
475
966a5609669e Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
476
966a5609669e Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
477 /* ======= Buffer type ======= */
966a5609669e Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
478
2330
ea4bf6df1a8a Whitespace cleanup.
Bram Moolenaar <bram@vim.org>
parents: 2320
diff changeset
479 static luaV_Buffer *
ea4bf6df1a8a Whitespace cleanup.
Bram Moolenaar <bram@vim.org>
parents: 2320
diff changeset
480 luaV_newbuffer(lua_State *L, buf_T *buf)
ea4bf6df1a8a Whitespace cleanup.
Bram Moolenaar <bram@vim.org>
parents: 2320
diff changeset
481 {
2320
966a5609669e Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
482 luaV_Buffer *b = (luaV_Buffer *) lua_newuserdata(L, sizeof(luaV_Buffer));
966a5609669e Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
483 *b = buf;
966a5609669e Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
484 lua_pushlightuserdata(L, (void *) buf);
966a5609669e Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
485 lua_pushvalue(L, -2);
966a5609669e Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
486 lua_rawset(L, LUA_ENVIRONINDEX); /* env[buf] = udata */
966a5609669e Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
487 /* to avoid GC, store as key in env */
966a5609669e Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
488 lua_pushvalue(L, -1);
966a5609669e Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
489 lua_pushboolean(L, 1);
966a5609669e Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
490 lua_rawset(L, LUA_ENVIRONINDEX); /* env[udata] = true */
966a5609669e Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
491 /* set metatable */
966a5609669e Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
492 luaV_getfield(L, LUAVIM_BUFFER);
966a5609669e Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
493 lua_setmetatable(L, -2);
966a5609669e Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
494 return b;
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
ea4bf6df1a8a Whitespace cleanup.
Bram Moolenaar <bram@vim.org>
parents: 2320
diff changeset
497 static luaV_Buffer *
ea4bf6df1a8a Whitespace cleanup.
Bram Moolenaar <bram@vim.org>
parents: 2320
diff changeset
498 luaV_pushbuffer (lua_State *L, buf_T *buf)
ea4bf6df1a8a Whitespace cleanup.
Bram Moolenaar <bram@vim.org>
parents: 2320
diff changeset
499 {
2320
966a5609669e Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
500 luaV_Buffer *b = NULL;
966a5609669e Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
501 if (buf == NULL)
2330
ea4bf6df1a8a Whitespace cleanup.
Bram Moolenaar <bram@vim.org>
parents: 2320
diff changeset
502 lua_pushnil(L);
2320
966a5609669e Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
503 else {
2330
ea4bf6df1a8a Whitespace cleanup.
Bram Moolenaar <bram@vim.org>
parents: 2320
diff changeset
504 lua_pushlightuserdata(L, (void *) buf);
ea4bf6df1a8a Whitespace cleanup.
Bram Moolenaar <bram@vim.org>
parents: 2320
diff changeset
505 lua_rawget(L, LUA_ENVIRONINDEX);
ea4bf6df1a8a Whitespace cleanup.
Bram Moolenaar <bram@vim.org>
parents: 2320
diff changeset
506 if (lua_isnil(L, -1)) /* not interned? */
ea4bf6df1a8a Whitespace cleanup.
Bram Moolenaar <bram@vim.org>
parents: 2320
diff changeset
507 {
ea4bf6df1a8a Whitespace cleanup.
Bram Moolenaar <bram@vim.org>
parents: 2320
diff changeset
508 lua_pop(L, 1);
ea4bf6df1a8a Whitespace cleanup.
Bram Moolenaar <bram@vim.org>
parents: 2320
diff changeset
509 b = luaV_newbuffer(L, buf);
ea4bf6df1a8a Whitespace cleanup.
Bram Moolenaar <bram@vim.org>
parents: 2320
diff changeset
510 }
ea4bf6df1a8a Whitespace cleanup.
Bram Moolenaar <bram@vim.org>
parents: 2320
diff changeset
511 else
ea4bf6df1a8a Whitespace cleanup.
Bram Moolenaar <bram@vim.org>
parents: 2320
diff changeset
512 b = (luaV_Buffer *) lua_touserdata(L, -1);
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 b;
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
966a5609669e Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
517 /* Buffer metamethods */
966a5609669e Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
518
2330
ea4bf6df1a8a Whitespace cleanup.
Bram Moolenaar <bram@vim.org>
parents: 2320
diff changeset
519 static int
ea4bf6df1a8a Whitespace cleanup.
Bram Moolenaar <bram@vim.org>
parents: 2320
diff changeset
520 luaV_buffer_tostring(lua_State *L)
ea4bf6df1a8a Whitespace cleanup.
Bram Moolenaar <bram@vim.org>
parents: 2320
diff changeset
521 {
2320
966a5609669e Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
522 lua_pushfstring(L, "%s: %p", LUAVIM_BUFFER, lua_touserdata(L, 1));
966a5609669e Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
523 return 1;
966a5609669e Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
524 }
966a5609669e Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
525
2330
ea4bf6df1a8a Whitespace cleanup.
Bram Moolenaar <bram@vim.org>
parents: 2320
diff changeset
526 static int
ea4bf6df1a8a Whitespace cleanup.
Bram Moolenaar <bram@vim.org>
parents: 2320
diff changeset
527 luaV_buffer_len(lua_State *L)
ea4bf6df1a8a Whitespace cleanup.
Bram Moolenaar <bram@vim.org>
parents: 2320
diff changeset
528 {
2320
966a5609669e Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
529 luaV_Buffer *b = lua_touserdata(L, 1);
966a5609669e Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
530 lua_pushinteger(L, (*b)->b_ml.ml_line_count);
966a5609669e Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
531 return 1;
966a5609669e Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
532 }
966a5609669e Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
533
2330
ea4bf6df1a8a Whitespace cleanup.
Bram Moolenaar <bram@vim.org>
parents: 2320
diff changeset
534 static int
ea4bf6df1a8a Whitespace cleanup.
Bram Moolenaar <bram@vim.org>
parents: 2320
diff changeset
535 luaV_buffer_call(lua_State *L)
ea4bf6df1a8a Whitespace cleanup.
Bram Moolenaar <bram@vim.org>
parents: 2320
diff changeset
536 {
2320
966a5609669e Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
537 luaV_Buffer *b = (luaV_Buffer *) lua_touserdata(L, 1);
966a5609669e Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
538 lua_settop(L, 1);
966a5609669e Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
539 set_curbuf(*b, DOBUF_SPLIT);
966a5609669e Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
540 return 1;
966a5609669e Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
541 }
966a5609669e Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
542
2330
ea4bf6df1a8a Whitespace cleanup.
Bram Moolenaar <bram@vim.org>
parents: 2320
diff changeset
543 static int
ea4bf6df1a8a Whitespace cleanup.
Bram Moolenaar <bram@vim.org>
parents: 2320
diff changeset
544 luaV_buffer_index(lua_State *L)
ea4bf6df1a8a Whitespace cleanup.
Bram Moolenaar <bram@vim.org>
parents: 2320
diff changeset
545 {
2320
966a5609669e Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
546 luaV_Buffer *b = (luaV_Buffer *) lua_touserdata(L, 1);
966a5609669e Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
547 linenr_T n = (linenr_T) lua_tointeger(L, 2);
966a5609669e Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
548 if (n > 0 && n <= (*b)->b_ml.ml_line_count)
2330
ea4bf6df1a8a Whitespace cleanup.
Bram Moolenaar <bram@vim.org>
parents: 2320
diff changeset
549 luaV_pushline(L, *b, n);
ea4bf6df1a8a Whitespace cleanup.
Bram Moolenaar <bram@vim.org>
parents: 2320
diff changeset
550 else if (lua_isstring(L, 2))
ea4bf6df1a8a Whitespace cleanup.
Bram Moolenaar <bram@vim.org>
parents: 2320
diff changeset
551 {
ea4bf6df1a8a Whitespace cleanup.
Bram Moolenaar <bram@vim.org>
parents: 2320
diff changeset
552 const char *s = lua_tostring(L, 2);
ea4bf6df1a8a Whitespace cleanup.
Bram Moolenaar <bram@vim.org>
parents: 2320
diff changeset
553 if (strncmp(s, "name", 4) == 0)
ea4bf6df1a8a Whitespace cleanup.
Bram Moolenaar <bram@vim.org>
parents: 2320
diff changeset
554 lua_pushstring(L, (char *) (*b)->b_sfname);
ea4bf6df1a8a Whitespace cleanup.
Bram Moolenaar <bram@vim.org>
parents: 2320
diff changeset
555 else if (strncmp(s, "fname", 5) == 0)
ea4bf6df1a8a Whitespace cleanup.
Bram Moolenaar <bram@vim.org>
parents: 2320
diff changeset
556 lua_pushstring(L, (char *) (*b)->b_ffname);
ea4bf6df1a8a Whitespace cleanup.
Bram Moolenaar <bram@vim.org>
parents: 2320
diff changeset
557 else if (strncmp(s, "number", 6) == 0)
ea4bf6df1a8a Whitespace cleanup.
Bram Moolenaar <bram@vim.org>
parents: 2320
diff changeset
558 lua_pushinteger(L, (*b)->b_fnum);
ea4bf6df1a8a Whitespace cleanup.
Bram Moolenaar <bram@vim.org>
parents: 2320
diff changeset
559 /* methods */
ea4bf6df1a8a Whitespace cleanup.
Bram Moolenaar <bram@vim.org>
parents: 2320
diff changeset
560 else if (strncmp(s, "insert", 6) == 0
ea4bf6df1a8a Whitespace cleanup.
Bram Moolenaar <bram@vim.org>
parents: 2320
diff changeset
561 || strncmp(s, "next", 4) == 0
ea4bf6df1a8a Whitespace cleanup.
Bram Moolenaar <bram@vim.org>
parents: 2320
diff changeset
562 || strncmp(s, "previous", 8) == 0
ea4bf6df1a8a Whitespace cleanup.
Bram Moolenaar <bram@vim.org>
parents: 2320
diff changeset
563 || strncmp(s, "isvalid", 7) == 0)
ea4bf6df1a8a Whitespace cleanup.
Bram Moolenaar <bram@vim.org>
parents: 2320
diff changeset
564 {
ea4bf6df1a8a Whitespace cleanup.
Bram Moolenaar <bram@vim.org>
parents: 2320
diff changeset
565 lua_getmetatable(L, 1);
ea4bf6df1a8a Whitespace cleanup.
Bram Moolenaar <bram@vim.org>
parents: 2320
diff changeset
566 lua_getfield(L, -1, s);
ea4bf6df1a8a Whitespace cleanup.
Bram Moolenaar <bram@vim.org>
parents: 2320
diff changeset
567 }
ea4bf6df1a8a Whitespace cleanup.
Bram Moolenaar <bram@vim.org>
parents: 2320
diff changeset
568 else
ea4bf6df1a8a Whitespace cleanup.
Bram Moolenaar <bram@vim.org>
parents: 2320
diff changeset
569 lua_pushnil(L);
2320
966a5609669e Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
570 }
2330
ea4bf6df1a8a Whitespace cleanup.
Bram Moolenaar <bram@vim.org>
parents: 2320
diff changeset
571 else
ea4bf6df1a8a Whitespace cleanup.
Bram Moolenaar <bram@vim.org>
parents: 2320
diff changeset
572 lua_pushnil(L);
2320
966a5609669e Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
573 return 1;
966a5609669e Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
574 }
966a5609669e Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
575
2330
ea4bf6df1a8a Whitespace cleanup.
Bram Moolenaar <bram@vim.org>
parents: 2320
diff changeset
576 static int
ea4bf6df1a8a Whitespace cleanup.
Bram Moolenaar <bram@vim.org>
parents: 2320
diff changeset
577 luaV_buffer_newindex(lua_State *L)
ea4bf6df1a8a Whitespace cleanup.
Bram Moolenaar <bram@vim.org>
parents: 2320
diff changeset
578 {
2320
966a5609669e Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
579 luaV_Buffer *b = (luaV_Buffer *) lua_touserdata(L, 1);
966a5609669e Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
580 linenr_T n = (linenr_T) luaL_checkinteger(L, 2);
966a5609669e Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
581 #ifdef HAVE_SANDBOX
966a5609669e Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
582 luaV_checksandbox(L);
966a5609669e Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
583 #endif
966a5609669e Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
584 if (n < 1 || n > (*b)->b_ml.ml_line_count)
2330
ea4bf6df1a8a Whitespace cleanup.
Bram Moolenaar <bram@vim.org>
parents: 2320
diff changeset
585 luaL_error(L, "invalid line number");
ea4bf6df1a8a Whitespace cleanup.
Bram Moolenaar <bram@vim.org>
parents: 2320
diff changeset
586 if (lua_isnil(L, 3)) /* delete line */
ea4bf6df1a8a Whitespace cleanup.
Bram Moolenaar <bram@vim.org>
parents: 2320
diff changeset
587 {
ea4bf6df1a8a Whitespace cleanup.
Bram Moolenaar <bram@vim.org>
parents: 2320
diff changeset
588 buf_T *buf = curbuf;
ea4bf6df1a8a Whitespace cleanup.
Bram Moolenaar <bram@vim.org>
parents: 2320
diff changeset
589 curbuf = *b;
ea4bf6df1a8a Whitespace cleanup.
Bram Moolenaar <bram@vim.org>
parents: 2320
diff changeset
590 if (u_savedel(n, 1L) == FAIL)
ea4bf6df1a8a Whitespace cleanup.
Bram Moolenaar <bram@vim.org>
parents: 2320
diff changeset
591 {
ea4bf6df1a8a Whitespace cleanup.
Bram Moolenaar <bram@vim.org>
parents: 2320
diff changeset
592 curbuf = buf;
ea4bf6df1a8a Whitespace cleanup.
Bram Moolenaar <bram@vim.org>
parents: 2320
diff changeset
593 luaL_error(L, "cannot save undo information");
ea4bf6df1a8a Whitespace cleanup.
Bram Moolenaar <bram@vim.org>
parents: 2320
diff changeset
594 }
ea4bf6df1a8a Whitespace cleanup.
Bram Moolenaar <bram@vim.org>
parents: 2320
diff changeset
595 else if (ml_delete(n, FALSE) == FAIL)
ea4bf6df1a8a Whitespace cleanup.
Bram Moolenaar <bram@vim.org>
parents: 2320
diff changeset
596 {
ea4bf6df1a8a Whitespace cleanup.
Bram Moolenaar <bram@vim.org>
parents: 2320
diff changeset
597 curbuf = buf;
ea4bf6df1a8a Whitespace cleanup.
Bram Moolenaar <bram@vim.org>
parents: 2320
diff changeset
598 luaL_error(L, "cannot delete line");
ea4bf6df1a8a Whitespace cleanup.
Bram Moolenaar <bram@vim.org>
parents: 2320
diff changeset
599 }
ea4bf6df1a8a Whitespace cleanup.
Bram Moolenaar <bram@vim.org>
parents: 2320
diff changeset
600 else {
ea4bf6df1a8a Whitespace cleanup.
Bram Moolenaar <bram@vim.org>
parents: 2320
diff changeset
601 deleted_lines_mark(n, 1L);
ea4bf6df1a8a Whitespace cleanup.
Bram Moolenaar <bram@vim.org>
parents: 2320
diff changeset
602 if (*b == curwin->w_buffer) /* fix cursor in current window? */
ea4bf6df1a8a Whitespace cleanup.
Bram Moolenaar <bram@vim.org>
parents: 2320
diff changeset
603 {
ea4bf6df1a8a Whitespace cleanup.
Bram Moolenaar <bram@vim.org>
parents: 2320
diff changeset
604 if (curwin->w_cursor.lnum >= n)
ea4bf6df1a8a Whitespace cleanup.
Bram Moolenaar <bram@vim.org>
parents: 2320
diff changeset
605 {
ea4bf6df1a8a Whitespace cleanup.
Bram Moolenaar <bram@vim.org>
parents: 2320
diff changeset
606 if (curwin->w_cursor.lnum > n)
ea4bf6df1a8a Whitespace cleanup.
Bram Moolenaar <bram@vim.org>
parents: 2320
diff changeset
607 {
ea4bf6df1a8a Whitespace cleanup.
Bram Moolenaar <bram@vim.org>
parents: 2320
diff changeset
608 curwin->w_cursor.lnum -= 1;
ea4bf6df1a8a Whitespace cleanup.
Bram Moolenaar <bram@vim.org>
parents: 2320
diff changeset
609 check_cursor_col();
ea4bf6df1a8a Whitespace cleanup.
Bram Moolenaar <bram@vim.org>
parents: 2320
diff changeset
610 }
ea4bf6df1a8a Whitespace cleanup.
Bram Moolenaar <bram@vim.org>
parents: 2320
diff changeset
611 else check_cursor();
ea4bf6df1a8a Whitespace cleanup.
Bram Moolenaar <bram@vim.org>
parents: 2320
diff changeset
612 changed_cline_bef_curs();
ea4bf6df1a8a Whitespace cleanup.
Bram Moolenaar <bram@vim.org>
parents: 2320
diff changeset
613 }
ea4bf6df1a8a Whitespace cleanup.
Bram Moolenaar <bram@vim.org>
parents: 2320
diff changeset
614 invalidate_botline();
ea4bf6df1a8a Whitespace cleanup.
Bram Moolenaar <bram@vim.org>
parents: 2320
diff changeset
615 }
ea4bf6df1a8a Whitespace cleanup.
Bram Moolenaar <bram@vim.org>
parents: 2320
diff changeset
616 }
ea4bf6df1a8a Whitespace cleanup.
Bram Moolenaar <bram@vim.org>
parents: 2320
diff changeset
617 curbuf = buf;
2320
966a5609669e Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
618 }
2330
ea4bf6df1a8a Whitespace cleanup.
Bram Moolenaar <bram@vim.org>
parents: 2320
diff changeset
619 else if (lua_isstring(L, 3)) /* update line */
ea4bf6df1a8a Whitespace cleanup.
Bram Moolenaar <bram@vim.org>
parents: 2320
diff changeset
620 {
ea4bf6df1a8a Whitespace cleanup.
Bram Moolenaar <bram@vim.org>
parents: 2320
diff changeset
621 buf_T *buf = curbuf;
ea4bf6df1a8a Whitespace cleanup.
Bram Moolenaar <bram@vim.org>
parents: 2320
diff changeset
622 curbuf = *b;
ea4bf6df1a8a Whitespace cleanup.
Bram Moolenaar <bram@vim.org>
parents: 2320
diff changeset
623 if (u_savesub(n) == FAIL)
ea4bf6df1a8a Whitespace cleanup.
Bram Moolenaar <bram@vim.org>
parents: 2320
diff changeset
624 {
ea4bf6df1a8a Whitespace cleanup.
Bram Moolenaar <bram@vim.org>
parents: 2320
diff changeset
625 curbuf = buf;
ea4bf6df1a8a Whitespace cleanup.
Bram Moolenaar <bram@vim.org>
parents: 2320
diff changeset
626 luaL_error(L, "cannot save undo information");
ea4bf6df1a8a Whitespace cleanup.
Bram Moolenaar <bram@vim.org>
parents: 2320
diff changeset
627 }
ea4bf6df1a8a Whitespace cleanup.
Bram Moolenaar <bram@vim.org>
parents: 2320
diff changeset
628 else if (ml_replace(n, luaV_toline(L, 3), TRUE) == FAIL)
ea4bf6df1a8a Whitespace cleanup.
Bram Moolenaar <bram@vim.org>
parents: 2320
diff changeset
629 {
ea4bf6df1a8a Whitespace cleanup.
Bram Moolenaar <bram@vim.org>
parents: 2320
diff changeset
630 curbuf = buf;
ea4bf6df1a8a Whitespace cleanup.
Bram Moolenaar <bram@vim.org>
parents: 2320
diff changeset
631 luaL_error(L, "cannot replace line");
ea4bf6df1a8a Whitespace cleanup.
Bram Moolenaar <bram@vim.org>
parents: 2320
diff changeset
632 }
ea4bf6df1a8a Whitespace cleanup.
Bram Moolenaar <bram@vim.org>
parents: 2320
diff changeset
633 else changed_bytes(n, 0);
ea4bf6df1a8a Whitespace cleanup.
Bram Moolenaar <bram@vim.org>
parents: 2320
diff changeset
634 curbuf = buf;
ea4bf6df1a8a Whitespace cleanup.
Bram Moolenaar <bram@vim.org>
parents: 2320
diff changeset
635 if (*b == curwin->w_buffer)
ea4bf6df1a8a Whitespace cleanup.
Bram Moolenaar <bram@vim.org>
parents: 2320
diff changeset
636 check_cursor_col();
2320
966a5609669e Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
637 }
966a5609669e Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
638 else
2330
ea4bf6df1a8a Whitespace cleanup.
Bram Moolenaar <bram@vim.org>
parents: 2320
diff changeset
639 luaL_error(L, "wrong argument to change line");
2320
966a5609669e Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
640 return 0;
966a5609669e Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
641 }
966a5609669e Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
642
2330
ea4bf6df1a8a Whitespace cleanup.
Bram Moolenaar <bram@vim.org>
parents: 2320
diff changeset
643 static int
ea4bf6df1a8a Whitespace cleanup.
Bram Moolenaar <bram@vim.org>
parents: 2320
diff changeset
644 luaV_buffer_insert(lua_State *L)
ea4bf6df1a8a Whitespace cleanup.
Bram Moolenaar <bram@vim.org>
parents: 2320
diff changeset
645 {
2320
966a5609669e Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
646 luaV_Buffer *b = luaV_checkudata(L, 1, LUAVIM_BUFFER);
966a5609669e Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
647 linenr_T last = (*b)->b_ml.ml_line_count;
966a5609669e Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
648 linenr_T n = (linenr_T) luaL_optinteger(L, 3, last);
966a5609669e Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
649 buf_T *buf;
966a5609669e Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
650 luaL_checktype(L, 2, LUA_TSTRING);
966a5609669e Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
651 #ifdef HAVE_SANDBOX
966a5609669e Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
652 luaV_checksandbox(L);
966a5609669e Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
653 #endif
966a5609669e Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
654 /* fix insertion line */
966a5609669e Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
655 if (n < 0) n = 0;
966a5609669e Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
656 if (n > last) n = last;
966a5609669e Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
657 /* insert */
966a5609669e Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
658 buf = curbuf;
966a5609669e Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
659 curbuf = *b;
2330
ea4bf6df1a8a Whitespace cleanup.
Bram Moolenaar <bram@vim.org>
parents: 2320
diff changeset
660 if (u_save(n, n + 1) == FAIL)
ea4bf6df1a8a Whitespace cleanup.
Bram Moolenaar <bram@vim.org>
parents: 2320
diff changeset
661 {
ea4bf6df1a8a Whitespace cleanup.
Bram Moolenaar <bram@vim.org>
parents: 2320
diff changeset
662 curbuf = buf;
ea4bf6df1a8a Whitespace cleanup.
Bram Moolenaar <bram@vim.org>
parents: 2320
diff changeset
663 luaL_error(L, "cannot save undo information");
2320
966a5609669e Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
664 }
2330
ea4bf6df1a8a Whitespace cleanup.
Bram Moolenaar <bram@vim.org>
parents: 2320
diff changeset
665 else if (ml_append(n, luaV_toline(L, 2), 0, FALSE) == FAIL)
ea4bf6df1a8a Whitespace cleanup.
Bram Moolenaar <bram@vim.org>
parents: 2320
diff changeset
666 {
ea4bf6df1a8a Whitespace cleanup.
Bram Moolenaar <bram@vim.org>
parents: 2320
diff changeset
667 curbuf = buf;
ea4bf6df1a8a Whitespace cleanup.
Bram Moolenaar <bram@vim.org>
parents: 2320
diff changeset
668 luaL_error(L, "cannot insert line");
2320
966a5609669e Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
669 }
966a5609669e Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
670 else
2330
ea4bf6df1a8a Whitespace cleanup.
Bram Moolenaar <bram@vim.org>
parents: 2320
diff changeset
671 appended_lines_mark(n, 1L);
2320
966a5609669e Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
672 curbuf = buf;
966a5609669e Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
673 update_screen(VALID);
966a5609669e Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
674 return 0;
966a5609669e Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
675 }
966a5609669e Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
676
2330
ea4bf6df1a8a Whitespace cleanup.
Bram Moolenaar <bram@vim.org>
parents: 2320
diff changeset
677 static int
ea4bf6df1a8a Whitespace cleanup.
Bram Moolenaar <bram@vim.org>
parents: 2320
diff changeset
678 luaV_buffer_next(lua_State *L)
ea4bf6df1a8a Whitespace cleanup.
Bram Moolenaar <bram@vim.org>
parents: 2320
diff changeset
679 {
2320
966a5609669e Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
680 luaV_Buffer *b = luaV_checkudata(L, 1, LUAVIM_BUFFER);
966a5609669e Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
681 luaV_pushbuffer(L, (*b)->b_next);
966a5609669e Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
682 return 1;
966a5609669e Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
683 }
966a5609669e Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
684
2330
ea4bf6df1a8a Whitespace cleanup.
Bram Moolenaar <bram@vim.org>
parents: 2320
diff changeset
685 static int
ea4bf6df1a8a Whitespace cleanup.
Bram Moolenaar <bram@vim.org>
parents: 2320
diff changeset
686 luaV_buffer_previous(lua_State *L)
ea4bf6df1a8a Whitespace cleanup.
Bram Moolenaar <bram@vim.org>
parents: 2320
diff changeset
687 {
2320
966a5609669e Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
688 luaV_Buffer *b = luaV_checkudata(L, 1, LUAVIM_BUFFER);
966a5609669e Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
689 luaV_pushbuffer(L, (*b)->b_prev);
966a5609669e Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
690 return 1;
966a5609669e Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
691 }
966a5609669e Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
692
2330
ea4bf6df1a8a Whitespace cleanup.
Bram Moolenaar <bram@vim.org>
parents: 2320
diff changeset
693 static int
ea4bf6df1a8a Whitespace cleanup.
Bram Moolenaar <bram@vim.org>
parents: 2320
diff changeset
694 luaV_buffer_isvalid(lua_State *L)
ea4bf6df1a8a Whitespace cleanup.
Bram Moolenaar <bram@vim.org>
parents: 2320
diff changeset
695 {
2320
966a5609669e Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
696 luaV_Buffer *b = luaV_checkudata(L, 1, LUAVIM_BUFFER);
966a5609669e Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
697 lua_pushlightuserdata(L, (void *) (*b));
966a5609669e Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
698 lua_rawget(L, LUA_ENVIRONINDEX);
966a5609669e Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
699 lua_pushboolean(L, !lua_isnil(L, -1));
966a5609669e Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
700 return 1;
966a5609669e Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
701 }
966a5609669e Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
702
966a5609669e Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
703 static const luaL_Reg luaV_Buffer_mt[] = {
966a5609669e Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
704 {"__tostring", luaV_buffer_tostring},
966a5609669e Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
705 {"__len", luaV_buffer_len},
966a5609669e Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
706 {"__call", luaV_buffer_call},
966a5609669e Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
707 {"__index", luaV_buffer_index},
966a5609669e Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
708 {"__newindex", luaV_buffer_newindex},
966a5609669e Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
709 {"insert", luaV_buffer_insert},
966a5609669e Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
710 {"next", luaV_buffer_next},
966a5609669e Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
711 {"previous", luaV_buffer_previous},
966a5609669e Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
712 {"isvalid", luaV_buffer_isvalid},
966a5609669e Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
713 {NULL, NULL}
966a5609669e Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
714 };
966a5609669e Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
715
966a5609669e Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
716
966a5609669e Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
717 /* ======= Window type ======= */
966a5609669e Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
718
2330
ea4bf6df1a8a Whitespace cleanup.
Bram Moolenaar <bram@vim.org>
parents: 2320
diff changeset
719 static luaV_Window *
ea4bf6df1a8a Whitespace cleanup.
Bram Moolenaar <bram@vim.org>
parents: 2320
diff changeset
720 luaV_newwindow(lua_State *L, win_T *win)
ea4bf6df1a8a Whitespace cleanup.
Bram Moolenaar <bram@vim.org>
parents: 2320
diff changeset
721 {
2320
966a5609669e Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
722 luaV_Window *w = (luaV_Window *) lua_newuserdata(L, sizeof(luaV_Window));
966a5609669e Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
723 *w = win;
966a5609669e Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
724 lua_pushlightuserdata(L, (void *) win);
966a5609669e Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
725 lua_pushvalue(L, -2);
966a5609669e Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
726 lua_rawset(L, LUA_ENVIRONINDEX); /* env[win] = udata */
966a5609669e Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
727 /* to avoid GC, store as key in env */
966a5609669e Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
728 lua_pushvalue(L, -1);
966a5609669e Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
729 lua_pushboolean(L, 1);
966a5609669e Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
730 lua_rawset(L, LUA_ENVIRONINDEX); /* env[udata] = true */
966a5609669e Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
731 /* set metatable */
966a5609669e Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
732 luaV_getfield(L, LUAVIM_WINDOW);
966a5609669e Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
733 lua_setmetatable(L, -2);
966a5609669e Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
734 return w;
966a5609669e Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
735 }
966a5609669e Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
736
2330
ea4bf6df1a8a Whitespace cleanup.
Bram Moolenaar <bram@vim.org>
parents: 2320
diff changeset
737 static luaV_Window *
ea4bf6df1a8a Whitespace cleanup.
Bram Moolenaar <bram@vim.org>
parents: 2320
diff changeset
738 luaV_pushwindow(lua_State *L, win_T *win)
ea4bf6df1a8a Whitespace cleanup.
Bram Moolenaar <bram@vim.org>
parents: 2320
diff changeset
739 {
2320
966a5609669e Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
740 luaV_Window *w = NULL;
966a5609669e Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
741 if (win == NULL)
2330
ea4bf6df1a8a Whitespace cleanup.
Bram Moolenaar <bram@vim.org>
parents: 2320
diff changeset
742 lua_pushnil(L);
2320
966a5609669e Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
743 else {
2330
ea4bf6df1a8a Whitespace cleanup.
Bram Moolenaar <bram@vim.org>
parents: 2320
diff changeset
744 lua_pushlightuserdata(L, (void *) win);
ea4bf6df1a8a Whitespace cleanup.
Bram Moolenaar <bram@vim.org>
parents: 2320
diff changeset
745 lua_rawget(L, LUA_ENVIRONINDEX);
ea4bf6df1a8a Whitespace cleanup.
Bram Moolenaar <bram@vim.org>
parents: 2320
diff changeset
746 if (lua_isnil(L, -1)) /* not interned? */
ea4bf6df1a8a Whitespace cleanup.
Bram Moolenaar <bram@vim.org>
parents: 2320
diff changeset
747 {
ea4bf6df1a8a Whitespace cleanup.
Bram Moolenaar <bram@vim.org>
parents: 2320
diff changeset
748 lua_pop(L, 1);
ea4bf6df1a8a Whitespace cleanup.
Bram Moolenaar <bram@vim.org>
parents: 2320
diff changeset
749 w = luaV_newwindow(L, win);
ea4bf6df1a8a Whitespace cleanup.
Bram Moolenaar <bram@vim.org>
parents: 2320
diff changeset
750 }
ea4bf6df1a8a Whitespace cleanup.
Bram Moolenaar <bram@vim.org>
parents: 2320
diff changeset
751 else w = (luaV_Window *) lua_touserdata(L, -1);
2320
966a5609669e Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
752 }
966a5609669e Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
753 return w;
966a5609669e Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
754 }
966a5609669e Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
755
966a5609669e Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
756 /* Window metamethods */
966a5609669e Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
757
2330
ea4bf6df1a8a Whitespace cleanup.
Bram Moolenaar <bram@vim.org>
parents: 2320
diff changeset
758 static int
ea4bf6df1a8a Whitespace cleanup.
Bram Moolenaar <bram@vim.org>
parents: 2320
diff changeset
759 luaV_window_tostring(lua_State *L)
ea4bf6df1a8a Whitespace cleanup.
Bram Moolenaar <bram@vim.org>
parents: 2320
diff changeset
760 {
2320
966a5609669e Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
761 lua_pushfstring(L, "%s: %p", LUAVIM_WINDOW, lua_touserdata(L, 1));
966a5609669e Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
762 return 1;
966a5609669e Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
763 }
966a5609669e Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
764
2330
ea4bf6df1a8a Whitespace cleanup.
Bram Moolenaar <bram@vim.org>
parents: 2320
diff changeset
765 static int
ea4bf6df1a8a Whitespace cleanup.
Bram Moolenaar <bram@vim.org>
parents: 2320
diff changeset
766 luaV_window_call(lua_State *L)
ea4bf6df1a8a Whitespace cleanup.
Bram Moolenaar <bram@vim.org>
parents: 2320
diff changeset
767 {
2320
966a5609669e Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
768 luaV_Window *w = (luaV_Window *) lua_touserdata(L, 1);
966a5609669e Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
769 lua_settop(L, 1);
966a5609669e Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
770 win_goto(*w);
966a5609669e Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
771 return 1;
966a5609669e Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
772 }
966a5609669e Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
773
2330
ea4bf6df1a8a Whitespace cleanup.
Bram Moolenaar <bram@vim.org>
parents: 2320
diff changeset
774 static int
ea4bf6df1a8a Whitespace cleanup.
Bram Moolenaar <bram@vim.org>
parents: 2320
diff changeset
775 luaV_window_index(lua_State *L)
ea4bf6df1a8a Whitespace cleanup.
Bram Moolenaar <bram@vim.org>
parents: 2320
diff changeset
776 {
2320
966a5609669e Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
777 luaV_Window *w = (luaV_Window *) lua_touserdata(L, 1);
966a5609669e Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
778 const char *s = luaL_checkstring(L, 2);
966a5609669e Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
779 if (strncmp(s, "buffer", 6) == 0)
2330
ea4bf6df1a8a Whitespace cleanup.
Bram Moolenaar <bram@vim.org>
parents: 2320
diff changeset
780 luaV_pushbuffer(L, (*w)->w_buffer);
2320
966a5609669e Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
781 else if (strncmp(s, "line", 4) == 0)
2330
ea4bf6df1a8a Whitespace cleanup.
Bram Moolenaar <bram@vim.org>
parents: 2320
diff changeset
782 lua_pushinteger(L, (*w)->w_cursor.lnum);
2320
966a5609669e Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
783 else if (strncmp(s, "col", 3) == 0)
2330
ea4bf6df1a8a Whitespace cleanup.
Bram Moolenaar <bram@vim.org>
parents: 2320
diff changeset
784 lua_pushinteger(L, (*w)->w_cursor.col + 1);
2320
966a5609669e Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
785 #ifdef FEAT_VERTSPLIT
966a5609669e Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
786 else if (strncmp(s, "width", 5) == 0)
2330
ea4bf6df1a8a Whitespace cleanup.
Bram Moolenaar <bram@vim.org>
parents: 2320
diff changeset
787 lua_pushinteger(L, W_WIDTH((*w)));
2320
966a5609669e Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
788 #endif
966a5609669e Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
789 else if (strncmp(s, "height", 6) == 0)
2330
ea4bf6df1a8a Whitespace cleanup.
Bram Moolenaar <bram@vim.org>
parents: 2320
diff changeset
790 lua_pushinteger(L, (*w)->w_height);
2320
966a5609669e Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
791 /* methods */
966a5609669e Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
792 else if (strncmp(s, "next", 4) == 0
2330
ea4bf6df1a8a Whitespace cleanup.
Bram Moolenaar <bram@vim.org>
parents: 2320
diff changeset
793 || strncmp(s, "previous", 8) == 0
ea4bf6df1a8a Whitespace cleanup.
Bram Moolenaar <bram@vim.org>
parents: 2320
diff changeset
794 || strncmp(s, "isvalid", 7) == 0)
ea4bf6df1a8a Whitespace cleanup.
Bram Moolenaar <bram@vim.org>
parents: 2320
diff changeset
795 {
ea4bf6df1a8a Whitespace cleanup.
Bram Moolenaar <bram@vim.org>
parents: 2320
diff changeset
796 lua_getmetatable(L, 1);
ea4bf6df1a8a Whitespace cleanup.
Bram Moolenaar <bram@vim.org>
parents: 2320
diff changeset
797 lua_getfield(L, -1, s);
2320
966a5609669e Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
798 }
966a5609669e Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
799 else
2330
ea4bf6df1a8a Whitespace cleanup.
Bram Moolenaar <bram@vim.org>
parents: 2320
diff changeset
800 lua_pushnil(L);
2320
966a5609669e Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
801 return 1;
966a5609669e Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
802 }
966a5609669e Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
803
2330
ea4bf6df1a8a Whitespace cleanup.
Bram Moolenaar <bram@vim.org>
parents: 2320
diff changeset
804 static int
ea4bf6df1a8a Whitespace cleanup.
Bram Moolenaar <bram@vim.org>
parents: 2320
diff changeset
805 luaV_window_newindex (lua_State *L)
ea4bf6df1a8a Whitespace cleanup.
Bram Moolenaar <bram@vim.org>
parents: 2320
diff changeset
806 {
2320
966a5609669e Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
807 luaV_Window *w = (luaV_Window *) lua_touserdata(L, 1);
966a5609669e Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
808 const char *s = luaL_checkstring(L, 2);
966a5609669e Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
809 int v = luaL_checkinteger(L, 3);
2330
ea4bf6df1a8a Whitespace cleanup.
Bram Moolenaar <bram@vim.org>
parents: 2320
diff changeset
810 if (strncmp(s, "line", 4) == 0)
ea4bf6df1a8a Whitespace cleanup.
Bram Moolenaar <bram@vim.org>
parents: 2320
diff changeset
811 {
2320
966a5609669e Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
812 #ifdef HAVE_SANDBOX
2330
ea4bf6df1a8a Whitespace cleanup.
Bram Moolenaar <bram@vim.org>
parents: 2320
diff changeset
813 luaV_checksandbox(L);
2320
966a5609669e Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
814 #endif
2330
ea4bf6df1a8a Whitespace cleanup.
Bram Moolenaar <bram@vim.org>
parents: 2320
diff changeset
815 if (v < 1 || v > (*w)->w_buffer->b_ml.ml_line_count)
ea4bf6df1a8a Whitespace cleanup.
Bram Moolenaar <bram@vim.org>
parents: 2320
diff changeset
816 luaL_error(L, "line out of range");
ea4bf6df1a8a Whitespace cleanup.
Bram Moolenaar <bram@vim.org>
parents: 2320
diff changeset
817 (*w)->w_cursor.lnum = v;
ea4bf6df1a8a Whitespace cleanup.
Bram Moolenaar <bram@vim.org>
parents: 2320
diff changeset
818 update_screen(VALID);
2320
966a5609669e Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
819 }
2330
ea4bf6df1a8a Whitespace cleanup.
Bram Moolenaar <bram@vim.org>
parents: 2320
diff changeset
820 else if (strncmp(s, "col", 3) == 0)
ea4bf6df1a8a Whitespace cleanup.
Bram Moolenaar <bram@vim.org>
parents: 2320
diff changeset
821 {
2320
966a5609669e Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
822 #ifdef HAVE_SANDBOX
2330
ea4bf6df1a8a Whitespace cleanup.
Bram Moolenaar <bram@vim.org>
parents: 2320
diff changeset
823 luaV_checksandbox(L);
2320
966a5609669e Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
824 #endif
2330
ea4bf6df1a8a Whitespace cleanup.
Bram Moolenaar <bram@vim.org>
parents: 2320
diff changeset
825 (*w)->w_cursor.col = v - 1;
ea4bf6df1a8a Whitespace cleanup.
Bram Moolenaar <bram@vim.org>
parents: 2320
diff changeset
826 update_screen(VALID);
2320
966a5609669e Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
827 }
966a5609669e Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
828 #ifdef FEAT_VERTSPLIT
2330
ea4bf6df1a8a Whitespace cleanup.
Bram Moolenaar <bram@vim.org>
parents: 2320
diff changeset
829 else if (strncmp(s, "width", 5) == 0)
ea4bf6df1a8a Whitespace cleanup.
Bram Moolenaar <bram@vim.org>
parents: 2320
diff changeset
830 {
ea4bf6df1a8a Whitespace cleanup.
Bram Moolenaar <bram@vim.org>
parents: 2320
diff changeset
831 win_T *win = curwin;
2320
966a5609669e Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
832 #ifdef FEAT_GUI
2330
ea4bf6df1a8a Whitespace cleanup.
Bram Moolenaar <bram@vim.org>
parents: 2320
diff changeset
833 need_mouse_correct = TRUE;
2320
966a5609669e Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
834 #endif
2330
ea4bf6df1a8a Whitespace cleanup.
Bram Moolenaar <bram@vim.org>
parents: 2320
diff changeset
835 curwin = *w;
ea4bf6df1a8a Whitespace cleanup.
Bram Moolenaar <bram@vim.org>
parents: 2320
diff changeset
836 win_setwidth(v);
ea4bf6df1a8a Whitespace cleanup.
Bram Moolenaar <bram@vim.org>
parents: 2320
diff changeset
837 curwin = win;
2320
966a5609669e Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
838 }
966a5609669e Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
839 #endif
2330
ea4bf6df1a8a Whitespace cleanup.
Bram Moolenaar <bram@vim.org>
parents: 2320
diff changeset
840 else if (strncmp(s, "height", 6) == 0)
ea4bf6df1a8a Whitespace cleanup.
Bram Moolenaar <bram@vim.org>
parents: 2320
diff changeset
841 {
ea4bf6df1a8a Whitespace cleanup.
Bram Moolenaar <bram@vim.org>
parents: 2320
diff changeset
842 win_T *win = curwin;
2320
966a5609669e Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
843 #ifdef FEAT_GUI
2330
ea4bf6df1a8a Whitespace cleanup.
Bram Moolenaar <bram@vim.org>
parents: 2320
diff changeset
844 need_mouse_correct = TRUE;
2320
966a5609669e Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
845 #endif
2330
ea4bf6df1a8a Whitespace cleanup.
Bram Moolenaar <bram@vim.org>
parents: 2320
diff changeset
846 curwin = *w;
ea4bf6df1a8a Whitespace cleanup.
Bram Moolenaar <bram@vim.org>
parents: 2320
diff changeset
847 win_setheight(v);
ea4bf6df1a8a Whitespace cleanup.
Bram Moolenaar <bram@vim.org>
parents: 2320
diff changeset
848 curwin = win;
2320
966a5609669e Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
849 }
966a5609669e Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
850 else
2330
ea4bf6df1a8a Whitespace cleanup.
Bram Moolenaar <bram@vim.org>
parents: 2320
diff changeset
851 luaL_error(L, "invalid window property: `%s'", s);
2320
966a5609669e Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
852 return 0;
966a5609669e Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
853 }
966a5609669e Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
854
2330
ea4bf6df1a8a Whitespace cleanup.
Bram Moolenaar <bram@vim.org>
parents: 2320
diff changeset
855 static int
ea4bf6df1a8a Whitespace cleanup.
Bram Moolenaar <bram@vim.org>
parents: 2320
diff changeset
856 luaV_window_next(lua_State *L)
ea4bf6df1a8a Whitespace cleanup.
Bram Moolenaar <bram@vim.org>
parents: 2320
diff changeset
857 {
2320
966a5609669e Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
858 luaV_Window *w = luaV_checkudata(L, 1, LUAVIM_WINDOW);
966a5609669e Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
859 luaV_pushwindow(L, (*w)->w_next);
966a5609669e Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
860 return 1;
966a5609669e Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
861 }
966a5609669e Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
862
2330
ea4bf6df1a8a Whitespace cleanup.
Bram Moolenaar <bram@vim.org>
parents: 2320
diff changeset
863 static int
ea4bf6df1a8a Whitespace cleanup.
Bram Moolenaar <bram@vim.org>
parents: 2320
diff changeset
864 luaV_window_previous(lua_State *L)
ea4bf6df1a8a Whitespace cleanup.
Bram Moolenaar <bram@vim.org>
parents: 2320
diff changeset
865 {
2320
966a5609669e Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
866 luaV_Window *w = luaV_checkudata(L, 1, LUAVIM_WINDOW);
966a5609669e Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
867 luaV_pushwindow(L, (*w)->w_prev);
966a5609669e Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
868 return 1;
966a5609669e Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
869 }
966a5609669e Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
870
2330
ea4bf6df1a8a Whitespace cleanup.
Bram Moolenaar <bram@vim.org>
parents: 2320
diff changeset
871 static int
ea4bf6df1a8a Whitespace cleanup.
Bram Moolenaar <bram@vim.org>
parents: 2320
diff changeset
872 luaV_window_isvalid(lua_State *L)
ea4bf6df1a8a Whitespace cleanup.
Bram Moolenaar <bram@vim.org>
parents: 2320
diff changeset
873 {
2320
966a5609669e Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
874 luaV_Window *w = luaV_checkudata(L, 1, LUAVIM_WINDOW);
966a5609669e Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
875 lua_pushlightuserdata(L, (void *) (*w));
966a5609669e Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
876 lua_rawget(L, LUA_ENVIRONINDEX);
966a5609669e Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
877 lua_pushboolean(L, !lua_isnil(L, -1));
966a5609669e Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
878 return 1;
966a5609669e Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
879 }
966a5609669e Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
880
966a5609669e Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
881 static const luaL_Reg luaV_Window_mt[] = {
966a5609669e Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
882 {"__tostring", luaV_window_tostring},
966a5609669e Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
883 {"__call", luaV_window_call},
966a5609669e Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
884 {"__index", luaV_window_index},
966a5609669e Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
885 {"__newindex", luaV_window_newindex},
966a5609669e Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
886 {"next", luaV_window_next},
966a5609669e Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
887 {"previous", luaV_window_previous},
966a5609669e Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
888 {"isvalid", luaV_window_isvalid},
966a5609669e Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
889 {NULL, NULL}
966a5609669e Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
890 };
966a5609669e Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
891
966a5609669e Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
892
966a5609669e Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
893 /* ======= Vim module ======= */
966a5609669e Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
894
2330
ea4bf6df1a8a Whitespace cleanup.
Bram Moolenaar <bram@vim.org>
parents: 2320
diff changeset
895 static int
ea4bf6df1a8a Whitespace cleanup.
Bram Moolenaar <bram@vim.org>
parents: 2320
diff changeset
896 luaV_print(lua_State *L)
ea4bf6df1a8a Whitespace cleanup.
Bram Moolenaar <bram@vim.org>
parents: 2320
diff changeset
897 {
2320
966a5609669e Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
898 int i, n = lua_gettop(L); /* nargs */
966a5609669e Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
899 const char *s;
966a5609669e Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
900 size_t l;
966a5609669e Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
901 luaL_Buffer b;
966a5609669e Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
902 luaL_buffinit(L, &b);
966a5609669e Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
903 lua_getglobal(L, "tostring");
2330
ea4bf6df1a8a Whitespace cleanup.
Bram Moolenaar <bram@vim.org>
parents: 2320
diff changeset
904 for (i = 1; i <= n; i++)
ea4bf6df1a8a Whitespace cleanup.
Bram Moolenaar <bram@vim.org>
parents: 2320
diff changeset
905 {
ea4bf6df1a8a Whitespace cleanup.
Bram Moolenaar <bram@vim.org>
parents: 2320
diff changeset
906 lua_pushvalue(L, -1); /* tostring */
ea4bf6df1a8a Whitespace cleanup.
Bram Moolenaar <bram@vim.org>
parents: 2320
diff changeset
907 lua_pushvalue(L, i); /* arg */
ea4bf6df1a8a Whitespace cleanup.
Bram Moolenaar <bram@vim.org>
parents: 2320
diff changeset
908 lua_call(L, 1, 1);
ea4bf6df1a8a Whitespace cleanup.
Bram Moolenaar <bram@vim.org>
parents: 2320
diff changeset
909 s = lua_tolstring(L, -1, &l);
ea4bf6df1a8a Whitespace cleanup.
Bram Moolenaar <bram@vim.org>
parents: 2320
diff changeset
910 if (s == NULL)
ea4bf6df1a8a Whitespace cleanup.
Bram Moolenaar <bram@vim.org>
parents: 2320
diff changeset
911 return luaL_error(L, "cannot convert to string");
ea4bf6df1a8a Whitespace cleanup.
Bram Moolenaar <bram@vim.org>
parents: 2320
diff changeset
912 if (i > 1) luaL_addchar(&b, ' '); /* use space instead of tab */
ea4bf6df1a8a Whitespace cleanup.
Bram Moolenaar <bram@vim.org>
parents: 2320
diff changeset
913 luaV_addlstring(&b, s, l, 0);
ea4bf6df1a8a Whitespace cleanup.
Bram Moolenaar <bram@vim.org>
parents: 2320
diff changeset
914 lua_pop(L, 1);
2320
966a5609669e Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
915 }
966a5609669e Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
916 luaL_pushresult(&b);
966a5609669e Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
917 luaV_msg(L);
966a5609669e Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
918 return 0;
966a5609669e Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
919 }
966a5609669e Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
920
2330
ea4bf6df1a8a Whitespace cleanup.
Bram Moolenaar <bram@vim.org>
parents: 2320
diff changeset
921 static int
ea4bf6df1a8a Whitespace cleanup.
Bram Moolenaar <bram@vim.org>
parents: 2320
diff changeset
922 luaV_command(lua_State *L)
ea4bf6df1a8a Whitespace cleanup.
Bram Moolenaar <bram@vim.org>
parents: 2320
diff changeset
923 {
2320
966a5609669e Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
924 do_cmdline_cmd((char_u *) luaL_checkstring(L, 1));
966a5609669e Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
925 update_screen(VALID);
966a5609669e Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
926 return 0;
966a5609669e Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
927 }
966a5609669e Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
928
2330
ea4bf6df1a8a Whitespace cleanup.
Bram Moolenaar <bram@vim.org>
parents: 2320
diff changeset
929 static int
ea4bf6df1a8a Whitespace cleanup.
Bram Moolenaar <bram@vim.org>
parents: 2320
diff changeset
930 luaV_eval(lua_State *L)
ea4bf6df1a8a Whitespace cleanup.
Bram Moolenaar <bram@vim.org>
parents: 2320
diff changeset
931 {
2320
966a5609669e Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
932 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
933 if (tv == NULL) luaL_error(L, "invalid expression");
966a5609669e Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
934 luaV_pushtypval(L, tv);
966a5609669e Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
935 return 1;
966a5609669e Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
936 }
966a5609669e Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
937
2330
ea4bf6df1a8a Whitespace cleanup.
Bram Moolenaar <bram@vim.org>
parents: 2320
diff changeset
938 static int
2331
3840b7508835 Make it easier to build with Lua. Remove compiler warnings.
Bram Moolenaar <bram@vim.org>
parents: 2330
diff changeset
939 luaV_beep(lua_State *L UNUSED)
2330
ea4bf6df1a8a Whitespace cleanup.
Bram Moolenaar <bram@vim.org>
parents: 2320
diff changeset
940 {
2320
966a5609669e Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
941 vim_beep();
966a5609669e Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
942 return 0;
966a5609669e Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
943 }
966a5609669e Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
944
2330
ea4bf6df1a8a Whitespace cleanup.
Bram Moolenaar <bram@vim.org>
parents: 2320
diff changeset
945 static int
ea4bf6df1a8a Whitespace cleanup.
Bram Moolenaar <bram@vim.org>
parents: 2320
diff changeset
946 luaV_line(lua_State *L)
ea4bf6df1a8a Whitespace cleanup.
Bram Moolenaar <bram@vim.org>
parents: 2320
diff changeset
947 {
2320
966a5609669e Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
948 luaV_pushline(L, curbuf, curwin->w_cursor.lnum);
966a5609669e Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
949 return 1;
966a5609669e Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
950 }
966a5609669e Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
951
2330
ea4bf6df1a8a Whitespace cleanup.
Bram Moolenaar <bram@vim.org>
parents: 2320
diff changeset
952 static int
ea4bf6df1a8a Whitespace cleanup.
Bram Moolenaar <bram@vim.org>
parents: 2320
diff changeset
953 luaV_buffer(lua_State *L)
ea4bf6df1a8a Whitespace cleanup.
Bram Moolenaar <bram@vim.org>
parents: 2320
diff changeset
954 {
2320
966a5609669e Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
955 buf_T *buf;
2330
ea4bf6df1a8a Whitespace cleanup.
Bram Moolenaar <bram@vim.org>
parents: 2320
diff changeset
956 if (lua_isstring(L, 1)) /* get by number or name? */
ea4bf6df1a8a Whitespace cleanup.
Bram Moolenaar <bram@vim.org>
parents: 2320
diff changeset
957 {
ea4bf6df1a8a Whitespace cleanup.
Bram Moolenaar <bram@vim.org>
parents: 2320
diff changeset
958 if (lua_isnumber(L, 1)) /* by number? */
ea4bf6df1a8a Whitespace cleanup.
Bram Moolenaar <bram@vim.org>
parents: 2320
diff changeset
959 {
ea4bf6df1a8a Whitespace cleanup.
Bram Moolenaar <bram@vim.org>
parents: 2320
diff changeset
960 int n = lua_tointeger(L, 1);
ea4bf6df1a8a Whitespace cleanup.
Bram Moolenaar <bram@vim.org>
parents: 2320
diff changeset
961 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
ea4bf6df1a8a Whitespace cleanup.
Bram Moolenaar <bram@vim.org>
parents: 2320
diff changeset
962 if (buf->b_fnum == n) break;
ea4bf6df1a8a Whitespace cleanup.
Bram Moolenaar <bram@vim.org>
parents: 2320
diff changeset
963 }
ea4bf6df1a8a Whitespace cleanup.
Bram Moolenaar <bram@vim.org>
parents: 2320
diff changeset
964 else { /* by name */
ea4bf6df1a8a Whitespace cleanup.
Bram Moolenaar <bram@vim.org>
parents: 2320
diff changeset
965 size_t l;
ea4bf6df1a8a Whitespace cleanup.
Bram Moolenaar <bram@vim.org>
parents: 2320
diff changeset
966 const char *s = lua_tolstring(L, 1, &l);
ea4bf6df1a8a Whitespace cleanup.
Bram Moolenaar <bram@vim.org>
parents: 2320
diff changeset
967 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
ea4bf6df1a8a Whitespace cleanup.
Bram Moolenaar <bram@vim.org>
parents: 2320
diff changeset
968 {
ea4bf6df1a8a Whitespace cleanup.
Bram Moolenaar <bram@vim.org>
parents: 2320
diff changeset
969 if (buf->b_ffname == NULL || buf->b_sfname == NULL)
ea4bf6df1a8a Whitespace cleanup.
Bram Moolenaar <bram@vim.org>
parents: 2320
diff changeset
970 {
ea4bf6df1a8a Whitespace cleanup.
Bram Moolenaar <bram@vim.org>
parents: 2320
diff changeset
971 if (l == 0) break;
ea4bf6df1a8a Whitespace cleanup.
Bram Moolenaar <bram@vim.org>
parents: 2320
diff changeset
972 }
2331
3840b7508835 Make it easier to build with Lua. Remove compiler warnings.
Bram Moolenaar <bram@vim.org>
parents: 2330
diff changeset
973 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
974 || strncmp(s, (char *)buf->b_sfname, l) == 0)
2330
ea4bf6df1a8a Whitespace cleanup.
Bram Moolenaar <bram@vim.org>
parents: 2320
diff changeset
975 break;
ea4bf6df1a8a Whitespace cleanup.
Bram Moolenaar <bram@vim.org>
parents: 2320
diff changeset
976 }
ea4bf6df1a8a Whitespace cleanup.
Bram Moolenaar <bram@vim.org>
parents: 2320
diff changeset
977 }
ea4bf6df1a8a Whitespace cleanup.
Bram Moolenaar <bram@vim.org>
parents: 2320
diff changeset
978 if (buf == NULL) /* not found? */
ea4bf6df1a8a Whitespace cleanup.
Bram Moolenaar <bram@vim.org>
parents: 2320
diff changeset
979 lua_pushnil(L);
ea4bf6df1a8a Whitespace cleanup.
Bram Moolenaar <bram@vim.org>
parents: 2320
diff changeset
980 else
ea4bf6df1a8a Whitespace cleanup.
Bram Moolenaar <bram@vim.org>
parents: 2320
diff changeset
981 luaV_pushbuffer(L, buf);
2320
966a5609669e Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
982 }
966a5609669e Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
983 else {
2330
ea4bf6df1a8a Whitespace cleanup.
Bram Moolenaar <bram@vim.org>
parents: 2320
diff changeset
984 buf = (lua_toboolean(L, 1)) ? firstbuf : curbuf; /* first buffer? */
ea4bf6df1a8a Whitespace cleanup.
Bram Moolenaar <bram@vim.org>
parents: 2320
diff changeset
985 luaV_pushbuffer(L, buf);
2320
966a5609669e Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
986 }
966a5609669e Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
987 return 1;
966a5609669e Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
988 }
966a5609669e Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
989
2330
ea4bf6df1a8a Whitespace cleanup.
Bram Moolenaar <bram@vim.org>
parents: 2320
diff changeset
990 static int
ea4bf6df1a8a Whitespace cleanup.
Bram Moolenaar <bram@vim.org>
parents: 2320
diff changeset
991 luaV_window(lua_State *L)
ea4bf6df1a8a Whitespace cleanup.
Bram Moolenaar <bram@vim.org>
parents: 2320
diff changeset
992 {
2320
966a5609669e Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
993 win_T *win;
2330
ea4bf6df1a8a Whitespace cleanup.
Bram Moolenaar <bram@vim.org>
parents: 2320
diff changeset
994 if (lua_isnumber(L, 1)) /* get by number? */
ea4bf6df1a8a Whitespace cleanup.
Bram Moolenaar <bram@vim.org>
parents: 2320
diff changeset
995 {
ea4bf6df1a8a Whitespace cleanup.
Bram Moolenaar <bram@vim.org>
parents: 2320
diff changeset
996 int n = lua_tointeger(L, 1);
ea4bf6df1a8a Whitespace cleanup.
Bram Moolenaar <bram@vim.org>
parents: 2320
diff changeset
997 for (win = firstwin; win != NULL; win = win->w_next, n--)
ea4bf6df1a8a Whitespace cleanup.
Bram Moolenaar <bram@vim.org>
parents: 2320
diff changeset
998 if (n == 1) break;
ea4bf6df1a8a Whitespace cleanup.
Bram Moolenaar <bram@vim.org>
parents: 2320
diff changeset
999 if (win == NULL) /* not found? */
ea4bf6df1a8a Whitespace cleanup.
Bram Moolenaar <bram@vim.org>
parents: 2320
diff changeset
1000 lua_pushnil(L);
ea4bf6df1a8a Whitespace cleanup.
Bram Moolenaar <bram@vim.org>
parents: 2320
diff changeset
1001 else
ea4bf6df1a8a Whitespace cleanup.
Bram Moolenaar <bram@vim.org>
parents: 2320
diff changeset
1002 luaV_pushwindow(L, win);
2320
966a5609669e Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
1003 }
966a5609669e Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
1004 else {
2330
ea4bf6df1a8a Whitespace cleanup.
Bram Moolenaar <bram@vim.org>
parents: 2320
diff changeset
1005 win = (lua_toboolean(L, 1)) ? firstwin : curwin; /* first window? */
ea4bf6df1a8a Whitespace cleanup.
Bram Moolenaar <bram@vim.org>
parents: 2320
diff changeset
1006 luaV_pushwindow(L, win);
2320
966a5609669e Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
1007 }
966a5609669e Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
1008 return 1;
966a5609669e Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
1009 }
966a5609669e Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
1010
2330
ea4bf6df1a8a Whitespace cleanup.
Bram Moolenaar <bram@vim.org>
parents: 2320
diff changeset
1011 static int
ea4bf6df1a8a Whitespace cleanup.
Bram Moolenaar <bram@vim.org>
parents: 2320
diff changeset
1012 luaV_open(lua_State *L)
ea4bf6df1a8a Whitespace cleanup.
Bram Moolenaar <bram@vim.org>
parents: 2320
diff changeset
1013 {
2320
966a5609669e Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
1014 luaV_Buffer *b;
966a5609669e Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
1015 char_u *s = NULL;
966a5609669e Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
1016 #ifdef HAVE_SANDBOX
966a5609669e Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
1017 luaV_checksandbox(L);
966a5609669e Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
1018 #endif
966a5609669e Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
1019 if (lua_isstring(L, 1)) s = (char_u *) lua_tostring(L, 1);
966a5609669e Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
1020 b = luaV_pushbuffer(L, buflist_new(s, NULL, 1L, BLN_LISTED));
966a5609669e Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
1021 return 1;
966a5609669e Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
1022 }
966a5609669e Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
1023
2330
ea4bf6df1a8a Whitespace cleanup.
Bram Moolenaar <bram@vim.org>
parents: 2320
diff changeset
1024 static int
ea4bf6df1a8a Whitespace cleanup.
Bram Moolenaar <bram@vim.org>
parents: 2320
diff changeset
1025 luaV_isbuffer(lua_State *L)
ea4bf6df1a8a Whitespace cleanup.
Bram Moolenaar <bram@vim.org>
parents: 2320
diff changeset
1026 {
2320
966a5609669e Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
1027 lua_pushboolean(L, luaV_toudata(L, 1, LUAVIM_BUFFER) != NULL);
966a5609669e Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
1028 return 1;
966a5609669e Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
1029 }
966a5609669e Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
1030
2330
ea4bf6df1a8a Whitespace cleanup.
Bram Moolenaar <bram@vim.org>
parents: 2320
diff changeset
1031 static int
ea4bf6df1a8a Whitespace cleanup.
Bram Moolenaar <bram@vim.org>
parents: 2320
diff changeset
1032 luaV_iswindow(lua_State *L)
ea4bf6df1a8a Whitespace cleanup.
Bram Moolenaar <bram@vim.org>
parents: 2320
diff changeset
1033 {
2320
966a5609669e Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
1034 lua_pushboolean(L, luaV_toudata(L, 1, LUAVIM_WINDOW) != NULL);
966a5609669e Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
1035 return 1;
966a5609669e Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
1036 }
966a5609669e Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
1037
966a5609669e Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
1038 /* for freeing buffer and window objects; lightuserdata as arg */
2330
ea4bf6df1a8a Whitespace cleanup.
Bram Moolenaar <bram@vim.org>
parents: 2320
diff changeset
1039 static int
ea4bf6df1a8a Whitespace cleanup.
Bram Moolenaar <bram@vim.org>
parents: 2320
diff changeset
1040 luaV_free(lua_State *L)
ea4bf6df1a8a Whitespace cleanup.
Bram Moolenaar <bram@vim.org>
parents: 2320
diff changeset
1041 {
2320
966a5609669e Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
1042 lua_pushvalue(L, 1); /* lightudata */
966a5609669e Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
1043 lua_rawget(L, LUA_ENVIRONINDEX);
2330
ea4bf6df1a8a Whitespace cleanup.
Bram Moolenaar <bram@vim.org>
parents: 2320
diff changeset
1044 if (!lua_isnil(L, -1))
ea4bf6df1a8a Whitespace cleanup.
Bram Moolenaar <bram@vim.org>
parents: 2320
diff changeset
1045 {
ea4bf6df1a8a Whitespace cleanup.
Bram Moolenaar <bram@vim.org>
parents: 2320
diff changeset
1046 lua_pushnil(L);
ea4bf6df1a8a Whitespace cleanup.
Bram Moolenaar <bram@vim.org>
parents: 2320
diff changeset
1047 lua_rawset(L, LUA_ENVIRONINDEX); /* env[udata] = nil */
ea4bf6df1a8a Whitespace cleanup.
Bram Moolenaar <bram@vim.org>
parents: 2320
diff changeset
1048 lua_pushnil(L);
ea4bf6df1a8a Whitespace cleanup.
Bram Moolenaar <bram@vim.org>
parents: 2320
diff changeset
1049 lua_rawset(L, LUA_ENVIRONINDEX); /* env[lightudata] = nil */
2320
966a5609669e Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
1050 }
966a5609669e Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
1051 return 0;
966a5609669e Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
1052 }
966a5609669e Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
1053
966a5609669e Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
1054 static const luaL_Reg luaV_module[] = {
966a5609669e Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
1055 {"command", luaV_command},
966a5609669e Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
1056 {"eval", luaV_eval},
966a5609669e Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
1057 {"beep", luaV_beep},
966a5609669e Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
1058 {"line", luaV_line},
966a5609669e Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
1059 {"buffer", luaV_buffer},
966a5609669e Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
1060 {"window", luaV_window},
966a5609669e Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
1061 {"open", luaV_open},
966a5609669e Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
1062 {"isbuffer", luaV_isbuffer},
966a5609669e Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
1063 {"iswindow", luaV_iswindow},
966a5609669e Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
1064 {NULL, NULL}
966a5609669e Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
1065 };
966a5609669e Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
1066
2330
ea4bf6df1a8a Whitespace cleanup.
Bram Moolenaar <bram@vim.org>
parents: 2320
diff changeset
1067 static int
ea4bf6df1a8a Whitespace cleanup.
Bram Moolenaar <bram@vim.org>
parents: 2320
diff changeset
1068 luaopen_vim(lua_State *L)
ea4bf6df1a8a Whitespace cleanup.
Bram Moolenaar <bram@vim.org>
parents: 2320
diff changeset
1069 {
2320
966a5609669e Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
1070 /* set environment */
966a5609669e Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
1071 lua_newtable(L);
966a5609669e Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
1072 lua_newtable(L);
966a5609669e Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
1073 lua_pushliteral(L, "v");
966a5609669e Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
1074 lua_setfield(L, -2, "__mode");
966a5609669e Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
1075 lua_setmetatable(L, -2);
966a5609669e Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
1076 lua_replace(L, LUA_ENVIRONINDEX);
966a5609669e Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
1077 /* print */
966a5609669e Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
1078 lua_pushcfunction(L, luaV_print);
966a5609669e Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
1079 lua_setglobal(L, "print");
966a5609669e Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
1080 /* free */
966a5609669e Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
1081 lua_pushlightuserdata(L, (void *) LUAVIM_FREE);
966a5609669e Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
1082 lua_pushcfunction(L, luaV_free);
966a5609669e Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
1083 lua_rawset(L, LUA_REGISTRYINDEX);
966a5609669e Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
1084 /* register */
966a5609669e Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
1085 luaV_newmetatable(L, LUAVIM_BUFFER);
966a5609669e Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
1086 luaL_register(L, NULL, luaV_Buffer_mt);
966a5609669e Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
1087 luaV_newmetatable(L, LUAVIM_WINDOW);
966a5609669e Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
1088 luaL_register(L, NULL, luaV_Window_mt);
966a5609669e Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
1089 luaL_register(L, LUAVIM_NAME, luaV_module);
966a5609669e Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
1090 return 0;
966a5609669e Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
1091 }
966a5609669e Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
1092
2330
ea4bf6df1a8a Whitespace cleanup.
Bram Moolenaar <bram@vim.org>
parents: 2320
diff changeset
1093 static lua_State *
ea4bf6df1a8a Whitespace cleanup.
Bram Moolenaar <bram@vim.org>
parents: 2320
diff changeset
1094 luaV_newstate(void)
ea4bf6df1a8a Whitespace cleanup.
Bram Moolenaar <bram@vim.org>
parents: 2320
diff changeset
1095 {
2320
966a5609669e Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
1096 lua_State *L = luaL_newstate();
966a5609669e Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
1097 const luaL_Reg luaV_core_libs[] = {
2330
ea4bf6df1a8a Whitespace cleanup.
Bram Moolenaar <bram@vim.org>
parents: 2320
diff changeset
1098 {"", luaopen_base},
ea4bf6df1a8a Whitespace cleanup.
Bram Moolenaar <bram@vim.org>
parents: 2320
diff changeset
1099 {LUA_TABLIBNAME, luaopen_table},
ea4bf6df1a8a Whitespace cleanup.
Bram Moolenaar <bram@vim.org>
parents: 2320
diff changeset
1100 {LUA_STRLIBNAME, luaopen_string},
ea4bf6df1a8a Whitespace cleanup.
Bram Moolenaar <bram@vim.org>
parents: 2320
diff changeset
1101 {LUA_MATHLIBNAME, luaopen_math},
ea4bf6df1a8a Whitespace cleanup.
Bram Moolenaar <bram@vim.org>
parents: 2320
diff changeset
1102 {LUA_OSLIBNAME, luaopen_os}, /* restricted */
ea4bf6df1a8a Whitespace cleanup.
Bram Moolenaar <bram@vim.org>
parents: 2320
diff changeset
1103 {LUA_LOADLIBNAME, luaopen_package},
ea4bf6df1a8a Whitespace cleanup.
Bram Moolenaar <bram@vim.org>
parents: 2320
diff changeset
1104 {LUA_DBLIBNAME, luaopen_debug},
ea4bf6df1a8a Whitespace cleanup.
Bram Moolenaar <bram@vim.org>
parents: 2320
diff changeset
1105 {NULL, NULL}
2320
966a5609669e Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
1106 };
966a5609669e Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
1107 const char *os_funcs[] = {
2330
ea4bf6df1a8a Whitespace cleanup.
Bram Moolenaar <bram@vim.org>
parents: 2320
diff changeset
1108 "date", "clock", "time", "difftime", "getenv", NULL
2320
966a5609669e Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
1109 };
966a5609669e Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
1110 const luaL_Reg *reg = luaV_core_libs;
966a5609669e Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
1111 const char **s = os_funcs;
966a5609669e Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
1112 /* core libs */
2330
ea4bf6df1a8a Whitespace cleanup.
Bram Moolenaar <bram@vim.org>
parents: 2320
diff changeset
1113 for ( ; reg->func; reg++)
ea4bf6df1a8a Whitespace cleanup.
Bram Moolenaar <bram@vim.org>
parents: 2320
diff changeset
1114 {
ea4bf6df1a8a Whitespace cleanup.
Bram Moolenaar <bram@vim.org>
parents: 2320
diff changeset
1115 lua_pushcfunction(L, reg->func);
ea4bf6df1a8a Whitespace cleanup.
Bram Moolenaar <bram@vim.org>
parents: 2320
diff changeset
1116 lua_pushstring(L, reg->name);
ea4bf6df1a8a Whitespace cleanup.
Bram Moolenaar <bram@vim.org>
parents: 2320
diff changeset
1117 lua_call(L, 1, 0);
2320
966a5609669e Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
1118 }
966a5609669e Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
1119 /* restricted os lib */
966a5609669e Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
1120 lua_getglobal(L, LUA_OSLIBNAME);
966a5609669e Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
1121 lua_newtable(L);
2330
ea4bf6df1a8a Whitespace cleanup.
Bram Moolenaar <bram@vim.org>
parents: 2320
diff changeset
1122 for ( ; *s; s++)
ea4bf6df1a8a Whitespace cleanup.
Bram Moolenaar <bram@vim.org>
parents: 2320
diff changeset
1123 {
ea4bf6df1a8a Whitespace cleanup.
Bram Moolenaar <bram@vim.org>
parents: 2320
diff changeset
1124 lua_getfield(L, -2, *s);
ea4bf6df1a8a Whitespace cleanup.
Bram Moolenaar <bram@vim.org>
parents: 2320
diff changeset
1125 lua_setfield(L, -2, *s);
2320
966a5609669e Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
1126 }
966a5609669e Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
1127 lua_setglobal(L, LUA_OSLIBNAME);
966a5609669e Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
1128 lua_pop(L, 1); /* os table */
966a5609669e Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
1129 /* vim */
966a5609669e Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
1130 lua_pushcfunction(L, luaopen_vim);
966a5609669e Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
1131 lua_call(L, 0, 0);
966a5609669e Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
1132 return L;
966a5609669e Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
1133 }
966a5609669e Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
1134
2330
ea4bf6df1a8a Whitespace cleanup.
Bram Moolenaar <bram@vim.org>
parents: 2320
diff changeset
1135 static void
ea4bf6df1a8a Whitespace cleanup.
Bram Moolenaar <bram@vim.org>
parents: 2320
diff changeset
1136 luaV_setrange(lua_State *L, int line1, int line2)
ea4bf6df1a8a Whitespace cleanup.
Bram Moolenaar <bram@vim.org>
parents: 2320
diff changeset
1137 {
2320
966a5609669e Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
1138 lua_getglobal(L, LUAVIM_NAME);
966a5609669e Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
1139 lua_pushinteger(L, line1);
966a5609669e Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
1140 lua_setfield(L, -2, "firstline");
966a5609669e Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
1141 lua_pushinteger(L, line2);
966a5609669e Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
1142 lua_setfield(L, -2, "lastline");
966a5609669e Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
1143 lua_pop(L, 1); /* vim table */
966a5609669e Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
1144 }
966a5609669e Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
1145
966a5609669e Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
1146
966a5609669e Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
1147 /* ======= Interface ======= */
966a5609669e Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
1148
966a5609669e Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
1149 static lua_State *L = NULL;
966a5609669e Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
1150
2330
ea4bf6df1a8a Whitespace cleanup.
Bram Moolenaar <bram@vim.org>
parents: 2320
diff changeset
1151 static int
ea4bf6df1a8a Whitespace cleanup.
Bram Moolenaar <bram@vim.org>
parents: 2320
diff changeset
1152 lua_init(void)
ea4bf6df1a8a Whitespace cleanup.
Bram Moolenaar <bram@vim.org>
parents: 2320
diff changeset
1153 {
ea4bf6df1a8a Whitespace cleanup.
Bram Moolenaar <bram@vim.org>
parents: 2320
diff changeset
1154 if (L == NULL)
ea4bf6df1a8a Whitespace cleanup.
Bram Moolenaar <bram@vim.org>
parents: 2320
diff changeset
1155 {
2320
966a5609669e Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
1156 #ifdef DYNAMIC_LUA
2330
ea4bf6df1a8a Whitespace cleanup.
Bram Moolenaar <bram@vim.org>
parents: 2320
diff changeset
1157 if (!lua_enabled(TRUE))
ea4bf6df1a8a Whitespace cleanup.
Bram Moolenaar <bram@vim.org>
parents: 2320
diff changeset
1158 {
ea4bf6df1a8a Whitespace cleanup.
Bram Moolenaar <bram@vim.org>
parents: 2320
diff changeset
1159 EMSG(_("Lua library cannot be loaded."));
ea4bf6df1a8a Whitespace cleanup.
Bram Moolenaar <bram@vim.org>
parents: 2320
diff changeset
1160 return FAIL;
ea4bf6df1a8a Whitespace cleanup.
Bram Moolenaar <bram@vim.org>
parents: 2320
diff changeset
1161 }
2320
966a5609669e Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
1162 #endif
2330
ea4bf6df1a8a Whitespace cleanup.
Bram Moolenaar <bram@vim.org>
parents: 2320
diff changeset
1163 L = luaV_newstate();
2320
966a5609669e Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
1164 }
966a5609669e Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
1165 return OK;
966a5609669e Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
1166 }
966a5609669e Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
1167
2330
ea4bf6df1a8a Whitespace cleanup.
Bram Moolenaar <bram@vim.org>
parents: 2320
diff changeset
1168 void
ea4bf6df1a8a Whitespace cleanup.
Bram Moolenaar <bram@vim.org>
parents: 2320
diff changeset
1169 lua_end(void)
ea4bf6df1a8a Whitespace cleanup.
Bram Moolenaar <bram@vim.org>
parents: 2320
diff changeset
1170 {
ea4bf6df1a8a Whitespace cleanup.
Bram Moolenaar <bram@vim.org>
parents: 2320
diff changeset
1171 if (L != NULL)
ea4bf6df1a8a Whitespace cleanup.
Bram Moolenaar <bram@vim.org>
parents: 2320
diff changeset
1172 {
ea4bf6df1a8a Whitespace cleanup.
Bram Moolenaar <bram@vim.org>
parents: 2320
diff changeset
1173 lua_close(L);
ea4bf6df1a8a Whitespace cleanup.
Bram Moolenaar <bram@vim.org>
parents: 2320
diff changeset
1174 L = NULL;
2320
966a5609669e Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
1175 #ifdef DYNAMIC_LUA
2330
ea4bf6df1a8a Whitespace cleanup.
Bram Moolenaar <bram@vim.org>
parents: 2320
diff changeset
1176 end_dynamic_lua();
2320
966a5609669e Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
1177 #endif
966a5609669e Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
1178 }
966a5609669e Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
1179 }
966a5609669e Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
1180
966a5609669e Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
1181 /* ex commands */
2330
ea4bf6df1a8a Whitespace cleanup.
Bram Moolenaar <bram@vim.org>
parents: 2320
diff changeset
1182 void
ea4bf6df1a8a Whitespace cleanup.
Bram Moolenaar <bram@vim.org>
parents: 2320
diff changeset
1183 ex_lua(exarg_T *eap)
ea4bf6df1a8a Whitespace cleanup.
Bram Moolenaar <bram@vim.org>
parents: 2320
diff changeset
1184 {
2320
966a5609669e Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
1185 char *script;
966a5609669e Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
1186 if (lua_init() == FAIL) return;
966a5609669e Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
1187 script = (char *) script_get(eap, eap->arg);
2330
ea4bf6df1a8a Whitespace cleanup.
Bram Moolenaar <bram@vim.org>
parents: 2320
diff changeset
1188 if (!eap->skip)
ea4bf6df1a8a Whitespace cleanup.
Bram Moolenaar <bram@vim.org>
parents: 2320
diff changeset
1189 {
ea4bf6df1a8a Whitespace cleanup.
Bram Moolenaar <bram@vim.org>
parents: 2320
diff changeset
1190 char *s = (script) ? script : (char *) eap->arg;
ea4bf6df1a8a Whitespace cleanup.
Bram Moolenaar <bram@vim.org>
parents: 2320
diff changeset
1191 luaV_setrange(L, eap->line1, eap->line2);
ea4bf6df1a8a Whitespace cleanup.
Bram Moolenaar <bram@vim.org>
parents: 2320
diff changeset
1192 if (luaL_loadbuffer(L, s, strlen(s), LUAVIM_CHUNKNAME)
ea4bf6df1a8a Whitespace cleanup.
Bram Moolenaar <bram@vim.org>
parents: 2320
diff changeset
1193 || lua_pcall(L, 0, 0, 0))
ea4bf6df1a8a Whitespace cleanup.
Bram Moolenaar <bram@vim.org>
parents: 2320
diff changeset
1194 luaV_emsg(L);
2320
966a5609669e Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
1195 }
966a5609669e Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
1196 if (script != NULL) vim_free(script);
966a5609669e Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
1197 }
966a5609669e Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
1198
2330
ea4bf6df1a8a Whitespace cleanup.
Bram Moolenaar <bram@vim.org>
parents: 2320
diff changeset
1199 void
ea4bf6df1a8a Whitespace cleanup.
Bram Moolenaar <bram@vim.org>
parents: 2320
diff changeset
1200 ex_luado(exarg_T *eap)
ea4bf6df1a8a Whitespace cleanup.
Bram Moolenaar <bram@vim.org>
parents: 2320
diff changeset
1201 {
2320
966a5609669e Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
1202 linenr_T l;
966a5609669e Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
1203 const char *s = (const char *) eap->arg;
966a5609669e Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
1204 luaL_Buffer b;
966a5609669e Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
1205 size_t len;
966a5609669e Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
1206 if (lua_init() == FAIL) return;
2330
ea4bf6df1a8a Whitespace cleanup.
Bram Moolenaar <bram@vim.org>
parents: 2320
diff changeset
1207 if (u_save(eap->line1 - 1, eap->line2 + 1) == FAIL)
ea4bf6df1a8a Whitespace cleanup.
Bram Moolenaar <bram@vim.org>
parents: 2320
diff changeset
1208 {
ea4bf6df1a8a Whitespace cleanup.
Bram Moolenaar <bram@vim.org>
parents: 2320
diff changeset
1209 EMSG(_("cannot save undo information"));
ea4bf6df1a8a Whitespace cleanup.
Bram Moolenaar <bram@vim.org>
parents: 2320
diff changeset
1210 return;
2320
966a5609669e Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
1211 }
966a5609669e Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
1212 luaV_setrange(L, eap->line1, eap->line2);
966a5609669e Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
1213 luaL_buffinit(L, &b);
966a5609669e Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
1214 luaL_addlstring(&b, "return function(line) ", 22); /* header */
966a5609669e Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
1215 luaL_addlstring(&b, s, strlen(s));
966a5609669e Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
1216 luaL_addlstring(&b, " end", 4); /* footer */
966a5609669e Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
1217 luaL_pushresult(&b);
966a5609669e Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
1218 s = lua_tolstring(L, -1, &len);
2330
ea4bf6df1a8a Whitespace cleanup.
Bram Moolenaar <bram@vim.org>
parents: 2320
diff changeset
1219 if (luaL_loadbuffer(L, s, len, LUAVIM_CHUNKNAME))
ea4bf6df1a8a Whitespace cleanup.
Bram Moolenaar <bram@vim.org>
parents: 2320
diff changeset
1220 {
ea4bf6df1a8a Whitespace cleanup.
Bram Moolenaar <bram@vim.org>
parents: 2320
diff changeset
1221 luaV_emsg(L);
ea4bf6df1a8a Whitespace cleanup.
Bram Moolenaar <bram@vim.org>
parents: 2320
diff changeset
1222 lua_pop(L, 1); /* function body */
ea4bf6df1a8a Whitespace cleanup.
Bram Moolenaar <bram@vim.org>
parents: 2320
diff changeset
1223 return;
2320
966a5609669e Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
1224 }
966a5609669e Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
1225 lua_call(L, 0, 1);
966a5609669e Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
1226 lua_replace(L, -2); /* function -> body */
2330
ea4bf6df1a8a Whitespace cleanup.
Bram Moolenaar <bram@vim.org>
parents: 2320
diff changeset
1227 for (l = eap->line1; l <= eap->line2; l++)
ea4bf6df1a8a Whitespace cleanup.
Bram Moolenaar <bram@vim.org>
parents: 2320
diff changeset
1228 {
ea4bf6df1a8a Whitespace cleanup.
Bram Moolenaar <bram@vim.org>
parents: 2320
diff changeset
1229 lua_pushvalue(L, -1); /* function */
ea4bf6df1a8a Whitespace cleanup.
Bram Moolenaar <bram@vim.org>
parents: 2320
diff changeset
1230 luaV_pushline(L, curbuf, l); /* current line as arg */
ea4bf6df1a8a Whitespace cleanup.
Bram Moolenaar <bram@vim.org>
parents: 2320
diff changeset
1231 if (lua_pcall(L, 1, 1, 0))
ea4bf6df1a8a Whitespace cleanup.
Bram Moolenaar <bram@vim.org>
parents: 2320
diff changeset
1232 {
ea4bf6df1a8a Whitespace cleanup.
Bram Moolenaar <bram@vim.org>
parents: 2320
diff changeset
1233 luaV_emsg(L);
ea4bf6df1a8a Whitespace cleanup.
Bram Moolenaar <bram@vim.org>
parents: 2320
diff changeset
1234 break;
ea4bf6df1a8a Whitespace cleanup.
Bram Moolenaar <bram@vim.org>
parents: 2320
diff changeset
1235 }
ea4bf6df1a8a Whitespace cleanup.
Bram Moolenaar <bram@vim.org>
parents: 2320
diff changeset
1236 if (lua_isstring(L, -1)) /* update line? */
ea4bf6df1a8a Whitespace cleanup.
Bram Moolenaar <bram@vim.org>
parents: 2320
diff changeset
1237 {
2320
966a5609669e Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
1238 #ifdef HAVE_SANDBOX
2330
ea4bf6df1a8a Whitespace cleanup.
Bram Moolenaar <bram@vim.org>
parents: 2320
diff changeset
1239 luaV_checksandbox(L);
2320
966a5609669e Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
1240 #endif
2330
ea4bf6df1a8a Whitespace cleanup.
Bram Moolenaar <bram@vim.org>
parents: 2320
diff changeset
1241 ml_replace(l, luaV_toline(L, -1), TRUE);
ea4bf6df1a8a Whitespace cleanup.
Bram Moolenaar <bram@vim.org>
parents: 2320
diff changeset
1242 changed_bytes(l, 0);
ea4bf6df1a8a Whitespace cleanup.
Bram Moolenaar <bram@vim.org>
parents: 2320
diff changeset
1243 lua_pop(L, 1); /* result from luaV_toline */
ea4bf6df1a8a Whitespace cleanup.
Bram Moolenaar <bram@vim.org>
parents: 2320
diff changeset
1244 }
ea4bf6df1a8a Whitespace cleanup.
Bram Moolenaar <bram@vim.org>
parents: 2320
diff changeset
1245 lua_pop(L, 1); /* line */
2320
966a5609669e Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
1246 }
966a5609669e Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
1247 lua_pop(L, 1); /* function */
966a5609669e Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
1248 check_cursor();
966a5609669e Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
1249 update_screen(NOT_VALID);
966a5609669e Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
1250 }
966a5609669e Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
1251
2330
ea4bf6df1a8a Whitespace cleanup.
Bram Moolenaar <bram@vim.org>
parents: 2320
diff changeset
1252 void
ea4bf6df1a8a Whitespace cleanup.
Bram Moolenaar <bram@vim.org>
parents: 2320
diff changeset
1253 ex_luafile(exarg_T *eap)
ea4bf6df1a8a Whitespace cleanup.
Bram Moolenaar <bram@vim.org>
parents: 2320
diff changeset
1254 {
ea4bf6df1a8a Whitespace cleanup.
Bram Moolenaar <bram@vim.org>
parents: 2320
diff changeset
1255 if (lua_init() == FAIL)
ea4bf6df1a8a Whitespace cleanup.
Bram Moolenaar <bram@vim.org>
parents: 2320
diff changeset
1256 return;
ea4bf6df1a8a Whitespace cleanup.
Bram Moolenaar <bram@vim.org>
parents: 2320
diff changeset
1257 if (!eap->skip)
ea4bf6df1a8a Whitespace cleanup.
Bram Moolenaar <bram@vim.org>
parents: 2320
diff changeset
1258 {
ea4bf6df1a8a Whitespace cleanup.
Bram Moolenaar <bram@vim.org>
parents: 2320
diff changeset
1259 luaV_setrange(L, eap->line1, eap->line2);
ea4bf6df1a8a Whitespace cleanup.
Bram Moolenaar <bram@vim.org>
parents: 2320
diff changeset
1260 if (luaL_loadfile(L, (char *) eap->arg) || lua_pcall(L, 0, 0, 0))
ea4bf6df1a8a Whitespace cleanup.
Bram Moolenaar <bram@vim.org>
parents: 2320
diff changeset
1261 luaV_emsg(L);
2320
966a5609669e Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
1262 }
966a5609669e Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
1263 }
966a5609669e Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
1264
966a5609669e Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
1265 /* buffer */
2330
ea4bf6df1a8a Whitespace cleanup.
Bram Moolenaar <bram@vim.org>
parents: 2320
diff changeset
1266 void
ea4bf6df1a8a Whitespace cleanup.
Bram Moolenaar <bram@vim.org>
parents: 2320
diff changeset
1267 lua_buffer_free(buf_T *buf)
ea4bf6df1a8a Whitespace cleanup.
Bram Moolenaar <bram@vim.org>
parents: 2320
diff changeset
1268 {
2320
966a5609669e Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
1269 if (lua_init() == FAIL) return;
966a5609669e Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
1270 luaV_getfield(L, LUAVIM_FREE);
966a5609669e Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
1271 lua_pushlightuserdata(L, (void *) buf);
966a5609669e Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
1272 lua_call(L, 1, 0);
966a5609669e Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
1273 }
966a5609669e Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
1274
966a5609669e Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
1275 /* window */
2330
ea4bf6df1a8a Whitespace cleanup.
Bram Moolenaar <bram@vim.org>
parents: 2320
diff changeset
1276 void
ea4bf6df1a8a Whitespace cleanup.
Bram Moolenaar <bram@vim.org>
parents: 2320
diff changeset
1277 lua_window_free(win_T *win)
ea4bf6df1a8a Whitespace cleanup.
Bram Moolenaar <bram@vim.org>
parents: 2320
diff changeset
1278 {
2320
966a5609669e Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
1279 if (lua_init() == FAIL) return;
966a5609669e Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
1280 luaV_getfield(L, LUAVIM_FREE);
966a5609669e Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
1281 lua_pushlightuserdata(L, (void *) win);
966a5609669e Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
1282 lua_call(L, 1, 0);
966a5609669e Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
1283 }
966a5609669e Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
1284
966a5609669e Added Lua interfae. (Luis Carvalho)
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
1285 #endif