From 9e171e7d1d644654010b54c775a1d688b6d56fb2 Mon Sep 17 00:00:00 2001 From: yehor <64604791+oworope@users.noreply.github.com> Date: Thu, 21 Nov 2024 02:24:55 +0300 Subject: [PATCH] Add default-yank-register option (#11430) Co-authored-by: Michael Davis --- book/src/editor.md | 1 + helix-term/src/commands.rs | 33 ++++++++++++++++++++++++++------- helix-view/src/editor.rs | 3 +++ 3 files changed, 30 insertions(+), 7 deletions(-) diff --git a/book/src/editor.md b/book/src/editor.md index b5a5608af..fa5aef47e 100644 --- a/book/src/editor.md +++ b/book/src/editor.md @@ -24,6 +24,7 @@ ### `[editor]` Section |--|--|---------| | `scrolloff` | Number of lines of padding around the edge of the screen when scrolling | `5` | | `mouse` | Enable mouse mode | `true` | +| `default-yank-register` | Default register used for yank/paste | `"` | | `middle-click-paste` | Middle click paste support | `true` | | `scroll-lines` | Number of lines to scroll per scroll wheel step | `3` | | `shell` | Shell to use when running external commands | Unix: `["sh", "-c"]`
Windows: `["cmd", "/C"]` | diff --git a/helix-term/src/commands.rs b/helix-term/src/commands.rs index 073413989..61855d356 100644 --- a/helix-term/src/commands.rs +++ b/helix-term/src/commands.rs @@ -2735,7 +2735,9 @@ fn delete_selection_impl(cx: &mut Context, op: Operation, yank: YankAction) { // yank the selection let text = doc.text().slice(..); let values: Vec = selection.fragments(text).map(Cow::into_owned).collect(); - let reg_name = cx.register.unwrap_or('"'); + let reg_name = cx + .register + .unwrap_or_else(|| cx.editor.config.load().default_yank_register); if let Err(err) = cx.editor.registers.write(reg_name, values) { cx.editor.set_error(err.to_string()); return; @@ -4221,7 +4223,11 @@ fn commit_undo_checkpoint(cx: &mut Context) { // Yank / Paste fn yank(cx: &mut Context) { - yank_impl(cx.editor, cx.register.unwrap_or('"')); + yank_impl( + cx.editor, + cx.register + .unwrap_or(cx.editor.config().default_yank_register), + ); exit_select_mode(cx); } @@ -4282,7 +4288,12 @@ fn yank_joined_impl(editor: &mut Editor, separator: &str, register: char) { fn yank_joined(cx: &mut Context) { let separator = doc!(cx.editor).line_ending.as_str(); - yank_joined_impl(cx.editor, separator, cx.register.unwrap_or('"')); + yank_joined_impl( + cx.editor, + separator, + cx.register + .unwrap_or(cx.editor.config().default_yank_register), + ); exit_select_mode(cx); } @@ -4442,7 +4453,12 @@ fn paste_primary_clipboard_before(cx: &mut Context) { } fn replace_with_yanked(cx: &mut Context) { - replace_with_yanked_impl(cx.editor, cx.register.unwrap_or('"'), cx.count()); + replace_with_yanked_impl( + cx.editor, + cx.register + .unwrap_or(cx.editor.config().default_yank_register), + cx.count(), + ); exit_select_mode(cx); } @@ -4505,7 +4521,8 @@ fn paste(editor: &mut Editor, register: char, pos: Paste, count: usize) { fn paste_after(cx: &mut Context) { paste( cx.editor, - cx.register.unwrap_or('"'), + cx.register + .unwrap_or(cx.editor.config().default_yank_register), Paste::After, cx.count(), ); @@ -4515,7 +4532,8 @@ fn paste_after(cx: &mut Context) { fn paste_before(cx: &mut Context) { paste( cx.editor, - cx.register.unwrap_or('"'), + cx.register + .unwrap_or(cx.editor.config().default_yank_register), Paste::Before, cx.count(), ); @@ -5369,7 +5387,8 @@ fn insert_register(cx: &mut Context) { cx.register = Some(ch); paste( cx.editor, - cx.register.unwrap_or('"'), + cx.register + .unwrap_or(cx.editor.config().default_yank_register), Paste::Cursor, cx.count(), ); diff --git a/helix-view/src/editor.rs b/helix-view/src/editor.rs index 48d3bc365..9e1bee8e1 100644 --- a/helix-view/src/editor.rs +++ b/helix-view/src/editor.rs @@ -270,6 +270,8 @@ pub struct Config { pub auto_completion: bool, /// Automatic formatting on save. Defaults to true. pub auto_format: bool, + /// Default register used for yank/paste. Defaults to '"' + pub default_yank_register: char, /// Automatic save on focus lost and/or after delay. /// Time delay in milliseconds since last edit after which auto save timer triggers. /// Time delay defaults to false with 3000ms delay. Focus lost defaults to false. @@ -951,6 +953,7 @@ fn default() -> Self { auto_pairs: AutoPairConfig::default(), auto_completion: true, auto_format: true, + default_yank_register: '"', auto_save: AutoSave::default(), idle_timeout: Duration::from_millis(250), completion_timeout: Duration::from_millis(250),