e&&l.splice(p,1,e,l[p+1],r),p+=2,i=s(e,r);if(t)if(o.opaque)l.splice(n,p-n,e,"overlay "+t),p=n+2;else for(;ne.options.maxHighlightLength&&et(e.doc.mode,o.state),i=ot(e,t,o);r&&(o.state=r),t.stateAfter=o.save(!r),t.styles=i.styles,i.classes?t.styleClasses=i.classes:t.styleClasses&&(t.styleClasses=null),n===e.doc.highlightFrontier&&(e.doc.modeFrontier=l(e.doc.modeFrontier,++e.doc.highlightFrontier))}return t.styles}function at(e,t,n){var o=e.doc,r=e.display;if(!o.mode.startState)return new vs(o,!0,t);var i=ut(e,t,n),s=i>o.first&&R(o,i-1).stateAfter,l=s?vs.fromSaved(o,s,i):new vs(o,nt(o.mode),i);return o.iter(i,t,function(n){it(e,n.text,l);var o=l.line;n.stateAfter=o==t-1||0==o%5||o>=r.viewFrom&∨r++){o&&(o[0]=tt(e,n).mode);var i=e.token(t,n);if(t.pos>t.start)return i}throw new Error("Mode "+e.name+" failed to advance stream.")}function dt(e,t,n,o){var r=e.doc,i=r.mode,s;t=J(r,t);var l=R(r,t.line),d=at(e,t.line,n),c=new fs(l.text,e.options.tabSize,d),p;for(o&&(p=[]);(o||c.pose.options.maxHighlightLength?(d=!1,l&&it(e,t,o,u.pos),u.pos=t.length,h=null):h=ct(lt(n,u,o.state,m),i),m){var g=m[0].name;g&&(h="m-"+(h?g+" "+h:g))}if(!d||p!=h){for(;cr;--i){if(i<=o.first)return o.first;var d=R(o,i-1),c=d.stateAfter;if(c&&(!n||i+(c instanceof xs?c.lookAhead:0)<=o.modeFrontier))return i;var p=v(d.text,null,e.options.tabSize);(null==l||s>p)&&(l=i-1,s=p)}return l}function mt(e,t){if(e.modeFrontier=s(e.modeFrontier,t),!(e.highlightFrontiern;o--)if(r=R(e,o).stateAfter,r&&(!(r instanceof xs)||o+r.lookAheadIi&&(c=!0),e.pos+=t.length;else{u=document.createDocumentFragment();for(var h=0;;){d.lastIndex=h;var g=d.exec(t),f=g?g.index-h:t.length-h;if(f){var y=document.createTextNode(l.slice(h,h+f));Ai&&9>Ii?u.appendChild(p("span",[y])):u.appendChild(y),e.map.push(e.pos,e.pos+f,y),e.col+=f,e.pos+=f}if(!g)break;h+=f+1;var b=void 0;if("\t"==g[0]){var x=e.cm.options.tabSize,v=x-e.col%x;b=u.appendChild(p("span",S(v),"cm-tab")),b.setAttribute("role","presentation"),b.setAttribute("cm-text","\t"),e.col+=v}else"\r"==g[0]||"\n"==g[0]?(b=u.appendChild(p("span","\r"==g[0]?"\u240D":"\u2424","cm-invalidchar")),b.setAttribute("cm-text",g[0]),e.col+=1):(b=e.cm.options.specialCharPlaceholder(g[0]),b.setAttribute("cm-text",g[0]),Ai&&9>Ii?u.appendChild(p("span",[b])):u.appendChild(b),e.col+=1);e.map.push(e.pos,e.pos+1,b),e.pos++}}if(e.trailingSpace=32==l.charCodeAt(t.length-1),n||o||r||c||s){var k=n||"";o&&(k+=o),r&&(k+=r);var C=p("span",[u],k,s);return i&&(C.title=i),e.content.appendChild(C)}e.content.appendChild(u)}}function vt(e,t){if(1p&&m.from<=p));h++);if(m.to>=u)return e(n,o,r,s,l,d,c);e(n,o.slice(0,m.to-p),r,s,null,d,c),s=null,o=o.slice(m.to-p),p=m.to}}}function St(e,t,n,o){var r=!o&&n.widgetNode;r&&e.map.push(e.pos,e.pos+t,r),!o&&e.cm.display.input.needsContentAttribute&&(!r&&(r=e.content.appendChild(document.createElement("span"))),r.setAttribute("cm-marker",n.id)),r&&(e.cm.display.input.setUneditable(r),e.content.appendChild(r)),e.pos+=t,e.trailingSpace=!1}function Ct(e,t,n){var o=e.markedSpans,r=e.text,l=0;if(!o){for(var d=1;dp||E.collapsed&&T.to==p&&T.from==p)?(null!=T.to&&T.to!=p&&h>T.to&&(h=T.to,b=""),E.className&&(y+=" "+E.className),E.css&&(f=(f?f+";":"")+E.css),E.startStyle&&T.from==p&&(x+=" "+E.startStyle),E.endStyle&&T.to==h&&(C||(C=[])).push(E.endStyle,T.to),E.title&&!v&&(v=E.title),E.collapsed&&(!k||0>ge(k.marker,E))&&(k=T)):T.from>p&&h>T.from&&(h=T.from)}if(C)for(var m=0;m=c)break;for(var I=s(c,h);;){if(i){var N=p+i.length;if(!k){var L=N>I?i.slice(0,I-p):i;t.addToken(t,L,g?g+y:y,x,p+L.length==h?b:"",v,f)}if(N>=I){i=i.slice(I-p),p=I;break}p=N,x=""}i=r.slice(l,l=n[u++]),g=ft(n[u++],t.cm.options)}}}function wt(e,t,n){this.line=t,this.rest=Ce(t),this.size=this.rest?U(C(this.rest))-n+1:1,this.node=this.text=null,this.hidden=Ee(e,t)}function Tt(e,t,n){for(var o=[],r=t,i,s;rIi&&(e.node.style.zIndex=2)),e.node}function Ot(e,t){var n=t.bgClass?t.bgClass+" "+(t.line.bgClass||""):t.line.bgClass;if(n&&(n+=" CodeMirror-linebackground"),t.background)n?t.background.className=n:(t.background.parentNode.removeChild(t.background),t.background=null);else if(n){var o=Mt(t);t.background=o.insertBefore(p("div",null,n),o.firstChild),e.display.input.setUneditable(t.background)}}function Dt(e,t){var n=e.display.externalMeasured;return n&&n.line==t.line?(e.display.externalMeasured=null,t.measure=n.measure,n.built):yt(e,t)}function Rt(e,t){var n=t.text.className,o=Dt(e,t);t.text==t.node&&(t.node=o.pre),t.text.parentNode.replaceChild(o.pre,t.text),t.text=o.pre,o.bgClass!=t.bgClass||o.textClass!=t.textClass?(t.bgClass=o.bgClass,t.textClass=o.textClass,Pt(e,t)):n&&(t.text.className=n)}function Pt(e,t){Ot(e,t),t.line.wrapClass?Mt(t).className=t.line.wrapClass:t.node!=t.text&&(t.node.className="");var n=t.textClass?t.textClass+" "+(t.line.textClass||""):t.line.textClass;t.text.className=n||""}function Ft(e,t,n,o){if(t.gutter&&(t.node.removeChild(t.gutter),t.gutter=null),t.gutterBackground&&(t.node.removeChild(t.gutterBackground),t.gutterBackground=null),t.line.gutterClass){var r=Mt(t);t.gutterBackground=p("div",null,"CodeMirror-gutter-background "+t.line.gutterClass,"left: "+(e.options.fixedGutter?o.fixedPos:-o.gutterTotalWidth)+"px; width: "+o.gutterTotalWidth+"px"),e.display.input.setUneditable(t.gutterBackground),r.insertBefore(t.gutterBackground,t.text)}var i=t.line.gutterMarkers;if(e.options.lineNumbers||i){var s=Mt(t),l=t.gutter=p("div",null,"CodeMirror-gutter-wrapper","left: "+(e.options.fixedGutter?o.fixedPos:-o.gutterTotalWidth)+"px");if(e.display.input.setUneditable(l),s.insertBefore(l,t.text),t.line.gutterClass&&(l.className+=" "+t.line.gutterClass),!e.options.lineNumbers||i&&i["CodeMirror-linenumbers"]||(t.lineNumber=l.appendChild(p("div",j(e.options,n),"CodeMirror-linenumber CodeMirror-gutter-elt","left: "+o.gutterLeft["CodeMirror-linenumbers"]+"px; width: "+e.display.lineNumInnerWidth+"px"))),i)for(var d=0;dn)return{map:e.measure.maps[r],cache:e.measure.caches[r],before:!0}}function Zt(e,t){t=ke(t);var n=U(t),o=e.display.externalMeasured=new wt(e.doc,t,n);o.lineN=n;var r=o.built=yt(e,o);return o.text=r.pre,c(e.display.lineMeasure,r.pre),o}function en(e,t,n,o){return rn(e,nn(e,t),n,o)}function tn(e,t){if(t>=e.display.viewFrom&&t=n.lineN&&tt)&&(s=c-d,i=s-1,t>=c&&(l="right")),null!=i){if(r=e[o+2],d==c&&n==(r.insertLeft?"left":"right")&&(l=n),"left"==n&&0==i)for(;o&&e[o-2]==e[o-3]&&e[o-1].insertLeft;)r=e[(o-=3)+2],l="left";if("right"==n&&i==c-d)for(;ou;u++){for(;l&&_(t.line.text.charAt(r.coverStart+l));)--l;for(;r.coverStart+dIi&&0==l&&d==r.coverEnd-r.coverStart?s.parentNode.getBoundingClientRect():sn(Ki(s,l,d).getClientRects(),o),p.left||p.right||0==l)break;d=l,--l,c="right"}Ai&&11>Ii&&(p=dn(e.display.measure,p))}else{0Ii&&!l&&(!p||!p.left&&!p.right)){var h=s.parentNode.getClientRects()[0];p=h?{left:h.left,right:h.left+Ln(e.display),top:h.top,bottom:h.bottom}:As}for(var g=p.top-t.rect.top,f=p.bottom-t.rect.top,y=t.view.measure.heights,b=0;b=o.text.length?(c=o.text.length,p="before"):0>=c&&(c=0,p="after"),!d)return s("before"==p?c-1:c,"before"==p);var u=Me(d,c,p),m=as,h=l(c,u,"before"==p);return null!=m&&(h.other=l(c,m,"before"!=p)),h}function vn(e,t){var n=0;t=J(e.doc,t),e.options.lineWrapping||(n=Ln(e.display)*t.ch);var o=R(e.doc,t.line),r=Ie(o)+Ht(e.display);return{left:n,right:n,top:r,bottom:r+o.height}}function kn(e,t,n,o,r){var i=W(e,t,n);return i.xRel=r,o&&(i.outside=!0),i}function Sn(e,t,n){var o=e.doc;if(n+=e.display.viewOffset,0>n)return kn(o.first,0,null,!0,-1);var r=V(o,n),i=o.first+o.size-1;if(r>i)return kn(o.first+o.size-1,R(o,i).text.length,null,!0,1);0>t&&(t=0);for(var s=R(o,r);;){var l=En(e,s,r,t,n),d=xe(s,l.ch+(0o},i,r),{begin:i,end:r}}function wn(e,t,n,o){n||(n=nn(e,t));var r=fn(e,t,rn(e,n,o),"line").top;return Cn(e,t,n,r)}function Tn(e,t,n,o){return!(e.bottom<=n)&&(!!(e.top>n)||(o?e.left:e.right)>t)}function En(e,t,n,o,r){r-=Ie(t);var i=nn(e,t),s=gn(t),l=0,d=t.text.length,c=!0,p=Oe(t,e.doc.direction);if(p){var u=(e.options.lineWrapping?In:An)(e,t,n,i,p,o,r);c=1!=u.level,l=c?u.from:u.to-1,d=c?u.to:u.from-1}var m=null,h=null,g=O(function(t){var n=rn(e,i,t);return(n.top+=s,n.bottom+=s,!!Tn(n,o,r,!1))&&(n.top<=r&&n.left<=o&&(m=t,h=n),!0)},l,d),f=!1,y,b;if(h){var x=o-h.left