nvim_utils/vim/api/
global.rs

1#[cfg(feature = "unstable")]
2#[cfg_attr(docsrs, doc(cfg(feature = "unstable")))]
3use serde::{Deserialize, Serialize};
4
5use crate::prelude::*;
6
7/// Corresponds to `vim.api.nvim_get_current_buf`
8pub fn nvim_get_current_buf(lua: &Lua) -> LuaResult<i64> {
9    vim::api::get(lua)?.call_function("nvim_get_current_buf", ())
10}
11
12/// Corresponds to `vim.api.nvim_get_current_line`
13pub fn nvim_get_current_line(lua: &Lua) -> LuaResult<String> {
14    vim::api::get(lua)?.call_function("nvim_get_current_line", ())
15}
16
17/// Corresponds to `vim.api.nvim_get_current_tabpage`
18pub fn nvim_get_current_tabpage(lua: &Lua) -> LuaResult<i64> {
19    vim::api::get(lua)?.call_function("nvim_get_current_tabpage", ())
20}
21
22/// Corresponds to `vim.api.nvim_get_current_win`
23pub fn nvim_get_current_win(lua: &Lua) -> LuaResult<i64> {
24    vim::api::get(lua)?.call_function("nvim_get_current_win", ())
25}
26
27/// Corresponds to `vim.api.nvim_list_bufs`
28pub fn nvim_list_bufs(lua: &Lua) -> LuaResult<Vec<LuaInteger>> {
29    vim::api::get(lua)?.call_function("nvim_list_bufs", ())
30}
31
32/// Corresponds to `vim.api.nvim_exec`
33pub fn nvim_exec<'a>(lua: &'a Lua, cmd: &str, output: bool) -> LuaResult<LuaValue<'a>> {
34    vim::api::get(lua)?.call_function("nvim_exec", (cmd, output))
35}
36
37/// Corresponds to `vim.api.nvim_feedkeys`
38pub fn nvim_feedkeys(lua: &Lua, keys: &str, mode: &str, escape_ks: bool) -> LuaResult<()> {
39    vim::api::get(lua)?.call_function("nvim_feedkeys", (keys, mode, escape_ks))
40}
41
42/// Result struct for `vim.api.nvim_get_mode`
43#[cfg(feature = "unstable")]
44#[cfg_attr(docsrs, doc(cfg(feature = "unstable")))]
45#[derive(Clone, Deserialize)]
46pub struct GetModeRes {
47    pub mode: String,
48    pub blocking: bool,
49}
50
51/// Corresponds to `vim.api.nvim_get_mode`
52#[cfg(feature = "unstable")]
53#[cfg_attr(docsrs, doc(cfg(feature = "unstable")))]
54pub fn nvim_get_mode(lua: &Lua) -> LuaResult<GetModeRes> {
55    lua.from_value(vim::api::get(lua)?.call_function("nvim_get_mode", ())?)
56}
57
58/// Corresponds to `vim.api.nvim_stats`
59#[cfg(feature = "unstable")]
60#[cfg_attr(docsrs, doc(cfg(feature = "unstable")))]
61pub fn nvim_stats(lua: &Lua) -> LuaResult<LuaTable> {
62    vim::api::get(lua)?
63        .get::<_, LuaFunction>("nvim_stats")?
64        .call(())
65}
66
67/// Corresponds to `vim.api.nvim_chan_send`
68#[cfg(feature = "unstable")]
69#[cfg_attr(docsrs, doc(cfg(feature = "unstable")))]
70pub fn nvim_chan_send(lua: &Lua, chan: i32, data: &str) -> LuaResult<()> {
71    vim::api::get(lua)?
72        .get::<_, LuaFunction>("nvim_chan_send")?
73        .call((chan, data))
74}
75
76/// Corresponds to `vim.api.nvim_create_buf`
77#[cfg(feature = "unstable")]
78#[cfg_attr(docsrs, doc(cfg(feature = "unstable")))]
79pub fn nvim_create_buf(lua: &Lua, listed: bool, scratch: bool) -> LuaResult<i64> {
80    vim::api::get(lua)?
81        .get::<_, LuaFunction>("nvim_create_buf")?
82        .call((listed, scratch))
83}
84
85/// Corresponds to `vim.api.nvim_del_current_line`
86#[cfg(feature = "unstable")]
87#[cfg_attr(docsrs, doc(cfg(feature = "unstable")))]
88pub fn nvim_del_current_line(lua: &Lua) -> LuaResult<()> {
89    vim::api::get(lua)?
90        .get::<_, LuaFunction>("nvim_del_current_line")?
91        .call(())
92}
93
94/// Corresponds to `vim.api.nvim_del_keymap`
95#[cfg(feature = "unstable")]
96#[cfg_attr(docsrs, doc(cfg(feature = "unstable")))]
97pub fn nvim_del_keymap(lua: &Lua, mode: &str, lhs: &str) -> LuaResult<()> {
98    vim::api::get(lua)?
99        .get::<_, LuaFunction>("nvim_del_keymap")?
100        .call((mode, lhs))
101}
102
103/// Corresponds to `vim.api.nvim_del_mark`
104#[cfg(feature = "unstable")]
105#[cfg_attr(docsrs, doc(cfg(feature = "unstable")))]
106pub fn nvim_del_mark(lua: &Lua, name: &str) -> LuaResult<bool> {
107    vim::api::get(lua)?
108        .get::<_, LuaFunction>("nvim_del_mark")?
109        .call(name)
110}
111
112/// Corresponds to `vim.api.nvim_del_var`
113#[cfg(feature = "unstable")]
114#[cfg_attr(docsrs, doc(cfg(feature = "unstable")))]
115pub fn nvim_del_var(lua: &Lua, name: &str) -> LuaResult<()> {
116    vim::api::get(lua)?
117        .get::<_, LuaFunction>("nvim_del_var")?
118        .call(name)
119}
120
121/// Corresponds to `vim.api.nvim_echo`
122#[cfg(feature = "unstable")]
123#[cfg_attr(docsrs, doc(cfg(feature = "unstable")))]
124pub fn nvim_echo(
125    lua: &Lua,
126    chunks: LuaTable,
127    history: bool,
128    opts: Option<LuaTable>,
129) -> LuaResult<()> {
130    vim::api::get(lua)?
131        .get::<_, LuaFunction>("nvim_echo")?
132        .call((chunks, history, opts))
133}
134
135/// Corresponds to `vim.api.nvim_err_write`
136#[cfg(feature = "unstable")]
137#[cfg_attr(docsrs, doc(cfg(feature = "unstable")))]
138pub fn nvim_err_write(lua: &Lua, msg: &str) -> LuaResult<()> {
139    vim::api::get(lua)?
140        .get::<_, LuaFunction>("nvim_err_write")?
141        .call(msg)
142}
143
144/// Corresponds to `vim.api.nvim_err_writeln`
145#[cfg(feature = "unstable")]
146#[cfg_attr(docsrs, doc(cfg(feature = "unstable")))]
147pub fn nvim_err_writeln(lua: &Lua, msg: &str) -> LuaResult<()> {
148    vim::api::get(lua)?
149        .get::<_, LuaFunction>("nvim_err_writeln")?
150        .call(msg)
151}
152
153/// Options for `nvim_eval_statusline`
154#[cfg(feature = "unstable")]
155#[cfg_attr(docsrs, doc(cfg(feature = "unstable")))]
156#[derive(Debug)]
157pub struct EvalStatuslineOpt {
158    winid: LuaInteger,
159    maxwidth: LuaInteger,
160    fillchar: String,
161    highlights: bool,
162    use_winbar: bool,
163    use_tabline: bool,
164}
165
166#[cfg(feature = "unstable")]
167#[cfg_attr(docsrs, doc(cfg(feature = "unstable")))]
168impl<'a> ToLua<'a> for EvalStatuslineOpt {
169    fn to_lua(self, lua: &'a Lua) -> LuaResult<LuaValue<'a>> {
170        let table = lua.create_table()?;
171        table.set("winid", self.winid)?;
172        table.set("maxwidth", self.maxwidth)?;
173        table.set("fillchar", self.fillchar)?;
174        table.set("highlights", self.highlights)?;
175        table.set("use_winbar", self.use_winbar)?;
176        table.set("use_tabline", self.use_tabline)?;
177        lua.pack(table)
178    }
179}
180
181/// Result of `nvim_eval_statusline`
182#[cfg(feature = "unstable")]
183#[cfg_attr(docsrs, doc(cfg(feature = "unstable")))]
184#[derive(Debug)]
185pub struct EvalStatuslineRes {
186    pub str: String,
187    pub width: LuaInteger,
188    pub highlights: Vec<HighlightInfo>,
189}
190
191/// Highlight info for `nvim_eval_statusline`
192#[cfg(feature = "unstable")]
193#[cfg_attr(docsrs, doc(cfg(feature = "unstable")))]
194#[derive(Debug)]
195pub struct HighlightInfo {
196    pub start: LuaInteger,
197    pub group: String,
198}
199
200#[cfg(feature = "unstable")]
201#[cfg_attr(docsrs, doc(cfg(feature = "unstable")))]
202impl<'a> FromLua<'a> for HighlightInfo {
203    fn from_lua(lua_value: LuaValue<'a>, _lua: &'a Lua) -> LuaResult<Self> {
204        match lua_value {
205            LuaValue::Table(table) => {
206                let start = table.get::<_, LuaInteger>("start")?;
207                let group = table.get::<_, String>("group")?;
208                Ok(HighlightInfo { start, group })
209            }
210            _ => {
211                return Err(LuaError::FromLuaConversionError {
212                    from: "LuaValue",
213                    to: "HighlightInfo",
214                    message: Some("Expected LuaValue::Table".to_string()),
215                })
216            }
217        }
218    }
219}
220
221#[cfg(feature = "unstable")]
222#[cfg_attr(docsrs, doc(cfg(feature = "unstable")))]
223impl<'a> FromLua<'a> for EvalStatuslineRes {
224    fn from_lua(lua_value: LuaValue<'a>, _lua: &'a Lua) -> LuaResult<Self> {
225        match lua_value {
226            LuaValue::Table(table) => {
227                let str = table.get::<_, String>("str")?;
228                let width = table.get::<_, LuaInteger>("width")?;
229                let highlights = table.get::<_, Vec<HighlightInfo>>("highlights")?;
230                Ok(EvalStatuslineRes {
231                    str,
232                    width,
233                    highlights,
234                })
235            }
236            _ => {
237                return Err(LuaError::FromLuaConversionError {
238                    from: "LuaValue",
239                    to: "EvalStatuslineRes",
240                    message: Some("Expected LuaValue::Table".to_string()),
241                })
242            }
243        }
244    }
245}
246
247/// Corresponds to `vim.api.nvim_eval_statusline`
248#[cfg(feature = "unstable")]
249#[cfg_attr(docsrs, doc(cfg(feature = "unstable")))]
250pub fn nvim_eval_statusline<'a>(
251    lua: &'a Lua,
252    expr: &str,
253    opt: Option<EvalStatuslineOpt>,
254) -> LuaResult<LuaTable<'a>> {
255    vim::api::get(lua)?
256        .get::<_, LuaFunction>("nvim_eval_statusline")?
257        .call((expr, opt))
258}
259
260/// Corresponds to `vim.api.nvim_exec_lua`
261#[cfg(feature = "unstable")]
262#[cfg_attr(docsrs, doc(cfg(feature = "unstable")))]
263pub fn nvim_exec_lua<'a>(
264    lua: &'a Lua,
265    code: &str,
266    args: Option<LuaTable<'a>>,
267) -> LuaResult<LuaValue<'a>> {
268    vim::api::get(lua)?
269        .get::<_, LuaFunction>("nvim_exec_lua")?
270        .call((code, args))
271}
272
273/// Corresponds to `vim.api.nvim_get_api_info`
274// TODO: return type
275#[cfg(feature = "unstable")]
276#[cfg_attr(docsrs, doc(cfg(feature = "unstable")))]
277pub fn nvim_get_api_info(lua: &Lua) -> LuaResult<(LuaInteger, LuaTable)> {
278    vim::api::get(lua)?
279        .get::<_, LuaFunction>("nvim_get_api_info")?
280        .call(())
281}
282
283/// Info struct for `nvim_get_chan_info`
284#[cfg(feature = "unstable")]
285#[cfg_attr(docsrs, doc(cfg(feature = "unstable")))]
286#[derive(Debug)]
287pub struct ChannelInfo<'a> {
288    pub id: LuaInteger,
289    pub stream: String,
290    pub mode: String,
291    pub pty: Option<String>,
292    pub argv: Vec<String>,
293    pub buffer: Option<String>,
294    pub client: Option<LuaTable<'a>>,
295}
296
297#[cfg(feature = "unstable")]
298#[cfg_attr(docsrs, doc(cfg(feature = "unstable")))]
299impl<'a> FromLua<'a> for ChannelInfo<'a> {
300    fn from_lua(lua_value: LuaValue<'a>, _lua: &'a Lua) -> LuaResult<Self> {
301        match lua_value {
302            LuaValue::Table(table) => {
303                let id = table.get::<_, LuaInteger>("id")?;
304                let stream = table.get::<_, String>("stream")?;
305                let mode = table.get::<_, String>("mode")?;
306                let pty = table.get::<_, Option<String>>("pty")?;
307                let argv = table.get::<_, Vec<String>>("argv")?;
308                let buffer = table.get::<_, Option<String>>("buffer")?;
309                let client = table.get::<_, Option<LuaTable<'a>>>("client")?;
310                Ok(ChannelInfo {
311                    id,
312                    stream,
313                    mode,
314                    pty,
315                    argv,
316                    buffer,
317                    client,
318                })
319            }
320            _ => {
321                return Err(LuaError::FromLuaConversionError {
322                    from: "LuaValue",
323                    to: "ChannelInfo",
324                    message: Some("Expected LuaValue::Table".to_string()),
325                })
326            }
327        }
328    }
329}
330
331/// Corresponds to `vim.api.nvim_get_chan_info`
332#[cfg(feature = "unstable")]
333#[cfg_attr(docsrs, doc(cfg(feature = "unstable")))]
334pub fn nvim_get_chan_info<'a>(lua: &'a Lua, chan: LuaInteger) -> LuaResult<ChannelInfo<'a>> {
335    vim::api::get(lua)?
336        .get::<_, LuaFunction>("nvim_get_chan_info")?
337        .call(chan)
338}
339
340/// Corresponds to `vim.api.nvim_get_color_by_name`
341/// Returns 24-bit RGB value or -1 for invalid color.
342#[cfg(feature = "unstable")]
343#[cfg_attr(docsrs, doc(cfg(feature = "unstable")))]
344pub fn nvim_get_color_by_name(lua: &Lua, name: &str) -> LuaResult<i64> {
345    vim::api::get(lua)?
346        .get::<_, LuaFunction>("nvim_get_color_by_name")?
347        .call(name)
348}
349
350/// Corresponds to `vim.api.nvim_get_color_map`
351// TODO: return type
352#[cfg(feature = "unstable")]
353#[cfg_attr(docsrs, doc(cfg(feature = "unstable")))]
354pub fn nvim_get_color_map(lua: &Lua) -> LuaResult<LuaTable> {
355    vim::api::get(lua)?
356        .get::<_, LuaFunction>("nvim_get_color_map")?
357        .call(())
358}
359
360#[cfg(feature = "unstable")]
361#[cfg_attr(docsrs, doc(cfg(feature = "unstable")))]
362pub type GetContextOpt = Vec<String>;
363
364/// Corresponds to `vim.api.nvim_get_context`
365// TODO: return type
366#[cfg(feature = "unstable")]
367#[cfg_attr(docsrs, doc(cfg(feature = "unstable")))]
368pub fn nvim_get_context(lua: &Lua, opt: Option<GetContextOpt>) -> LuaResult<LuaTable> {
369    vim::api::get(lua)?
370        .get::<_, LuaFunction>("nvim_get_context")?
371        .call(opt)
372}
373
374/// Corresponds to `vim.api.nvim_get_hl_by_id`
375#[cfg(feature = "unstable")]
376#[cfg_attr(docsrs, doc(cfg(feature = "unstable")))]
377pub fn nvim_get_hl_by_id<'a>(lua: &'a Lua, id: LuaInteger, rgb: bool) -> LuaResult<LuaTable<'a>> {
378    vim::api::get(lua)?
379        .get::<_, LuaFunction>("nvim_get_hl_by_id")?
380        .call((id, rgb))
381}
382
383/// Corresponds to `vim.api.nvim_get_hl_by_name`
384#[cfg(feature = "unstable")]
385#[cfg_attr(docsrs, doc(cfg(feature = "unstable")))]
386pub fn nvim_get_hl_by_name<'a>(lua: &'a Lua, name: &str, rgb: bool) -> LuaResult<LuaTable<'a>> {
387    vim::api::get(lua)?
388        .get::<_, LuaFunction>("nvim_get_hl_by_name")?
389        .call((name, rgb))
390}
391
392/// Corresponds to `vim.api.nvim_get_hl_id_by_name`
393#[cfg(feature = "unstable")]
394#[cfg_attr(docsrs, doc(cfg(feature = "unstable")))]
395pub fn nvim_get_hl_id_by_name(lua: &Lua, name: &str) -> LuaResult<i64> {
396    vim::api::get(lua)?
397        .get::<_, LuaFunction>("nvim_get_hl_id_by_name")?
398        .call(name)
399}
400
401#[derive(Debug)]
402#[cfg(feature = "unstable")]
403#[cfg_attr(docsrs, doc(cfg(feature = "unstable")))]
404pub struct Mapping<'a> {
405    pub buffer: LuaInteger,
406    pub expr: LuaValue<'a>,
407    pub lhs: String,
408    pub lhsraw: Vec<u8>,
409    pub lnum: LuaInteger,
410    pub mode: String,
411    pub noremap: bool,
412    pub nowait: bool,
413    pub script: LuaInteger,
414    pub sid: LuaInteger,
415    pub silent: bool,
416}
417
418#[cfg(feature = "unstable")]
419#[cfg_attr(docsrs, doc(cfg(feature = "unstable")))]
420impl<'a> FromLua<'a> for Mapping<'a> {
421    fn from_lua(lua_value: LuaValue<'a>, _lua: &'a Lua) -> LuaResult<Self> {
422        match lua_value {
423            LuaValue::Table(table) => {
424                let buffer = table.get::<_, LuaInteger>("buffer")?;
425                let expr = table.get::<_, LuaValue>("expr")?;
426                let lhs = table.get::<_, String>("lhs")?;
427                let lhsraw = table.get::<_, Vec<u8>>("lhsraw")?;
428                let lnum = table.get::<_, LuaInteger>("lnum")?;
429                let mode = table.get::<_, String>("mode")?;
430                let noremap = table.get::<_, bool>("noremap")?;
431                let nowait = table.get::<_, bool>("nowait")?;
432                let script = table.get::<_, LuaInteger>("script")?;
433                let sid = table.get::<_, LuaInteger>("sid")?;
434                let silent = table.get::<_, bool>("silent")?;
435                Ok(Mapping {
436                    buffer,
437                    expr,
438                    lhs,
439                    lhsraw,
440                    lnum,
441                    mode,
442                    noremap,
443                    nowait,
444                    script,
445                    sid,
446                    silent,
447                })
448            }
449            _ => {
450                return Err(LuaError::FromLuaConversionError {
451                    from: "LuaValue",
452                    to: "Mapping",
453                    message: Some("Expected LuaValue::Table".to_string()),
454                })
455            }
456        }
457    }
458}
459
460#[cfg(feature = "unstable")]
461#[cfg_attr(docsrs, doc(cfg(feature = "unstable")))]
462pub fn nvim_get_keymap<'a>(lua: &'a Lua, mode: &str) -> LuaResult<Vec<Mapping<'a>>> {
463    vim::api::get(lua)?
464        .get::<_, LuaFunction>("nvim_get_keymap")?
465        .call(mode)
466}
467
468#[cfg(feature = "unstable")]
469#[cfg_attr(docsrs, doc(cfg(feature = "unstable")))]
470pub fn nvim_get_mark(
471    lua: &Lua,
472    name: &str,
473) -> LuaResult<(LuaInteger, LuaInteger, LuaInteger, String)> {
474    vim::api::get(lua)?
475        .get::<_, LuaFunction>("nvim_get_mark")?
476        .call(name)
477}
478
479#[cfg(feature = "unstable")]
480#[cfg_attr(docsrs, doc(cfg(feature = "unstable")))]
481pub fn nvim_get_proc<'a>(lua: &'a Lua, pid: LuaInteger) -> LuaResult<LuaValue<'a>> {
482    vim::api::get(lua)?
483        .get::<_, LuaFunction>("nvim_get_proc")?
484        .call(pid)
485}