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