+
+
+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