+ if (opt.forward) {
+ if (opt.matchword && cur.pos() > 0) { // Skip word-characters if we are in the mid of a word
+ if (cur.inTexted()) {
+ Paragraph const & par = cur.paragraph();
+ int len_limit, new_pos;
+ if (cur.lastpos() < par.size())
+ len_limit = cur.lastpos();
+ else
+ len_limit = par.size();
+ for (new_pos = cur.pos() - 1; new_pos < len_limit; new_pos++) {
+ if (!isWordChar(par.getChar(new_pos)))
+ break;
+ }
+ if (new_pos > cur.pos())
+ cur.pos() = new_pos;
+ }
+ else if (cur.inMathed()) {
+ // Check if 'cur.pos()-1' and 'cur.pos()' both point to a letter,
+ // I am not sure, we should consider the selection
+ bool sel = bv->cursor().selection();
+ if (!sel && cur.pos() < cur.lastpos()) {
+ CursorSlice const & cs = cur.top();
+ MathData md = cs.cell();
+ int len = -1;
+ MathData::const_iterator it_end = md.end();
+ MathData md2;
+ // Start the check with one character before actual cursor position
+ for (MathData::const_iterator it = md.begin() + cs.pos() - 1;
+ it != it_end; ++it)
+ md2.push_back(*it);
+ docstring inp = asString(md2);
+ for (len = 0; (unsigned) len < inp.size() && len + cur.pos() <= cur.lastpos(); len++) {
+ if (!isWordChar(inp[len]))
+ break;
+ }
+ // len == 0 means previous char was a word separator
+ // len == 1 search starts with a word separator
+ // len == 2 ... we have to skip len -1 chars
+ if (len > 1)
+ cur.pos() = cur.pos() + len - 1;
+ }
+ }
+ opt.matchword = false;
+ }