Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

'foldcolumn' auto option (like 'signcolumn') #1866

Closed
benknoble opened this issue Jul 23, 2017 · 2 comments
Closed

'foldcolumn' auto option (like 'signcolumn') #1866

benknoble opened this issue Jul 23, 2017 · 2 comments

Comments

@benknoble
Copy link
Contributor

Current behavior

With 'foldcolumn' set to a positive integer, display a foldcolumn no matter what.

Desired behavior

With 'foldcolumn' set to auto, display a foldcolumn if folds are presents; otherwise, display no foldcolumn.

Thoughts

This would of course require Vim to be able to detect if folds are present in a file (based on fold method, or just searching around for them?). There would also need to be a discussion of how wide the auto-foldcolumn could be.

Current workaround

I found this somewhere, StackOverflow I think, that achieves the result but isn't perfect (I get a Terminal bell on almost all BufLoads because of the way folds are detected).

" Detects if folds present, sets foldcolumn to 2 if true or 0 if false
" From http://stackoverflow.com/questions/8757168/gvim-automatic-show-foldcolumn-when-there-are-folds-in-a-file
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=2
    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=2
        else
            set foldcolumn=0
        endif
    end
    call winrestview(l:winview) "restore window/cursor position
endfunction

" Automatically add foldcolumn if folds present
 au BufWinEnter ?* call HasFolds()
@benknoble
Copy link
Contributor Author

benknoble commented Aug 27, 2017

I took this and created the plugin AutoOrigami, so I'll close this issue, but I would still have rather seen Vim support this natively.

@teto
Copy link

teto commented Apr 2, 2020

available in neovim master neovim/neovim#11716

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants