aboutsummaryrefslogtreecommitdiff
path: root/imports/codemirror/lib/util/search.js
diff options
context:
space:
mode:
Diffstat (limited to 'imports/codemirror/lib/util/search.js')
-rw-r--r--imports/codemirror/lib/util/search.js32
1 files changed, 18 insertions, 14 deletions
diff --git a/imports/codemirror/lib/util/search.js b/imports/codemirror/lib/util/search.js
index 63ebca9b..c5a2bccf 100644
--- a/imports/codemirror/lib/util/search.js
+++ b/imports/codemirror/lib/util/search.js
@@ -14,6 +14,10 @@
14 function getSearchState(cm) { 14 function getSearchState(cm) {
15 return cm._searchState || (cm._searchState = new SearchState()); 15 return cm._searchState || (cm._searchState = new SearchState());
16 } 16 }
17 function getSearchCursor(cm, query, pos) {
18 // Heuristic: if the query string is all lowercase, do a case insensitive search.
19 return cm.getSearchCursor(query, pos, typeof query == "string" && query == query.toLowerCase());
20 }
17 function dialog(cm, text, shortText, f) { 21 function dialog(cm, text, shortText, f) {
18 if (cm.openDialog) cm.openDialog(text, f); 22 if (cm.openDialog) cm.openDialog(text, f);
19 else f(prompt(shortText, "")); 23 else f(prompt(shortText, ""));
@@ -23,11 +27,11 @@
23 else if (confirm(shortText)) fs[0](); 27 else if (confirm(shortText)) fs[0]();
24 } 28 }
25 function parseQuery(query) { 29 function parseQuery(query) {
26 var isRE = query.match(/^\/(.*)\/$/); 30 var isRE = query.match(/^\/(.*)\/([a-z]*)$/);
27 return isRE ? new RegExp(isRE[1]) : query; 31 return isRE ? new RegExp(isRE[1], isRE[2].indexOf("i") == -1 ? "" : "i") : query;
28 } 32 }
29 var queryDialog = 33 var queryDialog =
30 'Search: <input type="text" style="width: 10em"> <span style="color: #888">(Use /re/ syntax for regexp search)</span>'; 34 'Search: <input type="text" style="width: 10em"/> <span style="color: #888">(Use /re/ syntax for regexp search)</span>';
31 function doSearch(cm, rev) { 35 function doSearch(cm, rev) {
32 var state = getSearchState(cm); 36 var state = getSearchState(cm);
33 if (state.query) return findNext(cm, rev); 37 if (state.query) return findNext(cm, rev);
@@ -36,7 +40,7 @@
36 if (!query || state.query) return; 40 if (!query || state.query) return;
37 state.query = parseQuery(query); 41 state.query = parseQuery(query);
38 if (cm.lineCount() < 2000) { // This is too expensive on big documents. 42 if (cm.lineCount() < 2000) { // This is too expensive on big documents.
39 for (var cursor = cm.getSearchCursor(query); cursor.findNext();) 43 for (var cursor = getSearchCursor(cm, query); cursor.findNext();)
40 state.marked.push(cm.markText(cursor.from(), cursor.to(), "CodeMirror-searching")); 44 state.marked.push(cm.markText(cursor.from(), cursor.to(), "CodeMirror-searching"));
41 } 45 }
42 state.posFrom = state.posTo = cm.getCursor(); 46 state.posFrom = state.posTo = cm.getCursor();
@@ -46,9 +50,9 @@
46 } 50 }
47 function findNext(cm, rev) {cm.operation(function() { 51 function findNext(cm, rev) {cm.operation(function() {
48 var state = getSearchState(cm); 52 var state = getSearchState(cm);
49 var cursor = cm.getSearchCursor(state.query, rev ? state.posFrom : state.posTo); 53 var cursor = getSearchCursor(cm, state.query, rev ? state.posFrom : state.posTo);
50 if (!cursor.find(rev)) { 54 if (!cursor.find(rev)) {
51 cursor = cm.getSearchCursor(state.query, rev ? {line: cm.lineCount() - 1} : {line: 0, ch: 0}); 55 cursor = getSearchCursor(cm, state.query, rev ? {line: cm.lineCount() - 1} : {line: 0, ch: 0});
52 if (!cursor.find(rev)) return; 56 if (!cursor.find(rev)) return;
53 } 57 }
54 cm.setSelection(cursor.from(), cursor.to()); 58 cm.setSelection(cursor.from(), cursor.to());
@@ -63,8 +67,8 @@
63 })} 67 })}
64 68
65 var replaceQueryDialog = 69 var replaceQueryDialog =
66 'Replace: <input type="text" style="width: 10em"> <span style="color: #888">(Use /re/ syntax for regexp search)</span>'; 70 'Replace: <input type="text" style="width: 10em"/> <span style="color: #888">(Use /re/ syntax for regexp search)</span>';
67 var replacementQueryDialog = 'With: <input type="text" style="width: 10em">'; 71 var replacementQueryDialog = 'With: <input type="text" style="width: 10em"/>';
68 var doReplaceConfirm = "Replace? <button>Yes</button> <button>No</button> <button>Stop</button>"; 72 var doReplaceConfirm = "Replace? <button>Yes</button> <button>No</button> <button>Stop</button>";
69 function replace(cm, all) { 73 function replace(cm, all) {
70 dialog(cm, replaceQueryDialog, "Replace:", function(query) { 74 dialog(cm, replaceQueryDialog, "Replace:", function(query) {
@@ -72,23 +76,23 @@
72 query = parseQuery(query); 76 query = parseQuery(query);
73 dialog(cm, replacementQueryDialog, "Replace with:", function(text) { 77 dialog(cm, replacementQueryDialog, "Replace with:", function(text) {
74 if (all) { 78 if (all) {
75 cm.operation(function() { 79 cm.compoundChange(function() { cm.operation(function() {
76 for (var cursor = cm.getSearchCursor(query); cursor.findNext();) { 80 for (var cursor = getSearchCursor(cm, query); cursor.findNext();) {
77 if (typeof query != "string") { 81 if (typeof query != "string") {
78 var match = cm.getRange(cursor.from(), cursor.to()).match(query); 82 var match = cm.getRange(cursor.from(), cursor.to()).match(query);
79 cursor.replace(text.replace(/\$(\d)/, function(w, i) {return match[i];})); 83 cursor.replace(text.replace(/\$(\d)/, function(w, i) {return match[i];}));
80 } else cursor.replace(text); 84 } else cursor.replace(text);
81 } 85 }
82 }); 86 })});
83 } else { 87 } else {
84 clearSearch(cm); 88 clearSearch(cm);
85 var cursor = cm.getSearchCursor(query, cm.getCursor()); 89 var cursor = getSearchCursor(cm, query, cm.getCursor());
86 function advance() { 90 function advance() {
87 var start = cursor.from(), match; 91 var start = cursor.from(), match;
88 if (!(match = cursor.findNext())) { 92 if (!(match = cursor.findNext())) {
89 cursor = cm.getSearchCursor(query); 93 cursor = getSearchCursor(cm, query);
90 if (!(match = cursor.findNext()) || 94 if (!(match = cursor.findNext()) ||
91 (cursor.from().line == start.line && cursor.from().ch == start.ch)) return; 95 (start && cursor.from().line == start.line && cursor.from().ch == start.ch)) return;
92 } 96 }
93 cm.setSelection(cursor.from(), cursor.to()); 97 cm.setSelection(cursor.from(), cursor.to());
94 confirmDialog(cm, doReplaceConfirm, "Replace?", 98 confirmDialog(cm, doReplaceConfirm, "Replace?",