(function(g){"object"==typeof exports&&"object"==typeof module?g(require("../../lib/codemirror")):"function"==typeof define&&define.amd?define(["../../lib/codemirror"],g):g(CodeMirror)})(function(g){function q(a,d,c,b){this.atOccurrence=!1;this.doc=a;null==b&&"string"==typeof d&&(b=!1);c=c?a.clipPos(c):l(0,0);this.pos={from:c,to:c};if("string"!=typeof d)d.global||(d=RegExp(d.source,d.ignoreCase?"ig":"g")),this.matches=function(c,k){if(c){d.lastIndex=0;for(var b=a.getLine(k.line).slice(0,k.ch),f=0,
e,h;;){d.lastIndex=f;f=d.exec(b);if(!f)break;e=f;h=e.index;f=e.index+(e[0].length||1);if(f==b.length)break}(f=e&&e[0].length||0)||(0==h&&0==b.length?e=void 0:h!=a.getLine(k.line).length&&f++)}else d.lastIndex=k.ch,b=a.getLine(k.line),f=(e=d.exec(b))&&e[0].length||0,h=e&&e.index,h+f==b.length||f||(f=1);if(e&&f)return{from:l(k.line,h),to:l(k.line,h+f),match:e}};else{var g=d;b&&(d=d.toLowerCase());var n=b?function(a){return a.toLowerCase()}:function(a){return a},m=d.split("\n");if(1==m.length)this.matches=
d.length?function(b,k){if(b){var c=a.getLine(k.line).slice(0,k.ch),f=n(c),e=f.lastIndexOf(d);if(-1<e)return e=r(c,f,e),{from:l(k.line,e),to:l(k.line,e+g.length)}}else if(c=a.getLine(k.line).slice(k.ch),f=n(c),e=f.indexOf(d),-1<e)return e=r(c,f,e)+k.ch,{from:l(k.line,e),to:l(k.line,e+g.length)}}:function(){};else{var p=g.split("\n");this.matches=function(d,b){var c=m.length-1;if(d){if(!(b.line-(m.length-1)<a.firstLine())&&n(a.getLine(b.line).slice(0,p[c].length))==m[m.length-1]){for(var f=l(b.line,
p[c].length),e=b.line-1,h=c-1;1<=h;--h,--e)if(m[h]!=n(a.getLine(e)))return;var h=a.getLine(e),g=h.length-p[0].length;if(n(h.slice(g))==m[0])return{from:l(e,g),to:f}}}else if(!(b.line+(m.length-1)>a.lastLine())&&(h=a.getLine(b.line),g=h.length-p[0].length,n(h.slice(g))==m[0])){f=l(b.line,g);e=b.line+1;for(h=1;h<c;++h,++e)if(m[h]!=n(a.getLine(e)))return;if(a.getLine(e).slice(0,p[c].length)==m[c])return{from:f,to:l(e,p[c].length)}}}}}}function r(a,d,c){if(a.length==d.length)return c;for(d=Math.min(c,
a.length);;){var b=a.slice(0,d).toLowerCase().length;if(b<c)++d;else if(b>c)--d;else return d}}var l=g.Pos;q.prototype={findNext:function(){return this.find(!1)},findPrevious:function(){return this.find(!0)},find:function(a){function d(a){a=l(a,0);c.pos={from:a,to:a};return c.atOccurrence=!1}for(var c=this,b=this.doc.clipPos(a?this.pos.from:this.pos.to);;){if(this.pos=this.matches(a,b))return this.atOccurrence=!0,this.pos.match||!0;if(a){if(!b.line)return d(0);b=l(b.line-1,this.doc.getLine(b.line-
1).length)}else{var g=this.doc.lineCount();if(b.line==g-1)return d(g);b=l(b.line+1,0)}}},from:function(){if(this.atOccurrence)return this.pos.from},to:function(){if(this.atOccurrence)return this.pos.to},replace:function(a){this.atOccurrence&&(a=g.splitLines(a),this.doc.replaceRange(a,this.pos.from,this.pos.to),this.pos.to=l(this.pos.from.line+a.length-1,a[a.length-1].length+(1==a.length?this.pos.from.ch:0)))}};g.defineExtension("getSearchCursor",function(a,d,c){return new q(this.doc,a,d,c)});g.defineDocExtension("getSearchCursor",
function(a,d,c){return new q(this,a,d,c)});g.defineExtension("selectMatches",function(a,d){for(var c=[],b=this.getSearchCursor(a,this.getCursor("from"),d);b.findNext()&&!(0<g.cmpPos(b.to(),this.getCursor("to")));)c.push({anchor:b.from(),head:b.to()});c.length&&this.setSelections(c,0)})});
