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