on master help sign in
Support primary clipboard (#548)

* clipboard-none: add in-memory fallback buffer Signed-off-by: Dmitry Sharshakov <d3dx12.xx@gmail.com> * view: add Wayland primary clipboard Signed-off-by: Dmitry Sharshakov <d3dx12.xx@gmail.com> * Format Signed-off-by: Dmitry Sharshakov <d3dx12.xx@gmail.com> * helix-term: copy to primary selection after mouse move stops Signed-off-by: Dmitry Sharshakov <d3dx12.xx@gmail.com> * helix-term: don't update primary selection if it is a single character Signed-off-by: Dmitry Sharshakov <d3dx12.xx@gmail.com> * helix-term: discard result of setting primary selection Signed-off-by: Dmitry Sharshakov <d3dx12.xx@gmail.com> * helix-term: add commands for interaction with primary clipboard Signed-off-by: Dmitry Sharshakov <d3dx12.xx@gmail.com> * editor: implement primary selection copy/paste using commands Signed-off-by: Dmitry Sharshakov <d3dx12.xx@gmail.com> * clipboard: support xsel for primary selection Signed-off-by: Dmitry Sharshakov <d3dx12.xx@gmail.com> * clipboard: support xclip for primary selection Signed-off-by: Dmitry Sharshakov <d3dx12.xx@gmail.com> * helix-term: multiple cursor support for middle click paste Signed-off-by: Dmitry Sharshakov <d3dx12.xx@gmail.com> * rename primary selection to primary clipboard in scope of PR Signed-off-by: Dmitry Sharshakov <d3dx12.xx@gmail.com> * helix-term: make middle click paste optional Signed-off-by: Dmitry Sharshakov <d3dx12.xx@gmail.com> * Format Signed-off-by: Dmitry Sharshakov <d3dx12.xx@gmail.com> * Update helix-term/src/ui/editor.rs * fix formatting Signed-off-by: Dmitry Sharshakov <d3dx12.xx@gmail.com> * config: correct defaults if terminal prop is not set Signed-off-by: Dmitry Sharshakov <d3dx12.xx@gmail.com> * refactor: merge clipboard and primary selection implementations Signed-off-by: Dmitry Sharshakov <d3dx12.xx@gmail.com> * Tidy up code Signed-off-by: Dmitry Sharshakov <d3dx12.xx@gmail.com> * view: remove names for different clipboard/selection providers Signed-off-by: Dmitry Sharshakov <d3dx12.xx@gmail.com> * Update helix-view/src/clipboard.rs Co-authored-by: Gokul Soumya <gokulps15@gmail.com> * helix-view: tidy macros Signed-off-by: Dmitry Sharshakov <d3dx12.xx@gmail.com> * helix-term: refactor paste-replace commands Signed-off-by: Dmitry Sharshakov <d3dx12.xx@gmail.com> * helix-term: use new config for middle-click-paste Signed-off-by: Dmitry Sharshakov <d3dx12.xx@gmail.com> * clipboard: remove memory fallback for command and windows providers Signed-off-by: Dmitry Sharshakov <d3dx12.xx@gmail.com> * clipboard-win: fix build Signed-off-by: Dmitry Sharshakov <d3dx12.xx@gmail.com> * clipboard: return empty string when primary clipboard is missing Signed-off-by: Dmitry Sharshakov <d3dx12.xx@gmail.com> * clipboard: fix errors in Windows build Signed-off-by: Dmitry Sharshakov <d3dx12.xx@gmail.com> Co-authored-by: Gokul Soumya <gokulps15@gmail.com>

commit7d51805e94a461834ce34e0829da5859d1f9db32 authorDmitry Sharshakov <d3dx12.xx@gmail.com> dateThu 12 Aug 2021, 05:53 (5 years ago) parentd03982e
4 files changed+341−45
helix-term/src/commands.rs+160−22 helix-term/src/ui/editor.rs+55 helix-view/src/clipboard.rs+123−23 helix-view/src/editor.rs+3
helix-term/src/commands.rs+160−22
@@ -12,8 +12,8 @@ use helix_core::{
1212};
1313
1414use helix_view::{
15− document::Mode, editor::Action, input::KeyEvent, keyboard::KeyCode, view::View, Document,
16− DocumentId, Editor, ViewId,
15+ clipboard::ClipboardType, document::Mode, editor::Action, input::KeyEvent, keyboard::KeyCode,
16+ view::View, Document, DocumentId, Editor, ViewId,
1717};
1818
1919use anyhow::{anyhow, bail, Context as _};
@@ -258,12 +258,17 @@ impl Command {
258258 yank, "Yank selection",
259259 yank_joined_to_clipboard, "Join and yank selections to clipboard",
260260 yank_main_selection_to_clipboard, "Yank main selection to clipboard",
261+ yank_joined_to_primary_clipboard, "Join and yank selections to primary clipboard",
262+ yank_main_selection_to_primary_clipboard, "Yank main selection to primary clipboard",
261263 replace_with_yanked, "Replace with yanked text",
262264 replace_selections_with_clipboard, "Replace selections by clipboard content",
265+ replace_selections_with_primary_clipboard, "Replace selections by primary clipboard content",
263266 paste_after, "Paste after selection",
264267 paste_before, "Paste before selection",
265268 paste_clipboard_after, "Paste clipboard after selections",
266269 paste_clipboard_before, "Paste clipboard before selections",
270+ paste_primary_clipboard_after, "Paste primary clipboard after selections",
271+ paste_primary_clipboard_before, "Paste primary clipboard before selections",
267272 indent, "Indent selection",
268273 unindent, "Unindent selection",
269274 format_selections, "Format selection",
@@ -1705,7 +1710,7 @@ mod cmd {
17051710 _args: &[&str],
17061711 _event: PromptEvent,
17071712 ) -> anyhow::Result<()> {
1708− yank_main_selection_to_clipboard_impl(&mut cx.editor)
1713+ yank_main_selection_to_clipboard_impl(&mut cx.editor, ClipboardType::Clipboard)
17091714 }
17101715
17111716 fn yank_joined_to_clipboard(
@@ -1718,7 +1723,28 @@ mod cmd {
17181723 .first()
17191724 .copied()
17201725 .unwrap_or_else(|| doc.line_ending.as_str());
1721− yank_joined_to_clipboard_impl(&mut cx.editor, separator)
1726+ yank_joined_to_clipboard_impl(&mut cx.editor, separator, ClipboardType::Clipboard)
1727+ }
1728+
1729+ fn yank_main_selection_to_primary_clipboard(
1730+ cx: &mut compositor::Context,
1731+ _args: &[&str],
1732+ _event: PromptEvent,
1733+ ) -> anyhow::Result<()> {
1734+ yank_main_selection_to_clipboard_impl(&mut cx.editor, ClipboardType::Selection)
1735+ }
1736+
1737+ fn yank_joined_to_primary_clipboard(
1738+ cx: &mut compositor::Context,
1739+ args: &[&str],
1740+ _event: PromptEvent,
1741+ ) -> anyhow::Result<()> {
1742+ let (_, doc) = current!(cx.editor);
1743+ let separator = args
1744+ .first()
1745+ .copied()
1746+ .unwrap_or_else(|| doc.line_ending.as_str());
1747+ yank_joined_to_clipboard_impl(&mut cx.editor, separator, ClipboardType::Selection)
17221748 }
17231749
17241750 fn paste_clipboard_after(
@@ -1726,7 +1752,7 @@ mod cmd {
17261752 _args: &[&str],
17271753 _event: PromptEvent,
17281754 ) -> anyhow::Result<()> {
1729− paste_clipboard_impl(&mut cx.editor, Paste::After)
1755+ paste_clipboard_impl(&mut cx.editor, Paste::After, ClipboardType::Clipboard)
17301756 }
17311757
17321758 fn paste_clipboard_before(
@@ -1734,17 +1760,32 @@ mod cmd {
17341760 _args: &[&str],
17351761 _event: PromptEvent,
17361762 ) -> anyhow::Result<()> {
1737− paste_clipboard_impl(&mut cx.editor, Paste::After)
1763+ paste_clipboard_impl(&mut cx.editor, Paste::After, ClipboardType::Clipboard)
17381764 }
17391765
1740− fn replace_selections_with_clipboard(
1766+ fn paste_primary_clipboard_after(
17411767 cx: &mut compositor::Context,
17421768 _args: &[&str],
17431769 _event: PromptEvent,
1770+ ) -> anyhow::Result<()> {
1771+ paste_clipboard_impl(&mut cx.editor, Paste::After, ClipboardType::Selection)
1772+ }
1773+
1774+ fn paste_primary_clipboard_before(
1775+ cx: &mut compositor::Context,
1776+ _args: &[&str],
1777+ _event: PromptEvent,
1778+ ) -> anyhow::Result<()> {
1779+ paste_clipboard_impl(&mut cx.editor, Paste::After, ClipboardType::Selection)
1780+ }
1781+
1782+ fn replace_selections_with_clipboard_impl(
1783+ cx: &mut compositor::Context,
1784+ clipboard_type: ClipboardType,
17441785 ) -> anyhow::Result<()> {
17451786 let (view, doc) = current!(cx.editor);
17461787
1747− match cx.editor.clipboard_provider.get_contents() {
1788+ match cx.editor.clipboard_provider.get_contents(clipboard_type) {
17481789 Ok(contents) => {
17491790 let selection = doc.selection(view.id);
17501791 let transaction =
@@ -1760,13 +1801,29 @@ mod cmd {
17601801 }
17611802 }
17621803
1804+ fn replace_selections_with_clipboard(
1805+ cx: &mut compositor::Context,
1806+ _args: &[&str],
1807+ _event: PromptEvent,
1808+ ) -> anyhow::Result<()> {
1809+ replace_selections_with_clipboard_impl(cx, ClipboardType::Clipboard)
1810+ }
1811+
1812+ fn replace_selections_with_primary_clipboard(
1813+ cx: &mut compositor::Context,
1814+ _args: &[&str],
1815+ _event: PromptEvent,
1816+ ) -> anyhow::Result<()> {
1817+ replace_selections_with_clipboard_impl(cx, ClipboardType::Selection)
1818+ }
1819+
17631820 fn show_clipboard_provider(
17641821 cx: &mut compositor::Context,
17651822 _args: &[&str],
17661823 _event: PromptEvent,
17671824 ) -> anyhow::Result<()> {
17681825 cx.editor
1769− .set_status(cx.editor.clipboard_provider.name().into());
1826+ .set_status(cx.editor.clipboard_provider.name().to_string());
17701827 Ok(())
17711828 }
17721829
@@ -1967,6 +2024,20 @@ mod cmd {
19672024 fun: yank_joined_to_clipboard,
19682025 completer: None,
19692026 },
2027+ TypableCommand {
2028+ name: "primary-clipboard-yank",
2029+ alias: None,
2030+ doc: "Yank main selection into system primary clipboard.",
2031+ fun: yank_main_selection_to_primary_clipboard,
2032+ completer: None,
2033+ },
2034+ TypableCommand {
2035+ name: "primary-clipboard-yank-join",
2036+ alias: None,
2037+ doc: "Yank joined selections into system primary clipboard. A separator can be provided as first argument. Default value is newline.", // FIXME: current UI can't display long doc.
2038+ fun: yank_joined_to_primary_clipboard,
2039+ completer: None,
2040+ },
19702041 TypableCommand {
19712042 name: "clipboard-paste-after",
19722043 alias: None,
@@ -1988,6 +2059,27 @@ mod cmd {
19882059 fun: replace_selections_with_clipboard,
19892060 completer: None,
19902061 },
2062+ TypableCommand {
2063+ name: "primary-clipboard-paste-after",
2064+ alias: None,
2065+ doc: "Paste primary clipboard after selections.",
2066+ fun: paste_primary_clipboard_after,
2067+ completer: None,
2068+ },
2069+ TypableCommand {
2070+ name: "primary-clipboard-paste-before",
2071+ alias: None,
2072+ doc: "Paste primary clipboard before selections.",
2073+ fun: paste_primary_clipboard_before,
2074+ completer: None,
2075+ },
2076+ TypableCommand {
2077+ name: "primary-clipboard-paste-replace",
2078+ alias: None,
2079+ doc: "Replace selections with content of system primary clipboard.",
2080+ fun: replace_selections_with_primary_clipboard,
2081+ completer: None,
2082+ },
19912083 TypableCommand {
19922084 name: "show-clipboard-provider",
19932085 alias: None,
@@ -3209,7 +3301,11 @@ fn yank(cx: &mut Context) {
32093301 exit_select_mode(cx);
32103302}
32113303
3212−fn yank_joined_to_clipboard_impl(editor: &mut Editor, separator: &str) -> anyhow::Result<()> {
3304+fn yank_joined_to_clipboard_impl(
3305+ editor: &mut Editor,
3306+ separator: &str,
3307+ clipboard_type: ClipboardType,
3308+) -> anyhow::Result<()> {
32133309 let (view, doc) = current!(editor);
32143310 let text = doc.text().slice(..);
32153311
@@ -3228,7 +3324,7 @@ fn yank_joined_to_clipboard_impl(editor: &mut Editor, separator: &str) -> anyhow
32283324
32293325 editor
32303326 .clipboard_provider
3231− .set_contents(joined)
3327+ .set_contents(joined, clipboard_type)
32323328 .context("Couldn't set system clipboard content")?;
32333329
32343330 editor.set_status(msg);
@@ -3238,17 +3334,27 @@ fn yank_joined_to_clipboard_impl(editor: &mut Editor, separator: &str) -> anyhow
32383334
32393335fn yank_joined_to_clipboard(cx: &mut Context) {
32403336 let line_ending = current!(cx.editor).1.line_ending;
3241− let _ = yank_joined_to_clipboard_impl(&mut cx.editor, line_ending.as_str());
3337+ let _ = yank_joined_to_clipboard_impl(
3338+ &mut cx.editor,
3339+ line_ending.as_str(),
3340+ ClipboardType::Clipboard,
3341+ );
32423342 exit_select_mode(cx);
32433343}
32443344
3245−fn yank_main_selection_to_clipboard_impl(editor: &mut Editor) -> anyhow::Result<()> {
3345+fn yank_main_selection_to_clipboard_impl(
3346+ editor: &mut Editor,
3347+ clipboard_type: ClipboardType,
3348+) -> anyhow::Result<()> {
32463349 let (view, doc) = current!(editor);
32473350 let text = doc.text().slice(..);
32483351
32493352 let value = doc.selection(view.id).primary().fragment(text);
32503353
3251− if let Err(e) = editor.clipboard_provider.set_contents(value.into_owned()) {
3354+ if let Err(e) = editor
3355+ .clipboard_provider
3356+ .set_contents(value.into_owned(), clipboard_type)
3357+ {
32523358 bail!("Couldn't set system clipboard content: {:?}", e);
32533359 }
32543360
@@ -3257,7 +3363,20 @@ fn yank_main_selection_to_clipboard_impl(editor: &mut Editor) -> anyhow::Result<
32573363}
32583364
32593365fn yank_main_selection_to_clipboard(cx: &mut Context) {
3260− let _ = yank_main_selection_to_clipboard_impl(&mut cx.editor);
3366+ let _ = yank_main_selection_to_clipboard_impl(&mut cx.editor, ClipboardType::Clipboard);
3367+}
3368+
3369+fn yank_joined_to_primary_clipboard(cx: &mut Context) {
3370+ let line_ending = current!(cx.editor).1.line_ending;
3371+ let _ = yank_joined_to_clipboard_impl(
3372+ &mut cx.editor,
3373+ line_ending.as_str(),
3374+ ClipboardType::Selection,
3375+ );
3376+}
3377+
3378+fn yank_main_selection_to_primary_clipboard(cx: &mut Context) {
3379+ let _ = yank_main_selection_to_clipboard_impl(&mut cx.editor, ClipboardType::Selection);
32613380 exit_select_mode(cx);
32623381}
32633382
@@ -3310,12 +3429,16 @@ fn paste_impl(
33103429 Some(transaction)
33113430}
33123431
3313−fn paste_clipboard_impl(editor: &mut Editor, action: Paste) -> anyhow::Result<()> {
3432+fn paste_clipboard_impl(
3433+ editor: &mut Editor,
3434+ action: Paste,
3435+ clipboard_type: ClipboardType,
3436+) -> anyhow::Result<()> {
33143437 let (view, doc) = current!(editor);
33153438
33163439 match editor
33173440 .clipboard_provider
3318− .get_contents()
3441+ .get_contents(clipboard_type)
33193442 .map(|contents| paste_impl(&[contents], doc, view, action))
33203443 {
33213444 Ok(Some(transaction)) => {
@@ -3329,11 +3452,19 @@ fn paste_clipboard_impl(editor: &mut Editor, action: Paste) -> anyhow::Result<()
33293452}
33303453
33313454fn paste_clipboard_after(cx: &mut Context) {
3332− let _ = paste_clipboard_impl(&mut cx.editor, Paste::After);
3455+ let _ = paste_clipboard_impl(&mut cx.editor, Paste::After, ClipboardType::Clipboard);
33333456}
33343457
33353458fn paste_clipboard_before(cx: &mut Context) {
3336− let _ = paste_clipboard_impl(&mut cx.editor, Paste::Before);
3459+ let _ = paste_clipboard_impl(&mut cx.editor, Paste::Before, ClipboardType::Clipboard);
3460+}
3461+
3462+fn paste_primary_clipboard_after(cx: &mut Context) {
3463+ let _ = paste_clipboard_impl(&mut cx.editor, Paste::After, ClipboardType::Selection);
3464+}
3465+
3466+fn paste_primary_clipboard_before(cx: &mut Context) {
3467+ let _ = paste_clipboard_impl(&mut cx.editor, Paste::Before, ClipboardType::Selection);
33373468}
33383469
33393470fn replace_with_yanked(cx: &mut Context) {
@@ -3358,10 +3489,13 @@ fn replace_with_yanked(cx: &mut Context) {
33583489 }
33593490}
33603491
3361−fn replace_selections_with_clipboard_impl(editor: &mut Editor) -> anyhow::Result<()> {
3492+fn replace_selections_with_clipboard_impl(
3493+ editor: &mut Editor,
3494+ clipboard_type: ClipboardType,
3495+) -> anyhow::Result<()> {
33623496 let (view, doc) = current!(editor);
33633497
3364− match editor.clipboard_provider.get_contents() {
3498+ match editor.clipboard_provider.get_contents(clipboard_type) {
33653499 Ok(contents) => {
33663500 let selection = doc.selection(view.id);
33673501 let transaction = Transaction::change_by_selection(doc.text(), selection, |range| {
@@ -3377,7 +3511,11 @@ fn replace_selections_with_clipboard_impl(editor: &mut Editor) -> anyhow::Result
33773511}
33783512
33793513fn replace_selections_with_clipboard(cx: &mut Context) {
3380− let _ = replace_selections_with_clipboard_impl(&mut cx.editor);
3514+ let _ = replace_selections_with_clipboard_impl(&mut cx.editor, ClipboardType::Clipboard);
3515+}
3516+
3517+fn replace_selections_with_primary_clipboard(cx: &mut Context) {
3518+ let _ = replace_selections_with_clipboard_impl(&mut cx.editor, ClipboardType::Selection);
33813519}
33823520
33833521fn paste_after(cx: &mut Context) {
helix-term/src/ui/editor.rs+55
@@ -785,6 +785,61 @@ impl EditorView {
785785
786786 EventResult::Consumed(None)
787787 }
788+
789+ MouseEvent {
790+ kind: MouseEventKind::Up(MouseButton::Left),
791+ ..
792+ } => {
793+ if !cxt.editor.config.middle_click_paste {
794+ return EventResult::Ignored;
795+ }
796+
797+ let (view, doc) = current!(cxt.editor);
798+ let range = doc.selection(view.id).primary();
799+
800+ if range.to() - range.from() <= 1 {
801+ return EventResult::Ignored;
802+ }
803+
804+ commands::Command::yank_main_selection_to_primary_clipboard.execute(cxt);
805+
806+ EventResult::Consumed(None)
807+ }
808+
809+ MouseEvent {
810+ kind: MouseEventKind::Up(MouseButton::Middle),
811+ row,
812+ column,
813+ modifiers,
814+ ..
815+ } => {
816+ let editor = &mut cxt.editor;
817+ if !editor.config.middle_click_paste {
818+ return EventResult::Ignored;
819+ }
820+
821+ if modifiers == crossterm::event::KeyModifiers::ALT {
822+ commands::Command::replace_selections_with_primary_clipboard.execute(cxt);
823+
824+ return EventResult::Consumed(None);
825+ }
826+
827+ let result = editor.tree.views().find_map(|(view, _focus)| {
828+ view.pos_at_screen_coords(&editor.documents[view.doc], row, column)
829+ .map(|pos| (pos, view.id))
830+ });
831+
832+ if let Some((pos, view_id)) = result {
833+ let doc = &mut editor.documents[editor.tree.get(view_id).doc];
834+ doc.set_selection(view_id, Selection::point(pos));
835+ editor.tree.focus = view_id;
836+ commands::Command::paste_primary_clipboard_before.execute(cxt);
837+ return EventResult::Consumed(None);
838+ }
839+
840+ EventResult::Ignored
841+ }
842+
788843 _ => EventResult::Ignored,
789844 }
790845 }
helix-view/src/clipboard.rs+123−23
@@ -3,10 +3,15 @@
33use anyhow::Result;
44use std::borrow::Cow;
55
6+pub enum ClipboardType {
7+ Clipboard,
8+ Selection,
9+}
10+
611pub trait ClipboardProvider: std::fmt::Debug {
712 fn name(&self) -> Cow<str>;
8− fn get_contents(&self) -> Result<String>;
9− fn set_contents(&self, contents: String) -> Result<()>;
13+ fn get_contents(&self, clipboard_type: ClipboardType) -> Result<String>;
14+ fn set_contents(&mut self, contents: String, clipboard_type: ClipboardType) -> Result<()>;
1015}
1116
1217macro_rules! command_provider {
@@ -20,6 +25,33 @@ macro_rules! command_provider {
2025 prg: $set_prg,
2126 args: &[ $( $set_arg ),* ],
2227 },
28+ get_primary_cmd: None,
29+ set_primary_cmd: None,
30+ })
31+ }};
32+
33+ (paste => $get_prg:literal $( , $get_arg:literal )* ;
34+ copy => $set_prg:literal $( , $set_arg:literal )* ;
35+ primary_paste => $pr_get_prg:literal $( , $pr_get_arg:literal )* ;
36+ primary_copy => $pr_set_prg:literal $( , $pr_set_arg:literal )* ;
37+ ) => {{
38+ Box::new(provider::CommandProvider {
39+ get_cmd: provider::CommandConfig {
40+ prg: $get_prg,
41+ args: &[ $( $get_arg ),* ],
42+ },
43+ set_cmd: provider::CommandConfig {
44+ prg: $set_prg,
45+ args: &[ $( $set_arg ),* ],
46+ },
47+ get_primary_cmd: Some(provider::CommandConfig {
48+ prg: $pr_get_prg,
49+ args: &[ $( $pr_get_arg ),* ],
50+ }),
51+ set_primary_cmd: Some(provider::CommandConfig {
52+ prg: $pr_set_prg,
53+ args: &[ $( $pr_set_arg ),* ],
54+ }),
2355 })
2456 }};
2557}
@@ -37,11 +69,15 @@ pub fn get_clipboard_provider() -> Box<dyn ClipboardProvider> {
3769 command_provider! {
3870 paste => "wl-paste", "--no-newline";
3971 copy => "wl-copy", "--type", "text/plain";
72+ primary_paste => "wl-paste", "-p", "--no-newline";
73+ primary_copy => "wl-copy", "-p", "--type", "text/plain";
4074 }
4175 } else if env_var_is_set("DISPLAY") && exists("xclip") {
4276 command_provider! {
4377 paste => "xclip", "-o", "-selection", "clipboard";
4478 copy => "xclip", "-i", "-selection", "clipboard";
79+ primary_paste => "xclip", "-o";
80+ primary_copy => "xclip", "-i";
4581 }
4682 } else if env_var_is_set("DISPLAY") && exists("xsel") && is_exit_success("xsel", &["-o", "-b"])
4783 {
@@ -49,6 +85,8 @@ pub fn get_clipboard_provider() -> Box<dyn ClipboardProvider> {
4985 command_provider! {
5086 paste => "xsel", "-o", "-b";
5187 copy => "xsel", "--nodetach", "-i", "-b";
88+ primary_paste => "xsel", "-o";
89+ primary_copy => "xsel", "-i";
5290 }
5391 } else if exists("lemonade") {
5492 command_provider! {
@@ -78,10 +116,10 @@ pub fn get_clipboard_provider() -> Box<dyn ClipboardProvider> {
78116 }
79117 } else {
80118 #[cfg(target_os = "windows")]
81− return Box::new(provider::WindowsProvider);
119+ return Box::new(provider::WindowsProvider::new());
82120
83121 #[cfg(not(target_os = "windows"))]
84− return Box::new(provider::NopProvider);
122+ return Box::new(provider::NopProvider::new());
85123 }
86124}
87125
@@ -103,30 +141,62 @@ fn is_exit_success(program: &str, args: &[&str]) -> bool {
103141}
104142
105143mod provider {
106− use super::ClipboardProvider;
144+ use super::{ClipboardProvider, ClipboardType};
107145 use anyhow::{bail, Context as _, Result};
108146 use std::borrow::Cow;
109147
110148 #[derive(Debug)]
111− pub struct NopProvider;
149+ pub struct NopProvider {
150+ buf: String,
151+ primary_buf: String,
152+ }
153+
154+ impl NopProvider {
155+ pub fn new() -> Self {
156+ Self {
157+ buf: String::new(),
158+ primary_buf: String::new(),
159+ }
160+ }
161+ }
112162
113163 impl ClipboardProvider for NopProvider {
114164 fn name(&self) -> Cow<str> {
115165 Cow::Borrowed("none")
116166 }
117167
118− fn get_contents(&self) -> Result<String> {
119− Ok(String::new())
168+ fn get_contents(&self, clipboard_type: ClipboardType) -> Result<String> {
169+ let value = match clipboard_type {
170+ ClipboardType::Clipboard => self.buf.clone(),
171+ ClipboardType::Selection => self.primary_buf.clone(),
172+ };
173+
174+ Ok(value)
120175 }
121176
122− fn set_contents(&self, _: String) -> Result<()> {
177+ fn set_contents(&mut self, content: String, clipboard_type: ClipboardType) -> Result<()> {
178+ match clipboard_type {
179+ ClipboardType::Clipboard => self.buf = content,
180+ ClipboardType::Selection => self.primary_buf = content,
181+ }
123182 Ok(())
124183 }
125184 }
126185
127186 #[cfg(target_os = "windows")]
128187 #[derive(Debug)]
129− pub struct WindowsProvider;
188+ pub struct WindowsProvider {
189+ selection_buf: String,
190+ }
191+
192+ #[cfg(target_os = "windows")]
193+ impl WindowsProvider {
194+ pub fn new() -> Self {
195+ Self {
196+ selection_buf: String::new(),
197+ }
198+ }
199+ }
130200
131201 #[cfg(target_os = "windows")]
132202 impl ClipboardProvider for WindowsProvider {
@@ -134,13 +204,23 @@ mod provider {
134204 Cow::Borrowed("clipboard-win")
135205 }
136206
137− fn get_contents(&self) -> Result<String> {
138− let contents = clipboard_win::get_clipboard(clipboard_win::formats::Unicode)?;
139− Ok(contents)
207+ fn get_contents(&self, clipboard_type: ClipboardType) -> Result<String> {
208+ match clipboard_type {
209+ ClipboardType::Clipboard => {
210+ let contents = clipboard_win::get_clipboard(clipboard_win::formats::Unicode)?;
211+ Ok(contents)
212+ }
213+ ClipboardType::Selection => Ok(String::new()),
214+ }
140215 }
141216
142− fn set_contents(&self, contents: String) -> Result<()> {
143− clipboard_win::set_clipboard(clipboard_win::formats::Unicode, contents)?;
217+ fn set_contents(&mut self, contents: String, clipboard_type: ClipboardType) -> Result<()> {
218+ match clipboard_type {
219+ ClipboardType::Clipboard => {
220+ clipboard_win::set_clipboard(clipboard_win::formats::Unicode, contents);
221+ }
222+ ClipboardType::Selection => {}
223+ };
144224 Ok(())
145225 }
146226 }
@@ -192,6 +272,8 @@ mod provider {
192272 pub struct CommandProvider {
193273 pub get_cmd: CommandConfig,
194274 pub set_cmd: CommandConfig,
275+ pub get_primary_cmd: Option<CommandConfig>,
276+ pub set_primary_cmd: Option<CommandConfig>,
195277 }
196278
197279 impl ClipboardProvider for CommandProvider {
@@ -203,16 +285,34 @@ mod provider {
203285 }
204286 }
205287
206− fn get_contents(&self) -> Result<String> {
207− let output = self
208− .get_cmd
209− .execute(None, true)?
210− .context("output is missing")?;
211− Ok(output)
288+ fn get_contents(&self, clipboard_type: ClipboardType) -> Result<String> {
289+ match clipboard_type {
290+ ClipboardType::Clipboard => Ok(self
291+ .get_cmd
292+ .execute(None, true)?
293+ .context("output is missing")?),
294+ ClipboardType::Selection => {
295+ if let Some(cmd) = &self.get_primary_cmd {
296+ return cmd.execute(None, true)?.context("output is missing");
297+ }
298+
299+ Ok(String::new())
300+ }
301+ }
212302 }
213303
214− fn set_contents(&self, value: String) -> Result<()> {
215− self.set_cmd.execute(Some(&value), false).map(|_| ())
304+ fn set_contents(&mut self, value: String, clipboard_type: ClipboardType) -> Result<()> {
305+ let cmd = match clipboard_type {
306+ ClipboardType::Clipboard => &self.set_cmd,
307+ ClipboardType::Selection => {
308+ if let Some(cmd) = &self.set_primary_cmd {
309+ cmd
310+ } else {
311+ return Ok(());
312+ }
313+ }
314+ };
315+ cmd.execute(Some(&value), false).map(|_| ())
216316 }
217317 }
218318}
helix-view/src/editor.rs+3
@@ -29,6 +29,8 @@ pub struct Config {
2929 pub scroll_lines: isize,
3030 /// Mouse support. Defaults to true.
3131 pub mouse: bool,
32+ /// Middle click paste support. Defaults to true
33+ pub middle_click_paste: bool,
3234}
3335
3436impl Default for Config {
@@ -37,6 +39,7 @@ impl Default for Config {
3739 scrolloff: 5,
3840 scroll_lines: 3,
3941 mouse: true,
42+ middle_click_paste: true,
4043 }
4144 }
4245}