]> git.lyx.org Git - lyx.git/blobdiff - src/text2.C
bug 1987: Crash when switching to different document class
[lyx.git] / src / text2.C
index 77f4699c4ae2372147d625c9f8e3f85368314393..e8001c7c7bd1dfdead898756c8b05a440739ffed 100644 (file)
@@ -481,11 +481,9 @@ void LyXText::setFont(LCursor & cur, LyXFont const & font, bool toggleall)
 void LyXText::cursorHome(LCursor & cur)
 {
        BOOST_ASSERT(this == cur.text());
-       Paragraph const & par = cur.paragraph();
-       if (cur.boundary() && cur.pos())
-               setCursor(cur, cur.pit(), par.getRow(cur.pos()-1).pos());
-       else
-               setCursor(cur, cur.pit(), cur.textRow().pos());
+       Row const & row = cur.paragraph().getRow(cur.pos(),cur.boundary());
+
+       setCursor(cur, cur.pit(), row.pos());
 }
 
 
@@ -496,13 +494,17 @@ void LyXText::cursorEnd(LCursor & cur)
        // the final space exept if I have a spanning inset or one string
        // is so long that we force a break.
        pos_type end = cur.textRow().endpos();
+       if (end == 0)
+               // empty text, end-1 is no valid position
+               return;
        bool boundary = false;
-       if (!cur.paragraph().isLineSeparator(end-1) &&
-           !cur.paragraph().isNewline(end-1))
-       {
-               boundary = true;
-       } else if (end != cur.lastpos())
-               --end;
+       if (end != cur.lastpos()) {
+               if (!cur.paragraph().isLineSeparator(end-1) 
+                       && !cur.paragraph().isNewline(end-1))
+                       boundary = true;
+               else
+                       --end;
+       }
        setCursor(cur, cur.pit(), end, true, boundary);
 }
 
@@ -624,18 +626,8 @@ void LyXText::insertInset(LCursor & cur, InsetBase * inset)
 // needed to insert the selection
 void LyXText::insertStringAsLines(LCursor & cur, string const & str)
 {
-       pit_type pit = cur.pit();
-       pos_type pos = cur.pos();
-       recordUndo(cur);
-
-       // only to be sure, should not be neccessary
-       cur.clearSelection();
-       cur.buffer().insertStringAsLines(pars_, pit, pos, current_font, str,
-               autoBreakRows_);
-
-       cur.resetAnchor();
-       setCursor(cur, cur.pit(), pos);
-       cur.setSelection();
+       cur.buffer().insertStringAsLines(pars_, cur.pit(), cur.pos(), 
+               current_font, str, autoBreakRows_);
 }
 
 
@@ -870,7 +862,7 @@ pos_type LyXText::getColumnNearX(pit_type const pit,
 #endif
 
        x = int(tmpx) + xo;
-       int const col = c - row.pos();
+       pos_type const col = c - row.pos();
 
         if (!c || end == par.size())
                 return col;
@@ -1029,7 +1021,8 @@ bool LyXText::cursorRight(LCursor & cur)
        if (cur.pos() != cur.lastpos()) {
                bool updateNeeded = false;
                if (!checkAndActivateInset(cur, true)) {
-                       if (cur.textRow().endpos() == (cur.pos() + 1) &&
+                       if (cur.textRow().endpos() == cur.pos() + 1 &&
+                           cur.textRow().endpos() != cur.lastpos() &&
                            !cur.paragraph().isLineSeparator(cur.pos()) &&
                            !cur.paragraph().isNewline(cur.pos()))
                        {
@@ -1084,7 +1077,8 @@ bool LyXText::cursorUp(LCursor & cur)
        } else if (cur.pit() > 0) {
                --cur.pit();
                //cannot use 'par' now
-               updateNeeded |= setCursor(cur, cur.pit(), x2pos(cur.pit(), cur.paragraph().rows().size() - 1, x));
+               updateNeeded |= setCursor(cur, cur.pit(),
+      x2pos(cur.pit(), cur.paragraph().rows().size() - 1, x));
        }
 
        cur.x_target() = x;
@@ -1229,7 +1223,8 @@ bool LyXText::deleteEmptyParagraphMechanism(LCursor & cur, LCursor const & old)
                if (old.pos() > 0
                    && old.pos() < oldpar.size()
                    && oldpar.isLineSeparator(old.pos())
-                   && oldpar.isLineSeparator(old.pos() - 1)) {
+                   && oldpar.isLineSeparator(old.pos() - 1)
+                   && oldpar.lookupChange(old.pos() - 1) != Change::DELETED) {
                        // We need to set the text to Change::INSERTED to
                        // get it erased properly
                        pars_[old.pit()].setChange(old.pos() -1,
@@ -1298,8 +1293,10 @@ bool LyXText::deleteEmptyParagraphMechanism(LCursor & cur, LCursor const & old)
                }
        }
 
-       if (deleted)
+       if (deleted) {
+               updateCounters(cur.buffer());
                return true;
+       }
 
        if (pars_[old.pit()].stripLeadingSpaces())
                cur.resetAnchor();
@@ -1308,12 +1305,6 @@ bool LyXText::deleteEmptyParagraphMechanism(LCursor & cur, LCursor const & old)
 }
 
 
-ParagraphList & LyXText::paragraphs() const
-{
-       return const_cast<ParagraphList &>(pars_);
-}
-
-
 void LyXText::recUndo(pit_type first, pit_type last) const
 {
        recordUndo(bv()->cursor(), Undo::ATOMIC, first, last);