@@ -25,18 +25,21 @@ local function try_netrw_hijack(path)
2525 return require (" neo-tree.setup.netrw" ).hijack ()
2626end
2727
28- local augroup = vim .api .nvim_create_augroup (" NeoTree_NetrwDeferred " , { clear = true })
28+ local augroup = vim .api .nvim_create_augroup (" NeoTree " , { clear = true })
2929
3030-- lazy load until bufenter/netrw hijack
3131vim .api .nvim_create_autocmd ({ " BufEnter" }, {
3232 group = augroup ,
33+ desc = " Lazy-load until bufenter/opened dir" ,
3334 callback = function (args )
3435 return vim .g .neotree_watching_bufenter == 1 or try_netrw_hijack (args .file )
3536 end ,
3637})
3738
3839-- track window order
3940vim .api .nvim_create_autocmd ({ " WinEnter" }, {
41+ group = augroup ,
42+ desc = " Track prior windows for opening intuitiveness" ,
4043 callback = function (ev )
4144 local win = vim .api .nvim_get_current_win ()
4245 local utils = require (" neo-tree.utils" )
@@ -73,11 +76,81 @@ vim.api.nvim_create_autocmd({ "WinEnter" }, {
7376
7477-- setup session loading
7578vim .api .nvim_create_autocmd (" SessionLoadPost" , {
79+ group = augroup ,
80+ desc = " Session loading" ,
7681 callback = function ()
7782 if require (" neo-tree" ).ensure_config ().auto_clean_after_session_restore then
7883 require (" neo-tree.ui.renderer" ).clean_invalid_neotree_buffers (true )
7984 end
8085 end ,
8186})
8287
88+ vim .api .nvim_create_autocmd (" WinClosed" , {
89+ group = augroup ,
90+ desc = " close_if_last_window autocmd" ,
91+ callback = function (args )
92+ local closing_win = tonumber (args .match )
93+ local visible_winids = vim .api .nvim_tabpage_list_wins (0 )
94+ local other_panes = {}
95+ local utils = require (" neo-tree.utils" )
96+ for _ , winid in ipairs (visible_winids ) do
97+ if not utils .is_floating (winid ) and winid ~= closing_win then
98+ other_panes [# other_panes + 1 ] = winid
99+ end
100+ end
101+
102+ if # other_panes ~= 1 then
103+ return
104+ end
105+
106+ local remaining_pane = other_panes [1 ]
107+ local remaining_buf = vim .api .nvim_win_get_buf (remaining_pane )
108+
109+ if vim .bo [remaining_buf ].filetype ~= " neo-tree" then
110+ return
111+ end
112+
113+ local position = vim .b [remaining_buf ].neo_tree_position
114+ local source = vim .b [remaining_buf ].neo_tree_source
115+ -- close_if_last_window just doesn't make sense for a split style
116+ if position == " current" then
117+ return
118+ end
119+
120+ local log = require (" neo-tree.log" )
121+ log .trace (" last window, closing" )
122+ local state = require (" neo-tree.sources.manager" ).get_state (source )
123+ if not state then
124+ return
125+ end
126+ if not require (" neo-tree" ).ensure_config ().close_if_last_window then
127+ return
128+ end
129+ local mod = utils .get_opened_buffers ()
130+ log .debug (" close_if_last_window, modified files found: " , vim .inspect (mod ))
131+ for filename , buf_info in pairs (mod ) do
132+ if buf_info .modified then
133+ local buf_name , message
134+ if vim .startswith (filename , " [No Name]#" ) then
135+ buf_name = string.sub (filename , 11 )
136+ message =
137+ " Cannot close because an unnamed buffer is modified. Please save or discard this file."
138+ else
139+ buf_name = filename
140+ message =
141+ " Cannot close because one of the files is modified. Please save or discard changes."
142+ end
143+ log .trace (" close_if_last_window, showing unnamed modified buffer: " , filename )
144+ vim .schedule (function ()
145+ log .warn (message )
146+ vim .cmd (" rightbelow vertical split" )
147+ vim .api .nvim_win_set_width (0 , state .window .width or 40 )
148+ vim .cmd (" b " .. buf_name )
149+ end )
150+ return
151+ end
152+ end
153+ end ,
154+ })
155+
83156vim .g .loaded_neo_tree = 1
0 commit comments