diff --git a/src/conky.cc b/src/conky.cc index 9b490d637..867a6484a 100644 --- a/src/conky.cc +++ b/src/conky.cc @@ -38,6 +38,7 @@ #include #include #include +#include #include #include #include @@ -275,7 +276,7 @@ int text_width = 1, struct information info; /* path to config file */ -std::string current_config; +std::filesystem::path current_config; /* set to 1 if you want all text to be in uppercase */ static conky::simple_config_setting stuff_in_uppercase("uppercase", false, @@ -1961,6 +1962,40 @@ void load_config_file() { lua::stack_sentry s(l); l.checkstack(2); + // Extend lua package.path so scripts can use relative paths + { + struct stat file_stat {}; + + std::string path_ext; + + // add XDG directory to lua path + auto xdg_path = + std::filesystem::path(to_real_path(XDG_CONFIG_FILE)).parent_path(); + if (stat(xdg_path.c_str(), &file_stat) == 0) { + path_ext.push_back(';'); + path_ext.append(xdg_path); + path_ext.append("/?.lua"); + } + + auto parent_path = current_config.parent_path(); + if (xdg_path != parent_path && stat(path_ext.c_str(), &file_stat) == 0) { + path_ext.push_back(';'); + path_ext.append(parent_path); + path_ext.append("/?.lua"); + } + + l.getglobal("package"); + l.getfield(-1, "path"); + + auto path = l.tostring(-1); + path.append(path_ext); + l.pop(); + l.pushstring(path.c_str()); + + l.setfield(-2, "path"); + l.pop(); + } + try { #ifdef BUILD_BUILTIN_CONFIG if (current_config == builtin_config_magic) { diff --git a/src/conky.h b/src/conky.h index c7a357248..efb8633f9 100644 --- a/src/conky.h +++ b/src/conky.h @@ -38,6 +38,7 @@ #include /* defines */ #include /* struct uname_s */ #include +#include #include #include "colours.h" @@ -346,7 +347,7 @@ extern conky::simple_config_setting utf8_mode; extern conky::range_config_setting max_user_text; /* path to config file */ -extern std::string current_config; +extern std::filesystem::path current_config; #define DEFAULT_TEXT_BUFFER_SIZE_S "##DEFAULT_TEXT_BUFFER_SIZE"