@@ -3,9 +3,11 @@ require("common.common_functions")
33local response_sock = nil
44local unception_quitpre_autocmd_id = nil
55local unception_bufunload_autocmd_id = nil
6+ local unception_tabclosed_autocmd_id = nil
67local filepath_to_check = nil
78local blocked_terminal_buffer_id = nil
89local last_replaced_buffer_id = nil
10+ local last_tabpage = nil
911
1012local function unblock_client_and_reset_state ()
1113 -- Remove the autocmds we made.
@@ -25,6 +27,11 @@ local function unblock_client_and_reset_state()
2527 last_replaced_buffer_id = nil
2628end
2729
30+ local function unception_reset_last_tab ()
31+ vim .api .nvim_del_autocmd (unception_tabclosed_autocmd_id )
32+ last_tabpage = nil
33+ end
34+
2835function _G .unception_handle_bufunload (unloaded_buffer_filepath )
2936 unloaded_buffer_filepath = unception_get_absolute_filepath (unloaded_buffer_filepath )
3037 unloaded_buffer_filepath = unception_escape_special_chars (unloaded_buffer_filepath )
@@ -50,6 +57,14 @@ function _G.unception_handle_quitpre(quitpre_buffer_filepath)
5057 end
5158end
5259
60+ function _G .unception_handle_tabclosed ()
61+ -- only switch tab when needed this will depend on the config
62+ if last_tabpage ~= nil then
63+ vim .api .nvim_set_current_tabpage (last_tabpage )
64+ end
65+ unception_reset_last_tab ()
66+ end
67+
5368function _G .unception_notify_when_done_editing (pipe_to_respond_on , filepath )
5469 filepath_to_check = filepath
5570 blocked_terminal_buffer_id = last_replaced_buffer_id
@@ -60,6 +75,12 @@ function _G.unception_notify_when_done_editing(pipe_to_respond_on, filepath)
6075 -- Create an autocmd for BufUnload as a failsafe should QuitPre not get triggered on the target buffer (e.g. if a user runs :bdelete).
6176 unception_bufunload_autocmd_id = vim .api .nvim_create_autocmd (" BufUnload" ,
6277 { command = " lua unception_handle_bufunload(vim.fn.expand('<afile>:p'))" })
78+
79+ -- When done editing in another tab we can't use QuitPre becuase if we switch
80+ -- back to the previous tabpage within QuitPre it will prevent the tab to be closed
81+ -- So here we use the TabClosed event
82+ unception_tabclosed_autocmd_id = vim .api .nvim_create_autocmd (" TabClosed" ,
83+ { callback = unception_handle_tabclosed })
6384end
6485
6586local open_methods_table = {
@@ -136,6 +157,9 @@ function _G.unception_edit_files(file_args, options)
136157 local open_method = unception_detect_open_method (options )
137158
138159 if (# file_args > 0 ) then
160+ if options .open_in_new_tab or open_method == " tabnew" then
161+ last_tabpage = vim .api .nvim_get_current_tabpage ()
162+ end
139163 -- if argadd is selected but we have only one file
140164 -- let's not use argadd so we can use line number specifier
141165 if (open_method ~= " argadd" or # file_args == 1 ) then
0 commit comments