Commit ffe0e585 authored by Ticki's avatar Ticki

Major refactoring

parent a920d2fd
- [ ] Fix `d<motion>`
- [ ] Add 't' command
- [ ] Clear up `let` borrowck bypassers
- [ ] Make editor.pos method and use that instead of
......@@ -62,18 +62,40 @@ impl Editor {
mode: InsertMode::Insert,
}));
}
Char('h') => self.goto_left(n),
Char('j') => self.goto_down(n),
Char('k') => self.goto_up(n),
Char('l') => self.goto_right(n),
Char('J') => self.goto_down(15 * n),
Char('K') => self.goto_up(15 * n),
Char('h') => {
let left = self.left(n);
self.goto(left);
},
Char('j') => {
let down = self.down(n);
self.goto(down);
},
Char('k') => {
let up = self.up(n);
self.goto(up);
},
Char('l') => {
let right = self.right(n);
self.goto(right);
},
Char('J') => {
let down = self.down(15 * n);
self.goto(down);
},
Char('K') => {
let up = self.up(15 * n);
self.goto(up);
},
Char('x') => self.delete(),
Char('X') => {
self.goto_previous();
let previous = self.previous();
self.goto(previous);
self.delete();
},
Char('L') => self.goto_ln_end(),
Char('L') => {
let ln_end = self.ln_end();
self.goto(ln_end);
},
Char('H') => self.cursor_mut().x = 0,
Char('r') => {
let x = self.x();
......@@ -126,7 +148,10 @@ impl Editor {
// Char('J') => {
// self.goto((0, self.text.len() - 1));
// },
Char(' ') => self.goto_next(),
Char(' ') => {
let next = self.next();
self.goto(next);
},
_ => {},
},
Primitive(Insert(opt)) => {
......
......@@ -53,32 +53,38 @@ impl Editor {
},
Key::Backspace => { // Backspace
if self.x() != 0 || self.y() != 0 {
self.goto_previous();
let prev = self.previous();
self.goto(prev);
self.delete();
}
},
Key::Char(c) => {
self.text[y].insert(x, c);
self.goto_next();
let right = self.right(1);
self.goto(right);
}
_ => {},
},
InsertMode::Replace => match k {
Key::Char(c) => {
if x == self.text[y].len() {
self.goto_next();
let next = self.next();
self.goto(next);
x = self.x();
y = self.y();
}
if self.text.len() != y {
if self.text[y].len() == x {
self.goto_next();
let next = self.next();
self.goto(next);
} else {
self.text[y][x] = c;
}
}
self.goto_next();
let next = self.next();
self.goto(next);
},
_ => {},
},
......
......@@ -5,13 +5,13 @@ impl Editor {
pub fn to_motion(&mut self, Inst(n, cmd): Inst) -> (usize, usize) {
use super::Key::*;
match cmd {
Char('h') => self.left_pos(n.d()),
Char('l') => self.right_pos(n.d()),
Char('j') => self.down_pos(n.d()),
Char('k') => self.up_pos(n.d()),
Char('h') => self.left(n.d()),
Char('l') => self.right(n.d()),
Char('j') => self.down(n.d()),
Char('k') => self.up(n.d()),
Char('g') => (0, n.or(1) - 1),
Char('G') => (0, self.text.len() - 1),
Char('L') => self.ln_end_pos(),
Char('L') => self.ln_end(),
Char('H') => (0, self.y()),
Char('t') => {
let ch = self.next_char();
......
use super::*;
// TODO! Clear up naming!
impl Editor {
/// Goto a given position
#[inline]
......@@ -8,113 +10,95 @@ impl Editor {
self.cursor_mut().y = y;
}
/// Get the position of the next char
/// Get the previous position
pub fn previous(&self) -> (usize, usize) {
self.before((self.x(), self.y()))
}
/// Get the next position
pub fn next(&self) -> (usize, usize) {
self.after((self.x(), self.y()))
}
/// Get position of char after a given char
#[inline]
pub fn next_pos(&self) -> (usize, usize) {
// TODO: Add numerals
if self.x() == self.text[self.y()].len() {
if self.y() < self.text.len() - 1 {
(0, self.y() + 1)
pub fn after(&self, (x, y): (usize, usize)) -> (usize, usize) {
if x == self.text[y].len() {
if y < self.text.len() - 1 {
(0, y + 1)
} else {
(self.x(), self.y())
(x, y)
}
} else {
(self.x() + 1, self.y())
(x + 1, y)
}
}
/// Get the position of previous char
#[inline]
pub fn previous_pos(&self) -> (usize, usize) {
if self.x() == 0 {
if self.y() > 0 {
(self.text[self.y() - 1].len(), self.y() - 1)
/// Get the position of the char before a given char's position
pub fn before(&self, (x, y): (usize, usize)) -> (usize, usize) {
if x == 0 {
if y > 0 {
(self.text[y - 1].len(), y - 1)
} else {
(self.x(), self.y())
(x, y)
}
} else {
(self.x() - 1, self.y())
(x - 1, y)
}
}
/// Goto the next char
#[inline]
pub fn goto_next(&mut self) {
let p = self.next_pos();
self.goto(p);
}
/// Goto the previous char
#[inline]
pub fn goto_previous(&mut self) {
let p = self.previous_pos();
self.goto(p);
}
/// Get the position of the right char
#[inline]
pub fn right_pos(&self, n: usize) -> (usize, usize) {
pub fn right(&self, n: usize) -> (usize, usize) {
(self.x() + n, self.y())
}
/// Goto the right char
#[inline]
pub fn goto_right(&mut self, n: usize) {
self.cursor_mut().x = self.right_pos(n).0;
}
/// Get the position of the left char
#[inline]
pub fn left_pos(&self, n: usize) -> (usize, usize) {
pub fn left(&self, n: usize) -> (usize, usize) {
if n <= self.x() {
(self.x() - n, self.y())
} else {
(0, self.y())
}
}
/// Goto the left char
#[inline]
pub fn goto_left(&mut self, n: usize) {
self.cursor_mut().x = self.left_pos(n).0;
}
/// Get the position of the char above the cursor
#[inline]
pub fn up_pos(&self, n: usize) -> (usize, usize) {
pub fn up(&self, n: usize) -> (usize, usize) {
if n <= self.y() {
(self.cursor().x, self.y() - n)
} else {
(self.cursor().x, 0)
}
}
/// Go a char up
#[inline]
pub fn goto_up(&mut self, n: usize) {
let p = self.up_pos(n);
self.goto(p);
}
/// Get the position under the char
#[inline]
pub fn down_pos(&self, n: usize) -> (usize, usize) {
pub fn down(&self, n: usize) -> (usize, usize) {
(self.cursor().x, self.y() + n)
}
/// Go down
#[inline]
pub fn goto_down(&mut self, n: usize) {
let p = self.down_pos(n);
self.goto(p);
}
/// Get the position of the end of the line
#[inline]
pub fn ln_end_pos(&self) -> (usize, usize) {
pub fn ln_end(&self) -> (usize, usize) {
(self.text[self.y()].len(), self.y())
}
/// Goto line end
/// Get next ocurrence of a given charecter
#[inline]
pub fn goto_ln_end(&mut self) {
let p = self.ln_end_pos();
self.goto(p);
pub fn next_ocur(&self, c: char) -> Option<(usize, usize)> {
loop {
let (mut x, mut y) = self.after((self.x(), self.y()));
if self.text[y][x] == c {
return Some((x, y));
}
if (x, y) == (self.text[self.text.len() - 1].len(), self.text.len() - 1) {
return None;
}
}
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment