diff --git a/src/nvim/ext.rs b/src/nvim/ext.rs new file mode 100644 index 0000000..9a69507 --- /dev/null +++ b/src/nvim/ext.rs @@ -0,0 +1,42 @@ + +use std::result; +use super::client::NeovimRef; + +use neovim_lib::{NeovimApi, CallError}; + +pub trait ErrorReport { + fn report_err(&self, nvim: &mut NeovimApi); + + fn ok_and_report(self, nvim: &mut NeovimApi) -> Option; +} + +impl ErrorReport for result::Result { + fn report_err(&self, _: &mut NeovimApi) { + if let Err(ref err) = *self { + error!("{}", err); + //nvim.report_error(&err_msg).expect("Error report error :)"); + } + } + + fn ok_and_report(self, nvim: &mut NeovimApi) -> Option { + self.report_err(nvim); + self.ok() + } +} + +pub trait NeovimExt: Sized { + fn non_blocked(self) -> Option; +} + +impl <'a>NeovimExt for NeovimRef<'a> { + fn non_blocked(mut self) -> Option { + self.get_mode().ok_and_report(&mut *self).and_then(|mode| { + mode.iter() + .find(|kv| { + kv.0.as_str().map(|key| key == "blocking").unwrap_or(false) + }) + .map(|kv| kv.1.as_bool().unwrap_or(false)) + .and_then(|block| if block { None } else { Some(self) }) + }) + } +} diff --git a/src/nvim/mod.rs b/src/nvim/mod.rs index 9ccdda9..3f4035a 100644 --- a/src/nvim/mod.rs +++ b/src/nvim/mod.rs @@ -4,11 +4,13 @@ mod handler; mod mode_info; mod redraw_handler; mod repaint_mode; +mod ext; pub use self::redraw_handler::{RedrawEvents, GuiApi}; pub use self::repaint_mode::RepaintMode; pub use self::client::{NeovimClient, NeovimClientAsync, NeovimRef}; pub use self::mode_info::{ModeInfo, CursorShape}; +pub use self::ext::{ErrorReport, NeovimExt}; use std::error; use std::fmt; @@ -18,7 +20,7 @@ use std::result; use std::sync::Arc; use std::time::Duration; -use neovim_lib::{Neovim, NeovimApi, Session, UiAttachOptions, CallError}; +use neovim_lib::{Neovim, NeovimApi, Session, UiAttachOptions}; use ui::UiMutex; use shell; @@ -162,23 +164,3 @@ pub fn post_start_init( Ok(()) } - -pub trait ErrorReport { - fn report_err(&self, nvim: &mut NeovimApi); - - fn ok_and_report(self, nvim: &mut NeovimApi) -> Option; -} - -impl ErrorReport for result::Result { - fn report_err(&self, _: &mut NeovimApi) { - if let Err(ref err) = *self { - println!("{}", err); - //nvim.report_error(&err_msg).expect("Error report error :)"); - } - } - - fn ok_and_report(self, nvim: &mut NeovimApi) -> Option { - self.report_err(nvim); - self.ok() - } -} diff --git a/src/shell.rs b/src/shell.rs index bf57a30..7d6f09a 100644 --- a/src/shell.rs +++ b/src/shell.rs @@ -21,9 +21,8 @@ use neovim_lib::neovim_api::Tabpage; use settings::{Settings, FontSource}; use ui_model::{UiModel, Attrs, ModelRect}; use color::{ColorModel, Color, COLOR_BLACK, COLOR_WHITE, COLOR_RED}; -use nvim; -use nvim::{RedrawEvents, GuiApi, RepaintMode, ErrorReport, NeovimClient, NeovimRef, - NeovimClientAsync}; +use nvim::{self, RedrawEvents, GuiApi, RepaintMode, ErrorReport, NeovimExt, NeovimClient, + NeovimRef, NeovimClientAsync}; use input; use input::keyval_to_input_string; use cursor::Cursor; @@ -119,6 +118,13 @@ impl State { } } + /// Return NeovimRef only if vim in non blocking state + /// + /// Note that this call also do neovim api call get_mode + pub fn nvim_non_blocked(&self) -> Option { + self.nvim().and_then(NeovimExt::non_blocked) + } + pub fn nvim(&self) -> Option { self.nvim.nvim() } @@ -607,11 +613,10 @@ impl Deref for Shell { } fn gtk_focus_in(state: &mut State) -> Inhibit { - // in case nvim want some input - this will freeze nvim-gtk - //if let Some(mut nvim) = state.nvim() { - // nvim.command("if exists('#FocusGained') | doautocmd FocusGained | endif") - // .report_err(&mut *nvim); - //} + if let Some(mut nvim) = state.nvim_non_blocked() { + nvim.command("if exists('#FocusGained') | doautocmd FocusGained | endif") + .report_err(&mut *nvim); + } state.im_context.focus_in(); state.cursor.as_mut().unwrap().enter_focus(); @@ -621,11 +626,10 @@ fn gtk_focus_in(state: &mut State) -> Inhibit { } fn gtk_focus_out(state: &mut State) -> Inhibit { - // in case nvim want some input - this will freeze nvim-gtk - //if let Some(mut nvim) = state.nvim() { - // nvim.command("if exists('#FocusLost') | doautocmd FocusLost | endif") - // .report_err(&mut *nvim); - //} + if let Some(mut nvim) = state.nvim_non_blocked() { + nvim.command("if exists('#FocusLost') | doautocmd FocusLost | endif") + .report_err(&mut *nvim); + } state.im_context.focus_out(); state.cursor.as_mut().unwrap().leave_focus();