From bfc546c7a02a7c2f874aa6b48b583f901882f75f Mon Sep 17 00:00:00 2001 From: Techlive Zheng Date: Thu, 20 Jun 2013 07:56:03 +0800 Subject: [PATCH 1/7] Clean up code, rearrange it a bit --- plugin/minibufexpl.vim | 24 ++++++++++++------------ 1 file changed, 12 insertions(+), 12 deletions(-) diff --git a/plugin/minibufexpl.vim b/plugin/minibufexpl.vim index e2b3348..18005f4 100644 --- a/plugin/minibufexpl.vim +++ b/plugin/minibufexpl.vim @@ -1542,10 +1542,10 @@ function! BuildBufferList(curBufNum) call add(l:tabList, l:tab) endfor - if t:miniBufExplSortBy == "name" - call sort(l:tabList, "NameCmp") - elseif t:miniBufExplSortBy == "mru" + if t:miniBufExplSortBy == "mru" call sort(l:tabList, "MRUCmp") + elseif t:miniBufExplSortBy == "name" + call sort(l:tabList, "NameCmp") endif let l:miniBufExplBufList = '' @@ -1976,6 +1976,15 @@ function! UpdateBufferStateDict(bufNum,deleted) call DEBUG('Leaving UpdateBufferStateDict()',10) endfunction +" }}} +" MRUCmp - compares tabs based on MRU order {{{ +" +function! MRUCmp(tab1, tab2) + let l:buf1 = str2nr(matchstr(a:tab1, '[0-9]\+')) + let l:buf2 = str2nr(matchstr(a:tab2, '[0-9]\+')) + return index(s:MRUList, l:buf1) - index(s:MRUList, l:buf2) +endfunction + " }}} " NameCmp - compares tabs based on filename {{{ " @@ -1991,15 +2000,6 @@ function! NameCmp(tab1, tab2) endif endfunction -" }}} -" MRUCmp - compares tabs based on MRU order {{{ -" -function! MRUCmp(tab1, tab2) - let l:buf1 = str2nr(matchstr(a:tab1, '[0-9]\+')) - let l:buf2 = str2nr(matchstr(a:tab2, '[0-9]\+')) - return index(s:MRUList, l:buf1) - index(s:MRUList, l:buf2) -endfunction - " }}} " HasEligibleBuffers - Are there enough MBE eligible buffers to open the MBE window? {{{ " From 66d5ba5da63007105470671a6db7ca96638a202c Mon Sep 17 00:00:00 2001 From: Techlive Zheng Date: Thu, 20 Jun 2013 08:01:22 +0800 Subject: [PATCH 2/7] Fix techlivezheng/vim-plugin-minibufexpl#12 --- plugin/minibufexpl.vim | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/plugin/minibufexpl.vim b/plugin/minibufexpl.vim index 18005f4..ae25b2d 100644 --- a/plugin/minibufexpl.vim +++ b/plugin/minibufexpl.vim @@ -1546,6 +1546,9 @@ function! BuildBufferList(curBufNum) call sort(l:tabList, "MRUCmp") elseif t:miniBufExplSortBy == "name" call sort(l:tabList, "NameCmp") + else + " Sort by buffer number by default + call sort(l:tabList, "NumberCmp") endif let l:miniBufExplBufList = '' @@ -2000,6 +2003,15 @@ function! NameCmp(tab1, tab2) endif endfunction +" }}} +" NumberCmp - compares tabs based on buffer number {{{ +" +function! NumberCmp(tab1, tab2) + let l:buf1 = str2nr(matchstr(a:tab1, '[0-9]\+')) + let l:buf2 = str2nr(matchstr(a:tab2, '[0-9]\+')) + return l:buf1 - l:buf2 +endfunction + " }}} " HasEligibleBuffers - Are there enough MBE eligible buffers to open the MBE window? {{{ " From b17ff534072d6ee3062ce5dcb0b225f492a9a5f6 Mon Sep 17 00:00:00 2001 From: Techlive Zheng Date: Thu, 20 Jun 2013 09:18:55 +0800 Subject: [PATCH 3/7] Bug fix, there is a few lines of code never being executed --- plugin/minibufexpl.vim | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-) diff --git a/plugin/minibufexpl.vim b/plugin/minibufexpl.vim index ae25b2d..a716b00 100644 --- a/plugin/minibufexpl.vim +++ b/plugin/minibufexpl.vim @@ -2181,17 +2181,16 @@ function! GetSelectedBuffer() else let l:retv = substitute(@",'\([0-9]*\):.*', '\1', '') + 0 endif - let @" = l:save_reg - call DEBUG('Leaving GetSelectedBuffer()',10) - return l:retv else - let @" = l:save_reg - call DEBUG('Leaving GetSelectedBuffer()',10) - return -1 + let l:retv = -1 endif + let @" = l:save_reg let &report = l:save_rep let &showcmd = l:save_sc + + call DEBUG('Leaving GetSelectedBuffer()',10) + return l:retv endfunction " }}} From e1d4c5db734fa9d31745e266fb23208b060d4d2e Mon Sep 17 00:00:00 2001 From: Techlive Zheng Date: Thu, 20 Jun 2013 09:20:45 +0800 Subject: [PATCH 4/7] Extract getting buffer number from string to a separate function --- plugin/minibufexpl.vim | 42 ++++++++++++++++++++++++++++-------------- 1 file changed, 28 insertions(+), 14 deletions(-) diff --git a/plugin/minibufexpl.vim b/plugin/minibufexpl.vim index a716b00..c4786a6 100644 --- a/plugin/minibufexpl.vim +++ b/plugin/minibufexpl.vim @@ -1983,8 +1983,8 @@ endfunction " MRUCmp - compares tabs based on MRU order {{{ " function! MRUCmp(tab1, tab2) - let l:buf1 = str2nr(matchstr(a:tab1, '[0-9]\+')) - let l:buf2 = str2nr(matchstr(a:tab2, '[0-9]\+')) + let l:buf1 = GetBufferNumber(a:tab1) + let l:buf2 = GetBufferNumber(a:tab2) return index(s:MRUList, l:buf1) - index(s:MRUList, l:buf2) endfunction @@ -2007,8 +2007,8 @@ endfunction " NumberCmp - compares tabs based on buffer number {{{ " function! NumberCmp(tab1, tab2) - let l:buf1 = str2nr(matchstr(a:tab1, '[0-9]\+')) - let l:buf2 = str2nr(matchstr(a:tab2, '[0-9]\+')) + let l:buf1 = GetBufferNumber(a:tab1) + let l:buf2 = GetBufferNumber(a:tab2) return l:buf1 - l:buf2 endfunction @@ -2136,12 +2136,34 @@ function! QuitIfLastOpen() abort endif endfunction +" }}} +" GetBufferNumber - Get the buffer number from a formated string {{{ +" +function! GetBufferNumber(bufname) + call DEBUG('Entering GetBufferNumber()',10) + call DEBUG('The buffer name is '.a:bufname,9) + if !g:miniBufExplShowBufNumbers + " This is a bit ugly, but it works, unless we come up with a + " better way to get the key for a dictionary by its value. + let l:bufUniqNameDictKeys = keys(s:bufUniqNameDict) + let l:bufUniqNameDictValues = values(s:bufUniqNameDict) + let l:retv = l:bufUniqNameDictKeys[match(l:bufUniqNameDictValues,substitute(a:bufname,'\[*\([^\]]*\)\]*.*', '\1', ''))] + else + let l:retv = substitute(a:bufname,'\[*\([0-9]*\):[^\]]*\]*.*', '\1', '') + 0 + endif + call DEBUG('The buffer number is '.l:retv,9) + call DEBUG('Leaving GetBufferNumber()',10) + return str2nr(l:retv) +endfunction + " }}} " GetActiveBuffer {{{ " function! GetActiveBuffer() call DEBUG('Entering GetActiveBuffer()',10) - let l:bufNum = substitute(s:miniBufExplBufList,'\[\([0-9]*\):[^\]]*\][^\!]*!', '\1', '') + 0 + let l:bufStr = substitute(s:miniBufExplBufList,'.*\(\[[0-9]*:*[^\]]*\][^\!]*!\).*', '\1', '') + call DEBUG('Currently active buffer is '.l:bufStr,10) + let l:bufNum = GetBufferNumber(l:bufStr) call DEBUG('Currently active buffer is '.l:bufNum,10) call DEBUG('Leaving GetActiveBuffer()',10) return l:bufNum @@ -2172,15 +2194,7 @@ function! GetSelectedBuffer() let @" = "" normal ""yi[ if @" != "" - if !g:miniBufExplShowBufNumbers - " This is a bit ugly, but it works, unless we come up with a - " better way to get the key for a dictionary by its value. - let l:bufUniqNameDictKeys = keys(s:bufUniqNameDict) - let l:bufUniqNameDictValues = values(s:bufUniqNameDict) - let l:retv = l:bufUniqNameDictKeys[match(l:bufUniqNameDictValues,substitute(@",'[0-9]*:\(.*\)', '\1', ''))] - else - let l:retv = substitute(@",'\([0-9]*\):.*', '\1', '') + 0 - endif + let l:retv = GetBufferNumber(@") else let l:retv = -1 endif From bfc56c337f1c13e0958a72f3d419f8ebed67717c Mon Sep 17 00:00:00 2001 From: Techlive Zheng Date: Thu, 20 Jun 2013 16:47:11 +0800 Subject: [PATCH 5/7] Check to make sure our buffer is still there Some autocmds might have deleted it already. Fix fholgado/minibufexpl.vim#90. --- plugin/minibufexpl.vim | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/plugin/minibufexpl.vim b/plugin/minibufexpl.vim index c4786a6..eda6f6d 100644 --- a/plugin/minibufexpl.vim +++ b/plugin/minibufexpl.vim @@ -469,6 +469,12 @@ endfunction function! BufAddHandler() call DEBUG('Entering BufAdd Handler', 10) + " Check to make sure our buffer is still there, some autocmds + " might have deleted it already. + if !bufexists(expand("ListAdd(s:BufList,str2nr(expand(""))) call ListAdd(s:MRUList,str2nr(expand(""))) From 97272ee4d32c6223d2e3105b8dbf3ac7849150f5 Mon Sep 17 00:00:00 2001 From: Techlive Zheng Date: Thu, 20 Jun 2013 23:05:57 +0800 Subject: [PATCH 6/7] Revert "Check to make sure our buffer is still there" This reverts commit bfc56c337f1c13e0958a72f3d419f8ebed67717c. --- plugin/minibufexpl.vim | 6 ------ 1 file changed, 6 deletions(-) diff --git a/plugin/minibufexpl.vim b/plugin/minibufexpl.vim index eda6f6d..c4786a6 100644 --- a/plugin/minibufexpl.vim +++ b/plugin/minibufexpl.vim @@ -469,12 +469,6 @@ endfunction function! BufAddHandler() call DEBUG('Entering BufAdd Handler', 10) - " Check to make sure our buffer is still there, some autocmds - " might have deleted it already. - if !bufexists(expand("ListAdd(s:BufList,str2nr(expand(""))) call ListAdd(s:MRUList,str2nr(expand(""))) From a1f51f1a8ea9e9868cdc50cce21c3cac431009f0 Mon Sep 17 00:00:00 2001 From: Techlive Zheng Date: Sun, 23 Jun 2013 18:58:47 +0800 Subject: [PATCH 7/7] Be well with Vim session --- plugin/minibufexpl.vim | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/plugin/minibufexpl.vim b/plugin/minibufexpl.vim index c4786a6..8ef2aa8 100644 --- a/plugin/minibufexpl.vim +++ b/plugin/minibufexpl.vim @@ -434,7 +434,14 @@ function! VimEnterHandler() if g:miniBufExplAutoStart && t:miniBufExplAutoUpdate == 1 \ && (t:skipEligibleBuffersCheck == 1 || HasEligibleBuffers() == 1) - call StartExplorer(bufnr("%")) + + " VimEnter event will be triggered after a session is loaded, if there is + " already a MBE window, we need to update it at this point. + if FindWindow('-MiniBufExplorer-', 1) == -1 + call StartExplorer(bufnr("%")) + else + call UpdateExplorer(bufnr("%")) + endif " Let the MBE open in the new tab let s:TabsMBEState = 1