use core::iter::Iterator;
use std::rc::Rc;
use std::cell::RefCell;
use crate::error::*;
mod history;
pub use history::*;
mod substitute;
mod verify;
pub use verify::*;
mod finding;
pub use finding::*;
mod editing;
pub use editing::*;
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct Line {
pub(crate)tag: RefCell<char>,
pub(crate)matched: RefCell<bool>,
pub(crate)text: Rc<String>,
}
pub type SelectionIter<'b> = std::iter::Map<
std::slice::Iter<'b, Line>,
for<'a> fn(&'a Line) -> &'a str
>;
pub type TaggedSelectionIter<'b> = std::iter::Map<
std::slice::Iter<'b, Line>,
for<'a> fn(&'a Line) -> (char, &'a str)
>;
#[derive(Clone, Debug)]
pub struct Buffer {
pub history: History<Line>,
pub clipboard: Vec<Line>,
}
impl Default for Buffer {
fn default() -> Self { Self::new() }
}
impl Buffer {
pub fn new() -> Self
{
Self{
history: History::new(),
clipboard: Vec::new(),
}
}
pub fn len(&self) -> usize { self.history.current().len() }
pub fn is_empty(&self) -> bool { self.history.current().is_empty() }
pub fn saved(&self) -> bool { self.history.saved() }
pub fn set_saved(&mut self) { self.history.set_saved() }
pub fn undo(&mut self,
steps: isize,
) -> Result<()> {
self.history.undo(steps)
}
pub fn get_selection(&self,
selection: (usize, usize),
) -> Result<SelectionIter> {
verify_selection(self, selection)?;
let tmp = self.history.current()[selection.0 - 1 .. selection.1]
.iter()
.map(get_selection_helper as fn(&Line) -> &str)
;
Ok(tmp)
}
pub fn get_tagged_selection(&self,
selection: (usize, usize),
) -> Result<TaggedSelectionIter> {
verify_selection(self, selection)?;
let tmp = self.history.current()[selection.0 - 1 .. selection.1]
.iter()
.map(get_tagged_selection_helper as fn(&Line) -> (char, &str))
;
Ok(tmp)
}
}
pub fn get_selection_helper(line: &Line) -> &str {
&line.text[..]
}
pub fn get_tagged_selection_helper(line: &Line) -> (char, &str) {
(*line.tag.borrow(), &line.text[..])
}