+static bool moveItem(Paragraph & fromPar, pos_type fromPos,
+ Paragraph & toPar, pos_type toPos, BufferParams const & params)
+{
+ // Note: moveItem() does not honour change tracking!
+ // Therefore, it should only be used for breaking and merging paragraphs
+
+ // We need a copy here because the character at fromPos is going to be erased.
+ Font const tmpFont = fromPar.getFontSettings(params, fromPos);
+ Change const tmpChange = fromPar.lookupChange(fromPos);
+
+ if (Inset * tmpInset = fromPar.getInset(fromPos)) {
+ fromPar.releaseInset(fromPos);
+ // The inset is not in fromPar any more.
+ if (!toPar.insertInset(toPos, tmpInset, tmpFont, tmpChange)) {
+ delete tmpInset;
+ return false;
+ }
+ return true;
+ }
+
+ char_type const tmpChar = fromPar.getChar(fromPos);
+ fromPar.eraseChar(fromPos, false);
+ toPar.insertChar(toPos, tmpChar, tmpFont, tmpChange);
+ return true;
+}
+
+
+void breakParagraphConservative(BufferParams const & bparams,
+ ParagraphList & pars, pit_type par_offset, pos_type pos)
+{
+ // create a new paragraph
+ Paragraph & tmp = *pars.insert(boost::next(pars.begin(), par_offset + 1),
+ Paragraph());
+ Paragraph & par = pars[par_offset];
+
+ tmp.setInsetOwner(&par.inInset());
+ tmp.makeSameLayout(par);
+
+ LASSERT(pos <= par.size(), /**/);
+
+ if (pos < par.size()) {
+ // move everything behind the break position to the new paragraph
+ pos_type pos_end = par.size() - 1;
+
+ for (pos_type i = pos, j = 0; i <= pos_end; ++i) {
+ if (moveItem(par, pos, tmp, j, bparams)) {
+ ++j;
+ }
+ }
+ // Move over the end-of-par change information
+ tmp.setChange(tmp.size(), par.lookupChange(par.size()));
+ par.setChange(par.size(), Change(bparams.trackChanges ?
+ Change::INSERTED : Change::UNCHANGED));
+ }
+}
+
+
+void mergeParagraph(BufferParams const & bparams,
+ ParagraphList & pars, pit_type par_offset)
+{
+ Paragraph & next = pars[par_offset + 1];
+ Paragraph & par = pars[par_offset];
+
+ pos_type pos_end = next.size() - 1;
+ pos_type pos_insert = par.size();
+
+ // the imaginary end-of-paragraph character (at par.size()) has to be
+ // marked as unmodified. Otherwise, its change is adopted by the first
+ // character of the next paragraph.
+ if (par.isChanged(par.size())) {
+ LYXERR(Debug::CHANGES,
+ "merging par with inserted/deleted end-of-par character");
+ par.setChange(par.size(), Change(Change::UNCHANGED));
+ }
+
+ Change change = next.lookupChange(next.size());
+
+ // move the content of the second paragraph to the end of the first one
+ for (pos_type i = 0, j = pos_insert; i <= pos_end; ++i) {
+ if (moveItem(next, 0, par, j, bparams)) {
+ ++j;
+ }
+ }
+
+ // move the change of the end-of-paragraph character
+ par.setChange(par.size(), change);
+
+ pars.erase(boost::next(pars.begin(), par_offset + 1));
+}
+
+
+Text::Text(InsetText * owner, bool use_default_layout)
+ : owner_(owner), autoBreakRows_(false), undo_counter_(0)
+{
+ pars_.push_back(Paragraph());
+ Paragraph & par = pars_.back();
+ par.setInsetOwner(owner);
+ DocumentClass const & dc = owner->buffer().params().documentClass();
+ if (use_default_layout)
+ par.setDefaultLayout(dc);
+ else
+ par.setPlainLayout(dc);
+}
+
+
+Text::Text(InsetText * owner, Text const & text)
+ : owner_(owner), autoBreakRows_(text.autoBreakRows_), undo_counter_(0)
+{
+ pars_ = text.pars_;
+ ParagraphList::iterator const end = pars_.end();
+ ParagraphList::iterator it = pars_.begin();
+ for (; it != end; ++it)
+ it->setInsetOwner(owner);
+}
+
+
+pit_type Text::depthHook(pit_type pit, depth_type depth) const
+{
+ pit_type newpit = pit;
+
+ if (newpit != 0)
+ --newpit;
+
+ while (newpit != 0 && pars_[newpit].getDepth() > depth)
+ --newpit;
+
+ if (pars_[newpit].getDepth() > depth)
+ return pit;
+
+ return newpit;
+}
+
+
+pit_type Text::outerHook(pit_type par_offset) const
+{
+ Paragraph const & par = pars_[par_offset];
+
+ if (par.getDepth() == 0)
+ return pars_.size();
+ return depthHook(par_offset, depth_type(par.getDepth() - 1));
+}
+
+
+bool Text::isFirstInSequence(pit_type par_offset) const
+{
+ Paragraph const & par = pars_[par_offset];
+
+ pit_type dhook_offset = depthHook(par_offset, par.getDepth());
+
+ if (dhook_offset == par_offset)
+ return true;
+
+ Paragraph const & dhook = pars_[dhook_offset];
+
+ return dhook.layout() != par.layout()
+ || dhook.getDepth() != par.getDepth();
+}
+
+
+Font const Text::outerFont(pit_type par_offset) const
+{
+ depth_type par_depth = pars_[par_offset].getDepth();
+ FontInfo tmpfont = inherit_font;
+
+ // Resolve against environment font information
+ while (par_offset != pit_type(pars_.size())
+ && par_depth
+ && !tmpfont.resolved()) {
+ par_offset = outerHook(par_offset);
+ if (par_offset != pit_type(pars_.size())) {
+ tmpfont.realize(pars_[par_offset].layout().font);
+ par_depth = pars_[par_offset].getDepth();
+ }
+ }
+
+ return Font(tmpfont);
+}
+
+
+void acceptChanges(ParagraphList & pars, BufferParams const & bparams)
+{
+ pit_type pars_size = static_cast<pit_type>(pars.size());
+
+ // first, accept changes within each individual paragraph
+ // (do not consider end-of-par)
+ for (pit_type pit = 0; pit < pars_size; ++pit) {
+ if (!pars[pit].empty()) // prevent assertion failure
+ pars[pit].acceptChanges(0, pars[pit].size());
+ }