'use strict';(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 x(a,b){"string"==typeof a?a=new RegExp(a.replace(/[\-\[\]\/\{\}\(\)\*\+\?\.\\\^\$\|]/g,"\\$&"),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 y(){this.overlay=this.posFrom=this.posTo=this.lastQuery=this.query=null}function k(a){return a.state.search||(a.state.search=new y)}function l(a){return"string"==typeof a&&a==a.toLowerCase()}function m(a,b,e){return a.getSearchCursor(b,e,l(b))}function z(a,b,e,c){a.openDialog(b,c,{value:e,selectValueOnOpen:!0,closeOnEnter:!1,onClose:function(){h(a)}})}function p(a,b,
e,c,d){a.openDialog?a.openDialog(b,d,{value:c,selectValueOnOpen:!0}):d(prompt(e,c))}function A(a,b,e,c){if(a.openConfirm)a.openConfirm(b,c);else if(confirm(e))c[0]()}function t(a){var b=a.match(/^\/(.*)\/([a-z]*)$/);if(b)try{a=new RegExp(b[1],-1==b[2].indexOf("i")?"":"i")}catch(e){}if("string"==typeof a?""==a:a.test(""))a=/x^/;return a}function u(a,b,e){b.queryText=e;b.query=t(e);a.removeOverlay(b.overlay,l(b.query));b.overlay=x(b.query,l(b.query));a.addOverlay(b.overlay);a.showMatchesOnScrollbar&&
(b.annotate&&(b.annotate.clear(),b.annotate=null),b.annotate=a.showMatchesOnScrollbar(b.query,l(b.query)))}function n(a,b,e){var c=k(a);if(c.query)return q(a,b);var d=a.getSelection()||c.lastQuery||"";if(e&&a.openDialog){var r=null;z(a,'Search: <input type="text" style="width: 10em" class="CodeMirror-search-field"/> <span style="color: #888" class="CodeMirror-search-hint">(Use /re/ syntax for regexp search)</span>',d,function(b,d){f.e_stop(d);b&&(b!=c.queryText&&u(a,c,b),r&&(r.style.opacity=1),q(a,
d.shiftKey,function(b,c){var d;3>c.line&&document.querySelector&&(d=a.display.wrapper.querySelector(".CodeMirror-dialog"))&&d.getBoundingClientRect().bottom-4>a.cursorCoords(c,"window").top&&((r=d).style.opacity=.4)}))})}else p(a,'Search: <input type="text" style="width: 10em" class="CodeMirror-search-field"/> <span style="color: #888" class="CodeMirror-search-hint">(Use /re/ syntax for regexp search)</span>',f.I18N.getMessage("Search_for")+":",d,function(d){d&&!c.query&&a.operation(function(){u(a,
c,d);c.posFrom=c.posTo=a.getCursor();q(a,b)})})}function q(a,b,e){a.operation(function(){var c=k(a),d=m(a,c.query,b?c.posFrom:c.posTo);if(!d.find(b)&&(d=m(a,c.query,b?f.Pos(a.lastLine()):f.Pos(a.firstLine(),0)),!d.find(b)))return;a.setSelection(d.from(),d.to());a.scrollIntoView({from:d.from(),to:d.to()},20);c.posFrom=d.from();c.posTo=d.to();e&&e(d.from(),d.to())})}function h(a){return a.operation(function(){var b=k(a);if(b.lastQuery=b.query)return b.query=b.queryText=null,a.removeOverlay(b.overlay),
b.annotate&&(b.annotate.clear(),b.annotate=null),!0})}function v(a,b,e){a.operation(function(){for(var c=m(a,b);c.findNext();)if("string"!=typeof b){var d=a.getRange(c.from(),c.to()).match(b);c.replace(e.replace(/\$(\d)/g,function(a,b){return d[b]}))}else c.replace(e)})}function w(a,b){if(!a.getOption("readOnly")){var e=a.getSelection()||k(a).lastQuery||"",c=(b?f.I18N.getMessage("Replace_all"):f.I18N.getMessage("Replace"))+":";p(a,c+' <input type="text" style="width: 10em" class="CodeMirror-search-field"/> <span style="color: #888" class="CodeMirror-search-hint">(Use /re/ syntax for regexp search)</span>',
c,e,function(c){c&&(c=t(c),p(a,'With: <input type="text" style="width: 10em" class="CodeMirror-search-field"/>',f.I18N.getMessage("Replace_with")+":","",function(e){if(b)v(a,c,e);else{h(a);var g=m(a,c,a.getCursor()),k=function(){var b=g.from(),h;if(!(h=g.findNext())&&(g=m(a,c),!(h=g.findNext())||b&&g.from().line==b.line&&g.from().ch==b.ch))return;a.setSelection(g.from(),g.to());a.scrollIntoView({from:g.from(),to:g.to()});window.setTimeout(function(){A(a,"Replace? <button>Yes</button> <button>No</button> <button>All</button> <button>Stop</button>",
f.I18N.getMessage("Replace_"),[function(){l(h)},k,function(){v(a,c,e)}])},1)},l=function(a){g.replace("string"==typeof c?e:e.replace(/\$(\d)/g,function(b,c){return a[c]}));k()};k()}}))})}}f.commands.find=function(a){h(a);n(a)};f.commands.findPersistent=function(a){h(a);n(a,!1,!0)};f.commands.findNext=n;f.commands.findPrev=function(a){n(a,!0)};f.commands.clearSearch=h;f.commands.replace=w;f.commands.replaceAll=function(a){w(a,!0)}});
