1
0

search.js 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  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() {
  8. function searchOverlay(query) {
  9. if (typeof query == "string") return {token: function(stream) {
  10. if (stream.match(query)) return "searching";
  11. stream.next();
  12. stream.skipTo(query.charAt(0)) || stream.skipToEnd();
  13. }};
  14. return {token: function(stream) {
  15. if (stream.match(query)) return "searching";
  16. while (!stream.eol()) {
  17. stream.next();
  18. if (stream.match(query, false)) break;
  19. }
  20. }};
  21. }
  22. function SearchState() {
  23. this.posFrom = this.posTo = this.query = null;
  24. this.overlay = null;
  25. }
  26. function getSearchState(cm) {
  27. return cm.state.search || (cm.state.search = new SearchState());
  28. }
  29. function getSearchCursor(cm, query, pos) {
  30. // Heuristic: if the query string is all lowercase, do a case insensitive search.
  31. return cm.getSearchCursor(query, pos, typeof query == "string" && query == query.toLowerCase());
  32. }
  33. function dialog(cm, text, shortText, f) {
  34. if (cm.openDialog) cm.openDialog(text, f);
  35. else f(prompt(shortText, ""));
  36. }
  37. function confirmDialog(cm, text, shortText, fs) {
  38. if (cm.openConfirm) cm.openConfirm(text, fs);
  39. else if (confirm(shortText)) fs[0]();
  40. }
  41. function parseQuery(query) {
  42. var isRE = query.match(/^\/(.*)\/([a-z]*)$/);
  43. return isRE ? new RegExp(isRE[1], isRE[2].indexOf("i") == -1 ? "" : "i") : query;
  44. }
  45. var queryDialog =
  46. 'Search: <input type="text" style="width: 10em"/> <span style="color: #888">(Use /re/ syntax for regexp search)</span>';
  47. function doSearch(cm, rev) {
  48. var state = getSearchState(cm);
  49. if (state.query) return findNext(cm, rev);
  50. dialog(cm, queryDialog, "Search for:", function(query) {
  51. cm.operation(function() {
  52. if (!query || state.query) return;
  53. state.query = parseQuery(query);
  54. cm.removeOverlay(state.overlay);
  55. state.overlay = searchOverlay(query);
  56. cm.addOverlay(state.overlay);
  57. state.posFrom = state.posTo = cm.getCursor();
  58. findNext(cm, rev);
  59. });
  60. });
  61. }
  62. function findNext(cm, rev) {cm.operation(function() {
  63. var state = getSearchState(cm);
  64. var cursor = getSearchCursor(cm, state.query, rev ? state.posFrom : state.posTo);
  65. if (!cursor.find(rev)) {
  66. cursor = getSearchCursor(cm, state.query, rev ? CodeMirror.Pos(cm.lastLine()) : CodeMirror.Pos(cm.firstLine(), 0));
  67. if (!cursor.find(rev)) return;
  68. }
  69. cm.setSelection(cursor.from(), cursor.to());
  70. state.posFrom = cursor.from(); state.posTo = cursor.to();
  71. });}
  72. function clearSearch(cm) {cm.operation(function() {
  73. var state = getSearchState(cm);
  74. if (!state.query) return;
  75. state.query = null;
  76. cm.removeOverlay(state.overlay);
  77. });}
  78. var replaceQueryDialog =
  79. 'Replace: <input type="text" style="width: 10em"/> <span style="color: #888">(Use /re/ syntax for regexp search)</span>';
  80. var replacementQueryDialog = 'With: <input type="text" style="width: 10em"/>';
  81. var doReplaceConfirm = "Replace? <button>Yes</button> <button>No</button> <button>Stop</button>";
  82. function replace(cm, all) {
  83. dialog(cm, replaceQueryDialog, "Replace:", function(query) {
  84. if (!query) return;
  85. query = parseQuery(query);
  86. dialog(cm, replacementQueryDialog, "Replace with:", function(text) {
  87. if (all) {
  88. cm.operation(function() {
  89. for (var cursor = getSearchCursor(cm, query); cursor.findNext();) {
  90. if (typeof query != "string") {
  91. var match = cm.getRange(cursor.from(), cursor.to()).match(query);
  92. cursor.replace(text.replace(/\$(\d)/, function(_, i) {return match[i];}));
  93. } else cursor.replace(text);
  94. }
  95. });
  96. } else {
  97. clearSearch(cm);
  98. var cursor = getSearchCursor(cm, query, cm.getCursor());
  99. var advance = function() {
  100. var start = cursor.from(), match;
  101. if (!(match = cursor.findNext())) {
  102. cursor = getSearchCursor(cm, query);
  103. if (!(match = cursor.findNext()) ||
  104. (start && cursor.from().line == start.line && cursor.from().ch == start.ch)) return;
  105. }
  106. cm.setSelection(cursor.from(), cursor.to());
  107. confirmDialog(cm, doReplaceConfirm, "Replace?",
  108. [function() {doReplace(match);}, advance]);
  109. };
  110. var doReplace = function(match) {
  111. cursor.replace(typeof query == "string" ? text :
  112. text.replace(/\$(\d)/, function(_, i) {return match[i];}));
  113. advance();
  114. };
  115. advance();
  116. }
  117. });
  118. });
  119. }
  120. CodeMirror.commands.find = function(cm) {clearSearch(cm); doSearch(cm);};
  121. CodeMirror.commands.findNext = doSearch;
  122. CodeMirror.commands.findPrev = function(cm) {doSearch(cm, true);};
  123. CodeMirror.commands.clearSearch = clearSearch;
  124. CodeMirror.commands.replace = replace;
  125. CodeMirror.commands.replaceAll = function(cm) {replace(cm, true);};
  126. })();