VIM更改折叠标记以使用vimrc显示行数和单词数

时间:2017-05-21 14:22:36

标签: vim markdown folding

我在Windows上使用GVIM编写sf小说,然后折叠章节,以便隐藏它们。折叠后的章节'显示这样的事情:

##1 – Spaceman Spoof Encounters the Watterson........ 115 lines [2.4%] +--+--

这很有用,但只是一点点。有些行可能只是几个单词而其他行可能超过100个单词。我更喜欢的是行数和字数如下:

##1 – Spaceman Spoof Encounters the Watterson........ l:115 w:1032 [4.4%] +--+--

百分比是单词而不是单词。任何帮助肯定会受到赞赏。

这是我_vimrc的折叠部分:

function! MarkdownFolds()
    let thisline = getline(v:lnum)
    if match(thisline, '^##') >= 0
        return ">2"
    elseif match(thisline, '^#') >= 0
        return ">1"
    else
        return "="
    endif
endfunction

setlocal foldmethod=expr
setlocal foldexpr=MarkdownFolds()

function! MarkdownFoldText()
    "get first non-blank line
    let fs = v:foldstart
    while getline(fs) =~ '^\s*$' | let fs = nextnonblank(fs + 1)
    endwhile
    if fs > v:foldend
        let line = getline(v:foldstart)
    else
        let line = substitute(getline(fs), '\t', repeat(' ', &tabstop), 'g')
    endif

    let w = winwidth(0) - &foldcolumn - (&number ? 8 : 0)
    let foldSize = 1 + v:foldend - v:foldstart
    let foldWords = v:foldend,v:foldstart!wc -w
    let foldSizeStr = " " . foldSize . " lines "
    let foldWordsStr = " " . foldWords . " w "
    let foldLevelStr = repeat("+--", v:foldlevel)
    let lineCount = line("$")
    let foldPercentage = printf("[%.1f", (foldSize*1.0)/lineCount*100) . "%] "
    " let expansionString = "."
    let expansionString = repeat(".", w - strwidth(foldSizeStr.line.foldLevelStr.foldPercentage))
    return line . expansionString . foldSizeStr . foldPercentage . foldLevelStr
    " return line . expansionString . foldSizeStr . foldPercentage . foldWordsStr . foldLevelStr
    " return line . "......" . foldSizeStr . foldPercentage . foldLevelStr
endfunction

setlocal foldtext=MarkdownFoldText()

2 个答案:

答案 0 :(得分:0)

这是一个解决方案:

function! MarkdownFoldText()
    "get first non-blank line
    let fs = v:foldstart
    while getline(fs) =~ '^\s*$' | let fs = nextnonblank(fs + 1)
    endwhile
    if fs > v:foldend
        let line = getline(v:foldstart)
    else
        let line = substitute(getline(fs), '\t', repeat(' ', &tabstop), 'g')
    endif

    let w = winwidth(0) - &foldcolumn - (&number ? 8 : 0)
    let foldSize = 1 + v:foldend - v:foldstart

    let i = 0
    let foldWords=0
    let wordCount=0
    while (i<line('$'))
      let lineWords = len(split(getline(i)))
      if (i<=v:foldend && i>= v:foldstart)
        let foldWords = foldWords + lineWords
      endif
      let wordCount += lineWords
      let i += 1
    endwhile

    let foldWordsStr = " " . foldWords . " w,"
    let foldSizeStr = foldWordsStr . foldSize . " lines "
    let foldLevelStr = repeat("+--", v:foldlevel)
    let foldPercentage = printf("[%.1f", (foldWords*1.0)/wordCount*100) . "%] "
    let expansionString = repeat(".", w - strwidth(foldSizeStr.line.foldLevelStr.foldPercentage))
    return line . expansionString . foldSizeStr . foldPercentage . foldLevelStr
endfunction

这个想法是计算全局字数,以及同时在所选区域内的字数。

然后我们根据要求用字数替换百分比。

答案 1 :(得分:0)

这是另一个解决方案,使用wordcount()函数,这次:

function! MarkdownFoldText()
    "get first non-blank line
    let fs = v:foldstart
    while getline(fs) =~ '^\s*$' | let fs = nextnonblank(fs + 1)
    endwhile
    if fs > v:foldend
        let line = getline(v:foldstart)
    else
        let line = substitute(getline(fs), '\t', repeat(' ', &tabstop), 'g')
    endif

    let w = winwidth(0) - &foldcolumn - (&number ? 8 : 0)
    let foldSize = 1 + v:foldend - v:foldstart

    let i = v:foldstart
    let foldWords=0
    while (i<v:foldend)
      let lineWords = len(split(getline(i)))
      let foldWords = foldWords + lineWords
      let i += 1
    endwhile
    let wordCount = wordcount()["words"]

    " let foldWords = v:foldend,v:foldstart!wc -w
    let foldWordsStr = " " . foldWords . " w,"
    let foldSizeStr = foldWordsStr . foldSize . " lines "
    let foldLevelStr = repeat("+--", v:foldlevel)
    let foldPercentage = printf("[%.1f", (foldWords*1.0)/wordCount*100) . "%] "
    " let expansionString = "."
    let expansionString = repeat(".", w - strwidth(foldSizeStr.line.foldLevelStr.foldPercentage))
    return line . expansionString . foldSizeStr . foldPercentage . foldLevelStr
    " return line . expansionString . foldSizeStr . foldPercentage . foldWordsStr . foldLevelStr
    " return line . "......" . foldSizeStr . foldPercentage . foldLevelStr
endfunction

应该更快。并不是百分比是这个文件夹与整个缓冲区相比所代表的程度。使用wordcount()函数,我们从最终到目前为止构建的程度是多么微不足道。