Configurable Fold Handler

Chris Kent (ckent@espeed.co.uk)

Introduction

This plugin adds a fold handler that allows the user to specify the strings that define the start and end of a fold.

Enabling Configurable Folding

To enable configurable folding the folding mode must be set to "custom". This is done from the folding mode drop downs in the buffer options dialog or by setting the default folding mode in global options->editing or global options->mode-specific options panes.

Fold Strings

Different fold strings can be specified for each edit mode and for individual buffers. Fold settings work in the same way as for other buffer settings. Global and mode-specific options are retained between jEdit sessions but buffer-specific fold strings are only retained as long as the buffer remains open.

Defaults fold strings for each mode are set in the Configurable Folding section of the Plugin Options dialog. Fold strings are set for each buffer individually using Plugins->Configurable Fold Handler->Buffer Fold Strings.

Fold strings cannot be blank. If invalid strings are entered in the Plugin Options dialog then an error message is displayed and folding is disabled for the selected mode. The Buffer Fold Strings dialog will not allow invalid strings to be entered.

Regular Expressions

If the "Use Regular Expressions" checkbox is selected then the fold handler will treat the strings as regular expressions and use them to match the start and end of folds. Under Java 1.3 the gnu.regexp package is used for matching. Under Java 1.4 the java.util.regex package is used which supports a wider range of regular expression syntax.

Multiple Fold Strings

In order to match on multiple strings the "Use Regular Expressions" option should be enabled and the list of strings separated with the | (pipe) character. For example public|private|if|switch . If there are any regular expression special characters in the start or end strings then they'll need to be preceeded with a backslash ('\') character otherwise the folding won't work as anticipated. These characters include the following:
  * . \ ? + | [ ] ( ) ^

Some Example Regular Expressions

For folding XML files with no indent the following start and end strings can be used:
  <[^!/\?].*?[^/-]>
  </.*?>
These only work properly if tags aren't broken across multiple lines.

For Java, C and C++ code the following are useful to fold on both brackets and range comments:

  {|/\*
  }|\*/
(Java 1.3)
  \{|/\*
  \}|\*/
(Java 1.4)

If the start or end fold string is a substring of the other then normal folding will not work correctly. This can sometimes be worked around using regualar expressions. For example to match the strings Function, End Function the following regular expressions would work (under Java 1.4):

  (?<!End )Function
  End Function

The following versions are slightly more complete as they specify that the strings must be preceeded and followed by a word break (\b).

  (?<!End )\bFunction\b
  \bEnd Function\b

If anyone has any other useful regular expressions or any default fold strings for any jEdit mode then please let me know and I'll include them.