on master help sign in
Add the '%' (current filename) register
32d071aMichael Davis
helix-view/src/register.rs+17−4
@@ -2,7 +2,7 @@ use std::{borrow::Cow, collections::HashMap, iter};
2
2
3use anyhow::Result;
3use anyhow::Result;
4
4
5−use crate::Editor;
5+use crate::{document::SCRATCH_BUFFER_NAME, Editor};
6
6
7/// A key-value store for saving sets of values.
7/// A key-value store for saving sets of values.
8///
8///
@@ -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)))
38 }
39 }
40+ '%' => {
41+ let doc = doc!(editor);
42+
43+ let path = doc
44+ .path()
45+ .as_ref()
46+ .map(|p| p.to_string_lossy())
47+ .unwrap_or_else(|| SCRATCH_BUFFER_NAME.into());
48+
49+ Some(RegisterValues::new(iter::once(path)))
50+ }
39 _ => self
51 _ => self
40 .inner
52 .inner
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")),
50 _ => {
62 _ => {
51 self.inner.insert(name, values);
63 self.inner.insert(name, values);
52 Ok(())
64 Ok(())
@@ -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")),
61 _ => {
73 _ => {
62 self.inner.entry(name).or_insert_with(Vec::new).push(value);
74 self.inner.entry(name).or_insert_with(Vec::new).push(value);
63 Ok(())
75 Ok(())
@@ -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>"),
92 ]
105 ]
93 .iter()
106 .iter()
94 .copied(),
107 .copied(),
@@ -101,7 +114,7 @@ impl Registers {
101
114
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(),
106 }
119 }
107 }
120 }