Vim Tips Wiki
(Move categories to tip template)
(Change <tt> to <code>, perhaps also minor tweak.)
 
(14 intermediate revisions by 5 users not shown)
Line 1: Line 1:
{{review}}
 
 
{{TipImported
 
{{TipImported
 
|id=76
 
|id=76
 
|previous=75
 
|previous=75
 
|next=78
 
|next=78
|created=June 8, 2001
+
|created=2001
 
|complexity=intermediate
 
|complexity=intermediate
|author=Mark A. Hillebrand
+
|author=
|version=6.0
+
|version=7.0
 
|rating=1/3
 
|rating=1/3
 
|category1=Compiler
 
|category1=Compiler
 
|category2=Folding
 
|category2=Folding
 
}}
 
}}
The Quickfix mode aims to "speed up the edit-compile-edit cycle" according to {{help|quickfix}}. After executing <tt>:make</tt> or <tt>:grep</tt> it is possible to skim through the list of errors/matches and the appropriate source code locations with, for instance, the <tt>:cnext</tt> command.
+
After executing <code>:make</code> or <code>:grep</code> you can browse the list of errors/matches, and the appropriate source code locations, with commands like <code>:cnext</code>. {{help|quickfix}}
   
  +
This tip [[Folding|folds away lines]] in the current buffer that have no errors (when using <code>:make</code>), or that do not match the search pattern (when using <code>:grep</code>). See [[VimTip801|here]] to fold lines in the quickfix list.
Another way to get a quick overview is to use VIMs folding mode, to fold away all the error-free/match-free regions.
 
The following script can be used for this purpose. It is at the moment not elaborate enough to put it up as a ''script''; but it might give someone inspiration to do so.
 
   
  +
==Fold away misses==
'''Big restrictions / bugs are as follows:'''
 
  +
The following script can be used to fold away lines with no errors/matches.
# Vim Perl interface is required, i.e. the output of <tt>:version</tt> must contain '+perl' (People with Vim scripting knowledge might fix this)
 
# Works only for one file, i.e. the current buffer.
 
# It's a quick hack.
 
   
'''Sample usage: '''
+
'''Usage'''
  +
*Create file <code>~/.vim/plugin/foldmisses.vim</code> (Unix) or <code>$HOME/vimfiles/plugin/foldmisses.vim</code> (Windows) containing the script below, then restart Vim.
 
*Edit a file.
 
*Enter a command like <code>:vimgrep /regexp/ %</code> to get a quickfix list.
  +
*Enter <code>:FoldMisses</code> to fold away lines in the current buffer that are not in the quickfix list.
  +
*Enter <code>:FoldLMisses</code> to fold away lines in the current buffer that are not in the buffer's location list.
  +
*Optional: <code>:1FoldMisses</code> will give 1 extra context line (fewer lines folded). You can set a default via <code>let g:foldmisses_context = 1</code>
  +
*As normal, <code>za</code> toggles a fold, and <code>zR</code> opens all folds.
   
 
<pre>
(a) edit a file,
 
  +
if ! exists('g:foldmisses_context')
  +
let g:foldmisses_context = 0
  +
endif
   
  +
" Add manual fold from line1 to line2, inclusive.
(b) do <tt>:grep regexp %</tt> to get a quickfix error list and
 
  +
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.
(c) <tt>:source foldqf.vim</tt> will fold as described
 
  +
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)
Increasing the value of <tt>$CONTEXT</tt> gives you more context around the error regions.
 
  +
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
<pre>
 
  +
":[N]FoldLMisses [N] of context) in the current buffer that appear
"---foldqf.vim
 
  +
" in the quickfix / location list.
cwindow
 
  +
" Missed, error-free lines are folded away.
perl $CONTEXT = 0;
 
  +
command! -bar -count=99999 FoldMisses call s:FoldMisses(getqflist(), <count>)
perl @A = map { m/\|(\d+)\|/; $1 +0 } $curbuf-&gt;Get(1..$curbuf-&gt;Count());
 
  +
command! -bar -count=99999 FoldLMisses call s:FoldMisses(getloclist(0), <count>)
close
 
normal! zD
 
perl sub fold { VIM::DoCommand( $_[0] . ',' . ($_[1]) . "fold" ) if( $_[0] &lt; $_[1] ); }
 
perl $last = 0; for (@A) { fold( $last+1+$CONTEXT, $_-1-$CONTEXT ); $last = $_; }; VIM::DoCommand(($A[-1]+1+$CONTEXT ) . ',$fold' );
 
 
</pre>
 
</pre>
   
 
==Comments==
 
==Comments==
TODO: I would have like to set this tip it as dodgy (*) because I remember another tip (don't ask me its number) addressing the exact same issue of folding the cwindow. Must we merge those two tips ?
 
 
(*) <nowiki>{{dodgy}}</nowiki> would have been misleading as this tip does not deserve deletion.
 
 
--[[User:Luc Hermitte|Luc Hermitte]] 18:34, 26 July 2007 (UTC)
 
----
 

Latest revision as of 05:12, 13 July 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 here 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 s:FoldMisses(getqflist(), <count>)
command! -bar -count=99999 FoldLMisses call s:FoldMisses(getloclist(0), <count>)

Comments[]