on master help sign in
Add yank_joined command (#7195)
d8b7232spectre256
book/src/generated/typable-cmd.md+1
@@ -31,6 +31,7 @@
31| `:cquit`, `:cq` | Quit with exit code (default 1). Accepts an optional integer exit code (:cq 2). |
31| `:cquit`, `:cq` | Quit with exit code (default 1). Accepts an optional integer exit code (:cq 2). |
32| `:cquit!`, `:cq!` | Force quit with exit code (default 1) ignoring unsaved changes. Accepts an optional integer exit code (:cq! 2). |
32| `:cquit!`, `:cq!` | Force quit with exit code (default 1) ignoring unsaved changes. Accepts an optional integer exit code (:cq! 2). |
33| `:theme` | Change the editor theme (show current theme if no name specified). |
33| `:theme` | Change the editor theme (show current theme if no name specified). |
34+| `:yank-join` | Yank joined selections. A separator can be provided as first argument. Default value is newline. |
34| `:clipboard-yank` | Yank main selection into system clipboard. |
35| `:clipboard-yank` | Yank main selection into system clipboard. |
35| `:clipboard-yank-join` | Yank joined selections into system clipboard. A separator can be provided as first argument. Default value is newline. |
36| `:clipboard-yank-join` | Yank joined selections into system clipboard. A separator can be provided as first argument. Default value is newline. |
36| `:primary-clipboard-yank` | Yank main selection into system primary clipboard. |
37| `:primary-clipboard-yank` | Yank main selection into system primary clipboard. |
helix-term/src/commands.rs+33
@@ -376,6 +376,7 @@ impl MappableCommand {
376 later, "Move forward in history",
376 later, "Move forward in history",
377 commit_undo_checkpoint, "Commit changes to new checkpoint",
377 commit_undo_checkpoint, "Commit changes to new checkpoint",
378 yank, "Yank selection",
378 yank, "Yank selection",
379+ yank_joined, "Join and yank selections",
379 yank_joined_to_clipboard, "Join and yank selections to clipboard",
380 yank_joined_to_clipboard, "Join and yank selections to clipboard",
380 yank_main_selection_to_clipboard, "Yank main selection to clipboard",
381 yank_main_selection_to_clipboard, "Yank main selection to clipboard",
381 yank_joined_to_primary_clipboard, "Join and yank selections to primary clipboard",
382 yank_joined_to_primary_clipboard, "Join and yank selections to primary clipboard",
@@ -3724,6 +3725,38 @@ fn yank(cx: &mut Context) {
3724 exit_select_mode(cx);
3725 exit_select_mode(cx);
3725}
3726}
3726
3727
3728+fn yank_joined_impl(editor: &mut Editor, separator: &str, register: char) {
3729+ let (view, doc) = current!(editor);
3730+ let text = doc.text().slice(..);
3731+
3732+ let selection = doc.selection(view.id);
3733+ let joined = selection
3734+ .fragments(text)
3735+ .fold(String::new(), |mut acc, fragment| {
3736+ if !acc.is_empty() {
3737+ acc.push_str(separator);
3738+ }
3739+ acc.push_str(&fragment);
3740+ acc
3741+ });
3742+
3743+ let msg = format!(
3744+ "joined and yanked {} selection(s) to register {}",
3745+ selection.len(),
3746+ register,
3747+ );
3748+
3749+ editor.registers.write(register, vec![joined]);
3750+ editor.set_status(msg);
3751+}
3752+
3753+fn yank_joined(cx: &mut Context) {
3754+ let line_ending = doc!(cx.editor).line_ending;
3755+ let register = cx.register.unwrap_or('"');
3756+ yank_joined_impl(cx.editor, line_ending.as_str(), register);
3757+ exit_select_mode(cx);
3758+}
3759+
3727fn yank_joined_to_clipboard_impl(
3760fn yank_joined_to_clipboard_impl(
3728 editor: &mut Editor,
3761 editor: &mut Editor,
3729 separator: &str,
3762 separator: &str,
helix-term/src/commands/typed.rs+26
@@ -895,6 +895,25 @@ fn yank_main_selection_to_clipboard(
895 yank_main_selection_to_clipboard_impl(cx.editor, ClipboardType::Clipboard)
895 yank_main_selection_to_clipboard_impl(cx.editor, ClipboardType::Clipboard)
896}
896}
897
897
898+fn yank_joined(
899+ cx: &mut compositor::Context,
900+ args: &[Cow<str>],
901+ event: PromptEvent,
902+) -> anyhow::Result<()> {
903+ if event != PromptEvent::Validate {
904+ return Ok(());
905+ }
906+
907+ ensure!(args.len() <= 1, ":yank-join takes at most 1 argument");
908+
909+ let doc = doc!(cx.editor);
910+ let default_sep = Cow::Borrowed(doc.line_ending.as_str());
911+ let separator = args.first().unwrap_or(&default_sep);
912+ let register = cx.editor.selected_register.unwrap_or('"');
913+ yank_joined_impl(cx.editor, separator, register);
914+ Ok(())
915+}
916+
898fn yank_joined_to_clipboard(
917fn yank_joined_to_clipboard(
899 cx: &mut compositor::Context,
918 cx: &mut compositor::Context,
900 args: &[Cow<str>],
919 args: &[Cow<str>],
@@ -2474,6 +2493,13 @@ pub const TYPABLE_COMMAND_LIST: &[TypableCommand] = &[
2474 fun: theme,
2493 fun: theme,
2475 signature: CommandSignature::positional(&[completers::theme]),
2494 signature: CommandSignature::positional(&[completers::theme]),
2476 },
2495 },
2496+ TypableCommand {
2497+ name: "yank-join",
2498+ aliases: &[],
2499+ doc: "Yank joined selections. A separator can be provided as first argument. Default value is newline.",
2500+ fun: yank_joined,
2501+ signature: CommandSignature::none(),
2502+ },
2477 TypableCommand {
2503 TypableCommand {
2478 name: "clipboard-yank",
2504 name: "clipboard-yank",
2479 aliases: &[],
2505 aliases: &[],