Path: blob/master/web-gui/buildyourownbotnet/assets/js/codemirror/lib/codemirror.js
1293 views
// CodeMirror version 3.211//2// CodeMirror is the only global var we claim3window.CodeMirror = (function() {4"use strict";56// BROWSER SNIFFING78// Crude, but necessary to handle a number of hard-to-feature-detect9// bugs and behavior differences.10var gecko = /gecko\/\d/i.test(navigator.userAgent);11// IE11 currently doesn't count as 'ie', since it has almost none of12// the same bugs as earlier versions. Use ie_gt10 to handle13// incompatibilities in that version.14var old_ie = /MSIE \d/.test(navigator.userAgent);15var ie_lt8 = old_ie && (document.documentMode == null || document.documentMode < 8);16var ie_lt9 = old_ie && (document.documentMode == null || document.documentMode < 9);17var ie_gt10 = /Trident\/([7-9]|\d{2,})\./.test(navigator.userAgent);18var ie = old_ie || ie_gt10;19var webkit = /WebKit\//.test(navigator.userAgent);20var qtwebkit = webkit && /Qt\/\d+\.\d+/.test(navigator.userAgent);21var chrome = /Chrome\//.test(navigator.userAgent);22var opera = /Opera\//.test(navigator.userAgent);23var safari = /Apple Computer/.test(navigator.vendor);24var khtml = /KHTML\//.test(navigator.userAgent);25var mac_geLion = /Mac OS X 1\d\D([7-9]|\d\d)\D/.test(navigator.userAgent);26var mac_geMountainLion = /Mac OS X 1\d\D([8-9]|\d\d)\D/.test(navigator.userAgent);27var phantom = /PhantomJS/.test(navigator.userAgent);2829var ios = /AppleWebKit/.test(navigator.userAgent) && /Mobile\/\w+/.test(navigator.userAgent);30// This is woefully incomplete. Suggestions for alternative methods welcome.31var mobile = ios || /Android|webOS|BlackBerry|Opera Mini|Opera Mobi|IEMobile/i.test(navigator.userAgent);32var mac = ios || /Mac/.test(navigator.platform);33var windows = /win/i.test(navigator.platform);3435var opera_version = opera && navigator.userAgent.match(/Version\/(\d*\.\d*)/);36if (opera_version) opera_version = Number(opera_version[1]);37if (opera_version && opera_version >= 15) { opera = false; webkit = true; }38// Some browsers use the wrong event properties to signal cmd/ctrl on OS X39var flipCtrlCmd = mac && (qtwebkit || opera && (opera_version == null || opera_version < 12.11));40var captureMiddleClick = gecko || (old_ie && !ie_lt9);4142// Optimize some code when these features are not used43var sawReadOnlySpans = false, sawCollapsedSpans = false;4445// CONSTRUCTOR4647function CodeMirror(place, options) {48if (!(this instanceof CodeMirror)) return new CodeMirror(place, options);4950this.options = options = options || {};51// Determine effective options based on given values and defaults.52for (var opt in defaults) if (!options.hasOwnProperty(opt) && defaults.hasOwnProperty(opt))53options[opt] = defaults[opt];54setGuttersForLineNumbers(options);5556var docStart = typeof options.value == "string" ? 0 : options.value.first;57var display = this.display = makeDisplay(place, docStart);58display.wrapper.CodeMirror = this;59updateGutters(this);60if (options.autofocus && !mobile) focusInput(this);6162this.state = {keyMaps: [],63overlays: [],64modeGen: 0,65overwrite: false, focused: false,66suppressEdits: false,67pasteIncoming: false, cutIncoming: false,68draggingText: false,69highlight: new Delayed()};7071themeChanged(this);72if (options.lineWrapping)73this.display.wrapper.className += " CodeMirror-wrap";7475var doc = options.value;76if (typeof doc == "string") doc = new Doc(options.value, options.mode);77operation(this, attachDoc)(this, doc);7879// Override magic textarea content restore that IE sometimes does80// on our hidden textarea on reload81if (old_ie) setTimeout(bind(resetInput, this, true), 20);8283registerEventHandlers(this);84// IE throws unspecified error in certain cases, when85// trying to access activeElement before onload86var hasFocus; try { hasFocus = (document.activeElement == display.input); } catch(e) { }87if (hasFocus || (options.autofocus && !mobile)) setTimeout(bind(onFocus, this), 20);88else onBlur(this);8990operation(this, function() {91for (var opt in optionHandlers)92if (optionHandlers.propertyIsEnumerable(opt))93optionHandlers[opt](this, options[opt], Init);94for (var i = 0; i < initHooks.length; ++i) initHooks[i](this);95})();96}9798// DISPLAY CONSTRUCTOR99100function makeDisplay(place, docStart) {101var d = {};102103var input = d.input = elt("textarea", null, null, "position: absolute; padding: 0; width: 1px; height: 1em; outline: none; font-size: 4px;");104if (webkit) input.style.width = "1000px";105else input.setAttribute("wrap", "off");106// if border: 0; -- iOS fails to open keyboard (issue #1287)107if (ios) input.style.border = "1px solid black";108input.setAttribute("autocorrect", "off"); input.setAttribute("autocapitalize", "off"); input.setAttribute("spellcheck", "false");109110// Wraps and hides input textarea111d.inputDiv = elt("div", [input], null, "overflow: hidden; position: relative; width: 3px; height: 0px;");112// The actual fake scrollbars.113d.scrollbarH = elt("div", [elt("div", null, null, "height: 1px")], "CodeMirror-hscrollbar");114d.scrollbarV = elt("div", [elt("div", null, null, "width: 1px")], "CodeMirror-vscrollbar");115d.scrollbarFiller = elt("div", null, "CodeMirror-scrollbar-filler");116d.gutterFiller = elt("div", null, "CodeMirror-gutter-filler");117// DIVs containing the selection and the actual code118d.lineDiv = elt("div", null, "CodeMirror-code");119d.selectionDiv = elt("div", null, null, "position: relative; z-index: 1");120// Blinky cursor, and element used to ensure cursor fits at the end of a line121d.cursor = elt("div", "\u00a0", "CodeMirror-cursor");122// Secondary cursor, shown when on a 'jump' in bi-directional text123d.otherCursor = elt("div", "\u00a0", "CodeMirror-cursor CodeMirror-secondarycursor");124// Used to measure text size125d.measure = elt("div", null, "CodeMirror-measure");126// Wraps everything that needs to exist inside the vertically-padded coordinate system127d.lineSpace = elt("div", [d.measure, d.selectionDiv, d.lineDiv, d.cursor, d.otherCursor],128null, "position: relative; outline: none");129// Moved around its parent to cover visible view130d.mover = elt("div", [elt("div", [d.lineSpace], "CodeMirror-lines")], null, "position: relative");131// Set to the height of the text, causes scrolling132d.sizer = elt("div", [d.mover], "CodeMirror-sizer");133// D is needed because behavior of elts with overflow: auto and padding is inconsistent across browsers134d.heightForcer = elt("div", null, null, "position: absolute; height: " + scrollerCutOff + "px; width: 1px;");135// Will contain the gutters, if any136d.gutters = elt("div", null, "CodeMirror-gutters");137d.lineGutter = null;138// Provides scrolling139d.scroller = elt("div", [d.sizer, d.heightForcer, d.gutters], "CodeMirror-scroll");140d.scroller.setAttribute("tabIndex", "-1");141// The element in which the editor lives.142d.wrapper = elt("div", [d.inputDiv, d.scrollbarH, d.scrollbarV,143d.scrollbarFiller, d.gutterFiller, d.scroller], "CodeMirror");144// Work around IE7 z-index bug145if (ie_lt8) { d.gutters.style.zIndex = -1; d.scroller.style.paddingRight = 0; }146if (place.appendChild) place.appendChild(d.wrapper); else place(d.wrapper);147148// Needed to hide big blue blinking cursor on Mobile Safari149if (ios) input.style.width = "0px";150if (!webkit) d.scroller.draggable = true;151// Needed to handle Tab key in KHTML152if (khtml) { d.inputDiv.style.height = "1px"; d.inputDiv.style.position = "absolute"; }153// Need to set a minimum width to see the scrollbar on IE7 (but must not set it on IE8).154else if (ie_lt8) d.scrollbarH.style.minWidth = d.scrollbarV.style.minWidth = "18px";155156// Current visible range (may be bigger than the view window).157d.viewOffset = d.lastSizeC = 0;158d.showingFrom = d.showingTo = docStart;159160// Used to only resize the line number gutter when necessary (when161// the amount of lines crosses a boundary that makes its width change)162d.lineNumWidth = d.lineNumInnerWidth = d.lineNumChars = null;163// See readInput and resetInput164d.prevInput = "";165// Set to true when a non-horizontal-scrolling widget is added. As166// an optimization, widget aligning is skipped when d is false.167d.alignWidgets = false;168// Flag that indicates whether we currently expect input to appear169// (after some event like 'keypress' or 'input') and are polling170// intensively.171d.pollingFast = false;172// Self-resetting timeout for the poller173d.poll = new Delayed();174175d.cachedCharWidth = d.cachedTextHeight = null;176d.measureLineCache = [];177d.measureLineCachePos = 0;178179// Tracks when resetInput has punted to just putting a short180// string instead of the (large) selection.181d.inaccurateSelection = false;182183// Tracks the maximum line length so that the horizontal scrollbar184// can be kept static when scrolling.185d.maxLine = null;186d.maxLineLength = 0;187d.maxLineChanged = false;188189// Used for measuring wheel scrolling granularity190d.wheelDX = d.wheelDY = d.wheelStartX = d.wheelStartY = null;191192return d;193}194195// STATE UPDATES196197// Used to get the editor into a consistent state again when options change.198199function loadMode(cm) {200cm.doc.mode = CodeMirror.getMode(cm.options, cm.doc.modeOption);201resetModeState(cm);202}203204function resetModeState(cm) {205cm.doc.iter(function(line) {206if (line.stateAfter) line.stateAfter = null;207if (line.styles) line.styles = null;208});209cm.doc.frontier = cm.doc.first;210startWorker(cm, 100);211cm.state.modeGen++;212if (cm.curOp) regChange(cm);213}214215function wrappingChanged(cm) {216if (cm.options.lineWrapping) {217cm.display.wrapper.className += " CodeMirror-wrap";218cm.display.sizer.style.minWidth = "";219} else {220cm.display.wrapper.className = cm.display.wrapper.className.replace(" CodeMirror-wrap", "");221computeMaxLength(cm);222}223estimateLineHeights(cm);224regChange(cm);225clearCaches(cm);226setTimeout(function(){updateScrollbars(cm);}, 100);227}228229function estimateHeight(cm) {230var th = textHeight(cm.display), wrapping = cm.options.lineWrapping;231var perLine = wrapping && Math.max(5, cm.display.scroller.clientWidth / charWidth(cm.display) - 3);232return function(line) {233if (lineIsHidden(cm.doc, line))234return 0;235else if (wrapping)236return (Math.ceil(line.text.length / perLine) || 1) * th;237else238return th;239};240}241242function estimateLineHeights(cm) {243var doc = cm.doc, est = estimateHeight(cm);244doc.iter(function(line) {245var estHeight = est(line);246if (estHeight != line.height) updateLineHeight(line, estHeight);247});248}249250function keyMapChanged(cm) {251var map = keyMap[cm.options.keyMap], style = map.style;252cm.display.wrapper.className = cm.display.wrapper.className.replace(/\s*cm-keymap-\S+/g, "") +253(style ? " cm-keymap-" + style : "");254}255256function themeChanged(cm) {257cm.display.wrapper.className = cm.display.wrapper.className.replace(/\s*cm-s-\S+/g, "") +258cm.options.theme.replace(/(^|\s)\s*/g, " cm-s-");259clearCaches(cm);260}261262function guttersChanged(cm) {263updateGutters(cm);264regChange(cm);265setTimeout(function(){alignHorizontally(cm);}, 20);266}267268function updateGutters(cm) {269var gutters = cm.display.gutters, specs = cm.options.gutters;270removeChildren(gutters);271for (var i = 0; i < specs.length; ++i) {272var gutterClass = specs[i];273var gElt = gutters.appendChild(elt("div", null, "CodeMirror-gutter " + gutterClass));274if (gutterClass == "CodeMirror-linenumbers") {275cm.display.lineGutter = gElt;276gElt.style.width = (cm.display.lineNumWidth || 1) + "px";277}278}279gutters.style.display = i ? "" : "none";280}281282function lineLength(doc, line) {283if (line.height == 0) return 0;284var len = line.text.length, merged, cur = line;285while (merged = collapsedSpanAtStart(cur)) {286var found = merged.find();287cur = getLine(doc, found.from.line);288len += found.from.ch - found.to.ch;289}290cur = line;291while (merged = collapsedSpanAtEnd(cur)) {292var found = merged.find();293len -= cur.text.length - found.from.ch;294cur = getLine(doc, found.to.line);295len += cur.text.length - found.to.ch;296}297return len;298}299300function computeMaxLength(cm) {301var d = cm.display, doc = cm.doc;302d.maxLine = getLine(doc, doc.first);303d.maxLineLength = lineLength(doc, d.maxLine);304d.maxLineChanged = true;305doc.iter(function(line) {306var len = lineLength(doc, line);307if (len > d.maxLineLength) {308d.maxLineLength = len;309d.maxLine = line;310}311});312}313314// Make sure the gutters options contains the element315// "CodeMirror-linenumbers" when the lineNumbers option is true.316function setGuttersForLineNumbers(options) {317var found = indexOf(options.gutters, "CodeMirror-linenumbers");318if (found == -1 && options.lineNumbers) {319options.gutters = options.gutters.concat(["CodeMirror-linenumbers"]);320} else if (found > -1 && !options.lineNumbers) {321options.gutters = options.gutters.slice(0);322options.gutters.splice(found, 1);323}324}325326// SCROLLBARS327328// Re-synchronize the fake scrollbars with the actual size of the329// content. Optionally force a scrollTop.330function updateScrollbars(cm) {331var d = cm.display, docHeight = cm.doc.height;332var totalHeight = docHeight + paddingVert(d);333d.sizer.style.minHeight = d.heightForcer.style.top = totalHeight + "px";334d.gutters.style.height = Math.max(totalHeight, d.scroller.clientHeight - scrollerCutOff) + "px";335var scrollHeight = Math.max(totalHeight, d.scroller.scrollHeight);336var needsH = d.scroller.scrollWidth > (d.scroller.clientWidth + 1);337var needsV = scrollHeight > (d.scroller.clientHeight + 1);338if (needsV) {339d.scrollbarV.style.display = "block";340d.scrollbarV.style.bottom = needsH ? scrollbarWidth(d.measure) + "px" : "0";341d.scrollbarV.firstChild.style.height =342(scrollHeight - d.scroller.clientHeight + d.scrollbarV.clientHeight) + "px";343} else {344d.scrollbarV.style.display = "";345d.scrollbarV.firstChild.style.height = "0";346}347if (needsH) {348d.scrollbarH.style.display = "block";349d.scrollbarH.style.right = needsV ? scrollbarWidth(d.measure) + "px" : "0";350d.scrollbarH.firstChild.style.width =351(d.scroller.scrollWidth - d.scroller.clientWidth + d.scrollbarH.clientWidth) + "px";352} else {353d.scrollbarH.style.display = "";354d.scrollbarH.firstChild.style.width = "0";355}356if (needsH && needsV) {357d.scrollbarFiller.style.display = "block";358d.scrollbarFiller.style.height = d.scrollbarFiller.style.width = scrollbarWidth(d.measure) + "px";359} else d.scrollbarFiller.style.display = "";360if (needsH && cm.options.coverGutterNextToScrollbar && cm.options.fixedGutter) {361d.gutterFiller.style.display = "block";362d.gutterFiller.style.height = scrollbarWidth(d.measure) + "px";363d.gutterFiller.style.width = d.gutters.offsetWidth + "px";364} else d.gutterFiller.style.display = "";365366if (mac_geLion && scrollbarWidth(d.measure) === 0) {367d.scrollbarV.style.minWidth = d.scrollbarH.style.minHeight = mac_geMountainLion ? "18px" : "12px";368d.scrollbarV.style.pointerEvents = d.scrollbarH.style.pointerEvents = "none";369}370}371372function visibleLines(display, doc, viewPort) {373var top = display.scroller.scrollTop, height = display.wrapper.clientHeight;374if (typeof viewPort == "number") top = viewPort;375else if (viewPort) {top = viewPort.top; height = viewPort.bottom - viewPort.top;}376top = Math.floor(top - paddingTop(display));377var bottom = Math.ceil(top + height);378return {from: lineAtHeight(doc, top), to: lineAtHeight(doc, bottom)};379}380381// LINE NUMBERS382383function alignHorizontally(cm) {384var display = cm.display;385if (!display.alignWidgets && (!display.gutters.firstChild || !cm.options.fixedGutter)) return;386var comp = compensateForHScroll(display) - display.scroller.scrollLeft + cm.doc.scrollLeft;387var gutterW = display.gutters.offsetWidth, l = comp + "px";388for (var n = display.lineDiv.firstChild; n; n = n.nextSibling) if (n.alignable) {389for (var i = 0, a = n.alignable; i < a.length; ++i) a[i].style.left = l;390}391if (cm.options.fixedGutter)392display.gutters.style.left = (comp + gutterW) + "px";393}394395function maybeUpdateLineNumberWidth(cm) {396if (!cm.options.lineNumbers) return false;397var doc = cm.doc, last = lineNumberFor(cm.options, doc.first + doc.size - 1), display = cm.display;398if (last.length != display.lineNumChars) {399var test = display.measure.appendChild(elt("div", [elt("div", last)],400"CodeMirror-linenumber CodeMirror-gutter-elt"));401var innerW = test.firstChild.offsetWidth, padding = test.offsetWidth - innerW;402display.lineGutter.style.width = "";403display.lineNumInnerWidth = Math.max(innerW, display.lineGutter.offsetWidth - padding);404display.lineNumWidth = display.lineNumInnerWidth + padding;405display.lineNumChars = display.lineNumInnerWidth ? last.length : -1;406display.lineGutter.style.width = display.lineNumWidth + "px";407return true;408}409return false;410}411412function lineNumberFor(options, i) {413return String(options.lineNumberFormatter(i + options.firstLineNumber));414}415function compensateForHScroll(display) {416return getRect(display.scroller).left - getRect(display.sizer).left;417}418419// DISPLAY DRAWING420421function updateDisplay(cm, changes, viewPort, forced) {422var oldFrom = cm.display.showingFrom, oldTo = cm.display.showingTo, updated;423var visible = visibleLines(cm.display, cm.doc, viewPort);424for (var first = true;; first = false) {425var oldWidth = cm.display.scroller.clientWidth;426if (!updateDisplayInner(cm, changes, visible, forced)) break;427updated = true;428changes = [];429updateSelection(cm);430updateScrollbars(cm);431if (first && cm.options.lineWrapping && oldWidth != cm.display.scroller.clientWidth) {432forced = true;433continue;434}435forced = false;436437// Clip forced viewport to actual scrollable area438if (viewPort)439viewPort = Math.min(cm.display.scroller.scrollHeight - cm.display.scroller.clientHeight,440typeof viewPort == "number" ? viewPort : viewPort.top);441visible = visibleLines(cm.display, cm.doc, viewPort);442if (visible.from >= cm.display.showingFrom && visible.to <= cm.display.showingTo)443break;444}445446if (updated) {447signalLater(cm, "update", cm);448if (cm.display.showingFrom != oldFrom || cm.display.showingTo != oldTo)449signalLater(cm, "viewportChange", cm, cm.display.showingFrom, cm.display.showingTo);450}451return updated;452}453454// Uses a set of changes plus the current scroll position to455// determine which DOM updates have to be made, and makes the456// updates.457function updateDisplayInner(cm, changes, visible, forced) {458var display = cm.display, doc = cm.doc;459if (!display.wrapper.offsetWidth) {460display.showingFrom = display.showingTo = doc.first;461display.viewOffset = 0;462return;463}464465// Bail out if the visible area is already rendered and nothing changed.466if (!forced && changes.length == 0 &&467visible.from > display.showingFrom && visible.to < display.showingTo)468return;469470if (maybeUpdateLineNumberWidth(cm))471changes = [{from: doc.first, to: doc.first + doc.size}];472var gutterW = display.sizer.style.marginLeft = display.gutters.offsetWidth + "px";473display.scrollbarH.style.left = cm.options.fixedGutter ? gutterW : "0";474475// Used to determine which lines need their line numbers updated476var positionsChangedFrom = Infinity;477if (cm.options.lineNumbers)478for (var i = 0; i < changes.length; ++i)479if (changes[i].diff && changes[i].from < positionsChangedFrom) { positionsChangedFrom = changes[i].from; }480481var end = doc.first + doc.size;482var from = Math.max(visible.from - cm.options.viewportMargin, doc.first);483var to = Math.min(end, visible.to + cm.options.viewportMargin);484if (display.showingFrom < from && from - display.showingFrom < 20) from = Math.max(doc.first, display.showingFrom);485if (display.showingTo > to && display.showingTo - to < 20) to = Math.min(end, display.showingTo);486if (sawCollapsedSpans) {487from = lineNo(visualLine(doc, getLine(doc, from)));488while (to < end && lineIsHidden(doc, getLine(doc, to))) ++to;489}490491// Create a range of theoretically intact lines, and punch holes492// in that using the change info.493var intact = [{from: Math.max(display.showingFrom, doc.first),494to: Math.min(display.showingTo, end)}];495if (intact[0].from >= intact[0].to) intact = [];496else intact = computeIntact(intact, changes);497// When merged lines are present, we might have to reduce the498// intact ranges because changes in continued fragments of the499// intact lines do require the lines to be redrawn.500if (sawCollapsedSpans)501for (var i = 0; i < intact.length; ++i) {502var range = intact[i], merged;503while (merged = collapsedSpanAtEnd(getLine(doc, range.to - 1))) {504var newTo = merged.find().from.line;505if (newTo > range.from) range.to = newTo;506else { intact.splice(i--, 1); break; }507}508}509510// Clip off the parts that won't be visible511var intactLines = 0;512for (var i = 0; i < intact.length; ++i) {513var range = intact[i];514if (range.from < from) range.from = from;515if (range.to > to) range.to = to;516if (range.from >= range.to) intact.splice(i--, 1);517else intactLines += range.to - range.from;518}519if (!forced && intactLines == to - from && from == display.showingFrom && to == display.showingTo) {520updateViewOffset(cm);521return;522}523intact.sort(function(a, b) {return a.from - b.from;});524525// Avoid crashing on IE's "unspecified error" when in iframes526try {527var focused = document.activeElement;528} catch(e) {}529if (intactLines < (to - from) * .7) display.lineDiv.style.display = "none";530patchDisplay(cm, from, to, intact, positionsChangedFrom);531display.lineDiv.style.display = "";532if (focused && document.activeElement != focused && focused.offsetHeight) focused.focus();533534var different = from != display.showingFrom || to != display.showingTo ||535display.lastSizeC != display.wrapper.clientHeight;536// This is just a bogus formula that detects when the editor is537// resized or the font size changes.538if (different) {539display.lastSizeC = display.wrapper.clientHeight;540startWorker(cm, 400);541}542display.showingFrom = from; display.showingTo = to;543544display.gutters.style.height = "";545updateHeightsInViewport(cm);546updateViewOffset(cm);547548return true;549}550551function updateHeightsInViewport(cm) {552var display = cm.display;553var prevBottom = display.lineDiv.offsetTop;554for (var node = display.lineDiv.firstChild, height; node; node = node.nextSibling) if (node.lineObj) {555if (ie_lt8) {556var bot = node.offsetTop + node.offsetHeight;557height = bot - prevBottom;558prevBottom = bot;559} else {560var box = getRect(node);561height = box.bottom - box.top;562}563var diff = node.lineObj.height - height;564if (height < 2) height = textHeight(display);565if (diff > .001 || diff < -.001) {566updateLineHeight(node.lineObj, height);567var widgets = node.lineObj.widgets;568if (widgets) for (var i = 0; i < widgets.length; ++i)569widgets[i].height = widgets[i].node.offsetHeight;570}571}572}573574function updateViewOffset(cm) {575var off = cm.display.viewOffset = heightAtLine(cm, getLine(cm.doc, cm.display.showingFrom));576// Position the mover div to align with the current virtual scroll position577cm.display.mover.style.top = off + "px";578}579580function computeIntact(intact, changes) {581for (var i = 0, l = changes.length || 0; i < l; ++i) {582var change = changes[i], intact2 = [], diff = change.diff || 0;583for (var j = 0, l2 = intact.length; j < l2; ++j) {584var range = intact[j];585if (change.to <= range.from && change.diff) {586intact2.push({from: range.from + diff, to: range.to + diff});587} else if (change.to <= range.from || change.from >= range.to) {588intact2.push(range);589} else {590if (change.from > range.from)591intact2.push({from: range.from, to: change.from});592if (change.to < range.to)593intact2.push({from: change.to + diff, to: range.to + diff});594}595}596intact = intact2;597}598return intact;599}600601function getDimensions(cm) {602var d = cm.display, left = {}, width = {};603for (var n = d.gutters.firstChild, i = 0; n; n = n.nextSibling, ++i) {604left[cm.options.gutters[i]] = n.offsetLeft;605width[cm.options.gutters[i]] = n.offsetWidth;606}607return {fixedPos: compensateForHScroll(d),608gutterTotalWidth: d.gutters.offsetWidth,609gutterLeft: left,610gutterWidth: width,611wrapperWidth: d.wrapper.clientWidth};612}613614function patchDisplay(cm, from, to, intact, updateNumbersFrom) {615var dims = getDimensions(cm);616var display = cm.display, lineNumbers = cm.options.lineNumbers;617if (!intact.length && (!webkit || !cm.display.currentWheelTarget))618removeChildren(display.lineDiv);619var container = display.lineDiv, cur = container.firstChild;620621function rm(node) {622var next = node.nextSibling;623if (webkit && mac && cm.display.currentWheelTarget == node) {624node.style.display = "none";625node.lineObj = null;626} else {627node.parentNode.removeChild(node);628}629return next;630}631632var nextIntact = intact.shift(), lineN = from;633cm.doc.iter(from, to, function(line) {634if (nextIntact && nextIntact.to == lineN) nextIntact = intact.shift();635if (lineIsHidden(cm.doc, line)) {636if (line.height != 0) updateLineHeight(line, 0);637if (line.widgets && cur && cur.previousSibling) for (var i = 0; i < line.widgets.length; ++i) {638var w = line.widgets[i];639if (w.showIfHidden) {640var prev = cur.previousSibling;641if (/pre/i.test(prev.nodeName)) {642var wrap = elt("div", null, null, "position: relative");643prev.parentNode.replaceChild(wrap, prev);644wrap.appendChild(prev);645prev = wrap;646}647var wnode = prev.appendChild(elt("div", [w.node], "CodeMirror-linewidget"));648if (!w.handleMouseEvents) wnode.ignoreEvents = true;649positionLineWidget(w, wnode, prev, dims);650}651}652} else if (nextIntact && nextIntact.from <= lineN && nextIntact.to > lineN) {653// This line is intact. Skip to the actual node. Update its654// line number if needed.655while (cur.lineObj != line) cur = rm(cur);656if (lineNumbers && updateNumbersFrom <= lineN && cur.lineNumber)657setTextContent(cur.lineNumber, lineNumberFor(cm.options, lineN));658cur = cur.nextSibling;659} else {660// For lines with widgets, make an attempt to find and reuse661// the existing element, so that widgets aren't needlessly662// removed and re-inserted into the dom663if (line.widgets) for (var j = 0, search = cur, reuse; search && j < 20; ++j, search = search.nextSibling)664if (search.lineObj == line && /div/i.test(search.nodeName)) { reuse = search; break; }665// This line needs to be generated.666var lineNode = buildLineElement(cm, line, lineN, dims, reuse);667if (lineNode != reuse) {668container.insertBefore(lineNode, cur);669} else {670while (cur != reuse) cur = rm(cur);671cur = cur.nextSibling;672}673674lineNode.lineObj = line;675}676++lineN;677});678while (cur) cur = rm(cur);679}680681function buildLineElement(cm, line, lineNo, dims, reuse) {682var built = buildLineContent(cm, line), lineElement = built.pre;683var markers = line.gutterMarkers, display = cm.display, wrap;684685var bgClass = built.bgClass ? built.bgClass + " " + (line.bgClass || "") : line.bgClass;686if (!cm.options.lineNumbers && !markers && !bgClass && !line.wrapClass && !line.widgets)687return lineElement;688689// Lines with gutter elements, widgets or a background class need690// to be wrapped again, and have the extra elements added to the691// wrapper div692693if (reuse) {694reuse.alignable = null;695var isOk = true, widgetsSeen = 0, insertBefore = null;696for (var n = reuse.firstChild, next; n; n = next) {697next = n.nextSibling;698if (!/\bCodeMirror-linewidget\b/.test(n.className)) {699reuse.removeChild(n);700} else {701for (var i = 0; i < line.widgets.length; ++i) {702var widget = line.widgets[i];703if (widget.node == n.firstChild) {704if (!widget.above && !insertBefore) insertBefore = n;705positionLineWidget(widget, n, reuse, dims);706++widgetsSeen;707break;708}709}710if (i == line.widgets.length) { isOk = false; break; }711}712}713reuse.insertBefore(lineElement, insertBefore);714if (isOk && widgetsSeen == line.widgets.length) {715wrap = reuse;716reuse.className = line.wrapClass || "";717}718}719if (!wrap) {720wrap = elt("div", null, line.wrapClass, "position: relative");721wrap.appendChild(lineElement);722}723// Kludge to make sure the styled element lies behind the selection (by z-index)724if (bgClass)725wrap.insertBefore(elt("div", null, bgClass + " CodeMirror-linebackground"), wrap.firstChild);726if (cm.options.lineNumbers || markers) {727var gutterWrap = wrap.insertBefore(elt("div", null, "CodeMirror-gutter-wrapper", "position: absolute; left: " +728(cm.options.fixedGutter ? dims.fixedPos : -dims.gutterTotalWidth) + "px"),729lineElement);730if (cm.options.fixedGutter) (wrap.alignable || (wrap.alignable = [])).push(gutterWrap);731if (cm.options.lineNumbers && (!markers || !markers["CodeMirror-linenumbers"]))732wrap.lineNumber = gutterWrap.appendChild(733elt("div", lineNumberFor(cm.options, lineNo),734"CodeMirror-linenumber CodeMirror-gutter-elt",735"left: " + dims.gutterLeft["CodeMirror-linenumbers"] + "px; width: "736+ display.lineNumInnerWidth + "px"));737if (markers)738for (var k = 0; k < cm.options.gutters.length; ++k) {739var id = cm.options.gutters[k], found = markers.hasOwnProperty(id) && markers[id];740if (found)741gutterWrap.appendChild(elt("div", [found], "CodeMirror-gutter-elt", "left: " +742dims.gutterLeft[id] + "px; width: " + dims.gutterWidth[id] + "px"));743}744}745if (ie_lt8) wrap.style.zIndex = 2;746if (line.widgets && wrap != reuse) for (var i = 0, ws = line.widgets; i < ws.length; ++i) {747var widget = ws[i], node = elt("div", [widget.node], "CodeMirror-linewidget");748if (!widget.handleMouseEvents) node.ignoreEvents = true;749positionLineWidget(widget, node, wrap, dims);750if (widget.above)751wrap.insertBefore(node, cm.options.lineNumbers && line.height != 0 ? gutterWrap : lineElement);752else753wrap.appendChild(node);754signalLater(widget, "redraw");755}756return wrap;757}758759function positionLineWidget(widget, node, wrap, dims) {760if (widget.noHScroll) {761(wrap.alignable || (wrap.alignable = [])).push(node);762var width = dims.wrapperWidth;763node.style.left = dims.fixedPos + "px";764if (!widget.coverGutter) {765width -= dims.gutterTotalWidth;766node.style.paddingLeft = dims.gutterTotalWidth + "px";767}768node.style.width = width + "px";769}770if (widget.coverGutter) {771node.style.zIndex = 5;772node.style.position = "relative";773if (!widget.noHScroll) node.style.marginLeft = -dims.gutterTotalWidth + "px";774}775}776777// SELECTION / CURSOR778779function updateSelection(cm) {780var display = cm.display;781var collapsed = posEq(cm.doc.sel.from, cm.doc.sel.to);782if (collapsed || cm.options.showCursorWhenSelecting)783updateSelectionCursor(cm);784else785display.cursor.style.display = display.otherCursor.style.display = "none";786if (!collapsed)787updateSelectionRange(cm);788else789display.selectionDiv.style.display = "none";790791// Move the hidden textarea near the cursor to prevent scrolling artifacts792if (cm.options.moveInputWithCursor) {793var headPos = cursorCoords(cm, cm.doc.sel.head, "div");794var wrapOff = getRect(display.wrapper), lineOff = getRect(display.lineDiv);795display.inputDiv.style.top = Math.max(0, Math.min(display.wrapper.clientHeight - 10,796headPos.top + lineOff.top - wrapOff.top)) + "px";797display.inputDiv.style.left = Math.max(0, Math.min(display.wrapper.clientWidth - 10,798headPos.left + lineOff.left - wrapOff.left)) + "px";799}800}801802// No selection, plain cursor803function updateSelectionCursor(cm) {804var display = cm.display, pos = cursorCoords(cm, cm.doc.sel.head, "div");805display.cursor.style.left = pos.left + "px";806display.cursor.style.top = pos.top + "px";807display.cursor.style.height = Math.max(0, pos.bottom - pos.top) * cm.options.cursorHeight + "px";808display.cursor.style.display = "";809810if (pos.other) {811display.otherCursor.style.display = "";812display.otherCursor.style.left = pos.other.left + "px";813display.otherCursor.style.top = pos.other.top + "px";814display.otherCursor.style.height = (pos.other.bottom - pos.other.top) * .85 + "px";815} else { display.otherCursor.style.display = "none"; }816}817818// Highlight selection819function updateSelectionRange(cm) {820var display = cm.display, doc = cm.doc, sel = cm.doc.sel;821var fragment = document.createDocumentFragment();822var clientWidth = display.lineSpace.offsetWidth, pl = paddingLeft(cm.display);823824function add(left, top, width, bottom) {825if (top < 0) top = 0;826fragment.appendChild(elt("div", null, "CodeMirror-selected", "position: absolute; left: " + left +827"px; top: " + top + "px; width: " + (width == null ? clientWidth - left : width) +828"px; height: " + (bottom - top) + "px"));829}830831function drawForLine(line, fromArg, toArg) {832var lineObj = getLine(doc, line);833var lineLen = lineObj.text.length;834var start, end;835function coords(ch, bias) {836return charCoords(cm, Pos(line, ch), "div", lineObj, bias);837}838839iterateBidiSections(getOrder(lineObj), fromArg || 0, toArg == null ? lineLen : toArg, function(from, to, dir) {840var leftPos = coords(from, "left"), rightPos, left, right;841if (from == to) {842rightPos = leftPos;843left = right = leftPos.left;844} else {845rightPos = coords(to - 1, "right");846if (dir == "rtl") { var tmp = leftPos; leftPos = rightPos; rightPos = tmp; }847left = leftPos.left;848right = rightPos.right;849}850if (fromArg == null && from == 0) left = pl;851if (rightPos.top - leftPos.top > 3) { // Different lines, draw top part852add(left, leftPos.top, null, leftPos.bottom);853left = pl;854if (leftPos.bottom < rightPos.top) add(left, leftPos.bottom, null, rightPos.top);855}856if (toArg == null && to == lineLen) right = clientWidth;857if (!start || leftPos.top < start.top || leftPos.top == start.top && leftPos.left < start.left)858start = leftPos;859if (!end || rightPos.bottom > end.bottom || rightPos.bottom == end.bottom && rightPos.right > end.right)860end = rightPos;861if (left < pl + 1) left = pl;862add(left, rightPos.top, right - left, rightPos.bottom);863});864return {start: start, end: end};865}866867if (sel.from.line == sel.to.line) {868drawForLine(sel.from.line, sel.from.ch, sel.to.ch);869} else {870var fromLine = getLine(doc, sel.from.line), toLine = getLine(doc, sel.to.line);871var singleVLine = visualLine(doc, fromLine) == visualLine(doc, toLine);872var leftEnd = drawForLine(sel.from.line, sel.from.ch, singleVLine ? fromLine.text.length : null).end;873var rightStart = drawForLine(sel.to.line, singleVLine ? 0 : null, sel.to.ch).start;874if (singleVLine) {875if (leftEnd.top < rightStart.top - 2) {876add(leftEnd.right, leftEnd.top, null, leftEnd.bottom);877add(pl, rightStart.top, rightStart.left, rightStart.bottom);878} else {879add(leftEnd.right, leftEnd.top, rightStart.left - leftEnd.right, leftEnd.bottom);880}881}882if (leftEnd.bottom < rightStart.top)883add(pl, leftEnd.bottom, null, rightStart.top);884}885886removeChildrenAndAdd(display.selectionDiv, fragment);887display.selectionDiv.style.display = "";888}889890// Cursor-blinking891function restartBlink(cm) {892if (!cm.state.focused) return;893var display = cm.display;894clearInterval(display.blinker);895var on = true;896display.cursor.style.visibility = display.otherCursor.style.visibility = "";897if (cm.options.cursorBlinkRate > 0)898display.blinker = setInterval(function() {899display.cursor.style.visibility = display.otherCursor.style.visibility = (on = !on) ? "" : "hidden";900}, cm.options.cursorBlinkRate);901}902903// HIGHLIGHT WORKER904905function startWorker(cm, time) {906if (cm.doc.mode.startState && cm.doc.frontier < cm.display.showingTo)907cm.state.highlight.set(time, bind(highlightWorker, cm));908}909910function highlightWorker(cm) {911var doc = cm.doc;912if (doc.frontier < doc.first) doc.frontier = doc.first;913if (doc.frontier >= cm.display.showingTo) return;914var end = +new Date + cm.options.workTime;915var state = copyState(doc.mode, getStateBefore(cm, doc.frontier));916var changed = [], prevChange;917doc.iter(doc.frontier, Math.min(doc.first + doc.size, cm.display.showingTo + 500), function(line) {918if (doc.frontier >= cm.display.showingFrom) { // Visible919var oldStyles = line.styles;920line.styles = highlightLine(cm, line, state, true);921var ischange = !oldStyles || oldStyles.length != line.styles.length;922for (var i = 0; !ischange && i < oldStyles.length; ++i) ischange = oldStyles[i] != line.styles[i];923if (ischange) {924if (prevChange && prevChange.end == doc.frontier) prevChange.end++;925else changed.push(prevChange = {start: doc.frontier, end: doc.frontier + 1});926}927line.stateAfter = copyState(doc.mode, state);928} else {929processLine(cm, line.text, state);930line.stateAfter = doc.frontier % 5 == 0 ? copyState(doc.mode, state) : null;931}932++doc.frontier;933if (+new Date > end) {934startWorker(cm, cm.options.workDelay);935return true;936}937});938if (changed.length)939operation(cm, function() {940for (var i = 0; i < changed.length; ++i)941regChange(this, changed[i].start, changed[i].end);942})();943}944945// Finds the line to start with when starting a parse. Tries to946// find a line with a stateAfter, so that it can start with a947// valid state. If that fails, it returns the line with the948// smallest indentation, which tends to need the least context to949// parse correctly.950function findStartLine(cm, n, precise) {951var minindent, minline, doc = cm.doc;952var lim = precise ? -1 : n - (cm.doc.mode.innerMode ? 1000 : 100);953for (var search = n; search > lim; --search) {954if (search <= doc.first) return doc.first;955var line = getLine(doc, search - 1);956if (line.stateAfter && (!precise || search <= doc.frontier)) return search;957var indented = countColumn(line.text, null, cm.options.tabSize);958if (minline == null || minindent > indented) {959minline = search - 1;960minindent = indented;961}962}963return minline;964}965966function getStateBefore(cm, n, precise) {967var doc = cm.doc, display = cm.display;968if (!doc.mode.startState) return true;969var pos = findStartLine(cm, n, precise), state = pos > doc.first && getLine(doc, pos-1).stateAfter;970if (!state) state = startState(doc.mode);971else state = copyState(doc.mode, state);972doc.iter(pos, n, function(line) {973processLine(cm, line.text, state);974var save = pos == n - 1 || pos % 5 == 0 || pos >= display.showingFrom && pos < display.showingTo;975line.stateAfter = save ? copyState(doc.mode, state) : null;976++pos;977});978if (precise) doc.frontier = pos;979return state;980}981982// POSITION MEASUREMENT983984function paddingTop(display) {return display.lineSpace.offsetTop;}985function paddingVert(display) {return display.mover.offsetHeight - display.lineSpace.offsetHeight;}986function paddingLeft(display) {987var e = removeChildrenAndAdd(display.measure, elt("pre", null, null, "text-align: left")).appendChild(elt("span", "x"));988return e.offsetLeft;989}990991function measureChar(cm, line, ch, data, bias) {992var dir = -1;993data = data || measureLine(cm, line);994if (data.crude) {995var left = data.left + ch * data.width;996return {left: left, right: left + data.width, top: data.top, bottom: data.bottom};997}998999for (var pos = ch;; pos += dir) {1000var r = data[pos];1001if (r) break;1002if (dir < 0 && pos == 0) dir = 1;1003}1004bias = pos > ch ? "left" : pos < ch ? "right" : bias;1005if (bias == "left" && r.leftSide) r = r.leftSide;1006else if (bias == "right" && r.rightSide) r = r.rightSide;1007return {left: pos < ch ? r.right : r.left,1008right: pos > ch ? r.left : r.right,1009top: r.top,1010bottom: r.bottom};1011}10121013function findCachedMeasurement(cm, line) {1014var cache = cm.display.measureLineCache;1015for (var i = 0; i < cache.length; ++i) {1016var memo = cache[i];1017if (memo.text == line.text && memo.markedSpans == line.markedSpans &&1018cm.display.scroller.clientWidth == memo.width &&1019memo.classes == line.textClass + "|" + line.wrapClass)1020return memo;1021}1022}10231024function clearCachedMeasurement(cm, line) {1025var exists = findCachedMeasurement(cm, line);1026if (exists) exists.text = exists.measure = exists.markedSpans = null;1027}10281029function measureLine(cm, line) {1030// First look in the cache1031var cached = findCachedMeasurement(cm, line);1032if (cached) return cached.measure;10331034// Failing that, recompute and store result in cache1035var measure = measureLineInner(cm, line);1036var cache = cm.display.measureLineCache;1037var memo = {text: line.text, width: cm.display.scroller.clientWidth,1038markedSpans: line.markedSpans, measure: measure,1039classes: line.textClass + "|" + line.wrapClass};1040if (cache.length == 16) cache[++cm.display.measureLineCachePos % 16] = memo;1041else cache.push(memo);1042return measure;1043}10441045function measureLineInner(cm, line) {1046if (!cm.options.lineWrapping && line.text.length >= cm.options.crudeMeasuringFrom)1047return crudelyMeasureLine(cm, line);10481049var display = cm.display, measure = emptyArray(line.text.length);1050var pre = buildLineContent(cm, line, measure, true).pre;10511052// IE does not cache element positions of inline elements between1053// calls to getBoundingClientRect. This makes the loop below,1054// which gathers the positions of all the characters on the line,1055// do an amount of layout work quadratic to the number of1056// characters. When line wrapping is off, we try to improve things1057// by first subdividing the line into a bunch of inline blocks, so1058// that IE can reuse most of the layout information from caches1059// for those blocks. This does interfere with line wrapping, so it1060// doesn't work when wrapping is on, but in that case the1061// situation is slightly better, since IE does cache line-wrapping1062// information and only recomputes per-line.1063if (old_ie && !ie_lt8 && !cm.options.lineWrapping && pre.childNodes.length > 100) {1064var fragment = document.createDocumentFragment();1065var chunk = 10, n = pre.childNodes.length;1066for (var i = 0, chunks = Math.ceil(n / chunk); i < chunks; ++i) {1067var wrap = elt("div", null, null, "display: inline-block");1068for (var j = 0; j < chunk && n; ++j) {1069wrap.appendChild(pre.firstChild);1070--n;1071}1072fragment.appendChild(wrap);1073}1074pre.appendChild(fragment);1075}10761077removeChildrenAndAdd(display.measure, pre);10781079var outer = getRect(display.lineDiv);1080var vranges = [], data = emptyArray(line.text.length), maxBot = pre.offsetHeight;1081// Work around an IE7/8 bug where it will sometimes have randomly1082// replaced our pre with a clone at this point.1083if (ie_lt9 && display.measure.first != pre)1084removeChildrenAndAdd(display.measure, pre);10851086function measureRect(rect) {1087var top = rect.top - outer.top, bot = rect.bottom - outer.top;1088if (bot > maxBot) bot = maxBot;1089if (top < 0) top = 0;1090for (var i = vranges.length - 2; i >= 0; i -= 2) {1091var rtop = vranges[i], rbot = vranges[i+1];1092if (rtop > bot || rbot < top) continue;1093if (rtop <= top && rbot >= bot ||1094top <= rtop && bot >= rbot ||1095Math.min(bot, rbot) - Math.max(top, rtop) >= (bot - top) >> 1) {1096vranges[i] = Math.min(top, rtop);1097vranges[i+1] = Math.max(bot, rbot);1098break;1099}1100}1101if (i < 0) { i = vranges.length; vranges.push(top, bot); }1102return {left: rect.left - outer.left,1103right: rect.right - outer.left,1104top: i, bottom: null};1105}1106function finishRect(rect) {1107rect.bottom = vranges[rect.top+1];1108rect.top = vranges[rect.top];1109}11101111for (var i = 0, cur; i < measure.length; ++i) if (cur = measure[i]) {1112var node = cur, rect = null;1113// A widget might wrap, needs special care1114if (/\bCodeMirror-widget\b/.test(cur.className) && cur.getClientRects) {1115if (cur.firstChild.nodeType == 1) node = cur.firstChild;1116var rects = node.getClientRects();1117if (rects.length > 1) {1118rect = data[i] = measureRect(rects[0]);1119rect.rightSide = measureRect(rects[rects.length - 1]);1120}1121}1122if (!rect) rect = data[i] = measureRect(getRect(node));1123if (cur.measureRight) rect.right = getRect(cur.measureRight).left - outer.left;1124if (cur.leftSide) rect.leftSide = measureRect(getRect(cur.leftSide));1125}1126removeChildren(cm.display.measure);1127for (var i = 0, cur; i < data.length; ++i) if (cur = data[i]) {1128finishRect(cur);1129if (cur.leftSide) finishRect(cur.leftSide);1130if (cur.rightSide) finishRect(cur.rightSide);1131}1132return data;1133}11341135function crudelyMeasureLine(cm, line) {1136var copy = new Line(line.text.slice(0, 100), null);1137if (line.textClass) copy.textClass = line.textClass;1138var measure = measureLineInner(cm, copy);1139var left = measureChar(cm, copy, 0, measure, "left");1140var right = measureChar(cm, copy, 99, measure, "right");1141return {crude: true, top: left.top, left: left.left, bottom: left.bottom, width: (right.right - left.left) / 100};1142}11431144function measureLineWidth(cm, line) {1145var hasBadSpan = false;1146if (line.markedSpans) for (var i = 0; i < line.markedSpans; ++i) {1147var sp = line.markedSpans[i];1148if (sp.collapsed && (sp.to == null || sp.to == line.text.length)) hasBadSpan = true;1149}1150var cached = !hasBadSpan && findCachedMeasurement(cm, line);1151if (cached || line.text.length >= cm.options.crudeMeasuringFrom)1152return measureChar(cm, line, line.text.length, cached && cached.measure, "right").right;11531154var pre = buildLineContent(cm, line, null, true).pre;1155var end = pre.appendChild(zeroWidthElement(cm.display.measure));1156removeChildrenAndAdd(cm.display.measure, pre);1157return getRect(end).right - getRect(cm.display.lineDiv).left;1158}11591160function clearCaches(cm) {1161cm.display.measureLineCache.length = cm.display.measureLineCachePos = 0;1162cm.display.cachedCharWidth = cm.display.cachedTextHeight = null;1163if (!cm.options.lineWrapping) cm.display.maxLineChanged = true;1164cm.display.lineNumChars = null;1165}11661167function pageScrollX() { return window.pageXOffset || (document.documentElement || document.body).scrollLeft; }1168function pageScrollY() { return window.pageYOffset || (document.documentElement || document.body).scrollTop; }11691170// Context is one of "line", "div" (display.lineDiv), "local"/null (editor), or "page"1171function intoCoordSystem(cm, lineObj, rect, context) {1172if (lineObj.widgets) for (var i = 0; i < lineObj.widgets.length; ++i) if (lineObj.widgets[i].above) {1173var size = widgetHeight(lineObj.widgets[i]);1174rect.top += size; rect.bottom += size;1175}1176if (context == "line") return rect;1177if (!context) context = "local";1178var yOff = heightAtLine(cm, lineObj);1179if (context == "local") yOff += paddingTop(cm.display);1180else yOff -= cm.display.viewOffset;1181if (context == "page" || context == "window") {1182var lOff = getRect(cm.display.lineSpace);1183yOff += lOff.top + (context == "window" ? 0 : pageScrollY());1184var xOff = lOff.left + (context == "window" ? 0 : pageScrollX());1185rect.left += xOff; rect.right += xOff;1186}1187rect.top += yOff; rect.bottom += yOff;1188return rect;1189}11901191// Context may be "window", "page", "div", or "local"/null1192// Result is in "div" coords1193function fromCoordSystem(cm, coords, context) {1194if (context == "div") return coords;1195var left = coords.left, top = coords.top;1196// First move into "page" coordinate system1197if (context == "page") {1198left -= pageScrollX();1199top -= pageScrollY();1200} else if (context == "local" || !context) {1201var localBox = getRect(cm.display.sizer);1202left += localBox.left;1203top += localBox.top;1204}12051206var lineSpaceBox = getRect(cm.display.lineSpace);1207return {left: left - lineSpaceBox.left, top: top - lineSpaceBox.top};1208}12091210function charCoords(cm, pos, context, lineObj, bias) {1211if (!lineObj) lineObj = getLine(cm.doc, pos.line);1212return intoCoordSystem(cm, lineObj, measureChar(cm, lineObj, pos.ch, null, bias), context);1213}12141215function cursorCoords(cm, pos, context, lineObj, measurement) {1216lineObj = lineObj || getLine(cm.doc, pos.line);1217if (!measurement) measurement = measureLine(cm, lineObj);1218function get(ch, right) {1219var m = measureChar(cm, lineObj, ch, measurement, right ? "right" : "left");1220if (right) m.left = m.right; else m.right = m.left;1221return intoCoordSystem(cm, lineObj, m, context);1222}1223function getBidi(ch, partPos) {1224var part = order[partPos], right = part.level % 2;1225if (ch == bidiLeft(part) && partPos && part.level < order[partPos - 1].level) {1226part = order[--partPos];1227ch = bidiRight(part) - (part.level % 2 ? 0 : 1);1228right = true;1229} else if (ch == bidiRight(part) && partPos < order.length - 1 && part.level < order[partPos + 1].level) {1230part = order[++partPos];1231ch = bidiLeft(part) - part.level % 2;1232right = false;1233}1234if (right && ch == part.to && ch > part.from) return get(ch - 1);1235return get(ch, right);1236}1237var order = getOrder(lineObj), ch = pos.ch;1238if (!order) return get(ch);1239var partPos = getBidiPartAt(order, ch);1240var val = getBidi(ch, partPos);1241if (bidiOther != null) val.other = getBidi(ch, bidiOther);1242return val;1243}12441245function PosWithInfo(line, ch, outside, xRel) {1246var pos = new Pos(line, ch);1247pos.xRel = xRel;1248if (outside) pos.outside = true;1249return pos;1250}12511252// Coords must be lineSpace-local1253function coordsChar(cm, x, y) {1254var doc = cm.doc;1255y += cm.display.viewOffset;1256if (y < 0) return PosWithInfo(doc.first, 0, true, -1);1257var lineNo = lineAtHeight(doc, y), last = doc.first + doc.size - 1;1258if (lineNo > last)1259return PosWithInfo(doc.first + doc.size - 1, getLine(doc, last).text.length, true, 1);1260if (x < 0) x = 0;12611262for (;;) {1263var lineObj = getLine(doc, lineNo);1264var found = coordsCharInner(cm, lineObj, lineNo, x, y);1265var merged = collapsedSpanAtEnd(lineObj);1266var mergedPos = merged && merged.find();1267if (merged && (found.ch > mergedPos.from.ch || found.ch == mergedPos.from.ch && found.xRel > 0))1268lineNo = mergedPos.to.line;1269else1270return found;1271}1272}12731274function coordsCharInner(cm, lineObj, lineNo, x, y) {1275var innerOff = y - heightAtLine(cm, lineObj);1276var wrongLine = false, adjust = 2 * cm.display.wrapper.clientWidth;1277var measurement = measureLine(cm, lineObj);12781279function getX(ch) {1280var sp = cursorCoords(cm, Pos(lineNo, ch), "line",1281lineObj, measurement);1282wrongLine = true;1283if (innerOff > sp.bottom) return sp.left - adjust;1284else if (innerOff < sp.top) return sp.left + adjust;1285else wrongLine = false;1286return sp.left;1287}12881289var bidi = getOrder(lineObj), dist = lineObj.text.length;1290var from = lineLeft(lineObj), to = lineRight(lineObj);1291var fromX = getX(from), fromOutside = wrongLine, toX = getX(to), toOutside = wrongLine;12921293if (x > toX) return PosWithInfo(lineNo, to, toOutside, 1);1294// Do a binary search between these bounds.1295for (;;) {1296if (bidi ? to == from || to == moveVisually(lineObj, from, 1) : to - from <= 1) {1297var ch = x < fromX || x - fromX <= toX - x ? from : to;1298var xDiff = x - (ch == from ? fromX : toX);1299while (isExtendingChar(lineObj.text.charAt(ch))) ++ch;1300var pos = PosWithInfo(lineNo, ch, ch == from ? fromOutside : toOutside,1301xDiff < 0 ? -1 : xDiff ? 1 : 0);1302return pos;1303}1304var step = Math.ceil(dist / 2), middle = from + step;1305if (bidi) {1306middle = from;1307for (var i = 0; i < step; ++i) middle = moveVisually(lineObj, middle, 1);1308}1309var middleX = getX(middle);1310if (middleX > x) {to = middle; toX = middleX; if (toOutside = wrongLine) toX += 1000; dist = step;}1311else {from = middle; fromX = middleX; fromOutside = wrongLine; dist -= step;}1312}1313}13141315var measureText;1316function textHeight(display) {1317if (display.cachedTextHeight != null) return display.cachedTextHeight;1318if (measureText == null) {1319measureText = elt("pre");1320// Measure a bunch of lines, for browsers that compute1321// fractional heights.1322for (var i = 0; i < 49; ++i) {1323measureText.appendChild(document.createTextNode("x"));1324measureText.appendChild(elt("br"));1325}1326measureText.appendChild(document.createTextNode("x"));1327}1328removeChildrenAndAdd(display.measure, measureText);1329var height = measureText.offsetHeight / 50;1330if (height > 3) display.cachedTextHeight = height;1331removeChildren(display.measure);1332return height || 1;1333}13341335function charWidth(display) {1336if (display.cachedCharWidth != null) return display.cachedCharWidth;1337var anchor = elt("span", "x");1338var pre = elt("pre", [anchor]);1339removeChildrenAndAdd(display.measure, pre);1340var width = anchor.offsetWidth;1341if (width > 2) display.cachedCharWidth = width;1342return width || 10;1343}13441345// OPERATIONS13461347// Operations are used to wrap changes in such a way that each1348// change won't have to update the cursor and display (which would1349// be awkward, slow, and error-prone), but instead updates are1350// batched and then all combined and executed at once.13511352var nextOpId = 0;1353function startOperation(cm) {1354cm.curOp = {1355// An array of ranges of lines that have to be updated. See1356// updateDisplay.1357changes: [],1358forceUpdate: false,1359updateInput: null,1360userSelChange: null,1361textChanged: null,1362selectionChanged: false,1363cursorActivity: false,1364updateMaxLine: false,1365updateScrollPos: false,1366id: ++nextOpId1367};1368if (!delayedCallbackDepth++) delayedCallbacks = [];1369}13701371function endOperation(cm) {1372var op = cm.curOp, doc = cm.doc, display = cm.display;1373cm.curOp = null;13741375if (op.updateMaxLine) computeMaxLength(cm);1376if (display.maxLineChanged && !cm.options.lineWrapping && display.maxLine) {1377var width = measureLineWidth(cm, display.maxLine);1378display.sizer.style.minWidth = Math.max(0, width + 3 + scrollerCutOff) + "px";1379display.maxLineChanged = false;1380var maxScrollLeft = Math.max(0, display.sizer.offsetLeft + display.sizer.offsetWidth - display.scroller.clientWidth);1381if (maxScrollLeft < doc.scrollLeft && !op.updateScrollPos)1382setScrollLeft(cm, Math.min(display.scroller.scrollLeft, maxScrollLeft), true);1383}1384var newScrollPos, updated;1385if (op.updateScrollPos) {1386newScrollPos = op.updateScrollPos;1387} else if (op.selectionChanged && display.scroller.clientHeight) { // don't rescroll if not visible1388var coords = cursorCoords(cm, doc.sel.head);1389newScrollPos = calculateScrollPos(cm, coords.left, coords.top, coords.left, coords.bottom);1390}1391if (op.changes.length || op.forceUpdate || newScrollPos && newScrollPos.scrollTop != null) {1392updated = updateDisplay(cm, op.changes, newScrollPos && newScrollPos.scrollTop, op.forceUpdate);1393if (cm.display.scroller.offsetHeight) cm.doc.scrollTop = cm.display.scroller.scrollTop;1394}1395if (!updated && op.selectionChanged) updateSelection(cm);1396if (op.updateScrollPos) {1397var top = Math.max(0, Math.min(display.scroller.scrollHeight - display.scroller.clientHeight, newScrollPos.scrollTop));1398var left = Math.max(0, Math.min(display.scroller.scrollWidth - display.scroller.clientWidth, newScrollPos.scrollLeft));1399display.scroller.scrollTop = display.scrollbarV.scrollTop = doc.scrollTop = top;1400display.scroller.scrollLeft = display.scrollbarH.scrollLeft = doc.scrollLeft = left;1401alignHorizontally(cm);1402if (op.scrollToPos)1403scrollPosIntoView(cm, clipPos(cm.doc, op.scrollToPos.from),1404clipPos(cm.doc, op.scrollToPos.to), op.scrollToPos.margin);1405} else if (newScrollPos) {1406scrollCursorIntoView(cm);1407}1408if (op.selectionChanged) restartBlink(cm);14091410if (cm.state.focused && op.updateInput)1411resetInput(cm, op.userSelChange);14121413var hidden = op.maybeHiddenMarkers, unhidden = op.maybeUnhiddenMarkers;1414if (hidden) for (var i = 0; i < hidden.length; ++i)1415if (!hidden[i].lines.length) signal(hidden[i], "hide");1416if (unhidden) for (var i = 0; i < unhidden.length; ++i)1417if (unhidden[i].lines.length) signal(unhidden[i], "unhide");14181419var delayed;1420if (!--delayedCallbackDepth) {1421delayed = delayedCallbacks;1422delayedCallbacks = null;1423}1424if (op.textChanged)1425signal(cm, "change", cm, op.textChanged);1426if (op.cursorActivity) signal(cm, "cursorActivity", cm);1427if (delayed) for (var i = 0; i < delayed.length; ++i) delayed[i]();1428}14291430// Wraps a function in an operation. Returns the wrapped function.1431function operation(cm1, f) {1432return function() {1433var cm = cm1 || this, withOp = !cm.curOp;1434if (withOp) startOperation(cm);1435try { var result = f.apply(cm, arguments); }1436finally { if (withOp) endOperation(cm); }1437return result;1438};1439}1440function docOperation(f) {1441return function() {1442var withOp = this.cm && !this.cm.curOp, result;1443if (withOp) startOperation(this.cm);1444try { result = f.apply(this, arguments); }1445finally { if (withOp) endOperation(this.cm); }1446return result;1447};1448}1449function runInOp(cm, f) {1450var withOp = !cm.curOp, result;1451if (withOp) startOperation(cm);1452try { result = f(); }1453finally { if (withOp) endOperation(cm); }1454return result;1455}14561457function regChange(cm, from, to, lendiff) {1458if (from == null) from = cm.doc.first;1459if (to == null) to = cm.doc.first + cm.doc.size;1460cm.curOp.changes.push({from: from, to: to, diff: lendiff});1461}14621463// INPUT HANDLING14641465function slowPoll(cm) {1466if (cm.display.pollingFast) return;1467cm.display.poll.set(cm.options.pollInterval, function() {1468readInput(cm);1469if (cm.state.focused) slowPoll(cm);1470});1471}14721473function fastPoll(cm) {1474var missed = false;1475cm.display.pollingFast = true;1476function p() {1477var changed = readInput(cm);1478if (!changed && !missed) {missed = true; cm.display.poll.set(60, p);}1479else {cm.display.pollingFast = false; slowPoll(cm);}1480}1481cm.display.poll.set(20, p);1482}14831484// prevInput is a hack to work with IME. If we reset the textarea1485// on every change, that breaks IME. So we look for changes1486// compared to the previous content instead. (Modern browsers have1487// events that indicate IME taking place, but these are not widely1488// supported or compatible enough yet to rely on.)1489function readInput(cm) {1490var input = cm.display.input, prevInput = cm.display.prevInput, doc = cm.doc, sel = doc.sel;1491if (!cm.state.focused || hasSelection(input) || isReadOnly(cm) || cm.options.disableInput) return false;1492if (cm.state.pasteIncoming && cm.state.fakedLastChar) {1493input.value = input.value.substring(0, input.value.length - 1);1494cm.state.fakedLastChar = false;1495}1496var text = input.value;1497if (text == prevInput && posEq(sel.from, sel.to)) return false;1498if (ie && !ie_lt9 && cm.display.inputHasSelection === text) {1499resetInput(cm, true);1500return false;1501}15021503var withOp = !cm.curOp;1504if (withOp) startOperation(cm);1505sel.shift = false;1506var same = 0, l = Math.min(prevInput.length, text.length);1507while (same < l && prevInput.charCodeAt(same) == text.charCodeAt(same)) ++same;1508var from = sel.from, to = sel.to;1509var inserted = text.slice(same);1510if (same < prevInput.length)1511from = Pos(from.line, from.ch - (prevInput.length - same));1512else if (cm.state.overwrite && posEq(from, to) && !cm.state.pasteIncoming)1513to = Pos(to.line, Math.min(getLine(doc, to.line).text.length, to.ch + inserted.length));15141515var updateInput = cm.curOp.updateInput;1516var changeEvent = {from: from, to: to, text: splitLines(inserted),1517origin: cm.state.pasteIncoming ? "paste" : cm.state.cutIncoming ? "cut" : "+input"};1518makeChange(cm.doc, changeEvent, "end");1519cm.curOp.updateInput = updateInput;1520signalLater(cm, "inputRead", cm, changeEvent);1521if (inserted && !cm.state.pasteIncoming && cm.options.electricChars &&1522cm.options.smartIndent && sel.head.ch < 100) {1523var electric = cm.getModeAt(sel.head).electricChars;1524if (electric) for (var i = 0; i < electric.length; i++)1525if (inserted.indexOf(electric.charAt(i)) > -1) {1526indentLine(cm, sel.head.line, "smart");1527break;1528}1529}15301531if (text.length > 1000 || text.indexOf("\n") > -1) input.value = cm.display.prevInput = "";1532else cm.display.prevInput = text;1533if (withOp) endOperation(cm);1534cm.state.pasteIncoming = cm.state.cutIncoming = false;1535return true;1536}15371538function resetInput(cm, user) {1539var minimal, selected, doc = cm.doc;1540if (!posEq(doc.sel.from, doc.sel.to)) {1541cm.display.prevInput = "";1542minimal = hasCopyEvent &&1543(doc.sel.to.line - doc.sel.from.line > 100 || (selected = cm.getSelection()).length > 1000);1544var content = minimal ? "-" : selected || cm.getSelection();1545cm.display.input.value = content;1546if (cm.state.focused) selectInput(cm.display.input);1547if (ie && !ie_lt9) cm.display.inputHasSelection = content;1548} else if (user) {1549cm.display.prevInput = cm.display.input.value = "";1550if (ie && !ie_lt9) cm.display.inputHasSelection = null;1551}1552cm.display.inaccurateSelection = minimal;1553}15541555function focusInput(cm) {1556if (cm.options.readOnly != "nocursor" && (!mobile || document.activeElement != cm.display.input))1557cm.display.input.focus();1558}15591560function isReadOnly(cm) {1561return cm.options.readOnly || cm.doc.cantEdit;1562}15631564// EVENT HANDLERS15651566function registerEventHandlers(cm) {1567var d = cm.display;1568on(d.scroller, "mousedown", operation(cm, onMouseDown));1569if (old_ie)1570on(d.scroller, "dblclick", operation(cm, function(e) {1571if (signalDOMEvent(cm, e)) return;1572var pos = posFromMouse(cm, e);1573if (!pos || clickInGutter(cm, e) || eventInWidget(cm.display, e)) return;1574e_preventDefault(e);1575var word = findWordAt(getLine(cm.doc, pos.line).text, pos);1576extendSelection(cm.doc, word.from, word.to);1577}));1578else1579on(d.scroller, "dblclick", function(e) { signalDOMEvent(cm, e) || e_preventDefault(e); });1580on(d.lineSpace, "selectstart", function(e) {1581if (!eventInWidget(d, e)) e_preventDefault(e);1582});1583// Gecko browsers fire contextmenu *after* opening the menu, at1584// which point we can't mess with it anymore. Context menu is1585// handled in onMouseDown for Gecko.1586if (!captureMiddleClick) on(d.scroller, "contextmenu", function(e) {onContextMenu(cm, e);});15871588on(d.scroller, "scroll", function() {1589if (d.scroller.clientHeight) {1590setScrollTop(cm, d.scroller.scrollTop);1591setScrollLeft(cm, d.scroller.scrollLeft, true);1592signal(cm, "scroll", cm);1593}1594});1595on(d.scrollbarV, "scroll", function() {1596if (d.scroller.clientHeight) setScrollTop(cm, d.scrollbarV.scrollTop);1597});1598on(d.scrollbarH, "scroll", function() {1599if (d.scroller.clientHeight) setScrollLeft(cm, d.scrollbarH.scrollLeft);1600});16011602on(d.scroller, "mousewheel", function(e){onScrollWheel(cm, e);});1603on(d.scroller, "DOMMouseScroll", function(e){onScrollWheel(cm, e);});16041605function reFocus() { if (cm.state.focused) setTimeout(bind(focusInput, cm), 0); }1606on(d.scrollbarH, "mousedown", reFocus);1607on(d.scrollbarV, "mousedown", reFocus);1608// Prevent wrapper from ever scrolling1609on(d.wrapper, "scroll", function() { d.wrapper.scrollTop = d.wrapper.scrollLeft = 0; });16101611var resizeTimer;1612function onResize() {1613if (resizeTimer == null) resizeTimer = setTimeout(function() {1614resizeTimer = null;1615// Might be a text scaling operation, clear size caches.1616d.cachedCharWidth = d.cachedTextHeight = knownScrollbarWidth = null;1617clearCaches(cm);1618runInOp(cm, bind(regChange, cm));1619}, 100);1620}1621on(window, "resize", onResize);1622// Above handler holds on to the editor and its data structures.1623// Here we poll to unregister it when the editor is no longer in1624// the document, so that it can be garbage-collected.1625function unregister() {1626for (var p = d.wrapper.parentNode; p && p != document.body; p = p.parentNode) {}1627if (p) setTimeout(unregister, 5000);1628else off(window, "resize", onResize);1629}1630setTimeout(unregister, 5000);16311632on(d.input, "keyup", operation(cm, function(e) {1633if (signalDOMEvent(cm, e) || cm.options.onKeyEvent && cm.options.onKeyEvent(cm, addStop(e))) return;1634if (e.keyCode == 16) cm.doc.sel.shift = false;1635}));1636on(d.input, "input", function() {1637if (ie && !ie_lt9 && cm.display.inputHasSelection) cm.display.inputHasSelection = null;1638fastPoll(cm);1639});1640on(d.input, "keydown", operation(cm, onKeyDown));1641on(d.input, "keypress", operation(cm, onKeyPress));1642on(d.input, "focus", bind(onFocus, cm));1643on(d.input, "blur", bind(onBlur, cm));16441645function drag_(e) {1646if (signalDOMEvent(cm, e) || cm.options.onDragEvent && cm.options.onDragEvent(cm, addStop(e))) return;1647e_stop(e);1648}1649if (cm.options.dragDrop) {1650on(d.scroller, "dragstart", function(e){onDragStart(cm, e);});1651on(d.scroller, "dragenter", drag_);1652on(d.scroller, "dragover", drag_);1653on(d.scroller, "drop", operation(cm, onDrop));1654}1655on(d.scroller, "paste", function(e) {1656if (eventInWidget(d, e)) return;1657focusInput(cm);1658fastPoll(cm);1659});1660on(d.input, "paste", function() {1661// Workaround for webkit bug https://bugs.webkit.org/show_bug.cgi?id=902061662// Add a char to the end of textarea before paste occur so that1663// selection doesn't span to the end of textarea.1664if (webkit && !cm.state.fakedLastChar && !(new Date - cm.state.lastMiddleDown < 200)) {1665var start = d.input.selectionStart, end = d.input.selectionEnd;1666d.input.value += "$";1667d.input.selectionStart = start;1668d.input.selectionEnd = end;1669cm.state.fakedLastChar = true;1670}1671cm.state.pasteIncoming = true;1672fastPoll(cm);1673});16741675function prepareCopy(e) {1676if (d.inaccurateSelection) {1677d.prevInput = "";1678d.inaccurateSelection = false;1679d.input.value = cm.getSelection();1680selectInput(d.input);1681}1682if (e.type == "cut") cm.state.cutIncoming = true;1683}1684on(d.input, "cut", prepareCopy);1685on(d.input, "copy", prepareCopy);16861687// Needed to handle Tab key in KHTML1688if (khtml) on(d.sizer, "mouseup", function() {1689if (document.activeElement == d.input) d.input.blur();1690focusInput(cm);1691});1692}16931694function eventInWidget(display, e) {1695for (var n = e_target(e); n != display.wrapper; n = n.parentNode) {1696if (!n || n.ignoreEvents || n.parentNode == display.sizer && n != display.mover) return true;1697}1698}16991700function posFromMouse(cm, e, liberal) {1701var display = cm.display;1702if (!liberal) {1703var target = e_target(e);1704if (target == display.scrollbarH || target == display.scrollbarH.firstChild ||1705target == display.scrollbarV || target == display.scrollbarV.firstChild ||1706target == display.scrollbarFiller || target == display.gutterFiller) return null;1707}1708var x, y, space = getRect(display.lineSpace);1709// Fails unpredictably on IE[67] when mouse is dragged around quickly.1710try { x = e.clientX; y = e.clientY; } catch (e) { return null; }1711return coordsChar(cm, x - space.left, y - space.top);1712}17131714var lastClick, lastDoubleClick;1715function onMouseDown(e) {1716if (signalDOMEvent(this, e)) return;1717var cm = this, display = cm.display, doc = cm.doc, sel = doc.sel;1718sel.shift = e.shiftKey;17191720if (eventInWidget(display, e)) {1721if (!webkit) {1722display.scroller.draggable = false;1723setTimeout(function(){display.scroller.draggable = true;}, 100);1724}1725return;1726}1727if (clickInGutter(cm, e)) return;1728var start = posFromMouse(cm, e);17291730switch (e_button(e)) {1731case 3:1732if (captureMiddleClick) onContextMenu.call(cm, cm, e);1733return;1734case 2:1735if (webkit) cm.state.lastMiddleDown = +new Date;1736if (start) extendSelection(cm.doc, start);1737setTimeout(bind(focusInput, cm), 20);1738e_preventDefault(e);1739return;1740}1741// For button 1, if it was clicked inside the editor1742// (posFromMouse returning non-null), we have to adjust the1743// selection.1744if (!start) {if (e_target(e) == display.scroller) e_preventDefault(e); return;}17451746if (!cm.state.focused) onFocus(cm);17471748var now = +new Date, type = "single";1749if (lastDoubleClick && lastDoubleClick.time > now - 400 && posEq(lastDoubleClick.pos, start)) {1750type = "triple";1751e_preventDefault(e);1752setTimeout(bind(focusInput, cm), 20);1753selectLine(cm, start.line);1754} else if (lastClick && lastClick.time > now - 400 && posEq(lastClick.pos, start)) {1755type = "double";1756lastDoubleClick = {time: now, pos: start};1757e_preventDefault(e);1758var word = findWordAt(getLine(doc, start.line).text, start);1759extendSelection(cm.doc, word.from, word.to);1760} else { lastClick = {time: now, pos: start}; }17611762var last = start;1763if (cm.options.dragDrop && dragAndDrop && !isReadOnly(cm) && !posEq(sel.from, sel.to) &&1764!posLess(start, sel.from) && !posLess(sel.to, start) && type == "single") {1765var dragEnd = operation(cm, function(e2) {1766if (webkit) display.scroller.draggable = false;1767cm.state.draggingText = false;1768off(document, "mouseup", dragEnd);1769off(display.scroller, "drop", dragEnd);1770if (Math.abs(e.clientX - e2.clientX) + Math.abs(e.clientY - e2.clientY) < 10) {1771e_preventDefault(e2);1772extendSelection(cm.doc, start);1773focusInput(cm);1774// Work around unexplainable focus problem in IE9 (#2127)1775if (old_ie && !ie_lt9)1776setTimeout(function() {document.body.focus(); focusInput(cm);}, 20);1777}1778});1779// Let the drag handler handle this.1780if (webkit) display.scroller.draggable = true;1781cm.state.draggingText = dragEnd;1782// IE's approach to draggable1783if (display.scroller.dragDrop) display.scroller.dragDrop();1784on(document, "mouseup", dragEnd);1785on(display.scroller, "drop", dragEnd);1786return;1787}1788e_preventDefault(e);1789if (type == "single") extendSelection(cm.doc, clipPos(doc, start));17901791var startstart = sel.from, startend = sel.to, lastPos = start;17921793function doSelect(cur) {1794if (posEq(lastPos, cur)) return;1795lastPos = cur;17961797if (type == "single") {1798extendSelection(cm.doc, clipPos(doc, start), cur);1799return;1800}18011802startstart = clipPos(doc, startstart);1803startend = clipPos(doc, startend);1804if (type == "double") {1805var word = findWordAt(getLine(doc, cur.line).text, cur);1806if (posLess(cur, startstart)) extendSelection(cm.doc, word.from, startend);1807else extendSelection(cm.doc, startstart, word.to);1808} else if (type == "triple") {1809if (posLess(cur, startstart)) extendSelection(cm.doc, startend, clipPos(doc, Pos(cur.line, 0)));1810else extendSelection(cm.doc, startstart, clipPos(doc, Pos(cur.line + 1, 0)));1811}1812}18131814var editorSize = getRect(display.wrapper);1815// Used to ensure timeout re-tries don't fire when another extend1816// happened in the meantime (clearTimeout isn't reliable -- at1817// least on Chrome, the timeouts still happen even when cleared,1818// if the clear happens after their scheduled firing time).1819var counter = 0;18201821function extend(e) {1822var curCount = ++counter;1823var cur = posFromMouse(cm, e, true);1824if (!cur) return;1825if (!posEq(cur, last)) {1826if (!cm.state.focused) onFocus(cm);1827last = cur;1828doSelect(cur);1829var visible = visibleLines(display, doc);1830if (cur.line >= visible.to || cur.line < visible.from)1831setTimeout(operation(cm, function(){if (counter == curCount) extend(e);}), 150);1832} else {1833var outside = e.clientY < editorSize.top ? -20 : e.clientY > editorSize.bottom ? 20 : 0;1834if (outside) setTimeout(operation(cm, function() {1835if (counter != curCount) return;1836display.scroller.scrollTop += outside;1837extend(e);1838}), 50);1839}1840}18411842function done(e) {1843counter = Infinity;1844e_preventDefault(e);1845focusInput(cm);1846off(document, "mousemove", move);1847off(document, "mouseup", up);1848}18491850var move = operation(cm, function(e) {1851if (!old_ie && !e_button(e)) done(e);1852else extend(e);1853});1854var up = operation(cm, done);1855on(document, "mousemove", move);1856on(document, "mouseup", up);1857}18581859function gutterEvent(cm, e, type, prevent, signalfn) {1860try { var mX = e.clientX, mY = e.clientY; }1861catch(e) { return false; }1862if (mX >= Math.floor(getRect(cm.display.gutters).right)) return false;1863if (prevent) e_preventDefault(e);18641865var display = cm.display;1866var lineBox = getRect(display.lineDiv);18671868if (mY > lineBox.bottom || !hasHandler(cm, type)) return e_defaultPrevented(e);1869mY -= lineBox.top - display.viewOffset;18701871for (var i = 0; i < cm.options.gutters.length; ++i) {1872var g = display.gutters.childNodes[i];1873if (g && getRect(g).right >= mX) {1874var line = lineAtHeight(cm.doc, mY);1875var gutter = cm.options.gutters[i];1876signalfn(cm, type, cm, line, gutter, e);1877return e_defaultPrevented(e);1878}1879}1880}18811882function contextMenuInGutter(cm, e) {1883if (!hasHandler(cm, "gutterContextMenu")) return false;1884return gutterEvent(cm, e, "gutterContextMenu", false, signal);1885}18861887function clickInGutter(cm, e) {1888return gutterEvent(cm, e, "gutterClick", true, signalLater);1889}18901891// Kludge to work around strange IE behavior where it'll sometimes1892// re-fire a series of drag-related events right after the drop (#1551)1893var lastDrop = 0;18941895function onDrop(e) {1896var cm = this;1897if (signalDOMEvent(cm, e) || eventInWidget(cm.display, e) || (cm.options.onDragEvent && cm.options.onDragEvent(cm, addStop(e))))1898return;1899e_preventDefault(e);1900if (ie) lastDrop = +new Date;1901var pos = posFromMouse(cm, e, true), files = e.dataTransfer.files;1902if (!pos || isReadOnly(cm)) return;1903if (files && files.length && window.FileReader && window.File) {1904var n = files.length, text = Array(n), read = 0;1905var loadFile = function(file, i) {1906var reader = new FileReader;1907reader.onload = function() {1908text[i] = reader.result;1909if (++read == n) {1910pos = clipPos(cm.doc, pos);1911makeChange(cm.doc, {from: pos, to: pos, text: splitLines(text.join("\n")), origin: "paste"}, "around");1912}1913};1914reader.readAsText(file);1915};1916for (var i = 0; i < n; ++i) loadFile(files[i], i);1917} else {1918// Don't do a replace if the drop happened inside of the selected text.1919if (cm.state.draggingText && !(posLess(pos, cm.doc.sel.from) || posLess(cm.doc.sel.to, pos))) {1920cm.state.draggingText(e);1921// Ensure the editor is re-focused1922setTimeout(bind(focusInput, cm), 20);1923return;1924}1925try {1926var text = e.dataTransfer.getData("Text");1927if (text) {1928var curFrom = cm.doc.sel.from, curTo = cm.doc.sel.to;1929setSelection(cm.doc, pos, pos);1930if (cm.state.draggingText) replaceRange(cm.doc, "", curFrom, curTo, "paste");1931cm.replaceSelection(text, null, "paste");1932focusInput(cm);1933}1934}1935catch(e){}1936}1937}19381939function onDragStart(cm, e) {1940if (ie && (!cm.state.draggingText || +new Date - lastDrop < 100)) { e_stop(e); return; }1941if (signalDOMEvent(cm, e) || eventInWidget(cm.display, e)) return;19421943var txt = cm.getSelection();1944e.dataTransfer.setData("Text", txt);19451946// Use dummy image instead of default browsers image.1947// Recent Safari (~6.0.2) have a tendency to segfault when this happens, so we don't do it there.1948if (e.dataTransfer.setDragImage && !safari) {1949var img = elt("img", null, null, "position: fixed; left: 0; top: 0;");1950img.src = "data:image/gif;base64,R0lGODlhAQABAAAAACH5BAEKAAEALAAAAAABAAEAAAICTAEAOw==";1951if (opera) {1952img.width = img.height = 1;1953cm.display.wrapper.appendChild(img);1954// Force a relayout, or Opera won't use our image for some obscure reason1955img._top = img.offsetTop;1956}1957e.dataTransfer.setDragImage(img, 0, 0);1958if (opera) img.parentNode.removeChild(img);1959}1960}19611962function setScrollTop(cm, val) {1963if (Math.abs(cm.doc.scrollTop - val) < 2) return;1964cm.doc.scrollTop = val;1965if (!gecko) updateDisplay(cm, [], val);1966if (cm.display.scroller.scrollTop != val) cm.display.scroller.scrollTop = val;1967if (cm.display.scrollbarV.scrollTop != val) cm.display.scrollbarV.scrollTop = val;1968if (gecko) updateDisplay(cm, []);1969startWorker(cm, 100);1970}1971function setScrollLeft(cm, val, isScroller) {1972if (isScroller ? val == cm.doc.scrollLeft : Math.abs(cm.doc.scrollLeft - val) < 2) return;1973val = Math.min(val, cm.display.scroller.scrollWidth - cm.display.scroller.clientWidth);1974cm.doc.scrollLeft = val;1975alignHorizontally(cm);1976if (cm.display.scroller.scrollLeft != val) cm.display.scroller.scrollLeft = val;1977if (cm.display.scrollbarH.scrollLeft != val) cm.display.scrollbarH.scrollLeft = val;1978}19791980// Since the delta values reported on mouse wheel events are1981// unstandardized between browsers and even browser versions, and1982// generally horribly unpredictable, this code starts by measuring1983// the scroll effect that the first few mouse wheel events have,1984// and, from that, detects the way it can convert deltas to pixel1985// offsets afterwards.1986//1987// The reason we want to know the amount a wheel event will scroll1988// is that it gives us a chance to update the display before the1989// actual scrolling happens, reducing flickering.19901991var wheelSamples = 0, wheelPixelsPerUnit = null;1992// Fill in a browser-detected starting value on browsers where we1993// know one. These don't have to be accurate -- the result of them1994// being wrong would just be a slight flicker on the first wheel1995// scroll (if it is large enough).1996if (old_ie) wheelPixelsPerUnit = -.53;1997else if (gecko) wheelPixelsPerUnit = 15;1998else if (chrome) wheelPixelsPerUnit = -.7;1999else if (safari) wheelPixelsPerUnit = -1/3;20002001function onScrollWheel(cm, e) {2002var dx = e.wheelDeltaX, dy = e.wheelDeltaY;2003if (dx == null && e.detail && e.axis == e.HORIZONTAL_AXIS) dx = e.detail;2004if (dy == null && e.detail && e.axis == e.VERTICAL_AXIS) dy = e.detail;2005else if (dy == null) dy = e.wheelDelta;20062007var display = cm.display, scroll = display.scroller;2008// Quit if there's nothing to scroll here2009if (!(dx && scroll.scrollWidth > scroll.clientWidth ||2010dy && scroll.scrollHeight > scroll.clientHeight)) return;20112012// Webkit browsers on OS X abort momentum scrolls when the target2013// of the scroll event is removed from the scrollable element.2014// This hack (see related code in patchDisplay) makes sure the2015// element is kept around.2016if (dy && mac && webkit) {2017for (var cur = e.target; cur != scroll; cur = cur.parentNode) {2018if (cur.lineObj) {2019cm.display.currentWheelTarget = cur;2020break;2021}2022}2023}20242025// On some browsers, horizontal scrolling will cause redraws to2026// happen before the gutter has been realigned, causing it to2027// wriggle around in a most unseemly way. When we have an2028// estimated pixels/delta value, we just handle horizontal2029// scrolling entirely here. It'll be slightly off from native, but2030// better than glitching out.2031if (dx && !gecko && !opera && wheelPixelsPerUnit != null) {2032if (dy)2033setScrollTop(cm, Math.max(0, Math.min(scroll.scrollTop + dy * wheelPixelsPerUnit, scroll.scrollHeight - scroll.clientHeight)));2034setScrollLeft(cm, Math.max(0, Math.min(scroll.scrollLeft + dx * wheelPixelsPerUnit, scroll.scrollWidth - scroll.clientWidth)));2035e_preventDefault(e);2036display.wheelStartX = null; // Abort measurement, if in progress2037return;2038}20392040if (dy && wheelPixelsPerUnit != null) {2041var pixels = dy * wheelPixelsPerUnit;2042var top = cm.doc.scrollTop, bot = top + display.wrapper.clientHeight;2043if (pixels < 0) top = Math.max(0, top + pixels - 50);2044else bot = Math.min(cm.doc.height, bot + pixels + 50);2045updateDisplay(cm, [], {top: top, bottom: bot});2046}20472048if (wheelSamples < 20) {2049if (display.wheelStartX == null) {2050display.wheelStartX = scroll.scrollLeft; display.wheelStartY = scroll.scrollTop;2051display.wheelDX = dx; display.wheelDY = dy;2052setTimeout(function() {2053if (display.wheelStartX == null) return;2054var movedX = scroll.scrollLeft - display.wheelStartX;2055var movedY = scroll.scrollTop - display.wheelStartY;2056var sample = (movedY && display.wheelDY && movedY / display.wheelDY) ||2057(movedX && display.wheelDX && movedX / display.wheelDX);2058display.wheelStartX = display.wheelStartY = null;2059if (!sample) return;2060wheelPixelsPerUnit = (wheelPixelsPerUnit * wheelSamples + sample) / (wheelSamples + 1);2061++wheelSamples;2062}, 200);2063} else {2064display.wheelDX += dx; display.wheelDY += dy;2065}2066}2067}20682069function doHandleBinding(cm, bound, dropShift) {2070if (typeof bound == "string") {2071bound = commands[bound];2072if (!bound) return false;2073}2074// Ensure previous input has been read, so that the handler sees a2075// consistent view of the document2076if (cm.display.pollingFast && readInput(cm)) cm.display.pollingFast = false;2077var doc = cm.doc, prevShift = doc.sel.shift, done = false;2078try {2079if (isReadOnly(cm)) cm.state.suppressEdits = true;2080if (dropShift) doc.sel.shift = false;2081done = bound(cm) != Pass;2082} finally {2083doc.sel.shift = prevShift;2084cm.state.suppressEdits = false;2085}2086return done;2087}20882089function allKeyMaps(cm) {2090var maps = cm.state.keyMaps.slice(0);2091if (cm.options.extraKeys) maps.push(cm.options.extraKeys);2092maps.push(cm.options.keyMap);2093return maps;2094}20952096var maybeTransition;2097function handleKeyBinding(cm, e) {2098// Handle auto keymap transitions2099var startMap = getKeyMap(cm.options.keyMap), next = startMap.auto;2100clearTimeout(maybeTransition);2101if (next && !isModifierKey(e)) maybeTransition = setTimeout(function() {2102if (getKeyMap(cm.options.keyMap) == startMap) {2103cm.options.keyMap = (next.call ? next.call(null, cm) : next);2104keyMapChanged(cm);2105}2106}, 50);21072108var name = keyName(e, true), handled = false;2109if (!name) return false;2110var keymaps = allKeyMaps(cm);21112112if (e.shiftKey) {2113// First try to resolve full name (including 'Shift-'). Failing2114// that, see if there is a cursor-motion command (starting with2115// 'go') bound to the keyname without 'Shift-'.2116handled = lookupKey("Shift-" + name, keymaps, function(b) {return doHandleBinding(cm, b, true);})2117|| lookupKey(name, keymaps, function(b) {2118if (typeof b == "string" ? /^go[A-Z]/.test(b) : b.motion)2119return doHandleBinding(cm, b);2120});2121} else {2122handled = lookupKey(name, keymaps, function(b) { return doHandleBinding(cm, b); });2123}21242125if (handled) {2126e_preventDefault(e);2127restartBlink(cm);2128if (ie_lt9) { e.oldKeyCode = e.keyCode; e.keyCode = 0; }2129signalLater(cm, "keyHandled", cm, name, e);2130}2131return handled;2132}21332134function handleCharBinding(cm, e, ch) {2135var handled = lookupKey("'" + ch + "'", allKeyMaps(cm),2136function(b) { return doHandleBinding(cm, b, true); });2137if (handled) {2138e_preventDefault(e);2139restartBlink(cm);2140signalLater(cm, "keyHandled", cm, "'" + ch + "'", e);2141}2142return handled;2143}21442145var lastStoppedKey = null;2146function onKeyDown(e) {2147var cm = this;2148if (!cm.state.focused) onFocus(cm);2149if (signalDOMEvent(cm, e) || cm.options.onKeyEvent && cm.options.onKeyEvent(cm, addStop(e))) return;2150if (old_ie && e.keyCode == 27) e.returnValue = false;2151var code = e.keyCode;2152// IE does strange things with escape.2153cm.doc.sel.shift = code == 16 || e.shiftKey;2154// First give onKeyEvent option a chance to handle this.2155var handled = handleKeyBinding(cm, e);2156if (opera) {2157lastStoppedKey = handled ? code : null;2158// Opera has no cut event... we try to at least catch the key combo2159if (!handled && code == 88 && !hasCopyEvent && (mac ? e.metaKey : e.ctrlKey))2160cm.replaceSelection("");2161}2162}21632164function onKeyPress(e) {2165var cm = this;2166if (signalDOMEvent(cm, e) || cm.options.onKeyEvent && cm.options.onKeyEvent(cm, addStop(e))) return;2167var keyCode = e.keyCode, charCode = e.charCode;2168if (opera && keyCode == lastStoppedKey) {lastStoppedKey = null; e_preventDefault(e); return;}2169if (((opera && (!e.which || e.which < 10)) || khtml) && handleKeyBinding(cm, e)) return;2170var ch = String.fromCharCode(charCode == null ? keyCode : charCode);2171if (handleCharBinding(cm, e, ch)) return;2172if (ie && !ie_lt9) cm.display.inputHasSelection = null;2173fastPoll(cm);2174}21752176function onFocus(cm) {2177if (cm.options.readOnly == "nocursor") return;2178if (!cm.state.focused) {2179signal(cm, "focus", cm);2180cm.state.focused = true;2181if (cm.display.wrapper.className.search(/\bCodeMirror-focused\b/) == -1)2182cm.display.wrapper.className += " CodeMirror-focused";2183if (!cm.curOp) {2184resetInput(cm, true);2185if (webkit) setTimeout(bind(resetInput, cm, true), 0); // Issue #17302186}2187}2188slowPoll(cm);2189restartBlink(cm);2190}2191function onBlur(cm) {2192if (cm.state.focused) {2193signal(cm, "blur", cm);2194cm.state.focused = false;2195cm.display.wrapper.className = cm.display.wrapper.className.replace(" CodeMirror-focused", "");2196}2197clearInterval(cm.display.blinker);2198setTimeout(function() {if (!cm.state.focused) cm.doc.sel.shift = false;}, 150);2199}22002201var detectingSelectAll;2202function onContextMenu(cm, e) {2203if (signalDOMEvent(cm, e, "contextmenu")) return;2204var display = cm.display, sel = cm.doc.sel;2205if (eventInWidget(display, e) || contextMenuInGutter(cm, e)) return;22062207var pos = posFromMouse(cm, e), scrollPos = display.scroller.scrollTop;2208if (!pos || opera) return; // Opera is difficult.22092210// Reset the current text selection only if the click is done outside of the selection2211// and 'resetSelectionOnContextMenu' option is true.2212var reset = cm.options.resetSelectionOnContextMenu;2213if (reset && (posEq(sel.from, sel.to) || posLess(pos, sel.from) || !posLess(pos, sel.to)))2214operation(cm, setSelection)(cm.doc, pos, pos);22152216var oldCSS = display.input.style.cssText;2217display.inputDiv.style.position = "absolute";2218display.input.style.cssText = "position: fixed; width: 30px; height: 30px; top: " + (e.clientY - 5) +2219"px; left: " + (e.clientX - 5) + "px; z-index: 1000; background: transparent; outline: none;" +2220"border-width: 0; outline: none; overflow: hidden; opacity: .05; -ms-opacity: .05; filter: alpha(opacity=5);";2221focusInput(cm);2222resetInput(cm, true);2223// Adds "Select all" to context menu in FF2224if (posEq(sel.from, sel.to)) display.input.value = display.prevInput = " ";22252226function prepareSelectAllHack() {2227if (display.input.selectionStart != null) {2228var extval = display.input.value = "\u200b" + (posEq(sel.from, sel.to) ? "" : display.input.value);2229display.prevInput = "\u200b";2230display.input.selectionStart = 1; display.input.selectionEnd = extval.length;2231}2232}2233function rehide() {2234display.inputDiv.style.position = "relative";2235display.input.style.cssText = oldCSS;2236if (ie_lt9) display.scrollbarV.scrollTop = display.scroller.scrollTop = scrollPos;2237slowPoll(cm);22382239// Try to detect the user choosing select-all2240if (display.input.selectionStart != null) {2241if (!old_ie || ie_lt9) prepareSelectAllHack();2242clearTimeout(detectingSelectAll);2243var i = 0, poll = function(){2244if (display.prevInput == "\u200b" && display.input.selectionStart == 0)2245operation(cm, commands.selectAll)(cm);2246else if (i++ < 10) detectingSelectAll = setTimeout(poll, 500);2247else resetInput(cm);2248};2249detectingSelectAll = setTimeout(poll, 200);2250}2251}22522253if (old_ie && !ie_lt9) prepareSelectAllHack();2254if (captureMiddleClick) {2255e_stop(e);2256var mouseup = function() {2257off(window, "mouseup", mouseup);2258setTimeout(rehide, 20);2259};2260on(window, "mouseup", mouseup);2261} else {2262setTimeout(rehide, 50);2263}2264}22652266// UPDATING22672268var changeEnd = CodeMirror.changeEnd = function(change) {2269if (!change.text) return change.to;2270return Pos(change.from.line + change.text.length - 1,2271lst(change.text).length + (change.text.length == 1 ? change.from.ch : 0));2272};22732274// Make sure a position will be valid after the given change.2275function clipPostChange(doc, change, pos) {2276if (!posLess(change.from, pos)) return clipPos(doc, pos);2277var diff = (change.text.length - 1) - (change.to.line - change.from.line);2278if (pos.line > change.to.line + diff) {2279var preLine = pos.line - diff, lastLine = doc.first + doc.size - 1;2280if (preLine > lastLine) return Pos(lastLine, getLine(doc, lastLine).text.length);2281return clipToLen(pos, getLine(doc, preLine).text.length);2282}2283if (pos.line == change.to.line + diff)2284return clipToLen(pos, lst(change.text).length + (change.text.length == 1 ? change.from.ch : 0) +2285getLine(doc, change.to.line).text.length - change.to.ch);2286var inside = pos.line - change.from.line;2287return clipToLen(pos, change.text[inside].length + (inside ? 0 : change.from.ch));2288}22892290// Hint can be null|"end"|"start"|"around"|{anchor,head}2291function computeSelAfterChange(doc, change, hint) {2292if (hint && typeof hint == "object") // Assumed to be {anchor, head} object2293return {anchor: clipPostChange(doc, change, hint.anchor),2294head: clipPostChange(doc, change, hint.head)};22952296if (hint == "start") return {anchor: change.from, head: change.from};22972298var end = changeEnd(change);2299if (hint == "around") return {anchor: change.from, head: end};2300if (hint == "end") return {anchor: end, head: end};23012302// hint is null, leave the selection alone as much as possible2303var adjustPos = function(pos) {2304if (posLess(pos, change.from)) return pos;2305if (!posLess(change.to, pos)) return end;23062307var line = pos.line + change.text.length - (change.to.line - change.from.line) - 1, ch = pos.ch;2308if (pos.line == change.to.line) ch += end.ch - change.to.ch;2309return Pos(line, ch);2310};2311return {anchor: adjustPos(doc.sel.anchor), head: adjustPos(doc.sel.head)};2312}23132314function filterChange(doc, change, update) {2315var obj = {2316canceled: false,2317from: change.from,2318to: change.to,2319text: change.text,2320origin: change.origin,2321cancel: function() { this.canceled = true; }2322};2323if (update) obj.update = function(from, to, text, origin) {2324if (from) this.from = clipPos(doc, from);2325if (to) this.to = clipPos(doc, to);2326if (text) this.text = text;2327if (origin !== undefined) this.origin = origin;2328};2329signal(doc, "beforeChange", doc, obj);2330if (doc.cm) signal(doc.cm, "beforeChange", doc.cm, obj);23312332if (obj.canceled) return null;2333return {from: obj.from, to: obj.to, text: obj.text, origin: obj.origin};2334}23352336// Replace the range from from to to by the strings in replacement.2337// change is a {from, to, text [, origin]} object2338function makeChange(doc, change, selUpdate, ignoreReadOnly) {2339if (doc.cm) {2340if (!doc.cm.curOp) return operation(doc.cm, makeChange)(doc, change, selUpdate, ignoreReadOnly);2341if (doc.cm.state.suppressEdits) return;2342}23432344if (hasHandler(doc, "beforeChange") || doc.cm && hasHandler(doc.cm, "beforeChange")) {2345change = filterChange(doc, change, true);2346if (!change) return;2347}23482349// Possibly split or suppress the update based on the presence2350// of read-only spans in its range.2351var split = sawReadOnlySpans && !ignoreReadOnly && removeReadOnlyRanges(doc, change.from, change.to);2352if (split) {2353for (var i = split.length - 1; i >= 1; --i)2354makeChangeNoReadonly(doc, {from: split[i].from, to: split[i].to, text: [""]});2355if (split.length)2356makeChangeNoReadonly(doc, {from: split[0].from, to: split[0].to, text: change.text}, selUpdate);2357} else {2358makeChangeNoReadonly(doc, change, selUpdate);2359}2360}23612362function makeChangeNoReadonly(doc, change, selUpdate) {2363if (change.text.length == 1 && change.text[0] == "" && posEq(change.from, change.to)) return;2364var selAfter = computeSelAfterChange(doc, change, selUpdate);2365addToHistory(doc, change, selAfter, doc.cm ? doc.cm.curOp.id : NaN);23662367makeChangeSingleDoc(doc, change, selAfter, stretchSpansOverChange(doc, change));2368var rebased = [];23692370linkedDocs(doc, function(doc, sharedHist) {2371if (!sharedHist && indexOf(rebased, doc.history) == -1) {2372rebaseHist(doc.history, change);2373rebased.push(doc.history);2374}2375makeChangeSingleDoc(doc, change, null, stretchSpansOverChange(doc, change));2376});2377}23782379function makeChangeFromHistory(doc, type) {2380if (doc.cm && doc.cm.state.suppressEdits) return;23812382var hist = doc.history;2383var event = (type == "undo" ? hist.done : hist.undone).pop();2384if (!event) return;23852386var anti = {changes: [], anchorBefore: event.anchorAfter, headBefore: event.headAfter,2387anchorAfter: event.anchorBefore, headAfter: event.headBefore,2388generation: hist.generation};2389(type == "undo" ? hist.undone : hist.done).push(anti);2390hist.generation = event.generation || ++hist.maxGeneration;23912392var filter = hasHandler(doc, "beforeChange") || doc.cm && hasHandler(doc.cm, "beforeChange");23932394for (var i = event.changes.length - 1; i >= 0; --i) {2395var change = event.changes[i];2396change.origin = type;2397if (filter && !filterChange(doc, change, false)) {2398(type == "undo" ? hist.done : hist.undone).length = 0;2399return;2400}24012402anti.changes.push(historyChangeFromChange(doc, change));24032404var after = i ? computeSelAfterChange(doc, change, null)2405: {anchor: event.anchorBefore, head: event.headBefore};2406makeChangeSingleDoc(doc, change, after, mergeOldSpans(doc, change));2407var rebased = [];24082409linkedDocs(doc, function(doc, sharedHist) {2410if (!sharedHist && indexOf(rebased, doc.history) == -1) {2411rebaseHist(doc.history, change);2412rebased.push(doc.history);2413}2414makeChangeSingleDoc(doc, change, null, mergeOldSpans(doc, change));2415});2416}2417}24182419function shiftDoc(doc, distance) {2420function shiftPos(pos) {return Pos(pos.line + distance, pos.ch);}2421doc.first += distance;2422if (doc.cm) regChange(doc.cm, doc.first, doc.first, distance);2423doc.sel.head = shiftPos(doc.sel.head); doc.sel.anchor = shiftPos(doc.sel.anchor);2424doc.sel.from = shiftPos(doc.sel.from); doc.sel.to = shiftPos(doc.sel.to);2425}24262427function makeChangeSingleDoc(doc, change, selAfter, spans) {2428if (doc.cm && !doc.cm.curOp)2429return operation(doc.cm, makeChangeSingleDoc)(doc, change, selAfter, spans);24302431if (change.to.line < doc.first) {2432shiftDoc(doc, change.text.length - 1 - (change.to.line - change.from.line));2433return;2434}2435if (change.from.line > doc.lastLine()) return;24362437// Clip the change to the size of this doc2438if (change.from.line < doc.first) {2439var shift = change.text.length - 1 - (doc.first - change.from.line);2440shiftDoc(doc, shift);2441change = {from: Pos(doc.first, 0), to: Pos(change.to.line + shift, change.to.ch),2442text: [lst(change.text)], origin: change.origin};2443}2444var last = doc.lastLine();2445if (change.to.line > last) {2446change = {from: change.from, to: Pos(last, getLine(doc, last).text.length),2447text: [change.text[0]], origin: change.origin};2448}24492450change.removed = getBetween(doc, change.from, change.to);24512452if (!selAfter) selAfter = computeSelAfterChange(doc, change, null);2453if (doc.cm) makeChangeSingleDocInEditor(doc.cm, change, spans, selAfter);2454else updateDoc(doc, change, spans, selAfter);2455}24562457function makeChangeSingleDocInEditor(cm, change, spans, selAfter) {2458var doc = cm.doc, display = cm.display, from = change.from, to = change.to;24592460var recomputeMaxLength = false, checkWidthStart = from.line;2461if (!cm.options.lineWrapping) {2462checkWidthStart = lineNo(visualLine(doc, getLine(doc, from.line)));2463doc.iter(checkWidthStart, to.line + 1, function(line) {2464if (line == display.maxLine) {2465recomputeMaxLength = true;2466return true;2467}2468});2469}24702471if (!posLess(doc.sel.head, change.from) && !posLess(change.to, doc.sel.head))2472cm.curOp.cursorActivity = true;24732474updateDoc(doc, change, spans, selAfter, estimateHeight(cm));24752476if (!cm.options.lineWrapping) {2477doc.iter(checkWidthStart, from.line + change.text.length, function(line) {2478var len = lineLength(doc, line);2479if (len > display.maxLineLength) {2480display.maxLine = line;2481display.maxLineLength = len;2482display.maxLineChanged = true;2483recomputeMaxLength = false;2484}2485});2486if (recomputeMaxLength) cm.curOp.updateMaxLine = true;2487}24882489// Adjust frontier, schedule worker2490doc.frontier = Math.min(doc.frontier, from.line);2491startWorker(cm, 400);24922493var lendiff = change.text.length - (to.line - from.line) - 1;2494// Remember that these lines changed, for updating the display2495regChange(cm, from.line, to.line + 1, lendiff);24962497if (hasHandler(cm, "change")) {2498var changeObj = {from: from, to: to,2499text: change.text,2500removed: change.removed,2501origin: change.origin};2502if (cm.curOp.textChanged) {2503for (var cur = cm.curOp.textChanged; cur.next; cur = cur.next) {}2504cur.next = changeObj;2505} else cm.curOp.textChanged = changeObj;2506}2507}25082509function replaceRange(doc, code, from, to, origin) {2510if (!to) to = from;2511if (posLess(to, from)) { var tmp = to; to = from; from = tmp; }2512if (typeof code == "string") code = splitLines(code);2513makeChange(doc, {from: from, to: to, text: code, origin: origin}, null);2514}25152516// POSITION OBJECT25172518function Pos(line, ch) {2519if (!(this instanceof Pos)) return new Pos(line, ch);2520this.line = line; this.ch = ch;2521}2522CodeMirror.Pos = Pos;25232524function posEq(a, b) {return a.line == b.line && a.ch == b.ch;}2525function posLess(a, b) {return a.line < b.line || (a.line == b.line && a.ch < b.ch);}2526function cmp(a, b) {return a.line - b.line || a.ch - b.ch;}2527function copyPos(x) {return Pos(x.line, x.ch);}25282529// SELECTION25302531function clipLine(doc, n) {return Math.max(doc.first, Math.min(n, doc.first + doc.size - 1));}2532function clipPos(doc, pos) {2533if (pos.line < doc.first) return Pos(doc.first, 0);2534var last = doc.first + doc.size - 1;2535if (pos.line > last) return Pos(last, getLine(doc, last).text.length);2536return clipToLen(pos, getLine(doc, pos.line).text.length);2537}2538function clipToLen(pos, linelen) {2539var ch = pos.ch;2540if (ch == null || ch > linelen) return Pos(pos.line, linelen);2541else if (ch < 0) return Pos(pos.line, 0);2542else return pos;2543}2544function isLine(doc, l) {return l >= doc.first && l < doc.first + doc.size;}25452546// If shift is held, this will move the selection anchor. Otherwise,2547// it'll set the whole selection.2548function extendSelection(doc, pos, other, bias) {2549if (doc.sel.shift || doc.sel.extend) {2550var anchor = doc.sel.anchor;2551if (other) {2552var posBefore = posLess(pos, anchor);2553if (posBefore != posLess(other, anchor)) {2554anchor = pos;2555pos = other;2556} else if (posBefore != posLess(pos, other)) {2557pos = other;2558}2559}2560setSelection(doc, anchor, pos, bias);2561} else {2562setSelection(doc, pos, other || pos, bias);2563}2564if (doc.cm) doc.cm.curOp.userSelChange = true;2565}25662567function filterSelectionChange(doc, anchor, head) {2568var obj = {anchor: anchor, head: head};2569signal(doc, "beforeSelectionChange", doc, obj);2570if (doc.cm) signal(doc.cm, "beforeSelectionChange", doc.cm, obj);2571obj.anchor = clipPos(doc, obj.anchor); obj.head = clipPos(doc, obj.head);2572return obj;2573}25742575// Update the selection. Last two args are only used by2576// updateDoc, since they have to be expressed in the line2577// numbers before the update.2578function setSelection(doc, anchor, head, bias, checkAtomic) {2579if (!checkAtomic && hasHandler(doc, "beforeSelectionChange") || doc.cm && hasHandler(doc.cm, "beforeSelectionChange")) {2580var filtered = filterSelectionChange(doc, anchor, head);2581head = filtered.head;2582anchor = filtered.anchor;2583}25842585var sel = doc.sel;2586sel.goalColumn = null;2587if (bias == null) bias = posLess(head, sel.head) ? -1 : 1;2588// Skip over atomic spans.2589if (checkAtomic || !posEq(anchor, sel.anchor))2590anchor = skipAtomic(doc, anchor, bias, checkAtomic != "push");2591if (checkAtomic || !posEq(head, sel.head))2592head = skipAtomic(doc, head, bias, checkAtomic != "push");25932594if (posEq(sel.anchor, anchor) && posEq(sel.head, head)) return;25952596sel.anchor = anchor; sel.head = head;2597var inv = posLess(head, anchor);2598sel.from = inv ? head : anchor;2599sel.to = inv ? anchor : head;26002601if (doc.cm)2602doc.cm.curOp.updateInput = doc.cm.curOp.selectionChanged =2603doc.cm.curOp.cursorActivity = true;26042605signalLater(doc, "cursorActivity", doc);2606}26072608function reCheckSelection(cm) {2609setSelection(cm.doc, cm.doc.sel.from, cm.doc.sel.to, null, "push");2610}26112612function skipAtomic(doc, pos, bias, mayClear) {2613var flipped = false, curPos = pos;2614var dir = bias || 1;2615doc.cantEdit = false;2616search: for (;;) {2617var line = getLine(doc, curPos.line);2618if (line.markedSpans) {2619for (var i = 0; i < line.markedSpans.length; ++i) {2620var sp = line.markedSpans[i], m = sp.marker;2621if ((sp.from == null || (m.inclusiveLeft ? sp.from <= curPos.ch : sp.from < curPos.ch)) &&2622(sp.to == null || (m.inclusiveRight ? sp.to >= curPos.ch : sp.to > curPos.ch))) {2623if (mayClear) {2624signal(m, "beforeCursorEnter");2625if (m.explicitlyCleared) {2626if (!line.markedSpans) break;2627else {--i; continue;}2628}2629}2630if (!m.atomic) continue;2631var newPos = m.find()[dir < 0 ? "from" : "to"];2632if (posEq(newPos, curPos)) {2633newPos.ch += dir;2634if (newPos.ch < 0) {2635if (newPos.line > doc.first) newPos = clipPos(doc, Pos(newPos.line - 1));2636else newPos = null;2637} else if (newPos.ch > line.text.length) {2638if (newPos.line < doc.first + doc.size - 1) newPos = Pos(newPos.line + 1, 0);2639else newPos = null;2640}2641if (!newPos) {2642if (flipped) {2643// Driven in a corner -- no valid cursor position found at all2644// -- try again *with* clearing, if we didn't already2645if (!mayClear) return skipAtomic(doc, pos, bias, true);2646// Otherwise, turn off editing until further notice, and return the start of the doc2647doc.cantEdit = true;2648return Pos(doc.first, 0);2649}2650flipped = true; newPos = pos; dir = -dir;2651}2652}2653curPos = newPos;2654continue search;2655}2656}2657}2658return curPos;2659}2660}26612662// SCROLLING26632664function scrollCursorIntoView(cm) {2665var coords = scrollPosIntoView(cm, cm.doc.sel.head, null, cm.options.cursorScrollMargin);2666if (!cm.state.focused) return;2667var display = cm.display, box = getRect(display.sizer), doScroll = null;2668if (coords.top + box.top < 0) doScroll = true;2669else if (coords.bottom + box.top > (window.innerHeight || document.documentElement.clientHeight)) doScroll = false;2670if (doScroll != null && !phantom) {2671var scrollNode = elt("div", "\u200b", null, "position: absolute; top: " +2672(coords.top - display.viewOffset) + "px; height: " +2673(coords.bottom - coords.top + scrollerCutOff) + "px; left: " +2674coords.left + "px; width: 2px;");2675cm.display.lineSpace.appendChild(scrollNode);2676scrollNode.scrollIntoView(doScroll);2677cm.display.lineSpace.removeChild(scrollNode);2678}2679}26802681function scrollPosIntoView(cm, pos, end, margin) {2682if (margin == null) margin = 0;2683for (;;) {2684var changed = false, coords = cursorCoords(cm, pos);2685var endCoords = !end || end == pos ? coords : cursorCoords(cm, end);2686var scrollPos = calculateScrollPos(cm, Math.min(coords.left, endCoords.left),2687Math.min(coords.top, endCoords.top) - margin,2688Math.max(coords.left, endCoords.left),2689Math.max(coords.bottom, endCoords.bottom) + margin);2690var startTop = cm.doc.scrollTop, startLeft = cm.doc.scrollLeft;2691if (scrollPos.scrollTop != null) {2692setScrollTop(cm, scrollPos.scrollTop);2693if (Math.abs(cm.doc.scrollTop - startTop) > 1) changed = true;2694}2695if (scrollPos.scrollLeft != null) {2696setScrollLeft(cm, scrollPos.scrollLeft);2697if (Math.abs(cm.doc.scrollLeft - startLeft) > 1) changed = true;2698}2699if (!changed) return coords;2700}2701}27022703function scrollIntoView(cm, x1, y1, x2, y2) {2704var scrollPos = calculateScrollPos(cm, x1, y1, x2, y2);2705if (scrollPos.scrollTop != null) setScrollTop(cm, scrollPos.scrollTop);2706if (scrollPos.scrollLeft != null) setScrollLeft(cm, scrollPos.scrollLeft);2707}27082709function calculateScrollPos(cm, x1, y1, x2, y2) {2710var display = cm.display, snapMargin = textHeight(cm.display);2711if (y1 < 0) y1 = 0;2712var screen = display.scroller.clientHeight - scrollerCutOff, screentop = display.scroller.scrollTop, result = {};2713var docBottom = cm.doc.height + paddingVert(display);2714var atTop = y1 < snapMargin, atBottom = y2 > docBottom - snapMargin;2715if (y1 < screentop) {2716result.scrollTop = atTop ? 0 : y1;2717} else if (y2 > screentop + screen) {2718var newTop = Math.min(y1, (atBottom ? docBottom : y2) - screen);2719if (newTop != screentop) result.scrollTop = newTop;2720}27212722var screenw = display.scroller.clientWidth - scrollerCutOff, screenleft = display.scroller.scrollLeft;2723x1 += display.gutters.offsetWidth; x2 += display.gutters.offsetWidth;2724var gutterw = display.gutters.offsetWidth;2725var atLeft = x1 < gutterw + 10;2726if (x1 < screenleft + gutterw || atLeft) {2727if (atLeft) x1 = 0;2728result.scrollLeft = Math.max(0, x1 - 10 - gutterw);2729} else if (x2 > screenw + screenleft - 3) {2730result.scrollLeft = x2 + 10 - screenw;2731}2732return result;2733}27342735function updateScrollPos(cm, left, top) {2736cm.curOp.updateScrollPos = {scrollLeft: left == null ? cm.doc.scrollLeft : left,2737scrollTop: top == null ? cm.doc.scrollTop : top};2738}27392740function addToScrollPos(cm, left, top) {2741var pos = cm.curOp.updateScrollPos || (cm.curOp.updateScrollPos = {scrollLeft: cm.doc.scrollLeft, scrollTop: cm.doc.scrollTop});2742var scroll = cm.display.scroller;2743pos.scrollTop = Math.max(0, Math.min(scroll.scrollHeight - scroll.clientHeight, pos.scrollTop + top));2744pos.scrollLeft = Math.max(0, Math.min(scroll.scrollWidth - scroll.clientWidth, pos.scrollLeft + left));2745}27462747// API UTILITIES27482749function indentLine(cm, n, how, aggressive) {2750var doc = cm.doc;2751if (how == null) how = "add";2752if (how == "smart") {2753if (!cm.doc.mode.indent) how = "prev";2754else var state = getStateBefore(cm, n);2755}27562757var tabSize = cm.options.tabSize;2758var line = getLine(doc, n), curSpace = countColumn(line.text, null, tabSize);2759var curSpaceString = line.text.match(/^\s*/)[0], indentation;2760if (!aggressive && !/\S/.test(line.text)) {2761indentation = 0;2762how = "not";2763} else if (how == "smart") {2764indentation = cm.doc.mode.indent(state, line.text.slice(curSpaceString.length), line.text);2765if (indentation == Pass) {2766if (!aggressive) return;2767how = "prev";2768}2769}2770if (how == "prev") {2771if (n > doc.first) indentation = countColumn(getLine(doc, n-1).text, null, tabSize);2772else indentation = 0;2773} else if (how == "add") {2774indentation = curSpace + cm.options.indentUnit;2775} else if (how == "subtract") {2776indentation = curSpace - cm.options.indentUnit;2777} else if (typeof how == "number") {2778indentation = curSpace + how;2779}2780indentation = Math.max(0, indentation);27812782var indentString = "", pos = 0;2783if (cm.options.indentWithTabs)2784for (var i = Math.floor(indentation / tabSize); i; --i) {pos += tabSize; indentString += "\t";}2785if (pos < indentation) indentString += spaceStr(indentation - pos);27862787if (indentString != curSpaceString)2788replaceRange(cm.doc, indentString, Pos(n, 0), Pos(n, curSpaceString.length), "+input");2789else if (doc.sel.head.line == n && doc.sel.head.ch < curSpaceString.length)2790setSelection(doc, Pos(n, curSpaceString.length), Pos(n, curSpaceString.length), 1);2791line.stateAfter = null;2792}27932794function changeLine(cm, handle, op) {2795var no = handle, line = handle, doc = cm.doc;2796if (typeof handle == "number") line = getLine(doc, clipLine(doc, handle));2797else no = lineNo(handle);2798if (no == null) return null;2799if (op(line, no)) regChange(cm, no, no + 1);2800else return null;2801return line;2802}28032804function findPosH(doc, pos, dir, unit, visually) {2805var line = pos.line, ch = pos.ch, origDir = dir;2806var lineObj = getLine(doc, line);2807var possible = true;2808function findNextLine() {2809var l = line + dir;2810if (l < doc.first || l >= doc.first + doc.size) return (possible = false);2811line = l;2812return lineObj = getLine(doc, l);2813}2814function moveOnce(boundToLine) {2815var next = (visually ? moveVisually : moveLogically)(lineObj, ch, dir, true);2816if (next == null) {2817if (!boundToLine && findNextLine()) {2818if (visually) ch = (dir < 0 ? lineRight : lineLeft)(lineObj);2819else ch = dir < 0 ? lineObj.text.length : 0;2820} else return (possible = false);2821} else ch = next;2822return true;2823}28242825if (unit == "char") moveOnce();2826else if (unit == "column") moveOnce(true);2827else if (unit == "word" || unit == "group") {2828var sawType = null, group = unit == "group";2829for (var first = true;; first = false) {2830if (dir < 0 && !moveOnce(!first)) break;2831var cur = lineObj.text.charAt(ch) || "\n";2832var type = isWordChar(cur) ? "w"2833: !group ? null2834: /\s/.test(cur) ? null2835: "p";2836if (sawType && sawType != type) {2837if (dir < 0) {dir = 1; moveOnce();}2838break;2839}2840if (type) sawType = type;2841if (dir > 0 && !moveOnce(!first)) break;2842}2843}2844var result = skipAtomic(doc, Pos(line, ch), origDir, true);2845if (!possible) result.hitSide = true;2846return result;2847}28482849function findPosV(cm, pos, dir, unit) {2850var doc = cm.doc, x = pos.left, y;2851if (unit == "page") {2852var pageSize = Math.min(cm.display.wrapper.clientHeight, window.innerHeight || document.documentElement.clientHeight);2853y = pos.top + dir * (pageSize - (dir < 0 ? 1.5 : .5) * textHeight(cm.display));2854} else if (unit == "line") {2855y = dir > 0 ? pos.bottom + 3 : pos.top - 3;2856}2857for (;;) {2858var target = coordsChar(cm, x, y);2859if (!target.outside) break;2860if (dir < 0 ? y <= 0 : y >= doc.height) { target.hitSide = true; break; }2861y += dir * 5;2862}2863return target;2864}28652866function findWordAt(line, pos) {2867var start = pos.ch, end = pos.ch;2868if (line) {2869if ((pos.xRel < 0 || end == line.length) && start) --start; else ++end;2870var startChar = line.charAt(start);2871var check = isWordChar(startChar) ? isWordChar2872: /\s/.test(startChar) ? function(ch) {return /\s/.test(ch);}2873: function(ch) {return !/\s/.test(ch) && !isWordChar(ch);};2874while (start > 0 && check(line.charAt(start - 1))) --start;2875while (end < line.length && check(line.charAt(end))) ++end;2876}2877return {from: Pos(pos.line, start), to: Pos(pos.line, end)};2878}28792880function selectLine(cm, line) {2881extendSelection(cm.doc, Pos(line, 0), clipPos(cm.doc, Pos(line + 1, 0)));2882}28832884// PROTOTYPE28852886// The publicly visible API. Note that operation(null, f) means2887// 'wrap f in an operation, performed on its `this` parameter'28882889CodeMirror.prototype = {2890constructor: CodeMirror,2891focus: function(){window.focus(); focusInput(this); fastPoll(this);},28922893setOption: function(option, value) {2894var options = this.options, old = options[option];2895if (options[option] == value && option != "mode") return;2896options[option] = value;2897if (optionHandlers.hasOwnProperty(option))2898operation(this, optionHandlers[option])(this, value, old);2899},29002901getOption: function(option) {return this.options[option];},2902getDoc: function() {return this.doc;},29032904addKeyMap: function(map, bottom) {2905this.state.keyMaps[bottom ? "push" : "unshift"](map);2906},2907removeKeyMap: function(map) {2908var maps = this.state.keyMaps;2909for (var i = 0; i < maps.length; ++i)2910if (maps[i] == map || (typeof maps[i] != "string" && maps[i].name == map)) {2911maps.splice(i, 1);2912return true;2913}2914},29152916addOverlay: operation(null, function(spec, options) {2917var mode = spec.token ? spec : CodeMirror.getMode(this.options, spec);2918if (mode.startState) throw new Error("Overlays may not be stateful.");2919this.state.overlays.push({mode: mode, modeSpec: spec, opaque: options && options.opaque});2920this.state.modeGen++;2921regChange(this);2922}),2923removeOverlay: operation(null, function(spec) {2924var overlays = this.state.overlays;2925for (var i = 0; i < overlays.length; ++i) {2926var cur = overlays[i].modeSpec;2927if (cur == spec || typeof spec == "string" && cur.name == spec) {2928overlays.splice(i, 1);2929this.state.modeGen++;2930regChange(this);2931return;2932}2933}2934}),29352936indentLine: operation(null, function(n, dir, aggressive) {2937if (typeof dir != "string" && typeof dir != "number") {2938if (dir == null) dir = this.options.smartIndent ? "smart" : "prev";2939else dir = dir ? "add" : "subtract";2940}2941if (isLine(this.doc, n)) indentLine(this, n, dir, aggressive);2942}),2943indentSelection: operation(null, function(how) {2944var sel = this.doc.sel;2945if (posEq(sel.from, sel.to)) return indentLine(this, sel.from.line, how, true);2946var e = sel.to.line - (sel.to.ch ? 0 : 1);2947for (var i = sel.from.line; i <= e; ++i) indentLine(this, i, how);2948}),29492950// Fetch the parser token for a given character. Useful for hacks2951// that want to inspect the mode state (say, for completion).2952getTokenAt: function(pos, precise) {2953var doc = this.doc;2954pos = clipPos(doc, pos);2955var state = getStateBefore(this, pos.line, precise), mode = this.doc.mode;2956var line = getLine(doc, pos.line);2957var stream = new StringStream(line.text, this.options.tabSize);2958while (stream.pos < pos.ch && !stream.eol()) {2959stream.start = stream.pos;2960var style = mode.token(stream, state);2961}2962return {start: stream.start,2963end: stream.pos,2964string: stream.current(),2965className: style || null, // Deprecated, use 'type' instead2966type: style || null,2967state: state};2968},29692970getTokenTypeAt: function(pos) {2971pos = clipPos(this.doc, pos);2972var styles = getLineStyles(this, getLine(this.doc, pos.line));2973var before = 0, after = (styles.length - 1) / 2, ch = pos.ch;2974if (ch == 0) return styles[2];2975for (;;) {2976var mid = (before + after) >> 1;2977if ((mid ? styles[mid * 2 - 1] : 0) >= ch) after = mid;2978else if (styles[mid * 2 + 1] < ch) before = mid + 1;2979else return styles[mid * 2 + 2];2980}2981},29822983getModeAt: function(pos) {2984var mode = this.doc.mode;2985if (!mode.innerMode) return mode;2986return CodeMirror.innerMode(mode, this.getTokenAt(pos).state).mode;2987},29882989getHelper: function(pos, type) {2990return this.getHelpers(pos, type)[0];2991},29922993getHelpers: function(pos, type) {2994var found = [];2995if (!helpers.hasOwnProperty(type)) return helpers;2996var help = helpers[type], mode = this.getModeAt(pos);2997if (typeof mode[type] == "string") {2998if (help[mode[type]]) found.push(help[mode[type]]);2999} else if (mode[type]) {3000for (var i = 0; i < mode[type].length; i++) {3001var val = help[mode[type][i]];3002if (val) found.push(val);3003}3004} else if (mode.helperType && help[mode.helperType]) {3005found.push(help[mode.helperType]);3006} else if (help[mode.name]) {3007found.push(help[mode.name]);3008}3009for (var i = 0; i < help._global.length; i++) {3010var cur = help._global[i];3011if (cur.pred(mode, this) && indexOf(found, cur.val) == -1)3012found.push(cur.val);3013}3014return found;3015},30163017getStateAfter: function(line, precise) {3018var doc = this.doc;3019line = clipLine(doc, line == null ? doc.first + doc.size - 1: line);3020return getStateBefore(this, line + 1, precise);3021},30223023cursorCoords: function(start, mode) {3024var pos, sel = this.doc.sel;3025if (start == null) pos = sel.head;3026else if (typeof start == "object") pos = clipPos(this.doc, start);3027else pos = start ? sel.from : sel.to;3028return cursorCoords(this, pos, mode || "page");3029},30303031charCoords: function(pos, mode) {3032return charCoords(this, clipPos(this.doc, pos), mode || "page");3033},30343035coordsChar: function(coords, mode) {3036coords = fromCoordSystem(this, coords, mode || "page");3037return coordsChar(this, coords.left, coords.top);3038},30393040lineAtHeight: function(height, mode) {3041height = fromCoordSystem(this, {top: height, left: 0}, mode || "page").top;3042return lineAtHeight(this.doc, height + this.display.viewOffset);3043},3044heightAtLine: function(line, mode) {3045var end = false, last = this.doc.first + this.doc.size - 1;3046if (line < this.doc.first) line = this.doc.first;3047else if (line > last) { line = last; end = true; }3048var lineObj = getLine(this.doc, line);3049return intoCoordSystem(this, getLine(this.doc, line), {top: 0, left: 0}, mode || "page").top +3050(end ? lineObj.height : 0);3051},30523053defaultTextHeight: function() { return textHeight(this.display); },3054defaultCharWidth: function() { return charWidth(this.display); },30553056setGutterMarker: operation(null, function(line, gutterID, value) {3057return changeLine(this, line, function(line) {3058var markers = line.gutterMarkers || (line.gutterMarkers = {});3059markers[gutterID] = value;3060if (!value && isEmpty(markers)) line.gutterMarkers = null;3061return true;3062});3063}),30643065clearGutter: operation(null, function(gutterID) {3066var cm = this, doc = cm.doc, i = doc.first;3067doc.iter(function(line) {3068if (line.gutterMarkers && line.gutterMarkers[gutterID]) {3069line.gutterMarkers[gutterID] = null;3070regChange(cm, i, i + 1);3071if (isEmpty(line.gutterMarkers)) line.gutterMarkers = null;3072}3073++i;3074});3075}),30763077addLineClass: operation(null, function(handle, where, cls) {3078return changeLine(this, handle, function(line) {3079var prop = where == "text" ? "textClass" : where == "background" ? "bgClass" : "wrapClass";3080if (!line[prop]) line[prop] = cls;3081else if (new RegExp("(?:^|\\s)" + cls + "(?:$|\\s)").test(line[prop])) return false;3082else line[prop] += " " + cls;3083return true;3084});3085}),30863087removeLineClass: operation(null, function(handle, where, cls) {3088return changeLine(this, handle, function(line) {3089var prop = where == "text" ? "textClass" : where == "background" ? "bgClass" : "wrapClass";3090var cur = line[prop];3091if (!cur) return false;3092else if (cls == null) line[prop] = null;3093else {3094var found = cur.match(new RegExp("(?:^|\\s+)" + cls + "(?:$|\\s+)"));3095if (!found) return false;3096var end = found.index + found[0].length;3097line[prop] = cur.slice(0, found.index) + (!found.index || end == cur.length ? "" : " ") + cur.slice(end) || null;3098}3099return true;3100});3101}),31023103addLineWidget: operation(null, function(handle, node, options) {3104return addLineWidget(this, handle, node, options);3105}),31063107removeLineWidget: function(widget) { widget.clear(); },31083109lineInfo: function(line) {3110if (typeof line == "number") {3111if (!isLine(this.doc, line)) return null;3112var n = line;3113line = getLine(this.doc, line);3114if (!line) return null;3115} else {3116var n = lineNo(line);3117if (n == null) return null;3118}3119return {line: n, handle: line, text: line.text, gutterMarkers: line.gutterMarkers,3120textClass: line.textClass, bgClass: line.bgClass, wrapClass: line.wrapClass,3121widgets: line.widgets};3122},31233124getViewport: function() { return {from: this.display.showingFrom, to: this.display.showingTo};},31253126addWidget: function(pos, node, scroll, vert, horiz) {3127var display = this.display;3128pos = cursorCoords(this, clipPos(this.doc, pos));3129var top = pos.bottom, left = pos.left;3130node.style.position = "absolute";3131display.sizer.appendChild(node);3132if (vert == "over") {3133top = pos.top;3134} else if (vert == "above" || vert == "near") {3135var vspace = Math.max(display.wrapper.clientHeight, this.doc.height),3136hspace = Math.max(display.sizer.clientWidth, display.lineSpace.clientWidth);3137// Default to positioning above (if specified and possible); otherwise default to positioning below3138if ((vert == 'above' || pos.bottom + node.offsetHeight > vspace) && pos.top > node.offsetHeight)3139top = pos.top - node.offsetHeight;3140else if (pos.bottom + node.offsetHeight <= vspace)3141top = pos.bottom;3142if (left + node.offsetWidth > hspace)3143left = hspace - node.offsetWidth;3144}3145node.style.top = top + "px";3146node.style.left = node.style.right = "";3147if (horiz == "right") {3148left = display.sizer.clientWidth - node.offsetWidth;3149node.style.right = "0px";3150} else {3151if (horiz == "left") left = 0;3152else if (horiz == "middle") left = (display.sizer.clientWidth - node.offsetWidth) / 2;3153node.style.left = left + "px";3154}3155if (scroll)3156scrollIntoView(this, left, top, left + node.offsetWidth, top + node.offsetHeight);3157},31583159triggerOnKeyDown: operation(null, onKeyDown),31603161execCommand: function(cmd) {3162if (commands.hasOwnProperty(cmd))3163return commands[cmd](this);3164},31653166findPosH: function(from, amount, unit, visually) {3167var dir = 1;3168if (amount < 0) { dir = -1; amount = -amount; }3169for (var i = 0, cur = clipPos(this.doc, from); i < amount; ++i) {3170cur = findPosH(this.doc, cur, dir, unit, visually);3171if (cur.hitSide) break;3172}3173return cur;3174},31753176moveH: operation(null, function(dir, unit) {3177var sel = this.doc.sel, pos;3178if (sel.shift || sel.extend || posEq(sel.from, sel.to))3179pos = findPosH(this.doc, sel.head, dir, unit, this.options.rtlMoveVisually);3180else3181pos = dir < 0 ? sel.from : sel.to;3182extendSelection(this.doc, pos, pos, dir);3183}),31843185deleteH: operation(null, function(dir, unit) {3186var sel = this.doc.sel;3187if (!posEq(sel.from, sel.to)) replaceRange(this.doc, "", sel.from, sel.to, "+delete");3188else replaceRange(this.doc, "", sel.from, findPosH(this.doc, sel.head, dir, unit, false), "+delete");3189this.curOp.userSelChange = true;3190}),31913192findPosV: function(from, amount, unit, goalColumn) {3193var dir = 1, x = goalColumn;3194if (amount < 0) { dir = -1; amount = -amount; }3195for (var i = 0, cur = clipPos(this.doc, from); i < amount; ++i) {3196var coords = cursorCoords(this, cur, "div");3197if (x == null) x = coords.left;3198else coords.left = x;3199cur = findPosV(this, coords, dir, unit);3200if (cur.hitSide) break;3201}3202return cur;3203},32043205moveV: operation(null, function(dir, unit) {3206var sel = this.doc.sel, target, goal;3207if (sel.shift || sel.extend || posEq(sel.from, sel.to)) {3208var pos = cursorCoords(this, sel.head, "div");3209if (sel.goalColumn != null) pos.left = sel.goalColumn;3210target = findPosV(this, pos, dir, unit);3211if (unit == "page") addToScrollPos(this, 0, charCoords(this, target, "div").top - pos.top);3212goal = pos.left;3213} else {3214target = dir < 0 ? sel.from : sel.to;3215}3216extendSelection(this.doc, target, target, dir);3217if (goal != null) sel.goalColumn = goal;3218}),32193220toggleOverwrite: function(value) {3221if (value != null && value == this.state.overwrite) return;3222if (this.state.overwrite = !this.state.overwrite)3223this.display.cursor.className += " CodeMirror-overwrite";3224else3225this.display.cursor.className = this.display.cursor.className.replace(" CodeMirror-overwrite", "");3226},3227hasFocus: function() { return this.state.focused; },32283229scrollTo: operation(null, function(x, y) {3230updateScrollPos(this, x, y);3231}),3232getScrollInfo: function() {3233var scroller = this.display.scroller, co = scrollerCutOff;3234return {left: scroller.scrollLeft, top: scroller.scrollTop,3235height: scroller.scrollHeight - co, width: scroller.scrollWidth - co,3236clientHeight: scroller.clientHeight - co, clientWidth: scroller.clientWidth - co};3237},32383239scrollIntoView: operation(null, function(range, margin) {3240if (range == null) range = {from: this.doc.sel.head, to: null};3241else if (typeof range == "number") range = {from: Pos(range, 0), to: null};3242else if (range.from == null) range = {from: range, to: null};3243if (!range.to) range.to = range.from;3244if (!margin) margin = 0;32453246var coords = range;3247if (range.from.line != null) {3248this.curOp.scrollToPos = {from: range.from, to: range.to, margin: margin};3249coords = {from: cursorCoords(this, range.from),3250to: cursorCoords(this, range.to)};3251}3252var sPos = calculateScrollPos(this, Math.min(coords.from.left, coords.to.left),3253Math.min(coords.from.top, coords.to.top) - margin,3254Math.max(coords.from.right, coords.to.right),3255Math.max(coords.from.bottom, coords.to.bottom) + margin);3256updateScrollPos(this, sPos.scrollLeft, sPos.scrollTop);3257}),32583259setSize: operation(null, function(width, height) {3260function interpret(val) {3261return typeof val == "number" || /^\d+$/.test(String(val)) ? val + "px" : val;3262}3263if (width != null) this.display.wrapper.style.width = interpret(width);3264if (height != null) this.display.wrapper.style.height = interpret(height);3265if (this.options.lineWrapping)3266this.display.measureLineCache.length = this.display.measureLineCachePos = 0;3267this.curOp.forceUpdate = true;3268}),32693270operation: function(f){return runInOp(this, f);},32713272refresh: operation(null, function() {3273var badHeight = this.display.cachedTextHeight == null;3274clearCaches(this);3275updateScrollPos(this, this.doc.scrollLeft, this.doc.scrollTop);3276regChange(this);3277if (badHeight) estimateLineHeights(this);3278}),32793280swapDoc: operation(null, function(doc) {3281var old = this.doc;3282old.cm = null;3283attachDoc(this, doc);3284clearCaches(this);3285resetInput(this, true);3286updateScrollPos(this, doc.scrollLeft, doc.scrollTop);3287signalLater(this, "swapDoc", this, old);3288return old;3289}),32903291getInputField: function(){return this.display.input;},3292getWrapperElement: function(){return this.display.wrapper;},3293getScrollerElement: function(){return this.display.scroller;},3294getGutterElement: function(){return this.display.gutters;}3295};3296eventMixin(CodeMirror);32973298// OPTION DEFAULTS32993300var optionHandlers = CodeMirror.optionHandlers = {};33013302// The default configuration options.3303var defaults = CodeMirror.defaults = {};33043305function option(name, deflt, handle, notOnInit) {3306CodeMirror.defaults[name] = deflt;3307if (handle) optionHandlers[name] =3308notOnInit ? function(cm, val, old) {if (old != Init) handle(cm, val, old);} : handle;3309}33103311var Init = CodeMirror.Init = {toString: function(){return "CodeMirror.Init";}};33123313// These two are, on init, called from the constructor because they3314// have to be initialized before the editor can start at all.3315option("value", "", function(cm, val) {3316cm.setValue(val);3317}, true);3318option("mode", null, function(cm, val) {3319cm.doc.modeOption = val;3320loadMode(cm);3321}, true);33223323option("indentUnit", 2, loadMode, true);3324option("indentWithTabs", false);3325option("smartIndent", true);3326option("tabSize", 4, function(cm) {3327resetModeState(cm);3328clearCaches(cm);3329regChange(cm);3330}, true);3331option("specialChars", /[\t\u0000-\u0019\u00ad\u200b\u2028\u2029\ufeff]/g, function(cm, val) {3332cm.options.specialChars = new RegExp(val.source + (val.test("\t") ? "" : "|\t"), "g");3333cm.refresh();3334}, true);3335option("specialCharPlaceholder", defaultSpecialCharPlaceholder, function(cm) {cm.refresh();}, true);3336option("electricChars", true);3337option("rtlMoveVisually", !windows);3338option("wholeLineUpdateBefore", true);33393340option("theme", "default", function(cm) {3341themeChanged(cm);3342guttersChanged(cm);3343}, true);3344option("keyMap", "default", keyMapChanged);3345option("extraKeys", null);33463347option("onKeyEvent", null);3348option("onDragEvent", null);33493350option("lineWrapping", false, wrappingChanged, true);3351option("gutters", [], function(cm) {3352setGuttersForLineNumbers(cm.options);3353guttersChanged(cm);3354}, true);3355option("fixedGutter", true, function(cm, val) {3356cm.display.gutters.style.left = val ? compensateForHScroll(cm.display) + "px" : "0";3357cm.refresh();3358}, true);3359option("coverGutterNextToScrollbar", false, updateScrollbars, true);3360option("lineNumbers", false, function(cm) {3361setGuttersForLineNumbers(cm.options);3362guttersChanged(cm);3363}, true);3364option("firstLineNumber", 1, guttersChanged, true);3365option("lineNumberFormatter", function(integer) {return integer;}, guttersChanged, true);3366option("showCursorWhenSelecting", false, updateSelection, true);33673368option("resetSelectionOnContextMenu", true);33693370option("readOnly", false, function(cm, val) {3371if (val == "nocursor") {3372onBlur(cm);3373cm.display.input.blur();3374cm.display.disabled = true;3375} else {3376cm.display.disabled = false;3377if (!val) resetInput(cm, true);3378}3379});3380option("disableInput", false, function(cm, val) {if (!val) resetInput(cm, true);}, true);3381option("dragDrop", true);33823383option("cursorBlinkRate", 530);3384option("cursorScrollMargin", 0);3385option("cursorHeight", 1);3386option("workTime", 100);3387option("workDelay", 100);3388option("flattenSpans", true, resetModeState, true);3389option("addModeClass", false, resetModeState, true);3390option("pollInterval", 100);3391option("undoDepth", 40, function(cm, val){cm.doc.history.undoDepth = val;});3392option("historyEventDelay", 500);3393option("viewportMargin", 10, function(cm){cm.refresh();}, true);3394option("maxHighlightLength", 10000, resetModeState, true);3395option("crudeMeasuringFrom", 10000);3396option("moveInputWithCursor", true, function(cm, val) {3397if (!val) cm.display.inputDiv.style.top = cm.display.inputDiv.style.left = 0;3398});33993400option("tabindex", null, function(cm, val) {3401cm.display.input.tabIndex = val || "";3402});3403option("autofocus", null);34043405// MODE DEFINITION AND QUERYING34063407// Known modes, by name and by MIME3408var modes = CodeMirror.modes = {}, mimeModes = CodeMirror.mimeModes = {};34093410CodeMirror.defineMode = function(name, mode) {3411if (!CodeMirror.defaults.mode && name != "null") CodeMirror.defaults.mode = name;3412if (arguments.length > 2) {3413mode.dependencies = [];3414for (var i = 2; i < arguments.length; ++i) mode.dependencies.push(arguments[i]);3415}3416modes[name] = mode;3417};34183419CodeMirror.defineMIME = function(mime, spec) {3420mimeModes[mime] = spec;3421};34223423CodeMirror.resolveMode = function(spec) {3424if (typeof spec == "string" && mimeModes.hasOwnProperty(spec)) {3425spec = mimeModes[spec];3426} else if (spec && typeof spec.name == "string" && mimeModes.hasOwnProperty(spec.name)) {3427var found = mimeModes[spec.name];3428spec = createObj(found, spec);3429spec.name = found.name;3430} else if (typeof spec == "string" && /^[\w\-]+\/[\w\-]+\+xml$/.test(spec)) {3431return CodeMirror.resolveMode("application/xml");3432}3433if (typeof spec == "string") return {name: spec};3434else return spec || {name: "null"};3435};34363437CodeMirror.getMode = function(options, spec) {3438var spec = CodeMirror.resolveMode(spec);3439var mfactory = modes[spec.name];3440if (!mfactory) return CodeMirror.getMode(options, "text/plain");3441var modeObj = mfactory(options, spec);3442if (modeExtensions.hasOwnProperty(spec.name)) {3443var exts = modeExtensions[spec.name];3444for (var prop in exts) {3445if (!exts.hasOwnProperty(prop)) continue;3446if (modeObj.hasOwnProperty(prop)) modeObj["_" + prop] = modeObj[prop];3447modeObj[prop] = exts[prop];3448}3449}3450modeObj.name = spec.name;3451if (spec.helperType) modeObj.helperType = spec.helperType;3452if (spec.modeProps) for (var prop in spec.modeProps)3453modeObj[prop] = spec.modeProps[prop];34543455return modeObj;3456};34573458CodeMirror.defineMode("null", function() {3459return {token: function(stream) {stream.skipToEnd();}};3460});3461CodeMirror.defineMIME("text/plain", "null");34623463var modeExtensions = CodeMirror.modeExtensions = {};3464CodeMirror.extendMode = function(mode, properties) {3465var exts = modeExtensions.hasOwnProperty(mode) ? modeExtensions[mode] : (modeExtensions[mode] = {});3466copyObj(properties, exts);3467};34683469// EXTENSIONS34703471CodeMirror.defineExtension = function(name, func) {3472CodeMirror.prototype[name] = func;3473};3474CodeMirror.defineDocExtension = function(name, func) {3475Doc.prototype[name] = func;3476};3477CodeMirror.defineOption = option;34783479var initHooks = [];3480CodeMirror.defineInitHook = function(f) {initHooks.push(f);};34813482var helpers = CodeMirror.helpers = {};3483CodeMirror.registerHelper = function(type, name, value) {3484if (!helpers.hasOwnProperty(type)) helpers[type] = CodeMirror[type] = {_global: []};3485helpers[type][name] = value;3486};3487CodeMirror.registerGlobalHelper = function(type, name, predicate, value) {3488CodeMirror.registerHelper(type, name, value);3489helpers[type]._global.push({pred: predicate, val: value});3490};34913492// UTILITIES34933494CodeMirror.isWordChar = isWordChar;34953496// MODE STATE HANDLING34973498// Utility functions for working with state. Exported because modes3499// sometimes need to do this.3500function copyState(mode, state) {3501if (state === true) return state;3502if (mode.copyState) return mode.copyState(state);3503var nstate = {};3504for (var n in state) {3505var val = state[n];3506if (val instanceof Array) val = val.concat([]);3507nstate[n] = val;3508}3509return nstate;3510}3511CodeMirror.copyState = copyState;35123513function startState(mode, a1, a2) {3514return mode.startState ? mode.startState(a1, a2) : true;3515}3516CodeMirror.startState = startState;35173518CodeMirror.innerMode = function(mode, state) {3519while (mode.innerMode) {3520var info = mode.innerMode(state);3521if (!info || info.mode == mode) break;3522state = info.state;3523mode = info.mode;3524}3525return info || {mode: mode, state: state};3526};35273528// STANDARD COMMANDS35293530var commands = CodeMirror.commands = {3531selectAll: function(cm) {cm.setSelection(Pos(cm.firstLine(), 0), Pos(cm.lastLine()));},3532killLine: function(cm) {3533var from = cm.getCursor(true), to = cm.getCursor(false), sel = !posEq(from, to);3534if (!sel && cm.getLine(from.line).length == from.ch)3535cm.replaceRange("", from, Pos(from.line + 1, 0), "+delete");3536else cm.replaceRange("", from, sel ? to : Pos(from.line), "+delete");3537},3538deleteLine: function(cm) {3539var l = cm.getCursor().line;3540cm.replaceRange("", Pos(l, 0), Pos(l), "+delete");3541},3542delLineLeft: function(cm) {3543var cur = cm.getCursor();3544cm.replaceRange("", Pos(cur.line, 0), cur, "+delete");3545},3546undo: function(cm) {cm.undo();},3547redo: function(cm) {cm.redo();},3548goDocStart: function(cm) {cm.extendSelection(Pos(cm.firstLine(), 0));},3549goDocEnd: function(cm) {cm.extendSelection(Pos(cm.lastLine()));},3550goLineStart: function(cm) {3551cm.extendSelection(lineStart(cm, cm.getCursor().line));3552},3553goLineStartSmart: function(cm) {3554var cur = cm.getCursor(), start = lineStart(cm, cur.line);3555var line = cm.getLineHandle(start.line);3556var order = getOrder(line);3557if (!order || order[0].level == 0) {3558var firstNonWS = Math.max(0, line.text.search(/\S/));3559var inWS = cur.line == start.line && cur.ch <= firstNonWS && cur.ch;3560cm.extendSelection(Pos(start.line, inWS ? 0 : firstNonWS));3561} else cm.extendSelection(start);3562},3563goLineEnd: function(cm) {3564cm.extendSelection(lineEnd(cm, cm.getCursor().line));3565},3566goLineRight: function(cm) {3567var top = cm.charCoords(cm.getCursor(), "div").top + 5;3568cm.extendSelection(cm.coordsChar({left: cm.display.lineDiv.offsetWidth + 100, top: top}, "div"));3569},3570goLineLeft: function(cm) {3571var top = cm.charCoords(cm.getCursor(), "div").top + 5;3572cm.extendSelection(cm.coordsChar({left: 0, top: top}, "div"));3573},3574goLineUp: function(cm) {cm.moveV(-1, "line");},3575goLineDown: function(cm) {cm.moveV(1, "line");},3576goPageUp: function(cm) {cm.moveV(-1, "page");},3577goPageDown: function(cm) {cm.moveV(1, "page");},3578goCharLeft: function(cm) {cm.moveH(-1, "char");},3579goCharRight: function(cm) {cm.moveH(1, "char");},3580goColumnLeft: function(cm) {cm.moveH(-1, "column");},3581goColumnRight: function(cm) {cm.moveH(1, "column");},3582goWordLeft: function(cm) {cm.moveH(-1, "word");},3583goGroupRight: function(cm) {cm.moveH(1, "group");},3584goGroupLeft: function(cm) {cm.moveH(-1, "group");},3585goWordRight: function(cm) {cm.moveH(1, "word");},3586delCharBefore: function(cm) {cm.deleteH(-1, "char");},3587delCharAfter: function(cm) {cm.deleteH(1, "char");},3588delWordBefore: function(cm) {cm.deleteH(-1, "word");},3589delWordAfter: function(cm) {cm.deleteH(1, "word");},3590delGroupBefore: function(cm) {cm.deleteH(-1, "group");},3591delGroupAfter: function(cm) {cm.deleteH(1, "group");},3592indentAuto: function(cm) {cm.indentSelection("smart");},3593indentMore: function(cm) {cm.indentSelection("add");},3594indentLess: function(cm) {cm.indentSelection("subtract");},3595insertTab: function(cm) {3596cm.replaceSelection("\t", "end", "+input");3597},3598defaultTab: function(cm) {3599if (cm.somethingSelected()) cm.indentSelection("add");3600else cm.replaceSelection("\t", "end", "+input");3601},3602transposeChars: function(cm) {3603var cur = cm.getCursor(), line = cm.getLine(cur.line);3604if (cur.ch > 0 && cur.ch < line.length - 1)3605cm.replaceRange(line.charAt(cur.ch) + line.charAt(cur.ch - 1),3606Pos(cur.line, cur.ch - 1), Pos(cur.line, cur.ch + 1));3607},3608newlineAndIndent: function(cm) {3609operation(cm, function() {3610cm.replaceSelection("\n", "end", "+input");3611cm.indentLine(cm.getCursor().line, null, true);3612})();3613},3614toggleOverwrite: function(cm) {cm.toggleOverwrite();}3615};36163617// STANDARD KEYMAPS36183619var keyMap = CodeMirror.keyMap = {};3620keyMap.basic = {3621"Left": "goCharLeft", "Right": "goCharRight", "Up": "goLineUp", "Down": "goLineDown",3622"End": "goLineEnd", "Home": "goLineStartSmart", "PageUp": "goPageUp", "PageDown": "goPageDown",3623"Delete": "delCharAfter", "Backspace": "delCharBefore", "Shift-Backspace": "delCharBefore",3624"Tab": "defaultTab", "Shift-Tab": "indentAuto",3625"Enter": "newlineAndIndent", "Insert": "toggleOverwrite"3626};3627// Note that the save and find-related commands aren't defined by3628// default. Unknown commands are simply ignored.3629keyMap.pcDefault = {3630"Ctrl-A": "selectAll", "Ctrl-D": "deleteLine", "Ctrl-Z": "undo", "Shift-Ctrl-Z": "redo", "Ctrl-Y": "redo",3631"Ctrl-Home": "goDocStart", "Alt-Up": "goDocStart", "Ctrl-End": "goDocEnd", "Ctrl-Down": "goDocEnd",3632"Ctrl-Left": "goGroupLeft", "Ctrl-Right": "goGroupRight", "Alt-Left": "goLineStart", "Alt-Right": "goLineEnd",3633"Ctrl-Backspace": "delGroupBefore", "Ctrl-Delete": "delGroupAfter", "Ctrl-S": "save", "Ctrl-F": "find",3634"Ctrl-G": "findNext", "Shift-Ctrl-G": "findPrev", "Shift-Ctrl-F": "replace", "Shift-Ctrl-R": "replaceAll",3635"Ctrl-[": "indentLess", "Ctrl-]": "indentMore",3636fallthrough: "basic"3637};3638keyMap.macDefault = {3639"Cmd-A": "selectAll", "Cmd-D": "deleteLine", "Cmd-Z": "undo", "Shift-Cmd-Z": "redo", "Cmd-Y": "redo",3640"Cmd-Up": "goDocStart", "Cmd-End": "goDocEnd", "Cmd-Down": "goDocEnd", "Alt-Left": "goGroupLeft",3641"Alt-Right": "goGroupRight", "Cmd-Left": "goLineStart", "Cmd-Right": "goLineEnd", "Alt-Backspace": "delGroupBefore",3642"Ctrl-Alt-Backspace": "delGroupAfter", "Alt-Delete": "delGroupAfter", "Cmd-S": "save", "Cmd-F": "find",3643"Cmd-G": "findNext", "Shift-Cmd-G": "findPrev", "Cmd-Alt-F": "replace", "Shift-Cmd-Alt-F": "replaceAll",3644"Cmd-[": "indentLess", "Cmd-]": "indentMore", "Cmd-Backspace": "delLineLeft",3645fallthrough: ["basic", "emacsy"]3646};3647keyMap["default"] = mac ? keyMap.macDefault : keyMap.pcDefault;3648keyMap.emacsy = {3649"Ctrl-F": "goCharRight", "Ctrl-B": "goCharLeft", "Ctrl-P": "goLineUp", "Ctrl-N": "goLineDown",3650"Alt-F": "goWordRight", "Alt-B": "goWordLeft", "Ctrl-A": "goLineStart", "Ctrl-E": "goLineEnd",3651"Ctrl-V": "goPageDown", "Shift-Ctrl-V": "goPageUp", "Ctrl-D": "delCharAfter", "Ctrl-H": "delCharBefore",3652"Alt-D": "delWordAfter", "Alt-Backspace": "delWordBefore", "Ctrl-K": "killLine", "Ctrl-T": "transposeChars"3653};36543655// KEYMAP DISPATCH36563657function getKeyMap(val) {3658if (typeof val == "string") return keyMap[val];3659else return val;3660}36613662function lookupKey(name, maps, handle) {3663function lookup(map) {3664map = getKeyMap(map);3665var found = map[name];3666if (found === false) return "stop";3667if (found != null && handle(found)) return true;3668if (map.nofallthrough) return "stop";36693670var fallthrough = map.fallthrough;3671if (fallthrough == null) return false;3672if (Object.prototype.toString.call(fallthrough) != "[object Array]")3673return lookup(fallthrough);3674for (var i = 0, e = fallthrough.length; i < e; ++i) {3675var done = lookup(fallthrough[i]);3676if (done) return done;3677}3678return false;3679}36803681for (var i = 0; i < maps.length; ++i) {3682var done = lookup(maps[i]);3683if (done) return done != "stop";3684}3685}3686function isModifierKey(event) {3687var name = keyNames[event.keyCode];3688return name == "Ctrl" || name == "Alt" || name == "Shift" || name == "Mod";3689}3690function keyName(event, noShift) {3691if (opera && event.keyCode == 34 && event["char"]) return false;3692var name = keyNames[event.keyCode];3693if (name == null || event.altGraphKey) return false;3694if (event.altKey) name = "Alt-" + name;3695if (flipCtrlCmd ? event.metaKey : event.ctrlKey) name = "Ctrl-" + name;3696if (flipCtrlCmd ? event.ctrlKey : event.metaKey) name = "Cmd-" + name;3697if (!noShift && event.shiftKey) name = "Shift-" + name;3698return name;3699}3700CodeMirror.lookupKey = lookupKey;3701CodeMirror.isModifierKey = isModifierKey;3702CodeMirror.keyName = keyName;37033704// FROMTEXTAREA37053706CodeMirror.fromTextArea = function(textarea, options) {3707if (!options) options = {};3708options.value = textarea.value;3709if (!options.tabindex && textarea.tabindex)3710options.tabindex = textarea.tabindex;3711if (!options.placeholder && textarea.placeholder)3712options.placeholder = textarea.placeholder;3713// Set autofocus to true if this textarea is focused, or if it has3714// autofocus and no other element is focused.3715if (options.autofocus == null) {3716var hasFocus = document.body;3717// doc.activeElement occasionally throws on IE3718try { hasFocus = document.activeElement; } catch(e) {}3719options.autofocus = hasFocus == textarea ||3720textarea.getAttribute("autofocus") != null && hasFocus == document.body;3721}37223723function save() {textarea.value = cm.getValue();}3724if (textarea.form) {3725on(textarea.form, "submit", save);3726// Deplorable hack to make the submit method do the right thing.3727if (!options.leaveSubmitMethodAlone) {3728var form = textarea.form, realSubmit = form.submit;3729try {3730var wrappedSubmit = form.submit = function() {3731save();3732form.submit = realSubmit;3733form.submit();3734form.submit = wrappedSubmit;3735};3736} catch(e) {}3737}3738}37393740textarea.style.display = "none";3741var cm = CodeMirror(function(node) {3742textarea.parentNode.insertBefore(node, textarea.nextSibling);3743}, options);3744cm.save = save;3745cm.getTextArea = function() { return textarea; };3746cm.toTextArea = function() {3747save();3748textarea.parentNode.removeChild(cm.getWrapperElement());3749textarea.style.display = "";3750if (textarea.form) {3751off(textarea.form, "submit", save);3752if (typeof textarea.form.submit == "function")3753textarea.form.submit = realSubmit;3754}3755};3756return cm;3757};37583759// STRING STREAM37603761// Fed to the mode parsers, provides helper functions to make3762// parsers more succinct.37633764// The character stream used by a mode's parser.3765function StringStream(string, tabSize) {3766this.pos = this.start = 0;3767this.string = string;3768this.tabSize = tabSize || 8;3769this.lastColumnPos = this.lastColumnValue = 0;3770this.lineStart = 0;3771}37723773StringStream.prototype = {3774eol: function() {return this.pos >= this.string.length;},3775sol: function() {return this.pos == this.lineStart;},3776peek: function() {return this.string.charAt(this.pos) || undefined;},3777next: function() {3778if (this.pos < this.string.length)3779return this.string.charAt(this.pos++);3780},3781eat: function(match) {3782var ch = this.string.charAt(this.pos);3783if (typeof match == "string") var ok = ch == match;3784else var ok = ch && (match.test ? match.test(ch) : match(ch));3785if (ok) {++this.pos; return ch;}3786},3787eatWhile: function(match) {3788var start = this.pos;3789while (this.eat(match)){}3790return this.pos > start;3791},3792eatSpace: function() {3793var start = this.pos;3794while (/[\s\u00a0]/.test(this.string.charAt(this.pos))) ++this.pos;3795return this.pos > start;3796},3797skipToEnd: function() {this.pos = this.string.length;},3798skipTo: function(ch) {3799var found = this.string.indexOf(ch, this.pos);3800if (found > -1) {this.pos = found; return true;}3801},3802backUp: function(n) {this.pos -= n;},3803column: function() {3804if (this.lastColumnPos < this.start) {3805this.lastColumnValue = countColumn(this.string, this.start, this.tabSize, this.lastColumnPos, this.lastColumnValue);3806this.lastColumnPos = this.start;3807}3808return this.lastColumnValue - (this.lineStart ? countColumn(this.string, this.lineStart, this.tabSize) : 0);3809},3810indentation: function() {3811return countColumn(this.string, null, this.tabSize) -3812(this.lineStart ? countColumn(this.string, this.lineStart, this.tabSize) : 0);3813},3814match: function(pattern, consume, caseInsensitive) {3815if (typeof pattern == "string") {3816var cased = function(str) {return caseInsensitive ? str.toLowerCase() : str;};3817var substr = this.string.substr(this.pos, pattern.length);3818if (cased(substr) == cased(pattern)) {3819if (consume !== false) this.pos += pattern.length;3820return true;3821}3822} else {3823var match = this.string.slice(this.pos).match(pattern);3824if (match && match.index > 0) return null;3825if (match && consume !== false) this.pos += match[0].length;3826return match;3827}3828},3829current: function(){return this.string.slice(this.start, this.pos);},3830hideFirstChars: function(n, inner) {3831this.lineStart += n;3832try { return inner(); }3833finally { this.lineStart -= n; }3834}3835};3836CodeMirror.StringStream = StringStream;38373838// TEXTMARKERS38393840function TextMarker(doc, type) {3841this.lines = [];3842this.type = type;3843this.doc = doc;3844}3845CodeMirror.TextMarker = TextMarker;3846eventMixin(TextMarker);38473848TextMarker.prototype.clear = function() {3849if (this.explicitlyCleared) return;3850var cm = this.doc.cm, withOp = cm && !cm.curOp;3851if (withOp) startOperation(cm);3852if (hasHandler(this, "clear")) {3853var found = this.find();3854if (found) signalLater(this, "clear", found.from, found.to);3855}3856var min = null, max = null;3857for (var i = 0; i < this.lines.length; ++i) {3858var line = this.lines[i];3859var span = getMarkedSpanFor(line.markedSpans, this);3860if (span.to != null) max = lineNo(line);3861line.markedSpans = removeMarkedSpan(line.markedSpans, span);3862if (span.from != null)3863min = lineNo(line);3864else if (this.collapsed && !lineIsHidden(this.doc, line) && cm)3865updateLineHeight(line, textHeight(cm.display));3866}3867if (cm && this.collapsed && !cm.options.lineWrapping) for (var i = 0; i < this.lines.length; ++i) {3868var visual = visualLine(cm.doc, this.lines[i]), len = lineLength(cm.doc, visual);3869if (len > cm.display.maxLineLength) {3870cm.display.maxLine = visual;3871cm.display.maxLineLength = len;3872cm.display.maxLineChanged = true;3873}3874}38753876if (min != null && cm) regChange(cm, min, max + 1);3877this.lines.length = 0;3878this.explicitlyCleared = true;3879if (this.atomic && this.doc.cantEdit) {3880this.doc.cantEdit = false;3881if (cm) reCheckSelection(cm);3882}3883if (withOp) endOperation(cm);3884};38853886TextMarker.prototype.find = function(bothSides) {3887var from, to;3888for (var i = 0; i < this.lines.length; ++i) {3889var line = this.lines[i];3890var span = getMarkedSpanFor(line.markedSpans, this);3891if (span.from != null || span.to != null) {3892var found = lineNo(line);3893if (span.from != null) from = Pos(found, span.from);3894if (span.to != null) to = Pos(found, span.to);3895}3896}3897if (this.type == "bookmark" && !bothSides) return from;3898return from && {from: from, to: to};3899};39003901TextMarker.prototype.changed = function() {3902var pos = this.find(), cm = this.doc.cm;3903if (!pos || !cm) return;3904if (this.type != "bookmark") pos = pos.from;3905var line = getLine(this.doc, pos.line);3906clearCachedMeasurement(cm, line);3907if (pos.line >= cm.display.showingFrom && pos.line < cm.display.showingTo) {3908for (var node = cm.display.lineDiv.firstChild; node; node = node.nextSibling) if (node.lineObj == line) {3909if (node.offsetHeight != line.height) updateLineHeight(line, node.offsetHeight);3910break;3911}3912runInOp(cm, function() {3913cm.curOp.selectionChanged = cm.curOp.forceUpdate = cm.curOp.updateMaxLine = true;3914});3915}3916};39173918TextMarker.prototype.attachLine = function(line) {3919if (!this.lines.length && this.doc.cm) {3920var op = this.doc.cm.curOp;3921if (!op.maybeHiddenMarkers || indexOf(op.maybeHiddenMarkers, this) == -1)3922(op.maybeUnhiddenMarkers || (op.maybeUnhiddenMarkers = [])).push(this);3923}3924this.lines.push(line);3925};3926TextMarker.prototype.detachLine = function(line) {3927this.lines.splice(indexOf(this.lines, line), 1);3928if (!this.lines.length && this.doc.cm) {3929var op = this.doc.cm.curOp;3930(op.maybeHiddenMarkers || (op.maybeHiddenMarkers = [])).push(this);3931}3932};39333934var nextMarkerId = 0;39353936function markText(doc, from, to, options, type) {3937if (options && options.shared) return markTextShared(doc, from, to, options, type);3938if (doc.cm && !doc.cm.curOp) return operation(doc.cm, markText)(doc, from, to, options, type);39393940var marker = new TextMarker(doc, type);3941if (options) copyObj(options, marker);3942if (posLess(to, from) || posEq(from, to) && marker.clearWhenEmpty !== false)3943return marker;3944if (marker.replacedWith) {3945marker.collapsed = true;3946marker.replacedWith = elt("span", [marker.replacedWith], "CodeMirror-widget");3947if (!options.handleMouseEvents) marker.replacedWith.ignoreEvents = true;3948}3949if (marker.collapsed) {3950if (conflictingCollapsedRange(doc, from.line, from, to, marker) ||3951from.line != to.line && conflictingCollapsedRange(doc, to.line, from, to, marker))3952throw new Error("Inserting collapsed marker partially overlapping an existing one");3953sawCollapsedSpans = true;3954}39553956if (marker.addToHistory)3957addToHistory(doc, {from: from, to: to, origin: "markText"},3958{head: doc.sel.head, anchor: doc.sel.anchor}, NaN);39593960var curLine = from.line, cm = doc.cm, updateMaxLine;3961doc.iter(curLine, to.line + 1, function(line) {3962if (cm && marker.collapsed && !cm.options.lineWrapping && visualLine(doc, line) == cm.display.maxLine)3963updateMaxLine = true;3964var span = {from: null, to: null, marker: marker};3965if (curLine == from.line) span.from = from.ch;3966if (curLine == to.line) span.to = to.ch;3967if (marker.collapsed && curLine != from.line) updateLineHeight(line, 0);3968addMarkedSpan(line, span);3969++curLine;3970});3971if (marker.collapsed) doc.iter(from.line, to.line + 1, function(line) {3972if (lineIsHidden(doc, line)) updateLineHeight(line, 0);3973});39743975if (marker.clearOnEnter) on(marker, "beforeCursorEnter", function() { marker.clear(); });39763977if (marker.readOnly) {3978sawReadOnlySpans = true;3979if (doc.history.done.length || doc.history.undone.length)3980doc.clearHistory();3981}3982if (marker.collapsed) {3983marker.id = ++nextMarkerId;3984marker.atomic = true;3985}3986if (cm) {3987if (updateMaxLine) cm.curOp.updateMaxLine = true;3988if (marker.className || marker.title || marker.startStyle || marker.endStyle || marker.collapsed)3989regChange(cm, from.line, to.line + 1);3990if (marker.atomic) reCheckSelection(cm);3991}3992return marker;3993}39943995// SHARED TEXTMARKERS39963997function SharedTextMarker(markers, primary) {3998this.markers = markers;3999this.primary = primary;4000for (var i = 0, me = this; i < markers.length; ++i) {4001markers[i].parent = this;4002on(markers[i], "clear", function(){me.clear();});4003}4004}4005CodeMirror.SharedTextMarker = SharedTextMarker;4006eventMixin(SharedTextMarker);40074008SharedTextMarker.prototype.clear = function() {4009if (this.explicitlyCleared) return;4010this.explicitlyCleared = true;4011for (var i = 0; i < this.markers.length; ++i)4012this.markers[i].clear();4013signalLater(this, "clear");4014};4015SharedTextMarker.prototype.find = function() {4016return this.primary.find();4017};40184019function markTextShared(doc, from, to, options, type) {4020options = copyObj(options);4021options.shared = false;4022var markers = [markText(doc, from, to, options, type)], primary = markers[0];4023var widget = options.replacedWith;4024linkedDocs(doc, function(doc) {4025if (widget) options.replacedWith = widget.cloneNode(true);4026markers.push(markText(doc, clipPos(doc, from), clipPos(doc, to), options, type));4027for (var i = 0; i < doc.linked.length; ++i)4028if (doc.linked[i].isParent) return;4029primary = lst(markers);4030});4031return new SharedTextMarker(markers, primary);4032}40334034// TEXTMARKER SPANS40354036function getMarkedSpanFor(spans, marker) {4037if (spans) for (var i = 0; i < spans.length; ++i) {4038var span = spans[i];4039if (span.marker == marker) return span;4040}4041}4042function removeMarkedSpan(spans, span) {4043for (var r, i = 0; i < spans.length; ++i)4044if (spans[i] != span) (r || (r = [])).push(spans[i]);4045return r;4046}4047function addMarkedSpan(line, span) {4048line.markedSpans = line.markedSpans ? line.markedSpans.concat([span]) : [span];4049span.marker.attachLine(line);4050}40514052function markedSpansBefore(old, startCh, isInsert) {4053if (old) for (var i = 0, nw; i < old.length; ++i) {4054var span = old[i], marker = span.marker;4055var startsBefore = span.from == null || (marker.inclusiveLeft ? span.from <= startCh : span.from < startCh);4056if (startsBefore || span.from == startCh && marker.type == "bookmark" && (!isInsert || !span.marker.insertLeft)) {4057var endsAfter = span.to == null || (marker.inclusiveRight ? span.to >= startCh : span.to > startCh);4058(nw || (nw = [])).push({from: span.from,4059to: endsAfter ? null : span.to,4060marker: marker});4061}4062}4063return nw;4064}40654066function markedSpansAfter(old, endCh, isInsert) {4067if (old) for (var i = 0, nw; i < old.length; ++i) {4068var span = old[i], marker = span.marker;4069var endsAfter = span.to == null || (marker.inclusiveRight ? span.to >= endCh : span.to > endCh);4070if (endsAfter || span.from == endCh && marker.type == "bookmark" && (!isInsert || span.marker.insertLeft)) {4071var startsBefore = span.from == null || (marker.inclusiveLeft ? span.from <= endCh : span.from < endCh);4072(nw || (nw = [])).push({from: startsBefore ? null : span.from - endCh,4073to: span.to == null ? null : span.to - endCh,4074marker: marker});4075}4076}4077return nw;4078}40794080function stretchSpansOverChange(doc, change) {4081var oldFirst = isLine(doc, change.from.line) && getLine(doc, change.from.line).markedSpans;4082var oldLast = isLine(doc, change.to.line) && getLine(doc, change.to.line).markedSpans;4083if (!oldFirst && !oldLast) return null;40844085var startCh = change.from.ch, endCh = change.to.ch, isInsert = posEq(change.from, change.to);4086// Get the spans that 'stick out' on both sides4087var first = markedSpansBefore(oldFirst, startCh, isInsert);4088var last = markedSpansAfter(oldLast, endCh, isInsert);40894090// Next, merge those two ends4091var sameLine = change.text.length == 1, offset = lst(change.text).length + (sameLine ? startCh : 0);4092if (first) {4093// Fix up .to properties of first4094for (var i = 0; i < first.length; ++i) {4095var span = first[i];4096if (span.to == null) {4097var found = getMarkedSpanFor(last, span.marker);4098if (!found) span.to = startCh;4099else if (sameLine) span.to = found.to == null ? null : found.to + offset;4100}4101}4102}4103if (last) {4104// Fix up .from in last (or move them into first in case of sameLine)4105for (var i = 0; i < last.length; ++i) {4106var span = last[i];4107if (span.to != null) span.to += offset;4108if (span.from == null) {4109var found = getMarkedSpanFor(first, span.marker);4110if (!found) {4111span.from = offset;4112if (sameLine) (first || (first = [])).push(span);4113}4114} else {4115span.from += offset;4116if (sameLine) (first || (first = [])).push(span);4117}4118}4119}4120// Make sure we didn't create any zero-length spans4121if (first) first = clearEmptySpans(first);4122if (last && last != first) last = clearEmptySpans(last);41234124var newMarkers = [first];4125if (!sameLine) {4126// Fill gap with whole-line-spans4127var gap = change.text.length - 2, gapMarkers;4128if (gap > 0 && first)4129for (var i = 0; i < first.length; ++i)4130if (first[i].to == null)4131(gapMarkers || (gapMarkers = [])).push({from: null, to: null, marker: first[i].marker});4132for (var i = 0; i < gap; ++i)4133newMarkers.push(gapMarkers);4134newMarkers.push(last);4135}4136return newMarkers;4137}41384139function clearEmptySpans(spans) {4140for (var i = 0; i < spans.length; ++i) {4141var span = spans[i];4142if (span.from != null && span.from == span.to && span.marker.clearWhenEmpty !== false)4143spans.splice(i--, 1);4144}4145if (!spans.length) return null;4146return spans;4147}41484149function mergeOldSpans(doc, change) {4150var old = getOldSpans(doc, change);4151var stretched = stretchSpansOverChange(doc, change);4152if (!old) return stretched;4153if (!stretched) return old;41544155for (var i = 0; i < old.length; ++i) {4156var oldCur = old[i], stretchCur = stretched[i];4157if (oldCur && stretchCur) {4158spans: for (var j = 0; j < stretchCur.length; ++j) {4159var span = stretchCur[j];4160for (var k = 0; k < oldCur.length; ++k)4161if (oldCur[k].marker == span.marker) continue spans;4162oldCur.push(span);4163}4164} else if (stretchCur) {4165old[i] = stretchCur;4166}4167}4168return old;4169}41704171function removeReadOnlyRanges(doc, from, to) {4172var markers = null;4173doc.iter(from.line, to.line + 1, function(line) {4174if (line.markedSpans) for (var i = 0; i < line.markedSpans.length; ++i) {4175var mark = line.markedSpans[i].marker;4176if (mark.readOnly && (!markers || indexOf(markers, mark) == -1))4177(markers || (markers = [])).push(mark);4178}4179});4180if (!markers) return null;4181var parts = [{from: from, to: to}];4182for (var i = 0; i < markers.length; ++i) {4183var mk = markers[i], m = mk.find();4184for (var j = 0; j < parts.length; ++j) {4185var p = parts[j];4186if (posLess(p.to, m.from) || posLess(m.to, p.from)) continue;4187var newParts = [j, 1];4188if (posLess(p.from, m.from) || !mk.inclusiveLeft && posEq(p.from, m.from))4189newParts.push({from: p.from, to: m.from});4190if (posLess(m.to, p.to) || !mk.inclusiveRight && posEq(p.to, m.to))4191newParts.push({from: m.to, to: p.to});4192parts.splice.apply(parts, newParts);4193j += newParts.length - 1;4194}4195}4196return parts;4197}41984199function extraLeft(marker) { return marker.inclusiveLeft ? -1 : 0; }4200function extraRight(marker) { return marker.inclusiveRight ? 1 : 0; }42014202function compareCollapsedMarkers(a, b) {4203var lenDiff = a.lines.length - b.lines.length;4204if (lenDiff != 0) return lenDiff;4205var aPos = a.find(), bPos = b.find();4206var fromCmp = cmp(aPos.from, bPos.from) || extraLeft(a) - extraLeft(b);4207if (fromCmp) return -fromCmp;4208var toCmp = cmp(aPos.to, bPos.to) || extraRight(a) - extraRight(b);4209if (toCmp) return toCmp;4210return b.id - a.id;4211}42124213function collapsedSpanAtSide(line, start) {4214var sps = sawCollapsedSpans && line.markedSpans, found;4215if (sps) for (var sp, i = 0; i < sps.length; ++i) {4216sp = sps[i];4217if (sp.marker.collapsed && (start ? sp.from : sp.to) == null &&4218(!found || compareCollapsedMarkers(found, sp.marker) < 0))4219found = sp.marker;4220}4221return found;4222}4223function collapsedSpanAtStart(line) { return collapsedSpanAtSide(line, true); }4224function collapsedSpanAtEnd(line) { return collapsedSpanAtSide(line, false); }42254226function conflictingCollapsedRange(doc, lineNo, from, to, marker) {4227var line = getLine(doc, lineNo);4228var sps = sawCollapsedSpans && line.markedSpans;4229if (sps) for (var i = 0; i < sps.length; ++i) {4230var sp = sps[i];4231if (!sp.marker.collapsed) continue;4232var found = sp.marker.find(true);4233var fromCmp = cmp(found.from, from) || extraLeft(sp.marker) - extraLeft(marker);4234var toCmp = cmp(found.to, to) || extraRight(sp.marker) - extraRight(marker);4235if (fromCmp >= 0 && toCmp <= 0 || fromCmp <= 0 && toCmp >= 0) continue;4236if (fromCmp <= 0 && (cmp(found.to, from) || extraRight(sp.marker) - extraLeft(marker)) > 0 ||4237fromCmp >= 0 && (cmp(found.from, to) || extraLeft(sp.marker) - extraRight(marker)) < 0)4238return true;4239}4240}42414242function visualLine(doc, line) {4243var merged;4244while (merged = collapsedSpanAtStart(line))4245line = getLine(doc, merged.find().from.line);4246return line;4247}42484249function lineIsHidden(doc, line) {4250var sps = sawCollapsedSpans && line.markedSpans;4251if (sps) for (var sp, i = 0; i < sps.length; ++i) {4252sp = sps[i];4253if (!sp.marker.collapsed) continue;4254if (sp.from == null) return true;4255if (sp.marker.replacedWith) continue;4256if (sp.from == 0 && sp.marker.inclusiveLeft && lineIsHiddenInner(doc, line, sp))4257return true;4258}4259}4260function lineIsHiddenInner(doc, line, span) {4261if (span.to == null) {4262var end = span.marker.find().to, endLine = getLine(doc, end.line);4263return lineIsHiddenInner(doc, endLine, getMarkedSpanFor(endLine.markedSpans, span.marker));4264}4265if (span.marker.inclusiveRight && span.to == line.text.length)4266return true;4267for (var sp, i = 0; i < line.markedSpans.length; ++i) {4268sp = line.markedSpans[i];4269if (sp.marker.collapsed && !sp.marker.replacedWith && sp.from == span.to &&4270(sp.to == null || sp.to != span.from) &&4271(sp.marker.inclusiveLeft || span.marker.inclusiveRight) &&4272lineIsHiddenInner(doc, line, sp)) return true;4273}4274}42754276function detachMarkedSpans(line) {4277var spans = line.markedSpans;4278if (!spans) return;4279for (var i = 0; i < spans.length; ++i)4280spans[i].marker.detachLine(line);4281line.markedSpans = null;4282}42834284function attachMarkedSpans(line, spans) {4285if (!spans) return;4286for (var i = 0; i < spans.length; ++i)4287spans[i].marker.attachLine(line);4288line.markedSpans = spans;4289}42904291// LINE WIDGETS42924293var LineWidget = CodeMirror.LineWidget = function(cm, node, options) {4294if (options) for (var opt in options) if (options.hasOwnProperty(opt))4295this[opt] = options[opt];4296this.cm = cm;4297this.node = node;4298};4299eventMixin(LineWidget);4300function widgetOperation(f) {4301return function() {4302var withOp = !this.cm.curOp;4303if (withOp) startOperation(this.cm);4304try {var result = f.apply(this, arguments);}4305finally {if (withOp) endOperation(this.cm);}4306return result;4307};4308}4309LineWidget.prototype.clear = widgetOperation(function() {4310var ws = this.line.widgets, no = lineNo(this.line);4311if (no == null || !ws) return;4312for (var i = 0; i < ws.length; ++i) if (ws[i] == this) ws.splice(i--, 1);4313if (!ws.length) this.line.widgets = null;4314var aboveVisible = heightAtLine(this.cm, this.line) < this.cm.doc.scrollTop;4315updateLineHeight(this.line, Math.max(0, this.line.height - widgetHeight(this)));4316if (aboveVisible) addToScrollPos(this.cm, 0, -this.height);4317regChange(this.cm, no, no + 1);4318});4319LineWidget.prototype.changed = widgetOperation(function() {4320var oldH = this.height;4321this.height = null;4322var diff = widgetHeight(this) - oldH;4323if (!diff) return;4324updateLineHeight(this.line, this.line.height + diff);4325var no = lineNo(this.line);4326regChange(this.cm, no, no + 1);4327});43284329function widgetHeight(widget) {4330if (widget.height != null) return widget.height;4331if (!widget.node.parentNode || widget.node.parentNode.nodeType != 1)4332removeChildrenAndAdd(widget.cm.display.measure, elt("div", [widget.node], null, "position: relative"));4333return widget.height = widget.node.offsetHeight;4334}43354336function addLineWidget(cm, handle, node, options) {4337var widget = new LineWidget(cm, node, options);4338if (widget.noHScroll) cm.display.alignWidgets = true;4339changeLine(cm, handle, function(line) {4340var widgets = line.widgets || (line.widgets = []);4341if (widget.insertAt == null) widgets.push(widget);4342else widgets.splice(Math.min(widgets.length - 1, Math.max(0, widget.insertAt)), 0, widget);4343widget.line = line;4344if (!lineIsHidden(cm.doc, line) || widget.showIfHidden) {4345var aboveVisible = heightAtLine(cm, line) < cm.doc.scrollTop;4346updateLineHeight(line, line.height + widgetHeight(widget));4347if (aboveVisible) addToScrollPos(cm, 0, widget.height);4348}4349return true;4350});4351return widget;4352}43534354// LINE DATA STRUCTURE43554356// Line objects. These hold state related to a line, including4357// highlighting info (the styles array).4358var Line = CodeMirror.Line = function(text, markedSpans, estimateHeight) {4359this.text = text;4360attachMarkedSpans(this, markedSpans);4361this.height = estimateHeight ? estimateHeight(this) : 1;4362};4363eventMixin(Line);4364Line.prototype.lineNo = function() { return lineNo(this); };43654366function updateLine(line, text, markedSpans, estimateHeight) {4367line.text = text;4368if (line.stateAfter) line.stateAfter = null;4369if (line.styles) line.styles = null;4370if (line.order != null) line.order = null;4371detachMarkedSpans(line);4372attachMarkedSpans(line, markedSpans);4373var estHeight = estimateHeight ? estimateHeight(line) : 1;4374if (estHeight != line.height) updateLineHeight(line, estHeight);4375}43764377function cleanUpLine(line) {4378line.parent = null;4379detachMarkedSpans(line);4380}43814382// Run the given mode's parser over a line, update the styles4383// array, which contains alternating fragments of text and CSS4384// classes.4385function runMode(cm, text, mode, state, f, forceToEnd) {4386var flattenSpans = mode.flattenSpans;4387if (flattenSpans == null) flattenSpans = cm.options.flattenSpans;4388var curStart = 0, curStyle = null;4389var stream = new StringStream(text, cm.options.tabSize), style;4390if (text == "" && mode.blankLine) mode.blankLine(state);4391while (!stream.eol()) {4392if (stream.pos > cm.options.maxHighlightLength) {4393flattenSpans = false;4394if (forceToEnd) processLine(cm, text, state, stream.pos);4395stream.pos = text.length;4396style = null;4397} else {4398style = mode.token(stream, state);4399}4400if (cm.options.addModeClass) {4401var mName = CodeMirror.innerMode(mode, state).mode.name;4402if (mName) style = "m-" + (style ? mName + " " + style : mName);4403}4404if (!flattenSpans || curStyle != style) {4405if (curStart < stream.start) f(stream.start, curStyle);4406curStart = stream.start; curStyle = style;4407}4408stream.start = stream.pos;4409}4410while (curStart < stream.pos) {4411// Webkit seems to refuse to render text nodes longer than 57444 characters4412var pos = Math.min(stream.pos, curStart + 50000);4413f(pos, curStyle);4414curStart = pos;4415}4416}44174418function highlightLine(cm, line, state, forceToEnd) {4419// A styles array always starts with a number identifying the4420// mode/overlays that it is based on (for easy invalidation).4421var st = [cm.state.modeGen];4422// Compute the base array of styles4423runMode(cm, line.text, cm.doc.mode, state, function(end, style) {4424st.push(end, style);4425}, forceToEnd);44264427// Run overlays, adjust style array.4428for (var o = 0; o < cm.state.overlays.length; ++o) {4429var overlay = cm.state.overlays[o], i = 1, at = 0;4430runMode(cm, line.text, overlay.mode, true, function(end, style) {4431var start = i;4432// Ensure there's a token end at the current position, and that i points at it4433while (at < end) {4434var i_end = st[i];4435if (i_end > end)4436st.splice(i, 1, end, st[i+1], i_end);4437i += 2;4438at = Math.min(end, i_end);4439}4440if (!style) return;4441if (overlay.opaque) {4442st.splice(start, i - start, end, style);4443i = start + 2;4444} else {4445for (; start < i; start += 2) {4446var cur = st[start+1];4447st[start+1] = cur ? cur + " " + style : style;4448}4449}4450});4451}44524453return st;4454}44554456function getLineStyles(cm, line) {4457if (!line.styles || line.styles[0] != cm.state.modeGen)4458line.styles = highlightLine(cm, line, line.stateAfter = getStateBefore(cm, lineNo(line)));4459return line.styles;4460}44614462// Lightweight form of highlight -- proceed over this line and4463// update state, but don't save a style array.4464function processLine(cm, text, state, startAt) {4465var mode = cm.doc.mode;4466var stream = new StringStream(text, cm.options.tabSize);4467stream.start = stream.pos = startAt || 0;4468if (text == "" && mode.blankLine) mode.blankLine(state);4469while (!stream.eol() && stream.pos <= cm.options.maxHighlightLength) {4470mode.token(stream, state);4471stream.start = stream.pos;4472}4473}44744475var styleToClassCache = {}, styleToClassCacheWithMode = {};4476function interpretTokenStyle(style, builder) {4477if (!style) return null;4478for (;;) {4479var lineClass = style.match(/(?:^|\s)line-(background-)?(\S+)/);4480if (!lineClass) break;4481style = style.slice(0, lineClass.index) + style.slice(lineClass.index + lineClass[0].length);4482var prop = lineClass[1] ? "bgClass" : "textClass";4483if (builder[prop] == null)4484builder[prop] = lineClass[2];4485else if (!(new RegExp("(?:^|\s)" + lineClass[2] + "(?:$|\s)")).test(builder[prop]))4486builder[prop] += " " + lineClass[2];4487}4488var cache = builder.cm.options.addModeClass ? styleToClassCacheWithMode : styleToClassCache;4489return cache[style] ||4490(cache[style] = "cm-" + style.replace(/ +/g, " cm-"));4491}44924493function buildLineContent(cm, realLine, measure, copyWidgets) {4494var merged, line = realLine, empty = true;4495while (merged = collapsedSpanAtStart(line))4496line = getLine(cm.doc, merged.find().from.line);44974498var builder = {pre: elt("pre"), col: 0, pos: 0,4499measure: null, measuredSomething: false, cm: cm,4500copyWidgets: copyWidgets};45014502do {4503if (line.text) empty = false;4504builder.measure = line == realLine && measure;4505builder.pos = 0;4506builder.addToken = builder.measure ? buildTokenMeasure : buildToken;4507if ((old_ie || webkit) && cm.getOption("lineWrapping"))4508builder.addToken = buildTokenSplitSpaces(builder.addToken);4509var next = insertLineContent(line, builder, getLineStyles(cm, line));4510if (measure && line == realLine && !builder.measuredSomething) {4511measure[0] = builder.pre.appendChild(zeroWidthElement(cm.display.measure));4512builder.measuredSomething = true;4513}4514if (next) line = getLine(cm.doc, next.to.line);4515} while (next);45164517if (measure && !builder.measuredSomething && !measure[0])4518measure[0] = builder.pre.appendChild(empty ? elt("span", "\u00a0") : zeroWidthElement(cm.display.measure));4519if (!builder.pre.firstChild && !lineIsHidden(cm.doc, realLine))4520builder.pre.appendChild(document.createTextNode("\u00a0"));45214522var order;4523// Work around problem with the reported dimensions of single-char4524// direction spans on IE (issue #1129). See also the comment in4525// cursorCoords.4526if (measure && ie && (order = getOrder(line))) {4527var l = order.length - 1;4528if (order[l].from == order[l].to) --l;4529var last = order[l], prev = order[l - 1];4530if (last.from + 1 == last.to && prev && last.level < prev.level) {4531var span = measure[builder.pos - 1];4532if (span) span.parentNode.insertBefore(span.measureRight = zeroWidthElement(cm.display.measure),4533span.nextSibling);4534}4535}45364537var textClass = builder.textClass ? builder.textClass + " " + (realLine.textClass || "") : realLine.textClass;4538if (textClass) builder.pre.className = textClass;45394540signal(cm, "renderLine", cm, realLine, builder.pre);4541return builder;4542}45434544function defaultSpecialCharPlaceholder(ch) {4545var token = elt("span", "\u2022", "cm-invalidchar");4546token.title = "\\u" + ch.charCodeAt(0).toString(16);4547return token;4548}45494550function buildToken(builder, text, style, startStyle, endStyle, title) {4551if (!text) return;4552var special = builder.cm.options.specialChars;4553if (!special.test(text)) {4554builder.col += text.length;4555var content = document.createTextNode(text);4556} else {4557var content = document.createDocumentFragment(), pos = 0;4558while (true) {4559special.lastIndex = pos;4560var m = special.exec(text);4561var skipped = m ? m.index - pos : text.length - pos;4562if (skipped) {4563content.appendChild(document.createTextNode(text.slice(pos, pos + skipped)));4564builder.col += skipped;4565}4566if (!m) break;4567pos += skipped + 1;4568if (m[0] == "\t") {4569var tabSize = builder.cm.options.tabSize, tabWidth = tabSize - builder.col % tabSize;4570content.appendChild(elt("span", spaceStr(tabWidth), "cm-tab"));4571builder.col += tabWidth;4572} else {4573var token = builder.cm.options.specialCharPlaceholder(m[0]);4574content.appendChild(token);4575builder.col += 1;4576}4577}4578}4579if (style || startStyle || endStyle || builder.measure) {4580var fullStyle = style || "";4581if (startStyle) fullStyle += startStyle;4582if (endStyle) fullStyle += endStyle;4583var token = elt("span", [content], fullStyle);4584if (title) token.title = title;4585return builder.pre.appendChild(token);4586}4587builder.pre.appendChild(content);4588}45894590function buildTokenMeasure(builder, text, style, startStyle, endStyle) {4591var wrapping = builder.cm.options.lineWrapping;4592for (var i = 0; i < text.length; ++i) {4593var start = i == 0, to = i + 1;4594while (to < text.length && isExtendingChar(text.charAt(to))) ++to;4595var ch = text.slice(i, to);4596i = to - 1;4597if (i && wrapping && spanAffectsWrapping(text, i))4598builder.pre.appendChild(elt("wbr"));4599var old = builder.measure[builder.pos];4600var span = builder.measure[builder.pos] =4601buildToken(builder, ch, style,4602start && startStyle, i == text.length - 1 && endStyle);4603if (old) span.leftSide = old.leftSide || old;4604// In IE single-space nodes wrap differently than spaces4605// embedded in larger text nodes, except when set to4606// white-space: normal (issue #1268).4607if (old_ie && wrapping && ch == " " && i && !/\s/.test(text.charAt(i - 1)) &&4608i < text.length - 1 && !/\s/.test(text.charAt(i + 1)))4609span.style.whiteSpace = "normal";4610builder.pos += ch.length;4611}4612if (text.length) builder.measuredSomething = true;4613}46144615function buildTokenSplitSpaces(inner) {4616function split(old) {4617var out = " ";4618for (var i = 0; i < old.length - 2; ++i) out += i % 2 ? " " : "\u00a0";4619out += " ";4620return out;4621}4622return function(builder, text, style, startStyle, endStyle, title) {4623return inner(builder, text.replace(/ {3,}/g, split), style, startStyle, endStyle, title);4624};4625}46264627function buildCollapsedSpan(builder, size, marker, ignoreWidget) {4628var widget = !ignoreWidget && marker.replacedWith;4629if (widget) {4630if (builder.copyWidgets) widget = widget.cloneNode(true);4631builder.pre.appendChild(widget);4632if (builder.measure) {4633if (size) {4634builder.measure[builder.pos] = widget;4635} else {4636var elt = zeroWidthElement(builder.cm.display.measure);4637if (marker.type == "bookmark" && !marker.insertLeft)4638builder.measure[builder.pos] = builder.pre.appendChild(elt);4639else if (builder.measure[builder.pos])4640return;4641else4642builder.measure[builder.pos] = builder.pre.insertBefore(elt, widget);4643}4644builder.measuredSomething = true;4645}4646}4647builder.pos += size;4648}46494650// Outputs a number of spans to make up a line, taking highlighting4651// and marked text into account.4652function insertLineContent(line, builder, styles) {4653var spans = line.markedSpans, allText = line.text, at = 0;4654if (!spans) {4655for (var i = 1; i < styles.length; i+=2)4656builder.addToken(builder, allText.slice(at, at = styles[i]), interpretTokenStyle(styles[i+1], builder));4657return;4658}46594660var len = allText.length, pos = 0, i = 1, text = "", style;4661var nextChange = 0, spanStyle, spanEndStyle, spanStartStyle, title, collapsed;4662for (;;) {4663if (nextChange == pos) { // Update current marker set4664spanStyle = spanEndStyle = spanStartStyle = title = "";4665collapsed = null; nextChange = Infinity;4666var foundBookmarks = [];4667for (var j = 0; j < spans.length; ++j) {4668var sp = spans[j], m = sp.marker;4669if (sp.from <= pos && (sp.to == null || sp.to > pos)) {4670if (sp.to != null && nextChange > sp.to) { nextChange = sp.to; spanEndStyle = ""; }4671if (m.className) spanStyle += " " + m.className;4672if (m.startStyle && sp.from == pos) spanStartStyle += " " + m.startStyle;4673if (m.endStyle && sp.to == nextChange) spanEndStyle += " " + m.endStyle;4674if (m.title && !title) title = m.title;4675if (m.collapsed && (!collapsed || compareCollapsedMarkers(collapsed.marker, m) < 0))4676collapsed = sp;4677} else if (sp.from > pos && nextChange > sp.from) {4678nextChange = sp.from;4679}4680if (m.type == "bookmark" && sp.from == pos && m.replacedWith) foundBookmarks.push(m);4681}4682if (collapsed && (collapsed.from || 0) == pos) {4683buildCollapsedSpan(builder, (collapsed.to == null ? len : collapsed.to) - pos,4684collapsed.marker, collapsed.from == null);4685if (collapsed.to == null) return collapsed.marker.find();4686}4687if (!collapsed && foundBookmarks.length) for (var j = 0; j < foundBookmarks.length; ++j)4688buildCollapsedSpan(builder, 0, foundBookmarks[j]);4689}4690if (pos >= len) break;46914692var upto = Math.min(len, nextChange);4693while (true) {4694if (text) {4695var end = pos + text.length;4696if (!collapsed) {4697var tokenText = end > upto ? text.slice(0, upto - pos) : text;4698builder.addToken(builder, tokenText, style ? style + spanStyle : spanStyle,4699spanStartStyle, pos + tokenText.length == nextChange ? spanEndStyle : "", title);4700}4701if (end >= upto) {text = text.slice(upto - pos); pos = upto; break;}4702pos = end;4703spanStartStyle = "";4704}4705text = allText.slice(at, at = styles[i++]);4706style = interpretTokenStyle(styles[i++], builder);4707}4708}4709}47104711// DOCUMENT DATA STRUCTURE47124713function updateDoc(doc, change, markedSpans, selAfter, estimateHeight) {4714function spansFor(n) {return markedSpans ? markedSpans[n] : null;}4715function update(line, text, spans) {4716updateLine(line, text, spans, estimateHeight);4717signalLater(line, "change", line, change);4718}47194720var from = change.from, to = change.to, text = change.text;4721var firstLine = getLine(doc, from.line), lastLine = getLine(doc, to.line);4722var lastText = lst(text), lastSpans = spansFor(text.length - 1), nlines = to.line - from.line;47234724// First adjust the line structure4725if (from.ch == 0 && to.ch == 0 && lastText == "" &&4726(!doc.cm || doc.cm.options.wholeLineUpdateBefore)) {4727// This is a whole-line replace. Treated specially to make4728// sure line objects move the way they are supposed to.4729for (var i = 0, e = text.length - 1, added = []; i < e; ++i)4730added.push(new Line(text[i], spansFor(i), estimateHeight));4731update(lastLine, lastLine.text, lastSpans);4732if (nlines) doc.remove(from.line, nlines);4733if (added.length) doc.insert(from.line, added);4734} else if (firstLine == lastLine) {4735if (text.length == 1) {4736update(firstLine, firstLine.text.slice(0, from.ch) + lastText + firstLine.text.slice(to.ch), lastSpans);4737} else {4738for (var added = [], i = 1, e = text.length - 1; i < e; ++i)4739added.push(new Line(text[i], spansFor(i), estimateHeight));4740added.push(new Line(lastText + firstLine.text.slice(to.ch), lastSpans, estimateHeight));4741update(firstLine, firstLine.text.slice(0, from.ch) + text[0], spansFor(0));4742doc.insert(from.line + 1, added);4743}4744} else if (text.length == 1) {4745update(firstLine, firstLine.text.slice(0, from.ch) + text[0] + lastLine.text.slice(to.ch), spansFor(0));4746doc.remove(from.line + 1, nlines);4747} else {4748update(firstLine, firstLine.text.slice(0, from.ch) + text[0], spansFor(0));4749update(lastLine, lastText + lastLine.text.slice(to.ch), lastSpans);4750for (var i = 1, e = text.length - 1, added = []; i < e; ++i)4751added.push(new Line(text[i], spansFor(i), estimateHeight));4752if (nlines > 1) doc.remove(from.line + 1, nlines - 1);4753doc.insert(from.line + 1, added);4754}47554756signalLater(doc, "change", doc, change);4757setSelection(doc, selAfter.anchor, selAfter.head, null, true);4758}47594760function LeafChunk(lines) {4761this.lines = lines;4762this.parent = null;4763for (var i = 0, e = lines.length, height = 0; i < e; ++i) {4764lines[i].parent = this;4765height += lines[i].height;4766}4767this.height = height;4768}47694770LeafChunk.prototype = {4771chunkSize: function() { return this.lines.length; },4772removeInner: function(at, n) {4773for (var i = at, e = at + n; i < e; ++i) {4774var line = this.lines[i];4775this.height -= line.height;4776cleanUpLine(line);4777signalLater(line, "delete");4778}4779this.lines.splice(at, n);4780},4781collapse: function(lines) {4782lines.splice.apply(lines, [lines.length, 0].concat(this.lines));4783},4784insertInner: function(at, lines, height) {4785this.height += height;4786this.lines = this.lines.slice(0, at).concat(lines).concat(this.lines.slice(at));4787for (var i = 0, e = lines.length; i < e; ++i) lines[i].parent = this;4788},4789iterN: function(at, n, op) {4790for (var e = at + n; at < e; ++at)4791if (op(this.lines[at])) return true;4792}4793};47944795function BranchChunk(children) {4796this.children = children;4797var size = 0, height = 0;4798for (var i = 0, e = children.length; i < e; ++i) {4799var ch = children[i];4800size += ch.chunkSize(); height += ch.height;4801ch.parent = this;4802}4803this.size = size;4804this.height = height;4805this.parent = null;4806}48074808BranchChunk.prototype = {4809chunkSize: function() { return this.size; },4810removeInner: function(at, n) {4811this.size -= n;4812for (var i = 0; i < this.children.length; ++i) {4813var child = this.children[i], sz = child.chunkSize();4814if (at < sz) {4815var rm = Math.min(n, sz - at), oldHeight = child.height;4816child.removeInner(at, rm);4817this.height -= oldHeight - child.height;4818if (sz == rm) { this.children.splice(i--, 1); child.parent = null; }4819if ((n -= rm) == 0) break;4820at = 0;4821} else at -= sz;4822}4823if (this.size - n < 25) {4824var lines = [];4825this.collapse(lines);4826this.children = [new LeafChunk(lines)];4827this.children[0].parent = this;4828}4829},4830collapse: function(lines) {4831for (var i = 0, e = this.children.length; i < e; ++i) this.children[i].collapse(lines);4832},4833insertInner: function(at, lines, height) {4834this.size += lines.length;4835this.height += height;4836for (var i = 0, e = this.children.length; i < e; ++i) {4837var child = this.children[i], sz = child.chunkSize();4838if (at <= sz) {4839child.insertInner(at, lines, height);4840if (child.lines && child.lines.length > 50) {4841while (child.lines.length > 50) {4842var spilled = child.lines.splice(child.lines.length - 25, 25);4843var newleaf = new LeafChunk(spilled);4844child.height -= newleaf.height;4845this.children.splice(i + 1, 0, newleaf);4846newleaf.parent = this;4847}4848this.maybeSpill();4849}4850break;4851}4852at -= sz;4853}4854},4855maybeSpill: function() {4856if (this.children.length <= 10) return;4857var me = this;4858do {4859var spilled = me.children.splice(me.children.length - 5, 5);4860var sibling = new BranchChunk(spilled);4861if (!me.parent) { // Become the parent node4862var copy = new BranchChunk(me.children);4863copy.parent = me;4864me.children = [copy, sibling];4865me = copy;4866} else {4867me.size -= sibling.size;4868me.height -= sibling.height;4869var myIndex = indexOf(me.parent.children, me);4870me.parent.children.splice(myIndex + 1, 0, sibling);4871}4872sibling.parent = me.parent;4873} while (me.children.length > 10);4874me.parent.maybeSpill();4875},4876iterN: function(at, n, op) {4877for (var i = 0, e = this.children.length; i < e; ++i) {4878var child = this.children[i], sz = child.chunkSize();4879if (at < sz) {4880var used = Math.min(n, sz - at);4881if (child.iterN(at, used, op)) return true;4882if ((n -= used) == 0) break;4883at = 0;4884} else at -= sz;4885}4886}4887};48884889var nextDocId = 0;4890var Doc = CodeMirror.Doc = function(text, mode, firstLine) {4891if (!(this instanceof Doc)) return new Doc(text, mode, firstLine);4892if (firstLine == null) firstLine = 0;48934894BranchChunk.call(this, [new LeafChunk([new Line("", null)])]);4895this.first = firstLine;4896this.scrollTop = this.scrollLeft = 0;4897this.cantEdit = false;4898this.history = makeHistory();4899this.cleanGeneration = 1;4900this.frontier = firstLine;4901var start = Pos(firstLine, 0);4902this.sel = {from: start, to: start, head: start, anchor: start, shift: false, extend: false, goalColumn: null};4903this.id = ++nextDocId;4904this.modeOption = mode;49054906if (typeof text == "string") text = splitLines(text);4907updateDoc(this, {from: start, to: start, text: text}, null, {head: start, anchor: start});4908};49094910Doc.prototype = createObj(BranchChunk.prototype, {4911constructor: Doc,4912iter: function(from, to, op) {4913if (op) this.iterN(from - this.first, to - from, op);4914else this.iterN(this.first, this.first + this.size, from);4915},49164917insert: function(at, lines) {4918var height = 0;4919for (var i = 0, e = lines.length; i < e; ++i) height += lines[i].height;4920this.insertInner(at - this.first, lines, height);4921},4922remove: function(at, n) { this.removeInner(at - this.first, n); },49234924getValue: function(lineSep) {4925var lines = getLines(this, this.first, this.first + this.size);4926if (lineSep === false) return lines;4927return lines.join(lineSep || "\n");4928},4929setValue: function(code) {4930var top = Pos(this.first, 0), last = this.first + this.size - 1;4931makeChange(this, {from: top, to: Pos(last, getLine(this, last).text.length),4932text: splitLines(code), origin: "setValue"},4933{head: top, anchor: top}, true);4934},4935replaceRange: function(code, from, to, origin) {4936from = clipPos(this, from);4937to = to ? clipPos(this, to) : from;4938replaceRange(this, code, from, to, origin);4939},4940getRange: function(from, to, lineSep) {4941var lines = getBetween(this, clipPos(this, from), clipPos(this, to));4942if (lineSep === false) return lines;4943return lines.join(lineSep || "\n");4944},49454946getLine: function(line) {var l = this.getLineHandle(line); return l && l.text;},4947setLine: function(line, text) {4948if (isLine(this, line))4949replaceRange(this, text, Pos(line, 0), clipPos(this, Pos(line)));4950},4951removeLine: function(line) {4952if (line) replaceRange(this, "", clipPos(this, Pos(line - 1)), clipPos(this, Pos(line)));4953else replaceRange(this, "", Pos(0, 0), clipPos(this, Pos(1, 0)));4954},49554956getLineHandle: function(line) {if (isLine(this, line)) return getLine(this, line);},4957getLineNumber: function(line) {return lineNo(line);},49584959getLineHandleVisualStart: function(line) {4960if (typeof line == "number") line = getLine(this, line);4961return visualLine(this, line);4962},49634964lineCount: function() {return this.size;},4965firstLine: function() {return this.first;},4966lastLine: function() {return this.first + this.size - 1;},49674968clipPos: function(pos) {return clipPos(this, pos);},49694970getCursor: function(start) {4971var sel = this.sel, pos;4972if (start == null || start == "head") pos = sel.head;4973else if (start == "anchor") pos = sel.anchor;4974else if (start == "end" || start === false) pos = sel.to;4975else pos = sel.from;4976return copyPos(pos);4977},4978somethingSelected: function() {return !posEq(this.sel.head, this.sel.anchor);},49794980setCursor: docOperation(function(line, ch, extend) {4981var pos = clipPos(this, typeof line == "number" ? Pos(line, ch || 0) : line);4982if (extend) extendSelection(this, pos);4983else setSelection(this, pos, pos);4984}),4985setSelection: docOperation(function(anchor, head, bias) {4986setSelection(this, clipPos(this, anchor), clipPos(this, head || anchor), bias);4987}),4988extendSelection: docOperation(function(from, to, bias) {4989extendSelection(this, clipPos(this, from), to && clipPos(this, to), bias);4990}),49914992getSelection: function(lineSep) {return this.getRange(this.sel.from, this.sel.to, lineSep);},4993replaceSelection: function(code, collapse, origin) {4994makeChange(this, {from: this.sel.from, to: this.sel.to, text: splitLines(code), origin: origin}, collapse || "around");4995},4996undo: docOperation(function() {makeChangeFromHistory(this, "undo");}),4997redo: docOperation(function() {makeChangeFromHistory(this, "redo");}),49984999setExtending: function(val) {this.sel.extend = val;},50005001historySize: function() {5002var hist = this.history;5003return {undo: hist.done.length, redo: hist.undone.length};5004},5005clearHistory: function() {this.history = makeHistory(this.history.maxGeneration);},50065007markClean: function() {5008this.cleanGeneration = this.changeGeneration(true);5009},5010changeGeneration: function(forceSplit) {5011if (forceSplit)5012this.history.lastOp = this.history.lastOrigin = null;5013return this.history.generation;5014},5015isClean: function (gen) {5016return this.history.generation == (gen || this.cleanGeneration);5017},50185019getHistory: function() {5020return {done: copyHistoryArray(this.history.done),5021undone: copyHistoryArray(this.history.undone)};5022},5023setHistory: function(histData) {5024var hist = this.history = makeHistory(this.history.maxGeneration);5025hist.done = histData.done.slice(0);5026hist.undone = histData.undone.slice(0);5027},50285029markText: function(from, to, options) {5030return markText(this, clipPos(this, from), clipPos(this, to), options, "range");5031},5032setBookmark: function(pos, options) {5033var realOpts = {replacedWith: options && (options.nodeType == null ? options.widget : options),5034insertLeft: options && options.insertLeft,5035clearWhenEmpty: false};5036pos = clipPos(this, pos);5037return markText(this, pos, pos, realOpts, "bookmark");5038},5039findMarksAt: function(pos) {5040pos = clipPos(this, pos);5041var markers = [], spans = getLine(this, pos.line).markedSpans;5042if (spans) for (var i = 0; i < spans.length; ++i) {5043var span = spans[i];5044if ((span.from == null || span.from <= pos.ch) &&5045(span.to == null || span.to >= pos.ch))5046markers.push(span.marker.parent || span.marker);5047}5048return markers;5049},5050getAllMarks: function() {5051var markers = [];5052this.iter(function(line) {5053var sps = line.markedSpans;5054if (sps) for (var i = 0; i < sps.length; ++i)5055if (sps[i].from != null) markers.push(sps[i].marker);5056});5057return markers;5058},50595060posFromIndex: function(off) {5061var ch, lineNo = this.first;5062this.iter(function(line) {5063var sz = line.text.length + 1;5064if (sz > off) { ch = off; return true; }5065off -= sz;5066++lineNo;5067});5068return clipPos(this, Pos(lineNo, ch));5069},5070indexFromPos: function (coords) {5071coords = clipPos(this, coords);5072var index = coords.ch;5073if (coords.line < this.first || coords.ch < 0) return 0;5074this.iter(this.first, coords.line, function (line) {5075index += line.text.length + 1;5076});5077return index;5078},50795080copy: function(copyHistory) {5081var doc = new Doc(getLines(this, this.first, this.first + this.size), this.modeOption, this.first);5082doc.scrollTop = this.scrollTop; doc.scrollLeft = this.scrollLeft;5083doc.sel = {from: this.sel.from, to: this.sel.to, head: this.sel.head, anchor: this.sel.anchor,5084shift: this.sel.shift, extend: false, goalColumn: this.sel.goalColumn};5085if (copyHistory) {5086doc.history.undoDepth = this.history.undoDepth;5087doc.setHistory(this.getHistory());5088}5089return doc;5090},50915092linkedDoc: function(options) {5093if (!options) options = {};5094var from = this.first, to = this.first + this.size;5095if (options.from != null && options.from > from) from = options.from;5096if (options.to != null && options.to < to) to = options.to;5097var copy = new Doc(getLines(this, from, to), options.mode || this.modeOption, from);5098if (options.sharedHist) copy.history = this.history;5099(this.linked || (this.linked = [])).push({doc: copy, sharedHist: options.sharedHist});5100copy.linked = [{doc: this, isParent: true, sharedHist: options.sharedHist}];5101return copy;5102},5103unlinkDoc: function(other) {5104if (other instanceof CodeMirror) other = other.doc;5105if (this.linked) for (var i = 0; i < this.linked.length; ++i) {5106var link = this.linked[i];5107if (link.doc != other) continue;5108this.linked.splice(i, 1);5109other.unlinkDoc(this);5110break;5111}5112// If the histories were shared, split them again5113if (other.history == this.history) {5114var splitIds = [other.id];5115linkedDocs(other, function(doc) {splitIds.push(doc.id);}, true);5116other.history = makeHistory();5117other.history.done = copyHistoryArray(this.history.done, splitIds);5118other.history.undone = copyHistoryArray(this.history.undone, splitIds);5119}5120},5121iterLinkedDocs: function(f) {linkedDocs(this, f);},51225123getMode: function() {return this.mode;},5124getEditor: function() {return this.cm;}5125});51265127Doc.prototype.eachLine = Doc.prototype.iter;51285129// The Doc methods that should be available on CodeMirror instances5130var dontDelegate = "iter insert remove copy getEditor".split(" ");5131for (var prop in Doc.prototype) if (Doc.prototype.hasOwnProperty(prop) && indexOf(dontDelegate, prop) < 0)5132CodeMirror.prototype[prop] = (function(method) {5133return function() {return method.apply(this.doc, arguments);};5134})(Doc.prototype[prop]);51355136eventMixin(Doc);51375138function linkedDocs(doc, f, sharedHistOnly) {5139function propagate(doc, skip, sharedHist) {5140if (doc.linked) for (var i = 0; i < doc.linked.length; ++i) {5141var rel = doc.linked[i];5142if (rel.doc == skip) continue;5143var shared = sharedHist && rel.sharedHist;5144if (sharedHistOnly && !shared) continue;5145f(rel.doc, shared);5146propagate(rel.doc, doc, shared);5147}5148}5149propagate(doc, null, true);5150}51515152function attachDoc(cm, doc) {5153if (doc.cm) throw new Error("This document is already in use.");5154cm.doc = doc;5155doc.cm = cm;5156estimateLineHeights(cm);5157loadMode(cm);5158if (!cm.options.lineWrapping) computeMaxLength(cm);5159cm.options.mode = doc.modeOption;5160regChange(cm);5161}51625163// LINE UTILITIES51645165function getLine(chunk, n) {5166n -= chunk.first;5167while (!chunk.lines) {5168for (var i = 0;; ++i) {5169var child = chunk.children[i], sz = child.chunkSize();5170if (n < sz) { chunk = child; break; }5171n -= sz;5172}5173}5174return chunk.lines[n];5175}51765177function getBetween(doc, start, end) {5178var out = [], n = start.line;5179doc.iter(start.line, end.line + 1, function(line) {5180var text = line.text;5181if (n == end.line) text = text.slice(0, end.ch);5182if (n == start.line) text = text.slice(start.ch);5183out.push(text);5184++n;5185});5186return out;5187}5188function getLines(doc, from, to) {5189var out = [];5190doc.iter(from, to, function(line) { out.push(line.text); });5191return out;5192}51935194function updateLineHeight(line, height) {5195var diff = height - line.height;5196for (var n = line; n; n = n.parent) n.height += diff;5197}51985199function lineNo(line) {5200if (line.parent == null) return null;5201var cur = line.parent, no = indexOf(cur.lines, line);5202for (var chunk = cur.parent; chunk; cur = chunk, chunk = chunk.parent) {5203for (var i = 0;; ++i) {5204if (chunk.children[i] == cur) break;5205no += chunk.children[i].chunkSize();5206}5207}5208return no + cur.first;5209}52105211function lineAtHeight(chunk, h) {5212var n = chunk.first;5213outer: do {5214for (var i = 0, e = chunk.children.length; i < e; ++i) {5215var child = chunk.children[i], ch = child.height;5216if (h < ch) { chunk = child; continue outer; }5217h -= ch;5218n += child.chunkSize();5219}5220return n;5221} while (!chunk.lines);5222for (var i = 0, e = chunk.lines.length; i < e; ++i) {5223var line = chunk.lines[i], lh = line.height;5224if (h < lh) break;5225h -= lh;5226}5227return n + i;5228}52295230function heightAtLine(cm, lineObj) {5231lineObj = visualLine(cm.doc, lineObj);52325233var h = 0, chunk = lineObj.parent;5234for (var i = 0; i < chunk.lines.length; ++i) {5235var line = chunk.lines[i];5236if (line == lineObj) break;5237else h += line.height;5238}5239for (var p = chunk.parent; p; chunk = p, p = chunk.parent) {5240for (var i = 0; i < p.children.length; ++i) {5241var cur = p.children[i];5242if (cur == chunk) break;5243else h += cur.height;5244}5245}5246return h;5247}52485249function getOrder(line) {5250var order = line.order;5251if (order == null) order = line.order = bidiOrdering(line.text);5252return order;5253}52545255// HISTORY52565257function makeHistory(startGen) {5258return {5259// Arrays of history events. Doing something adds an event to5260// done and clears undo. Undoing moves events from done to5261// undone, redoing moves them in the other direction.5262done: [], undone: [], undoDepth: Infinity,5263// Used to track when changes can be merged into a single undo5264// event5265lastTime: 0, lastOp: null, lastOrigin: null,5266// Used by the isClean() method5267generation: startGen || 1, maxGeneration: startGen || 15268};5269}52705271function attachLocalSpans(doc, change, from, to) {5272var existing = change["spans_" + doc.id], n = 0;5273doc.iter(Math.max(doc.first, from), Math.min(doc.first + doc.size, to), function(line) {5274if (line.markedSpans)5275(existing || (existing = change["spans_" + doc.id] = {}))[n] = line.markedSpans;5276++n;5277});5278}52795280function historyChangeFromChange(doc, change) {5281var from = { line: change.from.line, ch: change.from.ch };5282var histChange = {from: from, to: changeEnd(change), text: getBetween(doc, change.from, change.to)};5283attachLocalSpans(doc, histChange, change.from.line, change.to.line + 1);5284linkedDocs(doc, function(doc) {attachLocalSpans(doc, histChange, change.from.line, change.to.line + 1);}, true);5285return histChange;5286}52875288function addToHistory(doc, change, selAfter, opId) {5289var hist = doc.history;5290hist.undone.length = 0;5291var time = +new Date, cur = lst(hist.done);52925293if (cur &&5294(hist.lastOp == opId ||5295hist.lastOrigin == change.origin && change.origin &&5296((change.origin.charAt(0) == "+" && doc.cm && hist.lastTime > time - doc.cm.options.historyEventDelay) ||5297change.origin.charAt(0) == "*"))) {5298// Merge this change into the last event5299var last = lst(cur.changes);5300if (posEq(change.from, change.to) && posEq(change.from, last.to)) {5301// Optimized case for simple insertion -- don't want to add5302// new changesets for every character typed5303last.to = changeEnd(change);5304} else {5305// Add new sub-event5306cur.changes.push(historyChangeFromChange(doc, change));5307}5308cur.anchorAfter = selAfter.anchor; cur.headAfter = selAfter.head;5309} else {5310// Can not be merged, start a new event.5311cur = {changes: [historyChangeFromChange(doc, change)],5312generation: hist.generation,5313anchorBefore: doc.sel.anchor, headBefore: doc.sel.head,5314anchorAfter: selAfter.anchor, headAfter: selAfter.head};5315hist.done.push(cur);5316while (hist.done.length > hist.undoDepth)5317hist.done.shift();5318}5319hist.generation = ++hist.maxGeneration;5320hist.lastTime = time;5321hist.lastOp = opId;5322hist.lastOrigin = change.origin;5323}53245325function removeClearedSpans(spans) {5326if (!spans) return null;5327for (var i = 0, out; i < spans.length; ++i) {5328if (spans[i].marker.explicitlyCleared) { if (!out) out = spans.slice(0, i); }5329else if (out) out.push(spans[i]);5330}5331return !out ? spans : out.length ? out : null;5332}53335334function getOldSpans(doc, change) {5335var found = change["spans_" + doc.id];5336if (!found) return null;5337for (var i = 0, nw = []; i < change.text.length; ++i)5338nw.push(removeClearedSpans(found[i]));5339return nw;5340}53415342// Used both to provide a JSON-safe object in .getHistory, and, when5343// detaching a document, to split the history in two5344function copyHistoryArray(events, newGroup) {5345for (var i = 0, copy = []; i < events.length; ++i) {5346var event = events[i], changes = event.changes, newChanges = [];5347copy.push({changes: newChanges, anchorBefore: event.anchorBefore, headBefore: event.headBefore,5348anchorAfter: event.anchorAfter, headAfter: event.headAfter});5349for (var j = 0; j < changes.length; ++j) {5350var change = changes[j], m;5351newChanges.push({from: change.from, to: change.to, text: change.text});5352if (newGroup) for (var prop in change) if (m = prop.match(/^spans_(\d+)$/)) {5353if (indexOf(newGroup, Number(m[1])) > -1) {5354lst(newChanges)[prop] = change[prop];5355delete change[prop];5356}5357}5358}5359}5360return copy;5361}53625363// Rebasing/resetting history to deal with externally-sourced changes53645365function rebaseHistSel(pos, from, to, diff) {5366if (to < pos.line) {5367pos.line += diff;5368} else if (from < pos.line) {5369pos.line = from;5370pos.ch = 0;5371}5372}53735374// Tries to rebase an array of history events given a change in the5375// document. If the change touches the same lines as the event, the5376// event, and everything 'behind' it, is discarded. If the change is5377// before the event, the event's positions are updated. Uses a5378// copy-on-write scheme for the positions, to avoid having to5379// reallocate them all on every rebase, but also avoid problems with5380// shared position objects being unsafely updated.5381function rebaseHistArray(array, from, to, diff) {5382for (var i = 0; i < array.length; ++i) {5383var sub = array[i], ok = true;5384for (var j = 0; j < sub.changes.length; ++j) {5385var cur = sub.changes[j];5386if (!sub.copied) { cur.from = copyPos(cur.from); cur.to = copyPos(cur.to); }5387if (to < cur.from.line) {5388cur.from.line += diff;5389cur.to.line += diff;5390} else if (from <= cur.to.line) {5391ok = false;5392break;5393}5394}5395if (!sub.copied) {5396sub.anchorBefore = copyPos(sub.anchorBefore); sub.headBefore = copyPos(sub.headBefore);5397sub.anchorAfter = copyPos(sub.anchorAfter); sub.readAfter = copyPos(sub.headAfter);5398sub.copied = true;5399}5400if (!ok) {5401array.splice(0, i + 1);5402i = 0;5403} else {5404rebaseHistSel(sub.anchorBefore); rebaseHistSel(sub.headBefore);5405rebaseHistSel(sub.anchorAfter); rebaseHistSel(sub.headAfter);5406}5407}5408}54095410function rebaseHist(hist, change) {5411var from = change.from.line, to = change.to.line, diff = change.text.length - (to - from) - 1;5412rebaseHistArray(hist.done, from, to, diff);5413rebaseHistArray(hist.undone, from, to, diff);5414}54155416// EVENT OPERATORS54175418function stopMethod() {e_stop(this);}5419// Ensure an event has a stop method.5420function addStop(event) {5421if (!event.stop) event.stop = stopMethod;5422return event;5423}54245425function e_preventDefault(e) {5426if (e.preventDefault) e.preventDefault();5427else e.returnValue = false;5428}5429function e_stopPropagation(e) {5430if (e.stopPropagation) e.stopPropagation();5431else e.cancelBubble = true;5432}5433function e_defaultPrevented(e) {5434return e.defaultPrevented != null ? e.defaultPrevented : e.returnValue == false;5435}5436function e_stop(e) {e_preventDefault(e); e_stopPropagation(e);}5437CodeMirror.e_stop = e_stop;5438CodeMirror.e_preventDefault = e_preventDefault;5439CodeMirror.e_stopPropagation = e_stopPropagation;54405441function e_target(e) {return e.target || e.srcElement;}5442function e_button(e) {5443var b = e.which;5444if (b == null) {5445if (e.button & 1) b = 1;5446else if (e.button & 2) b = 3;5447else if (e.button & 4) b = 2;5448}5449if (mac && e.ctrlKey && b == 1) b = 3;5450return b;5451}54525453// EVENT HANDLING54545455function on(emitter, type, f) {5456if (emitter.addEventListener)5457emitter.addEventListener(type, f, false);5458else if (emitter.attachEvent)5459emitter.attachEvent("on" + type, f);5460else {5461var map = emitter._handlers || (emitter._handlers = {});5462var arr = map[type] || (map[type] = []);5463arr.push(f);5464}5465}54665467function off(emitter, type, f) {5468if (emitter.removeEventListener)5469emitter.removeEventListener(type, f, false);5470else if (emitter.detachEvent)5471emitter.detachEvent("on" + type, f);5472else {5473var arr = emitter._handlers && emitter._handlers[type];5474if (!arr) return;5475for (var i = 0; i < arr.length; ++i)5476if (arr[i] == f) { arr.splice(i, 1); break; }5477}5478}54795480function signal(emitter, type /*, values...*/) {5481var arr = emitter._handlers && emitter._handlers[type];5482if (!arr) return;5483var args = Array.prototype.slice.call(arguments, 2);5484for (var i = 0; i < arr.length; ++i) arr[i].apply(null, args);5485}54865487var delayedCallbacks, delayedCallbackDepth = 0;5488function signalLater(emitter, type /*, values...*/) {5489var arr = emitter._handlers && emitter._handlers[type];5490if (!arr) return;5491var args = Array.prototype.slice.call(arguments, 2);5492if (!delayedCallbacks) {5493++delayedCallbackDepth;5494delayedCallbacks = [];5495setTimeout(fireDelayed, 0);5496}5497function bnd(f) {return function(){f.apply(null, args);};};5498for (var i = 0; i < arr.length; ++i)5499delayedCallbacks.push(bnd(arr[i]));5500}55015502function signalDOMEvent(cm, e, override) {5503signal(cm, override || e.type, cm, e);5504return e_defaultPrevented(e) || e.codemirrorIgnore;5505}55065507function fireDelayed() {5508--delayedCallbackDepth;5509var delayed = delayedCallbacks;5510delayedCallbacks = null;5511for (var i = 0; i < delayed.length; ++i) delayed[i]();5512}55135514function hasHandler(emitter, type) {5515var arr = emitter._handlers && emitter._handlers[type];5516return arr && arr.length > 0;5517}55185519CodeMirror.on = on; CodeMirror.off = off; CodeMirror.signal = signal;55205521function eventMixin(ctor) {5522ctor.prototype.on = function(type, f) {on(this, type, f);};5523ctor.prototype.off = function(type, f) {off(this, type, f);};5524}55255526// MISC UTILITIES55275528// Number of pixels added to scroller and sizer to hide scrollbar5529var scrollerCutOff = 30;55305531// Returned or thrown by various protocols to signal 'I'm not5532// handling this'.5533var Pass = CodeMirror.Pass = {toString: function(){return "CodeMirror.Pass";}};55345535function Delayed() {this.id = null;}5536Delayed.prototype = {set: function(ms, f) {clearTimeout(this.id); this.id = setTimeout(f, ms);}};55375538// Counts the column offset in a string, taking tabs into account.5539// Used mostly to find indentation.5540function countColumn(string, end, tabSize, startIndex, startValue) {5541if (end == null) {5542end = string.search(/[^\s\u00a0]/);5543if (end == -1) end = string.length;5544}5545for (var i = startIndex || 0, n = startValue || 0; i < end; ++i) {5546if (string.charAt(i) == "\t") n += tabSize - (n % tabSize);5547else ++n;5548}5549return n;5550}5551CodeMirror.countColumn = countColumn;55525553var spaceStrs = [""];5554function spaceStr(n) {5555while (spaceStrs.length <= n)5556spaceStrs.push(lst(spaceStrs) + " ");5557return spaceStrs[n];5558}55595560function lst(arr) { return arr[arr.length-1]; }55615562function selectInput(node) {5563if (ios) { // Mobile Safari apparently has a bug where select() is broken.5564node.selectionStart = 0;5565node.selectionEnd = node.value.length;5566} else {5567// Suppress mysterious IE10 errors5568try { node.select(); }5569catch(_e) {}5570}5571}55725573function indexOf(collection, elt) {5574if (collection.indexOf) return collection.indexOf(elt);5575for (var i = 0, e = collection.length; i < e; ++i)5576if (collection[i] == elt) return i;5577return -1;5578}55795580function createObj(base, props) {5581function Obj() {}5582Obj.prototype = base;5583var inst = new Obj();5584if (props) copyObj(props, inst);5585return inst;5586}55875588function copyObj(obj, target) {5589if (!target) target = {};5590for (var prop in obj) if (obj.hasOwnProperty(prop)) target[prop] = obj[prop];5591return target;5592}55935594function emptyArray(size) {5595for (var a = [], i = 0; i < size; ++i) a.push(undefined);5596return a;5597}55985599function bind(f) {5600var args = Array.prototype.slice.call(arguments, 1);5601return function(){return f.apply(null, args);};5602}56035604var nonASCIISingleCaseWordChar = /[\u3040-\u309f\u30a0-\u30ff\u3400-\u4db5\u4e00-\u9fcc\uac00-\ud7af]/;5605function isWordChar(ch) {5606return /\w/.test(ch) || ch > "\x80" &&5607(ch.toUpperCase() != ch.toLowerCase() || nonASCIISingleCaseWordChar.test(ch));5608}56095610function isEmpty(obj) {5611for (var n in obj) if (obj.hasOwnProperty(n) && obj[n]) return false;5612return true;5613}56145615var 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]/;5616function isExtendingChar(ch) { return ch.charCodeAt(0) >= 768 && extendingChars.test(ch); }56175618// DOM UTILITIES56195620function elt(tag, content, className, style) {5621var e = document.createElement(tag);5622if (className) e.className = className;5623if (style) e.style.cssText = style;5624if (typeof content == "string") setTextContent(e, content);5625else if (content) for (var i = 0; i < content.length; ++i) e.appendChild(content[i]);5626return e;5627}56285629function removeChildren(e) {5630for (var count = e.childNodes.length; count > 0; --count)5631e.removeChild(e.firstChild);5632return e;5633}56345635function removeChildrenAndAdd(parent, e) {5636return removeChildren(parent).appendChild(e);5637}56385639function setTextContent(e, str) {5640if (ie_lt9) {5641e.innerHTML = "";5642e.appendChild(document.createTextNode(str));5643} else e.textContent = str;5644}56455646function getRect(node) {5647return node.getBoundingClientRect();5648}5649CodeMirror.replaceGetRect = function(f) { getRect = f; };56505651// FEATURE DETECTION56525653// Detect drag-and-drop5654var dragAndDrop = function() {5655// There is *some* kind of drag-and-drop support in IE6-8, but I5656// couldn't get it to work yet.5657if (ie_lt9) return false;5658var div = elt('div');5659return "draggable" in div || "dragDrop" in div;5660}();56615662// For a reason I have yet to figure out, some browsers disallow5663// word wrapping between certain characters *only* if a new inline5664// element is started between them. This makes it hard to reliably5665// measure the position of things, since that requires inserting an5666// extra span. This terribly fragile set of tests matches the5667// character combinations that suffer from this phenomenon on the5668// various browsers.5669function spanAffectsWrapping() { return false; }5670if (gecko) // Only for "$'"5671spanAffectsWrapping = function(str, i) {5672return str.charCodeAt(i - 1) == 36 && str.charCodeAt(i) == 39;5673};5674else if (safari && !/Version\/([6-9]|\d\d)\b/.test(navigator.userAgent))5675spanAffectsWrapping = function(str, i) {5676return /\-[^ \-?]|\?[^ !\'\"\),.\-\/:;\?\]\}]/.test(str.slice(i - 1, i + 1));5677};5678else if (webkit && /Chrome\/(?:29|[3-9]\d|\d\d\d)\./.test(navigator.userAgent))5679spanAffectsWrapping = function(str, i) {5680var code = str.charCodeAt(i - 1);5681return code >= 8208 && code <= 8212;5682};5683else if (webkit)5684spanAffectsWrapping = function(str, i) {5685if (i > 1 && str.charCodeAt(i - 1) == 45) {5686if (/\w/.test(str.charAt(i - 2)) && /[^\-?\.]/.test(str.charAt(i))) return true;5687if (i > 2 && /[\d\.,]/.test(str.charAt(i - 2)) && /[\d\.,]/.test(str.charAt(i))) return false;5688}5689return /[~!#%&*)=+}\]\\|\"\.>,:;][({[<]|-[^\-?\.\u2010-\u201f\u2026]|\?[\w~`@#$%\^&*(_=+{[|><]|\u2026[\w~`@#$%\^&*(_=+{[><]/.test(str.slice(i - 1, i + 1));5690};56915692var knownScrollbarWidth;5693function scrollbarWidth(measure) {5694if (knownScrollbarWidth != null) return knownScrollbarWidth;5695var test = elt("div", null, null, "width: 50px; height: 50px; overflow-x: scroll");5696removeChildrenAndAdd(measure, test);5697if (test.offsetWidth)5698knownScrollbarWidth = test.offsetHeight - test.clientHeight;5699return knownScrollbarWidth || 0;5700}57015702var zwspSupported;5703function zeroWidthElement(measure) {5704if (zwspSupported == null) {5705var test = elt("span", "\u200b");5706removeChildrenAndAdd(measure, elt("span", [test, document.createTextNode("x")]));5707if (measure.firstChild.offsetHeight != 0)5708zwspSupported = test.offsetWidth <= 1 && test.offsetHeight > 2 && !ie_lt8;5709}5710if (zwspSupported) return elt("span", "\u200b");5711else return elt("span", "\u00a0", null, "display: inline-block; width: 1px; margin-right: -1px");5712}57135714// See if "".split is the broken IE version, if so, provide an5715// alternative way to split lines.5716var splitLines = "\n\nb".split(/\n/).length != 3 ? function(string) {5717var pos = 0, result = [], l = string.length;5718while (pos <= l) {5719var nl = string.indexOf("\n", pos);5720if (nl == -1) nl = string.length;5721var line = string.slice(pos, string.charAt(nl - 1) == "\r" ? nl - 1 : nl);5722var rt = line.indexOf("\r");5723if (rt != -1) {5724result.push(line.slice(0, rt));5725pos += rt + 1;5726} else {5727result.push(line);5728pos = nl + 1;5729}5730}5731return result;5732} : function(string){return string.split(/\r\n?|\n/);};5733CodeMirror.splitLines = splitLines;57345735var hasSelection = window.getSelection ? function(te) {5736try { return te.selectionStart != te.selectionEnd; }5737catch(e) { return false; }5738} : function(te) {5739try {var range = te.ownerDocument.selection.createRange();}5740catch(e) {}5741if (!range || range.parentElement() != te) return false;5742return range.compareEndPoints("StartToEnd", range) != 0;5743};57445745var hasCopyEvent = (function() {5746var e = elt("div");5747if ("oncopy" in e) return true;5748e.setAttribute("oncopy", "return;");5749return typeof e.oncopy == 'function';5750})();57515752// KEY NAMING57535754var keyNames = {3: "Enter", 8: "Backspace", 9: "Tab", 13: "Enter", 16: "Shift", 17: "Ctrl", 18: "Alt",575519: "Pause", 20: "CapsLock", 27: "Esc", 32: "Space", 33: "PageUp", 34: "PageDown", 35: "End",575636: "Home", 37: "Left", 38: "Up", 39: "Right", 40: "Down", 44: "PrintScrn", 45: "Insert",575746: "Delete", 59: ";", 61: "=", 91: "Mod", 92: "Mod", 93: "Mod", 107: "=", 109: "-", 127: "Delete",5758173: "-", 186: ";", 187: "=", 188: ",", 189: "-", 190: ".", 191: "/", 192: "`", 219: "[", 220: "\\",5759221: "]", 222: "'", 63232: "Up", 63233: "Down", 63234: "Left", 63235: "Right", 63272: "Delete",576063273: "Home", 63275: "End", 63276: "PageUp", 63277: "PageDown", 63302: "Insert"};5761CodeMirror.keyNames = keyNames;5762(function() {5763// Number keys5764for (var i = 0; i < 10; i++) keyNames[i + 48] = keyNames[i + 96] = String(i);5765// Alphabetic keys5766for (var i = 65; i <= 90; i++) keyNames[i] = String.fromCharCode(i);5767// Function keys5768for (var i = 1; i <= 12; i++) keyNames[i + 111] = keyNames[i + 63235] = "F" + i;5769})();57705771// BIDI HELPERS57725773function iterateBidiSections(order, from, to, f) {5774if (!order) return f(from, to, "ltr");5775var found = false;5776for (var i = 0; i < order.length; ++i) {5777var part = order[i];5778if (part.from < to && part.to > from || from == to && part.to == from) {5779f(Math.max(part.from, from), Math.min(part.to, to), part.level == 1 ? "rtl" : "ltr");5780found = true;5781}5782}5783if (!found) f(from, to, "ltr");5784}57855786function bidiLeft(part) { return part.level % 2 ? part.to : part.from; }5787function bidiRight(part) { return part.level % 2 ? part.from : part.to; }57885789function lineLeft(line) { var order = getOrder(line); return order ? bidiLeft(order[0]) : 0; }5790function lineRight(line) {5791var order = getOrder(line);5792if (!order) return line.text.length;5793return bidiRight(lst(order));5794}57955796function lineStart(cm, lineN) {5797var line = getLine(cm.doc, lineN);5798var visual = visualLine(cm.doc, line);5799if (visual != line) lineN = lineNo(visual);5800var order = getOrder(visual);5801var ch = !order ? 0 : order[0].level % 2 ? lineRight(visual) : lineLeft(visual);5802return Pos(lineN, ch);5803}5804function lineEnd(cm, lineN) {5805var merged, line;5806while (merged = collapsedSpanAtEnd(line = getLine(cm.doc, lineN)))5807lineN = merged.find().to.line;5808var order = getOrder(line);5809var ch = !order ? line.text.length : order[0].level % 2 ? lineLeft(line) : lineRight(line);5810return Pos(lineN, ch);5811}58125813function compareBidiLevel(order, a, b) {5814var linedir = order[0].level;5815if (a == linedir) return true;5816if (b == linedir) return false;5817return a < b;5818}5819var bidiOther;5820function getBidiPartAt(order, pos) {5821bidiOther = null;5822for (var i = 0, found; i < order.length; ++i) {5823var cur = order[i];5824if (cur.from < pos && cur.to > pos) return i;5825if ((cur.from == pos || cur.to == pos)) {5826if (found == null) {5827found = i;5828} else if (compareBidiLevel(order, cur.level, order[found].level)) {5829if (cur.from != cur.to) bidiOther = found;5830return i;5831} else {5832if (cur.from != cur.to) bidiOther = i;5833return found;5834}5835}5836}5837return found;5838}58395840function moveInLine(line, pos, dir, byUnit) {5841if (!byUnit) return pos + dir;5842do pos += dir;5843while (pos > 0 && isExtendingChar(line.text.charAt(pos)));5844return pos;5845}58465847// This is somewhat involved. It is needed in order to move5848// 'visually' through bi-directional text -- i.e., pressing left5849// should make the cursor go left, even when in RTL text. The5850// tricky part is the 'jumps', where RTL and LTR text touch each5851// other. This often requires the cursor offset to move more than5852// one unit, in order to visually move one unit.5853function moveVisually(line, start, dir, byUnit) {5854var bidi = getOrder(line);5855if (!bidi) return moveLogically(line, start, dir, byUnit);5856var pos = getBidiPartAt(bidi, start), part = bidi[pos];5857var target = moveInLine(line, start, part.level % 2 ? -dir : dir, byUnit);58585859for (;;) {5860if (target > part.from && target < part.to) return target;5861if (target == part.from || target == part.to) {5862if (getBidiPartAt(bidi, target) == pos) return target;5863part = bidi[pos += dir];5864return (dir > 0) == part.level % 2 ? part.to : part.from;5865} else {5866part = bidi[pos += dir];5867if (!part) return null;5868if ((dir > 0) == part.level % 2)5869target = moveInLine(line, part.to, -1, byUnit);5870else5871target = moveInLine(line, part.from, 1, byUnit);5872}5873}5874}58755876function moveLogically(line, start, dir, byUnit) {5877var target = start + dir;5878if (byUnit) while (target > 0 && isExtendingChar(line.text.charAt(target))) target += dir;5879return target < 0 || target > line.text.length ? null : target;5880}58815882// Bidirectional ordering algorithm5883// See http://unicode.org/reports/tr9/tr9-13.html for the algorithm5884// that this (partially) implements.58855886// One-char codes used for character types:5887// L (L): Left-to-Right5888// R (R): Right-to-Left5889// r (AL): Right-to-Left Arabic5890// 1 (EN): European Number5891// + (ES): European Number Separator5892// % (ET): European Number Terminator5893// n (AN): Arabic Number5894// , (CS): Common Number Separator5895// m (NSM): Non-Spacing Mark5896// b (BN): Boundary Neutral5897// s (B): Paragraph Separator5898// t (S): Segment Separator5899// w (WS): Whitespace5900// N (ON): Other Neutrals59015902// Returns null if characters are ordered as they appear5903// (left-to-right), or an array of sections ({from, to, level}5904// objects) in the order in which they occur visually.5905var bidiOrdering = (function() {5906// Character types for codepoints 0 to 0xff5907var lowTypes = "bbbbbbbbbtstwsbbbbbbbbbbbbbbssstwNN%%%NNNNNN,N,N1111111111NNNNNNNLLLLLLLLLLLLLLLLLLLLLLLLLLNNNNNNLLLLLLLLLLLLLLLLLLLLLLLLLLNNNNbbbbbbsbbbbbbbbbbbbbbbbbbbbbbbbbb,N%%%%NNNNLNNNNN%%11NLNNN1LNNNNNLLLLLLLLLLLLLLLLLLLLLLLNLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLNLLLLLLLL";5908// Character types for codepoints 0x600 to 0x6ff5909var arabicTypes = "rrrrrrrrrrrr,rNNmmmmmmrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrmmmmmmmmmmmmmmrrrrrrrnnnnnnnnnn%nnrrrmrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrmmmmmmmmmmmmmmmmmmmNmmmmrrrrrrrrrrrrrrrrrr";5910function charType(code) {5911if (code <= 0xff) return lowTypes.charAt(code);5912else if (0x590 <= code && code <= 0x5f4) return "R";5913else if (0x600 <= code && code <= 0x6ff) return arabicTypes.charAt(code - 0x600);5914else if (0x700 <= code && code <= 0x8ac) return "r";5915else return "L";5916}59175918var bidiRE = /[\u0590-\u05f4\u0600-\u06ff\u0700-\u08ac]/;5919var isNeutral = /[stwN]/, isStrong = /[LRr]/, countsAsLeft = /[Lb1n]/, countsAsNum = /[1n]/;5920// Browsers seem to always treat the boundaries of block elements as being L.5921var outerType = "L";59225923return function(str) {5924if (!bidiRE.test(str)) return false;5925var len = str.length, types = [];5926for (var i = 0, type; i < len; ++i)5927types.push(type = charType(str.charCodeAt(i)));59285929// W1. Examine each non-spacing mark (NSM) in the level run, and5930// change the type of the NSM to the type of the previous5931// character. If the NSM is at the start of the level run, it will5932// get the type of sor.5933for (var i = 0, prev = outerType; i < len; ++i) {5934var type = types[i];5935if (type == "m") types[i] = prev;5936else prev = type;5937}59385939// W2. Search backwards from each instance of a European number5940// until the first strong type (R, L, AL, or sor) is found. If an5941// AL is found, change the type of the European number to Arabic5942// number.5943// W3. Change all ALs to R.5944for (var i = 0, cur = outerType; i < len; ++i) {5945var type = types[i];5946if (type == "1" && cur == "r") types[i] = "n";5947else if (isStrong.test(type)) { cur = type; if (type == "r") types[i] = "R"; }5948}59495950// W4. A single European separator between two European numbers5951// changes to a European number. A single common separator between5952// two numbers of the same type changes to that type.5953for (var i = 1, prev = types[0]; i < len - 1; ++i) {5954var type = types[i];5955if (type == "+" && prev == "1" && types[i+1] == "1") types[i] = "1";5956else if (type == "," && prev == types[i+1] &&5957(prev == "1" || prev == "n")) types[i] = prev;5958prev = type;5959}59605961// W5. A sequence of European terminators adjacent to European5962// numbers changes to all European numbers.5963// W6. Otherwise, separators and terminators change to Other5964// Neutral.5965for (var i = 0; i < len; ++i) {5966var type = types[i];5967if (type == ",") types[i] = "N";5968else if (type == "%") {5969for (var end = i + 1; end < len && types[end] == "%"; ++end) {}5970var replace = (i && types[i-1] == "!") || (end < len && types[end] == "1") ? "1" : "N";5971for (var j = i; j < end; ++j) types[j] = replace;5972i = end - 1;5973}5974}59755976// W7. Search backwards from each instance of a European number5977// until the first strong type (R, L, or sor) is found. If an L is5978// found, then change the type of the European number to L.5979for (var i = 0, cur = outerType; i < len; ++i) {5980var type = types[i];5981if (cur == "L" && type == "1") types[i] = "L";5982else if (isStrong.test(type)) cur = type;5983}59845985// N1. A sequence of neutrals takes the direction of the5986// surrounding strong text if the text on both sides has the same5987// direction. European and Arabic numbers act as if they were R in5988// terms of their influence on neutrals. Start-of-level-run (sor)5989// and end-of-level-run (eor) are used at level run boundaries.5990// N2. Any remaining neutrals take the embedding direction.5991for (var i = 0; i < len; ++i) {5992if (isNeutral.test(types[i])) {5993for (var end = i + 1; end < len && isNeutral.test(types[end]); ++end) {}5994var before = (i ? types[i-1] : outerType) == "L";5995var after = (end < len ? types[end] : outerType) == "L";5996var replace = before || after ? "L" : "R";5997for (var j = i; j < end; ++j) types[j] = replace;5998i = end - 1;5999}6000}60016002// Here we depart from the documented algorithm, in order to avoid6003// building up an actual levels array. Since there are only three6004// levels (0, 1, 2) in an implementation that doesn't take6005// explicit embedding into account, we can build up the order on6006// the fly, without following the level-based algorithm.6007var order = [], m;6008for (var i = 0; i < len;) {6009if (countsAsLeft.test(types[i])) {6010var start = i;6011for (++i; i < len && countsAsLeft.test(types[i]); ++i) {}6012order.push({from: start, to: i, level: 0});6013} else {6014var pos = i, at = order.length;6015for (++i; i < len && types[i] != "L"; ++i) {}6016for (var j = pos; j < i;) {6017if (countsAsNum.test(types[j])) {6018if (pos < j) order.splice(at, 0, {from: pos, to: j, level: 1});6019var nstart = j;6020for (++j; j < i && countsAsNum.test(types[j]); ++j) {}6021order.splice(at, 0, {from: nstart, to: j, level: 2});6022pos = j;6023} else ++j;6024}6025if (pos < i) order.splice(at, 0, {from: pos, to: i, level: 1});6026}6027}6028if (order[0].level == 1 && (m = str.match(/^\s+/))) {6029order[0].from = m[0].length;6030order.unshift({from: 0, to: m[0].length, level: 0});6031}6032if (lst(order).level == 1 && (m = str.match(/\s+$/))) {6033lst(order).to -= m[0].length;6034order.push({from: len - m[0].length, to: len, level: 0});6035}6036if (order[0].level != lst(order).level)6037order.push({from: len, to: len, level: order[0].level});60386039return order;6040};6041})();60426043// THE END60446045CodeMirror.version = "3.21.0";60466047return CodeMirror;6048})();604960506051