unversioning not used extension in codemirror

This commit is contained in:
Erik Amaru Ortiz
2011-01-20 22:51:21 +00:00
parent 74ed2feafc
commit a048964238
6 changed files with 0 additions and 1168 deletions

View File

@@ -1,74 +0,0 @@
<!doctype html>
<html>
<head>
<title>CodeMirror 2</title>
<link rel="stylesheet" href="lib/codemirror.css">
<script src="lib/codemirror.js"></script>
<script src="mode/javascript/javascript.js"></script>
<link rel="stylesheet" href="mode/javascript/javascript.css">
</head>
<body>
<h1>CodeMirror 2</h1>
<p>This is a new project, aiming to create a CodeMirror-style
component without using an IFRAME.
The <a href="http://ajaxorg.github.com/ace/">ACE</a> editor proved
that this is viable, which motivated me to give it a shot. (Though I took a somewhat different
approach.)</p>
<p>No IE support so far, only tested with recent Chrome, Opera,
and Firefox builds. The JavaScript parser is the only one I've
ported over.</p>
<p>Code at <a href="https://github.com/marijnh/codemirror2">https://github.com/marijnh/codemirror2</a>.</p>
<form><textarea id="code" style="display: none" name="code">
// Demo code (the actual new parser character stream implementation)
function StringStream(string) {
this.pos = 0;
this.string = string;
}
StringStream.prototype = {
done: function() {return this.pos >= this.string.length;},
peek: function() {return this.string.charAt(this.pos);},
next: function() {
if (this.pos &lt; this.string.length)
return this.string.charAt(this.pos++);
},
eat: function(match) {
var ch = this.string.charAt(this.pos);
if (typeof match == "string") var ok = ch == match;
else var ok = ch &amp;&amp; match.test ? match.test(ch) : match(ch);
if (ok) {this.pos++; return ch;}
},
eatWhile: function(match) {
var start = this.pos;
while (this.eat(match));
if (this.pos > start) return this.string.slice(start, this.pos);
},
backUp: function(n) {this.pos -= n;},
column: function() {return this.pos;},
eatSpace: function() {
var start = this.pos;
while (/\s/.test(this.string.charAt(this.pos))) this.pos++;
return this.pos - start;
},
match: function(pattern, consume, caseInsensitive) {
if (typeof pattern == "string") {
function cased(str) {return caseInsensitive ? str.toLowerCase() : str;}
if (cased(this.string).indexOf(cased(pattern), this.pos) == this.pos) {
if (consume !== false) this.pos += str.length;
return true;
}
}
else {
var match = this.string.slice(this.pos).match(pattern);
if (match &amp;&amp; consume !== false) this.pos += match[0].length;
return match;
}
}
};
</textarea></form>
<script>
var editor = CodeMirror.fromTextArea(document.getElementById("code"), {});
</script>
</body>
</html>

View File

@@ -1,4 +0,0 @@
* Mouse drag to copy a region in X
* Indentation
* Line numbers
* Horizontal scrolling

View File

@@ -1,41 +0,0 @@
.CodeMirror {
position: relative;
border: 1px solid black;
}
.CodeMirror-cursor {
width: 1px;
height: 1.2em !important;
background: black;
position: absolute;
margin-top: -.1em;
display: none;
}
.CodeMirror-focused .CodeMirror-cursor {
display: block;
}
.CodeMirror-code {
position: relative;
white-space: pre;
overflow: auto;
height: 300px;
font-family: monospace;
line-height: 1em;
padding: .4em;
}
.CodeMirror-code div {
height: 1em;
min-width: 1px;
}
span.CodeMirror-selected {
background: #bbb !important;
color: white !important;
}
.CodeMirror-focused .CodeMirror-selected {
background: #88f !important;
}

View File

