Closed keflavich closed 11 years ago
Here's how to recreate the bug: 1) Open a fresh vim 2) split the window either with :split, :new or probably some other commands
3) open a new tab with :tabnew $MYVIMRC
4) test your function with :echo WhichTab($MYVIMRC) and it should return "3".
You only have 2 tabs open, though, and tabs are numbered from [1, N)
The reason for the bug can be seen if you switch back to the first tab with the 2 windows in it. This tab has 2 open buffers, as you'll see if you say :echo tabpagebuflist(). For further clarification, check the output of :buffers in the first tab, and you'll see the 2 numbered buffers in those 2 windows.
NOTE: the list returned by WhichTabNo may contain more than one tab, because the same buffer may be open in more than one place. It will return them in left to right order with regards to the tab number. You could even have the same tab number a couple times in a row too if the same buffer is opened in more than one window in the same tab. For my needs, I decided that I would use the first (leftmost) tab found, so I return them in this order. If you would rather use the rightmost tab, you could look at the last item in the tabNo list returned, or you could change:
for tabNo in range(1, tabpagenr("$"))
to:
for tabNo in range(tabpagenr("$"), 1, -1)
Also of note, I have a bug I'm working on in my script (which is pretty similar to yours) with regard to undisplayed buffers. If you open a file with :e somefile, and then change to a different file with :e someotherfile, then check the output of :buffers, you'll see that there's still a buffer for somefile, even though it's not currently open. I just need to figure out the best way to filter for those.
OK, cool, that works. I've merged your code with mine (so that it searches for a buffer name instead of a buffer number). I don't deal with the case of the file being in a buffer but not a tab, though. What are you trying to filter for, in the case of closed files? Right now, they just aren't found in WhichTab.vim
Yeah, I think my bug was elsewhere and I was interpreting it wrong. Looks like we're both safe on that front.
OK, this one looks solved. Thanks.
See bug description here: http://stackoverflow.com/questions/8839846/vim-check-if-a-file-is-open-in-current-tab-window-and-activate-it/14427211#14427211