]> git.lyx.org Git - lyx.git/blobdiff - src/BufferView2.C
finished fixing removeAutoInsets()
[lyx.git] / src / BufferView2.C
index 7bdda14a64331c48a76ad5896b6e4f6514771b29..8ad5d776a2eeca43070b9626d799200051bc6657 100644 (file)
@@ -110,18 +110,76 @@ bool BufferView::removeAutoInsets()
 {
        LyXCursor tmpcursor = text->cursor;
        Paragraph * cur_par = tmpcursor.par();
+       Paragraph * cur_par_prev = cur_par ? cur_par->previous() : 0;
+       Paragraph * cur_par_next = cur_par ? cur_par->next() : 0;
        pos_type cur_pos = tmpcursor.pos();
-       
+
        bool found = false;
 
+       // Trap the deletion of the paragraph the cursor is in.
+       // It should be almost impossible for the new cursor par to be
+       // deleted later on in this function.
+       // This is the way to segfault this now. Although you may have to do this
+       // multiple times: Have an InsetERT with an unknown command in it.
+       // View->DVI, move cursor between Error box and InsetERT and hit <Enter>,
+       // <down-arrow>, <Enter> again, View->DVI, BANG!
+       //
+       while ((cur_par_prev || cur_par_next)
+              && text->setCursor(this,
+                                 cur_par_prev ? cur_par_prev : cur_par_next,
+                                 0)) {
+               // we just removed cur_par so have to fix the "cursor"
+               if (cur_par_prev) {
+                       cur_par = cur_par_prev;
+                       cur_pos = cur_par->size();
+               } else {
+                       cur_par = cur_par_next;
+                       cur_pos = 0;
+               }
+               cur_par_prev = cur_par->previous();
+               cur_par_next = cur_par->next();
+       }
+
        ParIterator it = buffer()->par_iterator_begin();
        ParIterator end = buffer()->par_iterator_end();
        for (; it != end; ++it) {
                Paragraph * par = *it;
+               Paragraph * par_prev = par ? par->previous() : 0;
                bool removed = false;
 
-               text->setCursor(this, par, 0);
-               
+               if (text->setCursor(this, par, 0)
+                   && cur_par == par_prev) {
+                       // The previous setCursor line was deleted and that
+                       // was the cur_par line.  This can only happen if an
+                       // error box was the sole item on cur_par.
+                       if (cur_par_prev) {
+                               // '|' = par, '.' = cur_par, 'E' = error box
+                               // First step below may occur before while{}
+                               //  a    |a      a     a    .a
+                               //  E -> .E -> |.E -> .  -> |b
+                               // .      b      b    |b
+                               //  b
+                               cur_par = cur_par_prev;
+                               cur_pos = cur_par_prev->size();
+                               cur_par_prev = cur_par->previous();
+                               // cur_par_next remains the same 
+                       } else if (cur_par_next) {
+                               // First step below may occur before while{}
+                               // .
+                               //  E -> |.E -> |.  -> . -> .|a
+                               //  a      a      a    |a
+                               cur_par = cur_par_next;
+                               cur_pos = 0;
+                               // cur_par_prev remains unset
+                               cur_par_next = cur_par->next();
+                       } else {
+                               // I can't find a way to trigger this
+                               // so it should be unreachable code
+                               // unless the buffer is corrupted.
+                               lyxerr << "BufferView::removeAutoInsets() is bad\n";
+                       }
+               }
+
                Paragraph::inset_iterator pit = par->inset_iterator_begin();
                Paragraph::inset_iterator pend = par->inset_iterator_end();
                while (pit != pend) {
@@ -174,7 +232,7 @@ void BufferView::insertErrors(TeXErrors & terr)
                Paragraph * texrowpar = 0;
 
                if (tmpid == -1) {
-                       texrowpar = text->firstParagraph();
+                       texrowpar = text->ownerParagraph();
                        tmppos = 0;
                } else {
                        texrowpar = buffer()->getParFromID(tmpid);
@@ -203,7 +261,7 @@ void BufferView::setCursorFromRow(int row)
        Paragraph * texrowpar;
 
        if (tmpid == -1) {
-               texrowpar = text->firstParagraph();
+               texrowpar = text->ownerParagraph();
                tmppos = 0;
        } else {
                texrowpar = buffer()->getParFromID(tmpid);
@@ -590,7 +648,10 @@ bool BufferView::ChangeRefsIfUnique(string const & from, string const & to)
 {
        // Check if the label 'from' appears more than once
        vector<string> labels = buffer()->getLabelList();
-       if (count(labels.begin(), labels.end(), from) > 1)
+       // count is broken on some systems, so use the HP version
+       int res;
+       count(labels.begin(), labels.end(), from, res);
+       if (res > 1)
                return false;
 
        return ChangeInsets(Inset::REF_CODE, from, to);