search.js 5.0 KB

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