Skip to content
Browse files

merge in latest CM2 stuff

  • Loading branch information...
1 parent ac58a32 commit 81c7c4625c4b5487de46a068ff88874669b3cf91 @mzero committed Feb 17, 2011
Showing with 52 additions and 29 deletions.
  1. +52 −29 seed/static/codemirror.js
View
81 seed/static/codemirror.js
@@ -1,15 +1,17 @@
var CodeMirror = (function() {
+ function stopEvent() {
+ if (this.preventDefault) {this.preventDefault(); this.stopPropagation();}
+ else {this.returnValue = false; this.cancelBubble = true;}
+ }
+ function addStop(event) {
+ if (!event.stop) event.stop = stopEvent;
+ return event;
+ }
+
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;
- },
+ stop: function() {stopEvent.call(this.e);},
+ 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;
@@ -42,12 +44,18 @@ var CodeMirror = (function() {
var pre = document.createElement("pre");
pre.innerHTML = " "; return !pre.innerHTML;
})();
- // TODO can this reliably be feature-detected?
- var lineSep = (window.opera || /\bMSIE\b/.test(navigator.userAgent)) ? "\r\n" : "\n";
+ var lineSep = "\n";
+ // Feature-detect whether newlines in textareas are converted to \r\n
+ (function () {
+ var te = document.createElement("textarea");
+ te.value = "foo\nbar";
+ if (te.value.indexOf("\r") > -1) lineSep = "\r\n";
+ }());
var tabSize = 8;
function countIndentation(string) {
var spaceEnd = string.search(/[^\s\u00a0]/), n = 0;
+ if (spaceEnd == -1) spaceEnd = string.length;
for (var i = 0; i < spaceEnd; ++i) {
if (string.charAt(i) == "\t") n += tabSize - (n % tabSize);
else ++n;
@@ -294,10 +302,10 @@ var CodeMirror = (function() {
else {
extraStyle += " " + mark.style;
if (mark.to != null) upto = Math.min(upto, mark.to);
- }
}
- copyUntil(upto, extraStyle);
}
+ copyUntil(upto, extraStyle);
+ }
}
return finish();
}
@@ -379,7 +387,7 @@ var CodeMirror = (function() {
connect(window, "resize", updateDisplay);
connect(input, "keyup", operation(onKeyUp));
connect(input, "keydown", operation(onKeyDown));
- connect(input, "keypress", operation(onKeyPress));
+ connect(input, "keypress", onKeyPress);
connect(input, "focus", onFocus);
connect(input, "blur", onBlur);
@@ -483,22 +491,22 @@ var CodeMirror = (function() {
function onKeyDown(e) {
if (!focused) onFocus();
- var code = e.e.keyCode, done = false;
+ var code = e.e.keyCode;
var mod = (mac ? e.e.metaKey : e.e.ctrlKey) && !e.e.altKey, anyMod = e.e.ctrlKey || e.e.altKey || e.e.metaKey;
if (e.e.shiftKey) shiftSelecting = shiftSelecting || (sel.inverted ? sel.to : sel.from);
else shiftSelecting = null;
-
- if (code == 33 || code == 34) {scrollPage(code == 34); done = true;} // page up/down
- else if (mod && (code == 36 || code == 35)) {scrollEnd(code == 36); done = true;} // ctrl-home/end
- else if (mod && code == 65) {selectAll(); done = true;} // ctrl-a
- else if (!options.readOnly) {
- if (!anyMod && code == 13) {handleEnter(); done = true;} // enter
- else if (!anyMod && code == 9) {if (handleTab(e.e.shiftKey)) done = true;} // tab
- else if (mod && code == 90) {undo(); done = true;} // ctrl-z
- else if (mod && ((e.e.shiftKey && code == 90) || code == 89)) {redo(); done = true;} // ctrl-shift-z, ctrl-y
+ if (options.keyHook && options.keyHook(instance, addStop(e.e))) return;
+
+ if (code == 33 || code == 34) {scrollPage(code == 34); return e.stop();} // page up/down
+ if (mod && (code == 36 || code == 35)) {scrollEnd(code == 36); return e.stop();} // ctrl-home/end
+ if (mod && code == 65) {selectAll(); return e.stop();} // ctrl-a
+ if (!options.readOnly) {
+ if (!anyMod && code == 13) {handleEnter(); return e.stop();} // enter
+ if (!anyMod && code == 9 && handleTab(e.e.shiftKey)) return e.stop(); // tab
+ if (mod && code == 90) {undo(); return e.stop();} // ctrl-z
+ if (mod && ((e.e.shiftKey && code == 90) || code == 89)) {redo(); return e.stop();} // ctrl-shift-z, ctrl-y
}
- if (done) {e.stop(); return;}
var id = (mod ? "c" : "") + code;
if (sel.inverted && movementKeys.hasOwnProperty(id)) {
var range = selRange(input);
@@ -517,6 +525,12 @@ var CodeMirror = (function() {
if (e.e.keyCode == 16) shiftSelecting = null;
}
function onKeyPress(e) {
+ if (options.keyHook && options.keyHook(instance, addStop(e.e))) return;
+ if (options.electricChars && mode.electricChars) {
+ var ch = String.fromCharCode(e.e.charCode == null ? e.e.keyCode : e.e.charCode);
+ if (mode.electricChars.indexOf(ch) > -1)
+ setTimeout(operation(function() {indentLine(sel.to.line, "smart");}), 50);
+ }
var code = e.e.keyCode;
if (code == 13 || (code == 9 && options.tabMode != "default")) e.stop();
}
@@ -854,8 +868,11 @@ var CodeMirror = (function() {
var html = [];
for (var i = showingFrom; i < showingTo; ++i) {
var marker = lines[i].gutterMarker;
- var text = (marker && marker.text && htmlEscape(marker.text)) ||
- (options.lineNumbers && i + options.firstLineNumber) || "\u00a0";
+ var text = options.lineNumbers && i + options.firstLineNumber;
+ if (marker && marker.text)
+ text = marker.text.replace("%N%", text != null ? text : "");
+ else if (text == null)
+ text = "\u00a0";
html.push((marker && marker.style ? '<pre class="' + marker.style + '">' : "<pre>"), text, "</pre>");
}
gutter.style.display = "none";
@@ -962,7 +979,11 @@ var CodeMirror = (function() {
for (var i = sel.from.line, e = sel.to.line; i <= e; ++i) indentLine(i, "smart");
break;
case "classic":
- if (posEq(sel.from, sel.to)) {replaceSelection("\t", "end"); break;}
+ if (posEq(sel.from, sel.to)) {
+ if (shift) indentLine(sel.from.line, "smart");
+ else replaceSelection("\t", "end");
+ break;
+ }
case "shift":
for (var i = sel.from.line, e = sel.to.line; i <= e; ++i) indentLine(i, shift ? "subtract" : "add");
break;
@@ -1188,7 +1209,7 @@ var CodeMirror = (function() {
var ch = match.charAt(0), forward = match.charAt(1) == ">", d = forward ? 1 : -1, st = line.styles;
for (var off = pos + 1, i = 0, e = st.length; i < e; i+=2)
if ((off -= st[i].length) <= 0) {var style = st[i+1]; break;}
-
+
var stack = [line.text.charAt(pos)], re = /[(){}[\]]/;
function scan(line, from, to) {
if (!line.text) return;
@@ -1480,6 +1501,8 @@ var CodeMirror = (function() {
indentWithTabs: false,
tabMode: "classic",
enterMode: "indent",
+ electricChars: true,
+ keyHook: null,
lineNumbers: false,
gutter: false,
firstLineNumber: 1,

0 comments on commit 81c7c46

Please sign in to comment.
Something went wrong with that request. Please try again.