!function(e){"object"==typeof exports&&"object"==typeof module?e(require("../../lib/codemirror")):"function"==typeof define&&define.amd?define(["../../lib/codemirror"],e):e(CodeMirror)}(function(k){var r={pairs:"()[]{}''\"\"",closeBefore:")]}'\":;>",triples:"",explode:"[]{}"},y=k.Pos;function S(e,t){return"pairs"==t&&"string"==typeof e?e:"object"==typeof e&&null!=e[t]?e[t]:r[t]}k.defineOption("autoCloseBrackets",!1,function(e,t,r){r&&r!=k.Init&&(e.removeKeyMap(i),e.state.closeBrackets=null),t&&(n(S(t,"pairs")),e.state.closeBrackets=t,e.addKeyMap(i))});var i={Backspace:function(e){var t=O(e);if(!t||e.getOption("disableInput"))return k.Pass;for(var r=S(t,"pairs"),n=e.listSelections(),i=0;i<n.length;i++){if(!n[i].empty())return k.Pass;var a=s(e,n[i].head);if(!a||r.indexOf(a)%2!=0)return k.Pass}for(i=n.length-1;0<=i;i--){var o=n[i].head;e.replaceRange("",y(o.line,o.ch-1),y(o.line,o.ch+1),"+delete")}},Enter:function(n){var e=O(n),t=e&&S(e,"explode");if(!t||n.getOption("disableInput"))return k.Pass;for(var i=n.listSelections(),r=0;r<i.length;r++){if(!i[r].empty())return k.Pass;var a=s(n,i[r].head);if(!a||t.indexOf(a)%2!=0)return k.Pass}n.operation(function(){var e=n.lineSeparator()||"\n";n.replaceSelection(e+e,null),n.execCommand("goCharLeft"),i=n.listSelections();for(var t=0;t<i.length;t++){var r=i[t].head.line;n.indentLine(r,null,!0),n.indentLine(r+1,null,!0)}})}};function n(e){for(var t=0;t<e.length;t++){var r=e.charAt(t),n="'"+r+"'";i[n]||(i[n]=a(r))}}function a(t){return function(e){return function(i,e){var t=O(i);if(!t||i.getOption("disableInput"))return k.Pass;var r=S(t,"pairs"),n=r.indexOf(e);if(-1==n)return k.Pass;for(var a,o=S(t,"closeBefore"),s=S(t,"triples"),l=r.charAt(n+1)==e,c=i.listSelections(),f=n%2==0,h=0;h<c.length;h++){var u,d=c[h],p=d.head,g=i.getRange(p,y(p.line,p.ch+1));if(f&&!d.empty())u="surround";else if(!l&&f||g!=e)if(l&&1<p.ch&&0<=s.indexOf(e)&&i.getRange(y(p.line,p.ch-2),p)==e+e){if(2<p.ch&&/\bstring/.test(i.getTokenTypeAt(y(p.line,p.ch-2))))return k.Pass;u="addFour"}else if(l){var v=0==p.ch?" ":i.getRange(y(p.line,p.ch-1),p);if(k.isWordChar(g)||v==e||k.isWordChar(v))return k.Pass;u="both"}else{if(!f||!(0===g.length||/\s/.test(g)||-1<o.indexOf(g)))return k.Pass;u="both"}else u=!l||(b=p,void 0,C=(m=i).getTokenAt(y(b.line,b.ch+1)),!/\bstring/.test(C.type)||C.start!=b.ch||0!=b.ch&&/\bstring/.test(m.getTokenTypeAt(b)))?0<=s.indexOf(e)&&i.getRange(p,y(p.line,p.ch+3))==e+e+e?"skipThree":"skip":"both";if(a){if(a!=u)return k.Pass}else a=u}var m,b,C;var x=n%2?r.charAt(n-1):e,P=n%2?e:r.charAt(n+1);i.operation(function(){if("skip"==a)i.execCommand("goCharRight");else if("skipThree"==a)for(var e=0;e<3;e++)i.execCommand("goCharRight");else if("surround"==a){var t=i.getSelections();for(e=0;e<t.length;e++)t[e]=x+t[e]+P;i.replaceSelections(t,"around"),t=i.listSelections().slice();for(e=0;e<t.length;e++)t[e]=(r=t[e],void 0,n=0<k.cmpPos(r.anchor,r.head),{anchor:new y(r.anchor.line,r.anchor.ch+(n?-1:1)),head:new y(r.head.line,r.head.ch+(n?1:-1))});i.setSelections(t)}else"both"==a?(i.replaceSelection(x+P,null),i.triggerElectric(x+P),i.execCommand("goCharLeft")):"addFour"==a&&(i.replaceSelection(x+x+x+x,"before"),i.execCommand("goCharRight"));var r,n})}(e,t)}}function O(e){var t=e.state.closeBrackets;return!t||t.override?t:e.getModeAt(e.getCursor()).closeBrackets||t}function s(e,t){var r=e.getRange(y(t.line,t.ch-1),y(t.line,t.ch+1));return 2==r.length?r:null}n(r.pairs+"`")});