From 11e4d54f20dc7276a1f937a9c9a0afb9894eba1b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tin=20=C5=A0vagelj?= Date: Tue, 30 Apr 2024 19:24:32 +0200 Subject: [PATCH] Append script parent dir to lua package.path MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Tin Å vagelj --- src/conky.cc | 30 +++++++++++++++++++++++++++++- src/conky.h | 3 ++- 2 files changed, 31 insertions(+), 2 deletions(-) diff --git a/src/conky.cc b/src/conky.cc index 9b490d637..3daed97c6 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,33 @@ void load_config_file() { lua::stack_sentry s(l); l.checkstack(2); + // Extend lua package.path so scripts can use relative paths + { + std::string path_ext = ";"; + + // add XDG directory to lua path + auto xdg_path = std::filesystem::path(to_real_path(XDG_CONFIG_FILE)) + .replace_filename("?.lua"); + path_ext.append(xdg_path); + + auto parent_path = current_config.replace_filename("?.lua"); + if (xdg_path != parent_path) { + path_ext.push_back(';'); + path_ext.append(parent_path); + } + + 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"