diff --git a/book/src/generated/typable-cmd.md b/book/src/generated/typable-cmd.md index 0ff501a33fdb3..bef867a76dd69 100644 --- a/book/src/generated/typable-cmd.md +++ b/book/src/generated/typable-cmd.md @@ -74,3 +74,4 @@ | `:pipe` | Pipe each selection to the shell command. | | `:pipe-to` | Pipe each selection to the shell command, ignoring output. | | `:run-shell-command`, `:sh` | Run a shell command | +| `:clear-register` | Clear given register. If no argument is provided, clear all registers. | diff --git a/helix-core/src/register.rs b/helix-core/src/register.rs index 52eb6e3e72bf3..df68a75943e62 100644 --- a/helix-core/src/register.rs +++ b/helix-core/src/register.rs @@ -78,4 +78,12 @@ impl Registers { pub fn inner(&self) -> &HashMap { &self.inner } + + pub fn clear(&mut self) { + self.inner.clear(); + } + + pub fn remove(&mut self, name: char) -> Option { + self.inner.remove(&name) + } } diff --git a/helix-term/src/commands/typed.rs b/helix-term/src/commands/typed.rs index 0cc1b7432978c..d65b3d2c5038e 100644 --- a/helix-term/src/commands/typed.rs +++ b/helix-term/src/commands/typed.rs @@ -1953,6 +1953,38 @@ fn run_shell_command( Ok(()) } +fn clear_register( + cx: &mut compositor::Context, + args: &[Cow], + event: PromptEvent, +) -> anyhow::Result<()> { + if event != PromptEvent::Validate { + return Ok(()); + } + + ensure!(args.len() <= 1, ":clear-register takes at most 1 argument"); + if args.is_empty() { + cx.editor.registers.clear(); + cx.editor.set_status("All registers cleared"); + return Ok(()); + } + + ensure!( + args[0].chars().count() == 1, + format!("Invalid register {}", args[0]) + ); + let register = args[0].chars().next().unwrap_or_default(); + match cx.editor.registers.remove(register) { + Some(_) => cx + .editor + .set_status(format!("Register {} cleared", register)), + None => cx + .editor + .set_error(format!("Register {} not found", register)), + } + Ok(()) +} + pub const TYPABLE_COMMAND_LIST: &[TypableCommand] = &[ TypableCommand { name: "quit", @@ -2475,6 +2507,13 @@ pub const TYPABLE_COMMAND_LIST: &[TypableCommand] = &[ fun: run_shell_command, completer: Some(completers::filename), }, + TypableCommand { + name: "clear-register", + aliases: &[], + doc: "Clear given register. If no argument is provided, clear all registers.", + fun: clear_register, + completer: None, + }, ]; pub static TYPABLE_COMMAND_MAP: Lazy> =