+bool Cursor::posVisRight(bool skip_inset)
+{
+ Cursor new_cur = *this; // where we will move to
+ pos_type left_pos; // position visually left of current cursor
+ pos_type right_pos; // position visually right of current cursor
+
+ getSurroundingPos(left_pos, right_pos);
+
+ LYXERR(Debug::RTL, left_pos <<"|"<< right_pos << " (pos: "<< pos() <<")");
+
+ // Are we at an inset?
+ new_cur.pos() = right_pos;
+ new_cur.boundary(false);
+ if (!skip_inset &&
+ text()->checkAndActivateInsetVisual(new_cur, right_pos >= pos(), false)) {
+ // we actually move the cursor at the end of this
+ // function, for now we just keep track of the new
+ // position in new_cur...
+ LYXERR(Debug::RTL, "entering inset at: " << new_cur.pos());
+ }
+
+ // Are we already at rightmost pos in row?
+ else if (text()->empty() || right_pos == -1) {
+
+ new_cur = *this;
+ if (!new_cur.posVisToNewRow(false)) {
+ LYXERR(Debug::RTL, "not moving!");
+ return false;
+ }
+
+ // we actually move the cursor at the end of this
+ // function, for now just keep track of the new
+ // position in new_cur...
+ LYXERR(Debug::RTL, "right edge, moving: " << int(new_cur.pit()) << ","
+ << int(new_cur.pos()) << "," << (new_cur.boundary() ? 1 : 0));
+
+ }
+ // normal movement to the right
+ else {
+ new_cur = *this;
+ // Recall, if the cursor is at position 'x', that
+ // means *before* the character at position 'x'. In
+ // RTL, "before" means "to the right of", in LTR, "to
+ // the left of". So currently our situation is this:
+ // the position to our right is 'right_pos' (i.e.,
+ // we're currently to the left of 'right_pos'). In
+ // order to move to the right, it depends whether or
+ // not the character at 'right_pos' is RTL.
+ bool const new_pos_is_RTL = paragraph().getFontSettings(
+ buffer()->params(), right_pos).isVisibleRightToLeft();
+ // If the character at 'right_pos' *is* LTR, then in
+ // order to move to the right of it, we need to be
+ // *after* 'right_pos', i.e., move to position
+ // 'right_pos' + 1.
+ if (!new_pos_is_RTL) {
+ new_cur.pos() = right_pos + 1;
+ // set the boundary to true in two situations:
+ if (
+ // 1. if new_pos is now lastpos, and we're in
+ // an RTL paragraph (this means that we're
+ // moving right to the end of an LTR chunk
+ // which is at the end of an RTL paragraph);
+ (new_cur.pos() == lastpos()
+ && paragraph().isRTL(buffer()->params()))
+ // 2. if the position *after* right_pos is RTL
+ // (we want to be *after* right_pos, not
+ // before right_pos + 1!)
+ || paragraph().getFontSettings(buffer()->params(),
+ new_cur.pos()).isVisibleRightToLeft()
+ )
+ new_cur.boundary(true);
+ else // set the boundary to false
+ new_cur.boundary(false);
+ }
+ // Otherwise (if the character at position 'right_pos'
+ // is RTL), then moving to the right of it is as easy
+ // as setting the new position to 'right_pos'.
+ else {
+ new_cur.pos() = right_pos;
+ new_cur.boundary(false);
+ }
+
+ }
+
+ bool const moved = new_cur != *this || new_cur.boundary() != boundary();
+
+ if (moved) {
+ LYXERR(Debug::RTL, "moving to: " << new_cur.pos()
+ << (new_cur.boundary() ? " (boundary)" : ""));
+ *this = new_cur;
+ }
+
+ return moved;
+}
+
+
+bool Cursor::posVisLeft(bool skip_inset)
+{
+ Cursor new_cur = *this; // where we will move to
+ pos_type left_pos; // position visually left of current cursor
+ pos_type right_pos; // position visually right of current cursor
+
+ getSurroundingPos(left_pos, right_pos);
+
+ LYXERR(Debug::RTL, left_pos <<"|"<< right_pos << " (pos: "<< pos() <<")");
+
+ // Are we at an inset?
+ new_cur.pos() = left_pos;
+ new_cur.boundary(false);
+ if (!skip_inset &&
+ text()->checkAndActivateInsetVisual(new_cur, left_pos >= pos(), true)) {
+ // we actually move the cursor at the end of this
+ // function, for now we just keep track of the new
+ // position in new_cur...
+ LYXERR(Debug::RTL, "entering inset at: " << new_cur.pos());
+ }
+
+ // Are we already at leftmost pos in row?
+ else if (text()->empty() || left_pos == -1) {
+
+ new_cur = *this;
+ if (!new_cur.posVisToNewRow(true)) {
+ LYXERR(Debug::RTL, "not moving!");
+ return false;
+ }
+
+ // we actually move the cursor at the end of this
+ // function, for now just keep track of the new
+ // position in new_cur...
+ LYXERR(Debug::RTL, "left edge, moving: " << int(new_cur.pit()) << ","
+ << int(new_cur.pos()) << "," << (new_cur.boundary() ? 1 : 0));
+
+ }
+ // normal movement to the left
+ else {
+ new_cur = *this;
+ // Recall, if the cursor is at position 'x', that
+ // means *before* the character at position 'x'. In
+ // RTL, "before" means "to the right of", in LTR, "to
+ // the left of". So currently our situation is this:
+ // the position to our left is 'left_pos' (i.e., we're
+ // currently to the right of 'left_pos'). In order to
+ // move to the left, it depends whether or not the
+ // character at 'left_pos' is RTL.
+ bool const new_pos_is_RTL = paragraph().getFontSettings(
+ buffer()->params(), left_pos).isVisibleRightToLeft();
+ // If the character at 'left_pos' *is* RTL, then in
+ // order to move to the left of it, we need to be
+ // *after* 'left_pos', i.e., move to position
+ // 'left_pos' + 1.
+ if (new_pos_is_RTL) {
+ new_cur.pos() = left_pos + 1;
+ // set the boundary to true in two situations:
+ if (
+ // 1. if new_pos is now lastpos and we're in
+ // an LTR paragraph (this means that we're
+ // moving left to the end of an RTL chunk
+ // which is at the end of an LTR paragraph);
+ (new_cur.pos() == lastpos()
+ && !paragraph().isRTL(buffer()->params()))
+ // 2. if the position *after* left_pos is not
+ // RTL (we want to be *after* left_pos, not
+ // before left_pos + 1!)
+ || !paragraph().getFontSettings(buffer()->params(),
+ new_cur.pos()).isVisibleRightToLeft()
+ )
+ new_cur.boundary(true);
+ else // set the boundary to false
+ new_cur.boundary(false);
+ }
+ // Otherwise (if the character at position 'left_pos'
+ // is LTR), then moving to the left of it is as easy
+ // as setting the new position to 'left_pos'.
+ else {
+ new_cur.pos() = left_pos;
+ new_cur.boundary(false);
+ }
+
+ }
+
+ bool const moved = new_cur != *this || new_cur.boundary() != boundary();
+
+ if (moved) {
+ LYXERR(Debug::RTL, "moving to: " << new_cur.pos()
+ << (new_cur.boundary() ? " (boundary)" : ""));
+ *this = new_cur;
+ }
+
+ return moved;
+}
+
+
+namespace {
+
+// Return true on success
+bool findNonVirtual(Row const & row, Row::const_iterator & cit, bool onleft)
+{
+ if (onleft) {
+ while (cit != row.begin() && cit->isVirtual())
+ --cit;
+ } else {
+ while (cit != row.end() && cit->isVirtual())
+ ++cit;
+ }
+ return cit != row.end() && !cit->isVirtual();
+}
+
+}
+
+void Cursor::getSurroundingPos(pos_type & left_pos, pos_type & right_pos) const
+{
+ // by default, we know nothing.
+ left_pos = -1;
+ right_pos = -1;
+
+ Row const & row = textRow();
+ TextMetrics const & tm = bv_->textMetrics(text());
+ double dummy = 0;
+ Row::const_iterator cit = tm.findRowElement(row, pos(), boundary(), dummy);
+ // Handle the case of empty row
+ if (cit == row.end()) {
+ if (paragraph().isRTL(buffer()->params()))
+ right_pos = row.pos();
+ else
+ left_pos = row.pos() - 1;
+ return;
+ }
+
+ // skip virtual elements and exit if no non-virtual one exists
+ if (!findNonVirtual(row, cit, !cit->isRTL()))
+ return;
+
+ // if the position is at the left side of the element, we have to
+ // look at the previous element
+ if (pos() == cit->left_pos()) {
+ LYXERR(Debug::RTL, "getSurroundingPos(" << pos() << (boundary() ? "b" : "")
+ << "), AT LEFT of *cit=" << *cit);
+ // this one is easy (see common case below)
+ right_pos = pos() - (cit->isRTL() ? 1 : 0);
+ // at the left of the row
+ if (cit == row.begin())
+ return;
+ --cit;
+ if (!findNonVirtual(row, cit, true))
+ return;
+ // [...[ is the row element, | is cursor position (! with boundary)
+ // [ 1 2 [ is a ltr row element with pos=1 and endpos=3
+ // ] 2 1] is an rtl row element with pos=1 and endpos=3
+ // [ 1 2 [ [|3 4 [ => (2, 3)
+ // or [ 1 2 [ ]!4 3 ] => (2, 4)
+ // or ] 2 1 ] [|3 4 [ => (1, 3)
+ // or ] 4 3 ] ]!2 1 ] => (3, 2)
+ left_pos = cit->right_pos() - (cit->isRTL() ? 0 : 1);
+ // happens with consecutive row of same direction
+ if (left_pos == right_pos) {
+ left_pos += cit->isRTL() ? 1 : -1;
+ }
+ }
+ // same code but with the element at the right
+ else if (pos() == cit->right_pos()) {
+ LYXERR(Debug::RTL, "getSurroundingPos(" << pos() << (boundary() ? "b" : "")
+ << "), AT RIGHT of *cit=" << *cit);
+ // this one is easy (see common case below)
+ left_pos = pos() - (cit->isRTL() ? 0 : 1);
+ // at the right of the row
+ if (cit + 1 == row.end())
+ return;
+ ++cit;
+ if (!findNonVirtual(row, cit, false))
+ return;
+ // [ 1 2![ [ 3 4 [ => (2, 3)
+ // or [ 1 2![ ] 4 3 ] => (2, 4)
+ // or ] 2 1|] [ 3 4 [ => (1, 3)
+ // or ] 4 3|] ] 2 1 ] => (3, 2)
+ right_pos = cit->left_pos() - (cit->isRTL() ? 1 : 0);
+ // happens with consecutive row of same direction
+ if (right_pos == left_pos)
+ right_pos += cit->isRTL() ? -1 : 1;
+ }
+ // common case: both positions are inside the row element
+ else {
+ // [ 1 2|3 [ => (2, 3)
+ // or ] 3|2 1 ] => (3, 2)
+ left_pos = pos() - (cit->isRTL() ? 0 : 1);
+ right_pos = pos() - (cit->isRTL() ? 1 : 0);
+ }
+
+ // Note that debug message does not catch all early returns above
+ LYXERR(Debug::RTL,"getSurroundingPos(" << pos() << (boundary() ? "b" : "")
+ << ") => (" << left_pos << ", " << right_pos <<")");
+}
+
+
+bool Cursor::posVisToNewRow(bool movingLeft)
+{
+ Paragraph const & par = paragraph();
+ Buffer const & buf = *buffer();
+ Row const & row = textRow();
+ bool par_is_LTR = !par.isRTL(buf.params());
+
+ // Inside a table, determining whether to move to the next or
+ // previous row should be done based on the table's direction.
+ if (inset().asInsetTabular()) {
+ par_is_LTR = !inset().asInsetTabular()->isRightToLeft(*this);
+ LYXERR(Debug::RTL, "Inside table! par_is_LTR=" << (par_is_LTR ? 1 : 0));
+ }
+
+ // if moving left in an LTR paragraph or moving right in an
+ // RTL one, move to previous row
+ if (par_is_LTR == movingLeft) {
+ if (row.pos() == 0) { // we're at first row in paragraph
+ if (pit() == 0) // no previous paragraph! don't move
+ return false;
+ // move to last pos in previous par
+ --pit();
+ pos() = lastpos();
+ boundary(false);
+ } else { // move to previous row in this par
+ pos() = row.pos() - 1; // this is guaranteed to be in previous row
+ boundary(false);
+ }
+ }
+ // if moving left in an RTL paragraph or moving right in an
+ // LTR one, move to next row
+ else {
+ if (row.endpos() == lastpos()) { // we're at last row in paragraph
+ if (pit() == lastpit()) // last paragraph! don't move
+ return false;
+ // move to first row in next par
+ ++pit();
+ pos() = 0;
+ boundary(false);
+ } else { // move to next row in this par
+ pos() = row.endpos();
+ boundary(false);
+ }
+ }
+
+ // make sure we're at left-/right-most pos in new row
+ posVisToRowExtremity(!movingLeft);
+
+ return true;
+}
+
+
+void Cursor::posVisToRowExtremity(bool left)
+{
+ LYXERR(Debug::RTL, "entering extremity: " << pit() << "," << pos() << ","
+ << (boundary() ? 1 : 0));
+
+ TextMetrics const & tm = bv_->textMetrics(text());
+ // Looking for extremities is like clicking on the left or the
+ // right of the row.
+ int x = tm.origin().x_ + (left ? 0 : textRow().width());
+ bool b = false;
+ pos() = tm.getPosNearX(textRow(), x, b);
+ boundary(b);
+
+ LYXERR(Debug::RTL, "leaving extremity: " << pit() << "," << pos() << ","
+ << (boundary() ? 1 : 0));
+}
+
+
+bool Cursor::reverseDirectionNeeded() const