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
8 * \author Dov Feldstern
10 * \author Stefan Schimanski
12 * Full author contact details are available in file CREDITS.
18 #include "BufferParams.h"
19 #include "BufferView.h"
20 #include "CoordCache.h"
22 #include "CutAndPaste.h"
23 #include "DispatchResult.h"
26 #include "FuncRequest.h"
29 #include "LyXAction.h"
31 #include "Paragraph.h"
32 #include "ParIterator.h"
34 #include "texstream.h"
36 #include "TextMetrics.h"
37 #include "TocBackend.h"
39 #include "support/debug.h"
40 #include "support/docstream.h"
41 #include "support/ExceptionMessage.h"
42 #include "support/gettext.h"
43 #include "support/lassert.h"
45 #include "insets/InsetTabular.h"
46 #include "insets/InsetText.h"
48 #include "mathed/InsetMath.h"
49 #include "mathed/InsetMathBrace.h"
50 #include "mathed/InsetMathEnsureMath.h"
51 #include "mathed/InsetMathScript.h"
52 #include "mathed/MacroTable.h"
53 #include "mathed/MathData.h"
54 #include "mathed/MathFactory.h"
55 #include "mathed/InsetMathMacro.h"
68 // Find position closest to (x, y) in cell given by iter.
69 // Used only in mathed
70 DocIterator bruteFind(Cursor const & c, int x, int y)
72 double best_dist = numeric_limits<double>::max();
79 et.pos() = et.lastpos();
80 for (size_t i = 0;; ++i) {
83 Inset const * inset = &it.inset();
84 CoordCache::Insets const & insetCache = c.bv().coordCache().getInsets();
86 // FIXME: in the case where the inset is not in the cache, this
87 // means that no part of it is visible on screen. In this case
88 // we don't do elaborate search and we just return the forwarded
89 // DocIterator at its beginning.
90 if (!insetCache.has(inset)) {
95 Point const o = insetCache.xy(inset);
96 inset->cursorPos(c.bv(), it.top(), c.boundary(), xo, yo);
97 // Convert to absolute
100 double d = (x - xo) * (x - xo) + (y - yo) * (y - yo);
101 // '<=' in order to take the last possible position
102 // this is important for clicking behind \sum in e.g. '\sum_i a'
103 LYXERR(Debug::DEBUG, "i: " << i << " d: " << d
104 << " best: " << best_dist);
105 if (d <= best_dist) {
120 CursorData::CursorData()
121 : DocIterator(), anchor_(),
122 selection_(false), mark_(false), word_selection_(false),
123 current_font(inherit_font),
124 autocorrect_(false), macromode_(false)
128 CursorData::CursorData(Buffer * buffer)
129 : DocIterator(buffer), anchor_(),
130 selection_(false), mark_(false), word_selection_(false),
131 current_font(inherit_font),
132 autocorrect_(false), macromode_(false)
136 CursorData::CursorData(DocIterator const & dit)
137 : DocIterator(dit), anchor_(),
138 selection_(false), mark_(false), word_selection_(false),
139 current_font(inherit_font),
140 autocorrect_(false), macromode_(false)
146 ostream & operator<<(ostream & os, CursorData const & cur)
148 os << "\n cursor: | anchor:\n";
149 for (size_t i = 0, n = cur.depth(); i != n; ++i) {
150 os << " " << cur[i] << " | ";
151 if (i < cur.anchor_.depth())
152 os << cur.anchor_[i];
154 os << "-------------------------------";
157 for (size_t i = cur.depth(), n = cur.anchor_.depth(); i < n; ++i) {
158 os << "------------------------------- | " << cur.anchor_[i] << "\n";
160 os << " selection: " << cur.selection_
161 // << " x_target: " << cur.x_target_
162 << " boundary: " << cur.boundary() << endl;
167 LyXErr & operator<<(LyXErr & os, CursorData const & cur)
174 // be careful: this is called from the bv's constructor, too, so
175 // bv functions are not yet available!
176 Cursor::Cursor(BufferView & bv)
177 : CursorData(&bv.buffer()), bv_(&bv),
178 x_target_(-1), textTargetOffset_(0),
179 beforeDispatchPosX_(0), beforeDispatchPosY_(0)
186 push_back(CursorSlice(buffer()->inset()));
187 anchor_ = doc_iterator_begin(buffer());
189 new_word_ = doc_iterator_begin(buffer());
197 void Cursor::setCursor(DocIterator const & cur)
199 DocIterator::operator=(cur);
203 void Cursor::setCursorSelectionTo(DocIterator dit)
207 while (i < dit.depth() && i < anchor_.depth() && dit[i] == anchor_[i])
209 if (i != dit.depth()) {
210 // otherwise the cursor is already normal
211 if (i == anchor_.depth())
212 // dit is a proper extension of the anchor_
214 else if (i + 1 < dit.depth()) {
215 // one has dit[i] != anchor_[i] but either dit[i-1] == anchor_[i-1]
216 // or i == 0. Remove excess.
218 if (dit[i] > anchor_[i])
219 // place dit after the inset it was in
228 void Cursor::setCursorToAnchor()
231 DocIterator normal = anchor_;
232 while (depth() < normal.depth())
234 if (depth() < anchor_.depth() && top() <= anchor_[depth() - 1])
241 void Cursor::setCursorData(CursorData const & data)
243 CursorData::operator=(data);
247 bool Cursor::getStatus(FuncRequest const & cmd, FuncStatus & status) const
251 // Try to fix cursor in case it is broken.
254 // Is this a function that acts on inset at point?
255 Inset * inset = cur.nextInset();
256 if (lyxaction.funcHasFlag(cmd.action(), LyXAction::AtPoint)
257 && inset && inset->getStatus(cur, cmd, status))
260 // This is, of course, a mess. Better create a new doc iterator and use
261 // this in Inset::getStatus. This might require an additional
262 // BufferView * arg, though (which should be avoided)
263 //Cursor safe = *this;
265 for ( ; cur.depth(); cur.pop()) {
266 //lyxerr << "\nCursor::getStatus: cmd: " << cmd << endl << *this << endl;
267 // LASSERT: Is it safe to continue here, or should we return?
268 LASSERT(cur.idx() <= cur.lastidx(), /**/);
269 LASSERT(cur.pit() <= cur.lastpit(), /**/);
270 LASSERT(cur.pos() <= cur.lastpos(), /**/);
272 // The inset's getStatus() will return 'true' if it made
273 // a definitive decision on whether it want to handle the
274 // request or not. The result of this decision is put into
275 // the 'status' parameter.
276 if (cur.inset().getStatus(cur, cmd, status)) {
285 void Cursor::saveBeforeDispatchPosXY()
287 getPos(beforeDispatchPosX_, beforeDispatchPosY_);
291 void Cursor::dispatch(FuncRequest const & cmd0)
293 LYXERR(Debug::ACTION, "Cursor::dispatch: cmd: " << cmd0 << '\n' << *this);
298 FuncRequest cmd = cmd0;
301 disp_ = DispatchResult();
305 // Is this a function that acts on inset at point?
306 if (lyxaction.funcHasFlag(cmd.action(), LyXAction::AtPoint)
308 disp_.dispatched(true);
309 disp_.screenUpdate(Update::FitCursor | Update::Force);
310 FuncRequest tmpcmd = cmd;
311 LYXERR(Debug::DEBUG, "Cursor::dispatch: (AtPoint) cmd: "
312 << cmd0 << endl << *this);
313 nextInset()->dispatch(*this, tmpcmd);
314 if (disp_.dispatched()) {
320 // store some values to be used inside of the handlers
321 beforeDispatchCursor_ = *this;
322 for (; depth(); pop(), boundary(false)) {
323 LYXERR(Debug::DEBUG, "Cursor::dispatch: cmd: "
324 << cmd0 << endl << *this);
326 // In any of these cases, the cursor is invalid, and we should
327 // try to save this document rather than crash.
328 LBUFERR(pos() <= lastpos());
329 LBUFERR(idx() <= lastidx());
330 LBUFERR(pit() <= lastpit());
332 // The common case is 'LFUN handled, need update', so make the
333 // LFUN handler's life easier by assuming this as default value.
334 // The handler can reset the update and val flags if necessary.
335 disp_.screenUpdate(Update::FitCursor | Update::Force);
336 disp_.dispatched(true);
337 inset().dispatch(*this, cmd);
338 if (disp_.dispatched())
342 // it completely to get a 'bomb early' behaviour in case this
343 // object will be used again.
344 if (!disp_.dispatched()) {
345 LYXERR(Debug::DEBUG, "RESTORING OLD CURSOR!");
346 // We might have invalidated the cursor when removing an empty
347 // paragraph while the cursor could not be moved out the inset
348 // while we initially thought we could. This might happen when
349 // a multiline inset becomes an inline inset when the second
350 // paragraph is removed.
351 if (safe.pit() > safe.lastpit()) {
352 safe.pit() = safe.lastpit();
353 safe.pos() = safe.lastpos();
356 disp_.screenUpdate(Update::None);
357 disp_.dispatched(false);
359 // restore the previous one because nested Cursor::dispatch calls
360 // are possible which would change it
361 beforeDispatchCursor_ = safe.beforeDispatchCursor_;
365 // NOTE: The code below has been copied to BufferView::dispatch.
366 // If you need to modify this, please update the other one too.
368 // notify insets we just left
370 old.beginUndoGroup();
372 bool badcursor = notifyCursorLeavesOrEnters(old, *this);
375 bv().resetInlineCompletionPos();
382 DispatchResult const & Cursor::result() const
388 BufferView & Cursor::bv() const
397 LBUFERR(depth() >= 1);
402 void Cursor::push(Inset & p)
404 push_back(CursorSlice(p));
405 p.setBuffer(*buffer());
409 void Cursor::pushBackward(Inset & p)
411 LASSERT(!empty(), return);
412 //lyxerr << "Entering inset " << t << " front" << endl;
418 bool Cursor::popBackward()
420 LASSERT(!empty(), return false);
428 bool Cursor::popForward()
430 LASSERT(!empty(), return false);
431 //lyxerr << "Leaving inset from in back" << endl;
432 const pos_type lp = (depth() > 1) ? (*this)[depth() - 2].lastpos() : 0;
436 pos() += lastpos() - lp + 1;
441 int Cursor::currentMode()
443 LASSERT(!empty(), return Inset::UNDECIDED_MODE);
444 for (int i = depth() - 1; i >= 0; --i) {
445 int res = operator[](i).inset().currentMode();
446 bool locked_mode = operator[](i).inset().lockedMode();
447 // Also return UNDECIDED_MODE when the mode is locked,
448 // as in this case it is treated the same as TEXT_MODE
449 if (res != Inset::UNDECIDED_MODE || locked_mode)
452 return Inset::TEXT_MODE;
456 void Cursor::getPos(int & x, int & y) const
458 Point p = bv().getPos(*this);
464 Row const & Cursor::textRow() const
466 CursorSlice const & cs = innerTextSlice();
467 ParagraphMetrics const & pm = bv().parMetrics(cs.text(), cs.pit());
468 return pm.getRow(pos(), boundary());
472 void Cursor::resetAnchor()
475 checkNewWordPosition();
479 void Cursor::markNewWordPosition()
481 if (lyxrc.spellcheck_continuously && inTexted() && new_word_.empty()) {
482 FontSpan nw = locateWord(WHOLE_WORD);
483 if (nw.size() == 1) {
484 LYXERR(Debug::DEBUG, "start new word: "
486 << " pos: " << nw.first);
493 void Cursor::clearNewWordPosition()
495 if (!new_word_.empty()) {
496 LYXERR(Debug::DEBUG, "clear new word: "
498 << " pos: " << pos());
504 void Cursor::checkNewWordPosition()
506 if (!lyxrc.spellcheck_continuously || new_word_.empty())
509 clearNewWordPosition();
511 // forget the position of the current new word if
512 // 1) the paragraph changes or
513 // 2) the count of nested insets changes or
514 // 3) the cursor pos is out of paragraph bound
515 if (pit() != new_word_.pit() ||
516 depth() != new_word_.depth() ||
517 new_word_.pos() > new_word_.lastpos()) {
518 clearNewWordPosition();
519 } else if (new_word_.fixIfBroken())
520 // 4) or the remembered position was "broken"
521 clearNewWordPosition();
523 FontSpan nw = locateWord(WHOLE_WORD);
525 FontSpan ow = new_word_.locateWord(WHOLE_WORD);
526 if (nw.intersect(ow).empty())
527 clearNewWordPosition();
529 LYXERR(Debug::DEBUG, "new word: "
531 << " pos: " << nw.first << ".." << nw.last);
533 clearNewWordPosition();
540 bool Cursor::posVisRight(bool skip_inset)
542 Cursor new_cur = *this; // where we will move to
543 pos_type left_pos; // position visually left of current cursor
544 pos_type right_pos; // position visually right of current cursor
546 getSurroundingPos(left_pos, right_pos);
548 LYXERR(Debug::RTL, left_pos <<"|"<< right_pos << " (pos: "<< pos() <<")");
550 // Are we at an inset?
551 new_cur.pos() = right_pos;
552 new_cur.boundary(false);
554 text()->checkAndActivateInsetVisual(new_cur, right_pos >= pos(), false)) {
555 // we actually move the cursor at the end of this
556 // function, for now we just keep track of the new
557 // position in new_cur...
558 LYXERR(Debug::RTL, "entering inset at: " << new_cur.pos());
561 // Are we already at rightmost pos in row?
562 else if (text()->empty() || right_pos == -1) {
565 if (!new_cur.posVisToNewRow(false)) {
566 LYXERR(Debug::RTL, "not moving!");
570 // we actually move the cursor at the end of this
571 // function, for now just keep track of the new
572 // position in new_cur...
573 LYXERR(Debug::RTL, "right edge, moving: " << int(new_cur.pit()) << ","
574 << int(new_cur.pos()) << "," << (new_cur.boundary() ? 1 : 0));
577 // normal movement to the right
580 // Recall, if the cursor is at position 'x', that
581 // means *before* the character at position 'x'. In
582 // RTL, "before" means "to the right of", in LTR, "to
583 // the left of". So currently our situation is this:
584 // the position to our right is 'right_pos' (i.e.,
585 // we're currently to the left of 'right_pos'). In
586 // order to move to the right, it depends whether or
587 // not the character at 'right_pos' is RTL.
588 bool const new_pos_is_RTL = paragraph().getFontSettings(
589 buffer()->params(), right_pos).isVisibleRightToLeft();
590 // If the character at 'right_pos' *is* LTR, then in
591 // order to move to the right of it, we need to be
592 // *after* 'right_pos', i.e., move to position
594 if (!new_pos_is_RTL) {
595 new_cur.pos() = right_pos + 1;
596 // set the boundary to true in two situations:
598 // 1. if new_pos is now lastpos, and we're in
599 // an RTL paragraph (this means that we're
600 // moving right to the end of an LTR chunk
601 // which is at the end of an RTL paragraph);
602 (new_cur.pos() == lastpos()
603 && paragraph().isRTL(buffer()->params()))
604 // 2. if the position *after* right_pos is RTL
605 // (we want to be *after* right_pos, not
606 // before right_pos + 1!)
607 || paragraph().getFontSettings(buffer()->params(),
608 new_cur.pos()).isVisibleRightToLeft()
610 new_cur.boundary(true);
611 else // set the boundary to false
612 new_cur.boundary(false);
614 // Otherwise (if the character at position 'right_pos'
615 // is RTL), then moving to the right of it is as easy
616 // as setting the new position to 'right_pos'.
618 new_cur.pos() = right_pos;
619 new_cur.boundary(false);
624 bool const moved = new_cur != *this || new_cur.boundary() != boundary();
627 LYXERR(Debug::RTL, "moving to: " << new_cur.pos()
628 << (new_cur.boundary() ? " (boundary)" : ""));
636 bool Cursor::posVisLeft(bool skip_inset)
638 Cursor new_cur = *this; // where we will move to
639 pos_type left_pos; // position visually left of current cursor
640 pos_type right_pos; // position visually right of current cursor
642 getSurroundingPos(left_pos, right_pos);
644 LYXERR(Debug::RTL, left_pos <<"|"<< right_pos << " (pos: "<< pos() <<")");
646 // Are we at an inset?
647 new_cur.pos() = left_pos;
648 new_cur.boundary(false);
650 text()->checkAndActivateInsetVisual(new_cur, left_pos >= pos(), true)) {
651 // we actually move the cursor at the end of this
652 // function, for now we just keep track of the new
653 // position in new_cur...
654 LYXERR(Debug::RTL, "entering inset at: " << new_cur.pos());
657 // Are we already at leftmost pos in row?
658 else if (text()->empty() || left_pos == -1) {
661 if (!new_cur.posVisToNewRow(true)) {
662 LYXERR(Debug::RTL, "not moving!");
666 // we actually move the cursor at the end of this
667 // function, for now just keep track of the new
668 // position in new_cur...
669 LYXERR(Debug::RTL, "left edge, moving: " << int(new_cur.pit()) << ","
670 << int(new_cur.pos()) << "," << (new_cur.boundary() ? 1 : 0));
673 // normal movement to the left
676 // Recall, if the cursor is at position 'x', that
677 // means *before* the character at position 'x'. In
678 // RTL, "before" means "to the right of", in LTR, "to
679 // the left of". So currently our situation is this:
680 // the position to our left is 'left_pos' (i.e., we're
681 // currently to the right of 'left_pos'). In order to
682 // move to the left, it depends whether or not the
683 // character at 'left_pos' is RTL.
684 bool const new_pos_is_RTL = paragraph().getFontSettings(
685 buffer()->params(), left_pos).isVisibleRightToLeft();
686 // If the character at 'left_pos' *is* RTL, then in
687 // order to move to the left of it, we need to be
688 // *after* 'left_pos', i.e., move to position
690 if (new_pos_is_RTL) {
691 new_cur.pos() = left_pos + 1;
692 // set the boundary to true in two situations:
694 // 1. if new_pos is now lastpos and we're in
695 // an LTR paragraph (this means that we're
696 // moving left to the end of an RTL chunk
697 // which is at the end of an LTR paragraph);
698 (new_cur.pos() == lastpos()
699 && !paragraph().isRTL(buffer()->params()))
700 // 2. if the position *after* left_pos is not
701 // RTL (we want to be *after* left_pos, not
702 // before left_pos + 1!)
703 || !paragraph().getFontSettings(buffer()->params(),
704 new_cur.pos()).isVisibleRightToLeft()
706 new_cur.boundary(true);
707 else // set the boundary to false
708 new_cur.boundary(false);
710 // Otherwise (if the character at position 'left_pos'
711 // is LTR), then moving to the left of it is as easy
712 // as setting the new position to 'left_pos'.
714 new_cur.pos() = left_pos;
715 new_cur.boundary(false);
720 bool const moved = new_cur != *this || new_cur.boundary() != boundary();
723 LYXERR(Debug::RTL, "moving to: " << new_cur.pos()
724 << (new_cur.boundary() ? " (boundary)" : ""));
734 // Return true on success
735 bool findNonVirtual(Row const & row, Row::const_iterator & cit, bool onleft)
738 while (cit != row.begin() && cit->isVirtual())
741 while (cit != row.end() && cit->isVirtual())
744 return cit != row.end() && !cit->isVirtual();
749 void Cursor::getSurroundingPos(pos_type & left_pos, pos_type & right_pos) const
751 // by default, we know nothing.
755 Row const & row = textRow();
757 Row::const_iterator cit = row.findElement(pos(), boundary(), dummy);
758 // Handle the case of empty row
759 if (cit == row.end()) {
761 right_pos = row.pos();
763 left_pos = row.pos() - 1;
767 // skip virtual elements and exit if no non-virtual one exists
768 if (!findNonVirtual(row, cit, !cit->isRTL()))
771 // if the position is at the left side of the element, we have to
772 // look at the previous element
773 if (pos() == cit->left_pos()) {
774 LYXERR(Debug::RTL, "getSurroundingPos(" << pos() << (boundary() ? "b" : "")
775 << "), AT LEFT of *cit=" << *cit);
776 // this one is easy (see common case below)
777 right_pos = pos() - (cit->isRTL() ? 1 : 0);
778 // at the left of the row
779 if (cit == row.begin())
782 if (!findNonVirtual(row, cit, true))
784 // [...[ is the row element, | is cursor position (! with boundary)
785 // [ 1 2 [ is a ltr row element with pos=1 and endpos=3
786 // ] 2 1] is an rtl row element with pos=1 and endpos=3
787 // [ 1 2 [ [|3 4 [ => (2, 3)
788 // or [ 1 2 [ ]!4 3 ] => (2, 4)
789 // or ] 2 1 ] [|3 4 [ => (1, 3)
790 // or ] 4 3 ] ]!2 1 ] => (3, 2)
791 left_pos = cit->right_pos() - (cit->isRTL() ? 0 : 1);
792 // happens with consecutive row of same direction
793 if (left_pos == right_pos) {
794 left_pos += cit->isRTL() ? 1 : -1;
797 // same code but with the element at the right
798 else if (pos() == cit->right_pos()) {
799 LYXERR(Debug::RTL, "getSurroundingPos(" << pos() << (boundary() ? "b" : "")
800 << "), AT RIGHT of *cit=" << *cit);
801 // this one is easy (see common case below)
802 left_pos = pos() - (cit->isRTL() ? 0 : 1);
803 // at the right of the row
804 if (cit + 1 == row.end())
807 if (!findNonVirtual(row, cit, false))
809 // [ 1 2![ [ 3 4 [ => (2, 3)
810 // or [ 1 2![ ] 4 3 ] => (2, 4)
811 // or ] 2 1|] [ 3 4 [ => (1, 3)
812 // or ] 4 3|] ] 2 1 ] => (3, 2)
813 right_pos = cit->left_pos() - (cit->isRTL() ? 1 : 0);
814 // happens with consecutive row of same direction
815 if (right_pos == left_pos)
816 right_pos += cit->isRTL() ? -1 : 1;
818 // common case: both positions are inside the row element
820 // [ 1 2|3 [ => (2, 3)
821 // or ] 3|2 1 ] => (3, 2)
822 left_pos = pos() - (cit->isRTL() ? 0 : 1);
823 right_pos = pos() - (cit->isRTL() ? 1 : 0);
826 // Note that debug message does not catch all early returns above
827 LYXERR(Debug::RTL,"getSurroundingPos(" << pos() << (boundary() ? "b" : "")
828 << ") => (" << left_pos << ", " << right_pos <<")");
832 bool Cursor::posVisToNewRow(bool movingLeft)
834 Row const & row = textRow();
835 bool par_is_LTR = !row.isRTL();
837 // Inside a table, determining whether to move to the next or
838 // previous row should be done based on the table's direction.
839 if (inset().asInsetTabular()) {
840 par_is_LTR = !inset().asInsetTabular()->isRightToLeft(*this);
841 LYXERR(Debug::RTL, "Inside table! par_is_LTR=" << (par_is_LTR ? 1 : 0));
844 // if moving left in an LTR paragraph or moving right in an
845 // RTL one, move to previous row
846 if (par_is_LTR == movingLeft) {
847 if (row.pos() == 0) { // we're at first row in paragraph
848 if (pit() == 0) // no previous paragraph! don't move
850 // move to last pos in previous par
854 } else { // move to previous row in this par
855 pos() = row.pos() - 1; // this is guaranteed to be in previous row
859 // if moving left in an RTL paragraph or moving right in an
860 // LTR one, move to next row
862 if (row.endpos() == lastpos()) { // we're at last row in paragraph
863 if (pit() == lastpit()) // last paragraph! don't move
865 // move to first row in next par
869 } else { // move to next row in this par
870 pos() = row.endpos();
875 // make sure we're at left-/right-most pos in new row
876 posVisToRowExtremity(!movingLeft);
882 void Cursor::posVisToRowExtremity(bool left)
884 LYXERR(Debug::RTL, "entering extremity: " << pit() << "," << pos() << ","
885 << (boundary() ? 1 : 0));
887 TextMetrics const & tm = bv_->textMetrics(text());
888 // Looking for extremities is like clicking on the left or the
890 int x = tm.origin().x_ + (left ? 0 : textRow().width());
892 pos() = tm.getPosNearX(textRow(), x, b);
895 LYXERR(Debug::RTL, "leaving extremity: " << pit() << "," << pos() << ","
896 << (boundary() ? 1 : 0));
900 bool Cursor::reverseDirectionNeeded() const
903 * We determine the directions based on the direction of the
904 * bottom() --- i.e., outermost --- paragraph, because that is
905 * the only way to achieve consistency of the arrow's movements
906 * within a paragraph, and thus avoid situations in which the
909 return bottom().paragraph().isRTL(bv().buffer().params());
913 CursorSlice Cursor::normalAnchor() const
917 // LASSERT: There have been several bugs around this code, that seem
918 // to involve failures to reset the anchor. We can at least not crash
919 // in release mode by resetting it ourselves.
920 if (anchor_.depth() < depth()) {
921 LYXERR0("Cursor is deeper than anchor. PLEASE REPORT.\nCursor is"
923 const_cast<DocIterator &>(anchor_) = *this;
926 CursorSlice normal = anchor_[depth() - 1];
927 if (depth() < anchor_.depth() && top() <= normal) {
928 // anchor is behind cursor -> move anchor behind the inset
935 CursorSlice Cursor::selBegin() const
939 return normalAnchor() < top() ? normalAnchor() : top();
943 CursorSlice Cursor::selEnd() const
947 return normalAnchor() > top() ? normalAnchor() : top();
951 DocIterator Cursor::selectionBegin() const
957 // FIXME: This is a work-around for the problem that
958 // CursorSlice doesn't keep track of the boundary.
959 if (normalAnchor() == top())
960 di = anchor_.boundary() > boundary() ? anchor_ : *this;
962 di = normalAnchor() < top() ? anchor_ : *this;
968 DocIterator Cursor::selectionEnd() const
974 // FIXME: This is a work-around for the problem that
975 // CursorSlice doesn't keep track of the boundary.
976 if (normalAnchor() == top())
977 di = anchor_.boundary() < boundary() ? anchor_ : *this;
979 di = normalAnchor() > top() ? anchor_ : *this;
981 if (di.depth() > depth()) {
989 void Cursor::setSelection()
992 if (idx() == normalAnchor().idx() &&
993 pit() == normalAnchor().pit() &&
994 pos() == normalAnchor().pos())
999 void Cursor::setSelection(DocIterator const & where, int n)
1008 void Cursor::clearSelection()
1011 setWordSelection(false);
1017 int Cursor::countInsetsInSelection(InsetCode const & inset_code)
1022 DocIterator from, to;
1023 from = selectionBegin();
1024 to = selectionEnd();
1028 if (!from.nextInset()) //move to closest inset
1029 from.forwardInset();
1031 while (!from.empty() && from < to) {
1032 Inset * inset = from.nextInset();
1035 if (inset->lyxCode() == inset_code)
1037 from.forwardInset();
1043 bool Cursor::insetInSelection(InsetCode const & inset_code)
1048 DocIterator from, to;
1049 from = selectionBegin();
1050 to = selectionEnd();
1052 if (!from.nextInset()) //move to closest inset
1053 from.forwardInset();
1055 while (!from.empty() && from < to) {
1056 Inset * inset = from.nextInset();
1059 if (inset->lyxCode() == inset_code)
1061 from.forwardInset();
1067 void Cursor::setTargetX(int x)
1070 textTargetOffset_ = 0;
1074 int Cursor::x_target() const
1080 void Cursor::clearTargetX()
1083 textTargetOffset_ = 0;
1087 void Cursor::updateTextTargetOffset()
1092 textTargetOffset_ = x - x_target_;
1096 void Cursor::info(odocstream & os, bool devel_mode) const
1098 for (int i = 1, n = depth(); i < n; ++i) {
1099 operator[](i).inset().infoize(os);
1103 Inset const * inset = prevInset();
1104 // prevInset() can return 0 in certain case.
1106 prevInset()->infoize2(os);
1109 InsetMath * math = inset().asInsetMath();
1111 os << _(", Inset: ") << math->id();
1112 os << _(", Cell: ") << idx();
1113 os << _(", Position: ") << pos();
1119 bool Cursor::selHandle(bool sel)
1121 //lyxerr << "Cursor::selHandle" << endl;
1124 if (sel == selection())
1128 cap::saveSelection(*this);
1137 ///////////////////////////////////////////////////////////////////
1140 // The part below is the non-integrated rest of the original math
1141 // cursor. This should be either generalized for texted or moved
1142 // back to mathed (in most cases to InsetMathNest).
1144 ///////////////////////////////////////////////////////////////////
1146 #include "mathed/InsetMathChar.h"
1147 #include "mathed/InsetMathGrid.h"
1148 #include "mathed/InsetMathScript.h"
1149 #include "mathed/InsetMathUnknown.h"
1150 #include "mathed/MathFactory.h"
1151 #include "mathed/MathStream.h"
1152 #include "mathed/MathSupport.h"
1157 bool Cursor::isInside(Inset const * p) const
1159 for (size_t i = 0; i != depth(); ++i)
1160 if (&operator[](i).inset() == p)
1166 void Cursor::leaveInset(Inset const & inset)
1168 for (size_t i = 0; i != depth(); ++i) {
1169 if (&operator[](i).inset() == &inset) {
1177 bool Cursor::openable(MathAtom const & t) const
1188 // we can't move into anything new during selection
1189 if (depth() >= anchor_.depth())
1191 if (t.nucleus() != &anchor_[depth()].inset())
1198 void Cursor::setScreenPos(int x, int /*y*/)
1201 //bruteFind(*this, x, y, 0, bv().workWidth(), 0, bv().workHeight());
1206 void Cursor::plainErase()
1208 cell().erase(pos());
1212 void Cursor::markInsert()
1214 insert(char_type(0));
1218 void Cursor::markErase()
1220 cell().erase(pos());
1224 void Cursor::plainInsert(MathAtom const & t)
1226 cell().insert(pos(), t);
1228 inset().setBuffer(bv_->buffer());
1230 checkBufferStructure();
1234 void Cursor::insert(docstring const & str)
1236 for (char_type c : str)
1241 void Cursor::insert(char_type c)
1243 //lyxerr << "Cursor::insert char '" << c << "'" << endl;
1244 LASSERT(!empty(), return);
1246 cap::selClearOrDel(*this);
1247 insert(new InsetMathChar(c));
1249 text()->insertChar(*this, c);
1254 void Cursor::insert(MathAtom const & t)
1256 //lyxerr << "Cursor::insert MathAtom '" << t << "'" << endl;
1258 cap::selClearOrDel(*this);
1263 void Cursor::insert(Inset * inset0)
1265 LASSERT(inset0, return);
1267 insert(MathAtom(inset0->asInsetMath()));
1269 text()->insertInset(*this, inset0);
1270 inset0->setBuffer(bv_->buffer());
1272 if (inset0->isLabeled())
1273 forceBufferUpdate();
1278 int Cursor::niceInsert(docstring const & t, Parse::flags f, bool enter)
1280 MathData ar(buffer());
1282 if (ar.size() == 1 && (enter || selection()))
1290 void Cursor::niceInsert(MathAtom const & t)
1293 docstring const safe = cap::grabAndEraseSelection(*this);
1295 // If possible, enter the new inset and move the contents of the selection
1296 if (t->isActive()) {
1298 // be careful here: don't use 'pushBackward(t)' as this we need to
1299 // push the clone, not the original
1300 pushBackward(*nextInset());
1301 // We may not use niceInsert here (recursion)
1302 MathData ar(buffer());
1305 } else if (t->asMacro() && !safe.empty()) {
1306 MathData ar(buffer());
1308 docstring const name = t->asMacro()->name();
1309 MacroData const * data = buffer()->getMacro(name);
1310 if (data && data->numargs() - data->optionals() > 0) {
1311 plainInsert(MathAtom(new InsetMathBrace(ar)));
1318 void Cursor::insert(MathData const & ar)
1322 cap::eraseSelection(*this);
1323 cell().insert(pos(), ar);
1325 // FIXME audit setBuffer calls
1326 inset().setBuffer(bv_->buffer());
1330 bool Cursor::backspace(bool const force)
1333 cap::eraseSelection(*this);
1338 // If empty cell, and not part of a big cell
1339 if (lastpos() == 0 && inset().nargs() == 1) {
1341 // Directly delete empty cell: [|[]] => [|]
1347 // [|], can not delete from inside
1358 if (inMacroMode()) {
1359 InsetMathUnknown * p = activeMacro();
1360 if (p->name().size() > 1) {
1361 p->setName(p->name().substr(0, p->name().size() - 1));
1366 if (pos() != 0 && !force && prevAtom()->confirmDeletion()) {
1367 // let's require two backspaces for 'big stuff' and
1368 // highlight on the first
1380 bool Cursor::erase(bool const force)
1386 cap::eraseSelection(*this);
1390 // delete empty cells if possible
1391 if (pos() == lastpos() && inset().idxDelete(idx()))
1394 // special behaviour when in last position of cell
1395 if (pos() == lastpos()) {
1396 bool one_cell = inset().nargs() == 1;
1397 if (one_cell && lastpos() == 0) {
1399 // Directly delete empty cell: [|[]] => [|]
1405 // [|], can not delete from inside
1410 inset().idxGlue(idx());
1414 // 'clever' UI hack: only erase large items if previously slected
1415 if (pos() != lastpos() && !force && nextAtom()->confirmDeletion()) {
1430 DocIterator save = *this;
1431 FuncRequest cmd(selection() ? LFUN_UP_SELECT : LFUN_UP, docstring());
1432 this->dispatch(cmd);
1433 if (disp_.dispatched())
1443 DocIterator save = *this;
1444 FuncRequest cmd(selection() ? LFUN_DOWN_SELECT : LFUN_DOWN, docstring());
1445 this->dispatch(cmd);
1446 if (disp_.dispatched())
1453 bool Cursor::macroModeClose(bool cancel)
1457 InsetMathUnknown * p = activeMacro();
1459 MathData selection(buffer());
1460 asArray(p->selection(), selection);
1461 docstring const s = p->name();
1463 cell().erase(pos());
1465 // trigger updates of macros, at least, if no full
1466 // updates take place anyway
1467 screenUpdateFlags(Update::Force);
1469 // do nothing if the macro name is empty
1470 if (s == "\\" || cancel) {
1474 docstring const name = s.substr(1);
1475 InsetMathNest * const in = inset().asInsetMath()->asNestInset();
1476 if (in && in->interpretString(*this, s))
1478 bool const user_macro = buffer()->getMacro(name, *this, false);
1479 MathAtom atom = user_macro ? MathAtom(new InsetMathMacro(buffer(), name))
1480 : createInsetMath(name, buffer());
1482 // try to put argument into macro, if we just inserted a macro
1483 bool macroArg = false;
1484 InsetMathMacro * atomAsMacro = atom.nucleus()->asMacro();
1486 // macros here are still unfolded (in init mode in fact). So
1487 // we have to resolve the macro here manually and check its arity
1488 // to put the selection behind it if arity > 0.
1489 MacroData const * data = buffer()->getMacro(atomAsMacro->name());
1490 if (!selection.empty() && data && data->numargs() - data->optionals() > 0) {
1492 atomAsMacro->setDisplayMode(InsetMathMacro::DISPLAY_INTERACTIVE_INIT, 1);
1494 // non-greedy case. Do not touch the arguments behind
1495 atomAsMacro->setDisplayMode(InsetMathMacro::DISPLAY_INTERACTIVE_INIT, 0);
1498 // insert remembered selection into first argument of a non-macro
1499 else if (atom.nucleus()->nargs() > 0)
1500 atom.nucleus()->cell(0).append(selection);
1502 MathWordList const & words = mathedWordList();
1503 MathWordList::const_iterator it = words.find(name);
1504 bool keep_mathmode = user_macro
1505 || (it != words.end() && (it->second.inset == "font"
1506 || it->second.inset == "oldfont"
1507 || it->second.inset == "mbox"));
1508 bool ert_macro = !user_macro && it == words.end() && atomAsMacro;
1510 if (in && in->currentMode() == Inset::TEXT_MODE
1511 && atom.nucleus()->currentMode() == Inset::MATH_MODE
1512 && name != from_ascii("ensuremath") && !ert_macro) {
1513 MathAtom at(new InsetMathEnsureMath(buffer()));
1514 at.nucleus()->cell(0).push_back(atom);
1517 } else if (in && in->currentMode() == Inset::MATH_MODE
1518 && atom.nucleus()->currentMode() == Inset::TEXT_MODE
1519 && !keep_mathmode) {
1520 MathAtom at = createInsetMath("text", buffer());
1521 at.nucleus()->cell(0).push_back(atom);
1527 // finally put the macro argument behind, if needed
1529 if (selection.size() > 1 || selection[0]->asScriptInset())
1530 plainInsert(MathAtom(new InsetMathBrace(selection)));
1539 docstring Cursor::macroName()
1541 return inMacroMode() ? activeMacro()->name() : docstring();
1545 void Cursor::handleNest(MathAtom const & a, int c)
1547 //lyxerr << "Cursor::handleNest: " << c << endl;
1549 asArray(cap::grabAndEraseSelection(*this), t.nucleus()->cell(c));
1552 pushBackward(*nextInset());
1556 int Cursor::targetX() const
1558 if (x_target() != -1)
1567 int Cursor::textTargetOffset() const
1569 return textTargetOffset_;
1573 void Cursor::setTargetX()
1582 bool Cursor::inMacroMode() const
1586 if (pos() == 0 || cell().empty())
1588 InsetMathUnknown const * p = prevAtom()->asUnknownInset();
1589 return p && !p->final();
1593 InsetMathUnknown * Cursor::activeMacro()
1595 return inMacroMode() ? prevAtom().nucleus()->asUnknownInset() : 0;
1599 InsetMathUnknown const * Cursor::activeMacro() const
1601 return inMacroMode() ? prevAtom().nucleus()->asUnknownInset() : 0;
1605 void Cursor::pullArg()
1608 MathData ar = cell();
1609 if (popBackward() && inMathed()) {
1611 cell().insert(pos(), ar);
1614 //formula()->mutateToText();
1619 void Cursor::touch()
1623 DocIterator::const_iterator it = begin();
1624 DocIterator::const_iterator et = end();
1625 for ( ; it != et; ++it)
1631 void Cursor::normalize()
1633 if (idx() > lastidx()) {
1634 lyxerr << "this should not really happen - 1: "
1635 << idx() << ' ' << nargs()
1636 << " in: " << &inset() << endl;
1640 if (pos() > lastpos()) {
1641 lyxerr << "this should not really happen - 2: "
1642 << pos() << ' ' << lastpos() << " in idx: " << idx()
1644 odocstringstream os;
1645 otexrowstream ots(os);
1646 WriteStream wi(ots, false, true, WriteStream::wsDefault);
1647 inset().asInsetMath()->write(wi);
1648 lyxerr << to_utf8(os.str()) << endl;
1654 bool Cursor::upDownInMath(bool up)
1656 // Be warned: The 'logic' implemented in this function is highly
1657 // fragile. A distance of one pixel or a '<' vs '<=' _really
1658 // matters. So fiddle around with it only if you think you know
1659 // what you are doing!
1663 xo = beforeDispatchPosX_;
1665 // check if we had something else in mind, if not, this is the future
1667 if (x_target_ == -1)
1669 else if (inset().asInsetText() && xo - textTargetOffset() != x_target()) {
1670 // In text mode inside the line (not left or right) possibly set a new target_x,
1671 // but only if we are somewhere else than the previous target-offset.
1673 // We want to keep the x-target on subsequent up/down movements
1674 // that cross beyond the end of short lines. Thus a special
1675 // handling when the cursor is at the end of line: Use the new
1676 // x-target only if the old one was before the end of line
1677 // or the old one was after the beginning of the line
1678 bool inRTL = innerParagraph().isRTL(bv().buffer().params());
1682 left = pos() == textRow().endpos();
1683 right = pos() == textRow().pos();
1685 left = pos() == textRow().pos();
1686 right = pos() == textRow().endpos();
1688 if ((!left && !right) ||
1689 (left && !right && xo < x_target_) ||
1690 (!left && right && x_target_ < xo))
1697 // try neigbouring script insets
1699 if (inMathed() && !selection()) {
1702 InsetMathScript const * p = prevAtom()->asScriptInset();
1703 if (p && p->has(up)) {
1705 push(*const_cast<InsetMathScript*>(p));
1706 idx() = p->idxOfScript(up);
1709 // we went in the right direction? Otherwise don't jump into the script
1713 int oy = beforeDispatchPosY_;
1714 if ((!up && y <= oy) ||
1723 if (pos() != lastpos()) {
1724 InsetMathScript const * p = nextAtom()->asScriptInset();
1725 if (p && p->has(up)) {
1726 push(*const_cast<InsetMathScript*>(p));
1727 idx() = p->idxOfScript(up);
1730 // we went in the right direction? Otherwise don't jump into the script
1734 int oy = beforeDispatchPosY_;
1735 if ((!up && y <= oy) ||
1744 // try to find an inset that knows better then we,
1745 if (inset().idxUpDown(*this, up)) {
1746 //lyxerr << "idxUpDown triggered" << endl;
1747 // try to find best position within this inset
1749 setCursor(bruteFind(*this, xo, yo));
1753 // any improvement going just out of inset?
1754 if (popBackward() && inMathed()) {
1755 //lyxerr << "updown: popBackward succeeded" << endl;
1758 int yold = beforeDispatchPosY_;
1760 if (up ? ynew < yold : ynew > yold)
1764 // no success, we are probably at the document top or bottom
1770 InsetMath & Cursor::nextMath()
1772 return *nextAtom().nucleus();
1776 InsetMath & Cursor::prevMath()
1778 return *prevAtom().nucleus();
1782 bool Cursor::mathForward(bool word)
1784 LASSERT(inMathed(), return false);
1785 if (pos() < lastpos()) {
1787 // word: skip a group of insets of the form X*(B*|R*|P*) (greedy
1788 // match) where X is any math class, B is mathbin, R is mathrel, and
1789 // P is mathpunct. Make sure that the following remains true:
1790 // mathForward(true); mathBackward(true); mathForward(true)
1791 // is the same as mathForward(true) and
1792 // mathBackward(true); mathForward(true); mathBackward(true)
1793 // is the same as mathBackward(true).
1794 MathClass mc = nextMath().mathClass();
1797 while (pos() < lastpos() && mc == nextMath().mathClass());
1798 if (pos() < lastpos() &&
1799 ((mc = nextMath().mathClass()) == MC_BIN ||
1800 mc == MC_REL || mc == MC_PUNCT))
1803 while (pos() < lastpos() && mc == nextMath().mathClass());
1804 } else if (openable(nextAtom())) {
1805 // single step: try to enter the next inset
1806 pushBackward(nextMath());
1807 inset().idxFirst(*this);
1812 if (inset().idxForward(*this))
1814 // try to pop forwards --- but don't pop out of math! leave that to
1816 int s = depth() - 2;
1817 if (s >= 0 && operator[](s).inset().asInsetMath())
1818 return popForward();
1823 bool Cursor::mathBackward(bool word)
1825 LASSERT(inMathed(), return false);
1828 // word: skip a group of insets. See the comment in mathForward.
1829 MathClass mc = prevMath().mathClass();
1832 while (pos() > 0 && mc == prevMath().mathClass());
1833 if (pos() > 0 && (mc == MC_BIN || mc == MC_REL || mc == MC_PUNCT)) {
1834 mc = prevMath().mathClass();
1837 while (pos() > 0 && mc == prevMath().mathClass());
1839 } else if (openable(prevAtom())) {
1840 // single step: try to enter the preceding inset
1843 inset().idxLast(*this);
1848 if (inset().idxBackward(*this))
1850 // try to pop backwards --- but don't pop out of math! leave that to
1852 int s = depth() - 2;
1853 if (s >= 0 && operator[](s).inset().asInsetMath())
1854 return popBackward();
1859 bool Cursor::atFirstOrLastRow(bool up)
1861 TextMetrics const & tm = bv_->textMetrics(text());
1862 ParagraphMetrics const & pm = tm.parMetrics(pit());
1865 if (pos() && boundary())
1866 row = pm.pos2row(pos() - 1);
1868 row = pm.pos2row(pos());
1871 if (pit() == 0 && row == 0)
1874 if (pit() + 1 >= int(text()->paragraphs().size()) &&
1875 row + 1 >= int(pm.rows().size()))
1882 bool Cursor::upDownInText(bool up, bool & updateNeeded)
1884 LASSERT(text(), return false);
1890 xo = beforeDispatchPosX_;
1892 // update the targetX - this is here before the "return false"
1893 // to set a new target which can be used by InsetTexts above
1894 // if we cannot move up/down inside this inset anymore
1895 if (x_target_ == -1)
1897 else if (xo - textTargetOffset() != x_target() &&
1898 depth() == beforeDispatchCursor_.depth()) {
1899 // In text mode inside the line (not left or right)
1900 // possibly set a new target_x, but only if we are
1901 // somewhere else than the previous target-offset.
1903 // We want to keep the x-target on subsequent up/down
1904 // movements that cross beyond the end of short lines.
1905 // Thus a special handling when the cursor is at the
1906 // end of line: Use the new x-target only if the old
1907 // one was before the end of line or the old one was
1908 // after the beginning of the line
1909 bool inRTL = innerParagraph().isRTL(bv().buffer().params());
1913 left = pos() == textRow().endpos();
1914 right = pos() == textRow().pos();
1916 left = pos() == textRow().pos();
1917 right = pos() == textRow().endpos();
1919 if ((!left && !right) ||
1920 (left && !right && xo < x_target_) ||
1921 (!left && right && x_target_ < xo))
1928 // first get the current line
1929 TextMetrics & tm = bv_->textMetrics(text());
1930 ParagraphMetrics const & pm = tm.parMetrics(pit());
1932 if (pos() && boundary())
1933 row = pm.pos2row(pos() - 1);
1935 row = pm.pos2row(pos());
1937 if (atFirstOrLastRow(up)) {
1938 // Is there a place for the cursor to go ? If yes, we
1939 // can execute the DEPM, otherwise we should keep the
1940 // paragraph to host the cursor.
1941 Cursor dummy = *this;
1942 bool valid_destination = false;
1943 for(; dummy.depth(); dummy.pop())
1944 if (!dummy.atFirstOrLastRow(up)) {
1945 valid_destination = true;
1949 // will a next dispatch follow and if there is a new
1950 // dispatch will it move the cursor out ?
1951 if (depth() > 1 && valid_destination) {
1952 // The cursor hasn't changed yet. This happens when
1953 // you e.g. move out of an inset. And to give the
1954 // DEPM the possibility of doing something we must
1955 // provide it with two different cursors. (Lgb, vfr)
1957 dummy.pos() = dummy.pos() == 0 ? dummy.lastpos() : 0;
1958 dummy.pit() = dummy.pit() == 0 ? dummy.lastpit() : 0;
1960 updateNeeded |= bv().checkDepm(dummy, *this);
1961 updateTextTargetOffset();
1963 forceBufferUpdate();
1968 // with and without selection are handled differently
1970 int yo = bv().getPos(*this).y_;
1972 // To next/previous row
1973 // FIXME: the y position is often guessed wrongly across styles and
1974 // insets, which leads to weird behaviour.
1976 tm.editXY(*this, xo, yo - textRow().ascent() - 1);
1978 tm.editXY(*this, xo, yo + textRow().descent() + 1);
1979 x_target_ = old.x_target_;
1982 // This happens when you move out of an inset.
1983 // And to give the DEPM the possibility of doing
1984 // something we must provide it with two different
1986 Cursor dummy = *this;
1989 if (bv().checkDepm(dummy, old)) {
1990 updateNeeded = true;
1991 // Make sure that cur gets back whatever happened to dummy (Lgb)
1994 if (inTexted() && pos() && paragraph().isEnvSeparator(pos() - 1))
1997 // if there is a selection, we stay out of any inset,
1998 // and just jump to the right position:
2004 } else if (pit() > 0) {
2006 TextMetrics & tm = bv_->textMetrics(text());
2007 if (!tm.contains(pit()))
2008 tm.newParMetricsUp();
2009 ParagraphMetrics const & pmcur = tm.parMetrics(pit());
2010 next_row = pmcur.rows().size() - 1;
2013 if (row + 1 < int(pm.rows().size())) {
2015 } else if (pit() + 1 < int(text()->paragraphs().size())) {
2017 TextMetrics & tm = bv_->textMetrics(text());
2018 if (!tm.contains(pit()))
2019 tm.newParMetricsDown();
2024 Row const & real_next_row = tm.parMetrics(pit()).rows()[next_row];
2026 top().pos() = tm.getPosNearX(real_next_row, xo, bound);
2028 // When selection==false, this is done by TextMetrics::editXY
2031 updateNeeded |= bv().checkDepm(*this, old);
2035 forceBufferUpdate();
2036 updateTextTargetOffset();
2041 void Cursor::handleFont(string const & font)
2043 LYXERR(Debug::DEBUG, font);
2047 safe = cap::grabAndEraseSelection(*this);
2052 if (lastpos() != 0) {
2053 // something left in the cell
2055 // cursor in first position
2057 } else if (pos() == lastpos()) {
2058 // cursor in last position
2061 // cursor in between. split cell
2062 MathData::iterator bt = cell().begin();
2063 MathAtom at = createInsetMath(from_utf8(font), buffer());
2064 at.nucleus()->cell(0) = MathData(buffer(), bt, bt + pos());
2065 cell().erase(bt, bt + pos());
2070 // nothing left in the cell
2079 void Cursor::message(docstring const & msg) const
2081 disp_.setMessage(msg);
2085 void Cursor::errorMessage(docstring const & msg) const
2087 disp_.setMessage(msg);
2088 disp_.setError(true);
2094 docstring parbreak(Cursor const * cur)
2096 odocstringstream os;
2098 // only add blank line if we're not in a ParbreakIsNewline situation
2099 if (!cur->inset().getLayout().parbreakIsNewline()
2100 && !cur->paragraph().layout().parbreak_is_newline)
2108 docstring Cursor::selectionAsString(bool with_label) const
2114 return cap::grabSelection(*this);
2116 int const label = with_label
2117 ? AS_STR_LABEL | AS_STR_INSETS : AS_STR_INSETS;
2119 idx_type const startidx = selBegin().idx();
2120 idx_type const endidx = selEnd().idx();
2121 if (startidx != endidx) {
2122 // multicell selection
2123 InsetTabular * table = inset().asInsetTabular();
2124 LASSERT(table, return docstring());
2125 return table->asString(startidx, endidx);
2128 ParagraphList const & pars = text()->paragraphs();
2130 pit_type const startpit = selBegin().pit();
2131 pit_type const endpit = selEnd().pit();
2132 size_t const startpos = selBegin().pos();
2133 size_t const endpos = selEnd().pos();
2135 if (startpit == endpit)
2136 return pars[startpit].asString(startpos, endpos, label);
2138 // First paragraph in selection
2139 docstring result = pars[startpit].
2140 asString(startpos, pars[startpit].size(), label)
2143 // The paragraphs in between (if any)
2144 for (pit_type pit = startpit + 1; pit != endpit; ++pit) {
2145 Paragraph const & par = pars[pit];
2146 result += par.asString(0, par.size(), label)
2150 // Last paragraph in selection
2151 result += pars[endpit].asString(0, endpos, label);
2157 docstring Cursor::currentState(bool devel_mode) const
2160 odocstringstream os;
2161 info(os, devel_mode);
2166 return text()->currentState(*this, devel_mode);
2172 docstring Cursor::getPossibleLabel() const
2174 return inMathed() ? from_ascii("eq:") : text()->getPossibleLabel(*this);
2178 void Cursor::undispatched() const
2180 disp_.dispatched(false);
2184 void Cursor::dispatched() const
2186 disp_.dispatched(true);
2190 void Cursor::screenUpdateFlags(Update::flags f) const
2192 disp_.screenUpdate(f);
2196 void Cursor::forceBufferUpdate() const
2198 disp_.forceBufferUpdate();
2202 void Cursor::clearBufferUpdate() const
2204 disp_.clearBufferUpdate();
2208 bool Cursor::needBufferUpdate() const
2210 return disp_.needBufferUpdate();
2214 void Cursor::noScreenUpdate() const
2216 disp_.screenUpdate(Update::None);
2220 Font Cursor::getFont() const
2222 // The logic here should more or less match to the
2223 // Cursor::setCurrentFont logic, i.e. the cursor height should
2224 // give a hint what will happen if a character is entered.
2225 // FIXME: this is not the case, what about removing this method ? (see #10478).
2227 // HACK. far from being perfect...
2229 CursorSlice const & sl = innerTextSlice();
2230 Text const & text = *sl.text();
2231 Paragraph const & par = text.getPar(sl.pit());
2233 // on boundary, so we are really at the character before
2234 pos_type pos = sl.pos();
2235 if (pos > 0 && boundary())
2238 // on space? Take the font before (only for RTL boundary stay)
2240 TextMetrics const & tm = bv().textMetrics(&text);
2241 if (pos == sl.lastpos()
2242 || (par.isSeparator(pos)
2243 && !tm.isRTLBoundary(sl.pit(), pos)))
2247 // get font at the position
2248 Font font = par.getFont(buffer()->params(), pos,
2249 text.outerFont(sl.pit()));
2255 bool Cursor::fixIfBroken()
2257 bool const broken_cursor = DocIterator::fixIfBroken();
2258 bool const broken_anchor = anchor_.fixIfBroken();
2260 if (broken_cursor || broken_anchor) {
2261 clearNewWordPosition();
2269 void Cursor::sanitize()
2271 setBuffer(&bv_->buffer());
2272 DocIterator::sanitize();
2273 new_word_.sanitize();
2281 bool notifyCursorLeavesOrEnters(Cursor const & old, Cursor & cur)
2283 // find inset in common
2285 for (i = 0; i < old.depth() && i < cur.depth(); ++i) {
2286 if (&old[i].inset() != &cur[i].inset())
2290 // update words if we just moved to another paragraph
2291 if (i == old.depth() && i == cur.depth()
2292 && !cur.buffer()->isClean()
2293 && cur.inTexted() && old.inTexted()
2294 && cur.pit() != old.pit()) {
2295 old.paragraph().updateWords();
2298 // notify everything on top of the common part in old cursor,
2299 // but stop if the inset claims the cursor to be invalid now
2300 for (size_type j = i; j < old.depth(); ++j) {
2301 Cursor inset_pos = old;
2302 inset_pos.cutOff(j);
2303 if (old[j].inset().notifyCursorLeaves(inset_pos, cur))
2307 // notify everything on top of the common part in new cursor,
2308 // but stop if the inset claims the cursor to be invalid now
2309 for (; i < cur.depth(); ++i) {
2310 if (cur[i].inset().notifyCursorEnters(cur))
2318 void Cursor::setCurrentFont()
2320 CursorSlice const & cs = innerTextSlice();
2321 Paragraph const & par = cs.paragraph();
2322 pos_type cpit = cs.pit();
2323 pos_type cpos = cs.pos();
2324 Text const & ctext = *cs.text();
2325 TextMetrics const & tm = bv().textMetrics(&ctext);
2327 // are we behind previous char in fact? -> go to that char
2328 if (cpos > 0 && boundary())
2331 // find position to take the font from
2333 // paragraph end? -> font of last char
2334 if (cpos == lastpos())
2336 // on space? -> look at the words in front of space
2337 else if (cpos > 0 && par.isSeparator(cpos)) {
2338 // abc| def -> font of c
2339 // abc |[WERBEH], i.e. boundary==true -> font of c
2340 // abc [WERBEH]| def, font of the space
2341 if (!tm.isRTLBoundary(cpit, cpos))
2347 BufferParams const & bufparams = buffer()->params();
2348 current_font = par.getFontSettings(bufparams, cpos);
2349 real_current_font = tm.displayFont(cpit, cpos);
2351 // special case for paragraph end
2352 if (cs.pos() == lastpos()
2353 && tm.isRTLBoundary(cpit, cs.pos())
2355 Language const * lang = par.getParLanguage(bufparams);
2356 current_font.setLanguage(lang);
2357 current_font.fontInfo().setNumber(FONT_OFF);
2358 real_current_font.setLanguage(lang);
2359 real_current_font.fontInfo().setNumber(FONT_OFF);
2364 bool Cursor::textUndo()
2366 if (!buffer()->undo().textUndo(*this))
2373 bool Cursor::textRedo()
2375 if (!buffer()->undo().textRedo(*this))
2382 void Cursor::finishUndo() const
2384 buffer()->undo().finishUndo();
2388 void Cursor::beginUndoGroup() const
2390 buffer()->undo().beginUndoGroup(*this);
2394 void Cursor::endUndoGroup() const
2396 buffer()->undo().endUndoGroup(*this);
2400 void Cursor::recordUndo(pit_type from, pit_type to) const
2402 buffer()->undo().recordUndo(*this, from, to);
2406 void Cursor::recordUndo(pit_type from) const
2408 buffer()->undo().recordUndo(*this, from, pit());
2412 void Cursor::recordUndo(UndoKind kind) const
2414 buffer()->undo().recordUndo(*this, kind);
2418 void Cursor::recordUndoInset(Inset const * in) const
2420 buffer()->undo().recordUndoInset(*this, in);
2424 void Cursor::recordUndoFullBuffer() const
2426 buffer()->undo().recordUndoFullBuffer(*this);
2430 void Cursor::recordUndoBufferParams() const
2432 buffer()->undo().recordUndoBufferParams(*this);
2436 void Cursor::recordUndoSelection() const
2439 if (cap::multipleCellsSelected(*this))
2444 buffer()->undo().recordUndo(*this,
2445 selBegin().pit(), selEnd().pit());
2450 void Cursor::checkBufferStructure()
2452 Buffer const * master = buffer()->masterBuffer();
2453 master->tocBackend().updateItem(*this);
2454 if (master != buffer() && !master->hasGuiDelegate())
2455 // In case the master has no gui associated with it,
2456 // the TocItem is not updated (part of bug 5699).
2457 buffer()->tocBackend().updateItem(*this);
2459 // If the last tracked change of the paragraph has just been
2460 // deleted, then we need to recompute the buffer flag
2461 // tracked_changes_present_.
2462 if (inTexted() && paragraph().isChangeUpdateRequired())
2463 disp_.forceChangesUpdate();
2467 bool Cursor::confirmDeletion(bool const before) const
2470 if (Inset const * inset = before ? prevInset() : nextInset())
2471 return inset->confirmDeletion();
2473 DocIterator dit = selectionBegin();
2474 CursorSlice const end = selectionEnd().top();
2475 for (; dit.top() < end; dit.top().forwardPos())
2476 if (Inset const * inset = dit.nextInset())
2477 if (inset->confirmDeletion())
2484 void Cursor::moveToClosestEdge(int const x, bool const edit)
2486 if (Inset const * inset = nextInset()) {
2487 // stay in front of insets for which we want to open the dialog
2488 // (e.g. InsetMathSpace).
2489 if (edit && (inset->hasSettings() || !inset->contextMenuName().empty()))
2491 CoordCache::Insets const & insetCache = bv().coordCache().getInsets();
2492 if (!insetCache.has(inset))
2494 int const wid = insetCache.dim(inset).wid;
2495 Point p = insetCache.xy(inset);
2496 if (x > p.x_ + (wid + 1) / 2)