comparison src/if_lua.c @ 25527:d5e9c05b4811 v8.2.3300

patch 8.2.3300: Lua: can only execute on Vim command at a time Commit: https://github.com/vim/vim/commit/11328bc7df0ecc47f4025a10bb86882a659e9994 Author: Yegappan Lakshmanan <yegappan@yahoo.com> Date: Fri Aug 6 21:34:38 2021 +0200 patch 8.2.3300: Lua: can only execute on Vim command at a time Problem: Lua: can only execute on Vim command at a time. Not easy to get the Vim version. Solution: Make vim.command() accept multiple lines. Add vim.version(). (Yegappan Lakshmanan, closes #8716)
author Bram Moolenaar <Bram@vim.org>
date Fri, 06 Aug 2021 21:45:05 +0200
parents d84f6784c27c
children 2ee0425960fe
comparison
equal deleted inserted replaced
25526:f9ebb5b05597 25527:d5e9c05b4811
8 * Do ":help credits" in Vim to see a list of people who contributed. 8 * Do ":help credits" in Vim to see a list of people who contributed.
9 * See README.txt for an overview of the Vim source code. 9 * See README.txt for an overview of the Vim source code.
10 */ 10 */
11 11
12 #include "vim.h" 12 #include "vim.h"
13 #include "version.h"
13 14
14 #include <lua.h> 15 #include <lua.h>
15 #include <lualib.h> 16 #include <lualib.h>
16 #include <lauxlib.h> 17 #include <lauxlib.h>
17 18
189 #define lua_pushlightuserdata dll_lua_pushlightuserdata 190 #define lua_pushlightuserdata dll_lua_pushlightuserdata
190 #define lua_getfield dll_lua_getfield 191 #define lua_getfield dll_lua_getfield
191 #define lua_rawget dll_lua_rawget 192 #define lua_rawget dll_lua_rawget
192 #define lua_rawgeti dll_lua_rawgeti 193 #define lua_rawgeti dll_lua_rawgeti
193 #define lua_createtable dll_lua_createtable 194 #define lua_createtable dll_lua_createtable
195 #define lua_settable dll_lua_settable
194 #if LUA_VERSION_NUM >= 504 196 #if LUA_VERSION_NUM >= 504
195 #define lua_newuserdatauv dll_lua_newuserdatauv 197 #define lua_newuserdatauv dll_lua_newuserdatauv
196 #else 198 #else
197 #define lua_newuserdata dll_lua_newuserdata 199 #define lua_newuserdata dll_lua_newuserdata
198 #endif 200 #endif
300 #else 302 #else
301 int (*dll_lua_rawget) (lua_State *L, int idx); 303 int (*dll_lua_rawget) (lua_State *L, int idx);
302 int (*dll_lua_rawgeti) (lua_State *L, int idx, lua_Integer n); 304 int (*dll_lua_rawgeti) (lua_State *L, int idx, lua_Integer n);
303 #endif 305 #endif
304 void (*dll_lua_createtable) (lua_State *L, int narr, int nrec); 306 void (*dll_lua_createtable) (lua_State *L, int narr, int nrec);
307 void (*dll_lua_settable) (lua_State *L, int idx);
305 #if LUA_VERSION_NUM >= 504 308 #if LUA_VERSION_NUM >= 504
306 void *(*dll_lua_newuserdatauv) (lua_State *L, size_t sz, int nuvalue); 309 void *(*dll_lua_newuserdatauv) (lua_State *L, size_t sz, int nuvalue);
307 #else 310 #else
308 void *(*dll_lua_newuserdata) (lua_State *L, size_t sz); 311 void *(*dll_lua_newuserdata) (lua_State *L, size_t sz);
309 #endif 312 #endif
411 {"lua_pushlightuserdata", (luaV_function) &dll_lua_pushlightuserdata}, 414 {"lua_pushlightuserdata", (luaV_function) &dll_lua_pushlightuserdata},
412 {"lua_getfield", (luaV_function) &dll_lua_getfield}, 415 {"lua_getfield", (luaV_function) &dll_lua_getfield},
413 {"lua_rawget", (luaV_function) &dll_lua_rawget}, 416 {"lua_rawget", (luaV_function) &dll_lua_rawget},
414 {"lua_rawgeti", (luaV_function) &dll_lua_rawgeti}, 417 {"lua_rawgeti", (luaV_function) &dll_lua_rawgeti},
415 {"lua_createtable", (luaV_function) &dll_lua_createtable}, 418 {"lua_createtable", (luaV_function) &dll_lua_createtable},
419 {"lua_settable", (luaV_function) &dll_lua_settable},
416 #if LUA_VERSION_NUM >= 504 420 #if LUA_VERSION_NUM >= 504
417 {"lua_newuserdatauv", (luaV_function) &dll_lua_newuserdatauv}, 421 {"lua_newuserdatauv", (luaV_function) &dll_lua_newuserdatauv},
418 #else 422 #else
419 {"lua_newuserdata", (luaV_function) &dll_lua_newuserdata}, 423 {"lua_newuserdata", (luaV_function) &dll_lua_newuserdata},
420 #endif 424 #endif
1817 1821
1818 dict = luaV_get_var_scope(L); 1822 dict = luaV_get_var_scope(L);
1819 if (dict == NULL) 1823 if (dict == NULL)
1820 return 0; 1824 return 0;
1821 1825
1822 di = dict_find(dict, (char_u *)name, len); 1826 di = dict_find(dict, (char_u *)name, (int)len);
1823 if (di != NULL) 1827 if (di != NULL)
1824 { 1828 {
1825 if (di->di_flags & DI_FLAGS_RO) 1829 if (di->di_flags & DI_FLAGS_RO)
1826 error = "variable is read-only"; 1830 error = "variable is read-only";
1827 else if (di->di_flags & DI_FLAGS_LOCK) 1831 else if (di->di_flags & DI_FLAGS_LOCK)
1891 luaV_getvar(lua_State *L) 1895 luaV_getvar(lua_State *L)
1892 { 1896 {
1893 dict_T *dict = luaV_get_var_scope(L); 1897 dict_T *dict = luaV_get_var_scope(L);
1894 size_t len; 1898 size_t len;
1895 const char *name = luaL_checklstring(L, 3, &len); 1899 const char *name = luaL_checklstring(L, 3, &len);
1896 1900 dictitem_T *di = dict_find(dict, (char_u *)name, (int)len);
1897 dictitem_T *di = dict_find(dict, (char_u *)name, len); 1901
1898 if (di == NULL) 1902 if (di == NULL)
1899 return 0; // nil 1903 return 0; // nil
1900 1904
1901 luaV_pushtypval(L, &di->di_tv); 1905 luaV_pushtypval(L, &di->di_tv);
1902 return 1; 1906 return 1;
1903 } 1907 }
1904 1908
1905 static int 1909 static int
1906 luaV_command(lua_State *L) 1910 luaV_command(lua_State *L)
1907 { 1911 {
1908 do_cmdline_cmd((char_u *) luaL_checkstring(L, 1)); 1912 char_u *s = vim_strsave((char_u *)luaL_checkstring(L, 1));
1913
1914 execute_cmds_from_string(s);
1915 vim_free(s);
1909 update_screen(VALID); 1916 update_screen(VALID);
1910 return 0; 1917 return 0;
1911 } 1918 }
1912 1919
1913 static int 1920 static int
1914 luaV_eval(lua_State *L) 1921 luaV_eval(lua_State *L)
1915 { 1922 {
1916 typval_T *tv = eval_expr((char_u *) luaL_checkstring(L, 1), NULL); 1923 typval_T *tv = eval_expr((char_u *) luaL_checkstring(L, 1), NULL);
1924
1917 if (tv == NULL) luaL_error(L, "invalid expression"); 1925 if (tv == NULL) luaL_error(L, "invalid expression");
1918 luaV_pushtypval(L, tv); 1926 luaV_pushtypval(L, tv);
1919 free_tv(tv); 1927 free_tv(tv);
1920 return 1; 1928 return 1;
1921 } 1929 }
2212 return 1; 2220 return 1;
2213 else 2221 else
2214 return luaL_error(L, error); 2222 return luaL_error(L, error);
2215 } 2223 }
2216 2224
2225 /*
2226 * Return the Vim version as a Lua table
2227 */
2228 static int
2229 luaV_version(lua_State *L)
2230 {
2231 lua_newtable(L);
2232 lua_pushstring(L, "major");
2233 lua_pushinteger(L, VIM_VERSION_MAJOR);
2234 lua_settable(L, -3);
2235 lua_pushstring(L, "minor");
2236 lua_pushinteger(L, VIM_VERSION_MINOR);
2237 lua_settable(L, -3);
2238 lua_pushstring(L, "patch");
2239 lua_pushinteger(L, highest_patch());
2240 lua_settable(L, -3);
2241 return 1;
2242 }
2243
2217 static const luaL_Reg luaV_module[] = { 2244 static const luaL_Reg luaV_module[] = {
2218 {"command", luaV_command}, 2245 {"command", luaV_command},
2219 {"eval", luaV_eval}, 2246 {"eval", luaV_eval},
2220 {"beep", luaV_beep}, 2247 {"beep", luaV_beep},
2221 {"line", luaV_line}, 2248 {"line", luaV_line},
2228 {"open", luaV_open}, 2255 {"open", luaV_open},
2229 {"type", luaV_type}, 2256 {"type", luaV_type},
2230 {"call", luaV_call}, 2257 {"call", luaV_call},
2231 {"_getvar", luaV_getvar}, 2258 {"_getvar", luaV_getvar},
2232 {"_setvar", luaV_setvar}, 2259 {"_setvar", luaV_setvar},
2260 {"version", luaV_version},
2233 {"lua_version", NULL}, 2261 {"lua_version", NULL},
2234 {NULL, NULL} 2262 {NULL, NULL}
2235 }; 2263 };
2236 2264
2237 /* 2265 /*