(function(e){"object"==typeof exports&&"object"==typeof module?e(require("../../lib/codemirror"),require("./searchcursor"),require("../dialog/dialog")):"function"==typeof define&&define.amd?define(["../../lib/codemirror","./searchcursor","../dialog/dialog"],e):e(CodeMirror)})(function(e){function s(a,c){var b;"string"==typeof a?(b=a.charAt(0),a=RegExp("^"+a.replace(/[\-\[\]\/\{\}\(\)\*\+\?\.\\\^\$\|]/g,"\\$&"),c?"i":"")):a=RegExp("^(?:"+a.source+")",a.ignoreCase?"i":"");return{token:function(d){if(d.match(a))return"searching";
for(;!d.eol()&&(d.next(),b&&(d.skipTo(b)||d.skipToEnd()),!d.match(a,!1)););}}}function t(){this.overlay=this.posFrom=this.posTo=this.query=null}function g(a){return a.state.search||(a.state.search=new t)}function n(a){return"string"==typeof a&&a==a.toLowerCase()}function h(a,c,b){return a.getSearchCursor(c,b,n(c))}function k(a,c,b,d,f){a.openDialog?a.openDialog(c,f,{value:d}):f(prompt(b,d))}function u(a,c,b,d){if(a.openConfirm)a.openConfirm(c,d);else if(confirm(b))d[0]()}function p(a){var c=a.match(/^\/(.*)\/([a-z]*)$/);
return c?RegExp(c[1],-1==c[2].indexOf("i")?"":"i"):a}function l(a,c){var b=g(a);if(b.query)return q(a,c);k(a,v,"Search for:",a.getSelection(),function(d){a.operation(function(){d&&!b.query&&(b.query=p(d),a.removeOverlay(b.overlay,n(b.query)),b.overlay=s(b.query),a.addOverlay(b.overlay),b.posFrom=b.posTo=a.getCursor(),q(a,c))})})}function q(a,c){a.operation(function(){var b=g(a),d=h(a,b.query,c?b.posFrom:b.posTo);if(!d.find(c)&&(d=h(a,b.query,c?e.Pos(a.lastLine()):e.Pos(a.firstLine(),0)),!d.find(c)))return;
a.setSelection(d.from(),d.to());a.scrollIntoView({from:d.from(),to:d.to()});b.posFrom=d.from();b.posTo=d.to()})}function m(a){return a.operation(function(){var c=g(a);if(c.query)return c.query=null,a.removeOverlay(c.overlay),!0})}function r(a,c){k(a,w,"Replace:",a.getSelection(),function(b){b&&(b=p(b),k(a,x,"Replace with:","",function(d){if(c)a.operation(function(){for(var c=h(a,b);c.findNext();)if("string"!=typeof b){var e=a.getRange(c.from(),c.to()).match(b);c.replace(d.replace(/\$(\d)/,function(a,
b){return e[b]}))}else c.replace(d)});else{m(a);var f=h(a,b,a.getCursor()),e=function(){var c=f.from(),d;if(!(d=f.findNext())&&(f=h(a,b),!(d=f.findNext())||c&&f.from().line==c.line&&f.from().ch==c.ch))return;a.setSelection(f.from(),f.to());a.scrollIntoView({from:f.from(),to:f.to()});u(a,y,"Replace?",[function(){g(d)},e])},g=function(a){f.replace("string"==typeof b?d:d.replace(/\$(\d)/,function(b,c){return a[c]}));e()};e()}}))})}var v='Search: <input type="text" style="width: 10em"/> <span style="color: #888">(Use /re/ syntax for regexp search)</span>',
w='Replace: <input type="text" style="width: 10em"/> <span style="color: #888">(Use /re/ syntax for regexp search)</span>',x='With: <input type="text" style="width: 10em"/>',y="Replace? <button>Yes</button> <button>No</button> <button>Stop</button>";e.commands.find=function(a){m(a);l(a)};e.commands.findNext=l;e.commands.findPrev=function(a){l(a,!0)};e.commands.clearSearch=m;e.commands.replace=r;e.commands.replaceAll=function(a){r(a,!0)}});
