search.js 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  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. query = new RegExp(isRE[1], isRE[2].indexOf("i") == -1 ? "" : "i");
  62. if (query.test("")) query = /x^/;
  63. } else if (query == "") {
  64. query = /x^/;
  65. }
  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. state.posFrom = state.posTo = cm.getCursor();
  81. findNext(cm, rev);
  82. });
  83. });
  84. }
  85. function findNext(cm, rev) {cm.operation(function() {
  86. var state = getSearchState(cm);
  87. var cursor = getSearchCursor(cm, state.query, rev ? state.posFrom : state.posTo);
  88. if (!cursor.find(rev)) {
  89. cursor = getSearchCursor(cm, state.query, rev ? CodeMirror.Pos(cm.lastLine()) : CodeMirror.Pos(cm.firstLine(), 0));
  90. if (!cursor.find(rev)) return;
  91. }
  92. cm.setSelection(cursor.from(), cursor.to());
  93. cm.scrollIntoView({from: cursor.from(), to: cursor.to()});
  94. state.posFrom = cursor.from(); state.posTo = cursor.to();
  95. });}
  96. function clearSearch(cm) {cm.operation(function() {
  97. var state = getSearchState(cm);
  98. if (!state.query) return;
  99. state.query = null;
  100. cm.removeOverlay(state.overlay);
  101. });}
  102. var replaceQueryDialog =
  103. '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>';
  104. var replacementQueryDialog = 'With: <input type="text" style="width: 10em" class="CodeMirror-search-field"/>';
  105. var doReplaceConfirm = "Replace? <button>Yes</button> <button>No</button> <button>Stop</button>";
  106. function replace(cm, all) {
  107. if (cm.getOption("readOnly")) return;
  108. dialog(cm, replaceQueryDialog, "Replace:", cm.getSelection(), function(query) {
  109. if (!query) return;
  110. query = parseQuery(query);
  111. dialog(cm, replacementQueryDialog, "Replace with:", "", function(text) {
  112. if (all) {
  113. cm.operation(function() {
  114. for (var cursor = getSearchCursor(cm, query); cursor.findNext();) {
  115. if (typeof query != "string") {
  116. var match = cm.getRange(cursor.from(), cursor.to()).match(query);
  117. cursor.replace(text.replace(/\$(\d)/g, function(_, i) {return match[i];}));
  118. } else cursor.replace(text);
  119. }
  120. });
  121. } else {
  122. clearSearch(cm);
  123. var cursor = getSearchCursor(cm, query, cm.getCursor());
  124. var advance = function() {
  125. var start = cursor.from(), match;
  126. if (!(match = cursor.findNext())) {
  127. cursor = getSearchCursor(cm, query);
  128. if (!(match = cursor.findNext()) ||
  129. (start && cursor.from().line == start.line && cursor.from().ch == start.ch)) return;
  130. }
  131. cm.setSelection(cursor.from(), cursor.to());
  132. cm.scrollIntoView({from: cursor.from(), to: cursor.to()});
  133. confirmDialog(cm, doReplaceConfirm, "Replace?",
  134. [function() {doReplace(match);}, advance]);
  135. };
  136. var doReplace = function(match) {
  137. cursor.replace(typeof query == "string" ? text :
  138. text.replace(/\$(\d)/g, function(_, i) {return match[i];}));
  139. advance();
  140. };
  141. advance();
  142. }
  143. });
  144. });
  145. }
  146. CodeMirror.commands.find = function(cm) {clearSearch(cm); doSearch(cm);};
  147. CodeMirror.commands.findNext = doSearch;
  148. CodeMirror.commands.findPrev = function(cm) {doSearch(cm, true);};
  149. CodeMirror.commands.clearSearch = clearSearch;
  150. CodeMirror.commands.replace = replace;
  151. CodeMirror.commands.replaceAll = function(cm) {replace(cm, true);};
  152. });