Skip to content
Permalink
Browse files

vis: fix default lua package.path and support $XDG_CONFIG_HOME

The ordered list of paths for startup and lexer files is:

 - $VIS_PATH/{,lexers}
 - $XDG_CONFIG_HOME/vis/{,lexers} (defaulting to $HOME/.config/vis/{,lexers})
 - /usr/local/share/vis/{,lexers}
 - /usr/share/vis/{,lexers}
 - package.path (standard lua search path)
  • Loading branch information...
martanne committed Dec 29, 2015
1 parent b3ed399 commit d138908cf8149eb10120957271cd6979272b4730
Showing with 23 additions and 14 deletions.
  1. +1 −1 lexers/README.md
  2. +22 −13 vis-lua.c
@@ -9,7 +9,7 @@ based lexers from the [Scintillua](http://foicica.com/scintillua/) project.
Vis searches the lexers in the following locations:

* `$VIS_PATH/lexers`
* `$HOME/.vis/lexers`
* `$XDG_CONFIG_HOME/vis/lexers`
* `/usr/local/share/vis/lexers`
* `/usr/share/vis/lexers`
* `package.path` (standard lua search path)
@@ -359,17 +359,10 @@ void vis_lua_start(Vis *vis) {
vis->lua = L;
luaL_openlibs(L);

/* try to get users home directory */
const char *home = getenv("HOME");
if (!home || !*home) {
struct passwd *pw = getpwuid(getuid());
if (pw)
home = pw->pw_dir;
}

/* extends lua's package.path with:
* - $VIS_PATH/{,lexers}
* - $HOME/.vis{,lexers}
* - $XDG_CONFIG_HOME/vis/{,lexers} (defaulting to $HOME/.config/vis/{,lexers})
* - /usr/local/share/vis/{,lexers}
* - /usr/share/vis/{,lexers}
* - package.path (standard lua search path)
@@ -387,17 +380,33 @@ void vis_lua_start(Vis *vis) {
paths++;
}

if (home && *home) {
/* try to get users home directory */
const char *home = getenv("HOME");
if (!home || !*home) {
struct passwd *pw = getpwuid(getuid());
if (pw)
home = pw->pw_dir;
}

const char *xdg_config = getenv("XDG_CONFIG_HOME");
if (xdg_config) {
lua_pushstring(L, xdg_config);
lua_pushstring(L, "/vis/?.lua;");
lua_pushstring(L, xdg_config);
lua_pushstring(L, "/vis/lexers/?.lua;");
lua_concat(L, 4);
paths++;
} else if (home && *home) {
lua_pushstring(L, home);
lua_pushstring(L, "/.vis/?.lua;");
lua_pushstring(L, "/.config/vis/?.lua;");
lua_pushstring(L, home);
lua_pushstring(L, "/.vis/lexers/?.lua;");
lua_pushstring(L, "/.config/vis/lexers/?.lua;");
lua_concat(L, 4);
paths++;
}

lua_pushstring(L, "/usr/local/share/vis/?.lua;/usr/local/share/vis/lexers/?.lua");
lua_pushstring(L, "/usr/share/vis/?.lua;/usr/share/vis/lexers/?.lua");
lua_pushstring(L, "/usr/local/share/vis/?.lua;/usr/local/share/vis/lexers/?.lua;");
lua_pushstring(L, "/usr/share/vis/?.lua;/usr/share/vis/lexers/?.lua;");
lua_getfield(L, -paths, "path");
lua_concat(L, paths);
lua_setfield(L, -2, "path");

0 comments on commit d138908

Please sign in to comment.
You can’t perform that action at this time.