Move back focus events, add check if nvim currently in nonblocking state

#24
This commit is contained in:
daa 2017-12-10 21:09:36 +03:00
parent bec3e60b6b
commit 7325837d07
3 changed files with 62 additions and 34 deletions

42
src/nvim/ext.rs Normal file
View File

@ -0,0 +1,42 @@
use std::result;
use super::client::NeovimRef;
use neovim_lib::{NeovimApi, CallError};
pub trait ErrorReport<T> {
fn report_err(&self, nvim: &mut NeovimApi);
fn ok_and_report(self, nvim: &mut NeovimApi) -> Option<T>;
}
impl<T> ErrorReport<T> for result::Result<T, CallError> {
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<T> {
self.report_err(nvim);
self.ok()
}
}
pub trait NeovimExt: Sized {
fn non_blocked(self) -> Option<Self>;
}
impl <'a>NeovimExt for NeovimRef<'a> {
fn non_blocked(mut self) -> Option<Self> {
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) })
})
}
}

View File

@ -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<T> {
fn report_err(&self, nvim: &mut NeovimApi);
fn ok_and_report(self, nvim: &mut NeovimApi) -> Option<T>;
}
impl<T> ErrorReport<T> for result::Result<T, CallError> {
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<T> {
self.report_err(nvim);
self.ok()
}
}

View File

@ -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<NeovimRef> {
self.nvim().and_then(NeovimExt::non_blocked)
}
pub fn nvim(&self) -> Option<NeovimRef> {
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();