diff --git a/src/file.cc b/src/file.cc index a8c20205..19f2a764 100644 --- a/src/file.cc +++ b/src/file.cc @@ -17,6 +17,10 @@ #include #include +#if defined(__APPLE__) +#include +#endif + namespace Kakoune { @@ -408,4 +412,24 @@ time_t get_fs_timestamp(StringView filename) return st.st_mtime; } +String get_kak_binary_path() +{ + char buffer[2048]; +#if defined(__linux__) or defined(__CYGWIN__) + ssize_t res = readlink("/proc/self/exe", buffer, 2048); + kak_assert(res != -1); + buffer[res] = '\0'; + return buffer; +#elif defined(__APPLE__) + uint32_t bufsize = 2048; + _NSGetExecutablePath(buffer, &bufsize); + char* canonical_path = realpath(buffer, nullptr); + String path = canonical_path; + free(canonical_path); + return path; +#else +# error "finding executable path is not implemented on this platform" +#endif +} + } diff --git a/src/file.hh b/src/file.hh index e3c03fba..99bb3715 100644 --- a/src/file.hh +++ b/src/file.hh @@ -29,6 +29,8 @@ String parse_filename(StringView filename); String real_path(StringView filename); String compact_path(StringView filename); +String get_kak_binary_path(); + String read_fd(int fd); String read_file(StringView filename); diff --git a/src/main.cc b/src/main.cc index 6e973411..4402d53c 100644 --- a/src/main.cc +++ b/src/main.cc @@ -24,10 +24,6 @@ #include "interned_string.hh" #include "window.hh" -#if defined(__APPLE__) -#include -#endif - #include #include #include @@ -43,24 +39,11 @@ void run_unit_tests(); String runtime_directory() { - char buffer[2048]; -#if defined(__linux__) or defined(__CYGWIN__) - ssize_t res = readlink("/proc/self/exe", buffer, 2048); - kak_assert(res != -1); - buffer[res] = '\0'; -#elif defined(__APPLE__) - uint32_t bufsize = 2048; - _NSGetExecutablePath(buffer, &bufsize); - char* canonical_path = realpath(buffer, nullptr); - strncpy(buffer, canonical_path, 2048); - free(canonical_path); -#else -# error "finding executable path is not implemented on this platform" -#endif - char* ptr = strrchr(buffer, '/'); - if (not ptr) + String bin_path = get_kak_binary_path(); + size_t last_slash = bin_path.find_last_of('/'); + if (last_slash == String::npos) throw runtime_error("unable to determine runtime directory"); - return String(buffer, ptr) + "/../share/kak"; + return bin_path.substr(0_byte, (int)last_slash) + "/../share/kak"; } void register_env_vars() diff --git a/src/shell_manager.cc b/src/shell_manager.cc index af2aa98d..8ac683ef 100644 --- a/src/shell_manager.cc +++ b/src/shell_manager.cc @@ -2,6 +2,7 @@ #include "context.hh" #include "debug.hh" +#include "file.hh" #include #include @@ -15,6 +16,15 @@ static const Regex env_var_regex(R"(\bkak_(\w+)\b)"); ShellManager::ShellManager() { + const char* path = getenv("PATH"); + String new_path; + if (path) + new_path = path + ":"_str; + + String kak_path = get_kak_binary_path(); + StringView kak_dir = kak_path.substr(0_byte, (int)kak_path.find_last_of('/')); + new_path += kak_dir; + setenv("PATH", new_path.c_str(), 1); } String ShellManager::eval(StringView cmdline, const Context& context,