[comp.editors] Folding

trb@stag.UUCP ( Todd Burkey ) (01/27/89)

In article <1036@bacchus.dec.com> gringort@decwrl.dec.com (Joel Gringorten) writes:
>At the risk of seeming incredibly naive, what is "Folding?"
>

Here is a quick (and messy) description:

Folding in a text editor is a mechanism that allows you to cause
sections of code or text be replaced by a single line of text. This
is useful for 'hiding' away clean and functional sections of code
while you work on and only see the parts that you are currently
debugging. It also proves useful for hiding away big comment blocks.

In FOLDED, I implemented three types of folding. In each case, a
segment of your file is reduced such that all you see is the first
line of the segment in reverse image (or bold, depending on your
standout terminal mode). First, is the simple 'fold a marked block'.
This allows you to mark any arbitrary region of text and fold it away.
For example, lets say we had started with the following text (the
numbers are just for reference in this posting):

 1: main()
 2: /* GID for main - Just a test case
 3:    Author: Todd Burkey
 4:    Date:   1/27/89
 5:    DESC:   Methodology for a test case
 6: */   
 7: {
 8:   if(this==that) {
 9:     do_something_that_works();
10:     }
11:   }

By defining a region between lines 2 and 6 and then folding, what you
would see on the screen would be:

 1: main()
 2: /* GID for main - Just a test case        <-pretend this line is bold
 7: {
 8:   if(this==that) {
 9:     do_something_that_works();
10:     }
11:   }

Another method of folding is that based on 'indent level'. The logic
behind indent level folding is simply to start with the line the user
is currently on, move down in the text until an indent occurs, and
then continue down until an undent occurs that is less than or equal
to the original lines 'indented' position. For example, if you
had been on line 8 in the above 'folded' text when the 'f' key was
pressed, you would see the following:

 1: main()
 2: /* GID for main - Just a test case        <-pretend this line is bold
 7: {
 8:   if(this==that) {                        <-pretend this line is bold
11:   }

Unfortunately, most source code contains lots of things that tend to
break up the clean indentation levels, so I added a third folding
mechanism. This one is a little smarter in that it does a 'logical
fold'. A logical fold looks for logical block begin/end marks in the
code (the braces in C) and skips around comments, strings, etc. For
example, if you had pressed the 'F' key on line 1 in any of the above
steps, you would end up with:

  1: main()                           <-pretend this line is bold

In all cases, unfolding the text is performed by simply moving to the
folded line and pressing the 'f' or 'F' key.

Folding isn't all that novel a concept. It has been used for years now
in thought processor programs on the Mac, IBM PC, and even back in the
CP/M days. Thought processors usually use the 'fold by indent level' 
technique. Take the following simple example:

FOLDED Information
  * specifications    <-pretend this line is bold
  * plans             <-pretend this line is bold
  * problems          <-pretend this line is bold

This is all I see when I use FOLDED to call up my folded.info file.
Now, if I move to line 3 and press the f key, I will see:

FOLDED Information
  * specifications    <-pretend this line is bold
  * plans
    - Documentation   <-pretend this line is bold
    - Enhancements    <-pretend this line is bold
    - Bullet-proofing <-pretend this line is bold
  * problems          <-pretend this line is bold

Yes, folding is definitely nestable.

I hope this answers your questions...I will have a better description
when I get time to sit down and document this mess.

  -Todd Burkey   "A member of STdNET-The ST developers' Network"
   trb@stag.UUCP