Author Topic: Edit buffer questions  (Read 2087 times)

0 Members and 1 Guest are viewing this topic.

Offline Deep Toaster

  • So much to do, so much time, so little motivation
  • Administrator
  • LV13 Extreme Addict (Next: 9001)
  • *************
  • Posts: 8217
  • Rating: +758/-15
    • View Profile
    • ClrHome
Edit buffer questions
« on: April 13, 2011, 06:45:28 pm »
Anyone here used the edit buffer before, can you help me figure it out? I have the "Unsupported Info" guide to edit buffers and related routines, as well as a couple of other resources, but there are still many things I'm confused about.

  • What's editTail for? Is it basically just (editCursor)+1? That makes it sound pointless, but "Unsupported Info" says it's a "pointer to all the data that's after the cursor." Not sure what this means.
  • What's all the iMath pointer stuff? All the guides I've found on opening an existing program with the edit buffer say that these need to be set up, but what are they?
I have lots of other questions, so these are the ones I need to figure out now. Thanks in advance for any help!
« Last Edit: April 13, 2011, 06:55:11 pm by Deep Thought »




Offline FloppusMaximus

  • LV5 Advanced (Next: 300)
  • *****
  • Posts: 290
  • Rating: +57/-5
    • View Profile
Re: Edit buffer questions
« Reply #1 on: April 14, 2011, 11:45:10 pm »
There are two independent "layers" here, which are often confusingly lumped together.

First, we have the routines EditProg and CloseProg.  These routines are documented in the SDK guide.  EditProg allocates all free RAM into a given variable; CloseProg reverses the effect.  In order for this to work, the iMathPtrs are used to remember which variable is "open" for editing.  You shouldn't ever modify iMathPtr1, iMathPtr3, or iMathPtr4; you'll presumably need to modify iMathPtr2 in order to change the size of the variable.

Second, we have the "edit buffer" layer.  An edit buffer is a region of RAM that stores text for the user to edit.  The buffer might be allocated using EditProg (which is what the OS often does, e.g. when editing a program or equation), or it might be a static buffer that you create in safe RAM somewhere.  Or it might be allocated in some other way; it's up to you.  The edit buffer routines have nothing to do with the iMathPtrs; they use only the pointers (editTop), (editCursor), (editTail), and (editBtm).

An edit buffer consists of three parts: "head", "gap", and "tail".  The head contains the tokens that come before the insertion point, the tail contains the tokens that come after the insertion point, and the gap contains all of the free space.  These three areas are defined by the four pointers I mentioned above:
Code: [Select]
   tokens          free space            more tokens
|***************|.....................|*****************|
^               ^                     ^                 ^
(editTop)       (editCursor)          (editTail)        (editBtm)
This arrangement means that all of the usual editing operations take constant time.  Moving the insertion point "right", for instance, means copying a byte from (editTail) to (editCursor), then incrementing both pointers.  Inserting a token means writing it to (editCursor) and incrementing (editCursor).  Deleting a token means simply incrementing (editTail).  (Of course, if you're dealing with BASIC tokens, you also need to consider the possibility of 2-byte tokens, but you get the idea.)  On the other hand, it's not random access: you can't just start editing at an arbitrary place in the buffer; you first need to "move" the insertion point to that location.

You can see from the above diagram that:
- (editTop) ≤ (editCursor) ≤ (editTail) ≤ (editBtm); all of the system routines will assume this is true, and will most likely crash if it's violated.
- If (editCursor) = (editTop), the insertion point is at the "beginning" of the buffer.
- If (editTail) = (editBtm), the insertion point is at the "end" of the buffer.
- If (editCursor) = (editTail), the buffer is "full".

Offline Deep Toaster

  • So much to do, so much time, so little motivation
  • Administrator
  • LV13 Extreme Addict (Next: 9001)
  • *************
  • Posts: 8217
  • Rating: +758/-15
    • View Profile
    • ClrHome
Re: Edit buffer questions
« Reply #2 on: April 15, 2011, 08:01:36 pm »
Thanks, I get it now.

I don't have to use the OS's buffer routines to edit it though, right? If I wanted I could just lump the whole thing together into one block and resolve it at the end?