(function(){function t(t,n,r,i){this.atOccurrence=!1,this.doc=t,i==null&&typeof n=="string"&&(i=!1),r=r?t.clipPos(r):e(0,0),this.pos={from:r,to:r};if(typeof n!="string")n.global||(n=new RegExp(n.source,n.ignoreCase?"ig":"g")),this.matches=function(r,i){if(r){n.lastIndex=0;var s=t.getLine(i.line).slice(0,i.ch),o=0,u,a;for(;;){n.lastIndex=o;var f=n.exec(s);if(!f)break;u=f,a=u.index,o=u.index+(u[0].length||1);if(o==s.length)break}var l=u&&u[0].length||0;l||(a==0&&s.length==0?u=undefined:a!=t.getLine(i.line).length&&l++)}else{n.lastIndex=i.ch;var s=t.getLine(i.line),u=n.exec(s),l=u&&u[0].length||0,a=u&&u.index;a+l!=s.length&&!l&&(l=1)}if(u&&l)return{from:e(i.line,a),to:e(i.line,a+l),match:u}};else{i&&(n=n.toLowerCase());var s=i?function(e){return e.toLowerCase()}:function(e){return e},o=n.split("\n");o.length==1?n.length?this.matches=function(r,i){var o=s(t.getLine(i.line)),u=n.length,a;if(r?i.ch>=u&&(a=o.lastIndexOf(n,i.ch-u))!=-1:(a=o.indexOf(n,i.ch))!=-1)return{from:e(i.line,a),to:e(i.line,a+u)}}:this.matches=function(){}:this.matches=function(n,r){var i=r.line,u=n?o.length-1:0,a=o[u],f=s(t.getLine(i)),l=n?f.indexOf(a)+a.length:f.lastIndexOf(a);if(n?l>=r.ch||l!=a.length:l<=r.ch||l!=f.length-a.length)return;for(;;){if(n?!i:i==t.lineCount()-1)return;f=s(t.getLine(i+=n?-1:1)),a=o[n?--u:++u];if(u>0&&u<o.length-1){if(f!=a)return;continue}var c=n?f.lastIndexOf(a):f.indexOf(a)+a.length;if(n?c!=f.length-a.length:c!=a.length)return;var h=e(r.line,l),p=e(i,c);return{from:n?p:h,to:n?h:p}}}}}var e=CodeMirror.Pos;t.prototype={findNext:function(){return this.find(!1)},findPrevious:function(){return this.find(!0)},find:function(t){function i(t){var r=e(t,0);return n.pos={from:r,to:r},n.atOccurrence=!1,!1}var n=this,r=this.doc.clipPos(t?this.pos.from:this.pos.to);for(;;){if(this.pos=this.matches(t,r))return(!this.pos.from||!this.pos.to)&&console.log(this.matches,this.pos),this.atOccurrence=!0,this.pos.match||!0;if(t){if(!r.line)return i(0);r=e(r.line-1,this.doc.getLine(r.line-1).length)}else{var s=this.doc.lineCount();if(r.line==s-1)return i(s);r=e(r.line+1,0)}}},from:function(){if(this.atOccurrence)return this.pos.from},to:function(){if(this.atOccurrence)return this.pos.to},replace:function(t){if(!this.atOccurrence)return;var n=CodeMirror.splitLines(t);this.doc.replaceRange(n,this.pos.from,this.pos.to),this.pos.to=e(this.pos.from.line+n.length-1,n[n.length-1].length+(n.length==1?this.pos.from.ch:0))}},CodeMirror.defineExtension("getSearchCursor",function(e,n,r){return new t(this.doc,e,n,r)}),CodeMirror.defineDocExtension("getSearchCursor",function(e,n,r){return new t(this,e,n,r)})})();