(function(f){"object"==typeof exports&&"object"==typeof module?f(require("../../lib/codemirror"),require("./matchesonscrollbar")):"function"==typeof define&&define.amd?define(["../../lib/codemirror","./matchesonscrollbar"],f):f(CodeMirror)})(function(f){function r(a){this.options={};for(var b in h)this.options[b]=(a&&a.hasOwnProperty(b)?a:h)[b];this.matchesonscroll=this.overlay=this.timeout=null;this.active=!1}function k(a){var b=a.state.matchHighlighter;(b.active||a.hasFocus())&&l(a,b)}function m(a){var b=
a.state.matchHighlighter;b.active||(b.active=!0,l(a,b))}function l(a,b){clearTimeout(b.timeout);b.timeout=setTimeout(function(){n(a)},b.options.delay)}function p(a,b,d,c){var e=a.state.matchHighlighter;a.addOverlay(e.overlay=t(b,d,c));e.options.annotateScrollbar&&a.showMatchesOnScrollbar&&(b=d?new RegExp((/\w/.test(b.charAt(0))?"\\b":"")+b.replace(/[\\\[.+*?(){|^$]/g,"\\$&")+(/\w/.test(b.charAt(b.length-1))?"\\b":"")):b,e.matchesonscroll=a.showMatchesOnScrollbar(b,!1,{className:"CodeMirror-selection-highlight-scrollbar"}))}
function q(a){var b=a.state.matchHighlighter;b.overlay&&(a.removeOverlay(b.overlay),b.overlay=null,b.matchesonscroll&&(b.matchesonscroll.clear(),b.matchesonscroll=null))}function n(a){a.operation(function(){var b=a.state.matchHighlighter;q(a);if(!a.somethingSelected()&&b.options.showToken){for(var d=!0===b.options.showToken?/[\w$]/:b.options.showToken,c=a.getCursor(),e=a.getLine(c.line),g=c=c.ch;c&&d.test(e.charAt(c-1));)--c;for(;g<e.length&&d.test(e.charAt(g));)++g;c<g&&p(a,e.slice(c,g),d,b.options.style)}else if(d=
a.getCursor("from"),e=a.getCursor("to"),d.line==e.line){if(c=b.options.wordsOnly){a:if(null!==a.getRange(d,e).match(/^\w+$/)){if(0<d.ch&&(c={line:d.line,ch:d.ch-1},c=a.getRange(c,d),null===c.match(/\W/))){c=!1;break a}if(e.ch<a.getLine(d.line).length&&(c={line:e.line,ch:e.ch+1},c=a.getRange(e,c),null===c.match(/\W/))){c=!1;break a}c=!0}else c=!1;c=!c}c||(d=a.getRange(d,e),b.options.trim&&(d=d.replace(/^\s+|\s+$/g,"")),d.length>=b.options.minChars&&p(a,d,!1,b.options.style))}})}function t(a,b,d){return{token:function(c){var e;
if(e=c.match(a))(e=!b)||(e=(!c.start||!b.test(c.string.charAt(c.start-1)))&&(c.pos==c.string.length||!b.test(c.string.charAt(c.pos))));if(e)return d;c.next();c.skipTo(a.charAt(0))||c.skipToEnd()}}}var h={style:"matchhighlight",minChars:2,delay:100,wordsOnly:!1,annotateScrollbar:!1,showToken:!1,trim:!0};f.defineOption("highlightSelectionMatches",!1,function(a,b,d){d&&d!=f.Init&&(q(a),clearTimeout(a.state.matchHighlighter.timeout),a.state.matchHighlighter=null,a.off("cursorActivity",k),a.off("focus",
m));if(b){b=a.state.matchHighlighter=new r(b);if(a.hasFocus())b.active=!0,n(a);else a.on("focus",m);a.on("cursorActivity",k)}})});
