aboutsummaryrefslogtreecommitdiff
path: root/src/nvim/os/stdpaths.c
diff options
context:
space:
mode:
authorKeerthan Jaic <jckeerthan@gmail.com>2015-08-04 21:41:38 -0400
committerZyX <kp-pav@yandex.ru>2015-10-23 14:54:09 +0300
commitc2dd07448ff1b2922e8ea14f909a9cbde481e5fc (patch)
tree747efd028186695c226327a724fdc1fa72ed5dad /src/nvim/os/stdpaths.c
parente38cbb93670272d0da15c60222a123b88ec55002 (diff)
downloadrneovim-c2dd07448ff1b2922e8ea14f909a9cbde481e5fc.tar.gz
rneovim-c2dd07448ff1b2922e8ea14f909a9cbde481e5fc.tar.bz2
rneovim-c2dd07448ff1b2922e8ea14f909a9cbde481e5fc.zip
option,main: Partial support of XDG base directory specification
- Add functions that are able to query XDG. - Replace defaults for - &runtimepath. Does not follow #78. - &viewdir. - &undodir. - &directory. - &backupdir. Does not follow #78. - vimrc location. - Remove user vimrc file line from :version message.
Diffstat (limited to 'src/nvim/os/stdpaths.c')
-rw-r--r--src/nvim/os/stdpaths.c100
1 files changed, 100 insertions, 0 deletions
diff --git a/src/nvim/os/stdpaths.c b/src/nvim/os/stdpaths.c
new file mode 100644
index 0000000000..230e760d7d
--- /dev/null
+++ b/src/nvim/os/stdpaths.c
@@ -0,0 +1,100 @@
+#include "nvim/os/os.h"
+#include "nvim/strings.h"
+#include "nvim/path.h"
+#include "nvim/garray.h"
+
+typedef enum {
+ kXDGConfigHome,
+ kXDGDataHome,
+ kXDGCacheHome,
+ kXDGRuntimeDir,
+ kXDGConfigDirs,
+ kXDGDataDirs,
+} XDGDirType;
+
+static const char *xdg_env_vars[] = {
+ [kXDGConfigHome] = "XDG_CONFIG_HOME",
+ [kXDGDataHome] = "XDG_DATA_HOME",
+ [kXDGCacheHome] = "XDG_CACHE_HOME",
+ [kXDGRuntimeDir] = "XDG_RUNTIME_DIR",
+ [kXDGConfigDirs] = "XDG_CONFIG_DIRS",
+ [kXDGDataDirs] = "XDG_DATA_DIRS",
+};
+
+static const char *const xdg_defaults[] = {
+ // Windows, Apple stuff are just shims right now
+#ifdef WIN32
+ // Windows
+#elif APPLE
+ // Apple (this includes iOS, which we might need to handle differently)
+ [kXDGConfigHome] = "~/Library/Preferences",
+ [kXDGDataHome] = "~/Library/Application Support",
+ [kXDGCacheHome] = "~/Library/Caches",
+ [kXDGRuntimeDir] = "~/Library/Application Support",
+ [kXDGConfigDirs] = "/Library/Application Support",
+ [kXDGDataDirs] = "/Library/Application Support",
+#else
+ // Linux, BSD, CYGWIN
+ [kXDGConfigHome] = "~/.config",
+ [kXDGDataHome] = "~/.local/share",
+ [kXDGCacheHome] = "~/.cache",
+ [kXDGRuntimeDir] = "",
+ [kXDGConfigDirs] = "/etc/xdg/",
+ [kXDGDataDirs] = "/usr/local/share/:/usr/share/",
+};
+#endif
+
+static const char *get_xdg(XDGDirType idx)
+{
+ const char *env = xdg_env_vars[idx];
+ const char *fallback = xdg_defaults[idx];
+
+ const char *ret = os_getenv(env);
+ if (!ret && fallback) {
+ ret = (const char *)expand_env_save((char_u *)fallback);
+ }
+
+ return ret;
+}
+
+static const char *get_xdg_home(XDGDirType idx)
+{
+ const char *dir = get_xdg(idx);
+ if (dir) {
+ dir = (const char *)concat_fnames(dir, "nvim", true);
+ }
+ return dir;
+}
+
+static void create_dir(const char *dir, int mode, const char *suffix)
+{
+ char *failed;
+ if (!os_mkdir_recurse(dir, mode, &failed)) {
+ // TODO: Create a folder in $TMPDIR instead
+ DLOG("Create dir failed");
+ }
+}
+
+const char *get_user_conf_dir(void)
+{
+ return get_xdg_home(kXDGConfigHome);
+}
+
+const char *get_from_user_conf(const char * fname)
+{
+ return (const char *)concat_fnames(get_user_conf_dir(), fname, true);
+}
+
+const char *get_user_data_dir(void)
+{
+ return get_xdg_home(kXDGDataHome);
+}
+
+const char *get_from_user_data(const char * fname)
+{
+ const char *dir = (const char *)concat_fnames(get_user_data_dir(), fname, true);
+ if (!os_isdir((char_u *)dir)) {
+ create_dir(dir, 0755, fname);
+ }
+ return dir;
+}