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 LyXText * LCursor::text() const
479 return current_ ? current().text() : bv_->text();
483 Paragraph & LCursor::paragraph()
485 BOOST_ASSERT(!inMathed());
486 return current_ ? current().paragraph() : *bv_->text()->getPar(par());
490 Paragraph const & LCursor::paragraph() const
492 BOOST_ASSERT(!inMathed());
493 return current_ ? current().paragraph() : *bv_->text()->getPar(par());
497 LCursor::par_type LCursor::lastpar() const
499 return inMathed() ? 0 : text()->paragraphs().size() - 1;
503 LCursor::pos_type LCursor::lastpos() const
505 InsetBase * inset = current().inset();
506 return inset && inset->asMathInset() ? cell().size() : paragraph().size();
510 LCursor::row_type LCursor::crow() const
512 return paragraph().row(pos());
516 LCursor::row_type LCursor::lastcrow() const
518 return paragraph().rows.size();
522 size_t LCursor::nargs() const
524 // assume 1x1 grid for 'plain text'
525 return current_ ? current().nargs() : 1;
529 size_t LCursor::ncols() const
531 // assume 1x1 grid for 'plain text'
532 return current_ ? current().ncols() : 1;
536 size_t LCursor::nrows() const
538 // assume 1x1 grid for 'plain text'
539 return current_ ? current().nrows() : 1;
543 LCursor::row_type LCursor::row() const
545 BOOST_ASSERT(current_ > 0);
546 return current().row();
550 LCursor::col_type LCursor::col() const
552 BOOST_ASSERT(current_ > 0);
553 return current().col();
557 MathArray const & LCursor::cell() const
559 BOOST_ASSERT(current_ > 0);
560 return current().cell();
564 MathArray & LCursor::cell()
566 BOOST_ASSERT(current_ > 0);
567 return current().cell();
571 void LCursor::info(std::ostream & os)
573 for (int i = 1, n = depth(); i < n; ++i) {
574 cursor_[i].inset()->infoize(os);
579 // prevAtom()->infoize2(os);
580 // overwite old message
587 void region(CursorSlice const & i1, CursorSlice const & i2,
588 LCursor::row_type & r1, LCursor::row_type & r2,
589 LCursor::col_type & c1, LCursor::col_type & c2)
591 InsetBase * p = i1.inset();
592 c1 = p->col(i1.idx_);
593 c2 = p->col(i2.idx_);
596 r1 = p->row(i1.idx_);
597 r2 = p->row(i2.idx_);
605 string LCursor::grabSelection()
610 CursorSlice i1 = selBegin();
611 CursorSlice i2 = selEnd();
613 if (i1.idx_ == i2.idx_) {
614 if (i1.inset()->asMathInset()) {
615 MathArray::const_iterator it = i1.cell().begin();
616 return asString(MathArray(it + i1.pos_, it + i2.pos_));
618 return "unknown selection 1";
624 region(i1, i2, r1, r2, c1, c2);
627 if (i1.inset()->asMathInset()) {
628 for (row_type row = r1; row <= r2; ++row) {
631 for (col_type col = c1; col <= c2; ++col) {
634 data += asString(i1.asMathInset()->cell(i1.asMathInset()->index(row, col)));
638 data = "unknown selection 2";
644 void LCursor::eraseSelection()
646 CursorSlice i1 = selBegin();
647 CursorSlice i2 = selEnd();
649 if (i1.inset()->asMathInset()) {
650 if (i1.idx_ == i2.idx_) {
651 i1.cell().erase(i1.pos_, i2.pos_);
653 MathInset * p = i1.asMathInset();
656 region(i1, i2, r1, r2, c1, c2);
657 for (row_type row = r1; row <= r2; ++row)
658 for (col_type col = c1; col <= c2; ++col)
659 p->cell(p->index(row, col)).clear();
663 lyxerr << "can't erase this selection 1" << endl;
668 string LCursor::grabAndEraseSelection()
672 string res = grabSelection();
679 void LCursor::selClear()
686 void LCursor::selCopy()
689 theCutBuffer.push(grabSelection());
692 //theCutBuffer.erase();
697 void LCursor::selCut()
699 theCutBuffer.push(grabAndEraseSelection());
703 void LCursor::selDel()
712 void LCursor::selPaste(size_t n)
715 if (n < theCutBuffer.size())
716 paste(theCutBuffer[n]);
722 void LCursor::selHandle(bool sel)
724 if (sel == selection())
731 void LCursor::selStart()
738 void LCursor::selClearOrDel()
740 if (lyxrc.auto_region_delete)
747 std::ostream & operator<<(std::ostream & os, LCursor const & cur)
750 for (size_t i = 0, n = cur.cursor_.size(); i != n; ++i)
751 os << " (" << cur.cursor_[i] << " | " << cur.anchor_[i] << "\n";
752 return os << "current: " << cur.current_ << endl;
763 void increment(CursorBase & it)
765 CursorSlice & top = it.back();
766 MathArray & ar = top.asMathInset()->cell(top.idx_);
768 // move into the current inset if possible
769 // it is impossible for pos() == size()!
771 if (top.pos() != top.lastpos())
772 n = (ar.begin() + top.pos_)->nucleus();
773 if (n && n->isActive()) {
774 it.push_back(CursorSlice(n));
778 // otherwise move on one cell back if possible
779 if (top.pos() < top.lastpos()) {
780 // pos() == lastpos() is valid!
785 // otherwise try to move on one cell if possible
786 while (top.idx() < top.lastidx()) {
788 if (top.asMathInset()->validCell(top.idx_)) {
794 // otherwise leave array, move on one back
795 // this might yield pos() == size(), but that's a ok.
797 // it certainly invalidates top
802 CursorBase ibegin(InsetBase * p)
805 it.push_back(CursorSlice(p));
810 CursorBase iend(InsetBase * p)
813 it.push_back(CursorSlice(p));
814 CursorSlice & cur = it.back();
815 cur.idx() = cur.lastidx();
816 cur.pos() = cur.lastpos();
823 ///////////////////////////////////////////////////////////////////
825 // The part below is the non-integrated rest of the original math
826 // cursor. This should be either generalized for texted or moved
827 // back to the math insets.
829 ///////////////////////////////////////////////////////////////////
831 #include "mathed/math_braceinset.h"
832 #include "mathed/math_charinset.h"
833 #include "mathed/math_commentinset.h"
834 #include "mathed/math_factory.h"
835 #include "mathed/math_gridinset.h"
836 #include "mathed/math_macroarg.h"
837 #include "mathed/math_macrotemplate.h"
838 #include "mathed/math_mathmlstream.h"
839 #include "mathed/math_scriptinset.h"
840 #include "mathed/math_spaceinset.h"
841 #include "mathed/math_support.h"
842 #include "mathed/math_unknowninset.h"
844 //#define FILEDEBUG 1
847 bool LCursor::isInside(InsetBase const * p)
849 for (unsigned i = 0; i < depth(); ++i)
850 if (cursor_[i].inset() == p)
856 bool LCursor::openable(MathAtom const & t)
867 // we can't move into anything new during selection
868 if (depth() == anchor_.size())
870 if (t.nucleus() != anchor_[depth()].inset())
877 bool LCursor::inNucleus()
879 return inset()->asMathInset()->asScriptInset() && idx() == 2;
885 autocorrect() = false;
892 if (pos() != 0 && openable(prevAtom())) {
894 push(nextAtom().nucleus());
895 inset()->idxLast(*this);
899 return posLeft() || idxLeft() || popLeft() || selection();
903 bool LCursor::right()
905 autocorrect() = false;
912 if (pos() != lastpos() && openable(nextAtom())) {
913 pushLeft(nextAtom().nucleus());
914 inset()->idxFirst(*this);
918 return posRight() || idxRight() || popRight() || selection();
922 bool positionable(CursorBase const & cursor, CursorBase const & anchor)
924 // avoid deeper nested insets when selecting
925 if (cursor.size() > anchor.size())
928 // anchor might be deeper, should have same path then
929 for (size_t i = 0; i < cursor.size(); ++i)
930 if (cursor[i].inset() != anchor[i].inset())
933 // position should be ok.
938 void LCursor::setScreenPos(int x, int y)
940 bool res = bruteFind(x, y, formula()->xlow(), formula()->xhigh(),
941 formula()->ylow(), formula()->yhigh());
943 // this can happen on creation of "math-display"
954 autocorrect() = false;
956 if (!inset()->idxHome(*this))
965 autocorrect() = false;
967 if (!inset()->idxEnd(*this))
974 void LCursor::plainErase()
980 void LCursor::markInsert()
982 cell().insert(pos(), MathAtom(new MathCharInset(0)));
986 void LCursor::markErase()
992 void LCursor::plainInsert(MathAtom const & t)
994 cell().insert(pos(), t);
999 void LCursor::insert2(string const & str)
1007 void LCursor::insert(string const & str)
1009 lyxerr << "inserting '" << str << "'" << endl;
1011 for (string::const_iterator it = str.begin(); it != str.end(); ++it)
1012 plainInsert(MathAtom(new MathCharInset(*it)));
1016 void LCursor::insert(char c)
1018 lyxerr << "inserting '" << c << "'" << endl;
1020 plainInsert(MathAtom(new MathCharInset(c)));
1024 void LCursor::insert(MathAtom const & t)
1032 void LCursor::niceInsert(string const & t)
1034 lyxerr << "*** LCursor::niceInsert 1: " << t << endl;
1044 void LCursor::niceInsert(MathAtom const & t)
1046 lyxerr << "*** LCursor::niceInsert 2: " << t << endl;
1048 string safe = grabAndEraseSelection();
1050 // enter the new inset and move the contents of the selection if possible
1051 if (t->isActive()) {
1053 pushLeft(nextAtom().nucleus());
1056 lyxerr << "*** LCursor::niceInsert 3: " << t << endl;
1060 void LCursor::insert(MathArray const & ar)
1065 cell().insert(pos(), ar);
1070 bool LCursor::backspace()
1072 autocorrect() = false;
1080 if (inset()->nargs() == 1 && depth() == 1 && lastpos() == 0)
1086 if (inMacroMode()) {
1087 MathUnknownInset * p = activeMacro();
1088 if (p->name().size() > 1) {
1089 p->setName(p->name().substr(0, p->name().size() - 1));
1094 if (pos() != 0 && prevAtom()->nargs() > 0) {
1095 // let's require two backspaces for 'big stuff' and
1096 // highlight on the first
1107 bool LCursor::erase()
1109 autocorrect() = false;
1118 // delete empty cells if possible
1119 if (pos() == lastpos() && inset()->idxDelete(idx()))
1122 // special behaviour when in last position of cell
1123 if (pos() == lastpos()) {
1124 bool one_cell = inset()->nargs() == 1;
1125 if (one_cell && depth() == 1 && lastpos() == 0)
1131 inset()->idxGlue(idx());
1135 if (pos() != lastpos() && inset()->nargs() > 0) {
1149 CursorBase save = cursor_;
1153 autocorrect() = false;
1158 bool LCursor::down()
1161 CursorBase save = cursor_;
1162 if (goUpDown(false))
1165 autocorrect() = false;
1170 void LCursor::macroModeClose()
1174 MathUnknownInset * p = activeMacro();
1176 string s = p->name();
1178 cell().erase(pos());
1180 // do nothing if the macro name is empty
1184 string const name = s.substr(1);
1186 // prevent entering of recursive macros
1187 if (formula()->lyxCode() == InsetOld::MATHMACRO_CODE
1188 && formula()->getInsetName() == name)
1189 lyxerr << "can't enter recursive macro" << endl;
1191 niceInsert(createMathInset(name));
1195 string LCursor::macroName()
1197 return inMacroMode() ? activeMacro()->name() : string();
1201 void LCursor::handleNest(MathAtom const & a, int c)
1204 asArray(grabAndEraseSelection(), t.nucleus()->cell(c));
1207 pushLeft(t.nucleus());
1211 int LCursor::targetX() const
1213 if (x_target() != -1)
1222 MathHullInset * LCursor::formula() const
1224 for (int i = cursor_.size() - 1; i >= 1; --i)
1225 if (cursor_[i].inset()->lyxCode() == InsetBase::MATH_CODE)
1226 return static_cast<MathHullInset *>(cursor_[i].inset());
1231 void LCursor::adjust(pos_type from, int diff)
1235 if (anchor().pos_ > from)
1236 anchor().pos_ += diff;
1237 // just to be on the safe side
1238 // theoretically unecessary
1243 bool LCursor::inMacroMode() const
1247 MathUnknownInset const * p = prevAtom()->asUnknownInset();
1248 return p && !p->final();
1252 MathUnknownInset * LCursor::activeMacro()
1254 return inMacroMode() ? prevAtom().nucleus()->asUnknownInset() : 0;
1258 bool LCursor::inMacroArgMode() const
1260 return pos() > 0 && prevAtom()->getChar() == '#';
1264 MathGridInset * LCursor::enclosingGrid(idx_type & idx) const
1266 for (MathInset::difference_type i = depth() - 1; i >= 0; --i) {
1267 MathInset * m = cursor_[i].inset()->asMathInset();
1270 MathGridInset * p = m->asGridInset();
1272 idx = cursor_[i].idx_;
1280 void LCursor::pullArg()
1284 MathArray ar = cell();
1287 cell().insert(pos(), ar);
1290 formula()->mutateToText();
1296 void LCursor::touch()
1300 CursorBase::const_iterator it = cursor_.begin();
1301 CursorBase::const_iterator et = cursor_.end();
1302 for ( ; it != et; ++it)
1308 void LCursor::normalize()
1310 if (idx() >= nargs()) {
1311 lyxerr << "this should not really happen - 1: "
1312 << idx() << ' ' << nargs()
1313 << " in: " << inset() << endl;
1315 idx() = min(idx(), nargs() - 1);
1317 if (pos() > lastpos()) {
1318 lyxerr << "this should not really happen - 2: "
1319 << pos() << ' ' << lastpos() << " in idx: " << idx()
1321 WriteStream wi(lyxerr, false, true);
1322 inset()->asMathInset()->write(wi);
1325 pos() = min(pos(), lastpos());
1329 char LCursor::valign()
1332 MathGridInset * p = enclosingGrid(idx);
1333 return p ? p->valign() : '\0';
1337 char LCursor::halign()
1340 MathGridInset * p = enclosingGrid(idx);
1341 return p ? p->halign(idx % p->ncols()) : '\0';
1345 bool LCursor::goUpDown(bool up)
1347 // Be warned: The 'logic' implemented in this function is highly fragile.
1348 // A distance of one pixel or a '<' vs '<=' _really_ matters.
1349 // So fiddle around with it only if you know what you are doing!
1354 // check if we had something else in mind, if not, this is the future goal
1355 if (x_target() == -1)
1360 // try neigbouring script insets
1364 MathScriptInset const * p = prevAtom()->asScriptInset();
1365 if (p && p->has(up)) {
1368 idx() = up; // the superscript has index 1
1370 //lyxerr << "updown: handled by scriptinset to the left" << endl;
1376 if (pos() != lastpos()) {
1377 MathScriptInset const * p = nextAtom()->asScriptInset();
1378 if (p && p->has(up)) {
1382 //lyxerr << "updown: handled by scriptinset to the right" << endl;
1388 // try current cell for e.g. text insets
1389 if (inset()->idxUpDown2(*this, up))
1392 //xarray().boundingBox(xlow, xhigh, ylow, yhigh);
1397 //if (bruteFind(xo, yo, xlow, xhigh, ylow, yhigh)) {
1398 // lyxerr << "updown: handled by brute find in the same cell" << endl;
1402 // try to find an inset that knows better then we
1404 //lyxerr << "updown: We are in " << inset() << " idx: " << idx() << endl;
1406 if (inset()->idxUpDown(*this, up)) {
1407 // try to find best position within this inset
1413 // no such inset found, just take something "above"
1414 //lyxerr << "updown: handled by strange case" << endl;
1420 up ? formula()->ylow() : yo + 4,
1421 up ? yo - 4 : formula()->yhigh()
1425 // any improvement so far?
1428 if (up ? ynew < yo : ynew > yo)
1434 bool LCursor::bruteFind(int x, int y, int xlow, int xhigh, int ylow, int yhigh)
1436 CursorBase best_cursor;
1437 double best_dist = 1e10;
1439 CursorBase it = ibegin(formula());
1440 CursorBase et = iend(formula());
1442 // avoid invalid nesting when selecting
1443 if (!selection() || positionable(it, anchor_)) {
1445 CursorSlice & cur = it.back();
1446 cur.inset()->getCursorPos(cur, xo, yo);
1447 if (xlow <= xo && xo <= xhigh && ylow <= yo && yo <= yhigh) {
1448 double d = (x - xo) * (x - xo) + (y - yo) * (y - yo);
1449 //lyxerr << "x: " << x << " y: " << y << " d: " << endl;
1450 // '<=' in order to take the last possible position
1451 // this is important for clicking behind \sum in e.g. '\sum_i a'
1452 if (d <= best_dist) {
1464 if (best_dist < 1e10)
1465 cursor_ = best_cursor;
1466 return best_dist < 1e10;
1470 void LCursor::bruteFind2(int x, int y)
1472 double best_dist = 1e10;
1474 CursorBase it = cursor_;
1476 CursorBase et = cursor_;
1477 int n = et.back().asMathInset()->cell(et.back().idx_).size();
1479 for (int i = 0; ; ++i) {
1481 CursorSlice & cur = it.back();
1482 cur.inset()->getCursorPos(cur, xo, yo);
1483 double d = (x - xo) * (x - xo) + (y - yo) * (y - yo);
1484 // '<=' in order to take the last possible position
1485 // this is important for clicking behind \sum in e.g. '\sum_i a'
1486 lyxerr << "i: " << i << " d: " << d << " best: " << best_dist << endl;
1487 if (d <= best_dist) {
1498 bool LCursor::idxLineLast()
1500 idx() -= idx() % ncols();
1501 idx() += ncols() - 1;
1507 bool LCursor::idxLeft()
1509 return inset()->idxLeft(*this);
1513 bool LCursor::idxRight()
1515 return inset()->idxRight(*this);
1519 bool LCursor::script(bool up)
1521 // Hack to get \\^ and \\_ working
1522 lyxerr << "handling script: up: " << up << endl;
1523 if (inMacroMode() && macroName() == "\\") {
1525 niceInsert(createMathInset("mathcircumflex"));
1532 string safe = grabAndEraseSelection();
1534 // we are in a nucleus of a script inset, move to _our_ script
1535 inset()->asMathInset()->asScriptInset()->ensure(up);
1538 } else if (pos() != 0 && prevAtom()->asScriptInset()) {
1539 prevAtom().nucleus()->asScriptInset()->ensure(up);
1544 } else if (pos() != 0) {
1546 cell()[pos()] = MathAtom(new MathScriptInset(nextAtom(), up));
1551 plainInsert(MathAtom(new MathScriptInset(up)));
1553 nextAtom().nucleus()->asScriptInset()->ensure(up);
1563 bool LCursor::interpret(char c)
1565 lyxerr << "interpret 2: '" << c << "'" << endl;
1567 if (inMacroArgMode()) {
1573 MathMacroTemplate const * p = formula()->asMacroTemplate();
1574 if (p && 1 <= n && n <= p->numargs())
1575 insert(MathAtom(new MathMacroArgument(c - '0')));
1577 insert(createMathInset("#"));
1578 interpret(c); // try again
1585 if (inMacroMode()) {
1586 string name = macroName();
1587 //lyxerr << "interpret name: '" << name << "'" << endl;
1590 activeMacro()->setName(activeMacro()->name() + c);
1594 // handle 'special char' macros
1599 if (currentMode() == MathInset::TEXT_MODE)
1600 niceInsert(createMathInset("textbackslash"));
1602 niceInsert(createMathInset("backslash"));
1603 } else if (c == '{') {
1604 niceInsert(MathAtom(new MathBraceInset));
1606 niceInsert(createMathInset(string(1, c)));
1611 // leave macro mode and try again if necessary
1614 niceInsert(MathAtom(new MathBraceInset));
1620 // This is annoying as one has to press <space> far too often.
1624 // leave autocorrect mode if necessary
1625 if (autocorrect() && c == ' ') {
1626 autocorrect() = false;
1631 // just clear selection on pressing the space bar
1632 if (selection() && c == ' ') {
1633 selection() = false;
1640 //lyxerr << "starting with macro" << endl;
1641 insert(MathAtom(new MathUnknownInset("\\", false)));
1646 if (currentMode() == MathInset::TEXT_MODE)
1652 if (currentMode() == MathInset::TEXT_MODE) {
1653 // insert spaces in text mode,
1654 // but suppress direct insertion of two spaces in a row
1655 // the still allows typing '<space>a<space>' and deleting the 'a', but
1656 // it is better than nothing...
1657 if (!pos() != 0 || prevAtom()->getChar() != ' ')
1661 if (pos() != 0 && prevAtom()->asSpaceInset()) {
1662 prevAtom().nucleus()->asSpaceInset()->incSpace();
1667 // if are at the very end, leave the formula
1668 return pos() != lastpos();
1681 if (c == '{' || c == '}' || c == '#' || c == '&' || c == '$') {
1682 niceInsert(createMathInset(string(1, c)));
1687 niceInsert(MathAtom(new MathCommentInset));
1691 // try auto-correction
1692 //if (autocorrect() && hasPrevAtom() && math_autocorrect(prevAtom(), c))
1695 // no special circumstances, so insert the character without any fuss
1697 autocorrect() = true;
1702 void LCursor::lockToggle()
1704 if (pos() != lastpos()) {
1705 // toggle previous inset ...
1706 nextAtom().nucleus()->lock(!nextAtom()->lock());
1707 } else if (popLeft() && pos() != lastpos()) {
1708 // ... or enclosing inset if we are in the last inset position
1709 nextAtom().nucleus()->lock(!nextAtom()->lock());
1715 CursorSlice LCursor::normalAnchor()
1717 if (anchor_.size() < depth()) {
1719 lyxerr << "unusual Anchor size" << endl;
1721 //lyx::BOOST_ASSERT(Anchor_.size() >= cursor.depth());
1722 // use Anchor on the same level as Cursor
1723 CursorSlice normal = anchor_[current_];
1725 if (depth() < anchor_.size() && !(normal < xx())) {
1726 // anchor is behind cursor -> move anchor behind the inset
1735 DispatchResult dispatch(LCursor & cur, FuncRequest const & cmd)
1737 // mouse clicks are somewhat special
1739 switch (cmd.action) {
1740 case LFUN_MOUSE_PRESS:
1741 case LFUN_MOUSE_MOTION:
1742 case LFUN_MOUSE_RELEASE:
1743 case LFUN_MOUSE_DOUBLE: {
1744 CursorSlice & pos = cursor_.back();
1748 if (x < cmd.x && pos() != 0) {
1749 DispatchResult const res = prevAtom().nucleus()->dispatch(cmd);
1750 if (res.dispatched())
1753 if (x > cmd.x && pos() != lastpos()) {
1754 DispatchResult const res = inset()->dispatch(cmd);
1755 if (res.dispatched())
1766 void LCursor::handleFont(string const & font)
1771 safe = grabAndEraseSelection();
1774 if (lastpos() != 0) {
1775 // something left in the cell
1777 // cursor in first position
1779 } else if (pos() == lastpos()) {
1780 // cursor in last position
1783 // cursor in between. split cell
1784 MathArray::iterator bt = cell().begin();
1785 MathAtom at = createMathInset(font);
1786 at.nucleus()->cell(0) = MathArray(bt, bt + pos());
1787 cell().erase(bt, bt + pos());
1792 // nothing left in the cell
1800 void LCursor::releaseMathCursor()
1803 formula()->insetUnlock(bv());
1807 bool LCursor::inMathed() const
1813 InsetBase * LCursor::nextInset()
1815 if (pos() == lastpos())
1818 return nextAtom().nucleus();
1819 Paragraph & par = paragraph();
1820 return par.isInset(pos()) ? par.getInset(pos()) : 0;
1824 InsetBase * LCursor::prevInset()
1829 return prevAtom().nucleus();
1830 Paragraph & par = paragraph();
1831 return par.isInset(pos() - 1) ? par.getInset(pos() - 1) : 0;