code: function findWord(cm, lineNum, pos, dir, regexps) {
      var line = cm.getLine(lineNum);
      while (true) {
        var stop = (dir > 0) ? line.length : -1;
        var wordStart = stop, wordEnd = stop;
        // Find bounds of next word.
        for (; pos != stop; pos += dir) {
          for (var i = 0; i < regexps.length; ++i) {
            if (regexps[i].test(line.charAt(pos))) {
              wordStart = pos;
              // Advance to end of word.
              for (; pos != stop && regexps[i].test(line.charAt(pos)); pos += dir) {}
              wordEnd = (dir > 0) ? pos : pos + 1;
              return {
                  from: Math.min(wordStart, wordEnd),
                  to: Math.max(wordStart, wordEnd),
                  line: lineNum};
            }
          }
        }
        // Advance to next/prev line.
        lineNum += dir;
        if (!isLine(cm, lineNum)) return null;
        line = cm.getLine(lineNum);
        pos = (dir > 0) ? 0 : line.length;
      }
    }
docs: Finds a word on the given line, and continue searching the next line if it can't find one.