Problem with foldlevel() and foldexpr synching considerations
I recently wrote a function to be used with 'foldexpr' option. I needed to do folding that was state-dependent; i.e., the beginning of a fold depended not just upon a finding certain pattern, but also upon whether we were within a fold of certain level. Although help indicates that foldexpr() "usually" correctly returns the fold level of the preceding line, I found that this was not so in my case. Often, after a call to my foldexpr function returned something like '>2', when the function was called for the subsequent line, foldlevel(v:lnum-1) returned -1.
Thus, instead of using foldlevel(), I tried using a static state variable that I updated myself within the foldexpr function. This actually worked well, when the file was first opened. The problem is that when the file is subsequently modified, the foldexpr function is called multiple times, working backwards through the file in the reverse direction from the point of the change. I'm guessing it is looking for a sync point because my foldexpr function would most likely have returned '=' at that point. But this won't work, since once I've begun foldlevel 1, I then need to move forward to find subsequent, nested folds.
Finally, I didn't want to use syntax foldmethod, because my syntax file has been stable for some time, and switching to syntax foldmethod would require a rework...
I don't have time to look into the folding source right now, so any insight anyone can provide into the inner workings of foldlevel(), and how foldexpr is used to synch up after a file modification, etc..., would be greatly appreciated. As I mentioned, the static variable state machine method appears to work, but I'm afraid it may break under some text modification scenarios, due to the way foldexpr is called during the redraw.
Mit freundlichem Gruß / Kind regards
Senior Software Engineer
Siemens VDO Automotive AG
SV C BC
100 Electronics Blvd #240001
Huntsville, AL 35824-6401
The United States of America
E-Mail: [hidden email] Internet: http://www.siemensvdo.com <http://www.siemensvdo.com/>