]> git.lyx.org Git - lyx.git/blobdiff - src/Text.cpp
SCons: msvc does not need this /TP option any more after we rename .C => .cpp. Also...
[lyx.git] / src / Text.cpp
index c05cc2130d7512289eb9e49177b323bc7ae0c59d..61467dc207a7a05a84927a0efafa8c14912c714e 100644 (file)
@@ -5,9 +5,11 @@
  *
  * \author Asger Alstrup
  * \author Lars Gullik Bjønnes
+ * \author Dov Feldstern
  * \author Jean-Marc Lasgouttes
  * \author John Levon
  * \author André Pönitz
+ * \author Stefan Schimanski
  * \author Dekel Tsur
  * \author Jürgen Vigna
  *
@@ -116,7 +118,7 @@ void readParToken(Buffer const & buf, Paragraph & par, Lexer & lex,
 #endif
        } else if (token == "\\begin_layout") {
                lex.eatLine();
-               string layoutname = lex.getString();
+               docstring layoutname = lex.getDocString();
 
                font = Font(Font::ALL_INHERIT, bp.language);
                change = Change(Change::UNCHANGED);
@@ -132,7 +134,7 @@ void readParToken(Buffer const & buf, Paragraph & par, Lexer & lex,
                if (!hasLayout) {
                        errorList.push_back(ErrorItem(_("Unknown layout"),
                        bformat(_("Layout '%1$s' does not exist in textclass '%2$s'\nTrying to use the default instead.\n"),
-                       from_utf8(layoutname), from_utf8(tclass.name())), par.id(), 0, par.size()));
+                       layoutname, from_utf8(tclass.name())), par.id(), 0, par.size()));
                        layoutname = tclass.defaultLayoutName();
                }
 
@@ -342,6 +344,13 @@ void readParagraph(Buffer const & buf, Paragraph & par, Lexer & lex,
 } // namespace anon
 
 
+bool Text::empty() const
+{
+       return pars_.empty() || (pars_.size() == 1 && pars_[0].empty()
+               // FIXME: Should we consider the labeled type as empty too? 
+               && pars_[0].layout()->labeltype == LABEL_NO_LABEL);
+}
+
 
 double Text::spacing(Buffer const & buffer,
                Paragraph const & par) const
@@ -352,39 +361,6 @@ double Text::spacing(Buffer const & buffer,
 }
 
 
