Skip to content

Commit

Permalink
yank multiple words at the same time
Browse files Browse the repository at this point in the history
Currently, using control-w to cut multiple words leaves only
the most recently cut word in your cut buffer for yanking with
control-y.

This patch makes reedline behave like readline: it restores all
concecutive cuts as a single buffer.
  • Loading branch information
alsuren committed Sep 9, 2023
1 parent 409b517 commit 2dd7a31
Showing 1 changed file with 82 additions and 0 deletions.
82 changes: 82 additions & 0 deletions src/core_editor/editor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ use crate::{core_editor::get_default_clipboard, EditCommand};
pub struct Editor {
line_buffer: LineBuffer,
cut_buffer: Box<dyn Clipboard>,
last_edit_was_cut: bool,

edit_stack: EditStack<LineBuffer>,
last_undo_behavior: UndoBehavior,
Expand All @@ -19,6 +20,7 @@ impl Default for Editor {
Editor {
line_buffer: LineBuffer::new(),
cut_buffer: Box::new(get_default_clipboard()),
last_edit_was_cut: false,
edit_stack: EditStack::new(),
last_undo_behavior: UndoBehavior::CreateUndoPoint,
}
Expand Down Expand Up @@ -98,6 +100,26 @@ impl Editor {
EditCommand::MoveLeftBefore(c) => self.move_left_until_char(*c, true, true),
}

self.last_edit_was_cut = match command {
EditCommand::CutChar
| EditCommand::CutCurrentLine
| EditCommand::CutFromStart
| EditCommand::CutFromLineStart
| EditCommand::CutToEnd
| EditCommand::CutToLineEnd
| EditCommand::CutWordLeft
| EditCommand::CutBigWordLeft
| EditCommand::CutWordRight
| EditCommand::CutBigWordRight
| EditCommand::CutWordRightToNext
| EditCommand::CutBigWordRightToNext
| EditCommand::CutRightUntil(_)
| EditCommand::CutRightBefore(_)
| EditCommand::CutLeftUntil(_)
| EditCommand::CutLeftBefore(_) => true,
_ => false,
};

let new_undo_behavior = match (command, command.edit_type()) {
(_, EditType::MoveCursor) => UndoBehavior::MoveCursor,
(EditCommand::InsertChar(c), EditType::EditText) => UndoBehavior::InsertCharacter(*c),
Expand Down Expand Up @@ -218,6 +240,16 @@ impl Editor {

let cut_slice = &self.line_buffer.get_buffer()[deletion_range.clone()];
if !cut_slice.is_empty() {
let combined;
let cut_slice = {
if self.last_edit_was_cut {
let existing = self.cut_buffer.get().0;
combined = format!("{cut_slice}{existing}");
&combined
} else {
cut_slice
}
};
self.cut_buffer.set(cut_slice, ClipboardMode::Lines);
self.line_buffer.set_insertion_point(deletion_range.start);
self.line_buffer.clear_range(deletion_range);
Expand All @@ -241,6 +273,16 @@ impl Editor {
let deletion_range = self.line_buffer.insertion_point()..previous_offset;
let cut_slice = &self.line_buffer.get_buffer()[deletion_range.clone()];
if !cut_slice.is_empty() {
let combined;
let cut_slice = {
if self.last_edit_was_cut {
let existing = self.cut_buffer.get().0;
combined = format!("{cut_slice}{existing}");
&combined
} else {
cut_slice
}
};
self.cut_buffer.set(cut_slice, ClipboardMode::Normal);
self.line_buffer.clear_range(deletion_range);
}
Expand All @@ -249,6 +291,16 @@ impl Editor {
fn cut_from_end(&mut self) {
let cut_slice = &self.line_buffer.get_buffer()[self.line_buffer.insertion_point()..];
if !cut_slice.is_empty() {
let combined;
let cut_slice = {
if self.last_edit_was_cut {
let existing = self.cut_buffer.get().0;
combined = format!("{cut_slice}{existing}");
&combined
} else {
cut_slice
}
};
self.cut_buffer.set(cut_slice, ClipboardMode::Normal);
self.line_buffer.clear_to_end();
}
Expand All @@ -258,6 +310,16 @@ impl Editor {
let cut_slice = &self.line_buffer.get_buffer()
[self.line_buffer.insertion_point()..self.line_buffer.find_current_line_end()];
if !cut_slice.is_empty() {
let combined;
let cut_slice = {
if self.last_edit_was_cut {
let existing = self.cut_buffer.get().0;
combined = format!("{cut_slice}{existing}");
&combined
} else {
cut_slice
}
};
self.cut_buffer.set(cut_slice, ClipboardMode::Normal);
self.line_buffer.clear_to_line_end();
}
Expand Down Expand Up @@ -418,6 +480,16 @@ impl Editor {
&self.line_buffer.get_buffer()[self.line_buffer.insertion_point()..index + extra];

if !cut_slice.is_empty() {
let combined;
let cut_slice = {
if self.last_edit_was_cut {
let existing = self.cut_buffer.get().0;
combined = format!("{cut_slice}{existing}");
&combined
} else {
cut_slice
}
};
self.cut_buffer.set(cut_slice, ClipboardMode::Normal);

if before_char {
Expand All @@ -438,6 +510,16 @@ impl Editor {
&self.line_buffer.get_buffer()[index + extra..self.line_buffer.insertion_point()];

if !cut_slice.is_empty() {
let combined;
let cut_slice = {
if self.last_edit_was_cut {
let existing = self.cut_buffer.get().0;
combined = format!("{cut_slice}{existing}");
&combined
} else {
cut_slice
}
};
self.cut_buffer.set(cut_slice, ClipboardMode::Normal);

if before_char {
Expand Down

0 comments on commit 2dd7a31

Please sign in to comment.