Contact
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutSign UpSign In
Avatar for KuCalc : dev & ops.
| Download
Views: 39649
1
// CodeMirror, copyright (c) by Marijn Haverbeke and others
2
// Distributed under an MIT license: http://codemirror.net/LICENSE
3
4
// Define search commands. Depends on dialog.js or another
5
// implementation of the openDialog method.
6
7
// Replace works a little oddly -- it will do the replace on the next
8
// Ctrl-G (or whatever is bound to findNext) press. You prevent a
9
// replace by making sure the match is no longer selected when hitting
10
// Ctrl-G.
11
12
13
"use strict";
14
15
function searchOverlay(query, caseInsensitive) {
16
if (typeof query == "string")
17
query = new RegExp(query.replace(/[\-\[\]\/\{\}\(\)\*\+\?\.\\\^\$\|]/g, "\\$&"), caseInsensitive ? "gi" : "g");
18
else if (!query.global)
19
query = new RegExp(query.source, query.ignoreCase ? "gi" : "g");
20
21
return {token: function(stream) {
22
query.lastIndex = stream.pos;
23
var match = query.exec(stream.string);
24
if (match && match.index == stream.pos) {
25
stream.pos += match[0].length || 1;
26
return "searching";
27
} else if (match) {
28
stream.pos = match.index;
29
} else {
30
stream.skipToEnd();
31
}
32
}};
33
}
34
35
function SearchState() {
36
this.posFrom = this.posTo = this.lastQuery = this.query = null;
37
this.overlay = null;
38
}
39
40
function getSearchState(cm) {
41
return cm.state.search || (cm.state.search = new SearchState());
42
}
43
44
function queryCaseInsensitive(query) {
45
return typeof query == "string" && query == query.toLowerCase();
46
}
47
48
function getSearchCursor(cm, query, pos) {
49
// Heuristic: if the query string is all lowercase, do a case insensitive search.
50
return cm.getSearchCursor(query, pos, queryCaseInsensitive(query));
51
}
52
53
function persistentDialog(cm, text, deflt, onEnter, onKeyDown) {
54
cm.openDialog(text, onEnter, {
55
value: deflt,
56
selectValueOnOpen: true,
57
closeOnEnter: false,
58
onClose: function() { clearSearch(cm); },
59
onKeyDown: onKeyDown
60
});
61
}
62
63
function dialog(cm, text, shortText, deflt, f) {
64
if (cm.openDialog) cm.openDialog(text, f, {value: deflt, selectValueOnOpen: true});
65
else f(prompt(shortText, deflt));
66
}
67
68
function confirmDialog(cm, text, shortText, fs) {
69
if (cm.openConfirm) cm.openConfirm(text, fs);
70
else if (confirm(shortText)) fs[0]();
71
}
72
73
function parseString(string) {
74
return string.replace(/\\(.)/g, function(_, ch) {
75
if (ch == "n") return "\n"
76
if (ch == "r") return "\r"
77
return ch
78
})
79
}
80
81
function parseQuery(query) {
82
var isRE = query.match(/^\/(.*)\/([a-z]*)$/);
83
if (isRE) {
84
try { query = new RegExp(isRE[1], isRE[2].indexOf("i") == -1 ? "" : "i"); }
85
catch(e) {} // Not a regular expression after all, do a string search
86
} else {
87
query = parseString(query)
88
}
89
if (typeof query == "string" ? query == "" : query.test(""))
90
query = /x^/;
91
return query;
92
}
93
94
if(navigator.platform == "MacIntel") {
95
var queryDialog =
96
'Search: <input type="text" style="width: 10em" class="CodeMirror-search-field"/> <span style="color: #666" class="CodeMirror-search-hint">Hit enter, then Next (⌘-G) and Prev (Shift-⌘-G). Use /re/ for regular expression.';
97
} else {
98
var queryDialog =
99
'Search: <input type="text" style="width: 10em" class="CodeMirror-search-field"/> <span style="color: #666" class="CodeMirror-search-hint">Hit enter, then Next (Ctrl-G) and Prev (Shift-Ctrl-G). Use /re/ for regular expression.';
100
}
101
102
function startSearch(cm, state, query) {
103
state.queryText = query;
104
state.query = parseQuery(query);
105
cm.removeOverlay(state.overlay, queryCaseInsensitive(state.query));
106
state.overlay = searchOverlay(state.query, queryCaseInsensitive(state.query));
107
cm.addOverlay(state.overlay);
108
if (cm.showMatchesOnScrollbar) {
109
if (state.annotate) { state.annotate.clear(); state.annotate = null; }
110
state.annotate = cm.showMatchesOnScrollbar(state.query, queryCaseInsensitive(state.query));
111
}
112
}
113
114
function doSearch(cm, rev, persistent, immediate) {
115
var state = getSearchState(cm);
116
if (state.query) return findNext(cm, rev);
117
var q = cm.getSelection() || state.lastQuery;
118
if (persistent && cm.openDialog) {
119
var hiding = null
120
var searchNext = function(query, event) {
121
CodeMirror.e_stop(event);
122
if (!query) return;
123
if (query != state.queryText) {
124
startSearch(cm, state, query);
125
state.posFrom = state.posTo = cm.getCursor();
126
}
127
if (hiding) hiding.style.opacity = 1
128
findNext(cm, event.shiftKey, function(_, to) {
129
var dialog
130
if (to.line < 3 && document.querySelector &&
131
(dialog = cm.display.wrapper.querySelector(".CodeMirror-dialog")) &&
132
dialog.getBoundingClientRect().bottom - 4 > cm.cursorCoords(to, "window").top)
133
(hiding = dialog).style.opacity = .4
134
})
135
};
136
persistentDialog(cm, queryDialog, q, searchNext, function(event, query) {
137
var keyName = CodeMirror.keyName(event)
138
var cmd = CodeMirror.keyMap[cm.getOption("keyMap")][keyName]
139
if (!cmd) cmd = cm.getOption('extraKeys')[keyName]
140
if (cmd == "findNext" || cmd == "findPrev" ||
141
cmd == "findPersistentNext" || cmd == "findPersistentPrev") {
142
CodeMirror.e_stop(event);
143
startSearch(cm, getSearchState(cm), query);
144
cm.execCommand(cmd);
145
} else if (cmd == "find" || cmd == "findPersistent") {
146
CodeMirror.e_stop(event);
147
searchNext(query, event);
148
}
149
});
150
if (immediate && q) {
151
startSearch(cm, state, q);
152
findNext(cm, rev);
153
}
154
} else {
155
dialog(cm, queryDialog, "Search for:", q, function(query) {
156
if (query && !state.query) cm.operation(function() {
157
startSearch(cm, state, query);
158
state.posFrom = state.posTo = cm.getCursor();
159
findNext(cm, rev);
160
});
161
});
162
}
163
}
164
165
/* Return true if the given position is part of a collapsed mark.
166
See https://github.com/sagemathinc/cocalc/issues/522
167
We need this since codemirror's own search doesn't ignore
168
collapsed ranges, e.g., the output in sage worksheets.
169
*/
170
function isCollapsedPos(cm, pos) {
171
var marks = cm.findMarksAt(pos);
172
for(var i=0; i<marks.length; i++) {
173
if(marks[i].collapsed) {
174
return true;
175
}
176
}
177
return false;
178
}
179
180
function findNext(cm, rev, callback) {cm.operation(function() {
181
var state = getSearchState(cm);
182
var cursor = getSearchCursor(cm, state.query, rev ? state.posFrom : state.posTo);
183
if (!cursor.find(rev)) {
184
cursor = getSearchCursor(cm, state.query, rev ? CodeMirror.Pos(cm.lastLine()) : CodeMirror.Pos(cm.firstLine(), 0));
185
if (!cursor.find(rev)) {
186
state.init = false;
187
return;
188
}
189
}
190
cm.setSelection(cursor.from(), cursor.to());
191
cm.scrollIntoView({from: cursor.from(), to: cursor.to()}, 20);
192
state.posFrom = cursor.from(); state.posTo = cursor.to();
193
194
if (isCollapsedPos(cm, state.posFrom) &&
195
(!state.init || (state.init.ch != state.posFrom.ch || state.init.line != state.posFrom.line))) {
196
/* In a collapsed range -- try again. This won't lead to an infinite loop in case
197
of no , since that's handled by the "if (!cursor.find(rev))" above and only_one below. */
198
if (!state.init) {
199
state.init = {line:state.posFrom.line, ch:state.posFrom.ch};
200
}
201
findNext(cm, rev, callback);
202
return
203
}
204
state.init = false;
205
if (callback) callback(cursor.from(), cursor.to())
206
});}
207
208
209
210
function clearSearch(cm) {cm.operation(function() {
211
var state = getSearchState(cm);
212
state.lastQuery = state.query;
213
if (!state.query) return;
214
state.query = state.queryText = null;
215
cm.removeOverlay(state.overlay);
216
if (state.annotate) { state.annotate.clear(); state.annotate = null; }
217
});}
218
219
var replaceQueryDialog =
220
' <input type="text" style="width: 10em" class="CodeMirror-search-field"/> <span style="color: #666" class="CodeMirror-search-hint">Hit enter (use /re/ syntax for regular expression search)</span>';
221
var replacementQueryDialog = 'With: <input type="text" style="width: 10em" class="CodeMirror-search-field"/>';
222
var doReplaceConfirm = "Replace? <button>Yes</button> <button>No</button> <button>All</button> <button>Stop</button>";
223
224
function replaceAll(cm, query, text) {
225
cm.operation(function() {
226
for (var cursor = getSearchCursor(cm, query); cursor.findNext();) {
227
if (isCollapsedPos(cm, cursor.from())) {
228
/* ignore when cursor starts in a collapsed range. */
229
continue;
230
}
231
if (typeof query != "string") {
232
var match = cm.getRange(cursor.from(), cursor.to()).match(query);
233
cursor.replace(text.replace(/\$(\d)/g, function(_, i) {return match[i];}));
234
} else cursor.replace(text);
235
}
236
});
237
}
238
239
function replace(cm, all) {
240
if (cm.getOption("readOnly")) return;
241
var query = cm.getSelection() || getSearchState(cm).lastQuery;
242
var dialogText = all ? "Replace all:" : "Replace:"
243
dialog(cm, dialogText + replaceQueryDialog, dialogText, query, function(query) {
244
if (!query) return;
245
query = parseQuery(query);
246
dialog(cm, replacementQueryDialog, "Replace with:", "", function(text) {
247
text = parseString(text)
248
if (all) {
249
replaceAll(cm, query, text)
250
} else {
251
clearSearch(cm);
252
var cursor = getSearchCursor(cm, query, cm.getCursor("from"));
253
var init = false;
254
var advance = function() {
255
var start = cursor.from(), match;
256
if (!(match = cursor.findNext())) {
257
cursor = getSearchCursor(cm, query);
258
if (!(match = cursor.findNext()) ||
259
(start && cursor.from().line == start.line && cursor.from().ch == start.ch))
260
{ init = false;
261
return; }
262
}
263
cm.setSelection(cursor.from(), cursor.to());
264
cm.scrollIntoView({from: cursor.from(), to: cursor.to()});
265
266
/* search again when cursor is in a collapsed range. */
267
var pos = cursor.from();
268
if (isCollapsedPos(cm, pos) &&
269
(!init || (init.ch != pos.ch || init.line != pos.line))) {
270
if (!init) {
271
init = {line:pos.line, ch:pos.ch};
272
}
273
advance();
274
return;
275
}
276
277
init = false;
278
confirmDialog(cm, doReplaceConfirm, "Replace?",
279
[function() {doReplace(match);}, advance,
280
function() {replaceAll(cm, query, text)}]);
281
};
282
var doReplace = function(match) {
283
cursor.replace(typeof query == "string" ? text :
284
text.replace(/\$(\d)/g, function(_, i) {return match[i];}));
285
advance();
286
};
287
advance();
288
}
289
});
290
});
291
}
292
293
CodeMirror.commands.find = function(cm) {clearSearch(cm); doSearch(cm);};
294
CodeMirror.commands.findNext = doSearch;
295
CodeMirror.commands.findPrev = function(cm) {doSearch(cm, true);};
296
297
CodeMirror.commands.findPersistent = function(cm) {clearSearch(cm); doSearch(cm, false, true);};
298
CodeMirror.commands.findPersistentNext = function(cm) {doSearch(cm, false, true, true);};
299
CodeMirror.commands.findPersistentPrev = function(cm) {doSearch(cm, true, true, true);};
300
301
CodeMirror.commands.findPersistent = function(cm) {clearSearch(cm); doSearch(cm, false, true);};
302
CodeMirror.commands.findPersistentNext = function(cm) {doSearch(cm, false, true, true);};
303
CodeMirror.commands.findPersistentPrev = function(cm) {doSearch(cm, true, true, true);};
304
305
CodeMirror.commands.clearSearch = clearSearch;
306
CodeMirror.commands.replace = replace;
307
CodeMirror.commands.replaceAll = function(cm) {replace(cm, true);};
308
309
310