Skip to content

Commit

Permalink
vis: fix default lua package.path and support $XDG_CONFIG_HOME
Browse files Browse the repository at this point in the history
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 d138908
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 14 deletions.
2 changes: 1 addition & 1 deletion lexers/README.md
Expand Up @@ -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)
Expand Down
35 changes: 22 additions & 13 deletions vis-lua.c
Expand Up @@ -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)
Expand All @@ -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");
Expand Down

0 comments on commit d138908

Please sign in to comment.