(function(f){"object"==typeof exports&&"object"==typeof module?f(require("../../lib/codemirror"),require("./searchcursor"),require("../dialog/dialog")):"function"==typeof define&&define.amd?define(["../../lib/codemirror","./searchcursor","../dialog/dialog"],f):f(CodeMirror)})(function(f){function y(a,b){"string"==typeof a?a=new RegExp(a.replace(/[\-\[\]\/\{\}\(\)\*\+\?\.\\\^\$\|]/g,"\\$\x26"),b?"gi":"g"):a.global||(a=new RegExp(a.source,a.ignoreCase?"gi":"g"));return{token:function(b){a.lastIndex=
b.pos;var c=a.exec(b.string);if(c&&c.index==b.pos)return b.pos+=c[0].length||1,"searching";c?b.pos=c.index:b.skipToEnd()}}}function z(){this.overlay=this.posFrom=this.posTo=this.lastQuery=this.query=null}function h(a){return a.state.search||(a.state.search=new z)}function m(a){return"string"==typeof a&&a==a.toLowerCase()}function k(a,b,d){return a.getSearchCursor(b,d,m(b))}function A(a,b,d,c,e){a.openDialog(b,c,{value:d,selectValueOnOpen:!0,closeOnEnter:!1,onClose:function(){l(a)},onKeyDown:e})}function r(a,
b,d,c,e){a.openDialog?a.openDialog(b,e,{value:c,selectValueOnOpen:!0}):e(prompt(d,c))}function B(a,b,d,c){if(a.openConfirm)a.openConfirm(b,c);else if(confirm(d))c[0]()}function u(a){return a.replace(/\\(.)/g,function(a,d){return"n"==d?"\n":"r"==d?"\r":d})}function v(a){var b=a.match(/^\/(.*)\/([a-z]*)$/);if(b)try{a=new RegExp(b[1],-1==b[2].indexOf("i")?"":"i")}catch(d){}else a=u(a);if("string"==typeof a?""==a:a.test(""))a=/x^/;return a}function n(a,b,d){b.queryText=d;b.query=v(d);a.removeOverlay(b.overlay,
m(b.query));b.overlay=y(b.query,m(b.query));a.addOverlay(b.overlay);a.showMatchesOnScrollbar&&(b.annotate&&(b.annotate.clear(),b.annotate=null),b.annotate=a.showMatchesOnScrollbar(b.query,m(b.query)))}function g(a,b,d,c){var e=h(a);if(e.query)return p(a,b);var q=a.getSelection()||e.lastQuery;if(d&&a.openDialog){var t=null,g=function(b,c){f.e_stop(c);b&&(b!=e.queryText&&(n(a,e,b),e.posFrom=e.posTo=a.getCursor()),t&&(t.style.opacity=1),p(a,c.shiftKey,function(b,e){var c;3>e.line&&document.querySelector&&
(c=a.display.wrapper.querySelector(".CodeMirror-dialog"))&&c.getBoundingClientRect().bottom-4>a.cursorCoords(e,"window").top&&((t=c).style.opacity=.4)}))};A(a,'Search: \x3cinput type\x3d"text" style\x3d"width: 10em" class\x3d"CodeMirror-search-field"/\x3e \x3cspan style\x3d"color: #888" class\x3d"CodeMirror-search-hint"\x3e(Use /re/ syntax for regexp search)\x3c/span\x3e',q,g,function(b,e){var c=f.keyName(b),d=f.keyMap[a.getOption("keyMap")][c];d||(d=a.getOption("extraKeys")[c]);if("findNext"==d||
"findPrev"==d||"findPersistentNext"==d||"findPersistentPrev"==d)f.e_stop(b),n(a,h(a),e),a.execCommand(d);else if("find"==d||"findPersistent"==d)f.e_stop(b),g(e,b)});c&&q&&(n(a,e,q),p(a,b))}else r(a,'Search: \x3cinput type\x3d"text" style\x3d"width: 10em" class\x3d"CodeMirror-search-field"/\x3e \x3cspan style\x3d"color: #888" class\x3d"CodeMirror-search-hint"\x3e(Use /re/ syntax for regexp search)\x3c/span\x3e',"Search for:",q,function(c){c&&!e.query&&a.operation(function(){n(a,e,c);e.posFrom=e.posTo=
a.getCursor();p(a,b)})})}function p(a,b,d){a.operation(function(){var c=h(a),e=k(a,c.query,b?c.posFrom:c.posTo);if(!e.find(b)&&(e=k(a,c.query,b?f.Pos(a.lastLine()):f.Pos(a.firstLine(),0)),!e.find(b)))return;a.setSelection(e.from(),e.to());a.scrollIntoView({from:e.from(),to:e.to()},20);c.posFrom=e.from();c.posTo=e.to();d&&d(e.from(),e.to())})}function l(a){a.operation(function(){var b=h(a);if(b.lastQuery=b.query)b.query=b.queryText=null,a.removeOverlay(b.overlay),b.annotate&&(b.annotate.clear(),b.annotate=
null)})}function w(a,b,d){a.operation(function(){for(var c=k(a,b);c.findNext();)if("string"!=typeof b){var e=a.getRange(c.from(),c.to()).match(b);c.replace(d.replace(/\$(\d)/g,function(a,b){return e[b]}))}else c.replace(d)})}function x(a,b){if(!a.getOption("readOnly")){var d=a.getSelection()||h(a).lastQuery,c=b?"Replace all:":"Replace:";r(a,c+' \x3cinput type\x3d"text" style\x3d"width: 10em" class\x3d"CodeMirror-search-field"/\x3e \x3cspan style\x3d"color: #888" class\x3d"CodeMirror-search-hint"\x3e(Use /re/ syntax for regexp search)\x3c/span\x3e',
c,d,function(e){e&&(e=v(e),r(a,'With: \x3cinput type\x3d"text" style\x3d"width: 10em" class\x3d"CodeMirror-search-field"/\x3e',"Replace with:","",function(c){c=u(c);if(b)w(a,e,c);else{l(a);var d=k(a,e,a.getCursor("from")),f=function(){var b=d.from(),h;if(!(h=d.findNext())&&(d=k(a,e),!(h=d.findNext())||b&&d.from().line==b.line&&d.from().ch==b.ch))return;a.setSelection(d.from(),d.to());a.scrollIntoView({from:d.from(),to:d.to()});B(a,"Replace? \x3cbutton\x3eYes\x3c/button\x3e \x3cbutton\x3eNo\x3c/button\x3e \x3cbutton\x3eAll\x3c/button\x3e \x3cbutton\x3eStop\x3c/button\x3e",
"Replace?",[function(){g(h)},f,function(){w(a,e,c)}])},g=function(a){d.replace("string"==typeof e?c:c.replace(/\$(\d)/g,function(b,c){return a[c]}));f()};f()}}))})}}f.commands.find=function(a){l(a);g(a)};f.commands.findPersistent=function(a){l(a);g(a,!1,!0)};f.commands.findPersistentNext=function(a){g(a,!1,!0,!0)};f.commands.findPersistentPrev=function(a){g(a,!0,!0,!0)};f.commands.findNext=g;f.commands.findPrev=function(a){g(a,!0)};f.commands.clearSearch=l;f.commands.replace=x;f.commands.replaceAll=
function(a){x(a,!0)}});