search.js 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. // Define search commands. Depends on dialog.js or another
  2. // implementation of the openDialog method.
  3. // Replace works a little oddly -- it will do the replace on the next
  4. // Ctrl-G (or whatever is bound to findNext) press. You prevent a
  5. // replace by making sure the match is no longer selected when hitting
  6. // Ctrl-G.
  7. (function(mod) {
  8. if (typeof exports == "object" && typeof module == "object") // CommonJS
  9. mod(require("../../lib/codemirror"), require("./searchcursor"), require("../dialog/dialog"));
  10. else if (typeof define == "function" && define.amd) // AMD
  11. define(["../../lib/codemirror", "./searchcursor", "../dialog/dialog"], mod);
  12. else // Plain browser env
  13. mod(CodeMirror);
  14. })(function(CodeMirror) {
  15. "use strict";
  16. function searchOverlay(query, caseInsensitive) {
  17. var startChar;
  18. if (typeof query == "string") {
  19. startChar = query.charAt(0);
  20. query = new RegExp("^" + query.replace(/[\-\[\]\/\{\}\(\)\*\+\?\.\\\^\$\|]/g, "\\$&"),
  21. caseInsensitive ? "i" : "");
  22. } else {
  23. query = new RegExp("^(?:" + query.source + ")", query.ignoreCase ? "i" : "");
  24. }
  25. return {token: function(stream) {
  26. if (stream.match(query)) return "searching";
  27. while (!stream.eol()) {
  28. stream.next();
  29. if (startChar && !caseInsensitive)
  30. stream.skipTo(startChar) || stream.skipToEnd();
  31. if (stream.match(query, false)) break;
  32. }
  33. }};
  34. }
  35. function SearchState() {
  36. this.posFrom = this.posTo = this.query = null;
  37. this.overlay = null;
  38. }
  39. function getSearchState(cm) {
  40. return cm.state.search || (cm.state.search = new SearchState());
  41. }
  42. function queryCaseInsensitive(query) {
  43. return typeof query == "string" && query == query.toLowerCase();
  44. }
  45. function getSearchCursor(cm, query, pos) {
  46. // Heuristic: if the query string is all lowercase, do a case insensitive search.
  47. return cm.getSearchCursor(query, pos, queryCaseInsensitive(query));
  48. }
  49. function dialog(cm, text, shortText, deflt, f) {
  50. if (cm.openDialog) cm.openDialog(text, f, {value: deflt});
  51. else f(prompt(shortText, deflt));
  52. }
  53. function confirmDialog(cm, text, shortText, fs) {
  54. if (cm.openConfirm) cm.openConfirm(text, fs);
  55. else if (confirm(shortText)) fs[0]();
  56. }
  57. function parseQuery(query) {
  58. var isRE = query.match(/^\/(.*)\/([a-z]*)$/);
  59. if (isRE) {
  60. query = new RegExp(isRE[1], isRE[2].indexOf("i") == -1 ? "" : "i");
  61. if (query.test("")) query = /x^/;
  62. } else if (query == "") {
  63. query = /x^/;
  64. }
  65. return query;
  66. }
  67. var queryDialog =
  68. 'Search: <input type="text" style="width: 10em"/> <span style="color: #888">(Use /re/ syntax for regexp search)</span>';
  69. function doSearch(cm, rev) {
  70. var state = getSearchState(cm);
  71. if (state.query) return findNext(cm, rev);
  72. dialog(cm, queryDialog, "Search for:", cm.getSelection(), function(query) {
  73. cm.operation(function() {
  74. if (!query || state.query) return;
  75. state.query = parseQuery(query);
  76. cm.removeOverlay(state.overlay, queryCaseInsensitive(state.query));
  77. state.overlay = searchOverlay(state.query, queryCaseInsensitive(state.query));
  78. cm.addOverlay(state.overlay);
  79. state.posFrom = state.posTo = cm.getCursor();
  80. findNext(cm, rev);
  81. });
  82. });
  83. }
  84. function findNext(cm, rev) {cm.operation(function() {
  85. var state = getSearchState(cm);
  86. var cursor = getSearchCursor(cm, state.query, rev ? state.posFrom : state.posTo);
  87. if (!cursor.find(rev)) {
  88. cursor = getSearchCursor(cm, state.query, rev ? CodeMirror.Pos(cm.lastLine()) : CodeMirror.Pos(cm.firstLine(), 0));
  89. if (!cursor.find(rev)) return;
  90. }
  91. cm.setSelection(cursor.from(), cursor.to());
  92. cm.scrollIntoView({from: cursor.from(), to: cursor.to()});
  93. state.posFrom = cursor.from(); state.posTo = cursor.to();
  94. });}
  95. function clearSearch(cm) {cm.operation(function() {
  96. var state = getSearchState(cm);
  97. if (!state.query) return;
  98. state.query = null;
  99. cm.removeOverlay(state.overlay);
  100. });}
  101. var replaceQueryDialog =
  102. 'Replace: <input type="text" style="width: 10em"/> <span style="color: #888">(Use /re/ syntax for regexp search)</span>';
  103. var replacementQueryDialog = 'With: <input type="text" style="width: 10em"/>';
  104. var doReplaceConfirm = "Replace? <button>Yes</button> <button>No</button> <button>Stop</button>";
  105. function replace(cm, all) {
  106. dialog(cm, replaceQueryDialog, "Replace:", cm.getSelection(), function(query) {
  107. if (!query) return;
  108. query = parseQuery(query);
  109. dialog(cm, replacementQueryDialog, "Replace with:", "", function(text) {
  110. if (all) {
  111. cm.operation(function() {
  112. for (var cursor = getSearchCursor(cm, query); cursor.findNext();) {
  113. if (typeof query != "string") {
  114. var match = cm.getRange(cursor.from(), cursor.to()).match(query);
  115. cursor.replace(text.replace(/\$(\d)/g, function(_, i) {return match[i];}));
  116. } else cursor.replace(text);
  117. }
  118. });
  119. } else {
  120. clearSearch(cm);
  121. var cursor = getSearchCursor(cm, query, cm.getCursor());
  122. var advance = function() {
  123. var start = cursor.from(), match;
  124. if (!(match = cursor.findNext())) {
  125. cursor = getSearchCursor(cm, query);
  126. if (!(match = cursor.findNext()) ||
  127. (start && cursor.from().line == start.line && cursor.from().ch == start.ch)) return;
  128. }
  129. cm.setSelection(cursor.from(), cursor.to());
  130. cm.scrollIntoView({from: cursor.from(), to: cursor.to()});
  131. confirmDialog(cm, doReplaceConfirm, "Replace?",
  132. [function() {doReplace(match);}, advance]);
  133. };
  134. var doReplace = function(match) {
  135. cursor.replace(typeof query == "string" ? text :
  136. text.replace(/\$(\d)/g, function(_, i) {return match[i];}));
  137. advance();
  138. };
  139. advance();
  140. }
  141. });
  142. });
  143. }
  144. CodeMirror.commands.find = function(cm) {clearSearch(cm); doSearch(cm);};
  145. CodeMirror.commands.findNext = doSearch;
  146. CodeMirror.commands.findPrev = function(cm) {doSearch(cm, true);};
  147. CodeMirror.commands.clearSearch = clearSearch;
  148. CodeMirror.commands.replace = replace;
  149. CodeMirror.commands.replaceAll = function(cm) {replace(cm, true);};
  150. });