Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix CommandTOpen (GotoOrOpen) command #315

Closed
wants to merge 2 commits into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 20 additions & 8 deletions autoload/commandt.vim
Original file line number Diff line number Diff line change
Expand Up @@ -139,21 +139,34 @@ function! commandt#CheckBuffer(buffer_number) abort
endif
endfunction

function! s:BufHidden(buffer)
" visible == exists, loaded, listed and not hidden
" (buffer is opened in a window - in current or another tab)
function! s:BufVisible(buffer)
" buffer is opened in current tab (quick check for current tab)
if bufwinnr('^' . a:buffer . '$') != -1 | return 1 | end
" buffer exists if it has been opened at least once (unless wiped)
if !bufexists(a:buffer) | return 0 | end
" buffer is not loaded when its last window is closed (`set nohidden` only)
if !bufloaded(a:buffer) | return 0 | end
" buffer is not listed when it's deleted
if !buflisted(a:buffer) | return 0 | end

let bufno = bufnr(a:buffer)
let listed_buffers = ''
let ls_buffers = ''

redir => listed_buffers
redir => ls_buffers
silent ls
redir END

for line in split(listed_buffers, "\n")
" buffer is hidden when its last window is closed (`set hidden` only)
for line in split(ls_buffers, "\n")
let components = split(line)
if components[0] == bufno
return match(components[1], 'h') != -1
return match(components[1], 'h') == -1
endif
endfor
return 0

return 1
endfunction

function! commandt#GotoOrOpen(command_and_args) abort
Expand All @@ -164,8 +177,7 @@ function! commandt#GotoOrOpen(command_and_args) abort
" `bufwinnr()` doesn't see windows in other tabs, meaning we open them again
" instead of switching to the other tab; but `bufname()` sees hidden
" buffers, and if we try to open one of those, we get an unwanted split.
if bufwinnr('^' . l:file . '$') != -1 ||
\ (bufname('^' . l:file . '$') !=# '' && !s:BufHidden(l:file))
if s:BufVisible(l:file)
execute 'sbuffer ' . l:file
else
execute l:command . l:file
Expand Down