Skip to content

Instantly share code, notes, and snippets.

@intuited
Created June 6, 2010 04:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save intuited/427303 to your computer and use it in GitHub Desktop.
Save intuited/427303 to your computer and use it in GitHub Desktop.
" indent_fold_bug.vim
" Author: Ted Tibbetts
" License: WTFPL version 2 (http://sam.zoy.org/wtfpl/)
" Demonstrates a bug in vim's 'indent' foldmethod
" Usage: Two different options:
" $ vim -u indent_fold_bug.vim
" This will start vim, using this script as the vimrc.
" This option will ensure that default settings are used.
" :source indent_fold_bug.vim
" This will open a new tab and therein reproduce and analyze the bug.
" Using this method,
" loaded plugins or set options may interfere with bug reproduction.
let s:initial_lines = [' first section', ' first subsection', '', 'second section', ' second subsection']
funct! s:BufferIsEmpty()
return line('$') == 1 && empty(getline(1))
endfunct
funct! s:GetFoldLevels()
let flvls = []
for line in range(line('$'))
let flvls += [ foldlevel(line) ]
endfor
return flvls
endfunct
let stages = []
command! RecordStage let stages += [ { 'change': s:stage_change, 'buffer contents': getline(1, '$'), 'foldlevels': <SID>GetFoldLevels() } ]
if !s:BufferIsEmpty()
tabedit
endif
"" Set Options
" When this script is used as the vimrc via $(vim -u %),
" this will ensure that no plugins are loaded.
set runtimepath=
" Set other options in a way that will reproduce the bug.
" I'm not sure which of these are necessary.
set nocompatible
setlocal expandtab
setlocal tabstop=2
setlocal shiftwidth=2
setlocal autoindent
setlocal foldmethod=indent
setlocal foldlevel=1
let s:stage_change = 'Initial setup'
silent put =s:initial_lines
normal ggdd
RecordStage
let s:stage_change = 'Indent the empty-line-delimited last section'
call search('second section')
normal >G
RecordStage
let s:stage_change = 'Delete the empty line'
normal kdd
RecordStage
let s:stage_change = 'Fold things in a way that demonstrates the bug'
normal zc
normal ggjzc
normal gg
RecordStage
rightbelow new
normal iStages:
for stage in stages
call append('$',
\ [ ' change: ' . stage.change,
\ ' line [foldlevel: contents]:' ]
\ + map(stage['buffer contents'], '" ".string(stage.foldlevels[v:key]).": ".string(v:val)')
\ + [ '' ] )
endfor
normal GddggzR
wincmd w
4wincmd _
@intuited
Copy link
Author

intuited commented Jun 6, 2010

Note that if you source the file from within vim, it will nuke your &runtimepath. I guess I should fix this. Or you can!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment