+/**
+ * This breaks a paragraph at the specified position.
+ * The new paragraph will:
+ * - Decrease depth by one (or change layout to default layout) when
+ * keep_layout == false
+ * - keep current depth and layout when keep_layout == true
+ */
+static void breakParagraph(Text & text, pit_type par_offset, pos_type pos,
+ bool keep_layout)
+{
+ BufferParams const & bparams = text.inset().buffer().params();
+ ParagraphList & pars = text.paragraphs();
+ // create a new paragraph, and insert into the list
+ ParagraphList::iterator tmp =
+ pars.insert(boost::next(pars.begin(), par_offset + 1),
+ Paragraph());
+
+ Paragraph & par = pars[par_offset];
+
+ // remember to set the inset_owner
+ tmp->setInsetOwner(&par.inInset());
+ // without doing that we get a crash when typing <Return> at the
+ // end of a paragraph
+ tmp->setPlainOrDefaultLayout(bparams.documentClass());
+
+ // layout stays the same with latex-environments
+ if (keep_layout) {
+ tmp->setLayout(par.layout());
+ tmp->setLabelWidthString(par.params().labelWidthString());
+ tmp->params().depth(par.params().depth());
+ } else if (par.params().depth() > 0) {
+ Paragraph const & hook = pars[text.outerHook(par_offset)];
+ tmp->setLayout(hook.layout());
+ // not sure the line below is useful
+ tmp->setLabelWidthString(par.params().labelWidthString());
+ tmp->params().depth(hook.params().depth());
+ }
+
+ bool const isempty = (par.allowEmpty() && par.empty());
+
+ if (!isempty && (par.size() > pos || par.empty())) {
+ tmp->setLayout(par.layout());
+ tmp->params().align(par.params().align());
+ tmp->setLabelWidthString(par.params().labelWidthString());
+
+ tmp->params().depth(par.params().depth());
+ tmp->params().noindent(par.params().noindent());
+
+ // move everything behind the break position
+ // to the new paragraph
+
+ /* Note: if !keepempty, empty() == true, then we reach
+ * here with size() == 0. So pos_end becomes - 1. This
+ * doesn't cause problems because both loops below
+ * enforce pos <= pos_end and 0 <= pos
+ */
+ 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));
+
+ if (pos) {
+ // Make sure that we keep the language when
+ // breaking paragraph.
+ if (tmp->empty()) {
+ Font changed = tmp->getFirstFontSettings(bparams);
+ Font const & old = par.getFontSettings(bparams, par.size());
+ changed.setLanguage(old.language());
+ tmp->setFont(0, changed);
+ }
+
+ return;
+ }
+
+ if (!isempty) {
+ bool const soa = par.params().startOfAppendix();
+ par.params().clear();
+ // do not lose start of appendix marker (bug 4212)
+ par.params().startOfAppendix(soa);
+ par.setPlainOrDefaultLayout(bparams.documentClass());
+ }
+
+ // layout stays the same with latex-environments
+ if (keep_layout) {
+ par.setLayout(tmp->layout());
+ par.setLabelWidthString(tmp->params().labelWidthString());
+ par.params().depth(tmp->params().depth());
+ }
+}
+
+