From 514a1227e49351eb56a9a11453643a1ac0ac1c88 Mon Sep 17 00:00:00 2001 From: tophf Date: Sat, 7 Mar 2015 03:08:04 +0300 Subject: [PATCH] Search all sections on find/findNext/findPrev --- edit.js | 83 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 83 insertions(+) diff --git a/edit.js b/edit.js index e0f87d5f..35f2d1e7 100644 --- a/edit.js +++ b/edit.js @@ -149,6 +149,88 @@ function removeSection(event) { makeDirty(); } +function setupGlobalSearch() { + var originalCommand = { + find: CodeMirror.commands.find, + findNext: CodeMirror.commands.findNext, + findPrev: CodeMirror.commands.findPrev + } + + function shouldIgnoreCase(query) { // treat all-lowercase non-regexp queries as case-insensitive + return typeof query == "string" && query == query.toLowerCase(); + } + + function find(activeCM) { + var originalOpenDialog = activeCM.openDialog; + activeCM.openDialog = function(template, callback, options) { + originalOpenDialog.call(activeCM, template, function(query) { + activeCM.openDialog = originalOpenDialog; + callback(query); + var state = activeCM.state.search; + if (editors.length == 1 || !state.query) { + return; + } + for (var i=0; i < editors.length; i++) { + var cm = editors[i]; + if (cm == activeCM) { + continue; + } + cm.execCommand("clearSearch"); + cm.state.search = { + query: state.query, + overlay: state.overlay, + annotate: cm.showMatchesOnScrollbar(state.query, shouldIgnoreCase(state.query)) + } + cm.addOverlay(state.overlay); + } + if (CodeMirror.cmpPos(activeCM.state.search.posFrom, activeCM.state.search.posTo) == 0) { + findNext(activeCM); + } + }, options); + } + originalCommand.find(activeCM); + } + + function findNext(activeCM, reverse) { + if (!activeCM.state.search || !activeCM.state.search.query) { + find(activeCM); + return; + } + var pos = activeCM.getCursor(); + // check if the search term is currently selected in the editor + var m = activeCM.getSelection().match(activeCM.state.search.query); + if (m && m[0].length == activeCM.getSelection().length) { + pos = activeCM.getCursor(reverse ? "from" : "to"); + activeCM.setSelection(activeCM.getCursor()); + } + + for (var i=0, cm=activeCM; i < editors.length; i++) { + var state = cm.state.search; + if (cm != activeCM) { + pos = reverse ? CodeMirror.Pos(cm.lastLine()) : CodeMirror.Pos(0, 0); + } + var searchCursor = cm.getSearchCursor(state.query, pos, shouldIgnoreCase(state.query)); + if (searchCursor.find(reverse) || editors.length == 1) { + if (editors.length > 1) { + cm.focus(); + } + // speedup the original findNext + state.posFrom = reverse ? searchCursor.to() : searchCursor.from(); + state.posTo = CodeMirror.Pos(state.posFrom.line, state.posFrom.ch); + originalCommand[reverse ? "findPrev" : "findNext"](cm); + return; + } + cm = editors[(editors.indexOf(cm) + (reverse ? -1 + editors.length : 1)) % editors.length]; + } + // nothing found so far, so call the original search with wrap-around + originalCommand[reverse ? "findPrev" : "findNext"](activeCM); + } + + CodeMirror.commands.find = find; + CodeMirror.commands.findNext = function(cm) { findNext(cm) } + CodeMirror.commands.findPrev = function(cm) { findNext(cm, true) } +} + window.addEventListener("load", init, false); function init() { @@ -188,6 +270,7 @@ function initWithStyle(style) { div.parentNode.removeChild(div); }); style.sections.forEach(addSection); + setupGlobalSearch(); } function initTitle(name) {