]> git.pld-linux.org Git - packages/vim.git/blob - 7.3.490
- add patches 7.3.619-743
[packages/vim.git] / 7.3.490
1 To: vim_dev@googlegroups.com
2 Subject: Patch 7.3.490
3 Fcc: outbox
4 From: Bram Moolenaar <Bram@moolenaar.net>
5 Mime-Version: 1.0
6 Content-Type: text/plain; charset=UTF-8
7 Content-Transfer-Encoding: 8bit
8 ------------
9
10 Patch 7.3.490
11 Problem:    Member confusion in Lua interface.
12 Solution:   Fix it.  Add luaeval(). (Taro Muraoka, Luis Carvalho)
13 Files:      runtime/doc/if_lua.txt, src/eval.c, src/if_lua.c,
14             src/proto/if_lua.pro
15
16
17 *** ../vim-7.3.489/runtime/doc/if_lua.txt       2010-08-15 21:57:14.000000000 +0200
18 --- runtime/doc/if_lua.txt      2012-04-05 16:41:35.000000000 +0200
19 ***************
20 *** 1,4 ****
21 ! *if_lua.txt*    For Vim version 7.3.  Last change: 2010 Jul 22
22   
23   
24                   VIM REFERENCE MANUAL    by Luis Carvalho
25 --- 1,4 ----
26 ! *if_lua.txt*    For Vim version 7.3.  Last change: 2012 Jan 16
27   
28   
29                   VIM REFERENCE MANUAL    by Luis Carvalho
30 ***************
31 *** 8,15 ****
32   
33   1. Commands                   |lua-commands|
34   2. The vim module             |lua-vim|
35 ! 3. Buffer userdata            |lua-buffer|
36 ! 4. Window userdata            |lua-window|
37   
38   {Vi does not have any of these commands}
39   
40 --- 8,18 ----
41   
42   1. Commands                   |lua-commands|
43   2. The vim module             |lua-vim|
44 ! 3. List userdata              |lua-list|
45 ! 4. Dict userdata              |lua-dict|
46 ! 5. Buffer userdata            |lua-buffer|
47 ! 6. Window userdata            |lua-window|
48 ! 7. The luaeval function               |lua-luaeval|
49   
50   {Vi does not have any of these commands}
51   
52 ***************
53 *** 88,98 ****
54   All these commands execute a Lua chunk from either the command line (:lua and
55   :luado) or a file (:luafile) with the given line [range]. Similarly to the Lua
56   interpreter, each chunk has its own scope and so only global variables are
57 ! shared between command calls. Lua default libraries "table", "string", "math",
58 ! and "package" are available, "io" and "debug" are not, and "os" is restricted
59 ! to functions "date", "clock", "time", "difftime", and "getenv". In addition,
60 ! Lua "print" function has its output redirected to the Vim message area, with
61 ! arguments separated by a white space instead of a tab.
62   
63   Lua uses the "vim" module (see |lua-vim|) to issue commands to Vim
64   and manage buffers (|lua-buffer|) and windows (|lua-window|). However,
65 --- 91,99 ----
66   All these commands execute a Lua chunk from either the command line (:lua and
67   :luado) or a file (:luafile) with the given line [range]. Similarly to the Lua
68   interpreter, each chunk has its own scope and so only global variables are
69 ! shared between command calls. All Lua default libraries are available. In
70 ! addition, Lua "print" function has its output redirected to the Vim message
71 ! area, with arguments separated by a white space instead of a tab.
72   
73   Lua uses the "vim" module (see |lua-vim|) to issue commands to Vim
74   and manage buffers (|lua-buffer|) and windows (|lua-window|). However,
75 ***************
76 *** 108,116 ****
77   module also includes routines for buffer, window, and current line queries,
78   Vim evaluation and command execution, and others.
79   
80 !       vim.isbuffer(value)     Returns 'true' (boolean, not string) if
81 !                               "value" is a buffer userdata and 'false'
82 !                               otherwise (see |lua-buffer|).
83   
84         vim.buffer([arg])       If "arg" is a number, returns buffer with
85                                 number "arg" in the buffer list or, if "arg"
86 --- 109,117 ----
87   module also includes routines for buffer, window, and current line queries,
88   Vim evaluation and command execution, and others.
89   
90 !       vim.list()              Returns an empty list (see |List|).
91
92 !       vim.dict()              Returns an empty dictionary (see |Dictionary|).
93   
94         vim.buffer([arg])       If "arg" is a number, returns buffer with
95                                 number "arg" in the buffer list or, if "arg"
96 ***************
97 *** 121,136 ****
98                                 'true' returns the first buffer in the buffer
99                                 list or else the current buffer.
100   
101 -       vim.iswindow(value)     Returns 'true' (boolean, not string) if
102 -                               "value" is a window userdata and
103 -                               'false' otherwise (see |lua-window|).
104
105         vim.window([arg])       If "arg" is a number, returns window with
106                                 number "arg" or 'nil' (nil value, not string)
107                                 if not found. Otherwise, if "toboolean(arg)"
108                                 is 'true' returns the first window or else the
109                                 current window.
110   
111         vim.command({cmd})      Executes the vim (ex-mode) command {cmd}.
112                                 Examples: >
113                                         :lua vim.command"set tw=60"
114 --- 122,142 ----
115                                 'true' returns the first buffer in the buffer
116                                 list or else the current buffer.
117   
118         vim.window([arg])       If "arg" is a number, returns window with
119                                 number "arg" or 'nil' (nil value, not string)
120                                 if not found. Otherwise, if "toboolean(arg)"
121                                 is 'true' returns the first window or else the
122                                 current window.
123   
124 +       vim.type({arg})         Returns the type of {arg}. It is equivalent to
125 +                               Lua's "type" function, but returns "list",
126 +                               "dict", "buffer", or "window" if {arg} is a
127 +                               list, dictionary, buffer, or window,
128 +                               respectively. Examples: >
129 +                                       :lua l = vim.list()
130 +                                       :lua print(type(l), vim.type(l))
131 +                                       :" userdata list
132 + <
133         vim.command({cmd})      Executes the vim (ex-mode) command {cmd}.
134                                 Examples: >
135                                         :lua vim.command"set tw=60"
136 ***************
137 *** 141,147 ****
138                                 Vim strings and numbers are directly converted
139                                 to Lua strings and numbers respectively. Vim
140                                 lists and dictionaries are converted to Lua
141 !                               tables (lists become integer-keyed tables).
142                                 Examples: >
143                                         :lua tw = vim.eval"&tw"
144                                         :lua print(vim.eval"{'a': 'one'}".a)
145 --- 147,153 ----
146                                 Vim strings and numbers are directly converted
147                                 to Lua strings and numbers respectively. Vim
148                                 lists and dictionaries are converted to Lua
149 !                               userdata (see |lua-list| and |lua-dict|).
150                                 Examples: >
151                                         :lua tw = vim.eval"&tw"
152                                         :lua print(vim.eval"{'a': 'one'}".a)
153 ***************
154 *** 157,163 ****
155   
156   
157   ==============================================================================
158 ! 3. Buffer userdata                                    *lua-buffer*
159   
160   Buffer userdata represent vim buffers. A buffer userdata "b" has the following
161   properties and methods:
162 --- 163,234 ----
163   
164   
165   ==============================================================================
166 ! 3. List userdata                                      *lua-list*
167
168 ! List userdata represent vim lists, and the interface tries to follow closely
169 ! Vim's syntax for lists. Since lists are objects, changes in list references in
170 ! Lua are reflected in Vim and vice-versa. A list "l" has the following
171 ! properties and methods:
172
173 ! Properties
174 ! ----------
175 !       o "#l" is the number of items in list "l", equivalent to "len(l)"
176 !           in Vim.
177 !       o "l[k]" returns the k-th item in "l"; "l" is zero-indexed, as in Vim.
178 !           To modify the k-th item, simply do "l[k] = newitem"; in
179 !           particular, "l[k] = nil" removes the k-th item from "l".
180 !       o "l()" returns an iterator for "l".
181
182 ! Methods
183 ! -------
184 !       o "l:add(item)" appends "item" to the end of "l".
185 !       o "l:insert(item[, pos])" inserts "item" at (optional)
186 !           position "pos" in the list. The default value for "pos" is 0.
187
188 ! Examples:
189 ! >
190 !       :let l = [1, 'item']
191 !       :lua l = vim.eval('l') -- same 'l'
192 !       :lua l:add(vim.list())
193 !       :lua l[0] = math.pi
194 !       :echo l[0] " 3.141593
195 !       :lua l[0] = nil -- remove first item
196 !       :lua l:insert(true, 1)
197 !       :lua print(l, #l, l[0], l[1], l[-1])
198 !       :lua for item in l() do print(item) end
199 ! <
200
201 ! ==============================================================================
202 ! 4. Dict userdata                                      *lua-dict*
203
204 ! Similarly to list userdata, dict userdata represent vim dictionaries; since
205 ! dictionaries are also objects, references are kept between Lua and Vim. A dict
206 ! "d" has the following properties:
207
208 ! Properties
209 ! ----------
210 !       o "#d" is the number of items in dict "d", equivalent to "len(d)"
211 !           in Vim.
212 !       o "d.key" or "d['key']" returns the value at entry "key" in "d".
213 !           To modify the entry at this key, simply do "d.key = newvalue"; in
214 !           particular, "d.key = nil" removes the entry from "d".
215 !       o "d()" returns an iterator for "d" and is equivalent to "items(d)" in
216 !           Vim.
217
218 ! Examples:
219 ! >
220 !       :let d = {'n':10}
221 !       :lua d = vim.eval('d') -- same 'd'
222 !       :lua print(d, d.n, #d)
223 !       :let d.self = d
224 !       :lua for k, v in d() do print(d, k, v) end
225 !       :lua d.x = math.pi
226 !       :lua d.self = nil -- remove entry
227 !       :echo d
228 ! <
229
230 ! ==============================================================================
231 ! 5. Buffer userdata                                    *lua-buffer*
232   
233   Buffer userdata represent vim buffers. A buffer userdata "b" has the following
234   properties and methods:
235 ***************
236 *** 209,215 ****
237   <
238   
239   ==============================================================================
240 ! 4. Window userdata                                    *lua-window*
241   
242   Window objects represent vim windows. A window userdata "w" has the following
243   properties and methods:
244 --- 280,286 ----
245   <
246   
247   ==============================================================================
248 ! 6. Window userdata                                    *lua-window*
249   
250   Window objects represent vim windows. A window userdata "w" has the following
251   properties and methods:
252 ***************
253 *** 241,244 ****
254   <
255   
256   ==============================================================================
257 !  vim:tw=78:ts=8:ft=help:norl:
258 --- 312,340 ----
259   <
260   
261   ==============================================================================
262 ! 7. The luaeval function                                       *lua-luaeval*
263
264 ! The (dual) equivalent of "vim.eval" for passing Lua values to Vim is
265 ! "luaeval". "luaeval" takes an expression string and an optional argument and
266 ! returns the result of the expression. It is semantically equivalent in Lua to:
267 ! >
268 !       local chunkheader = "local _A = select(1, ...) return "
269 !       function luaeval (expstr, arg)
270 !           local chunk = assert(loadstring(chunkheader .. expstr, "luaeval"))
271 !           return chunk(arg) -- return typval
272 !       end
273 ! <
274 ! Note that "_A" receives the argument to "luaeval". Examples: >
275
276 !       :echo luaeval('math.pi')
277 !       :lua a = vim.list():add('newlist')
278 !       :let a = luaeval('a')
279 !       :echo a[0] " 'newlist'
280 !       :function Rand(x,y) " random uniform between x and y
281 !       :  return luaeval('(_A.y-_A.x)*math.random()+_A.x', {'x':a:x,'y':a:y})
282 !       :  endfunction
283 !       :echo Rand(1,10)
284
285
286 ! ==============================================================================
287 !  vim:tw=78:ts=8:noet:ft=help:norl:
288 *** ../vim-7.3.489/src/eval.c   2012-03-28 16:49:25.000000000 +0200
289 --- src/eval.c  2012-04-05 16:41:35.000000000 +0200
290 ***************
291 *** 622,627 ****
292 --- 622,630 ----
293   static void f_log __ARGS((typval_T *argvars, typval_T *rettv));
294   static void f_log10 __ARGS((typval_T *argvars, typval_T *rettv));
295   #endif
296 + #ifdef FEAT_LUA
297 + static void f_luaeval __ARGS((typval_T *argvars, typval_T *rettv));
298 + #endif
299   static void f_map __ARGS((typval_T *argvars, typval_T *rettv));
300   static void f_maparg __ARGS((typval_T *argvars, typval_T *rettv));
301   static void f_mapcheck __ARGS((typval_T *argvars, typval_T *rettv));
302 ***************
303 *** 6777,6782 ****
304 --- 6780,6789 ----
305       /* v: vars */
306       set_ref_in_ht(&vimvarht, copyID);
307   
308 + #ifdef FEAT_LUA
309 +     set_ref_in_lua(copyID);
310 + #endif
311
312       /*
313        * 2. Free lists and dictionaries that are not referenced.
314        */
315 ***************
316 *** 7946,7951 ****
317 --- 7953,7961 ----
318       {"log",           1, 1, f_log},
319       {"log10",         1, 1, f_log10},
320   #endif
321 + #ifdef FEAT_LUA
322 +     {"luaeval",         1, 2, f_luaeval},
323 + #endif
324       {"map",           2, 2, f_map},
325       {"maparg",                1, 4, f_maparg},
326       {"mapcheck",      1, 3, f_mapcheck},
327 ***************
328 *** 13626,13631 ****
329 --- 13636,13658 ----
330   }
331   #endif
332   
333 + #ifdef FEAT_LUA
334 + /*
335 +  * "luaeval()" function
336 +  */
337 +     static void
338 + f_luaeval(argvars, rettv)
339 +     typval_T  *argvars;
340 +     typval_T  *rettv;
341 + {
342 +     char_u    *str;
343 +     char_u    buf[NUMBUFLEN];
344
345 +     str = get_tv_string_buf(&argvars[0], buf);
346 +     do_luaeval(str, argvars + 1, rettv);
347 + }
348 + #endif
349
350   /*
351    * "map()" function
352    */
353 *** ../vim-7.3.489/src/if_lua.c 2011-12-08 16:00:12.000000000 +0100
354 --- src/if_lua.c        2012-04-05 16:41:35.000000000 +0200
355 ***************
356 *** 1,4 ****
357 ! /* vi:set ts=8 sts=4 sw=4:
358    *
359    * VIM - Vi IMproved  by Bram Moolenaar
360    *
361 --- 1,4 ----
362 ! /* vi:set ts=8 sts=4 sw=4 noet:
363    *
364    * VIM - Vi IMproved  by Bram Moolenaar
365    *
366 ***************
367 *** 21,35 ****
368 --- 21,53 ----
369   
370   #define LUAVIM_CHUNKNAME "vim chunk"
371   #define LUAVIM_NAME "vim"
372 + #define LUAVIM_EVALNAME "luaeval"
373 + #define LUAVIM_EVALHEADER "local _A=select(1,...) return "
374   
375   typedef buf_T *luaV_Buffer;
376   typedef win_T *luaV_Window;
377 + typedef dict_T *luaV_Dict;
378 + typedef list_T *luaV_List;
379   typedef void (*msgfunc_T)(char_u *);
380   
381 + static const char LUAVIM_DICT[] = "dict";
382 + static const char LUAVIM_LIST[] = "list";
383   static const char LUAVIM_BUFFER[] = "buffer";
384   static const char LUAVIM_WINDOW[] = "window";
385   static const char LUAVIM_FREE[] = "luaV_free";
386 + static const char LUAVIM_LUAEVAL[] = "luaV_luaeval";
387 + static const char LUAVIM_SETREF[] = "luaV_setref";
388   
389 + /* most functions are closures with a cache table as first upvalue;
390 +  * get/setudata manage references to vim userdata in cache table through
391 +  * object pointers (light userdata) */
392 + #define luaV_getudata(L, v) \
393 +     lua_pushlightuserdata((L), (void *) (v)); \
394 +     lua_rawget((L), lua_upvalueindex(1))
395 + #define luaV_setudata(L, v) \
396 +     lua_pushlightuserdata((L), (void *) (v)); \
397 +     lua_pushvalue((L), -2); \
398 +     lua_rawset((L), lua_upvalueindex(1))
399   #define luaV_getfield(L, s) \
400       lua_pushlightuserdata((L), (void *)(s)); \
401       lua_rawget((L), LUA_REGISTRYINDEX)
402 ***************
403 *** 38,43 ****
404 --- 56,70 ----
405   #define luaV_msg(L) luaV_msgfunc((L), (msgfunc_T) msg)
406   #define luaV_emsg(L) luaV_msgfunc((L), (msgfunc_T) emsg)
407   
408 + static luaV_List *luaV_pushlist (lua_State *L, list_T *lis);
409 + static luaV_Dict *luaV_pushdict (lua_State *L, dict_T *dic);
410
411 + #if LUA_VERSION_NUM <= 501
412 + #define luaV_openlib(L, l, n) luaL_openlib(L, NULL, l, n)
413 + #define luaL_typeerror luaL_typerror
414 + #else
415 + #define luaV_openlib luaL_setfuncs
416 + #endif
417   
418   #ifdef DYNAMIC_LUA
419   
420 ***************
421 *** 54,85 ****
422   #endif
423   
424   /* lauxlib */
425   #define luaL_register dll_luaL_register
426   #define luaL_typerror dll_luaL_typerror
427   #define luaL_checklstring dll_luaL_checklstring
428   #define luaL_checkinteger dll_luaL_checkinteger
429   #define luaL_optinteger dll_luaL_optinteger
430   #define luaL_checktype dll_luaL_checktype
431   #define luaL_error dll_luaL_error
432 - #define luaL_loadfile dll_luaL_loadfile
433 - #define luaL_loadbuffer dll_luaL_loadbuffer
434   #define luaL_newstate dll_luaL_newstate
435   #define luaL_buffinit dll_luaL_buffinit
436 - #define luaL_prepbuffer dll_luaL_prepbuffer
437   #define luaL_addlstring dll_luaL_addlstring
438   #define luaL_pushresult dll_luaL_pushresult
439   /* lua */
440   #define lua_close dll_lua_close
441   #define lua_gettop dll_lua_gettop
442   #define lua_settop dll_lua_settop
443   #define lua_pushvalue dll_lua_pushvalue
444   #define lua_replace dll_lua_replace
445   #define lua_isnumber dll_lua_isnumber
446   #define lua_isstring dll_lua_isstring
447   #define lua_type dll_lua_type
448   #define lua_rawequal dll_lua_rawequal
449 - #define lua_tonumber dll_lua_tonumber
450 - #define lua_tointeger dll_lua_tointeger
451   #define lua_toboolean dll_lua_toboolean
452   #define lua_tolstring dll_lua_tolstring
453   #define lua_touserdata dll_lua_touserdata
454 --- 81,134 ----
455   #endif
456   
457   /* lauxlib */
458 + #if LUA_VERSION_NUM <= 501
459   #define luaL_register dll_luaL_register
460 + #define luaL_prepbuffer dll_luaL_prepbuffer
461 + #define luaL_openlib dll_luaL_openlib
462   #define luaL_typerror dll_luaL_typerror
463 + #define luaL_loadfile dll_luaL_loadfile
464 + #define luaL_loadbuffer dll_luaL_loadbuffer
465 + #else
466 + #define luaL_prepbuffsize dll_luaL_prepbuffsize
467 + #define luaL_setfuncs dll_luaL_setfuncs
468 + #define luaL_loadfilex dll_luaL_loadfilex
469 + #define luaL_loadbufferx dll_luaL_loadbufferx
470 + #define luaL_argerror dll_luaL_argerror
471 + #endif
472   #define luaL_checklstring dll_luaL_checklstring
473   #define luaL_checkinteger dll_luaL_checkinteger
474   #define luaL_optinteger dll_luaL_optinteger
475   #define luaL_checktype dll_luaL_checktype
476   #define luaL_error dll_luaL_error
477   #define luaL_newstate dll_luaL_newstate
478   #define luaL_buffinit dll_luaL_buffinit
479   #define luaL_addlstring dll_luaL_addlstring
480   #define luaL_pushresult dll_luaL_pushresult
481   /* lua */
482 + #if LUA_VERSION_NUM <= 501
483 + #define lua_tonumber dll_lua_tonumber
484 + #define lua_tointeger dll_lua_tointeger
485 + #define lua_call dll_lua_call
486 + #define lua_pcall dll_lua_pcall
487 + #else
488 + #define lua_tonumberx dll_lua_tonumberx
489 + #define lua_tointegerx dll_lua_tointegerx
490 + #define lua_callk dll_lua_callk
491 + #define lua_pcallk dll_lua_pcallk
492 + #define lua_getglobal dll_lua_getglobal
493 + #define lua_setglobal dll_lua_setglobal
494 + #define lua_typename dll_lua_typename
495 + #endif
496   #define lua_close dll_lua_close
497   #define lua_gettop dll_lua_gettop
498   #define lua_settop dll_lua_settop
499   #define lua_pushvalue dll_lua_pushvalue
500   #define lua_replace dll_lua_replace
501 + #define lua_remove dll_lua_remove
502   #define lua_isnumber dll_lua_isnumber
503   #define lua_isstring dll_lua_isstring
504   #define lua_type dll_lua_type
505   #define lua_rawequal dll_lua_rawequal
506   #define lua_toboolean dll_lua_toboolean
507   #define lua_tolstring dll_lua_tolstring
508   #define lua_touserdata dll_lua_touserdata
509 ***************
510 *** 94,109 ****
511   #define lua_pushlightuserdata dll_lua_pushlightuserdata
512   #define lua_getfield dll_lua_getfield
513   #define lua_rawget dll_lua_rawget
514   #define lua_createtable dll_lua_createtable
515   #define lua_newuserdata dll_lua_newuserdata
516   #define lua_getmetatable dll_lua_getmetatable
517   #define lua_setfield dll_lua_setfield
518   #define lua_rawset dll_lua_rawset
519   #define lua_rawseti dll_lua_rawseti
520 - #define lua_remove dll_lua_remove
521   #define lua_setmetatable dll_lua_setmetatable
522 - #define lua_call dll_lua_call
523 - #define lua_pcall dll_lua_pcall
524   /* libs */
525   #define luaopen_base dll_luaopen_base
526   #define luaopen_table dll_luaopen_table
527 --- 143,156 ----
528   #define lua_pushlightuserdata dll_lua_pushlightuserdata
529   #define lua_getfield dll_lua_getfield
530   #define lua_rawget dll_lua_rawget
531 + #define lua_rawgeti dll_lua_rawgeti
532   #define lua_createtable dll_lua_createtable
533   #define lua_newuserdata dll_lua_newuserdata
534   #define lua_getmetatable dll_lua_getmetatable
535   #define lua_setfield dll_lua_setfield
536   #define lua_rawset dll_lua_rawset
537   #define lua_rawseti dll_lua_rawseti
538   #define lua_setmetatable dll_lua_setmetatable
539   /* libs */
540   #define luaopen_base dll_luaopen_base
541   #define luaopen_table dll_luaopen_table
542 ***************
543 *** 116,147 ****
544   #define luaL_openlibs dll_luaL_openlibs
545   
546   /* lauxlib */
547   void (*dll_luaL_register) (lua_State *L, const char *libname, const luaL_Reg *l);
548   int (*dll_luaL_typerror) (lua_State *L, int narg, const char *tname);
549   const char *(*dll_luaL_checklstring) (lua_State *L, int numArg, size_t *l);
550   lua_Integer (*dll_luaL_checkinteger) (lua_State *L, int numArg);
551   lua_Integer (*dll_luaL_optinteger) (lua_State *L, int nArg, lua_Integer def);
552   void (*dll_luaL_checktype) (lua_State *L, int narg, int t);
553   int (*dll_luaL_error) (lua_State *L, const char *fmt, ...);
554 - int (*dll_luaL_loadfile) (lua_State *L, const char *filename);
555 - int (*dll_luaL_loadbuffer) (lua_State *L, const char *buff, size_t sz, const char *name);
556   lua_State *(*dll_luaL_newstate) (void);
557   void (*dll_luaL_buffinit) (lua_State *L, luaL_Buffer *B);
558 - char *(*dll_luaL_prepbuffer) (luaL_Buffer *B);
559   void (*dll_luaL_addlstring) (luaL_Buffer *B, const char *s, size_t l);
560   void (*dll_luaL_pushresult) (luaL_Buffer *B);
561   /* lua */
562   void       (*dll_lua_close) (lua_State *L);
563   int (*dll_lua_gettop) (lua_State *L);
564   void (*dll_lua_settop) (lua_State *L, int idx);
565   void (*dll_lua_pushvalue) (lua_State *L, int idx);
566   void (*dll_lua_replace) (lua_State *L, int idx);
567   int (*dll_lua_isnumber) (lua_State *L, int idx);
568   int (*dll_lua_isstring) (lua_State *L, int idx);
569   int (*dll_lua_type) (lua_State *L, int idx);
570   int (*dll_lua_rawequal) (lua_State *L, int idx1, int idx2);
571 - lua_Number (*dll_lua_tonumber) (lua_State *L, int idx);
572 - lua_Integer (*dll_lua_tointeger) (lua_State *L, int idx);
573   int (*dll_lua_toboolean) (lua_State *L, int idx);
574   const char *(*dll_lua_tolstring) (lua_State *L, int idx, size_t *len);
575   void *(*dll_lua_touserdata) (lua_State *L, int idx);
576 --- 163,218 ----
577   #define luaL_openlibs dll_luaL_openlibs
578   
579   /* lauxlib */
580 + #if LUA_VERSION_NUM <= 501
581   void (*dll_luaL_register) (lua_State *L, const char *libname, const luaL_Reg *l);
582 + char *(*dll_luaL_prepbuffer) (luaL_Buffer *B);
583 + void (*dll_luaL_openlib) (lua_State *L, const char *libname, const luaL_Reg *l, int nup);
584   int (*dll_luaL_typerror) (lua_State *L, int narg, const char *tname);
585 + int (*dll_luaL_loadfile) (lua_State *L, const char *filename);
586 + int (*dll_luaL_loadbuffer) (lua_State *L, const char *buff, size_t sz, const char *name);
587 + #else
588 + char *(*dll_luaL_prepbuffsize) (luaL_Buffer *B, size_t sz);
589 + void (*dll_luaL_setfuncs) (lua_State *L, const luaL_Reg *l, int nup);
590 + int (*dll_luaL_loadfilex) (lua_State *L, const char *filename, const char *mode);
591 + int (*dll_luaL_loadbufferx) (lua_State *L, const char *buff, size_t sz, const char *name, const char *mode);
592 + int (*dll_luaL_argerror) (lua_State *L, int numarg, const char *extramsg);
593 + #endif
594   const char *(*dll_luaL_checklstring) (lua_State *L, int numArg, size_t *l);
595   lua_Integer (*dll_luaL_checkinteger) (lua_State *L, int numArg);
596   lua_Integer (*dll_luaL_optinteger) (lua_State *L, int nArg, lua_Integer def);
597   void (*dll_luaL_checktype) (lua_State *L, int narg, int t);
598   int (*dll_luaL_error) (lua_State *L, const char *fmt, ...);
599   lua_State *(*dll_luaL_newstate) (void);
600   void (*dll_luaL_buffinit) (lua_State *L, luaL_Buffer *B);
601   void (*dll_luaL_addlstring) (luaL_Buffer *B, const char *s, size_t l);
602   void (*dll_luaL_pushresult) (luaL_Buffer *B);
603   /* lua */
604 + #if LUA_VERSION_NUM <= 501
605 + lua_Number (*dll_lua_tonumber) (lua_State *L, int idx);
606 + lua_Integer (*dll_lua_tointeger) (lua_State *L, int idx);
607 + void (*dll_lua_call) (lua_State *L, int nargs, int nresults);
608 + int (*dll_lua_pcall) (lua_State *L, int nargs, int nresults, int errfunc);
609 + #else
610 + lua_Number (*dll_lua_tonumberx) (lua_State *L, int idx, int *isnum);
611 + lua_Integer (*dll_lua_tointegerx) (lua_State *L, int idx, int *isnum);
612 + void (*dll_lua_callk) (lua_State *L, int nargs, int nresults, int ctx,
613 +         lua_CFunction k);
614 + int (*dll_lua_pcallk) (lua_State *L, int nargs, int nresults, int errfunc,
615 +         int ctx, lua_CFunction k);
616 + void (*dll_lua_getglobal) (lua_State *L, const char *var);
617 + void (*dll_lua_setglobal) (lua_State *L, const char *var);
618 + const char *(*dll_lua_typename) (lua_State *L, int tp);
619 + #endif
620   void       (*dll_lua_close) (lua_State *L);
621   int (*dll_lua_gettop) (lua_State *L);
622   void (*dll_lua_settop) (lua_State *L, int idx);
623   void (*dll_lua_pushvalue) (lua_State *L, int idx);
624   void (*dll_lua_replace) (lua_State *L, int idx);
625 + void (*dll_lua_remove) (lua_State *L, int idx);
626   int (*dll_lua_isnumber) (lua_State *L, int idx);
627   int (*dll_lua_isstring) (lua_State *L, int idx);
628   int (*dll_lua_type) (lua_State *L, int idx);
629   int (*dll_lua_rawequal) (lua_State *L, int idx1, int idx2);
630   int (*dll_lua_toboolean) (lua_State *L, int idx);
631   const char *(*dll_lua_tolstring) (lua_State *L, int idx, size_t *len);
632   void *(*dll_lua_touserdata) (lua_State *L, int idx);
633 ***************
634 *** 156,171 ****
635   void (*dll_lua_pushlightuserdata) (lua_State *L, void *p);
636   void (*dll_lua_getfield) (lua_State *L, int idx, const char *k);
637   void (*dll_lua_rawget) (lua_State *L, int idx);
638   void (*dll_lua_createtable) (lua_State *L, int narr, int nrec);
639   void *(*dll_lua_newuserdata) (lua_State *L, size_t sz);
640   int (*dll_lua_getmetatable) (lua_State *L, int objindex);
641   void (*dll_lua_setfield) (lua_State *L, int idx, const char *k);
642   void (*dll_lua_rawset) (lua_State *L, int idx);
643   void (*dll_lua_rawseti) (lua_State *L, int idx, int n);
644 - void (*dll_lua_remove) (lua_State *L, int idx);
645   int (*dll_lua_setmetatable) (lua_State *L, int objindex);
646 - void (*dll_lua_call) (lua_State *L, int nargs, int nresults);
647 - int (*dll_lua_pcall) (lua_State *L, int nargs, int nresults, int errfunc);
648   /* libs */
649   int (*dll_luaopen_base) (lua_State *L);
650   int (*dll_luaopen_table) (lua_State *L);
651 --- 227,240 ----
652   void (*dll_lua_pushlightuserdata) (lua_State *L, void *p);
653   void (*dll_lua_getfield) (lua_State *L, int idx, const char *k);
654   void (*dll_lua_rawget) (lua_State *L, int idx);
655 + void (*dll_lua_rawgeti) (lua_State *L, int idx, int n);
656   void (*dll_lua_createtable) (lua_State *L, int narr, int nrec);
657   void *(*dll_lua_newuserdata) (lua_State *L, size_t sz);
658   int (*dll_lua_getmetatable) (lua_State *L, int objindex);
659   void (*dll_lua_setfield) (lua_State *L, int idx, const char *k);
660   void (*dll_lua_rawset) (lua_State *L, int idx);
661   void (*dll_lua_rawseti) (lua_State *L, int idx, int n);
662   int (*dll_lua_setmetatable) (lua_State *L, int objindex);
663   /* libs */
664   int (*dll_luaopen_base) (lua_State *L);
665   int (*dll_luaopen_table) (lua_State *L);
666 ***************
667 *** 185,216 ****
668   
669   static const luaV_Reg luaV_dll[] = {
670       /* lauxlib */
671       {"luaL_register", (luaV_function) &dll_luaL_register},
672       {"luaL_typerror", (luaV_function) &dll_luaL_typerror},
673       {"luaL_checklstring", (luaV_function) &dll_luaL_checklstring},
674       {"luaL_checkinteger", (luaV_function) &dll_luaL_checkinteger},
675       {"luaL_optinteger", (luaV_function) &dll_luaL_optinteger},
676       {"luaL_checktype", (luaV_function) &dll_luaL_checktype},
677       {"luaL_error", (luaV_function) &dll_luaL_error},
678 -     {"luaL_loadfile", (luaV_function) &dll_luaL_loadfile},
679 -     {"luaL_loadbuffer", (luaV_function) &dll_luaL_loadbuffer},
680       {"luaL_newstate", (luaV_function) &dll_luaL_newstate},
681       {"luaL_buffinit", (luaV_function) &dll_luaL_buffinit},
682 -     {"luaL_prepbuffer", (luaV_function) &dll_luaL_prepbuffer},
683       {"luaL_addlstring", (luaV_function) &dll_luaL_addlstring},
684       {"luaL_pushresult", (luaV_function) &dll_luaL_pushresult},
685       /* lua */
686       {"lua_close", (luaV_function) &dll_lua_close},
687       {"lua_gettop", (luaV_function) &dll_lua_gettop},
688       {"lua_settop", (luaV_function) &dll_lua_settop},
689       {"lua_pushvalue", (luaV_function) &dll_lua_pushvalue},
690       {"lua_replace", (luaV_function) &dll_lua_replace},
691       {"lua_isnumber", (luaV_function) &dll_lua_isnumber},
692       {"lua_isstring", (luaV_function) &dll_lua_isstring},
693       {"lua_type", (luaV_function) &dll_lua_type},
694       {"lua_rawequal", (luaV_function) &dll_lua_rawequal},
695 -     {"lua_tonumber", (luaV_function) &dll_lua_tonumber},
696 -     {"lua_tointeger", (luaV_function) &dll_lua_tointeger},
697       {"lua_toboolean", (luaV_function) &dll_lua_toboolean},
698       {"lua_tolstring", (luaV_function) &dll_lua_tolstring},
699       {"lua_touserdata", (luaV_function) &dll_lua_touserdata},
700 --- 254,307 ----
701   
702   static const luaV_Reg luaV_dll[] = {
703       /* lauxlib */
704 + #if LUA_VERSION_NUM <= 501
705       {"luaL_register", (luaV_function) &dll_luaL_register},
706 +     {"luaL_prepbuffer", (luaV_function) &dll_luaL_prepbuffer},
707 +     {"luaL_openlib", (luaV_function) &dll_luaL_openlib},
708       {"luaL_typerror", (luaV_function) &dll_luaL_typerror},
709 +     {"luaL_loadfile", (luaV_function) &dll_luaL_loadfile},
710 +     {"luaL_loadbuffer", (luaV_function) &dll_luaL_loadbuffer},
711 + #else
712 +     {"luaL_prepbuffsize", (luaV_function) &dll_luaL_prepbuffsize},
713 +     {"luaL_setfuncs", (luaV_function) &dll_luaL_setfuncs},
714 +     {"luaL_loadfilex", (luaV_function) &dll_luaL_loadfilex},
715 +     {"luaL_loadbufferx", (luaV_function) &dll_luaL_loadbufferx},
716 +     {"luaL_argerror", (luaV_function) &dll_luaL_argerror},
717 + #endif
718       {"luaL_checklstring", (luaV_function) &dll_luaL_checklstring},
719       {"luaL_checkinteger", (luaV_function) &dll_luaL_checkinteger},
720       {"luaL_optinteger", (luaV_function) &dll_luaL_optinteger},
721       {"luaL_checktype", (luaV_function) &dll_luaL_checktype},
722       {"luaL_error", (luaV_function) &dll_luaL_error},
723       {"luaL_newstate", (luaV_function) &dll_luaL_newstate},
724       {"luaL_buffinit", (luaV_function) &dll_luaL_buffinit},
725       {"luaL_addlstring", (luaV_function) &dll_luaL_addlstring},
726       {"luaL_pushresult", (luaV_function) &dll_luaL_pushresult},
727       /* lua */
728 + #if LUA_VERSION_NUM <= 501
729 +     {"lua_tonumber", (luaV_function) &dll_lua_tonumber},
730 +     {"lua_tointeger", (luaV_function) &dll_lua_tointeger},
731 +     {"lua_call", (luaV_function) &dll_lua_call},
732 +     {"lua_pcall", (luaV_function) &dll_lua_pcall},
733 + #else
734 +     {"lua_tonumberx", (luaV_function) &dll_lua_tonumberx},
735 +     {"lua_tointegerx", (luaV_function) &dll_lua_tointegerx},
736 +     {"lua_callk", (luaV_function) &dll_lua_callk},
737 +     {"lua_pcallk", (luaV_function) &dll_lua_pcallk},
738 +     {"lua_getglobal", (luaV_function) &dll_lua_getglobal},
739 +     {"lua_setglobal", (luaV_function) &dll_lua_setglobal},
740 +     {"lua_typename", (luaV_function) &dll_lua_typename},
741 + #endif
742       {"lua_close", (luaV_function) &dll_lua_close},
743       {"lua_gettop", (luaV_function) &dll_lua_gettop},
744       {"lua_settop", (luaV_function) &dll_lua_settop},
745       {"lua_pushvalue", (luaV_function) &dll_lua_pushvalue},
746       {"lua_replace", (luaV_function) &dll_lua_replace},
747 +     {"lua_remove", (luaV_function) &dll_lua_remove},
748       {"lua_isnumber", (luaV_function) &dll_lua_isnumber},
749       {"lua_isstring", (luaV_function) &dll_lua_isstring},
750       {"lua_type", (luaV_function) &dll_lua_type},
751       {"lua_rawequal", (luaV_function) &dll_lua_rawequal},
752       {"lua_toboolean", (luaV_function) &dll_lua_toboolean},
753       {"lua_tolstring", (luaV_function) &dll_lua_tolstring},
754       {"lua_touserdata", (luaV_function) &dll_lua_touserdata},
755 ***************
756 *** 225,240 ****
757       {"lua_pushlightuserdata", (luaV_function) &dll_lua_pushlightuserdata},
758       {"lua_getfield", (luaV_function) &dll_lua_getfield},
759       {"lua_rawget", (luaV_function) &dll_lua_rawget},
760       {"lua_createtable", (luaV_function) &dll_lua_createtable},
761       {"lua_newuserdata", (luaV_function) &dll_lua_newuserdata},
762       {"lua_getmetatable", (luaV_function) &dll_lua_getmetatable},
763       {"lua_setfield", (luaV_function) &dll_lua_setfield},
764       {"lua_rawset", (luaV_function) &dll_lua_rawset},
765       {"lua_rawseti", (luaV_function) &dll_lua_rawseti},
766 -     {"lua_remove", (luaV_function) &dll_lua_remove},
767       {"lua_setmetatable", (luaV_function) &dll_lua_setmetatable},
768 -     {"lua_call", (luaV_function) &dll_lua_call},
769 -     {"lua_pcall", (luaV_function) &dll_lua_pcall},
770       /* libs */
771       {"luaopen_base", (luaV_function) &dll_luaopen_base},
772       {"luaopen_table", (luaV_function) &dll_luaopen_table},
773 --- 316,329 ----
774       {"lua_pushlightuserdata", (luaV_function) &dll_lua_pushlightuserdata},
775       {"lua_getfield", (luaV_function) &dll_lua_getfield},
776       {"lua_rawget", (luaV_function) &dll_lua_rawget},
777 +     {"lua_rawgeti", (luaV_function) &dll_lua_rawgeti},
778       {"lua_createtable", (luaV_function) &dll_lua_createtable},
779       {"lua_newuserdata", (luaV_function) &dll_lua_newuserdata},
780       {"lua_getmetatable", (luaV_function) &dll_lua_getmetatable},
781       {"lua_setfield", (luaV_function) &dll_lua_setfield},
782       {"lua_rawset", (luaV_function) &dll_lua_rawset},
783       {"lua_rawseti", (luaV_function) &dll_lua_rawseti},
784       {"lua_setmetatable", (luaV_function) &dll_lua_setmetatable},
785       /* libs */
786       {"luaopen_base", (luaV_function) &dll_luaopen_base},
787       {"luaopen_table", (luaV_function) &dll_luaopen_table},
788 ***************
789 *** 294,299 ****
790 --- 383,398 ----
791   
792   #endif /* DYNAMIC_LUA */
793   
794 + #if LUA_VERSION_NUM > 501
795 +     static int
796 + luaL_typeerror (lua_State *L, int narg, const char *tname)
797 + {
798 +     const char *msg = lua_pushfstring(L, "%s expected, got %s",
799 +             tname, luaL_typename(L, narg));
800 +     return luaL_argerror(L, narg, msg);
801 + }
802 + #endif
803
804   
805   /* =======   Internal   ======= */
806   
807 ***************
808 *** 327,343 ****
809   }
810   
811       static void *
812   luaV_checkudata(lua_State *L, int ud, const char *tname)
813   {
814       void *p = luaV_toudata(L, ud, tname);
815 !     if (p == NULL) luaL_typerror(L, ud, tname);
816       return p;
817   }
818   
819       static void
820   luaV_pushtypval(lua_State *L, typval_T *tv)
821   {
822 !     if (tv == NULL) luaL_error(L, "null type");
823       switch (tv->v_type)
824       {
825         case VAR_STRING:
826 --- 426,460 ----
827   }
828   
829       static void *
830 + luaV_checkcache(lua_State *L, void *p)
831 + {
832 +     luaV_getudata(L, p);
833 +     if (lua_isnil(L, -1)) luaL_error(L, "invalid object");
834 +     lua_pop(L, 1);
835 +     return p;
836 + }
837
838 + #define luaV_unbox(L,luatyp,ud) (*((luatyp *) lua_touserdata((L),(ud))))
839
840 + #define luaV_checkvalid(L,luatyp,ud) \
841 +     luaV_checkcache((L), (void *) luaV_unbox((L),luatyp,(ud)))
842
843 +     static void *
844   luaV_checkudata(lua_State *L, int ud, const char *tname)
845   {
846       void *p = luaV_toudata(L, ud, tname);
847 !     if (p == NULL) luaL_typeerror(L, ud, tname);
848       return p;
849   }
850   
851       static void
852   luaV_pushtypval(lua_State *L, typval_T *tv)
853   {
854 !     if (tv == NULL)
855 !     {
856 !       lua_pushnil(L);
857 !       return;
858 !     }
859       switch (tv->v_type)
860       {
861         case VAR_STRING:
862 ***************
863 *** 351,418 ****
864             lua_pushnumber(L, (lua_Number) tv->vval.v_float);
865             break;
866   #endif
867 !       case VAR_LIST: {
868 !           list_T *l = tv->vval.v_list;
869   
870 !           if (l != NULL)
871             {
872 !               /* check cache */
873 !               lua_pushlightuserdata(L, (void *) l);
874 !               lua_rawget(L, LUA_ENVIRONINDEX);
875 !               if (lua_isnil(L, -1)) /* not interned? */
876                 {
877 !                   listitem_T *li;
878 !                   int n = 0;
879 !                   lua_pop(L, 1); /* nil */
880 !                   lua_newtable(L);
881 !                   lua_pushlightuserdata(L, (void *) l);
882 !                   lua_pushvalue(L, -2);
883 !                   lua_rawset(L, LUA_ENVIRONINDEX);
884 !                   for (li = l->lv_first; li != NULL; li = li->li_next)
885 !                   {
886 !                       luaV_pushtypval(L, &li->li_tv);
887 !                       lua_rawseti(L, -2, ++n);
888 !                   }
889                 }
890 !           }
891 !           else lua_pushnil(L);
892 !           break;
893 !                      }
894 !       case VAR_DICT: {
895 !           dict_T *d = tv->vval.v_dict;
896
897 !           if (d != NULL)
898 !           {
899 !               /* check cache */
900 !               lua_pushlightuserdata(L, (void *) d);
901 !               lua_rawget(L, LUA_ENVIRONINDEX);
902 !               if (lua_isnil(L, -1)) /* not interned? */
903                 {
904 !                   hashtab_T *ht = &d->dv_hashtab;
905 !                   hashitem_T *hi;
906 !                   int n = ht->ht_used; /* remaining items */
907 !                   lua_pop(L, 1); /* nil */
908 !                   lua_newtable(L);
909 !                   lua_pushlightuserdata(L, (void *) d);
910 !                   lua_pushvalue(L, -2);
911 !                   lua_rawset(L, LUA_ENVIRONINDEX);
912 !                   for (hi = ht->ht_array; n > 0; hi++)
913 !                   {
914 !                       if (!HASHITEM_EMPTY(hi))
915 !                       {
916 !                           dictitem_T *di = dict_lookup(hi);
917 !                           luaV_pushtypval(L, &di->di_tv);
918 !                           lua_setfield(L, -2, (char *) hi->hi_key);
919 !                           n--;
920 !                       }
921 !                   }
922                 }
923             }
924 -           else lua_pushnil(L);
925             break;
926         }
927         default:
928 !           luaL_error(L, "invalid type");
929       }
930   }
931   
932 --- 468,537 ----
933             lua_pushnumber(L, (lua_Number) tv->vval.v_float);
934             break;
935   #endif
936 !       case VAR_LIST:
937 !           luaV_pushlist(L, tv->vval.v_list);
938 !           break;
939 !       case VAR_DICT:
940 !           luaV_pushdict(L, tv->vval.v_dict);
941 !           break;
942 !       default:
943 !           lua_pushnil(L);
944 !     }
945 ! }
946   
947 ! /* converts lua value at 'pos' to typval 'tv' */
948 !     static void
949 ! luaV_totypval (lua_State *L, int pos, typval_T *tv)
950 ! {
951 !     switch(lua_type(L, pos)) {
952 !       case LUA_TBOOLEAN:
953 !           tv->v_type = VAR_NUMBER;
954 !           tv->vval.v_number = (varnumber_T) lua_toboolean(L, pos);
955 !           break;
956 !       case LUA_TSTRING:
957 !           tv->v_type = VAR_STRING;
958 !           tv->vval.v_string = vim_strsave((char_u *) lua_tostring(L, pos));
959 !           break;
960 !       case LUA_TNUMBER:
961 ! #ifdef FEAT_FLOAT
962 !           tv->v_type = VAR_FLOAT;
963 !           tv->vval.v_float = (float_T) lua_tonumber(L, pos);
964 ! #else
965 !           tv->v_type = VAR_NUMBER;
966 !           tv->vval.v_number = (varnumber_T) lua_tointeger(L, pos);
967 ! #endif
968 !           break;
969 !       case LUA_TUSERDATA: {
970 !           void *p = lua_touserdata(L, pos);
971 !           if (lua_getmetatable(L, pos)) /* has metatable? */
972             {
973 !               /* check list */
974 !               luaV_getfield(L, LUAVIM_LIST);
975 !               if (lua_rawequal(L, -1, -2))
976                 {
977 !                   tv->v_type = VAR_LIST;
978 !                   tv->vval.v_list = *((luaV_List *) p);
979 !                   ++tv->vval.v_list->lv_refcount;
980 !                   lua_pop(L, 2); /* MTs */
981 !                   return;
982                 }
983 !               /* check dict */
984 !               luaV_getfield(L, LUAVIM_DICT);
985 !               if (lua_rawequal(L, -1, -3))
986                 {
987 !                   tv->v_type = VAR_DICT;
988 !                   tv->vval.v_dict = *((luaV_Dict *) p);
989 !                   ++tv->vval.v_dict->dv_refcount;
990 !                   lua_pop(L, 3); /* MTs */
991 !                   return;
992                 }
993 +               lua_pop(L, 3); /* MTs */
994             }
995             break;
996         }
997         default:
998 !           tv->v_type = VAR_NUMBER;
999 !           tv->vval.v_number = 0;
1000       }
1001   }
1002   
1003 ***************
1004 *** 481,569 ****
1005       lua_pop(L, 2); /* original and modified strings */
1006   }
1007   
1008   
1009 ! /* =======   Buffer type   ======= */
1010   
1011 !     static luaV_Buffer *
1012 ! luaV_newbuffer(lua_State *L, buf_T *buf)
1013   {
1014 !     luaV_Buffer *b = (luaV_Buffer *) lua_newuserdata(L, sizeof(luaV_Buffer));
1015 !     *b = buf;
1016 !     lua_pushlightuserdata(L, (void *) buf);
1017 !     lua_pushvalue(L, -2);
1018 !     lua_rawset(L, LUA_ENVIRONINDEX); /* env[buf] = udata */
1019 !     /* to avoid GC, store as key in env */
1020 !     lua_pushvalue(L, -1);
1021 !     lua_pushboolean(L, 1);
1022 !     lua_rawset(L, LUA_ENVIRONINDEX); /* env[udata] = true */
1023 !     /* set metatable */
1024 !     luaV_getfield(L, LUAVIM_BUFFER);
1025       lua_setmetatable(L, -2);
1026 !     return b;
1027   }
1028   
1029 !     static luaV_Buffer *
1030 ! luaV_pushbuffer (lua_State *L, buf_T *buf)
1031   {
1032 !     luaV_Buffer *b = NULL;
1033 !     if (buf == NULL)
1034 !       lua_pushnil(L);
1035 !     else {
1036 !       lua_pushlightuserdata(L, (void *) buf);
1037 !       lua_rawget(L, LUA_ENVIRONINDEX);
1038 !       if (lua_isnil(L, -1)) /* not interned? */
1039         {
1040 !           lua_pop(L, 1);
1041 !           b = luaV_newbuffer(L, buf);
1042         }
1043         else
1044 !           b = (luaV_Buffer *) lua_touserdata(L, -1);
1045       }
1046 !     return b;
1047   }
1048   
1049 ! /* Buffer metamethods */
1050   
1051       static int
1052 ! luaV_buffer_tostring(lua_State *L)
1053   {
1054 !     lua_pushfstring(L, "%s: %p", LUAVIM_BUFFER, lua_touserdata(L, 1));
1055       return 1;
1056   }
1057   
1058       static int
1059   luaV_buffer_len(lua_State *L)
1060   {
1061 !     luaV_Buffer *b = lua_touserdata(L, 1);
1062 !     lua_pushinteger(L, (*b)->b_ml.ml_line_count);
1063       return 1;
1064   }
1065   
1066       static int
1067   luaV_buffer_call(lua_State *L)
1068   {
1069 !     luaV_Buffer *b = (luaV_Buffer *) lua_touserdata(L, 1);
1070       lua_settop(L, 1);
1071 !     set_curbuf(*b, DOBUF_SPLIT);
1072       return 1;
1073   }
1074   
1075       static int
1076   luaV_buffer_index(lua_State *L)
1077   {
1078 !     luaV_Buffer *b = (luaV_Buffer *) lua_touserdata(L, 1);
1079       linenr_T n = (linenr_T) lua_tointeger(L, 2);
1080 !     if (n > 0 && n <= (*b)->b_ml.ml_line_count)
1081 !       luaV_pushline(L, *b, n);
1082       else if (lua_isstring(L, 2))
1083       {
1084         const char *s = lua_tostring(L, 2);
1085         if (strncmp(s, "name", 4) == 0)
1086 !           lua_pushstring(L, (char *) (*b)->b_sfname);
1087         else if (strncmp(s, "fname", 5) == 0)
1088 !           lua_pushstring(L, (char *) (*b)->b_ffname);
1089         else if (strncmp(s, "number", 6) == 0)
1090 !           lua_pushinteger(L, (*b)->b_fnum);
1091         /* methods */
1092         else if (strncmp(s,   "insert", 6) == 0
1093                 || strncmp(s, "next", 4) == 0
1094 --- 600,1107 ----
1095       lua_pop(L, 2); /* original and modified strings */
1096   }
1097   
1098 + #define luaV_newtype(typ,tname,luatyp,luatname) \
1099 +       static luatyp * \
1100 +     luaV_new##tname (lua_State *L, typ *obj) \
1101 +     { \
1102 +       luatyp *o = (luatyp *) lua_newuserdata(L, sizeof(luatyp)); \
1103 +       *o = obj; \
1104 +       luaV_setudata(L, obj); /* cache[obj] = udata */ \
1105 +       luaV_getfield(L, luatname); \
1106 +       lua_setmetatable(L, -2); \
1107 +       return o; \
1108 +     }
1109
1110 + #define luaV_pushtype(typ,tname,luatyp) \
1111 +       static luatyp * \
1112 +     luaV_push##tname (lua_State *L, typ *obj) \
1113 +     { \
1114 +       luatyp *o = NULL; \
1115 +       if (obj == NULL) \
1116 +           lua_pushnil(L); \
1117 +       else { \
1118 +           luaV_getudata(L, obj); \
1119 +           if (lua_isnil(L, -1)) /* not interned? */ \
1120 +           { \
1121 +               lua_pop(L, 1); \
1122 +               o = luaV_new##tname(L, obj); \
1123 +           } \
1124 +           else \
1125 +               o = (luatyp *) lua_touserdata(L, -1); \
1126 +       } \
1127 +       return o; \
1128 +     }
1129
1130 + #define luaV_type_tostring(tname,luatname) \
1131 +       static int \
1132 +     luaV_##tname##_tostring (lua_State *L) \
1133 +     { \
1134 +       lua_pushfstring(L, "%s: %p", luatname, lua_touserdata(L, 1)); \
1135 +       return 1; \
1136 +     }
1137
1138   
1139 ! /* adapted from eval.c */
1140
1141 ! #define listitem_alloc() (listitem_T *)alloc(sizeof(listitem_T))
1142
1143 !     static listitem_T *
1144 ! list_find (list_T *l, long n)
1145 ! {
1146 !     listitem_T *li;
1147 !     if (l == NULL || n < -l->lv_len || n >= l->lv_len)
1148 !       return NULL;
1149 !     if (n < 0) /* search backward? */
1150 !       for (li = l->lv_last; n < -1; li = li->li_prev)
1151 !           n++;
1152 !     else /* search forward */
1153 !       for (li = l->lv_first; n > 0; li = li->li_next)
1154 !           n--;
1155 !     return li;
1156 ! }
1157   
1158 !     static void
1159 ! list_remove (list_T *l, listitem_T *li)
1160   {
1161 !     listwatch_T *lw;
1162 !     --l->lv_len;
1163 !     /* fix watchers */
1164 !     for (lw = l->lv_watch; lw != NULL; lw = lw->lw_next)
1165 !       if (lw->lw_item == li)
1166 !           lw->lw_item = li->li_next;
1167 !     /* fix list pointers */
1168 !     if (li->li_next == NULL) /* last? */
1169 !       l->lv_last = li->li_prev;
1170 !     else
1171 !       li->li_next->li_prev = li->li_prev;
1172 !     if (li->li_prev == NULL) /* first? */
1173 !       l->lv_first = li->li_next;
1174 !     else
1175 !       li->li_prev->li_next = li->li_next;
1176 !     l->lv_idx_item = NULL;
1177 ! }
1178
1179 !     static void
1180 ! list_append(list_T *l, listitem_T *item)
1181 ! {
1182 !     if (l->lv_last == NULL) /* empty list? */
1183 !       l->lv_first = item;
1184 !     else
1185 !       l->lv_last->li_next = item;
1186 !     item->li_prev = l->lv_last;
1187 !     item->li_next = NULL;
1188 !     l->lv_last = item;
1189 !     ++l->lv_len;
1190 ! }
1191
1192 !     static int
1193 ! list_insert_tv(list_T *l, typval_T *tv, listitem_T *item)
1194 ! {
1195 !     listitem_T        *ni = listitem_alloc();
1196
1197 !     if (ni == NULL)
1198 !       return FAIL;
1199 !     copy_tv(tv, &ni->li_tv);
1200 !     if (item == NULL)
1201 !       list_append(l, ni);
1202 !     else
1203 !     {
1204 !       ni->li_prev = item->li_prev;
1205 !       ni->li_next = item;
1206 !       if (item->li_prev == NULL)
1207 !       {
1208 !           l->lv_first = ni;
1209 !           ++l->lv_idx;
1210 !       }
1211 !       else
1212 !       {
1213 !           item->li_prev->li_next = ni;
1214 !           l->lv_idx_item = NULL;
1215 !       }
1216 !       item->li_prev = ni;
1217 !       ++l->lv_len;
1218 !     }
1219 !     return OK;
1220 ! }
1221
1222 ! /* set references */
1223
1224 ! static void set_ref_in_tv (typval_T *tv, int copyID);
1225
1226 !     static void
1227 ! set_ref_in_dict(dict_T *d, int copyID)
1228 ! {
1229 !     hashtab_T *ht = &d->dv_hashtab;
1230 !     int n = ht->ht_used;
1231 !     hashitem_T *hi;
1232 !     for (hi = ht->ht_array; n > 0; ++hi)
1233 !       if (!HASHITEM_EMPTY(hi))
1234 !       {
1235 !           dictitem_T *di = dict_lookup(hi);
1236 !           set_ref_in_tv(&di->di_tv, copyID);
1237 !           --n;
1238 !       }
1239 ! }
1240
1241 !     static void
1242 ! set_ref_in_list(list_T *l, int copyID)
1243 ! {
1244 !     listitem_T *li;
1245 !     for (li = l->lv_first; li != NULL; li = li->li_next)
1246 !       set_ref_in_tv(&li->li_tv, copyID);
1247 ! }
1248
1249 !     static void
1250 ! set_ref_in_tv(typval_T *tv, int copyID)
1251 ! {
1252 !     if (tv->v_type == VAR_LIST)
1253 !     {
1254 !       list_T *l = tv->vval.v_list;
1255 !       if (l != NULL && l->lv_copyID != copyID)
1256 !       {
1257 !           l->lv_copyID = copyID;
1258 !           set_ref_in_list(l, copyID);
1259 !       }
1260 !     }
1261 !     else if (tv->v_type == VAR_DICT)
1262 !     {
1263 !       dict_T *d = tv->vval.v_dict;
1264 !       if (d != NULL && d->dv_copyID != copyID)
1265 !       {
1266 !           d->dv_copyID = copyID;
1267 !           set_ref_in_dict(d, copyID);
1268 !       }
1269 !     }
1270 ! }
1271
1272
1273 ! /* =======   List type   ======= */
1274
1275 !     static luaV_List *
1276 ! luaV_newlist (lua_State *L, list_T *lis)
1277 ! {
1278 !     luaV_List *l = (luaV_List *) lua_newuserdata(L, sizeof(luaV_List));
1279 !     *l = lis;
1280 !     lis->lv_refcount++; /* reference in Lua */
1281 !     luaV_setudata(L, lis); /* cache[lis] = udata */
1282 !     luaV_getfield(L, LUAVIM_LIST);
1283       lua_setmetatable(L, -2);
1284 !     return l;
1285   }
1286   
1287 ! luaV_pushtype(list_T, list, luaV_List)
1288 ! luaV_type_tostring(list, LUAVIM_LIST)
1289
1290 !     static int
1291 ! luaV_list_gc (lua_State *L)
1292   {
1293 !     list_unref(luaV_unbox(L, luaV_List, 1));
1294 !     return 0;
1295 ! }
1296
1297 !     static int
1298 ! luaV_list_len (lua_State *L)
1299 ! {
1300 !     list_T *l = luaV_unbox(L, luaV_List, 1);
1301 !     lua_pushinteger(L, (l == NULL) ? 0 : (int) l->lv_len);
1302 !     return 1;
1303 ! }
1304
1305 !     static int
1306 ! luaV_list_iter (lua_State *L)
1307 ! {
1308 !     listitem_T *li = (listitem_T *) lua_touserdata(L, lua_upvalueindex(2));
1309 !     if (li == NULL) return 0;
1310 !     luaV_pushtypval(L, &li->li_tv);
1311 !     lua_pushlightuserdata(L, (void *) li->li_next);
1312 !     lua_replace(L, lua_upvalueindex(2));
1313 !     return 1;
1314 ! }
1315
1316 !     static int
1317 ! luaV_list_call (lua_State *L)
1318 ! {
1319 !     list_T *l = luaV_unbox(L, luaV_List, 1);
1320 !     lua_pushvalue(L, lua_upvalueindex(1)); /* pass cache table along */
1321 !     lua_pushlightuserdata(L, (void *) l->lv_first);
1322 !     lua_pushcclosure(L, luaV_list_iter, 2);
1323 !     return 1;
1324 ! }
1325
1326 !     static int
1327 ! luaV_list_index (lua_State *L)
1328 ! {
1329 !     list_T *l = luaV_unbox(L, luaV_List, 1);
1330 !     if (lua_isnumber(L, 2)) /* list item? */
1331 !     {
1332 !       listitem_T *li = list_find(l, (long) luaL_checkinteger(L, 2));
1333 !       if (li == NULL)
1334 !           lua_pushnil(L);
1335 !       else
1336 !           luaV_pushtypval(L, &li->li_tv);
1337 !     }
1338 !     else if (lua_isstring(L, 2)) /* method? */
1339 !     {
1340 !       const char *s = lua_tostring(L, 2);
1341 !       if (strncmp(s, "add", 3) == 0
1342 !               || strncmp(s, "insert", 6) == 0
1343 !               || strncmp(s, "extend", 6) == 0)
1344         {
1345 !           lua_getmetatable(L, 1);
1346 !           lua_getfield(L, -1, s);
1347         }
1348         else
1349 !           lua_pushnil(L);
1350       }
1351 !     else
1352 !       lua_pushnil(L);
1353 !     return 1;
1354   }
1355   
1356 !     static int
1357 ! luaV_list_newindex (lua_State *L)
1358 ! {
1359 !     list_T *l = luaV_unbox(L, luaV_List, 1);
1360 !     long n = (long) luaL_checkinteger(L, 2);
1361 !     listitem_T *li;
1362 !     if (l->lv_lock)
1363 !       luaL_error(L, "list is locked");
1364 !     li = list_find(l, n);
1365 !     if (li == NULL) return 0;
1366 !     if (lua_isnil(L, 3)) /* remove? */
1367 !     {
1368 !       list_remove(l, li);
1369 !       clear_tv(&li->li_tv);
1370 !       vim_free(li);
1371 !     }
1372 !     else
1373 !     {
1374 !       typval_T v;
1375 !       luaV_totypval(L, 3, &v);
1376 !       clear_tv(&li->li_tv);
1377 !       copy_tv(&v, &li->li_tv);
1378 !     }
1379 !     return 0;
1380 ! }
1381   
1382       static int
1383 ! luaV_list_add (lua_State *L)
1384   {
1385 !     luaV_List *lis = luaV_checkudata(L, 1, LUAVIM_LIST);
1386 !     list_T *l = (list_T *) luaV_checkcache(L, (void *) *lis);
1387 !     listitem_T *li;
1388 !     if (l->lv_lock)
1389 !       luaL_error(L, "list is locked");
1390 !     li = listitem_alloc();
1391 !     if (li != NULL)
1392 !     {
1393 !       typval_T v;
1394 !       lua_settop(L, 2);
1395 !       luaV_totypval(L, 2, &v);
1396 !       copy_tv(&v, &li->li_tv);
1397 !       list_append(l, li);
1398 !     }
1399 !     lua_settop(L, 1);
1400       return 1;
1401   }
1402   
1403       static int
1404 + luaV_list_insert (lua_State *L)
1405 + {
1406 +     luaV_List *lis = luaV_checkudata(L, 1, LUAVIM_LIST);
1407 +     list_T *l = (list_T *) luaV_checkcache(L, (void *) *lis);
1408 +     long pos = luaL_optlong(L, 3, 0);
1409 +     listitem_T *li = NULL;
1410 +     typval_T v;
1411 +     if (l->lv_lock)
1412 +       luaL_error(L, "list is locked");
1413 +     if (pos < l->lv_len)
1414 +     {
1415 +       li = list_find(l, pos);
1416 +       if (li == NULL)
1417 +           luaL_error(L, "invalid position");
1418 +     }
1419 +     lua_settop(L, 2);
1420 +     luaV_totypval(L, 2, &v);
1421 +     list_insert_tv(l, &v, li);
1422 +     lua_settop(L, 1);
1423 +     return 1;
1424 + }
1425
1426 + static const luaL_Reg luaV_List_mt[] = {
1427 +     {"__tostring", luaV_list_tostring},
1428 +     {"__gc", luaV_list_gc},
1429 +     {"__len", luaV_list_len},
1430 +     {"__call", luaV_list_call},
1431 +     {"__index", luaV_list_index},
1432 +     {"__newindex", luaV_list_newindex},
1433 +     {"add", luaV_list_add},
1434 +     {"insert", luaV_list_insert},
1435 +     {NULL, NULL}
1436 + };
1437
1438
1439 + /* =======   Dict type   ======= */
1440
1441 +     static luaV_Dict *
1442 + luaV_newdict (lua_State *L, dict_T *dic)
1443 + {
1444 +     luaV_Dict *d = (luaV_Dict *) lua_newuserdata(L, sizeof(luaV_Dict));
1445 +     *d = dic;
1446 +     dic->dv_refcount++; /* reference in Lua */
1447 +     luaV_setudata(L, dic); /* cache[dic] = udata */
1448 +     luaV_getfield(L, LUAVIM_DICT);
1449 +     lua_setmetatable(L, -2);
1450 +     return d;
1451 + }
1452
1453 + luaV_pushtype(dict_T, dict, luaV_Dict)
1454 + luaV_type_tostring(dict, LUAVIM_DICT)
1455
1456 +     static int
1457 + luaV_dict_gc (lua_State *L)
1458 + {
1459 +     dict_unref(luaV_unbox(L, luaV_Dict, 1));
1460 +     return 0;
1461 + }
1462
1463 +     static int
1464 + luaV_dict_len (lua_State *L)
1465 + {
1466 +     dict_T *d = luaV_unbox(L, luaV_Dict, 1);
1467 +     lua_pushinteger(L, (d == NULL) ? 0 : (int) d->dv_hashtab.ht_used);
1468 +     return 1;
1469 + }
1470
1471 +     static int
1472 + luaV_dict_iter (lua_State *L)
1473 + {
1474 +     hashitem_T *hi = (hashitem_T *) lua_touserdata(L, lua_upvalueindex(2));
1475 +     int n = lua_tointeger(L, lua_upvalueindex(3));
1476 +     dictitem_T *di;
1477 +     if (n <= 0) return 0;
1478 +     while (HASHITEM_EMPTY(hi)) hi++;
1479 +     di = dict_lookup(hi);
1480 +     lua_pushstring(L, (char *) hi->hi_key);
1481 +     luaV_pushtypval(L, &di->di_tv);
1482 +     lua_pushlightuserdata(L, (void *) (hi + 1));
1483 +     lua_replace(L, lua_upvalueindex(2));
1484 +     lua_pushinteger(L, n - 1);
1485 +     lua_replace(L, lua_upvalueindex(3));
1486 +     return 2;
1487 + }
1488
1489 +     static int
1490 + luaV_dict_call (lua_State *L)
1491 + {
1492 +     dict_T *d = luaV_unbox(L, luaV_Dict, 1);
1493 +     hashtab_T *ht = &d->dv_hashtab;
1494 +     lua_pushvalue(L, lua_upvalueindex(1)); /* pass cache table along */
1495 +     lua_pushlightuserdata(L, (void *) ht->ht_array);
1496 +     lua_pushinteger(L, ht->ht_used); /* # remaining items */
1497 +     lua_pushcclosure(L, luaV_dict_iter, 3);
1498 +     return 1;
1499 + }
1500
1501 +     static int
1502 + luaV_dict_index (lua_State *L)
1503 + {
1504 +     dict_T *d = luaV_unbox(L, luaV_Dict, 1);
1505 +     char_u *key = (char_u *) luaL_checkstring(L, 2);
1506 +     dictitem_T *di = dict_find(d, key, -1);
1507 +     if (di == NULL)
1508 +       lua_pushnil(L);
1509 +     else
1510 +       luaV_pushtypval(L, &di->di_tv);
1511 +     return 1;
1512 + }
1513
1514 +     static int
1515 + luaV_dict_newindex (lua_State *L)
1516 + {
1517 +     dict_T *d = luaV_unbox(L, luaV_Dict, 1);
1518 +     char_u *key = (char_u *) luaL_checkstring(L, 2);
1519 +     dictitem_T *di;
1520 +     if (d->dv_lock)
1521 +       luaL_error(L, "dict is locked");
1522 +     di = dict_find(d, key, -1);
1523 +     if (di == NULL) /* non-existing key? */
1524 +     {
1525 +       if (lua_isnil(L, 3)) return 0;
1526 +       di = dictitem_alloc(key);
1527 +       if (di == NULL) return 0;
1528 +       if (dict_add(d, di) == FAIL)
1529 +       {
1530 +               vim_free(di);
1531 +               return 0;
1532 +       }
1533 +     }
1534 +     else
1535 +       clear_tv(&di->di_tv);
1536 +     if (lua_isnil(L, 3)) /* remove? */
1537 +     {
1538 +       hashitem_T *hi = hash_find(&d->dv_hashtab, di->di_key);
1539 +       hash_remove(&d->dv_hashtab, hi);
1540 +       dictitem_free(di);
1541 +     }
1542 +     else {
1543 +       typval_T v;
1544 +       luaV_totypval(L, 3, &v);
1545 +       copy_tv(&v, &di->di_tv);
1546 +     }
1547 +     return 0;
1548 + }
1549
1550 + static const luaL_Reg luaV_Dict_mt[] = {
1551 +     {"__tostring", luaV_dict_tostring},
1552 +     {"__gc", luaV_dict_gc},
1553 +     {"__len", luaV_dict_len},
1554 +     {"__call", luaV_dict_call},
1555 +     {"__index", luaV_dict_index},
1556 +     {"__newindex", luaV_dict_newindex},
1557 +     {NULL, NULL}
1558 + };
1559
1560
1561 + /* =======   Buffer type   ======= */
1562
1563 + luaV_newtype(buf_T, buffer, luaV_Buffer, LUAVIM_BUFFER)
1564 + luaV_pushtype(buf_T, buffer, luaV_Buffer)
1565 + luaV_type_tostring(buffer, LUAVIM_BUFFER)
1566
1567 +     static int
1568   luaV_buffer_len(lua_State *L)
1569   {
1570 !     buf_T *b = (buf_T *) luaV_checkvalid(L, luaV_Buffer, 1);
1571 !     lua_pushinteger(L, b->b_ml.ml_line_count);
1572       return 1;
1573   }
1574   
1575       static int
1576   luaV_buffer_call(lua_State *L)
1577   {
1578 !     buf_T *b = (buf_T *) luaV_checkvalid(L, luaV_Buffer, 1);
1579       lua_settop(L, 1);
1580 !     set_curbuf(b, DOBUF_SPLIT);
1581       return 1;
1582   }
1583   
1584       static int
1585   luaV_buffer_index(lua_State *L)
1586   {
1587 !     buf_T *b = (buf_T *) luaV_checkvalid(L, luaV_Buffer, 1);
1588       linenr_T n = (linenr_T) lua_tointeger(L, 2);
1589 !     if (n > 0 && n <= b->b_ml.ml_line_count)
1590 !       luaV_pushline(L, b, n);
1591       else if (lua_isstring(L, 2))
1592       {
1593         const char *s = lua_tostring(L, 2);
1594         if (strncmp(s, "name", 4) == 0)
1595 !           lua_pushstring(L, (char *) b->b_sfname);
1596         else if (strncmp(s, "fname", 5) == 0)
1597 !           lua_pushstring(L, (char *) b->b_ffname);
1598         else if (strncmp(s, "number", 6) == 0)
1599 !           lua_pushinteger(L, b->b_fnum);
1600         /* methods */
1601         else if (strncmp(s,   "insert", 6) == 0
1602                 || strncmp(s, "next", 4) == 0
1603 ***************
1604 *** 584,600 ****
1605       static int
1606   luaV_buffer_newindex(lua_State *L)
1607   {
1608 !     luaV_Buffer *b = (luaV_Buffer *) lua_touserdata(L, 1);
1609       linenr_T n = (linenr_T) luaL_checkinteger(L, 2);
1610   #ifdef HAVE_SANDBOX
1611       luaV_checksandbox(L);
1612   #endif
1613 !     if (n < 1 || n > (*b)->b_ml.ml_line_count)
1614         luaL_error(L, "invalid line number");
1615       if (lua_isnil(L, 3)) /* delete line */
1616       {
1617         buf_T *buf = curbuf;
1618 !       curbuf = *b;
1619         if (u_savedel(n, 1L) == FAIL)
1620         {
1621             curbuf = buf;
1622 --- 1122,1138 ----
1623       static int
1624   luaV_buffer_newindex(lua_State *L)
1625   {
1626 !     buf_T *b = (buf_T *) luaV_checkvalid(L, luaV_Buffer, 1);
1627       linenr_T n = (linenr_T) luaL_checkinteger(L, 2);
1628   #ifdef HAVE_SANDBOX
1629       luaV_checksandbox(L);
1630   #endif
1631 !     if (n < 1 || n > b->b_ml.ml_line_count)
1632         luaL_error(L, "invalid line number");
1633       if (lua_isnil(L, 3)) /* delete line */
1634       {
1635         buf_T *buf = curbuf;
1636 !       curbuf = b;
1637         if (u_savedel(n, 1L) == FAIL)
1638         {
1639             curbuf = buf;
1640 ***************
1641 *** 607,613 ****
1642         }
1643         else {
1644             deleted_lines_mark(n, 1L);
1645 !           if (*b == curwin->w_buffer) /* fix cursor in current window? */
1646             {
1647                 if (curwin->w_cursor.lnum >= n)
1648                 {
1649 --- 1145,1151 ----
1650         }
1651         else {
1652             deleted_lines_mark(n, 1L);
1653 !           if (b == curwin->w_buffer) /* fix cursor in current window? */
1654             {
1655                 if (curwin->w_cursor.lnum >= n)
1656                 {
1657 ***************
1658 *** 627,633 ****
1659       else if (lua_isstring(L, 3)) /* update line */
1660       {
1661         buf_T *buf = curbuf;
1662 !       curbuf = *b;
1663         if (u_savesub(n) == FAIL)
1664         {
1665             curbuf = buf;
1666 --- 1165,1171 ----
1667       else if (lua_isstring(L, 3)) /* update line */
1668       {
1669         buf_T *buf = curbuf;
1670 !       curbuf = b;
1671         if (u_savesub(n) == FAIL)
1672         {
1673             curbuf = buf;
1674 ***************
1675 *** 640,646 ****
1676         }
1677         else changed_bytes(n, 0);
1678         curbuf = buf;
1679 !       if (*b == curwin->w_buffer)
1680             check_cursor_col();
1681       }
1682       else
1683 --- 1178,1184 ----
1684         }
1685         else changed_bytes(n, 0);
1686         curbuf = buf;
1687 !       if (b == curwin->w_buffer)
1688             check_cursor_col();
1689       }
1690       else
1691 ***************
1692 *** 651,658 ****
1693       static int
1694   luaV_buffer_insert(lua_State *L)
1695   {
1696 !     luaV_Buffer *b = luaV_checkudata(L, 1, LUAVIM_BUFFER);
1697 !     linenr_T last = (*b)->b_ml.ml_line_count;
1698       linenr_T n = (linenr_T) luaL_optinteger(L, 3, last);
1699       buf_T *buf;
1700       luaL_checktype(L, 2, LUA_TSTRING);
1701 --- 1189,1197 ----
1702       static int
1703   luaV_buffer_insert(lua_State *L)
1704   {
1705 !     luaV_Buffer *lb = luaV_checkudata(L, 1, LUAVIM_BUFFER);
1706 !     buf_T *b = (buf_T *) luaV_checkcache(L, (void *) *lb);
1707 !     linenr_T last = b->b_ml.ml_line_count;
1708       linenr_T n = (linenr_T) luaL_optinteger(L, 3, last);
1709       buf_T *buf;
1710       luaL_checktype(L, 2, LUA_TSTRING);
1711 ***************
1712 *** 664,670 ****
1713       if (n > last) n = last;
1714       /* insert */
1715       buf = curbuf;
1716 !     curbuf = *b;
1717       if (u_save(n, n + 1) == FAIL)
1718       {
1719         curbuf = buf;
1720 --- 1203,1209 ----
1721       if (n > last) n = last;
1722       /* insert */
1723       buf = curbuf;
1724 !     curbuf = b;
1725       if (u_save(n, n + 1) == FAIL)
1726       {
1727         curbuf = buf;
1728 ***************
1729 *** 686,692 ****
1730   luaV_buffer_next(lua_State *L)
1731   {
1732       luaV_Buffer *b = luaV_checkudata(L, 1, LUAVIM_BUFFER);
1733 !     luaV_pushbuffer(L, (*b)->b_next);
1734       return 1;
1735   }
1736   
1737 --- 1225,1232 ----
1738   luaV_buffer_next(lua_State *L)
1739   {
1740       luaV_Buffer *b = luaV_checkudata(L, 1, LUAVIM_BUFFER);
1741 !     buf_T *buf = (buf_T *) luaV_checkcache(L, (void *) *b);
1742 !     luaV_pushbuffer(L, buf->b_next);
1743       return 1;
1744   }
1745   
1746 ***************
1747 *** 694,700 ****
1748   luaV_buffer_previous(lua_State *L)
1749   {
1750       luaV_Buffer *b = luaV_checkudata(L, 1, LUAVIM_BUFFER);
1751 !     luaV_pushbuffer(L, (*b)->b_prev);
1752       return 1;
1753   }
1754   
1755 --- 1234,1241 ----
1756   luaV_buffer_previous(lua_State *L)
1757   {
1758       luaV_Buffer *b = luaV_checkudata(L, 1, LUAVIM_BUFFER);
1759 !     buf_T *buf = (buf_T *) luaV_checkcache(L, (void *) *b);
1760 !     luaV_pushbuffer(L, buf->b_prev);
1761       return 1;
1762   }
1763   
1764 ***************
1765 *** 702,709 ****
1766   luaV_buffer_isvalid(lua_State *L)
1767   {
1768       luaV_Buffer *b = luaV_checkudata(L, 1, LUAVIM_BUFFER);
1769 !     lua_pushlightuserdata(L, (void *) (*b));
1770 !     lua_rawget(L, LUA_ENVIRONINDEX);
1771       lua_pushboolean(L, !lua_isnil(L, -1));
1772       return 1;
1773   }
1774 --- 1243,1249 ----
1775   luaV_buffer_isvalid(lua_State *L)
1776   {
1777       luaV_Buffer *b = luaV_checkudata(L, 1, LUAVIM_BUFFER);
1778 !     luaV_getudata(L, *b);
1779       lua_pushboolean(L, !lua_isnil(L, -1));
1780       return 1;
1781   }
1782 ***************
1783 *** 724,801 ****
1784   
1785   /* =======   Window type   ======= */
1786   
1787 !     static luaV_Window *
1788 ! luaV_newwindow(lua_State *L, win_T *win)
1789 ! {
1790 !     luaV_Window *w = (luaV_Window *) lua_newuserdata(L, sizeof(luaV_Window));
1791 !     *w = win;
1792 !     lua_pushlightuserdata(L, (void *) win);
1793 !     lua_pushvalue(L, -2);
1794 !     lua_rawset(L, LUA_ENVIRONINDEX); /* env[win] = udata */
1795 !     /* to avoid GC, store as key in env */
1796 !     lua_pushvalue(L, -1);
1797 !     lua_pushboolean(L, 1);
1798 !     lua_rawset(L, LUA_ENVIRONINDEX); /* env[udata] = true */
1799 !     /* set metatable */
1800 !     luaV_getfield(L, LUAVIM_WINDOW);
1801 !     lua_setmetatable(L, -2);
1802 !     return w;
1803 ! }
1804
1805 !     static luaV_Window *
1806 ! luaV_pushwindow(lua_State *L, win_T *win)
1807 ! {
1808 !     luaV_Window *w = NULL;
1809 !     if (win == NULL)
1810 !       lua_pushnil(L);
1811 !     else {
1812 !       lua_pushlightuserdata(L, (void *) win);
1813 !       lua_rawget(L, LUA_ENVIRONINDEX);
1814 !       if (lua_isnil(L, -1)) /* not interned? */
1815 !       {
1816 !           lua_pop(L, 1);
1817 !           w = luaV_newwindow(L, win);
1818 !       }
1819 !       else w = (luaV_Window *) lua_touserdata(L, -1);
1820 !     }
1821 !     return w;
1822 ! }
1823
1824 ! /* Window metamethods */
1825
1826 !     static int
1827 ! luaV_window_tostring(lua_State *L)
1828 ! {
1829 !     lua_pushfstring(L, "%s: %p", LUAVIM_WINDOW, lua_touserdata(L, 1));
1830 !     return 1;
1831 ! }
1832   
1833       static int
1834   luaV_window_call(lua_State *L)
1835   {
1836 !     luaV_Window *w = (luaV_Window *) lua_touserdata(L, 1);
1837       lua_settop(L, 1);
1838 !     win_goto(*w);
1839       return 1;
1840   }
1841   
1842       static int
1843   luaV_window_index(lua_State *L)
1844   {
1845 !     luaV_Window *w = (luaV_Window *) lua_touserdata(L, 1);
1846       const char *s = luaL_checkstring(L, 2);
1847       if (strncmp(s, "buffer", 6) == 0)
1848 !       luaV_pushbuffer(L, (*w)->w_buffer);
1849       else if (strncmp(s, "line", 4) == 0)
1850 !       lua_pushinteger(L, (*w)->w_cursor.lnum);
1851       else if (strncmp(s, "col", 3) == 0)
1852 !       lua_pushinteger(L, (*w)->w_cursor.col + 1);
1853   #ifdef FEAT_VERTSPLIT
1854       else if (strncmp(s, "width", 5) == 0)
1855 !       lua_pushinteger(L, W_WIDTH((*w)));
1856   #endif
1857       else if (strncmp(s, "height", 6) == 0)
1858 !       lua_pushinteger(L, (*w)->w_height);
1859       /* methods */
1860       else if (strncmp(s,   "next", 4) == 0
1861             || strncmp(s, "previous", 8) == 0
1862 --- 1264,1299 ----
1863   
1864   /* =======   Window type   ======= */
1865   
1866 ! luaV_newtype(win_T, window, luaV_Window, LUAVIM_WINDOW)
1867 ! luaV_pushtype(win_T, window, luaV_Window)
1868 ! luaV_type_tostring(window, LUAVIM_WINDOW)
1869   
1870       static int
1871   luaV_window_call(lua_State *L)
1872   {
1873 !     win_T *w = (win_T *) luaV_checkvalid(L, luaV_Window, 1);
1874       lua_settop(L, 1);
1875 !     win_goto(w);
1876       return 1;
1877   }
1878   
1879       static int
1880   luaV_window_index(lua_State *L)
1881   {
1882 !     win_T *w = (win_T *) luaV_checkvalid(L, luaV_Window, 1);
1883       const char *s = luaL_checkstring(L, 2);
1884       if (strncmp(s, "buffer", 6) == 0)
1885 !       luaV_pushbuffer(L, w->w_buffer);
1886       else if (strncmp(s, "line", 4) == 0)
1887 !       lua_pushinteger(L, w->w_cursor.lnum);
1888       else if (strncmp(s, "col", 3) == 0)
1889 !       lua_pushinteger(L, w->w_cursor.col + 1);
1890   #ifdef FEAT_VERTSPLIT
1891       else if (strncmp(s, "width", 5) == 0)
1892 !       lua_pushinteger(L, W_WIDTH(w));
1893   #endif
1894       else if (strncmp(s, "height", 6) == 0)
1895 !       lua_pushinteger(L, w->w_height);
1896       /* methods */
1897       else if (strncmp(s,   "next", 4) == 0
1898             || strncmp(s, "previous", 8) == 0
1899 ***************
1900 *** 812,818 ****
1901       static int
1902   luaV_window_newindex (lua_State *L)
1903   {
1904 !     luaV_Window *w = (luaV_Window *) lua_touserdata(L, 1);
1905       const char *s = luaL_checkstring(L, 2);
1906       int v = luaL_checkinteger(L, 3);
1907       if (strncmp(s, "line", 4) == 0)
1908 --- 1310,1316 ----
1909       static int
1910   luaV_window_newindex (lua_State *L)
1911   {
1912 !     win_T *w = (win_T *) luaV_checkvalid(L, luaV_Window, 1);
1913       const char *s = luaL_checkstring(L, 2);
1914       int v = luaL_checkinteger(L, 3);
1915       if (strncmp(s, "line", 4) == 0)
1916 ***************
1917 *** 820,828 ****
1918   #ifdef HAVE_SANDBOX
1919         luaV_checksandbox(L);
1920   #endif
1921 !       if (v < 1 || v > (*w)->w_buffer->b_ml.ml_line_count)
1922             luaL_error(L, "line out of range");
1923 !       (*w)->w_cursor.lnum = v;
1924         update_screen(VALID);
1925       }
1926       else if (strncmp(s, "col", 3) == 0)
1927 --- 1318,1326 ----
1928   #ifdef HAVE_SANDBOX
1929         luaV_checksandbox(L);
1930   #endif
1931 !       if (v < 1 || v > w->w_buffer->b_ml.ml_line_count)
1932             luaL_error(L, "line out of range");
1933 !       w->w_cursor.lnum = v;
1934         update_screen(VALID);
1935       }
1936       else if (strncmp(s, "col", 3) == 0)
1937 ***************
1938 *** 830,836 ****
1939   #ifdef HAVE_SANDBOX
1940         luaV_checksandbox(L);
1941   #endif
1942 !       (*w)->w_cursor.col = v - 1;
1943         update_screen(VALID);
1944       }
1945   #ifdef FEAT_VERTSPLIT
1946 --- 1328,1334 ----
1947   #ifdef HAVE_SANDBOX
1948         luaV_checksandbox(L);
1949   #endif
1950 !       w->w_cursor.col = v - 1;
1951         update_screen(VALID);
1952       }
1953   #ifdef FEAT_VERTSPLIT
1954 ***************
1955 *** 840,846 ****
1956   #ifdef FEAT_GUI
1957         need_mouse_correct = TRUE;
1958   #endif
1959 !       curwin = *w;
1960         win_setwidth(v);
1961         curwin = win;
1962       }
1963 --- 1338,1344 ----
1964   #ifdef FEAT_GUI
1965         need_mouse_correct = TRUE;
1966   #endif
1967 !       curwin = w;
1968         win_setwidth(v);
1969         curwin = win;
1970       }
1971 ***************
1972 *** 851,857 ****
1973   #ifdef FEAT_GUI
1974         need_mouse_correct = TRUE;
1975   #endif
1976 !       curwin = *w;
1977         win_setheight(v);
1978         curwin = win;
1979       }
1980 --- 1349,1355 ----
1981   #ifdef FEAT_GUI
1982         need_mouse_correct = TRUE;
1983   #endif
1984 !       curwin = w;
1985         win_setheight(v);
1986         curwin = win;
1987       }
1988 ***************
1989 *** 864,870 ****
1990   luaV_window_next(lua_State *L)
1991   {
1992       luaV_Window *w = luaV_checkudata(L, 1, LUAVIM_WINDOW);
1993 !     luaV_pushwindow(L, (*w)->w_next);
1994       return 1;
1995   }
1996   
1997 --- 1362,1369 ----
1998   luaV_window_next(lua_State *L)
1999   {
2000       luaV_Window *w = luaV_checkudata(L, 1, LUAVIM_WINDOW);
2001 !     win_T *win = (win_T *) luaV_checkcache(L, (void *) *w);
2002 !     luaV_pushwindow(L, win->w_next);
2003       return 1;
2004   }
2005   
2006 ***************
2007 *** 872,878 ****
2008   luaV_window_previous(lua_State *L)
2009   {
2010       luaV_Window *w = luaV_checkudata(L, 1, LUAVIM_WINDOW);
2011 !     luaV_pushwindow(L, (*w)->w_prev);
2012       return 1;
2013   }
2014   
2015 --- 1371,1378 ----
2016   luaV_window_previous(lua_State *L)
2017   {
2018       luaV_Window *w = luaV_checkudata(L, 1, LUAVIM_WINDOW);
2019 !     win_T *win = (win_T *) luaV_checkcache(L, (void *) *w);
2020 !     luaV_pushwindow(L, win->w_prev);
2021       return 1;
2022   }
2023   
2024 ***************
2025 *** 880,887 ****
2026   luaV_window_isvalid(lua_State *L)
2027   {
2028       luaV_Window *w = luaV_checkudata(L, 1, LUAVIM_WINDOW);
2029 !     lua_pushlightuserdata(L, (void *) (*w));
2030 !     lua_rawget(L, LUA_ENVIRONINDEX);
2031       lua_pushboolean(L, !lua_isnil(L, -1));
2032       return 1;
2033   }
2034 --- 1380,1386 ----
2035   luaV_window_isvalid(lua_State *L)
2036   {
2037       luaV_Window *w = luaV_checkudata(L, 1, LUAVIM_WINDOW);
2038 !     luaV_getudata(L, *w);
2039       lua_pushboolean(L, !lua_isnil(L, -1));
2040       return 1;
2041   }
2042 ***************
2043 *** 983,988 ****
2044 --- 1482,1509 ----
2045   }
2046   
2047       static int
2048 + luaV_list(lua_State *L)
2049 + {
2050 +     list_T *l = list_alloc();
2051 +     if (l == NULL)
2052 +       lua_pushnil(L);
2053 +     else
2054 +       luaV_newlist(L, l);
2055 +     return 1;
2056 + }
2057
2058 +     static int
2059 + luaV_dict(lua_State *L)
2060 + {
2061 +     dict_T *d = dict_alloc();
2062 +     if (d == NULL)
2063 +       lua_pushnil(L);
2064 +     else
2065 +       luaV_newdict(L, d);
2066 +     return 1;
2067 + }
2068
2069 +     static int
2070   luaV_buffer(lua_State *L)
2071   {
2072       buf_T *buf;
2073 ***************
2074 *** 1008,1022 ****
2075                     break;
2076             }
2077         }
2078 -       if (buf == NULL) /* not found? */
2079 -           lua_pushnil(L);
2080 -       else
2081 -           luaV_pushbuffer(L, buf);
2082       }
2083 !     else {
2084         buf = (lua_toboolean(L, 1)) ? firstbuf : curbuf; /* first buffer? */
2085 !       luaV_pushbuffer(L, buf);
2086 !     }
2087       return 1;
2088   }
2089   
2090 --- 1529,1538 ----
2091                     break;
2092             }
2093         }
2094       }
2095 !     else
2096         buf = (lua_toboolean(L, 1)) ? firstbuf : curbuf; /* first buffer? */
2097 !     luaV_pushbuffer(L, buf);
2098       return 1;
2099   }
2100   
2101 ***************
2102 *** 1029,1043 ****
2103         int n = lua_tointeger(L, 1);
2104         for (win = firstwin; win != NULL; win = win->w_next, n--)
2105             if (n == 1) break;
2106 -       if (win == NULL) /* not found? */
2107 -           lua_pushnil(L);
2108 -       else
2109 -           luaV_pushwindow(L, win);
2110       }
2111 !     else {
2112         win = (lua_toboolean(L, 1)) ? firstwin : curwin; /* first window? */
2113 !       luaV_pushwindow(L, win);
2114 !     }
2115       return 1;
2116   }
2117   
2118 --- 1545,1554 ----
2119         int n = lua_tointeger(L, 1);
2120         for (win = firstwin; win != NULL; win = win->w_next, n--)
2121             if (n == 1) break;
2122       }
2123 !     else
2124         win = (lua_toboolean(L, 1)) ? firstwin : curwin; /* first window? */
2125 !     luaV_pushwindow(L, win);
2126       return 1;
2127   }
2128   
2129 ***************
2130 *** 1054,1086 ****
2131   }
2132   
2133       static int
2134 ! luaV_isbuffer(lua_State *L)
2135 ! {
2136 !     lua_pushboolean(L, luaV_toudata(L, 1, LUAVIM_BUFFER) != NULL);
2137 !     return 1;
2138 ! }
2139
2140 !     static int
2141 ! luaV_iswindow(lua_State *L)
2142   {
2143 !     lua_pushboolean(L, luaV_toudata(L, 1, LUAVIM_WINDOW) != NULL);
2144 !     return 1;
2145 ! }
2146
2147 ! /* for freeing buffer and window objects; lightuserdata as arg */
2148 !     static int
2149 ! luaV_free(lua_State *L)
2150 ! {
2151 !     lua_pushvalue(L, 1); /* lightudata */
2152 !     lua_rawget(L, LUA_ENVIRONINDEX);
2153 !     if (!lua_isnil(L, -1))
2154       {
2155 !       lua_pushnil(L);
2156 !       lua_rawset(L, LUA_ENVIRONINDEX); /* env[udata] = nil */
2157 !       lua_pushnil(L);
2158 !       lua_rawset(L, LUA_ENVIRONINDEX); /* env[lightudata] = nil */
2159       }
2160 !     return 0;
2161   }
2162   
2163   static const luaL_Reg luaV_module[] = {
2164 --- 1565,1606 ----
2165   }
2166   
2167       static int
2168 ! luaV_type(lua_State *L)
2169   {
2170 !     luaL_checkany(L, 1);
2171 !     if (lua_type(L, 1) == LUA_TUSERDATA) /* check vim udata? */
2172       {
2173 !       lua_settop(L, 1);
2174 !       if (lua_getmetatable(L, 1))
2175 !       {
2176 !           luaV_getfield(L, LUAVIM_LIST);
2177 !           if (lua_rawequal(L, -1, 2))
2178 !           {
2179 !               lua_pushstring(L, "list");
2180 !               return 1;
2181 !           }
2182 !           luaV_getfield(L, LUAVIM_DICT);
2183 !           if (lua_rawequal(L, -1, 2))
2184 !           {
2185 !               lua_pushstring(L, "dict");
2186 !               return 1;
2187 !           }
2188 !           luaV_getfield(L, LUAVIM_BUFFER);
2189 !           if (lua_rawequal(L, -1, 2))
2190 !           {
2191 !               lua_pushstring(L, "buffer");
2192 !               return 1;
2193 !           }
2194 !           luaV_getfield(L, LUAVIM_WINDOW);
2195 !           if (lua_rawequal(L, -1, 2))
2196 !           {
2197 !               lua_pushstring(L, "window");
2198 !               return 1;
2199 !           }
2200 !       }
2201       }
2202 !     lua_pushstring(L, luaL_typename(L, 1)); /* fallback */
2203 !     return 1;
2204   }
2205   
2206   static const luaL_Reg luaV_module[] = {
2207 ***************
2208 *** 1088,1111 ****
2209       {"eval", luaV_eval},
2210       {"beep", luaV_beep},
2211       {"line", luaV_line},
2212       {"buffer", luaV_buffer},
2213       {"window", luaV_window},
2214       {"open", luaV_open},
2215 !     {"isbuffer", luaV_isbuffer},
2216 !     {"iswindow", luaV_iswindow},
2217       {NULL, NULL}
2218   };
2219   
2220       static int
2221   luaopen_vim(lua_State *L)
2222   {
2223 !     /* set environment */
2224       lua_newtable(L);
2225       lua_newtable(L);
2226 !     lua_pushliteral(L, "v");
2227       lua_setfield(L, -2, "__mode");
2228 !     lua_setmetatable(L, -2);
2229 !     lua_replace(L, LUA_ENVIRONINDEX);
2230       /* print */
2231       lua_pushcfunction(L, luaV_print);
2232       lua_setglobal(L, "print");
2233 --- 1608,1695 ----
2234       {"eval", luaV_eval},
2235       {"beep", luaV_beep},
2236       {"line", luaV_line},
2237 +     {"list", luaV_list},
2238 +     {"dict", luaV_dict},
2239       {"buffer", luaV_buffer},
2240       {"window", luaV_window},
2241       {"open", luaV_open},
2242 !     {"type", luaV_type},
2243       {NULL, NULL}
2244   };
2245   
2246 + /* for freeing list, dict, buffer and window objects; lightuserdata as arg */
2247 +     static int
2248 + luaV_free(lua_State *L)
2249 + {
2250 +     lua_pushnil(L);
2251 +     luaV_setudata(L, lua_touserdata(L, 1));
2252 +     return 0;
2253 + }
2254
2255 +     static int
2256 + luaV_luaeval (lua_State *L)
2257 + {
2258 +     luaL_Buffer b;
2259 +     size_t l;
2260 +     const char *str = lua_tolstring(L, 1, &l);
2261 +     typval_T *arg = (typval_T *) lua_touserdata(L, 2);
2262 +     typval_T *rettv = (typval_T *) lua_touserdata(L, 3);
2263 +     luaL_buffinit(L, &b);
2264 +     luaL_addlstring(&b, LUAVIM_EVALHEADER, sizeof(LUAVIM_EVALHEADER) - 1);
2265 +     luaL_addlstring(&b, str, l);
2266 +     luaL_pushresult(&b);
2267 +     str = lua_tolstring(L, -1, &l);
2268 +     if (luaL_loadbuffer(L, str, l, LUAVIM_EVALNAME)) /* compile error? */
2269 +     {
2270 +       luaV_emsg(L);
2271 +       return 0;
2272 +     }
2273 +     luaV_pushtypval(L, arg);
2274 +     if (lua_pcall(L, 1, 1, 0)) /* running error? */
2275 +     {
2276 +       luaV_emsg(L);
2277 +       return 0;
2278 +     }
2279 +     luaV_totypval(L, -1, rettv);
2280 +     return 0;
2281 + }
2282
2283 +     static int
2284 + luaV_setref (lua_State *L)
2285 + {
2286 +     int copyID = lua_tointeger(L, 1);
2287 +     typval_T tv;
2288 +     luaV_getfield(L, LUAVIM_LIST);
2289 +     luaV_getfield(L, LUAVIM_DICT);
2290 +     lua_pushnil(L);
2291 +     while (lua_next(L, lua_upvalueindex(1)) != 0) /* traverse cache table */
2292 +     {
2293 +       lua_getmetatable(L, -1);
2294 +       if (lua_rawequal(L, -1, 2)) /* list? */
2295 +       {
2296 +           tv.v_type = VAR_LIST;
2297 +           tv.vval.v_list = (list_T *) lua_touserdata(L, 4); /* key */
2298 +       }
2299 +       else if (lua_rawequal(L, -1, 3)) /* dict? */
2300 +       {
2301 +           tv.v_type = VAR_DICT;
2302 +           tv.vval.v_dict = (dict_T *) lua_touserdata(L, 4); /* key */
2303 +       }
2304 +       lua_pop(L, 2); /* metatable and value */
2305 +       set_ref_in_tv(&tv, copyID);
2306 +     }
2307 +     return 0;
2308 + }
2309
2310       static int
2311   luaopen_vim(lua_State *L)
2312   {
2313 !     /* set cache table */
2314       lua_newtable(L);
2315       lua_newtable(L);
2316 !     lua_pushstring(L, "v");
2317       lua_setfield(L, -2, "__mode");
2318 !     lua_setmetatable(L, -2); /* cache is weak-valued */
2319       /* print */
2320       lua_pushcfunction(L, luaV_print);
2321       lua_setglobal(L, "print");
2322 ***************
2323 *** 1116,1129 ****
2324       lua_pop(L, 1);
2325       /* free */
2326       lua_pushlightuserdata(L, (void *) LUAVIM_FREE);
2327 !     lua_pushcfunction(L, luaV_free);
2328       lua_rawset(L, LUA_REGISTRYINDEX);
2329       /* register */
2330       luaV_newmetatable(L, LUAVIM_BUFFER);
2331 !     luaL_register(L, NULL, luaV_Buffer_mt);
2332       luaV_newmetatable(L, LUAVIM_WINDOW);
2333 !     luaL_register(L, NULL, luaV_Window_mt);
2334 !     luaL_register(L, LUAVIM_NAME, luaV_module);
2335       return 0;
2336   }
2337   
2338 --- 1700,1735 ----
2339       lua_pop(L, 1);
2340       /* free */
2341       lua_pushlightuserdata(L, (void *) LUAVIM_FREE);
2342 !     lua_pushvalue(L, 1); /* cache table */
2343 !     lua_pushcclosure(L, luaV_free, 1);
2344 !     lua_rawset(L, LUA_REGISTRYINDEX);
2345 !     /* luaeval */
2346 !     lua_pushlightuserdata(L, (void *) LUAVIM_LUAEVAL);
2347 !     lua_pushvalue(L, 1); /* cache table */
2348 !     lua_pushcclosure(L, luaV_luaeval, 1);
2349 !     lua_rawset(L, LUA_REGISTRYINDEX);
2350 !     /* setref */
2351 !     lua_pushlightuserdata(L, (void *) LUAVIM_SETREF);
2352 !     lua_pushvalue(L, 1); /* cache table */
2353 !     lua_pushcclosure(L, luaV_setref, 1);
2354       lua_rawset(L, LUA_REGISTRYINDEX);
2355       /* register */
2356 +     luaV_newmetatable(L, LUAVIM_LIST);
2357 +     lua_pushvalue(L, 1);
2358 +     luaV_openlib(L, luaV_List_mt, 1);
2359 +     luaV_newmetatable(L, LUAVIM_DICT);
2360 +     lua_pushvalue(L, 1);
2361 +     luaV_openlib(L, luaV_Dict_mt, 1);
2362       luaV_newmetatable(L, LUAVIM_BUFFER);
2363 !     lua_pushvalue(L, 1); /* cache table */
2364 !     luaV_openlib(L, luaV_Buffer_mt, 1);
2365       luaV_newmetatable(L, LUAVIM_WINDOW);
2366 !     lua_pushvalue(L, 1); /* cache table */
2367 !     luaV_openlib(L, luaV_Window_mt, 1);
2368 !     lua_newtable(L); /* vim table */
2369 !     lua_pushvalue(L, 1); /* cache table */
2370 !     luaV_openlib(L, luaV_module, 1);
2371 !     lua_setglobal(L, LUAVIM_NAME);
2372       return 0;
2373   }
2374   
2375 ***************
2376 *** 1154,1160 ****
2377   static lua_State *L = NULL;
2378   
2379       static int
2380 ! lua_is_open(void)
2381   {
2382       return L != NULL;
2383   }
2384 --- 1760,1766 ----
2385   static lua_State *L = NULL;
2386   
2387       static int
2388 ! lua_isopen(void)
2389   {
2390       return L != NULL;
2391   }
2392 ***************
2393 *** 1162,1168 ****
2394       static int
2395   lua_init(void)
2396   {
2397 !     if (L == NULL)
2398       {
2399   #ifdef DYNAMIC_LUA
2400         if (!lua_enabled(TRUE))
2401 --- 1768,1774 ----
2402       static int
2403   lua_init(void)
2404   {
2405 !     if (!lua_isopen())
2406       {
2407   #ifdef DYNAMIC_LUA
2408         if (!lua_enabled(TRUE))
2409 ***************
2410 *** 1179,1185 ****
2411       void
2412   lua_end(void)
2413   {
2414 !     if (L != NULL)
2415       {
2416         lua_close(L);
2417         L = NULL;
2418 --- 1785,1791 ----
2419       void
2420   lua_end(void)
2421   {
2422 !     if (lua_isopen())
2423       {
2424         lua_close(L);
2425         L = NULL;
2426 ***************
2427 *** 1273,1295 ****
2428       }
2429   }
2430   
2431 ! /* buffer */
2432       void
2433 ! lua_buffer_free(buf_T *buf)
2434   {
2435 !     if (!lua_is_open()) return;
2436 !     luaV_getfield(L, LUAVIM_FREE);
2437 !     lua_pushlightuserdata(L, (void *) buf);
2438 !     lua_call(L, 1, 0);
2439   }
2440   
2441 - /* window */
2442       void
2443 ! lua_window_free(win_T *win)
2444   {
2445 !     if (!lua_is_open()) return;
2446 !     luaV_getfield(L, LUAVIM_FREE);
2447 !     lua_pushlightuserdata(L, (void *) win);
2448       lua_call(L, 1, 0);
2449   }
2450   
2451 --- 1879,1914 ----
2452       }
2453   }
2454   
2455 ! #define luaV_freetype(typ,tname) \
2456 !       void \
2457 !     lua_##tname##_free(typ *o) \
2458 !     { \
2459 !       if (!lua_isopen()) return; \
2460 !       luaV_getfield(L, LUAVIM_FREE); \
2461 !       lua_pushlightuserdata(L, (void *) o); \
2462 !       lua_call(L, 1, 0); \
2463 !     }
2464
2465 ! luaV_freetype(buf_T, buffer)
2466 ! luaV_freetype(win_T, window)
2467
2468       void
2469 ! do_luaeval (char_u *str, typval_T *arg, typval_T *rettv)
2470   {
2471 !     lua_init();
2472 !     luaV_getfield(L, LUAVIM_LUAEVAL);
2473 !     lua_pushstring(L, (char *) str);
2474 !     lua_pushlightuserdata(L, (void *) arg);
2475 !     lua_pushlightuserdata(L, (void *) rettv);
2476 !     lua_call(L, 3, 0);
2477   }
2478   
2479       void
2480 ! set_ref_in_lua (int copyID)
2481   {
2482 !     if (!lua_isopen()) return;
2483 !     luaV_getfield(L, LUAVIM_SETREF);
2484 !     lua_pushinteger(L, copyID);
2485       lua_call(L, 1, 0);
2486   }
2487   
2488 *** ../vim-7.3.489/src/proto/if_lua.pro 2010-08-15 21:57:28.000000000 +0200
2489 --- src/proto/if_lua.pro        2012-04-05 16:41:35.000000000 +0200
2490 ***************
2491 *** 6,9 ****
2492 --- 6,11 ----
2493   void ex_luafile __ARGS((exarg_T *eap));
2494   void lua_buffer_free __ARGS((buf_T *buf));
2495   void lua_window_free __ARGS((win_T *win));
2496 + void do_luaeval __ARGS((char_u *str, typval_T *arg, typval_T *rettv));
2497 + void set_ref_in_lua __ARGS((int copyID));
2498   /* vim: set ft=c : */
2499 *** ../vim-7.3.489/src/version.c        2012-04-05 16:07:01.000000000 +0200
2500 --- src/version.c       2012-04-05 16:52:08.000000000 +0200
2501 ***************
2502 *** 716,717 ****
2503 --- 716,719 ----
2504   {   /* Add new patch number below this line */
2505 + /**/
2506 +     490,
2507   /**/
2508
2509 -- 
2510 Even got a Datapoint 3600(?) with a DD50 connector instead of the
2511 usual DB25...  what a nightmare trying to figure out the pinout
2512 for *that* with no spex...
2513
2514  /// Bram Moolenaar -- Bram@Moolenaar.net -- http://www.Moolenaar.net   \\\
2515 ///        sponsor Vim, vote for features -- http://www.Vim.org/sponsor/ \\\
2516 \\\  an exciting new programming language -- http://www.Zimbu.org        ///
2517  \\\            help me help AIDS victims -- http://ICCF-Holland.org    ///
This page took 0.211395 seconds and 3 git commands to generate.