+
+ // character styles
+ InsetIterator const i_end = inset_iterator_end(in);
+ for (InsetIterator it = inset_iterator_begin(in); it != i_end; ++it) {
+ if (it->lyxCode() == InsetBase::CHARSTYLE_CODE) {
+ InsetCharStyle & inset =
+ static_cast<InsetCharStyle &>(*it);
+ string const name = inset.params().type;
+ CharStyles::iterator const found_cs =
+ tclass2.charstyle(name);
+ if (found_cs == tclass2.charstyles().end()) {
+ // The character style is undefined in tclass2
+ inset.setUndefined();
+ docstring const s = bformat(_(
+ "Character style %1$s is "
+ "undefined because of class "
+ "conversion from\n%2$s to %3$s"),
+ from_utf8(name), from_utf8(tclass1.name()),
+ from_utf8(tclass2.name()));
+ // To warn the user that something had to be done.
+ errorlist.push_back(ErrorItem(
+ _("Undefined character style"),
+ s, it.paragraph().id(), it.pos(), it.pos() + 1));
+ } else if (inset.undefined()) {
+ // The character style is undefined in
+ // tclass1 and is defined in tclass2
+ inset.setDefined(found_cs);
+ }
+ }
+ }
+}
+
+
+std::vector<docstring> const availableSelections(Buffer const & buffer)
+{
+ vector<docstring> selList;
+
+ CutStack::const_iterator cit = theCuts.begin();
+ CutStack::const_iterator end = theCuts.end();
+ for (; cit != end; ++cit) {
+ // we do not use cit-> here because gcc 2.9x does not
+ // like it (JMarc)
+ ParagraphList const & pars = (*cit).first;
+ docstring asciiSel;
+ ParagraphList::const_iterator pit = pars.begin();
+ ParagraphList::const_iterator pend = pars.end();
+ for (; pit != pend; ++pit) {
+ asciiSel += pit->asString(buffer, false);
+ if (asciiSel.size() > 25) {
+ asciiSel.replace(22, docstring::npos,
+ from_ascii("..."));
+ break;
+ }
+ }
+
+ selList.push_back(asciiSel);
+ }
+
+ return selList;
+}
+
+
+size_type numberOfSelections()
+{
+ return theCuts.size();
+}
+
+
+void cutSelection(LCursor & cur, bool doclear, bool realcut)
+{
+ // This doesn't make sense, if there is no selection
+ if (!cur.selection())
+ return;
+
+ // OK, we have a selection. This is always between cur.selBegin()
+ // and cur.selEnd()
+
+ if (cur.inTexted()) {
+ LyXText * text = cur.text();
+ BOOST_ASSERT(text);
+ // Stuff what we got on the clipboard. Even if there is no selection.
+
+ // There is a problem with having the stuffing here in that the
+ // larger the selection the slower LyX will get. This can be
+ // solved by running the line below only when the selection has
+ // finished. The solution used currently just works, to make it
+ // faster we need to be more clever and probably also have more
+ // calls to theSelection().put. (Lgb)
+// theSelection().put(cur.selectionAsString(true));
+
+
+ // make sure that the depth behind the selection are restored, too
+ recordUndoSelection(cur);
+ pit_type begpit = cur.selBegin().pit();
+ pit_type endpit = cur.selEnd().pit();
+
+ int endpos = cur.selEnd().pos();
+
+ BufferParams const & bp = cur.buffer().params();
+ if (realcut) {
+ copySelectionHelper(cur.buffer(),
+ text->paragraphs(),
+ begpit, endpit,
+ cur.selBegin().pos(), endpos,
+ bp.textclass);
+ }
+
+ boost::tie(endpit, endpos) =
+ eraseSelectionHelper(bp,
+ text->paragraphs(),
+ begpit, endpit,
+ cur.selBegin().pos(), endpos,
+ doclear);
+
+ // sometimes necessary
+ if (doclear)
+ text->paragraphs()[begpit].stripLeadingSpaces();
+
+ // cutSelection can invalidate the cursor so we need to set
+ // it anew. (Lgb)
+ // we prefer the end for when tracking changes
+ cur.pos() = endpos;
+ cur.pit() = endpit;
+
+ // need a valid cursor. (Lgb)
+ cur.clearSelection();
+ updateLabels(cur.buffer());
+
+ // tell tabular that a recent copy happened
+ dirtyTabularStack(false);
+ }
+
+ if (cur.inMathed()) {
+ if (cur.selBegin().idx() != cur.selEnd().idx()) {
+ // The current selection spans more than one cell.
+ // Record all cells
+ recordUndoInset(cur);
+ } else {
+ // Record only the current cell to avoid a jumping
+ // cursor after undo
+ recordUndo(cur);
+ }
+ if (realcut)
+ copySelection(cur);
+ eraseSelection(cur);
+ }
+}
+
+
+void copySelection(LCursor & cur)
+{
+ // stuff the selection onto the X clipboard, from an explicit copy request
+ theClipboard().put(cur.selectionAsString(true));
+
+ // this doesn't make sense, if there is no selection
+ if (!cur.selection())
+ return;
+
+ if (cur.inTexted()) {
+ LyXText * text = cur.text();
+ BOOST_ASSERT(text);
+ // ok we have a selection. This is always between cur.selBegin()
+ // and sel_end cursor
+
+ // copy behind a space if there is one
+ ParagraphList & pars = text->paragraphs();
+ pos_type pos = cur.selBegin().pos();
+ pit_type par = cur.selBegin().pit();
+ while (pos < pars[par].size()
+ && pars[par].isLineSeparator(pos)
+ && (par != cur.selEnd().pit() || pos < cur.selEnd().pos()))
+ ++pos;
+
+ copySelectionHelper(cur.buffer(), pars, par, cur.selEnd().pit(),
+ pos, cur.selEnd().pos(), cur.buffer().params().textclass);
+ }
+
+ if (cur.inMathed()) {
+ //lyxerr << "copySelection in mathed" << endl;
+ ParagraphList pars;
+ Paragraph par;
+ BufferParams const & bp = cur.buffer().params();
+ par.layout(bp.getLyXTextClass().defaultLayout());
+ par.insert(0, grabSelection(cur), LyXFont(), Change(Change::UNCHANGED));
+ pars.push_back(par);
+ theCuts.push(make_pair(pars, bp.textclass));
+ }
+ // tell tabular that a recent copy happened
+ dirtyTabularStack(false);
+}
+
+
+docstring getSelection(Buffer const & buf, size_t sel_index)
+{
+ return sel_index < theCuts.size()
+ ? theCuts[sel_index].first.back().asString(buf, false)
+ : docstring();
+}
+
+
+void pasteParagraphList(LCursor & cur, ParagraphList const & parlist,
+ textclass_type textclass, ErrorList & errorList)
+{
+ if (cur.inTexted()) {
+ LyXText * text = cur.text();
+ BOOST_ASSERT(text);
+
+ pit_type endpit;
+ PitPosPair ppp;
+
+ boost::tie(ppp, endpit) =
+ pasteSelectionHelper(cur, parlist,
+ textclass, errorList);
+ updateLabels(cur.buffer());
+ cur.clearSelection();
+ text->setCursor(cur, ppp.first, ppp.second);
+ }
+
+ // mathed is handled in InsetMathNest/InsetMathGrid
+ BOOST_ASSERT(!cur.inMathed());
+}
+
+
+void pasteSelection(LCursor & cur, ErrorList & errorList, size_t sel_index)
+{
+ // this does not make sense, if there is nothing to paste
+ if (!checkPastePossible(sel_index))
+ return;
+
+ recordUndo(cur);
+ pasteParagraphList(cur, theCuts[sel_index].first,
+ theCuts[sel_index].second, errorList);
+ cur.setSelection();
+}
+
+
+// simple replacing. The font of the first selected character is used
+void replaceSelectionWithString(LCursor & cur, string const & str, bool backwards)
+{
+ recordUndo(cur);
+ DocIterator selbeg = cur.selectionBegin();
+
+ // Get font setting before we cut
+ LyXFont const font =
+ selbeg.paragraph().getFontSettings(cur.buffer().params(), selbeg.pos());
+
+ // Insert the new string
+ pos_type pos = cur.selEnd().pos();
+ Paragraph & par = cur.selEnd().paragraph();
+ string::const_iterator cit = str.begin();
+ string::const_iterator end = str.end();
+ for (; cit != end; ++cit, ++pos)
+ par.insertChar(pos, (*cit), font, cur.buffer().params().trackChanges);
+
+ // Cut the selection
+ cutSelection(cur, true, false);
+
+ // select the replacement
+ if (backwards) {
+ selbeg.pos() += str.length();
+ cur.setSelection(selbeg, -int(str.length()));
+ } else
+ cur.setSelection(selbeg, str.length());
+}
+
+
+void replaceSelection(LCursor & cur)
+{
+ if (cur.selection())
+ cutSelection(cur, true, false);
+}
+
+
+void eraseSelection(LCursor & cur)
+{
+ //lyxerr << "LCursor::eraseSelection begin: " << cur << endl;
+ CursorSlice const & i1 = cur.selBegin();
+ CursorSlice const & i2 = cur.selEnd();
+ if (i1.inset().asInsetMath()) {
+ cur.top() = i1;
+ if (i1.idx() == i2.idx()) {
+ i1.cell().erase(i1.pos(), i2.pos());
+ // We may have deleted i1.cell(cur.pos()).
+ // Make sure that pos is valid.
+ if (cur.pos() > cur.lastpos())
+ cur.pos() = cur.lastpos();
+ } else {
+ InsetMath * p = i1.asInsetMath();
+ InsetBase::row_type r1, r2;
+ InsetBase::col_type c1, c2;
+ region(i1, i2, r1, r2, c1, c2);
+ for (InsetBase::row_type row = r1; row <= r2; ++row)
+ for (InsetBase::col_type col = c1; col <= c2; ++col)
+ p->cell(p->index(row, col)).clear();
+ // We've deleted the whole cell. Only pos 0 is valid.
+ cur.pos() = 0;
+ }
+ // need a valid cursor. (Lgb)
+ cur.clearSelection();
+ } else {
+ lyxerr << "can't erase this selection 1" << endl;
+ }
+ //lyxerr << "LCursor::eraseSelection end: " << cur << endl;