Skip to main content

Edit

Enum Edit 

Source
#[non_exhaustive]
pub enum Edit {
Show 16 variants Insert(char), Backspace, Delete, Left, Right, WordLeft, WordRight, Home, End, KillToStart, KillToEnd, KillWordLeft, KillWordRight, Yank, HistoryPrev, HistoryNext,
}
Expand description

One editing command for an Editor.

Edit is the boundary between the terminal and the editor. The host reads key events however its platform delivers them — a raw-mode terminal library, a GUI text field, a test script — and maps each one to an Edit; the editor applies it to the line and reports whether anything changed. Keeping the mapping on the host’s side means the editor never touches a terminal and any keymap (Emacs, Vi-style, custom) is a lookup table away.

Motions and deletions work in display units: a visible character together with any zero-width characters attached to it, such as a combining accent. The cursor never stops inside a unit. A word is a run of identifier characters (Unicode XID_Continue: letters, digits, _, combining marks); everything else separates words.

The usual Emacs/readline bindings are noted on each variant as a guide; they are not built in.

The enum is #[non_exhaustive], so new commands can arrive in a minor release. Hosts build Edit values rather than matching on them, so this costs nothing in practice.

§Examples

use repl_lang::{Edit, Editor};

let mut editor = Editor::new();
for c in "print x".chars() {
    editor.apply(Edit::Insert(c));
}
assert!(editor.apply(Edit::WordLeft));    // cursor before `x`
assert!(editor.apply(Edit::KillToStart)); // removes "print "
assert_eq!(editor.line(), "x");
assert!(editor.apply(Edit::Yank));        // and puts it back
assert_eq!(editor.line(), "print x");

Variants (Non-exhaustive)§

This enum is marked as non-exhaustive
Non-exhaustive enums could have additional variants added in future. Therefore, when matching against variants of non-exhaustive enums, an extra wildcard arm must be added to account for any future variants.
§

Insert(char)

Inserts a character at the cursor and moves the cursor past it.

Control characters ('\n', '\t', escape, and the rest of the C0/C1 ranges) are refused: they have no printable form on a single line, and a terminal key handler should turn them into commands instead. So are the bidirectional formatting characters (U+061C, U+200E, U+200F, U+202A to U+202E, U+2066 to U+2069), which are invisible and change how the text around them is displayed.

§

Backspace

Deletes the display unit before the cursor. Backspace, Ctrl-H.

§

Delete

Deletes the display unit under the cursor. Delete, Ctrl-D on a non-empty line.

§

Left

Moves one display unit left. ←, Ctrl-B.

§

Right

Moves one display unit right. →, Ctrl-F.

§

WordLeft

Moves to the start of the current or previous word. Ctrl-←, Alt-B.

§

WordRight

Moves to the end of the current or next word. Ctrl-→, Alt-F.

§

Home

Moves to the start of the line. Home, Ctrl-A.

§

End

Moves to the end of the line. End, Ctrl-E.

§

KillToStart

Cuts everything before the cursor into the kill buffer. Ctrl-U.

§

KillToEnd

Cuts everything from the cursor to the end into the kill buffer. Ctrl-K.

§

KillWordLeft

Cuts the word before the cursor into the kill buffer. Ctrl-W, Alt-Backspace.

§

KillWordRight

Cuts the word after the cursor into the kill buffer. Alt-D.

§

Yank

Inserts the kill buffer at the cursor. Ctrl-Y.

§

HistoryPrev

Replaces the line with the previous (older) history entry. The first step saves the line being typed so HistoryNext can return to it. ↑, Ctrl-P.

§

HistoryNext

Replaces the line with the next (newer) history entry, or restores the line that was being typed when browsing started. ↓, Ctrl-N.

Trait Implementations§

Source§

impl Clone for Edit

Source§

fn clone(&self) -> Self

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Copy for Edit

Source§

impl Debug for Edit

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Eq for Edit

Source§

impl Hash for Edit

Source§

fn hash<__H: Hasher>(&self, state: &mut __H)

Feeds this value into the given Hasher. Read more
1.3.0 · Source§

fn hash_slice<H>(data: &[Self], state: &mut H)
where H: Hasher, Self: Sized,

Feeds a slice of this type into the given Hasher. Read more
Source§

impl PartialEq for Edit

Source§

fn eq(&self, other: &Self) -> bool

Equality operator ==. Read more
1.0.0 (const: unstable) · Source§

fn ne(&self, other: &Rhs) -> bool

Inequality operator !=. Read more
Source§

impl StructuralPartialEq for Edit

Auto Trait Implementations§

§

impl Forget for Edit

§

impl Freeze for Edit

§

impl Move for Edit

§

impl RefUnwindSafe for Edit

§

impl Send for Edit

§

impl Sync for Edit

§

impl Unpin for Edit

§

impl UnsafeUnpin for Edit

§

impl UnwindSafe for Edit

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = !

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, !>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.