(function(g){"object"==typeof exports&&"object"==typeof module?g(require("../lib/codemirror")):"function"==typeof define&&define.amd?define(["../lib/codemirror"],g):g(CodeMirror)})(function(g){function y(a,b){return a.line==b.line&&a.ch==b.ch}function z(a){l.push(a);50<l.length&&l.shift()}function E(a){return l[l.length-(a?Math.min(a,1):1)]||""}function M(){1<l.length&&l.pop();return E()}function s(a,b,c,d,e){null==e&&(e=a.getRange(b,c));d&&r&&r.cm==a&&y(b,r.pos)&&a.isClean(r.gen)?l.length?l[l.length-
1]+=e:z(e):z(e);a.replaceRange("",b,c,"+delete");r=d?{cm:a,pos:b,gen:a.changeGeneration()}:null}function p(a,b,c){return a.findPosH(b,c,"char",!0)}function t(a,b,c){return a.findPosH(b,c,"word",!0)}function u(a,b,c){return a.findPosV(b,c,"line",a.doc.sel.goalColumn)}function v(a,b,c){return a.findPosV(b,c,"page",a.doc.sel.goalColumn)}function F(a,b,c){var d=b.line,e=a.getLine(d);b=/\S/.test(0>c?e.slice(0,b.ch):e.slice(b.ch));for(var f=a.firstLine(),g=a.lastLine();;){d+=c;if(d<f||d>g)return a.clipPos(h(d-
c,0>c?0:null));e=a.getLine(d);if(/\S/.test(e))b=!0;else if(b)return h(d,0)}}function w(a,b,c){var d=b.line,e=b.ch;b=a.getLine(b.line);for(var f=!1;;){var g=b.charAt(e+(0>c?-1:0));if(g){if(f&&/[!?.]/.test(g))return h(d,e+(0<c?1:0));f||(f=/\w/.test(g));e+=c}else{if(d==(0>c?a.firstLine():a.lastLine()))return h(d,e);b=a.getLine(d+c);if(!/\S/.test(b))return h(d,e);d+=c;e=0>c?b.length:0}}}function m(a,b,c){var d;if(a.findMatchingBracket&&(d=a.findMatchingBracket(b,!0))&&d.match&&(d.forward?1:-1)==c)return 0<
c?h(d.to.line,d.to.ch+1):d.to;for(var e=!0;;e=!1){var f=a.getTokenAt(b);d=h(b.line,0>c?f.start:f.end);if(e&&0<c&&f.end==b.ch||!/\w/.test(f.string)){e=a.findPosH(d,c,"char");if(y(d,e))return b;b=e}else return d}}function q(a,b){var c=a.state.emacsPrefix;if(!c)return b?null:1;A(a);return"-"==c?-1:Number(c)}function k(a){var b="string"==typeof a?function(b){b.execCommand(a)}:a;return function(a){var d=q(a);b(a);for(var e=1;e<d;++e)b(a)}}function B(a,b,c){var d=a.getCursor(),e=q(a);0>e&&(c=-c,e=-e);for(var f=
0;f<e;++f){var g=b(a,d,c);if(y(g,d))break;d=g}return d}function f(a,b){var c=function(c){c.extendSelection(B(c,a,b))};c.motion=!0;return c}function n(a,b,c){s(a,a.getCursor(),B(a,b,c),!0)}function G(a,b){a.state.emacsPrefix?"-"!=b&&(a.state.emacsPrefix+=b):(a.state.emacsPrefix=b,a.on("keyHandled",H),a.on("inputRead",I))}function H(a,b){a.state.emacsPrefixMap||J.hasOwnProperty(b)||A(a)}function A(a){a.state.emacsPrefix=null;a.off("keyHandled",H);a.off("inputRead",I)}function I(a,b){var c=q(a);if(1<
c&&"+input"==b.origin){for(var d=b.text.join("\n"),e="",f=1;f<c;++f)e+=d;a.replaceSelection(e,null,"+input")}}function x(a,b){if("string"!=typeof b||!/^\d$/.test(b)&&"Ctrl-U"!=b)a.removeKeyMap(C),a.state.emacsPrefixMap=!1,a.off("keyHandled",x),a.off("inputRead",x)}function K(a){a.setCursor(a.getCursor());a.setExtending(!0);a.on("change",function(){a.setExtending(!1)})}function N(a,b,c){a.openDialog?a.openDialog(b+': <input type="text" style="width: 10em"/>',c,{bottom:!0}):c(prompt(b,""))}function D(a,
b){var c=a.getCursor(),d=a.findPosH(c,1,"word");a.replaceRange(b(a.getRange(c,d)),c,d);a.setCursor(d)}function L(a){C[a]=function(b){G(b,a)};O["Ctrl-"+a]=function(b){G(b,a)};J["Ctrl-"+a]=!0}var h=g.Pos,l=[],r=null,J={"Alt-G":!0,"Ctrl-X":!0,"Ctrl-Q":!0,"Ctrl-U":!0},O=g.keyMap.emacs={"Ctrl-W":function(a){s(a,a.getCursor("start"),a.getCursor("end"))},"Ctrl-K":k(function(a){var b=a.getCursor(),c=a.clipPos(h(b.line)),d=a.getRange(b,c);/\S/.test(d)||(d+="\n",c=h(b.line+1,0));s(a,b,c,!0,d)}),"Alt-W":function(a){z(a.getSelection());
a.setExtending(!1);a.setCursor(a.getCursor())},"Ctrl-Y":function(a){var b=a.getCursor();a.replaceRange(E(q(a)),b,b,"paste");a.setSelection(b,a.getCursor())},"Alt-Y":function(a){a.replaceSelection(M(),"around")},"Ctrl-Space":K,"Ctrl-Shift-2":K,"Ctrl-F":f(p,1),"Ctrl-B":f(p,-1),Right:f(p,1),Left:f(p,-1),"Ctrl-D":function(a){n(a,p,1)},Delete:function(a){n(a,p,1)},"Ctrl-H":function(a){n(a,p,-1)},Backspace:function(a){n(a,p,-1)},"Alt-F":f(t,1),"Alt-B":f(t,-1),"Alt-D":function(a){n(a,t,1)},"Alt-Backspace":function(a){n(a,
t,-1)},"Ctrl-N":f(u,1),"Ctrl-P":f(u,-1),Down:f(u,1),Up:f(u,-1),"Ctrl-A":"goLineStart","Ctrl-E":"goLineEnd",End:"goLineEnd",Home:"goLineStart","Alt-V":f(v,-1),"Ctrl-V":f(v,1),PageUp:f(v,-1),PageDown:f(v,1),"Ctrl-Up":f(F,-1),"Ctrl-Down":f(F,1),"Alt-A":f(w,-1),"Alt-E":f(w,1),"Alt-K":function(a){n(a,w,1)},"Ctrl-Alt-K":function(a){n(a,m,1)},"Ctrl-Alt-Backspace":function(a){n(a,m,-1)},"Ctrl-Alt-F":f(m,1),"Ctrl-Alt-B":f(m,-1),"Shift-Ctrl-Alt-2":function(a){a.setSelection(B(a,m,1),a.getCursor())},"Ctrl-Alt-T":function(a){var b=
m(a,a.getCursor(),-1),c=m(a,b,1),d=m(a,c,1),e=m(a,d,-1);a.replaceRange(a.getRange(e,d)+a.getRange(c,e)+a.getRange(b,c),b,d)},"Ctrl-Alt-U":k(function(a){for(var b=a.getCursor(),c=b.line,b=b.ch,d=[];c>=a.firstLine();){for(var e=a.getLine(c),f=null==b?e.length:b;0<f;)if(b=e.charAt(--f),")"==b)d.push("(");else if("]"==b)d.push("[");else if("}"==b)d.push("{");else if(/[\(\{\[]/.test(b)&&(!d.length||d.pop()!=b))return a.extendSelection(h(c,f));--c;b=null}}),"Alt-Space":function(a){for(var b=a.getCursor(),
c=b.ch,d=b.ch,e=a.getLine(b.line);c&&/\s/.test(e.charAt(c-1));)--c;for(;d<e.length&&/\s/.test(e.charAt(d));)++d;a.replaceRange(" ",h(b.line,c),h(b.line,d))},"Ctrl-O":k(function(a){a.replaceSelection("\n","start")}),"Ctrl-T":k(function(a){var b=a.getCursor();b.ch<a.getLine(b.line).length&&(b=h(b.line,b.ch+1));var c=a.findPosH(b,-2,"char"),d=a.getRange(c,b);2==d.length&&(a.setSelection(c,b),a.replaceSelection(d.charAt(1)+d.charAt(0)))}),"Alt-C":k(function(a){D(a,function(a){var c=a.search(/\w/);return-1==
c?a:a.slice(0,c)+a.charAt(c).toUpperCase()+a.slice(c+1).toLowerCase()})}),"Alt-U":k(function(a){D(a,function(a){return a.toUpperCase()})}),"Alt-L":k(function(a){D(a,function(a){return a.toLowerCase()})}),"Alt-;":"toggleComment","Ctrl-/":k("undo"),"Shift-Ctrl--":k("undo"),"Ctrl-Z":k("undo"),"Cmd-Z":k("undo"),"Shift-Alt-,":"goDocStart","Shift-Alt-.":"goDocEnd","Ctrl-S":"findNext","Ctrl-R":"findPrev","Ctrl-G":function(a){a.execCommand("clearSearch");a.setExtending(!1);a.setCursor(a.getCursor())},"Shift-Alt-5":"replace",
"Alt-/":"autocomplete","Ctrl-J":"newlineAndIndent",Enter:!1,Tab:"indentAuto","Alt-G":function(a){a.setOption("keyMap","emacs-Alt-G")},"Ctrl-X":function(a){a.setOption("keyMap","emacs-Ctrl-X")},"Ctrl-Q":function(a){a.setOption("keyMap","emacs-Ctrl-Q")},"Ctrl-U":function(a){a.state.emacsPrefixMap=!0;a.addKeyMap(C);a.on("keyHandled",x);a.on("inputRead",x)}};g.keyMap["emacs-Ctrl-X"]={Tab:function(a){a.indentSelection(q(a,!0)||a.getOption("indentUnit"))},"Ctrl-X":function(a){a.setSelection(a.getCursor("head"),
a.getCursor("anchor"))},"Ctrl-S":"save","Ctrl-W":"save",S:"saveAll",F:"open",U:k("undo"),K:"close",Delete:function(a){s(a,a.getCursor(),w(a,a.getCursor(),1),!0)},auto:"emacs",nofallthrough:!0,disableInput:!0};g.keyMap["emacs-Alt-G"]={G:function(a){var b=q(a,!0);if(null!=b&&0<b)return a.setCursor(b-1);N(a,"Goto line",function(b){var d;b&&!isNaN(d=Number(b))&&d==d|0&&0<d&&a.setCursor(d-1)})},auto:"emacs",nofallthrough:!0,disableInput:!0};g.keyMap["emacs-Ctrl-Q"]={Tab:k("insertTab"),auto:"emacs",nofallthrough:!0};
var C={"Ctrl-G":A};for(g=0;10>g;++g)L(String(g));L("-")});
