3 * This file is part of LyX, the document processor.
4 * Licence details can be found in the file COPYING.
6 * \author Alejandro Aguilar Sierra
7 * \author Alfredo Braunstein
10 * Full author contact details are available in file CREDITS.
16 #include "BufferView.h"
19 #include "dispatchresult.h"
20 #include "funcrequest.h"
21 #include "iterators.h"
26 #include "paragraph.h"
28 #include "insets/updatableinset.h"
29 #include "insets/insettabular.h"
30 #include "insets/insettext.h"
32 #include "mathed/math_data.h"
33 #include "mathed/math_hullinset.h"
34 #include "mathed/math_support.h"
36 #include "support/limited_stack.h"
38 #include <boost/assert.hpp>
43 #ifndef CXX_GLOBAL_CSTD
52 limited_stack<string> theCutBuffer;
55 LCursor::LCursor(BufferView & bv)
56 : cursor_(1), anchor_(1), bv_(&bv), current_(0),
57 cached_y_(0), x_target_(-1),
58 selection_(false), mark_(false)
66 cursor_.push_back(CursorSlice());
67 anchor_.push_back(CursorSlice());
76 DispatchResult LCursor::dispatch(FuncRequest const & cmd0)
78 //lyxerr << "\nLCursor::dispatch: cmd: " << cmd0 << endl << *this << endl;
79 FuncRequest cmd = cmd0;
81 for (int i = cursor_.size() - 1; i >= 1; --i) {
83 CursorSlice const & citem = cursor_[i];
84 lyxerr << "trying to dispatch to inset " << citem.inset_ << endl;
85 DispatchResult res = inset()->dispatch(*this, cmd);
86 if (res.dispatched()) {
87 lyxerr << " successfully dispatched to inset " << citem.inset_ << endl;
88 return DispatchResult(true, true);
90 // remove one level of cursor
94 cmd = FuncRequest(LFUN_FINISHED_LEFT);
98 cmd = FuncRequest(LFUN_FINISHED_RIGHT);
102 cmd = FuncRequest(LFUN_FINISHED_UP);
106 cmd = FuncRequest(LFUN_FINISHED_DOWN);
109 lyxerr << "not handled on level " << i << " val: " << res.val() << endl;
113 lyxerr << "trying to dispatch to main text " << bv_->text() << endl;
114 DispatchResult res = bv_->text()->dispatch(*this, cmd);
115 lyxerr << " result: " << res.val() << endl;
120 void LCursor::push(InsetBase * inset)
122 lyxerr << "LCursor::push() inset: " << inset << endl;
123 cursor_.push_back(CursorSlice(inset));
124 anchor_.push_back(CursorSlice(inset));
130 void LCursor::pop(int depth)
132 //lyxerr << "LCursor::pop() to depth " << depth << endl;
133 while (int(cursor_.size()) > depth)
140 BOOST_ASSERT(!cursor_.empty());
141 //lyxerr << "LCursor::pop() a level" << endl;
142 if (cursor_.size() <= 1)
143 lyxerr << "### TRYING TO POP FROM EMPTY CURSOR" << endl;
147 current_ = cursor_.size() - 1;
149 //lyxerr << "LCursor::pop() current now: " << current_ << endl;
153 void LCursor::pushLeft(InsetBase * p)
155 BOOST_ASSERT(!cursor_.empty());
156 //lyxerr << "Entering inset " << t << " left" << endl;
162 bool LCursor::popLeft()
164 BOOST_ASSERT(!cursor_.empty());
165 //lyxerr << "Leaving inset to the left" << endl;
168 inset()->notifyCursorLeaves(idx());
171 inset()->notifyCursorLeaves(idx());
177 bool LCursor::popRight()
179 BOOST_ASSERT(!cursor_.empty());
180 //lyxerr << "Leaving inset to the right" << endl;
183 inset()->notifyCursorLeaves(idx());
186 inset()->notifyCursorLeaves(idx());
193 CursorSlice & LCursor::current()
195 BOOST_ASSERT(!cursor_.empty());
196 //lyxerr << "accessing cursor slice " << current_
197 // << ": " << cursor_[current_] << endl;
198 return cursor_[current_];
202 CursorSlice const & LCursor::current() const
204 //lyxerr << "accessing cursor slice " << current_
205 // << ": " << cursor_[current_] << endl;
206 return cursor_[current_];
210 int LCursor::currentMode()
212 BOOST_ASSERT(!cursor_.empty());
213 for (int i = cursor_.size() - 1; i >= 1; --i) {
214 int res = cursor_[i].inset()->currentMode();
215 if (res != MathInset::UNDECIDED_MODE)
218 return MathInset::TEXT_MODE;
222 LyXText * LCursor::innerText() const
224 BOOST_ASSERT(!cursor_.empty());
225 //lyxerr << "LCursor::innerText() depth: " << cursor_.size() << endl;
226 if (cursor_.size() > 1) {
227 // go up until first non-0 text is hit
228 // (innermost text is 0 in mathed)
229 for (int i = cursor_.size() - 1; i >= 1; --i)
230 if (cursor_[i].text())
231 return cursor_[i].text();
237 CursorSlice const & LCursor::innerTextSlice() const
239 BOOST_ASSERT(!cursor_.empty());
240 //lyxerr << "LCursor::innerTextSlice() depth: " << cursor_.size() << endl;
241 if (cursor_.size() > 1) {
242 // go up until first non-0 text is hit
243 // (innermost text is 0 in mathed)
244 for (int i = cursor_.size() - 1; i >= 1; --i)
245 if (cursor_[i].text())
252 void LCursor::updatePos()
254 BOOST_ASSERT(!cursor_.empty());
255 if (cursor_.size() > 1)
256 cached_y_ = bv_->top_y() + cursor_.back().inset()->yo();
257 //cached_y_ = cursor_.back().inset()->yo();
261 void LCursor::getDim(int & asc, int & des) const
263 BOOST_ASSERT(!cursor_.empty());
264 LyXText * text = innerText();
265 #warning crashes with text-in-math
267 RowList::iterator const rit = text->cursorRow();
268 if (rit != text->endRow()) {
269 asc = rit->baseline();
270 des = rit->height() - asc;
278 //innerInset()->getCursorDim(asc, des);
283 void LCursor::getPos(int & x, int & y) const
285 BOOST_ASSERT(!cursor_.empty());
288 if (cursor_.size() <= 1) {
289 x = bv_->text()->cursorX(cursor_.front());
290 y = bv_->text()->cursorY(cursor_.front());
292 inset()->getCursorPos(cursor_.back(), x, y);
293 // getCursorPos gives _screen_ coordinates. We need to add
294 // top_y to get document coordinates. This is hidden in cached_y_.
295 //y += cached_y_ - inset()->yo();
296 // The rest is non-obvious. The reason we have to have these
297 // extra computation is that the getCursorPos() calls rely
298 // on the inset's own knowledge of its screen position.
299 // If we scroll up or down in a big enough increment,
300 // inset->draw() is not called: this doesn't update
301 // inset.yo_, so getCursor() returns an old value.
304 lyxerr << "#### LCursor::getPos: x: " << x << " y: " << y << endl;
308 void LCursor::paste(string const & data)
310 dispatch(FuncRequest(LFUN_PASTE, data));
314 InsetBase * LCursor::innerInsetOfType(int code) const
316 for (int i = cursor_.size() - 1; i >= 1; --i)
317 if (cursor_[i].inset_->lyxCode() == code)
318 return cursor_[i].inset_;
323 InsetTabular * LCursor::innerInsetTabular() const
325 return static_cast<InsetTabular *>(innerInsetOfType(InsetBase::TABULAR_CODE));
329 void LCursor::resetAnchor()
335 BufferView & LCursor::bv() const
341 MathAtom const & LCursor::prevAtom() const
343 BOOST_ASSERT(pos() > 0);
344 return cell()[pos() - 1];
348 MathAtom & LCursor::prevAtom()
350 BOOST_ASSERT(pos() > 0);
351 return cell()[pos() - 1];
355 MathAtom const & LCursor::nextAtom() const
357 BOOST_ASSERT(pos() < lastpos());
358 return cell()[pos()];
362 MathAtom & LCursor::nextAtom()
364 BOOST_ASSERT(pos() < lastpos());
365 return cell()[pos()];
369 bool LCursor::posLeft()
378 bool LCursor::posRight()
380 if (pos() == lastpos())
387 CursorSlice & LCursor::anchor()
389 return anchor_.back();
393 CursorSlice const & LCursor::anchor() const
395 return anchor_.back();
399 CursorSlice const & LCursor::selBegin() const
402 return cursor_.back();
403 // can't use std::min as this creates a new object
404 return anchor() < cursor_.back() ? anchor() : cursor_.back();
408 CursorSlice & LCursor::selBegin()
411 return cursor_.back();
412 return anchor() < cursor_.back() ? anchor() : cursor_.back();
416 CursorSlice const & LCursor::selEnd() const
419 return cursor_.back();
420 return anchor() > cursor_.back() ? anchor() : cursor_.back();
424 CursorSlice & LCursor::selEnd()
427 return cursor_.back();
428 return anchor() > cursor_.back() ? anchor() : cursor_.back();
432 void LCursor::setSelection()
435 // a selection with no contents is not a selection
436 if (par() == anchor().par() && pos() == anchor().pos())
441 void LCursor::setSelection(CursorBase const & where, size_t n)
450 void LCursor::clearSelection()
459 int & LCursor::x_target()
465 int LCursor::x_target() const
471 void LCursor::clearTargetX()
477 Paragraph & LCursor::paragraph()
479 return current_ ? current().paragraph() : *bv_->text()->getPar(par());
483 Paragraph const & LCursor::paragraph() const
485 return current_ ? current().paragraph() : *bv_->text()->getPar(par());
489 LCursor::pos_type LCursor::lastpos() const
491 InsetBase * inset = current().inset();
492 return inset && inset->asMathInset() ? cell().size() : paragraph().size();
496 size_t LCursor::nargs() const
498 // assume 1x1 grid for 'plain text'
499 return current_ ? current().nargs() : 1;
503 size_t LCursor::ncols() const
505 // assume 1x1 grid for 'plain text'
506 return current_ ? current().ncols() : 1;
510 size_t LCursor::nrows() const
512 // assume 1x1 grid for 'plain text'
513 return current_ ? current().nrows() : 1;
517 LCursor::row_type LCursor::row() const
519 BOOST_ASSERT(current_ > 0);
520 return current().row();
524 LCursor::col_type LCursor::col() const
526 BOOST_ASSERT(current_ > 0);
527 return current().col();
531 MathArray const & LCursor::cell() const
533 BOOST_ASSERT(current_ > 0);
534 return current().cell();
538 MathArray & LCursor::cell()
540 BOOST_ASSERT(current_ > 0);
541 return current().cell();
545 void LCursor::info(std::ostream & os)
547 for (int i = 1, n = depth(); i < n; ++i) {
548 cursor_[i].inset()->infoize(os);
553 // prevAtom()->infoize2(os);
554 // overwite old message
561 void region(CursorSlice const & i1, CursorSlice const & i2,
562 LCursor::row_type & r1, LCursor::row_type & r2,
563 LCursor::col_type & c1, LCursor::col_type & c2)
565 InsetBase * p = i1.inset();
566 c1 = p->col(i1.idx_);
567 c2 = p->col(i2.idx_);
570 r1 = p->row(i1.idx_);
571 r2 = p->row(i2.idx_);
579 string LCursor::grabSelection()
584 CursorSlice i1 = selBegin();
585 CursorSlice i2 = selEnd();
587 if (i1.idx_ == i2.idx_) {
588 if (i1.inset()->asMathInset()) {
589 MathArray::const_iterator it = i1.cell().begin();
590 return asString(MathArray(it + i1.pos_, it + i2.pos_));
592 return "unknown selection 1";
598 region(i1, i2, r1, r2, c1, c2);
601 if (i1.inset()->asMathInset()) {
602 for (row_type row = r1; row <= r2; ++row) {
605 for (col_type col = c1; col <= c2; ++col) {
608 data += asString(i1.asMathInset()->cell(i1.asMathInset()->index(row, col)));
612 data = "unknown selection 2";
618 void LCursor::eraseSelection()
620 CursorSlice i1 = selBegin();
621 CursorSlice i2 = selEnd();
623 if (i1.inset()->asMathInset()) {
624 if (i1.idx_ == i2.idx_) {
625 i1.cell().erase(i1.pos_, i2.pos_);
627 MathInset * p = i1.asMathInset();
630 region(i1, i2, r1, r2, c1, c2);
631 for (row_type row = r1; row <= r2; ++row)
632 for (col_type col = c1; col <= c2; ++col)
633 p->cell(p->index(row, col)).clear();
637 lyxerr << "can't erase this selection 1" << endl;
642 string LCursor::grabAndEraseSelection()
646 string res = grabSelection();
653 void LCursor::selClear()
660 void LCursor::selCopy()
663 theCutBuffer.push(grabSelection());
666 //theCutBuffer.erase();
671 void LCursor::selCut()
673 theCutBuffer.push(grabAndEraseSelection());
677 void LCursor::selDel()
686 void LCursor::selPaste(size_t n)
689 if (n < theCutBuffer.size())
690 paste(theCutBuffer[n]);
696 void LCursor::selHandle(bool sel)
698 if (sel == selection())
705 void LCursor::selStart()
712 void LCursor::selClearOrDel()
714 if (lyxrc.auto_region_delete)
721 std::ostream & operator<<(std::ostream & os, LCursor const & cur)
724 for (size_t i = 0, n = cur.cursor_.size(); i != n; ++i)
725 os << " (" << cur.cursor_[i] << " | " << cur.anchor_[i] << "\n";
737 void increment(CursorBase & it)
739 CursorSlice & top = it.back();
740 MathArray & ar = top.asMathInset()->cell(top.idx_);
742 // move into the current inset if possible
743 // it is impossible for pos() == size()!
745 if (top.pos() != top.lastpos())
746 n = (ar.begin() + top.pos_)->nucleus();
747 if (n && n->isActive()) {
748 it.push_back(CursorSlice(n));
752 // otherwise move on one cell back if possible
753 if (top.pos() < top.lastpos()) {
754 // pos() == lastpos() is valid!
759 // otherwise try to move on one cell if possible
760 while (top.idx() < top.lastidx()) {
762 if (top.asMathInset()->validCell(top.idx_)) {
768 // otherwise leave array, move on one back
769 // this might yield pos() == size(), but that's a ok.
771 // it certainly invalidates top
776 CursorBase ibegin(InsetBase * p)
779 it.push_back(CursorSlice(p));
784 CursorBase iend(InsetBase * p)
787 it.push_back(CursorSlice(p));
788 CursorSlice & cur = it.back();
789 cur.idx() = cur.lastidx();
790 cur.pos() = cur.lastpos();
797 ///////////////////////////////////////////////////////////////////
799 // The part below is the non-integrated rest of the original math
800 // cursor. This should be either generalized for texted or moved
801 // back to the math insets.
803 ///////////////////////////////////////////////////////////////////
805 #include "mathed/math_braceinset.h"
806 #include "mathed/math_charinset.h"
807 #include "mathed/math_commentinset.h"
808 #include "mathed/math_factory.h"
809 #include "mathed/math_gridinset.h"
810 #include "mathed/math_macroarg.h"
811 #include "mathed/math_macrotemplate.h"
812 #include "mathed/math_mathmlstream.h"
813 #include "mathed/math_scriptinset.h"
814 #include "mathed/math_spaceinset.h"
815 #include "mathed/math_support.h"
816 #include "mathed/math_unknowninset.h"
818 //#define FILEDEBUG 1
821 bool LCursor::isInside(InsetBase const * p)
823 for (unsigned i = 0; i < depth(); ++i)
824 if (cursor_[i].inset() == p)
830 bool LCursor::openable(MathAtom const & t)
841 // we can't move into anything new during selection
842 if (depth() == anchor_.size())
844 if (t.nucleus() != anchor_[depth()].inset())
851 bool LCursor::inNucleus()
853 return inset()->asMathInset()->asScriptInset() && idx() == 2;
859 autocorrect() = false;
866 if (pos() != 0 && openable(prevAtom())) {
868 push(nextAtom().nucleus());
869 inset()->idxLast(*this);
873 return posLeft() || idxLeft() || popLeft() || selection();
877 bool LCursor::right()
879 autocorrect() = false;
886 if (pos() != lastpos() && openable(nextAtom())) {
887 pushLeft(nextAtom().nucleus());
888 inset()->idxFirst(*this);
892 return posRight() || idxRight() || popRight() || selection();
896 bool positionable(CursorBase const & cursor, CursorBase const & anchor)
898 // avoid deeper nested insets when selecting
899 if (cursor.size() > anchor.size())
902 // anchor might be deeper, should have same path then
903 for (size_t i = 0; i < cursor.size(); ++i)
904 if (cursor[i].inset() != anchor[i].inset())
907 // position should be ok.
912 void LCursor::setScreenPos(int x, int y)
914 bool res = bruteFind(x, y, formula()->xlow(), formula()->xhigh(),
915 formula()->ylow(), formula()->yhigh());
917 // this can happen on creation of "math-display"
928 autocorrect() = false;
930 if (!inset()->idxHome(*this))
939 autocorrect() = false;
941 if (!inset()->idxEnd(*this))
948 void LCursor::plainErase()
954 void LCursor::markInsert()
956 cell().insert(pos(), MathAtom(new MathCharInset(0)));
960 void LCursor::markErase()
966 void LCursor::plainInsert(MathAtom const & t)
968 cell().insert(pos(), t);
973 void LCursor::insert2(string const & str)
981 void LCursor::insert(string const & str)
983 lyxerr << "inserting '" << str << "'" << endl;
985 for (string::const_iterator it = str.begin(); it != str.end(); ++it)
986 plainInsert(MathAtom(new MathCharInset(*it)));
990 void LCursor::insert(char c)
992 lyxerr << "inserting '" << c << "'" << endl;
994 plainInsert(MathAtom(new MathCharInset(c)));
998 void LCursor::insert(MathAtom const & t)
1006 void LCursor::niceInsert(string const & t)
1008 lyxerr << "*** LCursor::niceInsert 1: " << t << endl;
1018 void LCursor::niceInsert(MathAtom const & t)
1020 lyxerr << "*** LCursor::niceInsert 2: " << t << endl;
1022 string safe = grabAndEraseSelection();
1024 // enter the new inset and move the contents of the selection if possible
1025 if (t->isActive()) {
1027 pushLeft(nextAtom().nucleus());
1030 lyxerr << "*** LCursor::niceInsert 3: " << t << endl;
1034 void LCursor::insert(MathArray const & ar)
1039 cell().insert(pos(), ar);
1044 bool LCursor::backspace()
1046 autocorrect() = false;
1054 if (inset()->nargs() == 1 && depth() == 1 && lastpos() == 0)
1060 if (inMacroMode()) {
1061 MathUnknownInset * p = activeMacro();
1062 if (p->name().size() > 1) {
1063 p->setName(p->name().substr(0, p->name().size() - 1));
1068 if (pos() != 0 && prevAtom()->nargs() > 0) {
1069 // let's require two backspaces for 'big stuff' and
1070 // highlight on the first
1081 bool LCursor::erase()
1083 autocorrect() = false;
1092 // delete empty cells if possible
1094 //if (cell().empty() && inset()->idxDelete(idx()))
1097 // special behaviour when in last position of cell
1098 if (pos() == lastpos()) {
1099 bool one_cell = inset()->nargs() == 1;
1100 if (one_cell && depth() == 1 && lastpos() == 0)
1106 inset()->idxGlue(idx());
1110 if (pos() != lastpos() && inset()->nargs() > 0) {
1124 CursorBase save = cursor_;
1128 autocorrect() = false;
1133 bool LCursor::down()
1136 CursorBase save = cursor_;
1137 if (goUpDown(false))
1140 autocorrect() = false;
1145 void LCursor::macroModeClose()
1149 MathUnknownInset * p = activeMacro();
1151 string s = p->name();
1153 cell().erase(pos());
1155 // do nothing if the macro name is empty
1159 string const name = s.substr(1);
1161 // prevent entering of recursive macros
1162 if (formula()->lyxCode() == InsetOld::MATHMACRO_CODE
1163 && formula()->getInsetName() == name)
1164 lyxerr << "can't enter recursive macro" << endl;
1166 niceInsert(createMathInset(name));
1170 string LCursor::macroName()
1172 return inMacroMode() ? activeMacro()->name() : string();
1176 void LCursor::drawSelection(PainterInfo & pi)
1180 CursorSlice i1 = selBegin();
1181 CursorSlice i2 = selEnd();
1182 i1.asMathInset()->drawSelection(pi, i1.idx_, i1.pos_, i2.idx_, i2.pos_);
1186 void LCursor::handleNest(MathAtom const & a, int c)
1189 asArray(grabAndEraseSelection(), t.nucleus()->cell(c));
1192 pushLeft(t.nucleus());
1196 int LCursor::targetX() const
1198 if (x_target() != -1)
1207 MathHullInset * LCursor::formula() const
1209 for (int i = cursor_.size() - 1; i >= 1; --i)
1210 if (cursor_[i].inset()->lyxCode() == InsetBase::MATH_CODE)
1211 return static_cast<MathHullInset *>(cursor_[i].inset());
1216 void LCursor::adjust(pos_type from, int diff)
1220 if (anchor().pos_ > from)
1221 anchor().pos_ += diff;
1222 // just to be on the safe side
1223 // theoretically unecessary
1228 bool LCursor::inMacroMode() const
1232 MathUnknownInset const * p = prevAtom()->asUnknownInset();
1233 return p && !p->final();
1237 MathUnknownInset * LCursor::activeMacro()
1239 return inMacroMode() ? prevAtom().nucleus()->asUnknownInset() : 0;
1243 bool LCursor::inMacroArgMode() const
1245 return pos() > 0 && prevAtom()->getChar() == '#';
1249 MathGridInset * LCursor::enclosingGrid(idx_type & idx) const
1251 for (MathInset::difference_type i = depth() - 1; i >= 0; --i) {
1252 MathInset * m = cursor_[i].inset()->asMathInset();
1255 MathGridInset * p = m->asGridInset();
1257 idx = cursor_[i].idx_;
1265 void LCursor::pullArg()
1269 MathArray ar = cell();
1272 cell().insert(pos(), ar);
1275 formula()->mutateToText();
1281 void LCursor::touch()
1285 CursorBase::const_iterator it = cursor_.begin();
1286 CursorBase::const_iterator et = cursor_.end();
1287 for ( ; it != et; ++it)
1293 void LCursor::normalize()
1295 if (idx() >= nargs()) {
1296 lyxerr << "this should not really happen - 1: "
1297 << idx() << ' ' << nargs()
1298 << " in: " << inset() << endl;
1300 idx() = min(idx(), nargs() - 1);
1302 if (pos() > lastpos()) {
1303 lyxerr << "this should not really happen - 2: "
1304 << pos() << ' ' << lastpos() << " in idx: " << idx()
1306 WriteStream wi(lyxerr, false, true);
1307 inset()->asMathInset()->write(wi);
1310 pos() = min(pos(), lastpos());
1314 char LCursor::valign()
1317 MathGridInset * p = enclosingGrid(idx);
1318 return p ? p->valign() : '\0';
1322 char LCursor::halign()
1325 MathGridInset * p = enclosingGrid(idx);
1326 return p ? p->halign(idx % p->ncols()) : '\0';
1330 bool LCursor::goUpDown(bool up)
1332 // Be warned: The 'logic' implemented in this function is highly fragile.
1333 // A distance of one pixel or a '<' vs '<=' _really_ matters.
1334 // So fiddle around with it only if you know what you are doing!
1339 // check if we had something else in mind, if not, this is the future goal
1340 if (x_target() == -1)
1345 // try neigbouring script insets
1349 MathScriptInset const * p = prevAtom()->asScriptInset();
1350 if (p && p->has(up)) {
1353 idx() = up; // the superscript has index 1
1355 //lyxerr << "updown: handled by scriptinset to the left" << endl;
1361 if (pos() != lastpos()) {
1362 MathScriptInset const * p = nextAtom()->asScriptInset();
1363 if (p && p->has(up)) {
1367 //lyxerr << "updown: handled by scriptinset to the right" << endl;
1373 // try current cell for e.g. text insets
1374 if (inset()->idxUpDown2(*this, up))
1377 //xarray().boundingBox(xlow, xhigh, ylow, yhigh);
1382 //if (bruteFind(xo, yo, xlow, xhigh, ylow, yhigh)) {
1383 // lyxerr << "updown: handled by brute find in the same cell" << endl;
1387 // try to find an inset that knows better then we
1389 //lyxerr << "updown: We are in " << inset() << " idx: " << idx() << endl;
1391 if (inset()->idxUpDown(*this, up)) {
1392 // try to find best position within this inset
1398 // no such inset found, just take something "above"
1399 //lyxerr << "updown: handled by strange case" << endl;
1405 up ? formula()->ylow() : yo + 4,
1406 up ? yo - 4 : formula()->yhigh()
1410 // any improvement so far?
1413 if (up ? ynew < yo : ynew > yo)
1419 bool LCursor::bruteFind(int x, int y, int xlow, int xhigh, int ylow, int yhigh)
1421 CursorBase best_cursor;
1422 double best_dist = 1e10;
1424 CursorBase it = ibegin(formula());
1425 CursorBase et = iend(formula());
1427 // avoid invalid nesting when selecting
1428 if (!selection() || positionable(it, anchor_)) {
1430 CursorSlice & cur = it.back();
1431 cur.inset()->getCursorPos(cur, xo, yo);
1432 if (xlow <= xo && xo <= xhigh && ylow <= yo && yo <= yhigh) {
1433 double d = (x - xo) * (x - xo) + (y - yo) * (y - yo);
1434 //lyxerr << "x: " << x << " y: " << y << " d: " << endl;
1435 // '<=' in order to take the last possible position
1436 // this is important for clicking behind \sum in e.g. '\sum_i a'
1437 if (d <= best_dist) {
1449 if (best_dist < 1e10)
1450 cursor_ = best_cursor;
1451 return best_dist < 1e10;
1455 void LCursor::bruteFind2(int x, int y)
1457 double best_dist = 1e10;
1459 CursorBase it = cursor_;
1461 CursorBase et = cursor_;
1462 int n = et.back().asMathInset()->cell(et.back().idx_).size();
1464 for (int i = 0; ; ++i) {
1466 CursorSlice & cur = it.back();
1467 cur.inset()->getCursorPos(cur, xo, yo);
1468 double d = (x - xo) * (x - xo) + (y - yo) * (y - yo);
1469 // '<=' in order to take the last possible position
1470 // this is important for clicking behind \sum in e.g. '\sum_i a'
1471 lyxerr << "i: " << i << " d: " << d << " best: " << best_dist << endl;
1472 if (d <= best_dist) {
1483 bool LCursor::idxLineLast()
1485 idx() -= idx() % ncols();
1486 idx() += ncols() - 1;
1492 bool LCursor::idxLeft()
1494 return inset()->idxLeft(*this);
1498 bool LCursor::idxRight()
1500 return inset()->idxRight(*this);
1504 bool LCursor::script(bool up)
1506 // Hack to get \\^ and \\_ working
1507 if (inMacroMode() && macroName() == "\\") {
1509 niceInsert(createMathInset("mathcircumflex"));
1516 string safe = grabAndEraseSelection();
1518 // we are in a nucleus of a script inset, move to _our_ script
1519 inset()->asMathInset()->asScriptInset()->ensure(up);
1522 } else if (pos() != 0 && prevAtom()->asScriptInset()) {
1523 prevAtom().nucleus()->asScriptInset()->ensure(up);
1528 } else if (pos() != 0) {
1531 = MathAtom(new MathScriptInset(nextAtom(), up));
1536 plainInsert(MathAtom(new MathScriptInset(up)));
1538 nextAtom().nucleus()->asScriptInset()->ensure(up);
1548 bool LCursor::interpret(char c)
1550 //lyxerr << "interpret 2: '" << c << "'" << endl;
1552 if (inMacroArgMode()) {
1558 MathMacroTemplate const * p = formula()->asMacroTemplate();
1559 if (p && 1 <= n && n <= p->numargs())
1560 insert(MathAtom(new MathMacroArgument(c - '0')));
1562 insert(createMathInset("#"));
1563 interpret(c); // try again
1570 if (inMacroMode()) {
1571 string name = macroName();
1572 //lyxerr << "interpret name: '" << name << "'" << endl;
1575 activeMacro()->setName(activeMacro()->name() + c);
1579 // handle 'special char' macros
1584 if (currentMode() == MathInset::TEXT_MODE)
1585 niceInsert(createMathInset("textbackslash"));
1587 niceInsert(createMathInset("backslash"));
1588 } else if (c == '{') {
1589 niceInsert(MathAtom(new MathBraceInset));
1591 niceInsert(createMathInset(string(1, c)));
1596 // leave macro mode and try again if necessary
1599 niceInsert(MathAtom(new MathBraceInset));
1605 // This is annoying as one has to press <space> far too often.
1609 // leave autocorrect mode if necessary
1610 if (autocorrect() && c == ' ') {
1611 autocorrect() = false;
1616 // just clear selection on pressing the space bar
1617 if (selection() && c == ' ') {
1618 selection() = false;
1625 //lyxerr << "starting with macro" << endl;
1626 insert(MathAtom(new MathUnknownInset("\\", false)));
1631 if (currentMode() == MathInset::TEXT_MODE)
1637 if (currentMode() == MathInset::TEXT_MODE) {
1638 // insert spaces in text mode,
1639 // but suppress direct insertion of two spaces in a row
1640 // the still allows typing '<space>a<space>' and deleting the 'a', but
1641 // it is better than nothing...
1642 if (!pos() != 0 || prevAtom()->getChar() != ' ')
1646 if (pos() != 0 && prevAtom()->asSpaceInset()) {
1647 prevAtom().nucleus()->asSpaceInset()->incSpace();
1652 // if are at the very end, leave the formula
1653 return pos() != lastpos();
1666 if (c == '{' || c == '}' || c == '#' || c == '&' || c == '$') {
1667 niceInsert(createMathInset(string(1, c)));
1672 niceInsert(MathAtom(new MathCommentInset));
1676 // try auto-correction
1677 //if (autocorrect() && hasPrevAtom() && math_autocorrect(prevAtom(), c))
1680 // no special circumstances, so insert the character without any fuss
1682 autocorrect() = true;
1687 void LCursor::lockToggle()
1689 if (pos() != lastpos()) {
1690 // toggle previous inset ...
1691 nextAtom().nucleus()->lock(!nextAtom()->lock());
1692 } else if (popLeft() && pos() != lastpos()) {
1693 // ... or enclosing inset if we are in the last inset position
1694 nextAtom().nucleus()->lock(!nextAtom()->lock());
1700 CursorSlice LCursor::normalAnchor()
1702 if (anchor_.size() < depth()) {
1704 lyxerr << "unusual Anchor size" << endl;
1706 //lyx::BOOST_ASSERT(Anchor_.size() >= cursor.depth());
1707 // use Anchor on the same level as Cursor
1708 CursorSlice normal = anchor_[depth() - 1];
1710 if (depth() < anchor_.size() && !(normal < xx())) {
1711 // anchor is behind cursor -> move anchor behind the inset
1720 DispatchResult dispatch(LCursor & cur, FuncRequest const & cmd)
1722 // mouse clicks are somewhat special
1724 switch (cmd.action) {
1725 case LFUN_MOUSE_PRESS:
1726 case LFUN_MOUSE_MOTION:
1727 case LFUN_MOUSE_RELEASE:
1728 case LFUN_MOUSE_DOUBLE: {
1729 CursorSlice & pos = cursor_.back();
1733 if (x < cmd.x && pos() != 0) {
1734 DispatchResult const res = prevAtom().nucleus()->dispatch(cmd);
1735 if (res.dispatched())
1738 if (x > cmd.x && pos() != lastpos()) {
1739 DispatchResult const res = inset()->dispatch(cmd);
1740 if (res.dispatched())
1751 void LCursor::handleFont(string const & font)
1756 safe = grabAndEraseSelection();
1759 if (lastpos() != 0) {
1760 // something left in the cell
1762 // cursor in first position
1764 } else if (pos() == lastpos()) {
1765 // cursor in last position
1768 // cursor in between. split cell
1769 MathArray::iterator bt = cell().begin();
1770 MathAtom at = createMathInset(font);
1771 at.nucleus()->cell(0) = MathArray(bt, bt + pos());
1772 cell().erase(bt, bt + pos());
1777 // nothing left in the cell
1785 void LCursor::releaseMathCursor()
1788 formula()->insetUnlock(bv());
1792 bool LCursor::inMathed() const