diff --git a/doc/pages/expansions.asciidoc b/doc/pages/expansions.asciidoc index 14149324..2a61e7f9 100644 --- a/doc/pages/expansions.asciidoc +++ b/doc/pages/expansions.asciidoc @@ -80,7 +80,10 @@ informations about Kakoune's state: history id of the current buffer, the history id is an integer value which is used to reference a specific buffer version in the undo tree *kak_runtime*:: - directory containing the kak binary + directory containing the kak support files, determined from kakoune's + binary location. +*kak_config*:: + directory containing the user configuration *kak_count*:: count parameter passed to the command *kak_opt_*:: diff --git a/src/file.cc b/src/file.cc index d8595763..3dee5db5 100644 --- a/src/file.cc +++ b/src/file.cc @@ -10,13 +10,14 @@ #include "unicode.hh" #include -#include -#include -#include -#include -#include #include +#include +#include +#include +#include +#include #include +#include #if defined(__FreeBSD__) #include @@ -51,7 +52,7 @@ String parse_filename(StringView filename) { auto prefix = filename.substr(0_byte, 2_byte); if (prefix == "~" or prefix == "~/") - return getenv("HOME") + filename.substr(1_byte); + return homedir() + filename.substr(1_byte); return filename.str(); } @@ -105,10 +106,10 @@ String compact_path(StringView filename) if (prefix_match(real_filename, real_cwd)) return real_filename.substr(real_cwd.length()).str(); - const char* home = getenv("HOME"); - if (home) + const StringView home = homedir(); + if (not home.empty()) { - ByteCount home_len = (int)strlen(home); + ByteCount home_len = home.length(); if (real_filename.substr(0, home_len) == home) return "~" + real_filename.substr(home_len); } @@ -125,6 +126,14 @@ StringView tmpdir() return "/tmp"; } +StringView homedir() +{ + StringView home = getenv("HOME"); + if (home.empty()) + return getpwuid(geteuid())->pw_dir; + return home; +} + bool fd_readable(int fd) { fd_set rfds; @@ -331,7 +340,7 @@ String find_file(StringView filename, ConstArrayView paths) } if (filename.substr(0_byte, 2_byte) == "~/") { - String candidate = getenv("HOME") + filename.substr(1_byte); + String candidate = homedir() + filename.substr(1_byte); if (stat(candidate.c_str(), &buf) == 0 and S_ISREG(buf.st_mode)) return candidate; return ""; diff --git a/src/file.hh b/src/file.hh index 23fbe0a2..232ff539 100644 --- a/src/file.hh +++ b/src/file.hh @@ -25,6 +25,7 @@ String real_path(StringView filename); String compact_path(StringView filename); StringView tmpdir(); +StringView homedir(); // returns pair { directory, filename } std::pair split_path(StringView path); diff --git a/src/main.cc b/src/main.cc index aff7f9f2..c18af600 100644 --- a/src/main.cc +++ b/src/main.cc @@ -72,7 +72,7 @@ String config_directory() { StringView config_home = getenv("XDG_CONFIG_HOME"); if (config_home.empty()) - return format("{}/.config/kak", getenv("HOME")); + return format("{}/.config/kak", homedir()); return format("{}/kak", config_home); }