michael@0: // Highlighting text that matches the selection michael@0: // michael@0: // Defines an option highlightSelectionMatches, which, when enabled, michael@0: // will style strings that match the selection throughout the michael@0: // document. michael@0: // michael@0: // The option can be set to true to simply enable it, or to a michael@0: // {minChars, style, showToken} object to explicitly configure it. michael@0: // minChars is the minimum amount of characters that should be michael@0: // selected for the behavior to occur, and style is the token style to michael@0: // apply to the matches. This will be prefixed by "cm-" to create an michael@0: // actual CSS class name. showToken, when enabled, will cause the michael@0: // current token to be highlighted when nothing is selected. michael@0: michael@0: (function(mod) { michael@0: if (typeof exports == "object" && typeof module == "object") // CommonJS michael@0: mod(require("../../lib/codemirror")); michael@0: else if (typeof define == "function" && define.amd) // AMD michael@0: define(["../../lib/codemirror"], mod); michael@0: else // Plain browser env michael@0: mod(CodeMirror); michael@0: })(function(CodeMirror) { michael@0: "use strict"; michael@0: michael@0: var DEFAULT_MIN_CHARS = 2; michael@0: var DEFAULT_TOKEN_STYLE = "matchhighlight"; michael@0: var DEFAULT_DELAY = 100; michael@0: michael@0: function State(options) { michael@0: if (typeof options == "object") { michael@0: this.minChars = options.minChars; michael@0: this.style = options.style; michael@0: this.showToken = options.showToken; michael@0: this.delay = options.delay; michael@0: } michael@0: if (this.style == null) this.style = DEFAULT_TOKEN_STYLE; michael@0: if (this.minChars == null) this.minChars = DEFAULT_MIN_CHARS; michael@0: if (this.delay == null) this.delay = DEFAULT_DELAY; michael@0: this.overlay = this.timeout = null; michael@0: } michael@0: michael@0: CodeMirror.defineOption("highlightSelectionMatches", false, function(cm, val, old) { michael@0: if (old && old != CodeMirror.Init) { michael@0: var over = cm.state.matchHighlighter.overlay; michael@0: if (over) cm.removeOverlay(over); michael@0: clearTimeout(cm.state.matchHighlighter.timeout); michael@0: cm.state.matchHighlighter = null; michael@0: cm.off("cursorActivity", cursorActivity); michael@0: } michael@0: if (val) { michael@0: cm.state.matchHighlighter = new State(val); michael@0: highlightMatches(cm); michael@0: cm.on("cursorActivity", cursorActivity); michael@0: } michael@0: }); michael@0: michael@0: function cursorActivity(cm) { michael@0: var state = cm.state.matchHighlighter; michael@0: clearTimeout(state.timeout); michael@0: state.timeout = setTimeout(function() {highlightMatches(cm);}, state.delay); michael@0: } michael@0: michael@0: function highlightMatches(cm) { michael@0: cm.operation(function() { michael@0: var state = cm.state.matchHighlighter; michael@0: if (state.overlay) { michael@0: cm.removeOverlay(state.overlay); michael@0: state.overlay = null; michael@0: } michael@0: if (!cm.somethingSelected() && state.showToken) { michael@0: var re = state.showToken === true ? /[\w$]/ : state.showToken; michael@0: var cur = cm.getCursor(), line = cm.getLine(cur.line), start = cur.ch, end = start; michael@0: while (start && re.test(line.charAt(start - 1))) --start; michael@0: while (end < line.length && re.test(line.charAt(end))) ++end; michael@0: if (start < end) michael@0: cm.addOverlay(state.overlay = makeOverlay(line.slice(start, end), re, state.style)); michael@0: return; michael@0: } michael@0: if (cm.getCursor("head").line != cm.getCursor("anchor").line) return; michael@0: var selection = cm.getSelections()[0].replace(/^\s+|\s+$/g, ""); michael@0: if (selection.length >= state.minChars) michael@0: cm.addOverlay(state.overlay = makeOverlay(selection, false, state.style)); michael@0: }); michael@0: } michael@0: michael@0: function boundariesAround(stream, re) { michael@0: return (!stream.start || !re.test(stream.string.charAt(stream.start - 1))) && michael@0: (stream.pos == stream.string.length || !re.test(stream.string.charAt(stream.pos))); michael@0: } michael@0: michael@0: function makeOverlay(query, hasBoundary, style) { michael@0: return {token: function(stream) { michael@0: if (stream.match(query) && michael@0: (!hasBoundary || boundariesAround(stream, hasBoundary))) michael@0: return style; michael@0: stream.next(); michael@0: stream.skipTo(query.charAt(0)) || stream.skipToEnd(); michael@0: }}; michael@0: } michael@0: });