comparison src/if_lua.c @ 18798:f0f9692d4487 v8.1.2387

patch 8.1.2387: using old C style comments Commit: https://github.com/vim/vim/commit/2ab2e8608f9b2c85432715bb9a7f226fdbf8cd35 Author: Bram Moolenaar <Bram@vim.org> Date: Wed Dec 4 21:24:53 2019 +0100 patch 8.1.2387: using old C style comments Problem: Using old C style comments. Solution: Use // comments where appropriate.
author Bram Moolenaar <Bram@vim.org>
date Wed, 04 Dec 2019 21:30:04 +0100
parents 026034963159
children ba9f50bfda83
comparison
equal deleted inserted replaced
18797:76af6d0ea316 18798:f0f9692d4487
13 13
14 #include <lua.h> 14 #include <lua.h>
15 #include <lualib.h> 15 #include <lualib.h>
16 #include <lauxlib.h> 16 #include <lauxlib.h>
17 17
18 /* Only do the following when the feature is enabled. Needed for "make 18 // Only do the following when the feature is enabled. Needed for "make
19 * depend". */ 19 // depend".
20 #if defined(FEAT_LUA) || defined(PROTO) 20 #if defined(FEAT_LUA) || defined(PROTO)
21 21
22 #define LUAVIM_CHUNKNAME "vim chunk" 22 #define LUAVIM_CHUNKNAME "vim chunk"
23 #define LUAVIM_NAME "vim" 23 #define LUAVIM_NAME "vim"
24 #define LUAVIM_EVALNAME "luaeval" 24 #define LUAVIM_EVALNAME "luaeval"
43 static const char LUAVIM_WINDOW[] = "window"; 43 static const char LUAVIM_WINDOW[] = "window";
44 static const char LUAVIM_FREE[] = "luaV_free"; 44 static const char LUAVIM_FREE[] = "luaV_free";
45 static const char LUAVIM_LUAEVAL[] = "luaV_luaeval"; 45 static const char LUAVIM_LUAEVAL[] = "luaV_luaeval";
46 static const char LUAVIM_SETREF[] = "luaV_setref"; 46 static const char LUAVIM_SETREF[] = "luaV_setref";
47 47
48 /* most functions are closures with a cache table as first upvalue; 48 // most functions are closures with a cache table as first upvalue;
49 * get/setudata manage references to vim userdata in cache table through 49 // get/setudata manage references to vim userdata in cache table through
50 * object pointers (light userdata) */ 50 // object pointers (light userdata)
51 #define luaV_getudata(L, v) \ 51 #define luaV_getudata(L, v) \
52 lua_pushlightuserdata((L), (void *) (v)); \ 52 lua_pushlightuserdata((L), (void *) (v)); \
53 lua_rawget((L), lua_upvalueindex(1)) 53 lua_rawget((L), lua_upvalueindex(1))
54 #define luaV_setudata(L, v) \ 54 #define luaV_setudata(L, v) \
55 lua_pushlightuserdata((L), (void *) (v)); \ 55 lua_pushlightuserdata((L), (void *) (v)); \
92 # define load_dll vimLoadLib 92 # define load_dll vimLoadLib
93 # define symbol_from_dll GetProcAddress 93 # define symbol_from_dll GetProcAddress
94 # define close_dll FreeLibrary 94 # define close_dll FreeLibrary
95 #endif 95 #endif
96 96
97 /* lauxlib */ 97 // lauxlib
98 #if LUA_VERSION_NUM <= 501 98 #if LUA_VERSION_NUM <= 501
99 #define luaL_register dll_luaL_register 99 #define luaL_register dll_luaL_register
100 #define luaL_prepbuffer dll_luaL_prepbuffer 100 #define luaL_prepbuffer dll_luaL_prepbuffer
101 #define luaL_openlib dll_luaL_openlib 101 #define luaL_openlib dll_luaL_openlib
102 #define luaL_typerror dll_luaL_typerror 102 #define luaL_typerror dll_luaL_typerror
117 #define luaL_error dll_luaL_error 117 #define luaL_error dll_luaL_error
118 #define luaL_newstate dll_luaL_newstate 118 #define luaL_newstate dll_luaL_newstate
119 #define luaL_buffinit dll_luaL_buffinit 119 #define luaL_buffinit dll_luaL_buffinit
120 #define luaL_addlstring dll_luaL_addlstring 120 #define luaL_addlstring dll_luaL_addlstring
121 #define luaL_pushresult dll_luaL_pushresult 121 #define luaL_pushresult dll_luaL_pushresult
122 /* lua */ 122 // lua
123 #if LUA_VERSION_NUM <= 501 123 #if LUA_VERSION_NUM <= 501
124 #define lua_tonumber dll_lua_tonumber 124 #define lua_tonumber dll_lua_tonumber
125 #define lua_tointeger dll_lua_tointeger 125 #define lua_tointeger dll_lua_tointeger
126 #define lua_call dll_lua_call 126 #define lua_call dll_lua_call
127 #define lua_pcall dll_lua_pcall 127 #define lua_pcall dll_lua_pcall
175 #define lua_setfield dll_lua_setfield 175 #define lua_setfield dll_lua_setfield
176 #define lua_rawset dll_lua_rawset 176 #define lua_rawset dll_lua_rawset
177 #define lua_rawseti dll_lua_rawseti 177 #define lua_rawseti dll_lua_rawseti
178 #define lua_setmetatable dll_lua_setmetatable 178 #define lua_setmetatable dll_lua_setmetatable
179 #define lua_next dll_lua_next 179 #define lua_next dll_lua_next
180 /* libs */ 180 // libs
181 #define luaopen_base dll_luaopen_base 181 #define luaopen_base dll_luaopen_base
182 #define luaopen_table dll_luaopen_table 182 #define luaopen_table dll_luaopen_table
183 #define luaopen_string dll_luaopen_string 183 #define luaopen_string dll_luaopen_string
184 #define luaopen_math dll_luaopen_math 184 #define luaopen_math dll_luaopen_math
185 #define luaopen_io dll_luaopen_io 185 #define luaopen_io dll_luaopen_io
186 #define luaopen_os dll_luaopen_os 186 #define luaopen_os dll_luaopen_os
187 #define luaopen_package dll_luaopen_package 187 #define luaopen_package dll_luaopen_package
188 #define luaopen_debug dll_luaopen_debug 188 #define luaopen_debug dll_luaopen_debug
189 #define luaL_openlibs dll_luaL_openlibs 189 #define luaL_openlibs dll_luaL_openlibs
190 190
191 /* lauxlib */ 191 // lauxlib
192 #if LUA_VERSION_NUM <= 501 192 #if LUA_VERSION_NUM <= 501
193 void (*dll_luaL_register) (lua_State *L, const char *libname, const luaL_Reg *l); 193 void (*dll_luaL_register) (lua_State *L, const char *libname, const luaL_Reg *l);
194 char *(*dll_luaL_prepbuffer) (luaL_Buffer *B); 194 char *(*dll_luaL_prepbuffer) (luaL_Buffer *B);
195 void (*dll_luaL_openlib) (lua_State *L, const char *libname, const luaL_Reg *l, int nup); 195 void (*dll_luaL_openlib) (lua_State *L, const char *libname, const luaL_Reg *l, int nup);
196 int (*dll_luaL_typerror) (lua_State *L, int narg, const char *tname); 196 int (*dll_luaL_typerror) (lua_State *L, int narg, const char *tname);
211 int (*dll_luaL_error) (lua_State *L, const char *fmt, ...); 211 int (*dll_luaL_error) (lua_State *L, const char *fmt, ...);
212 lua_State *(*dll_luaL_newstate) (void); 212 lua_State *(*dll_luaL_newstate) (void);
213 void (*dll_luaL_buffinit) (lua_State *L, luaL_Buffer *B); 213 void (*dll_luaL_buffinit) (lua_State *L, luaL_Buffer *B);
214 void (*dll_luaL_addlstring) (luaL_Buffer *B, const char *s, size_t l); 214 void (*dll_luaL_addlstring) (luaL_Buffer *B, const char *s, size_t l);
215 void (*dll_luaL_pushresult) (luaL_Buffer *B); 215 void (*dll_luaL_pushresult) (luaL_Buffer *B);
216 /* lua */ 216 // lua
217 #if LUA_VERSION_NUM <= 501 217 #if LUA_VERSION_NUM <= 501
218 lua_Number (*dll_lua_tonumber) (lua_State *L, int idx); 218 lua_Number (*dll_lua_tonumber) (lua_State *L, int idx);
219 lua_Integer (*dll_lua_tointeger) (lua_State *L, int idx); 219 lua_Integer (*dll_lua_tointeger) (lua_State *L, int idx);
220 void (*dll_lua_call) (lua_State *L, int nargs, int nresults); 220 void (*dll_lua_call) (lua_State *L, int nargs, int nresults);
221 int (*dll_lua_pcall) (lua_State *L, int nargs, int nresults, int errfunc); 221 int (*dll_lua_pcall) (lua_State *L, int nargs, int nresults, int errfunc);
280 #else 280 #else
281 void (*dll_lua_rawseti) (lua_State *L, int idx, lua_Integer n); 281 void (*dll_lua_rawseti) (lua_State *L, int idx, lua_Integer n);
282 #endif 282 #endif
283 int (*dll_lua_setmetatable) (lua_State *L, int objindex); 283 int (*dll_lua_setmetatable) (lua_State *L, int objindex);
284 int (*dll_lua_next) (lua_State *L, int idx); 284 int (*dll_lua_next) (lua_State *L, int idx);
285 /* libs */ 285 // libs
286 int (*dll_luaopen_base) (lua_State *L); 286 int (*dll_luaopen_base) (lua_State *L);
287 int (*dll_luaopen_table) (lua_State *L); 287 int (*dll_luaopen_table) (lua_State *L);
288 int (*dll_luaopen_string) (lua_State *L); 288 int (*dll_luaopen_string) (lua_State *L);
289 int (*dll_luaopen_math) (lua_State *L); 289 int (*dll_luaopen_math) (lua_State *L);
290 int (*dll_luaopen_io) (lua_State *L); 290 int (*dll_luaopen_io) (lua_State *L);
298 const char *name; 298 const char *name;
299 luaV_function func; 299 luaV_function func;
300 } luaV_Reg; 300 } luaV_Reg;
301 301
302 static const luaV_Reg luaV_dll[] = { 302 static const luaV_Reg luaV_dll[] = {
303 /* lauxlib */ 303 // lauxlib
304 #if LUA_VERSION_NUM <= 501 304 #if LUA_VERSION_NUM <= 501
305 {"luaL_register", (luaV_function) &dll_luaL_register}, 305 {"luaL_register", (luaV_function) &dll_luaL_register},
306 {"luaL_prepbuffer", (luaV_function) &dll_luaL_prepbuffer}, 306 {"luaL_prepbuffer", (luaV_function) &dll_luaL_prepbuffer},
307 {"luaL_openlib", (luaV_function) &dll_luaL_openlib}, 307 {"luaL_openlib", (luaV_function) &dll_luaL_openlib},
308 {"luaL_typerror", (luaV_function) &dll_luaL_typerror}, 308 {"luaL_typerror", (luaV_function) &dll_luaL_typerror},
323 {"luaL_error", (luaV_function) &dll_luaL_error}, 323 {"luaL_error", (luaV_function) &dll_luaL_error},
324 {"luaL_newstate", (luaV_function) &dll_luaL_newstate}, 324 {"luaL_newstate", (luaV_function) &dll_luaL_newstate},
325 {"luaL_buffinit", (luaV_function) &dll_luaL_buffinit}, 325 {"luaL_buffinit", (luaV_function) &dll_luaL_buffinit},
326 {"luaL_addlstring", (luaV_function) &dll_luaL_addlstring}, 326 {"luaL_addlstring", (luaV_function) &dll_luaL_addlstring},
327 {"luaL_pushresult", (luaV_function) &dll_luaL_pushresult}, 327 {"luaL_pushresult", (luaV_function) &dll_luaL_pushresult},
328 /* lua */ 328 // lua
329 #if LUA_VERSION_NUM <= 501 329 #if LUA_VERSION_NUM <= 501
330 {"lua_tonumber", (luaV_function) &dll_lua_tonumber}, 330 {"lua_tonumber", (luaV_function) &dll_lua_tonumber},
331 {"lua_tointeger", (luaV_function) &dll_lua_tointeger}, 331 {"lua_tointeger", (luaV_function) &dll_lua_tointeger},
332 {"lua_call", (luaV_function) &dll_lua_call}, 332 {"lua_call", (luaV_function) &dll_lua_call},
333 {"lua_pcall", (luaV_function) &dll_lua_pcall}, 333 {"lua_pcall", (luaV_function) &dll_lua_pcall},
381 {"lua_setfield", (luaV_function) &dll_lua_setfield}, 381 {"lua_setfield", (luaV_function) &dll_lua_setfield},
382 {"lua_rawset", (luaV_function) &dll_lua_rawset}, 382 {"lua_rawset", (luaV_function) &dll_lua_rawset},
383 {"lua_rawseti", (luaV_function) &dll_lua_rawseti}, 383 {"lua_rawseti", (luaV_function) &dll_lua_rawseti},
384 {"lua_setmetatable", (luaV_function) &dll_lua_setmetatable}, 384 {"lua_setmetatable", (luaV_function) &dll_lua_setmetatable},
385 {"lua_next", (luaV_function) &dll_lua_next}, 385 {"lua_next", (luaV_function) &dll_lua_next},
386 /* libs */ 386 // libs
387 {"luaopen_base", (luaV_function) &dll_luaopen_base}, 387 {"luaopen_base", (luaV_function) &dll_luaopen_base},
388 {"luaopen_table", (luaV_function) &dll_luaopen_table}, 388 {"luaopen_table", (luaV_function) &dll_luaopen_table},
389 {"luaopen_string", (luaV_function) &dll_luaopen_string}, 389 {"luaopen_string", (luaV_function) &dll_luaopen_string},
390 {"luaopen_math", (luaV_function) &dll_luaopen_math}, 390 {"luaopen_math", (luaV_function) &dll_luaopen_math},
391 {"luaopen_io", (luaV_function) &dll_luaopen_io}, 391 {"luaopen_io", (luaV_function) &dll_luaopen_io},
431 return FAIL; 431 return FAIL;
432 } 432 }
433 } 433 }
434 return OK; 434 return OK;
435 } 435 }
436 #endif /* DYNAMIC_LUA */ 436 #endif // DYNAMIC_LUA
437 437
438 #if defined(DYNAMIC_LUA) || defined(PROTO) 438 #if defined(DYNAMIC_LUA) || defined(PROTO)
439 int 439 int
440 lua_enabled(int verbose) 440 lua_enabled(int verbose)
441 { 441 {
452 return luaL_argerror(L, narg, msg); 452 return luaL_argerror(L, narg, msg);
453 } 453 }
454 #endif 454 #endif
455 455
456 456
457 /* ======= Internal ======= */ 457 // ======= Internal =======
458 458
459 static void 459 static void
460 luaV_newmetatable(lua_State *L, const char *tname) 460 luaV_newmetatable(lua_State *L, const char *tname)
461 { 461 {
462 lua_newtable(L); 462 lua_newtable(L);
468 static void * 468 static void *
469 luaV_toudata(lua_State *L, int ud, const char *tname) 469 luaV_toudata(lua_State *L, int ud, const char *tname)
470 { 470 {
471 void *p = lua_touserdata(L, ud); 471 void *p = lua_touserdata(L, ud);
472 472
473 if (p != NULL) /* value is userdata? */ 473 if (p != NULL) // value is userdata?
474 { 474 {
475 if (lua_getmetatable(L, ud)) /* does it have a metatable? */ 475 if (lua_getmetatable(L, ud)) // does it have a metatable?
476 { 476 {
477 luaV_getfield(L, tname); /* get metatable */ 477 luaV_getfield(L, tname); // get metatable
478 if (lua_rawequal(L, -1, -2)) /* MTs match? */ 478 if (lua_rawequal(L, -1, -2)) // MTs match?
479 { 479 {
480 lua_pop(L, 2); /* MTs */ 480 lua_pop(L, 2); // MTs
481 return p; 481 return p;
482 } 482 }
483 } 483 }
484 } 484 }
485 return NULL; 485 return NULL;
641 status = FAIL; 641 status = FAIL;
642 } 642 }
643 return status; 643 return status;
644 } 644 }
645 645
646 /* similar to luaL_addlstring, but replaces \0 with \n if toline and 646 /*
647 * \n with \0 otherwise */ 647 * similar to luaL_addlstring, but replaces \0 with \n if toline and
648 * \n with \0 otherwise
649 */
648 static void 650 static void
649 luaV_addlstring(luaL_Buffer *b, const char *s, size_t l, int toline) 651 luaV_addlstring(luaL_Buffer *b, const char *s, size_t l, int toline)
650 { 652 {
651 while (l--) 653 while (l--)
652 { 654 {
681 luaV_addlstring(&b, s, l, 1); 683 luaV_addlstring(&b, s, l, 1);
682 luaL_pushresult(&b); 684 luaL_pushresult(&b);
683 return (char_u *) lua_tostring(L, -1); 685 return (char_u *) lua_tostring(L, -1);
684 } 686 }
685 687
686 /* pops a string s from the top of the stack and calls mf(t) for pieces t of 688 /*
687 * s separated by newlines */ 689 * pops a string s from the top of the stack and calls mf(t) for pieces t of
690 * s separated by newlines
691 */
688 static void 692 static void
689 luaV_msgfunc(lua_State *L, msgfunc_T mf) 693 luaV_msgfunc(lua_State *L, msgfunc_T mf)
690 { 694 {
691 luaL_Buffer b; 695 luaL_Buffer b;
692 size_t l; 696 size_t l;
693 const char *p, *s = lua_tolstring(L, -1, &l); 697 const char *p, *s = lua_tolstring(L, -1, &l);
694 luaL_buffinit(L, &b); 698 luaL_buffinit(L, &b);
695 luaV_addlstring(&b, s, l, 0); 699 luaV_addlstring(&b, s, l, 0);
696 luaL_pushresult(&b); 700 luaL_pushresult(&b);
697 /* break string */ 701 // break string
698 p = s = lua_tolstring(L, -1, &l); 702 p = s = lua_tolstring(L, -1, &l);
699 while (l--) 703 while (l--)
700 { 704 {
701 if (*p++ == '\0') /* break? */ 705 if (*p++ == '\0') // break?
702 { 706 {
703 mf((char_u *) s); 707 mf((char_u *) s);
704 s = p; 708 s = p;
705 } 709 }
706 } 710 }
707 mf((char_u *) s); 711 mf((char_u *) s);
708 lua_pop(L, 2); /* original and modified strings */ 712 lua_pop(L, 2); // original and modified strings
709 } 713 }
710 714
711 #define luaV_newtype(typ,tname,luatyp,luatname) \ 715 #define luaV_newtype(typ,tname,luatyp,luatname) \
712 static luatyp * \ 716 static luatyp * \
713 luaV_new##tname(lua_State *L, typ *obj) \ 717 luaV_new##tname(lua_State *L, typ *obj) \
746 { \ 750 { \
747 lua_pushfstring(L, "%s: %p", luatname, lua_touserdata(L, 1)); \ 751 lua_pushfstring(L, "%s: %p", luatname, lua_touserdata(L, 1)); \
748 return 1; \ 752 return 1; \
749 } 753 }
750 754
751 /* ======= List type ======= */ 755 // ======= List type =======
752 756
753 static luaV_List * 757 static luaV_List *
754 luaV_newlist(lua_State *L, list_T *lis) 758 luaV_newlist(lua_State *L, list_T *lis)
755 { 759 {
756 luaV_List *l = (luaV_List *) lua_newuserdata(L, sizeof(luaV_List)); 760 luaV_List *l = (luaV_List *) lua_newuserdata(L, sizeof(luaV_List));
757 *l = lis; 761 *l = lis;
758 lis->lv_refcount++; /* reference in Lua */ 762 lis->lv_refcount++; // reference in Lua
759 luaV_setudata(L, lis); /* cache[lis] = udata */ 763 luaV_setudata(L, lis); // cache[lis] = udata
760 luaV_getfield(L, LUAVIM_LIST); 764 luaV_getfield(L, LUAVIM_LIST);
761 lua_setmetatable(L, -2); 765 lua_setmetatable(L, -2);
762 return l; 766 return l;
763 } 767 }
764 768
786 790
787 static int 791 static int
788 luaV_list_call(lua_State *L) 792 luaV_list_call(lua_State *L)
789 { 793 {
790 list_T *l = luaV_unbox(L, luaV_List, 1); 794 list_T *l = luaV_unbox(L, luaV_List, 1);
791 lua_pushvalue(L, lua_upvalueindex(1)); /* pass cache table along */ 795 lua_pushvalue(L, lua_upvalueindex(1)); // pass cache table along
792 lua_pushlightuserdata(L, (void *) l->lv_first); 796 lua_pushlightuserdata(L, (void *) l->lv_first);
793 lua_pushcclosure(L, luaV_list_iter, 2); 797 lua_pushcclosure(L, luaV_list_iter, 2);
794 return 1; 798 return 1;
795 } 799 }
796 800
797 static int 801 static int
798 luaV_list_index(lua_State *L) 802 luaV_list_index(lua_State *L)
799 { 803 {
800 list_T *l = luaV_unbox(L, luaV_List, 1); 804 list_T *l = luaV_unbox(L, luaV_List, 1);
801 if (lua_isnumber(L, 2)) /* list item? */ 805 if (lua_isnumber(L, 2)) // list item?
802 { 806 {
803 listitem_T *li = list_find(l, (long) luaL_checkinteger(L, 2)); 807 listitem_T *li = list_find(l, (long) luaL_checkinteger(L, 2));
804 if (li == NULL) 808 if (li == NULL)
805 lua_pushnil(L); 809 lua_pushnil(L);
806 else 810 else
807 luaV_pushtypval(L, &li->li_tv); 811 luaV_pushtypval(L, &li->li_tv);
808 } 812 }
809 else if (lua_isstring(L, 2)) /* method? */ 813 else if (lua_isstring(L, 2)) // method?
810 { 814 {
811 const char *s = lua_tostring(L, 2); 815 const char *s = lua_tostring(L, 2);
812 if (strncmp(s, "add", 3) == 0 816 if (strncmp(s, "add", 3) == 0
813 || strncmp(s, "insert", 6) == 0) 817 || strncmp(s, "insert", 6) == 0)
814 { 818 {
831 listitem_T *li; 835 listitem_T *li;
832 if (l->lv_lock) 836 if (l->lv_lock)
833 luaL_error(L, "list is locked"); 837 luaL_error(L, "list is locked");
834 li = list_find(l, n); 838 li = list_find(l, n);
835 if (li == NULL) return 0; 839 if (li == NULL) return 0;
836 if (lua_isnil(L, 3)) /* remove? */ 840 if (lua_isnil(L, 3)) // remove?
837 { 841 {
838 vimlist_remove(l, li, li); 842 vimlist_remove(l, li, li);
839 clear_tv(&li->li_tv); 843 clear_tv(&li->li_tv);
840 vim_free(li); 844 vim_free(li);
841 } 845 }
902 {"insert", luaV_list_insert}, 906 {"insert", luaV_list_insert},
903 {NULL, NULL} 907 {NULL, NULL}
904 }; 908 };
905 909
906 910
907 /* ======= Dict type ======= */ 911 // ======= Dict type =======
908 912
909 static luaV_Dict * 913 static luaV_Dict *
910 luaV_newdict(lua_State *L, dict_T *dic) 914 luaV_newdict(lua_State *L, dict_T *dic)
911 { 915 {
912 luaV_Dict *d = (luaV_Dict *) lua_newuserdata(L, sizeof(luaV_Dict)); 916 luaV_Dict *d = (luaV_Dict *) lua_newuserdata(L, sizeof(luaV_Dict));
913 *d = dic; 917 *d = dic;
914 dic->dv_refcount++; /* reference in Lua */ 918 dic->dv_refcount++; // reference in Lua
915 luaV_setudata(L, dic); /* cache[dic] = udata */ 919 luaV_setudata(L, dic); // cache[dic] = udata
916 luaV_getfield(L, LUAVIM_DICT); 920 luaV_getfield(L, LUAVIM_DICT);
917 lua_setmetatable(L, -2); 921 lua_setmetatable(L, -2);
918 return d; 922 return d;
919 } 923 }
920 924
954 static int 958 static int
955 luaV_dict_call(lua_State *L) 959 luaV_dict_call(lua_State *L)
956 { 960 {
957 dict_T *d = luaV_unbox(L, luaV_Dict, 1); 961 dict_T *d = luaV_unbox(L, luaV_Dict, 1);
958 hashtab_T *ht = &d->dv_hashtab; 962 hashtab_T *ht = &d->dv_hashtab;
959 lua_pushvalue(L, lua_upvalueindex(1)); /* pass cache table along */ 963 lua_pushvalue(L, lua_upvalueindex(1)); // pass cache table along
960 lua_pushlightuserdata(L, (void *) ht->ht_array); 964 lua_pushlightuserdata(L, (void *) ht->ht_array);
961 lua_pushinteger(L, ht->ht_used); /* # remaining items */ 965 lua_pushinteger(L, ht->ht_used); // # remaining items
962 lua_pushcclosure(L, luaV_dict_iter, 3); 966 lua_pushcclosure(L, luaV_dict_iter, 3);
963 return 1; 967 return 1;
964 } 968 }
965 969
966 static int 970 static int
973 if (di == NULL) 977 if (di == NULL)
974 lua_pushnil(L); 978 lua_pushnil(L);
975 else 979 else
976 { 980 {
977 luaV_pushtypval(L, &di->di_tv); 981 luaV_pushtypval(L, &di->di_tv);
978 if (di->di_tv.v_type == VAR_FUNC) /* funcref? */ 982 if (di->di_tv.v_type == VAR_FUNC) // funcref?
979 { 983 {
980 luaV_Funcref *f = (luaV_Funcref *) lua_touserdata(L, -1); 984 luaV_Funcref *f = (luaV_Funcref *) lua_touserdata(L, -1);
981 f->self = d; /* keep "self" reference */ 985 f->self = d; // keep "self" reference
982 d->dv_refcount++; 986 d->dv_refcount++;
983 } 987 }
984 } 988 }
985 return 1; 989 return 1;
986 } 990 }
997 luaL_error(L, "dict is locked"); 1001 luaL_error(L, "dict is locked");
998 if (key == NULL) 1002 if (key == NULL)
999 return 0; 1003 return 0;
1000 if (*key == NUL) 1004 if (*key == NUL)
1001 luaL_error(L, "empty key"); 1005 luaL_error(L, "empty key");
1002 if (!lua_isnil(L, 3)) /* read value? */ 1006 if (!lua_isnil(L, 3)) // read value?
1003 { 1007 {
1004 luaV_checktypval(L, 3, &v, "setting dict item"); 1008 luaV_checktypval(L, 3, &v, "setting dict item");
1005 if (d->dv_scope == VAR_DEF_SCOPE && v.v_type == VAR_FUNC) 1009 if (d->dv_scope == VAR_DEF_SCOPE && v.v_type == VAR_FUNC)
1006 luaL_error(L, "cannot assign funcref to builtin scope"); 1010 luaL_error(L, "cannot assign funcref to builtin scope");
1007 } 1011 }
1008 di = dict_find(d, key, -1); 1012 di = dict_find(d, key, -1);
1009 if (di == NULL) /* non-existing key? */ 1013 if (di == NULL) // non-existing key?
1010 { 1014 {
1011 if (lua_isnil(L, 3)) 1015 if (lua_isnil(L, 3))
1012 return 0; 1016 return 0;
1013 di = dictitem_alloc(key); 1017 di = dictitem_alloc(key);
1014 if (di == NULL) 1018 if (di == NULL)
1019 return 0; 1023 return 0;
1020 } 1024 }
1021 } 1025 }
1022 else 1026 else
1023 clear_tv(&di->di_tv); 1027 clear_tv(&di->di_tv);
1024 if (lua_isnil(L, 3)) /* remove? */ 1028 if (lua_isnil(L, 3)) // remove?
1025 { 1029 {
1026 hashitem_T *hi = hash_find(&d->dv_hashtab, di->di_key); 1030 hashitem_T *hi = hash_find(&d->dv_hashtab, di->di_key);
1027 hash_remove(&d->dv_hashtab, hi); 1031 hash_remove(&d->dv_hashtab, hi);
1028 dictitem_free(di); 1032 dictitem_free(di);
1029 } 1033 }
1043 {"__newindex", luaV_dict_newindex}, 1047 {"__newindex", luaV_dict_newindex},
1044 {NULL, NULL} 1048 {NULL, NULL}
1045 }; 1049 };
1046 1050
1047 1051
1048 /* ======= Blob type ======= */ 1052 // ======= Blob type =======
1049 1053
1050 static luaV_Blob * 1054 static luaV_Blob *
1051 luaV_newblob(lua_State *L, blob_T *blo) 1055 luaV_newblob(lua_State *L, blob_T *blo)
1052 { 1056 {
1053 luaV_Blob *b = (luaV_Blob *) lua_newuserdata(L, sizeof(luaV_Blob)); 1057 luaV_Blob *b = (luaV_Blob *) lua_newuserdata(L, sizeof(luaV_Blob));
1054 *b = blo; 1058 *b = blo;
1055 blo->bv_refcount++; /* reference in Lua */ 1059 blo->bv_refcount++; // reference in Lua
1056 luaV_setudata(L, blo); /* cache[blo] = udata */ 1060 luaV_setudata(L, blo); // cache[blo] = udata
1057 luaV_getfield(L, LUAVIM_BLOB); 1061 luaV_getfield(L, LUAVIM_BLOB);
1058 lua_setmetatable(L, -2); 1062 lua_setmetatable(L, -2);
1059 return b; 1063 return b;
1060 } 1064 }
1061 1065
1161 {"add", luaV_blob_add}, 1165 {"add", luaV_blob_add},
1162 {NULL, NULL} 1166 {NULL, NULL}
1163 }; 1167 };
1164 1168
1165 1169
1166 /* ======= Funcref type ======= */ 1170 // ======= Funcref type =======
1167 1171
1168 static luaV_Funcref * 1172 static luaV_Funcref *
1169 luaV_newfuncref(lua_State *L, char_u *name) 1173 luaV_newfuncref(lua_State *L, char_u *name)
1170 { 1174 {
1171 luaV_Funcref *f = (luaV_Funcref *)lua_newuserdata(L, sizeof(luaV_Funcref)); 1175 luaV_Funcref *f = (luaV_Funcref *)lua_newuserdata(L, sizeof(luaV_Funcref));
1200 // NOTE: Don't call "dict_unref(f->self)", because the dict of "f->self" 1204 // NOTE: Don't call "dict_unref(f->self)", because the dict of "f->self"
1201 // will be (or has been already) freed by Vim's garbage collection. 1205 // will be (or has been already) freed by Vim's garbage collection.
1202 return 0; 1206 return 0;
1203 } 1207 }
1204 1208
1205 /* equivalent to string(funcref) */ 1209 // equivalent to string(funcref)
1206 static int 1210 static int
1207 luaV_funcref_len(lua_State *L) 1211 luaV_funcref_len(lua_State *L)
1208 { 1212 {
1209 luaV_Funcref *f = (luaV_Funcref *) lua_touserdata(L, 1); 1213 luaV_Funcref *f = (luaV_Funcref *) lua_touserdata(L, 1);
1210 1214
1252 {"__call", luaV_funcref_call}, 1256 {"__call", luaV_funcref_call},
1253 {NULL, NULL} 1257 {NULL, NULL}
1254 }; 1258 };
1255 1259
1256 1260
1257 /* ======= Buffer type ======= */ 1261 // ======= Buffer type =======
1258 1262
1259 luaV_newtype(buf_T, buffer, luaV_Buffer, LUAVIM_BUFFER) 1263 luaV_newtype(buf_T, buffer, luaV_Buffer, LUAVIM_BUFFER)
1260 luaV_pushtype(buf_T, buffer, luaV_Buffer) 1264 luaV_pushtype(buf_T, buffer, luaV_Buffer)
1261 luaV_type_tostring(buffer, LUAVIM_BUFFER) 1265 luaV_type_tostring(buffer, LUAVIM_BUFFER)
1262 1266
1293 else if (strncmp(s, "fname", 5) == 0) 1297 else if (strncmp(s, "fname", 5) == 0)
1294 lua_pushstring(L, (b->b_ffname == NULL) 1298 lua_pushstring(L, (b->b_ffname == NULL)
1295 ? "" : (char *) b->b_ffname); 1299 ? "" : (char *) b->b_ffname);
1296 else if (strncmp(s, "number", 6) == 0) 1300 else if (strncmp(s, "number", 6) == 0)
1297 lua_pushinteger(L, b->b_fnum); 1301 lua_pushinteger(L, b->b_fnum);
1298 /* methods */ 1302 // methods
1299 else if (strncmp(s, "insert", 6) == 0 1303 else if (strncmp(s, "insert", 6) == 0
1300 || strncmp(s, "next", 4) == 0 1304 || strncmp(s, "next", 4) == 0
1301 || strncmp(s, "previous", 8) == 0 1305 || strncmp(s, "previous", 8) == 0
1302 || strncmp(s, "isvalid", 7) == 0) 1306 || strncmp(s, "isvalid", 7) == 0)
1303 { 1307 {
1320 #ifdef HAVE_SANDBOX 1324 #ifdef HAVE_SANDBOX
1321 luaV_checksandbox(L); 1325 luaV_checksandbox(L);
1322 #endif 1326 #endif
1323 if (n < 1 || n > b->b_ml.ml_line_count) 1327 if (n < 1 || n > b->b_ml.ml_line_count)
1324 luaL_error(L, "invalid line number"); 1328 luaL_error(L, "invalid line number");
1325 if (lua_isnil(L, 3)) /* delete line */ 1329 if (lua_isnil(L, 3)) // delete line
1326 { 1330 {
1327 buf_T *buf = curbuf; 1331 buf_T *buf = curbuf;
1328 curbuf = b; 1332 curbuf = b;
1329 if (u_savedel(n, 1L) == FAIL) 1333 if (u_savedel(n, 1L) == FAIL)
1330 { 1334 {
1337 luaL_error(L, "cannot delete line"); 1341 luaL_error(L, "cannot delete line");
1338 } 1342 }
1339 else 1343 else
1340 { 1344 {
1341 deleted_lines_mark(n, 1L); 1345 deleted_lines_mark(n, 1L);
1342 if (b == curwin->w_buffer) /* fix cursor in current window? */ 1346 if (b == curwin->w_buffer) // fix cursor in current window?
1343 { 1347 {
1344 if (curwin->w_cursor.lnum >= n) 1348 if (curwin->w_cursor.lnum >= n)
1345 { 1349 {
1346 if (curwin->w_cursor.lnum > n) 1350 if (curwin->w_cursor.lnum > n)
1347 { 1351 {
1354 invalidate_botline(); 1358 invalidate_botline();
1355 } 1359 }
1356 } 1360 }
1357 curbuf = buf; 1361 curbuf = buf;
1358 } 1362 }
1359 else if (lua_isstring(L, 3)) /* update line */ 1363 else if (lua_isstring(L, 3)) // update line
1360 { 1364 {
1361 buf_T *buf = curbuf; 1365 buf_T *buf = curbuf;
1362 curbuf = b; 1366 curbuf = b;
1363 if (u_savesub(n) == FAIL) 1367 if (u_savesub(n) == FAIL)
1364 { 1368 {
1390 buf_T *buf; 1394 buf_T *buf;
1391 luaL_checktype(L, 2, LUA_TSTRING); 1395 luaL_checktype(L, 2, LUA_TSTRING);
1392 #ifdef HAVE_SANDBOX 1396 #ifdef HAVE_SANDBOX
1393 luaV_checksandbox(L); 1397 luaV_checksandbox(L);
1394 #endif 1398 #endif
1395 /* fix insertion line */ 1399 // fix insertion line
1396 if (n < 0) n = 0; 1400 if (n < 0) n = 0;
1397 if (n > last) n = last; 1401 if (n > last) n = last;
1398 /* insert */ 1402 // insert
1399 buf = curbuf; 1403 buf = curbuf;
1400 curbuf = b; 1404 curbuf = b;
1401 if (u_save(n, n + 1) == FAIL) 1405 if (u_save(n, n + 1) == FAIL)
1402 { 1406 {
1403 curbuf = buf; 1407 curbuf = buf;
1454 {"isvalid", luaV_buffer_isvalid}, 1458 {"isvalid", luaV_buffer_isvalid},
1455 {NULL, NULL} 1459 {NULL, NULL}
1456 }; 1460 };
1457 1461
1458 1462
1459 /* ======= Window type ======= */ 1463 // ======= Window type =======
1460 1464
1461 luaV_newtype(win_T, window, luaV_Window, LUAVIM_WINDOW) 1465 luaV_newtype(win_T, window, luaV_Window, LUAVIM_WINDOW)
1462 luaV_pushtype(win_T, window, luaV_Window) 1466 luaV_pushtype(win_T, window, luaV_Window)
1463 luaV_type_tostring(window, LUAVIM_WINDOW) 1467 luaV_type_tostring(window, LUAVIM_WINDOW)
1464 1468
1484 lua_pushinteger(L, w->w_cursor.col + 1); 1488 lua_pushinteger(L, w->w_cursor.col + 1);
1485 else if (strncmp(s, "width", 5) == 0) 1489 else if (strncmp(s, "width", 5) == 0)
1486 lua_pushinteger(L, w->w_width); 1490 lua_pushinteger(L, w->w_width);
1487 else if (strncmp(s, "height", 6) == 0) 1491 else if (strncmp(s, "height", 6) == 0)
1488 lua_pushinteger(L, w->w_height); 1492 lua_pushinteger(L, w->w_height);
1489 /* methods */ 1493 // methods
1490 else if (strncmp(s, "next", 4) == 0 1494 else if (strncmp(s, "next", 4) == 0
1491 || strncmp(s, "previous", 8) == 0 1495 || strncmp(s, "previous", 8) == 0
1492 || strncmp(s, "isvalid", 7) == 0) 1496 || strncmp(s, "isvalid", 7) == 0)
1493 { 1497 {
1494 lua_getmetatable(L, 1); 1498 lua_getmetatable(L, 1);
1586 {"isvalid", luaV_window_isvalid}, 1590 {"isvalid", luaV_window_isvalid},
1587 {NULL, NULL} 1591 {NULL, NULL}
1588 }; 1592 };
1589 1593
1590 1594
1591 /* ======= Vim module ======= */ 1595 // ======= Vim module =======
1592 1596
1593 static int 1597 static int
1594 luaV_print(lua_State *L) 1598 luaV_print(lua_State *L)
1595 { 1599 {
1596 int i, n = lua_gettop(L); /* nargs */ 1600 int i, n = lua_gettop(L); // nargs
1597 const char *s; 1601 const char *s;
1598 size_t l; 1602 size_t l;
1599 luaL_Buffer b; 1603 luaL_Buffer b;
1600 luaL_buffinit(L, &b); 1604 luaL_buffinit(L, &b);
1601 lua_getglobal(L, "tostring"); 1605 lua_getglobal(L, "tostring");
1602 for (i = 1; i <= n; i++) 1606 for (i = 1; i <= n; i++)
1603 { 1607 {
1604 lua_pushvalue(L, -1); /* tostring */ 1608 lua_pushvalue(L, -1); // tostring
1605 lua_pushvalue(L, i); /* arg */ 1609 lua_pushvalue(L, i); // arg
1606 lua_call(L, 1, 1); 1610 lua_call(L, 1, 1);
1607 s = lua_tolstring(L, -1, &l); 1611 s = lua_tolstring(L, -1, &l);
1608 if (s == NULL) 1612 if (s == NULL)
1609 return luaL_error(L, "cannot convert to string"); 1613 return luaL_error(L, "cannot convert to string");
1610 if (i > 1) luaL_addchar(&b, ' '); /* use space instead of tab */ 1614 if (i > 1) luaL_addchar(&b, ' '); // use space instead of tab
1611 luaV_addlstring(&b, s, l, 0); 1615 luaV_addlstring(&b, s, l, 0);
1612 lua_pop(L, 1); 1616 lua_pop(L, 1);
1613 } 1617 }
1614 luaL_pushresult(&b); 1618 luaL_pushresult(&b);
1615 if (!got_int) 1619 if (!got_int)
1621 luaV_debug(lua_State *L) 1625 luaV_debug(lua_State *L)
1622 { 1626 {
1623 lua_settop(L, 0); 1627 lua_settop(L, 0);
1624 lua_getglobal(L, "vim"); 1628 lua_getglobal(L, "vim");
1625 lua_getfield(L, -1, "eval"); 1629 lua_getfield(L, -1, "eval");
1626 lua_remove(L, -2); /* vim.eval at position 1 */ 1630 lua_remove(L, -2); // vim.eval at position 1
1627 for (;;) 1631 for (;;)
1628 { 1632 {
1629 const char *input; 1633 const char *input;
1630 size_t l; 1634 size_t l;
1631 lua_pushvalue(L, 1); /* vim.eval */ 1635 lua_pushvalue(L, 1); // vim.eval
1632 lua_pushliteral(L, "input('lua_debug> ')"); 1636 lua_pushliteral(L, "input('lua_debug> ')");
1633 lua_call(L, 1, 1); /* return string */ 1637 lua_call(L, 1, 1); // return string
1634 input = lua_tolstring(L, -1, &l); 1638 input = lua_tolstring(L, -1, &l);
1635 if (l == 0 || strcmp(input, "cont") == 0) 1639 if (l == 0 || strcmp(input, "cont") == 0)
1636 return 0; 1640 return 0;
1637 msg_putchar('\n'); /* avoid outputting on input line */ 1641 msg_putchar('\n'); // avoid outputting on input line
1638 if (luaL_loadbuffer(L, input, l, "=(debug command)") 1642 if (luaL_loadbuffer(L, input, l, "=(debug command)")
1639 || lua_pcall(L, 0, 0, 0)) 1643 || lua_pcall(L, 0, 0, 0))
1640 luaV_emsg(L); 1644 luaV_emsg(L);
1641 lua_settop(L, 1); /* remove eventual returns, but keep vim.eval */ 1645 lua_settop(L, 1); // remove eventual returns, but keep vim.eval
1642 } 1646 }
1643 } 1647 }
1644 1648
1645 static int 1649 static int
1646 luaV_command(lua_State *L) 1650 luaV_command(lua_State *L)
1686 if (l == NULL) 1690 if (l == NULL)
1687 lua_pushnil(L); 1691 lua_pushnil(L);
1688 else 1692 else
1689 { 1693 {
1690 luaV_newlist(L, l); 1694 luaV_newlist(L, l);
1691 if (initarg) /* traverse table to init list */ 1695 if (initarg) // traverse table to init list
1692 { 1696 {
1693 int notnil, i = 0; 1697 int notnil, i = 0;
1694 typval_T v; 1698 typval_T v;
1695 do 1699 do
1696 { 1700 {
1700 { 1704 {
1701 luaV_checktypval(L, -1, &v, "vim.list"); 1705 luaV_checktypval(L, -1, &v, "vim.list");
1702 list_append_tv(l, &v); 1706 list_append_tv(l, &v);
1703 clear_tv(&v); 1707 clear_tv(&v);
1704 } 1708 }
1705 lua_pop(L, 1); /* value */ 1709 lua_pop(L, 1); // value
1706 } while (notnil); 1710 } while (notnil);
1707 } 1711 }
1708 } 1712 }
1709 return 1; 1713 return 1;
1710 } 1714 }
1721 if (d == NULL) 1725 if (d == NULL)
1722 lua_pushnil(L); 1726 lua_pushnil(L);
1723 else 1727 else
1724 { 1728 {
1725 luaV_newdict(L, d); 1729 luaV_newdict(L, d);
1726 if (initarg) /* traverse table to init dict */ 1730 if (initarg) // traverse table to init dict
1727 { 1731 {
1728 lua_pushnil(L); 1732 lua_pushnil(L);
1729 while (lua_next(L, 1)) 1733 while (lua_next(L, 1))
1730 { 1734 {
1731 char_u *key; 1735 char_u *key;
1732 dictitem_T *di; 1736 dictitem_T *di;
1733 typval_T v; 1737 typval_T v;
1734 1738
1735 lua_pushvalue(L, -2); /* dup key in case it's a number */ 1739 lua_pushvalue(L, -2); // dup key in case it's a number
1736 key = (char_u *) lua_tostring(L, -1); 1740 key = (char_u *) lua_tostring(L, -1);
1737 if (key == NULL) 1741 if (key == NULL)
1738 { 1742 {
1739 lua_pushnil(L); 1743 lua_pushnil(L);
1740 return 1; 1744 return 1;
1741 } 1745 }
1742 if (*key == NUL) 1746 if (*key == NUL)
1743 luaL_error(L, "table has empty key"); 1747 luaL_error(L, "table has empty key");
1744 luaV_checktypval(L, -2, &v, "vim.dict"); /* value */ 1748 luaV_checktypval(L, -2, &v, "vim.dict"); // value
1745 di = dictitem_alloc(key); 1749 di = dictitem_alloc(key);
1746 if (di == NULL || dict_add(d, di) == FAIL) 1750 if (di == NULL || dict_add(d, di) == FAIL)
1747 { 1751 {
1748 vim_free(di); 1752 vim_free(di);
1749 lua_pushnil(L); 1753 lua_pushnil(L);
1750 return 1; 1754 return 1;
1751 } 1755 }
1752 copy_tv(&v, &di->di_tv); 1756 copy_tv(&v, &di->di_tv);
1753 clear_tv(&v); 1757 clear_tv(&v);
1754 lua_pop(L, 2); /* key copy and value */ 1758 lua_pop(L, 2); // key copy and value
1755 } 1759 }
1756 } 1760 }
1757 } 1761 }
1758 return 1; 1762 return 1;
1759 } 1763 }
1787 1791
1788 static int 1792 static int
1789 luaV_funcref(lua_State *L) 1793 luaV_funcref(lua_State *L)
1790 { 1794 {
1791 const char *name = luaL_checkstring(L, 1); 1795 const char *name = luaL_checkstring(L, 1);
1792 /* note: not checking if function exists (needs function_exists) */ 1796 // note: not checking if function exists (needs function_exists)
1793 if (name == NULL || *name == NUL || VIM_ISDIGIT(*name)) 1797 if (name == NULL || *name == NUL || VIM_ISDIGIT(*name))
1794 luaL_error(L, "invalid function name: %s", name); 1798 luaL_error(L, "invalid function name: %s", name);
1795 luaV_newfuncref(L, (char_u *) name); 1799 luaV_newfuncref(L, (char_u *) name);
1796 return 1; 1800 return 1;
1797 } 1801 }
1798 1802
1799 static int 1803 static int
1800 luaV_buffer(lua_State *L) 1804 luaV_buffer(lua_State *L)
1801 { 1805 {
1802 buf_T *buf; 1806 buf_T *buf;
1803 if (lua_isstring(L, 1)) /* get by number or name? */ 1807 if (lua_isstring(L, 1)) // get by number or name?
1804 { 1808 {
1805 if (lua_isnumber(L, 1)) /* by number? */ 1809 if (lua_isnumber(L, 1)) // by number?
1806 { 1810 {
1807 int n = lua_tointeger(L, 1); 1811 int n = lua_tointeger(L, 1);
1808 FOR_ALL_BUFFERS(buf) 1812 FOR_ALL_BUFFERS(buf)
1809 if (buf->b_fnum == n) break; 1813 if (buf->b_fnum == n) break;
1810 } 1814 }
1823 break; 1827 break;
1824 } 1828 }
1825 } 1829 }
1826 } 1830 }
1827 else 1831 else
1828 buf = (lua_toboolean(L, 1)) ? firstbuf : curbuf; /* first buffer? */ 1832 buf = (lua_toboolean(L, 1)) ? firstbuf : curbuf; // first buffer?
1829 luaV_pushbuffer(L, buf); 1833 luaV_pushbuffer(L, buf);
1830 return 1; 1834 return 1;
1831 } 1835 }
1832 1836
1833 static int 1837 static int
1834 luaV_window(lua_State *L) 1838 luaV_window(lua_State *L)
1835 { 1839 {
1836 win_T *win; 1840 win_T *win;
1837 if (lua_isnumber(L, 1)) /* get by number? */ 1841 if (lua_isnumber(L, 1)) // get by number?
1838 { 1842 {
1839 int n = lua_tointeger(L, 1); 1843 int n = lua_tointeger(L, 1);
1840 for (win = firstwin; win != NULL; win = win->w_next, n--) 1844 for (win = firstwin; win != NULL; win = win->w_next, n--)
1841 if (n == 1) break; 1845 if (n == 1) break;
1842 } 1846 }
1843 else 1847 else
1844 win = (lua_toboolean(L, 1)) ? firstwin : curwin; /* first window? */ 1848 win = (lua_toboolean(L, 1)) ? firstwin : curwin; // first window?
1845 luaV_pushwindow(L, win); 1849 luaV_pushwindow(L, win);
1846 return 1; 1850 return 1;
1847 } 1851 }
1848 1852
1849 static int 1853 static int
1860 1864
1861 static int 1865 static int
1862 luaV_type(lua_State *L) 1866 luaV_type(lua_State *L)
1863 { 1867 {
1864 luaL_checkany(L, 1); 1868 luaL_checkany(L, 1);
1865 if (lua_type(L, 1) == LUA_TUSERDATA) /* check vim udata? */ 1869 if (lua_type(L, 1) == LUA_TUSERDATA) // check vim udata?
1866 { 1870 {
1867 lua_settop(L, 1); 1871 lua_settop(L, 1);
1868 if (lua_getmetatable(L, 1)) 1872 if (lua_getmetatable(L, 1))
1869 { 1873 {
1870 luaV_getfield(L, LUAVIM_LIST); 1874 luaV_getfield(L, LUAVIM_LIST);
1903 lua_pushstring(L, "window"); 1907 lua_pushstring(L, "window");
1904 return 1; 1908 return 1;
1905 } 1909 }
1906 } 1910 }
1907 } 1911 }
1908 lua_pushstring(L, luaL_typename(L, 1)); /* fallback */ 1912 lua_pushstring(L, luaL_typename(L, 1)); // fallback
1909 return 1; 1913 return 1;
1910 } 1914 }
1911 1915
1912 static const luaL_Reg luaV_module[] = { 1916 static const luaL_Reg luaV_module[] = {
1913 {"command", luaV_command}, 1917 {"command", luaV_command},
1923 {"open", luaV_open}, 1927 {"open", luaV_open},
1924 {"type", luaV_type}, 1928 {"type", luaV_type},
1925 {NULL, NULL} 1929 {NULL, NULL}
1926 }; 1930 };
1927 1931
1928 /* for freeing list, dict, buffer and window objects; lightuserdata as arg */ 1932 /*
1933 * for freeing list, dict, buffer and window objects; lightuserdata as arg
1934 */
1929 static int 1935 static int
1930 luaV_free(lua_State *L) 1936 luaV_free(lua_State *L)
1931 { 1937 {
1932 lua_pushnil(L); 1938 lua_pushnil(L);
1933 luaV_setudata(L, lua_touserdata(L, 1)); 1939 luaV_setudata(L, lua_touserdata(L, 1));
1945 luaL_buffinit(L, &b); 1951 luaL_buffinit(L, &b);
1946 luaL_addlstring(&b, LUAVIM_EVALHEADER, sizeof(LUAVIM_EVALHEADER) - 1); 1952 luaL_addlstring(&b, LUAVIM_EVALHEADER, sizeof(LUAVIM_EVALHEADER) - 1);
1947 luaL_addlstring(&b, str, l); 1953 luaL_addlstring(&b, str, l);
1948 luaL_pushresult(&b); 1954 luaL_pushresult(&b);
1949 str = lua_tolstring(L, -1, &l); 1955 str = lua_tolstring(L, -1, &l);
1950 if (luaL_loadbuffer(L, str, l, LUAVIM_EVALNAME)) /* compile error? */ 1956 if (luaL_loadbuffer(L, str, l, LUAVIM_EVALNAME)) // compile error?
1951 { 1957 {
1952 luaV_emsg(L); 1958 luaV_emsg(L);
1953 return 0; 1959 return 0;
1954 } 1960 }
1955 luaV_pushtypval(L, arg); 1961 luaV_pushtypval(L, arg);
1956 if (lua_pcall(L, 1, 1, 0)) /* running error? */ 1962 if (lua_pcall(L, 1, 1, 0)) // running error?
1957 { 1963 {
1958 luaV_emsg(L); 1964 luaV_emsg(L);
1959 return 0; 1965 return 0;
1960 } 1966 }
1961 if (luaV_totypval(L, -1, rettv) == FAIL) 1967 if (luaV_totypval(L, -1, rettv) == FAIL)
2002 } 2008 }
2003 2009
2004 static int 2010 static int
2005 luaopen_vim(lua_State *L) 2011 luaopen_vim(lua_State *L)
2006 { 2012 {
2007 /* set cache table */ 2013 // set cache table
2008 lua_newtable(L); 2014 lua_newtable(L);
2009 lua_newtable(L); 2015 lua_newtable(L);
2010 lua_pushstring(L, "v"); 2016 lua_pushstring(L, "v");
2011 lua_setfield(L, -2, "__mode"); 2017 lua_setfield(L, -2, "__mode");
2012 lua_setmetatable(L, -2); /* cache is weak-valued */ 2018 lua_setmetatable(L, -2); // cache is weak-valued
2013 /* print */ 2019 // print
2014 lua_pushcfunction(L, luaV_print); 2020 lua_pushcfunction(L, luaV_print);
2015 lua_setglobal(L, "print"); 2021 lua_setglobal(L, "print");
2016 /* debug.debug */ 2022 // debug.debug
2017 lua_getglobal(L, "debug"); 2023 lua_getglobal(L, "debug");
2018 lua_pushcfunction(L, luaV_debug); 2024 lua_pushcfunction(L, luaV_debug);
2019 lua_setfield(L, -2, "debug"); 2025 lua_setfield(L, -2, "debug");
2020 lua_pop(L, 1); 2026 lua_pop(L, 1);
2021 /* free */ 2027 // free
2022 lua_pushlightuserdata(L, (void *) LUAVIM_FREE); 2028 lua_pushlightuserdata(L, (void *) LUAVIM_FREE);
2023 lua_pushvalue(L, 1); /* cache table */ 2029 lua_pushvalue(L, 1); // cache table
2024 lua_pushcclosure(L, luaV_free, 1); 2030 lua_pushcclosure(L, luaV_free, 1);
2025 lua_rawset(L, LUA_REGISTRYINDEX); 2031 lua_rawset(L, LUA_REGISTRYINDEX);
2026 /* luaeval */ 2032 // luaeval
2027 lua_pushlightuserdata(L, (void *) LUAVIM_LUAEVAL); 2033 lua_pushlightuserdata(L, (void *) LUAVIM_LUAEVAL);
2028 lua_pushvalue(L, 1); /* cache table */ 2034 lua_pushvalue(L, 1); // cache table
2029 lua_pushcclosure(L, luaV_luaeval, 1); 2035 lua_pushcclosure(L, luaV_luaeval, 1);
2030 lua_rawset(L, LUA_REGISTRYINDEX); 2036 lua_rawset(L, LUA_REGISTRYINDEX);
2031 /* setref */ 2037 // setref
2032 lua_pushlightuserdata(L, (void *) LUAVIM_SETREF); 2038 lua_pushlightuserdata(L, (void *) LUAVIM_SETREF);
2033 lua_pushvalue(L, 1); /* cache table */ 2039 lua_pushvalue(L, 1); // cache table
2034 lua_pushcclosure(L, luaV_setref, 1); 2040 lua_pushcclosure(L, luaV_setref, 1);
2035 lua_rawset(L, LUA_REGISTRYINDEX); 2041 lua_rawset(L, LUA_REGISTRYINDEX);
2036 /* register */ 2042 // register
2037 luaV_newmetatable(L, LUAVIM_LIST); 2043 luaV_newmetatable(L, LUAVIM_LIST);
2038 lua_pushvalue(L, 1); 2044 lua_pushvalue(L, 1);
2039 luaV_openlib(L, luaV_List_mt, 1); 2045 luaV_openlib(L, luaV_List_mt, 1);
2040 luaV_newmetatable(L, LUAVIM_DICT); 2046 luaV_newmetatable(L, LUAVIM_DICT);
2041 lua_pushvalue(L, 1); 2047 lua_pushvalue(L, 1);
2045 luaV_openlib(L, luaV_Blob_mt, 1); 2051 luaV_openlib(L, luaV_Blob_mt, 1);
2046 luaV_newmetatable(L, LUAVIM_FUNCREF); 2052 luaV_newmetatable(L, LUAVIM_FUNCREF);
2047 lua_pushvalue(L, 1); 2053 lua_pushvalue(L, 1);
2048 luaV_openlib(L, luaV_Funcref_mt, 1); 2054 luaV_openlib(L, luaV_Funcref_mt, 1);
2049 luaV_newmetatable(L, LUAVIM_BUFFER); 2055 luaV_newmetatable(L, LUAVIM_BUFFER);
2050 lua_pushvalue(L, 1); /* cache table */ 2056 lua_pushvalue(L, 1); // cache table
2051 luaV_openlib(L, luaV_Buffer_mt, 1); 2057 luaV_openlib(L, luaV_Buffer_mt, 1);
2052 luaV_newmetatable(L, LUAVIM_WINDOW); 2058 luaV_newmetatable(L, LUAVIM_WINDOW);
2053 lua_pushvalue(L, 1); /* cache table */ 2059 lua_pushvalue(L, 1); // cache table
2054 luaV_openlib(L, luaV_Window_mt, 1); 2060 luaV_openlib(L, luaV_Window_mt, 1);
2055 lua_newtable(L); /* vim table */ 2061 lua_newtable(L); // vim table
2056 lua_pushvalue(L, 1); /* cache table */ 2062 lua_pushvalue(L, 1); // cache table
2057 luaV_openlib(L, luaV_module, 1); 2063 luaV_openlib(L, luaV_module, 1);
2058 lua_setglobal(L, LUAVIM_NAME); 2064 lua_setglobal(L, LUAVIM_NAME);
2059 return 0; 2065 return 0;
2060 } 2066 }
2061 2067
2062 static lua_State * 2068 static lua_State *
2063 luaV_newstate(void) 2069 luaV_newstate(void)
2064 { 2070 {
2065 lua_State *L = luaL_newstate(); 2071 lua_State *L = luaL_newstate();
2066 luaL_openlibs(L); /* core libs */ 2072 luaL_openlibs(L); // core libs
2067 lua_pushcfunction(L, luaopen_vim); /* vim */ 2073 lua_pushcfunction(L, luaopen_vim); // vim
2068 lua_call(L, 0, 0); 2074 lua_call(L, 0, 0);
2069 return L; 2075 return L;
2070 } 2076 }
2071 2077
2072 static void 2078 static void
2075 lua_getglobal(L, LUAVIM_NAME); 2081 lua_getglobal(L, LUAVIM_NAME);
2076 lua_pushinteger(L, line1); 2082 lua_pushinteger(L, line1);
2077 lua_setfield(L, -2, "firstline"); 2083 lua_setfield(L, -2, "firstline");
2078 lua_pushinteger(L, line2); 2084 lua_pushinteger(L, line2);
2079 lua_setfield(L, -2, "lastline"); 2085 lua_setfield(L, -2, "lastline");
2080 lua_pop(L, 1); /* vim table */ 2086 lua_pop(L, 1); // vim table
2081 } 2087 }
2082 2088
2083 2089
2084 /* ======= Interface ======= */ 2090 // ======= Interface =======
2085 2091
2086 static lua_State *L = NULL; 2092 static lua_State *L = NULL;
2087 2093
2088 static int 2094 static int
2089 lua_isopen(void) 2095 lua_isopen(void)
2119 end_dynamic_lua(); 2125 end_dynamic_lua();
2120 #endif 2126 #endif
2121 } 2127 }
2122 } 2128 }
2123 2129
2124 /* ex commands */ 2130 /*
2131 * ex commands
2132 */
2125 void 2133 void
2126 ex_lua(exarg_T *eap) 2134 ex_lua(exarg_T *eap)
2127 { 2135 {
2128 char *script; 2136 char *script;
2129 if (lua_init() == FAIL) return; 2137 if (lua_init() == FAIL) return;
2154 emsg(_("cannot save undo information")); 2162 emsg(_("cannot save undo information"));
2155 return; 2163 return;
2156 } 2164 }
2157 luaV_setrange(L, eap->line1, eap->line2); 2165 luaV_setrange(L, eap->line1, eap->line2);
2158 luaL_buffinit(L, &b); 2166 luaL_buffinit(L, &b);
2159 luaL_addlstring(&b, "return function(line, linenr) ", 30); /* header */ 2167 luaL_addlstring(&b, "return function(line, linenr) ", 30); // header
2160 luaL_addlstring(&b, s, strlen(s)); 2168 luaL_addlstring(&b, s, strlen(s));
2161 luaL_addlstring(&b, " end", 4); /* footer */ 2169 luaL_addlstring(&b, " end", 4); // footer
2162 luaL_pushresult(&b); 2170 luaL_pushresult(&b);
2163 s = lua_tolstring(L, -1, &len); 2171 s = lua_tolstring(L, -1, &len);
2164 if (luaL_loadbuffer(L, s, len, LUAVIM_CHUNKNAME)) 2172 if (luaL_loadbuffer(L, s, len, LUAVIM_CHUNKNAME))
2165 { 2173 {
2166 luaV_emsg(L); 2174 luaV_emsg(L);
2167 lua_pop(L, 1); /* function body */ 2175 lua_pop(L, 1); // function body
2168 return; 2176 return;
2169 } 2177 }
2170 lua_call(L, 0, 1); 2178 lua_call(L, 0, 1);
2171 lua_replace(L, -2); /* function -> body */ 2179 lua_replace(L, -2); // function -> body
2172 for (l = eap->line1; l <= eap->line2; l++) 2180 for (l = eap->line1; l <= eap->line2; l++)
2173 { 2181 {
2174 /* Check the line number, the command my have deleted lines. */ 2182 // Check the line number, the command my have deleted lines.
2175 if (l > curbuf->b_ml.ml_line_count) 2183 if (l > curbuf->b_ml.ml_line_count)
2176 break; 2184 break;
2177 2185
2178 lua_pushvalue(L, -1); /* function */ 2186 lua_pushvalue(L, -1); // function
2179 luaV_pushline(L, curbuf, l); /* current line as arg */ 2187 luaV_pushline(L, curbuf, l); // current line as arg
2180 lua_pushinteger(L, l); /* current line number as arg */ 2188 lua_pushinteger(L, l); // current line number as arg
2181 if (lua_pcall(L, 2, 1, 0)) 2189 if (lua_pcall(L, 2, 1, 0))
2182 { 2190 {
2183 luaV_emsg(L); 2191 luaV_emsg(L);
2184 break; 2192 break;
2185 } 2193 }
2186 /* Catch the command switching to another buffer. */ 2194 // Catch the command switching to another buffer.
2187 if (curbuf != was_curbuf) 2195 if (curbuf != was_curbuf)
2188 break; 2196 break;
2189 if (lua_isstring(L, -1)) /* update line? */ 2197 if (lua_isstring(L, -1)) // update line?
2190 { 2198 {
2191 #ifdef HAVE_SANDBOX 2199 #ifdef HAVE_SANDBOX
2192 luaV_checksandbox(L); 2200 luaV_checksandbox(L);
2193 #endif 2201 #endif
2194 ml_replace(l, luaV_toline(L, -1), TRUE); 2202 ml_replace(l, luaV_toline(L, -1), TRUE);
2195 changed_bytes(l, 0); 2203 changed_bytes(l, 0);
2196 lua_pop(L, 1); /* result from luaV_toline */ 2204 lua_pop(L, 1); // result from luaV_toline
2197 } 2205 }
2198 lua_pop(L, 1); /* line */ 2206 lua_pop(L, 1); // line
2199 } 2207 }
2200 lua_pop(L, 1); /* function */ 2208 lua_pop(L, 1); // function
2201 check_cursor(); 2209 check_cursor();
2202 update_screen(NOT_VALID); 2210 update_screen(NOT_VALID);
2203 } 2211 }
2204 2212
2205 void 2213 void
2245 int aborted = 0; 2253 int aborted = 0;
2246 2254
2247 if (lua_isopen()) 2255 if (lua_isopen())
2248 { 2256 {
2249 luaV_getfield(L, LUAVIM_SETREF); 2257 luaV_getfield(L, LUAVIM_SETREF);
2250 /* call the function with 1 arg, getting 1 result back */ 2258 // call the function with 1 arg, getting 1 result back
2251 lua_pushinteger(L, copyID); 2259 lua_pushinteger(L, copyID);
2252 lua_call(L, 1, 1); 2260 lua_call(L, 1, 1);
2253 /* get the result */ 2261 // get the result
2254 aborted = lua_tointeger(L, -1); 2262 aborted = lua_tointeger(L, -1);
2255 /* pop result off the stack */ 2263 // pop result off the stack
2256 lua_pop(L, 1); 2264 lua_pop(L, 1);
2257 } 2265 }
2258 return aborted; 2266 return aborted;
2259 } 2267 }
2260 2268