Gvim automatic show foldcolumn when there are folds in the file

I know you can use

set foldcolumn=1

to enable fold column

but is there a way to automatically turn it on only when folds exist in the file?

+5
source share
3 answers

Most likely, you can create a function to check if the file has any creases, for example:

function HasFoldedLine() 
    let lnum=1 
    while lnum <= line("$") 
        if (foldclosed(lnum) > -1) 
            return 1 
        endif 
        let lnum+=1 
    endwhile 
    return 0 
 endfu 

Now you can use it with some autocommand, for example:

au CursorHold * if HasFoldedLine() == 1 | set fdc=1 | else |set fdc=0 | endif 

NTN

+1
source

, @Zsolt Botykai, . , . . , .

function HasFolds()
    "Attempt to move between folds, checking line numbers to see if it worked.
    "If it did, there are folds.

    function! HasFoldsInner()
        let origline=line('.')  
        :norm zk
        if origline==line('.')
            :norm zj
            if origline==line('.')
                return 0
            else
                return 1
            endif
        else
            return 1
        endif
        return 0
    endfunction

    let l:winview=winsaveview() "save window and cursor position
    let foldsexist=HasFoldsInner()
    if foldsexist
        set foldcolumn=1
    else
        "Move to the end of the current fold and check again in case the
        "cursor was on the sole fold in the file when we checked
        if line('.')!=1
            :norm [z
            :norm k
        else
            :norm ]z
            :norm j
        endif
        let foldsexist=HasFoldsInner()
        if foldsexist
            set foldcolumn=1
        else
            set foldcolumn=0
        endif
    end
    call winrestview(l:winview) "restore window/cursor position
endfunction

au CursorHold,BufWinEnter ?* call HasFolds()
+4

(Self-planning plugin)

I created a plugin for this: Auto Origami modeled on @SnoringFrog answer .

Leave the following example in your vimrc after installing it to see how the magic happens (and read :help auto-origamito find out how to configure it):

augroup autofoldcolumn
  au!

  " Or whatever autocmd-events you want
  au CursorHold,BufWinEnter * let &foldcolumn = auto_origami#Foldcolumn()
augroup END
0
source

All Articles