neovim-gtk/src/nvim.rs

156 lines
4.3 KiB
Rust
Raw Normal View History

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-28 14:03:21 +00:00
use std::result;
2016-03-31 16:19:08 +00:00
use std::collections::HashMap;
2016-03-28 15:05:10 +00:00
use ui_model::UiModel;
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-31 10:09:34 +00:00
use ui;
use ui::Ui;
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 {
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-29 09:22:16 +00:00
fn on_clear(&mut self);
2016-03-29 09:43:01 +00:00
fn on_resize(&mut self, columns: u64, rows: u64);
2016-03-31 13:52:22 +00:00
fn on_redraw(&self);
2016-03-31 16:19:08 +00:00
fn on_highlight_set(&mut self, attrs: &HashMap<String, Value>);
fn on_eol_clear(&mut self);
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-31 10:09:34 +00:00
pub fn initialize(ui: &mut Ui) -> Result<()> {
// 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-31 10:09:34 +00:00
let mut nvim = ui.nvim();
2016-03-31 10:09:34 +00:00
nvim.session.start_event_loop_cb(move |m, p| nvim_cb(m, p));
// fix neovim --embed bug to start embed mode
nvim.input("i").unwrap();
try!(nvim.ui_attach(80, 24, true).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-31 10:09:34 +00:00
fn nvim_cb(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-31 12:08:32 +00:00
for ref local_args in ev_args.iter().skip(1) {
let args = match *local_args {
&Value::Array(ref ar) => ar.clone(),
_ => vec![],
};
call(ev_name, args);
}
2016-03-23 11:59:18 +00:00
} else {
println!("Unsupported event {:?}", ev_args);
2016-03-19 10:27:39 +00:00
}
} else {
println!("Unsupported event type {:?}", ev);
2016-03-19 10:27:39 +00:00
}
}
2016-03-31 13:52:22 +00:00
safe_call(move |ui| {
ui.on_redraw();
Ok(())
});
} else {
println!("Notification {}", method);
2016-03-19 10:27:39 +00:00
}
}
2016-03-28 14:03:21 +00:00
2016-03-31 10:09:34 +00:00
fn call(method: &str, args: Vec<Value>) {
2016-03-28 14:03:21 +00:00
match method {
"cursor_goto" => {
2016-03-31 10:09:34 +00:00
safe_call(move |ui| {
ui.on_cursor_goto(try_int!(args[0]), try_int!(args[1]));
2016-03-28 14:03:21 +00:00
Ok(())
})
2016-03-29 09:22:16 +00:00
}
2016-03-28 15:05:10 +00:00
"put" => {
2016-03-31 10:09:34 +00:00
safe_call(move |ui| {
ui.on_put(try_str!(args[0]));
2016-03-28 15:05:10 +00:00
Ok(())
})
2016-03-28 14:03:21 +00:00
}
2016-03-29 09:22:16 +00:00
"clear" => {
2016-03-31 10:09:34 +00:00
safe_call(move |ui| {
ui.on_clear();
2016-03-29 09:22:16 +00:00
Ok(())
})
}
2016-03-29 09:43:01 +00:00
"resize" => {
2016-03-31 10:09:34 +00:00
safe_call(move |ui| {
ui.on_resize(try_int!(args[0]), try_int!(args[1]));
2016-03-29 09:43:01 +00:00
Ok(())
});
}
2016-03-31 16:19:08 +00:00
"highlight_set" => {
safe_call(move |ui| {
if let Value::Map(ref attrs) = args[0] {
let attrs_map: HashMap<String, Value> = attrs.iter().map(|v| {
match v {
&(Value::String(ref key), ref value) => (key.clone(), value.clone()),
_ => panic!("attribute key must be string"),
}
}).collect();
ui.on_highlight_set(&attrs_map);
} else {
panic!("Supports only map value as argument");
}
Ok(())
});
}
"eol_clear" => {
safe_call(move |ui| {
ui.on_eol_clear();
Ok(())
})
}
2016-03-31 16:19:08 +00:00
_ => println!("Event {}({:?})", method, args),
2016-03-28 14:03:21 +00:00
};
}
2016-03-31 10:09:34 +00:00
fn safe_call<F>(cb: F)
where F: Fn(&mut 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-31 10:09:34 +00:00
ui::UI.with(|ui_cell| {
if let Err(msg) = cb(&mut *ui_cell.borrow_mut()) {
println!("Error call function: {}", msg);
}
});
2016-03-28 14:14:10 +00:00
glib::Continue(false)
2016-03-28 14:03:21 +00:00
});
}