Re: Text Editing

new topic     » goto parent     » topic index » view thread      » older message » newer message

Ian Smith wrote:

> I am wondering how to write a window that
> can scroll through a text buffer(a sequence
> holding all the text read in a file) and to be
> able to scroll through it with all the correct
> character breaks and no word-wrapping.

Typically I'll store each line of text as a seperate string. This makes
things a lot easier. The user can move anywhere in the display area,
independant of whether there is really text there or not.

I keep track of the topLine, which is the line number of the top line, and
the leftMargin, which is the position of the left margin. The screen display
code looks something like this:

   line = 1    -- position of the top line in the window
   row = 10  -- position of the left margin in the window

   -- display a screen of text
   for i = topLine to topLine + numberOfLines - 1 do

      -- is the line past the end of file?
      if i > length( text ) then
         -- blank line
         showText = repeat( ' ', pageWidth )

      else
         -- get a line of text
         theText = text[i]

         -- text left of margin?
         if length( theText ) < leftMargin then
            showText = repeat( ' ', pageWidth )

         else
            -- make the slice safe
            theText &= repeat( ' ', pageWidth )

            -- slice to fit screen
            showText = theText[leftMargin..leftMargin+pageWidth-1]

      end if

      -- display the text
      position( line, row )
      puts( 1, showText )

   end for

When the user presses a key, my editor first checks to see if there is
actual text where the cursor is positioned. For example, if the cursor were
located at the '_':

   this is an example        _

and the string wasn't that long, the first thing the editor would do would
add padding to that line so that the cursor is above a real character (the .
represents a space):

   this is an example.........

Then I can simply insert a character into that position, or whatever.
Actually, there's a test it performs before that: it makes sure that the
line acually exists. For example:

   this is the end of the file.


       _

Here, the cursor is four lines past the end of the file. So I first extend
the file, and then pad to the cursor position:


   this is the end of the file.
   .
   .
   .
   ....

And insert/delete/whatever.

Did that answer the question?

-- David Cuny

new topic     » goto parent     » topic index » view thread      » older message » newer message

Search



Quick Links

User menu

Not signed in.

Misc Menu