From 5ffd3d031c9361755749521389909302bca92315 Mon Sep 17 00:00:00 2001 From: Sebastian Ramacher Date: Sun, 17 Jan 2021 15:58:07 +0100 Subject: [PATCH] Honor clipboard setting when copying links --- zathura/callbacks.c | 11 +++++++++-- zathura/links.c | 12 +++++++----- zathura/links.h | 5 ++++- zathura/utils.c | 15 --------------- zathura/utils.h | 14 -------------- 5 files changed, 20 insertions(+), 37 deletions(-) diff --git a/zathura/callbacks.c b/zathura/callbacks.c index 4b7047d..738834a 100644 --- a/zathura/callbacks.c +++ b/zathura/callbacks.c @@ -426,9 +426,16 @@ handle_link(GtkEntry* entry, girara_session_t* session, case ZATHURA_LINK_ACTION_DISPLAY: zathura_link_display(zathura, link); break; - case ZATHURA_LINK_ACTION_COPY: - zathura_link_copy(zathura, link); + case ZATHURA_LINK_ACTION_COPY: { + GdkAtom* selection = get_selection(zathura); + if (selection == NULL) { + break; + } + + zathura_link_copy(zathura, link, selection); + g_free(selection); break; + } } } } diff --git a/zathura/links.c b/zathura/links.c index 8181744..b547782 100644 --- a/zathura/links.c +++ b/zathura/links.c @@ -5,7 +5,6 @@ #include #include #include -#include #include "adjustment.h" #include "links.h" @@ -294,21 +293,24 @@ zathura_link_display(zathura_t* zathura, zathura_link_t* link) } void -zathura_link_copy(zathura_t* zathura, zathura_link_t* link) +zathura_link_copy(zathura_t* zathura, zathura_link_t* link, GdkAtom* selection) { zathura_link_type_t type = zathura_link_get_type(link); zathura_link_target_t target = zathura_link_get_target(link); switch (type) { - case ZATHURA_LINK_GOTO_DEST: - copy_int_to_clipboard(target.page_number); + case ZATHURA_LINK_GOTO_DEST: { + gchar* tmp = g_strdup_printf("%d", target.page_number); + gtk_clipboard_set_text(gtk_clipboard_get(*selection), tmp, -1); + g_free(tmp); girara_notify(zathura->ui.session, GIRARA_INFO, _("Copied page number: %d"), target.page_number); break; + } case ZATHURA_LINK_GOTO_REMOTE: case ZATHURA_LINK_URI: case ZATHURA_LINK_LAUNCH: case ZATHURA_LINK_NAMED: - copy_str_to_clipboard(target.value); + gtk_clipboard_set_text(gtk_clipboard_get(*selection), target.value, -1); girara_notify(zathura->ui.session, GIRARA_INFO, _("Copied link: %s"), target.value); break; diff --git a/zathura/links.h b/zathura/links.h index 1db52a0..38a9fc4 100644 --- a/zathura/links.h +++ b/zathura/links.h @@ -5,6 +5,8 @@ #include "types.h" +#include + /** * Creates a new zathura link * @@ -69,7 +71,8 @@ void zathura_link_display(zathura_t* zathura, zathura_link_t* link); * * @param zathura Zathura instance * @param link The link + * @param selection target clipboard */ -void zathura_link_copy(zathura_t* zathura, zathura_link_t* link); +void zathura_link_copy(zathura_t* zathura, zathura_link_t* link, GdkAtom* selection); #endif // LINK_H diff --git a/zathura/utils.c b/zathura/utils.c index e2a5872..b4b058f 100644 --- a/zathura/utils.c +++ b/zathura/utils.c @@ -460,18 +460,3 @@ flatten_rectangles(girara_list_t* rectangles) { girara_list_free(points); return new_rectangles; } - -void copy_str_to_clipboard(const char* text) { - GtkClipboard* clipboard = gtk_clipboard_get(GDK_SELECTION_CLIPBOARD); - gtk_clipboard_set_text(clipboard, text, -1); -} - -void copy_int_to_clipboard(int n) { - char* str; - const char fmt[] = "%d"; - const int len = 1 + snprintf(NULL, 0, fmt, n); - str = malloc(len); - snprintf(str, len, fmt, n); - copy_str_to_clipboard(str); - free(str); -} diff --git a/zathura/utils.h b/zathura/utils.h index 25c0d4d..e1da74b 100644 --- a/zathura/utils.h +++ b/zathura/utils.h @@ -145,18 +145,4 @@ bool running_under_wsl(void); */ girara_list_t* flatten_rectangles(girara_list_t* rectangles); -/** - * Copy text into the clipboard. - * - * @param str The string to be copied - */ -void copy_str_to_clipboard(const char* text); - -/** - * Copy integer as a string into the clipboard. - * - * @param n The integer to be copied - */ -void copy_int_to_clipboard(int n); - #endif // UTILS_H