Unnamed repository; edit this file 'description' to name the repository.
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
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
use std::{
    borrow::Cow,
    collections::{HashMap, VecDeque},
    iter,
    num::NonZeroUsize,
};

use anyhow::Result;
use helix_core::NATIVE_LINE_ENDING;

use crate::{
    clipboard::{get_clipboard_provider, ClipboardProvider, ClipboardType},
    document::SCRATCH_BUFFER_NAME,
    Editor,
};

/// Standard registers store up to this many yanks worth of history.
/// Once a register hits this many yanks, it discards the oldest values to
/// make space for new yanks.
const MAX_REGISTER_HISTORY_LEN: usize = 100;

#[derive(Debug, Default)]
struct Register {
    /// The values held by the register.
    ///
    /// When yanking to a register, all values are pushed into this `VecDeque`. The length
    /// of those values is stored in `length`. So each yank is stored in this flat sequence,
    /// but this `VecDeque` holds the sequence of all yanks.
    ///
    /// This `VecDeque` should only be empty when constructing `Register` via `Default`, which
    /// we do in `Registers` for simplicity. (Note that it should be impossible to `write`
    /// with an empty set of values.)
    ///
    /// Yanks are stored least to most recent. Within each yank, values are stored in order.
    values: VecDeque<String>,
    /// The length of each yank into the register.
    lengths: VecDeque<NonZeroUsize>,
}

impl Register {
    fn latest_value(&self) -> Option<&String> {
        self.values.back()
    }

    fn values(&self) -> RegisterValues<'_> {
        let length = self.lengths.back().map(|len| len.get()).unwrap_or_default();
        RegisterValues::new(
            self.values
                .iter()
                .rev()
                .take(length)
                .rev()
                .map(|s| Cow::Borrowed(s.as_str())),
        )
    }

    fn write<I: IntoIterator<Item = String>>(&mut self, values: I) {
        // If the register is full, discard the oldest yank in history.
        if self.lengths.len() > MAX_REGISTER_HISTORY_LEN {
            // Greater than max length implies non-empty.
            let oldest_len = self.lengths.pop_front().unwrap();
            self.values.drain(..oldest_len.get());
        }

        let pre_yank_len = self.values.len();
        self.values.extend(values.into_iter());
        let yank_len = NonZeroUsize::new(self.values.len() - pre_yank_len)
            .expect("writes to registers must not be empty");
        self.lengths.push_back(yank_len);
    }

    fn push(&mut self, value: String) {
        self.values.push_back(value);
        if let Some(last_length) = self.lengths.back_mut() {
            *last_length = NonZeroUsize::new(last_length.get() + 1).unwrap();
        } else {
            self.lengths.push_back(NonZeroUsize::new(1).unwrap());
        }
    }
}

/// A key-value store for saving sets of values.
///
/// Each register corresponds to a `char`. Most chars can be used to store any set of
/// values but a few chars are "special registers". Special registers have unique
/// behaviors when read or written to:
///
/// * Black hole (`_`): all values read and written are discarded
/// * Selection indices (`#`): index number of each selection starting at 1
/// * Selection contents (`.`)
/// * Document path (`%`): filename of the current buffer
/// * System clipboard (`*`)
/// * Primary clipboard (`+`)
#[derive(Debug)]
pub struct Registers {
    /// The mapping of register to values.
    /// This contains non-special registers plus '+' and '*'.
    inner: HashMap<char, Register>,
    clipboard_provider: Box<dyn ClipboardProvider>,
    pub last_search_register: char,
}

impl Default for Registers {
    fn default() -> Self {
        Self {
            inner: Default::default(),
            clipboard_provider: get_clipboard_provider(),
            last_search_register: '/',
        }
    }
}

impl Registers {
    pub fn read<'a>(&'a self, name: char, editor: &'a Editor) -> Option<RegisterValues<'a>> {
        match name {
            '_' => Some(RegisterValues::new(iter::empty())),
            '#' => {
                let (view, doc) = current_ref!(editor);
                let selections = doc.selection(view.id).len();
                // ExactSizeIterator is implemented for Range<usize> but
                // not RangeInclusive<usize>.
                Some(RegisterValues::new(
                    (0..selections).map(|i| (i + 1).to_string().into()),
                ))
            }
            '.' => {
                let (view, doc) = current_ref!(editor);
                let text = doc.text().slice(..);
                Some(RegisterValues::new(doc.selection(view.id).fragments(text)))
            }
            '%' => {
                let doc = doc!(editor);

                let path = doc
                    .path()
                    .as_ref()
                    .map(|p| p.to_string_lossy())
                    .unwrap_or_else(|| SCRATCH_BUFFER_NAME.into());

                Some(RegisterValues::new(iter::once(path)))
            }
            '*' | '+' => Some(read_from_clipboard(
                self.clipboard_provider.as_ref(),
                self.inner.get(&name),
                match name {
                    '+' => ClipboardType::Clipboard,
                    '*' => ClipboardType::Selection,
                    _ => unreachable!(),
                },
            )),
            _ => self.inner.get(&name).map(Register::values),
        }
    }

