3 * This file is part of LyX, the document processor.
4 * Licence details can be found in the file COPYING.
6 * \author Alejandro Aguilar Sierra
7 * \author Alfredo Braunstein
10 * Full author contact details are available in file CREDITS.
16 #include "BufferView.h"
19 #include "dispatchresult.h"
20 #include "funcrequest.h"
21 #include "iterators.h"
23 #include "lyxfunc.h" // only for setMessage()
27 #include "paragraph.h"
29 #include "insets/updatableinset.h"
30 #include "insets/insettabular.h"
31 #include "insets/insettext.h"
33 #include "mathed/math_data.h"
34 #include "mathed/math_hullinset.h"
35 #include "mathed/math_support.h"
37 #include "support/limited_stack.h"
38 #include "frontends/LyXView.h"
40 #include <boost/assert.hpp>
45 #ifndef CXX_GLOBAL_CSTD
54 limited_stack<string> theCutBuffer;
57 LCursor::LCursor(BufferView & bv)
58 : cursor_(1), anchor_(1), bv_(&bv), current_(0),
59 cached_y_(0), x_target_(-1),
60 selection_(false), mark_(false)
68 cursor_.push_back(CursorSlice());
69 anchor_.push_back(CursorSlice());
78 DispatchResult LCursor::dispatch(FuncRequest const & cmd0)
80 lyxerr << "\nLCursor::dispatch: cmd: " << cmd0 << endl << *this << endl;
81 FuncRequest cmd = cmd0;
82 for (current_ = cursor_.size() - 1; current_ >= 1; --current_) {
83 lyxerr << "trying to dispatch to inset " << inset() << endl;
84 DispatchResult res = inset()->dispatch(*this, cmd);
85 if (res.dispatched()) {
86 lyxerr << " successfully dispatched to inset " << inset() << endl;
87 return DispatchResult(true, true);
89 // "Mutate" the request for semi-handled requests that need
90 // additional handling in outer levels.
93 cmd = FuncRequest(LFUN_FINISHED_LEFT);
96 cmd = FuncRequest(LFUN_FINISHED_RIGHT);
99 cmd = FuncRequest(LFUN_FINISHED_UP);
102 cmd = FuncRequest(LFUN_FINISHED_DOWN);
105 //lyxerr << "not handled on level " << current_
106 // << " val: " << res.val() << endl;
110 BOOST_ASSERT(current_ == 0);
111 //lyxerr << "trying to dispatch to main text " << bv_->text()
112 // << " with cursor: " << *this << endl;
113 DispatchResult res = bv_->text()->dispatch(*this, cmd);
114 //lyxerr << " result: " << res.val() << endl;
119 void LCursor::push(InsetBase * inset)
121 lyxerr << "LCursor::push() inset: " << inset << endl;
122 cursor_.push_back(CursorSlice(inset));
123 anchor_.push_back(CursorSlice(inset));
129 void LCursor::pop(int depth)
131 while (int(cursor_.size()) > depth + 1)
133 lyxerr << "LCursor::pop() result: " << *this << endl;
139 BOOST_ASSERT(cursor_.size() >= 1);
142 current_ = cursor_.size() - 1;
146 void LCursor::pushLeft(InsetBase * p)
148 BOOST_ASSERT(!cursor_.empty());
149 //lyxerr << "Entering inset " << t << " left" << endl;
155 bool LCursor::popLeft()
157 BOOST_ASSERT(!cursor_.empty());
158 //lyxerr << "Leaving inset to the left" << endl;
161 inset()->notifyCursorLeaves(idx());
164 inset()->notifyCursorLeaves(idx());
170 bool LCursor::popRight()
172 BOOST_ASSERT(!cursor_.empty());
173 //lyxerr << "Leaving inset to the right" << endl;
176 inset()->notifyCursorLeaves(idx());
179 inset()->notifyCursorLeaves(idx());
186 CursorSlice & LCursor::current()
188 BOOST_ASSERT(!cursor_.empty());
189 //lyxerr << "accessing cursor slice " << current_
190 // << ": " << cursor_[current_] << endl;
191 return cursor_[current_];
195 CursorSlice const & LCursor::current() const
197 //lyxerr << "accessing cursor slice " << current_
198 // << ": " << cursor_[current_] << endl;
199 return cursor_[current_];
203 int LCursor::currentMode()
205 BOOST_ASSERT(!cursor_.empty());
206 for (int i = cursor_.size() - 1; i >= 1; --i) {
207 int res = cursor_[i].inset()->currentMode();
208 if (res != MathInset::UNDECIDED_MODE)
211 return MathInset::TEXT_MODE;
215 LyXText * LCursor::innerText() const
217 BOOST_ASSERT(!cursor_.empty());
218 if (cursor_.size() > 1) {
219 // go up until first non-0 text is hit
220 // (innermost text is 0 in mathed)
221 for (int i = cursor_.size() - 1; i >= 1; --i)
222 if (cursor_[i].text())
223 return cursor_[i].text();
229 CursorSlice const & LCursor::innerTextSlice() const
231 BOOST_ASSERT(!cursor_.empty());
232 if (cursor_.size() > 1) {
233 // go up until first non-0 text is hit
234 // (innermost text is 0 in mathed)
235 for (int i = cursor_.size() - 1; i >= 1; --i)
236 if (cursor_[i].text())
243 void LCursor::updatePos()
245 BOOST_ASSERT(!cursor_.empty());
246 if (cursor_.size() > 1)
247 cached_y_ = bv_->top_y() + cursor_.back().inset()->yo();
248 //cached_y_ = cursor_.back().inset()->yo();
252 void LCursor::getDim(int & asc, int & des) const
254 BOOST_ASSERT(!cursor_.empty());
256 //inset()->asMathInset()->getCursorDim(asc, des);
260 Row const & row = *text()->cursorRow();
261 asc = row.baseline();
262 des = row.height() - asc;
267 void LCursor::getPos(int & x, int & y) const
269 BOOST_ASSERT(!cursor_.empty());
272 if (cursor_.size() <= 1) {
273 x = bv_->text()->cursorX(cursor_.front());
274 y = bv_->text()->cursorY(cursor_.front());
276 inset()->getCursorPos(cursor_.back(), x, y);
277 // getCursorPos gives _screen_ coordinates. We need to add
278 // top_y to get document coordinates. This is hidden in cached_y_.
279 //y += cached_y_ - inset()->yo();
280 // The rest is non-obvious. The reason we have to have these
281 // extra computation is that the getCursorPos() calls rely
282 // on the inset's own knowledge of its screen position.
283 // If we scroll up or down in a big enough increment,
284 // inset->draw() is not called: this doesn't update
285 // inset.yo_, so getCursor() returns an old value.
288 //lyxerr << "#### LCursor::getPos: " << *this
289 // << " x: " << x << " y: " << y << endl;
293 void LCursor::paste(string const & data)
295 dispatch(FuncRequest(LFUN_PASTE, data));
299 InsetBase * LCursor::innerInsetOfType(int code) const
301 for (int i = cursor_.size() - 1; i >= 1; --i)
302 if (cursor_[i].inset_->lyxCode() == code)
303 return cursor_[i].inset_;
308 InsetTabular * LCursor::innerInsetTabular() const
310 return static_cast<InsetTabular *>(innerInsetOfType(InsetBase::TABULAR_CODE));
314 void LCursor::resetAnchor()
320 BufferView & LCursor::bv() const
326 MathAtom const & LCursor::prevAtom() const
328 BOOST_ASSERT(pos() > 0);
329 return cell()[pos() - 1];
333 MathAtom & LCursor::prevAtom()
335 BOOST_ASSERT(pos() > 0);
336 return cell()[pos() - 1];
340 MathAtom const & LCursor::nextAtom() const
342 BOOST_ASSERT(pos() < lastpos());
343 return cell()[pos()];
347 MathAtom & LCursor::nextAtom()
349 BOOST_ASSERT(pos() < lastpos());
350 return cell()[pos()];
354 bool LCursor::posLeft()
363 bool LCursor::posRight()
365 if (pos() == lastpos())
372 CursorSlice & LCursor::anchor()
374 return anchor_.back();
378 CursorSlice const & LCursor::anchor() const
380 return anchor_.back();
384 CursorSlice const & LCursor::selBegin() const
387 return cursor_.back();
388 // can't use std::min as this creates a new object
389 return anchor() < cursor_.back() ? anchor() : cursor_.back();
393 CursorSlice & LCursor::selBegin()
396 return cursor_.back();
397 return anchor() < cursor_.back() ? anchor() : cursor_.back();
401 CursorSlice const & LCursor::selEnd() const
404 return cursor_.back();
405 return anchor() > cursor_.back() ? anchor() : cursor_.back();
409 CursorSlice & LCursor::selEnd()
412 return cursor_.back();
413 return anchor() > cursor_.back() ? anchor() : cursor_.back();
417 void LCursor::setSelection()
420 // a selection with no contents is not a selection
421 if (par() == anchor().par() && pos() == anchor().pos())
426 void LCursor::setSelection(CursorBase const & where, size_t n)
435 void LCursor::clearSelection()
444 int & LCursor::x_target()
450 int LCursor::x_target() const
456 void LCursor::clearTargetX()
462 LyXText * LCursor::text() const
464 return current_ ? current().text() : bv_->text();
468 Paragraph & LCursor::paragraph()
470 BOOST_ASSERT(!inMathed());
471 return current_ ? current().paragraph() : *bv_->text()->getPar(par());
475 Paragraph const & LCursor::paragraph() const
477 BOOST_ASSERT(!inMathed());
478 return current_ ? current().paragraph() : *bv_->text()->getPar(par());
482 LCursor::par_type LCursor::lastpar() const
484 return inMathed() ? 0 : text()->paragraphs().size() - 1;
488 LCursor::pos_type LCursor::lastpos() const
490 InsetBase * inset = current().inset();
491 return inset && inset->asMathInset() ? cell().size() : paragraph().size();
495 LCursor::row_type LCursor::crow() const
497 return paragraph().row(pos());
501 LCursor::row_type LCursor::lastcrow() const
503 return paragraph().rows.size();
507 size_t LCursor::nargs() const
509 // assume 1x1 grid for 'plain text'
510 return current_ ? current().nargs() : 1;
514 size_t LCursor::ncols() const
516 // assume 1x1 grid for 'plain text'
517 return current_ ? current().ncols() : 1;
521 size_t LCursor::nrows() const
523 // assume 1x1 grid for 'plain text'
524 return current_ ? current().nrows() : 1;
528 LCursor::row_type LCursor::row() const
530 BOOST_ASSERT(current_ > 0);
531 return current().row();
535 LCursor::col_type LCursor::col() const
537 BOOST_ASSERT(current_ > 0);
538 return current().col();
542 MathArray const & LCursor::cell() const
544 BOOST_ASSERT(current_ > 0);
545 return current().cell();
549 MathArray & LCursor::cell()
551 BOOST_ASSERT(current_ > 0);
552 return current().cell();
556 void LCursor::info(std::ostream & os)
558 for (int i = 1, n = depth(); i < n; ++i) {
559 cursor_[i].inset()->infoize(os);
563 prevInset()->infoize2(os);
564 // overwite old message
571 void region(CursorSlice const & i1, CursorSlice const & i2,
572 LCursor::row_type & r1, LCursor::row_type & r2,
573 LCursor::col_type & c1, LCursor::col_type & c2)
575 InsetBase * p = i1.inset();
576 c1 = p->col(i1.idx_);
577 c2 = p->col(i2.idx_);
580 r1 = p->row(i1.idx_);
581 r2 = p->row(i2.idx_);
589 string LCursor::grabSelection()
594 CursorSlice i1 = selBegin();
595 CursorSlice i2 = selEnd();
597 if (i1.idx_ == i2.idx_) {
598 if (i1.inset()->asMathInset()) {
599 MathArray::const_iterator it = i1.cell().begin();
600 return asString(MathArray(it + i1.pos_, it + i2.pos_));
602 return "unknown selection 1";
608 region(i1, i2, r1, r2, c1, c2);
611 if (i1.inset()->asMathInset()) {
612 for (row_type row = r1; row <= r2; ++row) {
615 for (col_type col = c1; col <= c2; ++col) {
618 data += asString(i1.asMathInset()->cell(i1.asMathInset()->index(row, col)));
622 data = "unknown selection 2";
628 void LCursor::eraseSelection()
630 CursorSlice i1 = selBegin();
631 CursorSlice i2 = selEnd();
633 if (i1.inset()->asMathInset()) {
634 if (i1.idx_ == i2.idx_) {
635 i1.cell().erase(i1.pos_, i2.pos_);
637 MathInset * p = i1.asMathInset();
640 region(i1, i2, r1, r2, c1, c2);
641 for (row_type row = r1; row <= r2; ++row)
642 for (col_type col = c1; col <= c2; ++col)
643 p->cell(p->index(row, col)).clear();
647 lyxerr << "can't erase this selection 1" << endl;
652 string LCursor::grabAndEraseSelection()
656 string res = grabSelection();
663 void LCursor::selClear()
670 void LCursor::selCopy()
673 theCutBuffer.push(grabSelection());
676 //theCutBuffer.erase();
681 void LCursor::selCut()
683 theCutBuffer.push(grabAndEraseSelection());
687 void LCursor::selDel()
696 void LCursor::selPaste(size_t n)
699 if (n < theCutBuffer.size())
700 paste(theCutBuffer[n]);
706 void LCursor::selHandle(bool sel)
708 if (sel == selection())
715 void LCursor::selStart()
722 void LCursor::selClearOrDel()
724 if (lyxrc.auto_region_delete)
731 std::ostream & operator<<(std::ostream & os, LCursor const & cur)
734 for (size_t i = 0, n = cur.cursor_.size(); i != n; ++i)
735 os << " (" << cur.cursor_[i] << " | " << cur.anchor_[i] << "\n";
736 return os << "current: " << cur.current_ << endl;
747 void increment(CursorBase & it)
749 CursorSlice & top = it.back();
750 MathArray & ar = top.asMathInset()->cell(top.idx_);
752 // move into the current inset if possible
753 // it is impossible for pos() == size()!
755 if (top.pos() != top.lastpos())
756 n = (ar.begin() + top.pos_)->nucleus();
757 if (n && n->isActive()) {
758 it.push_back(CursorSlice(n));
762 // otherwise move on one cell back if possible
763 if (top.pos() < top.lastpos()) {
764 // pos() == lastpos() is valid!
769 // otherwise try to move on one cell if possible
770 while (top.idx() < top.lastidx()) {
772 if (top.asMathInset()->validCell(top.idx_)) {
778 // otherwise leave array, move on one back
779 // this might yield pos() == size(), but that's a ok.
781 // it certainly invalidates top
786 CursorBase ibegin(InsetBase * p)
789 it.push_back(CursorSlice(p));
794 CursorBase iend(InsetBase * p)
797 it.push_back(CursorSlice(p));
798 CursorSlice & cur = it.back();
799 cur.idx() = cur.lastidx();
800 cur.pos() = cur.lastpos();
807 ///////////////////////////////////////////////////////////////////
809 // The part below is the non-integrated rest of the original math
810 // cursor. This should be either generalized for texted or moved
811 // back to the math insets.
813 ///////////////////////////////////////////////////////////////////
815 #include "mathed/math_braceinset.h"
816 #include "mathed/math_charinset.h"
817 #include "mathed/math_commentinset.h"
818 #include "mathed/math_factory.h"
819 #include "mathed/math_gridinset.h"
820 #include "mathed/math_macroarg.h"
821 #include "mathed/math_macrotemplate.h"
822 #include "mathed/math_mathmlstream.h"
823 #include "mathed/math_scriptinset.h"
824 #include "mathed/math_spaceinset.h"
825 #include "mathed/math_support.h"
826 #include "mathed/math_unknowninset.h"
828 //#define FILEDEBUG 1
831 bool LCursor::isInside(InsetBase const * p)
833 for (unsigned i = 0; i < depth(); ++i)
834 if (cursor_[i].inset() == p)
840 bool LCursor::openable(MathAtom const & t)
851 // we can't move into anything new during selection
852 if (depth() == anchor_.size())
854 if (!ptr_cmp(t.nucleus(), anchor_[depth()].inset()))
861 bool LCursor::inNucleus()
863 return inset()->asMathInset()->asScriptInset() && idx() == 2;
869 autocorrect() = false;
876 if (pos() != 0 && openable(prevAtom())) {
878 push(nextAtom().nucleus());
879 inset()->idxLast(*this);
883 return posLeft() || idxLeft() || popLeft() || selection();
887 bool LCursor::right()
889 autocorrect() = false;
896 if (pos() != lastpos() && openable(nextAtom())) {
897 pushLeft(nextAtom().nucleus());
898 inset()->idxFirst(*this);
902 return posRight() || idxRight() || popRight() || selection();
906 bool positionable(CursorBase const & cursor, CursorBase const & anchor)
908 // avoid deeper nested insets when selecting
909 if (cursor.size() > anchor.size())
912 // anchor might be deeper, should have same path then
913 for (size_t i = 0; i < cursor.size(); ++i)
914 if (cursor[i].inset() != anchor[i].inset())
917 // position should be ok.
922 void LCursor::setScreenPos(int x, int y)
924 bool res = bruteFind(x, y, formula()->xlow(), formula()->xhigh(),
925 formula()->ylow(), formula()->yhigh());
927 // this can happen on creation of "math-display"
938 autocorrect() = false;
940 if (!inset()->idxHome(*this))
949 autocorrect() = false;
951 if (!inset()->idxEnd(*this))
958 void LCursor::plainErase()
964 void LCursor::markInsert()
966 cell().insert(pos(), MathAtom(new MathCharInset(0)));
970 void LCursor::markErase()
976 void LCursor::plainInsert(MathAtom const & t)
978 cell().insert(pos(), t);
983 void LCursor::insert2(string const & str)
991 void LCursor::insert(string const & str)
993 lyxerr << "inserting '" << str << "'" << endl;
995 for (string::const_iterator it = str.begin(); it != str.end(); ++it)
996 plainInsert(MathAtom(new MathCharInset(*it)));
1000 void LCursor::insert(char c)
1002 lyxerr << "inserting '" << c << "'" << endl;
1004 plainInsert(MathAtom(new MathCharInset(c)));
1008 void LCursor::insert(MathAtom const & t)
1016 void LCursor::niceInsert(string const & t)
1018 lyxerr << "*** LCursor::niceInsert 1: " << t << endl;
1028 void LCursor::niceInsert(MathAtom const & t)
1030 lyxerr << "*** LCursor::niceInsert 2: " << t << endl;
1032 string safe = grabAndEraseSelection();
1034 // enter the new inset and move the contents of the selection if possible
1035 if (t->isActive()) {
1037 pushLeft(nextAtom().nucleus());
1040 lyxerr << "*** LCursor::niceInsert 3: " << t << endl;
1044 void LCursor::insert(MathArray const & ar)
1049 cell().insert(pos(), ar);
1054 bool LCursor::backspace()
1056 autocorrect() = false;
1064 if (inset()->nargs() == 1 && depth() == 1 && lastpos() == 0)
1070 if (inMacroMode()) {
1071 MathUnknownInset * p = activeMacro();
1072 if (p->name().size() > 1) {
1073 p->setName(p->name().substr(0, p->name().size() - 1));
1078 if (pos() != 0 && prevAtom()->nargs() > 0) {
1079 // let's require two backspaces for 'big stuff' and
1080 // highlight on the first
1091 bool LCursor::erase()
1093 autocorrect() = false;
1102 // delete empty cells if possible
1103 if (pos() == lastpos() && inset()->idxDelete(idx()))
1106 // special behaviour when in last position of cell
1107 if (pos() == lastpos()) {
1108 bool one_cell = inset()->nargs() == 1;
1109 if (one_cell && depth() == 1 && lastpos() == 0)
1115 inset()->idxGlue(idx());
1119 if (pos() != lastpos() && inset()->nargs() > 0) {
1133 CursorBase save = cursor_;
1137 autocorrect() = false;
1142 bool LCursor::down()
1145 CursorBase save = cursor_;
1146 if (goUpDown(false))
1149 autocorrect() = false;
1154 void LCursor::macroModeClose()
1158 MathUnknownInset * p = activeMacro();
1160 string s = p->name();
1162 cell().erase(pos());
1164 // do nothing if the macro name is empty
1168 string const name = s.substr(1);
1170 // prevent entering of recursive macros
1171 if (formula()->lyxCode() == InsetOld::MATHMACRO_CODE
1172 && formula()->getInsetName() == name)
1173 lyxerr << "can't enter recursive macro" << endl;
1175 niceInsert(createMathInset(name));
1179 string LCursor::macroName()
1181 return inMacroMode() ? activeMacro()->name() : string();
1185 void LCursor::handleNest(MathAtom const & a, int c)
1188 asArray(grabAndEraseSelection(), t.nucleus()->cell(c));
1191 pushLeft(t.nucleus());
1195 int LCursor::targetX() const
1197 if (x_target() != -1)
1206 MathHullInset * LCursor::formula() const
1208 for (int i = cursor_.size() - 1; i >= 1; --i)
1209 if (cursor_[i].inset()->lyxCode() == InsetBase::MATH_CODE)
1210 return static_cast<MathHullInset *>(cursor_[i].inset());
1215 void LCursor::adjust(pos_type from, int diff)
1219 if (anchor().pos_ > from)
1220 anchor().pos_ += diff;
1221 // just to be on the safe side
1222 // theoretically unecessary
1227 bool LCursor::inMacroMode() const
1231 MathUnknownInset const * p = prevAtom()->asUnknownInset();
1232 return p && !p->final();
1236 MathUnknownInset * LCursor::activeMacro()
1238 return inMacroMode() ? prevAtom().nucleus()->asUnknownInset() : 0;
1242 bool LCursor::inMacroArgMode() const
1244 return pos() > 0 && prevAtom()->getChar() == '#';
1248 MathGridInset * LCursor::enclosingGrid(idx_type & idx) const
1250 for (MathInset::difference_type i = depth() - 1; i >= 0; --i) {
1251 MathInset * m = cursor_[i].inset()->asMathInset();
1254 MathGridInset * p = m->asGridInset();
1256 idx = cursor_[i].idx_;
1264 void LCursor::pullArg()
1268 MathArray ar = cell();
1271 cell().insert(pos(), ar);
1274 formula()->mutateToText();
1280 void LCursor::touch()
1284 CursorBase::const_iterator it = cursor_.begin();
1285 CursorBase::const_iterator et = cursor_.end();
1286 for ( ; it != et; ++it)
1292 void LCursor::normalize()
1294 if (idx() >= nargs()) {
1295 lyxerr << "this should not really happen - 1: "
1296 << idx() << ' ' << nargs()
1297 << " in: " << inset() << endl;
1299 idx() = min(idx(), lastidx());
1301 if (pos() > lastpos()) {
1302 lyxerr << "this should not really happen - 2: "
1303 << pos() << ' ' << lastpos() << " in idx: " << idx()
1305 WriteStream wi(lyxerr, false, true);
1306 inset()->asMathInset()->write(wi);
1309 pos() = min(pos(), lastpos());
1313 char LCursor::valign()
1316 MathGridInset * p = enclosingGrid(idx);
1317 return p ? p->valign() : '\0';
1321 char LCursor::halign()
1324 MathGridInset * p = enclosingGrid(idx);
1325 return p ? p->halign(idx % p->ncols()) : '\0';
1329 bool LCursor::goUpDown(bool up)
1331 // Be warned: The 'logic' implemented in this function is highly fragile.
1332 // A distance of one pixel or a '<' vs '<=' _really_ matters.
1333 // So fiddle around with it only if you know what you are doing!
1338 // check if we had something else in mind, if not, this is the future goal
1339 if (x_target() == -1)
1344 // try neigbouring script insets
1348 MathScriptInset const * p = prevAtom()->asScriptInset();
1349 if (p && p->has(up)) {
1352 idx() = up; // the superscript has index 1
1354 //lyxerr << "updown: handled by scriptinset to the left" << endl;
1360 if (pos() != lastpos()) {
1361 MathScriptInset const * p = nextAtom()->asScriptInset();
1362 if (p && p->has(up)) {
1366 //lyxerr << "updown: handled by scriptinset to the right" << endl;
1372 // try current cell for e.g. text insets
1373 if (inset()->idxUpDown2(*this, up))
1376 //xarray().boundingBox(xlow, xhigh, ylow, yhigh);
1381 //if (bruteFind(xo, yo, xlow, xhigh, ylow, yhigh)) {
1382 // lyxerr << "updown: handled by brute find in the same cell" << endl;
1386 // try to find an inset that knows better then we
1388 //lyxerr << "updown: We are in " << inset() << " idx: " << idx() << endl;
1390 if (inset()->idxUpDown(*this, up)) {
1391 // try to find best position within this inset
1397 // no such inset found, just take something "above"
1398 //lyxerr << "updown: handled by strange case" << endl;
1404 up ? formula()->ylow() : yo + 4,
1405 up ? yo - 4 : formula()->yhigh()
1409 // any improvement so far?
1412 if (up ? ynew < yo : ynew > yo)
1418 bool LCursor::bruteFind(int x, int y, int xlow, int xhigh, int ylow, int yhigh)
1420 CursorBase best_cursor;
1421 double best_dist = 1e10;
1423 CursorBase it = ibegin(formula());
1424 CursorBase et = iend(formula());
1426 // avoid invalid nesting when selecting
1427 if (!selection() || positionable(it, anchor_)) {
1429 CursorSlice & cur = it.back();
1430 cur.inset()->getCursorPos(cur, xo, yo);
1431 if (xlow <= xo && xo <= xhigh && ylow <= yo && yo <= yhigh) {
1432 double d = (x - xo) * (x - xo) + (y - yo) * (y - yo);
1433 //lyxerr << "x: " << x << " y: " << y << " d: " << endl;
1434 // '<=' in order to take the last possible position
1435 // this is important for clicking behind \sum in e.g. '\sum_i a'
1436 if (d <= best_dist) {
1448 if (best_dist < 1e10)
1449 cursor_ = best_cursor;
1450 return best_dist < 1e10;
1454 void LCursor::bruteFind2(int x, int y)
1456 double best_dist = 1e10;
1458 CursorBase it = cursor_;
1460 CursorBase et = cursor_;
1461 int n = et.back().asMathInset()->cell(et.back().idx_).size();
1463 for (int i = 0; ; ++i) {
1465 CursorSlice & cur = it.back();
1466 cur.inset()->getCursorPos(cur, xo, yo);
1467 double d = (x - xo) * (x - xo) + (y - yo) * (y - yo);
1468 // '<=' in order to take the last possible position
1469 // this is important for clicking behind \sum in e.g. '\sum_i a'
1470 lyxerr << "i: " << i << " d: " << d << " best: " << best_dist << endl;
1471 if (d <= best_dist) {
1482 bool LCursor::idxLineLast()
1484 idx() -= idx() % ncols();
1485 idx() += ncols() - 1;
1491 bool LCursor::idxLeft()
1493 return inset()->idxLeft(*this);
1497 bool LCursor::idxRight()
1499 return inset()->idxRight(*this);
1503 bool LCursor::script(bool up)
1505 // Hack to get \\^ and \\_ working
1506 lyxerr << "handling script: up: " << up << endl;
1507 if (inMacroMode() && macroName() == "\\") {
1509 niceInsert(createMathInset("mathcircumflex"));
1516 string safe = grabAndEraseSelection();
1518 // we are in a nucleus of a script inset, move to _our_ script
1519 inset()->asMathInset()->asScriptInset()->ensure(up);
1522 } else if (pos() != 0 && prevAtom()->asScriptInset()) {
1524 nextAtom().nucleus()->asScriptInset()->ensure(up);
1528 } else if (pos() != 0) {
1530 cell()[pos()] = MathAtom(new MathScriptInset(nextAtom(), up));
1535 plainInsert(MathAtom(new MathScriptInset(up)));
1537 nextAtom().nucleus()->asScriptInset()->ensure(up);
1547 bool LCursor::interpret(char c)
1549 //lyxerr << "interpret 2: '" << c << "'" << endl;
1551 if (inMacroArgMode()) {
1557 MathMacroTemplate const * p = formula()->asMacroTemplate();
1558 if (p && 1 <= n && n <= p->numargs())
1559 insert(MathAtom(new MathMacroArgument(c - '0')));
1561 insert(createMathInset("#"));
1562 interpret(c); // try again
1569 if (inMacroMode()) {
1570 string name = macroName();
1571 //lyxerr << "interpret name: '" << name << "'" << endl;
1574 activeMacro()->setName(activeMacro()->name() + c);
1578 // handle 'special char' macros
1583 if (currentMode() == MathInset::TEXT_MODE)
1584 niceInsert(createMathInset("textbackslash"));
1586 niceInsert(createMathInset("backslash"));
1587 } else if (c == '{') {
1588 niceInsert(MathAtom(new MathBraceInset));
1590 niceInsert(createMathInset(string(1, c)));
1595 // leave macro mode and try again if necessary
1598 niceInsert(MathAtom(new MathBraceInset));
1604 // This is annoying as one has to press <space> far too often.
1608 // leave autocorrect mode if necessary
1609 if (autocorrect() && c == ' ') {
1610 autocorrect() = false;
1615 // just clear selection on pressing the space bar
1616 if (selection() && c == ' ') {
1617 selection() = false;
1624 //lyxerr << "starting with macro" << endl;
1625 insert(MathAtom(new MathUnknownInset("\\", false)));
1630 if (currentMode() == MathInset::TEXT_MODE)
1636 if (currentMode() == MathInset::TEXT_MODE) {
1637 // insert spaces in text mode,
1638 // but suppress direct insertion of two spaces in a row
1639 // the still allows typing '<space>a<space>' and deleting the 'a', but
1640 // it is better than nothing...
1641 if (!pos() != 0 || prevAtom()->getChar() != ' ')
1645 if (pos() != 0 && prevAtom()->asSpaceInset()) {
1646 prevAtom().nucleus()->asSpaceInset()->incSpace();
1651 // if are at the very end, leave the formula
1652 return pos() != lastpos();
1665 if (c == '{' || c == '}' || c == '#' || c == '&' || c == '$') {
1666 niceInsert(createMathInset(string(1, c)));
1671 niceInsert(MathAtom(new MathCommentInset));
1675 // try auto-correction
1676 //if (autocorrect() && hasPrevAtom() && math_autocorrect(prevAtom(), c))
1679 // no special circumstances, so insert the character without any fuss
1681 autocorrect() = true;
1686 void LCursor::lockToggle()
1688 if (pos() != lastpos()) {
1689 // toggle previous inset ...
1690 nextAtom().nucleus()->lock(!nextAtom()->lock());
1691 } else if (popLeft() && pos() != lastpos()) {
1692 // ... or enclosing inset if we are in the last inset position
1693 nextAtom().nucleus()->lock(!nextAtom()->lock());
1699 CursorSlice LCursor::normalAnchor()
1701 if (anchor_.size() < depth()) {
1703 lyxerr << "unusual Anchor size" << endl;
1705 //lyx::BOOST_ASSERT(Anchor_.size() >= cursor.depth());
1706 // use Anchor on the same level as Cursor
1707 CursorSlice normal = anchor_[current_];
1709 if (depth() < anchor_.size() && !(normal < xx())) {
1710 // anchor is behind cursor -> move anchor behind the inset
1719 DispatchResult dispatch(LCursor & cur, FuncRequest const & cmd)
1721 // mouse clicks are somewhat special
1723 switch (cmd.action) {
1724 case LFUN_MOUSE_PRESS:
1725 case LFUN_MOUSE_MOTION:
1726 case LFUN_MOUSE_RELEASE:
1727 case LFUN_MOUSE_DOUBLE: {
1728 CursorSlice & pos = cursor_.back();
1732 if (x < cmd.x && pos() != 0) {
1733 DispatchResult const res = prevAtom().nucleus()->dispatch(cmd);
1734 if (res.dispatched())
1737 if (x > cmd.x && pos() != lastpos()) {
1738 DispatchResult const res = inset()->dispatch(cmd);
1739 if (res.dispatched())
1750 void LCursor::handleFont(string const & font)
1755 safe = grabAndEraseSelection();
1758 if (lastpos() != 0) {
1759 // something left in the cell
1761 // cursor in first position
1763 } else if (pos() == lastpos()) {
1764 // cursor in last position
1767 // cursor in between. split cell
1768 MathArray::iterator bt = cell().begin();
1769 MathAtom at = createMathInset(font);
1770 at.nucleus()->cell(0) = MathArray(bt, bt + pos());
1771 cell().erase(bt, bt + pos());
1776 // nothing left in the cell
1784 void LCursor::releaseMathCursor()
1787 formula()->insetUnlock(bv());
1791 bool LCursor::inMathed() const
1797 InsetBase * LCursor::nextInset()
1799 if (pos() == lastpos())
1802 return nextAtom().nucleus();
1803 Paragraph & par = paragraph();
1804 return par.isInset(pos()) ? par.getInset(pos()) : 0;
1808 InsetBase * LCursor::prevInset()
1813 return prevAtom().nucleus();
1814 Paragraph & par = paragraph();
1815 return par.isInset(pos() - 1) ? par.getInset(pos() - 1) : 0;
1819 void LCursor::message(string const & msg) const
1821 bv().owner()->getLyXFunc().setMessage(msg);
1825 void LCursor::errorMessage(string const & msg) const
1827 bv().owner()->getLyXFunc().setErrorMessage(msg);