Skip to content

Commit

Permalink
Merge pull request #2386 from masatake/masatake--itchyny-support-xdg-…
Browse files Browse the repository at this point in the history
…config-home

<FOCUSING ON non-Windows> main: support XDG base directory specification (based on #2348, rearranged)
  • Loading branch information
masatake committed Dec 28, 2019
2 parents 1924e8a + 00d7422 commit 68da03a
Show file tree
Hide file tree
Showing 20 changed files with 87 additions and 12 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
7
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
--_echo=hello
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
--_echo=world
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
--_force-quit=7
13 changes: 13 additions & 0 deletions Tmain/load-conf-files-under-default-xdg-config-home/run.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# Copyright: 2019 itchyny
# License: GPL-2

CTAGS=$1

. ../utils.sh

exit_if_win32 "$CTAGS"

# $HOME/.config/ctags/*.ctags are loaded
export HOME=./myhome
export XDG_CONFIG_HOME=
${CTAGS}
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
ctags: Notice: hello
ctags: Notice: world
Empty file.
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
--_echo=hello
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
--_echo=world
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
--_force-quit=7
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
7
12 changes: 12 additions & 0 deletions Tmain/load-conf-files-under-xdg-config-home/run.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
# Copyright: 2019 itchyny
# License: GPL-2

CTAGS=$1

. ../utils.sh

exit_if_win32 "$CTAGS"

# $XDG_CONFIG_HOME/ctags/*.ctags are loaded
export XDG_CONFIG_HOME=./.config
${CTAGS}
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
ctags: Notice: hello
ctags: Notice: world
Empty file.
3 changes: 3 additions & 0 deletions docs/man/ctags.1.rst
Original file line number Diff line number Diff line change
Expand Up @@ -1842,6 +1842,9 @@ TMPDIR
FILES
-----

$XDG_CONFIG_HOME/ctags/\*.ctags, or $HOME/.config/ctags/\*.ctags if $XDG_CONFIG_HOME is not defeind
(on other than MSWindows)

$HOME/.ctags.d/\*.ctags

$HOMEDRIVE$HOMEPATH/ctags.d/\*.ctags (on MSWindows only)
Expand Down
1 change: 1 addition & 0 deletions docs/optlib.rst
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ from Exuberant-ctags.
At start-up time, Universal-ctags loads files having :file:`.ctags` as a
file extension under the following statically defined directories:

#. :file:`$XDG_CONFIG_HOME/ctags`, or :file:`$HOME/.config/ctags` if `$XDG_CONFIG_HOME` is not defined (on other than ``Windows``)
#. :file:`$HOME/.ctags.d`
#. :file:`$HOMEDRIVE$HOMEPATH/ctags.d` (in ``Windows``)
#. :file:`.ctags.d`
Expand Down
38 changes: 32 additions & 6 deletions main/options.c
Original file line number Diff line number Diff line change
Expand Up @@ -576,7 +576,8 @@ static const char *const StageDescription [] = {
[OptionLoadingStageDosCnf] = "DOS .cnf file",
[OptionLoadingStageEtc] = "file under /etc (e.g. ctags.conf)",
[OptionLoadingStageLocalEtc] = "file under /usr/local/etc (e.g. ctags.conf)",
[OptionLoadingStageHomeRecursive] = "file(s) under HOME",
[OptionLoadingStageXdg] = "file(s) under $XDG_CONFIG_HOME and $HOME/.config",
[OptionLoadingStageHomeRecursive] = "file(s) under $HOME",
[OptionLoadingStageCurrentRecursive] = "file(s) under the current directory",
[OptionLoadingStagePreload] = "optlib preload files",
[OptionLoadingStageEnvVar] = "environment variable",
Expand Down Expand Up @@ -3522,12 +3523,24 @@ static char* prependEnvvar (const char *path, const char* envvar)
char *full_path = NULL;

const char* const envval = getenv (envvar);
if (envval)
if (envval && strlen (envval))
full_path = combinePathAndFile(envval, path);

return full_path;
}

#ifndef WIN32
static char *getConfigForXDG (const char *path CTAGS_ATTR_UNUSED,
const char* extra CTAGS_ATTR_UNUSED)
{
char *r = prependEnvvar ("ctags", "XDG_CONFIG_HOME");
if (r)
return r;

return prependEnvvar (".config/ctags", "HOME");
}
#endif

#ifdef WIN32
static char *getConfigAtHomeOnWindows (const char *path,
const char* extra CTAGS_ATTR_UNUSED)
Expand All @@ -3544,9 +3557,11 @@ static char *getConfigAtHomeOnWindows (const char *path,
vStringCatS (windowsHome, homeDrive);
vStringCatS (windowsHome, homePath);

char *tmp = combinePathAndFile (vStringValue(windowsHome), path);
vStringDelete (windowsHome);
char *tmp = vStringIsEmpty (windowsHome)
? NULL
: combinePathAndFile (vStringValue(windowsHome), path);

vStringDelete (windowsHome);
return tmp;
}
return NULL;
Expand All @@ -3559,7 +3574,7 @@ static void preload (struct preloadPathElt *pathList)
stringList* loaded;

loaded = stringListNew ();
for (i = 0; pathList[i].path != NULL; ++i)
for (i = 0; pathList[i].path != NULL || pathList[i].makePath != NULL; ++i)
{
struct preloadPathElt *elt = pathList + i;
preloadMakePathFunc maker = elt->makePath;
Expand Down Expand Up @@ -3599,6 +3614,14 @@ static struct preloadPathElt preload_path_list [] = {
.makePath = NULL,
.stage = OptionLoadingStageCustom,
},
#endif
#ifndef WIN32
{
.path = NULL,
.isDirectory = true,
.makePath = getConfigForXDG,
.stage = OptionLoadingStageXdg,
},
#endif
{
.path = ".ctags.d",
Expand Down Expand Up @@ -3628,7 +3651,10 @@ static struct preloadPathElt preload_path_list [] = {
.makePath = NULL,
.stage = OptionLoadingStageCurrentRecursive,
},
{ .path = NULL },
{
.path = NULL,
.makePath = NULL,
},
};

static void parseConfigurationFileOptions (void)
Expand Down
1 change: 1 addition & 0 deletions main/options_p.h
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ typedef enum eOptionLoadingStage {
OptionLoadingStageDosCnf,
OptionLoadingStageEtc,
OptionLoadingStageLocalEtc,
OptionLoadingStageXdg,
OptionLoadingStageHomeRecursive,
OptionLoadingStageCurrentRecursive,
OptionLoadingStagePreload,
Expand Down
16 changes: 10 additions & 6 deletions main/routines.c
Original file line number Diff line number Diff line change
Expand Up @@ -714,14 +714,18 @@ extern char *combinePathAndFile (
const char *const path, const char *const file)
{
vString *const filePath = vStringNew ();
const int lastChar = path [strlen (path) - 1];
bool terminated = isPathSeparator (lastChar);
size_t len = strlen (path);

vStringCopyS (filePath, path);
if (! terminated)
vStringPut (filePath, OUTPUT_PATH_SEPARATOR);
vStringCatS (filePath, file);
if (len)
{
const int lastChar = path [len - 1];
bool terminated = isPathSeparator (lastChar);
vStringCopyS (filePath, path);
if (! terminated)
vStringPut (filePath, OUTPUT_PATH_SEPARATOR);
}

vStringCatS (filePath, file);
return vStringDeleteUnwrap (filePath);
}

Expand Down
3 changes: 3 additions & 0 deletions man/ctags.1.rst.in
Original file line number Diff line number Diff line change
Expand Up @@ -1842,6 +1842,9 @@ TMPDIR
FILES
-----

$XDG_CONFIG_HOME/ctags/\*.ctags, or $HOME/.config/ctags/\*.ctags if $XDG_CONFIG_HOME is not defeind
(on other than MSWindows)

$HOME/.ctags.d/\*.ctags

$HOMEDRIVE$HOMEPATH/ctags.d/\*.ctags (on MSWindows only)
Expand Down

0 comments on commit 68da03a

Please sign in to comment.