Fork me on GitHub
#vim
<
2020-10-17
>
berkeleytrue04:10:53

Is there a way to get fold data? I'm trying to figure out how folds effect the line numbers of the buffer. I've found some work arounds that do a lot to get this information (saving state, unfolding everything, check if foldables sections line by line, then returning to prev state), but it seems like there should just be a function that I can call similar to winsaveview that will say something like "lines 3-5 are folded". Does this exist? (searching help and google hasn't turned up anything.)

nbardiuk12:10:25

I guess the simplest way is to iterate over lines and find which are folded

nbardiuk12:10:43

function! Folds() abort
    let folds = []
    let i = 1
    while i <= line('$')
      let foldends = foldclosedend(i)
      if foldends != -1
        call insert(folds, [i, foldends])
        let i = foldends
      endif
      let i = i+1
    endwhile
    return folds
endfunction

nbardiuk12:10:11

:echo json_encode(Folds())

berkeleytrue15:10:46

@U076FM90B Thanks, I think this is just what I was looking for!