diff --git a/src/nvim.rs b/src/nvim.rs index cde1e27..b6cca69 100644 --- a/src/nvim.rs +++ b/src/nvim.rs @@ -1,13 +1,31 @@ use neovim_lib::{Neovim, NeovimApi, Session}; use std::io::{Result, Error, ErrorKind}; use std::sync::Arc; +use std::result; use ui_mutex::UiMutex; use rmp::Value; +use rmp::value::Integer; use ui::Ui; +use gtk; pub type SharedUi = Arc>; pub trait RedrawEvents { + fn on_cursor_goto(&mut self, row: u64, col: u64); +} + +macro_rules! try_str { + ($exp:expr) => (match $exp { + Value::String(ref val) => val.to_owned(), + _ => return Err("Can't convert argument to string".to_owned()) + }) +} + +macro_rules! try_int { + ($exp:expr) => (match $exp { + Value::Integer(Integer::U64(val)) => val, + _ => return Err("Can't convert argument to int".to_owned()) + }) } pub fn initialize(mut ui: Ui) -> Result { @@ -42,7 +60,9 @@ fn nvim_cb(ui: &SharedUi, method: &str, params: Vec) { for ev in params { if let Value::Array(ev_args) = ev { if let Value::String(ref ev_name) = ev_args[0] { - println!("Event {}", ev_name); + let mut args = vec![]; + args.extend_from_slice(&ev_args[1..]); + call(ui, ev_name, args); } else { println!("Unsupported event {:?}", ev_args); } @@ -54,3 +74,26 @@ fn nvim_cb(ui: &SharedUi, method: &str, params: Vec) { println!("Notification {}", method); } } + +fn call(ui: &SharedUi, method: &str, args: Vec) { + match method { + "cursor_goto" => { + safe_call(ui.clone(), move |ui| { + ui.borrow_mut().on_cursor_goto(try_int!(args[0]), try_int!(args[1])); + Ok(()) + }) + } + _ => println!("Event {}", method), + }; +} + +fn safe_call(mutex: SharedUi, cb: F) + where F: Fn(&UiMutex) -> result::Result<(), String> + 'static +{ + gtk::idle_add(move || { + if let Err(msg) = cb(&*mutex) { + println!("Error call function: {}", msg); + } + gtk::Continue(false) + }); +} diff --git a/src/ui.rs b/src/ui.rs index 2caceae..9a3b446 100644 --- a/src/ui.rs +++ b/src/ui.rs @@ -79,5 +79,8 @@ impl Ui { } impl RedrawEvents for Ui { + fn on_cursor_goto(&mut self, row: u64, col: u64) { + self.model.set_cursor(row, col); + } } diff --git a/src/ui_model.rs b/src/ui_model.rs index fb50497..a7face1 100644 --- a/src/ui_model.rs +++ b/src/ui_model.rs @@ -37,7 +37,7 @@ impl UiModel { } } - pub fn set_cursor(&mut self, col: u64, row: u64) { + pub fn set_cursor(&mut self, row: u64, col: u64) { self.cur_col = col; self.cur_row = row; } diff --git a/src/ui_mutex.rs b/src/ui_mutex.rs index 578f2ca..70d7702 100644 --- a/src/ui_mutex.rs +++ b/src/ui_mutex.rs @@ -1,8 +1,6 @@ use std::cell::{RefCell, RefMut}; use std::thread; -use std::sync::Arc; -use gtk; pub struct UiMutex { data: RefCell, @@ -28,16 +26,6 @@ impl UiMutex { self.data.borrow_mut() } - - pub fn safe_call(mutex: Arc>, cb: F) - where I: 'static, - F: Fn(&UiMutex) + 'static - { - gtk::idle_add(move || { - cb(&*mutex); - gtk::Continue(false) - }); - } }