1mod client;
2mod handler;
3mod redraw_handler;
4mod repaint_mode;
5mod ext;
6
7pub use self::redraw_handler::{CompleteItem, NvimCommand};
8pub use self::repaint_mode::RepaintMode;
9pub use self::client::{NeovimClient, NeovimClientAsync, NeovimRef};
10pub use self::ext::ErrorReport;
11pub use self::handler::NvimHandler;
12
13use std::error;
14use std::fmt;
15use std::env;
16use std::process::{Command, Stdio};
17use std::result;
18use std::time::Duration;
19
20use neovim_lib::{Neovim, NeovimApi, Session, UiAttachOptions};
21
22use crate::nvim_config::NvimConfig;
23
24#[derive(Debug)]
25pub struct NvimInitError {
26 source: Box<dyn error::Error>,
27 cmd: Option<String>,
28}
29
30impl NvimInitError {
31 pub fn new_post_init<E>(error: E) -> NvimInitError
32 where
33 E: Into<Box<dyn error::Error>>,
34 {
35 NvimInitError {
36 cmd: None,
37 source: error.into(),
38 }
39 }
40
41 pub fn new<E>(cmd: &Command, error: E) -> NvimInitError
42 where
43 E: Into<Box<dyn error::Error>>,
44 {
45 NvimInitError {
46 cmd: Some(format!("{:?}", cmd)),
47 source: error.into(),
48 }
49 }
50
51 pub fn source(&self) -> String {
52 format!("{}", self.source)
53 }
54
55 pub fn cmd(&self) -> Option<&String> {
56 self.cmd.as_ref()
57 }
58}
59
60impl fmt::Display for NvimInitError {
61 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
62 write!(f, "{:?}", self.source)
63 }
64}
65
66impl error::Error for NvimInitError {
67 fn description(&self) -> &str {
68 "Can't start nvim instance"
69 }
70
71 fn cause(&self) -> Option<&dyn error::Error> {
72 Some(&*self.source)
73 }
74}
75
76#[cfg(target_os = "windows")]
77fn set_windows_creation_flags(cmd: &mut Command) {
78 use std::os::windows::process::CommandExt;
79 cmd.creation_flags(0x08000000); }
81
82pub fn start(
83 handler: NvimHandler,
84 nvim_bin_path: Option<&String>,
85 timeout: Option<Duration>,
86 args_for_neovim: Vec<String>,
87) -> result::Result<Neovim, NvimInitError> {
88 let mut cmd = if let Some(path) = nvim_bin_path {
89 Command::new(path)
90 } else {
91 Command::new("nvim")
92 };
93
94 cmd.arg("--embed")
95 .arg("--cmd")
96 .arg("set termguicolors")
97 .arg("--cmd")
98 .arg("let g:GtkGuiLoaded = 1")
99 .stderr(Stdio::inherit());
100
101 #[cfg(target_os = "windows")]
102 set_windows_creation_flags(&mut cmd);
103
104 if let Ok(runtime_path) = env::var("NVIM_GTK_RUNTIME_PATH") {
105 cmd.arg("--cmd")
106 .arg(format!("let &rtp.=',{}'", runtime_path));
107 } else if let Some(prefix) = option_env!("PREFIX") {
108 cmd.arg("--cmd")
109 .arg(format!("let &rtp.=',{}/share/nvim-gtk/runtime'", prefix));
110 } else {
111 cmd.arg("--cmd").arg("let &rtp.=',runtime'");
112 }
113
114 if let Some(nvim_config) = NvimConfig::config_path() {
115 if let Some(path) = nvim_config.to_str() {
116 cmd.arg("--cmd").arg(format!("source {}", path));
117 }
118 }
119
120 for arg in args_for_neovim {
121 cmd.arg(arg);
122 }
123
124 let session = Session::new_child_cmd(&mut cmd);
125
126 let mut session = match session {
127 Err(e) => return Err(NvimInitError::new(&cmd, e)),
128 Ok(s) => s,
129 };
130
131 session.set_timeout(timeout.unwrap_or(Duration::from_millis(10_000)));
132
133 let mut nvim = Neovim::new(session);
134
135 nvim.session.start_event_loop_handler(handler);
136
137 Ok(nvim)
138}
139
140pub fn post_start_init(
141 nvim: NeovimClientAsync,
142 cols: i64,
143 rows: i64,
144 input_data: Option<String>,
145) -> result::Result<(), NvimInitError> {
146 nvim.borrow()
147 .unwrap()
148 .ui_attach(
149 cols,
150 rows,
151 UiAttachOptions::new()
152 .set_popupmenu_external(true)
153 .set_tabline_external(true)
154 .set_linegrid_external(true)
155 .set_hlstate_external(true)
156 )
157 .map_err(NvimInitError::new_post_init)?;
158
159 nvim.borrow()
160 .unwrap()
161 .command("runtime! ginit.vim")
162 .map_err(NvimInitError::new_post_init)?;
163
164 if let Some(input_data) = input_data {
165 let mut nvim = nvim.borrow().unwrap();
166 let buf = nvim.get_current_buf().ok_and_report();
167
168 if let Some(buf) = buf {
169 buf.set_lines(
170 &mut *nvim,
171 0,
172 0,
173 true,
174 input_data.lines().map(|l| l.to_owned()).collect(),
175 ).report_err();
176 }
177 }
178
179 Ok(())
180}