on master help sign in
Add the '%' (current filename) register

This register also comes from Kakoune. It's read-only and produces the current document's name, defaulting to the scratch buffer name constant. (Also see PR5577.) Co-authored-by: Ivan Tham <pickfire@riseup.net>

commit32d071a39207b9f1c1753692e93aff45fcdf12c0 authorMichael Davis <mcarsondavis@gmail.com> dateMon 10 Jul 2023, 17:04 (3 years ago) parentda2afe7
1 file changed+17−4
helix-view/src/register.rs+17−4
helix-view/src/register.rs+17−4
@@ -2,7 +2,7 @@ use std::{borrow::Cow, collections::HashMap, iter};
22
33use anyhow::Result;
44
5−use crate::Editor;
5+use crate::{document::SCRATCH_BUFFER_NAME, Editor};
66
77/// A key-value store for saving sets of values.
88///
@@ -13,6 +13,7 @@ use crate::Editor;
1313/// * Black hole (`_`): all values read and written are discarded
1414/// * Selection indices (`#`): index number of each selection starting at 1
1515/// * Selection contents (`.`)
16+/// * Document path (`%`): filename of the current buffer
1617#[derive(Debug, Default)]
1718pub struct Registers {
1819 inner: HashMap<char, Vec<String>>,
@@ -36,6 +37,17 @@ impl Registers {
3637 let text = doc.text().slice(..);
3738 Some(RegisterValues::new(doc.selection(view.id).fragments(text)))
3839 }
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+ }
3951 _ => self
4052 .inner
4153 .get(&name)
@@ -46,7 +58,7 @@ impl Registers {
4658 pub fn write(&mut self, name: char, values: Vec<String>) -> Result<()> {
4759 match name {
4860 '_' => Ok(()),
49− '#' | '.' => Err(anyhow::anyhow!("Register {name} does not support writing")),
61+ '#' | '.' | '%' => Err(anyhow::anyhow!("Register {name} does not support writing")),
5062 _ => {
5163 self.inner.insert(name, values);
5264 Ok(())
@@ -57,7 +69,7 @@ impl Registers {
5769 pub fn push(&mut self, name: char, value: String) -> Result<()> {
5870 match name {
5971 '_' => Ok(()),
60− '#' | '.' => Err(anyhow::anyhow!("Register {name} does not support pushing")),
72+ '#' | '.' | '%' => Err(anyhow::anyhow!("Register {name} does not support pushing")),
6173 _ => {
6274 self.inner.entry(name).or_insert_with(Vec::new).push(value);
6375 Ok(())
@@ -89,6 +101,7 @@ impl Registers {
89101 ('_', "<empty>"),
90102 ('#', "<selection indices>"),
91103 ('.', "<selection contents>"),
104+ ('%', "<document path>"),
92105 ]
93106 .iter()
94107 .copied(),
@@ -101,7 +114,7 @@ impl Registers {
101114
102115 pub fn remove(&mut self, name: char) -> bool {
103116 match name {
104− '_' | '#' | '.' => false,
117+ '_' | '#' | '.' | '%' => false,
105118 _ => self.inner.remove(&name).is_some(),
106119 }
107120 }