1
0

search.js 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  1. // CodeMirror, copyright (c) by Marijn Haverbeke and others
  2. // Distributed under an MIT license: http://codemirror.net/LICENSE
  3. // Define search commands. Depends on dialog.js or another
  4. // implementation of the openDialog method.
  5. // Replace works a little oddly -- it will do the replace on the next
  6. // Ctrl-G (or whatever is bound to findNext) press. You prevent a
  7. // replace by making sure the match is no longer selected when hitting
  8. // Ctrl-G.
  9. (function(mod) {
  10. if (typeof exports == "object" && typeof module == "object") // CommonJS
  11. mod(require("../../lib/codemirror"), require("./searchcursor"), require("../dialog/dialog"));
  12. else if (typeof define == "function" && define.amd) // AMD
  13. define(["../../lib/codemirror", "./searchcursor", "../dialog/dialog"], mod);
  14. else // Plain browser env
  15. mod(CodeMirror);
  16. })(function(CodeMirror) {
  17. "use strict";
  18. function searchOverlay(query, caseInsensitive) {
  19. if (typeof query == "string")
  20. query = new RegExp(query.replace(/[\-\[\]\/\{\}\(\)\*\+\?\.\\\^\$\|]/g, "\\$&"), caseInsensitive ? "gi" : "g");
  21. else if (!query.global)
  22. query = new RegExp(query.source, query.ignoreCase ? "gi" : "g");
  23. return {token: function(stream) {
  24. query.lastIndex = stream.pos;
  25. var match = query.exec(stream.string);
  26. if (match && match.index == stream.pos) {
  27. stream.pos += match[0].length || 1;
  28. return "searching";
  29. } else if (match) {
  30. stream.pos = match.index;
  31. } else {
  32. stream.skipToEnd();
  33. }
  34. }};
  35. }
  36. function SearchState() {
  37. this.posFrom = this.posTo = this.lastQuery = this.query = null;
  38. this.overlay = null;
  39. }
  40. function getSearchState(cm) {
  41. return cm.state.search || (cm.state.search = new SearchState());
  42. }
  43. function queryCaseInsensitive(query) {
  44. return typeof query == "string" && query == query.toLowerCase();
  45. }
  46. function getSearchCursor(cm, query, pos) {
  47. // Heuristic: if the query string is all lowercase, do a case insensitive search.
  48. return cm.getSearchCursor(query, pos, queryCaseInsensitive(query));
  49. }
  50. function persistentDialog(cm, text, deflt, f) {
  51. cm.openDialog(text, f, {
  52. value: deflt,
  53. selectValueOnOpen: true,
  54. closeOnEnter: false,
  55. onClose: function() { clearSearch(cm); }
  56. });
  57. }
  58. function dialog(cm, text, shortText, deflt, f) {
  59. if (cm.openDialog) cm.openDialog(text, f, {value: deflt, selectValueOnOpen: true});
  60. else f(prompt(shortText, deflt));
  61. }
  62. function confirmDialog(cm, text, shortText, fs) {
  63. if (cm.openConfirm) cm.openConfirm(text, fs);
  64. else if (confirm(shortText)) fs[0]();
  65. }
  66. function parseString(string) {
  67. return string.replace(/\\(.)/g, function(_, ch) {
  68. if (ch == "n") return "\n"
  69. if (ch == "r") return "\r"
  70. return ch
  71. })
  72. }
  73. function parseQuery(query) {
  74. var isRE = query.match(/^\/(.*)\/([a-z]*)$/);
  75. if (isRE) {
  76. try { query = new RegExp(isRE[1], isRE[2].indexOf("i") == -1 ? "" : "i"); }
  77. catch(e) {} // Not a regular expression after all, do a string search
  78. } else {
  79. query = parseString(query)
  80. }
  81. if (typeof query == "string" ? query == "" : query.test(""))
  82. query = /x^/;
  83. return query;
  84. }
  85. var queryDialog =
  86. 'Search: <input type="text" style="width: 10em" class="CodeMirror-search-field"/> <span style="color: #888" class="CodeMirror-search-hint">(Use /re/ syntax for regexp search)</span>';
  87. function startSearch(cm, state, query) {
  88. state.queryText = query;
  89. state.query = parseQuery(query);
  90. cm.removeOverlay(state.overlay, queryCaseInsensitive(state.query));
  91. state.overlay = searchOverlay(state.query, queryCaseInsensitive(state.query));
  92. cm.addOverlay(state.overlay);
  93. if (cm.showMatchesOnScrollbar) {
  94. if (state.annotate) { state.annotate.clear(); state.annotate = null; }
  95. state.annotate = cm.showMatchesOnScrollbar(state.query, queryCaseInsensitive(state.query));
  96. }
  97. }
  98. function doSearch(cm, rev, persistent) {
  99. var state = getSearchState(cm);
  100. if (state.query) return findNext(cm, rev);
  101. var q = cm.getSelection() || state.lastQuery;
  102. if (persistent && cm.openDialog) {
  103. var hiding = null
  104. persistentDialog(cm, queryDialog, q, function(query, event) {
  105. CodeMirror.e_stop(event);
  106. if (!query) return;
  107. if (query != state.queryText) startSearch(cm, state, query);
  108. if (hiding) hiding.style.opacity = 1
  109. findNext(cm, event.shiftKey, function(_, to) {
  110. var dialog
  111. if (to.line < 3 && document.querySelector &&
  112. (dialog = cm.display.wrapper.querySelector(".CodeMirror-dialog")) &&
  113. dialog.getBoundingClientRect().bottom - 4 > cm.cursorCoords(to, "window").top)
  114. (hiding = dialog).style.opacity = .4
  115. })
  116. });
  117. } else {
  118. dialog(cm, queryDialog, "Search for:", q, function(query) {
  119. if (query && !state.query) cm.operation(function() {
  120. startSearch(cm, state, query);
  121. state.posFrom = state.posTo = cm.getCursor();
  122. findNext(cm, rev);
  123. });
  124. });
  125. }
  126. }
  127. function findNext(cm, rev, callback) {cm.operation(function() {
  128. var state = getSearchState(cm);
  129. var cursor = getSearchCursor(cm, state.query, rev ? state.posFrom : state.posTo);
  130. if (!cursor.find(rev)) {
  131. cursor = getSearchCursor(cm, state.query, rev ? CodeMirror.Pos(cm.lastLine()) : CodeMirror.Pos(cm.firstLine(), 0));
  132. if (!cursor.find(rev)) return;
  133. }
  134. cm.setSelection(cursor.from(), cursor.to());
  135. cm.scrollIntoView({from: cursor.from(), to: cursor.to()}, 20);
  136. state.posFrom = cursor.from(); state.posTo = cursor.to();
  137. if (callback) callback(cursor.from(), cursor.to())
  138. });}
  139. function clearSearch(cm) {cm.operation(function() {
  140. var state = getSearchState(cm);
  141. state.lastQuery = state.query;
  142. if (!state.query) return;
  143. state.query = state.queryText = null;
  144. cm.removeOverlay(state.overlay);
  145. if (state.annotate) { state.annotate.clear(); state.annotate = null; }
  146. });}
  147. var replaceQueryDialog =
  148. ' <input type="text" style="width: 10em" class="CodeMirror-search-field"/> <span style="color: #888" class="CodeMirror-search-hint">(Use /re/ syntax for regexp search)</span>';
  149. var replacementQueryDialog = 'With: <input type="text" style="width: 10em" class="CodeMirror-search-field"/>';
  150. var doReplaceConfirm = "Replace? <button>Yes</button> <button>No</button> <button>All</button> <button>Stop</button>";
  151. function replaceAll(cm, query, text) {
  152. cm.operation(function() {
  153. for (var cursor = getSearchCursor(cm, query); cursor.findNext();) {
  154. if (typeof query != "string") {
  155. var match = cm.getRange(cursor.from(), cursor.to()).match(query);
  156. cursor.replace(text.replace(/\$(\d)/g, function(_, i) {return match[i];}));
  157. } else cursor.replace(text);
  158. }
  159. });
  160. }
  161. function replace(cm, all) {
  162. if (cm.getOption("readOnly")) return;
  163. var query = cm.getSelection() || getSearchState(cm).lastQuery;
  164. var dialogText = all ? "Replace all:" : "Replace:"
  165. dialog(cm, dialogText + replaceQueryDialog, dialogText, query, function(query) {
  166. if (!query) return;
  167. query = parseQuery(query);
  168. dialog(cm, replacementQueryDialog, "Replace with:", "", function(text) {
  169. text = parseString(text)
  170. if (all) {
  171. replaceAll(cm, query, text)
  172. } else {
  173. clearSearch(cm);
  174. var cursor = getSearchCursor(cm, query, cm.getCursor());
  175. var advance = function() {
  176. var start = cursor.from(), match;
  177. if (!(match = cursor.findNext())) {
  178. cursor = getSearchCursor(cm, query);
  179. if (!(match = cursor.findNext()) ||
  180. (start && cursor.from().line == start.line && cursor.from().ch == start.ch)) return;
  181. }
  182. cm.setSelection(cursor.from(), cursor.to());
  183. cm.scrollIntoView({from: cursor.from(), to: cursor.to()});
  184. confirmDialog(cm, doReplaceConfirm, "Replace?",
  185. [function() {doReplace(match);}, advance,
  186. function() {replaceAll(cm, query, text)}]);
  187. };
  188. var doReplace = function(match) {
  189. cursor.replace(typeof query == "string" ? text :
  190. text.replace(/\$(\d)/g, function(_, i) {return match[i];}));
  191. advance();
  192. };
  193. advance();
  194. }
  195. });
  196. });
  197. }
  198. CodeMirror.commands.find = function(cm) {clearSearch(cm); doSearch(cm);};
  199. CodeMirror.commands.findPersistent = function(cm) {clearSearch(cm); doSearch(cm, false, true);};
  200. CodeMirror.commands.findNext = doSearch;
  201. CodeMirror.commands.findPrev = function(cm) {doSearch(cm, true);};
  202. CodeMirror.commands.clearSearch = clearSearch;
  203. CodeMirror.commands.replace = replace;
  204. CodeMirror.commands.replaceAll = function(cm) {replace(cm, true);};
  205. });