neovim-gtk/src/nvim.rs

199 lines
6.0 KiB
Rust
Raw Normal View History

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;
2016-03-28 15:05:10 +00:00
use ui_model::UiModel;
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: &Vec<(Value, Value)>);
fn on_eol_clear(&mut self);
2016-04-03 15:13:18 +00:00
fn on_set_scroll_region(&mut self, top: u64, bot: u64, left: u64, right: u64);
fn on_scroll(&mut self, count: i64);
fn on_update_bg(&mut self, bg: i64);
fn on_update_fg(&mut self, fg: i64);
2016-05-04 06:23:39 +00:00
fn on_update_sp(&mut self, sp: i64);
2016-05-04 06:23:39 +00:00
fn on_mode_change(&mut self, mode: &str);
fn on_mouse_on(&mut self);
fn on_mouse_off(&mut self);
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
}
pub fn initialize(ui: &mut Ui, nvim_bin_path: Option<&String>) -> Result<()> {
let session = if let Some(path) = nvim_bin_path {
Session::new_child_path(path)?
} else {
Session::new_child()?
};
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-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));
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-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| {
for ev in &params {
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![],
};
call(ui, ev_name, &args)?;
}
} 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
}
ui.on_redraw();
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)?;
ui.on_redraw();
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
}
} 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-06 21:05:48 +00:00
fn call_gui_event(ui: &mut Ui, method: &str, args: &Vec<Value>) -> result::Result<(), String> {
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-06 21:05:48 +00:00
fn call(ui: &mut Ui, method: &str, args: &Vec<Value>) -> result::Result<(), String> {
2016-03-28 14:03:21 +00:00
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] {
ui.on_highlight_set(attrs);
2017-03-06 21:05:48 +00:00
} else {
panic!("Supports only map value as argument");
}
}
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-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])),
"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(),
2016-03-31 16:19:08 +00:00
_ => println!("Event {}({:?})", method, args),
2016-03-28 14:03:21 +00:00
};
2017-03-06 21:05:48 +00:00
Ok(())
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 || {
2017-02-26 19:33:44 +00:00
ui::UI.with(|ui_cell| if let Err(msg) = cb(&mut *ui_cell.borrow_mut()) {
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
}
impl<T> ErrorReport for result::Result<T, CallError> {
2017-03-09 13:18:13 +00:00
fn report_err(&self, _: &mut NeovimApi) {
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 :)");
}
}
}