Vim Tips Wiki
(→‎Fold away misses: FIX: Negative default count doesn't work, also cannot default to 0, so use 99999 as a workaround.)
(minor tweaks, replace old comments with request for ideas on 801)
Line 5: Line 5:
 
|created=2001
 
|created=2001
 
|complexity=intermediate
 
|complexity=intermediate
|author=Mark A. Hillebrand
+
|author=
 
|version=7.0
 
|version=7.0
 
|rating=1/3
 
|rating=1/3
Line 13: Line 13:
 
After executing <tt>:make</tt> or <tt>:grep</tt> you can browse the list of errors/matches, and the appropriate source code locations, with commands like <tt>:cnext</tt>. {{help|quickfix}}
 
After executing <tt>:make</tt> or <tt>:grep</tt> you can browse the list of errors/matches, and the appropriate source code locations, with commands like <tt>:cnext</tt>. {{help|quickfix}}
   
It can be useful to fold away the lines that have no errors (when using <tt>:make</tt>), or that do not match the search pattern (when using <tt>:grep</tt>).
+
This tip [[Folding|folds away lines]] in the current buffer that have no errors (when using <tt>:make</tt>), or that do not match the search pattern (when using <tt>:grep</tt>). See [[VimTip801|fold quickfix list]] to fold lines in the quickfix list.
   
 
==Fold away misses==
 
==Fold away misses==
Line 29: Line 29:
 
<pre>
 
<pre>
 
if ! exists('g:foldmisses_context')
 
if ! exists('g:foldmisses_context')
let g:foldmisses_context = 0
+
let g:foldmisses_context = 0
 
endif
 
endif
   
Line 63: Line 63:
 
endfunction
 
endfunction
   
":[N]FoldMisses [N] Show only the lines (and surrounding [N] lines of
+
":[N]FoldMisses [N] Show only the lines (and surrounding [N] lines
":[N]FoldLMisses [N] context) in the current buffer that appear in the
+
":[N]FoldLMisses [N] of context) in the current buffer that appear
" quickfix / location list. Missed, error-free lines are
+
" in the quickfix / location list.
  +
" Missed, error-free lines are folded away.
" folded away.
 
 
command! -bar -count=99999 FoldMisses call <SID>FoldMisses(getqflist(), <count>)
 
command! -bar -count=99999 FoldMisses call <SID>FoldMisses(getqflist(), <count>)
 
command! -bar -count=99999 FoldLMisses call <SID>FoldMisses(getloclist(0), <count>)
 
command! -bar -count=99999 FoldLMisses call <SID>FoldMisses(getloclist(0), <count>)
Line 72: Line 72:
   
 
==Comments==
 
==Comments==
  +
Thanks Inkarkat for the improvements. I guess all the old comments here (including the original Perl code) are redundant, and I have removed them. We need to see if [[VimTip801]] is ok, and fix its name. It is currently [[Fold quickfix list on directory or file names]] but tip is more than that now. Any ideas? [[User:JohnBeckett|JohnBeckett]] 04:02, February 10, 2012 (UTC)
{{Todo}}
 
*I recently merged in tip [[VimTip801]] and a suggestion from vim_use into this tip. However, I have now put that material in [[VimTip801]] after realising that tip 801 deals with folding the quickfix list, while tip 76 deals with folding a buffer (using hits in the quickfix list).
 
*Perhaps rename this tip? Any suggestions?
 
--[[User:JohnBeckett|JohnBeckett]] 09:11, 30 October 2008 (UTC)
 
 
==Original tip==
 
The original tip used a Perl script. I ''think'' I've translated that to Vim script (shown as the first script in the current tip). To check, following is the original.
 
*Vim Perl interface is required, i.e. the output of <tt>:version</tt> must contain '+perl'.
 
 
'''Usage'''
 
*Edit a file.
 
*<tt>:grep regexp %</tt> to get a quickfix error list.
 
*<tt>:source foldqf.vim</tt> will fold as described.
 
 
Increasing the value of <tt>$CONTEXT</tt> gives you more context around the error regions.
 
<pre>
 
"---foldqf.vim
 
cwindow
 
perl $CONTEXT = 0;
 
perl @A = map { m/\|(\d+)\|/; $1 +0 } $curbuf->Get(1..$curbuf->Count());
 
close
 
normal! zD
 
perl sub fold { VIM::DoCommand( $_[0] . ',' . ($_[1]) . "fold" ) if( $_[0] < $_[1] ); }
 
perl $last = 0; for (@A) { fold( $last+1+$CONTEXT, $_-1-$CONTEXT ); $last = $_; }; VIM::DoCommand(($A[-1]+1+$CONTEXT ) . ',$fold' );
 
</pre>
 
 
----
 

Revision as of 04:02, 10 February 2012

Tip 76 Printable Monobook Previous Next

created 2001 · complexity intermediate · version 7.0


After executing :make or :grep you can browse the list of errors/matches, and the appropriate source code locations, with commands like :cnext. :help quickfix

This tip folds away lines in the current buffer that have no errors (when using :make), or that do not match the search pattern (when using :grep). See fold quickfix list to fold lines in the quickfix list.

Fold away misses

The following script can be used to fold away lines with no errors/matches.

Usage

  • Create file ~/.vim/plugin/foldmisses.vim (Unix) or $HOME/vimfiles/plugin/foldmisses.vim (Windows) containing the script below, then restart Vim.
  • Edit a file.
  • Enter a command like :vimgrep /regexp/ % to get a quickfix list.
  • Enter :FoldMisses to fold away lines in the current buffer that are not in the quickfix list.
  • Enter :FoldLMisses to fold away lines in the current buffer that are not in the buffer's location list.
  • Optional: :1FoldMisses will give 1 extra context line (fewer lines folded). You can set a default via let g:foldmisses_context = 1
  • As normal, za toggles a fold, and zR opens all folds.
if ! exists('g:foldmisses_context')
  let g:foldmisses_context = 0
endif

" Add manual fold from line1 to line2, inclusive.
function! s:Fold(line1, line2)
  if a:line1 < a:line2
    execute a:line1.','.a:line2.'fold'
  endif
endfunction

" Return list of line numbers for current buffer found in quickfix list.
function! s:GetHitLineNumbers(list)
  let result = []
  for d in a:list
    if d.valid && d.bufnr == bufnr('')
      call add(result, d.lnum)
    endif
  endfor
  return result
endfunction

function! s:FoldMisses(list, context)
  setlocal foldmethod=manual
  normal! zE
  let extra = a:context == 99999 ? g:foldmisses_context : a:context
  let last = 0
  for lnum in s:GetHitLineNumbers(a:list)
    let start = last==0 ? 1 : last+1+extra
    call s:Fold(start, lnum-1-extra)
    let last = lnum
  endfor
  call s:Fold(last+1+extra, line('$'))
endfunction

":[N]FoldMisses [N]     Show only the lines (and surrounding [N] lines
":[N]FoldLMisses [N]    of context) in the current buffer that appear
"                       in the quickfix / location list.
"                       Missed, error-free lines are folded away.
command! -bar -count=99999 FoldMisses call <SID>FoldMisses(getqflist(), <count>)
command! -bar -count=99999 FoldLMisses call <SID>FoldMisses(getloclist(0), <count>)

Comments

Thanks Inkarkat for the improvements. I guess all the old comments here (including the original Perl code) are redundant, and I have removed them. We need to see if VimTip801 is ok, and fix its name. It is currently Fold quickfix list on directory or file names but tip is more than that now. Any ideas? JohnBeckett 04:02, February 10, 2012 (UTC)