@@ -1,713 +0,0 @@
// TODO handle 0-offsets when editor is hidden
var CodeMirror = (function() {
function Event(orig) {this.e = orig;}
Event.prototype = {
stop: function() {
if (this.e.stopPropagation) this.e.stopPropagation();
else this.e.cancelBubble = true;
if (this.e.preventDefault) this.e.preventDefault();
else this.e.returnValue = false;
},
target: function() {
return this.e.target || this.e.srcElement;
},
button: function() {
if (this.e.which) return this.e.which;
else if (this.e.button & 1) return 1;
else if (this.e.button & 2) return 3;
else if (this.e.button & 4) return 2;
}
};
function connect(node, type, handler, disconnect) {
function wrapHandler(event) {handler(new Event(event || window.event));}
if (typeof node.addEventListener == "function") {
node.addEventListener(type, wrapHandler, false);
if (disconnect) return function() {node.removeEventListener(type, wrapHandler, false);};
}
else {
node.attachEvent("on" + type, wrapHandler);
if (disconnect) return function() {node.detachEvent("on" + type, wrapHandler);};
}
}
var lineSep = window.opera ? "\r\n" : "\n";
function removeElement(node) {
if (node.parentNode) node.parentNode.removeChild(node);
}
function eltOffset(node) {
var x = 0, y = 0;
while (node) {
x += node.offsetLeft; y += node.offsetTop;
node = node.offsetParent;
}
return {left: x, top: y};
}
function addTextSpan(node, text) {
var span = node.appendChild(document.createElement("span"));
span.appendChild(document.createTextNode(text));
return span;
}
function posEq(a, b) {return a.line == b.line && a.ch == b.ch;}
function posLess(a, b) {return a.line < b.line || (a.line == b.line && a.ch < b.ch);}
function copyPos(x) {return {line: x.line, ch: x.ch};}
function htmlEscape(str) {
return str.replace(/[<&]/, function(str) {return str == "&" ? "&amp;" : "&lt;";});
}
var movementKeys = {};
for (var i = 35; i <= 40; i++)
movementKeys[i] = movementKeys["c" + i] = true;
function lineElt(line) {return line.selDiv || line.div;}
function CodeMirror(place, options) {
var div = this.div = document.createElement("div");
if (place.appendChild) place.appendChild(div); else place(div);
div.className = "CodeMirror";
var te = this.input = div.appendChild(document.createElement("textarea"));
te.style.position = "absolute";
te.style.width = "10000px";
te.style.left = te.style.top = "-100000px";
var code = this.code = div.appendChild(document.createElement("div"));
code.className = "CodeMirror-code";
this.cursor = code.appendChild(document.createElement("div"));
this.cursor.className = "CodeMirror-cursor";
this.cursor.style.display = "none";
this.restartBlink();
this.measure = code.appendChild(document.createElement("span"));
this.measure.style.position = "absolute";
this.measure.style.visibility = "hidden";
this.measure.innerHTML = "-";
this.parser = parsers[options.parser || defaultParser];
if (!this.parser) throw new Error("No parser found.");
this.lines = []; this.work = [];
var zero = {line: 0, ch: 0};
this.selection = {from: zero, to: zero}; this.prevSelection = {from: zero, to: zero};
this.$setValue(options.value || "");
this.endOperation();
var self = this;
connect(code, "mousedown", this.operation("onMouseDown"));
connect(te, "keyup", this.operation("onKeyUp"));
connect(te, "keydown", this.operation("onKeyDown"));
connect(te, "focus", function(){self.onFocus();});
connect(te, "blur", function(){self.onBlur();});
connect(code, "dragenter", function(e){e.stop();});
connect(code, "dragover", function(e){e.stop();});
connect(code, "drop", this.operation("onDrop"));
connect(code, "paste", function(e){self.input.focus(); self.schedulePoll(20);});
if (document.activeElement == te) this.onFocus();
else this.onBlur();
}
CodeMirror.prototype = {
$setValue: function(code) {
this.replaceLines(0, this.lines.length, code.split(/\r?\n/g));
},
getValue: function(code) {
var lines = [];
for (var i = 0, l = this.lines.length; i < l; i++)
lines.push(this.lines[i].text);
return lines.join("\n");
},
onMouseDown: function(e) {
// Make sure we're not clicking the scrollbar
var corner = eltOffset(this.code);
if (e.e.pageX - corner.left > this.code.clientWidth ||
e.e.pageY - corner.top > this.code.clientHeight) return;
this.shiftSelecting = null;
var start = this.mouseEventPos(e), last = start, self = this;
this.setCursor(start.line, start.ch, false);
if (e.button() != 1) return;
e.stop();
function end() {
if (!self.focused) {
self.input.focus();
self.onFocus();
self.prepareInput();
}
self.updateInput = true;
move(); up(); leave();
}
var move = connect(this.div, "mousemove", this.operation(function(e) {
var cur = this.clipPos(this.mouseEventPos(e));
if (!posEq(cur, last)) {
last = cur;
this.setSelection(this.clipPos(start), cur);
}
}), true);
var up = connect(this.div, "mouseup", this.operation(function(e) {
this.setSelection(this.clipPos(start), this.clipPos(this.mouseEventPos(e)));
end();
}), true);
// TODO dragging out of window should scroll
var leave = connect(this.div, "mouseout", this.operation(function(e) {
if (e.target() == this.div) end();
}), true);
},
onDrop: function(e) {
try {var text = e.e.dataTransfer.getData("Text");}
catch(e){}
if (!text) return;
var pos = this.clipPos(this.mouseEventPos(e));
this.setSelection(pos, pos);
this.$replaceSelection(text);
},
onKeyDown: function(e) {
if (!this.focused) this.onFocus();
var code = e.e.keyCode, ctrl = e.e.ctrlKey && !e.e.altKey;
if (code == 33 || code == 34) { // page up/down
this.scrollPage(code == 34);
e.stop();
}
else if (ctrl && (code == 36 || code == 35)) { // ctrl-home/end
this.scrollEnd(code == 36);
e.stop();
}
else if (ctrl && code == 65) { // ctrl-a
this.selectAll();
e.stop();
}
else if (!ctrl && code == 13) { // enter
this.insertNewline();
e.stop();
}
else if (!ctrl && code == 9) { // tab
this.handleTab();
e.stop();
}
else if (code == 16) { // shift
this.shiftSelecting = this.selection.inverted ? this.selection.to : this.selection.from;
}
else {
var id = (ctrl ? "c" : "") + code;
if (this.selection.inverted && movementKeys.hasOwnProperty(id)) {
this.reducedSelection = {anchor: this.input.selectionStart};
this.input.selectionEnd = this.input.selectionStart;
}
this.schedulePoll(20, id);
}
},
onKeyUp: function(e) {
if (this.reducedSelection) {
this.reducedSelection = null;
this.prepareInput();
}
if (e.e.keyCode == 16)
this.shiftSelecting = null;
},
onFocus: function() {
this.focused = true;
this.schedulePoll(2000);
if (this.div.className.search(/\bCodeMirror-focused\b/) == -1)
this.div.className += " CodeMirror-focused";
},
onBlur: function() {
this.shiftSelecting = null;
this.focused = false;
this.div.className = this.div.className.replace(" CodeMirror-focused", "");
},
replaceLines: function(from, to, newText) {
// Make sure only changed lines are replaced
var lines = this.lines;
while (from < to && newText[0] == lines[from].text) {
from++; newText.shift();
}
while (to > from + 1 && newText[newText.length-1] == lines[to-1].text) {
to--; newText.pop();
}
// Update this.lines length and the associated DIVs
var lendiff = newText.length - (to - from);
if (lendiff < 0) {
var removed = lines.splice(from, -lendiff);
for (var i = 0, l = removed.length; i < l; i++)
removeElement(lineElt(removed[i]));
}
else if (lendiff > 0) {
var spliceargs = [from, 0], before = lines[from] ? lines[from].div : null;
for (var i = 0; i < lendiff; i++) {
var div = this.code.insertBefore(document.createElement("div"), before);
spliceargs.push({div: div});
}
lines.splice.apply(lines, spliceargs);
}
for (var i = 0, l = newText.length; i < l; i++) {
var line = lines[from + i];
var text = line.text = newText[i];
if (line.selDiv) this.code.replaceChild(line.div, line.selDiv);
line.stateAfter = line.selDiv = null;
line.div.innerHTML = "";
addTextSpan(line.div, line.text);
}
var newWork = [];
for (var i = 0, l = this.work.length; i < l; i++) {
var task = this.work[i];
if (task < from) newWork.push(task);
else if (task >= to) newWork.push(task + lendiff);
}
if (newText.length) newWork.push(from);
this.work = newWork;
this.startWorker(100);
var selLine = this.selection.from.line;
if (lendiff || from != selLine || to != selLine + 1)
this.updateInput = true;
},
schedulePoll: function(time, keyId) {
var self = this;
function pollForKey() {
var state = self.readInput();
if (state == "moved") movementKeys[keyId] = true;
if (state) self.schedulePoll(200, keyId);
else self.schedulePoll(2000);
}
function poll() {
self.readInput();
if (self.focused) self.schedulePoll(2000);
}
clearTimeout(this.pollTimer);
if (time) this.pollTimer = setTimeout(this.operation(keyId ? pollForKey : poll), time);
},
readInput: function() {
var ed = this.editing, changed = false, sel = this.selection, te = this.input;
var text = te.value, selstart = te.selectionStart, selend = te.selectionEnd;
var changed = ed.text != text, rs = this.reducedSelection;
var moved = changed || selstart != ed.start || selend != (rs ? ed.start : ed.end);
if (!moved) return false;
function computeOffset(n, startLine) {
var pos = 0;
while (true) {
var found = text.indexOf("\n", pos);
if (found == -1 || (text.charAt(found-1) == "\r" ? found - 1 : found) >= n)
return {line: startLine, ch: n - pos};
startLine++;
pos = found + 1;
}
}
var from = computeOffset(selstart, ed.from),
to = computeOffset(selend, ed.from);
if (rs) {
from = selstart == rs.anchor ? to : from;
to = this.shiftSelecting ? sel.to : selstart == rs.anchor ? from : to;
if (!posLess(from, to)) {
this.reducedSelection = null;
this.selection.inverted = false;
var tmp = from; from = to; to = tmp;
}
}
if (changed) {
this.shiftSelecting = null;
this.replaceLines(ed.from, ed.to, text.split(/\r?\n/g));
}
ed.text = text; ed.start = selstart; ed.end = selend;
this.setSelection(from, to);
return changed ? "changed" : moved ? "moved" : false;
},
prepareInput: function() {
var sel = this.selection, text = [];
var from = Math.max(0, sel.from.line - 1), to = Math.min(this.lines.length, sel.to.line + 2);
for (var i = from; i < to; i++) text.push(this.lines[i].text);
text = this.input.value = text.join(lineSep);
var startch = sel.from.ch, endch = sel.to.ch;
for (var i = from; i < sel.from.line; i++)
startch += lineSep.length + this.lines[i].text.length;
for (var i = from; i < sel.to.line; i++)
endch += lineSep.length + this.lines[i].text.length;
this.editing = {text: text, from: from, to: to, start: startch, end: endch};
this.input.setSelectionRange(startch, this.reducedSelection ? startch : endch);
},
displaySelection: function() {
var sel = this.selection, pr = this.prevSelection;
if (posEq(sel.from, sel.to)) {
this.cursor.style.display = "";
if (!posEq(pr.from, pr.to)) {
for (var i = pr.from.line; i <= pr.to.line; i++)
this.removeSelectedStyle(i);
}
var linediv = lineElt(this.lines[sel.from.line]);
this.cursor.style.top = linediv.offsetTop + "px";
this.cursor.style.left = (linediv.offsetLeft + this.charWidth() * sel.from.ch) + "px";
}
else {
this.cursor.style.display = "none";
if (!posEq(pr.from, pr.to)) {
for (var i = pr.from.line, e = Math.min(sel.from.line, pr.to.line + 1); i < e; i++)
this.removeSelectedStyle(i);
for (var i = Math.max(sel.to.line + 1, pr.from.line); i <= pr.to.line; i++)
this.removeSelectedStyle(i);
}
if (sel.from.line == sel.to.line) {
this.setSelectedStyle(sel.from.line, sel.from.ch, sel.to.ch);
}
else {
this.setSelectedStyle(sel.from.line, sel.from.ch, null);
for (var i = sel.from.line + 1; i < sel.to.line; i++)
this.setSelectedStyle(i, 0, null);
this.setSelectedStyle(sel.to.line, 0, sel.to.ch);
}
}
var head = sel.inverted ? sel.from : sel.to, headLine = lineElt(this.lines[head.line]);
var ypos = headLine.offsetTop, line = this.lineHeight(),
screen = this.code.clientHeight, screentop = this.code.scrollTop;
if (ypos < screentop)
this.code.scrollTop = Math.max(0, ypos - 10);
else if (ypos + line > screentop + screen)
this.code.scrollTop = (ypos + line + 10) - screen;
var xpos = head.ch * this.charWidth(),
screenw = headLine.offsetWidth, screenleft = this.code.scrollLeft;
if (xpos < screenleft)
this.code.scrollLeft = Math.max(0, xpos - 10);
else if (xpos > screenw + screenleft)
this.code.scrollLeft = (xpos + 10) - screenw;
},
setSelection: function(from, to) {
var sel = this.selection, sh = this.shiftSelecting;
if (posLess(to, from)) {var tmp = to; to = from; from = tmp;}
if (sh) {
if (posLess(sh, from)) from = sh;
else if (posLess(to, sh)) to = sh;
}
var startEq = posEq(sel.to, to), endEq = posEq(sel.from, from);
if (posEq(from, to)) sel.inverted = false;
else if (startEq && !endEq) sel.inverted = true;
else if (endEq && !startEq) sel.inverted = false;
sel.from = from; sel.to = to;
},
setCursor: function(line, ch) {
var pos = this.clipPos({line: line, ch: ch || 0});
this.setSelection(pos, pos);
},
scrollPage: function(down) {
var linesPerPage = Math.floor(this.div.clientHeight / this.lineHeight());
this.setCursor(this.selection.from.line + (Math.max(linesPerPage - 1, 1) * (down ? 1 : -1)));
},
scrollEnd: function(top) {
this.setCursor(top ? 0 : this.lines.length - 1);
},
selectAll: function() {
this.shiftSelecting = null;
var endLine = this.lines.length - 1;
this.setSelection({line: 0, ch: 0}, {line: endLine, ch: this.lines[endLine].text.length});
},
insertNewline: function() {
this.$replaceSelection("\n", "end");
this.indentLine(this.selection.from.line);
},
handleTab: function() {
var sel = this.selection;
for (var i = sel.from.line, e = sel.to.line; i <= e; i++)
this.indentLine(i);
},
indentLine: function(n) {
var state = this.getStateBefore(n);
if (!state) return;
var text = this.lines[n].text;
var curSpace = text.match(/^\s*/)[0].length;
var indentation = this.parser.indent(state, text.slice(curSpace)), diff = indentation - curSpace;
if (!diff) return;
if (diff > 0) {
var space = "";
for (var i = 0; i < diff; i++) space = space + " ";
this.replaceLines(n, n + 1, [space + text]);
}
else
this.replaceLines(n, n + 1, [text.slice(-diff)]);
var from = copyPos(this.selection.from), to = copyPos(this.selection.to);
if (from.line == n) from.ch = Math.max(indentation, from.ch + diff);
if (to.line == n) to.ch = Math.max(indentation, to.ch + diff);
this.setSelection(from, to);
},
$replaceSelection: function(code, collapse) {
var lines = code.split(/\r?\n/g), sel = this.selection;
lines[0] = this.lines[sel.from.line].text.slice(0, sel.from.ch) + lines[0];
var endch = lines[lines.length-1].length;
lines[lines.length-1] += this.lines[sel.to.line].text.slice(sel.to.ch);
var from = sel.from, to = {line: sel.from.line + lines.length - 1, ch: endch};
this.replaceLines(sel.from.line, sel.to.line + 1, lines);
if (collapse == "end") from = to;
else if (collapse == "start") to = from;
this.setSelection(from, to);
},
lineHeight: function() {
var firstline = this.lines[0];
return lineElt(firstline).offsetHeight;
},
charWidth: function() {
return this.measure.offsetWidth || 1;
},
mouseEventPos: function(e) {
var off = eltOffset(lineElt(this.lines[0])),
x = e.e.pageX - off.left + this.code.scrollLeft,
y = e.e.pageY - off.top + this.code.scrollTop;
return {line: Math.floor(y / this.lineHeight()), ch: Math.floor(x / this.charWidth())};
},
clipPos: function(pos) {
pos.line = Math.max(0, Math.min(this.lines.length - 1, pos.line));
pos.ch = Math.max(0, Math.min(this.lines[pos.line].text.length, pos.ch));
return pos;
},
removeSelectedStyle: function(line) {
if (line >= this.lines.length) return;
line = this.lines[line];
if (!line.selDiv) return;
this.code.replaceChild(line.div, line.selDiv);
line.selDiv = null;
},
setSelectedStyle: function(line, start, end) {
line = this.lines[line];
var div = line.selDiv, repl = line.div;
if (div) {
if (div.start == start && div.end == end) return;
repl = div;
}
div = line.selDiv = document.createElement("div");
if (start > 0)
addTextSpan(div, line.text.slice(0, start));
var seltext = line.text.slice(start, end == null ? line.text.length : end);
if (end == null) seltext += " ";
addTextSpan(div, seltext).className = "CodeMirror-selected";
if (end != null && end < line.text.length)
addTextSpan(div, line.text.slice(end));
div.start = start; div.end = end;
this.code.replaceChild(div, repl);
},
restartBlink: function() {
clearInterval(this.blinker);
this.cursor.style.visibility = "";
var self = this;
this.blinker = setInterval(function() {
if (!self.div.parentNode) clearInterval(self.blinker);
var st = self.cursor.style;
st.visibility = st.visibility ? "" : "hidden";
}, 650);
},
highlightLine: function(line, state) {
var stream = new StringStream(line.text), html = [];
while (!stream.done()) {
var start = stream.pos, style = this.parser.token(stream, state, start == 0);
var str = line.text.slice(start, stream.pos);
html.push("<span class=\"" + style + "\">" + htmlEscape(str) + "</span>");
}
// TODO benchmark with removing/cloning/hiding/whatever
line.div.innerHTML = html.join("");
},
getStateBefore: function(n) {
var state;
for (var search = n - 1, lim = n - 40;; search--) {
if (search < 0) {state = this.parser.startState(); break;}
if (search < lim) return null;
if (state = this.lines[search].stateAfter) {state = copyState(state); break;}
}
for (search++; search < n; search++) {
var line = this.lines[search];
this.highlightLine(line, state);
line.stateAfter = copyState(state);
}
if (!this.lines[n].stateAfter) this.work.push(n);
return state;
},
highlightWorker: function(start) {
// TODO have a mode where the document is always parsed to the end
var end = +new Date + 200;
while (this.work.length) {
var task = this.work.pop()
if (this.lines[task].stateAfter) continue;
if (task) {
var state = this.lines[task-1].stateAfter;
if (!state) continue;
state = copyState(state);
}
else var state = this.parser.startState();
for (var i = task, l = this.lines.length; i < l; i++) {
var line = this.lines[i];
if (line.stateAfter) break;
if (+new Date > end) {
this.work.push(i);
this.startWorker(300);
return;
}
this.highlightLine(line, state);
line.stateAfter = copyState(state);
}
}
},
startWorker: function(time) {
if (!this.work.length) return;
var self = this;
clearTimeout(this.highlightTimeout);
this.highlightTimeout = setTimeout(this.operation("highlightWorker"), time);
},
startOperation: function() {
var ps = this.prevSelection, sel = this.selection;
ps.from = sel.from; ps.to = sel.to;
this.updateInput = false;
},
endOperation: function() {
var ps = this.prevSelection, sel = this.selection;
if (!posEq(ps.from, sel.from) || !posEq(ps.to, sel.to)) {
this.displaySelection();
this.restartBlink();
}
if (ps.from.line != sel.from.line || ps.to.line != sel.to.line || this.updateInput)
this.prepareInput();
},
operation: function(f) {
var self = this;
if (typeof f == "string") f = this[f];
return function() {
self.startOperation();
var result = f.apply(self, arguments);
self.endOperation();
return result;
};
}
};
// Wrap API functions as operations
var proto = CodeMirror.prototype;
function apiOp(name) {
var f = proto[name];
proto[name.slice(1)] = function() {
this.startOperation();
return f.apply(this, arguments);
this.endOperation();
};
}
for (var n in proto) if (n.charAt(0) == "$") apiOp(n);
var parsers = {}, defaultParser = null;
CodeMirror.addParser = function(name, parser) {
if (!defaultParser) defaultParser = name;
parsers[name] = parser;
};
CodeMirror.fromTextArea = function(textarea, options) {
if (options && options.value == null)
options.value = textarea.value;
function save() {textarea.value = instance.getValue();}
if (textarea.form) {
var rmSubmit = connect(textarea.form, "submit", save);
var realSubmit = textarea.form.submit;
function wrappedSubmit() {
updateField();
textarea.form.submit = realSubmit;
textarea.form.submit();
textarea.form.submit = wrappedSubmit;
}
textarea.form.submit = wrappedSubmit;
}
textarea.style.display = "none";
var instance = new CodeMirror(function(node) {
textarea.parentNode.insertBefore(node, textarea.nextSibling);
}, options);
instance.save = save;
instance.toTextArea = function() {
save();
textaarea.parentNode.removeChild(instance.div);
textarea.style.display = "";
if (textarea.form) {
textarea.form.submit = realSubmit;
rmSubmit();
}
};
return instance;
};
function StringStream(string) {
this.pos = 0;
this.string = string;
}
StringStream.prototype = {
done: function() {return this.pos >= this.string.length;},
peek: function() {return this.string.charAt(this.pos);},
next: function() {
if (this.pos < this.string.length)
return this.string.charAt(this.pos++);
},
eat: function(match) {
var ch = this.string.charAt(this.pos);
if (typeof match == "string") var ok = ch == match;
else var ok = ch && match.test ? match.test(ch) : match(ch);
if (ok) {this.pos++; return ch;}
},
eatWhile: function(match) {
var start = this.pos;
while (this.eat(match));
if (this.pos > start) return this.string.slice(start, this.pos);
},
backUp: function(n) {this.pos -= n;},
column: function() {return this.pos;},
eatSpace: function() {
var start = this.pos;
while (/\s/.test(this.string.charAt(this.pos))) this.pos++;
return this.pos - start;
},
match: function(pattern, consume, caseInsensitive) {
if (typeof pattern == "string") {
function cased(str) {return caseInsensitive ? str.toLowerCase() : str;}
if (cased(this.string).indexOf(cased(pattern), this.pos) == this.pos) {
if (consume !== false) this.pos += str.length;
return true;
}
}
else {
var match = this.string.slice(this.pos).match(pattern);
if (match && consume !== false) this.pos += match[0].length;
return match;
}
}
};
function copyState(state) {
if (state.copy) return state.copy();
var nstate = {};
for (var n in state) {
var val = state[n];
if (val instanceof Array) val = val.concat([]);
nstate[n] = val;
}
return nstate;
}
return CodeMirror;
})();

