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"
23 #include "lyxfunc.h" // only for setMessage()
27 #include "paragraph.h"
29 #include "insets/updatableinset.h"
30 #include "insets/insettabular.h"
31 #include "insets/insettext.h"
33 #include "mathed/math_data.h"
34 #include "mathed/math_hullinset.h"
35 #include "mathed/math_support.h"
37 #include "support/limited_stack.h"
38 #include "frontends/LyXView.h"
40 #include <boost/assert.hpp>
45 #ifndef CXX_GLOBAL_CSTD
54 limited_stack<string> theCutBuffer;
57 LCursor::LCursor(BufferView & bv)
58 : cursor_(1), anchor_(1), bv_(&bv), current_(0),
59 cached_y_(0), x_target_(-1),
60 selection_(false), mark_(false)
68 cursor_.push_back(CursorSlice());
69 anchor_.push_back(CursorSlice());
78 DispatchResult LCursor::dispatch(FuncRequest const & cmd0)
80 lyxerr << "\nLCursor::dispatch: cmd: " << cmd0 << endl << *this << endl;
81 FuncRequest cmd = cmd0;
82 for (current_ = cursor_.size() - 1; current_ >= 1; --current_) {
83 lyxerr << "trying to dispatch to inset " << inset() << endl;
84 DispatchResult res = inset()->dispatch(*this, cmd);
85 if (res.dispatched()) {
86 lyxerr << " successfully dispatched to inset " << inset() << endl;
87 return DispatchResult(true, true);
89 // "Mutate" the request for semi-handled requests that need
90 // additional handling in outer levels.
93 cmd = FuncRequest(LFUN_FINISHED_LEFT);
96 cmd = FuncRequest(LFUN_FINISHED_RIGHT);
99 cmd = FuncRequest(LFUN_FINISHED_UP);
102 cmd = FuncRequest(LFUN_FINISHED_DOWN);
105 //lyxerr << "not handled on level " << current_
106 // << " val: " << res.val() << endl;
110 BOOST_ASSERT(current_ == 0);
111 //lyxerr << "trying to dispatch to main text " << bv_->text()
112 // << " with cursor: " << *this << endl;
113 DispatchResult res = bv_->text()->dispatch(*this, cmd);
114 //lyxerr << " result: " << res.val() << endl;
119 void LCursor::push(InsetBase * inset)
121 lyxerr << "LCursor::push() inset: " << inset << endl;
122 cursor_.push_back(CursorSlice(inset));
123 anchor_.push_back(CursorSlice(inset));
129 void LCursor::pop(int depth)
131 while (int(cursor_.size()) > depth + 1)
133 lyxerr << "LCursor::pop() result: " << *this << endl;
139 BOOST_ASSERT(cursor_.size() >= 1);
142 current_ = cursor_.size() - 1;
146 void LCursor::pushLeft(InsetBase * p)
148 BOOST_ASSERT(!cursor_.empty());
149 //lyxerr << "Entering inset " << t << " left" << endl;
155 bool LCursor::popLeft()
157 BOOST_ASSERT(!cursor_.empty());
158 //lyxerr << "Leaving inset to the left" << endl;
161 inset()->notifyCursorLeaves(idx());
164 inset()->notifyCursorLeaves(idx());
170 bool LCursor::popRight()
172 BOOST_ASSERT(!cursor_.empty());
173 //lyxerr << "Leaving inset to the right" << endl;
176 inset()->notifyCursorLeaves(idx());
179 inset()->notifyCursorLeaves(idx());
186 CursorSlice & LCursor::current()
188 BOOST_ASSERT(!cursor_.empty());
189 //lyxerr << "accessing cursor slice " << current_
190 // << ": " << cursor_[current_] << endl;
191 return cursor_[current_];
195 CursorSlice const & LCursor::current() const
197 //lyxerr << "accessing cursor slice " << current_
198 // << ": " << cursor_[current_] << endl;
199 return cursor_[current_];
203 int LCursor::currentMode()
205 BOOST_ASSERT(!cursor_.empty());
206 for (int i = cursor_.size() - 1; i >= 1; --i) {
207 int res = cursor_[i].inset()->currentMode();
208 if (res != MathInset::UNDECIDED_MODE)
211 return MathInset::TEXT_MODE;
215 LyXText * LCursor::innerText() const
217 BOOST_ASSERT(!cursor_.empty());
218 if (cursor_.size() > 1) {
219 // go up until first non-0 text is hit
220 // (innermost text is 0 in mathed)
221 for (int i = cursor_.size() - 1; i >= 1; --i)
222 if (cursor_[i].text())
223 return cursor_[i].text();
229 CursorSlice const & LCursor::innerTextSlice() const
231 BOOST_ASSERT(!cursor_.empty());
232 if (cursor_.size() > 1) {
233 // go up until first non-0 text is hit
234 // (innermost text is 0 in mathed)
235 for (int i = cursor_.size() - 1; i >= 1; --i)
236 if (cursor_[i].text())
243 void LCursor::updatePos()
245 BOOST_ASSERT(!cursor_.empty());
246 if (cursor_.size() > 1)
247 cached_y_ = bv_->top_y() + cursor_.back().inset()->yo();
248 //cached_y_ = cursor_.back().inset()->yo();
252 void LCursor::getDim(int & asc, int & des) const
254 BOOST_ASSERT(!cursor_.empty());
256 BOOST_ASSERT(inset());
257 BOOST_ASSERT(inset()->asMathInset());
258 //inset()->asMathInset()->getCursorDim(asc, des);
262 Row const & row = *text()->cursorRow();
263 asc = row.baseline();
264 des = row.height() - asc;
269 void LCursor::getPos(int & x, int & y) const
271 BOOST_ASSERT(!cursor_.empty());
274 if (cursor_.size() == 1) {
275 x = bv_->text()->cursorX(cursor_.front());
276 y = bv_->text()->cursorY(cursor_.front());
279 lyxerr << "#### LCursor::getPos: " << *this << endl;
280 BOOST_ASSERT(inset());
282 inset()->getCursorPos(cursor_.back(), x, y);
283 // getCursorPos gives _screen_ coordinates. We need to add
284 // top_y to get document coordinates. This is hidden in cached_y_.
285 //y += cached_y_ - inset()->yo();
286 // The rest is non-obvious. The reason we have to have these
287 // extra computation is that the getCursorPos() calls rely
288 // on the inset's own knowledge of its screen position.
289 // If we scroll up or down in a big enough increment,
290 // inset->draw() is not called: this doesn't update
291 // inset.yo_, so getCursor() returns an old value.
294 //lyxerr << "#### LCursor::getPos: " << *this
295 // << " x: " << x << " y: " << y << endl;
299 void LCursor::paste(string const & data)
301 dispatch(FuncRequest(LFUN_PASTE, data));
305 InsetBase * LCursor::innerInsetOfType(int code) const
307 for (int i = cursor_.size() - 1; i >= 1; --i)
308 if (cursor_[i].inset_->lyxCode() == code)
309 return cursor_[i].inset_;
314 InsetTabular * LCursor::innerInsetTabular() const
316 return static_cast<InsetTabular *>(innerInsetOfType(InsetBase::TABULAR_CODE));
320 void LCursor::resetAnchor()
326 BufferView & LCursor::bv() const
332 MathAtom const & LCursor::prevAtom() const
334 BOOST_ASSERT(pos() > 0);
335 return cell()[pos() - 1];
339 MathAtom & LCursor::prevAtom()
341 BOOST_ASSERT(pos() > 0);
342 return cell()[pos() - 1];
346 MathAtom const & LCursor::nextAtom() const
348 BOOST_ASSERT(pos() < lastpos());
349 return cell()[pos()];
353 MathAtom & LCursor::nextAtom()
355 BOOST_ASSERT(pos() < lastpos());
356 return cell()[pos()];
360 bool LCursor::posLeft()
369 bool LCursor::posRight()
371 if (pos() == lastpos())
378 CursorSlice & LCursor::anchor()
380 return anchor_.back();
384 CursorSlice const & LCursor::anchor() const
386 return anchor_.back();
390 CursorSlice const & LCursor::selBegin() const
393 return cursor_.back();
394 // can't use std::min as this creates a new object
395 return anchor() < cursor_.back() ? anchor() : cursor_.back();
399 CursorSlice & LCursor::selBegin()
402 return cursor_.back();
403 return anchor() < cursor_.back() ? anchor() : cursor_.back();
407 CursorSlice const & LCursor::selEnd() const
410 return cursor_.back();
411 return anchor() > cursor_.back() ? anchor() : cursor_.back();
415 CursorSlice & LCursor::selEnd()
418 return cursor_.back();
419 return anchor() > cursor_.back() ? anchor() : cursor_.back();
423 void LCursor::setSelection()
426 // a selection with no contents is not a selection
427 if (par() == anchor().par() && pos() == anchor().pos())
432 void LCursor::setSelection(CursorBase const & where, size_t n)
441 void LCursor::clearSelection()
450 int & LCursor::x_target()
456 int LCursor::x_target() const
462 void LCursor::clearTargetX()
468 LyXText * LCursor::text() const
470 return current_ ? current().text() : bv_->text();
474 Paragraph & LCursor::paragraph()
476 BOOST_ASSERT(inTexted());
477 return current_ ? current().paragraph() : *bv_->text()->getPar(par());
481 Paragraph const & LCursor::paragraph() const
483 BOOST_ASSERT(inTexted());
484 return current_ ? current().paragraph() : *bv_->text()->getPar(par());
488 LCursor::par_type LCursor::lastpar() const
490 return inMathed() ? 0 : text()->paragraphs().size() - 1;
494 LCursor::pos_type LCursor::lastpos() const
496 InsetBase * inset = current().inset();
497 return inset && inset->asMathInset() ? cell().size() : paragraph().size();
501 LCursor::row_type LCursor::crow() const
503 return paragraph().row(pos());
507 LCursor::row_type LCursor::lastcrow() const
509 return paragraph().rows.size();
513 size_t LCursor::nargs() const
515 // assume 1x1 grid for 'plain text'
516 return current_ ? current().nargs() : 1;
520 size_t LCursor::ncols() const
522 // assume 1x1 grid for 'plain text'
523 return current_ ? current().ncols() : 1;
527 size_t LCursor::nrows() const
529 // assume 1x1 grid for 'plain text'
530 return current_ ? current().nrows() : 1;
534 LCursor::row_type LCursor::row() const
536 BOOST_ASSERT(current_ > 0);
537 return current().row();
541 LCursor::col_type LCursor::col() const
543 BOOST_ASSERT(current_ > 0);
544 return current().col();
548 MathArray const & LCursor::cell() const
550 BOOST_ASSERT(current_ > 0);
551 return current().cell();
555 MathArray & LCursor::cell()
557 BOOST_ASSERT(current_ > 0);
558 return current().cell();
562 void LCursor::info(std::ostream & os)
564 for (int i = 1, n = depth(); i < n; ++i) {
565 cursor_[i].inset()->infoize(os);
569 prevInset()->infoize2(os);
570 // overwite old message
577 void region(CursorSlice const & i1, CursorSlice const & i2,
578 LCursor::row_type & r1, LCursor::row_type & r2,
579 LCursor::col_type & c1, LCursor::col_type & c2)
581 InsetBase * p = i1.inset();
582 c1 = p->col(i1.idx_);
583 c2 = p->col(i2.idx_);
586 r1 = p->row(i1.idx_);
587 r2 = p->row(i2.idx_);
595 string LCursor::grabSelection()
600 CursorSlice i1 = selBegin();
601 CursorSlice i2 = selEnd();
603 if (i1.idx_ == i2.idx_) {
604 if (i1.inset()->asMathInset()) {
605 MathArray::const_iterator it = i1.cell().begin();
606 return asString(MathArray(it + i1.pos_, it + i2.pos_));
608 return "unknown selection 1";
614 region(i1, i2, r1, r2, c1, c2);
617 if (i1.inset()->asMathInset()) {
618 for (row_type row = r1; row <= r2; ++row) {
621 for (col_type col = c1; col <= c2; ++col) {
624 data += asString(i1.asMathInset()->cell(i1.asMathInset()->index(row, col)));
628 data = "unknown selection 2";
634 void LCursor::eraseSelection()
636 CursorSlice i1 = selBegin();
637 CursorSlice i2 = selEnd();
639 if (i1.inset()->asMathInset()) {
640 if (i1.idx_ == i2.idx_) {
641 i1.cell().erase(i1.pos_, i2.pos_);
643 MathInset * p = i1.asMathInset();
646 region(i1, i2, r1, r2, c1, c2);
647 for (row_type row = r1; row <= r2; ++row)
648 for (col_type col = c1; col <= c2; ++col)
649 p->cell(p->index(row, col)).clear();
653 lyxerr << "can't erase this selection 1" << endl;
658 string LCursor::grabAndEraseSelection()
662 string res = grabSelection();
669 void LCursor::selClear()
676 void LCursor::selCopy()
679 theCutBuffer.push(grabSelection());
682 //theCutBuffer.erase();
687 void LCursor::selCut()
689 theCutBuffer.push(grabAndEraseSelection());
693 void LCursor::selDel()
702 void LCursor::selPaste(size_t n)
705 if (n < theCutBuffer.size())
706 paste(theCutBuffer[n]);
712 void LCursor::selHandle(bool sel)
714 if (sel == selection())
721 void LCursor::selStart()
728 void LCursor::selClearOrDel()
730 if (lyxrc.auto_region_delete)
737 std::ostream & operator<<(std::ostream & os, LCursor const & cur)
740 for (size_t i = 0, n = cur.cursor_.size(); i != n; ++i)
741 os << " (" << cur.cursor_[i] << " | " << cur.anchor_[i] << "\n";
742 return os << "current: " << cur.current_ << endl;
753 void increment(CursorBase & it)
755 CursorSlice & top = it.back();
756 MathArray & ar = top.asMathInset()->cell(top.idx_);
758 // move into the current inset if possible
759 // it is impossible for pos() == size()!
761 if (top.pos() != top.lastpos())
762 n = (ar.begin() + top.pos_)->nucleus();
763 if (n && n->isActive()) {
764 it.push_back(CursorSlice(n));
768 // otherwise move on one cell back if possible
769 if (top.pos() < top.lastpos()) {
770 // pos() == lastpos() is valid!
775 // otherwise try to move on one cell if possible
776 while (top.idx() < top.lastidx()) {
778 if (top.asMathInset()->validCell(top.idx_)) {
784 // otherwise leave array, move on one back
785 // this might yield pos() == size(), but that's a ok.
787 // it certainly invalidates top
792 CursorBase ibegin(InsetBase * p)
795 it.push_back(CursorSlice(p));
800 CursorBase iend(InsetBase * p)
803 it.push_back(CursorSlice(p));
804 CursorSlice & cur = it.back();
805 cur.idx() = cur.lastidx();
806 cur.pos() = cur.lastpos();
813 ///////////////////////////////////////////////////////////////////
815 // The part below is the non-integrated rest of the original math
816 // cursor. This should be either generalized for texted or moved
817 // back to the math insets.
819 ///////////////////////////////////////////////////////////////////
821 #include "mathed/math_braceinset.h"
822 #include "mathed/math_charinset.h"
823 #include "mathed/math_commentinset.h"
824 #include "mathed/math_factory.h"
825 #include "mathed/math_gridinset.h"
826 #include "mathed/math_macroarg.h"
827 #include "mathed/math_macrotemplate.h"
828 #include "mathed/math_mathmlstream.h"
829 #include "mathed/math_scriptinset.h"
830 #include "mathed/math_spaceinset.h"
831 #include "mathed/math_support.h"
832 #include "mathed/math_unknowninset.h"
834 //#define FILEDEBUG 1
837 bool LCursor::isInside(InsetBase const * p)
839 for (unsigned i = 0; i < depth(); ++i)
840 if (cursor_[i].inset() == p)
846 bool LCursor::openable(MathAtom const & t)
857 // we can't move into anything new during selection
858 if (depth() == anchor_.size())
860 if (!ptr_cmp(t.nucleus(), anchor_[depth()].inset()))
867 bool LCursor::inNucleus()
869 return inset()->asMathInset()->asScriptInset() && idx() == 2;
875 autocorrect() = false;
882 if (pos() != 0 && openable(prevAtom())) {
884 push(nextAtom().nucleus());
885 inset()->idxLast(*this);
889 return posLeft() || idxLeft() || popLeft() || selection();
893 bool LCursor::right()
895 autocorrect() = false;
902 if (pos() != lastpos() && openable(nextAtom())) {
903 pushLeft(nextAtom().nucleus());
904 inset()->idxFirst(*this);
908 return posRight() || idxRight() || popRight() || selection();
912 bool positionable(CursorBase const & cursor, CursorBase const & anchor)
914 // avoid deeper nested insets when selecting
915 if (cursor.size() > anchor.size())
918 // anchor might be deeper, should have same path then
919 for (size_t i = 0; i < cursor.size(); ++i)
920 if (cursor[i].inset() != anchor[i].inset())
923 // position should be ok.
928 void LCursor::setScreenPos(int x, int y)
930 bool res = bruteFind(x, y, formula()->xlow(), formula()->xhigh(),
931 formula()->ylow(), formula()->yhigh());
933 // this can happen on creation of "math-display"
944 autocorrect() = false;
946 if (!inset()->idxHome(*this))
955 autocorrect() = false;
957 if (!inset()->idxEnd(*this))
964 void LCursor::plainErase()
970 void LCursor::markInsert()
972 cell().insert(pos(), MathAtom(new MathCharInset(0)));
976 void LCursor::markErase()
982 void LCursor::plainInsert(MathAtom const & t)
984 cell().insert(pos(), t);
989 void LCursor::insert2(string const & str)
997 void LCursor::insert(string const & str)
999 lyxerr << "inserting '" << str << "'" << endl;
1001 for (string::const_iterator it = str.begin(); it != str.end(); ++it)
1002 plainInsert(MathAtom(new MathCharInset(*it)));
1006 void LCursor::insert(char c)
1008 lyxerr << "inserting '" << c << "'" << endl;
1010 plainInsert(MathAtom(new MathCharInset(c)));
1014 void LCursor::insert(MathAtom const & t)
1022 void LCursor::niceInsert(string const & t)
1024 lyxerr << "*** LCursor::niceInsert 1: " << t << endl;
1034 void LCursor::niceInsert(MathAtom const & t)
1036 lyxerr << "*** LCursor::niceInsert 2: " << t << endl;
1038 string safe = grabAndEraseSelection();
1040 // enter the new inset and move the contents of the selection if possible
1041 if (t->isActive()) {
1043 pushLeft(nextAtom().nucleus());
1046 lyxerr << "*** LCursor::niceInsert 3: " << t << endl;
1050 void LCursor::insert(MathArray const & ar)
1055 cell().insert(pos(), ar);
1060 bool LCursor::backspace()
1062 autocorrect() = false;
1070 if (inset()->nargs() == 1 && depth() == 1 && lastpos() == 0)
1076 if (inMacroMode()) {
1077 MathUnknownInset * p = activeMacro();
1078 if (p->name().size() > 1) {
1079 p->setName(p->name().substr(0, p->name().size() - 1));
1084 if (pos() != 0 && prevAtom()->nargs() > 0) {
1085 // let's require two backspaces for 'big stuff' and
1086 // highlight on the first
1097 bool LCursor::erase()
1099 autocorrect() = false;
1108 // delete empty cells if possible
1109 if (pos() == lastpos() && inset()->idxDelete(idx()))
1112 // special behaviour when in last position of cell
1113 if (pos() == lastpos()) {
1114 bool one_cell = inset()->nargs() == 1;
1115 if (one_cell && depth() == 1 && lastpos() == 0)
1121 inset()->idxGlue(idx());
1125 if (pos() != lastpos() && inset()->nargs() > 0) {
1139 CursorBase save = cursor_;
1143 autocorrect() = false;
1148 bool LCursor::down()
1151 CursorBase save = cursor_;
1152 if (goUpDown(false))
1155 autocorrect() = false;
1160 void LCursor::macroModeClose()
1164 MathUnknownInset * p = activeMacro();
1166 string s = p->name();
1168 cell().erase(pos());
1170 // do nothing if the macro name is empty
1174 string const name = s.substr(1);
1176 // prevent entering of recursive macros
1177 if (formula()->lyxCode() == InsetOld::MATHMACRO_CODE
1178 && formula()->getInsetName() == name)
1179 lyxerr << "can't enter recursive macro" << endl;
1181 niceInsert(createMathInset(name));
1185 string LCursor::macroName()
1187 return inMacroMode() ? activeMacro()->name() : string();
1191 void LCursor::handleNest(MathAtom const & a, int c)
1194 asArray(grabAndEraseSelection(), t.nucleus()->cell(c));
1197 pushLeft(t.nucleus());
1201 int LCursor::targetX() const
1203 if (x_target() != -1)
1212 MathHullInset * LCursor::formula() const
1214 for (int i = cursor_.size() - 1; i >= 1; --i) {
1215 MathInset * inset = cursor_[i].inset()->asMathInset();
1216 if (inset && inset->asHullInset())
1217 return static_cast<MathHullInset *>(inset);
1223 void LCursor::adjust(pos_type from, int diff)
1227 if (anchor().pos_ > from)
1228 anchor().pos_ += diff;
1229 // just to be on the safe side
1230 // theoretically unecessary
1235 bool LCursor::inMacroMode() const
1239 MathUnknownInset const * p = prevAtom()->asUnknownInset();
1240 return p && !p->final();
1244 MathUnknownInset * LCursor::activeMacro()
1246 return inMacroMode() ? prevAtom().nucleus()->asUnknownInset() : 0;
1250 bool LCursor::inMacroArgMode() const
1252 return pos() > 0 && prevAtom()->getChar() == '#';
1256 MathGridInset * LCursor::enclosingGrid(idx_type & idx) const
1258 for (MathInset::difference_type i = depth() - 1; i >= 0; --i) {
1259 MathInset * m = cursor_[i].inset()->asMathInset();
1262 MathGridInset * p = m->asGridInset();
1264 idx = cursor_[i].idx_;
1272 void LCursor::pullArg()
1276 MathArray ar = cell();
1279 cell().insert(pos(), ar);
1282 formula()->mutateToText();
1288 void LCursor::touch()
1292 CursorBase::const_iterator it = cursor_.begin();
1293 CursorBase::const_iterator et = cursor_.end();
1294 for ( ; it != et; ++it)
1300 void LCursor::normalize()
1302 if (idx() >= nargs()) {
1303 lyxerr << "this should not really happen - 1: "
1304 << idx() << ' ' << nargs()
1305 << " in: " << inset() << endl;
1307 idx() = min(idx(), lastidx());
1309 if (pos() > lastpos()) {
1310 lyxerr << "this should not really happen - 2: "
1311 << pos() << ' ' << lastpos() << " in idx: " << idx()
1313 WriteStream wi(lyxerr, false, true);
1314 inset()->asMathInset()->write(wi);
1317 pos() = min(pos(), lastpos());
1321 char LCursor::valign()
1324 MathGridInset * p = enclosingGrid(idx);
1325 return p ? p->valign() : '\0';
1329 char LCursor::halign()
1332 MathGridInset * p = enclosingGrid(idx);
1333 return p ? p->halign(idx % p->ncols()) : '\0';
1337 bool LCursor::goUpDown(bool up)
1339 // Be warned: The 'logic' implemented in this function is highly fragile.
1340 // A distance of one pixel or a '<' vs '<=' _really_ matters.
1341 // So fiddle around with it only if you know what you are doing!
1346 // check if we had something else in mind, if not, this is the future goal
1347 if (x_target() == -1)
1352 // try neigbouring script insets
1356 MathScriptInset const * p = prevAtom()->asScriptInset();
1357 if (p && p->has(up)) {
1360 idx() = up; // the superscript has index 1
1362 //lyxerr << "updown: handled by scriptinset to the left" << endl;
1368 if (pos() != lastpos()) {
1369 MathScriptInset const * p = nextAtom()->asScriptInset();
1370 if (p && p->has(up)) {
1374 //lyxerr << "updown: handled by scriptinset to the right" << endl;
1380 // try current cell for e.g. text insets
1381 if (inset()->idxUpDown2(*this, up))
1384 //xarray().boundingBox(xlow, xhigh, ylow, yhigh);
1389 //if (bruteFind(xo, yo, xlow, xhigh, ylow, yhigh)) {
1390 // lyxerr << "updown: handled by brute find in the same cell" << endl;
1394 // try to find an inset that knows better then we
1396 //lyxerr << "updown: We are in " << inset() << " idx: " << idx() << endl;
1398 if (inset()->idxUpDown(*this, up)) {
1399 // try to find best position within this inset
1405 // no such inset found, just take something "above"
1406 //lyxerr << "updown: handled by strange case" << endl;
1412 up ? formula()->ylow() : yo + 4,
1413 up ? yo - 4 : formula()->yhigh()
1417 // any improvement so far?
1420 if (up ? ynew < yo : ynew > yo)
1426 bool LCursor::bruteFind(int x, int y, int xlow, int xhigh, int ylow, int yhigh)
1428 CursorBase best_cursor;
1429 double best_dist = 1e10;
1431 CursorBase it = ibegin(formula());
1432 CursorBase et = iend(formula());
1434 // avoid invalid nesting when selecting
1435 if (!selection() || positionable(it, anchor_)) {
1437 CursorSlice & cur = it.back();
1438 cur.inset()->getCursorPos(cur, xo, yo);
1439 if (xlow <= xo && xo <= xhigh && ylow <= yo && yo <= yhigh) {
1440 double d = (x - xo) * (x - xo) + (y - yo) * (y - yo);
1441 //lyxerr << "x: " << x << " y: " << y << " d: " << endl;
1442 // '<=' in order to take the last possible position
1443 // this is important for clicking behind \sum in e.g. '\sum_i a'
1444 if (d <= best_dist) {
1456 if (best_dist < 1e10)
1457 cursor_ = best_cursor;
1458 return best_dist < 1e10;
1462 void LCursor::bruteFind2(int x, int y)
1464 double best_dist = 1e10;
1466 CursorBase it = cursor_;
1468 CursorBase et = cursor_;
1469 int n = et.back().asMathInset()->cell(et.back().idx_).size();
1471 for (int i = 0; ; ++i) {
1473 CursorSlice & cur = it.back();
1474 cur.inset()->getCursorPos(cur, xo, yo);
1475 double d = (x - xo) * (x - xo) + (y - yo) * (y - yo);
1476 // '<=' in order to take the last possible position
1477 // this is important for clicking behind \sum in e.g. '\sum_i a'
1478 lyxerr << "i: " << i << " d: " << d << " best: " << best_dist << endl;
1479 if (d <= best_dist) {
1490 bool LCursor::idxLineLast()
1492 idx() -= idx() % ncols();
1493 idx() += ncols() - 1;
1499 bool LCursor::idxLeft()
1501 return inset()->idxLeft(*this);
1505 bool LCursor::idxRight()
1507 return inset()->idxRight(*this);
1511 bool LCursor::script(bool up)
1513 // Hack to get \\^ and \\_ working
1514 lyxerr << "handling script: up: " << up << endl;
1515 if (inMacroMode() && macroName() == "\\") {
1517 niceInsert(createMathInset("mathcircumflex"));
1524 string safe = grabAndEraseSelection();
1526 // we are in a nucleus of a script inset, move to _our_ script
1527 inset()->asMathInset()->asScriptInset()->ensure(up);
1530 } else if (pos() != 0 && prevAtom()->asScriptInset()) {
1532 nextAtom().nucleus()->asScriptInset()->ensure(up);
1536 } else if (pos() != 0) {
1538 cell()[pos()] = MathAtom(new MathScriptInset(nextAtom(), up));
1543 plainInsert(MathAtom(new MathScriptInset(up)));
1545 nextAtom().nucleus()->asScriptInset()->ensure(up);
1555 bool LCursor::interpret(char c)
1557 //lyxerr << "interpret 2: '" << c << "'" << endl;
1559 if (inMacroArgMode()) {
1565 MathMacroTemplate const * p = formula()->asMacroTemplate();
1566 if (p && 1 <= n && n <= p->numargs())
1567 insert(MathAtom(new MathMacroArgument(c - '0')));
1569 insert(createMathInset("#"));
1570 interpret(c); // try again
1577 if (inMacroMode()) {
1578 string name = macroName();
1579 //lyxerr << "interpret name: '" << name << "'" << endl;
1582 activeMacro()->setName(activeMacro()->name() + c);
1586 // handle 'special char' macros
1591 if (currentMode() == MathInset::TEXT_MODE)
1592 niceInsert(createMathInset("textbackslash"));
1594 niceInsert(createMathInset("backslash"));
1595 } else if (c == '{') {
1596 niceInsert(MathAtom(new MathBraceInset));
1598 niceInsert(createMathInset(string(1, c)));
1603 // leave macro mode and try again if necessary
1606 niceInsert(MathAtom(new MathBraceInset));
1612 // This is annoying as one has to press <space> far too often.
1616 // leave autocorrect mode if necessary
1617 if (autocorrect() && c == ' ') {
1618 autocorrect() = false;
1623 // just clear selection on pressing the space bar
1624 if (selection() && c == ' ') {
1625 selection() = false;
1632 //lyxerr << "starting with macro" << endl;
1633 insert(MathAtom(new MathUnknownInset("\\", false)));
1638 if (currentMode() == MathInset::TEXT_MODE)
1644 if (currentMode() == MathInset::TEXT_MODE) {
1645 // insert spaces in text mode,
1646 // but suppress direct insertion of two spaces in a row
1647 // the still allows typing '<space>a<space>' and deleting the 'a', but
1648 // it is better than nothing...
1649 if (!pos() != 0 || prevAtom()->getChar() != ' ')
1653 if (pos() != 0 && prevAtom()->asSpaceInset()) {
1654 prevAtom().nucleus()->asSpaceInset()->incSpace();
1659 // if are at the very end, leave the formula
1660 return pos() != lastpos();
1673 if (c == '{' || c == '}' || c == '#' || c == '&' || c == '$') {
1674 niceInsert(createMathInset(string(1, c)));
1679 niceInsert(MathAtom(new MathCommentInset));
1683 // try auto-correction
1684 //if (autocorrect() && hasPrevAtom() && math_autocorrect(prevAtom(), c))
1687 // no special circumstances, so insert the character without any fuss
1689 autocorrect() = true;
1694 void LCursor::lockToggle()
1696 if (pos() != lastpos()) {
1697 // toggle previous inset ...
1698 nextAtom().nucleus()->lock(!nextAtom()->lock());
1699 } else if (popLeft() && pos() != lastpos()) {
1700 // ... or enclosing inset if we are in the last inset position
1701 nextAtom().nucleus()->lock(!nextAtom()->lock());
1707 CursorSlice LCursor::normalAnchor()
1709 if (anchor_.size() < depth()) {
1711 lyxerr << "unusual Anchor size" << endl;
1713 //lyx::BOOST_ASSERT(Anchor_.size() >= cursor.depth());
1714 // use Anchor on the same level as Cursor
1715 CursorSlice normal = anchor_[current_];
1717 if (depth() < anchor_.size() && !(normal < xx())) {
1718 // anchor is behind cursor -> move anchor behind the inset
1727 DispatchResult dispatch(LCursor & cur, FuncRequest const & cmd)
1729 // mouse clicks are somewhat special
1731 switch (cmd.action) {
1732 case LFUN_MOUSE_PRESS:
1733 case LFUN_MOUSE_MOTION:
1734 case LFUN_MOUSE_RELEASE:
1735 case LFUN_MOUSE_DOUBLE: {
1736 CursorSlice & pos = cursor_.back();
1740 if (x < cmd.x && pos() != 0) {
1741 DispatchResult const res = prevAtom().nucleus()->dispatch(cmd);
1742 if (res.dispatched())
1745 if (x > cmd.x && pos() != lastpos()) {
1746 DispatchResult const res = inset()->dispatch(cmd);
1747 if (res.dispatched())
1758 void LCursor::handleFont(string const & font)
1763 safe = grabAndEraseSelection();
1766 if (lastpos() != 0) {
1767 // something left in the cell
1769 // cursor in first position
1771 } else if (pos() == lastpos()) {
1772 // cursor in last position
1775 // cursor in between. split cell
1776 MathArray::iterator bt = cell().begin();
1777 MathAtom at = createMathInset(font);
1778 at.nucleus()->cell(0) = MathArray(bt, bt + pos());
1779 cell().erase(bt, bt + pos());
1784 // nothing left in the cell
1792 void LCursor::releaseMathCursor()
1795 formula()->insetUnlock(bv());
1799 bool LCursor::inMathed() const
1805 bool LCursor::inTexted() const
1811 InsetBase * LCursor::nextInset()
1813 if (pos() == lastpos())
1816 return nextAtom().nucleus();
1817 Paragraph & par = paragraph();
1818 return par.isInset(pos()) ? par.getInset(pos()) : 0;
1822 InsetBase * LCursor::prevInset()
1827 return prevAtom().nucleus();
1828 Paragraph & par = paragraph();
1829 return par.isInset(pos() - 1) ? par.getInset(pos() - 1) : 0;
1833 void LCursor::message(string const & msg) const
1835 bv().owner()->getLyXFunc().setMessage(msg);
1839 void LCursor::errorMessage(string const & msg) const
1841 bv().owner()->getLyXFunc().setErrorMessage(msg);