From 89c47d106c6ead05017244cec730d8735f545967 Mon Sep 17 00:00:00 2001 From: blankie Date: Mon, 25 Sep 2023 14:00:29 +1000 Subject: [PATCH] Add $DBUS variable for exec Fixes #375 --- doc/man/zathura.1.rst | 3 ++- doc/man/zathurarc.5.rst | 3 ++- zathura/commands.c | 7 +++++++ zathura/dbus-interface.c | 14 +++++++++++++- zathura/dbus-interface.h | 1 + zathura/shortcuts.c | 36 ++++++++++++++++++++++-------------- 6 files changed, 47 insertions(+), 17 deletions(-) diff --git a/doc/man/zathura.1.rst b/doc/man/zathura.1.rst index 912a29e..4550015 100644 --- a/doc/man/zathura.1.rst +++ b/doc/man/zathura.1.rst @@ -230,7 +230,8 @@ close exec Execute an external command. ``$FILE`` expands to the current document path, - and ``$PAGE`` to the current page number + ``$PAGE`` to the current page number, and ``$DBUS`` to the bus name of the + D-Bus interface info Show document information diff --git a/doc/man/zathurarc.5.rst b/doc/man/zathurarc.5.rst index e6a5480..55e85f2 100644 --- a/doc/man/zathurarc.5.rst +++ b/doc/man/zathurarc.5.rst @@ -232,7 +232,8 @@ They can also be combined with modifiers: * ``exec``: Execute an external command. ``$FILE`` expands to the current document path, - and ``$PAGE`` to the current page number. + ``$PAGE`` to the current page number, and ``$DBUS`` to the bus name of the + D-Bus interface. * ``focus_inputbar`` diff --git a/zathura/commands.c b/zathura/commands.c index 56a8d00..6cd5d07 100644 --- a/zathura/commands.c +++ b/zathura/commands.c @@ -9,6 +9,7 @@ #include "commands.h" #include "config.h" #include "database.h" +#include "dbus-interface.h" #include "document.h" #include "internal.h" #include "page-widget.h" @@ -544,6 +545,12 @@ cmd_exec(girara_session_t* session, girara_list_t* argument_list) return false; } + const char* bus_name = zathura_dbus_get_name(zathura); + GIRARA_LIST_FOREACH_BODY_WITH_ITER( + argument_list, char*, iter, value, char* s = girara_replace_substring(value, "$DBUS", bus_name); if (s != NULL) { + girara_list_iterator_set(iter, s); + }); + if (zathura->document != NULL) { const char* path = zathura_document_get_path(zathura->document); unsigned int page = zathura_document_get_current_page_number(zathura->document); diff --git a/zathura/dbus-interface.c b/zathura/dbus-interface.c index 79211e8..8446e3a 100644 --- a/zathura/dbus-interface.c +++ b/zathura/dbus-interface.c @@ -40,6 +40,7 @@ typedef struct private_s { GDBusConnection* connection; guint owner_id; guint registration_id; + char* bus_name; } ZathuraDbusPrivate; G_DEFINE_TYPE_WITH_CODE(ZathuraDbus, zathura_dbus, G_TYPE_OBJECT, G_ADD_PRIVATE(ZathuraDbus)) @@ -71,6 +72,8 @@ finalize(GObject* object) g_dbus_node_info_unref(priv->introspection_data); } + g_free(priv->bus_name); + G_OBJECT_CLASS(zathura_dbus_parent_class)->finalize(object); } @@ -91,6 +94,7 @@ zathura_dbus_init(ZathuraDbus* dbus) priv->connection = NULL; priv->owner_id = 0; priv->registration_id = 0; + priv->bus_name = NULL; } static void @@ -168,13 +172,21 @@ zathura_dbus_new(zathura_t* zathura) } char* well_known_name = g_strdup_printf(DBUS_NAME_TEMPLATE, getpid()); + priv->bus_name = well_known_name; priv->owner_id = g_bus_own_name(G_BUS_TYPE_SESSION, well_known_name, G_BUS_NAME_OWNER_FLAGS_NONE, bus_acquired, name_acquired, name_lost, dbus, NULL); - g_free(well_known_name); return dbus; } +const char* +zathura_dbus_get_name(zathura_t* zathura) +{ + ZathuraDbusPrivate* priv = zathura_dbus_get_instance_private(zathura->dbus); + + return priv->bus_name; +} + void zathura_dbus_edit(ZathuraDbus* edit, unsigned int page, unsigned int x, unsigned int y) { diff --git a/zathura/dbus-interface.h b/zathura/dbus-interface.h index 3e565c8..7d1df54 100644 --- a/zathura/dbus-interface.h +++ b/zathura/dbus-interface.h @@ -40,6 +40,7 @@ struct zathura_dbus_class_s GType zathura_dbus_get_type(void) G_GNUC_CONST; ZathuraDbus* zathura_dbus_new(zathura_t* zathura); +const char* zathura_dbus_get_name(zathura_t* zathura); /** * Emit the 'Edit' signal on the D-Bus connection. diff --git a/zathura/shortcuts.c b/zathura/shortcuts.c index 28208b0..2975db1 100644 --- a/zathura/shortcuts.c +++ b/zathura/shortcuts.c @@ -10,6 +10,7 @@ #include "callbacks.h" #include "shortcuts.h" +#include "dbus-interface.h" #include "document.h" #include "zathura.h" #include "render.h" @@ -1492,33 +1493,40 @@ sc_exec(girara_session_t* session, girara_argument_t* argument, girara_event_t* return false; } + girara_argument_t new_argument = *argument; + const char* bus_name = zathura_dbus_get_name(zathura); + char* s = girara_replace_substring(new_argument.data, "$DBUS", bus_name); + if (s == NULL) { + return false; + } + new_argument.data = s; + if (zathura->document != NULL) { const char* path = zathura_document_get_path(zathura->document); unsigned int page = zathura_document_get_current_page_number(zathura->document); char page_buf[G_ASCII_DTOSTR_BUF_SIZE]; g_ascii_dtostr(page_buf, G_ASCII_DTOSTR_BUF_SIZE, page+1); - girara_argument_t new_argument = *argument; - - char* r = girara_replace_substring(argument->data, "$FILE", path); - if (r == NULL) { - return false; - } - - char* s = girara_replace_substring(r, "$PAGE", page_buf); - g_free(r); + s = girara_replace_substring(new_argument.data, "$FILE", path); + g_free(new_argument.data); + + if (s == NULL) { + return false; + } + new_argument.data = s; + + s = girara_replace_substring(new_argument.data, "$PAGE", page_buf); + g_free(new_argument.data); if (s == NULL) { return false; } - new_argument.data = s; - const bool ret = girara_sc_exec(session, &new_argument, event, t); - g_free(s); - return ret; } - return girara_sc_exec(session, argument, event, t); + const bool ret = girara_sc_exec(session, &new_argument, event, t); + g_free(new_argument.data); + return ret; } bool