Folding

Whats folding all about...?

Description of Folding

This editor is a 'folding editor'. This is a highly useful feature. Once you have got used to the folding paradigm, you will not want to use a flat editor again!

Folding is an idea borrowed from the language occam by INMOS, although there is prior work in this area. A fold is a group of lines that may be considered a unit and described by a single line.

Consider the flat piece of code :-

while not eof(f) do
  begin
    read(f, c);
    write(c)
  end

This could be folded to give :-

while not eof(f) do
  ... echo out another character from the file

Where the fold contains :-

begin
  read(f, c);
  write(c)
end

The ... line hides away the details of whats going on. Obviously it makes more sense to fold larger and more complicated peices of code away. Its a bit like inline subroutines. A particularly nice way to fold Pascal programs looks like this :-

program fred(input, output);

  {highly trivial example of folding}

var
  i: integer;

  ... initialise
  ... main body

begin
  initialise;
  main_body
end.

As you can see, this style of folding allows you to rapidly find any given procedure and enables you to see the structure of the program as a whole. Also, you no longer need to page up miles to find the variables!

Folds can be open or closed. The ones shown already were closed, but if they are opened they look like this :-

while not eof(f)
  {{{ echo out another character from the file
  begin
    read(f, c);
    write(c)
  end
  }}}

This allows the user to see whats in them without actually entering them.

There is a second kind of fold, called a virtual fold which allows links to other files to be made. For example you might see :-

program fred(input, output)

{$I 'header.inc'}
::: header.inc

begin
  etc.

If you use fold_in on the virtual fold line, then the file is read and selected. If its a directory, then a listing of the directory contents is generated.

If you later use fold_out from within the top fold of header.inc file, then the file is optionally written if modified, discarded, then the original buffer is selected. (Non files, such as directory listings, are discarded even if modified). In this way many files may edited as if they were only one file.

Virtual folds may also be opened, in which case they look like this :-

>>> header.inc

You cannot see the contents of an open virtual fold, since they are in another file, and are not loaded until the virtual fold is entered.

The significance of opening virtual folds becomes apparent in searching, where it is possible to search just open virtual folds, and to skip closed ones, during a global search.

Virtual folds can include a comment section, which follows the filename :-

::: header.inc - main header is in here

If the filename part has spaces, then these must be preceed by ' characters :-

::: c:\Program' Files\Find.Ini - an initialisation file

Now virtual filenames may identify directories. When entered, AE constructs a directory listing, with each entry as a virtual fold. This supports directory navigation. When using the search functions of the editor, AE will not enter virtual folds referring to the . and .. directories.

Summary of benefits of folding

Folding accelerates navigation around a file. The time to find a given places is order(log(average_fold_size)), as opposed to order(file_size).

Folding allows you to hide details of sub-module implementation, thus aiding abstraction.

Folding allows you to hierachically structure programs, even those written in 'flat' languages such as Assembler and Prolog. In particular, function locality can be emphasized by putting functions used only by a given routine, as a sub-fold of that routine.

The rule of thumb that says that 'each function should fit on a page' can be rewritten as 'each function should fit on a page when folded'. This allows you to write huge functions, without bringing the complexity of the function (as viewed at each level) any higher than before. Each fold is effectively an inline function.

Folding can be used as a focusing tool. For example, if you are in a fold holding the implementation of a stack object (to modify or enhance it say), you know you need to focus you attention on this fold (and subfolds perhaps), but NOT elsewhere.