Subversion Repositories wimsdev

Rev

Rev 16859 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
14283 obado 1
// CodeMirror, copyright (c) by Marijn Haverbeke and others
16859 obado 2
// Distributed under an MIT license: https://codemirror.net/5/LICENSE
14283 obado 3
 
16859 obado 4
// This is CodeMirror (https://codemirror.net/5), a code editor
14283 obado 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) :
15152 obado 13
  (global = global || self, global.CodeMirror = factory());
14283 obado 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);
16859 obado 29
  var chrome = !edge && /Chrome\/(\d+)/.exec(userAgent);
30
  var chrome_version = chrome && +chrome[1];
14283 obado 31
  var presto = /Opera\//.test(userAgent);
32
  var safari = /Apple Computer/.test(navigator.vendor);
33
  var mac_geMountainLion = /Mac OS X 1\d\D([8-9]|\d\d)\D/.test(userAgent);
34
  var phantom = /PhantomJS/.test(userAgent);
35
 
16493 obado 36
  var ios = safari && (/Mobile\/\w+/.test(userAgent) || navigator.maxTouchPoints > 2);
14283 obado 37
  var android = /Android/.test(userAgent);
38
  // This is woefully incomplete. Suggestions for alternative methods welcome.
39
  var mobile = ios || android || /webOS|BlackBerry|Opera Mini|Opera Mobi|IEMobile/i.test(userAgent);
40
  var mac = ios || /Mac/.test(platform);
41
  var chromeOS = /\bCrOS\b/.test(userAgent);
42
  var windows = /win/i.test(platform);
43
 
44
  var presto_version = presto && userAgent.match(/Version\/(\d*\.\d*)/);
45
  if (presto_version) { presto_version = Number(presto_version[1]); }
46
  if (presto_version && presto_version >= 15) { presto = false; webkit = true; }
47
  // Some browsers use the wrong event properties to signal cmd/ctrl on OS X
48
  var flipCtrlCmd = mac && (qtwebkit || presto && (presto_version == null || presto_version < 12.11));
49
  var captureRightClick = gecko || (ie && ie_version >= 9);
50
 
51
  function classTest(cls) { return new RegExp("(^|\\s)" + cls + "(?:$|\\s)\\s*") }
52
 
53
  var rmClass = function(node, cls) {
54
    var current = node.className;
55
    var match = classTest(cls).exec(current);
56
    if (match) {
57
      var after = current.slice(match.index + match[0].length);
58
      node.className = current.slice(0, match.index) + (after ? match[1] + after : "");
59
    }
60
  };
61
 
62
  function removeChildren(e) {
63
    for (var count = e.childNodes.length; count > 0; --count)
64
      { e.removeChild(e.firstChild); }
65
    return e
66
  }
67
 
68
  function removeChildrenAndAdd(parent, e) {
69
    return removeChildren(parent).appendChild(e)
70
  }
71
 
72
  function elt(tag, content, className, style) {
73
    var e = document.createElement(tag);
74
    if (className) { e.className = className; }
75
    if (style) { e.style.cssText = style; }
76
    if (typeof content == "string") { e.appendChild(document.createTextNode(content)); }
77
    else if (content) { for (var i = 0; i < content.length; ++i) { e.appendChild(content[i]); } }
78
    return e
79
  }
80
  // wrapper for elt, which removes the elt from the accessibility tree
81
  function eltP(tag, content, className, style) {
82
    var e = elt(tag, content, className, style);
83
    e.setAttribute("role", "presentation");
84
    return e
85
  }
86
 
87
  var range;
88
  if (document.createRange) { range = function(node, start, end, endNode) {
89
    var r = document.createRange();
90
    r.setEnd(endNode || node, end);
91
    r.setStart(node, start);
92
    return r
93
  }; }
94
  else { range = function(node, start, end) {
95
    var r = document.body.createTextRange();
96
    try { r.moveToElementText(node.parentNode); }
97
    catch(e) { return r }
98
    r.collapse(true);
99
    r.moveEnd("character", end);
100
    r.moveStart("character", start);
101
    return r
102
  }; }
103
 
104
  function contains(parent, child) {
105
    if (child.nodeType == 3) // Android browser always returns false when child is a textnode
106
      { child = child.parentNode; }
107
    if (parent.contains)
108
      { return parent.contains(child) }
109
    do {
110
      if (child.nodeType == 11) { child = child.host; }
111
      if (child == parent) { return true }
112
    } while (child = child.parentNode)
113
  }
114
 
17702 obado 115
  function activeElt(doc) {
14283 obado 116
    // IE and Edge may throw an "Unspecified Error" when accessing document.activeElement.
117
    // IE < 10 will throw when accessed while the page is loading or in an iframe.
118
    // IE > 9 and Edge will throw when accessed in an iframe if document.body is unavailable.
119
    var activeElement;
120
    try {
17702 obado 121
      activeElement = doc.activeElement;
14283 obado 122
    } catch(e) {
17702 obado 123
      activeElement = doc.body || null;
14283 obado 124
    }
125
    while (activeElement && activeElement.shadowRoot && activeElement.shadowRoot.activeElement)
126
      { activeElement = activeElement.shadowRoot.activeElement; }
127
    return activeElement
128
  }
129
 
130
  function addClass(node, cls) {
131
    var current = node.className;
132
    if (!classTest(cls).test(current)) { node.className += (current ? " " : "") + cls; }
133
  }
134
  function joinClasses(a, b) {
135
    var as = a.split(" ");
136
    for (var i = 0; i < as.length; i++)
137
      { if (as[i] && !classTest(as[i]).test(b)) { b += " " + as[i]; } }
138
    return b
139
  }
140
 
141
  var selectInput = function(node) { node.select(); };
142
  if (ios) // Mobile Safari apparently has a bug where select() is broken.
143
    { selectInput = function(node) { node.selectionStart = 0; node.selectionEnd = node.value.length; }; }
144
  else if (ie) // Suppress mysterious IE10 errors
145
    { selectInput = function(node) { try { node.select(); } catch(_e) {} }; }
146
 
17702 obado 147
  function doc(cm) { return cm.display.wrapper.ownerDocument }
148
 
149
  function win(cm) { return doc(cm).defaultView }
150
 
14283 obado 151
  function bind(f) {
152
    var args = Array.prototype.slice.call(arguments, 1);
153
    return function(){return f.apply(null, args)}
154
  }
155
 
156
  function copyObj(obj, target, overwrite) {
157
    if (!target) { target = {}; }
158
    for (var prop in obj)
159
      { if (obj.hasOwnProperty(prop) && (overwrite !== false || !target.hasOwnProperty(prop)))
160
        { target[prop] = obj[prop]; } }
161
    return target
162
  }
163
 
164
  // Counts the column offset in a string, taking tabs into account.
165
  // Used mostly to find indentation.
166
  function countColumn(string, end, tabSize, startIndex, startValue) {
167
    if (end == null) {
168
      end = string.search(/[^\s\u00a0]/);
169
      if (end == -1) { end = string.length; }
170
    }
171
    for (var i = startIndex || 0, n = startValue || 0;;) {
172
      var nextTab = string.indexOf("\t", i);
173
      if (nextTab < 0 || nextTab >= end)
174
        { return n + (end - i) }
175
      n += nextTab - i;
176
      n += tabSize - (n % tabSize);
177
      i = nextTab + 1;
178
    }
179
  }
180
 
15152 obado 181
  var Delayed = function() {
182
    this.id = null;
183
    this.f = null;
184
    this.time = 0;
185
    this.handler = bind(this.onTimeout, this);
186
  };
187
  Delayed.prototype.onTimeout = function (self) {
188
    self.id = 0;
189
    if (self.time <= +new Date) {
190
      self.f();
191
    } else {
192
      setTimeout(self.handler, self.time - +new Date);
193
    }
194
  };
14283 obado 195
  Delayed.prototype.set = function (ms, f) {
15152 obado 196
    this.f = f;
197
    var time = +new Date + ms;
198
    if (!this.id || time < this.time) {
199
      clearTimeout(this.id);
200
      this.id = setTimeout(this.handler, ms);
201
      this.time = time;
202
    }
14283 obado 203
  };
204
 
205
  function indexOf(array, elt) {
206
    for (var i = 0; i < array.length; ++i)
207
      { if (array[i] == elt) { return i } }
208
    return -1
209
  }
210
 
211
  // Number of pixels added to scroller and sizer to hide scrollbar
15152 obado 212
  var scrollerGap = 50;
14283 obado 213
 
214
  // Returned or thrown by various protocols to signal 'I'm not
215
  // handling this'.
216
  var Pass = {toString: function(){return "CodeMirror.Pass"}};
217
 
218
  // Reused option objects for setSelection & friends
219
  var sel_dontScroll = {scroll: false}, sel_mouse = {origin: "*mouse"}, sel_move = {origin: "+move"};
220
 
221
  // The inverse of countColumn -- find the offset that corresponds to
222
  // a particular column.
223
  function findColumn(string, goal, tabSize) {
224
    for (var pos = 0, col = 0;;) {
225
      var nextTab = string.indexOf("\t", pos);
226
      if (nextTab == -1) { nextTab = string.length; }
227
      var skipped = nextTab - pos;
228
      if (nextTab == string.length || col + skipped >= goal)
229
        { return pos + Math.min(skipped, goal - col) }
230
      col += nextTab - pos;
231
      col += tabSize - (col % tabSize);
232
      pos = nextTab + 1;
233
      if (col >= goal) { return pos }
234
    }
235
  }
236
 
237
  var spaceStrs = [""];
238
  function spaceStr(n) {
239
    while (spaceStrs.length <= n)
240
      { spaceStrs.push(lst(spaceStrs) + " "); }
241
    return spaceStrs[n]
242
  }
243
 
244
  function lst(arr) { return arr[arr.length-1] }
245
 
246
  function map(array, f) {
247
    var out = [];
248
    for (var i = 0; i < array.length; i++) { out[i] = f(array[i], i); }
249
    return out
250
  }
251
 
252
  function insertSorted(array, value, score) {
253
    var pos = 0, priority = score(value);
254
    while (pos < array.length && score(array[pos]) <= priority) { pos++; }
255
    array.splice(pos, 0, value);
256
  }
257
 
258
  function nothing() {}
259
 
260
  function createObj(base, props) {
261
    var inst;
262
    if (Object.create) {
263
      inst = Object.create(base);
264
    } else {
265
      nothing.prototype = base;
266
      inst = new nothing();
267
    }
268
    if (props) { copyObj(props, inst); }
269
    return inst
270
  }
271
 
272
  var nonASCIISingleCaseWordChar = /[\u00df\u0587\u0590-\u05f4\u0600-\u06ff\u3040-\u309f\u30a0-\u30ff\u3400-\u4db5\u4e00-\u9fcc\uac00-\ud7af]/;
273
  function isWordCharBasic(ch) {
274
    return /\w/.test(ch) || ch > "\x80" &&
275
      (ch.toUpperCase() != ch.toLowerCase() || nonASCIISingleCaseWordChar.test(ch))
276
  }
277
  function isWordChar(ch, helper) {
278
    if (!helper) { return isWordCharBasic(ch) }
279
    if (helper.source.indexOf("\\w") > -1 && isWordCharBasic(ch)) { return true }
280
    return helper.test(ch)
281
  }
282
 
283
  function isEmpty(obj) {
284
    for (var n in obj) { if (obj.hasOwnProperty(n) && obj[n]) { return false } }
285
    return true
286
  }
287
 
288
  // Extending unicode characters. A series of a non-extending char +
289
  // any number of extending chars is treated as a single unit as far
290
  // as editing and measuring is concerned. This is not fully correct,
291
  // since some scripts/fonts/browsers also treat other configurations
292
  // of code points as a group.
293
  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]/;
294
  function isExtendingChar(ch) { return ch.charCodeAt(0) >= 768 && extendingChars.test(ch) }
295
 
296
  // Returns a number from the range [`0`; `str.length`] unless `pos` is outside that range.
297
  function skipExtendingChars(str, pos, dir) {
298
    while ((dir < 0 ? pos > 0 : pos < str.length) && isExtendingChar(str.charAt(pos))) { pos += dir; }
299
    return pos
300
  }
301
 
302
  // Returns the value from the range [`from`; `to`] that satisfies
303
  // `pred` and is closest to `from`. Assumes that at least `to`
304
  // satisfies `pred`. Supports `from` being greater than `to`.
305
  function findFirst(pred, from, to) {
306
    // At any point we are certain `to` satisfies `pred`, don't know
307
    // whether `from` does.
308
    var dir = from > to ? -1 : 1;
309
    for (;;) {
310
      if (from == to) { return from }
311
      var midF = (from + to) / 2, mid = dir < 0 ? Math.ceil(midF) : Math.floor(midF);
312
      if (mid == from) { return pred(mid) ? from : to }
313
      if (pred(mid)) { to = mid; }
314
      else { from = mid + dir; }
315
    }
316
  }
317
 
318
  // BIDI HELPERS
319
 
320
  function iterateBidiSections(order, from, to, f) {
321
    if (!order) { return f(from, to, "ltr", 0) }
322
    var found = false;
323
    for (var i = 0; i < order.length; ++i) {
324
      var part = order[i];
325
      if (part.from < to && part.to > from || from == to && part.to == from) {
326
        f(Math.max(part.from, from), Math.min(part.to, to), part.level == 1 ? "rtl" : "ltr", i);
327
        found = true;
328
      }
329
    }
330
    if (!found) { f(from, to, "ltr"); }
331
  }
332
 
333
  var bidiOther = null;
334
  function getBidiPartAt(order, ch, sticky) {
335
    var found;
336
    bidiOther = null;
337
    for (var i = 0; i < order.length; ++i) {
338
      var cur = order[i];
339
      if (cur.from < ch && cur.to > ch) { return i }
340
      if (cur.to == ch) {
341
        if (cur.from != cur.to && sticky == "before") { found = i; }
342
        else { bidiOther = i; }
343
      }
344
      if (cur.from == ch) {
345
        if (cur.from != cur.to && sticky != "before") { found = i; }
346
        else { bidiOther = i; }
347
      }
348
    }
349
    return found != null ? found : bidiOther
350
  }
351
 
352
  // Bidirectional ordering algorithm
353
  // See http://unicode.org/reports/tr9/tr9-13.html for the algorithm
354
  // that this (partially) implements.
355
 
356
  // One-char codes used for character types:
357
  // L (L):   Left-to-Right
358
  // R (R):   Right-to-Left
359
  // r (AL):  Right-to-Left Arabic
360
  // 1 (EN):  European Number
361
  // + (ES):  European Number Separator
362
  // % (ET):  European Number Terminator
363
  // n (AN):  Arabic Number
364
  // , (CS):  Common Number Separator
365
  // m (NSM): Non-Spacing Mark
366
  // b (BN):  Boundary Neutral
367
  // s (B):   Paragraph Separator
368
  // t (S):   Segment Separator
369
  // w (WS):  Whitespace
370
  // N (ON):  Other Neutrals
371
 
372
  // Returns null if characters are ordered as they appear
373
  // (left-to-right), or an array of sections ({from, to, level}
374
  // objects) in the order in which they occur visually.
375
  var bidiOrdering = (function() {
376
    // Character types for codepoints 0 to 0xff
377
    var lowTypes = "bbbbbbbbbtstwsbbbbbbbbbbbbbbssstwNN%%%NNNNNN,N,N1111111111NNNNNNNLLLLLLLLLLLLLLLLLLLLLLLLLLNNNNNNLLLLLLLLLLLLLLLLLLLLLLLLLLNNNNbbbbbbsbbbbbbbbbbbbbbbbbbbbbbbbbb,N%%%%NNNNLNNNNN%%11NLNNN1LNNNNNLLLLLLLLLLLLLLLLLLLLLLLNLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLN";
378
    // Character types for codepoints 0x600 to 0x6f9
379
    var arabicTypes = "nnnnnnNNr%%r,rNNmmmmmmmmmmmrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrmmmmmmmmmmmmmmmmmmmmmnnnnnnnnnn%nnrrrmrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrmmmmmmmnNmmmmmmrrmmNmmmmrr1111111111";
380
    function charType(code) {
381
      if (code <= 0xf7) { return lowTypes.charAt(code) }
382
      else if (0x590 <= code && code <= 0x5f4) { return "R" }
383
      else if (0x600 <= code && code <= 0x6f9) { return arabicTypes.charAt(code - 0x600) }
384
      else if (0x6ee <= code && code <= 0x8ac) { return "r" }
385
      else if (0x2000 <= code && code <= 0x200b) { return "w" }
386
      else if (code == 0x200c) { return "b" }
387
      else { return "L" }
388
    }
389
 
390
    var bidiRE = /[\u0590-\u05f4\u0600-\u06ff\u0700-\u08ac]/;
391
    var isNeutral = /[stwN]/, isStrong = /[LRr]/, countsAsLeft = /[Lb1n]/, countsAsNum = /[1n]/;
392
 
393
    function BidiSpan(level, from, to) {
394
      this.level = level;
395
      this.from = from; this.to = to;
396
    }
397
 
398
    return function(str, direction) {
399
      var outerType = direction == "ltr" ? "L" : "R";
400
 
401
      if (str.length == 0 || direction == "ltr" && !bidiRE.test(str)) { return false }
402
      var len = str.length, types = [];
403
      for (var i = 0; i < len; ++i)
404
        { types.push(charType(str.charCodeAt(i))); }
405
 
406
      // W1. Examine each non-spacing mark (NSM) in the level run, and
407
      // change the type of the NSM to the type of the previous
408
      // character. If the NSM is at the start of the level run, it will
409
      // get the type of sor.
410
      for (var i$1 = 0, prev = outerType; i$1 < len; ++i$1) {
411
        var type = types[i$1];
412
        if (type == "m") { types[i$1] = prev; }
413
        else { prev = type; }
414
      }
415
 
416
      // W2. Search backwards from each instance of a European number
417
      // until the first strong type (R, L, AL, or sor) is found. If an
418
      // AL is found, change the type of the European number to Arabic
419
      // number.
420
      // W3. Change all ALs to R.
421
      for (var i$2 = 0, cur = outerType; i$2 < len; ++i$2) {
422
        var type$1 = types[i$2];
423
        if (type$1 == "1" && cur == "r") { types[i$2] = "n"; }
424
        else if (isStrong.test(type$1)) { cur = type$1; if (type$1 == "r") { types[i$2] = "R"; } }
425
      }
426
 
427
      // W4. A single European separator between two European numbers
428
      // changes to a European number. A single common separator between
429
      // two numbers of the same type changes to that type.
430
      for (var i$3 = 1, prev$1 = types[0]; i$3 < len - 1; ++i$3) {
431
        var type$2 = types[i$3];
432
        if (type$2 == "+" && prev$1 == "1" && types[i$3+1] == "1") { types[i$3] = "1"; }
433
        else if (type$2 == "," && prev$1 == types[i$3+1] &&
434
                 (prev$1 == "1" || prev$1 == "n")) { types[i$3] = prev$1; }
435
        prev$1 = type$2;
436
      }
437
 
438
      // W5. A sequence of European terminators adjacent to European
439
      // numbers changes to all European numbers.
440
      // W6. Otherwise, separators and terminators change to Other
441
      // Neutral.
442
      for (var i$4 = 0; i$4 < len; ++i$4) {
443
        var type$3 = types[i$4];
444
        if (type$3 == ",") { types[i$4] = "N"; }
445
        else if (type$3 == "%") {
446
          var end = (void 0);
447
          for (end = i$4 + 1; end < len && types[end] == "%"; ++end) {}
448
          var replace = (i$4 && types[i$4-1] == "!") || (end < len && types[end] == "1") ? "1" : "N";
449
          for (var j = i$4; j < end; ++j) { types[j] = replace; }
450
          i$4 = end - 1;
451
        }
452
      }
453
 
454
      // W7. Search backwards from each instance of a European number
455
      // until the first strong type (R, L, or sor) is found. If an L is
456
      // found, then change the type of the European number to L.
457
      for (var i$5 = 0, cur$1 = outerType; i$5 < len; ++i$5) {
458
        var type$4 = types[i$5];
459
        if (cur$1 == "L" && type$4 == "1") { types[i$5] = "L"; }
460
        else if (isStrong.test(type$4)) { cur$1 = type$4; }
461
      }
462
 
463
      // N1. A sequence of neutrals takes the direction of the
464
      // surrounding strong text if the text on both sides has the same
465
      // direction. European and Arabic numbers act as if they were R in
466
      // terms of their influence on neutrals. Start-of-level-run (sor)
467
      // and end-of-level-run (eor) are used at level run boundaries.
468
      // N2. Any remaining neutrals take the embedding direction.
469
      for (var i$6 = 0; i$6 < len; ++i$6) {
470
        if (isNeutral.test(types[i$6])) {
471
          var end$1 = (void 0);
472
          for (end$1 = i$6 + 1; end$1 < len && isNeutral.test(types[end$1]); ++end$1) {}
473
          var before = (i$6 ? types[i$6-1] : outerType) == "L";
474
          var after = (end$1 < len ? types[end$1] : outerType) == "L";
475
          var replace$1 = before == after ? (before ? "L" : "R") : outerType;
476
          for (var j$1 = i$6; j$1 < end$1; ++j$1) { types[j$1] = replace$1; }
477
          i$6 = end$1 - 1;
478
        }
479
      }
480
 
481
      // Here we depart from the documented algorithm, in order to avoid
482
      // building up an actual levels array. Since there are only three
483
      // levels (0, 1, 2) in an implementation that doesn't take
484
      // explicit embedding into account, we can build up the order on
485
      // the fly, without following the level-based algorithm.
486
      var order = [], m;
487
      for (var i$7 = 0; i$7 < len;) {
488
        if (countsAsLeft.test(types[i$7])) {
489
          var start = i$7;
490
          for (++i$7; i$7 < len && countsAsLeft.test(types[i$7]); ++i$7) {}
491
          order.push(new BidiSpan(0, start, i$7));
492
        } else {
15152 obado 493
          var pos = i$7, at = order.length, isRTL = direction == "rtl" ? 1 : 0;
14283 obado 494
          for (++i$7; i$7 < len && types[i$7] != "L"; ++i$7) {}
495
          for (var j$2 = pos; j$2 < i$7;) {
496
            if (countsAsNum.test(types[j$2])) {
15152 obado 497
              if (pos < j$2) { order.splice(at, 0, new BidiSpan(1, pos, j$2)); at += isRTL; }
14283 obado 498
              var nstart = j$2;
499
              for (++j$2; j$2 < i$7 && countsAsNum.test(types[j$2]); ++j$2) {}
500
              order.splice(at, 0, new BidiSpan(2, nstart, j$2));
15152 obado 501
              at += isRTL;
14283 obado 502
              pos = j$2;
503
            } else { ++j$2; }
504
          }
505
          if (pos < i$7) { order.splice(at, 0, new BidiSpan(1, pos, i$7)); }
506
        }
507
      }
508
      if (direction == "ltr") {
509
        if (order[0].level == 1 && (m = str.match(/^\s+/))) {
510
          order[0].from = m[0].length;
511
          order.unshift(new BidiSpan(0, 0, m[0].length));
512
        }
513
        if (lst(order).level == 1 && (m = str.match(/\s+$/))) {
514
          lst(order).to -= m[0].length;
515
          order.push(new BidiSpan(0, len - m[0].length, len));
516
        }
517
      }
518
 
519
      return direction == "rtl" ? order.reverse() : order
520
    }
521
  })();
522
 
523
  // Get the bidi ordering for the given line (and cache it). Returns
524
  // false for lines that are fully left-to-right, and an array of
525
  // BidiSpan objects otherwise.
526
  function getOrder(line, direction) {
527
    var order = line.order;
528
    if (order == null) { order = line.order = bidiOrdering(line.text, direction); }
529
    return order
530
  }
531
 
532
  // EVENT HANDLING
533
 
534
  // Lightweight event framework. on/off also work on DOM nodes,
535
  // registering native DOM handlers.
536
 
537
  var noHandlers = [];
538
 
539
  var on = function(emitter, type, f) {
540
    if (emitter.addEventListener) {
541
      emitter.addEventListener(type, f, false);
542
    } else if (emitter.attachEvent) {
543
      emitter.attachEvent("on" + type, f);
544
    } else {
15152 obado 545
      var map = emitter._handlers || (emitter._handlers = {});
546
      map[type] = (map[type] || noHandlers).concat(f);
14283 obado 547
    }
548
  };
549
 
550
  function getHandlers(emitter, type) {
551
    return emitter._handlers && emitter._handlers[type] || noHandlers
552
  }
553
 
554
  function off(emitter, type, f) {
555
    if (emitter.removeEventListener) {
556
      emitter.removeEventListener(type, f, false);
557
    } else if (emitter.detachEvent) {
558
      emitter.detachEvent("on" + type, f);
559
    } else {
15152 obado 560
      var map = emitter._handlers, arr = map && map[type];
14283 obado 561
      if (arr) {
562
        var index = indexOf(arr, f);
563
        if (index > -1)
15152 obado 564
          { map[type] = arr.slice(0, index).concat(arr.slice(index + 1)); }
14283 obado 565
      }
566
    }
567
  }
568
 
569
  function signal(emitter, type /*, values...*/) {
570
    var handlers = getHandlers(emitter, type);
571
    if (!handlers.length) { return }
572
    var args = Array.prototype.slice.call(arguments, 2);
573
    for (var i = 0; i < handlers.length; ++i) { handlers[i].apply(null, args); }
574
  }
575
 
576
  // The DOM events that CodeMirror handles can be overridden by
577
  // registering a (non-DOM) handler on the editor for the event name,
578
  // and preventDefault-ing the event in that handler.
579
  function signalDOMEvent(cm, e, override) {
580
    if (typeof e == "string")
581
      { e = {type: e, preventDefault: function() { this.defaultPrevented = true; }}; }
582
    signal(cm, override || e.type, cm, e);
583
    return e_defaultPrevented(e) || e.codemirrorIgnore
584
  }
585
 
586
  function signalCursorActivity(cm) {
587
    var arr = cm._handlers && cm._handlers.cursorActivity;
588
    if (!arr) { return }
589
    var set = cm.curOp.cursorActivityHandlers || (cm.curOp.cursorActivityHandlers = []);
590
    for (var i = 0; i < arr.length; ++i) { if (indexOf(set, arr[i]) == -1)
591
      { set.push(arr[i]); } }
592
  }
593
 
594
  function hasHandler(emitter, type) {
595
    return getHandlers(emitter, type).length > 0
596
  }
597
 
598
  // Add on and off methods to a constructor's prototype, to make
599
  // registering events on such objects more convenient.
600
  function eventMixin(ctor) {
601
    ctor.prototype.on = function(type, f) {on(this, type, f);};
602
    ctor.prototype.off = function(type, f) {off(this, type, f);};
603
  }
604
 
605
  // Due to the fact that we still support jurassic IE versions, some
606
  // compatibility wrappers are needed.
607
 
608
  function e_preventDefault(e) {
609
    if (e.preventDefault) { e.preventDefault(); }
610
    else { e.returnValue = false; }
611
  }
612
  function e_stopPropagation(e) {
613
    if (e.stopPropagation) { e.stopPropagation(); }
614
    else { e.cancelBubble = true; }
615
  }
616
  function e_defaultPrevented(e) {
617
    return e.defaultPrevented != null ? e.defaultPrevented : e.returnValue == false
618
  }
619
  function e_stop(e) {e_preventDefault(e); e_stopPropagation(e);}
620
 
621
  function e_target(e) {return e.target || e.srcElement}
622
  function e_button(e) {
623
    var b = e.which;
624
    if (b == null) {
625
      if (e.button & 1) { b = 1; }
626
      else if (e.button & 2) { b = 3; }
627
      else if (e.button & 4) { b = 2; }
628
    }
629
    if (mac && e.ctrlKey && b == 1) { b = 3; }
630
    return b
631
  }
632
 
633
  // Detect drag-and-drop
634
  var dragAndDrop = function() {
635
    // There is *some* kind of drag-and-drop support in IE6-8, but I
636
    // couldn't get it to work yet.
637
    if (ie && ie_version < 9) { return false }
638
    var div = elt('div');
639
    return "draggable" in div || "dragDrop" in div
640
  }();
641
 
642
  var zwspSupported;
643
  function zeroWidthElement(measure) {
644
    if (zwspSupported == null) {
645
      var test = elt("span", "\u200b");
646
      removeChildrenAndAdd(measure, elt("span", [test, document.createTextNode("x")]));
647
      if (measure.firstChild.offsetHeight != 0)
648
        { zwspSupported = test.offsetWidth <= 1 && test.offsetHeight > 2 && !(ie && ie_version < 8); }
649
    }
650
    var node = zwspSupported ? elt("span", "\u200b") :
651
      elt("span", "\u00a0", null, "display: inline-block; width: 1px; margin-right: -1px");
652
    node.setAttribute("cm-text", "");
653
    return node
654
  }
655
 
656
  // Feature-detect IE's crummy client rect reporting for bidi text
657
  var badBidiRects;
658
  function hasBadBidiRects(measure) {
659
    if (badBidiRects != null) { return badBidiRects }
660
    var txt = removeChildrenAndAdd(measure, document.createTextNode("A\u062eA"));
661
    var r0 = range(txt, 0, 1).getBoundingClientRect();
662
    var r1 = range(txt, 1, 2).getBoundingClientRect();
663
    removeChildren(measure);
664
    if (!r0 || r0.left == r0.right) { return false } // Safari returns null in some cases (#2780)
665
    return badBidiRects = (r1.right - r0.right < 3)
666
  }
667
 
668
  // See if "".split is the broken IE version, if so, provide an
669
  // alternative way to split lines.
670
  var splitLinesAuto = "\n\nb".split(/\n/).length != 3 ? function (string) {
671
    var pos = 0, result = [], l = string.length;
672
    while (pos <= l) {
673
      var nl = string.indexOf("\n", pos);
674
      if (nl == -1) { nl = string.length; }
675
      var line = string.slice(pos, string.charAt(nl - 1) == "\r" ? nl - 1 : nl);
676
      var rt = line.indexOf("\r");
677
      if (rt != -1) {
678
        result.push(line.slice(0, rt));
679
        pos += rt + 1;
680
      } else {
681
        result.push(line);
682
        pos = nl + 1;
683
      }
684
    }
685
    return result
686
  } : function (string) { return string.split(/\r\n?|\n/); };
687
 
688
  var hasSelection = window.getSelection ? function (te) {
689
    try { return te.selectionStart != te.selectionEnd }
690
    catch(e) { return false }
691
  } : function (te) {
15152 obado 692
    var range;
693
    try {range = te.ownerDocument.selection.createRange();}
14283 obado 694
    catch(e) {}
15152 obado 695
    if (!range || range.parentElement() != te) { return false }
696
    return range.compareEndPoints("StartToEnd", range) != 0
14283 obado 697
  };
698
 
699
  var hasCopyEvent = (function () {
700
    var e = elt("div");
701
    if ("oncopy" in e) { return true }
702
    e.setAttribute("oncopy", "return;");
703
    return typeof e.oncopy == "function"
704
  })();
705
 
706
  var badZoomedRects = null;
707
  function hasBadZoomedRects(measure) {
708
    if (badZoomedRects != null) { return badZoomedRects }
709
    var node = removeChildrenAndAdd(measure, elt("span", "x"));
710
    var normal = node.getBoundingClientRect();
711
    var fromRange = range(node, 0, 1).getBoundingClientRect();
712
    return badZoomedRects = Math.abs(normal.left - fromRange.left) > 1
713
  }
714
 
715
  // Known modes, by name and by MIME
716
  var modes = {}, mimeModes = {};
717
 
718
  // Extra arguments are stored as the mode's dependencies, which is
719
  // used by (legacy) mechanisms like loadmode.js to automatically
720
  // load a mode. (Preferred mechanism is the require/define calls.)
721
  function defineMode(name, mode) {
722
    if (arguments.length > 2)
723
      { mode.dependencies = Array.prototype.slice.call(arguments, 2); }
724
    modes[name] = mode;
725
  }
726
 
727
  function defineMIME(mime, spec) {
728
    mimeModes[mime] = spec;
729
  }
730
 
731
  // Given a MIME type, a {name, ...options} config object, or a name
732
  // string, return a mode config object.
733
  function resolveMode(spec) {
734
    if (typeof spec == "string" && mimeModes.hasOwnProperty(spec)) {
735
      spec = mimeModes[spec];
736
    } else if (spec && typeof spec.name == "string" && mimeModes.hasOwnProperty(spec.name)) {
737
      var found = mimeModes[spec.name];
738
      if (typeof found == "string") { found = {name: found}; }
739
      spec = createObj(found, spec);
740
      spec.name = found.name;
741
    } else if (typeof spec == "string" && /^[\w\-]+\/[\w\-]+\+xml$/.test(spec)) {
742
      return resolveMode("application/xml")
743
    } else if (typeof spec == "string" && /^[\w\-]+\/[\w\-]+\+json$/.test(spec)) {
744
      return resolveMode("application/json")
745
    }
746
    if (typeof spec == "string") { return {name: spec} }
747
    else { return spec || {name: "null"} }
748
  }
749
 
750
  // Given a mode spec (anything that resolveMode accepts), find and
751
  // initialize an actual mode object.
752
  function getMode(options, spec) {
753
    spec = resolveMode(spec);
754
    var mfactory = modes[spec.name];
755
    if (!mfactory) { return getMode(options, "text/plain") }
756
    var modeObj = mfactory(options, spec);
757
    if (modeExtensions.hasOwnProperty(spec.name)) {
758
      var exts = modeExtensions[spec.name];
759
      for (var prop in exts) {
760
        if (!exts.hasOwnProperty(prop)) { continue }
761
        if (modeObj.hasOwnProperty(prop)) { modeObj["_" + prop] = modeObj[prop]; }
762
        modeObj[prop] = exts[prop];
763
      }
764
    }
765
    modeObj.name = spec.name;
766
    if (spec.helperType) { modeObj.helperType = spec.helperType; }
767
    if (spec.modeProps) { for (var prop$1 in spec.modeProps)
768
      { modeObj[prop$1] = spec.modeProps[prop$1]; } }
769
 
770
    return modeObj
771
  }
772
 
773
  // This can be used to attach properties to mode objects from
774
  // outside the actual mode definition.
775
  var modeExtensions = {};
776
  function extendMode(mode, properties) {
777
    var exts = modeExtensions.hasOwnProperty(mode) ? modeExtensions[mode] : (modeExtensions[mode] = {});
778
    copyObj(properties, exts);
779
  }
780
 
781
  function copyState(mode, state) {
782
    if (state === true) { return state }
783
    if (mode.copyState) { return mode.copyState(state) }
784
    var nstate = {};
785
    for (var n in state) {
786
      var val = state[n];
787
      if (val instanceof Array) { val = val.concat([]); }
788
      nstate[n] = val;
789
    }
790
    return nstate
791
  }
792
 
793
  // Given a mode and a state (for that mode), find the inner mode and
794
  // state at the position that the state refers to.
795
  function innerMode(mode, state) {
796
    var info;
797
    while (mode.innerMode) {
798
      info = mode.innerMode(state);
799
      if (!info || info.mode == mode) { break }
800
      state = info.state;
801
      mode = info.mode;
802
    }
803
    return info || {mode: mode, state: state}
804
  }
805
 
806
  function startState(mode, a1, a2) {
807
    return mode.startState ? mode.startState(a1, a2) : true
808
  }
809
 
810
  // STRING STREAM
811
 
812
  // Fed to the mode parsers, provides helper functions to make
813
  // parsers more succinct.
814
 
815
  var StringStream = function(string, tabSize, lineOracle) {
816
    this.pos = this.start = 0;
817
    this.string = string;
818
    this.tabSize = tabSize || 8;
819
    this.lastColumnPos = this.lastColumnValue = 0;
820
    this.lineStart = 0;
821
    this.lineOracle = lineOracle;
822
  };
823
 
824
  StringStream.prototype.eol = function () {return this.pos >= this.string.length};
825
  StringStream.prototype.sol = function () {return this.pos == this.lineStart};
826
  StringStream.prototype.peek = function () {return this.string.charAt(this.pos) || undefined};
827
  StringStream.prototype.next = function () {
828
    if (this.pos < this.string.length)
829
      { return this.string.charAt(this.pos++) }
830
  };
831
  StringStream.prototype.eat = function (match) {
832
    var ch = this.string.charAt(this.pos);
833
    var ok;
834
    if (typeof match == "string") { ok = ch == match; }
835
    else { ok = ch && (match.test ? match.test(ch) : match(ch)); }
836
    if (ok) {++this.pos; return ch}
837
  };
838
  StringStream.prototype.eatWhile = function (match) {
839
    var start = this.pos;
840
    while (this.eat(match)){}
841
    return this.pos > start
842
  };
843
  StringStream.prototype.eatSpace = function () {
844
    var start = this.pos;
15152 obado 845
    while (/[\s\u00a0]/.test(this.string.charAt(this.pos))) { ++this.pos; }
14283 obado 846
    return this.pos > start
847
  };
848
  StringStream.prototype.skipToEnd = function () {this.pos = this.string.length;};
849
  StringStream.prototype.skipTo = function (ch) {
850
    var found = this.string.indexOf(ch, this.pos);
851
    if (found > -1) {this.pos = found; return true}
852
  };
853
  StringStream.prototype.backUp = function (n) {this.pos -= n;};
854
  StringStream.prototype.column = function () {
855
    if (this.lastColumnPos < this.start) {
856
      this.lastColumnValue = countColumn(this.string, this.start, this.tabSize, this.lastColumnPos, this.lastColumnValue);
857
      this.lastColumnPos = this.start;
858
    }
859
    return this.lastColumnValue - (this.lineStart ? countColumn(this.string, this.lineStart, this.tabSize) : 0)
860
  };
861
  StringStream.prototype.indentation = function () {
862
    return countColumn(this.string, null, this.tabSize) -
863
      (this.lineStart ? countColumn(this.string, this.lineStart, this.tabSize) : 0)
864
  };
865
  StringStream.prototype.match = function (pattern, consume, caseInsensitive) {
866
    if (typeof pattern == "string") {
867
      var cased = function (str) { return caseInsensitive ? str.toLowerCase() : str; };
868
      var substr = this.string.substr(this.pos, pattern.length);
869
      if (cased(substr) == cased(pattern)) {
870
        if (consume !== false) { this.pos += pattern.length; }
871
        return true
872
      }
873
    } else {
874
      var match = this.string.slice(this.pos).match(pattern);
875
      if (match && match.index > 0) { return null }
876
      if (match && consume !== false) { this.pos += match[0].length; }
877
      return match
878
    }
879
  };
880
  StringStream.prototype.current = function (){return this.string.slice(this.start, this.pos)};
881
  StringStream.prototype.hideFirstChars = function (n, inner) {
882
    this.lineStart += n;
883
    try { return inner() }
884
    finally { this.lineStart -= n; }
885
  };
886
  StringStream.prototype.lookAhead = function (n) {
887
    var oracle = this.lineOracle;
888
    return oracle && oracle.lookAhead(n)
889
  };
890
  StringStream.prototype.baseToken = function () {
891
    var oracle = this.lineOracle;
892
    return oracle && oracle.baseToken(this.pos)
893
  };
894
 
895
  // Find the line object corresponding to the given line number.
896
  function getLine(doc, n) {
897
    n -= doc.first;
898
    if (n < 0 || n >= doc.size) { throw new Error("There is no line " + (n + doc.first) + " in the document.") }
899
    var chunk = doc;
900
    while (!chunk.lines) {
901
      for (var i = 0;; ++i) {
902
        var child = chunk.children[i], sz = child.chunkSize();
903
        if (n < sz) { chunk = child; break }
904
        n -= sz;
905
      }
906
    }
907
    return chunk.lines[n]
908
  }
909
 
910
  // Get the part of a document between two positions, as an array of
911
  // strings.
912
  function getBetween(doc, start, end) {
913
    var out = [], n = start.line;
914
    doc.iter(start.line, end.line + 1, function (line) {
915
      var text = line.text;
916
      if (n == end.line) { text = text.slice(0, end.ch); }
917
      if (n == start.line) { text = text.slice(start.ch); }
918
      out.push(text);
919
      ++n;
920
    });
921
    return out
922
  }
923
  // Get the lines between from and to, as array of strings.
924
  function getLines(doc, from, to) {
925
    var out = [];
926
    doc.iter(from, to, function (line) { out.push(line.text); }); // iter aborts when callback returns truthy value
927
    return out
928
  }
929
 
930
  // Update the height of a line, propagating the height change
931
  // upwards to parent nodes.
932
  function updateLineHeight(line, height) {
933
    var diff = height - line.height;
934
    if (diff) { for (var n = line; n; n = n.parent) { n.height += diff; } }
935
  }
936
 
937
  // Given a line object, find its line number by walking up through
938
  // its parent links.
939
  function lineNo(line) {
940
    if (line.parent == null) { return null }
941
    var cur = line.parent, no = indexOf(cur.lines, line);
942
    for (var chunk = cur.parent; chunk; cur = chunk, chunk = chunk.parent) {
943
      for (var i = 0;; ++i) {
944
        if (chunk.children[i] == cur) { break }
945
        no += chunk.children[i].chunkSize();
946
      }
947
    }
948
    return no + cur.first
949
  }
950
 
951
  // Find the line at the given vertical position, using the height
952
  // information in the document tree.
953
  function lineAtHeight(chunk, h) {
954
    var n = chunk.first;
955
    outer: do {
956
      for (var i$1 = 0; i$1 < chunk.children.length; ++i$1) {
957
        var child = chunk.children[i$1], ch = child.height;
958
        if (h < ch) { chunk = child; continue outer }
959
        h -= ch;
960
        n += child.chunkSize();
961
      }
962
      return n
963
    } while (!chunk.lines)
964
    var i = 0;
965
    for (; i < chunk.lines.length; ++i) {
966
      var line = chunk.lines[i], lh = line.height;
967
      if (h < lh) { break }
968
      h -= lh;
969
    }
970
    return n + i
971
  }
972
 
973
  function isLine(doc, l) {return l >= doc.first && l < doc.first + doc.size}
974
 
975
  function lineNumberFor(options, i) {
976
    return String(options.lineNumberFormatter(i + options.firstLineNumber))
977
  }
978
 
979
  // A Pos instance represents a position within the text.
980
  function Pos(line, ch, sticky) {
981
    if ( sticky === void 0 ) sticky = null;
982
 
983
    if (!(this instanceof Pos)) { return new Pos(line, ch, sticky) }
984
    this.line = line;
985
    this.ch = ch;
986
    this.sticky = sticky;
987
  }
988
 
989
  // Compare two positions, return 0 if they are the same, a negative
990
  // number when a is less, and a positive number otherwise.
991
  function cmp(a, b) { return a.line - b.line || a.ch - b.ch }
992
 
993
  function equalCursorPos(a, b) { return a.sticky == b.sticky && cmp(a, b) == 0 }
994
 
995
  function copyPos(x) {return Pos(x.line, x.ch)}
996
  function maxPos(a, b) { return cmp(a, b) < 0 ? b : a }
997
  function minPos(a, b) { return cmp(a, b) < 0 ? a : b }
998
 
999
  // Most of the external API clips given positions to make sure they
1000
  // actually exist within the document.
1001
  function clipLine(doc, n) {return Math.max(doc.first, Math.min(n, doc.first + doc.size - 1))}
1002
  function clipPos(doc, pos) {
1003
    if (pos.line < doc.first) { return Pos(doc.first, 0) }
1004
    var last = doc.first + doc.size - 1;
1005
    if (pos.line > last) { return Pos(last, getLine(doc, last).text.length) }
1006
    return clipToLen(pos, getLine(doc, pos.line).text.length)
1007
  }
1008
  function clipToLen(pos, linelen) {
1009
    var ch = pos.ch;
1010
    if (ch == null || ch > linelen) { return Pos(pos.line, linelen) }
1011
    else if (ch < 0) { return Pos(pos.line, 0) }
1012
    else { return pos }
1013
  }
1014
  function clipPosArray(doc, array) {
1015
    var out = [];
1016
    for (var i = 0; i < array.length; i++) { out[i] = clipPos(doc, array[i]); }
1017
    return out
1018
  }
1019
 
1020
  var SavedContext = function(state, lookAhead) {
1021
    this.state = state;
1022
    this.lookAhead = lookAhead;
1023
  };
1024
 
1025
  var Context = function(doc, state, line, lookAhead) {
1026
    this.state = state;
1027
    this.doc = doc;
1028
    this.line = line;
1029
    this.maxLookAhead = lookAhead || 0;
1030
    this.baseTokens = null;
1031
    this.baseTokenPos = 1;
1032
  };
1033
 
1034
  Context.prototype.lookAhead = function (n) {
1035
    var line = this.doc.getLine(this.line + n);
1036
    if (line != null && n > this.maxLookAhead) { this.maxLookAhead = n; }
1037
    return line
1038
  };
1039
 
1040
  Context.prototype.baseToken = function (n) {
1041
    if (!this.baseTokens) { return null }
1042
    while (this.baseTokens[this.baseTokenPos] <= n)
15152 obado 1043
      { this.baseTokenPos += 2; }
14283 obado 1044
    var type = this.baseTokens[this.baseTokenPos + 1];
1045
    return {type: type && type.replace(/( |^)overlay .*/, ""),
1046
            size: this.baseTokens[this.baseTokenPos] - n}
1047
  };
1048
 
1049
  Context.prototype.nextLine = function () {
1050
    this.line++;
1051
    if (this.maxLookAhead > 0) { this.maxLookAhead--; }
1052
  };
1053
 
1054
  Context.fromSaved = function (doc, saved, line) {
1055
    if (saved instanceof SavedContext)
1056
      { return new Context(doc, copyState(doc.mode, saved.state), line, saved.lookAhead) }
1057
    else
1058
      { return new Context(doc, copyState(doc.mode, saved), line) }
1059
  };
1060
 
1061
  Context.prototype.save = function (copy) {
1062
    var state = copy !== false ? copyState(this.doc.mode, this.state) : this.state;
1063
    return this.maxLookAhead > 0 ? new SavedContext(state, this.maxLookAhead) : state
1064
  };
1065
 
1066
 
1067
  // Compute a style array (an array starting with a mode generation
1068
  // -- for invalidation -- followed by pairs of end positions and
1069
  // style strings), which is used to highlight the tokens on the
1070
  // line.
1071
  function highlightLine(cm, line, context, forceToEnd) {
1072
    // A styles array always starts with a number identifying the
1073
    // mode/overlays that it is based on (for easy invalidation).
1074
    var st = [cm.state.modeGen], lineClasses = {};
1075
    // Compute the base array of styles
1076
    runMode(cm, line.text, cm.doc.mode, context, function (end, style) { return st.push(end, style); },
1077
            lineClasses, forceToEnd);
1078
    var state = context.state;
1079
 
1080
    // Run overlays, adjust style array.
1081
    var loop = function ( o ) {
1082
      context.baseTokens = st;
1083
      var overlay = cm.state.overlays[o], i = 1, at = 0;
1084
      context.state = true;
1085
      runMode(cm, line.text, overlay.mode, context, function (end, style) {
1086
        var start = i;
1087
        // Ensure there's a token end at the current position, and that i points at it
1088
        while (at < end) {
1089
          var i_end = st[i];
1090
          if (i_end > end)
1091
            { st.splice(i, 1, end, st[i+1], i_end); }
1092
          i += 2;
1093
          at = Math.min(end, i_end);
1094
        }
1095
        if (!style) { return }
1096
        if (overlay.opaque) {
1097
          st.splice(start, i - start, end, "overlay " + style);
1098
          i = start + 2;
1099
        } else {
1100
          for (; start < i; start += 2) {
1101
            var cur = st[start+1];
1102
            st[start+1] = (cur ? cur + " " : "") + "overlay " + style;
1103
          }
1104
        }
1105
      }, lineClasses);
1106
      context.state = state;
1107
      context.baseTokens = null;
1108
      context.baseTokenPos = 1;
1109
    };
1110
 
1111
    for (var o = 0; o < cm.state.overlays.length; ++o) loop( o );
1112
 
1113
    return {styles: st, classes: lineClasses.bgClass || lineClasses.textClass ? lineClasses : null}
1114
  }
1115
 
1116
  function getLineStyles(cm, line, updateFrontier) {
1117
    if (!line.styles || line.styles[0] != cm.state.modeGen) {
1118
      var context = getContextBefore(cm, lineNo(line));
1119
      var resetState = line.text.length > cm.options.maxHighlightLength && copyState(cm.doc.mode, context.state);
1120
      var result = highlightLine(cm, line, context);
1121
      if (resetState) { context.state = resetState; }
1122
      line.stateAfter = context.save(!resetState);
1123
      line.styles = result.styles;
1124
      if (result.classes) { line.styleClasses = result.classes; }
1125
      else if (line.styleClasses) { line.styleClasses = null; }
1126
      if (updateFrontier === cm.doc.highlightFrontier)
1127
        { cm.doc.modeFrontier = Math.max(cm.doc.modeFrontier, ++cm.doc.highlightFrontier); }
1128
    }
1129
    return line.styles
1130
  }
1131
 
1132
  function getContextBefore(cm, n, precise) {
1133
    var doc = cm.doc, display = cm.display;
1134
    if (!doc.mode.startState) { return new Context(doc, true, n) }
1135
    var start = findStartLine(cm, n, precise);
1136
    var saved = start > doc.first && getLine(doc, start - 1).stateAfter;
1137
    var context = saved ? Context.fromSaved(doc, saved, start) : new Context(doc, startState(doc.mode), start);
1138
 
1139
    doc.iter(start, n, function (line) {
1140
      processLine(cm, line.text, context);
1141
      var pos = context.line;
1142
      line.stateAfter = pos == n - 1 || pos % 5 == 0 || pos >= display.viewFrom && pos < display.viewTo ? context.save() : null;
1143
      context.nextLine();
1144
    });
1145
    if (precise) { doc.modeFrontier = context.line; }
1146
    return context
1147
  }
1148
 
1149
  // Lightweight form of highlight -- proceed over this line and
1150
  // update state, but don't save a style array. Used for lines that
1151
  // aren't currently visible.
1152
  function processLine(cm, text, context, startAt) {
1153
    var mode = cm.doc.mode;
1154
    var stream = new StringStream(text, cm.options.tabSize, context);
1155
    stream.start = stream.pos = startAt || 0;
1156
    if (text == "") { callBlankLine(mode, context.state); }
1157
    while (!stream.eol()) {
1158
      readToken(mode, stream, context.state);
1159
      stream.start = stream.pos;
1160
    }
1161
  }
1162
 
1163
  function callBlankLine(mode, state) {
1164
    if (mode.blankLine) { return mode.blankLine(state) }
1165
    if (!mode.innerMode) { return }
1166
    var inner = innerMode(mode, state);
1167
    if (inner.mode.blankLine) { return inner.mode.blankLine(inner.state) }
1168
  }
1169
 
1170
  function readToken(mode, stream, state, inner) {
1171
    for (var i = 0; i < 10; i++) {
1172
      if (inner) { inner[0] = innerMode(mode, state).mode; }
1173
      var style = mode.token(stream, state);
1174
      if (stream.pos > stream.start) { return style }
1175
    }
1176
    throw new Error("Mode " + mode.name + " failed to advance stream.")
1177
  }
1178
 
1179
  var Token = function(stream, type, state) {
1180
    this.start = stream.start; this.end = stream.pos;
1181
    this.string = stream.current();
1182
    this.type = type || null;
1183
    this.state = state;
1184
  };
1185
 
1186
  // Utility for getTokenAt and getLineTokens
1187
  function takeToken(cm, pos, precise, asArray) {
1188
    var doc = cm.doc, mode = doc.mode, style;
1189
    pos = clipPos(doc, pos);
1190
    var line = getLine(doc, pos.line), context = getContextBefore(cm, pos.line, precise);
1191
    var stream = new StringStream(line.text, cm.options.tabSize, context), tokens;
1192
    if (asArray) { tokens = []; }
1193
    while ((asArray || stream.pos < pos.ch) && !stream.eol()) {
1194
      stream.start = stream.pos;
1195
      style = readToken(mode, stream, context.state);
1196
      if (asArray) { tokens.push(new Token(stream, style, copyState(doc.mode, context.state))); }
1197
    }
1198
    return asArray ? tokens : new Token(stream, style, context.state)
1199
  }
1200
 
1201
  function extractLineClasses(type, output) {
1202
    if (type) { for (;;) {
1203
      var lineClass = type.match(/(?:^|\s+)line-(background-)?(\S+)/);
1204
      if (!lineClass) { break }
1205
      type = type.slice(0, lineClass.index) + type.slice(lineClass.index + lineClass[0].length);
1206
      var prop = lineClass[1] ? "bgClass" : "textClass";
1207
      if (output[prop] == null)
1208
        { output[prop] = lineClass[2]; }
15152 obado 1209
      else if (!(new RegExp("(?:^|\\s)" + lineClass[2] + "(?:$|\\s)")).test(output[prop]))
14283 obado 1210
        { output[prop] += " " + lineClass[2]; }
1211
    } }
1212
    return type
1213
  }
1214
 
1215
  // Run the given mode's parser over a line, calling f for each token.
1216
  function runMode(cm, text, mode, context, f, lineClasses, forceToEnd) {
1217
    var flattenSpans = mode.flattenSpans;
1218
    if (flattenSpans == null) { flattenSpans = cm.options.flattenSpans; }
1219
    var curStart = 0, curStyle = null;
1220
    var stream = new StringStream(text, cm.options.tabSize, context), style;
1221
    var inner = cm.options.addModeClass && [null];
1222
    if (text == "") { extractLineClasses(callBlankLine(mode, context.state), lineClasses); }
1223
    while (!stream.eol()) {
1224
      if (stream.pos > cm.options.maxHighlightLength) {
1225
        flattenSpans = false;
1226
        if (forceToEnd) { processLine(cm, text, context, stream.pos); }
1227
        stream.pos = text.length;
1228
        style = null;
1229
      } else {
1230
        style = extractLineClasses(readToken(mode, stream, context.state, inner), lineClasses);
1231
      }
1232
      if (inner) {
1233
        var mName = inner[0].name;
1234
        if (mName) { style = "m-" + (style ? mName + " " + style : mName); }
1235
      }
1236
      if (!flattenSpans || curStyle != style) {
1237
        while (curStart < stream.start) {
1238
          curStart = Math.min(stream.start, curStart + 5000);
1239
          f(curStart, curStyle);
1240
        }
1241
        curStyle = style;
1242
      }
1243
      stream.start = stream.pos;
1244
    }
1245
    while (curStart < stream.pos) {
1246
      // Webkit seems to refuse to render text nodes longer than 57444
1247
      // characters, and returns inaccurate measurements in nodes
1248
      // starting around 5000 chars.
1249
      var pos = Math.min(stream.pos, curStart + 5000);
1250
      f(pos, curStyle);
1251
      curStart = pos;
1252
    }
1253
  }
1254
 
1255
  // Finds the line to start with when starting a parse. Tries to
1256
  // find a line with a stateAfter, so that it can start with a
1257
  // valid state. If that fails, it returns the line with the
1258
  // smallest indentation, which tends to need the least context to
1259
  // parse correctly.
1260
  function findStartLine(cm, n, precise) {
1261
    var minindent, minline, doc = cm.doc;
1262
    var lim = precise ? -1 : n - (cm.doc.mode.innerMode ? 1000 : 100);
1263
    for (var search = n; search > lim; --search) {
1264
      if (search <= doc.first) { return doc.first }
1265
      var line = getLine(doc, search - 1), after = line.stateAfter;
1266
      if (after && (!precise || search + (after instanceof SavedContext ? after.lookAhead : 0) <= doc.modeFrontier))
1267
        { return search }
1268
      var indented = countColumn(line.text, null, cm.options.tabSize);
1269
      if (minline == null || minindent > indented) {
1270
        minline = search - 1;
1271
        minindent = indented;
1272
      }
1273
    }
1274
    return minline
1275
  }
1276
 
1277
  function retreatFrontier(doc, n) {
1278
    doc.modeFrontier = Math.min(doc.modeFrontier, n);
1279
    if (doc.highlightFrontier < n - 10) { return }
1280
    var start = doc.first;
1281
    for (var line = n - 1; line > start; line--) {
1282
      var saved = getLine(doc, line).stateAfter;
1283
      // change is on 3
1284
      // state on line 1 looked ahead 2 -- so saw 3
1285
      // test 1 + 2 < 3 should cover this
1286
      if (saved && (!(saved instanceof SavedContext) || line + saved.lookAhead < n)) {
1287
        start = line + 1;
1288
        break
1289
      }
1290
    }
1291
    doc.highlightFrontier = Math.min(doc.highlightFrontier, start);
1292
  }
1293
 
1294
  // Optimize some code when these features are not used.
1295
  var sawReadOnlySpans = false, sawCollapsedSpans = false;
1296
 
1297
  function seeReadOnlySpans() {
1298
    sawReadOnlySpans = true;
1299
  }
1300
 
1301
  function seeCollapsedSpans() {
1302
    sawCollapsedSpans = true;
1303
  }
1304
 
1305
  // TEXTMARKER SPANS
1306
 
1307
  function MarkedSpan(marker, from, to) {
1308
    this.marker = marker;
1309
    this.from = from; this.to = to;
1310
  }
1311
 
1312
  // Search an array of spans for a span matching the given marker.
1313
  function getMarkedSpanFor(spans, marker) {
1314
    if (spans) { for (var i = 0; i < spans.length; ++i) {
1315
      var span = spans[i];
1316
      if (span.marker == marker) { return span }
1317
    } }
1318
  }
16493 obado 1319
 
14283 obado 1320
  // Remove a span from an array, returning undefined if no spans are
1321
  // left (we don't store arrays for lines without spans).
1322
  function removeMarkedSpan(spans, span) {
1323
    var r;
1324
    for (var i = 0; i < spans.length; ++i)
1325
      { if (spans[i] != span) { (r || (r = [])).push(spans[i]); } }
1326
    return r
1327
  }
16493 obado 1328
 
14283 obado 1329
  // Add a span to a line.
16493 obado 1330
  function addMarkedSpan(line, span, op) {
1331
    var inThisOp = op && window.WeakSet && (op.markedSpans || (op.markedSpans = new WeakSet));
16859 obado 1332
    if (inThisOp && line.markedSpans && inThisOp.has(line.markedSpans)) {
16493 obado 1333
      line.markedSpans.push(span);
1334
    } else {
1335
      line.markedSpans = line.markedSpans ? line.markedSpans.concat([span]) : [span];
1336
      if (inThisOp) { inThisOp.add(line.markedSpans); }
1337
    }
14283 obado 1338
    span.marker.attachLine(line);
1339
  }
1340
 
1341
  // Used for the algorithm that adjusts markers for a change in the
1342
  // document. These functions cut an array of spans at a given
1343
  // character position, returning an array of remaining chunks (or
1344
  // undefined if nothing remains).
1345
  function markedSpansBefore(old, startCh, isInsert) {
1346
    var nw;
1347
    if (old) { for (var i = 0; i < old.length; ++i) {
1348
      var span = old[i], marker = span.marker;
1349
      var startsBefore = span.from == null || (marker.inclusiveLeft ? span.from <= startCh : span.from < startCh);
1350
      if (startsBefore || span.from == startCh && marker.type == "bookmark" && (!isInsert || !span.marker.insertLeft)) {
1351
        var endsAfter = span.to == null || (marker.inclusiveRight ? span.to >= startCh : span.to > startCh)
1352
        ;(nw || (nw = [])).push(new MarkedSpan(marker, span.from, endsAfter ? null : span.to));
1353
      }
1354
    } }
1355
    return nw
1356
  }
1357
  function markedSpansAfter(old, endCh, isInsert) {
1358
    var nw;
1359
    if (old) { for (var i = 0; i < old.length; ++i) {
1360
      var span = old[i], marker = span.marker;
1361
      var endsAfter = span.to == null || (marker.inclusiveRight ? span.to >= endCh : span.to > endCh);
1362
      if (endsAfter || span.from == endCh && marker.type == "bookmark" && (!isInsert || span.marker.insertLeft)) {
1363
        var startsBefore = span.from == null || (marker.inclusiveLeft ? span.from <= endCh : span.from < endCh)
1364
        ;(nw || (nw = [])).push(new MarkedSpan(marker, startsBefore ? null : span.from - endCh,
1365
                                              span.to == null ? null : span.to - endCh));
1366
      }
1367
    } }
1368
    return nw
1369
  }
1370
 
1371
  // Given a change object, compute the new set of marker spans that
1372
  // cover the line in which the change took place. Removes spans
1373
  // entirely within the change, reconnects spans belonging to the
1374
  // same marker that appear on both sides of the change, and cuts off
1375
  // spans partially within the change. Returns an array of span
1376
  // arrays with one element for each line in (after) the change.
1377
  function stretchSpansOverChange(doc, change) {
1378
    if (change.full) { return null }
1379
    var oldFirst = isLine(doc, change.from.line) && getLine(doc, change.from.line).markedSpans;
1380
    var oldLast = isLine(doc, change.to.line) && getLine(doc, change.to.line).markedSpans;
1381
    if (!oldFirst && !oldLast) { return null }
1382
 
1383
    var startCh = change.from.ch, endCh = change.to.ch, isInsert = cmp(change.from, change.to) == 0;
1384
    // Get the spans that 'stick out' on both sides
1385
    var first = markedSpansBefore(oldFirst, startCh, isInsert);
1386
    var last = markedSpansAfter(oldLast, endCh, isInsert);
1387
 
1388
    // Next, merge those two ends
1389
    var sameLine = change.text.length == 1, offset = lst(change.text).length + (sameLine ? startCh : 0);
1390
    if (first) {
1391
      // Fix up .to properties of first
1392
      for (var i = 0; i < first.length; ++i) {
1393
        var span = first[i];
1394
        if (span.to == null) {
1395
          var found = getMarkedSpanFor(last, span.marker);
1396
          if (!found) { span.to = startCh; }
1397
          else if (sameLine) { span.to = found.to == null ? null : found.to + offset; }
1398
        }
1399
      }
1400
    }
1401
    if (last) {
1402
      // Fix up .from in last (or move them into first in case of sameLine)
1403
      for (var i$1 = 0; i$1 < last.length; ++i$1) {
1404
        var span$1 = last[i$1];
1405
        if (span$1.to != null) { span$1.to += offset; }
1406
        if (span$1.from == null) {
1407
          var found$1 = getMarkedSpanFor(first, span$1.marker);
1408
          if (!found$1) {
1409
            span$1.from = offset;
1410
            if (sameLine) { (first || (first = [])).push(span$1); }
1411
          }
1412
        } else {
1413
          span$1.from += offset;
1414
          if (sameLine) { (first || (first = [])).push(span$1); }
1415
        }
1416
      }
1417
    }
1418
    // Make sure we didn't create any zero-length spans
1419
    if (first) { first = clearEmptySpans(first); }
1420
    if (last && last != first) { last = clearEmptySpans(last); }
1421
 
1422
    var newMarkers = [first];
1423
    if (!sameLine) {
1424
      // Fill gap with whole-line-spans
1425
      var gap = change.text.length - 2, gapMarkers;
1426
      if (gap > 0 && first)
1427
        { for (var i$2 = 0; i$2 < first.length; ++i$2)
1428
          { if (first[i$2].to == null)
1429
            { (gapMarkers || (gapMarkers = [])).push(new MarkedSpan(first[i$2].marker, null, null)); } } }
1430
      for (var i$3 = 0; i$3 < gap; ++i$3)
1431
        { newMarkers.push(gapMarkers); }
1432
      newMarkers.push(last);
1433
    }
1434
    return newMarkers
1435
  }
1436
 
1437
  // Remove spans that are empty and don't have a clearWhenEmpty
1438
  // option of false.
1439
  function clearEmptySpans(spans) {
1440
    for (var i = 0; i < spans.length; ++i) {
1441
      var span = spans[i];
1442
      if (span.from != null && span.from == span.to && span.marker.clearWhenEmpty !== false)
1443
        { spans.splice(i--, 1); }
1444
    }
1445
    if (!spans.length) { return null }
1446
    return spans
1447
  }
1448
 
1449
  // Used to 'clip' out readOnly ranges when making a change.
1450
  function removeReadOnlyRanges(doc, from, to) {
1451
    var markers = null;
1452
    doc.iter(from.line, to.line + 1, function (line) {
1453
      if (line.markedSpans) { for (var i = 0; i < line.markedSpans.length; ++i) {
1454
        var mark = line.markedSpans[i].marker;
1455
        if (mark.readOnly && (!markers || indexOf(markers, mark) == -1))
1456
          { (markers || (markers = [])).push(mark); }
1457
      } }
1458
    });
1459
    if (!markers) { return null }
1460
    var parts = [{from: from, to: to}];
1461
    for (var i = 0; i < markers.length; ++i) {
1462
      var mk = markers[i], m = mk.find(0);
1463
      for (var j = 0; j < parts.length; ++j) {
1464
        var p = parts[j];
1465
        if (cmp(p.to, m.from) < 0 || cmp(p.from, m.to) > 0) { continue }
1466
        var newParts = [j, 1], dfrom = cmp(p.from, m.from), dto = cmp(p.to, m.to);
1467
        if (dfrom < 0 || !mk.inclusiveLeft && !dfrom)
1468
          { newParts.push({from: p.from, to: m.from}); }
1469
        if (dto > 0 || !mk.inclusiveRight && !dto)
1470
          { newParts.push({from: m.to, to: p.to}); }
1471
        parts.splice.apply(parts, newParts);
1472
        j += newParts.length - 3;
1473
      }
1474
    }
1475
    return parts
1476
  }
1477
 
1478
  // Connect or disconnect spans from a line.
1479
  function detachMarkedSpans(line) {
1480
    var spans = line.markedSpans;
1481
    if (!spans) { return }
1482
    for (var i = 0; i < spans.length; ++i)
1483
      { spans[i].marker.detachLine(line); }
1484
    line.markedSpans = null;
1485
  }
1486
  function attachMarkedSpans(line, spans) {
1487
    if (!spans) { return }
1488
    for (var i = 0; i < spans.length; ++i)
1489
      { spans[i].marker.attachLine(line); }
1490
    line.markedSpans = spans;
1491
  }
1492
 
1493
  // Helpers used when computing which overlapping collapsed span
1494
  // counts as the larger one.
1495
  function extraLeft(marker) { return marker.inclusiveLeft ? -1 : 0 }
1496
  function extraRight(marker) { return marker.inclusiveRight ? 1 : 0 }
1497
 
1498
  // Returns a number indicating which of two overlapping collapsed
1499
  // spans is larger (and thus includes the other). Falls back to
1500
  // comparing ids when the spans cover exactly the same range.
1501
  function compareCollapsedMarkers(a, b) {
1502
    var lenDiff = a.lines.length - b.lines.length;
1503
    if (lenDiff != 0) { return lenDiff }
1504
    var aPos = a.find(), bPos = b.find();
1505
    var fromCmp = cmp(aPos.from, bPos.from) || extraLeft(a) - extraLeft(b);
1506
    if (fromCmp) { return -fromCmp }
1507
    var toCmp = cmp(aPos.to, bPos.to) || extraRight(a) - extraRight(b);
1508
    if (toCmp) { return toCmp }
1509
    return b.id - a.id
1510
  }
1511
 
1512
  // Find out whether a line ends or starts in a collapsed span. If
1513
  // so, return the marker for that span.
1514
  function collapsedSpanAtSide(line, start) {
1515
    var sps = sawCollapsedSpans && line.markedSpans, found;
1516
    if (sps) { for (var sp = (void 0), i = 0; i < sps.length; ++i) {
1517
      sp = sps[i];
1518
      if (sp.marker.collapsed && (start ? sp.from : sp.to) == null &&
1519
          (!found || compareCollapsedMarkers(found, sp.marker) < 0))
1520
        { found = sp.marker; }
1521
    } }
1522
    return found
1523
  }
1524
  function collapsedSpanAtStart(line) { return collapsedSpanAtSide(line, true) }
1525
  function collapsedSpanAtEnd(line) { return collapsedSpanAtSide(line, false) }
1526
 
1527
  function collapsedSpanAround(line, ch) {
1528
    var sps = sawCollapsedSpans && line.markedSpans, found;
1529
    if (sps) { for (var i = 0; i < sps.length; ++i) {
1530
      var sp = sps[i];
1531
      if (sp.marker.collapsed && (sp.from == null || sp.from < ch) && (sp.to == null || sp.to > ch) &&
1532
          (!found || compareCollapsedMarkers(found, sp.marker) < 0)) { found = sp.marker; }
1533
    } }
1534
    return found
1535
  }
1536
 
1537
  // Test whether there exists a collapsed span that partially
1538
  // overlaps (covers the start or end, but not both) of a new span.
1539
  // Such overlap is not allowed.
15152 obado 1540
  function conflictingCollapsedRange(doc, lineNo, from, to, marker) {
1541
    var line = getLine(doc, lineNo);
14283 obado 1542
    var sps = sawCollapsedSpans && line.markedSpans;
1543
    if (sps) { for (var i = 0; i < sps.length; ++i) {
1544
      var sp = sps[i];
1545
      if (!sp.marker.collapsed) { continue }
1546
      var found = sp.marker.find(0);
1547
      var fromCmp = cmp(found.from, from) || extraLeft(sp.marker) - extraLeft(marker);
1548
      var toCmp = cmp(found.to, to) || extraRight(sp.marker) - extraRight(marker);
1549
      if (fromCmp >= 0 && toCmp <= 0 || fromCmp <= 0 && toCmp >= 0) { continue }
1550
      if (fromCmp <= 0 && (sp.marker.inclusiveRight && marker.inclusiveLeft ? cmp(found.to, from) >= 0 : cmp(found.to, from) > 0) ||
1551
          fromCmp >= 0 && (sp.marker.inclusiveRight && marker.inclusiveLeft ? cmp(found.from, to) <= 0 : cmp(found.from, to) < 0))
1552
        { return true }
1553
    } }
1554
  }
1555
 
1556
  // A visual line is a line as drawn on the screen. Folding, for
1557
  // example, can cause multiple logical lines to appear on the same
1558
  // visual line. This finds the start of the visual line that the
1559
  // given line is part of (usually that is the line itself).
1560
  function visualLine(line) {
1561
    var merged;
1562
    while (merged = collapsedSpanAtStart(line))
1563
      { line = merged.find(-1, true).line; }
1564
    return line
1565
  }
1566
 
1567
  function visualLineEnd(line) {
1568
    var merged;
1569
    while (merged = collapsedSpanAtEnd(line))
1570
      { line = merged.find(1, true).line; }
1571
    return line
1572
  }
1573
 
1574
  // Returns an array of logical lines that continue the visual line
1575
  // started by the argument, or undefined if there are no such lines.
1576
  function visualLineContinued(line) {
1577
    var merged, lines;
1578
    while (merged = collapsedSpanAtEnd(line)) {
1579
      line = merged.find(1, true).line
1580
      ;(lines || (lines = [])).push(line);
1581
    }
1582
    return lines
1583
  }
1584
 
1585
  // Get the line number of the start of the visual line that the
1586
  // given line number is part of.
1587
  function visualLineNo(doc, lineN) {
1588
    var line = getLine(doc, lineN), vis = visualLine(line);
1589
    if (line == vis) { return lineN }
1590
    return lineNo(vis)
1591
  }
1592
 
1593
  // Get the line number of the start of the next visual line after
1594
  // the given line.
1595
  function visualLineEndNo(doc, lineN) {
1596
    if (lineN > doc.lastLine()) { return lineN }
1597
    var line = getLine(doc, lineN), merged;
1598
    if (!lineIsHidden(doc, line)) { return lineN }
1599
    while (merged = collapsedSpanAtEnd(line))
1600
      { line = merged.find(1, true).line; }
1601
    return lineNo(line) + 1
1602
  }
1603
 
1604
  // Compute whether a line is hidden. Lines count as hidden when they
1605
  // are part of a visual line that starts with another line, or when
1606
  // they are entirely covered by collapsed, non-widget span.
1607
  function lineIsHidden(doc, line) {
1608
    var sps = sawCollapsedSpans && line.markedSpans;
1609
    if (sps) { for (var sp = (void 0), i = 0; i < sps.length; ++i) {
1610
      sp = sps[i];
1611
      if (!sp.marker.collapsed) { continue }
1612
      if (sp.from == null) { return true }
1613
      if (sp.marker.widgetNode) { continue }
1614
      if (sp.from == 0 && sp.marker.inclusiveLeft && lineIsHiddenInner(doc, line, sp))
1615
        { return true }
1616
    } }
1617
  }
1618
  function lineIsHiddenInner(doc, line, span) {
1619
    if (span.to == null) {
1620
      var end = span.marker.find(1, true);
1621
      return lineIsHiddenInner(doc, end.line, getMarkedSpanFor(end.line.markedSpans, span.marker))
1622
    }
1623
    if (span.marker.inclusiveRight && span.to == line.text.length)
1624
      { return true }
1625
    for (var sp = (void 0), i = 0; i < line.markedSpans.length; ++i) {
1626
      sp = line.markedSpans[i];
1627
      if (sp.marker.collapsed && !sp.marker.widgetNode && sp.from == span.to &&
1628
          (sp.to == null || sp.to != span.from) &&
1629
          (sp.marker.inclusiveLeft || span.marker.inclusiveRight) &&
1630
          lineIsHiddenInner(doc, line, sp)) { return true }
1631
    }
1632
  }
1633
 
1634
  // Find the height above the given line.
1635
  function heightAtLine(lineObj) {
1636
    lineObj = visualLine(lineObj);
1637
 
1638
    var h = 0, chunk = lineObj.parent;
1639
    for (var i = 0; i < chunk.lines.length; ++i) {
1640
      var line = chunk.lines[i];
1641
      if (line == lineObj) { break }
1642
      else { h += line.height; }
1643
    }
1644
    for (var p = chunk.parent; p; chunk = p, p = chunk.parent) {
1645
      for (var i$1 = 0; i$1 < p.children.length; ++i$1) {
1646
        var cur = p.children[i$1];
1647
        if (cur == chunk) { break }
1648
        else { h += cur.height; }
1649
      }
1650
    }
1651
    return h
1652
  }
1653
 
1654
  // Compute the character length of a line, taking into account
1655
  // collapsed ranges (see markText) that might hide parts, and join
1656
  // other lines onto it.
1657
  function lineLength(line) {
1658
    if (line.height == 0) { return 0 }
1659
    var len = line.text.length, merged, cur = line;
1660
    while (merged = collapsedSpanAtStart(cur)) {
1661
      var found = merged.find(0, true);
1662
      cur = found.from.line;
1663
      len += found.from.ch - found.to.ch;
1664
    }
1665
    cur = line;
1666
    while (merged = collapsedSpanAtEnd(cur)) {
1667
      var found$1 = merged.find(0, true);
1668
      len -= cur.text.length - found$1.from.ch;
1669
      cur = found$1.to.line;
1670
      len += cur.text.length - found$1.to.ch;
1671
    }
1672
    return len
1673
  }
1674
 
1675
  // Find the longest line in the document.
1676
  function findMaxLine(cm) {
1677
    var d = cm.display, doc = cm.doc;
1678
    d.maxLine = getLine(doc, doc.first);
1679
    d.maxLineLength = lineLength(d.maxLine);
1680
    d.maxLineChanged = true;
1681
    doc.iter(function (line) {
1682
      var len = lineLength(line);
1683
      if (len > d.maxLineLength) {
1684
        d.maxLineLength = len;
1685
        d.maxLine = line;
1686
      }
1687
    });
1688
  }
1689
 
1690
  // LINE DATA STRUCTURE
1691
 
1692
  // Line objects. These hold state related to a line, including
1693
  // highlighting info (the styles array).
1694
  var Line = function(text, markedSpans, estimateHeight) {
1695
    this.text = text;
1696
    attachMarkedSpans(this, markedSpans);
1697
    this.height = estimateHeight ? estimateHeight(this) : 1;
1698
  };
1699
 
1700
  Line.prototype.lineNo = function () { return lineNo(this) };
1701
  eventMixin(Line);
1702
 
1703
  // Change the content (text, markers) of a line. Automatically
1704
  // invalidates cached information and tries to re-estimate the
1705
  // line's height.
1706
  function updateLine(line, text, markedSpans, estimateHeight) {
1707
    line.text = text;
1708
    if (line.stateAfter) { line.stateAfter = null; }
1709
    if (line.styles) { line.styles = null; }
1710
    if (line.order != null) { line.order = null; }
1711
    detachMarkedSpans(line);
1712
    attachMarkedSpans(line, markedSpans);
1713
    var estHeight = estimateHeight ? estimateHeight(line) : 1;
1714
    if (estHeight != line.height) { updateLineHeight(line, estHeight); }
1715
  }
1716
 
1717
  // Detach a line from the document tree and its markers.
1718
  function cleanUpLine(line) {
1719
    line.parent = null;
1720
    detachMarkedSpans(line);
1721
  }
1722
 
1723
  // Convert a style as returned by a mode (either null, or a string
1724
  // containing one or more styles) to a CSS style. This is cached,
1725
  // and also looks for line-wide styles.
1726
  var styleToClassCache = {}, styleToClassCacheWithMode = {};
1727
  function interpretTokenStyle(style, options) {
1728
    if (!style || /^\s*$/.test(style)) { return null }
1729
    var cache = options.addModeClass ? styleToClassCacheWithMode : styleToClassCache;
1730
    return cache[style] ||
1731
      (cache[style] = style.replace(/\S+/g, "cm-$&"))
1732
  }
1733
 
1734
  // Render the DOM representation of the text of a line. Also builds
1735
  // up a 'line map', which points at the DOM nodes that represent
1736
  // specific stretches of text, and is used by the measuring code.
1737
  // The returned object contains the DOM node, this map, and
1738
  // information about line-wide styles that were set by the mode.
1739
  function buildLineContent(cm, lineView) {
1740
    // The padding-right forces the element to have a 'border', which
1741
    // is needed on Webkit to be able to get line-level bounding
1742
    // rectangles for it (in measureChar).
1743
    var content = eltP("span", null, null, webkit ? "padding-right: .1px" : null);
1744
    var builder = {pre: eltP("pre", [content], "CodeMirror-line"), content: content,
1745
                   col: 0, pos: 0, cm: cm,
1746
                   trailingSpace: false,
1747
                   splitSpaces: cm.getOption("lineWrapping")};
1748
    lineView.measure = {};
1749
 
1750
    // Iterate over the logical lines that make up this visual line.
1751
    for (var i = 0; i <= (lineView.rest ? lineView.rest.length : 0); i++) {
1752
      var line = i ? lineView.rest[i - 1] : lineView.line, order = (void 0);
1753
      builder.pos = 0;
1754
      builder.addToken = buildToken;
1755
      // Optionally wire in some hacks into the token-rendering
1756
      // algorithm, to deal with browser quirks.
1757
      if (hasBadBidiRects(cm.display.measure) && (order = getOrder(line, cm.doc.direction)))
1758
        { builder.addToken = buildTokenBadBidi(builder.addToken, order); }
1759
      builder.map = [];
1760
      var allowFrontierUpdate = lineView != cm.display.externalMeasured && lineNo(line);
1761
      insertLineContent(line, builder, getLineStyles(cm, line, allowFrontierUpdate));
1762
      if (line.styleClasses) {
1763
        if (line.styleClasses.bgClass)
1764
          { builder.bgClass = joinClasses(line.styleClasses.bgClass, builder.bgClass || ""); }
1765
        if (line.styleClasses.textClass)
1766
          { builder.textClass = joinClasses(line.styleClasses.textClass, builder.textClass || ""); }
1767
      }
1768
 
1769
      // Ensure at least a single node is present, for measuring.
1770
      if (builder.map.length == 0)
1771
        { builder.map.push(0, 0, builder.content.appendChild(zeroWidthElement(cm.display.measure))); }
1772
 
1773
      // Store the map and a cache object for the current logical line
1774
      if (i == 0) {
1775
        lineView.measure.map = builder.map;
1776
        lineView.measure.cache = {};
1777
      } else {
1778
  (lineView.measure.maps || (lineView.measure.maps = [])).push(builder.map)
1779
        ;(lineView.measure.caches || (lineView.measure.caches = [])).push({});
1780
      }
1781
    }
1782
 
1783
    // See issue #2901
1784
    if (webkit) {
1785
      var last = builder.content.lastChild;
1786
      if (/\bcm-tab\b/.test(last.className) || (last.querySelector && last.querySelector(".cm-tab")))
1787
        { builder.content.className = "cm-tab-wrap-hack"; }
1788
    }
1789
 
1790
    signal(cm, "renderLine", cm, lineView.line, builder.pre);
1791
    if (builder.pre.className)
1792
      { builder.textClass = joinClasses(builder.pre.className, builder.textClass || ""); }
1793
 
1794
    return builder
1795
  }
1796
 
1797
  function defaultSpecialCharPlaceholder(ch) {
1798
    var token = elt("span", "\u2022", "cm-invalidchar");
1799
    token.title = "\\u" + ch.charCodeAt(0).toString(16);
1800
    token.setAttribute("aria-label", token.title);
1801
    return token
1802
  }
1803
 
1804
  // Build up the DOM representation for a single token, and add it to
1805
  // the line map. Takes care to render special characters separately.
1806
  function buildToken(builder, text, style, startStyle, endStyle, css, attributes) {
1807
    if (!text) { return }
1808
    var displayText = builder.splitSpaces ? splitSpaces(text, builder.trailingSpace) : text;
1809
    var special = builder.cm.state.specialChars, mustWrap = false;
1810
    var content;
1811
    if (!special.test(text)) {
1812
      builder.col += text.length;
1813
      content = document.createTextNode(displayText);
1814
      builder.map.push(builder.pos, builder.pos + text.length, content);
1815
      if (ie && ie_version < 9) { mustWrap = true; }
1816
      builder.pos += text.length;
1817
    } else {
1818
      content = document.createDocumentFragment();
1819
      var pos = 0;
1820
      while (true) {
1821
        special.lastIndex = pos;
1822
        var m = special.exec(text);
1823
        var skipped = m ? m.index - pos : text.length - pos;
1824
        if (skipped) {
1825
          var txt = document.createTextNode(displayText.slice(pos, pos + skipped));
1826
          if (ie && ie_version < 9) { content.appendChild(elt("span", [txt])); }
1827
          else { content.appendChild(txt); }
1828
          builder.map.push(builder.pos, builder.pos + skipped, txt);
1829
          builder.col += skipped;
1830
          builder.pos += skipped;
1831
        }
1832
        if (!m) { break }
1833
        pos += skipped + 1;
1834
        var txt$1 = (void 0);
1835
        if (m[0] == "\t") {
1836
          var tabSize = builder.cm.options.tabSize, tabWidth = tabSize - builder.col % tabSize;
1837
          txt$1 = content.appendChild(elt("span", spaceStr(tabWidth), "cm-tab"));
1838
          txt$1.setAttribute("role", "presentation");
1839
          txt$1.setAttribute("cm-text", "\t");
1840
          builder.col += tabWidth;
1841
        } else if (m[0] == "\r" || m[0] == "\n") {
1842
          txt$1 = content.appendChild(elt("span", m[0] == "\r" ? "\u240d" : "\u2424", "cm-invalidchar"));
1843
          txt$1.setAttribute("cm-text", m[0]);
1844
          builder.col += 1;
1845
        } else {
1846
          txt$1 = builder.cm.options.specialCharPlaceholder(m[0]);
1847
          txt$1.setAttribute("cm-text", m[0]);
1848
          if (ie && ie_version < 9) { content.appendChild(elt("span", [txt$1])); }
1849
          else { content.appendChild(txt$1); }
1850
          builder.col += 1;
1851
        }
1852
        builder.map.push(builder.pos, builder.pos + 1, txt$1);
1853
        builder.pos++;
1854
      }
1855
    }
1856
    builder.trailingSpace = displayText.charCodeAt(text.length - 1) == 32;
16493 obado 1857
    if (style || startStyle || endStyle || mustWrap || css || attributes) {
14283 obado 1858
      var fullStyle = style || "";
1859
      if (startStyle) { fullStyle += startStyle; }
1860
      if (endStyle) { fullStyle += endStyle; }
1861
      var token = elt("span", [content], fullStyle, css);
1862
      if (attributes) {
1863
        for (var attr in attributes) { if (attributes.hasOwnProperty(attr) && attr != "style" && attr != "class")
1864
          { token.setAttribute(attr, attributes[attr]); } }
1865
      }
1866
      return builder.content.appendChild(token)
1867
    }
1868
    builder.content.appendChild(content);
1869
  }
1870
 
1871
  // Change some spaces to NBSP to prevent the browser from collapsing
1872
  // trailing spaces at the end of a line when rendering text (issue #1362).
1873
  function splitSpaces(text, trailingBefore) {
1874
    if (text.length > 1 && !/  /.test(text)) { return text }
1875
    var spaceBefore = trailingBefore, result = "";
1876
    for (var i = 0; i < text.length; i++) {
1877
      var ch = text.charAt(i);
1878
      if (ch == " " && spaceBefore && (i == text.length - 1 || text.charCodeAt(i + 1) == 32))
1879
        { ch = "\u00a0"; }
1880
      result += ch;
1881
      spaceBefore = ch == " ";
1882
    }
1883
    return result
1884
  }
1885
 
1886
  // Work around nonsense dimensions being reported for stretches of
1887
  // right-to-left text.
1888
  function buildTokenBadBidi(inner, order) {
1889
    return function (builder, text, style, startStyle, endStyle, css, attributes) {
1890
      style = style ? style + " cm-force-border" : "cm-force-border";
1891
      var start = builder.pos, end = start + text.length;
1892
      for (;;) {
1893
        // Find the part that overlaps with the start of this text
1894
        var part = (void 0);
1895
        for (var i = 0; i < order.length; i++) {
1896
          part = order[i];
1897
          if (part.to > start && part.from <= start) { break }
1898
        }
1899
        if (part.to >= end) { return inner(builder, text, style, startStyle, endStyle, css, attributes) }
1900
        inner(builder, text.slice(0, part.to - start), style, startStyle, null, css, attributes);
1901
        startStyle = null;
1902
        text = text.slice(part.to - start);
1903
        start = part.to;
1904
      }
1905
    }
1906
  }
1907
 
1908
  function buildCollapsedSpan(builder, size, marker, ignoreWidget) {
1909
    var widget = !ignoreWidget && marker.widgetNode;
1910
    if (widget) { builder.map.push(builder.pos, builder.pos + size, widget); }
1911
    if (!ignoreWidget && builder.cm.display.input.needsContentAttribute) {
1912
      if (!widget)
1913
        { widget = builder.content.appendChild(document.createElement("span")); }
1914
      widget.setAttribute("cm-marker", marker.id);
1915
    }
1916
    if (widget) {
1917
      builder.cm.display.input.setUneditable(widget);
1918
      builder.content.appendChild(widget);
1919
    }
1920
    builder.pos += size;
1921
    builder.trailingSpace = false;
1922
  }
1923
 
1924
  // Outputs a number of spans to make up a line, taking highlighting
1925
  // and marked text into account.
1926
  function insertLineContent(line, builder, styles) {
1927
    var spans = line.markedSpans, allText = line.text, at = 0;
1928
    if (!spans) {
1929
      for (var i$1 = 1; i$1 < styles.length; i$1+=2)
1930
        { builder.addToken(builder, allText.slice(at, at = styles[i$1]), interpretTokenStyle(styles[i$1+1], builder.cm.options)); }
1931
      return
1932
    }
1933
 
1934
    var len = allText.length, pos = 0, i = 1, text = "", style, css;
1935
    var nextChange = 0, spanStyle, spanEndStyle, spanStartStyle, collapsed, attributes;
1936
    for (;;) {
1937
      if (nextChange == pos) { // Update current marker set
1938
        spanStyle = spanEndStyle = spanStartStyle = css = "";
1939
        attributes = null;
1940
        collapsed = null; nextChange = Infinity;
1941
        var foundBookmarks = [], endStyles = (void 0);
1942
        for (var j = 0; j < spans.length; ++j) {
1943
          var sp = spans[j], m = sp.marker;
1944
          if (m.type == "bookmark" && sp.from == pos && m.widgetNode) {
1945
            foundBookmarks.push(m);
1946
          } else if (sp.from <= pos && (sp.to == null || sp.to > pos || m.collapsed && sp.to == pos && sp.from == pos)) {
1947
            if (sp.to != null && sp.to != pos && nextChange > sp.to) {
1948
              nextChange = sp.to;
1949
              spanEndStyle = "";
1950
            }
1951
            if (m.className) { spanStyle += " " + m.className; }
1952
            if (m.css) { css = (css ? css + ";" : "") + m.css; }
1953
            if (m.startStyle && sp.from == pos) { spanStartStyle += " " + m.startStyle; }
1954
            if (m.endStyle && sp.to == nextChange) { (endStyles || (endStyles = [])).push(m.endStyle, sp.to); }
1955
            // support for the old title property
1956
            // https://github.com/codemirror/CodeMirror/pull/5673
1957
            if (m.title) { (attributes || (attributes = {})).title = m.title; }
1958
            if (m.attributes) {
1959
              for (var attr in m.attributes)
1960
                { (attributes || (attributes = {}))[attr] = m.attributes[attr]; }
1961
            }
1962
            if (m.collapsed && (!collapsed || compareCollapsedMarkers(collapsed.marker, m) < 0))
1963
              { collapsed = sp; }
1964
          } else if (sp.from > pos && nextChange > sp.from) {
1965
            nextChange = sp.from;
1966
          }
1967
        }
1968
        if (endStyles) { for (var j$1 = 0; j$1 < endStyles.length; j$1 += 2)
1969
          { if (endStyles[j$1 + 1] == nextChange) { spanEndStyle += " " + endStyles[j$1]; } } }
1970
 
1971
        if (!collapsed || collapsed.from == pos) { for (var j$2 = 0; j$2 < foundBookmarks.length; ++j$2)
1972
          { buildCollapsedSpan(builder, 0, foundBookmarks[j$2]); } }
1973
        if (collapsed && (collapsed.from || 0) == pos) {
1974
          buildCollapsedSpan(builder, (collapsed.to == null ? len + 1 : collapsed.to) - pos,
1975
                             collapsed.marker, collapsed.from == null);
1976
          if (collapsed.to == null) { return }
1977
          if (collapsed.to == pos) { collapsed = false; }
1978
        }
1979
      }
1980
      if (pos >= len) { break }
1981
 
1982
      var upto = Math.min(len, nextChange);
1983
      while (true) {
1984
        if (text) {
1985
          var end = pos + text.length;
1986
          if (!collapsed) {
1987
            var tokenText = end > upto ? text.slice(0, upto - pos) : text;
1988
            builder.addToken(builder, tokenText, style ? style + spanStyle : spanStyle,
1989
                             spanStartStyle, pos + tokenText.length == nextChange ? spanEndStyle : "", css, attributes);
1990
          }
1991
          if (end >= upto) {text = text.slice(upto - pos); pos = upto; break}
1992
          pos = end;
1993
          spanStartStyle = "";
1994
        }
1995
        text = allText.slice(at, at = styles[i++]);
1996
        style = interpretTokenStyle(styles[i++], builder.cm.options);
1997
      }
1998
    }
1999
  }
2000
 
2001
 
2002
  // These objects are used to represent the visible (currently drawn)
2003
  // part of the document. A LineView may correspond to multiple
2004
  // logical lines, if those are connected by collapsed ranges.
2005
  function LineView(doc, line, lineN) {
2006
    // The starting line
2007
    this.line = line;
2008
    // Continuing lines, if any
2009
    this.rest = visualLineContinued(line);
2010
    // Number of logical lines in this visual line
2011
    this.size = this.rest ? lineNo(lst(this.rest)) - lineN + 1 : 1;
2012
    this.node = this.text = null;
2013
    this.hidden = lineIsHidden(doc, line);
2014
  }
2015
 
2016
  // Create a range of LineView objects for the given lines.
2017
  function buildViewArray(cm, from, to) {
2018
    var array = [], nextPos;
2019
    for (var pos = from; pos < to; pos = nextPos) {
2020
      var view = new LineView(cm.doc, getLine(cm.doc, pos), pos);
2021
      nextPos = pos + view.size;
2022
      array.push(view);
2023
    }
2024
    return array
2025
  }
2026
 
2027
  var operationGroup = null;
2028
 
2029
  function pushOperation(op) {
2030
    if (operationGroup) {
2031
      operationGroup.ops.push(op);
2032
    } else {
2033
      op.ownsGroup = operationGroup = {
2034
        ops: [op],
2035
        delayedCallbacks: []
2036
      };
2037
    }
2038
  }
2039
 
2040
  function fireCallbacksForOps(group) {
2041
    // Calls delayed callbacks and cursorActivity handlers until no
2042
    // new ones appear
2043
    var callbacks = group.delayedCallbacks, i = 0;
2044
    do {
2045
      for (; i < callbacks.length; i++)
2046
        { callbacks[i].call(null); }
2047
      for (var j = 0; j < group.ops.length; j++) {
2048
        var op = group.ops[j];
2049
        if (op.cursorActivityHandlers)
2050
          { while (op.cursorActivityCalled < op.cursorActivityHandlers.length)
2051
            { op.cursorActivityHandlers[op.cursorActivityCalled++].call(null, op.cm); } }
2052
      }
2053
    } while (i < callbacks.length)
2054
  }
2055
 
2056
  function finishOperation(op, endCb) {
2057
    var group = op.ownsGroup;
2058
    if (!group) { return }
2059
 
2060
    try { fireCallbacksForOps(group); }
2061
    finally {
2062
      operationGroup = null;
2063
      endCb(group);
2064
    }
2065
  }
2066
 
2067
  var orphanDelayedCallbacks = null;
2068
 
2069
  // Often, we want to signal events at a point where we are in the
2070
  // middle of some work, but don't want the handler to start calling
2071
  // other methods on the editor, which might be in an inconsistent
2072
  // state or simply not expect any other events to happen.
2073
  // signalLater looks whether there are any handlers, and schedules
2074
  // them to be executed when the last operation ends, or, if no
2075
  // operation is active, when a timeout fires.
2076
  function signalLater(emitter, type /*, values...*/) {
2077
    var arr = getHandlers(emitter, type);
2078
    if (!arr.length) { return }
2079
    var args = Array.prototype.slice.call(arguments, 2), list;
2080
    if (operationGroup) {
2081
      list = operationGroup.delayedCallbacks;
2082
    } else if (orphanDelayedCallbacks) {
2083
      list = orphanDelayedCallbacks;
2084
    } else {
2085
      list = orphanDelayedCallbacks = [];
2086
      setTimeout(fireOrphanDelayed, 0);
2087
    }
2088
    var loop = function ( i ) {
2089
      list.push(function () { return arr[i].apply(null, args); });
2090
    };
2091
 
2092
    for (var i = 0; i < arr.length; ++i)
2093
      loop( i );
2094
  }
2095
 
2096
  function fireOrphanDelayed() {
2097
    var delayed = orphanDelayedCallbacks;
2098
    orphanDelayedCallbacks = null;
2099
    for (var i = 0; i < delayed.length; ++i) { delayed[i](); }
2100
  }
2101
 
2102
  // When an aspect of a line changes, a string is added to
2103
  // lineView.changes. This updates the relevant part of the line's
2104
  // DOM structure.
2105
  function updateLineForChanges(cm, lineView, lineN, dims) {
2106
    for (var j = 0; j < lineView.changes.length; j++) {
2107
      var type = lineView.changes[j];
2108
      if (type == "text") { updateLineText(cm, lineView); }
2109
      else if (type == "gutter") { updateLineGutter(cm, lineView, lineN, dims); }
2110
      else if (type == "class") { updateLineClasses(cm, lineView); }
2111
      else if (type == "widget") { updateLineWidgets(cm, lineView, dims); }
2112
    }
2113
    lineView.changes = null;
2114
  }
2115
 
2116
  // Lines with gutter elements, widgets or a background class need to
2117
  // be wrapped, and have the extra elements added to the wrapper div
2118
  function ensureLineWrapped(lineView) {
2119
    if (lineView.node == lineView.text) {
2120
      lineView.node = elt("div", null, null, "position: relative");
2121
      if (lineView.text.parentNode)
2122
        { lineView.text.parentNode.replaceChild(lineView.node, lineView.text); }
2123
      lineView.node.appendChild(lineView.text);
2124
      if (ie && ie_version < 8) { lineView.node.style.zIndex = 2; }
2125
    }
2126
    return lineView.node
2127
  }
2128
 
2129
  function updateLineBackground(cm, lineView) {
2130
    var cls = lineView.bgClass ? lineView.bgClass + " " + (lineView.line.bgClass || "") : lineView.line.bgClass;
2131
    if (cls) { cls += " CodeMirror-linebackground"; }
2132
    if (lineView.background) {
2133
      if (cls) { lineView.background.className = cls; }
2134
      else { lineView.background.parentNode.removeChild(lineView.background); lineView.background = null; }
2135
    } else if (cls) {
2136
      var wrap = ensureLineWrapped(lineView);
2137
      lineView.background = wrap.insertBefore(elt("div", null, cls), wrap.firstChild);
2138
      cm.display.input.setUneditable(lineView.background);
2139
    }
2140
  }
2141
 
2142
  // Wrapper around buildLineContent which will reuse the structure
2143
  // in display.externalMeasured when possible.
2144
  function getLineContent(cm, lineView) {
2145
    var ext = cm.display.externalMeasured;
2146
    if (ext && ext.line == lineView.line) {
2147
      cm.display.externalMeasured = null;
2148
      lineView.measure = ext.measure;
2149
      return ext.built
2150
    }
2151
    return buildLineContent(cm, lineView)
2152
  }
2153
 
2154
  // Redraw the line's text. Interacts with the background and text
2155
  // classes because the mode may output tokens that influence these
2156
  // classes.
2157
  function updateLineText(cm, lineView) {
2158
    var cls = lineView.text.className;
2159
    var built = getLineContent(cm, lineView);
2160
    if (lineView.text == lineView.node) { lineView.node = built.pre; }
2161
    lineView.text.parentNode.replaceChild(built.pre, lineView.text);
2162
    lineView.text = built.pre;
2163
    if (built.bgClass != lineView.bgClass || built.textClass != lineView.textClass) {
2164
      lineView.bgClass = built.bgClass;
2165
      lineView.textClass = built.textClass;
2166
      updateLineClasses(cm, lineView);
2167
    } else if (cls) {
2168
      lineView.text.className = cls;
2169
    }
2170
  }
2171
 
2172
  function updateLineClasses(cm, lineView) {
2173
    updateLineBackground(cm, lineView);
2174
    if (lineView.line.wrapClass)
2175
      { ensureLineWrapped(lineView).className = lineView.line.wrapClass; }
2176
    else if (lineView.node != lineView.text)
2177
      { lineView.node.className = ""; }
2178
    var textClass = lineView.textClass ? lineView.textClass + " " + (lineView.line.textClass || "") : lineView.line.textClass;
2179
    lineView.text.className = textClass || "";
2180
  }
2181
 
2182
  function updateLineGutter(cm, lineView, lineN, dims) {
2183
    if (lineView.gutter) {
2184
      lineView.node.removeChild(lineView.gutter);
2185
      lineView.gutter = null;
2186
    }
2187
    if (lineView.gutterBackground) {
2188
      lineView.node.removeChild(lineView.gutterBackground);
2189
      lineView.gutterBackground = null;
2190
    }
2191
    if (lineView.line.gutterClass) {
2192
      var wrap = ensureLineWrapped(lineView);
2193
      lineView.gutterBackground = elt("div", null, "CodeMirror-gutter-background " + lineView.line.gutterClass,
2194
                                      ("left: " + (cm.options.fixedGutter ? dims.fixedPos : -dims.gutterTotalWidth) + "px; width: " + (dims.gutterTotalWidth) + "px"));
2195
      cm.display.input.setUneditable(lineView.gutterBackground);
2196
      wrap.insertBefore(lineView.gutterBackground, lineView.text);
2197
    }
2198
    var markers = lineView.line.gutterMarkers;
2199
    if (cm.options.lineNumbers || markers) {
2200
      var wrap$1 = ensureLineWrapped(lineView);
2201
      var gutterWrap = lineView.gutter = elt("div", null, "CodeMirror-gutter-wrapper", ("left: " + (cm.options.fixedGutter ? dims.fixedPos : -dims.gutterTotalWidth) + "px"));
16493 obado 2202
      gutterWrap.setAttribute("aria-hidden", "true");
14283 obado 2203
      cm.display.input.setUneditable(gutterWrap);
2204
      wrap$1.insertBefore(gutterWrap, lineView.text);
2205
      if (lineView.line.gutterClass)
2206
        { gutterWrap.className += " " + lineView.line.gutterClass; }
2207
      if (cm.options.lineNumbers && (!markers || !markers["CodeMirror-linenumbers"]))
2208
        { lineView.lineNumber = gutterWrap.appendChild(
2209
          elt("div", lineNumberFor(cm.options, lineN),
2210
              "CodeMirror-linenumber CodeMirror-gutter-elt",
2211
              ("left: " + (dims.gutterLeft["CodeMirror-linenumbers"]) + "px; width: " + (cm.display.lineNumInnerWidth) + "px"))); }
2212
      if (markers) { for (var k = 0; k < cm.display.gutterSpecs.length; ++k) {
2213
        var id = cm.display.gutterSpecs[k].className, found = markers.hasOwnProperty(id) && markers[id];
2214
        if (found)
2215
          { gutterWrap.appendChild(elt("div", [found], "CodeMirror-gutter-elt",
2216
                                     ("left: " + (dims.gutterLeft[id]) + "px; width: " + (dims.gutterWidth[id]) + "px"))); }
2217
      } }
2218
    }
2219
  }
2220
 
2221
  function updateLineWidgets(cm, lineView, dims) {
2222
    if (lineView.alignable) { lineView.alignable = null; }
15152 obado 2223
    var isWidget = classTest("CodeMirror-linewidget");
14283 obado 2224
    for (var node = lineView.node.firstChild, next = (void 0); node; node = next) {
2225
      next = node.nextSibling;
15152 obado 2226
      if (isWidget.test(node.className)) { lineView.node.removeChild(node); }
14283 obado 2227
    }
2228
    insertLineWidgets(cm, lineView, dims);
2229
  }
2230
 
2231
  // Build a line's DOM representation from scratch
2232
  function buildLineElement(cm, lineView, lineN, dims) {
2233
    var built = getLineContent(cm, lineView);
2234
    lineView.text = lineView.node = built.pre;
2235
    if (built.bgClass) { lineView.bgClass = built.bgClass; }
2236
    if (built.textClass) { lineView.textClass = built.textClass; }
2237
 
2238
    updateLineClasses(cm, lineView);
2239
    updateLineGutter(cm, lineView, lineN, dims);
2240
    insertLineWidgets(cm, lineView, dims);
2241
    return lineView.node
2242
  }
2243
 
2244
  // A lineView may contain multiple logical lines (when merged by
2245
  // collapsed spans). The widgets for all of them need to be drawn.
2246
  function insertLineWidgets(cm, lineView, dims) {
2247
    insertLineWidgetsFor(cm, lineView.line, lineView, dims, true);
2248
    if (lineView.rest) { for (var i = 0; i < lineView.rest.length; i++)
2249
      { insertLineWidgetsFor(cm, lineView.rest[i], lineView, dims, false); } }
2250
  }
2251
 
2252
  function insertLineWidgetsFor(cm, line, lineView, dims, allowAbove) {
2253
    if (!line.widgets) { return }
2254
    var wrap = ensureLineWrapped(lineView);
2255
    for (var i = 0, ws = line.widgets; i < ws.length; ++i) {
15152 obado 2256
      var widget = ws[i], node = elt("div", [widget.node], "CodeMirror-linewidget" + (widget.className ? " " + widget.className : ""));
14283 obado 2257
      if (!widget.handleMouseEvents) { node.setAttribute("cm-ignore-events", "true"); }
2258
      positionLineWidget(widget, node, lineView, dims);
2259
      cm.display.input.setUneditable(node);
2260
      if (allowAbove && widget.above)
2261
        { wrap.insertBefore(node, lineView.gutter || lineView.text); }
2262
      else
2263
        { wrap.appendChild(node); }
2264
      signalLater(widget, "redraw");
2265
    }
2266
  }
2267
 
2268
  function positionLineWidget(widget, node, lineView, dims) {
2269
    if (widget.noHScroll) {
2270
  (lineView.alignable || (lineView.alignable = [])).push(node);
2271
      var width = dims.wrapperWidth;
2272
      node.style.left = dims.fixedPos + "px";
2273
      if (!widget.coverGutter) {
2274
        width -= dims.gutterTotalWidth;
2275
        node.style.paddingLeft = dims.gutterTotalWidth + "px";
2276
      }
2277
      node.style.width = width + "px";
2278
    }
2279
    if (widget.coverGutter) {
2280
      node.style.zIndex = 5;
2281
      node.style.position = "relative";
2282
      if (!widget.noHScroll) { node.style.marginLeft = -dims.gutterTotalWidth + "px"; }
2283
    }
2284
  }
2285
 
2286
  function widgetHeight(widget) {
2287
    if (widget.height != null) { return widget.height }
2288
    var cm = widget.doc.cm;
2289
    if (!cm) { return 0 }
2290
    if (!contains(document.body, widget.node)) {
2291
      var parentStyle = "position: relative;";
2292
      if (widget.coverGutter)
2293
        { parentStyle += "margin-left: -" + cm.display.gutters.offsetWidth + "px;"; }
2294
      if (widget.noHScroll)
2295
        { parentStyle += "width: " + cm.display.wrapper.clientWidth + "px;"; }
2296
      removeChildrenAndAdd(cm.display.measure, elt("div", [widget.node], null, parentStyle));
2297
    }
2298
    return widget.height = widget.node.parentNode.offsetHeight
2299
  }
2300
 
2301
  // Return true when the given mouse event happened in a widget
2302
  function eventInWidget(display, e) {
2303
    for (var n = e_target(e); n != display.wrapper; n = n.parentNode) {
2304
      if (!n || (n.nodeType == 1 && n.getAttribute("cm-ignore-events") == "true") ||
2305
          (n.parentNode == display.sizer && n != display.mover))
2306
        { return true }
2307
    }
2308
  }
2309
 
2310
  // POSITION MEASUREMENT
2311
 
2312
  function paddingTop(display) {return display.lineSpace.offsetTop}
2313
  function paddingVert(display) {return display.mover.offsetHeight - display.lineSpace.offsetHeight}
2314
  function paddingH(display) {
2315
    if (display.cachedPaddingH) { return display.cachedPaddingH }
2316
    var e = removeChildrenAndAdd(display.measure, elt("pre", "x", "CodeMirror-line-like"));
2317
    var style = window.getComputedStyle ? window.getComputedStyle(e) : e.currentStyle;
2318
    var data = {left: parseInt(style.paddingLeft), right: parseInt(style.paddingRight)};
2319
    if (!isNaN(data.left) && !isNaN(data.right)) { display.cachedPaddingH = data; }
2320
    return data
2321
  }
2322
 
2323
  function scrollGap(cm) { return scrollerGap - cm.display.nativeBarWidth }
2324
  function displayWidth(cm) {
2325
    return cm.display.scroller.clientWidth - scrollGap(cm) - cm.display.barWidth
2326
  }
2327
  function displayHeight(cm) {
2328
    return cm.display.scroller.clientHeight - scrollGap(cm) - cm.display.barHeight
2329
  }
2330
 
2331
  // Ensure the lineView.wrapping.heights array is populated. This is
2332
  // an array of bottom offsets for the lines that make up a drawn
2333
  // line. When lineWrapping is on, there might be more than one
2334
  // height.
2335
  function ensureLineHeights(cm, lineView, rect) {
2336
    var wrapping = cm.options.lineWrapping;
2337
    var curWidth = wrapping && displayWidth(cm);
2338
    if (!lineView.measure.heights || wrapping && lineView.measure.width != curWidth) {
2339
      var heights = lineView.measure.heights = [];
2340
      if (wrapping) {
2341
        lineView.measure.width = curWidth;
2342
        var rects = lineView.text.firstChild.getClientRects();
2343
        for (var i = 0; i < rects.length - 1; i++) {
2344
          var cur = rects[i], next = rects[i + 1];
2345
          if (Math.abs(cur.bottom - next.bottom) > 2)
2346
            { heights.push((cur.bottom + next.top) / 2 - rect.top); }
2347
        }
2348
      }
2349
      heights.push(rect.bottom - rect.top);
2350
    }
2351
  }
2352
 
2353
  // Find a line map (mapping character offsets to text nodes) and a
2354
  // measurement cache for the given line number. (A line view might
2355
  // contain multiple lines when collapsed ranges are present.)
2356
  function mapFromLineView(lineView, line, lineN) {
2357
    if (lineView.line == line)
2358
      { return {map: lineView.measure.map, cache: lineView.measure.cache} }
16493 obado 2359
    if (lineView.rest) {
2360
      for (var i = 0; i < lineView.rest.length; i++)
2361
        { if (lineView.rest[i] == line)
2362
          { return {map: lineView.measure.maps[i], cache: lineView.measure.caches[i]} } }
2363
      for (var i$1 = 0; i$1 < lineView.rest.length; i$1++)
2364
        { if (lineNo(lineView.rest[i$1]) > lineN)
2365
          { return {map: lineView.measure.maps[i$1], cache: lineView.measure.caches[i$1], before: true} } }
2366
    }
14283 obado 2367
  }
2368
 
2369
  // Render a line into the hidden node display.externalMeasured. Used
2370
  // when measurement is needed for a line that's not in the viewport.
2371
  function updateExternalMeasurement(cm, line) {
2372
    line = visualLine(line);
2373
    var lineN = lineNo(line);
2374
    var view = cm.display.externalMeasured = new LineView(cm.doc, line, lineN);
2375
    view.lineN = lineN;
2376
    var built = view.built = buildLineContent(cm, view);
2377
    view.text = built.pre;
2378
    removeChildrenAndAdd(cm.display.lineMeasure, built.pre);
2379
    return view
2380
  }
2381
 
2382
  // Get a {top, bottom, left, right} box (in line-local coordinates)
2383
  // for a given character.
2384
  function measureChar(cm, line, ch, bias) {
2385
    return measureCharPrepared(cm, prepareMeasureForLine(cm, line), ch, bias)
2386
  }
2387
 
2388
  // Find a line view that corresponds to the given line number.
2389
  function findViewForLine(cm, lineN) {
2390
    if (lineN >= cm.display.viewFrom && lineN < cm.display.viewTo)
2391
      { return cm.display.view[findViewIndex(cm, lineN)] }
2392
    var ext = cm.display.externalMeasured;
2393
    if (ext && lineN >= ext.lineN && lineN < ext.lineN + ext.size)
2394
      { return ext }
2395
  }
2396
 
2397
  // Measurement can be split in two steps, the set-up work that
2398
  // applies to the whole line, and the measurement of the actual
2399
  // character. Functions like coordsChar, that need to do a lot of
2400
  // measurements in a row, can thus ensure that the set-up work is
2401
  // only done once.
2402
  function prepareMeasureForLine(cm, line) {
2403
    var lineN = lineNo(line);
2404
    var view = findViewForLine(cm, lineN);
2405
    if (view && !view.text) {
2406
      view = null;
2407
    } else if (view && view.changes) {
2408
      updateLineForChanges(cm, view, lineN, getDimensions(cm));
2409
      cm.curOp.forceUpdate = true;
2410
    }
2411
    if (!view)
2412
      { view = updateExternalMeasurement(cm, line); }
2413
 
2414
    var info = mapFromLineView(view, line, lineN);
2415
    return {
2416
      line: line, view: view, rect: null,
2417
      map: info.map, cache: info.cache, before: info.before,
2418
      hasHeights: false
2419
    }
2420
  }
2421
 
2422
  // Given a prepared measurement object, measures the position of an
2423
  // actual character (or fetches it from the cache).
2424
  function measureCharPrepared(cm, prepared, ch, bias, varHeight) {
2425
    if (prepared.before) { ch = -1; }
2426
    var key = ch + (bias || ""), found;
2427
    if (prepared.cache.hasOwnProperty(key)) {
2428
      found = prepared.cache[key];
2429
    } else {
2430
      if (!prepared.rect)
2431
        { prepared.rect = prepared.view.text.getBoundingClientRect(); }
2432
      if (!prepared.hasHeights) {
2433
        ensureLineHeights(cm, prepared.view, prepared.rect);
2434
        prepared.hasHeights = true;
2435
      }
2436
      found = measureCharInner(cm, prepared, ch, bias);
2437
      if (!found.bogus) { prepared.cache[key] = found; }
2438
    }
2439
    return {left: found.left, right: found.right,
2440
            top: varHeight ? found.rtop : found.top,
2441
            bottom: varHeight ? found.rbottom : found.bottom}
2442
  }
2443
 
2444
  var nullRect = {left: 0, right: 0, top: 0, bottom: 0};
2445
 
15152 obado 2446
  function nodeAndOffsetInLineMap(map, ch, bias) {
14283 obado 2447
    var node, start, end, collapse, mStart, mEnd;
2448
    // First, search the line map for the text node corresponding to,
2449
    // or closest to, the target character.
15152 obado 2450
    for (var i = 0; i < map.length; i += 3) {
2451
      mStart = map[i];
2452
      mEnd = map[i + 1];
14283 obado 2453
      if (ch < mStart) {
2454
        start = 0; end = 1;
2455
        collapse = "left";
2456
      } else if (ch < mEnd) {
2457
        start = ch - mStart;
2458
        end = start + 1;
15152 obado 2459
      } else if (i == map.length - 3 || ch == mEnd && map[i + 3] > ch) {
14283 obado 2460
        end = mEnd - mStart;
2461
        start = end - 1;
2462
        if (ch >= mEnd) { collapse = "right"; }
2463
      }
2464
      if (start != null) {
15152 obado 2465
        node = map[i + 2];
14283 obado 2466
        if (mStart == mEnd && bias == (node.insertLeft ? "left" : "right"))
2467
          { collapse = bias; }
2468
        if (bias == "left" && start == 0)
15152 obado 2469
          { while (i && map[i - 2] == map[i - 3] && map[i - 1].insertLeft) {
2470
            node = map[(i -= 3) + 2];
14283 obado 2471
            collapse = "left";
2472
          } }
2473
        if (bias == "right" && start == mEnd - mStart)
15152 obado 2474
          { while (i < map.length - 3 && map[i + 3] == map[i + 4] && !map[i + 5].insertLeft) {
2475
            node = map[(i += 3) + 2];
14283 obado 2476
            collapse = "right";
2477
          } }
2478
        break
2479
      }
2480
    }
2481
    return {node: node, start: start, end: end, collapse: collapse, coverStart: mStart, coverEnd: mEnd}
2482
  }
2483
 
2484
  function getUsefulRect(rects, bias) {
2485
    var rect = nullRect;
2486
    if (bias == "left") { for (var i = 0; i < rects.length; i++) {
2487
      if ((rect = rects[i]).left != rect.right) { break }
2488
    } } else { for (var i$1 = rects.length - 1; i$1 >= 0; i$1--) {
2489
      if ((rect = rects[i$1]).left != rect.right) { break }
2490
    } }
2491
    return rect
2492
  }
2493
 
2494
  function measureCharInner(cm, prepared, ch, bias) {
2495
    var place = nodeAndOffsetInLineMap(prepared.map, ch, bias);
2496
    var node = place.node, start = place.start, end = place.end, collapse = place.collapse;
2497
 
2498
    var rect;
2499
    if (node.nodeType == 3) { // If it is a text node, use a range to retrieve the coordinates.
2500
      for (var i$1 = 0; i$1 < 4; i$1++) { // Retry a maximum of 4 times when nonsense rectangles are returned
2501
        while (start && isExtendingChar(prepared.line.text.charAt(place.coverStart + start))) { --start; }
2502
        while (place.coverStart + end < place.coverEnd && isExtendingChar(prepared.line.text.charAt(place.coverStart + end))) { ++end; }
2503
        if (ie && ie_version < 9 && start == 0 && end == place.coverEnd - place.coverStart)
2504
          { rect = node.parentNode.getBoundingClientRect(); }
2505
        else
2506
          { rect = getUsefulRect(range(node, start, end).getClientRects(), bias); }
2507
        if (rect.left || rect.right || start == 0) { break }
2508
        end = start;
2509
        start = start - 1;
2510
        collapse = "right";
2511
      }
2512
      if (ie && ie_version < 11) { rect = maybeUpdateRectForZooming(cm.display.measure, rect); }
2513
    } else { // If it is a widget, simply get the box for the whole widget.
2514
      if (start > 0) { collapse = bias = "right"; }
2515
      var rects;
2516
      if (cm.options.lineWrapping && (rects = node.getClientRects()).length > 1)
2517
        { rect = rects[bias == "right" ? rects.length - 1 : 0]; }
2518
      else
2519
        { rect = node.getBoundingClientRect(); }
2520
    }
2521
    if (ie && ie_version < 9 && !start && (!rect || !rect.left && !rect.right)) {
2522
      var rSpan = node.parentNode.getClientRects()[0];
2523
      if (rSpan)
2524
        { rect = {left: rSpan.left, right: rSpan.left + charWidth(cm.display), top: rSpan.top, bottom: rSpan.bottom}; }
2525
      else
2526
        { rect = nullRect; }
2527
    }
2528
 
2529
    var rtop = rect.top - prepared.rect.top, rbot = rect.bottom - prepared.rect.top;
2530
    var mid = (rtop + rbot) / 2;
2531
    var heights = prepared.view.measure.heights;
2532
    var i = 0;
2533
    for (; i < heights.length - 1; i++)
2534
      { if (mid < heights[i]) { break } }
2535
    var top = i ? heights[i - 1] : 0, bot = heights[i];
2536
    var result = {left: (collapse == "right" ? rect.right : rect.left) - prepared.rect.left,
2537
                  right: (collapse == "left" ? rect.left : rect.right) - prepared.rect.left,
2538
                  top: top, bottom: bot};
2539
    if (!rect.left && !rect.right) { result.bogus = true; }
2540
    if (!cm.options.singleCursorHeightPerLine) { result.rtop = rtop; result.rbottom = rbot; }
2541
 
2542
    return result
2543
  }
2544
 
2545
  // Work around problem with bounding client rects on ranges being
2546
  // returned incorrectly when zoomed on IE10 and below.
2547
  function maybeUpdateRectForZooming(measure, rect) {
2548
    if (!window.screen || screen.logicalXDPI == null ||
2549
        screen.logicalXDPI == screen.deviceXDPI || !hasBadZoomedRects(measure))
2550
      { return rect }
2551
    var scaleX = screen.logicalXDPI / screen.deviceXDPI;
2552
    var scaleY = screen.logicalYDPI / screen.deviceYDPI;
2553
    return {left: rect.left * scaleX, right: rect.right * scaleX,
2554
            top: rect.top * scaleY, bottom: rect.bottom * scaleY}
2555
  }
2556
 
2557
  function clearLineMeasurementCacheFor(lineView) {
2558
    if (lineView.measure) {
2559
      lineView.measure.cache = {};
2560
      lineView.measure.heights = null;
2561
      if (lineView.rest) { for (var i = 0; i < lineView.rest.length; i++)
2562
        { lineView.measure.caches[i] = {}; } }
2563
    }
2564
  }
2565
 
2566
  function clearLineMeasurementCache(cm) {
2567
    cm.display.externalMeasure = null;
2568
    removeChildren(cm.display.lineMeasure);
2569
    for (var i = 0; i < cm.display.view.length; i++)
2570
      { clearLineMeasurementCacheFor(cm.display.view[i]); }
2571
  }
2572
 
2573
  function clearCaches(cm) {
2574
    clearLineMeasurementCache(cm);
2575
    cm.display.cachedCharWidth = cm.display.cachedTextHeight = cm.display.cachedPaddingH = null;
2576
    if (!cm.options.lineWrapping) { cm.display.maxLineChanged = true; }
2577
    cm.display.lineNumChars = null;
2578
  }
2579
 
17702 obado 2580
  function pageScrollX(doc) {
14283 obado 2581
    // Work around https://bugs.chromium.org/p/chromium/issues/detail?id=489206
2582
    // which causes page_Offset and bounding client rects to use
2583
    // different reference viewports and invalidate our calculations.
17702 obado 2584
    if (chrome && android) { return -(doc.body.getBoundingClientRect().left - parseInt(getComputedStyle(doc.body).marginLeft)) }
2585
    return doc.defaultView.pageXOffset || (doc.documentElement || doc.body).scrollLeft
14283 obado 2586
  }
17702 obado 2587
  function pageScrollY(doc) {
2588
    if (chrome && android) { return -(doc.body.getBoundingClientRect().top - parseInt(getComputedStyle(doc.body).marginTop)) }
2589
    return doc.defaultView.pageYOffset || (doc.documentElement || doc.body).scrollTop
14283 obado 2590
  }
2591
 
2592
  function widgetTopHeight(lineObj) {
16493 obado 2593
    var ref = visualLine(lineObj);
2594
    var widgets = ref.widgets;
14283 obado 2595
    var height = 0;
16493 obado 2596
    if (widgets) { for (var i = 0; i < widgets.length; ++i) { if (widgets[i].above)
2597
      { height += widgetHeight(widgets[i]); } } }
14283 obado 2598
    return height
2599
  }
2600
 
2601
  // Converts a {top, bottom, left, right} box from line-local
2602
  // coordinates into another coordinate system. Context may be one of
2603
  // "line", "div" (display.lineDiv), "local"./null (editor), "window",
2604
  // or "page".
2605
  function intoCoordSystem(cm, lineObj, rect, context, includeWidgets) {
2606
    if (!includeWidgets) {
2607
      var height = widgetTopHeight(lineObj);
2608
      rect.top += height; rect.bottom += height;
2609
    }
2610
    if (context == "line") { return rect }
2611
    if (!context) { context = "local"; }
2612
    var yOff = heightAtLine(lineObj);
2613
    if (context == "local") { yOff += paddingTop(cm.display); }
2614
    else { yOff -= cm.display.viewOffset; }
2615
    if (context == "page" || context == "window") {
2616
      var lOff = cm.display.lineSpace.getBoundingClientRect();
17702 obado 2617
      yOff += lOff.top + (context == "window" ? 0 : pageScrollY(doc(cm)));
2618
      var xOff = lOff.left + (context == "window" ? 0 : pageScrollX(doc(cm)));
14283 obado 2619
      rect.left += xOff; rect.right += xOff;
2620
    }
2621
    rect.top += yOff; rect.bottom += yOff;
2622
    return rect
2623
  }
2624
 
2625
  // Coverts a box from "div" coords to another coordinate system.
2626
  // Context may be "window", "page", "div", or "local"./null.
2627
  function fromCoordSystem(cm, coords, context) {
2628
    if (context == "div") { return coords }
2629
    var left = coords.left, top = coords.top;
2630
    // First move into "page" coordinate system
2631
    if (context == "page") {
17702 obado 2632
      left -= pageScrollX(doc(cm));
2633
      top -= pageScrollY(doc(cm));
14283 obado 2634
    } else if (context == "local" || !context) {
2635
      var localBox = cm.display.sizer.getBoundingClientRect();
2636
      left += localBox.left;
2637
      top += localBox.top;
2638
    }
2639
 
2640
    var lineSpaceBox = cm.display.lineSpace.getBoundingClientRect();
2641
    return {left: left - lineSpaceBox.left, top: top - lineSpaceBox.top}
2642
  }
2643
 
2644
  function charCoords(cm, pos, context, lineObj, bias) {
2645
    if (!lineObj) { lineObj = getLine(cm.doc, pos.line); }
2646
    return intoCoordSystem(cm, lineObj, measureChar(cm, lineObj, pos.ch, bias), context)
2647
  }
2648
 
2649
  // Returns a box for a given cursor position, which may have an
2650
  // 'other' property containing the position of the secondary cursor
2651
  // on a bidi boundary.
2652
  // A cursor Pos(line, char, "before") is on the same visual line as `char - 1`
2653
  // and after `char - 1` in writing order of `char - 1`
2654
  // A cursor Pos(line, char, "after") is on the same visual line as `char`
2655
  // and before `char` in writing order of `char`
2656
  // Examples (upper-case letters are RTL, lower-case are LTR):
2657
  //     Pos(0, 1, ...)
2658
  //     before   after
2659
  // ab     a|b     a|b
2660
  // aB     a|B     aB|
2661
  // Ab     |Ab     A|b
2662
  // AB     B|A     B|A
2663
  // Every position after the last character on a line is considered to stick
2664
  // to the last character on the line.
2665
  function cursorCoords(cm, pos, context, lineObj, preparedMeasure, varHeight) {
2666
    lineObj = lineObj || getLine(cm.doc, pos.line);
2667
    if (!preparedMeasure) { preparedMeasure = prepareMeasureForLine(cm, lineObj); }
2668
    function get(ch, right) {
2669
      var m = measureCharPrepared(cm, preparedMeasure, ch, right ? "right" : "left", varHeight);
2670
      if (right) { m.left = m.right; } else { m.right = m.left; }
2671
      return intoCoordSystem(cm, lineObj, m, context)
2672
    }
2673
    var order = getOrder(lineObj, cm.doc.direction), ch = pos.ch, sticky = pos.sticky;
2674
    if (ch >= lineObj.text.length) {
2675
      ch = lineObj.text.length;
2676
      sticky = "before";
2677
    } else if (ch <= 0) {
2678
      ch = 0;
2679
      sticky = "after";
2680
    }
2681
    if (!order) { return get(sticky == "before" ? ch - 1 : ch, sticky == "before") }
2682
 
2683
    function getBidi(ch, partPos, invert) {
2684
      var part = order[partPos], right = part.level == 1;
2685
      return get(invert ? ch - 1 : ch, right != invert)
2686
    }
2687
    var partPos = getBidiPartAt(order, ch, sticky);
2688
    var other = bidiOther;
2689
    var val = getBidi(ch, partPos, sticky == "before");
2690
    if (other != null) { val.other = getBidi(ch, other, sticky != "before"); }
2691
    return val
2692
  }
2693
 
2694
  // Used to cheaply estimate the coordinates for a position. Used for
2695
  // intermediate scroll updates.
2696
  function estimateCoords(cm, pos) {
2697
    var left = 0;
2698
    pos = clipPos(cm.doc, pos);
2699
    if (!cm.options.lineWrapping) { left = charWidth(cm.display) * pos.ch; }
2700
    var lineObj = getLine(cm.doc, pos.line);
2701
    var top = heightAtLine(lineObj) + paddingTop(cm.display);
2702
    return {left: left, right: left, top: top, bottom: top + lineObj.height}
2703
  }
2704
 
2705
  // Positions returned by coordsChar contain some extra information.
2706
  // xRel is the relative x position of the input coordinates compared
2707
  // to the found position (so xRel > 0 means the coordinates are to
2708
  // the right of the character position, for example). When outside
2709
  // is true, that means the coordinates lie outside the line's
2710
  // vertical range.
2711
  function PosWithInfo(line, ch, sticky, outside, xRel) {
2712
    var pos = Pos(line, ch, sticky);
2713
    pos.xRel = xRel;
2714
    if (outside) { pos.outside = outside; }
2715
    return pos
2716
  }
2717
 
2718
  // Compute the character position closest to the given coordinates.
2719
  // Input must be lineSpace-local ("div" coordinate system).
2720
  function coordsChar(cm, x, y) {
2721
    var doc = cm.doc;
2722
    y += cm.display.viewOffset;
2723
    if (y < 0) { return PosWithInfo(doc.first, 0, null, -1, -1) }
2724
    var lineN = lineAtHeight(doc, y), last = doc.first + doc.size - 1;
2725
    if (lineN > last)
2726
      { return PosWithInfo(doc.first + doc.size - 1, getLine(doc, last).text.length, null, 1, 1) }
2727
    if (x < 0) { x = 0; }
2728
 
2729
    var lineObj = getLine(doc, lineN);
2730
    for (;;) {
2731
      var found = coordsCharInner(cm, lineObj, lineN, x, y);
2732
      var collapsed = collapsedSpanAround(lineObj, found.ch + (found.xRel > 0 || found.outside > 0 ? 1 : 0));
2733
      if (!collapsed) { return found }
2734
      var rangeEnd = collapsed.find(1);
2735
      if (rangeEnd.line == lineN) { return rangeEnd }
2736
      lineObj = getLine(doc, lineN = rangeEnd.line);
2737
    }
2738
  }
2739
 
2740
  function wrappedLineExtent(cm, lineObj, preparedMeasure, y) {
2741
    y -= widgetTopHeight(lineObj);
2742
    var end = lineObj.text.length;
2743
    var begin = findFirst(function (ch) { return measureCharPrepared(cm, preparedMeasure, ch - 1).bottom <= y; }, end, 0);
2744
    end = findFirst(function (ch) { return measureCharPrepared(cm, preparedMeasure, ch).top > y; }, begin, end);
2745
    return {begin: begin, end: end}
2746
  }
2747
 
2748
  function wrappedLineExtentChar(cm, lineObj, preparedMeasure, target) {
2749
    if (!preparedMeasure) { preparedMeasure = prepareMeasureForLine(cm, lineObj); }
2750
    var targetTop = intoCoordSystem(cm, lineObj, measureCharPrepared(cm, preparedMeasure, target), "line").top;
2751
    return wrappedLineExtent(cm, lineObj, preparedMeasure, targetTop)
2752
  }
2753
 
2754
  // Returns true if the given side of a box is after the given
2755
  // coordinates, in top-to-bottom, left-to-right order.
2756
  function boxIsAfter(box, x, y, left) {
2757
    return box.bottom <= y ? false : box.top > y ? true : (left ? box.left : box.right) > x
2758
  }
2759
 
15152 obado 2760
  function coordsCharInner(cm, lineObj, lineNo, x, y) {
14283 obado 2761
    // Move y into line-local coordinate space
2762
    y -= heightAtLine(lineObj);
2763
    var preparedMeasure = prepareMeasureForLine(cm, lineObj);
2764
    // When directly calling `measureCharPrepared`, we have to adjust
2765
    // for the widgets at this line.
15152 obado 2766
    var widgetHeight = widgetTopHeight(lineObj);
14283 obado 2767
    var begin = 0, end = lineObj.text.length, ltr = true;
2768
 
2769
    var order = getOrder(lineObj, cm.doc.direction);
2770
    // If the line isn't plain left-to-right text, first figure out
2771
    // which bidi section the coordinates fall into.
2772
    if (order) {
2773
      var part = (cm.options.lineWrapping ? coordsBidiPartWrapped : coordsBidiPart)
15152 obado 2774
                   (cm, lineObj, lineNo, preparedMeasure, order, x, y);
14283 obado 2775
      ltr = part.level != 1;
2776
      // The awkward -1 offsets are needed because findFirst (called
2777
      // on these below) will treat its first bound as inclusive,
2778
      // second as exclusive, but we want to actually address the
2779
      // characters in the part's range
2780
      begin = ltr ? part.from : part.to - 1;
2781
      end = ltr ? part.to : part.from - 1;
2782
    }
2783
 
2784
    // A binary search to find the first character whose bounding box
2785
    // starts after the coordinates. If we run across any whose box wrap
2786
    // the coordinates, store that.
2787
    var chAround = null, boxAround = null;
2788
    var ch = findFirst(function (ch) {
2789
      var box = measureCharPrepared(cm, preparedMeasure, ch);
15152 obado 2790
      box.top += widgetHeight; box.bottom += widgetHeight;
14283 obado 2791
      if (!boxIsAfter(box, x, y, false)) { return false }
2792
      if (box.top <= y && box.left <= x) {
2793
        chAround = ch;
2794
        boxAround = box;
2795
      }
2796
      return true
2797
    }, begin, end);
2798
 
2799
    var baseX, sticky, outside = false;
2800
    // If a box around the coordinates was found, use that
2801
    if (boxAround) {
2802
      // Distinguish coordinates nearer to the left or right side of the box
2803
      var atLeft = x - boxAround.left < boxAround.right - x, atStart = atLeft == ltr;
2804
      ch = chAround + (atStart ? 0 : 1);
2805
      sticky = atStart ? "after" : "before";
2806
      baseX = atLeft ? boxAround.left : boxAround.right;
2807
    } else {
2808
      // (Adjust for extended bound, if necessary.)
2809
      if (!ltr && (ch == end || ch == begin)) { ch++; }
2810
      // To determine which side to associate with, get the box to the
2811
      // left of the character and compare it's vertical position to the
2812
      // coordinates
2813
      sticky = ch == 0 ? "after" : ch == lineObj.text.length ? "before" :
15152 obado 2814
        (measureCharPrepared(cm, preparedMeasure, ch - (ltr ? 1 : 0)).bottom + widgetHeight <= y) == ltr ?
14283 obado 2815
        "after" : "before";
2816
      // Now get accurate coordinates for this place, in order to get a
2817
      // base X position
15152 obado 2818
      var coords = cursorCoords(cm, Pos(lineNo, ch, sticky), "line", lineObj, preparedMeasure);
14283 obado 2819
      baseX = coords.left;
2820
      outside = y < coords.top ? -1 : y >= coords.bottom ? 1 : 0;
2821
    }
2822
 
2823
    ch = skipExtendingChars(lineObj.text, ch, 1);
15152 obado 2824
    return PosWithInfo(lineNo, ch, sticky, outside, x - baseX)
14283 obado 2825
  }
2826
 
15152 obado 2827
  function coordsBidiPart(cm, lineObj, lineNo, preparedMeasure, order, x, y) {
14283 obado 2828
    // Bidi parts are sorted left-to-right, and in a non-line-wrapping
2829
    // situation, we can take this ordering to correspond to the visual
2830
    // ordering. This finds the first part whose end is after the given
2831
    // coordinates.
2832
    var index = findFirst(function (i) {
2833
      var part = order[i], ltr = part.level != 1;
15152 obado 2834
      return boxIsAfter(cursorCoords(cm, Pos(lineNo, ltr ? part.to : part.from, ltr ? "before" : "after"),
14283 obado 2835
                                     "line", lineObj, preparedMeasure), x, y, true)
2836
    }, 0, order.length - 1);
2837
    var part = order[index];
2838
    // If this isn't the first part, the part's start is also after
2839
    // the coordinates, and the coordinates aren't on the same line as
2840
    // that start, move one part back.
2841
    if (index > 0) {
2842
      var ltr = part.level != 1;
15152 obado 2843
      var start = cursorCoords(cm, Pos(lineNo, ltr ? part.from : part.to, ltr ? "after" : "before"),
14283 obado 2844
                               "line", lineObj, preparedMeasure);
2845
      if (boxIsAfter(start, x, y, true) && start.top > y)
2846
        { part = order[index - 1]; }
2847
    }
2848
    return part
2849
  }
2850
 
2851
  function coordsBidiPartWrapped(cm, lineObj, _lineNo, preparedMeasure, order, x, y) {
2852
    // In a wrapped line, rtl text on wrapping boundaries can do things
2853
    // that don't correspond to the ordering in our `order` array at
2854
    // all, so a binary search doesn't work, and we want to return a
2855
    // part that only spans one line so that the binary search in
2856
    // coordsCharInner is safe. As such, we first find the extent of the
2857
    // wrapped line, and then do a flat search in which we discard any
2858
    // spans that aren't on the line.
2859
    var ref = wrappedLineExtent(cm, lineObj, preparedMeasure, y);
2860
    var begin = ref.begin;
2861
    var end = ref.end;
2862
    if (/\s/.test(lineObj.text.charAt(end - 1))) { end--; }
2863
    var part = null, closestDist = null;
2864
    for (var i = 0; i < order.length; i++) {
2865
      var p = order[i];
2866
      if (p.from >= end || p.to <= begin) { continue }
2867
      var ltr = p.level != 1;
2868
      var endX = measureCharPrepared(cm, preparedMeasure, ltr ? Math.min(end, p.to) - 1 : Math.max(begin, p.from)).right;
2869
      // Weigh against spans ending before this, so that they are only
2870
      // picked if nothing ends after
2871
      var dist = endX < x ? x - endX + 1e9 : endX - x;
2872
      if (!part || closestDist > dist) {
2873
        part = p;
2874
        closestDist = dist;
2875
      }
2876
    }
2877
    if (!part) { part = order[order.length - 1]; }
2878
    // Clip the part to the wrapped line.
2879
    if (part.from < begin) { part = {from: begin, to: part.to, level: part.level}; }
2880
    if (part.to > end) { part = {from: part.from, to: end, level: part.level}; }
2881
    return part
2882
  }
2883
 
2884
  var measureText;
2885
  // Compute the default text height.
2886
  function textHeight(display) {
2887
    if (display.cachedTextHeight != null) { return display.cachedTextHeight }
2888
    if (measureText == null) {
2889
      measureText = elt("pre", null, "CodeMirror-line-like");
2890
      // Measure a bunch of lines, for browsers that compute
2891
      // fractional heights.
2892
      for (var i = 0; i < 49; ++i) {
2893
        measureText.appendChild(document.createTextNode("x"));
2894
        measureText.appendChild(elt("br"));
2895
      }
2896
      measureText.appendChild(document.createTextNode("x"));
2897
    }
2898
    removeChildrenAndAdd(display.measure, measureText);
2899
    var height = measureText.offsetHeight / 50;
2900
    if (height > 3) { display.cachedTextHeight = height; }
2901
    removeChildren(display.measure);
2902
    return height || 1
2903
  }
2904
 
2905
  // Compute the default character width.
2906
  function charWidth(display) {
2907
    if (display.cachedCharWidth != null) { return display.cachedCharWidth }
2908
    var anchor = elt("span", "xxxxxxxxxx");
2909
    var pre = elt("pre", [anchor], "CodeMirror-line-like");
2910
    removeChildrenAndAdd(display.measure, pre);
2911
    var rect = anchor.getBoundingClientRect(), width = (rect.right - rect.left) / 10;
2912
    if (width > 2) { display.cachedCharWidth = width; }
2913
    return width || 10
2914
  }
2915
 
2916
  // Do a bulk-read of the DOM positions and sizes needed to draw the
2917
  // view, so that we don't interleave reading and writing to the DOM.
2918
  function getDimensions(cm) {
2919
    var d = cm.display, left = {}, width = {};
2920
    var gutterLeft = d.gutters.clientLeft;
2921
    for (var n = d.gutters.firstChild, i = 0; n; n = n.nextSibling, ++i) {
2922
      var id = cm.display.gutterSpecs[i].className;
2923
      left[id] = n.offsetLeft + n.clientLeft + gutterLeft;
2924
      width[id] = n.clientWidth;
2925
    }
2926
    return {fixedPos: compensateForHScroll(d),
2927
            gutterTotalWidth: d.gutters.offsetWidth,
2928
            gutterLeft: left,
2929
            gutterWidth: width,
2930
            wrapperWidth: d.wrapper.clientWidth}
2931
  }
2932
 
2933
  // Computes display.scroller.scrollLeft + display.gutters.offsetWidth,
2934
  // but using getBoundingClientRect to get a sub-pixel-accurate
2935
  // result.
2936
  function compensateForHScroll(display) {
2937
    return display.scroller.getBoundingClientRect().left - display.sizer.getBoundingClientRect().left
2938
  }
2939
 
2940
  // Returns a function that estimates the height of a line, to use as
2941
  // first approximation until the line becomes visible (and is thus
2942
  // properly measurable).
2943
  function estimateHeight(cm) {
2944
    var th = textHeight(cm.display), wrapping = cm.options.lineWrapping;
2945
    var perLine = wrapping && Math.max(5, cm.display.scroller.clientWidth / charWidth(cm.display) - 3);
2946
    return function (line) {
2947
      if (lineIsHidden(cm.doc, line)) { return 0 }
2948
 
2949
      var widgetsHeight = 0;
2950
      if (line.widgets) { for (var i = 0; i < line.widgets.length; i++) {
2951
        if (line.widgets[i].height) { widgetsHeight += line.widgets[i].height; }
2952
      } }
2953
 
2954
      if (wrapping)
2955
        { return widgetsHeight + (Math.ceil(line.text.length / perLine) || 1) * th }
2956
      else
2957
        { return widgetsHeight + th }
2958
    }
2959
  }
2960
 
2961
  function estimateLineHeights(cm) {
2962
    var doc = cm.doc, est = estimateHeight(cm);
2963
    doc.iter(function (line) {
2964
      var estHeight = est(line);
2965
      if (estHeight != line.height) { updateLineHeight(line, estHeight); }
2966
    });
2967
  }
2968
 
2969
  // Given a mouse event, find the corresponding position. If liberal
2970
  // is false, it checks whether a gutter or scrollbar was clicked,
2971
  // and returns null if it was. forRect is used by rectangular
2972
  // selections, and tries to estimate a character position even for
2973
  // coordinates beyond the right of the text.
2974
  function posFromMouse(cm, e, liberal, forRect) {
2975
    var display = cm.display;
2976
    if (!liberal && e_target(e).getAttribute("cm-not-content") == "true") { return null }
2977
 
2978
    var x, y, space = display.lineSpace.getBoundingClientRect();
2979
    // Fails unpredictably on IE[67] when mouse is dragged around quickly.
2980
    try { x = e.clientX - space.left; y = e.clientY - space.top; }
15332 obado 2981
    catch (e$1) { return null }
14283 obado 2982
    var coords = coordsChar(cm, x, y), line;
15152 obado 2983
    if (forRect && coords.xRel > 0 && (line = getLine(cm.doc, coords.line).text).length == coords.ch) {
14283 obado 2984
      var colDiff = countColumn(line, line.length, cm.options.tabSize) - line.length;
2985
      coords = Pos(coords.line, Math.max(0, Math.round((x - paddingH(cm.display).left) / charWidth(cm.display)) - colDiff));
2986
    }
2987
    return coords
2988
  }
2989
 
2990
  // Find the view element corresponding to a given line. Return null
2991
  // when the line isn't visible.
2992
  function findViewIndex(cm, n) {
2993
    if (n >= cm.display.viewTo) { return null }
2994
    n -= cm.display.viewFrom;
2995
    if (n < 0) { return null }
2996
    var view = cm.display.view;
2997
    for (var i = 0; i < view.length; i++) {
2998
      n -= view[i].size;
2999
      if (n < 0) { return i }
3000
    }
3001
  }
3002
 
3003
  // Updates the display.view data structure for a given change to the
3004
  // document. From and to are in pre-change coordinates. Lendiff is
3005
  // the amount of lines added or subtracted by the change. This is
3006
  // used for changes that span multiple lines, or change the way
3007
  // lines are divided into visual lines. regLineChange (below)
3008
  // registers single-line changes.
3009
  function regChange(cm, from, to, lendiff) {
3010
    if (from == null) { from = cm.doc.first; }
3011
    if (to == null) { to = cm.doc.first + cm.doc.size; }
3012
    if (!lendiff) { lendiff = 0; }
3013
 
3014
    var display = cm.display;
3015
    if (lendiff && to < display.viewTo &&
3016
        (display.updateLineNumbers == null || display.updateLineNumbers > from))
3017
      { display.updateLineNumbers = from; }
3018
 
3019
    cm.curOp.viewChanged = true;
3020
 
3021
    if (from >= display.viewTo) { // Change after
3022
      if (sawCollapsedSpans && visualLineNo(cm.doc, from) < display.viewTo)
3023
        { resetView(cm); }
3024
    } else if (to <= display.viewFrom) { // Change before
3025
      if (sawCollapsedSpans && visualLineEndNo(cm.doc, to + lendiff) > display.viewFrom) {
3026
        resetView(cm);
3027
      } else {
3028
        display.viewFrom += lendiff;
3029
        display.viewTo += lendiff;
3030
      }
3031
    } else if (from <= display.viewFrom && to >= display.viewTo) { // Full overlap
3032
      resetView(cm);
3033
    } else if (from <= display.viewFrom) { // Top overlap
3034
      var cut = viewCuttingPoint(cm, to, to + lendiff, 1);
3035
      if (cut) {
3036
        display.view = display.view.slice(cut.index);
3037
        display.viewFrom = cut.lineN;
3038
        display.viewTo += lendiff;
3039
      } else {
3040
        resetView(cm);
3041
      }
3042
    } else if (to >= display.viewTo) { // Bottom overlap
3043
      var cut$1 = viewCuttingPoint(cm, from, from, -1);
3044
      if (cut$1) {
3045
        display.view = display.view.slice(0, cut$1.index);
3046
        display.viewTo = cut$1.lineN;
3047
      } else {
3048
        resetView(cm);
3049
      }
3050
    } else { // Gap in the middle
3051
      var cutTop = viewCuttingPoint(cm, from, from, -1);
3052
      var cutBot = viewCuttingPoint(cm, to, to + lendiff, 1);
3053
      if (cutTop && cutBot) {
3054
        display.view = display.view.slice(0, cutTop.index)
3055
          .concat(buildViewArray(cm, cutTop.lineN, cutBot.lineN))
3056
          .concat(display.view.slice(cutBot.index));
3057
        display.viewTo += lendiff;
3058
      } else {
3059
        resetView(cm);
3060
      }
3061
    }
3062
 
3063
    var ext = display.externalMeasured;
3064
    if (ext) {
3065
      if (to < ext.lineN)
3066
        { ext.lineN += lendiff; }
3067
      else if (from < ext.lineN + ext.size)
3068
        { display.externalMeasured = null; }
3069
    }
3070
  }
3071
 
3072
  // Register a change to a single line. Type must be one of "text",
3073
  // "gutter", "class", "widget"
3074
  function regLineChange(cm, line, type) {
3075
    cm.curOp.viewChanged = true;
3076
    var display = cm.display, ext = cm.display.externalMeasured;
3077
    if (ext && line >= ext.lineN && line < ext.lineN + ext.size)
3078
      { display.externalMeasured = null; }
3079
 
3080
    if (line < display.viewFrom || line >= display.viewTo) { return }
3081
    var lineView = display.view[findViewIndex(cm, line)];
3082
    if (lineView.node == null) { return }
3083
    var arr = lineView.changes || (lineView.changes = []);
3084
    if (indexOf(arr, type) == -1) { arr.push(type); }
3085
  }
3086
 
3087
  // Clear the view.
3088
  function resetView(cm) {
3089
    cm.display.viewFrom = cm.display.viewTo = cm.doc.first;
3090
    cm.display.view = [];
3091
    cm.display.viewOffset = 0;
3092
  }
3093
 
3094
  function viewCuttingPoint(cm, oldN, newN, dir) {
3095
    var index = findViewIndex(cm, oldN), diff, view = cm.display.view;
3096
    if (!sawCollapsedSpans || newN == cm.doc.first + cm.doc.size)
3097
      { return {index: index, lineN: newN} }
3098
    var n = cm.display.viewFrom;
3099
    for (var i = 0; i < index; i++)
3100
      { n += view[i].size; }
3101
    if (n != oldN) {
3102
      if (dir > 0) {
3103
        if (index == view.length - 1) { return null }
3104
        diff = (n + view[index].size) - oldN;
3105
        index++;
3106
      } else {
3107
        diff = n - oldN;
3108
      }
3109
      oldN += diff; newN += diff;
3110
    }
3111
    while (visualLineNo(cm.doc, newN) != newN) {
3112
      if (index == (dir < 0 ? 0 : view.length - 1)) { return null }
3113
      newN += dir * view[index - (dir < 0 ? 1 : 0)].size;
3114
      index += dir;
3115
    }
3116
    return {index: index, lineN: newN}
3117
  }
3118
 
3119
  // Force the view to cover a given range, adding empty view element
3120
  // or clipping off existing ones as needed.
3121
  function adjustView(cm, from, to) {
3122
    var display = cm.display, view = display.view;
3123
    if (view.length == 0 || from >= display.viewTo || to <= display.viewFrom) {
3124
      display.view = buildViewArray(cm, from, to);
3125
      display.viewFrom = from;
3126
    } else {
3127
      if (display.viewFrom > from)
3128
        { display.view = buildViewArray(cm, from, display.viewFrom).concat(display.view); }
3129
      else if (display.viewFrom < from)
3130
        { display.view = display.view.slice(findViewIndex(cm, from)); }
3131
      display.viewFrom = from;
3132
      if (display.viewTo < to)
3133
        { display.view = display.view.concat(buildViewArray(cm, display.viewTo, to)); }
3134
      else if (display.viewTo > to)
3135
        { display.view = display.view.slice(0, findViewIndex(cm, to)); }
3136
    }
3137
    display.viewTo = to;
3138
  }
3139
 
3140
  // Count the number of lines in the view whose DOM representation is
3141
  // out of date (or nonexistent).
3142
  function countDirtyView(cm) {
3143
    var view = cm.display.view, dirty = 0;
3144
    for (var i = 0; i < view.length; i++) {
3145
      var lineView = view[i];
3146
      if (!lineView.hidden && (!lineView.node || lineView.changes)) { ++dirty; }
3147
    }
3148
    return dirty
3149
  }
3150
 
3151
  function updateSelection(cm) {
3152
    cm.display.input.showSelection(cm.display.input.prepareSelection());
3153
  }
3154
 
3155
  function prepareSelection(cm, primary) {
3156
    if ( primary === void 0 ) primary = true;
3157
 
3158
    var doc = cm.doc, result = {};
3159
    var curFragment = result.cursors = document.createDocumentFragment();
3160
    var selFragment = result.selection = document.createDocumentFragment();
3161
 
16493 obado 3162
    var customCursor = cm.options.$customCursor;
3163
    if (customCursor) { primary = true; }
14283 obado 3164
    for (var i = 0; i < doc.sel.ranges.length; i++) {
3165
      if (!primary && i == doc.sel.primIndex) { continue }
15152 obado 3166
      var range = doc.sel.ranges[i];
3167
      if (range.from().line >= cm.display.viewTo || range.to().line < cm.display.viewFrom) { continue }
3168
      var collapsed = range.empty();
16493 obado 3169
      if (customCursor) {
3170
        var head = customCursor(cm, range);
3171
        if (head) { drawSelectionCursor(cm, head, curFragment); }
3172
      } else if (collapsed || cm.options.showCursorWhenSelecting) {
3173
        drawSelectionCursor(cm, range.head, curFragment);
3174
      }
14283 obado 3175
      if (!collapsed)
15152 obado 3176
        { drawSelectionRange(cm, range, selFragment); }
14283 obado 3177
    }
3178
    return result
3179
  }
3180
 
3181
  // Draws a cursor for the given range
3182
  function drawSelectionCursor(cm, head, output) {
3183
    var pos = cursorCoords(cm, head, "div", null, null, !cm.options.singleCursorHeightPerLine);
3184
 
3185
    var cursor = output.appendChild(elt("div", "\u00a0", "CodeMirror-cursor"));
3186
    cursor.style.left = pos.left + "px";
3187
    cursor.style.top = pos.top + "px";
3188
    cursor.style.height = Math.max(0, pos.bottom - pos.top) * cm.options.cursorHeight + "px";
3189
 
16493 obado 3190
    if (/\bcm-fat-cursor\b/.test(cm.getWrapperElement().className)) {
3191
      var charPos = charCoords(cm, head, "div", null, null);
3192
      var width = charPos.right - charPos.left;
3193
      cursor.style.width = (width > 0 ? width : cm.defaultCharWidth()) + "px";
3194
    }
3195
 
14283 obado 3196
    if (pos.other) {
3197
      // Secondary cursor, shown when on a 'jump' in bi-directional text
3198
      var otherCursor = output.appendChild(elt("div", "\u00a0", "CodeMirror-cursor CodeMirror-secondarycursor"));
3199
      otherCursor.style.display = "";
3200
      otherCursor.style.left = pos.other.left + "px";
3201
      otherCursor.style.top = pos.other.top + "px";
3202
      otherCursor.style.height = (pos.other.bottom - pos.other.top) * .85 + "px";
3203
    }
3204
  }
3205
 
3206
  function cmpCoords(a, b) { return a.top - b.top || a.left - b.left }
3207
 
3208
  // Draws the given range as a highlighted selection
15152 obado 3209
  function drawSelectionRange(cm, range, output) {
14283 obado 3210
    var display = cm.display, doc = cm.doc;
3211
    var fragment = document.createDocumentFragment();
3212
    var padding = paddingH(cm.display), leftSide = padding.left;
3213
    var rightSide = Math.max(display.sizerWidth, displayWidth(cm) - display.sizer.offsetLeft) - padding.right;
3214
    var docLTR = doc.direction == "ltr";
3215
 
3216
    function add(left, top, width, bottom) {
3217
      if (top < 0) { top = 0; }
3218
      top = Math.round(top);
3219
      bottom = Math.round(bottom);
3220
      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")));
3221
    }
3222
 
3223
    function drawForLine(line, fromArg, toArg) {
3224
      var lineObj = getLine(doc, line);
3225
      var lineLen = lineObj.text.length;
3226
      var start, end;
3227
      function coords(ch, bias) {
3228
        return charCoords(cm, Pos(line, ch), "div", lineObj, bias)
3229
      }
3230
 
3231
      function wrapX(pos, dir, side) {
3232
        var extent = wrappedLineExtentChar(cm, lineObj, null, pos);
3233
        var prop = (dir == "ltr") == (side == "after") ? "left" : "right";
3234
        var ch = side == "after" ? extent.begin : extent.end - (/\s/.test(lineObj.text.charAt(extent.end - 1)) ? 2 : 1);
3235
        return coords(ch, prop)[prop]
3236
      }
3237
 
3238
      var order = getOrder(lineObj, doc.direction);
3239
      iterateBidiSections(order, fromArg || 0, toArg == null ? lineLen : toArg, function (from, to, dir, i) {
3240
        var ltr = dir == "ltr";
3241
        var fromPos = coords(from, ltr ? "left" : "right");
3242
        var toPos = coords(to - 1, ltr ? "right" : "left");
3243
 
3244
        var openStart = fromArg == null && from == 0, openEnd = toArg == null && to == lineLen;
3245
        var first = i == 0, last = !order || i == order.length - 1;
3246
        if (toPos.top - fromPos.top <= 3) { // Single line
3247
          var openLeft = (docLTR ? openStart : openEnd) && first;
3248
          var openRight = (docLTR ? openEnd : openStart) && last;
3249
          var left = openLeft ? leftSide : (ltr ? fromPos : toPos).left;
3250
          var right = openRight ? rightSide : (ltr ? toPos : fromPos).right;
3251
          add(left, fromPos.top, right - left, fromPos.bottom);
3252
        } else { // Multiple lines
3253
          var topLeft, topRight, botLeft, botRight;
3254
          if (ltr) {
3255
            topLeft = docLTR && openStart && first ? leftSide : fromPos.left;
3256
            topRight = docLTR ? rightSide : wrapX(from, dir, "before");
3257
            botLeft = docLTR ? leftSide : wrapX(to, dir, "after");
3258
            botRight = docLTR && openEnd && last ? rightSide : toPos.right;
3259
          } else {
3260
            topLeft = !docLTR ? leftSide : wrapX(from, dir, "before");
3261
            topRight = !docLTR && openStart && first ? rightSide : fromPos.right;
3262
            botLeft = !docLTR && openEnd && last ? leftSide : toPos.left;
3263
            botRight = !docLTR ? rightSide : wrapX(to, dir, "after");
3264
          }
3265
          add(topLeft, fromPos.top, topRight - topLeft, fromPos.bottom);
3266
          if (fromPos.bottom < toPos.top) { add(leftSide, fromPos.bottom, null, toPos.top); }
3267
          add(botLeft, toPos.top, botRight - botLeft, toPos.bottom);
3268
        }
3269
 
3270
        if (!start || cmpCoords(fromPos, start) < 0) { start = fromPos; }
3271
        if (cmpCoords(toPos, start) < 0) { start = toPos; }
3272
        if (!end || cmpCoords(fromPos, end) < 0) { end = fromPos; }
3273
        if (cmpCoords(toPos, end) < 0) { end = toPos; }
3274
      });
3275
      return {start: start, end: end}
3276
    }
3277
 
15152 obado 3278
    var sFrom = range.from(), sTo = range.to();
14283 obado 3279
    if (sFrom.line == sTo.line) {
3280
      drawForLine(sFrom.line, sFrom.ch, sTo.ch);
3281
    } else {
3282
      var fromLine = getLine(doc, sFrom.line), toLine = getLine(doc, sTo.line);
3283
      var singleVLine = visualLine(fromLine) == visualLine(toLine);
3284
      var leftEnd = drawForLine(sFrom.line, sFrom.ch, singleVLine ? fromLine.text.length + 1 : null).end;
3285
      var rightStart = drawForLine(sTo.line, singleVLine ? 0 : null, sTo.ch).start;
3286
      if (singleVLine) {
3287
        if (leftEnd.top < rightStart.top - 2) {
3288
          add(leftEnd.right, leftEnd.top, null, leftEnd.bottom);
3289
          add(leftSide, rightStart.top, rightStart.left, rightStart.bottom);
3290
        } else {
3291
          add(leftEnd.right, leftEnd.top, rightStart.left - leftEnd.right, leftEnd.bottom);
3292
        }
3293
      }
3294
      if (leftEnd.bottom < rightStart.top)
3295
        { add(leftSide, leftEnd.bottom, null, rightStart.top); }
3296
    }
3297
 
3298
    output.appendChild(fragment);
3299
  }
3300
 
3301
  // Cursor-blinking
3302
  function restartBlink(cm) {
3303
    if (!cm.state.focused) { return }
3304
    var display = cm.display;
3305
    clearInterval(display.blinker);
3306
    var on = true;
3307
    display.cursorDiv.style.visibility = "";
3308
    if (cm.options.cursorBlinkRate > 0)
16493 obado 3309
      { display.blinker = setInterval(function () {
3310
        if (!cm.hasFocus()) { onBlur(cm); }
3311
        display.cursorDiv.style.visibility = (on = !on) ? "" : "hidden";
3312
      }, cm.options.cursorBlinkRate); }
14283 obado 3313
    else if (cm.options.cursorBlinkRate < 0)
3314
      { display.cursorDiv.style.visibility = "hidden"; }
3315
  }
3316
 
3317
  function ensureFocus(cm) {
16493 obado 3318
    if (!cm.hasFocus()) {
3319
      cm.display.input.focus();
3320
      if (!cm.state.focused) { onFocus(cm); }
3321
    }
14283 obado 3322
  }
3323
 
3324
  function delayBlurEvent(cm) {
3325
    cm.state.delayingBlurEvent = true;
3326
    setTimeout(function () { if (cm.state.delayingBlurEvent) {
3327
      cm.state.delayingBlurEvent = false;
16493 obado 3328
      if (cm.state.focused) { onBlur(cm); }
14283 obado 3329
    } }, 100);
3330
  }
3331
 
3332
  function onFocus(cm, e) {
16493 obado 3333
    if (cm.state.delayingBlurEvent && !cm.state.draggingText) { cm.state.delayingBlurEvent = false; }
14283 obado 3334
 
3335
    if (cm.options.readOnly == "nocursor") { return }
3336
    if (!cm.state.focused) {
3337
      signal(cm, "focus", cm, e);
3338
      cm.state.focused = true;
3339
      addClass(cm.display.wrapper, "CodeMirror-focused");
3340
      // This test prevents this from firing when a context
3341
      // menu is closed (since the input reset would kill the
3342
      // select-all detection hack)
3343
      if (!cm.curOp && cm.display.selForContextMenu != cm.doc.sel) {
3344
        cm.display.input.reset();
3345
        if (webkit) { setTimeout(function () { return cm.display.input.reset(true); }, 20); } // Issue #1730
3346
      }
3347
      cm.display.input.receivedFocus();
3348
    }
3349
    restartBlink(cm);
3350
  }
3351
  function onBlur(cm, e) {
3352
    if (cm.state.delayingBlurEvent) { return }
3353
 
3354
    if (cm.state.focused) {
3355
      signal(cm, "blur", cm, e);
3356
      cm.state.focused = false;
3357
      rmClass(cm.display.wrapper, "CodeMirror-focused");
3358
    }
3359
    clearInterval(cm.display.blinker);
3360
    setTimeout(function () { if (!cm.state.focused) { cm.display.shift = false; } }, 150);
3361
  }
3362
 
3363
  // Read the actual heights of the rendered lines, and update their
3364
  // stored heights to match.
3365
  function updateHeightsInViewport(cm) {
3366
    var display = cm.display;
3367
    var prevBottom = display.lineDiv.offsetTop;
16493 obado 3368
    var viewTop = Math.max(0, display.scroller.getBoundingClientRect().top);
3369
    var oldHeight = display.lineDiv.getBoundingClientRect().top;
3370
    var mustScroll = 0;
14283 obado 3371
    for (var i = 0; i < display.view.length; i++) {
3372
      var cur = display.view[i], wrapping = cm.options.lineWrapping;
3373
      var height = (void 0), width = 0;
3374
      if (cur.hidden) { continue }
16493 obado 3375
      oldHeight += cur.line.height;
14283 obado 3376
      if (ie && ie_version < 8) {
3377
        var bot = cur.node.offsetTop + cur.node.offsetHeight;
3378
        height = bot - prevBottom;
3379
        prevBottom = bot;
3380
      } else {
3381
        var box = cur.node.getBoundingClientRect();
3382
        height = box.bottom - box.top;
3383
        // Check that lines don't extend past the right of the current
3384
        // editor width
3385
        if (!wrapping && cur.text.firstChild)
3386
          { width = cur.text.firstChild.getBoundingClientRect().right - box.left - 1; }
3387
      }
3388
      var diff = cur.line.height - height;
3389
      if (diff > .005 || diff < -.005) {
16493 obado 3390
        if (oldHeight < viewTop) { mustScroll -= diff; }
14283 obado 3391
        updateLineHeight(cur.line, height);
3392
        updateWidgetHeight(cur.line);
3393
        if (cur.rest) { for (var j = 0; j < cur.rest.length; j++)
3394
          { updateWidgetHeight(cur.rest[j]); } }
3395
      }
3396
      if (width > cm.display.sizerWidth) {
3397
        var chWidth = Math.ceil(width / charWidth(cm.display));
3398
        if (chWidth > cm.display.maxLineLength) {
3399
          cm.display.maxLineLength = chWidth;
3400
          cm.display.maxLine = cur.line;
3401
          cm.display.maxLineChanged = true;
3402
        }
3403
      }
3404
    }
16493 obado 3405
    if (Math.abs(mustScroll) > 2) { display.scroller.scrollTop += mustScroll; }
14283 obado 3406
  }
3407
 
3408
  // Read and store the height of line widgets associated with the
3409
  // given line.
3410
  function updateWidgetHeight(line) {
3411
    if (line.widgets) { for (var i = 0; i < line.widgets.length; ++i) {
3412
      var w = line.widgets[i], parent = w.node.parentNode;
3413
      if (parent) { w.height = parent.offsetHeight; }
3414
    } }
3415
  }
3416
 
3417
  // Compute the lines that are visible in a given viewport (defaults
3418
  // the the current scroll position). viewport may contain top,
3419
  // height, and ensure (see op.scrollToPos) properties.
3420
  function visibleLines(display, doc, viewport) {
3421
    var top = viewport && viewport.top != null ? Math.max(0, viewport.top) : display.scroller.scrollTop;
3422
    top = Math.floor(top - paddingTop(display));
3423
    var bottom = viewport && viewport.bottom != null ? viewport.bottom : top + display.wrapper.clientHeight;
3424
 
3425
    var from = lineAtHeight(doc, top), to = lineAtHeight(doc, bottom);
3426
    // Ensure is a {from: {line, ch}, to: {line, ch}} object, and
3427
    // forces those lines into the viewport (if possible).
3428
    if (viewport && viewport.ensure) {
3429
      var ensureFrom = viewport.ensure.from.line, ensureTo = viewport.ensure.to.line;
3430
      if (ensureFrom < from) {
3431
        from = ensureFrom;
3432
        to = lineAtHeight(doc, heightAtLine(getLine(doc, ensureFrom)) + display.wrapper.clientHeight);
3433
      } else if (Math.min(ensureTo, doc.lastLine()) >= to) {
3434
        from = lineAtHeight(doc, heightAtLine(getLine(doc, ensureTo)) - display.wrapper.clientHeight);
3435
        to = ensureTo;
3436
      }
3437
    }
3438
    return {from: from, to: Math.max(to, from + 1)}
3439
  }
3440
 
3441
  // SCROLLING THINGS INTO VIEW
3442
 
3443
  // If an editor sits on the top or bottom of the window, partially
3444
  // scrolled out of view, this ensures that the cursor is visible.
3445
  function maybeScrollWindow(cm, rect) {
3446
    if (signalDOMEvent(cm, "scrollCursorIntoView")) { return }
3447
 
3448
    var display = cm.display, box = display.sizer.getBoundingClientRect(), doScroll = null;
17702 obado 3449
    var doc = display.wrapper.ownerDocument;
14283 obado 3450
    if (rect.top + box.top < 0) { doScroll = true; }
17702 obado 3451
    else if (rect.bottom + box.top > (doc.defaultView.innerHeight || doc.documentElement.clientHeight)) { doScroll = false; }
14283 obado 3452
    if (doScroll != null && !phantom) {
3453
      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;"));
3454
      cm.display.lineSpace.appendChild(scrollNode);
3455
      scrollNode.scrollIntoView(doScroll);
3456
      cm.display.lineSpace.removeChild(scrollNode);
3457
    }
3458
  }
3459
 
3460
  // Scroll a given position into view (immediately), verifying that
3461
  // it actually became visible (as line heights are accurately
3462
  // measured, the position of something may 'drift' during drawing).
3463
  function scrollPosIntoView(cm, pos, end, margin) {
3464
    if (margin == null) { margin = 0; }
3465
    var rect;
3466
    if (!cm.options.lineWrapping && pos == end) {
3467
      // Set pos and end to the cursor positions around the character pos sticks to
3468
      // If pos.sticky == "before", that is around pos.ch - 1, otherwise around pos.ch
3469
      // If pos == Pos(_, 0, "before"), pos and end are unchanged
16493 obado 3470
      end = pos.sticky == "before" ? Pos(pos.line, pos.ch + 1, "before") : pos;
14283 obado 3471
      pos = pos.ch ? Pos(pos.line, pos.sticky == "before" ? pos.ch - 1 : pos.ch, "after") : pos;
3472
    }
3473
    for (var limit = 0; limit < 5; limit++) {
3474
      var changed = false;
3475
      var coords = cursorCoords(cm, pos);
3476
      var endCoords = !end || end == pos ? coords : cursorCoords(cm, end);
3477
      rect = {left: Math.min(coords.left, endCoords.left),
3478
              top: Math.min(coords.top, endCoords.top) - margin,
3479
              right: Math.max(coords.left, endCoords.left),
3480
              bottom: Math.max(coords.bottom, endCoords.bottom) + margin};
3481
      var scrollPos = calculateScrollPos(cm, rect);
3482
      var startTop = cm.doc.scrollTop, startLeft = cm.doc.scrollLeft;
3483
      if (scrollPos.scrollTop != null) {
3484
        updateScrollTop(cm, scrollPos.scrollTop);
3485
        if (Math.abs(cm.doc.scrollTop - startTop) > 1) { changed = true; }
3486
      }
3487
      if (scrollPos.scrollLeft != null) {
3488
        setScrollLeft(cm, scrollPos.scrollLeft);
3489
        if (Math.abs(cm.doc.scrollLeft - startLeft) > 1) { changed = true; }
3490
      }
3491
      if (!changed) { break }
3492
    }
3493
    return rect
3494
  }
3495
 
3496
  // Scroll a given set of coordinates into view (immediately).
3497
  function scrollIntoView(cm, rect) {
3498
    var scrollPos = calculateScrollPos(cm, rect);
3499
    if (scrollPos.scrollTop != null) { updateScrollTop(cm, scrollPos.scrollTop); }
3500
    if (scrollPos.scrollLeft != null) { setScrollLeft(cm, scrollPos.scrollLeft); }
3501
  }
3502
 
3503
  // Calculate a new scroll position needed to scroll the given
3504
  // rectangle into view. Returns an object with scrollTop and
3505
  // scrollLeft properties. When these are undefined, the
3506
  // vertical/horizontal position does not need to be adjusted.
3507
  function calculateScrollPos(cm, rect) {
3508
    var display = cm.display, snapMargin = textHeight(cm.display);
3509
    if (rect.top < 0) { rect.top = 0; }
3510
    var screentop = cm.curOp && cm.curOp.scrollTop != null ? cm.curOp.scrollTop : display.scroller.scrollTop;
3511
    var screen = displayHeight(cm), result = {};
3512
    if (rect.bottom - rect.top > screen) { rect.bottom = rect.top + screen; }
3513
    var docBottom = cm.doc.height + paddingVert(display);
3514
    var atTop = rect.top < snapMargin, atBottom = rect.bottom > docBottom - snapMargin;
3515
    if (rect.top < screentop) {
3516
      result.scrollTop = atTop ? 0 : rect.top;
3517
    } else if (rect.bottom > screentop + screen) {
3518
      var newTop = Math.min(rect.top, (atBottom ? docBottom : rect.bottom) - screen);
3519
      if (newTop != screentop) { result.scrollTop = newTop; }
3520
    }
3521
 
16493 obado 3522
    var gutterSpace = cm.options.fixedGutter ? 0 : display.gutters.offsetWidth;
3523
    var screenleft = cm.curOp && cm.curOp.scrollLeft != null ? cm.curOp.scrollLeft : display.scroller.scrollLeft - gutterSpace;
3524
    var screenw = displayWidth(cm) - display.gutters.offsetWidth;
14283 obado 3525
    var tooWide = rect.right - rect.left > screenw;
3526
    if (tooWide) { rect.right = rect.left + screenw; }
3527
    if (rect.left < 10)
3528
      { result.scrollLeft = 0; }
3529
    else if (rect.left < screenleft)
16493 obado 3530
      { result.scrollLeft = Math.max(0, rect.left + gutterSpace - (tooWide ? 0 : 10)); }
14283 obado 3531
    else if (rect.right > screenw + screenleft - 3)
3532
      { result.scrollLeft = rect.right + (tooWide ? 0 : 10) - screenw; }
3533
    return result
3534
  }
3535
 
3536
  // Store a relative adjustment to the scroll position in the current
3537
  // operation (to be applied when the operation finishes).
3538
  function addToScrollTop(cm, top) {
3539
    if (top == null) { return }
3540
    resolveScrollToPos(cm);
3541
    cm.curOp.scrollTop = (cm.curOp.scrollTop == null ? cm.doc.scrollTop : cm.curOp.scrollTop) + top;
3542
  }
3543
 
3544
  // Make sure that at the end of the operation the current cursor is
3545
  // shown.
3546
  function ensureCursorVisible(cm) {
3547
    resolveScrollToPos(cm);
3548
    var cur = cm.getCursor();
3549
    cm.curOp.scrollToPos = {from: cur, to: cur, margin: cm.options.cursorScrollMargin};
3550
  }
3551
 
3552
  function scrollToCoords(cm, x, y) {
3553
    if (x != null || y != null) { resolveScrollToPos(cm); }
3554
    if (x != null) { cm.curOp.scrollLeft = x; }
3555
    if (y != null) { cm.curOp.scrollTop = y; }
3556
  }
3557
 
15152 obado 3558
  function scrollToRange(cm, range) {
14283 obado 3559
    resolveScrollToPos(cm);
15152 obado 3560
    cm.curOp.scrollToPos = range;
14283 obado 3561
  }
3562
 
3563
  // When an operation has its scrollToPos property set, and another
3564
  // scroll action is applied before the end of the operation, this
3565
  // 'simulates' scrolling that position into view in a cheap way, so
3566
  // that the effect of intermediate scroll commands is not ignored.
3567
  function resolveScrollToPos(cm) {
15152 obado 3568
    var range = cm.curOp.scrollToPos;
3569
    if (range) {
14283 obado 3570
      cm.curOp.scrollToPos = null;
15152 obado 3571
      var from = estimateCoords(cm, range.from), to = estimateCoords(cm, range.to);
3572
      scrollToCoordsRange(cm, from, to, range.margin);
14283 obado 3573
    }
3574
  }
3575
 
3576
  function scrollToCoordsRange(cm, from, to, margin) {
3577
    var sPos = calculateScrollPos(cm, {
3578
      left: Math.min(from.left, to.left),
3579
      top: Math.min(from.top, to.top) - margin,
3580
      right: Math.max(from.right, to.right),
3581
      bottom: Math.max(from.bottom, to.bottom) + margin
3582
    });
3583
    scrollToCoords(cm, sPos.scrollLeft, sPos.scrollTop);
3584
  }
3585
 
3586
  // Sync the scrollable area and scrollbars, ensure the viewport
3587
  // covers the visible area.
3588
  function updateScrollTop(cm, val) {
3589
    if (Math.abs(cm.doc.scrollTop - val) < 2) { return }
3590
    if (!gecko) { updateDisplaySimple(cm, {top: val}); }
3591
    setScrollTop(cm, val, true);
3592
    if (gecko) { updateDisplaySimple(cm); }
3593
    startWorker(cm, 100);
3594
  }
3595
 
3596
  function setScrollTop(cm, val, forceScroll) {
15152 obado 3597
    val = Math.max(0, Math.min(cm.display.scroller.scrollHeight - cm.display.scroller.clientHeight, val));
14283 obado 3598
    if (cm.display.scroller.scrollTop == val && !forceScroll) { return }
3599
    cm.doc.scrollTop = val;
3600
    cm.display.scrollbars.setScrollTop(val);
3601
    if (cm.display.scroller.scrollTop != val) { cm.display.scroller.scrollTop = val; }
3602
  }
3603
 
3604
  // Sync scroller and scrollbar, ensure the gutter elements are
3605
  // aligned.
3606
  function setScrollLeft(cm, val, isScroller, forceScroll) {
15152 obado 3607
    val = Math.max(0, Math.min(val, cm.display.scroller.scrollWidth - cm.display.scroller.clientWidth));
14283 obado 3608
    if ((isScroller ? val == cm.doc.scrollLeft : Math.abs(cm.doc.scrollLeft - val) < 2) && !forceScroll) { return }
3609
    cm.doc.scrollLeft = val;
3610
    alignHorizontally(cm);
3611
    if (cm.display.scroller.scrollLeft != val) { cm.display.scroller.scrollLeft = val; }
3612
    cm.display.scrollbars.setScrollLeft(val);
3613
  }
3614
 
3615
  // SCROLLBARS
3616
 
3617
  // Prepare DOM reads needed to update the scrollbars. Done in one
3618
  // shot to minimize update/measure roundtrips.
3619
  function measureForScrollbars(cm) {
3620
    var d = cm.display, gutterW = d.gutters.offsetWidth;
3621
    var docH = Math.round(cm.doc.height + paddingVert(cm.display));
3622
    return {
3623
      clientHeight: d.scroller.clientHeight,
3624
      viewHeight: d.wrapper.clientHeight,
3625
      scrollWidth: d.scroller.scrollWidth, clientWidth: d.scroller.clientWidth,
3626
      viewWidth: d.wrapper.clientWidth,
3627
      barLeft: cm.options.fixedGutter ? gutterW : 0,
3628
      docHeight: docH,
3629
      scrollHeight: docH + scrollGap(cm) + d.barHeight,
3630
      nativeBarWidth: d.nativeBarWidth,
3631
      gutterWidth: gutterW
3632
    }
3633
  }
3634
 
3635
  var NativeScrollbars = function(place, scroll, cm) {
3636
    this.cm = cm;
3637
    var vert = this.vert = elt("div", [elt("div", null, null, "min-width: 1px")], "CodeMirror-vscrollbar");
3638
    var horiz = this.horiz = elt("div", [elt("div", null, null, "height: 100%; min-height: 1px")], "CodeMirror-hscrollbar");
3639
    vert.tabIndex = horiz.tabIndex = -1;
3640
    place(vert); place(horiz);
3641
 
3642
    on(vert, "scroll", function () {
3643
      if (vert.clientHeight) { scroll(vert.scrollTop, "vertical"); }
3644
    });
3645
    on(horiz, "scroll", function () {
3646
      if (horiz.clientWidth) { scroll(horiz.scrollLeft, "horizontal"); }
3647
    });
3648
 
3649
    this.checkedZeroWidth = false;
3650
    // Need to set a minimum width to see the scrollbar on IE7 (but must not set it on IE8).
3651
    if (ie && ie_version < 8) { this.horiz.style.minHeight = this.vert.style.minWidth = "18px"; }
3652
  };
3653
 
3654
  NativeScrollbars.prototype.update = function (measure) {
3655
    var needsH = measure.scrollWidth > measure.clientWidth + 1;
3656
    var needsV = measure.scrollHeight > measure.clientHeight + 1;
3657
    var sWidth = measure.nativeBarWidth;
3658
 
3659
    if (needsV) {
3660
      this.vert.style.display = "block";
3661
      this.vert.style.bottom = needsH ? sWidth + "px" : "0";
3662
      var totalHeight = measure.viewHeight - (needsH ? sWidth : 0);
3663
      // A bug in IE8 can cause this value to be negative, so guard it.
3664
      this.vert.firstChild.style.height =
3665
        Math.max(0, measure.scrollHeight - measure.clientHeight + totalHeight) + "px";
3666
    } else {
16493 obado 3667
      this.vert.scrollTop = 0;
14283 obado 3668
      this.vert.style.display = "";
3669
      this.vert.firstChild.style.height = "0";
3670
    }
3671
 
3672
    if (needsH) {
3673
      this.horiz.style.display = "block";
3674
      this.horiz.style.right = needsV ? sWidth + "px" : "0";
3675
      this.horiz.style.left = measure.barLeft + "px";
3676
      var totalWidth = measure.viewWidth - measure.barLeft - (needsV ? sWidth : 0);
3677
      this.horiz.firstChild.style.width =
3678
        Math.max(0, measure.scrollWidth - measure.clientWidth + totalWidth) + "px";
3679
    } else {
3680
      this.horiz.style.display = "";
3681
      this.horiz.firstChild.style.width = "0";
3682
    }
3683
 
3684
    if (!this.checkedZeroWidth && measure.clientHeight > 0) {
3685
      if (sWidth == 0) { this.zeroWidthHack(); }
3686
      this.checkedZeroWidth = true;
3687
    }
3688
 
3689
    return {right: needsV ? sWidth : 0, bottom: needsH ? sWidth : 0}
3690
  };
3691
 
3692
  NativeScrollbars.prototype.setScrollLeft = function (pos) {
3693
    if (this.horiz.scrollLeft != pos) { this.horiz.scrollLeft = pos; }
3694
    if (this.disableHoriz) { this.enableZeroWidthBar(this.horiz, this.disableHoriz, "horiz"); }
3695
  };
3696
 
3697
  NativeScrollbars.prototype.setScrollTop = function (pos) {
3698
    if (this.vert.scrollTop != pos) { this.vert.scrollTop = pos; }
3699
    if (this.disableVert) { this.enableZeroWidthBar(this.vert, this.disableVert, "vert"); }
3700
  };
3701
 
3702
  NativeScrollbars.prototype.zeroWidthHack = function () {
3703
    var w = mac && !mac_geMountainLion ? "12px" : "18px";
3704
    this.horiz.style.height = this.vert.style.width = w;
16859 obado 3705
    this.horiz.style.visibility = this.vert.style.visibility = "hidden";
14283 obado 3706
    this.disableHoriz = new Delayed;
3707
    this.disableVert = new Delayed;
3708
  };
3709
 
3710
  NativeScrollbars.prototype.enableZeroWidthBar = function (bar, delay, type) {
16859 obado 3711
    bar.style.visibility = "";
14283 obado 3712
    function maybeDisable() {
3713
      // To find out whether the scrollbar is still visible, we
3714
      // check whether the element under the pixel in the bottom
3715
      // right corner of the scrollbar box is the scrollbar box
3716
      // itself (when the bar is still visible) or its filler child
3717
      // (when the bar is hidden). If it is still visible, we keep
3718
      // it enabled, if it's hidden, we disable pointer events.
3719
      var box = bar.getBoundingClientRect();
15152 obado 3720
      var elt = type == "vert" ? document.elementFromPoint(box.right - 1, (box.top + box.bottom) / 2)
14283 obado 3721
          : document.elementFromPoint((box.right + box.left) / 2, box.bottom - 1);
16859 obado 3722
      if (elt != bar) { bar.style.visibility = "hidden"; }
14283 obado 3723
      else { delay.set(1000, maybeDisable); }
3724
    }
3725
    delay.set(1000, maybeDisable);
3726
  };
3727
 
3728
  NativeScrollbars.prototype.clear = function () {
3729
    var parent = this.horiz.parentNode;
3730
    parent.removeChild(this.horiz);
3731
    parent.removeChild(this.vert);
3732
  };
3733
 
3734
  var NullScrollbars = function () {};
3735
 
3736
  NullScrollbars.prototype.update = function () { return {bottom: 0, right: 0} };
3737
  NullScrollbars.prototype.setScrollLeft = function () {};
3738
  NullScrollbars.prototype.setScrollTop = function () {};
3739
  NullScrollbars.prototype.clear = function () {};
3740
 
3741
  function updateScrollbars(cm, measure) {
3742
    if (!measure) { measure = measureForScrollbars(cm); }
3743
    var startWidth = cm.display.barWidth, startHeight = cm.display.barHeight;
3744
    updateScrollbarsInner(cm, measure);
3745
    for (var i = 0; i < 4 && startWidth != cm.display.barWidth || startHeight != cm.display.barHeight; i++) {
3746
      if (startWidth != cm.display.barWidth && cm.options.lineWrapping)
3747
        { updateHeightsInViewport(cm); }
3748
      updateScrollbarsInner(cm, measureForScrollbars(cm));
3749
      startWidth = cm.display.barWidth; startHeight = cm.display.barHeight;
3750
    }
3751
  }
3752
 
3753
  // Re-synchronize the fake scrollbars with the actual size of the
3754
  // content.
3755
  function updateScrollbarsInner(cm, measure) {
3756
    var d = cm.display;
3757
    var sizes = d.scrollbars.update(measure);
3758
 
3759
    d.sizer.style.paddingRight = (d.barWidth = sizes.right) + "px";
3760
    d.sizer.style.paddingBottom = (d.barHeight = sizes.bottom) + "px";
3761
    d.heightForcer.style.borderBottom = sizes.bottom + "px solid transparent";
3762
 
3763
    if (sizes.right && sizes.bottom) {
3764
      d.scrollbarFiller.style.display = "block";
3765
      d.scrollbarFiller.style.height = sizes.bottom + "px";
3766
      d.scrollbarFiller.style.width = sizes.right + "px";
3767
    } else { d.scrollbarFiller.style.display = ""; }
3768
    if (sizes.bottom && cm.options.coverGutterNextToScrollbar && cm.options.fixedGutter) {
3769
      d.gutterFiller.style.display = "block";
3770
      d.gutterFiller.style.height = sizes.bottom + "px";
3771
      d.gutterFiller.style.width = measure.gutterWidth + "px";
3772
    } else { d.gutterFiller.style.display = ""; }
3773
  }
3774
 
3775
  var scrollbarModel = {"native": NativeScrollbars, "null": NullScrollbars};
3776
 
3777
  function initScrollbars(cm) {
3778
    if (cm.display.scrollbars) {
3779
      cm.display.scrollbars.clear();
3780
      if (cm.display.scrollbars.addClass)
3781
        { rmClass(cm.display.wrapper, cm.display.scrollbars.addClass); }
3782
    }
3783
 
3784
    cm.display.scrollbars = new scrollbarModel[cm.options.scrollbarStyle](function (node) {
3785
      cm.display.wrapper.insertBefore(node, cm.display.scrollbarFiller);
3786
      // Prevent clicks in the scrollbars from killing focus
3787
      on(node, "mousedown", function () {
3788
        if (cm.state.focused) { setTimeout(function () { return cm.display.input.focus(); }, 0); }
3789
      });
3790
      node.setAttribute("cm-not-content", "true");
3791
    }, function (pos, axis) {
3792
      if (axis == "horizontal") { setScrollLeft(cm, pos); }
3793
      else { updateScrollTop(cm, pos); }
3794
    }, cm);
3795
    if (cm.display.scrollbars.addClass)
3796
      { addClass(cm.display.wrapper, cm.display.scrollbars.addClass); }
3797
  }
3798
 
3799
  // Operations are used to wrap a series of changes to the editor
3800
  // state in such a way that each change won't have to update the
3801
  // cursor and display (which would be awkward, slow, and
3802
  // error-prone). Instead, display updates are batched and then all
3803
  // combined and executed at once.
3804
 
3805
  var nextOpId = 0;
3806
  // Start a new operation.
3807
  function startOperation(cm) {
3808
    cm.curOp = {
3809
      cm: cm,
3810
      viewChanged: false,      // Flag that indicates that lines might need to be redrawn
3811
      startHeight: cm.doc.height, // Used to detect need to update scrollbar
3812
      forceUpdate: false,      // Used to force a redraw
3813
      updateInput: 0,       // Whether to reset the input textarea
3814
      typing: false,           // Whether this reset should be careful to leave existing text (for compositing)
3815
      changeObjs: null,        // Accumulated changes, for firing change events
3816
      cursorActivityHandlers: null, // Set of handlers to fire cursorActivity on
3817
      cursorActivityCalled: 0, // Tracks which cursorActivity handlers have been called already
3818
      selectionChanged: false, // Whether the selection needs to be redrawn
3819
      updateMaxLine: false,    // Set when the widest line needs to be determined anew
3820
      scrollLeft: null, scrollTop: null, // Intermediate scroll position, not pushed to DOM yet
3821
      scrollToPos: null,       // Used to scroll to a specific position
3822
      focus: false,
16493 obado 3823
      id: ++nextOpId,          // Unique ID
3824
      markArrays: null         // Used by addMarkedSpan
14283 obado 3825
    };
3826
    pushOperation(cm.curOp);
3827
  }
3828
 
3829
  // Finish an operation, updating the display and signalling delayed events
3830
  function endOperation(cm) {
3831
    var op = cm.curOp;
3832
    if (op) { finishOperation(op, function (group) {
3833
      for (var i = 0; i < group.ops.length; i++)
3834
        { group.ops[i].cm.curOp = null; }
3835
      endOperations(group);
3836
    }); }
3837
  }
3838
 
3839
  // The DOM updates done when an operation finishes are batched so
3840
  // that the minimum number of relayouts are required.
3841
  function endOperations(group) {
3842
    var ops = group.ops;
3843
    for (var i = 0; i < ops.length; i++) // Read DOM
3844
      { endOperation_R1(ops[i]); }
3845
    for (var i$1 = 0; i$1 < ops.length; i$1++) // Write DOM (maybe)
3846
      { endOperation_W1(ops[i$1]); }
3847
    for (var i$2 = 0; i$2 < ops.length; i$2++) // Read DOM
3848
      { endOperation_R2(ops[i$2]); }
3849
    for (var i$3 = 0; i$3 < ops.length; i$3++) // Write DOM (maybe)
3850
      { endOperation_W2(ops[i$3]); }
3851
    for (var i$4 = 0; i$4 < ops.length; i$4++) // Read DOM
3852
      { endOperation_finish(ops[i$4]); }
3853
  }
3854
 
3855
  function endOperation_R1(op) {
3856
    var cm = op.cm, display = cm.display;
3857
    maybeClipScrollbars(cm);
3858
    if (op.updateMaxLine) { findMaxLine(cm); }
3859
 
3860
    op.mustUpdate = op.viewChanged || op.forceUpdate || op.scrollTop != null ||
3861
      op.scrollToPos && (op.scrollToPos.from.line < display.viewFrom ||
3862
                         op.scrollToPos.to.line >= display.viewTo) ||
3863
      display.maxLineChanged && cm.options.lineWrapping;
3864
    op.update = op.mustUpdate &&
3865
      new DisplayUpdate(cm, op.mustUpdate && {top: op.scrollTop, ensure: op.scrollToPos}, op.forceUpdate);
3866
  }
3867
 
3868
  function endOperation_W1(op) {
3869
    op.updatedDisplay = op.mustUpdate && updateDisplayIfNeeded(op.cm, op.update);
3870
  }
3871
 
3872
  function endOperation_R2(op) {
3873
    var cm = op.cm, display = cm.display;
3874
    if (op.updatedDisplay) { updateHeightsInViewport(cm); }
3875
 
3876
    op.barMeasure = measureForScrollbars(cm);
3877
 
3878
    // If the max line changed since it was last measured, measure it,
3879
    // and ensure the document's width matches it.
3880
    // updateDisplay_W2 will use these properties to do the actual resizing
3881
    if (display.maxLineChanged && !cm.options.lineWrapping) {
3882
      op.adjustWidthTo = measureChar(cm, display.maxLine, display.maxLine.text.length).left + 3;
3883
      cm.display.sizerWidth = op.adjustWidthTo;
3884
      op.barMeasure.scrollWidth =
3885
        Math.max(display.scroller.clientWidth, display.sizer.offsetLeft + op.adjustWidthTo + scrollGap(cm) + cm.display.barWidth);
3886
      op.maxScrollLeft = Math.max(0, display.sizer.offsetLeft + op.adjustWidthTo - displayWidth(cm));
3887
    }
3888
 
3889
    if (op.updatedDisplay || op.selectionChanged)
3890
      { op.preparedSelection = display.input.prepareSelection(); }
3891
  }
3892
 
3893
  function endOperation_W2(op) {
3894
    var cm = op.cm;
3895
 
3896
    if (op.adjustWidthTo != null) {
3897
      cm.display.sizer.style.minWidth = op.adjustWidthTo + "px";
3898
      if (op.maxScrollLeft < cm.doc.scrollLeft)
3899
        { setScrollLeft(cm, Math.min(cm.display.scroller.scrollLeft, op.maxScrollLeft), true); }
3900
      cm.display.maxLineChanged = false;
3901
    }
3902
 
17702 obado 3903
    var takeFocus = op.focus && op.focus == activeElt(doc(cm));
14283 obado 3904
    if (op.preparedSelection)
3905
      { cm.display.input.showSelection(op.preparedSelection, takeFocus); }
3906
    if (op.updatedDisplay || op.startHeight != cm.doc.height)
3907
      { updateScrollbars(cm, op.barMeasure); }
3908
    if (op.updatedDisplay)
3909
      { setDocumentHeight(cm, op.barMeasure); }
3910
 
3911
    if (op.selectionChanged) { restartBlink(cm); }
3912
 
3913
    if (cm.state.focused && op.updateInput)
3914
      { cm.display.input.reset(op.typing); }
3915
    if (takeFocus) { ensureFocus(op.cm); }
3916
  }
3917
 
3918
  function endOperation_finish(op) {
3919
    var cm = op.cm, display = cm.display, doc = cm.doc;
3920
 
3921
    if (op.updatedDisplay) { postUpdateDisplay(cm, op.update); }
3922
 
3923
    // Abort mouse wheel delta measurement, when scrolling explicitly
3924
    if (display.wheelStartX != null && (op.scrollTop != null || op.scrollLeft != null || op.scrollToPos))
3925
      { display.wheelStartX = display.wheelStartY = null; }
3926
 
3927
    // Propagate the scroll position to the actual DOM scroller
3928
    if (op.scrollTop != null) { setScrollTop(cm, op.scrollTop, op.forceScroll); }
3929
 
3930
    if (op.scrollLeft != null) { setScrollLeft(cm, op.scrollLeft, true, true); }
3931
    // If we need to scroll a specific position into view, do so.
3932
    if (op.scrollToPos) {
3933
      var rect = scrollPosIntoView(cm, clipPos(doc, op.scrollToPos.from),
3934
                                   clipPos(doc, op.scrollToPos.to), op.scrollToPos.margin);
3935
      maybeScrollWindow(cm, rect);
3936
    }
3937
 
3938
    // Fire events for markers that are hidden/unidden by editing or
3939
    // undoing
3940
    var hidden = op.maybeHiddenMarkers, unhidden = op.maybeUnhiddenMarkers;
3941
    if (hidden) { for (var i = 0; i < hidden.length; ++i)
3942
      { if (!hidden[i].lines.length) { signal(hidden[i], "hide"); } } }
3943
    if (unhidden) { for (var i$1 = 0; i$1 < unhidden.length; ++i$1)
3944
      { if (unhidden[i$1].lines.length) { signal(unhidden[i$1], "unhide"); } } }
3945
 
3946
    if (display.wrapper.offsetHeight)
3947
      { doc.scrollTop = cm.display.scroller.scrollTop; }
3948
 
3949
    // Fire change events, and delayed event handlers
3950
    if (op.changeObjs)
3951
      { signal(cm, "changes", cm, op.changeObjs); }
3952
    if (op.update)
3953
      { op.update.finish(); }
3954
  }
3955
 
3956
  // Run the given function in an operation
3957
  function runInOp(cm, f) {
3958
    if (cm.curOp) { return f() }
3959
    startOperation(cm);
3960
    try { return f() }
3961
    finally { endOperation(cm); }
3962
  }
3963
  // Wraps a function in an operation. Returns the wrapped function.
3964
  function operation(cm, f) {
3965
    return function() {
3966
      if (cm.curOp) { return f.apply(cm, arguments) }
3967
      startOperation(cm);
3968
      try { return f.apply(cm, arguments) }
3969
      finally { endOperation(cm); }
3970
    }
3971
  }
3972
  // Used to add methods to editor and doc instances, wrapping them in
3973
  // operations.
3974
  function methodOp(f) {
3975
    return function() {
3976
      if (this.curOp) { return f.apply(this, arguments) }
3977
      startOperation(this);
3978
      try { return f.apply(this, arguments) }
3979
      finally { endOperation(this); }
3980
    }
3981
  }
3982
  function docMethodOp(f) {
3983
    return function() {
3984
      var cm = this.cm;
3985
      if (!cm || cm.curOp) { return f.apply(this, arguments) }
3986
      startOperation(cm);
3987
      try { return f.apply(this, arguments) }
3988
      finally { endOperation(cm); }
3989
    }
3990
  }
3991
 
3992
  // HIGHLIGHT WORKER
3993
 
3994
  function startWorker(cm, time) {
3995
    if (cm.doc.highlightFrontier < cm.display.viewTo)
3996
      { cm.state.highlight.set(time, bind(highlightWorker, cm)); }
3997
  }
3998
 
3999
  function highlightWorker(cm) {
4000
    var doc = cm.doc;
4001
    if (doc.highlightFrontier >= cm.display.viewTo) { return }
4002
    var end = +new Date + cm.options.workTime;
4003
    var context = getContextBefore(cm, doc.highlightFrontier);
4004
    var changedLines = [];
4005
 
4006
    doc.iter(context.line, Math.min(doc.first + doc.size, cm.display.viewTo + 500), function (line) {
4007
      if (context.line >= cm.display.viewFrom) { // Visible
4008
        var oldStyles = line.styles;
4009
        var resetState = line.text.length > cm.options.maxHighlightLength ? copyState(doc.mode, context.state) : null;
4010
        var highlighted = highlightLine(cm, line, context, true);
4011
        if (resetState) { context.state = resetState; }
4012
        line.styles = highlighted.styles;
4013
        var oldCls = line.styleClasses, newCls = highlighted.classes;
4014
        if (newCls) { line.styleClasses = newCls; }
4015
        else if (oldCls) { line.styleClasses = null; }
4016
        var ischange = !oldStyles || oldStyles.length != line.styles.length ||
4017
          oldCls != newCls && (!oldCls || !newCls || oldCls.bgClass != newCls.bgClass || oldCls.textClass != newCls.textClass);
4018
        for (var i = 0; !ischange && i < oldStyles.length; ++i) { ischange = oldStyles[i] != line.styles[i]; }
4019
        if (ischange) { changedLines.push(context.line); }
4020
        line.stateAfter = context.save();
4021
        context.nextLine();
4022
      } else {
4023
        if (line.text.length <= cm.options.maxHighlightLength)
4024
          { processLine(cm, line.text, context); }
4025
        line.stateAfter = context.line % 5 == 0 ? context.save() : null;
4026
        context.nextLine();
4027
      }
4028
      if (+new Date > end) {
4029
        startWorker(cm, cm.options.workDelay);
4030
        return true
4031
      }
4032
    });
4033
    doc.highlightFrontier = context.line;
4034
    doc.modeFrontier = Math.max(doc.modeFrontier, context.line);
4035
    if (changedLines.length) { runInOp(cm, function () {
4036
      for (var i = 0; i < changedLines.length; i++)
4037
        { regLineChange(cm, changedLines[i], "text"); }
4038
    }); }
4039
  }
4040
 
4041
  // DISPLAY DRAWING
4042
 
4043
  var DisplayUpdate = function(cm, viewport, force) {
4044
    var display = cm.display;
4045
 
4046
    this.viewport = viewport;
4047
    // Store some values that we'll need later (but don't want to force a relayout for)
4048
    this.visible = visibleLines(display, cm.doc, viewport);
4049
    this.editorIsHidden = !display.wrapper.offsetWidth;
4050
    this.wrapperHeight = display.wrapper.clientHeight;
4051
    this.wrapperWidth = display.wrapper.clientWidth;
4052
    this.oldDisplayWidth = displayWidth(cm);
4053
    this.force = force;
4054
    this.dims = getDimensions(cm);
4055
    this.events = [];
4056
  };
4057
 
4058
  DisplayUpdate.prototype.signal = function (emitter, type) {
4059
    if (hasHandler(emitter, type))
4060
      { this.events.push(arguments); }
4061
  };
4062
  DisplayUpdate.prototype.finish = function () {
4063
    for (var i = 0; i < this.events.length; i++)
15152 obado 4064
      { signal.apply(null, this.events[i]); }
14283 obado 4065
  };
4066
 
4067
  function maybeClipScrollbars(cm) {
4068
    var display = cm.display;
4069
    if (!display.scrollbarsClipped && display.scroller.offsetWidth) {
4070
      display.nativeBarWidth = display.scroller.offsetWidth - display.scroller.clientWidth;
4071
      display.heightForcer.style.height = scrollGap(cm) + "px";
4072
      display.sizer.style.marginBottom = -display.nativeBarWidth + "px";
4073
      display.sizer.style.borderRightWidth = scrollGap(cm) + "px";
4074
      display.scrollbarsClipped = true;
4075
    }
4076
  }
4077
 
4078
  function selectionSnapshot(cm) {
4079
    if (cm.hasFocus()) { return null }
17702 obado 4080
    var active = activeElt(doc(cm));
14283 obado 4081
    if (!active || !contains(cm.display.lineDiv, active)) { return null }
4082
    var result = {activeElt: active};
4083
    if (window.getSelection) {
17702 obado 4084
      var sel = win(cm).getSelection();
14283 obado 4085
      if (sel.anchorNode && sel.extend && contains(cm.display.lineDiv, sel.anchorNode)) {
4086
        result.anchorNode = sel.anchorNode;
4087
        result.anchorOffset = sel.anchorOffset;
4088
        result.focusNode = sel.focusNode;
4089
        result.focusOffset = sel.focusOffset;
4090
      }
4091
    }
4092
    return result
4093
  }
4094
 
4095
  function restoreSelection(snapshot) {
17702 obado 4096
    if (!snapshot || !snapshot.activeElt || snapshot.activeElt == activeElt(snapshot.activeElt.ownerDocument)) { return }
14283 obado 4097
    snapshot.activeElt.focus();
15152 obado 4098
    if (!/^(INPUT|TEXTAREA)$/.test(snapshot.activeElt.nodeName) &&
4099
        snapshot.anchorNode && contains(document.body, snapshot.anchorNode) && contains(document.body, snapshot.focusNode)) {
17702 obado 4100
      var doc = snapshot.activeElt.ownerDocument;
4101
      var sel = doc.defaultView.getSelection(), range = doc.createRange();
15152 obado 4102
      range.setEnd(snapshot.anchorNode, snapshot.anchorOffset);
4103
      range.collapse(false);
14283 obado 4104
      sel.removeAllRanges();
15152 obado 4105
      sel.addRange(range);
14283 obado 4106
      sel.extend(snapshot.focusNode, snapshot.focusOffset);
4107
    }
4108
  }
4109
 
4110
  // Does the actual updating of the line display. Bails out
4111
  // (returning false) when there is nothing to be done and forced is
4112
  // false.
4113
  function updateDisplayIfNeeded(cm, update) {
4114
    var display = cm.display, doc = cm.doc;
4115
 
4116
    if (update.editorIsHidden) {
4117
      resetView(cm);
4118
      return false
4119
    }
4120
 
4121
    // Bail out if the visible area is already rendered and nothing changed.
4122
    if (!update.force &&
4123
        update.visible.from >= display.viewFrom && update.visible.to <= display.viewTo &&
4124
        (display.updateLineNumbers == null || display.updateLineNumbers >= display.viewTo) &&
4125
        display.renderedView == display.view && countDirtyView(cm) == 0)
4126
      { return false }
4127
 
4128
    if (maybeUpdateLineNumberWidth(cm)) {
4129
      resetView(cm);
4130
      update.dims = getDimensions(cm);
4131
    }
4132
 
4133
    // Compute a suitable new viewport (from & to)
4134
    var end = doc.first + doc.size;
4135
    var from = Math.max(update.visible.from - cm.options.viewportMargin, doc.first);
4136
    var to = Math.min(end, update.visible.to + cm.options.viewportMargin);
4137
    if (display.viewFrom < from && from - display.viewFrom < 20) { from = Math.max(doc.first, display.viewFrom); }
4138
    if (display.viewTo > to && display.viewTo - to < 20) { to = Math.min(end, display.viewTo); }
4139
    if (sawCollapsedSpans) {
4140
      from = visualLineNo(cm.doc, from);
4141
      to = visualLineEndNo(cm.doc, to);
4142
    }
4143
 
4144
    var different = from != display.viewFrom || to != display.viewTo ||
4145
      display.lastWrapHeight != update.wrapperHeight || display.lastWrapWidth != update.wrapperWidth;
4146
    adjustView(cm, from, to);
4147
 
4148
    display.viewOffset = heightAtLine(getLine(cm.doc, display.viewFrom));
4149
    // Position the mover div to align with the current scroll position
4150
    cm.display.mover.style.top = display.viewOffset + "px";
4151
 
4152
    var toUpdate = countDirtyView(cm);
4153
    if (!different && toUpdate == 0 && !update.force && display.renderedView == display.view &&
4154
        (display.updateLineNumbers == null || display.updateLineNumbers >= display.viewTo))
4155
      { return false }
4156
 
4157
    // For big changes, we hide the enclosing element during the
4158
    // update, since that speeds up the operations on most browsers.
4159
    var selSnapshot = selectionSnapshot(cm);
4160
    if (toUpdate > 4) { display.lineDiv.style.display = "none"; }
4161
    patchDisplay(cm, display.updateLineNumbers, update.dims);
4162
    if (toUpdate > 4) { display.lineDiv.style.display = ""; }
4163
    display.renderedView = display.view;
4164
    // There might have been a widget with a focused element that got
4165
    // hidden or updated, if so re-focus it.
4166
    restoreSelection(selSnapshot);
4167
 
4168
    // Prevent selection and cursors from interfering with the scroll
4169
    // width and height.
4170
    removeChildren(display.cursorDiv);
4171
    removeChildren(display.selectionDiv);
4172
    display.gutters.style.height = display.sizer.style.minHeight = 0;
4173
 
4174
    if (different) {
4175
      display.lastWrapHeight = update.wrapperHeight;
4176
      display.lastWrapWidth = update.wrapperWidth;
4177
      startWorker(cm, 400);
4178
    }
4179
 
4180
    display.updateLineNumbers = null;
4181
 
4182
    return true
4183
  }
4184
 
4185
  function postUpdateDisplay(cm, update) {
4186
    var viewport = update.viewport;
4187
 
4188
    for (var first = true;; first = false) {
4189
      if (!first || !cm.options.lineWrapping || update.oldDisplayWidth == displayWidth(cm)) {
4190
        // Clip forced viewport to actual scrollable area.
4191
        if (viewport && viewport.top != null)
4192
          { viewport = {top: Math.min(cm.doc.height + paddingVert(cm.display) - displayHeight(cm), viewport.top)}; }
4193
        // Updated line heights might result in the drawn area not
4194
        // actually covering the viewport. Keep looping until it does.
4195
        update.visible = visibleLines(cm.display, cm.doc, viewport);
4196
        if (update.visible.from >= cm.display.viewFrom && update.visible.to <= cm.display.viewTo)
4197
          { break }
15152 obado 4198
      } else if (first) {
4199
        update.visible = visibleLines(cm.display, cm.doc, viewport);
14283 obado 4200
      }
4201
      if (!updateDisplayIfNeeded(cm, update)) { break }
4202
      updateHeightsInViewport(cm);
4203
      var barMeasure = measureForScrollbars(cm);
4204
      updateSelection(cm);
4205
      updateScrollbars(cm, barMeasure);
4206
      setDocumentHeight(cm, barMeasure);
4207
      update.force = false;
4208
    }
4209
 
4210
    update.signal(cm, "update", cm);
4211
    if (cm.display.viewFrom != cm.display.reportedViewFrom || cm.display.viewTo != cm.display.reportedViewTo) {
4212
      update.signal(cm, "viewportChange", cm, cm.display.viewFrom, cm.display.viewTo);
4213
      cm.display.reportedViewFrom = cm.display.viewFrom; cm.display.reportedViewTo = cm.display.viewTo;
4214
    }
4215
  }
4216
 
4217
  function updateDisplaySimple(cm, viewport) {
4218
    var update = new DisplayUpdate(cm, viewport);
4219
    if (updateDisplayIfNeeded(cm, update)) {
4220
      updateHeightsInViewport(cm);
4221
      postUpdateDisplay(cm, update);
4222
      var barMeasure = measureForScrollbars(cm);
4223
      updateSelection(cm);
4224
      updateScrollbars(cm, barMeasure);
4225
      setDocumentHeight(cm, barMeasure);
4226
      update.finish();
4227
    }
4228
  }
4229
 
4230
  // Sync the actual display DOM structure with display.view, removing
4231
  // nodes for lines that are no longer in view, and creating the ones
4232
  // that are not there yet, and updating the ones that are out of
4233
  // date.
4234
  function patchDisplay(cm, updateNumbersFrom, dims) {
4235
    var display = cm.display, lineNumbers = cm.options.lineNumbers;
4236
    var container = display.lineDiv, cur = container.firstChild;
4237
 
4238
    function rm(node) {
4239
      var next = node.nextSibling;
4240
      // Works around a throw-scroll bug in OS X Webkit
4241
      if (webkit && mac && cm.display.currentWheelTarget == node)
4242
        { node.style.display = "none"; }
4243
      else
4244
        { node.parentNode.removeChild(node); }
4245
      return next
4246
    }
4247
 
4248
    var view = display.view, lineN = display.viewFrom;
4249
    // Loop over the elements in the view, syncing cur (the DOM nodes
4250
    // in display.lineDiv) with the view as we go.
4251
    for (var i = 0; i < view.length; i++) {
4252
      var lineView = view[i];
4253
      if (lineView.hidden) ; else if (!lineView.node || lineView.node.parentNode != container) { // Not drawn yet
4254
        var node = buildLineElement(cm, lineView, lineN, dims);
4255
        container.insertBefore(node, cur);
4256
      } else { // Already drawn
4257
        while (cur != lineView.node) { cur = rm(cur); }
4258
        var updateNumber = lineNumbers && updateNumbersFrom != null &&
4259
          updateNumbersFrom <= lineN && lineView.lineNumber;
4260
        if (lineView.changes) {
4261
          if (indexOf(lineView.changes, "gutter") > -1) { updateNumber = false; }
4262
          updateLineForChanges(cm, lineView, lineN, dims);
4263
        }
4264
        if (updateNumber) {
4265
          removeChildren(lineView.lineNumber);
4266
          lineView.lineNumber.appendChild(document.createTextNode(lineNumberFor(cm.options, lineN)));
4267
        }
4268
        cur = lineView.node.nextSibling;
4269
      }
4270
      lineN += lineView.size;
4271
    }
4272
    while (cur) { cur = rm(cur); }
4273
  }
4274
 
4275
  function updateGutterSpace(display) {
4276
    var width = display.gutters.offsetWidth;
4277
    display.sizer.style.marginLeft = width + "px";
16493 obado 4278
    // Send an event to consumers responding to changes in gutter width.
4279
    signalLater(display, "gutterChanged", display);
14283 obado 4280
  }
4281
 
4282
  function setDocumentHeight(cm, measure) {
4283
    cm.display.sizer.style.minHeight = measure.docHeight + "px";
4284
    cm.display.heightForcer.style.top = measure.docHeight + "px";
4285
    cm.display.gutters.style.height = (measure.docHeight + cm.display.barHeight + scrollGap(cm)) + "px";
4286
  }
4287
 
4288
  // Re-align line numbers and gutter marks to compensate for
4289
  // horizontal scrolling.
4290
  function alignHorizontally(cm) {
4291
    var display = cm.display, view = display.view;
4292
    if (!display.alignWidgets && (!display.gutters.firstChild || !cm.options.fixedGutter)) { return }
4293
    var comp = compensateForHScroll(display) - display.scroller.scrollLeft + cm.doc.scrollLeft;
4294
    var gutterW = display.gutters.offsetWidth, left = comp + "px";
4295
    for (var i = 0; i < view.length; i++) { if (!view[i].hidden) {
4296
      if (cm.options.fixedGutter) {
4297
        if (view[i].gutter)
4298
          { view[i].gutter.style.left = left; }
4299
        if (view[i].gutterBackground)
4300
          { view[i].gutterBackground.style.left = left; }
4301
      }
4302
      var align = view[i].alignable;
4303
      if (align) { for (var j = 0; j < align.length; j++)
4304
        { align[j].style.left = left; } }
4305
    } }
4306
    if (cm.options.fixedGutter)
4307
      { display.gutters.style.left = (comp + gutterW) + "px"; }
4308
  }
4309
 
4310
  // Used to ensure that the line number gutter is still the right
4311
  // size for the current document size. Returns true when an update
4312
  // is needed.
4313
  function maybeUpdateLineNumberWidth(cm) {
4314
    if (!cm.options.lineNumbers) { return false }
4315
    var doc = cm.doc, last = lineNumberFor(cm.options, doc.first + doc.size - 1), display = cm.display;
4316
    if (last.length != display.lineNumChars) {
4317
      var test = display.measure.appendChild(elt("div", [elt("div", last)],
4318
                                                 "CodeMirror-linenumber CodeMirror-gutter-elt"));
4319
      var innerW = test.firstChild.offsetWidth, padding = test.offsetWidth - innerW;
4320
      display.lineGutter.style.width = "";
4321
      display.lineNumInnerWidth = Math.max(innerW, display.lineGutter.offsetWidth - padding) + 1;
4322
      display.lineNumWidth = display.lineNumInnerWidth + padding;
4323
      display.lineNumChars = display.lineNumInnerWidth ? last.length : -1;
4324
      display.lineGutter.style.width = display.lineNumWidth + "px";
4325
      updateGutterSpace(cm.display);
4326
      return true
4327
    }
4328
    return false
4329
  }
4330
 
4331
  function getGutters(gutters, lineNumbers) {
4332
    var result = [], sawLineNumbers = false;
4333
    for (var i = 0; i < gutters.length; i++) {
4334
      var name = gutters[i], style = null;
4335
      if (typeof name != "string") { style = name.style; name = name.className; }
4336
      if (name == "CodeMirror-linenumbers") {
4337
        if (!lineNumbers) { continue }
4338
        else { sawLineNumbers = true; }
4339
      }
4340
      result.push({className: name, style: style});
4341
    }
4342
    if (lineNumbers && !sawLineNumbers) { result.push({className: "CodeMirror-linenumbers", style: null}); }
4343
    return result
4344
  }
4345
 
4346
  // Rebuild the gutter elements, ensure the margin to the left of the
4347
  // code matches their width.
4348
  function renderGutters(display) {
4349
    var gutters = display.gutters, specs = display.gutterSpecs;
4350
    removeChildren(gutters);
4351
    display.lineGutter = null;
4352
    for (var i = 0; i < specs.length; ++i) {
4353
      var ref = specs[i];
4354
      var className = ref.className;
4355
      var style = ref.style;
4356
      var gElt = gutters.appendChild(elt("div", null, "CodeMirror-gutter " + className));
4357
      if (style) { gElt.style.cssText = style; }
4358
      if (className == "CodeMirror-linenumbers") {
4359
        display.lineGutter = gElt;
4360
        gElt.style.width = (display.lineNumWidth || 1) + "px";
4361
      }
4362
    }
4363
    gutters.style.display = specs.length ? "" : "none";
4364
    updateGutterSpace(display);
4365
  }
4366
 
4367
  function updateGutters(cm) {
4368
    renderGutters(cm.display);
4369
    regChange(cm);
4370
    alignHorizontally(cm);
4371
  }
4372
 
4373
  // The display handles the DOM integration, both for input reading
4374
  // and content drawing. It holds references to DOM nodes and
4375
  // display-related state.
4376
 
4377
  function Display(place, doc, input, options) {
4378
    var d = this;
4379
    this.input = input;
4380
 
4381
    // Covers bottom-right square when both scrollbars are present.
4382
    d.scrollbarFiller = elt("div", null, "CodeMirror-scrollbar-filler");
4383
    d.scrollbarFiller.setAttribute("cm-not-content", "true");
4384
    // Covers bottom of gutter when coverGutterNextToScrollbar is on
4385
    // and h scrollbar is present.
4386
    d.gutterFiller = elt("div", null, "CodeMirror-gutter-filler");
4387
    d.gutterFiller.setAttribute("cm-not-content", "true");
4388
    // Will contain the actual code, positioned to cover the viewport.
4389
    d.lineDiv = eltP("div", null, "CodeMirror-code");
4390
    // Elements are added to these to represent selection and cursors.
4391
    d.selectionDiv = elt("div", null, null, "position: relative; z-index: 1");
4392
    d.cursorDiv = elt("div", null, "CodeMirror-cursors");
4393
    // A visibility: hidden element used to find the size of things.
4394
    d.measure = elt("div", null, "CodeMirror-measure");
4395
    // When lines outside of the viewport are measured, they are drawn in this.
4396
    d.lineMeasure = elt("div", null, "CodeMirror-measure");
4397
    // Wraps everything that needs to exist inside the vertically-padded coordinate system
4398
    d.lineSpace = eltP("div", [d.measure, d.lineMeasure, d.selectionDiv, d.cursorDiv, d.lineDiv],
4399
                      null, "position: relative; outline: none");
4400
    var lines = eltP("div", [d.lineSpace], "CodeMirror-lines");
4401
    // Moved around its parent to cover visible view.
4402
    d.mover = elt("div", [lines], null, "position: relative");
4403
    // Set to the height of the document, allowing scrolling.
4404
    d.sizer = elt("div", [d.mover], "CodeMirror-sizer");
4405
    d.sizerWidth = null;
4406
    // Behavior of elts with overflow: auto and padding is
4407
    // inconsistent across browsers. This is used to ensure the
4408
    // scrollable area is big enough.
4409
    d.heightForcer = elt("div", null, null, "position: absolute; height: " + scrollerGap + "px; width: 1px;");
4410
    // Will contain the gutters, if any.
4411
    d.gutters = elt("div", null, "CodeMirror-gutters");
4412
    d.lineGutter = null;
4413
    // Actual scrollable element.
4414
    d.scroller = elt("div", [d.sizer, d.heightForcer, d.gutters], "CodeMirror-scroll");
4415
    d.scroller.setAttribute("tabIndex", "-1");
4416
    // The element in which the editor lives.
4417
    d.wrapper = elt("div", [d.scrollbarFiller, d.gutterFiller, d.scroller], "CodeMirror");
17702 obado 4418
    // See #6982. FIXME remove when this has been fixed for a while in Chrome
4419
    if (chrome && chrome_version >= 105) { d.wrapper.style.clipPath = "inset(0px)"; }
14283 obado 4420
 
16493 obado 4421
    // This attribute is respected by automatic translation systems such as Google Translate,
4422
    // and may also be respected by tools used by human translators.
4423
    d.wrapper.setAttribute('translate', 'no');
4424
 
14283 obado 4425
    // Work around IE7 z-index bug (not perfect, hence IE7 not really being supported)
4426
    if (ie && ie_version < 8) { d.gutters.style.zIndex = -1; d.scroller.style.paddingRight = 0; }
4427
    if (!webkit && !(gecko && mobile)) { d.scroller.draggable = true; }
4428
 
4429
    if (place) {
4430
      if (place.appendChild) { place.appendChild(d.wrapper); }
4431
      else { place(d.wrapper); }
4432
    }
4433
 
4434
    // Current rendered range (may be bigger than the view window).
4435
    d.viewFrom = d.viewTo = doc.first;
4436
    d.reportedViewFrom = d.reportedViewTo = doc.first;
4437
    // Information about the rendered lines.
4438
    d.view = [];
4439
    d.renderedView = null;
4440
    // Holds info about a single rendered line when it was rendered
4441
    // for measurement, while not in view.
4442
    d.externalMeasured = null;
4443
    // Empty space (in pixels) above the view
4444
    d.viewOffset = 0;
4445
    d.lastWrapHeight = d.lastWrapWidth = 0;
4446
    d.updateLineNumbers = null;
4447
 
4448
    d.nativeBarWidth = d.barHeight = d.barWidth = 0;
4449
    d.scrollbarsClipped = false;
4450
 
4451
    // Used to only resize the line number gutter when necessary (when
4452
    // the amount of lines crosses a boundary that makes its width change)
4453
    d.lineNumWidth = d.lineNumInnerWidth = d.lineNumChars = null;
4454
    // Set to true when a non-horizontal-scrolling line widget is
4455
    // added. As an optimization, line widget aligning is skipped when
4456
    // this is false.
4457
    d.alignWidgets = false;
4458
 
4459
    d.cachedCharWidth = d.cachedTextHeight = d.cachedPaddingH = null;
4460
 
4461
    // Tracks the maximum line length so that the horizontal scrollbar
4462
    // can be kept static when scrolling.
4463
    d.maxLine = null;
4464
    d.maxLineLength = 0;
4465
    d.maxLineChanged = false;
4466
 
4467
    // Used for measuring wheel scrolling granularity
4468
    d.wheelDX = d.wheelDY = d.wheelStartX = d.wheelStartY = null;
4469
 
4470
    // True when shift is held down.
4471
    d.shift = false;
4472
 
4473
    // Used to track whether anything happened since the context menu
4474
    // was opened.
4475
    d.selForContextMenu = null;
4476
 
4477
    d.activeTouch = null;
4478
 
4479
    d.gutterSpecs = getGutters(options.gutters, options.lineNumbers);
4480
    renderGutters(d);
4481
 
4482
    input.init(d);
4483
  }
4484
 
4485
  // Since the delta values reported on mouse wheel events are
4486
  // unstandardized between browsers and even browser versions, and
4487
  // generally horribly unpredictable, this code starts by measuring
4488
  // the scroll effect that the first few mouse wheel events have,
4489
  // and, from that, detects the way it can convert deltas to pixel
4490
  // offsets afterwards.
4491
  //
4492
  // The reason we want to know the amount a wheel event will scroll
4493
  // is that it gives us a chance to update the display before the
4494
  // actual scrolling happens, reducing flickering.
4495
 
4496
  var wheelSamples = 0, wheelPixelsPerUnit = null;
4497
  // Fill in a browser-detected starting value on browsers where we
4498
  // know one. These don't have to be accurate -- the result of them
4499
  // being wrong would just be a slight flicker on the first wheel
4500
  // scroll (if it is large enough).
4501
  if (ie) { wheelPixelsPerUnit = -.53; }
4502
  else if (gecko) { wheelPixelsPerUnit = 15; }
4503
  else if (chrome) { wheelPixelsPerUnit = -.7; }
4504
  else if (safari) { wheelPixelsPerUnit = -1/3; }
4505
 
4506
  function wheelEventDelta(e) {
4507
    var dx = e.wheelDeltaX, dy = e.wheelDeltaY;
4508
    if (dx == null && e.detail && e.axis == e.HORIZONTAL_AXIS) { dx = e.detail; }
4509
    if (dy == null && e.detail && e.axis == e.VERTICAL_AXIS) { dy = e.detail; }
4510
    else if (dy == null) { dy = e.wheelDelta; }
4511
    return {x: dx, y: dy}
4512
  }
4513
  function wheelEventPixels(e) {
4514
    var delta = wheelEventDelta(e);
4515
    delta.x *= wheelPixelsPerUnit;
4516
    delta.y *= wheelPixelsPerUnit;
4517
    return delta
4518
  }
4519
 
4520
  function onScrollWheel(cm, e) {
16859 obado 4521
    // On Chrome 102, viewport updates somehow stop wheel-based
4522
    // scrolling. Turning off pointer events during the scroll seems
4523
    // to avoid the issue.
17702 obado 4524
    if (chrome && chrome_version == 102) {
16859 obado 4525
      if (cm.display.chromeScrollHack == null) { cm.display.sizer.style.pointerEvents = "none"; }
4526
      else { clearTimeout(cm.display.chromeScrollHack); }
4527
      cm.display.chromeScrollHack = setTimeout(function () {
4528
        cm.display.chromeScrollHack = null;
4529
        cm.display.sizer.style.pointerEvents = "";
4530
      }, 100);
4531
    }
14283 obado 4532
    var delta = wheelEventDelta(e), dx = delta.x, dy = delta.y;
16493 obado 4533
    var pixelsPerUnit = wheelPixelsPerUnit;
4534
    if (e.deltaMode === 0) {
4535
      dx = e.deltaX;
4536
      dy = e.deltaY;
4537
      pixelsPerUnit = 1;
4538
    }
14283 obado 4539
 
4540
    var display = cm.display, scroll = display.scroller;
4541
    // Quit if there's nothing to scroll here
4542
    var canScrollX = scroll.scrollWidth > scroll.clientWidth;
4543
    var canScrollY = scroll.scrollHeight > scroll.clientHeight;
4544
    if (!(dx && canScrollX || dy && canScrollY)) { return }
4545
 
4546
    // Webkit browsers on OS X abort momentum scrolls when the target
4547
    // of the scroll event is removed from the scrollable element.
4548
    // This hack (see related code in patchDisplay) makes sure the
4549
    // element is kept around.
4550
    if (dy && mac && webkit) {
4551
      outer: for (var cur = e.target, view = display.view; cur != scroll; cur = cur.parentNode) {
4552
        for (var i = 0; i < view.length; i++) {
4553
          if (view[i].node == cur) {
4554
            cm.display.currentWheelTarget = cur;
4555
            break outer
4556
          }
4557
        }
4558
      }
4559
    }
4560
 
4561
    // On some browsers, horizontal scrolling will cause redraws to
4562
    // happen before the gutter has been realigned, causing it to
4563
    // wriggle around in a most unseemly way. When we have an
4564
    // estimated pixels/delta value, we just handle horizontal
4565
    // scrolling entirely here. It'll be slightly off from native, but
4566
    // better than glitching out.
16493 obado 4567
    if (dx && !gecko && !presto && pixelsPerUnit != null) {
14283 obado 4568
      if (dy && canScrollY)
16493 obado 4569
        { updateScrollTop(cm, Math.max(0, scroll.scrollTop + dy * pixelsPerUnit)); }
4570
      setScrollLeft(cm, Math.max(0, scroll.scrollLeft + dx * pixelsPerUnit));
14283 obado 4571
      // Only prevent default scrolling if vertical scrolling is
4572
      // actually possible. Otherwise, it causes vertical scroll
4573
      // jitter on OSX trackpads when deltaX is small and deltaY
4574
      // is large (issue #3579)
4575
      if (!dy || (dy && canScrollY))
4576
        { e_preventDefault(e); }
4577
      display.wheelStartX = null; // Abort measurement, if in progress
4578
      return
4579
    }
4580
 
4581
    // 'Project' the visible viewport to cover the area that is being
4582
    // scrolled into view (if we know enough to estimate it).
16493 obado 4583
    if (dy && pixelsPerUnit != null) {
4584
      var pixels = dy * pixelsPerUnit;
14283 obado 4585
      var top = cm.doc.scrollTop, bot = top + display.wrapper.clientHeight;
4586
      if (pixels < 0) { top = Math.max(0, top + pixels - 50); }
4587
      else { bot = Math.min(cm.doc.height, bot + pixels + 50); }
4588
      updateDisplaySimple(cm, {top: top, bottom: bot});
4589
    }
4590
 
16493 obado 4591
    if (wheelSamples < 20 && e.deltaMode !== 0) {
14283 obado 4592
      if (display.wheelStartX == null) {
4593
        display.wheelStartX = scroll.scrollLeft; display.wheelStartY = scroll.scrollTop;
4594
        display.wheelDX = dx; display.wheelDY = dy;
4595
        setTimeout(function () {
4596
          if (display.wheelStartX == null) { return }
4597
          var movedX = scroll.scrollLeft - display.wheelStartX;
4598
          var movedY = scroll.scrollTop - display.wheelStartY;
4599
          var sample = (movedY && display.wheelDY && movedY / display.wheelDY) ||
4600
            (movedX && display.wheelDX && movedX / display.wheelDX);
4601
          display.wheelStartX = display.wheelStartY = null;
4602
          if (!sample) { return }
4603
          wheelPixelsPerUnit = (wheelPixelsPerUnit * wheelSamples + sample) / (wheelSamples + 1);
4604
          ++wheelSamples;
4605
        }, 200);
4606
      } else {
4607
        display.wheelDX += dx; display.wheelDY += dy;
4608
      }
4609
    }
4610
  }
4611
 
4612
  // Selection objects are immutable. A new one is created every time
4613
  // the selection changes. A selection is one or more non-overlapping
4614
  // (and non-touching) ranges, sorted, and an integer that indicates
4615
  // which one is the primary selection (the one that's scrolled into
4616
  // view, that getCursor returns, etc).
4617
  var Selection = function(ranges, primIndex) {
4618
    this.ranges = ranges;
4619
    this.primIndex = primIndex;
4620
  };
4621
 
4622
  Selection.prototype.primary = function () { return this.ranges[this.primIndex] };
4623
 
4624
  Selection.prototype.equals = function (other) {
4625
    if (other == this) { return true }
4626
    if (other.primIndex != this.primIndex || other.ranges.length != this.ranges.length) { return false }
4627
    for (var i = 0; i < this.ranges.length; i++) {
15152 obado 4628
      var here = this.ranges[i], there = other.ranges[i];
14283 obado 4629
      if (!equalCursorPos(here.anchor, there.anchor) || !equalCursorPos(here.head, there.head)) { return false }
4630
    }
4631
    return true
4632
  };
4633
 
4634
  Selection.prototype.deepCopy = function () {
4635
    var out = [];
4636
    for (var i = 0; i < this.ranges.length; i++)
15152 obado 4637
      { out[i] = new Range(copyPos(this.ranges[i].anchor), copyPos(this.ranges[i].head)); }
14283 obado 4638
    return new Selection(out, this.primIndex)
4639
  };
4640
 
4641
  Selection.prototype.somethingSelected = function () {
4642
    for (var i = 0; i < this.ranges.length; i++)
15152 obado 4643
      { if (!this.ranges[i].empty()) { return true } }
14283 obado 4644
    return false
4645
  };
4646
 
4647
  Selection.prototype.contains = function (pos, end) {
4648
    if (!end) { end = pos; }
4649
    for (var i = 0; i < this.ranges.length; i++) {
15152 obado 4650
      var range = this.ranges[i];
14283 obado 4651
      if (cmp(end, range.from()) >= 0 && cmp(pos, range.to()) <= 0)
4652
        { return i }
4653
    }
4654
    return -1
4655
  };
4656
 
4657
  var Range = function(anchor, head) {
4658
    this.anchor = anchor; this.head = head;
4659
  };
4660
 
4661
  Range.prototype.from = function () { return minPos(this.anchor, this.head) };
4662
  Range.prototype.to = function () { return maxPos(this.anchor, this.head) };
4663
  Range.prototype.empty = function () { return this.head.line == this.anchor.line && this.head.ch == this.anchor.ch };
4664
 
4665
  // Take an unsorted, potentially overlapping set of ranges, and
4666
  // build a selection out of it. 'Consumes' ranges array (modifying
4667
  // it).
4668
  function normalizeSelection(cm, ranges, primIndex) {
4669
    var mayTouch = cm && cm.options.selectionsMayTouch;
4670
    var prim = ranges[primIndex];
4671
    ranges.sort(function (a, b) { return cmp(a.from(), b.from()); });
4672
    primIndex = indexOf(ranges, prim);
4673
    for (var i = 1; i < ranges.length; i++) {
4674
      var cur = ranges[i], prev = ranges[i - 1];
4675
      var diff = cmp(prev.to(), cur.from());
4676
      if (mayTouch && !cur.empty() ? diff > 0 : diff >= 0) {
4677
        var from = minPos(prev.from(), cur.from()), to = maxPos(prev.to(), cur.to());
4678
        var inv = prev.empty() ? cur.from() == cur.head : prev.from() == prev.head;
4679
        if (i <= primIndex) { --primIndex; }
4680
        ranges.splice(--i, 2, new Range(inv ? to : from, inv ? from : to));
4681
      }
4682
    }
4683
    return new Selection(ranges, primIndex)
4684
  }
4685
 
4686
  function simpleSelection(anchor, head) {
4687
    return new Selection([new Range(anchor, head || anchor)], 0)
4688
  }
4689
 
4690
  // Compute the position of the end of a change (its 'to' property
4691
  // refers to the pre-change end).
4692
  function changeEnd(change) {
4693
    if (!change.text) { return change.to }
4694
    return Pos(change.from.line + change.text.length - 1,
4695
               lst(change.text).length + (change.text.length == 1 ? change.from.ch : 0))
4696
  }
4697
 
4698
  // Adjust a position to refer to the post-change position of the
4699
  // same text, or the end of the change if the change covers it.
4700
  function adjustForChange(pos, change) {
4701
    if (cmp(pos, change.from) < 0) { return pos }
4702
    if (cmp(pos, change.to) <= 0) { return changeEnd(change) }
4703
 
4704
    var line = pos.line + change.text.length - (change.to.line - change.from.line) - 1, ch = pos.ch;
4705
    if (pos.line == change.to.line) { ch += changeEnd(change).ch - change.to.ch; }
4706
    return Pos(line, ch)
4707
  }
4708
 
4709
  function computeSelAfterChange(doc, change) {
4710
    var out = [];
4711
    for (var i = 0; i < doc.sel.ranges.length; i++) {
4712
      var range = doc.sel.ranges[i];
4713
      out.push(new Range(adjustForChange(range.anchor, change),
4714
                         adjustForChange(range.head, change)));
4715
    }
4716
    return normalizeSelection(doc.cm, out, doc.sel.primIndex)
4717
  }
4718
 
4719
  function offsetPos(pos, old, nw) {
4720
    if (pos.line == old.line)
4721
      { return Pos(nw.line, pos.ch - old.ch + nw.ch) }
4722
    else
4723
      { return Pos(nw.line + (pos.line - old.line), pos.ch) }
4724
  }
4725
 
4726
  // Used by replaceSelections to allow moving the selection to the
4727
  // start or around the replaced test. Hint may be "start" or "around".
4728
  function computeReplacedSel(doc, changes, hint) {
4729
    var out = [];
4730
    var oldPrev = Pos(doc.first, 0), newPrev = oldPrev;
4731
    for (var i = 0; i < changes.length; i++) {
4732
      var change = changes[i];
4733
      var from = offsetPos(change.from, oldPrev, newPrev);
4734
      var to = offsetPos(changeEnd(change), oldPrev, newPrev);
4735
      oldPrev = change.to;
4736
      newPrev = to;
4737
      if (hint == "around") {
4738
        var range = doc.sel.ranges[i], inv = cmp(range.head, range.anchor) < 0;
4739
        out[i] = new Range(inv ? to : from, inv ? from : to);
4740
      } else {
4741
        out[i] = new Range(from, from);
4742
      }
4743
    }
4744
    return new Selection(out, doc.sel.primIndex)
4745
  }
4746
 
4747
  // Used to get the editor into a consistent state again when options change.
4748
 
4749
  function loadMode(cm) {
4750
    cm.doc.mode = getMode(cm.options, cm.doc.modeOption);
4751
    resetModeState(cm);
4752
  }
4753
 
4754
  function resetModeState(cm) {
4755
    cm.doc.iter(function (line) {
4756
      if (line.stateAfter) { line.stateAfter = null; }
4757
      if (line.styles) { line.styles = null; }
4758
    });
4759
    cm.doc.modeFrontier = cm.doc.highlightFrontier = cm.doc.first;
4760
    startWorker(cm, 100);
4761
    cm.state.modeGen++;
4762
    if (cm.curOp) { regChange(cm); }
4763
  }
4764
 
4765
  // DOCUMENT DATA STRUCTURE
4766
 
4767
  // By default, updates that start and end at the beginning of a line
4768
  // are treated specially, in order to make the association of line
4769
  // widgets and marker elements with the text behave more intuitive.
4770
  function isWholeLineUpdate(doc, change) {
4771
    return change.from.ch == 0 && change.to.ch == 0 && lst(change.text) == "" &&
4772
      (!doc.cm || doc.cm.options.wholeLineUpdateBefore)
4773
  }
4774
 
4775
  // Perform a change on the document data structure.
15152 obado 4776
  function updateDoc(doc, change, markedSpans, estimateHeight) {
14283 obado 4777
    function spansFor(n) {return markedSpans ? markedSpans[n] : null}
4778
    function update(line, text, spans) {
15152 obado 4779
      updateLine(line, text, spans, estimateHeight);
14283 obado 4780
      signalLater(line, "change", line, change);
4781
    }
4782
    function linesFor(start, end) {
4783
      var result = [];
4784
      for (var i = start; i < end; ++i)
15152 obado 4785
        { result.push(new Line(text[i], spansFor(i), estimateHeight)); }
14283 obado 4786
      return result
4787
    }
4788
 
4789
    var from = change.from, to = change.to, text = change.text;
4790
    var firstLine = getLine(doc, from.line), lastLine = getLine(doc, to.line);
4791
    var lastText = lst(text), lastSpans = spansFor(text.length - 1), nlines = to.line - from.line;
4792
 
4793
    // Adjust the line structure
4794
    if (change.full) {
4795
      doc.insert(0, linesFor(0, text.length));
4796
      doc.remove(text.length, doc.size - text.length);
4797
    } else if (isWholeLineUpdate(doc, change)) {
4798
      // This is a whole-line replace. Treated specially to make
4799
      // sure line objects move the way they are supposed to.
4800
      var added = linesFor(0, text.length - 1);
4801
      update(lastLine, lastLine.text, lastSpans);
4802
      if (nlines) { doc.remove(from.line, nlines); }
4803
      if (added.length) { doc.insert(from.line, added); }
4804
    } else if (firstLine == lastLine) {
4805
      if (text.length == 1) {
4806
        update(firstLine, firstLine.text.slice(0, from.ch) + lastText + firstLine.text.slice(to.ch), lastSpans);
4807
      } else {
4808
        var added$1 = linesFor(1, text.length - 1);
15152 obado 4809
        added$1.push(new Line(lastText + firstLine.text.slice(to.ch), lastSpans, estimateHeight));
14283 obado 4810
        update(firstLine, firstLine.text.slice(0, from.ch) + text[0], spansFor(0));
4811
        doc.insert(from.line + 1, added$1);
4812
      }
4813
    } else if (text.length == 1) {
4814
      update(firstLine, firstLine.text.slice(0, from.ch) + text[0] + lastLine.text.slice(to.ch), spansFor(0));
4815
      doc.remove(from.line + 1, nlines);
4816
    } else {
4817
      update(firstLine, firstLine.text.slice(0, from.ch) + text[0], spansFor(0));
4818
      update(lastLine, lastText + lastLine.text.slice(to.ch), lastSpans);
4819
      var added$2 = linesFor(1, text.length - 1);
4820
      if (nlines > 1) { doc.remove(from.line + 1, nlines - 1); }
4821
      doc.insert(from.line + 1, added$2);
4822
    }
4823
 
4824
    signalLater(doc, "change", doc, change);
4825
  }
4826
 
4827
  // Call f for all linked documents.
4828
  function linkedDocs(doc, f, sharedHistOnly) {
4829
    function propagate(doc, skip, sharedHist) {
4830
      if (doc.linked) { for (var i = 0; i < doc.linked.length; ++i) {
4831
        var rel = doc.linked[i];
4832
        if (rel.doc == skip) { continue }
4833
        var shared = sharedHist && rel.sharedHist;
4834
        if (sharedHistOnly && !shared) { continue }
4835
        f(rel.doc, shared);
4836
        propagate(rel.doc, doc, shared);
4837
      } }
4838
    }
4839
    propagate(doc, null, true);
4840
  }
4841
 
4842
  // Attach a document to an editor.
4843
  function attachDoc(cm, doc) {
4844
    if (doc.cm) { throw new Error("This document is already in use.") }
4845
    cm.doc = doc;
4846
    doc.cm = cm;
4847
    estimateLineHeights(cm);
4848
    loadMode(cm);
4849
    setDirectionClass(cm);
16493 obado 4850
    cm.options.direction = doc.direction;
14283 obado 4851
    if (!cm.options.lineWrapping) { findMaxLine(cm); }
4852
    cm.options.mode = doc.modeOption;
4853
    regChange(cm);
4854
  }
4855
 
4856
  function setDirectionClass(cm) {
4857
  (cm.doc.direction == "rtl" ? addClass : rmClass)(cm.display.lineDiv, "CodeMirror-rtl");
4858
  }
4859
 
4860
  function directionChanged(cm) {
4861
    runInOp(cm, function () {
4862
      setDirectionClass(cm);
4863
      regChange(cm);
4864
    });
4865
  }
4866
 
16493 obado 4867
  function History(prev) {
14283 obado 4868
    // Arrays of change events and selections. Doing something adds an
4869
    // event to done and clears undo. Undoing moves events from done
4870
    // to undone, redoing moves them in the other direction.
4871
    this.done = []; this.undone = [];
16493 obado 4872
    this.undoDepth = prev ? prev.undoDepth : Infinity;
14283 obado 4873
    // Used to track when changes can be merged into a single undo
4874
    // event
4875
    this.lastModTime = this.lastSelTime = 0;
4876
    this.lastOp = this.lastSelOp = null;
4877
    this.lastOrigin = this.lastSelOrigin = null;
4878
    // Used by the isClean() method
16493 obado 4879
    this.generation = this.maxGeneration = prev ? prev.maxGeneration : 1;
14283 obado 4880
  }
4881
 
4882
  // Create a history change event from an updateDoc-style change
4883
  // object.
4884
  function historyChangeFromChange(doc, change) {
4885
    var histChange = {from: copyPos(change.from), to: changeEnd(change), text: getBetween(doc, change.from, change.to)};
4886
    attachLocalSpans(doc, histChange, change.from.line, change.to.line + 1);
4887
    linkedDocs(doc, function (doc) { return attachLocalSpans(doc, histChange, change.from.line, change.to.line + 1); }, true);
4888
    return histChange
4889
  }
4890
 
4891
  // Pop all selection events off the end of a history array. Stop at
4892
  // a change event.
4893
  function clearSelectionEvents(array) {
4894
    while (array.length) {
4895
      var last = lst(array);
4896
      if (last.ranges) { array.pop(); }
4897
      else { break }
4898
    }
4899
  }
4900
 
4901
  // Find the top change event in the history. Pop off selection
4902
  // events that are in the way.
4903
  function lastChangeEvent(hist, force) {
4904
    if (force) {
4905
      clearSelectionEvents(hist.done);
4906
      return lst(hist.done)
4907
    } else if (hist.done.length && !lst(hist.done).ranges) {
4908
      return lst(hist.done)
4909
    } else if (hist.done.length > 1 && !hist.done[hist.done.length - 2].ranges) {
4910
      hist.done.pop();
4911
      return lst(hist.done)
4912
    }
4913
  }
4914
 
4915
  // Register a change in the history. Merges changes that are within
4916
  // a single operation, or are close together with an origin that
4917
  // allows merging (starting with "+") into a single event.
4918
  function addChangeToHistory(doc, change, selAfter, opId) {
4919
    var hist = doc.history;
4920
    hist.undone.length = 0;
4921
    var time = +new Date, cur;
4922
    var last;
4923
 
4924
    if ((hist.lastOp == opId ||
4925
         hist.lastOrigin == change.origin && change.origin &&
4926
         ((change.origin.charAt(0) == "+" && hist.lastModTime > time - (doc.cm ? doc.cm.options.historyEventDelay : 500)) ||
4927
          change.origin.charAt(0) == "*")) &&
4928
        (cur = lastChangeEvent(hist, hist.lastOp == opId))) {
4929
      // Merge this change into the last event
4930
      last = lst(cur.changes);
4931
      if (cmp(change.from, change.to) == 0 && cmp(change.from, last.to) == 0) {
4932
        // Optimized case for simple insertion -- don't want to add
4933
        // new changesets for every character typed
4934
        last.to = changeEnd(change);
4935
      } else {
4936
        // Add new sub-event
4937
        cur.changes.push(historyChangeFromChange(doc, change));
4938
      }
4939
    } else {
4940
      // Can not be merged, start a new event.
4941
      var before = lst(hist.done);
4942
      if (!before || !before.ranges)
4943
        { pushSelectionToHistory(doc.sel, hist.done); }
4944
      cur = {changes: [historyChangeFromChange(doc, change)],
4945
             generation: hist.generation};
4946
      hist.done.push(cur);
4947
      while (hist.done.length > hist.undoDepth) {
4948
        hist.done.shift();
4949
        if (!hist.done[0].ranges) { hist.done.shift(); }
4950
      }
4951
    }
4952
    hist.done.push(selAfter);
4953
    hist.generation = ++hist.maxGeneration;
4954
    hist.lastModTime = hist.lastSelTime = time;
4955
    hist.lastOp = hist.lastSelOp = opId;
4956
    hist.lastOrigin = hist.lastSelOrigin = change.origin;
4957
 
4958
    if (!last) { signal(doc, "historyAdded"); }
4959
  }
4960
 
4961
  function selectionEventCanBeMerged(doc, origin, prev, sel) {
4962
    var ch = origin.charAt(0);
4963
    return ch == "*" ||
4964
      ch == "+" &&
4965
      prev.ranges.length == sel.ranges.length &&
4966
      prev.somethingSelected() == sel.somethingSelected() &&
4967
      new Date - doc.history.lastSelTime <= (doc.cm ? doc.cm.options.historyEventDelay : 500)
4968
  }
4969
 
4970
  // Called whenever the selection changes, sets the new selection as
4971
  // the pending selection in the history, and pushes the old pending
4972
  // selection into the 'done' array when it was significantly
4973
  // different (in number of selected ranges, emptiness, or time).
4974
  function addSelectionToHistory(doc, sel, opId, options) {
4975
    var hist = doc.history, origin = options && options.origin;
4976
 
4977
    // A new event is started when the previous origin does not match
4978
    // the current, or the origins don't allow matching. Origins
4979
    // starting with * are always merged, those starting with + are
4980
    // merged when similar and close together in time.
4981
    if (opId == hist.lastSelOp ||
4982
        (origin && hist.lastSelOrigin == origin &&
4983
         (hist.lastModTime == hist.lastSelTime && hist.lastOrigin == origin ||
4984
          selectionEventCanBeMerged(doc, origin, lst(hist.done), sel))))
4985
      { hist.done[hist.done.length - 1] = sel; }
4986
    else
4987
      { pushSelectionToHistory(sel, hist.done); }
4988
 
4989
    hist.lastSelTime = +new Date;
4990
    hist.lastSelOrigin = origin;
4991
    hist.lastSelOp = opId;
4992
    if (options && options.clearRedo !== false)
4993
      { clearSelectionEvents(hist.undone); }
4994
  }
4995
 
4996
  function pushSelectionToHistory(sel, dest) {
4997
    var top = lst(dest);
4998
    if (!(top && top.ranges && top.equals(sel)))
4999
      { dest.push(sel); }
5000
  }
5001
 
5002
  // Used to store marked span information in the history.
5003
  function attachLocalSpans(doc, change, from, to) {
5004
    var existing = change["spans_" + doc.id], n = 0;
5005
    doc.iter(Math.max(doc.first, from), Math.min(doc.first + doc.size, to), function (line) {
5006
      if (line.markedSpans)
5007
        { (existing || (existing = change["spans_" + doc.id] = {}))[n] = line.markedSpans; }
5008
      ++n;
5009
    });
5010
  }
5011
 
5012
  // When un/re-doing restores text containing marked spans, those
5013
  // that have been explicitly cleared should not be restored.
5014
  function removeClearedSpans(spans) {
5015
    if (!spans) { return null }
5016
    var out;
5017
    for (var i = 0; i < spans.length; ++i) {
5018
      if (spans[i].marker.explicitlyCleared) { if (!out) { out = spans.slice(0, i); } }
5019
      else if (out) { out.push(spans[i]); }
5020
    }
5021
    return !out ? spans : out.length ? out : null
5022
  }
5023
 
5024
  // Retrieve and filter the old marked spans stored in a change event.
5025
  function getOldSpans(doc, change) {
5026
    var found = change["spans_" + doc.id];
5027
    if (!found) { return null }
5028
    var nw = [];
5029
    for (var i = 0; i < change.text.length; ++i)
5030
      { nw.push(removeClearedSpans(found[i])); }
5031
    return nw
5032
  }
5033
 
5034
  // Used for un/re-doing changes from the history. Combines the
5035
  // result of computing the existing spans with the set of spans that
5036
  // existed in the history (so that deleting around a span and then
5037
  // undoing brings back the span).
5038
  function mergeOldSpans(doc, change) {
5039
    var old = getOldSpans(doc, change);
5040
    var stretched = stretchSpansOverChange(doc, change);
5041
    if (!old) { return stretched }
5042
    if (!stretched) { return old }
5043
 
5044
    for (var i = 0; i < old.length; ++i) {
5045
      var oldCur = old[i], stretchCur = stretched[i];
5046
      if (oldCur && stretchCur) {
5047
        spans: for (var j = 0; j < stretchCur.length; ++j) {
5048
          var span = stretchCur[j];
5049
          for (var k = 0; k < oldCur.length; ++k)
5050
            { if (oldCur[k].marker == span.marker) { continue spans } }
5051
          oldCur.push(span);
5052
        }
5053
      } else if (stretchCur) {
5054
        old[i] = stretchCur;
5055
      }
5056
    }
5057
    return old
5058
  }
5059
 
5060
  // Used both to provide a JSON-safe object in .getHistory, and, when
5061
  // detaching a document, to split the history in two
5062
  function copyHistoryArray(events, newGroup, instantiateSel) {
5063
    var copy = [];
5064
    for (var i = 0; i < events.length; ++i) {
5065
      var event = events[i];
5066
      if (event.ranges) {
5067
        copy.push(instantiateSel ? Selection.prototype.deepCopy.call(event) : event);
5068
        continue
5069
      }
5070
      var changes = event.changes, newChanges = [];
5071
      copy.push({changes: newChanges});
5072
      for (var j = 0; j < changes.length; ++j) {
5073
        var change = changes[j], m = (void 0);
5074
        newChanges.push({from: change.from, to: change.to, text: change.text});
5075
        if (newGroup) { for (var prop in change) { if (m = prop.match(/^spans_(\d+)$/)) {
5076
          if (indexOf(newGroup, Number(m[1])) > -1) {
5077
            lst(newChanges)[prop] = change[prop];
5078
            delete change[prop];
5079
          }
5080
        } } }
5081
      }
5082
    }
5083
    return copy
5084
  }
5085
 
5086
  // The 'scroll' parameter given to many of these indicated whether
5087
  // the new cursor position should be scrolled into view after
5088
  // modifying the selection.
5089
 
5090
  // If shift is held or the extend flag is set, extends a range to
5091
  // include a given position (and optionally a second position).
5092
  // Otherwise, simply returns the range between the given positions.
5093
  // Used for cursor motion and such.
5094
  function extendRange(range, head, other, extend) {
5095
    if (extend) {
5096
      var anchor = range.anchor;
5097
      if (other) {
5098
        var posBefore = cmp(head, anchor) < 0;
5099
        if (posBefore != (cmp(other, anchor) < 0)) {
5100
          anchor = head;
5101
          head = other;
5102
        } else if (posBefore != (cmp(head, other) < 0)) {
5103
          head = other;
5104
        }
5105
      }
5106
      return new Range(anchor, head)
5107
    } else {
5108
      return new Range(other || head, head)
5109
    }
5110
  }
5111
 
5112
  // Extend the primary selection range, discard the rest.
5113
  function extendSelection(doc, head, other, options, extend) {
5114
    if (extend == null) { extend = doc.cm && (doc.cm.display.shift || doc.extend); }
5115
    setSelection(doc, new Selection([extendRange(doc.sel.primary(), head, other, extend)], 0), options);
5116
  }
5117
 
5118
  // Extend all selections (pos is an array of selections with length
5119
  // equal the number of selections)
5120
  function extendSelections(doc, heads, options) {
5121
    var out = [];
5122
    var extend = doc.cm && (doc.cm.display.shift || doc.extend);
5123
    for (var i = 0; i < doc.sel.ranges.length; i++)
5124
      { out[i] = extendRange(doc.sel.ranges[i], heads[i], null, extend); }
5125
    var newSel = normalizeSelection(doc.cm, out, doc.sel.primIndex);
5126
    setSelection(doc, newSel, options);
5127
  }
5128
 
5129
  // Updates a single range in the selection.
5130
  function replaceOneSelection(doc, i, range, options) {
5131
    var ranges = doc.sel.ranges.slice(0);
5132
    ranges[i] = range;
5133
    setSelection(doc, normalizeSelection(doc.cm, ranges, doc.sel.primIndex), options);
5134
  }
5135
 
5136
  // Reset the selection to a single range.
5137
  function setSimpleSelection(doc, anchor, head, options) {
5138
    setSelection(doc, simpleSelection(anchor, head), options);
5139
  }
5140
 
5141
  // Give beforeSelectionChange handlers a change to influence a
5142
  // selection update.
5143
  function filterSelectionChange(doc, sel, options) {
5144
    var obj = {
5145
      ranges: sel.ranges,
5146
      update: function(ranges) {
5147
        this.ranges = [];
5148
        for (var i = 0; i < ranges.length; i++)
15152 obado 5149
          { this.ranges[i] = new Range(clipPos(doc, ranges[i].anchor),
14283 obado 5150
                                     clipPos(doc, ranges[i].head)); }
5151
      },
5152
      origin: options && options.origin
5153
    };
5154
    signal(doc, "beforeSelectionChange", doc, obj);
5155
    if (doc.cm) { signal(doc.cm, "beforeSelectionChange", doc.cm, obj); }
5156
    if (obj.ranges != sel.ranges) { return normalizeSelection(doc.cm, obj.ranges, obj.ranges.length - 1) }
5157
    else { return sel }
5158
  }
5159
 
5160
  function setSelectionReplaceHistory(doc, sel, options) {
5161
    var done = doc.history.done, last = lst(done);
5162
    if (last && last.ranges) {
5163
      done[done.length - 1] = sel;
5164
      setSelectionNoUndo(doc, sel, options);
5165
    } else {
5166
      setSelection(doc, sel, options);
5167
    }
5168
  }
5169
 
5170
  // Set a new selection.
5171
  function setSelection(doc, sel, options) {
5172
    setSelectionNoUndo(doc, sel, options);
5173
    addSelectionToHistory(doc, doc.sel, doc.cm ? doc.cm.curOp.id : NaN, options);
5174
  }
5175
 
5176
  function setSelectionNoUndo(doc, sel, options) {
5177
    if (hasHandler(doc, "beforeSelectionChange") || doc.cm && hasHandler(doc.cm, "beforeSelectionChange"))
5178
      { sel = filterSelectionChange(doc, sel, options); }
5179
 
5180
    var bias = options && options.bias ||
5181
      (cmp(sel.primary().head, doc.sel.primary().head) < 0 ? -1 : 1);
5182
    setSelectionInner(doc, skipAtomicInSelection(doc, sel, bias, true));
5183
 
16493 obado 5184
    if (!(options && options.scroll === false) && doc.cm && doc.cm.getOption("readOnly") != "nocursor")
14283 obado 5185
      { ensureCursorVisible(doc.cm); }
5186
  }
5187
 
5188
  function setSelectionInner(doc, sel) {
5189
    if (sel.equals(doc.sel)) { return }
5190
 
5191
    doc.sel = sel;
5192
 
5193
    if (doc.cm) {
5194
      doc.cm.curOp.updateInput = 1;
5195
      doc.cm.curOp.selectionChanged = true;
5196
      signalCursorActivity(doc.cm);
5197
    }
5198
    signalLater(doc, "cursorActivity", doc);
5199
  }
5200
 
5201
  // Verify that the selection does not partially select any atomic
5202
  // marked ranges.
5203
  function reCheckSelection(doc) {
5204
    setSelectionInner(doc, skipAtomicInSelection(doc, doc.sel, null, false));
5205
  }
5206
 
5207
  // Return a selection that does not partially select any atomic
5208
  // ranges.
5209
  function skipAtomicInSelection(doc, sel, bias, mayClear) {
5210
    var out;
5211
    for (var i = 0; i < sel.ranges.length; i++) {
5212
      var range = sel.ranges[i];
5213
      var old = sel.ranges.length == doc.sel.ranges.length && doc.sel.ranges[i];
5214
      var newAnchor = skipAtomic(doc, range.anchor, old && old.anchor, bias, mayClear);
16859 obado 5215
      var newHead = range.head == range.anchor ? newAnchor : skipAtomic(doc, range.head, old && old.head, bias, mayClear);
14283 obado 5216
      if (out || newAnchor != range.anchor || newHead != range.head) {
5217
        if (!out) { out = sel.ranges.slice(0, i); }
5218
        out[i] = new Range(newAnchor, newHead);
5219
      }
5220
    }
5221
    return out ? normalizeSelection(doc.cm, out, sel.primIndex) : sel
5222
  }
5223
 
5224
  function skipAtomicInner(doc, pos, oldPos, dir, mayClear) {
5225
    var line = getLine(doc, pos.line);
5226
    if (line.markedSpans) { for (var i = 0; i < line.markedSpans.length; ++i) {
5227
      var sp = line.markedSpans[i], m = sp.marker;
5228
 
5229
      // Determine if we should prevent the cursor being placed to the left/right of an atomic marker
5230
      // Historically this was determined using the inclusiveLeft/Right option, but the new way to control it
5231
      // is with selectLeft/Right
5232
      var preventCursorLeft = ("selectLeft" in m) ? !m.selectLeft : m.inclusiveLeft;
5233
      var preventCursorRight = ("selectRight" in m) ? !m.selectRight : m.inclusiveRight;
5234
 
5235
      if ((sp.from == null || (preventCursorLeft ? sp.from <= pos.ch : sp.from < pos.ch)) &&
5236
          (sp.to == null || (preventCursorRight ? sp.to >= pos.ch : sp.to > pos.ch))) {
5237
        if (mayClear) {
5238
          signal(m, "beforeCursorEnter");
5239
          if (m.explicitlyCleared) {
5240
            if (!line.markedSpans) { break }
5241
            else {--i; continue}
5242
          }
5243
        }
5244
        if (!m.atomic) { continue }
5245
 
5246
        if (oldPos) {
5247
          var near = m.find(dir < 0 ? 1 : -1), diff = (void 0);
5248
          if (dir < 0 ? preventCursorRight : preventCursorLeft)
5249
            { near = movePos(doc, near, -dir, near && near.line == pos.line ? line : null); }
5250
          if (near && near.line == pos.line && (diff = cmp(near, oldPos)) && (dir < 0 ? diff < 0 : diff > 0))
5251
            { return skipAtomicInner(doc, near, pos, dir, mayClear) }
5252
        }
5253
 
5254
        var far = m.find(dir < 0 ? -1 : 1);
5255
        if (dir < 0 ? preventCursorLeft : preventCursorRight)
5256
          { far = movePos(doc, far, dir, far.line == pos.line ? line : null); }
5257
        return far ? skipAtomicInner(doc, far, pos, dir, mayClear) : null
5258
      }
5259
    } }
5260
    return pos
5261
  }
5262
 
5263
  // Ensure a given position is not inside an atomic range.
5264
  function skipAtomic(doc, pos, oldPos, bias, mayClear) {
5265
    var dir = bias || 1;
5266
    var found = skipAtomicInner(doc, pos, oldPos, dir, mayClear) ||
5267
        (!mayClear && skipAtomicInner(doc, pos, oldPos, dir, true)) ||
5268
        skipAtomicInner(doc, pos, oldPos, -dir, mayClear) ||
5269
        (!mayClear && skipAtomicInner(doc, pos, oldPos, -dir, true));
5270
    if (!found) {
5271
      doc.cantEdit = true;
5272
      return Pos(doc.first, 0)
5273
    }
5274
    return found
5275
  }
5276
 
5277
  function movePos(doc, pos, dir, line) {
5278
    if (dir < 0 && pos.ch == 0) {
5279
      if (pos.line > doc.first) { return clipPos(doc, Pos(pos.line - 1)) }
5280
      else { return null }
5281
    } else if (dir > 0 && pos.ch == (line || getLine(doc, pos.line)).text.length) {
5282
      if (pos.line < doc.first + doc.size - 1) { return Pos(pos.line + 1, 0) }
5283
      else { return null }
5284
    } else {
5285
      return new Pos(pos.line, pos.ch + dir)
5286
    }
5287
  }
5288
 
5289
  function selectAll(cm) {
5290
    cm.setSelection(Pos(cm.firstLine(), 0), Pos(cm.lastLine()), sel_dontScroll);
5291
  }
5292
 
5293
  // UPDATING
5294
 
5295
  // Allow "beforeChange" event handlers to influence a change
5296
  function filterChange(doc, change, update) {
5297
    var obj = {
5298
      canceled: false,
5299
      from: change.from,
5300
      to: change.to,
5301
      text: change.text,
5302
      origin: change.origin,
5303
      cancel: function () { return obj.canceled = true; }
5304
    };
5305
    if (update) { obj.update = function (from, to, text, origin) {
5306
      if (from) { obj.from = clipPos(doc, from); }
5307
      if (to) { obj.to = clipPos(doc, to); }
5308
      if (text) { obj.text = text; }
5309
      if (origin !== undefined) { obj.origin = origin; }
5310
    }; }
5311
    signal(doc, "beforeChange", doc, obj);
5312
    if (doc.cm) { signal(doc.cm, "beforeChange", doc.cm, obj); }
5313
 
5314
    if (obj.canceled) {
5315
      if (doc.cm) { doc.cm.curOp.updateInput = 2; }
5316
      return null
5317
    }
5318
    return {from: obj.from, to: obj.to, text: obj.text, origin: obj.origin}
5319
  }
5320
 
5321
  // Apply a change to a document, and add it to the document's
5322
  // history, and propagating it to all linked documents.
5323
  function makeChange(doc, change, ignoreReadOnly) {
5324
    if (doc.cm) {
5325
      if (!doc.cm.curOp) { return operation(doc.cm, makeChange)(doc, change, ignoreReadOnly) }
5326
      if (doc.cm.state.suppressEdits) { return }
5327
    }
5328
 
5329
    if (hasHandler(doc, "beforeChange") || doc.cm && hasHandler(doc.cm, "beforeChange")) {
5330
      change = filterChange(doc, change, true);
5331
      if (!change) { return }
5332
    }
5333
 
5334
    // Possibly split or suppress the update based on the presence
5335
    // of read-only spans in its range.
5336
    var split = sawReadOnlySpans && !ignoreReadOnly && removeReadOnlyRanges(doc, change.from, change.to);
5337
    if (split) {
5338
      for (var i = split.length - 1; i >= 0; --i)
5339
        { makeChangeInner(doc, {from: split[i].from, to: split[i].to, text: i ? [""] : change.text, origin: change.origin}); }
5340
    } else {
5341
      makeChangeInner(doc, change);
5342
    }
5343
  }
5344
 
5345
  function makeChangeInner(doc, change) {
5346
    if (change.text.length == 1 && change.text[0] == "" && cmp(change.from, change.to) == 0) { return }
5347
    var selAfter = computeSelAfterChange(doc, change);
5348
    addChangeToHistory(doc, change, selAfter, doc.cm ? doc.cm.curOp.id : NaN);
5349
 
5350
    makeChangeSingleDoc(doc, change, selAfter, stretchSpansOverChange(doc, change));
5351
    var rebased = [];
5352
 
5353
    linkedDocs(doc, function (doc, sharedHist) {
5354
      if (!sharedHist && indexOf(rebased, doc.history) == -1) {
5355
        rebaseHist(doc.history, change);
5356
        rebased.push(doc.history);
5357
      }
5358
      makeChangeSingleDoc(doc, change, null, stretchSpansOverChange(doc, change));
5359
    });
5360
  }
5361
 
5362
  // Revert a change stored in a document's history.
5363
  function makeChangeFromHistory(doc, type, allowSelectionOnly) {
5364
    var suppress = doc.cm && doc.cm.state.suppressEdits;
5365
    if (suppress && !allowSelectionOnly) { return }
5366
 
5367
    var hist = doc.history, event, selAfter = doc.sel;
5368
    var source = type == "undo" ? hist.done : hist.undone, dest = type == "undo" ? hist.undone : hist.done;
5369
 
5370
    // Verify that there is a useable event (so that ctrl-z won't
5371
    // needlessly clear selection events)
5372
    var i = 0;
5373
    for (; i < source.length; i++) {
5374
      event = source[i];
5375
      if (allowSelectionOnly ? event.ranges && !event.equals(doc.sel) : !event.ranges)
5376
        { break }
5377
    }
5378
    if (i == source.length) { return }
5379
    hist.lastOrigin = hist.lastSelOrigin = null;
5380
 
5381
    for (;;) {
5382
      event = source.pop();
5383
      if (event.ranges) {
5384
        pushSelectionToHistory(event, dest);
5385
        if (allowSelectionOnly && !event.equals(doc.sel)) {
5386
          setSelection(doc, event, {clearRedo: false});
5387
          return
5388
        }
5389
        selAfter = event;
5390
      } else if (suppress) {
5391
        source.push(event);
5392
        return
5393
      } else { break }
5394
    }
5395
 
5396
    // Build up a reverse change object to add to the opposite history
5397
    // stack (redo when undoing, and vice versa).
5398
    var antiChanges = [];
5399
    pushSelectionToHistory(selAfter, dest);
5400
    dest.push({changes: antiChanges, generation: hist.generation});
5401
    hist.generation = event.generation || ++hist.maxGeneration;
5402
 
5403
    var filter = hasHandler(doc, "beforeChange") || doc.cm && hasHandler(doc.cm, "beforeChange");
5404
 
5405
    var loop = function ( i ) {
5406
      var change = event.changes[i];
5407
      change.origin = type;
5408
      if (filter && !filterChange(doc, change, false)) {
5409
        source.length = 0;
5410
        return {}
5411
      }
5412
 
5413
      antiChanges.push(historyChangeFromChange(doc, change));
5414
 
5415
      var after = i ? computeSelAfterChange(doc, change) : lst(source);
5416
      makeChangeSingleDoc(doc, change, after, mergeOldSpans(doc, change));
5417
      if (!i && doc.cm) { doc.cm.scrollIntoView({from: change.from, to: changeEnd(change)}); }
5418
      var rebased = [];
5419
 
5420
      // Propagate to the linked documents
5421
      linkedDocs(doc, function (doc, sharedHist) {
5422
        if (!sharedHist && indexOf(rebased, doc.history) == -1) {
5423
          rebaseHist(doc.history, change);
5424
          rebased.push(doc.history);
5425
        }
5426
        makeChangeSingleDoc(doc, change, null, mergeOldSpans(doc, change));
5427
      });
5428
    };
5429
 
5430
    for (var i$1 = event.changes.length - 1; i$1 >= 0; --i$1) {
5431
      var returned = loop( i$1 );
5432
 
5433
      if ( returned ) return returned.v;
5434
    }
5435
  }
5436
 
5437
  // Sub-views need their line numbers shifted when text is added
5438
  // above or below them in the parent document.
5439
  function shiftDoc(doc, distance) {
5440
    if (distance == 0) { return }
5441
    doc.first += distance;
5442
    doc.sel = new Selection(map(doc.sel.ranges, function (range) { return new Range(
5443
      Pos(range.anchor.line + distance, range.anchor.ch),
5444
      Pos(range.head.line + distance, range.head.ch)
5445
    ); }), doc.sel.primIndex);
5446
    if (doc.cm) {
5447
      regChange(doc.cm, doc.first, doc.first - distance, distance);
5448
      for (var d = doc.cm.display, l = d.viewFrom; l < d.viewTo; l++)
5449
        { regLineChange(doc.cm, l, "gutter"); }
5450
    }
5451
  }
5452
 
5453
  // More lower-level change function, handling only a single document
5454
  // (not linked ones).
5455
  function makeChangeSingleDoc(doc, change, selAfter, spans) {
5456
    if (doc.cm && !doc.cm.curOp)
5457
      { return operation(doc.cm, makeChangeSingleDoc)(doc, change, selAfter, spans) }
5458
 
5459
    if (change.to.line < doc.first) {
5460
      shiftDoc(doc, change.text.length - 1 - (change.to.line - change.from.line));
5461
      return
5462
    }
5463
    if (change.from.line > doc.lastLine()) { return }
5464
 
5465
    // Clip the change to the size of this doc
5466
    if (change.from.line < doc.first) {
5467
      var shift = change.text.length - 1 - (doc.first - change.from.line);
5468
      shiftDoc(doc, shift);
5469
      change = {from: Pos(doc.first, 0), to: Pos(change.to.line + shift, change.to.ch),
5470
                text: [lst(change.text)], origin: change.origin};
5471
    }
5472
    var last = doc.lastLine();
5473
    if (change.to.line > last) {
5474
      change = {from: change.from, to: Pos(last, getLine(doc, last).text.length),
5475
                text: [change.text[0]], origin: change.origin};
5476
    }
5477
 
5478
    change.removed = getBetween(doc, change.from, change.to);
5479
 
5480
    if (!selAfter) { selAfter = computeSelAfterChange(doc, change); }
5481
    if (doc.cm) { makeChangeSingleDocInEditor(doc.cm, change, spans); }
5482
    else { updateDoc(doc, change, spans); }
5483
    setSelectionNoUndo(doc, selAfter, sel_dontScroll);
5484
 
5485
    if (doc.cantEdit && skipAtomic(doc, Pos(doc.firstLine(), 0)))
5486
      { doc.cantEdit = false; }
5487
  }
5488
 
5489
  // Handle the interaction of a change to a document with the editor
5490
  // that this document is part of.
5491
  function makeChangeSingleDocInEditor(cm, change, spans) {
5492
    var doc = cm.doc, display = cm.display, from = change.from, to = change.to;
5493
 
5494
    var recomputeMaxLength = false, checkWidthStart = from.line;
5495
    if (!cm.options.lineWrapping) {
5496
      checkWidthStart = lineNo(visualLine(getLine(doc, from.line)));
5497
      doc.iter(checkWidthStart, to.line + 1, function (line) {
5498
        if (line == display.maxLine) {
5499
          recomputeMaxLength = true;
5500
          return true
5501
        }
5502
      });
5503
    }
5504
 
5505
    if (doc.sel.contains(change.from, change.to) > -1)
5506
      { signalCursorActivity(cm); }
5507
 
5508
    updateDoc(doc, change, spans, estimateHeight(cm));
5509
 
5510
    if (!cm.options.lineWrapping) {
5511
      doc.iter(checkWidthStart, from.line + change.text.length, function (line) {
5512
        var len = lineLength(line);
5513
        if (len > display.maxLineLength) {
5514
          display.maxLine = line;
5515
          display.maxLineLength = len;
5516
          display.maxLineChanged = true;
5517
          recomputeMaxLength = false;
5518
        }
5519
      });
5520
      if (recomputeMaxLength) { cm.curOp.updateMaxLine = true; }
5521
    }
5522
 
5523
    retreatFrontier(doc, from.line);
5524
    startWorker(cm, 400);
5525
 
5526
    var lendiff = change.text.length - (to.line - from.line) - 1;
5527
    // Remember that these lines changed, for updating the display
5528
    if (change.full)
5529
      { regChange(cm); }
5530
    else if (from.line == to.line && change.text.length == 1 && !isWholeLineUpdate(cm.doc, change))
5531
      { regLineChange(cm, from.line, "text"); }
5532
    else
5533
      { regChange(cm, from.line, to.line + 1, lendiff); }
5534
 
5535
    var changesHandler = hasHandler(cm, "changes"), changeHandler = hasHandler(cm, "change");
5536
    if (changeHandler || changesHandler) {
5537
      var obj = {
5538
        from: from, to: to,
5539
        text: change.text,
5540
        removed: change.removed,
5541
        origin: change.origin
5542
      };
5543
      if (changeHandler) { signalLater(cm, "change", cm, obj); }
5544
      if (changesHandler) { (cm.curOp.changeObjs || (cm.curOp.changeObjs = [])).push(obj); }
5545
    }
5546
    cm.display.selForContextMenu = null;
5547
  }
5548
 
5549
  function replaceRange(doc, code, from, to, origin) {
5550
    var assign;
5551
 
5552
    if (!to) { to = from; }
5553
    if (cmp(to, from) < 0) { (assign = [to, from], from = assign[0], to = assign[1]); }
5554
    if (typeof code == "string") { code = doc.splitLines(code); }
5555
    makeChange(doc, {from: from, to: to, text: code, origin: origin});
5556
  }
5557
 
5558
  // Rebasing/resetting history to deal with externally-sourced changes
5559
 
5560
  function rebaseHistSelSingle(pos, from, to, diff) {
5561
    if (to < pos.line) {
5562
      pos.line += diff;
5563
    } else if (from < pos.line) {
5564
      pos.line = from;
5565
      pos.ch = 0;
5566
    }
5567
  }
5568
 
5569
  // Tries to rebase an array of history events given a change in the
5570
  // document. If the change touches the same lines as the event, the
5571
  // event, and everything 'behind' it, is discarded. If the change is
5572
  // before the event, the event's positions are updated. Uses a
5573
  // copy-on-write scheme for the positions, to avoid having to
5574
  // reallocate them all on every rebase, but also avoid problems with
5575
  // shared position objects being unsafely updated.
5576
  function rebaseHistArray(array, from, to, diff) {
5577
    for (var i = 0; i < array.length; ++i) {
5578
      var sub = array[i], ok = true;
5579
      if (sub.ranges) {
5580
        if (!sub.copied) { sub = array[i] = sub.deepCopy(); sub.copied = true; }
5581
        for (var j = 0; j < sub.ranges.length; j++) {
5582
          rebaseHistSelSingle(sub.ranges[j].anchor, from, to, diff);
5583
          rebaseHistSelSingle(sub.ranges[j].head, from, to, diff);
5584
        }
5585
        continue
5586
      }
5587
      for (var j$1 = 0; j$1 < sub.changes.length; ++j$1) {
5588
        var cur = sub.changes[j$1];
5589
        if (to < cur.from.line) {
5590
          cur.from = Pos(cur.from.line + diff, cur.from.ch);
5591
          cur.to = Pos(cur.to.line + diff, cur.to.ch);
5592
        } else if (from <= cur.to.line) {
5593
          ok = false;
5594
          break
5595
        }
5596
      }
5597
      if (!ok) {
5598
        array.splice(0, i + 1);
5599
        i = 0;
5600
      }
5601
    }
5602
  }
5603
 
5604
  function rebaseHist(hist, change) {
5605
    var from = change.from.line, to = change.to.line, diff = change.text.length - (to - from) - 1;
5606
    rebaseHistArray(hist.done, from, to, diff);
5607
    rebaseHistArray(hist.undone, from, to, diff);
5608
  }
5609
 
5610
  // Utility for applying a change to a line by handle or number,
5611
  // returning the number and optionally registering the line as
5612
  // changed.
5613
  function changeLine(doc, handle, changeType, op) {
5614
    var no = handle, line = handle;
5615
    if (typeof handle == "number") { line = getLine(doc, clipLine(doc, handle)); }
5616
    else { no = lineNo(handle); }
5617
    if (no == null) { return null }
5618
    if (op(line, no) && doc.cm) { regLineChange(doc.cm, no, changeType); }
5619
    return line
5620
  }
5621
 
5622
  // The document is represented as a BTree consisting of leaves, with
5623
  // chunk of lines in them, and branches, with up to ten leaves or
5624
  // other branch nodes below them. The top node is always a branch
5625
  // node, and is the document object itself (meaning it has
5626
  // additional methods and properties).
5627
  //
5628
  // All nodes have parent links. The tree is used both to go from
5629
  // line numbers to line objects, and to go from objects to numbers.
5630
  // It also indexes by height, and is used to convert between height
5631
  // and line object, and to find the total height of the document.
5632
  //
5633
  // See also http://marijnhaverbeke.nl/blog/codemirror-line-tree.html
5634
 
5635
  function LeafChunk(lines) {
5636
    this.lines = lines;
5637
    this.parent = null;
5638
    var height = 0;
5639
    for (var i = 0; i < lines.length; ++i) {
15152 obado 5640
      lines[i].parent = this;
14283 obado 5641
      height += lines[i].height;
5642
    }
5643
    this.height = height;
5644
  }
5645
 
5646
  LeafChunk.prototype = {
5647
    chunkSize: function() { return this.lines.length },
5648
 
5649
    // Remove the n lines at offset 'at'.
5650
    removeInner: function(at, n) {
5651
      for (var i = at, e = at + n; i < e; ++i) {
15152 obado 5652
        var line = this.lines[i];
5653
        this.height -= line.height;
14283 obado 5654
        cleanUpLine(line);
5655
        signalLater(line, "delete");
5656
      }
5657
      this.lines.splice(at, n);
5658
    },
5659
 
5660
    // Helper used to collapse a small branch into a single leaf.
5661
    collapse: function(lines) {
5662
      lines.push.apply(lines, this.lines);
5663
    },
5664
 
5665
    // Insert the given array of lines at offset 'at', count them as
5666
    // having the given height.
5667
    insertInner: function(at, lines, height) {
5668
      this.height += height;
5669
      this.lines = this.lines.slice(0, at).concat(lines).concat(this.lines.slice(at));
15152 obado 5670
      for (var i = 0; i < lines.length; ++i) { lines[i].parent = this; }
14283 obado 5671
    },
5672
 
5673
    // Used to iterate over a part of the tree.
5674
    iterN: function(at, n, op) {
5675
      for (var e = at + n; at < e; ++at)
15152 obado 5676
        { if (op(this.lines[at])) { return true } }
14283 obado 5677
    }
5678
  };
5679
 
5680
  function BranchChunk(children) {
5681
    this.children = children;
5682
    var size = 0, height = 0;
5683
    for (var i = 0; i < children.length; ++i) {
5684
      var ch = children[i];
5685
      size += ch.chunkSize(); height += ch.height;
15152 obado 5686
      ch.parent = this;
14283 obado 5687
    }
5688
    this.size = size;
5689
    this.height = height;
5690
    this.parent = null;
5691
  }
5692
 
5693
  BranchChunk.prototype = {
5694
    chunkSize: function() { return this.size },
5695
 
5696
    removeInner: function(at, n) {
5697
      this.size -= n;
5698
      for (var i = 0; i < this.children.length; ++i) {
15152 obado 5699
        var child = this.children[i], sz = child.chunkSize();
14283 obado 5700
        if (at < sz) {
5701
          var rm = Math.min(n, sz - at), oldHeight = child.height;
5702
          child.removeInner(at, rm);
15152 obado 5703
          this.height -= oldHeight - child.height;
5704
          if (sz == rm) { this.children.splice(i--, 1); child.parent = null; }
14283 obado 5705
          if ((n -= rm) == 0) { break }
5706
          at = 0;
5707
        } else { at -= sz; }
5708
      }
5709
      // If the result is smaller than 25 lines, ensure that it is a
5710
      // single leaf node.
5711
      if (this.size - n < 25 &&
5712
          (this.children.length > 1 || !(this.children[0] instanceof LeafChunk))) {
5713
        var lines = [];
5714
        this.collapse(lines);
5715
        this.children = [new LeafChunk(lines)];
5716
        this.children[0].parent = this;
5717
      }
5718
    },
5719
 
5720
    collapse: function(lines) {
15152 obado 5721
      for (var i = 0; i < this.children.length; ++i) { this.children[i].collapse(lines); }
14283 obado 5722
    },
5723
 
5724
    insertInner: function(at, lines, height) {
5725
      this.size += lines.length;
5726
      this.height += height;
5727
      for (var i = 0; i < this.children.length; ++i) {
15152 obado 5728
        var child = this.children[i], sz = child.chunkSize();
14283 obado 5729
        if (at <= sz) {
5730
          child.insertInner(at, lines, height);
5731
          if (child.lines && child.lines.length > 50) {
5732
            // To avoid memory thrashing when child.lines is huge (e.g. first view of a large file), it's never spliced.
5733
            // Instead, small slices are taken. They're taken in order because sequential memory accesses are fastest.
5734
            var remaining = child.lines.length % 25 + 25;
5735
            for (var pos = remaining; pos < child.lines.length;) {
5736
              var leaf = new LeafChunk(child.lines.slice(pos, pos += 25));
5737
              child.height -= leaf.height;
15152 obado 5738
              this.children.splice(++i, 0, leaf);
5739
              leaf.parent = this;
14283 obado 5740
            }
5741
            child.lines = child.lines.slice(0, remaining);
15152 obado 5742
            this.maybeSpill();
14283 obado 5743
          }
5744
          break
5745
        }
5746
        at -= sz;
5747
      }
5748
    },
5749
 
5750
    // When a node has grown, check whether it should be split.
5751
    maybeSpill: function() {
5752
      if (this.children.length <= 10) { return }
5753
      var me = this;
5754
      do {
5755
        var spilled = me.children.splice(me.children.length - 5, 5);
5756
        var sibling = new BranchChunk(spilled);
5757
        if (!me.parent) { // Become the parent node
5758
          var copy = new BranchChunk(me.children);
5759
          copy.parent = me;
5760
          me.children = [copy, sibling];
5761
          me = copy;
5762
       } else {
5763
          me.size -= sibling.size;
5764
          me.height -= sibling.height;
5765
          var myIndex = indexOf(me.parent.children, me);
5766
          me.parent.children.splice(myIndex + 1, 0, sibling);
5767
        }
5768
        sibling.parent = me.parent;
5769
      } while (me.children.length > 10)
5770
      me.parent.maybeSpill();
5771
    },
5772
 
5773
    iterN: function(at, n, op) {
5774
      for (var i = 0; i < this.children.length; ++i) {
15152 obado 5775
        var child = this.children[i], sz = child.chunkSize();
14283 obado 5776
        if (at < sz) {
5777
          var used = Math.min(n, sz - at);
5778
          if (child.iterN(at, used, op)) { return true }
5779
          if ((n -= used) == 0) { break }
5780
          at = 0;
5781
        } else { at -= sz; }
5782
      }
5783
    }
5784
  };
5785
 
5786
  // Line widgets are block elements displayed above or below a line.
5787
 
5788
  var LineWidget = function(doc, node, options) {
5789
    if (options) { for (var opt in options) { if (options.hasOwnProperty(opt))
15152 obado 5790
      { this[opt] = options[opt]; } } }
14283 obado 5791
    this.doc = doc;
5792
    this.node = node;
5793
  };
5794
 
5795
  LineWidget.prototype.clear = function () {
5796
    var cm = this.doc.cm, ws = this.line.widgets, line = this.line, no = lineNo(line);
5797
    if (no == null || !ws) { return }
15152 obado 5798
    for (var i = 0; i < ws.length; ++i) { if (ws[i] == this) { ws.splice(i--, 1); } }
14283 obado 5799
    if (!ws.length) { line.widgets = null; }
5800
    var height = widgetHeight(this);
5801
    updateLineHeight(line, Math.max(0, line.height - height));
5802
    if (cm) {
5803
      runInOp(cm, function () {
5804
        adjustScrollWhenAboveVisible(cm, line, -height);
5805
        regLineChange(cm, no, "widget");
5806
      });
5807
      signalLater(cm, "lineWidgetCleared", cm, this, no);
5808
    }
5809
  };
5810
 
5811
  LineWidget.prototype.changed = function () {
5812
      var this$1 = this;
5813
 
5814
    var oldH = this.height, cm = this.doc.cm, line = this.line;
5815
    this.height = null;
5816
    var diff = widgetHeight(this) - oldH;
5817
    if (!diff) { return }
5818
    if (!lineIsHidden(this.doc, line)) { updateLineHeight(line, line.height + diff); }
5819
    if (cm) {
5820
      runInOp(cm, function () {
5821
        cm.curOp.forceUpdate = true;
5822
        adjustScrollWhenAboveVisible(cm, line, diff);
5823
        signalLater(cm, "lineWidgetChanged", cm, this$1, lineNo(line));
5824
      });
5825
    }
5826
  };
5827
  eventMixin(LineWidget);
5828
 
5829
  function adjustScrollWhenAboveVisible(cm, line, diff) {
5830
    if (heightAtLine(line) < ((cm.curOp && cm.curOp.scrollTop) || cm.doc.scrollTop))
5831
      { addToScrollTop(cm, diff); }
5832
  }
5833
 
5834
  function addLineWidget(doc, handle, node, options) {
5835
    var widget = new LineWidget(doc, node, options);
5836
    var cm = doc.cm;
5837
    if (cm && widget.noHScroll) { cm.display.alignWidgets = true; }
5838
    changeLine(doc, handle, "widget", function (line) {
5839
      var widgets = line.widgets || (line.widgets = []);
5840
      if (widget.insertAt == null) { widgets.push(widget); }
16493 obado 5841
      else { widgets.splice(Math.min(widgets.length, Math.max(0, widget.insertAt)), 0, widget); }
14283 obado 5842
      widget.line = line;
5843
      if (cm && !lineIsHidden(doc, line)) {
5844
        var aboveVisible = heightAtLine(line) < doc.scrollTop;
5845
        updateLineHeight(line, line.height + widgetHeight(widget));
5846
        if (aboveVisible) { addToScrollTop(cm, widget.height); }
5847
        cm.curOp.forceUpdate = true;
5848
      }
5849
      return true
5850
    });
5851
    if (cm) { signalLater(cm, "lineWidgetAdded", cm, widget, typeof handle == "number" ? handle : lineNo(handle)); }
5852
    return widget
5853
  }
5854
 
5855
  // TEXTMARKERS
5856
 
5857
  // Created with markText and setBookmark methods. A TextMarker is a
5858
  // handle that can be used to clear or find a marked position in the
5859
  // document. Line objects hold arrays (markedSpans) containing
5860
  // {from, to, marker} object pointing to such marker objects, and
5861
  // indicating that such a marker is present on that line. Multiple
5862
  // lines may point to the same marker when it spans across lines.
5863
  // The spans will have null for their from/to properties when the
5864
  // marker continues beyond the start/end of the line. Markers have
5865
  // links back to the lines they currently touch.
5866
 
5867
  // Collapsed markers have unique ids, in order to be able to order
5868
  // them, which is needed for uniquely determining an outer marker
5869
  // when they overlap (they may nest, but not partially overlap).
5870
  var nextMarkerId = 0;
5871
 
5872
  var TextMarker = function(doc, type) {
5873
    this.lines = [];
5874
    this.type = type;
5875
    this.doc = doc;
5876
    this.id = ++nextMarkerId;
5877
  };
5878
 
5879
  // Clear the marker.
5880
  TextMarker.prototype.clear = function () {
5881
    if (this.explicitlyCleared) { return }
5882
    var cm = this.doc.cm, withOp = cm && !cm.curOp;
5883
    if (withOp) { startOperation(cm); }
5884
    if (hasHandler(this, "clear")) {
5885
      var found = this.find();
5886
      if (found) { signalLater(this, "clear", found.from, found.to); }
5887
    }
5888
    var min = null, max = null;
5889
    for (var i = 0; i < this.lines.length; ++i) {
15152 obado 5890
      var line = this.lines[i];
5891
      var span = getMarkedSpanFor(line.markedSpans, this);
5892
      if (cm && !this.collapsed) { regLineChange(cm, lineNo(line), "text"); }
14283 obado 5893
      else if (cm) {
5894
        if (span.to != null) { max = lineNo(line); }
5895
        if (span.from != null) { min = lineNo(line); }
5896
      }
5897
      line.markedSpans = removeMarkedSpan(line.markedSpans, span);
15152 obado 5898
      if (span.from == null && this.collapsed && !lineIsHidden(this.doc, line) && cm)
14283 obado 5899
        { updateLineHeight(line, textHeight(cm.display)); }
5900
    }
5901
    if (cm && this.collapsed && !cm.options.lineWrapping) { for (var i$1 = 0; i$1 < this.lines.length; ++i$1) {
15152 obado 5902
      var visual = visualLine(this.lines[i$1]), len = lineLength(visual);
14283 obado 5903
      if (len > cm.display.maxLineLength) {
5904
        cm.display.maxLine = visual;
5905
        cm.display.maxLineLength = len;
5906
        cm.display.maxLineChanged = true;
5907
      }
5908
    } }
5909
 
5910
    if (min != null && cm && this.collapsed) { regChange(cm, min, max + 1); }
5911
    this.lines.length = 0;
5912
    this.explicitlyCleared = true;
5913
    if (this.atomic && this.doc.cantEdit) {
5914
      this.doc.cantEdit = false;
5915
      if (cm) { reCheckSelection(cm.doc); }
5916
    }
5917
    if (cm) { signalLater(cm, "markerCleared", cm, this, min, max); }
5918
    if (withOp) { endOperation(cm); }
5919
    if (this.parent) { this.parent.clear(); }
5920
  };
5921
 
5922
  // Find the position of the marker in the document. Returns a {from,
5923
  // to} object by default. Side can be passed to get a specific side
5924
  // -- 0 (both), -1 (left), or 1 (right). When lineObj is true, the
5925
  // Pos objects returned contain a line object, rather than a line
5926
  // number (used to prevent looking up the same line twice).
5927
  TextMarker.prototype.find = function (side, lineObj) {
5928
    if (side == null && this.type == "bookmark") { side = 1; }
5929
    var from, to;
5930
    for (var i = 0; i < this.lines.length; ++i) {
15152 obado 5931
      var line = this.lines[i];
5932
      var span = getMarkedSpanFor(line.markedSpans, this);
14283 obado 5933
      if (span.from != null) {
5934
        from = Pos(lineObj ? line : lineNo(line), span.from);
5935
        if (side == -1) { return from }
5936
      }
5937
      if (span.to != null) {
5938
        to = Pos(lineObj ? line : lineNo(line), span.to);
5939
        if (side == 1) { return to }
5940
      }
5941
    }
5942
    return from && {from: from, to: to}
5943
  };
5944
 
5945
  // Signals that the marker's widget changed, and surrounding layout
5946
  // should be recomputed.
5947
  TextMarker.prototype.changed = function () {
5948
      var this$1 = this;
5949
 
5950
    var pos = this.find(-1, true), widget = this, cm = this.doc.cm;
5951
    if (!pos || !cm) { return }
5952
    runInOp(cm, function () {
5953
      var line = pos.line, lineN = lineNo(pos.line);
5954
      var view = findViewForLine(cm, lineN);
5955
      if (view) {
5956
        clearLineMeasurementCacheFor(view);
5957
        cm.curOp.selectionChanged = cm.curOp.forceUpdate = true;
5958
      }
5959
      cm.curOp.updateMaxLine = true;
5960
      if (!lineIsHidden(widget.doc, line) && widget.height != null) {
5961
        var oldHeight = widget.height;
5962
        widget.height = null;
5963
        var dHeight = widgetHeight(widget) - oldHeight;
5964
        if (dHeight)
5965
          { updateLineHeight(line, line.height + dHeight); }
5966
      }
5967
      signalLater(cm, "markerChanged", cm, this$1);
5968
    });
5969
  };
5970
 
5971
  TextMarker.prototype.attachLine = function (line) {
5972
    if (!this.lines.length && this.doc.cm) {
5973
      var op = this.doc.cm.curOp;
5974
      if (!op.maybeHiddenMarkers || indexOf(op.maybeHiddenMarkers, this) == -1)
5975
        { (op.maybeUnhiddenMarkers || (op.maybeUnhiddenMarkers = [])).push(this); }
5976
    }
5977
    this.lines.push(line);
5978
  };
5979
 
5980
  TextMarker.prototype.detachLine = function (line) {
5981
    this.lines.splice(indexOf(this.lines, line), 1);
5982
    if (!this.lines.length && this.doc.cm) {
5983
      var op = this.doc.cm.curOp
5984
      ;(op.maybeHiddenMarkers || (op.maybeHiddenMarkers = [])).push(this);
5985
    }
5986
  };
5987
  eventMixin(TextMarker);
5988
 
5989
  // Create a marker, wire it up to the right lines, and
5990
  function markText(doc, from, to, options, type) {
5991
    // Shared markers (across linked documents) are handled separately
5992
    // (markTextShared will call out to this again, once per
5993
    // document).
5994
    if (options && options.shared) { return markTextShared(doc, from, to, options, type) }
5995
    // Ensure we are in an operation.
5996
    if (doc.cm && !doc.cm.curOp) { return operation(doc.cm, markText)(doc, from, to, options, type) }
5997
 
5998
    var marker = new TextMarker(doc, type), diff = cmp(from, to);
5999
    if (options) { copyObj(options, marker, false); }
6000
    // Don't connect empty markers unless clearWhenEmpty is false
6001
    if (diff > 0 || diff == 0 && marker.clearWhenEmpty !== false)
6002
      { return marker }
6003
    if (marker.replacedWith) {
6004
      // Showing up as a widget implies collapsed (widget replaces text)
6005
      marker.collapsed = true;
6006
      marker.widgetNode = eltP("span", [marker.replacedWith], "CodeMirror-widget");
6007
      if (!options.handleMouseEvents) { marker.widgetNode.setAttribute("cm-ignore-events", "true"); }
6008
      if (options.insertLeft) { marker.widgetNode.insertLeft = true; }
6009
    }
6010
    if (marker.collapsed) {
6011
      if (conflictingCollapsedRange(doc, from.line, from, to, marker) ||
6012
          from.line != to.line && conflictingCollapsedRange(doc, to.line, from, to, marker))
6013
        { throw new Error("Inserting collapsed marker partially overlapping an existing one") }
6014
      seeCollapsedSpans();
6015
    }
6016
 
6017
    if (marker.addToHistory)
6018
      { addChangeToHistory(doc, {from: from, to: to, origin: "markText"}, doc.sel, NaN); }
6019
 
6020
    var curLine = from.line, cm = doc.cm, updateMaxLine;
6021
    doc.iter(curLine, to.line + 1, function (line) {
6022
      if (cm && marker.collapsed && !cm.options.lineWrapping && visualLine(line) == cm.display.maxLine)
6023
        { updateMaxLine = true; }
6024
      if (marker.collapsed && curLine != from.line) { updateLineHeight(line, 0); }
6025
      addMarkedSpan(line, new MarkedSpan(marker,
6026
                                         curLine == from.line ? from.ch : null,
16493 obado 6027
                                         curLine == to.line ? to.ch : null), doc.cm && doc.cm.curOp);
14283 obado 6028
      ++curLine;
6029
    });
6030
    // lineIsHidden depends on the presence of the spans, so needs a second pass
6031
    if (marker.collapsed) { doc.iter(from.line, to.line + 1, function (line) {
6032
      if (lineIsHidden(doc, line)) { updateLineHeight(line, 0); }
6033
    }); }
6034
 
6035
    if (marker.clearOnEnter) { on(marker, "beforeCursorEnter", function () { return marker.clear(); }); }
6036
 
6037
    if (marker.readOnly) {
6038
      seeReadOnlySpans();
6039
      if (doc.history.done.length || doc.history.undone.length)
6040
        { doc.clearHistory(); }
6041
    }
6042
    if (marker.collapsed) {
6043
      marker.id = ++nextMarkerId;
6044
      marker.atomic = true;
6045
    }
6046
    if (cm) {
6047
      // Sync editor state
6048
      if (updateMaxLine) { cm.curOp.updateMaxLine = true; }
6049
      if (marker.collapsed)
6050
        { regChange(cm, from.line, to.line + 1); }
6051
      else if (marker.className || marker.startStyle || marker.endStyle || marker.css ||
6052
               marker.attributes || marker.title)
6053
        { for (var i = from.line; i <= to.line; i++) { regLineChange(cm, i, "text"); } }
6054
      if (marker.atomic) { reCheckSelection(cm.doc); }
6055
      signalLater(cm, "markerAdded", cm, marker);
6056
    }
6057
    return marker
6058
  }
6059
 
6060
  // SHARED TEXTMARKERS
6061
 
6062
  // A shared marker spans multiple linked documents. It is
6063
  // implemented as a meta-marker-object controlling multiple normal
6064
  // markers.
6065
  var SharedTextMarker = function(markers, primary) {
6066
    this.markers = markers;
6067
    this.primary = primary;
6068
    for (var i = 0; i < markers.length; ++i)
15152 obado 6069
      { markers[i].parent = this; }
14283 obado 6070
  };
6071
 
6072
  SharedTextMarker.prototype.clear = function () {
6073
    if (this.explicitlyCleared) { return }
6074
    this.explicitlyCleared = true;
6075
    for (var i = 0; i < this.markers.length; ++i)
15152 obado 6076
      { this.markers[i].clear(); }
14283 obado 6077
    signalLater(this, "clear");
6078
  };
6079
 
6080
  SharedTextMarker.prototype.find = function (side, lineObj) {
6081
    return this.primary.find(side, lineObj)
6082
  };
6083
  eventMixin(SharedTextMarker);
6084
 
6085
  function markTextShared(doc, from, to, options, type) {
6086
    options = copyObj(options);
6087
    options.shared = false;
6088
    var markers = [markText(doc, from, to, options, type)], primary = markers[0];
6089
    var widget = options.widgetNode;
6090
    linkedDocs(doc, function (doc) {
6091
      if (widget) { options.widgetNode = widget.cloneNode(true); }
6092
      markers.push(markText(doc, clipPos(doc, from), clipPos(doc, to), options, type));
6093
      for (var i = 0; i < doc.linked.length; ++i)
6094
        { if (doc.linked[i].isParent) { return } }
6095
      primary = lst(markers);
6096
    });
6097
    return new SharedTextMarker(markers, primary)
6098
  }
6099
 
6100
  function findSharedMarkers(doc) {
6101
    return doc.findMarks(Pos(doc.first, 0), doc.clipPos(Pos(doc.lastLine())), function (m) { return m.parent; })
6102
  }
6103
 
6104
  function copySharedMarkers(doc, markers) {
6105
    for (var i = 0; i < markers.length; i++) {
6106
      var marker = markers[i], pos = marker.find();
6107
      var mFrom = doc.clipPos(pos.from), mTo = doc.clipPos(pos.to);
6108
      if (cmp(mFrom, mTo)) {
6109
        var subMark = markText(doc, mFrom, mTo, marker.primary, marker.primary.type);
6110
        marker.markers.push(subMark);
6111
        subMark.parent = marker;
6112
      }
6113
    }
6114
  }
6115
 
6116
  function detachSharedMarkers(markers) {
6117
    var loop = function ( i ) {
6118
      var marker = markers[i], linked = [marker.primary.doc];
6119
      linkedDocs(marker.primary.doc, function (d) { return linked.push(d); });
6120
      for (var j = 0; j < marker.markers.length; j++) {
6121
        var subMarker = marker.markers[j];
6122
        if (indexOf(linked, subMarker.doc) == -1) {
6123
          subMarker.parent = null;
6124
          marker.markers.splice(j--, 1);
6125
        }
6126
      }
6127
    };
6128
 
6129
    for (var i = 0; i < markers.length; i++) loop( i );
6130
  }
6131
 
6132
  var nextDocId = 0;
6133
  var Doc = function(text, mode, firstLine, lineSep, direction) {
6134
    if (!(this instanceof Doc)) { return new Doc(text, mode, firstLine, lineSep, direction) }
6135
    if (firstLine == null) { firstLine = 0; }
6136
 
6137
    BranchChunk.call(this, [new LeafChunk([new Line("", null)])]);
6138
    this.first = firstLine;
6139
    this.scrollTop = this.scrollLeft = 0;
6140
    this.cantEdit = false;
6141
    this.cleanGeneration = 1;
6142
    this.modeFrontier = this.highlightFrontier = firstLine;
6143
    var start = Pos(firstLine, 0);
6144
    this.sel = simpleSelection(start);
6145
    this.history = new History(null);
6146
    this.id = ++nextDocId;
6147
    this.modeOption = mode;
6148
    this.lineSep = lineSep;
6149
    this.direction = (direction == "rtl") ? "rtl" : "ltr";
6150
    this.extend = false;
6151
 
6152
    if (typeof text == "string") { text = this.splitLines(text); }
6153
    updateDoc(this, {from: start, to: start, text: text});
6154
    setSelection(this, simpleSelection(start), sel_dontScroll);
6155
  };
6156
 
6157
  Doc.prototype = createObj(BranchChunk.prototype, {
6158
    constructor: Doc,
6159
    // Iterate over the document. Supports two forms -- with only one
6160
    // argument, it calls that for each line in the document. With
6161
    // three, it iterates over the range given by the first two (with
6162
    // the second being non-inclusive).
6163
    iter: function(from, to, op) {
6164
      if (op) { this.iterN(from - this.first, to - from, op); }
6165
      else { this.iterN(this.first, this.first + this.size, from); }
6166
    },
6167
 
6168
    // Non-public interface for adding and removing lines.
6169
    insert: function(at, lines) {
6170
      var height = 0;
6171
      for (var i = 0; i < lines.length; ++i) { height += lines[i].height; }
6172
      this.insertInner(at - this.first, lines, height);
6173
    },
6174
    remove: function(at, n) { this.removeInner(at - this.first, n); },
6175
 
6176
    // From here, the methods are part of the public interface. Most
6177
    // are also available from CodeMirror (editor) instances.
6178
 
6179
    getValue: function(lineSep) {
6180
      var lines = getLines(this, this.first, this.first + this.size);
6181
      if (lineSep === false) { return lines }
6182
      return lines.join(lineSep || this.lineSeparator())
6183
    },
6184
    setValue: docMethodOp(function(code) {
6185
      var top = Pos(this.first, 0), last = this.first + this.size - 1;
6186
      makeChange(this, {from: top, to: Pos(last, getLine(this, last).text.length),
6187
                        text: this.splitLines(code), origin: "setValue", full: true}, true);
6188
      if (this.cm) { scrollToCoords(this.cm, 0, 0); }
6189
      setSelection(this, simpleSelection(top), sel_dontScroll);
6190
    }),
6191
    replaceRange: function(code, from, to, origin) {
6192
      from = clipPos(this, from);
6193
      to = to ? clipPos(this, to) : from;
6194
      replaceRange(this, code, from, to, origin);
6195
    },
6196
    getRange: function(from, to, lineSep) {
6197
      var lines = getBetween(this, clipPos(this, from), clipPos(this, to));
6198
      if (lineSep === false) { return lines }
16493 obado 6199
      if (lineSep === '') { return lines.join('') }
14283 obado 6200
      return lines.join(lineSep || this.lineSeparator())
6201
    },
6202
 
6203
    getLine: function(line) {var l = this.getLineHandle(line); return l && l.text},
6204
 
6205
    getLineHandle: function(line) {if (isLine(this, line)) { return getLine(this, line) }},
6206
    getLineNumber: function(line) {return lineNo(line)},
6207
 
6208
    getLineHandleVisualStart: function(line) {
6209
      if (typeof line == "number") { line = getLine(this, line); }
6210
      return visualLine(line)
6211
    },
6212
 
6213
    lineCount: function() {return this.size},
6214
    firstLine: function() {return this.first},
6215
    lastLine: function() {return this.first + this.size - 1},
6216
 
6217
    clipPos: function(pos) {return clipPos(this, pos)},
6218
 
6219
    getCursor: function(start) {
15152 obado 6220
      var range = this.sel.primary(), pos;
6221
      if (start == null || start == "head") { pos = range.head; }
6222
      else if (start == "anchor") { pos = range.anchor; }
6223
      else if (start == "end" || start == "to" || start === false) { pos = range.to(); }
6224
      else { pos = range.from(); }
14283 obado 6225
      return pos
6226
    },
6227
    listSelections: function() { return this.sel.ranges },
6228
    somethingSelected: function() {return this.sel.somethingSelected()},
6229
 
6230
    setCursor: docMethodOp(function(line, ch, options) {
6231
      setSimpleSelection(this, clipPos(this, typeof line == "number" ? Pos(line, ch || 0) : line), null, options);
6232
    }),
6233
    setSelection: docMethodOp(function(anchor, head, options) {
6234
      setSimpleSelection(this, clipPos(this, anchor), clipPos(this, head || anchor), options);
6235
    }),
6236
    extendSelection: docMethodOp(function(head, other, options) {
6237
      extendSelection(this, clipPos(this, head), other && clipPos(this, other), options);
6238
    }),
6239
    extendSelections: docMethodOp(function(heads, options) {
6240
      extendSelections(this, clipPosArray(this, heads), options);
6241
    }),
6242
    extendSelectionsBy: docMethodOp(function(f, options) {
6243
      var heads = map(this.sel.ranges, f);
6244
      extendSelections(this, clipPosArray(this, heads), options);
6245
    }),
6246
    setSelections: docMethodOp(function(ranges, primary, options) {
6247
      if (!ranges.length) { return }
6248
      var out = [];
6249
      for (var i = 0; i < ranges.length; i++)
15152 obado 6250
        { out[i] = new Range(clipPos(this, ranges[i].anchor),
16493 obado 6251
                           clipPos(this, ranges[i].head || ranges[i].anchor)); }
14283 obado 6252
      if (primary == null) { primary = Math.min(ranges.length - 1, this.sel.primIndex); }
6253
      setSelection(this, normalizeSelection(this.cm, out, primary), options);
6254
    }),
6255
    addSelection: docMethodOp(function(anchor, head, options) {
6256
      var ranges = this.sel.ranges.slice(0);
6257
      ranges.push(new Range(clipPos(this, anchor), clipPos(this, head || anchor)));
6258
      setSelection(this, normalizeSelection(this.cm, ranges, ranges.length - 1), options);
6259
    }),
6260
 
6261
    getSelection: function(lineSep) {
6262
      var ranges = this.sel.ranges, lines;
6263
      for (var i = 0; i < ranges.length; i++) {
15152 obado 6264
        var sel = getBetween(this, ranges[i].from(), ranges[i].to());
14283 obado 6265
        lines = lines ? lines.concat(sel) : sel;
6266
      }
6267
      if (lineSep === false) { return lines }
6268
      else { return lines.join(lineSep || this.lineSeparator()) }
6269
    },
6270
    getSelections: function(lineSep) {
6271
      var parts = [], ranges = this.sel.ranges;
6272
      for (var i = 0; i < ranges.length; i++) {
15152 obado 6273
        var sel = getBetween(this, ranges[i].from(), ranges[i].to());
6274
        if (lineSep !== false) { sel = sel.join(lineSep || this.lineSeparator()); }
14283 obado 6275
        parts[i] = sel;
6276
      }
6277
      return parts
6278
    },
6279
    replaceSelection: function(code, collapse, origin) {
6280
      var dup = [];
6281
      for (var i = 0; i < this.sel.ranges.length; i++)
6282
        { dup[i] = code; }
6283
      this.replaceSelections(dup, collapse, origin || "+input");
6284
    },
6285
    replaceSelections: docMethodOp(function(code, collapse, origin) {
6286
      var changes = [], sel = this.sel;
6287
      for (var i = 0; i < sel.ranges.length; i++) {
15152 obado 6288
        var range = sel.ranges[i];
6289
        changes[i] = {from: range.from(), to: range.to(), text: this.splitLines(code[i]), origin: origin};
14283 obado 6290
      }
6291
      var newSel = collapse && collapse != "end" && computeReplacedSel(this, changes, collapse);
6292
      for (var i$1 = changes.length - 1; i$1 >= 0; i$1--)
15152 obado 6293
        { makeChange(this, changes[i$1]); }
14283 obado 6294
      if (newSel) { setSelectionReplaceHistory(this, newSel); }
6295
      else if (this.cm) { ensureCursorVisible(this.cm); }
6296
    }),
6297
    undo: docMethodOp(function() {makeChangeFromHistory(this, "undo");}),
6298
    redo: docMethodOp(function() {makeChangeFromHistory(this, "redo");}),
6299
    undoSelection: docMethodOp(function() {makeChangeFromHistory(this, "undo", true);}),
6300
    redoSelection: docMethodOp(function() {makeChangeFromHistory(this, "redo", true);}),
6301
 
6302
    setExtending: function(val) {this.extend = val;},
6303
    getExtending: function() {return this.extend},
6304
 
6305
    historySize: function() {
6306
      var hist = this.history, done = 0, undone = 0;
6307
      for (var i = 0; i < hist.done.length; i++) { if (!hist.done[i].ranges) { ++done; } }
6308
      for (var i$1 = 0; i$1 < hist.undone.length; i$1++) { if (!hist.undone[i$1].ranges) { ++undone; } }
6309
      return {undo: done, redo: undone}
6310
    },
15152 obado 6311
    clearHistory: function() {
6312
      var this$1 = this;
14283 obado 6313
 
16493 obado 6314
      this.history = new History(this.history);
15152 obado 6315
      linkedDocs(this, function (doc) { return doc.history = this$1.history; }, true);
6316
    },
6317
 
14283 obado 6318
    markClean: function() {
6319
      this.cleanGeneration = this.changeGeneration(true);
6320
    },
6321
    changeGeneration: function(forceSplit) {
6322
      if (forceSplit)
6323
        { this.history.lastOp = this.history.lastSelOp = this.history.lastOrigin = null; }
6324
      return this.history.generation
6325
    },
6326
    isClean: function (gen) {
6327
      return this.history.generation == (gen || this.cleanGeneration)
6328
    },
6329
 
6330
    getHistory: function() {
6331
      return {done: copyHistoryArray(this.history.done),
6332
              undone: copyHistoryArray(this.history.undone)}
6333
    },
6334
    setHistory: function(histData) {
16493 obado 6335
      var hist = this.history = new History(this.history);
14283 obado 6336
      hist.done = copyHistoryArray(histData.done.slice(0), null, true);
6337
      hist.undone = copyHistoryArray(histData.undone.slice(0), null, true);
6338
    },
6339
 
6340
    setGutterMarker: docMethodOp(function(line, gutterID, value) {
6341
      return changeLine(this, line, "gutter", function (line) {
6342
        var markers = line.gutterMarkers || (line.gutterMarkers = {});
6343
        markers[gutterID] = value;
6344
        if (!value && isEmpty(markers)) { line.gutterMarkers = null; }
6345
        return true
6346
      })
6347
    }),
6348
 
6349
    clearGutter: docMethodOp(function(gutterID) {
6350
      var this$1 = this;
6351
 
6352
      this.iter(function (line) {
6353
        if (line.gutterMarkers && line.gutterMarkers[gutterID]) {
6354
          changeLine(this$1, line, "gutter", function () {
6355
            line.gutterMarkers[gutterID] = null;
6356
            if (isEmpty(line.gutterMarkers)) { line.gutterMarkers = null; }
6357
            return true
6358
          });
6359
        }
6360
      });
6361
    }),
6362
 
6363
    lineInfo: function(line) {
6364
      var n;
6365
      if (typeof line == "number") {
6366
        if (!isLine(this, line)) { return null }
6367
        n = line;
6368
        line = getLine(this, line);
6369
        if (!line) { return null }
6370
      } else {
6371
        n = lineNo(line);
6372
        if (n == null) { return null }
6373
      }
6374
      return {line: n, handle: line, text: line.text, gutterMarkers: line.gutterMarkers,
6375
              textClass: line.textClass, bgClass: line.bgClass, wrapClass: line.wrapClass,
6376
              widgets: line.widgets}
6377
    },
6378
 
6379
    addLineClass: docMethodOp(function(handle, where, cls) {
6380
      return changeLine(this, handle, where == "gutter" ? "gutter" : "class", function (line) {
6381
        var prop = where == "text" ? "textClass"
6382
                 : where == "background" ? "bgClass"
6383
                 : where == "gutter" ? "gutterClass" : "wrapClass";
6384
        if (!line[prop]) { line[prop] = cls; }
6385
        else if (classTest(cls).test(line[prop])) { return false }
6386
        else { line[prop] += " " + cls; }
6387
        return true
6388
      })
6389
    }),
6390
    removeLineClass: docMethodOp(function(handle, where, cls) {
6391
      return changeLine(this, handle, where == "gutter" ? "gutter" : "class", function (line) {
6392
        var prop = where == "text" ? "textClass"
6393
                 : where == "background" ? "bgClass"
6394
                 : where == "gutter" ? "gutterClass" : "wrapClass";
6395
        var cur = line[prop];
6396
        if (!cur) { return false }
6397
        else if (cls == null) { line[prop] = null; }
6398
        else {
6399
          var found = cur.match(classTest(cls));
6400
          if (!found) { return false }
6401
          var end = found.index + found[0].length;
6402
          line[prop] = cur.slice(0, found.index) + (!found.index || end == cur.length ? "" : " ") + cur.slice(end) || null;
6403
        }
6404
        return true
6405
      })
6406
    }),
6407
 
6408
    addLineWidget: docMethodOp(function(handle, node, options) {
6409
      return addLineWidget(this, handle, node, options)
6410
    }),
6411
    removeLineWidget: function(widget) { widget.clear(); },
6412
 
6413
    markText: function(from, to, options) {
6414
      return markText(this, clipPos(this, from), clipPos(this, to), options, options && options.type || "range")
6415
    },
6416
    setBookmark: function(pos, options) {
6417
      var realOpts = {replacedWith: options && (options.nodeType == null ? options.widget : options),
6418
                      insertLeft: options && options.insertLeft,
6419
                      clearWhenEmpty: false, shared: options && options.shared,
6420
                      handleMouseEvents: options && options.handleMouseEvents};
6421
      pos = clipPos(this, pos);
6422
      return markText(this, pos, pos, realOpts, "bookmark")
6423
    },
6424
    findMarksAt: function(pos) {
6425
      pos = clipPos(this, pos);
6426
      var markers = [], spans = getLine(this, pos.line).markedSpans;
6427
      if (spans) { for (var i = 0; i < spans.length; ++i) {
6428
        var span = spans[i];
6429
        if ((span.from == null || span.from <= pos.ch) &&
6430
            (span.to == null || span.to >= pos.ch))
6431
          { markers.push(span.marker.parent || span.marker); }
6432
      } }
6433
      return markers
6434
    },
6435
    findMarks: function(from, to, filter) {
6436
      from = clipPos(this, from); to = clipPos(this, to);
15152 obado 6437
      var found = [], lineNo = from.line;
14283 obado 6438
      this.iter(from.line, to.line + 1, function (line) {
6439
        var spans = line.markedSpans;
6440
        if (spans) { for (var i = 0; i < spans.length; i++) {
6441
          var span = spans[i];
15152 obado 6442
          if (!(span.to != null && lineNo == from.line && from.ch >= span.to ||
6443
                span.from == null && lineNo != from.line ||
6444
                span.from != null && lineNo == to.line && span.from >= to.ch) &&
14283 obado 6445
              (!filter || filter(span.marker)))
6446
            { found.push(span.marker.parent || span.marker); }
6447
        } }
15152 obado 6448
        ++lineNo;
14283 obado 6449
      });
6450
      return found
6451
    },
6452
    getAllMarks: function() {
6453
      var markers = [];
6454
      this.iter(function (line) {
6455
        var sps = line.markedSpans;
6456
        if (sps) { for (var i = 0; i < sps.length; ++i)
6457
          { if (sps[i].from != null) { markers.push(sps[i].marker); } } }
6458
      });
6459
      return markers
6460
    },
6461
 
6462
    posFromIndex: function(off) {
15152 obado 6463
      var ch, lineNo = this.first, sepSize = this.lineSeparator().length;
14283 obado 6464
      this.iter(function (line) {
6465
        var sz = line.text.length + sepSize;
6466
        if (sz > off) { ch = off; return true }
6467
        off -= sz;
15152 obado 6468
        ++lineNo;
14283 obado 6469
      });
15152 obado 6470
      return clipPos(this, Pos(lineNo, ch))
14283 obado 6471
    },
6472
    indexFromPos: function (coords) {
6473
      coords = clipPos(this, coords);
6474
      var index = coords.ch;
6475
      if (coords.line < this.first || coords.ch < 0) { return 0 }
6476
      var sepSize = this.lineSeparator().length;
6477
      this.iter(this.first, coords.line, function (line) { // iter aborts when callback returns a truthy value
6478
        index += line.text.length + sepSize;
6479
      });
6480
      return index
6481
    },
6482
 
6483
    copy: function(copyHistory) {
6484
      var doc = new Doc(getLines(this, this.first, this.first + this.size),
6485
                        this.modeOption, this.first, this.lineSep, this.direction);
6486
      doc.scrollTop = this.scrollTop; doc.scrollLeft = this.scrollLeft;
6487
      doc.sel = this.sel;
6488
      doc.extend = false;
6489
      if (copyHistory) {
6490
        doc.history.undoDepth = this.history.undoDepth;
6491
        doc.setHistory(this.getHistory());
6492
      }
6493
      return doc
6494
    },
6495
 
6496
    linkedDoc: function(options) {
6497
      if (!options) { options = {}; }
6498
      var from = this.first, to = this.first + this.size;
6499
      if (options.from != null && options.from > from) { from = options.from; }
6500
      if (options.to != null && options.to < to) { to = options.to; }
6501
      var copy = new Doc(getLines(this, from, to), options.mode || this.modeOption, from, this.lineSep, this.direction);
6502
      if (options.sharedHist) { copy.history = this.history
6503
      ; }(this.linked || (this.linked = [])).push({doc: copy, sharedHist: options.sharedHist});
6504
      copy.linked = [{doc: this, isParent: true, sharedHist: options.sharedHist}];
6505
      copySharedMarkers(copy, findSharedMarkers(this));
6506
      return copy
6507
    },
6508
    unlinkDoc: function(other) {
6509
      if (other instanceof CodeMirror) { other = other.doc; }
6510
      if (this.linked) { for (var i = 0; i < this.linked.length; ++i) {
15152 obado 6511
        var link = this.linked[i];
14283 obado 6512
        if (link.doc != other) { continue }
15152 obado 6513
        this.linked.splice(i, 1);
6514
        other.unlinkDoc(this);
6515
        detachSharedMarkers(findSharedMarkers(this));
14283 obado 6516
        break
6517
      } }
6518
      // If the histories were shared, split them again
6519
      if (other.history == this.history) {
6520
        var splitIds = [other.id];
6521
        linkedDocs(other, function (doc) { return splitIds.push(doc.id); }, true);
6522
        other.history = new History(null);
6523
        other.history.done = copyHistoryArray(this.history.done, splitIds);
6524
        other.history.undone = copyHistoryArray(this.history.undone, splitIds);
6525
      }
6526
    },
6527
    iterLinkedDocs: function(f) {linkedDocs(this, f);},
6528
 
6529
    getMode: function() {return this.mode},
6530
    getEditor: function() {return this.cm},
6531
 
6532
    splitLines: function(str) {
6533
      if (this.lineSep) { return str.split(this.lineSep) }
6534
      return splitLinesAuto(str)
6535
    },
6536
    lineSeparator: function() { return this.lineSep || "\n" },
6537
 
6538
    setDirection: docMethodOp(function (dir) {
6539
      if (dir != "rtl") { dir = "ltr"; }
6540
      if (dir == this.direction) { return }
6541
      this.direction = dir;
6542
      this.iter(function (line) { return line.order = null; });
6543
      if (this.cm) { directionChanged(this.cm); }
6544
    })
6545
  });
6546
 
6547
  // Public alias.
6548
  Doc.prototype.eachLine = Doc.prototype.iter;
6549
 
6550
  // Kludge to work around strange IE behavior where it'll sometimes
6551
  // re-fire a series of drag-related events right after the drop (#1551)
6552
  var lastDrop = 0;
6553
 
6554
  function onDrop(e) {
6555
    var cm = this;
6556
    clearDragCursor(cm);
6557
    if (signalDOMEvent(cm, e) || eventInWidget(cm.display, e))
6558
      { return }
6559
    e_preventDefault(e);
6560
    if (ie) { lastDrop = +new Date; }
6561
    var pos = posFromMouse(cm, e, true), files = e.dataTransfer.files;
6562
    if (!pos || cm.isReadOnly()) { return }
6563
    // Might be a file drop, in which case we simply extract the text
6564
    // and insert it.
6565
    if (files && files.length && window.FileReader && window.File) {
6566
      var n = files.length, text = Array(n), read = 0;
15152 obado 6567
      var markAsReadAndPasteIfAllFilesAreRead = function () {
6568
        if (++read == n) {
6569
          operation(cm, function () {
14283 obado 6570
            pos = clipPos(cm.doc, pos);
6571
            var change = {from: pos, to: pos,
15152 obado 6572
                          text: cm.doc.splitLines(
6573
                              text.filter(function (t) { return t != null; }).join(cm.doc.lineSeparator())),
14283 obado 6574
                          origin: "paste"};
6575
            makeChange(cm.doc, change);
15152 obado 6576
            setSelectionReplaceHistory(cm.doc, simpleSelection(clipPos(cm.doc, pos), clipPos(cm.doc, changeEnd(change))));
6577
          })();
6578
        }
6579
      };
6580
      var readTextFromFile = function (file, i) {
6581
        if (cm.options.allowDropFileTypes &&
6582
            indexOf(cm.options.allowDropFileTypes, file.type) == -1) {
6583
          markAsReadAndPasteIfAllFilesAreRead();
6584
          return
6585
        }
6586
        var reader = new FileReader;
6587
        reader.onerror = function () { return markAsReadAndPasteIfAllFilesAreRead(); };
6588
        reader.onload = function () {
6589
          var content = reader.result;
6590
          if (/[\x00-\x08\x0e-\x1f]{2}/.test(content)) {
6591
            markAsReadAndPasteIfAllFilesAreRead();
6592
            return
14283 obado 6593
          }
15152 obado 6594
          text[i] = content;
6595
          markAsReadAndPasteIfAllFilesAreRead();
6596
        };
14283 obado 6597
        reader.readAsText(file);
6598
      };
15152 obado 6599
      for (var i = 0; i < files.length; i++) { readTextFromFile(files[i], i); }
14283 obado 6600
    } else { // Normal drop
6601
      // Don't do a replace if the drop happened inside of the selected text.
6602
      if (cm.state.draggingText && cm.doc.sel.contains(pos) > -1) {
6603
        cm.state.draggingText(e);
6604
        // Ensure the editor is re-focused
6605
        setTimeout(function () { return cm.display.input.focus(); }, 20);
6606
        return
6607
      }
6608
      try {
6609
        var text$1 = e.dataTransfer.getData("Text");
6610
        if (text$1) {
6611
          var selected;
6612
          if (cm.state.draggingText && !cm.state.draggingText.copy)
6613
            { selected = cm.listSelections(); }
6614
          setSelectionNoUndo(cm.doc, simpleSelection(pos, pos));
6615
          if (selected) { for (var i$1 = 0; i$1 < selected.length; ++i$1)
6616
            { replaceRange(cm.doc, "", selected[i$1].anchor, selected[i$1].head, "drag"); } }
6617
          cm.replaceSelection(text$1, "around", "paste");
6618
          cm.display.input.focus();
6619
        }
6620
      }
15332 obado 6621
      catch(e$1){}
14283 obado 6622
    }
6623
  }
6624
 
6625
  function onDragStart(cm, e) {
6626
    if (ie && (!cm.state.draggingText || +new Date - lastDrop < 100)) { e_stop(e); return }
6627
    if (signalDOMEvent(cm, e) || eventInWidget(cm.display, e)) { return }
6628
 
6629
    e.dataTransfer.setData("Text", cm.getSelection());
6630
    e.dataTransfer.effectAllowed = "copyMove";
6631
 
6632
    // Use dummy image instead of default browsers image.
6633
    // Recent Safari (~6.0.2) have a tendency to segfault when this happens, so we don't do it there.
6634
    if (e.dataTransfer.setDragImage && !safari) {
6635
      var img = elt("img", null, null, "position: fixed; left: 0; top: 0;");
6636
      img.src = "data:image/gif;base64,R0lGODlhAQABAAAAACH5BAEKAAEALAAAAAABAAEAAAICTAEAOw==";
6637
      if (presto) {
6638
        img.width = img.height = 1;
6639
        cm.display.wrapper.appendChild(img);
6640
        // Force a relayout, or Opera won't use our image for some obscure reason
6641
        img._top = img.offsetTop;
6642
      }
6643
      e.dataTransfer.setDragImage(img, 0, 0);
6644
      if (presto) { img.parentNode.removeChild(img); }
6645
    }
6646
  }
6647
 
6648
  function onDragOver(cm, e) {
6649
    var pos = posFromMouse(cm, e);
6650
    if (!pos) { return }
6651
    var frag = document.createDocumentFragment();
6652
    drawSelectionCursor(cm, pos, frag);
6653
    if (!cm.display.dragCursor) {
6654
      cm.display.dragCursor = elt("div", null, "CodeMirror-cursors CodeMirror-dragcursors");
6655
      cm.display.lineSpace.insertBefore(cm.display.dragCursor, cm.display.cursorDiv);
6656
    }
6657
    removeChildrenAndAdd(cm.display.dragCursor, frag);
6658
  }
6659
 
6660
  function clearDragCursor(cm) {
6661
    if (cm.display.dragCursor) {
6662
      cm.display.lineSpace.removeChild(cm.display.dragCursor);
6663
      cm.display.dragCursor = null;
6664
    }
6665
  }
6666
 
6667
  // These must be handled carefully, because naively registering a
6668
  // handler for each editor will cause the editors to never be
6669
  // garbage collected.
6670
 
6671
  function forEachCodeMirror(f) {
6672
    if (!document.getElementsByClassName) { return }
6673
    var byClass = document.getElementsByClassName("CodeMirror"), editors = [];
6674
    for (var i = 0; i < byClass.length; i++) {
6675
      var cm = byClass[i].CodeMirror;
6676
      if (cm) { editors.push(cm); }
6677
    }
6678
    if (editors.length) { editors[0].operation(function () {
6679
      for (var i = 0; i < editors.length; i++) { f(editors[i]); }
6680
    }); }
6681
  }
6682
 
6683
  var globalsRegistered = false;
6684
  function ensureGlobalHandlers() {
6685
    if (globalsRegistered) { return }
6686
    registerGlobalHandlers();
6687
    globalsRegistered = true;
6688
  }
6689
  function registerGlobalHandlers() {
6690
    // When the window resizes, we need to refresh active editors.
6691
    var resizeTimer;
6692
    on(window, "resize", function () {
6693
      if (resizeTimer == null) { resizeTimer = setTimeout(function () {
6694
        resizeTimer = null;
6695
        forEachCodeMirror(onResize);
6696
      }, 100); }
6697
    });
6698
    // When the window loses focus, we want to show the editor as blurred
6699
    on(window, "blur", function () { return forEachCodeMirror(onBlur); });
6700
  }
6701
  // Called when the window resizes
6702
  function onResize(cm) {
6703
    var d = cm.display;
6704
    // Might be a text scaling operation, clear size caches.
6705
    d.cachedCharWidth = d.cachedTextHeight = d.cachedPaddingH = null;
6706
    d.scrollbarsClipped = false;
6707
    cm.setSize();
6708
  }
6709
 
6710
  var keyNames = {
6711
    3: "Pause", 8: "Backspace", 9: "Tab", 13: "Enter", 16: "Shift", 17: "Ctrl", 18: "Alt",
6712
    19: "Pause", 20: "CapsLock", 27: "Esc", 32: "Space", 33: "PageUp", 34: "PageDown", 35: "End",
6713
    36: "Home", 37: "Left", 38: "Up", 39: "Right", 40: "Down", 44: "PrintScrn", 45: "Insert",
6714
    46: "Delete", 59: ";", 61: "=", 91: "Mod", 92: "Mod", 93: "Mod",
6715
    106: "*", 107: "=", 109: "-", 110: ".", 111: "/", 145: "ScrollLock",
6716
    173: "-", 186: ";", 187: "=", 188: ",", 189: "-", 190: ".", 191: "/", 192: "`", 219: "[", 220: "\\",
15332 obado 6717
    221: "]", 222: "'", 224: "Mod", 63232: "Up", 63233: "Down", 63234: "Left", 63235: "Right", 63272: "Delete",
14283 obado 6718
    63273: "Home", 63275: "End", 63276: "PageUp", 63277: "PageDown", 63302: "Insert"
6719
  };
6720
 
6721
  // Number keys
6722
  for (var i = 0; i < 10; i++) { keyNames[i + 48] = keyNames[i + 96] = String(i); }
6723
  // Alphabetic keys
6724
  for (var i$1 = 65; i$1 <= 90; i$1++) { keyNames[i$1] = String.fromCharCode(i$1); }
6725
  // Function keys
6726
  for (var i$2 = 1; i$2 <= 12; i$2++) { keyNames[i$2 + 111] = keyNames[i$2 + 63235] = "F" + i$2; }
6727
 
6728
  var keyMap = {};
6729
 
6730
  keyMap.basic = {
6731
    "Left": "goCharLeft", "Right": "goCharRight", "Up": "goLineUp", "Down": "goLineDown",
6732
    "End": "goLineEnd", "Home": "goLineStartSmart", "PageUp": "goPageUp", "PageDown": "goPageDown",
6733
    "Delete": "delCharAfter", "Backspace": "delCharBefore", "Shift-Backspace": "delCharBefore",
6734
    "Tab": "defaultTab", "Shift-Tab": "indentAuto",
6735
    "Enter": "newlineAndIndent", "Insert": "toggleOverwrite",
6736
    "Esc": "singleSelection"
6737
  };
6738
  // Note that the save and find-related commands aren't defined by
6739
  // default. User code or addons can define them. Unknown commands
6740
  // are simply ignored.
6741
  keyMap.pcDefault = {
6742
    "Ctrl-A": "selectAll", "Ctrl-D": "deleteLine", "Ctrl-Z": "undo", "Shift-Ctrl-Z": "redo", "Ctrl-Y": "redo",
6743
    "Ctrl-Home": "goDocStart", "Ctrl-End": "goDocEnd", "Ctrl-Up": "goLineUp", "Ctrl-Down": "goLineDown",
6744
    "Ctrl-Left": "goGroupLeft", "Ctrl-Right": "goGroupRight", "Alt-Left": "goLineStart", "Alt-Right": "goLineEnd",
6745
    "Ctrl-Backspace": "delGroupBefore", "Ctrl-Delete": "delGroupAfter", "Ctrl-S": "save", "Ctrl-F": "find",
6746
    "Ctrl-G": "findNext", "Shift-Ctrl-G": "findPrev", "Shift-Ctrl-F": "replace", "Shift-Ctrl-R": "replaceAll",
6747
    "Ctrl-[": "indentLess", "Ctrl-]": "indentMore",
6748
    "Ctrl-U": "undoSelection", "Shift-Ctrl-U": "redoSelection", "Alt-U": "redoSelection",
6749
    "fallthrough": "basic"
6750
  };
6751
  // Very basic readline/emacs-style bindings, which are standard on Mac.
6752
  keyMap.emacsy = {
6753
    "Ctrl-F": "goCharRight", "Ctrl-B": "goCharLeft", "Ctrl-P": "goLineUp", "Ctrl-N": "goLineDown",
16493 obado 6754
    "Ctrl-A": "goLineStart", "Ctrl-E": "goLineEnd", "Ctrl-V": "goPageDown", "Shift-Ctrl-V": "goPageUp",
6755
    "Ctrl-D": "delCharAfter", "Ctrl-H": "delCharBefore", "Alt-Backspace": "delWordBefore", "Ctrl-K": "killLine",
6756
    "Ctrl-T": "transposeChars", "Ctrl-O": "openLine"
14283 obado 6757
  };
6758
  keyMap.macDefault = {
6759
    "Cmd-A": "selectAll", "Cmd-D": "deleteLine", "Cmd-Z": "undo", "Shift-Cmd-Z": "redo", "Cmd-Y": "redo",
6760
    "Cmd-Home": "goDocStart", "Cmd-Up": "goDocStart", "Cmd-End": "goDocEnd", "Cmd-Down": "goDocEnd", "Alt-Left": "goGroupLeft",
6761
    "Alt-Right": "goGroupRight", "Cmd-Left": "goLineLeft", "Cmd-Right": "goLineRight", "Alt-Backspace": "delGroupBefore",
6762
    "Ctrl-Alt-Backspace": "delGroupAfter", "Alt-Delete": "delGroupAfter", "Cmd-S": "save", "Cmd-F": "find",
6763
    "Cmd-G": "findNext", "Shift-Cmd-G": "findPrev", "Cmd-Alt-F": "replace", "Shift-Cmd-Alt-F": "replaceAll",
6764
    "Cmd-[": "indentLess", "Cmd-]": "indentMore", "Cmd-Backspace": "delWrappedLineLeft", "Cmd-Delete": "delWrappedLineRight",
6765
    "Cmd-U": "undoSelection", "Shift-Cmd-U": "redoSelection", "Ctrl-Up": "goDocStart", "Ctrl-Down": "goDocEnd",
6766
    "fallthrough": ["basic", "emacsy"]
6767
  };
6768
  keyMap["default"] = mac ? keyMap.macDefault : keyMap.pcDefault;
6769
 
6770
  // KEYMAP DISPATCH
6771
 
6772
  function normalizeKeyName(name) {
6773
    var parts = name.split(/-(?!$)/);
6774
    name = parts[parts.length - 1];
6775
    var alt, ctrl, shift, cmd;
6776
    for (var i = 0; i < parts.length - 1; i++) {
6777
      var mod = parts[i];
6778
      if (/^(cmd|meta|m)$/i.test(mod)) { cmd = true; }
6779
      else if (/^a(lt)?$/i.test(mod)) { alt = true; }
6780
      else if (/^(c|ctrl|control)$/i.test(mod)) { ctrl = true; }
6781
      else if (/^s(hift)?$/i.test(mod)) { shift = true; }
6782
      else { throw new Error("Unrecognized modifier name: " + mod) }
6783
    }
6784
    if (alt) { name = "Alt-" + name; }
6785
    if (ctrl) { name = "Ctrl-" + name; }
6786
    if (cmd) { name = "Cmd-" + name; }
6787
    if (shift) { name = "Shift-" + name; }
6788
    return name
6789
  }
6790
 
6791
  // This is a kludge to keep keymaps mostly working as raw objects
6792
  // (backwards compatibility) while at the same time support features
6793
  // like normalization and multi-stroke key bindings. It compiles a
6794
  // new normalized keymap, and then updates the old object to reflect
6795
  // this.
6796
  function normalizeKeyMap(keymap) {
6797
    var copy = {};
6798
    for (var keyname in keymap) { if (keymap.hasOwnProperty(keyname)) {
6799
      var value = keymap[keyname];
6800
      if (/^(name|fallthrough|(de|at)tach)$/.test(keyname)) { continue }
6801
      if (value == "...") { delete keymap[keyname]; continue }
6802
 
6803
      var keys = map(keyname.split(" "), normalizeKeyName);
6804
      for (var i = 0; i < keys.length; i++) {
6805
        var val = (void 0), name = (void 0);
6806
        if (i == keys.length - 1) {
6807
          name = keys.join(" ");
6808
          val = value;
6809
        } else {
6810
          name = keys.slice(0, i + 1).join(" ");
6811
          val = "...";
6812
        }
6813
        var prev = copy[name];
6814
        if (!prev) { copy[name] = val; }
6815
        else if (prev != val) { throw new Error("Inconsistent bindings for " + name) }
6816
      }
6817
      delete keymap[keyname];
6818
    } }
6819
    for (var prop in copy) { keymap[prop] = copy[prop]; }
6820
    return keymap
6821
  }
6822
 
15152 obado 6823
  function lookupKey(key, map, handle, context) {
6824
    map = getKeyMap(map);
6825
    var found = map.call ? map.call(key, context) : map[key];
14283 obado 6826
    if (found === false) { return "nothing" }
6827
    if (found === "...") { return "multi" }
6828
    if (found != null && handle(found)) { return "handled" }
6829
 
15152 obado 6830
    if (map.fallthrough) {
6831
      if (Object.prototype.toString.call(map.fallthrough) != "[object Array]")
6832
        { return lookupKey(key, map.fallthrough, handle, context) }
6833
      for (var i = 0; i < map.fallthrough.length; i++) {
6834
        var result = lookupKey(key, map.fallthrough[i], handle, context);
14283 obado 6835
        if (result) { return result }
6836
      }
6837
    }
6838
  }
6839
 
6840
  // Modifier key presses don't count as 'real' key presses for the
6841
  // purpose of keymap fallthrough.
6842
  function isModifierKey(value) {
6843
    var name = typeof value == "string" ? value : keyNames[value.keyCode];
6844
    return name == "Ctrl" || name == "Alt" || name == "Shift" || name == "Mod"
6845
  }
6846
 
6847
  function addModifierNames(name, event, noShift) {
6848
    var base = name;
6849
    if (event.altKey && base != "Alt") { name = "Alt-" + name; }
6850
    if ((flipCtrlCmd ? event.metaKey : event.ctrlKey) && base != "Ctrl") { name = "Ctrl-" + name; }
15332 obado 6851
    if ((flipCtrlCmd ? event.ctrlKey : event.metaKey) && base != "Mod") { name = "Cmd-" + name; }
14283 obado 6852
    if (!noShift && event.shiftKey && base != "Shift") { name = "Shift-" + name; }
6853
    return name
6854
  }
6855
 
6856
  // Look up the name of a key as indicated by an event object.
6857
  function keyName(event, noShift) {
6858
    if (presto && event.keyCode == 34 && event["char"]) { return false }
6859
    var name = keyNames[event.keyCode];
6860
    if (name == null || event.altGraphKey) { return false }
6861
    // Ctrl-ScrollLock has keyCode 3, same as Ctrl-Pause,
6862
    // so we'll use event.code when available (Chrome 48+, FF 38+, Safari 10.1+)
6863
    if (event.keyCode == 3 && event.code) { name = event.code; }
6864
    return addModifierNames(name, event, noShift)
6865
  }
6866
 
6867
  function getKeyMap(val) {
6868
    return typeof val == "string" ? keyMap[val] : val
6869
  }
6870
 
6871
  // Helper for deleting text near the selection(s), used to implement
6872
  // backspace, delete, and similar functionality.
6873
  function deleteNearSelection(cm, compute) {
6874
    var ranges = cm.doc.sel.ranges, kill = [];
6875
    // Build up a set of ranges to kill first, merging overlapping
6876
    // ranges.
6877
    for (var i = 0; i < ranges.length; i++) {
6878
      var toKill = compute(ranges[i]);
6879
      while (kill.length && cmp(toKill.from, lst(kill).to) <= 0) {
6880
        var replaced = kill.pop();
6881
        if (cmp(replaced.from, toKill.from) < 0) {
6882
          toKill.from = replaced.from;
6883
          break
6884
        }
6885
      }
6886
      kill.push(toKill);
6887
    }
6888
    // Next, remove those actual ranges.
6889
    runInOp(cm, function () {
6890
      for (var i = kill.length - 1; i >= 0; i--)
6891
        { replaceRange(cm.doc, "", kill[i].from, kill[i].to, "+delete"); }
6892
      ensureCursorVisible(cm);
6893
    });
6894
  }
6895
 
6896
  function moveCharLogically(line, ch, dir) {
6897
    var target = skipExtendingChars(line.text, ch + dir, dir);
6898
    return target < 0 || target > line.text.length ? null : target
6899
  }
6900
 
6901
  function moveLogically(line, start, dir) {
6902
    var ch = moveCharLogically(line, start.ch, dir);
6903
    return ch == null ? null : new Pos(start.line, ch, dir < 0 ? "after" : "before")
6904
  }
6905
 
6906
  function endOfLine(visually, cm, lineObj, lineNo, dir) {
6907
    if (visually) {
15152 obado 6908
      if (cm.doc.direction == "rtl") { dir = -dir; }
14283 obado 6909
      var order = getOrder(lineObj, cm.doc.direction);
6910
      if (order) {
6911
        var part = dir < 0 ? lst(order) : order[0];
6912
        var moveInStorageOrder = (dir < 0) == (part.level == 1);
6913
        var sticky = moveInStorageOrder ? "after" : "before";
6914
        var ch;
6915
        // With a wrapped rtl chunk (possibly spanning multiple bidi parts),
6916
        // it could be that the last bidi part is not on the last visual line,
6917
        // since visual lines contain content order-consecutive chunks.
6918
        // Thus, in rtl, we are looking for the first (content-order) character
6919
        // in the rtl chunk that is on the last line (that is, the same line
6920
        // as the last (content-order) character).
6921
        if (part.level > 0 || cm.doc.direction == "rtl") {
6922
          var prep = prepareMeasureForLine(cm, lineObj);
6923
          ch = dir < 0 ? lineObj.text.length - 1 : 0;
6924
          var targetTop = measureCharPrepared(cm, prep, ch).top;
6925
          ch = findFirst(function (ch) { return measureCharPrepared(cm, prep, ch).top == targetTop; }, (dir < 0) == (part.level == 1) ? part.from : part.to - 1, ch);
6926
          if (sticky == "before") { ch = moveCharLogically(lineObj, ch, 1); }
6927
        } else { ch = dir < 0 ? part.to : part.from; }
6928
        return new Pos(lineNo, ch, sticky)
6929
      }
6930
    }
6931
    return new Pos(lineNo, dir < 0 ? lineObj.text.length : 0, dir < 0 ? "before" : "after")
6932
  }
6933
 
6934
  function moveVisually(cm, line, start, dir) {
6935
    var bidi = getOrder(line, cm.doc.direction);
6936
    if (!bidi) { return moveLogically(line, start, dir) }
6937
    if (start.ch >= line.text.length) {
6938
      start.ch = line.text.length;
6939
      start.sticky = "before";
6940
    } else if (start.ch <= 0) {
6941
      start.ch = 0;
6942
      start.sticky = "after";
6943
    }
6944
    var partPos = getBidiPartAt(bidi, start.ch, start.sticky), part = bidi[partPos];
6945
    if (cm.doc.direction == "ltr" && part.level % 2 == 0 && (dir > 0 ? part.to > start.ch : part.from < start.ch)) {
6946
      // Case 1: We move within an ltr part in an ltr editor. Even with wrapped lines,
6947
      // nothing interesting happens.
6948
      return moveLogically(line, start, dir)
6949
    }
6950
 
6951
    var mv = function (pos, dir) { return moveCharLogically(line, pos instanceof Pos ? pos.ch : pos, dir); };
6952
    var prep;
6953
    var getWrappedLineExtent = function (ch) {
6954
      if (!cm.options.lineWrapping) { return {begin: 0, end: line.text.length} }
6955
      prep = prep || prepareMeasureForLine(cm, line);
6956
      return wrappedLineExtentChar(cm, line, prep, ch)
6957
    };
6958
    var wrappedLineExtent = getWrappedLineExtent(start.sticky == "before" ? mv(start, -1) : start.ch);
6959
 
6960
    if (cm.doc.direction == "rtl" || part.level == 1) {
6961
      var moveInStorageOrder = (part.level == 1) == (dir < 0);
6962
      var ch = mv(start, moveInStorageOrder ? 1 : -1);
6963
      if (ch != null && (!moveInStorageOrder ? ch >= part.from && ch >= wrappedLineExtent.begin : ch <= part.to && ch <= wrappedLineExtent.end)) {
6964
        // Case 2: We move within an rtl part or in an rtl editor on the same visual line
6965
        var sticky = moveInStorageOrder ? "before" : "after";
6966
        return new Pos(start.line, ch, sticky)
6967
      }
6968
    }
6969
 
6970
    // Case 3: Could not move within this bidi part in this visual line, so leave
6971
    // the current bidi part
6972
 
6973
    var searchInVisualLine = function (partPos, dir, wrappedLineExtent) {
6974
      var getRes = function (ch, moveInStorageOrder) { return moveInStorageOrder
6975
        ? new Pos(start.line, mv(ch, 1), "before")
6976
        : new Pos(start.line, ch, "after"); };
6977
 
6978
      for (; partPos >= 0 && partPos < bidi.length; partPos += dir) {
6979
        var part = bidi[partPos];
6980
        var moveInStorageOrder = (dir > 0) == (part.level != 1);
6981
        var ch = moveInStorageOrder ? wrappedLineExtent.begin : mv(wrappedLineExtent.end, -1);
6982
        if (part.from <= ch && ch < part.to) { return getRes(ch, moveInStorageOrder) }
6983
        ch = moveInStorageOrder ? part.from : mv(part.to, -1);
6984
        if (wrappedLineExtent.begin <= ch && ch < wrappedLineExtent.end) { return getRes(ch, moveInStorageOrder) }
6985
      }
6986
    };
6987
 
6988
    // Case 3a: Look for other bidi parts on the same visual line
6989
    var res = searchInVisualLine(partPos + dir, dir, wrappedLineExtent);
6990
    if (res) { return res }
6991
 
6992
    // Case 3b: Look for other bidi parts on the next visual line
6993
    var nextCh = dir > 0 ? wrappedLineExtent.end : mv(wrappedLineExtent.begin, -1);
6994
    if (nextCh != null && !(dir > 0 && nextCh == line.text.length)) {
6995
      res = searchInVisualLine(dir > 0 ? 0 : bidi.length - 1, dir, getWrappedLineExtent(nextCh));
6996
      if (res) { return res }
6997
    }
6998
 
6999
    // Case 4: Nowhere to move
7000
    return null
7001
  }
7002
 
7003
  // Commands are parameter-less actions that can be performed on an
7004
  // editor, mostly used for keybindings.
7005
  var commands = {
7006
    selectAll: selectAll,
7007
    singleSelection: function (cm) { return cm.setSelection(cm.getCursor("anchor"), cm.getCursor("head"), sel_dontScroll); },
7008
    killLine: function (cm) { return deleteNearSelection(cm, function (range) {
7009
      if (range.empty()) {
7010
        var len = getLine(cm.doc, range.head.line).text.length;
7011
        if (range.head.ch == len && range.head.line < cm.lastLine())
7012
          { return {from: range.head, to: Pos(range.head.line + 1, 0)} }
7013
        else
7014
          { return {from: range.head, to: Pos(range.head.line, len)} }
7015
      } else {
7016
        return {from: range.from(), to: range.to()}
7017
      }
7018
    }); },
7019
    deleteLine: function (cm) { return deleteNearSelection(cm, function (range) { return ({
7020
      from: Pos(range.from().line, 0),
7021
      to: clipPos(cm.doc, Pos(range.to().line + 1, 0))
7022
    }); }); },
7023
    delLineLeft: function (cm) { return deleteNearSelection(cm, function (range) { return ({
7024
      from: Pos(range.from().line, 0), to: range.from()
7025
    }); }); },
7026
    delWrappedLineLeft: function (cm) { return deleteNearSelection(cm, function (range) {
7027
      var top = cm.charCoords(range.head, "div").top + 5;
7028
      var leftPos = cm.coordsChar({left: 0, top: top}, "div");
7029
      return {from: leftPos, to: range.from()}
7030
    }); },
7031
    delWrappedLineRight: function (cm) { return deleteNearSelection(cm, function (range) {
7032
      var top = cm.charCoords(range.head, "div").top + 5;
7033
      var rightPos = cm.coordsChar({left: cm.display.lineDiv.offsetWidth + 100, top: top}, "div");
7034
      return {from: range.from(), to: rightPos }
7035
    }); },
7036
    undo: function (cm) { return cm.undo(); },
7037
    redo: function (cm) { return cm.redo(); },
7038
    undoSelection: function (cm) { return cm.undoSelection(); },
7039
    redoSelection: function (cm) { return cm.redoSelection(); },
7040
    goDocStart: function (cm) { return cm.extendSelection(Pos(cm.firstLine(), 0)); },
7041
    goDocEnd: function (cm) { return cm.extendSelection(Pos(cm.lastLine())); },
7042
    goLineStart: function (cm) { return cm.extendSelectionsBy(function (range) { return lineStart(cm, range.head.line); },
7043
      {origin: "+move", bias: 1}
7044
    ); },
7045
    goLineStartSmart: function (cm) { return cm.extendSelectionsBy(function (range) { return lineStartSmart(cm, range.head); },
7046
      {origin: "+move", bias: 1}
7047
    ); },
7048
    goLineEnd: function (cm) { return cm.extendSelectionsBy(function (range) { return lineEnd(cm, range.head.line); },
7049
      {origin: "+move", bias: -1}
7050
    ); },
7051
    goLineRight: function (cm) { return cm.extendSelectionsBy(function (range) {
7052
      var top = cm.cursorCoords(range.head, "div").top + 5;
7053
      return cm.coordsChar({left: cm.display.lineDiv.offsetWidth + 100, top: top}, "div")
7054
    }, sel_move); },
7055
    goLineLeft: function (cm) { return cm.extendSelectionsBy(function (range) {
7056
      var top = cm.cursorCoords(range.head, "div").top + 5;
7057
      return cm.coordsChar({left: 0, top: top}, "div")
7058
    }, sel_move); },
7059
    goLineLeftSmart: function (cm) { return cm.extendSelectionsBy(function (range) {
7060
      var top = cm.cursorCoords(range.head, "div").top + 5;
7061
      var pos = cm.coordsChar({left: 0, top: top}, "div");
7062
      if (pos.ch < cm.getLine(pos.line).search(/\S/)) { return lineStartSmart(cm, range.head) }
7063
      return pos
7064
    }, sel_move); },
7065
    goLineUp: function (cm) { return cm.moveV(-1, "line"); },
7066
    goLineDown: function (cm) { return cm.moveV(1, "line"); },
7067
    goPageUp: function (cm) { return cm.moveV(-1, "page"); },
7068
    goPageDown: function (cm) { return cm.moveV(1, "page"); },
7069
    goCharLeft: function (cm) { return cm.moveH(-1, "char"); },
7070
    goCharRight: function (cm) { return cm.moveH(1, "char"); },
7071
    goColumnLeft: function (cm) { return cm.moveH(-1, "column"); },
7072
    goColumnRight: function (cm) { return cm.moveH(1, "column"); },
7073
    goWordLeft: function (cm) { return cm.moveH(-1, "word"); },
7074
    goGroupRight: function (cm) { return cm.moveH(1, "group"); },
7075
    goGroupLeft: function (cm) { return cm.moveH(-1, "group"); },
7076
    goWordRight: function (cm) { return cm.moveH(1, "word"); },
16493 obado 7077
    delCharBefore: function (cm) { return cm.deleteH(-1, "codepoint"); },
14283 obado 7078
    delCharAfter: function (cm) { return cm.deleteH(1, "char"); },
7079
    delWordBefore: function (cm) { return cm.deleteH(-1, "word"); },
7080
    delWordAfter: function (cm) { return cm.deleteH(1, "word"); },
7081
    delGroupBefore: function (cm) { return cm.deleteH(-1, "group"); },
7082
    delGroupAfter: function (cm) { return cm.deleteH(1, "group"); },
7083
    indentAuto: function (cm) { return cm.indentSelection("smart"); },
7084
    indentMore: function (cm) { return cm.indentSelection("add"); },
7085
    indentLess: function (cm) { return cm.indentSelection("subtract"); },
7086
    insertTab: function (cm) { return cm.replaceSelection("\t"); },
7087
    insertSoftTab: function (cm) {
7088
      var spaces = [], ranges = cm.listSelections(), tabSize = cm.options.tabSize;
7089
      for (var i = 0; i < ranges.length; i++) {
7090
        var pos = ranges[i].from();
7091
        var col = countColumn(cm.getLine(pos.line), pos.ch, tabSize);
7092
        spaces.push(spaceStr(tabSize - col % tabSize));
7093
      }
7094
      cm.replaceSelections(spaces);
7095
    },
7096
    defaultTab: function (cm) {
7097
      if (cm.somethingSelected()) { cm.indentSelection("add"); }
7098
      else { cm.execCommand("insertTab"); }
7099
    },
7100
    // Swap the two chars left and right of each selection's head.
7101
    // Move cursor behind the two swapped characters afterwards.
7102
    //
7103
    // Doesn't consider line feeds a character.
7104
    // Doesn't scan more than one line above to find a character.
7105
    // Doesn't do anything on an empty line.
7106
    // Doesn't do anything with non-empty selections.
7107
    transposeChars: function (cm) { return runInOp(cm, function () {
7108
      var ranges = cm.listSelections(), newSel = [];
7109
      for (var i = 0; i < ranges.length; i++) {
7110
        if (!ranges[i].empty()) { continue }
7111
        var cur = ranges[i].head, line = getLine(cm.doc, cur.line).text;
7112
        if (line) {
7113
          if (cur.ch == line.length) { cur = new Pos(cur.line, cur.ch - 1); }
7114
          if (cur.ch > 0) {
7115
            cur = new Pos(cur.line, cur.ch + 1);
7116
            cm.replaceRange(line.charAt(cur.ch - 1) + line.charAt(cur.ch - 2),
7117
                            Pos(cur.line, cur.ch - 2), cur, "+transpose");
7118
          } else if (cur.line > cm.doc.first) {
7119
            var prev = getLine(cm.doc, cur.line - 1).text;
7120
            if (prev) {
7121
              cur = new Pos(cur.line, 1);
7122
              cm.replaceRange(line.charAt(0) + cm.doc.lineSeparator() +
7123
                              prev.charAt(prev.length - 1),
7124
                              Pos(cur.line - 1, prev.length - 1), cur, "+transpose");
7125
            }
7126
          }
7127
        }
7128
        newSel.push(new Range(cur, cur));
7129
      }
7130
      cm.setSelections(newSel);
7131
    }); },
7132
    newlineAndIndent: function (cm) { return runInOp(cm, function () {
7133
      var sels = cm.listSelections();
7134
      for (var i = sels.length - 1; i >= 0; i--)
7135
        { cm.replaceRange(cm.doc.lineSeparator(), sels[i].anchor, sels[i].head, "+input"); }
7136
      sels = cm.listSelections();
7137
      for (var i$1 = 0; i$1 < sels.length; i$1++)
7138
        { cm.indentLine(sels[i$1].from().line, null, true); }
7139
      ensureCursorVisible(cm);
7140
    }); },
7141
    openLine: function (cm) { return cm.replaceSelection("\n", "start"); },
7142
    toggleOverwrite: function (cm) { return cm.toggleOverwrite(); }
7143
  };
7144
 
7145
 
7146
  function lineStart(cm, lineN) {
7147
    var line = getLine(cm.doc, lineN);
7148
    var visual = visualLine(line);
7149
    if (visual != line) { lineN = lineNo(visual); }
7150
    return endOfLine(true, cm, visual, lineN, 1)
7151
  }
7152
  function lineEnd(cm, lineN) {
7153
    var line = getLine(cm.doc, lineN);
7154
    var visual = visualLineEnd(line);
7155
    if (visual != line) { lineN = lineNo(visual); }
7156
    return endOfLine(true, cm, line, lineN, -1)
7157
  }
7158
  function lineStartSmart(cm, pos) {
7159
    var start = lineStart(cm, pos.line);
7160
    var line = getLine(cm.doc, start.line);
7161
    var order = getOrder(line, cm.doc.direction);
7162
    if (!order || order[0].level == 0) {
15152 obado 7163
      var firstNonWS = Math.max(start.ch, line.text.search(/\S/));
14283 obado 7164
      var inWS = pos.line == start.line && pos.ch <= firstNonWS && pos.ch;
7165
      return Pos(start.line, inWS ? 0 : firstNonWS, start.sticky)
7166
    }
7167
    return start
7168
  }
7169
 
7170
  // Run a handler that was bound to a key.
7171
  function doHandleBinding(cm, bound, dropShift) {
7172
    if (typeof bound == "string") {
7173
      bound = commands[bound];
7174
      if (!bound) { return false }
7175
    }
7176
    // Ensure previous input has been read, so that the handler sees a
7177
    // consistent view of the document
7178
    cm.display.input.ensurePolled();
7179
    var prevShift = cm.display.shift, done = false;
7180
    try {
7181
      if (cm.isReadOnly()) { cm.state.suppressEdits = true; }
7182
      if (dropShift) { cm.display.shift = false; }
7183
      done = bound(cm) != Pass;
7184
    } finally {
7185
      cm.display.shift = prevShift;
7186
      cm.state.suppressEdits = false;
7187
    }
7188
    return done
7189
  }
7190
 
7191
  function lookupKeyForEditor(cm, name, handle) {
7192
    for (var i = 0; i < cm.state.keyMaps.length; i++) {
7193
      var result = lookupKey(name, cm.state.keyMaps[i], handle, cm);
7194
      if (result) { return result }
7195
    }
7196
    return (cm.options.extraKeys && lookupKey(name, cm.options.extraKeys, handle, cm))
7197
      || lookupKey(name, cm.options.keyMap, handle, cm)
7198
  }
7199
 
7200
  // Note that, despite the name, this function is also used to check
7201
  // for bound mouse clicks.
7202
 
7203
  var stopSeq = new Delayed;
7204
 
7205
  function dispatchKey(cm, name, e, handle) {
7206
    var seq = cm.state.keySeq;
7207
    if (seq) {
7208
      if (isModifierKey(name)) { return "handled" }
7209
      if (/\'$/.test(name))
7210
        { cm.state.keySeq = null; }
7211
      else
7212
        { stopSeq.set(50, function () {
7213
          if (cm.state.keySeq == seq) {
7214
            cm.state.keySeq = null;
7215
            cm.display.input.reset();
7216
          }
7217
        }); }
7218
      if (dispatchKeyInner(cm, seq + " " + name, e, handle)) { return true }
7219
    }
7220
    return dispatchKeyInner(cm, name, e, handle)
7221
  }
7222
 
7223
  function dispatchKeyInner(cm, name, e, handle) {
7224
    var result = lookupKeyForEditor(cm, name, handle);
7225
 
7226
    if (result == "multi")
7227
      { cm.state.keySeq = name; }
7228
    if (result == "handled")
7229
      { signalLater(cm, "keyHandled", cm, name, e); }
7230
 
7231
    if (result == "handled" || result == "multi") {
7232
      e_preventDefault(e);
7233
      restartBlink(cm);
7234
    }
7235
 
7236
    return !!result
7237
  }
7238
 
7239
  // Handle a key from the keydown event.
7240
  function handleKeyBinding(cm, e) {
7241
    var name = keyName(e, true);
7242
    if (!name) { return false }
7243
 
7244
    if (e.shiftKey && !cm.state.keySeq) {
7245
      // First try to resolve full name (including 'Shift-'). Failing
7246
      // that, see if there is a cursor-motion command (starting with
7247
      // 'go') bound to the keyname without 'Shift-'.
7248
      return dispatchKey(cm, "Shift-" + name, e, function (b) { return doHandleBinding(cm, b, true); })
7249
          || dispatchKey(cm, name, e, function (b) {
7250
               if (typeof b == "string" ? /^go[A-Z]/.test(b) : b.motion)
7251
                 { return doHandleBinding(cm, b) }
7252
             })
7253
    } else {
7254
      return dispatchKey(cm, name, e, function (b) { return doHandleBinding(cm, b); })
7255
    }
7256
  }
7257
 
7258
  // Handle a key from the keypress event
7259
  function handleCharBinding(cm, e, ch) {
7260
    return dispatchKey(cm, "'" + ch + "'", e, function (b) { return doHandleBinding(cm, b, true); })
7261
  }
7262
 
7263
  var lastStoppedKey = null;
7264
  function onKeyDown(e) {
7265
    var cm = this;
15152 obado 7266
    if (e.target && e.target != cm.display.input.getField()) { return }
17702 obado 7267
    cm.curOp.focus = activeElt(doc(cm));
14283 obado 7268
    if (signalDOMEvent(cm, e)) { return }
7269
    // IE does strange things with escape.
7270
    if (ie && ie_version < 11 && e.keyCode == 27) { e.returnValue = false; }
7271
    var code = e.keyCode;
7272
    cm.display.shift = code == 16 || e.shiftKey;
7273
    var handled = handleKeyBinding(cm, e);
7274
    if (presto) {
7275
      lastStoppedKey = handled ? code : null;
7276
      // Opera has no cut event... we try to at least catch the key combo
7277
      if (!handled && code == 88 && !hasCopyEvent && (mac ? e.metaKey : e.ctrlKey))
7278
        { cm.replaceSelection("", null, "cut"); }
7279
    }
15152 obado 7280
    if (gecko && !mac && !handled && code == 46 && e.shiftKey && !e.ctrlKey && document.execCommand)
7281
      { document.execCommand("cut"); }
14283 obado 7282
 
7283
    // Turn mouse into crosshair when Alt is held on Mac.
7284
    if (code == 18 && !/\bCodeMirror-crosshair\b/.test(cm.display.lineDiv.className))
7285
      { showCrossHair(cm); }
7286
  }
7287
 
7288
  function showCrossHair(cm) {
7289
    var lineDiv = cm.display.lineDiv;
7290
    addClass(lineDiv, "CodeMirror-crosshair");
7291
 
7292
    function up(e) {
7293
      if (e.keyCode == 18 || !e.altKey) {
7294
        rmClass(lineDiv, "CodeMirror-crosshair");
7295
        off(document, "keyup", up);
7296
        off(document, "mouseover", up);
7297
      }
7298
    }
7299
    on(document, "keyup", up);
7300
    on(document, "mouseover", up);
7301
  }
7302
 
7303
  function onKeyUp(e) {
7304
    if (e.keyCode == 16) { this.doc.sel.shift = false; }
7305
    signalDOMEvent(this, e);
7306
  }
7307
 
7308
  function onKeyPress(e) {
7309
    var cm = this;
15152 obado 7310
    if (e.target && e.target != cm.display.input.getField()) { return }
14283 obado 7311
    if (eventInWidget(cm.display, e) || signalDOMEvent(cm, e) || e.ctrlKey && !e.altKey || mac && e.metaKey) { return }
7312
    var keyCode = e.keyCode, charCode = e.charCode;
7313
    if (presto && keyCode == lastStoppedKey) {lastStoppedKey = null; e_preventDefault(e); return}
7314
    if ((presto && (!e.which || e.which < 10)) && handleKeyBinding(cm, e)) { return }
7315
    var ch = String.fromCharCode(charCode == null ? keyCode : charCode);
7316
    // Some browsers fire keypress events for backspace
7317
    if (ch == "\x08") { return }
7318
    if (handleCharBinding(cm, e, ch)) { return }
7319
    cm.display.input.onKeyPress(e);
7320
  }
7321
 
7322
  var DOUBLECLICK_DELAY = 400;
7323
 
7324
  var PastClick = function(time, pos, button) {
7325
    this.time = time;
7326
    this.pos = pos;
7327
    this.button = button;
7328
  };
7329
 
7330
  PastClick.prototype.compare = function (time, pos, button) {
7331
    return this.time + DOUBLECLICK_DELAY > time &&
7332
      cmp(pos, this.pos) == 0 && button == this.button
7333
  };
7334
 
7335
  var lastClick, lastDoubleClick;
7336
  function clickRepeat(pos, button) {
7337
    var now = +new Date;
7338
    if (lastDoubleClick && lastDoubleClick.compare(now, pos, button)) {
7339
      lastClick = lastDoubleClick = null;
7340
      return "triple"
7341
    } else if (lastClick && lastClick.compare(now, pos, button)) {
7342
      lastDoubleClick = new PastClick(now, pos, button);
7343
      lastClick = null;
7344
      return "double"
7345
    } else {
7346
      lastClick = new PastClick(now, pos, button);
7347
      lastDoubleClick = null;
7348
      return "single"
7349
    }
7350
  }
7351
 
7352
  // A mouse down can be a single click, double click, triple click,
7353
  // start of selection drag, start of text drag, new cursor
7354
  // (ctrl-click), rectangle drag (alt-drag), or xwin
7355
  // middle-click-paste. Or it might be a click on something we should
7356
  // not interfere with, such as a scrollbar or widget.
7357
  function onMouseDown(e) {
7358
    var cm = this, display = cm.display;
7359
    if (signalDOMEvent(cm, e) || display.activeTouch && display.input.supportsTouch()) { return }
7360
    display.input.ensurePolled();
7361
    display.shift = e.shiftKey;
7362
 
7363
    if (eventInWidget(display, e)) {
7364
      if (!webkit) {
7365
        // Briefly turn off draggability, to allow widgets to do
7366
        // normal dragging things.
7367
        display.scroller.draggable = false;
7368
        setTimeout(function () { return display.scroller.draggable = true; }, 100);
7369
      }
7370
      return
7371
    }
7372
    if (clickInGutter(cm, e)) { return }
7373
    var pos = posFromMouse(cm, e), button = e_button(e), repeat = pos ? clickRepeat(pos, button) : "single";
17702 obado 7374
    win(cm).focus();
14283 obado 7375
 
7376
    // #3261: make sure, that we're not starting a second selection
7377
    if (button == 1 && cm.state.selectingText)
7378
      { cm.state.selectingText(e); }
7379
 
7380
    if (pos && handleMappedButton(cm, button, pos, repeat, e)) { return }
7381
 
7382
    if (button == 1) {
7383
      if (pos) { leftButtonDown(cm, pos, repeat, e); }
7384
      else if (e_target(e) == display.scroller) { e_preventDefault(e); }
7385
    } else if (button == 2) {
7386
      if (pos) { extendSelection(cm.doc, pos); }
7387
      setTimeout(function () { return display.input.focus(); }, 20);
7388
    } else if (button == 3) {
7389
      if (captureRightClick) { cm.display.input.onContextMenu(e); }
7390
      else { delayBlurEvent(cm); }
7391
    }
7392
  }
7393
 
7394
  function handleMappedButton(cm, button, pos, repeat, event) {
7395
    var name = "Click";
7396
    if (repeat == "double") { name = "Double" + name; }
7397
    else if (repeat == "triple") { name = "Triple" + name; }
7398
    name = (button == 1 ? "Left" : button == 2 ? "Middle" : "Right") + name;
7399
 
7400
    return dispatchKey(cm,  addModifierNames(name, event), event, function (bound) {
7401
      if (typeof bound == "string") { bound = commands[bound]; }
7402
      if (!bound) { return false }
7403
      var done = false;
7404
      try {
7405
        if (cm.isReadOnly()) { cm.state.suppressEdits = true; }
7406
        done = bound(cm, pos) != Pass;
7407
      } finally {
7408
        cm.state.suppressEdits = false;
7409
      }
7410
      return done
7411
    })
7412
  }
7413
 
7414
  function configureMouse(cm, repeat, event) {
7415
    var option = cm.getOption("configureMouse");
7416
    var value = option ? option(cm, repeat, event) : {};
7417
    if (value.unit == null) {
7418
      var rect = chromeOS ? event.shiftKey && event.metaKey : event.altKey;
7419
      value.unit = rect ? "rectangle" : repeat == "single" ? "char" : repeat == "double" ? "word" : "line";
7420
    }
7421
    if (value.extend == null || cm.doc.extend) { value.extend = cm.doc.extend || event.shiftKey; }
7422
    if (value.addNew == null) { value.addNew = mac ? event.metaKey : event.ctrlKey; }
7423
    if (value.moveOnDrag == null) { value.moveOnDrag = !(mac ? event.altKey : event.ctrlKey); }
7424
    return value
7425
  }
7426
 
7427
  function leftButtonDown(cm, pos, repeat, event) {
7428
    if (ie) { setTimeout(bind(ensureFocus, cm), 0); }
17702 obado 7429
    else { cm.curOp.focus = activeElt(doc(cm)); }
14283 obado 7430
 
7431
    var behavior = configureMouse(cm, repeat, event);
7432
 
7433
    var sel = cm.doc.sel, contained;
7434
    if (cm.options.dragDrop && dragAndDrop && !cm.isReadOnly() &&
7435
        repeat == "single" && (contained = sel.contains(pos)) > -1 &&
7436
        (cmp((contained = sel.ranges[contained]).from(), pos) < 0 || pos.xRel > 0) &&
7437
        (cmp(contained.to(), pos) > 0 || pos.xRel < 0))
7438
      { leftButtonStartDrag(cm, event, pos, behavior); }
7439
    else
7440
      { leftButtonSelect(cm, event, pos, behavior); }
7441
  }
7442
 
7443
  // Start a text drag. When it ends, see if any dragging actually
7444
  // happen, and treat as a click if it didn't.
7445
  function leftButtonStartDrag(cm, event, pos, behavior) {
7446
    var display = cm.display, moved = false;
7447
    var dragEnd = operation(cm, function (e) {
7448
      if (webkit) { display.scroller.draggable = false; }
7449
      cm.state.draggingText = false;
16493 obado 7450
      if (cm.state.delayingBlurEvent) {
7451
        if (cm.hasFocus()) { cm.state.delayingBlurEvent = false; }
7452
        else { delayBlurEvent(cm); }
7453
      }
14283 obado 7454
      off(display.wrapper.ownerDocument, "mouseup", dragEnd);
7455
      off(display.wrapper.ownerDocument, "mousemove", mouseMove);
7456
      off(display.scroller, "dragstart", dragStart);
7457
      off(display.scroller, "drop", dragEnd);
7458
      if (!moved) {
7459
        e_preventDefault(e);
7460
        if (!behavior.addNew)
7461
          { extendSelection(cm.doc, pos, null, null, behavior.extend); }
7462
        // Work around unexplainable focus problem in IE9 (#2127) and Chrome (#3081)
15152 obado 7463
        if ((webkit && !safari) || ie && ie_version == 9)
7464
          { setTimeout(function () {display.wrapper.ownerDocument.body.focus({preventScroll: true}); display.input.focus();}, 20); }
14283 obado 7465
        else
7466
          { display.input.focus(); }
7467
      }
7468
    });
7469
    var mouseMove = function(e2) {
7470
      moved = moved || Math.abs(event.clientX - e2.clientX) + Math.abs(event.clientY - e2.clientY) >= 10;
7471
    };
7472
    var dragStart = function () { return moved = true; };
7473
    // Let the drag handler handle this.
7474
    if (webkit) { display.scroller.draggable = true; }
7475
    cm.state.draggingText = dragEnd;
7476
    dragEnd.copy = !behavior.moveOnDrag;
7477
    on(display.wrapper.ownerDocument, "mouseup", dragEnd);
7478
    on(display.wrapper.ownerDocument, "mousemove", mouseMove);
7479
    on(display.scroller, "dragstart", dragStart);
7480
    on(display.scroller, "drop", dragEnd);
7481
 
16493 obado 7482
    cm.state.delayingBlurEvent = true;
14283 obado 7483
    setTimeout(function () { return display.input.focus(); }, 20);
16493 obado 7484
    // IE's approach to draggable
7485
    if (display.scroller.dragDrop) { display.scroller.dragDrop(); }
14283 obado 7486
  }
7487
 
7488
  function rangeForUnit(cm, pos, unit) {
7489
    if (unit == "char") { return new Range(pos, pos) }
7490
    if (unit == "word") { return cm.findWordAt(pos) }
7491
    if (unit == "line") { return new Range(Pos(pos.line, 0), clipPos(cm.doc, Pos(pos.line + 1, 0))) }
7492
    var result = unit(cm, pos);
7493
    return new Range(result.from, result.to)
7494
  }
7495
 
7496
  // Normal selection, as opposed to text dragging.
7497
  function leftButtonSelect(cm, event, start, behavior) {
16493 obado 7498
    if (ie) { delayBlurEvent(cm); }
17702 obado 7499
    var display = cm.display, doc$1 = cm.doc;
14283 obado 7500
    e_preventDefault(event);
7501
 
17702 obado 7502
    var ourRange, ourIndex, startSel = doc$1.sel, ranges = startSel.ranges;
14283 obado 7503
    if (behavior.addNew && !behavior.extend) {
17702 obado 7504
      ourIndex = doc$1.sel.contains(start);
14283 obado 7505
      if (ourIndex > -1)
7506
        { ourRange = ranges[ourIndex]; }
7507
      else
7508
        { ourRange = new Range(start, start); }
7509
    } else {
17702 obado 7510
      ourRange = doc$1.sel.primary();
7511
      ourIndex = doc$1.sel.primIndex;
14283 obado 7512
    }
7513
 
7514
    if (behavior.unit == "rectangle") {
7515
      if (!behavior.addNew) { ourRange = new Range(start, start); }
7516
      start = posFromMouse(cm, event, true, true);
7517
      ourIndex = -1;
7518
    } else {
15152 obado 7519
      var range = rangeForUnit(cm, start, behavior.unit);
14283 obado 7520
      if (behavior.extend)
15152 obado 7521
        { ourRange = extendRange(ourRange, range.anchor, range.head, behavior.extend); }
14283 obado 7522
      else
15152 obado 7523
        { ourRange = range; }
14283 obado 7524
    }
7525
 
7526
    if (!behavior.addNew) {
7527
      ourIndex = 0;
17702 obado 7528
      setSelection(doc$1, new Selection([ourRange], 0), sel_mouse);
7529
      startSel = doc$1.sel;
14283 obado 7530
    } else if (ourIndex == -1) {
7531
      ourIndex = ranges.length;
17702 obado 7532
      setSelection(doc$1, normalizeSelection(cm, ranges.concat([ourRange]), ourIndex),
14283 obado 7533
                   {scroll: false, origin: "*mouse"});
7534
    } else if (ranges.length > 1 && ranges[ourIndex].empty() && behavior.unit == "char" && !behavior.extend) {
17702 obado 7535
      setSelection(doc$1, normalizeSelection(cm, ranges.slice(0, ourIndex).concat(ranges.slice(ourIndex + 1)), 0),
14283 obado 7536
                   {scroll: false, origin: "*mouse"});
17702 obado 7537
      startSel = doc$1.sel;
14283 obado 7538
    } else {
17702 obado 7539
      replaceOneSelection(doc$1, ourIndex, ourRange, sel_mouse);
14283 obado 7540
    }
7541
 
7542
    var lastPos = start;
7543
    function extendTo(pos) {
7544
      if (cmp(lastPos, pos) == 0) { return }
7545
      lastPos = pos;
7546
 
7547
      if (behavior.unit == "rectangle") {
7548
        var ranges = [], tabSize = cm.options.tabSize;
17702 obado 7549
        var startCol = countColumn(getLine(doc$1, start.line).text, start.ch, tabSize);
7550
        var posCol = countColumn(getLine(doc$1, pos.line).text, pos.ch, tabSize);
14283 obado 7551
        var left = Math.min(startCol, posCol), right = Math.max(startCol, posCol);
7552
        for (var line = Math.min(start.line, pos.line), end = Math.min(cm.lastLine(), Math.max(start.line, pos.line));
7553
             line <= end; line++) {
17702 obado 7554
          var text = getLine(doc$1, line).text, leftPos = findColumn(text, left, tabSize);
14283 obado 7555
          if (left == right)
7556
            { ranges.push(new Range(Pos(line, leftPos), Pos(line, leftPos))); }
7557
          else if (text.length > leftPos)
7558
            { ranges.push(new Range(Pos(line, leftPos), Pos(line, findColumn(text, right, tabSize)))); }
7559
        }
7560
        if (!ranges.length) { ranges.push(new Range(start, start)); }
17702 obado 7561
        setSelection(doc$1, normalizeSelection(cm, startSel.ranges.slice(0, ourIndex).concat(ranges), ourIndex),
14283 obado 7562
                     {origin: "*mouse", scroll: false});
7563
        cm.scrollIntoView(pos);
7564
      } else {
7565
        var oldRange = ourRange;
15152 obado 7566
        var range = rangeForUnit(cm, pos, behavior.unit);
14283 obado 7567
        var anchor = oldRange.anchor, head;
15152 obado 7568
        if (cmp(range.anchor, anchor) > 0) {
7569
          head = range.head;
7570
          anchor = minPos(oldRange.from(), range.anchor);
14283 obado 7571
        } else {
15152 obado 7572
          head = range.anchor;
7573
          anchor = maxPos(oldRange.to(), range.head);
14283 obado 7574
        }
7575
        var ranges$1 = startSel.ranges.slice(0);
17702 obado 7576
        ranges$1[ourIndex] = bidiSimplify(cm, new Range(clipPos(doc$1, anchor), head));
7577
        setSelection(doc$1, normalizeSelection(cm, ranges$1, ourIndex), sel_mouse);
14283 obado 7578
      }
7579
    }
7580
 
7581
    var editorSize = display.wrapper.getBoundingClientRect();
7582
    // Used to ensure timeout re-tries don't fire when another extend
7583
    // happened in the meantime (clearTimeout isn't reliable -- at
7584
    // least on Chrome, the timeouts still happen even when cleared,
7585
    // if the clear happens after their scheduled firing time).
7586
    var counter = 0;
7587
 
7588
    function extend(e) {
7589
      var curCount = ++counter;
7590
      var cur = posFromMouse(cm, e, true, behavior.unit == "rectangle");
7591
      if (!cur) { return }
7592
      if (cmp(cur, lastPos) != 0) {
17702 obado 7593
        cm.curOp.focus = activeElt(doc(cm));
14283 obado 7594
        extendTo(cur);
17702 obado 7595
        var visible = visibleLines(display, doc$1);
14283 obado 7596
        if (cur.line >= visible.to || cur.line < visible.from)
7597
          { setTimeout(operation(cm, function () {if (counter == curCount) { extend(e); }}), 150); }
7598
      } else {
7599
        var outside = e.clientY < editorSize.top ? -20 : e.clientY > editorSize.bottom ? 20 : 0;
7600
        if (outside) { setTimeout(operation(cm, function () {
7601
          if (counter != curCount) { return }
7602
          display.scroller.scrollTop += outside;
7603
          extend(e);
7604
        }), 50); }
7605
      }
7606
    }
7607
 
7608
    function done(e) {
7609
      cm.state.selectingText = false;
7610
      counter = Infinity;
7611
      // If e is null or undefined we interpret this as someone trying
7612
      // to explicitly cancel the selection rather than the user
7613
      // letting go of the mouse button.
7614
      if (e) {
7615
        e_preventDefault(e);
7616
        display.input.focus();
7617
      }
7618
      off(display.wrapper.ownerDocument, "mousemove", move);
7619
      off(display.wrapper.ownerDocument, "mouseup", up);
17702 obado 7620
      doc$1.history.lastSelOrigin = null;
14283 obado 7621
    }
7622
 
7623
    var move = operation(cm, function (e) {
7624
      if (e.buttons === 0 || !e_button(e)) { done(e); }
7625
      else { extend(e); }
7626
    });
7627
    var up = operation(cm, done);
7628
    cm.state.selectingText = up;
7629
    on(display.wrapper.ownerDocument, "mousemove", move);
7630
    on(display.wrapper.ownerDocument, "mouseup", up);
7631
  }
7632
 
7633
  // Used when mouse-selecting to adjust the anchor to the proper side
7634
  // of a bidi jump depending on the visual position of the head.
15152 obado 7635
  function bidiSimplify(cm, range) {
7636
    var anchor = range.anchor;
7637
    var head = range.head;
14283 obado 7638
    var anchorLine = getLine(cm.doc, anchor.line);
15152 obado 7639
    if (cmp(anchor, head) == 0 && anchor.sticky == head.sticky) { return range }
14283 obado 7640
    var order = getOrder(anchorLine);
15152 obado 7641
    if (!order) { return range }
14283 obado 7642
    var index = getBidiPartAt(order, anchor.ch, anchor.sticky), part = order[index];
15152 obado 7643
    if (part.from != anchor.ch && part.to != anchor.ch) { return range }
14283 obado 7644
    var boundary = index + ((part.from == anchor.ch) == (part.level != 1) ? 0 : 1);
15152 obado 7645
    if (boundary == 0 || boundary == order.length) { return range }
14283 obado 7646
 
7647
    // Compute the relative visual position of the head compared to the
7648
    // anchor (<0 is to the left, >0 to the right)
7649
    var leftSide;
7650
    if (head.line != anchor.line) {
7651
      leftSide = (head.line - anchor.line) * (cm.doc.direction == "ltr" ? 1 : -1) > 0;
7652
    } else {
7653
      var headIndex = getBidiPartAt(order, head.ch, head.sticky);
7654
      var dir = headIndex - index || (head.ch - anchor.ch) * (part.level == 1 ? -1 : 1);
7655
      if (headIndex == boundary - 1 || headIndex == boundary)
7656
        { leftSide = dir < 0; }
7657
      else
7658
        { leftSide = dir > 0; }
7659
    }
7660
 
7661
    var usePart = order[boundary + (leftSide ? -1 : 0)];
7662
    var from = leftSide == (usePart.level == 1);
7663
    var ch = from ? usePart.from : usePart.to, sticky = from ? "after" : "before";
15152 obado 7664
    return anchor.ch == ch && anchor.sticky == sticky ? range : new Range(new Pos(anchor.line, ch, sticky), head)
14283 obado 7665
  }
7666
 
7667
 
7668
  // Determines whether an event happened in the gutter, and fires the
7669
  // handlers for the corresponding event.
7670
  function gutterEvent(cm, e, type, prevent) {
7671
    var mX, mY;
7672
    if (e.touches) {
7673
      mX = e.touches[0].clientX;
7674
      mY = e.touches[0].clientY;
7675
    } else {
7676
      try { mX = e.clientX; mY = e.clientY; }
15332 obado 7677
      catch(e$1) { return false }
14283 obado 7678
    }
7679
    if (mX >= Math.floor(cm.display.gutters.getBoundingClientRect().right)) { return false }
7680
    if (prevent) { e_preventDefault(e); }
7681
 
7682
    var display = cm.display;
7683
    var lineBox = display.lineDiv.getBoundingClientRect();
7684
 
7685
    if (mY > lineBox.bottom || !hasHandler(cm, type)) { return e_defaultPrevented(e) }
7686
    mY -= lineBox.top - display.viewOffset;
7687
 
7688
    for (var i = 0; i < cm.display.gutterSpecs.length; ++i) {
7689
      var g = display.gutters.childNodes[i];
7690
      if (g && g.getBoundingClientRect().right >= mX) {
7691
        var line = lineAtHeight(cm.doc, mY);
7692
        var gutter = cm.display.gutterSpecs[i];
7693
        signal(cm, type, cm, line, gutter.className, e);
7694
        return e_defaultPrevented(e)
7695
      }
7696
    }
7697
  }
7698
 
7699
  function clickInGutter(cm, e) {
7700
    return gutterEvent(cm, e, "gutterClick", true)
7701
  }
7702
 
7703
  // CONTEXT MENU HANDLING
7704
 
7705
  // To make the context menu work, we need to briefly unhide the
7706
  // textarea (making it as unobtrusive as possible) to let the
7707
  // right-click take effect on it.
7708
  function onContextMenu(cm, e) {
7709
    if (eventInWidget(cm.display, e) || contextMenuInGutter(cm, e)) { return }
7710
    if (signalDOMEvent(cm, e, "contextmenu")) { return }
7711
    if (!captureRightClick) { cm.display.input.onContextMenu(e); }
7712
  }
7713
 
7714
  function contextMenuInGutter(cm, e) {
7715
    if (!hasHandler(cm, "gutterContextMenu")) { return false }
7716
    return gutterEvent(cm, e, "gutterContextMenu", false)
7717
  }
7718
 
7719
  function themeChanged(cm) {
7720
    cm.display.wrapper.className = cm.display.wrapper.className.replace(/\s*cm-s-\S+/g, "") +
7721
      cm.options.theme.replace(/(^|\s)\s*/g, " cm-s-");
7722
    clearCaches(cm);
7723
  }
7724
 
7725
  var Init = {toString: function(){return "CodeMirror.Init"}};
7726
 
7727
  var defaults = {};
7728
  var optionHandlers = {};
7729
 
7730
  function defineOptions(CodeMirror) {
7731
    var optionHandlers = CodeMirror.optionHandlers;
7732
 
7733
    function option(name, deflt, handle, notOnInit) {
7734
      CodeMirror.defaults[name] = deflt;
7735
      if (handle) { optionHandlers[name] =
7736
        notOnInit ? function (cm, val, old) {if (old != Init) { handle(cm, val, old); }} : handle; }
7737
    }
7738
 
7739
    CodeMirror.defineOption = option;
7740
 
7741
    // Passed to option handlers when there is no old value.
7742
    CodeMirror.Init = Init;
7743
 
7744
    // These two are, on init, called from the constructor because they
7745
    // have to be initialized before the editor can start at all.
7746
    option("value", "", function (cm, val) { return cm.setValue(val); }, true);
7747
    option("mode", null, function (cm, val) {
7748
      cm.doc.modeOption = val;
7749
      loadMode(cm);
7750
    }, true);
7751
 
7752
    option("indentUnit", 2, loadMode, true);
7753
    option("indentWithTabs", false);
7754
    option("smartIndent", true);
7755
    option("tabSize", 4, function (cm) {
7756
      resetModeState(cm);
7757
      clearCaches(cm);
7758
      regChange(cm);
7759
    }, true);
7760
 
7761
    option("lineSeparator", null, function (cm, val) {
7762
      cm.doc.lineSep = val;
7763
      if (!val) { return }
7764
      var newBreaks = [], lineNo = cm.doc.first;
7765
      cm.doc.iter(function (line) {
7766
        for (var pos = 0;;) {
7767
          var found = line.text.indexOf(val, pos);
7768
          if (found == -1) { break }
7769
          pos = found + val.length;
7770
          newBreaks.push(Pos(lineNo, found));
7771
        }
7772
        lineNo++;
7773
      });
7774
      for (var i = newBreaks.length - 1; i >= 0; i--)
7775
        { replaceRange(cm.doc, val, newBreaks[i], Pos(newBreaks[i].line, newBreaks[i].ch + val.length)); }
7776
    });
17702 obado 7777
    option("specialChars", /[\u0000-\u001f\u007f-\u009f\u00ad\u061c\u200b\u200e\u200f\u2028\u2029\u202d\u202e\u2066\u2067\u2069\ufeff\ufff9-\ufffc]/g, function (cm, val, old) {
14283 obado 7778
      cm.state.specialChars = new RegExp(val.source + (val.test("\t") ? "" : "|\t"), "g");
7779
      if (old != Init) { cm.refresh(); }
7780
    });
7781
    option("specialCharPlaceholder", defaultSpecialCharPlaceholder, function (cm) { return cm.refresh(); }, true);
7782
    option("electricChars", true);
7783
    option("inputStyle", mobile ? "contenteditable" : "textarea", function () {
7784
      throw new Error("inputStyle can not (yet) be changed in a running editor") // FIXME
7785
    }, true);
7786
    option("spellcheck", false, function (cm, val) { return cm.getInputField().spellcheck = val; }, true);
7787
    option("autocorrect", false, function (cm, val) { return cm.getInputField().autocorrect = val; }, true);
7788
    option("autocapitalize", false, function (cm, val) { return cm.getInputField().autocapitalize = val; }, true);
7789
    option("rtlMoveVisually", !windows);
7790
    option("wholeLineUpdateBefore", true);
7791
 
7792
    option("theme", "default", function (cm) {
7793
      themeChanged(cm);
7794
      updateGutters(cm);
7795
    }, true);
7796
    option("keyMap", "default", function (cm, val, old) {
7797
      var next = getKeyMap(val);
7798
      var prev = old != Init && getKeyMap(old);
7799
      if (prev && prev.detach) { prev.detach(cm, next); }
7800
      if (next.attach) { next.attach(cm, prev || null); }
7801
    });
7802
    option("extraKeys", null);
7803
    option("configureMouse", null);
7804
 
7805
    option("lineWrapping", false, wrappingChanged, true);
7806
    option("gutters", [], function (cm, val) {
7807
      cm.display.gutterSpecs = getGutters(val, cm.options.lineNumbers);
7808
      updateGutters(cm);
7809
    }, true);
7810
    option("fixedGutter", true, function (cm, val) {
7811
      cm.display.gutters.style.left = val ? compensateForHScroll(cm.display) + "px" : "0";
7812
      cm.refresh();
7813
    }, true);
7814
    option("coverGutterNextToScrollbar", false, function (cm) { return updateScrollbars(cm); }, true);
7815
    option("scrollbarStyle", "native", function (cm) {
7816
      initScrollbars(cm);
7817
      updateScrollbars(cm);
7818
      cm.display.scrollbars.setScrollTop(cm.doc.scrollTop);
7819
      cm.display.scrollbars.setScrollLeft(cm.doc.scrollLeft);
7820
    }, true);
7821
    option("lineNumbers", false, function (cm, val) {
7822
      cm.display.gutterSpecs = getGutters(cm.options.gutters, val);
7823
      updateGutters(cm);
7824
    }, true);
7825
    option("firstLineNumber", 1, updateGutters, true);
7826
    option("lineNumberFormatter", function (integer) { return integer; }, updateGutters, true);
7827
    option("showCursorWhenSelecting", false, updateSelection, true);
7828
 
7829
    option("resetSelectionOnContextMenu", true);
7830
    option("lineWiseCopyCut", true);
7831
    option("pasteLinesPerSelection", true);
7832
    option("selectionsMayTouch", false);
7833
 
7834
    option("readOnly", false, function (cm, val) {
7835
      if (val == "nocursor") {
7836
        onBlur(cm);
7837
        cm.display.input.blur();
7838
      }
7839
      cm.display.input.readOnlyChanged(val);
7840
    });
15152 obado 7841
 
7842
    option("screenReaderLabel", null, function (cm, val) {
7843
      val = (val === '') ? null : val;
7844
      cm.display.input.screenReaderLabelChanged(val);
7845
    });
7846
 
14283 obado 7847
    option("disableInput", false, function (cm, val) {if (!val) { cm.display.input.reset(); }}, true);
7848
    option("dragDrop", true, dragDropChanged);
7849
    option("allowDropFileTypes", null);
7850
 
7851
    option("cursorBlinkRate", 530);
7852
    option("cursorScrollMargin", 0);
7853
    option("cursorHeight", 1, updateSelection, true);
7854
    option("singleCursorHeightPerLine", true, updateSelection, true);
7855
    option("workTime", 100);
7856
    option("workDelay", 100);
7857
    option("flattenSpans", true, resetModeState, true);
7858
    option("addModeClass", false, resetModeState, true);
7859
    option("pollInterval", 100);
7860
    option("undoDepth", 200, function (cm, val) { return cm.doc.history.undoDepth = val; });
7861
    option("historyEventDelay", 1250);
7862
    option("viewportMargin", 10, function (cm) { return cm.refresh(); }, true);
7863
    option("maxHighlightLength", 10000, resetModeState, true);
7864
    option("moveInputWithCursor", true, function (cm, val) {
7865
      if (!val) { cm.display.input.resetPosition(); }
7866
    });
7867
 
7868
    option("tabindex", null, function (cm, val) { return cm.display.input.getField().tabIndex = val || ""; });
7869
    option("autofocus", null);
7870
    option("direction", "ltr", function (cm, val) { return cm.doc.setDirection(val); }, true);
7871
    option("phrases", null);
7872
  }
7873
 
7874
  function dragDropChanged(cm, value, old) {
7875
    var wasOn = old && old != Init;
7876
    if (!value != !wasOn) {
7877
      var funcs = cm.display.dragFunctions;
7878
      var toggle = value ? on : off;
7879
      toggle(cm.display.scroller, "dragstart", funcs.start);
7880
      toggle(cm.display.scroller, "dragenter", funcs.enter);
7881
      toggle(cm.display.scroller, "dragover", funcs.over);
7882
      toggle(cm.display.scroller, "dragleave", funcs.leave);
7883
      toggle(cm.display.scroller, "drop", funcs.drop);
7884
    }
7885
  }
7886
 
7887
  function wrappingChanged(cm) {
7888
    if (cm.options.lineWrapping) {
7889
      addClass(cm.display.wrapper, "CodeMirror-wrap");
7890
      cm.display.sizer.style.minWidth = "";
7891
      cm.display.sizerWidth = null;
7892
    } else {
7893
      rmClass(cm.display.wrapper, "CodeMirror-wrap");
7894
      findMaxLine(cm);
7895
    }
7896
    estimateLineHeights(cm);
7897
    regChange(cm);
7898
    clearCaches(cm);
7899
    setTimeout(function () { return updateScrollbars(cm); }, 100);
7900
  }
7901
 
7902
  // A CodeMirror instance represents an editor. This is the object
7903
  // that user code is usually dealing with.
7904
 
7905
  function CodeMirror(place, options) {
7906
    var this$1 = this;
7907
 
7908
    if (!(this instanceof CodeMirror)) { return new CodeMirror(place, options) }
7909
 
7910
    this.options = options = options ? copyObj(options) : {};
7911
    // Determine effective options based on given values and defaults.
7912
    copyObj(defaults, options, false);
7913
 
7914
    var doc = options.value;
7915
    if (typeof doc == "string") { doc = new Doc(doc, options.mode, null, options.lineSeparator, options.direction); }
7916
    else if (options.mode) { doc.modeOption = options.mode; }
7917
    this.doc = doc;
7918
 
7919
    var input = new CodeMirror.inputStyles[options.inputStyle](this);
7920
    var display = this.display = new Display(place, doc, input, options);
7921
    display.wrapper.CodeMirror = this;
7922
    themeChanged(this);
7923
    if (options.lineWrapping)
7924
      { this.display.wrapper.className += " CodeMirror-wrap"; }
7925
    initScrollbars(this);
7926
 
7927
    this.state = {
7928
      keyMaps: [],  // stores maps added by addKeyMap
7929
      overlays: [], // highlighting overlays, as added by addOverlay
7930
      modeGen: 0,   // bumped when mode/overlay changes, used to invalidate highlighting info
7931
      overwrite: false,
7932
      delayingBlurEvent: false,
7933
      focused: false,
7934
      suppressEdits: false, // used to disable editing during key handlers when in readOnly mode
7935
      pasteIncoming: -1, cutIncoming: -1, // help recognize paste/cut edits in input.poll
7936
      selectingText: false,
7937
      draggingText: false,
7938
      highlight: new Delayed(), // stores highlight worker timeout
7939
      keySeq: null,  // Unfinished key sequence
7940
      specialChars: null
7941
    };
7942
 
7943
    if (options.autofocus && !mobile) { display.input.focus(); }
7944
 
7945
    // Override magic textarea content restore that IE sometimes does
7946
    // on our hidden textarea on reload
7947
    if (ie && ie_version < 11) { setTimeout(function () { return this$1.display.input.reset(true); }, 20); }
7948
 
7949
    registerEventHandlers(this);
7950
    ensureGlobalHandlers();
7951
 
7952
    startOperation(this);
7953
    this.curOp.forceUpdate = true;
7954
    attachDoc(this, doc);
7955
 
7956
    if ((options.autofocus && !mobile) || this.hasFocus())
16493 obado 7957
      { setTimeout(function () {
7958
        if (this$1.hasFocus() && !this$1.state.focused) { onFocus(this$1); }
7959
      }, 20); }
14283 obado 7960
    else
7961
      { onBlur(this); }
7962
 
7963
    for (var opt in optionHandlers) { if (optionHandlers.hasOwnProperty(opt))
15152 obado 7964
      { optionHandlers[opt](this, options[opt], Init); } }
14283 obado 7965
    maybeUpdateLineNumberWidth(this);
7966
    if (options.finishInit) { options.finishInit(this); }
15152 obado 7967
    for (var i = 0; i < initHooks.length; ++i) { initHooks[i](this); }
14283 obado 7968
    endOperation(this);
7969
    // Suppress optimizelegibility in Webkit, since it breaks text
7970
    // measuring on line wrapping boundaries.
7971
    if (webkit && options.lineWrapping &&
7972
        getComputedStyle(display.lineDiv).textRendering == "optimizelegibility")
7973
      { display.lineDiv.style.textRendering = "auto"; }
7974
  }
7975
 
7976
  // The default configuration options.
7977
  CodeMirror.defaults = defaults;
7978
  // Functions to run when options are changed.
7979
  CodeMirror.optionHandlers = optionHandlers;
7980
 
7981
  // Attach the necessary event handlers when initializing the editor
7982
  function registerEventHandlers(cm) {
7983
    var d = cm.display;
7984
    on(d.scroller, "mousedown", operation(cm, onMouseDown));
7985
    // Older IE's will not fire a second mousedown for a double click
7986
    if (ie && ie_version < 11)
7987
      { on(d.scroller, "dblclick", operation(cm, function (e) {
7988
        if (signalDOMEvent(cm, e)) { return }
7989
        var pos = posFromMouse(cm, e);
7990
        if (!pos || clickInGutter(cm, e) || eventInWidget(cm.display, e)) { return }
7991
        e_preventDefault(e);
7992
        var word = cm.findWordAt(pos);
7993
        extendSelection(cm.doc, word.anchor, word.head);
7994
      })); }
7995
    else
7996
      { on(d.scroller, "dblclick", function (e) { return signalDOMEvent(cm, e) || e_preventDefault(e); }); }
7997
    // Some browsers fire contextmenu *after* opening the menu, at
7998
    // which point we can't mess with it anymore. Context menu is
7999
    // handled in onMouseDown for these browsers.
8000
    on(d.scroller, "contextmenu", function (e) { return onContextMenu(cm, e); });
15152 obado 8001
    on(d.input.getField(), "contextmenu", function (e) {
8002
      if (!d.scroller.contains(e.target)) { onContextMenu(cm, e); }
8003
    });
14283 obado 8004
 
8005
    // Used to suppress mouse event handling when a touch happens
8006
    var touchFinished, prevTouch = {end: 0};
8007
    function finishTouch() {
8008
      if (d.activeTouch) {
8009
        touchFinished = setTimeout(function () { return d.activeTouch = null; }, 1000);
8010
        prevTouch = d.activeTouch;
8011
        prevTouch.end = +new Date;
8012
      }
8013
    }
8014
    function isMouseLikeTouchEvent(e) {
8015
      if (e.touches.length != 1) { return false }
8016
      var touch = e.touches[0];
8017
      return touch.radiusX <= 1 && touch.radiusY <= 1
8018
    }
8019
    function farAway(touch, other) {
8020
      if (other.left == null) { return true }
8021
      var dx = other.left - touch.left, dy = other.top - touch.top;
8022
      return dx * dx + dy * dy > 20 * 20
8023
    }
8024
    on(d.scroller, "touchstart", function (e) {
8025
      if (!signalDOMEvent(cm, e) && !isMouseLikeTouchEvent(e) && !clickInGutter(cm, e)) {
8026
        d.input.ensurePolled();
8027
        clearTimeout(touchFinished);
8028
        var now = +new Date;
8029
        d.activeTouch = {start: now, moved: false,
8030
                         prev: now - prevTouch.end <= 300 ? prevTouch : null};
8031
        if (e.touches.length == 1) {
8032
          d.activeTouch.left = e.touches[0].pageX;
8033
          d.activeTouch.top = e.touches[0].pageY;
8034
        }
8035
      }
8036
    });
8037
    on(d.scroller, "touchmove", function () {
8038
      if (d.activeTouch) { d.activeTouch.moved = true; }
8039
    });
8040
    on(d.scroller, "touchend", function (e) {
8041
      var touch = d.activeTouch;
8042
      if (touch && !eventInWidget(d, e) && touch.left != null &&
8043
          !touch.moved && new Date - touch.start < 300) {
8044
        var pos = cm.coordsChar(d.activeTouch, "page"), range;
8045
        if (!touch.prev || farAway(touch, touch.prev)) // Single tap
8046
          { range = new Range(pos, pos); }
8047
        else if (!touch.prev.prev || farAway(touch, touch.prev.prev)) // Double tap
8048
          { range = cm.findWordAt(pos); }
8049
        else // Triple tap
8050
          { range = new Range(Pos(pos.line, 0), clipPos(cm.doc, Pos(pos.line + 1, 0))); }
8051
        cm.setSelection(range.anchor, range.head);
8052
        cm.focus();
8053
        e_preventDefault(e);
8054
      }
8055
      finishTouch();
8056
    });
8057
    on(d.scroller, "touchcancel", finishTouch);
8058
 
8059
    // Sync scrolling between fake scrollbars and real scrollable
8060
    // area, ensure viewport is updated when scrolling.
8061
    on(d.scroller, "scroll", function () {
8062
      if (d.scroller.clientHeight) {
8063
        updateScrollTop(cm, d.scroller.scrollTop);
8064
        setScrollLeft(cm, d.scroller.scrollLeft, true);
8065
        signal(cm, "scroll", cm);
8066
      }
8067
    });
8068
 
8069
    // Listen to wheel events in order to try and update the viewport on time.
8070
    on(d.scroller, "mousewheel", function (e) { return onScrollWheel(cm, e); });
8071
    on(d.scroller, "DOMMouseScroll", function (e) { return onScrollWheel(cm, e); });
8072
 
8073
    // Prevent wrapper from ever scrolling
8074
    on(d.wrapper, "scroll", function () { return d.wrapper.scrollTop = d.wrapper.scrollLeft = 0; });
8075
 
8076
    d.dragFunctions = {
8077
      enter: function (e) {if (!signalDOMEvent(cm, e)) { e_stop(e); }},
8078
      over: function (e) {if (!signalDOMEvent(cm, e)) { onDragOver(cm, e); e_stop(e); }},
8079
      start: function (e) { return onDragStart(cm, e); },
8080
      drop: operation(cm, onDrop),
8081
      leave: function (e) {if (!signalDOMEvent(cm, e)) { clearDragCursor(cm); }}
8082
    };
8083
 
8084
    var inp = d.input.getField();
8085
    on(inp, "keyup", function (e) { return onKeyUp.call(cm, e); });
8086
    on(inp, "keydown", operation(cm, onKeyDown));
8087
    on(inp, "keypress", operation(cm, onKeyPress));
8088
    on(inp, "focus", function (e) { return onFocus(cm, e); });
8089
    on(inp, "blur", function (e) { return onBlur(cm, e); });
8090
  }
8091
 
8092
  var initHooks = [];
8093
  CodeMirror.defineInitHook = function (f) { return initHooks.push(f); };
8094
 
8095
  // Indent the given line. The how parameter can be "smart",
8096
  // "add"/null, "subtract", or "prev". When aggressive is false
8097
  // (typically set to true for forced single-line indents), empty
8098
  // lines are not indented, and places where the mode returns Pass
8099
  // are left alone.
8100
  function indentLine(cm, n, how, aggressive) {
8101
    var doc = cm.doc, state;
8102
    if (how == null) { how = "add"; }
8103
    if (how == "smart") {
8104
      // Fall back to "prev" when the mode doesn't have an indentation
8105
      // method.
8106
      if (!doc.mode.indent) { how = "prev"; }
8107
      else { state = getContextBefore(cm, n).state; }
8108
    }
8109
 
8110
    var tabSize = cm.options.tabSize;
8111
    var line = getLine(doc, n), curSpace = countColumn(line.text, null, tabSize);
8112
    if (line.stateAfter) { line.stateAfter = null; }
8113
    var curSpaceString = line.text.match(/^\s*/)[0], indentation;
8114
    if (!aggressive && !/\S/.test(line.text)) {
8115
      indentation = 0;
8116
      how = "not";
8117
    } else if (how == "smart") {
8118
      indentation = doc.mode.indent(state, line.text.slice(curSpaceString.length), line.text);
8119
      if (indentation == Pass || indentation > 150) {
8120
        if (!aggressive) { return }
8121
        how = "prev";
8122
      }
8123
    }
8124
    if (how == "prev") {
8125
      if (n > doc.first) { indentation = countColumn(getLine(doc, n-1).text, null, tabSize); }
8126
      else { indentation = 0; }
8127
    } else if (how == "add") {
8128
      indentation = curSpace + cm.options.indentUnit;
8129
    } else if (how == "subtract") {
8130
      indentation = curSpace - cm.options.indentUnit;
8131
    } else if (typeof how == "number") {
8132
      indentation = curSpace + how;
8133
    }
8134
    indentation = Math.max(0, indentation);
8135
 
8136
    var indentString = "", pos = 0;
8137
    if (cm.options.indentWithTabs)
8138
      { for (var i = Math.floor(indentation / tabSize); i; --i) {pos += tabSize; indentString += "\t";} }
8139
    if (pos < indentation) { indentString += spaceStr(indentation - pos); }
8140
 
8141
    if (indentString != curSpaceString) {
8142
      replaceRange(doc, indentString, Pos(n, 0), Pos(n, curSpaceString.length), "+input");
8143
      line.stateAfter = null;
8144
      return true
8145
    } else {
8146
      // Ensure that, if the cursor was in the whitespace at the start
8147
      // of the line, it is moved to the end of that space.
8148
      for (var i$1 = 0; i$1 < doc.sel.ranges.length; i$1++) {
8149
        var range = doc.sel.ranges[i$1];
8150
        if (range.head.line == n && range.head.ch < curSpaceString.length) {
8151
          var pos$1 = Pos(n, curSpaceString.length);
8152
          replaceOneSelection(doc, i$1, new Range(pos$1, pos$1));
8153
          break
8154
        }
8155
      }
8156
    }
8157
  }
8158
 
8159
  // This will be set to a {lineWise: bool, text: [string]} object, so
8160
  // that, when pasting, we know what kind of selections the copied
8161
  // text was made out of.
8162
  var lastCopied = null;
8163
 
8164
  function setLastCopied(newLastCopied) {
8165
    lastCopied = newLastCopied;
8166
  }
8167
 
8168
  function applyTextInput(cm, inserted, deleted, sel, origin) {
8169
    var doc = cm.doc;
8170
    cm.display.shift = false;
8171
    if (!sel) { sel = doc.sel; }
8172
 
8173
    var recent = +new Date - 200;
8174
    var paste = origin == "paste" || cm.state.pasteIncoming > recent;
8175
    var textLines = splitLinesAuto(inserted), multiPaste = null;
8176
    // When pasting N lines into N selections, insert one line per selection
8177
    if (paste && sel.ranges.length > 1) {
8178
      if (lastCopied && lastCopied.text.join("\n") == inserted) {
8179
        if (sel.ranges.length % lastCopied.text.length == 0) {
8180
          multiPaste = [];
8181
          for (var i = 0; i < lastCopied.text.length; i++)
8182
            { multiPaste.push(doc.splitLines(lastCopied.text[i])); }
8183
        }
8184
      } else if (textLines.length == sel.ranges.length && cm.options.pasteLinesPerSelection) {
8185
        multiPaste = map(textLines, function (l) { return [l]; });
8186
      }
8187
    }
8188
 
8189
    var updateInput = cm.curOp.updateInput;
8190
    // Normal behavior is to insert the new text into every selection
8191
    for (var i$1 = sel.ranges.length - 1; i$1 >= 0; i$1--) {
15152 obado 8192
      var range = sel.ranges[i$1];
8193
      var from = range.from(), to = range.to();
8194
      if (range.empty()) {
14283 obado 8195
        if (deleted && deleted > 0) // Handle deletion
8196
          { from = Pos(from.line, from.ch - deleted); }
8197
        else if (cm.state.overwrite && !paste) // Handle overwrite
8198
          { to = Pos(to.line, Math.min(getLine(doc, to.line).text.length, to.ch + lst(textLines).length)); }
15332 obado 8199
        else if (paste && lastCopied && lastCopied.lineWise && lastCopied.text.join("\n") == textLines.join("\n"))
14283 obado 8200
          { from = to = Pos(from.line, 0); }
8201
      }
8202
      var changeEvent = {from: from, to: to, text: multiPaste ? multiPaste[i$1 % multiPaste.length] : textLines,
8203
                         origin: origin || (paste ? "paste" : cm.state.cutIncoming > recent ? "cut" : "+input")};
8204
      makeChange(cm.doc, changeEvent);
8205
      signalLater(cm, "inputRead", cm, changeEvent);
8206
    }
8207
    if (inserted && !paste)
8208
      { triggerElectric(cm, inserted); }
8209
 
8210
    ensureCursorVisible(cm);
8211
    if (cm.curOp.updateInput < 2) { cm.curOp.updateInput = updateInput; }
8212
    cm.curOp.typing = true;
8213
    cm.state.pasteIncoming = cm.state.cutIncoming = -1;
8214
  }
8215
 
8216
  function handlePaste(e, cm) {
8217
    var pasted = e.clipboardData && e.clipboardData.getData("Text");
8218
    if (pasted) {
8219
      e.preventDefault();
16859 obado 8220
      if (!cm.isReadOnly() && !cm.options.disableInput && cm.hasFocus())
14283 obado 8221
        { runInOp(cm, function () { return applyTextInput(cm, pasted, 0, null, "paste"); }); }
8222
      return true
8223
    }
8224
  }
8225
 
8226
  function triggerElectric(cm, inserted) {
8227
    // When an 'electric' character is inserted, immediately trigger a reindent
8228
    if (!cm.options.electricChars || !cm.options.smartIndent) { return }
8229
    var sel = cm.doc.sel;
8230
 
8231
    for (var i = sel.ranges.length - 1; i >= 0; i--) {
15152 obado 8232
      var range = sel.ranges[i];
8233
      if (range.head.ch > 100 || (i && sel.ranges[i - 1].head.line == range.head.line)) { continue }
8234
      var mode = cm.getModeAt(range.head);
14283 obado 8235
      var indented = false;
8236
      if (mode.electricChars) {
8237
        for (var j = 0; j < mode.electricChars.length; j++)
8238
          { if (inserted.indexOf(mode.electricChars.charAt(j)) > -1) {
15152 obado 8239
            indented = indentLine(cm, range.head.line, "smart");
14283 obado 8240
            break
8241
          } }
8242
      } else if (mode.electricInput) {
15152 obado 8243
        if (mode.electricInput.test(getLine(cm.doc, range.head.line).text.slice(0, range.head.ch)))
8244
          { indented = indentLine(cm, range.head.line, "smart"); }
14283 obado 8245
      }
15152 obado 8246
      if (indented) { signalLater(cm, "electricInput", cm, range.head.line); }
14283 obado 8247
    }
8248
  }
8249
 
8250
  function copyableRanges(cm) {
8251
    var text = [], ranges = [];
8252
    for (var i = 0; i < cm.doc.sel.ranges.length; i++) {
8253
      var line = cm.doc.sel.ranges[i].head.line;
8254
      var lineRange = {anchor: Pos(line, 0), head: Pos(line + 1, 0)};
8255
      ranges.push(lineRange);
8256
      text.push(cm.getRange(lineRange.anchor, lineRange.head));
8257
    }
8258
    return {text: text, ranges: ranges}
8259
  }
8260
 
8261
  function disableBrowserMagic(field, spellcheck, autocorrect, autocapitalize) {
17702 obado 8262
    field.setAttribute("autocorrect", autocorrect ? "on" : "off");
8263
    field.setAttribute("autocapitalize", autocapitalize ? "on" : "off");
14283 obado 8264
    field.setAttribute("spellcheck", !!spellcheck);
8265
  }
8266
 
8267
  function hiddenTextarea() {
16493 obado 8268
    var te = elt("textarea", null, null, "position: absolute; bottom: -1em; padding: 0; width: 1px; height: 1em; min-height: 1em; outline: none");
14283 obado 8269
    var div = elt("div", [te], null, "overflow: hidden; position: relative; width: 3px; height: 0px;");
8270
    // The textarea is kept positioned near the cursor to prevent the
8271
    // fact that it'll be scrolled into view on input from scrolling
8272
    // our fake cursor out of view. On webkit, when wrap=off, paste is
8273
    // very slow. So make the area wide instead.
8274
    if (webkit) { te.style.width = "1000px"; }
8275
    else { te.setAttribute("wrap", "off"); }
8276
    // If border: 0; -- iOS fails to open keyboard (issue #1287)
8277
    if (ios) { te.style.border = "1px solid black"; }
8278
    return div
8279
  }
8280
 
8281
  // The publicly visible API. Note that methodOp(f) means
8282
  // 'wrap f in an operation, performed on its `this` parameter'.
8283
 
8284
  // This is not the complete set of editor methods. Most of the
8285
  // methods defined on the Doc type are also injected into
8286
  // CodeMirror.prototype, for backwards compatibility and
8287
  // convenience.
8288
 
8289
  function addEditorMethods(CodeMirror) {
8290
    var optionHandlers = CodeMirror.optionHandlers;
8291
 
8292
    var helpers = CodeMirror.helpers = {};
8293
 
8294
    CodeMirror.prototype = {
8295
      constructor: CodeMirror,
17702 obado 8296
      focus: function(){win(this).focus(); this.display.input.focus();},
14283 obado 8297
 
8298
      setOption: function(option, value) {
8299
        var options = this.options, old = options[option];
8300
        if (options[option] == value && option != "mode") { return }
8301
        options[option] = value;
8302
        if (optionHandlers.hasOwnProperty(option))
8303
          { operation(this, optionHandlers[option])(this, value, old); }
8304
        signal(this, "optionChange", this, option);
8305
      },
8306
 
8307
      getOption: function(option) {return this.options[option]},
8308
      getDoc: function() {return this.doc},
8309
 
15152 obado 8310
      addKeyMap: function(map, bottom) {
8311
        this.state.keyMaps[bottom ? "push" : "unshift"](getKeyMap(map));
14283 obado 8312
      },
15152 obado 8313
      removeKeyMap: function(map) {
14283 obado 8314
        var maps = this.state.keyMaps;
8315
        for (var i = 0; i < maps.length; ++i)
15152 obado 8316
          { if (maps[i] == map || maps[i].name == map) {
14283 obado 8317
            maps.splice(i, 1);
8318
            return true
8319
          } }
8320
      },
8321
 
8322
      addOverlay: methodOp(function(spec, options) {
8323
        var mode = spec.token ? spec : CodeMirror.getMode(this.options, spec);
8324
        if (mode.startState) { throw new Error("Overlays may not be stateful.") }
8325
        insertSorted(this.state.overlays,
8326
                     {mode: mode, modeSpec: spec, opaque: options && options.opaque,
8327
                      priority: (options && options.priority) || 0},
8328
                     function (overlay) { return overlay.priority; });
8329
        this.state.modeGen++;
8330
        regChange(this);
8331
      }),
8332
      removeOverlay: methodOp(function(spec) {
8333
        var overlays = this.state.overlays;
8334
        for (var i = 0; i < overlays.length; ++i) {
8335
          var cur = overlays[i].modeSpec;
8336
          if (cur == spec || typeof spec == "string" && cur.name == spec) {
8337
            overlays.splice(i, 1);
15152 obado 8338
            this.state.modeGen++;
8339
            regChange(this);
14283 obado 8340
            return
8341
          }
8342
        }
8343
      }),
8344
 
8345
      indentLine: methodOp(function(n, dir, aggressive) {
8346
        if (typeof dir != "string" && typeof dir != "number") {
8347
          if (dir == null) { dir = this.options.smartIndent ? "smart" : "prev"; }
8348
          else { dir = dir ? "add" : "subtract"; }
8349
        }
8350
        if (isLine(this.doc, n)) { indentLine(this, n, dir, aggressive); }
8351
      }),
8352
      indentSelection: methodOp(function(how) {
8353
        var ranges = this.doc.sel.ranges, end = -1;
8354
        for (var i = 0; i < ranges.length; i++) {
15152 obado 8355
          var range = ranges[i];
8356
          if (!range.empty()) {
8357
            var from = range.from(), to = range.to();
14283 obado 8358
            var start = Math.max(end, from.line);
15152 obado 8359
            end = Math.min(this.lastLine(), to.line - (to.ch ? 0 : 1)) + 1;
14283 obado 8360
            for (var j = start; j < end; ++j)
15152 obado 8361
              { indentLine(this, j, how); }
8362
            var newRanges = this.doc.sel.ranges;
14283 obado 8363
            if (from.ch == 0 && ranges.length == newRanges.length && newRanges[i].from().ch > 0)
15152 obado 8364
              { replaceOneSelection(this.doc, i, new Range(from, newRanges[i].to()), sel_dontScroll); }
8365
          } else if (range.head.line > end) {
8366
            indentLine(this, range.head.line, how, true);
8367
            end = range.head.line;
8368
            if (i == this.doc.sel.primIndex) { ensureCursorVisible(this); }
14283 obado 8369
          }
8370
        }
8371
      }),
8372
 
8373
      // Fetch the parser token for a given character. Useful for hacks
8374
      // that want to inspect the mode state (say, for completion).
8375
      getTokenAt: function(pos, precise) {
8376
        return takeToken(this, pos, precise)
8377
      },
8378
 
8379
      getLineTokens: function(line, precise) {
8380
        return takeToken(this, Pos(line), precise, true)
8381
      },
8382
 
8383
      getTokenTypeAt: function(pos) {
8384
        pos = clipPos(this.doc, pos);
8385
        var styles = getLineStyles(this, getLine(this.doc, pos.line));
8386
        var before = 0, after = (styles.length - 1) / 2, ch = pos.ch;
8387
        var type;
8388
        if (ch == 0) { type = styles[2]; }
8389
        else { for (;;) {
8390
          var mid = (before + after) >> 1;
8391
          if ((mid ? styles[mid * 2 - 1] : 0) >= ch) { after = mid; }
8392
          else if (styles[mid * 2 + 1] < ch) { before = mid + 1; }
8393
          else { type = styles[mid * 2 + 2]; break }
8394
        } }
8395
        var cut = type ? type.indexOf("overlay ") : -1;
8396
        return cut < 0 ? type : cut == 0 ? null : type.slice(0, cut - 1)
8397
      },
8398
 
8399
      getModeAt: function(pos) {
8400
        var mode = this.doc.mode;
8401
        if (!mode.innerMode) { return mode }
8402
        return CodeMirror.innerMode(mode, this.getTokenAt(pos).state).mode
8403
      },
8404
 
8405
      getHelper: function(pos, type) {
8406
        return this.getHelpers(pos, type)[0]
8407
      },
8408
 
8409
      getHelpers: function(pos, type) {
8410
        var found = [];
8411
        if (!helpers.hasOwnProperty(type)) { return found }
8412
        var help = helpers[type], mode = this.getModeAt(pos);
8413
        if (typeof mode[type] == "string") {
8414
          if (help[mode[type]]) { found.push(help[mode[type]]); }
8415
        } else if (mode[type]) {
8416
          for (var i = 0; i < mode[type].length; i++) {
8417
            var val = help[mode[type][i]];
8418
            if (val) { found.push(val); }
8419
          }
8420
        } else if (mode.helperType && help[mode.helperType]) {
8421
          found.push(help[mode.helperType]);
8422
        } else if (help[mode.name]) {
8423
          found.push(help[mode.name]);
8424
        }
8425
        for (var i$1 = 0; i$1 < help._global.length; i$1++) {
8426
          var cur = help._global[i$1];
15152 obado 8427
          if (cur.pred(mode, this) && indexOf(found, cur.val) == -1)
14283 obado 8428
            { found.push(cur.val); }
8429
        }
8430
        return found
8431
      },
8432
 
8433
      getStateAfter: function(line, precise) {
8434
        var doc = this.doc;
8435
        line = clipLine(doc, line == null ? doc.first + doc.size - 1: line);
8436
        return getContextBefore(this, line + 1, precise).state
8437
      },
8438
 
8439
      cursorCoords: function(start, mode) {
15152 obado 8440
        var pos, range = this.doc.sel.primary();
8441
        if (start == null) { pos = range.head; }
14283 obado 8442
        else if (typeof start == "object") { pos = clipPos(this.doc, start); }
15152 obado 8443
        else { pos = start ? range.from() : range.to(); }
14283 obado 8444
        return cursorCoords(this, pos, mode || "page")
8445
      },
8446
 
8447
      charCoords: function(pos, mode) {
8448
        return charCoords(this, clipPos(this.doc, pos), mode || "page")
8449
      },
8450
 
8451
      coordsChar: function(coords, mode) {
8452
        coords = fromCoordSystem(this, coords, mode || "page");
8453
        return coordsChar(this, coords.left, coords.top)
8454
      },
8455
 
8456
      lineAtHeight: function(height, mode) {
8457
        height = fromCoordSystem(this, {top: height, left: 0}, mode || "page").top;
8458
        return lineAtHeight(this.doc, height + this.display.viewOffset)
8459
      },
8460
      heightAtLine: function(line, mode, includeWidgets) {
8461
        var end = false, lineObj;
8462
        if (typeof line == "number") {
8463
          var last = this.doc.first + this.doc.size - 1;
8464
          if (line < this.doc.first) { line = this.doc.first; }
8465
          else if (line > last) { line = last; end = true; }
8466
          lineObj = getLine(this.doc, line);
8467
        } else {
8468
          lineObj = line;
8469
        }
8470
        return intoCoordSystem(this, lineObj, {top: 0, left: 0}, mode || "page", includeWidgets || end).top +
8471
          (end ? this.doc.height - heightAtLine(lineObj) : 0)
8472
      },
8473
 
8474
      defaultTextHeight: function() { return textHeight(this.display) },
8475
      defaultCharWidth: function() { return charWidth(this.display) },
8476
 
8477
      getViewport: function() { return {from: this.display.viewFrom, to: this.display.viewTo}},
8478
 
8479
      addWidget: function(pos, node, scroll, vert, horiz) {
8480
        var display = this.display;
8481
        pos = cursorCoords(this, clipPos(this.doc, pos));
8482
        var top = pos.bottom, left = pos.left;
8483
        node.style.position = "absolute";
8484
        node.setAttribute("cm-ignore-events", "true");
8485
        this.display.input.setUneditable(node);
8486
        display.sizer.appendChild(node);
8487
        if (vert == "over") {
8488
          top = pos.top;
8489
        } else if (vert == "above" || vert == "near") {
8490
          var vspace = Math.max(display.wrapper.clientHeight, this.doc.height),
8491
          hspace = Math.max(display.sizer.clientWidth, display.lineSpace.clientWidth);
8492
          // Default to positioning above (if specified and possible); otherwise default to positioning below
8493
          if ((vert == 'above' || pos.bottom + node.offsetHeight > vspace) && pos.top > node.offsetHeight)
8494
            { top = pos.top - node.offsetHeight; }
8495
          else if (pos.bottom + node.offsetHeight <= vspace)
8496
            { top = pos.bottom; }
8497
          if (left + node.offsetWidth > hspace)
8498
            { left = hspace - node.offsetWidth; }
8499
        }
8500
        node.style.top = top + "px";
8501
        node.style.left = node.style.right = "";
8502
        if (horiz == "right") {
8503
          left = display.sizer.clientWidth - node.offsetWidth;
8504
          node.style.right = "0px";
8505
        } else {
8506
          if (horiz == "left") { left = 0; }
8507
          else if (horiz == "middle") { left = (display.sizer.clientWidth - node.offsetWidth) / 2; }
8508
          node.style.left = left + "px";
8509
        }
8510
        if (scroll)
8511
          { scrollIntoView(this, {left: left, top: top, right: left + node.offsetWidth, bottom: top + node.offsetHeight}); }
8512
      },
8513
 
8514
      triggerOnKeyDown: methodOp(onKeyDown),
8515
      triggerOnKeyPress: methodOp(onKeyPress),
8516
      triggerOnKeyUp: onKeyUp,
8517
      triggerOnMouseDown: methodOp(onMouseDown),
8518
 
8519
      execCommand: function(cmd) {
8520
        if (commands.hasOwnProperty(cmd))
8521
          { return commands[cmd].call(null, this) }
8522
      },
8523
 
8524
      triggerElectric: methodOp(function(text) { triggerElectric(this, text); }),
8525
 
8526
      findPosH: function(from, amount, unit, visually) {
8527
        var dir = 1;
8528
        if (amount < 0) { dir = -1; amount = -amount; }
8529
        var cur = clipPos(this.doc, from);
8530
        for (var i = 0; i < amount; ++i) {
15152 obado 8531
          cur = findPosH(this.doc, cur, dir, unit, visually);
14283 obado 8532
          if (cur.hitSide) { break }
8533
        }
8534
        return cur
8535
      },
8536
 
8537
      moveH: methodOp(function(dir, unit) {
8538
        var this$1 = this;
8539
 
15152 obado 8540
        this.extendSelectionsBy(function (range) {
8541
          if (this$1.display.shift || this$1.doc.extend || range.empty())
8542
            { return findPosH(this$1.doc, range.head, dir, unit, this$1.options.rtlMoveVisually) }
14283 obado 8543
          else
15152 obado 8544
            { return dir < 0 ? range.from() : range.to() }
14283 obado 8545
        }, sel_move);
8546
      }),
8547
 
8548
      deleteH: methodOp(function(dir, unit) {
8549
        var sel = this.doc.sel, doc = this.doc;
8550
        if (sel.somethingSelected())
8551
          { doc.replaceSelection("", null, "+delete"); }
8552
        else
15152 obado 8553
          { deleteNearSelection(this, function (range) {
8554
            var other = findPosH(doc, range.head, dir, unit, false);
8555
            return dir < 0 ? {from: other, to: range.head} : {from: range.head, to: other}
14283 obado 8556
          }); }
8557
      }),
8558
 
8559
      findPosV: function(from, amount, unit, goalColumn) {
8560
        var dir = 1, x = goalColumn;
8561
        if (amount < 0) { dir = -1; amount = -amount; }
8562
        var cur = clipPos(this.doc, from);
8563
        for (var i = 0; i < amount; ++i) {
15152 obado 8564
          var coords = cursorCoords(this, cur, "div");
14283 obado 8565
          if (x == null) { x = coords.left; }
8566
          else { coords.left = x; }
15152 obado 8567
          cur = findPosV(this, coords, dir, unit);
14283 obado 8568
          if (cur.hitSide) { break }
8569
        }
8570
        return cur
8571
      },
8572
 
8573
      moveV: methodOp(function(dir, unit) {
8574
        var this$1 = this;
8575
 
8576
        var doc = this.doc, goals = [];
8577
        var collapse = !this.display.shift && !doc.extend && doc.sel.somethingSelected();
15152 obado 8578
        doc.extendSelectionsBy(function (range) {
14283 obado 8579
          if (collapse)
15152 obado 8580
            { return dir < 0 ? range.from() : range.to() }
8581
          var headPos = cursorCoords(this$1, range.head, "div");
8582
          if (range.goalColumn != null) { headPos.left = range.goalColumn; }
14283 obado 8583
          goals.push(headPos.left);
8584
          var pos = findPosV(this$1, headPos, dir, unit);
15152 obado 8585
          if (unit == "page" && range == doc.sel.primary())
14283 obado 8586
            { addToScrollTop(this$1, charCoords(this$1, pos, "div").top - headPos.top); }
8587
          return pos
8588
        }, sel_move);
8589
        if (goals.length) { for (var i = 0; i < doc.sel.ranges.length; i++)
8590
          { doc.sel.ranges[i].goalColumn = goals[i]; } }
8591
      }),
8592
 
8593
      // Find the word at the given position (as returned by coordsChar).
8594
      findWordAt: function(pos) {
8595
        var doc = this.doc, line = getLine(doc, pos.line).text;
8596
        var start = pos.ch, end = pos.ch;
8597
        if (line) {
8598
          var helper = this.getHelper(pos, "wordChars");
8599
          if ((pos.sticky == "before" || end == line.length) && start) { --start; } else { ++end; }
8600
          var startChar = line.charAt(start);
8601
          var check = isWordChar(startChar, helper)
8602
            ? function (ch) { return isWordChar(ch, helper); }
8603
            : /\s/.test(startChar) ? function (ch) { return /\s/.test(ch); }
8604
            : function (ch) { return (!/\s/.test(ch) && !isWordChar(ch)); };
8605
          while (start > 0 && check(line.charAt(start - 1))) { --start; }
8606
          while (end < line.length && check(line.charAt(end))) { ++end; }
8607
        }
8608
        return new Range(Pos(pos.line, start), Pos(pos.line, end))
8609
      },
8610
 
8611
      toggleOverwrite: function(value) {
8612
        if (value != null && value == this.state.overwrite) { return }
8613
        if (this.state.overwrite = !this.state.overwrite)
8614
          { addClass(this.display.cursorDiv, "CodeMirror-overwrite"); }
8615
        else
8616
          { rmClass(this.display.cursorDiv, "CodeMirror-overwrite"); }
8617
 
8618
        signal(this, "overwriteToggle", this, this.state.overwrite);
8619
      },
17702 obado 8620
      hasFocus: function() { return this.display.input.getField() == activeElt(doc(this)) },
14283 obado 8621
      isReadOnly: function() { return !!(this.options.readOnly || this.doc.cantEdit) },
8622
 
8623
      scrollTo: methodOp(function (x, y) { scrollToCoords(this, x, y); }),
8624
      getScrollInfo: function() {
8625
        var scroller = this.display.scroller;
8626
        return {left: scroller.scrollLeft, top: scroller.scrollTop,
8627
                height: scroller.scrollHeight - scrollGap(this) - this.display.barHeight,
8628
                width: scroller.scrollWidth - scrollGap(this) - this.display.barWidth,
8629
                clientHeight: displayHeight(this), clientWidth: displayWidth(this)}
8630
      },
8631
 
15152 obado 8632
      scrollIntoView: methodOp(function(range, margin) {
8633
        if (range == null) {
8634
          range = {from: this.doc.sel.primary().head, to: null};
14283 obado 8635
          if (margin == null) { margin = this.options.cursorScrollMargin; }
15152 obado 8636
        } else if (typeof range == "number") {
8637
          range = {from: Pos(range, 0), to: null};
8638
        } else if (range.from == null) {
8639
          range = {from: range, to: null};
14283 obado 8640
        }
15152 obado 8641
        if (!range.to) { range.to = range.from; }
8642
        range.margin = margin || 0;
14283 obado 8643
 
15152 obado 8644
        if (range.from.line != null) {
8645
          scrollToRange(this, range);
14283 obado 8646
        } else {
15152 obado 8647
          scrollToCoordsRange(this, range.from, range.to, range.margin);
14283 obado 8648
        }
8649
      }),
8650
 
8651
      setSize: methodOp(function(width, height) {
8652
        var this$1 = this;
8653
 
8654
        var interpret = function (val) { return typeof val == "number" || /^\d+$/.test(String(val)) ? val + "px" : val; };
8655
        if (width != null) { this.display.wrapper.style.width = interpret(width); }
8656
        if (height != null) { this.display.wrapper.style.height = interpret(height); }
8657
        if (this.options.lineWrapping) { clearLineMeasurementCache(this); }
15152 obado 8658
        var lineNo = this.display.viewFrom;
8659
        this.doc.iter(lineNo, this.display.viewTo, function (line) {
14283 obado 8660
          if (line.widgets) { for (var i = 0; i < line.widgets.length; i++)
15152 obado 8661
            { if (line.widgets[i].noHScroll) { regLineChange(this$1, lineNo, "widget"); break } } }
8662
          ++lineNo;
14283 obado 8663
        });
8664
        this.curOp.forceUpdate = true;
8665
        signal(this, "refresh", this);
8666
      }),
8667
 
8668
      operation: function(f){return runInOp(this, f)},
8669
      startOperation: function(){return startOperation(this)},
8670
      endOperation: function(){return endOperation(this)},
8671
 
8672
      refresh: methodOp(function() {
8673
        var oldHeight = this.display.cachedTextHeight;
8674
        regChange(this);
8675
        this.curOp.forceUpdate = true;
8676
        clearCaches(this);
8677
        scrollToCoords(this, this.doc.scrollLeft, this.doc.scrollTop);
8678
        updateGutterSpace(this.display);
15152 obado 8679
        if (oldHeight == null || Math.abs(oldHeight - textHeight(this.display)) > .5 || this.options.lineWrapping)
14283 obado 8680
          { estimateLineHeights(this); }
8681
        signal(this, "refresh", this);
8682
      }),
8683
 
8684
      swapDoc: methodOp(function(doc) {
8685
        var old = this.doc;
8686
        old.cm = null;
8687
        // Cancel the current text selection if any (#5821)
8688
        if (this.state.selectingText) { this.state.selectingText(); }
8689
        attachDoc(this, doc);
8690
        clearCaches(this);
8691
        this.display.input.reset();
8692
        scrollToCoords(this, doc.scrollLeft, doc.scrollTop);
8693
        this.curOp.forceScroll = true;
8694
        signalLater(this, "swapDoc", this, old);
8695
        return old
8696
      }),
8697
 
8698
      phrase: function(phraseText) {
8699
        var phrases = this.options.phrases;
8700
        return phrases && Object.prototype.hasOwnProperty.call(phrases, phraseText) ? phrases[phraseText] : phraseText
8701
      },
8702
 
8703
      getInputField: function(){return this.display.input.getField()},
8704
      getWrapperElement: function(){return this.display.wrapper},
8705
      getScrollerElement: function(){return this.display.scroller},
8706
      getGutterElement: function(){return this.display.gutters}
8707
    };
8708
    eventMixin(CodeMirror);
8709
 
8710
    CodeMirror.registerHelper = function(type, name, value) {
8711
      if (!helpers.hasOwnProperty(type)) { helpers[type] = CodeMirror[type] = {_global: []}; }
8712
      helpers[type][name] = value;
8713
    };
8714
    CodeMirror.registerGlobalHelper = function(type, name, predicate, value) {
8715
      CodeMirror.registerHelper(type, name, value);
8716
      helpers[type]._global.push({pred: predicate, val: value});
8717
    };
8718
  }
8719
 
8720
  // Used for horizontal relative motion. Dir is -1 or 1 (left or
16493 obado 8721
  // right), unit can be "codepoint", "char", "column" (like char, but
8722
  // doesn't cross line boundaries), "word" (across next word), or
8723
  // "group" (to the start of next group of word or
8724
  // non-word-non-whitespace chars). The visually param controls
8725
  // whether, in right-to-left text, direction 1 means to move towards
8726
  // the next index in the string, or towards the character to the right
8727
  // of the current position. The resulting position will have a
8728
  // hitSide=true property if it reached the end of the document.
14283 obado 8729
  function findPosH(doc, pos, dir, unit, visually) {
8730
    var oldPos = pos;
8731
    var origDir = dir;
8732
    var lineObj = getLine(doc, pos.line);
15152 obado 8733
    var lineDir = visually && doc.direction == "rtl" ? -dir : dir;
14283 obado 8734
    function findNextLine() {
15152 obado 8735
      var l = pos.line + lineDir;
14283 obado 8736
      if (l < doc.first || l >= doc.first + doc.size) { return false }
8737
      pos = new Pos(l, pos.ch, pos.sticky);
8738
      return lineObj = getLine(doc, l)
8739
    }
8740
    function moveOnce(boundToLine) {
8741
      var next;
16493 obado 8742
      if (unit == "codepoint") {
8743
        var ch = lineObj.text.charCodeAt(pos.ch + (dir > 0 ? 0 : -1));
8744
        if (isNaN(ch)) {
8745
          next = null;
8746
        } else {
8747
          var astral = dir > 0 ? ch >= 0xD800 && ch < 0xDC00 : ch >= 0xDC00 && ch < 0xDFFF;
8748
          next = new Pos(pos.line, Math.max(0, Math.min(lineObj.text.length, pos.ch + dir * (astral ? 2 : 1))), -dir);
8749
        }
8750
      } else if (visually) {
14283 obado 8751
        next = moveVisually(doc.cm, lineObj, pos, dir);
8752
      } else {
8753
        next = moveLogically(lineObj, pos, dir);
8754
      }
8755
      if (next == null) {
8756
        if (!boundToLine && findNextLine())
15152 obado 8757
          { pos = endOfLine(visually, doc.cm, lineObj, pos.line, lineDir); }
14283 obado 8758
        else
8759
          { return false }
8760
      } else {
8761
        pos = next;
8762
      }
8763
      return true
8764
    }
8765
 
16493 obado 8766
    if (unit == "char" || unit == "codepoint") {
14283 obado 8767
      moveOnce();
8768
    } else if (unit == "column") {
8769
      moveOnce(true);
8770
    } else if (unit == "word" || unit == "group") {
8771
      var sawType = null, group = unit == "group";
8772
      var helper = doc.cm && doc.cm.getHelper(pos, "wordChars");
8773
      for (var first = true;; first = false) {
8774
        if (dir < 0 && !moveOnce(!first)) { break }
8775
        var cur = lineObj.text.charAt(pos.ch) || "\n";
8776
        var type = isWordChar(cur, helper) ? "w"
8777
          : group && cur == "\n" ? "n"
8778
          : !group || /\s/.test(cur) ? null
8779
          : "p";
8780
        if (group && !first && !type) { type = "s"; }
8781
        if (sawType && sawType != type) {
8782
          if (dir < 0) {dir = 1; moveOnce(); pos.sticky = "after";}
8783
          break
8784
        }
8785
 
8786
        if (type) { sawType = type; }
8787
        if (dir > 0 && !moveOnce(!first)) { break }
8788
      }
8789
    }
8790
    var result = skipAtomic(doc, pos, oldPos, origDir, true);
8791
    if (equalCursorPos(oldPos, result)) { result.hitSide = true; }
8792
    return result
8793
  }
8794
 
8795
  // For relative vertical movement. Dir may be -1 or 1. Unit can be
8796
  // "page" or "line". The resulting position will have a hitSide=true
8797
  // property if it reached the end of the document.
8798
  function findPosV(cm, pos, dir, unit) {
8799
    var doc = cm.doc, x = pos.left, y;
8800
    if (unit == "page") {
17702 obado 8801
      var pageSize = Math.min(cm.display.wrapper.clientHeight, win(cm).innerHeight || doc(cm).documentElement.clientHeight);
14283 obado 8802
      var moveAmount = Math.max(pageSize - .5 * textHeight(cm.display), 3);
8803
      y = (dir > 0 ? pos.bottom : pos.top) + dir * moveAmount;
8804
 
8805
    } else if (unit == "line") {
8806
      y = dir > 0 ? pos.bottom + 3 : pos.top - 3;
8807
    }
8808
    var target;
8809
    for (;;) {
8810
      target = coordsChar(cm, x, y);
8811
      if (!target.outside) { break }
8812
      if (dir < 0 ? y <= 0 : y >= doc.height) { target.hitSide = true; break }
8813
      y += dir * 5;
8814
    }
8815
    return target
8816
  }
8817
 
8818
  // CONTENTEDITABLE INPUT STYLE
8819
 
8820
  var ContentEditableInput = function(cm) {
8821
    this.cm = cm;
8822
    this.lastAnchorNode = this.lastAnchorOffset = this.lastFocusNode = this.lastFocusOffset = null;
8823
    this.polling = new Delayed();
8824
    this.composing = null;
8825
    this.gracePeriod = false;
8826
    this.readDOMTimeout = null;
8827
  };
8828
 
8829
  ContentEditableInput.prototype.init = function (display) {
8830
      var this$1 = this;
8831
 
8832
    var input = this, cm = input.cm;
8833
    var div = input.div = display.lineDiv;
16493 obado 8834
    div.contentEditable = true;
14283 obado 8835
    disableBrowserMagic(div, cm.options.spellcheck, cm.options.autocorrect, cm.options.autocapitalize);
8836
 
15152 obado 8837
    function belongsToInput(e) {
8838
      for (var t = e.target; t; t = t.parentNode) {
8839
        if (t == div) { return true }
8840
        if (/\bCodeMirror-(?:line)?widget\b/.test(t.className)) { break }
8841
      }
8842
      return false
8843
    }
8844
 
14283 obado 8845
    on(div, "paste", function (e) {
15152 obado 8846
      if (!belongsToInput(e) || signalDOMEvent(cm, e) || handlePaste(e, cm)) { return }
14283 obado 8847
      // IE doesn't fire input events, so we schedule a read for the pasted content in this way
8848
      if (ie_version <= 11) { setTimeout(operation(cm, function () { return this$1.updateFromDOM(); }), 20); }
8849
    });
8850
 
8851
    on(div, "compositionstart", function (e) {
8852
      this$1.composing = {data: e.data, done: false};
8853
    });
8854
    on(div, "compositionupdate", function (e) {
8855
      if (!this$1.composing) { this$1.composing = {data: e.data, done: false}; }
8856
    });
8857
    on(div, "compositionend", function (e) {
8858
      if (this$1.composing) {
8859
        if (e.data != this$1.composing.data) { this$1.readFromDOMSoon(); }
8860
        this$1.composing.done = true;
8861
      }
8862
    });
8863
 
8864
    on(div, "touchstart", function () { return input.forceCompositionEnd(); });
8865
 
8866
    on(div, "input", function () {
8867
      if (!this$1.composing) { this$1.readFromDOMSoon(); }
8868
    });
8869
 
8870
    function onCopyCut(e) {
15152 obado 8871
      if (!belongsToInput(e) || signalDOMEvent(cm, e)) { return }
14283 obado 8872
      if (cm.somethingSelected()) {
8873
        setLastCopied({lineWise: false, text: cm.getSelections()});
8874
        if (e.type == "cut") { cm.replaceSelection("", null, "cut"); }
8875
      } else if (!cm.options.lineWiseCopyCut) {
8876
        return
8877
      } else {
8878
        var ranges = copyableRanges(cm);
8879
        setLastCopied({lineWise: true, text: ranges.text});
8880
        if (e.type == "cut") {
8881
          cm.operation(function () {
8882
            cm.setSelections(ranges.ranges, 0, sel_dontScroll);
8883
            cm.replaceSelection("", null, "cut");
8884
          });
8885
        }
8886
      }
8887
      if (e.clipboardData) {
8888
        e.clipboardData.clearData();
8889
        var content = lastCopied.text.join("\n");
8890
        // iOS exposes the clipboard API, but seems to discard content inserted into it
8891
        e.clipboardData.setData("Text", content);
8892
        if (e.clipboardData.getData("Text") == content) {
8893
          e.preventDefault();
8894
          return
8895
        }
8896
      }
8897
      // Old-fashioned briefly-focus-a-textarea hack
8898
      var kludge = hiddenTextarea(), te = kludge.firstChild;
17702 obado 8899
      disableBrowserMagic(te);
14283 obado 8900
      cm.display.lineSpace.insertBefore(kludge, cm.display.lineSpace.firstChild);
8901
      te.value = lastCopied.text.join("\n");
17702 obado 8902
      var hadFocus = activeElt(div.ownerDocument);
14283 obado 8903
      selectInput(te);
8904
      setTimeout(function () {
8905
        cm.display.lineSpace.removeChild(kludge);
8906
        hadFocus.focus();
8907
        if (hadFocus == div) { input.showPrimarySelection(); }
8908
      }, 50);
8909
    }
8910
    on(div, "copy", onCopyCut);
8911
    on(div, "cut", onCopyCut);
8912
  };
8913
 
15152 obado 8914
  ContentEditableInput.prototype.screenReaderLabelChanged = function (label) {
8915
    // Label for screenreaders, accessibility
8916
    if(label) {
8917
      this.div.setAttribute('aria-label', label);
8918
    } else {
8919
      this.div.removeAttribute('aria-label');
8920
    }
8921
  };
8922
 
14283 obado 8923
  ContentEditableInput.prototype.prepareSelection = function () {
8924
    var result = prepareSelection(this.cm, false);
17702 obado 8925
    result.focus = activeElt(this.div.ownerDocument) == this.div;
14283 obado 8926
    return result
8927
  };
8928
 
8929
  ContentEditableInput.prototype.showSelection = function (info, takeFocus) {
8930
    if (!info || !this.cm.display.view.length) { return }
8931
    if (info.focus || takeFocus) { this.showPrimarySelection(); }
8932
    this.showMultipleSelections(info);
8933
  };
8934
 
8935
  ContentEditableInput.prototype.getSelection = function () {
8936
    return this.cm.display.wrapper.ownerDocument.getSelection()
8937
  };
8938
 
8939
  ContentEditableInput.prototype.showPrimarySelection = function () {
8940
    var sel = this.getSelection(), cm = this.cm, prim = cm.doc.sel.primary();
8941
    var from = prim.from(), to = prim.to();
8942
 
8943
    if (cm.display.viewTo == cm.display.viewFrom || from.line >= cm.display.viewTo || to.line < cm.display.viewFrom) {
8944
      sel.removeAllRanges();
8945
      return
8946
    }
8947
 
8948
    var curAnchor = domToPos(cm, sel.anchorNode, sel.anchorOffset);
8949
    var curFocus = domToPos(cm, sel.focusNode, sel.focusOffset);
8950
    if (curAnchor && !curAnchor.bad && curFocus && !curFocus.bad &&
8951
        cmp(minPos(curAnchor, curFocus), from) == 0 &&
8952
        cmp(maxPos(curAnchor, curFocus), to) == 0)
8953
      { return }
8954
 
8955
    var view = cm.display.view;
8956
    var start = (from.line >= cm.display.viewFrom && posToDOM(cm, from)) ||
8957
        {node: view[0].measure.map[2], offset: 0};
8958
    var end = to.line < cm.display.viewTo && posToDOM(cm, to);
8959
    if (!end) {
8960
      var measure = view[view.length - 1].measure;
15152 obado 8961
      var map = measure.maps ? measure.maps[measure.maps.length - 1] : measure.map;
8962
      end = {node: map[map.length - 1], offset: map[map.length - 2] - map[map.length - 3]};
14283 obado 8963
    }
8964
 
8965
    if (!start || !end) {
8966
      sel.removeAllRanges();
8967
      return
8968
    }
8969
 
8970
    var old = sel.rangeCount && sel.getRangeAt(0), rng;
8971
    try { rng = range(start.node, start.offset, end.offset, end.node); }
8972
    catch(e) {} // Our model of the DOM might be outdated, in which case the range we try to set can be impossible
8973
    if (rng) {
8974
      if (!gecko && cm.state.focused) {
8975
        sel.collapse(start.node, start.offset);
8976
        if (!rng.collapsed) {
8977
          sel.removeAllRanges();
8978
          sel.addRange(rng);
8979
        }
8980
      } else {
8981
        sel.removeAllRanges();
8982
        sel.addRange(rng);
8983
      }
8984
      if (old && sel.anchorNode == null) { sel.addRange(old); }
8985
      else if (gecko) { this.startGracePeriod(); }
8986
    }
8987
    this.rememberSelection();
8988
  };
8989
 
8990
  ContentEditableInput.prototype.startGracePeriod = function () {
8991
      var this$1 = this;
8992
 
8993
    clearTimeout(this.gracePeriod);
8994
    this.gracePeriod = setTimeout(function () {
8995
      this$1.gracePeriod = false;
8996
      if (this$1.selectionChanged())
8997
        { this$1.cm.operation(function () { return this$1.cm.curOp.selectionChanged = true; }); }
8998
    }, 20);
8999
  };
9000
 
9001
  ContentEditableInput.prototype.showMultipleSelections = function (info) {
9002
    removeChildrenAndAdd(this.cm.display.cursorDiv, info.cursors);
9003
    removeChildrenAndAdd(this.cm.display.selectionDiv, info.selection);
9004
  };
9005
 
9006
  ContentEditableInput.prototype.rememberSelection = function () {
9007
    var sel = this.getSelection();
9008
    this.lastAnchorNode = sel.anchorNode; this.lastAnchorOffset = sel.anchorOffset;
9009
    this.lastFocusNode = sel.focusNode; this.lastFocusOffset = sel.focusOffset;
9010
  };
9011
 
9012
  ContentEditableInput.prototype.selectionInEditor = function () {
9013
    var sel = this.getSelection();
9014
    if (!sel.rangeCount) { return false }
9015
    var node = sel.getRangeAt(0).commonAncestorContainer;
9016
    return contains(this.div, node)
9017
  };
9018
 
9019
  ContentEditableInput.prototype.focus = function () {
9020
    if (this.cm.options.readOnly != "nocursor") {
17702 obado 9021
      if (!this.selectionInEditor() || activeElt(this.div.ownerDocument) != this.div)
14283 obado 9022
        { this.showSelection(this.prepareSelection(), true); }
9023
      this.div.focus();
9024
    }
9025
  };
9026
  ContentEditableInput.prototype.blur = function () { this.div.blur(); };
9027
  ContentEditableInput.prototype.getField = function () { return this.div };
9028
 
9029
  ContentEditableInput.prototype.supportsTouch = function () { return true };
9030
 
9031
  ContentEditableInput.prototype.receivedFocus = function () {
16493 obado 9032
      var this$1 = this;
9033
 
14283 obado 9034
    var input = this;
9035
    if (this.selectionInEditor())
16493 obado 9036
      { setTimeout(function () { return this$1.pollSelection(); }, 20); }
14283 obado 9037
    else
9038
      { runInOp(this.cm, function () { return input.cm.curOp.selectionChanged = true; }); }
9039
 
9040
    function poll() {
9041
      if (input.cm.state.focused) {
9042
        input.pollSelection();
9043
        input.polling.set(input.cm.options.pollInterval, poll);
9044
      }
9045
    }
9046
    this.polling.set(this.cm.options.pollInterval, poll);
9047
  };
9048
 
9049
  ContentEditableInput.prototype.selectionChanged = function () {
9050
    var sel = this.getSelection();
9051
    return sel.anchorNode != this.lastAnchorNode || sel.anchorOffset != this.lastAnchorOffset ||
9052
      sel.focusNode != this.lastFocusNode || sel.focusOffset != this.lastFocusOffset
9053
  };
9054
 
9055
  ContentEditableInput.prototype.pollSelection = function () {
9056
    if (this.readDOMTimeout != null || this.gracePeriod || !this.selectionChanged()) { return }
9057
    var sel = this.getSelection(), cm = this.cm;
9058
    // On Android Chrome (version 56, at least), backspacing into an
9059
    // uneditable block element will put the cursor in that element,
9060
    // and then, because it's not editable, hide the virtual keyboard.
9061
    // Because Android doesn't allow us to actually detect backspace
9062
    // presses in a sane way, this code checks for when that happens
9063
    // and simulates a backspace press in this case.
9064
    if (android && chrome && this.cm.display.gutterSpecs.length && isInGutter(sel.anchorNode)) {
9065
      this.cm.triggerOnKeyDown({type: "keydown", keyCode: 8, preventDefault: Math.abs});
9066
      this.blur();
9067
      this.focus();
9068
      return
9069
    }
9070
    if (this.composing) { return }
9071
    this.rememberSelection();
9072
    var anchor = domToPos(cm, sel.anchorNode, sel.anchorOffset);
9073
    var head = domToPos(cm, sel.focusNode, sel.focusOffset);
9074
    if (anchor && head) { runInOp(cm, function () {
9075
      setSelection(cm.doc, simpleSelection(anchor, head), sel_dontScroll);
9076
      if (anchor.bad || head.bad) { cm.curOp.selectionChanged = true; }
9077
    }); }
9078
  };
9079
 
9080
  ContentEditableInput.prototype.pollContent = function () {
9081
    if (this.readDOMTimeout != null) {
9082
      clearTimeout(this.readDOMTimeout);
9083
      this.readDOMTimeout = null;
9084
    }
9085
 
9086
    var cm = this.cm, display = cm.display, sel = cm.doc.sel.primary();
9087
    var from = sel.from(), to = sel.to();
9088
    if (from.ch == 0 && from.line > cm.firstLine())
9089
      { from = Pos(from.line - 1, getLine(cm.doc, from.line - 1).length); }
9090
    if (to.ch == getLine(cm.doc, to.line).text.length && to.line < cm.lastLine())
9091
      { to = Pos(to.line + 1, 0); }
9092
    if (from.line < display.viewFrom || to.line > display.viewTo - 1) { return false }
9093
 
9094
    var fromIndex, fromLine, fromNode;
9095
    if (from.line == display.viewFrom || (fromIndex = findViewIndex(cm, from.line)) == 0) {
9096
      fromLine = lineNo(display.view[0].line);
9097
      fromNode = display.view[0].node;
9098
    } else {
9099
      fromLine = lineNo(display.view[fromIndex].line);
9100
      fromNode = display.view[fromIndex - 1].node.nextSibling;
9101
    }
9102
    var toIndex = findViewIndex(cm, to.line);
9103
    var toLine, toNode;
9104
    if (toIndex == display.view.length - 1) {
9105
      toLine = display.viewTo - 1;
9106
      toNode = display.lineDiv.lastChild;
9107
    } else {
9108
      toLine = lineNo(display.view[toIndex + 1].line) - 1;
9109
      toNode = display.view[toIndex + 1].node.previousSibling;
9110
    }
9111
 
9112
    if (!fromNode) { return false }
9113
    var newText = cm.doc.splitLines(domTextBetween(cm, fromNode, toNode, fromLine, toLine));
9114
    var oldText = getBetween(cm.doc, Pos(fromLine, 0), Pos(toLine, getLine(cm.doc, toLine).text.length));
9115
    while (newText.length > 1 && oldText.length > 1) {
9116
      if (lst(newText) == lst(oldText)) { newText.pop(); oldText.pop(); toLine--; }
9117
      else if (newText[0] == oldText[0]) { newText.shift(); oldText.shift(); fromLine++; }
9118
      else { break }
9119
    }
9120
 
9121
    var cutFront = 0, cutEnd = 0;
9122
    var newTop = newText[0], oldTop = oldText[0], maxCutFront = Math.min(newTop.length, oldTop.length);
9123
    while (cutFront < maxCutFront && newTop.charCodeAt(cutFront) == oldTop.charCodeAt(cutFront))
9124
      { ++cutFront; }
9125
    var newBot = lst(newText), oldBot = lst(oldText);
9126
    var maxCutEnd = Math.min(newBot.length - (newText.length == 1 ? cutFront : 0),
9127
                             oldBot.length - (oldText.length == 1 ? cutFront : 0));
9128
    while (cutEnd < maxCutEnd &&
9129
           newBot.charCodeAt(newBot.length - cutEnd - 1) == oldBot.charCodeAt(oldBot.length - cutEnd - 1))
9130
      { ++cutEnd; }
9131
    // Try to move start of change to start of selection if ambiguous
9132
    if (newText.length == 1 && oldText.length == 1 && fromLine == from.line) {
9133
      while (cutFront && cutFront > from.ch &&
9134
             newBot.charCodeAt(newBot.length - cutEnd - 1) == oldBot.charCodeAt(oldBot.length - cutEnd - 1)) {
9135
        cutFront--;
9136
        cutEnd++;
9137
      }
9138
    }
9139
 
9140
    newText[newText.length - 1] = newBot.slice(0, newBot.length - cutEnd).replace(/^\u200b+/, "");
9141
    newText[0] = newText[0].slice(cutFront).replace(/\u200b+$/, "");
9142
 
9143
    var chFrom = Pos(fromLine, cutFront);
9144
    var chTo = Pos(toLine, oldText.length ? lst(oldText).length - cutEnd : 0);
9145
    if (newText.length > 1 || newText[0] || cmp(chFrom, chTo)) {
9146
      replaceRange(cm.doc, newText, chFrom, chTo, "+input");
9147
      return true
9148
    }
9149
  };
9150
 
9151
  ContentEditableInput.prototype.ensurePolled = function () {
9152
    this.forceCompositionEnd();
9153
  };
9154
  ContentEditableInput.prototype.reset = function () {
9155
    this.forceCompositionEnd();
9156
  };
9157
  ContentEditableInput.prototype.forceCompositionEnd = function () {
9158
    if (!this.composing) { return }
9159
    clearTimeout(this.readDOMTimeout);
9160
    this.composing = null;
9161
    this.updateFromDOM();
9162
    this.div.blur();
9163
    this.div.focus();
9164
  };
9165
  ContentEditableInput.prototype.readFromDOMSoon = function () {
9166
      var this$1 = this;
9167
 
9168
    if (this.readDOMTimeout != null) { return }
9169
    this.readDOMTimeout = setTimeout(function () {
9170
      this$1.readDOMTimeout = null;
9171
      if (this$1.composing) {
9172
        if (this$1.composing.done) { this$1.composing = null; }
9173
        else { return }
9174
      }
9175
      this$1.updateFromDOM();
9176
    }, 80);
9177
  };
9178
 
9179
  ContentEditableInput.prototype.updateFromDOM = function () {
9180
      var this$1 = this;
9181
 
9182
    if (this.cm.isReadOnly() || !this.pollContent())
9183
      { runInOp(this.cm, function () { return regChange(this$1.cm); }); }
9184
  };
9185
 
9186
  ContentEditableInput.prototype.setUneditable = function (node) {
9187
    node.contentEditable = "false";
9188
  };
9189
 
9190
  ContentEditableInput.prototype.onKeyPress = function (e) {
9191
    if (e.charCode == 0 || this.composing) { return }
9192
    e.preventDefault();
9193
    if (!this.cm.isReadOnly())
9194
      { operation(this.cm, applyTextInput)(this.cm, String.fromCharCode(e.charCode == null ? e.keyCode : e.charCode), 0); }
9195
  };
9196
 
9197
  ContentEditableInput.prototype.readOnlyChanged = function (val) {
9198
    this.div.contentEditable = String(val != "nocursor");
9199
  };
9200
 
9201
  ContentEditableInput.prototype.onContextMenu = function () {};
9202
  ContentEditableInput.prototype.resetPosition = function () {};
9203
 
9204
  ContentEditableInput.prototype.needsContentAttribute = true;
9205
 
9206
  function posToDOM(cm, pos) {
9207
    var view = findViewForLine(cm, pos.line);
9208
    if (!view || view.hidden) { return null }
9209
    var line = getLine(cm.doc, pos.line);
9210
    var info = mapFromLineView(view, line, pos.line);
9211
 
9212
    var order = getOrder(line, cm.doc.direction), side = "left";
9213
    if (order) {
9214
      var partPos = getBidiPartAt(order, pos.ch);
9215
      side = partPos % 2 ? "right" : "left";
9216
    }
9217
    var result = nodeAndOffsetInLineMap(info.map, pos.ch, side);
9218
    result.offset = result.collapse == "right" ? result.end : result.start;
9219
    return result
9220
  }
9221
 
9222
  function isInGutter(node) {
9223
    for (var scan = node; scan; scan = scan.parentNode)
9224
      { if (/CodeMirror-gutter-wrapper/.test(scan.className)) { return true } }
9225
    return false
9226
  }
9227
 
9228
  function badPos(pos, bad) { if (bad) { pos.bad = true; } return pos }
9229
 
9230
  function domTextBetween(cm, from, to, fromLine, toLine) {
9231
    var text = "", closing = false, lineSep = cm.doc.lineSeparator(), extraLinebreak = false;
9232
    function recognizeMarker(id) { return function (marker) { return marker.id == id; } }
9233
    function close() {
9234
      if (closing) {
9235
        text += lineSep;
9236
        if (extraLinebreak) { text += lineSep; }
9237
        closing = extraLinebreak = false;
9238
      }
9239
    }
9240
    function addText(str) {
9241
      if (str) {
9242
        close();
9243
        text += str;
9244
      }
9245
    }
9246
    function walk(node) {
9247
      if (node.nodeType == 1) {
9248
        var cmText = node.getAttribute("cm-text");
9249
        if (cmText) {
9250
          addText(cmText);
9251
          return
9252
        }
15152 obado 9253
        var markerID = node.getAttribute("cm-marker"), range;
14283 obado 9254
        if (markerID) {
9255
          var found = cm.findMarks(Pos(fromLine, 0), Pos(toLine + 1, 0), recognizeMarker(+markerID));
15152 obado 9256
          if (found.length && (range = found[0].find(0)))
9257
            { addText(getBetween(cm.doc, range.from, range.to).join(lineSep)); }
14283 obado 9258
          return
9259
        }
9260
        if (node.getAttribute("contenteditable") == "false") { return }
9261
        var isBlock = /^(pre|div|p|li|table|br)$/i.test(node.nodeName);
9262
        if (!/^br$/i.test(node.nodeName) && node.textContent.length == 0) { return }
9263
 
9264
        if (isBlock) { close(); }
9265
        for (var i = 0; i < node.childNodes.length; i++)
9266
          { walk(node.childNodes[i]); }
9267
 
9268
        if (/^(pre|p)$/i.test(node.nodeName)) { extraLinebreak = true; }
9269
        if (isBlock) { closing = true; }
9270
      } else if (node.nodeType == 3) {
9271
        addText(node.nodeValue.replace(/\u200b/g, "").replace(/\u00a0/g, " "));
9272
      }
9273
    }
9274
    for (;;) {
9275
      walk(from);
9276
      if (from == to) { break }
9277
      from = from.nextSibling;
9278
      extraLinebreak = false;
9279
    }
9280
    return text
9281
  }
9282
 
9283
  function domToPos(cm, node, offset) {
9284
    var lineNode;
9285
    if (node == cm.display.lineDiv) {
9286
      lineNode = cm.display.lineDiv.childNodes[offset];
9287
      if (!lineNode) { return badPos(cm.clipPos(Pos(cm.display.viewTo - 1)), true) }
9288
      node = null; offset = 0;
9289
    } else {
9290
      for (lineNode = node;; lineNode = lineNode.parentNode) {
9291
        if (!lineNode || lineNode == cm.display.lineDiv) { return null }
9292
        if (lineNode.parentNode && lineNode.parentNode == cm.display.lineDiv) { break }
9293
      }
9294
    }
9295
    for (var i = 0; i < cm.display.view.length; i++) {
9296
      var lineView = cm.display.view[i];
9297
      if (lineView.node == lineNode)
9298
        { return locateNodeInLineView(lineView, node, offset) }
9299
    }
9300
  }
9301
 
9302
  function locateNodeInLineView(lineView, node, offset) {
9303
    var wrapper = lineView.text.firstChild, bad = false;
9304
    if (!node || !contains(wrapper, node)) { return badPos(Pos(lineNo(lineView.line), 0), true) }
9305
    if (node == wrapper) {
9306
      bad = true;
9307
      node = wrapper.childNodes[offset];
9308
      offset = 0;
9309
      if (!node) {
9310
        var line = lineView.rest ? lst(lineView.rest) : lineView.line;
9311
        return badPos(Pos(lineNo(line), line.text.length), bad)
9312
      }
9313
    }
9314
 
9315
    var textNode = node.nodeType == 3 ? node : null, topNode = node;
9316
    if (!textNode && node.childNodes.length == 1 && node.firstChild.nodeType == 3) {
9317
      textNode = node.firstChild;
9318
      if (offset) { offset = textNode.nodeValue.length; }
9319
    }
9320
    while (topNode.parentNode != wrapper) { topNode = topNode.parentNode; }
9321
    var measure = lineView.measure, maps = measure.maps;
9322
 
9323
    function find(textNode, topNode, offset) {
9324
      for (var i = -1; i < (maps ? maps.length : 0); i++) {
15152 obado 9325
        var map = i < 0 ? measure.map : maps[i];
9326
        for (var j = 0; j < map.length; j += 3) {
9327
          var curNode = map[j + 2];
14283 obado 9328
          if (curNode == textNode || curNode == topNode) {
9329
            var line = lineNo(i < 0 ? lineView.line : lineView.rest[i]);
15152 obado 9330
            var ch = map[j] + offset;
9331
            if (offset < 0 || curNode != textNode) { ch = map[j + (offset ? 1 : 0)]; }
14283 obado 9332
            return Pos(line, ch)
9333
          }
9334
        }
9335
      }
9336
    }
9337
    var found = find(textNode, topNode, offset);
9338
    if (found) { return badPos(found, bad) }
9339
 
9340
    // FIXME this is all really shaky. might handle the few cases it needs to handle, but likely to cause problems
9341
    for (var after = topNode.nextSibling, dist = textNode ? textNode.nodeValue.length - offset : 0; after; after = after.nextSibling) {
9342
      found = find(after, after.firstChild, 0);
9343
      if (found)
9344
        { return badPos(Pos(found.line, found.ch - dist), bad) }
9345
      else
9346
        { dist += after.textContent.length; }
9347
    }
9348
    for (var before = topNode.previousSibling, dist$1 = offset; before; before = before.previousSibling) {
9349
      found = find(before, before.firstChild, -1);
9350
      if (found)
9351
        { return badPos(Pos(found.line, found.ch + dist$1), bad) }
9352
      else
9353
        { dist$1 += before.textContent.length; }
9354
    }
9355
  }
9356
 
9357
  // TEXTAREA INPUT STYLE
9358
 
9359
  var TextareaInput = function(cm) {
9360
    this.cm = cm;
9361
    // See input.poll and input.reset
9362
    this.prevInput = "";
9363
 
9364
    // Flag that indicates whether we expect input to appear real soon
9365
    // now (after some event like 'keypress' or 'input') and are
9366
    // polling intensively.
9367
    this.pollingFast = false;
9368
    // Self-resetting timeout for the poller
9369
    this.polling = new Delayed();
9370
    // Used to work around IE issue with selection being forgotten when focus moves away from textarea
9371
    this.hasSelection = false;
9372
    this.composing = null;
17702 obado 9373
    this.resetting = false;
14283 obado 9374
  };
9375
 
9376
  TextareaInput.prototype.init = function (display) {
9377
      var this$1 = this;
9378
 
9379
    var input = this, cm = this.cm;
9380
    this.createField(display);
9381
    var te = this.textarea;
9382
 
9383
    display.wrapper.insertBefore(this.wrapper, display.wrapper.firstChild);
9384
 
9385
    // Needed to hide big blue blinking cursor on Mobile Safari (doesn't seem to work in iOS 8 anymore)
9386
    if (ios) { te.style.width = "0px"; }
9387
 
9388
    on(te, "input", function () {
9389
      if (ie && ie_version >= 9 && this$1.hasSelection) { this$1.hasSelection = null; }
9390
      input.poll();
9391
    });
9392
 
9393
    on(te, "paste", function (e) {
9394
      if (signalDOMEvent(cm, e) || handlePaste(e, cm)) { return }
9395
 
9396
      cm.state.pasteIncoming = +new Date;
9397
      input.fastPoll();
9398
    });
9399
 
9400
    function prepareCopyCut(e) {
9401
      if (signalDOMEvent(cm, e)) { return }
9402
      if (cm.somethingSelected()) {
9403
        setLastCopied({lineWise: false, text: cm.getSelections()});
9404
      } else if (!cm.options.lineWiseCopyCut) {
9405
        return
9406
      } else {
9407
        var ranges = copyableRanges(cm);
9408
        setLastCopied({lineWise: true, text: ranges.text});
9409
        if (e.type == "cut") {
9410
          cm.setSelections(ranges.ranges, null, sel_dontScroll);
9411
        } else {
9412
          input.prevInput = "";
9413
          te.value = ranges.text.join("\n");
9414
          selectInput(te);
9415
        }
9416
      }
9417
      if (e.type == "cut") { cm.state.cutIncoming = +new Date; }
9418
    }
9419
    on(te, "cut", prepareCopyCut);
9420
    on(te, "copy", prepareCopyCut);
9421
 
9422
    on(display.scroller, "paste", function (e) {
9423
      if (eventInWidget(display, e) || signalDOMEvent(cm, e)) { return }
9424
      if (!te.dispatchEvent) {
9425
        cm.state.pasteIncoming = +new Date;
9426
        input.focus();
9427
        return
9428
      }
9429
 
9430
      // Pass the `paste` event to the textarea so it's handled by its event listener.
9431
      var event = new Event("paste");
9432
      event.clipboardData = e.clipboardData;
9433
      te.dispatchEvent(event);
9434
    });
9435
 
9436
    // Prevent normal selection in the editor (we handle our own)
9437
    on(display.lineSpace, "selectstart", function (e) {
9438
      if (!eventInWidget(display, e)) { e_preventDefault(e); }
9439
    });
9440
 
9441
    on(te, "compositionstart", function () {
9442
      var start = cm.getCursor("from");
9443
      if (input.composing) { input.composing.range.clear(); }
9444
      input.composing = {
9445
        start: start,
9446
        range: cm.markText(start, cm.getCursor("to"), {className: "CodeMirror-composing"})
9447
      };
9448
    });
9449
    on(te, "compositionend", function () {
9450
      if (input.composing) {
9451
        input.poll();
9452
        input.composing.range.clear();
9453
        input.composing = null;
9454
      }
9455
    });
9456
  };
9457
 
9458
  TextareaInput.prototype.createField = function (_display) {
9459
    // Wraps and hides input textarea
9460
    this.wrapper = hiddenTextarea();
9461
    // The semihidden textarea that is focused when the editor is
9462
    // focused, and receives input.
9463
    this.textarea = this.wrapper.firstChild;
17702 obado 9464
    var opts = this.cm.options;
9465
    disableBrowserMagic(this.textarea, opts.spellcheck, opts.autocorrect, opts.autocapitalize);
14283 obado 9466
  };
9467
 
15152 obado 9468
  TextareaInput.prototype.screenReaderLabelChanged = function (label) {
9469
    // Label for screenreaders, accessibility
9470
    if(label) {
9471
      this.textarea.setAttribute('aria-label', label);
9472
    } else {
9473
      this.textarea.removeAttribute('aria-label');
9474
    }
9475
  };
9476
 
14283 obado 9477
  TextareaInput.prototype.prepareSelection = function () {
9478
    // Redraw the selection and/or cursor
9479
    var cm = this.cm, display = cm.display, doc = cm.doc;
9480
    var result = prepareSelection(cm);
9481
 
9482
    // Move the hidden textarea near the cursor to prevent scrolling artifacts
9483
    if (cm.options.moveInputWithCursor) {
9484
      var headPos = cursorCoords(cm, doc.sel.primary().head, "div");
9485
      var wrapOff = display.wrapper.getBoundingClientRect(), lineOff = display.lineDiv.getBoundingClientRect();
9486
      result.teTop = Math.max(0, Math.min(display.wrapper.clientHeight - 10,
9487
                                          headPos.top + lineOff.top - wrapOff.top));
9488
      result.teLeft = Math.max(0, Math.min(display.wrapper.clientWidth - 10,
9489
                                           headPos.left + lineOff.left - wrapOff.left));
9490
    }
9491
 
9492
    return result
9493
  };
9494
 
9495
  TextareaInput.prototype.showSelection = function (drawn) {
9496
    var cm = this.cm, display = cm.display;
9497
    removeChildrenAndAdd(display.cursorDiv, drawn.cursors);
9498
    removeChildrenAndAdd(display.selectionDiv, drawn.selection);
9499
    if (drawn.teTop != null) {
9500
      this.wrapper.style.top = drawn.teTop + "px";
9501
      this.wrapper.style.left = drawn.teLeft + "px";
9502
    }
9503
  };
9504
 
9505
  // Reset the input to correspond to the selection (or to be empty,
9506
  // when not typing and nothing is selected)
9507
  TextareaInput.prototype.reset = function (typing) {
17702 obado 9508
    if (this.contextMenuPending || this.composing && typing) { return }
14283 obado 9509
    var cm = this.cm;
17702 obado 9510
    this.resetting = true;
14283 obado 9511
    if (cm.somethingSelected()) {
9512
      this.prevInput = "";
9513
      var content = cm.getSelection();
9514
      this.textarea.value = content;
9515
      if (cm.state.focused) { selectInput(this.textarea); }
9516
      if (ie && ie_version >= 9) { this.hasSelection = content; }
9517
    } else if (!typing) {
9518
      this.prevInput = this.textarea.value = "";
9519
      if (ie && ie_version >= 9) { this.hasSelection = null; }
9520
    }
17702 obado 9521
    this.resetting = false;
14283 obado 9522
  };
9523
 
9524
  TextareaInput.prototype.getField = function () { return this.textarea };
9525
 
9526
  TextareaInput.prototype.supportsTouch = function () { return false };
9527
 
9528
  TextareaInput.prototype.focus = function () {
17702 obado 9529
    if (this.cm.options.readOnly != "nocursor" && (!mobile || activeElt(this.textarea.ownerDocument) != this.textarea)) {
14283 obado 9530
      try { this.textarea.focus(); }
9531
      catch (e) {} // IE8 will throw if the textarea is display: none or not in DOM
9532
    }
9533
  };
9534
 
9535
  TextareaInput.prototype.blur = function () { this.textarea.blur(); };
9536
 
9537
  TextareaInput.prototype.resetPosition = function () {
9538
    this.wrapper.style.top = this.wrapper.style.left = 0;
9539
  };
9540
 
9541
  TextareaInput.prototype.receivedFocus = function () { this.slowPoll(); };
9542
 
9543
  // Poll for input changes, using the normal rate of polling. This
9544
  // runs as long as the editor is focused.
9545
  TextareaInput.prototype.slowPoll = function () {
9546
      var this$1 = this;
9547
 
9548
    if (this.pollingFast) { return }
9549
    this.polling.set(this.cm.options.pollInterval, function () {
9550
      this$1.poll();
9551
      if (this$1.cm.state.focused) { this$1.slowPoll(); }
9552
    });
9553
  };
9554
 
9555
  // When an event has just come in that is likely to add or change
9556
  // something in the input textarea, we poll faster, to ensure that
9557
  // the change appears on the screen quickly.
9558
  TextareaInput.prototype.fastPoll = function () {
9559
    var missed = false, input = this;
9560
    input.pollingFast = true;
9561
    function p() {
9562
      var changed = input.poll();
9563
      if (!changed && !missed) {missed = true; input.polling.set(60, p);}
9564
      else {input.pollingFast = false; input.slowPoll();}
9565
    }
9566
    input.polling.set(20, p);
9567
  };
9568
 
9569
  // Read input from the textarea, and update the document to match.
9570
  // When something is selected, it is present in the textarea, and
9571
  // selected (unless it is huge, in which case a placeholder is
9572
  // used). When nothing is selected, the cursor sits after previously
9573
  // seen text (can be empty), which is stored in prevInput (we must
9574
  // not reset the textarea when typing, because that breaks IME).
9575
  TextareaInput.prototype.poll = function () {
9576
      var this$1 = this;
9577
 
9578
    var cm = this.cm, input = this.textarea, prevInput = this.prevInput;
9579
    // Since this is called a *lot*, try to bail out as cheaply as
9580
    // possible when it is clear that nothing happened. hasSelection
9581
    // will be the case when there is a lot of text in the textarea,
9582
    // in which case reading its value would be expensive.
17702 obado 9583
    if (this.contextMenuPending || this.resetting || !cm.state.focused ||
14283 obado 9584
        (hasSelection(input) && !prevInput && !this.composing) ||
9585
        cm.isReadOnly() || cm.options.disableInput || cm.state.keySeq)
9586
      { return false }
9587
 
9588
    var text = input.value;
9589
    // If nothing changed, bail.
9590
    if (text == prevInput && !cm.somethingSelected()) { return false }
9591
    // Work around nonsensical selection resetting in IE9/10, and
9592
    // inexplicable appearance of private area unicode characters on
9593
    // some key combos in Mac (#2689).
9594
    if (ie && ie_version >= 9 && this.hasSelection === text ||
9595
        mac && /[\uf700-\uf7ff]/.test(text)) {
9596
      cm.display.input.reset();
9597
      return false
9598
    }
9599
 
9600
    if (cm.doc.sel == cm.display.selForContextMenu) {
9601
      var first = text.charCodeAt(0);
9602
      if (first == 0x200b && !prevInput) { prevInput = "\u200b"; }
9603
      if (first == 0x21da) { this.reset(); return this.cm.execCommand("undo") }
9604
    }
9605
    // Find the part of the input that is actually new
9606
    var same = 0, l = Math.min(prevInput.length, text.length);
9607
    while (same < l && prevInput.charCodeAt(same) == text.charCodeAt(same)) { ++same; }
9608
 
9609
    runInOp(cm, function () {
9610
      applyTextInput(cm, text.slice(same), prevInput.length - same,
9611
                     null, this$1.composing ? "*compose" : null);
9612
 
9613
      // Don't leave long text in the textarea, since it makes further polling slow
9614
      if (text.length > 1000 || text.indexOf("\n") > -1) { input.value = this$1.prevInput = ""; }
9615
      else { this$1.prevInput = text; }
9616
 
9617
      if (this$1.composing) {
9618
        this$1.composing.range.clear();
9619
        this$1.composing.range = cm.markText(this$1.composing.start, cm.getCursor("to"),
9620
                                           {className: "CodeMirror-composing"});
9621
      }
9622
    });
9623
    return true
9624
  };
9625
 
9626
  TextareaInput.prototype.ensurePolled = function () {
9627
    if (this.pollingFast && this.poll()) { this.pollingFast = false; }
9628
  };
9629
 
9630
  TextareaInput.prototype.onKeyPress = function () {
9631
    if (ie && ie_version >= 9) { this.hasSelection = null; }
9632
    this.fastPoll();
9633
  };
9634
 
9635
  TextareaInput.prototype.onContextMenu = function (e) {
9636
    var input = this, cm = input.cm, display = cm.display, te = input.textarea;
9637
    if (input.contextMenuPending) { input.contextMenuPending(); }
9638
    var pos = posFromMouse(cm, e), scrollPos = display.scroller.scrollTop;
9639
    if (!pos || presto) { return } // Opera is difficult.
9640
 
9641
    // Reset the current text selection only if the click is done outside of the selection
9642
    // and 'resetSelectionOnContextMenu' option is true.
9643
    var reset = cm.options.resetSelectionOnContextMenu;
9644
    if (reset && cm.doc.sel.contains(pos) == -1)
9645
      { operation(cm, setSelection)(cm.doc, simpleSelection(pos), sel_dontScroll); }
9646
 
9647
    var oldCSS = te.style.cssText, oldWrapperCSS = input.wrapper.style.cssText;
9648
    var wrapperBox = input.wrapper.offsetParent.getBoundingClientRect();
9649
    input.wrapper.style.cssText = "position: static";
9650
    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);";
9651
    var oldScrollY;
17702 obado 9652
    if (webkit) { oldScrollY = te.ownerDocument.defaultView.scrollY; } // Work around Chrome issue (#2712)
14283 obado 9653
    display.input.focus();
17702 obado 9654
    if (webkit) { te.ownerDocument.defaultView.scrollTo(null, oldScrollY); }
14283 obado 9655
    display.input.reset();
9656
    // Adds "Select all" to context menu in FF
9657
    if (!cm.somethingSelected()) { te.value = input.prevInput = " "; }
9658
    input.contextMenuPending = rehide;
9659
    display.selForContextMenu = cm.doc.sel;
9660
    clearTimeout(display.detectingSelectAll);
9661
 
9662
    // Select-all will be greyed out if there's nothing to select, so
9663
    // this adds a zero-width space so that we can later check whether
9664
    // it got selected.
9665
    function prepareSelectAllHack() {
9666
      if (te.selectionStart != null) {
9667
        var selected = cm.somethingSelected();
9668
        var extval = "\u200b" + (selected ? te.value : "");
9669
        te.value = "\u21da"; // Used to catch context-menu undo
9670
        te.value = extval;
9671
        input.prevInput = selected ? "" : "\u200b";
9672
        te.selectionStart = 1; te.selectionEnd = extval.length;
9673
        // Re-set this, in case some other handler touched the
9674
        // selection in the meantime.
9675
        display.selForContextMenu = cm.doc.sel;
9676
      }
9677
    }
9678
    function rehide() {
9679
      if (input.contextMenuPending != rehide) { return }
9680
      input.contextMenuPending = false;
9681
      input.wrapper.style.cssText = oldWrapperCSS;
9682
      te.style.cssText = oldCSS;
9683
      if (ie && ie_version < 9) { display.scrollbars.setScrollTop(display.scroller.scrollTop = scrollPos); }
9684
 
9685
      // Try to detect the user choosing select-all
9686
      if (te.selectionStart != null) {
9687
        if (!ie || (ie && ie_version < 9)) { prepareSelectAllHack(); }
9688
        var i = 0, poll = function () {
9689
          if (display.selForContextMenu == cm.doc.sel && te.selectionStart == 0 &&
9690
              te.selectionEnd > 0 && input.prevInput == "\u200b") {
9691
            operation(cm, selectAll)(cm);
9692
          } else if (i++ < 10) {
9693
            display.detectingSelectAll = setTimeout(poll, 500);
9694
          } else {
9695
            display.selForContextMenu = null;
9696
            display.input.reset();
9697
          }
9698
        };
9699
        display.detectingSelectAll = setTimeout(poll, 200);
9700
      }
9701
    }
9702
 
9703
    if (ie && ie_version >= 9) { prepareSelectAllHack(); }
9704
    if (captureRightClick) {
9705
      e_stop(e);
9706
      var mouseup = function () {
9707
        off(window, "mouseup", mouseup);
9708
        setTimeout(rehide, 20);
9709
      };
9710
      on(window, "mouseup", mouseup);
9711
    } else {
9712
      setTimeout(rehide, 50);
9713
    }
9714
  };
9715
 
9716
  TextareaInput.prototype.readOnlyChanged = function (val) {
9717
    if (!val) { this.reset(); }
9718
    this.textarea.disabled = val == "nocursor";
16493 obado 9719
    this.textarea.readOnly = !!val;
14283 obado 9720
  };
9721
 
9722
  TextareaInput.prototype.setUneditable = function () {};
9723
 
9724
  TextareaInput.prototype.needsContentAttribute = false;
9725
 
9726
  function fromTextArea(textarea, options) {
9727
    options = options ? copyObj(options) : {};
9728
    options.value = textarea.value;
9729
    if (!options.tabindex && textarea.tabIndex)
9730
      { options.tabindex = textarea.tabIndex; }
9731
    if (!options.placeholder && textarea.placeholder)
9732
      { options.placeholder = textarea.placeholder; }
9733
    // Set autofocus to true if this textarea is focused, or if it has
9734
    // autofocus and no other element is focused.
9735
    if (options.autofocus == null) {
17702 obado 9736
      var hasFocus = activeElt(textarea.ownerDocument);
14283 obado 9737
      options.autofocus = hasFocus == textarea ||
9738
        textarea.getAttribute("autofocus") != null && hasFocus == document.body;
9739
    }
9740
 
9741
    function save() {textarea.value = cm.getValue();}
9742
 
9743
    var realSubmit;
9744
    if (textarea.form) {
9745
      on(textarea.form, "submit", save);
9746
      // Deplorable hack to make the submit method do the right thing.
9747
      if (!options.leaveSubmitMethodAlone) {
9748
        var form = textarea.form;
9749
        realSubmit = form.submit;
9750
        try {
9751
          var wrappedSubmit = form.submit = function () {
9752
            save();
9753
            form.submit = realSubmit;
9754
            form.submit();
9755
            form.submit = wrappedSubmit;
9756
          };
9757
        } catch(e) {}
9758
      }
9759
    }
9760
 
9761
    options.finishInit = function (cm) {
9762
      cm.save = save;
9763
      cm.getTextArea = function () { return textarea; };
9764
      cm.toTextArea = function () {
9765
        cm.toTextArea = isNaN; // Prevent this from being ran twice
9766
        save();
9767
        textarea.parentNode.removeChild(cm.getWrapperElement());
9768
        textarea.style.display = "";
9769
        if (textarea.form) {
9770
          off(textarea.form, "submit", save);
15152 obado 9771
          if (!options.leaveSubmitMethodAlone && typeof textarea.form.submit == "function")
14283 obado 9772
            { textarea.form.submit = realSubmit; }
9773
        }
9774
      };
9775
    };
9776
 
9777
    textarea.style.display = "none";
9778
    var cm = CodeMirror(function (node) { return textarea.parentNode.insertBefore(node, textarea.nextSibling); },
9779
      options);
9780
    return cm
9781
  }
9782
 
9783
  function addLegacyProps(CodeMirror) {
9784
    CodeMirror.off = off;
9785
    CodeMirror.on = on;
9786
    CodeMirror.wheelEventPixels = wheelEventPixels;
9787
    CodeMirror.Doc = Doc;
9788
    CodeMirror.splitLines = splitLinesAuto;
9789
    CodeMirror.countColumn = countColumn;
9790
    CodeMirror.findColumn = findColumn;
9791
    CodeMirror.isWordChar = isWordCharBasic;
9792
    CodeMirror.Pass = Pass;
9793
    CodeMirror.signal = signal;
9794
    CodeMirror.Line = Line;
9795
    CodeMirror.changeEnd = changeEnd;
9796
    CodeMirror.scrollbarModel = scrollbarModel;
9797
    CodeMirror.Pos = Pos;
9798
    CodeMirror.cmpPos = cmp;
9799
    CodeMirror.modes = modes;
9800
    CodeMirror.mimeModes = mimeModes;
9801
    CodeMirror.resolveMode = resolveMode;
9802
    CodeMirror.getMode = getMode;
9803
    CodeMirror.modeExtensions = modeExtensions;
9804
    CodeMirror.extendMode = extendMode;
9805
    CodeMirror.copyState = copyState;
9806
    CodeMirror.startState = startState;
9807
    CodeMirror.innerMode = innerMode;
9808
    CodeMirror.commands = commands;
9809
    CodeMirror.keyMap = keyMap;
9810
    CodeMirror.keyName = keyName;
9811
    CodeMirror.isModifierKey = isModifierKey;
9812
    CodeMirror.lookupKey = lookupKey;
9813
    CodeMirror.normalizeKeyMap = normalizeKeyMap;
9814
    CodeMirror.StringStream = StringStream;
9815
    CodeMirror.SharedTextMarker = SharedTextMarker;
9816
    CodeMirror.TextMarker = TextMarker;
9817
    CodeMirror.LineWidget = LineWidget;
9818
    CodeMirror.e_preventDefault = e_preventDefault;
9819
    CodeMirror.e_stopPropagation = e_stopPropagation;
9820
    CodeMirror.e_stop = e_stop;
9821
    CodeMirror.addClass = addClass;
9822
    CodeMirror.contains = contains;
9823
    CodeMirror.rmClass = rmClass;
9824
    CodeMirror.keyNames = keyNames;
9825
  }
9826
 
9827
  // EDITOR CONSTRUCTOR
9828
 
9829
  defineOptions(CodeMirror);
9830
 
9831
  addEditorMethods(CodeMirror);
9832
 
9833
  // Set up methods on CodeMirror's prototype to redirect to the editor's document.
9834
  var dontDelegate = "iter insert remove copy getEditor constructor".split(" ");
9835
  for (var prop in Doc.prototype) { if (Doc.prototype.hasOwnProperty(prop) && indexOf(dontDelegate, prop) < 0)
9836
    { CodeMirror.prototype[prop] = (function(method) {
9837
      return function() {return method.apply(this.doc, arguments)}
9838
    })(Doc.prototype[prop]); } }
9839
 
9840
  eventMixin(Doc);
9841
  CodeMirror.inputStyles = {"textarea": TextareaInput, "contenteditable": ContentEditableInput};
9842
 
9843
  // Extra arguments are stored as the mode's dependencies, which is
9844
  // used by (legacy) mechanisms like loadmode.js to automatically
9845
  // load a mode. (Preferred mechanism is the require/define calls.)
9846
  CodeMirror.defineMode = function(name/*, mode, …*/) {
9847
    if (!CodeMirror.defaults.mode && name != "null") { CodeMirror.defaults.mode = name; }
9848
    defineMode.apply(this, arguments);
9849
  };
9850
 
9851
  CodeMirror.defineMIME = defineMIME;
9852
 
9853
  // Minimal default mode.
9854
  CodeMirror.defineMode("null", function () { return ({token: function (stream) { return stream.skipToEnd(); }}); });
9855
  CodeMirror.defineMIME("text/plain", "null");
9856
 
9857
  // EXTENSIONS
9858
 
9859
  CodeMirror.defineExtension = function (name, func) {
9860
    CodeMirror.prototype[name] = func;
9861
  };
9862
  CodeMirror.defineDocExtension = function (name, func) {
9863
    Doc.prototype[name] = func;
9864
  };
9865
 
9866
  CodeMirror.fromTextArea = fromTextArea;
9867
 
9868
  addLegacyProps(CodeMirror);
9869
 
17702 obado 9870
  CodeMirror.version = "5.65.13";
14283 obado 9871
 
9872
  return CodeMirror;
9873
 
9874
})));