Last active
February 29, 2020 19:36
-
-
Save mhinz/9eb7b46571d54c6cc1187c65a3e83e3a to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
augroup grepper-side | |
autocmd! | |
autocmd FileType GrepperSide silent! keeppatterns %s/^\ze[^\>]/ / | 1 | |
augroup END | |
function! s:context_jump(close_window) | |
let fileline = search('\v^\>\>\> [[:alnum:]\/\-_.~]+:\d+', 'bcn') | |
if empty(fileline) | |
return | |
endif | |
let [filename, line] = | |
\ matchlist(getline(fileline), '\v^\>\>\> ([[:alnum:]\/\-_.~]+):(\d+)')[1:2] | |
if a:close_window | |
bdelete | |
execute 'edit +'.line filename | |
else | |
wincmd p | |
execute 'edit +'.line filename | |
wincmd p | |
endif | |
endfunction | |
Grepper -tool ag -noopen -nojump -highlight -query foo | |
sleep 1 | |
vnew | |
" Contexts are lists of a fixed format: | |
" | |
" [0] = actual line number | |
" [1] = begin of context | |
" [2] = end of context | |
let contexts = {} | |
" process quickfix entries | |
for entry in getqflist() | |
let bufname = bufname(entry.bufnr) | |
if has_key(contexts, bufname) | |
if (contexts[bufname][-1][2] + 2) > entry.lnum | |
" merge entries that are close to each other into the same context | |
let contexts[bufname][-1][2] = entry.lnum + 2 | |
else | |
" new context in same file | |
let start = entry.lnum - 4 | |
if start < 0 | |
let start = 0 | |
endif | |
let contexts[bufname] += [[entry.lnum, start, entry.lnum+2]] | |
endif | |
else | |
" new context in new file | |
let start = entry.lnum - 4 | |
if start < 0 | |
let start = 0 | |
endif | |
let contexts[bufname] = [[entry.lnum, start, entry.lnum+2]] | |
end | |
endfor | |
" write contexts to buffer | |
for [filename, contexts] in items(contexts) | |
let file = readfile(expand(filename)) | |
for context in contexts | |
call append('$', '>>> '. filename .':'. context[0]) | |
call append('$', file[context[1]:context[2]]) | |
endfor | |
call append('$', '') | |
endfor | |
silent $delete _ | |
silent 1delete _ | |
nnoremap <buffer> <cr> :call <sid>context_jump(1)<cr> | |
nnoremap <buffer> o :call <sid>context_jump(0)<cr> | |
nnoremap <buffer> } :call search('\v^\>\>\> [[:alnum:]\/\-_.~]+:\d+')<cr> | |
nnoremap <buffer> { :call search('\v^\>\>\> [[:alnum:]\/\-_.~]+:\d+', 'b')<cr> | |
setfiletype GrepperSide | |
syntax match GrepperSideFile /\v^\>\>\> [[:alnum:]\/\-_.~]+:\d+/ | |
highlight default link GrepperSideFile Directory | |
setlocal buftype=nofile | |
let nummatches = len(getqflist()) | |
let numfiles = len(uniq(map(getqflist(), 'bufname(v:val.bufnr)'))) | |
let &l:statusline = printf(' Found %d matches in %d files.', nummatches, numfiles) | |
normal! zR " open all folds | |
normal! n " jump to first match |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment