Skip to content
Snippets Groups Projects
Commit 7956618c authored by Ktoks's avatar Ktoks
Browse files

Added e, w, $ functionality; added 0 stub

parent 184ab145
No related branches found
No related tags found
No related merge requests found
......@@ -48,7 +48,6 @@ impl Editor {
}
}
}
// }
}
/// Get the position before a given position, i.e. a generalisation .before(). Includes
......@@ -90,7 +89,18 @@ impl Editor {
pub fn right_unbounded(&self, n: usize) -> (isize, isize) {
((self.x() + n) as isize, self.y() as isize)
}
/// Get the position of the first word right of the cursor (horizontally bounded)
#[inline]
pub fn next_word(&self, n: usize, tight: bool) -> (usize, usize) {
let next_n = self.next_word_forward(n);
self.bound_hor((self.x() + (next_n), self.y()), tight)
}
/// Get the position of the first word end right of the cursor (horizontally bounded)
#[inline]
pub fn next_word_end(&self, n: usize, tight: bool) -> (usize, usize) {
let next_n = self.next_word_end_forward(n);
self.bound_hor((self.x() + (next_n), self.y()), tight)
}
/// Get the position of the character left to the cursor (horizontally bounded)
#[inline]
pub fn left(&self, n: usize) -> (usize, usize) {
......@@ -180,27 +190,60 @@ impl Editor {
None
}
/// Get next WORD forward
/// Get end of next WORD forward
/// "A WORD consists of a sequence of non-blank characters, separated with
/// whitespace. An empty line is also considered to be a WORD."
pub fn _next_word_forward(&self, n: usize) -> Option<usize> {
let mut dn = 0;
let mut x = self.x();
pub fn next_word_forward(&self, n_opt: usize) -> usize {
let mut word_count: usize = 0;
let x: usize = self.x();
let mut has_ws = false;
for (i, ch) in self.buffers.current_buffer()[self.y()]
for (i, current_char) in self.buffers.current_buffer()[self.y()]
.chars()
.skip(x)
.enumerate()
{
if ch.is_whitespace() {
dn += 1;
if dn == n {
x += i + 1;
return Some(x);
{
if current_char.is_whitespace() {
has_ws = true;
} else if has_ws && !current_char.is_whitespace() {
word_count += 1;
if word_count < n_opt - 1 {
has_ws = false;
} else {
return i;
}
}
}
0
}
None
/// Get beginning of next WORD forward
/// "A WORD consists of a sequence of non-blank characters, separated with
/// whitespace. An empty line is also considered to be a WORD."
pub fn next_word_end_forward(&self, n_opt: usize) -> usize {
let mut word_count: usize = 0;
let x: usize = self.x();
let mut word_char: bool = true;
let mut last: usize = 0;
for (i, current_char) in self.buffers.current_buffer()[self.y()]
.chars()
.skip(x)
.enumerate()
{
// if a word_char
if !current_char.is_whitespace() {
word_char = true;
last = i;
word_count += 1;
} else if current_char.is_whitespace() {
if word_char && word_count > n_opt {
return i - 1;
}
word_char = false;
}
}
last
}
}
}
\ No newline at end of file
......@@ -103,6 +103,16 @@ impl Editor {
self.goto(right);
mov = true;
}
(Command(Normal), Char('w')) => {
let next_word = self.next_word(n, true);
self.goto(next_word);
mov = true;
}
(Command(Normal), Char('e')) => {
let next_word = self.next_word_end(n, true);
self.goto(next_word);
mov = true;
}
(Command(Normal), Char('J')) => {
let down = self.down(15 * n);
self.goto(down);
......@@ -130,7 +140,20 @@ impl Editor {
mov = true;
}
}
(Command(Normal), Char('$')) => {
if self.buffers.current_buffer()[self.y()].len() != 0 {
let ln_end = (self.buffers.current_buffer()[self.y()].len() - 1, self.y());
self.goto(ln_end);
mov = true;
}
}
(Command(Normal), Char('H')) => {
println!("H pressed");
self.cursor_mut().x = 0;
mov = true;
}
(Command(Normal), Char('0')) => {
println!("0 pressed");
self.cursor_mut().x = 0;
mov = true;
}
......@@ -230,14 +253,14 @@ impl Editor {
mov = true;
}
}
(Command(Normal), Char('W')) => {
let pos = self._next_word_forward(n);
if let Some(p) = pos {
let y = self.y();
self.goto((p, y));
mov = true;
}
}
// (Command(Normal), Char('W')) => {
// let pos = self.next_word_forward(n);
// if let Some(p) = pos {
// let y = self.y();
// self.goto((p, y));
// mov = true;
// }
// }
(Command(Normal), Char(';')) => {
self.cursor_mut().mode = Mode::Primitive(PrimitiveMode::Prompt)
}
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment