+ // It is possible that the last line is empty if it was cursor_par
+ // and/or only had an error inset on it. So we set the cursor to the
+ // start of the doc to force its removal and ensure a valid saved cursor
+ if (text->setCursor(this, text->ownerParagraph(), 0)
+ && 0 == cursor_par_next) {
+ cursor_par = cursor_par_prev;
+ cursor_pos = cursor_par->size();
+ } else if (cursor_pos > cursor_par->size()) {
+ // Some C-Enter lines were removed by the setCursor call which
+ // then invalidated cursor_pos. It could still be "wrong" because
+ // the cursor may appear to have jumped but since we collapsed
+ // some C-Enter lines this should be a reasonable compromise.
+ cursor_pos = cursor_par->size();
+ }
+
+ // restore the original cursor in its corrected location.
+ text->setCursorIntern(this, cursor_par, cursor_pos);