Skip to content

Commit 99b5a84

Browse files
committed
Fix some bugs in search util script
1 parent 80ea07c commit 99b5a84

File tree

4 files changed

+10
-9
lines changed

4 files changed

+10
-9
lines changed

demo/search.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ <h1>CodeMirror: Search/Replace Demo</h1>
7171
<dt>Ctrl-F / Cmd-F</dt><dd>Start searching</dd>
7272
<dt>Ctrl-G / Cmd-G</dt><dd>Find next</dd>
7373
<dt>Shift-Ctrl-G / Shift-Cmd-G</dt><dd>Find previous</dd>
74-
<dt>Ctrl-R / Cmd-Option-F</dt><dd>Replace</dd>
74+
<dt>Shift-Ctrl-F / Cmd-Option-F</dt><dd>Replace</dd>
7575
<dt>Shift-Ctrl-R / Shift-Cmd-Option-F</dt><dd>Replace all</dd>
7676
</dl>
7777
<p>Searching is enabled by

lib/codemirror.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1846,7 +1846,7 @@ var CodeMirror = (function() {
18461846
"Ctrl-Home": "goDocStart", "Alt-Up": "goDocStart", "Ctrl-End": "goDocEnd", "Ctrl-Down": "goDocEnd",
18471847
"Ctrl-Left": "goWordLeft", "Ctrl-Right": "goWordRight", "Alt-Left": "goLineStart", "Alt-Right": "goLineEnd",
18481848
"Ctrl-Backspace": "delWordLeft", "Ctrl-Delete": "delWordRight", "Ctrl-S": "save", "Ctrl-F": "find",
1849-
"Ctrl-G": "findNext", "Shift-Ctrl-G": "findPrev", "Ctrl-R": "replace", "Shift-Ctrl-R": "replaceAll",
1849+
"Ctrl-G": "findNext", "Shift-Ctrl-G": "findPrev", "Shift-Ctrl-F": "replace", "Shift-Ctrl-R": "replaceAll",
18501850
fallthrough: "basic"
18511851
};
18521852
keyMap.macDefault = {

lib/util/search.js

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,8 @@
88

99
(function() {
1010
function SearchState() {
11-
this.lastPos = null; this.query = this.replacing = null; this.marked = [];
11+
this.posFrom = this.posTo = null;
12+
this.query = this.replacing = null; this.marked = [];
1213
}
1314
function getSearchState(cm) {
1415
return cm._searchState || (cm._searchState = new SearchState());
@@ -34,7 +35,7 @@
3435
for (var cursor = cm.getSearchCursor(query); cursor.findNext();)
3536
state.marked.push(cm.markText(cursor.from(), cursor.to(), "CodeMirror-searching"));
3637
}
37-
state.lastPos = cm.getCursor();
38+
state.posFrom = state.posTo = cm.getCursor();
3839
findNext(cm, rev);
3940
});
4041
});
@@ -50,13 +51,13 @@
5051
if (match) cm.replaceSelection(state.replacing.replace(/\$(\d)/, function(w, i) {return match[i];}));
5152
}
5253
}
53-
var cursor = cm.getSearchCursor(state.query, state.lastPos);
54+
var cursor = cm.getSearchCursor(state.query, rev ? state.posFrom : state.posTo);
5455
if (!cursor.find(rev)) {
5556
cursor = cm.getSearchCursor(state.query, rev ? {line: cm.lineCount() - 1} : {line: 0, ch: 0});
5657
if (!cursor.find(rev)) return;
5758
}
5859
cm.setSelection(cursor.from(), cursor.to());
59-
state.lastPos = rev ? cursor.from() : cursor.to();
60+
state.posFrom = cursor.from(); state.posTo = cursor.to();
6061
})}
6162
function clearSearch(cm) {cm.operation(function() {
6263
var state = getSearchState(cm);
@@ -88,7 +89,7 @@
8889
clearSearch(cm);
8990
state.query = query;
9091
state.replacing = text;
91-
state.lastPos = cm.getCursor();
92+
state.posFrom = state.posTo = cm.getCursor();
9293
findNext(cm);
9394
}
9495
});

lib/util/searchcursor.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@
5555
return;
5656
for (;;) {
5757
if (reverse ? !ln : ln == cm.lineCount() - 1) return;
58-
line = fold(getLine(ln += reverse ? -1 : 1).text);
58+
line = fold(cm.getLine(ln += reverse ? -1 : 1));
5959
match = target[reverse ? --idx : ++idx];
6060
if (idx > 0 && idx < target.length - 1) {
6161
if (line != match) return;
@@ -91,7 +91,7 @@
9191
}
9292
if (reverse) {
9393
if (!pos.line) return savePosAndFail(0);
94-
pos = {line: pos.line-1, ch: getLine(pos.line-1).text.length};
94+
pos = {line: pos.line-1, ch: this.cm.getLine(pos.line-1).length};
9595
}
9696
else {
9797
var maxLine = this.cm.lineCount();

0 commit comments

Comments
 (0)