23
if (typeof exports == "object" && typeof module == "object")
24
mod(require("../../lib/codemirror"), require("./matchesonscrollbar"));
25
else if (typeof define == "function" && define.amd)
26
define(["../../lib/codemirror", "./matchesonscrollbar"], mod);
29
})(function(CodeMirror) {
33
style: "matchhighlight",
37
annotateScrollbar: false,
42
function State(options) {
44
for (var name in defaults)
45
this.options[name] = (options && options.hasOwnProperty(name) ? options : defaults)[name]
46
this.overlay = this.timeout = null;
47
this.matchesonscroll = null;
51
CodeMirror.defineOption("highlightSelectionMatches", false, function(cm, val, old) {
52
if (old && old != CodeMirror.Init) {
54
clearTimeout(cm.state.matchHighlighter.timeout);
55
cm.state.matchHighlighter = null;
56
cm.off("cursorActivity", cursorActivity);
57
cm.off("focus", onFocus)
60
var state = cm.state.matchHighlighter = new State(val);
65
cm.on("focus", onFocus)
67
cm.on("cursorActivity", cursorActivity);
71
function cursorActivity(cm) {
72
var state = cm.state.matchHighlighter;
73
if (state.active || cm.hasFocus()) scheduleHighlight(cm, state)
76
function onFocus(cm) {
77
var state = cm.state.matchHighlighter
80
scheduleHighlight(cm, state)
84
function scheduleHighlight(cm, state) {
85
clearTimeout(state.timeout);
86
state.timeout = setTimeout(function() {highlightMatches(cm);}, state.options.delay);
89
function addOverlay(cm, query, hasBoundary, style) {
90
var state = cm.state.matchHighlighter;
91
cm.addOverlay(state.overlay = makeOverlay(query, hasBoundary, style));
92
if (state.options.annotateScrollbar && cm.showMatchesOnScrollbar) {
93
var searchFor = hasBoundary ? new RegExp((/\w/.test(query.charAt(0)) ? "\\b" : "") +
94
query.replace(/[\\\[.+*?(){|^$]/g, "\\$&") +
95
(/\w/.test(query.charAt(query.length - 1)) ? "\\b" : "")) : query;
96
state.matchesonscroll = cm.showMatchesOnScrollbar(searchFor, false,
97
{className: "CodeMirror-selection-highlight-scrollbar"});
101
function removeOverlay(cm) {
102
var state = cm.state.matchHighlighter;
104
cm.removeOverlay(state.overlay);
105
state.overlay = null;
106
if (state.matchesonscroll) {
107
state.matchesonscroll.clear();
108
state.matchesonscroll = null;
113
function highlightMatches(cm) {
114
cm.operation(function() {
115
var state = cm.state.matchHighlighter;
117
if (!cm.somethingSelected() && state.options.showToken) {
118
var re = state.options.showToken === true ? /[\w$]/ : state.options.showToken;
119
var cur = cm.getCursor(), line = cm.getLine(cur.line), start = cur.ch, end = start;
120
while (start && re.test(line.charAt(start - 1))) --start;
121
while (end < line.length && re.test(line.charAt(end))) ++end;
123
addOverlay(cm, line.slice(start, end), re, state.options.style);
126
var from = cm.getCursor("from"), to = cm.getCursor("to");
127
if (from.line != to.line) return;
128
if (state.options.wordsOnly && !isWord(cm, from, to)) return;
129
var selection = cm.getRange(from, to)
130
if (state.options.trim) selection = selection.replace(/^\s+|\s+$/g, "")
131
if (selection.length >= state.options.minChars)
132
addOverlay(cm, selection, false, state.options.style);
136
function isWord(cm, from, to) {
137
var str = cm.getRange(from, to);
138
if (str.match(/^\w+$/) !== null) {
140
var pos = {line: from.line, ch: from.ch - 1};
141
var chr = cm.getRange(pos, from);
142
if (chr.match(/\W/) === null) return false;
144
if (to.ch < cm.getLine(from.line).length) {
145
var pos = {line: to.line, ch: to.ch + 1};
146
var chr = cm.getRange(to, pos);
147
if (chr.match(/\W/) === null) return false;
153
function boundariesAround(stream, re) {
154
return (!stream.start || !re.test(stream.string.charAt(stream.start - 1))) &&
155
(stream.pos == stream.string.length || !re.test(stream.string.charAt(stream.pos)));
158
function makeOverlay(query, hasBoundary, style) {
159
return {token: function(stream) {
160
if (stream.match(query) &&
161
(!hasBoundary || boundariesAround(stream, hasBoundary)))
164
stream.skipTo(query.charAt(0)) || stream.skipToEnd();