'use strict';(function(k){"object"==typeof exports&&"object"==typeof module?k(require("../lib/codemirror"),require("../addon/search/searchcursor"),require("../addon/edit/matchbrackets")):"function"==typeof define&&define.amd?define(["../lib/codemirror","../addon/search/searchcursor","../addon/edit/matchbrackets"],k):k(CodeMirror)})(function(k){function t(a,d){a.extendSelectionsBy(function(c){if(a.display.shift||a.doc.extend||c.empty()){var h;var m=a.doc;c=c.head;if(0>d&&0==c.ch)h=m.clipPos(l(c.line-
1));else{var b=m.getLine(c.line);if(0<d&&c.ch>=b.length)h=m.clipPos(l(c.line+1,0));else{for(var m="start",g=c.ch,e=0>d?0:b.length,f=0;g!=e;g+=d,f++){var q=b.charAt(0>d?g-1:g),p="_"!=q&&k.isWordChar(q)?"w":"o";"w"==p&&q.toUpperCase()==q&&(p="W");if("start"==m)"o"!=p&&(m="in",h=p);else if("in"==m&&h!=p)if("w"==h&&"W"==p&&0>d&&g--,"W"==h&&"w"==p&&0<d)h="w";else break}h=l(c.line,g)}}return h}return 0>d?c.from():c.to()})}function u(a,d){a.operation(function(){for(var c=a.listSelections().length,h=[],m=
-1,b=0;b<c;b++){var g=a.listSelections()[b].head;g.line<=m||(m=l(g.line+(d?0:1),0),a.replaceRange("\n",m,null,"+insertLine"),a.indentLine(m.line,null,!0),h.push({head:m,anchor:m}),m=g.line+1)}a.setSelections(h)})}function r(a,d){for(var c=d.ch,h=c,m=a.getLine(d.line);c&&k.isWordChar(m.charAt(c-1));)--c;for(;h<m.length&&k.isWordChar(m.charAt(h));)++h;return{from:l(d.line,c),to:l(d.line,h),word:m.slice(c,h)}}function v(a){var d=a.getCursor(),c=a.scanForBracket(d,-1);if(c)for(;;){d=a.scanForBracket(d,
1);if(!d)break;if(d.ch=="(){}[]".charAt("(){}[]".indexOf(c.ch)+1))return a.setSelection(l(c.pos.line,c.pos.ch+1),d.pos,!1),!0;d=l(d.pos.line,d.pos.ch+1)}}function w(a,d){for(var c=a.listSelections(),h=[],m,b=0;b<c.length;b++){var g=c[b];if(!g.empty()){for(var e=g.from().line,f=g.to().line;b<c.length-1&&c[b+1].from().line==f;)f=g[++b].to().line;h.push(e,f)}}h.length?m=!0:h.push(a.firstLine(),a.lastLine());a.operation(function(){for(var c=[],b=0;b<h.length;b+=2){var g=h[b+1],e=l(h[b],0),g=l(g),f=a.getRange(e,
g,!1);d?f.sort():f.sort(function(a,c){var d=a.toUpperCase(),h=c.toUpperCase();d!=h&&(a=d,c=h);return a<c?-1:a==c?0:1});a.replaceRange(f,e,g);m&&c.push({anchor:e,head:g})}m&&a.setSelections(c,0)})}function x(a,d){a.operation(function(){for(var c=a.listSelections(),h=[],b=[],e=0;e<c.length;e++){var g=c[e];g.empty()?(h.push(e),b.push("")):b.push(d(a.getRange(g.from(),g.to())))}a.replaceSelections(b,"around","case");for(var e=h.length-1,f;0<=e;e--)g=c[h[e]],f&&0<k.cmpPos(g.head,f)||(b=r(a,g.head),f=b.from,
a.replaceRange(d(b.word),b.from,b.to))})}function y(a){var d=a.getCursor("from"),c=a.getCursor("to");if(0==k.cmpPos(d,c)){var h=r(a,d);if(!h.word)return;d=h.from;c=h.to}return{from:d,to:c,query:a.getRange(d,c),word:h}}function z(a,d){var c=y(a);if(c){var h=c.query,b=a.getSearchCursor(h,d?c.to:c.from);(d?b.findNext():b.findPrevious())?a.setSelection(b.from(),b.to()):(b=a.getSearchCursor(h,d?l(a.firstLine(),0):a.clipPos(l(a.lastLine()))),(d?b.findNext():b.findPrevious())?a.setSelection(b.from(),b.to()):
c.word&&a.setSelection(c.from,c.to))}}var b=k.keyMap.sublime={fallthrough:"default"},f=k.commands,l=k.Pos,n=k.keyMap["default"]==k.keyMap.macDefault,e=n?"Cmd-":"Ctrl-";f[b["Alt-Left"]="goSubwordLeft"]=function(a){t(a,-1)};f[b["Alt-Right"]="goSubwordRight"]=function(a){t(a,1)};var A=n?"Ctrl-Alt-":"Ctrl-";f[b[A+"Up"]="scrollLineUp"]=function(a){var d=a.getScrollInfo();if(!a.somethingSelected()){var c=a.lineAtHeight(d.top+d.clientHeight,"local");a.getCursor().line>=c&&a.execCommand("goLineUp")}a.scrollTo(null,
d.top-a.defaultTextHeight())};f[b[A+"Down"]="scrollLineDown"]=function(a){var d=a.getScrollInfo();if(!a.somethingSelected()){var c=a.lineAtHeight(d.top,"local")+1;a.getCursor().line<=c&&a.execCommand("goLineDown")}a.scrollTo(null,d.top+a.defaultTextHeight())};f[b["Shift-"+e+"L"]="splitSelectionByLine"]=function(a){for(var d=a.listSelections(),c=[],b=0;b<d.length;b++)for(var e=d[b].from(),f=d[b].to(),g=e.line;g<=f.line;++g)f.line>e.line&&g==f.line&&0==f.ch||c.push({anchor:g==e.line?e:l(g,0),head:g==
f.line?f:l(g)});a.setSelections(c,0)};b["Shift-Tab"]="indentLess";f[b.Esc="singleSelectionTop"]=function(a){var d;if((d=a.listSelections())&&1<d.length)d=d[0],a.setSelection(d.anchor,d.head,{scroll:!1});else return k.Pass};f[b[e+"L"]="selectLine"]=function(a){for(var d=a.listSelections(),c=[],b=0;b<d.length;b++){var e=d[b];c.push({anchor:l(e.from().line,0),head:l(e.to().line+1,0)})}a.setSelections(c)};b["Shift-"+e+"K"]="deleteLine";f[b[e+"Enter"]="insertLineAfter"]=function(a){u(a,!1)};f[b["Shift-"+
e+"Enter"]="insertLineBefore"]=function(a){u(a,!0)};f[b[e+"D"]="selectNextOccurrence"]=function(a){var d=a.getCursor("from"),c=a.getCursor("to"),b=a.state.sublimeFindFullWord==a.doc.sel;if(0==k.cmpPos(d,c)){b=r(a,d);if(!b.word)return;a.setSelection(b.from,b.to);b=!0}else d=a.getRange(d,c),d=b?new RegExp("\\b"+d+"\\b"):d,c=a.getSearchCursor(d,c),c.findNext()?a.addSelection(c.from(),c.to()):(c=a.getSearchCursor(d,l(a.firstLine(),0)),c.findNext()&&a.addSelection(c.from(),c.to()));b&&(a.state.sublimeFindFullWord=
a.doc.sel)};f[b["Shift-"+e+"Space"]="selectScope"]=function(a){v(a)||a.execCommand("selectAll")};f[b["Shift-"+e+"M"]="selectBetweenBrackets"]=function(a){if(!v(a))return k.Pass};f[b[e+"M"]="goToBracket"]=function(a){a.extendSelectionsBy(function(d){var c=a.scanForBracket(d.head,1);return c&&0!=k.cmpPos(c.pos,d.head)?c.pos:(c=a.scanForBracket(d.head,-1))&&l(c.pos.line,c.pos.ch+1)||d.head})};n=n?"Cmd-Ctrl-":"Shift-Ctrl-";f[b[n+"Up"]="swapLineUp"]=function(a){for(var d=a.listSelections(),c=[],b=a.firstLine()-
1,e=[],f=0;f<d.length;f++){var g=d[f],k=g.from().line-1,n=g.to().line;e.push({anchor:l(g.anchor.line-1,g.anchor.ch),head:l(g.head.line-1,g.head.ch)});0!=g.to().ch||g.empty()||--n;k>b?c.push(k,n):c.length&&(c[c.length-1]=n);b=n}a.operation(function(){for(var d=0;d<c.length;d+=2){var b=c[d],h=c[d+1],f=a.getLine(b);a.replaceRange("",l(b,0),l(b+1,0),"+swapLine");h>a.lastLine()?a.replaceRange("\n"+f,l(a.lastLine()),null,"+swapLine"):a.replaceRange(f+"\n",l(h,0),null,"+swapLine")}a.setSelections(e);a.scrollIntoView()})};
f[b[n+"Down"]="swapLineDown"]=function(a){for(var d=a.listSelections(),c=[],b=a.lastLine()+1,e=d.length-1;0<=e;e--){var f=d[e],g=f.to().line+1,k=f.from().line;0!=f.to().ch||f.empty()||g--;g<b?c.push(g,k):c.length&&(c[c.length-1]=k);b=k}a.operation(function(){for(var d=c.length-2;0<=d;d-=2){var b=c[d],h=c[d+1],e=a.getLine(b);b==a.lastLine()?a.replaceRange("",l(b-1),l(b),"+swapLine"):a.replaceRange("",l(b,0),l(b+1,0),"+swapLine");a.replaceRange(e+"\n",l(h,0),null,"+swapLine")}a.scrollIntoView()})};
f[b[e+"/"]=b[e+"7"]="toggleComment"]=function(a){a.toggleComment({indent:!0})};f[b[e+"Shift-/"]=b[e+"Shift-7"]="toggleBlockComment"]=function(a){a.toggleComment({indent:!0,blockComment:!0})};f[b[e+"J"]="joinLines"]=function(a){for(var d=a.listSelections(),c=[],b=0;b<d.length;b++){for(var e=d[b],f=e.from(),g=f.line,k=e.to().line;b<d.length-1&&d[b+1].from().line==k;)k=d[++b].to().line;c.push({start:g,end:k,anchor:!e.empty()&&f})}a.operation(function(){for(var d=0,b=[],e=0;e<c.length;e++){for(var f=
c[e],h=f.anchor&&l(f.anchor.line-d,f.anchor.ch),g,k=f.start;k<=f.end;k++){var m=k-d;k==f.end&&(g=l(m,a.getLine(m).length+1));m<a.lastLine()&&(a.replaceRange(" ",l(m),l(m+1,/^\s*/.exec(a.getLine(m+1))[0].length)),++d)}b.push({anchor:h||g,head:g})}a.setSelections(b,0)})};f[b["Shift-"+e+"D"]="duplicateLine"]=function(a){a.operation(function(){for(var d=a.listSelections().length,b=0;b<d;b++){var e=a.listSelections()[b];e.empty()?a.replaceRange(a.getLine(e.head.line)+"\n",l(e.head.line,0)):a.replaceRange(a.getRange(e.from(),
e.to()),e.from())}a.scrollIntoView()})};b[e+"T"]="transposeChars";f[b.F9="sortLines"]=function(a){w(a,!0)};f[b[e+"F9"]="sortLinesInsensitive"]=function(a){w(a,!1)};b.F2="nextBookmark";b["Shift-F2"]="prevBookmark";b[e+"F2"]="toggleBookmark";b["Shift-"+e+"F2"]="clearBookmarks";b["Alt-F2"]="selectBookmarks";b["Alt-Q"]="wrapLines";n=e+"K ";b[n+e+"Backspace"]="delLineLeft";f[b.Backspace="smartBackspace"]=function(a){if(a.somethingSelected())return k.Pass;var d=a.getCursor(),b=a.getRange({line:d.line,ch:0},
d),e=k.countColumn(b,null,a.getOption("tabSize")),f=a.getOption("indentUnit");return b&&!/\S/.test(b)&&0==e%f?(b=new l(d.line,k.findColumn(b,e-f,f)),b.ch==d.ch?k.Pass:a.replaceRange("",b,d,"+delete")):k.Pass};f[b[n+e+"K"]="delLineRight"]=function(a){a.operation(function(){for(var b=a.listSelections(),c=b.length-1;0<=c;c--)a.replaceRange("",b[c].anchor,l(b[c].to().line),"+delete");a.scrollIntoView()})};f[b[n+e+"U"]="upcaseAtCursor"]=function(a){x(a,function(a){return a.toUpperCase()})};f[b[n+e+"L"]=
"downcaseAtCursor"]=function(a){x(a,function(a){return a.toLowerCase()})};f[b[n+e+"Space"]="setSublimeMark"]=function(a){a.state.sublimeMark&&a.state.sublimeMark.clear();a.state.sublimeMark=a.setBookmark(a.getCursor())};f[b[n+e+"A"]="selectToSublimeMark"]=function(a){var b=a.state.sublimeMark&&a.state.sublimeMark.find();b&&a.setSelection(a.getCursor(),b)};f[b[n+e+"W"]="deleteToSublimeMark"]=function(a){var b=a.state.sublimeMark&&a.state.sublimeMark.find();if(b){var c=a.getCursor();if(0<k.cmpPos(c,
b))var e=b,b=c,c=e;a.state.sublimeKilled=a.getRange(c,b);a.replaceRange("",c,b)}};f[b[n+e+"X"]="swapWithSublimeMark"]=function(a){var b=a.state.sublimeMark&&a.state.sublimeMark.find();b&&(a.state.sublimeMark.clear(),a.state.sublimeMark=a.setBookmark(a.getCursor()),a.setCursor(b))};f[b[n+e+"Y"]="sublimeYank"]=function(a){null!=a.state.sublimeKilled&&a.replaceSelection(a.state.sublimeKilled,null,"paste")};b[n+e+"G"]="clearBookmarks";f[b[n+e+"C"]="showInCenter"]=function(a){var b=a.cursorCoords(null,
"local");a.scrollTo(null,(b.top+b.bottom)/2-a.getScrollInfo().clientHeight/2)};f[b["Shift-Alt-Up"]="selectLinesUpward"]=function(a){a.operation(function(){for(var b=a.listSelections(),c=0;c<b.length;c++){var e=b[c];e.head.line>a.firstLine()&&a.addSelection(l(e.head.line-1,e.head.ch))}})};f[b["Shift-Alt-Down"]="selectLinesDownward"]=function(a){a.operation(function(){for(var b=a.listSelections(),c=0;c<b.length;c++){var e=b[c];e.head.line<a.lastLine()&&a.addSelection(l(e.head.line+1,e.head.ch))}})};
f[b[e+"F3"]="findUnder"]=function(a){z(a,!0)};f[b["Shift-"+e+"F3"]="findUnderPrevious"]=function(a){z(a,!1)};f[b["Alt-F3"]="findAllUnder"]=function(a){var b=y(a);if(b){for(var c=a.getSearchCursor(b.query),e=[],f=-1;c.findNext();)e.push({anchor:c.from(),head:c.to()}),c.from().line<=b.from.line&&c.from().ch<=b.from.ch&&f++;a.setSelections(e,f)}};b["Shift-"+e+"["]="fold";b["Shift-"+e+"]"]="unfold";b[n+e+"0"]=b[n+e+"j"]="unfoldAll";b[e+"I"]="findIncremental";b["Shift-"+e+"I"]="findIncrementalReverse";
b[e+"H"]="replace";b.F3="findNext";b["Shift-F3"]="findPrev";k.normalizeKeyMap(b)});
