Added command to yank current url or selected text to clipboard.
authorDaniel Carl <danielcarl@gmx.de>
Sun, 23 Dec 2012 23:50:01 +0000 (00:50 +0100)
committerDaniel Carl <danielcarl@gmx.de>
Mon, 24 Dec 2012 01:53:00 +0000 (02:53 +0100)
doc/config
src/command.c
src/command.h
src/main.c
src/main.h

index 0c11ded..89dbe81 100644 (file)
@@ -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 <tab> complete
 cmap <shift-tab> complete-back
 hmap <tab> hint-focus-next
index 69a362c..247bb80 100644 (file)
@@ -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)
index ebefee2..3a6fa10 100644 (file)
 #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 */
index 5ea7371..811b664 100644 (file)
@@ -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, "");
index 1571af4..ee77d01 100644 (file)
@@ -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 */