| 1170 | |
| 1171 | // Ensure a given position is not inside an atomic range. |
| 1172 | function skipAtomic(doc, pos, bias, mayClear) { |
| 1173 | var flipped = false, curPos = pos; |
| 1174 | var dir = bias || 1; |
| 1175 | doc.cantEdit = false; |
| 1176 | search: for (;;) { |
| 1177 | var line = getLine(doc, curPos.line); |
| 1178 | if (line.markedSpans) { |
| 1179 | for (var i = 0; i < line.markedSpans.length; ++i) { |
| 1180 | var sp = line.markedSpans[i], m = sp.marker; |
| 1181 | if ((sp.from == null || (m.inclusiveLeft ? sp.from <= curPos.ch : sp.from < curPos.ch)) && |
| 1182 | (sp.to == null || (m.inclusiveRight ? sp.to >= curPos.ch : sp.to > curPos.ch))) { |
| 1183 | if (mayClear) { |
| 1184 | signal(m, "beforeCursorEnter"); |
| 1185 | if (m.explicitlyCleared) { |
| 1186 | if (!line.markedSpans) break; |
| 1187 | else {--i; continue;} |
| 1188 | } |
| 1189 | } |
| 1190 | if (!m.atomic) continue; |
| 1191 | var newPos = m.find(dir < 0 ? -1 : 1); |
| 1192 | if (cmp(newPos, curPos) == 0) { |
| 1193 | newPos.ch += dir; |
| 1194 | if (newPos.ch < 0) { |
| 1195 | if (newPos.line > doc.first) newPos = clipPos(doc, Pos(newPos.line - 1)); |
| 1196 | else newPos = null; |
| 1197 | } else if (newPos.ch > line.text.length) { |
| 1198 | if (newPos.line < doc.first + doc.size - 1) newPos = Pos(newPos.line + 1, 0); |
| 1199 | else newPos = null; |
| 1200 | } |
| 1201 | if (!newPos) { |
| 1202 | if (flipped) { |
| 1203 | // Driven in a corner -- no valid cursor position found at all |
| 1204 | // -- try again *with* clearing, if we didn't already |
| 1205 | if (!mayClear) return skipAtomic(doc, pos, bias, true); |
| 1206 | // Otherwise, turn off editing until further notice, and return the start of the doc |
| 1207 | doc.cantEdit = true; |
| 1208 | return Pos(doc.first, 0); |
| 1209 | } |
| 1210 | flipped = true; newPos = pos; dir = -dir; |
| 1211 | } |
| 1212 | } |
| 1213 | curPos = newPos; |
| 1214 | continue search; |
| 1215 | } |
| 1216 | } |
| 1217 | } |
| 1218 | return curPos; |
| 1219 | } |
| 1220 | } |
| 1221 | |
| 1222 | // SELECTION DRAWING |
| 1223 | |