From 80c35e85bde4f114d22dc8ded2583ce52a402a79 Mon Sep 17 00:00:00 2001 From: Arun Prakash Jana Date: Sun, 9 Jun 2019 10:36:35 +0530 Subject: [PATCH] Support XDG_CONFIG_HOME --- scripts/quitcd/quitcd.bash | 2 +- scripts/quitcd/quitcd.csh | 1 + scripts/quitcd/quitcd.fish | 1 + scripts/quitcd/quitcd.zsh | 2 +- src/nnn.c | 54 +++++++++++++++++++++++++++++--------- 5 files changed, 46 insertions(+), 14 deletions(-) diff --git a/scripts/quitcd/quitcd.bash b/scripts/quitcd/quitcd.bash index 85b028ac..3f9f7fc1 100644 --- a/scripts/quitcd/quitcd.bash +++ b/scripts/quitcd/quitcd.bash @@ -2,7 +2,7 @@ n() { nnn "$@" - NNN_TMPFILE=~/.config/nnn/.lastd + NNN_TMPFILE=${XDG_CONFIG_HOME:-$HOME/.config}/nnn/.lastd if [ -f $NNN_TMPFILE ]; then . $NNN_TMPFILE diff --git a/scripts/quitcd/quitcd.csh b/scripts/quitcd/quitcd.csh index 62e98979..9ea952d2 100644 --- a/scripts/quitcd/quitcd.csh +++ b/scripts/quitcd/quitcd.csh @@ -1,2 +1,3 @@ +# NOTE: set NNN_TMPFILE correctly if you use 'XDG_CONFIG_HOME' set NNN_TMPFILE=~/.config/nnn/.lastd alias n 'nnn; source "$NNN_TMPFILE"; rm "$NNN_TMPFILE"' diff --git a/scripts/quitcd/quitcd.fish b/scripts/quitcd/quitcd.fish index 34780144..67433866 100644 --- a/scripts/quitcd/quitcd.fish +++ b/scripts/quitcd/quitcd.fish @@ -5,6 +5,7 @@ function n --description 'support nnn quit and change directory' nnn $argv + # NOTE: set NNN_TMPFILE correctly if you use 'XDG_CONFIG_HOME' set NNN_TMPFILE ~/.config/nnn/.lastd if test -e $NNN_TMPFILE diff --git a/scripts/quitcd/quitcd.zsh b/scripts/quitcd/quitcd.zsh index 79f5a030..24860ac3 100644 --- a/scripts/quitcd/quitcd.zsh +++ b/scripts/quitcd/quitcd.zsh @@ -2,7 +2,7 @@ n() { nnn "$@" - NNN_TMPFILE=~/.config/nnn/.lastd + NNN_TMPFILE=${XDG_CONFIG_HOME:-$HOME/.config}/nnn/.lastd if [ -f $NNN_TMPFILE ]; then . $NNN_TMPFILE diff --git a/src/nnn.c b/src/nnn.c index 161b7dba..869d5b73 100644 --- a/src/nnn.c +++ b/src/nnn.c @@ -4301,47 +4301,77 @@ static void usage(void) static bool setup_config(void) { size_t r, len; + char *xdgcfg = getenv("XDG_CONFIG_HOME"); + bool xdg = FALSE; /* Set up configuration file paths */ - len = strlen(home) + 1 + 20; /* add length of "/.config/nnn/plugins" */ + if (xdgcfg && xdgcfg[0]) { + DPRINTF_S(xdgcfg); + if (xdgcfg[0] == '~') { + r = xstrlcpy(g_buf, home, PATH_MAX); + xstrlcpy(g_buf + r - 1, xdgcfg + 1, PATH_MAX); + xdgcfg = g_buf; + DPRINTF_S(xdgcfg); + } + + if (!xdiraccess(xdgcfg)) { + xerror(); + return FALSE; + } + + len = strlen(xdgcfg) + 1 + 12; /* add length of "/nnn/plugins" */ + xdg = TRUE; + } + + if (!xdg) + len = strlen(home) + 1 + 20; /* add length of "/.config/nnn/plugins" */ + cfgdir = (char *)malloc(len); plugindir = (char *)malloc(len); if (!cfgdir || !plugindir) { xerror(); return FALSE; } - r = xstrlcpy(cfgdir, home, len); - /* Create ~/.config */ - xstrlcpy(cfgdir + r - 1, "/.config", len - r); - DPRINTF_S(cfgdir); - if (!create_dir(cfgdir)) { - xerror(); - return FALSE; + if (xdg) { + xstrlcpy(cfgdir, xdgcfg, len); + r = len - 12; + } else { + r = xstrlcpy(cfgdir, home, len); + + /* Create ~/.config */ + xstrlcpy(cfgdir + r - 1, "/.config", len - r); + DPRINTF_S(cfgdir); + if (!create_dir(cfgdir)) { + xerror(); + return FALSE; + } + + r += 8; /* length of "/.config" */ } /* Create ~/.config/nnn */ - xstrlcpy(cfgdir + r - 1, "/.config/nnn", len - r); + xstrlcpy(cfgdir + r - 1, "/nnn", len - r); DPRINTF_S(cfgdir); if (!create_dir(cfgdir)) { xerror(); return FALSE; } - xstrlcpy(cfgdir + r + 12 - 1, "/plugins", 9); + /* Create ~/.config/nnn/plugins */ + xstrlcpy(cfgdir + r + 4 - 1, "/plugins", 9); DPRINTF_S(cfgdir); xstrlcpy(plugindir, cfgdir, len); DPRINTF_S(plugindir); - /* Create ~/.config/nnn/plugins */ if (!create_dir(cfgdir)) { xerror(); return FALSE; } /* Reset to config path */ - cfgdir[r + 11] = '\0'; + cfgdir[r + 3] = '\0'; DPRINTF_S(cfgdir); /* Set selection file path */