2017-03-11 20:06:45 +00:00
|
|
|
use neovim_lib::{Neovim, NeovimApi, Session, Value, Integer, UiAttachOptions, CallError};
|
2016-03-19 10:27:39 +00:00
|
|
|
use std::io::{Result, Error, ErrorKind};
|
2016-03-28 14:03:21 +00:00
|
|
|
use std::result;
|
2017-03-17 22:18:41 +00:00
|
|
|
use ui_model::{UiModel, ModelRect};
|
2016-03-31 10:09:34 +00:00
|
|
|
use ui;
|
2017-03-15 15:33:47 +00:00
|
|
|
use shell::Shell;
|
2016-03-28 14:14:10 +00:00
|
|
|
use glib;
|
2016-03-19 10:27:39 +00:00
|
|
|
|
2016-03-23 11:59:18 +00:00
|
|
|
pub trait RedrawEvents {
|
2017-03-18 09:45:05 +00:00
|
|
|
fn on_cursor_goto(&mut self, row: u64, col: u64) -> RepaintMode;
|
2016-03-28 15:05:10 +00:00
|
|
|
|
2017-03-18 09:45:05 +00:00
|
|
|
fn on_put(&mut self, text: &str) -> RepaintMode;
|
2016-03-29 09:22:16 +00:00
|
|
|
|
2017-03-18 09:45:05 +00:00
|
|
|
fn on_clear(&mut self) -> RepaintMode;
|
2016-03-29 09:43:01 +00:00
|
|
|
|
2017-03-18 09:45:05 +00:00
|
|
|
fn on_resize(&mut self, columns: u64, rows: u64) -> RepaintMode;
|
2016-03-31 13:52:22 +00:00
|
|
|
|
2017-03-17 22:18:41 +00:00
|
|
|
fn on_redraw(&self, mode: &RepaintMode);
|
2016-03-31 16:19:08 +00:00
|
|
|
|
2017-03-18 09:45:05 +00:00
|
|
|
fn on_highlight_set(&mut self, attrs: &Vec<(Value, Value)>) -> RepaintMode;
|
2016-04-03 13:04:52 +00:00
|
|
|
|
2017-03-18 09:45:05 +00:00
|
|
|
fn on_eol_clear(&mut self) -> RepaintMode;
|
2016-04-03 15:13:18 +00:00
|
|
|
|
2017-03-18 09:45:05 +00:00
|
|
|
fn on_set_scroll_region(&mut self, top: u64, bot: u64, left: u64, right: u64) -> RepaintMode;
|
2016-04-03 15:13:18 +00:00
|
|
|
|
2017-03-18 09:45:05 +00:00
|
|
|
fn on_scroll(&mut self, count: i64) -> RepaintMode;
|
2016-04-05 22:04:40 +00:00
|
|
|
|
2017-03-18 09:45:05 +00:00
|
|
|
fn on_update_bg(&mut self, bg: i64) -> RepaintMode;
|
2016-04-05 22:04:40 +00:00
|
|
|
|
2017-03-18 09:45:05 +00:00
|
|
|
fn on_update_fg(&mut self, fg: i64) -> RepaintMode;
|
2016-05-04 06:23:39 +00:00
|
|
|
|
2017-03-18 09:45:05 +00:00
|
|
|
fn on_update_sp(&mut self, sp: i64) -> RepaintMode;
|
2017-03-09 10:19:44 +00:00
|
|
|
|
2017-03-18 09:45:05 +00:00
|
|
|
fn on_mode_change(&mut self, mode: &str) -> RepaintMode;
|
2016-05-04 14:59:51 +00:00
|
|
|
|
2017-03-18 09:45:05 +00:00
|
|
|
fn on_mouse_on(&mut self) -> RepaintMode;
|
2016-05-04 14:59:51 +00:00
|
|
|
|
2017-03-18 09:45:05 +00:00
|
|
|
fn on_mouse_off(&mut self) -> RepaintMode;
|
2016-03-28 14:03:21 +00:00
|
|
|
}
|
|
|
|
|
2017-03-06 21:05:48 +00:00
|
|
|
pub trait GuiApi {
|
|
|
|
fn set_font(&mut self, font_desc: &str);
|
|
|
|
}
|
|
|
|
|
2016-03-28 14:03:21 +00:00
|
|
|
macro_rules! try_str {
|
|
|
|
($exp:expr) => (match $exp {
|
2016-03-28 15:05:10 +00:00
|
|
|
Value::String(ref val) => val,
|
2016-03-28 14:03:21 +00:00
|
|
|
_ => return Err("Can't convert argument to string".to_owned())
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
macro_rules! try_int {
|
2016-04-03 15:13:18 +00:00
|
|
|
($expr:expr) => (match $expr {
|
|
|
|
Value::Integer(Integer::U64(val)) => val as i64,
|
|
|
|
Value::Integer(Integer::I64(val)) => val,
|
|
|
|
_ => return Err("Can't convert argument to int".to_owned())
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
macro_rules! try_uint {
|
2016-03-28 14:03:21 +00:00
|
|
|
($exp:expr) => (match $exp {
|
|
|
|
Value::Integer(Integer::U64(val)) => val,
|
2016-04-03 15:13:18 +00:00
|
|
|
_ => return Err("Can't convert argument to u64".to_owned())
|
2016-03-28 14:03:21 +00:00
|
|
|
})
|
2016-03-19 10:27:39 +00:00
|
|
|
}
|
|
|
|
|
2017-03-15 15:33:47 +00:00
|
|
|
pub fn initialize(ui: &mut Shell,
|
2017-03-14 20:12:31 +00:00
|
|
|
nvim_bin_path: Option<&String>)
|
2017-03-14 19:31:56 +00:00
|
|
|
-> Result<()> {
|
2017-03-07 15:20:48 +00:00
|
|
|
let session = if let Some(path) = nvim_bin_path {
|
|
|
|
Session::new_child_path(path)?
|
2016-03-28 10:09:31 +00:00
|
|
|
} else {
|
2017-03-07 15:20:48 +00:00
|
|
|
Session::new_child()?
|
2016-03-28 10:09:31 +00:00
|
|
|
};
|
2017-03-07 15:20:48 +00:00
|
|
|
|
2016-03-28 10:09:31 +00:00
|
|
|
let nvim = Neovim::new(session);
|
|
|
|
ui.set_nvim(nvim);
|
2016-04-06 08:31:00 +00:00
|
|
|
ui.model = UiModel::new(24, 80);
|
2016-03-28 10:09:31 +00:00
|
|
|
|
2016-03-31 10:09:34 +00:00
|
|
|
let mut nvim = ui.nvim();
|
2016-03-28 10:09:31 +00:00
|
|
|
|
2016-03-31 10:09:34 +00:00
|
|
|
nvim.session.start_event_loop_cb(move |m, p| nvim_cb(m, p));
|
2017-03-11 20:06:45 +00:00
|
|
|
nvim.ui_attach(80, 24, UiAttachOptions::new()).map_err(|e| Error::new(ErrorKind::Other, e))?;
|
2017-03-07 10:44:28 +00:00
|
|
|
nvim.command("runtime! ginit.vim").map_err(|e| Error::new(ErrorKind::Other, e))?;
|
2016-03-19 10:27:39 +00:00
|
|
|
|
2016-03-31 10:09:34 +00:00
|
|
|
Ok(())
|
2016-03-28 10:09:31 +00:00
|
|
|
}
|
|
|
|
|
2017-03-14 20:12:31 +00:00
|
|
|
pub fn open_file(nvim: &mut NeovimApi, file: Option<&String>) {
|
|
|
|
if let Some(file_name) = file {
|
|
|
|
nvim.command(&format!("e {}", file_name)).report_err(nvim);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-03-31 10:09:34 +00:00
|
|
|
fn nvim_cb(method: &str, params: Vec<Value>) {
|
2017-03-06 21:05:48 +00:00
|
|
|
match method {
|
|
|
|
"redraw" => {
|
|
|
|
safe_call(move |ui| {
|
2017-03-18 09:45:05 +00:00
|
|
|
let mut repaint_mode = RepaintMode::Nothing;
|
|
|
|
|
2017-03-06 21:05:48 +00:00
|
|
|
for ev in ¶ms {
|
|
|
|
if let &Value::Array(ref ev_args) = ev {
|
|
|
|
if let Value::String(ref ev_name) = ev_args[0] {
|
|
|
|
for ref local_args in ev_args.iter().skip(1) {
|
|
|
|
let args = match *local_args {
|
|
|
|
&Value::Array(ref ar) => ar.clone(),
|
|
|
|
_ => vec![],
|
|
|
|
};
|
2017-03-18 09:45:05 +00:00
|
|
|
let call_reapint_mode = call(ui, ev_name, &args)?;
|
|
|
|
repaint_mode = repaint_mode.join(&call_reapint_mode);
|
2017-03-06 21:05:48 +00:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
println!("Unsupported event {:?}", ev_args);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
println!("Unsupported event type {:?}", ev);
|
2016-03-31 12:08:32 +00:00
|
|
|
}
|
2017-03-06 21:05:48 +00:00
|
|
|
}
|
|
|
|
|
2017-03-18 09:45:05 +00:00
|
|
|
ui.on_redraw(&repaint_mode);
|
2017-03-06 21:05:48 +00:00
|
|
|
Ok(())
|
|
|
|
});
|
|
|
|
}
|
|
|
|
"Gui" => {
|
|
|
|
if params.len() > 0 {
|
|
|
|
if let Value::String(ev_name) = params[0].clone() {
|
|
|
|
let args = params.iter().skip(1).cloned().collect();
|
|
|
|
safe_call(move |ui| {
|
|
|
|
call_gui_event(ui, &ev_name, &args)?;
|
2017-03-17 22:18:41 +00:00
|
|
|
ui.on_redraw(&RepaintMode::All);
|
2017-03-06 21:05:48 +00:00
|
|
|
Ok(())
|
|
|
|
});
|
2016-03-23 11:59:18 +00:00
|
|
|
} else {
|
2017-03-06 21:05:48 +00:00
|
|
|
println!("Unsupported event {:?}", params);
|
2016-03-19 10:27:39 +00:00
|
|
|
}
|
2016-03-28 10:09:31 +00:00
|
|
|
} else {
|
2017-03-06 21:05:48 +00:00
|
|
|
println!("Unsupported event {:?}", params);
|
2016-03-19 10:27:39 +00:00
|
|
|
}
|
|
|
|
}
|
2017-03-06 21:05:48 +00:00
|
|
|
_ => {
|
|
|
|
println!("Notification {}({:?})", method, params);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2016-03-31 13:52:22 +00:00
|
|
|
|
2017-03-15 15:33:47 +00:00
|
|
|
fn call_gui_event(ui: &mut Shell, method: &str, args: &Vec<Value>) -> result::Result<(), String> {
|
2017-03-06 21:05:48 +00:00
|
|
|
match method {
|
|
|
|
"Font" => ui.set_font(try_str!(args[0])),
|
|
|
|
_ => return Err(format!("Unsupported event {}({:?})", method, args)),
|
2016-03-19 10:27:39 +00:00
|
|
|
}
|
2017-03-06 21:05:48 +00:00
|
|
|
Ok(())
|
2016-03-19 10:27:39 +00:00
|
|
|
}
|
2016-03-28 14:03:21 +00:00
|
|
|
|
2017-03-18 09:45:05 +00:00
|
|
|
fn call(ui: &mut Shell, method: &str, args: &Vec<Value>) -> result::Result<RepaintMode, String> {
|
|
|
|
Ok(match method {
|
2017-03-06 21:05:48 +00:00
|
|
|
"cursor_goto" => ui.on_cursor_goto(try_uint!(args[0]), try_uint!(args[1])),
|
|
|
|
"put" => ui.on_put(try_str!(args[0])),
|
|
|
|
"clear" => ui.on_clear(),
|
|
|
|
"resize" => ui.on_resize(try_uint!(args[0]), try_uint!(args[1])),
|
2016-03-31 16:19:08 +00:00
|
|
|
"highlight_set" => {
|
2017-03-06 21:05:48 +00:00
|
|
|
if let Value::Map(ref attrs) = args[0] {
|
2017-03-10 09:32:58 +00:00
|
|
|
ui.on_highlight_set(attrs);
|
2017-03-06 21:05:48 +00:00
|
|
|
} else {
|
|
|
|
panic!("Supports only map value as argument");
|
|
|
|
}
|
2017-03-18 09:45:05 +00:00
|
|
|
RepaintMode::Nothing
|
2016-04-03 13:04:52 +00:00
|
|
|
}
|
2017-03-06 21:05:48 +00:00
|
|
|
"eol_clear" => ui.on_eol_clear(),
|
2016-04-03 15:13:18 +00:00
|
|
|
"set_scroll_region" => {
|
2017-03-06 21:05:48 +00:00
|
|
|
ui.on_set_scroll_region(try_uint!(args[0]),
|
|
|
|
try_uint!(args[1]),
|
|
|
|
try_uint!(args[2]),
|
|
|
|
try_uint!(args[3]));
|
2017-03-18 09:45:05 +00:00
|
|
|
RepaintMode::Nothing
|
2016-05-04 14:59:51 +00:00
|
|
|
}
|
2017-03-06 21:05:48 +00:00
|
|
|
"scroll" => ui.on_scroll(try_int!(args[0])),
|
|
|
|
"update_bg" => ui.on_update_bg(try_int!(args[0])),
|
|
|
|
"update_fg" => ui.on_update_fg(try_int!(args[0])),
|
2017-03-09 10:19:44 +00:00
|
|
|
"update_sp" => ui.on_update_sp(try_int!(args[0])),
|
2017-03-06 21:05:48 +00:00
|
|
|
"mode_change" => ui.on_mode_change(try_str!(args[0])),
|
|
|
|
"mouse_on" => ui.on_mouse_on(),
|
|
|
|
"mouse_off" => ui.on_mouse_off(),
|
2017-03-18 09:45:05 +00:00
|
|
|
_ => {
|
|
|
|
println!("Event {}({:?})", method, args);
|
|
|
|
RepaintMode::Nothing
|
|
|
|
},
|
|
|
|
})
|
2016-03-28 14:03:21 +00:00
|
|
|
}
|
|
|
|
|
2016-03-31 10:09:34 +00:00
|
|
|
fn safe_call<F>(cb: F)
|
2017-03-15 15:33:47 +00:00
|
|
|
where F: Fn(&mut Shell) -> result::Result<(), String> + 'static + Send
|
2016-03-28 14:03:21 +00:00
|
|
|
{
|
2016-03-28 14:14:10 +00:00
|
|
|
glib::idle_add(move || {
|
2017-03-15 15:33:47 +00:00
|
|
|
ui::UI.with(|ui_cell| if let Err(msg) = cb(&mut ui_cell.borrow_mut().shell) {
|
2017-02-26 19:33:44 +00:00
|
|
|
println!("Error call function: {}", msg);
|
2016-03-31 10:09:34 +00:00
|
|
|
});
|
2016-03-28 14:14:10 +00:00
|
|
|
glib::Continue(false)
|
2016-03-28 14:03:21 +00:00
|
|
|
});
|
|
|
|
}
|
2017-03-08 19:22:58 +00:00
|
|
|
|
|
|
|
pub trait ErrorReport {
|
2017-03-09 13:18:13 +00:00
|
|
|
fn report_err(&self, nvim: &mut NeovimApi);
|
2017-03-08 19:22:58 +00:00
|
|
|
}
|
|
|
|
|
2017-03-11 20:06:45 +00:00
|
|
|
impl<T> ErrorReport for result::Result<T, CallError> {
|
2017-03-09 13:18:13 +00:00
|
|
|
fn report_err(&self, _: &mut NeovimApi) {
|
2017-03-11 20:06:45 +00:00
|
|
|
if let &Err(ref err) = self {
|
|
|
|
println!("{}", err);
|
2017-03-08 19:22:58 +00:00
|
|
|
//nvim.report_error(&err_msg).expect("Error report error :)");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2017-03-17 22:18:41 +00:00
|
|
|
|
2017-03-18 09:45:05 +00:00
|
|
|
#[derive(Clone)]
|
2017-03-17 22:18:41 +00:00
|
|
|
pub enum RepaintMode {
|
2017-03-18 09:45:05 +00:00
|
|
|
Nothing,
|
2017-03-17 22:18:41 +00:00
|
|
|
All,
|
|
|
|
Area(ModelRect),
|
|
|
|
}
|
|
|
|
|
2017-03-18 09:45:05 +00:00
|
|
|
impl RepaintMode {
|
|
|
|
pub fn join(&self, mode: &RepaintMode) -> RepaintMode {
|
|
|
|
match (self, mode) {
|
|
|
|
(&RepaintMode::Nothing, m) => m.clone(),
|
|
|
|
(m, &RepaintMode::Nothing) => m.clone(),
|
|
|
|
(&RepaintMode::All, _) => RepaintMode::All,
|
|
|
|
(_, &RepaintMode::All) => RepaintMode::All,
|
|
|
|
(&RepaintMode::Area(ref mr1), &RepaintMode::Area(ref mr2)) => {
|
|
|
|
let mut area = mr1.clone();
|
|
|
|
area.join(mr2);
|
|
|
|
RepaintMode::Area(area)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2017-03-20 15:18:08 +00:00
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
|
|
|
use super::*;
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_mode() {
|
|
|
|
let mode = RepaintMode::Area(ModelRect::point(1, 1));
|
|
|
|
mode.join(&RepaintMode::Nothing);
|
|
|
|
|
|
|
|
match mode {
|
|
|
|
RepaintMode::Area(ref rect) => {
|
|
|
|
assert_eq!(1, rect.top);
|
|
|
|
assert_eq!(1, rect.bot);
|
|
|
|
assert_eq!(1, rect.left);
|
|
|
|
assert_eq!(1, rect.right);
|
|
|
|
}
|
|
|
|
_ => panic!("mode is worng")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|