    pub fn write<I: IntoIterator<Item = String>>(&mut self, name: char, values: I) -> Result<()> {
        match name {
            '_' => Ok(()),
            '#' | '.' | '%' => Err(anyhow::anyhow!("Register {name} does not support writing")),
            '*' | '+' => {
                self.inner.entry(name).or_default().write(values);
                let mut contents = String::new();
                for val in self.inner[&name].values() {
                    if !contents.is_empty() {
                        contents.push_str(NATIVE_LINE_ENDING.as_str());
                    }
                    contents.push_str(&val);
                }
                self.clipboard_provider.set_contents(
                    contents,
                    match name {
                        '+' => ClipboardType::Clipboard,
                        '*' => ClipboardType::Selection,
                        _ => unreachable!(),
                    },
                )?;
                Ok(())
            }
            _ => {
                self.inner.entry(name).or_default().write(values);
                Ok(())
            }
        }
    }

    pub fn push(&mut self, name: char, mut value: String) -> Result<()> {
        match name {
            '_' => Ok(()),
            '#' | '.' | '%' => Err(anyhow::anyhow!("Register {name} does not support pushing")),
            '*' | '+' => {
                let clipboard_type = match name {
                    '+' => ClipboardType::Clipboard,
                    '*' => ClipboardType::Selection,
                    _ => unreachable!(),
                };
                let contents = self.clipboard_provider.get_contents(clipboard_type)?;
                let register = self.inner.entry(name).or_default();

                if !contents_are_saved(register.values(), &contents) {
                    anyhow::bail!("Failed to push to register {name}: clipboard does not match register contents");
                }

                register.push(value.clone());
                if !contents.is_empty() {
                    value.push_str(NATIVE_LINE_ENDING.as_str());
                }
                value.push_str(&contents);
                self.clipboard_provider
                    .set_contents(value, clipboard_type)?;

                Ok(())
            }
            _ => {
                self.inner.entry(name).or_default().push(value);
                Ok(())
            }
        }
    }

    /// "Selects" the index at the given index for the given register.
    ///
    /// Selecting an item pulls it to the front of the register's history.
    ///
    /// If the register is a special register other than a clipboard register ('+' or '*')
    /// or if the index is out of bounds for the given register, this command is a no-op.
    pub fn select_history_entry(&mut self, name: char, index: usize) -> Result<()> {
        match name {
            '_' | '#' | '.' | '%' => {
                Err(anyhow::anyhow!("Register {name} does not support writing"))
            }
            _ => {
                let Some(register) = self.inner.get_mut(&name) else {
                    return Ok(());
                };
                register.select_history_entry(index);
                self.sync_clipboard_register(name)
            }
        }
    }

    fn sync_clipboard_register(&mut self, name: char) -> Result<()> {
        let clipboard_type = match name {
            '+' => ClipboardType::Clipboard,
            '*' => ClipboardType::Selection,
            _ => return Ok(()),
        };

        let mut contents = String::new();
        for val in self.inner[&name].values() {
            if !contents.is_empty() {
                contents.push_str(NATIVE_LINE_ENDING.as_str());
            }
            contents.push_str(&val);
        }
        self.clipboard_provider
            .set_contents(contents, clipboard_type)
    }

    /// Returns the latest value in the given register.
    ///
    /// The latest value is the value most recently pushed to the register when
    /// using `push`, or the last value returned by the iterator passed to [write].
    pub fn latest<'a>(&'a self, name: char, editor: &'a Editor) -> Option<Cow<'a, str>> {
        self.read(name, editor).and_then(|values| values.last())
    }

    /// Returns the oldest value in the given register.
    /// This is the opposite of `latest`.
    pub fn oldest<'a>(&'a self, name: char, editor: &'a Editor) -> Option<Cow<'a, str>> {
        self.read(name, editor).and_then(|mut values| values.next())
    }

    pub fn iter_preview(&self) -> impl Iterator<Item = (char, &str)> {
        self.inner
            .iter()
            .filter(|(name, _)| !matches!(name, '*' | '+'))
            .map(|(name, register)| {
                let preview = register
                    .latest_value()
                    .and_then(|s| s.lines().next())
                    .unwrap_or("<empty>");

                (*name, preview)
            })
            .chain(
                [
                    ('_', "<empty>"),
                    ('#', "<selection indices>"),
                    ('.', "<selection contents>"),
                    ('%', "<document path>"),
                    ('+', "<system clipboard>"),
                    ('*', "<primary clipboard>"),
                ]
                .iter()
                .copied(),
            )
    }

