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-04-12 10:12:05 +00:00
|
|
|
use std::sync::Arc;
|
|
|
|
|
|
|
|
use ui::UiMutex;
|
|
|
|
use neovim_lib::{Handler, Neovim, NeovimApi, Session, Value, UiAttachOptions, CallError};
|
|
|
|
use ui_model::ModelRect;
|
|
|
|
use 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-04-03 15:16:04 +00:00
|
|
|
fn on_mouse(&mut self, on: bool) -> RepaintMode;
|
2016-05-04 14:59:51 +00:00
|
|
|
|
2017-04-03 15:16:04 +00:00
|
|
|
fn on_busy(&mut self, busy: bool) -> RepaintMode;
|
2017-04-19 08:40:53 +00:00
|
|
|
|
|
|
|
fn popupmenu_show(&mut self,
|
|
|
|
menu: &Vec<Vec<&str>>,
|
|
|
|
selected: i64,
|
|
|
|
row: u64,
|
|
|
|
col: u64)
|
|
|
|
-> RepaintMode;
|
|
|
|
|
|
|
|
fn popupmenu_hide(&mut self) -> RepaintMode;
|
|
|
|
|
|
|
|
fn popupmenu_select(&mut self, selected: i64) -> 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 {
|
2017-04-19 08:40:53 +00:00
|
|
|
($exp:expr) => ($exp.as_str().ok_or("Can't convert argument to string".to_owned())?)
|
2016-03-28 14:03:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
macro_rules! try_int {
|
2017-04-19 08:40:53 +00:00
|
|
|
($expr:expr) => ($expr.as_i64().ok_or("Can't convert argument to int".to_owned())?)
|
2016-04-03 15:13:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
macro_rules! try_uint {
|
2017-04-19 08:40:53 +00:00
|
|
|
($exp:expr) => ($exp.as_u64().ok_or("Can't convert argument to u64".to_owned())?)
|
2016-03-19 10:27:39 +00:00
|
|
|
}
|
|
|
|
|
2017-04-12 10:12:05 +00:00
|
|
|
pub fn initialize(shell: Arc<UiMutex<shell::State>>,
|
2017-04-19 08:40:53 +00:00
|
|
|
nvim_bin_path: Option<&String>,
|
|
|
|
external_popup: bool)
|
2017-04-12 10:12:05 +00:00
|
|
|
-> Result<Neovim> {
|
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
|
|
|
|
2017-04-12 10:12:05 +00:00
|
|
|
let mut nvim = Neovim::new(session);
|
2016-03-28 10:09:31 +00:00
|
|
|
|
2017-04-12 10:12:05 +00:00
|
|
|
nvim.session
|
|
|
|
.start_event_loop_handler(NvimHandler::new(shell));
|
2017-04-19 08:40:53 +00:00
|
|
|
let mut opts = UiAttachOptions::new();
|
|
|
|
opts.set_popupmenu_external(external_popup);
|
|
|
|
nvim.ui_attach(80, 24, opts)
|
2017-04-12 10:12:05 +00:00
|
|
|
.map_err(|e| Error::new(ErrorKind::Other, e))?;
|
|
|
|
nvim.command("runtime! ginit.vim")
|
|
|
|
.map_err(|e| Error::new(ErrorKind::Other, e))?;
|
2016-03-28 10:09:31 +00:00
|
|
|
|
2017-04-12 10:12:05 +00:00
|
|
|
Ok(nvim)
|
2016-03-28 10:09:31 +00:00
|
|
|
}
|
|
|
|
|
2017-04-12 10:12:05 +00:00
|
|
|
pub struct NvimHandler {
|
|
|
|
shell: Arc<UiMutex<shell::State>>,
|
2017-03-14 20:12:31 +00:00
|
|
|
}
|
|
|
|
|
2017-04-01 14:48:11 +00:00
|
|
|
impl NvimHandler {
|
2017-04-12 10:12:05 +00:00
|
|
|
pub fn new(shell: Arc<UiMutex<shell::State>>) -> NvimHandler {
|
|
|
|
NvimHandler { shell: shell }
|
2017-04-01 14:48:11 +00:00
|
|
|
}
|
|
|
|
|
2017-04-12 10:12:05 +00:00
|
|
|
fn nvim_cb(&self, method: &str, params: Vec<Value>) {
|
|
|
|
match method {
|
|
|
|
"redraw" => {
|
|
|
|
self.safe_call(move |ui| {
|
|
|
|
let mut repaint_mode = RepaintMode::Nothing;
|
|
|
|
|
|
|
|
for ev in ¶ms {
|
|
|
|
if let Some(ev_args) = ev.as_array() {
|
|
|
|
if let Some(ev_name) = ev_args[0].as_str() {
|
|
|
|
for ref local_args in ev_args.iter().skip(1) {
|
|
|
|
let args = match *local_args {
|
|
|
|
&Value::Array(ref ar) => ar.clone(),
|
|
|
|
_ => vec![],
|
|
|
|
};
|
|
|
|
let call_reapint_mode = call(ui, ev_name, &args)?;
|
|
|
|
repaint_mode = repaint_mode.join(&call_reapint_mode);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
println!("Unsupported event {:?}", ev_args);
|
2017-03-06 21:05:48 +00:00
|
|
|
}
|
|
|
|
} else {
|
2017-04-12 10:12:05 +00:00
|
|
|
println!("Unsupported event type {:?}", ev);
|
2017-03-06 21:05:48 +00:00
|
|
|
}
|
2016-03-31 12:08:32 +00:00
|
|
|
}
|
2017-03-06 21:05:48 +00:00
|
|
|
|
2017-04-12 10:12:05 +00:00
|
|
|
ui.on_redraw(&repaint_mode);
|
|
|
|
Ok(())
|
|
|
|
});
|
|
|
|
}
|
|
|
|
"Gui" => {
|
|
|
|
if params.len() > 0 {
|
|
|
|
if let Some(ev_name) = params[0].as_str().map(String::from) {
|
|
|
|
let args = params.iter().skip(1).cloned().collect();
|
|
|
|
self.safe_call(move |ui| {
|
|
|
|
call_gui_event(ui, &ev_name, &args)?;
|
|
|
|
ui.on_redraw(&RepaintMode::All);
|
|
|
|
Ok(())
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
println!("Unsupported event {:?}", params);
|
|
|
|
}
|
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
|
|
|
}
|
2017-04-12 10:12:05 +00:00
|
|
|
}
|
|
|
|
_ => {
|
|
|
|
println!("Notification {}({:?})", method, params);
|
2016-03-19 10:27:39 +00:00
|
|
|
}
|
|
|
|
}
|
2017-04-12 10:12:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
fn safe_call<F>(&self, cb: F)
|
|
|
|
where F: Fn(&mut shell::State) -> result::Result<(), String> + 'static + Send
|
|
|
|
{
|
|
|
|
let shell = self.shell.clone();
|
|
|
|
glib::idle_add(move || {
|
|
|
|
if let Err(msg) = cb(&mut shell.borrow_mut()) {
|
|
|
|
println!("Error call function: {}", msg);
|
|
|
|
}
|
|
|
|
glib::Continue(false)
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Handler for NvimHandler {
|
|
|
|
fn handle_notify(&mut self, name: &str, args: &Vec<Value>) {
|
|
|
|
self.nvim_cb(name, args.clone());
|
2017-03-06 21:05:48 +00:00
|
|
|
}
|
|
|
|
}
|
2016-03-31 13:52:22 +00:00
|
|
|
|
2017-04-12 10:12:05 +00:00
|
|
|
|
|
|
|
fn call_gui_event(ui: &mut shell::State,
|
|
|
|
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-04-12 10:12:05 +00:00
|
|
|
fn call(ui: &mut shell::State,
|
|
|
|
method: &str,
|
|
|
|
args: &Vec<Value>)
|
|
|
|
-> result::Result<RepaintMode, String> {
|
2017-04-19 08:40:53 +00:00
|
|
|
let repaint_mode = match method {
|
|
|
|
"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])),
|
|
|
|
"highlight_set" => {
|
|
|
|
if let Value::Map(ref attrs) = args[0] {
|
|
|
|
ui.on_highlight_set(attrs);
|
|
|
|
} else {
|
|
|
|
panic!("Supports only map value as argument");
|
|
|
|
}
|
|
|
|
RepaintMode::Nothing
|
2017-04-01 14:48:11 +00:00
|
|
|
}
|
2017-04-19 08:40:53 +00:00
|
|
|
"eol_clear" => ui.on_eol_clear(),
|
|
|
|
"set_scroll_region" => {
|
|
|
|
ui.on_set_scroll_region(try_uint!(args[0]),
|
|
|
|
try_uint!(args[1]),
|
|
|
|
try_uint!(args[2]),
|
|
|
|
try_uint!(args[3]));
|
|
|
|
RepaintMode::Nothing
|
|
|
|
}
|
|
|
|
"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])),
|
|
|
|
"mode_change" => ui.on_mode_change(try_str!(args[0])),
|
|
|
|
"mouse_on" => ui.on_mouse(true),
|
|
|
|
"mouse_off" => ui.on_mouse(false),
|
|
|
|
"busy_start" => ui.on_busy(true),
|
|
|
|
"busy_stop" => ui.on_busy(false),
|
|
|
|
"popupmenu_show" => {
|
|
|
|
let mut menu_items = Vec::new();
|
|
|
|
|
|
|
|
let items = args[0].as_array().ok_or("Error get menu list array")?;
|
|
|
|
for item in items {
|
|
|
|
let item_line: result::Result<Vec<_>, &str> = item.as_array()
|
|
|
|
.ok_or("Error get menu item array")?
|
|
|
|
.iter()
|
|
|
|
.map(|col| col.as_str().ok_or("Error get menu column"))
|
|
|
|
.collect();
|
|
|
|
menu_items.push(item_line?);
|
|
|
|
}
|
|
|
|
|
|
|
|
ui.popupmenu_show(&menu_items,
|
|
|
|
try_int!(args[1]),
|
|
|
|
try_uint!(args[2]),
|
|
|
|
try_uint!(args[3]))
|
|
|
|
}
|
|
|
|
"popupmenu_hide" => ui.popupmenu_hide(),
|
|
|
|
"popupmenu_select" => ui.popupmenu_select(try_int!(args[0])),
|
|
|
|
_ => {
|
|
|
|
println!("Event {}({:?})", method, args);
|
|
|
|
RepaintMode::Nothing
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
Ok(repaint_mode)
|
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);
|
|
|
|
}
|
2017-04-01 14:48:11 +00:00
|
|
|
_ => panic!("mode is worng"),
|
2017-03-20 15:18:08 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|