2016-03-19 11:32:59 +00:00
|
|
|
use neovim_lib::{Neovim, NeovimApi, Session};
|
2016-03-19 10:27:39 +00:00
|
|
|
use std::io::{Result, Error, ErrorKind};
|
2016-03-25 09:51:28 +00:00
|
|
|
use std::sync::Arc;
|
2016-03-28 14:03:21 +00:00
|
|
|
use std::result;
|
2016-03-28 15:05:10 +00:00
|
|
|
use ui_model::UiModel;
|
2016-03-28 10:09:31 +00:00
|
|
|
use ui_mutex::UiMutex;
|
2016-03-19 10:27:39 +00:00
|
|
|
use rmp::Value;
|
2016-03-28 14:03:21 +00:00
|
|
|
use rmp::value::Integer;
|
2016-03-23 15:22:28 +00:00
|
|
|
use ui::Ui;
|
2016-03-28 14:14:10 +00:00
|
|
|
use glib;
|
2016-03-19 10:27:39 +00:00
|
|
|
|
2016-03-28 10:09:31 +00:00
|
|
|
pub type SharedUi = Arc<UiMutex<Ui>>;
|
2016-03-23 11:59:18 +00:00
|
|
|
|
|
|
|
pub trait RedrawEvents {
|
2016-03-28 14:03:21 +00:00
|
|
|
fn on_cursor_goto(&mut self, row: u64, col: u64);
|
2016-03-28 15:05:10 +00:00
|
|
|
|
|
|
|
fn on_put(&mut self, text: &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 {
|
|
|
|
($exp:expr) => (match $exp {
|
|
|
|
Value::Integer(Integer::U64(val)) => val,
|
|
|
|
_ => return Err("Can't convert argument to int".to_owned())
|
|
|
|
})
|
2016-03-19 10:27:39 +00:00
|
|
|
}
|
|
|
|
|
2016-03-28 10:09:31 +00:00
|
|
|
pub fn initialize(mut ui: Ui) -> Result<SharedUi> {
|
|
|
|
// let mut session = try!(Session::new_tcp("127.0.0.1:6666"));
|
|
|
|
let session = if cfg!(target_os = "windows") {
|
|
|
|
Session::new_child_path("E:\\Neovim\\bin\\nvim.exe").unwrap()
|
|
|
|
} else {
|
|
|
|
Session::new_child().unwrap()
|
|
|
|
};
|
|
|
|
let nvim = Neovim::new(session);
|
|
|
|
ui.set_nvim(nvim);
|
2016-03-28 15:05:10 +00:00
|
|
|
ui.model = UiModel::new(80, 24);
|
2016-03-28 10:09:31 +00:00
|
|
|
|
|
|
|
let sh_ui = Arc::new(UiMutex::new(ui));
|
|
|
|
|
|
|
|
|
|
|
|
{
|
|
|
|
let mut ui = (*sh_ui).borrow_mut();
|
|
|
|
let mut nvim = ui.nvim();
|
|
|
|
|
|
|
|
let moved_sh_ui = sh_ui.clone();
|
|
|
|
nvim.session.start_event_loop_cb(move |m, p| nvim_cb(&moved_sh_ui, m, p));
|
2016-03-19 11:32:59 +00:00
|
|
|
// fix neovim --embed bug to start embed mode
|
|
|
|
nvim.input("i").unwrap();
|
2016-03-19 10:27:39 +00:00
|
|
|
try!(nvim.ui_attach(80, 24, true).map_err(|e| Error::new(ErrorKind::Other, e)));
|
|
|
|
}
|
|
|
|
|
2016-03-28 10:09:31 +00:00
|
|
|
Ok(sh_ui)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn nvim_cb(ui: &SharedUi, method: &str, params: Vec<Value>) {
|
|
|
|
if method == "redraw" {
|
|
|
|
for ev in params {
|
|
|
|
if let Value::Array(ev_args) = ev {
|
|
|
|
if let Value::String(ref ev_name) = ev_args[0] {
|
2016-03-28 15:05:10 +00:00
|
|
|
let args = match ev_args[1] {
|
|
|
|
Value::Array(ref ar) => ar.clone(),
|
|
|
|
_ => vec![],
|
|
|
|
};
|
2016-03-28 14:03:21 +00:00
|
|
|
call(ui, ev_name, args);
|
2016-03-23 11:59:18 +00:00
|
|
|
} else {
|
2016-03-28 10:09:31 +00:00
|
|
|
println!("Unsupported event {:?}", ev_args);
|
2016-03-19 10:27:39 +00:00
|
|
|
}
|
2016-03-28 10:09:31 +00:00
|
|
|
} else {
|
|
|
|
println!("Unsupported event type {:?}", ev);
|
2016-03-19 10:27:39 +00:00
|
|
|
}
|
|
|
|
}
|
2016-03-28 10:09:31 +00:00
|
|
|
} else {
|
|
|
|
println!("Notification {}", method);
|
2016-03-19 10:27:39 +00:00
|
|
|
}
|
|
|
|
}
|
2016-03-28 14:03:21 +00:00
|
|
|
|
|
|
|
fn call(ui: &SharedUi, method: &str, args: Vec<Value>) {
|
|
|
|
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(())
|
|
|
|
})
|
2016-03-28 15:05:10 +00:00
|
|
|
},
|
|
|
|
"put" => {
|
|
|
|
safe_call(ui.clone(), move |ui| {
|
|
|
|
ui.borrow_mut().on_put(try_str!(args[0]));
|
|
|
|
Ok(())
|
|
|
|
})
|
2016-03-28 14:03:21 +00:00
|
|
|
}
|
|
|
|
_ => println!("Event {}", method),
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
fn safe_call<F>(mutex: SharedUi, cb: F)
|
2016-03-28 14:14:10 +00:00
|
|
|
where F: Fn(&UiMutex<Ui>) -> 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 || {
|
2016-03-28 14:03:21 +00:00
|
|
|
if let Err(msg) = cb(&*mutex) {
|
|
|
|
println!("Error call function: {}", msg);
|
|
|
|
}
|
2016-03-28 14:14:10 +00:00
|
|
|
glib::Continue(false)
|
2016-03-28 14:03:21 +00:00
|
|
|
});
|
|
|
|
}
|