+ //xarray().boundingBox(xlow, xhigh, ylow, yhigh);
+ //if (up)
+ // yhigh = yo - 4;
+ //else
+ // ylow = yo + 4;
+ //if (bruteFind(*this, 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 (true) {
+ //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())
+ setCursor(bruteFind2(*this, xo, yo));
+ return true;
+ }
+
+ // no such inset found, just take something "above"
+ //lyxerr << "updown: handled by strange case" << endl;
+ if (!popLeft()) {
+ int ylow = up ? 0 : yo + 1;
+ int yhigh = up ? yo - 1 : bv().workHeight();
+ return bruteFind(*this, xo, yo, 0, bv().workWidth(), ylow, yhigh);
+ }
+
+ // any improvement so far?
+ int xnew;
+ int ynew;
+ getPos(xnew, ynew);
+ if (up ? ynew < yo : ynew > yo)
+ return true;
+ }
+
+ // we should not come here.
+ BOOST_ASSERT(false);
+}
+
+
+void LCursor::handleFont(string const & font)
+{
+ lyxerr[Debug::DEBUG] << BOOST_CURRENT_FUNCTION << ": " << font << endl;
+ string safe;
+ if (selection()) {
+ macroModeClose();
+ safe = lyx::cap::grabAndEraseSelection(*this);
+ }
+
+ 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();
+ ParagraphList & pars = text()->paragraphs();
+
+ // should be const ...
+ pit_type startpit = selBegin().pit();
+ pit_type endpit = selEnd().pit();
+ size_t const startpos = selBegin().pos();
+ size_t const endpos = selEnd().pos();
+
+ if (startpit == endpit)
+ return pars[startpit].asString(buffer, startpos, endpos, label);
+
+ // First paragraph in selection
+ string result = pars[startpit].
+ asString(buffer, startpos, pars[startpit].size(), label) + "\n\n";
+
+ // The paragraphs in between (if any)
+ for (pit_type pit = startpit + 1; pit != endpit; ++pit) {
+ Paragraph & par = pars[pit];
+ result += par.asString(buffer, 0, par.size(), label) + "\n\n";
+ }
+
+ // Last paragraph in selection
+ result += pars[endpit].asString(buffer, 0, endpos, label);
+
+ return result;
+ }
+
+#ifdef WITH_WARNINGS
+#warning and mathed?
+#endif
+ return string();
+}
+
+
+string LCursor::currentState()
+{
+ if (inMathed()) {
+ std::ostringstream os;
+ info(os);
+ return os.str();
+ }
+
+ if (inTexted())
+ return text()->currentState(*this);
+
+ return string();
+}
+
+
+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 = depth() - 1; s >= 0; --s)
+ if (operator[](s).text())
+ break;
+ CursorSlice const & sl = operator[](s);
+ LyXText const & text = *sl.text();
+ LyXFont font = text.getPar(sl.pit()).getFont(
+ bv().buffer()->params(), sl.pos(), outerFont(sl.pit(), text.paragraphs()));
+ return font.language()->encoding();
+}
+
+
+void LCursor::undispatched()
+{
+ disp_.dispatched(false);
+}
+
+
+void LCursor::dispatched()
+{
+ disp_.dispatched(true);
+}
+
+
+void LCursor::needsUpdate()
+{
+ disp_.update(true);
+}
+
+
+void LCursor::noUpdate()
+{
+ disp_.update(false);
+}
+
+
+LyXFont LCursor::getFont() const
+{
+ // HACK. far from being perfect...
+ int s = 0;
+ // go up until first non-0 text is hit
+ // (innermost text is 0 in mathed)
+ for (s = depth() - 1; s >= 0; --s)
+ if (operator[](s).text())
+ break;
+ CursorSlice const & sl = operator[](s);
+ LyXText const & text = *sl.text();
+ LyXFont font = text.getPar(sl.pit()).getFont(
+ bv().buffer()->params(),
+ sl.pos(),
+ outerFont(sl.pit(), text.paragraphs()));
+
+ return font;
+}
+
+
+void LCursor::fixIfBroken()
+{
+ // find out last good level
+ LCursor copy = *this;
+ size_t newdepth = depth();
+ while (!copy.empty()) {
+ if (copy.idx() > copy.lastidx()) {
+ lyxerr << "wrong idx " << copy.idx()
+ << ", max is " << copy.lastidx()
+ << " at level " << copy.depth()
+ << ". Trying to correct this." << endl;
+ newdepth = copy.depth() - 1;
+ }
+ else if (copy.pit() > copy.lastpit()) {
+ lyxerr << "wrong pit " << copy.pit()
+ << ", max is " << copy.lastpit()
+ << " at level " << copy.depth()
+ << ". Trying to correct this." << endl;
+ newdepth = copy.depth() - 1;
+ }
+ else if (copy.pos() > copy.lastpos()) {
+ lyxerr << "wrong pos " << copy.pos()
+ << ", max is " << copy.lastpos()
+ << " at level " << copy.depth()
+ << ". Trying to correct this." << endl;
+ newdepth = copy.depth() - 1;
+ }
+ copy.pop();
+ }
+ // shrink cursor to a size where everything is valid, possibly
+ // leaving insets
+ while (depth() > newdepth) {
+ pop();
+ lyxerr << "correcting cursor to level " << depth() << endl;