Rearrange source to src dir.
[redakcja.git] / src / redakcja / static / js / lib / codemirror-0.8 / editor.js
1 /* The Editor object manages the content of the editable frame. It
2  * catches events, colours nodes, and indents lines. This file also
3  * holds some functions for transforming arbitrary DOM structures into
4  * plain sequences of <span> and <br> elements
5  */
6
7 var internetExplorer = document.selection && window.ActiveXObject && /MSIE/.test(navigator.userAgent);
8 var webkit = /AppleWebKit/.test(navigator.userAgent);
9 var safari = /Apple Computers, Inc/.test(navigator.vendor);
10 var gecko = /gecko\/(\d{8})/i.test(navigator.userAgent);
11 // TODO this is related to the backspace-at-end-of-line bug. Remove
12 // this if Opera gets their act together, make the version check more
13 // broad if they don't.
14 var brokenOpera = window.opera && /Version\/10.[56]/.test(navigator.userAgent);
15
16 // Make sure a string does not contain two consecutive 'collapseable'
17 // whitespace characters.
18 function makeWhiteSpace(n) {
19   var buffer = [], nb = true;
20   for (; n > 0; n--) {
21     buffer.push((nb || n == 1) ? nbsp : " ");
22     nb ^= true;
23   }
24   return buffer.join("");
25 }
26
27 // Create a set of white-space characters that will not be collapsed
28 // by the browser, but will not break text-wrapping either.
29 function fixSpaces(string) {
30   if (string.charAt(0) == " ") string = nbsp + string.slice(1);
31   return string.replace(/\t/g, function() {return makeWhiteSpace(indentUnit);})
32     .replace(/[ \u00a0]{2,}/g, function(s) {return makeWhiteSpace(s.length);});
33 }
34
35 function cleanText(text) {
36   return text.replace(/\u00a0/g, " ");
37 }
38
39 // Create a SPAN node with the expected properties for document part
40 // spans.
41 function makePartSpan(value, doc) {
42   var text = value;
43   if (value.nodeType == 3) text = value.nodeValue;
44   else value = doc.createTextNode(text);
45
46   var span = doc.createElement("SPAN");
47   span.isPart = true;
48   span.appendChild(value);
49   span.currentText = text;
50   return span;
51 }
52
53 var Editor = (function(){
54   // The HTML elements whose content should be suffixed by a newline
55   // when converting them to flat text.
56   var newlineElements = {"P": true, "DIV": true, "LI": true};
57
58   function asEditorLines(string) {
59     var tab = makeWhiteSpace(indentUnit);
60     return map(string.replace(/\t/g, tab).replace(/\u00a0/g, " ").replace(/\r\n?/g, "\n").split("\n"), fixSpaces);
61   }
62
63   // Helper function for traverseDOM. Flattens an arbitrary DOM node
64   // into an array of textnodes and <br> tags.
65   function simplifyDOM(root, atEnd) {
66     var doc = root.ownerDocument;
67     var result = [];
68     var leaving = true;
69
70     function simplifyNode(node, top) {
71       if (node.nodeType == 3) {
72         var text = node.nodeValue = fixSpaces(node.nodeValue.replace(/\r/g, "").replace(/\n/g, " "));
73         if (text.length) leaving = false;
74         result.push(node);
75       }
76       else if (isBR(node) && node.childNodes.length == 0) {
77         leaving = true;
78         result.push(node);
79       }
80       else {
81         for (var n = node.firstChild; n; n = n.nextSibling) simplifyNode(n);
82         if (!leaving && newlineElements.hasOwnProperty(node.nodeName.toUpperCase())) {
83           leaving = true;
84           if (!atEnd || !top)
85             result.push(doc.createElement("BR"));
86         }
87       }
88     }
89
90     simplifyNode(root, true);
91     return result;
92   }
93
94   // Creates a MochiKit-style iterator that goes over a series of DOM
95   // nodes. The values it yields are strings, the textual content of
96   // the nodes. It makes sure that all nodes up to and including the
97   // one whose text is being yielded have been 'normalized' to be just
98   // <span> and <br> elements.
99   function traverseDOM(start){
100     var owner = start.ownerDocument;
101     var nodeQueue = [];
102
103     // Create a function that can be used to insert nodes after the
104     // one given as argument.
105     function pointAt(node){
106       var parent = node.parentNode;
107       var next = node.nextSibling;
108       return function(newnode) {
109         parent.insertBefore(newnode, next);
110       };
111     }
112     var point = null;
113
114     // This an Opera-specific hack -- always insert an empty span
115     // between two BRs, because Opera's cursor code gets terribly
116     // confused when the cursor is between two BRs.
117     var afterBR = true;
118
119     // Insert a normalized node at the current point. If it is a text
120     // node, wrap it in a <span>, and give that span a currentText
121     // property -- this is used to cache the nodeValue, because
122     // directly accessing nodeValue is horribly slow on some browsers.
123     // The dirty property is used by the highlighter to determine
124     // which parts of the document have to be re-highlighted.
125     function insertPart(part){
126       var text = "\n";
127       if (part.nodeType == 3) {
128         select.snapshotChanged();
129         part = makePartSpan(part, owner);
130         text = part.currentText;
131         afterBR = false;
132       }
133       else {
134         if (afterBR && window.opera)
135           point(makePartSpan("", owner));
136         afterBR = true;
137       }
138       part.dirty = true;
139       nodeQueue.push(part);
140       point(part);
141       return text;
142     }
143
144     // Extract the text and newlines from a DOM node, insert them into
145     // the document, and return the textual content. Used to replace
146     // non-normalized nodes.
147     function writeNode(node, end) {
148       var simplified = simplifyDOM(node, end);
149       for (var i = 0; i < simplified.length; i++)
150         simplified[i] = insertPart(simplified[i]);
151       return simplified.join("");
152     }
153
154     // Check whether a node is a normalized <span> element.
155     function partNode(node){
156       if (node.isPart && node.childNodes.length == 1 && node.firstChild.nodeType == 3) {
157         node.currentText = node.firstChild.nodeValue;
158         return !/[\n\t\r]/.test(node.currentText);
159       }
160       return false;
161     }
162
163     // Advance to next node, return string for current node.
164     function next() {
165       if (!start) throw StopIteration;
166       var node = start;
167       start = node.nextSibling;
168
169       if (partNode(node)){
170         nodeQueue.push(node);
171         afterBR = false;
172         return node.currentText;
173       }
174       else if (isBR(node)) {
175         if (afterBR && window.opera)
176           node.parentNode.insertBefore(makePartSpan("", owner), node);
177         nodeQueue.push(node);
178         afterBR = true;
179         return "\n";
180       }
181       else {
182         var end = !node.nextSibling;
183         point = pointAt(node);
184         removeElement(node);
185         return writeNode(node, end);
186       }
187     }
188
189     // MochiKit iterators are objects with a next function that
190     // returns the next value or throws StopIteration when there are
191     // no more values.
192     return {next: next, nodes: nodeQueue};
193   }
194
195   // Determine the text size of a processed node.
196   function nodeSize(node) {
197     return isBR(node) ? 1 : node.currentText.length;
198   }
199
200   // Search backwards through the top-level nodes until the next BR or
201   // the start of the frame.
202   function startOfLine(node) {
203     while (node && !isBR(node)) node = node.previousSibling;
204     return node;
205   }
206   function endOfLine(node, container) {
207     if (!node) node = container.firstChild;
208     else if (isBR(node)) node = node.nextSibling;
209
210     while (node && !isBR(node)) node = node.nextSibling;
211     return node;
212   }
213
214   function time() {return new Date().getTime();}
215
216   // Client interface for searching the content of the editor. Create
217   // these by calling CodeMirror.getSearchCursor. To use, call
218   // findNext on the resulting object -- this returns a boolean
219   // indicating whether anything was found, and can be called again to
220   // skip to the next find. Use the select and replace methods to
221   // actually do something with the found locations.
222   function SearchCursor(editor, string, fromCursor, caseFold) {
223     this.editor = editor;
224     if (caseFold == undefined) {
225       caseFold = (string == string.toLowerCase());
226     }
227     this.caseFold = caseFold;
228     if (caseFold) string = string.toLowerCase();
229     this.history = editor.history;
230     this.history.commit();
231
232     // Are we currently at an occurrence of the search string?
233     this.atOccurrence = false;
234     // The object stores a set of nodes coming after its current
235     // position, so that when the current point is taken out of the
236     // DOM tree, we can still try to continue.
237     this.fallbackSize = 15;
238     var cursor;
239     // Start from the cursor when specified and a cursor can be found.
240     if (fromCursor && (cursor = select.cursorPos(this.editor.container))) {
241       this.line = cursor.node;
242       this.offset = cursor.offset;
243     }
244     else {
245       this.line = null;
246       this.offset = 0;
247     }
248     this.valid = !!string;
249
250     // Create a matcher function based on the kind of string we have.
251     var target = string.split("\n"), self = this;
252     this.matches = (target.length == 1) ?
253       // For one-line strings, searching can be done simply by calling
254       // indexOf on the current line.
255       function() {
256         var line = cleanText(self.history.textAfter(self.line).slice(self.offset));
257         var match = (self.caseFold ? line.toLowerCase() : line).indexOf(string);
258         if (match > -1)
259           return {from: {node: self.line, offset: self.offset + match},
260                   to: {node: self.line, offset: self.offset + match + string.length}};
261       } :
262       // Multi-line strings require internal iteration over lines, and
263       // some clunky checks to make sure the first match ends at the
264       // end of the line and the last match starts at the start.
265       function() {
266         var firstLine = cleanText(self.history.textAfter(self.line).slice(self.offset));
267         var match = (self.caseFold ? firstLine.toLowerCase() : firstLine).lastIndexOf(target[0]);
268         if (match == -1 || match != firstLine.length - target[0].length)
269           return false;
270         var startOffset = self.offset + match;
271
272         var line = self.history.nodeAfter(self.line);
273         for (var i = 1; i < target.length - 1; i++) {
274           var lineText = cleanText(self.history.textAfter(line));
275           if ((self.caseFold ? lineText.toLowerCase() : lineText) != target[i])
276             return false;
277           line = self.history.nodeAfter(line);
278         }
279
280         var lastLine = cleanText(self.history.textAfter(line));
281         if ((self.caseFold ? lastLine.toLowerCase() : lastLine).indexOf(target[target.length - 1]) != 0)
282           return false;
283
284         return {from: {node: self.line, offset: startOffset},
285                 to: {node: line, offset: target[target.length - 1].length}};
286       };
287   }
288
289   SearchCursor.prototype = {
290     findNext: function() {
291       if (!this.valid) return false;
292       this.atOccurrence = false;
293       var self = this;
294
295       // Go back to the start of the document if the current line is
296       // no longer in the DOM tree.
297       if (this.line && !this.line.parentNode) {
298         this.line = null;
299         this.offset = 0;
300       }
301
302       // Set the cursor's position one character after the given
303       // position.
304       function saveAfter(pos) {
305         if (self.history.textAfter(pos.node).length > pos.offset) {
306           self.line = pos.node;
307           self.offset = pos.offset + 1;
308         }
309         else {
310           self.line = self.history.nodeAfter(pos.node);
311           self.offset = 0;
312         }
313       }
314
315       while (true) {
316         var match = this.matches();
317         // Found the search string.
318         if (match) {
319           this.atOccurrence = match;
320           saveAfter(match.from);
321           return true;
322         }
323         this.line = this.history.nodeAfter(this.line);
324         this.offset = 0;
325         // End of document.
326         if (!this.line) {
327           this.valid = false;
328           return false;
329         }
330       }
331     },
332
333     select: function() {
334       if (this.atOccurrence) {
335         select.setCursorPos(this.editor.container, this.atOccurrence.from, this.atOccurrence.to);
336         select.scrollToCursor(this.editor.container);
337       }
338     },
339
340     replace: function(string) {
341       if (this.atOccurrence) {
342         var end = this.editor.replaceRange(this.atOccurrence.from, this.atOccurrence.to, string);
343         this.line = end.node;
344         this.offset = end.offset;
345         this.atOccurrence = false;
346       }
347     }
348   };
349
350   // The Editor object is the main inside-the-iframe interface.
351   function Editor(options) {
352     this.options = options;
353     window.indentUnit = options.indentUnit;
354     this.parent = parent;
355     this.doc = document;
356     var container = this.container = this.doc.body;
357     this.win = window;
358     this.history = new UndoHistory(container, options.undoDepth, options.undoDelay, this);
359     var self = this;
360
361     if (!Editor.Parser)
362       throw "No parser loaded.";
363     if (options.parserConfig && Editor.Parser.configure)
364       Editor.Parser.configure(options.parserConfig);
365
366     if (!options.readOnly)
367       select.setCursorPos(container, {node: null, offset: 0});
368
369     this.dirty = [];
370     this.importCode(options.content || "");
371     this.history.onChange = options.onChange;
372
373     if (!options.readOnly) {
374       if (options.continuousScanning !== false) {
375         this.scanner = this.documentScanner(options.passTime);
376         this.delayScanning();
377       }
378
379       function setEditable() {
380         // Use contentEditable instead of designMode on IE, since designMode frames
381         // can not run any scripts. It would be nice if we could use contentEditable
382         // everywhere, but it is significantly flakier than designMode on every
383         // single non-IE browser.
384         if (document.body.contentEditable != undefined && internetExplorer)
385           document.body.contentEditable = "true";
386         else
387           document.designMode = "on";
388
389         document.documentElement.style.borderWidth = "0";
390         if (!options.textWrapping)
391           container.style.whiteSpace = "nowrap";
392       }
393
394       // If setting the frame editable fails, try again when the user
395       // focus it (happens when the frame is not visible on
396       // initialisation, in Firefox).
397       try {
398         setEditable();
399       }
400       catch(e) {
401         var focusEvent = addEventHandler(document, "focus", function() {
402           focusEvent();
403           setEditable();
404         }, true);
405       }
406
407       addEventHandler(document, "keydown", method(this, "keyDown"));
408       addEventHandler(document, "keypress", method(this, "keyPress"));
409       addEventHandler(document, "keyup", method(this, "keyUp"));
410
411       function cursorActivity() {self.cursorActivity(false);}
412       addEventHandler(document.body, "mouseup", cursorActivity);
413       addEventHandler(document.body, "cut", cursorActivity);
414
415       // workaround for a gecko bug [?] where going forward and then
416       // back again breaks designmode (no more cursor)
417       if (gecko)
418         addEventHandler(this.win, "pagehide", function(){self.unloaded = true;});
419
420       addEventHandler(document.body, "paste", function(event) {
421         cursorActivity();
422         var text = null;
423         try {
424           var clipboardData = event.clipboardData || window.clipboardData;
425           if (clipboardData) text = clipboardData.getData('Text');
426         }
427         catch(e) {}
428         if (text !== null) {
429           event.stop();
430           self.replaceSelection(text);
431           select.scrollToCursor(self.container);
432         }
433       });
434
435       if (this.options.autoMatchParens)
436         addEventHandler(document.body, "click", method(this, "scheduleParenHighlight"));
437     }
438     else if (!options.textWrapping) {
439       container.style.whiteSpace = "nowrap";
440     }
441   }
442
443   function isSafeKey(code) {
444     return (code >= 16 && code <= 18) || // shift, control, alt
445            (code >= 33 && code <= 40); // arrows, home, end
446   }
447
448   Editor.prototype = {
449     // Import a piece of code into the editor.
450     importCode: function(code) {
451       this.history.push(null, null, asEditorLines(code));
452       this.history.reset();
453     },
454
455     // Extract the code from the editor.
456     getCode: function() {
457       if (!this.container.firstChild)
458         return "";
459
460       var accum = [];
461       select.markSelection(this.win);
462       forEach(traverseDOM(this.container.firstChild), method(accum, "push"));
463       select.selectMarked();
464       return cleanText(accum.join(""));
465     },
466
467     checkLine: function(node) {
468       if (node === false || !(node == null || node.parentNode == this.container))
469         throw parent.CodeMirror.InvalidLineHandle;
470     },
471
472     cursorPosition: function(start) {
473       if (start == null) start = true;
474       var pos = select.cursorPos(this.container, start);
475       if (pos) return {line: pos.node, character: pos.offset};
476       else return {line: null, character: 0};
477     },
478
479     firstLine: function() {
480       return null;
481     },
482
483     lastLine: function() {
484       if (this.container.lastChild) return startOfLine(this.container.lastChild);
485       else return null;
486     },
487
488     nextLine: function(line) {
489       this.checkLine(line);
490       var end = endOfLine(line, this.container);
491       return end || false;
492     },
493
494     prevLine: function(line) {
495       this.checkLine(line);
496       if (line == null) return false;
497       return startOfLine(line.previousSibling);
498     },
499
500     visibleLineCount: function() {
501       var line = this.container.firstChild;
502       while (line && isBR(line)) line = line.nextSibling; // BR heights are unreliable
503       if (!line) return false;
504       var innerHeight = (window.innerHeight
505                          || document.documentElement.clientHeight
506                          || document.body.clientHeight);
507       return Math.floor(innerHeight / line.offsetHeight);
508     },
509
510     selectLines: function(startLine, startOffset, endLine, endOffset) {
511       this.checkLine(startLine);
512       var start = {node: startLine, offset: startOffset}, end = null;
513       if (endOffset !== undefined) {
514         this.checkLine(endLine);
515         end = {node: endLine, offset: endOffset};
516       }
517       select.setCursorPos(this.container, start, end);
518       select.scrollToCursor(this.container);
519     },
520
521     lineContent: function(line) {
522       var accum = [];
523       for (line = line ? line.nextSibling : this.container.firstChild;
524            line && !isBR(line); line = line.nextSibling)
525         accum.push(nodeText(line));
526       return cleanText(accum.join(""));
527     },
528
529     setLineContent: function(line, content) {
530       this.history.commit();
531       this.replaceRange({node: line, offset: 0},
532                         {node: line, offset: this.history.textAfter(line).length},
533                         content);
534       this.addDirtyNode(line);
535       this.scheduleHighlight();
536     },
537
538     removeLine: function(line) {
539       var node = line ? line.nextSibling : this.container.firstChild;
540       while (node) {
541         var next = node.nextSibling;
542         removeElement(node);
543         if (isBR(node)) break;
544         node = next;
545       }
546       this.addDirtyNode(line);
547       this.scheduleHighlight();
548     },
549
550     insertIntoLine: function(line, position, content) {
551       var before = null;
552       if (position == "end") {
553         before = endOfLine(line, this.container);
554       }
555       else {
556         for (var cur = line ? line.nextSibling : this.container.firstChild; cur; cur = cur.nextSibling) {
557           if (position == 0) {
558             before = cur;
559             break;
560           }
561           var text = nodeText(cur);
562           if (text.length > position) {
563             before = cur.nextSibling;
564             content = text.slice(0, position) + content + text.slice(position);
565             removeElement(cur);
566             break;
567           }
568           position -= text.length;
569         }
570       }
571
572       var lines = asEditorLines(content), doc = this.container.ownerDocument;
573       for (var i = 0; i < lines.length; i++) {
574         if (i > 0) this.container.insertBefore(doc.createElement("BR"), before);
575         this.container.insertBefore(makePartSpan(lines[i], doc), before);
576       }
577       this.addDirtyNode(line);
578       this.scheduleHighlight();
579     },
580
581     // Retrieve the selected text.
582     selectedText: function() {
583       var h = this.history;
584       h.commit();
585
586       var start = select.cursorPos(this.container, true),
587           end = select.cursorPos(this.container, false);
588       if (!start || !end) return "";
589
590       if (start.node == end.node)
591         return h.textAfter(start.node).slice(start.offset, end.offset);
592
593       var text = [h.textAfter(start.node).slice(start.offset)];
594       for (var pos = h.nodeAfter(start.node); pos != end.node; pos = h.nodeAfter(pos))
595         text.push(h.textAfter(pos));
596       text.push(h.textAfter(end.node).slice(0, end.offset));
597       return cleanText(text.join("\n"));
598     },
599
600     // Replace the selection with another piece of text.
601     replaceSelection: function(text) {
602       this.history.commit();
603
604       var start = select.cursorPos(this.container, true),
605           end = select.cursorPos(this.container, false);
606       if (!start || !end) return;
607
608       end = this.replaceRange(start, end, text);
609       select.setCursorPos(this.container, end);
610     },
611
612     cursorCoords: function(start) {
613       var sel = select.cursorPos(this.container, start);
614       if (!sel) return null;
615       var off = sel.offset, node = sel.node, doc = this.win.document, self = this;
616       function measureFromNode(node, xOffset) {
617         var y = -(self.win.document.body.scrollTop || self.win.document.documentElement.scrollTop || 0),
618             x = -(self.win.document.body.scrollLeft || self.win.document.documentElement.scrollLeft || 0) + xOffset;
619         forEach([node, self.win.frameElement], function(n) {
620           while (n) {x += n.offsetLeft; y += n.offsetTop;n = n.offsetParent;}
621         });
622         return {x: x, y: y, yBot: y + node.offsetHeight};
623       }
624       function withTempNode(text, f) {
625         var node = doc.createElement("SPAN");
626         node.appendChild(doc.createTextNode(text));
627         try {return f(node);}
628         finally {if (node.parentNode) node.parentNode.removeChild(node);}
629       }
630
631       while (off) {
632         node = node ? node.nextSibling : this.container.firstChild;
633         var txt = nodeText(node);
634         if (off < txt.length)
635           return withTempNode(txt.substr(0, off), function(tmp) {
636             tmp.style.position = "absolute"; tmp.style.visibility = "hidden";
637             tmp.className = node.className;
638             self.container.appendChild(tmp);
639             return measureFromNode(node, tmp.offsetWidth);
640           });
641         off -= txt.length;
642       }
643       if (node && isSpan(node))
644         return measureFromNode(node, node.offsetWidth);
645       else if (node && node.nextSibling && isSpan(node.nextSibling))
646         return measureFromNode(node.nextSibling, 0);
647       else
648         return withTempNode("\u200b", function(tmp) {
649           if (node) node.parentNode.insertBefore(tmp, node.nextSibling);
650           else self.container.insertBefore(tmp, self.container.firstChild);
651           return measureFromNode(tmp, 0);
652         });
653     },
654
655     reroutePasteEvent: function() {
656       if (this.capturingPaste || window.opera) return;
657       this.capturingPaste = true;
658       var te = window.frameElement.CodeMirror.textareaHack;
659       parent.focus();
660       te.value = "";
661       te.focus();
662
663       var self = this;
664       this.parent.setTimeout(function() {
665         self.capturingPaste = false;
666         self.win.focus();
667         if (self.selectionSnapshot) // IE hack
668           self.win.select.setBookmark(self.container, self.selectionSnapshot);
669         var text = te.value;
670         if (text) {
671           self.replaceSelection(text);
672           select.scrollToCursor(self.container);
673         }
674       }, 10);
675     },
676
677     replaceRange: function(from, to, text) {
678       var lines = asEditorLines(text);
679       lines[0] = this.history.textAfter(from.node).slice(0, from.offset) + lines[0];
680       var lastLine = lines[lines.length - 1];
681       lines[lines.length - 1] = lastLine + this.history.textAfter(to.node).slice(to.offset);
682       var end = this.history.nodeAfter(to.node);
683       this.history.push(from.node, end, lines);
684       return {node: this.history.nodeBefore(end),
685               offset: lastLine.length};
686     },
687
688     getSearchCursor: function(string, fromCursor, caseFold) {
689       return new SearchCursor(this, string, fromCursor, caseFold);
690     },
691
692     // Re-indent the whole buffer
693     reindent: function() {
694       if (this.container.firstChild)
695         this.indentRegion(null, this.container.lastChild);
696     },
697
698     reindentSelection: function(direction) {
699       if (!select.somethingSelected(this.win)) {
700         this.indentAtCursor(direction);
701       }
702       else {
703         var start = select.selectionTopNode(this.container, true),
704             end = select.selectionTopNode(this.container, false);
705         if (start === false || end === false) return;
706         this.indentRegion(start, end, direction);
707       }
708     },
709
710     grabKeys: function(eventHandler, filter) {
711       this.frozen = eventHandler;
712       this.keyFilter = filter;
713     },
714     ungrabKeys: function() {
715       this.frozen = "leave";
716     },
717
718     setParser: function(name, parserConfig) {
719       Editor.Parser = window[name];
720       parserConfig = parserConfig || this.options.parserConfig;
721       if (parserConfig && Editor.Parser.configure)
722         Editor.Parser.configure(parserConfig);
723
724       if (this.container.firstChild) {
725         forEach(this.container.childNodes, function(n) {
726           if (n.nodeType != 3) n.dirty = true;
727         });
728         this.addDirtyNode(this.firstChild);
729         this.scheduleHighlight();
730       }
731     },
732
733     // Intercept enter and tab, and assign their new functions.
734     keyDown: function(event) {
735       if (this.frozen == "leave") {this.frozen = null; this.keyFilter = null;}
736       if (this.frozen && (!this.keyFilter || this.keyFilter(event.keyCode, event))) {
737         event.stop();
738         this.frozen(event);
739         return;
740       }
741
742       var code = this.lastKeyDownCode = event.keyCode;
743       // Don't scan when the user is typing.
744       this.delayScanning();
745       // Schedule a paren-highlight event, if configured.
746       if (this.options.autoMatchParens)
747         this.scheduleParenHighlight();
748
749       // The various checks for !altKey are there because AltGr sets both
750       // ctrlKey and altKey to true, and should not be recognised as
751       // Control.
752       if (code == 13) { // enter
753         if (event.ctrlKey && !event.altKey) {
754           this.reparseBuffer();
755         }
756         else {
757           select.insertNewlineAtCursor(this.win);
758           this.indentAtCursor();
759           select.scrollToCursor(this.container);
760         }
761         event.stop();
762       }
763       else if (code == 9 && this.options.tabMode != "default" && !event.ctrlKey) { // tab
764         this.handleTab(!event.shiftKey);
765         event.stop();
766       }
767       else if (code == 32 && event.shiftKey && this.options.tabMode == "default") { // space
768         this.handleTab(true);
769         event.stop();
770       }
771       else if (code == 36 && !event.shiftKey && !event.ctrlKey) { // home
772         if (this.home()) event.stop();
773       }
774       else if (code == 35 && !event.shiftKey && !event.ctrlKey) { // end
775         if (this.end()) event.stop();
776       }
777       // Only in Firefox is the default behavior for PgUp/PgDn correct.
778       else if (code == 33 && !event.shiftKey && !event.ctrlKey && !gecko) { // PgUp
779         if (this.pageUp()) event.stop();
780       }
781       else if (code == 34 && !event.shiftKey && !event.ctrlKey && !gecko) {  // PgDn
782         if (this.pageDown()) event.stop();
783       }
784       else if ((code == 219 || code == 221) && event.ctrlKey && !event.altKey) { // [, ]
785         this.highlightParens(event.shiftKey, true);
786         event.stop();
787       }
788       else if (event.metaKey && !event.shiftKey && (code == 37 || code == 39)) { // Meta-left/right
789         var cursor = select.selectionTopNode(this.container);
790         if (cursor !== false && this.container.firstChild) {
791           if (code == 37) select.focusAfterNode(startOfLine(cursor), this.container);
792           else {
793             var end = endOfLine(cursor, this.container);
794             select.focusAfterNode(end ? end.previousSibling : this.container.lastChild, this.container);
795           }
796           event.stop();
797         }
798       }
799       else if ((event.ctrlKey || event.metaKey) && !event.altKey) {
800         if ((event.shiftKey && code == 90) || code == 89) { // shift-Z, Y
801           select.scrollToNode(this.history.redo());
802           event.stop();
803         }
804         else if (code == 90 || (safari && code == 8)) { // Z, backspace
805           select.scrollToNode(this.history.undo());
806           event.stop();
807         }
808         else if (code == 83 && this.options.saveFunction) { // S
809           this.options.saveFunction();
810           event.stop();
811         }
812         else if (internetExplorer && code == 86) {
813           this.reroutePasteEvent();
814         }
815       }
816       this.keyUpOrPressAfterLastKeyDown = false;
817     },
818
819     // Check for characters that should re-indent the current line,
820     // and prevent Opera from handling enter and tab anyway.
821     keyPress: function(event) {
822       this.keyUpOrPressAfterLastKeyDown = true;
823       var electric = Editor.Parser.electricChars, self = this;
824       // Hack for Opera, and Firefox on OS X, in which stopping a
825       // keydown event does not prevent the associated keypress event
826       // from happening, so we have to cancel enter and tab again
827       // here.
828       if ((this.frozen && (!this.keyFilter || this.keyFilter(event.keyCode || event.code, event))) ||
829           event.code == 13 || (event.code == 9 && this.options.tabMode != "default") ||
830           (event.code == 32 && event.shiftKey && this.options.tabMode == "default"))
831         event.stop();
832       else if (electric && electric.indexOf(event.character) != -1)
833         this.parent.setTimeout(function(){self.indentAtCursor(null);}, 0);
834       else if ((event.character == "v" || event.character == "V")
835                && (event.ctrlKey || event.metaKey) && !event.altKey) // ctrl-V
836         this.reroutePasteEvent();
837       // Work around a bug where pressing backspace at the end of a
838       // line often causes the cursor to jump to the start of the line
839       // in Opera 10.60.
840       else if (brokenOpera && event.code == 8) {
841         var sel = select.selectionTopNode(this.container), self = this,
842             next = sel ? sel.nextSibling : this.container.firstChild;
843         if (sel !== false && next && isBR(next))
844           this.parent.setTimeout(function(){
845             if (select.selectionTopNode(self.container) == next)
846               select.focusAfterNode(next.previousSibling, self.container);
847           }, 20);
848       }
849     },
850
851     // Mark the node at the cursor dirty when a non-safe key is
852     // released.
853     keyUp: function(event) {
854       this.keyUpOrPressAfterLastKeyDown = true;
855       this.cursorActivity(isSafeKey(event.keyCode));
856     },
857
858     // Indent the line following a given <br>, or null for the first
859     // line. If given a <br> element, this must have been highlighted
860     // so that it has an indentation method. Returns the whitespace
861     // element that has been modified or created (if any).
862     indentLineAfter: function(start, direction) {
863       // whiteSpace is the whitespace span at the start of the line,
864       // or null if there is no such node.
865       var whiteSpace = start ? start.nextSibling : this.container.firstChild;
866       if (whiteSpace && !hasClass(whiteSpace, "whitespace"))
867         whiteSpace = null;
868
869       // Sometimes the start of the line can influence the correct
870       // indentation, so we retrieve it.
871       var firstText = whiteSpace ? whiteSpace.nextSibling : (start ? start.nextSibling : this.container.firstChild);
872       var nextChars = (start && firstText && firstText.currentText) ? firstText.currentText : "";
873
874       // Ask the lexical context for the correct indentation, and
875       // compute how much this differs from the current indentation.
876       var newIndent = 0, curIndent = whiteSpace ? whiteSpace.currentText.length : 0;
877       if (direction != null && this.options.tabMode == "shift")
878         newIndent = direction ? curIndent + indentUnit : Math.max(0, curIndent - indentUnit)
879       else if (start)
880         newIndent = start.indentation(nextChars, curIndent, direction);
881       else if (Editor.Parser.firstIndentation)
882         newIndent = Editor.Parser.firstIndentation(nextChars, curIndent, direction);
883       var indentDiff = newIndent - curIndent;
884
885       // If there is too much, this is just a matter of shrinking a span.
886       if (indentDiff < 0) {
887         if (newIndent == 0) {
888           if (firstText) select.snapshotMove(whiteSpace.firstChild, firstText.firstChild, 0);
889           removeElement(whiteSpace);
890           whiteSpace = null;
891         }
892         else {
893           select.snapshotMove(whiteSpace.firstChild, whiteSpace.firstChild, indentDiff, true);
894           whiteSpace.currentText = makeWhiteSpace(newIndent);
895           whiteSpace.firstChild.nodeValue = whiteSpace.currentText;
896         }
897       }
898       // Not enough...
899       else if (indentDiff > 0) {
900         // If there is whitespace, we grow it.
901         if (whiteSpace) {
902           whiteSpace.currentText = makeWhiteSpace(newIndent);
903           whiteSpace.firstChild.nodeValue = whiteSpace.currentText;
904           select.snapshotMove(whiteSpace.firstChild, whiteSpace.firstChild, indentDiff, true);
905         }
906         // Otherwise, we have to add a new whitespace node.
907         else {
908           whiteSpace = makePartSpan(makeWhiteSpace(newIndent), this.doc);
909           whiteSpace.className = "whitespace";
910           if (start) insertAfter(whiteSpace, start);
911           else this.container.insertBefore(whiteSpace, this.container.firstChild);
912           select.snapshotMove(firstText && (firstText.firstChild || firstText),
913                               whiteSpace.firstChild, newIndent, false, true);
914         }
915       }
916       if (indentDiff != 0) this.addDirtyNode(start);
917     },
918
919     // Re-highlight the selected part of the document.
920     highlightAtCursor: function() {
921       var pos = select.selectionTopNode(this.container, true);
922       var to = select.selectionTopNode(this.container, false);
923       if (pos === false || to === false) return false;
924
925       select.markSelection(this.win);
926       if (this.highlight(pos, endOfLine(to, this.container), true, 20) === false)
927         return false;
928       select.selectMarked();
929       return true;
930     },
931
932     // When tab is pressed with text selected, the whole selection is
933     // re-indented, when nothing is selected, the line with the cursor
934     // is re-indented.
935     handleTab: function(direction) {
936       if (this.options.tabMode == "spaces")
937         select.insertTabAtCursor(this.win);
938       else
939         this.reindentSelection(direction);
940     },
941
942     // Custom home behaviour that doesn't land the cursor in front of
943     // leading whitespace unless pressed twice.
944     home: function() {
945       var cur = select.selectionTopNode(this.container, true), start = cur;
946       if (cur === false || !(!cur || cur.isPart || isBR(cur)) || !this.container.firstChild)
947         return false;
948
949       while (cur && !isBR(cur)) cur = cur.previousSibling;
950       var next = cur ? cur.nextSibling : this.container.firstChild;
951       if (next && next != start && next.isPart && hasClass(next, "whitespace"))
952         select.focusAfterNode(next, this.container);
953       else
954         select.focusAfterNode(cur, this.container);
955
956       select.scrollToCursor(this.container);
957       return true;
958     },
959
960     // Some browsers (Opera) don't manage to handle the end key
961     // properly in the face of vertical scrolling.
962     end: function() {
963       var cur = select.selectionTopNode(this.container, true);
964       if (cur === false) return false;
965       cur = endOfLine(cur, this.container);
966       if (!cur) return false;
967       select.focusAfterNode(cur.previousSibling, this.container);
968       select.scrollToCursor(this.container);
969       return true;
970     },
971
972     pageUp: function() {
973       var line = this.cursorPosition().line, scrollAmount = this.visibleLineCount();
974       if (line === false || scrollAmount === false) return false;
975       // Try to keep one line on the screen.
976       scrollAmount -= 2;
977       for (var i = 0; i < scrollAmount; i++) {
978         line = this.prevLine(line);
979         if (line === false) break;
980       }
981       if (i == 0) return false; // Already at first line
982       select.setCursorPos(this.container, {node: line, offset: 0});
983       select.scrollToCursor(this.container);
984       return true;
985     },
986
987     pageDown: function() {
988       var line = this.cursorPosition().line, scrollAmount = this.visibleLineCount();
989       if (line === false || scrollAmount === false) return false;
990       // Try to move to the last line of the current page.
991       scrollAmount -= 2;
992       for (var i = 0; i < scrollAmount; i++) {
993         var nextLine = this.nextLine(line);
994         if (nextLine === false) break;
995         line = nextLine;
996       }
997       if (i == 0) return false; // Already at last line
998       select.setCursorPos(this.container, {node: line, offset: 0});
999       select.scrollToCursor(this.container);
1000       return true;
1001     },
1002
1003     // Delay (or initiate) the next paren highlight event.
1004     scheduleParenHighlight: function() {
1005       if (this.parenEvent) this.parent.clearTimeout(this.parenEvent);
1006       var self = this;
1007       this.parenEvent = this.parent.setTimeout(function(){self.highlightParens();}, 300);
1008     },
1009
1010     // Take the token before the cursor. If it contains a character in
1011     // '()[]{}', search for the matching paren/brace/bracket, and
1012     // highlight them in green for a moment, or red if no proper match
1013     // was found.
1014     highlightParens: function(jump, fromKey) {
1015       var self = this;
1016       // give the relevant nodes a colour.
1017       function highlight(node, ok) {
1018         if (!node) return;
1019         if (self.options.markParen) {
1020           self.options.markParen(node, ok);
1021         }
1022         else {
1023           node.style.fontWeight = "bold";
1024           node.style.color = ok ? "#8F8" : "#F88";
1025         }
1026       }
1027       function unhighlight(node) {
1028         if (!node) return;
1029         if (self.options.unmarkParen) {
1030           self.options.unmarkParen(node);
1031         }
1032         else {
1033           node.style.fontWeight = "";
1034           node.style.color = "";
1035         }
1036       }
1037       if (!fromKey && self.highlighted) {
1038         unhighlight(self.highlighted[0]);
1039         unhighlight(self.highlighted[1]);
1040       }
1041
1042       if (!window.select) return;
1043       // Clear the event property.
1044       if (this.parenEvent) this.parent.clearTimeout(this.parenEvent);
1045       this.parenEvent = null;
1046
1047       // Extract a 'paren' from a piece of text.
1048       function paren(node) {
1049         if (node.currentText) {
1050           var match = node.currentText.match(/^[\s\u00a0]*([\(\)\[\]{}])[\s\u00a0]*$/);
1051           return match && match[1];
1052         }
1053       }
1054       // Determine the direction a paren is facing.
1055       function forward(ch) {
1056         return /[\(\[\{]/.test(ch);
1057       }
1058
1059       var ch, cursor = select.selectionTopNode(this.container, true);
1060       if (!cursor || !this.highlightAtCursor()) return;
1061       cursor = select.selectionTopNode(this.container, true);
1062       if (!(cursor && ((ch = paren(cursor)) || (cursor = cursor.nextSibling) && (ch = paren(cursor)))))
1063         return;
1064       // We only look for tokens with the same className.
1065       var className = cursor.className, dir = forward(ch), match = matching[ch];
1066
1067       // Since parts of the document might not have been properly
1068       // highlighted, and it is hard to know in advance which part we
1069       // have to scan, we just try, and when we find dirty nodes we
1070       // abort, parse them, and re-try.
1071       function tryFindMatch() {
1072         var stack = [], ch, ok = true;
1073         for (var runner = cursor; runner; runner = dir ? runner.nextSibling : runner.previousSibling) {
1074           if (runner.className == className && isSpan(runner) && (ch = paren(runner))) {
1075             if (forward(ch) == dir)
1076               stack.push(ch);
1077             else if (!stack.length)
1078               ok = false;
1079             else if (stack.pop() != matching[ch])
1080               ok = false;
1081             if (!stack.length) break;
1082           }
1083           else if (runner.dirty || !isSpan(runner) && !isBR(runner)) {
1084             return {node: runner, status: "dirty"};
1085           }
1086         }
1087         return {node: runner, status: runner && ok};
1088       }
1089
1090       while (true) {
1091         var found = tryFindMatch();
1092         if (found.status == "dirty") {
1093           this.highlight(found.node, endOfLine(found.node));
1094           // Needed because in some corner cases a highlight does not
1095           // reach a node.
1096           found.node.dirty = false;
1097           continue;
1098         }
1099         else {
1100           highlight(cursor, found.status);
1101           highlight(found.node, found.status);
1102           if (fromKey)
1103             self.parent.setTimeout(function() {unhighlight(cursor); unhighlight(found.node);}, 500);
1104           else
1105             self.highlighted = [cursor, found.node];
1106           if (jump && found.node)
1107             select.focusAfterNode(found.node.previousSibling, this.container);
1108           break;
1109         }
1110       }
1111     },
1112
1113     // Adjust the amount of whitespace at the start of the line that
1114     // the cursor is on so that it is indented properly.
1115     indentAtCursor: function(direction) {
1116       if (!this.container.firstChild) return;
1117       // The line has to have up-to-date lexical information, so we
1118       // highlight it first.
1119       if (!this.highlightAtCursor()) return;
1120       var cursor = select.selectionTopNode(this.container, false);
1121       // If we couldn't determine the place of the cursor,
1122       // there's nothing to indent.
1123       if (cursor === false)
1124         return;
1125       select.markSelection(this.win);
1126       this.indentLineAfter(startOfLine(cursor), direction);
1127       select.selectMarked();
1128     },
1129
1130     // Indent all lines whose start falls inside of the current
1131     // selection.
1132     indentRegion: function(start, end, direction) {
1133       var current = (start = startOfLine(start)), before = start && startOfLine(start.previousSibling);
1134       if (!isBR(end)) end = endOfLine(end, this.container);
1135       this.addDirtyNode(start);
1136
1137       do {
1138         var next = endOfLine(current, this.container);
1139         if (current) this.highlight(before, next, true);
1140         this.indentLineAfter(current, direction);
1141         before = current;
1142         current = next;
1143       } while (current != end);
1144       select.setCursorPos(this.container, {node: start, offset: 0}, {node: end, offset: 0});
1145     },
1146
1147     // Find the node that the cursor is in, mark it as dirty, and make
1148     // sure a highlight pass is scheduled.
1149     cursorActivity: function(safe) {
1150       // pagehide event hack above
1151       if (this.unloaded) {
1152         this.win.document.designMode = "off";
1153         this.win.document.designMode = "on";
1154         this.unloaded = false;
1155       }
1156
1157       if (internetExplorer) {
1158         this.container.createTextRange().execCommand("unlink");
1159         this.selectionSnapshot = select.getBookmark(this.container);
1160       }
1161
1162       var activity = this.options.cursorActivity;
1163       if (!safe || activity) {
1164         var cursor = select.selectionTopNode(this.container, false);
1165         if (cursor === false || !this.container.firstChild) return;
1166         cursor = cursor || this.container.firstChild;
1167         if (activity) activity(cursor);
1168         if (!safe) {
1169           this.scheduleHighlight();
1170           this.addDirtyNode(cursor);
1171         }
1172       }
1173     },
1174
1175     reparseBuffer: function() {
1176       forEach(this.container.childNodes, function(node) {node.dirty = true;});
1177       if (this.container.firstChild)
1178         this.addDirtyNode(this.container.firstChild);
1179     },
1180
1181     // Add a node to the set of dirty nodes, if it isn't already in
1182     // there.
1183     addDirtyNode: function(node) {
1184       node = node || this.container.firstChild;
1185       if (!node) return;
1186
1187       for (var i = 0; i < this.dirty.length; i++)
1188         if (this.dirty[i] == node) return;
1189
1190       if (node.nodeType != 3)
1191         node.dirty = true;
1192       this.dirty.push(node);
1193     },
1194
1195     allClean: function() {
1196       return !this.dirty.length;
1197     },
1198
1199     // Cause a highlight pass to happen in options.passDelay
1200     // milliseconds. Clear the existing timeout, if one exists. This
1201     // way, the passes do not happen while the user is typing, and
1202     // should as unobtrusive as possible.
1203     scheduleHighlight: function() {
1204       // Timeouts are routed through the parent window, because on
1205       // some browsers designMode windows do not fire timeouts.
1206       var self = this;
1207       this.parent.clearTimeout(this.highlightTimeout);
1208       this.highlightTimeout = this.parent.setTimeout(function(){self.highlightDirty();}, this.options.passDelay);
1209     },
1210
1211     // Fetch one dirty node, and remove it from the dirty set.
1212     getDirtyNode: function() {
1213       while (this.dirty.length > 0) {
1214         var found = this.dirty.pop();
1215         // IE8 sometimes throws an unexplainable 'invalid argument'
1216         // exception for found.parentNode
1217         try {
1218           // If the node has been coloured in the meantime, or is no
1219           // longer in the document, it should not be returned.
1220           while (found && found.parentNode != this.container)
1221             found = found.parentNode;
1222           if (found && (found.dirty || found.nodeType == 3))
1223             return found;
1224         } catch (e) {}
1225       }
1226       return null;
1227     },
1228
1229     // Pick dirty nodes, and highlight them, until options.passTime
1230     // milliseconds have gone by. The highlight method will continue
1231     // to next lines as long as it finds dirty nodes. It returns
1232     // information about the place where it stopped. If there are
1233     // dirty nodes left after this function has spent all its lines,
1234     // it shedules another highlight to finish the job.
1235     highlightDirty: function(force) {
1236       // Prevent FF from raising an error when it is firing timeouts
1237       // on a page that's no longer loaded.
1238       if (!window.select) return false;
1239
1240       if (!this.options.readOnly) select.markSelection(this.win);
1241       var start, endTime = force ? null : time() + this.options.passTime;
1242       while ((time() < endTime || force) && (start = this.getDirtyNode())) {
1243         var result = this.highlight(start, endTime);
1244         if (result && result.node && result.dirty)
1245           this.addDirtyNode(result.node);
1246       }
1247       if (!this.options.readOnly) select.selectMarked();
1248       if (start) this.scheduleHighlight();
1249       return this.dirty.length == 0;
1250     },
1251
1252     // Creates a function that, when called through a timeout, will
1253     // continuously re-parse the document.
1254     documentScanner: function(passTime) {
1255       var self = this, pos = null;
1256       return function() {
1257         // FF timeout weirdness workaround.
1258         if (!window.select) return;
1259         // If the current node is no longer in the document... oh
1260         // well, we start over.
1261         if (pos && pos.parentNode != self.container)
1262           pos = null;
1263         select.markSelection(self.win);
1264         var result = self.highlight(pos, time() + passTime, true);
1265         select.selectMarked();
1266         var newPos = result ? (result.node && result.node.nextSibling) : null;
1267         pos = (pos == newPos) ? null : newPos;
1268         self.delayScanning();
1269       };
1270     },
1271
1272     // Starts the continuous scanning process for this document after
1273     // a given interval.
1274     delayScanning: function() {
1275       if (this.scanner) {
1276         this.parent.clearTimeout(this.documentScan);
1277         this.documentScan = this.parent.setTimeout(this.scanner, this.options.continuousScanning);
1278       }
1279     },
1280
1281     isIMEOn: function() {
1282       // chrome:  keyDown keyCode is 229 while IME on
1283       // firefox: no keyUps or keyPresses fires after first keyDown while IME on
1284       return this.lastKeyDownCode == 229 || this.keyUpOrPressAfterLastKeyDown === false;
1285     },
1286
1287     // The function that does the actual highlighting/colouring (with
1288     // help from the parser and the DOM normalizer). Its interface is
1289     // rather overcomplicated, because it is used in different
1290     // situations: ensuring that a certain line is highlighted, or
1291     // highlighting up to X milliseconds starting from a certain
1292     // point. The 'from' argument gives the node at which it should
1293     // start. If this is null, it will start at the beginning of the
1294     // document. When a timestamp is given with the 'target' argument,
1295     // it will stop highlighting at that time. If this argument holds
1296     // a DOM node, it will highlight until it reaches that node. If at
1297     // any time it comes across two 'clean' lines (no dirty nodes), it
1298     // will stop, except when 'cleanLines' is true. maxBacktrack is
1299     // the maximum number of lines to backtrack to find an existing
1300     // parser instance. This is used to give up in situations where a
1301     // highlight would take too long and freeze the browser interface.
1302     highlight: function(from, target, cleanLines, maxBacktrack){
1303       var container = this.container, self = this, active = this.options.activeTokens;
1304       var endTime = (typeof target == "number" ? target : null);
1305
1306       if (!container.firstChild || this.isIMEOn())
1307         return false;
1308       // Backtrack to the first node before from that has a partial
1309       // parse stored.
1310       while (from && (!from.parserFromHere || from.dirty)) {
1311         if (maxBacktrack != null && isBR(from) && (--maxBacktrack) < 0)
1312           return false;
1313         from = from.previousSibling;
1314       }
1315       // If we are at the end of the document, do nothing.
1316       if (from && !from.nextSibling)
1317         return false;
1318
1319       // Check whether a part (<span> node) and the corresponding token
1320       // match.
1321       function correctPart(token, part){
1322         return !part.reduced && part.currentText == token.value && part.className == token.style;
1323       }
1324       // Shorten the text associated with a part by chopping off
1325       // characters from the front. Note that only the currentText
1326       // property gets changed. For efficiency reasons, we leave the
1327       // nodeValue alone -- we set the reduced flag to indicate that
1328       // this part must be replaced.
1329       function shortenPart(part, minus){
1330         part.currentText = part.currentText.substring(minus);
1331         part.reduced = true;
1332       }
1333       // Create a part corresponding to a given token.
1334       function tokenPart(token){
1335         var part = makePartSpan(token.value, self.doc);     
1336         part.className = token.style;
1337         return part;
1338       }
1339
1340       function maybeTouch(node) {
1341         if (node) {
1342           var old = node.oldNextSibling;
1343           if (lineDirty || old === undefined || node.nextSibling != old)
1344             self.history.touch(node);
1345           node.oldNextSibling = node.nextSibling;
1346         }
1347         else {
1348           var old = self.container.oldFirstChild;
1349           if (lineDirty || old === undefined || self.container.firstChild != old)
1350             self.history.touch(null);
1351           self.container.oldFirstChild = self.container.firstChild;
1352         }
1353       }
1354
1355       // Get the token stream. If from is null, we start with a new
1356       // parser from the start of the frame, otherwise a partial parse
1357       // is resumed.
1358       var traversal = traverseDOM(from ? from.nextSibling : container.firstChild),
1359           stream = stringStream(traversal),
1360           parsed = from ? from.parserFromHere(stream) : Editor.Parser.make(stream);
1361
1362       function surroundedByBRs(node) {
1363         return (node.previousSibling == null || isBR(node.previousSibling)) &&
1364                (node.nextSibling == null || isBR(node.nextSibling));
1365       }
1366
1367       // parts is an interface to make it possible to 'delay' fetching
1368       // the next DOM node until we are completely done with the one
1369       // before it. This is necessary because often the next node is
1370       // not yet available when we want to proceed past the current
1371       // one.
1372       var parts = {
1373         current: null,
1374         // Fetch current node.
1375         get: function(){
1376           if (!this.current)
1377             this.current = traversal.nodes.shift();
1378           return this.current;
1379         },
1380         // Advance to the next part (do not fetch it yet).
1381         next: function(){
1382           this.current = null;
1383         },
1384         // Remove the current part from the DOM tree, and move to the
1385         // next.
1386         remove: function(){
1387           container.removeChild(this.get());
1388           this.current = null;
1389         },
1390         // Advance to the next part that is not empty, discarding empty
1391         // parts.
1392         getNonEmpty: function(){
1393           var part = this.get();
1394           // Allow empty nodes when they are alone on a line, needed
1395           // for the FF cursor bug workaround (see select.js,
1396           // insertNewlineAtCursor).
1397           while (part && isSpan(part) && part.currentText == "") {
1398             // Leave empty nodes that are alone on a line alone in
1399             // Opera, since that browsers doesn't deal well with
1400             // having 2 BRs in a row.
1401             if (window.opera && surroundedByBRs(part)) {
1402               this.next();
1403               part = this.get();
1404             }
1405             else {
1406               var old = part;
1407               this.remove();
1408               part = this.get();
1409               // Adjust selection information, if any. See select.js for details.
1410               select.snapshotMove(old.firstChild, part && (part.firstChild || part), 0);
1411             }
1412           }
1413           
1414           return part;
1415         }
1416       };
1417
1418       var lineDirty = false, prevLineDirty = true, lineNodes = 0;
1419
1420       // This forEach loops over the tokens from the parsed stream, and
1421       // at the same time uses the parts object to proceed through the
1422       // corresponding DOM nodes.
1423       forEach(parsed, function(token){
1424         var part = parts.getNonEmpty();
1425
1426         if (token.value == "\n"){
1427           // The idea of the two streams actually staying synchronized
1428           // is such a long shot that we explicitly check.
1429           if (!isBR(part))
1430             throw "Parser out of sync. Expected BR.";
1431
1432           if (part.dirty || !part.indentation) lineDirty = true;
1433           maybeTouch(from);
1434           from = part;
1435
1436           // Every <br> gets a copy of the parser state and a lexical
1437           // context assigned to it. The first is used to be able to
1438           // later resume parsing from this point, the second is used
1439           // for indentation.
1440           part.parserFromHere = parsed.copy();
1441           part.indentation = token.indentation;
1442           part.dirty = false;
1443
1444           // If the target argument wasn't an integer, go at least
1445           // until that node.
1446           if (endTime == null && part == target) throw StopIteration;
1447
1448           // A clean line with more than one node means we are done.
1449           // Throwing a StopIteration is the way to break out of a
1450           // MochiKit forEach loop.
1451           if ((endTime != null && time() >= endTime) || (!lineDirty && !prevLineDirty && lineNodes > 1 && !cleanLines))
1452             throw StopIteration;
1453           prevLineDirty = lineDirty; lineDirty = false; lineNodes = 0;
1454           parts.next();
1455         }
1456         else {
1457           if (!isSpan(part))
1458             throw "Parser out of sync. Expected SPAN.";
1459           if (part.dirty)
1460             lineDirty = true;
1461           lineNodes++;
1462
1463           // If the part matches the token, we can leave it alone.
1464           if (correctPart(token, part)){
1465             part.dirty = false;
1466             parts.next();
1467           }
1468           // Otherwise, we have to fix it.
1469           else {
1470             lineDirty = true;
1471             // Insert the correct part.
1472             var newPart = tokenPart(token);
1473             container.insertBefore(newPart, part);
1474             if (active) active(newPart, token, self);
1475             var tokensize = token.value.length;
1476             var offset = 0;
1477             // Eat up parts until the text for this token has been
1478             // removed, adjusting the stored selection info (see
1479             // select.js) in the process.
1480             while (tokensize > 0) {
1481               part = parts.get();
1482               var partsize = part.currentText.length;
1483               select.snapshotReplaceNode(part.firstChild, newPart.firstChild, tokensize, offset);
1484               if (partsize > tokensize){
1485                 shortenPart(part, tokensize);
1486                 tokensize = 0;
1487               }
1488               else {
1489                 tokensize -= partsize;
1490                 offset += partsize;
1491                 parts.remove();
1492               }
1493             }
1494           }
1495         }
1496       });
1497       maybeTouch(from);
1498
1499       // The function returns some status information that is used by
1500       // hightlightDirty to determine whether and where it has to
1501       // continue.
1502       return {node: parts.getNonEmpty(),
1503               dirty: lineDirty};
1504     }
1505   };
1506
1507   return Editor;
1508 })();
1509
1510 addEventHandler(window, "load", function() {
1511   var CodeMirror = window.frameElement.CodeMirror;
1512   var e = CodeMirror.editor = new Editor(CodeMirror.options);
1513   this.parent.setTimeout(method(CodeMirror, "init"), 0);
1514 });