code: function clipPostChange(doc, change, pos) {
      if (!posLess(change.from, pos)) return clipPos(doc, pos);
      var diff = (change.text.length - 1) - (change.to.line - change.from.line);
      if (pos.line > change.to.line + diff) {
        var preLine = pos.line - diff, lastLine = doc.first + doc.size - 1;
        if (preLine > lastLine) return Pos(lastLine, getLine(doc, lastLine).text.length);
        return clipToLen(pos, getLine(doc, preLine).text.length);
      }
      if (pos.line == change.to.line + diff)
        return clipToLen(pos, lst(change.text).length + (change.text.length == 1 ? change.from.ch : 0) +
                         getLine(doc, change.to.line).text.length - change.to.ch);
      var inside = pos.line - change.from.line;
      return clipToLen(pos, change.text[inside].length + (inside ? 0 : change.from.ch));
    }
docs: Make sure a position will be valid after the given change.