From aafef5b99830c7ac5c30b0b10dda5cb33ccdacc1 Mon Sep 17 00:00:00 2001 From: Scott Jaderholm Date: Tue, 7 Feb 2012 00:43:22 -0500 Subject: [PATCH] copy command: universal-argument (C-u) means append text to clipboard --- modules/commands.js | 7 ++----- modules/element.js | 23 ++++++++++++++++++++++- modules/utils.js | 47 +++++++++++++++++++++++++++++------------------ 3 files changed, 53 insertions(+), 24 deletions(-) diff --git a/modules/commands.js b/modules/commands.js index 023f87b..2a853e4 100644 --- a/modules/commands.js +++ b/modules/commands.js @@ -489,11 +489,8 @@ interactive("save", interactive("copy", null, - function (I) { - var element = yield read_browser_object(I); - browser_element_copy(I.buffer, element); - }, - $browser_object = browser_object_links); + alternates(copy_text, copy_text_append), + $browser_object = browser_object_links); interactive("paste-url", "Open a URL from the clipboard in the current buffer.", alternates(follow_current_buffer, follow_new_buffer, follow_new_window), diff --git a/modules/element.js b/modules/element.js index 28d40ed..0c91e63 100644 --- a/modules/element.js +++ b/modules/element.js @@ -556,7 +556,7 @@ function element_get_operation_label (element, op_name, suffix) { } -function browser_element_copy (buffer, elem) { +function browser_element_text (buffer, elem) { try { var spec = load_spec(elem); } catch (e) {} @@ -582,11 +582,32 @@ function browser_element_copy (buffer, elem) { text = elem.textContent; } } + return text; +} + +function browser_element_copy (buffer, elem) { + var text = browser_element_text(buffer, elem); browser_set_element_focus(buffer, elem); writeToClipboard(text); buffer.window.minibuffer.message("Copied: " + text); } +define_variable("copy_append_separator", "\n", + "String used to separate old and new text when text is appended to clipboard"); + +function copy_text (I) { + var element = yield read_browser_object(I); + browser_element_copy(I.buffer, element); +} + +function copy_text_append (I) { + var element = yield read_browser_object(I); + var new_text = browser_element_text(I.buffer, element); + var text = read_from_clipboard() + copy_append_separator + new_text; + writeToClipboard(text); + I.buffer.window.minibuffer.message("Copied: ..." + new_text); +} + define_variable("view_source_use_external_editor", false, "When true, the `view-source' command will send its document to "+ diff --git a/modules/utils.js b/modules/utils.js index de6f0e4..2210fad 100644 --- a/modules/utils.js +++ b/modules/utils.js @@ -297,28 +297,25 @@ function create_info_panel (window, panel_class, row_arr) { } -/** - * Paste from the X primary selection, unless the system doesn't support a - * primary selection, in which case fall back to the clipboard. +/* + * Return clipboard contents as string. When which_clipboard is given, it + * may be an nsIClipboard constant specifying which clipboard to use. */ -function read_from_x_primary_selection () { - // Get clipboard. - let clipboard = Components.classes["@mozilla.org/widget/clipboard;1"] - .getService(Components.interfaces.nsIClipboard); - - // Fall back to global clipboard if the system doesn't support a selection - let which_clipboard = clipboard.supportsSelectionClipboard() ? - clipboard.kSelectionClipboard : clipboard.kGlobalClipboard; +function read_from_clipboard (which_clipboard) { + var clipboard = Cc["@mozilla.org/widget/clipboard;1"] + .getService(Ci.nsIClipboard); + if (which_clipboard == null) + which_clipboard = clipboard.kGlobalClipboard; - let flavors = ["text/unicode"]; + var flavors = ["text/unicode"]; // Don't barf if there's nothing on the clipboard if (!clipboard.hasDataMatchingFlavors(flavors, flavors.length, which_clipboard)) return ""; // Create transferable that will transfer the text. - let trans = Components.classes["@mozilla.org/widget/transferable;1"] - .createInstance(Components.interfaces.nsITransferable); + var trans = Cc["@mozilla.org/widget/transferable;1"] + .createInstance(Ci.nsITransferable); for each (let flavor in flavors) { trans.addDataFlavor(flavor); @@ -331,14 +328,28 @@ function read_from_x_primary_selection () { trans.getAnyTransferData(data_flavor, data, dataLen); if (data) { - data = data.value.QueryInterface(Components.interfaces.nsISupportsString); - let data_length = dataLen.value; + data = data.value.QueryInterface(Ci.nsISupportsString); + var data_length = dataLen.value; if (data_flavor.value == "text/unicode") data_length = dataLen.value / 2; return data.data.substring(0, data_length); - } else { + } else return ""; - } +} + + +/** + * Return selection clipboard contents as a string, or regular clipboard + * contents if the system does not support a selection clipboard. + */ +function read_from_x_primary_selection () { + var clipboard = Cc["@mozilla.org/widget/clipboard;1"] + .getService(Ci.nsIClipboard); + // fall back to global clipboard if the + // system doesn't support a selection + var which_clipboard = clipboard.supportsSelectionClipboard() ? + clipboard.kSelectionClipboard : clipboard.kGlobalClipboard; + return read_from_clipboard(which_clipboard); } -- 2.11.4.GIT