Commit bea6176f authored by Ticki's avatar Ticki

Add append mode

parent 10770f6a
- [ ] Make editor.pos method and use that instead of
- [x] Make editor.pos method and use that instead of
- [ ] Add word navigation
Known bugs:
- [ ] When using `t` with a char that isn't in the document, Sodium will crash.
- [x] When using `t` with a char that isn't in the document, Sodium will crash.
- [ ] `d<motion>` does not do anything if: 1) the motion moves to the end of a line. 2) if the motion moves to the last line.
The bug causing these two bugs, is localised to be in position.rs. It resolves by returning a value one over bound x
- [ ] The x value is wrongly bounded. Reproduction:
- [x] The x value is wrongly bounded. Reproduction:
1) Make two lines:
- abc
- abcdef
......@@ -19,3 +19,11 @@ The bug causing these two bugs, is localised to be in position.rs. It resolves b
5) Type 2l.
6) Now go one down
7) You'll end up on e, even though it should be d
- [ ] Crashes when:
1) Write abc on line 1
2) Press o to go to the next line
3) Go to normal mode
4) Press a and go to append mode
5) Type text
6) Out of bound (index) error
......@@ -46,6 +46,13 @@ impl Editor {
mode: InsertMode::Insert,
}));
},
Char('a') => {
self.cursor_mut().mode = Mode::Primitive(PrimitiveMode::Insert(
InsertOptions {
mode: InsertMode::Append,
}));
},
Char('o') => {
// TODO: Autoindent (keep the same indentation level)
......
......@@ -5,12 +5,19 @@ impl Editor {
/// Redraw the window
pub fn redraw(&mut self) {
// TODO: Only draw when relevant for the window
let (x, y) = self.pos();
let (mut pos_x, pos_y) = self.pos();
// Redraw window
self.window.set(Color::rgb(25, 25, 25));
self.window.rect(8 * (x - self.scroll_y) as isize,
16 * (y - self.scroll_x) as isize,
pos_x += match self.cursor().mode {
Mode::Primitive(PrimitiveMode::Insert(InsertOptions {
mode: InsertMode::Append,
})) => 1,
_ => 0,
};
self.window.rect(8 * (pos_x - self.scroll_y) as isize,
16 * (pos_y - self.scroll_x) as isize,
8,
16,
Color::WHITE);
......@@ -39,7 +46,7 @@ impl Editor {
let c = if c == '\t' { ' ' } else { c };
if self.x() == x && self.y() == y {
if pos_x == x && pos_y == y {
self.window.char(8 * (x - self.scroll_y) as isize,
16 * (y - self.scroll_x) as isize,
c,
......
......@@ -77,6 +77,7 @@ Cursor management:
Editing:
- i : Go to insert mode
- a : Go to insert (append) mode
- r<char> : Replace the current char
with <char>
- R : Go to replace mode
......
......@@ -25,48 +25,55 @@ impl Editor {
pub fn insert(&mut self, k: Key, InsertOptions { mode: mode }: InsertOptions) {
let (mut x, mut y) = self.pos();
match mode {
InsertMode::Insert => match k {
Key::Char('\n') => {
let ln = self.text[y].clone();
let (slice, _) = ln.as_slices();
InsertMode::Insert | InsertMode::Append => {
let d = match mode {
InsertMode::Append => 1,
_ => 0,
};
let first_part = (&slice[..x]).clone();
let second_part = (&slice[x..]).clone();
match k {
Key::Char('\n') => {
let ln = self.text[y].clone();
let (slice, _) = ln.as_slices();
self.text[y] = VecDeque::from_iter(first_part.iter().map(|x| *x));
let first_part = (&slice[..x + d]).clone();
let second_part = (&slice[x + d..]).clone();
let ind = if self.options.autoindent {
self.get_indent(y)
} else {
VecDeque::new()
};
let begin = ind.len();
self.text[y] = VecDeque::from_iter(first_part.iter().map(|x| *x));
self.text.insert(y + 1, VecDeque::from_iter(
ind.into_iter().chain(second_part.iter().map(|x| *x))));
let ind = if self.options.autoindent {
self.get_indent(y)
} else {
VecDeque::new()
};
let begin = ind.len();
self.goto((begin, y + 1));
},
Key::Escape => { // Escape key
self.cursor_mut().mode = Mode::Command(CommandMode::Normal);
},
Key::Backspace => { // Backspace
let prev = self.previous();
if let Some(p) = prev {
if self.x() != 0 || self.y() != 0 {
self.text.insert(y + 1, VecDeque::from_iter(
ind.into_iter().chain(second_part.iter().map(|x| *x))));
self.goto((begin, y + 1));
},
Key::Escape => { // Escape key
self.cursor_mut().mode = Mode::Command(CommandMode::Normal);
},
Key::Backspace => { // Backspace
let prev = self.previous();
if let Some(p) = prev {
//if self.x() != 0 || self.y() != 0 {
self.goto(p);
self.delete();
//}
}
}
},
Key::Char(c) => {
//debugln!("length is: {}. \n y is: {} \n x is: {} \n x bound is: {}", self.text.len(), y, x, self.text[y].len());
self.text[y].insert(x, c);
},
Key::Char(c) => {
//debugln!("length is: {}. \n y is: {} \n x is: {} \n x bound is: {}", self.text.len(), y, x, self.text[y].len());
self.text[y].insert(x + d, c);
let right = self.right(1);
self.goto(right);
let right = self.right(1);
self.goto(right);
}
_ => {},
}
_ => {},
},
InsertMode::Replace => match k {
Key::Char(c) => {
......@@ -96,7 +103,6 @@ impl Editor {
},
_ => {},
},
_ => {},
}
}
......
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