+
+ if (startpit + 1 == pit_type(pars.size()))
+ return PitPosPair(endpit, endpos);
+
+ if (doclear) {
+ pars[startpit + 1].stripLeadingSpaces();
+ }
+
+ // paste the paragraphs again, if possible
+ if (all_erased &&
+ (pars[startpit].hasSameLayout(pars[startpit + 1]) ||
+ pars[startpit + 1].empty())) {
+ mergeParagraph(params, pars, startpit);
+ // this because endpar gets deleted here!
+ endpit = startpit;
+ endpos = startpos;
+ }
+
+ return PitPosPair(endpit, endpos);
+
+}
+
+
+void copySelectionHelper(ParagraphList & pars,
+ pit_type startpit, pit_type endpit,
+ int start, int end, textclass_type tc)
+{
+ BOOST_ASSERT(0 <= start && start <= pars[startpit].size());
+ BOOST_ASSERT(0 <= end && end <= pars[endpit].size());
+ BOOST_ASSERT(startpit != endpit || start <= end);
+
+ // Clone the paragraphs within the selection.
+ ParagraphList paragraphs(pars.begin() + startpit, pars.begin() + endpit + 1);
+ for_each(paragraphs.begin(), paragraphs.end(), resetOwnerAndChanges());
+
+ // Cut out the end of the last paragraph.
+ Paragraph & back = paragraphs.back();
+ back.erase(end, back.size());
+
+ // Cut out the begin of the first paragraph
+ Paragraph & front = paragraphs.front();
+ front.erase(0, start);
+
+ theCuts.push(make_pair(paragraphs, tc));
+}
+
+
+
+PitPosPair cutSelectionHelper(BufferParams const & params,
+ ParagraphList & pars, pit_type startpit, pit_type endpit,
+ int startpos, int endpos, textclass_type tc, bool doclear)
+{
+ copySelectionHelper(pars, startpit, endpit, startpos, endpos, tc);
+ return eraseSelectionHelper(params, pars, startpit, endpit,
+ startpos, endpos, doclear);
+}
+
+
+} // namespace anon
+
+
+
+
+namespace lyx {
+namespace cap {
+
+string grabAndEraseSelection(LCursor & cur)
+{
+ if (!cur.selection())
+ return string();
+ string res = grabSelection(cur);
+ eraseSelection(cur);
+ cur.selection() = false;
+ return res;
+}
+
+
+int SwitchLayoutsBetweenClasses(textclass_type c1, textclass_type c2,
+ ParagraphList & pars, ErrorList & errorlist)
+{
+ BOOST_ASSERT(!pars.empty());
+ int ret = 0;
+ if (c1 == c2)
+ return ret;
+
+ LyXTextClass const & tclass1 = textclasslist[c1];
+ LyXTextClass const & tclass2 = textclasslist[c2];
+
+ InsetText in;
+ std::swap(in.paragraphs(), pars);
+
+ ParIterator end = par_iterator_end(in);
+ for (ParIterator it = par_iterator_begin(in); it != end; ++it) {
+ string const name = it->layout()->name();
+ bool hasLayout = tclass2.hasLayout(name);
+
+ if (hasLayout)
+ it->layout(tclass2[name]);
+ else
+ it->layout(tclass2.defaultLayout());
+
+ if (!hasLayout && name != tclass1.defaultLayoutName()) {
+ ++ret;
+ string const s = bformat(
+ _("Layout had to be changed from\n%1$s to %2$s\n"
+ "because of class conversion from\n%3$s to %4$s"),
+ name, it->layout()->name(), tclass1.name(), tclass2.name());
+ // To warn the user that something had to be done.
+ errorlist.push_back(ErrorItem("Changed Layout", s,
+ it->id(), 0,
+ it->size()));