codemirror.js (400161B)
1 // CodeMirror, copyright (c) by Marijn Haverbeke and others 2 // Distributed under an MIT license: https://codemirror.net/LICENSE 3 4 // This is CodeMirror (https://codemirror.net), a code editor 5 // implemented in JavaScript on top of the browser's DOM. 6 // 7 // You can find some technical background for some of the code below 8 // at http://marijnhaverbeke.nl/blog/#cm-internals . 9 10 (function (global, factory) { 11 typeof exports === 'object' && typeof module !== 'undefined' ? module.exports = factory() : 12 typeof define === 'function' && define.amd ? define(factory) : 13 (global = global || self, global.CodeMirror = factory()); 14 }(this, (function () { 'use strict'; 15 16 // Kludges for bugs and behavior differences that can't be feature 17 // detected are enabled based on userAgent etc sniffing. 18 var userAgent = navigator.userAgent; 19 var platform = navigator.platform; 20 21 var gecko = /gecko\/\d/i.test(userAgent); 22 var ie_upto10 = /MSIE \d/.test(userAgent); 23 var ie_11up = /Trident\/(?:[7-9]|\d{2,})\..*rv:(\d+)/.exec(userAgent); 24 var edge = /Edge\/(\d+)/.exec(userAgent); 25 var ie = ie_upto10 || ie_11up || edge; 26 var ie_version = ie && (ie_upto10 ? document.documentMode || 6 : +(edge || ie_11up)[1]); 27 var webkit = !edge && /WebKit\//.test(userAgent); 28 var qtwebkit = webkit && /Qt\/\d+\.\d+/.test(userAgent); 29 var chrome = !edge && /Chrome\//.test(userAgent); 30 var presto = /Opera\//.test(userAgent); 31 var safari = /Apple Computer/.test(navigator.vendor); 32 var mac_geMountainLion = /Mac OS X 1\d\D([8-9]|\d\d)\D/.test(userAgent); 33 var phantom = /PhantomJS/.test(userAgent); 34 35 var ios = safari && (/Mobile\/\w+/.test(userAgent) || navigator.maxTouchPoints > 2); 36 var android = /Android/.test(userAgent); 37 // This is woefully incomplete. Suggestions for alternative methods welcome. 38 var mobile = ios || android || /webOS|BlackBerry|Opera Mini|Opera Mobi|IEMobile/i.test(userAgent); 39 var mac = ios || /Mac/.test(platform); 40 var chromeOS = /\bCrOS\b/.test(userAgent); 41 var windows = /win/i.test(platform); 42 43 var presto_version = presto && userAgent.match(/Version\/(\d*\.\d*)/); 44 if (presto_version) { presto_version = Number(presto_version[1]); } 45 if (presto_version && presto_version >= 15) { presto = false; webkit = true; } 46 // Some browsers use the wrong event properties to signal cmd/ctrl on OS X 47 var flipCtrlCmd = mac && (qtwebkit || presto && (presto_version == null || presto_version < 12.11)); 48 var captureRightClick = gecko || (ie && ie_version >= 9); 49 50 function classTest(cls) { return new RegExp("(^|\\s)" + cls + "(?:$|\\s)\\s*") } 51 52 var rmClass = function(node, cls) { 53 var current = node.className; 54 var match = classTest(cls).exec(current); 55 if (match) { 56 var after = current.slice(match.index + match[0].length); 57 node.className = current.slice(0, match.index) + (after ? match[1] + after : ""); 58 } 59 }; 60 61 function removeChildren(e) { 62 for (var count = e.childNodes.length; count > 0; --count) 63 { e.removeChild(e.firstChild); } 64 return e 65 } 66 67 function removeChildrenAndAdd(parent, e) { 68 return removeChildren(parent).appendChild(e) 69 } 70 71 function elt(tag, content, className, style) { 72 var e = document.createElement(tag); 73 if (className) { e.className = className; } 74 if (style) { e.style.cssText = style; } 75 if (typeof content == "string") { e.appendChild(document.createTextNode(content)); } 76 else if (content) { for (var i = 0; i < content.length; ++i) { e.appendChild(content[i]); } } 77 return e 78 } 79 // wrapper for elt, which removes the elt from the accessibility tree 80 function eltP(tag, content, className, style) { 81 var e = elt(tag, content, className, style); 82 e.setAttribute("role", "presentation"); 83 return e 84 } 85 86 var range; 87 if (document.createRange) { range = function(node, start, end, endNode) { 88 var r = document.createRange(); 89 r.setEnd(endNode || node, end); 90 r.setStart(node, start); 91 return r 92 }; } 93 else { range = function(node, start, end) { 94 var r = document.body.createTextRange(); 95 try { r.moveToElementText(node.parentNode); } 96 catch(e) { return r } 97 r.collapse(true); 98 r.moveEnd("character", end); 99 r.moveStart("character", start); 100 return r 101 }; } 102 103 function contains(parent, child) { 104 if (child.nodeType == 3) // Android browser always returns false when child is a textnode 105 { child = child.parentNode; } 106 if (parent.contains) 107 { return parent.contains(child) } 108 do { 109 if (child.nodeType == 11) { child = child.host; } 110 if (child == parent) { return true } 111 } while (child = child.parentNode) 112 } 113 114 function activeElt() { 115 // IE and Edge may throw an "Unspecified Error" when accessing document.activeElement. 116 // IE < 10 will throw when accessed while the page is loading or in an iframe. 117 // IE > 9 and Edge will throw when accessed in an iframe if document.body is unavailable. 118 var activeElement; 119 try { 120 activeElement = document.activeElement; 121 } catch(e) { 122 activeElement = document.body || null; 123 } 124 while (activeElement && activeElement.shadowRoot && activeElement.shadowRoot.activeElement) 125 { activeElement = activeElement.shadowRoot.activeElement; } 126 return activeElement 127 } 128 129 function addClass(node, cls) { 130 var current = node.className; 131 if (!classTest(cls).test(current)) { node.className += (current ? " " : "") + cls; } 132 } 133 function joinClasses(a, b) { 134 var as = a.split(" "); 135 for (var i = 0; i < as.length; i++) 136 { if (as[i] && !classTest(as[i]).test(b)) { b += " " + as[i]; } } 137 return b 138 } 139 140 var selectInput = function(node) { node.select(); }; 141 if (ios) // Mobile Safari apparently has a bug where select() is broken. 142 { selectInput = function(node) { node.selectionStart = 0; node.selectionEnd = node.value.length; }; } 143 else if (ie) // Suppress mysterious IE10 errors 144 { selectInput = function(node) { try { node.select(); } catch(_e) {} }; } 145 146 function bind(f) { 147 var args = Array.prototype.slice.call(arguments, 1); 148 return function(){return f.apply(null, args)} 149 } 150 151 function copyObj(obj, target, overwrite) { 152 if (!target) { target = {}; } 153 for (var prop in obj) 154 { if (obj.hasOwnProperty(prop) && (overwrite !== false || !target.hasOwnProperty(prop))) 155 { target[prop] = obj[prop]; } } 156 return target 157 } 158 159 // Counts the column offset in a string, taking tabs into account. 160 // Used mostly to find indentation. 161 function countColumn(string, end, tabSize, startIndex, startValue) { 162 if (end == null) { 163 end = string.search(/[^\s\u00a0]/); 164 if (end == -1) { end = string.length; } 165 } 166 for (var i = startIndex || 0, n = startValue || 0;;) { 167 var nextTab = string.indexOf("\t", i); 168 if (nextTab < 0 || nextTab >= end) 169 { return n + (end - i) } 170 n += nextTab - i; 171 n += tabSize - (n % tabSize); 172 i = nextTab + 1; 173 } 174 } 175 176 var Delayed = function() { 177 this.id = null; 178 this.f = null; 179 this.time = 0; 180 this.handler = bind(this.onTimeout, this); 181 }; 182 Delayed.prototype.onTimeout = function (self) { 183 self.id = 0; 184 if (self.time <= +new Date) { 185 self.f(); 186 } else { 187 setTimeout(self.handler, self.time - +new Date); 188 } 189 }; 190 Delayed.prototype.set = function (ms, f) { 191 this.f = f; 192 var time = +new Date + ms; 193 if (!this.id || time < this.time) { 194 clearTimeout(this.id); 195 this.id = setTimeout(this.handler, ms); 196 this.time = time; 197 } 198 }; 199 200 function indexOf(array, elt) { 201 for (var i = 0; i < array.length; ++i) 202 { if (array[i] == elt) { return i } } 203 return -1 204 } 205 206 // Number of pixels added to scroller and sizer to hide scrollbar 207 var scrollerGap = 50; 208 209 // Returned or thrown by various protocols to signal 'I'm not 210 // handling this'. 211 var Pass = {toString: function(){return "CodeMirror.Pass"}}; 212 213 // Reused option objects for setSelection & friends 214 var sel_dontScroll = {scroll: false}, sel_mouse = {origin: "*mouse"}, sel_move = {origin: "+move"}; 215 216 // The inverse of countColumn -- find the offset that corresponds to 217 // a particular column. 218 function findColumn(string, goal, tabSize) { 219 for (var pos = 0, col = 0;;) { 220 var nextTab = string.indexOf("\t", pos); 221 if (nextTab == -1) { nextTab = string.length; } 222 var skipped = nextTab - pos; 223 if (nextTab == string.length || col + skipped >= goal) 224 { return pos + Math.min(skipped, goal - col) } 225 col += nextTab - pos; 226 col += tabSize - (col % tabSize); 227 pos = nextTab + 1; 228 if (col >= goal) { return pos } 229 } 230 } 231 232 var spaceStrs = [""]; 233 function spaceStr(n) { 234 while (spaceStrs.length <= n) 235 { spaceStrs.push(lst(spaceStrs) + " "); } 236 return spaceStrs[n] 237 } 238 239 function lst(arr) { return arr[arr.length-1] } 240 241 function map(array, f) { 242 var out = []; 243 for (var i = 0; i < array.length; i++) { out[i] = f(array[i], i); } 244 return out 245 } 246 247 function insertSorted(array, value, score) { 248 var pos = 0, priority = score(value); 249 while (pos < array.length && score(array[pos]) <= priority) { pos++; } 250 array.splice(pos, 0, value); 251 } 252 253 function nothing() {} 254 255 function createObj(base, props) { 256 var inst; 257 if (Object.create) { 258 inst = Object.create(base); 259 } else { 260 nothing.prototype = base; 261 inst = new nothing(); 262 } 263 if (props) { copyObj(props, inst); } 264 return inst 265 } 266 267 var nonASCIISingleCaseWordChar = /[\u00df\u0587\u0590-\u05f4\u0600-\u06ff\u3040-\u309f\u30a0-\u30ff\u3400-\u4db5\u4e00-\u9fcc\uac00-\ud7af]/; 268 function isWordCharBasic(ch) { 269 return /\w/.test(ch) || ch > "\x80" && 270 (ch.toUpperCase() != ch.toLowerCase() || nonASCIISingleCaseWordChar.test(ch)) 271 } 272 function isWordChar(ch, helper) { 273 if (!helper) { return isWordCharBasic(ch) } 274 if (helper.source.indexOf("\\w") > -1 && isWordCharBasic(ch)) { return true } 275 return helper.test(ch) 276 } 277 278 function isEmpty(obj) { 279 for (var n in obj) { if (obj.hasOwnProperty(n) && obj[n]) { return false } } 280 return true 281 } 282 283 // Extending unicode characters. A series of a non-extending char + 284 // any number of extending chars is treated as a single unit as far 285 // as editing and measuring is concerned. This is not fully correct, 286 // since some scripts/fonts/browsers also treat other configurations 287 // of code points as a group. 288 var extendingChars = /[\u0300-\u036f\u0483-\u0489\u0591-\u05bd\u05bf\u05c1\u05c2\u05c4\u05c5\u05c7\u0610-\u061a\u064b-\u065e\u0670\u06d6-\u06dc\u06de-\u06e4\u06e7\u06e8\u06ea-\u06ed\u0711\u0730-\u074a\u07a6-\u07b0\u07eb-\u07f3\u0816-\u0819\u081b-\u0823\u0825-\u0827\u0829-\u082d\u0900-\u0902\u093c\u0941-\u0948\u094d\u0951-\u0955\u0962\u0963\u0981\u09bc\u09be\u09c1-\u09c4\u09cd\u09d7\u09e2\u09e3\u0a01\u0a02\u0a3c\u0a41\u0a42\u0a47\u0a48\u0a4b-\u0a4d\u0a51\u0a70\u0a71\u0a75\u0a81\u0a82\u0abc\u0ac1-\u0ac5\u0ac7\u0ac8\u0acd\u0ae2\u0ae3\u0b01\u0b3c\u0b3e\u0b3f\u0b41-\u0b44\u0b4d\u0b56\u0b57\u0b62\u0b63\u0b82\u0bbe\u0bc0\u0bcd\u0bd7\u0c3e-\u0c40\u0c46-\u0c48\u0c4a-\u0c4d\u0c55\u0c56\u0c62\u0c63\u0cbc\u0cbf\u0cc2\u0cc6\u0ccc\u0ccd\u0cd5\u0cd6\u0ce2\u0ce3\u0d3e\u0d41-\u0d44\u0d4d\u0d57\u0d62\u0d63\u0dca\u0dcf\u0dd2-\u0dd4\u0dd6\u0ddf\u0e31\u0e34-\u0e3a\u0e47-\u0e4e\u0eb1\u0eb4-\u0eb9\u0ebb\u0ebc\u0ec8-\u0ecd\u0f18\u0f19\u0f35\u0f37\u0f39\u0f71-\u0f7e\u0f80-\u0f84\u0f86\u0f87\u0f90-\u0f97\u0f99-\u0fbc\u0fc6\u102d-\u1030\u1032-\u1037\u1039\u103a\u103d\u103e\u1058\u1059\u105e-\u1060\u1071-\u1074\u1082\u1085\u1086\u108d\u109d\u135f\u1712-\u1714\u1732-\u1734\u1752\u1753\u1772\u1773\u17b7-\u17bd\u17c6\u17c9-\u17d3\u17dd\u180b-\u180d\u18a9\u1920-\u1922\u1927\u1928\u1932\u1939-\u193b\u1a17\u1a18\u1a56\u1a58-\u1a5e\u1a60\u1a62\u1a65-\u1a6c\u1a73-\u1a7c\u1a7f\u1b00-\u1b03\u1b34\u1b36-\u1b3a\u1b3c\u1b42\u1b6b-\u1b73\u1b80\u1b81\u1ba2-\u1ba5\u1ba8\u1ba9\u1c2c-\u1c33\u1c36\u1c37\u1cd0-\u1cd2\u1cd4-\u1ce0\u1ce2-\u1ce8\u1ced\u1dc0-\u1de6\u1dfd-\u1dff\u200c\u200d\u20d0-\u20f0\u2cef-\u2cf1\u2de0-\u2dff\u302a-\u302f\u3099\u309a\ua66f-\ua672\ua67c\ua67d\ua6f0\ua6f1\ua802\ua806\ua80b\ua825\ua826\ua8c4\ua8e0-\ua8f1\ua926-\ua92d\ua947-\ua951\ua980-\ua982\ua9b3\ua9b6-\ua9b9\ua9bc\uaa29-\uaa2e\uaa31\uaa32\uaa35\uaa36\uaa43\uaa4c\uaab0\uaab2-\uaab4\uaab7\uaab8\uaabe\uaabf\uaac1\uabe5\uabe8\uabed\udc00-\udfff\ufb1e\ufe00-\ufe0f\ufe20-\ufe26\uff9e\uff9f]/; 289 function isExtendingChar(ch) { return ch.charCodeAt(0) >= 768 && extendingChars.test(ch) } 290 291 // Returns a number from the range [`0`; `str.length`] unless `pos` is outside that range. 292 function skipExtendingChars(str, pos, dir) { 293 while ((dir < 0 ? pos > 0 : pos < str.length) && isExtendingChar(str.charAt(pos))) { pos += dir; } 294 return pos 295 } 296 297 // Returns the value from the range [`from`; `to`] that satisfies 298 // `pred` and is closest to `from`. Assumes that at least `to` 299 // satisfies `pred`. Supports `from` being greater than `to`. 300 function findFirst(pred, from, to) { 301 // At any point we are certain `to` satisfies `pred`, don't know 302 // whether `from` does. 303 var dir = from > to ? -1 : 1; 304 for (;;) { 305 if (from == to) { return from } 306 var midF = (from + to) / 2, mid = dir < 0 ? Math.ceil(midF) : Math.floor(midF); 307 if (mid == from) { return pred(mid) ? from : to } 308 if (pred(mid)) { to = mid; } 309 else { from = mid + dir; } 310 } 311 } 312 313 // BIDI HELPERS 314 315 function iterateBidiSections(order, from, to, f) { 316 if (!order) { return f(from, to, "ltr", 0) } 317 var found = false; 318 for (var i = 0; i < order.length; ++i) { 319 var part = order[i]; 320 if (part.from < to && part.to > from || from == to && part.to == from) { 321 f(Math.max(part.from, from), Math.min(part.to, to), part.level == 1 ? "rtl" : "ltr", i); 322 found = true; 323 } 324 } 325 if (!found) { f(from, to, "ltr"); } 326 } 327 328 var bidiOther = null; 329 function getBidiPartAt(order, ch, sticky) { 330 var found; 331 bidiOther = null; 332 for (var i = 0; i < order.length; ++i) { 333 var cur = order[i]; 334 if (cur.from < ch && cur.to > ch) { return i } 335 if (cur.to == ch) { 336 if (cur.from != cur.to && sticky == "before") { found = i; } 337 else { bidiOther = i; } 338 } 339 if (cur.from == ch) { 340 if (cur.from != cur.to && sticky != "before") { found = i; } 341 else { bidiOther = i; } 342 } 343 } 344 return found != null ? found : bidiOther 345 } 346 347 // Bidirectional ordering algorithm 348 // See http://unicode.org/reports/tr9/tr9-13.html for the algorithm 349 // that this (partially) implements. 350 351 // One-char codes used for character types: 352 // L (L): Left-to-Right 353 // R (R): Right-to-Left 354 // r (AL): Right-to-Left Arabic 355 // 1 (EN): European Number 356 // + (ES): European Number Separator 357 // % (ET): European Number Terminator 358 // n (AN): Arabic Number 359 // , (CS): Common Number Separator 360 // m (NSM): Non-Spacing Mark 361 // b (BN): Boundary Neutral 362 // s (B): Paragraph Separator 363 // t (S): Segment Separator 364 // w (WS): Whitespace 365 // N (ON): Other Neutrals 366 367 // Returns null if characters are ordered as they appear 368 // (left-to-right), or an array of sections ({from, to, level} 369 // objects) in the order in which they occur visually. 370 var bidiOrdering = (function() { 371 // Character types for codepoints 0 to 0xff 372 var lowTypes = "bbbbbbbbbtstwsbbbbbbbbbbbbbbssstwNN%%%NNNNNN,N,N1111111111NNNNNNNLLLLLLLLLLLLLLLLLLLLLLLLLLNNNNNNLLLLLLLLLLLLLLLLLLLLLLLLLLNNNNbbbbbbsbbbbbbbbbbbbbbbbbbbbbbbbbb,N%%%%NNNNLNNNNN%%11NLNNN1LNNNNNLLLLLLLLLLLLLLLLLLLLLLLNLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLN"; 373 // Character types for codepoints 0x600 to 0x6f9 374 var arabicTypes = "nnnnnnNNr%%r,rNNmmmmmmmmmmmrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrmmmmmmmmmmmmmmmmmmmmmnnnnnnnnnn%nnrrrmrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrmmmmmmmnNmmmmmmrrmmNmmmmrr1111111111"; 375 function charType(code) { 376 if (code <= 0xf7) { return lowTypes.charAt(code) } 377 else if (0x590 <= code && code <= 0x5f4) { return "R" } 378 else if (0x600 <= code && code <= 0x6f9) { return arabicTypes.charAt(code - 0x600) } 379 else if (0x6ee <= code && code <= 0x8ac) { return "r" } 380 else if (0x2000 <= code && code <= 0x200b) { return "w" } 381 else if (code == 0x200c) { return "b" } 382 else { return "L" } 383 } 384 385 var bidiRE = /[\u0590-\u05f4\u0600-\u06ff\u0700-\u08ac]/; 386 var isNeutral = /[stwN]/, isStrong = /[LRr]/, countsAsLeft = /[Lb1n]/, countsAsNum = /[1n]/; 387 388 function BidiSpan(level, from, to) { 389 this.level = level; 390 this.from = from; this.to = to; 391 } 392 393 return function(str, direction) { 394 var outerType = direction == "ltr" ? "L" : "R"; 395 396 if (str.length == 0 || direction == "ltr" && !bidiRE.test(str)) { return false } 397 var len = str.length, types = []; 398 for (var i = 0; i < len; ++i) 399 { types.push(charType(str.charCodeAt(i))); } 400 401 // W1. Examine each non-spacing mark (NSM) in the level run, and 402 // change the type of the NSM to the type of the previous 403 // character. If the NSM is at the start of the level run, it will 404 // get the type of sor. 405 for (var i$1 = 0, prev = outerType; i$1 < len; ++i$1) { 406 var type = types[i$1]; 407 if (type == "m") { types[i$1] = prev; } 408 else { prev = type; } 409 } 410 411 // W2. Search backwards from each instance of a European number 412 // until the first strong type (R, L, AL, or sor) is found. If an 413 // AL is found, change the type of the European number to Arabic 414 // number. 415 // W3. Change all ALs to R. 416 for (var i$2 = 0, cur = outerType; i$2 < len; ++i$2) { 417 var type$1 = types[i$2]; 418 if (type$1 == "1" && cur == "r") { types[i$2] = "n"; } 419 else if (isStrong.test(type$1)) { cur = type$1; if (type$1 == "r") { types[i$2] = "R"; } } 420 } 421 422 // W4. A single European separator between two European numbers 423 // changes to a European number. A single common separator between 424 // two numbers of the same type changes to that type. 425 for (var i$3 = 1, prev$1 = types[0]; i$3 < len - 1; ++i$3) { 426 var type$2 = types[i$3]; 427 if (type$2 == "+" && prev$1 == "1" && types[i$3+1] == "1") { types[i$3] = "1"; } 428 else if (type$2 == "," && prev$1 == types[i$3+1] && 429 (prev$1 == "1" || prev$1 == "n")) { types[i$3] = prev$1; } 430 prev$1 = type$2; 431 } 432 433 // W5. A sequence of European terminators adjacent to European 434 // numbers changes to all European numbers. 435 // W6. Otherwise, separators and terminators change to Other 436 // Neutral. 437 for (var i$4 = 0; i$4 < len; ++i$4) { 438 var type$3 = types[i$4]; 439 if (type$3 == ",") { types[i$4] = "N"; } 440 else if (type$3 == "%") { 441 var end = (void 0); 442 for (end = i$4 + 1; end < len && types[end] == "%"; ++end) {} 443 var replace = (i$4 && types[i$4-1] == "!") || (end < len && types[end] == "1") ? "1" : "N"; 444 for (var j = i$4; j < end; ++j) { types[j] = replace; } 445 i$4 = end - 1; 446 } 447 } 448 449 // W7. Search backwards from each instance of a European number 450 // until the first strong type (R, L, or sor) is found. If an L is 451 // found, then change the type of the European number to L. 452 for (var i$5 = 0, cur$1 = outerType; i$5 < len; ++i$5) { 453 var type$4 = types[i$5]; 454 if (cur$1 == "L" && type$4 == "1") { types[i$5] = "L"; } 455 else if (isStrong.test(type$4)) { cur$1 = type$4; } 456 } 457 458 // N1. A sequence of neutrals takes the direction of the 459 // surrounding strong text if the text on both sides has the same 460 // direction. European and Arabic numbers act as if they were R in 461 // terms of their influence on neutrals. Start-of-level-run (sor) 462 // and end-of-level-run (eor) are used at level run boundaries. 463 // N2. Any remaining neutrals take the embedding direction. 464 for (var i$6 = 0; i$6 < len; ++i$6) { 465 if (isNeutral.test(types[i$6])) { 466 var end$1 = (void 0); 467 for (end$1 = i$6 + 1; end$1 < len && isNeutral.test(types[end$1]); ++end$1) {} 468 var before = (i$6 ? types[i$6-1] : outerType) == "L"; 469 var after = (end$1 < len ? types[end$1] : outerType) == "L"; 470 var replace$1 = before == after ? (before ? "L" : "R") : outerType; 471 for (var j$1 = i$6; j$1 < end$1; ++j$1) { types[j$1] = replace$1; } 472 i$6 = end$1 - 1; 473 } 474 } 475 476 // Here we depart from the documented algorithm, in order to avoid 477 // building up an actual levels array. Since there are only three 478 // levels (0, 1, 2) in an implementation that doesn't take 479 // explicit embedding into account, we can build up the order on 480 // the fly, without following the level-based algorithm. 481 var order = [], m; 482 for (var i$7 = 0; i$7 < len;) { 483 if (countsAsLeft.test(types[i$7])) { 484 var start = i$7; 485 for (++i$7; i$7 < len && countsAsLeft.test(types[i$7]); ++i$7) {} 486 order.push(new BidiSpan(0, start, i$7)); 487 } else { 488 var pos = i$7, at = order.length, isRTL = direction == "rtl" ? 1 : 0; 489 for (++i$7; i$7 < len && types[i$7] != "L"; ++i$7) {} 490 for (var j$2 = pos; j$2 < i$7;) { 491 if (countsAsNum.test(types[j$2])) { 492 if (pos < j$2) { order.splice(at, 0, new BidiSpan(1, pos, j$2)); at += isRTL; } 493 var nstart = j$2; 494 for (++j$2; j$2 < i$7 && countsAsNum.test(types[j$2]); ++j$2) {} 495 order.splice(at, 0, new BidiSpan(2, nstart, j$2)); 496 at += isRTL; 497 pos = j$2; 498 } else { ++j$2; } 499 } 500 if (pos < i$7) { order.splice(at, 0, new BidiSpan(1, pos, i$7)); } 501 } 502 } 503 if (direction == "ltr") { 504 if (order[0].level == 1 && (m = str.match(/^\s+/))) { 505 order[0].from = m[0].length; 506 order.unshift(new BidiSpan(0, 0, m[0].length)); 507 } 508 if (lst(order).level == 1 && (m = str.match(/\s+$/))) { 509 lst(order).to -= m[0].length; 510 order.push(new BidiSpan(0, len - m[0].length, len)); 511 } 512 } 513 514 return direction == "rtl" ? order.reverse() : order 515 } 516 })(); 517 518 // Get the bidi ordering for the given line (and cache it). Returns 519 // false for lines that are fully left-to-right, and an array of 520 // BidiSpan objects otherwise. 521 function getOrder(line, direction) { 522 var order = line.order; 523 if (order == null) { order = line.order = bidiOrdering(line.text, direction); } 524 return order 525 } 526 527 // EVENT HANDLING 528 529 // Lightweight event framework. on/off also work on DOM nodes, 530 // registering native DOM handlers. 531 532 var noHandlers = []; 533 534 var on = function(emitter, type, f) { 535 if (emitter.addEventListener) { 536 emitter.addEventListener(type, f, false); 537 } else if (emitter.attachEvent) { 538 emitter.attachEvent("on" + type, f); 539 } else { 540 var map = emitter._handlers || (emitter._handlers = {}); 541 map[type] = (map[type] || noHandlers).concat(f); 542 } 543 }; 544 545 function getHandlers(emitter, type) { 546 return emitter._handlers && emitter._handlers[type] || noHandlers 547 } 548 549 function off(emitter, type, f) { 550 if (emitter.removeEventListener) { 551 emitter.removeEventListener(type, f, false); 552 } else if (emitter.detachEvent) { 553 emitter.detachEvent("on" + type, f); 554 } else { 555 var map = emitter._handlers, arr = map && map[type]; 556 if (arr) { 557 var index = indexOf(arr, f); 558 if (index > -1) 559 { map[type] = arr.slice(0, index).concat(arr.slice(index + 1)); } 560 } 561 } 562 } 563 564 function signal(emitter, type /*, values...*/) { 565 var handlers = getHandlers(emitter, type); 566 if (!handlers.length) { return } 567 var args = Array.prototype.slice.call(arguments, 2); 568 for (var i = 0; i < handlers.length; ++i) { handlers[i].apply(null, args); } 569 } 570 571 // The DOM events that CodeMirror handles can be overridden by 572 // registering a (non-DOM) handler on the editor for the event name, 573 // and preventDefault-ing the event in that handler. 574 function signalDOMEvent(cm, e, override) { 575 if (typeof e == "string") 576 { e = {type: e, preventDefault: function() { this.defaultPrevented = true; }}; } 577 signal(cm, override || e.type, cm, e); 578 return e_defaultPrevented(e) || e.codemirrorIgnore 579 } 580 581 function signalCursorActivity(cm) { 582 var arr = cm._handlers && cm._handlers.cursorActivity; 583 if (!arr) { return } 584 var set = cm.curOp.cursorActivityHandlers || (cm.curOp.cursorActivityHandlers = []); 585 for (var i = 0; i < arr.length; ++i) { if (indexOf(set, arr[i]) == -1) 586 { set.push(arr[i]); } } 587 } 588 589 function hasHandler(emitter, type) { 590 return getHandlers(emitter, type).length > 0 591 } 592 593 // Add on and off methods to a constructor's prototype, to make 594 // registering events on such objects more convenient. 595 function eventMixin(ctor) { 596 ctor.prototype.on = function(type, f) {on(this, type, f);}; 597 ctor.prototype.off = function(type, f) {off(this, type, f);}; 598 } 599 600 // Due to the fact that we still support jurassic IE versions, some 601 // compatibility wrappers are needed. 602 603 function e_preventDefault(e) { 604 if (e.preventDefault) { e.preventDefault(); } 605 else { e.returnValue = false; } 606 } 607 function e_stopPropagation(e) { 608 if (e.stopPropagation) { e.stopPropagation(); } 609 else { e.cancelBubble = true; } 610 } 611 function e_defaultPrevented(e) { 612 return e.defaultPrevented != null ? e.defaultPrevented : e.returnValue == false 613 } 614 function e_stop(e) {e_preventDefault(e); e_stopPropagation(e);} 615 616 function e_target(e) {return e.target || e.srcElement} 617 function e_button(e) { 618 var b = e.which; 619 if (b == null) { 620 if (e.button & 1) { b = 1; } 621 else if (e.button & 2) { b = 3; } 622 else if (e.button & 4) { b = 2; } 623 } 624 if (mac && e.ctrlKey && b == 1) { b = 3; } 625 return b 626 } 627 628 // Detect drag-and-drop 629 var dragAndDrop = function() { 630 // There is *some* kind of drag-and-drop support in IE6-8, but I 631 // couldn't get it to work yet. 632 if (ie && ie_version < 9) { return false } 633 var div = elt('div'); 634 return "draggable" in div || "dragDrop" in div 635 }(); 636 637 var zwspSupported; 638 function zeroWidthElement(measure) { 639 if (zwspSupported == null) { 640 var test = elt("span", "\u200b"); 641 removeChildrenAndAdd(measure, elt("span", [test, document.createTextNode("x")])); 642 if (measure.firstChild.offsetHeight != 0) 643 { zwspSupported = test.offsetWidth <= 1 && test.offsetHeight > 2 && !(ie && ie_version < 8); } 644 } 645 var node = zwspSupported ? elt("span", "\u200b") : 646 elt("span", "\u00a0", null, "display: inline-block; width: 1px; margin-right: -1px"); 647 node.setAttribute("cm-text", ""); 648 return node 649 } 650 651 // Feature-detect IE's crummy client rect reporting for bidi text 652 var badBidiRects; 653 function hasBadBidiRects(measure) { 654 if (badBidiRects != null) { return badBidiRects } 655 var txt = removeChildrenAndAdd(measure, document.createTextNode("A\u062eA")); 656 var r0 = range(txt, 0, 1).getBoundingClientRect(); 657 var r1 = range(txt, 1, 2).getBoundingClientRect(); 658 removeChildren(measure); 659 if (!r0 || r0.left == r0.right) { return false } // Safari returns null in some cases (#2780) 660 return badBidiRects = (r1.right - r0.right < 3) 661 } 662 663 // See if "".split is the broken IE version, if so, provide an 664 // alternative way to split lines. 665 var splitLinesAuto = "\n\nb".split(/\n/).length != 3 ? function (string) { 666 var pos = 0, result = [], l = string.length; 667 while (pos <= l) { 668 var nl = string.indexOf("\n", pos); 669 if (nl == -1) { nl = string.length; } 670 var line = string.slice(pos, string.charAt(nl - 1) == "\r" ? nl - 1 : nl); 671 var rt = line.indexOf("\r"); 672 if (rt != -1) { 673 result.push(line.slice(0, rt)); 674 pos += rt + 1; 675 } else { 676 result.push(line); 677 pos = nl + 1; 678 } 679 } 680 return result 681 } : function (string) { return string.split(/\r\n?|\n/); }; 682 683 var hasSelection = window.getSelection ? function (te) { 684 try { return te.selectionStart != te.selectionEnd } 685 catch(e) { return false } 686 } : function (te) { 687 var range; 688 try {range = te.ownerDocument.selection.createRange();} 689 catch(e) {} 690 if (!range || range.parentElement() != te) { return false } 691 return range.compareEndPoints("StartToEnd", range) != 0 692 }; 693 694 var hasCopyEvent = (function () { 695 var e = elt("div"); 696 if ("oncopy" in e) { return true } 697 e.setAttribute("oncopy", "return;"); 698 return typeof e.oncopy == "function" 699 })(); 700 701 var badZoomedRects = null; 702 function hasBadZoomedRects(measure) { 703 if (badZoomedRects != null) { return badZoomedRects } 704 var node = removeChildrenAndAdd(measure, elt("span", "x")); 705 var normal = node.getBoundingClientRect(); 706 var fromRange = range(node, 0, 1).getBoundingClientRect(); 707 return badZoomedRects = Math.abs(normal.left - fromRange.left) > 1 708 } 709 710 // Known modes, by name and by MIME 711 var modes = {}, mimeModes = {}; 712 713 // Extra arguments are stored as the mode's dependencies, which is 714 // used by (legacy) mechanisms like loadmode.js to automatically 715 // load a mode. (Preferred mechanism is the require/define calls.) 716 function defineMode(name, mode) { 717 if (arguments.length > 2) 718 { mode.dependencies = Array.prototype.slice.call(arguments, 2); } 719 modes[name] = mode; 720 } 721 722 function defineMIME(mime, spec) { 723 mimeModes[mime] = spec; 724 } 725 726 // Given a MIME type, a {name, ...options} config object, or a name 727 // string, return a mode config object. 728 function resolveMode(spec) { 729 if (typeof spec == "string" && mimeModes.hasOwnProperty(spec)) { 730 spec = mimeModes[spec]; 731 } else if (spec && typeof spec.name == "string" && mimeModes.hasOwnProperty(spec.name)) { 732 var found = mimeModes[spec.name]; 733 if (typeof found == "string") { found = {name: found}; } 734 spec = createObj(found, spec); 735 spec.name = found.name; 736 } else if (typeof spec == "string" && /^[\w\-]+\/[\w\-]+\+xml$/.test(spec)) { 737 return resolveMode("application/xml") 738 } else if (typeof spec == "string" && /^[\w\-]+\/[\w\-]+\+json$/.test(spec)) { 739 return resolveMode("application/json") 740 } 741 if (typeof spec == "string") { return {name: spec} } 742 else { return spec || {name: "null"} } 743 } 744 745 // Given a mode spec (anything that resolveMode accepts), find and 746 // initialize an actual mode object. 747 function getMode(options, spec) { 748 spec = resolveMode(spec); 749 var mfactory = modes[spec.name]; 750 if (!mfactory) { return getMode(options, "text/plain") } 751 var modeObj = mfactory(options, spec); 752 if (modeExtensions.hasOwnProperty(spec.name)) { 753 var exts = modeExtensions[spec.name]; 754 for (var prop in exts) { 755 if (!exts.hasOwnProperty(prop)) { continue } 756 if (modeObj.hasOwnProperty(prop)) { modeObj["_" + prop] = modeObj[prop]; } 757 modeObj[prop] = exts[prop]; 758 } 759 } 760 modeObj.name = spec.name; 761 if (spec.helperType) { modeObj.helperType = spec.helperType; } 762 if (spec.modeProps) { for (var prop$1 in spec.modeProps) 763 { modeObj[prop$1] = spec.modeProps[prop$1]; } } 764 765 return modeObj 766 } 767 768 // This can be used to attach properties to mode objects from 769 // outside the actual mode definition. 770 var modeExtensions = {}; 771 function extendMode(mode, properties) { 772 var exts = modeExtensions.hasOwnProperty(mode) ? modeExtensions[mode] : (modeExtensions[mode] = {}); 773 copyObj(properties, exts); 774 } 775 776 function copyState(mode, state) { 777 if (state === true) { return state } 778 if (mode.copyState) { return mode.copyState(state) } 779 var nstate = {}; 780 for (var n in state) { 781 var val = state[n]; 782 if (val instanceof Array) { val = val.concat([]); } 783 nstate[n] = val; 784 } 785 return nstate 786 } 787 788 // Given a mode and a state (for that mode), find the inner mode and 789 // state at the position that the state refers to. 790 function innerMode(mode, state) { 791 var info; 792 while (mode.innerMode) { 793 info = mode.innerMode(state); 794 if (!info || info.mode == mode) { break } 795 state = info.state; 796 mode = info.mode; 797 } 798 return info || {mode: mode, state: state} 799 } 800 801 function startState(mode, a1, a2) { 802 return mode.startState ? mode.startState(a1, a2) : true 803 } 804 805 // STRING STREAM 806 807 // Fed to the mode parsers, provides helper functions to make 808 // parsers more succinct. 809 810 var StringStream = function(string, tabSize, lineOracle) { 811 this.pos = this.start = 0; 812 this.string = string; 813 this.tabSize = tabSize || 8; 814 this.lastColumnPos = this.lastColumnValue = 0; 815 this.lineStart = 0; 816 this.lineOracle = lineOracle; 817 }; 818 819 StringStream.prototype.eol = function () {return this.pos >= this.string.length}; 820 StringStream.prototype.sol = function () {return this.pos == this.lineStart}; 821 StringStream.prototype.peek = function () {return this.string.charAt(this.pos) || undefined}; 822 StringStream.prototype.next = function () { 823 if (this.pos < this.string.length) 824 { return this.string.charAt(this.pos++) } 825 }; 826 StringStream.prototype.eat = function (match) { 827 var ch = this.string.charAt(this.pos); 828 var ok; 829 if (typeof match == "string") { ok = ch == match; } 830 else { ok = ch && (match.test ? match.test(ch) : match(ch)); } 831 if (ok) {++this.pos; return ch} 832 }; 833 StringStream.prototype.eatWhile = function (match) { 834 var start = this.pos; 835 while (this.eat(match)){} 836 return this.pos > start 837 }; 838 StringStream.prototype.eatSpace = function () { 839 var start = this.pos; 840 while (/[\s\u00a0]/.test(this.string.charAt(this.pos))) { ++this.pos; } 841 return this.pos > start 842 }; 843 StringStream.prototype.skipToEnd = function () {this.pos = this.string.length;}; 844 StringStream.prototype.skipTo = function (ch) { 845 var found = this.string.indexOf(ch, this.pos); 846 if (found > -1) {this.pos = found; return true} 847 }; 848 StringStream.prototype.backUp = function (n) {this.pos -= n;}; 849 StringStream.prototype.column = function () { 850 if (this.lastColumnPos < this.start) { 851 this.lastColumnValue = countColumn(this.string, this.start, this.tabSize, this.lastColumnPos, this.lastColumnValue); 852 this.lastColumnPos = this.start; 853 } 854 return this.lastColumnValue - (this.lineStart ? countColumn(this.string, this.lineStart, this.tabSize) : 0) 855 }; 856 StringStream.prototype.indentation = function () { 857 return countColumn(this.string, null, this.tabSize) - 858 (this.lineStart ? countColumn(this.string, this.lineStart, this.tabSize) : 0) 859 }; 860 StringStream.prototype.match = function (pattern, consume, caseInsensitive) { 861 if (typeof pattern == "string") { 862 var cased = function (str) { return caseInsensitive ? str.toLowerCase() : str; }; 863 var substr = this.string.substr(this.pos, pattern.length); 864 if (cased(substr) == cased(pattern)) { 865 if (consume !== false) { this.pos += pattern.length; } 866 return true 867 } 868 } else { 869 var match = this.string.slice(this.pos).match(pattern); 870 if (match && match.index > 0) { return null } 871 if (match && consume !== false) { this.pos += match[0].length; } 872 return match 873 } 874 }; 875 StringStream.prototype.current = function (){return this.string.slice(this.start, this.pos)}; 876 StringStream.prototype.hideFirstChars = function (n, inner) { 877 this.lineStart += n; 878 try { return inner() } 879 finally { this.lineStart -= n; } 880 }; 881 StringStream.prototype.lookAhead = function (n) { 882 var oracle = this.lineOracle; 883 return oracle && oracle.lookAhead(n) 884 }; 885 StringStream.prototype.baseToken = function () { 886 var oracle = this.lineOracle; 887 return oracle && oracle.baseToken(this.pos) 888 }; 889 890 // Find the line object corresponding to the given line number. 891 function getLine(doc, n) { 892 n -= doc.first; 893 if (n < 0 || n >= doc.size) { throw new Error("There is no line " + (n + doc.first) + " in the document.") } 894 var chunk = doc; 895 while (!chunk.lines) { 896 for (var i = 0;; ++i) { 897 var child = chunk.children[i], sz = child.chunkSize(); 898 if (n < sz) { chunk = child; break } 899 n -= sz; 900 } 901 } 902 return chunk.lines[n] 903 } 904 905 // Get the part of a document between two positions, as an array of 906 // strings. 907 function getBetween(doc, start, end) { 908 var out = [], n = start.line; 909 doc.iter(start.line, end.line + 1, function (line) { 910 var text = line.text; 911 if (n == end.line) { text = text.slice(0, end.ch); } 912 if (n == start.line) { text = text.slice(start.ch); } 913 out.push(text); 914 ++n; 915 }); 916 return out 917 } 918 // Get the lines between from and to, as array of strings. 919 function getLines(doc, from, to) { 920 var out = []; 921 doc.iter(from, to, function (line) { out.push(line.text); }); // iter aborts when callback returns truthy value 922 return out 923 } 924 925 // Update the height of a line, propagating the height change 926 // upwards to parent nodes. 927 function updateLineHeight(line, height) { 928 var diff = height - line.height; 929 if (diff) { for (var n = line; n; n = n.parent) { n.height += diff; } } 930 } 931 932 // Given a line object, find its line number by walking up through 933 // its parent links. 934 function lineNo(line) { 935 if (line.parent == null) { return null } 936 var cur = line.parent, no = indexOf(cur.lines, line); 937 for (var chunk = cur.parent; chunk; cur = chunk, chunk = chunk.parent) { 938 for (var i = 0;; ++i) { 939 if (chunk.children[i] == cur) { break } 940 no += chunk.children[i].chunkSize(); 941 } 942 } 943 return no + cur.first 944 } 945 946 // Find the line at the given vertical position, using the height 947 // information in the document tree. 948 function lineAtHeight(chunk, h) { 949 var n = chunk.first; 950 outer: do { 951 for (var i$1 = 0; i$1 < chunk.children.length; ++i$1) { 952 var child = chunk.children[i$1], ch = child.height; 953 if (h < ch) { chunk = child; continue outer } 954 h -= ch; 955 n += child.chunkSize(); 956 } 957 return n 958 } while (!chunk.lines) 959 var i = 0; 960 for (; i < chunk.lines.length; ++i) { 961 var line = chunk.lines[i], lh = line.height; 962 if (h < lh) { break } 963 h -= lh; 964 } 965 return n + i 966 } 967 968 function isLine(doc, l) {return l >= doc.first && l < doc.first + doc.size} 969 970 function lineNumberFor(options, i) { 971 return String(options.lineNumberFormatter(i + options.firstLineNumber)) 972 } 973 974 // A Pos instance represents a position within the text. 975 function Pos(line, ch, sticky) { 976 if ( sticky === void 0 ) sticky = null; 977 978 if (!(this instanceof Pos)) { return new Pos(line, ch, sticky) } 979 this.line = line; 980 this.ch = ch; 981 this.sticky = sticky; 982 } 983 984 // Compare two positions, return 0 if they are the same, a negative 985 // number when a is less, and a positive number otherwise. 986 function cmp(a, b) { return a.line - b.line || a.ch - b.ch } 987 988 function equalCursorPos(a, b) { return a.sticky == b.sticky && cmp(a, b) == 0 } 989 990 function copyPos(x) {return Pos(x.line, x.ch)} 991 function maxPos(a, b) { return cmp(a, b) < 0 ? b : a } 992 function minPos(a, b) { return cmp(a, b) < 0 ? a : b } 993 994 // Most of the external API clips given positions to make sure they 995 // actually exist within the document. 996 function clipLine(doc, n) {return Math.max(doc.first, Math.min(n, doc.first + doc.size - 1))} 997 function clipPos(doc, pos) { 998 if (pos.line < doc.first) { return Pos(doc.first, 0) } 999 var last = doc.first + doc.size - 1; 1000 if (pos.line > last) { return Pos(last, getLine(doc, last).text.length) } 1001 return clipToLen(pos, getLine(doc, pos.line).text.length) 1002 } 1003 function clipToLen(pos, linelen) { 1004 var ch = pos.ch; 1005 if (ch == null || ch > linelen) { return Pos(pos.line, linelen) } 1006 else if (ch < 0) { return Pos(pos.line, 0) } 1007 else { return pos } 1008 } 1009 function clipPosArray(doc, array) { 1010 var out = []; 1011 for (var i = 0; i < array.length; i++) { out[i] = clipPos(doc, array[i]); } 1012 return out 1013 } 1014 1015 var SavedContext = function(state, lookAhead) { 1016 this.state = state; 1017 this.lookAhead = lookAhead; 1018 }; 1019 1020 var Context = function(doc, state, line, lookAhead) { 1021 this.state = state; 1022 this.doc = doc; 1023 this.line = line; 1024 this.maxLookAhead = lookAhead || 0; 1025 this.baseTokens = null; 1026 this.baseTokenPos = 1; 1027 }; 1028 1029 Context.prototype.lookAhead = function (n) { 1030 var line = this.doc.getLine(this.line + n); 1031 if (line != null && n > this.maxLookAhead) { this.maxLookAhead = n; } 1032 return line 1033 }; 1034 1035 Context.prototype.baseToken = function (n) { 1036 if (!this.baseTokens) { return null } 1037 while (this.baseTokens[this.baseTokenPos] <= n) 1038 { this.baseTokenPos += 2; } 1039 var type = this.baseTokens[this.baseTokenPos + 1]; 1040 return {type: type && type.replace(/( |^)overlay .*/, ""), 1041 size: this.baseTokens[this.baseTokenPos] - n} 1042 }; 1043 1044 Context.prototype.nextLine = function () { 1045 this.line++; 1046 if (this.maxLookAhead > 0) { this.maxLookAhead--; } 1047 }; 1048 1049 Context.fromSaved = function (doc, saved, line) { 1050 if (saved instanceof SavedContext) 1051 { return new Context(doc, copyState(doc.mode, saved.state), line, saved.lookAhead) } 1052 else 1053 { return new Context(doc, copyState(doc.mode, saved), line) } 1054 }; 1055 1056 Context.prototype.save = function (copy) { 1057 var state = copy !== false ? copyState(this.doc.mode, this.state) : this.state; 1058 return this.maxLookAhead > 0 ? new SavedContext(state, this.maxLookAhead) : state 1059 }; 1060 1061 1062 // Compute a style array (an array starting with a mode generation 1063 // -- for invalidation -- followed by pairs of end positions and 1064 // style strings), which is used to highlight the tokens on the 1065 // line. 1066 function highlightLine(cm, line, context, forceToEnd) { 1067 // A styles array always starts with a number identifying the 1068 // mode/overlays that it is based on (for easy invalidation). 1069 var st = [cm.state.modeGen], lineClasses = {}; 1070 // Compute the base array of styles 1071 runMode(cm, line.text, cm.doc.mode, context, function (end, style) { return st.push(end, style); }, 1072 lineClasses, forceToEnd); 1073 var state = context.state; 1074 1075 // Run overlays, adjust style array. 1076 var loop = function ( o ) { 1077 context.baseTokens = st; 1078 var overlay = cm.state.overlays[o], i = 1, at = 0; 1079 context.state = true; 1080 runMode(cm, line.text, overlay.mode, context, function (end, style) { 1081 var start = i; 1082 // Ensure there's a token end at the current position, and that i points at it 1083 while (at < end) { 1084 var i_end = st[i]; 1085 if (i_end > end) 1086 { st.splice(i, 1, end, st[i+1], i_end); } 1087 i += 2; 1088 at = Math.min(end, i_end); 1089 } 1090 if (!style) { return } 1091 if (overlay.opaque) { 1092 st.splice(start, i - start, end, "overlay " + style); 1093 i = start + 2; 1094 } else { 1095 for (; start < i; start += 2) { 1096 var cur = st[start+1]; 1097 st[start+1] = (cur ? cur + " " : "") + "overlay " + style; 1098 } 1099 } 1100 }, lineClasses); 1101 context.state = state; 1102 context.baseTokens = null; 1103 context.baseTokenPos = 1; 1104 }; 1105 1106 for (var o = 0; o < cm.state.overlays.length; ++o) loop( o ); 1107 1108 return {styles: st, classes: lineClasses.bgClass || lineClasses.textClass ? lineClasses : null} 1109 } 1110 1111 function getLineStyles(cm, line, updateFrontier) { 1112 if (!line.styles || line.styles[0] != cm.state.modeGen) { 1113 var context = getContextBefore(cm, lineNo(line)); 1114 var resetState = line.text.length > cm.options.maxHighlightLength && copyState(cm.doc.mode, context.state); 1115 var result = highlightLine(cm, line, context); 1116 if (resetState) { context.state = resetState; } 1117 line.stateAfter = context.save(!resetState); 1118 line.styles = result.styles; 1119 if (result.classes) { line.styleClasses = result.classes; } 1120 else if (line.styleClasses) { line.styleClasses = null; } 1121 if (updateFrontier === cm.doc.highlightFrontier) 1122 { cm.doc.modeFrontier = Math.max(cm.doc.modeFrontier, ++cm.doc.highlightFrontier); } 1123 } 1124 return line.styles 1125 } 1126 1127 function getContextBefore(cm, n, precise) { 1128 var doc = cm.doc, display = cm.display; 1129 if (!doc.mode.startState) { return new Context(doc, true, n) } 1130 var start = findStartLine(cm, n, precise); 1131 var saved = start > doc.first && getLine(doc, start - 1).stateAfter; 1132 var context = saved ? Context.fromSaved(doc, saved, start) : new Context(doc, startState(doc.mode), start); 1133 1134 doc.iter(start, n, function (line) { 1135 processLine(cm, line.text, context); 1136 var pos = context.line; 1137 line.stateAfter = pos == n - 1 || pos % 5 == 0 || pos >= display.viewFrom && pos < display.viewTo ? context.save() : null; 1138 context.nextLine(); 1139 }); 1140 if (precise) { doc.modeFrontier = context.line; } 1141 return context 1142 } 1143 1144 // Lightweight form of highlight -- proceed over this line and 1145 // update state, but don't save a style array. Used for lines that 1146 // aren't currently visible. 1147 function processLine(cm, text, context, startAt) { 1148 var mode = cm.doc.mode; 1149 var stream = new StringStream(text, cm.options.tabSize, context); 1150 stream.start = stream.pos = startAt || 0; 1151 if (text == "") { callBlankLine(mode, context.state); } 1152 while (!stream.eol()) { 1153 readToken(mode, stream, context.state); 1154 stream.start = stream.pos; 1155 } 1156 } 1157 1158 function callBlankLine(mode, state) { 1159 if (mode.blankLine) { return mode.blankLine(state) } 1160 if (!mode.innerMode) { return } 1161 var inner = innerMode(mode, state); 1162 if (inner.mode.blankLine) { return inner.mode.blankLine(inner.state) } 1163 } 1164 1165 function readToken(mode, stream, state, inner) { 1166 for (var i = 0; i < 10; i++) { 1167 if (inner) { inner[0] = innerMode(mode, state).mode; } 1168 var style = mode.token(stream, state); 1169 if (stream.pos > stream.start) { return style } 1170 } 1171 throw new Error("Mode " + mode.name + " failed to advance stream.") 1172 } 1173 1174 var Token = function(stream, type, state) { 1175 this.start = stream.start; this.end = stream.pos; 1176 this.string = stream.current(); 1177 this.type = type || null; 1178 this.state = state; 1179 }; 1180 1181 // Utility for getTokenAt and getLineTokens 1182 function takeToken(cm, pos, precise, asArray) { 1183 var doc = cm.doc, mode = doc.mode, style; 1184 pos = clipPos(doc, pos); 1185 var line = getLine(doc, pos.line), context = getContextBefore(cm, pos.line, precise); 1186 var stream = new StringStream(line.text, cm.options.tabSize, context), tokens; 1187 if (asArray) { tokens = []; } 1188 while ((asArray || stream.pos < pos.ch) && !stream.eol()) { 1189 stream.start = stream.pos; 1190 style = readToken(mode, stream, context.state); 1191 if (asArray) { tokens.push(new Token(stream, style, copyState(doc.mode, context.state))); } 1192 } 1193 return asArray ? tokens : new Token(stream, style, context.state) 1194 } 1195 1196 function extractLineClasses(type, output) { 1197 if (type) { for (;;) { 1198 var lineClass = type.match(/(?:^|\s+)line-(background-)?(\S+)/); 1199 if (!lineClass) { break } 1200 type = type.slice(0, lineClass.index) + type.slice(lineClass.index + lineClass[0].length); 1201 var prop = lineClass[1] ? "bgClass" : "textClass"; 1202 if (output[prop] == null) 1203 { output[prop] = lineClass[2]; } 1204 else if (!(new RegExp("(?:^|\\s)" + lineClass[2] + "(?:$|\\s)")).test(output[prop])) 1205 { output[prop] += " " + lineClass[2]; } 1206 } } 1207 return type 1208 } 1209 1210 // Run the given mode's parser over a line, calling f for each token. 1211 function runMode(cm, text, mode, context, f, lineClasses, forceToEnd) { 1212 var flattenSpans = mode.flattenSpans; 1213 if (flattenSpans == null) { flattenSpans = cm.options.flattenSpans; } 1214 var curStart = 0, curStyle = null; 1215 var stream = new StringStream(text, cm.options.tabSize, context), style; 1216 var inner = cm.options.addModeClass && [null]; 1217 if (text == "") { extractLineClasses(callBlankLine(mode, context.state), lineClasses); } 1218 while (!stream.eol()) { 1219 if (stream.pos > cm.options.maxHighlightLength) { 1220 flattenSpans = false; 1221 if (forceToEnd) { processLine(cm, text, context, stream.pos); } 1222 stream.pos = text.length; 1223 style = null; 1224 } else { 1225 style = extractLineClasses(readToken(mode, stream, context.state, inner), lineClasses); 1226 } 1227 if (inner) { 1228 var mName = inner[0].name; 1229 if (mName) { style = "m-" + (style ? mName + " " + style : mName); } 1230 } 1231 if (!flattenSpans || curStyle != style) { 1232 while (curStart < stream.start) { 1233 curStart = Math.min(stream.start, curStart + 5000); 1234 f(curStart, curStyle); 1235 } 1236 curStyle = style; 1237 } 1238 stream.start = stream.pos; 1239 } 1240 while (curStart < stream.pos) { 1241 // Webkit seems to refuse to render text nodes longer than 57444 1242 // characters, and returns inaccurate measurements in nodes 1243 // starting around 5000 chars. 1244 var pos = Math.min(stream.pos, curStart + 5000); 1245 f(pos, curStyle); 1246 curStart = pos; 1247 } 1248 } 1249 1250 // Finds the line to start with when starting a parse. Tries to 1251 // find a line with a stateAfter, so that it can start with a 1252 // valid state. If that fails, it returns the line with the 1253 // smallest indentation, which tends to need the least context to 1254 // parse correctly. 1255 function findStartLine(cm, n, precise) { 1256 var minindent, minline, doc = cm.doc; 1257 var lim = precise ? -1 : n - (cm.doc.mode.innerMode ? 1000 : 100); 1258 for (var search = n; search > lim; --search) { 1259 if (search <= doc.first) { return doc.first } 1260 var line = getLine(doc, search - 1), after = line.stateAfter; 1261 if (after && (!precise || search + (after instanceof SavedContext ? after.lookAhead : 0) <= doc.modeFrontier)) 1262 { return search } 1263 var indented = countColumn(line.text, null, cm.options.tabSize); 1264 if (minline == null || minindent > indented) { 1265 minline = search - 1; 1266 minindent = indented; 1267 } 1268 } 1269 return minline 1270 } 1271 1272 function retreatFrontier(doc, n) { 1273 doc.modeFrontier = Math.min(doc.modeFrontier, n); 1274 if (doc.highlightFrontier < n - 10) { return } 1275 var start = doc.first; 1276 for (var line = n - 1; line > start; line--) { 1277 var saved = getLine(doc, line).stateAfter; 1278 // change is on 3 1279 // state on line 1 looked ahead 2 -- so saw 3 1280 // test 1 + 2 < 3 should cover this 1281 if (saved && (!(saved instanceof SavedContext) || line + saved.lookAhead < n)) { 1282 start = line + 1; 1283 break 1284 } 1285 } 1286 doc.highlightFrontier = Math.min(doc.highlightFrontier, start); 1287 } 1288 1289 // Optimize some code when these features are not used. 1290 var sawReadOnlySpans = false, sawCollapsedSpans = false; 1291 1292 function seeReadOnlySpans() { 1293 sawReadOnlySpans = true; 1294 } 1295 1296 function seeCollapsedSpans() { 1297 sawCollapsedSpans = true; 1298 } 1299 1300 // TEXTMARKER SPANS 1301 1302 function MarkedSpan(marker, from, to) { 1303 this.marker = marker; 1304 this.from = from; this.to = to; 1305 } 1306 1307 // Search an array of spans for a span matching the given marker. 1308 function getMarkedSpanFor(spans, marker) { 1309 if (spans) { for (var i = 0; i < spans.length; ++i) { 1310 var span = spans[i]; 1311 if (span.marker == marker) { return span } 1312 } } 1313 } 1314 1315 // Remove a span from an array, returning undefined if no spans are 1316 // left (we don't store arrays for lines without spans). 1317 function removeMarkedSpan(spans, span) { 1318 var r; 1319 for (var i = 0; i < spans.length; ++i) 1320 { if (spans[i] != span) { (r || (r = [])).push(spans[i]); } } 1321 return r 1322 } 1323 1324 // Add a span to a line. 1325 function addMarkedSpan(line, span, op) { 1326 var inThisOp = op && window.WeakSet && (op.markedSpans || (op.markedSpans = new WeakSet)); 1327 if (inThisOp && inThisOp.has(line.markedSpans)) { 1328 line.markedSpans.push(span); 1329 } else { 1330 line.markedSpans = line.markedSpans ? line.markedSpans.concat([span]) : [span]; 1331 if (inThisOp) { inThisOp.add(line.markedSpans); } 1332 } 1333 span.marker.attachLine(line); 1334 } 1335 1336 // Used for the algorithm that adjusts markers for a change in the 1337 // document. These functions cut an array of spans at a given 1338 // character position, returning an array of remaining chunks (or 1339 // undefined if nothing remains). 1340 function markedSpansBefore(old, startCh, isInsert) { 1341 var nw; 1342 if (old) { for (var i = 0; i < old.length; ++i) { 1343 var span = old[i], marker = span.marker; 1344 var startsBefore = span.from == null || (marker.inclusiveLeft ? span.from <= startCh : span.from < startCh); 1345 if (startsBefore || span.from == startCh && marker.type == "bookmark" && (!isInsert || !span.marker.insertLeft)) { 1346 var endsAfter = span.to == null || (marker.inclusiveRight ? span.to >= startCh : span.to > startCh) 1347 ;(nw || (nw = [])).push(new MarkedSpan(marker, span.from, endsAfter ? null : span.to)); 1348 } 1349 } } 1350 return nw 1351 } 1352 function markedSpansAfter(old, endCh, isInsert) { 1353 var nw; 1354 if (old) { for (var i = 0; i < old.length; ++i) { 1355 var span = old[i], marker = span.marker; 1356 var endsAfter = span.to == null || (marker.inclusiveRight ? span.to >= endCh : span.to > endCh); 1357 if (endsAfter || span.from == endCh && marker.type == "bookmark" && (!isInsert || span.marker.insertLeft)) { 1358 var startsBefore = span.from == null || (marker.inclusiveLeft ? span.from <= endCh : span.from < endCh) 1359 ;(nw || (nw = [])).push(new MarkedSpan(marker, startsBefore ? null : span.from - endCh, 1360 span.to == null ? null : span.to - endCh)); 1361 } 1362 } } 1363 return nw 1364 } 1365 1366 // Given a change object, compute the new set of marker spans that 1367 // cover the line in which the change took place. Removes spans 1368 // entirely within the change, reconnects spans belonging to the 1369 // same marker that appear on both sides of the change, and cuts off 1370 // spans partially within the change. Returns an array of span 1371 // arrays with one element for each line in (after) the change. 1372 function stretchSpansOverChange(doc, change) { 1373 if (change.full) { return null } 1374 var oldFirst = isLine(doc, change.from.line) && getLine(doc, change.from.line).markedSpans; 1375 var oldLast = isLine(doc, change.to.line) && getLine(doc, change.to.line).markedSpans; 1376 if (!oldFirst && !oldLast) { return null } 1377 1378 var startCh = change.from.ch, endCh = change.to.ch, isInsert = cmp(change.from, change.to) == 0; 1379 // Get the spans that 'stick out' on both sides 1380 var first = markedSpansBefore(oldFirst, startCh, isInsert); 1381 var last = markedSpansAfter(oldLast, endCh, isInsert); 1382 1383 // Next, merge those two ends 1384 var sameLine = change.text.length == 1, offset = lst(change.text).length + (sameLine ? startCh : 0); 1385 if (first) { 1386 // Fix up .to properties of first 1387 for (var i = 0; i < first.length; ++i) { 1388 var span = first[i]; 1389 if (span.to == null) { 1390 var found = getMarkedSpanFor(last, span.marker); 1391 if (!found) { span.to = startCh; } 1392 else if (sameLine) { span.to = found.to == null ? null : found.to + offset; } 1393 } 1394 } 1395 } 1396 if (last) { 1397 // Fix up .from in last (or move them into first in case of sameLine) 1398 for (var i$1 = 0; i$1 < last.length; ++i$1) { 1399 var span$1 = last[i$1]; 1400 if (span$1.to != null) { span$1.to += offset; } 1401 if (span$1.from == null) { 1402 var found$1 = getMarkedSpanFor(first, span$1.marker); 1403 if (!found$1) { 1404 span$1.from = offset; 1405 if (sameLine) { (first || (first = [])).push(span$1); } 1406 } 1407 } else { 1408 span$1.from += offset; 1409 if (sameLine) { (first || (first = [])).push(span$1); } 1410 } 1411 } 1412 } 1413 // Make sure we didn't create any zero-length spans 1414 if (first) { first = clearEmptySpans(first); } 1415 if (last && last != first) { last = clearEmptySpans(last); } 1416 1417 var newMarkers = [first]; 1418 if (!sameLine) { 1419 // Fill gap with whole-line-spans 1420 var gap = change.text.length - 2, gapMarkers; 1421 if (gap > 0 && first) 1422 { for (var i$2 = 0; i$2 < first.length; ++i$2) 1423 { if (first[i$2].to == null) 1424 { (gapMarkers || (gapMarkers = [])).push(new MarkedSpan(first[i$2].marker, null, null)); } } } 1425 for (var i$3 = 0; i$3 < gap; ++i$3) 1426 { newMarkers.push(gapMarkers); } 1427 newMarkers.push(last); 1428 } 1429 return newMarkers 1430 } 1431 1432 // Remove spans that are empty and don't have a clearWhenEmpty 1433 // option of false. 1434 function clearEmptySpans(spans) { 1435 for (var i = 0; i < spans.length; ++i) { 1436 var span = spans[i]; 1437 if (span.from != null && span.from == span.to && span.marker.clearWhenEmpty !== false) 1438 { spans.splice(i--, 1); } 1439 } 1440 if (!spans.length) { return null } 1441 return spans 1442 } 1443 1444 // Used to 'clip' out readOnly ranges when making a change. 1445 function removeReadOnlyRanges(doc, from, to) { 1446 var markers = null; 1447 doc.iter(from.line, to.line + 1, function (line) { 1448 if (line.markedSpans) { for (var i = 0; i < line.markedSpans.length; ++i) { 1449 var mark = line.markedSpans[i].marker; 1450 if (mark.readOnly && (!markers || indexOf(markers, mark) == -1)) 1451 { (markers || (markers = [])).push(mark); } 1452 } } 1453 }); 1454 if (!markers) { return null } 1455 var parts = [{from: from, to: to}]; 1456 for (var i = 0; i < markers.length; ++i) { 1457 var mk = markers[i], m = mk.find(0); 1458 for (var j = 0; j < parts.length; ++j) { 1459 var p = parts[j]; 1460 if (cmp(p.to, m.from) < 0 || cmp(p.from, m.to) > 0) { continue } 1461 var newParts = [j, 1], dfrom = cmp(p.from, m.from), dto = cmp(p.to, m.to); 1462 if (dfrom < 0 || !mk.inclusiveLeft && !dfrom) 1463 { newParts.push({from: p.from, to: m.from}); } 1464 if (dto > 0 || !mk.inclusiveRight && !dto) 1465 { newParts.push({from: m.to, to: p.to}); } 1466 parts.splice.apply(parts, newParts); 1467 j += newParts.length - 3; 1468 } 1469 } 1470 return parts 1471 } 1472 1473 // Connect or disconnect spans from a line. 1474 function detachMarkedSpans(line) { 1475 var spans = line.markedSpans; 1476 if (!spans) { return } 1477 for (var i = 0; i < spans.length; ++i) 1478 { spans[i].marker.detachLine(line); } 1479 line.markedSpans = null; 1480 } 1481 function attachMarkedSpans(line, spans) { 1482 if (!spans) { return } 1483 for (var i = 0; i < spans.length; ++i) 1484 { spans[i].marker.attachLine(line); } 1485 line.markedSpans = spans; 1486 } 1487 1488 // Helpers used when computing which overlapping collapsed span 1489 // counts as the larger one. 1490 function extraLeft(marker) { return marker.inclusiveLeft ? -1 : 0 } 1491 function extraRight(marker) { return marker.inclusiveRight ? 1 : 0 } 1492 1493 // Returns a number indicating which of two overlapping collapsed 1494 // spans is larger (and thus includes the other). Falls back to 1495 // comparing ids when the spans cover exactly the same range. 1496 function compareCollapsedMarkers(a, b) { 1497 var lenDiff = a.lines.length - b.lines.length; 1498 if (lenDiff != 0) { return lenDiff } 1499 var aPos = a.find(), bPos = b.find(); 1500 var fromCmp = cmp(aPos.from, bPos.from) || extraLeft(a) - extraLeft(b); 1501 if (fromCmp) { return -fromCmp } 1502 var toCmp = cmp(aPos.to, bPos.to) || extraRight(a) - extraRight(b); 1503 if (toCmp) { return toCmp } 1504 return b.id - a.id 1505 } 1506 1507 // Find out whether a line ends or starts in a collapsed span. If 1508 // so, return the marker for that span. 1509 function collapsedSpanAtSide(line, start) { 1510 var sps = sawCollapsedSpans && line.markedSpans, found; 1511 if (sps) { for (var sp = (void 0), i = 0; i < sps.length; ++i) { 1512 sp = sps[i]; 1513 if (sp.marker.collapsed && (start ? sp.from : sp.to) == null && 1514 (!found || compareCollapsedMarkers(found, sp.marker) < 0)) 1515 { found = sp.marker; } 1516 } } 1517 return found 1518 } 1519 function collapsedSpanAtStart(line) { return collapsedSpanAtSide(line, true) } 1520 function collapsedSpanAtEnd(line) { return collapsedSpanAtSide(line, false) } 1521 1522 function collapsedSpanAround(line, ch) { 1523 var sps = sawCollapsedSpans && line.markedSpans, found; 1524 if (sps) { for (var i = 0; i < sps.length; ++i) { 1525 var sp = sps[i]; 1526 if (sp.marker.collapsed && (sp.from == null || sp.from < ch) && (sp.to == null || sp.to > ch) && 1527 (!found || compareCollapsedMarkers(found, sp.marker) < 0)) { found = sp.marker; } 1528 } } 1529 return found 1530 } 1531 1532 // Test whether there exists a collapsed span that partially 1533 // overlaps (covers the start or end, but not both) of a new span. 1534 // Such overlap is not allowed. 1535 function conflictingCollapsedRange(doc, lineNo, from, to, marker) { 1536 var line = getLine(doc, lineNo); 1537 var sps = sawCollapsedSpans && line.markedSpans; 1538 if (sps) { for (var i = 0; i < sps.length; ++i) { 1539 var sp = sps[i]; 1540 if (!sp.marker.collapsed) { continue } 1541 var found = sp.marker.find(0); 1542 var fromCmp = cmp(found.from, from) || extraLeft(sp.marker) - extraLeft(marker); 1543 var toCmp = cmp(found.to, to) || extraRight(sp.marker) - extraRight(marker); 1544 if (fromCmp >= 0 && toCmp <= 0 || fromCmp <= 0 && toCmp >= 0) { continue } 1545 if (fromCmp <= 0 && (sp.marker.inclusiveRight && marker.inclusiveLeft ? cmp(found.to, from) >= 0 : cmp(found.to, from) > 0) || 1546 fromCmp >= 0 && (sp.marker.inclusiveRight && marker.inclusiveLeft ? cmp(found.from, to) <= 0 : cmp(found.from, to) < 0)) 1547 { return true } 1548 } } 1549 } 1550 1551 // A visual line is a line as drawn on the screen. Folding, for 1552 // example, can cause multiple logical lines to appear on the same 1553 // visual line. This finds the start of the visual line that the 1554 // given line is part of (usually that is the line itself). 1555 function visualLine(line) { 1556 var merged; 1557 while (merged = collapsedSpanAtStart(line)) 1558 { line = merged.find(-1, true).line; } 1559 return line 1560 } 1561 1562 function visualLineEnd(line) { 1563 var merged; 1564 while (merged = collapsedSpanAtEnd(line)) 1565 { line = merged.find(1, true).line; } 1566 return line 1567 } 1568 1569 // Returns an array of logical lines that continue the visual line 1570 // started by the argument, or undefined if there are no such lines. 1571 function visualLineContinued(line) { 1572 var merged, lines; 1573 while (merged = collapsedSpanAtEnd(line)) { 1574 line = merged.find(1, true).line 1575 ;(lines || (lines = [])).push(line); 1576 } 1577 return lines 1578 } 1579 1580 // Get the line number of the start of the visual line that the 1581 // given line number is part of. 1582 function visualLineNo(doc, lineN) { 1583 var line = getLine(doc, lineN), vis = visualLine(line); 1584 if (line == vis) { return lineN } 1585 return lineNo(vis) 1586 } 1587 1588 // Get the line number of the start of the next visual line after 1589 // the given line. 1590 function visualLineEndNo(doc, lineN) { 1591 if (lineN > doc.lastLine()) { return lineN } 1592 var line = getLine(doc, lineN), merged; 1593 if (!lineIsHidden(doc, line)) { return lineN } 1594 while (merged = collapsedSpanAtEnd(line)) 1595 { line = merged.find(1, true).line; } 1596 return lineNo(line) + 1 1597 } 1598 1599 // Compute whether a line is hidden. Lines count as hidden when they 1600 // are part of a visual line that starts with another line, or when 1601 // they are entirely covered by collapsed, non-widget span. 1602 function lineIsHidden(doc, line) { 1603 var sps = sawCollapsedSpans && line.markedSpans; 1604 if (sps) { for (var sp = (void 0), i = 0; i < sps.length; ++i) { 1605 sp = sps[i]; 1606 if (!sp.marker.collapsed) { continue } 1607 if (sp.from == null) { return true } 1608 if (sp.marker.widgetNode) { continue } 1609 if (sp.from == 0 && sp.marker.inclusiveLeft && lineIsHiddenInner(doc, line, sp)) 1610 { return true } 1611 } } 1612 } 1613 function lineIsHiddenInner(doc, line, span) { 1614 if (span.to == null) { 1615 var end = span.marker.find(1, true); 1616 return lineIsHiddenInner(doc, end.line, getMarkedSpanFor(end.line.markedSpans, span.marker)) 1617 } 1618 if (span.marker.inclusiveRight && span.to == line.text.length) 1619 { return true } 1620 for (var sp = (void 0), i = 0; i < line.markedSpans.length; ++i) { 1621 sp = line.markedSpans[i]; 1622 if (sp.marker.collapsed && !sp.marker.widgetNode && sp.from == span.to && 1623 (sp.to == null || sp.to != span.from) && 1624 (sp.marker.inclusiveLeft || span.marker.inclusiveRight) && 1625 lineIsHiddenInner(doc, line, sp)) { return true } 1626 } 1627 } 1628 1629 // Find the height above the given line. 1630 function heightAtLine(lineObj) { 1631 lineObj = visualLine(lineObj); 1632 1633 var h = 0, chunk = lineObj.parent; 1634 for (var i = 0; i < chunk.lines.length; ++i) { 1635 var line = chunk.lines[i]; 1636 if (line == lineObj) { break } 1637 else { h += line.height; } 1638 } 1639 for (var p = chunk.parent; p; chunk = p, p = chunk.parent) { 1640 for (var i$1 = 0; i$1 < p.children.length; ++i$1) { 1641 var cur = p.children[i$1]; 1642 if (cur == chunk) { break } 1643 else { h += cur.height; } 1644 } 1645 } 1646 return h 1647 } 1648 1649 // Compute the character length of a line, taking into account 1650 // collapsed ranges (see markText) that might hide parts, and join 1651 // other lines onto it. 1652 function lineLength(line) { 1653 if (line.height == 0) { return 0 } 1654 var len = line.text.length, merged, cur = line; 1655 while (merged = collapsedSpanAtStart(cur)) { 1656 var found = merged.find(0, true); 1657 cur = found.from.line; 1658 len += found.from.ch - found.to.ch; 1659 } 1660 cur = line; 1661 while (merged = collapsedSpanAtEnd(cur)) { 1662 var found$1 = merged.find(0, true); 1663 len -= cur.text.length - found$1.from.ch; 1664 cur = found$1.to.line; 1665 len += cur.text.length - found$1.to.ch; 1666 } 1667 return len 1668 } 1669 1670 // Find the longest line in the document. 1671 function findMaxLine(cm) { 1672 var d = cm.display, doc = cm.doc; 1673 d.maxLine = getLine(doc, doc.first); 1674 d.maxLineLength = lineLength(d.maxLine); 1675 d.maxLineChanged = true; 1676 doc.iter(function (line) { 1677 var len = lineLength(line); 1678 if (len > d.maxLineLength) { 1679 d.maxLineLength = len; 1680 d.maxLine = line; 1681 } 1682 }); 1683 } 1684 1685 // LINE DATA STRUCTURE 1686 1687 // Line objects. These hold state related to a line, including 1688 // highlighting info (the styles array). 1689 var Line = function(text, markedSpans, estimateHeight) { 1690 this.text = text; 1691 attachMarkedSpans(this, markedSpans); 1692 this.height = estimateHeight ? estimateHeight(this) : 1; 1693 }; 1694 1695 Line.prototype.lineNo = function () { return lineNo(this) }; 1696 eventMixin(Line); 1697 1698 // Change the content (text, markers) of a line. Automatically 1699 // invalidates cached information and tries to re-estimate the 1700 // line's height. 1701 function updateLine(line, text, markedSpans, estimateHeight) { 1702 line.text = text; 1703 if (line.stateAfter) { line.stateAfter = null; } 1704 if (line.styles) { line.styles = null; } 1705 if (line.order != null) { line.order = null; } 1706 detachMarkedSpans(line); 1707 attachMarkedSpans(line, markedSpans); 1708 var estHeight = estimateHeight ? estimateHeight(line) : 1; 1709 if (estHeight != line.height) { updateLineHeight(line, estHeight); } 1710 } 1711 1712 // Detach a line from the document tree and its markers. 1713 function cleanUpLine(line) { 1714 line.parent = null; 1715 detachMarkedSpans(line); 1716 } 1717 1718 // Convert a style as returned by a mode (either null, or a string 1719 // containing one or more styles) to a CSS style. This is cached, 1720 // and also looks for line-wide styles. 1721 var styleToClassCache = {}, styleToClassCacheWithMode = {}; 1722 function interpretTokenStyle(style, options) { 1723 if (!style || /^\s*$/.test(style)) { return null } 1724 var cache = options.addModeClass ? styleToClassCacheWithMode : styleToClassCache; 1725 return cache[style] || 1726 (cache[style] = style.replace(/\S+/g, "cm-$&")) 1727 } 1728 1729 // Render the DOM representation of the text of a line. Also builds 1730 // up a 'line map', which points at the DOM nodes that represent 1731 // specific stretches of text, and is used by the measuring code. 1732 // The returned object contains the DOM node, this map, and 1733 // information about line-wide styles that were set by the mode. 1734 function buildLineContent(cm, lineView) { 1735 // The padding-right forces the element to have a 'border', which 1736 // is needed on Webkit to be able to get line-level bounding 1737 // rectangles for it (in measureChar). 1738 var content = eltP("span", null, null, webkit ? "padding-right: .1px" : null); 1739 var builder = {pre: eltP("pre", [content], "CodeMirror-line"), content: content, 1740 col: 0, pos: 0, cm: cm, 1741 trailingSpace: false, 1742 splitSpaces: cm.getOption("lineWrapping")}; 1743 lineView.measure = {}; 1744 1745 // Iterate over the logical lines that make up this visual line. 1746 for (var i = 0; i <= (lineView.rest ? lineView.rest.length : 0); i++) { 1747 var line = i ? lineView.rest[i - 1] : lineView.line, order = (void 0); 1748 builder.pos = 0; 1749 builder.addToken = buildToken; 1750 // Optionally wire in some hacks into the token-rendering 1751 // algorithm, to deal with browser quirks. 1752 if (hasBadBidiRects(cm.display.measure) && (order = getOrder(line, cm.doc.direction))) 1753 { builder.addToken = buildTokenBadBidi(builder.addToken, order); } 1754 builder.map = []; 1755 var allowFrontierUpdate = lineView != cm.display.externalMeasured && lineNo(line); 1756 insertLineContent(line, builder, getLineStyles(cm, line, allowFrontierUpdate)); 1757 if (line.styleClasses) { 1758 if (line.styleClasses.bgClass) 1759 { builder.bgClass = joinClasses(line.styleClasses.bgClass, builder.bgClass || ""); } 1760 if (line.styleClasses.textClass) 1761 { builder.textClass = joinClasses(line.styleClasses.textClass, builder.textClass || ""); } 1762 } 1763 1764 // Ensure at least a single node is present, for measuring. 1765 if (builder.map.length == 0) 1766 { builder.map.push(0, 0, builder.content.appendChild(zeroWidthElement(cm.display.measure))); } 1767 1768 // Store the map and a cache object for the current logical line 1769 if (i == 0) { 1770 lineView.measure.map = builder.map; 1771 lineView.measure.cache = {}; 1772 } else { 1773 (lineView.measure.maps || (lineView.measure.maps = [])).push(builder.map) 1774 ;(lineView.measure.caches || (lineView.measure.caches = [])).push({}); 1775 } 1776 } 1777 1778 // See issue #2901 1779 if (webkit) { 1780 var last = builder.content.lastChild; 1781 if (/\bcm-tab\b/.test(last.className) || (last.querySelector && last.querySelector(".cm-tab"))) 1782 { builder.content.className = "cm-tab-wrap-hack"; } 1783 } 1784 1785 signal(cm, "renderLine", cm, lineView.line, builder.pre); 1786 if (builder.pre.className) 1787 { builder.textClass = joinClasses(builder.pre.className, builder.textClass || ""); } 1788 1789 return builder 1790 } 1791 1792 function defaultSpecialCharPlaceholder(ch) { 1793 var token = elt("span", "\u2022", "cm-invalidchar"); 1794 token.title = "\\u" + ch.charCodeAt(0).toString(16); 1795 token.setAttribute("aria-label", token.title); 1796 return token 1797 } 1798 1799 // Build up the DOM representation for a single token, and add it to 1800 // the line map. Takes care to render special characters separately. 1801 function buildToken(builder, text, style, startStyle, endStyle, css, attributes) { 1802 if (!text) { return } 1803 var displayText = builder.splitSpaces ? splitSpaces(text, builder.trailingSpace) : text; 1804 var special = builder.cm.state.specialChars, mustWrap = false; 1805 var content; 1806 if (!special.test(text)) { 1807 builder.col += text.length; 1808 content = document.createTextNode(displayText); 1809 builder.map.push(builder.pos, builder.pos + text.length, content); 1810 if (ie && ie_version < 9) { mustWrap = true; } 1811 builder.pos += text.length; 1812 } else { 1813 content = document.createDocumentFragment(); 1814 var pos = 0; 1815 while (true) { 1816 special.lastIndex = pos; 1817 var m = special.exec(text); 1818 var skipped = m ? m.index - pos : text.length - pos; 1819 if (skipped) { 1820 var txt = document.createTextNode(displayText.slice(pos, pos + skipped)); 1821 if (ie && ie_version < 9) { content.appendChild(elt("span", [txt])); } 1822 else { content.appendChild(txt); } 1823 builder.map.push(builder.pos, builder.pos + skipped, txt); 1824 builder.col += skipped; 1825 builder.pos += skipped; 1826 } 1827 if (!m) { break } 1828 pos += skipped + 1; 1829 var txt$1 = (void 0); 1830 if (m[0] == "\t") { 1831 var tabSize = builder.cm.options.tabSize, tabWidth = tabSize - builder.col % tabSize; 1832 txt$1 = content.appendChild(elt("span", spaceStr(tabWidth), "cm-tab")); 1833 txt$1.setAttribute("role", "presentation"); 1834 txt$1.setAttribute("cm-text", "\t"); 1835 builder.col += tabWidth; 1836 } else if (m[0] == "\r" || m[0] == "\n") { 1837 txt$1 = content.appendChild(elt("span", m[0] == "\r" ? "\u240d" : "\u2424", "cm-invalidchar")); 1838 txt$1.setAttribute("cm-text", m[0]); 1839 builder.col += 1; 1840 } else { 1841 txt$1 = builder.cm.options.specialCharPlaceholder(m[0]); 1842 txt$1.setAttribute("cm-text", m[0]); 1843 if (ie && ie_version < 9) { content.appendChild(elt("span", [txt$1])); } 1844 else { content.appendChild(txt$1); } 1845 builder.col += 1; 1846 } 1847 builder.map.push(builder.pos, builder.pos + 1, txt$1); 1848 builder.pos++; 1849 } 1850 } 1851 builder.trailingSpace = displayText.charCodeAt(text.length - 1) == 32; 1852 if (style || startStyle || endStyle || mustWrap || css || attributes) { 1853 var fullStyle = style || ""; 1854 if (startStyle) { fullStyle += startStyle; } 1855 if (endStyle) { fullStyle += endStyle; } 1856 var token = elt("span", [content], fullStyle, css); 1857 if (attributes) { 1858 for (var attr in attributes) { if (attributes.hasOwnProperty(attr) && attr != "style" && attr != "class") 1859 { token.setAttribute(attr, attributes[attr]); } } 1860 } 1861 return builder.content.appendChild(token) 1862 } 1863 builder.content.appendChild(content); 1864 } 1865 1866 // Change some spaces to NBSP to prevent the browser from collapsing 1867 // trailing spaces at the end of a line when rendering text (issue #1362). 1868 function splitSpaces(text, trailingBefore) { 1869 if (text.length > 1 && !/ /.test(text)) { return text } 1870 var spaceBefore = trailingBefore, result = ""; 1871 for (var i = 0; i < text.length; i++) { 1872 var ch = text.charAt(i); 1873 if (ch == " " && spaceBefore && (i == text.length - 1 || text.charCodeAt(i + 1) == 32)) 1874 { ch = "\u00a0"; } 1875 result += ch; 1876 spaceBefore = ch == " "; 1877 } 1878 return result 1879 } 1880 1881 // Work around nonsense dimensions being reported for stretches of 1882 // right-to-left text. 1883 function buildTokenBadBidi(inner, order) { 1884 return function (builder, text, style, startStyle, endStyle, css, attributes) { 1885 style = style ? style + " cm-force-border" : "cm-force-border"; 1886 var start = builder.pos, end = start + text.length; 1887 for (;;) { 1888 // Find the part that overlaps with the start of this text 1889 var part = (void 0); 1890 for (var i = 0; i < order.length; i++) { 1891 part = order[i]; 1892 if (part.to > start && part.from <= start) { break } 1893 } 1894 if (part.to >= end) { return inner(builder, text, style, startStyle, endStyle, css, attributes) } 1895 inner(builder, text.slice(0, part.to - start), style, startStyle, null, css, attributes); 1896 startStyle = null; 1897 text = text.slice(part.to - start); 1898 start = part.to; 1899 } 1900 } 1901 } 1902 1903 function buildCollapsedSpan(builder, size, marker, ignoreWidget) { 1904 var widget = !ignoreWidget && marker.widgetNode; 1905 if (widget) { builder.map.push(builder.pos, builder.pos + size, widget); } 1906 if (!ignoreWidget && builder.cm.display.input.needsContentAttribute) { 1907 if (!widget) 1908 { widget = builder.content.appendChild(document.createElement("span")); } 1909 widget.setAttribute("cm-marker", marker.id); 1910 } 1911 if (widget) { 1912 builder.cm.display.input.setUneditable(widget); 1913 builder.content.appendChild(widget); 1914 } 1915 builder.pos += size; 1916 builder.trailingSpace = false; 1917 } 1918 1919 // Outputs a number of spans to make up a line, taking highlighting 1920 // and marked text into account. 1921 function insertLineContent(line, builder, styles) { 1922 var spans = line.markedSpans, allText = line.text, at = 0; 1923 if (!spans) { 1924 for (var i$1 = 1; i$1 < styles.length; i$1+=2) 1925 { builder.addToken(builder, allText.slice(at, at = styles[i$1]), interpretTokenStyle(styles[i$1+1], builder.cm.options)); } 1926 return 1927 } 1928 1929 var len = allText.length, pos = 0, i = 1, text = "", style, css; 1930 var nextChange = 0, spanStyle, spanEndStyle, spanStartStyle, collapsed, attributes; 1931 for (;;) { 1932 if (nextChange == pos) { // Update current marker set 1933 spanStyle = spanEndStyle = spanStartStyle = css = ""; 1934 attributes = null; 1935 collapsed = null; nextChange = Infinity; 1936 var foundBookmarks = [], endStyles = (void 0); 1937 for (var j = 0; j < spans.length; ++j) { 1938 var sp = spans[j], m = sp.marker; 1939 if (m.type == "bookmark" && sp.from == pos && m.widgetNode) { 1940 foundBookmarks.push(m); 1941 } else if (sp.from <= pos && (sp.to == null || sp.to > pos || m.collapsed && sp.to == pos && sp.from == pos)) { 1942 if (sp.to != null && sp.to != pos && nextChange > sp.to) { 1943 nextChange = sp.to; 1944 spanEndStyle = ""; 1945 } 1946 if (m.className) { spanStyle += " " + m.className; } 1947 if (m.css) { css = (css ? css + ";" : "") + m.css; } 1948 if (m.startStyle && sp.from == pos) { spanStartStyle += " " + m.startStyle; } 1949 if (m.endStyle && sp.to == nextChange) { (endStyles || (endStyles = [])).push(m.endStyle, sp.to); } 1950 // support for the old title property 1951 // https://github.com/codemirror/CodeMirror/pull/5673 1952 if (m.title) { (attributes || (attributes = {})).title = m.title; } 1953 if (m.attributes) { 1954 for (var attr in m.attributes) 1955 { (attributes || (attributes = {}))[attr] = m.attributes[attr]; } 1956 } 1957 if (m.collapsed && (!collapsed || compareCollapsedMarkers(collapsed.marker, m) < 0)) 1958 { collapsed = sp; } 1959 } else if (sp.from > pos && nextChange > sp.from) { 1960 nextChange = sp.from; 1961 } 1962 } 1963 if (endStyles) { for (var j$1 = 0; j$1 < endStyles.length; j$1 += 2) 1964 { if (endStyles[j$1 + 1] == nextChange) { spanEndStyle += " " + endStyles[j$1]; } } } 1965 1966 if (!collapsed || collapsed.from == pos) { for (var j$2 = 0; j$2 < foundBookmarks.length; ++j$2) 1967 { buildCollapsedSpan(builder, 0, foundBookmarks[j$2]); } } 1968 if (collapsed && (collapsed.from || 0) == pos) { 1969 buildCollapsedSpan(builder, (collapsed.to == null ? len + 1 : collapsed.to) - pos, 1970 collapsed.marker, collapsed.from == null); 1971 if (collapsed.to == null) { return } 1972 if (collapsed.to == pos) { collapsed = false; } 1973 } 1974 } 1975 if (pos >= len) { break } 1976 1977 var upto = Math.min(len, nextChange); 1978 while (true) { 1979 if (text) { 1980 var end = pos + text.length; 1981 if (!collapsed) { 1982 var tokenText = end > upto ? text.slice(0, upto - pos) : text; 1983 builder.addToken(builder, tokenText, style ? style + spanStyle : spanStyle, 1984 spanStartStyle, pos + tokenText.length == nextChange ? spanEndStyle : "", css, attributes); 1985 } 1986 if (end >= upto) {text = text.slice(upto - pos); pos = upto; break} 1987 pos = end; 1988 spanStartStyle = ""; 1989 } 1990 text = allText.slice(at, at = styles[i++]); 1991 style = interpretTokenStyle(styles[i++], builder.cm.options); 1992 } 1993 } 1994 } 1995 1996 1997 // These objects are used to represent the visible (currently drawn) 1998 // part of the document. A LineView may correspond to multiple 1999 // logical lines, if those are connected by collapsed ranges. 2000 function LineView(doc, line, lineN) { 2001 // The starting line 2002 this.line = line; 2003 // Continuing lines, if any 2004 this.rest = visualLineContinued(line); 2005 // Number of logical lines in this visual line 2006 this.size = this.rest ? lineNo(lst(this.rest)) - lineN + 1 : 1; 2007 this.node = this.text = null; 2008 this.hidden = lineIsHidden(doc, line); 2009 } 2010 2011 // Create a range of LineView objects for the given lines. 2012 function buildViewArray(cm, from, to) { 2013 var array = [], nextPos; 2014 for (var pos = from; pos < to; pos = nextPos) { 2015 var view = new LineView(cm.doc, getLine(cm.doc, pos), pos); 2016 nextPos = pos + view.size; 2017 array.push(view); 2018 } 2019 return array 2020 } 2021 2022 var operationGroup = null; 2023 2024 function pushOperation(op) { 2025 if (operationGroup) { 2026 operationGroup.ops.push(op); 2027 } else { 2028 op.ownsGroup = operationGroup = { 2029 ops: [op], 2030 delayedCallbacks: [] 2031 }; 2032 } 2033 } 2034 2035 function fireCallbacksForOps(group) { 2036 // Calls delayed callbacks and cursorActivity handlers until no 2037 // new ones appear 2038 var callbacks = group.delayedCallbacks, i = 0; 2039 do { 2040 for (; i < callbacks.length; i++) 2041 { callbacks[i].call(null); } 2042 for (var j = 0; j < group.ops.length; j++) { 2043 var op = group.ops[j]; 2044 if (op.cursorActivityHandlers) 2045 { while (op.cursorActivityCalled < op.cursorActivityHandlers.length) 2046 { op.cursorActivityHandlers[op.cursorActivityCalled++].call(null, op.cm); } } 2047 } 2048 } while (i < callbacks.length) 2049 } 2050 2051 function finishOperation(op, endCb) { 2052 var group = op.ownsGroup; 2053 if (!group) { return } 2054 2055 try { fireCallbacksForOps(group); } 2056 finally { 2057 operationGroup = null; 2058 endCb(group); 2059 } 2060 } 2061 2062 var orphanDelayedCallbacks = null; 2063 2064 // Often, we want to signal events at a point where we are in the 2065 // middle of some work, but don't want the handler to start calling 2066 // other methods on the editor, which might be in an inconsistent 2067 // state or simply not expect any other events to happen. 2068 // signalLater looks whether there are any handlers, and schedules 2069 // them to be executed when the last operation ends, or, if no 2070 // operation is active, when a timeout fires. 2071 function signalLater(emitter, type /*, values...*/) { 2072 var arr = getHandlers(emitter, type); 2073 if (!arr.length) { return } 2074 var args = Array.prototype.slice.call(arguments, 2), list; 2075 if (operationGroup) { 2076 list = operationGroup.delayedCallbacks; 2077 } else if (orphanDelayedCallbacks) { 2078 list = orphanDelayedCallbacks; 2079 } else { 2080 list = orphanDelayedCallbacks = []; 2081 setTimeout(fireOrphanDelayed, 0); 2082 } 2083 var loop = function ( i ) { 2084 list.push(function () { return arr[i].apply(null, args); }); 2085 }; 2086 2087 for (var i = 0; i < arr.length; ++i) 2088 loop( i ); 2089 } 2090 2091 function fireOrphanDelayed() { 2092 var delayed = orphanDelayedCallbacks; 2093 orphanDelayedCallbacks = null; 2094 for (var i = 0; i < delayed.length; ++i) { delayed[i](); } 2095 } 2096 2097 // When an aspect of a line changes, a string is added to 2098 // lineView.changes. This updates the relevant part of the line's 2099 // DOM structure. 2100 function updateLineForChanges(cm, lineView, lineN, dims) { 2101 for (var j = 0; j < lineView.changes.length; j++) { 2102 var type = lineView.changes[j]; 2103 if (type == "text") { updateLineText(cm, lineView); } 2104 else if (type == "gutter") { updateLineGutter(cm, lineView, lineN, dims); } 2105 else if (type == "class") { updateLineClasses(cm, lineView); } 2106 else if (type == "widget") { updateLineWidgets(cm, lineView, dims); } 2107 } 2108 lineView.changes = null; 2109 } 2110 2111 // Lines with gutter elements, widgets or a background class need to 2112 // be wrapped, and have the extra elements added to the wrapper div 2113 function ensureLineWrapped(lineView) { 2114 if (lineView.node == lineView.text) { 2115 lineView.node = elt("div", null, null, "position: relative"); 2116 if (lineView.text.parentNode) 2117 { lineView.text.parentNode.replaceChild(lineView.node, lineView.text); } 2118 lineView.node.appendChild(lineView.text); 2119 if (ie && ie_version < 8) { lineView.node.style.zIndex = 2; } 2120 } 2121 return lineView.node 2122 } 2123 2124 function updateLineBackground(cm, lineView) { 2125 var cls = lineView.bgClass ? lineView.bgClass + " " + (lineView.line.bgClass || "") : lineView.line.bgClass; 2126 if (cls) { cls += " CodeMirror-linebackground"; } 2127 if (lineView.background) { 2128 if (cls) { lineView.background.className = cls; } 2129 else { lineView.background.parentNode.removeChild(lineView.background); lineView.background = null; } 2130 } else if (cls) { 2131 var wrap = ensureLineWrapped(lineView); 2132 lineView.background = wrap.insertBefore(elt("div", null, cls), wrap.firstChild); 2133 cm.display.input.setUneditable(lineView.background); 2134 } 2135 } 2136 2137 // Wrapper around buildLineContent which will reuse the structure 2138 // in display.externalMeasured when possible. 2139 function getLineContent(cm, lineView) { 2140 var ext = cm.display.externalMeasured; 2141 if (ext && ext.line == lineView.line) { 2142 cm.display.externalMeasured = null; 2143 lineView.measure = ext.measure; 2144 return ext.built 2145 } 2146 return buildLineContent(cm, lineView) 2147 } 2148 2149 // Redraw the line's text. Interacts with the background and text 2150 // classes because the mode may output tokens that influence these 2151 // classes. 2152 function updateLineText(cm, lineView) { 2153 var cls = lineView.text.className; 2154 var built = getLineContent(cm, lineView); 2155 if (lineView.text == lineView.node) { lineView.node = built.pre; } 2156 lineView.text.parentNode.replaceChild(built.pre, lineView.text); 2157 lineView.text = built.pre; 2158 if (built.bgClass != lineView.bgClass || built.textClass != lineView.textClass) { 2159 lineView.bgClass = built.bgClass; 2160 lineView.textClass = built.textClass; 2161 updateLineClasses(cm, lineView); 2162 } else if (cls) { 2163 lineView.text.className = cls; 2164 } 2165 } 2166 2167 function updateLineClasses(cm, lineView) { 2168 updateLineBackground(cm, lineView); 2169 if (lineView.line.wrapClass) 2170 { ensureLineWrapped(lineView).className = lineView.line.wrapClass; } 2171 else if (lineView.node != lineView.text) 2172 { lineView.node.className = ""; } 2173 var textClass = lineView.textClass ? lineView.textClass + " " + (lineView.line.textClass || "") : lineView.line.textClass; 2174 lineView.text.className = textClass || ""; 2175 } 2176 2177 function updateLineGutter(cm, lineView, lineN, dims) { 2178 if (lineView.gutter) { 2179 lineView.node.removeChild(lineView.gutter); 2180 lineView.gutter = null; 2181 } 2182 if (lineView.gutterBackground) { 2183 lineView.node.removeChild(lineView.gutterBackground); 2184 lineView.gutterBackground = null; 2185 } 2186 if (lineView.line.gutterClass) { 2187 var wrap = ensureLineWrapped(lineView); 2188 lineView.gutterBackground = elt("div", null, "CodeMirror-gutter-background " + lineView.line.gutterClass, 2189 ("left: " + (cm.options.fixedGutter ? dims.fixedPos : -dims.gutterTotalWidth) + "px; width: " + (dims.gutterTotalWidth) + "px")); 2190 cm.display.input.setUneditable(lineView.gutterBackground); 2191 wrap.insertBefore(lineView.gutterBackground, lineView.text); 2192 } 2193 var markers = lineView.line.gutterMarkers; 2194 if (cm.options.lineNumbers || markers) { 2195 var wrap$1 = ensureLineWrapped(lineView); 2196 var gutterWrap = lineView.gutter = elt("div", null, "CodeMirror-gutter-wrapper", ("left: " + (cm.options.fixedGutter ? dims.fixedPos : -dims.gutterTotalWidth) + "px")); 2197 gutterWrap.setAttribute("aria-hidden", "true"); 2198 cm.display.input.setUneditable(gutterWrap); 2199 wrap$1.insertBefore(gutterWrap, lineView.text); 2200 if (lineView.line.gutterClass) 2201 { gutterWrap.className += " " + lineView.line.gutterClass; } 2202 if (cm.options.lineNumbers && (!markers || !markers["CodeMirror-linenumbers"])) 2203 { lineView.lineNumber = gutterWrap.appendChild( 2204 elt("div", lineNumberFor(cm.options, lineN), 2205 "CodeMirror-linenumber CodeMirror-gutter-elt", 2206 ("left: " + (dims.gutterLeft["CodeMirror-linenumbers"]) + "px; width: " + (cm.display.lineNumInnerWidth) + "px"))); } 2207 if (markers) { for (var k = 0; k < cm.display.gutterSpecs.length; ++k) { 2208 var id = cm.display.gutterSpecs[k].className, found = markers.hasOwnProperty(id) && markers[id]; 2209 if (found) 2210 { gutterWrap.appendChild(elt("div", [found], "CodeMirror-gutter-elt", 2211 ("left: " + (dims.gutterLeft[id]) + "px; width: " + (dims.gutterWidth[id]) + "px"))); } 2212 } } 2213 } 2214 } 2215 2216 function updateLineWidgets(cm, lineView, dims) { 2217 if (lineView.alignable) { lineView.alignable = null; } 2218 var isWidget = classTest("CodeMirror-linewidget"); 2219 for (var node = lineView.node.firstChild, next = (void 0); node; node = next) { 2220 next = node.nextSibling; 2221 if (isWidget.test(node.className)) { lineView.node.removeChild(node); } 2222 } 2223 insertLineWidgets(cm, lineView, dims); 2224 } 2225 2226 // Build a line's DOM representation from scratch 2227 function buildLineElement(cm, lineView, lineN, dims) { 2228 var built = getLineContent(cm, lineView); 2229 lineView.text = lineView.node = built.pre; 2230 if (built.bgClass) { lineView.bgClass = built.bgClass; } 2231 if (built.textClass) { lineView.textClass = built.textClass; } 2232 2233 updateLineClasses(cm, lineView); 2234 updateLineGutter(cm, lineView, lineN, dims); 2235 insertLineWidgets(cm, lineView, dims); 2236 return lineView.node 2237 } 2238 2239 // A lineView may contain multiple logical lines (when merged by 2240 // collapsed spans). The widgets for all of them need to be drawn. 2241 function insertLineWidgets(cm, lineView, dims) { 2242 insertLineWidgetsFor(cm, lineView.line, lineView, dims, true); 2243 if (lineView.rest) { for (var i = 0; i < lineView.rest.length; i++) 2244 { insertLineWidgetsFor(cm, lineView.rest[i], lineView, dims, false); } } 2245 } 2246 2247 function insertLineWidgetsFor(cm, line, lineView, dims, allowAbove) { 2248 if (!line.widgets) { return } 2249 var wrap = ensureLineWrapped(lineView); 2250 for (var i = 0, ws = line.widgets; i < ws.length; ++i) { 2251 var widget = ws[i], node = elt("div", [widget.node], "CodeMirror-linewidget" + (widget.className ? " " + widget.className : "")); 2252 if (!widget.handleMouseEvents) { node.setAttribute("cm-ignore-events", "true"); } 2253 positionLineWidget(widget, node, lineView, dims); 2254 cm.display.input.setUneditable(node); 2255 if (allowAbove && widget.above) 2256 { wrap.insertBefore(node, lineView.gutter || lineView.text); } 2257 else 2258 { wrap.appendChild(node); } 2259 signalLater(widget, "redraw"); 2260 } 2261 } 2262 2263 function positionLineWidget(widget, node, lineView, dims) { 2264 if (widget.noHScroll) { 2265 (lineView.alignable || (lineView.alignable = [])).push(node); 2266 var width = dims.wrapperWidth; 2267 node.style.left = dims.fixedPos + "px"; 2268 if (!widget.coverGutter) { 2269 width -= dims.gutterTotalWidth; 2270 node.style.paddingLeft = dims.gutterTotalWidth + "px"; 2271 } 2272 node.style.width = width + "px"; 2273 } 2274 if (widget.coverGutter) { 2275 node.style.zIndex = 5; 2276 node.style.position = "relative"; 2277 if (!widget.noHScroll) { node.style.marginLeft = -dims.gutterTotalWidth + "px"; } 2278 } 2279 } 2280 2281 function widgetHeight(widget) { 2282 if (widget.height != null) { return widget.height } 2283 var cm = widget.doc.cm; 2284 if (!cm) { return 0 } 2285 if (!contains(document.body, widget.node)) { 2286 var parentStyle = "position: relative;"; 2287 if (widget.coverGutter) 2288 { parentStyle += "margin-left: -" + cm.display.gutters.offsetWidth + "px;"; } 2289 if (widget.noHScroll) 2290 { parentStyle += "width: " + cm.display.wrapper.clientWidth + "px;"; } 2291 removeChildrenAndAdd(cm.display.measure, elt("div", [widget.node], null, parentStyle)); 2292 } 2293 return widget.height = widget.node.parentNode.offsetHeight 2294 } 2295 2296 // Return true when the given mouse event happened in a widget 2297 function eventInWidget(display, e) { 2298 for (var n = e_target(e); n != display.wrapper; n = n.parentNode) { 2299 if (!n || (n.nodeType == 1 && n.getAttribute("cm-ignore-events") == "true") || 2300 (n.parentNode == display.sizer && n != display.mover)) 2301 { return true } 2302 } 2303 } 2304 2305 // POSITION MEASUREMENT 2306 2307 function paddingTop(display) {return display.lineSpace.offsetTop} 2308 function paddingVert(display) {return display.mover.offsetHeight - display.lineSpace.offsetHeight} 2309 function paddingH(display) { 2310 if (display.cachedPaddingH) { return display.cachedPaddingH } 2311 var e = removeChildrenAndAdd(display.measure, elt("pre", "x", "CodeMirror-line-like")); 2312 var style = window.getComputedStyle ? window.getComputedStyle(e) : e.currentStyle; 2313 var data = {left: parseInt(style.paddingLeft), right: parseInt(style.paddingRight)}; 2314 if (!isNaN(data.left) && !isNaN(data.right)) { display.cachedPaddingH = data; } 2315 return data 2316 } 2317 2318 function scrollGap(cm) { return scrollerGap - cm.display.nativeBarWidth } 2319 function displayWidth(cm) { 2320 return cm.display.scroller.clientWidth - scrollGap(cm) - cm.display.barWidth 2321 } 2322 function displayHeight(cm) { 2323 return cm.display.scroller.clientHeight - scrollGap(cm) - cm.display.barHeight 2324 } 2325 2326 // Ensure the lineView.wrapping.heights array is populated. This is 2327 // an array of bottom offsets for the lines that make up a drawn 2328 // line. When lineWrapping is on, there might be more than one 2329 // height. 2330 function ensureLineHeights(cm, lineView, rect) { 2331 var wrapping = cm.options.lineWrapping; 2332 var curWidth = wrapping && displayWidth(cm); 2333 if (!lineView.measure.heights || wrapping && lineView.measure.width != curWidth) { 2334 var heights = lineView.measure.heights = []; 2335 if (wrapping) { 2336 lineView.measure.width = curWidth; 2337 var rects = lineView.text.firstChild.getClientRects(); 2338 for (var i = 0; i < rects.length - 1; i++) { 2339 var cur = rects[i], next = rects[i + 1]; 2340 if (Math.abs(cur.bottom - next.bottom) > 2) 2341 { heights.push((cur.bottom + next.top) / 2 - rect.top); } 2342 } 2343 } 2344 heights.push(rect.bottom - rect.top); 2345 } 2346 } 2347 2348 // Find a line map (mapping character offsets to text nodes) and a 2349 // measurement cache for the given line number. (A line view might 2350 // contain multiple lines when collapsed ranges are present.) 2351 function mapFromLineView(lineView, line, lineN) { 2352 if (lineView.line == line) 2353 { return {map: lineView.measure.map, cache: lineView.measure.cache} } 2354 if (lineView.rest) { 2355 for (var i = 0; i < lineView.rest.length; i++) 2356 { if (lineView.rest[i] == line) 2357 { return {map: lineView.measure.maps[i], cache: lineView.measure.caches[i]} } } 2358 for (var i$1 = 0; i$1 < lineView.rest.length; i$1++) 2359 { if (lineNo(lineView.rest[i$1]) > lineN) 2360 { return {map: lineView.measure.maps[i$1], cache: lineView.measure.caches[i$1], before: true} } } 2361 } 2362 } 2363 2364 // Render a line into the hidden node display.externalMeasured. Used 2365 // when measurement is needed for a line that's not in the viewport. 2366 function updateExternalMeasurement(cm, line) { 2367 line = visualLine(line); 2368 var lineN = lineNo(line); 2369 var view = cm.display.externalMeasured = new LineView(cm.doc, line, lineN); 2370 view.lineN = lineN; 2371 var built = view.built = buildLineContent(cm, view); 2372 view.text = built.pre; 2373 removeChildrenAndAdd(cm.display.lineMeasure, built.pre); 2374 return view 2375 } 2376 2377 // Get a {top, bottom, left, right} box (in line-local coordinates) 2378 // for a given character. 2379 function measureChar(cm, line, ch, bias) { 2380 return measureCharPrepared(cm, prepareMeasureForLine(cm, line), ch, bias) 2381 } 2382 2383 // Find a line view that corresponds to the given line number. 2384 function findViewForLine(cm, lineN) { 2385 if (lineN >= cm.display.viewFrom && lineN < cm.display.viewTo) 2386 { return cm.display.view[findViewIndex(cm, lineN)] } 2387 var ext = cm.display.externalMeasured; 2388 if (ext && lineN >= ext.lineN && lineN < ext.lineN + ext.size) 2389 { return ext } 2390 } 2391 2392 // Measurement can be split in two steps, the set-up work that 2393 // applies to the whole line, and the measurement of the actual 2394 // character. Functions like coordsChar, that need to do a lot of 2395 // measurements in a row, can thus ensure that the set-up work is 2396 // only done once. 2397 function prepareMeasureForLine(cm, line) { 2398 var lineN = lineNo(line); 2399 var view = findViewForLine(cm, lineN); 2400 if (view && !view.text) { 2401 view = null; 2402 } else if (view && view.changes) { 2403 updateLineForChanges(cm, view, lineN, getDimensions(cm)); 2404 cm.curOp.forceUpdate = true; 2405 } 2406 if (!view) 2407 { view = updateExternalMeasurement(cm, line); } 2408 2409 var info = mapFromLineView(view, line, lineN); 2410 return { 2411 line: line, view: view, rect: null, 2412 map: info.map, cache: info.cache, before: info.before, 2413 hasHeights: false 2414 } 2415 } 2416 2417 // Given a prepared measurement object, measures the position of an 2418 // actual character (or fetches it from the cache). 2419 function measureCharPrepared(cm, prepared, ch, bias, varHeight) { 2420 if (prepared.before) { ch = -1; } 2421 var key = ch + (bias || ""), found; 2422 if (prepared.cache.hasOwnProperty(key)) { 2423 found = prepared.cache[key]; 2424 } else { 2425 if (!prepared.rect) 2426 { prepared.rect = prepared.view.text.getBoundingClientRect(); } 2427 if (!prepared.hasHeights) { 2428 ensureLineHeights(cm, prepared.view, prepared.rect); 2429 prepared.hasHeights = true; 2430 } 2431 found = measureCharInner(cm, prepared, ch, bias); 2432 if (!found.bogus) { prepared.cache[key] = found; } 2433 } 2434 return {left: found.left, right: found.right, 2435 top: varHeight ? found.rtop : found.top, 2436 bottom: varHeight ? found.rbottom : found.bottom} 2437 } 2438 2439 var nullRect = {left: 0, right: 0, top: 0, bottom: 0}; 2440 2441 function nodeAndOffsetInLineMap(map, ch, bias) { 2442 var node, start, end, collapse, mStart, mEnd; 2443 // First, search the line map for the text node corresponding to, 2444 // or closest to, the target character. 2445 for (var i = 0; i < map.length; i += 3) { 2446 mStart = map[i]; 2447 mEnd = map[i + 1]; 2448 if (ch < mStart) { 2449 start = 0; end = 1; 2450 collapse = "left"; 2451 } else if (ch < mEnd) { 2452 start = ch - mStart; 2453 end = start + 1; 2454 } else if (i == map.length - 3 || ch == mEnd && map[i + 3] > ch) { 2455 end = mEnd - mStart; 2456 start = end - 1; 2457 if (ch >= mEnd) { collapse = "right"; } 2458 } 2459 if (start != null) { 2460 node = map[i + 2]; 2461 if (mStart == mEnd && bias == (node.insertLeft ? "left" : "right")) 2462 { collapse = bias; } 2463 if (bias == "left" && start == 0) 2464 { while (i && map[i - 2] == map[i - 3] && map[i - 1].insertLeft) { 2465 node = map[(i -= 3) + 2]; 2466 collapse = "left"; 2467 } } 2468 if (bias == "right" && start == mEnd - mStart) 2469 { while (i < map.length - 3 && map[i + 3] == map[i + 4] && !map[i + 5].insertLeft) { 2470 node = map[(i += 3) + 2]; 2471 collapse = "right"; 2472 } } 2473 break 2474 } 2475 } 2476 return {node: node, start: start, end: end, collapse: collapse, coverStart: mStart, coverEnd: mEnd} 2477 } 2478 2479 function getUsefulRect(rects, bias) { 2480 var rect = nullRect; 2481 if (bias == "left") { for (var i = 0; i < rects.length; i++) { 2482 if ((rect = rects[i]).left != rect.right) { break } 2483 } } else { for (var i$1 = rects.length - 1; i$1 >= 0; i$1--) { 2484 if ((rect = rects[i$1]).left != rect.right) { break } 2485 } } 2486 return rect 2487 } 2488 2489 function measureCharInner(cm, prepared, ch, bias) { 2490 var place = nodeAndOffsetInLineMap(prepared.map, ch, bias); 2491 var node = place.node, start = place.start, end = place.end, collapse = place.collapse; 2492 2493 var rect; 2494 if (node.nodeType == 3) { // If it is a text node, use a range to retrieve the coordinates. 2495 for (var i$1 = 0; i$1 < 4; i$1++) { // Retry a maximum of 4 times when nonsense rectangles are returned 2496 while (start && isExtendingChar(prepared.line.text.charAt(place.coverStart + start))) { --start; } 2497 while (place.coverStart + end < place.coverEnd && isExtendingChar(prepared.line.text.charAt(place.coverStart + end))) { ++end; } 2498 if (ie && ie_version < 9 && start == 0 && end == place.coverEnd - place.coverStart) 2499 { rect = node.parentNode.getBoundingClientRect(); } 2500 else 2501 { rect = getUsefulRect(range(node, start, end).getClientRects(), bias); } 2502 if (rect.left || rect.right || start == 0) { break } 2503 end = start; 2504 start = start - 1; 2505 collapse = "right"; 2506 } 2507 if (ie && ie_version < 11) { rect = maybeUpdateRectForZooming(cm.display.measure, rect); } 2508 } else { // If it is a widget, simply get the box for the whole widget. 2509 if (start > 0) { collapse = bias = "right"; } 2510 var rects; 2511 if (cm.options.lineWrapping && (rects = node.getClientRects()).length > 1) 2512 { rect = rects[bias == "right" ? rects.length - 1 : 0]; } 2513 else 2514 { rect = node.getBoundingClientRect(); } 2515 } 2516 if (ie && ie_version < 9 && !start && (!rect || !rect.left && !rect.right)) { 2517 var rSpan = node.parentNode.getClientRects()[0]; 2518 if (rSpan) 2519 { rect = {left: rSpan.left, right: rSpan.left + charWidth(cm.display), top: rSpan.top, bottom: rSpan.bottom}; } 2520 else 2521 { rect = nullRect; } 2522 } 2523 2524 var rtop = rect.top - prepared.rect.top, rbot = rect.bottom - prepared.rect.top; 2525 var mid = (rtop + rbot) / 2; 2526 var heights = prepared.view.measure.heights; 2527 var i = 0; 2528 for (; i < heights.length - 1; i++) 2529 { if (mid < heights[i]) { break } } 2530 var top = i ? heights[i - 1] : 0, bot = heights[i]; 2531 var result = {left: (collapse == "right" ? rect.right : rect.left) - prepared.rect.left, 2532 right: (collapse == "left" ? rect.left : rect.right) - prepared.rect.left, 2533 top: top, bottom: bot}; 2534 if (!rect.left && !rect.right) { result.bogus = true; } 2535 if (!cm.options.singleCursorHeightPerLine) { result.rtop = rtop; result.rbottom = rbot; } 2536 2537 return result 2538 } 2539 2540 // Work around problem with bounding client rects on ranges being 2541 // returned incorrectly when zoomed on IE10 and below. 2542 function maybeUpdateRectForZooming(measure, rect) { 2543 if (!window.screen || screen.logicalXDPI == null || 2544 screen.logicalXDPI == screen.deviceXDPI || !hasBadZoomedRects(measure)) 2545 { return rect } 2546 var scaleX = screen.logicalXDPI / screen.deviceXDPI; 2547 var scaleY = screen.logicalYDPI / screen.deviceYDPI; 2548 return {left: rect.left * scaleX, right: rect.right * scaleX, 2549 top: rect.top * scaleY, bottom: rect.bottom * scaleY} 2550 } 2551 2552 function clearLineMeasurementCacheFor(lineView) { 2553 if (lineView.measure) { 2554 lineView.measure.cache = {}; 2555 lineView.measure.heights = null; 2556 if (lineView.rest) { for (var i = 0; i < lineView.rest.length; i++) 2557 { lineView.measure.caches[i] = {}; } } 2558 } 2559 } 2560 2561 function clearLineMeasurementCache(cm) { 2562 cm.display.externalMeasure = null; 2563 removeChildren(cm.display.lineMeasure); 2564 for (var i = 0; i < cm.display.view.length; i++) 2565 { clearLineMeasurementCacheFor(cm.display.view[i]); } 2566 } 2567 2568 function clearCaches(cm) { 2569 clearLineMeasurementCache(cm); 2570 cm.display.cachedCharWidth = cm.display.cachedTextHeight = cm.display.cachedPaddingH = null; 2571 if (!cm.options.lineWrapping) { cm.display.maxLineChanged = true; } 2572 cm.display.lineNumChars = null; 2573 } 2574 2575 function pageScrollX() { 2576 // Work around https://bugs.chromium.org/p/chromium/issues/detail?id=489206 2577 // which causes page_Offset and bounding client rects to use 2578 // different reference viewports and invalidate our calculations. 2579 if (chrome && android) { return -(document.body.getBoundingClientRect().left - parseInt(getComputedStyle(document.body).marginLeft)) } 2580 return window.pageXOffset || (document.documentElement || document.body).scrollLeft 2581 } 2582 function pageScrollY() { 2583 if (chrome && android) { return -(document.body.getBoundingClientRect().top - parseInt(getComputedStyle(document.body).marginTop)) } 2584 return window.pageYOffset || (document.documentElement || document.body).scrollTop 2585 } 2586 2587 function widgetTopHeight(lineObj) { 2588 var height = 0; 2589 if (lineObj.widgets) { for (var i = 0; i < lineObj.widgets.length; ++i) { if (lineObj.widgets[i].above) 2590 { height += widgetHeight(lineObj.widgets[i]); } } } 2591 return height 2592 } 2593 2594 // Converts a {top, bottom, left, right} box from line-local 2595 // coordinates into another coordinate system. Context may be one of 2596 // "line", "div" (display.lineDiv), "local"./null (editor), "window", 2597 // or "page". 2598 function intoCoordSystem(cm, lineObj, rect, context, includeWidgets) { 2599 if (!includeWidgets) { 2600 var height = widgetTopHeight(lineObj); 2601 rect.top += height; rect.bottom += height; 2602 } 2603 if (context == "line") { return rect } 2604 if (!context) { context = "local"; } 2605 var yOff = heightAtLine(lineObj); 2606 if (context == "local") { yOff += paddingTop(cm.display); } 2607 else { yOff -= cm.display.viewOffset; } 2608 if (context == "page" || context == "window") { 2609 var lOff = cm.display.lineSpace.getBoundingClientRect(); 2610 yOff += lOff.top + (context == "window" ? 0 : pageScrollY()); 2611 var xOff = lOff.left + (context == "window" ? 0 : pageScrollX()); 2612 rect.left += xOff; rect.right += xOff; 2613 } 2614 rect.top += yOff; rect.bottom += yOff; 2615 return rect 2616 } 2617 2618 // Coverts a box from "div" coords to another coordinate system. 2619 // Context may be "window", "page", "div", or "local"./null. 2620 function fromCoordSystem(cm, coords, context) { 2621 if (context == "div") { return coords } 2622 var left = coords.left, top = coords.top; 2623 // First move into "page" coordinate system 2624 if (context == "page") { 2625 left -= pageScrollX(); 2626 top -= pageScrollY(); 2627 } else if (context == "local" || !context) { 2628 var localBox = cm.display.sizer.getBoundingClientRect(); 2629 left += localBox.left; 2630 top += localBox.top; 2631 } 2632 2633 var lineSpaceBox = cm.display.lineSpace.getBoundingClientRect(); 2634 return {left: left - lineSpaceBox.left, top: top - lineSpaceBox.top} 2635 } 2636 2637 function charCoords(cm, pos, context, lineObj, bias) { 2638 if (!lineObj) { lineObj = getLine(cm.doc, pos.line); } 2639 return intoCoordSystem(cm, lineObj, measureChar(cm, lineObj, pos.ch, bias), context) 2640 } 2641 2642 // Returns a box for a given cursor position, which may have an 2643 // 'other' property containing the position of the secondary cursor 2644 // on a bidi boundary. 2645 // A cursor Pos(line, char, "before") is on the same visual line as `char - 1` 2646 // and after `char - 1` in writing order of `char - 1` 2647 // A cursor Pos(line, char, "after") is on the same visual line as `char` 2648 // and before `char` in writing order of `char` 2649 // Examples (upper-case letters are RTL, lower-case are LTR): 2650 // Pos(0, 1, ...) 2651 // before after 2652 // ab a|b a|b 2653 // aB a|B aB| 2654 // Ab |Ab A|b 2655 // AB B|A B|A 2656 // Every position after the last character on a line is considered to stick 2657 // to the last character on the line. 2658 function cursorCoords(cm, pos, context, lineObj, preparedMeasure, varHeight) { 2659 lineObj = lineObj || getLine(cm.doc, pos.line); 2660 if (!preparedMeasure) { preparedMeasure = prepareMeasureForLine(cm, lineObj); } 2661 function get(ch, right) { 2662 var m = measureCharPrepared(cm, preparedMeasure, ch, right ? "right" : "left", varHeight); 2663 if (right) { m.left = m.right; } else { m.right = m.left; } 2664 return intoCoordSystem(cm, lineObj, m, context) 2665 } 2666 var order = getOrder(lineObj, cm.doc.direction), ch = pos.ch, sticky = pos.sticky; 2667 if (ch >= lineObj.text.length) { 2668 ch = lineObj.text.length; 2669 sticky = "before"; 2670 } else if (ch <= 0) { 2671 ch = 0; 2672 sticky = "after"; 2673 } 2674 if (!order) { return get(sticky == "before" ? ch - 1 : ch, sticky == "before") } 2675 2676 function getBidi(ch, partPos, invert) { 2677 var part = order[partPos], right = part.level == 1; 2678 return get(invert ? ch - 1 : ch, right != invert) 2679 } 2680 var partPos = getBidiPartAt(order, ch, sticky); 2681 var other = bidiOther; 2682 var val = getBidi(ch, partPos, sticky == "before"); 2683 if (other != null) { val.other = getBidi(ch, other, sticky != "before"); } 2684 return val 2685 } 2686 2687 // Used to cheaply estimate the coordinates for a position. Used for 2688 // intermediate scroll updates. 2689 function estimateCoords(cm, pos) { 2690 var left = 0; 2691 pos = clipPos(cm.doc, pos); 2692 if (!cm.options.lineWrapping) { left = charWidth(cm.display) * pos.ch; } 2693 var lineObj = getLine(cm.doc, pos.line); 2694 var top = heightAtLine(lineObj) + paddingTop(cm.display); 2695 return {left: left, right: left, top: top, bottom: top + lineObj.height} 2696 } 2697 2698 // Positions returned by coordsChar contain some extra information. 2699 // xRel is the relative x position of the input coordinates compared 2700 // to the found position (so xRel > 0 means the coordinates are to 2701 // the right of the character position, for example). When outside 2702 // is true, that means the coordinates lie outside the line's 2703 // vertical range. 2704 function PosWithInfo(line, ch, sticky, outside, xRel) { 2705 var pos = Pos(line, ch, sticky); 2706 pos.xRel = xRel; 2707 if (outside) { pos.outside = outside; } 2708 return pos 2709 } 2710 2711 // Compute the character position closest to the given coordinates. 2712 // Input must be lineSpace-local ("div" coordinate system). 2713 function coordsChar(cm, x, y) { 2714 var doc = cm.doc; 2715 y += cm.display.viewOffset; 2716 if (y < 0) { return PosWithInfo(doc.first, 0, null, -1, -1) } 2717 var lineN = lineAtHeight(doc, y), last = doc.first + doc.size - 1; 2718 if (lineN > last) 2719 { return PosWithInfo(doc.first + doc.size - 1, getLine(doc, last).text.length, null, 1, 1) } 2720 if (x < 0) { x = 0; } 2721 2722 var lineObj = getLine(doc, lineN); 2723 for (;;) { 2724 var found = coordsCharInner(cm, lineObj, lineN, x, y); 2725 var collapsed = collapsedSpanAround(lineObj, found.ch + (found.xRel > 0 || found.outside > 0 ? 1 : 0)); 2726 if (!collapsed) { return found } 2727 var rangeEnd = collapsed.find(1); 2728 if (rangeEnd.line == lineN) { return rangeEnd } 2729 lineObj = getLine(doc, lineN = rangeEnd.line); 2730 } 2731 } 2732 2733 function wrappedLineExtent(cm, lineObj, preparedMeasure, y) { 2734 y -= widgetTopHeight(lineObj); 2735 var end = lineObj.text.length; 2736 var begin = findFirst(function (ch) { return measureCharPrepared(cm, preparedMeasure, ch - 1).bottom <= y; }, end, 0); 2737 end = findFirst(function (ch) { return measureCharPrepared(cm, preparedMeasure, ch).top > y; }, begin, end); 2738 return {begin: begin, end: end} 2739 } 2740 2741 function wrappedLineExtentChar(cm, lineObj, preparedMeasure, target) { 2742 if (!preparedMeasure) { preparedMeasure = prepareMeasureForLine(cm, lineObj); } 2743 var targetTop = intoCoordSystem(cm, lineObj, measureCharPrepared(cm, preparedMeasure, target), "line").top; 2744 return wrappedLineExtent(cm, lineObj, preparedMeasure, targetTop) 2745 } 2746 2747 // Returns true if the given side of a box is after the given 2748 // coordinates, in top-to-bottom, left-to-right order. 2749 function boxIsAfter(box, x, y, left) { 2750 return box.bottom <= y ? false : box.top > y ? true : (left ? box.left : box.right) > x 2751 } 2752 2753 function coordsCharInner(cm, lineObj, lineNo, x, y) { 2754 // Move y into line-local coordinate space 2755 y -= heightAtLine(lineObj); 2756 var preparedMeasure = prepareMeasureForLine(cm, lineObj); 2757 // When directly calling `measureCharPrepared`, we have to adjust 2758 // for the widgets at this line. 2759 var widgetHeight = widgetTopHeight(lineObj); 2760 var begin = 0, end = lineObj.text.length, ltr = true; 2761 2762 var order = getOrder(lineObj, cm.doc.direction); 2763 // If the line isn't plain left-to-right text, first figure out 2764 // which bidi section the coordinates fall into. 2765 if (order) { 2766 var part = (cm.options.lineWrapping ? coordsBidiPartWrapped : coordsBidiPart) 2767 (cm, lineObj, lineNo, preparedMeasure, order, x, y); 2768 ltr = part.level != 1; 2769 // The awkward -1 offsets are needed because findFirst (called 2770 // on these below) will treat its first bound as inclusive, 2771 // second as exclusive, but we want to actually address the 2772 // characters in the part's range 2773 begin = ltr ? part.from : part.to - 1; 2774 end = ltr ? part.to : part.from - 1; 2775 } 2776 2777 // A binary search to find the first character whose bounding box 2778 // starts after the coordinates. If we run across any whose box wrap 2779 // the coordinates, store that. 2780 var chAround = null, boxAround = null; 2781 var ch = findFirst(function (ch) { 2782 var box = measureCharPrepared(cm, preparedMeasure, ch); 2783 box.top += widgetHeight; box.bottom += widgetHeight; 2784 if (!boxIsAfter(box, x, y, false)) { return false } 2785 if (box.top <= y && box.left <= x) { 2786 chAround = ch; 2787 boxAround = box; 2788 } 2789 return true 2790 }, begin, end); 2791 2792 var baseX, sticky, outside = false; 2793 // If a box around the coordinates was found, use that 2794 if (boxAround) { 2795 // Distinguish coordinates nearer to the left or right side of the box 2796 var atLeft = x - boxAround.left < boxAround.right - x, atStart = atLeft == ltr; 2797 ch = chAround + (atStart ? 0 : 1); 2798 sticky = atStart ? "after" : "before"; 2799 baseX = atLeft ? boxAround.left : boxAround.right; 2800 } else { 2801 // (Adjust for extended bound, if necessary.) 2802 if (!ltr && (ch == end || ch == begin)) { ch++; } 2803 // To determine which side to associate with, get the box to the 2804 // left of the character and compare it's vertical position to the 2805 // coordinates 2806 sticky = ch == 0 ? "after" : ch == lineObj.text.length ? "before" : 2807 (measureCharPrepared(cm, preparedMeasure, ch - (ltr ? 1 : 0)).bottom + widgetHeight <= y) == ltr ? 2808 "after" : "before"; 2809 // Now get accurate coordinates for this place, in order to get a 2810 // base X position 2811 var coords = cursorCoords(cm, Pos(lineNo, ch, sticky), "line", lineObj, preparedMeasure); 2812 baseX = coords.left; 2813 outside = y < coords.top ? -1 : y >= coords.bottom ? 1 : 0; 2814 } 2815 2816 ch = skipExtendingChars(lineObj.text, ch, 1); 2817 return PosWithInfo(lineNo, ch, sticky, outside, x - baseX) 2818 } 2819 2820 function coordsBidiPart(cm, lineObj, lineNo, preparedMeasure, order, x, y) { 2821 // Bidi parts are sorted left-to-right, and in a non-line-wrapping 2822 // situation, we can take this ordering to correspond to the visual 2823 // ordering. This finds the first part whose end is after the given 2824 // coordinates. 2825 var index = findFirst(function (i) { 2826 var part = order[i], ltr = part.level != 1; 2827 return boxIsAfter(cursorCoords(cm, Pos(lineNo, ltr ? part.to : part.from, ltr ? "before" : "after"), 2828 "line", lineObj, preparedMeasure), x, y, true) 2829 }, 0, order.length - 1); 2830 var part = order[index]; 2831 // If this isn't the first part, the part's start is also after 2832 // the coordinates, and the coordinates aren't on the same line as 2833 // that start, move one part back. 2834 if (index > 0) { 2835 var ltr = part.level != 1; 2836 var start = cursorCoords(cm, Pos(lineNo, ltr ? part.from : part.to, ltr ? "after" : "before"), 2837 "line", lineObj, preparedMeasure); 2838 if (boxIsAfter(start, x, y, true) && start.top > y) 2839 { part = order[index - 1]; } 2840 } 2841 return part 2842 } 2843 2844 function coordsBidiPartWrapped(cm, lineObj, _lineNo, preparedMeasure, order, x, y) { 2845 // In a wrapped line, rtl text on wrapping boundaries can do things 2846 // that don't correspond to the ordering in our `order` array at 2847 // all, so a binary search doesn't work, and we want to return a 2848 // part that only spans one line so that the binary search in 2849 // coordsCharInner is safe. As such, we first find the extent of the 2850 // wrapped line, and then do a flat search in which we discard any 2851 // spans that aren't on the line. 2852 var ref = wrappedLineExtent(cm, lineObj, preparedMeasure, y); 2853 var begin = ref.begin; 2854 var end = ref.end; 2855 if (/\s/.test(lineObj.text.charAt(end - 1))) { end--; } 2856 var part = null, closestDist = null; 2857 for (var i = 0; i < order.length; i++) { 2858 var p = order[i]; 2859 if (p.from >= end || p.to <= begin) { continue } 2860 var ltr = p.level != 1; 2861 var endX = measureCharPrepared(cm, preparedMeasure, ltr ? Math.min(end, p.to) - 1 : Math.max(begin, p.from)).right; 2862 // Weigh against spans ending before this, so that they are only 2863 // picked if nothing ends after 2864 var dist = endX < x ? x - endX + 1e9 : endX - x; 2865 if (!part || closestDist > dist) { 2866 part = p; 2867 closestDist = dist; 2868 } 2869 } 2870 if (!part) { part = order[order.length - 1]; } 2871 // Clip the part to the wrapped line. 2872 if (part.from < begin) { part = {from: begin, to: part.to, level: part.level}; } 2873 if (part.to > end) { part = {from: part.from, to: end, level: part.level}; } 2874 return part 2875 } 2876 2877 var measureText; 2878 // Compute the default text height. 2879 function textHeight(display) { 2880 if (display.cachedTextHeight != null) { return display.cachedTextHeight } 2881 if (measureText == null) { 2882 measureText = elt("pre", null, "CodeMirror-line-like"); 2883 // Measure a bunch of lines, for browsers that compute 2884 // fractional heights. 2885 for (var i = 0; i < 49; ++i) { 2886 measureText.appendChild(document.createTextNode("x")); 2887 measureText.appendChild(elt("br")); 2888 } 2889 measureText.appendChild(document.createTextNode("x")); 2890 } 2891 removeChildrenAndAdd(display.measure, measureText); 2892 var height = measureText.offsetHeight / 50; 2893 if (height > 3) { display.cachedTextHeight = height; } 2894 removeChildren(display.measure); 2895 return height || 1 2896 } 2897 2898 // Compute the default character width. 2899 function charWidth(display) { 2900 if (display.cachedCharWidth != null) { return display.cachedCharWidth } 2901 var anchor = elt("span", "xxxxxxxxxx"); 2902 var pre = elt("pre", [anchor], "CodeMirror-line-like"); 2903 removeChildrenAndAdd(display.measure, pre); 2904 var rect = anchor.getBoundingClientRect(), width = (rect.right - rect.left) / 10; 2905 if (width > 2) { display.cachedCharWidth = width; } 2906 return width || 10 2907 } 2908 2909 // Do a bulk-read of the DOM positions and sizes needed to draw the 2910 // view, so that we don't interleave reading and writing to the DOM. 2911 function getDimensions(cm) { 2912 var d = cm.display, left = {}, width = {}; 2913 var gutterLeft = d.gutters.clientLeft; 2914 for (var n = d.gutters.firstChild, i = 0; n; n = n.nextSibling, ++i) { 2915 var id = cm.display.gutterSpecs[i].className; 2916 left[id] = n.offsetLeft + n.clientLeft + gutterLeft; 2917 width[id] = n.clientWidth; 2918 } 2919 return {fixedPos: compensateForHScroll(d), 2920 gutterTotalWidth: d.gutters.offsetWidth, 2921 gutterLeft: left, 2922 gutterWidth: width, 2923 wrapperWidth: d.wrapper.clientWidth} 2924 } 2925 2926 // Computes display.scroller.scrollLeft + display.gutters.offsetWidth, 2927 // but using getBoundingClientRect to get a sub-pixel-accurate 2928 // result. 2929 function compensateForHScroll(display) { 2930 return display.scroller.getBoundingClientRect().left - display.sizer.getBoundingClientRect().left 2931 } 2932 2933 // Returns a function that estimates the height of a line, to use as 2934 // first approximation until the line becomes visible (and is thus 2935 // properly measurable). 2936 function estimateHeight(cm) { 2937 var th = textHeight(cm.display), wrapping = cm.options.lineWrapping; 2938 var perLine = wrapping && Math.max(5, cm.display.scroller.clientWidth / charWidth(cm.display) - 3); 2939 return function (line) { 2940 if (lineIsHidden(cm.doc, line)) { return 0 } 2941 2942 var widgetsHeight = 0; 2943 if (line.widgets) { for (var i = 0; i < line.widgets.length; i++) { 2944 if (line.widgets[i].height) { widgetsHeight += line.widgets[i].height; } 2945 } } 2946 2947 if (wrapping) 2948 { return widgetsHeight + (Math.ceil(line.text.length / perLine) || 1) * th } 2949 else 2950 { return widgetsHeight + th } 2951 } 2952 } 2953 2954 function estimateLineHeights(cm) { 2955 var doc = cm.doc, est = estimateHeight(cm); 2956 doc.iter(function (line) { 2957 var estHeight = est(line); 2958 if (estHeight != line.height) { updateLineHeight(line, estHeight); } 2959 }); 2960 } 2961 2962 // Given a mouse event, find the corresponding position. If liberal 2963 // is false, it checks whether a gutter or scrollbar was clicked, 2964 // and returns null if it was. forRect is used by rectangular 2965 // selections, and tries to estimate a character position even for 2966 // coordinates beyond the right of the text. 2967 function posFromMouse(cm, e, liberal, forRect) { 2968 var display = cm.display; 2969 if (!liberal && e_target(e).getAttribute("cm-not-content") == "true") { return null } 2970 2971 var x, y, space = display.lineSpace.getBoundingClientRect(); 2972 // Fails unpredictably on IE[67] when mouse is dragged around quickly. 2973 try { x = e.clientX - space.left; y = e.clientY - space.top; } 2974 catch (e$1) { return null } 2975 var coords = coordsChar(cm, x, y), line; 2976 if (forRect && coords.xRel > 0 && (line = getLine(cm.doc, coords.line).text).length == coords.ch) { 2977 var colDiff = countColumn(line, line.length, cm.options.tabSize) - line.length; 2978 coords = Pos(coords.line, Math.max(0, Math.round((x - paddingH(cm.display).left) / charWidth(cm.display)) - colDiff)); 2979 } 2980 return coords 2981 } 2982 2983 // Find the view element corresponding to a given line. Return null 2984 // when the line isn't visible. 2985 function findViewIndex(cm, n) { 2986 if (n >= cm.display.viewTo) { return null } 2987 n -= cm.display.viewFrom; 2988 if (n < 0) { return null } 2989 var view = cm.display.view; 2990 for (var i = 0; i < view.length; i++) { 2991 n -= view[i].size; 2992 if (n < 0) { return i } 2993 } 2994 } 2995 2996 // Updates the display.view data structure for a given change to the 2997 // document. From and to are in pre-change coordinates. Lendiff is 2998 // the amount of lines added or subtracted by the change. This is 2999 // used for changes that span multiple lines, or change the way 3000 // lines are divided into visual lines. regLineChange (below) 3001 // registers single-line changes. 3002 function regChange(cm, from, to, lendiff) { 3003 if (from == null) { from = cm.doc.first; } 3004 if (to == null) { to = cm.doc.first + cm.doc.size; } 3005 if (!lendiff) { lendiff = 0; } 3006 3007 var display = cm.display; 3008 if (lendiff && to < display.viewTo && 3009 (display.updateLineNumbers == null || display.updateLineNumbers > from)) 3010 { display.updateLineNumbers = from; } 3011 3012 cm.curOp.viewChanged = true; 3013 3014 if (from >= display.viewTo) { // Change after 3015 if (sawCollapsedSpans && visualLineNo(cm.doc, from) < display.viewTo) 3016 { resetView(cm); } 3017 } else if (to <= display.viewFrom) { // Change before 3018 if (sawCollapsedSpans && visualLineEndNo(cm.doc, to + lendiff) > display.viewFrom) { 3019 resetView(cm); 3020 } else { 3021 display.viewFrom += lendiff; 3022 display.viewTo += lendiff; 3023 } 3024 } else if (from <= display.viewFrom && to >= display.viewTo) { // Full overlap 3025 resetView(cm); 3026 } else if (from <= display.viewFrom) { // Top overlap 3027 var cut = viewCuttingPoint(cm, to, to + lendiff, 1); 3028 if (cut) { 3029 display.view = display.view.slice(cut.index); 3030 display.viewFrom = cut.lineN; 3031 display.viewTo += lendiff; 3032 } else { 3033 resetView(cm); 3034 } 3035 } else if (to >= display.viewTo) { // Bottom overlap 3036 var cut$1 = viewCuttingPoint(cm, from, from, -1); 3037 if (cut$1) { 3038 display.view = display.view.slice(0, cut$1.index); 3039 display.viewTo = cut$1.lineN; 3040 } else { 3041 resetView(cm); 3042 } 3043 } else { // Gap in the middle 3044 var cutTop = viewCuttingPoint(cm, from, from, -1); 3045 var cutBot = viewCuttingPoint(cm, to, to + lendiff, 1); 3046 if (cutTop && cutBot) { 3047 display.view = display.view.slice(0, cutTop.index) 3048 .concat(buildViewArray(cm, cutTop.lineN, cutBot.lineN)) 3049 .concat(display.view.slice(cutBot.index)); 3050 display.viewTo += lendiff; 3051 } else { 3052 resetView(cm); 3053 } 3054 } 3055 3056 var ext = display.externalMeasured; 3057 if (ext) { 3058 if (to < ext.lineN) 3059 { ext.lineN += lendiff; } 3060 else if (from < ext.lineN + ext.size) 3061 { display.externalMeasured = null; } 3062 } 3063 } 3064 3065 // Register a change to a single line. Type must be one of "text", 3066 // "gutter", "class", "widget" 3067 function regLineChange(cm, line, type) { 3068 cm.curOp.viewChanged = true; 3069 var display = cm.display, ext = cm.display.externalMeasured; 3070 if (ext && line >= ext.lineN && line < ext.lineN + ext.size) 3071 { display.externalMeasured = null; } 3072 3073 if (line < display.viewFrom || line >= display.viewTo) { return } 3074 var lineView = display.view[findViewIndex(cm, line)]; 3075 if (lineView.node == null) { return } 3076 var arr = lineView.changes || (lineView.changes = []); 3077 if (indexOf(arr, type) == -1) { arr.push(type); } 3078 } 3079 3080 // Clear the view. 3081 function resetView(cm) { 3082 cm.display.viewFrom = cm.display.viewTo = cm.doc.first; 3083 cm.display.view = []; 3084 cm.display.viewOffset = 0; 3085 } 3086 3087 function viewCuttingPoint(cm, oldN, newN, dir) { 3088 var index = findViewIndex(cm, oldN), diff, view = cm.display.view; 3089 if (!sawCollapsedSpans || newN == cm.doc.first + cm.doc.size) 3090 { return {index: index, lineN: newN} } 3091 var n = cm.display.viewFrom; 3092 for (var i = 0; i < index; i++) 3093 { n += view[i].size; } 3094 if (n != oldN) { 3095 if (dir > 0) { 3096 if (index == view.length - 1) { return null } 3097 diff = (n + view[index].size) - oldN; 3098 index++; 3099 } else { 3100 diff = n - oldN; 3101 } 3102 oldN += diff; newN += diff; 3103 } 3104 while (visualLineNo(cm.doc, newN) != newN) { 3105 if (index == (dir < 0 ? 0 : view.length - 1)) { return null } 3106 newN += dir * view[index - (dir < 0 ? 1 : 0)].size; 3107 index += dir; 3108 } 3109 return {index: index, lineN: newN} 3110 } 3111 3112 // Force the view to cover a given range, adding empty view element 3113 // or clipping off existing ones as needed. 3114 function adjustView(cm, from, to) { 3115 var display = cm.display, view = display.view; 3116 if (view.length == 0 || from >= display.viewTo || to <= display.viewFrom) { 3117 display.view = buildViewArray(cm, from, to); 3118 display.viewFrom = from; 3119 } else { 3120 if (display.viewFrom > from) 3121 { display.view = buildViewArray(cm, from, display.viewFrom).concat(display.view); } 3122 else if (display.viewFrom < from) 3123 { display.view = display.view.slice(findViewIndex(cm, from)); } 3124 display.viewFrom = from; 3125 if (display.viewTo < to) 3126 { display.view = display.view.concat(buildViewArray(cm, display.viewTo, to)); } 3127 else if (display.viewTo > to) 3128 { display.view = display.view.slice(0, findViewIndex(cm, to)); } 3129 } 3130 display.viewTo = to; 3131 } 3132 3133 // Count the number of lines in the view whose DOM representation is 3134 // out of date (or nonexistent). 3135 function countDirtyView(cm) { 3136 var view = cm.display.view, dirty = 0; 3137 for (var i = 0; i < view.length; i++) { 3138 var lineView = view[i]; 3139 if (!lineView.hidden && (!lineView.node || lineView.changes)) { ++dirty; } 3140 } 3141 return dirty 3142 } 3143 3144 function updateSelection(cm) { 3145 cm.display.input.showSelection(cm.display.input.prepareSelection()); 3146 } 3147 3148 function prepareSelection(cm, primary) { 3149 if ( primary === void 0 ) primary = true; 3150 3151 var doc = cm.doc, result = {}; 3152 var curFragment = result.cursors = document.createDocumentFragment(); 3153 var selFragment = result.selection = document.createDocumentFragment(); 3154 3155 var customCursor = cm.options.$customCursor; 3156 if (customCursor) { primary = true; } 3157 for (var i = 0; i < doc.sel.ranges.length; i++) { 3158 if (!primary && i == doc.sel.primIndex) { continue } 3159 var range = doc.sel.ranges[i]; 3160 if (range.from().line >= cm.display.viewTo || range.to().line < cm.display.viewFrom) { continue } 3161 var collapsed = range.empty(); 3162 if (customCursor) { 3163 var head = customCursor(cm, range); 3164 if (head) { drawSelectionCursor(cm, head, curFragment); } 3165 } else if (collapsed || cm.options.showCursorWhenSelecting) { 3166 drawSelectionCursor(cm, range.head, curFragment); 3167 } 3168 if (!collapsed) 3169 { drawSelectionRange(cm, range, selFragment); } 3170 } 3171 return result 3172 } 3173 3174 // Draws a cursor for the given range 3175 function drawSelectionCursor(cm, head, output) { 3176 var pos = cursorCoords(cm, head, "div", null, null, !cm.options.singleCursorHeightPerLine); 3177 3178 var cursor = output.appendChild(elt("div", "\u00a0", "CodeMirror-cursor")); 3179 cursor.style.left = pos.left + "px"; 3180 cursor.style.top = pos.top + "px"; 3181 cursor.style.height = Math.max(0, pos.bottom - pos.top) * cm.options.cursorHeight + "px"; 3182 3183 if (/\bcm-fat-cursor\b/.test(cm.getWrapperElement().className)) { 3184 var charPos = charCoords(cm, head, "div", null, null); 3185 var width = charPos.right - charPos.left; 3186 cursor.style.width = (width > 0 ? width : cm.defaultCharWidth()) + "px"; 3187 } 3188 3189 if (pos.other) { 3190 // Secondary cursor, shown when on a 'jump' in bi-directional text 3191 var otherCursor = output.appendChild(elt("div", "\u00a0", "CodeMirror-cursor CodeMirror-secondarycursor")); 3192 otherCursor.style.display = ""; 3193 otherCursor.style.left = pos.other.left + "px"; 3194 otherCursor.style.top = pos.other.top + "px"; 3195 otherCursor.style.height = (pos.other.bottom - pos.other.top) * .85 + "px"; 3196 } 3197 } 3198 3199 function cmpCoords(a, b) { return a.top - b.top || a.left - b.left } 3200 3201 // Draws the given range as a highlighted selection 3202 function drawSelectionRange(cm, range, output) { 3203 var display = cm.display, doc = cm.doc; 3204 var fragment = document.createDocumentFragment(); 3205 var padding = paddingH(cm.display), leftSide = padding.left; 3206 var rightSide = Math.max(display.sizerWidth, displayWidth(cm) - display.sizer.offsetLeft) - padding.right; 3207 var docLTR = doc.direction == "ltr"; 3208 3209 function add(left, top, width, bottom) { 3210 if (top < 0) { top = 0; } 3211 top = Math.round(top); 3212 bottom = Math.round(bottom); 3213 fragment.appendChild(elt("div", null, "CodeMirror-selected", ("position: absolute; left: " + left + "px;\n top: " + top + "px; width: " + (width == null ? rightSide - left : width) + "px;\n height: " + (bottom - top) + "px"))); 3214 } 3215 3216 function drawForLine(line, fromArg, toArg) { 3217 var lineObj = getLine(doc, line); 3218 var lineLen = lineObj.text.length; 3219 var start, end; 3220 function coords(ch, bias) { 3221 return charCoords(cm, Pos(line, ch), "div", lineObj, bias) 3222 } 3223 3224 function wrapX(pos, dir, side) { 3225 var extent = wrappedLineExtentChar(cm, lineObj, null, pos); 3226 var prop = (dir == "ltr") == (side == "after") ? "left" : "right"; 3227 var ch = side == "after" ? extent.begin : extent.end - (/\s/.test(lineObj.text.charAt(extent.end - 1)) ? 2 : 1); 3228 return coords(ch, prop)[prop] 3229 } 3230 3231 var order = getOrder(lineObj, doc.direction); 3232 iterateBidiSections(order, fromArg || 0, toArg == null ? lineLen : toArg, function (from, to, dir, i) { 3233 var ltr = dir == "ltr"; 3234 var fromPos = coords(from, ltr ? "left" : "right"); 3235 var toPos = coords(to - 1, ltr ? "right" : "left"); 3236 3237 var openStart = fromArg == null && from == 0, openEnd = toArg == null && to == lineLen; 3238 var first = i == 0, last = !order || i == order.length - 1; 3239 if (toPos.top - fromPos.top <= 3) { // Single line 3240 var openLeft = (docLTR ? openStart : openEnd) && first; 3241 var openRight = (docLTR ? openEnd : openStart) && last; 3242 var left = openLeft ? leftSide : (ltr ? fromPos : toPos).left; 3243 var right = openRight ? rightSide : (ltr ? toPos : fromPos).right; 3244 add(left, fromPos.top, right - left, fromPos.bottom); 3245 } else { // Multiple lines 3246 var topLeft, topRight, botLeft, botRight; 3247 if (ltr) { 3248 topLeft = docLTR && openStart && first ? leftSide : fromPos.left; 3249 topRight = docLTR ? rightSide : wrapX(from, dir, "before"); 3250 botLeft = docLTR ? leftSide : wrapX(to, dir, "after"); 3251 botRight = docLTR && openEnd && last ? rightSide : toPos.right; 3252 } else { 3253 topLeft = !docLTR ? leftSide : wrapX(from, dir, "before"); 3254 topRight = !docLTR && openStart && first ? rightSide : fromPos.right; 3255 botLeft = !docLTR && openEnd && last ? leftSide : toPos.left; 3256 botRight = !docLTR ? rightSide : wrapX(to, dir, "after"); 3257 } 3258 add(topLeft, fromPos.top, topRight - topLeft, fromPos.bottom); 3259 if (fromPos.bottom < toPos.top) { add(leftSide, fromPos.bottom, null, toPos.top); } 3260 add(botLeft, toPos.top, botRight - botLeft, toPos.bottom); 3261 } 3262 3263 if (!start || cmpCoords(fromPos, start) < 0) { start = fromPos; } 3264 if (cmpCoords(toPos, start) < 0) { start = toPos; } 3265 if (!end || cmpCoords(fromPos, end) < 0) { end = fromPos; } 3266 if (cmpCoords(toPos, end) < 0) { end = toPos; } 3267 }); 3268 return {start: start, end: end} 3269 } 3270 3271 var sFrom = range.from(), sTo = range.to(); 3272 if (sFrom.line == sTo.line) { 3273 drawForLine(sFrom.line, sFrom.ch, sTo.ch); 3274 } else { 3275 var fromLine = getLine(doc, sFrom.line), toLine = getLine(doc, sTo.line); 3276 var singleVLine = visualLine(fromLine) == visualLine(toLine); 3277 var leftEnd = drawForLine(sFrom.line, sFrom.ch, singleVLine ? fromLine.text.length + 1 : null).end; 3278 var rightStart = drawForLine(sTo.line, singleVLine ? 0 : null, sTo.ch).start; 3279 if (singleVLine) { 3280 if (leftEnd.top < rightStart.top - 2) { 3281 add(leftEnd.right, leftEnd.top, null, leftEnd.bottom); 3282 add(leftSide, rightStart.top, rightStart.left, rightStart.bottom); 3283 } else { 3284 add(leftEnd.right, leftEnd.top, rightStart.left - leftEnd.right, leftEnd.bottom); 3285 } 3286 } 3287 if (leftEnd.bottom < rightStart.top) 3288 { add(leftSide, leftEnd.bottom, null, rightStart.top); } 3289 } 3290 3291 output.appendChild(fragment); 3292 } 3293 3294 // Cursor-blinking 3295 function restartBlink(cm) { 3296 if (!cm.state.focused) { return } 3297 var display = cm.display; 3298 clearInterval(display.blinker); 3299 var on = true; 3300 display.cursorDiv.style.visibility = ""; 3301 if (cm.options.cursorBlinkRate > 0) 3302 { display.blinker = setInterval(function () { 3303 if (!cm.hasFocus()) { onBlur(cm); } 3304 display.cursorDiv.style.visibility = (on = !on) ? "" : "hidden"; 3305 }, cm.options.cursorBlinkRate); } 3306 else if (cm.options.cursorBlinkRate < 0) 3307 { display.cursorDiv.style.visibility = "hidden"; } 3308 } 3309 3310 function ensureFocus(cm) { 3311 if (!cm.hasFocus()) { 3312 cm.display.input.focus(); 3313 if (!cm.state.focused) { onFocus(cm); } 3314 } 3315 } 3316 3317 function delayBlurEvent(cm) { 3318 cm.state.delayingBlurEvent = true; 3319 setTimeout(function () { if (cm.state.delayingBlurEvent) { 3320 cm.state.delayingBlurEvent = false; 3321 if (cm.state.focused) { onBlur(cm); } 3322 } }, 100); 3323 } 3324 3325 function onFocus(cm, e) { 3326 if (cm.state.delayingBlurEvent && !cm.state.draggingText) { cm.state.delayingBlurEvent = false; } 3327 3328 if (cm.options.readOnly == "nocursor") { return } 3329 if (!cm.state.focused) { 3330 signal(cm, "focus", cm, e); 3331 cm.state.focused = true; 3332 addClass(cm.display.wrapper, "CodeMirror-focused"); 3333 // This test prevents this from firing when a context 3334 // menu is closed (since the input reset would kill the 3335 // select-all detection hack) 3336 if (!cm.curOp && cm.display.selForContextMenu != cm.doc.sel) { 3337 cm.display.input.reset(); 3338 if (webkit) { setTimeout(function () { return cm.display.input.reset(true); }, 20); } // Issue #1730 3339 } 3340 cm.display.input.receivedFocus(); 3341 } 3342 restartBlink(cm); 3343 } 3344 function onBlur(cm, e) { 3345 if (cm.state.delayingBlurEvent) { return } 3346 3347 if (cm.state.focused) { 3348 signal(cm, "blur", cm, e); 3349 cm.state.focused = false; 3350 rmClass(cm.display.wrapper, "CodeMirror-focused"); 3351 } 3352 clearInterval(cm.display.blinker); 3353 setTimeout(function () { if (!cm.state.focused) { cm.display.shift = false; } }, 150); 3354 } 3355 3356 // Read the actual heights of the rendered lines, and update their 3357 // stored heights to match. 3358 function updateHeightsInViewport(cm) { 3359 var display = cm.display; 3360 var prevBottom = display.lineDiv.offsetTop; 3361 var viewTop = Math.max(0, display.scroller.getBoundingClientRect().top); 3362 var oldHeight = display.lineDiv.getBoundingClientRect().top; 3363 var mustScroll = 0; 3364 for (var i = 0; i < display.view.length; i++) { 3365 var cur = display.view[i], wrapping = cm.options.lineWrapping; 3366 var height = (void 0), width = 0; 3367 if (cur.hidden) { continue } 3368 oldHeight += cur.line.height; 3369 if (ie && ie_version < 8) { 3370 var bot = cur.node.offsetTop + cur.node.offsetHeight; 3371 height = bot - prevBottom; 3372 prevBottom = bot; 3373 } else { 3374 var box = cur.node.getBoundingClientRect(); 3375 height = box.bottom - box.top; 3376 // Check that lines don't extend past the right of the current 3377 // editor width 3378 if (!wrapping && cur.text.firstChild) 3379 { width = cur.text.firstChild.getBoundingClientRect().right - box.left - 1; } 3380 } 3381 var diff = cur.line.height - height; 3382 if (diff > .005 || diff < -.005) { 3383 if (oldHeight < viewTop) { mustScroll -= diff; } 3384 updateLineHeight(cur.line, height); 3385 updateWidgetHeight(cur.line); 3386 if (cur.rest) { for (var j = 0; j < cur.rest.length; j++) 3387 { updateWidgetHeight(cur.rest[j]); } } 3388 } 3389 if (width > cm.display.sizerWidth) { 3390 var chWidth = Math.ceil(width / charWidth(cm.display)); 3391 if (chWidth > cm.display.maxLineLength) { 3392 cm.display.maxLineLength = chWidth; 3393 cm.display.maxLine = cur.line; 3394 cm.display.maxLineChanged = true; 3395 } 3396 } 3397 } 3398 if (Math.abs(mustScroll) > 2) { display.scroller.scrollTop += mustScroll; } 3399 } 3400 3401 // Read and store the height of line widgets associated with the 3402 // given line. 3403 function updateWidgetHeight(line) { 3404 if (line.widgets) { for (var i = 0; i < line.widgets.length; ++i) { 3405 var w = line.widgets[i], parent = w.node.parentNode; 3406 if (parent) { w.height = parent.offsetHeight; } 3407 } } 3408 } 3409 3410 // Compute the lines that are visible in a given viewport (defaults 3411 // the the current scroll position). viewport may contain top, 3412 // height, and ensure (see op.scrollToPos) properties. 3413 function visibleLines(display, doc, viewport) { 3414 var top = viewport && viewport.top != null ? Math.max(0, viewport.top) : display.scroller.scrollTop; 3415 top = Math.floor(top - paddingTop(display)); 3416 var bottom = viewport && viewport.bottom != null ? viewport.bottom : top + display.wrapper.clientHeight; 3417 3418 var from = lineAtHeight(doc, top), to = lineAtHeight(doc, bottom); 3419 // Ensure is a {from: {line, ch}, to: {line, ch}} object, and 3420 // forces those lines into the viewport (if possible). 3421 if (viewport && viewport.ensure) { 3422 var ensureFrom = viewport.ensure.from.line, ensureTo = viewport.ensure.to.line; 3423 if (ensureFrom < from) { 3424 from = ensureFrom; 3425 to = lineAtHeight(doc, heightAtLine(getLine(doc, ensureFrom)) + display.wrapper.clientHeight); 3426 } else if (Math.min(ensureTo, doc.lastLine()) >= to) { 3427 from = lineAtHeight(doc, heightAtLine(getLine(doc, ensureTo)) - display.wrapper.clientHeight); 3428 to = ensureTo; 3429 } 3430 } 3431 return {from: from, to: Math.max(to, from + 1)} 3432 } 3433 3434 // SCROLLING THINGS INTO VIEW 3435 3436 // If an editor sits on the top or bottom of the window, partially 3437 // scrolled out of view, this ensures that the cursor is visible. 3438 function maybeScrollWindow(cm, rect) { 3439 if (signalDOMEvent(cm, "scrollCursorIntoView")) { return } 3440 3441 var display = cm.display, box = display.sizer.getBoundingClientRect(), doScroll = null; 3442 if (rect.top + box.top < 0) { doScroll = true; } 3443 else if (rect.bottom + box.top > (window.innerHeight || document.documentElement.clientHeight)) { doScroll = false; } 3444 if (doScroll != null && !phantom) { 3445 var scrollNode = elt("div", "\u200b", null, ("position: absolute;\n top: " + (rect.top - display.viewOffset - paddingTop(cm.display)) + "px;\n height: " + (rect.bottom - rect.top + scrollGap(cm) + display.barHeight) + "px;\n left: " + (rect.left) + "px; width: " + (Math.max(2, rect.right - rect.left)) + "px;")); 3446 cm.display.lineSpace.appendChild(scrollNode); 3447 scrollNode.scrollIntoView(doScroll); 3448 cm.display.lineSpace.removeChild(scrollNode); 3449 } 3450 } 3451 3452 // Scroll a given position into view (immediately), verifying that 3453 // it actually became visible (as line heights are accurately 3454 // measured, the position of something may 'drift' during drawing). 3455 function scrollPosIntoView(cm, pos, end, margin) { 3456 if (margin == null) { margin = 0; } 3457 var rect; 3458 if (!cm.options.lineWrapping && pos == end) { 3459 // Set pos and end to the cursor positions around the character pos sticks to 3460 // If pos.sticky == "before", that is around pos.ch - 1, otherwise around pos.ch 3461 // If pos == Pos(_, 0, "before"), pos and end are unchanged 3462 end = pos.sticky == "before" ? Pos(pos.line, pos.ch + 1, "before") : pos; 3463 pos = pos.ch ? Pos(pos.line, pos.sticky == "before" ? pos.ch - 1 : pos.ch, "after") : pos; 3464 } 3465 for (var limit = 0; limit < 5; limit++) { 3466 var changed = false; 3467 var coords = cursorCoords(cm, pos); 3468 var endCoords = !end || end == pos ? coords : cursorCoords(cm, end); 3469 rect = {left: Math.min(coords.left, endCoords.left), 3470 top: Math.min(coords.top, endCoords.top) - margin, 3471 right: Math.max(coords.left, endCoords.left), 3472 bottom: Math.max(coords.bottom, endCoords.bottom) + margin}; 3473 var scrollPos = calculateScrollPos(cm, rect); 3474 var startTop = cm.doc.scrollTop, startLeft = cm.doc.scrollLeft; 3475 if (scrollPos.scrollTop != null) { 3476 updateScrollTop(cm, scrollPos.scrollTop); 3477 if (Math.abs(cm.doc.scrollTop - startTop) > 1) { changed = true; } 3478 } 3479 if (scrollPos.scrollLeft != null) { 3480 setScrollLeft(cm, scrollPos.scrollLeft); 3481 if (Math.abs(cm.doc.scrollLeft - startLeft) > 1) { changed = true; } 3482 } 3483 if (!changed) { break } 3484 } 3485 return rect 3486 } 3487 3488 // Scroll a given set of coordinates into view (immediately). 3489 function scrollIntoView(cm, rect) { 3490 var scrollPos = calculateScrollPos(cm, rect); 3491 if (scrollPos.scrollTop != null) { updateScrollTop(cm, scrollPos.scrollTop); } 3492 if (scrollPos.scrollLeft != null) { setScrollLeft(cm, scrollPos.scrollLeft); } 3493 } 3494 3495 // Calculate a new scroll position needed to scroll the given 3496 // rectangle into view. Returns an object with scrollTop and 3497 // scrollLeft properties. When these are undefined, the 3498 // vertical/horizontal position does not need to be adjusted. 3499 function calculateScrollPos(cm, rect) { 3500 var display = cm.display, snapMargin = textHeight(cm.display); 3501 if (rect.top < 0) { rect.top = 0; } 3502 var screentop = cm.curOp && cm.curOp.scrollTop != null ? cm.curOp.scrollTop : display.scroller.scrollTop; 3503 var screen = displayHeight(cm), result = {}; 3504 if (rect.bottom - rect.top > screen) { rect.bottom = rect.top + screen; } 3505 var docBottom = cm.doc.height + paddingVert(display); 3506 var atTop = rect.top < snapMargin, atBottom = rect.bottom > docBottom - snapMargin; 3507 if (rect.top < screentop) { 3508 result.scrollTop = atTop ? 0 : rect.top; 3509 } else if (rect.bottom > screentop + screen) { 3510 var newTop = Math.min(rect.top, (atBottom ? docBottom : rect.bottom) - screen); 3511 if (newTop != screentop) { result.scrollTop = newTop; } 3512 } 3513 3514 var gutterSpace = cm.options.fixedGutter ? 0 : display.gutters.offsetWidth; 3515 var screenleft = cm.curOp && cm.curOp.scrollLeft != null ? cm.curOp.scrollLeft : display.scroller.scrollLeft - gutterSpace; 3516 var screenw = displayWidth(cm) - display.gutters.offsetWidth; 3517 var tooWide = rect.right - rect.left > screenw; 3518 if (tooWide) { rect.right = rect.left + screenw; } 3519 if (rect.left < 10) 3520 { result.scrollLeft = 0; } 3521 else if (rect.left < screenleft) 3522 { result.scrollLeft = Math.max(0, rect.left + gutterSpace - (tooWide ? 0 : 10)); } 3523 else if (rect.right > screenw + screenleft - 3) 3524 { result.scrollLeft = rect.right + (tooWide ? 0 : 10) - screenw; } 3525 return result 3526 } 3527 3528 // Store a relative adjustment to the scroll position in the current 3529 // operation (to be applied when the operation finishes). 3530 function addToScrollTop(cm, top) { 3531 if (top == null) { return } 3532 resolveScrollToPos(cm); 3533 cm.curOp.scrollTop = (cm.curOp.scrollTop == null ? cm.doc.scrollTop : cm.curOp.scrollTop) + top; 3534 } 3535 3536 // Make sure that at the end of the operation the current cursor is 3537 // shown. 3538 function ensureCursorVisible(cm) { 3539 resolveScrollToPos(cm); 3540 var cur = cm.getCursor(); 3541 cm.curOp.scrollToPos = {from: cur, to: cur, margin: cm.options.cursorScrollMargin}; 3542 } 3543 3544 function scrollToCoords(cm, x, y) { 3545 if (x != null || y != null) { resolveScrollToPos(cm); } 3546 if (x != null) { cm.curOp.scrollLeft = x; } 3547 if (y != null) { cm.curOp.scrollTop = y; } 3548 } 3549 3550 function scrollToRange(cm, range) { 3551 resolveScrollToPos(cm); 3552 cm.curOp.scrollToPos = range; 3553 } 3554 3555 // When an operation has its scrollToPos property set, and another 3556 // scroll action is applied before the end of the operation, this 3557 // 'simulates' scrolling that position into view in a cheap way, so 3558 // that the effect of intermediate scroll commands is not ignored. 3559 function resolveScrollToPos(cm) { 3560 var range = cm.curOp.scrollToPos; 3561 if (range) { 3562 cm.curOp.scrollToPos = null; 3563 var from = estimateCoords(cm, range.from), to = estimateCoords(cm, range.to); 3564 scrollToCoordsRange(cm, from, to, range.margin); 3565 } 3566 } 3567 3568 function scrollToCoordsRange(cm, from, to, margin) { 3569 var sPos = calculateScrollPos(cm, { 3570 left: Math.min(from.left, to.left), 3571 top: Math.min(from.top, to.top) - margin, 3572 right: Math.max(from.right, to.right), 3573 bottom: Math.max(from.bottom, to.bottom) + margin 3574 }); 3575 scrollToCoords(cm, sPos.scrollLeft, sPos.scrollTop); 3576 } 3577 3578 // Sync the scrollable area and scrollbars, ensure the viewport 3579 // covers the visible area. 3580 function updateScrollTop(cm, val) { 3581 if (Math.abs(cm.doc.scrollTop - val) < 2) { return } 3582 if (!gecko) { updateDisplaySimple(cm, {top: val}); } 3583 setScrollTop(cm, val, true); 3584 if (gecko) { updateDisplaySimple(cm); } 3585 startWorker(cm, 100); 3586 } 3587 3588 function setScrollTop(cm, val, forceScroll) { 3589 val = Math.max(0, Math.min(cm.display.scroller.scrollHeight - cm.display.scroller.clientHeight, val)); 3590 if (cm.display.scroller.scrollTop == val && !forceScroll) { return } 3591 cm.doc.scrollTop = val; 3592 cm.display.scrollbars.setScrollTop(val); 3593 if (cm.display.scroller.scrollTop != val) { cm.display.scroller.scrollTop = val; } 3594 } 3595 3596 // Sync scroller and scrollbar, ensure the gutter elements are 3597 // aligned. 3598 function setScrollLeft(cm, val, isScroller, forceScroll) { 3599 val = Math.max(0, Math.min(val, cm.display.scroller.scrollWidth - cm.display.scroller.clientWidth)); 3600 if ((isScroller ? val == cm.doc.scrollLeft : Math.abs(cm.doc.scrollLeft - val) < 2) && !forceScroll) { return } 3601 cm.doc.scrollLeft = val; 3602 alignHorizontally(cm); 3603 if (cm.display.scroller.scrollLeft != val) { cm.display.scroller.scrollLeft = val; } 3604 cm.display.scrollbars.setScrollLeft(val); 3605 } 3606 3607 // SCROLLBARS 3608 3609 // Prepare DOM reads needed to update the scrollbars. Done in one 3610 // shot to minimize update/measure roundtrips. 3611 function measureForScrollbars(cm) { 3612 var d = cm.display, gutterW = d.gutters.offsetWidth; 3613 var docH = Math.round(cm.doc.height + paddingVert(cm.display)); 3614 return { 3615 clientHeight: d.scroller.clientHeight, 3616 viewHeight: d.wrapper.clientHeight, 3617 scrollWidth: d.scroller.scrollWidth, clientWidth: d.scroller.clientWidth, 3618 viewWidth: d.wrapper.clientWidth, 3619 barLeft: cm.options.fixedGutter ? gutterW : 0, 3620 docHeight: docH, 3621 scrollHeight: docH + scrollGap(cm) + d.barHeight, 3622 nativeBarWidth: d.nativeBarWidth, 3623 gutterWidth: gutterW 3624 } 3625 } 3626 3627 var NativeScrollbars = function(place, scroll, cm) { 3628 this.cm = cm; 3629 var vert = this.vert = elt("div", [elt("div", null, null, "min-width: 1px")], "CodeMirror-vscrollbar"); 3630 var horiz = this.horiz = elt("div", [elt("div", null, null, "height: 100%; min-height: 1px")], "CodeMirror-hscrollbar"); 3631 vert.tabIndex = horiz.tabIndex = -1; 3632 place(vert); place(horiz); 3633 3634 on(vert, "scroll", function () { 3635 if (vert.clientHeight) { scroll(vert.scrollTop, "vertical"); } 3636 }); 3637 on(horiz, "scroll", function () { 3638 if (horiz.clientWidth) { scroll(horiz.scrollLeft, "horizontal"); } 3639 }); 3640 3641 this.checkedZeroWidth = false; 3642 // Need to set a minimum width to see the scrollbar on IE7 (but must not set it on IE8). 3643 if (ie && ie_version < 8) { this.horiz.style.minHeight = this.vert.style.minWidth = "18px"; } 3644 }; 3645 3646 NativeScrollbars.prototype.update = function (measure) { 3647 var needsH = measure.scrollWidth > measure.clientWidth + 1; 3648 var needsV = measure.scrollHeight > measure.clientHeight + 1; 3649 var sWidth = measure.nativeBarWidth; 3650 3651 if (needsV) { 3652 this.vert.style.display = "block"; 3653 this.vert.style.bottom = needsH ? sWidth + "px" : "0"; 3654 var totalHeight = measure.viewHeight - (needsH ? sWidth : 0); 3655 // A bug in IE8 can cause this value to be negative, so guard it. 3656 this.vert.firstChild.style.height = 3657 Math.max(0, measure.scrollHeight - measure.clientHeight + totalHeight) + "px"; 3658 } else { 3659 this.vert.scrollTop = 0; 3660 this.vert.style.display = ""; 3661 this.vert.firstChild.style.height = "0"; 3662 } 3663 3664 if (needsH) { 3665 this.horiz.style.display = "block"; 3666 this.horiz.style.right = needsV ? sWidth + "px" : "0"; 3667 this.horiz.style.left = measure.barLeft + "px"; 3668 var totalWidth = measure.viewWidth - measure.barLeft - (needsV ? sWidth : 0); 3669 this.horiz.firstChild.style.width = 3670 Math.max(0, measure.scrollWidth - measure.clientWidth + totalWidth) + "px"; 3671 } else { 3672 this.horiz.style.display = ""; 3673 this.horiz.firstChild.style.width = "0"; 3674 } 3675 3676 if (!this.checkedZeroWidth && measure.clientHeight > 0) { 3677 if (sWidth == 0) { this.zeroWidthHack(); } 3678 this.checkedZeroWidth = true; 3679 } 3680 3681 return {right: needsV ? sWidth : 0, bottom: needsH ? sWidth : 0} 3682 }; 3683 3684 NativeScrollbars.prototype.setScrollLeft = function (pos) { 3685 if (this.horiz.scrollLeft != pos) { this.horiz.scrollLeft = pos; } 3686 if (this.disableHoriz) { this.enableZeroWidthBar(this.horiz, this.disableHoriz, "horiz"); } 3687 }; 3688 3689 NativeScrollbars.prototype.setScrollTop = function (pos) { 3690 if (this.vert.scrollTop != pos) { this.vert.scrollTop = pos; } 3691 if (this.disableVert) { this.enableZeroWidthBar(this.vert, this.disableVert, "vert"); } 3692 }; 3693 3694 NativeScrollbars.prototype.zeroWidthHack = function () { 3695 var w = mac && !mac_geMountainLion ? "12px" : "18px"; 3696 this.horiz.style.height = this.vert.style.width = w; 3697 this.horiz.style.pointerEvents = this.vert.style.pointerEvents = "none"; 3698 this.disableHoriz = new Delayed; 3699 this.disableVert = new Delayed; 3700 }; 3701 3702 NativeScrollbars.prototype.enableZeroWidthBar = function (bar, delay, type) { 3703 bar.style.pointerEvents = "auto"; 3704 function maybeDisable() { 3705 // To find out whether the scrollbar is still visible, we 3706 // check whether the element under the pixel in the bottom 3707 // right corner of the scrollbar box is the scrollbar box 3708 // itself (when the bar is still visible) or its filler child 3709 // (when the bar is hidden). If it is still visible, we keep 3710 // it enabled, if it's hidden, we disable pointer events. 3711 var box = bar.getBoundingClientRect(); 3712 var elt = type == "vert" ? document.elementFromPoint(box.right - 1, (box.top + box.bottom) / 2) 3713 : document.elementFromPoint((box.right + box.left) / 2, box.bottom - 1); 3714 if (elt != bar) { bar.style.pointerEvents = "none"; } 3715 else { delay.set(1000, maybeDisable); } 3716 } 3717 delay.set(1000, maybeDisable); 3718 }; 3719 3720 NativeScrollbars.prototype.clear = function () { 3721 var parent = this.horiz.parentNode; 3722 parent.removeChild(this.horiz); 3723 parent.removeChild(this.vert); 3724 }; 3725 3726 var NullScrollbars = function () {}; 3727 3728 NullScrollbars.prototype.update = function () { return {bottom: 0, right: 0} }; 3729 NullScrollbars.prototype.setScrollLeft = function () {}; 3730 NullScrollbars.prototype.setScrollTop = function () {}; 3731 NullScrollbars.prototype.clear = function () {}; 3732 3733 function updateScrollbars(cm, measure) { 3734 if (!measure) { measure = measureForScrollbars(cm); } 3735 var startWidth = cm.display.barWidth, startHeight = cm.display.barHeight; 3736 updateScrollbarsInner(cm, measure); 3737 for (var i = 0; i < 4 && startWidth != cm.display.barWidth || startHeight != cm.display.barHeight; i++) { 3738 if (startWidth != cm.display.barWidth && cm.options.lineWrapping) 3739 { updateHeightsInViewport(cm); } 3740 updateScrollbarsInner(cm, measureForScrollbars(cm)); 3741 startWidth = cm.display.barWidth; startHeight = cm.display.barHeight; 3742 } 3743 } 3744 3745 // Re-synchronize the fake scrollbars with the actual size of the 3746 // content. 3747 function updateScrollbarsInner(cm, measure) { 3748 var d = cm.display; 3749 var sizes = d.scrollbars.update(measure); 3750 3751 d.sizer.style.paddingRight = (d.barWidth = sizes.right) + "px"; 3752 d.sizer.style.paddingBottom = (d.barHeight = sizes.bottom) + "px"; 3753 d.heightForcer.style.borderBottom = sizes.bottom + "px solid transparent"; 3754 3755 if (sizes.right && sizes.bottom) { 3756 d.scrollbarFiller.style.display = "block"; 3757 d.scrollbarFiller.style.height = sizes.bottom + "px"; 3758 d.scrollbarFiller.style.width = sizes.right + "px"; 3759 } else { d.scrollbarFiller.style.display = ""; } 3760 if (sizes.bottom && cm.options.coverGutterNextToScrollbar && cm.options.fixedGutter) { 3761 d.gutterFiller.style.display = "block"; 3762 d.gutterFiller.style.height = sizes.bottom + "px"; 3763 d.gutterFiller.style.width = measure.gutterWidth + "px"; 3764 } else { d.gutterFiller.style.display = ""; } 3765 } 3766 3767 var scrollbarModel = {"native": NativeScrollbars, "null": NullScrollbars}; 3768 3769 function initScrollbars(cm) { 3770 if (cm.display.scrollbars) { 3771 cm.display.scrollbars.clear(); 3772 if (cm.display.scrollbars.addClass) 3773 { rmClass(cm.display.wrapper, cm.display.scrollbars.addClass); } 3774 } 3775 3776 cm.display.scrollbars = new scrollbarModel[cm.options.scrollbarStyle](function (node) { 3777 cm.display.wrapper.insertBefore(node, cm.display.scrollbarFiller); 3778 // Prevent clicks in the scrollbars from killing focus 3779 on(node, "mousedown", function () { 3780 if (cm.state.focused) { setTimeout(function () { return cm.display.input.focus(); }, 0); } 3781 }); 3782 node.setAttribute("cm-not-content", "true"); 3783 }, function (pos, axis) { 3784 if (axis == "horizontal") { setScrollLeft(cm, pos); } 3785 else { updateScrollTop(cm, pos); } 3786 }, cm); 3787 if (cm.display.scrollbars.addClass) 3788 { addClass(cm.display.wrapper, cm.display.scrollbars.addClass); } 3789 } 3790 3791 // Operations are used to wrap a series of changes to the editor 3792 // state in such a way that each change won't have to update the 3793 // cursor and display (which would be awkward, slow, and 3794 // error-prone). Instead, display updates are batched and then all 3795 // combined and executed at once. 3796 3797 var nextOpId = 0; 3798 // Start a new operation. 3799 function startOperation(cm) { 3800 cm.curOp = { 3801 cm: cm, 3802 viewChanged: false, // Flag that indicates that lines might need to be redrawn 3803 startHeight: cm.doc.height, // Used to detect need to update scrollbar 3804 forceUpdate: false, // Used to force a redraw 3805 updateInput: 0, // Whether to reset the input textarea 3806 typing: false, // Whether this reset should be careful to leave existing text (for compositing) 3807 changeObjs: null, // Accumulated changes, for firing change events 3808 cursorActivityHandlers: null, // Set of handlers to fire cursorActivity on 3809 cursorActivityCalled: 0, // Tracks which cursorActivity handlers have been called already 3810 selectionChanged: false, // Whether the selection needs to be redrawn 3811 updateMaxLine: false, // Set when the widest line needs to be determined anew 3812 scrollLeft: null, scrollTop: null, // Intermediate scroll position, not pushed to DOM yet 3813 scrollToPos: null, // Used to scroll to a specific position 3814 focus: false, 3815 id: ++nextOpId, // Unique ID 3816 markArrays: null // Used by addMarkedSpan 3817 }; 3818 pushOperation(cm.curOp); 3819 } 3820 3821 // Finish an operation, updating the display and signalling delayed events 3822 function endOperation(cm) { 3823 var op = cm.curOp; 3824 if (op) { finishOperation(op, function (group) { 3825 for (var i = 0; i < group.ops.length; i++) 3826 { group.ops[i].cm.curOp = null; } 3827 endOperations(group); 3828 }); } 3829 } 3830 3831 // The DOM updates done when an operation finishes are batched so 3832 // that the minimum number of relayouts are required. 3833 function endOperations(group) { 3834 var ops = group.ops; 3835 for (var i = 0; i < ops.length; i++) // Read DOM 3836 { endOperation_R1(ops[i]); } 3837 for (var i$1 = 0; i$1 < ops.length; i$1++) // Write DOM (maybe) 3838 { endOperation_W1(ops[i$1]); } 3839 for (var i$2 = 0; i$2 < ops.length; i$2++) // Read DOM 3840 { endOperation_R2(ops[i$2]); } 3841 for (var i$3 = 0; i$3 < ops.length; i$3++) // Write DOM (maybe) 3842 { endOperation_W2(ops[i$3]); } 3843 for (var i$4 = 0; i$4 < ops.length; i$4++) // Read DOM 3844 { endOperation_finish(ops[i$4]); } 3845 } 3846 3847 function endOperation_R1(op) { 3848 var cm = op.cm, display = cm.display; 3849 maybeClipScrollbars(cm); 3850 if (op.updateMaxLine) { findMaxLine(cm); } 3851 3852 op.mustUpdate = op.viewChanged || op.forceUpdate || op.scrollTop != null || 3853 op.scrollToPos && (op.scrollToPos.from.line < display.viewFrom || 3854 op.scrollToPos.to.line >= display.viewTo) || 3855 display.maxLineChanged && cm.options.lineWrapping; 3856 op.update = op.mustUpdate && 3857 new DisplayUpdate(cm, op.mustUpdate && {top: op.scrollTop, ensure: op.scrollToPos}, op.forceUpdate); 3858 } 3859 3860 function endOperation_W1(op) { 3861 op.updatedDisplay = op.mustUpdate && updateDisplayIfNeeded(op.cm, op.update); 3862 } 3863 3864 function endOperation_R2(op) { 3865 var cm = op.cm, display = cm.display; 3866 if (op.updatedDisplay) { updateHeightsInViewport(cm); } 3867 3868 op.barMeasure = measureForScrollbars(cm); 3869 3870 // If the max line changed since it was last measured, measure it, 3871 // and ensure the document's width matches it. 3872 // updateDisplay_W2 will use these properties to do the actual resizing 3873 if (display.maxLineChanged && !cm.options.lineWrapping) { 3874 op.adjustWidthTo = measureChar(cm, display.maxLine, display.maxLine.text.length).left + 3; 3875 cm.display.sizerWidth = op.adjustWidthTo; 3876 op.barMeasure.scrollWidth = 3877 Math.max(display.scroller.clientWidth, display.sizer.offsetLeft + op.adjustWidthTo + scrollGap(cm) + cm.display.barWidth); 3878 op.maxScrollLeft = Math.max(0, display.sizer.offsetLeft + op.adjustWidthTo - displayWidth(cm)); 3879 } 3880 3881 if (op.updatedDisplay || op.selectionChanged) 3882 { op.preparedSelection = display.input.prepareSelection(); } 3883 } 3884 3885 function endOperation_W2(op) { 3886 var cm = op.cm; 3887 3888 if (op.adjustWidthTo != null) { 3889 cm.display.sizer.style.minWidth = op.adjustWidthTo + "px"; 3890 if (op.maxScrollLeft < cm.doc.scrollLeft) 3891 { setScrollLeft(cm, Math.min(cm.display.scroller.scrollLeft, op.maxScrollLeft), true); } 3892 cm.display.maxLineChanged = false; 3893 } 3894 3895 var takeFocus = op.focus && op.focus == activeElt(); 3896 if (op.preparedSelection) 3897 { cm.display.input.showSelection(op.preparedSelection, takeFocus); } 3898 if (op.updatedDisplay || op.startHeight != cm.doc.height) 3899 { updateScrollbars(cm, op.barMeasure); } 3900 if (op.updatedDisplay) 3901 { setDocumentHeight(cm, op.barMeasure); } 3902 3903 if (op.selectionChanged) { restartBlink(cm); } 3904 3905 if (cm.state.focused && op.updateInput) 3906 { cm.display.input.reset(op.typing); } 3907 if (takeFocus) { ensureFocus(op.cm); } 3908 } 3909 3910 function endOperation_finish(op) { 3911 var cm = op.cm, display = cm.display, doc = cm.doc; 3912 3913 if (op.updatedDisplay) { postUpdateDisplay(cm, op.update); } 3914 3915 // Abort mouse wheel delta measurement, when scrolling explicitly 3916 if (display.wheelStartX != null && (op.scrollTop != null || op.scrollLeft != null || op.scrollToPos)) 3917 { display.wheelStartX = display.wheelStartY = null; } 3918 3919 // Propagate the scroll position to the actual DOM scroller 3920 if (op.scrollTop != null) { setScrollTop(cm, op.scrollTop, op.forceScroll); } 3921 3922 if (op.scrollLeft != null) { setScrollLeft(cm, op.scrollLeft, true, true); } 3923 // If we need to scroll a specific position into view, do so. 3924 if (op.scrollToPos) { 3925 var rect = scrollPosIntoView(cm, clipPos(doc, op.scrollToPos.from), 3926 clipPos(doc, op.scrollToPos.to), op.scrollToPos.margin); 3927 maybeScrollWindow(cm, rect); 3928 } 3929 3930 // Fire events for markers that are hidden/unidden by editing or 3931 // undoing 3932 var hidden = op.maybeHiddenMarkers, unhidden = op.maybeUnhiddenMarkers; 3933 if (hidden) { for (var i = 0; i < hidden.length; ++i) 3934 { if (!hidden[i].lines.length) { signal(hidden[i], "hide"); } } } 3935 if (unhidden) { for (var i$1 = 0; i$1 < unhidden.length; ++i$1) 3936 { if (unhidden[i$1].lines.length) { signal(unhidden[i$1], "unhide"); } } } 3937 3938 if (display.wrapper.offsetHeight) 3939 { doc.scrollTop = cm.display.scroller.scrollTop; } 3940 3941 // Fire change events, and delayed event handlers 3942 if (op.changeObjs) 3943 { signal(cm, "changes", cm, op.changeObjs); } 3944 if (op.update) 3945 { op.update.finish(); } 3946 } 3947 3948 // Run the given function in an operation 3949 function runInOp(cm, f) { 3950 if (cm.curOp) { return f() } 3951 startOperation(cm); 3952 try { return f() } 3953 finally { endOperation(cm); } 3954 } 3955 // Wraps a function in an operation. Returns the wrapped function. 3956 function operation(cm, f) { 3957 return function() { 3958 if (cm.curOp) { return f.apply(cm, arguments) } 3959 startOperation(cm); 3960 try { return f.apply(cm, arguments) } 3961 finally { endOperation(cm); } 3962 } 3963 } 3964 // Used to add methods to editor and doc instances, wrapping them in 3965 // operations. 3966 function methodOp(f) { 3967 return function() { 3968 if (this.curOp) { return f.apply(this, arguments) } 3969 startOperation(this); 3970 try { return f.apply(this, arguments) } 3971 finally { endOperation(this); } 3972 } 3973 } 3974 function docMethodOp(f) { 3975 return function() { 3976 var cm = this.cm; 3977 if (!cm || cm.curOp) { return f.apply(this, arguments) } 3978 startOperation(cm); 3979 try { return f.apply(this, arguments) } 3980 finally { endOperation(cm); } 3981 } 3982 } 3983 3984 // HIGHLIGHT WORKER 3985 3986 function startWorker(cm, time) { 3987 if (cm.doc.highlightFrontier < cm.display.viewTo) 3988 { cm.state.highlight.set(time, bind(highlightWorker, cm)); } 3989 } 3990 3991 function highlightWorker(cm) { 3992 var doc = cm.doc; 3993 if (doc.highlightFrontier >= cm.display.viewTo) { return } 3994 var end = +new Date + cm.options.workTime; 3995 var context = getContextBefore(cm, doc.highlightFrontier); 3996 var changedLines = []; 3997 3998 doc.iter(context.line, Math.min(doc.first + doc.size, cm.display.viewTo + 500), function (line) { 3999 if (context.line >= cm.display.viewFrom) { // Visible 4000 var oldStyles = line.styles; 4001 var resetState = line.text.length > cm.options.maxHighlightLength ? copyState(doc.mode, context.state) : null; 4002 var highlighted = highlightLine(cm, line, context, true); 4003 if (resetState) { context.state = resetState; } 4004 line.styles = highlighted.styles; 4005 var oldCls = line.styleClasses, newCls = highlighted.classes; 4006 if (newCls) { line.styleClasses = newCls; } 4007 else if (oldCls) { line.styleClasses = null; } 4008 var ischange = !oldStyles || oldStyles.length != line.styles.length || 4009 oldCls != newCls && (!oldCls || !newCls || oldCls.bgClass != newCls.bgClass || oldCls.textClass != newCls.textClass); 4010 for (var i = 0; !ischange && i < oldStyles.length; ++i) { ischange = oldStyles[i] != line.styles[i]; } 4011 if (ischange) { changedLines.push(context.line); } 4012 line.stateAfter = context.save(); 4013 context.nextLine(); 4014 } else { 4015 if (line.text.length <= cm.options.maxHighlightLength) 4016 { processLine(cm, line.text, context); } 4017 line.stateAfter = context.line % 5 == 0 ? context.save() : null; 4018 context.nextLine(); 4019 } 4020 if (+new Date > end) { 4021 startWorker(cm, cm.options.workDelay); 4022 return true 4023 } 4024 }); 4025 doc.highlightFrontier = context.line; 4026 doc.modeFrontier = Math.max(doc.modeFrontier, context.line); 4027 if (changedLines.length) { runInOp(cm, function () { 4028 for (var i = 0; i < changedLines.length; i++) 4029 { regLineChange(cm, changedLines[i], "text"); } 4030 }); } 4031 } 4032 4033 // DISPLAY DRAWING 4034 4035 var DisplayUpdate = function(cm, viewport, force) { 4036 var display = cm.display; 4037 4038 this.viewport = viewport; 4039 // Store some values that we'll need later (but don't want to force a relayout for) 4040 this.visible = visibleLines(display, cm.doc, viewport); 4041 this.editorIsHidden = !display.wrapper.offsetWidth; 4042 this.wrapperHeight = display.wrapper.clientHeight; 4043 this.wrapperWidth = display.wrapper.clientWidth; 4044 this.oldDisplayWidth = displayWidth(cm); 4045 this.force = force; 4046 this.dims = getDimensions(cm); 4047 this.events = []; 4048 }; 4049 4050 DisplayUpdate.prototype.signal = function (emitter, type) { 4051 if (hasHandler(emitter, type)) 4052 { this.events.push(arguments); } 4053 }; 4054 DisplayUpdate.prototype.finish = function () { 4055 for (var i = 0; i < this.events.length; i++) 4056 { signal.apply(null, this.events[i]); } 4057 }; 4058 4059 function maybeClipScrollbars(cm) { 4060 var display = cm.display; 4061 if (!display.scrollbarsClipped && display.scroller.offsetWidth) { 4062 display.nativeBarWidth = display.scroller.offsetWidth - display.scroller.clientWidth; 4063 display.heightForcer.style.height = scrollGap(cm) + "px"; 4064 display.sizer.style.marginBottom = -display.nativeBarWidth + "px"; 4065 display.sizer.style.borderRightWidth = scrollGap(cm) + "px"; 4066 display.scrollbarsClipped = true; 4067 } 4068 } 4069 4070 function selectionSnapshot(cm) { 4071 if (cm.hasFocus()) { return null } 4072 var active = activeElt(); 4073 if (!active || !contains(cm.display.lineDiv, active)) { return null } 4074 var result = {activeElt: active}; 4075 if (window.getSelection) { 4076 var sel = window.getSelection(); 4077 if (sel.anchorNode && sel.extend && contains(cm.display.lineDiv, sel.anchorNode)) { 4078 result.anchorNode = sel.anchorNode; 4079 result.anchorOffset = sel.anchorOffset; 4080 result.focusNode = sel.focusNode; 4081 result.focusOffset = sel.focusOffset; 4082 } 4083 } 4084 return result 4085 } 4086 4087 function restoreSelection(snapshot) { 4088 if (!snapshot || !snapshot.activeElt || snapshot.activeElt == activeElt()) { return } 4089 snapshot.activeElt.focus(); 4090 if (!/^(INPUT|TEXTAREA)$/.test(snapshot.activeElt.nodeName) && 4091 snapshot.anchorNode && contains(document.body, snapshot.anchorNode) && contains(document.body, snapshot.focusNode)) { 4092 var sel = window.getSelection(), range = document.createRange(); 4093 range.setEnd(snapshot.anchorNode, snapshot.anchorOffset); 4094 range.collapse(false); 4095 sel.removeAllRanges(); 4096 sel.addRange(range); 4097 sel.extend(snapshot.focusNode, snapshot.focusOffset); 4098 } 4099 } 4100 4101 // Does the actual updating of the line display. Bails out 4102 // (returning false) when there is nothing to be done and forced is 4103 // false. 4104 function updateDisplayIfNeeded(cm, update) { 4105 var display = cm.display, doc = cm.doc; 4106 4107 if (update.editorIsHidden) { 4108 resetView(cm); 4109 return false 4110 } 4111 4112 // Bail out if the visible area is already rendered and nothing changed. 4113 if (!update.force && 4114 update.visible.from >= display.viewFrom && update.visible.to <= display.viewTo && 4115 (display.updateLineNumbers == null || display.updateLineNumbers >= display.viewTo) && 4116 display.renderedView == display.view && countDirtyView(cm) == 0) 4117 { return false } 4118 4119 if (maybeUpdateLineNumberWidth(cm)) { 4120 resetView(cm); 4121 update.dims = getDimensions(cm); 4122 } 4123 4124 // Compute a suitable new viewport (from & to) 4125 var end = doc.first + doc.size; 4126 var from = Math.max(update.visible.from - cm.options.viewportMargin, doc.first); 4127 var to = Math.min(end, update.visible.to + cm.options.viewportMargin); 4128 if (display.viewFrom < from && from - display.viewFrom < 20) { from = Math.max(doc.first, display.viewFrom); } 4129 if (display.viewTo > to && display.viewTo - to < 20) { to = Math.min(end, display.viewTo); } 4130 if (sawCollapsedSpans) { 4131 from = visualLineNo(cm.doc, from); 4132 to = visualLineEndNo(cm.doc, to); 4133 } 4134 4135 var different = from != display.viewFrom || to != display.viewTo || 4136 display.lastWrapHeight != update.wrapperHeight || display.lastWrapWidth != update.wrapperWidth; 4137 adjustView(cm, from, to); 4138 4139 display.viewOffset = heightAtLine(getLine(cm.doc, display.viewFrom)); 4140 // Position the mover div to align with the current scroll position 4141 cm.display.mover.style.top = display.viewOffset + "px"; 4142 4143 var toUpdate = countDirtyView(cm); 4144 if (!different && toUpdate == 0 && !update.force && display.renderedView == display.view && 4145 (display.updateLineNumbers == null || display.updateLineNumbers >= display.viewTo)) 4146 { return false } 4147 4148 // For big changes, we hide the enclosing element during the 4149 // update, since that speeds up the operations on most browsers. 4150 var selSnapshot = selectionSnapshot(cm); 4151 if (toUpdate > 4) { display.lineDiv.style.display = "none"; } 4152 patchDisplay(cm, display.updateLineNumbers, update.dims); 4153 if (toUpdate > 4) { display.lineDiv.style.display = ""; } 4154 display.renderedView = display.view; 4155 // There might have been a widget with a focused element that got 4156 // hidden or updated, if so re-focus it. 4157 restoreSelection(selSnapshot); 4158 4159 // Prevent selection and cursors from interfering with the scroll 4160 // width and height. 4161 removeChildren(display.cursorDiv); 4162 removeChildren(display.selectionDiv); 4163 display.gutters.style.height = display.sizer.style.minHeight = 0; 4164 4165 if (different) { 4166 display.lastWrapHeight = update.wrapperHeight; 4167 display.lastWrapWidth = update.wrapperWidth; 4168 startWorker(cm, 400); 4169 } 4170 4171 display.updateLineNumbers = null; 4172 4173 return true 4174 } 4175 4176 function postUpdateDisplay(cm, update) { 4177 var viewport = update.viewport; 4178 4179 for (var first = true;; first = false) { 4180 if (!first || !cm.options.lineWrapping || update.oldDisplayWidth == displayWidth(cm)) { 4181 // Clip forced viewport to actual scrollable area. 4182 if (viewport && viewport.top != null) 4183 { viewport = {top: Math.min(cm.doc.height + paddingVert(cm.display) - displayHeight(cm), viewport.top)}; } 4184 // Updated line heights might result in the drawn area not 4185 // actually covering the viewport. Keep looping until it does. 4186 update.visible = visibleLines(cm.display, cm.doc, viewport); 4187 if (update.visible.from >= cm.display.viewFrom && update.visible.to <= cm.display.viewTo) 4188 { break } 4189 } else if (first) { 4190 update.visible = visibleLines(cm.display, cm.doc, viewport); 4191 } 4192 if (!updateDisplayIfNeeded(cm, update)) { break } 4193 updateHeightsInViewport(cm); 4194 var barMeasure = measureForScrollbars(cm); 4195 updateSelection(cm); 4196 updateScrollbars(cm, barMeasure); 4197 setDocumentHeight(cm, barMeasure); 4198 update.force = false; 4199 } 4200 4201 update.signal(cm, "update", cm); 4202 if (cm.display.viewFrom != cm.display.reportedViewFrom || cm.display.viewTo != cm.display.reportedViewTo) { 4203 update.signal(cm, "viewportChange", cm, cm.display.viewFrom, cm.display.viewTo); 4204 cm.display.reportedViewFrom = cm.display.viewFrom; cm.display.reportedViewTo = cm.display.viewTo; 4205 } 4206 } 4207 4208 function updateDisplaySimple(cm, viewport) { 4209 var update = new DisplayUpdate(cm, viewport); 4210 if (updateDisplayIfNeeded(cm, update)) { 4211 updateHeightsInViewport(cm); 4212 postUpdateDisplay(cm, update); 4213 var barMeasure = measureForScrollbars(cm); 4214 updateSelection(cm); 4215 updateScrollbars(cm, barMeasure); 4216 setDocumentHeight(cm, barMeasure); 4217 update.finish(); 4218 } 4219 } 4220 4221 // Sync the actual display DOM structure with display.view, removing 4222 // nodes for lines that are no longer in view, and creating the ones 4223 // that are not there yet, and updating the ones that are out of 4224 // date. 4225 function patchDisplay(cm, updateNumbersFrom, dims) { 4226 var display = cm.display, lineNumbers = cm.options.lineNumbers; 4227 var container = display.lineDiv, cur = container.firstChild; 4228 4229 function rm(node) { 4230 var next = node.nextSibling; 4231 // Works around a throw-scroll bug in OS X Webkit 4232 if (webkit && mac && cm.display.currentWheelTarget == node) 4233 { node.style.display = "none"; } 4234 else 4235 { node.parentNode.removeChild(node); } 4236 return next 4237 } 4238 4239 var view = display.view, lineN = display.viewFrom; 4240 // Loop over the elements in the view, syncing cur (the DOM nodes 4241 // in display.lineDiv) with the view as we go. 4242 for (var i = 0; i < view.length; i++) { 4243 var lineView = view[i]; 4244 if (lineView.hidden) ; else if (!lineView.node || lineView.node.parentNode != container) { // Not drawn yet 4245 var node = buildLineElement(cm, lineView, lineN, dims); 4246 container.insertBefore(node, cur); 4247 } else { // Already drawn 4248 while (cur != lineView.node) { cur = rm(cur); } 4249 var updateNumber = lineNumbers && updateNumbersFrom != null && 4250 updateNumbersFrom <= lineN && lineView.lineNumber; 4251 if (lineView.changes) { 4252 if (indexOf(lineView.changes, "gutter") > -1) { updateNumber = false; } 4253 updateLineForChanges(cm, lineView, lineN, dims); 4254 } 4255 if (updateNumber) { 4256 removeChildren(lineView.lineNumber); 4257 lineView.lineNumber.appendChild(document.createTextNode(lineNumberFor(cm.options, lineN))); 4258 } 4259 cur = lineView.node.nextSibling; 4260 } 4261 lineN += lineView.size; 4262 } 4263 while (cur) { cur = rm(cur); } 4264 } 4265 4266 function updateGutterSpace(display) { 4267 var width = display.gutters.offsetWidth; 4268 display.sizer.style.marginLeft = width + "px"; 4269 // Send an event to consumers responding to changes in gutter width. 4270 signalLater(display, "gutterChanged", display); 4271 } 4272 4273 function setDocumentHeight(cm, measure) { 4274 cm.display.sizer.style.minHeight = measure.docHeight + "px"; 4275 cm.display.heightForcer.style.top = measure.docHeight + "px"; 4276 cm.display.gutters.style.height = (measure.docHeight + cm.display.barHeight + scrollGap(cm)) + "px"; 4277 } 4278 4279 // Re-align line numbers and gutter marks to compensate for 4280 // horizontal scrolling. 4281 function alignHorizontally(cm) { 4282 var display = cm.display, view = display.view; 4283 if (!display.alignWidgets && (!display.gutters.firstChild || !cm.options.fixedGutter)) { return } 4284 var comp = compensateForHScroll(display) - display.scroller.scrollLeft + cm.doc.scrollLeft; 4285 var gutterW = display.gutters.offsetWidth, left = comp + "px"; 4286 for (var i = 0; i < view.length; i++) { if (!view[i].hidden) { 4287 if (cm.options.fixedGutter) { 4288 if (view[i].gutter) 4289 { view[i].gutter.style.left = left; } 4290 if (view[i].gutterBackground) 4291 { view[i].gutterBackground.style.left = left; } 4292 } 4293 var align = view[i].alignable; 4294 if (align) { for (var j = 0; j < align.length; j++) 4295 { align[j].style.left = left; } } 4296 } } 4297 if (cm.options.fixedGutter) 4298 { display.gutters.style.left = (comp + gutterW) + "px"; } 4299 } 4300 4301 // Used to ensure that the line number gutter is still the right 4302 // size for the current document size. Returns true when an update 4303 // is needed. 4304 function maybeUpdateLineNumberWidth(cm) { 4305 if (!cm.options.lineNumbers) { return false } 4306 var doc = cm.doc, last = lineNumberFor(cm.options, doc.first + doc.size - 1), display = cm.display; 4307 if (last.length != display.lineNumChars) { 4308 var test = display.measure.appendChild(elt("div", [elt("div", last)], 4309 "CodeMirror-linenumber CodeMirror-gutter-elt")); 4310 var innerW = test.firstChild.offsetWidth, padding = test.offsetWidth - innerW; 4311 display.lineGutter.style.width = ""; 4312 display.lineNumInnerWidth = Math.max(innerW, display.lineGutter.offsetWidth - padding) + 1; 4313 display.lineNumWidth = display.lineNumInnerWidth + padding; 4314 display.lineNumChars = display.lineNumInnerWidth ? last.length : -1; 4315 display.lineGutter.style.width = display.lineNumWidth + "px"; 4316 updateGutterSpace(cm.display); 4317 return true 4318 } 4319 return false 4320 } 4321 4322 function getGutters(gutters, lineNumbers) { 4323 var result = [], sawLineNumbers = false; 4324 for (var i = 0; i < gutters.length; i++) { 4325 var name = gutters[i], style = null; 4326 if (typeof name != "string") { style = name.style; name = name.className; } 4327 if (name == "CodeMirror-linenumbers") { 4328 if (!lineNumbers) { continue } 4329 else { sawLineNumbers = true; } 4330 } 4331 result.push({className: name, style: style}); 4332 } 4333 if (lineNumbers && !sawLineNumbers) { result.push({className: "CodeMirror-linenumbers", style: null}); } 4334 return result 4335 } 4336 4337 // Rebuild the gutter elements, ensure the margin to the left of the 4338 // code matches their width. 4339 function renderGutters(display) { 4340 var gutters = display.gutters, specs = display.gutterSpecs; 4341 removeChildren(gutters); 4342 display.lineGutter = null; 4343 for (var i = 0; i < specs.length; ++i) { 4344 var ref = specs[i]; 4345 var className = ref.className; 4346 var style = ref.style; 4347 var gElt = gutters.appendChild(elt("div", null, "CodeMirror-gutter " + className)); 4348 if (style) { gElt.style.cssText = style; } 4349 if (className == "CodeMirror-linenumbers") { 4350 display.lineGutter = gElt; 4351 gElt.style.width = (display.lineNumWidth || 1) + "px"; 4352 } 4353 } 4354 gutters.style.display = specs.length ? "" : "none"; 4355 updateGutterSpace(display); 4356 } 4357 4358 function updateGutters(cm) { 4359 renderGutters(cm.display); 4360 regChange(cm); 4361 alignHorizontally(cm); 4362 } 4363 4364 // The display handles the DOM integration, both for input reading 4365 // and content drawing. It holds references to DOM nodes and 4366 // display-related state. 4367 4368 function Display(place, doc, input, options) { 4369 var d = this; 4370 this.input = input; 4371 4372 // Covers bottom-right square when both scrollbars are present. 4373 d.scrollbarFiller = elt("div", null, "CodeMirror-scrollbar-filler"); 4374 d.scrollbarFiller.setAttribute("cm-not-content", "true"); 4375 // Covers bottom of gutter when coverGutterNextToScrollbar is on 4376 // and h scrollbar is present. 4377 d.gutterFiller = elt("div", null, "CodeMirror-gutter-filler"); 4378 d.gutterFiller.setAttribute("cm-not-content", "true"); 4379 // Will contain the actual code, positioned to cover the viewport. 4380 d.lineDiv = eltP("div", null, "CodeMirror-code"); 4381 // Elements are added to these to represent selection and cursors. 4382 d.selectionDiv = elt("div", null, null, "position: relative; z-index: 1"); 4383 d.cursorDiv = elt("div", null, "CodeMirror-cursors"); 4384 // A visibility: hidden element used to find the size of things. 4385 d.measure = elt("div", null, "CodeMirror-measure"); 4386 // When lines outside of the viewport are measured, they are drawn in this. 4387 d.lineMeasure = elt("div", null, "CodeMirror-measure"); 4388 // Wraps everything that needs to exist inside the vertically-padded coordinate system 4389 d.lineSpace = eltP("div", [d.measure, d.lineMeasure, d.selectionDiv, d.cursorDiv, d.lineDiv], 4390 null, "position: relative; outline: none"); 4391 var lines = eltP("div", [d.lineSpace], "CodeMirror-lines"); 4392 // Moved around its parent to cover visible view. 4393 d.mover = elt("div", [lines], null, "position: relative"); 4394 // Set to the height of the document, allowing scrolling. 4395 d.sizer = elt("div", [d.mover], "CodeMirror-sizer"); 4396 d.sizerWidth = null; 4397 // Behavior of elts with overflow: auto and padding is 4398 // inconsistent across browsers. This is used to ensure the 4399 // scrollable area is big enough. 4400 d.heightForcer = elt("div", null, null, "position: absolute; height: " + scrollerGap + "px; width: 1px;"); 4401 // Will contain the gutters, if any. 4402 d.gutters = elt("div", null, "CodeMirror-gutters"); 4403 d.lineGutter = null; 4404 // Actual scrollable element. 4405 d.scroller = elt("div", [d.sizer, d.heightForcer, d.gutters], "CodeMirror-scroll"); 4406 d.scroller.setAttribute("tabIndex", "-1"); 4407 // The element in which the editor lives. 4408 d.wrapper = elt("div", [d.scrollbarFiller, d.gutterFiller, d.scroller], "CodeMirror"); 4409 4410 // This attribute is respected by automatic translation systems such as Google Translate, 4411 // and may also be respected by tools used by human translators. 4412 d.wrapper.setAttribute('translate', 'no'); 4413 4414 // Work around IE7 z-index bug (not perfect, hence IE7 not really being supported) 4415 if (ie && ie_version < 8) { d.gutters.style.zIndex = -1; d.scroller.style.paddingRight = 0; } 4416 if (!webkit && !(gecko && mobile)) { d.scroller.draggable = true; } 4417 4418 if (place) { 4419 if (place.appendChild) { place.appendChild(d.wrapper); } 4420 else { place(d.wrapper); } 4421 } 4422 4423 // Current rendered range (may be bigger than the view window). 4424 d.viewFrom = d.viewTo = doc.first; 4425 d.reportedViewFrom = d.reportedViewTo = doc.first; 4426 // Information about the rendered lines. 4427 d.view = []; 4428 d.renderedView = null; 4429 // Holds info about a single rendered line when it was rendered 4430 // for measurement, while not in view. 4431 d.externalMeasured = null; 4432 // Empty space (in pixels) above the view 4433 d.viewOffset = 0; 4434 d.lastWrapHeight = d.lastWrapWidth = 0; 4435 d.updateLineNumbers = null; 4436 4437 d.nativeBarWidth = d.barHeight = d.barWidth = 0; 4438 d.scrollbarsClipped = false; 4439 4440 // Used to only resize the line number gutter when necessary (when 4441 // the amount of lines crosses a boundary that makes its width change) 4442 d.lineNumWidth = d.lineNumInnerWidth = d.lineNumChars = null; 4443 // Set to true when a non-horizontal-scrolling line widget is 4444 // added. As an optimization, line widget aligning is skipped when 4445 // this is false. 4446 d.alignWidgets = false; 4447 4448 d.cachedCharWidth = d.cachedTextHeight = d.cachedPaddingH = null; 4449 4450 // Tracks the maximum line length so that the horizontal scrollbar 4451 // can be kept static when scrolling. 4452 d.maxLine = null; 4453 d.maxLineLength = 0; 4454 d.maxLineChanged = false; 4455 4456 // Used for measuring wheel scrolling granularity 4457 d.wheelDX = d.wheelDY = d.wheelStartX = d.wheelStartY = null; 4458 4459 // True when shift is held down. 4460 d.shift = false; 4461 4462 // Used to track whether anything happened since the context menu 4463 // was opened. 4464 d.selForContextMenu = null; 4465 4466 d.activeTouch = null; 4467 4468 d.gutterSpecs = getGutters(options.gutters, options.lineNumbers); 4469 renderGutters(d); 4470 4471 input.init(d); 4472 } 4473 4474 // Since the delta values reported on mouse wheel events are 4475 // unstandardized between browsers and even browser versions, and 4476 // generally horribly unpredictable, this code starts by measuring 4477 // the scroll effect that the first few mouse wheel events have, 4478 // and, from that, detects the way it can convert deltas to pixel 4479 // offsets afterwards. 4480 // 4481 // The reason we want to know the amount a wheel event will scroll 4482 // is that it gives us a chance to update the display before the 4483 // actual scrolling happens, reducing flickering. 4484 4485 var wheelSamples = 0, wheelPixelsPerUnit = null; 4486 // Fill in a browser-detected starting value on browsers where we 4487 // know one. These don't have to be accurate -- the result of them 4488 // being wrong would just be a slight flicker on the first wheel 4489 // scroll (if it is large enough). 4490 if (ie) { wheelPixelsPerUnit = -.53; } 4491 else if (gecko) { wheelPixelsPerUnit = 15; } 4492 else if (chrome) { wheelPixelsPerUnit = -.7; } 4493 else if (safari) { wheelPixelsPerUnit = -1/3; } 4494 4495 function wheelEventDelta(e) { 4496 var dx = e.wheelDeltaX, dy = e.wheelDeltaY; 4497 if (dx == null && e.detail && e.axis == e.HORIZONTAL_AXIS) { dx = e.detail; } 4498 if (dy == null && e.detail && e.axis == e.VERTICAL_AXIS) { dy = e.detail; } 4499 else if (dy == null) { dy = e.wheelDelta; } 4500 return {x: dx, y: dy} 4501 } 4502 function wheelEventPixels(e) { 4503 var delta = wheelEventDelta(e); 4504 delta.x *= wheelPixelsPerUnit; 4505 delta.y *= wheelPixelsPerUnit; 4506 return delta 4507 } 4508 4509 function onScrollWheel(cm, e) { 4510 var delta = wheelEventDelta(e), dx = delta.x, dy = delta.y; 4511 var pixelsPerUnit = wheelPixelsPerUnit; 4512 if (e.deltaMode === 0) { 4513 dx = e.deltaX; 4514 dy = e.deltaY; 4515 pixelsPerUnit = 1; 4516 } 4517 4518 var display = cm.display, scroll = display.scroller; 4519 // Quit if there's nothing to scroll here 4520 var canScrollX = scroll.scrollWidth > scroll.clientWidth; 4521 var canScrollY = scroll.scrollHeight > scroll.clientHeight; 4522 if (!(dx && canScrollX || dy && canScrollY)) { return } 4523 4524 // Webkit browsers on OS X abort momentum scrolls when the target 4525 // of the scroll event is removed from the scrollable element. 4526 // This hack (see related code in patchDisplay) makes sure the 4527 // element is kept around. 4528 if (dy && mac && webkit) { 4529 outer: for (var cur = e.target, view = display.view; cur != scroll; cur = cur.parentNode) { 4530 for (var i = 0; i < view.length; i++) { 4531 if (view[i].node == cur) { 4532 cm.display.currentWheelTarget = cur; 4533 break outer 4534 } 4535 } 4536 } 4537 } 4538 4539 // On some browsers, horizontal scrolling will cause redraws to 4540 // happen before the gutter has been realigned, causing it to 4541 // wriggle around in a most unseemly way. When we have an 4542 // estimated pixels/delta value, we just handle horizontal 4543 // scrolling entirely here. It'll be slightly off from native, but 4544 // better than glitching out. 4545 if (dx && !gecko && !presto && pixelsPerUnit != null) { 4546 if (dy && canScrollY) 4547 { updateScrollTop(cm, Math.max(0, scroll.scrollTop + dy * pixelsPerUnit)); } 4548 setScrollLeft(cm, Math.max(0, scroll.scrollLeft + dx * pixelsPerUnit)); 4549 // Only prevent default scrolling if vertical scrolling is 4550 // actually possible. Otherwise, it causes vertical scroll 4551 // jitter on OSX trackpads when deltaX is small and deltaY 4552 // is large (issue #3579) 4553 if (!dy || (dy && canScrollY)) 4554 { e_preventDefault(e); } 4555 display.wheelStartX = null; // Abort measurement, if in progress 4556 return 4557 } 4558 4559 // 'Project' the visible viewport to cover the area that is being 4560 // scrolled into view (if we know enough to estimate it). 4561 if (dy && pixelsPerUnit != null) { 4562 var pixels = dy * pixelsPerUnit; 4563 var top = cm.doc.scrollTop, bot = top + display.wrapper.clientHeight; 4564 if (pixels < 0) { top = Math.max(0, top + pixels - 50); } 4565 else { bot = Math.min(cm.doc.height, bot + pixels + 50); } 4566 updateDisplaySimple(cm, {top: top, bottom: bot}); 4567 } 4568 4569 if (wheelSamples < 20 && e.deltaMode !== 0) { 4570 if (display.wheelStartX == null) { 4571 display.wheelStartX = scroll.scrollLeft; display.wheelStartY = scroll.scrollTop; 4572 display.wheelDX = dx; display.wheelDY = dy; 4573 setTimeout(function () { 4574 if (display.wheelStartX == null) { return } 4575 var movedX = scroll.scrollLeft - display.wheelStartX; 4576 var movedY = scroll.scrollTop - display.wheelStartY; 4577 var sample = (movedY && display.wheelDY && movedY / display.wheelDY) || 4578 (movedX && display.wheelDX && movedX / display.wheelDX); 4579 display.wheelStartX = display.wheelStartY = null; 4580 if (!sample) { return } 4581 wheelPixelsPerUnit = (wheelPixelsPerUnit * wheelSamples + sample) / (wheelSamples + 1); 4582 ++wheelSamples; 4583 }, 200); 4584 } else { 4585 display.wheelDX += dx; display.wheelDY += dy; 4586 } 4587 } 4588 } 4589 4590 // Selection objects are immutable. A new one is created every time 4591 // the selection changes. A selection is one or more non-overlapping 4592 // (and non-touching) ranges, sorted, and an integer that indicates 4593 // which one is the primary selection (the one that's scrolled into 4594 // view, that getCursor returns, etc). 4595 var Selection = function(ranges, primIndex) { 4596 this.ranges = ranges; 4597 this.primIndex = primIndex; 4598 }; 4599 4600 Selection.prototype.primary = function () { return this.ranges[this.primIndex] }; 4601 4602 Selection.prototype.equals = function (other) { 4603 if (other == this) { return true } 4604 if (other.primIndex != this.primIndex || other.ranges.length != this.ranges.length) { return false } 4605 for (var i = 0; i < this.ranges.length; i++) { 4606 var here = this.ranges[i], there = other.ranges[i]; 4607 if (!equalCursorPos(here.anchor, there.anchor) || !equalCursorPos(here.head, there.head)) { return false } 4608 } 4609 return true 4610 }; 4611 4612 Selection.prototype.deepCopy = function () { 4613 var out = []; 4614 for (var i = 0; i < this.ranges.length; i++) 4615 { out[i] = new Range(copyPos(this.ranges[i].anchor), copyPos(this.ranges[i].head)); } 4616 return new Selection(out, this.primIndex) 4617 }; 4618 4619 Selection.prototype.somethingSelected = function () { 4620 for (var i = 0; i < this.ranges.length; i++) 4621 { if (!this.ranges[i].empty()) { return true } } 4622 return false 4623 }; 4624 4625 Selection.prototype.contains = function (pos, end) { 4626 if (!end) { end = pos; } 4627 for (var i = 0; i < this.ranges.length; i++) { 4628 var range = this.ranges[i]; 4629 if (cmp(end, range.from()) >= 0 && cmp(pos, range.to()) <= 0) 4630 { return i } 4631 } 4632 return -1 4633 }; 4634 4635 var Range = function(anchor, head) { 4636 this.anchor = anchor; this.head = head; 4637 }; 4638 4639 Range.prototype.from = function () { return minPos(this.anchor, this.head) }; 4640 Range.prototype.to = function () { return maxPos(this.anchor, this.head) }; 4641 Range.prototype.empty = function () { return this.head.line == this.anchor.line && this.head.ch == this.anchor.ch }; 4642 4643 // Take an unsorted, potentially overlapping set of ranges, and 4644 // build a selection out of it. 'Consumes' ranges array (modifying 4645 // it). 4646 function normalizeSelection(cm, ranges, primIndex) { 4647 var mayTouch = cm && cm.options.selectionsMayTouch; 4648 var prim = ranges[primIndex]; 4649 ranges.sort(function (a, b) { return cmp(a.from(), b.from()); }); 4650 primIndex = indexOf(ranges, prim); 4651 for (var i = 1; i < ranges.length; i++) { 4652 var cur = ranges[i], prev = ranges[i - 1]; 4653 var diff = cmp(prev.to(), cur.from()); 4654 if (mayTouch && !cur.empty() ? diff > 0 : diff >= 0) { 4655 var from = minPos(prev.from(), cur.from()), to = maxPos(prev.to(), cur.to()); 4656 var inv = prev.empty() ? cur.from() == cur.head : prev.from() == prev.head; 4657 if (i <= primIndex) { --primIndex; } 4658 ranges.splice(--i, 2, new Range(inv ? to : from, inv ? from : to)); 4659 } 4660 } 4661 return new Selection(ranges, primIndex) 4662 } 4663 4664 function simpleSelection(anchor, head) { 4665 return new Selection([new Range(anchor, head || anchor)], 0) 4666 } 4667 4668 // Compute the position of the end of a change (its 'to' property 4669 // refers to the pre-change end). 4670 function changeEnd(change) { 4671 if (!change.text) { return change.to } 4672 return Pos(change.from.line + change.text.length - 1, 4673 lst(change.text).length + (change.text.length == 1 ? change.from.ch : 0)) 4674 } 4675 4676 // Adjust a position to refer to the post-change position of the 4677 // same text, or the end of the change if the change covers it. 4678 function adjustForChange(pos, change) { 4679 if (cmp(pos, change.from) < 0) { return pos } 4680 if (cmp(pos, change.to) <= 0) { return changeEnd(change) } 4681 4682 var line = pos.line + change.text.length - (change.to.line - change.from.line) - 1, ch = pos.ch; 4683 if (pos.line == change.to.line) { ch += changeEnd(change).ch - change.to.ch; } 4684 return Pos(line, ch) 4685 } 4686 4687 function computeSelAfterChange(doc, change) { 4688 var out = []; 4689 for (var i = 0; i < doc.sel.ranges.length; i++) { 4690 var range = doc.sel.ranges[i]; 4691 out.push(new Range(adjustForChange(range.anchor, change), 4692 adjustForChange(range.head, change))); 4693 } 4694 return normalizeSelection(doc.cm, out, doc.sel.primIndex) 4695 } 4696 4697 function offsetPos(pos, old, nw) { 4698 if (pos.line == old.line) 4699 { return Pos(nw.line, pos.ch - old.ch + nw.ch) } 4700 else 4701 { return Pos(nw.line + (pos.line - old.line), pos.ch) } 4702 } 4703 4704 // Used by replaceSelections to allow moving the selection to the 4705 // start or around the replaced test. Hint may be "start" or "around". 4706 function computeReplacedSel(doc, changes, hint) { 4707 var out = []; 4708 var oldPrev = Pos(doc.first, 0), newPrev = oldPrev; 4709 for (var i = 0; i < changes.length; i++) { 4710 var change = changes[i]; 4711 var from = offsetPos(change.from, oldPrev, newPrev); 4712 var to = offsetPos(changeEnd(change), oldPrev, newPrev); 4713 oldPrev = change.to; 4714 newPrev = to; 4715 if (hint == "around") { 4716 var range = doc.sel.ranges[i], inv = cmp(range.head, range.anchor) < 0; 4717 out[i] = new Range(inv ? to : from, inv ? from : to); 4718 } else { 4719 out[i] = new Range(from, from); 4720 } 4721 } 4722 return new Selection(out, doc.sel.primIndex) 4723 } 4724 4725 // Used to get the editor into a consistent state again when options change. 4726 4727 function loadMode(cm) { 4728 cm.doc.mode = getMode(cm.options, cm.doc.modeOption); 4729 resetModeState(cm); 4730 } 4731 4732 function resetModeState(cm) { 4733 cm.doc.iter(function (line) { 4734 if (line.stateAfter) { line.stateAfter = null; } 4735 if (line.styles) { line.styles = null; } 4736 }); 4737 cm.doc.modeFrontier = cm.doc.highlightFrontier = cm.doc.first; 4738 startWorker(cm, 100); 4739 cm.state.modeGen++; 4740 if (cm.curOp) { regChange(cm); } 4741 } 4742 4743 // DOCUMENT DATA STRUCTURE 4744 4745 // By default, updates that start and end at the beginning of a line 4746 // are treated specially, in order to make the association of line 4747 // widgets and marker elements with the text behave more intuitive. 4748 function isWholeLineUpdate(doc, change) { 4749 return change.from.ch == 0 && change.to.ch == 0 && lst(change.text) == "" && 4750 (!doc.cm || doc.cm.options.wholeLineUpdateBefore) 4751 } 4752 4753 // Perform a change on the document data structure. 4754 function updateDoc(doc, change, markedSpans, estimateHeight) { 4755 function spansFor(n) {return markedSpans ? markedSpans[n] : null} 4756 function update(line, text, spans) { 4757 updateLine(line, text, spans, estimateHeight); 4758 signalLater(line, "change", line, change); 4759 } 4760 function linesFor(start, end) { 4761 var result = []; 4762 for (var i = start; i < end; ++i) 4763 { result.push(new Line(text[i], spansFor(i), estimateHeight)); } 4764 return result 4765 } 4766 4767 var from = change.from, to = change.to, text = change.text; 4768 var firstLine = getLine(doc, from.line), lastLine = getLine(doc, to.line); 4769 var lastText = lst(text), lastSpans = spansFor(text.length - 1), nlines = to.line - from.line; 4770 4771 // Adjust the line structure 4772 if (change.full) { 4773 doc.insert(0, linesFor(0, text.length)); 4774 doc.remove(text.length, doc.size - text.length); 4775 } else if (isWholeLineUpdate(doc, change)) { 4776 // This is a whole-line replace. Treated specially to make 4777 // sure line objects move the way they are supposed to. 4778 var added = linesFor(0, text.length - 1); 4779 update(lastLine, lastLine.text, lastSpans); 4780 if (nlines) { doc.remove(from.line, nlines); } 4781 if (added.length) { doc.insert(from.line, added); } 4782 } else if (firstLine == lastLine) { 4783 if (text.length == 1) { 4784 update(firstLine, firstLine.text.slice(0, from.ch) + lastText + firstLine.text.slice(to.ch), lastSpans); 4785 } else { 4786 var added$1 = linesFor(1, text.length - 1); 4787 added$1.push(new Line(lastText + firstLine.text.slice(to.ch), lastSpans, estimateHeight)); 4788 update(firstLine, firstLine.text.slice(0, from.ch) + text[0], spansFor(0)); 4789 doc.insert(from.line + 1, added$1); 4790 } 4791 } else if (text.length == 1) { 4792 update(firstLine, firstLine.text.slice(0, from.ch) + text[0] + lastLine.text.slice(to.ch), spansFor(0)); 4793 doc.remove(from.line + 1, nlines); 4794 } else { 4795 update(firstLine, firstLine.text.slice(0, from.ch) + text[0], spansFor(0)); 4796 update(lastLine, lastText + lastLine.text.slice(to.ch), lastSpans); 4797 var added$2 = linesFor(1, text.length - 1); 4798 if (nlines > 1) { doc.remove(from.line + 1, nlines - 1); } 4799 doc.insert(from.line + 1, added$2); 4800 } 4801 4802 signalLater(doc, "change", doc, change); 4803 } 4804 4805 // Call f for all linked documents. 4806 function linkedDocs(doc, f, sharedHistOnly) { 4807 function propagate(doc, skip, sharedHist) { 4808 if (doc.linked) { for (var i = 0; i < doc.linked.length; ++i) { 4809 var rel = doc.linked[i]; 4810 if (rel.doc == skip) { continue } 4811 var shared = sharedHist && rel.sharedHist; 4812 if (sharedHistOnly && !shared) { continue } 4813 f(rel.doc, shared); 4814 propagate(rel.doc, doc, shared); 4815 } } 4816 } 4817 propagate(doc, null, true); 4818 } 4819 4820 // Attach a document to an editor. 4821 function attachDoc(cm, doc) { 4822 if (doc.cm) { throw new Error("This document is already in use.") } 4823 cm.doc = doc; 4824 doc.cm = cm; 4825 estimateLineHeights(cm); 4826 loadMode(cm); 4827 setDirectionClass(cm); 4828 cm.options.direction = doc.direction; 4829 if (!cm.options.lineWrapping) { findMaxLine(cm); } 4830 cm.options.mode = doc.modeOption; 4831 regChange(cm); 4832 } 4833 4834 function setDirectionClass(cm) { 4835 (cm.doc.direction == "rtl" ? addClass : rmClass)(cm.display.lineDiv, "CodeMirror-rtl"); 4836 } 4837 4838 function directionChanged(cm) { 4839 runInOp(cm, function () { 4840 setDirectionClass(cm); 4841 regChange(cm); 4842 }); 4843 } 4844 4845 function History(prev) { 4846 // Arrays of change events and selections. Doing something adds an 4847 // event to done and clears undo. Undoing moves events from done 4848 // to undone, redoing moves them in the other direction. 4849 this.done = []; this.undone = []; 4850 this.undoDepth = prev ? prev.undoDepth : Infinity; 4851 // Used to track when changes can be merged into a single undo 4852 // event 4853 this.lastModTime = this.lastSelTime = 0; 4854 this.lastOp = this.lastSelOp = null; 4855 this.lastOrigin = this.lastSelOrigin = null; 4856 // Used by the isClean() method 4857 this.generation = this.maxGeneration = prev ? prev.maxGeneration : 1; 4858 } 4859 4860 // Create a history change event from an updateDoc-style change 4861 // object. 4862 function historyChangeFromChange(doc, change) { 4863 var histChange = {from: copyPos(change.from), to: changeEnd(change), text: getBetween(doc, change.from, change.to)}; 4864 attachLocalSpans(doc, histChange, change.from.line, change.to.line + 1); 4865 linkedDocs(doc, function (doc) { return attachLocalSpans(doc, histChange, change.from.line, change.to.line + 1); }, true); 4866 return histChange 4867 } 4868 4869 // Pop all selection events off the end of a history array. Stop at 4870 // a change event. 4871 function clearSelectionEvents(array) { 4872 while (array.length) { 4873 var last = lst(array); 4874 if (last.ranges) { array.pop(); } 4875 else { break } 4876 } 4877 } 4878 4879 // Find the top change event in the history. Pop off selection 4880 // events that are in the way. 4881 function lastChangeEvent(hist, force) { 4882 if (force) { 4883 clearSelectionEvents(hist.done); 4884 return lst(hist.done) 4885 } else if (hist.done.length && !lst(hist.done).ranges) { 4886 return lst(hist.done) 4887 } else if (hist.done.length > 1 && !hist.done[hist.done.length - 2].ranges) { 4888 hist.done.pop(); 4889 return lst(hist.done) 4890 } 4891 } 4892 4893 // Register a change in the history. Merges changes that are within 4894 // a single operation, or are close together with an origin that 4895 // allows merging (starting with "+") into a single event. 4896 function addChangeToHistory(doc, change, selAfter, opId) { 4897 var hist = doc.history; 4898 hist.undone.length = 0; 4899 var time = +new Date, cur; 4900 var last; 4901 4902 if ((hist.lastOp == opId || 4903 hist.lastOrigin == change.origin && change.origin && 4904 ((change.origin.charAt(0) == "+" && hist.lastModTime > time - (doc.cm ? doc.cm.options.historyEventDelay : 500)) || 4905 change.origin.charAt(0) == "*")) && 4906 (cur = lastChangeEvent(hist, hist.lastOp == opId))) { 4907 // Merge this change into the last event 4908 last = lst(cur.changes); 4909 if (cmp(change.from, change.to) == 0 && cmp(change.from, last.to) == 0) { 4910 // Optimized case for simple insertion -- don't want to add 4911 // new changesets for every character typed 4912 last.to = changeEnd(change); 4913 } else { 4914 // Add new sub-event 4915 cur.changes.push(historyChangeFromChange(doc, change)); 4916 } 4917 } else { 4918 // Can not be merged, start a new event. 4919 var before = lst(hist.done); 4920 if (!before || !before.ranges) 4921 { pushSelectionToHistory(doc.sel, hist.done); } 4922 cur = {changes: [historyChangeFromChange(doc, change)], 4923 generation: hist.generation}; 4924 hist.done.push(cur); 4925 while (hist.done.length > hist.undoDepth) { 4926 hist.done.shift(); 4927 if (!hist.done[0].ranges) { hist.done.shift(); } 4928 } 4929 } 4930 hist.done.push(selAfter); 4931 hist.generation = ++hist.maxGeneration; 4932 hist.lastModTime = hist.lastSelTime = time; 4933 hist.lastOp = hist.lastSelOp = opId; 4934 hist.lastOrigin = hist.lastSelOrigin = change.origin; 4935 4936 if (!last) { signal(doc, "historyAdded"); } 4937 } 4938 4939 function selectionEventCanBeMerged(doc, origin, prev, sel) { 4940 var ch = origin.charAt(0); 4941 return ch == "*" || 4942 ch == "+" && 4943 prev.ranges.length == sel.ranges.length && 4944 prev.somethingSelected() == sel.somethingSelected() && 4945 new Date - doc.history.lastSelTime <= (doc.cm ? doc.cm.options.historyEventDelay : 500) 4946 } 4947 4948 // Called whenever the selection changes, sets the new selection as 4949 // the pending selection in the history, and pushes the old pending 4950 // selection into the 'done' array when it was significantly 4951 // different (in number of selected ranges, emptiness, or time). 4952 function addSelectionToHistory(doc, sel, opId, options) { 4953 var hist = doc.history, origin = options && options.origin; 4954 4955 // A new event is started when the previous origin does not match 4956 // the current, or the origins don't allow matching. Origins 4957 // starting with * are always merged, those starting with + are 4958 // merged when similar and close together in time. 4959 if (opId == hist.lastSelOp || 4960 (origin && hist.lastSelOrigin == origin && 4961 (hist.lastModTime == hist.lastSelTime && hist.lastOrigin == origin || 4962 selectionEventCanBeMerged(doc, origin, lst(hist.done), sel)))) 4963 { hist.done[hist.done.length - 1] = sel; } 4964 else 4965 { pushSelectionToHistory(sel, hist.done); } 4966 4967 hist.lastSelTime = +new Date; 4968 hist.lastSelOrigin = origin; 4969 hist.lastSelOp = opId; 4970 if (options && options.clearRedo !== false) 4971 { clearSelectionEvents(hist.undone); } 4972 } 4973 4974 function pushSelectionToHistory(sel, dest) { 4975 var top = lst(dest); 4976 if (!(top && top.ranges && top.equals(sel))) 4977 { dest.push(sel); } 4978 } 4979 4980 // Used to store marked span information in the history. 4981 function attachLocalSpans(doc, change, from, to) { 4982 var existing = change["spans_" + doc.id], n = 0; 4983 doc.iter(Math.max(doc.first, from), Math.min(doc.first + doc.size, to), function (line) { 4984 if (line.markedSpans) 4985 { (existing || (existing = change["spans_" + doc.id] = {}))[n] = line.markedSpans; } 4986 ++n; 4987 }); 4988 } 4989 4990 // When un/re-doing restores text containing marked spans, those 4991 // that have been explicitly cleared should not be restored. 4992 function removeClearedSpans(spans) { 4993 if (!spans) { return null } 4994 var out; 4995 for (var i = 0; i < spans.length; ++i) { 4996 if (spans[i].marker.explicitlyCleared) { if (!out) { out = spans.slice(0, i); } } 4997 else if (out) { out.push(spans[i]); } 4998 } 4999 return !out ? spans : out.length ? out : null 5000 } 5001 5002 // Retrieve and filter the old marked spans stored in a change event. 5003 function getOldSpans(doc, change) { 5004 var found = change["spans_" + doc.id]; 5005 if (!found) { return null } 5006 var nw = []; 5007 for (var i = 0; i < change.text.length; ++i) 5008 { nw.push(removeClearedSpans(found[i])); } 5009 return nw 5010 } 5011 5012 // Used for un/re-doing changes from the history. Combines the 5013 // result of computing the existing spans with the set of spans that 5014 // existed in the history (so that deleting around a span and then 5015 // undoing brings back the span). 5016 function mergeOldSpans(doc, change) { 5017 var old = getOldSpans(doc, change); 5018 var stretched = stretchSpansOverChange(doc, change); 5019 if (!old) { return stretched } 5020 if (!stretched) { return old } 5021 5022 for (var i = 0; i < old.length; ++i) { 5023 var oldCur = old[i], stretchCur = stretched[i]; 5024 if (oldCur && stretchCur) { 5025 spans: for (var j = 0; j < stretchCur.length; ++j) { 5026 var span = stretchCur[j]; 5027 for (var k = 0; k < oldCur.length; ++k) 5028 { if (oldCur[k].marker == span.marker) { continue spans } } 5029 oldCur.push(span); 5030 } 5031 } else if (stretchCur) { 5032 old[i] = stretchCur; 5033 } 5034 } 5035 return old 5036 } 5037 5038 // Used both to provide a JSON-safe object in .getHistory, and, when 5039 // detaching a document, to split the history in two 5040 function copyHistoryArray(events, newGroup, instantiateSel) { 5041 var copy = []; 5042 for (var i = 0; i < events.length; ++i) { 5043 var event = events[i]; 5044 if (event.ranges) { 5045 copy.push(instantiateSel ? Selection.prototype.deepCopy.call(event) : event); 5046 continue 5047 } 5048 var changes = event.changes, newChanges = []; 5049 copy.push({changes: newChanges}); 5050 for (var j = 0; j < changes.length; ++j) { 5051 var change = changes[j], m = (void 0); 5052 newChanges.push({from: change.from, to: change.to, text: change.text}); 5053 if (newGroup) { for (var prop in change) { if (m = prop.match(/^spans_(\d+)$/)) { 5054 if (indexOf(newGroup, Number(m[1])) > -1) { 5055 lst(newChanges)[prop] = change[prop]; 5056 delete change[prop]; 5057 } 5058 } } } 5059 } 5060 } 5061 return copy 5062 } 5063 5064 // The 'scroll' parameter given to many of these indicated whether 5065 // the new cursor position should be scrolled into view after 5066 // modifying the selection. 5067 5068 // If shift is held or the extend flag is set, extends a range to 5069 // include a given position (and optionally a second position). 5070 // Otherwise, simply returns the range between the given positions. 5071 // Used for cursor motion and such. 5072 function extendRange(range, head, other, extend) { 5073 if (extend) { 5074 var anchor = range.anchor; 5075 if (other) { 5076 var posBefore = cmp(head, anchor) < 0; 5077 if (posBefore != (cmp(other, anchor) < 0)) { 5078 anchor = head; 5079 head = other; 5080 } else if (posBefore != (cmp(head, other) < 0)) { 5081 head = other; 5082 } 5083 } 5084 return new Range(anchor, head) 5085 } else { 5086 return new Range(other || head, head) 5087 } 5088 } 5089 5090 // Extend the primary selection range, discard the rest. 5091 function extendSelection(doc, head, other, options, extend) { 5092 if (extend == null) { extend = doc.cm && (doc.cm.display.shift || doc.extend); } 5093 setSelection(doc, new Selection([extendRange(doc.sel.primary(), head, other, extend)], 0), options); 5094 } 5095 5096 // Extend all selections (pos is an array of selections with length 5097 // equal the number of selections) 5098 function extendSelections(doc, heads, options) { 5099 var out = []; 5100 var extend = doc.cm && (doc.cm.display.shift || doc.extend); 5101 for (var i = 0; i < doc.sel.ranges.length; i++) 5102 { out[i] = extendRange(doc.sel.ranges[i], heads[i], null, extend); } 5103 var newSel = normalizeSelection(doc.cm, out, doc.sel.primIndex); 5104 setSelection(doc, newSel, options); 5105 } 5106 5107 // Updates a single range in the selection. 5108 function replaceOneSelection(doc, i, range, options) { 5109 var ranges = doc.sel.ranges.slice(0); 5110 ranges[i] = range; 5111 setSelection(doc, normalizeSelection(doc.cm, ranges, doc.sel.primIndex), options); 5112 } 5113 5114 // Reset the selection to a single range. 5115 function setSimpleSelection(doc, anchor, head, options) { 5116 setSelection(doc, simpleSelection(anchor, head), options); 5117 } 5118 5119 // Give beforeSelectionChange handlers a change to influence a 5120 // selection update. 5121 function filterSelectionChange(doc, sel, options) { 5122 var obj = { 5123 ranges: sel.ranges, 5124 update: function(ranges) { 5125 this.ranges = []; 5126 for (var i = 0; i < ranges.length; i++) 5127 { this.ranges[i] = new Range(clipPos(doc, ranges[i].anchor), 5128 clipPos(doc, ranges[i].head)); } 5129 }, 5130 origin: options && options.origin 5131 }; 5132 signal(doc, "beforeSelectionChange", doc, obj); 5133 if (doc.cm) { signal(doc.cm, "beforeSelectionChange", doc.cm, obj); } 5134 if (obj.ranges != sel.ranges) { return normalizeSelection(doc.cm, obj.ranges, obj.ranges.length - 1) } 5135 else { return sel } 5136 } 5137 5138 function setSelectionReplaceHistory(doc, sel, options) { 5139 var done = doc.history.done, last = lst(done); 5140 if (last && last.ranges) { 5141 done[done.length - 1] = sel; 5142 setSelectionNoUndo(doc, sel, options); 5143 } else { 5144 setSelection(doc, sel, options); 5145 } 5146 } 5147 5148 // Set a new selection. 5149 function setSelection(doc, sel, options) { 5150 setSelectionNoUndo(doc, sel, options); 5151 addSelectionToHistory(doc, doc.sel, doc.cm ? doc.cm.curOp.id : NaN, options); 5152 } 5153 5154 function setSelectionNoUndo(doc, sel, options) { 5155 if (hasHandler(doc, "beforeSelectionChange") || doc.cm && hasHandler(doc.cm, "beforeSelectionChange")) 5156 { sel = filterSelectionChange(doc, sel, options); } 5157 5158 var bias = options && options.bias || 5159 (cmp(sel.primary().head, doc.sel.primary().head) < 0 ? -1 : 1); 5160 setSelectionInner(doc, skipAtomicInSelection(doc, sel, bias, true)); 5161 5162 if (!(options && options.scroll === false) && doc.cm && doc.cm.getOption("readOnly") != "nocursor") 5163 { ensureCursorVisible(doc.cm); } 5164 } 5165 5166 function setSelectionInner(doc, sel) { 5167 if (sel.equals(doc.sel)) { return } 5168 5169 doc.sel = sel; 5170 5171 if (doc.cm) { 5172 doc.cm.curOp.updateInput = 1; 5173 doc.cm.curOp.selectionChanged = true; 5174 signalCursorActivity(doc.cm); 5175 } 5176 signalLater(doc, "cursorActivity", doc); 5177 } 5178 5179 // Verify that the selection does not partially select any atomic 5180 // marked ranges. 5181 function reCheckSelection(doc) { 5182 setSelectionInner(doc, skipAtomicInSelection(doc, doc.sel, null, false)); 5183 } 5184 5185 // Return a selection that does not partially select any atomic 5186 // ranges. 5187 function skipAtomicInSelection(doc, sel, bias, mayClear) { 5188 var out; 5189 for (var i = 0; i < sel.ranges.length; i++) { 5190 var range = sel.ranges[i]; 5191 var old = sel.ranges.length == doc.sel.ranges.length && doc.sel.ranges[i]; 5192 var newAnchor = skipAtomic(doc, range.anchor, old && old.anchor, bias, mayClear); 5193 var newHead = skipAtomic(doc, range.head, old && old.head, bias, mayClear); 5194 if (out || newAnchor != range.anchor || newHead != range.head) { 5195 if (!out) { out = sel.ranges.slice(0, i); } 5196 out[i] = new Range(newAnchor, newHead); 5197 } 5198 } 5199 return out ? normalizeSelection(doc.cm, out, sel.primIndex) : sel 5200 } 5201 5202 function skipAtomicInner(doc, pos, oldPos, dir, mayClear) { 5203 var line = getLine(doc, pos.line); 5204 if (line.markedSpans) { for (var i = 0; i < line.markedSpans.length; ++i) { 5205 var sp = line.markedSpans[i], m = sp.marker; 5206 5207 // Determine if we should prevent the cursor being placed to the left/right of an atomic marker 5208 // Historically this was determined using the inclusiveLeft/Right option, but the new way to control it 5209 // is with selectLeft/Right 5210 var preventCursorLeft = ("selectLeft" in m) ? !m.selectLeft : m.inclusiveLeft; 5211 var preventCursorRight = ("selectRight" in m) ? !m.selectRight : m.inclusiveRight; 5212 5213 if ((sp.from == null || (preventCursorLeft ? sp.from <= pos.ch : sp.from < pos.ch)) && 5214 (sp.to == null || (preventCursorRight ? sp.to >= pos.ch : sp.to > pos.ch))) { 5215 if (mayClear) { 5216 signal(m, "beforeCursorEnter"); 5217 if (m.explicitlyCleared) { 5218 if (!line.markedSpans) { break } 5219 else {--i; continue} 5220 } 5221 } 5222 if (!m.atomic) { continue } 5223 5224 if (oldPos) { 5225 var near = m.find(dir < 0 ? 1 : -1), diff = (void 0); 5226 if (dir < 0 ? preventCursorRight : preventCursorLeft) 5227 { near = movePos(doc, near, -dir, near && near.line == pos.line ? line : null); } 5228 if (near && near.line == pos.line && (diff = cmp(near, oldPos)) && (dir < 0 ? diff < 0 : diff > 0)) 5229 { return skipAtomicInner(doc, near, pos, dir, mayClear) } 5230 } 5231 5232 var far = m.find(dir < 0 ? -1 : 1); 5233 if (dir < 0 ? preventCursorLeft : preventCursorRight) 5234 { far = movePos(doc, far, dir, far.line == pos.line ? line : null); } 5235 return far ? skipAtomicInner(doc, far, pos, dir, mayClear) : null 5236 } 5237 } } 5238 return pos 5239 } 5240 5241 // Ensure a given position is not inside an atomic range. 5242 function skipAtomic(doc, pos, oldPos, bias, mayClear) { 5243 var dir = bias || 1; 5244 var found = skipAtomicInner(doc, pos, oldPos, dir, mayClear) || 5245 (!mayClear && skipAtomicInner(doc, pos, oldPos, dir, true)) || 5246 skipAtomicInner(doc, pos, oldPos, -dir, mayClear) || 5247 (!mayClear && skipAtomicInner(doc, pos, oldPos, -dir, true)); 5248 if (!found) { 5249 doc.cantEdit = true; 5250 return Pos(doc.first, 0) 5251 } 5252 return found 5253 } 5254 5255 function movePos(doc, pos, dir, line) { 5256 if (dir < 0 && pos.ch == 0) { 5257 if (pos.line > doc.first) { return clipPos(doc, Pos(pos.line - 1)) } 5258 else { return null } 5259 } else if (dir > 0 && pos.ch == (line || getLine(doc, pos.line)).text.length) { 5260 if (pos.line < doc.first + doc.size - 1) { return Pos(pos.line + 1, 0) } 5261 else { return null } 5262 } else { 5263 return new Pos(pos.line, pos.ch + dir) 5264 } 5265 } 5266 5267 function selectAll(cm) { 5268 cm.setSelection(Pos(cm.firstLine(), 0), Pos(cm.lastLine()), sel_dontScroll); 5269 } 5270 5271 // UPDATING 5272 5273 // Allow "beforeChange" event handlers to influence a change 5274 function filterChange(doc, change, update) { 5275 var obj = { 5276 canceled: false, 5277 from: change.from, 5278 to: change.to, 5279 text: change.text, 5280 origin: change.origin, 5281 cancel: function () { return obj.canceled = true; } 5282 }; 5283 if (update) { obj.update = function (from, to, text, origin) { 5284 if (from) { obj.from = clipPos(doc, from); } 5285 if (to) { obj.to = clipPos(doc, to); } 5286 if (text) { obj.text = text; } 5287 if (origin !== undefined) { obj.origin = origin; } 5288 }; } 5289 signal(doc, "beforeChange", doc, obj); 5290 if (doc.cm) { signal(doc.cm, "beforeChange", doc.cm, obj); } 5291 5292 if (obj.canceled) { 5293 if (doc.cm) { doc.cm.curOp.updateInput = 2; } 5294 return null 5295 } 5296 return {from: obj.from, to: obj.to, text: obj.text, origin: obj.origin} 5297 } 5298 5299 // Apply a change to a document, and add it to the document's 5300 // history, and propagating it to all linked documents. 5301 function makeChange(doc, change, ignoreReadOnly) { 5302 if (doc.cm) { 5303 if (!doc.cm.curOp) { return operation(doc.cm, makeChange)(doc, change, ignoreReadOnly) } 5304 if (doc.cm.state.suppressEdits) { return } 5305 } 5306 5307 if (hasHandler(doc, "beforeChange") || doc.cm && hasHandler(doc.cm, "beforeChange")) { 5308 change = filterChange(doc, change, true); 5309 if (!change) { return } 5310 } 5311 5312 // Possibly split or suppress the update based on the presence 5313 // of read-only spans in its range. 5314 var split = sawReadOnlySpans && !ignoreReadOnly && removeReadOnlyRanges(doc, change.from, change.to); 5315 if (split) { 5316 for (var i = split.length - 1; i >= 0; --i) 5317 { makeChangeInner(doc, {from: split[i].from, to: split[i].to, text: i ? [""] : change.text, origin: change.origin}); } 5318 } else { 5319 makeChangeInner(doc, change); 5320 } 5321 } 5322 5323 function makeChangeInner(doc, change) { 5324 if (change.text.length == 1 && change.text[0] == "" && cmp(change.from, change.to) == 0) { return } 5325 var selAfter = computeSelAfterChange(doc, change); 5326 addChangeToHistory(doc, change, selAfter, doc.cm ? doc.cm.curOp.id : NaN); 5327 5328 makeChangeSingleDoc(doc, change, selAfter, stretchSpansOverChange(doc, change)); 5329 var rebased = []; 5330 5331 linkedDocs(doc, function (doc, sharedHist) { 5332 if (!sharedHist && indexOf(rebased, doc.history) == -1) { 5333 rebaseHist(doc.history, change); 5334 rebased.push(doc.history); 5335 } 5336 makeChangeSingleDoc(doc, change, null, stretchSpansOverChange(doc, change)); 5337 }); 5338 } 5339 5340 // Revert a change stored in a document's history. 5341 function makeChangeFromHistory(doc, type, allowSelectionOnly) { 5342 var suppress = doc.cm && doc.cm.state.suppressEdits; 5343 if (suppress && !allowSelectionOnly) { return } 5344 5345 var hist = doc.history, event, selAfter = doc.sel; 5346 var source = type == "undo" ? hist.done : hist.undone, dest = type == "undo" ? hist.undone : hist.done; 5347 5348 // Verify that there is a useable event (so that ctrl-z won't 5349 // needlessly clear selection events) 5350 var i = 0; 5351 for (; i < source.length; i++) { 5352 event = source[i]; 5353 if (allowSelectionOnly ? event.ranges && !event.equals(doc.sel) : !event.ranges) 5354 { break } 5355 } 5356 if (i == source.length) { return } 5357 hist.lastOrigin = hist.lastSelOrigin = null; 5358 5359 for (;;) { 5360 event = source.pop(); 5361 if (event.ranges) { 5362 pushSelectionToHistory(event, dest); 5363 if (allowSelectionOnly && !event.equals(doc.sel)) { 5364 setSelection(doc, event, {clearRedo: false}); 5365 return 5366 } 5367 selAfter = event; 5368 } else if (suppress) { 5369 source.push(event); 5370 return 5371 } else { break } 5372 } 5373 5374 // Build up a reverse change object to add to the opposite history 5375 // stack (redo when undoing, and vice versa). 5376 var antiChanges = []; 5377 pushSelectionToHistory(selAfter, dest); 5378 dest.push({changes: antiChanges, generation: hist.generation}); 5379 hist.generation = event.generation || ++hist.maxGeneration; 5380 5381 var filter = hasHandler(doc, "beforeChange") || doc.cm && hasHandler(doc.cm, "beforeChange"); 5382 5383 var loop = function ( i ) { 5384 var change = event.changes[i]; 5385 change.origin = type; 5386 if (filter && !filterChange(doc, change, false)) { 5387 source.length = 0; 5388 return {} 5389 } 5390 5391 antiChanges.push(historyChangeFromChange(doc, change)); 5392 5393 var after = i ? computeSelAfterChange(doc, change) : lst(source); 5394 makeChangeSingleDoc(doc, change, after, mergeOldSpans(doc, change)); 5395 if (!i && doc.cm) { doc.cm.scrollIntoView({from: change.from, to: changeEnd(change)}); } 5396 var rebased = []; 5397 5398 // Propagate to the linked documents 5399 linkedDocs(doc, function (doc, sharedHist) { 5400 if (!sharedHist && indexOf(rebased, doc.history) == -1) { 5401 rebaseHist(doc.history, change); 5402 rebased.push(doc.history); 5403 } 5404 makeChangeSingleDoc(doc, change, null, mergeOldSpans(doc, change)); 5405 }); 5406 }; 5407 5408 for (var i$1 = event.changes.length - 1; i$1 >= 0; --i$1) { 5409 var returned = loop( i$1 ); 5410 5411 if ( returned ) return returned.v; 5412 } 5413 } 5414 5415 // Sub-views need their line numbers shifted when text is added 5416 // above or below them in the parent document. 5417 function shiftDoc(doc, distance) { 5418 if (distance == 0) { return } 5419 doc.first += distance; 5420 doc.sel = new Selection(map(doc.sel.ranges, function (range) { return new Range( 5421 Pos(range.anchor.line + distance, range.anchor.ch), 5422 Pos(range.head.line + distance, range.head.ch) 5423 ); }), doc.sel.primIndex); 5424 if (doc.cm) { 5425 regChange(doc.cm, doc.first, doc.first - distance, distance); 5426 for (var d = doc.cm.display, l = d.viewFrom; l < d.viewTo; l++) 5427 { regLineChange(doc.cm, l, "gutter"); } 5428 } 5429 } 5430 5431 // More lower-level change function, handling only a single document 5432 // (not linked ones). 5433 function makeChangeSingleDoc(doc, change, selAfter, spans) { 5434 if (doc.cm && !doc.cm.curOp) 5435 { return operation(doc.cm, makeChangeSingleDoc)(doc, change, selAfter, spans) } 5436 5437 if (change.to.line < doc.first) { 5438 shiftDoc(doc, change.text.length - 1 - (change.to.line - change.from.line)); 5439 return 5440 } 5441 if (change.from.line > doc.lastLine()) { return } 5442 5443 // Clip the change to the size of this doc 5444 if (change.from.line < doc.first) { 5445 var shift = change.text.length - 1 - (doc.first - change.from.line); 5446 shiftDoc(doc, shift); 5447 change = {from: Pos(doc.first, 0), to: Pos(change.to.line + shift, change.to.ch), 5448 text: [lst(change.text)], origin: change.origin}; 5449 } 5450 var last = doc.lastLine(); 5451 if (change.to.line > last) { 5452 change = {from: change.from, to: Pos(last, getLine(doc, last).text.length), 5453 text: [change.text[0]], origin: change.origin}; 5454 } 5455 5456 change.removed = getBetween(doc, change.from, change.to); 5457 5458 if (!selAfter) { selAfter = computeSelAfterChange(doc, change); } 5459 if (doc.cm) { makeChangeSingleDocInEditor(doc.cm, change, spans); } 5460 else { updateDoc(doc, change, spans); } 5461 setSelectionNoUndo(doc, selAfter, sel_dontScroll); 5462 5463 if (doc.cantEdit && skipAtomic(doc, Pos(doc.firstLine(), 0))) 5464 { doc.cantEdit = false; } 5465 } 5466 5467 // Handle the interaction of a change to a document with the editor 5468 // that this document is part of. 5469 function makeChangeSingleDocInEditor(cm, change, spans) { 5470 var doc = cm.doc, display = cm.display, from = change.from, to = change.to; 5471 5472 var recomputeMaxLength = false, checkWidthStart = from.line; 5473 if (!cm.options.lineWrapping) { 5474 checkWidthStart = lineNo(visualLine(getLine(doc, from.line))); 5475 doc.iter(checkWidthStart, to.line + 1, function (line) { 5476 if (line == display.maxLine) { 5477 recomputeMaxLength = true; 5478 return true 5479 } 5480 }); 5481 } 5482 5483 if (doc.sel.contains(change.from, change.to) > -1) 5484 { signalCursorActivity(cm); } 5485 5486 updateDoc(doc, change, spans, estimateHeight(cm)); 5487 5488 if (!cm.options.lineWrapping) { 5489 doc.iter(checkWidthStart, from.line + change.text.length, function (line) { 5490 var len = lineLength(line); 5491 if (len > display.maxLineLength) { 5492 display.maxLine = line; 5493 display.maxLineLength = len; 5494 display.maxLineChanged = true; 5495 recomputeMaxLength = false; 5496 } 5497 }); 5498 if (recomputeMaxLength) { cm.curOp.updateMaxLine = true; } 5499 } 5500 5501 retreatFrontier(doc, from.line); 5502 startWorker(cm, 400); 5503 5504 var lendiff = change.text.length - (to.line - from.line) - 1; 5505 // Remember that these lines changed, for updating the display 5506 if (change.full) 5507 { regChange(cm); } 5508 else if (from.line == to.line && change.text.length == 1 && !isWholeLineUpdate(cm.doc, change)) 5509 { regLineChange(cm, from.line, "text"); } 5510 else 5511 { regChange(cm, from.line, to.line + 1, lendiff); } 5512 5513 var changesHandler = hasHandler(cm, "changes"), changeHandler = hasHandler(cm, "change"); 5514 if (changeHandler || changesHandler) { 5515 var obj = { 5516 from: from, to: to, 5517 text: change.text, 5518 removed: change.removed, 5519 origin: change.origin 5520 }; 5521 if (changeHandler) { signalLater(cm, "change", cm, obj); } 5522 if (changesHandler) { (cm.curOp.changeObjs || (cm.curOp.changeObjs = [])).push(obj); } 5523 } 5524 cm.display.selForContextMenu = null; 5525 } 5526 5527 function replaceRange(doc, code, from, to, origin) { 5528 var assign; 5529 5530 if (!to) { to = from; } 5531 if (cmp(to, from) < 0) { (assign = [to, from], from = assign[0], to = assign[1]); } 5532 if (typeof code == "string") { code = doc.splitLines(code); } 5533 makeChange(doc, {from: from, to: to, text: code, origin: origin}); 5534 } 5535 5536 // Rebasing/resetting history to deal with externally-sourced changes 5537 5538 function rebaseHistSelSingle(pos, from, to, diff) { 5539 if (to < pos.line) { 5540 pos.line += diff; 5541 } else if (from < pos.line) { 5542 pos.line = from; 5543 pos.ch = 0; 5544 } 5545 } 5546 5547 // Tries to rebase an array of history events given a change in the 5548 // document. If the change touches the same lines as the event, the 5549 // event, and everything 'behind' it, is discarded. If the change is 5550 // before the event, the event's positions are updated. Uses a 5551 // copy-on-write scheme for the positions, to avoid having to 5552 // reallocate them all on every rebase, but also avoid problems with 5553 // shared position objects being unsafely updated. 5554 function rebaseHistArray(array, from, to, diff) { 5555 for (var i = 0; i < array.length; ++i) { 5556 var sub = array[i], ok = true; 5557 if (sub.ranges) { 5558 if (!sub.copied) { sub = array[i] = sub.deepCopy(); sub.copied = true; } 5559 for (var j = 0; j < sub.ranges.length; j++) { 5560 rebaseHistSelSingle(sub.ranges[j].anchor, from, to, diff); 5561 rebaseHistSelSingle(sub.ranges[j].head, from, to, diff); 5562 } 5563 continue 5564 } 5565 for (var j$1 = 0; j$1 < sub.changes.length; ++j$1) { 5566 var cur = sub.changes[j$1]; 5567 if (to < cur.from.line) { 5568 cur.from = Pos(cur.from.line + diff, cur.from.ch); 5569 cur.to = Pos(cur.to.line + diff, cur.to.ch); 5570 } else if (from <= cur.to.line) { 5571 ok = false; 5572 break 5573 } 5574 } 5575 if (!ok) { 5576 array.splice(0, i + 1); 5577 i = 0; 5578 } 5579 } 5580 } 5581 5582 function rebaseHist(hist, change) { 5583 var from = change.from.line, to = change.to.line, diff = change.text.length - (to - from) - 1; 5584 rebaseHistArray(hist.done, from, to, diff); 5585 rebaseHistArray(hist.undone, from, to, diff); 5586 } 5587 5588 // Utility for applying a change to a line by handle or number, 5589 // returning the number and optionally registering the line as 5590 // changed. 5591 function changeLine(doc, handle, changeType, op) { 5592 var no = handle, line = handle; 5593 if (typeof handle == "number") { line = getLine(doc, clipLine(doc, handle)); } 5594 else { no = lineNo(handle); } 5595 if (no == null) { return null } 5596 if (op(line, no) && doc.cm) { regLineChange(doc.cm, no, changeType); } 5597 return line 5598 } 5599 5600 // The document is represented as a BTree consisting of leaves, with 5601 // chunk of lines in them, and branches, with up to ten leaves or 5602 // other branch nodes below them. The top node is always a branch 5603 // node, and is the document object itself (meaning it has 5604 // additional methods and properties). 5605 // 5606 // All nodes have parent links. The tree is used both to go from 5607 // line numbers to line objects, and to go from objects to numbers. 5608 // It also indexes by height, and is used to convert between height 5609 // and line object, and to find the total height of the document. 5610 // 5611 // See also http://marijnhaverbeke.nl/blog/codemirror-line-tree.html 5612 5613 function LeafChunk(lines) { 5614 this.lines = lines; 5615 this.parent = null; 5616 var height = 0; 5617 for (var i = 0; i < lines.length; ++i) { 5618 lines[i].parent = this; 5619 height += lines[i].height; 5620 } 5621 this.height = height; 5622 } 5623 5624 LeafChunk.prototype = { 5625 chunkSize: function() { return this.lines.length }, 5626 5627 // Remove the n lines at offset 'at'. 5628 removeInner: function(at, n) { 5629 for (var i = at, e = at + n; i < e; ++i) { 5630 var line = this.lines[i]; 5631 this.height -= line.height; 5632 cleanUpLine(line); 5633 signalLater(line, "delete"); 5634 } 5635 this.lines.splice(at, n); 5636 }, 5637 5638 // Helper used to collapse a small branch into a single leaf. 5639 collapse: function(lines) { 5640 lines.push.apply(lines, this.lines); 5641 }, 5642 5643 // Insert the given array of lines at offset 'at', count them as 5644 // having the given height. 5645 insertInner: function(at, lines, height) { 5646 this.height += height; 5647 this.lines = this.lines.slice(0, at).concat(lines).concat(this.lines.slice(at)); 5648 for (var i = 0; i < lines.length; ++i) { lines[i].parent = this; } 5649 }, 5650 5651 // Used to iterate over a part of the tree. 5652 iterN: function(at, n, op) { 5653 for (var e = at + n; at < e; ++at) 5654 { if (op(this.lines[at])) { return true } } 5655 } 5656 }; 5657 5658 function BranchChunk(children) { 5659 this.children = children; 5660 var size = 0, height = 0; 5661 for (var i = 0; i < children.length; ++i) { 5662 var ch = children[i]; 5663 size += ch.chunkSize(); height += ch.height; 5664 ch.parent = this; 5665 } 5666 this.size = size; 5667 this.height = height; 5668 this.parent = null; 5669 } 5670 5671 BranchChunk.prototype = { 5672 chunkSize: function() { return this.size }, 5673 5674 removeInner: function(at, n) { 5675 this.size -= n; 5676 for (var i = 0; i < this.children.length; ++i) { 5677 var child = this.children[i], sz = child.chunkSize(); 5678 if (at < sz) { 5679 var rm = Math.min(n, sz - at), oldHeight = child.height; 5680 child.removeInner(at, rm); 5681 this.height -= oldHeight - child.height; 5682 if (sz == rm) { this.children.splice(i--, 1); child.parent = null; } 5683 if ((n -= rm) == 0) { break } 5684 at = 0; 5685 } else { at -= sz; } 5686 } 5687 // If the result is smaller than 25 lines, ensure that it is a 5688 // single leaf node. 5689 if (this.size - n < 25 && 5690 (this.children.length > 1 || !(this.children[0] instanceof LeafChunk))) { 5691 var lines = []; 5692 this.collapse(lines); 5693 this.children = [new LeafChunk(lines)]; 5694 this.children[0].parent = this; 5695 } 5696 }, 5697 5698 collapse: function(lines) { 5699 for (var i = 0; i < this.children.length; ++i) { this.children[i].collapse(lines); } 5700 }, 5701 5702 insertInner: function(at, lines, height) { 5703 this.size += lines.length; 5704 this.height += height; 5705 for (var i = 0; i < this.children.length; ++i) { 5706 var child = this.children[i], sz = child.chunkSize(); 5707 if (at <= sz) { 5708 child.insertInner(at, lines, height); 5709 if (child.lines && child.lines.length > 50) { 5710 // To avoid memory thrashing when child.lines is huge (e.g. first view of a large file), it's never spliced. 5711 // Instead, small slices are taken. They're taken in order because sequential memory accesses are fastest. 5712 var remaining = child.lines.length % 25 + 25; 5713 for (var pos = remaining; pos < child.lines.length;) { 5714 var leaf = new LeafChunk(child.lines.slice(pos, pos += 25)); 5715 child.height -= leaf.height; 5716 this.children.splice(++i, 0, leaf); 5717 leaf.parent = this; 5718 } 5719 child.lines = child.lines.slice(0, remaining); 5720 this.maybeSpill(); 5721 } 5722 break 5723 } 5724 at -= sz; 5725 } 5726 }, 5727 5728 // When a node has grown, check whether it should be split. 5729 maybeSpill: function() { 5730 if (this.children.length <= 10) { return } 5731 var me = this; 5732 do { 5733 var spilled = me.children.splice(me.children.length - 5, 5); 5734 var sibling = new BranchChunk(spilled); 5735 if (!me.parent) { // Become the parent node 5736 var copy = new BranchChunk(me.children); 5737 copy.parent = me; 5738 me.children = [copy, sibling]; 5739 me = copy; 5740 } else { 5741 me.size -= sibling.size; 5742 me.height -= sibling.height; 5743 var myIndex = indexOf(me.parent.children, me); 5744 me.parent.children.splice(myIndex + 1, 0, sibling); 5745 } 5746 sibling.parent = me.parent; 5747 } while (me.children.length > 10) 5748 me.parent.maybeSpill(); 5749 }, 5750 5751 iterN: function(at, n, op) { 5752 for (var i = 0; i < this.children.length; ++i) { 5753 var child = this.children[i], sz = child.chunkSize(); 5754 if (at < sz) { 5755 var used = Math.min(n, sz - at); 5756 if (child.iterN(at, used, op)) { return true } 5757 if ((n -= used) == 0) { break } 5758 at = 0; 5759 } else { at -= sz; } 5760 } 5761 } 5762 }; 5763 5764 // Line widgets are block elements displayed above or below a line. 5765 5766 var LineWidget = function(doc, node, options) { 5767 if (options) { for (var opt in options) { if (options.hasOwnProperty(opt)) 5768 { this[opt] = options[opt]; } } } 5769 this.doc = doc; 5770 this.node = node; 5771 }; 5772 5773 LineWidget.prototype.clear = function () { 5774 var cm = this.doc.cm, ws = this.line.widgets, line = this.line, no = lineNo(line); 5775 if (no == null || !ws) { return } 5776 for (var i = 0; i < ws.length; ++i) { if (ws[i] == this) { ws.splice(i--, 1); } } 5777 if (!ws.length) { line.widgets = null; } 5778 var height = widgetHeight(this); 5779 updateLineHeight(line, Math.max(0, line.height - height)); 5780 if (cm) { 5781 runInOp(cm, function () { 5782 adjustScrollWhenAboveVisible(cm, line, -height); 5783 regLineChange(cm, no, "widget"); 5784 }); 5785 signalLater(cm, "lineWidgetCleared", cm, this, no); 5786 } 5787 }; 5788 5789 LineWidget.prototype.changed = function () { 5790 var this$1 = this; 5791 5792 var oldH = this.height, cm = this.doc.cm, line = this.line; 5793 this.height = null; 5794 var diff = widgetHeight(this) - oldH; 5795 if (!diff) { return } 5796 if (!lineIsHidden(this.doc, line)) { updateLineHeight(line, line.height + diff); } 5797 if (cm) { 5798 runInOp(cm, function () { 5799 cm.curOp.forceUpdate = true; 5800 adjustScrollWhenAboveVisible(cm, line, diff); 5801 signalLater(cm, "lineWidgetChanged", cm, this$1, lineNo(line)); 5802 }); 5803 } 5804 }; 5805 eventMixin(LineWidget); 5806 5807 function adjustScrollWhenAboveVisible(cm, line, diff) { 5808 if (heightAtLine(line) < ((cm.curOp && cm.curOp.scrollTop) || cm.doc.scrollTop)) 5809 { addToScrollTop(cm, diff); } 5810 } 5811 5812 function addLineWidget(doc, handle, node, options) { 5813 var widget = new LineWidget(doc, node, options); 5814 var cm = doc.cm; 5815 if (cm && widget.noHScroll) { cm.display.alignWidgets = true; } 5816 changeLine(doc, handle, "widget", function (line) { 5817 var widgets = line.widgets || (line.widgets = []); 5818 if (widget.insertAt == null) { widgets.push(widget); } 5819 else { widgets.splice(Math.min(widgets.length, Math.max(0, widget.insertAt)), 0, widget); } 5820 widget.line = line; 5821 if (cm && !lineIsHidden(doc, line)) { 5822 var aboveVisible = heightAtLine(line) < doc.scrollTop; 5823 updateLineHeight(line, line.height + widgetHeight(widget)); 5824 if (aboveVisible) { addToScrollTop(cm, widget.height); } 5825 cm.curOp.forceUpdate = true; 5826 } 5827 return true 5828 }); 5829 if (cm) { signalLater(cm, "lineWidgetAdded", cm, widget, typeof handle == "number" ? handle : lineNo(handle)); } 5830 return widget 5831 } 5832 5833 // TEXTMARKERS 5834 5835 // Created with markText and setBookmark methods. A TextMarker is a 5836 // handle that can be used to clear or find a marked position in the 5837 // document. Line objects hold arrays (markedSpans) containing 5838 // {from, to, marker} object pointing to such marker objects, and 5839 // indicating that such a marker is present on that line. Multiple 5840 // lines may point to the same marker when it spans across lines. 5841 // The spans will have null for their from/to properties when the 5842 // marker continues beyond the start/end of the line. Markers have 5843 // links back to the lines they currently touch. 5844 5845 // Collapsed markers have unique ids, in order to be able to order 5846 // them, which is needed for uniquely determining an outer marker 5847 // when they overlap (they may nest, but not partially overlap). 5848 var nextMarkerId = 0; 5849 5850 var TextMarker = function(doc, type) { 5851 this.lines = []; 5852 this.type = type; 5853 this.doc = doc; 5854 this.id = ++nextMarkerId; 5855 }; 5856 5857 // Clear the marker. 5858 TextMarker.prototype.clear = function () { 5859 if (this.explicitlyCleared) { return } 5860 var cm = this.doc.cm, withOp = cm && !cm.curOp; 5861 if (withOp) { startOperation(cm); } 5862 if (hasHandler(this, "clear")) { 5863 var found = this.find(); 5864 if (found) { signalLater(this, "clear", found.from, found.to); } 5865 } 5866 var min = null, max = null; 5867 for (var i = 0; i < this.lines.length; ++i) { 5868 var line = this.lines[i]; 5869 var span = getMarkedSpanFor(line.markedSpans, this); 5870 if (cm && !this.collapsed) { regLineChange(cm, lineNo(line), "text"); } 5871 else if (cm) { 5872 if (span.to != null) { max = lineNo(line); } 5873 if (span.from != null) { min = lineNo(line); } 5874 } 5875 line.markedSpans = removeMarkedSpan(line.markedSpans, span); 5876 if (span.from == null && this.collapsed && !lineIsHidden(this.doc, line) && cm) 5877 { updateLineHeight(line, textHeight(cm.display)); } 5878 } 5879 if (cm && this.collapsed && !cm.options.lineWrapping) { for (var i$1 = 0; i$1 < this.lines.length; ++i$1) { 5880 var visual = visualLine(this.lines[i$1]), len = lineLength(visual); 5881 if (len > cm.display.maxLineLength) { 5882 cm.display.maxLine = visual; 5883 cm.display.maxLineLength = len; 5884 cm.display.maxLineChanged = true; 5885 } 5886 } } 5887 5888 if (min != null && cm && this.collapsed) { regChange(cm, min, max + 1); } 5889 this.lines.length = 0; 5890 this.explicitlyCleared = true; 5891 if (this.atomic && this.doc.cantEdit) { 5892 this.doc.cantEdit = false; 5893 if (cm) { reCheckSelection(cm.doc); } 5894 } 5895 if (cm) { signalLater(cm, "markerCleared", cm, this, min, max); } 5896 if (withOp) { endOperation(cm); } 5897 if (this.parent) { this.parent.clear(); } 5898 }; 5899 5900 // Find the position of the marker in the document. Returns a {from, 5901 // to} object by default. Side can be passed to get a specific side 5902 // -- 0 (both), -1 (left), or 1 (right). When lineObj is true, the 5903 // Pos objects returned contain a line object, rather than a line 5904 // number (used to prevent looking up the same line twice). 5905 TextMarker.prototype.find = function (side, lineObj) { 5906 if (side == null && this.type == "bookmark") { side = 1; } 5907 var from, to; 5908 for (var i = 0; i < this.lines.length; ++i) { 5909 var line = this.lines[i]; 5910 var span = getMarkedSpanFor(line.markedSpans, this); 5911 if (span.from != null) { 5912 from = Pos(lineObj ? line : lineNo(line), span.from); 5913 if (side == -1) { return from } 5914 } 5915 if (span.to != null) { 5916 to = Pos(lineObj ? line : lineNo(line), span.to); 5917 if (side == 1) { return to } 5918 } 5919 } 5920 return from && {from: from, to: to} 5921 }; 5922 5923 // Signals that the marker's widget changed, and surrounding layout 5924 // should be recomputed. 5925 TextMarker.prototype.changed = function () { 5926 var this$1 = this; 5927 5928 var pos = this.find(-1, true), widget = this, cm = this.doc.cm; 5929 if (!pos || !cm) { return } 5930 runInOp(cm, function () { 5931 var line = pos.line, lineN = lineNo(pos.line); 5932 var view = findViewForLine(cm, lineN); 5933 if (view) { 5934 clearLineMeasurementCacheFor(view); 5935 cm.curOp.selectionChanged = cm.curOp.forceUpdate = true; 5936 } 5937 cm.curOp.updateMaxLine = true; 5938 if (!lineIsHidden(widget.doc, line) && widget.height != null) { 5939 var oldHeight = widget.height; 5940 widget.height = null; 5941 var dHeight = widgetHeight(widget) - oldHeight; 5942 if (dHeight) 5943 { updateLineHeight(line, line.height + dHeight); } 5944 } 5945 signalLater(cm, "markerChanged", cm, this$1); 5946 }); 5947 }; 5948 5949 TextMarker.prototype.attachLine = function (line) { 5950 if (!this.lines.length && this.doc.cm) { 5951 var op = this.doc.cm.curOp; 5952 if (!op.maybeHiddenMarkers || indexOf(op.maybeHiddenMarkers, this) == -1) 5953 { (op.maybeUnhiddenMarkers || (op.maybeUnhiddenMarkers = [])).push(this); } 5954 } 5955 this.lines.push(line); 5956 }; 5957 5958 TextMarker.prototype.detachLine = function (line) { 5959 this.lines.splice(indexOf(this.lines, line), 1); 5960 if (!this.lines.length && this.doc.cm) { 5961 var op = this.doc.cm.curOp 5962 ;(op.maybeHiddenMarkers || (op.maybeHiddenMarkers = [])).push(this); 5963 } 5964 }; 5965 eventMixin(TextMarker); 5966 5967 // Create a marker, wire it up to the right lines, and 5968 function markText(doc, from, to, options, type) { 5969 // Shared markers (across linked documents) are handled separately 5970 // (markTextShared will call out to this again, once per 5971 // document). 5972 if (options && options.shared) { return markTextShared(doc, from, to, options, type) } 5973 // Ensure we are in an operation. 5974 if (doc.cm && !doc.cm.curOp) { return operation(doc.cm, markText)(doc, from, to, options, type) } 5975 5976 var marker = new TextMarker(doc, type), diff = cmp(from, to); 5977 if (options) { copyObj(options, marker, false); } 5978 // Don't connect empty markers unless clearWhenEmpty is false 5979 if (diff > 0 || diff == 0 && marker.clearWhenEmpty !== false) 5980 { return marker } 5981 if (marker.replacedWith) { 5982 // Showing up as a widget implies collapsed (widget replaces text) 5983 marker.collapsed = true; 5984 marker.widgetNode = eltP("span", [marker.replacedWith], "CodeMirror-widget"); 5985 if (!options.handleMouseEvents) { marker.widgetNode.setAttribute("cm-ignore-events", "true"); } 5986 if (options.insertLeft) { marker.widgetNode.insertLeft = true; } 5987 } 5988 if (marker.collapsed) { 5989 if (conflictingCollapsedRange(doc, from.line, from, to, marker) || 5990 from.line != to.line && conflictingCollapsedRange(doc, to.line, from, to, marker)) 5991 { throw new Error("Inserting collapsed marker partially overlapping an existing one") } 5992 seeCollapsedSpans(); 5993 } 5994 5995 if (marker.addToHistory) 5996 { addChangeToHistory(doc, {from: from, to: to, origin: "markText"}, doc.sel, NaN); } 5997 5998 var curLine = from.line, cm = doc.cm, updateMaxLine; 5999 doc.iter(curLine, to.line + 1, function (line) { 6000 if (cm && marker.collapsed && !cm.options.lineWrapping && visualLine(line) == cm.display.maxLine) 6001 { updateMaxLine = true; } 6002 if (marker.collapsed && curLine != from.line) { updateLineHeight(line, 0); } 6003 addMarkedSpan(line, new MarkedSpan(marker, 6004 curLine == from.line ? from.ch : null, 6005 curLine == to.line ? to.ch : null), doc.cm && doc.cm.curOp); 6006 ++curLine; 6007 }); 6008 // lineIsHidden depends on the presence of the spans, so needs a second pass 6009 if (marker.collapsed) { doc.iter(from.line, to.line + 1, function (line) { 6010 if (lineIsHidden(doc, line)) { updateLineHeight(line, 0); } 6011 }); } 6012 6013 if (marker.clearOnEnter) { on(marker, "beforeCursorEnter", function () { return marker.clear(); }); } 6014 6015 if (marker.readOnly) { 6016 seeReadOnlySpans(); 6017 if (doc.history.done.length || doc.history.undone.length) 6018 { doc.clearHistory(); } 6019 } 6020 if (marker.collapsed) { 6021 marker.id = ++nextMarkerId; 6022 marker.atomic = true; 6023 } 6024 if (cm) { 6025 // Sync editor state 6026 if (updateMaxLine) { cm.curOp.updateMaxLine = true; } 6027 if (marker.collapsed) 6028 { regChange(cm, from.line, to.line + 1); } 6029 else if (marker.className || marker.startStyle || marker.endStyle || marker.css || 6030 marker.attributes || marker.title) 6031 { for (var i = from.line; i <= to.line; i++) { regLineChange(cm, i, "text"); } } 6032 if (marker.atomic) { reCheckSelection(cm.doc); } 6033 signalLater(cm, "markerAdded", cm, marker); 6034 } 6035 return marker 6036 } 6037 6038 // SHARED TEXTMARKERS 6039 6040 // A shared marker spans multiple linked documents. It is 6041 // implemented as a meta-marker-object controlling multiple normal 6042 // markers. 6043 var SharedTextMarker = function(markers, primary) { 6044 this.markers = markers; 6045 this.primary = primary; 6046 for (var i = 0; i < markers.length; ++i) 6047 { markers[i].parent = this; } 6048 }; 6049 6050 SharedTextMarker.prototype.clear = function () { 6051 if (this.explicitlyCleared) { return } 6052 this.explicitlyCleared = true; 6053 for (var i = 0; i < this.markers.length; ++i) 6054 { this.markers[i].clear(); } 6055 signalLater(this, "clear"); 6056 }; 6057 6058 SharedTextMarker.prototype.find = function (side, lineObj) { 6059 return this.primary.find(side, lineObj) 6060 }; 6061 eventMixin(SharedTextMarker); 6062 6063 function markTextShared(doc, from, to, options, type) { 6064 options = copyObj(options); 6065 options.shared = false; 6066 var markers = [markText(doc, from, to, options, type)], primary = markers[0]; 6067 var widget = options.widgetNode; 6068 linkedDocs(doc, function (doc) { 6069 if (widget) { options.widgetNode = widget.cloneNode(true); } 6070 markers.push(markText(doc, clipPos(doc, from), clipPos(doc, to), options, type)); 6071 for (var i = 0; i < doc.linked.length; ++i) 6072 { if (doc.linked[i].isParent) { return } } 6073 primary = lst(markers); 6074 }); 6075 return new SharedTextMarker(markers, primary) 6076 } 6077 6078 function findSharedMarkers(doc) { 6079 return doc.findMarks(Pos(doc.first, 0), doc.clipPos(Pos(doc.lastLine())), function (m) { return m.parent; }) 6080 } 6081 6082 function copySharedMarkers(doc, markers) { 6083 for (var i = 0; i < markers.length; i++) { 6084 var marker = markers[i], pos = marker.find(); 6085 var mFrom = doc.clipPos(pos.from), mTo = doc.clipPos(pos.to); 6086 if (cmp(mFrom, mTo)) { 6087 var subMark = markText(doc, mFrom, mTo, marker.primary, marker.primary.type); 6088 marker.markers.push(subMark); 6089 subMark.parent = marker; 6090 } 6091 } 6092 } 6093 6094 function detachSharedMarkers(markers) { 6095 var loop = function ( i ) { 6096 var marker = markers[i], linked = [marker.primary.doc]; 6097 linkedDocs(marker.primary.doc, function (d) { return linked.push(d); }); 6098 for (var j = 0; j < marker.markers.length; j++) { 6099 var subMarker = marker.markers[j]; 6100 if (indexOf(linked, subMarker.doc) == -1) { 6101 subMarker.parent = null; 6102 marker.markers.splice(j--, 1); 6103 } 6104 } 6105 }; 6106 6107 for (var i = 0; i < markers.length; i++) loop( i ); 6108 } 6109 6110 var nextDocId = 0; 6111 var Doc = function(text, mode, firstLine, lineSep, direction) { 6112 if (!(this instanceof Doc)) { return new Doc(text, mode, firstLine, lineSep, direction) } 6113 if (firstLine == null) { firstLine = 0; } 6114 6115 BranchChunk.call(this, [new LeafChunk([new Line("", null)])]); 6116 this.first = firstLine; 6117 this.scrollTop = this.scrollLeft = 0; 6118 this.cantEdit = false; 6119 this.cleanGeneration = 1; 6120 this.modeFrontier = this.highlightFrontier = firstLine; 6121 var start = Pos(firstLine, 0); 6122 this.sel = simpleSelection(start); 6123 this.history = new History(null); 6124 this.id = ++nextDocId; 6125 this.modeOption = mode; 6126 this.lineSep = lineSep; 6127 this.direction = (direction == "rtl") ? "rtl" : "ltr"; 6128 this.extend = false; 6129 6130 if (typeof text == "string") { text = this.splitLines(text); } 6131 updateDoc(this, {from: start, to: start, text: text}); 6132 setSelection(this, simpleSelection(start), sel_dontScroll); 6133 }; 6134 6135 Doc.prototype = createObj(BranchChunk.prototype, { 6136 constructor: Doc, 6137 // Iterate over the document. Supports two forms -- with only one 6138 // argument, it calls that for each line in the document. With 6139 // three, it iterates over the range given by the first two (with 6140 // the second being non-inclusive). 6141 iter: function(from, to, op) { 6142 if (op) { this.iterN(from - this.first, to - from, op); } 6143 else { this.iterN(this.first, this.first + this.size, from); } 6144 }, 6145 6146 // Non-public interface for adding and removing lines. 6147 insert: function(at, lines) { 6148 var height = 0; 6149 for (var i = 0; i < lines.length; ++i) { height += lines[i].height; } 6150 this.insertInner(at - this.first, lines, height); 6151 }, 6152 remove: function(at, n) { this.removeInner(at - this.first, n); }, 6153 6154 // From here, the methods are part of the public interface. Most 6155 // are also available from CodeMirror (editor) instances. 6156 6157 getValue: function(lineSep) { 6158 var lines = getLines(this, this.first, this.first + this.size); 6159 if (lineSep === false) { return lines } 6160 return lines.join(lineSep || this.lineSeparator()) 6161 }, 6162 setValue: docMethodOp(function(code) { 6163 var top = Pos(this.first, 0), last = this.first + this.size - 1; 6164 makeChange(this, {from: top, to: Pos(last, getLine(this, last).text.length), 6165 text: this.splitLines(code), origin: "setValue", full: true}, true); 6166 if (this.cm) { scrollToCoords(this.cm, 0, 0); } 6167 setSelection(this, simpleSelection(top), sel_dontScroll); 6168 }), 6169 replaceRange: function(code, from, to, origin) { 6170 from = clipPos(this, from); 6171 to = to ? clipPos(this, to) : from; 6172 replaceRange(this, code, from, to, origin); 6173 }, 6174 getRange: function(from, to, lineSep) { 6175 var lines = getBetween(this, clipPos(this, from), clipPos(this, to)); 6176 if (lineSep === false) { return lines } 6177 if (lineSep === '') { return lines.join('') } 6178 return lines.join(lineSep || this.lineSeparator()) 6179 }, 6180 6181 getLine: function(line) {var l = this.getLineHandle(line); return l && l.text}, 6182 6183 getLineHandle: function(line) {if (isLine(this, line)) { return getLine(this, line) }}, 6184 getLineNumber: function(line) {return lineNo(line)}, 6185 6186 getLineHandleVisualStart: function(line) { 6187 if (typeof line == "number") { line = getLine(this, line); } 6188 return visualLine(line) 6189 }, 6190 6191 lineCount: function() {return this.size}, 6192 firstLine: function() {return this.first}, 6193 lastLine: function() {return this.first + this.size - 1}, 6194 6195 clipPos: function(pos) {return clipPos(this, pos)}, 6196 6197 getCursor: function(start) { 6198 var range = this.sel.primary(), pos; 6199 if (start == null || start == "head") { pos = range.head; } 6200 else if (start == "anchor") { pos = range.anchor; } 6201 else if (start == "end" || start == "to" || start === false) { pos = range.to(); } 6202 else { pos = range.from(); } 6203 return pos 6204 }, 6205 listSelections: function() { return this.sel.ranges }, 6206 somethingSelected: function() {return this.sel.somethingSelected()}, 6207 6208 setCursor: docMethodOp(function(line, ch, options) { 6209 setSimpleSelection(this, clipPos(this, typeof line == "number" ? Pos(line, ch || 0) : line), null, options); 6210 }), 6211 setSelection: docMethodOp(function(anchor, head, options) { 6212 setSimpleSelection(this, clipPos(this, anchor), clipPos(this, head || anchor), options); 6213 }), 6214 extendSelection: docMethodOp(function(head, other, options) { 6215 extendSelection(this, clipPos(this, head), other && clipPos(this, other), options); 6216 }), 6217 extendSelections: docMethodOp(function(heads, options) { 6218 extendSelections(this, clipPosArray(this, heads), options); 6219 }), 6220 extendSelectionsBy: docMethodOp(function(f, options) { 6221 var heads = map(this.sel.ranges, f); 6222 extendSelections(this, clipPosArray(this, heads), options); 6223 }), 6224 setSelections: docMethodOp(function(ranges, primary, options) { 6225 if (!ranges.length) { return } 6226 var out = []; 6227 for (var i = 0; i < ranges.length; i++) 6228 { out[i] = new Range(clipPos(this, ranges[i].anchor), 6229 clipPos(this, ranges[i].head || ranges[i].anchor)); } 6230 if (primary == null) { primary = Math.min(ranges.length - 1, this.sel.primIndex); } 6231 setSelection(this, normalizeSelection(this.cm, out, primary), options); 6232 }), 6233 addSelection: docMethodOp(function(anchor, head, options) { 6234 var ranges = this.sel.ranges.slice(0); 6235 ranges.push(new Range(clipPos(this, anchor), clipPos(this, head || anchor))); 6236 setSelection(this, normalizeSelection(this.cm, ranges, ranges.length - 1), options); 6237 }), 6238 6239 getSelection: function(lineSep) { 6240 var ranges = this.sel.ranges, lines; 6241 for (var i = 0; i < ranges.length; i++) { 6242 var sel = getBetween(this, ranges[i].from(), ranges[i].to()); 6243 lines = lines ? lines.concat(sel) : sel; 6244 } 6245 if (lineSep === false) { return lines } 6246 else { return lines.join(lineSep || this.lineSeparator()) } 6247 }, 6248 getSelections: function(lineSep) { 6249 var parts = [], ranges = this.sel.ranges; 6250 for (var i = 0; i < ranges.length; i++) { 6251 var sel = getBetween(this, ranges[i].from(), ranges[i].to()); 6252 if (lineSep !== false) { sel = sel.join(lineSep || this.lineSeparator()); } 6253 parts[i] = sel; 6254 } 6255 return parts 6256 }, 6257 replaceSelection: function(code, collapse, origin) { 6258 var dup = []; 6259 for (var i = 0; i < this.sel.ranges.length; i++) 6260 { dup[i] = code; } 6261 this.replaceSelections(dup, collapse, origin || "+input"); 6262 }, 6263 replaceSelections: docMethodOp(function(code, collapse, origin) { 6264 var changes = [], sel = this.sel; 6265 for (var i = 0; i < sel.ranges.length; i++) { 6266 var range = sel.ranges[i]; 6267 changes[i] = {from: range.from(), to: range.to(), text: this.splitLines(code[i]), origin: origin}; 6268 } 6269 var newSel = collapse && collapse != "end" && computeReplacedSel(this, changes, collapse); 6270 for (var i$1 = changes.length - 1; i$1 >= 0; i$1--) 6271 { makeChange(this, changes[i$1]); } 6272 if (newSel) { setSelectionReplaceHistory(this, newSel); } 6273 else if (this.cm) { ensureCursorVisible(this.cm); } 6274 }), 6275 undo: docMethodOp(function() {makeChangeFromHistory(this, "undo");}), 6276 redo: docMethodOp(function() {makeChangeFromHistory(this, "redo");}), 6277 undoSelection: docMethodOp(function() {makeChangeFromHistory(this, "undo", true);}), 6278 redoSelection: docMethodOp(function() {makeChangeFromHistory(this, "redo", true);}), 6279 6280 setExtending: function(val) {this.extend = val;}, 6281 getExtending: function() {return this.extend}, 6282 6283 historySize: function() { 6284 var hist = this.history, done = 0, undone = 0; 6285 for (var i = 0; i < hist.done.length; i++) { if (!hist.done[i].ranges) { ++done; } } 6286 for (var i$1 = 0; i$1 < hist.undone.length; i$1++) { if (!hist.undone[i$1].ranges) { ++undone; } } 6287 return {undo: done, redo: undone} 6288 }, 6289 clearHistory: function() { 6290 var this$1 = this; 6291 6292 this.history = new History(this.history); 6293 linkedDocs(this, function (doc) { return doc.history = this$1.history; }, true); 6294 }, 6295 6296 markClean: function() { 6297 this.cleanGeneration = this.changeGeneration(true); 6298 }, 6299 changeGeneration: function(forceSplit) { 6300 if (forceSplit) 6301 { this.history.lastOp = this.history.lastSelOp = this.history.lastOrigin = null; } 6302 return this.history.generation 6303 }, 6304 isClean: function (gen) { 6305 return this.history.generation == (gen || this.cleanGeneration) 6306 }, 6307 6308 getHistory: function() { 6309 return {done: copyHistoryArray(this.history.done), 6310 undone: copyHistoryArray(this.history.undone)} 6311 }, 6312 setHistory: function(histData) { 6313 var hist = this.history = new History(this.history); 6314 hist.done = copyHistoryArray(histData.done.slice(0), null, true); 6315 hist.undone = copyHistoryArray(histData.undone.slice(0), null, true); 6316 }, 6317 6318 setGutterMarker: docMethodOp(function(line, gutterID, value) { 6319 return changeLine(this, line, "gutter", function (line) { 6320 var markers = line.gutterMarkers || (line.gutterMarkers = {}); 6321 markers[gutterID] = value; 6322 if (!value && isEmpty(markers)) { line.gutterMarkers = null; } 6323 return true 6324 }) 6325 }), 6326 6327 clearGutter: docMethodOp(function(gutterID) { 6328 var this$1 = this; 6329 6330 this.iter(function (line) { 6331 if (line.gutterMarkers && line.gutterMarkers[gutterID]) { 6332 changeLine(this$1, line, "gutter", function () { 6333 line.gutterMarkers[gutterID] = null; 6334 if (isEmpty(line.gutterMarkers)) { line.gutterMarkers = null; } 6335 return true 6336 }); 6337 } 6338 }); 6339 }), 6340 6341 lineInfo: function(line) { 6342 var n; 6343 if (typeof line == "number") { 6344 if (!isLine(this, line)) { return null } 6345 n = line; 6346 line = getLine(this, line); 6347 if (!line) { return null } 6348 } else { 6349 n = lineNo(line); 6350 if (n == null) { return null } 6351 } 6352 return {line: n, handle: line, text: line.text, gutterMarkers: line.gutterMarkers, 6353 textClass: line.textClass, bgClass: line.bgClass, wrapClass: line.wrapClass, 6354 widgets: line.widgets} 6355 }, 6356 6357 addLineClass: docMethodOp(function(handle, where, cls) { 6358 return changeLine(this, handle, where == "gutter" ? "gutter" : "class", function (line) { 6359 var prop = where == "text" ? "textClass" 6360 : where == "background" ? "bgClass" 6361 : where == "gutter" ? "gutterClass" : "wrapClass"; 6362 if (!line[prop]) { line[prop] = cls; } 6363 else if (classTest(cls).test(line[prop])) { return false } 6364 else { line[prop] += " " + cls; } 6365 return true 6366 }) 6367 }), 6368 removeLineClass: docMethodOp(function(handle, where, cls) { 6369 return changeLine(this, handle, where == "gutter" ? "gutter" : "class", function (line) { 6370 var prop = where == "text" ? "textClass" 6371 : where == "background" ? "bgClass" 6372 : where == "gutter" ? "gutterClass" : "wrapClass"; 6373 var cur = line[prop]; 6374 if (!cur) { return false } 6375 else if (cls == null) { line[prop] = null; } 6376 else { 6377 var found = cur.match(classTest(cls)); 6378 if (!found) { return false } 6379 var end = found.index + found[0].length; 6380 line[prop] = cur.slice(0, found.index) + (!found.index || end == cur.length ? "" : " ") + cur.slice(end) || null; 6381 } 6382 return true 6383 }) 6384 }), 6385 6386 addLineWidget: docMethodOp(function(handle, node, options) { 6387 return addLineWidget(this, handle, node, options) 6388 }), 6389 removeLineWidget: function(widget) { widget.clear(); }, 6390 6391 markText: function(from, to, options) { 6392 return markText(this, clipPos(this, from), clipPos(this, to), options, options && options.type || "range") 6393 }, 6394 setBookmark: function(pos, options) { 6395 var realOpts = {replacedWith: options && (options.nodeType == null ? options.widget : options), 6396 insertLeft: options && options.insertLeft, 6397 clearWhenEmpty: false, shared: options && options.shared, 6398 handleMouseEvents: options && options.handleMouseEvents}; 6399 pos = clipPos(this, pos); 6400 return markText(this, pos, pos, realOpts, "bookmark") 6401 }, 6402 findMarksAt: function(pos) { 6403 pos = clipPos(this, pos); 6404 var markers = [], spans = getLine(this, pos.line).markedSpans; 6405 if (spans) { for (var i = 0; i < spans.length; ++i) { 6406 var span = spans[i]; 6407 if ((span.from == null || span.from <= pos.ch) && 6408 (span.to == null || span.to >= pos.ch)) 6409 { markers.push(span.marker.parent || span.marker); } 6410 } } 6411 return markers 6412 }, 6413 findMarks: function(from, to, filter) { 6414 from = clipPos(this, from); to = clipPos(this, to); 6415 var found = [], lineNo = from.line; 6416 this.iter(from.line, to.line + 1, function (line) { 6417 var spans = line.markedSpans; 6418 if (spans) { for (var i = 0; i < spans.length; i++) { 6419 var span = spans[i]; 6420 if (!(span.to != null && lineNo == from.line && from.ch >= span.to || 6421 span.from == null && lineNo != from.line || 6422 span.from != null && lineNo == to.line && span.from >= to.ch) && 6423 (!filter || filter(span.marker))) 6424 { found.push(span.marker.parent || span.marker); } 6425 } } 6426 ++lineNo; 6427 }); 6428 return found 6429 }, 6430 getAllMarks: function() { 6431 var markers = []; 6432 this.iter(function (line) { 6433 var sps = line.markedSpans; 6434 if (sps) { for (var i = 0; i < sps.length; ++i) 6435 { if (sps[i].from != null) { markers.push(sps[i].marker); } } } 6436 }); 6437 return markers 6438 }, 6439 6440 posFromIndex: function(off) { 6441 var ch, lineNo = this.first, sepSize = this.lineSeparator().length; 6442 this.iter(function (line) { 6443 var sz = line.text.length + sepSize; 6444 if (sz > off) { ch = off; return true } 6445 off -= sz; 6446 ++lineNo; 6447 }); 6448 return clipPos(this, Pos(lineNo, ch)) 6449 }, 6450 indexFromPos: function (coords) { 6451 coords = clipPos(this, coords); 6452 var index = coords.ch; 6453 if (coords.line < this.first || coords.ch < 0) { return 0 } 6454 var sepSize = this.lineSeparator().length; 6455 this.iter(this.first, coords.line, function (line) { // iter aborts when callback returns a truthy value 6456 index += line.text.length + sepSize; 6457 }); 6458 return index 6459 }, 6460 6461 copy: function(copyHistory) { 6462 var doc = new Doc(getLines(this, this.first, this.first + this.size), 6463 this.modeOption, this.first, this.lineSep, this.direction); 6464 doc.scrollTop = this.scrollTop; doc.scrollLeft = this.scrollLeft; 6465 doc.sel = this.sel; 6466 doc.extend = false; 6467 if (copyHistory) { 6468 doc.history.undoDepth = this.history.undoDepth; 6469 doc.setHistory(this.getHistory()); 6470 } 6471 return doc 6472 }, 6473 6474 linkedDoc: function(options) { 6475 if (!options) { options = {}; } 6476 var from = this.first, to = this.first + this.size; 6477 if (options.from != null && options.from > from) { from = options.from; } 6478 if (options.to != null && options.to < to) { to = options.to; } 6479 var copy = new Doc(getLines(this, from, to), options.mode || this.modeOption, from, this.lineSep, this.direction); 6480 if (options.sharedHist) { copy.history = this.history 6481 ; }(this.linked || (this.linked = [])).push({doc: copy, sharedHist: options.sharedHist}); 6482 copy.linked = [{doc: this, isParent: true, sharedHist: options.sharedHist}]; 6483 copySharedMarkers(copy, findSharedMarkers(this)); 6484 return copy 6485 }, 6486 unlinkDoc: function(other) { 6487 if (other instanceof CodeMirror) { other = other.doc; } 6488 if (this.linked) { for (var i = 0; i < this.linked.length; ++i) { 6489 var link = this.linked[i]; 6490 if (link.doc != other) { continue } 6491 this.linked.splice(i, 1); 6492 other.unlinkDoc(this); 6493 detachSharedMarkers(findSharedMarkers(this)); 6494 break 6495 } } 6496 // If the histories were shared, split them again 6497 if (other.history == this.history) { 6498 var splitIds = [other.id]; 6499 linkedDocs(other, function (doc) { return splitIds.push(doc.id); }, true); 6500 other.history = new History(null); 6501 other.history.done = copyHistoryArray(this.history.done, splitIds); 6502 other.history.undone = copyHistoryArray(this.history.undone, splitIds); 6503 } 6504 }, 6505 iterLinkedDocs: function(f) {linkedDocs(this, f);}, 6506 6507 getMode: function() {return this.mode}, 6508 getEditor: function() {return this.cm}, 6509 6510 splitLines: function(str) { 6511 if (this.lineSep) { return str.split(this.lineSep) } 6512 return splitLinesAuto(str) 6513 }, 6514 lineSeparator: function() { return this.lineSep || "\n" }, 6515 6516 setDirection: docMethodOp(function (dir) { 6517 if (dir != "rtl") { dir = "ltr"; } 6518 if (dir == this.direction) { return } 6519 this.direction = dir; 6520 this.iter(function (line) { return line.order = null; }); 6521 if (this.cm) { directionChanged(this.cm); } 6522 }) 6523 }); 6524 6525 // Public alias. 6526 Doc.prototype.eachLine = Doc.prototype.iter; 6527 6528 // Kludge to work around strange IE behavior where it'll sometimes 6529 // re-fire a series of drag-related events right after the drop (#1551) 6530 var lastDrop = 0; 6531 6532 function onDrop(e) { 6533 var cm = this; 6534 clearDragCursor(cm); 6535 if (signalDOMEvent(cm, e) || eventInWidget(cm.display, e)) 6536 { return } 6537 e_preventDefault(e); 6538 if (ie) { lastDrop = +new Date; } 6539 var pos = posFromMouse(cm, e, true), files = e.dataTransfer.files; 6540 if (!pos || cm.isReadOnly()) { return } 6541 // Might be a file drop, in which case we simply extract the text 6542 // and insert it. 6543 if (files && files.length && window.FileReader && window.File) { 6544 var n = files.length, text = Array(n), read = 0; 6545 var markAsReadAndPasteIfAllFilesAreRead = function () { 6546 if (++read == n) { 6547 operation(cm, function () { 6548 pos = clipPos(cm.doc, pos); 6549 var change = {from: pos, to: pos, 6550 text: cm.doc.splitLines( 6551 text.filter(function (t) { return t != null; }).join(cm.doc.lineSeparator())), 6552 origin: "paste"}; 6553 makeChange(cm.doc, change); 6554 setSelectionReplaceHistory(cm.doc, simpleSelection(clipPos(cm.doc, pos), clipPos(cm.doc, changeEnd(change)))); 6555 })(); 6556 } 6557 }; 6558 var readTextFromFile = function (file, i) { 6559 if (cm.options.allowDropFileTypes && 6560 indexOf(cm.options.allowDropFileTypes, file.type) == -1) { 6561 markAsReadAndPasteIfAllFilesAreRead(); 6562 return 6563 } 6564 var reader = new FileReader; 6565 reader.onerror = function () { return markAsReadAndPasteIfAllFilesAreRead(); }; 6566 reader.onload = function () { 6567 var content = reader.result; 6568 if (/[\x00-\x08\x0e-\x1f]{2}/.test(content)) { 6569 markAsReadAndPasteIfAllFilesAreRead(); 6570 return 6571 } 6572 text[i] = content; 6573 markAsReadAndPasteIfAllFilesAreRead(); 6574 }; 6575 reader.readAsText(file); 6576 }; 6577 for (var i = 0; i < files.length; i++) { readTextFromFile(files[i], i); } 6578 } else { // Normal drop 6579 // Don't do a replace if the drop happened inside of the selected text. 6580 if (cm.state.draggingText && cm.doc.sel.contains(pos) > -1) { 6581 cm.state.draggingText(e); 6582 // Ensure the editor is re-focused 6583 setTimeout(function () { return cm.display.input.focus(); }, 20); 6584 return 6585 } 6586 try { 6587 var text$1 = e.dataTransfer.getData("Text"); 6588 if (text$1) { 6589 var selected; 6590 if (cm.state.draggingText && !cm.state.draggingText.copy) 6591 { selected = cm.listSelections(); } 6592 setSelectionNoUndo(cm.doc, simpleSelection(pos, pos)); 6593 if (selected) { for (var i$1 = 0; i$1 < selected.length; ++i$1) 6594 { replaceRange(cm.doc, "", selected[i$1].anchor, selected[i$1].head, "drag"); } } 6595 cm.replaceSelection(text$1, "around", "paste"); 6596 cm.display.input.focus(); 6597 } 6598 } 6599 catch(e$1){} 6600 } 6601 } 6602 6603 function onDragStart(cm, e) { 6604 if (ie && (!cm.state.draggingText || +new Date - lastDrop < 100)) { e_stop(e); return } 6605 if (signalDOMEvent(cm, e) || eventInWidget(cm.display, e)) { return } 6606 6607 e.dataTransfer.setData("Text", cm.getSelection()); 6608 e.dataTransfer.effectAllowed = "copyMove"; 6609 6610 // Use dummy image instead of default browsers image. 6611 // Recent Safari (~6.0.2) have a tendency to segfault when this happens, so we don't do it there. 6612 if (e.dataTransfer.setDragImage && !safari) { 6613 var img = elt("img", null, null, "position: fixed; left: 0; top: 0;"); 6614 img.src = "data:image/gif;base64,R0lGODlhAQABAAAAACH5BAEKAAEALAAAAAABAAEAAAICTAEAOw=="; 6615 if (presto) { 6616 img.width = img.height = 1; 6617 cm.display.wrapper.appendChild(img); 6618 // Force a relayout, or Opera won't use our image for some obscure reason 6619 img._top = img.offsetTop; 6620 } 6621 e.dataTransfer.setDragImage(img, 0, 0); 6622 if (presto) { img.parentNode.removeChild(img); } 6623 } 6624 } 6625 6626 function onDragOver(cm, e) { 6627 var pos = posFromMouse(cm, e); 6628 if (!pos) { return } 6629 var frag = document.createDocumentFragment(); 6630 drawSelectionCursor(cm, pos, frag); 6631 if (!cm.display.dragCursor) { 6632 cm.display.dragCursor = elt("div", null, "CodeMirror-cursors CodeMirror-dragcursors"); 6633 cm.display.lineSpace.insertBefore(cm.display.dragCursor, cm.display.cursorDiv); 6634 } 6635 removeChildrenAndAdd(cm.display.dragCursor, frag); 6636 } 6637 6638 function clearDragCursor(cm) { 6639 if (cm.display.dragCursor) { 6640 cm.display.lineSpace.removeChild(cm.display.dragCursor); 6641 cm.display.dragCursor = null; 6642 } 6643 } 6644 6645 // These must be handled carefully, because naively registering a 6646 // handler for each editor will cause the editors to never be 6647 // garbage collected. 6648 6649 function forEachCodeMirror(f) { 6650 if (!document.getElementsByClassName) { return } 6651 var byClass = document.getElementsByClassName("CodeMirror"), editors = []; 6652 for (var i = 0; i < byClass.length; i++) { 6653 var cm = byClass[i].CodeMirror; 6654 if (cm) { editors.push(cm); } 6655 } 6656 if (editors.length) { editors[0].operation(function () { 6657 for (var i = 0; i < editors.length; i++) { f(editors[i]); } 6658 }); } 6659 } 6660 6661 var globalsRegistered = false; 6662 function ensureGlobalHandlers() { 6663 if (globalsRegistered) { return } 6664 registerGlobalHandlers(); 6665 globalsRegistered = true; 6666 } 6667 function registerGlobalHandlers() { 6668 // When the window resizes, we need to refresh active editors. 6669 var resizeTimer; 6670 on(window, "resize", function () { 6671 if (resizeTimer == null) { resizeTimer = setTimeout(function () { 6672 resizeTimer = null; 6673 forEachCodeMirror(onResize); 6674 }, 100); } 6675 }); 6676 // When the window loses focus, we want to show the editor as blurred 6677 on(window, "blur", function () { return forEachCodeMirror(onBlur); }); 6678 } 6679 // Called when the window resizes 6680 function onResize(cm) { 6681 var d = cm.display; 6682 // Might be a text scaling operation, clear size caches. 6683 d.cachedCharWidth = d.cachedTextHeight = d.cachedPaddingH = null; 6684 d.scrollbarsClipped = false; 6685 cm.setSize(); 6686 } 6687 6688 var keyNames = { 6689 3: "Pause", 8: "Backspace", 9: "Tab", 13: "Enter", 16: "Shift", 17: "Ctrl", 18: "Alt", 6690 19: "Pause", 20: "CapsLock", 27: "Esc", 32: "Space", 33: "PageUp", 34: "PageDown", 35: "End", 6691 36: "Home", 37: "Left", 38: "Up", 39: "Right", 40: "Down", 44: "PrintScrn", 45: "Insert", 6692 46: "Delete", 59: ";", 61: "=", 91: "Mod", 92: "Mod", 93: "Mod", 6693 106: "*", 107: "=", 109: "-", 110: ".", 111: "/", 145: "ScrollLock", 6694 173: "-", 186: ";", 187: "=", 188: ",", 189: "-", 190: ".", 191: "/", 192: "`", 219: "[", 220: "\\", 6695 221: "]", 222: "'", 224: "Mod", 63232: "Up", 63233: "Down", 63234: "Left", 63235: "Right", 63272: "Delete", 6696 63273: "Home", 63275: "End", 63276: "PageUp", 63277: "PageDown", 63302: "Insert" 6697 }; 6698 6699 // Number keys 6700 for (var i = 0; i < 10; i++) { keyNames[i + 48] = keyNames[i + 96] = String(i); } 6701 // Alphabetic keys 6702 for (var i$1 = 65; i$1 <= 90; i$1++) { keyNames[i$1] = String.fromCharCode(i$1); } 6703 // Function keys 6704 for (var i$2 = 1; i$2 <= 12; i$2++) { keyNames[i$2 + 111] = keyNames[i$2 + 63235] = "F" + i$2; } 6705 6706 var keyMap = {}; 6707 6708 keyMap.basic = { 6709 "Left": "goCharLeft", "Right": "goCharRight", "Up": "goLineUp", "Down": "goLineDown", 6710 "End": "goLineEnd", "Home": "goLineStartSmart", "PageUp": "goPageUp", "PageDown": "goPageDown", 6711 "Delete": "delCharAfter", "Backspace": "delCharBefore", "Shift-Backspace": "delCharBefore", 6712 "Tab": "defaultTab", "Shift-Tab": "indentAuto", 6713 "Enter": "newlineAndIndent", "Insert": "toggleOverwrite", 6714 "Esc": "singleSelection" 6715 }; 6716 // Note that the save and find-related commands aren't defined by 6717 // default. User code or addons can define them. Unknown commands 6718 // are simply ignored. 6719 keyMap.pcDefault = { 6720 "Ctrl-A": "selectAll", "Ctrl-D": "deleteLine", "Ctrl-Z": "undo", "Shift-Ctrl-Z": "redo", "Ctrl-Y": "redo", 6721 "Ctrl-Home": "goDocStart", "Ctrl-End": "goDocEnd", "Ctrl-Up": "goLineUp", "Ctrl-Down": "goLineDown", 6722 "Ctrl-Left": "goGroupLeft", "Ctrl-Right": "goGroupRight", "Alt-Left": "goLineStart", "Alt-Right": "goLineEnd", 6723 "Ctrl-Backspace": "delGroupBefore", "Ctrl-Delete": "delGroupAfter", "Ctrl-S": "save", "Ctrl-F": "find", 6724 "Ctrl-G": "findNext", "Shift-Ctrl-G": "findPrev", "Shift-Ctrl-F": "replace", "Shift-Ctrl-R": "replaceAll", 6725 "Ctrl-[": "indentLess", "Ctrl-]": "indentMore", 6726 "Ctrl-U": "undoSelection", "Shift-Ctrl-U": "redoSelection", "Alt-U": "redoSelection", 6727 "fallthrough": "basic" 6728 }; 6729 // Very basic readline/emacs-style bindings, which are standard on Mac. 6730 keyMap.emacsy = { 6731 "Ctrl-F": "goCharRight", "Ctrl-B": "goCharLeft", "Ctrl-P": "goLineUp", "Ctrl-N": "goLineDown", 6732 "Ctrl-A": "goLineStart", "Ctrl-E": "goLineEnd", "Ctrl-V": "goPageDown", "Shift-Ctrl-V": "goPageUp", 6733 "Ctrl-D": "delCharAfter", "Ctrl-H": "delCharBefore", "Alt-Backspace": "delWordBefore", "Ctrl-K": "killLine", 6734 "Ctrl-T": "transposeChars", "Ctrl-O": "openLine" 6735 }; 6736 keyMap.macDefault = { 6737 "Cmd-A": "selectAll", "Cmd-D": "deleteLine", "Cmd-Z": "undo", "Shift-Cmd-Z": "redo", "Cmd-Y": "redo", 6738 "Cmd-Home": "goDocStart", "Cmd-Up": "goDocStart", "Cmd-End": "goDocEnd", "Cmd-Down": "goDocEnd", "Alt-Left": "goGroupLeft", 6739 "Alt-Right": "goGroupRight", "Cmd-Left": "goLineLeft", "Cmd-Right": "goLineRight", "Alt-Backspace": "delGroupBefore", 6740 "Ctrl-Alt-Backspace": "delGroupAfter", "Alt-Delete": "delGroupAfter", "Cmd-S": "save", "Cmd-F": "find", 6741 "Cmd-G": "findNext", "Shift-Cmd-G": "findPrev", "Cmd-Alt-F": "replace", "Shift-Cmd-Alt-F": "replaceAll", 6742 "Cmd-[": "indentLess", "Cmd-]": "indentMore", "Cmd-Backspace": "delWrappedLineLeft", "Cmd-Delete": "delWrappedLineRight", 6743 "Cmd-U": "undoSelection", "Shift-Cmd-U": "redoSelection", "Ctrl-Up": "goDocStart", "Ctrl-Down": "goDocEnd", 6744 "fallthrough": ["basic", "emacsy"] 6745 }; 6746 keyMap["default"] = mac ? keyMap.macDefault : keyMap.pcDefault; 6747 6748 // KEYMAP DISPATCH 6749 6750 function normalizeKeyName(name) { 6751 var parts = name.split(/-(?!$)/); 6752 name = parts[parts.length - 1]; 6753 var alt, ctrl, shift, cmd; 6754 for (var i = 0; i < parts.length - 1; i++) { 6755 var mod = parts[i]; 6756 if (/^(cmd|meta|m)$/i.test(mod)) { cmd = true; } 6757 else if (/^a(lt)?$/i.test(mod)) { alt = true; } 6758 else if (/^(c|ctrl|control)$/i.test(mod)) { ctrl = true; } 6759 else if (/^s(hift)?$/i.test(mod)) { shift = true; } 6760 else { throw new Error("Unrecognized modifier name: " + mod) } 6761 } 6762 if (alt) { name = "Alt-" + name; } 6763 if (ctrl) { name = "Ctrl-" + name; } 6764 if (cmd) { name = "Cmd-" + name; } 6765 if (shift) { name = "Shift-" + name; } 6766 return name 6767 } 6768 6769 // This is a kludge to keep keymaps mostly working as raw objects 6770 // (backwards compatibility) while at the same time support features 6771 // like normalization and multi-stroke key bindings. It compiles a 6772 // new normalized keymap, and then updates the old object to reflect 6773 // this. 6774 function normalizeKeyMap(keymap) { 6775 var copy = {}; 6776 for (var keyname in keymap) { if (keymap.hasOwnProperty(keyname)) { 6777 var value = keymap[keyname]; 6778 if (/^(name|fallthrough|(de|at)tach)$/.test(keyname)) { continue } 6779 if (value == "...") { delete keymap[keyname]; continue } 6780 6781 var keys = map(keyname.split(" "), normalizeKeyName); 6782 for (var i = 0; i < keys.length; i++) { 6783 var val = (void 0), name = (void 0); 6784 if (i == keys.length - 1) { 6785 name = keys.join(" "); 6786 val = value; 6787 } else { 6788 name = keys.slice(0, i + 1).join(" "); 6789 val = "..."; 6790 } 6791 var prev = copy[name]; 6792 if (!prev) { copy[name] = val; } 6793 else if (prev != val) { throw new Error("Inconsistent bindings for " + name) } 6794 } 6795 delete keymap[keyname]; 6796 } } 6797 for (var prop in copy) { keymap[prop] = copy[prop]; } 6798 return keymap 6799 } 6800 6801 function lookupKey(key, map, handle, context) { 6802 map = getKeyMap(map); 6803 var found = map.call ? map.call(key, context) : map[key]; 6804 if (found === false) { return "nothing" } 6805 if (found === "...") { return "multi" } 6806 if (found != null && handle(found)) { return "handled" } 6807 6808 if (map.fallthrough) { 6809 if (Object.prototype.toString.call(map.fallthrough) != "[object Array]") 6810 { return lookupKey(key, map.fallthrough, handle, context) } 6811 for (var i = 0; i < map.fallthrough.length; i++) { 6812 var result = lookupKey(key, map.fallthrough[i], handle, context); 6813 if (result) { return result } 6814 } 6815 } 6816 } 6817 6818 // Modifier key presses don't count as 'real' key presses for the 6819 // purpose of keymap fallthrough. 6820 function isModifierKey(value) { 6821 var name = typeof value == "string" ? value : keyNames[value.keyCode]; 6822 return name == "Ctrl" || name == "Alt" || name == "Shift" || name == "Mod" 6823 } 6824 6825 function addModifierNames(name, event, noShift) { 6826 var base = name; 6827 if (event.altKey && base != "Alt") { name = "Alt-" + name; } 6828 if ((flipCtrlCmd ? event.metaKey : event.ctrlKey) && base != "Ctrl") { name = "Ctrl-" + name; } 6829 if ((flipCtrlCmd ? event.ctrlKey : event.metaKey) && base != "Mod") { name = "Cmd-" + name; } 6830 if (!noShift && event.shiftKey && base != "Shift") { name = "Shift-" + name; } 6831 return name 6832 } 6833 6834 // Look up the name of a key as indicated by an event object. 6835 function keyName(event, noShift) { 6836 if (presto && event.keyCode == 34 && event["char"]) { return false } 6837 var name = keyNames[event.keyCode]; 6838 if (name == null || event.altGraphKey) { return false } 6839 // Ctrl-ScrollLock has keyCode 3, same as Ctrl-Pause, 6840 // so we'll use event.code when available (Chrome 48+, FF 38+, Safari 10.1+) 6841 if (event.keyCode == 3 && event.code) { name = event.code; } 6842 return addModifierNames(name, event, noShift) 6843 } 6844 6845 function getKeyMap(val) { 6846 return typeof val == "string" ? keyMap[val] : val 6847 } 6848 6849 // Helper for deleting text near the selection(s), used to implement 6850 // backspace, delete, and similar functionality. 6851 function deleteNearSelection(cm, compute) { 6852 var ranges = cm.doc.sel.ranges, kill = []; 6853 // Build up a set of ranges to kill first, merging overlapping 6854 // ranges. 6855 for (var i = 0; i < ranges.length; i++) { 6856 var toKill = compute(ranges[i]); 6857 while (kill.length && cmp(toKill.from, lst(kill).to) <= 0) { 6858 var replaced = kill.pop(); 6859 if (cmp(replaced.from, toKill.from) < 0) { 6860 toKill.from = replaced.from; 6861 break 6862 } 6863 } 6864 kill.push(toKill); 6865 } 6866 // Next, remove those actual ranges. 6867 runInOp(cm, function () { 6868 for (var i = kill.length - 1; i >= 0; i--) 6869 { replaceRange(cm.doc, "", kill[i].from, kill[i].to, "+delete"); } 6870 ensureCursorVisible(cm); 6871 }); 6872 } 6873 6874 function moveCharLogically(line, ch, dir) { 6875 var target = skipExtendingChars(line.text, ch + dir, dir); 6876 return target < 0 || target > line.text.length ? null : target 6877 } 6878 6879 function moveLogically(line, start, dir) { 6880 var ch = moveCharLogically(line, start.ch, dir); 6881 return ch == null ? null : new Pos(start.line, ch, dir < 0 ? "after" : "before") 6882 } 6883 6884 function endOfLine(visually, cm, lineObj, lineNo, dir) { 6885 if (visually) { 6886 if (cm.doc.direction == "rtl") { dir = -dir; } 6887 var order = getOrder(lineObj, cm.doc.direction); 6888 if (order) { 6889 var part = dir < 0 ? lst(order) : order[0]; 6890 var moveInStorageOrder = (dir < 0) == (part.level == 1); 6891 var sticky = moveInStorageOrder ? "after" : "before"; 6892 var ch; 6893 // With a wrapped rtl chunk (possibly spanning multiple bidi parts), 6894 // it could be that the last bidi part is not on the last visual line, 6895 // since visual lines contain content order-consecutive chunks. 6896 // Thus, in rtl, we are looking for the first (content-order) character 6897 // in the rtl chunk that is on the last line (that is, the same line 6898 // as the last (content-order) character). 6899 if (part.level > 0 || cm.doc.direction == "rtl") { 6900 var prep = prepareMeasureForLine(cm, lineObj); 6901 ch = dir < 0 ? lineObj.text.length - 1 : 0; 6902 var targetTop = measureCharPrepared(cm, prep, ch).top; 6903 ch = findFirst(function (ch) { return measureCharPrepared(cm, prep, ch).top == targetTop; }, (dir < 0) == (part.level == 1) ? part.from : part.to - 1, ch); 6904 if (sticky == "before") { ch = moveCharLogically(lineObj, ch, 1); } 6905 } else { ch = dir < 0 ? part.to : part.from; } 6906 return new Pos(lineNo, ch, sticky) 6907 } 6908 } 6909 return new Pos(lineNo, dir < 0 ? lineObj.text.length : 0, dir < 0 ? "before" : "after") 6910 } 6911 6912 function moveVisually(cm, line, start, dir) { 6913 var bidi = getOrder(line, cm.doc.direction); 6914 if (!bidi) { return moveLogically(line, start, dir) } 6915 if (start.ch >= line.text.length) { 6916 start.ch = line.text.length; 6917 start.sticky = "before"; 6918 } else if (start.ch <= 0) { 6919 start.ch = 0; 6920 start.sticky = "after"; 6921 } 6922 var partPos = getBidiPartAt(bidi, start.ch, start.sticky), part = bidi[partPos]; 6923 if (cm.doc.direction == "ltr" && part.level % 2 == 0 && (dir > 0 ? part.to > start.ch : part.from < start.ch)) { 6924 // Case 1: We move within an ltr part in an ltr editor. Even with wrapped lines, 6925 // nothing interesting happens. 6926 return moveLogically(line, start, dir) 6927 } 6928 6929 var mv = function (pos, dir) { return moveCharLogically(line, pos instanceof Pos ? pos.ch : pos, dir); }; 6930 var prep; 6931 var getWrappedLineExtent = function (ch) { 6932 if (!cm.options.lineWrapping) { return {begin: 0, end: line.text.length} } 6933 prep = prep || prepareMeasureForLine(cm, line); 6934 return wrappedLineExtentChar(cm, line, prep, ch) 6935 }; 6936 var wrappedLineExtent = getWrappedLineExtent(start.sticky == "before" ? mv(start, -1) : start.ch); 6937 6938 if (cm.doc.direction == "rtl" || part.level == 1) { 6939 var moveInStorageOrder = (part.level == 1) == (dir < 0); 6940 var ch = mv(start, moveInStorageOrder ? 1 : -1); 6941 if (ch != null && (!moveInStorageOrder ? ch >= part.from && ch >= wrappedLineExtent.begin : ch <= part.to && ch <= wrappedLineExtent.end)) { 6942 // Case 2: We move within an rtl part or in an rtl editor on the same visual line 6943 var sticky = moveInStorageOrder ? "before" : "after"; 6944 return new Pos(start.line, ch, sticky) 6945 } 6946 } 6947 6948 // Case 3: Could not move within this bidi part in this visual line, so leave 6949 // the current bidi part 6950 6951 var searchInVisualLine = function (partPos, dir, wrappedLineExtent) { 6952 var getRes = function (ch, moveInStorageOrder) { return moveInStorageOrder 6953 ? new Pos(start.line, mv(ch, 1), "before") 6954 : new Pos(start.line, ch, "after"); }; 6955 6956 for (; partPos >= 0 && partPos < bidi.length; partPos += dir) { 6957 var part = bidi[partPos]; 6958 var moveInStorageOrder = (dir > 0) == (part.level != 1); 6959 var ch = moveInStorageOrder ? wrappedLineExtent.begin : mv(wrappedLineExtent.end, -1); 6960 if (part.from <= ch && ch < part.to) { return getRes(ch, moveInStorageOrder) } 6961 ch = moveInStorageOrder ? part.from : mv(part.to, -1); 6962 if (wrappedLineExtent.begin <= ch && ch < wrappedLineExtent.end) { return getRes(ch, moveInStorageOrder) } 6963 } 6964 }; 6965 6966 // Case 3a: Look for other bidi parts on the same visual line 6967 var res = searchInVisualLine(partPos + dir, dir, wrappedLineExtent); 6968 if (res) { return res } 6969 6970 // Case 3b: Look for other bidi parts on the next visual line 6971 var nextCh = dir > 0 ? wrappedLineExtent.end : mv(wrappedLineExtent.begin, -1); 6972 if (nextCh != null && !(dir > 0 && nextCh == line.text.length)) { 6973 res = searchInVisualLine(dir > 0 ? 0 : bidi.length - 1, dir, getWrappedLineExtent(nextCh)); 6974 if (res) { return res } 6975 } 6976 6977 // Case 4: Nowhere to move 6978 return null 6979 } 6980 6981 // Commands are parameter-less actions that can be performed on an 6982 // editor, mostly used for keybindings. 6983 var commands = { 6984 selectAll: selectAll, 6985 singleSelection: function (cm) { return cm.setSelection(cm.getCursor("anchor"), cm.getCursor("head"), sel_dontScroll); }, 6986 killLine: function (cm) { return deleteNearSelection(cm, function (range) { 6987 if (range.empty()) { 6988 var len = getLine(cm.doc, range.head.line).text.length; 6989 if (range.head.ch == len && range.head.line < cm.lastLine()) 6990 { return {from: range.head, to: Pos(range.head.line + 1, 0)} } 6991 else 6992 { return {from: range.head, to: Pos(range.head.line, len)} } 6993 } else { 6994 return {from: range.from(), to: range.to()} 6995 } 6996 }); }, 6997 deleteLine: function (cm) { return deleteNearSelection(cm, function (range) { return ({ 6998 from: Pos(range.from().line, 0), 6999 to: clipPos(cm.doc, Pos(range.to().line + 1, 0)) 7000 }); }); }, 7001 delLineLeft: function (cm) { return deleteNearSelection(cm, function (range) { return ({ 7002 from: Pos(range.from().line, 0), to: range.from() 7003 }); }); }, 7004 delWrappedLineLeft: function (cm) { return deleteNearSelection(cm, function (range) { 7005 var top = cm.charCoords(range.head, "div").top + 5; 7006 var leftPos = cm.coordsChar({left: 0, top: top}, "div"); 7007 return {from: leftPos, to: range.from()} 7008 }); }, 7009 delWrappedLineRight: function (cm) { return deleteNearSelection(cm, function (range) { 7010 var top = cm.charCoords(range.head, "div").top + 5; 7011 var rightPos = cm.coordsChar({left: cm.display.lineDiv.offsetWidth + 100, top: top}, "div"); 7012 return {from: range.from(), to: rightPos } 7013 }); }, 7014 undo: function (cm) { return cm.undo(); }, 7015 redo: function (cm) { return cm.redo(); }, 7016 undoSelection: function (cm) { return cm.undoSelection(); }, 7017 redoSelection: function (cm) { return cm.redoSelection(); }, 7018 goDocStart: function (cm) { return cm.extendSelection(Pos(cm.firstLine(), 0)); }, 7019 goDocEnd: function (cm) { return cm.extendSelection(Pos(cm.lastLine())); }, 7020 goLineStart: function (cm) { return cm.extendSelectionsBy(function (range) { return lineStart(cm, range.head.line); }, 7021 {origin: "+move", bias: 1} 7022 ); }, 7023 goLineStartSmart: function (cm) { return cm.extendSelectionsBy(function (range) { return lineStartSmart(cm, range.head); }, 7024 {origin: "+move", bias: 1} 7025 ); }, 7026 goLineEnd: function (cm) { return cm.extendSelectionsBy(function (range) { return lineEnd(cm, range.head.line); }, 7027 {origin: "+move", bias: -1} 7028 ); }, 7029 goLineRight: function (cm) { return cm.extendSelectionsBy(function (range) { 7030 var top = cm.cursorCoords(range.head, "div").top + 5; 7031 return cm.coordsChar({left: cm.display.lineDiv.offsetWidth + 100, top: top}, "div") 7032 }, sel_move); }, 7033 goLineLeft: function (cm) { return cm.extendSelectionsBy(function (range) { 7034 var top = cm.cursorCoords(range.head, "div").top + 5; 7035 return cm.coordsChar({left: 0, top: top}, "div") 7036 }, sel_move); }, 7037 goLineLeftSmart: function (cm) { return cm.extendSelectionsBy(function (range) { 7038 var top = cm.cursorCoords(range.head, "div").top + 5; 7039 var pos = cm.coordsChar({left: 0, top: top}, "div"); 7040 if (pos.ch < cm.getLine(pos.line).search(/\S/)) { return lineStartSmart(cm, range.head) } 7041 return pos 7042 }, sel_move); }, 7043 goLineUp: function (cm) { return cm.moveV(-1, "line"); }, 7044 goLineDown: function (cm) { return cm.moveV(1, "line"); }, 7045 goPageUp: function (cm) { return cm.moveV(-1, "page"); }, 7046 goPageDown: function (cm) { return cm.moveV(1, "page"); }, 7047 goCharLeft: function (cm) { return cm.moveH(-1, "char"); }, 7048 goCharRight: function (cm) { return cm.moveH(1, "char"); }, 7049 goColumnLeft: function (cm) { return cm.moveH(-1, "column"); }, 7050 goColumnRight: function (cm) { return cm.moveH(1, "column"); }, 7051 goWordLeft: function (cm) { return cm.moveH(-1, "word"); }, 7052 goGroupRight: function (cm) { return cm.moveH(1, "group"); }, 7053 goGroupLeft: function (cm) { return cm.moveH(-1, "group"); }, 7054 goWordRight: function (cm) { return cm.moveH(1, "word"); }, 7055 delCharBefore: function (cm) { return cm.deleteH(-1, "codepoint"); }, 7056 delCharAfter: function (cm) { return cm.deleteH(1, "char"); }, 7057 delWordBefore: function (cm) { return cm.deleteH(-1, "word"); }, 7058 delWordAfter: function (cm) { return cm.deleteH(1, "word"); }, 7059 delGroupBefore: function (cm) { return cm.deleteH(-1, "group"); }, 7060 delGroupAfter: function (cm) { return cm.deleteH(1, "group"); }, 7061 indentAuto: function (cm) { return cm.indentSelection("smart"); }, 7062 indentMore: function (cm) { return cm.indentSelection("add"); }, 7063 indentLess: function (cm) { return cm.indentSelection("subtract"); }, 7064 insertTab: function (cm) { return cm.replaceSelection("\t"); }, 7065 insertSoftTab: function (cm) { 7066 var spaces = [], ranges = cm.listSelections(), tabSize = cm.options.tabSize; 7067 for (var i = 0; i < ranges.length; i++) { 7068 var pos = ranges[i].from(); 7069 var col = countColumn(cm.getLine(pos.line), pos.ch, tabSize); 7070 spaces.push(spaceStr(tabSize - col % tabSize)); 7071 } 7072 cm.replaceSelections(spaces); 7073 }, 7074 defaultTab: function (cm) { 7075 if (cm.somethingSelected()) { cm.indentSelection("add"); } 7076 else { cm.execCommand("insertTab"); } 7077 }, 7078 // Swap the two chars left and right of each selection's head. 7079 // Move cursor behind the two swapped characters afterwards. 7080 // 7081 // Doesn't consider line feeds a character. 7082 // Doesn't scan more than one line above to find a character. 7083 // Doesn't do anything on an empty line. 7084 // Doesn't do anything with non-empty selections. 7085 transposeChars: function (cm) { return runInOp(cm, function () { 7086 var ranges = cm.listSelections(), newSel = []; 7087 for (var i = 0; i < ranges.length; i++) { 7088 if (!ranges[i].empty()) { continue } 7089 var cur = ranges[i].head, line = getLine(cm.doc, cur.line).text; 7090 if (line) { 7091 if (cur.ch == line.length) { cur = new Pos(cur.line, cur.ch - 1); } 7092 if (cur.ch > 0) { 7093 cur = new Pos(cur.line, cur.ch + 1); 7094 cm.replaceRange(line.charAt(cur.ch - 1) + line.charAt(cur.ch - 2), 7095 Pos(cur.line, cur.ch - 2), cur, "+transpose"); 7096 } else if (cur.line > cm.doc.first) { 7097 var prev = getLine(cm.doc, cur.line - 1).text; 7098 if (prev) { 7099 cur = new Pos(cur.line, 1); 7100 cm.replaceRange(line.charAt(0) + cm.doc.lineSeparator() + 7101 prev.charAt(prev.length - 1), 7102 Pos(cur.line - 1, prev.length - 1), cur, "+transpose"); 7103 } 7104 } 7105 } 7106 newSel.push(new Range(cur, cur)); 7107 } 7108 cm.setSelections(newSel); 7109 }); }, 7110 newlineAndIndent: function (cm) { return runInOp(cm, function () { 7111 var sels = cm.listSelections(); 7112 for (var i = sels.length - 1; i >= 0; i--) 7113 { cm.replaceRange(cm.doc.lineSeparator(), sels[i].anchor, sels[i].head, "+input"); } 7114 sels = cm.listSelections(); 7115 for (var i$1 = 0; i$1 < sels.length; i$1++) 7116 { cm.indentLine(sels[i$1].from().line, null, true); } 7117 ensureCursorVisible(cm); 7118 }); }, 7119 openLine: function (cm) { return cm.replaceSelection("\n", "start"); }, 7120 toggleOverwrite: function (cm) { return cm.toggleOverwrite(); } 7121 }; 7122 7123 7124 function lineStart(cm, lineN) { 7125 var line = getLine(cm.doc, lineN); 7126 var visual = visualLine(line); 7127 if (visual != line) { lineN = lineNo(visual); } 7128 return endOfLine(true, cm, visual, lineN, 1) 7129 } 7130 function lineEnd(cm, lineN) { 7131 var line = getLine(cm.doc, lineN); 7132 var visual = visualLineEnd(line); 7133 if (visual != line) { lineN = lineNo(visual); } 7134 return endOfLine(true, cm, line, lineN, -1) 7135 } 7136 function lineStartSmart(cm, pos) { 7137 var start = lineStart(cm, pos.line); 7138 var line = getLine(cm.doc, start.line); 7139 var order = getOrder(line, cm.doc.direction); 7140 if (!order || order[0].level == 0) { 7141 var firstNonWS = Math.max(start.ch, line.text.search(/\S/)); 7142 var inWS = pos.line == start.line && pos.ch <= firstNonWS && pos.ch; 7143 return Pos(start.line, inWS ? 0 : firstNonWS, start.sticky) 7144 } 7145 return start 7146 } 7147 7148 // Run a handler that was bound to a key. 7149 function doHandleBinding(cm, bound, dropShift) { 7150 if (typeof bound == "string") { 7151 bound = commands[bound]; 7152 if (!bound) { return false } 7153 } 7154 // Ensure previous input has been read, so that the handler sees a 7155 // consistent view of the document 7156 cm.display.input.ensurePolled(); 7157 var prevShift = cm.display.shift, done = false; 7158 try { 7159 if (cm.isReadOnly()) { cm.state.suppressEdits = true; } 7160 if (dropShift) { cm.display.shift = false; } 7161 done = bound(cm) != Pass; 7162 } finally { 7163 cm.display.shift = prevShift; 7164 cm.state.suppressEdits = false; 7165 } 7166 return done 7167 } 7168 7169 function lookupKeyForEditor(cm, name, handle) { 7170 for (var i = 0; i < cm.state.keyMaps.length; i++) { 7171 var result = lookupKey(name, cm.state.keyMaps[i], handle, cm); 7172 if (result) { return result } 7173 } 7174 return (cm.options.extraKeys && lookupKey(name, cm.options.extraKeys, handle, cm)) 7175 || lookupKey(name, cm.options.keyMap, handle, cm) 7176 } 7177 7178 // Note that, despite the name, this function is also used to check 7179 // for bound mouse clicks. 7180 7181 var stopSeq = new Delayed; 7182 7183 function dispatchKey(cm, name, e, handle) { 7184 var seq = cm.state.keySeq; 7185 if (seq) { 7186 if (isModifierKey(name)) { return "handled" } 7187 if (/\'$/.test(name)) 7188 { cm.state.keySeq = null; } 7189 else 7190 { stopSeq.set(50, function () { 7191 if (cm.state.keySeq == seq) { 7192 cm.state.keySeq = null; 7193 cm.display.input.reset(); 7194 } 7195 }); } 7196 if (dispatchKeyInner(cm, seq + " " + name, e, handle)) { return true } 7197 } 7198 return dispatchKeyInner(cm, name, e, handle) 7199 } 7200 7201 function dispatchKeyInner(cm, name, e, handle) { 7202 var result = lookupKeyForEditor(cm, name, handle); 7203 7204 if (result == "multi") 7205 { cm.state.keySeq = name; } 7206 if (result == "handled") 7207 { signalLater(cm, "keyHandled", cm, name, e); } 7208 7209 if (result == "handled" || result == "multi") { 7210 e_preventDefault(e); 7211 restartBlink(cm); 7212 } 7213 7214 return !!result 7215 } 7216 7217 // Handle a key from the keydown event. 7218 function handleKeyBinding(cm, e) { 7219 var name = keyName(e, true); 7220 if (!name) { return false } 7221 7222 if (e.shiftKey && !cm.state.keySeq) { 7223 // First try to resolve full name (including 'Shift-'). Failing 7224 // that, see if there is a cursor-motion command (starting with 7225 // 'go') bound to the keyname without 'Shift-'. 7226 return dispatchKey(cm, "Shift-" + name, e, function (b) { return doHandleBinding(cm, b, true); }) 7227 || dispatchKey(cm, name, e, function (b) { 7228 if (typeof b == "string" ? /^go[A-Z]/.test(b) : b.motion) 7229 { return doHandleBinding(cm, b) } 7230 }) 7231 } else { 7232 return dispatchKey(cm, name, e, function (b) { return doHandleBinding(cm, b); }) 7233 } 7234 } 7235 7236 // Handle a key from the keypress event 7237 function handleCharBinding(cm, e, ch) { 7238 return dispatchKey(cm, "'" + ch + "'", e, function (b) { return doHandleBinding(cm, b, true); }) 7239 } 7240 7241 var lastStoppedKey = null; 7242 function onKeyDown(e) { 7243 var cm = this; 7244 if (e.target && e.target != cm.display.input.getField()) { return } 7245 cm.curOp.focus = activeElt(); 7246 if (signalDOMEvent(cm, e)) { return } 7247 // IE does strange things with escape. 7248 if (ie && ie_version < 11 && e.keyCode == 27) { e.returnValue = false; } 7249 var code = e.keyCode; 7250 cm.display.shift = code == 16 || e.shiftKey; 7251 var handled = handleKeyBinding(cm, e); 7252 if (presto) { 7253 lastStoppedKey = handled ? code : null; 7254 // Opera has no cut event... we try to at least catch the key combo 7255 if (!handled && code == 88 && !hasCopyEvent && (mac ? e.metaKey : e.ctrlKey)) 7256 { cm.replaceSelection("", null, "cut"); } 7257 } 7258 if (gecko && !mac && !handled && code == 46 && e.shiftKey && !e.ctrlKey && document.execCommand) 7259 { document.execCommand("cut"); } 7260 7261 // Turn mouse into crosshair when Alt is held on Mac. 7262 if (code == 18 && !/\bCodeMirror-crosshair\b/.test(cm.display.lineDiv.className)) 7263 { showCrossHair(cm); } 7264 } 7265 7266 function showCrossHair(cm) { 7267 var lineDiv = cm.display.lineDiv; 7268 addClass(lineDiv, "CodeMirror-crosshair"); 7269 7270 function up(e) { 7271 if (e.keyCode == 18 || !e.altKey) { 7272 rmClass(lineDiv, "CodeMirror-crosshair"); 7273 off(document, "keyup", up); 7274 off(document, "mouseover", up); 7275 } 7276 } 7277 on(document, "keyup", up); 7278 on(document, "mouseover", up); 7279 } 7280 7281 function onKeyUp(e) { 7282 if (e.keyCode == 16) { this.doc.sel.shift = false; } 7283 signalDOMEvent(this, e); 7284 } 7285 7286 function onKeyPress(e) { 7287 var cm = this; 7288 if (e.target && e.target != cm.display.input.getField()) { return } 7289 if (eventInWidget(cm.display, e) || signalDOMEvent(cm, e) || e.ctrlKey && !e.altKey || mac && e.metaKey) { return } 7290 var keyCode = e.keyCode, charCode = e.charCode; 7291 if (presto && keyCode == lastStoppedKey) {lastStoppedKey = null; e_preventDefault(e); return} 7292 if ((presto && (!e.which || e.which < 10)) && handleKeyBinding(cm, e)) { return } 7293 var ch = String.fromCharCode(charCode == null ? keyCode : charCode); 7294 // Some browsers fire keypress events for backspace 7295 if (ch == "\x08") { return } 7296 if (handleCharBinding(cm, e, ch)) { return } 7297 cm.display.input.onKeyPress(e); 7298 } 7299 7300 var DOUBLECLICK_DELAY = 400; 7301 7302 var PastClick = function(time, pos, button) { 7303 this.time = time; 7304 this.pos = pos; 7305 this.button = button; 7306 }; 7307 7308 PastClick.prototype.compare = function (time, pos, button) { 7309 return this.time + DOUBLECLICK_DELAY > time && 7310 cmp(pos, this.pos) == 0 && button == this.button 7311 }; 7312 7313 var lastClick, lastDoubleClick; 7314 function clickRepeat(pos, button) { 7315 var now = +new Date; 7316 if (lastDoubleClick && lastDoubleClick.compare(now, pos, button)) { 7317 lastClick = lastDoubleClick = null; 7318 return "triple" 7319 } else if (lastClick && lastClick.compare(now, pos, button)) { 7320 lastDoubleClick = new PastClick(now, pos, button); 7321 lastClick = null; 7322 return "double" 7323 } else { 7324 lastClick = new PastClick(now, pos, button); 7325 lastDoubleClick = null; 7326 return "single" 7327 } 7328 } 7329 7330 // A mouse down can be a single click, double click, triple click, 7331 // start of selection drag, start of text drag, new cursor 7332 // (ctrl-click), rectangle drag (alt-drag), or xwin 7333 // middle-click-paste. Or it might be a click on something we should 7334 // not interfere with, such as a scrollbar or widget. 7335 function onMouseDown(e) { 7336 var cm = this, display = cm.display; 7337 if (signalDOMEvent(cm, e) || display.activeTouch && display.input.supportsTouch()) { return } 7338 display.input.ensurePolled(); 7339 display.shift = e.shiftKey; 7340 7341 if (eventInWidget(display, e)) { 7342 if (!webkit) { 7343 // Briefly turn off draggability, to allow widgets to do 7344 // normal dragging things. 7345 display.scroller.draggable = false; 7346 setTimeout(function () { return display.scroller.draggable = true; }, 100); 7347 } 7348 return 7349 } 7350 if (clickInGutter(cm, e)) { return } 7351 var pos = posFromMouse(cm, e), button = e_button(e), repeat = pos ? clickRepeat(pos, button) : "single"; 7352 window.focus(); 7353 7354 // #3261: make sure, that we're not starting a second selection 7355 if (button == 1 && cm.state.selectingText) 7356 { cm.state.selectingText(e); } 7357 7358 if (pos && handleMappedButton(cm, button, pos, repeat, e)) { return } 7359 7360 if (button == 1) { 7361 if (pos) { leftButtonDown(cm, pos, repeat, e); } 7362 else if (e_target(e) == display.scroller) { e_preventDefault(e); } 7363 } else if (button == 2) { 7364 if (pos) { extendSelection(cm.doc, pos); } 7365 setTimeout(function () { return display.input.focus(); }, 20); 7366 } else if (button == 3) { 7367 if (captureRightClick) { cm.display.input.onContextMenu(e); } 7368 else { delayBlurEvent(cm); } 7369 } 7370 } 7371 7372 function handleMappedButton(cm, button, pos, repeat, event) { 7373 var name = "Click"; 7374 if (repeat == "double") { name = "Double" + name; } 7375 else if (repeat == "triple") { name = "Triple" + name; } 7376 name = (button == 1 ? "Left" : button == 2 ? "Middle" : "Right") + name; 7377 7378 return dispatchKey(cm, addModifierNames(name, event), event, function (bound) { 7379 if (typeof bound == "string") { bound = commands[bound]; } 7380 if (!bound) { return false } 7381 var done = false; 7382 try { 7383 if (cm.isReadOnly()) { cm.state.suppressEdits = true; } 7384 done = bound(cm, pos) != Pass; 7385 } finally { 7386 cm.state.suppressEdits = false; 7387 } 7388 return done 7389 }) 7390 } 7391 7392 function configureMouse(cm, repeat, event) { 7393 var option = cm.getOption("configureMouse"); 7394 var value = option ? option(cm, repeat, event) : {}; 7395 if (value.unit == null) { 7396 var rect = chromeOS ? event.shiftKey && event.metaKey : event.altKey; 7397 value.unit = rect ? "rectangle" : repeat == "single" ? "char" : repeat == "double" ? "word" : "line"; 7398 } 7399 if (value.extend == null || cm.doc.extend) { value.extend = cm.doc.extend || event.shiftKey; } 7400 if (value.addNew == null) { value.addNew = mac ? event.metaKey : event.ctrlKey; } 7401 if (value.moveOnDrag == null) { value.moveOnDrag = !(mac ? event.altKey : event.ctrlKey); } 7402 return value 7403 } 7404 7405 function leftButtonDown(cm, pos, repeat, event) { 7406 if (ie) { setTimeout(bind(ensureFocus, cm), 0); } 7407 else { cm.curOp.focus = activeElt(); } 7408 7409 var behavior = configureMouse(cm, repeat, event); 7410 7411 var sel = cm.doc.sel, contained; 7412 if (cm.options.dragDrop && dragAndDrop && !cm.isReadOnly() && 7413 repeat == "single" && (contained = sel.contains(pos)) > -1 && 7414 (cmp((contained = sel.ranges[contained]).from(), pos) < 0 || pos.xRel > 0) && 7415 (cmp(contained.to(), pos) > 0 || pos.xRel < 0)) 7416 { leftButtonStartDrag(cm, event, pos, behavior); } 7417 else 7418 { leftButtonSelect(cm, event, pos, behavior); } 7419 } 7420 7421 // Start a text drag. When it ends, see if any dragging actually 7422 // happen, and treat as a click if it didn't. 7423 function leftButtonStartDrag(cm, event, pos, behavior) { 7424 var display = cm.display, moved = false; 7425 var dragEnd = operation(cm, function (e) { 7426 if (webkit) { display.scroller.draggable = false; } 7427 cm.state.draggingText = false; 7428 if (cm.state.delayingBlurEvent) { 7429 if (cm.hasFocus()) { cm.state.delayingBlurEvent = false; } 7430 else { delayBlurEvent(cm); } 7431 } 7432 off(display.wrapper.ownerDocument, "mouseup", dragEnd); 7433 off(display.wrapper.ownerDocument, "mousemove", mouseMove); 7434 off(display.scroller, "dragstart", dragStart); 7435 off(display.scroller, "drop", dragEnd); 7436 if (!moved) { 7437 e_preventDefault(e); 7438 if (!behavior.addNew) 7439 { extendSelection(cm.doc, pos, null, null, behavior.extend); } 7440 // Work around unexplainable focus problem in IE9 (#2127) and Chrome (#3081) 7441 if ((webkit && !safari) || ie && ie_version == 9) 7442 { setTimeout(function () {display.wrapper.ownerDocument.body.focus({preventScroll: true}); display.input.focus();}, 20); } 7443 else 7444 { display.input.focus(); } 7445 } 7446 }); 7447 var mouseMove = function(e2) { 7448 moved = moved || Math.abs(event.clientX - e2.clientX) + Math.abs(event.clientY - e2.clientY) >= 10; 7449 }; 7450 var dragStart = function () { return moved = true; }; 7451 // Let the drag handler handle this. 7452 if (webkit) { display.scroller.draggable = true; } 7453 cm.state.draggingText = dragEnd; 7454 dragEnd.copy = !behavior.moveOnDrag; 7455 on(display.wrapper.ownerDocument, "mouseup", dragEnd); 7456 on(display.wrapper.ownerDocument, "mousemove", mouseMove); 7457 on(display.scroller, "dragstart", dragStart); 7458 on(display.scroller, "drop", dragEnd); 7459 7460 cm.state.delayingBlurEvent = true; 7461 setTimeout(function () { return display.input.focus(); }, 20); 7462 // IE's approach to draggable 7463 if (display.scroller.dragDrop) { display.scroller.dragDrop(); } 7464 } 7465 7466 function rangeForUnit(cm, pos, unit) { 7467 if (unit == "char") { return new Range(pos, pos) } 7468 if (unit == "word") { return cm.findWordAt(pos) } 7469 if (unit == "line") { return new Range(Pos(pos.line, 0), clipPos(cm.doc, Pos(pos.line + 1, 0))) } 7470 var result = unit(cm, pos); 7471 return new Range(result.from, result.to) 7472 } 7473 7474 // Normal selection, as opposed to text dragging. 7475 function leftButtonSelect(cm, event, start, behavior) { 7476 if (ie) { delayBlurEvent(cm); } 7477 var display = cm.display, doc = cm.doc; 7478 e_preventDefault(event); 7479 7480 var ourRange, ourIndex, startSel = doc.sel, ranges = startSel.ranges; 7481 if (behavior.addNew && !behavior.extend) { 7482 ourIndex = doc.sel.contains(start); 7483 if (ourIndex > -1) 7484 { ourRange = ranges[ourIndex]; } 7485 else 7486 { ourRange = new Range(start, start); } 7487 } else { 7488 ourRange = doc.sel.primary(); 7489 ourIndex = doc.sel.primIndex; 7490 } 7491 7492 if (behavior.unit == "rectangle") { 7493 if (!behavior.addNew) { ourRange = new Range(start, start); } 7494 start = posFromMouse(cm, event, true, true); 7495 ourIndex = -1; 7496 } else { 7497 var range = rangeForUnit(cm, start, behavior.unit); 7498 if (behavior.extend) 7499 { ourRange = extendRange(ourRange, range.anchor, range.head, behavior.extend); } 7500 else 7501 { ourRange = range; } 7502 } 7503 7504 if (!behavior.addNew) { 7505 ourIndex = 0; 7506 setSelection(doc, new Selection([ourRange], 0), sel_mouse); 7507 startSel = doc.sel; 7508 } else if (ourIndex == -1) { 7509 ourIndex = ranges.length; 7510 setSelection(doc, normalizeSelection(cm, ranges.concat([ourRange]), ourIndex), 7511 {scroll: false, origin: "*mouse"}); 7512 } else if (ranges.length > 1 && ranges[ourIndex].empty() && behavior.unit == "char" && !behavior.extend) { 7513 setSelection(doc, normalizeSelection(cm, ranges.slice(0, ourIndex).concat(ranges.slice(ourIndex + 1)), 0), 7514 {scroll: false, origin: "*mouse"}); 7515 startSel = doc.sel; 7516 } else { 7517 replaceOneSelection(doc, ourIndex, ourRange, sel_mouse); 7518 } 7519 7520 var lastPos = start; 7521 function extendTo(pos) { 7522 if (cmp(lastPos, pos) == 0) { return } 7523 lastPos = pos; 7524 7525 if (behavior.unit == "rectangle") { 7526 var ranges = [], tabSize = cm.options.tabSize; 7527 var startCol = countColumn(getLine(doc, start.line).text, start.ch, tabSize); 7528 var posCol = countColumn(getLine(doc, pos.line).text, pos.ch, tabSize); 7529 var left = Math.min(startCol, posCol), right = Math.max(startCol, posCol); 7530 for (var line = Math.min(start.line, pos.line), end = Math.min(cm.lastLine(), Math.max(start.line, pos.line)); 7531 line <= end; line++) { 7532 var text = getLine(doc, line).text, leftPos = findColumn(text, left, tabSize); 7533 if (left == right) 7534 { ranges.push(new Range(Pos(line, leftPos), Pos(line, leftPos))); } 7535 else if (text.length > leftPos) 7536 { ranges.push(new Range(Pos(line, leftPos), Pos(line, findColumn(text, right, tabSize)))); } 7537 } 7538 if (!ranges.length) { ranges.push(new Range(start, start)); } 7539 setSelection(doc, normalizeSelection(cm, startSel.ranges.slice(0, ourIndex).concat(ranges), ourIndex), 7540 {origin: "*mouse", scroll: false}); 7541 cm.scrollIntoView(pos); 7542 } else { 7543 var oldRange = ourRange; 7544 var range = rangeForUnit(cm, pos, behavior.unit); 7545 var anchor = oldRange.anchor, head; 7546 if (cmp(range.anchor, anchor) > 0) { 7547 head = range.head; 7548 anchor = minPos(oldRange.from(), range.anchor); 7549 } else { 7550 head = range.anchor; 7551 anchor = maxPos(oldRange.to(), range.head); 7552 } 7553 var ranges$1 = startSel.ranges.slice(0); 7554 ranges$1[ourIndex] = bidiSimplify(cm, new Range(clipPos(doc, anchor), head)); 7555 setSelection(doc, normalizeSelection(cm, ranges$1, ourIndex), sel_mouse); 7556 } 7557 } 7558 7559 var editorSize = display.wrapper.getBoundingClientRect(); 7560 // Used to ensure timeout re-tries don't fire when another extend 7561 // happened in the meantime (clearTimeout isn't reliable -- at 7562 // least on Chrome, the timeouts still happen even when cleared, 7563 // if the clear happens after their scheduled firing time). 7564 var counter = 0; 7565 7566 function extend(e) { 7567 var curCount = ++counter; 7568 var cur = posFromMouse(cm, e, true, behavior.unit == "rectangle"); 7569 if (!cur) { return } 7570 if (cmp(cur, lastPos) != 0) { 7571 cm.curOp.focus = activeElt(); 7572 extendTo(cur); 7573 var visible = visibleLines(display, doc); 7574 if (cur.line >= visible.to || cur.line < visible.from) 7575 { setTimeout(operation(cm, function () {if (counter == curCount) { extend(e); }}), 150); } 7576 } else { 7577 var outside = e.clientY < editorSize.top ? -20 : e.clientY > editorSize.bottom ? 20 : 0; 7578 if (outside) { setTimeout(operation(cm, function () { 7579 if (counter != curCount) { return } 7580 display.scroller.scrollTop += outside; 7581 extend(e); 7582 }), 50); } 7583 } 7584 } 7585 7586 function done(e) { 7587 cm.state.selectingText = false; 7588 counter = Infinity; 7589 // If e is null or undefined we interpret this as someone trying 7590 // to explicitly cancel the selection rather than the user 7591 // letting go of the mouse button. 7592 if (e) { 7593 e_preventDefault(e); 7594 display.input.focus(); 7595 } 7596 off(display.wrapper.ownerDocument, "mousemove", move); 7597 off(display.wrapper.ownerDocument, "mouseup", up); 7598 doc.history.lastSelOrigin = null; 7599 } 7600 7601 var move = operation(cm, function (e) { 7602 if (e.buttons === 0 || !e_button(e)) { done(e); } 7603 else { extend(e); } 7604 }); 7605 var up = operation(cm, done); 7606 cm.state.selectingText = up; 7607 on(display.wrapper.ownerDocument, "mousemove", move); 7608 on(display.wrapper.ownerDocument, "mouseup", up); 7609 } 7610 7611 // Used when mouse-selecting to adjust the anchor to the proper side 7612 // of a bidi jump depending on the visual position of the head. 7613 function bidiSimplify(cm, range) { 7614 var anchor = range.anchor; 7615 var head = range.head; 7616 var anchorLine = getLine(cm.doc, anchor.line); 7617 if (cmp(anchor, head) == 0 && anchor.sticky == head.sticky) { return range } 7618 var order = getOrder(anchorLine); 7619 if (!order) { return range } 7620 var index = getBidiPartAt(order, anchor.ch, anchor.sticky), part = order[index]; 7621 if (part.from != anchor.ch && part.to != anchor.ch) { return range } 7622 var boundary = index + ((part.from == anchor.ch) == (part.level != 1) ? 0 : 1); 7623 if (boundary == 0 || boundary == order.length) { return range } 7624 7625 // Compute the relative visual position of the head compared to the 7626 // anchor (<0 is to the left, >0 to the right) 7627 var leftSide; 7628 if (head.line != anchor.line) { 7629 leftSide = (head.line - anchor.line) * (cm.doc.direction == "ltr" ? 1 : -1) > 0; 7630 } else { 7631 var headIndex = getBidiPartAt(order, head.ch, head.sticky); 7632 var dir = headIndex - index || (head.ch - anchor.ch) * (part.level == 1 ? -1 : 1); 7633 if (headIndex == boundary - 1 || headIndex == boundary) 7634 { leftSide = dir < 0; } 7635 else 7636 { leftSide = dir > 0; } 7637 } 7638 7639 var usePart = order[boundary + (leftSide ? -1 : 0)]; 7640 var from = leftSide == (usePart.level == 1); 7641 var ch = from ? usePart.from : usePart.to, sticky = from ? "after" : "before"; 7642 return anchor.ch == ch && anchor.sticky == sticky ? range : new Range(new Pos(anchor.line, ch, sticky), head) 7643 } 7644 7645 7646 // Determines whether an event happened in the gutter, and fires the 7647 // handlers for the corresponding event. 7648 function gutterEvent(cm, e, type, prevent) { 7649 var mX, mY; 7650 if (e.touches) { 7651 mX = e.touches[0].clientX; 7652 mY = e.touches[0].clientY; 7653 } else { 7654 try { mX = e.clientX; mY = e.clientY; } 7655 catch(e$1) { return false } 7656 } 7657 if (mX >= Math.floor(cm.display.gutters.getBoundingClientRect().right)) { return false } 7658 if (prevent) { e_preventDefault(e); } 7659 7660 var display = cm.display; 7661 var lineBox = display.lineDiv.getBoundingClientRect(); 7662 7663 if (mY > lineBox.bottom || !hasHandler(cm, type)) { return e_defaultPrevented(e) } 7664 mY -= lineBox.top - display.viewOffset; 7665 7666 for (var i = 0; i < cm.display.gutterSpecs.length; ++i) { 7667 var g = display.gutters.childNodes[i]; 7668 if (g && g.getBoundingClientRect().right >= mX) { 7669 var line = lineAtHeight(cm.doc, mY); 7670 var gutter = cm.display.gutterSpecs[i]; 7671 signal(cm, type, cm, line, gutter.className, e); 7672 return e_defaultPrevented(e) 7673 } 7674 } 7675 } 7676 7677 function clickInGutter(cm, e) { 7678 return gutterEvent(cm, e, "gutterClick", true) 7679 } 7680 7681 // CONTEXT MENU HANDLING 7682 7683 // To make the context menu work, we need to briefly unhide the 7684 // textarea (making it as unobtrusive as possible) to let the 7685 // right-click take effect on it. 7686 function onContextMenu(cm, e) { 7687 if (eventInWidget(cm.display, e) || contextMenuInGutter(cm, e)) { return } 7688 if (signalDOMEvent(cm, e, "contextmenu")) { return } 7689 if (!captureRightClick) { cm.display.input.onContextMenu(e); } 7690 } 7691 7692 function contextMenuInGutter(cm, e) { 7693 if (!hasHandler(cm, "gutterContextMenu")) { return false } 7694 return gutterEvent(cm, e, "gutterContextMenu", false) 7695 } 7696 7697 function themeChanged(cm) { 7698 cm.display.wrapper.className = cm.display.wrapper.className.replace(/\s*cm-s-\S+/g, "") + 7699 cm.options.theme.replace(/(^|\s)\s*/g, " cm-s-"); 7700 clearCaches(cm); 7701 } 7702 7703 var Init = {toString: function(){return "CodeMirror.Init"}}; 7704 7705 var defaults = {}; 7706 var optionHandlers = {}; 7707 7708 function defineOptions(CodeMirror) { 7709 var optionHandlers = CodeMirror.optionHandlers; 7710 7711 function option(name, deflt, handle, notOnInit) { 7712 CodeMirror.defaults[name] = deflt; 7713 if (handle) { optionHandlers[name] = 7714 notOnInit ? function (cm, val, old) {if (old != Init) { handle(cm, val, old); }} : handle; } 7715 } 7716 7717 CodeMirror.defineOption = option; 7718 7719 // Passed to option handlers when there is no old value. 7720 CodeMirror.Init = Init; 7721 7722 // These two are, on init, called from the constructor because they 7723 // have to be initialized before the editor can start at all. 7724 option("value", "", function (cm, val) { return cm.setValue(val); }, true); 7725 option("mode", null, function (cm, val) { 7726 cm.doc.modeOption = val; 7727 loadMode(cm); 7728 }, true); 7729 7730 option("indentUnit", 2, loadMode, true); 7731 option("indentWithTabs", false); 7732 option("smartIndent", true); 7733 option("tabSize", 4, function (cm) { 7734 resetModeState(cm); 7735 clearCaches(cm); 7736 regChange(cm); 7737 }, true); 7738 7739 option("lineSeparator", null, function (cm, val) { 7740 cm.doc.lineSep = val; 7741 if (!val) { return } 7742 var newBreaks = [], lineNo = cm.doc.first; 7743 cm.doc.iter(function (line) { 7744 for (var pos = 0;;) { 7745 var found = line.text.indexOf(val, pos); 7746 if (found == -1) { break } 7747 pos = found + val.length; 7748 newBreaks.push(Pos(lineNo, found)); 7749 } 7750 lineNo++; 7751 }); 7752 for (var i = newBreaks.length - 1; i >= 0; i--) 7753 { replaceRange(cm.doc, val, newBreaks[i], Pos(newBreaks[i].line, newBreaks[i].ch + val.length)); } 7754 }); 7755 option("specialChars", /[\u0000-\u001f\u007f-\u009f\u00ad\u061c\u200b\u200e\u200f\u2028\u2029\ufeff\ufff9-\ufffc]/g, function (cm, val, old) { 7756 cm.state.specialChars = new RegExp(val.source + (val.test("\t") ? "" : "|\t"), "g"); 7757 if (old != Init) { cm.refresh(); } 7758 }); 7759 option("specialCharPlaceholder", defaultSpecialCharPlaceholder, function (cm) { return cm.refresh(); }, true); 7760 option("electricChars", true); 7761 option("inputStyle", mobile ? "contenteditable" : "textarea", function () { 7762 throw new Error("inputStyle can not (yet) be changed in a running editor") // FIXME 7763 }, true); 7764 option("spellcheck", false, function (cm, val) { return cm.getInputField().spellcheck = val; }, true); 7765 option("autocorrect", false, function (cm, val) { return cm.getInputField().autocorrect = val; }, true); 7766 option("autocapitalize", false, function (cm, val) { return cm.getInputField().autocapitalize = val; }, true); 7767 option("rtlMoveVisually", !windows); 7768 option("wholeLineUpdateBefore", true); 7769 7770 option("theme", "default", function (cm) { 7771 themeChanged(cm); 7772 updateGutters(cm); 7773 }, true); 7774 option("keyMap", "default", function (cm, val, old) { 7775 var next = getKeyMap(val); 7776 var prev = old != Init && getKeyMap(old); 7777 if (prev && prev.detach) { prev.detach(cm, next); } 7778 if (next.attach) { next.attach(cm, prev || null); } 7779 }); 7780 option("extraKeys", null); 7781 option("configureMouse", null); 7782 7783 option("lineWrapping", false, wrappingChanged, true); 7784 option("gutters", [], function (cm, val) { 7785 cm.display.gutterSpecs = getGutters(val, cm.options.lineNumbers); 7786 updateGutters(cm); 7787 }, true); 7788 option("fixedGutter", true, function (cm, val) { 7789 cm.display.gutters.style.left = val ? compensateForHScroll(cm.display) + "px" : "0"; 7790 cm.refresh(); 7791 }, true); 7792 option("coverGutterNextToScrollbar", false, function (cm) { return updateScrollbars(cm); }, true); 7793 option("scrollbarStyle", "native", function (cm) { 7794 initScrollbars(cm); 7795 updateScrollbars(cm); 7796 cm.display.scrollbars.setScrollTop(cm.doc.scrollTop); 7797 cm.display.scrollbars.setScrollLeft(cm.doc.scrollLeft); 7798 }, true); 7799 option("lineNumbers", false, function (cm, val) { 7800 cm.display.gutterSpecs = getGutters(cm.options.gutters, val); 7801 updateGutters(cm); 7802 }, true); 7803 option("firstLineNumber", 1, updateGutters, true); 7804 option("lineNumberFormatter", function (integer) { return integer; }, updateGutters, true); 7805 option("showCursorWhenSelecting", false, updateSelection, true); 7806 7807 option("resetSelectionOnContextMenu", true); 7808 option("lineWiseCopyCut", true); 7809 option("pasteLinesPerSelection", true); 7810 option("selectionsMayTouch", false); 7811 7812 option("readOnly", false, function (cm, val) { 7813 if (val == "nocursor") { 7814 onBlur(cm); 7815 cm.display.input.blur(); 7816 } 7817 cm.display.input.readOnlyChanged(val); 7818 }); 7819 7820 option("screenReaderLabel", null, function (cm, val) { 7821 val = (val === '') ? null : val; 7822 cm.display.input.screenReaderLabelChanged(val); 7823 }); 7824 7825 option("disableInput", false, function (cm, val) {if (!val) { cm.display.input.reset(); }}, true); 7826 option("dragDrop", true, dragDropChanged); 7827 option("allowDropFileTypes", null); 7828 7829 option("cursorBlinkRate", 530); 7830 option("cursorScrollMargin", 0); 7831 option("cursorHeight", 1, updateSelection, true); 7832 option("singleCursorHeightPerLine", true, updateSelection, true); 7833 option("workTime", 100); 7834 option("workDelay", 100); 7835 option("flattenSpans", true, resetModeState, true); 7836 option("addModeClass", false, resetModeState, true); 7837 option("pollInterval", 100); 7838 option("undoDepth", 200, function (cm, val) { return cm.doc.history.undoDepth = val; }); 7839 option("historyEventDelay", 1250); 7840 option("viewportMargin", 10, function (cm) { return cm.refresh(); }, true); 7841 option("maxHighlightLength", 10000, resetModeState, true); 7842 option("moveInputWithCursor", true, function (cm, val) { 7843 if (!val) { cm.display.input.resetPosition(); } 7844 }); 7845 7846 option("tabindex", null, function (cm, val) { return cm.display.input.getField().tabIndex = val || ""; }); 7847 option("autofocus", null); 7848 option("direction", "ltr", function (cm, val) { return cm.doc.setDirection(val); }, true); 7849 option("phrases", null); 7850 } 7851 7852 function dragDropChanged(cm, value, old) { 7853 var wasOn = old && old != Init; 7854 if (!value != !wasOn) { 7855 var funcs = cm.display.dragFunctions; 7856 var toggle = value ? on : off; 7857 toggle(cm.display.scroller, "dragstart", funcs.start); 7858 toggle(cm.display.scroller, "dragenter", funcs.enter); 7859 toggle(cm.display.scroller, "dragover", funcs.over); 7860 toggle(cm.display.scroller, "dragleave", funcs.leave); 7861 toggle(cm.display.scroller, "drop", funcs.drop); 7862 } 7863 } 7864 7865 function wrappingChanged(cm) { 7866 if (cm.options.lineWrapping) { 7867 addClass(cm.display.wrapper, "CodeMirror-wrap"); 7868 cm.display.sizer.style.minWidth = ""; 7869 cm.display.sizerWidth = null; 7870 } else { 7871 rmClass(cm.display.wrapper, "CodeMirror-wrap"); 7872 findMaxLine(cm); 7873 } 7874 estimateLineHeights(cm); 7875 regChange(cm); 7876 clearCaches(cm); 7877 setTimeout(function () { return updateScrollbars(cm); }, 100); 7878 } 7879 7880 // A CodeMirror instance represents an editor. This is the object 7881 // that user code is usually dealing with. 7882 7883 function CodeMirror(place, options) { 7884 var this$1 = this; 7885 7886 if (!(this instanceof CodeMirror)) { return new CodeMirror(place, options) } 7887 7888 this.options = options = options ? copyObj(options) : {}; 7889 // Determine effective options based on given values and defaults. 7890 copyObj(defaults, options, false); 7891 7892 var doc = options.value; 7893 if (typeof doc == "string") { doc = new Doc(doc, options.mode, null, options.lineSeparator, options.direction); } 7894 else if (options.mode) { doc.modeOption = options.mode; } 7895 this.doc = doc; 7896 7897 var input = new CodeMirror.inputStyles[options.inputStyle](this); 7898 var display = this.display = new Display(place, doc, input, options); 7899 display.wrapper.CodeMirror = this; 7900 themeChanged(this); 7901 if (options.lineWrapping) 7902 { this.display.wrapper.className += " CodeMirror-wrap"; } 7903 initScrollbars(this); 7904 7905 this.state = { 7906 keyMaps: [], // stores maps added by addKeyMap 7907 overlays: [], // highlighting overlays, as added by addOverlay 7908 modeGen: 0, // bumped when mode/overlay changes, used to invalidate highlighting info 7909 overwrite: false, 7910 delayingBlurEvent: false, 7911 focused: false, 7912 suppressEdits: false, // used to disable editing during key handlers when in readOnly mode 7913 pasteIncoming: -1, cutIncoming: -1, // help recognize paste/cut edits in input.poll 7914 selectingText: false, 7915 draggingText: false, 7916 highlight: new Delayed(), // stores highlight worker timeout 7917 keySeq: null, // Unfinished key sequence 7918 specialChars: null 7919 }; 7920 7921 if (options.autofocus && !mobile) { display.input.focus(); } 7922 7923 // Override magic textarea content restore that IE sometimes does 7924 // on our hidden textarea on reload 7925 if (ie && ie_version < 11) { setTimeout(function () { return this$1.display.input.reset(true); }, 20); } 7926 7927 registerEventHandlers(this); 7928 ensureGlobalHandlers(); 7929 7930 startOperation(this); 7931 this.curOp.forceUpdate = true; 7932 attachDoc(this, doc); 7933 7934 if ((options.autofocus && !mobile) || this.hasFocus()) 7935 { setTimeout(function () { 7936 if (this$1.hasFocus() && !this$1.state.focused) { onFocus(this$1); } 7937 }, 20); } 7938 else 7939 { onBlur(this); } 7940 7941 for (var opt in optionHandlers) { if (optionHandlers.hasOwnProperty(opt)) 7942 { optionHandlers[opt](this, options[opt], Init); } } 7943 maybeUpdateLineNumberWidth(this); 7944 if (options.finishInit) { options.finishInit(this); } 7945 for (var i = 0; i < initHooks.length; ++i) { initHooks[i](this); } 7946 endOperation(this); 7947 // Suppress optimizelegibility in Webkit, since it breaks text 7948 // measuring on line wrapping boundaries. 7949 if (webkit && options.lineWrapping && 7950 getComputedStyle(display.lineDiv).textRendering == "optimizelegibility") 7951 { display.lineDiv.style.textRendering = "auto"; } 7952 } 7953 7954 // The default configuration options. 7955 CodeMirror.defaults = defaults; 7956 // Functions to run when options are changed. 7957 CodeMirror.optionHandlers = optionHandlers; 7958 7959 // Attach the necessary event handlers when initializing the editor 7960 function registerEventHandlers(cm) { 7961 var d = cm.display; 7962 on(d.scroller, "mousedown", operation(cm, onMouseDown)); 7963 // Older IE's will not fire a second mousedown for a double click 7964 if (ie && ie_version < 11) 7965 { on(d.scroller, "dblclick", operation(cm, function (e) { 7966 if (signalDOMEvent(cm, e)) { return } 7967 var pos = posFromMouse(cm, e); 7968 if (!pos || clickInGutter(cm, e) || eventInWidget(cm.display, e)) { return } 7969 e_preventDefault(e); 7970 var word = cm.findWordAt(pos); 7971 extendSelection(cm.doc, word.anchor, word.head); 7972 })); } 7973 else 7974 { on(d.scroller, "dblclick", function (e) { return signalDOMEvent(cm, e) || e_preventDefault(e); }); } 7975 // Some browsers fire contextmenu *after* opening the menu, at 7976 // which point we can't mess with it anymore. Context menu is 7977 // handled in onMouseDown for these browsers. 7978 on(d.scroller, "contextmenu", function (e) { return onContextMenu(cm, e); }); 7979 on(d.input.getField(), "contextmenu", function (e) { 7980 if (!d.scroller.contains(e.target)) { onContextMenu(cm, e); } 7981 }); 7982 7983 // Used to suppress mouse event handling when a touch happens 7984 var touchFinished, prevTouch = {end: 0}; 7985 function finishTouch() { 7986 if (d.activeTouch) { 7987 touchFinished = setTimeout(function () { return d.activeTouch = null; }, 1000); 7988 prevTouch = d.activeTouch; 7989 prevTouch.end = +new Date; 7990 } 7991 } 7992 function isMouseLikeTouchEvent(e) { 7993 if (e.touches.length != 1) { return false } 7994 var touch = e.touches[0]; 7995 return touch.radiusX <= 1 && touch.radiusY <= 1 7996 } 7997 function farAway(touch, other) { 7998 if (other.left == null) { return true } 7999 var dx = other.left - touch.left, dy = other.top - touch.top; 8000 return dx * dx + dy * dy > 20 * 20 8001 } 8002 on(d.scroller, "touchstart", function (e) { 8003 if (!signalDOMEvent(cm, e) && !isMouseLikeTouchEvent(e) && !clickInGutter(cm, e)) { 8004 d.input.ensurePolled(); 8005 clearTimeout(touchFinished); 8006 var now = +new Date; 8007 d.activeTouch = {start: now, moved: false, 8008 prev: now - prevTouch.end <= 300 ? prevTouch : null}; 8009 if (e.touches.length == 1) { 8010 d.activeTouch.left = e.touches[0].pageX; 8011 d.activeTouch.top = e.touches[0].pageY; 8012 } 8013 } 8014 }); 8015 on(d.scroller, "touchmove", function () { 8016 if (d.activeTouch) { d.activeTouch.moved = true; } 8017 }); 8018 on(d.scroller, "touchend", function (e) { 8019 var touch = d.activeTouch; 8020 if (touch && !eventInWidget(d, e) && touch.left != null && 8021 !touch.moved && new Date - touch.start < 300) { 8022 var pos = cm.coordsChar(d.activeTouch, "page"), range; 8023 if (!touch.prev || farAway(touch, touch.prev)) // Single tap 8024 { range = new Range(pos, pos); } 8025 else if (!touch.prev.prev || farAway(touch, touch.prev.prev)) // Double tap 8026 { range = cm.findWordAt(pos); } 8027 else // Triple tap 8028 { range = new Range(Pos(pos.line, 0), clipPos(cm.doc, Pos(pos.line + 1, 0))); } 8029 cm.setSelection(range.anchor, range.head); 8030 cm.focus(); 8031 e_preventDefault(e); 8032 } 8033 finishTouch(); 8034 }); 8035 on(d.scroller, "touchcancel", finishTouch); 8036 8037 // Sync scrolling between fake scrollbars and real scrollable 8038 // area, ensure viewport is updated when scrolling. 8039 on(d.scroller, "scroll", function () { 8040 if (d.scroller.clientHeight) { 8041 updateScrollTop(cm, d.scroller.scrollTop); 8042 setScrollLeft(cm, d.scroller.scrollLeft, true); 8043 signal(cm, "scroll", cm); 8044 } 8045 }); 8046 8047 // Listen to wheel events in order to try and update the viewport on time. 8048 on(d.scroller, "mousewheel", function (e) { return onScrollWheel(cm, e); }); 8049 on(d.scroller, "DOMMouseScroll", function (e) { return onScrollWheel(cm, e); }); 8050 8051 // Prevent wrapper from ever scrolling 8052 on(d.wrapper, "scroll", function () { return d.wrapper.scrollTop = d.wrapper.scrollLeft = 0; }); 8053 8054 d.dragFunctions = { 8055 enter: function (e) {if (!signalDOMEvent(cm, e)) { e_stop(e); }}, 8056 over: function (e) {if (!signalDOMEvent(cm, e)) { onDragOver(cm, e); e_stop(e); }}, 8057 start: function (e) { return onDragStart(cm, e); }, 8058 drop: operation(cm, onDrop), 8059 leave: function (e) {if (!signalDOMEvent(cm, e)) { clearDragCursor(cm); }} 8060 }; 8061 8062 var inp = d.input.getField(); 8063 on(inp, "keyup", function (e) { return onKeyUp.call(cm, e); }); 8064 on(inp, "keydown", operation(cm, onKeyDown)); 8065 on(inp, "keypress", operation(cm, onKeyPress)); 8066 on(inp, "focus", function (e) { return onFocus(cm, e); }); 8067 on(inp, "blur", function (e) { return onBlur(cm, e); }); 8068 } 8069 8070 var initHooks = []; 8071 CodeMirror.defineInitHook = function (f) { return initHooks.push(f); }; 8072 8073 // Indent the given line. The how parameter can be "smart", 8074 // "add"/null, "subtract", or "prev". When aggressive is false 8075 // (typically set to true for forced single-line indents), empty 8076 // lines are not indented, and places where the mode returns Pass 8077 // are left alone. 8078 function indentLine(cm, n, how, aggressive) { 8079 var doc = cm.doc, state; 8080 if (how == null) { how = "add"; } 8081 if (how == "smart") { 8082 // Fall back to "prev" when the mode doesn't have an indentation 8083 // method. 8084 if (!doc.mode.indent) { how = "prev"; } 8085 else { state = getContextBefore(cm, n).state; } 8086 } 8087 8088 var tabSize = cm.options.tabSize; 8089 var line = getLine(doc, n), curSpace = countColumn(line.text, null, tabSize); 8090 if (line.stateAfter) { line.stateAfter = null; } 8091 var curSpaceString = line.text.match(/^\s*/)[0], indentation; 8092 if (!aggressive && !/\S/.test(line.text)) { 8093 indentation = 0; 8094 how = "not"; 8095 } else if (how == "smart") { 8096 indentation = doc.mode.indent(state, line.text.slice(curSpaceString.length), line.text); 8097 if (indentation == Pass || indentation > 150) { 8098 if (!aggressive) { return } 8099 how = "prev"; 8100 } 8101 } 8102 if (how == "prev") { 8103 if (n > doc.first) { indentation = countColumn(getLine(doc, n-1).text, null, tabSize); } 8104 else { indentation = 0; } 8105 } else if (how == "add") { 8106 indentation = curSpace + cm.options.indentUnit; 8107 } else if (how == "subtract") { 8108 indentation = curSpace - cm.options.indentUnit; 8109 } else if (typeof how == "number") { 8110 indentation = curSpace + how; 8111 } 8112 indentation = Math.max(0, indentation); 8113 8114 var indentString = "", pos = 0; 8115 if (cm.options.indentWithTabs) 8116 { for (var i = Math.floor(indentation / tabSize); i; --i) {pos += tabSize; indentString += "\t";} } 8117 if (pos < indentation) { indentString += spaceStr(indentation - pos); } 8118 8119 if (indentString != curSpaceString) { 8120 replaceRange(doc, indentString, Pos(n, 0), Pos(n, curSpaceString.length), "+input"); 8121 line.stateAfter = null; 8122 return true 8123 } else { 8124 // Ensure that, if the cursor was in the whitespace at the start 8125 // of the line, it is moved to the end of that space. 8126 for (var i$1 = 0; i$1 < doc.sel.ranges.length; i$1++) { 8127 var range = doc.sel.ranges[i$1]; 8128 if (range.head.line == n && range.head.ch < curSpaceString.length) { 8129 var pos$1 = Pos(n, curSpaceString.length); 8130 replaceOneSelection(doc, i$1, new Range(pos$1, pos$1)); 8131 break 8132 } 8133 } 8134 } 8135 } 8136 8137 // This will be set to a {lineWise: bool, text: [string]} object, so 8138 // that, when pasting, we know what kind of selections the copied 8139 // text was made out of. 8140 var lastCopied = null; 8141 8142 function setLastCopied(newLastCopied) { 8143 lastCopied = newLastCopied; 8144 } 8145 8146 function applyTextInput(cm, inserted, deleted, sel, origin) { 8147 var doc = cm.doc; 8148 cm.display.shift = false; 8149 if (!sel) { sel = doc.sel; } 8150 8151 var recent = +new Date - 200; 8152 var paste = origin == "paste" || cm.state.pasteIncoming > recent; 8153 var textLines = splitLinesAuto(inserted), multiPaste = null; 8154 // When pasting N lines into N selections, insert one line per selection 8155 if (paste && sel.ranges.length > 1) { 8156 if (lastCopied && lastCopied.text.join("\n") == inserted) { 8157 if (sel.ranges.length % lastCopied.text.length == 0) { 8158 multiPaste = []; 8159 for (var i = 0; i < lastCopied.text.length; i++) 8160 { multiPaste.push(doc.splitLines(lastCopied.text[i])); } 8161 } 8162 } else if (textLines.length == sel.ranges.length && cm.options.pasteLinesPerSelection) { 8163 multiPaste = map(textLines, function (l) { return [l]; }); 8164 } 8165 } 8166 8167 var updateInput = cm.curOp.updateInput; 8168 // Normal behavior is to insert the new text into every selection 8169 for (var i$1 = sel.ranges.length - 1; i$1 >= 0; i$1--) { 8170 var range = sel.ranges[i$1]; 8171 var from = range.from(), to = range.to(); 8172 if (range.empty()) { 8173 if (deleted && deleted > 0) // Handle deletion 8174 { from = Pos(from.line, from.ch - deleted); } 8175 else if (cm.state.overwrite && !paste) // Handle overwrite 8176 { to = Pos(to.line, Math.min(getLine(doc, to.line).text.length, to.ch + lst(textLines).length)); } 8177 else if (paste && lastCopied && lastCopied.lineWise && lastCopied.text.join("\n") == textLines.join("\n")) 8178 { from = to = Pos(from.line, 0); } 8179 } 8180 var changeEvent = {from: from, to: to, text: multiPaste ? multiPaste[i$1 % multiPaste.length] : textLines, 8181 origin: origin || (paste ? "paste" : cm.state.cutIncoming > recent ? "cut" : "+input")}; 8182 makeChange(cm.doc, changeEvent); 8183 signalLater(cm, "inputRead", cm, changeEvent); 8184 } 8185 if (inserted && !paste) 8186 { triggerElectric(cm, inserted); } 8187 8188 ensureCursorVisible(cm); 8189 if (cm.curOp.updateInput < 2) { cm.curOp.updateInput = updateInput; } 8190 cm.curOp.typing = true; 8191 cm.state.pasteIncoming = cm.state.cutIncoming = -1; 8192 } 8193 8194 function handlePaste(e, cm) { 8195 var pasted = e.clipboardData && e.clipboardData.getData("Text"); 8196 if (pasted) { 8197 e.preventDefault(); 8198 if (!cm.isReadOnly() && !cm.options.disableInput) 8199 { runInOp(cm, function () { return applyTextInput(cm, pasted, 0, null, "paste"); }); } 8200 return true 8201 } 8202 } 8203 8204 function triggerElectric(cm, inserted) { 8205 // When an 'electric' character is inserted, immediately trigger a reindent 8206 if (!cm.options.electricChars || !cm.options.smartIndent) { return } 8207 var sel = cm.doc.sel; 8208 8209 for (var i = sel.ranges.length - 1; i >= 0; i--) { 8210 var range = sel.ranges[i]; 8211 if (range.head.ch > 100 || (i && sel.ranges[i - 1].head.line == range.head.line)) { continue } 8212 var mode = cm.getModeAt(range.head); 8213 var indented = false; 8214 if (mode.electricChars) { 8215 for (var j = 0; j < mode.electricChars.length; j++) 8216 { if (inserted.indexOf(mode.electricChars.charAt(j)) > -1) { 8217 indented = indentLine(cm, range.head.line, "smart"); 8218 break 8219 } } 8220 } else if (mode.electricInput) { 8221 if (mode.electricInput.test(getLine(cm.doc, range.head.line).text.slice(0, range.head.ch))) 8222 { indented = indentLine(cm, range.head.line, "smart"); } 8223 } 8224 if (indented) { signalLater(cm, "electricInput", cm, range.head.line); } 8225 } 8226 } 8227 8228 function copyableRanges(cm) { 8229 var text = [], ranges = []; 8230 for (var i = 0; i < cm.doc.sel.ranges.length; i++) { 8231 var line = cm.doc.sel.ranges[i].head.line; 8232 var lineRange = {anchor: Pos(line, 0), head: Pos(line + 1, 0)}; 8233 ranges.push(lineRange); 8234 text.push(cm.getRange(lineRange.anchor, lineRange.head)); 8235 } 8236 return {text: text, ranges: ranges} 8237 } 8238 8239 function disableBrowserMagic(field, spellcheck, autocorrect, autocapitalize) { 8240 field.setAttribute("autocorrect", autocorrect ? "" : "off"); 8241 field.setAttribute("autocapitalize", autocapitalize ? "" : "off"); 8242 field.setAttribute("spellcheck", !!spellcheck); 8243 } 8244 8245 function hiddenTextarea() { 8246 var te = elt("textarea", null, null, "position: absolute; bottom: -1em; padding: 0; width: 1px; height: 1em; min-height: 1em; outline: none"); 8247 var div = elt("div", [te], null, "overflow: hidden; position: relative; width: 3px; height: 0px;"); 8248 // The textarea is kept positioned near the cursor to prevent the 8249 // fact that it'll be scrolled into view on input from scrolling 8250 // our fake cursor out of view. On webkit, when wrap=off, paste is 8251 // very slow. So make the area wide instead. 8252 if (webkit) { te.style.width = "1000px"; } 8253 else { te.setAttribute("wrap", "off"); } 8254 // If border: 0; -- iOS fails to open keyboard (issue #1287) 8255 if (ios) { te.style.border = "1px solid black"; } 8256 disableBrowserMagic(te); 8257 return div 8258 } 8259 8260 // The publicly visible API. Note that methodOp(f) means 8261 // 'wrap f in an operation, performed on its `this` parameter'. 8262 8263 // This is not the complete set of editor methods. Most of the 8264 // methods defined on the Doc type are also injected into 8265 // CodeMirror.prototype, for backwards compatibility and 8266 // convenience. 8267 8268 function addEditorMethods(CodeMirror) { 8269 var optionHandlers = CodeMirror.optionHandlers; 8270 8271 var helpers = CodeMirror.helpers = {}; 8272 8273 CodeMirror.prototype = { 8274 constructor: CodeMirror, 8275 focus: function(){window.focus(); this.display.input.focus();}, 8276 8277 setOption: function(option, value) { 8278 var options = this.options, old = options[option]; 8279 if (options[option] == value && option != "mode") { return } 8280 options[option] = value; 8281 if (optionHandlers.hasOwnProperty(option)) 8282 { operation(this, optionHandlers[option])(this, value, old); } 8283 signal(this, "optionChange", this, option); 8284 }, 8285 8286 getOption: function(option) {return this.options[option]}, 8287 getDoc: function() {return this.doc}, 8288 8289 addKeyMap: function(map, bottom) { 8290 this.state.keyMaps[bottom ? "push" : "unshift"](getKeyMap(map)); 8291 }, 8292 removeKeyMap: function(map) { 8293 var maps = this.state.keyMaps; 8294 for (var i = 0; i < maps.length; ++i) 8295 { if (maps[i] == map || maps[i].name == map) { 8296 maps.splice(i, 1); 8297 return true 8298 } } 8299 }, 8300 8301 addOverlay: methodOp(function(spec, options) { 8302 var mode = spec.token ? spec : CodeMirror.getMode(this.options, spec); 8303 if (mode.startState) { throw new Error("Overlays may not be stateful.") } 8304 insertSorted(this.state.overlays, 8305 {mode: mode, modeSpec: spec, opaque: options && options.opaque, 8306 priority: (options && options.priority) || 0}, 8307 function (overlay) { return overlay.priority; }); 8308 this.state.modeGen++; 8309 regChange(this); 8310 }), 8311 removeOverlay: methodOp(function(spec) { 8312 var overlays = this.state.overlays; 8313 for (var i = 0; i < overlays.length; ++i) { 8314 var cur = overlays[i].modeSpec; 8315 if (cur == spec || typeof spec == "string" && cur.name == spec) { 8316 overlays.splice(i, 1); 8317 this.state.modeGen++; 8318 regChange(this); 8319 return 8320 } 8321 } 8322 }), 8323 8324 indentLine: methodOp(function(n, dir, aggressive) { 8325 if (typeof dir != "string" && typeof dir != "number") { 8326 if (dir == null) { dir = this.options.smartIndent ? "smart" : "prev"; } 8327 else { dir = dir ? "add" : "subtract"; } 8328 } 8329 if (isLine(this.doc, n)) { indentLine(this, n, dir, aggressive); } 8330 }), 8331 indentSelection: methodOp(function(how) { 8332 var ranges = this.doc.sel.ranges, end = -1; 8333 for (var i = 0; i < ranges.length; i++) { 8334 var range = ranges[i]; 8335 if (!range.empty()) { 8336 var from = range.from(), to = range.to(); 8337 var start = Math.max(end, from.line); 8338 end = Math.min(this.lastLine(), to.line - (to.ch ? 0 : 1)) + 1; 8339 for (var j = start; j < end; ++j) 8340 { indentLine(this, j, how); } 8341 var newRanges = this.doc.sel.ranges; 8342 if (from.ch == 0 && ranges.length == newRanges.length && newRanges[i].from().ch > 0) 8343 { replaceOneSelection(this.doc, i, new Range(from, newRanges[i].to()), sel_dontScroll); } 8344 } else if (range.head.line > end) { 8345 indentLine(this, range.head.line, how, true); 8346 end = range.head.line; 8347 if (i == this.doc.sel.primIndex) { ensureCursorVisible(this); } 8348 } 8349 } 8350 }), 8351 8352 // Fetch the parser token for a given character. Useful for hacks 8353 // that want to inspect the mode state (say, for completion). 8354 getTokenAt: function(pos, precise) { 8355 return takeToken(this, pos, precise) 8356 }, 8357 8358 getLineTokens: function(line, precise) { 8359 return takeToken(this, Pos(line), precise, true) 8360 }, 8361 8362 getTokenTypeAt: function(pos) { 8363 pos = clipPos(this.doc, pos); 8364 var styles = getLineStyles(this, getLine(this.doc, pos.line)); 8365 var before = 0, after = (styles.length - 1) / 2, ch = pos.ch; 8366 var type; 8367 if (ch == 0) { type = styles[2]; } 8368 else { for (;;) { 8369 var mid = (before + after) >> 1; 8370 if ((mid ? styles[mid * 2 - 1] : 0) >= ch) { after = mid; } 8371 else if (styles[mid * 2 + 1] < ch) { before = mid + 1; } 8372 else { type = styles[mid * 2 + 2]; break } 8373 } } 8374 var cut = type ? type.indexOf("overlay ") : -1; 8375 return cut < 0 ? type : cut == 0 ? null : type.slice(0, cut - 1) 8376 }, 8377 8378 getModeAt: function(pos) { 8379 var mode = this.doc.mode; 8380 if (!mode.innerMode) { return mode } 8381 return CodeMirror.innerMode(mode, this.getTokenAt(pos).state).mode 8382 }, 8383 8384 getHelper: function(pos, type) { 8385 return this.getHelpers(pos, type)[0] 8386 }, 8387 8388 getHelpers: function(pos, type) { 8389 var found = []; 8390 if (!helpers.hasOwnProperty(type)) { return found } 8391 var help = helpers[type], mode = this.getModeAt(pos); 8392 if (typeof mode[type] == "string") { 8393 if (help[mode[type]]) { found.push(help[mode[type]]); } 8394 } else if (mode[type]) { 8395 for (var i = 0; i < mode[type].length; i++) { 8396 var val = help[mode[type][i]]; 8397 if (val) { found.push(val); } 8398 } 8399 } else if (mode.helperType && help[mode.helperType]) { 8400 found.push(help[mode.helperType]); 8401 } else if (help[mode.name]) { 8402 found.push(help[mode.name]); 8403 } 8404 for (var i$1 = 0; i$1 < help._global.length; i$1++) { 8405 var cur = help._global[i$1]; 8406 if (cur.pred(mode, this) && indexOf(found, cur.val) == -1) 8407 { found.push(cur.val); } 8408 } 8409 return found 8410 }, 8411 8412 getStateAfter: function(line, precise) { 8413 var doc = this.doc; 8414 line = clipLine(doc, line == null ? doc.first + doc.size - 1: line); 8415 return getContextBefore(this, line + 1, precise).state 8416 }, 8417 8418 cursorCoords: function(start, mode) { 8419 var pos, range = this.doc.sel.primary(); 8420 if (start == null) { pos = range.head; } 8421 else if (typeof start == "object") { pos = clipPos(this.doc, start); } 8422 else { pos = start ? range.from() : range.to(); } 8423 return cursorCoords(this, pos, mode || "page") 8424 }, 8425 8426 charCoords: function(pos, mode) { 8427 return charCoords(this, clipPos(this.doc, pos), mode || "page") 8428 }, 8429 8430 coordsChar: function(coords, mode) { 8431 coords = fromCoordSystem(this, coords, mode || "page"); 8432 return coordsChar(this, coords.left, coords.top) 8433 }, 8434 8435 lineAtHeight: function(height, mode) { 8436 height = fromCoordSystem(this, {top: height, left: 0}, mode || "page").top; 8437 return lineAtHeight(this.doc, height + this.display.viewOffset) 8438 }, 8439 heightAtLine: function(line, mode, includeWidgets) { 8440 var end = false, lineObj; 8441 if (typeof line == "number") { 8442 var last = this.doc.first + this.doc.size - 1; 8443 if (line < this.doc.first) { line = this.doc.first; } 8444 else if (line > last) { line = last; end = true; } 8445 lineObj = getLine(this.doc, line); 8446 } else { 8447 lineObj = line; 8448 } 8449 return intoCoordSystem(this, lineObj, {top: 0, left: 0}, mode || "page", includeWidgets || end).top + 8450 (end ? this.doc.height - heightAtLine(lineObj) : 0) 8451 }, 8452 8453 defaultTextHeight: function() { return textHeight(this.display) }, 8454 defaultCharWidth: function() { return charWidth(this.display) }, 8455 8456 getViewport: function() { return {from: this.display.viewFrom, to: this.display.viewTo}}, 8457 8458 addWidget: function(pos, node, scroll, vert, horiz) { 8459 var display = this.display; 8460 pos = cursorCoords(this, clipPos(this.doc, pos)); 8461 var top = pos.bottom, left = pos.left; 8462 node.style.position = "absolute"; 8463 node.setAttribute("cm-ignore-events", "true"); 8464 this.display.input.setUneditable(node); 8465 display.sizer.appendChild(node); 8466 if (vert == "over") { 8467 top = pos.top; 8468 } else if (vert == "above" || vert == "near") { 8469 var vspace = Math.max(display.wrapper.clientHeight, this.doc.height), 8470 hspace = Math.max(display.sizer.clientWidth, display.lineSpace.clientWidth); 8471 // Default to positioning above (if specified and possible); otherwise default to positioning below 8472 if ((vert == 'above' || pos.bottom + node.offsetHeight > vspace) && pos.top > node.offsetHeight) 8473 { top = pos.top - node.offsetHeight; } 8474 else if (pos.bottom + node.offsetHeight <= vspace) 8475 { top = pos.bottom; } 8476 if (left + node.offsetWidth > hspace) 8477 { left = hspace - node.offsetWidth; } 8478 } 8479 node.style.top = top + "px"; 8480 node.style.left = node.style.right = ""; 8481 if (horiz == "right") { 8482 left = display.sizer.clientWidth - node.offsetWidth; 8483 node.style.right = "0px"; 8484 } else { 8485 if (horiz == "left") { left = 0; } 8486 else if (horiz == "middle") { left = (display.sizer.clientWidth - node.offsetWidth) / 2; } 8487 node.style.left = left + "px"; 8488 } 8489 if (scroll) 8490 { scrollIntoView(this, {left: left, top: top, right: left + node.offsetWidth, bottom: top + node.offsetHeight}); } 8491 }, 8492 8493 triggerOnKeyDown: methodOp(onKeyDown), 8494 triggerOnKeyPress: methodOp(onKeyPress), 8495 triggerOnKeyUp: onKeyUp, 8496 triggerOnMouseDown: methodOp(onMouseDown), 8497 8498 execCommand: function(cmd) { 8499 if (commands.hasOwnProperty(cmd)) 8500 { return commands[cmd].call(null, this) } 8501 }, 8502 8503 triggerElectric: methodOp(function(text) { triggerElectric(this, text); }), 8504 8505 findPosH: function(from, amount, unit, visually) { 8506 var dir = 1; 8507 if (amount < 0) { dir = -1; amount = -amount; } 8508 var cur = clipPos(this.doc, from); 8509 for (var i = 0; i < amount; ++i) { 8510 cur = findPosH(this.doc, cur, dir, unit, visually); 8511 if (cur.hitSide) { break } 8512 } 8513 return cur 8514 }, 8515 8516 moveH: methodOp(function(dir, unit) { 8517 var this$1 = this; 8518 8519 this.extendSelectionsBy(function (range) { 8520 if (this$1.display.shift || this$1.doc.extend || range.empty()) 8521 { return findPosH(this$1.doc, range.head, dir, unit, this$1.options.rtlMoveVisually) } 8522 else 8523 { return dir < 0 ? range.from() : range.to() } 8524 }, sel_move); 8525 }), 8526 8527 deleteH: methodOp(function(dir, unit) { 8528 var sel = this.doc.sel, doc = this.doc; 8529 if (sel.somethingSelected()) 8530 { doc.replaceSelection("", null, "+delete"); } 8531 else 8532 { deleteNearSelection(this, function (range) { 8533 var other = findPosH(doc, range.head, dir, unit, false); 8534 return dir < 0 ? {from: other, to: range.head} : {from: range.head, to: other} 8535 }); } 8536 }), 8537 8538 findPosV: function(from, amount, unit, goalColumn) { 8539 var dir = 1, x = goalColumn; 8540 if (amount < 0) { dir = -1; amount = -amount; } 8541 var cur = clipPos(this.doc, from); 8542 for (var i = 0; i < amount; ++i) { 8543 var coords = cursorCoords(this, cur, "div"); 8544 if (x == null) { x = coords.left; } 8545 else { coords.left = x; } 8546 cur = findPosV(this, coords, dir, unit); 8547 if (cur.hitSide) { break } 8548 } 8549 return cur 8550 }, 8551 8552 moveV: methodOp(function(dir, unit) { 8553 var this$1 = this; 8554 8555 var doc = this.doc, goals = []; 8556 var collapse = !this.display.shift && !doc.extend && doc.sel.somethingSelected(); 8557 doc.extendSelectionsBy(function (range) { 8558 if (collapse) 8559 { return dir < 0 ? range.from() : range.to() } 8560 var headPos = cursorCoords(this$1, range.head, "div"); 8561 if (range.goalColumn != null) { headPos.left = range.goalColumn; } 8562 goals.push(headPos.left); 8563 var pos = findPosV(this$1, headPos, dir, unit); 8564 if (unit == "page" && range == doc.sel.primary()) 8565 { addToScrollTop(this$1, charCoords(this$1, pos, "div").top - headPos.top); } 8566 return pos 8567 }, sel_move); 8568 if (goals.length) { for (var i = 0; i < doc.sel.ranges.length; i++) 8569 { doc.sel.ranges[i].goalColumn = goals[i]; } } 8570 }), 8571 8572 // Find the word at the given position (as returned by coordsChar). 8573 findWordAt: function(pos) { 8574 var doc = this.doc, line = getLine(doc, pos.line).text; 8575 var start = pos.ch, end = pos.ch; 8576 if (line) { 8577 var helper = this.getHelper(pos, "wordChars"); 8578 if ((pos.sticky == "before" || end == line.length) && start) { --start; } else { ++end; } 8579 var startChar = line.charAt(start); 8580 var check = isWordChar(startChar, helper) 8581 ? function (ch) { return isWordChar(ch, helper); } 8582 : /\s/.test(startChar) ? function (ch) { return /\s/.test(ch); } 8583 : function (ch) { return (!/\s/.test(ch) && !isWordChar(ch)); }; 8584 while (start > 0 && check(line.charAt(start - 1))) { --start; } 8585 while (end < line.length && check(line.charAt(end))) { ++end; } 8586 } 8587 return new Range(Pos(pos.line, start), Pos(pos.line, end)) 8588 }, 8589 8590 toggleOverwrite: function(value) { 8591 if (value != null && value == this.state.overwrite) { return } 8592 if (this.state.overwrite = !this.state.overwrite) 8593 { addClass(this.display.cursorDiv, "CodeMirror-overwrite"); } 8594 else 8595 { rmClass(this.display.cursorDiv, "CodeMirror-overwrite"); } 8596 8597 signal(this, "overwriteToggle", this, this.state.overwrite); 8598 }, 8599 hasFocus: function() { return this.display.input.getField() == activeElt() }, 8600 isReadOnly: function() { return !!(this.options.readOnly || this.doc.cantEdit) }, 8601 8602 scrollTo: methodOp(function (x, y) { scrollToCoords(this, x, y); }), 8603 getScrollInfo: function() { 8604 var scroller = this.display.scroller; 8605 return {left: scroller.scrollLeft, top: scroller.scrollTop, 8606 height: scroller.scrollHeight - scrollGap(this) - this.display.barHeight, 8607 width: scroller.scrollWidth - scrollGap(this) - this.display.barWidth, 8608 clientHeight: displayHeight(this), clientWidth: displayWidth(this)} 8609 }, 8610 8611 scrollIntoView: methodOp(function(range, margin) { 8612 if (range == null) { 8613 range = {from: this.doc.sel.primary().head, to: null}; 8614 if (margin == null) { margin = this.options.cursorScrollMargin; } 8615 } else if (typeof range == "number") { 8616 range = {from: Pos(range, 0), to: null}; 8617 } else if (range.from == null) { 8618 range = {from: range, to: null}; 8619 } 8620 if (!range.to) { range.to = range.from; } 8621 range.margin = margin || 0; 8622 8623 if (range.from.line != null) { 8624 scrollToRange(this, range); 8625 } else { 8626 scrollToCoordsRange(this, range.from, range.to, range.margin); 8627 } 8628 }), 8629 8630 setSize: methodOp(function(width, height) { 8631 var this$1 = this; 8632 8633 var interpret = function (val) { return typeof val == "number" || /^\d+$/.test(String(val)) ? val + "px" : val; }; 8634 if (width != null) { this.display.wrapper.style.width = interpret(width); } 8635 if (height != null) { this.display.wrapper.style.height = interpret(height); } 8636 if (this.options.lineWrapping) { clearLineMeasurementCache(this); } 8637 var lineNo = this.display.viewFrom; 8638 this.doc.iter(lineNo, this.display.viewTo, function (line) { 8639 if (line.widgets) { for (var i = 0; i < line.widgets.length; i++) 8640 { if (line.widgets[i].noHScroll) { regLineChange(this$1, lineNo, "widget"); break } } } 8641 ++lineNo; 8642 }); 8643 this.curOp.forceUpdate = true; 8644 signal(this, "refresh", this); 8645 }), 8646 8647 operation: function(f){return runInOp(this, f)}, 8648 startOperation: function(){return startOperation(this)}, 8649 endOperation: function(){return endOperation(this)}, 8650 8651 refresh: methodOp(function() { 8652 var oldHeight = this.display.cachedTextHeight; 8653 regChange(this); 8654 this.curOp.forceUpdate = true; 8655 clearCaches(this); 8656 scrollToCoords(this, this.doc.scrollLeft, this.doc.scrollTop); 8657 updateGutterSpace(this.display); 8658 if (oldHeight == null || Math.abs(oldHeight - textHeight(this.display)) > .5 || this.options.lineWrapping) 8659 { estimateLineHeights(this); } 8660 signal(this, "refresh", this); 8661 }), 8662 8663 swapDoc: methodOp(function(doc) { 8664 var old = this.doc; 8665 old.cm = null; 8666 // Cancel the current text selection if any (#5821) 8667 if (this.state.selectingText) { this.state.selectingText(); } 8668 attachDoc(this, doc); 8669 clearCaches(this); 8670 this.display.input.reset(); 8671 scrollToCoords(this, doc.scrollLeft, doc.scrollTop); 8672 this.curOp.forceScroll = true; 8673 signalLater(this, "swapDoc", this, old); 8674 return old 8675 }), 8676 8677 phrase: function(phraseText) { 8678 var phrases = this.options.phrases; 8679 return phrases && Object.prototype.hasOwnProperty.call(phrases, phraseText) ? phrases[phraseText] : phraseText 8680 }, 8681 8682 getInputField: function(){return this.display.input.getField()}, 8683 getWrapperElement: function(){return this.display.wrapper}, 8684 getScrollerElement: function(){return this.display.scroller}, 8685 getGutterElement: function(){return this.display.gutters} 8686 }; 8687 eventMixin(CodeMirror); 8688 8689 CodeMirror.registerHelper = function(type, name, value) { 8690 if (!helpers.hasOwnProperty(type)) { helpers[type] = CodeMirror[type] = {_global: []}; } 8691 helpers[type][name] = value; 8692 }; 8693 CodeMirror.registerGlobalHelper = function(type, name, predicate, value) { 8694 CodeMirror.registerHelper(type, name, value); 8695 helpers[type]._global.push({pred: predicate, val: value}); 8696 }; 8697 } 8698 8699 // Used for horizontal relative motion. Dir is -1 or 1 (left or 8700 // right), unit can be "codepoint", "char", "column" (like char, but 8701 // doesn't cross line boundaries), "word" (across next word), or 8702 // "group" (to the start of next group of word or 8703 // non-word-non-whitespace chars). The visually param controls 8704 // whether, in right-to-left text, direction 1 means to move towards 8705 // the next index in the string, or towards the character to the right 8706 // of the current position. The resulting position will have a 8707 // hitSide=true property if it reached the end of the document. 8708 function findPosH(doc, pos, dir, unit, visually) { 8709 var oldPos = pos; 8710 var origDir = dir; 8711 var lineObj = getLine(doc, pos.line); 8712 var lineDir = visually && doc.direction == "rtl" ? -dir : dir; 8713 function findNextLine() { 8714 var l = pos.line + lineDir; 8715 if (l < doc.first || l >= doc.first + doc.size) { return false } 8716 pos = new Pos(l, pos.ch, pos.sticky); 8717 return lineObj = getLine(doc, l) 8718 } 8719 function moveOnce(boundToLine) { 8720 var next; 8721 if (unit == "codepoint") { 8722 var ch = lineObj.text.charCodeAt(pos.ch + (dir > 0 ? 0 : -1)); 8723 if (isNaN(ch)) { 8724 next = null; 8725 } else { 8726 var astral = dir > 0 ? ch >= 0xD800 && ch < 0xDC00 : ch >= 0xDC00 && ch < 0xDFFF; 8727 next = new Pos(pos.line, Math.max(0, Math.min(lineObj.text.length, pos.ch + dir * (astral ? 2 : 1))), -dir); 8728 } 8729 } else if (visually) { 8730 next = moveVisually(doc.cm, lineObj, pos, dir); 8731 } else { 8732 next = moveLogically(lineObj, pos, dir); 8733 } 8734 if (next == null) { 8735 if (!boundToLine && findNextLine()) 8736 { pos = endOfLine(visually, doc.cm, lineObj, pos.line, lineDir); } 8737 else 8738 { return false } 8739 } else { 8740 pos = next; 8741 } 8742 return true 8743 } 8744 8745 if (unit == "char" || unit == "codepoint") { 8746 moveOnce(); 8747 } else if (unit == "column") { 8748 moveOnce(true); 8749 } else if (unit == "word" || unit == "group") { 8750 var sawType = null, group = unit == "group"; 8751 var helper = doc.cm && doc.cm.getHelper(pos, "wordChars"); 8752 for (var first = true;; first = false) { 8753 if (dir < 0 && !moveOnce(!first)) { break } 8754 var cur = lineObj.text.charAt(pos.ch) || "\n"; 8755 var type = isWordChar(cur, helper) ? "w" 8756 : group && cur == "\n" ? "n" 8757 : !group || /\s/.test(cur) ? null 8758 : "p"; 8759 if (group && !first && !type) { type = "s"; } 8760 if (sawType && sawType != type) { 8761 if (dir < 0) {dir = 1; moveOnce(); pos.sticky = "after";} 8762 break 8763 } 8764 8765 if (type) { sawType = type; } 8766 if (dir > 0 && !moveOnce(!first)) { break } 8767 } 8768 } 8769 var result = skipAtomic(doc, pos, oldPos, origDir, true); 8770 if (equalCursorPos(oldPos, result)) { result.hitSide = true; } 8771 return result 8772 } 8773 8774 // For relative vertical movement. Dir may be -1 or 1. Unit can be 8775 // "page" or "line". The resulting position will have a hitSide=true 8776 // property if it reached the end of the document. 8777 function findPosV(cm, pos, dir, unit) { 8778 var doc = cm.doc, x = pos.left, y; 8779 if (unit == "page") { 8780 var pageSize = Math.min(cm.display.wrapper.clientHeight, window.innerHeight || document.documentElement.clientHeight); 8781 var moveAmount = Math.max(pageSize - .5 * textHeight(cm.display), 3); 8782 y = (dir > 0 ? pos.bottom : pos.top) + dir * moveAmount; 8783 8784 } else if (unit == "line") { 8785 y = dir > 0 ? pos.bottom + 3 : pos.top - 3; 8786 } 8787 var target; 8788 for (;;) { 8789 target = coordsChar(cm, x, y); 8790 if (!target.outside) { break } 8791 if (dir < 0 ? y <= 0 : y >= doc.height) { target.hitSide = true; break } 8792 y += dir * 5; 8793 } 8794 return target 8795 } 8796 8797 // CONTENTEDITABLE INPUT STYLE 8798 8799 var ContentEditableInput = function(cm) { 8800 this.cm = cm; 8801 this.lastAnchorNode = this.lastAnchorOffset = this.lastFocusNode = this.lastFocusOffset = null; 8802 this.polling = new Delayed(); 8803 this.composing = null; 8804 this.gracePeriod = false; 8805 this.readDOMTimeout = null; 8806 }; 8807 8808 ContentEditableInput.prototype.init = function (display) { 8809 var this$1 = this; 8810 8811 var input = this, cm = input.cm; 8812 var div = input.div = display.lineDiv; 8813 div.contentEditable = true; 8814 disableBrowserMagic(div, cm.options.spellcheck, cm.options.autocorrect, cm.options.autocapitalize); 8815 8816 function belongsToInput(e) { 8817 for (var t = e.target; t; t = t.parentNode) { 8818 if (t == div) { return true } 8819 if (/\bCodeMirror-(?:line)?widget\b/.test(t.className)) { break } 8820 } 8821 return false 8822 } 8823 8824 on(div, "paste", function (e) { 8825 if (!belongsToInput(e) || signalDOMEvent(cm, e) || handlePaste(e, cm)) { return } 8826 // IE doesn't fire input events, so we schedule a read for the pasted content in this way 8827 if (ie_version <= 11) { setTimeout(operation(cm, function () { return this$1.updateFromDOM(); }), 20); } 8828 }); 8829 8830 on(div, "compositionstart", function (e) { 8831 this$1.composing = {data: e.data, done: false}; 8832 }); 8833 on(div, "compositionupdate", function (e) { 8834 if (!this$1.composing) { this$1.composing = {data: e.data, done: false}; } 8835 }); 8836 on(div, "compositionend", function (e) { 8837 if (this$1.composing) { 8838 if (e.data != this$1.composing.data) { this$1.readFromDOMSoon(); } 8839 this$1.composing.done = true; 8840 } 8841 }); 8842 8843 on(div, "touchstart", function () { return input.forceCompositionEnd(); }); 8844 8845 on(div, "input", function () { 8846 if (!this$1.composing) { this$1.readFromDOMSoon(); } 8847 }); 8848 8849 function onCopyCut(e) { 8850 if (!belongsToInput(e) || signalDOMEvent(cm, e)) { return } 8851 if (cm.somethingSelected()) { 8852 setLastCopied({lineWise: false, text: cm.getSelections()}); 8853 if (e.type == "cut") { cm.replaceSelection("", null, "cut"); } 8854 } else if (!cm.options.lineWiseCopyCut) { 8855 return 8856 } else { 8857 var ranges = copyableRanges(cm); 8858 setLastCopied({lineWise: true, text: ranges.text}); 8859 if (e.type == "cut") { 8860 cm.operation(function () { 8861 cm.setSelections(ranges.ranges, 0, sel_dontScroll); 8862 cm.replaceSelection("", null, "cut"); 8863 }); 8864 } 8865 } 8866 if (e.clipboardData) { 8867 e.clipboardData.clearData(); 8868 var content = lastCopied.text.join("\n"); 8869 // iOS exposes the clipboard API, but seems to discard content inserted into it 8870 e.clipboardData.setData("Text", content); 8871 if (e.clipboardData.getData("Text") == content) { 8872 e.preventDefault(); 8873 return 8874 } 8875 } 8876 // Old-fashioned briefly-focus-a-textarea hack 8877 var kludge = hiddenTextarea(), te = kludge.firstChild; 8878 cm.display.lineSpace.insertBefore(kludge, cm.display.lineSpace.firstChild); 8879 te.value = lastCopied.text.join("\n"); 8880 var hadFocus = activeElt(); 8881 selectInput(te); 8882 setTimeout(function () { 8883 cm.display.lineSpace.removeChild(kludge); 8884 hadFocus.focus(); 8885 if (hadFocus == div) { input.showPrimarySelection(); } 8886 }, 50); 8887 } 8888 on(div, "copy", onCopyCut); 8889 on(div, "cut", onCopyCut); 8890 }; 8891 8892 ContentEditableInput.prototype.screenReaderLabelChanged = function (label) { 8893 // Label for screenreaders, accessibility 8894 if(label) { 8895 this.div.setAttribute('aria-label', label); 8896 } else { 8897 this.div.removeAttribute('aria-label'); 8898 } 8899 }; 8900 8901 ContentEditableInput.prototype.prepareSelection = function () { 8902 var result = prepareSelection(this.cm, false); 8903 result.focus = activeElt() == this.div; 8904 return result 8905 }; 8906 8907 ContentEditableInput.prototype.showSelection = function (info, takeFocus) { 8908 if (!info || !this.cm.display.view.length) { return } 8909 if (info.focus || takeFocus) { this.showPrimarySelection(); } 8910 this.showMultipleSelections(info); 8911 }; 8912 8913 ContentEditableInput.prototype.getSelection = function () { 8914 return this.cm.display.wrapper.ownerDocument.getSelection() 8915 }; 8916 8917 ContentEditableInput.prototype.showPrimarySelection = function () { 8918 var sel = this.getSelection(), cm = this.cm, prim = cm.doc.sel.primary(); 8919 var from = prim.from(), to = prim.to(); 8920 8921 if (cm.display.viewTo == cm.display.viewFrom || from.line >= cm.display.viewTo || to.line < cm.display.viewFrom) { 8922 sel.removeAllRanges(); 8923 return 8924 } 8925 8926 var curAnchor = domToPos(cm, sel.anchorNode, sel.anchorOffset); 8927 var curFocus = domToPos(cm, sel.focusNode, sel.focusOffset); 8928 if (curAnchor && !curAnchor.bad && curFocus && !curFocus.bad && 8929 cmp(minPos(curAnchor, curFocus), from) == 0 && 8930 cmp(maxPos(curAnchor, curFocus), to) == 0) 8931 { return } 8932 8933 var view = cm.display.view; 8934 var start = (from.line >= cm.display.viewFrom && posToDOM(cm, from)) || 8935 {node: view[0].measure.map[2], offset: 0}; 8936 var end = to.line < cm.display.viewTo && posToDOM(cm, to); 8937 if (!end) { 8938 var measure = view[view.length - 1].measure; 8939 var map = measure.maps ? measure.maps[measure.maps.length - 1] : measure.map; 8940 end = {node: map[map.length - 1], offset: map[map.length - 2] - map[map.length - 3]}; 8941 } 8942 8943 if (!start || !end) { 8944 sel.removeAllRanges(); 8945 return 8946 } 8947 8948 var old = sel.rangeCount && sel.getRangeAt(0), rng; 8949 try { rng = range(start.node, start.offset, end.offset, end.node); } 8950 catch(e) {} // Our model of the DOM might be outdated, in which case the range we try to set can be impossible 8951 if (rng) { 8952 if (!gecko && cm.state.focused) { 8953 sel.collapse(start.node, start.offset); 8954 if (!rng.collapsed) { 8955 sel.removeAllRanges(); 8956 sel.addRange(rng); 8957 } 8958 } else { 8959 sel.removeAllRanges(); 8960 sel.addRange(rng); 8961 } 8962 if (old && sel.anchorNode == null) { sel.addRange(old); } 8963 else if (gecko) { this.startGracePeriod(); } 8964 } 8965 this.rememberSelection(); 8966 }; 8967 8968 ContentEditableInput.prototype.startGracePeriod = function () { 8969 var this$1 = this; 8970 8971 clearTimeout(this.gracePeriod); 8972 this.gracePeriod = setTimeout(function () { 8973 this$1.gracePeriod = false; 8974 if (this$1.selectionChanged()) 8975 { this$1.cm.operation(function () { return this$1.cm.curOp.selectionChanged = true; }); } 8976 }, 20); 8977 }; 8978 8979 ContentEditableInput.prototype.showMultipleSelections = function (info) { 8980 removeChildrenAndAdd(this.cm.display.cursorDiv, info.cursors); 8981 removeChildrenAndAdd(this.cm.display.selectionDiv, info.selection); 8982 }; 8983 8984 ContentEditableInput.prototype.rememberSelection = function () { 8985 var sel = this.getSelection(); 8986 this.lastAnchorNode = sel.anchorNode; this.lastAnchorOffset = sel.anchorOffset; 8987 this.lastFocusNode = sel.focusNode; this.lastFocusOffset = sel.focusOffset; 8988 }; 8989 8990 ContentEditableInput.prototype.selectionInEditor = function () { 8991 var sel = this.getSelection(); 8992 if (!sel.rangeCount) { return false } 8993 var node = sel.getRangeAt(0).commonAncestorContainer; 8994 return contains(this.div, node) 8995 }; 8996 8997 ContentEditableInput.prototype.focus = function () { 8998 if (this.cm.options.readOnly != "nocursor") { 8999 if (!this.selectionInEditor() || activeElt() != this.div) 9000 { this.showSelection(this.prepareSelection(), true); } 9001 this.div.focus(); 9002 } 9003 }; 9004 ContentEditableInput.prototype.blur = function () { this.div.blur(); }; 9005 ContentEditableInput.prototype.getField = function () { return this.div }; 9006 9007 ContentEditableInput.prototype.supportsTouch = function () { return true }; 9008 9009 ContentEditableInput.prototype.receivedFocus = function () { 9010 var this$1 = this; 9011 9012 var input = this; 9013 if (this.selectionInEditor()) 9014 { setTimeout(function () { return this$1.pollSelection(); }, 20); } 9015 else 9016 { runInOp(this.cm, function () { return input.cm.curOp.selectionChanged = true; }); } 9017 9018 function poll() { 9019 if (input.cm.state.focused) { 9020 input.pollSelection(); 9021 input.polling.set(input.cm.options.pollInterval, poll); 9022 } 9023 } 9024 this.polling.set(this.cm.options.pollInterval, poll); 9025 }; 9026 9027 ContentEditableInput.prototype.selectionChanged = function () { 9028 var sel = this.getSelection(); 9029 return sel.anchorNode != this.lastAnchorNode || sel.anchorOffset != this.lastAnchorOffset || 9030 sel.focusNode != this.lastFocusNode || sel.focusOffset != this.lastFocusOffset 9031 }; 9032 9033 ContentEditableInput.prototype.pollSelection = function () { 9034 if (this.readDOMTimeout != null || this.gracePeriod || !this.selectionChanged()) { return } 9035 var sel = this.getSelection(), cm = this.cm; 9036 // On Android Chrome (version 56, at least), backspacing into an 9037 // uneditable block element will put the cursor in that element, 9038 // and then, because it's not editable, hide the virtual keyboard. 9039 // Because Android doesn't allow us to actually detect backspace 9040 // presses in a sane way, this code checks for when that happens 9041 // and simulates a backspace press in this case. 9042 if (android && chrome && this.cm.display.gutterSpecs.length && isInGutter(sel.anchorNode)) { 9043 this.cm.triggerOnKeyDown({type: "keydown", keyCode: 8, preventDefault: Math.abs}); 9044 this.blur(); 9045 this.focus(); 9046 return 9047 } 9048 if (this.composing) { return } 9049 this.rememberSelection(); 9050 var anchor = domToPos(cm, sel.anchorNode, sel.anchorOffset); 9051 var head = domToPos(cm, sel.focusNode, sel.focusOffset); 9052 if (anchor && head) { runInOp(cm, function () { 9053 setSelection(cm.doc, simpleSelection(anchor, head), sel_dontScroll); 9054 if (anchor.bad || head.bad) { cm.curOp.selectionChanged = true; } 9055 }); } 9056 }; 9057 9058 ContentEditableInput.prototype.pollContent = function () { 9059 if (this.readDOMTimeout != null) { 9060 clearTimeout(this.readDOMTimeout); 9061 this.readDOMTimeout = null; 9062 } 9063 9064 var cm = this.cm, display = cm.display, sel = cm.doc.sel.primary(); 9065 var from = sel.from(), to = sel.to(); 9066 if (from.ch == 0 && from.line > cm.firstLine()) 9067 { from = Pos(from.line - 1, getLine(cm.doc, from.line - 1).length); } 9068 if (to.ch == getLine(cm.doc, to.line).text.length && to.line < cm.lastLine()) 9069 { to = Pos(to.line + 1, 0); } 9070 if (from.line < display.viewFrom || to.line > display.viewTo - 1) { return false } 9071 9072 var fromIndex, fromLine, fromNode; 9073 if (from.line == display.viewFrom || (fromIndex = findViewIndex(cm, from.line)) == 0) { 9074 fromLine = lineNo(display.view[0].line); 9075 fromNode = display.view[0].node; 9076 } else { 9077 fromLine = lineNo(display.view[fromIndex].line); 9078 fromNode = display.view[fromIndex - 1].node.nextSibling; 9079 } 9080 var toIndex = findViewIndex(cm, to.line); 9081 var toLine, toNode; 9082 if (toIndex == display.view.length - 1) { 9083 toLine = display.viewTo - 1; 9084 toNode = display.lineDiv.lastChild; 9085 } else { 9086 toLine = lineNo(display.view[toIndex + 1].line) - 1; 9087 toNode = display.view[toIndex + 1].node.previousSibling; 9088 } 9089 9090 if (!fromNode) { return false } 9091 var newText = cm.doc.splitLines(domTextBetween(cm, fromNode, toNode, fromLine, toLine)); 9092 var oldText = getBetween(cm.doc, Pos(fromLine, 0), Pos(toLine, getLine(cm.doc, toLine).text.length)); 9093 while (newText.length > 1 && oldText.length > 1) { 9094 if (lst(newText) == lst(oldText)) { newText.pop(); oldText.pop(); toLine--; } 9095 else if (newText[0] == oldText[0]) { newText.shift(); oldText.shift(); fromLine++; } 9096 else { break } 9097 } 9098 9099 var cutFront = 0, cutEnd = 0; 9100 var newTop = newText[0], oldTop = oldText[0], maxCutFront = Math.min(newTop.length, oldTop.length); 9101 while (cutFront < maxCutFront && newTop.charCodeAt(cutFront) == oldTop.charCodeAt(cutFront)) 9102 { ++cutFront; } 9103 var newBot = lst(newText), oldBot = lst(oldText); 9104 var maxCutEnd = Math.min(newBot.length - (newText.length == 1 ? cutFront : 0), 9105 oldBot.length - (oldText.length == 1 ? cutFront : 0)); 9106 while (cutEnd < maxCutEnd && 9107 newBot.charCodeAt(newBot.length - cutEnd - 1) == oldBot.charCodeAt(oldBot.length - cutEnd - 1)) 9108 { ++cutEnd; } 9109 // Try to move start of change to start of selection if ambiguous 9110 if (newText.length == 1 && oldText.length == 1 && fromLine == from.line) { 9111 while (cutFront && cutFront > from.ch && 9112 newBot.charCodeAt(newBot.length - cutEnd - 1) == oldBot.charCodeAt(oldBot.length - cutEnd - 1)) { 9113 cutFront--; 9114 cutEnd++; 9115 } 9116 } 9117 9118 newText[newText.length - 1] = newBot.slice(0, newBot.length - cutEnd).replace(/^\u200b+/, ""); 9119 newText[0] = newText[0].slice(cutFront).replace(/\u200b+$/, ""); 9120 9121 var chFrom = Pos(fromLine, cutFront); 9122 var chTo = Pos(toLine, oldText.length ? lst(oldText).length - cutEnd : 0); 9123 if (newText.length > 1 || newText[0] || cmp(chFrom, chTo)) { 9124 replaceRange(cm.doc, newText, chFrom, chTo, "+input"); 9125 return true 9126 } 9127 }; 9128 9129 ContentEditableInput.prototype.ensurePolled = function () { 9130 this.forceCompositionEnd(); 9131 }; 9132 ContentEditableInput.prototype.reset = function () { 9133 this.forceCompositionEnd(); 9134 }; 9135 ContentEditableInput.prototype.forceCompositionEnd = function () { 9136 if (!this.composing) { return } 9137 clearTimeout(this.readDOMTimeout); 9138 this.composing = null; 9139 this.updateFromDOM(); 9140 this.div.blur(); 9141 this.div.focus(); 9142 }; 9143 ContentEditableInput.prototype.readFromDOMSoon = function () { 9144 var this$1 = this; 9145 9146 if (this.readDOMTimeout != null) { return } 9147 this.readDOMTimeout = setTimeout(function () { 9148 this$1.readDOMTimeout = null; 9149 if (this$1.composing) { 9150 if (this$1.composing.done) { this$1.composing = null; } 9151 else { return } 9152 } 9153 this$1.updateFromDOM(); 9154 }, 80); 9155 }; 9156 9157 ContentEditableInput.prototype.updateFromDOM = function () { 9158 var this$1 = this; 9159 9160 if (this.cm.isReadOnly() || !this.pollContent()) 9161 { runInOp(this.cm, function () { return regChange(this$1.cm); }); } 9162 }; 9163 9164 ContentEditableInput.prototype.setUneditable = function (node) { 9165 node.contentEditable = "false"; 9166 }; 9167 9168 ContentEditableInput.prototype.onKeyPress = function (e) { 9169 if (e.charCode == 0 || this.composing) { return } 9170 e.preventDefault(); 9171 if (!this.cm.isReadOnly()) 9172 { operation(this.cm, applyTextInput)(this.cm, String.fromCharCode(e.charCode == null ? e.keyCode : e.charCode), 0); } 9173 }; 9174 9175 ContentEditableInput.prototype.readOnlyChanged = function (val) { 9176 this.div.contentEditable = String(val != "nocursor"); 9177 }; 9178 9179 ContentEditableInput.prototype.onContextMenu = function () {}; 9180 ContentEditableInput.prototype.resetPosition = function () {}; 9181 9182 ContentEditableInput.prototype.needsContentAttribute = true; 9183 9184 function posToDOM(cm, pos) { 9185 var view = findViewForLine(cm, pos.line); 9186 if (!view || view.hidden) { return null } 9187 var line = getLine(cm.doc, pos.line); 9188 var info = mapFromLineView(view, line, pos.line); 9189 9190 var order = getOrder(line, cm.doc.direction), side = "left"; 9191 if (order) { 9192 var partPos = getBidiPartAt(order, pos.ch); 9193 side = partPos % 2 ? "right" : "left"; 9194 } 9195 var result = nodeAndOffsetInLineMap(info.map, pos.ch, side); 9196 result.offset = result.collapse == "right" ? result.end : result.start; 9197 return result 9198 } 9199 9200 function isInGutter(node) { 9201 for (var scan = node; scan; scan = scan.parentNode) 9202 { if (/CodeMirror-gutter-wrapper/.test(scan.className)) { return true } } 9203 return false 9204 } 9205 9206 function badPos(pos, bad) { if (bad) { pos.bad = true; } return pos } 9207 9208 function domTextBetween(cm, from, to, fromLine, toLine) { 9209 var text = "", closing = false, lineSep = cm.doc.lineSeparator(), extraLinebreak = false; 9210 function recognizeMarker(id) { return function (marker) { return marker.id == id; } } 9211 function close() { 9212 if (closing) { 9213 text += lineSep; 9214 if (extraLinebreak) { text += lineSep; } 9215 closing = extraLinebreak = false; 9216 } 9217 } 9218 function addText(str) { 9219 if (str) { 9220 close(); 9221 text += str; 9222 } 9223 } 9224 function walk(node) { 9225 if (node.nodeType == 1) { 9226 var cmText = node.getAttribute("cm-text"); 9227 if (cmText) { 9228 addText(cmText); 9229 return 9230 } 9231 var markerID = node.getAttribute("cm-marker"), range; 9232 if (markerID) { 9233 var found = cm.findMarks(Pos(fromLine, 0), Pos(toLine + 1, 0), recognizeMarker(+markerID)); 9234 if (found.length && (range = found[0].find(0))) 9235 { addText(getBetween(cm.doc, range.from, range.to).join(lineSep)); } 9236 return 9237 } 9238 if (node.getAttribute("contenteditable") == "false") { return } 9239 var isBlock = /^(pre|div|p|li|table|br)$/i.test(node.nodeName); 9240 if (!/^br$/i.test(node.nodeName) && node.textContent.length == 0) { return } 9241 9242 if (isBlock) { close(); } 9243 for (var i = 0; i < node.childNodes.length; i++) 9244 { walk(node.childNodes[i]); } 9245 9246 if (/^(pre|p)$/i.test(node.nodeName)) { extraLinebreak = true; } 9247 if (isBlock) { closing = true; } 9248 } else if (node.nodeType == 3) { 9249 addText(node.nodeValue.replace(/\u200b/g, "").replace(/\u00a0/g, " ")); 9250 } 9251 } 9252 for (;;) { 9253 walk(from); 9254 if (from == to) { break } 9255 from = from.nextSibling; 9256 extraLinebreak = false; 9257 } 9258 return text 9259 } 9260 9261 function domToPos(cm, node, offset) { 9262 var lineNode; 9263 if (node == cm.display.lineDiv) { 9264 lineNode = cm.display.lineDiv.childNodes[offset]; 9265 if (!lineNode) { return badPos(cm.clipPos(Pos(cm.display.viewTo - 1)), true) } 9266 node = null; offset = 0; 9267 } else { 9268 for (lineNode = node;; lineNode = lineNode.parentNode) { 9269 if (!lineNode || lineNode == cm.display.lineDiv) { return null } 9270 if (lineNode.parentNode && lineNode.parentNode == cm.display.lineDiv) { break } 9271 } 9272 } 9273 for (var i = 0; i < cm.display.view.length; i++) { 9274 var lineView = cm.display.view[i]; 9275 if (lineView.node == lineNode) 9276 { return locateNodeInLineView(lineView, node, offset) } 9277 } 9278 } 9279 9280 function locateNodeInLineView(lineView, node, offset) { 9281 var wrapper = lineView.text.firstChild, bad = false; 9282 if (!node || !contains(wrapper, node)) { return badPos(Pos(lineNo(lineView.line), 0), true) } 9283 if (node == wrapper) { 9284 bad = true; 9285 node = wrapper.childNodes[offset]; 9286 offset = 0; 9287 if (!node) { 9288 var line = lineView.rest ? lst(lineView.rest) : lineView.line; 9289 return badPos(Pos(lineNo(line), line.text.length), bad) 9290 } 9291 } 9292 9293 var textNode = node.nodeType == 3 ? node : null, topNode = node; 9294 if (!textNode && node.childNodes.length == 1 && node.firstChild.nodeType == 3) { 9295 textNode = node.firstChild; 9296 if (offset) { offset = textNode.nodeValue.length; } 9297 } 9298 while (topNode.parentNode != wrapper) { topNode = topNode.parentNode; } 9299 var measure = lineView.measure, maps = measure.maps; 9300 9301 function find(textNode, topNode, offset) { 9302 for (var i = -1; i < (maps ? maps.length : 0); i++) { 9303 var map = i < 0 ? measure.map : maps[i]; 9304 for (var j = 0; j < map.length; j += 3) { 9305 var curNode = map[j + 2]; 9306 if (curNode == textNode || curNode == topNode) { 9307 var line = lineNo(i < 0 ? lineView.line : lineView.rest[i]); 9308 var ch = map[j] + offset; 9309 if (offset < 0 || curNode != textNode) { ch = map[j + (offset ? 1 : 0)]; } 9310 return Pos(line, ch) 9311 } 9312 } 9313 } 9314 } 9315 var found = find(textNode, topNode, offset); 9316 if (found) { return badPos(found, bad) } 9317 9318 // FIXME this is all really shaky. might handle the few cases it needs to handle, but likely to cause problems 9319 for (var after = topNode.nextSibling, dist = textNode ? textNode.nodeValue.length - offset : 0; after; after = after.nextSibling) { 9320 found = find(after, after.firstChild, 0); 9321 if (found) 9322 { return badPos(Pos(found.line, found.ch - dist), bad) } 9323 else 9324 { dist += after.textContent.length; } 9325 } 9326 for (var before = topNode.previousSibling, dist$1 = offset; before; before = before.previousSibling) { 9327 found = find(before, before.firstChild, -1); 9328 if (found) 9329 { return badPos(Pos(found.line, found.ch + dist$1), bad) } 9330 else 9331 { dist$1 += before.textContent.length; } 9332 } 9333 } 9334 9335 // TEXTAREA INPUT STYLE 9336 9337 var TextareaInput = function(cm) { 9338 this.cm = cm; 9339 // See input.poll and input.reset 9340 this.prevInput = ""; 9341 9342 // Flag that indicates whether we expect input to appear real soon 9343 // now (after some event like 'keypress' or 'input') and are 9344 // polling intensively. 9345 this.pollingFast = false; 9346 // Self-resetting timeout for the poller 9347 this.polling = new Delayed(); 9348 // Used to work around IE issue with selection being forgotten when focus moves away from textarea 9349 this.hasSelection = false; 9350 this.composing = null; 9351 }; 9352 9353 TextareaInput.prototype.init = function (display) { 9354 var this$1 = this; 9355 9356 var input = this, cm = this.cm; 9357 this.createField(display); 9358 var te = this.textarea; 9359 9360 display.wrapper.insertBefore(this.wrapper, display.wrapper.firstChild); 9361 9362 // Needed to hide big blue blinking cursor on Mobile Safari (doesn't seem to work in iOS 8 anymore) 9363 if (ios) { te.style.width = "0px"; } 9364 9365 on(te, "input", function () { 9366 if (ie && ie_version >= 9 && this$1.hasSelection) { this$1.hasSelection = null; } 9367 input.poll(); 9368 }); 9369 9370 on(te, "paste", function (e) { 9371 if (signalDOMEvent(cm, e) || handlePaste(e, cm)) { return } 9372 9373 cm.state.pasteIncoming = +new Date; 9374 input.fastPoll(); 9375 }); 9376 9377 function prepareCopyCut(e) { 9378 if (signalDOMEvent(cm, e)) { return } 9379 if (cm.somethingSelected()) { 9380 setLastCopied({lineWise: false, text: cm.getSelections()}); 9381 } else if (!cm.options.lineWiseCopyCut) { 9382 return 9383 } else { 9384 var ranges = copyableRanges(cm); 9385 setLastCopied({lineWise: true, text: ranges.text}); 9386 if (e.type == "cut") { 9387 cm.setSelections(ranges.ranges, null, sel_dontScroll); 9388 } else { 9389 input.prevInput = ""; 9390 te.value = ranges.text.join("\n"); 9391 selectInput(te); 9392 } 9393 } 9394 if (e.type == "cut") { cm.state.cutIncoming = +new Date; } 9395 } 9396 on(te, "cut", prepareCopyCut); 9397 on(te, "copy", prepareCopyCut); 9398 9399 on(display.scroller, "paste", function (e) { 9400 if (eventInWidget(display, e) || signalDOMEvent(cm, e)) { return } 9401 if (!te.dispatchEvent) { 9402 cm.state.pasteIncoming = +new Date; 9403 input.focus(); 9404 return 9405 } 9406 9407 // Pass the `paste` event to the textarea so it's handled by its event listener. 9408 var event = new Event("paste"); 9409 event.clipboardData = e.clipboardData; 9410 te.dispatchEvent(event); 9411 }); 9412 9413 // Prevent normal selection in the editor (we handle our own) 9414 on(display.lineSpace, "selectstart", function (e) { 9415 if (!eventInWidget(display, e)) { e_preventDefault(e); } 9416 }); 9417 9418 on(te, "compositionstart", function () { 9419 var start = cm.getCursor("from"); 9420 if (input.composing) { input.composing.range.clear(); } 9421 input.composing = { 9422 start: start, 9423 range: cm.markText(start, cm.getCursor("to"), {className: "CodeMirror-composing"}) 9424 }; 9425 }); 9426 on(te, "compositionend", function () { 9427 if (input.composing) { 9428 input.poll(); 9429 input.composing.range.clear(); 9430 input.composing = null; 9431 } 9432 }); 9433 }; 9434 9435 TextareaInput.prototype.createField = function (_display) { 9436 // Wraps and hides input textarea 9437 this.wrapper = hiddenTextarea(); 9438 // The semihidden textarea that is focused when the editor is 9439 // focused, and receives input. 9440 this.textarea = this.wrapper.firstChild; 9441 }; 9442 9443 TextareaInput.prototype.screenReaderLabelChanged = function (label) { 9444 // Label for screenreaders, accessibility 9445 if(label) { 9446 this.textarea.setAttribute('aria-label', label); 9447 } else { 9448 this.textarea.removeAttribute('aria-label'); 9449 } 9450 }; 9451 9452 TextareaInput.prototype.prepareSelection = function () { 9453 // Redraw the selection and/or cursor 9454 var cm = this.cm, display = cm.display, doc = cm.doc; 9455 var result = prepareSelection(cm); 9456 9457 // Move the hidden textarea near the cursor to prevent scrolling artifacts 9458 if (cm.options.moveInputWithCursor) { 9459 var headPos = cursorCoords(cm, doc.sel.primary().head, "div"); 9460 var wrapOff = display.wrapper.getBoundingClientRect(), lineOff = display.lineDiv.getBoundingClientRect(); 9461 result.teTop = Math.max(0, Math.min(display.wrapper.clientHeight - 10, 9462 headPos.top + lineOff.top - wrapOff.top)); 9463 result.teLeft = Math.max(0, Math.min(display.wrapper.clientWidth - 10, 9464 headPos.left + lineOff.left - wrapOff.left)); 9465 } 9466 9467 return result 9468 }; 9469 9470 TextareaInput.prototype.showSelection = function (drawn) { 9471 var cm = this.cm, display = cm.display; 9472 removeChildrenAndAdd(display.cursorDiv, drawn.cursors); 9473 removeChildrenAndAdd(display.selectionDiv, drawn.selection); 9474 if (drawn.teTop != null) { 9475 this.wrapper.style.top = drawn.teTop + "px"; 9476 this.wrapper.style.left = drawn.teLeft + "px"; 9477 } 9478 }; 9479 9480 // Reset the input to correspond to the selection (or to be empty, 9481 // when not typing and nothing is selected) 9482 TextareaInput.prototype.reset = function (typing) { 9483 if (this.contextMenuPending || this.composing) { return } 9484 var cm = this.cm; 9485 if (cm.somethingSelected()) { 9486 this.prevInput = ""; 9487 var content = cm.getSelection(); 9488 this.textarea.value = content; 9489 if (cm.state.focused) { selectInput(this.textarea); } 9490 if (ie && ie_version >= 9) { this.hasSelection = content; } 9491 } else if (!typing) { 9492 this.prevInput = this.textarea.value = ""; 9493 if (ie && ie_version >= 9) { this.hasSelection = null; } 9494 } 9495 }; 9496 9497 TextareaInput.prototype.getField = function () { return this.textarea }; 9498 9499 TextareaInput.prototype.supportsTouch = function () { return false }; 9500 9501 TextareaInput.prototype.focus = function () { 9502 if (this.cm.options.readOnly != "nocursor" && (!mobile || activeElt() != this.textarea)) { 9503 try { this.textarea.focus(); } 9504 catch (e) {} // IE8 will throw if the textarea is display: none or not in DOM 9505 } 9506 }; 9507 9508 TextareaInput.prototype.blur = function () { this.textarea.blur(); }; 9509 9510 TextareaInput.prototype.resetPosition = function () { 9511 this.wrapper.style.top = this.wrapper.style.left = 0; 9512 }; 9513 9514 TextareaInput.prototype.receivedFocus = function () { this.slowPoll(); }; 9515 9516 // Poll for input changes, using the normal rate of polling. This 9517 // runs as long as the editor is focused. 9518 TextareaInput.prototype.slowPoll = function () { 9519 var this$1 = this; 9520 9521 if (this.pollingFast) { return } 9522 this.polling.set(this.cm.options.pollInterval, function () { 9523 this$1.poll(); 9524 if (this$1.cm.state.focused) { this$1.slowPoll(); } 9525 }); 9526 }; 9527 9528 // When an event has just come in that is likely to add or change 9529 // something in the input textarea, we poll faster, to ensure that 9530 // the change appears on the screen quickly. 9531 TextareaInput.prototype.fastPoll = function () { 9532 var missed = false, input = this; 9533 input.pollingFast = true; 9534 function p() { 9535 var changed = input.poll(); 9536 if (!changed && !missed) {missed = true; input.polling.set(60, p);} 9537 else {input.pollingFast = false; input.slowPoll();} 9538 } 9539 input.polling.set(20, p); 9540 }; 9541 9542 // Read input from the textarea, and update the document to match. 9543 // When something is selected, it is present in the textarea, and 9544 // selected (unless it is huge, in which case a placeholder is 9545 // used). When nothing is selected, the cursor sits after previously 9546 // seen text (can be empty), which is stored in prevInput (we must 9547 // not reset the textarea when typing, because that breaks IME). 9548 TextareaInput.prototype.poll = function () { 9549 var this$1 = this; 9550 9551 var cm = this.cm, input = this.textarea, prevInput = this.prevInput; 9552 // Since this is called a *lot*, try to bail out as cheaply as 9553 // possible when it is clear that nothing happened. hasSelection 9554 // will be the case when there is a lot of text in the textarea, 9555 // in which case reading its value would be expensive. 9556 if (this.contextMenuPending || !cm.state.focused || 9557 (hasSelection(input) && !prevInput && !this.composing) || 9558 cm.isReadOnly() || cm.options.disableInput || cm.state.keySeq) 9559 { return false } 9560 9561 var text = input.value; 9562 // If nothing changed, bail. 9563 if (text == prevInput && !cm.somethingSelected()) { return false } 9564 // Work around nonsensical selection resetting in IE9/10, and 9565 // inexplicable appearance of private area unicode characters on 9566 // some key combos in Mac (#2689). 9567 if (ie && ie_version >= 9 && this.hasSelection === text || 9568 mac && /[\uf700-\uf7ff]/.test(text)) { 9569 cm.display.input.reset(); 9570 return false 9571 } 9572 9573 if (cm.doc.sel == cm.display.selForContextMenu) { 9574 var first = text.charCodeAt(0); 9575 if (first == 0x200b && !prevInput) { prevInput = "\u200b"; } 9576 if (first == 0x21da) { this.reset(); return this.cm.execCommand("undo") } 9577 } 9578 // Find the part of the input that is actually new 9579 var same = 0, l = Math.min(prevInput.length, text.length); 9580 while (same < l && prevInput.charCodeAt(same) == text.charCodeAt(same)) { ++same; } 9581 9582 runInOp(cm, function () { 9583 applyTextInput(cm, text.slice(same), prevInput.length - same, 9584 null, this$1.composing ? "*compose" : null); 9585 9586 // Don't leave long text in the textarea, since it makes further polling slow 9587 if (text.length > 1000 || text.indexOf("\n") > -1) { input.value = this$1.prevInput = ""; } 9588 else { this$1.prevInput = text; } 9589 9590 if (this$1.composing) { 9591 this$1.composing.range.clear(); 9592 this$1.composing.range = cm.markText(this$1.composing.start, cm.getCursor("to"), 9593 {className: "CodeMirror-composing"}); 9594 } 9595 }); 9596 return true 9597 }; 9598 9599 TextareaInput.prototype.ensurePolled = function () { 9600 if (this.pollingFast && this.poll()) { this.pollingFast = false; } 9601 }; 9602 9603 TextareaInput.prototype.onKeyPress = function () { 9604 if (ie && ie_version >= 9) { this.hasSelection = null; } 9605 this.fastPoll(); 9606 }; 9607 9608 TextareaInput.prototype.onContextMenu = function (e) { 9609 var input = this, cm = input.cm, display = cm.display, te = input.textarea; 9610 if (input.contextMenuPending) { input.contextMenuPending(); } 9611 var pos = posFromMouse(cm, e), scrollPos = display.scroller.scrollTop; 9612 if (!pos || presto) { return } // Opera is difficult. 9613 9614 // Reset the current text selection only if the click is done outside of the selection 9615 // and 'resetSelectionOnContextMenu' option is true. 9616 var reset = cm.options.resetSelectionOnContextMenu; 9617 if (reset && cm.doc.sel.contains(pos) == -1) 9618 { operation(cm, setSelection)(cm.doc, simpleSelection(pos), sel_dontScroll); } 9619 9620 var oldCSS = te.style.cssText, oldWrapperCSS = input.wrapper.style.cssText; 9621 var wrapperBox = input.wrapper.offsetParent.getBoundingClientRect(); 9622 input.wrapper.style.cssText = "position: static"; 9623 te.style.cssText = "position: absolute; width: 30px; height: 30px;\n top: " + (e.clientY - wrapperBox.top - 5) + "px; left: " + (e.clientX - wrapperBox.left - 5) + "px;\n z-index: 1000; background: " + (ie ? "rgba(255, 255, 255, .05)" : "transparent") + ";\n outline: none; border-width: 0; outline: none; overflow: hidden; opacity: .05; filter: alpha(opacity=5);"; 9624 var oldScrollY; 9625 if (webkit) { oldScrollY = window.scrollY; } // Work around Chrome issue (#2712) 9626 display.input.focus(); 9627 if (webkit) { window.scrollTo(null, oldScrollY); } 9628 display.input.reset(); 9629 // Adds "Select all" to context menu in FF 9630 if (!cm.somethingSelected()) { te.value = input.prevInput = " "; } 9631 input.contextMenuPending = rehide; 9632 display.selForContextMenu = cm.doc.sel; 9633 clearTimeout(display.detectingSelectAll); 9634 9635 // Select-all will be greyed out if there's nothing to select, so 9636 // this adds a zero-width space so that we can later check whether 9637 // it got selected. 9638 function prepareSelectAllHack() { 9639 if (te.selectionStart != null) { 9640 var selected = cm.somethingSelected(); 9641 var extval = "\u200b" + (selected ? te.value : ""); 9642 te.value = "\u21da"; // Used to catch context-menu undo 9643 te.value = extval; 9644 input.prevInput = selected ? "" : "\u200b"; 9645 te.selectionStart = 1; te.selectionEnd = extval.length; 9646 // Re-set this, in case some other handler touched the 9647 // selection in the meantime. 9648 display.selForContextMenu = cm.doc.sel; 9649 } 9650 } 9651 function rehide() { 9652 if (input.contextMenuPending != rehide) { return } 9653 input.contextMenuPending = false; 9654 input.wrapper.style.cssText = oldWrapperCSS; 9655 te.style.cssText = oldCSS; 9656 if (ie && ie_version < 9) { display.scrollbars.setScrollTop(display.scroller.scrollTop = scrollPos); } 9657 9658 // Try to detect the user choosing select-all 9659 if (te.selectionStart != null) { 9660 if (!ie || (ie && ie_version < 9)) { prepareSelectAllHack(); } 9661 var i = 0, poll = function () { 9662 if (display.selForContextMenu == cm.doc.sel && te.selectionStart == 0 && 9663 te.selectionEnd > 0 && input.prevInput == "\u200b") { 9664 operation(cm, selectAll)(cm); 9665 } else if (i++ < 10) { 9666 display.detectingSelectAll = setTimeout(poll, 500); 9667 } else { 9668 display.selForContextMenu = null; 9669 display.input.reset(); 9670 } 9671 }; 9672 display.detectingSelectAll = setTimeout(poll, 200); 9673 } 9674 } 9675 9676 if (ie && ie_version >= 9) { prepareSelectAllHack(); } 9677 if (captureRightClick) { 9678 e_stop(e); 9679 var mouseup = function () { 9680 off(window, "mouseup", mouseup); 9681 setTimeout(rehide, 20); 9682 }; 9683 on(window, "mouseup", mouseup); 9684 } else { 9685 setTimeout(rehide, 50); 9686 } 9687 }; 9688 9689 TextareaInput.prototype.readOnlyChanged = function (val) { 9690 if (!val) { this.reset(); } 9691 this.textarea.disabled = val == "nocursor"; 9692 this.textarea.readOnly = !!val; 9693 }; 9694 9695 TextareaInput.prototype.setUneditable = function () {}; 9696 9697 TextareaInput.prototype.needsContentAttribute = false; 9698 9699 function fromTextArea(textarea, options) { 9700 options = options ? copyObj(options) : {}; 9701 options.value = textarea.value; 9702 if (!options.tabindex && textarea.tabIndex) 9703 { options.tabindex = textarea.tabIndex; } 9704 if (!options.placeholder && textarea.placeholder) 9705 { options.placeholder = textarea.placeholder; } 9706 // Set autofocus to true if this textarea is focused, or if it has 9707 // autofocus and no other element is focused. 9708 if (options.autofocus == null) { 9709 var hasFocus = activeElt(); 9710 options.autofocus = hasFocus == textarea || 9711 textarea.getAttribute("autofocus") != null && hasFocus == document.body; 9712 } 9713 9714 function save() {textarea.value = cm.getValue();} 9715 9716 var realSubmit; 9717 if (textarea.form) { 9718 on(textarea.form, "submit", save); 9719 // Deplorable hack to make the submit method do the right thing. 9720 if (!options.leaveSubmitMethodAlone) { 9721 var form = textarea.form; 9722 realSubmit = form.submit; 9723 try { 9724 var wrappedSubmit = form.submit = function () { 9725 save(); 9726 form.submit = realSubmit; 9727 form.submit(); 9728 form.submit = wrappedSubmit; 9729 }; 9730 } catch(e) {} 9731 } 9732 } 9733 9734 options.finishInit = function (cm) { 9735 cm.save = save; 9736 cm.getTextArea = function () { return textarea; }; 9737 cm.toTextArea = function () { 9738 cm.toTextArea = isNaN; // Prevent this from being ran twice 9739 save(); 9740 textarea.parentNode.removeChild(cm.getWrapperElement()); 9741 textarea.style.display = ""; 9742 if (textarea.form) { 9743 off(textarea.form, "submit", save); 9744 if (!options.leaveSubmitMethodAlone && typeof textarea.form.submit == "function") 9745 { textarea.form.submit = realSubmit; } 9746 } 9747 }; 9748 }; 9749 9750 textarea.style.display = "none"; 9751 var cm = CodeMirror(function (node) { return textarea.parentNode.insertBefore(node, textarea.nextSibling); }, 9752 options); 9753 return cm 9754 } 9755 9756 function addLegacyProps(CodeMirror) { 9757 CodeMirror.off = off; 9758 CodeMirror.on = on; 9759 CodeMirror.wheelEventPixels = wheelEventPixels; 9760 CodeMirror.Doc = Doc; 9761 CodeMirror.splitLines = splitLinesAuto; 9762 CodeMirror.countColumn = countColumn; 9763 CodeMirror.findColumn = findColumn; 9764 CodeMirror.isWordChar = isWordCharBasic; 9765 CodeMirror.Pass = Pass; 9766 CodeMirror.signal = signal; 9767 CodeMirror.Line = Line; 9768 CodeMirror.changeEnd = changeEnd; 9769 CodeMirror.scrollbarModel = scrollbarModel; 9770 CodeMirror.Pos = Pos; 9771 CodeMirror.cmpPos = cmp; 9772 CodeMirror.modes = modes; 9773 CodeMirror.mimeModes = mimeModes; 9774 CodeMirror.resolveMode = resolveMode; 9775 CodeMirror.getMode = getMode; 9776 CodeMirror.modeExtensions = modeExtensions; 9777 CodeMirror.extendMode = extendMode; 9778 CodeMirror.copyState = copyState; 9779 CodeMirror.startState = startState; 9780 CodeMirror.innerMode = innerMode; 9781 CodeMirror.commands = commands; 9782 CodeMirror.keyMap = keyMap; 9783 CodeMirror.keyName = keyName; 9784 CodeMirror.isModifierKey = isModifierKey; 9785 CodeMirror.lookupKey = lookupKey; 9786 CodeMirror.normalizeKeyMap = normalizeKeyMap; 9787 CodeMirror.StringStream = StringStream; 9788 CodeMirror.SharedTextMarker = SharedTextMarker; 9789 CodeMirror.TextMarker = TextMarker; 9790 CodeMirror.LineWidget = LineWidget; 9791 CodeMirror.e_preventDefault = e_preventDefault; 9792 CodeMirror.e_stopPropagation = e_stopPropagation; 9793 CodeMirror.e_stop = e_stop; 9794 CodeMirror.addClass = addClass; 9795 CodeMirror.contains = contains; 9796 CodeMirror.rmClass = rmClass; 9797 CodeMirror.keyNames = keyNames; 9798 } 9799 9800 // EDITOR CONSTRUCTOR 9801 9802 defineOptions(CodeMirror); 9803 9804 addEditorMethods(CodeMirror); 9805 9806 // Set up methods on CodeMirror's prototype to redirect to the editor's document. 9807 var dontDelegate = "iter insert remove copy getEditor constructor".split(" "); 9808 for (var prop in Doc.prototype) { if (Doc.prototype.hasOwnProperty(prop) && indexOf(dontDelegate, prop) < 0) 9809 { CodeMirror.prototype[prop] = (function(method) { 9810 return function() {return method.apply(this.doc, arguments)} 9811 })(Doc.prototype[prop]); } } 9812 9813 eventMixin(Doc); 9814 CodeMirror.inputStyles = {"textarea": TextareaInput, "contenteditable": ContentEditableInput}; 9815 9816 // Extra arguments are stored as the mode's dependencies, which is 9817 // used by (legacy) mechanisms like loadmode.js to automatically 9818 // load a mode. (Preferred mechanism is the require/define calls.) 9819 CodeMirror.defineMode = function(name/*, mode, …*/) { 9820 if (!CodeMirror.defaults.mode && name != "null") { CodeMirror.defaults.mode = name; } 9821 defineMode.apply(this, arguments); 9822 }; 9823 9824 CodeMirror.defineMIME = defineMIME; 9825 9826 // Minimal default mode. 9827 CodeMirror.defineMode("null", function () { return ({token: function (stream) { return stream.skipToEnd(); }}); }); 9828 CodeMirror.defineMIME("text/plain", "null"); 9829 9830 // EXTENSIONS 9831 9832 CodeMirror.defineExtension = function (name, func) { 9833 CodeMirror.prototype[name] = func; 9834 }; 9835 CodeMirror.defineDocExtension = function (name, func) { 9836 Doc.prototype[name] = func; 9837 }; 9838 9839 CodeMirror.fromTextArea = fromTextArea; 9840 9841 addLegacyProps(CodeMirror); 9842 9843 CodeMirror.version = "5.65.0"; 9844 9845 return CodeMirror; 9846 9847 })));