On Wed, Jun 04, 2003 at 05:08:24PM +0200, Andre' Poenitz wrote:
> This fixes part of the cursor positioning after undo.
> It even seems to work sometimes in insets.

And here's the patch...

Index: bufferview_funcs.C
===================================================================
RCS file: /usr/local/lyx/cvsroot/lyx-devel/src/bufferview_funcs.C,v
retrieving revision 1.81
diff -u -p -r1.81 bufferview_funcs.C
--- bufferview_funcs.C  16 May 2003 14:35:15 -0000      1.81
+++ bufferview_funcs.C  4 Jun 2003 14:53:25 -0000
@@ -359,6 +359,8 @@ string const currentState(BufferView * b
        }
 #ifdef DEVEL_VERSION
        state << _(", Paragraph: ") << text->cursor.par()->id();
+       state << "  Inset: " <<
+               (text->cursor.par()->inInset() ? text->cursor.par()->inInset()->id() : 
-1);
 #endif
        return STRCONV(state.str());
 }
Index: undo_funcs.C
===================================================================
RCS file: /usr/local/lyx/cvsroot/lyx-devel/src/undo_funcs.C,v
retrieving revision 1.70
diff -u -p -r1.70 undo_funcs.C
--- undo_funcs.C        4 Jun 2003 12:45:26 -0000       1.70
+++ undo_funcs.C        4 Jun 2003 14:53:26 -0000
@@ -60,11 +60,9 @@ bool textHandleUndo(BufferView * bv, Und
 */
 
        // Set the right(new) inset-owner of the paragraph if there is any.
-       UpdatableInset * inset = 0;
-       if (undo.inset_id >= 0) {
-               Inset * in = bv->buffer()->getInsetFromID(undo.inset_id);
-               inset = static_cast<UpdatableInset *>(in);
-       }
+       UpdatableInset * inset =
+               static_cast<UpdatableInset *>(buf->getInsetFromID(undo.inset_id));
+       
        ParagraphList::iterator pit = undo.pars.begin();
        ParagraphList::iterator end = undo.pars.end();
        for ( ; pit != end; ++pit)
@@ -72,8 +70,10 @@ bool textHandleUndo(BufferView * bv, Und
 
        lyxerr << "\nhandle: inset_id: " << undo.inset_id << "\n";
        lyxerr << "handle: inset: " << inset << "\n";
-       lyxerr << "handle: plist_id: " << undo.plist_id << "\n";
+       //lyxerr << "handle: plist_id: " << undo.plist_id << "\n";
        lyxerr << "handle: undo.pars.size(): " << undo.pars.size() << "\n";
+       lyxerr << "handle: first_offset:  " << undo.first_par_offset  << "\n";
+       lyxerr << "handle: last_offset: " << undo.last_par_offset << "\n";
 
        // remove stuff between first and behind
        {
@@ -83,16 +83,19 @@ bool textHandleUndo(BufferView * bv, Und
                advance(last, plist->size() - undo.last_par_offset);
                lyxerr << "handle: first_id:  " << first->id()  << "\n";
                lyxerr << "handle: last_id: " << last->id() << "\n";
+               lyxerr << "handle: remove: " << distance(first, last) + 1 << " pars\n";
                plist->erase(first, ++last);
-               lyxerr << "after remove\n";
+               lyxerr << "handle: after remove\n";
        }
 
        // re-insert old stuff
        {
                ParagraphList::iterator first = plist->begin();
                advance(first, undo.first_par_offset);
+               lyxerr << "handle: plist->size: " << plist->size() << "\n";
+               lyxerr << "handle: offset: " << undo.first_par_offset << "\n";
                plist->insert(first, undo.pars.begin(), undo.pars.end());
-               lyxerr << "after insert\n";
+               lyxerr << "handle: after insert\n";
        }
 
        /*
@@ -107,37 +110,27 @@ bool textHandleUndo(BufferView * bv, Und
        */
 
        // redo Paragraphs  (should be handled outside undo...)
-       LyXText * text = inset ? inset->getLyXText(bv) : bv->text;
        {
-               lyxerr << "text: " << text << "\n";
+               //LyXText * text = inset ? inset->getLyXText(bv) : bv->text;
+               LyXText * text = bv->text;
+               lyxerr << "handle: text: " << text << "\n";
                if (undo.first_par_offset) {
                        ParagraphList::iterator redo = plist->begin();
+                       lyxerr << "handle: 1\n";
                        advance(redo, undo.first_par_offset);
+                       lyxerr << "handle: 2\n";
                        text->setCursorIntern(plist->begin(), 0);
                }
+               lyxerr << "handle: 3\n";
                text->redoParagraphs(text->cursor, plist->end());
-               lyxerr << "after redo\n";
-       }
-
-/*
-       Inset * = bv->buffer()->getInsetFromID(inset_id);
-       lyxerr << "tmppar: " << tmppar->id() << "\n";
-       LyXText * t;
-       if (it) {
-               FuncRequest cmd(bv, LFUN_INSET_EDIT, "left");
-               it->localDispatch(cmd);
-               t = it->getLyXText(bv);
-       } else {
-               t = bv->text;
-       }
-       t->setCursorIntern(*tmppar, undo.cursor_pos);
-       // Clear any selection and set the selection
-       // cursor for an evt. new selection.
-       t->clearSelection();
-       t->selection.cursor = t->cursor;
-       t->updateCounters();
-*/
+               lyxerr << "handle: after redo\n";
 
+               if (inset) {
+                       FuncRequest cmd(bv, LFUN_INSET_EDIT, "left");
+                       inset->localDispatch(cmd);
+               }
+       }
+       
        if (inset) {
                lyxerr << "fit cursor...\n";
                bv->fitCursor();
@@ -146,9 +139,15 @@ bool textHandleUndo(BufferView * bv, Und
 
        // set cursor
        {
-               ParagraphList::iterator cursor = plist->begin();
+               LyXText * text = inset ? inset->getLyXText(bv) : bv->text;
+               ParagraphList::iterator cursor = text->ownerParagraphs().begin();
                advance(cursor, undo.cursor_par_offset);
-               bv->text->setCursorIntern(cursor, undo.cursor_pos);
+               text->setCursorIntern(cursor, undo.cursor_pos);
+               // Clear any selection and set the selection
+               // cursor for an evt. new selection.
+               text->clearSelection();
+               text->selection.cursor = text->cursor;
+               text->updateCounters();
                lyxerr << "after setCursor\n";
        }
 
@@ -172,6 +171,10 @@ void createUndo(BufferView * bv, Undo::u
 
        lyxerr << "\n";
 
+       UpdatableInset * inset = first->inInset();
+       LyXText * text = inset ? inset->getLyXText(bv) : bv->text;
+       int const inset_id = inset ? inset->id() : -1;
+
 #if 0
        // this is what we'd like to have in the end for small grained undo
        for (ParIterator it = buf->par_iterator_begin(); it != null; ++it) {
@@ -198,23 +201,13 @@ void createUndo(BufferView * bv, Undo::u
 
 #endif
 
-       int const inset_id = first->inInset() ? first->inInset()->id() : -1;
-
        int const first_offset  = std::distance(plist->begin(), first);
        int const last_offset   = std::distance(last, plist->end());
 
        if (last == plist->end()) {
-               lyxerr << "*** createUndo: last == end  schould not happen\n";
+               lyxerr << "*** createUndo: last == end  should not happen\n";
        }
 
-       //lyxerr << "create: plist_id:     " << plist->id()  << "\n";
-       lyxerr << "create: first_id:     " << first->id()  << "\n";
-       lyxerr << "create: last_id:      " << last->id()   << "\n";
-       lyxerr << "create: first_offset: " << first_offset  << "\n";
-       lyxerr << "create: last_offset:  " << last_offset   << "\n";
-       lyxerr << "create: inset_id:     " << inset_id  << "\n";
-       lyxerr << "create: kind:         " << kind  << "\n";
-
 
        // Undo::EDIT and Undo::FINISH are
        // always finished. (no overlapping there)
@@ -235,17 +228,24 @@ void createUndo(BufferView * bv, Undo::u
        }
 
        // Create a new Undo.
-/*
-       // this should be re-activated once we are back at fine-grained undo
-       LyXCursor const & cur = bv->theLockingInset() ?
-                       bv->theLockingInset()->cursor(bv) : bv->text->cursor;
-       int const cursor_offset = std::distance(plist->begin(), cur.par());
-
-       stack.push(Undo(kind, inset_id, plist->id(),
-               first_offset, last_offset, cursor_offset, cur.pos(), ParagraphList()));
-*/
+       int const cursor_offset = std::distance
+               (text->ownerParagraphs().begin(), text->cursor.par());
+
+       //lyxerr << "create: plist_id:      " << plist->id()  << "\n";
+       lyxerr << "create: inset_id:      " << inset_id  << "\n";
+       lyxerr << "create: first_id:      " << first->id()  << "\n";
+       lyxerr << "create: last_id:       " << last->id()   << "\n";
+       lyxerr << "create: first_offset:  " << first_offset  << "\n";
+       lyxerr << "create: last_offset:   " << last_offset   << "\n";
+       lyxerr << "create: cursor_offset: " << cursor_offset   << "\n";
+       lyxerr << "create: cursor_pos:    " << text->cursor.pos() << "\n";
+
        stack.push(Undo(kind, inset_id, 0, //plist->id(),
-               first_offset, last_offset, first_offset, 0, ParagraphList()));
+               first_offset, last_offset,
+               cursor_offset, text->cursor.pos(),
+               ParagraphList()));
+       //stack.push(Undo(kind, inset_id, 0,
+       //      first_offset, last_offset, first_offset, 0, ParagraphList()));
 
        ParagraphList & undo_pars = stack.top().pars;
 

Reply via email to