aboutsummaryrefslogtreecommitdiff
path: root/src/nvim/os/stdpaths.c
diff options
context:
space:
mode:
authorGhjuvan Lacambre <code@lacamb.re>2023-02-16 13:15:02 +0100
committerGitHub <noreply@github.com>2023-02-16 04:15:02 -0800
commitd34c64e342dfba9248d1055e702d02620a1b31a8 (patch)
treea685ef816f1621092ec82ea17d22fbd3a8c03957 /src/nvim/os/stdpaths.c
parent631775c05d257e5e61af31d20d9fd5be2dba82c2 (diff)
downloadrneovim-d34c64e342dfba9248d1055e702d02620a1b31a8.tar.gz
rneovim-d34c64e342dfba9248d1055e702d02620a1b31a8.tar.bz2
rneovim-d34c64e342dfba9248d1055e702d02620a1b31a8.zip
feat: $NVIM_APPNAME #22128
This commit implements the ability to control all of the XDG paths Neovim should use. This is done by setting an environment variable named NVIM_APPNAME. For example, setting $NVIM_APPNAME makes Neovim look for its configuration directory in $XDG_CONFIG_HOME/$NVIM_APPNAME instead of $XDG_CONFIG_HOME/nvim. If NVIM_APPNAME is not set or is an empty string, "nvim" will be used as default. The usecase for this feature is to enable an easy way to switch from configuration to configuration. One might argue that the various $XDG environment variables can already be used for this usecase. However, setting $XDG environment variables also affects tools spawned by Neovim. For example, while setting $XDG_CONFIG_HOME will enable Neovim to use a different configuration directory, it will also prevent Git from finding its "default" configuration. Closes https://github.com/neovim/neovim/issues/21691
Diffstat (limited to 'src/nvim/os/stdpaths.c')
-rw-r--r--src/nvim/os/stdpaths.c39
1 files changed, 27 insertions, 12 deletions
diff --git a/src/nvim/os/stdpaths.c b/src/nvim/os/stdpaths.c
index 6b07b6ef70..5235828f7a 100644
--- a/src/nvim/os/stdpaths.c
+++ b/src/nvim/os/stdpaths.c
@@ -57,6 +57,18 @@ static const char *const xdg_defaults[] = {
#endif
};
+/// Get the value of $NVIM_APPNAME or "nvim" if not set.
+///
+/// @return $NVIM_APPNAME value
+const char *get_appname(void)
+{
+ const char *env_val = os_getenv("NVIM_APPNAME");
+ if (env_val == NULL || *env_val == '\0') {
+ env_val = "nvim";
+ }
+ return env_val;
+}
+
/// Return XDG variable value
///
/// @param[in] idx XDG variable to use.
@@ -100,25 +112,28 @@ char *stdpaths_get_xdg_var(const XDGVarType idx)
/// Return Nvim-specific XDG directory subpath.
///
-/// Windows: Uses "…/nvim-data" for kXDGDataHome to avoid storing
+/// Windows: Uses "…/$NVIM_APPNAME-data" for kXDGDataHome to avoid storing
/// configuration and data files in the same path. #4403
///
/// @param[in] idx XDG directory to use.
///
-/// @return [allocated] "{xdg_directory}/nvim"
+/// @return [allocated] "{xdg_directory}/$NVIM_APPNAME"
char *get_xdg_home(const XDGVarType idx)
FUNC_ATTR_WARN_UNUSED_RESULT
{
char *dir = stdpaths_get_xdg_var(idx);
+ const char *appname = get_appname();
+ size_t appname_len = strlen(appname);
+ assert(appname_len < (IOSIZE - sizeof("-data")));
+
if (dir) {
+ xstrlcpy(IObuff, appname, appname_len + 1);
#if defined(MSWIN)
- dir = concat_fnames_realloc(dir,
- ((idx == kXDGDataHome
- || idx == kXDGStateHome) ? "nvim-data" : "nvim"),
- true);
-#else
- dir = concat_fnames_realloc(dir, "nvim", true);
+ if (idx == kXDGDataHome || idx == kXDGStateHome) {
+ STRCAT(IObuff, "-data");
+ }
#endif
+ dir = concat_fnames_realloc(dir, IObuff, true);
#ifdef BACKSLASH_IN_FILENAME
slash_adjust(dir);
@@ -131,7 +146,7 @@ char *get_xdg_home(const XDGVarType idx)
///
/// @param[in] fname New component of the path.
///
-/// @return [allocated] `$XDG_CACHE_HOME/nvim/{fname}`
+/// @return [allocated] `$XDG_CACHE_HOME/$NVIM_APPNAME/{fname}`
char *stdpaths_user_cache_subpath(const char *fname)
FUNC_ATTR_WARN_UNUSED_RESULT FUNC_ATTR_NONNULL_ALL FUNC_ATTR_NONNULL_RET
{
@@ -142,7 +157,7 @@ char *stdpaths_user_cache_subpath(const char *fname)
///
/// @param[in] fname New component of the path.
///
-/// @return [allocated] `$XDG_CONFIG_HOME/nvim/{fname}`
+/// @return [allocated] `$XDG_CONFIG_HOME/$NVIM_APPNAME/{fname}`
char *stdpaths_user_conf_subpath(const char *fname)
FUNC_ATTR_WARN_UNUSED_RESULT FUNC_ATTR_NONNULL_ALL FUNC_ATTR_NONNULL_RET
{
@@ -153,7 +168,7 @@ char *stdpaths_user_conf_subpath(const char *fname)
///
/// @param[in] fname New component of the path.
///
-/// @return [allocated] `$XDG_DATA_HOME/nvim/{fname}`
+/// @return [allocated] `$XDG_DATA_HOME/$NVIM_APPNAME/{fname}`
char *stdpaths_user_data_subpath(const char *fname)
FUNC_ATTR_WARN_UNUSED_RESULT FUNC_ATTR_NONNULL_ALL FUNC_ATTR_NONNULL_RET
{
@@ -166,7 +181,7 @@ char *stdpaths_user_data_subpath(const char *fname)
/// @param[in] trailing_pathseps Amount of trailing path separators to add.
/// @param[in] escape_commas If true, all commas will be escaped.
///
-/// @return [allocated] `$XDG_STATE_HOME/nvim/{fname}`.
+/// @return [allocated] `$XDG_STATE_HOME/$NVIM_APPNAME/{fname}`.
char *stdpaths_user_state_subpath(const char *fname, const size_t trailing_pathseps,
const bool escape_commas)
FUNC_ATTR_WARN_UNUSED_RESULT FUNC_ATTR_NONNULL_ALL FUNC_ATTR_NONNULL_RET