-int Text::singleWidth(Buffer const & buffer, Paragraph const & par,
-               pos_type pos) const
-{
-       return singleWidth(par, pos, par.getChar(pos),
-               getFont(buffer, par, pos));
-}
-
-
-int Text::singleWidth(Paragraph const & par,
-                        pos_type pos, char_type c, Font const & font) const
-{
-       // The most common case is handled first (Asger)
-       if (isPrintable(c)) {
-               Language const * language = font.language();
-               if (language->rightToLeft()) {
-                       if (language->lang() == "arabic") {
-                               if (Encodings::isComposeChar_arabic(c))
-                                       return 0;
-                               c = par.transformChar(c, pos);
-                       } else if (language->lang() == "hebrew" &&
-                                  Encodings::isComposeChar_hebrew(c))
-                               return 0;
-               }
-               return theFontMetrics(font).width(c);
-       }
-
-       if (c == Paragraph::META_INSET)
-               return par.getInset(pos)->width();
-
-       return theFontMetrics(font).width(c);
-}
-
-
 int Text::leftMargin(Buffer const & buffer, int max_width, pit_type pit) const
 {
        BOOST_ASSERT(pit >= 0);
@@ -405,16 +381,15 @@ int Text::leftMargin(Buffer const & buffer, int max_width,
        TextClass const & tclass = buffer.params().getTextClass();
        Layout_ptr const & layout = par.layout();
 
-       string parindent = layout->parindent;
+       docstring parindent = layout->parindent;
 
        int l_margin = 0;
 
        if (isMainText(buffer))
                l_margin += changebarMargin();
 
-       // FIXME UNICODE
-       docstring leftm = from_utf8(tclass.leftmargin());
-       l_margin += theFontMetrics(buffer.params().getFont()).signedWidth(leftm);
+       l_margin += theFontMetrics(buffer.params().getFont()).signedWidth(
+               tclass.leftmargin());
 
        if (par.getDepth() != 0) {
                // find the next level paragraph
@@ -445,55 +420,41 @@ int Text::leftMargin(Buffer const & buffer, int max_width,
        switch (layout->margintype) {
        case MARGIN_DYNAMIC:
                if (!layout->leftmargin.empty()) {
-                       // FIXME UNICODE
-                       docstring leftm = from_utf8(layout->leftmargin);
-                       l_margin += theFontMetrics(buffer.params().getFont()).signedWidth(leftm);
+                       l_margin += theFontMetrics(buffer.params().getFont()).signedWidth(
+                               layout->leftmargin);
                }
                if (!par.getLabelstring().empty()) {
-                       // FIXME UNICODE
-                       docstring labin = from_utf8(layout->labelindent);
-                       l_margin += labelfont_metrics.signedWidth(labin);
-                       docstring labstr = par.getLabelstring();
-                       l_margin += labelfont_metrics.width(labstr);
-                       docstring labsep = from_utf8(layout->labelsep);
-                       l_margin += labelfont_metrics.width(labsep);
+                       l_margin += labelfont_metrics.signedWidth(layout->labelindent);
+                       l_margin += labelfont_metrics.width(par.getLabelstring());
+                       l_margin += labelfont_metrics.width(layout->labelsep);
                }
                break;
 
        case MARGIN_MANUAL: {
-               // FIXME UNICODE
-               docstring labin = from_utf8(layout->labelindent);
-               l_margin += labelfont_metrics.signedWidth(labin);
+               l_margin += labelfont_metrics.signedWidth(layout->labelindent);
                // The width of an empty par, even with manual label, should be 0
                if (!par.empty() && pos >= par.beginOfBody()) {
                        if (!par.getLabelWidthString().empty()) {
                                docstring labstr = par.getLabelWidthString();
                                l_margin += labelfont_metrics.width(labstr);
-                               docstring labsep = from_utf8(layout->labelsep);
-                               l_margin += labelfont_metrics.width(labsep);
+                               l_margin += labelfont_metrics.width(layout->labelsep);
                        }
                }
                break;
        }
 
        case MARGIN_STATIC: {
-               // FIXME UNICODE
-               docstring leftm = from_utf8(layout->leftmargin);
-               l_margin += theFontMetrics(buffer.params().getFont()).signedWidth(leftm)
-                       * 4     / (par.getDepth() + 4);
+               l_margin += theFontMetrics(buffer.params().getFont()).
+                       signedWidth(layout->leftmargin) * 4     / (par.getDepth() + 4);
                break;
        }
 
        case MARGIN_FIRST_DYNAMIC:
                if (layout->labeltype == LABEL_MANUAL) {
                        if (pos >= par.beginOfBody()) {
-                               // FIXME UNICODE
-                               l_margin += labelfont_metrics.signedWidth(
-                                       from_utf8(layout->leftmargin));
+                               l_margin += labelfont_metrics.signedWidth(layout->leftmargin);
                        } else {
-                               // FIXME UNICODE
-                               l_margin += labelfont_metrics.signedWidth(
-                                       from_utf8(layout->labelindent));
+                               l_margin += labelfont_metrics.signedWidth(layout->labelindent);
                        }
                } else if (pos != 0
                           // Special case to fix problems with
@@ -501,14 +462,13 @@ int Text::leftMargin(Buffer const & buffer, int max_width,
                           || (layout->labeltype == LABEL_STATIC
                               && layout->latextype == LATEX_ENVIRONMENT
                               && !isFirstInSequence(pit, pars_))) {
-                       // FIXME UNICODE
-                       l_margin += labelfont_metrics.signedWidth(from_utf8(layout->leftmargin));
+                       l_margin += labelfont_metrics.signedWidth(layout->leftmargin);
                } else if (layout->labeltype != LABEL_TOP_ENVIRONMENT
                           && layout->labeltype != LABEL_BIBLIO
                           && layout->labeltype !=
                           LABEL_CENTERED_TOP_ENVIRONMENT) {
-                       l_margin += labelfont_metrics.signedWidth(from_utf8(layout->labelindent));
-                       l_margin += labelfont_metrics.width(from_utf8(layout->labelsep));
+                       l_margin += labelfont_metrics.signedWidth(layout->labelindent);
+                       l_margin += labelfont_metrics.width(layout->labelsep);
                        l_margin += labelfont_metrics.width(par.getLabelstring());
                }
                break;
@@ -519,9 +479,7 @@ int Text::leftMargin(Buffer const & buffer, int max_width,
                // row in this paragraph.
                RowList::iterator rit = par.rows().begin();
                RowList::iterator end = par.rows().end();
-#ifdef WITH_WARNINGS
-#warning This is wrong.
-#endif
+               // FIXME: This is wrong.
                int minfill = max_width;
                for ( ; rit != end; ++rit)
                        if (rit->fill() < minfill)
@@ -565,8 +523,8 @@ int Text::leftMargin(Buffer const & buffer, int max_width,
                || buffer.params().paragraph_separation ==
                   BufferParams::PARSEP_INDENT))
        {
-               docstring din = from_utf8(parindent);
-               l_margin += theFontMetrics(buffer.params().getFont()).signedWidth(din);
+               l_margin += theFontMetrics(buffer.params().getFont()).signedWidth(
+                       parindent);
        }
 
        return l_margin;
@@ -640,12 +598,7 @@ void Text::breakParagraph(Cursor & cur, bool keep_layout)
                        break; // the character couldn't be deleted physically due to change tracking
        }
 
-       ParIterator current_it(cur);
-       ParIterator last_it(cur);
-       ++last_it;
-       ++last_it;
-
-       updateLabels(cur.buffer(), current_it, last_it);
+       updateLabels(cur.buffer());
 
        // A singlePar update is not enough in this case.
        cur.updateFlags(Update::Force);
@@ -711,7 +664,51 @@ void Text::insertChar(Cursor & cur, char_type c)
                }
        }
 
-       // First check, if there will be two blanks together or a blank at
+       // In Bidi text, we want spaces to be treated in a special way: spaces
+       // which are between words in different languages should get the 
+       // paragraph's language; otherwise, spaces should keep the language 
+       // they were originally typed in. This is only in effect while typing;
+       // after the text is already typed in, the user can always go back and
+       // explicitly set the language of a space as desired. But 99.9% of the
+       // time, what we're doing here is what the user actually meant.
+       // 
+       // The following cases are the ones in which the language of the space
+       // should be changed to match that of the containing paragraph. In the
+       // depictions, lowercase is LTR, uppercase is RTL, underscore (_) 
+       // represents a space, pipe (|) represents the cursor position (so the
+       // character before it is the one just typed in). The different cases
+       // are depicted logically (not visually), from left to right:
+       // 
+       // 1. A_a|
+       // 2. a_A|
+       //
+       // Theoretically, there are other situations that we should, perhaps, deal
+       // with (e.g.: a|_A, A|_a). In practice, though, there really isn't any 
+       // point (to understand why, just try to create this situation...).
+
+       if ((cur.pos() >= 2) && (par.isLineSeparator(cur.pos() - 1))) {
+               // get font in front and behind the space in question. But do NOT 
+               // use getFont(cur.pos()) because the character c is not inserted yet
+               Font const & pre_space_font  = getFont(buffer, par, cur.pos() - 2);
+               Font const & post_space_font = real_current_font;
+               bool pre_space_rtl  = pre_space_font.isVisibleRightToLeft();
+               bool post_space_rtl = post_space_font.isVisibleRightToLeft();
+               
+               if (pre_space_rtl != post_space_rtl) {
+                       // Set the space's language to match the language of the 
+                       // adjacent character whose direction is the paragraph's
+                       // direction; don't touch other properties of the font
+                       Language const * lang = 
+                               (pre_space_rtl == par.isRightToLeftPar(buffer.params())) ?
+                               pre_space_font.language() : post_space_font.language();
+
+                       Font space_font = getFont(buffer, par, cur.pos() - 1);
+                       space_font.setLanguage(lang);
+                       par.setFont(cur.pos() - 1, space_font);
+               }
+       }
+       
+       // Next check, if there will be two blanks together or a blank at
        // the beginning of a paragraph.
        // I decided to handle blanks like normal characters, the main
        // difference are the special checks when calculating the row.fill
@@ -823,7 +820,6 @@ void Text::selectWord(Cursor & cur, word_location loc)
        cur.resetAnchor();
        setCursor(cur, to.pit(), to.pos());
        cur.setSelection();
-       cap::saveSelection(cur);
 }
 
 
@@ -945,36 +941,8 @@ void Text::acceptOrRejectChanges(Cursor & cur, ChangeOp op)
 
 void Text::acceptChanges(BufferParams const & bparams)
 {
-       pit_type pars_size = static_cast<pit_type>(pars_.size());
-
-       // first, accept changes within each individual paragraph
-       // (do not consider end-of-par)
-       for (pit_type pit = 0; pit < pars_size; ++pit) {
-               if (!pars_[pit].empty())   // prevent assertion failure
-                       pars_[pit].acceptChanges(bparams, 0, pars_[pit].size());
-       }
-
-       // next, accept imaginary end-of-par characters
-       for (pit_type pit = 0; pit < pars_size; ++pit) {
-               pos_type pos = pars_[pit].size();
-
-               if (pars_[pit].isInserted(pos)) {
-                       pars_[pit].setChange(pos, Change(Change::UNCHANGED));
-               } else if (pars_[pit].isDeleted(pos)) {
-                       if (pit == pars_size - 1) {
-                               // we cannot remove a par break at the end of the last
-                               // paragraph; instead, we mark it unchanged
-                               pars_[pit].setChange(pos, Change(Change::UNCHANGED));
-                       } else {
-                               mergeParagraph(bparams, pars_, pit);
-                               --pit;
-                               --pars_size;
-                       }
-               }
-       }
-
-       // finally, invoke the DEPM
-       deleteEmptyParagraphMechanism(0, pars_size - 1, bparams.trackChanges);
+       lyx::acceptChanges(pars_, bparams);
+       deleteEmptyParagraphMechanism(0, pars_.size() - 1, bparams.trackChanges);
 }
 
 
@@ -1214,7 +1182,7 @@ bool Text::erase(Cursor & cur)
                recordUndo(cur, Undo::DELETE);
                if(!par.eraseChar(cur.pos(), cur.buffer().params().trackChanges)) {
                        // the character has been logically deleted only => skip it
-                       cur.forwardPosNoDescend();
+                       cur.top().forwardPos();
                }
                checkBufferStructure(cur.buffer(), cur);
                needsUpdate = true;
@@ -1345,7 +1313,7 @@ bool Text::backspace(Cursor & cur)
 bool Text::dissolveInset(Cursor & cur) {
        BOOST_ASSERT(this == cur.text());
 
-       if (isMainText(*cur.bv().buffer()) || cur.inset().nargs() != 1)
+       if (isMainText(cur.bv().buffer()) || cur.inset().nargs() != 1)
                return false;
 
        recordUndoInset(cur);
@@ -1411,91 +1379,151 @@ void Text::drawSelection(PainterInfo & pi, int x, int) const
        DocIterator end = cur.selectionEnd();
 
        BufferView & bv = *pi.base.bv;
-       Buffer const & buffer = *bv.buffer();
 
-       // the selection doesn't touch the visible screen
+       // the selection doesn't touch the visible screen?
        if (bv_funcs::status(&bv, beg) == bv_funcs::CUR_BELOW
            || bv_funcs::status(&bv, end) == bv_funcs::CUR_ABOVE)
                return;
 
-       Paragraph const & par1 = pars_[beg.pit()];
-       Paragraph const & par2 = pars_[end.pit()];
        TextMetrics const & tm = bv.textMetrics(this);
        ParagraphMetrics const & pm1 = tm.parMetrics(beg.pit());
        ParagraphMetrics const & pm2 = tm.parMetrics(end.pit());
+       Row const & row1 = pm1.getRow(beg.pos(), beg.boundary());
+       Row const & row2 = pm2.getRow(end.pos(), end.boundary());
+
+       // clip above
+       int middleTop;
+       bool const clipAbove = 
+               (bv_funcs::status(&bv, beg) == bv_funcs::CUR_ABOVE);
+       if (clipAbove)
+               middleTop = 0;
+       else
+               middleTop = bv_funcs::getPos(bv, beg, beg.boundary()).y_ + row1.descent();
+       
+       // clip below
+       int middleBottom;
+       bool const clipBelow = 
+               (bv_funcs::status(&bv, end) == bv_funcs::CUR_BELOW);
+       if (clipBelow)
+               middleBottom = bv.workHeight();
+       else
+               middleBottom = bv_funcs::getPos(bv, end, end.boundary()).y_ - row2.ascent();
 
-       bool const above = (bv_funcs::status(&bv, beg)
-                           == bv_funcs::CUR_ABOVE);
-       bool const below = (bv_funcs::status(&bv, end)
-                           == bv_funcs::CUR_BELOW);
-       int y1,y2,x1,x2;
-       if (above) {
-               y1 = 0;
-               y2 = 0;
-               x1 = 0;
-               x2 = tm.width();
-       } else {
-               Row const & row1 = pm1.getRow(beg.pos(), beg.boundary());
-               y1 = bv_funcs::getPos(bv, beg, beg.boundary()).y_ - row1.ascent();
-               y2 = y1 + row1.height();
-               int const startx = cursorX(bv, beg.top(), beg.boundary());
-               if (!isRTL(buffer, par1)) {
-                       x1 = startx;
-                       x2 = 0 + tm.width();
+       // start and end in the same line?
+       if (!(clipAbove || clipBelow) && &row1 == &row2)
+               // then only draw this row's selection
+               drawRowSelection(pi, x, row1, beg, end, false, false);
+       else {
+               if (!clipAbove) {
+                       // get row end
+                       DocIterator begRowEnd = beg;
+                       begRowEnd.pos() = row1.endpos();
+                       begRowEnd.boundary(true);
+                       
+                       // draw upper rectangle
+                       drawRowSelection(pi, x, row1, beg, begRowEnd, false, true);
                }
-               else {
-                       x1 = 0;
-                       x2 = startx;
+                       
+               if (middleTop < middleBottom) {
+                       // draw middle rectangle
+                       pi.pain.fillRectangle(x, middleTop, 
+                                                                                                               tm.width(), middleBottom - middleTop, 
+                                                                                                               Color::selection);
                }
-       }
 
-       int Y1,Y2,X1,X2;
-       if (below) {
-               Y1 = bv.workHeight();
-               Y2 = bv.workHeight();
-               X1 = 0;
-               X2 = tm.width();
-       } else {
-               Row const & row2 = pm2.getRow(end.pos(), end.boundary());
-               Y1 = bv_funcs::getPos(bv, end, end.boundary()).y_ - row2.ascent();
-               Y2 = Y1 + row2.height();
-               int const endx = cursorX(bv, end.top(), end.boundary());
-               if (!isRTL(buffer, par2)) {
-                       X1 = 0;
-                       X2 = endx;
-               }
-               else {
-                       X1 = endx;
-                       X2 = 0 + tm.width();
+               if (!clipBelow) {
+                       // get row begin
+                       DocIterator endRowBeg = end;
+                       endRowBeg.pos() = row2.pos();
+                       endRowBeg.boundary(false);
+                       
+                       // draw low rectangle
+                       drawRowSelection(pi, x, row2, endRowBeg, end, true, false);
                }
        }
+}
 
-       if (!above && !below && &pm1.getRow(beg.pos(), beg.boundary())
-           == &pm2.getRow(end.pos(), end.boundary()))
-       {
-               // paint only one rectangle
-               int const b( !isRTL(*bv.buffer(), par1) ? x + x1 : x + X1 );
-               int const w( !isRTL(*bv.buffer(), par1) ? X2 - x1 : x2 - X1 );
-               pi.pain.fillRectangle(b, y1, w, y2 - y1, Color::selection);
-               return;
-       }
-
-       LYXERR(Debug::DEBUG) << " y1: " << y1 << " y2: " << y2
-               << "X1:" << X1 << " x2: " << X2 << " wid: " << tm.width()
-               << endl;
 
-       // paint upper rectangle
-       pi.pain.fillRectangle(x + x1, y1, x2 - x1, y2 - y1,
-                                     Color::selection);
-       // paint bottom rectangle
-       pi.pain.fillRectangle(x + X1, Y1, X2 - X1, Y2 - Y1,
-                                     Color::selection);
-       // paint center rectangle
-       pi.pain.fillRectangle(x, y2, tm.width(),
-                             Y1 - y2, Color::selection);
+void Text::drawRowSelection(PainterInfo & pi, int x, Row const & row,
+                                                                                                               DocIterator const & beg, DocIterator const & end, 
+                                                                                                               bool drawOnBegMargin, bool drawOnEndMargin) const
+{
+       BufferView & bv = *pi.base.bv;
+       Buffer & buffer = bv.buffer();
+       TextMetrics const & tm = bv.textMetrics(this);
+       DocIterator cur = beg;
+       int x1 = cursorX(bv, beg.top(), beg.boundary());
+       int x2 = cursorX(bv, end.top(), end.boundary());
+       int y1 = bv_funcs::getPos(bv, cur, cur.boundary()).y_ - row.ascent();
+       int y2 = y1 + row.height();
+       
+       // draw the margins
+       if (drawOnBegMargin) {
+               if (isRTL(buffer, beg.paragraph()))
+                       pi.pain.fillRectangle(x + x1, y1, tm.width() - x1, y2 - y1, Color::selection);
+               else
+                       pi.pain.fillRectangle(x, y1, x1, y2 - y1, Color::selection);
+       }
+       
+       if (drawOnEndMargin) {
+               if (isRTL(buffer, beg.paragraph()))
+                       pi.pain.fillRectangle(x, y1, x2, y2 - y1, Color::selection);
+               else
+                       pi.pain.fillRectangle(x + x2, y1, tm.width() - x2, y2 - y1, Color::selection);
+       }
+       
+       // if we are on a boundary from the beginning, it's probably
+       // a RTL boundary and we jump to the other side directly as this
+       // segement is 0-size and confuses the logic below
+       if (cur.boundary())
+               cur.boundary(false);
+       
+       // go through row and draw from RTL boundary to RTL boundary
+       while (cur < end) {
+               bool drawNow = false;
+               
+               // simplified cursorRight code below which does not
+               // descend into insets and which does not go into the
+               // next line. Compare the logic with the original cursorRight
+               
+               // if left of boundary -> just jump to right side
+               // but for RTL boundaries don't, because: abc|DDEEFFghi -> abcDDEEF|Fghi
+               if (cur.boundary()) {
+                       cur.boundary(false);
+               }       else if (isRTLBoundary(buffer, cur.paragraph(), cur.pos() + 1)) {
+                       // in front of RTL boundary -> Stay on this side of the boundary because:
+                       //   ab|cDDEEFFghi -> abc|DDEEFFghi
+                       ++cur.pos();
+                       cur.boundary(true);
+                       drawNow = true;
+               } else {
+                       // move right
+                       ++cur.pos();
+                       
+                       // line end?
+                       if (cur.pos() == row.endpos())
+                               cur.boundary(true);
+               }
+                       
+               if (x1 == -1) {
+                       // the previous segment was just drawn, now the next starts
+                       x1 = cursorX(bv, cur.top(), cur.boundary());
+               }
+               
+               if (!(cur < end) || drawNow) {
+                       x2 = cursorX(bv, cur.top(), cur.boundary());
+                       pi.pain.fillRectangle(x + min(x1,x2), y1, abs(x2 - x1), y2 - y1,
+                                                                                                               Color::selection);
+                       
+                       // reset x1, so it is set again next round (which will be on the 
+                       // right side of a boundary or at the selection end)
+                       x1 = -1;
+               }
+       }
 }
 
 
+
 bool Text::isLastRow(pit_type pit, Row const & row) const
 {
        return row.endpos() >= pars_[pit].size()
@@ -1556,6 +1584,10 @@ void Text::write(Buffer const & buf, std::ostream & os) const
        depth_type dth = 0;
        for (; pit != end; ++pit)
                pit->write(buf, os, buf.params(), dth);
+
+       // Close begin_deeper
+       for(; dth > 0; --dth)
+               os << "\n\\end_deeper";
 }
 
 
@@ -1630,12 +1662,20 @@ int Text::cursorX(BufferView const & bv, CursorSlice const & sl,
 
        pos_type cursor_vpos = 0;
 
-       Buffer const & buffer = *bv.buffer();
+       Buffer const & buffer = bv.buffer();
        RowMetrics const m = tm.computeRowMetrics(pit, row);
        double x = m.x;
+       Bidi bidi;
+       bidi.computeTables(par, buffer, row);
 
        pos_type const row_pos  = row.pos();
        pos_type const end      = row.endpos();
+       // Spaces at logical line breaks in bidi text must be skipped during 
+       // cursor positioning. However, they may appear visually in the middle
+       // of a row; they must be skipped, wherever they are...
+       // * logically "abc_[HEBREW_\nHEBREW]"
+       // * visually "abc_[_WERBEH\nWERBEH]"
+       pos_type skipped_sep_vpos = -1;
 
        if (end <= row_pos)
                cursor_vpos = row_pos;
@@ -1658,17 +1698,23 @@ int Text::cursorX(BufferView const & bv, CursorSlice const & sl,
        // Use font span to speed things up, see below
        FontSpan font_span;
        Font font;
-       FontMetrics const & labelfm = theFontMetrics(
-               getLabelFont(buffer, par));
 
+       // If the last logical character is a separator, skip it, unless
+       // it's in the last row of a paragraph; see skipped_sep_vpos declaration
+       if (end > 0 && end < par.size() && par.isSeparator(end - 1))
+               skipped_sep_vpos = bidi.log2vis(end - 1);
+       
        for (pos_type vpos = row_pos; vpos < cursor_vpos; ++vpos) {
+               // Skip the separator which is at the logical end of the row
+               if (vpos == skipped_sep_vpos)
+                       continue;
                pos_type pos = bidi.vis2log(vpos);
                if (body_pos > 0 && pos == body_pos - 1) {
-                       // FIXME UNICODE
-                       docstring const lsep = from_utf8(par.layout()->labelsep);
-                       x += m.label_hfill + labelfm.width(lsep);
+                       FontMetrics const & labelfm = theFontMetrics(
+                               getLabelFont(buffer, par));
+                       x += m.label_hfill + labelfm.width(par.layout()->labelsep);
                        if (par.isLineSeparator(body_pos - 1))
-                               x -= singleWidth(buffer, par, body_pos - 1);
+                               x -= tm.singleWidth(pit, body_pos - 1);
                }
 
                // Use font span to speed things up, see above
@@ -1677,7 +1723,7 @@ int Text::cursorX(BufferView const & bv, CursorSlice const & sl,
                        font = getFont(buffer, par, pos);
                }
 
-               x += singleWidth(par, pos, par.getChar(pos), font);
+               x += pm.singleWidth(pos, font);
 
                if (par.hfillExpansion(row, pos))
                        x += (pos >= body_pos) ? m.hfill : m.label_hfill;
@@ -1686,20 +1732,13 @@ int Text::cursorX(BufferView const & bv, CursorSlice const & sl,
        }
 
        // see correction above
-       if (boundary_correction)
-               if (getFont(buffer, par, ppos).isVisibleRightToLeft())
-                       x -= singleWidth(buffer, par, ppos);
+       if (boundary_correction) {
+               if (isRTL(buffer, sl, boundary))
+                       x -= tm.singleWidth(pit, ppos);
                else
-                       x += singleWidth(buffer, par, ppos);
-
-       // Make sure inside an inset we always count from the left
-       // edge (bidi!) -- MV
-       if (sl.pos() < par.size()) {
-               font = getFont(buffer, par, sl.pos());
-               if (!boundary && font.isVisibleRightToLeft()
-                 && par.isInset(sl.pos()))
-                       x -= par.getInset(sl.pos())->width();
+                       x += tm.singleWidth(pit, ppos);
        }
+
        return int(x);
 }