]> git.lyx.org Git - lyx.git/blobdiff - src/Text.cpp
Fix bug http://bugzilla.lyx.org/show_bug.cgi?id=3580
[lyx.git] / src / Text.cpp
index 2a3b5509a9beb47823825a29105e75792dde6be5..c9bb7e3b563c5e0af33fdfab50455511c0b455ab 100644 (file)
@@ -59,7 +59,7 @@
 #include "insets/InsetSpecialChar.h"
 #include "insets/InsetTabular.h"
 
-#include "support/assert.h"
+#include "support/lassert.h"
 #include "support/convert.h"
 #include "support/debug.h"
 #include "support/docstream.h"
@@ -584,20 +584,27 @@ bool Text::cursorForwardOneWord(Cursor & cur)
 {
        LASSERT(this == cur.text(), /**/);
 
-       Cursor old = cur;
+       pos_type const lastpos = cur.lastpos();
+       pit_type pit = cur.pit();
+       pos_type pos = cur.pos();
+       Paragraph const & par = cur.paragraph();
 
-       if (old.pos() == old.lastpos() && old.pit() != old.lastpit()) {
-               ++old.pit();
-               old.pos() = 0;
-       } else {
-               // Advance through word.
-               while (old.pos() != old.lastpos() && old.paragraph().isLetter(old.pos()))
-                       ++old.pos();
-               // Skip through trailing nonword stuff.
-               while (old.pos() != old.lastpos() && !old.paragraph().isLetter(old.pos()))
-                       ++old.pos();
+       // Skip over either a non-char inset or a full word
+       if (pos != lastpos && !par.isLetter(pos) && !par.isChar(pos))
+               ++pos;
+       else while (pos != lastpos && par.isLetter(pos))
+                       ++pos;
+
+       // Skip through trailing punctuation and spaces.
+       while (pos != lastpos && par.isChar(pos))
+               ++pos;
+
+       if (pos == lastpos && pit != cur.lastpit()) {
+               ++pit;
+               pos = 0;
        }
-       return setCursor(cur, old.pit(), old.pos());
+
+       return setCursor(cur, pit, pos);
 }
 
 
@@ -605,20 +612,104 @@ bool Text::cursorBackwardOneWord(Cursor & cur)
 {
        LASSERT(this == cur.text(), /**/);
 
-       Cursor old = cur;
+       pit_type pit = cur.pit();
+       pos_type pos = cur.pos();
+       Paragraph & par = cur.paragraph();
 
-       if (old.pos() == 0 && old.pit() != 0) {
-               --old.pit();
-               old.pos() = old.lastpos();
-       } else {
-               // Skip through initial nonword stuff.
-               while (old.pos() != 0 && !old.paragraph().isLetter(old.pos() - 1))
-                       --old.pos();
-               // Advance through word.
-               while (old.pos() != 0 && old.paragraph().isLetter(old.pos() - 1))
-                       --old.pos();
+       // Skip through puctuation and spaces.
+       while (pos != 0 && par.isChar(pos - 1))
+               --pos;
+
+       // Skip over either a non-char inset or a full word
+       if (pos != 0 && !par.isLetter(pos) && !par.isChar(pos - 1))
+               --pos;
+       else while (pos != 0 && par.isLetter(pos - 1))
+                       --pos;
+
+       if (pos == 0 && pit != 0) {
+               --pit;
+               pos = getPar(cur.pit() - 1).size();
+       }
+
+       return setCursor(cur, pit, pos);
+}
+
+
+bool Text::cursorVisLeftOneWord(Cursor & cur)
+{
+       LASSERT(this == cur.text(), /**/);
+
+       pos_type left_pos, right_pos;
+       bool left_is_letter, right_is_letter;
+
+       Cursor temp_cur = cur;
+
+       // always try to move at least once...
+       while (temp_cur.posVisLeft(true /* skip_inset */)) {
+
+               // collect some information about current cursor position
+               temp_cur.getSurroundingPos(left_pos, right_pos);
+               left_is_letter = 
+                       (left_pos > -1 ? temp_cur.paragraph().isLetter(left_pos) : false);
+               right_is_letter = 
+                       (right_pos > -1 ? temp_cur.paragraph().isLetter(right_pos) : false);
+
+               // if we're not at a letter/non-letter boundary, continue moving
+               if (left_is_letter == right_is_letter)
+                       continue;
+
+               // we should stop when we have an LTR word on our right or an RTL word
+               // on our left
+               if ((left_is_letter && temp_cur.paragraph().getFontSettings(
+                               temp_cur.bv().buffer().params(), 
+                               left_pos).isRightToLeft())
+                       || (right_is_letter && !temp_cur.paragraph().getFontSettings(
+                               temp_cur.bv().buffer().params(), 
+                               right_pos).isRightToLeft()))
+                       break;
+       }
+
+       return setCursor(cur, temp_cur.pit(), temp_cur.pos(), 
+                                        true, temp_cur.boundary());
+}
+
+
+bool Text::cursorVisRightOneWord(Cursor & cur)
+{
+       LASSERT(this == cur.text(), /**/);
+
+       pos_type left_pos, right_pos;
+       bool left_is_letter, right_is_letter;
+
+       Cursor temp_cur = cur;
+
+       // always try to move at least once...
+       while (temp_cur.posVisRight(true /* skip_inset */)) {
+
+               // collect some information about current cursor position
+               temp_cur.getSurroundingPos(left_pos, right_pos);
+               left_is_letter = 
+                       (left_pos > -1 ? temp_cur.paragraph().isLetter(left_pos) : false);
+               right_is_letter = 
+                       (right_pos > -1 ? temp_cur.paragraph().isLetter(right_pos) : false);
+
+               // if we're not at a letter/non-letter boundary, continue moving
+               if (left_is_letter == right_is_letter)
+                       continue;
+
+               // we should stop when we have an LTR word on our right or an RTL word
+               // on our left
+               if ((left_is_letter && temp_cur.paragraph().getFontSettings(
+                               temp_cur.bv().buffer().params(), 
+                               left_pos).isRightToLeft())
+                       || (right_is_letter && !temp_cur.paragraph().getFontSettings(
+                               temp_cur.bv().buffer().params(), 
+                               right_pos).isRightToLeft()))
+                       break;
        }
-       return setCursor(cur, old.pit(), old.pos());
+
+       return setCursor(cur, temp_cur.pit(), temp_cur.pos(), 
+                                        true, temp_cur.boundary());
 }
 
 
@@ -1299,7 +1390,7 @@ docstring Text::getPossibleLabel(Cursor & cur) const
        Layout const * layout = &(pars_[pit].layout());
 
        docstring text;
-       docstring par_text = pars_[pit].asString(false);
+       docstring par_text = pars_[pit].asString();
        string piece;
        // the return string of math matrices might contain linebreaks
        par_text = subst(par_text, '\n', '-');
@@ -1449,7 +1540,7 @@ docstring Text::previousWord(CursorSlice const & sl) const
                return docstring();
        
        Paragraph const & par = sl.paragraph();
-       return par.asString(from.pos(), to.pos(), false);
+       return par.asString(from.pos(), to.pos());
 }