Alfredo Braunstein wrote:
>> Help me through this, I'm getting confused. Here is my document:
>> 
>> "The quick brown fox jumped over the |lazy dog."
>> 
>> The position of the global cursor is indicated by '|'. Internally,
>> I imagine that this cursor contains a (valid) PosIterator.
>> 
>> I wish to find "brown":
>> PosIterator pos = search_for(buffer, "brown");
>> 
>> I now wish to replace "brown" with "red". How do I do so to ensure
>> that the global cursor remains valid?
> 
> Andr�'s solution is to modify only things under the global cursor.
> I.e, before the change put the global cursor in "pos", and then
> change "lazy" into "brown". The global cursor will remain valid,
> pointing to the "b" in brown.

Another solution that left the global cursor alone would be:

"The quick brown fox jumped over the |lazy dog."

// Find "brown"
PosIterator pos = search_for(buffer, "brown");
// Find offset from 1 past the end of "brown" to the cursor
lyx::distance_type offset = std::distance(pos + 5, cursor);
// Replace "brown" with "red"
// Returns 1 past the end of "red".
pos = replace(pos, pos + 5, "red");
// Reset the PositionIterator in cursor
if (offset > 0)
        cursor = std::advance(pos, offset);

Is such a strategy possible or desirable?

-- 
Angus

Reply via email to