]> git.r.bdr.sh - rbdr/dotfiles/blame - atom/packages/pretty-json/node_modules/jsonminify/report/assets/scripts/vendor/codemirror/util/search.js
Remove mc config
[rbdr/dotfiles] / atom / packages / pretty-json / node_modules / jsonminify / report / assets / scripts / vendor / codemirror / util / search.js
CommitLineData
06a3d686
BB
1// Define search commands. Depends on dialog.js or another
2// implementation of the openDialog method.
3
4// Replace works a little oddly -- it will do the replace on the next
5// Ctrl-G (or whatever is bound to findNext) press. You prevent a
6// replace by making sure the match is no longer selected when hitting
7// Ctrl-G.
8
9(function() {
10 function SearchState() {
11 this.posFrom = this.posTo = this.query = null;
12 this.marked = [];
13 }
14 function getSearchState(cm) {
15 return cm._searchState || (cm._searchState = new SearchState());
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 }
21 function dialog(cm, text, shortText, f) {
22 if (cm.openDialog) cm.openDialog(text, f);
23 else f(prompt(shortText, ""));
24 }
25 function confirmDialog(cm, text, shortText, fs) {
26 if (cm.openConfirm) cm.openConfirm(text, fs);
27 else if (confirm(shortText)) fs[0]();
28 }
29 function parseQuery(query) {
30 var isRE = query.match(/^\/(.*)\/([a-z]*)$/);
31 return isRE ? new RegExp(isRE[1], isRE[2].indexOf("i") == -1 ? "" : "i") : query;
32 }
33 var queryDialog =
34 'Search: <input type="text" style="width: 10em"/> <span style="color: #888">(Use /re/ syntax for regexp search)</span>';
35 function doSearch(cm, rev) {
36 var state = getSearchState(cm);
37 if (state.query) return findNext(cm, rev);
38 dialog(cm, queryDialog, "Search for:", function(query) {
39 cm.operation(function() {
40 if (!query || state.query) return;
41 state.query = parseQuery(query);
42 if (cm.lineCount() < 2000) { // This is too expensive on big documents.
43 for (var cursor = getSearchCursor(cm, state.query); cursor.findNext();)
44 state.marked.push(cm.markText(cursor.from(), cursor.to(),
45 {className: "CodeMirror-searching"}));
46 }
47 state.posFrom = state.posTo = cm.getCursor();
48 findNext(cm, rev);
49 });
50 });
51 }
52 function findNext(cm, rev) {cm.operation(function() {
53 var state = getSearchState(cm);
54 var cursor = getSearchCursor(cm, state.query, rev ? state.posFrom : state.posTo);
55 if (!cursor.find(rev)) {
56 cursor = getSearchCursor(cm, state.query, rev ? {line: cm.lineCount() - 1} : {line: 0, ch: 0});
57 if (!cursor.find(rev)) return;
58 }
59 cm.setSelection(cursor.from(), cursor.to());
60 state.posFrom = cursor.from(); state.posTo = cursor.to();
61 });}
62 function clearSearch(cm) {cm.operation(function() {
63 var state = getSearchState(cm);
64 if (!state.query) return;
65 state.query = null;
66 for (var i = 0; i < state.marked.length; ++i) state.marked[i].clear();
67 state.marked.length = 0;
68 });}
69
70 var replaceQueryDialog =
71 'Replace: <input type="text" style="width: 10em"/> <span style="color: #888">(Use /re/ syntax for regexp search)</span>';
72 var replacementQueryDialog = 'With: <input type="text" style="width: 10em"/>';
73 var doReplaceConfirm = "Replace? <button>Yes</button> <button>No</button> <button>Stop</button>";
74 function replace(cm, all) {
75 dialog(cm, replaceQueryDialog, "Replace:", function(query) {
76 if (!query) return;
77 query = parseQuery(query);
78 dialog(cm, replacementQueryDialog, "Replace with:", function(text) {
79 if (all) {
80 cm.operation(function() {
81 for (var cursor = getSearchCursor(cm, query); cursor.findNext();) {
82 if (typeof query != "string") {
83 var match = cm.getRange(cursor.from(), cursor.to()).match(query);
84 cursor.replace(text.replace(/\$(\d)/, function(_, i) {return match[i];}));
85 } else cursor.replace(text);
86 }
87 });
88 } else {
89 clearSearch(cm);
90 var cursor = getSearchCursor(cm, query, cm.getCursor());
91 function advance() {
92 var start = cursor.from(), match;
93 if (!(match = cursor.findNext())) {
94 cursor = getSearchCursor(cm, query);
95 if (!(match = cursor.findNext()) ||
96 (start && cursor.from().line == start.line && cursor.from().ch == start.ch)) return;
97 }
98 cm.setSelection(cursor.from(), cursor.to());
99 confirmDialog(cm, doReplaceConfirm, "Replace?",
100 [function() {doReplace(match);}, advance]);
101 }
102 function doReplace(match) {
103 cursor.replace(typeof query == "string" ? text :
104 text.replace(/\$(\d)/, function(_, i) {return match[i];}));
105 advance();
106 }
107 advance();
108 }
109 });
110 });
111 }
112
113 CodeMirror.commands.find = function(cm) {clearSearch(cm); doSearch(cm);};
114 CodeMirror.commands.findNext = doSearch;
115 CodeMirror.commands.findPrev = function(cm) {doSearch(cm, true);};
116 CodeMirror.commands.clearSearch = clearSearch;
117 CodeMirror.commands.replace = replace;
118 CodeMirror.commands.replaceAll = function(cm) {replace(cm, true);};
119})();