Honor clipboard setting when copying links

This commit is contained in:
Sebastian Ramacher 2021-01-17 15:58:07 +01:00
parent 26aeb76734
commit 5ffd3d031c
5 changed files with 20 additions and 37 deletions

View file

@ -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;
}
}
}
}

View file

@ -5,7 +5,6 @@
#include <girara/utils.h>
#include <girara/session.h>
#include <girara/settings.h>
#include <gtk/gtk.h>
#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;

View file

@ -5,6 +5,8 @@
#include "types.h"
#include <gtk/gtk.h>
/**
* 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

View file

@ -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);
}

View file

@ -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