/**
 * term.js - an xterm emulator
 * Copyright (c) 2012-2013, Christopher Jeffrey (MIT License)
 * https://github.com/chjj/term.js
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 *
 * Originally forked from (with the author's permission):
 *   Fabrice Bellard's javascript vt100 for jslinux:
 *   http://bellard.org/jslinux/
 *   Copyright (c) 2011 Fabrice Bellard
 *   The original design remains. The terminal itself
 *   has been extended to include xterm CSI codes, among
 *   other features.
 */(function(){"use strict";function n(){this._events=this._events||{}}function r(){n.call(this)}function h(e){var n=this;if(!(this instanceof h))return new h(arguments[0],arguments[1],arguments[2]);r.call(this),typeof e=="number"&&(e={cols:arguments[0],rows:arguments[1],handler:arguments[2]}),e=e||{},T(N(h.defaults),function(t){e[t]==null&&(e[t]=h.options[t],h[t]!==h.defaults[t]&&(e[t]=h[t])),n[t]=e[t]}),e.colors.length===8?e.colors=e.colors.concat(h._colors.slice(8)):e.colors.length===16?e.colors=e.colors.concat(h._colors.slice(16)):e.colors.length===10?e.colors=e.colors.slice(0,-2).concat(h._colors.slice(8,-2),e.colors.slice(-2)):e.colors.length===18&&(e.colors=e.colors.slice(0,-2).concat(h._colors.slice(16,-2),e.colors.slice(-2))),this.colors=e.colors,this.options=e,this.parent=e.body||e.parent||(t?t.getElementsByTagName("body")[0]:null),this.cols=e.cols||e.geometry[0],this.rows=e.rows||e.geometry[1],this.setRawMode,this.isTTY=!0,this.isRaw=!0,this.columns=this.cols,this.rows=this.rows,e.handler&&this.on("data",e.handler),this.ybase=0,this.ydisp=0,this.x=0,this.y=0,this.cursorState=0,this.cursorHidden=!1,this.convertEol,this.state=0,this.queue="",this.scrollTop=0,this.scrollBottom=this.rows-1,this.applicationKeypad=!1,this.applicationCursor=!1,this.originMode=!1,this.insertMode=!1,this.wraparoundMode=!1,this.normal=null,this.prefixMode=!1,this.selectMode=!1,this.visualMode=!1,this.searchMode=!1,this.searchDown,this.entry="",this.entryPrefix="Search: ",this._real,this._selected,this._textarea,this.charset=null,this.gcharset=null,this.glevel=0,this.charsets=[null],this.decLocator,this.x10Mouse,this.vt200Mouse,this.vt300Mouse,this.normalMouse,this.mouseEvents,this.sendFocus,this.utfMouse,this.sgrMouse,this.urxvtMouse,this.element,this.children,this.refreshStart,this.refreshEnd,this.savedX,this.savedY,this.savedCols,this.readable=!0,this.writable=!0,this.defAttr=131840,this.curAttr=this.defAttr,this.params=[],this.currentParam=0,this.prefix="",this.postfix="",this.lines=[];var i=this.rows;while(i--)this.lines.push(this.blankLine());this.tabs,this.setupStops()}function p(e,t,n,r){e.addEventListener(t,n,r||!1)}function d(e,t,n,r){e.removeEventListener(t,n,r||!1)}function v(e){return e.preventDefault&&e.preventDefault(),e.returnValue=!1,e.stopPropagation&&e.stopPropagation(),e.cancelBubble=!0,!1}function m(e,t){function n(){this.constructor=e}n.prototype=t.prototype,e.prototype=new n}function g(e){var t=e.getElementsByTagName("body")[0],n=e.createElement("div");n.className="terminal";var r=e.createElement("div"),i=e.createElement("span");i.innerHTML="hello world",r.appendChild(i),n.appendChild(r),t.appendChild(n);var s=i.scrollWidth;i.style.fontWeight="bold";var o=i.scrollWidth;return t.removeChild(n),s!==o}function E(e,t){var n=e.length;while(n--)if(e[n]===t)return n;return-1}function S(e){return e<="＀"?!1:e>="！"&&e<="ﾾ"||e>="ￂ"&&e<="ￇ"||e>="ￊ"&&e<="ￏ"||e>="ￒ"&&e<="ￗ"||e>="ￚ"&&e<="ￜ"||e>="￠"&&e<="￦"||e>="￨"&&e<="￮"}function x(e,t,n){var r=e<<16|t<<8|n;if(x._cache[r]!=null)return x._cache[r];var i=Infinity,s=-1,o=0,u,a,f,l,c;for(;o<h.vcolors.length;o++){u=h.vcolors[o],a=u[0],f=u[1],l=u[2],c=x.distance(e,t,n,a,f,l);if(c===0){s=o;break}c<i&&(i=c,s=o)}return x._cache[r]=s}function T(e,t,n){if(e.forEach)return e.forEach(t,n);for(var r=0;r<e.length;r++)t.call(n,e[r],r,e)}function N(e){if(Object.keys)return Object.keys(e);var t,n=[];for(t in e)Object.prototype.hasOwnProperty.call(e,t)&&n.push(t);return n}var e=this,t=this.document;n.prototype.addListener=function(e,t){this._events[e]=this._events[e]||[],this._events[e].push(t)},n.prototype.on=n.prototype.addListener,n.prototype.removeListener=function(e,t){if(!this._events[e])return;var n=this._events[e],r=n.length;while(r--)if(n[r]===t||n[r].listener===t){n.splice(r,1);return}},n.prototype.off=n.prototype.removeListener,n.prototype.removeAllListeners=function(e){this._events[e]&&delete this._events[e]},n.prototype.once=function(e,t){function n(){var r=Array.prototype.slice.call(arguments);return this.removeListener(e,n),t.apply(this,r)}return n.listener=t,this.on(e,n)},n.prototype.emit=function(e){if(!this._events[e])return;var t=Array.prototype.slice.call(arguments,1),n=this._events[e],r=n.length,i=0;for(;i<r;i++)n[i].apply(this,t)},n.prototype.listeners=function(e){return this._events[e]=this._events[e]||[]},m(r,n),r.prototype.pipe=function(e,t){function o(){n.removeListener("data",r),n.removeListener("error",i),n.removeListener("end",s),e.removeListener("error",i),e.removeListener("close",o)}var n=this,r,i,s;return n.on("data",r=function(t){e.write(t)}),n.on("error",i=function(e){o();if(!this.listeners("error").length)throw e}),n.on("end",s=function(){e.end(),o()}),e.on("error",i),e.on("close",o),e.emit("pipe",n),e};var i=0,s=1,o=2,u=3,a=4,f=5,l=6,c={type:"udk"};m(h,r),h.tangoColors=["#2e3436","#cc0000","#4e9a06","#c4a000","#3465a4","#75507b","#06989a","#d3d7cf","#555753","#ef2929","#8ae234","#fce94f","#729fcf","#ad7fa8","#34e2e2","#eeeeec"],h.xtermColors=["#000000","#cd0000","#00cd00","#cdcd00","#0000ee","#cd00cd","#00cdcd","#e5e5e5","#7f7f7f","#ff0000","#00ff00","#ffff00","#5c5cff","#ff00ff","#00ffff","#ffffff"],h.colors=function(){function r(t,n,r){e.push("#"+i(t)+i(n)+i(r))}function i(e){return e=e.toString(16),e.length<2?"0"+e:e}var e=h.tangoColors.slice(),t=[0,95,135,175,215,255],n;n=0;for(;n<216;n++)r(t[n/36%6|0],t[n/6%6|0],t[n%6]);n=0;for(;n<24;n++)t=8+n*10,r(t,t,t);return e}(),h.colors[256]="#000000",h.colors[257]="#f0f0f0",h._colors=h.colors.slice(),h.vcolors=function(){var e=[],t=h.colors,n=0,r;for(;n<256;n++)r=parseInt(t[n].substring(1),16),e.push([r>>16&255,r>>8&255,r&255]);return e}(),h.defaults={colors:h.colors,convertEol:!1,termName:"xterm",geometry:[80,24],cursorBlink:!0,visualBell:!1,popOnBell:!1,scrollback:1e3,screenKeys:!1,debug:!1,useStyle:!1},h.options={},T(N(h.defaults),function(e){h[e]=h.defaults[e],h.options[e]=h.defaults[e]}),h.focus=null,h.prototype.focus=function(){if(h.focus===this)return;h.focus&&h.focus.blur(),this.sendFocus&&this.send("[I"),this.showCursor(),h.focus=this},h.prototype.blur=function(){if(h.focus!==this)return;this.cursorState=0,this.refresh(this.y,this.y),this.sendFocus&&this.send("[O"),h.focus=null},h.prototype.initGlobal=function(){var e=this.document;h._boundDocs=h._boundDocs||[];if(~E(h._boundDocs,e))return;h._boundDocs.push(e),h.bindPaste(e),h.bindKeys(e),h.bindCopy(e),this.isMobile&&this.fixMobile(e),this.useStyle&&h.insertStyle(e,this.colors[256],this.colors[257])},h.bindPaste=function(e){var t=e.defaultView;p(t,"paste",function(e){var t=h.focus;if(!t)return;return e.clipboardData?t.send(e.clipboardData.getData("text/plain")):t.context.clipboardData&&t.send(t.context.clipboardData.getData("Text")),t.element.contentEditable="inherit",v(e)})},h.bindKeys=function(e){p(e,"keydown",function(e){if(!h.focus)return;var t=e.target||e.srcElement;if(!t)return;if(t===h.focus.element||t===h.focus.context||t===h.focus.document||t===h.focus.body||t===h._textarea||t===h.focus.parent)return h.focus.keyDown(e)},!0),p(e,"keypress",function(e){if(!h.focus)return;var t=e.target||e.srcElement;if(!t)return;if(e.ctrlKey&&e.key==="v")return;if(t===h.focus.element||t===h.focus.context||t===h.focus.document||t===h.focus.body||t===h._textarea||t===h.focus.parent)return h.focus.element.contentEditable="inherit",h.focus.keyPress(e)},!0),p(e,"mousedown",function(e){if(!h.focus)return;var t=e.target||e.srcElement;if(!t)return;do if(t===h.focus.element)return;while(t=t.parentNode);h.focus.blur()})},h.bindCopy=function(e){var t=e.defaultView;p(t,"copy",function(e){var t=h.focus;if(!t)return;if(!t._selected)return;var n=t.getCopyTextarea(),r=t.grabText(t._selected.x1,t._selected.x2,t._selected.y1,t._selected.y2);t.emit("copy",r),n.focus(),n.textContent=r,n.value=r,n.setSelectionRange(0,r.length),b(function(){t.element.focus(),t.focus()},1)})},h.prototype.fixMobile=function(e){var t=this,n=e.createElement("textarea");n.style.position="absolute",n.style.left="-32000px",n.style.top="-32000px",n.style.width="0px",n.style.height="0px",n.style.opacity="0",n.style.backgroundColor="transparent",n.style.borderStyle="none",n.style.outlineStyle="none",n.autocapitalize="none",n.autocorrect="off",e.getElementsByTagName("body")[0].appendChild(n),h._textarea=n,b(function(){n.focus()},1e3),this.isAndroid&&p(n,"change",function(){var e=n.textContent||n.value;n.value="",n.textContent="",t.send(e+"\r")})},h.insertStyle=function(e,t,n){var r=e.getElementById("term-style");if(r)return;var i=e.getElementsByTagName("head")[0];if(!i)return;var r=e.createElement("style");r.id="term-style",r.innerHTML=".terminal {\n  float: left;\n  border: "+t+" solid 5px;\n"+'  font-family: "DejaVu Sans Mono", "Liberation Mono", monospace;\n'+"  font-size: 11px;\n"+"  color: "+n+";\n"+"  background: "+t+";\n"+"}\n"+"\n"+".terminal-cursor {\n"+"  color: "+t+";\n"+"  background: "+n+";\n"+"}\n",i.insertBefore(r,i.firstChild)},h.prototype.open=function(t){var n=this,r=0,i;this.parent=t||this.parent;if(!this.parent)throw new Error("Terminal requires a parent element.");this.context=this.parent.ownerDocument.defaultView,this.document=this.parent.ownerDocument,this.body=this.document.getElementsByTagName("body")[0],this.context.navigator&&this.context.navigator.userAgent&&(this.isMac=!!~this.context.navigator.userAgent.indexOf("Mac"),this.isIpad=!!~this.context.navigator.userAgent.indexOf("iPad"),this.isIphone=!!~this.context.navigator.userAgent.indexOf("iPhone"),this.isAndroid=!!~this.context.navigator.userAgent.indexOf("Android"),this.isMobile=this.isIpad||this.isIphone||this.isAndroid,this.isMSIE=!!~this.context.navigator.userAgent.indexOf("MSIE"),this.isFirefox=!!~this.context.navigator.userAgent.indexOf("Firefox")),this.element=this.document.createElement("div"),this.element.className="terminal",this.element.style.outline="none",this.element.setAttribute("tabindex",0),this.element.setAttribute("spellcheck","false"),this.element.style.backgroundColor=this.colors[256],this.element.style.color=this.colors[257],this.children=[];for(;r<this.rows;r++)i=this.document.createElement("div"),this.element.appendChild(i),this.children.push(i);this.parent.appendChild(this.element),this.refresh(0,this.rows-1),(!("useEvents"in this.options)||this.options.useEvents)&&this.initGlobal();if(!("useFocus"in this.options)||this.options.useFocus)this.focus(),this.startBlink(),p(this.element,"focus",function(){n.focus(),n.isMobile&&h._textarea.focus()}),p(this.element,"mousedown",function(){n.focus()}),p(this.element,"mousedown",function(t){var r=t.button!=null?+t.button:t.which!=null?t.which-1:null;n.isMSIE&&(r=r===1?0:r===4?1:r);if(r!==1&&r!==2){n.element.contentEditable="inherit";return}n.element.contentEditable="true",n.isFirefox&&e.getSelection().collapse(n.element,0),n.isFirefox||b(function(){n.element.contentEditable="inherit"},1)},!0);(!("useMouse"in this.options)||this.options.useMouse)&&this.bindMouse(),(!("useFocus"in this.options)||this.options.useFocus)&&b(function(){n.element.focus()},100),h.brokenBold==null&&(h.brokenBold=g(this.document)),this.emit("open")},h.prototype.setRawMode=function(e){this.isRaw=!!e},h.prototype.bindMouse=function(){function i(e){var t,i;t=a(e),i=f(e);if(!i)return;u(t,i);switch(e.type){case"mousedown":n=t;break;case"mouseup":n=32;break;case r:}}function s(e){var t=n,r;r=f(e);if(!r)return;t+=32,u(t,r)}function o(e,n){if(!t.utfMouse){if(n===255)return e.push(0);n>127&&(n=127),e.push(n)}else{if(n===2047)return e.push(0);n<127?e.push(n):(n>2047&&(n=2047),e.push(192|n>>6),e.push(128|n&63))}}function u(e,n){if(t.vt300Mouse){e&=3,n.x-=32,n.y-=32;var r="[24";if(e===0)r+="1";else if(e===1)r+="3";else if(e===2)r+="5";else{if(e===3)return;r+="0"}r+="~["+n.x+","+n.y+"]\r",t.send(r);return}if(t.decLocator){e&=3,n.x-=32,n.y-=32,e===0?e=2:e===1?e=4:e===2?e=6:e===3&&(e=3),t.send("["+e+";"+(e===3?4:0)+";"+n.y+";"+n.x+";"+(n.page||0)+"&w");return}if(t.urxvtMouse){n.x-=32,n.y-=32,n.x++,n.y++,t.send("["+e+";"+n.x+";"+n.y+"M");return}if(t.sgrMouse){n.x-=32,n.y-=32,t.send("[<"+((e&3)===3?e&-4:e)+";"+n.x+";"+n.y+((e&3)===3?"m":"M"));return}var r=[];o(r,e),o(r,n.x),o(r,n.y),t.send("[M"+y.fromCharCode.apply(y,r))}function a(e){var n,r,i,s,o;switch(e.type){case"mousedown":n=e.button!=null?+e.button:e.which!=null?e.which-1:null,t.isMSIE&&(n=n===1?0:n===4?1:n);break;case"mouseup":n=3;break;case"DOMMouseScroll":n=e.detail<0?64:65;break;case"mousewheel":n=e.wheelDeltaY>0?64:65}return r=e.shiftKey?4:0,i=e.metaKey?8:0,s=e.ctrlKey?16:0,o=r|i|s,t.vt200Mouse?o&=s:t.normalMouse||(o=0),n=32+(o<<2)+n,n}function f(e){var n,i,s,o,u;if(e.pageX==null)return;n=e.pageX,i=e.pageY,u=t.element;while(u&&u!==t.document.documentElement)n-=u.offsetLeft,i-=u.offsetTop,u="offsetParent"in u?u.offsetParent:u.parentNode;return s=t.element.clientWidth,o=t.element.clientHeight,n=Math.round(n/s*t.cols),i=Math.round(i/o*t.rows),n<0&&(n=0),n>t.cols&&(n=t.cols),i<0&&(i=0),i>t.rows&&(i=t.rows),n+=32,i+=32,{x:n,y:i,type:e.type===r?"mousewheel":e.type}}var e=this.element,t=this,n=32,r="onmousewheel"in this.context?"mousewheel":"DOMMouseScroll";p(e,"mousedown",function(e){if(!t.mouseEvents)return;return i(e),t.focus(),t.normalMouse&&p(t.document,"mousemove",s),t.x10Mouse||p(t.document,"mouseup",function n(e){return i(e),t.normalMouse&&d(t.document,"mousemove",s),d(t.document,"mouseup",n),v(e)}),v(e)}),p(e,r,function(e){if(!t.mouseEvents)return;if(t.x10Mouse||t.vt300Mouse||t.decLocator)return;return i(e),v(e)}),p(e,r,function(e){if(t.mouseEvents)return;if(t.applicationKeypad)return;return e.type==="DOMMouseScroll"?t.scrollDisp(e.detail<0?-5:5):t.scrollDisp(e.wheelDeltaY>0?-5:5),v(e)})},h.prototype.close=h.prototype.destroySoon=h.prototype.destroy=function(){if(this.destroyed)return;this._blink&&(clearInterval(this._blink),delete this._blink),this.readable=!1,this.writable=!1,this.destroyed=!0,this._events={},this.handler=function(){},this.write=function(){},this.end=function(){},this.element.parentNode&&this.element.parentNode.removeChild(this.element),this.emit("end"),this.emit("close"),this.emit("finish"),this.emit("destroy")},h.prototype.refresh=function(e,t){var n,r,i,s,o,u,a,f,l,c,p,d,v,m;t-e>=this.rows/2&&(m=this.element.parentNode,m&&m.removeChild(this.element)),a=this.cols,r=e,t>=this.lines.length&&(this.log("`end` is too large. Most likely a bad CSR."),t=this.lines.length-1);for(;r<=t;r++){v=r+this.ydisp,s=this.lines[v],o="",r===this.y&&this.cursorState&&(this.ydisp===this.ybase||this.selectMode)&&!this.cursorHidden?n=this.x:n=-1,l=this.defAttr,i=0;for(;i<a;i++){f=s[i][0],u=s[i][1],i===n&&(f=-1),f!==l&&(l!==this.defAttr&&(o+="</span>"),f!==this.defAttr&&(f===-1?o+='<span class="reverse-video terminal-cursor">':(o+='<span style="',c=f&511,p=f>>9&511,d=f>>18,d&1&&(h.brokenBold||(o+="font-weight:bold;"),p<8&&(p+=8)),d&2&&(o+="text-decoration:underline;"),d&4&&(d&2?(o=o.slice(0,-1),o+=" blink;"):o+="text-decoration:blink;"),d&8&&(c=f>>9&511,p=f&511,d&1&&p<8&&(p+=8)),d&16&&(o+="visibility:hidden;"),c!==256&&(o+="background-color:"+this.colors[c]+";"),p!==257&&(o+="color:"+this.colors[p]+";"),o+='">')));switch(u){case"&":o+="&amp;";break;case"<":o+="&lt;";break;case">":o+="&gt;";break;default:u<=" "?o+="&nbsp;":(S(u)&&i++,o+=u)}l=f}l!==this.defAttr&&(o+="</span>"),this.children[r].innerHTML=o}m&&m.appendChild(this.element)},h.prototype._cursorBlink=function(){if(h.focus!==this)return;this.cursorState^=1,this.refresh(this.y,this.y)},h.prototype.showCursor=function(){this.cursorState||(this.cursorState=1,this.refresh(this.y,this.y))},h.prototype.startBlink=function(){if(!this.cursorBlink)return;var e=this;this._blinker=function(){e._cursorBlink()},this._blink=w(this._blinker,500)},h.prototype.refreshBlink=function(){if(!this.cursorBlink||!this._blink)return;clearInterval(this._blink),this._blink=w(this._blinker,500)},h.prototype.scroll=function(){var e;++this.ybase===this.scrollback&&(this.ybase=this.ybase/2|0,this.lines=this.lines.slice(-(this.ybase+this.rows)+1)),this.ydisp=this.ybase,e=this.ybase+this.rows-1,e-=this.rows-1-this.scrollBottom,e===this.lines.length?this.lines.push(this.blankLine()):this.lines.splice(e,0,this.blankLine()),this.scrollTop!==0&&(this.ybase!==0&&(this.ybase--,this.ydisp=this.ybase),this.lines.splice(this.ybase+this.scrollTop,1)),this.updateRange(this.scrollTop),this.updateRange(this.scrollBottom)},h.prototype.scrollDisp=function(e){this.ydisp+=e,this.ydisp>this.ybase?this.ydisp=this.ybase:this.ydisp<0&&(this.ydisp=0),this.refresh(0,this.rows-1)},h.prototype.write=function(e){var t=e.length,n=0,r,p,d;this.refreshStart=this.y,this.refreshEnd=this.y,this.ybase!==this.ydisp&&(this.ydisp=this.ybase,this.maxRange());for(;n<t;n++,this.lch=d){d=e[n];switch(this.state){case i:switch(d){case"":this.bell();break;case"\n":case"":case"\f":this.convertEol&&(this.x=0),this.y++,this.y>this.scrollBottom&&(this.y--,this.scroll());break;case"\r":this.x=0;break;case"\b":this.x>0&&this.x--;break;case"	":this.x=this.nextStop();break;case"":this.setgLevel(1);break;case"":this.setgLevel(0);break;case"":this.state=s;break;default:if(d>=" "){this.charset&&this.charset[d]&&(d=this.charset[d]),this.x>=this.cols&&(this.x=0,this.y++,this.y>this.scrollBottom&&(this.y--,this.scroll())),this.lines[this.y+this.ybase][this.x]=[this.curAttr,d],this.x++,this.updateRange(this.y);if(S(d)){r=this.y+this.ybase;if(this.cols<2||this.x>=this.cols){this.lines[r][this.x-1]=[this.curAttr," "];break}this.lines[r][this.x]=[this.curAttr," "],this.x++}}}break;case s:switch(d){case"[":this.params=[],this.currentParam=0,this.state=o;break;case"]":this.params=[],this.currentParam=0,this.state=u;break;case"P":this.params=[],this.prefix="",this.currentParam="",this.state=f;break;case"_":this.state=l;break;case"^":this.state=l;break;case"c":this.reset();break;case"E":this.x=0;case"D":this.index();break;case"M":this.reverseIndex();break;case"%":this.setgLevel(0),this.setgCharset(0,h.charsets.US),this.state=i,n++;break;case"(":case")":case"*":case"+":case"-":case".":switch(d){case"(":this.gcharset=0;break;case")":this.gcharset=1;break;case"*":this.gcharset=2;break;case"+":this.gcharset=3;break;case"-":this.gcharset=1;break;case".":this.gcharset=2}this.state=a;break;case"/":this.gcharset=3,this.state=a,n--;break;case"N":break;case"O":break;case"n":this.setgLevel(2);break;case"o":this.setgLevel(3);break;case"|":this.setgLevel(3);break;case"}":this.setgLevel(2);break;case"~":this.setgLevel(1);break;case"7":this.saveCursor(),this.state=i;break;case"8":this.restoreCursor(),this.state=i;break;case"#":this.state=i,n++;break;case"H":this.tabSet();break;case"=":this.log("Serial port requested application keypad."),this.applicationKeypad=!0,this.state=i;break;case">":this.log("Switching back to normal keypad."),this.applicationKeypad=!1,this.state=i;break;default:this.state=i,this.error("Unknown ESC control: %s.",d)}break;case a:switch(d){case"0":p=h.charsets.SCLD;break;case"A":p=h.charsets.UK;break;case"B":p=h.charsets.US;break;case"4":p=h.charsets.Dutch;break;case"C":case"5":p=h.charsets.Finnish;break;case"R":p=h.charsets.French;break;case"Q":p=h.charsets.FrenchCanadian;break;case"K":p=h.charsets.German;break;case"Y":p=h.charsets.Italian;break;case"E":case"6":p=h.charsets.NorwegianDanish;break;case"Z":p=h.charsets.Spanish;break;case"H":case"7":p=h.charsets.Swedish;break;case"=":p=h.charsets.Swiss;break;case"/":p=h.charsets.ISOLatin,n++;break;default:p=h.charsets.US}this.setgCharset(this.gcharset,p),this.gcharset=null,this.state=i;break;case u:if(this.lch===""&&d==="\\"||d===""){this.lch===""&&(typeof this.currentParam=="string"?this.currentParam=this.currentParam.slice(0,-1):typeof this.currentParam=="number"&&(this.currentParam=(this.currentParam-("".charCodeAt(0)-48))/10)),this.params.push(this.currentParam);switch(this.params[0]){case 0:case 1:case 2:this.params[1]&&(this.title=this.params[1],this.handleTitle(this.title));break;case 3:break;case 4:case 5:break;case 10:case 11:case 12:case 13:case 14:case 15:case 16:case 17:case 18:case 19:break;case 46:break;case 50:break;case 51:break;case 52:break;case 104:case 105:case 110:case 111:case 112:case 113:case 114:case 115:case 116:case 117:case 118:}this.params=[],this.currentParam=0,this.state=i}else this.params.length?this.currentParam+=d:d>="0"&&d<="9"?this.currentParam=this.currentParam*10+d.charCodeAt(0)-48:d===";"&&(this.params.push(this.currentParam),this.currentParam="");break;case o:if(d==="?"||d===">"||d==="!"){this.prefix=d;break}if(d>="0"&&d<="9"){this.currentParam=this.currentParam*10+d.charCodeAt(0)-48;break}if(d==="$"||d==='"'||d===" "||d==="'"){this.postfix=d;break}this.params.push(this.currentParam),this.currentParam=0;if(d===";")break;this.state=i;switch(d){case"A":this.cursorUp(this.params);break;case"B":this.cursorDown(this.params);break;case"C":this.cursorForward(this.params);break;case"D":this.cursorBackward(this.params);break;case"H":this.cursorPos(this.params);break;case"J":this.eraseInDisplay(this.params);break;case"K":this.eraseInLine(this.params);break;case"m":this.prefix||this.charAttributes(this.params);break;case"n":this.prefix||this.deviceStatus(this.params);break;case"@":this.insertChars(this.params);break;case"E":this.cursorNextLine(this.params);break;case"F":this.cursorPrecedingLine(this.params);break;case"G":this.cursorCharAbsolute(this.params);break;case"L":this.insertLines(this.params);break;case"M":this.deleteLines(this.params);break;case"P":this.deleteChars(this.params);break;case"X":this.eraseChars(this.params);break;case"`":this.charPosAbsolute(this.params);break;case"a":this.HPositionRelative(this.params);break;case"c":this.sendDeviceAttributes(this.params);break;case"d":this.linePosAbsolute(this.params);break;case"e":this.VPositionRelative(this.params);break;case"f":this.HVPosition(this.params);break;case"h":this.setMode(this.params);break;case"l":this.resetMode(this.params);break;case"r":this.setScrollRegion(this.params);break;case"s":this.saveCursor(this.params);break;case"u":this.restoreCursor(this.params);break;case"I":this.cursorForwardTab(this.params);break;case"S":this.scrollUp(this.params);break;case"T":this.params.length<2&&!this.prefix&&this.scrollDown(this.params);break;case"Z":this.cursorBackwardTab(this.params);break;case"b":this.repeatPrecedingCharacter(this.params);break;case"g":this.tabClear(this.params);break;case"p":switch(this.prefix){case"!":this.softReset(this.params)}break;default:this.error("Unknown CSI code: %s.",d)}this.prefix="",this.postfix="";break;case f:if(this.lch===""&&d==="\\"||d===""){if(this.prefix==="tmux;"&&d===""){this.currentParam+=d;continue}this.lch===""&&(typeof this.currentParam=="string"?this.currentParam=this.currentParam.slice(0,-1):typeof this.currentParam=="number"&&(this.currentParam=(this.currentParam-("".charCodeAt(0)-48))/10)),this.params.push(this.currentParam);var v=this.params[this.params.length-1];switch(this.prefix){case c:this.emit("udk",{clearAll:this.params[0]===0,eraseBelow:this.params[0]===1,lockKeys:this.params[1]===0,dontLockKeys:this.params[1]===1,keyList:(this.params[2]+"").split(";").map(function(e){return e=e.split("/"),{keyCode:e[0],hexKeyValue:e[1]}})});break;case"$q":var m=0;switch(v){case'"q':v='0"q',m=1;break;case'"p':v='61;0"p',m=1;break;case"r":v=""+(this.scrollTop+1)+";"+(this.scrollBottom+1)+"r",m=1;break;case"m":m=0;break;default:this.error("Unknown DCS Pt: %s.",v),m=0}this.send("P"+m+"$r"+v+"\\");break;case"+p":this.emit("set terminfo",{name:this.params[0]});break;case"+q":var m=!1;this.send("P"+ +m+"+r"+v+"\\");break;case"tmux;":this.emit("passthrough",v);break;default:this.error("Unknown DCS prefix: %s.",v)}this.currentParam=0,this.prefix="",this.state=i}else{this.currentParam+=d;if(!this.prefix)if(/^\d*;\d*\|/.test(this.currentParam))this.prefix=c,this.params=this.currentParam.split(/[;|]/).map(function(e){return e.length?+e:0}).slice(0,-1),this.currentParam="";else if(/^[$+][a-zA-Z]/.test(this.currentParam)||/^\w+;\x1b/.test(this.currentParam))this.prefix=this.currentParam,this.currentParam=""}break;case l:if(this.lch===""&&d==="\\"||d==="")this.state=i}}return this.updateRange(this.y),this.refresh(this.refreshStart,this.refreshEnd),!0},h.prototype.writeln=function(e){return this.write(e+"\r\n")},h.prototype.end=function(e){var t=!0;return e&&(t=this.write(e)),this.destroySoon(),t},h.prototype.resume=function(){},h.prototype.pause=function(){},h.prototype.keyDown=function(e){var t=this,n;switch(e.keyCode){case 8:if(e.altKey){n="";break}if(e.shiftKey){n="\b";break}n="";break;case 9:if(e.shiftKey){n="[Z";break}n="	";break;case 13:n="\r";break;case 27:n="";break;case 32:n=" ";break;case 37:if(this.applicationCursor){n="OD";break}if(e.ctrlKey){n="[5D";break}n="[D";break;case 39:if(this.applicationCursor){n="OC";break}if(e.ctrlKey){n="[5C";break}n="[C";break;case 38:if(this.applicationCursor){n="OA";break}if(e.ctrlKey)return this.scrollDisp(-1),v(e);n="[A";break;case 40:if(this.applicationCursor){n="OB";break}if(e.ctrlKey)return this.scrollDisp(1),v(e);n="[B";break;case 46:n="[3~";break;case 45:n="[2~";break;case 36:if(this.applicationKeypad){n="OH";break}n="OH";break;case 35:if(this.applicationKeypad){n="OF";break}n="OF";break;case 33:if(e.shiftKey)return this.scrollDisp(-(this.rows-1)),v(e);n="[5~";break;case 34:if(e.shiftKey)return this.scrollDisp(this.rows-1),v(e);n="[6~";break;case 112:n="OP";break;case 113:n="OQ";break;case 114:n="OR";break;case 115:n="OS";break;case 116:n="[15~";break;case 117:n="[17~";break;case 118:n="[18~";break;case 119:n="[19~";break;case 120:n="[20~";break;case 121:n="[21~";break;case 122:n="[23~";break;case 123:n="[24~";break;default:if(e.ctrlKey)if(e.keyCode>=65&&e.keyCode<=90){if(this.screenKeys&&!this.prefixMode&&!this.selectMode&&e.keyCode===65)return this.enterPrefix(),v(e);if(this.prefixMode&&e.keyCode===86){this.leavePrefix();var r=h.focus;r.element.contentEditable="true";return}if((this.prefixMode||this.selectMode)&&e.keyCode===67){this.visualMode&&b(function(){t.leaveVisual()},1);return}n=y.fromCharCode(e.keyCode-64)}else e.keyCode===32?n=y.fromCharCode(0):e.keyCode>=51&&e.keyCode<=55?n=y.fromCharCode(e.keyCode-51+27):e.keyCode===56?n=y.fromCharCode(127):e.keyCode===219?n=y.fromCharCode(27):e.keyCode===221&&(n=y.fromCharCode(29));else e.altKey&&(e.keyCode>=65&&e.keyCode<=90?n=""+y.fromCharCode(e.keyCode+32):e.keyCode===192?n="`":e.keyCode>=48&&e.keyCode<=57&&(n=""+(e.keyCode-48)))}return n?this.prefixMode?(this.leavePrefix(),v(e)):this.selectMode?(this.keySelect(e,n),v(e)):(this.emit("keydown",e),this.emit("key",n,e),this.showCursor(),this.handler(n),v(e)):!0},h.prototype.setgLevel=function(e){this.glevel=e,this.charset=this.charsets[e]},h.prototype.setgCharset=function(e,t){this.charsets[e]=t,this.glevel===e&&(this.charset=t)},h.prototype.keyPress=function(e){var t;v(e);if(e.charCode)t=e.charCode;else if(e.which==null)t=e.keyCode;else{if(e.which===0||e.charCode===0)return!1;t=e.which}return!t||e.ctrlKey||e.altKey||e.metaKey?!1:(t=y.fromCharCode(t),this.prefixMode?(this.leavePrefix(),this.keyPrefix(e,t),!1):this.selectMode?(this.keySelect(e,t),!1):(this.emit("keypress",t,e),this.emit("key",t,e),this.showCursor(),this.handler(t),!1))},h.prototype.send=function(e){var t=this;this.queue||b(function(){t.handler(t.queue),t.queue=""},1),this.queue+=e},h.prototype.bell=function(){this.emit("bell");if(!this.visualBell)return;var e=this;this.element.style.borderColor="white",b(function(){e.element.style.borderColor=""},10),this.popOnBell&&this.focus()},h.prototype.log=function(){if(!this.debug)return;if(!this.context.console||!this.context.console.log)return;var e=Array.prototype.slice.call(arguments);this.context.console.log.apply(this.context.console,e)},h.prototype.error=function(){if(!this.debug)return;if(!this.context.console||!this.context.console.error)return;var e=Array.prototype.slice.call(arguments);this.context.console.error.apply(this.context.console,e)},h.prototype.resize=function(e,t){var n,r,i,s,o;e<1&&(e=1),t<1&&(t=1),s=this.cols;if(s<e){o=[this.defAttr," "],i=this.lines.length;while(i--)while(this.lines[i].length<e)this.lines[i].push(o)}else if(s>e){i=this.lines.length;while(i--)while(this.lines[i].length>e)this.lines[i].pop()}this.setupStops(s),this.cols=e,this.columns=e,s=this.rows;if(s<t){r=this.element;while(s++<t)this.lines.length<t+this.ybase&&this.lines.push(this.blankLine()),this.children.length<t&&(n=this.document.createElement("div"),r.appendChild(n),this.children.push(n))}else if(s>t)while(s-->t){this.lines.length>t+this.ybase&&this.lines.pop();if(this.children.length>t){r=this.children.pop();if(!r)continue;r.parentNode.removeChild(r)}}this.rows=t,this.y>=t&&(this.y=t-1),this.x>=e&&(this.x=e-1),this.scrollTop=0,this.scrollBottom=t-1,this.refresh(0,this.rows-1),this.normal=null,this.emit("resize")},h.prototype.updateRange=function(e){e<this.refreshStart&&(this.refreshStart=e),e>this.refreshEnd&&(this.refreshEnd=e)},h.prototype.maxRange=function(){this.refreshStart=0,this.refreshEnd=this.rows-1},h.prototype.setupStops=function(e){e!=null?this.tabs[e]||(e=this.prevStop(e)):(this.tabs={},e=0);for(;e<this.cols;e+=8)this.tabs[e]=!0},h.prototype.prevStop=function(e){e==null&&(e=this.x);while(!this.tabs[--e]&&e>0);return e>=this.cols?this.cols-1:e<0?0:e},h.prototype.nextStop=function(e){e==null&&(e=this.x);while(!this.tabs[++e]&&e<this.cols);return e>=this.cols?this.cols-1:e<0?0:e},h.prototype.eraseAttr=function(){return this.defAttr&-512|this.curAttr&511},h.prototype.eraseRight=function(e,t){var n=this.lines[this.ybase+t],r=[this.eraseAttr()," "];for(;e<this.cols;e++)n[e]=r;this.updateRange(t)},h.prototype.eraseLeft=function(e,t){var n=this.lines[this.ybase+t],r=[this.eraseAttr()," "];e++;while(e--)n[e]=r;this.updateRange(t)},h.prototype.eraseLine=function(e){this.eraseRight(0,e)},h.prototype.blankLine=function(e){var t=e?this.eraseAttr():this.defAttr,n=[t," "],r=[],i=0;for(;i<this.cols;i++)r[i]=n;return r},h.prototype.ch=function(e){return e?[this.eraseAttr()," "]:[this.defAttr," "]},h.prototype.is=function(e){var t=this.termName;return(t+"").indexOf(e)===0},h.prototype.handler=function(e){this.emit("data",e)},h.prototype.handleTitle=function(e){this.emit("title",e)},h.prototype.index=function(){this.y++,this.y>this.scrollBottom&&(this.y--,this.scroll()),this.state=i},h.prototype.reverseIndex=function(){var e;this.y--,this.y<this.scrollTop&&(this.y++,this.lines.splice(this.y+this.ybase,0,this.blankLine(!0)),e=this.rows-1-this.scrollBottom,this.lines.splice(this.rows-1+this.ybase-e+1,1),this.updateRange(this.scrollTop),this.updateRange(this.scrollBottom)),this.state=i},h.prototype.reset=function(){this.options.rows=this.rows,this.options.cols=this.cols,h.call(this,this.options),this.refresh(0,this.rows-1)},h.prototype.tabSet=function(){this.tabs[this.x]=!0,this.state=i},h.prototype.cursorUp=function(e){var t=e[0];t<1&&(t=1),this.y-=t,this.y<0&&(this.y=0)},h.prototype.cursorDown=function(e){var t=e[0];t<1&&(t=1),this.y+=t,this.y>=this.rows&&(this.y=this.rows-1)},h.prototype.cursorForward=function(e){var t=e[0];t<1&&(t=1),this.x+=t,this.x>=this.cols&&(this.x=this.cols-1)},h.prototype.cursorBackward=function(e){var t=e[0];t<1&&(t=1),this.x-=t,this.x<0&&(this.x=0)},h.prototype.cursorPos=function(e){var t,n;t=e[0]-1,e.length>=2?
n=e[1]-1:n=0,t<0?t=0:t>=this.rows&&(t=this.rows-1),n<0?n=0:n>=this.cols&&(n=this.cols-1),this.x=n,this.y=t},h.prototype.eraseInDisplay=function(e){var t;switch(e[0]){case 0:this.eraseRight(this.x,this.y),t=this.y+1;for(;t<this.rows;t++)this.eraseLine(t);break;case 1:this.eraseLeft(this.x,this.y),t=this.y;while(t--)this.eraseLine(t);break;case 2:t=this.rows;while(t--)this.eraseLine(t);break;case 3:}},h.prototype.eraseInLine=function(e){switch(e[0]){case 0:this.eraseRight(this.x,this.y);break;case 1:this.eraseLeft(this.x,this.y);break;case 2:this.eraseLine(this.y)}},h.prototype.charAttributes=function(e){if(e.length===1&&e[0]===0){this.curAttr=this.defAttr;return}var t=e.length,n=0,r=this.curAttr>>18,i=this.curAttr>>9&511,s=this.curAttr&511,o;for(;n<t;n++)o=e[n],o>=30&&o<=37?i=o-30:o>=40&&o<=47?s=o-40:o>=90&&o<=97?(o+=8,i=o-90):o>=100&&o<=107?(o+=8,s=o-100):o===0?(r=this.defAttr>>18,i=this.defAttr>>9&511,s=this.defAttr&511):o===1?r|=1:o===4?r|=2:o===5?r|=4:o===7?r|=8:o===8?r|=16:o===22?r&=-2:o===24?r&=-3:o===25?r&=-5:o===27?r&=-9:o===28?r&=-17:o===39?i=this.defAttr>>9&511:o===49?s=this.defAttr&511:o===38?e[n+1]===2?(n+=2,i=x(e[n]&255,e[n+1]&255,e[n+2]&255),i===-1&&(i=511),n+=2):e[n+1]===5&&(n+=2,o=e[n]&255,i=o):o===48?e[n+1]===2?(n+=2,s=x(e[n]&255,e[n+1]&255,e[n+2]&255),s===-1&&(s=511),n+=2):e[n+1]===5&&(n+=2,o=e[n]&255,s=o):o===100?(i=this.defAttr>>9&511,s=this.defAttr&511):this.error("Unknown SGR attribute: %d.",o);this.curAttr=r<<18|i<<9|s},h.prototype.deviceStatus=function(e){if(!this.prefix)switch(e[0]){case 5:this.send("[0n");break;case 6:this.send("["+(this.y+1)+";"+(this.x+1)+"R")}else if(this.prefix==="?")switch(e[0]){case 6:this.send("[?"+(this.y+1)+";"+(this.x+1)+"R");break;case 15:break;case 25:break;case 26:break;case 53:}},h.prototype.insertChars=function(e){var t,n,r,i;t=e[0],t<1&&(t=1),n=this.y+this.ybase,r=this.x,i=[this.eraseAttr()," "];while(t--&&r<this.cols)this.lines[n].splice(r++,0,i),this.lines[n].pop()},h.prototype.cursorNextLine=function(e){var t=e[0];t<1&&(t=1),this.y+=t,this.y>=this.rows&&(this.y=this.rows-1),this.x=0},h.prototype.cursorPrecedingLine=function(e){var t=e[0];t<1&&(t=1),this.y-=t,this.y<0&&(this.y=0),this.x=0},h.prototype.cursorCharAbsolute=function(e){var t=e[0];t<1&&(t=1),this.x=t-1},h.prototype.insertLines=function(e){var t,n,r;t=e[0],t<1&&(t=1),n=this.y+this.ybase,r=this.rows-1-this.scrollBottom,r=this.rows-1+this.ybase-r+1;while(t--)this.lines.splice(n,0,this.blankLine(!0)),this.lines.splice(r,1);this.updateRange(this.y),this.updateRange(this.scrollBottom)},h.prototype.deleteLines=function(e){var t,n,r;t=e[0],t<1&&(t=1),n=this.y+this.ybase,r=this.rows-1-this.scrollBottom,r=this.rows-1+this.ybase-r;while(t--)this.lines.splice(r+1,0,this.blankLine(!0)),this.lines.splice(n,1);this.updateRange(this.y),this.updateRange(this.scrollBottom)},h.prototype.deleteChars=function(e){var t,n,r;t=e[0],t<1&&(t=1),n=this.y+this.ybase,r=[this.eraseAttr()," "];while(t--)this.lines[n].splice(this.x,1),this.lines[n].push(r)},h.prototype.eraseChars=function(e){var t,n,r,i;t=e[0],t<1&&(t=1),n=this.y+this.ybase,r=this.x,i=[this.eraseAttr()," "];while(t--&&r<this.cols)this.lines[n][r++]=i},h.prototype.charPosAbsolute=function(e){var t=e[0];t<1&&(t=1),this.x=t-1,this.x>=this.cols&&(this.x=this.cols-1)},h.prototype.HPositionRelative=function(e){var t=e[0];t<1&&(t=1),this.x+=t,this.x>=this.cols&&(this.x=this.cols-1)},h.prototype.sendDeviceAttributes=function(e){if(e[0]>0)return;this.prefix?this.prefix===">"&&(this.is("xterm")?this.send("[>0;276;0c"):this.is("rxvt-unicode")?this.send("[>85;95;0c"):this.is("linux")?this.send(e[0]+"c"):this.is("screen")&&this.send("[>83;40003;0c")):this.is("xterm")||this.is("rxvt-unicode")||this.is("screen")?this.send("[?1;2c"):this.is("linux")&&this.send("[?6c")},h.prototype.linePosAbsolute=function(e){var t=e[0];t<1&&(t=1),this.y=t-1,this.y>=this.rows&&(this.y=this.rows-1)},h.prototype.VPositionRelative=function(e){var t=e[0];t<1&&(t=1),this.y+=t,this.y>=this.rows&&(this.y=this.rows-1)},h.prototype.HVPosition=function(e){e[0]<1&&(e[0]=1),e[1]<1&&(e[1]=1),this.y=e[0]-1,this.y>=this.rows&&(this.y=this.rows-1),this.x=e[1]-1,this.x>=this.cols&&(this.x=this.cols-1)},h.prototype.setMode=function(e){if(typeof e=="object"){var t=e.length,n=0;for(;n<t;n++)this.setMode(e[n]);return}if(!this.prefix)switch(e){case 4:this.insertMode=!0;break;case 20:}else if(this.prefix==="?")switch(e){case 1:this.applicationCursor=!0;break;case 2:this.setgCharset(0,h.charsets.US),this.setgCharset(1,h.charsets.US),this.setgCharset(2,h.charsets.US),this.setgCharset(3,h.charsets.US);break;case 3:this.savedCols=this.cols,this.resize(132,this.rows);break;case 6:this.originMode=!0;break;case 7:this.wraparoundMode=!0;break;case 12:break;case 66:this.log("Serial port requested application keypad."),this.applicationKeypad=!0;break;case 9:case 1e3:case 1002:case 1003:this.x10Mouse=e===9,this.vt200Mouse=e===1e3,this.normalMouse=e>1e3,this.mouseEvents=!0,this.element.style.cursor="default",this.log("Binding to mouse events.");break;case 1004:this.sendFocus=!0;break;case 1005:this.utfMouse=!0;break;case 1006:this.sgrMouse=!0;break;case 1015:this.urxvtMouse=!0;break;case 25:this.cursorHidden=!1;break;case 1049:case 47:case 1047:if(!this.normal){var r={lines:this.lines,ybase:this.ybase,ydisp:this.ydisp,x:this.x,y:this.y,scrollTop:this.scrollTop,scrollBottom:this.scrollBottom,tabs:this.tabs};this.reset(),this.normal=r,this.showCursor()}}},h.prototype.resetMode=function(e){if(typeof e=="object"){var t=e.length,n=0;for(;n<t;n++)this.resetMode(e[n]);return}if(!this.prefix)switch(e){case 4:this.insertMode=!1;break;case 20:}else if(this.prefix==="?")switch(e){case 1:this.applicationCursor=!1;break;case 3:this.cols===132&&this.savedCols&&this.resize(this.savedCols,this.rows),delete this.savedCols;break;case 6:this.originMode=!1;break;case 7:this.wraparoundMode=!1;break;case 12:break;case 66:this.log("Switching back to normal keypad."),this.applicationKeypad=!1;break;case 9:case 1e3:case 1002:case 1003:this.x10Mouse=!1,this.vt200Mouse=!1,this.normalMouse=!1,this.mouseEvents=!1,this.element.style.cursor="";break;case 1004:this.sendFocus=!1;break;case 1005:this.utfMouse=!1;break;case 1006:this.sgrMouse=!1;break;case 1015:this.urxvtMouse=!1;break;case 25:this.cursorHidden=!0;break;case 1049:case 47:case 1047:this.normal&&(this.lines=this.normal.lines,this.ybase=this.normal.ybase,this.ydisp=this.normal.ydisp,this.x=this.normal.x,this.y=this.normal.y,this.scrollTop=this.normal.scrollTop,this.scrollBottom=this.normal.scrollBottom,this.tabs=this.normal.tabs,this.normal=null,this.refresh(0,this.rows-1),this.showCursor())}},h.prototype.setScrollRegion=function(e){if(this.prefix)return;this.scrollTop=(e[0]||1)-1,this.scrollBottom=(e[1]||this.rows)-1,this.x=0,this.y=0},h.prototype.saveCursor=function(e){this.savedX=this.x,this.savedY=this.y},h.prototype.restoreCursor=function(e){this.x=this.savedX||0,this.y=this.savedY||0},h.prototype.cursorForwardTab=function(e){var t=e[0]||1;while(t--)this.x=this.nextStop()},h.prototype.scrollUp=function(e){var t=e[0]||1;while(t--)this.lines.splice(this.ybase+this.scrollTop,1),this.lines.splice(this.ybase+this.scrollBottom,0,this.blankLine());this.updateRange(this.scrollTop),this.updateRange(this.scrollBottom)},h.prototype.scrollDown=function(e){var t=e[0]||1;while(t--)this.lines.splice(this.ybase+this.scrollBottom,1),this.lines.splice(this.ybase+this.scrollTop,0,this.blankLine());this.updateRange(this.scrollTop),this.updateRange(this.scrollBottom)},h.prototype.initMouseTracking=function(e){},h.prototype.resetTitleModes=function(e){},h.prototype.cursorBackwardTab=function(e){var t=e[0]||1;while(t--)this.x=this.prevStop()},h.prototype.repeatPrecedingCharacter=function(e){var t=e[0]||1,n=this.lines[this.ybase+this.y],r=n[this.x-1]||[this.defAttr," "];while(t--)n[this.x++]=r},h.prototype.tabClear=function(e){var t=e[0];t<=0?delete this.tabs[this.x]:t===3&&(this.tabs={})},h.prototype.mediaCopy=function(e){},h.prototype.setResources=function(e){},h.prototype.disableModifiers=function(e){},h.prototype.setPointerMode=function(e){},h.prototype.softReset=function(e){this.cursorHidden=!1,this.insertMode=!1,this.originMode=!1,this.wraparoundMode=!1,this.applicationKeypad=!1,this.applicationCursor=!1,this.scrollTop=0,this.scrollBottom=this.rows-1,this.curAttr=this.defAttr,this.x=this.y=0,this.charset=null,this.glevel=0,this.charsets=[null]},h.prototype.requestAnsiMode=function(e){},h.prototype.requestPrivateMode=function(e){},h.prototype.setConformanceLevel=function(e){},h.prototype.loadLEDs=function(e){},h.prototype.setCursorStyle=function(e){},h.prototype.setCharProtectionAttr=function(e){},h.prototype.restorePrivateValues=function(e){},h.prototype.setAttrInRectangle=function(e){var t=e[0],n=e[1],r=e[2],i=e[3],s=e[4],o,u;for(;t<r+1;t++){o=this.lines[this.ybase+t];for(u=n;u<i;u++)o[u]=[s,o[u][1]]}this.updateRange(e[0]),this.updateRange(e[2])},h.prototype.savePrivateValues=function(e){},h.prototype.manipulateWindow=function(e){},h.prototype.reverseAttrInRectangle=function(e){},h.prototype.setTitleModeFeature=function(e){},h.prototype.setWarningBellVolume=function(e){},h.prototype.setMarginBellVolume=function(e){},h.prototype.copyRectangle=function(e){},h.prototype.enableFilterRectangle=function(e){},h.prototype.requestParameters=function(e){},h.prototype.selectChangeExtent=function(e){},h.prototype.fillRectangle=function(e){var t=e[0],n=e[1],r=e[2],i=e[3],s=e[4],o,u;for(;n<i+1;n++){o=this.lines[this.ybase+n];for(u=r;u<s;u++)o[u]=[o[u][0],y.fromCharCode(t)]}this.updateRange(e[1]),this.updateRange(e[3])},h.prototype.enableLocatorReporting=function(e){var t=e[0]>0},h.prototype.eraseRectangle=function(e){var t=e[0],n=e[1],r=e[2],i=e[3],s,o,u;u=[this.eraseAttr()," "];for(;t<r+1;t++){s=this.lines[this.ybase+t];for(o=n;o<i;o++)s[o]=u}this.updateRange(e[0]),this.updateRange(e[2])},h.prototype.setLocatorEvents=function(e){},h.prototype.selectiveEraseRectangle=function(e){},h.prototype.requestLocatorPosition=function(e){},h.prototype.insertColumns=function(){var e=params[0],t=this.ybase+this.rows,n=[this.eraseAttr()," "],r;while(e--)for(r=this.ybase;r<t;r++)this.lines[r].splice(this.x+1,0,n),this.lines[r].pop();this.maxRange()},h.prototype.deleteColumns=function(){var e=params[0],t=this.ybase+this.rows,n=[this.eraseAttr()," "],r;while(e--)for(r=this.ybase;r<t;r++)this.lines[r].splice(this.x,1),this.lines[r].push(n);this.maxRange()},h.prototype.enterPrefix=function(){this.prefixMode=!0},h.prototype.leavePrefix=function(){this.prefixMode=!1},h.prototype.enterSelect=function(){this._real={x:this.x,y:this.y,ydisp:this.ydisp,ybase:this.ybase,cursorHidden:this.cursorHidden,lines:this.copyBuffer(this.lines),write:this.write},this.write=function(){},this.selectMode=!0,this.visualMode=!1,this.cursorHidden=!1,this.refresh(this.y,this.y)},h.prototype.leaveSelect=function(){this.x=this._real.x,this.y=this._real.y,this.ydisp=this._real.ydisp,this.ybase=this._real.ybase,this.cursorHidden=this._real.cursorHidden,this.lines=this._real.lines,this.write=this._real.write,delete this._real,this.selectMode=!1,this.visualMode=!1,this.refresh(0,this.rows-1)},h.prototype.enterVisual=function(){this._real.preVisual=this.copyBuffer(this.lines),this.selectText(this.x,this.x,this.ydisp+this.y,this.ydisp+this.y),this.visualMode=!0},h.prototype.leaveVisual=function(){this.lines=this._real.preVisual,delete this._real.preVisual,delete this._selected,this.visualMode=!1,this.refresh(0,this.rows-1)},h.prototype.enterSearch=function(e){this.entry="",this.searchMode=!0,this.searchDown=e,this._real.preSearch=this.copyBuffer(this.lines),this._real.preSearchX=this.x,this._real.preSearchY=this.y;var t=this.ydisp+this.rows-1;for(var n=0;n<this.entryPrefix.length;n++)this.lines[t][n]=[this.defAttr&-512|4,this.entryPrefix[n]];this.y=this.rows-1,this.x=this.entryPrefix.length,this.refresh(this.rows-1,this.rows-1)},h.prototype.leaveSearch=function(){this.searchMode=!1,this._real.preSearch&&(this.lines=this._real.preSearch,this.x=this._real.preSearchX,this.y=this._real.preSearchY,delete this._real.preSearch,delete this._real.preSearchX,delete this._real.preSearchY),this.refresh(this.rows-1,this.rows-1)},h.prototype.copyBuffer=function(e){var e=e||this.lines,t=[];for(var n=0;n<e.length;n++){t[n]=[];for(var r=0;r<e[n].length;r++)t[n][r]=[e[n][r][0],e[n][r][1]]}return t},h.prototype.getCopyTextarea=function(e){var t=this._copyTextarea,n=this.document;return t||(t=n.createElement("textarea"),t.style.position="absolute",t.style.left="-32000px",t.style.top="-32000px",t.style.width="0px",t.style.height="0px",t.style.opacity="0",t.style.backgroundColor="transparent",t.style.borderStyle="none",t.style.outlineStyle="none",n.getElementsByTagName("body")[0].appendChild(t),this._copyTextarea=t),t},h.prototype.copyText=function(e){var t=this,n=this.getCopyTextarea();this.emit("copy",e),n.focus(),n.textContent=e,n.value=e,n.setSelectionRange(0,e.length),b(function(){t.element.focus(),t.focus()},1)},h.prototype.selectText=function(e,t,n,r){var i,s,o,u,a,f,l,c,h;if(this._selected){i=this._selected.x1,s=this._selected.x2,o=this._selected.y1,u=this._selected.y2,u<o&&(a=s,s=i,i=a,a=u,u=o,o=a),s<i&&o===u&&(a=s,s=i,i=a);for(l=o;l<=u;l++){f=0,c=this.cols-1,l===o&&(f=i),l===u&&(c=s);for(;f<=c;f++)this.lines[l][f].old!=null&&(h=this.lines[l][f].old,delete this.lines[l][f].old,this.lines[l][f]=[h,this.lines[l][f][1]])}n=this._selected.y1,e=this._selected.x1}n=Math.max(n,0),n=Math.min(n,this.ydisp+this.rows-1),r=Math.max(r,0),r=Math.min(r,this.ydisp+this.rows-1),this._selected={x1:e,x2:t,y1:n,y2:r},r<n&&(a=t,t=e,e=a,a=r,r=n,n=a),t<e&&n===r&&(a=t,t=e,e=a);for(l=n;l<=r;l++){f=0,c=this.cols-1,l===n&&(f=e),l===r&&(c=t);for(;f<=c;f++)h=this.lines[l][f][0],this.lines[l][f]=[h&-512|261636,this.lines[l][f][1]],this.lines[l][f].old=h}n-=this.ydisp,r-=this.ydisp,n=Math.max(n,0),n=Math.min(n,this.rows-1),r=Math.max(r,0),r=Math.min(r,this.rows-1),this.refresh(0,this.rows-1)},h.prototype.grabText=function(e,t,n,r){var i="",s="",o,u,a,f,l;r<n&&(l=t,t=e,e=l,l=r,r=n,n=l),t<e&&n===r&&(l=t,t=e,e=l);for(a=n;a<=r;a++){u=0,f=this.cols-1,a===n&&(u=e),a===r&&(f=t);for(;u<=f;u++){o=this.lines[a][u][1];if(o===" "){s+=o;continue}s&&(i+=s,s=""),i+=o,S(o)&&u++}s="",i+="\n"}for(u=t,a=r;u<this.cols;u++)if(this.lines[a][u][1]!==" "){i=i.slice(0,-1);break}return i},h.prototype.keyPrefix=function(e,t){t==="k"||t==="&"?this.destroy():t==="p"||t==="]"?this.emit("request paste"):t==="c"?this.emit("request create"):t>="0"&&t<="9"?(t=+t-1,~t||(t=9),this.emit("request term",t)):t==="n"?this.emit("request term next"):t==="P"?this.emit("request term previous"):t===":"?this.emit("request command mode"):t==="["&&this.enterSelect()},h.prototype.keySelect=function(e,t){this.showCursor();if(this.searchMode||t==="n"||t==="N")return this.keySearch(e,t);if(t===""){var n=this.ydisp+this.y;this.ydisp===this.ybase?(this.y=Math.min(this.y+(this.rows-1)/2|0,this.rows-1),this.refresh(0,this.rows-1)):this.scrollDisp((this.rows-1)/2|0),this.visualMode&&this.selectText(this.x,this.x,n,this.ydisp+this.y);return}if(t===""){var n=this.ydisp+this.y;this.ydisp===0?(this.y=Math.max(this.y-(this.rows-1)/2|0,0),this.refresh(0,this.rows-1)):this.scrollDisp(-(this.rows-1)/2|0),this.visualMode&&this.selectText(this.x,this.x,n,this.ydisp+this.y);return}if(t===""){var n=this.ydisp+this.y;this.scrollDisp(this.rows-1),this.visualMode&&this.selectText(this.x,this.x,n,this.ydisp+this.y);return}if(t===""){var n=this.ydisp+this.y;this.scrollDisp(-(this.rows-1)),this.visualMode&&this.selectText(this.x,this.x,n,this.ydisp+this.y);return}if(t==="k"||t==="[A"){var n=this.ydisp+this.y;this.y--,this.y<0&&(this.y=0,this.scrollDisp(-1)),this.visualMode?this.selectText(this.x,this.x,n,this.ydisp+this.y):this.refresh(this.y,this.y+1);return}if(t==="j"||t==="[B"){var n=this.ydisp+this.y;this.y++,this.y>=this.rows&&(this.y=this.rows-1,this.scrollDisp(1)),this.visualMode?this.selectText(this.x,this.x,n,this.ydisp+this.y):this.refresh(this.y-1,this.y);return}if(t==="h"||t==="[D"){var r=this.x;this.x--,this.x<0&&(this.x=0),this.visualMode?this.selectText(r,this.x,this.ydisp+this.y,this.ydisp+this.y):this.refresh(this.y,this.y);return}if(t==="l"||t==="[C"){var r=this.x;this.x++,this.x>=this.cols&&(this.x=this.cols-1),this.visualMode?this.selectText(r,this.x,this.ydisp+this.y,this.ydisp+this.y):this.refresh(this.y,this.y);return}if(t==="v"||t===" "){this.visualMode?this.leaveVisual():this.enterVisual();return}if(t==="y"){if(this.visualMode){var i=this.grabText(this._selected.x1,this._selected.x2,this._selected.y1,this._selected.y2);this.copyText(i),this.leaveVisual()}return}if(t==="q"||t===""){this.visualMode?this.leaveVisual():this.leaveSelect();return}if(t==="w"||t==="W"){var s=this.x,o=this.y,u=this.ydisp,r=this.x,n=this.y,a=this.ydisp,f=!1;for(;;){var l=this.lines[a+n];while(r<this.cols){if(l[r][1]<=" ")f=!0;else if(f)break;r++}r>=this.cols&&(r=this.cols-1);if(r===this.cols-1&&l[r][1]<=" "){r=0;if(++n>=this.rows){n--;if(++a>this.ybase){a=this.ybase,r=this.x;break}}continue}break}this.x=r,this.y=n,this.scrollDisp(-this.ydisp+a),this.visualMode&&this.selectText(s,this.x,o+u,this.ydisp+this.y);return}if(t==="b"||t==="B"){var s=this.x,o=this.y,u=this.ydisp,r=this.x,n=this.y,a=this.ydisp;for(;;){var l=this.lines[a+n],f=r>0&&l[r][1]>" "&&l[r-1][1]>" ";while(r>=0){if(l[r][1]<=" "){if(f&&r+1<this.cols&&l[r+1][1]>" "){r++;break}f=!0}r--}r<0&&(r=0);if(r===0&&(l[r][1]<=" "||!f)){r=this.cols-1;if(--n<0){n++;if(--a<0){a++,r=0;break}}continue}break}this.x=r,this.y=n,this.scrollDisp(-this.ydisp+a),this.visualMode&&this.selectText(s,this.x,o+u,this.ydisp+this.y);return}if(t==="e"||t==="E"){var r=this.x+1,n=this.y,a=this.ydisp;r>=this.cols&&r--;for(;;){var l=this.lines[a+n];while(r<this.cols){if(!(l[r][1]<=" "))break;r++}while(r<this.cols){if(l[r][1]<=" "&&r-1>=0&&l[r-1][1]>" "){r--;break}r++}r>=this.cols&&(r=this.cols-1);if(r===this.cols-1&&l[r][1]<=" "){r=0;if(++n>=this.rows){n--;if(++a>this.ybase){a=this.ybase;break}}continue}break}this.x=r,this.y=n,this.scrollDisp(-this.ydisp+a),this.visualMode&&this.selectText(s,this.x,o+u,this.ydisp+this.y);return}if(t==="^"||t==="0"){var s=this.x;if(t==="0")this.x=0;else if(t==="^"){var l=this.lines[this.ydisp+this.y],r=0;while(r<this.cols){if(l[r][1]>" ")break;r++}r>=this.cols&&(r=this.cols-1),this.x=r}this.visualMode?this.selectText(s,this.x,this.ydisp+this.y,this.ydisp+this.y):this.refresh(this.y,this.y);return}if(t==="$"){var s=this.x,l=this.lines[this.ydisp+this.y],r=this.cols-1;while(r>=0){if(l[r][1]>" "){this.visualMode&&r<this.cols-1&&r++;break}r--}r<0&&(r=0),this.x=r,this.visualMode?this.selectText(s,this.x,this.ydisp+this.y,this.ydisp+this.y):this.refresh(this.y,this.y);return}if(t==="g"||t==="G"){var s=this.x,o=this.y,u=this.ydisp;t==="g"?(this.x=0,this.y=0,this.scrollDisp(-this.ydisp)):t==="G"&&(this.x=0,this.y=this.rows-1,this.scrollDisp(this.ybase)),this.visualMode&&this.selectText(s,this.x,o+u,this.ydisp+this.y);return}if(t==="H"||t==="M"||t==="L"){var s=this.x,o=this.y;t==="H"?(this.x=0,this.y=0):t==="M"?(this.x=0,this.y=this.rows/2|0):t==="L"&&(this.x=0,this.y=this.rows-1),this.visualMode?this.selectText(s,this.x,this.ydisp+o,this.ydisp+this.y):(this.refresh(o,o),this.refresh(this.y,this.y));return}if(t==="{"||t==="}"){var s=this.x,o=this.y,u=this.ydisp,l,c=!1,h=!1,p=-1,n=this.y+(t==="{"?-1:1),a=this.ydisp,d;t==="{"?n<0&&(n++,a>0&&a--):t==="}"&&n>=this.rows&&(n--,a<this.ybase&&a++);for(;;){l=this.lines[a+n];for(d=0;d<this.cols;d++){if(l[d][1]>" "){p===-1&&(p=0),c=!0;break}if(d===this.cols-1){p===-1?p=1:p===0?h=!0:p===1&&c&&(h=!0);break}}if(h)break;if(t==="{"){n--;if(n<0){n++;if(!(a>0))break;a--}}else if(t==="}"){n++;if(n>=this.rows){n--;if(!(a<this.ybase))break;a++}}}h||(t==="{"?(n=0,a=0):t==="}"&&(n=this.rows-1,a=this.ybase)),this.x=0,this.y=n,this.scrollDisp(-this.ydisp+a),this.visualMode&&this.selectText(s,this.x,o+u,this.ydisp+this.y);return}if(t==="/"||t==="?"){this.visualMode||this.enterSearch(t==="/");return}return!1},h.prototype.keySearch=function(e,t){if(t===""){this.leaveSearch();return}if(t==="\r"||!this.searchMode&&(t==="n"||t==="N")){this.leaveSearch();var n=this.entry;if(!n){this.refresh(0,this.rows-1);return}var r=this.x,i=this.y,s=this.ydisp,o,u=!1,a=!1,f=this.x+1,l=this.ydisp+this.y,c,h,p=t==="N"?this.searchDown:!this.searchDown;for(;;){o=this.lines[l];while(f<this.cols){for(h=0;h<n.length;h++){if(f+h>=this.cols)break;if(o[f+h][1]!==n[h])break;if(o[f+h][1]===n[h]&&h===n.length-1){u=!0;break}}if(u)break;f+=h+1}if(u)break;f=0;if(!p){l++;if(l>this.ybase+this.rows-1){if(a)break;a=!0,l=0}}else{l--;if(l<0){if(a)break;a=!0,l=this.ybase+this.rows-1}}}if(u){l-this.ybase<0?(c=l,l=0,c>this.ybase&&(l=c-this.ybase,c=this.ybase)):(c=this.ybase,l-=this.ybase),this.x=f,this.y=l,this.scrollDisp(-this.ydisp+c),this.visualMode&&this.selectText(r,this.x,i+s,this.ydisp+this.y);return}this.refresh(0,this.rows-1);return}if(t==="\b"||t===""){if(this.entry.length===0)return;var d=this.ydisp+this.rows-1;this.entry=this.entry.slice(0,-1);var h=this.entryPrefix.length+this.entry.length;this.lines[d][h]=[this.lines[d][h][0]," "],this.x--,this.refresh(this.rows-1,this.rows-1),this.refresh(this.y,this.y);return}if(t.length===1&&t>=" "&&t<="~"){var d=this.ydisp+this.rows-1;this.entry+=t;var h=this.entryPrefix.length+this.entry.length-1;this.lines[d][h]=[this.defAttr&-512|4,t],this.x++,this.refresh(this.rows-1,this.rows-1),this.refresh(this.y,this.y);return}return!1},h.charsets={},h.charsets.SCLD={"`":"◆",a:"▒",b:"	",c:"\f",d:"\r",e:"\n",f:"°",g:"±",h:"␤",i:"",j:"┘",k:"┐",l:"┌",m:"└",n:"┼",o:"⎺",p:"⎻",q:"─",r:"⎼",s:"⎽",t:"├",u:"┤",v:"┴",w:"┬",x:"│",y:"≤",z:"≥","{":"π","|":"≠","}":"£","~":"·"},h.charsets.UK=null,h.charsets.US=null,h.charsets.Dutch=null,h.charsets.Finnish=null,h.charsets.French=null,h.charsets.FrenchCanadian=null,h.charsets.German=null,h.charsets.Italian=null,h.charsets.NorwegianDanish=null,h.charsets.Spanish=null,h.charsets.Swedish=null,h.charsets.Swiss=null,h.charsets.ISOLatin=null;var y=this.String,b=this.setTimeout,w=this.setInterval;x._cache={},x.distance=function(e,t,n,r,i,s){return Math.pow(30*(e-r),2)+Math.pow(59*(t-i),2)+Math.pow(11*(n-s),2)},h.EventEmitter=n,h.Stream=r,h.inherits=m,h.on=p,h.off=d,h.cancel=v,typeof module!="undefined"?module.exports=h:this.Terminal=h}).call(function(){return this||(typeof window!="undefined"?window:global)}());