2017-10-15 11:34:48 +00:00
|
|
|
|
|
|
|
mod client;
|
|
|
|
mod handler;
|
|
|
|
mod mode_info;
|
|
|
|
mod redraw_handler;
|
|
|
|
mod repaint_mode;
|
2017-12-10 18:09:36 +00:00
|
|
|
mod ext;
|
2017-10-15 11:34:48 +00:00
|
|
|
|
2017-12-21 19:54:27 +00:00
|
|
|
pub use self::redraw_handler::{RedrawEvents, GuiApi, CompleteItem};
|
2017-10-15 11:34:48 +00:00
|
|
|
pub use self::repaint_mode::RepaintMode;
|
2017-11-10 15:36:54 +00:00
|
|
|
pub use self::client::{NeovimClient, NeovimClientAsync, NeovimRef};
|
2017-10-15 11:34:48 +00:00
|
|
|
pub use self::mode_info::{ModeInfo, CursorShape};
|
2017-12-11 20:15:17 +00:00
|
|
|
pub use self::ext::ErrorReport;
|
2017-10-15 11:34:48 +00:00
|
|
|
|
|
|
|
use std::error;
|
|
|
|
use std::fmt;
|
|
|
|
use std::env;
|
|
|
|
use std::process::{Stdio, Command};
|
|
|
|
use std::result;
|
|
|
|
use std::sync::Arc;
|
2017-12-10 09:46:36 +00:00
|
|
|
use std::time::Duration;
|
2017-10-15 11:34:48 +00:00
|
|
|
|
2017-12-10 18:09:36 +00:00
|
|
|
use neovim_lib::{Neovim, NeovimApi, Session, UiAttachOptions};
|
2017-10-15 11:34:48 +00:00
|
|
|
|
|
|
|
use ui::UiMutex;
|
|
|
|
use shell;
|
2017-11-06 19:05:11 +00:00
|
|
|
use nvim_config::NvimConfig;
|
2017-10-15 11:34:48 +00:00
|
|
|
|
|
|
|
#[derive(Debug)]
|
|
|
|
pub struct NvimInitError {
|
|
|
|
source: Box<error::Error>,
|
|
|
|
cmd: Option<String>,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl NvimInitError {
|
|
|
|
pub fn new_post_init<E>(error: E) -> NvimInitError
|
|
|
|
where
|
|
|
|
E: Into<Box<error::Error>>,
|
|
|
|
{
|
|
|
|
NvimInitError {
|
|
|
|
cmd: None,
|
|
|
|
source: error.into(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn new<E>(cmd: &Command, error: E) -> NvimInitError
|
|
|
|
where
|
|
|
|
E: Into<Box<error::Error>>,
|
|
|
|
{
|
|
|
|
NvimInitError {
|
|
|
|
cmd: Some(format!("{:?}", cmd)),
|
|
|
|
source: error.into(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn source(&self) -> String {
|
|
|
|
format!("{}", self.source)
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn cmd(&self) -> Option<&String> {
|
|
|
|
self.cmd.as_ref()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl fmt::Display for NvimInitError {
|
|
|
|
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn start(
|
|
|
|
shell: Arc<UiMutex<shell::State>>,
|
|
|
|
nvim_bin_path: Option<&String>,
|
2017-12-10 09:46:36 +00:00
|
|
|
timeout: Option<Duration>,
|
2017-10-15 11:34:48 +00:00
|
|
|
) -> result::Result<Neovim, NvimInitError> {
|
|
|
|
let mut cmd = if let Some(path) = nvim_bin_path {
|
|
|
|
Command::new(path)
|
|
|
|
} else {
|
|
|
|
Command::new("nvim")
|
|
|
|
};
|
|
|
|
|
|
|
|
cmd.arg("--embed")
|
|
|
|
.arg("--headless")
|
|
|
|
// Swap files are disabled because it shows message window on start up but frontend can't detect it.
|
|
|
|
.arg("-n")
|
|
|
|
.arg("--cmd")
|
|
|
|
.arg("set termguicolors")
|
|
|
|
.arg("--cmd")
|
|
|
|
.arg("let g:GtkGuiLoaded = 1")
|
|
|
|
.stderr(Stdio::inherit());
|
|
|
|
|
|
|
|
if let Ok(runtime_path) = env::var("NVIM_GTK_RUNTIME_PATH") {
|
|
|
|
cmd.arg("--cmd").arg(
|
|
|
|
format!("let &rtp.=',{}'", runtime_path),
|
|
|
|
);
|
|
|
|
} else if let Some(prefix) = option_env!("PREFIX") {
|
|
|
|
cmd.arg("--cmd").arg(format!(
|
|
|
|
"let &rtp.=',{}/share/nvim-gtk/runtime'",
|
|
|
|
prefix
|
|
|
|
));
|
|
|
|
} else {
|
|
|
|
cmd.arg("--cmd").arg("let &rtp.=',runtime'");
|
|
|
|
}
|
|
|
|
|
2017-11-06 19:05:11 +00:00
|
|
|
if let Some(nvim_config) = NvimConfig::config_path() {
|
|
|
|
if let Some(path) = nvim_config.to_str() {
|
|
|
|
cmd.arg("--cmd").arg(format!("source {}", path));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-10-15 11:34:48 +00:00
|
|
|
let session = Session::new_child_cmd(&mut cmd);
|
|
|
|
|
2017-12-10 09:46:36 +00:00
|
|
|
let mut session = match session {
|
2017-10-15 11:34:48 +00:00
|
|
|
Err(e) => return Err(NvimInitError::new(&cmd, e)),
|
|
|
|
Ok(s) => s,
|
|
|
|
};
|
|
|
|
|
2017-12-10 09:46:36 +00:00
|
|
|
session.set_timeout(timeout.unwrap_or(Duration::from_millis(10_000)));
|
|
|
|
|
2017-10-15 11:34:48 +00:00
|
|
|
let mut nvim = Neovim::new(session);
|
|
|
|
|
|
|
|
nvim.session.start_event_loop_handler(
|
|
|
|
handler::NvimHandler::new(shell),
|
|
|
|
);
|
|
|
|
|
|
|
|
Ok(nvim)
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn post_start_init(
|
2017-11-10 15:36:54 +00:00
|
|
|
nvim: NeovimClientAsync,
|
2017-10-15 11:34:48 +00:00
|
|
|
open_path: Option<&String>,
|
|
|
|
cols: u64,
|
|
|
|
rows: u64,
|
|
|
|
) -> result::Result<(), NvimInitError> {
|
|
|
|
let mut opts = UiAttachOptions::new();
|
2017-11-16 13:53:58 +00:00
|
|
|
opts.set_popupmenu_external(true);
|
2017-10-15 11:34:48 +00:00
|
|
|
opts.set_tabline_external(true);
|
2017-12-10 09:46:36 +00:00
|
|
|
nvim.borrow()
|
|
|
|
.unwrap()
|
|
|
|
.ui_attach(cols, rows, &opts)
|
|
|
|
.map_err(NvimInitError::new_post_init)?;
|
|
|
|
nvim.borrow()
|
|
|
|
.unwrap()
|
|
|
|
.command("runtime! ginit.vim")
|
|
|
|
.map_err(NvimInitError::new_post_init)?;
|
2017-10-15 11:34:48 +00:00
|
|
|
|
|
|
|
if let Some(path) = open_path {
|
2017-12-10 09:46:36 +00:00
|
|
|
nvim.borrow()
|
|
|
|
.unwrap()
|
|
|
|
.command(&format!("e {}", path))
|
|
|
|
.map_err(NvimInitError::new_post_init)?;
|
2017-10-15 11:34:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|