Author: forenr
Date: Fri Mar 18 18:26:17 2011
New Revision: 37954
URL: http://www.lyx.org/trac/changeset/37954

Log:
Fix bug #7361 (Assertion when a latex error occurs in the first (empty) 
paragraph)

Modified:
   lyx-devel/trunk/src/BufferView.cpp
   lyx-devel/trunk/src/BufferView.h
   lyx-devel/trunk/src/frontends/qt4/GuiErrorList.cpp

Modified: lyx-devel/trunk/src/BufferView.cpp
==============================================================================
--- lyx-devel/trunk/src/BufferView.cpp  Fri Mar 18 17:52:09 2011        (r37953)
+++ lyx-devel/trunk/src/BufferView.cpp  Fri Mar 18 18:26:17 2011        (r37954)
@@ -2364,6 +2364,42 @@
 }
 
 
+bool BufferView::selectIfEmpty(DocIterator & cur)
+{
+       if (!cur.paragraph().empty())
+               return false;
+
+       pit_type const beg_pit = cur.pit();
+       if (beg_pit > 0) {
+               // The paragraph associated to this item isn't
+               // the first one, so it can be selected
+               cur.backwardPos();
+       } else {
+               // We have to resort to select the space between the
+               // end of this item and the begin of the next one
+               cur.forwardPos();
+       }
+       if (cur.empty()) {
+               // If it is the only item in the document,
+               // nothing can be selected
+               return false;
+       }
+       pit_type const end_pit = cur.pit();
+       pos_type const end_pos = cur.pos();
+       d->cursor_.clearSelection();
+       d->cursor_.reset();
+       d->cursor_.setCursor(cur);
+       d->cursor_.pit() = beg_pit;
+       d->cursor_.pos() = 0;
+       d->cursor_.setSelection(false);
+       d->cursor_.resetAnchor();
+       d->cursor_.pit() = end_pit;
+       d->cursor_.pos() = end_pos;
+       d->cursor_.setSelection();
+       return true;
+}
+
+
 Cursor & BufferView::cursor()
 {
        return d->cursor_;

Modified: lyx-devel/trunk/src/BufferView.h
==============================================================================
--- lyx-devel/trunk/src/BufferView.h    Fri Mar 18 17:52:09 2011        (r37953)
+++ lyx-devel/trunk/src/BufferView.h    Fri Mar 18 18:26:17 2011        (r37954)
@@ -247,6 +247,9 @@
        void putSelectionAt(DocIterator const & cur,
                int length, bool backwards);
 
+       /// selects the item at cursor if its paragraph is empty.
+       bool selectIfEmpty(DocIterator & cur);
+
        /// update the internal \c ViewMetricsInfo.
        void updateMetrics();
 

Modified: lyx-devel/trunk/src/frontends/qt4/GuiErrorList.cpp
==============================================================================
--- lyx-devel/trunk/src/frontends/qt4/GuiErrorList.cpp  Fri Mar 18 17:52:09 
2011        (r37953)
+++ lyx-devel/trunk/src/frontends/qt4/GuiErrorList.cpp  Fri Mar 18 18:26:17 
2011        (r37954)
@@ -172,11 +172,17 @@
                return false;
        }
 
-       // If this paragraph is empty, highlight the previous one
-       while (dit.paragraph().empty())
-               dit.backwardPos();
-
        // Now make the selection.
+       BufferView * bv = const_cast<BufferView *>(bufferview());
+       if (bv->selectIfEmpty(dit)) {
+               // The paragraph is empty but can be selected
+               bv->processUpdateFlags(Update::Force | Update::FitCursor);
+               return true;
+       }
+       if (dit.empty()) {
+               // The paragraph is empty and cannot be selected
+               return false;
+       }
        // if pos_end is 0, this means it is end-of-paragraph
        pos_type const s = dit.paragraph().size();
        pos_type const end = err.pos_end ? min(err.pos_end, s) : s;
@@ -184,7 +190,6 @@
        pos_type const range = end == start ? s - start : end - start;
        // end-of-paragraph cannot be highlighted, so highlight the last thing
        dit.pos() = range ? start : end - 1;
-       BufferView * bv = const_cast<BufferView *>(bufferview());
        // FIXME LFUN
        // If we used an LFUN, we would not need these lines:
        bv->putSelectionAt(dit, max(range, pos_type(1)), false);

Reply via email to