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 "BufferView.h"
19 #include "CoordCache.h"
21 #include "CutAndPaste.h"
22 #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/InsetMathScript.h"
51 #include "mathed/MacroTable.h"
52 #include "mathed/MathData.h"
53 #include "mathed/MathMacro.h"
66 // Find position closest to (x, y) in cell given by iter.
67 // Used only in mathed
68 DocIterator bruteFind(Cursor const & c, int x, int y)
70 double best_dist = numeric_limits<double>::max();
77 et.pos() = et.lastpos();
78 for (size_t i = 0;; ++i) {
81 Inset const * inset = &it.inset();
82 CoordCache::Insets const & insetCache = c.bv().coordCache().getInsets();
84 // FIXME: in the case where the inset is not in the cache, this
85 // means that no part of it is visible on screen. In this case
86 // we don't do elaborate search and we just return the forwarded
87 // DocIterator at its beginning.
88 if (!insetCache.has(inset)) {
93 Point const o = insetCache.xy(inset);
94 inset->cursorPos(c.bv(), it.top(), c.boundary(), xo, yo);
95 // Convert to absolute
98 double d = (x - xo) * (x - xo) + (y - yo) * (y - yo);
99 // '<=' in order to take the last possible position
100 // this is important for clicking behind \sum in e.g. '\sum_i a'
101 LYXERR(Debug::DEBUG, "i: " << i << " d: " << d
102 << " best: " << best_dist);
103 if (d <= best_dist) {
118 CursorData::CursorData()
119 : DocIterator(), anchor_(),
120 selection_(false), mark_(false), word_selection_(false),
121 current_font(inherit_font),
122 autocorrect_(false), macromode_(false)
126 CursorData::CursorData(Buffer * buffer)
127 : DocIterator(buffer), anchor_(),
128 selection_(false), mark_(false), word_selection_(false),
129 current_font(inherit_font),
130 autocorrect_(false), macromode_(false)
134 CursorData::CursorData(DocIterator const & dit)
135 : DocIterator(dit), anchor_(),
136 selection_(false), mark_(false), word_selection_(false),
137 current_font(inherit_font),
138 autocorrect_(false), macromode_(false)
144 ostream & operator<<(ostream & os, CursorData const & cur)
146 os << "\n cursor: | anchor:\n";
147 for (size_t i = 0, n = cur.depth(); i != n; ++i) {
148 os << " " << cur[i] << " | ";
149 if (i < cur.anchor_.depth())
150 os << cur.anchor_[i];
152 os << "-------------------------------";
155 for (size_t i = cur.depth(), n = cur.anchor_.depth(); i < n; ++i) {
156 os << "------------------------------- | " << cur.anchor_[i] << "\n";
158 os << " selection: " << cur.selection_
159 // << " x_target: " << cur.x_target_
160 << " boundary: " << cur.boundary() << endl;
165 LyXErr & operator<<(LyXErr & os, CursorData const & cur)
172 // be careful: this is called from the bv's constructor, too, so
173 // bv functions are not yet available!
174 Cursor::Cursor(BufferView & bv)
175 : CursorData(&bv.buffer()), bv_(&bv),
176 x_target_(-1), textTargetOffset_(0),
177 beforeDispatchPosX_(0), beforeDispatchPosY_(0)
184 push_back(CursorSlice(buffer()->inset()));
185 anchor_ = doc_iterator_begin(buffer());
187 new_word_ = doc_iterator_begin(buffer());
195 // this (intentionally) does neither touch anchor nor selection status
196 void Cursor::setCursor(DocIterator const & cur)
198 DocIterator::operator=(cur);
202 void Cursor::setCursorToAnchor()
205 DocIterator normal = anchor_;
206 while (depth() < normal.depth())
208 if (depth() < anchor_.depth() && top() <= anchor_[depth() - 1])
215 void Cursor::setCursorData(CursorData const & data)
217 CursorData::operator=(data);
221 bool Cursor::getStatus(FuncRequest const & cmd, FuncStatus & status) const
225 // Try to fix cursor in case it is broken.
228 // Is this a function that acts on inset at point?
229 Inset * inset = cur.nextInset();
230 if (lyxaction.funcHasFlag(cmd.action(), LyXAction::AtPoint)
231 && inset && inset->getStatus(cur, cmd, status))
234 // This is, of course, a mess. Better create a new doc iterator and use
235 // this in Inset::getStatus. This might require an additional
236 // BufferView * arg, though (which should be avoided)
237 //Cursor safe = *this;
239 for ( ; cur.depth(); cur.pop()) {
240 //lyxerr << "\nCursor::getStatus: cmd: " << cmd << endl << *this << endl;
241 // LASSERT: Is it safe to continue here, or should we return?
242 LASSERT(cur.idx() <= cur.lastidx(), /**/);
243 LASSERT(cur.pit() <= cur.lastpit(), /**/);
244 LASSERT(cur.pos() <= cur.lastpos(), /**/);
246 // The inset's getStatus() will return 'true' if it made
247 // a definitive decision on whether it want to handle the
248 // request or not. The result of this decision is put into
249 // the 'status' parameter.
250 if (cur.inset().getStatus(cur, cmd, status)) {
259 void Cursor::saveBeforeDispatchPosXY()
261 getPos(beforeDispatchPosX_, beforeDispatchPosY_);
265 void Cursor::dispatch(FuncRequest const & cmd0)
267 LYXERR(Debug::ACTION, "Cursor::dispatch: cmd: " << cmd0 << '\n' << *this);
272 FuncRequest cmd = cmd0;
275 disp_ = DispatchResult();
279 // Is this a function that acts on inset at point?
280 if (lyxaction.funcHasFlag(cmd.action(), LyXAction::AtPoint)
282 disp_.dispatched(true);
283 disp_.screenUpdate(Update::FitCursor | Update::Force);
284 FuncRequest tmpcmd = cmd;
285 LYXERR(Debug::DEBUG, "Cursor::dispatch: (AtPoint) cmd: "
286 << cmd0 << endl << *this);
287 nextInset()->dispatch(*this, tmpcmd);
288 if (disp_.dispatched()) {
294 // store some values to be used inside of the handlers
295 beforeDispatchCursor_ = *this;
296 for (; depth(); pop(), boundary(false)) {
297 LYXERR(Debug::DEBUG, "Cursor::dispatch: cmd: "
298 << cmd0 << endl << *this);
300 // In any of these cases, the cursor is invalid, and we should
301 // try to save this document rather than crash.
302 LBUFERR(pos() <= lastpos());
303 LBUFERR(idx() <= lastidx());
304 LBUFERR(pit() <= lastpit());
306 // The common case is 'LFUN handled, need update', so make the
307 // LFUN handler's life easier by assuming this as default value.
308 // The handler can reset the update and val flags if necessary.
309 disp_.screenUpdate(Update::FitCursor | Update::Force);
310 disp_.dispatched(true);
311 inset().dispatch(*this, cmd);
312 if (disp_.dispatched())
316 // it completely to get a 'bomb early' behaviour in case this
317 // object will be used again.
318 if (!disp_.dispatched()) {
319 LYXERR(Debug::DEBUG, "RESTORING OLD CURSOR!");
320 // We might have invalidated the cursor when removing an empty
321 // paragraph while the cursor could not be moved out the inset
322 // while we initially thought we could. This might happen when
323 // a multiline inset becomes an inline inset when the second
324 // paragraph is removed.
325 if (safe.pit() > safe.lastpit()) {
326 safe.pit() = safe.lastpit();
327 safe.pos() = safe.lastpos();
330 disp_.screenUpdate(Update::None);
331 disp_.dispatched(false);
333 // restore the previous one because nested Cursor::dispatch calls
334 // are possible which would change it
335 beforeDispatchCursor_ = safe.beforeDispatchCursor_;
339 // NOTE: The code below has been copied to BufferView::dispatch.
340 // If you need to modify this, please update the other one too.
342 // notify insets we just left
344 old.beginUndoGroup();
346 bool badcursor = notifyCursorLeavesOrEnters(old, *this);
349 bv().resetInlineCompletionPos();
356 DispatchResult const & Cursor::result() const
362 BufferView & Cursor::bv() const
371 LBUFERR(depth() >= 1);
376 void Cursor::push(Inset & p)
378 push_back(CursorSlice(p));
379 p.setBuffer(*buffer());
383 void Cursor::pushBackward(Inset & p)
385 LASSERT(!empty(), return);
386 //lyxerr << "Entering inset " << t << " front" << endl;
392 bool Cursor::popBackward()
394 LASSERT(!empty(), return false);
402 bool Cursor::popForward()
404 LASSERT(!empty(), return false);
405 //lyxerr << "Leaving inset from in back" << endl;
406 const pos_type lp = (depth() > 1) ? (*this)[depth() - 2].lastpos() : 0;
410 pos() += lastpos() - lp + 1;
415 int Cursor::currentMode()
417 LASSERT(!empty(), return Inset::UNDECIDED_MODE);
418 for (int i = depth() - 1; i >= 0; --i) {
419 int res = operator[](i).inset().currentMode();
420 bool locked_mode = operator[](i).inset().lockedMode();
421 // Also return UNDECIDED_MODE when the mode is locked,
422 // as in this case it is treated the same as TEXT_MODE
423 if (res != Inset::UNDECIDED_MODE || locked_mode)
426 return Inset::TEXT_MODE;
430 bool Cursor::inCoordCache() const
432 // the root inset is not in cache, but we do not need it.
435 CoordCache::Insets const & icache = bv_->coordCache().getInsets();
436 for (size_t i = 1 ; i < depth() ; ++i)
437 if (!icache.has(&(*this)[i].inset()))
443 void Cursor::getPos(int & x, int & y) const
445 Point p = bv().getPos(*this);
451 Row const & Cursor::textRow() const
453 CursorSlice const & cs = innerTextSlice();
454 ParagraphMetrics const & pm = bv().parMetrics(cs.text(), cs.pit());
455 return pm.getRow(pos(), boundary());
459 void Cursor::resetAnchor()
462 checkNewWordPosition();
466 void Cursor::markNewWordPosition()
468 if (lyxrc.spellcheck_continuously && inTexted() && new_word_.empty()) {
469 FontSpan nw = locateWord(WHOLE_WORD);
470 if (nw.size() == 1) {
471 LYXERR(Debug::DEBUG, "start new word: "
473 << " pos: " << nw.first);
480 void Cursor::clearNewWordPosition()
482 if (!new_word_.empty()) {
483 LYXERR(Debug::DEBUG, "clear new word: "
485 << " pos: " << pos());
491 void Cursor::checkNewWordPosition()
493 if (!lyxrc.spellcheck_continuously || new_word_.empty())
496 clearNewWordPosition();
498 // forget the position of the current new word if
499 // 1) the paragraph changes or
500 // 2) the count of nested insets changes or
501 // 3) the cursor pos is out of paragraph bound
502 if (pit() != new_word_.pit() ||
503 depth() != new_word_.depth() ||
504 new_word_.pos() > new_word_.lastpos()) {
505 clearNewWordPosition();
506 } else if (new_word_.fixIfBroken())
507 // 4) or the remembered position was "broken"
508 clearNewWordPosition();
510 FontSpan nw = locateWord(WHOLE_WORD);
512 FontSpan ow = new_word_.locateWord(WHOLE_WORD);
513 if (nw.intersect(ow).empty())
514 clearNewWordPosition();
516 LYXERR(Debug::DEBUG, "new word: "
518 << " pos: " << nw.first << ".." << nw.last);
520 clearNewWordPosition();
527 bool Cursor::posBackward()
536 bool Cursor::posForward()
538 if (pos() == lastpos())
545 bool Cursor::posVisRight(bool skip_inset)
547 Cursor new_cur = *this; // where we will move to
548 pos_type left_pos; // position visually left of current cursor
549 pos_type right_pos; // position visually right of current cursor
551 getSurroundingPos(left_pos, right_pos);
553 LYXERR(Debug::RTL, left_pos <<"|"<< right_pos << " (pos: "<< pos() <<")");
555 // Are we at an inset?
556 new_cur.pos() = right_pos;
557 new_cur.boundary(false);
559 text()->checkAndActivateInsetVisual(new_cur, right_pos >= pos(), false)) {
560 // we actually move the cursor at the end of this
561 // function, for now we just keep track of the new
562 // position in new_cur...
563 LYXERR(Debug::RTL, "entering inset at: " << new_cur.pos());
566 // Are we already at rightmost pos in row?
567 else if (text()->empty() || right_pos == -1) {
570 if (!new_cur.posVisToNewRow(false)) {
571 LYXERR(Debug::RTL, "not moving!");
575 // we actually move the cursor at the end of this
576 // function, for now just keep track of the new
577 // position in new_cur...
578 LYXERR(Debug::RTL, "right edge, moving: " << int(new_cur.pit()) << ","
579 << int(new_cur.pos()) << "," << (new_cur.boundary() ? 1 : 0));
582 // normal movement to the right
585 // Recall, if the cursor is at position 'x', that
586 // means *before* the character at position 'x'. In
587 // RTL, "before" means "to the right of", in LTR, "to
588 // the left of". So currently our situation is this:
589 // the position to our right is 'right_pos' (i.e.,
590 // we're currently to the left of 'right_pos'). In
591 // order to move to the right, it depends whether or
592 // not the character at 'right_pos' is RTL.
593 bool const new_pos_is_RTL = paragraph().getFontSettings(
594 buffer()->params(), right_pos).isVisibleRightToLeft();
595 // If the character at 'right_pos' *is* LTR, then in
596 // order to move to the right of it, we need to be
597 // *after* 'right_pos', i.e., move to position
599 if (!new_pos_is_RTL) {
600 new_cur.pos() = right_pos + 1;
601 // set the boundary to true in two situations:
603 // 1. if new_pos is now lastpos, and we're in
604 // an RTL paragraph (this means that we're
605 // moving right to the end of an LTR chunk
606 // which is at the end of an RTL paragraph);
607 (new_cur.pos() == lastpos()
608 && paragraph().isRTL(buffer()->params()))
609 // 2. if the position *after* right_pos is RTL
610 // (we want to be *after* right_pos, not
611 // before right_pos + 1!)
612 || paragraph().getFontSettings(buffer()->params(),
613 new_cur.pos()).isVisibleRightToLeft()
615 new_cur.boundary(true);
616 else // set the boundary to false
617 new_cur.boundary(false);
619 // Otherwise (if the character at position 'right_pos'
620 // is RTL), then moving to the right of it is as easy
621 // as setting the new position to 'right_pos'.
623 new_cur.pos() = right_pos;
624 new_cur.boundary(false);
629 bool const moved = new_cur != *this || new_cur.boundary() != boundary();
632 LYXERR(Debug::RTL, "moving to: " << new_cur.pos()
633 << (new_cur.boundary() ? " (boundary)" : ""));
641 bool Cursor::posVisLeft(bool skip_inset)
643 Cursor new_cur = *this; // where we will move to
644 pos_type left_pos; // position visually left of current cursor
645 pos_type right_pos; // position visually right of current cursor
647 getSurroundingPos(left_pos, right_pos);
649 LYXERR(Debug::RTL, left_pos <<"|"<< right_pos << " (pos: "<< pos() <<")");
651 // Are we at an inset?
652 new_cur.pos() = left_pos;
653 new_cur.boundary(false);
655 text()->checkAndActivateInsetVisual(new_cur, left_pos >= pos(), true)) {
656 // we actually move the cursor at the end of this
657 // function, for now we just keep track of the new
658 // position in new_cur...
659 LYXERR(Debug::RTL, "entering inset at: " << new_cur.pos());
662 // Are we already at leftmost pos in row?
663 else if (text()->empty() || left_pos == -1) {
666 if (!new_cur.posVisToNewRow(true)) {
667 LYXERR(Debug::RTL, "not moving!");
671 // we actually move the cursor at the end of this
672 // function, for now just keep track of the new
673 // position in new_cur...
674 LYXERR(Debug::RTL, "left edge, moving: " << int(new_cur.pit()) << ","
675 << int(new_cur.pos()) << "," << (new_cur.boundary() ? 1 : 0));
678 // normal movement to the left
681 // Recall, if the cursor is at position 'x', that
682 // means *before* the character at position 'x'. In
683 // RTL, "before" means "to the right of", in LTR, "to
684 // the left of". So currently our situation is this:
685 // the position to our left is 'left_pos' (i.e., we're
686 // currently to the right of 'left_pos'). In order to
687 // move to the left, it depends whether or not the
688 // character at 'left_pos' is RTL.
689 bool const new_pos_is_RTL = paragraph().getFontSettings(
690 buffer()->params(), left_pos).isVisibleRightToLeft();
691 // If the character at 'left_pos' *is* RTL, then in
692 // order to move to the left of it, we need to be
693 // *after* 'left_pos', i.e., move to position
695 if (new_pos_is_RTL) {
696 new_cur.pos() = left_pos + 1;
697 // set the boundary to true in two situations:
699 // 1. if new_pos is now lastpos and we're in
700 // an LTR paragraph (this means that we're
701 // moving left to the end of an RTL chunk
702 // which is at the end of an LTR paragraph);
703 (new_cur.pos() == lastpos()
704 && !paragraph().isRTL(buffer()->params()))
705 // 2. if the position *after* left_pos is not
706 // RTL (we want to be *after* left_pos, not
707 // before left_pos + 1!)
708 || !paragraph().getFontSettings(buffer()->params(),
709 new_cur.pos()).isVisibleRightToLeft()
711 new_cur.boundary(true);
712 else // set the boundary to false
713 new_cur.boundary(false);
715 // Otherwise (if the character at position 'left_pos'
716 // is LTR), then moving to the left of it is as easy
717 // as setting the new position to 'left_pos'.
719 new_cur.pos() = left_pos;
720 new_cur.boundary(false);
725 bool const moved = new_cur != *this || new_cur.boundary() != boundary();
728 LYXERR(Debug::RTL, "moving to: " << new_cur.pos()
729 << (new_cur.boundary() ? " (boundary)" : ""));
739 // Return true on success
740 bool findNonVirtual(Row const & row, Row::const_iterator & cit, bool onleft)
743 while (cit != row.begin() && cit->isVirtual())
746 while (cit != row.end() && cit->isVirtual())
749 return cit != row.end() && !cit->isVirtual();
754 void Cursor::getSurroundingPos(pos_type & left_pos, pos_type & right_pos) const
756 // by default, we know nothing.
760 Row const & row = textRow();
761 TextMetrics const & tm = bv_->textMetrics(text());
763 Row::const_iterator cit = tm.findRowElement(row, pos(), boundary(), dummy);
764 // Handle the case of empty row
765 if (cit == row.end()) {
766 if (paragraph().isRTL(buffer()->params()))
767 right_pos = row.pos();
769 left_pos = row.pos() - 1;
773 // skip virtual elements and exit if no non-virtual one exists
774 if (!findNonVirtual(row, cit, !cit->isRTL()))
777 // if the position is at the left side of the element, we have to
778 // look at the previous element
779 if (pos() == cit->left_pos()) {
780 LYXERR(Debug::RTL, "getSurroundingPos(" << pos() << (boundary() ? "b" : "")
781 << "), AT LEFT of *cit=" << *cit);
782 // this one is easy (see common case below)
783 right_pos = pos() - (cit->isRTL() ? 1 : 0);
784 // at the left of the row
785 if (cit == row.begin())
788 if (!findNonVirtual(row, cit, true))
790 // [...[ is the row element, | is cursor position (! with boundary)
791 // [ 1 2 [ is a ltr row element with pos=1 and endpos=3
792 // ] 2 1] is an rtl row element with pos=1 and endpos=3
793 // [ 1 2 [ [|3 4 [ => (2, 3)
794 // or [ 1 2 [ ]!4 3 ] => (2, 4)
795 // or ] 2 1 ] [|3 4 [ => (1, 3)
796 // or ] 4 3 ] ]!2 1 ] => (3, 2)
797 left_pos = cit->right_pos() - (cit->isRTL() ? 0 : 1);
798 // happens with consecutive row of same direction
799 if (left_pos == right_pos) {
800 left_pos += cit->isRTL() ? 1 : -1;
803 // same code but with the element at the right
804 else if (pos() == cit->right_pos()) {
805 LYXERR(Debug::RTL, "getSurroundingPos(" << pos() << (boundary() ? "b" : "")
806 << "), AT RIGHT of *cit=" << *cit);
807 // this one is easy (see common case below)
808 left_pos = pos() - (cit->isRTL() ? 0 : 1);
809 // at the right of the row
810 if (cit + 1 == row.end())
813 if (!findNonVirtual(row, cit, false))
815 // [ 1 2![ [ 3 4 [ => (2, 3)
816 // or [ 1 2![ ] 4 3 ] => (2, 4)
817 // or ] 2 1|] [ 3 4 [ => (1, 3)
818 // or ] 4 3|] ] 2 1 ] => (3, 2)
819 right_pos = cit->left_pos() - (cit->isRTL() ? 1 : 0);
820 // happens with consecutive row of same direction
821 if (right_pos == left_pos)
822 right_pos += cit->isRTL() ? -1 : 1;
824 // common case: both positions are inside the row element
826 // [ 1 2|3 [ => (2, 3)
827 // or ] 3|2 1 ] => (3, 2)
828 left_pos = pos() - (cit->isRTL() ? 0 : 1);
829 right_pos = pos() - (cit->isRTL() ? 1 : 0);
832 // Note that debug message does not catch all early returns above
833 LYXERR(Debug::RTL,"getSurroundingPos(" << pos() << (boundary() ? "b" : "")
834 << ") => (" << left_pos << ", " << right_pos <<")");
838 bool Cursor::posVisToNewRow(bool movingLeft)
840 Paragraph const & par = paragraph();
841 Buffer const & buf = *buffer();
842 Row const & row = textRow();
843 bool par_is_LTR = !par.isRTL(buf.params());
845 // Inside a table, determining whether to move to the next or
846 // previous row should be done based on the table's direction.
847 if (inset().asInsetTabular()) {
848 par_is_LTR = !inset().asInsetTabular()->isRightToLeft(*this);
849 LYXERR(Debug::RTL, "Inside table! par_is_LTR=" << (par_is_LTR ? 1 : 0));
852 // if moving left in an LTR paragraph or moving right in an
853 // RTL one, move to previous row
854 if (par_is_LTR == movingLeft) {
855 if (row.pos() == 0) { // we're at first row in paragraph
856 if (pit() == 0) // no previous paragraph! don't move
858 // move to last pos in previous par
862 } else { // move to previous row in this par
863 pos() = row.pos() - 1; // this is guaranteed to be in previous row
867 // if moving left in an RTL paragraph or moving right in an
868 // LTR one, move to next row
870 if (row.endpos() == lastpos()) { // we're at last row in paragraph
871 if (pit() == lastpit()) // last paragraph! don't move
873 // move to first row in next par
877 } else { // move to next row in this par
878 pos() = row.endpos();
883 // make sure we're at left-/right-most pos in new row
884 posVisToRowExtremity(!movingLeft);
890 void Cursor::posVisToRowExtremity(bool left)
892 LYXERR(Debug::RTL, "entering extremity: " << pit() << "," << pos() << ","
893 << (boundary() ? 1 : 0));
895 TextMetrics const & tm = bv_->textMetrics(text());
896 // Looking for extremities is like clicking on the left or the
898 int x = tm.origin().x_ + (left ? 0 : textRow().width());
900 pos() = tm.getPosNearX(textRow(), x, b);
903 LYXERR(Debug::RTL, "leaving extremity: " << pit() << "," << pos() << ","
904 << (boundary() ? 1 : 0));
908 bool Cursor::reverseDirectionNeeded() const
911 * We determine the directions based on the direction of the
912 * bottom() --- i.e., outermost --- paragraph, because that is
913 * the only way to achieve consistency of the arrow's movements
914 * within a paragraph, and thus avoid situations in which the
917 return bottom().paragraph().isRTL(bv().buffer().params());
921 CursorSlice Cursor::normalAnchor() const
925 // LASSERT: There have been several bugs around this code, that seem
926 // to involve failures to reset the anchor. We can at least not crash
927 // in release mode by resetting it ourselves.
928 if (anchor_.depth() < depth()) {
929 LYXERR0("Cursor is deeper than anchor. PLEASE REPORT.\nCursor is"
931 const_cast<DocIterator &>(anchor_) = *this;
934 CursorSlice normal = anchor_[depth() - 1];
935 if (depth() < anchor_.depth() && top() <= normal) {
936 // anchor is behind cursor -> move anchor behind the inset
943 CursorSlice Cursor::selBegin() const
947 return normalAnchor() < top() ? normalAnchor() : top();
951 CursorSlice Cursor::selEnd() const
955 return normalAnchor() > top() ? normalAnchor() : top();
959 DocIterator Cursor::selectionBegin() const
965 // FIXME: This is a work-around for the problem that
966 // CursorSlice doesn't keep track of the boundary.
967 if (normalAnchor() == top())
968 di = anchor_.boundary() > boundary() ? anchor_ : *this;
970 di = normalAnchor() < top() ? anchor_ : *this;
976 DocIterator Cursor::selectionEnd() const
982 // FIXME: This is a work-around for the problem that
983 // CursorSlice doesn't keep track of the boundary.
984 if (normalAnchor() == top())
985 di = anchor_.boundary() < boundary() ? anchor_ : *this;
987 di = normalAnchor() > top() ? anchor_ : *this;
989 if (di.depth() > depth()) {
997 void Cursor::setSelection()
1000 if (idx() == normalAnchor().idx() &&
1001 pit() == normalAnchor().pit() &&
1002 pos() == normalAnchor().pos())
1007 void Cursor::setSelection(DocIterator const & where, int n)
1016 void Cursor::clearSelection()
1019 setWordSelection(false);
1025 void Cursor::setTargetX(int x)
1028 textTargetOffset_ = 0;
1032 int Cursor::x_target() const
1038 void Cursor::clearTargetX()
1041 textTargetOffset_ = 0;
1045 void Cursor::updateTextTargetOffset()
1050 textTargetOffset_ = x - x_target_;
1054 void Cursor::info(odocstream & os) const
1056 for (int i = 1, n = depth(); i < n; ++i) {
1057 operator[](i).inset().infoize(os);
1061 Inset const * inset = prevInset();
1062 // prevInset() can return 0 in certain case.
1064 prevInset()->infoize2(os);
1069 bool Cursor::selHandle(bool sel)
1071 //lyxerr << "Cursor::selHandle" << endl;
1074 if (sel == selection())
1078 cap::saveSelection(*this);
1087 ///////////////////////////////////////////////////////////////////
1090 // The part below is the non-integrated rest of the original math
1091 // cursor. This should be either generalized for texted or moved
1092 // back to mathed (in most cases to InsetMathNest).
1094 ///////////////////////////////////////////////////////////////////
1096 #include "mathed/InsetMathChar.h"
1097 #include "mathed/InsetMathGrid.h"
1098 #include "mathed/InsetMathScript.h"
1099 #include "mathed/InsetMathUnknown.h"
1100 #include "mathed/MathFactory.h"
1101 #include "mathed/MathStream.h"
1102 #include "mathed/MathSupport.h"
1107 bool Cursor::isInside(Inset const * p) const
1109 for (size_t i = 0; i != depth(); ++i)
1110 if (&operator[](i).inset() == p)
1116 void Cursor::leaveInset(Inset const & inset)
1118 for (size_t i = 0; i != depth(); ++i) {
1119 if (&operator[](i).inset() == &inset) {
1127 bool Cursor::openable(MathAtom const & t) const
1138 // we can't move into anything new during selection
1139 if (depth() >= anchor_.depth())
1141 if (t.nucleus() != &anchor_[depth()].inset())
1148 void Cursor::setScreenPos(int x, int /*y*/)
1151 //bruteFind(*this, x, y, 0, bv().workWidth(), 0, bv().workHeight());
1156 void Cursor::plainErase()
1158 cell().erase(pos());
1162 void Cursor::markInsert()
1164 insert(char_type(0));
1168 void Cursor::markErase()
1170 cell().erase(pos());
1174 void Cursor::plainInsert(MathAtom const & t)
1176 cell().insert(pos(), t);
1178 inset().setBuffer(bv_->buffer());
1180 checkBufferStructure();
1184 void Cursor::insert(docstring const & str)
1186 for (char_type c : str)
1191 void Cursor::insert(char_type c)
1193 //lyxerr << "Cursor::insert char '" << c << "'" << endl;
1194 LASSERT(!empty(), return);
1196 cap::selClearOrDel(*this);
1197 insert(new InsetMathChar(c));
1199 text()->insertChar(*this, c);
1204 void Cursor::insert(MathAtom const & t)
1206 //lyxerr << "Cursor::insert MathAtom '" << t << "'" << endl;
1208 cap::selClearOrDel(*this);
1213 void Cursor::insert(Inset * inset0)
1215 LASSERT(inset0, return);
1217 insert(MathAtom(inset0->asInsetMath()));
1219 text()->insertInset(*this, inset0);
1220 inset0->setBuffer(bv_->buffer());
1222 if (inset0->isLabeled())
1223 forceBufferUpdate();
1228 int Cursor::niceInsert(docstring const & t, Parse::flags f, bool enter)
1230 MathData ar(buffer());
1232 if (ar.size() == 1 && (enter || selection()))
1240 void Cursor::niceInsert(MathAtom const & t)
1243 docstring const safe = cap::grabAndEraseSelection(*this);
1245 // If possible, enter the new inset and move the contents of the selection
1246 if (t->isActive()) {
1248 // be careful here: don't use 'pushBackward(t)' as this we need to
1249 // push the clone, not the original
1250 pushBackward(*nextInset());
1251 // We may not use niceInsert here (recursion)
1252 MathData ar(buffer());
1255 } else if (t->asMacro() && !safe.empty()) {
1256 MathData ar(buffer());
1258 docstring const name = t->asMacro()->name();
1259 MacroData const * data = buffer()->getMacro(name);
1260 if (data && data->numargs() - data->optionals() > 0) {
1261 plainInsert(MathAtom(new InsetMathBrace(ar)));
1268 void Cursor::insert(MathData const & ar)
1272 cap::eraseSelection(*this);
1273 cell().insert(pos(), ar);
1275 // FIXME audit setBuffer calls
1276 inset().setBuffer(bv_->buffer());
1280 bool Cursor::backspace()
1283 cap::eraseSelection(*this);
1288 // If empty cell, and not part of a big cell
1289 if (lastpos() == 0 && inset().nargs() == 1) {
1291 // Directly delete empty cell: [|[]] => [|]
1297 // [|], can not delete from inside
1308 if (inMacroMode()) {
1309 InsetMathUnknown * p = activeMacro();
1310 if (p->name().size() > 1) {
1311 p->setName(p->name().substr(0, p->name().size() - 1));
1316 if (pos() != 0 && prevAtom()->nargs() > 0) {
1317 // let's require two backspaces for 'big stuff' and
1318 // highlight on the first
1330 bool Cursor::erase()
1336 cap::eraseSelection(*this);
1340 // delete empty cells if possible
1341 if (pos() == lastpos() && inset().idxDelete(idx()))
1344 // special behaviour when in last position of cell
1345 if (pos() == lastpos()) {
1346 bool one_cell = inset().nargs() == 1;
1347 if (one_cell && lastpos() == 0) {
1349 // Directly delete empty cell: [|[]] => [|]
1355 // [|], can not delete from inside
1360 inset().idxGlue(idx());
1364 // 'clever' UI hack: only erase large items if previously slected
1365 if (pos() != lastpos() && nextAtom()->nargs() > 0) {
1380 DocIterator save = *this;
1381 FuncRequest cmd(selection() ? LFUN_UP_SELECT : LFUN_UP, docstring());
1382 this->dispatch(cmd);
1383 if (disp_.dispatched())
1393 DocIterator save = *this;
1394 FuncRequest cmd(selection() ? LFUN_DOWN_SELECT : LFUN_DOWN, docstring());
1395 this->dispatch(cmd);
1396 if (disp_.dispatched())
1403 bool Cursor::macroModeClose()
1407 InsetMathUnknown * p = activeMacro();
1409 MathData selection(buffer());
1410 asArray(p->selection(), selection);
1411 docstring const s = p->name();
1413 cell().erase(pos());
1415 // do nothing if the macro name is empty
1419 // trigger updates of macros, at least, if no full
1420 // updates take place anyway
1421 screenUpdateFlags(Update::Force);
1423 docstring const name = s.substr(1);
1424 InsetMathNest * const in = inset().asInsetMath()->asNestInset();
1425 if (in && in->interpretString(*this, s))
1427 MathAtom atom = buffer()->getMacro(name, *this, false) ?
1428 MathAtom(new MathMacro(buffer(), name)) : createInsetMath(name, buffer());
1430 // try to put argument into macro, if we just inserted a macro
1431 bool macroArg = false;
1432 MathMacro * atomAsMacro = atom.nucleus()->asMacro();
1434 // macros here are still unfolded (in init mode in fact). So
1435 // we have to resolve the macro here manually and check its arity
1436 // to put the selection behind it if arity > 0.
1437 MacroData const * data = buffer()->getMacro(atomAsMacro->name());
1438 if (!selection.empty() && data && data->numargs() - data->optionals() > 0) {
1440 atomAsMacro->setDisplayMode(MathMacro::DISPLAY_INTERACTIVE_INIT, 1);
1442 // non-greedy case. Do not touch the arguments behind
1443 atomAsMacro->setDisplayMode(MathMacro::DISPLAY_INTERACTIVE_INIT, 0);
1446 // insert remembered selection into first argument of a non-macro
1447 else if (atom.nucleus()->nargs() > 0)
1448 atom.nucleus()->cell(0).append(selection);
1452 // finally put the macro argument behind, if needed
1454 if (selection.size() > 1 || selection[0]->asScriptInset())
1455 plainInsert(MathAtom(new InsetMathBrace(selection)));
1464 docstring Cursor::macroName()
1466 return inMacroMode() ? activeMacro()->name() : docstring();
1470 void Cursor::handleNest(MathAtom const & a, int c)
1472 //lyxerr << "Cursor::handleNest: " << c << endl;
1474 asArray(cap::grabAndEraseSelection(*this), t.nucleus()->cell(c));
1477 pushBackward(*nextInset());
1481 int Cursor::targetX() const
1483 if (x_target() != -1)
1492 int Cursor::textTargetOffset() const
1494 return textTargetOffset_;
1498 void Cursor::setTargetX()
1507 bool Cursor::inMacroMode() const
1511 if (pos() == 0 || cell().empty())
1513 InsetMathUnknown const * p = prevAtom()->asUnknownInset();
1514 return p && !p->final();
1518 InsetMathUnknown * Cursor::activeMacro()
1520 return inMacroMode() ? prevAtom().nucleus()->asUnknownInset() : 0;
1524 InsetMathUnknown const * Cursor::activeMacro() const
1526 return inMacroMode() ? prevAtom().nucleus()->asUnknownInset() : 0;
1530 void Cursor::pullArg()
1533 MathData ar = cell();
1534 if (popBackward() && inMathed()) {
1536 cell().insert(pos(), ar);
1539 //formula()->mutateToText();
1544 void Cursor::touch()
1548 DocIterator::const_iterator it = begin();
1549 DocIterator::const_iterator et = end();
1550 for ( ; it != et; ++it)
1556 void Cursor::normalize()
1558 if (idx() > lastidx()) {
1559 lyxerr << "this should not really happen - 1: "
1560 << idx() << ' ' << nargs()
1561 << " in: " << &inset() << endl;
1565 if (pos() > lastpos()) {
1566 lyxerr << "this should not really happen - 2: "
1567 << pos() << ' ' << lastpos() << " in idx: " << idx()
1569 odocstringstream os;
1570 otexrowstream ots(os, false);
1571 WriteStream wi(ots, false, true, WriteStream::wsDefault);
1572 inset().asInsetMath()->write(wi);
1573 lyxerr << to_utf8(os.str()) << endl;
1579 bool Cursor::upDownInMath(bool up)
1581 // Be warned: The 'logic' implemented in this function is highly
1582 // fragile. A distance of one pixel or a '<' vs '<=' _really
1583 // matters. So fiddle around with it only if you think you know
1584 // what you are doing!
1588 xo = beforeDispatchPosX_;
1590 // check if we had something else in mind, if not, this is the future
1592 if (x_target_ == -1)
1594 else if (inset().asInsetText() && xo - textTargetOffset() != x_target()) {
1595 // In text mode inside the line (not left or right) possibly set a new target_x,
1596 // but only if we are somewhere else than the previous target-offset.
1598 // We want to keep the x-target on subsequent up/down movements
1599 // that cross beyond the end of short lines. Thus a special
1600 // handling when the cursor is at the end of line: Use the new
1601 // x-target only if the old one was before the end of line
1602 // or the old one was after the beginning of the line
1603 bool inRTL = innerParagraph().isRTL(bv().buffer().params());
1607 left = pos() == textRow().endpos();
1608 right = pos() == textRow().pos();
1610 left = pos() == textRow().pos();
1611 right = pos() == textRow().endpos();
1613 if ((!left && !right) ||
1614 (left && !right && xo < x_target_) ||
1615 (!left && right && x_target_ < xo))
1622 // try neigbouring script insets
1624 if (inMathed() && !selection()) {
1627 InsetMathScript const * p = prevAtom()->asScriptInset();
1628 if (p && p->has(up)) {
1630 push(*const_cast<InsetMathScript*>(p));
1631 idx() = p->idxOfScript(up);
1634 // we went in the right direction? Otherwise don't jump into the script
1638 int oy = beforeDispatchPosY_;
1639 if ((!up && y <= oy) ||
1648 if (pos() != lastpos()) {
1649 InsetMathScript const * p = nextAtom()->asScriptInset();
1650 if (p && p->has(up)) {
1651 push(*const_cast<InsetMathScript*>(p));
1652 idx() = p->idxOfScript(up);
1655 // we went in the right direction? Otherwise don't jump into the script
1659 int oy = beforeDispatchPosY_;
1660 if ((!up && y <= oy) ||
1669 // try to find an inset that knows better then we,
1670 if (inset().idxUpDown(*this, up)) {
1671 //lyxerr << "idxUpDown triggered" << endl;
1672 // try to find best position within this inset
1674 setCursor(bruteFind(*this, xo, yo));
1678 // any improvement going just out of inset?
1679 if (popBackward() && inMathed()) {
1680 //lyxerr << "updown: popBackward succeeded" << endl;
1683 int yold = beforeDispatchPosY_;
1685 if (up ? ynew < yold : ynew > yold)
1689 // no success, we are probably at the document top or bottom
1695 bool Cursor::atFirstOrLastRow(bool up)
1697 TextMetrics const & tm = bv_->textMetrics(text());
1698 ParagraphMetrics const & pm = tm.parMetrics(pit());
1701 if (pos() && boundary())
1702 row = pm.pos2row(pos() - 1);
1704 row = pm.pos2row(pos());
1707 if (pit() == 0 && row == 0)
1710 if (pit() + 1 >= int(text()->paragraphs().size()) &&
1711 row + 1 >= int(pm.rows().size()))
1718 bool Cursor::upDownInText(bool up, bool & updateNeeded)
1720 LASSERT(text(), return false);
1726 xo = beforeDispatchPosX_;
1728 // update the targetX - this is here before the "return false"
1729 // to set a new target which can be used by InsetTexts above
1730 // if we cannot move up/down inside this inset anymore
1731 if (x_target_ == -1)
1733 else if (xo - textTargetOffset() != x_target() &&
1734 depth() == beforeDispatchCursor_.depth()) {
1735 // In text mode inside the line (not left or right)
1736 // possibly set a new target_x, but only if we are
1737 // somewhere else than the previous target-offset.
1739 // We want to keep the x-target on subsequent up/down
1740 // movements that cross beyond the end of short lines.
1741 // Thus a special handling when the cursor is at the
1742 // end of line: Use the new x-target only if the old
1743 // one was before the end of line or the old one was
1744 // after the beginning of the line
1745 bool inRTL = innerParagraph().isRTL(bv().buffer().params());
1749 left = pos() == textRow().endpos();
1750 right = pos() == textRow().pos();
1752 left = pos() == textRow().pos();
1753 right = pos() == textRow().endpos();
1755 if ((!left && !right) ||
1756 (left && !right && xo < x_target_) ||
1757 (!left && right && x_target_ < xo))
1764 // first get the current line
1765 TextMetrics & tm = bv_->textMetrics(text());
1766 ParagraphMetrics const & pm = tm.parMetrics(pit());
1768 if (pos() && boundary())
1769 row = pm.pos2row(pos() - 1);
1771 row = pm.pos2row(pos());
1773 if (atFirstOrLastRow(up)) {
1774 // Is there a place for the cursor to go ? If yes, we
1775 // can execute the DEPM, otherwise we should keep the
1776 // paragraph to host the cursor.
1777 Cursor dummy = *this;
1778 bool valid_destination = false;
1779 for(; dummy.depth(); dummy.pop())
1780 if (!dummy.atFirstOrLastRow(up)) {
1781 valid_destination = true;
1785 // will a next dispatch follow and if there is a new
1786 // dispatch will it move the cursor out ?
1787 if (depth() > 1 && valid_destination) {
1788 // The cursor hasn't changed yet. This happens when
1789 // you e.g. move out of an inset. And to give the
1790 // DEPM the possibility of doing something we must
1791 // provide it with two different cursors. (Lgb, vfr)
1793 dummy.pos() = dummy.pos() == 0 ? dummy.lastpos() : 0;
1794 dummy.pit() = dummy.pit() == 0 ? dummy.lastpit() : 0;
1796 updateNeeded |= bv().checkDepm(dummy, *this);
1797 updateTextTargetOffset();
1799 forceBufferUpdate();
1804 // with and without selection are handled differently
1806 int yo = bv().getPos(*this).y_;
1808 // To next/previous row
1810 tm.editXY(*this, xo, yo - textRow().ascent() - 1);
1812 tm.editXY(*this, xo, yo + textRow().descent() + 1);
1815 // This happens when you move out of an inset.
1816 // And to give the DEPM the possibility of doing
1817 // something we must provide it with two different
1819 Cursor dummy = *this;
1822 if (bv().checkDepm(dummy, old)) {
1823 updateNeeded = true;
1824 // Make sure that cur gets back whatever happened to dummy (Lgb)
1827 if (inTexted() && pos() && paragraph().isEnvSeparator(pos() - 1))
1830 // if there is a selection, we stay out of any inset,
1831 // and just jump to the right position:
1837 } else if (pit() > 0) {
1839 TextMetrics & tm = bv_->textMetrics(text());
1840 if (!tm.contains(pit()))
1841 tm.newParMetricsUp();
1842 ParagraphMetrics const & pmcur = tm.parMetrics(pit());
1843 next_row = pmcur.rows().size() - 1;
1846 if (row + 1 < int(pm.rows().size())) {
1848 } else if (pit() + 1 < int(text()->paragraphs().size())) {
1850 TextMetrics & tm = bv_->textMetrics(text());
1851 if (!tm.contains(pit()))
1852 tm.newParMetricsDown();
1857 Row const & real_next_row = tm.parMetrics(pit()).rows()[next_row];
1859 top().pos() = tm.getPosNearX(real_next_row, xo, bound);
1862 updateNeeded |= bv().checkDepm(*this, old);
1866 forceBufferUpdate();
1867 updateTextTargetOffset();
1872 void Cursor::handleFont(string const & font)
1874 LYXERR(Debug::DEBUG, font);
1878 safe = cap::grabAndEraseSelection(*this);
1883 if (lastpos() != 0) {
1884 // something left in the cell
1886 // cursor in first position
1888 } else if (pos() == lastpos()) {
1889 // cursor in last position
1892 // cursor in between. split cell
1893 MathData::iterator bt = cell().begin();
1894 MathAtom at = createInsetMath(from_utf8(font), buffer());
1895 at.nucleus()->cell(0) = MathData(buffer(), bt, bt + pos());
1896 cell().erase(bt, bt + pos());
1901 // nothing left in the cell
1910 void Cursor::message(docstring const & msg) const
1912 disp_.setMessage(msg);
1916 void Cursor::errorMessage(docstring const & msg) const
1918 disp_.setMessage(msg);
1919 disp_.setError(true);
1925 docstring parbreak(Cursor const * cur)
1927 odocstringstream os;
1929 // only add blank line if we're not in a ParbreakIsNewline situation
1930 if (!cur->inset().getLayout().parbreakIsNewline()
1931 && !cur->paragraph().layout().parbreak_is_newline)
1939 docstring Cursor::selectionAsString(bool with_label) const
1945 return cap::grabSelection(*this);
1947 int const label = with_label
1948 ? AS_STR_LABEL | AS_STR_INSETS : AS_STR_INSETS;
1950 idx_type const startidx = selBegin().idx();
1951 idx_type const endidx = selEnd().idx();
1952 if (startidx != endidx) {
1953 // multicell selection
1954 InsetTabular * table = inset().asInsetTabular();
1955 LASSERT(table, return docstring());
1956 return table->asString(startidx, endidx);
1959 ParagraphList const & pars = text()->paragraphs();
1961 pit_type const startpit = selBegin().pit();
1962 pit_type const endpit = selEnd().pit();
1963 size_t const startpos = selBegin().pos();
1964 size_t const endpos = selEnd().pos();
1966 if (startpit == endpit)
1967 return pars[startpit].asString(startpos, endpos, label);
1969 // First paragraph in selection
1970 docstring result = pars[startpit].
1971 asString(startpos, pars[startpit].size(), label)
1974 // The paragraphs in between (if any)
1975 for (pit_type pit = startpit + 1; pit != endpit; ++pit) {
1976 Paragraph const & par = pars[pit];
1977 result += par.asString(0, par.size(), label)
1981 // Last paragraph in selection
1982 result += pars[endpit].asString(0, endpos, label);
1988 docstring Cursor::currentState() const
1991 odocstringstream os;
1993 #ifdef DEVEL_VERSION
1994 InsetMath * math = inset().asInsetMath();
1996 os << _(", Inset: ") << math->id();
1997 os << _(", Cell: ") << idx();
1998 os << _(", Position: ") << pos();
2004 return text()->currentState(*this);
2010 docstring Cursor::getPossibleLabel() const
2012 return inMathed() ? from_ascii("eq:") : text()->getPossibleLabel(*this);
2016 Encoding const * Cursor::getEncoding() const
2020 CursorSlice const & sl = innerTextSlice();
2021 Text const & text = *sl.text();
2022 Font font = text.getPar(sl.pit()).getFont(
2023 bv().buffer().params(), sl.pos(), text.outerFont(sl.pit()));
2024 return font.language()->encoding();
2028 void Cursor::undispatched() const
2030 disp_.dispatched(false);
2034 void Cursor::dispatched() const
2036 disp_.dispatched(true);
2040 void Cursor::screenUpdateFlags(Update::flags f) const
2042 disp_.screenUpdate(f);
2046 void Cursor::forceBufferUpdate() const
2048 disp_.forceBufferUpdate();
2052 void Cursor::clearBufferUpdate() const
2054 disp_.clearBufferUpdate();
2058 bool Cursor::needBufferUpdate() const
2060 return disp_.needBufferUpdate();
2064 void Cursor::noScreenUpdate() const
2066 disp_.screenUpdate(Update::None);
2070 Font Cursor::getFont() const
2072 // The logic here should more or less match to the
2073 // Cursor::setCurrentFont logic, i.e. the cursor height should
2074 // give a hint what will happen if a character is entered.
2076 // HACK. far from being perfect...
2078 CursorSlice const & sl = innerTextSlice();
2079 Text const & text = *sl.text();
2080 Paragraph const & par = text.getPar(sl.pit());
2082 // on boundary, so we are really at the character before
2083 pos_type pos = sl.pos();
2084 if (pos > 0 && boundary())
2087 // on space? Take the font before (only for RTL boundary stay)
2089 TextMetrics const & tm = bv().textMetrics(&text);
2090 if (pos == sl.lastpos()
2091 || (par.isSeparator(pos)
2092 && !tm.isRTLBoundary(sl.pit(), pos)))
2096 // get font at the position
2097 Font font = par.getFont(buffer()->params(), pos,
2098 text.outerFont(sl.pit()));
2104 bool Cursor::fixIfBroken()
2106 bool const broken_cursor = DocIterator::fixIfBroken();
2107 bool const broken_anchor = anchor_.fixIfBroken();
2109 if (broken_cursor || broken_anchor) {
2110 clearNewWordPosition();
2118 void Cursor::sanitize()
2120 setBuffer(&bv_->buffer());
2121 DocIterator::sanitize();
2129 bool notifyCursorLeavesOrEnters(Cursor const & old, Cursor & cur)
2131 // find inset in common
2133 for (i = 0; i < old.depth() && i < cur.depth(); ++i) {
2134 if (&old[i].inset() != &cur[i].inset())
2138 // update words if we just moved to another paragraph
2139 if (i == old.depth() && i == cur.depth()
2140 && !cur.buffer()->isClean()
2141 && cur.inTexted() && old.inTexted()
2142 && cur.pit() != old.pit()) {
2143 old.paragraph().updateWords();
2146 // notify everything on top of the common part in old cursor,
2147 // but stop if the inset claims the cursor to be invalid now
2148 for (size_type j = i; j < old.depth(); ++j) {
2149 Cursor inset_pos = old;
2150 inset_pos.cutOff(j);
2151 if (old[j].inset().notifyCursorLeaves(inset_pos, cur))
2155 // notify everything on top of the common part in new cursor,
2156 // but stop if the inset claims the cursor to be invalid now
2157 for (; i < cur.depth(); ++i) {
2158 if (cur[i].inset().notifyCursorEnters(cur))
2166 void Cursor::setCurrentFont()
2168 CursorSlice const & cs = innerTextSlice();
2169 Paragraph const & par = cs.paragraph();
2170 pos_type cpit = cs.pit();
2171 pos_type cpos = cs.pos();
2172 Text const & ctext = *cs.text();
2173 TextMetrics const & tm = bv().textMetrics(&ctext);
2175 // are we behind previous char in fact? -> go to that char
2176 if (cpos > 0 && boundary())
2179 // find position to take the font from
2181 // paragraph end? -> font of last char
2182 if (cpos == lastpos())
2184 // on space? -> look at the words in front of space
2185 else if (cpos > 0 && par.isSeparator(cpos)) {
2186 // abc| def -> font of c
2187 // abc |[WERBEH], i.e. boundary==true -> font of c
2188 // abc [WERBEH]| def, font of the space
2189 if (!tm.isRTLBoundary(cpit, cpos))
2195 BufferParams const & bufparams = buffer()->params();
2196 current_font = par.getFontSettings(bufparams, cpos);
2197 real_current_font = tm.displayFont(cpit, cpos);
2199 // special case for paragraph end
2200 if (cs.pos() == lastpos()
2201 && tm.isRTLBoundary(cpit, cs.pos())
2203 Language const * lang = par.getParLanguage(bufparams);
2204 current_font.setLanguage(lang);
2205 current_font.fontInfo().setNumber(FONT_OFF);
2206 real_current_font.setLanguage(lang);
2207 real_current_font.fontInfo().setNumber(FONT_OFF);
2212 bool Cursor::textUndo()
2214 if (!buffer()->undo().textUndo(*this))
2221 bool Cursor::textRedo()
2223 if (!buffer()->undo().textRedo(*this))
2230 void Cursor::finishUndo() const
2232 buffer()->undo().finishUndo();
2236 void Cursor::beginUndoGroup() const
2238 buffer()->undo().beginUndoGroup(*this);
2242 void Cursor::endUndoGroup() const
2244 buffer()->undo().endUndoGroup(*this);
2248 void Cursor::recordUndo(pit_type from, pit_type to) const
2250 buffer()->undo().recordUndo(*this, from, to);
2254 void Cursor::recordUndo(pit_type from) const
2256 buffer()->undo().recordUndo(*this, from, pit());
2260 void Cursor::recordUndo(UndoKind kind) const
2262 buffer()->undo().recordUndo(*this, kind);
2266 void Cursor::recordUndoInset(Inset const * in) const
2268 buffer()->undo().recordUndoInset(*this, in);
2272 void Cursor::recordUndoFullBuffer() const
2274 buffer()->undo().recordUndoFullBuffer(*this);
2278 void Cursor::recordUndoBufferParams() const
2280 buffer()->undo().recordUndoBufferParams(*this);
2284 void Cursor::recordUndoSelection() const
2287 if (cap::multipleCellsSelected(*this))
2292 buffer()->undo().recordUndo(*this,
2293 selBegin().pit(), selEnd().pit());
2298 void Cursor::checkBufferStructure()
2300 Buffer const * master = buffer()->masterBuffer();
2301 master->tocBackend().updateItem(*this);
2302 if (master != buffer() && !master->hasGuiDelegate())
2303 // In case the master has no gui associated with it,
2304 // the TocItem is not updated (part of bug 5699).
2305 buffer()->tocBackend().updateItem(*this);
2307 // If the last tracked change of the paragraph has just been
2308 // deleted, then we need to recompute the buffer flag
2309 // tracked_changes_present_.
2310 if (inTexted() && paragraph().isChangeUpdateRequired())
2311 disp_.forceChangesUpdate();