Commit e6ba1dc3 authored by Ticki's avatar Ticki

Refactor, rename methods, add f command/motion

parent f754311a
......@@ -29,3 +29,5 @@ The bug causing these two bugs, is localised to be in position.rs. It resolves b
6) Out of bound (index) error
- [x] When typing the first char in a line in normal insert mode, it wont go to the next char.
- [ ] The modifier keys are only working for one command
......@@ -52,7 +52,7 @@ impl Editor {
editor.redraw();
loop {
let inp = editor.next_inst();
let inp = editor.get_inst();
editor.exec(inp);
editor.redraw();
editor.status_bar.mode = editor.cursor().mode.to_string();
......
......@@ -104,7 +104,7 @@ impl Editor {
Char('H') => self.cursor_mut().x = 0,
Char('r') => {
let (x, y) = self.pos();
self.text[y][x] = self.next_char();
self.text[y][x] = self.get_char();
},
Char('R') => {
self.cursor_mut().mode = Mode::Primitive(PrimitiveMode::Insert(
......@@ -113,7 +113,7 @@ impl Editor {
}));
},
Char('d') => {
let ins = self.next_inst();
let ins = self.get_inst();
if let Some(m) = self.to_motion_unbounded(ins) {
self.remove_rb(m);
}
......@@ -126,7 +126,7 @@ impl Editor {
if let Parameter::Int(n) = para {
self.goto((0, n - 1));
} else {
let inst = self.next_inst();
let inst = self.get_inst();
if let Some(m) = self.to_motion(inst) {
self.cursor_mut().x = m.0;
self.cursor_mut().y = m.1;
......@@ -145,13 +145,21 @@ impl Editor {
self.next_cursor();
},
Char('t') => {
let ch = self.next_char();
let ch = self.get_char();
let pos = self.next_ocur(ch, n);
if let Some(p) = pos {
self.goto(p);
}
},
Char('f') => {
let ch = self.get_char();
let pos = self.previous_ocur(ch, n);
if let Some(p) = pos {
self.goto(p);
}
},
Char(';') => {
self.cursor_mut().mode = Mode::Primitive(PrimitiveMode::Prompt);
},
......@@ -164,13 +172,10 @@ impl Editor {
// self.goto((0, self.text.len() - 1));
// },
Char(' ') => {
let next = self.next();
if let Some(p) = next {
self.goto(p);
}
self.next_cursor();
},
Char('z') => {
let Inst(param, cmd) = self.next_inst();
let Inst(param, cmd) = self.get_inst();
match param {
Parameter::Null => {
if let Some(m) = self.to_motion(Inst(param, cmd)) {
......
......@@ -69,7 +69,10 @@ Navigation:
- <numeral>g : Go to <numeral> line
- g<motion> : Do <motion>
- G : Go to the end of the document
- t<char> : Go to the next occurence of <char>
- <numeral>t<char> : Go to the next occurence
of <char>
- <numeral>f<char> : Go to the previous occurence
of <char>
Scrolling:
- z<motion> : Scroll <motion>
......@@ -93,6 +96,9 @@ Editing:
- o : Insert a new line
- ; : Go to prompt mode
Cursor:
- [space] : Go to the next cursor
# Insert
Insert text before the cursor.
......@@ -156,7 +162,10 @@ stated.
line
- <numeral>g : Go to <numeral> line
- G : Go to the end of the document
- t<char> : Go to the next occurence of <char>
- <numeral>t<char> : Go to the next occurence
of <char>
- <numeral>f<char> : Go to the previous occurence
of <char>
Tips'n'tricks
-------------
......
......@@ -21,7 +21,7 @@ impl Editor {
Char('H') => Some((0, self.y())),
Char('t') => {
let ch = self.next_char();
let ch = self.get_char();
if let Some(o) = self.next_ocur(ch, n.d()) {
Some(o)
......@@ -29,6 +29,16 @@ impl Editor {
None
}
},
Char('f') => {
let ch = self.get_char();
if let Some(o) = self.previous_ocur(ch, n.d()) {
Some(o)
} else {
None
}
},
Char(c) => {
self.status_bar.msg = format!("Motion not defined: '{}'", c);
self.redraw_status_bar();
......@@ -56,7 +66,7 @@ impl Editor {
Char('H') => Some((0, self.y() as isize)),
Char('t') => {
let ch = self.next_char();
let ch = self.get_char();
if let Some(o) = self.next_ocur(ch, n.d()) {
Some(to_signed_pos(o))
......@@ -64,6 +74,16 @@ impl Editor {
None
}
},
Char('f') => {
let ch = self.get_char();
if let Some(o) = self.previous_ocur(ch, n.d()) {
Some(to_signed_pos(o))
} else {
None
}
},
_ => None,
}
}
......
......@@ -11,10 +11,12 @@ impl Editor {
}
/// Get the previous position, i.e. the position before the cursor (*not* left to the cursor)
#[inline]
pub fn previous(&self) -> Option<(usize, usize)> {
self.before(self.pos())
}
/// Get the next position, i.e. the position after the cursor (*not* right to the cursor)
#[inline]
pub fn next(&self) -> Option<(usize, usize)> {
self.after(self.pos())
}
......@@ -37,6 +39,7 @@ impl Editor {
}
/// Get the position before a given position, i.e. a generalisation .before()
#[inline]
pub fn before(&self, (x, y): (usize, usize)) -> Option<(usize, usize)> {
if x == 0 {
if y > 0 {
......@@ -108,7 +111,6 @@ impl Editor {
}
/// Get n'th next ocurrence of a given charecter (relatively to the cursor)
#[inline]
pub fn next_ocur(&self, c: char, n: usize) -> Option<(usize, usize)> {
let mut dn = 0;
......@@ -135,4 +137,34 @@ impl Editor {
}
}
/// Get n'th previous ocurrence of a given charecter (relatively to the cursor)
pub fn previous_ocur(&self, c: char, n: usize) -> Option<(usize, usize)> {
let mut dn = 0;
let mut pos = self.before(self.pos());
loop {
match pos {
None => return None,
Some(mut p) => {
p = self.bound(p);
if self.text[p.1][p.0] == c {
dn += 1;
if dn == n {
return Some(p);
}
}
pos = self.before(p);
},
}
}
}
}
......@@ -34,7 +34,7 @@ impl Parameter {
impl Editor {
/// Get the next character input. Useful for commands taking a character as post-parameter,
/// such as r (replace).
pub fn next_char(&mut self) -> char {
pub fn get_char(&mut self) -> char {
loop {
if let EventOption::Key(k) = self.window.poll()
.unwrap_or(Event::new())
......@@ -50,7 +50,7 @@ impl Editor {
/// Get the next instruction, i.e. the next input of a command together with a numeral
/// parameter.
pub fn next_inst(&mut self) -> Inst {
pub fn get_inst(&mut self) -> Inst {
let mut n = 0;
let mut unset = true;
......
......@@ -15,7 +15,7 @@ impl Editor {
} else {
(self.x(), x)
};
for _ in a..(b + 1) {
for _ in a..b {
self.text[y].remove(a);
}
} else {
......
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