From 3e8abe7ee081dcb9e06bf0eb3ab2526b6a70621c Mon Sep 17 00:00:00 2001 From: Daniel Carl Date: Mon, 24 Dec 2012 00:50:01 +0100 Subject: [PATCH] Added command to yank current url or selected text to clipboard. --- doc/config | 2 ++ src/command.c | 34 ++++++++++++++++++++++++++++------ src/command.h | 8 ++++++++ src/main.c | 19 +++++++++++++++++++ src/main.h | 8 ++++++++ 5 files changed, 65 insertions(+), 6 deletions(-) diff --git a/doc/config b/doc/config index 0c11ded..89dbe81 100644 --- a/doc/config +++ b/doc/config @@ -27,6 +27,8 @@ nmap f hint-link nmap F hint-link-new nmap ;o hint-input-open nmap ;t hint-input-tabopen +nmap y yank-uri +nmap Y yank-selection cmap complete cmap complete-back hmap hint-focus-next diff --git a/src/command.c b/src/command.c index 69a362c..247bb80 100644 --- a/src/command.c +++ b/src/command.c @@ -70,6 +70,8 @@ static CommandInfo cmd_list[] = { {"hint-input-tabopen", command_hints, {HINTS_TYPE_LINK | HINTS_TARGET_BLANK | HINTS_PROCESS | HINTS_PROCESS_INPUT, ";t"}, VP_MODE_HINTING}, {"hint-focus-next", command_hints_focus, {0}, VP_MODE_HINTING}, {"hint-focus-prev", command_hints_focus, {1}, VP_MODE_HINTING}, + {"yank-uri", command_yank, {COMMAND_YANK_PRIMARY | COMMAND_YANK_SECONDARY | COMMAND_YANK_URI}, VP_MODE_NORMAL}, + {"yank-selection", command_yank, {COMMAND_YANK_PRIMARY | COMMAND_YANK_SECONDARY | COMMAND_YANK_SELECTION}, VP_MODE_NORMAL}, }; static void command_write_input(const gchar* str); @@ -289,14 +291,34 @@ gboolean command_hints_focus(const Arg* arg) gboolean command_yank(const Arg* arg) { - const gchar* uri = CURRENT_URL(); - if (!uri) { - return TRUE; - } + if (arg->i & COMMAND_YANK_SELECTION) { + gchar* text = NULL; + /* copy current selection to clipboard */ + webkit_web_view_copy_clipboard(vp.gui.webview); + text = gtk_clipboard_wait_for_text(PRIMARY_CLIPBOARD()); + if (!text) { + text = gtk_clipboard_wait_for_text(SECONDARY_CLIPBOARD()); + } + if (text) { + vp_echo(VP_MSG_NORMAL, FALSE, "Yanked: %s", text); + g_free(text); - vp_set_clipboard(uri, arg->i); + return TRUE; + } + } else { + /* use current arg.s a new clipboard content */ + Arg a = {arg->i, arg->s}; + if (arg->i & COMMAND_YANK_URI) { + /* yank current url */ + a.s = (gchar*)CURRENT_URL(); + } + if (vp_set_clipboard(&a)) { + vp_echo(VP_MSG_NORMAL, FALSE, "Yanked: %s", a.s); - return TRUE; + return TRUE; + } + } + return FALSE; } static void command_write_input(const gchar* str) diff --git a/src/command.h b/src/command.h index ebefee2..3a6fa10 100644 --- a/src/command.h +++ b/src/command.h @@ -20,6 +20,13 @@ #ifndef COMMAND_H #define COMMAND_H +enum { + COMMAND_YANK_PRIMARY = VP_CLIPBOARD_PRIMARY, + COMMAND_YANK_SECONDARY = VP_CLIPBOARD_SECONDARY, + COMMAND_YANK_URI = (COMMAND_YANK_SECONDARY<<1), + COMMAND_YANK_SELECTION = (COMMAND_YANK_SECONDARY<<2) +}; + typedef gboolean (*Command)(const Arg* arg); typedef struct { @@ -48,5 +55,6 @@ gboolean command_complete(const Arg* arg); gboolean command_inspect(const Arg* arg); gboolean command_hints(const Arg* arg); gboolean command_hints_focus(const Arg* arg); +gboolean command_yank(const Arg* arg); #endif /* end of include guard: COMMAND_H */ diff --git a/src/main.c b/src/main.c index 5ea7371..811b664 100644 --- a/src/main.c +++ b/src/main.c @@ -298,6 +298,25 @@ void vp_clean_input(void) vp_echo(VP_MSG_NORMAL, FALSE, ""); } +gboolean vp_set_clipboard(const Arg* arg) +{ + gboolean result = FALSE; + if (!arg->s) { + return result; + } + + if (arg->i & VP_CLIPBOARD_PRIMARY) { + gtk_clipboard_set_text(PRIMARY_CLIPBOARD(), arg->s, -1); + result = TRUE; + } + if (arg->i & VP_CLIPBOARD_SECONDARY) { + gtk_clipboard_set_text(SECONDARY_CLIPBOARD(), arg->s, -1); + result = TRUE; + } + + return result; +} + static gboolean vp_hide_message(void) { vp_echo(VP_MSG_NORMAL, FALSE, ""); diff --git a/src/main.h b/src/main.h index 1571af4..ee77d01 100644 --- a/src/main.h +++ b/src/main.h @@ -47,6 +47,8 @@ #define GET_CLEAN_MODE() (CLEAN_MODE(vp.state.mode)) #define CLEAR_INPUT() (vp_echo(VP_MSG_NORMAL, "")) #define CURRENT_URL() webkit_web_view_get_uri(vp.gui.webview) +#define PRIMARY_CLIPBOARD() gtk_clipboard_get(GDK_SELECTION_PRIMARY) +#define SECONDARY_CLIPBOARD() gtk_clipboard_get(GDK_NONE) #define IS_ESCAPE_KEY(k, s) ((k == GDK_Escape && s == 0) || (k == GDK_c && s == GDK_CONTROL_MASK)) #define CLEAN_STATE_WITH_SHIFT(e) ((e)->state & (GDK_MOD1_MASK|GDK_MOD4_MASK|GDK_SHIFT_MASK|GDK_CONTROL_MASK)) @@ -151,6 +153,11 @@ typedef enum { TYPE_COLOR, } Type; +enum { + VP_CLIPBOARD_PRIMARY = (1<<0), + VP_CLIPBOARD_SECONDARY = (1<<1) +}; + /* structs */ typedef struct { gint i; @@ -263,5 +270,6 @@ void vp_set_widget_font(GtkWidget* widget, const VpColor* fg, const VpColor* bg, gboolean vp_load_uri(const Arg* arg); void vp_clean_up(void); void vp_clean_input(void); +gboolean vp_set_clipboard(const Arg* arg); #endif /* end of include guard: MAIN_H */ -- 2.20.1