comparison src/if_lua.c @ 20609:054ba681412d v8.2.0858

patch 8.2.0858: not easy to require Lua modules Commit: https://github.com/vim/vim/commit/788fbb47079e6df4d4815d27273faf8390395029 Author: Bram Moolenaar <Bram@vim.org> Date: Sun May 31 14:08:12 2020 +0200 patch 8.2.0858: not easy to require Lua modules Problem: Not easy to require Lua modules. Solution: Improve use of Lua path. (Prabir Shrestha, closes https://github.com/vim/vim/issues/6098)
author Bram Moolenaar <Bram@vim.org>
date Sun, 31 May 2020 14:15:03 +0200
parents d571231175b4
children ae185f35e256
comparison
equal deleted inserted replaced
20608:13d49684acf5 20609:054ba681412d
2059 lua_pushinteger(L, abort); 2059 lua_pushinteger(L, abort);
2060 return 1; 2060 return 1;
2061 } 2061 }
2062 2062
2063 #define LUA_VIM_FN_CODE \ 2063 #define LUA_VIM_FN_CODE \
2064 "vim.fn = setmetatable({}, {"\ 2064 "vim.fn = setmetatable({}, {\n"\
2065 " __index = function (t, key)"\ 2065 " __index = function (t, key)\n"\
2066 " local function _fn(...)"\ 2066 " local function _fn(...)\n"\
2067 " return vim.call(key, ...)"\ 2067 " return vim.call(key, ...)\n"\
2068 " end"\ 2068 " end\n"\
2069 " t[key] = _fn"\ 2069 " t[key] = _fn\n"\
2070 " return _fn"\ 2070 " return _fn\n"\
2071 " end"\ 2071 " end\n"\
2072 "})" 2072 " })"
2073
2074 #define LUA_VIM_UPDATE_PACKAGE_PATHS \
2075 "local last_vim_paths = {}\n"\
2076 "vim._update_package_paths = function ()\n"\
2077 " local cur_vim_paths = {}\n"\
2078 " local function split(s, delimiter)\n"\
2079 " result = {}\n"\
2080 " for match in (s..delimiter):gmatch(\"(.-)\"..delimiter) do\n"\
2081 " table.insert(result, match)\n"\
2082 " end\n"\
2083 " return result\n"\
2084 " end\n"\
2085 " local rtps = split(vim.eval('&runtimepath'), ',')\n"\
2086 " local sep = package.config:sub(1, 1)\n"\
2087 " for _, key in ipairs({'path', 'cpath'}) do\n"\
2088 " local orig_str = package[key] .. ';'\n"\
2089 " local pathtrails_ordered = {}\n"\
2090 " -- Note: ignores trailing item without trailing `;`. Not using something\n"\
2091 " -- simpler in order to preserve empty items (stand for default path).\n"\
2092 " local orig = {}\n"\
2093 " for s in orig_str:gmatch('[^;]*;') do\n"\
2094 " s = s:sub(1, -2) -- Strip trailing semicolon\n"\
2095 " orig[#orig + 1] = s\n"\
2096 " end\n"\
2097 " if key == 'path' then\n"\
2098 " -- /?.lua and /?/init.lua\n"\
2099 " pathtrails_ordered = {sep .. '?.lua', sep .. '?' .. sep .. 'init.lua'}\n"\
2100 " else\n"\
2101 " local pathtrails = {}\n"\
2102 " for _, s in ipairs(orig) do\n"\
2103 " -- Find out path patterns. pathtrail should contain something like\n"\
2104 " -- /?.so, \?.dll. This allows not to bother determining what correct\n"\
2105 " -- suffixes are.\n"\
2106 " local pathtrail = s:match('[/\\\\][^/\\\\]*%?.*$')\n"\
2107 " if pathtrail and not pathtrails[pathtrail] then\n"\
2108 " pathtrails[pathtrail] = true\n"\
2109 " pathtrails_ordered[#pathtrails_ordered + 1] = pathtrail\n"\
2110 " end\n"\
2111 " end\n"\
2112 " end\n"\
2113 " local new = {}\n"\
2114 " for _, rtp in ipairs(rtps) do\n"\
2115 " if not rtp:match(';') then\n"\
2116 " for _, pathtrail in pairs(pathtrails_ordered) do\n"\
2117 " local new_path = rtp .. sep .. 'lua' .. pathtrail\n"\
2118 " -- Always keep paths from &runtimepath at the start:\n"\
2119 " -- append them here disregarding orig possibly containing one of them.\n"\
2120 " new[#new + 1] = new_path\n"\
2121 " cur_vim_paths[new_path] = true\n"\
2122 " end\n"\
2123 " end\n"\
2124 " end\n"\
2125 " for _, orig_path in ipairs(orig) do\n"\
2126 " -- Handle removing obsolete paths originating from &runtimepath: such\n"\
2127 " -- paths either belong to cur_nvim_paths and were already added above or\n"\
2128 " -- to last_nvim_paths and should not be added at all if corresponding\n"\
2129 " -- entry was removed from &runtimepath list.\n"\
2130 " if not (cur_vim_paths[orig_path] or last_vim_paths[orig_path]) then\n"\
2131 " new[#new + 1] = orig_path\n"\
2132 " end\n"\
2133 " end\n"\
2134 " package[key] = table.concat(new, ';')\n"\
2135 " end\n"\
2136 " last_vim_paths = cur_vim_paths\n"\
2137 "end"
2073 2138
2074 static int 2139 static int
2075 luaopen_vim(lua_State *L) 2140 luaopen_vim(lua_State *L)
2076 { 2141 {
2077 // set cache table 2142 // set cache table
2126 lua_pushvalue(L, 1); // cache table 2191 lua_pushvalue(L, 1); // cache table
2127 luaV_openlib(L, luaV_module, 1); 2192 luaV_openlib(L, luaV_module, 1);
2128 lua_setglobal(L, LUAVIM_NAME); 2193 lua_setglobal(L, LUAVIM_NAME);
2129 // custom code 2194 // custom code
2130 (void)luaL_dostring(L, LUA_VIM_FN_CODE); 2195 (void)luaL_dostring(L, LUA_VIM_FN_CODE);
2196 (void)luaL_dostring(L, LUA_VIM_UPDATE_PACKAGE_PATHS);
2197
2198 lua_getglobal(L, "vim");
2199 lua_getfield(L, -1, "_update_package_paths");
2200
2201 if (lua_pcall(L, 0, 0, 0))
2202 luaV_emsg(L);
2203
2131 return 0; 2204 return 0;
2132 } 2205 }
2133 2206
2134 static lua_State * 2207 static lua_State *
2135 luaV_newstate(void) 2208 luaV_newstate(void)
2327 lua_pop(L, 1); 2400 lua_pop(L, 1);
2328 } 2401 }
2329 return aborted; 2402 return aborted;
2330 } 2403 }
2331 2404
2332 #endif 2405 void
2406 update_package_paths_in_lua()
2407 {
2408 if (lua_isopen())
2409 {
2410 lua_getglobal(L, "vim");
2411 lua_getfield(L, -1, "_update_package_paths");
2412
2413 if (lua_pcall(L, 0, 0, 0))
2414 luaV_emsg(L);
2415 }
2416 }
2417
2418 #endif