Skip to content

Instantly share code, notes, and snippets.

@Raimondi
Created October 16, 2010 09:15
Show Gist options
  • Save Raimondi/629606 to your computer and use it in GitHub Desktop.
Save Raimondi/629606 to your computer and use it in GitHub Desktop.
Unified patch file folding.
" From jamessan#[email protected]
" Folding for unified diffs
function! DiffFoldLevel(lineno)
let line = getline(a:lineno)
if line =~ '^Index:'
return '>1'
elseif line =~ '^===' || line =~ '^RCS file: ' || line =~ '^retrieving revision '
let lvl = foldlevel(a:lineno - 1)
return lvl >= 0 ? lvl : '='
elseif line =~ '^diff'
return getline(a:lineno - 1) =~ '^retrieving revision ' ? '=' : '>1'
elseif line =~ '^--- ' && getline(a:lineno - 1) !~ '^diff\|^==='
return '>1'
elseif line =~ '^@'
return '>2'
elseif line =~ '^[- +\\]'
let lvl = foldlevel(a:lineno - 1)
return lvl >= 0 ? lvl : '='
else
return '0'
endif
endf
setlocal foldmethod=expr
setlocal foldexpr=DiffFoldLevel(v:lnum)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment