@@ -222,9 +222,14 @@ impl Command {
225+ yank_joined_to_clipboard,
226+ yank_main_selection_to_clipboard,
225 replace_with_yanked,
227 replace_with_yanked,
228+ replace_selections_with_clipboard,
226 paste_after,
229 paste_after,
227 paste_before,
230 paste_before,
231+ paste_clipboard_after,
232+ paste_clipboard_before,
229 unindent,
234 unindent,
230 format_selections,
235 format_selections,
@@ -1280,74 +1285,24 @@ mod cmd {
1280 editor.set_theme_from_name(theme);
1285 editor.set_theme_from_name(theme);
1283− fn yank_main_selection_to_clipboard(editor: &mut Editor, args: &[&str], event: PromptEvent) {
1288+ fn yank_main_selection_to_clipboard(editor: &mut Editor, _: &[&str], _: PromptEvent) {
1284− let (view, doc) = current!(editor);
1289+ yank_main_selection_to_clipboard_impl(editor);
1286− // TODO: currently the main selection is the first one. This needs to be revisited later.
1288− .selection(view.id)
1291− .expect("at least one selection");
1293− let value = range.fragment(doc.text().slice(..));
1295− if let Err(e) = editor.clipboard_provider.set_contents(value.into_owned()) {
1296− log::error!("Couldn't set system clipboard content: {:?}", e);
1299− editor.set_status("yanked main selection to system clipboard".to_owned());
1302− fn yank_joined_to_clipboard(editor: &mut Editor, args: &[&str], event: PromptEvent) {
1292+ fn yank_joined_to_clipboard(editor: &mut Editor, args: &[&str], _: PromptEvent) {
1303− let (view, doc) = current!(editor);
1293+ let separator = args.first().copied().unwrap_or("\n");
1304−
1294+ yank_joined_to_clipboard_impl(editor, separator);
1305− let values: Vec<String> = doc
1306− .selection(view.id)
1307− .fragments(doc.text().slice(..))
1308− .map(Cow::into_owned)
1312− "joined and yanked {} selection(s) to system clipboard",
1316− let joined = values.join("\n");
1318− if let Err(e) = editor.clipboard_provider.set_contents(joined) {
1319− log::error!("Couldn't set system clipboard content: {:?}", e);
1322− editor.set_status(msg);
1325− fn paste_clipboard_impl(editor: &mut Editor, action: Paste) {
1326− let (view, doc) = current!(editor);
1329− .clipboard_provider
1331− .map(|contents| paste_impl(&[contents], doc, view, action))
1333− Ok(Some(transaction)) => {
1334− doc.apply(&transaction, view.id);
1335− doc.append_changes_to_history(view.id);
1338− Err(e) => log::error!("Couldn't get system clipboard contents: {:?}", e),
1342 fn paste_clipboard_after(editor: &mut Editor, _: &[&str], _: PromptEvent) {
1297 fn paste_clipboard_after(editor: &mut Editor, _: &[&str], _: PromptEvent) {
1343 paste_clipboard_impl(editor, Paste::After);
1298 paste_clipboard_impl(editor, Paste::After);
1346− fn paste_clipboard_before(editor: &mut Editor, args: &[&str], event: PromptEvent) {
1301+ fn paste_clipboard_before(editor: &mut Editor, _: &[&str], _: PromptEvent) {
1347 paste_clipboard_impl(editor, Paste::After);
1302 paste_clipboard_impl(editor, Paste::After);
1350− fn replace_selections_with_clipboard(editor: &mut Editor, args: &[&str], event: PromptEvent) {
1305+ fn replace_selections_with_clipboard(editor: &mut Editor, _: &[&str], _: PromptEvent) {
1351 let (view, doc) = current!(editor);
1306 let (view, doc) = current!(editor);
1353 match editor.clipboard_provider.get_contents() {
1308 match editor.clipboard_provider.get_contents() {
@@ -1500,7 +1455,7 @@ mod cmd {
1500 TypableCommand {
1455 TypableCommand {
1501 name: "clipboard-yank-join",
1456 name: "clipboard-yank-join",
1502 alias: None,
1457 alias: None,
1503− doc: "Yank joined selections into system clipboard.",
1458+ doc: "Yank joined selections into system clipboard. A separator can be provided as first argument. Default value is newline.", // FIXME: current UI can't display long doc.
1504 fun: yank_joined_to_clipboard,
1459 fun: yank_joined_to_clipboard,
1505 completer: None,
1460 completer: None,
@@ -2525,6 +2480,52 @@ fn yank(cx: &mut Context) {
2525 cx.editor.set_status(msg)
2480 cx.editor.set_status(msg)
2483+fn yank_joined_to_clipboard_impl(editor: &mut Editor, separator: &str) {
2484+ let (view, doc) = current!(editor);
2486+ let values: Vec<String> = doc
2487+ .selection(view.id)
2488+ .fragments(doc.text().slice(..))
2489+ .map(Cow::into_owned)
2493+ "joined and yanked {} selection(s) to system clipboard",
2497+ let joined = values.join(separator);
2499+ if let Err(e) = editor.clipboard_provider.set_contents(joined) {
2500+ log::error!("Couldn't set system clipboard content: {:?}", e);
2503+ editor.set_status(msg);
2506+fn yank_joined_to_clipboard(cx: &mut Context) {
2507+ yank_joined_to_clipboard_impl(&mut cx.editor, "\n");
2510+fn yank_main_selection_to_clipboard_impl(editor: &mut Editor) {
2511+ let (view, doc) = current!(editor);
2514+ .selection(view.id)
2516+ .fragment(doc.text().slice(..));
2518+ if let Err(e) = editor.clipboard_provider.set_contents(value.into_owned()) {
2519+ log::error!("Couldn't set system clipboard content: {:?}", e);
2522+ editor.set_status("yanked main selection to system clipboard".to_owned());
2525+fn yank_main_selection_to_clipboard(cx: &mut Context) {
2526+ yank_main_selection_to_clipboard_impl(&mut cx.editor);
2528#[derive(Copy, Clone)]
2529#[derive(Copy, Clone)]
2529enum Paste {
2530enum Paste {
@@ -2568,6 +2569,31 @@ fn paste_impl(
2568 Some(transaction)
2569 Some(transaction)
2572+fn paste_clipboard_impl(editor: &mut Editor, action: Paste) {
2573+ let (view, doc) = current!(editor);
2576+ .clipboard_provider
2578+ .map(|contents| paste_impl(&[contents], doc, view, action))
2580+ Ok(Some(transaction)) => {
2581+ doc.apply(&transaction, view.id);
2582+ doc.append_changes_to_history(view.id);
2585+ Err(e) => log::error!("Couldn't get system clipboard contents: {:?}", e),
2589+fn paste_clipboard_after(cx: &mut Context) {
2590+ paste_clipboard_impl(&mut cx.editor, Paste::After);
2593+fn paste_clipboard_before(cx: &mut Context) {
2594+ paste_clipboard_impl(&mut cx.editor, Paste::Before);
2571fn replace_with_yanked(cx: &mut Context) {
2597fn replace_with_yanked(cx: &mut Context) {
2572 let reg_name = cx.selected_register.name();
2598 let reg_name = cx.selected_register.name();
2573 let (view, doc) = current!(cx.editor);
2599 let (view, doc) = current!(cx.editor);
@@ -2588,6 +2614,29 @@ fn replace_with_yanked(cx: &mut Context) {
2617+fn replace_selections_with_clipboard_impl(editor: &mut Editor) {
2618+ let (view, doc) = current!(editor);
2620+ match editor.clipboard_provider.get_contents() {
2623+ Transaction::change_by_selection(doc.text(), doc.selection(view.id), |range| {
2624+ let max_to = doc.text().len_chars().saturating_sub(1);
2625+ let to = std::cmp::min(max_to, range.to() + 1);
2626+ (range.from(), to, Some(contents.as_str().into()))
2629+ doc.apply(&transaction, view.id);
2630+ doc.append_changes_to_history(view.id);
2632+ Err(e) => log::error!("Couldn't get system clipboard contents: {:?}", e),
2636+fn replace_selections_with_clipboard(cx: &mut Context) {
2637+ replace_selections_with_clipboard_impl(&mut cx.editor);
2591// alt-p => paste every yanked selection after selected text
2640// alt-p => paste every yanked selection after selected text
2592// alt-P => paste every yanked selection before selected text
2641// alt-P => paste every yanked selection before selected text
2593// R => replace selected text with yanked text
2642// R => replace selected text with yanked text
@@ -3108,6 +3157,11 @@ fn space_mode(cx: &mut Context) {
3108 'b' => buffer_picker(cx),
3157 'b' => buffer_picker(cx),
3109 's' => symbol_picker(cx),
3158 's' => symbol_picker(cx),
3110 'w' => window_mode(cx),
3159 'w' => window_mode(cx),
3160+ 'y' => yank_joined_to_clipboard(cx),
3161+ 'Y' => yank_main_selection_to_clipboard(cx),
3162+ 'p' => paste_clipboard_after(cx),
3163+ 'P' => paste_clipboard_before(cx),
3164+ 'R' => replace_selections_with_clipboard(cx),
3111 // ' ' => toggle_alternate_buffer(cx),
3165 // ' ' => toggle_alternate_buffer(cx),
3112 // TODO: temporary since space mode took its old key
3166 // TODO: temporary since space mode took its old key
3113 ' ' => keep_primary_selection(cx),
3167 ' ' => keep_primary_selection(cx),