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