(function(){function e(){this.posFrom=this.posTo=this.query=null,this.marked=[]}function t(t){return t._searchState||(t._searchState=new e)}function n(e,t,n){return e.getSearchCursor(t,n,"string"==typeof t&&t==t.toLowerCase())}function i(e,t,n,i){e.openDialog?e.openDialog(t,i):i(prompt(n,""))}function r(e,t,n,i){e.openConfirm?e.openConfirm(t,i):confirm(n)&&i[0]()}function a(e){var t=e.match(/^\/(.*)\/([a-z]*)$/);return t?RegExp(t[1],-1==t[2].indexOf("i")?"":"i"):e}function o(e,r){var o=t(e);return o.query?s(e,r):(i(e,u,"Search for:",function(t){e.operation(function(){if(t&&!o.query){if(o.query=a(t),2e3>e.lineCount())for(var i=n(e,o.query);i.findNext();)o.marked.push(e.markText(i.from(),i.to(),"CodeMirror-searching"));o.posFrom=o.posTo=e.getCursor(),s(e,r)}})}),void 0)}function s(e,i){e.operation(function(){var r=t(e),a=n(e,r.query,i?r.posFrom:r.posTo);(a.find(i)||(a=n(e,r.query,i?{line:e.lineCount()-1}:{line:0,ch:0}),a.find(i)))&&(e.setSelection(a.from(),a.to()),r.posFrom=a.from(),r.posTo=a.to())})}function l(e){e.operation(function(){var n=t(e);if(n.query){n.query=null;for(var i=0;n.marked.length>i;++i)n.marked[i].clear();n.marked.length=0}})}function c(e,t){i(e,d,"Replace:",function(o){o&&(o=a(o),i(e,p,"Replace with:",function(i){function a(){var t,i=c.from();!(t=c.findNext())&&(c=n(e,o),!(t=c.findNext())||i&&c.from().line==i.line&&c.from().ch==i.ch)||(e.setSelection(c.from(),c.to()),r(e,m,"Replace?",[function(){s(t)},a]))}function s(e){c.replace("string"==typeof o?i:i.replace(/\$(\d)/,function(t,n){return e[n]})),a()}if(t)e.compoundChange(function(){e.operation(function(){for(var t=n(e,o);t.findNext();)if("string"!=typeof o){var r=e.getRange(t.from(),t.to()).match(o);t.replace(i.replace(/\$(\d)/,function(e,t){return r[t]}))}else t.replace(i)})});else{l(e);var c=n(e,o,e.getCursor());a()}}))})}var u='Search: (Use /re/ syntax for regexp search)',d='Replace: (Use /re/ syntax for regexp search)',p='With: ',m="Replace? ";CodeMirror.commands.find=function(e){l(e),o(e)},CodeMirror.commands.findNext=o,CodeMirror.commands.findPrev=function(e){o(e,!0)},CodeMirror.commands.clearSearch=l,CodeMirror.commands.replace=c,CodeMirror.commands.replaceAll=function(e){c(e,!0)}})();