Patch 8.2.0858
Problem: Not easy to require Lua modules.
Solution: Improve use of Lua path. (Prabir Shrestha, closes #6098)
Files: Filelist, src/if_lua.c, src/optionstr.c, src/proto/if_lua.pro,
src/testdir/test_lua.vim,
src/testdir/testluaplugin/lua/testluaplugin/hello.lua,
src/testdir/testluaplugin/lua/testluaplugin/init.lua
*** ../vim-8.2.0857/Filelist 2020-05-30 18:14:37.820521090 +0200
--- Filelist 2020-05-31 14:06:25.012904415 +0200
***************
*** 157,162 ****
--- 157,163 ----
src/testdir/*.py \
src/testdir/lsan-suppress.txt \
src/testdir/sautest/autoload/*.vim \
+ src/testdir/testluaplugin/lua/testluaplugin/*.lua \
src/testdir/check.vim \
src/testdir/gui_init.vim \
src/testdir/gui_preinit.vim \
*** ../vim-8.2.0857/src/if_lua.c 2020-05-30 20:30:42.892816571 +0200
--- src/if_lua.c 2020-05-31 13:57:42.295027017 +0200
***************
*** 2061,2075 ****
}
#define LUA_VIM_FN_CODE \
! "vim.fn = setmetatable({}, {"\
! " __index = function (t, key)"\
! " local function _fn(...)"\
! " return vim.call(key, ...)"\
! " end"\
! " t[key] = _fn"\
! " return _fn"\
! " end"\
! "})"
static int
luaopen_vim(lua_State *L)
--- 2061,2140 ----
}
#define LUA_VIM_FN_CODE \
! "vim.fn = setmetatable({}, {\n"\
! " __index = function (t, key)\n"\
! " local function _fn(...)\n"\
! " return vim.call(key, ...)\n"\
! " end\n"\
! " t[key] = _fn\n"\
! " return _fn\n"\
! " end\n"\
! " })"
!
! #define LUA_VIM_UPDATE_PACKAGE_PATHS \
! "local last_vim_paths = {}\n"\
! "vim._update_package_paths = function ()\n"\
! " local cur_vim_paths = {}\n"\
! " local function split(s, delimiter)\n"\
! " result = {}\n"\
! " for match in (s..delimiter):gmatch(\"(.-)\"..delimiter) do\n"\
! " table.insert(result, match)\n"\
! " end\n"\
! " return result\n"\
! " end\n"\
! " local rtps = split(vim.eval('&runtimepath'), ',')\n"\
! " local sep = package.config:sub(1, 1)\n"\
! " for _, key in ipairs({'path', 'cpath'}) do\n"\
! " local orig_str = package[key] .. ';'\n"\
! " local pathtrails_ordered = {}\n"\
! " -- Note: ignores trailing item without trailing `;`. Not using
something\n"\
! " -- simpler in order to preserve empty items (stand for default
path).\n"\
! " local orig = {}\n"\
! " for s in orig_str:gmatch('[^;]*;') do\n"\
! " s = s:sub(1, -2) -- Strip trailing semicolon\n"\
! " orig[#orig + 1] = s\n"\
! " end\n"\
! " if key == 'path' then\n"\
! " -- /?.lua and /?/init.lua\n"\
! " pathtrails_ordered = {sep .. '?.lua', sep .. '?' .. sep ..
'init.lua'}\n"\
! " else\n"\
! " local pathtrails = {}\n"\
! " for _, s in ipairs(orig) do\n"\
! " -- Find out path patterns. pathtrail should contain something
like\n"\
! " -- /?.so, \?.dll. This allows not to bother determining what
correct\n"\
! " -- suffixes are.\n"\
! " local pathtrail = s:match('[/\\\\][^/\\\\]*%?.*$')\n"\
! " if pathtrail and not pathtrails[pathtrail] then\n"\
! " pathtrails[pathtrail] = true\n"\
! " pathtrails_ordered[#pathtrails_ordered + 1] = pathtrail\n"\
! " end\n"\
! " end\n"\
! " end\n"\
! " local new = {}\n"\
! " for _, rtp in ipairs(rtps) do\n"\
! " if not rtp:match(';') then\n"\
! " for _, pathtrail in pairs(pathtrails_ordered) do\n"\
! " local new_path = rtp .. sep .. 'lua' .. pathtrail\n"\
! " -- Always keep paths from &runtimepath at the start:\n"\
! " -- append them here disregarding orig possibly containing one
of them.\n"\
! " new[#new + 1] = new_path\n"\
! " cur_vim_paths[new_path] = true\n"\
! " end\n"\
! " end\n"\
! " end\n"\
! " for _, orig_path in ipairs(orig) do\n"\
! " -- Handle removing obsolete paths originating from &runtimepath:
such\n"\
! " -- paths either belong to cur_nvim_paths and were already added
above or\n"\
! " -- to last_nvim_paths and should not be added at all if
corresponding\n"\
! " -- entry was removed from &runtimepath list.\n"\
! " if not (cur_vim_paths[orig_path] or last_vim_paths[orig_path])
then\n"\
! " new[#new + 1] = orig_path\n"\
! " end\n"\
! " end\n"\
! " package[key] = table.concat(new, ';')\n"\
! " end\n"\
! " last_vim_paths = cur_vim_paths\n"\
! "end"
static int
luaopen_vim(lua_State *L)
***************
*** 2128,2133 ****
--- 2193,2206 ----
lua_setglobal(L, LUAVIM_NAME);
// custom code
(void)luaL_dostring(L, LUA_VIM_FN_CODE);
+ (void)luaL_dostring(L, LUA_VIM_UPDATE_PACKAGE_PATHS);
+
+ lua_getglobal(L, "vim");
+ lua_getfield(L, -1, "_update_package_paths");
+
+ if (lua_pcall(L, 0, 0, 0))
+ luaV_emsg(L);
+
return 0;
}
***************
*** 2329,2332 ****
--- 2402,2418 ----
return aborted;
}
+ void
+ update_package_paths_in_lua()
+ {
+ if (lua_isopen())
+ {
+ lua_getglobal(L, "vim");
+ lua_getfield(L, -1, "_update_package_paths");
+
+ if (lua_pcall(L, 0, 0, 0))
+ luaV_emsg(L);
+ }
+ }
+
#endif
*** ../vim-8.2.0857/src/optionstr.c 2020-05-15 22:30:34.895488256 +0200
--- src/optionstr.c 2020-05-31 13:57:42.295027017 +0200
***************
*** 2402,2407 ****
--- 2402,2412 ----
setmouse(); // in case 'mouse' changed
}
+ #if defined(FEAT_LUA) || defined(PROTO)
+ if (varp == &p_rtp)
+ update_package_paths_in_lua();
+ #endif
+
if (curwin->w_curswant != MAXCOL
&& (get_option_flags(opt_idx) & (P_CURSWANT | P_RALL)) != 0)
curwin->w_set_curswant = TRUE;
*** ../vim-8.2.0857/src/proto/if_lua.pro 2019-12-12 12:55:23.000000000
+0100
--- src/proto/if_lua.pro 2020-05-31 14:02:02.773965206 +0200
***************
*** 8,11 ****
--- 8,12 ----
void lua_window_free(win_T *o);
void do_luaeval(char_u *str, typval_T *arg, typval_T *rettv);
int set_ref_in_lua(int copyID);
+ void update_package_paths_in_lua(void);
/* vim: set ft=c : */
*** ../vim-8.2.0857/src/testdir/test_lua.vim 2020-05-17 14:32:30.584490790
+0200
--- src/testdir/test_lua.vim 2020-05-31 13:57:42.295027017 +0200
***************
*** 536,541 ****
--- 536,546 ----
%bwipe!
endfunc
+ func Test_update_package_paths()
+ set runtimepath+=./testluaplugin
+ call assert_equal("hello from lua",
luaeval("require('testluaplugin').hello()"))
+ endfunc
+
" Test vim.line()
func Test_lua_line()
new
*** ../vim-8.2.0857/src/testdir/testluaplugin/lua/testluaplugin/hello.lua
1970-01-01 01:00:00.000000000 +0100
--- src/testdir/testluaplugin/lua/testluaplugin/hello.lua 2020-05-31
13:57:42.295027017 +0200
***************
*** 0 ****
--- 1,7 ----
+ local function hello()
+ return "hello from lua"
+ end
+
+ return {
+ hello = hello
+ }
*** ../vim-8.2.0857/src/testdir/testluaplugin/lua/testluaplugin/init.lua
1970-01-01 01:00:00.000000000 +0100
--- src/testdir/testluaplugin/lua/testluaplugin/init.lua 2020-05-31
13:57:42.295027017 +0200
***************
*** 0 ****
--- 1,5 ----
+ local hello = require('testluaplugin/hello').hello
+
+ return {
+ hello = hello
+ }
*** ../vim-8.2.0857/src/version.c 2020-05-31 13:53:01.548186282 +0200
--- src/version.c 2020-05-31 14:06:42.956832031 +0200
***************
*** 748,749 ****
--- 748,751 ----
{ /* Add new patch number below this line */
+ /**/
+ 858,
/**/
--
hundred-and-one symptoms of being an internet addict:
227. You sleep next to your monitor. Or on top of it.
/// Bram Moolenaar -- [email protected] -- http://www.Moolenaar.net \\\
/// sponsor Vim, vote for features -- http://www.Vim.org/sponsor/ \\\
\\\ an exciting new programming language -- http://www.Zimbu.org ///
\\\ help me help AIDS victims -- http://ICCF-Holland.org ///
--
--
You received this message from the "vim_dev" maillist.
Do not top-post! Type your reply below the text you are replying to.
For more information, visit http://www.vim.org/maillist.php
---
You received this message because you are subscribed to the Google Groups
"vim_dev" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To view this discussion on the web visit
https://groups.google.com/d/msgid/vim_dev/202005311208.04VC8ku9604687%40masaka.moolenaar.net.