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: " << *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;
117 if (!res.dispatched()) {
118 lyxerr << "trying to dispatch to bv " << bv_ << endl;
119 bool sucess = bv_->dispatch(cmd);
120 lyxerr << " result: " << sucess << endl;
121 res.dispatched(sucess);
128 void LCursor::push(InsetBase * inset)
130 lyxerr << "LCursor::push() inset: " << inset << endl;
131 cursor_.push_back(CursorSlice(inset));
132 anchor_.push_back(CursorSlice(inset));
138 void LCursor::pop(int depth)
140 lyxerr << "LCursor::pop() to depth " << depth << endl;
141 while (int(cursor_.size()) > depth)
148 lyxerr << "LCursor::pop() a level" << endl;
149 //BOOST_ASSERT(!cursor_.empty());
150 if (cursor_.size() <= 1)
151 lyxerr << "### TRYING TO POP FROM EMPTY CURSOR" << endl;
155 current_ = cursor_.size() - 1;
157 lyxerr << "LCursor::pop() current now: " << current_ << endl;
161 void LCursor::pushLeft(InsetBase * p)
163 //lyxerr << "Entering inset " << t << " left" << endl;
169 bool LCursor::popLeft()
171 //lyxerr << "Leaving inset to the left" << endl;
174 inset()->notifyCursorLeaves(idx());
177 inset()->notifyCursorLeaves(idx());
183 bool LCursor::popRight()
185 //lyxerr << "Leaving inset to the right" << endl;
188 inset()->notifyCursorLeaves(idx());
191 inset()->notifyCursorLeaves(idx());
198 CursorSlice & LCursor::current()
200 //lyxerr << "accessing cursor slice " << current_
201 // << ": " << cursor_[current_] << endl;
202 return cursor_[current_];
206 CursorSlice const & LCursor::current() const
208 //lyxerr << "accessing cursor slice " << current_
209 // << ": " << cursor_[current_] << endl;
210 return cursor_[current_];
214 int LCursor::currentMode()
216 for (int i = cursor_.size() - 1; i >= 1; --i) {
217 int res = cursor_[i].inset()->currentMode();
218 if (res != MathInset::UNDECIDED_MODE)
221 return MathInset::TEXT_MODE;
225 LyXText * LCursor::innerText() const
227 //lyxerr << "LCursor::innerText() depth: " << cursor_.size() << endl;
228 if (cursor_.size() > 1) {
229 // go up until first non-0 text is hit
230 // (innermost text is 0 in mathed)
231 for (int i = cursor_.size() - 1; i >= 1; --i)
232 if (cursor_[i].text())
233 return cursor_[i].text();
239 CursorSlice const & LCursor::innerTextSlice() const
241 //lyxerr << "LCursor::innerTextSlice() depth: " << cursor_.size() << endl;
242 if (cursor_.size() > 1) {
243 // go up until first non-0 text is hit
244 // (innermost text is 0 in mathed)
245 for (int i = cursor_.size() - 1; i >= 1; --i)
246 if (cursor_[i].text())
253 void LCursor::updatePos()
255 if (cursor_.size() > 1)
256 cached_y_ = bv_->top_y() + cursor_.back().inset()->y();
260 void LCursor::getDim(int & asc, int & desc) const
262 LyXText * txt = innerText();
265 Row const & row = *txt->cursorRow();
266 asc = row.baseline();
267 desc = row.height() - asc;
271 //innerInset()->getCursorDim(asc, desc);
276 void LCursor::getPos(int & x, int & y) const
278 if (cursor_.size() <= 1) {
279 x = bv_->text()->cursorX();
280 y = bv_->text()->cursorY();
281 // y -= bv_->top_y();
283 if (inset()->asUpdatableInset()) {
284 // Would be nice to clean this up to make some understandable sense...
285 // Non-obvious. The reason we have to have these
286 // extra checks is that the ->getCursor() calls rely
287 // on the inset's own knowledge of its screen position.
288 // If we scroll up or down in a big enough increment, the
289 // inset->draw() is not called: this doesn't update
290 // inset.top_baseline, so getCursor() returns an old value.
292 inset()->asUpdatableInset()->getCursorPos(cursor_.back().idx_, x, y);
293 x += inset()->asUpdatableInset()->x();
295 } else if (inset()->asMathInset()) {
298 // this should not happen
305 void LCursor::paste(string const & data)
307 dispatch(FuncRequest(LFUN_PASTE, data));
311 InsetBase * LCursor::innerInsetOfType(int code) const
313 for (int i = cursor_.size() - 1; i >= 1; --i)
314 if (cursor_[i].inset_->lyxCode() == code)
315 return cursor_[i].inset_;
320 InsetTabular * LCursor::innerInsetTabular() const
322 return static_cast<InsetTabular *>(innerInsetOfType(InsetBase::TABULAR_CODE));
326 void LCursor::resetAnchor()
332 BufferView & LCursor::bv() const
338 MathAtom const & LCursor::prevAtom() const
340 BOOST_ASSERT(pos() > 0);
341 return cell()[pos() - 1];
345 MathAtom & LCursor::prevAtom()
347 BOOST_ASSERT(pos() > 0);
348 return cell()[pos() - 1];
352 MathAtom const & LCursor::nextAtom() const
354 BOOST_ASSERT(pos() < lastpos());
355 return cell()[pos()];
359 MathAtom & LCursor::nextAtom()
361 BOOST_ASSERT(pos() < lastpos());
362 return cell()[pos()];
366 bool LCursor::posLeft()
375 bool LCursor::posRight()
377 if (pos() == lastpos())
384 CursorSlice & LCursor::anchor()
386 return anchor_.back();
390 CursorSlice const & LCursor::anchor() const
392 return anchor_.back();
396 CursorSlice const & LCursor::selBegin() const
399 return cursor_.back();
400 // can't use std::min as this creates a new object
401 return anchor() < cursor_.back() ? anchor() : cursor_.back();
405 CursorSlice & LCursor::selBegin()
408 return cursor_.back();
409 return anchor() < cursor_.back() ? anchor() : cursor_.back();
413 CursorSlice const & LCursor::selEnd() const
416 return cursor_.back();
417 return anchor() > cursor_.back() ? anchor() : cursor_.back();
421 CursorSlice & LCursor::selEnd()
424 return cursor_.back();
425 return anchor() > cursor_.back() ? anchor() : cursor_.back();
429 void LCursor::setSelection()
432 // a selection with no contents is not a selection
433 if (par() == anchor().par() && pos() == anchor().pos())
438 void LCursor::setSelection(CursorBase const & where, size_t n)
447 void LCursor::clearSelection()
456 void LCursor::x_target(int x)
462 int LCursor::x_target() const
468 Paragraph & LCursor::paragraph()
470 return current_ ? current().paragraph() : *bv_->text()->getPar(par());
474 Paragraph const & LCursor::paragraph() const
476 return current_ ? current().paragraph() : *bv_->text()->getPar(par());
480 LCursor::pos_type LCursor::lastpos() const
482 CursorSlice const & cur = current();
483 if (cur.inset() && cur.inset()->asMathInset())
484 return cell().size();
486 return paragraph().size();
490 size_t LCursor::nargs() const
492 // assume 1x1 grid for 'plain text'
493 return current_ ? current().nargs() : 1;
497 size_t LCursor::ncols() const
499 // assume 1x1 grid for 'plain text'
500 return current_ ? current().ncols() : 1;
504 size_t LCursor::nrows() const
506 // assume 1x1 grid for 'plain text'
507 return current_ ? current().nrows() : 1;
511 LCursor::row_type LCursor::row() const
513 BOOST_ASSERT(current_ > 0);
514 return current().row();
518 LCursor::col_type LCursor::col() const
520 BOOST_ASSERT(current_ > 0);
521 return current().col();
525 MathArray const & LCursor::cell() const
527 BOOST_ASSERT(current_ > 0);
528 return current().cell();
532 MathArray & LCursor::cell()
534 BOOST_ASSERT(current_ > 0);
535 return current().cell();
539 void LCursor::info(std::ostream & os)
541 for (int i = 1, n = depth(); i < n; ++i) {
542 cursor_[i].inset()->infoize(os);
547 // prevAtom()->infoize2(os);
548 // overwite old message
555 void region(CursorSlice const & i1, CursorSlice const & i2,
556 LCursor::row_type & r1, LCursor::row_type & r2,
557 LCursor::col_type & c1, LCursor::col_type & c2)
559 InsetBase * p = i1.inset();
560 c1 = p->col(i1.idx_);
561 c2 = p->col(i2.idx_);
564 r1 = p->row(i1.idx_);
565 r2 = p->row(i2.idx_);
573 string LCursor::grabSelection()
578 CursorSlice i1 = selBegin();
579 CursorSlice i2 = selEnd();
581 if (i1.idx_ == i2.idx_) {
582 if (i1.inset()->asMathInset()) {
583 MathArray::const_iterator it = i1.cell().begin();
584 return asString(MathArray(it + i1.pos_, it + i2.pos_));
586 return "unknown selection 1";
592 region(i1, i2, r1, r2, c1, c2);
595 if (i1.inset()->asMathInset()) {
596 for (row_type row = r1; row <= r2; ++row) {
599 for (col_type col = c1; col <= c2; ++col) {
602 data += asString(i1.asMathInset()->cell(i1.asMathInset()->index(row, col)));
606 data = "unknown selection 2";
612 void LCursor::eraseSelection()
614 CursorSlice i1 = selBegin();
615 CursorSlice i2 = selEnd();
617 if (i1.inset()->asMathInset()) {
618 if (i1.idx_ == i2.idx_) {
619 i1.cell().erase(i1.pos_, i2.pos_);
621 MathInset * p = i1.asMathInset();
624 region(i1, i2, r1, r2, c1, c2);
625 for (row_type row = r1; row <= r2; ++row)
626 for (col_type col = c1; col <= c2; ++col)
627 p->cell(p->index(row, col)).clear();
631 lyxerr << "can't erase this selection 1" << endl;
636 string LCursor::grabAndEraseSelection()
640 string res = grabSelection();
647 void LCursor::selClear()
654 void LCursor::selCopy()
657 theCutBuffer.push(grabSelection());
660 //theCutBuffer.erase();
665 void LCursor::selCut()
667 theCutBuffer.push(grabAndEraseSelection());
671 void LCursor::selDel()
680 void LCursor::selPaste(size_t n)
683 if (n < theCutBuffer.size())
684 paste(theCutBuffer[n]);
690 void LCursor::selHandle(bool sel)
692 if (sel == selection())
699 void LCursor::selStart()
706 void LCursor::selClearOrDel()
708 if (lyxrc.auto_region_delete)
715 std::ostream & operator<<(std::ostream & os, LCursor const & cur)
718 for (size_t i = 0, n = cur.cursor_.size(); i != n; ++i)
719 os << " (" << cur.cursor_[i] << " | " << cur.anchor_[i] << "\n";
731 void increment(CursorBase & it)
733 CursorSlice & top = it.back();
734 MathArray & ar = top.asMathInset()->cell(top.idx_);
736 // move into the current inset if possible
737 // it is impossible for pos() == size()!
739 if (top.pos() != top.lastpos())
740 n = (ar.begin() + top.pos_)->nucleus();
741 if (n && n->isActive()) {
742 it.push_back(CursorSlice(n));
746 // otherwise move on one cell back if possible
747 if (top.pos() < top.lastpos()) {
748 // pos() == lastpos() is valid!
753 // otherwise try to move on one cell if possible
754 while (top.idx() < top.lastidx()) {
756 if (top.asMathInset()->validCell(top.idx_)) {
762 // otherwise leave array, move on one back
763 // this might yield pos() == size(), but that's a ok.
765 // it certainly invalidates top
770 CursorBase ibegin(InsetBase * p)
773 it.push_back(CursorSlice(p));
778 CursorBase iend(InsetBase * p)
781 it.push_back(CursorSlice(p));
782 CursorSlice & cur = it.back();
783 cur.idx() = cur.lastidx();
784 cur.pos() = cur.lastpos();
791 ///////////////////////////////////////////////////////////////////
793 // The part below is the non-integrated rest of the original math
794 // cursor. This should be either generalized for texted or moved
795 // back to the math insets.
797 ///////////////////////////////////////////////////////////////////
799 #include "mathed/math_braceinset.h"
800 #include "mathed/math_charinset.h"
801 #include "mathed/math_commentinset.h"
802 #include "mathed/math_factory.h"
803 #include "mathed/math_gridinset.h"
804 #include "mathed/math_macroarg.h"
805 #include "mathed/math_macrotemplate.h"
806 #include "mathed/math_mathmlstream.h"
807 #include "mathed/math_scriptinset.h"
808 #include "mathed/math_spaceinset.h"
809 #include "mathed/math_support.h"
810 #include "mathed/math_unknowninset.h"
812 //#define FILEDEBUG 1
815 bool LCursor::isInside(InsetBase const * p)
817 for (unsigned i = 0; i < depth(); ++i)
818 if (cursor_[i].inset() == p)
824 bool LCursor::openable(MathAtom const & t)
835 // we can't move into anything new during selection
836 if (depth() == anchor_.size())
838 if (t.nucleus() != anchor_[depth()].inset())
845 bool LCursor::inNucleus()
847 return inset()->asMathInset()->asScriptInset() && idx() == 2;
853 autocorrect() = false;
854 x_target(-1); // "no target"
860 if (pos() != 0 && openable(prevAtom())) {
863 inset()->idxFirst(*this);
867 return posLeft() || idxLeft() || popLeft() || selection();
871 bool LCursor::right()
873 autocorrect() = false;
874 x_target(-1); // "no target"
880 if (pos() != lastpos() && openable(nextAtom())) {
885 return posRight() || idxRight() || popRight() || selection();
889 bool positionable(CursorBase const & cursor, CursorBase const & anchor)
891 // avoid deeper nested insets when selecting
892 if (cursor.size() > anchor.size())
895 // anchor might be deeper, should have same path then
896 for (CursorBase::size_type i = 0; i < cursor.size(); ++i)
897 if (cursor[i].inset() != anchor[i].inset())
900 // position should be ok.
905 void LCursor::setScreenPos(int x, int y)
907 bool res = bruteFind(x, y, formula()->xlow(), formula()->xhigh(),
908 formula()->ylow(), formula()->yhigh());
910 // this can happen on creation of "math-display"
914 x_target(-1); // "no target"
921 autocorrect() = false;
923 if (!inset()->idxHome(*this))
925 x_target(-1); // "no target"
932 autocorrect() = false;
934 if (!inset()->idxEnd(*this))
936 x_target(-1); // "no target"
941 void LCursor::plainErase()
947 void LCursor::markInsert()
949 cell().insert(pos(), MathAtom(new MathCharInset(0)));
953 void LCursor::markErase()
959 void LCursor::plainInsert(MathAtom const & t)
961 cell().insert(pos(), t);
966 void LCursor::insert2(string const & str)
974 void LCursor::insert(string const & str)
976 //lyxerr << "inserting '" << str << "'" << endl;
978 for (string::const_iterator it = str.begin(); it != str.end(); ++it)
979 plainInsert(MathAtom(new MathCharInset(*it)));
983 void LCursor::insert(char c)
985 //lyxerr << "inserting '" << c << "'" << endl;
987 plainInsert(MathAtom(new MathCharInset(c)));
991 void LCursor::insert(MathAtom const & t)
999 void LCursor::niceInsert(string const & t)
1010 void LCursor::niceInsert(MathAtom const & t)
1013 string safe = grabAndEraseSelection();
1015 // enter the new inset and move the contents of the selection if possible
1016 if (t->isActive()) {
1024 void LCursor::insert(MathArray const & ar)
1029 cell().insert(pos(), ar);
1034 bool LCursor::backspace()
1036 autocorrect() = false;
1044 if (inset()->nargs() == 1 && depth() == 1 && lastpos() == 0)
1050 if (inMacroMode()) {
1051 MathUnknownInset * p = activeMacro();
1052 if (p->name().size() > 1) {
1053 p->setName(p->name().substr(0, p->name().size() - 1));
1058 if (pos() != 0 && prevAtom()->nargs() > 0) {
1059 // let's require two backspaces for 'big stuff' and
1060 // highlight on the first
1071 bool LCursor::erase()
1073 autocorrect() = false;
1082 // delete empty cells if possible
1084 //if (cell().empty() && inset()->idxDelete(idx()))
1087 // special behaviour when in last position of cell
1088 if (pos() == lastpos()) {
1089 bool one_cell = inset()->nargs() == 1;
1090 if (one_cell && depth() == 1 && lastpos() == 0)
1096 inset()->idxGlue(idx());
1100 if (pos() != lastpos() && inset()->nargs() > 0) {
1114 CursorBase save = cursor_;
1118 autocorrect() = false;
1123 bool LCursor::down()
1126 CursorBase save = cursor_;
1127 if (goUpDown(false))
1130 autocorrect() = false;
1135 void LCursor::macroModeClose()
1139 MathUnknownInset * p = activeMacro();
1141 string s = p->name();
1143 cell().erase(pos());
1145 // do nothing if the macro name is empty
1149 string const name = s.substr(1);
1151 // prevent entering of recursive macros
1152 if (formula()->lyxCode() == InsetOld::MATHMACRO_CODE
1153 && formula()->getInsetName() == name)
1154 lyxerr << "can't enter recursive macro" << endl;
1156 niceInsert(createMathInset(name));
1160 string LCursor::macroName()
1162 return inMacroMode() ? activeMacro()->name() : string();
1166 void LCursor::drawSelection(PainterInfo & pi)
1170 CursorSlice i1 = selBegin();
1171 CursorSlice i2 = selEnd();
1172 i1.asMathInset()->drawSelection(pi, i1.idx_, i1.pos_, i2.idx_, i2.pos_);
1176 void LCursor::handleNest(MathAtom const & a, int c)
1179 asArray(grabAndEraseSelection(), at.nucleus()->cell(c));
1186 void LCursor::getScreenPos(int & x, int & y) const
1188 BOOST_ASSERT(inset());
1189 BOOST_ASSERT(inset()->asMathInset());
1190 inset()->asMathInset()->getScreenPos(idx(), pos(), x, y);
1194 int LCursor::targetX() const
1196 if (x_target() != -1)
1205 MathHullInset * LCursor::formula() const
1207 for (int i = cursor_.size() - 1; i >= 1; --i)
1208 if (cursor_[i].inset()->lyxCode() == InsetBase::MATH_CODE)
1209 return static_cast<MathHullInset *>(cursor_[i].inset());
1214 void LCursor::adjust(pos_type from, int diff)
1218 if (anchor().pos_ > from)
1219 anchor().pos_ += diff;
1220 // just to be on the safe side
1221 // theoretically unecessary
1226 bool LCursor::inMacroMode() const
1230 MathUnknownInset const * p = prevAtom()->asUnknownInset();
1231 return p && !p->final();
1235 MathUnknownInset * LCursor::activeMacro()
1237 return inMacroMode() ? prevAtom().nucleus()->asUnknownInset() : 0;
1241 bool LCursor::inMacroArgMode() const
1243 return pos() > 0 && prevAtom()->getChar() == '#';
1247 MathGridInset * LCursor::enclosingGrid(idx_type & idx) const
1249 for (MathInset::difference_type i = depth() - 1; i >= 0; --i) {
1250 MathInset * m = cursor_[i].inset()->asMathInset();
1253 MathGridInset * p = m->asGridInset();
1255 idx = cursor_[i].idx_;
1263 void LCursor::pullArg()
1267 MathArray ar = cell();
1270 cell().insert(pos(), ar);
1273 formula()->mutateToText();
1279 void LCursor::touch()
1283 CursorBase::const_iterator it = cursor_.begin();
1284 CursorBase::const_iterator et = cursor_.end();
1285 for ( ; it != et; ++it)
1291 void LCursor::normalize()
1293 if (idx() >= nargs()) {
1294 lyxerr << "this should not really happen - 1: "
1295 << idx() << ' ' << nargs()
1296 << " in: " << inset() << endl;
1298 idx() = min(idx(), nargs() - 1);
1300 if (pos() > lastpos()) {
1301 lyxerr << "this should not really happen - 2: "
1302 << pos() << ' ' << lastpos() << " in idx: " << idx()
1304 WriteStream wi(lyxerr, false, true);
1305 inset()->asMathInset()->write(wi);
1308 pos() = min(pos(), lastpos());
1312 char LCursor::valign()
1315 MathGridInset * p = enclosingGrid(idx);
1316 return p ? p->valign() : '\0';
1320 char LCursor::halign()
1323 MathGridInset * p = enclosingGrid(idx);
1324 return p ? p->halign(idx % p->ncols()) : '\0';
1328 bool LCursor::goUpDown(bool up)
1330 // Be warned: The 'logic' implemented in this function is highly fragile.
1331 // A distance of one pixel or a '<' vs '<=' _really_ matters.
1332 // So fiddle around with it only if you know what you are doing!
1335 getScreenPos(xo, yo);
1337 // check if we had something else in mind, if not, this is the future goal
1338 if (x_target() == -1)
1343 // try neigbouring script insets
1347 MathScriptInset const * p = prevAtom()->asScriptInset();
1348 if (p && p->has(up)) {
1351 idx() = up; // the superscript has index 1
1353 //lyxerr << "updown: handled by scriptinset to the left" << endl;
1359 if (pos() != lastpos()) {
1360 MathScriptInset const * p = nextAtom()->asScriptInset();
1361 if (p && p->has(up)) {
1365 //lyxerr << "updown: handled by scriptinset to the right" << endl;
1371 // try current cell for e.g. text insets
1372 if (inset()->idxUpDown2(*this, up))
1375 //xarray().boundingBox(xlow, xhigh, ylow, yhigh);
1380 //if (bruteFind(xo, yo, xlow, xhigh, ylow, yhigh)) {
1381 // lyxerr << "updown: handled by brute find in the same cell" << endl;
1385 // try to find an inset that knows better then we
1387 //lyxerr << "updown: We are in " << inset() << " idx: " << idx() << endl;
1389 if (inset()->idxUpDown(*this, up)) {
1390 // try to find best position within this inset
1396 // no such inset found, just take something "above"
1397 //lyxerr << "updown: handled by strange case" << endl;
1403 up ? formula()->ylow() : yo + 4,
1404 up ? yo - 4 : formula()->yhigh()
1408 // any improvement so far?
1410 getScreenPos(xnew, ynew);
1411 if (up ? ynew < yo : ynew > yo)
1417 bool LCursor::bruteFind(int x, int y, int xlow, int xhigh, int ylow, int yhigh)
1419 CursorBase best_cursor;
1420 double best_dist = 1e10;
1422 CursorBase it = ibegin(formula());
1423 CursorBase et = iend(formula());
1425 // avoid invalid nesting when selecting
1426 if (!selection() || positionable(it, anchor_)) {
1428 it.back().getScreenPos(xo, yo);
1429 if (xlow <= xo && xo <= xhigh && ylow <= yo && yo <= yhigh) {
1430 double d = (x - xo) * (x - xo) + (y - yo) * (y - yo);
1431 //lyxerr << "x: " << x << " y: " << y << " d: " << endl;
1432 // '<=' in order to take the last possible position
1433 // this is important for clicking behind \sum in e.g. '\sum_i a'
1434 if (d <= best_dist) {
1446 if (best_dist < 1e10)
1447 cursor_ = best_cursor;
1448 return best_dist < 1e10;
1452 void LCursor::bruteFind2(int x, int y)
1454 double best_dist = 1e10;
1456 CursorBase it = cursor_;
1458 CursorBase et = cursor_;
1459 int n = et.back().asMathInset()->cell(et.back().idx_).size();
1461 for (int i = 0; ; ++i) {
1463 it.back().getScreenPos(xo, yo);
1464 double d = (x - xo) * (x - xo) + (y - yo) * (y - yo);
1465 // '<=' in order to take the last possible position
1466 // this is important for clicking behind \sum in e.g. '\sum_i a'
1467 lyxerr << "i: " << i << " d: " << d << " best: " << best_dist << endl;
1468 if (d <= best_dist) {
1479 bool LCursor::idxLineLast()
1481 idx() -= idx() % ncols();
1482 idx() += ncols() - 1;
1488 bool LCursor::idxLeft()
1490 return inset()->idxLeft(*this);
1494 bool LCursor::idxRight()
1496 return inset()->idxRight(*this);
1500 bool LCursor::script(bool up)
1502 // Hack to get \\^ and \\_ working
1503 if (inMacroMode() && macroName() == "\\") {
1505 niceInsert(createMathInset("mathcircumflex"));
1512 string safe = grabAndEraseSelection();
1514 // we are in a nucleus of a script inset, move to _our_ script
1515 inset()->asMathInset()->asScriptInset()->ensure(up);
1518 } else if (pos() != 0 && prevAtom()->asScriptInset()) {
1519 prevAtom().nucleus()->asScriptInset()->ensure(up);
1524 } else if (pos() != 0) {
1527 = MathAtom(new MathScriptInset(nextAtom(), up));
1532 plainInsert(MathAtom(new MathScriptInset(up)));
1534 nextAtom().nucleus()->asScriptInset()->ensure(up);
1544 bool LCursor::interpret(char c)
1546 //lyxerr << "interpret 2: '" << c << "'" << endl;
1547 x_target(-1); // "no target"
1548 if (inMacroArgMode()) {
1554 MathMacroTemplate const * p = formula()->asMacroTemplate();
1555 if (p && 1 <= n && n <= p->numargs())
1556 insert(MathAtom(new MathMacroArgument(c - '0')));
1558 insert(createMathInset("#"));
1559 interpret(c); // try again
1566 if (inMacroMode()) {
1567 string name = macroName();
1568 //lyxerr << "interpret name: '" << name << "'" << endl;
1571 activeMacro()->setName(activeMacro()->name() + c);
1575 // handle 'special char' macros
1580 if (currentMode() == MathInset::TEXT_MODE)
1581 niceInsert(createMathInset("textbackslash"));
1583 niceInsert(createMathInset("backslash"));
1584 } else if (c == '{') {
1585 niceInsert(MathAtom(new MathBraceInset));
1587 niceInsert(createMathInset(string(1, c)));
1592 // leave macro mode and try again if necessary
1595 niceInsert(MathAtom(new MathBraceInset));
1601 // This is annoying as one has to press <space> far too often.
1605 // leave autocorrect mode if necessary
1606 if (autocorrect() && c == ' ') {
1607 autocorrect() = false;
1612 // just clear selection on pressing the space bar
1613 if (selection() && c == ' ') {
1614 selection() = false;
1621 //lyxerr << "starting with macro" << endl;
1622 insert(MathAtom(new MathUnknownInset("\\", false)));
1627 if (currentMode() == MathInset::TEXT_MODE)
1633 if (currentMode() == MathInset::TEXT_MODE) {
1634 // insert spaces in text mode,
1635 // but suppress direct insertion of two spaces in a row
1636 // the still allows typing '<space>a<space>' and deleting the 'a', but
1637 // it is better than nothing...
1638 if (!pos() != 0 || prevAtom()->getChar() != ' ')
1642 if (pos() != 0 && prevAtom()->asSpaceInset()) {
1643 prevAtom().nucleus()->asSpaceInset()->incSpace();
1648 // if are at the very end, leave the formula
1649 return pos() != lastpos();
1662 if (c == '{' || c == '}' || c == '#' || c == '&' || c == '$') {
1663 niceInsert(createMathInset(string(1, c)));
1668 niceInsert(MathAtom(new MathCommentInset));
1672 // try auto-correction
1673 //if (autocorrect() && hasPrevAtom() && math_autocorrect(prevAtom(), c))
1676 // no special circumstances, so insert the character without any fuss
1678 autocorrect() = true;
1683 void LCursor::lockToggle()
1685 if (pos() != lastpos()) {
1686 // toggle previous inset ...
1687 nextAtom().nucleus()->lock(!nextAtom()->lock());
1688 } else if (popLeft() && pos() != lastpos()) {
1689 // ... or enclosing inset if we are in the last inset position
1690 nextAtom().nucleus()->lock(!nextAtom()->lock());
1696 CursorSlice LCursor::normalAnchor()
1698 if (anchor_.size() < depth()) {
1700 lyxerr << "unusual Anchor size" << endl;
1702 //lyx::BOOST_ASSERT(Anchor_.size() >= cursor.depth());
1703 // use Anchor on the same level as Cursor
1704 CursorSlice normal = anchor_[depth() - 1];
1706 if (depth() < anchor_.size() && !(normal < xx())) {
1707 // anchor is behind cursor -> move anchor behind the inset
1716 DispatchResult dispatch(LCursor & cur, FuncRequest const & cmd)
1718 // mouse clicks are somewhat special
1720 switch (cmd.action) {
1721 case LFUN_MOUSE_PRESS:
1722 case LFUN_MOUSE_MOTION:
1723 case LFUN_MOUSE_RELEASE:
1724 case LFUN_MOUSE_DOUBLE: {
1725 CursorSlice & pos = cursor_.back();
1729 if (x < cmd.x && pos() != 0) {
1730 DispatchResult const res = prevAtom().nucleus()->dispatch(cmd);
1731 if (res.dispatched())
1734 if (x > cmd.x && pos() != lastpos()) {
1735 DispatchResult const res = inset()->dispatch(cmd);
1736 if (res.dispatched())
1748 void LCursor::handleFont(string const & font)
1753 safe = grabAndEraseSelection();
1756 if (lastpos() != 0) {
1757 // something left in the cell
1759 // cursor in first position
1761 } else if (pos() == lastpos()) {
1762 // cursor in last position
1765 // cursor in between. split cell
1766 MathArray::iterator bt = cell().begin();
1767 MathAtom at = createMathInset(font);
1768 at.nucleus()->cell(0) = MathArray(bt, bt + pos());
1769 cell().erase(bt, bt + pos());
1774 // nothing left in the cell
1782 void LCursor::releaseMathCursor()
1785 formula()->insetUnlock(bv());
1789 bool LCursor::inMathed() const