search.js 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  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(), "CodeMirror-searching"));
  43. }
  44. state.posFrom = state.posTo = cm.getCursor();
  45. findNext(cm, rev);
  46. });
  47. });
  48. }
  49. function findNext(cm, rev) {cm.operation(function() {
  50. var state = getSearchState(cm);
  51. var cursor = getSearchCursor(cm, state.query, rev ? state.posFrom : state.posTo);
  52. if (!cursor.find(rev)) {
  53. cursor = getSearchCursor(cm, state.query, rev ? {line: cm.lineCount() - 1} : {line: 0, ch: 0});
  54. if (!cursor.find(rev)) return;
  55. }
  56. cm.setSelection(cursor.from(), cursor.to());
  57. state.posFrom = cursor.from(); state.posTo = cursor.to();
  58. });}
  59. function clearSearch(cm) {cm.operation(function() {
  60. var state = getSearchState(cm);
  61. if (!state.query) return;
  62. state.query = null;
  63. for (var i = 0; i < state.marked.length; ++i) state.marked[i].clear();
  64. state.marked.length = 0;
  65. });}
  66. var replaceQueryDialog =
  67. 'Replace: <input type="text" style="width: 10em"/> <span style="color: #888">(Use /re/ syntax for regexp search)</span>';
  68. var replacementQueryDialog = 'With: <input type="text" style="width: 10em"/>';
  69. var doReplaceConfirm = "Replace? <button>Yes</button> <button>No</button> <button>Stop</button>";
  70. function replace(cm, all) {
  71. dialog(cm, replaceQueryDialog, "Replace:", function(query) {
  72. if (!query) return;
  73. query = parseQuery(query);
  74. dialog(cm, replacementQueryDialog, "Replace with:", function(text) {
  75. if (all) {
  76. cm.compoundChange(function() { cm.operation(function() {
  77. for (var cursor = getSearchCursor(cm, query); cursor.findNext();) {
  78. if (typeof query != "string") {
  79. var match = cm.getRange(cursor.from(), cursor.to()).match(query);
  80. cursor.replace(text.replace(/\$(\d)/, function(w, i) {return match[i];}));
  81. } else cursor.replace(text);
  82. }
  83. });});
  84. } else {
  85. clearSearch(cm);
  86. var cursor = getSearchCursor(cm, query, cm.getCursor());
  87. function advance() {
  88. var start = cursor.from(), match;
  89. if (!(match = cursor.findNext())) {
  90. cursor = getSearchCursor(cm, query);
  91. if (!(match = cursor.findNext()) ||
  92. (start && cursor.from().line == start.line && cursor.from().ch == start.ch)) return;
  93. }
  94. cm.setSelection(cursor.from(), cursor.to());
  95. confirmDialog(cm, doReplaceConfirm, "Replace?",
  96. [function() {doReplace(match);}, advance]);
  97. }
  98. function doReplace(match) {
  99. cursor.replace(typeof query == "string" ? text :
  100. text.replace(/\$(\d)/, function(w, i) {return match[i];}));
  101. advance();
  102. }
  103. advance();
  104. }
  105. });
  106. });
  107. }
  108. CodeMirror.commands.find = function(cm) {clearSearch(cm); doSearch(cm);};
  109. CodeMirror.commands.findNext = doSearch;
  110. CodeMirror.commands.findPrev = function(cm) {doSearch(cm, true);};
  111. CodeMirror.commands.clearSearch = clearSearch;
  112. CodeMirror.commands.replace = replace;
  113. CodeMirror.commands.replaceAll = function(cm) {replace(cm, true);};
  114. })();