1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
extern crate racer;
extern crate shellexpand;
extern crate syntect;
extern crate termion;
extern crate unicode_width;

mod buffer;
mod clipboard;
mod core;
mod cursor;
pub mod draw;
mod indent;
mod mode;
mod rustfmt;
pub mod syntax;
mod text_object;
pub mod theme;

pub use buffer::Buffer;
use core::Core;
use mode::{Mode, Normal, Transition};

pub struct BufferMode<'a> {
    buf: Buffer<'a>,
    mode: Box<Mode>,
}

impl<'a> BufferMode<'a> {
    pub fn new(buf: Buffer<'a>) -> Self {
        Self {
            buf,
            mode: Box::new(Normal::default()),
        }
    }

    pub fn event(&mut self, event: termion::event::Event) -> bool {
        match self.mode.event(&mut self.buf, event) {
            Transition::Exit => {
                return true;
            }
            Transition::Trans(mut t) => {
                t.init(&mut self.buf);
                self.mode = t;
            }
            _ => {}
        }
        false
    }

    pub fn draw(&mut self, term: &mut draw::Term) {
        self.mode.draw(&self.buf, term)
    }
}