How can I autofold POD in Perl using vim?

I am trying to edit files using vim and automatically stack POD (only POD, not Perl). I can not make it work. I can force myself zFto work, because I can manually select the lines and dial zFand this adds up correctly.

Can anyone shed some light on this?

+5
source share
5 answers

You forgot

:set foldmethod=syntax
:setf perl
:syntax on
:set foldenable
:syn region POD start=/^=head[123]/ end=/^=cut/ fold
+10
source

Paste the following at the end of the ~ / .vimrc file:

fu! MyFoldSettings()
  set foldmethod=expr
  set foldexpression=MyFoldLevel(v:lnum)
  set foldenable
  set foldminlines=1
endf      

fu! MyFoldLevel(lev)
    let mytext = getline(a:lev)
    let result="="
    if mytext =~ '^=item'
        let result=">3"
    elsei mytext =~ '^=back'
        let result="<2"
    elsei mytext =~ '^=over'
        let result=">2"
    elsei mytext =~ '^=cut'
        let result="<1"
    elsei mytext =~ '^=\w\+'
        let result='>1'
    en    
    return result
endf

augroup MyFoldSettings_AutoCommands
   au!
   au BufReadPost {*.pl,*.pod,*.pm} MyFoldSettings()
augroup END

- , , . , , , modeline:

# vim:fen:fdo=hor:fcl=all:fdm=expr:fde=MyFoldLevel(v:lnum):fml=1

, "foldopen = hor" "foldclose = all", / . , , , au (autocommand) . .

+2

You can put this in your .vimrc:

au FileType perl
    \ setlocal foldexpr=getline(v:lnum)=~'^=cut'?'<1':getline(v:lnum)=~'^='?'1':'=' |
    \ setlocal foldmethod=expr
0
source

Just define the perl_fold_ * variables, the perl syntax highlighting system takes care of the rest.

In .vimrc just add

let perl_fold=1
let perl_fold_blocks=1 
0
source

This is what I use

set syntax                                                                                                                                                               

augroup perl_folding                                                                                                                                                     
    au!                                                                                                                                                                  
    autocmd FileType perl setlocal foldmethod=syntax                                                                                                                     
    autocmd FileType perl setlocal foldlevel=1                                                                                                                           
    autocmd FileType perl let perl_fold = 1                                                                                                                              
    autocmd FileType perl let perl_fold_blocks = 1                                                                                                                       
    autocmd FileType perl setlocal foldenable                                                                                                                            
    au BufRead,BufNewFile *.pm set filetype=perl                                                                                                                         
augroup END
0
source

Source: https://habr.com/ru/post/1703494/


All Articles