2

我有一种数据文件(ENDF,如果你感兴趣的话),我想为它创建一个自定义的 ViM 折叠文件。折叠将取决于文件第 66-80 列的内容(由其定义)。

文件的第 66-80 列如下所示:

-columns 1--65                                             -125 0  0    0
-columns 1--65                                             -125 3  1    1
-columns 1--65                                             -125 3  1    2
-columns 1--65                                             -125 3  1    3
                                                            ...
-columns 1--65                                             -125 3  1   35
-columns 1--65                                             -125 3  099999
-columns 1--65                                             -125 3  2    1
                                                            ...
-columns 1--65                                             -125 3  2   35
-columns 1--65                                             -125 3  099999
                                                            ...
-columns 1--65                                             -125 3  099999
-columns 1--65                                             -125 0  0    0
-columns 1--65                                             -125 4  2    1
                                                            ...
-columns 1--65                                             -125 4  2  195

我希望第一个缩进级别是第 71-72 列具有相同编号的位置。在上面的示例中,这些数字是3, 4

第二个折叠级别是第 73-76 列具有相同编号的位置。在上面的示例中,这些数字是12,但可以有任何数字——最多三位。

这是我第一次尝试编写脚本。

" ENDF folding functions

setlocal foldmethod=expr
setlocal foldexpr=ENDFFolds(v:lnum)

let MF = '0'
let MT = '0'

" This function is executed 
function! ENDFFolds(lnum)
    " Get the current line
    let line = getline(v:lnum)

    let mf = strpart(line, 71, 72)
    echom mf

    " Check to see if we have moved into a new file (MF)
    if mf == MF

        " Check to see if we have moved into a new section (MT)
        let mt = strpart(line, 73, 75)
        if mt == MT
            return "="
        else
            MT = mt
            return ">2"
        endif

    else
        MF = mf
        return ">1"
    endif
endfunction

我已经把这个脚本放在~/.vim/ftplugin/endf/folding.vim. 我知道正在找到该文件,因为我看到了echo mf.

不幸的是,这不起作用。没有发现褶皱。请指教。

4

1 回答 1

0

经过艰苦的试验和错误,我想出了这个解决方案:

setlocal foldmethod=expr
setlocal foldexpr=ENDFFolds()
setlocal foldtext=ENDFFoldText()

let g:current_line = ''
let g:current_mf = 0
let g:current_mt = 0

" This function is executed for every line to determine the fold level
function! ENDFFolds()
    " Get the current line
    let line = getline(v:lnum)

    let g:previous_mf = g:current_mf
    let g:current_mf = str2nr(strpart(line, 70, 2))

    let g:previous_mt = g:current_mt
    let g:current_mt = str2nr(strpart(line, 72, 3))

    if g:previous_mf == 0
        let g:current_mt = 0
        return '>1'
    else
        if g:previous_mt == 0
            return ">2"
        else
            return "="
        endif
    endif
endfunction

function! ENDFFoldText()
    let foldsize = (v:foldend-v:foldstart)

    let line = getline(v:foldstart)
    let MAT = strpart(line, 66, 4)
    let MF = strpart(line, 70, 2)
    let MT = strpart(line, 72, 3)

    return 'MAT='.MAT.' MF='.MF.' MT='.MT.' '.' ('.foldsize.' lines)'
endfunction
于 2014-03-27T16:42:43.103 回答