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.
19 #include "BufferView.h"
20 #include "CoordCache.h"
22 #include "CutAndPaste.h"
23 #include "DispatchResult.h"
27 #include "FuncRequest.h"
29 #include "LyXFunc.h" // only for setMessage()
31 #include "paragraph_funcs.h"
32 #include "Paragraph.h"
33 #include "ParIterator.h"
36 #include "TextMetrics.h"
37 #include "TocBackend.h"
39 #include "support/lassert.h"
40 #include "support/debug.h"
41 #include "support/docstream.h"
43 #include "insets/InsetTabular.h"
44 #include "insets/InsetText.h"
46 #include "mathed/InsetMath.h"
47 #include "mathed/InsetMathBrace.h"
48 #include "mathed/InsetMathScript.h"
49 #include "mathed/MacroTable.h"
50 #include "mathed/MathData.h"
51 #include "mathed/MathMacro.h"
53 #include <boost/bind.hpp>
65 bool positionable(DocIterator const & cursor, DocIterator const & anchor)
67 // avoid deeper nested insets when selecting
68 if (cursor.depth() > anchor.depth())
71 // anchor might be deeper, should have same path then
72 for (size_t i = 0; i < cursor.depth(); ++i)
73 if (&cursor[i].inset() != &anchor[i].inset())
76 // position should be ok.
81 // Find position closest to (x, y) in cell given by iter.
82 // Used only in mathed
83 DocIterator bruteFind2(Cursor const & c, int x, int y)
85 double best_dist = numeric_limits<double>::max();
92 et.top().pos() = et.top().asInsetMath()->cell(et.top().idx()).size();
93 for (size_t i = 0;; ++i) {
96 Inset const * inset = &it.inset();
97 map<Inset const *, Geometry> const & data =
98 c.bv().coordCache().getInsets().getData();
99 map<Inset const *, Geometry>::const_iterator I = data.find(inset);
101 // FIXME: in the case where the inset is not in the cache, this
102 // means that no part of it is visible on screen. In this case
103 // we don't do elaborate search and we just return the forwarded
104 // DocIterator at its beginning.
105 if (I == data.end()) {
110 Point o = I->second.pos;
111 inset->cursorPos(c.bv(), it.top(), c.boundary(), xo, yo);
112 // Convert to absolute
115 double d = (x - xo) * (x - xo) + (y - yo) * (y - yo);
116 // '<=' in order to take the last possible position
117 // this is important for clicking behind \sum in e.g. '\sum_i a'
118 LYXERR(Debug::DEBUG, "i: " << i << " d: " << d
119 << " best: " << best_dist);
120 if (d <= best_dist) {
133 /// moves position closest to (x, y) in given box
134 bool bruteFind(Cursor & cursor,
135 int x, int y, int xlow, int xhigh, int ylow, int yhigh)
137 LASSERT(!cursor.empty(), return false);
138 Inset & inset = cursor[0].inset();
139 BufferView & bv = cursor.bv();
141 CoordCache::InnerParPosCache const & cache =
142 bv.coordCache().getParPos().find(cursor.bottom().text())->second;
143 // Get an iterator on the first paragraph in the cache
144 DocIterator it(inset);
145 it.push_back(CursorSlice(inset));
146 it.pit() = cache.begin()->first;
147 // Get an iterator after the last paragraph in the cache
148 DocIterator et(inset);
149 et.push_back(CursorSlice(inset));
150 et.pit() = boost::prior(cache.end())->first;
151 if (et.pit() >= et.lastpit())
152 et = doc_iterator_end(inset);
156 double best_dist = numeric_limits<double>::max();;
157 DocIterator best_cursor = et;
159 for ( ; it != et; it.forwardPos(true)) {
160 // avoid invalid nesting when selecting
161 if (!cursor.selection() || positionable(it, cursor.anchor_)) {
162 Point p = bv.getPos(it, false);
165 if (xlow <= xo && xo <= xhigh && ylow <= yo && yo <= yhigh) {
166 double const dx = xo - x;
167 double const dy = yo - y;
168 double const d = dx * dx + dy * dy;
169 // '<=' in order to take the last possible position
170 // this is important for clicking behind \sum in e.g. '\sum_i a'
171 if (d <= best_dist) {
172 // lyxerr << "*" << endl;
180 if (best_cursor != et) {
181 cursor.setCursor(best_cursor);
190 /// moves position closest to (x, y) in given box
191 bool bruteFind3(Cursor & cur, int x, int y, bool up)
193 BufferView & bv = cur.bv();
194 int ylow = up ? 0 : y + 1;
195 int yhigh = up ? y - 1 : bv.workHeight();
197 int xhigh = bv.workWidth();
199 // FIXME: bit more work needed to get 'from' and 'to' right.
200 pit_type from = cur.bottom().pit();
201 //pit_type to = cur.bottom().pit();
202 //lyxerr << "Pit start: " << from << endl;
204 //lyxerr << "bruteFind3: x: " << x << " y: " << y
205 // << " xlow: " << xlow << " xhigh: " << xhigh
206 // << " ylow: " << ylow << " yhigh: " << yhigh
208 DocIterator it = doc_iterator_begin(cur.buffer());
210 DocIterator et = doc_iterator_end(cur.buffer());
212 double best_dist = numeric_limits<double>::max();
213 DocIterator best_cursor = et;
215 for ( ; it != et; it.forwardPos()) {
216 // avoid invalid nesting when selecting
217 if (bv.cursorStatus(it) == CUR_INSIDE
218 && (!cur.selection() || positionable(it, cur.anchor_))) {
219 Point p = bv.getPos(it, false);
222 if (xlow <= xo && xo <= xhigh && ylow <= yo && yo <= yhigh) {
223 double const dx = xo - x;
224 double const dy = yo - y;
225 double const d = dx * dx + dy * dy;
226 //lyxerr << "itx: " << xo << " ity: " << yo << " d: " << d
227 // << " dx: " << dx << " dy: " << dy
228 // << " idx: " << it.idx() << " pos: " << it.pos()
231 // '<=' in order to take the last possible position
232 // this is important for clicking behind \sum in e.g. '\sum_i a'
233 if (d <= best_dist) {
234 //lyxerr << "*" << endl;
242 //lyxerr << "best_dist: " << best_dist << " cur:\n" << best_cursor << endl;
243 if (best_cursor == et)
245 cur.setCursor(best_cursor);
249 docstring parbreak(Paragraph const & par)
253 // only add blank line if we're not in an ERT or Listings inset
254 if (par.ownerCode() != ERT_CODE && par.ownerCode() != LISTINGS_CODE)
262 // be careful: this is called from the bv's constructor, too, so
263 // bv functions are not yet available!
264 Cursor::Cursor(BufferView & bv)
265 : DocIterator(&bv.buffer()), bv_(&bv), anchor_(),
266 x_target_(-1), textTargetOffset_(0),
267 selection_(false), mark_(false), logicalpos_(false),
268 current_font(inherit_font)
272 void Cursor::reset(Inset & inset)
275 push_back(CursorSlice(inset));
276 anchor_ = doc_iterator_begin(&inset.buffer(), &inset);
284 // this (intentionally) does neither touch anchor nor selection status
285 void Cursor::setCursor(DocIterator const & cur)
287 DocIterator::operator=(cur);
291 void Cursor::dispatch(FuncRequest const & cmd0)
293 LYXERR(Debug::DEBUG, "cmd: " << cmd0 << '\n' << *this);
298 FuncRequest cmd = cmd0;
301 // store some values to be used inside of the handlers
302 beforeDispatchCursor_ = *this;
303 for (; depth(); pop(), boundary(false)) {
304 LYXERR(Debug::DEBUG, "Cursor::dispatch: cmd: "
305 << cmd0 << endl << *this);
306 LASSERT(pos() <= lastpos(), /**/);
307 LASSERT(idx() <= lastidx(), /**/);
308 LASSERT(pit() <= lastpit(), /**/);
310 // The common case is 'LFUN handled, need update', so make the
311 // LFUN handler's life easier by assuming this as default value.
312 // The handler can reset the update and val flags if necessary.
313 disp_.update(Update::FitCursor | Update::Force);
314 disp_.dispatched(true);
315 inset().dispatch(*this, cmd);
316 if (disp_.dispatched())
320 // it completely to get a 'bomb early' behaviour in case this
321 // object will be used again.
322 if (!disp_.dispatched()) {
323 LYXERR(Debug::DEBUG, "RESTORING OLD CURSOR!");
325 disp_.update(Update::None);
326 disp_.dispatched(false);
328 // restore the previous one because nested Cursor::dispatch calls
329 // are possible which would change it
330 beforeDispatchCursor_ = safe.beforeDispatchCursor_;
335 DispatchResult Cursor::result() const
341 BufferView & Cursor::bv() const
350 LASSERT(depth() >= 1, /**/);
355 void Cursor::push(Inset & p)
357 push_back(CursorSlice(p));
358 p.setBuffer(*buffer());
362 void Cursor::pushBackward(Inset & p)
364 LASSERT(!empty(), return);
365 //lyxerr << "Entering inset " << t << " front" << endl;
371 bool Cursor::popBackward()
373 LASSERT(!empty(), return false);
381 bool Cursor::popForward()
383 LASSERT(!empty(), return false);
384 //lyxerr << "Leaving inset from in back" << endl;
385 const pos_type lp = (depth() > 1) ? (*this)[depth() - 2].lastpos() : 0;
389 pos() += lastpos() - lp + 1;
394 int Cursor::currentMode()
396 LASSERT(!empty(), /**/);
397 for (int i = depth() - 1; i >= 0; --i) {
398 int res = operator[](i).inset().currentMode();
399 if (res != Inset::UNDECIDED_MODE)
402 return Inset::TEXT_MODE;
406 void Cursor::getPos(int & x, int & y) const
408 Point p = bv().getPos(*this, boundary());
414 Row const & Cursor::textRow() const
416 CursorSlice const & cs = innerTextSlice();
417 ParagraphMetrics const & pm = bv().parMetrics(cs.text(), cs.pit());
418 LASSERT(!pm.rows().empty(), /**/);
419 return pm.getRow(pos(), boundary());
423 void Cursor::resetAnchor()
430 bool Cursor::posBackward()
439 bool Cursor::posForward()
441 if (pos() == lastpos())
448 bool Cursor::posVisRight(bool skip_inset)
450 Cursor new_cur = *this; // where we will move to
451 pos_type left_pos; // position visually left of current cursor
452 pos_type right_pos; // position visually right of current cursor
453 bool new_pos_is_RTL; // is new position we're moving to RTL?
455 getSurroundingPos(left_pos, right_pos);
457 LYXERR(Debug::RTL, left_pos <<"|"<< right_pos << " (pos: "<< pos() <<")");
459 // Are we at an inset?
460 new_cur.pos() = right_pos;
461 new_cur.boundary(false);
463 text()->checkAndActivateInsetVisual(new_cur, right_pos >= pos(), false)) {
464 // we actually move the cursor at the end of this function, for now
465 // we just keep track of the new position in new_cur...
466 LYXERR(Debug::RTL, "entering inset at: " << new_cur.pos());
469 // Are we already at rightmost pos in row?
470 else if (text()->empty() || right_pos == -1) {
473 if (!new_cur.posVisToNewRow(false)) {
474 LYXERR(Debug::RTL, "not moving!");
478 // we actually move the cursor at the end of this function, for now
479 // just keep track of the new position in new_cur...
480 LYXERR(Debug::RTL, "right edge, moving: " << int(new_cur.pit()) << ","
481 << int(new_cur.pos()) << "," << (new_cur.boundary() ? 1 : 0));
484 // normal movement to the right
487 // Recall, if the cursor is at position 'x', that means *before*
488 // the character at position 'x'. In RTL, "before" means "to the
489 // right of", in LTR, "to the left of". So currently our situation
490 // is this: the position to our right is 'right_pos' (i.e., we're
491 // currently to the left of 'right_pos'). In order to move to the
492 // right, it depends whether or not the character at 'right_pos' is RTL.
493 new_pos_is_RTL = paragraph().getFontSettings(
494 buffer()->params(), right_pos).isVisibleRightToLeft();
495 // If the character at 'right_pos' *is* LTR, then in order to move to
496 // the right of it, we need to be *after* 'right_pos', i.e., move to
497 // position 'right_pos' + 1.
498 if (!new_pos_is_RTL) {
499 new_cur.pos() = right_pos + 1;
500 // set the boundary to true in two situations:
502 // 1. if new_pos is now lastpos, and we're in an RTL paragraph
503 // (this means that we're moving right to the end of an LTR chunk
504 // which is at the end of an RTL paragraph);
505 (new_cur.pos() == lastpos()
506 && paragraph().isRTL(buffer()->params()))
507 // 2. if the position *after* right_pos is RTL (we want to be
508 // *after* right_pos, not before right_pos + 1!)
509 || paragraph().getFontSettings(buffer()->params(),
510 new_cur.pos()).isVisibleRightToLeft()
512 new_cur.boundary(true);
513 else // set the boundary to false
514 new_cur.boundary(false);
516 // Otherwise (if the character at position 'right_pos' is RTL), then
517 // moving to the right of it is as easy as setting the new position
520 new_cur.pos() = right_pos;
521 new_cur.boundary(false);
526 bool moved = (new_cur.pos() != pos()
527 || new_cur.pit() != pit()
528 || new_cur.boundary() != boundary()
529 || &new_cur.inset() != &inset());
532 LYXERR(Debug::RTL, "moving to: " << new_cur.pos()
533 << (new_cur.boundary() ? " (boundary)" : ""));
541 bool Cursor::posVisLeft(bool skip_inset)
543 Cursor new_cur = *this; // where we will move to
544 pos_type left_pos; // position visually left of current cursor
545 pos_type right_pos; // position visually right of current cursor
546 bool new_pos_is_RTL; // is new position we're moving to RTL?
548 getSurroundingPos(left_pos, right_pos);
550 LYXERR(Debug::RTL, left_pos <<"|"<< right_pos << " (pos: "<< pos() <<")");
552 // Are we at an inset?
553 new_cur.pos() = left_pos;
554 new_cur.boundary(false);
556 text()->checkAndActivateInsetVisual(new_cur, left_pos >= pos(), true)) {
557 // we actually move the cursor at the end of this function, for now
558 // we just keep track of the new position in new_cur...
559 LYXERR(Debug::RTL, "entering inset at: " << new_cur.pos());
562 // Are we already at leftmost pos in row?
563 else if (text()->empty() || left_pos == -1) {
566 if (!new_cur.posVisToNewRow(true)) {
567 LYXERR(Debug::RTL, "not moving!");
571 // we actually move the cursor at the end of this function, for now
572 // just keep track of the new position in new_cur...
573 LYXERR(Debug::RTL, "left edge, moving: " << int(new_cur.pit()) << ","
574 << int(new_cur.pos()) << "," << (new_cur.boundary() ? 1 : 0));
577 // normal movement to the left
580 // Recall, if the cursor is at position 'x', that means *before*
581 // the character at position 'x'. In RTL, "before" means "to the
582 // right of", in LTR, "to the left of". So currently our situation
583 // is this: the position to our left is 'left_pos' (i.e., we're
584 // currently to the right of 'left_pos'). In order to move to the
585 // left, it depends whether or not the character at 'left_pos' is RTL.
586 new_pos_is_RTL = paragraph().getFontSettings(
587 buffer()->params(), left_pos).isVisibleRightToLeft();
588 // If the character at 'left_pos' *is* RTL, then in order to move to
589 // the left of it, we need to be *after* 'left_pos', i.e., move to
590 // position 'left_pos' + 1.
591 if (new_pos_is_RTL) {
592 new_cur.pos() = left_pos + 1;
593 // set the boundary to true in two situations:
595 // 1. if new_pos is now lastpos and we're in an LTR paragraph
596 // (this means that we're moving left to the end of an RTL chunk
597 // which is at the end of an LTR paragraph);
598 (new_cur.pos() == lastpos()
599 && !paragraph().isRTL(buffer()->params()))
600 // 2. if the position *after* left_pos is not RTL (we want to be
601 // *after* left_pos, not before left_pos + 1!)
602 || !paragraph().getFontSettings(buffer()->params(),
603 new_cur.pos()).isVisibleRightToLeft()
605 new_cur.boundary(true);
606 else // set the boundary to false
607 new_cur.boundary(false);
609 // Otherwise (if the character at position 'left_pos' is LTR), then
610 // moving to the left of it is as easy as setting the new position
613 new_cur.pos() = left_pos;
614 new_cur.boundary(false);
619 bool moved = (new_cur.pos() != pos()
620 || new_cur.pit() != pit()
621 || new_cur.boundary() != boundary());
624 LYXERR(Debug::RTL, "moving to: " << new_cur.pos()
625 << (new_cur.boundary() ? " (boundary)" : ""));
633 void Cursor::getSurroundingPos(pos_type & left_pos, pos_type & right_pos)
635 // preparing bidi tables
636 Paragraph const & par = paragraph();
637 Buffer const & buf = *buffer();
638 Row const & row = textRow();
640 bidi.computeTables(par, buf, row);
642 LYXERR(Debug::RTL, "bidi: " << row.pos() << "--" << row.endpos());
644 // The cursor is painted *before* the character at pos(), or, if 'boundary'
645 // is true, *after* the character at (pos() - 1). So we already have one
646 // known position around the cursor:
647 pos_type known_pos = boundary() ? pos() - 1 : pos();
649 // edge case: if we're at the end of the paragraph, things are a little
650 // different (because lastpos is a position which does not really "exist"
651 // --- there's no character there yet).
652 if (known_pos == lastpos()) {
653 if (par.isRTL(buf.params())) {
655 right_pos = bidi.vis2log(row.pos());
657 else { // LTR paragraph
659 left_pos = bidi.vis2log(row.endpos() - 1);
664 // Whether 'known_pos' is to the left or to the right of the cursor depends
665 // on whether it is an RTL or LTR character...
666 bool const cur_is_RTL =
667 par.getFontSettings(buf.params(), known_pos).isVisibleRightToLeft();
668 // ... in the following manner:
669 // For an RTL character, "before" means "to the right" and "after" means
670 // "to the left"; and for LTR, it's the reverse. So, 'known_pos' is to the
671 // right of the cursor if (RTL && boundary) or (!RTL && !boundary):
672 bool known_pos_on_right = (cur_is_RTL == boundary());
674 // So we now know one of the positions surrounding the cursor. Let's
675 // determine the other one:
677 if (known_pos_on_right) {
678 right_pos = known_pos;
679 // *visual* position of 'left_pos':
680 pos_type v_left_pos = bidi.log2vis(right_pos) - 1;
681 // If the position we just identified as 'left_pos' is a "skipped
682 // separator" (a separator which is at the logical end of a row,
683 // except for the last row in a paragraph; such separators are not
684 // painted, so they "are not really there"; note that in bidi text,
685 // such a separator could appear visually in the middle of a row),
686 // set 'left_pos' to the *next* position to the left.
687 if (bidi.inRange(v_left_pos)
688 && bidi.vis2log(v_left_pos) + 1 == row.endpos()
689 && row.endpos() < lastpos()
690 && par.isSeparator(bidi.vis2log(v_left_pos))) {
693 // calculate the logical position of 'left_pos', if in row
694 if (!bidi.inRange(v_left_pos))
697 left_pos = bidi.vis2log(v_left_pos);
698 // If the position we identified as 'right_pos' is a "skipped
699 // separator", set 'right_pos' to the *next* position to the right.
700 if (right_pos + 1 == row.endpos() && row.endpos() < lastpos()
701 && par.isSeparator(right_pos)) {
702 pos_type v_right_pos = bidi.log2vis(right_pos) + 1;
703 if (!bidi.inRange(v_right_pos))
706 right_pos = bidi.vis2log(v_right_pos);
709 else { // known_pos is on the left
710 left_pos = known_pos;
711 // *visual* position of 'right_pos'
712 pos_type v_right_pos = bidi.log2vis(left_pos) + 1;
713 // If the position we just identified as 'right_pos' is a "skipped
714 // separator", set 'right_pos' to the *next* position to the right.
715 if (bidi.inRange(v_right_pos)
716 && bidi.vis2log(v_right_pos) + 1 == row.endpos()
717 && row.endpos() < lastpos()
718 && par.isSeparator(bidi.vis2log(v_right_pos))) {
721 // calculate the logical position of 'right_pos', if in row
722 if (!bidi.inRange(v_right_pos))
725 right_pos = bidi.vis2log(v_right_pos);
726 // If the position we identified as 'left_pos' is a "skipped
727 // separator", set 'left_pos' to the *next* position to the left.
728 if (left_pos + 1 == row.endpos() && row.endpos() < lastpos()
729 && par.isSeparator(left_pos)) {
730 pos_type v_left_pos = bidi.log2vis(left_pos) - 1;
731 if (!bidi.inRange(v_left_pos))
734 left_pos = bidi.vis2log(v_left_pos);
741 bool Cursor::posVisToNewRow(bool movingLeft)
743 Paragraph const & par = paragraph();
744 Buffer const & buf = *buffer();
745 Row const & row = textRow();
746 bool par_is_LTR = !par.isRTL(buf.params());
748 // Inside a table, determining whether to move to the next or previous row
749 // should be done based on the table's direction.
751 if (s >= 1 && (*this)[s].inset().asInsetTabular()) {
752 par_is_LTR = !(*this)[s].inset().asInsetTabular()->isRightToLeft(*this);
753 LYXERR(Debug::RTL, "Inside table! par_is_LTR=" << (par_is_LTR ? 1 : 0));
756 // if moving left in an LTR paragraph or moving right in an RTL one,
757 // move to previous row
758 if (par_is_LTR == movingLeft) {
759 if (row.pos() == 0) { // we're at first row in paragraph
760 if (pit() == 0) // no previous paragraph! don't move
762 // move to last pos in previous par
766 } else { // move to previous row in this par
767 pos() = row.pos() - 1; // this is guaranteed to be in previous row
771 // if moving left in an RTL paragraph or moving right in an LTR one,
774 if (row.endpos() == lastpos()) { // we're at last row in paragraph
775 if (pit() == lastpit()) // last paragraph! don't move
777 // move to first row in next par
781 } else { // move to next row in this par
782 pos() = row.endpos();
787 // make sure we're at left-/right-most pos in new row
788 posVisToRowExtremity(!movingLeft);
794 void Cursor::posVisToRowExtremity(bool left)
796 // prepare bidi tables
797 Paragraph const & par = paragraph();
798 Buffer const & buf = *buffer();
799 Row const & row = textRow();
801 bidi.computeTables(par, buf, row);
803 LYXERR(Debug::RTL, "entering extremity: " << pit() << "," << pos() << ","
804 << (boundary() ? 1 : 0));
806 if (left) { // move to leftmost position
807 // if this is an RTL paragraph, and we're at the last row in the
808 // paragraph, move to lastpos
809 if (par.isRTL(buf.params()) && row.endpos() == lastpos())
812 pos() = bidi.vis2log(row.pos());
814 // Moving to the leftmost position in the row, the cursor should
815 // normally be placed to the *left* of the leftmost position.
816 // A very common exception, though, is if the leftmost character
817 // also happens to be the separator at the (logical) end of the row
818 // --- in this case, the separator is positioned beyond the left
819 // margin, and we don't want to move the cursor there (moving to
820 // the left of the separator is equivalent to moving to the next
821 // line). So, in this case we actually want to place the cursor
822 // to the *right* of the leftmost position (the separator).
823 // Another exception is if we're moving to the logically last
824 // position in the row, which is *not* a separator: this means
825 // that the entire row has no separators (if there were any, the
826 // row would have been broken there); and therefore in this case
827 // we also move to the *right* of the last position (this indicates
828 // to the user that there is no space after this position, and is
829 // consistent with the behavior in the middle of a row --- moving
830 // right or left moves to the next/previous character; if we were
831 // to move to the *left* of this position, that would simulate
832 // a separator which is not really there!).
833 // Finally, there is an exception to the previous exception: if
834 // this non-separator-but-last-position-in-row is an inset, then
835 // we *do* want to stay to the left of it anyway: this is the
836 // "boundary" which we simulate at insets.
837 // Another exception is when row.endpos() is 0.
839 bool right_of_pos = false; // do we want to be to the right of pos?
841 // as explained above, if at last pos in row, stay to the right
842 if (row.endpos() > 0 && pos() == row.endpos() - 1
843 && !par.isInset(pos()))
846 // Now we know if we want to be to the left or to the right of pos,
847 // let's make sure we are where we want to be.
848 bool new_pos_is_RTL =
849 par.getFontSettings(buf.params(), pos()).isVisibleRightToLeft();
851 if (new_pos_is_RTL == !right_of_pos) {
858 else { // move to rightmost position
859 // if this is an LTR paragraph, and we're at the last row in the
860 // paragraph, move to lastpos
861 if (!par.isRTL(buf.params()) && row.endpos() == lastpos())
864 if (row.endpos() > 0)
865 pos() = bidi.vis2log(row.endpos() - 1);
869 // Moving to the rightmost position in the row, the cursor should
870 // normally be placed to the *right* of the rightmost position.
871 // A very common exception, though, is if the rightmost character
872 // also happens to be the separator at the (logical) end of the row
873 // --- in this case, the separator is positioned beyond the right
874 // margin, and we don't want to move the cursor there (moving to
875 // the right of the separator is equivalent to moving to the next
876 // line). So, in this case we actually want to place the cursor
877 // to the *left* of the rightmost position (the separator).
878 // Another exception is if we're moving to the logically last
879 // position in the row, which is *not* a separator: this means
880 // that the entire row has no separators (if there were any, the
881 // row would have been broken there); and therefore in this case
882 // we also move to the *left* of the last position (this indicates
883 // to the user that there is no space after this position, and is
884 // consistent with the behavior in the middle of a row --- moving
885 // right or left moves to the next/previous character; if we were
886 // to move to the *right* of this position, that would simulate
887 // a separator which is not really there!).
888 // Finally, there is an exception to the previous exception: if
889 // this non-separator-but-last-position-in-row is an inset, then
890 // we *do* want to stay to the right of it anyway: this is the
891 // "boundary" which we simulate at insets.
892 // Another exception is when row.endpos() is 0.
894 bool left_of_pos = false; // do we want to be to the left of pos?
896 // as explained above, if at last pos in row, stay to the left,
897 // unless the last position is the same as the first.
898 if (row.endpos() > 0 && pos() == row.endpos() - 1
899 && !par.isInset(pos()))
902 // Now we know if we want to be to the left or to the right of pos,
903 // let's make sure we are where we want to be.
904 bool new_pos_is_RTL =
905 par.getFontSettings(buf.params(), pos()).isVisibleRightToLeft();
907 if (new_pos_is_RTL == left_of_pos) {
913 LYXERR(Debug::RTL, "leaving extremity: " << pit() << "," << pos() << ","
914 << (boundary() ? 1 : 0));
918 CursorSlice Cursor::anchor() const
920 LASSERT(anchor_.depth() >= depth(), /**/);
921 CursorSlice normal = anchor_[depth() - 1];
922 if (depth() < anchor_.depth() && top() <= normal) {
923 // anchor is behind cursor -> move anchor behind the inset
930 CursorSlice Cursor::selBegin() const
934 return anchor() < top() ? anchor() : top();
938 CursorSlice Cursor::selEnd() const
942 return anchor() > top() ? anchor() : top();
946 DocIterator Cursor::selectionBegin() const
952 // FIXME: This is a work-around for the problem that
953 // CursorSlice doesn't keep track of the boundary.
954 if (anchor() == top())
955 di = anchor_.boundary() > boundary() ? anchor_ : *this;
957 di = anchor() < top() ? anchor_ : *this;
963 DocIterator Cursor::selectionEnd() const
969 // FIXME: This is a work-around for the problem that
970 // CursorSlice doesn't keep track of the boundary.
971 if (anchor() == top())
972 di = anchor_.boundary() < boundary() ? anchor_ : *this;
974 di = anchor() > top() ? anchor_ : *this;
976 if (di.depth() > depth()) {
984 void Cursor::setSelection()
987 // A selection with no contents is not a selection
988 // FIXME: doesnt look ok
989 if (idx() == anchor().idx() &&
990 pit() == anchor().pit() &&
991 pos() == anchor().pos())
996 void Cursor::setSelection(DocIterator const & where, int n)
1005 void Cursor::clearSelection()
1007 setSelection(false);
1013 void Cursor::setTargetX(int x)
1016 textTargetOffset_ = 0;
1020 int Cursor::x_target() const
1026 void Cursor::clearTargetX()
1029 textTargetOffset_ = 0;
1033 void Cursor::updateTextTargetOffset()
1038 textTargetOffset_ = x - x_target_;
1042 void Cursor::info(odocstream & os) const
1044 for (int i = 1, n = depth(); i < n; ++i) {
1045 operator[](i).inset().infoize(os);
1049 Inset const * inset = prevInset();
1050 // prevInset() can return 0 in certain case.
1052 prevInset()->infoize2(os);
1054 // overwite old message
1059 bool Cursor::selHandle(bool sel)
1061 //lyxerr << "Cursor::selHandle" << endl;
1064 if (sel == selection())
1068 cap::saveSelection(*this);
1076 ostream & operator<<(ostream & os, Cursor const & cur)
1078 os << "\n cursor: | anchor:\n";
1079 for (size_t i = 0, n = cur.depth(); i != n; ++i) {
1080 os << " " << cur[i] << " | ";
1081 if (i < cur.anchor_.depth())
1082 os << cur.anchor_[i];
1084 os << "-------------------------------";
1087 for (size_t i = cur.depth(), n = cur.anchor_.depth(); i < n; ++i) {
1088 os << "------------------------------- | " << cur.anchor_[i] << "\n";
1090 os << " selection: " << cur.selection_
1091 << " x_target: " << cur.x_target_ << endl;
1096 LyXErr & operator<<(LyXErr & os, Cursor const & cur)
1106 ///////////////////////////////////////////////////////////////////
1109 // The part below is the non-integrated rest of the original math
1110 // cursor. This should be either generalized for texted or moved
1111 // back to mathed (in most cases to InsetMathNest).
1113 ///////////////////////////////////////////////////////////////////
1115 #include "mathed/InsetMathChar.h"
1116 #include "mathed/InsetMathGrid.h"
1117 #include "mathed/InsetMathScript.h"
1118 #include "mathed/InsetMathUnknown.h"
1119 #include "mathed/MathFactory.h"
1120 #include "mathed/MathStream.h"
1121 #include "mathed/MathSupport.h"
1126 //#define FILEDEBUG 1
1129 bool Cursor::isInside(Inset const * p) const
1131 for (size_t i = 0; i != depth(); ++i)
1132 if (&operator[](i).inset() == p)
1138 void Cursor::leaveInset(Inset const & inset)
1140 for (size_t i = 0; i != depth(); ++i) {
1141 if (&operator[](i).inset() == &inset) {
1149 bool Cursor::openable(MathAtom const & t) const
1160 // we can't move into anything new during selection
1161 if (depth() >= anchor_.depth())
1163 if (t.nucleus() != &anchor_[depth()].inset())
1170 void Cursor::setScreenPos(int x, int /*y*/)
1173 //bruteFind(*this, x, y, 0, bv().workWidth(), 0, bv().workHeight());
1178 void Cursor::plainErase()
1180 cell().erase(pos());
1184 void Cursor::markInsert()
1186 insert(char_type(0));
1190 void Cursor::markErase()
1192 cell().erase(pos());
1196 void Cursor::plainInsert(MathAtom const & t)
1198 cell().insert(pos(), t);
1200 inset().setBuffer(bv_->buffer());
1205 void Cursor::insert(docstring const & str)
1207 for_each(str.begin(), str.end(),
1208 boost::bind(static_cast<void(Cursor::*)(char_type)>
1209 (&Cursor::insert), this, _1));
1213 void Cursor::insert(char_type c)
1215 //lyxerr << "Cursor::insert char '" << c << "'" << endl;
1216 LASSERT(!empty(), /**/);
1218 cap::selClearOrDel(*this);
1219 insert(new InsetMathChar(c));
1221 text()->insertChar(*this, c);
1226 void Cursor::insert(MathAtom const & t)
1228 //lyxerr << "Cursor::insert MathAtom '" << t << "'" << endl;
1230 cap::selClearOrDel(*this);
1235 void Cursor::insert(Inset * inset0)
1237 LASSERT(inset0, /**/);
1239 insert(MathAtom(inset0));
1241 text()->insertInset(*this, inset0);
1242 inset0->setBuffer(bv_->buffer());
1248 void Cursor::niceInsert(docstring const & t, Parse::flags f)
1259 void Cursor::niceInsert(MathAtom const & t)
1262 docstring const safe = cap::grabAndEraseSelection(*this);
1264 // enter the new inset and move the contents of the selection if possible
1265 if (t->isActive()) {
1267 // be careful here: don't use 'pushBackward(t)' as this we need to
1268 // push the clone, not the original
1269 pushBackward(*nextInset());
1270 // We may not use niceInsert here (recursion)
1278 void Cursor::insert(MathData const & ar)
1282 cap::eraseSelection(*this);
1283 cell().insert(pos(), ar);
1288 bool Cursor::backspace()
1290 autocorrect() = false;
1293 cap::eraseSelection(*this);
1298 // If empty cell, and not part of a big cell
1299 if (lastpos() == 0 && inset().nargs() == 1) {
1301 // Directly delete empty cell: [|[]] => [|]
1307 // [|], can not delete from inside
1318 if (inMacroMode()) {
1319 InsetMathUnknown * p = activeMacro();
1320 if (p->name().size() > 1) {
1321 p->setName(p->name().substr(0, p->name().size() - 1));
1326 if (pos() != 0 && prevAtom()->nargs() > 0) {
1327 // let's require two backspaces for 'big stuff' and
1328 // highlight on the first
1340 bool Cursor::erase()
1342 autocorrect() = false;
1347 cap::eraseSelection(*this);
1351 // delete empty cells if possible
1352 if (pos() == lastpos() && inset().idxDelete(idx()))
1355 // special behaviour when in last position of cell
1356 if (pos() == lastpos()) {
1357 bool one_cell = inset().nargs() == 1;
1358 if (one_cell && lastpos() == 0) {
1360 // Directly delete empty cell: [|[]] => [|]
1366 // [|], can not delete from inside
1371 inset().idxGlue(idx());
1375 // 'clever' UI hack: only erase large items if previously slected
1376 if (pos() != lastpos() && nextAtom()->nargs() > 0) {
1391 DocIterator save = *this;
1392 FuncRequest cmd(selection() ? LFUN_UP_SELECT : LFUN_UP, docstring());
1393 this->dispatch(cmd);
1394 if (disp_.dispatched())
1397 autocorrect() = false;
1405 DocIterator save = *this;
1406 FuncRequest cmd(selection() ? LFUN_DOWN_SELECT : LFUN_DOWN, docstring());
1407 this->dispatch(cmd);
1408 if (disp_.dispatched())
1411 autocorrect() = false;
1416 bool Cursor::macroModeClose()
1420 InsetMathUnknown * p = activeMacro();
1423 asArray(p->selection(), selection);
1424 docstring const s = p->name();
1426 cell().erase(pos());
1428 // do nothing if the macro name is empty
1432 // trigger updates of macros, at least, if no full
1433 // updates take place anyway
1434 updateFlags(Update::Force);
1436 docstring const name = s.substr(1);
1437 InsetMathNest * const in = inset().asInsetMath()->asNestInset();
1438 if (in && in->interpretString(*this, s))
1440 MathAtom atom = createInsetMath(name);
1442 // try to put argument into macro, if we just inserted a macro
1443 bool macroArg = false;
1444 MathMacro * atomAsMacro = atom.nucleus()->asMacro();
1446 // macros here are still unfolded (in init mode in fact). So
1447 // we have to resolve the macro here manually and check its arity
1448 // to put the selection behind it if arity > 0.
1449 MacroData const * data = buffer()->getMacro(atomAsMacro->name());
1450 if (selection.size() > 0 && data && data->numargs() - data->optionals() > 0) {
1452 atomAsMacro->setDisplayMode(MathMacro::DISPLAY_INTERACTIVE_INIT, 1);
1454 // non-greedy case. Do not touch the arguments behind
1455 atomAsMacro->setDisplayMode(MathMacro::DISPLAY_INTERACTIVE_INIT, 0);
1458 // insert remembered selection into first argument of a non-macro
1459 else if (atom.nucleus()->nargs() > 0)
1460 atom.nucleus()->cell(0).append(selection);
1464 // finally put the macro argument behind, if needed
1466 if (selection.size() > 1)
1467 plainInsert(MathAtom(new InsetMathBrace(selection)));
1476 docstring Cursor::macroName()
1478 return inMacroMode() ? activeMacro()->name() : docstring();
1482 void Cursor::handleNest(MathAtom const & a, int c)
1484 //lyxerr << "Cursor::handleNest: " << c << endl;
1486 asArray(cap::grabAndEraseSelection(*this), t.nucleus()->cell(c));
1489 pushBackward(*nextInset());
1493 int Cursor::targetX() const
1495 if (x_target() != -1)
1504 int Cursor::textTargetOffset() const
1506 return textTargetOffset_;
1510 void Cursor::setTargetX()
1519 bool Cursor::inMacroMode() const
1525 InsetMathUnknown const * p = prevAtom()->asUnknownInset();
1526 return p && !p->final();
1530 InsetMathUnknown * Cursor::activeMacro()
1532 return inMacroMode() ? prevAtom().nucleus()->asUnknownInset() : 0;
1536 InsetMathUnknown const * Cursor::activeMacro() const
1538 return inMacroMode() ? prevAtom().nucleus()->asUnknownInset() : 0;
1542 void Cursor::pullArg()
1545 MathData ar = cell();
1546 if (popBackward() && inMathed()) {
1548 cell().insert(pos(), ar);
1551 //formula()->mutateToText();
1556 void Cursor::touch()
1560 DocIterator::const_iterator it = begin();
1561 DocIterator::const_iterator et = end();
1562 for ( ; it != et; ++it)
1568 void Cursor::normalize()
1570 if (idx() > lastidx()) {
1571 lyxerr << "this should not really happen - 1: "
1572 << idx() << ' ' << nargs()
1573 << " in: " << &inset() << endl;
1577 if (pos() > lastpos()) {
1578 lyxerr << "this should not really happen - 2: "
1579 << pos() << ' ' << lastpos() << " in idx: " << idx()
1581 odocstringstream os;
1582 WriteStream wi(os, false, true, false);
1583 inset().asInsetMath()->write(wi);
1584 lyxerr << to_utf8(os.str()) << endl;
1590 bool Cursor::upDownInMath(bool up)
1592 // Be warned: The 'logic' implemented in this function is highly
1593 // fragile. A distance of one pixel or a '<' vs '<=' _really
1594 // matters. So fiddle around with it only if you think you know
1595 // what you are doing!
1599 xo = theLyXFunc().cursorBeforeDispatchX();
1601 // check if we had something else in mind, if not, this is the future
1603 if (x_target_ == -1)
1605 else if (inset().asInsetText() && xo - textTargetOffset() != x_target()) {
1606 // In text mode inside the line (not left or right) possibly set a new target_x,
1607 // but only if we are somewhere else than the previous target-offset.
1609 // We want to keep the x-target on subsequent up/down movements
1610 // that cross beyond the end of short lines. Thus a special
1611 // handling when the cursor is at the end of line: Use the new
1612 // x-target only if the old one was before the end of line
1613 // or the old one was after the beginning of the line
1614 bool inRTL = isWithinRtlParagraph(*this);
1618 left = pos() == textRow().endpos();
1619 right = pos() == textRow().pos();
1621 left = pos() == textRow().pos();
1622 right = pos() == textRow().endpos();
1624 if ((!left && !right) ||
1625 (left && !right && xo < x_target_) ||
1626 (!left && right && x_target_ < xo))
1633 // try neigbouring script insets
1635 if (inMathed() && !selection()) {
1638 InsetMathScript const * p = prevAtom()->asScriptInset();
1639 if (p && p->has(up)) {
1641 push(*const_cast<InsetMathScript*>(p));
1642 idx() = p->idxOfScript(up);
1645 // we went in the right direction? Otherwise don't jump into the script
1649 int oy = theLyXFunc().cursorBeforeDispatchY();
1650 if ((!up && y <= oy) ||
1659 if (pos() != lastpos()) {
1660 InsetMathScript const * p = nextAtom()->asScriptInset();
1661 if (p && p->has(up)) {
1662 push(*const_cast<InsetMathScript*>(p));
1663 idx() = p->idxOfScript(up);
1666 // we went in the right direction? Otherwise don't jump into the script
1670 int oy = theLyXFunc().cursorBeforeDispatchY();
1671 if ((!up && y <= oy) ||
1680 // try to find an inset that knows better then we,
1681 if (inset().idxUpDown(*this, up)) {
1682 //lyxerr << "idxUpDown triggered" << endl;
1683 // try to find best position within this inset
1685 setCursor(bruteFind2(*this, xo, yo));
1689 // any improvement going just out of inset?
1690 if (popBackward() && inMathed()) {
1691 //lyxerr << "updown: popBackward succeeded" << endl;
1694 int yold = theLyXFunc().cursorBeforeDispatchY();
1696 if (up ? ynew < yold : ynew > yold)
1700 // no success, we are probably at the document top or bottom
1706 bool Cursor::atFirstOrLastRow(bool up)
1708 TextMetrics const & tm = bv_->textMetrics(text());
1709 ParagraphMetrics const & pm = tm.parMetrics(pit());
1712 if (pos() && boundary())
1713 row = pm.pos2row(pos() - 1);
1715 row = pm.pos2row(pos());
1718 if (pit() == 0 && row == 0)
1721 if (pit() + 1 >= int(text()->paragraphs().size()) &&
1722 row + 1 >= int(pm.rows().size()))
1728 bool Cursor::upDownInText(bool up, bool & updateNeeded)
1730 LASSERT(text(), /**/);
1736 xo = theLyXFunc().cursorBeforeDispatchX();
1738 // update the targetX - this is here before the "return false"
1739 // to set a new target which can be used by InsetTexts above
1740 // if we cannot move up/down inside this inset anymore
1741 if (x_target_ == -1)
1743 else if (xo - textTargetOffset() != x_target() &&
1744 depth() == beforeDispatchCursor_.depth()) {
1745 // In text mode inside the line (not left or right) possibly set a new target_x,
1746 // but only if we are somewhere else than the previous target-offset.
1748 // We want to keep the x-target on subsequent up/down movements
1749 // that cross beyond the end of short lines. Thus a special
1750 // handling when the cursor is at the end of line: Use the new
1751 // x-target only if the old one was before the end of line
1752 // or the old one was after the beginning of the line
1753 bool inRTL = isWithinRtlParagraph(*this);
1757 left = pos() == textRow().endpos();
1758 right = pos() == textRow().pos();
1760 left = pos() == textRow().pos();
1761 right = pos() == textRow().endpos();
1763 if ((!left && !right) ||
1764 (left && !right && xo < x_target_) ||
1765 (!left && right && x_target_ < xo))
1772 // first get the current line
1773 TextMetrics & tm = bv_->textMetrics(text());
1774 ParagraphMetrics const & pm = tm.parMetrics(pit());
1776 if (pos() && boundary())
1777 row = pm.pos2row(pos() - 1);
1779 row = pm.pos2row(pos());
1781 if (atFirstOrLastRow(up))
1784 // with and without selection are handled differently
1786 int yo = bv().getPos(*this, boundary()).y_;
1788 // To next/previous row
1790 tm.editXY(*this, xo, yo - textRow().ascent() - 1);
1792 tm.editXY(*this, xo, yo + textRow().descent() + 1);
1795 // This happens when you move out of an inset.
1796 // And to give the DEPM the possibility of doing
1797 // something we must provide it with two different
1799 Cursor dummy = *this;
1802 if (bv().checkDepm(dummy, old)) {
1803 updateNeeded = true;
1804 // Make sure that cur gets back whatever happened to dummy(Lgb)
1808 // if there is a selection, we stay out of any inset, and just jump to the right position:
1812 top().pos() = min(tm.x2pos(pit(), row - 1, xo), top().lastpos());
1813 } else if (pit() > 0) {
1815 TextMetrics & tm = bv_->textMetrics(text());
1816 if (!tm.contains(pit()))
1817 tm.newParMetricsUp();
1818 ParagraphMetrics const & pmcur = tm.parMetrics(pit());
1819 top().pos() = min(tm.x2pos(pit(), pmcur.rows().size() - 1, xo), top().lastpos());
1822 if (row + 1 < int(pm.rows().size())) {
1823 top().pos() = min(tm.x2pos(pit(), row + 1, xo), top().lastpos());
1824 } else if (pit() + 1 < int(text()->paragraphs().size())) {
1826 TextMetrics & tm = bv_->textMetrics(text());
1827 if (!tm.contains(pit()))
1828 tm.newParMetricsDown();
1829 top().pos() = min(tm.x2pos(pit(), 0, xo), top().lastpos());
1833 updateNeeded |= bv().checkDepm(*this, old);
1836 updateTextTargetOffset();
1841 void Cursor::handleFont(string const & font)
1843 LYXERR(Debug::DEBUG, font);
1847 safe = cap::grabAndEraseSelection(*this);
1852 if (lastpos() != 0) {
1853 // something left in the cell
1855 // cursor in first position
1857 } else if (pos() == lastpos()) {
1858 // cursor in last position
1861 // cursor in between. split cell
1862 MathData::iterator bt = cell().begin();
1863 MathAtom at = createInsetMath(from_utf8(font));
1864 at.nucleus()->cell(0) = MathData(bt, bt + pos());
1865 cell().erase(bt, bt + pos());
1870 // nothing left in the cell
1879 void Cursor::message(docstring const & msg) const
1881 theLyXFunc().setMessage(msg);
1885 void Cursor::errorMessage(docstring const & msg) const
1887 theLyXFunc().setErrorMessage(msg);
1891 docstring Cursor::selectionAsString(bool with_label) const
1896 int const label = with_label
1897 ? AS_STR_LABEL | AS_STR_INSETS : AS_STR_INSETS;
1900 idx_type const startidx = selBegin().idx();
1901 idx_type const endidx = selEnd().idx();
1902 if (startidx != endidx) {
1903 // multicell selection
1904 InsetTabular * table = inset().asInsetTabular();
1905 LASSERT(table, return docstring());
1906 return table->asString(startidx, endidx);
1909 ParagraphList const & pars = text()->paragraphs();
1911 pit_type const startpit = selBegin().pit();
1912 pit_type const endpit = selEnd().pit();
1913 size_t const startpos = selBegin().pos();
1914 size_t const endpos = selEnd().pos();
1916 if (startpit == endpit)
1917 return pars[startpit].asString(startpos, endpos, label);
1919 // First paragraph in selection
1920 docstring result = pars[startpit].
1921 asString(startpos, pars[startpit].size(), label)
1922 + parbreak(pars[startpit]);
1924 // The paragraphs in between (if any)
1925 for (pit_type pit = startpit + 1; pit != endpit; ++pit) {
1926 Paragraph const & par = pars[pit];
1927 result += par.asString(0, par.size(), label)
1928 + parbreak(pars[pit]);
1931 // Last paragraph in selection
1932 result += pars[endpit].asString(0, endpos, label);
1938 return cap::grabSelection(*this);
1944 docstring Cursor::currentState() const
1947 odocstringstream os;
1953 return text()->currentState(*this);
1959 docstring Cursor::getPossibleLabel() const
1961 return inMathed() ? from_ascii("eq:") : text()->getPossibleLabel(*this);
1965 Encoding const * Cursor::getEncoding() const
1969 CursorSlice const & sl = innerTextSlice();
1970 Text const & text = *sl.text();
1971 Font font = text.getPar(sl.pit()).getFont(
1972 bv().buffer().params(), sl.pos(), outerFont(sl.pit(), text.paragraphs()));
1973 return font.language()->encoding();
1977 void Cursor::undispatched()
1979 disp_.dispatched(false);
1983 void Cursor::dispatched()
1985 disp_.dispatched(true);
1989 void Cursor::updateFlags(Update::flags f)
1995 void Cursor::noUpdate()
1997 disp_.update(Update::None);
2001 Font Cursor::getFont() const
2003 // The logic here should more or less match to the Cursor::setCurrentFont
2004 // logic, i.e. the cursor height should give a hint what will happen
2005 // if a character is entered.
2007 // HACK. far from being perfect...
2009 CursorSlice const & sl = innerTextSlice();
2010 Text const & text = *sl.text();
2011 Paragraph const & par = text.getPar(sl.pit());
2013 // on boundary, so we are really at the character before
2014 pos_type pos = sl.pos();
2015 if (pos > 0 && boundary())
2018 // on space? Take the font before (only for RTL boundary stay)
2020 TextMetrics const & tm = bv().textMetrics(&text);
2021 if (pos == sl.lastpos()
2022 || (par.isSeparator(pos)
2023 && !tm.isRTLBoundary(sl.pit(), pos)))
2027 // get font at the position
2028 Font font = par.getFont(buffer()->params(), pos,
2029 outerFont(sl.pit(), text.paragraphs()));
2035 bool Cursor::fixIfBroken()
2037 if (DocIterator::fixIfBroken()) {
2045 bool notifyCursorLeavesOrEnters(Cursor const & old, Cursor & cur)
2047 // find inset in common
2049 for (i = 0; i < old.depth() && i < cur.depth(); ++i) {
2050 if (&old[i].inset() != &cur[i].inset())
2054 // update words if we just moved to another paragraph
2055 if (i == old.depth() && i == cur.depth()
2056 && !cur.buffer()->isClean()
2057 && cur.inTexted() && old.inTexted()
2058 && cur.pit() != old.pit()) {
2059 old.paragraph().updateWords(old.top());
2062 // notify everything on top of the common part in old cursor,
2063 // but stop if the inset claims the cursor to be invalid now
2064 for (size_type j = i; j < old.depth(); ++j) {
2065 Cursor insetPos = old;
2067 if (old[j].inset().notifyCursorLeaves(insetPos, cur))
2071 // notify everything on top of the common part in new cursor,
2072 // but stop if the inset claims the cursor to be invalid now
2073 for (; i < cur.depth(); ++i) {
2074 if (cur[i].inset().notifyCursorEnters(cur))
2082 void Cursor::setCurrentFont()
2084 CursorSlice const & cs = innerTextSlice();
2085 Paragraph const & par = cs.paragraph();
2086 pos_type cpit = cs.pit();
2087 pos_type cpos = cs.pos();
2088 Text const & ctext = *cs.text();
2089 TextMetrics const & tm = bv().textMetrics(&ctext);
2091 // are we behind previous char in fact? -> go to that char
2092 if (cpos > 0 && boundary())
2095 // find position to take the font from
2097 // paragraph end? -> font of last char
2098 if (cpos == lastpos())
2100 // on space? -> look at the words in front of space
2101 else if (cpos > 0 && par.isSeparator(cpos)) {
2102 // abc| def -> font of c
2103 // abc |[WERBEH], i.e. boundary==true -> font of c
2104 // abc [WERBEH]| def, font of the space
2105 if (!tm.isRTLBoundary(cpit, cpos))
2111 BufferParams const & bufparams = buffer()->params();
2112 current_font = par.getFontSettings(bufparams, cpos);
2113 real_current_font = tm.displayFont(cpit, cpos);
2115 // special case for paragraph end
2116 if (cs.pos() == lastpos()
2117 && tm.isRTLBoundary(cpit, cs.pos())
2119 Language const * lang = par.getParLanguage(bufparams);
2120 current_font.setLanguage(lang);
2121 current_font.fontInfo().setNumber(FONT_OFF);
2122 real_current_font.setLanguage(lang);
2123 real_current_font.fontInfo().setNumber(FONT_OFF);
2128 bool Cursor::textUndo()
2130 DocIterator dit = *this;
2131 // Undo::textUndo() will modify dit.
2132 if (!buffer()->undo().textUndo(dit))
2142 bool Cursor::textRedo()
2144 DocIterator dit = *this;
2145 // Undo::textRedo() will modify dit.
2146 if (!buffer()->undo().textRedo(dit))
2156 void Cursor::finishUndo() const
2158 buffer()->undo().finishUndo();
2162 void Cursor::beginUndoGroup() const
2164 buffer()->undo().beginUndoGroup();
2168 void Cursor::endUndoGroup() const
2170 buffer()->undo().endUndoGroup();
2174 void Cursor::recordUndo(UndoKind kind, pit_type from, pit_type to) const
2176 buffer()->undo().recordUndo(*this, kind, from, to);
2180 void Cursor::recordUndo(UndoKind kind, pit_type from) const
2182 buffer()->undo().recordUndo(*this, kind, from);
2186 void Cursor::recordUndo(UndoKind kind) const
2188 buffer()->undo().recordUndo(*this, kind);
2192 void Cursor::recordUndoInset(UndoKind kind) const
2194 buffer()->undo().recordUndoInset(*this, kind);
2198 void Cursor::recordUndoFullDocument() const
2200 buffer()->undo().recordUndoFullDocument(*this);
2204 void Cursor::recordUndoSelection() const
2207 if (cap::multipleCellsSelected(*this))
2212 buffer()->undo().recordUndo(*this, ATOMIC_UNDO,
2213 selBegin().pit(), selEnd().pit());
2218 void Cursor::checkBufferStructure()
2220 Buffer const * master = buffer()->masterBuffer();
2221 master->tocBackend().updateItem(*this);
2222 if (master != buffer() && !master->hasGuiDelegate())
2223 // In case the master has no gui associated with it,
2224 // the TocItem is not updated (part of bug 5699).
2225 buffer()->tocBackend().updateItem(*this);