Skip to content

Commit

Permalink
merge in CM2 latest
Browse files Browse the repository at this point in the history
  • Loading branch information
mzero committed Feb 19, 2011
1 parent 3bc2304 commit d496d48
Showing 1 changed file with 33 additions and 25 deletions.
58 changes: 33 additions & 25 deletions seed/static/codemirror.js
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,9 @@ var CodeMirror = (function() {
for (var n = node; n != document.body; n = n.parentNode) {x -= n.scrollLeft; y -= n.scrollTop;}
return {left: x, top: y};
}
function eltText(node) {
return node.textContent || node.innerText || node.nodeValue || "";
}

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);}
Expand Down Expand Up @@ -164,11 +167,11 @@ var CodeMirror = (function() {
function copyStyles(from, to, source, dest) {
for (var i = 0, pos = 0, state = 0; pos < to; i+=2) {
var part = source[i], end = pos + part.length;
if (state === 0) {
if (state == 0) {
if (end > from) dest.push(part.slice(from - pos, Math.min(part.length, to - pos)), source[i+1]);
if (end >= from) state = 1;
}
else if (state === 1) {
else if (state == 1) {
if (end > to) dest.push(part.slice(0, to - pos), source[i+1]);
else dest.push(part, source[i+1]);
}
Expand Down Expand Up @@ -243,11 +246,15 @@ var CodeMirror = (function() {
},
getTokenAt: function(mode, state, ch) {
var txt = this.text, stream = new StringStream(txt);
while (stream.pos <= ch && !stream.eol()) {
while (stream.pos < ch && !stream.eol()) {
stream.start = stream.pos;
var style = mode.token(stream, state);
}
return {start: stream.start, end: stream.pos, string: stream.current(), className: style || null};
return {start: stream.start,
end: stream.pos,
string: stream.current(),
className: style || null,
state: state};
},
indentation: function() {return countIndentation(this.text);},
getHTML: function(sfrom, sto, includePre) {
Expand All @@ -257,16 +264,12 @@ var CodeMirror = (function() {
if (style) html.push('<span class="', style, '">', htmlEscape(text), "</span>");
else html.push(htmlEscape(text));
}
function finish() {
if (includePre) html.push("</pre>");
return html.join("");
}
var st = this.styles, allText = this.text, marked = this.marked;
if (sfrom === sto) sfrom = null;
if (sfrom == sto) sfrom = null;

if (!allText)
span(" ");
else if (!marked && sfrom === null)
else if (!marked && sfrom == null)
for (var i = 0, e = st.length; i < e; i+=2) span(st[i], st[i+1]);
else {
var pos = 0, i = 0, text = "", style, sg = 0;
Expand Down Expand Up @@ -310,7 +313,8 @@ var CodeMirror = (function() {
copyUntil(upto, extraStyle);
}
}
return finish();
if (includePre) html.push("</pre>");
return html.join("");
}
};

Expand Down Expand Up @@ -385,7 +389,7 @@ var CodeMirror = (function() {
operation(function(){setValue(options.value || ""); updateInput = false;})();
setTimeout(prepareInput, 20);

connect(code, "mousedown", operation(onMouseDown));
connect(div, "mousedown", operation(onMouseDown));
connect(code, "dblclick", operation(onDblClick));
connect(div, "scroll", updateDisplay);
connect(window, "resize", updateDisplay);
Expand All @@ -399,7 +403,7 @@ var CodeMirror = (function() {
connect(div, "dragover", function(e){e.stop();});
connect(div, "drop", operation(onDrop));
connect(div, "paste", function(){input.focus(); fastPoll();});
connect(input, "paste", fastPoll);
connect(input, "paste", function(){fastPoll();});

if (options.blockDocumentScroll) {
function stopScroll(e) {
Expand Down Expand Up @@ -429,17 +433,18 @@ var CodeMirror = (function() {
}

function onMouseDown(e) {
var start = posFromMouse(e), last = start, target = e.target(), going;
if (!start) return;
setCursor(start.line, start.ch, false);
if (e.button() != 1) return;
for (var n = target; n != mover; n = n.parentNode)
for (var n = e.target(); n != div; n = n.parentNode)
if (n.parentNode == gutterText) {
if (options.onGutterClick)
options.onGutterClick(instance, indexOf(gutterText.childNodes, n) + showingFrom);
return;
}

var start = posFromMouse(e), last = start, going;
if (!start) return;
setCursor(start.line, start.ch, false);
if (e.button() != 1) return;

if (!focused) onFocus();
e.stop();
function end() {
Expand Down Expand Up @@ -530,7 +535,7 @@ var CodeMirror = (function() {
setSelRange(input, range.start, range.start);
}
}
fastPoll(20, id);
fastPoll(id);
}
function onKeyUp(e) {
if (reducedSelection) {
Expand Down Expand Up @@ -894,9 +899,9 @@ var CodeMirror = (function() {
}
gutter.style.display = "none";
gutterText.innerHTML = html.join("");
var minwidth = String(lines.length).length, firstNode = gutterText.firstChild.firstChild, val = firstNode.nodeValue;
while (val.length < minwidth) val = "\u00a0" + val;
firstNode.nodeValue = val;
var minwidth = String(lines.length).length, firstNode = gutterText.firstChild, val = eltText(firstNode), pad = "";
while (val.length + pad.length < minwidth) pad += "\u00a0";
if (pad) firstNode.insertBefore(document.createTextNode(pad), firstNode.firstChild);
gutter.style.display = "";
lineWrap.style.marginLeft = gutter.offsetWidth + "px";
}
Expand Down Expand Up @@ -1166,7 +1171,9 @@ var CodeMirror = (function() {
var off = eltOffset(lineWrap),
x = e.pageX() - off.left,
y = e.pageY() - off.top;
if (e.target() == code && y < (lines.length * lineHeight())) return null;
if (!(e.target() == div && y > (lines.length * lineHeight())))
for (var n = e.target(); n != lineDiv; n = n.parentNode)
if (!n || n == div) return null;
var line = showingFrom + Math.floor(y / lineHeight());
return clipPos({line: line, ch: charFromX(clipLine(line), x)});
}
Expand Down Expand Up @@ -1446,11 +1453,11 @@ var CodeMirror = (function() {
setValue: operation(setValue),
getSelection: getSelection,
replaceSelection: operation(replaceSelection),
focus: function(){input.focus(); onFocus();},
focus: function(){input.focus(); onFocus(); fastPoll();},
setOption: function(option, value) {
options[option] = value;
if (option == "lineNumbers" || option == "gutter") gutterChanged();
else if (option == "mode") setMode(value);
else if (option == "mode" || option == "indentUnit") setMode(value);
},
getOption: function(option) {return options[option];},
undo: operation(undo),
Expand All @@ -1477,6 +1484,7 @@ var CodeMirror = (function() {

lineCount: function() {return lines.length;},
getCursor: function(start) {return copyPos(start ? sel.from : sel.to);},
somethingSelected: function() {return !posEq(sel.from, sel.to);},
setCursor: operation(function(line, ch) {
if (ch == null && typeof line.line == "number") setCursor(line.line, line.ch);
else setCursor(line, ch);
Expand Down

0 comments on commit d496d48

Please sign in to comment.