clickable.vim icon indicating copy to clipboard operation
clickable.vim copied to clipboard

Better fold detection

Open gelguy opened this issue 11 years ago • 2 comments

Currently the fold detection is done by

        function! local_config.folding.validate(...) dict "{{{
            return foldclosed('.') != -1
        endfunction "}}}

This means that only closed folds can be opened.

Below is a .vimrc snippet I use to detect folds

function! CheckFold()
  " returns 0 - cursor not in fold
  "         1 - cursor at start of open fold
  "         2 - cursor in middle of open fold
  "         3 - cursor at start of closed fold
  "         4 - cursor in middle of closed fold
  if foldclosed(line('.')) != -1
    if foldclosed(line('.')) == line('.')
      " Current line is start of fold and fold is closed
      return 3
    else
      " Current line is middle of fold and fold is closed
      return 4
    endif
  elseif foldlevel(line('.')) != 0
    " Current line is in a fold but the fold is open
    let old_view = winsaveview()
    let current_line = line('.')
    let current_fold_level = foldlevel(current_line)
    " Go to the start of fold
    " case  1: current line is start of fold
    "      a): the fold does not have a parent fold
    "       -> cursor remains at current line
    "      b): the fold has a parent fold
    "       -> cursor moves up (foldlevel will decrease)
    "
    "       2: current line is not start of fold
    "       -> cursor moves up (foldlevel does not decrease)
    normal! [z
    if line('.') == current_line || foldlevel(line('.')) < current_fold_level
      " case 1a: cursor remains at current line
      " case 1b: foldlevel decreased
      " Cursor is at start of open fold
      call winrestview(old_view)
      return 1
    else
      " Cursor is in middle of open fold
      call winrestview(old_view)
      return 2
    endif
  endif
  " Cursor not in fold
  return 0
endfunction

This allows checking for both open and closed folds and works on all fold methods, so we can use clickable to close folds as well. I have not yet tested this with the clickable plugin.

gelguy avatar Oct 21 '14 16:10 gelguy

Thanks for your nice work~

I have not tested.

But I wonder this will cause a problem that, we can not use <2-leftmouse> to select things ??

gu-fan avatar Oct 22 '14 10:10 gu-fan

We can still use normal! \<2-LeftMouse to trigger the default click function.

Example:

function! ClickFolds()
  let fold_status = CheckFold()
  if fold_status == 3 || fold_status == 4
    normal! zv
  elseif fold_status == 1
    normal! zc
  elseif fold_status == 0
    exe "normal! \<2-LeftMouse>"
  endif
endfunction

gelguy avatar Oct 22 '14 13:10 gelguy