From: jamesh@castle.ed.ac.uk (James Hammerton)
Newsgroups: comp.sys.acorn.tech
Subject: Re: How *does* one write a good text editor?
Date: 27 Aug 92 15:34:01 GMT
Organization: Edinburgh University

In article <17993@acorn.co.uk> pcolmer@acorn.co.uk (Philip Colmer) writes:
>If you were writing a text editor which allowed text effects (eg font
>changes, colour changes, etc) to be applied, how would you hold the
>data? My initial thoughts are:
>  
>  * keep the text as one contiguous block of memory

Bad idea, you'll end up having to move lots of characters about every
time you insert/delete text. Last year, I had to write a simple text
editor for a computing practical, in which I was introduced to a
simplified version of the standard way of storing text in a text editor.

What it involves is this:

To keep things simple I'll assume you are storing the text in a fixed
size array. 

What you do is you have the text split in two(this is where the cursor
currently is) with one block of text at one end of the array and the
other at the opposite end of the array:

		Character to left of cursor	Character to right of cursor

		    |				    |	
		    v				    v
----------------------------------------------------------------------
|xxxxxxxxxxxxxxxxxxxx                               xxxxxxxxxxxxxxxxx|
----------------------------------------------------------------------
		     ^				    ^		
		     |				    |
		    Pointer 1			   Pointer 2

This facilitates efficient insert/deleting of characters and moving
about the text quickly.

In order to insert a character you simply place the character into the
array element where Pointer 1 is, and move Pointer 1 on one element.

To delete a character, just move  Pointer 1 back an element.

To move forward one character, you copy the element at Pointer 2 into
Pointer 1 and move both Pointer 1 and Pointer 2 on one element.

To move back one character you would move Pointer 1 back an element,
copy the element at this position to Pointer 2 and move Pointer 2 back
an element.

Testing to see if the array is full involves testing whether Pointer 1 &
2 are adjacent, whilst testing to see if you are at the end of the array
involves testing to see if either pointer 1 is at the first element or
pointer 2 is outside the array bounds.

When using this, be sure to figure out what's going on on paper(look for
those boundary conditions!) before you start coding or you'll get some
very strange results(I did!).Also note that you can visualise the two
blocks as being a pair of stacks laid top to top where the operations
involve copying the top element of one stack top the other , or
adding/deleting the top element of one of them. When it comes to
displaying the text, things get tricky and you need to store a cursor
position and have it(and the display) updated accurately. 

>  * keep a linked list of effects, specifying the effect to be applied
>    and the start and end character offset for the effect

Well I can't help you here as the editor I wrote only involved
searching, inserting, deleting and moving about the text.

>  * keep a linked list of line start offsets.
>
See above comment.

>The line start offsets are calculated using the font manager to format
>the text into a given width.
>
>When the text is rendered, for a given line, the linked list of
>effects is checked and any that need to be applied are applied to that
>line of text. By keeping the effects in this manner, it makes it
>easier for the code to cope with effects which last more than one
>line.
>
>Does this seem efficient? It certainly seems to work for a fixed block
>of text, but what happens when the user wants to change the text, or
>the effects? Doing the formatting can take some time using my
>approach.
>
>Are there any "standard approaches" for this sort of thing?

I have given a simplified version of the 'standard' approach to storing
the text, however I don't know how effects are normally stored(save to
mention that I've seen wordprocessors which use special characters to
mark of where effects are applied).

James

-- 
  James Hammerton,2nd year Computer Science at University of Edinburgh
  Email: jamesh@uk.ac.ed.castle   Or: jh@uk.ac.ed.dcs