+std::vector<string> const availableSelections(Buffer const & buffer)
+{
+ vector<string> 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;
+ string 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, string::npos, "...");
+ break;
+ }
+ }
+
+ selList.push_back(asciiSel);
+ }
+
+ return selList;
+}
+
+
+int nrOfParagraphs()
+{
+ return theCuts.empty() ? 0 : theCuts[0].first.size();
+}
+
+
+void cutSelection(LCursor & cur, bool doclear, bool realcut)
+{
+ 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 stuffClipboard. (Lgb)
+ cur.bv().stuffClipboard(cur.selectionAsString(true));
+
+ // 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()
+
+ // 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(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();
+ updateCounters(cur.buffer());
+ }
+
+ if (cur.inMathed()) {
+ lyxerr << "cutSelection in mathed" << endl;
+ LCursor tmp = cur;
+ copySelection(cur);
+ cur.selection() = false;
+ eraseSelection(tmp);
+ }
+}
+
+
+void copySelection(LCursor & cur)
+{
+ // stuff the selection onto the X clipboard, from an explicit copy request
+ cur.bv().stuffClipboard(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(pars, par, cur.selEnd().pit(),
+ pos, cur.selEnd().pos(), cur.buffer().params().textclass);
+ }
+
+ if (cur.inMathed()) {
+ lyxerr << "copySelection in mathed" << endl;
+ ParagraphList pars;
+ pars.push_back(Paragraph());
+ BufferParams const & bp = cur.buffer().params();
+ pars.back().layout(bp.getLyXTextClass().defaultLayout());
+ for_each(pars.begin(), pars.end(), resetOwnerAndChanges());
+ pars.back().insert(0, grabSelection(cur), LyXFont());
+ theCuts.push(make_pair(pars, bp.textclass));
+ }
+}
+
+
+std::string getSelection(Buffer const & buf, size_t sel_index)
+{
+ return sel_index < theCuts.size()
+ ? theCuts[sel_index].first.back().asString(buf, false)
+ : string();
+}
+
+
+void pasteSelection(LCursor & cur, size_t sel_index)
+{
+ // this does not make sense, if there is nothing to paste
+ lyxerr << "#### pasteSelection " << sel_index << endl;
+ if (!checkPastePossible(sel_index))
+ return;
+
+ if (cur.inTexted()) {
+ LyXText * text = cur.text();
+ BOOST_ASSERT(text);
+
+ recordUndo(cur);
+
+ pit_type endpit;
+ PitPosPair ppp;
+
+ ErrorList el;
+
+ boost::tie(ppp, endpit) =
+ pasteSelectionHelper(cur.buffer(),
+ text->paragraphs(),
+ cur.pit(), cur.pos(),
+ cur.buffer().params().textclass,
+ sel_index, el);
+ bufferErrors(cur.buffer(), el);
+ cur.bv().showErrorList(_("Paste"));
+
+ cur.clearSelection();
+ cur.resetAnchor();
+ text->setCursor(cur, ppp.first, ppp.second);
+ cur.setSelection();
+ updateCounters(cur.buffer());
+ }
+
+ if (cur.inMathed()) {
+ lyxerr << "### should be handled in MathNest/GridInset" << endl;
+ }
+}
+
+
+void setSelectionRange(LCursor & cur, pos_type length)
+{
+ LyXText * text = cur.text();
+ BOOST_ASSERT(text);
+ if (!length)
+ return;
+ cur.resetAnchor();
+ while (length--)
+ text->cursorRight(cur);
+ cur.setSelection();
+}
+
+
+// simple replacing. The font of the first selected character is used
+void replaceSelectionWithString(LCursor & cur, string const & str)
+{
+ LyXText * text = cur.text();
+ BOOST_ASSERT(text);
+ recordUndo(cur);
+
+ // Get font setting before we cut
+ pos_type pos = cur.selEnd().pos();
+ Paragraph & par = text->getPar(cur.selEnd().pit());
+ LyXFont const font =
+ par.getFontSettings(cur.buffer().params(), cur.selBegin().pos());
+
+ // Insert the new string
+ string::const_iterator cit = str.begin();
+ string::const_iterator end = str.end();
+ for (; cit != end; ++cit, ++pos)
+ par.insertChar(pos, (*cit), font);
+
+ // Cut the selection
+ cutSelection(cur, true, false);
+}
+
+
+void replaceSelection(LCursor & cur)