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;
83 for (int i = cursor_.size() - 1; i >= 1; --i) {
85 CursorSlice const & citem = cursor_[i];
86 lyxerr << "trying to dispatch to inset " << citem.inset_ << endl;
87 DispatchResult res = inset()->dispatch(*this, cmd);
88 if (res.dispatched()) {
89 lyxerr << " successfully dispatched to inset " << citem.inset_ << endl;
90 return DispatchResult(true, true);
92 // remove one level of cursor
96 cmd = FuncRequest(LFUN_FINISHED_LEFT);
100 cmd = FuncRequest(LFUN_FINISHED_RIGHT);
104 cmd = FuncRequest(LFUN_FINISHED_UP);
108 cmd = FuncRequest(LFUN_FINISHED_DOWN);
111 lyxerr << "not handled on level " << i << " val: " << res.val() << endl;
115 lyxerr << "trying to dispatch to main text " << bv_->text() << endl;
116 DispatchResult res = bv_->text()->dispatch(*this, cmd);
117 lyxerr << " result: " << res.val() << endl;
122 void LCursor::push(InsetBase * inset)
124 lyxerr << "LCursor::push() inset: " << inset << endl;
125 cursor_.push_back(CursorSlice(inset));
126 anchor_.push_back(CursorSlice(inset));
132 void LCursor::pop(int depth)
134 //lyxerr << "LCursor::pop() to depth " << depth << endl;
135 while (int(cursor_.size()) > depth)
142 BOOST_ASSERT(!cursor_.empty());
143 //lyxerr << "LCursor::pop() a level" << endl;
144 if (cursor_.size() <= 1)
145 lyxerr << "### TRYING TO POP FROM EMPTY CURSOR" << endl;
149 current_ = cursor_.size() - 1;
151 //lyxerr << "LCursor::pop() current now: " << current_ << endl;
155 void LCursor::pushLeft(InsetBase * p)
157 BOOST_ASSERT(!cursor_.empty());
158 //lyxerr << "Entering inset " << t << " left" << endl;
164 bool LCursor::popLeft()
166 BOOST_ASSERT(!cursor_.empty());
167 //lyxerr << "Leaving inset to the left" << endl;
170 inset()->notifyCursorLeaves(idx());
173 inset()->notifyCursorLeaves(idx());
179 bool LCursor::popRight()
181 BOOST_ASSERT(!cursor_.empty());
182 //lyxerr << "Leaving inset to the right" << endl;
185 inset()->notifyCursorLeaves(idx());
188 inset()->notifyCursorLeaves(idx());
195 CursorSlice & LCursor::current()
197 BOOST_ASSERT(!cursor_.empty());
198 //lyxerr << "accessing cursor slice " << current_
199 // << ": " << cursor_[current_] << endl;
200 return cursor_[current_];
204 CursorSlice const & LCursor::current() const
206 //lyxerr << "accessing cursor slice " << current_
207 // << ": " << cursor_[current_] << endl;
208 return cursor_[current_];
212 int LCursor::currentMode()
214 BOOST_ASSERT(!cursor_.empty());
215 for (int i = cursor_.size() - 1; i >= 1; --i) {
216 int res = cursor_[i].inset()->currentMode();
217 if (res != MathInset::UNDECIDED_MODE)
220 return MathInset::TEXT_MODE;
224 LyXText * LCursor::innerText() const
226 BOOST_ASSERT(!cursor_.empty());
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 BOOST_ASSERT(!cursor_.empty());
242 //lyxerr << "LCursor::innerTextSlice() depth: " << cursor_.size() << endl;
243 if (cursor_.size() > 1) {
244 // go up until first non-0 text is hit
245 // (innermost text is 0 in mathed)
246 for (int i = cursor_.size() - 1; i >= 1; --i)
247 if (cursor_[i].text())
254 void LCursor::updatePos()
256 BOOST_ASSERT(!cursor_.empty());
257 if (cursor_.size() > 1)
258 cached_y_ = bv_->top_y() + cursor_.back().inset()->yo();
259 //cached_y_ = cursor_.back().inset()->yo();
263 void LCursor::getDim(int & asc, int & des) const
265 BOOST_ASSERT(!cursor_.empty());
266 LyXText * text = innerText();
267 #warning crashes with text-in-math
269 RowList::iterator const rit = text->cursorRow();
270 if (rit != text->endRow()) {
271 asc = rit->baseline();
272 des = rit->height() - asc;
280 //innerInset()->getCursorDim(asc, des);
285 void LCursor::getPos(int & x, int & y) const
287 BOOST_ASSERT(!cursor_.empty());
290 if (cursor_.size() <= 1) {
291 x = bv_->text()->cursorX(cursor_.front());
292 y = bv_->text()->cursorY(cursor_.front());
294 inset()->getCursorPos(cursor_.back(), x, y);
295 // getCursorPos gives _screen_ coordinates. We need to add
296 // top_y to get document coordinates. This is hidden in cached_y_.
297 //y += cached_y_ - inset()->yo();
298 // The rest is non-obvious. The reason we have to have these
299 // extra computation is that the getCursorPos() calls rely
300 // on the inset's own knowledge of its screen position.
301 // If we scroll up or down in a big enough increment,
302 // inset->draw() is not called: this doesn't update
303 // inset.yo_, so getCursor() returns an old value.
306 //lyxerr << "#### LCursor::getPos: " << *this
307 // << " x: " << x << " y: " << y << endl;
311 void LCursor::paste(string const & data)
313 dispatch(FuncRequest(LFUN_PASTE, data));
317 InsetBase * LCursor::innerInsetOfType(int code) const
319 for (int i = cursor_.size() - 1; i >= 1; --i)
320 if (cursor_[i].inset_->lyxCode() == code)
321 return cursor_[i].inset_;
326 InsetTabular * LCursor::innerInsetTabular() const
328 return static_cast<InsetTabular *>(innerInsetOfType(InsetBase::TABULAR_CODE));
332 void LCursor::resetAnchor()
338 BufferView & LCursor::bv() const
344 MathAtom const & LCursor::prevAtom() const
346 BOOST_ASSERT(pos() > 0);
347 return cell()[pos() - 1];
351 MathAtom & LCursor::prevAtom()
353 BOOST_ASSERT(pos() > 0);
354 return cell()[pos() - 1];
358 MathAtom const & LCursor::nextAtom() const
360 BOOST_ASSERT(pos() < lastpos());
361 return cell()[pos()];
365 MathAtom & LCursor::nextAtom()
367 BOOST_ASSERT(pos() < lastpos());
368 return cell()[pos()];
372 bool LCursor::posLeft()
381 bool LCursor::posRight()
383 if (pos() == lastpos())
390 CursorSlice & LCursor::anchor()
392 return anchor_.back();
396 CursorSlice const & LCursor::anchor() const
398 return anchor_.back();
402 CursorSlice const & LCursor::selBegin() const
405 return cursor_.back();
406 // can't use std::min as this creates a new object
407 return anchor() < cursor_.back() ? anchor() : cursor_.back();
411 CursorSlice & LCursor::selBegin()
414 return cursor_.back();
415 return anchor() < cursor_.back() ? anchor() : cursor_.back();
419 CursorSlice const & LCursor::selEnd() const
422 return cursor_.back();
423 return anchor() > cursor_.back() ? anchor() : cursor_.back();
427 CursorSlice & LCursor::selEnd()
430 return cursor_.back();
431 return anchor() > cursor_.back() ? anchor() : cursor_.back();
435 void LCursor::setSelection()
438 // a selection with no contents is not a selection
439 if (par() == anchor().par() && pos() == anchor().pos())
444 void LCursor::setSelection(CursorBase const & where, size_t n)
453 void LCursor::clearSelection()
462 int & LCursor::x_target()
468 int LCursor::x_target() const
474 void LCursor::clearTargetX()
480 LyXText * LCursor::text() const
482 return current_ ? current().text() : bv_->text();
486 Paragraph & LCursor::paragraph()
488 BOOST_ASSERT(!inMathed());
489 return current_ ? current().paragraph() : *bv_->text()->getPar(par());
493 Paragraph const & LCursor::paragraph() const
495 BOOST_ASSERT(!inMathed());
496 return current_ ? current().paragraph() : *bv_->text()->getPar(par());
500 LCursor::par_type LCursor::lastpar() const
502 return inMathed() ? 0 : text()->paragraphs().size() - 1;
506 LCursor::pos_type LCursor::lastpos() const
508 InsetBase * inset = current().inset();
509 return inset && inset->asMathInset() ? cell().size() : paragraph().size();
513 LCursor::row_type LCursor::crow() const
515 return paragraph().row(pos());
519 LCursor::row_type LCursor::lastcrow() const
521 return paragraph().rows.size();
525 size_t LCursor::nargs() const
527 // assume 1x1 grid for 'plain text'
528 return current_ ? current().nargs() : 1;
532 size_t LCursor::ncols() const
534 // assume 1x1 grid for 'plain text'
535 return current_ ? current().ncols() : 1;
539 size_t LCursor::nrows() const
541 // assume 1x1 grid for 'plain text'
542 return current_ ? current().nrows() : 1;
546 LCursor::row_type LCursor::row() const
548 BOOST_ASSERT(current_ > 0);
549 return current().row();
553 LCursor::col_type LCursor::col() const
555 BOOST_ASSERT(current_ > 0);
556 return current().col();
560 MathArray const & LCursor::cell() const
562 BOOST_ASSERT(current_ > 0);
563 return current().cell();
567 MathArray & LCursor::cell()
569 BOOST_ASSERT(current_ > 0);
570 return current().cell();
574 void LCursor::info(std::ostream & os)
576 for (int i = 1, n = depth(); i < n; ++i) {
577 cursor_[i].inset()->infoize(os);
581 prevInset()->infoize2(os);
582 // overwite old message
589 void region(CursorSlice const & i1, CursorSlice const & i2,
590 LCursor::row_type & r1, LCursor::row_type & r2,
591 LCursor::col_type & c1, LCursor::col_type & c2)
593 InsetBase * p = i1.inset();
594 c1 = p->col(i1.idx_);
595 c2 = p->col(i2.idx_);
598 r1 = p->row(i1.idx_);
599 r2 = p->row(i2.idx_);
607 string LCursor::grabSelection()
612 CursorSlice i1 = selBegin();
613 CursorSlice i2 = selEnd();
615 if (i1.idx_ == i2.idx_) {
616 if (i1.inset()->asMathInset()) {
617 MathArray::const_iterator it = i1.cell().begin();
618 return asString(MathArray(it + i1.pos_, it + i2.pos_));
620 return "unknown selection 1";
626 region(i1, i2, r1, r2, c1, c2);
629 if (i1.inset()->asMathInset()) {
630 for (row_type row = r1; row <= r2; ++row) {
633 for (col_type col = c1; col <= c2; ++col) {
636 data += asString(i1.asMathInset()->cell(i1.asMathInset()->index(row, col)));
640 data = "unknown selection 2";
646 void LCursor::eraseSelection()
648 CursorSlice i1 = selBegin();
649 CursorSlice i2 = selEnd();
651 if (i1.inset()->asMathInset()) {
652 if (i1.idx_ == i2.idx_) {
653 i1.cell().erase(i1.pos_, i2.pos_);
655 MathInset * p = i1.asMathInset();
658 region(i1, i2, r1, r2, c1, c2);
659 for (row_type row = r1; row <= r2; ++row)
660 for (col_type col = c1; col <= c2; ++col)
661 p->cell(p->index(row, col)).clear();
665 lyxerr << "can't erase this selection 1" << endl;
670 string LCursor::grabAndEraseSelection()
674 string res = grabSelection();
681 void LCursor::selClear()
688 void LCursor::selCopy()
691 theCutBuffer.push(grabSelection());
694 //theCutBuffer.erase();
699 void LCursor::selCut()
701 theCutBuffer.push(grabAndEraseSelection());
705 void LCursor::selDel()
714 void LCursor::selPaste(size_t n)
717 if (n < theCutBuffer.size())
718 paste(theCutBuffer[n]);
724 void LCursor::selHandle(bool sel)
726 if (sel == selection())
733 void LCursor::selStart()
740 void LCursor::selClearOrDel()
742 if (lyxrc.auto_region_delete)
749 std::ostream & operator<<(std::ostream & os, LCursor const & cur)
752 for (size_t i = 0, n = cur.cursor_.size(); i != n; ++i)
753 os << " (" << cur.cursor_[i] << " | " << cur.anchor_[i] << "\n";
754 return os << "current: " << cur.current_ << endl;
765 void increment(CursorBase & it)
767 CursorSlice & top = it.back();
768 MathArray & ar = top.asMathInset()->cell(top.idx_);
770 // move into the current inset if possible
771 // it is impossible for pos() == size()!
773 if (top.pos() != top.lastpos())
774 n = (ar.begin() + top.pos_)->nucleus();
775 if (n && n->isActive()) {
776 it.push_back(CursorSlice(n));
780 // otherwise move on one cell back if possible
781 if (top.pos() < top.lastpos()) {
782 // pos() == lastpos() is valid!
787 // otherwise try to move on one cell if possible
788 while (top.idx() < top.lastidx()) {
790 if (top.asMathInset()->validCell(top.idx_)) {
796 // otherwise leave array, move on one back
797 // this might yield pos() == size(), but that's a ok.
799 // it certainly invalidates top
804 CursorBase ibegin(InsetBase * p)
807 it.push_back(CursorSlice(p));
812 CursorBase iend(InsetBase * p)
815 it.push_back(CursorSlice(p));
816 CursorSlice & cur = it.back();
817 cur.idx() = cur.lastidx();
818 cur.pos() = cur.lastpos();
825 ///////////////////////////////////////////////////////////////////
827 // The part below is the non-integrated rest of the original math
828 // cursor. This should be either generalized for texted or moved
829 // back to the math insets.
831 ///////////////////////////////////////////////////////////////////
833 #include "mathed/math_braceinset.h"
834 #include "mathed/math_charinset.h"
835 #include "mathed/math_commentinset.h"
836 #include "mathed/math_factory.h"
837 #include "mathed/math_gridinset.h"
838 #include "mathed/math_macroarg.h"
839 #include "mathed/math_macrotemplate.h"
840 #include "mathed/math_mathmlstream.h"
841 #include "mathed/math_scriptinset.h"
842 #include "mathed/math_spaceinset.h"
843 #include "mathed/math_support.h"
844 #include "mathed/math_unknowninset.h"
846 //#define FILEDEBUG 1
849 bool LCursor::isInside(InsetBase const * p)
851 for (unsigned i = 0; i < depth(); ++i)
852 if (cursor_[i].inset() == p)
858 bool LCursor::openable(MathAtom const & t)
869 // we can't move into anything new during selection
870 if (depth() == anchor_.size())
872 if (t.nucleus() != anchor_[depth()].inset())
879 bool LCursor::inNucleus()
881 return inset()->asMathInset()->asScriptInset() && idx() == 2;
887 autocorrect() = false;
894 if (pos() != 0 && openable(prevAtom())) {
896 push(nextAtom().nucleus());
897 inset()->idxLast(*this);
901 return posLeft() || idxLeft() || popLeft() || selection();
905 bool LCursor::right()
907 autocorrect() = false;
914 if (pos() != lastpos() && openable(nextAtom())) {
915 pushLeft(nextAtom().nucleus());
916 inset()->idxFirst(*this);
920 return posRight() || idxRight() || popRight() || selection();
924 bool positionable(CursorBase const & cursor, CursorBase const & anchor)
926 // avoid deeper nested insets when selecting
927 if (cursor.size() > anchor.size())
930 // anchor might be deeper, should have same path then
931 for (size_t i = 0; i < cursor.size(); ++i)
932 if (cursor[i].inset() != anchor[i].inset())
935 // position should be ok.
940 void LCursor::setScreenPos(int x, int y)
942 bool res = bruteFind(x, y, formula()->xlow(), formula()->xhigh(),
943 formula()->ylow(), formula()->yhigh());
945 // this can happen on creation of "math-display"
956 autocorrect() = false;
958 if (!inset()->idxHome(*this))
967 autocorrect() = false;
969 if (!inset()->idxEnd(*this))
976 void LCursor::plainErase()
982 void LCursor::markInsert()
984 cell().insert(pos(), MathAtom(new MathCharInset(0)));
988 void LCursor::markErase()
994 void LCursor::plainInsert(MathAtom const & t)
996 cell().insert(pos(), t);
1001 void LCursor::insert2(string const & str)
1009 void LCursor::insert(string const & str)
1011 lyxerr << "inserting '" << str << "'" << endl;
1013 for (string::const_iterator it = str.begin(); it != str.end(); ++it)
1014 plainInsert(MathAtom(new MathCharInset(*it)));
1018 void LCursor::insert(char c)
1020 lyxerr << "inserting '" << c << "'" << endl;
1022 plainInsert(MathAtom(new MathCharInset(c)));
1026 void LCursor::insert(MathAtom const & t)
1034 void LCursor::niceInsert(string const & t)
1036 lyxerr << "*** LCursor::niceInsert 1: " << t << endl;
1046 void LCursor::niceInsert(MathAtom const & t)
1048 lyxerr << "*** LCursor::niceInsert 2: " << t << endl;
1050 string safe = grabAndEraseSelection();
1052 // enter the new inset and move the contents of the selection if possible
1053 if (t->isActive()) {
1055 pushLeft(nextAtom().nucleus());
1058 lyxerr << "*** LCursor::niceInsert 3: " << t << endl;
1062 void LCursor::insert(MathArray const & ar)
1067 cell().insert(pos(), ar);
1072 bool LCursor::backspace()
1074 autocorrect() = false;
1082 if (inset()->nargs() == 1 && depth() == 1 && lastpos() == 0)
1088 if (inMacroMode()) {
1089 MathUnknownInset * p = activeMacro();
1090 if (p->name().size() > 1) {
1091 p->setName(p->name().substr(0, p->name().size() - 1));
1096 if (pos() != 0 && prevAtom()->nargs() > 0) {
1097 // let's require two backspaces for 'big stuff' and
1098 // highlight on the first
1109 bool LCursor::erase()
1111 autocorrect() = false;
1120 // delete empty cells if possible
1121 if (pos() == lastpos() && inset()->idxDelete(idx()))
1124 // special behaviour when in last position of cell
1125 if (pos() == lastpos()) {
1126 bool one_cell = inset()->nargs() == 1;
1127 if (one_cell && depth() == 1 && lastpos() == 0)
1133 inset()->idxGlue(idx());
1137 if (pos() != lastpos() && inset()->nargs() > 0) {
1151 CursorBase save = cursor_;
1155 autocorrect() = false;
1160 bool LCursor::down()
1163 CursorBase save = cursor_;
1164 if (goUpDown(false))
1167 autocorrect() = false;
1172 void LCursor::macroModeClose()
1176 MathUnknownInset * p = activeMacro();
1178 string s = p->name();
1180 cell().erase(pos());
1182 // do nothing if the macro name is empty
1186 string const name = s.substr(1);
1188 // prevent entering of recursive macros
1189 if (formula()->lyxCode() == InsetOld::MATHMACRO_CODE
1190 && formula()->getInsetName() == name)
1191 lyxerr << "can't enter recursive macro" << endl;
1193 niceInsert(createMathInset(name));
1197 string LCursor::macroName()
1199 return inMacroMode() ? activeMacro()->name() : string();
1203 void LCursor::handleNest(MathAtom const & a, int c)
1206 asArray(grabAndEraseSelection(), t.nucleus()->cell(c));
1209 pushLeft(t.nucleus());
1213 int LCursor::targetX() const
1215 if (x_target() != -1)
1224 MathHullInset * LCursor::formula() const
1226 for (int i = cursor_.size() - 1; i >= 1; --i)
1227 if (cursor_[i].inset()->lyxCode() == InsetBase::MATH_CODE)
1228 return static_cast<MathHullInset *>(cursor_[i].inset());
1233 void LCursor::adjust(pos_type from, int diff)
1237 if (anchor().pos_ > from)
1238 anchor().pos_ += diff;
1239 // just to be on the safe side
1240 // theoretically unecessary
1245 bool LCursor::inMacroMode() const
1249 MathUnknownInset const * p = prevAtom()->asUnknownInset();
1250 return p && !p->final();
1254 MathUnknownInset * LCursor::activeMacro()
1256 return inMacroMode() ? prevAtom().nucleus()->asUnknownInset() : 0;
1260 bool LCursor::inMacroArgMode() const
1262 return pos() > 0 && prevAtom()->getChar() == '#';
1266 MathGridInset * LCursor::enclosingGrid(idx_type & idx) const
1268 for (MathInset::difference_type i = depth() - 1; i >= 0; --i) {
1269 MathInset * m = cursor_[i].inset()->asMathInset();
1272 MathGridInset * p = m->asGridInset();
1274 idx = cursor_[i].idx_;
1282 void LCursor::pullArg()
1286 MathArray ar = cell();
1289 cell().insert(pos(), ar);
1292 formula()->mutateToText();
1298 void LCursor::touch()
1302 CursorBase::const_iterator it = cursor_.begin();
1303 CursorBase::const_iterator et = cursor_.end();
1304 for ( ; it != et; ++it)
1310 void LCursor::normalize()
1312 if (idx() >= nargs()) {
1313 lyxerr << "this should not really happen - 1: "
1314 << idx() << ' ' << nargs()
1315 << " in: " << inset() << endl;
1317 idx() = min(idx(), lastidx());
1319 if (pos() > lastpos()) {
1320 lyxerr << "this should not really happen - 2: "
1321 << pos() << ' ' << lastpos() << " in idx: " << idx()
1323 WriteStream wi(lyxerr, false, true);
1324 inset()->asMathInset()->write(wi);
1327 pos() = min(pos(), lastpos());
1331 char LCursor::valign()
1334 MathGridInset * p = enclosingGrid(idx);
1335 return p ? p->valign() : '\0';
1339 char LCursor::halign()
1342 MathGridInset * p = enclosingGrid(idx);
1343 return p ? p->halign(idx % p->ncols()) : '\0';
1347 bool LCursor::goUpDown(bool up)
1349 // Be warned: The 'logic' implemented in this function is highly fragile.
1350 // A distance of one pixel or a '<' vs '<=' _really_ matters.
1351 // So fiddle around with it only if you know what you are doing!
1356 // check if we had something else in mind, if not, this is the future goal
1357 if (x_target() == -1)
1362 // try neigbouring script insets
1366 MathScriptInset const * p = prevAtom()->asScriptInset();
1367 if (p && p->has(up)) {
1370 idx() = up; // the superscript has index 1
1372 //lyxerr << "updown: handled by scriptinset to the left" << endl;
1378 if (pos() != lastpos()) {
1379 MathScriptInset const * p = nextAtom()->asScriptInset();
1380 if (p && p->has(up)) {
1384 //lyxerr << "updown: handled by scriptinset to the right" << endl;
1390 // try current cell for e.g. text insets
1391 if (inset()->idxUpDown2(*this, up))
1394 //xarray().boundingBox(xlow, xhigh, ylow, yhigh);
1399 //if (bruteFind(xo, yo, xlow, xhigh, ylow, yhigh)) {
1400 // lyxerr << "updown: handled by brute find in the same cell" << endl;
1404 // try to find an inset that knows better then we
1406 //lyxerr << "updown: We are in " << inset() << " idx: " << idx() << endl;
1408 if (inset()->idxUpDown(*this, up)) {
1409 // try to find best position within this inset
1415 // no such inset found, just take something "above"
1416 //lyxerr << "updown: handled by strange case" << endl;
1422 up ? formula()->ylow() : yo + 4,
1423 up ? yo - 4 : formula()->yhigh()
1427 // any improvement so far?
1430 if (up ? ynew < yo : ynew > yo)
1436 bool LCursor::bruteFind(int x, int y, int xlow, int xhigh, int ylow, int yhigh)
1438 CursorBase best_cursor;
1439 double best_dist = 1e10;
1441 CursorBase it = ibegin(formula());
1442 CursorBase et = iend(formula());
1444 // avoid invalid nesting when selecting
1445 if (!selection() || positionable(it, anchor_)) {
1447 CursorSlice & cur = it.back();
1448 cur.inset()->getCursorPos(cur, xo, yo);
1449 if (xlow <= xo && xo <= xhigh && ylow <= yo && yo <= yhigh) {
1450 double d = (x - xo) * (x - xo) + (y - yo) * (y - yo);
1451 //lyxerr << "x: " << x << " y: " << y << " d: " << endl;
1452 // '<=' in order to take the last possible position
1453 // this is important for clicking behind \sum in e.g. '\sum_i a'
1454 if (d <= best_dist) {
1466 if (best_dist < 1e10)
1467 cursor_ = best_cursor;
1468 return best_dist < 1e10;
1472 void LCursor::bruteFind2(int x, int y)
1474 double best_dist = 1e10;
1476 CursorBase it = cursor_;
1478 CursorBase et = cursor_;
1479 int n = et.back().asMathInset()->cell(et.back().idx_).size();
1481 for (int i = 0; ; ++i) {
1483 CursorSlice & cur = it.back();
1484 cur.inset()->getCursorPos(cur, xo, yo);
1485 double d = (x - xo) * (x - xo) + (y - yo) * (y - yo);
1486 // '<=' in order to take the last possible position
1487 // this is important for clicking behind \sum in e.g. '\sum_i a'
1488 lyxerr << "i: " << i << " d: " << d << " best: " << best_dist << endl;
1489 if (d <= best_dist) {
1500 bool LCursor::idxLineLast()
1502 idx() -= idx() % ncols();
1503 idx() += ncols() - 1;
1509 bool LCursor::idxLeft()
1511 return inset()->idxLeft(*this);
1515 bool LCursor::idxRight()
1517 return inset()->idxRight(*this);
1521 bool LCursor::script(bool up)
1523 // Hack to get \\^ and \\_ working
1524 lyxerr << "handling script: up: " << up << endl;
1525 if (inMacroMode() && macroName() == "\\") {
1527 niceInsert(createMathInset("mathcircumflex"));
1534 string safe = grabAndEraseSelection();
1536 // we are in a nucleus of a script inset, move to _our_ script
1537 inset()->asMathInset()->asScriptInset()->ensure(up);
1540 } else if (pos() != 0 && prevAtom()->asScriptInset()) {
1542 nextAtom().nucleus()->asScriptInset()->ensure(up);
1546 } else if (pos() != 0) {
1548 cell()[pos()] = MathAtom(new MathScriptInset(nextAtom(), up));
1553 plainInsert(MathAtom(new MathScriptInset(up)));
1555 nextAtom().nucleus()->asScriptInset()->ensure(up);
1565 bool LCursor::interpret(char c)
1567 //lyxerr << "interpret 2: '" << c << "'" << endl;
1569 if (inMacroArgMode()) {
1575 MathMacroTemplate const * p = formula()->asMacroTemplate();
1576 if (p && 1 <= n && n <= p->numargs())
1577 insert(MathAtom(new MathMacroArgument(c - '0')));
1579 insert(createMathInset("#"));
1580 interpret(c); // try again
1587 if (inMacroMode()) {
1588 string name = macroName();
1589 //lyxerr << "interpret name: '" << name << "'" << endl;
1592 activeMacro()->setName(activeMacro()->name() + c);
1596 // handle 'special char' macros
1601 if (currentMode() == MathInset::TEXT_MODE)
1602 niceInsert(createMathInset("textbackslash"));
1604 niceInsert(createMathInset("backslash"));
1605 } else if (c == '{') {
1606 niceInsert(MathAtom(new MathBraceInset));
1608 niceInsert(createMathInset(string(1, c)));
1613 // leave macro mode and try again if necessary
1616 niceInsert(MathAtom(new MathBraceInset));
1622 // This is annoying as one has to press <space> far too often.
1626 // leave autocorrect mode if necessary
1627 if (autocorrect() && c == ' ') {
1628 autocorrect() = false;
1633 // just clear selection on pressing the space bar
1634 if (selection() && c == ' ') {
1635 selection() = false;
1642 //lyxerr << "starting with macro" << endl;
1643 insert(MathAtom(new MathUnknownInset("\\", false)));
1648 if (currentMode() == MathInset::TEXT_MODE)
1654 if (currentMode() == MathInset::TEXT_MODE) {
1655 // insert spaces in text mode,
1656 // but suppress direct insertion of two spaces in a row
1657 // the still allows typing '<space>a<space>' and deleting the 'a', but
1658 // it is better than nothing...
1659 if (!pos() != 0 || prevAtom()->getChar() != ' ')
1663 if (pos() != 0 && prevAtom()->asSpaceInset()) {
1664 prevAtom().nucleus()->asSpaceInset()->incSpace();
1669 // if are at the very end, leave the formula
1670 return pos() != lastpos();
1683 if (c == '{' || c == '}' || c == '#' || c == '&' || c == '$') {
1684 niceInsert(createMathInset(string(1, c)));
1689 niceInsert(MathAtom(new MathCommentInset));
1693 // try auto-correction
1694 //if (autocorrect() && hasPrevAtom() && math_autocorrect(prevAtom(), c))
1697 // no special circumstances, so insert the character without any fuss
1699 autocorrect() = true;
1704 void LCursor::lockToggle()
1706 if (pos() != lastpos()) {
1707 // toggle previous inset ...
1708 nextAtom().nucleus()->lock(!nextAtom()->lock());
1709 } else if (popLeft() && pos() != lastpos()) {
1710 // ... or enclosing inset if we are in the last inset position
1711 nextAtom().nucleus()->lock(!nextAtom()->lock());
1717 CursorSlice LCursor::normalAnchor()
1719 if (anchor_.size() < depth()) {
1721 lyxerr << "unusual Anchor size" << endl;
1723 //lyx::BOOST_ASSERT(Anchor_.size() >= cursor.depth());
1724 // use Anchor on the same level as Cursor
1725 CursorSlice normal = anchor_[current_];
1727 if (depth() < anchor_.size() && !(normal < xx())) {
1728 // anchor is behind cursor -> move anchor behind the inset
1737 DispatchResult dispatch(LCursor & cur, FuncRequest const & cmd)
1739 // mouse clicks are somewhat special
1741 switch (cmd.action) {
1742 case LFUN_MOUSE_PRESS:
1743 case LFUN_MOUSE_MOTION:
1744 case LFUN_MOUSE_RELEASE:
1745 case LFUN_MOUSE_DOUBLE: {
1746 CursorSlice & pos = cursor_.back();
1750 if (x < cmd.x && pos() != 0) {
1751 DispatchResult const res = prevAtom().nucleus()->dispatch(cmd);
1752 if (res.dispatched())
1755 if (x > cmd.x && pos() != lastpos()) {
1756 DispatchResult const res = inset()->dispatch(cmd);
1757 if (res.dispatched())
1768 void LCursor::handleFont(string const & font)
1773 safe = grabAndEraseSelection();
1776 if (lastpos() != 0) {
1777 // something left in the cell
1779 // cursor in first position
1781 } else if (pos() == lastpos()) {
1782 // cursor in last position
1785 // cursor in between. split cell
1786 MathArray::iterator bt = cell().begin();
1787 MathAtom at = createMathInset(font);
1788 at.nucleus()->cell(0) = MathArray(bt, bt + pos());
1789 cell().erase(bt, bt + pos());
1794 // nothing left in the cell
1802 void LCursor::releaseMathCursor()
1805 formula()->insetUnlock(bv());
1809 bool LCursor::inMathed() const
1815 InsetBase * LCursor::nextInset()
1817 if (pos() == lastpos())
1820 return nextAtom().nucleus();
1821 Paragraph & par = paragraph();
1822 return par.isInset(pos()) ? par.getInset(pos()) : 0;
1826 InsetBase * LCursor::prevInset()
1831 return prevAtom().nucleus();
1832 Paragraph & par = paragraph();
1833 return par.isInset(pos() - 1) ? par.getInset(pos() - 1) : 0;
1837 void LCursor::message(string const & msg) const
1839 bv().owner()->getLyXFunc().setMessage(msg);
1843 void LCursor::errorMessage(string const & msg) const
1845 bv().owner()->getLyXFunc().setErrorMessage(msg);