From bfd6df1d06c04e680285a5e789d56001db2f004c Mon Sep 17 00:00:00 2001 From: "Kenneth Benzie (Benie)" Date: Sun, 11 Sep 2016 13:02:20 +0100 Subject: [PATCH] Add support for folding python blocks in vim files --- after/ftplugin/vim.vim | 46 +++++++++++++++++++++++++----------------- 1 file changed, 28 insertions(+), 18 deletions(-) diff --git a/after/ftplugin/vim.vim b/after/ftplugin/vim.vim index 45f8943..dbd61a4 100644 --- a/after/ftplugin/vim.vim +++ b/after/ftplugin/vim.vim @@ -9,24 +9,34 @@ setlocal foldexpr=VimFold(v:lnum) function! VimFold(lnum) let l:line = getline(a:lnum) - if l:line =~ '^\s*fun\(c\(tion\)\=\)\=!\=\s\+.*#\=\w*(.*).*$' - \ || l:line =~ '^\s*if\s*.*$' - \ || l:line =~ '^\s*el\(se\)\=\s*.*$' - \ || l:line =~ '^\s*elseif\=\s*.*$' - \ || l:line =~ '^\s*wh\(ile\)\=\s*.*$' - \ || l:line =~ '^\s*for\s*.*$' - \ || l:line =~ '^\s*try\s*$' - \ || l:line =~ '^\s*cat\(ch\)\=\s*.*$' - \ || l:line =~ '^\s*fina\(lly\)\=\s*$' - \ || l:line =~ '\s*".*{{{\s*$' - return '>'.string((indent(a:lnum) / &shiftwidth) + 1) - elseif l:line =~ '^\s*endfun\(c\(tion\)\=\)\=\s*$' - \ || l:line =~ '^\s*en\(dif\)\=\s*$' - \ || l:line =~ '^\s*endw\(hile\)\=\s*$' - \ || l:line =~ '^\s*endfor\=\s*$' - \ || l:line =~ '^\s*endt\(ry\)\=\s*$' - \ || l:line =~ '\s*".*}}}\s*$' - return '<'.string((indent(a:lnum) / &shiftwidth) + 1) + if l:line =~ '^python\s\+<<\s\+\w\+\s*$' + let b:vim_python_end = substitute(l:line, '^python\s\+<<\s\+\(\w\+\)\s*$', '\1', '') + return 'a1' + endif + + if exists('b:vim_python_end') + if l:line =~ '^'.b:vim_python_end.'\s*$' + unlet b:vim_python_end + return 's1' + endif + else + if l:line =~ '^\s*fun\(c\(tion\)\=\)\=!\=\s\+.*#\=\w*(.*).*$' + \ || l:line =~ '^\s*if\s*.*$' + \ || l:line =~ '^\s*el\(se\)\=\s*.*$' + \ || l:line =~ '^\s*elseif\=\s*.*$' + \ || l:line =~ '^\s*wh\(ile\)\=\s*.*$' + \ || l:line =~ '^\s*for\s*.*$' + \ || l:line =~ '^\s*try\s*$' + \ || l:line =~ '^\s*cat\(ch\)\=\s*.*$' + \ || l:line =~ '^\s*fina\(lly\)\=\s*$' + return '>'.string((indent(a:lnum) / &shiftwidth) + 1) + elseif l:line =~ '^\s*endfun\(c\(tion\)\=\)\=\s*$' + \ || l:line =~ '^\s*en\(dif\)\=\s*$' + \ || l:line =~ '^\s*endw\(hile\)\=\s*$' + \ || l:line =~ '^\s*endfor\=\s*$' + \ || l:line =~ '^\s*endt\(ry\)\=\s*$' + return '<'.string((indent(a:lnum) / &shiftwidth) + 1) + endif endif return '='