View File

@@ -1,8 +0,0 @@
span.js-punctuation {color: #666;}
span.js-operator {color: #666;}
span.js-keyword {color: #90b;}
span.js-atom {color: #281;}
span.js-variabledef {color: #00f;}
span.js-localvariable {color: #049;}
span.js-comment {color: #a70;}
span.js-string {color: #a22;}

View File

@@ -1,328 +0,0 @@
CodeMirror.addParser("javascript", (function() {
// Tokenizer
var keywords = function(){
function kw(type) {return {type: type, style: "js-keyword"};}
var A = kw("keyword a"), B = kw("keyword b"), C = kw("keyword c");
var operator = kw("operator"), atom = {type: "atom", style: "js-atom"};
return {
"if": A, "while": A, "with": A, "else": B, "do": B, "try": B, "finally": B,
"return": C, "break": C, "continue": C, "new": C, "delete": C, "throw": C,
"var": kw("var"), "function": kw("function"), "catch": kw("catch"),
"for": kw("for"), "switch": kw("switch"), "case": kw("case"), "default": kw("default"),
"in": operator, "typeof": operator, "instanceof": operator,
"true": atom, "false": atom, "null": atom, "undefined": atom, "NaN": atom, "Infinity": atom
};
}();
var isOperatorChar = /[+\-*&%=<>!?|]/;
function chain(stream, state, f) {
state.tokenize = f;
return f(stream, state);
}
function nextUntilUnescaped(stream, end) {
var escaped = false, next;
while ((next = stream.next()) != null) {
if (next == end && !escaped)
return false;
escaped = !escaped && next == "\\";
}
return escaped;
}
function jsTokenBase(stream, state) {
function readOperator(ch) {
return {type: "operator", style: "js-operator", content: ch + stream.eatWhile(isOperatorChar)};
}
var ch = stream.next();
if (ch == '"' || ch == "'")
return chain(stream, state, jsTokenString(ch));
else if (/[\[\]{}\(\),;\:\.]/.test(ch))
return {type: ch, style: "js-punctuation"};
else if (ch == "0" && stream.eat(/x/i)) {
while (stream.eat(/[\da-f]/i));
return {type: "number", style: "js-atom"};
}
else if (/\d/.test(ch)) {
stream.match(/^\d*(?:\.\d*)?(?:e[+\-]?\d+)?/);
return {type: "number", style: "js-atom"};
}
else if (ch == "/") {
if (stream.eat("*")) {
return chain(stream, state, jsTokenComment);
}
else if (stream.eat("/")) {
while (stream.next() != null);
return {type: "comment", style: "js-comment"};
}
else if (state.reAllowed) {
nextUntilUnescaped(stream, "/");
while (stream.eat(/[gimy]/)); // 'y' is "sticky" option in Mozilla
return {type: "regexp", style: "js-string"};
}
else return readOperator(ch);
}
else if (isOperatorChar.test(ch))
return readOperator(ch);
else {
var word = ch + stream.eatWhile(/[\w\$_]/);
var known = keywords.propertyIsEnumerable(word) && keywords[word];
return known ? {type: known.type, style: known.style, content: word} :
{type: "variable", style: "js-variable", content: word};
}
}
function jsTokenString(quote) {
return function(stream, state) {
if (!nextUntilUnescaped(stream, quote))
state.tokenize = jsTokenBase;
return {type: "string", style: "js-string"};
};
}
function jsTokenComment(stream, state) {
var maybeEnd = false, ch;
while (ch = stream.next()) {
if (ch == "/" && maybeEnd) {
state.tokenize = jsTokenBase;
break;
}
maybeEnd = (ch == "*");
}
return {type: "comment", style: "js-comment"};
}
// Parser
var atomicTypes = {"atom": true, "number": true, "variable": true, "string": true, "regexp": true};
function JSLexical(indented, column, type, align, prev, info) {
this.indented = indented;
this.column = column;
this.type = type;
this.prev = prev;
this.info = info;
if (align != null) this.align = align;
}
function indentJS(state, textAfter) {
var firstChar = textAfter && textAfter.charAt(0), lexical = state.lexical,
type = lexical.type, closing = firstChar == type, iu = state.indentUnit;
if (type == "vardef") return lexical.indented + 4;
else if (type == "form" && firstChar == "{") return lexical.indented;
else if (type == "stat" || type == "form") return lexical.indented + iu;
else if (lexical.info == "switch" && !closing)
return lexical.indented + (/^(?:case|default)\b/.test(textAfter) ? iu : 2 * iu);
else if (lexical.align) return lexical.column - (closing ? 1 : 0);
else return lexical.indented + (closing ? 0 : iu);
}
function startState(basecolumn, indentUnit) {
if (!indentUnit) indentUnit = 2;
return {
tokenize: jsTokenBase,
reAllowed: true,
cc: [],
lexical: new JSLexical((basecolumn || 0) - indentUnit, 0, "block", false),
context: null,
indented: 0,
indentUnit: indentUnit
};
}
function parseJS(token, column, indent, state) {
var cc = state.cc, type = token.type;
function pass() {
for (var i = arguments.length - 1; i >= 0; i--) cc.push(arguments[i]);
}
var cx = {
state: state,
column: column,
pass: pass,
cont: function(){pass.apply(null, arguments); return true;},
register: function(varname) {
if (state.context) {
cx.marked = "js-variabledef";
state.context.vars[varname] = true;
}
}
};
function inScope(varname) {
var cursor = state.context;
while (cursor) {
if (cursor.vars[varname])
return true;
cursor = cursor.prev;
}
}
if (indent != null) {
if (!state.lexical.hasOwnProperty("align"))
state.lexical.align = false;
state.indented = indent;
}
if (type == "whitespace" || type == "comment")
return token.style;
if (!state.lexical.hasOwnProperty("align"))
state.lexical.align = true;
while(true) {
var combinator = cc.length ? cc.pop() : state.json ? expression : statement;
if (combinator(cx, type, token.content)) {
while(cc.length && cc[cc.length - 1].lex)
cc.pop()(cx);
if (cx.marked) return cx.marked;
if (type == "variable" && inScope(token.content)) return "js-localvariable";
return token.style;
}
}
}
// Combinators
function pushcontext(cx) {
cx.state.context = {prev: cx.state.context, vars: {"this": true, "arguments": true}};
}
function popcontext(cx) {
cx.state.context = cx.state.context.prev;
}
function pushlex(type, info) {
var result = function(cx) {
var state = cx.state;
state.lexical = new JSLexical(state.indented, cx.column, type, null, state.lexical, info)
};
result.lex = true;
return result;
}
function poplex(cx) {
var state = cx.state;
if (state.lexical.type == ")")
state.indented = state.lexical.indented;
state.lexical = state.lexical.prev;
}
poplex.lex = true;
function expect(wanted) {
return function expecting(cx, type) {
if (type == wanted) return cx.cont();
else if (wanted == ";") return;
else return cx.cont(arguments.callee);
};
}
function statement(cx, type) {
if (type == "var") return cx.cont(pushlex("vardef"), vardef1, expect(";"), poplex);
if (type == "keyword a") return cx.cont(pushlex("form"), expression, statement, poplex);
if (type == "keyword b") return cx.cont(pushlex("form"), statement, poplex);
if (type == "{") return cx.cont(pushlex("}"), block, poplex);
if (type == ";") return cx.cont();
if (type == "function") return cx.cont(functiondef);
if (type == "for") return cx.cont(pushlex("form"), expect("("), pushlex(")"), forspec1, expect(")"),
poplex, statement, poplex);
if (type == "variable") return cx.cont(pushlex("stat"), maybelabel);
if (type == "switch") return cx.cont(pushlex("form"), expression, pushlex("}", "switch"), expect("{"),
block, poplex, poplex);
if (type == "case") return cx.cont(expression, expect(":"));
if (type == "default") return cx.cont(expect(":"));
if (type == "catch") return cx.cont(pushlex("form"), pushcontext, expect("("), funarg, expect(")"),
statement, poplex, popcontext);
return cx.pass(pushlex("stat"), expression, expect(";"), poplex);
}
function expression(cx, type) {
if (atomicTypes.hasOwnProperty(type)) return cx.cont(maybeoperator);
if (type == "function") return cx.cont(functiondef);
if (type == "keyword c") return cx.cont(expression);
if (type == "(") return cx.cont(pushlex(")"), expression, expect(")"), poplex, maybeoperator);
if (type == "operator") return cx.cont(expression);
if (type == "[") return cx.cont(pushlex("]"), commasep(expression, "]"), poplex, maybeoperator);
if (type == "{") return cx.cont(pushlex("}"), commasep(objprop, "}"), poplex, maybeoperator);
return cx.cont();
}
function maybeoperator(cx, type, value) {
if (type == "operator" && /\+\+|--/.test(value)) return cx.cont(maybeoperator);
if (type == "operator") return cx.cont(expression);
if (type == ";") return;
if (type == "(") return cx.cont(pushlex(")"), commasep(expression, ")"), poplex, maybeoperator);
if (type == ".") return cx.cont(property, maybeoperator);
if (type == "[") return cx.cont(pushlex("]"), expression, expect("]"), poplex, maybeoperator);
}
function maybelabel(cx, type) {
if (type == ":") return cx.cont(poplex, statement);
return cx.pass(maybeoperator, expect(";"), poplex);
}
function property(cx, type) {
if (type == "variable") {cx.marked = "js-property"; return cx.cont();}
}
function objprop(cx, type) {
if (type == "variable") cx.marked = "js-property";
if (atomicTypes.hasOwnProperty(type)) return cx.cont(expect(":"), expression);
}
function commasep(what, end) {
function proceed(cx, type) {
if (type == ",") return cx.cont(what, proceed);
if (type == end) return cx.cont();
return cx.cont(expect(end));
}
return function commaSeparated(cx, type) {
if (type == end) return cx.cont();
else return cx.pass(what, proceed);
};
}
function block(cx, type) {
if (type == "}") return cx.cont();
return cx.pass(statement, block);
}
function vardef1(cx, type, value) {
if (type == "variable"){cx.register(value); return cx.cont(vardef2);}
return cx.cont();
}
function vardef2(cx, type, value) {
if (value == "=") return cx.cont(expression, vardef2);
if (type == ",") return cx.cont(vardef1);
}
function forspec1(cx, type) {
if (type == "var") return cx.cont(vardef1, forspec2);
if (type == ";") return cx.pass(forspec2);
if (type == "variable") return cx.cont(formaybein);
return cx.pass(forspec2);
}
function formaybein(cx, type, value) {
if (value == "in") return cx.cont(expression);
return cx.cont(maybeoperator, forspec2);
}
function forspec2(cx, type, value) {
if (type == ";") return cx.cont(forspec3);
if (value == "in") return cx.cont(expression);
return cx.cont(expression, expect(";"), forspec3);
}
function forspec3(cx, type) {
if (type != ")") cx.cont(expression);
}
function functiondef(cx, type, value) {
if (type == "variable") {cx.register(value); return cx.cont(functiondef);}
if (type == "(") return cx.cont(pushcontext, commasep(funarg, ")"), statement, popcontext);
}
function funarg(cx, type, value) {
if (type == "variable") {cx.register(value); return cx.cont();}
}
// Interface
return {
startState: startState,
token: function(stream, state) {
if (stream.column() == 0)
var indent = stream.eatSpace();
var token = state.tokenize(stream, state);
state.reAllowed = token.type == "operator" || token.type == "keyword c" || token.type.match(/^[\[{}\(,;:]$/);
stream.eatSpace();
return parseJS(token, stream.column(), indent, state);
},
indent: indentJS
};
})());