@@ -1,7 +1,7 @@
11use helix_core::{
22 graphemes,
33 regex::Regex,
4− selection,
4+ register, selection,
55 state::{Direction, Granularity, Mode, State},
66 ChangeSet, Range, Selection, Tendril, Transaction,
77};
@@ -443,3 +443,36 @@ pub fn undo(view: &mut View, _count: usize) {
443443pub fn redo(view: &mut View, _count: usize) {
444444 view.history.redo(&mut view.state);
445445}
446+
447+// Yank / Paste
448+
449+pub fn yank(view: &mut View, _count: usize) {
450+ // TODO: should selections be made end inclusive?
451+ let values = view
452+ .state
453+ .selection()
454+ .fragments(&view.state.doc().slice(..))
455+ .map(|cow| cow.into_owned())
456+ .collect();
457+
458+ register::set('"', values);
459+}
460+
461+pub fn paste(view: &mut View, _count: usize) {
462+ if let Some(values) = register::get('"') {
463+ let repeat = std::iter::repeat(
464+ values
465+ .last()
466+ .map(|value| Tendril::from_slice(value))
467+ .unwrap(),
468+ );
469+
470+ let mut values = values.into_iter().map(Tendril::from).chain(repeat);
471+
472+ let transaction = Transaction::change_by_selection(&view.state, |range| {
473+ (range.head + 1, range.head + 1, Some(values.next().unwrap()))
474+ });
475+
476+ transaction.apply(&mut view.state);
477+ }
478+}