Skip to content

Instantly share code, notes, and snippets.

@ignamv
Last active February 6, 2019 18:12
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 ignamv/bc38c591b505085d14fe4b2fbee52629 to your computer and use it in GitHub Desktop.
Save ignamv/bc38c591b505085d14fe4b2fbee52629 to your computer and use it in GitHub Desktop.
Spectre line folding for Vim
setlocal keywordprg=manspectre
setlocal foldmethod=expr
setlocal foldexpr=GetSpectreFold(v:lnum)
function! NextNonCommentLine(lnum)
let numlines = line('$')
let current = a:lnum + 1
while current <= numlines
if getline(current) !~? '\v^\s*//'
return current
endif
let current += 1
endwhile
return -1
endfunction
function! LineContinues(lnum)
if getline(a:lnum) =~? '\v\\$'
return 1
endif
let nextline = NextNonCommentLine(a:lnum)
if nextline != -1
return getline(nextline) =~? '\v^\+'
endif
return 0
endfunction
function! GetSpectreFold(lnum)
if getline(a:lnum) =~? '\v^\s*$'
return '='
endif
if getline(a:lnum) =~? '\v^\s*(inline )?subckt'
return 'a1'
endif
if getline(a:lnum) =~? '\v^\s*ends'
return 's1'
endif
if getline(a:lnum) =~? '\v\s*\}.*\{\s*$'
return '='
endif
if getline(a:lnum) =~? '\v\{\s*$'
return 'a1'
endif
if getline(a:lnum) =~? '\v^\s*\}'
return 's1'
endif
if LineContinues(a:lnum) && ! LineContinues(a:lnum-1)
return 'a1'
endif
if LineContinues(a:lnum-1) && ! LineContinues(a:lnum)
return 's1'
endif
return '='
endfunction
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment