Neat your folds.

1 Point

ReneFroger ReneFroger

9 years ago

I remapped the fold functions to the letter s-another letter instead z, in order to have everything on my home row. I found the folds layout not so neat, so I wrote a function for it.

function! NeatFoldText() 
    let line = ' ' . substitute(getline(v:foldstart), '^\s*"\?\s*\|\s*"\?\s*{{' . '{\d*\s*', '', 'g') . ' '
    let lines_count = v:foldend - v:foldstart + 1

    let foldSize = 1 + v:foldend - v:foldstart
    let lineCount = line("$")

    if has("float")
        try
            let foldPercentage = printf(" %.1f", (foldSize*1.0)/lineCount*100) . "% "
        catch /^Vim\%((\a\+)\)\=:E806/	" E806: Using Float as String
            let foldPercentage = printf("| [of %d lines] |", lineCount)
        endtry
    endif

    let lines_count_text = '| ' . printf("%10s", lines_count . ' lines of Vi code') . ' - ' . foldPercentage . ' |'

    let foldchar = matchstr(&fillchars, 'fold:\zs.')
    let foldtextstart = strpart('+' . repeat(foldchar, v:foldlevel*2) . line, 0, (winwidth(0)*2)/3)
    let foldtextend = lines_count_text . repeat(foldchar, 28)
    let foldtextlength = strlen(substitute(foldtextstart . foldtextend, '.', 'x', 'g')) + &foldcolumn

    return foldtextstart . repeat(foldchar, winwidth(0)-foldtextlength) . foldtextend
endfunction


" Init function
set foldtext=NeatFoldText()