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 return current_ ? current().paragraph() : *bv_->text()->getPar(par());
489 Paragraph const & LCursor::paragraph() const
491 return current_ ? current().paragraph() : *bv_->text()->getPar(par());
495 LCursor::pos_type LCursor::lastpos() const
497 InsetBase * inset = current().inset();
498 return inset && inset->asMathInset() ? cell().size() : paragraph().size();
502 size_t LCursor::nargs() const
504 // assume 1x1 grid for 'plain text'
505 return current_ ? current().nargs() : 1;
509 size_t LCursor::ncols() const
511 // assume 1x1 grid for 'plain text'
512 return current_ ? current().ncols() : 1;
516 size_t LCursor::nrows() const
518 // assume 1x1 grid for 'plain text'
519 return current_ ? current().nrows() : 1;
523 LCursor::row_type LCursor::row() const
525 BOOST_ASSERT(current_ > 0);
526 return current().row();
530 LCursor::col_type LCursor::col() const
532 BOOST_ASSERT(current_ > 0);
533 return current().col();
537 MathArray const & LCursor::cell() const
539 BOOST_ASSERT(current_ > 0);
540 return current().cell();
544 MathArray & LCursor::cell()
546 BOOST_ASSERT(current_ > 0);
547 return current().cell();
551 void LCursor::info(std::ostream & os)
553 for (int i = 1, n = depth(); i < n; ++i) {
554 cursor_[i].inset()->infoize(os);
559 // prevAtom()->infoize2(os);
560 // overwite old message
567 void region(CursorSlice const & i1, CursorSlice const & i2,
568 LCursor::row_type & r1, LCursor::row_type & r2,
569 LCursor::col_type & c1, LCursor::col_type & c2)
571 InsetBase * p = i1.inset();
572 c1 = p->col(i1.idx_);
573 c2 = p->col(i2.idx_);
576 r1 = p->row(i1.idx_);
577 r2 = p->row(i2.idx_);
585 string LCursor::grabSelection()
590 CursorSlice i1 = selBegin();
591 CursorSlice i2 = selEnd();
593 if (i1.idx_ == i2.idx_) {
594 if (i1.inset()->asMathInset()) {
595 MathArray::const_iterator it = i1.cell().begin();
596 return asString(MathArray(it + i1.pos_, it + i2.pos_));
598 return "unknown selection 1";
604 region(i1, i2, r1, r2, c1, c2);
607 if (i1.inset()->asMathInset()) {
608 for (row_type row = r1; row <= r2; ++row) {
611 for (col_type col = c1; col <= c2; ++col) {
614 data += asString(i1.asMathInset()->cell(i1.asMathInset()->index(row, col)));
618 data = "unknown selection 2";
624 void LCursor::eraseSelection()
626 CursorSlice i1 = selBegin();
627 CursorSlice i2 = selEnd();
629 if (i1.inset()->asMathInset()) {
630 if (i1.idx_ == i2.idx_) {
631 i1.cell().erase(i1.pos_, i2.pos_);
633 MathInset * p = i1.asMathInset();
636 region(i1, i2, r1, r2, c1, c2);
637 for (row_type row = r1; row <= r2; ++row)
638 for (col_type col = c1; col <= c2; ++col)
639 p->cell(p->index(row, col)).clear();
643 lyxerr << "can't erase this selection 1" << endl;
648 string LCursor::grabAndEraseSelection()
652 string res = grabSelection();
659 void LCursor::selClear()
666 void LCursor::selCopy()
669 theCutBuffer.push(grabSelection());
672 //theCutBuffer.erase();
677 void LCursor::selCut()
679 theCutBuffer.push(grabAndEraseSelection());
683 void LCursor::selDel()
692 void LCursor::selPaste(size_t n)
695 if (n < theCutBuffer.size())
696 paste(theCutBuffer[n]);
702 void LCursor::selHandle(bool sel)
704 if (sel == selection())
711 void LCursor::selStart()
718 void LCursor::selClearOrDel()
720 if (lyxrc.auto_region_delete)
727 std::ostream & operator<<(std::ostream & os, LCursor const & cur)
730 for (size_t i = 0, n = cur.cursor_.size(); i != n; ++i)
731 os << " (" << cur.cursor_[i] << " | " << cur.anchor_[i] << "\n";
732 return os << "current: " << cur.current_ << endl;
743 void increment(CursorBase & it)
745 CursorSlice & top = it.back();
746 MathArray & ar = top.asMathInset()->cell(top.idx_);
748 // move into the current inset if possible
749 // it is impossible for pos() == size()!
751 if (top.pos() != top.lastpos())
752 n = (ar.begin() + top.pos_)->nucleus();
753 if (n && n->isActive()) {
754 it.push_back(CursorSlice(n));
758 // otherwise move on one cell back if possible
759 if (top.pos() < top.lastpos()) {
760 // pos() == lastpos() is valid!
765 // otherwise try to move on one cell if possible
766 while (top.idx() < top.lastidx()) {
768 if (top.asMathInset()->validCell(top.idx_)) {
774 // otherwise leave array, move on one back
775 // this might yield pos() == size(), but that's a ok.
777 // it certainly invalidates top
782 CursorBase ibegin(InsetBase * p)
785 it.push_back(CursorSlice(p));
790 CursorBase iend(InsetBase * p)
793 it.push_back(CursorSlice(p));
794 CursorSlice & cur = it.back();
795 cur.idx() = cur.lastidx();
796 cur.pos() = cur.lastpos();
803 ///////////////////////////////////////////////////////////////////
805 // The part below is the non-integrated rest of the original math
806 // cursor. This should be either generalized for texted or moved
807 // back to the math insets.
809 ///////////////////////////////////////////////////////////////////
811 #include "mathed/math_braceinset.h"
812 #include "mathed/math_charinset.h"
813 #include "mathed/math_commentinset.h"
814 #include "mathed/math_factory.h"
815 #include "mathed/math_gridinset.h"
816 #include "mathed/math_macroarg.h"
817 #include "mathed/math_macrotemplate.h"
818 #include "mathed/math_mathmlstream.h"
819 #include "mathed/math_scriptinset.h"
820 #include "mathed/math_spaceinset.h"
821 #include "mathed/math_support.h"
822 #include "mathed/math_unknowninset.h"
824 //#define FILEDEBUG 1
827 bool LCursor::isInside(InsetBase const * p)
829 for (unsigned i = 0; i < depth(); ++i)
830 if (cursor_[i].inset() == p)
836 bool LCursor::openable(MathAtom const & t)
847 // we can't move into anything new during selection
848 if (depth() == anchor_.size())
850 if (t.nucleus() != anchor_[depth()].inset())
857 bool LCursor::inNucleus()
859 return inset()->asMathInset()->asScriptInset() && idx() == 2;
865 autocorrect() = false;
872 if (pos() != 0 && openable(prevAtom())) {
874 push(nextAtom().nucleus());
875 inset()->idxLast(*this);
879 return posLeft() || idxLeft() || popLeft() || selection();
883 bool LCursor::right()
885 autocorrect() = false;
892 if (pos() != lastpos() && openable(nextAtom())) {
893 pushLeft(nextAtom().nucleus());
894 inset()->idxFirst(*this);
898 return posRight() || idxRight() || popRight() || selection();
902 bool positionable(CursorBase const & cursor, CursorBase const & anchor)
904 // avoid deeper nested insets when selecting
905 if (cursor.size() > anchor.size())
908 // anchor might be deeper, should have same path then
909 for (size_t i = 0; i < cursor.size(); ++i)
910 if (cursor[i].inset() != anchor[i].inset())
913 // position should be ok.
918 void LCursor::setScreenPos(int x, int y)
920 bool res = bruteFind(x, y, formula()->xlow(), formula()->xhigh(),
921 formula()->ylow(), formula()->yhigh());
923 // this can happen on creation of "math-display"
934 autocorrect() = false;
936 if (!inset()->idxHome(*this))
945 autocorrect() = false;
947 if (!inset()->idxEnd(*this))
954 void LCursor::plainErase()
960 void LCursor::markInsert()
962 cell().insert(pos(), MathAtom(new MathCharInset(0)));
966 void LCursor::markErase()
972 void LCursor::plainInsert(MathAtom const & t)
974 cell().insert(pos(), t);
979 void LCursor::insert2(string const & str)
987 void LCursor::insert(string const & str)
989 lyxerr << "inserting '" << str << "'" << endl;
991 for (string::const_iterator it = str.begin(); it != str.end(); ++it)
992 plainInsert(MathAtom(new MathCharInset(*it)));
996 void LCursor::insert(char c)
998 lyxerr << "inserting '" << c << "'" << endl;
1000 plainInsert(MathAtom(new MathCharInset(c)));
1004 void LCursor::insert(MathAtom const & t)
1012 void LCursor::niceInsert(string const & t)
1014 lyxerr << "*** LCursor::niceInsert 1: " << t << endl;
1024 void LCursor::niceInsert(MathAtom const & t)
1026 lyxerr << "*** LCursor::niceInsert 2: " << t << endl;
1028 string safe = grabAndEraseSelection();
1030 // enter the new inset and move the contents of the selection if possible
1031 if (t->isActive()) {
1033 pushLeft(nextAtom().nucleus());
1036 lyxerr << "*** LCursor::niceInsert 3: " << t << endl;
1040 void LCursor::insert(MathArray const & ar)
1045 cell().insert(pos(), ar);
1050 bool LCursor::backspace()
1052 autocorrect() = false;
1060 if (inset()->nargs() == 1 && depth() == 1 && lastpos() == 0)
1066 if (inMacroMode()) {
1067 MathUnknownInset * p = activeMacro();
1068 if (p->name().size() > 1) {
1069 p->setName(p->name().substr(0, p->name().size() - 1));
1074 if (pos() != 0 && prevAtom()->nargs() > 0) {
1075 // let's require two backspaces for 'big stuff' and
1076 // highlight on the first
1087 bool LCursor::erase()
1089 autocorrect() = false;
1098 // delete empty cells if possible
1100 //if (cell().empty() && inset()->idxDelete(idx()))
1103 // special behaviour when in last position of cell
1104 if (pos() == lastpos()) {
1105 bool one_cell = inset()->nargs() == 1;
1106 if (one_cell && depth() == 1 && lastpos() == 0)
1112 inset()->idxGlue(idx());
1116 if (pos() != lastpos() && inset()->nargs() > 0) {
1130 CursorBase save = cursor_;
1134 autocorrect() = false;
1139 bool LCursor::down()
1142 CursorBase save = cursor_;
1143 if (goUpDown(false))
1146 autocorrect() = false;
1151 void LCursor::macroModeClose()
1155 MathUnknownInset * p = activeMacro();
1157 string s = p->name();
1159 cell().erase(pos());
1161 // do nothing if the macro name is empty
1165 string const name = s.substr(1);
1167 // prevent entering of recursive macros
1168 if (formula()->lyxCode() == InsetOld::MATHMACRO_CODE
1169 && formula()->getInsetName() == name)
1170 lyxerr << "can't enter recursive macro" << endl;
1172 niceInsert(createMathInset(name));
1176 string LCursor::macroName()
1178 return inMacroMode() ? activeMacro()->name() : string();
1182 void LCursor::drawSelection(PainterInfo & pi)
1186 CursorSlice i1 = selBegin();
1187 CursorSlice i2 = selEnd();
1188 i1.asMathInset()->drawSelection(pi, i1.idx_, i1.pos_, i2.idx_, i2.pos_);
1192 void LCursor::handleNest(MathAtom const & a, int c)
1195 asArray(grabAndEraseSelection(), t.nucleus()->cell(c));
1198 pushLeft(t.nucleus());
1202 int LCursor::targetX() const
1204 if (x_target() != -1)
1213 MathHullInset * LCursor::formula() const
1215 for (int i = cursor_.size() - 1; i >= 1; --i)
1216 if (cursor_[i].inset()->lyxCode() == InsetBase::MATH_CODE)
1217 return static_cast<MathHullInset *>(cursor_[i].inset());
1222 void LCursor::adjust(pos_type from, int diff)
1226 if (anchor().pos_ > from)
1227 anchor().pos_ += diff;
1228 // just to be on the safe side
1229 // theoretically unecessary
1234 bool LCursor::inMacroMode() const
1238 MathUnknownInset const * p = prevAtom()->asUnknownInset();
1239 return p && !p->final();
1243 MathUnknownInset * LCursor::activeMacro()
1245 return inMacroMode() ? prevAtom().nucleus()->asUnknownInset() : 0;
1249 bool LCursor::inMacroArgMode() const
1251 return pos() > 0 && prevAtom()->getChar() == '#';
1255 MathGridInset * LCursor::enclosingGrid(idx_type & idx) const
1257 for (MathInset::difference_type i = depth() - 1; i >= 0; --i) {
1258 MathInset * m = cursor_[i].inset()->asMathInset();
1261 MathGridInset * p = m->asGridInset();
1263 idx = cursor_[i].idx_;
1271 void LCursor::pullArg()
1275 MathArray ar = cell();
1278 cell().insert(pos(), ar);
1281 formula()->mutateToText();
1287 void LCursor::touch()
1291 CursorBase::const_iterator it = cursor_.begin();
1292 CursorBase::const_iterator et = cursor_.end();
1293 for ( ; it != et; ++it)
1299 void LCursor::normalize()
1301 if (idx() >= nargs()) {
1302 lyxerr << "this should not really happen - 1: "
1303 << idx() << ' ' << nargs()
1304 << " in: " << inset() << endl;
1306 idx() = min(idx(), nargs() - 1);
1308 if (pos() > lastpos()) {
1309 lyxerr << "this should not really happen - 2: "
1310 << pos() << ' ' << lastpos() << " in idx: " << idx()
1312 WriteStream wi(lyxerr, false, true);
1313 inset()->asMathInset()->write(wi);
1316 pos() = min(pos(), lastpos());
1320 char LCursor::valign()
1323 MathGridInset * p = enclosingGrid(idx);
1324 return p ? p->valign() : '\0';
1328 char LCursor::halign()
1331 MathGridInset * p = enclosingGrid(idx);
1332 return p ? p->halign(idx % p->ncols()) : '\0';
1336 bool LCursor::goUpDown(bool up)
1338 // Be warned: The 'logic' implemented in this function is highly fragile.
1339 // A distance of one pixel or a '<' vs '<=' _really_ matters.
1340 // So fiddle around with it only if you know what you are doing!
1345 // check if we had something else in mind, if not, this is the future goal
1346 if (x_target() == -1)
1351 // try neigbouring script insets
1355 MathScriptInset const * p = prevAtom()->asScriptInset();
1356 if (p && p->has(up)) {
1359 idx() = up; // the superscript has index 1
1361 //lyxerr << "updown: handled by scriptinset to the left" << endl;
1367 if (pos() != lastpos()) {
1368 MathScriptInset const * p = nextAtom()->asScriptInset();
1369 if (p && p->has(up)) {
1373 //lyxerr << "updown: handled by scriptinset to the right" << endl;
1379 // try current cell for e.g. text insets
1380 if (inset()->idxUpDown2(*this, up))
1383 //xarray().boundingBox(xlow, xhigh, ylow, yhigh);
1388 //if (bruteFind(xo, yo, xlow, xhigh, ylow, yhigh)) {
1389 // lyxerr << "updown: handled by brute find in the same cell" << endl;
1393 // try to find an inset that knows better then we
1395 //lyxerr << "updown: We are in " << inset() << " idx: " << idx() << endl;
1397 if (inset()->idxUpDown(*this, up)) {
1398 // try to find best position within this inset
1404 // no such inset found, just take something "above"
1405 //lyxerr << "updown: handled by strange case" << endl;
1411 up ? formula()->ylow() : yo + 4,
1412 up ? yo - 4 : formula()->yhigh()
1416 // any improvement so far?
1419 if (up ? ynew < yo : ynew > yo)
1425 bool LCursor::bruteFind(int x, int y, int xlow, int xhigh, int ylow, int yhigh)
1427 CursorBase best_cursor;
1428 double best_dist = 1e10;
1430 CursorBase it = ibegin(formula());
1431 CursorBase et = iend(formula());
1433 // avoid invalid nesting when selecting
1434 if (!selection() || positionable(it, anchor_)) {
1436 CursorSlice & cur = it.back();
1437 cur.inset()->getCursorPos(cur, xo, yo);
1438 if (xlow <= xo && xo <= xhigh && ylow <= yo && yo <= yhigh) {
1439 double d = (x - xo) * (x - xo) + (y - yo) * (y - yo);
1440 //lyxerr << "x: " << x << " y: " << y << " d: " << endl;
1441 // '<=' in order to take the last possible position
1442 // this is important for clicking behind \sum in e.g. '\sum_i a'
1443 if (d <= best_dist) {
1455 if (best_dist < 1e10)
1456 cursor_ = best_cursor;
1457 return best_dist < 1e10;
1461 void LCursor::bruteFind2(int x, int y)
1463 double best_dist = 1e10;
1465 CursorBase it = cursor_;
1467 CursorBase et = cursor_;
1468 int n = et.back().asMathInset()->cell(et.back().idx_).size();
1470 for (int i = 0; ; ++i) {
1472 CursorSlice & cur = it.back();
1473 cur.inset()->getCursorPos(cur, xo, yo);
1474 double d = (x - xo) * (x - xo) + (y - yo) * (y - yo);
1475 // '<=' in order to take the last possible position
1476 // this is important for clicking behind \sum in e.g. '\sum_i a'
1477 lyxerr << "i: " << i << " d: " << d << " best: " << best_dist << endl;
1478 if (d <= best_dist) {
1489 bool LCursor::idxLineLast()
1491 idx() -= idx() % ncols();
1492 idx() += ncols() - 1;
1498 bool LCursor::idxLeft()
1500 return inset()->idxLeft(*this);
1504 bool LCursor::idxRight()
1506 return inset()->idxRight(*this);
1510 bool LCursor::script(bool up)
1512 // Hack to get \\^ and \\_ working
1513 if (inMacroMode() && macroName() == "\\") {
1515 niceInsert(createMathInset("mathcircumflex"));
1522 string safe = grabAndEraseSelection();
1524 // we are in a nucleus of a script inset, move to _our_ script
1525 inset()->asMathInset()->asScriptInset()->ensure(up);
1528 } else if (pos() != 0 && prevAtom()->asScriptInset()) {
1529 prevAtom().nucleus()->asScriptInset()->ensure(up);
1534 } else if (pos() != 0) {
1537 = MathAtom(new MathScriptInset(nextAtom(), up));
1542 plainInsert(MathAtom(new MathScriptInset(up)));
1544 nextAtom().nucleus()->asScriptInset()->ensure(up);
1554 bool LCursor::interpret(char c)
1556 //lyxerr << "interpret 2: '" << c << "'" << endl;
1558 if (inMacroArgMode()) {
1564 MathMacroTemplate const * p = formula()->asMacroTemplate();
1565 if (p && 1 <= n && n <= p->numargs())
1566 insert(MathAtom(new MathMacroArgument(c - '0')));
1568 insert(createMathInset("#"));
1569 interpret(c); // try again
1576 if (inMacroMode()) {
1577 string name = macroName();
1578 //lyxerr << "interpret name: '" << name << "'" << endl;
1581 activeMacro()->setName(activeMacro()->name() + c);
1585 // handle 'special char' macros
1590 if (currentMode() == MathInset::TEXT_MODE)
1591 niceInsert(createMathInset("textbackslash"));
1593 niceInsert(createMathInset("backslash"));
1594 } else if (c == '{') {
1595 niceInsert(MathAtom(new MathBraceInset));
1597 niceInsert(createMathInset(string(1, c)));
1602 // leave macro mode and try again if necessary
1605 niceInsert(MathAtom(new MathBraceInset));
1611 // This is annoying as one has to press <space> far too often.
1615 // leave autocorrect mode if necessary
1616 if (autocorrect() && c == ' ') {
1617 autocorrect() = false;
1622 // just clear selection on pressing the space bar
1623 if (selection() && c == ' ') {
1624 selection() = false;
1631 //lyxerr << "starting with macro" << endl;
1632 insert(MathAtom(new MathUnknownInset("\\", false)));
1637 if (currentMode() == MathInset::TEXT_MODE)
1643 if (currentMode() == MathInset::TEXT_MODE) {
1644 // insert spaces in text mode,
1645 // but suppress direct insertion of two spaces in a row
1646 // the still allows typing '<space>a<space>' and deleting the 'a', but
1647 // it is better than nothing...
1648 if (!pos() != 0 || prevAtom()->getChar() != ' ')
1652 if (pos() != 0 && prevAtom()->asSpaceInset()) {
1653 prevAtom().nucleus()->asSpaceInset()->incSpace();
1658 // if are at the very end, leave the formula
1659 return pos() != lastpos();
1672 if (c == '{' || c == '}' || c == '#' || c == '&' || c == '$') {
1673 niceInsert(createMathInset(string(1, c)));
1678 niceInsert(MathAtom(new MathCommentInset));
1682 // try auto-correction
1683 //if (autocorrect() && hasPrevAtom() && math_autocorrect(prevAtom(), c))
1686 // no special circumstances, so insert the character without any fuss
1688 autocorrect() = true;
1693 void LCursor::lockToggle()
1695 if (pos() != lastpos()) {
1696 // toggle previous inset ...
1697 nextAtom().nucleus()->lock(!nextAtom()->lock());
1698 } else if (popLeft() && pos() != lastpos()) {
1699 // ... or enclosing inset if we are in the last inset position
1700 nextAtom().nucleus()->lock(!nextAtom()->lock());
1706 CursorSlice LCursor::normalAnchor()
1708 if (anchor_.size() < depth()) {
1710 lyxerr << "unusual Anchor size" << endl;
1712 //lyx::BOOST_ASSERT(Anchor_.size() >= cursor.depth());
1713 // use Anchor on the same level as Cursor
1714 CursorSlice normal = anchor_[current_];
1716 if (depth() < anchor_.size() && !(normal < xx())) {
1717 // anchor is behind cursor -> move anchor behind the inset
1726 DispatchResult dispatch(LCursor & cur, FuncRequest const & cmd)
1728 // mouse clicks are somewhat special
1730 switch (cmd.action) {
1731 case LFUN_MOUSE_PRESS:
1732 case LFUN_MOUSE_MOTION:
1733 case LFUN_MOUSE_RELEASE:
1734 case LFUN_MOUSE_DOUBLE: {
1735 CursorSlice & pos = cursor_.back();
1739 if (x < cmd.x && pos() != 0) {
1740 DispatchResult const res = prevAtom().nucleus()->dispatch(cmd);
1741 if (res.dispatched())
1744 if (x > cmd.x && pos() != lastpos()) {
1745 DispatchResult const res = inset()->dispatch(cmd);
1746 if (res.dispatched())
1757 void LCursor::handleFont(string const & font)
1762 safe = grabAndEraseSelection();
1765 if (lastpos() != 0) {
1766 // something left in the cell
1768 // cursor in first position
1770 } else if (pos() == lastpos()) {
1771 // cursor in last position
1774 // cursor in between. split cell
1775 MathArray::iterator bt = cell().begin();
1776 MathAtom at = createMathInset(font);
1777 at.nucleus()->cell(0) = MathArray(bt, bt + pos());
1778 cell().erase(bt, bt + pos());
1783 // nothing left in the cell
1791 void LCursor::releaseMathCursor()
1794 formula()->insetUnlock(bv());
1798 bool LCursor::inMathed() const