+
+void LCursor::pullArg()
+{
+#warning Look here
+ MathArray ar = cell();
+ if (popLeft() && inMathed()) {
+ plainErase();
+ cell().insert(pos(), ar);
+ resetAnchor();
+ } else {
+ //formula()->mutateToText();
+ }
+}
+
+
+void LCursor::touch()
+{
+#warning look here
+#if 0
+ DocumentIterator::const_iterator it = begin();
+ DocumentIterator::const_iterator et = end();
+ for ( ; it != et; ++it)
+ it->cell().touch();
+#endif
+}
+
+
+void LCursor::normalize()
+{
+ if (idx() >= nargs()) {
+ lyxerr << "this should not really happen - 1: "
+ << idx() << ' ' << nargs()
+ << " in: " << &inset() << endl;
+ }
+ idx() = min(idx(), lastidx());
+
+ if (pos() > lastpos()) {
+ lyxerr << "this should not really happen - 2: "
+ << pos() << ' ' << lastpos() << " in idx: " << idx()
+ << " in atom: '";
+ WriteStream wi(lyxerr, false, true);
+ inset().asMathInset()->write(wi);
+ lyxerr << endl;
+ }
+ pos() = min(pos(), lastpos());
+}
+
+
+char LCursor::valign()
+{
+ idx_type idx;
+ MathGridInset * p = enclosingGrid(idx);
+ return p ? p->valign() : '\0';
+}
+
+
+char LCursor::halign()
+{
+ idx_type idx;
+ MathGridInset * p = enclosingGrid(idx);
+ return p ? p->halign(idx % p->ncols()) : '\0';
+}
+
+
+bool LCursor::goUpDown(bool up)
+{
+ // Be warned: The 'logic' implemented in this function is highly
+ // fragile. A distance of one pixel or a '<' vs '<=' _really
+ // matters. So fiddle around with it only if you think you know
+ // what you are doing!
+ int xo = 0;
+ int yo = 0;
+ getPos(xo, yo);
+
+ // check if we had something else in mind, if not, this is the future goal
+ if (x_target() == -1)
+ x_target() = xo;
+ else
+ xo = x_target();
+
+ // try neigbouring script insets
+ if (!selection()) {
+ // try left
+ if (pos() != 0) {
+ MathScriptInset const * p = prevAtom()->asScriptInset();
+ if (p && p->has(up)) {
+ --pos();
+ push(inset());
+ idx() = up; // the superscript has index 1
+ pos() = lastpos();
+ //lyxerr << "updown: handled by scriptinset to the left" << endl;
+ return true;
+ }
+ }
+
+ // try right
+ if (pos() != lastpos()) {
+ MathScriptInset const * p = nextAtom()->asScriptInset();
+ if (p && p->has(up)) {
+ push(inset());
+ idx() = up;
+ pos() = 0;
+ //lyxerr << "updown: handled by scriptinset to the right" << endl;
+ return true;
+ }
+ }
+ }
+
+ // try current cell for e.g. text insets
+ if (inset().idxUpDown2(*this, up))
+ return true;
+
+ //xarray().boundingBox(xlow, xhigh, ylow, yhigh);
+ //if (up)
+ // yhigh = yo - 4;
+ //else
+ // ylow = yo + 4;
+ //if (bruteFind(xo, yo, xlow, xhigh, ylow, yhigh)) {
+ // lyxerr << "updown: handled by brute find in the same cell" << endl;
+ // return true;
+ //}
+
+ // try to find an inset that knows better then we
+ while (1) {
+ //lyxerr << "updown: We are in " << &inset() << " idx: " << idx() << endl;
+ // ask inset first
+ if (inset().idxUpDown(*this, up)) {
+ // try to find best position within this inset
+ if (!selection())
+ bruteFind2(xo, yo);
+ return true;
+ }
+
+ // no such inset found, just take something "above"
+ //lyxerr << "updown: handled by strange case" << endl;
+ if (!popLeft()) {
+ return
+ bruteFind(xo, yo,
+ formula()->xlow(),
+ formula()->xhigh(),
+ up ? formula()->ylow() : yo + 4,
+ up ? yo - 4 : formula()->yhigh()
+ );
+ }
+
+ // any improvement so far?
+ int xnew, ynew;
+ getPos(xnew, ynew);
+ if (up ? ynew < yo : ynew > yo)
+ return true;
+ }
+}
+
+
+bool LCursor::bruteFind(int x, int y, int xlow, int xhigh, int ylow, int yhigh)
+{
+ DocumentIterator best_cursor;
+ double best_dist = 1e10;
+
+ DocumentIterator it = insetBegin(bv().buffer()->inset());
+ DocumentIterator et = insetEnd();
+ while (1) {
+ // avoid invalid nesting when selecting
+ if (!selection() || positionable(it, anchor_)) {
+ int xo, yo;
+ CursorSlice & cur = it.back();
+ cur.inset().getCursorPos(cur, xo, yo);
+ if (xlow <= xo && xo <= xhigh && ylow <= yo && yo <= yhigh) {
+ double d = (x - xo) * (x - xo) + (y - yo) * (y - yo);
+ //lyxerr << "x: " << x << " y: " << y << " d: " << endl;
+ // '<=' in order to take the last possible position
+ // this is important for clicking behind \sum in e.g. '\sum_i a'
+ if (d <= best_dist) {
+ best_dist = d;
+ best_cursor = it;
+ }
+ }
+ }
+
+ if (it == et)
+ break;
+ it.forwardPos();
+ }
+
+ if (best_dist < 1e10)
+ setCursor(best_cursor, false);
+ return best_dist < 1e10;
+}
+
+
+void LCursor::bruteFind2(int x, int y)
+{
+ double best_dist = 1e10;
+
+ DocumentIterator it = *this;
+ it.back().pos() = 0;
+ DocumentIterator et = *this;
+ et.back().pos() = et.back().asMathInset()->cell(et.back().idx_).size();
+ for (int i = 0; ; ++i) {
+ int xo, yo;
+ CursorSlice & cur = it.back();
+ cur.inset().getCursorPos(cur, xo, yo);
+ double d = (x - xo) * (x - xo) + (y - yo) * (y - yo);
+ // '<=' in order to take the last possible position
+ // this is important for clicking behind \sum in e.g. '\sum_i a'
+ lyxerr << "i: " << i << " d: " << d << " best: " << best_dist << endl;
+ if (d <= best_dist) {
+ best_dist = d;
+ setCursor(it, false);
+ }
+ if (it == et)
+ break;
+ it.forwardPos();
+ }
+}
+
+
+CursorSlice LCursor::normalAnchor()
+{
+ if (anchor_.size() < depth()) {
+ resetAnchor();
+ lyxerr << "unusual Anchor size" << endl;
+ }
+ //lyx::BOOST_ASSERT(Anchor_.size() >= cursor.depth());
+ // use Anchor on the same level as Cursor
+ CursorSlice normal = anchor_[size() - 1];
+#if 0
+ if (depth() < anchor_.size() && !(normal < xx())) {
+ // anchor is behind cursor -> move anchor behind the inset
+ ++normal.pos_;
+ }
+#endif
+ return normal;
+}
+
+
+void LCursor::handleFont(string const & font)
+{
+ lyxerr << "LCursor::handleFont: " << font << endl;
+ string safe;
+ if (selection()) {
+ macroModeClose();
+ safe = grabAndEraseSelection();
+ }
+
+ if (lastpos() != 0) {
+ // something left in the cell
+ if (pos() == 0) {
+ // cursor in first position
+ popLeft();
+ } else if (pos() == lastpos()) {
+ // cursor in last position
+ popRight();
+ } else {
+ // cursor in between. split cell
+ MathArray::iterator bt = cell().begin();
+ MathAtom at = createMathInset(font);
+ at.nucleus()->cell(0) = MathArray(bt, bt + pos());
+ cell().erase(bt, bt + pos());
+ popLeft();
+ plainInsert(at);
+ }
+ } else {
+ // nothing left in the cell
+ pullArg();
+ plainErase();
+ }
+ insert(safe);
+}
+
+
+void LCursor::message(string const & msg) const
+{
+ bv().owner()->getLyXFunc().setMessage(msg);
+}
+
+
+void LCursor::errorMessage(string const & msg) const
+{
+ bv().owner()->getLyXFunc().setErrorMessage(msg);
+}
+
+
+string LCursor::selectionAsString(bool label) const
+{
+ if (!selection())
+ return string();
+
+ if (inTexted()) {
+ Buffer const & buffer = *bv().buffer();
+
+ // should be const ...
+ ParagraphList::iterator startpit = text()->getPar(selBegin());
+ ParagraphList::iterator endpit = text()->getPar(selEnd());
+ size_t const startpos = selBegin().pos();
+ size_t const endpos = selEnd().pos();
+
+ if (startpit == endpit)
+ return startpit->asString(buffer, startpos, endpos, label);
+
+ // First paragraph in selection
+ string result =
+ startpit->asString(buffer, startpos, startpit->size(), label) + "\n\n";
+
+ // The paragraphs in between (if any)
+ ParagraphList::iterator pit = startpit;
+ for (++pit; pit != endpit; ++pit)
+ result += pit->asString(buffer, 0, pit->size(), label) + "\n\n";
+
+ // Last paragraph in selection
+ result += endpit->asString(buffer, 0, endpos, label);
+
+ return result;
+ }
+
+#warning and mathed?
+ return string();
+}
+
+
+string LCursor::currentState()
+{
+ if (inMathed()) {
+ std::ostringstream os;
+ info(os);
+ return os.str();
+ }
+
+ if (inTexted())
+ return text()->currentState(*this);
+
+ return string();
+}
+
+
+// only used by the spellchecker
+void LCursor::replaceWord(string const & replacestring)
+{
+ LyXText * t = text();
+ BOOST_ASSERT(t);
+
+ t->replaceSelectionWithString(*this, replacestring);
+ t->setSelectionRange(*this, replacestring.length());
+
+ // Go back so that replacement string is also spellchecked
+ for (string::size_type i = 0; i < replacestring.length() + 1; ++i)
+ t->cursorLeft(*this);
+}
+
+
+void LCursor::update()
+{
+ bv().update();
+}
+
+
+string LCursor::getPossibleLabel()
+{
+ return inMathed() ? "eq:" : text()->getPossibleLabel(*this);
+}
+
+
+Encoding const * LCursor::getEncoding() const
+{
+ if (empty())
+ return 0;
+ if (!bv().buffer())
+ return 0;
+ int s = 0;
+ // go up until first non-0 text is hit
+ // (innermost text is 0 in mathed)
+ for (s = size() - 1; s >= 0; --s)
+ if (operator[](s).text())
+ break;
+ CursorSlice const & sl = operator[](s);
+ LyXText & text = *sl.text();
+ ParagraphList::iterator pit = text.getPar(sl.par());
+ LyXFont font = pit->getFont(
+ bv().buffer()->params(), sl.pos(), outerFont(pit, text.paragraphs()));
+ return font.language()->encoding();
+}
+
+
+void LCursor::undispatched()
+{
+ disp_.dispatched(false);
+}
+
+
+void LCursor::noUpdate()
+{
+ disp_.update(false);