﻿!function(e){"object"==typeof exports&&"object"==typeof module?e(require("../../lib/codemirror")):"function"==typeof define&&define.amd?define("addon/search/searchcursor",["../../lib/codemirror"],e):e(CodeMirror)}(function(e){"use strict";function n(e){var n=e.flags;return null!=n?n:(e.ignoreCase?"i":"")+(e.global?"g":"")+(e.multiline?"m":"")}function t(e,t){for(var o=n(e),r=o,i=0;i<t.length;i++)-1==r.indexOf(t.charAt(i))&&(r+=t.charAt(i));return o==r?e:new RegExp(e.source,r)}function o(e){return/\\s|\\n|\n|\\W|\\D|\[\^/.test(e.source)}function r(e,n,o){n=t(n,"g");for(var r=o.line,i=o.ch,s=e.lastLine();r<=s;r++,i=0){n.lastIndex=i;var l=e.getLine(r),c=n.exec(l);if(c)return{from:g(r,c.index),to:g(r,c.index+c[0].length),match:c}}}function i(e,n,i){if(!o(n))return r(e,n,i);n=t(n,"gm");for(var s,l=1,c=i.line,a=e.lastLine();c<=a;){for(var u=0;u<l&&!(c>a);u++){var f=e.getLine(c++);s=null==s?f:s+"\n"+f}l*=2,n.lastIndex=i.ch;var h=n.exec(s);if(h){var d=s.slice(0,h.index).split("\n"),p=h[0].split("\n"),m=i.line+d.length-1,v=d[d.length-1].length;return{from:g(m,v),to:g(m+p.length-1,1==p.length?v+p[0].length:p[p.length-1].length),match:h}}}}function s(e,n){for(var t,o=0;;){n.lastIndex=o;var r=n.exec(e);if(!r)return t;if(t=r,(o=t.index+(t[0].length||1))==e.length)return t}}function l(e,n,o){n=t(n,"g");for(var r=o.line,i=o.ch,l=e.firstLine();r>=l;r--,i=-1){var c=e.getLine(r);i>-1&&(c=c.slice(0,i));var a=s(c,n);if(a)return{from:g(r,a.index),to:g(r,a.index+a[0].length),match:a}}}function c(e,n,o){n=t(n,"gm");for(var r,i=1,l=o.line,c=e.firstLine();l>=c;){for(var a=0;a<i;a++){var u=e.getLine(l--);r=null==r?u.slice(0,o.ch):u+"\n"+r}i*=2;var f=s(r,n);if(f){var h=r.slice(0,f.index).split("\n"),d=f[0].split("\n"),p=l+h.length,m=h[h.length-1].length;return{from:g(p,m),to:g(p+d.length-1,1==d.length?m+d[0].length:d[d.length-1].length),match:f}}}}function a(e,n,t,o){if(e.length==n.length)return t;for(var r=0,i=t+Math.max(0,e.length-n.length);;){if(r==i)return r;var s=r+i>>1,l=o(e.slice(0,s)).length;if(l==t)return s;l>t?i=s:r=s+1}}function u(e,n,t,o){if(!n.length)return null;var r=o?d:p,i=r(n).split(/\r|\n\r?/);e:for(var s=t.line,l=t.ch,c=e.lastLine()+1-i.length;s<=c;s++,l=0){var u=e.getLine(s).slice(l),f=r(u);if(1==i.length){var h=f.indexOf(i[0]);if(-1==h)continue e;var t=a(u,f,h,r)+l;return{from:g(s,a(u,f,h,r)+l),to:g(s,a(u,f,h+i[0].length,r)+l)}}var m=f.length-i[0].length;if(f.slice(m)==i[0]){for(var v=1;v<i.length-1;v++)if(r(e.getLine(s+v))!=i[v])continue e;var y=e.getLine(s+i.length-1),x=r(y),C=i[i.length-1];if(x.slice(0,C.length)==C)return{from:g(s,a(u,f,m,r)+l),to:g(s+i.length-1,a(y,x,C.length,r))}}}}function f(e,n,t,o){if(!n.length)return null;var r=o?d:p,i=r(n).split(/\r|\n\r?/);e:for(var s=t.line,l=t.ch,c=e.firstLine()-1+i.length;s>=c;s--,l=-1){var u=e.getLine(s);l>-1&&(u=u.slice(0,l));var f=r(u);if(1==i.length){var h=f.lastIndexOf(i[0]);if(-1==h)continue e;return{from:g(s,a(u,f,h,r)),to:g(s,a(u,f,h+i[0].length,r))}}var m=i[i.length-1];if(f.slice(0,m.length)==m){for(var v=1,t=s-i.length+1;v<i.length-1;v++)if(r(e.getLine(t+v))!=i[v])continue e;var y=e.getLine(s+1-i.length),x=r(y);if(x.slice(x.length-i[0].length)==i[0])return{from:g(s+1-i.length,a(y,x,y.length-i[0].length,r)),to:g(s,a(u,f,m.length,r))}}}}function h(e,n,o,s){this.atOccurrence=!1,this.doc=e,o=o?e.clipPos(o):g(0,0),this.pos={from:o,to:o};var a;"object"==typeof s?a=s.caseFold:(a=s,s=null),"string"==typeof n?(null==a&&(a=!1),this.matches=function(t,o){return(t?f:u)(e,n,o,a)}):(n=t(n,"gm"),s&&!1===s.multiline?this.matches=function(t,o){return(t?l:r)(e,n,o)}:this.matches=function(t,o){return(t?c:i)(e,n,o)})}var d,p,g=e.Pos;String.prototype.normalize?(d=function(e){return e.normalize("NFD").toLowerCase()},p=function(e){return e.normalize("NFD")}):(d=function(e){return e.toLowerCase()},p=function(e){return e}),h.prototype={findNext:function(){return this.find(!1)},findPrevious:function(){return this.find(!0)},find:function(n){for(var t=this.matches(n,this.doc.clipPos(n?this.pos.from:this.pos.to));t&&0==e.cmpPos(t.from,t.to);)n?t.from.ch?t.from=g(t.from.line,t.from.ch-1):t=t.from.line==this.doc.firstLine()?null:this.matches(n,this.doc.clipPos(g(t.from.line-1))):t.to.ch<this.doc.getLine(t.to.line).length?t.to=g(t.to.line,t.to.ch+1):t=t.to.line==this.doc.lastLine()?null:this.matches(n,g(t.to.line+1,0));if(t)return this.pos=t,this.atOccurrence=!0,this.pos.match||!0;var o=g(n?this.doc.firstLine():this.doc.lastLine()+1,0);return this.pos={from:o,to:o},this.atOccurrence=!1},from:function(){if(this.atOccurrence)return this.pos.from},to:function(){if(this.atOccurrence)return this.pos.to},replace:function(n,t){if(this.atOccurrence){var o=e.splitLines(n);this.doc.replaceRange(o,this.pos.from,this.pos.to,t),this.pos.to=g(this.pos.from.line+o.length-1,o[o.length-1].length+(1==o.length?this.pos.from.ch:0))}}},e.defineExtension("getSearchCursor",function(e,n,t){return new h(this.doc,e,n,t)}),e.defineDocExtension("getSearchCursor",function(e,n,t){return new h(this,e,n,t)}),e.defineExtension("selectMatches",function(n,t){for(var o=[],r=this.getSearchCursor(n,this.getCursor("from"),t);r.findNext()&&!(e.cmpPos(r.to(),this.getCursor("to"))>0);)o.push({anchor:r.from(),head:r.to()});o.length&&this.setSelections(o,0)})}),function(e){"object"==typeof exports&&"object"==typeof module?e(require("../../lib/codemirror")):"function"==typeof define&&define.amd?define("addon/dialog/dialog",["../../lib/codemirror"],e):e(CodeMirror)}(function(e){function n(n,t,o){var r,i=n.getWrapperElement();return r=i.appendChild(document.createElement("div")),r.className=o?"CodeMirror-dialog CodeMirror-dialog-bottom":"CodeMirror-dialog CodeMirror-dialog-top","string"==typeof t?r.innerHTML=t:r.appendChild(t),e.addClass(i,"dialog-opened"),r}function t(e,n){e.state.currentNotificationClose&&e.state.currentNotificationClose(),e.state.currentNotificationClose=n}e.defineExtension("openDialog",function(o,r,i){function s(n){if("string"==typeof n)f.value=n;else{if(a)return;a=!0,e.rmClass(c.parentNode,"dialog-opened"),c.parentNode.removeChild(c),u.focus(),i.onClose&&i.onClose(c)}}i||(i={}),t(this,null);var l,c=n(this,o,i.bottom),a=!1,u=this,f=c.getElementsByTagName("input")[0];return f?(f.focus(),i.value&&(f.value=i.value,!1!==i.selectValueOnOpen&&f.select()),i.onInput&&e.on(f,"input",function(e){i.onInput(e,f.value,s)}),i.onKeyUp&&e.on(f,"keyup",function(e){i.onKeyUp(e,f.value,s)}),e.on(f,"keydown",function(n){i&&i.onKeyDown&&i.onKeyDown(n,f.value,s)||((27==n.keyCode||!1!==i.closeOnEnter&&13==n.keyCode)&&(f.blur(),e.e_stop(n),s()),13==n.keyCode&&r(f.value,n))}),!1!==i.closeOnBlur&&e.on(f,"blur",s)):(l=c.getElementsByTagName("button")[0])&&(e.on(l,"click",function(){s(),u.focus()}),!1!==i.closeOnBlur&&e.on(l,"blur",s),l.focus()),s}),e.defineExtension("openConfirm",function(o,r,i){function s(){a||(a=!0,e.rmClass(l.parentNode,"dialog-opened"),l.parentNode.removeChild(l),u.focus())}t(this,null);var l=n(this,o,i&&i.bottom),c=l.getElementsByTagName("button"),a=!1,u=this,f=1;c[0].focus();for(var h=0;h<c.length;++h){var d=c[h];!function(n){e.on(d,"click",function(t){e.e_preventDefault(t),s(),n&&n(u)})}(r[h]),e.on(d,"blur",function(){--f,setTimeout(function(){f<=0&&s()},200)}),e.on(d,"focus",function(){++f})}}),e.defineExtension("openNotification",function(o,r){function i(){c||(c=!0,clearTimeout(s),e.rmClass(l.parentNode,"dialog-opened"),l.parentNode.removeChild(l))}t(this,i);var s,l=n(this,o,r&&r.bottom),c=!1,a=r&&void 0!==r.duration?r.duration:5e3;return e.on(l,"click",function(n){e.e_preventDefault(n),i()}),a&&(s=setTimeout(i,a)),i})}),function(e){"object"==typeof exports&&"object"==typeof module?e(require("../../lib/codemirror"),require("./searchcursor"),require("../dialog/dialog")):"function"==typeof define&&define.amd?define("addon/search/search.js",["../../lib/codemirror","./searchcursor","../dialog/dialog"],e):e(CodeMirror)}(function(e){"use strict";function n(e,n){return"string"==typeof e?e=new RegExp(e.replace(/[\-\[\]\/\{\}\(\)\*\+\?\.\\\^\$\|]/g,"\\$&"),n?"gi":"g"):e.global||(e=new RegExp(e.source,e.ignoreCase?"gi":"g")),{token:function(n){e.lastIndex=n.pos;var t=e.exec(n.string);if(t&&t.index==n.pos)return n.pos+=t[0].length||1,"searching";t?n.pos=t.index:n.skipToEnd()}}}function t(){this.posFrom=this.posTo=this.lastQuery=this.query=null,this.overlay=null}function o(e){return e.state.search||(e.state.search=new t)}function r(e){return"string"==typeof e&&e==e.toLowerCase()}function i(e,n,t){return e.getSearchCursor(n,t,{caseFold:r(n),multiline:!0})}function s(e,n,t,o,r){e.openDialog(n,o,{value:t,selectValueOnOpen:!0,closeOnEnter:!1,onClose:function(){p(e)},onKeyDown:r})}function l(e,n,t,o,r){e.openDialog?e.openDialog(n,r,{value:o,selectValueOnOpen:!0}):r(prompt(t,o))}function c(e,n,t,o){e.openConfirm?e.openConfirm(n,o):confirm(t)&&o[0]()}function a(e){return e.replace(/\\(.)/g,function(e,n){return"n"==n?"\n":"r"==n?"\r":n})}function u(e){var n=e.match(/^\/(.*)\/([a-z]*)$/);if(n)try{e=new RegExp(n[1],-1==n[2].indexOf("i")?"":"i")}catch(e){}else e=a(e);return("string"==typeof e?""==e:e.test(""))&&(e=/x^/),e}function f(e,t,o){t.queryText=o,t.query=u(o),e.removeOverlay(t.overlay,r(t.query)),t.overlay=n(t.query,r(t.query)),e.addOverlay(t.overlay),e.showMatchesOnScrollbar&&(t.annotate&&(t.annotate.clear(),t.annotate=null),t.annotate=e.showMatchesOnScrollbar(t.query,r(t.query)))}function h(n,t,r,i){var c=o(n);if(c.query)return d(n,t);var a=n.getSelection()||c.lastQuery;if(a instanceof RegExp&&"x^"==a.source&&(a=null),r&&n.openDialog){var u=null,h=function(t,o){e.e_stop(o),t&&(t!=c.queryText&&(f(n,c,t),c.posFrom=c.posTo=n.getCursor()),u&&(u.style.opacity=1),d(n,o.shiftKey,function(e,t){var o;t.line<3&&document.querySelector&&(o=n.display.wrapper.querySelector(".CodeMirror-dialog"))&&o.getBoundingClientRect().bottom-4>n.cursorCoords(t,"window").top&&((u=o).style.opacity=.4)}))};s(n,v,a,h,function(t,r){var i=e.keyName(t),s=n.getOption("extraKeys"),l=s&&s[i]||e.keyMap[n.getOption("keyMap")][i];"findNext"==l||"findPrev"==l||"findPersistentNext"==l||"findPersistentPrev"==l?(e.e_stop(t),f(n,o(n),r),n.execCommand(l)):"find"!=l&&"findPersistent"!=l||(e.e_stop(t),h(r,t))}),i&&a&&(f(n,c,a),d(n,t))}else l(n,v,"Search for:",a,function(e){e&&!c.query&&n.operation(function(){f(n,c,e),c.posFrom=c.posTo=n.getCursor(),d(n,t)})})}function d(n,t,r){n.operation(function(){var s=o(n),l=i(n,s.query,t?s.posFrom:s.posTo);(l.find(t)||(l=i(n,s.query,t?e.Pos(n.lastLine()):e.Pos(n.firstLine(),0)),l.find(t)))&&(n.setSelection(l.from(),l.to()),n.scrollIntoView({from:l.from(),to:l.to()},20),s.posFrom=l.from(),s.posTo=l.to(),r&&r(l.from(),l.to()))})}function p(e){e.operation(function(){var n=o(e);n.lastQuery=n.query,n.query&&(n.query=n.queryText=null,e.removeOverlay(n.overlay),n.annotate&&(n.annotate.clear(),n.annotate=null))})}function g(e,n,t){e.operation(function(){for(var o=i(e,n);o.findNext();)if("string"!=typeof n){var r=e.getRange(o.from(),o.to()).match(n);o.replace(t.replace(/\$(\d)/g,function(e,n){return r[n]}))}else o.replace(t)})}function m(e,n){if(!e.getOption("readOnly")){var t=e.getSelection()||o(e).lastQuery,r='<span class="CodeMirror-search-label">'+(n?"Replace all:":"Replace:")+"</span>";l(e,r+y,r,t,function(t){t&&(t=u(t),l(e,x,"Replace with:","",function(o){if(o=a(o),n)g(e,t,o);else{p(e);var r=i(e,t,e.getCursor("from")),s=function(){var n,a=r.from();!(n=r.findNext())&&(r=i(e,t),!(n=r.findNext())||a&&r.from().line==a.line&&r.from().ch==a.ch)||(e.setSelection(r.from(),r.to()),e.scrollIntoView({from:r.from(),to:r.to()}),c(e,C,"Replace?",[function(){l(n)},s,function(){g(e,t,o)}]))},l=function(e){r.replace("string"==typeof t?o:o.replace(/\$(\d)/g,function(n,t){return e[t]})),s()};s()}}))})}}var v='<span class="CodeMirror-search-label">Search:</span> <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>',y=' <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>',x='<span class="CodeMirror-search-label">With:</span> <input type="text" style="width: 10em" class="CodeMirror-search-field"/>',C='<span class="CodeMirror-search-label">Replace?</span> <button>Yes</button> <button>No</button> <button>All</button> <button>Stop</button>';e.commands.find=function(e){p(e),h(e)},e.commands.findPersistent=function(e){p(e),h(e,!1,!0)},e.commands.findPersistentNext=function(e){h(e,!1,!0,!0)},e.commands.findPersistentPrev=function(e){h(e,!0,!0,!0)},e.commands.findNext=h,e.commands.findPrev=function(e){h(e,!0)},e.commands.clearSearch=p,e.commands.replace=m,e.commands.replaceAll=function(e){m(e,!0)}}),function(e){"function"==typeof e.define&&e.define("addonSearch",["addon/search/search.js"],function(){})}(this);
