Skip to content

Commit

Permalink
Move initial setup to lua
Browse files Browse the repository at this point in the history
This cleans up the code and makes it slightly faster
  • Loading branch information
fredizzimo committed Oct 1, 2023
1 parent aa210c7 commit 0eef475
Show file tree
Hide file tree
Showing 2 changed files with 111 additions and 120 deletions.
86 changes: 86 additions & 0 deletions lua/init.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
---@class Args
---@field neovide_channel_id integer
---@field register_clipboard boolean
---@field register_right_click boolean

---@type Args
local args = ...


vim.g.neovide_channel_id = args.neovide_channel_id

-- Set some basic rendering options.
vim.o.lazyredraw = false
vim.o.termguicolors = true

local function rpcnotify(method, ...)
vim.rpcnotify(vim.g.neovide_channel_id, method, ...)
end

local function rpcrequest(method, ...)
vim.rpcrequest(vim.g.neovide_channel_id, method, ...)
end

local function set_clipboard(register)
return function(lines, regtype)
rpcrequest("neovide.set_clipboard", lines)
end
end

local function get_clipboard(register)
return function()
return rpcrequest("neovide.get_clipboard", register)
end
end

if args.register_clipboard and not vim.g.neovide_no_custom_clipboard then
vim.g.clipboard = {
name = "neovide",
copy = {
["+"] = set_clipboard("+"),
["*"] = set_clipboard("*"),
},
paste = {
["+"] = get_clipboard("+"),
["*"] = get_clipboard("*"),
},
cache_enabled = 0
}
end

if args.register_right_click then
vim.api.nvim_create_user_command("NeovideRegisterRightClick", function()
rpcnotify("neovide.register_right_click")
end, {})
vim.api.nvim_create_user_command("NeovideUnregisterRightClick", function()
rpcnotify("neovide.unregister_right_click")
end, {})
end

vim.api.nvim_create_autocmd({ "OptionSet" }, {
pattern = "columns",
once = false,
nested = true,
callback = function()
rpcnotify("neovide.columns", tonumber(vim.v.option_new))
end
})

vim.api.nvim_create_autocmd({ "OptionSet" }, {
pattern = "lines",
once = false,
nested = true,
callback = function()
rpcnotify("neovide.lines", tonumber(vim.v.option_new))
end
})

-- Create auto command for retrieving exit code from neovim on quit.
vim.api.nvim_create_autocmd({ "VimLeavePre" }, {
pattern = "*",
once = true,
nested = true,
callback = function()
rpcnotify("neovide.quit", vim.v.exiting)
end
})
145 changes: 25 additions & 120 deletions src/bridge/setup.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,50 +5,7 @@ use rmpv::Value;
use super::setup_intro_message_autocommand;
use crate::{bridge::NeovimWriter, error_handling::ResultPanicExplanation};

const REGISTER_CLIPBOARD_PROVIDER_LUA: &str = r"
local function set_clipboard(register)
return function(lines, regtype)
vim.rpcrequest(vim.g.neovide_channel_id, 'neovide.set_clipboard', lines)
end
end
local function get_clipboard(register)
return function()
return vim.rpcrequest(vim.g.neovide_channel_id, 'neovide.get_clipboard', register)
end
end
vim.g.clipboard = {
name = 'neovide',
copy = {
['+'] = set_clipboard('+'),
['*'] = set_clipboard('*'),
},
paste = {
['+'] = get_clipboard('+'),
['*'] = get_clipboard('*'),
},
cache_enabled = 0
}";

pub async fn setup_neovide_remote_clipboard(nvim: &Neovim<NeovimWriter>) {
// Users can opt-out with
// vim: `let g:neovide_no_custom_clipboard = v:true`
// lua: `vim.g.neovide_no_custom_clipboard = true`
let no_custom_clipboard = nvim
.get_var("neovide_no_custom_clipboard")
.await
.ok()
.and_then(|v| v.as_bool());
if Some(true) == no_custom_clipboard {
info!("Neovide working remotely but custom clipboard is disabled");
return;
}

nvim.execute_lua(REGISTER_CLIPBOARD_PROVIDER_LUA, vec![])
.await
.ok();
}
const INIT_LUA: &str = include_str!("../../lua/init.lua");

pub async fn setup_neovide_specific_state(
nvim: &Neovim<NeovimWriter>,
Expand Down Expand Up @@ -94,91 +51,39 @@ pub async fn setup_neovide_specific_state(
.ok()
.and_then(|info| info[0].as_u64());

if let Some(neovide_channel) = neovide_channel {
let neovide_channel = if let Some(neovide_channel) = neovide_channel {
// Record the channel to the log.
info!(
"Neovide registered to nvim with channel id {}",
neovide_channel
);

nvim.set_var("neovide_channel_id", Value::from(neovide_channel))
.await
.ok();

// Create a command for registering right click context hooking.
#[cfg(windows)]
nvim.command(&build_neovide_command(
neovide_channel,
0,
"NeovideRegisterRightClick",
"register_right_click",
))
.await
.ok();

// Create a command for unregistering the right click context hooking.
#[cfg(windows)]
nvim.command(&build_neovide_command(
neovide_channel,
0,
"NeovideUnregisterRightClick",
"unregister_right_click",
))
.await
.ok();

if should_handle_clipboard {
setup_neovide_remote_clipboard(nvim).await;
}
Value::from(neovide_channel)
} else {
warn!("Neovide could not find the correct channel id. Some functionality may be disabled.");
}
Value::Nil
};

// Set some basic rendering options.
nvim.set_option("lazyredraw", Value::Boolean(false))
let register_clipboard = should_handle_clipboard;
let register_right_click = cfg!(target_os = "windows");

let args = Value::from(vec![
(Value::from("neovide_channel_id"), neovide_channel),
(
Value::from("register_clipboard"),
Value::from(register_clipboard),
),
(
Value::from("register_right_click"),
Value::from(register_right_click),
),
]);

nvim.execute_lua(INIT_LUA, vec![args])
.await
.ok();
nvim.set_option("termguicolors", Value::Boolean(true))
.await
.ok();
.unwrap_or_explained_panic("Error when running Neovide init.lua");

// Create auto command for retrieving exit code from neovim on quit.
nvim.command(
"autocmd VimLeavePre * call rpcnotify(g:neovide_channel_id, 'neovide.quit', v:exiting)",
)
.await
.ok();

nvim.command(
"autocmd OptionSet columns call rpcnotify(g:neovide_channel_id, 'neovide.columns', str2nr(v:option_new))",
)
.await
.ok();
nvim.command(
"autocmd OptionSet lines call rpcnotify(g:neovide_channel_id, 'neovide.lines', str2nr(v:option_new))",
)
.await
.ok();

setup_intro_message_autocommand(nvim).await.ok();
}

#[cfg(windows)]
pub fn build_neovide_command(channel: u64, num_args: u64, command: &str, event: &str) -> String {
let nargs: String = if num_args > 1 {
"+".to_string()
} else {
num_args.to_string()
};
if num_args == 0 {
format!(
"command! -nargs={} {} call rpcnotify({}, 'neovide.{}')",
nargs, command, channel, event
)
} else {
format!(
"command! -nargs={} -complete=expression {} call rpcnotify({}, 'neovide.{}', <args>)",
nargs, command, channel, event
)
}
setup_intro_message_autocommand(nvim)
.await
.unwrap_or_explained_panic("Error setting up intro message");
}

0 comments on commit 0eef475

Please sign in to comment.