[racket] text formatting questions
At Sun, 22 Aug 2010 21:58:36 -0500, Mathew Kurian wrote:
> 1. In a text editor like Microsoft Word, in what form do they hold all the
> text that a person types (a single large string or just an appendable
> textbox that stores the data till the end)?
> 2. If Dr.Racket holds text in a textbox-like object where all the
> information is just appended on, how can it sense a word (like "define" or
> any variable or numbers) and change the color of the font *instantly
> *(wouldn't that
> take a lot of memory)?
> 3. How does Dr.Racket change the text color just for one word (i.e.[This is
> an ***example.***] (the word between the two *** is green).?
Others have given you good general answers.
For DrRacket specifically:
The content of a text editor is stored as a binary tree of lines, where
each line is a list of "snips". A snip represents a sequence of
characters all in the same style (font, color, etc.) or some other item
such as an image or a nested editor.
The tree of lines is key to making operations on the editor efficient:
* To quickly map from positions in the text to a snip or from a
graphical location to a snip, each node in the tree of lines
effectively keeps track of the number of characters and total
graphical height of its left subtree.
* To change the style on a small sequence of selected text, the
position of the selection is mapped to the snips that contain the
range. Those snips may need to be broken up so that snip boundaries
match the selection, but all of that work is within a single line. A
new tree of lines can be built around the new line, which is fast
because only path from the line to the root needs to be rebuilt.
* Insertion is similar, in that only the spine of the tree needs to be
rebuilt to add items to a line or lines to the tree. The tree is a
"red-black tree", so it stays balanced as lines are added to the
tree, which means that the part from any line to the tree root is
short relative to the size of the tree (i.e., the number of lines).