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: x: " << x << " y: " << y << endl;
310 void LCursor::paste(string const & data)
312 dispatch(FuncRequest(LFUN_PASTE, data));
316 InsetBase * LCursor::innerInsetOfType(int code) const
318 for (int i = cursor_.size() - 1; i >= 1; --i)
319 if (cursor_[i].inset_->lyxCode() == code)
320 return cursor_[i].inset_;
325 InsetTabular * LCursor::innerInsetTabular() const
327 return static_cast<InsetTabular *>(innerInsetOfType(InsetBase::TABULAR_CODE));
331 void LCursor::resetAnchor()
337 BufferView & LCursor::bv() const
343 MathAtom const & LCursor::prevAtom() const
345 BOOST_ASSERT(pos() > 0);
346 return cell()[pos() - 1];
350 MathAtom & LCursor::prevAtom()
352 BOOST_ASSERT(pos() > 0);
353 return cell()[pos() - 1];
357 MathAtom const & LCursor::nextAtom() const
359 BOOST_ASSERT(pos() < lastpos());
360 return cell()[pos()];
364 MathAtom & LCursor::nextAtom()
366 BOOST_ASSERT(pos() < lastpos());
367 return cell()[pos()];
371 bool LCursor::posLeft()
380 bool LCursor::posRight()
382 if (pos() == lastpos())
389 CursorSlice & LCursor::anchor()
391 return anchor_.back();
395 CursorSlice const & LCursor::anchor() const
397 return anchor_.back();
401 CursorSlice const & LCursor::selBegin() const
404 return cursor_.back();
405 // can't use std::min as this creates a new object
406 return anchor() < cursor_.back() ? anchor() : cursor_.back();
410 CursorSlice & LCursor::selBegin()
413 return cursor_.back();
414 return anchor() < cursor_.back() ? anchor() : cursor_.back();
418 CursorSlice const & LCursor::selEnd() const
421 return cursor_.back();
422 return anchor() > cursor_.back() ? anchor() : cursor_.back();
426 CursorSlice & LCursor::selEnd()
429 return cursor_.back();
430 return anchor() > cursor_.back() ? anchor() : cursor_.back();
434 void LCursor::setSelection()
437 // a selection with no contents is not a selection
438 if (par() == anchor().par() && pos() == anchor().pos())
443 void LCursor::setSelection(CursorBase const & where, size_t n)
452 void LCursor::clearSelection()
461 int & LCursor::x_target()
467 int LCursor::x_target() const
473 void LCursor::clearTargetX()
479 LyXText * LCursor::text() const
481 return current_ ? current().text() : bv_->text();
485 Paragraph & LCursor::paragraph()
487 BOOST_ASSERT(!inMathed());
488 return current_ ? current().paragraph() : *bv_->text()->getPar(par());
492 Paragraph const & LCursor::paragraph() const
494 BOOST_ASSERT(!inMathed());
495 return current_ ? current().paragraph() : *bv_->text()->getPar(par());
499 LCursor::par_type LCursor::lastpar() const
501 return inMathed() ? 0 : text()->paragraphs().size() - 1;
505 LCursor::pos_type LCursor::lastpos() const
507 InsetBase * inset = current().inset();
508 return inset && inset->asMathInset() ? cell().size() : paragraph().size();
512 LCursor::row_type LCursor::crow() const
514 return paragraph().row(pos());
518 LCursor::row_type LCursor::lastcrow() const
520 return paragraph().rows.size();
524 size_t LCursor::nargs() const
526 // assume 1x1 grid for 'plain text'
527 return current_ ? current().nargs() : 1;
531 size_t LCursor::ncols() const
533 // assume 1x1 grid for 'plain text'
534 return current_ ? current().ncols() : 1;
538 size_t LCursor::nrows() const
540 // assume 1x1 grid for 'plain text'
541 return current_ ? current().nrows() : 1;
545 LCursor::row_type LCursor::row() const
547 BOOST_ASSERT(current_ > 0);
548 return current().row();
552 LCursor::col_type LCursor::col() const
554 BOOST_ASSERT(current_ > 0);
555 return current().col();
559 MathArray const & LCursor::cell() const
561 BOOST_ASSERT(current_ > 0);
562 return current().cell();
566 MathArray & LCursor::cell()
568 BOOST_ASSERT(current_ > 0);
569 return current().cell();
573 void LCursor::info(std::ostream & os)
575 for (int i = 1, n = depth(); i < n; ++i) {
576 cursor_[i].inset()->infoize(os);
580 prevInset()->infoize2(os);
581 // overwite old message
588 void region(CursorSlice const & i1, CursorSlice const & i2,
589 LCursor::row_type & r1, LCursor::row_type & r2,
590 LCursor::col_type & c1, LCursor::col_type & c2)
592 InsetBase * p = i1.inset();
593 c1 = p->col(i1.idx_);
594 c2 = p->col(i2.idx_);
597 r1 = p->row(i1.idx_);
598 r2 = p->row(i2.idx_);
606 string LCursor::grabSelection()
611 CursorSlice i1 = selBegin();
612 CursorSlice i2 = selEnd();
614 if (i1.idx_ == i2.idx_) {
615 if (i1.inset()->asMathInset()) {
616 MathArray::const_iterator it = i1.cell().begin();
617 return asString(MathArray(it + i1.pos_, it + i2.pos_));
619 return "unknown selection 1";
625 region(i1, i2, r1, r2, c1, c2);
628 if (i1.inset()->asMathInset()) {
629 for (row_type row = r1; row <= r2; ++row) {
632 for (col_type col = c1; col <= c2; ++col) {
635 data += asString(i1.asMathInset()->cell(i1.asMathInset()->index(row, col)));
639 data = "unknown selection 2";
645 void LCursor::eraseSelection()
647 CursorSlice i1 = selBegin();
648 CursorSlice i2 = selEnd();
650 if (i1.inset()->asMathInset()) {
651 if (i1.idx_ == i2.idx_) {
652 i1.cell().erase(i1.pos_, i2.pos_);
654 MathInset * p = i1.asMathInset();
657 region(i1, i2, r1, r2, c1, c2);
658 for (row_type row = r1; row <= r2; ++row)
659 for (col_type col = c1; col <= c2; ++col)
660 p->cell(p->index(row, col)).clear();
664 lyxerr << "can't erase this selection 1" << endl;
669 string LCursor::grabAndEraseSelection()
673 string res = grabSelection();
680 void LCursor::selClear()
687 void LCursor::selCopy()
690 theCutBuffer.push(grabSelection());
693 //theCutBuffer.erase();
698 void LCursor::selCut()
700 theCutBuffer.push(grabAndEraseSelection());
704 void LCursor::selDel()
713 void LCursor::selPaste(size_t n)
716 if (n < theCutBuffer.size())
717 paste(theCutBuffer[n]);
723 void LCursor::selHandle(bool sel)
725 if (sel == selection())
732 void LCursor::selStart()
739 void LCursor::selClearOrDel()
741 if (lyxrc.auto_region_delete)
748 std::ostream & operator<<(std::ostream & os, LCursor const & cur)
751 for (size_t i = 0, n = cur.cursor_.size(); i != n; ++i)
752 os << " (" << cur.cursor_[i] << " | " << cur.anchor_[i] << "\n";
753 return os << "current: " << cur.current_ << endl;
764 void increment(CursorBase & it)
766 CursorSlice & top = it.back();
767 MathArray & ar = top.asMathInset()->cell(top.idx_);
769 // move into the current inset if possible
770 // it is impossible for pos() == size()!
772 if (top.pos() != top.lastpos())
773 n = (ar.begin() + top.pos_)->nucleus();
774 if (n && n->isActive()) {
775 it.push_back(CursorSlice(n));
779 // otherwise move on one cell back if possible
780 if (top.pos() < top.lastpos()) {
781 // pos() == lastpos() is valid!
786 // otherwise try to move on one cell if possible
787 while (top.idx() < top.lastidx()) {
789 if (top.asMathInset()->validCell(top.idx_)) {
795 // otherwise leave array, move on one back
796 // this might yield pos() == size(), but that's a ok.
798 // it certainly invalidates top
803 CursorBase ibegin(InsetBase * p)
806 it.push_back(CursorSlice(p));
811 CursorBase iend(InsetBase * p)
814 it.push_back(CursorSlice(p));
815 CursorSlice & cur = it.back();
816 cur.idx() = cur.lastidx();
817 cur.pos() = cur.lastpos();
824 ///////////////////////////////////////////////////////////////////
826 // The part below is the non-integrated rest of the original math
827 // cursor. This should be either generalized for texted or moved
828 // back to the math insets.
830 ///////////////////////////////////////////////////////////////////
832 #include "mathed/math_braceinset.h"
833 #include "mathed/math_charinset.h"
834 #include "mathed/math_commentinset.h"
835 #include "mathed/math_factory.h"
836 #include "mathed/math_gridinset.h"
837 #include "mathed/math_macroarg.h"
838 #include "mathed/math_macrotemplate.h"
839 #include "mathed/math_mathmlstream.h"
840 #include "mathed/math_scriptinset.h"
841 #include "mathed/math_spaceinset.h"
842 #include "mathed/math_support.h"
843 #include "mathed/math_unknowninset.h"
845 //#define FILEDEBUG 1
848 bool LCursor::isInside(InsetBase const * p)
850 for (unsigned i = 0; i < depth(); ++i)
851 if (cursor_[i].inset() == p)
857 bool LCursor::openable(MathAtom const & t)
868 // we can't move into anything new during selection
869 if (depth() == anchor_.size())
871 if (t.nucleus() != anchor_[depth()].inset())
878 bool LCursor::inNucleus()
880 return inset()->asMathInset()->asScriptInset() && idx() == 2;
886 autocorrect() = false;
893 if (pos() != 0 && openable(prevAtom())) {
895 push(nextAtom().nucleus());
896 inset()->idxLast(*this);
900 return posLeft() || idxLeft() || popLeft() || selection();
904 bool LCursor::right()
906 autocorrect() = false;
913 if (pos() != lastpos() && openable(nextAtom())) {
914 pushLeft(nextAtom().nucleus());
915 inset()->idxFirst(*this);
919 return posRight() || idxRight() || popRight() || selection();
923 bool positionable(CursorBase const & cursor, CursorBase const & anchor)
925 // avoid deeper nested insets when selecting
926 if (cursor.size() > anchor.size())
929 // anchor might be deeper, should have same path then
930 for (size_t i = 0; i < cursor.size(); ++i)
931 if (cursor[i].inset() != anchor[i].inset())
934 // position should be ok.
939 void LCursor::setScreenPos(int x, int y)
941 bool res = bruteFind(x, y, formula()->xlow(), formula()->xhigh(),
942 formula()->ylow(), formula()->yhigh());
944 // this can happen on creation of "math-display"
955 autocorrect() = false;
957 if (!inset()->idxHome(*this))
966 autocorrect() = false;
968 if (!inset()->idxEnd(*this))
975 void LCursor::plainErase()
981 void LCursor::markInsert()
983 cell().insert(pos(), MathAtom(new MathCharInset(0)));
987 void LCursor::markErase()
993 void LCursor::plainInsert(MathAtom const & t)
995 cell().insert(pos(), t);
1000 void LCursor::insert2(string const & str)
1008 void LCursor::insert(string const & str)
1010 lyxerr << "inserting '" << str << "'" << endl;
1012 for (string::const_iterator it = str.begin(); it != str.end(); ++it)
1013 plainInsert(MathAtom(new MathCharInset(*it)));
1017 void LCursor::insert(char c)
1019 lyxerr << "inserting '" << c << "'" << endl;
1021 plainInsert(MathAtom(new MathCharInset(c)));
1025 void LCursor::insert(MathAtom const & t)
1033 void LCursor::niceInsert(string const & t)
1035 lyxerr << "*** LCursor::niceInsert 1: " << t << endl;
1045 void LCursor::niceInsert(MathAtom const & t)
1047 lyxerr << "*** LCursor::niceInsert 2: " << t << endl;
1049 string safe = grabAndEraseSelection();
1051 // enter the new inset and move the contents of the selection if possible
1052 if (t->isActive()) {
1054 pushLeft(nextAtom().nucleus());
1057 lyxerr << "*** LCursor::niceInsert 3: " << t << endl;
1061 void LCursor::insert(MathArray const & ar)
1066 cell().insert(pos(), ar);
1071 bool LCursor::backspace()
1073 autocorrect() = false;
1081 if (inset()->nargs() == 1 && depth() == 1 && lastpos() == 0)
1087 if (inMacroMode()) {
1088 MathUnknownInset * p = activeMacro();
1089 if (p->name().size() > 1) {
1090 p->setName(p->name().substr(0, p->name().size() - 1));
1095 if (pos() != 0 && prevAtom()->nargs() > 0) {
1096 // let's require two backspaces for 'big stuff' and
1097 // highlight on the first
1108 bool LCursor::erase()
1110 autocorrect() = false;
1119 // delete empty cells if possible
1120 if (pos() == lastpos() && inset()->idxDelete(idx()))
1123 // special behaviour when in last position of cell
1124 if (pos() == lastpos()) {
1125 bool one_cell = inset()->nargs() == 1;
1126 if (one_cell && depth() == 1 && lastpos() == 0)
1132 inset()->idxGlue(idx());
1136 if (pos() != lastpos() && inset()->nargs() > 0) {
1150 CursorBase save = cursor_;
1154 autocorrect() = false;
1159 bool LCursor::down()
1162 CursorBase save = cursor_;
1163 if (goUpDown(false))
1166 autocorrect() = false;
1171 void LCursor::macroModeClose()
1175 MathUnknownInset * p = activeMacro();
1177 string s = p->name();
1179 cell().erase(pos());
1181 // do nothing if the macro name is empty
1185 string const name = s.substr(1);
1187 // prevent entering of recursive macros
1188 if (formula()->lyxCode() == InsetOld::MATHMACRO_CODE
1189 && formula()->getInsetName() == name)
1190 lyxerr << "can't enter recursive macro" << endl;
1192 niceInsert(createMathInset(name));
1196 string LCursor::macroName()
1198 return inMacroMode() ? activeMacro()->name() : string();
1202 void LCursor::handleNest(MathAtom const & a, int c)
1205 asArray(grabAndEraseSelection(), t.nucleus()->cell(c));
1208 pushLeft(t.nucleus());
1212 int LCursor::targetX() const
1214 if (x_target() != -1)
1223 MathHullInset * LCursor::formula() const
1225 for (int i = cursor_.size() - 1; i >= 1; --i)
1226 if (cursor_[i].inset()->lyxCode() == InsetBase::MATH_CODE)
1227 return static_cast<MathHullInset *>(cursor_[i].inset());
1232 void LCursor::adjust(pos_type from, int diff)
1236 if (anchor().pos_ > from)
1237 anchor().pos_ += diff;
1238 // just to be on the safe side
1239 // theoretically unecessary
1244 bool LCursor::inMacroMode() const
1248 MathUnknownInset const * p = prevAtom()->asUnknownInset();
1249 return p && !p->final();
1253 MathUnknownInset * LCursor::activeMacro()
1255 return inMacroMode() ? prevAtom().nucleus()->asUnknownInset() : 0;
1259 bool LCursor::inMacroArgMode() const
1261 return pos() > 0 && prevAtom()->getChar() == '#';
1265 MathGridInset * LCursor::enclosingGrid(idx_type & idx) const
1267 for (MathInset::difference_type i = depth() - 1; i >= 0; --i) {
1268 MathInset * m = cursor_[i].inset()->asMathInset();
1271 MathGridInset * p = m->asGridInset();
1273 idx = cursor_[i].idx_;
1281 void LCursor::pullArg()
1285 MathArray ar = cell();
1288 cell().insert(pos(), ar);
1291 formula()->mutateToText();
1297 void LCursor::touch()
1301 CursorBase::const_iterator it = cursor_.begin();
1302 CursorBase::const_iterator et = cursor_.end();
1303 for ( ; it != et; ++it)
1309 void LCursor::normalize()
1311 if (idx() >= nargs()) {
1312 lyxerr << "this should not really happen - 1: "
1313 << idx() << ' ' << nargs()
1314 << " in: " << inset() << endl;
1316 idx() = min(idx(), nargs() - 1);
1318 if (pos() > lastpos()) {
1319 lyxerr << "this should not really happen - 2: "
1320 << pos() << ' ' << lastpos() << " in idx: " << idx()
1322 WriteStream wi(lyxerr, false, true);
1323 inset()->asMathInset()->write(wi);
1326 pos() = min(pos(), lastpos());
1330 char LCursor::valign()
1333 MathGridInset * p = enclosingGrid(idx);
1334 return p ? p->valign() : '\0';
1338 char LCursor::halign()
1341 MathGridInset * p = enclosingGrid(idx);
1342 return p ? p->halign(idx % p->ncols()) : '\0';
1346 bool LCursor::goUpDown(bool up)
1348 // Be warned: The 'logic' implemented in this function is highly fragile.
1349 // A distance of one pixel or a '<' vs '<=' _really_ matters.
1350 // So fiddle around with it only if you know what you are doing!
1355 // check if we had something else in mind, if not, this is the future goal
1356 if (x_target() == -1)
1361 // try neigbouring script insets
1365 MathScriptInset const * p = prevAtom()->asScriptInset();
1366 if (p && p->has(up)) {
1369 idx() = up; // the superscript has index 1
1371 //lyxerr << "updown: handled by scriptinset to the left" << endl;
1377 if (pos() != lastpos()) {
1378 MathScriptInset const * p = nextAtom()->asScriptInset();
1379 if (p && p->has(up)) {
1383 //lyxerr << "updown: handled by scriptinset to the right" << endl;
1389 // try current cell for e.g. text insets
1390 if (inset()->idxUpDown2(*this, up))
1393 //xarray().boundingBox(xlow, xhigh, ylow, yhigh);
1398 //if (bruteFind(xo, yo, xlow, xhigh, ylow, yhigh)) {
1399 // lyxerr << "updown: handled by brute find in the same cell" << endl;
1403 // try to find an inset that knows better then we
1405 //lyxerr << "updown: We are in " << inset() << " idx: " << idx() << endl;
1407 if (inset()->idxUpDown(*this, up)) {
1408 // try to find best position within this inset
1414 // no such inset found, just take something "above"
1415 //lyxerr << "updown: handled by strange case" << endl;
1421 up ? formula()->ylow() : yo + 4,
1422 up ? yo - 4 : formula()->yhigh()
1426 // any improvement so far?
1429 if (up ? ynew < yo : ynew > yo)
1435 bool LCursor::bruteFind(int x, int y, int xlow, int xhigh, int ylow, int yhigh)
1437 CursorBase best_cursor;
1438 double best_dist = 1e10;
1440 CursorBase it = ibegin(formula());
1441 CursorBase et = iend(formula());
1443 // avoid invalid nesting when selecting
1444 if (!selection() || positionable(it, anchor_)) {
1446 CursorSlice & cur = it.back();
1447 cur.inset()->getCursorPos(cur, xo, yo);
1448 if (xlow <= xo && xo <= xhigh && ylow <= yo && yo <= yhigh) {
1449 double d = (x - xo) * (x - xo) + (y - yo) * (y - yo);
1450 //lyxerr << "x: " << x << " y: " << y << " d: " << endl;
1451 // '<=' in order to take the last possible position
1452 // this is important for clicking behind \sum in e.g. '\sum_i a'
1453 if (d <= best_dist) {
1465 if (best_dist < 1e10)
1466 cursor_ = best_cursor;
1467 return best_dist < 1e10;
1471 void LCursor::bruteFind2(int x, int y)
1473 double best_dist = 1e10;
1475 CursorBase it = cursor_;
1477 CursorBase et = cursor_;
1478 int n = et.back().asMathInset()->cell(et.back().idx_).size();
1480 for (int i = 0; ; ++i) {
1482 CursorSlice & cur = it.back();
1483 cur.inset()->getCursorPos(cur, xo, yo);
1484 double d = (x - xo) * (x - xo) + (y - yo) * (y - yo);
1485 // '<=' in order to take the last possible position
1486 // this is important for clicking behind \sum in e.g. '\sum_i a'
1487 lyxerr << "i: " << i << " d: " << d << " best: " << best_dist << endl;
1488 if (d <= best_dist) {
1499 bool LCursor::idxLineLast()
1501 idx() -= idx() % ncols();
1502 idx() += ncols() - 1;
1508 bool LCursor::idxLeft()
1510 return inset()->idxLeft(*this);
1514 bool LCursor::idxRight()
1516 return inset()->idxRight(*this);
1520 bool LCursor::script(bool up)
1522 // Hack to get \\^ and \\_ working
1523 lyxerr << "handling script: up: " << up << endl;
1524 if (inMacroMode() && macroName() == "\\") {
1526 niceInsert(createMathInset("mathcircumflex"));
1533 string safe = grabAndEraseSelection();
1535 // we are in a nucleus of a script inset, move to _our_ script
1536 inset()->asMathInset()->asScriptInset()->ensure(up);
1539 } else if (pos() != 0 && prevAtom()->asScriptInset()) {
1540 prevAtom().nucleus()->asScriptInset()->ensure(up);
1545 } else if (pos() != 0) {
1547 cell()[pos()] = MathAtom(new MathScriptInset(nextAtom(), up));
1552 plainInsert(MathAtom(new MathScriptInset(up)));
1554 nextAtom().nucleus()->asScriptInset()->ensure(up);
1564 bool LCursor::interpret(char c)
1566 lyxerr << "interpret 2: '" << c << "'" << endl;
1568 if (inMacroArgMode()) {
1574 MathMacroTemplate const * p = formula()->asMacroTemplate();
1575 if (p && 1 <= n && n <= p->numargs())
1576 insert(MathAtom(new MathMacroArgument(c - '0')));
1578 insert(createMathInset("#"));
1579 interpret(c); // try again
1586 if (inMacroMode()) {
1587 string name = macroName();
1588 //lyxerr << "interpret name: '" << name << "'" << endl;
1591 activeMacro()->setName(activeMacro()->name() + c);
1595 // handle 'special char' macros
1600 if (currentMode() == MathInset::TEXT_MODE)
1601 niceInsert(createMathInset("textbackslash"));
1603 niceInsert(createMathInset("backslash"));
1604 } else if (c == '{') {
1605 niceInsert(MathAtom(new MathBraceInset));
1607 niceInsert(createMathInset(string(1, c)));
1612 // leave macro mode and try again if necessary
1615 niceInsert(MathAtom(new MathBraceInset));
1621 // This is annoying as one has to press <space> far too often.
1625 // leave autocorrect mode if necessary
1626 if (autocorrect() && c == ' ') {
1627 autocorrect() = false;
1632 // just clear selection on pressing the space bar
1633 if (selection() && c == ' ') {
1634 selection() = false;
1641 //lyxerr << "starting with macro" << endl;
1642 insert(MathAtom(new MathUnknownInset("\\", false)));
1647 if (currentMode() == MathInset::TEXT_MODE)
1653 if (currentMode() == MathInset::TEXT_MODE) {
1654 // insert spaces in text mode,
1655 // but suppress direct insertion of two spaces in a row
1656 // the still allows typing '<space>a<space>' and deleting the 'a', but
1657 // it is better than nothing...
1658 if (!pos() != 0 || prevAtom()->getChar() != ' ')
1662 if (pos() != 0 && prevAtom()->asSpaceInset()) {
1663 prevAtom().nucleus()->asSpaceInset()->incSpace();
1668 // if are at the very end, leave the formula
1669 return pos() != lastpos();
1682 if (c == '{' || c == '}' || c == '#' || c == '&' || c == '$') {
1683 niceInsert(createMathInset(string(1, c)));
1688 niceInsert(MathAtom(new MathCommentInset));
1692 // try auto-correction
1693 //if (autocorrect() && hasPrevAtom() && math_autocorrect(prevAtom(), c))
1696 // no special circumstances, so insert the character without any fuss
1698 autocorrect() = true;
1703 void LCursor::lockToggle()
1705 if (pos() != lastpos()) {
1706 // toggle previous inset ...
1707 nextAtom().nucleus()->lock(!nextAtom()->lock());
1708 } else if (popLeft() && pos() != lastpos()) {
1709 // ... or enclosing inset if we are in the last inset position
1710 nextAtom().nucleus()->lock(!nextAtom()->lock());
1716 CursorSlice LCursor::normalAnchor()
1718 if (anchor_.size() < depth()) {
1720 lyxerr << "unusual Anchor size" << endl;
1722 //lyx::BOOST_ASSERT(Anchor_.size() >= cursor.depth());
1723 // use Anchor on the same level as Cursor
1724 CursorSlice normal = anchor_[current_];
1726 if (depth() < anchor_.size() && !(normal < xx())) {
1727 // anchor is behind cursor -> move anchor behind the inset
1736 DispatchResult dispatch(LCursor & cur, FuncRequest const & cmd)
1738 // mouse clicks are somewhat special
1740 switch (cmd.action) {
1741 case LFUN_MOUSE_PRESS:
1742 case LFUN_MOUSE_MOTION:
1743 case LFUN_MOUSE_RELEASE:
1744 case LFUN_MOUSE_DOUBLE: {
1745 CursorSlice & pos = cursor_.back();
1749 if (x < cmd.x && pos() != 0) {
1750 DispatchResult const res = prevAtom().nucleus()->dispatch(cmd);
1751 if (res.dispatched())
1754 if (x > cmd.x && pos() != lastpos()) {
1755 DispatchResult const res = inset()->dispatch(cmd);
1756 if (res.dispatched())
1767 void LCursor::handleFont(string const & font)
1772 safe = grabAndEraseSelection();
1775 if (lastpos() != 0) {
1776 // something left in the cell
1778 // cursor in first position
1780 } else if (pos() == lastpos()) {
1781 // cursor in last position
1784 // cursor in between. split cell
1785 MathArray::iterator bt = cell().begin();
1786 MathAtom at = createMathInset(font);
1787 at.nucleus()->cell(0) = MathArray(bt, bt + pos());
1788 cell().erase(bt, bt + pos());
1793 // nothing left in the cell
1801 void LCursor::releaseMathCursor()
1804 formula()->insetUnlock(bv());
1808 bool LCursor::inMathed() const
1814 InsetBase * LCursor::nextInset()
1816 if (pos() == lastpos())
1819 return nextAtom().nucleus();
1820 Paragraph & par = paragraph();
1821 return par.isInset(pos()) ? par.getInset(pos()) : 0;
1825 InsetBase * LCursor::prevInset()
1830 return prevAtom().nucleus();
1831 Paragraph & par = paragraph();
1832 return par.isInset(pos() - 1) ? par.getInset(pos() - 1) : 0;
1836 void LCursor::message(string const & msg) const
1838 bv().owner()->getLyXFunc().setMessage(msg);
1842 void LCursor::errorMessage(string const & msg) const
1844 bv().owner()->getLyXFunc().setErrorMessage(msg);