@@ -783,65 +783,75 @@ M.open_file = function(state, path, open_cmd, bufnr)
783783 end
784784 end
785785
786- if M .truthy (path ) then
787- local relative = require (" neo-tree" ).config .open_files_using_relative_paths
788- local escaped_path = M .escape_path_for_cmd (relative and vim .fn .fnamemodify (path , " :." ) or path )
789- local bufnr_or_path = bufnr or escaped_path
790- local events = require (" neo-tree.events" )
791- local result = true
792- local err = nil
793- local event_result = events .fire_event (events .FILE_OPEN_REQUESTED , {
794- state = state ,
795- path = path ,
796- open_cmd = open_cmd ,
797- bufnr = bufnr ,
798- }) or {}
799- if event_result .handled then
800- events .fire_event (events .FILE_OPENED , path )
801- return
802- end
803- if state .current_position == " current" then
804- --- @diagnostic disable-next-line : param-type-mismatch
805- result , err = pcall (vim .cmd , open_cmd .. " " .. bufnr_or_path )
806- else
807- local winid , is_neo_tree_window = M .get_appropriate_window (state )
808- vim .api .nvim_set_current_win (winid )
809- -- TODO: make this configurable, see issue #43
810- if is_neo_tree_window then
811- local width = vim .api .nvim_win_get_width (0 )
812- if width == vim .o .columns then
813- -- Neo-tree must be the only window, restore it's status as a sidebar
814- width = M .get_value (state , " window.width" , 40 , false )
815- width = M .resolve_width (width )
816- end
817- result , err = M .force_new_split (state .current_position , escaped_path )
818- vim .api .nvim_win_set_width (winid , width )
819- else
820- --- @diagnostic disable-next-line : param-type-mismatch
821- result , err = pcall (vim .cmd , open_cmd .. " " .. bufnr_or_path )
822- end
823- end
824- if not result and string.find (err or " " , " winfixbuf" ) and M .is_winfixbuf () then
825- local winid , is_neo_tree_window = M .get_appropriate_window (state , true )
826- -- Rescan window list to find a window that is not winfixbuf.
827- -- If found, retry executing command in that window,
828- -- otherwise, all windows are either neo-tree or winfixbuf so we make a new split.
829- if not is_neo_tree_window and not M .is_winfixbuf (winid ) then
830- vim .api .nvim_set_current_win (winid )
831- --- @diagnostic disable-next-line : param-type-mismatch
832- result , err = pcall (vim .cmd , open_cmd .. " " .. bufnr_or_path )
833- else
834- result , err = M .force_new_split (state .current_position , escaped_path )
786+ if not M .truthy (path ) then
787+ return
788+ end
789+
790+ local config = require (" neo-tree" ).config
791+ local relative = config .open_files_using_relative_paths
792+ local escaped_path = M .escape_path_for_cmd (relative and vim .fn .fnamemodify (path , " :." ) or path )
793+ local bufnr_or_path = bufnr or escaped_path
794+ local events = require (" neo-tree.events" )
795+ local event_result = events .fire_event (events .FILE_OPEN_REQUESTED , {
796+ state = state ,
797+ path = path ,
798+ open_cmd = open_cmd ,
799+ bufnr = bufnr ,
800+ }) or {}
801+ if event_result .handled then
802+ events .fire_event (events .FILE_OPENED , path )
803+ return
804+ end
805+
806+ local command = open_cmd .. " " .. bufnr_or_path
807+ if config .keep_altfile then
808+ -- see `:h g:netrw_altfile`
809+ command = " keepalt " .. command
810+ end
811+
812+ local result , err = true , nil
813+ if state .current_position == " current" then
814+ --- @diagnostic disable-next-line : param-type-mismatch
815+ result , err = pcall (vim .cmd , command )
816+ else
817+ local winid , is_neo_tree_window = M .get_appropriate_window (state )
818+ vim .api .nvim_set_current_win (winid )
819+ -- TODO: make this configurable, see issue #43
820+ if is_neo_tree_window then
821+ local width = vim .api .nvim_win_get_width (0 )
822+ if width == vim .o .columns then
823+ -- Neo-tree must be the only window, restore it's status as a sidebar
824+ width = M .get_value (state , " window.width" , 40 , false )
825+ width = M .resolve_width (width )
835826 end
827+ result , err = M .force_new_split (state .current_position , escaped_path )
828+ vim .api .nvim_win_set_width (winid , width )
829+ else
830+ --- @diagnostic disable-next-line : param-type-mismatch
831+ result , err = pcall (vim .cmd , command )
836832 end
837- if result or err == " Vim(edit):E325: ATTENTION" then
838- -- fixes #321
839- vim .bo [0 ].buflisted = true
840- events .fire_event (events .FILE_OPENED , path )
833+ end
834+
835+ if not result and string.find (err or " " , " winfixbuf" ) and M .is_winfixbuf () then
836+ local winid , is_neo_tree_window = M .get_appropriate_window (state , true )
837+ -- Rescan window list to find a window that is not winfixbuf.
838+ -- If found, retry executing command in that window,
839+ -- otherwise, all windows are either neo-tree or winfixbuf so we make a new split.
840+ if not is_neo_tree_window and not M .is_winfixbuf (winid ) then
841+ vim .api .nvim_set_current_win (winid )
842+ --- @diagnostic disable-next-line : param-type-mismatch
843+ result , err = pcall (vim .cmd , command )
841844 else
842- log . error ( " Error opening file: " , err )
845+ result , err = M . force_new_split ( state . current_position , escaped_path )
843846 end
844847 end
848+ if result or err == " Vim(edit):E325: ATTENTION" then
849+ -- fixes #321
850+ vim .bo [0 ].buflisted = true
851+ events .fire_event (events .FILE_OPENED , path )
852+ else
853+ log .error (" Error opening file:" , err )
854+ end
845855end
846856
847857M .reduce = function (list , memo , func )
0 commit comments