1
0

search.js 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  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;
  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.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 dialog(cm, text, shortText, deflt, f) {
  51. if (cm.openDialog) cm.openDialog(text, f, {value: deflt});
  52. else f(prompt(shortText, deflt));
  53. }
  54. function confirmDialog(cm, text, shortText, fs) {
  55. if (cm.openConfirm) cm.openConfirm(text, fs);
  56. else if (confirm(shortText)) fs[0]();
  57. }
  58. function parseQuery(query) {
  59. var isRE = query.match(/^\/(.*)\/([a-z]*)$/);
  60. if (isRE) {
  61. try { query = new RegExp(isRE[1], isRE[2].indexOf("i") == -1 ? "" : "i"); }
  62. catch(e) {} // Not a regular expression after all, do a string search
  63. }
  64. if (typeof query == "string" ? query == "" : query.test(""))
  65. query = /x^/;
  66. return query;
  67. }
  68. var queryDialog =
  69. '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>';
  70. function doSearch(cm, rev) {
  71. var state = getSearchState(cm);
  72. if (state.query) return findNext(cm, rev);
  73. dialog(cm, queryDialog, "Search for:", cm.getSelection(), function(query) {
  74. cm.operation(function() {
  75. if (!query || state.query) return;
  76. state.query = parseQuery(query);
  77. cm.removeOverlay(state.overlay, queryCaseInsensitive(state.query));
  78. state.overlay = searchOverlay(state.query, queryCaseInsensitive(state.query));
  79. cm.addOverlay(state.overlay);
  80. if (cm.showMatchesOnScrollbar) {
  81. if (state.annotate) { state.annotate.clear(); state.annotate = null; }
  82. state.annotate = cm.showMatchesOnScrollbar(state.query, queryCaseInsensitive(state.query));
  83. }
  84. state.posFrom = state.posTo = cm.getCursor();
  85. findNext(cm, rev);
  86. });
  87. });
  88. }
  89. function findNext(cm, rev) {cm.operation(function() {
  90. var state = getSearchState(cm);
  91. var cursor = getSearchCursor(cm, state.query, rev ? state.posFrom : state.posTo);
  92. if (!cursor.find(rev)) {
  93. cursor = getSearchCursor(cm, state.query, rev ? CodeMirror.Pos(cm.lastLine()) : CodeMirror.Pos(cm.firstLine(), 0));
  94. if (!cursor.find(rev)) return;
  95. }
  96. cm.setSelection(cursor.from(), cursor.to());
  97. cm.scrollIntoView({from: cursor.from(), to: cursor.to()});
  98. state.posFrom = cursor.from(); state.posTo = cursor.to();
  99. });}
  100. function clearSearch(cm) {cm.operation(function() {
  101. var state = getSearchState(cm);
  102. if (!state.query) return;
  103. state.query = null;
  104. cm.removeOverlay(state.overlay);
  105. if (state.annotate) { state.annotate.clear(); state.annotate = null; }
  106. });}
  107. var replaceQueryDialog =
  108. 'Replace: <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>';
  109. var replacementQueryDialog = 'With: <input type="text" style="width: 10em" class="CodeMirror-search-field"/>';
  110. var doReplaceConfirm = "Replace? <button>Yes</button> <button>No</button> <button>Stop</button>";
  111. function replace(cm, all) {
  112. if (cm.getOption("readOnly")) return;
  113. dialog(cm, replaceQueryDialog, "Replace:", cm.getSelection(), function(query) {
  114. if (!query) return;
  115. query = parseQuery(query);
  116. dialog(cm, replacementQueryDialog, "Replace with:", "", function(text) {
  117. if (all) {
  118. cm.operation(function() {
  119. for (var cursor = getSearchCursor(cm, query); cursor.findNext();) {
  120. if (typeof query != "string") {
  121. var match = cm.getRange(cursor.from(), cursor.to()).match(query);
  122. cursor.replace(text.replace(/\$(\d)/g, function(_, i) {return match[i];}));
  123. } else cursor.replace(text);
  124. }
  125. });
  126. } else {
  127. clearSearch(cm);
  128. var cursor = getSearchCursor(cm, query, cm.getCursor());
  129. var advance = function() {
  130. var start = cursor.from(), match;
  131. if (!(match = cursor.findNext())) {
  132. cursor = getSearchCursor(cm, query);
  133. if (!(match = cursor.findNext()) ||
  134. (start && cursor.from().line == start.line && cursor.from().ch == start.ch)) return;
  135. }
  136. cm.setSelection(cursor.from(), cursor.to());
  137. cm.scrollIntoView({from: cursor.from(), to: cursor.to()});
  138. confirmDialog(cm, doReplaceConfirm, "Replace?",
  139. [function() {doReplace(match);}, advance]);
  140. };
  141. var doReplace = function(match) {
  142. cursor.replace(typeof query == "string" ? text :
  143. text.replace(/\$(\d)/g, function(_, i) {return match[i];}));
  144. advance();
  145. };
  146. advance();
  147. }
  148. });
  149. });
  150. }
  151. CodeMirror.commands.find = function(cm) {clearSearch(cm); doSearch(cm);};
  152. CodeMirror.commands.findNext = doSearch;
  153. CodeMirror.commands.findPrev = function(cm) {doSearch(cm, true);};
  154. CodeMirror.commands.clearSearch = clearSearch;
  155. CodeMirror.commands.replace = replace;
  156. CodeMirror.commands.replaceAll = function(cm) {replace(cm, true);};
  157. });