    pub fn clear(&mut self) {
        self.clear_clipboard(ClipboardType::Clipboard);
        self.clear_clipboard(ClipboardType::Selection);
        self.inner.clear()
    }

    pub fn remove(&mut self, name: char) -> bool {
        match name {
            '*' | '+' => {
                self.clear_clipboard(match name {
                    '+' => ClipboardType::Clipboard,
                    '*' => ClipboardType::Selection,
                    _ => unreachable!(),
                });
                self.inner.remove(&name);

                true
            }
            '_' | '#' | '.' | '%' => false,
            _ => self.inner.remove(&name).is_some(),
        }
    }

    fn clear_clipboard(&mut self, clipboard_type: ClipboardType) {
        if let Err(err) = self
            .clipboard_provider
            .set_contents("".into(), clipboard_type)
        {
            log::error!(
                "Failed to clear {} clipboard: {err}",
                match clipboard_type {
                    ClipboardType::Clipboard => "system",
                    ClipboardType::Selection => "primary",
                }
            )
        }
    }

    pub fn clipboard_provider_name(&self) -> Cow<str> {
        self.clipboard_provider.name()
    }
}

fn read_from_clipboard<'a>(
    provider: &dyn ClipboardProvider,
    register: Option<&'a Register>,
    clipboard_type: ClipboardType,
) -> RegisterValues<'a> {
    match provider.get_contents(clipboard_type) {
        Ok(contents) => {
            // If we're pasting the same values that we just yanked, re-use
            // the saved values. This allows pasting multiple selections
            // even when yanked to a clipboard.
            let Some(register) = register else {
                return RegisterValues::new(iter::once(contents.into()));
            };

            if contents_are_saved(register.values(), &contents) {
                register.values()
            } else {
                RegisterValues::new(iter::once(contents.into()))
            }
        }
        Err(err) => {
            log::error!(
                "Failed to read {} clipboard: {err}",
                match clipboard_type {
                    ClipboardType::Clipboard => "system",
                    ClipboardType::Selection => "primary",
                }
            );

            RegisterValues::new(iter::empty())
        }
    }
}

fn contents_are_saved(mut values: RegisterValues<'_>, mut contents: &str) -> bool {
    let line_ending = NATIVE_LINE_ENDING.as_str();

    match values.next() {
        Some(first) if contents.starts_with(&*first) => {
            contents = &contents[first.len()..];
        }
        None if contents.is_empty() => return true,
        _ => return false,
    }

    for value in values {
        if contents.starts_with(line_ending) && contents[line_ending.len()..].starts_with(&*value) {
            contents = &contents[line_ending.len() + value.len()..];
        } else {
            return false;
        }
    }

    true
}

// This is a wrapper of an iterator that is both double ended and exact size,
// and can return either owned or borrowed values. Regular registers can
// return borrowed values while some special registers need to return owned
// values.
pub struct RegisterValues<'a> {
    iter: Box<dyn DoubleEndedExactSizeIterator<Item = Cow<'a, str>> + 'a>,
}

impl<'a> RegisterValues<'a> {
    fn new(
        iter: impl DoubleEndedIterator<Item = Cow<'a, str>>
            + ExactSizeIterator<Item = Cow<'a, str>>
            + 'a,
    ) -> Self {
        Self {
            iter: Box::new(iter),
        }
    }
}

impl<'a> Iterator for RegisterValues<'a> {
    type Item = Cow<'a, str>;

    fn next(&mut self) -> Option<Self::Item> {
        self.iter.next()
    }

    fn size_hint(&self) -> (usize, Option<usize>) {
        self.iter.size_hint()
    }
}

impl<'a> DoubleEndedIterator for RegisterValues<'a> {
    fn next_back(&mut self) -> Option<Self::Item> {
        self.iter.next_back()
    }
}

impl<'a> ExactSizeIterator for RegisterValues<'a> {
    fn len(&self) -> usize {
        self.iter.len()
    }
}

// Each RegisterValues iterator is both double ended and exact size. We can't
// type RegisterValues as `Box<dyn DoubleEndedIterator + ExactSizeIterator>`
// because only one non-auto trait is allowed in trait objects. So we need to
// create a new trait that covers both. `RegisterValues` wraps that type so that
// trait only needs to live in this module and not be imported for all register
// callsites.
trait DoubleEndedExactSizeIterator: DoubleEndedIterator + ExactSizeIterator {}

impl<I: DoubleEndedIterator + ExactSizeIterator> DoubleEndedExactSizeIterator for I {}