- if (!checkPastePossible(*par))
- return false;
-
- if (pos > (*par)->size())
- pos = (*par)->size();
-
-#if 0
- // Paragraph * tmpbuf;
- Paragraph * tmppar = *par;
- int tmppos = pos;
-
- // There are two cases: cutbuffer only one paragraph or many
- if (!buf->next()) {
- // only within a paragraph
- Paragraph * tmpbuf = new Paragraph(*buf, false);
-
- // Some provisions should be done here for checking
- // if we are inserting at the beginning of a
- // paragraph. If there are a space at the beginning
- // of the text to insert and we are inserting at
- // the beginning of the paragraph the space should
- // be removed.
- while (buf->size()) {
- // This is an attempt to fix the
- // "never insert a space at the
- // beginning of a paragraph" problem.
- if (!tmppos && buf->isLineSeparator(0)) {
- buf->erase(0);
+ return pasteSelection(buffer, pars, pit, pos, tc, 0, errorlist);
+}
+
+
+pair<PitPosPair, ParagraphList::iterator>
+CutAndPaste::pasteSelection(Buffer const & buffer,
+ ParagraphList & pars,
+ ParagraphList::iterator pit, int pos,
+ textclass_type tc, size_t cut_index,
+ ErrorList & errorlist)
+{
+ if (!checkPastePossible())
+ return make_pair(PitPosPair(pit, pos), pit);
+
+ Assert (pos <= pit->size());
+
+ // Make a copy of the CaP paragraphs.
+ ParagraphList simple_cut_clone = cuts[cut_index].first;
+ textclass_type const textclass = cuts[cut_index].second;
+
+ // Now remove all out of the pars which is NOT allowed in the
+ // new environment and set also another font if that is required.
+
+ // Make sure there is no class difference.
+ SwitchLayoutsBetweenClasses(textclass, tc, simple_cut_clone,
+ errorlist);
+
+ ParagraphList::iterator tmpbuf = simple_cut_clone.begin();
+ int depth_delta = pit->params().depth() - tmpbuf->params().depth();
+
+ Paragraph::depth_type max_depth = pit->getMaxDepthAfter();
+
+ for (; tmpbuf != simple_cut_clone.end(); ++tmpbuf) {
+ // If we have a negative jump so that the depth would
+ // go below 0 depth then we have to redo the delta to
+ // this new max depth level so that subsequent
+ // paragraphs are aligned correctly to this paragraph
+ // at level 0.
+ if ((int(tmpbuf->params().depth()) + depth_delta) < 0)
+ depth_delta = 0;
+
+ // Set the right depth so that we are not too deep or shallow.
+ tmpbuf->params().depth(tmpbuf->params().depth() + depth_delta);
+ if (tmpbuf->params().depth() > max_depth)
+ tmpbuf->params().depth(max_depth);
+
+ // Only set this from the 2nd on as the 2nd depends
+ // for maxDepth still on pit.
+ if (tmpbuf != simple_cut_clone.begin())
+ max_depth = tmpbuf->getMaxDepthAfter();
+
+ // Set the inset owner of this paragraph.
+ tmpbuf->setInsetOwner(pit->inInset());
+ for (pos_type i = 0; i < tmpbuf->size(); ++i) {
+ if (tmpbuf->getChar(i) == Paragraph::META_INSET) {
+ if (!pit->insetAllowed(tmpbuf->getInset(i)->lyxCode())) {
+ tmpbuf->erase(i--);
+ }