I've just attempted to add folding for POD to TM's Perl language definition - forgetting that I've already tried it previously. POD looks like this:
=head1 A heading
=head2 A sub heading
The =cut always ends the POD. So my patterns look like ^=(?!cut) and ^=cut\s*$ respectively. That doesn't work because the folding mechanism expects a recursive syntax - each opening 'thing' requires its own closing 'thing'.
So I was thinking about how the problem could be solved and it occurred to me that it'd be interesting to have an extension to regular expression syntax that allowed an assertion to be made about the matching scope.
Assuming the assertion was called ?^ you could match the text 'foo' only in storage.type.sub.perl or entity.name.function.perl with something like this:
Generally (?^ <scope re> : <re> ) would allow <re> to match only if the current scope name matched <scope re>.
To solve my folding problem the code that scans for folds would have to be scope aware - which may not be feasible architecturally.
And I realise that extending RE semantics to grok rich text is a bit of a big undertaking - but I thought it was a cute idea :)