function UndoHistory(a,d,c,f){this.container=a;this.maxDepth=d;this.commitDelay=c;this.editor=f;this.parent=f.parent;this.last=this.first=a={text:"",from:null,to:null};this.firstTouched=false;this.history=[];this.redoHistory=[];this.touched=[]}
UndoHistory.prototype={scheduleCommit:function(){var a=this;this.parent.clearTimeout(this.commitTimeout);this.commitTimeout=this.parent.setTimeout(function(){a.tryCommit()},this.commitDelay)},touch:function(a){this.setTouched(a);this.scheduleCommit()},undo:function(){this.commit();if(this.history.length){var a=this.history.pop();this.redoHistory.push(this.updateTo(a,"applyChain"));this.notifyEnvironment();return this.chainNode(a)}},redo:function(){this.commit();if(this.redoHistory.length){var a=this.redoHistory.pop();
this.addUndoLevel(this.updateTo(a,"applyChain"));this.notifyEnvironment();return this.chainNode(a)}},clear:function(){this.history=[];this.redoHistory=[]},historySize:function(){return{undo:this.history.length,redo:this.redoHistory.length}},push:function(a,d,c){for(var f=[],h=0;h<c.length;h++){var j=h==c.length-1?d:this.container.ownerDocument.createElement("BR");f.push({from:a,to:j,text:cleanText(c[h])});a=j}this.pushChains([f],a==null&&d==null);this.notifyEnvironment()},pushChains:function(a,d){this.commit(d);
this.addUndoLevel(this.updateTo(a,"applyChain"));this.redoHistory=[]},chainNode:function(a){for(var d=0;d<a.length;d++){var c=a[d][0];if(c=c&&(c.from||c.to))return c}},reset:function(){this.history=[];this.redoHistory=[]},textAfter:function(a){return this.after(a).text},nodeAfter:function(a){return this.after(a).to},nodeBefore:function(a){return this.before(a).from},tryCommit:function(){!window||!window.parent||!window.UndoHistory||(this.editor.highlightDirty()?this.commit(true):this.scheduleCommit())},
commit:function(a){this.parent.clearTimeout(this.commitTimeout);a||this.editor.highlightDirty(true);a=this.touchedChains();if(a.length){this.addUndoLevel(this.updateTo(a,"linkChain"));this.redoHistory=[];this.notifyEnvironment()}},updateTo:function(a,d){for(var c=[],f=[],h=0;h<a.length;h++){c.push(this.shadowChain(a[h]));f.push(this[d](a[h]))}d=="applyChain"&&this.notifyDirty(f);return c},notifyDirty:function(a){forEach(a,method(this.editor,"addDirtyNode"));this.editor.scheduleHighlight()},notifyEnvironment:function(){this.onChange&&
this.onChange();window.frameElement&&window.frameElement.CodeMirror.updateNumbers&&window.frameElement.CodeMirror.updateNumbers()},linkChain:function(a){for(var d=0;d<a.length;d++){var c=a[d];if(c.from)c.from.historyAfter=c;else this.first=c;if(c.to)c.to.historyBefore=c;else this.last=c}},after:function(a){return a?a.historyAfter:this.first},before:function(a){return a?a.historyBefore:this.last},setTouched:function(a){if(a){if(!a.historyTouched){this.touched.push(a);a.historyTouched=true}}else this.firstTouched=
true},addUndoLevel:function(a){this.history.push(a);this.history.length>this.maxDepth&&this.history.shift()},touchedChains:function(){function a(e,g){if(e)e.historyTemp=g;else h=g}function d(e){for(var g=[],b=e?e.nextSibling:f.container.firstChild;b&&(!isBR(b)||b.hackBR);b=b.nextSibling)!b.hackBR&&b.currentText&&g.push(b.currentText);return{from:e,to:b,text:cleanText(g.join(""))}}function c(e,g){for(var b=g+"Sibling",i=e[b];i&&!isBR(i);)i=i[b];return i}var f=this,h=null,j=[];f.firstTouched&&f.touched.push(null);
forEach(f.touched,function(e){if(!(e&&(e.parentNode!=f.container||e.hackBR))){if(e)e.historyTouched=false;else f.firstTouched=false;var g=d(e),b=f.after(e);if(!b||b.text!=g.text||b.to!=g.to){j.push(g);a(e,g)}}});var k=[];f.touched=[];forEach(j,function(e){if(e.from?e.from.historyTemp:h){for(var g=[],b=e.from,i=true;;){var l=b?b.historyTemp:h;if(!l)if(i)break;else l=d(b);g.unshift(l);a(b,null);if(!b)break;i=f.after(b);b=c(b,"previous")}b=e.to;for(i=f.before(e.from);;){if(!b)break;l=b?b.historyTemp:
h;if(!l)if(i)break;else l=d(b);g.push(l);a(b,null);i=f.before(b);b=c(b,"next")}k.push(g)}});return k},shadowChain:function(a){var d=[],c=this.after(a[0].from);for(a=a[a.length-1].to;;){d.push(c);c=c.to;if(!c||c==a)break;else c=c.historyAfter||this.before(a)}return d},applyChain:function(a){var d=select.cursorPos(this.container,false),c=this,f=a[0].from,h=a[a.length-1].to;(function(i,l){for(var m=i?i.nextSibling:c.container.firstChild;m!=l;){var n=m.nextSibling;removeElement(m);m=n}})(f,h);for(var j=
0;j<a.length;j++){var k=a[j];j>0&&c.container.insertBefore(k.from,h);var e=makePartSpan(fixSpaces(k.text));c.container.insertBefore(e,h);if(d&&d.node==k.from){e=0;var g=this.after(k.from);if(g&&j==a.length-1){for(var b=0;b<d.offset&&k.text.charAt(b)==g.text.charAt(b);b++);if(d.offset>b)e=k.text.length-g.text.length}select.setCursorPos(this.container,{node:k.from,offset:Math.max(0,d.offset+e)})}else d&&j==a.length-1&&d.node&&d.node.parentNode!=this.container&&select.setCursorPos(this.container,{node:k.from,
offset:k.text.length})}this.linkChain(a);return f}};