From db6e5083d9f975c3b63ed8506dcedb0c900309b1 Mon Sep 17 00:00:00 2001 From: Teddy Wing Date: Sat, 7 Apr 2018 23:10:15 +0200 Subject: Create `s:FilterTabPageElements()` Add a function that will filter out "Tab page 10" elements that don't have any files under them. Incomplete for now but I'm committing what I have here because it looks like I'm going to have to change things around just a bit. At the moment, the function will take the list of matched `:tabs` lines, make a list of the indexes of the "Tab page X" elements, and finally remove the empty "Tab page X" elements from that list, leaving only the indexes of those with files under them. Instead what I think I need is a list of the "Tab page X" indexes that don't have files so I can remove these from the list. --- plugin/tabs_grep.vim | 45 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 45 insertions(+) diff --git a/plugin/tabs_grep.vim b/plugin/tabs_grep.vim index 8b98f1b..70e7bce 100644 --- a/plugin/tabs_grep.vim +++ b/plugin/tabs_grep.vim @@ -10,6 +10,8 @@ function! TabsGrep(search) call filter(tabs, function('s:MatchString', [a:search])) echo tabs + call s:FilterTabPageElements(tabs) + " let filtered_tabs = system( " \ 'echo ' " \ . shellescape(tabs_output) @@ -25,4 +27,47 @@ function! s:MatchString(search, index, value) \ match(a:value, '\vTab page \d+') != -1 endfunction +function! s:IsTabPageLine(line) + return match(a:line, '\vTab page \d+') != -1 +endfunction + +function! s:FilterTabPageElements(list) + " Has matches: + " [0, 1, 1, 0, 0] + " + " Tab page line indexes: + " [1, 2, 4, 6, 7, 8] + " + " Tab page line indexes with matches: + " [2, 4] + + let tab_page_indexes = [] + + for i in range(len(a:list)) + if s:IsTabPageLine(a:list[i]) + call add(tab_page_indexes, i) + endif + endfor + + echo tab_page_indexes + + " loop 1..-1 + " if l[i-1] == l[i] - 1 + " delete l[i-1] + " if last l == len l - 1 + " delete last l + + let filtered_tab_page_indexes = copy(tab_page_indexes) + + for i in range(1, len(tab_page_indexes) - 1) + if tab_page_indexes[i - 1] == tab_page_indexes[i] - 1 + call remove(filtered_tab_page_indexes, i - 1) + endif + endfor + + if filtered_tab_page_indexes[-1] == len(a:list) - 1 + call remove(filtered_tab_page_indexes, -1) + endif +endfunction + command! -nargs=1 TabsGrep :call TabsGrep() -- cgit v1.2.3