Subversion-Projekte lars-tiefland.cienc

Revision

Details | Letzte Änderung | Log anzeigen | RSS feed

Revision Autor Zeilennr. Zeile
5 lars 1
// CodeMirror, copyright (c) by Marijn Haverbeke and others
2
// Distributed under an MIT license: http://codemirror.net/LICENSE
3
 
4
// Define search commands. Depends on dialog.js or another
5
// implementation of the openDialog method.
6
 
7
// Replace works a little oddly -- it will do the replace on the next
8
// Ctrl-G (or whatever is bound to findNext) press. You prevent a
9
// replace by making sure the match is no longer selected when hitting
10
// Ctrl-G.
11
 
12
(function(mod) {
13
  if (typeof exports == "object" && typeof module == "object") // CommonJS
14
    mod(require("../../lib/codemirror"), require("./searchcursor"), require("../dialog/dialog"));
15
  else if (typeof define == "function" && define.amd) // AMD
16
    define(["../../lib/codemirror", "./searchcursor", "../dialog/dialog"], mod);
17
  else // Plain browser env
18
    mod(CodeMirror);
19
})(function(CodeMirror) {
20
  "use strict";
21
 
22
  function searchOverlay(query, caseInsensitive) {
23
    if (typeof query == "string")
24
      query = new RegExp(query.replace(/[\-\[\]\/\{\}\(\)\*\+\?\.\\\^\$\|]/g, "\\$&"), caseInsensitive ? "gi" : "g");
25
    else if (!query.global)
26
      query = new RegExp(query.source, query.ignoreCase ? "gi" : "g");
27
 
28
    return {token: function(stream) {
29
      query.lastIndex = stream.pos;
30
      var match = query.exec(stream.string);
31
      if (match && match.index == stream.pos) {
32
        stream.pos += match[0].length;
33
        return "searching";
34
      } else if (match) {
35
        stream.pos = match.index;
36
      } else {
37
        stream.skipToEnd();
38
      }
39
    }};
40
  }
41
 
42
  function SearchState() {
43
    this.posFrom = this.posTo = this.lastQuery = this.query = null;
44
    this.overlay = null;
45
  }
46
 
47
  function getSearchState(cm) {
48
    return cm.state.search || (cm.state.search = new SearchState());
49
  }
50
 
51
  function queryCaseInsensitive(query) {
52
    return typeof query == "string" && query == query.toLowerCase();
53
  }
54
 
55
  function getSearchCursor(cm, query, pos) {
56
    // Heuristic: if the query string is all lowercase, do a case insensitive search.
57
    return cm.getSearchCursor(query, pos, queryCaseInsensitive(query));
58
  }
59
 
60
  function persistentDialog(cm, text, deflt, f) {
61
    cm.openDialog(text, f, {
62
      value: deflt,
63
      selectValueOnOpen: true,
64
      closeOnEnter: false,
65
      onClose: function() { clearSearch(cm); }
66
    });
67
  }
68
 
69
  function dialog(cm, text, shortText, deflt, f) {
70
    if (cm.openDialog) cm.openDialog(text, f, {value: deflt, selectValueOnOpen: true});
71
    else f(prompt(shortText, deflt));
72
  }
73
 
74
  function confirmDialog(cm, text, shortText, fs) {
75
    if (cm.openConfirm) cm.openConfirm(text, fs);
76
    else if (confirm(shortText)) fs[0]();
77
  }
78
 
79
  function parseString(string) {
80
    return string.replace(/\\(.)/g, function(_, ch) {
81
      if (ch == "n") return "\n"
82
      if (ch == "r") return "\r"
83
      return ch
84
    })
85
  }
86
 
87
  function parseQuery(query) {
88
    var isRE = query.match(/^\/(.*)\/([a-z]*)$/);
89
    if (isRE) {
90
      try { query = new RegExp(isRE[1], isRE[2].indexOf("i") == -1 ? "" : "i"); }
91
      catch(e) {} // Not a regular expression after all, do a string search
92
    } else {
93
      query = parseString(query)
94
    }
95
    if (typeof query == "string" ? query == "" : query.test(""))
96
      query = /x^/;
97
    return query;
98
  }
99
 
100
  var queryDialog =
101
    '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>';
102
 
103
  function startSearch(cm, state, query) {
104
    state.queryText = query;
105
    state.query = parseQuery(query);
106
    cm.removeOverlay(state.overlay, queryCaseInsensitive(state.query));
107
    state.overlay = searchOverlay(state.query, queryCaseInsensitive(state.query));
108
    cm.addOverlay(state.overlay);
109
    if (cm.showMatchesOnScrollbar) {
110
      if (state.annotate) { state.annotate.clear(); state.annotate = null; }
111
      state.annotate = cm.showMatchesOnScrollbar(state.query, queryCaseInsensitive(state.query));
112
    }
113
  }
114
 
115
  function doSearch(cm, rev, persistent) {
116
    var state = getSearchState(cm);
117
    if (state.query) return findNext(cm, rev);
118
    var q = cm.getSelection() || state.lastQuery;
119
    if (persistent && cm.openDialog) {
120
      persistentDialog(cm, queryDialog, q, function(query, event) {
121
        CodeMirror.e_stop(event);
122
        if (!query) return;
123
        if (query != state.queryText) startSearch(cm, state, query);
124
        findNext(cm, event.shiftKey);
125
      });
126
    } else {
127
      dialog(cm, queryDialog, "Search for:", q, function(query) {
128
        if (query && !state.query) cm.operation(function() {
129
          startSearch(cm, state, query);
130
          state.posFrom = state.posTo = cm.getCursor();
131
          findNext(cm, rev);
132
        });
133
      });
134
    }
135
  }
136
 
137
  function findNext(cm, rev) {cm.operation(function() {
138
    var state = getSearchState(cm);
139
    var cursor = getSearchCursor(cm, state.query, rev ? state.posFrom : state.posTo);
140
    if (!cursor.find(rev)) {
141
      cursor = getSearchCursor(cm, state.query, rev ? CodeMirror.Pos(cm.lastLine()) : CodeMirror.Pos(cm.firstLine(), 0));
142
      if (!cursor.find(rev)) return;
143
    }
144
    cm.setSelection(cursor.from(), cursor.to());
145
    cm.scrollIntoView({from: cursor.from(), to: cursor.to()}, 20);
146
    state.posFrom = cursor.from(); state.posTo = cursor.to();
147
  });}
148
 
149
  function clearSearch(cm) {cm.operation(function() {
150
    var state = getSearchState(cm);
151
    state.lastQuery = state.query;
152
    if (!state.query) return;
153
    state.query = state.queryText = null;
154
    cm.removeOverlay(state.overlay);
155
    if (state.annotate) { state.annotate.clear(); state.annotate = null; }
156
  });}
157
 
158
  var replaceQueryDialog =
159
    '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>';
160
  var replacementQueryDialog = 'With: <input type="text" style="width: 10em" class="CodeMirror-search-field"/>';
161
  var doReplaceConfirm = "Replace? <button>Yes</button> <button>No</button> <button>Stop</button>";
162
 
163
  function replace(cm, all) {
164
    if (cm.getOption("readOnly")) return;
165
    var query = cm.getSelection() || getSearchState(cm).lastQuery;
166
    dialog(cm, replaceQueryDialog, "Replace:", query, function(query) {
167
      if (!query) return;
168
      query = parseQuery(query);
169
      dialog(cm, replacementQueryDialog, "Replace with:", "", function(text) {
170
        text = parseString(text)
171
        if (all) {
172
          cm.operation(function() {
173
            for (var cursor = getSearchCursor(cm, query); cursor.findNext();) {
174
              if (typeof query != "string") {
175
                var match = cm.getRange(cursor.from(), cursor.to()).match(query);
176
                cursor.replace(text.replace(/\$(\d)/g, function(_, i) {return match[i];}));
177
              } else cursor.replace(text);
178
            }
179
          });
180
        } else {
181
          clearSearch(cm);
182
          var cursor = getSearchCursor(cm, query, cm.getCursor());
183
          var advance = function() {
184
            var start = cursor.from(), match;
185
            if (!(match = cursor.findNext())) {
186
              cursor = getSearchCursor(cm, query);
187
              if (!(match = cursor.findNext()) ||
188
                  (start && cursor.from().line == start.line && cursor.from().ch == start.ch)) return;
189
            }
190
            cm.setSelection(cursor.from(), cursor.to());
191
            cm.scrollIntoView({from: cursor.from(), to: cursor.to()});
192
            confirmDialog(cm, doReplaceConfirm, "Replace?",
193
                          [function() {doReplace(match);}, advance]);
194
          };
195
          var doReplace = function(match) {
196
            cursor.replace(typeof query == "string" ? text :
197
                           text.replace(/\$(\d)/g, function(_, i) {return match[i];}));
198
            advance();
199
          };
200
          advance();
201
        }
202
      });
203
    });
204
  }
205
 
206
  CodeMirror.commands.find = function(cm) {clearSearch(cm); doSearch(cm);};
207
  CodeMirror.commands.findPersistent = function(cm) {clearSearch(cm); doSearch(cm, false, true);};
208
  CodeMirror.commands.findNext = doSearch;
209
  CodeMirror.commands.findPrev = function(cm) {doSearch(cm, true);};
210
  CodeMirror.commands.clearSearch = clearSearch;
211
  CodeMirror.commands.replace = replace;
212
  CodeMirror.commands.replaceAll = function(cm) {replace(cm, true);};
213
});