@@ -2,7 +2,7 @@ use std::{borrow::Cow, collections::HashMap, iter};
3use anyhow::Result;
3use anyhow::Result;
5−use crate::Editor;
5+use crate::{document::SCRATCH_BUFFER_NAME, Editor};
7/// A key-value store for saving sets of values.
7/// A key-value store for saving sets of values.
@@ -13,6 +13,7 @@ use crate::Editor;
13/// * Black hole (`_`): all values read and written are discarded
13/// * Black hole (`_`): all values read and written are discarded
14/// * Selection indices (`#`): index number of each selection starting at 1
14/// * Selection indices (`#`): index number of each selection starting at 1
15/// * Selection contents (`.`)
15/// * Selection contents (`.`)
16+/// * Document path (`%`): filename of the current buffer
16#[derive(Debug, Default)]
17#[derive(Debug, Default)]
17pub struct Registers {
18pub struct Registers {
18 inner: HashMap<char, Vec<String>>,
19 inner: HashMap<char, Vec<String>>,
@@ -36,6 +37,17 @@ impl Registers {
36 let text = doc.text().slice(..);
37 let text = doc.text().slice(..);
37 Some(RegisterValues::new(doc.selection(view.id).fragments(text)))
38 Some(RegisterValues::new(doc.selection(view.id).fragments(text)))
41+ let doc = doc!(editor);
46+ .map(|p| p.to_string_lossy())
47+ .unwrap_or_else(|| SCRATCH_BUFFER_NAME.into());
49+ Some(RegisterValues::new(iter::once(path)))
41 .get(&name)
53 .get(&name)
@@ -46,7 +58,7 @@ impl Registers {
46 pub fn write(&mut self, name: char, values: Vec<String>) -> Result<()> {
58 pub fn write(&mut self, name: char, values: Vec<String>) -> Result<()> {
47 match name {
59 match name {
48 '_' => Ok(()),
60 '_' => Ok(()),
49− '#' | '.' => Err(anyhow::anyhow!("Register {name} does not support writing")),
61+ '#' | '.' | '%' => Err(anyhow::anyhow!("Register {name} does not support writing")),
51 self.inner.insert(name, values);
63 self.inner.insert(name, values);
@@ -57,7 +69,7 @@ impl Registers {
57 pub fn push(&mut self, name: char, value: String) -> Result<()> {
69 pub fn push(&mut self, name: char, value: String) -> Result<()> {
58 match name {
70 match name {
59 '_' => Ok(()),
71 '_' => Ok(()),
60− '#' | '.' => Err(anyhow::anyhow!("Register {name} does not support pushing")),
72+ '#' | '.' | '%' => Err(anyhow::anyhow!("Register {name} does not support pushing")),
62 self.inner.entry(name).or_insert_with(Vec::new).push(value);
74 self.inner.entry(name).or_insert_with(Vec::new).push(value);
@@ -89,6 +101,7 @@ impl Registers {
89 ('_', "<empty>"),
101 ('_', "<empty>"),
90 ('#', "<selection indices>"),
102 ('#', "<selection indices>"),
91 ('.', "<selection contents>"),
103 ('.', "<selection contents>"),
104+ ('%', "<document path>"),
94 .copied(),
107 .copied(),
@@ -101,7 +114,7 @@ impl Registers {
102 pub fn remove(&mut self, name: char) -> bool {
115 pub fn remove(&mut self, name: char) -> bool {
103 match name {
116 match name {
104− '_' | '#' | '.' => false,
117+ '_' | '#' | '.' | '%' => false,
105 _ => self.inner.remove(&name).is_some(),
118 _ => self.inner.remove(&name).is_some(),