1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
//! Vim-style register bank.
//!
//! Slots:
//! - `"` (unnamed) — written by every `y` / `d` / `c` / `x`; the
//! default source for `p` / `P`.
//! - `"0` — the most recent **yank**. Deletes do not touch it, so
//! `yw…dw…p` still pastes the original yank.
//! - `"1`–`"9` — small-delete ring. Each delete shifts the ring
//! (newest at `"1`, oldest dropped off `"9`).
//! - `"a`–`"z` — named slots. A capital letter (`"A`…) appends to
//! the matching lowercase slot, matching vim semantics.
#[derive(Default, Clone, Debug)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct Slot {
pub text: String,
pub linewise: bool,
}
impl Slot {
fn new(text: String, linewise: bool) -> Self {
Self { text, linewise }
}
}
#[derive(Default, Debug, Clone)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct Registers {
/// `"` — written by every yank / delete / change.
pub unnamed: Slot,
/// `"0` — last yank only.
pub yank_zero: Slot,
/// `"1`–`"9` — last 9 deletes (`"1` newest).
pub delete_ring: [Slot; 9],
/// `"a`–`"z` — named user registers.
pub named: [Slot; 26],
/// `"+` / `"*` — system clipboard register. Both selectors alias
/// the same slot (matches the typical Linux/macOS/Windows setup
/// where there's no separate primary selection in our pipeline).
/// The host (the host) syncs this slot from the OS clipboard
/// before paste and from the slot back out on yank.
pub clip: Slot,
/// `"%` — synthetic read-only register: current buffer filename.
/// Set by the host whenever the active slot changes.
pub filename: Option<String>,
/// Pre-built `Slot` for the `%` register. Kept in sync with `filename`
/// by [`Registers::set_filename`] so `read('%')` can return `&Slot`.
/// Derived from `filename` — not serialised independently.
#[cfg_attr(feature = "serde", serde(skip))]
filename_slot: Option<Slot>,
}
impl Registers {
/// Record a yank operation. Writes to `"`, `"0`, and (if
/// `target` is set) the named slot. When `target` is `'_'`
/// (black-hole register) all writes are suppressed — vim discards
/// the text without touching any register.
pub fn record_yank(&mut self, text: String, linewise: bool, target: Option<char>) {
// Black-hole register: discard the text entirely.
if target == Some('_') {
return;
}
let slot = Slot::new(text, linewise);
self.unnamed = slot.clone();
self.yank_zero = slot.clone();
if let Some(c) = target {
self.write_named(c, slot);
}
}
/// Record a delete / change. Writes to `"`, rotates the
/// `"1`–`"9` ring, and (if `target` is set) the named slot.
/// Empty deletes are dropped — vim doesn't pollute the ring
/// with no-ops. When `target` is `'_'` (black-hole register) all
/// writes are suppressed, preserving the previous register state.
pub fn record_delete(&mut self, text: String, linewise: bool, target: Option<char>) {
if text.is_empty() {
return;
}
// Black-hole register: discard the text entirely.
if target == Some('_') {
return;
}
let slot = Slot::new(text, linewise);
self.unnamed = slot.clone();
for i in (1..9).rev() {
self.delete_ring[i] = self.delete_ring[i - 1].clone();
}
self.delete_ring[0] = slot.clone();
if let Some(c) = target {
self.write_named(c, slot);
}
}
/// Read a register by its single-char selector. Returns `None`
/// for unrecognised selectors.
///
/// `'%'` is a synthetic read-only register: returns the current buffer
/// filename when one has been set via [`Registers::set_filename`].
pub fn read(&self, reg: char) -> Option<&Slot> {
match reg {
'"' => Some(&self.unnamed),
'0' => Some(&self.yank_zero),
'1'..='9' => Some(&self.delete_ring[(reg as u8 - b'1') as usize]),
'a'..='z' => Some(&self.named[(reg as u8 - b'a') as usize]),
'A'..='Z' => Some(&self.named[(reg.to_ascii_lowercase() as u8 - b'a') as usize]),
'+' | '*' => Some(&self.clip),
// `%` is a synthetic read-only register: current buffer filename.
'%' => self.filename_slot.as_ref(),
_ => None,
}
}
/// Host hook: set the `"%` register to the given filename. Call this
/// whenever the active buffer changes.
pub fn set_filename(&mut self, name: Option<String>) {
self.filename = name.clone();
self.filename_slot = name.map(|n| Slot::new(n, false));
}
/// Replace the clipboard slot's contents — host hook for syncing
/// from the OS clipboard before a paste from `"+` / `"*`.
pub fn set_clipboard(&mut self, text: String, linewise: bool) {
self.clip = Slot::new(text, linewise);
}
fn write_named(&mut self, c: char, slot: Slot) {
if c.is_ascii_lowercase() {
self.named[(c as u8 - b'a') as usize] = slot;
} else if c.is_ascii_uppercase() {
let idx = (c.to_ascii_lowercase() as u8 - b'a') as usize;
let cur = &mut self.named[idx];
cur.text.push_str(&slot.text);
cur.linewise = slot.linewise || cur.linewise;
} else if c == '+' || c == '*' {
self.clip = slot;
}
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn yank_writes_unnamed_and_zero() {
let mut r = Registers::default();
r.record_yank("foo".into(), false, None);
assert_eq!(r.read('"').unwrap().text, "foo");
assert_eq!(r.read('0').unwrap().text, "foo");
}
#[test]
fn delete_rotates_ring_and_skips_zero() {
let mut r = Registers::default();
r.record_yank("kept".into(), false, None);
r.record_delete("d1".into(), false, None);
r.record_delete("d2".into(), false, None);
// Newest delete is "1.
assert_eq!(r.read('1').unwrap().text, "d2");
assert_eq!(r.read('2').unwrap().text, "d1");
// "0 untouched by deletes.
assert_eq!(r.read('0').unwrap().text, "kept");
// Unnamed mirrors the latest write.
assert_eq!(r.read('"').unwrap().text, "d2");
}
#[test]
fn named_lowercase_overwrites_uppercase_appends() {
let mut r = Registers::default();
r.record_yank("hello ".into(), false, Some('a'));
r.record_yank("world".into(), false, Some('A'));
assert_eq!(r.read('a').unwrap().text, "hello world");
// "A is just a write target; reading 'A' returns the same slot.
assert_eq!(r.read('A').unwrap().text, "hello world");
}
#[test]
fn empty_delete_is_dropped() {
let mut r = Registers::default();
r.record_delete("first".into(), false, None);
r.record_delete(String::new(), false, None);
assert_eq!(r.read('1').unwrap().text, "first");
assert!(r.read('2').unwrap().text.is_empty());
}
#[test]
fn unknown_selector_returns_none() {
let r = Registers::default();
assert!(r.read('?').is_none());
assert!(r.read('!').is_none());
}
#[test]
fn plus_and_star_alias_clipboard_slot() {
let mut r = Registers::default();
r.set_clipboard("payload".into(), false);
assert_eq!(r.read('+').unwrap().text, "payload");
assert_eq!(r.read('*').unwrap().text, "payload");
}
#[test]
fn yank_to_plus_writes_clipboard_slot() {
let mut r = Registers::default();
r.record_yank("hi".into(), false, Some('+'));
assert_eq!(r.read('+').unwrap().text, "hi");
// Unnamed always mirrors the latest write.
assert_eq!(r.read('"').unwrap().text, "hi");
}
#[test]
fn percent_register_returns_none_when_no_filename() {
let r = Registers::default();
assert!(r.read('%').is_none());
}
#[test]
fn percent_register_returns_filename_after_set() {
let mut r = Registers::default();
r.set_filename(Some("src/main.rs".into()));
let slot = r
.read('%')
.expect("'%' should return Some after set_filename");
assert_eq!(slot.text, "src/main.rs");
assert!(!slot.linewise, "'%' slot should be charwise");
}
#[test]
fn percent_register_clears_when_set_to_none() {
let mut r = Registers::default();
r.set_filename(Some("foo.txt".into()));
r.set_filename(None);
assert!(r.read('%').is_none());
}
}