@@ -31,6 +31,10 @@ function M.yazi(config, input_path, args)
3131 config =
3232 vim .tbl_deep_extend (" force" , configModule .default (), M .config , config or {})
3333
34+ -- M.config called from M.setup is different
35+ -- from this one. Must be called explicitly
36+ M .yazi_nest_config = config
37+
3438 local Log = require (" yazi.log" )
3539 Log .level = config .log_level
3640
@@ -51,11 +55,11 @@ function M.yazi(config, input_path, args)
5155
5256 local win = require (" yazi.window" ).YaziFloatingWindow .new (config )
5357 win :open_and_display ()
54- local yazi_buffer = win .content_buffer
58+ vim . g . yazi_buffer = win .content_buffer
5559
5660 local yazi_process , yazi_context = YaziProcess :start (config , paths , {
5761 on_ya_first_event = function (api )
58- config .hooks .on_yazi_ready (yazi_buffer , config , api )
62+ config .hooks .on_yazi_ready (vim . g . yazi_buffer , config , api )
5963 do
6064 if not (args and args .reveal_path ) then
6165 Log :debug (" No reveal_path provided, skipping initial reveal" )
@@ -139,15 +143,18 @@ function M.yazi(config, input_path, args)
139143 })
140144
141145 M .active_contexts :push (yazi_context )
146+ -- Sometimes M.active_contexts:peek() causes vim.keymap.set
147+ -- to fail, so we copy the context variable directly
148+ M .yazi_nest_context = yazi_context
142149
143150 config .hooks .yazi_opened (path .filename , win .content_buffer , config )
144151
145152 if config .set_keymappings_function ~= nil then
146- config .set_keymappings_function (yazi_buffer , config , yazi_context )
153+ config .set_keymappings_function (vim . g . yazi_buffer , config , yazi_context )
147154 end
148155
149156 if config .keymaps ~= false then
150- require (" yazi.config" ).set_keymappings (yazi_buffer , config , yazi_context )
157+ require (" yazi.config" ).set_keymappings (vim . g . yazi_buffer , config , yazi_context )
151158 end
152159
153160 win .on_resized = function (event )
@@ -211,6 +218,70 @@ function M.setup(opts)
211218 then
212219 require (" yazi.integrations.snacks_relative_path" ).setup_copy_relative_path_picker_action_once ()
213220 end
221+
222+ -- #739: The plugin isn't aware of nested nvim sessions inside
223+ -- yazi, so the child process must control the parent via RPC.
224+ local chan_request = function (chan , cmd , args )
225+ vim .rpcrequest (
226+ chan ,
227+ " nvim_exec_lua" ,
228+ string.dump (cmd ),
229+ args
230+ )
231+ end
232+
233+ vim .api .nvim_create_autocmd (" VimEnter" , {
234+ pattern = " /tmp/yazi-*" ,
235+ callback = function ()
236+ if vim .env .NVIM == nil then return end
237+ local chan = vim .fn .sockconnect (" pipe" , vim .env .NVIM , { rpc = true })
238+ local cmd = function (cfg )
239+ vim .keymap .del ({ " t" }, cfg .open_file_in_vertical_split , { buffer = vim .g .yazi_buffer })
240+ vim .keymap .del ({ " t" }, cfg .open_file_in_horizontal_split , { buffer = vim .g .yazi_buffer })
241+ vim .keymap .del ({ " t" }, cfg .open_file_in_tab , { buffer = vim .g .yazi_buffer })
242+ end
243+ chan_request (chan , cmd , { M .config .keymaps })
244+ end ,
245+ })
246+ vim .api .nvim_create_autocmd (" VimLeavePre" , {
247+ pattern = " /tmp/yazi-*" ,
248+ callback = function ()
249+ if vim .env .NVIM == nil then return end
250+ local chan = vim .fn .sockconnect (" pipe" , vim .env .NVIM , { rpc = true })
251+ local cmd = function ()
252+ vim .api .nvim_exec_autocmds (" User" , { pattern = " YaziNestedClosed" })
253+ end
254+ chan_request (chan , cmd , {})
255+ end
256+ })
257+
258+ vim .api .nvim_create_autocmd (" User" , {
259+ group = yazi_augroup ,
260+ pattern = " YaziNestedClosed" ,
261+ callback = function ()
262+ local config = M .yazi_nest_config
263+ local context = M .yazi_nest_context
264+ local keybinding_helpers = require (" yazi.keybinding_helpers" )
265+ vim .keymap .set (
266+ { " t" },
267+ config .keymaps .open_file_in_vertical_split ,
268+ function () keybinding_helpers .open_file_in_vertical_split (config , context .api ) end ,
269+ { buffer = vim .g .yazi_buffer }
270+ )
271+ vim .keymap .set (
272+ { " t" },
273+ config .keymaps .open_file_in_horizontal_split ,
274+ function () keybinding_helpers .open_file_in_horizontal_split (config , context .api ) end ,
275+ { buffer = vim .g .yazi_buffer }
276+ )
277+ vim .keymap .set (
278+ { " t" },
279+ config .keymaps .open_file_in_tab ,
280+ function () keybinding_helpers .open_file_in_tab (config , context .api ) end ,
281+ { buffer = vim .g .yazi_buffer }
282+ )
283+ end
284+ })
214285end
215286
216287return M
0 commit comments