+
+
+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;
+}
+
+
+void selDel(LCursor & cur)
+{
+ //lyxerr << "LCursor::selDel" << endl;
+ if (cur.selection())
+ eraseSelection(cur);
+}
+
+
+void selClearOrDel(LCursor & cur)
+{
+ //lyxerr << "LCursor::selClearOrDel" << endl;
+ if (lyxrc.auto_region_delete)
+ selDel(cur);
+ else
+ cur.selection() = false;
+}
+
+
+docstring grabSelection(LCursor const & cur)
+{
+ if (!cur.selection())
+ return docstring();
+
+ // FIXME: What is wrong with the following?
+#if 0
+ std::ostringstream os;
+ for (DocIterator dit = cur.selectionBegin();
+ dit != cur.selectionEnd(); dit.forwardPos())
+ os << asString(dit.cell());
+ return os.str();
+#endif
+
+ CursorSlice i1 = cur.selBegin();
+ CursorSlice i2 = cur.selEnd();
+
+ if (i1.idx() == i2.idx()) {
+ if (i1.inset().asInsetMath()) {
+ MathArray::const_iterator it = i1.cell().begin();
+ return asString(MathArray(it + i1.pos(), it + i2.pos()));
+ } else {
+ return from_ascii("unknown selection 1");
+ }
+ }
+
+ InsetBase::row_type r1, r2;
+ InsetBase::col_type c1, c2;
+ region(i1, i2, r1, r2, c1, c2);
+
+ docstring data;
+ if (i1.inset().asInsetMath()) {
+ for (InsetBase::row_type row = r1; row <= r2; ++row) {
+ if (row > r1)
+ data += "\\\\";
+ for (InsetBase::col_type col = c1; col <= c2; ++col) {
+ if (col > c1)
+ data += '&';
+ data += asString(i1.asInsetMath()->
+ cell(i1.asInsetMath()->index(row, col)));
+ }
+ }
+ } else {
+ data = from_ascii("unknown selection 2");
+ }
+ return data;
+}
+
+
+void dirtyTabularStack(bool b)
+{
+ dirty_tabular_stack_ = b;
+}
+
+
+bool tabularStackDirty()
+{
+ return dirty_tabular_stack_;
+}
+
+
+} // namespace cap
+
+} // namespace lyx