Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

is line folded? - How to check for folds in VIM

I'm writing some folding functions and I am at a point where I need to check if the current line is actually a fold.

The reason for this is because it is a custom fold method that depends on searching/matching certain lines.

For example, if the current line is folded and looks like:

-FOO------------------------

If you do something like:

getline('.')

You would basically get FOO so there is no way (that I know of) to know if I am at a fold or not.

Is there a helper function for this?

I would think it would have to be something like:

is_folded('.')

I could probably mess with the foldtext to assign a special title for the fold but I want to avoid this.

like image 730
alfredodeza Avatar asked Feb 01 '11 14:02

alfredodeza


People also ask

How do you fold a string in visual Vim?

Vim folding commands zf#j creates a fold from the cursor down # lines. zf/string creates a fold from the cursor to string. zj moves the cursor to the next fold.

How does foldmarker work in Vim?

However, it works by adding special markers to your buffer text so that the same folds can be loaded by anyone opening the file in Vim. The markers are defined by the 'foldmarker' option, which defaults to three consecutive open curly braces to start a fold and three consecutive closed curly braces to end a fold.

What are Vim’s folding features?

Using Vim’s folding features, you can tuck away portions of a file’s text so that they’re out of sight until you want to work with them again. Here’s how. Vim’s folding commands begin with z — which, as the Vim docs point out, sort of looks like a folded piece of paper. OK, maybe not much, but at least the commands are consistent.

Why doesn’t Vim fold the text when I search?

Unfortunately, Vim is a little less than tidy in this regard — if a search causes a fold to be expanded, Vim doesn’t re-fold the text after moving the cursor outside the fold. However, a quick zm will restore the folds.


1 Answers

From :help eval.txt

foldclosed({lnum})

The result is a Number. If the line {lnum} is in a closed fold, the result is the number of the first line in that fold. If the line {lnum} is not in a closed fold, -1 is returned.

You can check for a given line if it returns -1 or a line number, you can probably implement your isfolded() function this way.

If you are looking for Vim script function or feature , it is a good idea to start by searching in eval.txt which contains lots of relevant information.

like image 115
Xavier T. Avatar answered Sep 22 '22 14:09

Xavier T.