Commit 48587ba6 authored by Wesley Wiser's avatar Wesley Wiser

Allow the possibility of multiple buffers

parent 61f820a5
......@@ -22,8 +22,8 @@ impl Editor {
Char('j') => Some(self.down(n.d())),
Char('k') => Some(self.up(n.d())),
Char('g') => Some((0, n.or(1) - 1)),
Char('G') => Some((0, self.buffer.len() - 1)),
Char('L') => Some((self.buffer[y].len() - 1, y)),
Char('G') => Some((0, self.current_buffer().len() - 1)),
Char('L') => Some((self.current_buffer()[y].len() - 1, y)),
Char('H') => Some((0, y)),
Char('t') => {
......@@ -71,8 +71,8 @@ impl Editor {
Char('j') => Some(self.down_unbounded(n.d())),
Char('k') => Some(self.up_unbounded(n.d())),
Char('g') => Some((0, n.or(1) as isize - 1)),
Char('G') => Some((0, self.buffer.len() as isize - 1)),
Char('L') => Some(to_signed_pos((x, self.buffer[y].len()))),
Char('G') => Some((0, self.current_buffer().len() as isize - 1)),
Char('L') => Some(to_signed_pos((x, self.current_buffer()[y].len()))),
Char('H') => Some((0, y as isize)),
Char('t') => {
......
......@@ -26,22 +26,22 @@ impl Editor {
pub fn after(&self, n: usize, (x, y): (usize, usize)) -> Option<(usize, usize)> {
// TODO: Make this more idiomatic {
if x + n < self.buffer[y].len() {
if x + n < self.current_buffer()[y].len() {
Some((x + n, y))
} else {
if y + 1 >= self.buffer.len() {
if y + 1 >= self.current_buffer().len() {
None
} else {
let mut mv = n + x - self.buffer[y].len();
let mut mv = n + x - self.current_buffer()[y].len();
let mut ry = y + 1;
loop {
if mv < self.buffer[ry].len() {
if mv < self.current_buffer()[ry].len() {
return Some((mv, ry));
} else {
if ry + 1 < self.buffer.len() {
mv -= self.buffer[ry].len();
if ry + 1 < self.current_buffer().len() {
mv -= self.current_buffer()[ry].len();
ry += 1;
} else {
return None;
......@@ -68,11 +68,11 @@ impl Editor {
let mut ry = y - 1;
loop {
if mv <= self.buffer[ry].len() {
return Some((self.buffer[ry].len() - mv, ry));
if mv <= self.current_buffer()[ry].len() {
return Some((self.current_buffer()[ry].len() - mv, ry));
} else {
if ry > 0 && mv >= self.buffer[ry].len() {
mv -= self.buffer[ry].len();
if ry > 0 && mv >= self.current_buffer()[ry].len() {
mv -= self.current_buffer()[ry].len();
ry -= 1;
} else if ry == 0 {
return None;
......@@ -142,7 +142,7 @@ impl Editor {
let mut dn = 0;
let x = self.x();
for ch in self.buffer[self.y()].chars().skip(x) {
for ch in self.current_buffer()[self.y()].chars().skip(x) {
if dn == n {
if ch == c {
dn += 1;
......@@ -162,7 +162,7 @@ impl Editor {
let x = self.x();
let y = self.y();
for ch in self.buffer[y].chars().rev().skip(self.buffer[y].len() - x) {
for ch in self.current_buffer()[y].chars().rev().skip(self.current_buffer()[y].len() - x) {
if dn == n {
if ch == c {
dn += 1;
......
......@@ -31,13 +31,13 @@ impl Editor {
pub fn bound(&self, (x, mut y): (usize, usize), tight: bool) -> (usize, usize) {
y = if y >= self.buffer.len() {
self.buffer.len() - 1
y = if y >= self.current_buffer().len() {
self.current_buffer().len() - 1
} else {
y
};
let ln = self.buffer[y].len() + if tight {0} else {1};
let ln = self.current_buffer()[y].len() + if tight {0} else {1};
if x >= ln {
if ln == 0 {
(0, y)
......@@ -61,8 +61,8 @@ impl Editor {
pub fn bound_ver(&self, (x, mut y): (usize, usize)) -> (usize, usize) {
// Is this premature optimization? Yes, yes it is!
y = if y > self.buffer.len() - 1 {
self.buffer.len() - 1
y = if y > self.current_buffer().len() - 1 {
self.current_buffer().len() - 1
} else {
y
};
......
......@@ -53,12 +53,12 @@ impl Editor {
(Command(Normal), Char('o')) => {
let y = self.y();
let ind = if self.options.autoindent {
self.buffer.get_indent(y).to_owned()
self.current_buffer().get_indent(y).to_owned()
} else {
String::new()
};
let last = ind.len();
self.buffer.insert_line(y, ind.into());
self.current_buffer_mut().insert_line(y, ind.into());
self.goto((last, y + 1));
self.cursor_mut().mode =
Mode::Primitive(PrimitiveMode::Insert(InsertOptions {
......@@ -106,7 +106,7 @@ impl Editor {
self.goto(bounded);
}
(Command(Normal), Char('L')) => {
let ln_end = (self.buffer[self.y()].len(), self.y());
let ln_end = (self.current_buffer()[self.y()].len(), self.y());
self.goto(ln_end);
mov = true;
}
......@@ -119,10 +119,10 @@ impl Editor {
let c = self.get_char();
// If there is nothing in the current buffer
// ignore the command
if self.buffer[y].len() > 0 {
self.buffer[y].remove(x);
if self.current_buffer()[y].len() > 0 {
self.current_buffer_mut()[y].remove(x);
}
self.buffer[y].insert(x, c);
self.current_buffer_mut()[y].insert(x, c);
}
(Command(Normal), Char('R')) => {
self.cursor_mut().mode =
......@@ -137,7 +137,7 @@ impl Editor {
}
}
(Command(Normal), Char('G')) => {
let last = self.buffer.len() - 1;
let last = self.current_buffer().len() - 1;
self.goto((0, last));
mov = true;
}
......
......@@ -8,14 +8,14 @@ impl Editor {
#[inline]
pub fn delete(&mut self) {
let &Cursor{ x, y, .. } = self.cursor();
if x == self.buffer[y].len() {
if y + 1 < self.buffer.len() {
let s = self.buffer.remove_line(y + 1);
self.buffer[y].push_str(&s);
if x == self.current_buffer()[y].len() {
if y + 1 < self.current_buffer().len() {
let s = self.current_buffer_mut().remove_line(y + 1);
self.current_buffer_mut()[y].push_str(&s);
self.redraw_task = RedrawTask::Lines(y..y + 1);
}
} else if x < self.buffer[y].len() {
self.buffer[y].remove(x);
} else if x < self.current_buffer()[y].len() {
self.current_buffer_mut()[y].remove(x);
self.redraw_task = RedrawTask::LinesAfter(y);
}
......
......@@ -26,33 +26,33 @@ impl Editor {
let (mut x, mut y) = self.pos();
match (mode, k) {
(InsertMode::Insert, Key::Char('\n')) => {
let first_part = self.buffer[y][..x].to_owned();
let second_part = self.buffer[y][x..].to_owned();
let first_part = self.current_buffer()[y][..x].to_owned();
let second_part = self.current_buffer()[y][x..].to_owned();
self.buffer[y] = first_part;
self.current_buffer_mut()[y] = first_part;
let nl = if self.options.autoindent {
self.buffer.get_indent(y).to_owned()
self.current_buffer().get_indent(y).to_owned()
} else {
String::new()
};
let begin = nl.len();
self.buffer.insert_line(y, nl + &second_part);
self.current_buffer_mut().insert_line(y, nl + &second_part);
self.redraw_task = RedrawTask::LinesAfter(y);
self.goto((begin, y + 1));
},
(InsertMode::Insert, Key::Backspace) => self.backspace(),
(InsertMode::Insert, Key::Char(c)) => {
self.buffer[y].insert(x, c);
self.current_buffer_mut()[y].insert(x, c);
self.redraw_task = RedrawTask::Lines(y..y + 1);
let right = self.right(1, false);
self.goto(right);
},
(InsertMode::Replace, Key::Char(c)) => {
if x == self.buffer[y].len() {
if x == self.current_buffer()[y].len() {
let next = self.next(1);
if let Some(p) = next {
self.goto(p);
......@@ -61,15 +61,15 @@ impl Editor {
}
}
if self.buffer.len() != y {
if self.buffer[y].len() == x {
if self.current_buffer_mut().len() != y {
if self.current_buffer()[y].len() == x {
let next = self.next(1);
if let Some(p) = next {
self.goto(p);
}
} else {
self.buffer[y].remove(x);
self.buffer[y].insert(x, c);
self.current_buffer_mut()[y].remove(x);
self.current_buffer_mut()[y].insert(x, c);
}
}
let next = self.next(1);
......
......@@ -8,8 +8,8 @@ impl Editor {
let current = self.current();
if let Some(cur) = current {
self.buffer[y].remove(x);
self.buffer[y].insert(x, invert(cur));
self.current_buffer_mut()[y].remove(x);
self.current_buffer_mut()[y].insert(x, invert(cur));
}
if let Some(m) = self.next(1) {
self.goto(m);
......
......@@ -15,7 +15,7 @@ impl Editor {
} else {
(self.x(), x)
};
for _ in self.buffer[y].drain(a..b) {}
for _ in self.current_buffer_mut()[y].drain(a..b) {}
} else {
let (_, y) = self.bound((x as usize, y as usize), true);
// Full line mode
......@@ -27,10 +27,10 @@ impl Editor {
// TODO: Make this more idiomatic (drain)
for _ in a..(b + 1) {
if self.buffer.len() > 1 {
self.buffer.remove_line(a);
if self.current_buffer().len() > 1 {
self.current_buffer_mut().remove_line(a);
} else {
self.buffer[0] = String::new();
self.current_buffer_mut()[0] = String::new();
}
}
}
......
......@@ -21,7 +21,8 @@ impl Editor {
let mut con = String::new();
let _ = file.read_to_string(&mut con);
self.buffer = SplitBuffer::from_str(&con);
self.buffers.push(SplitBuffer::from_str(&con));
self.current_buffer_index = self.buffers.len() - 1;
self.hint();
FileStatus::Ok
} else {
......@@ -33,7 +34,7 @@ impl Editor {
pub fn write(&mut self, path: &str) -> FileStatus {
self.status_bar.file = path.to_owned();
if let Some(mut file) = File::create(path).ok() {
if file.write(self.buffer.to_string().as_bytes()).is_ok() {
if file.write(self.current_buffer().to_string().as_bytes()).is_ok() {
FileStatus::Ok
} else {
FileStatus::Other
......
......@@ -34,7 +34,7 @@ impl Editor {
let mut string = false;
for (y, row) in self.buffer.lines().enumerate() {
for (y, row) in (&mut self.buffers[self.current_buffer_index]).lines().enumerate() {
for (x, c) in row.chars().enumerate() {
// TODO: Move outta here
let color = if self.options.highlight {
......
......@@ -29,7 +29,7 @@ impl Editor {
#[inline]
pub fn current(&self) -> Option<char> {
let (x, y) = self.pos();
match self.buffer[y].chars().nth(x) {
match self.current_buffer()[y].chars().nth(x) {
Some(c) => Some(c),
None => None,
}
......
......@@ -18,8 +18,10 @@ pub struct Editor {
pub current_cursor: u8,
/// The cursors
pub cursors: Vec<Cursor>,
/// The buffer (document)
pub buffer: SplitBuffer,
/// The buffers (documents)
pub buffers: Vec<SplitBuffer>,
/// The current buffer index
pub current_buffer_index: usize,
/// The x coordinate of the scroll
pub scroll_x: usize,
/// The y coordinate of the scroll
......@@ -52,7 +54,8 @@ impl Editor {
let mut editor = Editor {
current_cursor: 0,
cursors: vec![Cursor::new()],
buffer: SplitBuffer::new(),
buffers: vec![SplitBuffer::new()],
current_buffer_index: 0,
scroll_x: 0,
scroll_y: 0,
window: *window, // ORBITAL SPECIFIC!
......@@ -68,7 +71,8 @@ impl Editor {
let mut editor = Editor {
current_cursor: 0,
cursors: vec![Cursor::new()],
buffer: SplitBuffer::new(),
buffers: vec![SplitBuffer::new()],
current_buffer_index: 0,
scroll_x: 0,
scroll_y: 0,
status_bar: StatusBar::new(),
......@@ -106,7 +110,19 @@ impl Editor {
let x = self.cursor().x;
let y = self.cursor().y;
self.buffer.focus_hint_y(y);
self.buffer.focus_hint_x(x);
self.current_buffer_mut().focus_hint_y(y);
self.current_buffer_mut().focus_hint_x(x);
}
#[inline]
/// Get a reference to the currently open buffer.
pub fn current_buffer(&self) -> &SplitBuffer {
&self.buffers[self.current_buffer_index]
}
#[inline]
/// Get a mutable reference to the currently open buffer.
pub fn current_buffer_mut(&mut self) -> &mut SplitBuffer {
&mut self.buffers[self.current_buffer_index]
}
}
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