2017-07-06 09:41:35 +00:00
|
|
|
use std::error;
|
|
|
|
use std::fmt;
|
2017-05-24 11:00:23 +00:00
|
|
|
use std::env;
|
2017-05-15 14:44:37 +00:00
|
|
|
use std::process::{Stdio, Command};
|
2016-03-28 14:03:21 +00:00
|
|
|
use std::result;
|
2017-04-12 10:12:05 +00:00
|
|
|
use std::sync::Arc;
|
2017-07-06 09:41:35 +00:00
|
|
|
use std::ops::{Deref, DerefMut};
|
2017-04-12 10:12:05 +00:00
|
|
|
|
2017-05-27 08:56:47 +00:00
|
|
|
use neovim_lib::{Handler, Neovim, NeovimApi, Session, Value, UiAttachOptions, CallError, UiOption};
|
2017-05-27 16:50:25 +00:00
|
|
|
use neovim_lib::neovim_api::Tabpage;
|
|
|
|
|
|
|
|
use ui::UiMutex;
|
2017-04-22 11:31:06 +00:00
|
|
|
use ui_model::{ModelRect, ModelRectVec};
|
2017-04-12 10:12:05 +00:00
|
|
|
use shell;
|
2016-03-28 14:14:10 +00:00
|
|
|
use glib;
|
2016-03-19 10:27:39 +00:00
|
|
|
|
2017-07-08 11:02:56 +00:00
|
|
|
use value::ValueMapExt;
|
|
|
|
|
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-07-25 14:55:31 +00:00
|
|
|
fn on_highlight_set(&mut self, attrs: &[(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-07-08 20:45:55 +00:00
|
|
|
fn on_mode_change(&mut self, mode: &str, idx: u64) -> 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,
|
2017-07-25 14:55:31 +00:00
|
|
|
menu: &[Vec<&str>],
|
2017-04-19 08:40:53 +00:00
|
|
|
selected: i64,
|
|
|
|
row: u64,
|
|
|
|
col: u64)
|
|
|
|
-> RepaintMode;
|
|
|
|
|
|
|
|
fn popupmenu_hide(&mut self) -> RepaintMode;
|
|
|
|
|
|
|
|
fn popupmenu_select(&mut self, selected: i64) -> RepaintMode;
|
2017-05-27 16:50:25 +00:00
|
|
|
|
2017-05-28 09:29:50 +00:00
|
|
|
fn tabline_update(&mut self,
|
|
|
|
selected: Tabpage,
|
2017-07-08 11:02:56 +00:00
|
|
|
tabs: Vec<(Tabpage, Option<String>)>)
|
2017-05-28 09:29:50 +00:00
|
|
|
-> RepaintMode;
|
2017-07-06 14:50:55 +00:00
|
|
|
|
|
|
|
fn mode_info_set(&mut self,
|
|
|
|
cursor_style_enabled: bool,
|
|
|
|
mode_info: Vec<ModeInfo>)
|
|
|
|
-> 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-07-25 14:55:31 +00:00
|
|
|
($exp:expr) => ($exp.as_str().ok_or_else(|| "Can't convert argument to string".to_owned())?)
|
2016-03-28 14:03:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
macro_rules! try_int {
|
2017-07-25 14:55:31 +00:00
|
|
|
($expr:expr) => ($expr.as_i64().ok_or_else(|| "Can't convert argument to int".to_owned())?)
|
2016-04-03 15:13:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
macro_rules! try_uint {
|
2017-07-25 14:55:31 +00:00
|
|
|
($exp:expr) => ($exp.as_u64().ok_or_else(|| "Can't convert argument to u64".to_owned())?)
|
2016-03-19 10:27:39 +00:00
|
|
|
}
|
|
|
|
|
2017-07-06 14:50:55 +00:00
|
|
|
macro_rules! try_bool {
|
2017-07-25 14:55:31 +00:00
|
|
|
($exp:expr) => ($exp.as_bool().ok_or_else(|| "Can't convert argument to bool".to_owned())?)
|
2017-07-06 14:50:55 +00:00
|
|
|
}
|
|
|
|
|
2017-07-08 11:02:56 +00:00
|
|
|
macro_rules! map_array {
|
|
|
|
($arg:expr, $err:expr, |$item:ident| $exp:expr) => (
|
|
|
|
$arg.as_array()
|
2017-07-25 14:55:31 +00:00
|
|
|
.ok_or_else(|| $err)
|
2017-07-08 11:02:56 +00:00
|
|
|
.and_then(|items| items.iter().map(|$item| {
|
|
|
|
$exp
|
|
|
|
}).collect::<Result<Vec<_>, _>>())
|
|
|
|
);
|
|
|
|
($arg:expr, $err:expr, |$item:ident| {$exp:expr}) => (
|
|
|
|
$arg.as_array()
|
2017-07-25 14:55:31 +00:00
|
|
|
.ok_or_else(|| $err)
|
2017-07-08 11:02:56 +00:00
|
|
|
.and_then(|items| items.iter().map(|$item| {
|
|
|
|
$exp
|
|
|
|
}).collect::<Result<Vec<_>, _>>())
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2017-07-08 22:29:52 +00:00
|
|
|
#[derive(Debug, Clone)]
|
2017-07-06 14:50:55 +00:00
|
|
|
pub enum CursorShape {
|
|
|
|
Block,
|
|
|
|
Horizontal,
|
|
|
|
Vertical,
|
|
|
|
Unknown,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl CursorShape {
|
|
|
|
fn new(shape_code: &Value) -> Result<CursorShape, String> {
|
|
|
|
let str_code = shape_code
|
|
|
|
.as_str()
|
2017-07-25 14:55:31 +00:00
|
|
|
.ok_or_else(|| "Can't convert cursor shape to string".to_owned())?;
|
2017-07-06 14:50:55 +00:00
|
|
|
|
|
|
|
Ok(match str_code {
|
|
|
|
"block" => CursorShape::Block,
|
|
|
|
"horizontal" => CursorShape::Horizontal,
|
|
|
|
"vertical" => CursorShape::Vertical,
|
|
|
|
_ => {
|
|
|
|
error!("Unknown cursor_shape {}", str_code);
|
|
|
|
CursorShape::Unknown
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-07-08 22:29:52 +00:00
|
|
|
#[derive(Debug, Clone)]
|
2017-07-06 14:50:55 +00:00
|
|
|
pub struct ModeInfo {
|
|
|
|
cursor_shape: Option<CursorShape>,
|
2017-07-08 20:45:55 +00:00
|
|
|
cell_percentage: Option<u64>,
|
2017-07-06 14:50:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl ModeInfo {
|
|
|
|
pub fn new(mode_info_arr: &Vec<(Value, Value)>) -> Result<Self, String> {
|
2017-07-08 11:02:56 +00:00
|
|
|
let mode_info_map = mode_info_arr.to_attrs_map()?;
|
2017-07-06 14:50:55 +00:00
|
|
|
|
|
|
|
let cursor_shape = if let Some(shape) = mode_info_map.get("cursor_shape") {
|
|
|
|
Some(CursorShape::new(shape)?)
|
|
|
|
} else {
|
|
|
|
None
|
|
|
|
};
|
|
|
|
|
2017-07-08 20:45:55 +00:00
|
|
|
let cell_percentage = if let Some(cell_percentage) = mode_info_map.get("cell_percentage") {
|
|
|
|
cell_percentage.as_u64()
|
|
|
|
} else {
|
|
|
|
None
|
|
|
|
};
|
|
|
|
|
|
|
|
Ok(ModeInfo {
|
|
|
|
cursor_shape,
|
|
|
|
cell_percentage,
|
|
|
|
})
|
2017-07-06 14:50:55 +00:00
|
|
|
}
|
2017-07-08 22:29:52 +00:00
|
|
|
|
2017-07-09 09:40:31 +00:00
|
|
|
pub fn cursor_shape(&self) -> Option<&CursorShape> {
|
|
|
|
self.cursor_shape.as_ref()
|
2017-07-08 22:29:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn cell_percentage(&self) -> u64 {
|
2017-07-09 09:40:31 +00:00
|
|
|
self.cell_percentage.unwrap_or(0)
|
2017-07-08 22:29:52 +00:00
|
|
|
}
|
2017-07-06 14:50:55 +00:00
|
|
|
}
|
|
|
|
|
2017-07-06 09:41:35 +00:00
|
|
|
#[derive(Debug)]
|
|
|
|
pub struct NvimInitError {
|
|
|
|
source: Box<error::Error>,
|
2017-07-11 15:14:46 +00:00
|
|
|
cmd: Option<String>,
|
2017-07-06 09:41:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl NvimInitError {
|
2017-07-11 15:14:46 +00:00
|
|
|
pub fn new_post_init<E>(error: E) -> NvimInitError
|
|
|
|
where E: Into<Box<error::Error>>
|
|
|
|
{
|
|
|
|
NvimInitError {
|
|
|
|
cmd: None,
|
|
|
|
source: error.into(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-07-06 09:41:35 +00:00
|
|
|
pub fn new<E>(cmd: &Command, error: E) -> NvimInitError
|
|
|
|
where E: Into<Box<error::Error>>
|
|
|
|
{
|
|
|
|
NvimInitError {
|
2017-07-11 15:14:46 +00:00
|
|
|
cmd: Some(format!("{:?}", cmd)),
|
2017-07-06 09:41:35 +00:00
|
|
|
source: error.into(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn source(&self) -> String {
|
|
|
|
format!("{}", self.source)
|
|
|
|
}
|
|
|
|
|
2017-07-11 15:14:46 +00:00
|
|
|
pub fn cmd(&self) -> Option<&String> {
|
|
|
|
self.cmd.as_ref()
|
2017-07-06 09:41:35 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl fmt::Display for NvimInitError {
|
2017-07-06 14:50:55 +00:00
|
|
|
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
2017-07-06 09:41:35 +00:00
|
|
|
write!(f, "{:?}", self.source)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl error::Error for NvimInitError {
|
|
|
|
fn description(&self) -> &str {
|
|
|
|
"Can't start nvim instance"
|
|
|
|
}
|
|
|
|
|
|
|
|
fn cause(&self) -> Option<&error::Error> {
|
|
|
|
Some(&*self.source)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-07-11 15:14:46 +00:00
|
|
|
pub fn start(shell: Arc<UiMutex<shell::State>>,
|
|
|
|
nvim_bin_path: Option<&String>)
|
|
|
|
-> result::Result<Neovim, NvimInitError> {
|
2017-05-15 14:44:37 +00:00
|
|
|
let mut cmd = if let Some(path) = nvim_bin_path {
|
|
|
|
Command::new(path)
|
2016-03-28 10:09:31 +00:00
|
|
|
} else {
|
2017-05-15 14:44:37 +00:00
|
|
|
Command::new("nvim")
|
|
|
|
};
|
|
|
|
|
|
|
|
cmd.arg("--embed")
|
|
|
|
.arg("--headless")
|
|
|
|
.arg("--cmd")
|
|
|
|
.arg("set termguicolors")
|
2017-05-24 11:00:23 +00:00
|
|
|
.arg("--cmd")
|
|
|
|
.arg("let g:GtkGuiLoaded = 1")
|
2017-05-15 14:44:37 +00:00
|
|
|
.stderr(Stdio::inherit());
|
|
|
|
|
2017-05-24 11:00:23 +00:00
|
|
|
if let Ok(runtime_path) = env::var("NVIM_GTK_RUNTIME_PATH") {
|
|
|
|
cmd.arg("--cmd")
|
|
|
|
.arg(format!("let &rtp.=',{}'", runtime_path));
|
2017-05-27 08:56:47 +00:00
|
|
|
} else if let Some(prefix) = option_env!("PREFIX") {
|
2017-05-24 11:00:23 +00:00
|
|
|
cmd.arg("--cmd")
|
|
|
|
.arg(format!("let &rtp.=',{}/share/nvim-gtk/runtime'", prefix));
|
|
|
|
} else {
|
|
|
|
cmd.arg("--cmd").arg("let &rtp.=',runtime'");
|
|
|
|
}
|
|
|
|
|
2017-05-15 14:44:37 +00:00
|
|
|
let session = Session::new_child_cmd(&mut cmd);
|
|
|
|
|
|
|
|
let session = match session {
|
2017-07-06 09:41:35 +00:00
|
|
|
Err(e) => return Err(NvimInitError::new(&cmd, e)),
|
2017-05-15 14:44:37 +00:00
|
|
|
Ok(s) => s,
|
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-07-11 15:14:46 +00:00
|
|
|
|
|
|
|
Ok(nvim)
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn post_start_init(nvim: &mut Neovim,
|
|
|
|
open_path: Option<&String>,
|
|
|
|
cols: u64,
|
|
|
|
rows: u64)
|
|
|
|
-> result::Result<(), NvimInitError> {
|
2017-04-19 08:40:53 +00:00
|
|
|
let mut opts = UiAttachOptions::new();
|
2017-05-27 08:56:47 +00:00
|
|
|
opts.set_popupmenu_external(false);
|
2017-05-27 16:50:25 +00:00
|
|
|
opts.set_tabline_external(true);
|
2017-06-04 10:14:09 +00:00
|
|
|
nvim.ui_attach(cols, rows, opts)
|
2017-07-25 14:55:31 +00:00
|
|
|
.map_err(NvimInitError::new_post_init)?;
|
2017-04-12 10:12:05 +00:00
|
|
|
nvim.command("runtime! ginit.vim")
|
2017-07-25 14:55:31 +00:00
|
|
|
.map_err(NvimInitError::new_post_init)?;
|
2016-03-28 10:09:31 +00:00
|
|
|
|
2017-07-11 15:14:46 +00:00
|
|
|
if let Some(path) = open_path {
|
|
|
|
nvim.command(&format!("e {}", path))
|
2017-07-25 14:55:31 +00:00
|
|
|
.map_err(NvimInitError::new_post_init)?;
|
2017-07-11 15:14:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Ok(())
|
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() {
|
2017-07-25 14:55:31 +00:00
|
|
|
for local_args in ev_args.iter().skip(1) {
|
2017-04-12 10:12:05 +00:00
|
|
|
let args = match *local_args {
|
2017-07-25 14:55:31 +00:00
|
|
|
Value::Array(ref ar) => ar.clone(),
|
2017-04-12 10:12:05 +00:00
|
|
|
_ => vec![],
|
|
|
|
};
|
|
|
|
let call_reapint_mode = call(ui, ev_name, &args)?;
|
2017-04-22 19:25:05 +00:00
|
|
|
repaint_mode = repaint_mode.join(call_reapint_mode);
|
2017-04-12 10:12:05 +00:00
|
|
|
}
|
|
|
|
} 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" => {
|
2017-07-25 14:55:31 +00:00
|
|
|
if !params.is_empty() {
|
2017-04-12 10:12:05 +00:00
|
|
|
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])),
|
2017-05-27 08:56:47 +00:00
|
|
|
"Option" => {
|
|
|
|
match try_str!(args[0]) {
|
|
|
|
"Popupmenu" => {
|
|
|
|
ui.nvim()
|
|
|
|
.set_option(UiOption::ExtPopupmenu(try_uint!(args[1]) == 1))
|
|
|
|
.map_err(|e| e.to_string())?
|
|
|
|
}
|
2017-05-28 09:29:50 +00:00
|
|
|
"Tabline" => {
|
|
|
|
ui.nvim()
|
|
|
|
.set_option(UiOption::ExtTabline(try_uint!(args[1]) == 1))
|
|
|
|
.map_err(|e| e.to_string())?
|
|
|
|
}
|
2017-05-27 08:56:47 +00:00
|
|
|
opt => error!("Unknown option {}", opt),
|
|
|
|
}
|
|
|
|
}
|
2017-03-06 21:05:48 +00:00
|
|
|
_ => 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,
|
2017-07-25 14:55:31 +00:00
|
|
|
args: &[Value])
|
2017-04-12 10:12:05 +00:00
|
|
|
-> 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])),
|
2017-07-08 20:45:55 +00:00
|
|
|
"mode_change" => ui.on_mode_change(try_str!(args[0]), try_uint!(args[1])),
|
2017-04-19 08:40:53 +00:00
|
|
|
"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" => {
|
2017-07-08 11:02:56 +00:00
|
|
|
let menu_items = map_array!(args[0], "Error get menu list array", |item| {
|
|
|
|
map_array!(item,
|
|
|
|
"Error get menu item array",
|
|
|
|
|col| col.as_str().ok_or("Error get menu column"))
|
|
|
|
})?;
|
2017-04-19 08:40:53 +00:00
|
|
|
|
|
|
|
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])),
|
2017-05-27 16:50:25 +00:00
|
|
|
"tabline_update" => {
|
2017-07-08 11:02:56 +00:00
|
|
|
let tabs_out = map_array!(args[1], "Error get tabline list".to_owned(), |tab| {
|
|
|
|
tab.as_map()
|
2017-07-25 14:55:31 +00:00
|
|
|
.ok_or_else(|| "Error get map for tab".to_owned())
|
2017-07-08 11:02:56 +00:00
|
|
|
.and_then(|tab_map| tab_map.to_attrs_map())
|
|
|
|
.map(|tab_attrs| {
|
|
|
|
let name_attr = tab_attrs
|
|
|
|
.get("name")
|
|
|
|
.and_then(|n| n.as_str().map(|s| s.to_owned()));
|
|
|
|
let tab_attr = tab_attrs
|
|
|
|
.get("tab")
|
|
|
|
.map(|tab_id| Tabpage::new(tab_id.clone()))
|
|
|
|
.unwrap();
|
|
|
|
|
|
|
|
(tab_attr, name_attr)
|
|
|
|
})
|
|
|
|
})?;
|
2017-05-27 16:50:25 +00:00
|
|
|
ui.tabline_update(Tabpage::new(args[0].clone()), tabs_out)
|
2017-05-28 09:29:50 +00:00
|
|
|
}
|
2017-07-06 14:50:55 +00:00
|
|
|
"mode_info_set" => {
|
2017-07-08 11:02:56 +00:00
|
|
|
let mode_info = map_array!(args[1],
|
|
|
|
"Error get array key value for mode_info".to_owned(),
|
|
|
|
|mi| {
|
|
|
|
mi.as_map()
|
2017-07-25 14:55:31 +00:00
|
|
|
.ok_or_else(|| "Erro get map for mode_info".to_owned())
|
2017-07-08 11:02:56 +00:00
|
|
|
.and_then(|mi_map| ModeInfo::new(mi_map))
|
|
|
|
})?;
|
2017-07-06 14:50:55 +00:00
|
|
|
ui.mode_info_set(try_bool!(args[0]), mode_info)
|
|
|
|
}
|
2017-04-19 08:40:53 +00:00
|
|
|
_ => {
|
|
|
|
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-07-25 14:55:31 +00:00
|
|
|
if let Err(ref err) = *self {
|
2017-03-11 20:06:45 +00:00
|
|
|
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-04-22 19:18:59 +00:00
|
|
|
#[derive(Clone, Debug)]
|
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,
|
2017-04-22 11:31:06 +00:00
|
|
|
AreaList(ModelRectVec),
|
2017-03-17 22:18:41 +00:00
|
|
|
Area(ModelRect),
|
|
|
|
}
|
|
|
|
|
2017-03-18 09:45:05 +00:00
|
|
|
impl RepaintMode {
|
2017-04-22 19:25:05 +00:00
|
|
|
pub fn join(self, mode: RepaintMode) -> RepaintMode {
|
2017-03-18 09:45:05 +00:00
|
|
|
match (self, mode) {
|
2017-04-22 19:25:05 +00:00
|
|
|
(RepaintMode::Nothing, m) => m,
|
|
|
|
(m, RepaintMode::Nothing) => m,
|
|
|
|
(RepaintMode::All, _) => RepaintMode::All,
|
|
|
|
(_, RepaintMode::All) => RepaintMode::All,
|
|
|
|
(RepaintMode::Area(mr1), RepaintMode::Area(mr2)) => {
|
|
|
|
let mut vec = ModelRectVec::new(mr1);
|
|
|
|
vec.join(&mr2);
|
2017-04-22 11:31:06 +00:00
|
|
|
RepaintMode::AreaList(vec)
|
|
|
|
}
|
2017-04-22 19:25:05 +00:00
|
|
|
(RepaintMode::AreaList(mut target), RepaintMode::AreaList(source)) => {
|
2017-04-22 19:18:59 +00:00
|
|
|
for s in &source.list {
|
2017-07-25 14:55:31 +00:00
|
|
|
target.join(s);
|
2017-04-22 19:18:59 +00:00
|
|
|
}
|
2017-04-22 19:25:05 +00:00
|
|
|
RepaintMode::AreaList(target)
|
2017-04-22 11:31:06 +00:00
|
|
|
}
|
2017-04-22 19:25:05 +00:00
|
|
|
(RepaintMode::AreaList(mut list), RepaintMode::Area(l2)) => {
|
|
|
|
list.join(&l2);
|
2017-04-22 11:31:06 +00:00
|
|
|
RepaintMode::AreaList(list)
|
|
|
|
}
|
2017-04-22 19:25:05 +00:00
|
|
|
(RepaintMode::Area(l1), RepaintMode::AreaList(mut list)) => {
|
|
|
|
list.join(&l1);
|
2017-04-22 11:31:06 +00:00
|
|
|
RepaintMode::AreaList(list)
|
2017-03-18 09:45:05 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2017-03-20 15:18:08 +00:00
|
|
|
|
2017-07-06 09:41:35 +00:00
|
|
|
|
2017-07-12 15:32:10 +00:00
|
|
|
enum NeovimClientState {
|
2017-07-06 09:41:35 +00:00
|
|
|
Uninitialized,
|
2017-07-11 15:14:46 +00:00
|
|
|
InitInProgress,
|
2017-07-06 09:41:35 +00:00
|
|
|
Initialized(Neovim),
|
|
|
|
Error,
|
|
|
|
}
|
|
|
|
|
2017-07-12 15:32:10 +00:00
|
|
|
impl NeovimClientState {
|
2017-07-22 10:32:56 +00:00
|
|
|
pub fn is_initializing(&self) -> bool {
|
2017-07-11 15:14:46 +00:00
|
|
|
match *self {
|
2017-07-22 10:32:56 +00:00
|
|
|
NeovimClientState::InitInProgress => true,
|
2017-07-11 15:14:46 +00:00
|
|
|
_ => false,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-07-22 10:32:56 +00:00
|
|
|
pub fn is_uninitialized(&self) -> bool {
|
2017-07-06 09:41:35 +00:00
|
|
|
match *self {
|
2017-07-22 10:32:56 +00:00
|
|
|
NeovimClientState::Uninitialized => true,
|
2017-07-06 09:41:35 +00:00
|
|
|
_ => false,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-07-22 10:32:56 +00:00
|
|
|
pub fn is_initialized(&self) -> bool {
|
2017-07-06 09:41:35 +00:00
|
|
|
match *self {
|
2017-07-22 10:32:56 +00:00
|
|
|
NeovimClientState::Initialized(_) => true,
|
2017-07-06 09:41:35 +00:00
|
|
|
_ => false,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn nvim(&self) -> &Neovim {
|
|
|
|
match *self {
|
2017-07-12 15:32:10 +00:00
|
|
|
NeovimClientState::Initialized(ref nvim) => nvim,
|
|
|
|
NeovimClientState::InitInProgress |
|
|
|
|
NeovimClientState::Uninitialized => panic!("Access to uninitialized neovim client"),
|
|
|
|
NeovimClientState::Error => {
|
2017-07-06 14:50:55 +00:00
|
|
|
panic!("Access to neovim client that is not started due to some error")
|
|
|
|
}
|
2017-07-06 09:41:35 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn nvim_mut(&mut self) -> &mut Neovim {
|
|
|
|
match *self {
|
2017-07-12 15:32:10 +00:00
|
|
|
NeovimClientState::Initialized(ref mut nvim) => nvim,
|
|
|
|
NeovimClientState::InitInProgress |
|
|
|
|
NeovimClientState::Uninitialized => panic!("Access to uninitialized neovim client"),
|
|
|
|
NeovimClientState::Error => {
|
2017-07-06 14:50:55 +00:00
|
|
|
panic!("Access to neovim client that is not started due to some error")
|
|
|
|
}
|
2017-07-06 09:41:35 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub struct NeovimClient {
|
2017-07-12 15:32:10 +00:00
|
|
|
state: NeovimClientState,
|
2017-07-06 09:41:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl NeovimClient {
|
|
|
|
pub fn new() -> Self {
|
2017-07-12 15:32:10 +00:00
|
|
|
NeovimClient { state: NeovimClientState::Uninitialized }
|
2017-07-06 09:41:35 +00:00
|
|
|
}
|
|
|
|
|
2017-07-12 15:32:10 +00:00
|
|
|
pub fn set_initialized(&mut self, nvim: Neovim) {
|
|
|
|
self.state = NeovimClientState::Initialized(nvim);
|
2017-07-06 09:41:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn set_error(&mut self) {
|
2017-07-12 15:32:10 +00:00
|
|
|
self.state = NeovimClientState::Error;
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn set_in_progress(&mut self) {
|
|
|
|
self.state = NeovimClientState::InitInProgress;
|
2017-07-06 09:41:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn is_initialized(&self) -> bool {
|
2017-07-12 15:32:10 +00:00
|
|
|
self.state.is_initialized()
|
2017-07-06 09:41:35 +00:00
|
|
|
}
|
|
|
|
|
2017-07-11 15:14:46 +00:00
|
|
|
pub fn is_uninitialized(&self) -> bool {
|
2017-07-12 15:32:10 +00:00
|
|
|
self.state.is_uninitialized()
|
2017-07-11 15:14:46 +00:00
|
|
|
}
|
|
|
|
|
2017-07-22 10:32:56 +00:00
|
|
|
pub fn is_initializing(&self) -> bool {
|
|
|
|
self.state.is_initializing()
|
2017-07-06 09:41:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn nvim(&self) -> &Neovim {
|
2017-07-12 15:32:10 +00:00
|
|
|
self.state.nvim()
|
2017-07-06 09:41:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn nvim_mut(&mut self) -> &mut Neovim {
|
2017-07-12 15:32:10 +00:00
|
|
|
self.state.nvim_mut()
|
2017-07-06 09:41:35 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Deref for NeovimClient {
|
|
|
|
type Target = Neovim;
|
|
|
|
|
|
|
|
fn deref(&self) -> &Neovim {
|
|
|
|
self.nvim()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl DerefMut for NeovimClient {
|
|
|
|
fn deref_mut(&mut self) -> &mut Neovim {
|
|
|
|
self.nvim_mut()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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));
|
2017-04-23 08:48:00 +00:00
|
|
|
let mode = mode.join(RepaintMode::Nothing);
|
2017-03-20 15:18:08 +00:00
|
|
|
|
|
|
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|